final.c: Use rtx_sequence
[official-gcc.git] / gcc / ada / sem_ch3.adb
blobdd71672d39b0988d16dafe5b8c2b9ef7ab75ad83
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 3 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
43 with Layout; use Layout;
44 with Lib; use Lib;
45 with Lib.Xref; use Lib.Xref;
46 with Namet; use Namet;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Case; use Sem_Case;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch10; use Sem_Ch10;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Prag; use Sem_Prag;
68 with Sem_Res; use Sem_Res;
69 with Sem_Smem; use Sem_Smem;
70 with Sem_Type; use Sem_Type;
71 with Sem_Util; use Sem_Util;
72 with Sem_Warn; use Sem_Warn;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Targparm; use Targparm;
78 with Tbuild; use Tbuild;
79 with Ttypes; use Ttypes;
80 with Uintp; use Uintp;
81 with Urealp; use Urealp;
83 package body Sem_Ch3 is
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
89 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
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.
94 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
95 -- Analyze all delayed pragmas chained on the contract of object Obj_Id as
96 -- if they appeared at the end of the declarative region. The pragmas to be
97 -- considered are:
98 -- Async_Readers
99 -- Async_Writers
100 -- Effective_Reads
101 -- Effective_Writes
102 -- Part_Of
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);
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
115 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
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).
126 -- ??? These flags need a bit of re-examination and re-documentation:
127 -- ??? are they both necessary (both seem related to the recursion)?
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.
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.
145 procedure Build_Derived_Concurrent_Type
146 (N : Node_Id;
147 Parent_Type : Entity_Id;
148 Derived_Type : Entity_Id);
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.
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
159 -- Character and [Wide_]Wide_Character are special-cased.
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.
168 procedure Build_Derived_Private_Type
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Is_Completion : Boolean;
173 Derive_Subps : Boolean := True);
174 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
175 -- because the parent may or may not have a completion, and the derivation
176 -- may itself be a completion.
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);
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
187 -- N_Private_Extension_Declaration node. See the definition of this routine
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).
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
199 -- initialization procedures are built.
201 function Build_Discriminant_Constraints
202 (T : Entity_Id;
203 Def : Node_Id;
204 Derived_Def : Boolean := False) return Elist_Id;
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.
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
224 -- T (a possibly discriminated base type)
225 -- Def_Id (a very partially built subtype for T),
227 -- the call completes Def_Id to be the appropriate E_*_Subtype.
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
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.
238 function Build_Scalar_Bound
239 (Bound : Node_Id;
240 Par_T : Entity_Id;
241 Der_T : Entity_Id) return Node_Id;
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???
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).
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.
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.
280 procedure Check_Delta_Expression (E : Node_Id);
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.
284 procedure Check_Digits_Expression (E : Node_Id);
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.
288 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
289 -- Validate the initialization of an object declaration. T is the required
290 -- type, and Exp is the initialization expression.
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)
295 procedure Check_Or_Process_Discriminants
296 (N : Node_Id;
297 T : Entity_Id;
298 Prev : Entity_Id := Empty);
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.
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.
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.
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
322 procedure Convert_Scalar_Bounds
323 (N : Node_Id;
324 Parent_Type : Entity_Id;
325 Derived_Type : Entity_Id;
326 Loc : Source_Ptr);
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.
335 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
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.
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.
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
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
355 -- constraints for Typ.
357 function Constrain_Component_Type
358 (Comp : Entity_Id;
359 Constrained_Typ : Entity_Id;
360 Related_Node : Node_Id;
361 Typ : Entity_Id;
362 Constraints : Elist_Id) return Entity_Id;
363 -- Given a discriminated base type Typ, a list of discriminant constraints,
364 -- Constraints, for Typ and a component Comp of Typ, create and return the
365 -- type corresponding to Etype (Comp) where all discriminant references
366 -- are replaced with the corresponding constraint. If Etype (Comp) contains
367 -- no discriminant references then it is returned as-is. Constrained_Typ
368 -- is the final constrained subtype to which the constrained component
369 -- belongs. Related_Node is the node where we attach all created itypes.
371 procedure Constrain_Access
372 (Def_Id : in out Entity_Id;
373 S : Node_Id;
374 Related_Nod : Node_Id);
375 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
376 -- an anonymous type created for a subtype indication. In that case it is
377 -- created in the procedure and attached to Related_Nod.
379 procedure Constrain_Array
380 (Def_Id : in out Entity_Id;
381 SI : Node_Id;
382 Related_Nod : Node_Id;
383 Related_Id : Entity_Id;
384 Suffix : Character);
385 -- Apply a list of index constraints to an unconstrained array type. The
386 -- first parameter is the entity for the resulting subtype. A value of
387 -- Empty for Def_Id indicates that an implicit type must be created, but
388 -- creation is delayed (and must be done by this procedure) because other
389 -- subsidiary implicit types must be created first (which is why Def_Id
390 -- is an in/out parameter). The second parameter is a subtype indication
391 -- node for the constrained array to be created (e.g. something of the
392 -- form string (1 .. 10)). Related_Nod gives the place where this type
393 -- has to be inserted in the tree. The Related_Id and Suffix parameters
394 -- are used to build the associated Implicit type name.
396 procedure Constrain_Concurrent
397 (Def_Id : in out Entity_Id;
398 SI : Node_Id;
399 Related_Nod : Node_Id;
400 Related_Id : Entity_Id;
401 Suffix : Character);
402 -- Apply list of discriminant constraints to an unconstrained concurrent
403 -- type.
405 -- SI is the N_Subtype_Indication node containing the constraint and
406 -- the unconstrained type to constrain.
408 -- Def_Id is the entity for the resulting constrained subtype. A value
409 -- of Empty for Def_Id indicates that an implicit type must be created,
410 -- but creation is delayed (and must be done by this procedure) because
411 -- other subsidiary implicit types must be created first (which is why
412 -- Def_Id is an in/out parameter).
414 -- Related_Nod gives the place where this type has to be inserted
415 -- in the tree.
417 -- The last two arguments are used to create its external name if needed.
419 function Constrain_Corresponding_Record
420 (Prot_Subt : Entity_Id;
421 Corr_Rec : Entity_Id;
422 Related_Nod : Node_Id) return Entity_Id;
423 -- When constraining a protected type or task type with discriminants,
424 -- constrain the corresponding record with the same discriminant values.
426 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
427 -- Constrain a decimal fixed point type with a digits constraint and/or a
428 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
430 procedure Constrain_Discriminated_Type
431 (Def_Id : Entity_Id;
432 S : Node_Id;
433 Related_Nod : Node_Id;
434 For_Access : Boolean := False);
435 -- Process discriminant constraints of composite type. Verify that values
436 -- have been provided for all discriminants, that the original type is
437 -- unconstrained, and that the types of the supplied expressions match
438 -- the discriminant types. The first three parameters are like in routine
439 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
440 -- of For_Access.
442 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
443 -- Constrain an enumeration type with a range constraint. This is identical
444 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
446 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
447 -- Constrain a floating point type with either a digits constraint
448 -- and/or a range constraint, building a E_Floating_Point_Subtype.
450 procedure Constrain_Index
451 (Index : Node_Id;
452 S : Node_Id;
453 Related_Nod : Node_Id;
454 Related_Id : Entity_Id;
455 Suffix : Character;
456 Suffix_Index : Nat);
457 -- Process an index constraint S in a constrained array declaration. The
458 -- constraint can be a subtype name, or a range with or without an explicit
459 -- subtype mark. The index is the corresponding index of the unconstrained
460 -- array. The Related_Id and Suffix parameters are used to build the
461 -- associated Implicit type name.
463 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
464 -- Build subtype of a signed or modular integer type
466 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
467 -- Constrain an ordinary fixed point type with a range constraint, and
468 -- build an E_Ordinary_Fixed_Point_Subtype entity.
470 procedure Copy_And_Swap (Priv, Full : Entity_Id);
471 -- Copy the Priv entity into the entity of its full declaration then swap
472 -- the two entities in such a manner that the former private type is now
473 -- seen as a full type.
475 procedure Decimal_Fixed_Point_Type_Declaration
476 (T : Entity_Id;
477 Def : Node_Id);
478 -- Create a new decimal fixed point type, and apply the constraint to
479 -- obtain a subtype of this new type.
481 procedure Complete_Private_Subtype
482 (Priv : Entity_Id;
483 Full : Entity_Id;
484 Full_Base : Entity_Id;
485 Related_Nod : Node_Id);
486 -- Complete the implicit full view of a private subtype by setting the
487 -- appropriate semantic fields. If the full view of the parent is a record
488 -- type, build constrained components of subtype.
490 procedure Derive_Progenitor_Subprograms
491 (Parent_Type : Entity_Id;
492 Tagged_Type : Entity_Id);
493 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
494 -- operations of progenitors of Tagged_Type, and replace the subsidiary
495 -- subtypes with Tagged_Type, to build the specs of the inherited interface
496 -- primitives. The derived primitives are aliased to those of the
497 -- interface. This routine takes care also of transferring to the full view
498 -- subprograms associated with the partial view of Tagged_Type that cover
499 -- interface primitives.
501 procedure Derived_Standard_Character
502 (N : Node_Id;
503 Parent_Type : Entity_Id;
504 Derived_Type : Entity_Id);
505 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
506 -- derivations from types Standard.Character and Standard.Wide_Character.
508 procedure Derived_Type_Declaration
509 (T : Entity_Id;
510 N : Node_Id;
511 Is_Completion : Boolean);
512 -- Process a derived type declaration. Build_Derived_Type is invoked
513 -- to process the actual derived type definition. Parameters N and
514 -- Is_Completion have the same meaning as in Build_Derived_Type.
515 -- T is the N_Defining_Identifier for the entity defined in the
516 -- N_Full_Type_Declaration node N, that is T is the derived type.
518 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
519 -- Insert each literal in symbol table, as an overloadable identifier. Each
520 -- enumeration type is mapped into a sequence of integers, and each literal
521 -- is defined as a constant with integer value. If any of the literals are
522 -- character literals, the type is a character type, which means that
523 -- strings are legal aggregates for arrays of components of the type.
525 function Expand_To_Stored_Constraint
526 (Typ : Entity_Id;
527 Constraint : Elist_Id) return Elist_Id;
528 -- Given a constraint (i.e. a list of expressions) on the discriminants of
529 -- Typ, expand it into a constraint on the stored discriminants and return
530 -- the new list of expressions constraining the stored discriminants.
532 function Find_Type_Of_Object
533 (Obj_Def : Node_Id;
534 Related_Nod : Node_Id) return Entity_Id;
535 -- Get type entity for object referenced by Obj_Def, attaching the
536 -- implicit types generated to Related_Nod
538 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
539 -- Create a new float and apply the constraint to obtain subtype of it
541 function Has_Range_Constraint (N : Node_Id) return Boolean;
542 -- Given an N_Subtype_Indication node N, return True if a range constraint
543 -- is present, either directly, or as part of a digits or delta constraint.
544 -- In addition, a digits constraint in the decimal case returns True, since
545 -- it establishes a default range if no explicit range is present.
547 function Inherit_Components
548 (N : Node_Id;
549 Parent_Base : Entity_Id;
550 Derived_Base : Entity_Id;
551 Is_Tagged : Boolean;
552 Inherit_Discr : Boolean;
553 Discs : Elist_Id) return Elist_Id;
554 -- Called from Build_Derived_Record_Type to inherit the components of
555 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
556 -- For more information on derived types and component inheritance please
557 -- consult the comment above the body of Build_Derived_Record_Type.
559 -- N is the original derived type declaration
561 -- Is_Tagged is set if we are dealing with tagged types
563 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
564 -- Parent_Base, otherwise no discriminants are inherited.
566 -- Discs gives the list of constraints that apply to Parent_Base in the
567 -- derived type declaration. If Discs is set to No_Elist, then we have
568 -- the following situation:
570 -- type Parent (D1..Dn : ..) is [tagged] record ...;
571 -- type Derived is new Parent [with ...];
573 -- which gets treated as
575 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
577 -- For untagged types the returned value is an association list. The list
578 -- starts from the association (Parent_Base => Derived_Base), and then it
579 -- contains a sequence of the associations of the form
581 -- (Old_Component => New_Component),
583 -- where Old_Component is the Entity_Id of a component in Parent_Base and
584 -- New_Component is the Entity_Id of the corresponding component in
585 -- Derived_Base. For untagged records, this association list is needed when
586 -- copying the record declaration for the derived base. In the tagged case
587 -- the value returned is irrelevant.
589 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
590 -- Propagate static and dynamic predicate flags from a parent to the
591 -- subtype in a subtype declaration with and without constraints.
593 function Is_Valid_Constraint_Kind
594 (T_Kind : Type_Kind;
595 Constraint_Kind : Node_Kind) return Boolean;
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).
599 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
600 -- Create new modular type. Verify that modulus is in bounds
602 procedure New_Concatenation_Op (Typ : Entity_Id);
603 -- Create an abbreviated declaration for an operator in order to
604 -- materialize concatenation on array types.
606 procedure Ordinary_Fixed_Point_Type_Declaration
607 (T : Entity_Id;
608 Def : Node_Id);
609 -- Create a new ordinary fixed point type, and apply the constraint to
610 -- obtain subtype of it.
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.
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.
629 -- Inc_T is the defining identifier of an incomplete type declaration, its
630 -- Ekind is E_Incomplete_Type.
632 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
634 -- Full_T is N's defining identifier.
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.
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.
647 procedure Process_Real_Range_Specification (Def : Node_Id);
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.
653 procedure Record_Type_Declaration
654 (T : Entity_Id;
655 N : Node_Id;
656 Prev : Entity_Id);
657 -- Process a record type declaration (for both untagged and tagged
658 -- records). Parameters T and N are exactly like in procedure
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.
664 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
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.
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.
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.
692 procedure Set_Scalar_Range_For_Subtype
693 (Def_Id : Entity_Id;
694 R : Node_Id;
695 Subt : Entity_Id);
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.
701 procedure Set_Default_SSO (T : Entity_Id);
702 -- T is the entity for an array or record being declared. This procedure
703 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
704 -- to the setting of Opt.Default_SSO.
706 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
707 -- Create a new signed integer entity, and apply the constraint to obtain
708 -- the required first named subtype of this type.
710 procedure Set_Stored_Constraint_From_Discriminant_Constraint
711 (E : Entity_Id);
712 -- E is some record type. This routine computes E's Stored_Constraint
713 -- from its Discriminant_Constraint.
715 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
716 -- Check that an entity in a list of progenitors is an interface,
717 -- emit error otherwise.
719 -----------------------
720 -- Access_Definition --
721 -----------------------
723 function Access_Definition
724 (Related_Nod : Node_Id;
725 N : Node_Id) return Entity_Id
727 Anon_Type : Entity_Id;
728 Anon_Scope : Entity_Id;
729 Desig_Type : Entity_Id;
730 Enclosing_Prot_Type : Entity_Id := Empty;
732 begin
733 Check_SPARK_05_Restriction ("access type is not allowed", N);
735 if Is_Entry (Current_Scope)
736 and then Is_Task_Type (Etype (Scope (Current_Scope)))
737 then
738 Error_Msg_N ("task entries cannot have access parameters", N);
739 return Empty;
740 end if;
742 -- Ada 2005: For an object declaration the corresponding anonymous
743 -- type is declared in the current scope.
745 -- If the access definition is the return type of another access to
746 -- function, scope is the current one, because it is the one of the
747 -- current type declaration, except for the pathological case below.
749 if Nkind_In (Related_Nod, N_Object_Declaration,
750 N_Access_Function_Definition)
751 then
752 Anon_Scope := Current_Scope;
754 -- A pathological case: function returning access functions that
755 -- return access functions, etc. Each anonymous access type created
756 -- is in the enclosing scope of the outermost function.
758 declare
759 Par : Node_Id;
761 begin
762 Par := Related_Nod;
763 while Nkind_In (Par, N_Access_Function_Definition,
764 N_Access_Definition)
765 loop
766 Par := Parent (Par);
767 end loop;
769 if Nkind (Par) = N_Function_Specification then
770 Anon_Scope := Scope (Defining_Entity (Par));
771 end if;
772 end;
774 -- For the anonymous function result case, retrieve the scope of the
775 -- function specification's associated entity rather than using the
776 -- current scope. The current scope will be the function itself if the
777 -- formal part is currently being analyzed, but will be the parent scope
778 -- in the case of a parameterless function, and we always want to use
779 -- the function's parent scope. Finally, if the function is a child
780 -- unit, we must traverse the tree to retrieve the proper entity.
782 elsif Nkind (Related_Nod) = N_Function_Specification
783 and then Nkind (Parent (N)) /= N_Parameter_Specification
784 then
785 -- If the current scope is a protected type, the anonymous access
786 -- is associated with one of the protected operations, and must
787 -- be available in the scope that encloses the protected declaration.
788 -- Otherwise the type is in the scope enclosing the subprogram.
790 -- If the function has formals, The return type of a subprogram
791 -- declaration is analyzed in the scope of the subprogram (see
792 -- Process_Formals) and thus the protected type, if present, is
793 -- the scope of the current function scope.
795 if Ekind (Current_Scope) = E_Protected_Type then
796 Enclosing_Prot_Type := Current_Scope;
798 elsif Ekind (Current_Scope) = E_Function
799 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
800 then
801 Enclosing_Prot_Type := Scope (Current_Scope);
802 end if;
804 if Present (Enclosing_Prot_Type) then
805 Anon_Scope := Scope (Enclosing_Prot_Type);
807 else
808 Anon_Scope := Scope (Defining_Entity (Related_Nod));
809 end if;
811 -- For an access type definition, if the current scope is a child
812 -- unit it is the scope of the type.
814 elsif Is_Compilation_Unit (Current_Scope) then
815 Anon_Scope := Current_Scope;
817 -- For access formals, access components, and access discriminants, the
818 -- scope is that of the enclosing declaration,
820 else
821 Anon_Scope := Scope (Current_Scope);
822 end if;
824 Anon_Type :=
825 Create_Itype
826 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
828 if All_Present (N)
829 and then Ada_Version >= Ada_2005
830 then
831 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
832 end if;
834 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
835 -- the corresponding semantic routine
837 if Present (Access_To_Subprogram_Definition (N)) then
839 -- Compiler runtime units are compiled in Ada 2005 mode when building
840 -- the runtime library but must also be compilable in Ada 95 mode
841 -- (when bootstrapping the compiler).
843 Check_Compiler_Unit ("anonymous access to subprogram", N);
845 Access_Subprogram_Declaration
846 (T_Name => Anon_Type,
847 T_Def => Access_To_Subprogram_Definition (N));
849 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
850 Set_Ekind
851 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
852 else
853 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
854 end if;
856 Set_Can_Use_Internal_Rep
857 (Anon_Type, not Always_Compatible_Rep_On_Target);
859 -- If the anonymous access is associated with a protected operation,
860 -- create a reference to it after the enclosing protected definition
861 -- because the itype will be used in the subsequent bodies.
863 if Ekind (Current_Scope) = E_Protected_Type then
864 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
865 end if;
867 return Anon_Type;
868 end if;
870 Find_Type (Subtype_Mark (N));
871 Desig_Type := Entity (Subtype_Mark (N));
873 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
874 Set_Etype (Anon_Type, Anon_Type);
876 -- Make sure the anonymous access type has size and alignment fields
877 -- set, as required by gigi. This is necessary in the case of the
878 -- Task_Body_Procedure.
880 if not Has_Private_Component (Desig_Type) then
881 Layout_Type (Anon_Type);
882 end if;
884 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
885 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
886 -- the null value is allowed. In Ada 95 the null value is never allowed.
888 if Ada_Version >= Ada_2005 then
889 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
890 else
891 Set_Can_Never_Be_Null (Anon_Type, True);
892 end if;
894 -- The anonymous access type is as public as the discriminated type or
895 -- subprogram that defines it. It is imported (for back-end purposes)
896 -- if the designated type is.
898 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
900 -- Ada 2005 (AI-231): Propagate the access-constant attribute
902 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
904 -- The context is either a subprogram declaration, object declaration,
905 -- or an access discriminant, in a private or a full type declaration.
906 -- In the case of a subprogram, if the designated type is incomplete,
907 -- the operation will be a primitive operation of the full type, to be
908 -- updated subsequently. If the type is imported through a limited_with
909 -- clause, the subprogram is not a primitive operation of the type
910 -- (which is declared elsewhere in some other scope).
912 if Ekind (Desig_Type) = E_Incomplete_Type
913 and then not From_Limited_With (Desig_Type)
914 and then Is_Overloadable (Current_Scope)
915 then
916 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
917 Set_Has_Delayed_Freeze (Current_Scope);
918 end if;
920 -- Ada 2005: If the designated type is an interface that may contain
921 -- tasks, create a Master entity for the declaration. This must be done
922 -- before expansion of the full declaration, because the declaration may
923 -- include an expression that is an allocator, whose expansion needs the
924 -- proper Master for the created tasks.
926 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
927 then
928 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
929 then
930 Build_Class_Wide_Master (Anon_Type);
932 -- Similarly, if the type is an anonymous access that designates
933 -- tasks, create a master entity for it in the current context.
935 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
936 then
937 Build_Master_Entity (Defining_Identifier (Related_Nod));
938 Build_Master_Renaming (Anon_Type);
939 end if;
940 end if;
942 -- For a private component of a protected type, it is imperative that
943 -- the back-end elaborate the type immediately after the protected
944 -- declaration, because this type will be used in the declarations
945 -- created for the component within each protected body, so we must
946 -- create an itype reference for it now.
948 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
949 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
951 -- Similarly, if the access definition is the return result of a
952 -- function, create an itype reference for it because it will be used
953 -- within the function body. For a regular function that is not a
954 -- compilation unit, insert reference after the declaration. For a
955 -- protected operation, insert it after the enclosing protected type
956 -- declaration. In either case, do not create a reference for a type
957 -- obtained through a limited_with clause, because this would introduce
958 -- semantic dependencies.
960 -- Similarly, do not create a reference if the designated type is a
961 -- generic formal, because no use of it will reach the backend.
963 elsif Nkind (Related_Nod) = N_Function_Specification
964 and then not From_Limited_With (Desig_Type)
965 and then not Is_Generic_Type (Desig_Type)
966 then
967 if Present (Enclosing_Prot_Type) then
968 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
970 elsif Is_List_Member (Parent (Related_Nod))
971 and then Nkind (Parent (N)) /= N_Parameter_Specification
972 then
973 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
974 end if;
976 -- Finally, create an itype reference for an object declaration of an
977 -- anonymous access type. This is strictly necessary only for deferred
978 -- constants, but in any case will avoid out-of-scope problems in the
979 -- back-end.
981 elsif Nkind (Related_Nod) = N_Object_Declaration then
982 Build_Itype_Reference (Anon_Type, Related_Nod);
983 end if;
985 return Anon_Type;
986 end Access_Definition;
988 -----------------------------------
989 -- Access_Subprogram_Declaration --
990 -----------------------------------
992 procedure Access_Subprogram_Declaration
993 (T_Name : Entity_Id;
994 T_Def : Node_Id)
996 procedure Check_For_Premature_Usage (Def : Node_Id);
997 -- Check that type T_Name is not used, directly or recursively, as a
998 -- parameter or a return type in Def. Def is either a subtype, an
999 -- access_definition, or an access_to_subprogram_definition.
1001 -------------------------------
1002 -- Check_For_Premature_Usage --
1003 -------------------------------
1005 procedure Check_For_Premature_Usage (Def : Node_Id) is
1006 Param : Node_Id;
1008 begin
1009 -- Check for a subtype mark
1011 if Nkind (Def) in N_Has_Etype then
1012 if Etype (Def) = T_Name then
1013 Error_Msg_N
1014 ("type& cannot be used before end of its declaration", Def);
1015 end if;
1017 -- If this is not a subtype, then this is an access_definition
1019 elsif Nkind (Def) = N_Access_Definition then
1020 if Present (Access_To_Subprogram_Definition (Def)) then
1021 Check_For_Premature_Usage
1022 (Access_To_Subprogram_Definition (Def));
1023 else
1024 Check_For_Premature_Usage (Subtype_Mark (Def));
1025 end if;
1027 -- The only cases left are N_Access_Function_Definition and
1028 -- N_Access_Procedure_Definition.
1030 else
1031 if Present (Parameter_Specifications (Def)) then
1032 Param := First (Parameter_Specifications (Def));
1033 while Present (Param) loop
1034 Check_For_Premature_Usage (Parameter_Type (Param));
1035 Param := Next (Param);
1036 end loop;
1037 end if;
1039 if Nkind (Def) = N_Access_Function_Definition then
1040 Check_For_Premature_Usage (Result_Definition (Def));
1041 end if;
1042 end if;
1043 end Check_For_Premature_Usage;
1045 -- Local variables
1047 Formals : constant List_Id := Parameter_Specifications (T_Def);
1048 Formal : Entity_Id;
1049 D_Ityp : Node_Id;
1050 Desig_Type : constant Entity_Id :=
1051 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1053 -- Start of processing for Access_Subprogram_Declaration
1055 begin
1056 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1058 -- Associate the Itype node with the inner full-type declaration or
1059 -- subprogram spec or entry body. This is required to handle nested
1060 -- anonymous declarations. For example:
1062 -- procedure P
1063 -- (X : access procedure
1064 -- (Y : access procedure
1065 -- (Z : access T)))
1067 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1068 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1069 N_Private_Type_Declaration,
1070 N_Private_Extension_Declaration,
1071 N_Procedure_Specification,
1072 N_Function_Specification,
1073 N_Entry_Body)
1075 or else
1076 Nkind_In (D_Ityp, N_Object_Declaration,
1077 N_Object_Renaming_Declaration,
1078 N_Formal_Object_Declaration,
1079 N_Formal_Type_Declaration,
1080 N_Task_Type_Declaration,
1081 N_Protected_Type_Declaration))
1082 loop
1083 D_Ityp := Parent (D_Ityp);
1084 pragma Assert (D_Ityp /= Empty);
1085 end loop;
1087 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1089 if Nkind_In (D_Ityp, N_Procedure_Specification,
1090 N_Function_Specification)
1091 then
1092 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1094 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1095 N_Object_Declaration,
1096 N_Object_Renaming_Declaration,
1097 N_Formal_Type_Declaration)
1098 then
1099 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1100 end if;
1102 if Nkind (T_Def) = N_Access_Function_Definition then
1103 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1104 declare
1105 Acc : constant Node_Id := Result_Definition (T_Def);
1107 begin
1108 if Present (Access_To_Subprogram_Definition (Acc))
1109 and then
1110 Protected_Present (Access_To_Subprogram_Definition (Acc))
1111 then
1112 Set_Etype
1113 (Desig_Type,
1114 Replace_Anonymous_Access_To_Protected_Subprogram
1115 (T_Def));
1117 else
1118 Set_Etype
1119 (Desig_Type,
1120 Access_Definition (T_Def, Result_Definition (T_Def)));
1121 end if;
1122 end;
1124 else
1125 Analyze (Result_Definition (T_Def));
1127 declare
1128 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1130 begin
1131 -- If a null exclusion is imposed on the result type, then
1132 -- create a null-excluding itype (an access subtype) and use
1133 -- it as the function's Etype.
1135 if Is_Access_Type (Typ)
1136 and then Null_Exclusion_In_Return_Present (T_Def)
1137 then
1138 Set_Etype (Desig_Type,
1139 Create_Null_Excluding_Itype
1140 (T => Typ,
1141 Related_Nod => T_Def,
1142 Scope_Id => Current_Scope));
1144 else
1145 if From_Limited_With (Typ) then
1147 -- AI05-151: Incomplete types are allowed in all basic
1148 -- declarations, including access to subprograms.
1150 if Ada_Version >= Ada_2012 then
1151 null;
1153 else
1154 Error_Msg_NE
1155 ("illegal use of incomplete type&",
1156 Result_Definition (T_Def), Typ);
1157 end if;
1159 elsif Ekind (Current_Scope) = E_Package
1160 and then In_Private_Part (Current_Scope)
1161 then
1162 if Ekind (Typ) = E_Incomplete_Type then
1163 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1165 elsif Is_Class_Wide_Type (Typ)
1166 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1167 then
1168 Append_Elmt
1169 (Desig_Type, Private_Dependents (Etype (Typ)));
1170 end if;
1171 end if;
1173 Set_Etype (Desig_Type, Typ);
1174 end if;
1175 end;
1176 end if;
1178 if not (Is_Type (Etype (Desig_Type))) then
1179 Error_Msg_N
1180 ("expect type in function specification",
1181 Result_Definition (T_Def));
1182 end if;
1184 else
1185 Set_Etype (Desig_Type, Standard_Void_Type);
1186 end if;
1188 if Present (Formals) then
1189 Push_Scope (Desig_Type);
1191 -- Some special tests here. These special tests can be removed
1192 -- if and when Itypes always have proper parent pointers to their
1193 -- declarations???
1195 -- Special test 1) Link defining_identifier of formals. Required by
1196 -- First_Formal to provide its functionality.
1198 declare
1199 F : Node_Id;
1201 begin
1202 F := First (Formals);
1204 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1205 -- when it is part of an unconstrained type and subtype expansion
1206 -- is disabled. To avoid back-end problems with shared profiles,
1207 -- use previous subprogram type as the designated type, and then
1208 -- remove scope added above.
1210 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1211 then
1212 Set_Etype (T_Name, T_Name);
1213 Init_Size_Align (T_Name);
1214 Set_Directly_Designated_Type (T_Name,
1215 Scope (Defining_Identifier (F)));
1216 End_Scope;
1217 return;
1218 end if;
1220 while Present (F) loop
1221 if No (Parent (Defining_Identifier (F))) then
1222 Set_Parent (Defining_Identifier (F), F);
1223 end if;
1225 Next (F);
1226 end loop;
1227 end;
1229 Process_Formals (Formals, Parent (T_Def));
1231 -- Special test 2) End_Scope requires that the parent pointer be set
1232 -- to something reasonable, but Itypes don't have parent pointers. So
1233 -- we set it and then unset it ???
1235 Set_Parent (Desig_Type, T_Name);
1236 End_Scope;
1237 Set_Parent (Desig_Type, Empty);
1238 end if;
1240 -- Check for premature usage of the type being defined
1242 Check_For_Premature_Usage (T_Def);
1244 -- The return type and/or any parameter type may be incomplete. Mark the
1245 -- subprogram_type as depending on the incomplete type, so that it can
1246 -- be updated when the full type declaration is seen. This only applies
1247 -- to incomplete types declared in some enclosing scope, not to limited
1248 -- views from other packages.
1250 -- Prior to Ada 2012, access to functions can only have in_parameters.
1252 if Present (Formals) then
1253 Formal := First_Formal (Desig_Type);
1254 while Present (Formal) loop
1255 if Ekind (Formal) /= E_In_Parameter
1256 and then Nkind (T_Def) = N_Access_Function_Definition
1257 and then Ada_Version < Ada_2012
1258 then
1259 Error_Msg_N ("functions can only have IN parameters", Formal);
1260 end if;
1262 if Ekind (Etype (Formal)) = E_Incomplete_Type
1263 and then In_Open_Scopes (Scope (Etype (Formal)))
1264 then
1265 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1266 Set_Has_Delayed_Freeze (Desig_Type);
1267 end if;
1269 Next_Formal (Formal);
1270 end loop;
1271 end if;
1273 -- Check whether an indirect call without actuals may be possible. This
1274 -- is used when resolving calls whose result is then indexed.
1276 May_Need_Actuals (Desig_Type);
1278 -- If the return type is incomplete, this is legal as long as the type
1279 -- is declared in the current scope and will be completed in it (rather
1280 -- than being part of limited view).
1282 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1283 and then not Has_Delayed_Freeze (Desig_Type)
1284 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1285 then
1286 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1287 Set_Has_Delayed_Freeze (Desig_Type);
1288 end if;
1290 Check_Delayed_Subprogram (Desig_Type);
1292 if Protected_Present (T_Def) then
1293 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1294 Set_Convention (Desig_Type, Convention_Protected);
1295 else
1296 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1297 end if;
1299 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1301 Set_Etype (T_Name, T_Name);
1302 Init_Size_Align (T_Name);
1303 Set_Directly_Designated_Type (T_Name, Desig_Type);
1305 Generate_Reference_To_Formals (T_Name);
1307 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1309 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1311 Check_Restriction (No_Access_Subprograms, T_Def);
1312 end Access_Subprogram_Declaration;
1314 ----------------------------
1315 -- Access_Type_Declaration --
1316 ----------------------------
1318 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1319 P : constant Node_Id := Parent (Def);
1320 S : constant Node_Id := Subtype_Indication (Def);
1322 Full_Desig : Entity_Id;
1324 begin
1325 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1327 -- Check for permissible use of incomplete type
1329 if Nkind (S) /= N_Subtype_Indication then
1330 Analyze (S);
1332 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1333 Set_Directly_Designated_Type (T, Entity (S));
1335 -- If the designated type is a limited view, we cannot tell if
1336 -- the full view contains tasks, and there is no way to handle
1337 -- that full view in a client. We create a master entity for the
1338 -- scope, which will be used when a client determines that one
1339 -- is needed.
1341 if From_Limited_With (Entity (S))
1342 and then not Is_Class_Wide_Type (Entity (S))
1343 then
1344 Set_Ekind (T, E_Access_Type);
1345 Build_Master_Entity (T);
1346 Build_Master_Renaming (T);
1347 end if;
1349 else
1350 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1351 end if;
1353 -- If the access definition is of the form: ACCESS NOT NULL ..
1354 -- the subtype indication must be of an access type. Create
1355 -- a null-excluding subtype of it.
1357 if Null_Excluding_Subtype (Def) then
1358 if not Is_Access_Type (Entity (S)) then
1359 Error_Msg_N ("null exclusion must apply to access type", Def);
1361 else
1362 declare
1363 Loc : constant Source_Ptr := Sloc (S);
1364 Decl : Node_Id;
1365 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1367 begin
1368 Decl :=
1369 Make_Subtype_Declaration (Loc,
1370 Defining_Identifier => Nam,
1371 Subtype_Indication =>
1372 New_Occurrence_Of (Entity (S), Loc));
1373 Set_Null_Exclusion_Present (Decl);
1374 Insert_Before (Parent (Def), Decl);
1375 Analyze (Decl);
1376 Set_Entity (S, Nam);
1377 end;
1378 end if;
1379 end if;
1381 else
1382 Set_Directly_Designated_Type (T,
1383 Process_Subtype (S, P, T, 'P'));
1384 end if;
1386 if All_Present (Def) or Constant_Present (Def) then
1387 Set_Ekind (T, E_General_Access_Type);
1388 else
1389 Set_Ekind (T, E_Access_Type);
1390 end if;
1392 Full_Desig := Designated_Type (T);
1394 if Base_Type (Full_Desig) = T then
1395 Error_Msg_N ("access type cannot designate itself", S);
1397 -- In Ada 2005, the type may have a limited view through some unit in
1398 -- its own context, allowing the following circularity that cannot be
1399 -- detected earlier.
1401 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1402 then
1403 Error_Msg_N
1404 ("access type cannot designate its own classwide type", S);
1406 -- Clean up indication of tagged status to prevent cascaded errors
1408 Set_Is_Tagged_Type (T, False);
1409 end if;
1411 Set_Etype (T, T);
1413 -- If the type has appeared already in a with_type clause, it is frozen
1414 -- and the pointer size is already set. Else, initialize.
1416 if not From_Limited_With (T) then
1417 Init_Size_Align (T);
1418 end if;
1420 -- Note that Has_Task is always false, since the access type itself
1421 -- is not a task type. See Einfo for more description on this point.
1422 -- Exactly the same consideration applies to Has_Controlled_Component
1423 -- and to Has_Protected.
1425 Set_Has_Task (T, False);
1426 Set_Has_Controlled_Component (T, False);
1427 Set_Has_Protected (T, False);
1429 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1430 -- problems where an incomplete view of this entity has been previously
1431 -- established by a limited with and an overlaid version of this field
1432 -- (Stored_Constraint) was initialized for the incomplete view.
1434 -- This reset is performed in most cases except where the access type
1435 -- has been created for the purposes of allocating or deallocating a
1436 -- build-in-place object. Such access types have explicitly set pools
1437 -- and finalization masters.
1439 if No (Associated_Storage_Pool (T)) then
1440 Set_Finalization_Master (T, Empty);
1441 end if;
1443 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1444 -- attributes
1446 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1447 Set_Is_Access_Constant (T, Constant_Present (Def));
1448 end Access_Type_Declaration;
1450 ----------------------------------
1451 -- Add_Interface_Tag_Components --
1452 ----------------------------------
1454 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1455 Loc : constant Source_Ptr := Sloc (N);
1456 L : List_Id;
1457 Last_Tag : Node_Id;
1459 procedure Add_Tag (Iface : Entity_Id);
1460 -- Add tag for one of the progenitor interfaces
1462 -------------
1463 -- Add_Tag --
1464 -------------
1466 procedure Add_Tag (Iface : Entity_Id) is
1467 Decl : Node_Id;
1468 Def : Node_Id;
1469 Tag : Entity_Id;
1470 Offset : Entity_Id;
1472 begin
1473 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1475 -- This is a reasonable place to propagate predicates
1477 if Has_Predicates (Iface) then
1478 Set_Has_Predicates (Typ);
1479 end if;
1481 Def :=
1482 Make_Component_Definition (Loc,
1483 Aliased_Present => True,
1484 Subtype_Indication =>
1485 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1487 Tag := Make_Temporary (Loc, 'V');
1489 Decl :=
1490 Make_Component_Declaration (Loc,
1491 Defining_Identifier => Tag,
1492 Component_Definition => Def);
1494 Analyze_Component_Declaration (Decl);
1496 Set_Analyzed (Decl);
1497 Set_Ekind (Tag, E_Component);
1498 Set_Is_Tag (Tag);
1499 Set_Is_Aliased (Tag);
1500 Set_Related_Type (Tag, Iface);
1501 Init_Component_Location (Tag);
1503 pragma Assert (Is_Frozen (Iface));
1505 Set_DT_Entry_Count (Tag,
1506 DT_Entry_Count (First_Entity (Iface)));
1508 if No (Last_Tag) then
1509 Prepend (Decl, L);
1510 else
1511 Insert_After (Last_Tag, Decl);
1512 end if;
1514 Last_Tag := Decl;
1516 -- If the ancestor has discriminants we need to give special support
1517 -- to store the offset_to_top value of the secondary dispatch tables.
1518 -- For this purpose we add a supplementary component just after the
1519 -- field that contains the tag associated with each secondary DT.
1521 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1522 Def :=
1523 Make_Component_Definition (Loc,
1524 Subtype_Indication =>
1525 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1527 Offset := Make_Temporary (Loc, 'V');
1529 Decl :=
1530 Make_Component_Declaration (Loc,
1531 Defining_Identifier => Offset,
1532 Component_Definition => Def);
1534 Analyze_Component_Declaration (Decl);
1536 Set_Analyzed (Decl);
1537 Set_Ekind (Offset, E_Component);
1538 Set_Is_Aliased (Offset);
1539 Set_Related_Type (Offset, Iface);
1540 Init_Component_Location (Offset);
1541 Insert_After (Last_Tag, Decl);
1542 Last_Tag := Decl;
1543 end if;
1544 end Add_Tag;
1546 -- Local variables
1548 Elmt : Elmt_Id;
1549 Ext : Node_Id;
1550 Comp : Node_Id;
1552 -- Start of processing for Add_Interface_Tag_Components
1554 begin
1555 if not RTE_Available (RE_Interface_Tag) then
1556 Error_Msg
1557 ("(Ada 2005) interface types not supported by this run-time!",
1558 Sloc (N));
1559 return;
1560 end if;
1562 if Ekind (Typ) /= E_Record_Type
1563 or else (Is_Concurrent_Record_Type (Typ)
1564 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1565 or else (not Is_Concurrent_Record_Type (Typ)
1566 and then No (Interfaces (Typ))
1567 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1568 then
1569 return;
1570 end if;
1572 -- Find the current last tag
1574 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1575 Ext := Record_Extension_Part (Type_Definition (N));
1576 else
1577 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1578 Ext := Type_Definition (N);
1579 end if;
1581 Last_Tag := Empty;
1583 if not (Present (Component_List (Ext))) then
1584 Set_Null_Present (Ext, False);
1585 L := New_List;
1586 Set_Component_List (Ext,
1587 Make_Component_List (Loc,
1588 Component_Items => L,
1589 Null_Present => False));
1590 else
1591 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1592 L := Component_Items
1593 (Component_List
1594 (Record_Extension_Part
1595 (Type_Definition (N))));
1596 else
1597 L := Component_Items
1598 (Component_List
1599 (Type_Definition (N)));
1600 end if;
1602 -- Find the last tag component
1604 Comp := First (L);
1605 while Present (Comp) loop
1606 if Nkind (Comp) = N_Component_Declaration
1607 and then Is_Tag (Defining_Identifier (Comp))
1608 then
1609 Last_Tag := Comp;
1610 end if;
1612 Next (Comp);
1613 end loop;
1614 end if;
1616 -- At this point L references the list of components and Last_Tag
1617 -- references the current last tag (if any). Now we add the tag
1618 -- corresponding with all the interfaces that are not implemented
1619 -- by the parent.
1621 if Present (Interfaces (Typ)) then
1622 Elmt := First_Elmt (Interfaces (Typ));
1623 while Present (Elmt) loop
1624 Add_Tag (Node (Elmt));
1625 Next_Elmt (Elmt);
1626 end loop;
1627 end if;
1628 end Add_Interface_Tag_Components;
1630 -------------------------------------
1631 -- Add_Internal_Interface_Entities --
1632 -------------------------------------
1634 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1635 Elmt : Elmt_Id;
1636 Iface : Entity_Id;
1637 Iface_Elmt : Elmt_Id;
1638 Iface_Prim : Entity_Id;
1639 Ifaces_List : Elist_Id;
1640 New_Subp : Entity_Id := Empty;
1641 Prim : Entity_Id;
1642 Restore_Scope : Boolean := False;
1644 begin
1645 pragma Assert (Ada_Version >= Ada_2005
1646 and then Is_Record_Type (Tagged_Type)
1647 and then Is_Tagged_Type (Tagged_Type)
1648 and then Has_Interfaces (Tagged_Type)
1649 and then not Is_Interface (Tagged_Type));
1651 -- Ensure that the internal entities are added to the scope of the type
1653 if Scope (Tagged_Type) /= Current_Scope then
1654 Push_Scope (Scope (Tagged_Type));
1655 Restore_Scope := True;
1656 end if;
1658 Collect_Interfaces (Tagged_Type, Ifaces_List);
1660 Iface_Elmt := First_Elmt (Ifaces_List);
1661 while Present (Iface_Elmt) loop
1662 Iface := Node (Iface_Elmt);
1664 -- Originally we excluded here from this processing interfaces that
1665 -- are parents of Tagged_Type because their primitives are located
1666 -- in the primary dispatch table (and hence no auxiliary internal
1667 -- entities are required to handle secondary dispatch tables in such
1668 -- case). However, these auxiliary entities are also required to
1669 -- handle derivations of interfaces in formals of generics (see
1670 -- Derive_Subprograms).
1672 Elmt := First_Elmt (Primitive_Operations (Iface));
1673 while Present (Elmt) loop
1674 Iface_Prim := Node (Elmt);
1676 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1677 Prim :=
1678 Find_Primitive_Covering_Interface
1679 (Tagged_Type => Tagged_Type,
1680 Iface_Prim => Iface_Prim);
1682 if No (Prim) and then Serious_Errors_Detected > 0 then
1683 goto Continue;
1684 end if;
1686 pragma Assert (Present (Prim));
1688 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1689 -- differs from the name of the interface primitive then it is
1690 -- a private primitive inherited from a parent type. In such
1691 -- case, given that Tagged_Type covers the interface, the
1692 -- inherited private primitive becomes visible. For such
1693 -- purpose we add a new entity that renames the inherited
1694 -- private primitive.
1696 if Chars (Prim) /= Chars (Iface_Prim) then
1697 pragma Assert (Has_Suffix (Prim, 'P'));
1698 Derive_Subprogram
1699 (New_Subp => New_Subp,
1700 Parent_Subp => Iface_Prim,
1701 Derived_Type => Tagged_Type,
1702 Parent_Type => Iface);
1703 Set_Alias (New_Subp, Prim);
1704 Set_Is_Abstract_Subprogram
1705 (New_Subp, Is_Abstract_Subprogram (Prim));
1706 end if;
1708 Derive_Subprogram
1709 (New_Subp => New_Subp,
1710 Parent_Subp => Iface_Prim,
1711 Derived_Type => Tagged_Type,
1712 Parent_Type => Iface);
1714 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1715 -- associated with interface types. These entities are
1716 -- only registered in the list of primitives of its
1717 -- corresponding tagged type because they are only used
1718 -- to fill the contents of the secondary dispatch tables.
1719 -- Therefore they are removed from the homonym chains.
1721 Set_Is_Hidden (New_Subp);
1722 Set_Is_Internal (New_Subp);
1723 Set_Alias (New_Subp, Prim);
1724 Set_Is_Abstract_Subprogram
1725 (New_Subp, Is_Abstract_Subprogram (Prim));
1726 Set_Interface_Alias (New_Subp, Iface_Prim);
1728 -- If the returned type is an interface then propagate it to
1729 -- the returned type. Needed by the thunk to generate the code
1730 -- which displaces "this" to reference the corresponding
1731 -- secondary dispatch table in the returned object.
1733 if Is_Interface (Etype (Iface_Prim)) then
1734 Set_Etype (New_Subp, Etype (Iface_Prim));
1735 end if;
1737 -- Internal entities associated with interface types are
1738 -- only registered in the list of primitives of the tagged
1739 -- type. They are only used to fill the contents of the
1740 -- secondary dispatch tables. Therefore they are not needed
1741 -- in the homonym chains.
1743 Remove_Homonym (New_Subp);
1745 -- Hidden entities associated with interfaces must have set
1746 -- the Has_Delay_Freeze attribute to ensure that, in case of
1747 -- locally defined tagged types (or compiling with static
1748 -- dispatch tables generation disabled) the corresponding
1749 -- entry of the secondary dispatch table is filled when
1750 -- such an entity is frozen.
1752 Set_Has_Delayed_Freeze (New_Subp);
1753 end if;
1755 <<Continue>>
1756 Next_Elmt (Elmt);
1757 end loop;
1759 Next_Elmt (Iface_Elmt);
1760 end loop;
1762 if Restore_Scope then
1763 Pop_Scope;
1764 end if;
1765 end Add_Internal_Interface_Entities;
1767 -----------------------------------
1768 -- Analyze_Component_Declaration --
1769 -----------------------------------
1771 procedure Analyze_Component_Declaration (N : Node_Id) is
1772 Id : constant Entity_Id := Defining_Identifier (N);
1773 E : constant Node_Id := Expression (N);
1774 Typ : constant Node_Id :=
1775 Subtype_Indication (Component_Definition (N));
1776 T : Entity_Id;
1777 P : Entity_Id;
1779 function Contains_POC (Constr : Node_Id) return Boolean;
1780 -- Determines whether a constraint uses the discriminant of a record
1781 -- type thus becoming a per-object constraint (POC).
1783 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1784 -- Typ is the type of the current component, check whether this type is
1785 -- a limited type. Used to validate declaration against that of
1786 -- enclosing record.
1788 ------------------
1789 -- Contains_POC --
1790 ------------------
1792 function Contains_POC (Constr : Node_Id) return Boolean is
1793 begin
1794 -- Prevent cascaded errors
1796 if Error_Posted (Constr) then
1797 return False;
1798 end if;
1800 case Nkind (Constr) is
1801 when N_Attribute_Reference =>
1802 return Attribute_Name (Constr) = Name_Access
1803 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1805 when N_Discriminant_Association =>
1806 return Denotes_Discriminant (Expression (Constr));
1808 when N_Identifier =>
1809 return Denotes_Discriminant (Constr);
1811 when N_Index_Or_Discriminant_Constraint =>
1812 declare
1813 IDC : Node_Id;
1815 begin
1816 IDC := First (Constraints (Constr));
1817 while Present (IDC) loop
1819 -- One per-object constraint is sufficient
1821 if Contains_POC (IDC) then
1822 return True;
1823 end if;
1825 Next (IDC);
1826 end loop;
1828 return False;
1829 end;
1831 when N_Range =>
1832 return Denotes_Discriminant (Low_Bound (Constr))
1833 or else
1834 Denotes_Discriminant (High_Bound (Constr));
1836 when N_Range_Constraint =>
1837 return Denotes_Discriminant (Range_Expression (Constr));
1839 when others =>
1840 return False;
1842 end case;
1843 end Contains_POC;
1845 ----------------------
1846 -- Is_Known_Limited --
1847 ----------------------
1849 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1850 P : constant Entity_Id := Etype (Typ);
1851 R : constant Entity_Id := Root_Type (Typ);
1853 begin
1854 if Is_Limited_Record (Typ) then
1855 return True;
1857 -- If the root type is limited (and not a limited interface)
1858 -- so is the current type
1860 elsif Is_Limited_Record (R)
1861 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1862 then
1863 return True;
1865 -- Else the type may have a limited interface progenitor, but a
1866 -- limited record parent.
1868 elsif R /= P and then Is_Limited_Record (P) then
1869 return True;
1871 else
1872 return False;
1873 end if;
1874 end Is_Known_Limited;
1876 -- Start of processing for Analyze_Component_Declaration
1878 begin
1879 Generate_Definition (Id);
1880 Enter_Name (Id);
1882 if Present (Typ) then
1883 T := Find_Type_Of_Object
1884 (Subtype_Indication (Component_Definition (N)), N);
1886 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1887 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1888 end if;
1890 -- Ada 2005 (AI-230): Access Definition case
1892 else
1893 pragma Assert (Present
1894 (Access_Definition (Component_Definition (N))));
1896 T := Access_Definition
1897 (Related_Nod => N,
1898 N => Access_Definition (Component_Definition (N)));
1899 Set_Is_Local_Anonymous_Access (T);
1901 -- Ada 2005 (AI-254)
1903 if Present (Access_To_Subprogram_Definition
1904 (Access_Definition (Component_Definition (N))))
1905 and then Protected_Present (Access_To_Subprogram_Definition
1906 (Access_Definition
1907 (Component_Definition (N))))
1908 then
1909 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1910 end if;
1911 end if;
1913 -- If the subtype is a constrained subtype of the enclosing record,
1914 -- (which must have a partial view) the back-end does not properly
1915 -- handle the recursion. Rewrite the component declaration with an
1916 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1917 -- the tree directly because side effects have already been removed from
1918 -- discriminant constraints.
1920 if Ekind (T) = E_Access_Subtype
1921 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1922 and then Comes_From_Source (T)
1923 and then Nkind (Parent (T)) = N_Subtype_Declaration
1924 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1925 then
1926 Rewrite
1927 (Subtype_Indication (Component_Definition (N)),
1928 New_Copy_Tree (Subtype_Indication (Parent (T))));
1929 T := Find_Type_Of_Object
1930 (Subtype_Indication (Component_Definition (N)), N);
1931 end if;
1933 -- If the component declaration includes a default expression, then we
1934 -- check that the component is not of a limited type (RM 3.7(5)),
1935 -- and do the special preanalysis of the expression (see section on
1936 -- "Handling of Default and Per-Object Expressions" in the spec of
1937 -- package Sem).
1939 if Present (E) then
1940 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1941 Preanalyze_Default_Expression (E, T);
1942 Check_Initialization (T, E);
1944 if Ada_Version >= Ada_2005
1945 and then Ekind (T) = E_Anonymous_Access_Type
1946 and then Etype (E) /= Any_Type
1947 then
1948 -- Check RM 3.9.2(9): "if the expected type for an expression is
1949 -- an anonymous access-to-specific tagged type, then the object
1950 -- designated by the expression shall not be dynamically tagged
1951 -- unless it is a controlling operand in a call on a dispatching
1952 -- operation"
1954 if Is_Tagged_Type (Directly_Designated_Type (T))
1955 and then
1956 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1957 and then
1958 Ekind (Directly_Designated_Type (Etype (E))) =
1959 E_Class_Wide_Type
1960 then
1961 Error_Msg_N
1962 ("access to specific tagged type required (RM 3.9.2(9))", E);
1963 end if;
1965 -- (Ada 2005: AI-230): Accessibility check for anonymous
1966 -- components
1968 if Type_Access_Level (Etype (E)) >
1969 Deepest_Type_Access_Level (T)
1970 then
1971 Error_Msg_N
1972 ("expression has deeper access level than component " &
1973 "(RM 3.10.2 (12.2))", E);
1974 end if;
1976 -- The initialization expression is a reference to an access
1977 -- discriminant. The type of the discriminant is always deeper
1978 -- than any access type.
1980 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1981 and then Is_Entity_Name (E)
1982 and then Ekind (Entity (E)) = E_In_Parameter
1983 and then Present (Discriminal_Link (Entity (E)))
1984 then
1985 Error_Msg_N
1986 ("discriminant has deeper accessibility level than target",
1988 end if;
1989 end if;
1990 end if;
1992 -- The parent type may be a private view with unknown discriminants,
1993 -- and thus unconstrained. Regular components must be constrained.
1995 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1996 if Is_Class_Wide_Type (T) then
1997 Error_Msg_N
1998 ("class-wide subtype with unknown discriminants" &
1999 " in component declaration",
2000 Subtype_Indication (Component_Definition (N)));
2001 else
2002 Error_Msg_N
2003 ("unconstrained subtype in component declaration",
2004 Subtype_Indication (Component_Definition (N)));
2005 end if;
2007 -- Components cannot be abstract, except for the special case of
2008 -- the _Parent field (case of extending an abstract tagged type)
2010 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2011 Error_Msg_N ("type of a component cannot be abstract", N);
2012 end if;
2014 Set_Etype (Id, T);
2015 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2017 -- The component declaration may have a per-object constraint, set
2018 -- the appropriate flag in the defining identifier of the subtype.
2020 if Present (Subtype_Indication (Component_Definition (N))) then
2021 declare
2022 Sindic : constant Node_Id :=
2023 Subtype_Indication (Component_Definition (N));
2024 begin
2025 if Nkind (Sindic) = N_Subtype_Indication
2026 and then Present (Constraint (Sindic))
2027 and then Contains_POC (Constraint (Sindic))
2028 then
2029 Set_Has_Per_Object_Constraint (Id);
2030 end if;
2031 end;
2032 end if;
2034 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2035 -- out some static checks.
2037 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2038 Null_Exclusion_Static_Checks (N);
2039 end if;
2041 -- If this component is private (or depends on a private type), flag the
2042 -- record type to indicate that some operations are not available.
2044 P := Private_Component (T);
2046 if Present (P) then
2048 -- Check for circular definitions
2050 if P = Any_Type then
2051 Set_Etype (Id, Any_Type);
2053 -- There is a gap in the visibility of operations only if the
2054 -- component type is not defined in the scope of the record type.
2056 elsif Scope (P) = Scope (Current_Scope) then
2057 null;
2059 elsif Is_Limited_Type (P) then
2060 Set_Is_Limited_Composite (Current_Scope);
2062 else
2063 Set_Is_Private_Composite (Current_Scope);
2064 end if;
2065 end if;
2067 if P /= Any_Type
2068 and then Is_Limited_Type (T)
2069 and then Chars (Id) /= Name_uParent
2070 and then Is_Tagged_Type (Current_Scope)
2071 then
2072 if Is_Derived_Type (Current_Scope)
2073 and then not Is_Known_Limited (Current_Scope)
2074 then
2075 Error_Msg_N
2076 ("extension of nonlimited type cannot have limited components",
2079 if Is_Interface (Root_Type (Current_Scope)) then
2080 Error_Msg_N
2081 ("\limitedness is not inherited from limited interface", N);
2082 Error_Msg_N ("\add LIMITED to type indication", N);
2083 end if;
2085 Explain_Limited_Type (T, N);
2086 Set_Etype (Id, Any_Type);
2087 Set_Is_Limited_Composite (Current_Scope, False);
2089 elsif not Is_Derived_Type (Current_Scope)
2090 and then not Is_Limited_Record (Current_Scope)
2091 and then not Is_Concurrent_Type (Current_Scope)
2092 then
2093 Error_Msg_N
2094 ("nonlimited tagged type cannot have limited components", N);
2095 Explain_Limited_Type (T, N);
2096 Set_Etype (Id, Any_Type);
2097 Set_Is_Limited_Composite (Current_Scope, False);
2098 end if;
2099 end if;
2101 Set_Original_Record_Component (Id, Id);
2103 if Has_Aspects (N) then
2104 Analyze_Aspect_Specifications (N, Id);
2105 end if;
2107 Analyze_Dimension (N);
2108 end Analyze_Component_Declaration;
2110 --------------------------
2111 -- Analyze_Declarations --
2112 --------------------------
2114 procedure Analyze_Declarations (L : List_Id) is
2115 Decl : Node_Id;
2117 procedure Adjust_Decl;
2118 -- Adjust Decl not to include implicit label declarations, since these
2119 -- have strange Sloc values that result in elaboration check problems.
2120 -- (They have the sloc of the label as found in the source, and that
2121 -- is ahead of the current declarative part).
2123 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2124 -- Determine whether Body_Decl denotes the body of a late controlled
2125 -- primitive (either Initialize, Adjust or Finalize). If this is the
2126 -- case, add a proper spec if the body lacks one. The spec is inserted
2127 -- before Body_Decl and immedately analyzed.
2129 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2130 -- Spec_Id is the entity of a package that may define abstract states.
2131 -- If the states have visible refinement, remove the visibility of each
2132 -- constituent at the end of the package body declarations.
2134 -----------------
2135 -- Adjust_Decl --
2136 -----------------
2138 procedure Adjust_Decl is
2139 begin
2140 while Present (Prev (Decl))
2141 and then Nkind (Decl) = N_Implicit_Label_Declaration
2142 loop
2143 Prev (Decl);
2144 end loop;
2145 end Adjust_Decl;
2147 --------------------------------------
2148 -- Handle_Late_Controlled_Primitive --
2149 --------------------------------------
2151 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2152 Body_Spec : constant Node_Id := Specification (Body_Decl);
2153 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2154 Loc : constant Source_Ptr := Sloc (Body_Id);
2155 Params : constant List_Id :=
2156 Parameter_Specifications (Body_Spec);
2157 Spec : Node_Id;
2158 Spec_Id : Entity_Id;
2160 Dummy : Entity_Id;
2161 -- A dummy variable used to capture the unused result of subprogram
2162 -- spec analysis.
2164 begin
2165 -- Consider only procedure bodies whose name matches one of the three
2166 -- controlled primitives.
2168 if Nkind (Body_Spec) /= N_Procedure_Specification
2169 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2170 Name_Finalize,
2171 Name_Initialize)
2172 then
2173 return;
2175 -- A controlled primitive must have exactly one formal
2177 elsif List_Length (Params) /= 1 then
2178 return;
2179 end if;
2181 Dummy := Analyze_Subprogram_Specification (Body_Spec);
2183 -- The type of the formal must be derived from [Limited_]Controlled
2185 if not Is_Controlled (Etype (Defining_Entity (First (Params)))) then
2186 return;
2187 end if;
2189 Spec_Id := Find_Corresponding_Spec (Body_Decl, Post_Error => False);
2191 -- The body has a matching spec, therefore it cannot be a late
2192 -- primitive.
2194 if Present (Spec_Id) then
2195 return;
2196 end if;
2198 -- At this point the body is known to be a late controlled primitive.
2199 -- Generate a matching spec and insert it before the body. Note the
2200 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2201 -- tree in this case.
2203 Spec := Copy_Separate_Tree (Body_Spec);
2205 -- Ensure that the subprogram declaration does not inherit the null
2206 -- indicator from the body as we now have a proper spec/body pair.
2208 Set_Null_Present (Spec, False);
2210 Insert_Before_And_Analyze (Body_Decl,
2211 Make_Subprogram_Declaration (Loc,
2212 Specification => Spec));
2213 end Handle_Late_Controlled_Primitive;
2215 --------------------------------
2216 -- Remove_Visible_Refinements --
2217 --------------------------------
2219 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2220 State_Elmt : Elmt_Id;
2221 begin
2222 if Present (Abstract_States (Spec_Id)) then
2223 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2224 while Present (State_Elmt) loop
2225 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2226 Next_Elmt (State_Elmt);
2227 end loop;
2228 end if;
2229 end Remove_Visible_Refinements;
2231 -- Local variables
2233 Context : Node_Id;
2234 Freeze_From : Entity_Id := Empty;
2235 Next_Decl : Node_Id;
2236 Spec_Id : Entity_Id;
2238 Body_Seen : Boolean := False;
2239 -- Flag set when the first body [stub] is encountered
2241 In_Package_Body : Boolean := False;
2242 -- Flag set when the current declaration list belongs to a package body
2244 -- Start of processing for Analyze_Declarations
2246 begin
2247 if Restriction_Check_Required (SPARK_05) then
2248 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2249 end if;
2251 Decl := First (L);
2252 while Present (Decl) loop
2254 -- Package spec cannot contain a package declaration in SPARK
2256 if Nkind (Decl) = N_Package_Declaration
2257 and then Nkind (Parent (L)) = N_Package_Specification
2258 then
2259 Check_SPARK_05_Restriction
2260 ("package specification cannot contain a package declaration",
2261 Decl);
2262 end if;
2264 -- Complete analysis of declaration
2266 Analyze (Decl);
2267 Next_Decl := Next (Decl);
2269 if No (Freeze_From) then
2270 Freeze_From := First_Entity (Current_Scope);
2271 end if;
2273 -- At the end of a declarative part, freeze remaining entities
2274 -- declared in it. The end of the visible declarations of package
2275 -- specification is not the end of a declarative part if private
2276 -- declarations are present. The end of a package declaration is a
2277 -- freezing point only if it a library package. A task definition or
2278 -- protected type definition is not a freeze point either. Finally,
2279 -- we do not freeze entities in generic scopes, because there is no
2280 -- code generated for them and freeze nodes will be generated for
2281 -- the instance.
2283 -- The end of a package instantiation is not a freeze point, but
2284 -- for now we make it one, because the generic body is inserted
2285 -- (currently) immediately after. Generic instantiations will not
2286 -- be a freeze point once delayed freezing of bodies is implemented.
2287 -- (This is needed in any case for early instantiations ???).
2289 if No (Next_Decl) then
2290 if Nkind_In (Parent (L), N_Component_List,
2291 N_Task_Definition,
2292 N_Protected_Definition)
2293 then
2294 null;
2296 elsif Nkind (Parent (L)) /= N_Package_Specification then
2297 if Nkind (Parent (L)) = N_Package_Body then
2298 Freeze_From := First_Entity (Current_Scope);
2299 end if;
2301 -- There may have been several freezing points previously,
2302 -- for example object declarations or subprogram bodies, but
2303 -- at the end of a declarative part we check freezing from
2304 -- the beginning, even though entities may already be frozen,
2305 -- in order to perform visibility checks on delayed aspects.
2307 Adjust_Decl;
2308 Freeze_All (First_Entity (Current_Scope), Decl);
2309 Freeze_From := Last_Entity (Current_Scope);
2311 elsif Scope (Current_Scope) /= Standard_Standard
2312 and then not Is_Child_Unit (Current_Scope)
2313 and then No (Generic_Parent (Parent (L)))
2314 then
2315 null;
2317 elsif L /= Visible_Declarations (Parent (L))
2318 or else No (Private_Declarations (Parent (L)))
2319 or else Is_Empty_List (Private_Declarations (Parent (L)))
2320 then
2321 Adjust_Decl;
2322 Freeze_All (First_Entity (Current_Scope), Decl);
2323 Freeze_From := Last_Entity (Current_Scope);
2324 end if;
2326 -- If next node is a body then freeze all types before the body.
2327 -- An exception occurs for some expander-generated bodies. If these
2328 -- are generated at places where in general language rules would not
2329 -- allow a freeze point, then we assume that the expander has
2330 -- explicitly checked that all required types are properly frozen,
2331 -- and we do not cause general freezing here. This special circuit
2332 -- is used when the encountered body is marked as having already
2333 -- been analyzed.
2335 -- In all other cases (bodies that come from source, and expander
2336 -- generated bodies that have not been analyzed yet), freeze all
2337 -- types now. Note that in the latter case, the expander must take
2338 -- care to attach the bodies at a proper place in the tree so as to
2339 -- not cause unwanted freezing at that point.
2341 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2343 -- When a controlled type is frozen, the expander generates stream
2344 -- and controlled type support routines. If the freeze is caused
2345 -- by the stand alone body of Initialize, Adjust and Finalize, the
2346 -- expander will end up using the wrong version of these routines
2347 -- as the body has not been processed yet. To remedy this, detect
2348 -- a late controlled primitive and create a proper spec for it.
2349 -- This ensures that the primitive will override its inherited
2350 -- counterpart before the freeze takes place.
2352 -- If the declaration we just processed is a body, do not attempt
2353 -- to examine Next_Decl as the late primitive idiom can only apply
2354 -- to the first encountered body.
2356 -- The spec of the late primitive is not generated in ASIS mode to
2357 -- ensure a consistent list of primitives that indicates the true
2358 -- semantic structure of the program (which is not relevant when
2359 -- generating executable code.
2361 -- ??? a cleaner approach may be possible and/or this solution
2362 -- could be extended to general-purpose late primitives, TBD.
2364 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2365 then
2366 Body_Seen := True;
2368 if Nkind (Next_Decl) = N_Subprogram_Body then
2369 Handle_Late_Controlled_Primitive (Next_Decl);
2370 end if;
2371 end if;
2373 Adjust_Decl;
2374 Freeze_All (Freeze_From, Decl);
2375 Freeze_From := Last_Entity (Current_Scope);
2376 end if;
2378 Decl := Next_Decl;
2379 end loop;
2381 -- Analyze the contracts of packages and their bodies
2383 if Present (L) then
2384 Context := Parent (L);
2386 if Nkind (Context) = N_Package_Specification then
2388 -- When a package has private declarations, its contract must be
2389 -- analyzed at the end of the said declarations. This way both the
2390 -- analysis and freeze actions are properly synchronized in case
2391 -- of private type use within the contract.
2393 if L = Private_Declarations (Context) then
2394 Analyze_Package_Contract (Defining_Entity (Context));
2396 -- Build the bodies of the default initial condition procedures
2397 -- for all types subject to pragma Default_Initial_Condition.
2398 -- From a purely Ada stand point, this is a freezing activity,
2399 -- however freezing is not available under GNATprove_Mode. To
2400 -- accomodate both scenarios, the bodies are build at the end
2401 -- of private declaration analysis.
2403 Build_Default_Init_Cond_Procedure_Bodies (L);
2405 -- Otherwise the contract is analyzed at the end of the visible
2406 -- declarations.
2408 elsif L = Visible_Declarations (Context)
2409 and then No (Private_Declarations (Context))
2410 then
2411 Analyze_Package_Contract (Defining_Entity (Context));
2412 end if;
2414 elsif Nkind (Context) = N_Package_Body then
2415 In_Package_Body := True;
2416 Spec_Id := Corresponding_Spec (Context);
2418 Analyze_Package_Body_Contract (Defining_Entity (Context));
2419 end if;
2420 end if;
2422 -- Analyze the contracts of subprogram declarations, subprogram bodies
2423 -- and variables now due to the delayed visibility requirements of their
2424 -- aspects.
2426 Decl := First (L);
2427 while Present (Decl) loop
2428 if Nkind (Decl) = N_Object_Declaration then
2429 Analyze_Object_Contract (Defining_Entity (Decl));
2431 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2432 N_Subprogram_Declaration)
2433 then
2434 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2436 elsif Nkind (Decl) = N_Subprogram_Body then
2437 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2439 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2440 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2441 end if;
2443 Next (Decl);
2444 end loop;
2446 -- State refinements are visible upto the end the of the package body
2447 -- declarations. Hide the refinements from visibility to restore the
2448 -- original state conditions.
2450 if In_Package_Body then
2451 Remove_Visible_Refinements (Spec_Id);
2452 end if;
2453 end Analyze_Declarations;
2455 -----------------------------------
2456 -- Analyze_Full_Type_Declaration --
2457 -----------------------------------
2459 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2460 Def : constant Node_Id := Type_Definition (N);
2461 Def_Id : constant Entity_Id := Defining_Identifier (N);
2462 T : Entity_Id;
2463 Prev : Entity_Id;
2465 Is_Remote : constant Boolean :=
2466 (Is_Remote_Types (Current_Scope)
2467 or else Is_Remote_Call_Interface (Current_Scope))
2468 and then not (In_Private_Part (Current_Scope)
2469 or else In_Package_Body (Current_Scope));
2471 procedure Check_Ops_From_Incomplete_Type;
2472 -- If there is a tagged incomplete partial view of the type, traverse
2473 -- the primitives of the incomplete view and change the type of any
2474 -- controlling formals and result to indicate the full view. The
2475 -- primitives will be added to the full type's primitive operations
2476 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2477 -- is called from Process_Incomplete_Dependents).
2479 ------------------------------------
2480 -- Check_Ops_From_Incomplete_Type --
2481 ------------------------------------
2483 procedure Check_Ops_From_Incomplete_Type is
2484 Elmt : Elmt_Id;
2485 Formal : Entity_Id;
2486 Op : Entity_Id;
2488 begin
2489 if Prev /= T
2490 and then Ekind (Prev) = E_Incomplete_Type
2491 and then Is_Tagged_Type (Prev)
2492 and then Is_Tagged_Type (T)
2493 then
2494 Elmt := First_Elmt (Primitive_Operations (Prev));
2495 while Present (Elmt) loop
2496 Op := Node (Elmt);
2498 Formal := First_Formal (Op);
2499 while Present (Formal) loop
2500 if Etype (Formal) = Prev then
2501 Set_Etype (Formal, T);
2502 end if;
2504 Next_Formal (Formal);
2505 end loop;
2507 if Etype (Op) = Prev then
2508 Set_Etype (Op, T);
2509 end if;
2511 Next_Elmt (Elmt);
2512 end loop;
2513 end if;
2514 end Check_Ops_From_Incomplete_Type;
2516 -- Start of processing for Analyze_Full_Type_Declaration
2518 begin
2519 Prev := Find_Type_Name (N);
2521 -- The full view, if present, now points to the current type
2522 -- If there is an incomplete partial view, set a link to it, to
2523 -- simplify the retrieval of primitive operations of the type.
2525 -- Ada 2005 (AI-50217): If the type was previously decorated when
2526 -- imported through a LIMITED WITH clause, it appears as incomplete
2527 -- but has no full view.
2529 if Ekind (Prev) = E_Incomplete_Type and then Present (Full_View (Prev))
2530 then
2531 T := Full_View (Prev);
2532 Set_Incomplete_View (N, Parent (Prev));
2533 else
2534 T := Prev;
2535 end if;
2537 Set_Is_Pure (T, Is_Pure (Current_Scope));
2539 -- We set the flag Is_First_Subtype here. It is needed to set the
2540 -- corresponding flag for the Implicit class-wide-type created
2541 -- during tagged types processing.
2543 Set_Is_First_Subtype (T, True);
2545 -- Only composite types other than array types are allowed to have
2546 -- discriminants.
2548 case Nkind (Def) is
2550 -- For derived types, the rule will be checked once we've figured
2551 -- out the parent type.
2553 when N_Derived_Type_Definition =>
2554 null;
2556 -- For record types, discriminants are allowed, unless we are in
2557 -- SPARK.
2559 when N_Record_Definition =>
2560 if Present (Discriminant_Specifications (N)) then
2561 Check_SPARK_05_Restriction
2562 ("discriminant type is not allowed",
2563 Defining_Identifier
2564 (First (Discriminant_Specifications (N))));
2565 end if;
2567 when others =>
2568 if Present (Discriminant_Specifications (N)) then
2569 Error_Msg_N
2570 ("elementary or array type cannot have discriminants",
2571 Defining_Identifier
2572 (First (Discriminant_Specifications (N))));
2573 end if;
2574 end case;
2576 -- Elaborate the type definition according to kind, and generate
2577 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2578 -- already done (this happens during the reanalysis that follows a call
2579 -- to the high level optimizer).
2581 if not Analyzed (T) then
2582 Set_Analyzed (T);
2584 case Nkind (Def) is
2586 when N_Access_To_Subprogram_Definition =>
2587 Access_Subprogram_Declaration (T, Def);
2589 -- If this is a remote access to subprogram, we must create the
2590 -- equivalent fat pointer type, and related subprograms.
2592 if Is_Remote then
2593 Process_Remote_AST_Declaration (N);
2594 end if;
2596 -- Validate categorization rule against access type declaration
2597 -- usually a violation in Pure unit, Shared_Passive unit.
2599 Validate_Access_Type_Declaration (T, N);
2601 when N_Access_To_Object_Definition =>
2602 Access_Type_Declaration (T, Def);
2604 -- Validate categorization rule against access type declaration
2605 -- usually a violation in Pure unit, Shared_Passive unit.
2607 Validate_Access_Type_Declaration (T, N);
2609 -- If we are in a Remote_Call_Interface package and define a
2610 -- RACW, then calling stubs and specific stream attributes
2611 -- must be added.
2613 if Is_Remote
2614 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2615 then
2616 Add_RACW_Features (Def_Id);
2617 end if;
2619 -- Set no strict aliasing flag if config pragma seen
2621 if Opt.No_Strict_Aliasing then
2622 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2623 end if;
2625 when N_Array_Type_Definition =>
2626 Array_Type_Declaration (T, Def);
2628 when N_Derived_Type_Definition =>
2629 Derived_Type_Declaration (T, N, T /= Def_Id);
2631 when N_Enumeration_Type_Definition =>
2632 Enumeration_Type_Declaration (T, Def);
2634 when N_Floating_Point_Definition =>
2635 Floating_Point_Type_Declaration (T, Def);
2637 when N_Decimal_Fixed_Point_Definition =>
2638 Decimal_Fixed_Point_Type_Declaration (T, Def);
2640 when N_Ordinary_Fixed_Point_Definition =>
2641 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2643 when N_Signed_Integer_Type_Definition =>
2644 Signed_Integer_Type_Declaration (T, Def);
2646 when N_Modular_Type_Definition =>
2647 Modular_Type_Declaration (T, Def);
2649 when N_Record_Definition =>
2650 Record_Type_Declaration (T, N, Prev);
2652 -- If declaration has a parse error, nothing to elaborate.
2654 when N_Error =>
2655 null;
2657 when others =>
2658 raise Program_Error;
2660 end case;
2661 end if;
2663 if Etype (T) = Any_Type then
2664 return;
2665 end if;
2667 -- Controlled type is not allowed in SPARK
2669 if Is_Visibly_Controlled (T) then
2670 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2671 end if;
2673 -- Some common processing for all types
2675 Set_Depends_On_Private (T, Has_Private_Component (T));
2676 Check_Ops_From_Incomplete_Type;
2678 -- Both the declared entity, and its anonymous base type if one
2679 -- was created, need freeze nodes allocated.
2681 declare
2682 B : constant Entity_Id := Base_Type (T);
2684 begin
2685 -- In the case where the base type differs from the first subtype, we
2686 -- pre-allocate a freeze node, and set the proper link to the first
2687 -- subtype. Freeze_Entity will use this preallocated freeze node when
2688 -- it freezes the entity.
2690 -- This does not apply if the base type is a generic type, whose
2691 -- declaration is independent of the current derived definition.
2693 if B /= T and then not Is_Generic_Type (B) then
2694 Ensure_Freeze_Node (B);
2695 Set_First_Subtype_Link (Freeze_Node (B), T);
2696 end if;
2698 -- A type that is imported through a limited_with clause cannot
2699 -- generate any code, and thus need not be frozen. However, an access
2700 -- type with an imported designated type needs a finalization list,
2701 -- which may be referenced in some other package that has non-limited
2702 -- visibility on the designated type. Thus we must create the
2703 -- finalization list at the point the access type is frozen, to
2704 -- prevent unsatisfied references at link time.
2706 if not From_Limited_With (T) or else Is_Access_Type (T) then
2707 Set_Has_Delayed_Freeze (T);
2708 end if;
2709 end;
2711 -- Case where T is the full declaration of some private type which has
2712 -- been swapped in Defining_Identifier (N).
2714 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2715 Process_Full_View (N, T, Def_Id);
2717 -- Record the reference. The form of this is a little strange, since
2718 -- the full declaration has been swapped in. So the first parameter
2719 -- here represents the entity to which a reference is made which is
2720 -- the "real" entity, i.e. the one swapped in, and the second
2721 -- parameter provides the reference location.
2723 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2724 -- since we don't want a complaint about the full type being an
2725 -- unwanted reference to the private type
2727 declare
2728 B : constant Boolean := Has_Pragma_Unreferenced (T);
2729 begin
2730 Set_Has_Pragma_Unreferenced (T, False);
2731 Generate_Reference (T, T, 'c');
2732 Set_Has_Pragma_Unreferenced (T, B);
2733 end;
2735 Set_Completion_Referenced (Def_Id);
2737 -- For completion of incomplete type, process incomplete dependents
2738 -- and always mark the full type as referenced (it is the incomplete
2739 -- type that we get for any real reference).
2741 elsif Ekind (Prev) = E_Incomplete_Type then
2742 Process_Incomplete_Dependents (N, T, Prev);
2743 Generate_Reference (Prev, Def_Id, 'c');
2744 Set_Completion_Referenced (Def_Id);
2746 -- If not private type or incomplete type completion, this is a real
2747 -- definition of a new entity, so record it.
2749 else
2750 Generate_Definition (Def_Id);
2751 end if;
2753 if Chars (Scope (Def_Id)) = Name_System
2754 and then Chars (Def_Id) = Name_Address
2755 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2756 then
2757 Set_Is_Descendent_Of_Address (Def_Id);
2758 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2759 Set_Is_Descendent_Of_Address (Prev);
2760 end if;
2762 Set_Optimize_Alignment_Flags (Def_Id);
2763 Check_Eliminated (Def_Id);
2765 -- If the declaration is a completion and aspects are present, apply
2766 -- them to the entity for the type which is currently the partial
2767 -- view, but which is the one that will be frozen.
2769 if Has_Aspects (N) then
2770 if Prev /= Def_Id then
2771 Analyze_Aspect_Specifications (N, Prev);
2772 else
2773 Analyze_Aspect_Specifications (N, Def_Id);
2774 end if;
2775 end if;
2776 end Analyze_Full_Type_Declaration;
2778 ----------------------------------
2779 -- Analyze_Incomplete_Type_Decl --
2780 ----------------------------------
2782 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2783 F : constant Boolean := Is_Pure (Current_Scope);
2784 T : Entity_Id;
2786 begin
2787 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2789 Generate_Definition (Defining_Identifier (N));
2791 -- Process an incomplete declaration. The identifier must not have been
2792 -- declared already in the scope. However, an incomplete declaration may
2793 -- appear in the private part of a package, for a private type that has
2794 -- already been declared.
2796 -- In this case, the discriminants (if any) must match
2798 T := Find_Type_Name (N);
2800 Set_Ekind (T, E_Incomplete_Type);
2801 Init_Size_Align (T);
2802 Set_Is_First_Subtype (T, True);
2803 Set_Etype (T, T);
2805 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2806 -- incomplete types.
2808 if Tagged_Present (N) then
2809 Set_Is_Tagged_Type (T);
2810 Make_Class_Wide_Type (T);
2811 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2812 end if;
2814 Push_Scope (T);
2816 Set_Stored_Constraint (T, No_Elist);
2818 if Present (Discriminant_Specifications (N)) then
2819 Process_Discriminants (N);
2820 end if;
2822 End_Scope;
2824 -- If the type has discriminants, non-trivial subtypes may be
2825 -- declared before the full view of the type. The full views of those
2826 -- subtypes will be built after the full view of the type.
2828 Set_Private_Dependents (T, New_Elmt_List);
2829 Set_Is_Pure (T, F);
2830 end Analyze_Incomplete_Type_Decl;
2832 -----------------------------------
2833 -- Analyze_Interface_Declaration --
2834 -----------------------------------
2836 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2837 CW : constant Entity_Id := Class_Wide_Type (T);
2839 begin
2840 Set_Is_Tagged_Type (T);
2842 Set_Is_Limited_Record (T, Limited_Present (Def)
2843 or else Task_Present (Def)
2844 or else Protected_Present (Def)
2845 or else Synchronized_Present (Def));
2847 -- Type is abstract if full declaration carries keyword, or if previous
2848 -- partial view did.
2850 Set_Is_Abstract_Type (T);
2851 Set_Is_Interface (T);
2853 -- Type is a limited interface if it includes the keyword limited, task,
2854 -- protected, or synchronized.
2856 Set_Is_Limited_Interface
2857 (T, Limited_Present (Def)
2858 or else Protected_Present (Def)
2859 or else Synchronized_Present (Def)
2860 or else Task_Present (Def));
2862 Set_Interfaces (T, New_Elmt_List);
2863 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2865 -- Complete the decoration of the class-wide entity if it was already
2866 -- built (i.e. during the creation of the limited view)
2868 if Present (CW) then
2869 Set_Is_Interface (CW);
2870 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2871 end if;
2873 -- Check runtime support for synchronized interfaces
2875 if VM_Target = No_VM
2876 and then (Is_Task_Interface (T)
2877 or else Is_Protected_Interface (T)
2878 or else Is_Synchronized_Interface (T))
2879 and then not RTE_Available (RE_Select_Specific_Data)
2880 then
2881 Error_Msg_CRT ("synchronized interfaces", T);
2882 end if;
2883 end Analyze_Interface_Declaration;
2885 -----------------------------
2886 -- Analyze_Itype_Reference --
2887 -----------------------------
2889 -- Nothing to do. This node is placed in the tree only for the benefit of
2890 -- back end processing, and has no effect on the semantic processing.
2892 procedure Analyze_Itype_Reference (N : Node_Id) is
2893 begin
2894 pragma Assert (Is_Itype (Itype (N)));
2895 null;
2896 end Analyze_Itype_Reference;
2898 --------------------------------
2899 -- Analyze_Number_Declaration --
2900 --------------------------------
2902 procedure Analyze_Number_Declaration (N : Node_Id) is
2903 Id : constant Entity_Id := Defining_Identifier (N);
2904 E : constant Node_Id := Expression (N);
2905 T : Entity_Id;
2906 Index : Interp_Index;
2907 It : Interp;
2909 begin
2910 Generate_Definition (Id);
2911 Enter_Name (Id);
2913 -- This is an optimization of a common case of an integer literal
2915 if Nkind (E) = N_Integer_Literal then
2916 Set_Is_Static_Expression (E, True);
2917 Set_Etype (E, Universal_Integer);
2919 Set_Etype (Id, Universal_Integer);
2920 Set_Ekind (Id, E_Named_Integer);
2921 Set_Is_Frozen (Id, True);
2922 return;
2923 end if;
2925 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2927 -- Process expression, replacing error by integer zero, to avoid
2928 -- cascaded errors or aborts further along in the processing
2930 -- Replace Error by integer zero, which seems least likely to cause
2931 -- cascaded errors.
2933 if E = Error then
2934 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2935 Set_Error_Posted (E);
2936 end if;
2938 Analyze (E);
2940 -- Verify that the expression is static and numeric. If
2941 -- the expression is overloaded, we apply the preference
2942 -- rule that favors root numeric types.
2944 if not Is_Overloaded (E) then
2945 T := Etype (E);
2946 if Has_Dynamic_Predicate_Aspect (T) then
2947 Error_Msg_N
2948 ("subtype has dynamic predicate, "
2949 & "not allowed in number declaration", N);
2950 end if;
2952 else
2953 T := Any_Type;
2955 Get_First_Interp (E, Index, It);
2956 while Present (It.Typ) loop
2957 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
2958 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2959 then
2960 if T = Any_Type then
2961 T := It.Typ;
2963 elsif It.Typ = Universal_Real
2964 or else It.Typ = Universal_Integer
2965 then
2966 -- Choose universal interpretation over any other
2968 T := It.Typ;
2969 exit;
2970 end if;
2971 end if;
2973 Get_Next_Interp (Index, It);
2974 end loop;
2975 end if;
2977 if Is_Integer_Type (T) then
2978 Resolve (E, T);
2979 Set_Etype (Id, Universal_Integer);
2980 Set_Ekind (Id, E_Named_Integer);
2982 elsif Is_Real_Type (T) then
2984 -- Because the real value is converted to universal_real, this is a
2985 -- legal context for a universal fixed expression.
2987 if T = Universal_Fixed then
2988 declare
2989 Loc : constant Source_Ptr := Sloc (N);
2990 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2991 Subtype_Mark =>
2992 New_Occurrence_Of (Universal_Real, Loc),
2993 Expression => Relocate_Node (E));
2995 begin
2996 Rewrite (E, Conv);
2997 Analyze (E);
2998 end;
3000 elsif T = Any_Fixed then
3001 Error_Msg_N ("illegal context for mixed mode operation", E);
3003 -- Expression is of the form : universal_fixed * integer. Try to
3004 -- resolve as universal_real.
3006 T := Universal_Real;
3007 Set_Etype (E, T);
3008 end if;
3010 Resolve (E, T);
3011 Set_Etype (Id, Universal_Real);
3012 Set_Ekind (Id, E_Named_Real);
3014 else
3015 Wrong_Type (E, Any_Numeric);
3016 Resolve (E, T);
3018 Set_Etype (Id, T);
3019 Set_Ekind (Id, E_Constant);
3020 Set_Never_Set_In_Source (Id, True);
3021 Set_Is_True_Constant (Id, True);
3022 return;
3023 end if;
3025 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3026 Set_Etype (E, Etype (Id));
3027 end if;
3029 if not Is_OK_Static_Expression (E) then
3030 Flag_Non_Static_Expr
3031 ("non-static expression used in number declaration!", E);
3032 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3033 Set_Etype (E, Any_Type);
3034 end if;
3035 end Analyze_Number_Declaration;
3037 -----------------------------
3038 -- Analyze_Object_Contract --
3039 -----------------------------
3041 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3042 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3043 AR_Val : Boolean := False;
3044 AW_Val : Boolean := False;
3045 ER_Val : Boolean := False;
3046 EW_Val : Boolean := False;
3047 Prag : Node_Id;
3048 Seen : Boolean := False;
3050 begin
3051 if Ekind (Obj_Id) = E_Constant then
3053 -- A constant cannot be effectively volatile. This check is only
3054 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3055 -- rule. Do not flag internally-generated constants that map generic
3056 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3058 if SPARK_Mode = On
3059 and then Is_Effectively_Volatile (Obj_Id)
3060 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3061 then
3062 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3063 end if;
3065 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3067 -- The following checks are only relevant when SPARK_Mode is on as
3068 -- they are not standard Ada legality rules. Internally generated
3069 -- temporaries are ignored.
3071 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3072 if Is_Effectively_Volatile (Obj_Id) then
3074 -- The declaration of an effectively volatile object must
3075 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3077 if not Is_Library_Level_Entity (Obj_Id) then
3078 Error_Msg_N
3079 ("volatile variable & must be declared at library level",
3080 Obj_Id);
3082 -- An object of a discriminated type cannot be effectively
3083 -- volatile (SPARK RM C.6(4)).
3085 elsif Has_Discriminants (Obj_Typ) then
3086 Error_Msg_N
3087 ("discriminated object & cannot be volatile", Obj_Id);
3089 -- An object of a tagged type cannot be effectively volatile
3090 -- (SPARK RM C.6(5)).
3092 elsif Is_Tagged_Type (Obj_Typ) then
3093 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3094 end if;
3096 -- The object is not effectively volatile
3098 else
3099 -- A non-effectively volatile object cannot have effectively
3100 -- volatile components (SPARK RM 7.1.3(7)).
3102 if not Is_Effectively_Volatile (Obj_Id)
3103 and then Has_Volatile_Component (Obj_Typ)
3104 then
3105 Error_Msg_N
3106 ("non-volatile object & cannot have volatile components",
3107 Obj_Id);
3108 end if;
3109 end if;
3110 end if;
3112 -- Analyze all external properties
3114 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3116 if Present (Prag) then
3117 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3118 Seen := True;
3119 end if;
3121 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3123 if Present (Prag) then
3124 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3125 Seen := True;
3126 end if;
3128 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3130 if Present (Prag) then
3131 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3132 Seen := True;
3133 end if;
3135 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3137 if Present (Prag) then
3138 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3139 Seen := True;
3140 end if;
3142 -- Verify the mutual interaction of the various external properties
3144 if Seen then
3145 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3146 end if;
3148 -- Check whether the lack of indicator Part_Of agrees with the
3149 -- placement of the variable with respect to the state space.
3151 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3153 if No (Prag) then
3154 Check_Missing_Part_Of (Obj_Id);
3155 end if;
3156 end if;
3157 end Analyze_Object_Contract;
3159 --------------------------------
3160 -- Analyze_Object_Declaration --
3161 --------------------------------
3163 procedure Analyze_Object_Declaration (N : Node_Id) is
3164 Loc : constant Source_Ptr := Sloc (N);
3165 Id : constant Entity_Id := Defining_Identifier (N);
3166 T : Entity_Id;
3167 Act_T : Entity_Id;
3169 E : Node_Id := Expression (N);
3170 -- E is set to Expression (N) throughout this routine. When
3171 -- Expression (N) is modified, E is changed accordingly.
3173 Prev_Entity : Entity_Id := Empty;
3175 function Count_Tasks (T : Entity_Id) return Uint;
3176 -- This function is called when a non-generic library level object of a
3177 -- task type is declared. Its function is to count the static number of
3178 -- tasks declared within the type (it is only called if Has_Tasks is set
3179 -- for T). As a side effect, if an array of tasks with non-static bounds
3180 -- or a variant record type is encountered, Check_Restrictions is called
3181 -- indicating the count is unknown.
3183 -----------------
3184 -- Count_Tasks --
3185 -----------------
3187 function Count_Tasks (T : Entity_Id) return Uint is
3188 C : Entity_Id;
3189 X : Node_Id;
3190 V : Uint;
3192 begin
3193 if Is_Task_Type (T) then
3194 return Uint_1;
3196 elsif Is_Record_Type (T) then
3197 if Has_Discriminants (T) then
3198 Check_Restriction (Max_Tasks, N);
3199 return Uint_0;
3201 else
3202 V := Uint_0;
3203 C := First_Component (T);
3204 while Present (C) loop
3205 V := V + Count_Tasks (Etype (C));
3206 Next_Component (C);
3207 end loop;
3209 return V;
3210 end if;
3212 elsif Is_Array_Type (T) then
3213 X := First_Index (T);
3214 V := Count_Tasks (Component_Type (T));
3215 while Present (X) loop
3216 C := Etype (X);
3218 if not Is_OK_Static_Subtype (C) then
3219 Check_Restriction (Max_Tasks, N);
3220 return Uint_0;
3221 else
3222 V := V * (UI_Max (Uint_0,
3223 Expr_Value (Type_High_Bound (C)) -
3224 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3225 end if;
3227 Next_Index (X);
3228 end loop;
3230 return V;
3232 else
3233 return Uint_0;
3234 end if;
3235 end Count_Tasks;
3237 -- Start of processing for Analyze_Object_Declaration
3239 begin
3240 -- There are three kinds of implicit types generated by an
3241 -- object declaration:
3243 -- 1. Those generated by the original Object Definition
3245 -- 2. Those generated by the Expression
3247 -- 3. Those used to constrain the Object Definition with the
3248 -- expression constraints when the definition is unconstrained.
3250 -- They must be generated in this order to avoid order of elaboration
3251 -- issues. Thus the first step (after entering the name) is to analyze
3252 -- the object definition.
3254 if Constant_Present (N) then
3255 Prev_Entity := Current_Entity_In_Scope (Id);
3257 if Present (Prev_Entity)
3258 and then
3259 -- If the homograph is an implicit subprogram, it is overridden
3260 -- by the current declaration.
3262 ((Is_Overloadable (Prev_Entity)
3263 and then Is_Inherited_Operation (Prev_Entity))
3265 -- The current object is a discriminal generated for an entry
3266 -- family index. Even though the index is a constant, in this
3267 -- particular context there is no true constant redeclaration.
3268 -- Enter_Name will handle the visibility.
3270 or else
3271 (Is_Discriminal (Id)
3272 and then Ekind (Discriminal_Link (Id)) =
3273 E_Entry_Index_Parameter)
3275 -- The current object is the renaming for a generic declared
3276 -- within the instance.
3278 or else
3279 (Ekind (Prev_Entity) = E_Package
3280 and then Nkind (Parent (Prev_Entity)) =
3281 N_Package_Renaming_Declaration
3282 and then not Comes_From_Source (Prev_Entity)
3283 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3284 then
3285 Prev_Entity := Empty;
3286 end if;
3287 end if;
3289 if Present (Prev_Entity) then
3290 Constant_Redeclaration (Id, N, T);
3292 Generate_Reference (Prev_Entity, Id, 'c');
3293 Set_Completion_Referenced (Id);
3295 if Error_Posted (N) then
3297 -- Type mismatch or illegal redeclaration, Do not analyze
3298 -- expression to avoid cascaded errors.
3300 T := Find_Type_Of_Object (Object_Definition (N), N);
3301 Set_Etype (Id, T);
3302 Set_Ekind (Id, E_Variable);
3303 goto Leave;
3304 end if;
3306 -- In the normal case, enter identifier at the start to catch premature
3307 -- usage in the initialization expression.
3309 else
3310 Generate_Definition (Id);
3311 Enter_Name (Id);
3313 Mark_Coextensions (N, Object_Definition (N));
3315 T := Find_Type_Of_Object (Object_Definition (N), N);
3317 if Nkind (Object_Definition (N)) = N_Access_Definition
3318 and then Present
3319 (Access_To_Subprogram_Definition (Object_Definition (N)))
3320 and then Protected_Present
3321 (Access_To_Subprogram_Definition (Object_Definition (N)))
3322 then
3323 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3324 end if;
3326 if Error_Posted (Id) then
3327 Set_Etype (Id, T);
3328 Set_Ekind (Id, E_Variable);
3329 goto Leave;
3330 end if;
3331 end if;
3333 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3334 -- out some static checks
3336 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3338 -- In case of aggregates we must also take care of the correct
3339 -- initialization of nested aggregates bug this is done at the
3340 -- point of the analysis of the aggregate (see sem_aggr.adb).
3342 if Present (Expression (N))
3343 and then Nkind (Expression (N)) = N_Aggregate
3344 then
3345 null;
3347 else
3348 declare
3349 Save_Typ : constant Entity_Id := Etype (Id);
3350 begin
3351 Set_Etype (Id, T); -- Temp. decoration for static checks
3352 Null_Exclusion_Static_Checks (N);
3353 Set_Etype (Id, Save_Typ);
3354 end;
3355 end if;
3356 end if;
3358 -- Object is marked pure if it is in a pure scope
3360 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3362 -- If deferred constant, make sure context is appropriate. We detect
3363 -- a deferred constant as a constant declaration with no expression.
3364 -- A deferred constant can appear in a package body if its completion
3365 -- is by means of an interface pragma.
3367 if Constant_Present (N) and then No (E) then
3369 -- A deferred constant may appear in the declarative part of the
3370 -- following constructs:
3372 -- blocks
3373 -- entry bodies
3374 -- extended return statements
3375 -- package specs
3376 -- package bodies
3377 -- subprogram bodies
3378 -- task bodies
3380 -- When declared inside a package spec, a deferred constant must be
3381 -- completed by a full constant declaration or pragma Import. In all
3382 -- other cases, the only proper completion is pragma Import. Extended
3383 -- return statements are flagged as invalid contexts because they do
3384 -- not have a declarative part and so cannot accommodate the pragma.
3386 if Ekind (Current_Scope) = E_Return_Statement then
3387 Error_Msg_N
3388 ("invalid context for deferred constant declaration (RM 7.4)",
3390 Error_Msg_N
3391 ("\declaration requires an initialization expression",
3393 Set_Constant_Present (N, False);
3395 -- In Ada 83, deferred constant must be of private type
3397 elsif not Is_Private_Type (T) then
3398 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3399 Error_Msg_N
3400 ("(Ada 83) deferred constant must be private type", N);
3401 end if;
3402 end if;
3404 -- If not a deferred constant, then object declaration freezes its type
3406 else
3407 Check_Fully_Declared (T, N);
3408 Freeze_Before (N, T);
3409 end if;
3411 -- If the object was created by a constrained array definition, then
3412 -- set the link in both the anonymous base type and anonymous subtype
3413 -- that are built to represent the array type to point to the object.
3415 if Nkind (Object_Definition (Declaration_Node (Id))) =
3416 N_Constrained_Array_Definition
3417 then
3418 Set_Related_Array_Object (T, Id);
3419 Set_Related_Array_Object (Base_Type (T), Id);
3420 end if;
3422 -- Special checks for protected objects not at library level
3424 if Is_Protected_Type (T)
3425 and then not Is_Library_Level_Entity (Id)
3426 then
3427 Check_Restriction (No_Local_Protected_Objects, Id);
3429 -- Protected objects with interrupt handlers must be at library level
3431 -- Ada 2005: This test is not needed (and the corresponding clause
3432 -- in the RM is removed) because accessibility checks are sufficient
3433 -- to make handlers not at the library level illegal.
3435 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3436 -- applies to the '95 version of the language as well.
3438 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3439 Error_Msg_N
3440 ("interrupt object can only be declared at library level", Id);
3441 end if;
3442 end if;
3444 -- The actual subtype of the object is the nominal subtype, unless
3445 -- the nominal one is unconstrained and obtained from the expression.
3447 Act_T := T;
3449 -- These checks should be performed before the initialization expression
3450 -- is considered, so that the Object_Definition node is still the same
3451 -- as in source code.
3453 -- In SPARK, the nominal subtype is always given by a subtype mark
3454 -- and must not be unconstrained. (The only exception to this is the
3455 -- acceptance of declarations of constants of type String.)
3457 if not
3458 Nkind_In (Object_Definition (N), N_Identifier, N_Expanded_Name)
3459 then
3460 Check_SPARK_05_Restriction
3461 ("subtype mark required", Object_Definition (N));
3463 elsif Is_Array_Type (T)
3464 and then not Is_Constrained (T)
3465 and then T /= Standard_String
3466 then
3467 Check_SPARK_05_Restriction
3468 ("subtype mark of constrained type expected",
3469 Object_Definition (N));
3470 end if;
3472 -- There are no aliased objects in SPARK
3474 if Aliased_Present (N) then
3475 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3476 end if;
3478 -- Process initialization expression if present and not in error
3480 if Present (E) and then E /= Error then
3482 -- Generate an error in case of CPP class-wide object initialization.
3483 -- Required because otherwise the expansion of the class-wide
3484 -- assignment would try to use 'size to initialize the object
3485 -- (primitive that is not available in CPP tagged types).
3487 if Is_Class_Wide_Type (Act_T)
3488 and then
3489 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3490 or else
3491 (Present (Full_View (Root_Type (Etype (Act_T))))
3492 and then
3493 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3494 then
3495 Error_Msg_N
3496 ("predefined assignment not available for 'C'P'P tagged types",
3498 end if;
3500 Mark_Coextensions (N, E);
3501 Analyze (E);
3503 -- In case of errors detected in the analysis of the expression,
3504 -- decorate it with the expected type to avoid cascaded errors
3506 if No (Etype (E)) then
3507 Set_Etype (E, T);
3508 end if;
3510 -- If an initialization expression is present, then we set the
3511 -- Is_True_Constant flag. It will be reset if this is a variable
3512 -- and it is indeed modified.
3514 Set_Is_True_Constant (Id, True);
3516 -- If we are analyzing a constant declaration, set its completion
3517 -- flag after analyzing and resolving the expression.
3519 if Constant_Present (N) then
3520 Set_Has_Completion (Id);
3521 end if;
3523 -- Set type and resolve (type may be overridden later on). Note:
3524 -- Ekind (Id) must still be E_Void at this point so that incorrect
3525 -- early usage within E is properly diagnosed.
3527 Set_Etype (Id, T);
3529 -- If the expression is an aggregate we must look ahead to detect
3530 -- the possible presence of an address clause, and defer resolution
3531 -- and expansion of the aggregate to the freeze point of the entity.
3533 if Comes_From_Source (N)
3534 and then Expander_Active
3535 and then Has_Following_Address_Clause (N)
3536 and then Nkind (E) = N_Aggregate
3537 then
3538 Set_Etype (E, T);
3540 else
3541 Resolve (E, T);
3542 end if;
3544 -- No further action needed if E is a call to an inlined function
3545 -- which returns an unconstrained type and it has been expanded into
3546 -- a procedure call. In that case N has been replaced by an object
3547 -- declaration without initializing expression and it has been
3548 -- analyzed (see Expand_Inlined_Call).
3550 if Back_End_Inlining
3551 and then Expander_Active
3552 and then Nkind (E) = N_Function_Call
3553 and then Nkind (Name (E)) in N_Has_Entity
3554 and then Is_Inlined (Entity (Name (E)))
3555 and then not Is_Constrained (Etype (E))
3556 and then Analyzed (N)
3557 and then No (Expression (N))
3558 then
3559 return;
3560 end if;
3562 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3563 -- node (which was marked already-analyzed), we need to set the type
3564 -- to something other than Any_Access in order to keep gigi happy.
3566 if Etype (E) = Any_Access then
3567 Set_Etype (E, T);
3568 end if;
3570 -- If the object is an access to variable, the initialization
3571 -- expression cannot be an access to constant.
3573 if Is_Access_Type (T)
3574 and then not Is_Access_Constant (T)
3575 and then Is_Access_Type (Etype (E))
3576 and then Is_Access_Constant (Etype (E))
3577 then
3578 Error_Msg_N
3579 ("access to variable cannot be initialized "
3580 & "with an access-to-constant expression", E);
3581 end if;
3583 if not Assignment_OK (N) then
3584 Check_Initialization (T, E);
3585 end if;
3587 Check_Unset_Reference (E);
3589 -- If this is a variable, then set current value. If this is a
3590 -- declared constant of a scalar type with a static expression,
3591 -- indicate that it is always valid.
3593 if not Constant_Present (N) then
3594 if Compile_Time_Known_Value (E) then
3595 Set_Current_Value (Id, E);
3596 end if;
3598 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3599 Set_Is_Known_Valid (Id);
3600 end if;
3602 -- Deal with setting of null flags
3604 if Is_Access_Type (T) then
3605 if Known_Non_Null (E) then
3606 Set_Is_Known_Non_Null (Id, True);
3607 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3608 Set_Is_Known_Null (Id, True);
3609 end if;
3610 end if;
3612 -- Check incorrect use of dynamically tagged expressions
3614 if Is_Tagged_Type (T) then
3615 Check_Dynamically_Tagged_Expression
3616 (Expr => E,
3617 Typ => T,
3618 Related_Nod => N);
3619 end if;
3621 Apply_Scalar_Range_Check (E, T);
3622 Apply_Static_Length_Check (E, T);
3624 if Nkind (Original_Node (N)) = N_Object_Declaration
3625 and then Comes_From_Source (Original_Node (N))
3627 -- Only call test if needed
3629 and then Restriction_Check_Required (SPARK_05)
3630 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3631 then
3632 Check_SPARK_05_Restriction
3633 ("initialization expression is not appropriate", E);
3634 end if;
3635 end if;
3637 -- If the No_Streams restriction is set, check that the type of the
3638 -- object is not, and does not contain, any subtype derived from
3639 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3640 -- Has_Stream just for efficiency reasons. There is no point in
3641 -- spending time on a Has_Stream check if the restriction is not set.
3643 if Restriction_Check_Required (No_Streams) then
3644 if Has_Stream (T) then
3645 Check_Restriction (No_Streams, N);
3646 end if;
3647 end if;
3649 -- Deal with predicate check before we start to do major rewriting. It
3650 -- is OK to initialize and then check the initialized value, since the
3651 -- object goes out of scope if we get a predicate failure. Note that we
3652 -- do this in the analyzer and not the expander because the analyzer
3653 -- does some substantial rewriting in some cases.
3655 -- We need a predicate check if the type has predicates, and if either
3656 -- there is an initializing expression, or for default initialization
3657 -- when we have at least one case of an explicit default initial value
3658 -- and then this is not an internal declaration whose initialization
3659 -- comes later (as for an aggregate expansion).
3661 if not Suppress_Assignment_Checks (N)
3662 and then Present (Predicate_Function (T))
3663 and then not No_Initialization (N)
3664 and then
3665 (Present (E)
3666 or else
3667 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3668 then
3669 -- If the type has a static predicate and the expression is known at
3670 -- compile time, see if the expression satisfies the predicate.
3672 if Present (E) then
3673 Check_Expression_Against_Static_Predicate (E, T);
3674 end if;
3676 Insert_After (N,
3677 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3678 end if;
3680 -- Case of unconstrained type
3682 if Is_Indefinite_Subtype (T) then
3684 -- In SPARK, a declaration of unconstrained type is allowed
3685 -- only for constants of type string.
3687 if Is_String_Type (T) and then not Constant_Present (N) then
3688 Check_SPARK_05_Restriction
3689 ("declaration of object of unconstrained type not allowed", N);
3690 end if;
3692 -- Nothing to do in deferred constant case
3694 if Constant_Present (N) and then No (E) then
3695 null;
3697 -- Case of no initialization present
3699 elsif No (E) then
3700 if No_Initialization (N) then
3701 null;
3703 elsif Is_Class_Wide_Type (T) then
3704 Error_Msg_N
3705 ("initialization required in class-wide declaration ", N);
3707 else
3708 Error_Msg_N
3709 ("unconstrained subtype not allowed (need initialization)",
3710 Object_Definition (N));
3712 if Is_Record_Type (T) and then Has_Discriminants (T) then
3713 Error_Msg_N
3714 ("\provide initial value or explicit discriminant values",
3715 Object_Definition (N));
3717 Error_Msg_NE
3718 ("\or give default discriminant values for type&",
3719 Object_Definition (N), T);
3721 elsif Is_Array_Type (T) then
3722 Error_Msg_N
3723 ("\provide initial value or explicit array bounds",
3724 Object_Definition (N));
3725 end if;
3726 end if;
3728 -- Case of initialization present but in error. Set initial
3729 -- expression as absent (but do not make above complaints)
3731 elsif E = Error then
3732 Set_Expression (N, Empty);
3733 E := Empty;
3735 -- Case of initialization present
3737 else
3738 -- Check restrictions in Ada 83
3740 if not Constant_Present (N) then
3742 -- Unconstrained variables not allowed in Ada 83 mode
3744 if Ada_Version = Ada_83
3745 and then Comes_From_Source (Object_Definition (N))
3746 then
3747 Error_Msg_N
3748 ("(Ada 83) unconstrained variable not allowed",
3749 Object_Definition (N));
3750 end if;
3751 end if;
3753 -- Now we constrain the variable from the initializing expression
3755 -- If the expression is an aggregate, it has been expanded into
3756 -- individual assignments. Retrieve the actual type from the
3757 -- expanded construct.
3759 if Is_Array_Type (T)
3760 and then No_Initialization (N)
3761 and then Nkind (Original_Node (E)) = N_Aggregate
3762 then
3763 Act_T := Etype (E);
3765 -- In case of class-wide interface object declarations we delay
3766 -- the generation of the equivalent record type declarations until
3767 -- its expansion because there are cases in they are not required.
3769 elsif Is_Interface (T) then
3770 null;
3772 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
3773 -- we should prevent the generation of another Itype with the
3774 -- same name as the one already generated, or we end up with
3775 -- two identical types in GNATprove.
3777 elsif GNATprove_Mode then
3778 null;
3780 else
3781 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3782 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3783 end if;
3785 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3787 if Aliased_Present (N) then
3788 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3789 end if;
3791 Freeze_Before (N, Act_T);
3792 Freeze_Before (N, T);
3793 end if;
3795 elsif Is_Array_Type (T)
3796 and then No_Initialization (N)
3797 and then Nkind (Original_Node (E)) = N_Aggregate
3798 then
3799 if not Is_Entity_Name (Object_Definition (N)) then
3800 Act_T := Etype (E);
3801 Check_Compile_Time_Size (Act_T);
3803 if Aliased_Present (N) then
3804 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3805 end if;
3806 end if;
3808 -- When the given object definition and the aggregate are specified
3809 -- independently, and their lengths might differ do a length check.
3810 -- This cannot happen if the aggregate is of the form (others =>...)
3812 if not Is_Constrained (T) then
3813 null;
3815 elsif Nkind (E) = N_Raise_Constraint_Error then
3817 -- Aggregate is statically illegal. Place back in declaration
3819 Set_Expression (N, E);
3820 Set_No_Initialization (N, False);
3822 elsif T = Etype (E) then
3823 null;
3825 elsif Nkind (E) = N_Aggregate
3826 and then Present (Component_Associations (E))
3827 and then Present (Choices (First (Component_Associations (E))))
3828 and then Nkind (First
3829 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3830 then
3831 null;
3833 else
3834 Apply_Length_Check (E, T);
3835 end if;
3837 -- If the type is limited unconstrained with defaulted discriminants and
3838 -- there is no expression, then the object is constrained by the
3839 -- defaults, so it is worthwhile building the corresponding subtype.
3841 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3842 and then not Is_Constrained (T)
3843 and then Has_Discriminants (T)
3844 then
3845 if No (E) then
3846 Act_T := Build_Default_Subtype (T, N);
3847 else
3848 -- Ada 2005: A limited object may be initialized by means of an
3849 -- aggregate. If the type has default discriminants it has an
3850 -- unconstrained nominal type, Its actual subtype will be obtained
3851 -- from the aggregate, and not from the default discriminants.
3853 Act_T := Etype (E);
3854 end if;
3856 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
3858 elsif Nkind (E) = N_Function_Call
3859 and then Constant_Present (N)
3860 and then Has_Unconstrained_Elements (Etype (E))
3861 then
3862 -- The back-end has problems with constants of a discriminated type
3863 -- with defaults, if the initial value is a function call. We
3864 -- generate an intermediate temporary that will receive a reference
3865 -- to the result of the call. The initialization expression then
3866 -- becomes a dereference of that temporary.
3868 Remove_Side_Effects (E);
3870 -- If this is a constant declaration of an unconstrained type and
3871 -- the initialization is an aggregate, we can use the subtype of the
3872 -- aggregate for the declared entity because it is immutable.
3874 elsif not Is_Constrained (T)
3875 and then Has_Discriminants (T)
3876 and then Constant_Present (N)
3877 and then not Has_Unchecked_Union (T)
3878 and then Nkind (E) = N_Aggregate
3879 then
3880 Act_T := Etype (E);
3881 end if;
3883 -- Check No_Wide_Characters restriction
3885 Check_Wide_Character_Restriction (T, Object_Definition (N));
3887 -- Indicate this is not set in source. Certainly true for constants, and
3888 -- true for variables so far (will be reset for a variable if and when
3889 -- we encounter a modification in the source).
3891 Set_Never_Set_In_Source (Id, True);
3893 -- Now establish the proper kind and type of the object
3895 if Constant_Present (N) then
3896 Set_Ekind (Id, E_Constant);
3897 Set_Is_True_Constant (Id);
3899 else
3900 Set_Ekind (Id, E_Variable);
3902 -- A variable is set as shared passive if it appears in a shared
3903 -- passive package, and is at the outer level. This is not done for
3904 -- entities generated during expansion, because those are always
3905 -- manipulated locally.
3907 if Is_Shared_Passive (Current_Scope)
3908 and then Is_Library_Level_Entity (Id)
3909 and then Comes_From_Source (Id)
3910 then
3911 Set_Is_Shared_Passive (Id);
3912 Check_Shared_Var (Id, T, N);
3913 end if;
3915 -- Set Has_Initial_Value if initializing expression present. Note
3916 -- that if there is no initializing expression, we leave the state
3917 -- of this flag unchanged (usually it will be False, but notably in
3918 -- the case of exception choice variables, it will already be true).
3920 if Present (E) then
3921 Set_Has_Initial_Value (Id, True);
3922 end if;
3924 Set_Contract (Id, Make_Contract (Sloc (Id)));
3925 end if;
3927 -- Initialize alignment and size and capture alignment setting
3929 Init_Alignment (Id);
3930 Init_Esize (Id);
3931 Set_Optimize_Alignment_Flags (Id);
3933 -- Deal with aliased case
3935 if Aliased_Present (N) then
3936 Set_Is_Aliased (Id);
3938 -- If the object is aliased and the type is unconstrained with
3939 -- defaulted discriminants and there is no expression, then the
3940 -- object is constrained by the defaults, so it is worthwhile
3941 -- building the corresponding subtype.
3943 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3944 -- unconstrained, then only establish an actual subtype if the
3945 -- nominal subtype is indefinite. In definite cases the object is
3946 -- unconstrained in Ada 2005.
3948 if No (E)
3949 and then Is_Record_Type (T)
3950 and then not Is_Constrained (T)
3951 and then Has_Discriminants (T)
3952 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3953 then
3954 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3955 end if;
3956 end if;
3958 -- Now we can set the type of the object
3960 Set_Etype (Id, Act_T);
3962 -- Non-constant object is marked to be treated as volatile if type is
3963 -- volatile and we clear the Current_Value setting that may have been
3964 -- set above. Doing so for constants isn't required and might interfere
3965 -- with possible uses of the object as a static expression in contexts
3966 -- incompatible with volatility (e.g. as a case-statement alternative).
3968 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
3969 Set_Treat_As_Volatile (Id);
3970 Set_Current_Value (Id, Empty);
3971 end if;
3973 -- Deal with controlled types
3975 if Has_Controlled_Component (Etype (Id))
3976 or else Is_Controlled (Etype (Id))
3977 then
3978 if not Is_Library_Level_Entity (Id) then
3979 Check_Restriction (No_Nested_Finalization, N);
3980 else
3981 Validate_Controlled_Object (Id);
3982 end if;
3983 end if;
3985 if Has_Task (Etype (Id)) then
3986 Check_Restriction (No_Tasking, N);
3988 -- Deal with counting max tasks
3990 -- Nothing to do if inside a generic
3992 if Inside_A_Generic then
3993 null;
3995 -- If library level entity, then count tasks
3997 elsif Is_Library_Level_Entity (Id) then
3998 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4000 -- If not library level entity, then indicate we don't know max
4001 -- tasks and also check task hierarchy restriction and blocking
4002 -- operation (since starting a task is definitely blocking).
4004 else
4005 Check_Restriction (Max_Tasks, N);
4006 Check_Restriction (No_Task_Hierarchy, N);
4007 Check_Potentially_Blocking_Operation (N);
4008 end if;
4010 -- A rather specialized test. If we see two tasks being declared
4011 -- of the same type in the same object declaration, and the task
4012 -- has an entry with an address clause, we know that program error
4013 -- will be raised at run time since we can't have two tasks with
4014 -- entries at the same address.
4016 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4017 declare
4018 E : Entity_Id;
4020 begin
4021 E := First_Entity (Etype (Id));
4022 while Present (E) loop
4023 if Ekind (E) = E_Entry
4024 and then Present (Get_Attribute_Definition_Clause
4025 (E, Attribute_Address))
4026 then
4027 Error_Msg_Warn := SPARK_Mode /= On;
4028 Error_Msg_N
4029 ("more than one task with same entry address<<", N);
4030 Error_Msg_N ("\Program_Error [<<", N);
4031 Insert_Action (N,
4032 Make_Raise_Program_Error (Loc,
4033 Reason => PE_Duplicated_Entry_Address));
4034 exit;
4035 end if;
4037 Next_Entity (E);
4038 end loop;
4039 end;
4040 end if;
4041 end if;
4043 -- Some simple constant-propagation: if the expression is a constant
4044 -- string initialized with a literal, share the literal. This avoids
4045 -- a run-time copy.
4047 if Present (E)
4048 and then Is_Entity_Name (E)
4049 and then Ekind (Entity (E)) = E_Constant
4050 and then Base_Type (Etype (E)) = Standard_String
4051 then
4052 declare
4053 Val : constant Node_Id := Constant_Value (Entity (E));
4054 begin
4055 if Present (Val) and then Nkind (Val) = N_String_Literal then
4056 Rewrite (E, New_Copy (Val));
4057 end if;
4058 end;
4059 end if;
4061 -- Another optimization: if the nominal subtype is unconstrained and
4062 -- the expression is a function call that returns an unconstrained
4063 -- type, rewrite the declaration as a renaming of the result of the
4064 -- call. The exceptions below are cases where the copy is expected,
4065 -- either by the back end (Aliased case) or by the semantics, as for
4066 -- initializing controlled types or copying tags for classwide types.
4068 if Present (E)
4069 and then Nkind (E) = N_Explicit_Dereference
4070 and then Nkind (Original_Node (E)) = N_Function_Call
4071 and then not Is_Library_Level_Entity (Id)
4072 and then not Is_Constrained (Underlying_Type (T))
4073 and then not Is_Aliased (Id)
4074 and then not Is_Class_Wide_Type (T)
4075 and then not Is_Controlled (T)
4076 and then not Has_Controlled_Component (Base_Type (T))
4077 and then Expander_Active
4078 then
4079 Rewrite (N,
4080 Make_Object_Renaming_Declaration (Loc,
4081 Defining_Identifier => Id,
4082 Access_Definition => Empty,
4083 Subtype_Mark => New_Occurrence_Of
4084 (Base_Type (Etype (Id)), Loc),
4085 Name => E));
4087 Set_Renamed_Object (Id, E);
4089 -- Force generation of debugging information for the constant and for
4090 -- the renamed function call.
4092 Set_Debug_Info_Needed (Id);
4093 Set_Debug_Info_Needed (Entity (Prefix (E)));
4094 end if;
4096 if Present (Prev_Entity)
4097 and then Is_Frozen (Prev_Entity)
4098 and then not Error_Posted (Id)
4099 then
4100 Error_Msg_N ("full constant declaration appears too late", N);
4101 end if;
4103 Check_Eliminated (Id);
4105 -- Deal with setting In_Private_Part flag if in private part
4107 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4108 then
4109 Set_In_Private_Part (Id);
4110 end if;
4112 -- Check for violation of No_Local_Timing_Events
4114 if Restriction_Check_Required (No_Local_Timing_Events)
4115 and then not Is_Library_Level_Entity (Id)
4116 and then Is_RTE (Etype (Id), RE_Timing_Event)
4117 then
4118 Check_Restriction (No_Local_Timing_Events, N);
4119 end if;
4121 <<Leave>>
4122 -- Initialize the refined state of a variable here because this is a
4123 -- common destination for legal and illegal object declarations.
4125 if Ekind (Id) = E_Variable then
4126 Set_Encapsulating_State (Id, Empty);
4127 end if;
4129 if Has_Aspects (N) then
4130 Analyze_Aspect_Specifications (N, Id);
4131 end if;
4133 Analyze_Dimension (N);
4135 -- Verify whether the object declaration introduces an illegal hidden
4136 -- state within a package subject to a null abstract state.
4138 if Ekind (Id) = E_Variable then
4139 Check_No_Hidden_State (Id);
4140 end if;
4141 end Analyze_Object_Declaration;
4143 ---------------------------
4144 -- Analyze_Others_Choice --
4145 ---------------------------
4147 -- Nothing to do for the others choice node itself, the semantic analysis
4148 -- of the others choice will occur as part of the processing of the parent
4150 procedure Analyze_Others_Choice (N : Node_Id) is
4151 pragma Warnings (Off, N);
4152 begin
4153 null;
4154 end Analyze_Others_Choice;
4156 -------------------------------------------
4157 -- Analyze_Private_Extension_Declaration --
4158 -------------------------------------------
4160 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4161 T : constant Entity_Id := Defining_Identifier (N);
4162 Indic : constant Node_Id := Subtype_Indication (N);
4163 Parent_Type : Entity_Id;
4164 Parent_Base : Entity_Id;
4166 begin
4167 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4169 if Is_Non_Empty_List (Interface_List (N)) then
4170 declare
4171 Intf : Node_Id;
4172 T : Entity_Id;
4174 begin
4175 Intf := First (Interface_List (N));
4176 while Present (Intf) loop
4177 T := Find_Type_Of_Subtype_Indic (Intf);
4179 Diagnose_Interface (Intf, T);
4180 Next (Intf);
4181 end loop;
4182 end;
4183 end if;
4185 Generate_Definition (T);
4187 -- For other than Ada 2012, just enter the name in the current scope
4189 if Ada_Version < Ada_2012 then
4190 Enter_Name (T);
4192 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4193 -- case of private type that completes an incomplete type.
4195 else
4196 declare
4197 Prev : Entity_Id;
4199 begin
4200 Prev := Find_Type_Name (N);
4202 pragma Assert (Prev = T
4203 or else (Ekind (Prev) = E_Incomplete_Type
4204 and then Present (Full_View (Prev))
4205 and then Full_View (Prev) = T));
4206 end;
4207 end if;
4209 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4210 Parent_Base := Base_Type (Parent_Type);
4212 if Parent_Type = Any_Type
4213 or else Etype (Parent_Type) = Any_Type
4214 then
4215 Set_Ekind (T, Ekind (Parent_Type));
4216 Set_Etype (T, Any_Type);
4217 goto Leave;
4219 elsif not Is_Tagged_Type (Parent_Type) then
4220 Error_Msg_N
4221 ("parent of type extension must be a tagged type ", Indic);
4222 goto Leave;
4224 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4225 Error_Msg_N ("premature derivation of incomplete type", Indic);
4226 goto Leave;
4228 elsif Is_Concurrent_Type (Parent_Type) then
4229 Error_Msg_N
4230 ("parent type of a private extension cannot be "
4231 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4233 Set_Etype (T, Any_Type);
4234 Set_Ekind (T, E_Limited_Private_Type);
4235 Set_Private_Dependents (T, New_Elmt_List);
4236 Set_Error_Posted (T);
4237 goto Leave;
4238 end if;
4240 -- Perhaps the parent type should be changed to the class-wide type's
4241 -- specific type in this case to prevent cascading errors ???
4243 if Is_Class_Wide_Type (Parent_Type) then
4244 Error_Msg_N
4245 ("parent of type extension must not be a class-wide type", Indic);
4246 goto Leave;
4247 end if;
4249 if (not Is_Package_Or_Generic_Package (Current_Scope)
4250 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4251 or else In_Private_Part (Current_Scope)
4253 then
4254 Error_Msg_N ("invalid context for private extension", N);
4255 end if;
4257 -- Set common attributes
4259 Set_Is_Pure (T, Is_Pure (Current_Scope));
4260 Set_Scope (T, Current_Scope);
4261 Set_Ekind (T, E_Record_Type_With_Private);
4262 Init_Size_Align (T);
4263 Set_Default_SSO (T);
4265 Set_Etype (T, Parent_Base);
4266 Set_Has_Task (T, Has_Task (Parent_Base));
4267 Set_Has_Protected (T, Has_Task (Parent_Base));
4269 Set_Convention (T, Convention (Parent_Type));
4270 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4271 Set_Is_First_Subtype (T);
4272 Make_Class_Wide_Type (T);
4274 if Unknown_Discriminants_Present (N) then
4275 Set_Discriminant_Constraint (T, No_Elist);
4276 end if;
4278 Build_Derived_Record_Type (N, Parent_Type, T);
4280 -- Propagate inherited invariant information. The new type has
4281 -- invariants, if the parent type has inheritable invariants,
4282 -- and these invariants can in turn be inherited.
4284 if Has_Inheritable_Invariants (Parent_Type) then
4285 Set_Has_Inheritable_Invariants (T);
4286 Set_Has_Invariants (T);
4287 end if;
4289 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4290 -- synchronized formal derived type.
4292 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4293 Set_Is_Limited_Record (T);
4295 -- Formal derived type case
4297 if Is_Generic_Type (T) then
4299 -- The parent must be a tagged limited type or a synchronized
4300 -- interface.
4302 if (not Is_Tagged_Type (Parent_Type)
4303 or else not Is_Limited_Type (Parent_Type))
4304 and then
4305 (not Is_Interface (Parent_Type)
4306 or else not Is_Synchronized_Interface (Parent_Type))
4307 then
4308 Error_Msg_NE ("parent type of & must be tagged limited " &
4309 "or synchronized", N, T);
4310 end if;
4312 -- The progenitors (if any) must be limited or synchronized
4313 -- interfaces.
4315 if Present (Interfaces (T)) then
4316 declare
4317 Iface : Entity_Id;
4318 Iface_Elmt : Elmt_Id;
4320 begin
4321 Iface_Elmt := First_Elmt (Interfaces (T));
4322 while Present (Iface_Elmt) loop
4323 Iface := Node (Iface_Elmt);
4325 if not Is_Limited_Interface (Iface)
4326 and then not Is_Synchronized_Interface (Iface)
4327 then
4328 Error_Msg_NE ("progenitor & must be limited " &
4329 "or synchronized", N, Iface);
4330 end if;
4332 Next_Elmt (Iface_Elmt);
4333 end loop;
4334 end;
4335 end if;
4337 -- Regular derived extension, the parent must be a limited or
4338 -- synchronized interface.
4340 else
4341 if not Is_Interface (Parent_Type)
4342 or else (not Is_Limited_Interface (Parent_Type)
4343 and then not Is_Synchronized_Interface (Parent_Type))
4344 then
4345 Error_Msg_NE
4346 ("parent type of & must be limited interface", N, T);
4347 end if;
4348 end if;
4350 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4351 -- extension with a synchronized parent must be explicitly declared
4352 -- synchronized, because the full view will be a synchronized type.
4353 -- This must be checked before the check for limited types below,
4354 -- to ensure that types declared limited are not allowed to extend
4355 -- synchronized interfaces.
4357 elsif Is_Interface (Parent_Type)
4358 and then Is_Synchronized_Interface (Parent_Type)
4359 and then not Synchronized_Present (N)
4360 then
4361 Error_Msg_NE
4362 ("private extension of& must be explicitly synchronized",
4363 N, Parent_Type);
4365 elsif Limited_Present (N) then
4366 Set_Is_Limited_Record (T);
4368 if not Is_Limited_Type (Parent_Type)
4369 and then
4370 (not Is_Interface (Parent_Type)
4371 or else not Is_Limited_Interface (Parent_Type))
4372 then
4373 Error_Msg_NE ("parent type& of limited extension must be limited",
4374 N, Parent_Type);
4375 end if;
4376 end if;
4378 <<Leave>>
4379 if Has_Aspects (N) then
4380 Analyze_Aspect_Specifications (N, T);
4381 end if;
4382 end Analyze_Private_Extension_Declaration;
4384 ---------------------------------
4385 -- Analyze_Subtype_Declaration --
4386 ---------------------------------
4388 procedure Analyze_Subtype_Declaration
4389 (N : Node_Id;
4390 Skip : Boolean := False)
4392 Id : constant Entity_Id := Defining_Identifier (N);
4393 T : Entity_Id;
4394 R_Checks : Check_Result;
4396 begin
4397 Generate_Definition (Id);
4398 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4399 Init_Size_Align (Id);
4401 -- The following guard condition on Enter_Name is to handle cases where
4402 -- the defining identifier has already been entered into the scope but
4403 -- the declaration as a whole needs to be analyzed.
4405 -- This case in particular happens for derived enumeration types. The
4406 -- derived enumeration type is processed as an inserted enumeration type
4407 -- declaration followed by a rewritten subtype declaration. The defining
4408 -- identifier, however, is entered into the name scope very early in the
4409 -- processing of the original type declaration and therefore needs to be
4410 -- avoided here, when the created subtype declaration is analyzed. (See
4411 -- Build_Derived_Types)
4413 -- This also happens when the full view of a private type is derived
4414 -- type with constraints. In this case the entity has been introduced
4415 -- in the private declaration.
4417 -- Finally this happens in some complex cases when validity checks are
4418 -- enabled, where the same subtype declaration may be analyzed twice.
4419 -- This can happen if the subtype is created by the pre-analysis of
4420 -- an attribute tht gives the range of a loop statement, and the loop
4421 -- itself appears within an if_statement that will be rewritten during
4422 -- expansion.
4424 if Skip
4425 or else (Present (Etype (Id))
4426 and then (Is_Private_Type (Etype (Id))
4427 or else Is_Task_Type (Etype (Id))
4428 or else Is_Rewrite_Substitution (N)))
4429 then
4430 null;
4432 elsif Current_Entity (Id) = Id then
4433 null;
4435 else
4436 Enter_Name (Id);
4437 end if;
4439 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4441 -- Class-wide equivalent types of records with unknown discriminants
4442 -- involve the generation of an itype which serves as the private view
4443 -- of a constrained record subtype. In such cases the base type of the
4444 -- current subtype we are processing is the private itype. Use the full
4445 -- of the private itype when decorating various attributes.
4447 if Is_Itype (T)
4448 and then Is_Private_Type (T)
4449 and then Present (Full_View (T))
4450 then
4451 T := Full_View (T);
4452 end if;
4454 -- Inherit common attributes
4456 Set_Is_Volatile (Id, Is_Volatile (T));
4457 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4458 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4459 Set_Convention (Id, Convention (T));
4461 -- If ancestor has predicates then so does the subtype, and in addition
4462 -- we must delay the freeze to properly arrange predicate inheritance.
4464 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4465 -- in which T = ID, so the above tests and assignments do nothing???
4467 if Has_Predicates (T)
4468 or else (Present (Ancestor_Subtype (T))
4469 and then Has_Predicates (Ancestor_Subtype (T)))
4470 then
4471 Set_Has_Predicates (Id);
4472 Set_Has_Delayed_Freeze (Id);
4473 end if;
4475 -- Subtype of Boolean cannot have a constraint in SPARK
4477 if Is_Boolean_Type (T)
4478 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4479 then
4480 Check_SPARK_05_Restriction
4481 ("subtype of Boolean cannot have constraint", N);
4482 end if;
4484 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4485 declare
4486 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4487 One_Cstr : Node_Id;
4488 Low : Node_Id;
4489 High : Node_Id;
4491 begin
4492 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4493 One_Cstr := First (Constraints (Cstr));
4494 while Present (One_Cstr) loop
4496 -- Index or discriminant constraint in SPARK must be a
4497 -- subtype mark.
4499 if not
4500 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4501 then
4502 Check_SPARK_05_Restriction
4503 ("subtype mark required", One_Cstr);
4505 -- String subtype must have a lower bound of 1 in SPARK.
4506 -- Note that we do not need to test for the non-static case
4507 -- here, since that was already taken care of in
4508 -- Process_Range_Expr_In_Decl.
4510 elsif Base_Type (T) = Standard_String then
4511 Get_Index_Bounds (One_Cstr, Low, High);
4513 if Is_OK_Static_Expression (Low)
4514 and then Expr_Value (Low) /= 1
4515 then
4516 Check_SPARK_05_Restriction
4517 ("String subtype must have lower bound of 1", N);
4518 end if;
4519 end if;
4521 Next (One_Cstr);
4522 end loop;
4523 end if;
4524 end;
4525 end if;
4527 -- In the case where there is no constraint given in the subtype
4528 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4529 -- semantic attributes must be established here.
4531 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4532 Set_Etype (Id, Base_Type (T));
4534 -- Subtype of unconstrained array without constraint is not allowed
4535 -- in SPARK.
4537 if Is_Array_Type (T) and then not Is_Constrained (T) then
4538 Check_SPARK_05_Restriction
4539 ("subtype of unconstrained array must have constraint", N);
4540 end if;
4542 case Ekind (T) is
4543 when Array_Kind =>
4544 Set_Ekind (Id, E_Array_Subtype);
4545 Copy_Array_Subtype_Attributes (Id, T);
4547 when Decimal_Fixed_Point_Kind =>
4548 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4549 Set_Digits_Value (Id, Digits_Value (T));
4550 Set_Delta_Value (Id, Delta_Value (T));
4551 Set_Scale_Value (Id, Scale_Value (T));
4552 Set_Small_Value (Id, Small_Value (T));
4553 Set_Scalar_Range (Id, Scalar_Range (T));
4554 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4555 Set_Is_Constrained (Id, Is_Constrained (T));
4556 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4557 Set_RM_Size (Id, RM_Size (T));
4559 when Enumeration_Kind =>
4560 Set_Ekind (Id, E_Enumeration_Subtype);
4561 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4562 Set_Scalar_Range (Id, Scalar_Range (T));
4563 Set_Is_Character_Type (Id, Is_Character_Type (T));
4564 Set_Is_Constrained (Id, Is_Constrained (T));
4565 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4566 Set_RM_Size (Id, RM_Size (T));
4567 Inherit_Predicate_Flags (Id, T);
4569 when Ordinary_Fixed_Point_Kind =>
4570 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4571 Set_Scalar_Range (Id, Scalar_Range (T));
4572 Set_Small_Value (Id, Small_Value (T));
4573 Set_Delta_Value (Id, Delta_Value (T));
4574 Set_Is_Constrained (Id, Is_Constrained (T));
4575 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4576 Set_RM_Size (Id, RM_Size (T));
4578 when Float_Kind =>
4579 Set_Ekind (Id, E_Floating_Point_Subtype);
4580 Set_Scalar_Range (Id, Scalar_Range (T));
4581 Set_Digits_Value (Id, Digits_Value (T));
4582 Set_Is_Constrained (Id, Is_Constrained (T));
4584 when Signed_Integer_Kind =>
4585 Set_Ekind (Id, E_Signed_Integer_Subtype);
4586 Set_Scalar_Range (Id, Scalar_Range (T));
4587 Set_Is_Constrained (Id, Is_Constrained (T));
4588 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4589 Set_RM_Size (Id, RM_Size (T));
4590 Inherit_Predicate_Flags (Id, T);
4592 when Modular_Integer_Kind =>
4593 Set_Ekind (Id, E_Modular_Integer_Subtype);
4594 Set_Scalar_Range (Id, Scalar_Range (T));
4595 Set_Is_Constrained (Id, Is_Constrained (T));
4596 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4597 Set_RM_Size (Id, RM_Size (T));
4598 Inherit_Predicate_Flags (Id, T);
4600 when Class_Wide_Kind =>
4601 Set_Ekind (Id, E_Class_Wide_Subtype);
4602 Set_First_Entity (Id, First_Entity (T));
4603 Set_Last_Entity (Id, Last_Entity (T));
4604 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4605 Set_Cloned_Subtype (Id, T);
4606 Set_Is_Tagged_Type (Id, True);
4607 Set_Has_Unknown_Discriminants
4608 (Id, True);
4610 if Ekind (T) = E_Class_Wide_Subtype then
4611 Set_Equivalent_Type (Id, Equivalent_Type (T));
4612 end if;
4614 when E_Record_Type | E_Record_Subtype =>
4615 Set_Ekind (Id, E_Record_Subtype);
4617 if Ekind (T) = E_Record_Subtype
4618 and then Present (Cloned_Subtype (T))
4619 then
4620 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4621 else
4622 Set_Cloned_Subtype (Id, T);
4623 end if;
4625 Set_First_Entity (Id, First_Entity (T));
4626 Set_Last_Entity (Id, Last_Entity (T));
4627 Set_Has_Discriminants (Id, Has_Discriminants (T));
4628 Set_Is_Constrained (Id, Is_Constrained (T));
4629 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4630 Set_Has_Implicit_Dereference
4631 (Id, Has_Implicit_Dereference (T));
4632 Set_Has_Unknown_Discriminants
4633 (Id, Has_Unknown_Discriminants (T));
4635 if Has_Discriminants (T) then
4636 Set_Discriminant_Constraint
4637 (Id, Discriminant_Constraint (T));
4638 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4640 elsif Has_Unknown_Discriminants (Id) then
4641 Set_Discriminant_Constraint (Id, No_Elist);
4642 end if;
4644 if Is_Tagged_Type (T) then
4645 Set_Is_Tagged_Type (Id);
4646 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4647 Set_Direct_Primitive_Operations
4648 (Id, Direct_Primitive_Operations (T));
4649 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4651 if Is_Interface (T) then
4652 Set_Is_Interface (Id);
4653 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4654 end if;
4655 end if;
4657 when Private_Kind =>
4658 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4659 Set_Has_Discriminants (Id, Has_Discriminants (T));
4660 Set_Is_Constrained (Id, Is_Constrained (T));
4661 Set_First_Entity (Id, First_Entity (T));
4662 Set_Last_Entity (Id, Last_Entity (T));
4663 Set_Private_Dependents (Id, New_Elmt_List);
4664 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4665 Set_Has_Implicit_Dereference
4666 (Id, Has_Implicit_Dereference (T));
4667 Set_Has_Unknown_Discriminants
4668 (Id, Has_Unknown_Discriminants (T));
4669 Set_Known_To_Have_Preelab_Init
4670 (Id, Known_To_Have_Preelab_Init (T));
4672 if Is_Tagged_Type (T) then
4673 Set_Is_Tagged_Type (Id);
4674 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4675 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4676 Set_Direct_Primitive_Operations (Id,
4677 Direct_Primitive_Operations (T));
4678 end if;
4680 -- In general the attributes of the subtype of a private type
4681 -- are the attributes of the partial view of parent. However,
4682 -- the full view may be a discriminated type, and the subtype
4683 -- must share the discriminant constraint to generate correct
4684 -- calls to initialization procedures.
4686 if Has_Discriminants (T) then
4687 Set_Discriminant_Constraint
4688 (Id, Discriminant_Constraint (T));
4689 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4691 elsif Present (Full_View (T))
4692 and then Has_Discriminants (Full_View (T))
4693 then
4694 Set_Discriminant_Constraint
4695 (Id, Discriminant_Constraint (Full_View (T)));
4696 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4698 -- This would seem semantically correct, but apparently
4699 -- generates spurious errors about missing components ???
4701 -- Set_Has_Discriminants (Id);
4702 end if;
4704 Prepare_Private_Subtype_Completion (Id, N);
4706 -- If this is the subtype of a constrained private type with
4707 -- discriminants that has got a full view and we also have
4708 -- built a completion just above, show that the completion
4709 -- is a clone of the full view to the back-end.
4711 if Has_Discriminants (T)
4712 and then not Has_Unknown_Discriminants (T)
4713 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
4714 and then Present (Full_View (T))
4715 and then Present (Full_View (Id))
4716 then
4717 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
4718 end if;
4720 when Access_Kind =>
4721 Set_Ekind (Id, E_Access_Subtype);
4722 Set_Is_Constrained (Id, Is_Constrained (T));
4723 Set_Is_Access_Constant
4724 (Id, Is_Access_Constant (T));
4725 Set_Directly_Designated_Type
4726 (Id, Designated_Type (T));
4727 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4729 -- A Pure library_item must not contain the declaration of a
4730 -- named access type, except within a subprogram, generic
4731 -- subprogram, task unit, or protected unit, or if it has
4732 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4734 if Comes_From_Source (Id)
4735 and then In_Pure_Unit
4736 and then not In_Subprogram_Task_Protected_Unit
4737 and then not No_Pool_Assigned (Id)
4738 then
4739 Error_Msg_N
4740 ("named access types not allowed in pure unit", N);
4741 end if;
4743 when Concurrent_Kind =>
4744 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4745 Set_Corresponding_Record_Type (Id,
4746 Corresponding_Record_Type (T));
4747 Set_First_Entity (Id, First_Entity (T));
4748 Set_First_Private_Entity (Id, First_Private_Entity (T));
4749 Set_Has_Discriminants (Id, Has_Discriminants (T));
4750 Set_Is_Constrained (Id, Is_Constrained (T));
4751 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4752 Set_Last_Entity (Id, Last_Entity (T));
4754 if Has_Discriminants (T) then
4755 Set_Discriminant_Constraint (Id,
4756 Discriminant_Constraint (T));
4757 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4758 end if;
4760 when E_Incomplete_Type =>
4761 if Ada_Version >= Ada_2005 then
4763 -- In Ada 2005 an incomplete type can be explicitly tagged:
4764 -- propagate indication.
4766 Set_Ekind (Id, E_Incomplete_Subtype);
4767 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4768 Set_Private_Dependents (Id, New_Elmt_List);
4770 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
4771 -- incomplete type visible through a limited with clause.
4773 if From_Limited_With (T)
4774 and then Present (Non_Limited_View (T))
4775 then
4776 Set_From_Limited_With (Id);
4777 Set_Non_Limited_View (Id, Non_Limited_View (T));
4779 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4780 -- to the private dependents of the original incomplete
4781 -- type for future transformation.
4783 else
4784 Append_Elmt (Id, Private_Dependents (T));
4785 end if;
4787 -- If the subtype name denotes an incomplete type an error
4788 -- was already reported by Process_Subtype.
4790 else
4791 Set_Etype (Id, Any_Type);
4792 end if;
4794 when others =>
4795 raise Program_Error;
4796 end case;
4797 end if;
4799 if Etype (Id) = Any_Type then
4800 goto Leave;
4801 end if;
4803 -- Some common processing on all types
4805 Set_Size_Info (Id, T);
4806 Set_First_Rep_Item (Id, First_Rep_Item (T));
4808 -- If the parent type is a generic actual, so is the subtype. This may
4809 -- happen in a nested instance. Why Comes_From_Source test???
4811 if not Comes_From_Source (N) then
4812 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
4813 end if;
4815 T := Etype (Id);
4817 Set_Is_Immediately_Visible (Id, True);
4818 Set_Depends_On_Private (Id, Has_Private_Component (T));
4819 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
4821 if Is_Interface (T) then
4822 Set_Is_Interface (Id);
4823 end if;
4825 if Present (Generic_Parent_Type (N))
4826 and then
4827 (Nkind (Parent (Generic_Parent_Type (N))) /=
4828 N_Formal_Type_Declaration
4829 or else Nkind
4830 (Formal_Type_Definition (Parent (Generic_Parent_Type (N)))) /=
4831 N_Formal_Private_Type_Definition)
4832 then
4833 if Is_Tagged_Type (Id) then
4835 -- If this is a generic actual subtype for a synchronized type,
4836 -- the primitive operations are those of the corresponding record
4837 -- for which there is a separate subtype declaration.
4839 if Is_Concurrent_Type (Id) then
4840 null;
4841 elsif Is_Class_Wide_Type (Id) then
4842 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4843 else
4844 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4845 end if;
4847 elsif Scope (Etype (Id)) /= Standard_Standard then
4848 Derive_Subprograms (Generic_Parent_Type (N), Id);
4849 end if;
4850 end if;
4852 if Is_Private_Type (T) and then Present (Full_View (T)) then
4853 Conditional_Delay (Id, Full_View (T));
4855 -- The subtypes of components or subcomponents of protected types
4856 -- do not need freeze nodes, which would otherwise appear in the
4857 -- wrong scope (before the freeze node for the protected type). The
4858 -- proper subtypes are those of the subcomponents of the corresponding
4859 -- record.
4861 elsif Ekind (Scope (Id)) /= E_Protected_Type
4862 and then Present (Scope (Scope (Id))) -- error defense
4863 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4864 then
4865 Conditional_Delay (Id, T);
4866 end if;
4868 -- Check that Constraint_Error is raised for a scalar subtype indication
4869 -- when the lower or upper bound of a non-null range lies outside the
4870 -- range of the type mark.
4872 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4873 if Is_Scalar_Type (Etype (Id))
4874 and then Scalar_Range (Id) /=
4875 Scalar_Range (Etype (Subtype_Mark
4876 (Subtype_Indication (N))))
4877 then
4878 Apply_Range_Check
4879 (Scalar_Range (Id),
4880 Etype (Subtype_Mark (Subtype_Indication (N))));
4882 -- In the array case, check compatibility for each index
4884 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
4885 then
4886 -- This really should be a subprogram that finds the indications
4887 -- to check???
4889 declare
4890 Subt_Index : Node_Id := First_Index (Id);
4891 Target_Index : Node_Id :=
4892 First_Index (Etype
4893 (Subtype_Mark (Subtype_Indication (N))));
4894 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
4896 begin
4897 while Present (Subt_Index) loop
4898 if ((Nkind (Subt_Index) = N_Identifier
4899 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
4900 or else Nkind (Subt_Index) = N_Subtype_Indication)
4901 and then
4902 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
4903 then
4904 declare
4905 Target_Typ : constant Entity_Id :=
4906 Etype (Target_Index);
4907 begin
4908 R_Checks :=
4909 Get_Range_Checks
4910 (Scalar_Range (Etype (Subt_Index)),
4911 Target_Typ,
4912 Etype (Subt_Index),
4913 Defining_Identifier (N));
4915 -- Reset Has_Dynamic_Range_Check on the subtype to
4916 -- prevent elision of the index check due to a dynamic
4917 -- check generated for a preceding index (needed since
4918 -- Insert_Range_Checks tries to avoid generating
4919 -- redundant checks on a given declaration).
4921 Set_Has_Dynamic_Range_Check (N, False);
4923 Insert_Range_Checks
4924 (R_Checks,
4926 Target_Typ,
4927 Sloc (Defining_Identifier (N)));
4929 -- Record whether this index involved a dynamic check
4931 Has_Dyn_Chk :=
4932 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
4933 end;
4934 end if;
4936 Next_Index (Subt_Index);
4937 Next_Index (Target_Index);
4938 end loop;
4940 -- Finally, mark whether the subtype involves dynamic checks
4942 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
4943 end;
4944 end if;
4945 end if;
4947 -- A type invariant applies to any subtype in its scope, in particular
4948 -- to a generic actual.
4950 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
4951 Set_Has_Invariants (Id);
4952 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
4953 end if;
4955 -- Make sure that generic actual types are properly frozen. The subtype
4956 -- is marked as a generic actual type when the enclosing instance is
4957 -- analyzed, so here we identify the subtype from the tree structure.
4959 if Expander_Active
4960 and then Is_Generic_Actual_Type (Id)
4961 and then In_Instance
4962 and then not Comes_From_Source (N)
4963 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4964 and then Is_Frozen (T)
4965 then
4966 Freeze_Before (N, Id);
4967 end if;
4969 Set_Optimize_Alignment_Flags (Id);
4970 Check_Eliminated (Id);
4972 <<Leave>>
4973 if Has_Aspects (N) then
4974 Analyze_Aspect_Specifications (N, Id);
4975 end if;
4977 Analyze_Dimension (N);
4978 end Analyze_Subtype_Declaration;
4980 --------------------------------
4981 -- Analyze_Subtype_Indication --
4982 --------------------------------
4984 procedure Analyze_Subtype_Indication (N : Node_Id) is
4985 T : constant Entity_Id := Subtype_Mark (N);
4986 R : constant Node_Id := Range_Expression (Constraint (N));
4988 begin
4989 Analyze (T);
4991 if R /= Error then
4992 Analyze (R);
4993 Set_Etype (N, Etype (R));
4994 Resolve (R, Entity (T));
4995 else
4996 Set_Error_Posted (R);
4997 Set_Error_Posted (T);
4998 end if;
4999 end Analyze_Subtype_Indication;
5001 --------------------------
5002 -- Analyze_Variant_Part --
5003 --------------------------
5005 procedure Analyze_Variant_Part (N : Node_Id) is
5006 Discr_Name : Node_Id;
5007 Discr_Type : Entity_Id;
5009 procedure Process_Variant (A : Node_Id);
5010 -- Analyze declarations for a single variant
5012 package Analyze_Variant_Choices is
5013 new Generic_Analyze_Choices (Process_Variant);
5014 use Analyze_Variant_Choices;
5016 ---------------------
5017 -- Process_Variant --
5018 ---------------------
5020 procedure Process_Variant (A : Node_Id) is
5021 CL : constant Node_Id := Component_List (A);
5022 begin
5023 if not Null_Present (CL) then
5024 Analyze_Declarations (Component_Items (CL));
5026 if Present (Variant_Part (CL)) then
5027 Analyze (Variant_Part (CL));
5028 end if;
5029 end if;
5030 end Process_Variant;
5032 -- Start of processing for Analyze_Variant_Part
5034 begin
5035 Discr_Name := Name (N);
5036 Analyze (Discr_Name);
5038 -- If Discr_Name bad, get out (prevent cascaded errors)
5040 if Etype (Discr_Name) = Any_Type then
5041 return;
5042 end if;
5044 -- Check invalid discriminant in variant part
5046 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5047 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5048 end if;
5050 Discr_Type := Etype (Entity (Discr_Name));
5052 if not Is_Discrete_Type (Discr_Type) then
5053 Error_Msg_N
5054 ("discriminant in a variant part must be of a discrete type",
5055 Name (N));
5056 return;
5057 end if;
5059 -- Now analyze the choices, which also analyzes the declarations that
5060 -- are associated with each choice.
5062 Analyze_Choices (Variants (N), Discr_Type);
5064 -- Note: we used to instantiate and call Check_Choices here to check
5065 -- that the choices covered the discriminant, but it's too early to do
5066 -- that because of statically predicated subtypes, whose analysis may
5067 -- be deferred to their freeze point which may be as late as the freeze
5068 -- point of the containing record. So this call is now to be found in
5069 -- Freeze_Record_Declaration.
5071 end Analyze_Variant_Part;
5073 ----------------------------
5074 -- Array_Type_Declaration --
5075 ----------------------------
5077 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5078 Component_Def : constant Node_Id := Component_Definition (Def);
5079 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5080 Element_Type : Entity_Id;
5081 Implicit_Base : Entity_Id;
5082 Index : Node_Id;
5083 Related_Id : Entity_Id := Empty;
5084 Nb_Index : Nat;
5085 P : constant Node_Id := Parent (Def);
5086 Priv : Entity_Id;
5088 begin
5089 if Nkind (Def) = N_Constrained_Array_Definition then
5090 Index := First (Discrete_Subtype_Definitions (Def));
5091 else
5092 Index := First (Subtype_Marks (Def));
5093 end if;
5095 -- Find proper names for the implicit types which may be public. In case
5096 -- of anonymous arrays we use the name of the first object of that type
5097 -- as prefix.
5099 if No (T) then
5100 Related_Id := Defining_Identifier (P);
5101 else
5102 Related_Id := T;
5103 end if;
5105 Nb_Index := 1;
5106 while Present (Index) loop
5107 Analyze (Index);
5109 -- Test for odd case of trying to index a type by the type itself
5111 if Is_Entity_Name (Index) and then Entity (Index) = T then
5112 Error_Msg_N ("type& cannot be indexed by itself", Index);
5113 Set_Entity (Index, Standard_Boolean);
5114 Set_Etype (Index, Standard_Boolean);
5115 end if;
5117 -- Check SPARK restriction requiring a subtype mark
5119 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5120 Check_SPARK_05_Restriction ("subtype mark required", Index);
5121 end if;
5123 -- Add a subtype declaration for each index of private array type
5124 -- declaration whose etype is also private. For example:
5126 -- package Pkg is
5127 -- type Index is private;
5128 -- private
5129 -- type Table is array (Index) of ...
5130 -- end;
5132 -- This is currently required by the expander for the internally
5133 -- generated equality subprogram of records with variant parts in
5134 -- which the etype of some component is such private type.
5136 if Ekind (Current_Scope) = E_Package
5137 and then In_Private_Part (Current_Scope)
5138 and then Has_Private_Declaration (Etype (Index))
5139 then
5140 declare
5141 Loc : constant Source_Ptr := Sloc (Def);
5142 New_E : Entity_Id;
5143 Decl : Entity_Id;
5145 begin
5146 New_E := Make_Temporary (Loc, 'T');
5147 Set_Is_Internal (New_E);
5149 Decl :=
5150 Make_Subtype_Declaration (Loc,
5151 Defining_Identifier => New_E,
5152 Subtype_Indication =>
5153 New_Occurrence_Of (Etype (Index), Loc));
5155 Insert_Before (Parent (Def), Decl);
5156 Analyze (Decl);
5157 Set_Etype (Index, New_E);
5159 -- If the index is a range the Entity attribute is not
5160 -- available. Example:
5162 -- package Pkg is
5163 -- type T is private;
5164 -- private
5165 -- type T is new Natural;
5166 -- Table : array (T(1) .. T(10)) of Boolean;
5167 -- end Pkg;
5169 if Nkind (Index) /= N_Range then
5170 Set_Entity (Index, New_E);
5171 end if;
5172 end;
5173 end if;
5175 Make_Index (Index, P, Related_Id, Nb_Index);
5177 -- Check error of subtype with predicate for index type
5179 Bad_Predicated_Subtype_Use
5180 ("subtype& has predicate, not allowed as index subtype",
5181 Index, Etype (Index));
5183 -- Move to next index
5185 Next_Index (Index);
5186 Nb_Index := Nb_Index + 1;
5187 end loop;
5189 -- Process subtype indication if one is present
5191 if Present (Component_Typ) then
5192 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5194 Set_Etype (Component_Typ, Element_Type);
5196 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5197 Check_SPARK_05_Restriction
5198 ("subtype mark required", Component_Typ);
5199 end if;
5201 -- Ada 2005 (AI-230): Access Definition case
5203 else pragma Assert (Present (Access_Definition (Component_Def)));
5205 -- Indicate that the anonymous access type is created by the
5206 -- array type declaration.
5208 Element_Type := Access_Definition
5209 (Related_Nod => P,
5210 N => Access_Definition (Component_Def));
5211 Set_Is_Local_Anonymous_Access (Element_Type);
5213 -- Propagate the parent. This field is needed if we have to generate
5214 -- the master_id associated with an anonymous access to task type
5215 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5217 Set_Parent (Element_Type, Parent (T));
5219 -- Ada 2005 (AI-230): In case of components that are anonymous access
5220 -- types the level of accessibility depends on the enclosing type
5221 -- declaration
5223 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5225 -- Ada 2005 (AI-254)
5227 declare
5228 CD : constant Node_Id :=
5229 Access_To_Subprogram_Definition
5230 (Access_Definition (Component_Def));
5231 begin
5232 if Present (CD) and then Protected_Present (CD) then
5233 Element_Type :=
5234 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5235 end if;
5236 end;
5237 end if;
5239 -- Constrained array case
5241 if No (T) then
5242 T := Create_Itype (E_Void, P, Related_Id, 'T');
5243 end if;
5245 if Nkind (Def) = N_Constrained_Array_Definition then
5247 -- Establish Implicit_Base as unconstrained base type
5249 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5251 Set_Etype (Implicit_Base, Implicit_Base);
5252 Set_Scope (Implicit_Base, Current_Scope);
5253 Set_Has_Delayed_Freeze (Implicit_Base);
5254 Set_Default_SSO (Implicit_Base);
5256 -- The constrained array type is a subtype of the unconstrained one
5258 Set_Ekind (T, E_Array_Subtype);
5259 Init_Size_Align (T);
5260 Set_Etype (T, Implicit_Base);
5261 Set_Scope (T, Current_Scope);
5262 Set_Is_Constrained (T, True);
5263 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
5264 Set_Has_Delayed_Freeze (T);
5266 -- Complete setup of implicit base type
5268 Set_First_Index (Implicit_Base, First_Index (T));
5269 Set_Component_Type (Implicit_Base, Element_Type);
5270 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5271 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5272 Set_Component_Size (Implicit_Base, Uint_0);
5273 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5274 Set_Has_Controlled_Component
5275 (Implicit_Base, Has_Controlled_Component
5276 (Element_Type)
5277 or else Is_Controlled
5278 (Element_Type));
5279 Set_Finalize_Storage_Only
5280 (Implicit_Base, Finalize_Storage_Only
5281 (Element_Type));
5283 -- Unconstrained array case
5285 else
5286 Set_Ekind (T, E_Array_Type);
5287 Init_Size_Align (T);
5288 Set_Etype (T, T);
5289 Set_Scope (T, Current_Scope);
5290 Set_Component_Size (T, Uint_0);
5291 Set_Is_Constrained (T, False);
5292 Set_First_Index (T, First (Subtype_Marks (Def)));
5293 Set_Has_Delayed_Freeze (T, True);
5294 Set_Has_Task (T, Has_Task (Element_Type));
5295 Set_Has_Protected (T, Has_Protected (Element_Type));
5296 Set_Has_Controlled_Component (T, Has_Controlled_Component
5297 (Element_Type)
5298 or else
5299 Is_Controlled (Element_Type));
5300 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5301 (Element_Type));
5302 Set_Default_SSO (T);
5303 end if;
5305 -- Common attributes for both cases
5307 Set_Component_Type (Base_Type (T), Element_Type);
5308 Set_Packed_Array_Impl_Type (T, Empty);
5310 if Aliased_Present (Component_Definition (Def)) then
5311 Check_SPARK_05_Restriction
5312 ("aliased is not allowed", Component_Definition (Def));
5313 Set_Has_Aliased_Components (Etype (T));
5314 end if;
5316 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5317 -- array type to ensure that objects of this type are initialized.
5319 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5320 Set_Can_Never_Be_Null (T);
5322 if Null_Exclusion_Present (Component_Definition (Def))
5324 -- No need to check itypes because in their case this check was
5325 -- done at their point of creation
5327 and then not Is_Itype (Element_Type)
5328 then
5329 Error_Msg_N
5330 ("`NOT NULL` not allowed (null already excluded)",
5331 Subtype_Indication (Component_Definition (Def)));
5332 end if;
5333 end if;
5335 Priv := Private_Component (Element_Type);
5337 if Present (Priv) then
5339 -- Check for circular definitions
5341 if Priv = Any_Type then
5342 Set_Component_Type (Etype (T), Any_Type);
5344 -- There is a gap in the visibility of operations on the composite
5345 -- type only if the component type is defined in a different scope.
5347 elsif Scope (Priv) = Current_Scope then
5348 null;
5350 elsif Is_Limited_Type (Priv) then
5351 Set_Is_Limited_Composite (Etype (T));
5352 Set_Is_Limited_Composite (T);
5353 else
5354 Set_Is_Private_Composite (Etype (T));
5355 Set_Is_Private_Composite (T);
5356 end if;
5357 end if;
5359 -- A syntax error in the declaration itself may lead to an empty index
5360 -- list, in which case do a minimal patch.
5362 if No (First_Index (T)) then
5363 Error_Msg_N ("missing index definition in array type declaration", T);
5365 declare
5366 Indexes : constant List_Id :=
5367 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5368 begin
5369 Set_Discrete_Subtype_Definitions (Def, Indexes);
5370 Set_First_Index (T, First (Indexes));
5371 return;
5372 end;
5373 end if;
5375 -- Create a concatenation operator for the new type. Internal array
5376 -- types created for packed entities do not need such, they are
5377 -- compatible with the user-defined type.
5379 if Number_Dimensions (T) = 1
5380 and then not Is_Packed_Array_Impl_Type (T)
5381 then
5382 New_Concatenation_Op (T);
5383 end if;
5385 -- In the case of an unconstrained array the parser has already verified
5386 -- that all the indexes are unconstrained but we still need to make sure
5387 -- that the element type is constrained.
5389 if Is_Indefinite_Subtype (Element_Type) then
5390 Error_Msg_N
5391 ("unconstrained element type in array declaration",
5392 Subtype_Indication (Component_Def));
5394 elsif Is_Abstract_Type (Element_Type) then
5395 Error_Msg_N
5396 ("the type of a component cannot be abstract",
5397 Subtype_Indication (Component_Def));
5398 end if;
5400 -- There may be an invariant declared for the component type, but
5401 -- the construction of the component invariant checking procedure
5402 -- takes place during expansion.
5403 end Array_Type_Declaration;
5405 ------------------------------------------------------
5406 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5407 ------------------------------------------------------
5409 function Replace_Anonymous_Access_To_Protected_Subprogram
5410 (N : Node_Id) return Entity_Id
5412 Loc : constant Source_Ptr := Sloc (N);
5414 Curr_Scope : constant Scope_Stack_Entry :=
5415 Scope_Stack.Table (Scope_Stack.Last);
5417 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5419 Acc : Node_Id;
5420 -- Access definition in declaration
5422 Comp : Node_Id;
5423 -- Object definition or formal definition with an access definition
5425 Decl : Node_Id;
5426 -- Declaration of anonymous access to subprogram type
5428 Spec : Node_Id;
5429 -- Original specification in access to subprogram
5431 P : Node_Id;
5433 begin
5434 Set_Is_Internal (Anon);
5436 case Nkind (N) is
5437 when N_Component_Declaration |
5438 N_Unconstrained_Array_Definition |
5439 N_Constrained_Array_Definition =>
5440 Comp := Component_Definition (N);
5441 Acc := Access_Definition (Comp);
5443 when N_Discriminant_Specification =>
5444 Comp := Discriminant_Type (N);
5445 Acc := Comp;
5447 when N_Parameter_Specification =>
5448 Comp := Parameter_Type (N);
5449 Acc := Comp;
5451 when N_Access_Function_Definition =>
5452 Comp := Result_Definition (N);
5453 Acc := Comp;
5455 when N_Object_Declaration =>
5456 Comp := Object_Definition (N);
5457 Acc := Comp;
5459 when N_Function_Specification =>
5460 Comp := Result_Definition (N);
5461 Acc := Comp;
5463 when others =>
5464 raise Program_Error;
5465 end case;
5467 Spec := Access_To_Subprogram_Definition (Acc);
5469 Decl :=
5470 Make_Full_Type_Declaration (Loc,
5471 Defining_Identifier => Anon,
5472 Type_Definition => Copy_Separate_Tree (Spec));
5474 Mark_Rewrite_Insertion (Decl);
5476 -- In ASIS mode, analyze the profile on the original node, because
5477 -- the separate copy does not provide enough links to recover the
5478 -- original tree. Analysis is limited to type annotations, within
5479 -- a temporary scope that serves as an anonymous subprogram to collect
5480 -- otherwise useless temporaries and itypes.
5482 if ASIS_Mode then
5483 declare
5484 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5486 begin
5487 if Nkind (Spec) = N_Access_Function_Definition then
5488 Set_Ekind (Typ, E_Function);
5489 else
5490 Set_Ekind (Typ, E_Procedure);
5491 end if;
5493 Set_Parent (Typ, N);
5494 Set_Scope (Typ, Current_Scope);
5495 Push_Scope (Typ);
5497 Process_Formals (Parameter_Specifications (Spec), Spec);
5499 if Nkind (Spec) = N_Access_Function_Definition then
5500 declare
5501 Def : constant Node_Id := Result_Definition (Spec);
5503 begin
5504 -- The result might itself be an anonymous access type, so
5505 -- have to recurse.
5507 if Nkind (Def) = N_Access_Definition then
5508 if Present (Access_To_Subprogram_Definition (Def)) then
5509 Set_Etype
5510 (Def,
5511 Replace_Anonymous_Access_To_Protected_Subprogram
5512 (Spec));
5513 else
5514 Find_Type (Subtype_Mark (Def));
5515 end if;
5517 else
5518 Find_Type (Def);
5519 end if;
5520 end;
5521 end if;
5523 End_Scope;
5524 end;
5525 end if;
5527 -- Insert the new declaration in the nearest enclosing scope. If the
5528 -- node is a body and N is its return type, the declaration belongs in
5529 -- the enclosing scope.
5531 P := Parent (N);
5533 if Nkind (P) = N_Subprogram_Body
5534 and then Nkind (N) = N_Function_Specification
5535 then
5536 P := Parent (P);
5537 end if;
5539 while Present (P) and then not Has_Declarations (P) loop
5540 P := Parent (P);
5541 end loop;
5543 pragma Assert (Present (P));
5545 if Nkind (P) = N_Package_Specification then
5546 Prepend (Decl, Visible_Declarations (P));
5547 else
5548 Prepend (Decl, Declarations (P));
5549 end if;
5551 -- Replace the anonymous type with an occurrence of the new declaration.
5552 -- In all cases the rewritten node does not have the null-exclusion
5553 -- attribute because (if present) it was already inherited by the
5554 -- anonymous entity (Anon). Thus, in case of components we do not
5555 -- inherit this attribute.
5557 if Nkind (N) = N_Parameter_Specification then
5558 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5559 Set_Etype (Defining_Identifier (N), Anon);
5560 Set_Null_Exclusion_Present (N, False);
5562 elsif Nkind (N) = N_Object_Declaration then
5563 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5564 Set_Etype (Defining_Identifier (N), Anon);
5566 elsif Nkind (N) = N_Access_Function_Definition then
5567 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5569 elsif Nkind (N) = N_Function_Specification then
5570 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5571 Set_Etype (Defining_Unit_Name (N), Anon);
5573 else
5574 Rewrite (Comp,
5575 Make_Component_Definition (Loc,
5576 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5577 end if;
5579 Mark_Rewrite_Insertion (Comp);
5581 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5582 Analyze (Decl);
5584 else
5585 -- Temporarily remove the current scope (record or subprogram) from
5586 -- the stack to add the new declarations to the enclosing scope.
5588 Scope_Stack.Decrement_Last;
5589 Analyze (Decl);
5590 Set_Is_Itype (Anon);
5591 Scope_Stack.Append (Curr_Scope);
5592 end if;
5594 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5595 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5596 return Anon;
5597 end Replace_Anonymous_Access_To_Protected_Subprogram;
5599 -------------------------------
5600 -- Build_Derived_Access_Type --
5601 -------------------------------
5603 procedure Build_Derived_Access_Type
5604 (N : Node_Id;
5605 Parent_Type : Entity_Id;
5606 Derived_Type : Entity_Id)
5608 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5610 Desig_Type : Entity_Id;
5611 Discr : Entity_Id;
5612 Discr_Con_Elist : Elist_Id;
5613 Discr_Con_El : Elmt_Id;
5614 Subt : Entity_Id;
5616 begin
5617 -- Set the designated type so it is available in case this is an access
5618 -- to a self-referential type, e.g. a standard list type with a next
5619 -- pointer. Will be reset after subtype is built.
5621 Set_Directly_Designated_Type
5622 (Derived_Type, Designated_Type (Parent_Type));
5624 Subt := Process_Subtype (S, N);
5626 if Nkind (S) /= N_Subtype_Indication
5627 and then Subt /= Base_Type (Subt)
5628 then
5629 Set_Ekind (Derived_Type, E_Access_Subtype);
5630 end if;
5632 if Ekind (Derived_Type) = E_Access_Subtype then
5633 declare
5634 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5635 Ibase : constant Entity_Id :=
5636 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5637 Svg_Chars : constant Name_Id := Chars (Ibase);
5638 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5640 begin
5641 Copy_Node (Pbase, Ibase);
5643 Set_Chars (Ibase, Svg_Chars);
5644 Set_Next_Entity (Ibase, Svg_Next_E);
5645 Set_Sloc (Ibase, Sloc (Derived_Type));
5646 Set_Scope (Ibase, Scope (Derived_Type));
5647 Set_Freeze_Node (Ibase, Empty);
5648 Set_Is_Frozen (Ibase, False);
5649 Set_Comes_From_Source (Ibase, False);
5650 Set_Is_First_Subtype (Ibase, False);
5652 Set_Etype (Ibase, Pbase);
5653 Set_Etype (Derived_Type, Ibase);
5654 end;
5655 end if;
5657 Set_Directly_Designated_Type
5658 (Derived_Type, Designated_Type (Subt));
5660 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5661 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5662 Set_Size_Info (Derived_Type, Parent_Type);
5663 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5664 Set_Depends_On_Private (Derived_Type,
5665 Has_Private_Component (Derived_Type));
5666 Conditional_Delay (Derived_Type, Subt);
5668 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5669 -- that it is not redundant.
5671 if Null_Exclusion_Present (Type_Definition (N)) then
5672 Set_Can_Never_Be_Null (Derived_Type);
5674 -- What is with the "AND THEN FALSE" here ???
5676 if Can_Never_Be_Null (Parent_Type)
5677 and then False
5678 then
5679 Error_Msg_NE
5680 ("`NOT NULL` not allowed (& already excludes null)",
5681 N, Parent_Type);
5682 end if;
5684 elsif Can_Never_Be_Null (Parent_Type) then
5685 Set_Can_Never_Be_Null (Derived_Type);
5686 end if;
5688 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5689 -- the root type for this information.
5691 -- Apply range checks to discriminants for derived record case
5692 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5694 Desig_Type := Designated_Type (Derived_Type);
5695 if Is_Composite_Type (Desig_Type)
5696 and then (not Is_Array_Type (Desig_Type))
5697 and then Has_Discriminants (Desig_Type)
5698 and then Base_Type (Desig_Type) /= Desig_Type
5699 then
5700 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5701 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5703 Discr := First_Discriminant (Base_Type (Desig_Type));
5704 while Present (Discr_Con_El) loop
5705 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5706 Next_Elmt (Discr_Con_El);
5707 Next_Discriminant (Discr);
5708 end loop;
5709 end if;
5710 end Build_Derived_Access_Type;
5712 ------------------------------
5713 -- Build_Derived_Array_Type --
5714 ------------------------------
5716 procedure Build_Derived_Array_Type
5717 (N : Node_Id;
5718 Parent_Type : Entity_Id;
5719 Derived_Type : Entity_Id)
5721 Loc : constant Source_Ptr := Sloc (N);
5722 Tdef : constant Node_Id := Type_Definition (N);
5723 Indic : constant Node_Id := Subtype_Indication (Tdef);
5724 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5725 Implicit_Base : Entity_Id;
5726 New_Indic : Node_Id;
5728 procedure Make_Implicit_Base;
5729 -- If the parent subtype is constrained, the derived type is a subtype
5730 -- of an implicit base type derived from the parent base.
5732 ------------------------
5733 -- Make_Implicit_Base --
5734 ------------------------
5736 procedure Make_Implicit_Base is
5737 begin
5738 Implicit_Base :=
5739 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5741 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5742 Set_Etype (Implicit_Base, Parent_Base);
5744 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5745 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5747 Set_Has_Delayed_Freeze (Implicit_Base, True);
5748 end Make_Implicit_Base;
5750 -- Start of processing for Build_Derived_Array_Type
5752 begin
5753 if not Is_Constrained (Parent_Type) then
5754 if Nkind (Indic) /= N_Subtype_Indication then
5755 Set_Ekind (Derived_Type, E_Array_Type);
5757 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5758 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
5760 Set_Has_Delayed_Freeze (Derived_Type, True);
5762 else
5763 Make_Implicit_Base;
5764 Set_Etype (Derived_Type, Implicit_Base);
5766 New_Indic :=
5767 Make_Subtype_Declaration (Loc,
5768 Defining_Identifier => Derived_Type,
5769 Subtype_Indication =>
5770 Make_Subtype_Indication (Loc,
5771 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5772 Constraint => Constraint (Indic)));
5774 Rewrite (N, New_Indic);
5775 Analyze (N);
5776 end if;
5778 else
5779 if Nkind (Indic) /= N_Subtype_Indication then
5780 Make_Implicit_Base;
5782 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5783 Set_Etype (Derived_Type, Implicit_Base);
5784 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5786 else
5787 Error_Msg_N ("illegal constraint on constrained type", Indic);
5788 end if;
5789 end if;
5791 -- If parent type is not a derived type itself, and is declared in
5792 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5793 -- the new type's concatenation operator since Derive_Subprograms
5794 -- will not inherit the parent's operator. If the parent type is
5795 -- unconstrained, the operator is of the unconstrained base type.
5797 if Number_Dimensions (Parent_Type) = 1
5798 and then not Is_Limited_Type (Parent_Type)
5799 and then not Is_Derived_Type (Parent_Type)
5800 and then not Is_Package_Or_Generic_Package
5801 (Scope (Base_Type (Parent_Type)))
5802 then
5803 if not Is_Constrained (Parent_Type)
5804 and then Is_Constrained (Derived_Type)
5805 then
5806 New_Concatenation_Op (Implicit_Base);
5807 else
5808 New_Concatenation_Op (Derived_Type);
5809 end if;
5810 end if;
5811 end Build_Derived_Array_Type;
5813 -----------------------------------
5814 -- Build_Derived_Concurrent_Type --
5815 -----------------------------------
5817 procedure Build_Derived_Concurrent_Type
5818 (N : Node_Id;
5819 Parent_Type : Entity_Id;
5820 Derived_Type : Entity_Id)
5822 Loc : constant Source_Ptr := Sloc (N);
5824 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
5825 Corr_Decl : Node_Id;
5826 Corr_Decl_Needed : Boolean;
5827 -- If the derived type has fewer discriminants than its parent, the
5828 -- corresponding record is also a derived type, in order to account for
5829 -- the bound discriminants. We create a full type declaration for it in
5830 -- this case.
5832 Constraint_Present : constant Boolean :=
5833 Nkind (Subtype_Indication (Type_Definition (N))) =
5834 N_Subtype_Indication;
5836 D_Constraint : Node_Id;
5837 New_Constraint : Elist_Id;
5838 Old_Disc : Entity_Id;
5839 New_Disc : Entity_Id;
5840 New_N : Node_Id;
5842 begin
5843 Set_Stored_Constraint (Derived_Type, No_Elist);
5844 Corr_Decl_Needed := False;
5845 Old_Disc := Empty;
5847 if Present (Discriminant_Specifications (N))
5848 and then Constraint_Present
5849 then
5850 Old_Disc := First_Discriminant (Parent_Type);
5851 New_Disc := First (Discriminant_Specifications (N));
5852 while Present (New_Disc) and then Present (Old_Disc) loop
5853 Next_Discriminant (Old_Disc);
5854 Next (New_Disc);
5855 end loop;
5856 end if;
5858 if Present (Old_Disc) and then Expander_Active then
5860 -- The new type has fewer discriminants, so we need to create a new
5861 -- corresponding record, which is derived from the corresponding
5862 -- record of the parent, and has a stored constraint that captures
5863 -- the values of the discriminant constraints. The corresponding
5864 -- record is needed only if expander is active and code generation is
5865 -- enabled.
5867 -- The type declaration for the derived corresponding record has the
5868 -- same discriminant part and constraints as the current declaration.
5869 -- Copy the unanalyzed tree to build declaration.
5871 Corr_Decl_Needed := True;
5872 New_N := Copy_Separate_Tree (N);
5874 Corr_Decl :=
5875 Make_Full_Type_Declaration (Loc,
5876 Defining_Identifier => Corr_Record,
5877 Discriminant_Specifications =>
5878 Discriminant_Specifications (New_N),
5879 Type_Definition =>
5880 Make_Derived_Type_Definition (Loc,
5881 Subtype_Indication =>
5882 Make_Subtype_Indication (Loc,
5883 Subtype_Mark =>
5884 New_Occurrence_Of
5885 (Corresponding_Record_Type (Parent_Type), Loc),
5886 Constraint =>
5887 Constraint
5888 (Subtype_Indication (Type_Definition (New_N))))));
5889 end if;
5891 -- Copy Storage_Size and Relative_Deadline variables if task case
5893 if Is_Task_Type (Parent_Type) then
5894 Set_Storage_Size_Variable (Derived_Type,
5895 Storage_Size_Variable (Parent_Type));
5896 Set_Relative_Deadline_Variable (Derived_Type,
5897 Relative_Deadline_Variable (Parent_Type));
5898 end if;
5900 if Present (Discriminant_Specifications (N)) then
5901 Push_Scope (Derived_Type);
5902 Check_Or_Process_Discriminants (N, Derived_Type);
5904 if Constraint_Present then
5905 New_Constraint :=
5906 Expand_To_Stored_Constraint
5907 (Parent_Type,
5908 Build_Discriminant_Constraints
5909 (Parent_Type,
5910 Subtype_Indication (Type_Definition (N)), True));
5911 end if;
5913 End_Scope;
5915 elsif Constraint_Present then
5917 -- Build constrained subtype, copying the constraint, and derive
5918 -- from it to create a derived constrained type.
5920 declare
5921 Loc : constant Source_Ptr := Sloc (N);
5922 Anon : constant Entity_Id :=
5923 Make_Defining_Identifier (Loc,
5924 Chars => New_External_Name (Chars (Derived_Type), 'T'));
5925 Decl : Node_Id;
5927 begin
5928 Decl :=
5929 Make_Subtype_Declaration (Loc,
5930 Defining_Identifier => Anon,
5931 Subtype_Indication =>
5932 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
5933 Insert_Before (N, Decl);
5934 Analyze (Decl);
5936 Rewrite (Subtype_Indication (Type_Definition (N)),
5937 New_Occurrence_Of (Anon, Loc));
5938 Set_Analyzed (Derived_Type, False);
5939 Analyze (N);
5940 return;
5941 end;
5942 end if;
5944 -- By default, operations and private data are inherited from parent.
5945 -- However, in the presence of bound discriminants, a new corresponding
5946 -- record will be created, see below.
5948 Set_Has_Discriminants
5949 (Derived_Type, Has_Discriminants (Parent_Type));
5950 Set_Corresponding_Record_Type
5951 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5953 -- Is_Constrained is set according the parent subtype, but is set to
5954 -- False if the derived type is declared with new discriminants.
5956 Set_Is_Constrained
5957 (Derived_Type,
5958 (Is_Constrained (Parent_Type) or else Constraint_Present)
5959 and then not Present (Discriminant_Specifications (N)));
5961 if Constraint_Present then
5962 if not Has_Discriminants (Parent_Type) then
5963 Error_Msg_N ("untagged parent must have discriminants", N);
5965 elsif Present (Discriminant_Specifications (N)) then
5967 -- Verify that new discriminants are used to constrain old ones
5969 D_Constraint :=
5970 First
5971 (Constraints
5972 (Constraint (Subtype_Indication (Type_Definition (N)))));
5974 Old_Disc := First_Discriminant (Parent_Type);
5976 while Present (D_Constraint) loop
5977 if Nkind (D_Constraint) /= N_Discriminant_Association then
5979 -- Positional constraint. If it is a reference to a new
5980 -- discriminant, it constrains the corresponding old one.
5982 if Nkind (D_Constraint) = N_Identifier then
5983 New_Disc := First_Discriminant (Derived_Type);
5984 while Present (New_Disc) loop
5985 exit when Chars (New_Disc) = Chars (D_Constraint);
5986 Next_Discriminant (New_Disc);
5987 end loop;
5989 if Present (New_Disc) then
5990 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5991 end if;
5992 end if;
5994 Next_Discriminant (Old_Disc);
5996 -- if this is a named constraint, search by name for the old
5997 -- discriminants constrained by the new one.
5999 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6001 -- Find new discriminant with that name
6003 New_Disc := First_Discriminant (Derived_Type);
6004 while Present (New_Disc) loop
6005 exit when
6006 Chars (New_Disc) = Chars (Expression (D_Constraint));
6007 Next_Discriminant (New_Disc);
6008 end loop;
6010 if Present (New_Disc) then
6012 -- Verify that new discriminant renames some discriminant
6013 -- of the parent type, and associate the new discriminant
6014 -- with one or more old ones that it renames.
6016 declare
6017 Selector : Node_Id;
6019 begin
6020 Selector := First (Selector_Names (D_Constraint));
6021 while Present (Selector) loop
6022 Old_Disc := First_Discriminant (Parent_Type);
6023 while Present (Old_Disc) loop
6024 exit when Chars (Old_Disc) = Chars (Selector);
6025 Next_Discriminant (Old_Disc);
6026 end loop;
6028 if Present (Old_Disc) then
6029 Set_Corresponding_Discriminant
6030 (New_Disc, Old_Disc);
6031 end if;
6033 Next (Selector);
6034 end loop;
6035 end;
6036 end if;
6037 end if;
6039 Next (D_Constraint);
6040 end loop;
6042 New_Disc := First_Discriminant (Derived_Type);
6043 while Present (New_Disc) loop
6044 if No (Corresponding_Discriminant (New_Disc)) then
6045 Error_Msg_NE
6046 ("new discriminant& must constrain old one", N, New_Disc);
6048 elsif not
6049 Subtypes_Statically_Compatible
6050 (Etype (New_Disc),
6051 Etype (Corresponding_Discriminant (New_Disc)))
6052 then
6053 Error_Msg_NE
6054 ("& not statically compatible with parent discriminant",
6055 N, New_Disc);
6056 end if;
6058 Next_Discriminant (New_Disc);
6059 end loop;
6060 end if;
6062 elsif Present (Discriminant_Specifications (N)) then
6063 Error_Msg_N
6064 ("missing discriminant constraint in untagged derivation", N);
6065 end if;
6067 -- The entity chain of the derived type includes the new discriminants
6068 -- but shares operations with the parent.
6070 if Present (Discriminant_Specifications (N)) then
6071 Old_Disc := First_Discriminant (Parent_Type);
6072 while Present (Old_Disc) loop
6073 if No (Next_Entity (Old_Disc))
6074 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6075 then
6076 Set_Next_Entity
6077 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6078 exit;
6079 end if;
6081 Next_Discriminant (Old_Disc);
6082 end loop;
6084 else
6085 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6086 if Has_Discriminants (Parent_Type) then
6087 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6088 Set_Discriminant_Constraint (
6089 Derived_Type, Discriminant_Constraint (Parent_Type));
6090 end if;
6091 end if;
6093 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6095 Set_Has_Completion (Derived_Type);
6097 if Corr_Decl_Needed then
6098 Set_Stored_Constraint (Derived_Type, New_Constraint);
6099 Insert_After (N, Corr_Decl);
6100 Analyze (Corr_Decl);
6101 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6102 end if;
6103 end Build_Derived_Concurrent_Type;
6105 ------------------------------------
6106 -- Build_Derived_Enumeration_Type --
6107 ------------------------------------
6109 procedure Build_Derived_Enumeration_Type
6110 (N : Node_Id;
6111 Parent_Type : Entity_Id;
6112 Derived_Type : Entity_Id)
6114 Loc : constant Source_Ptr := Sloc (N);
6115 Def : constant Node_Id := Type_Definition (N);
6116 Indic : constant Node_Id := Subtype_Indication (Def);
6117 Implicit_Base : Entity_Id;
6118 Literal : Entity_Id;
6119 New_Lit : Entity_Id;
6120 Literals_List : List_Id;
6121 Type_Decl : Node_Id;
6122 Hi, Lo : Node_Id;
6123 Rang_Expr : Node_Id;
6125 begin
6126 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6127 -- not have explicit literals lists we need to process types derived
6128 -- from them specially. This is handled by Derived_Standard_Character.
6129 -- If the parent type is a generic type, there are no literals either,
6130 -- and we construct the same skeletal representation as for the generic
6131 -- parent type.
6133 if Is_Standard_Character_Type (Parent_Type) then
6134 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6136 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6137 declare
6138 Lo : Node_Id;
6139 Hi : Node_Id;
6141 begin
6142 if Nkind (Indic) /= N_Subtype_Indication then
6143 Lo :=
6144 Make_Attribute_Reference (Loc,
6145 Attribute_Name => Name_First,
6146 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6147 Set_Etype (Lo, Derived_Type);
6149 Hi :=
6150 Make_Attribute_Reference (Loc,
6151 Attribute_Name => Name_Last,
6152 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6153 Set_Etype (Hi, Derived_Type);
6155 Set_Scalar_Range (Derived_Type,
6156 Make_Range (Loc,
6157 Low_Bound => Lo,
6158 High_Bound => Hi));
6159 else
6161 -- Analyze subtype indication and verify compatibility
6162 -- with parent type.
6164 if Base_Type (Process_Subtype (Indic, N)) /=
6165 Base_Type (Parent_Type)
6166 then
6167 Error_Msg_N
6168 ("illegal constraint for formal discrete type", N);
6169 end if;
6170 end if;
6171 end;
6173 else
6174 -- If a constraint is present, analyze the bounds to catch
6175 -- premature usage of the derived literals.
6177 if Nkind (Indic) = N_Subtype_Indication
6178 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6179 then
6180 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6181 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6182 end if;
6184 -- Introduce an implicit base type for the derived type even if there
6185 -- is no constraint attached to it, since this seems closer to the
6186 -- Ada semantics. Build a full type declaration tree for the derived
6187 -- type using the implicit base type as the defining identifier. The
6188 -- build a subtype declaration tree which applies the constraint (if
6189 -- any) have it replace the derived type declaration.
6191 Literal := First_Literal (Parent_Type);
6192 Literals_List := New_List;
6193 while Present (Literal)
6194 and then Ekind (Literal) = E_Enumeration_Literal
6195 loop
6196 -- Literals of the derived type have the same representation as
6197 -- those of the parent type, but this representation can be
6198 -- overridden by an explicit representation clause. Indicate
6199 -- that there is no explicit representation given yet. These
6200 -- derived literals are implicit operations of the new type,
6201 -- and can be overridden by explicit ones.
6203 if Nkind (Literal) = N_Defining_Character_Literal then
6204 New_Lit :=
6205 Make_Defining_Character_Literal (Loc, Chars (Literal));
6206 else
6207 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6208 end if;
6210 Set_Ekind (New_Lit, E_Enumeration_Literal);
6211 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6212 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6213 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6214 Set_Alias (New_Lit, Literal);
6215 Set_Is_Known_Valid (New_Lit, True);
6217 Append (New_Lit, Literals_List);
6218 Next_Literal (Literal);
6219 end loop;
6221 Implicit_Base :=
6222 Make_Defining_Identifier (Sloc (Derived_Type),
6223 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6225 -- Indicate the proper nature of the derived type. This must be done
6226 -- before analysis of the literals, to recognize cases when a literal
6227 -- may be hidden by a previous explicit function definition (cf.
6228 -- c83031a).
6230 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6231 Set_Etype (Derived_Type, Implicit_Base);
6233 Type_Decl :=
6234 Make_Full_Type_Declaration (Loc,
6235 Defining_Identifier => Implicit_Base,
6236 Discriminant_Specifications => No_List,
6237 Type_Definition =>
6238 Make_Enumeration_Type_Definition (Loc, Literals_List));
6240 Mark_Rewrite_Insertion (Type_Decl);
6241 Insert_Before (N, Type_Decl);
6242 Analyze (Type_Decl);
6244 -- After the implicit base is analyzed its Etype needs to be changed
6245 -- to reflect the fact that it is derived from the parent type which
6246 -- was ignored during analysis. We also set the size at this point.
6248 Set_Etype (Implicit_Base, Parent_Type);
6250 Set_Size_Info (Implicit_Base, Parent_Type);
6251 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6252 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6254 -- Copy other flags from parent type
6256 Set_Has_Non_Standard_Rep
6257 (Implicit_Base, Has_Non_Standard_Rep
6258 (Parent_Type));
6259 Set_Has_Pragma_Ordered
6260 (Implicit_Base, Has_Pragma_Ordered
6261 (Parent_Type));
6262 Set_Has_Delayed_Freeze (Implicit_Base);
6264 -- Process the subtype indication including a validation check on the
6265 -- constraint, if any. If a constraint is given, its bounds must be
6266 -- implicitly converted to the new type.
6268 if Nkind (Indic) = N_Subtype_Indication then
6269 declare
6270 R : constant Node_Id :=
6271 Range_Expression (Constraint (Indic));
6273 begin
6274 if Nkind (R) = N_Range then
6275 Hi := Build_Scalar_Bound
6276 (High_Bound (R), Parent_Type, Implicit_Base);
6277 Lo := Build_Scalar_Bound
6278 (Low_Bound (R), Parent_Type, Implicit_Base);
6280 else
6281 -- Constraint is a Range attribute. Replace with explicit
6282 -- mention of the bounds of the prefix, which must be a
6283 -- subtype.
6285 Analyze (Prefix (R));
6286 Hi :=
6287 Convert_To (Implicit_Base,
6288 Make_Attribute_Reference (Loc,
6289 Attribute_Name => Name_Last,
6290 Prefix =>
6291 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6293 Lo :=
6294 Convert_To (Implicit_Base,
6295 Make_Attribute_Reference (Loc,
6296 Attribute_Name => Name_First,
6297 Prefix =>
6298 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6299 end if;
6300 end;
6302 else
6303 Hi :=
6304 Build_Scalar_Bound
6305 (Type_High_Bound (Parent_Type),
6306 Parent_Type, Implicit_Base);
6307 Lo :=
6308 Build_Scalar_Bound
6309 (Type_Low_Bound (Parent_Type),
6310 Parent_Type, Implicit_Base);
6311 end if;
6313 Rang_Expr :=
6314 Make_Range (Loc,
6315 Low_Bound => Lo,
6316 High_Bound => Hi);
6318 -- If we constructed a default range for the case where no range
6319 -- was given, then the expressions in the range must not freeze
6320 -- since they do not correspond to expressions in the source.
6322 if Nkind (Indic) /= N_Subtype_Indication then
6323 Set_Must_Not_Freeze (Lo);
6324 Set_Must_Not_Freeze (Hi);
6325 Set_Must_Not_Freeze (Rang_Expr);
6326 end if;
6328 Rewrite (N,
6329 Make_Subtype_Declaration (Loc,
6330 Defining_Identifier => Derived_Type,
6331 Subtype_Indication =>
6332 Make_Subtype_Indication (Loc,
6333 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6334 Constraint =>
6335 Make_Range_Constraint (Loc,
6336 Range_Expression => Rang_Expr))));
6338 Analyze (N);
6340 -- Apply a range check. Since this range expression doesn't have an
6341 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6342 -- this right???
6344 if Nkind (Indic) = N_Subtype_Indication then
6345 Apply_Range_Check (Range_Expression (Constraint (Indic)),
6346 Parent_Type,
6347 Source_Typ => Entity (Subtype_Mark (Indic)));
6348 end if;
6349 end if;
6350 end Build_Derived_Enumeration_Type;
6352 --------------------------------
6353 -- Build_Derived_Numeric_Type --
6354 --------------------------------
6356 procedure Build_Derived_Numeric_Type
6357 (N : Node_Id;
6358 Parent_Type : Entity_Id;
6359 Derived_Type : Entity_Id)
6361 Loc : constant Source_Ptr := Sloc (N);
6362 Tdef : constant Node_Id := Type_Definition (N);
6363 Indic : constant Node_Id := Subtype_Indication (Tdef);
6364 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6365 No_Constraint : constant Boolean := Nkind (Indic) /=
6366 N_Subtype_Indication;
6367 Implicit_Base : Entity_Id;
6369 Lo : Node_Id;
6370 Hi : Node_Id;
6372 begin
6373 -- Process the subtype indication including a validation check on
6374 -- the constraint if any.
6376 Discard_Node (Process_Subtype (Indic, N));
6378 -- Introduce an implicit base type for the derived type even if there
6379 -- is no constraint attached to it, since this seems closer to the Ada
6380 -- semantics.
6382 Implicit_Base :=
6383 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6385 Set_Etype (Implicit_Base, Parent_Base);
6386 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6387 Set_Size_Info (Implicit_Base, Parent_Base);
6388 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6389 Set_Parent (Implicit_Base, Parent (Derived_Type));
6390 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6392 -- Set RM Size for discrete type or decimal fixed-point type
6393 -- Ordinary fixed-point is excluded, why???
6395 if Is_Discrete_Type (Parent_Base)
6396 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6397 then
6398 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6399 end if;
6401 Set_Has_Delayed_Freeze (Implicit_Base);
6403 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6404 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6406 Set_Scalar_Range (Implicit_Base,
6407 Make_Range (Loc,
6408 Low_Bound => Lo,
6409 High_Bound => Hi));
6411 if Has_Infinities (Parent_Base) then
6412 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6413 end if;
6415 -- The Derived_Type, which is the entity of the declaration, is a
6416 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6417 -- absence of an explicit constraint.
6419 Set_Etype (Derived_Type, Implicit_Base);
6421 -- If we did not have a constraint, then the Ekind is set from the
6422 -- parent type (otherwise Process_Subtype has set the bounds)
6424 if No_Constraint then
6425 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6426 end if;
6428 -- If we did not have a range constraint, then set the range from the
6429 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6431 if No_Constraint
6432 or else not Has_Range_Constraint (Indic)
6433 then
6434 Set_Scalar_Range (Derived_Type,
6435 Make_Range (Loc,
6436 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6437 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6438 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6440 if Has_Infinities (Parent_Type) then
6441 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6442 end if;
6444 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6445 end if;
6447 Set_Is_Descendent_Of_Address (Derived_Type,
6448 Is_Descendent_Of_Address (Parent_Type));
6449 Set_Is_Descendent_Of_Address (Implicit_Base,
6450 Is_Descendent_Of_Address (Parent_Type));
6452 -- Set remaining type-specific fields, depending on numeric type
6454 if Is_Modular_Integer_Type (Parent_Type) then
6455 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6457 Set_Non_Binary_Modulus
6458 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6460 Set_Is_Known_Valid
6461 (Implicit_Base, Is_Known_Valid (Parent_Base));
6463 elsif Is_Floating_Point_Type (Parent_Type) then
6465 -- Digits of base type is always copied from the digits value of
6466 -- the parent base type, but the digits of the derived type will
6467 -- already have been set if there was a constraint present.
6469 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6470 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6472 if No_Constraint then
6473 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6474 end if;
6476 elsif Is_Fixed_Point_Type (Parent_Type) then
6478 -- Small of base type and derived type are always copied from the
6479 -- parent base type, since smalls never change. The delta of the
6480 -- base type is also copied from the parent base type. However the
6481 -- delta of the derived type will have been set already if a
6482 -- constraint was present.
6484 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6485 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6486 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6488 if No_Constraint then
6489 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6490 end if;
6492 -- The scale and machine radix in the decimal case are always
6493 -- copied from the parent base type.
6495 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6496 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6497 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6499 Set_Machine_Radix_10
6500 (Derived_Type, Machine_Radix_10 (Parent_Base));
6501 Set_Machine_Radix_10
6502 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6504 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6506 if No_Constraint then
6507 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6509 else
6510 -- the analysis of the subtype_indication sets the
6511 -- digits value of the derived type.
6513 null;
6514 end if;
6515 end if;
6516 end if;
6518 if Is_Integer_Type (Parent_Type) then
6519 Set_Has_Shift_Operator
6520 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6521 end if;
6523 -- The type of the bounds is that of the parent type, and they
6524 -- must be converted to the derived type.
6526 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6528 -- The implicit_base should be frozen when the derived type is frozen,
6529 -- but note that it is used in the conversions of the bounds. For fixed
6530 -- types we delay the determination of the bounds until the proper
6531 -- freezing point. For other numeric types this is rejected by GCC, for
6532 -- reasons that are currently unclear (???), so we choose to freeze the
6533 -- implicit base now. In the case of integers and floating point types
6534 -- this is harmless because subsequent representation clauses cannot
6535 -- affect anything, but it is still baffling that we cannot use the
6536 -- same mechanism for all derived numeric types.
6538 -- There is a further complication: actually some representation
6539 -- clauses can affect the implicit base type. For example, attribute
6540 -- definition clauses for stream-oriented attributes need to set the
6541 -- corresponding TSS entries on the base type, and this normally
6542 -- cannot be done after the base type is frozen, so the circuitry in
6543 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6544 -- and not use Set_TSS in this case.
6546 -- There are also consequences for the case of delayed representation
6547 -- aspects for some cases. For example, a Size aspect is delayed and
6548 -- should not be evaluated to the freeze point. This early freezing
6549 -- means that the size attribute evaluation happens too early???
6551 if Is_Fixed_Point_Type (Parent_Type) then
6552 Conditional_Delay (Implicit_Base, Parent_Type);
6553 else
6554 Freeze_Before (N, Implicit_Base);
6555 end if;
6556 end Build_Derived_Numeric_Type;
6558 --------------------------------
6559 -- Build_Derived_Private_Type --
6560 --------------------------------
6562 procedure Build_Derived_Private_Type
6563 (N : Node_Id;
6564 Parent_Type : Entity_Id;
6565 Derived_Type : Entity_Id;
6566 Is_Completion : Boolean;
6567 Derive_Subps : Boolean := True)
6569 Loc : constant Source_Ptr := Sloc (N);
6570 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6571 Par_Scope : constant Entity_Id := Scope (Par_Base);
6572 Der_Base : Entity_Id;
6573 Discr : Entity_Id;
6574 Full_Der : Entity_Id;
6575 Full_P : Entity_Id;
6576 Last_Discr : Entity_Id;
6578 procedure Build_Full_Derivation;
6579 -- Build full derivation, i.e. derive from the full view
6581 procedure Copy_And_Build;
6582 -- Copy derived type declaration, replace parent with its full view,
6583 -- and build derivation
6585 ---------------------------
6586 -- Build_Full_Derivation --
6587 ---------------------------
6589 procedure Build_Full_Derivation is
6590 begin
6591 -- If parent scope is not open, install the declarations
6593 if not In_Open_Scopes (Par_Scope) then
6594 Install_Private_Declarations (Par_Scope);
6595 Install_Visible_Declarations (Par_Scope);
6596 Copy_And_Build;
6597 Uninstall_Declarations (Par_Scope);
6599 -- If parent scope is open and in another unit, and parent has a
6600 -- completion, then the derivation is taking place in the visible
6601 -- part of a child unit. In that case retrieve the full view of
6602 -- the parent momentarily.
6604 elsif not In_Same_Source_Unit (N, Parent_Type) then
6605 Full_P := Full_View (Parent_Type);
6606 Exchange_Declarations (Parent_Type);
6607 Copy_And_Build;
6608 Exchange_Declarations (Full_P);
6610 -- Otherwise it is a local derivation
6612 else
6613 Copy_And_Build;
6614 end if;
6615 end Build_Full_Derivation;
6617 --------------------
6618 -- Copy_And_Build --
6619 --------------------
6621 procedure Copy_And_Build is
6622 Full_N : Node_Id;
6623 Full_Parent : Entity_Id := Parent_Type;
6625 begin
6626 -- If the parent is itself derived from another private type,
6627 -- installing the private declarations has not affected its
6628 -- privacy status, so use its own full view explicitly.
6630 if Is_Private_Type (Full_Parent)
6631 and then Present (Full_View (Full_Parent))
6632 then
6633 Full_Parent := Full_View (Full_Parent);
6634 end if;
6636 -- And its underlying full view if necessary
6638 if Is_Private_Type (Full_Parent)
6639 and then Present (Underlying_Full_View (Full_Parent))
6640 then
6641 Full_Parent := Underlying_Full_View (Full_Parent);
6642 end if;
6644 -- For record, access and most enumeration types, derivation from
6645 -- the full view requires a fully-fledged declaration. In the other
6646 -- cases, just use an itype.
6648 if Ekind (Full_Parent) in Record_Kind
6649 or else Ekind (Full_Parent) in Access_Kind
6650 or else
6651 (Ekind (Full_Parent) in Enumeration_Kind
6652 and then not Is_Standard_Character_Type (Full_Parent)
6653 and then not Is_Generic_Type (Root_Type (Full_Parent)))
6654 then
6655 -- Copy and adjust declaration to provide a completion for what
6656 -- is originally a private declaration. Indicate that full view
6657 -- is internally generated.
6659 Full_N := New_Copy_Tree (N);
6660 Full_Der := New_Copy (Derived_Type);
6661 Set_Comes_From_Source (Full_N, False);
6662 Set_Comes_From_Source (Full_Der, False);
6663 Set_Parent (Full_Der, Full_N);
6664 Set_Defining_Identifier (Full_N, Full_Der);
6666 -- If there are no constraints, adjust the subtype mark
6668 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
6669 N_Subtype_Indication
6670 then
6671 Set_Subtype_Indication
6672 (Type_Definition (Full_N),
6673 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
6674 end if;
6676 Insert_After (N, Full_N);
6678 -- Build full view of derived type from full view of parent which
6679 -- is now installed. Subprograms have been derived on the partial
6680 -- view, the completion does not derive them anew.
6682 if Ekind (Full_Parent) in Record_Kind then
6684 -- If parent type is tagged, the completion inherits the proper
6685 -- primitive operations.
6687 if Is_Tagged_Type (Parent_Type) then
6688 Build_Derived_Record_Type
6689 (Full_N, Full_Parent, Full_Der, Derive_Subps);
6690 else
6691 Build_Derived_Record_Type
6692 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
6693 end if;
6695 else
6696 Build_Derived_Type
6697 (Full_N, Full_Parent, Full_Der, True, Derive_Subps => False);
6698 end if;
6700 -- The full declaration has been introduced into the tree and
6701 -- processed in the step above. It should not be analyzed again
6702 -- (when encountered later in the current list of declarations)
6703 -- to prevent spurious name conflicts. The full entity remains
6704 -- invisible.
6706 Set_Analyzed (Full_N);
6708 else
6709 Full_Der :=
6710 Make_Defining_Identifier (Sloc (Derived_Type),
6711 Chars => Chars (Derived_Type));
6712 Set_Is_Itype (Full_Der);
6713 Set_Associated_Node_For_Itype (Full_Der, N);
6714 Set_Parent (Full_Der, N);
6715 Build_Derived_Type
6716 (N, Full_Parent, Full_Der, True, Derive_Subps => False);
6717 end if;
6719 Set_Has_Private_Declaration (Full_Der);
6720 Set_Has_Private_Declaration (Derived_Type);
6722 Set_Scope (Full_Der, Scope (Derived_Type));
6723 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
6724 Set_Has_Size_Clause (Full_Der, False);
6725 Set_Has_Alignment_Clause (Full_Der, False);
6726 Set_Has_Delayed_Freeze (Full_Der);
6727 Set_Is_Frozen (Full_Der, False);
6728 Set_Freeze_Node (Full_Der, Empty);
6729 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
6730 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6732 -- The convention on the base type may be set in the private part
6733 -- and not propagated to the subtype until later, so we obtain the
6734 -- convention from the base type of the parent.
6736 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
6737 end Copy_And_Build;
6739 -- Start of processing for Build_Derived_Private_Type
6741 begin
6742 if Is_Tagged_Type (Parent_Type) then
6743 Full_P := Full_View (Parent_Type);
6745 -- A type extension of a type with unknown discriminants is an
6746 -- indefinite type that the back-end cannot handle directly.
6747 -- We treat it as a private type, and build a completion that is
6748 -- derived from the full view of the parent, and hopefully has
6749 -- known discriminants.
6751 -- If the full view of the parent type has an underlying record view,
6752 -- use it to generate the underlying record view of this derived type
6753 -- (required for chains of derivations with unknown discriminants).
6755 -- Minor optimization: we avoid the generation of useless underlying
6756 -- record view entities if the private type declaration has unknown
6757 -- discriminants but its corresponding full view has no
6758 -- discriminants.
6760 if Has_Unknown_Discriminants (Parent_Type)
6761 and then Present (Full_P)
6762 and then (Has_Discriminants (Full_P)
6763 or else Present (Underlying_Record_View (Full_P)))
6764 and then not In_Open_Scopes (Par_Scope)
6765 and then Expander_Active
6766 then
6767 declare
6768 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
6769 New_Ext : constant Node_Id :=
6770 Copy_Separate_Tree
6771 (Record_Extension_Part (Type_Definition (N)));
6772 Decl : Node_Id;
6774 begin
6775 Build_Derived_Record_Type
6776 (N, Parent_Type, Derived_Type, Derive_Subps);
6778 -- Build anonymous completion, as a derivation from the full
6779 -- view of the parent. This is not a completion in the usual
6780 -- sense, because the current type is not private.
6782 Decl :=
6783 Make_Full_Type_Declaration (Loc,
6784 Defining_Identifier => Full_Der,
6785 Type_Definition =>
6786 Make_Derived_Type_Definition (Loc,
6787 Subtype_Indication =>
6788 New_Copy_Tree
6789 (Subtype_Indication (Type_Definition (N))),
6790 Record_Extension_Part => New_Ext));
6792 -- If the parent type has an underlying record view, use it
6793 -- here to build the new underlying record view.
6795 if Present (Underlying_Record_View (Full_P)) then
6796 pragma Assert
6797 (Nkind (Subtype_Indication (Type_Definition (Decl)))
6798 = N_Identifier);
6799 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
6800 Underlying_Record_View (Full_P));
6801 end if;
6803 Install_Private_Declarations (Par_Scope);
6804 Install_Visible_Declarations (Par_Scope);
6805 Insert_Before (N, Decl);
6807 -- Mark entity as an underlying record view before analysis,
6808 -- to avoid generating the list of its primitive operations
6809 -- (which is not really required for this entity) and thus
6810 -- prevent spurious errors associated with missing overriding
6811 -- of abstract primitives (overridden only for Derived_Type).
6813 Set_Ekind (Full_Der, E_Record_Type);
6814 Set_Is_Underlying_Record_View (Full_Der);
6815 Set_Default_SSO (Full_Der);
6817 Analyze (Decl);
6819 pragma Assert (Has_Discriminants (Full_Der)
6820 and then not Has_Unknown_Discriminants (Full_Der));
6822 Uninstall_Declarations (Par_Scope);
6824 -- Freeze the underlying record view, to prevent generation of
6825 -- useless dispatching information, which is simply shared with
6826 -- the real derived type.
6828 Set_Is_Frozen (Full_Der);
6830 -- Set up links between real entity and underlying record view
6832 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
6833 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
6834 end;
6836 -- If discriminants are known, build derived record
6838 else
6839 Build_Derived_Record_Type
6840 (N, Parent_Type, Derived_Type, Derive_Subps);
6841 end if;
6843 return;
6845 elsif Has_Discriminants (Parent_Type) then
6846 if Present (Full_View (Parent_Type)) then
6847 if not Is_Completion then
6848 -- If this is not a completion, construct the implicit full
6849 -- view by deriving from the full view of the parent type.
6851 Build_Full_Derivation;
6853 else
6854 -- If this is a completion, the full view being built is itself
6855 -- private. We build a subtype of the parent with the same
6856 -- constraints as this full view, to convey to the back end the
6857 -- constrained components and the size of this subtype. If the
6858 -- parent is constrained, its full view can serve as the
6859 -- underlying full view of the derived type.
6861 if No (Discriminant_Specifications (N)) then
6862 if Nkind (Subtype_Indication (Type_Definition (N))) =
6863 N_Subtype_Indication
6864 then
6865 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
6867 elsif Is_Constrained (Full_View (Parent_Type)) then
6868 Set_Underlying_Full_View
6869 (Derived_Type, Full_View (Parent_Type));
6870 end if;
6872 else
6873 -- If there are new discriminants, the parent subtype is
6874 -- constrained by them, but it is not clear how to build
6875 -- the Underlying_Full_View in this case???
6877 null;
6878 end if;
6879 end if;
6880 end if;
6882 -- Build partial view of derived type from partial view of parent
6884 Build_Derived_Record_Type
6885 (N, Parent_Type, Derived_Type, Derive_Subps);
6887 if Present (Full_View (Parent_Type)) and then not Is_Completion then
6888 -- Install full view in derived type (base type and subtype)
6890 Der_Base := Base_Type (Derived_Type);
6891 Set_Full_View (Derived_Type, Full_Der);
6892 Set_Full_View (Der_Base, Base_Type (Full_Der));
6894 -- Copy the discriminant list from full view to the partial views
6895 -- (base type and its subtype). Gigi requires that the partial and
6896 -- full views have the same discriminants.
6898 -- Note that since the partial view is pointing to discriminants
6899 -- in the full view, their scope will be that of the full view.
6900 -- This might cause some front end problems and need adjustment???
6902 Discr := First_Discriminant (Base_Type (Full_Der));
6903 Set_First_Entity (Der_Base, Discr);
6905 loop
6906 Last_Discr := Discr;
6907 Next_Discriminant (Discr);
6908 exit when No (Discr);
6909 end loop;
6911 Set_Last_Entity (Der_Base, Last_Discr);
6913 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6914 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
6915 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
6916 end if;
6918 elsif Present (Full_View (Parent_Type))
6919 and then Has_Discriminants (Full_View (Parent_Type))
6920 then
6921 if Has_Unknown_Discriminants (Parent_Type)
6922 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6923 N_Subtype_Indication
6924 then
6925 Error_Msg_N
6926 ("cannot constrain type with unknown discriminants",
6927 Subtype_Indication (Type_Definition (N)));
6928 return;
6929 end if;
6931 -- If this is not a completion, construct the implicit full view by
6932 -- deriving from the full view of the parent type. But if this is a
6933 -- completion, the derived private type being built is a full view
6934 -- and the full derivation can only be its underlying full view.
6936 Build_Full_Derivation;
6938 if not Is_Completion then
6939 Set_Full_View (Derived_Type, Full_Der);
6940 else
6941 Set_Underlying_Full_View (Derived_Type, Full_Der);
6942 end if;
6944 -- In any case, the primitive operations are inherited from the
6945 -- parent type, not from the internal full view.
6947 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6949 if Derive_Subps then
6950 Derive_Subprograms (Parent_Type, Derived_Type);
6951 end if;
6953 Set_Stored_Constraint (Derived_Type, No_Elist);
6954 Set_Is_Constrained
6955 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
6957 else
6958 -- Untagged type, No discriminants on either view
6960 if Nkind (Subtype_Indication (Type_Definition (N))) =
6961 N_Subtype_Indication
6962 then
6963 Error_Msg_N
6964 ("illegal constraint on type without discriminants", N);
6965 end if;
6967 if Present (Discriminant_Specifications (N))
6968 and then Present (Full_View (Parent_Type))
6969 and then not Is_Tagged_Type (Full_View (Parent_Type))
6970 then
6971 Error_Msg_N ("cannot add discriminants to untagged type", N);
6972 end if;
6974 Set_Stored_Constraint (Derived_Type, No_Elist);
6975 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6976 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6977 Set_Has_Controlled_Component
6978 (Derived_Type, Has_Controlled_Component
6979 (Parent_Type));
6981 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6983 if not Is_Controlled (Parent_Type) then
6984 Set_Finalize_Storage_Only
6985 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6986 end if;
6988 -- If this is not a completion, construct the implicit full view by
6989 -- deriving from the full view of the parent type.
6991 -- ??? If the parent is untagged private and its completion is
6992 -- tagged, this mechanism will not work because we cannot derive from
6993 -- the tagged full view unless we have an extension.
6995 if Present (Full_View (Parent_Type))
6996 and then not Is_Tagged_Type (Full_View (Parent_Type))
6997 and then not Is_Completion
6998 then
6999 Build_Full_Derivation;
7000 Set_Full_View (Derived_Type, Full_Der);
7001 end if;
7002 end if;
7004 Set_Has_Unknown_Discriminants (Derived_Type,
7005 Has_Unknown_Discriminants (Parent_Type));
7007 if Is_Private_Type (Derived_Type) then
7008 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7009 end if;
7011 -- If the parent base type is in scope, add the derived type to its
7012 -- list of private dependents, because its full view may become
7013 -- visible subsequently (in a nested private part, a body, or in a
7014 -- further child unit).
7016 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7017 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7019 -- Check for unusual case where a type completed by a private
7020 -- derivation occurs within a package nested in a child unit, and
7021 -- the parent is declared in an ancestor.
7023 if Is_Child_Unit (Scope (Current_Scope))
7024 and then Is_Completion
7025 and then In_Private_Part (Current_Scope)
7026 and then Scope (Parent_Type) /= Current_Scope
7028 -- Note that if the parent has a completion in the private part,
7029 -- (which is itself a derivation from some other private type)
7030 -- it is that completion that is visible, there is no full view
7031 -- available, and no special processing is needed.
7033 and then Present (Full_View (Parent_Type))
7034 then
7035 -- In this case, the full view of the parent type will become
7036 -- visible in the body of the enclosing child, and only then will
7037 -- the current type be possibly non-private. Build an underlying
7038 -- full view that will be installed when the enclosing child body
7039 -- is compiled.
7041 if Present (Underlying_Full_View (Derived_Type)) then
7042 Full_Der := Underlying_Full_View (Derived_Type);
7043 else
7044 Build_Full_Derivation;
7045 Set_Underlying_Full_View (Derived_Type, Full_Der);
7046 end if;
7048 -- The full view will be used to swap entities on entry/exit to
7049 -- the body, and must appear in the entity list for the package.
7051 Append_Entity (Full_Der, Scope (Derived_Type));
7052 end if;
7053 end if;
7054 end Build_Derived_Private_Type;
7056 -------------------------------
7057 -- Build_Derived_Record_Type --
7058 -------------------------------
7060 -- 1. INTRODUCTION
7062 -- Ideally we would like to use the same model of type derivation for
7063 -- tagged and untagged record types. Unfortunately this is not quite
7064 -- possible because the semantics of representation clauses is different
7065 -- for tagged and untagged records under inheritance. Consider the
7066 -- following:
7068 -- type R (...) is [tagged] record ... end record;
7069 -- type T (...) is new R (...) [with ...];
7071 -- The representation clauses for T can specify a completely different
7072 -- record layout from R's. Hence the same component can be placed in two
7073 -- very different positions in objects of type T and R. If R and T are
7074 -- tagged types, representation clauses for T can only specify the layout
7075 -- of non inherited components, thus components that are common in R and T
7076 -- have the same position in objects of type R and T.
7078 -- This has two implications. The first is that the entire tree for R's
7079 -- declaration needs to be copied for T in the untagged case, so that T
7080 -- can be viewed as a record type of its own with its own representation
7081 -- clauses. The second implication is the way we handle discriminants.
7082 -- Specifically, in the untagged case we need a way to communicate to Gigi
7083 -- what are the real discriminants in the record, while for the semantics
7084 -- we need to consider those introduced by the user to rename the
7085 -- discriminants in the parent type. This is handled by introducing the
7086 -- notion of stored discriminants. See below for more.
7088 -- Fortunately the way regular components are inherited can be handled in
7089 -- the same way in tagged and untagged types.
7091 -- To complicate things a bit more the private view of a private extension
7092 -- cannot be handled in the same way as the full view (for one thing the
7093 -- semantic rules are somewhat different). We will explain what differs
7094 -- below.
7096 -- 2. DISCRIMINANTS UNDER INHERITANCE
7098 -- The semantic rules governing the discriminants of derived types are
7099 -- quite subtle.
7101 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7102 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7104 -- If parent type has discriminants, then the discriminants that are
7105 -- declared in the derived type are [3.4 (11)]:
7107 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7108 -- there is one;
7110 -- o Otherwise, each discriminant of the parent type (implicitly declared
7111 -- in the same order with the same specifications). In this case, the
7112 -- discriminants are said to be "inherited", or if unknown in the parent
7113 -- are also unknown in the derived type.
7115 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7117 -- o The parent subtype must be constrained;
7119 -- o If the parent type is not a tagged type, then each discriminant of
7120 -- the derived type must be used in the constraint defining a parent
7121 -- subtype. [Implementation note: This ensures that the new discriminant
7122 -- can share storage with an existing discriminant.]
7124 -- For the derived type each discriminant of the parent type is either
7125 -- inherited, constrained to equal some new discriminant of the derived
7126 -- type, or constrained to the value of an expression.
7128 -- When inherited or constrained to equal some new discriminant, the
7129 -- parent discriminant and the discriminant of the derived type are said
7130 -- to "correspond".
7132 -- If a discriminant of the parent type is constrained to a specific value
7133 -- in the derived type definition, then the discriminant is said to be
7134 -- "specified" by that derived type definition.
7136 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7138 -- We have spoken about stored discriminants in point 1 (introduction)
7139 -- above. There are two sort of stored discriminants: implicit and
7140 -- explicit. As long as the derived type inherits the same discriminants as
7141 -- the root record type, stored discriminants are the same as regular
7142 -- discriminants, and are said to be implicit. However, if any discriminant
7143 -- in the root type was renamed in the derived type, then the derived
7144 -- type will contain explicit stored discriminants. Explicit stored
7145 -- discriminants are discriminants in addition to the semantically visible
7146 -- discriminants defined for the derived type. Stored discriminants are
7147 -- used by Gigi to figure out what are the physical discriminants in
7148 -- objects of the derived type (see precise definition in einfo.ads).
7149 -- As an example, consider the following:
7151 -- type R (D1, D2, D3 : Int) is record ... end record;
7152 -- type T1 is new R;
7153 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7154 -- type T3 is new T2;
7155 -- type T4 (Y : Int) is new T3 (Y, 99);
7157 -- The following table summarizes the discriminants and stored
7158 -- discriminants in R and T1 through T4.
7160 -- Type Discrim Stored Discrim Comment
7161 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7162 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7163 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7164 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7165 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7167 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7168 -- find the corresponding discriminant in the parent type, while
7169 -- Original_Record_Component (abbreviated ORC below), the actual physical
7170 -- component that is renamed. Finally the field Is_Completely_Hidden
7171 -- (abbreviated ICH below) is set for all explicit stored discriminants
7172 -- (see einfo.ads for more info). For the above example this gives:
7174 -- Discrim CD ORC ICH
7175 -- ^^^^^^^ ^^ ^^^ ^^^
7176 -- D1 in R empty itself no
7177 -- D2 in R empty itself no
7178 -- D3 in R empty itself no
7180 -- D1 in T1 D1 in R itself no
7181 -- D2 in T1 D2 in R itself no
7182 -- D3 in T1 D3 in R itself no
7184 -- X1 in T2 D3 in T1 D3 in T2 no
7185 -- X2 in T2 D1 in T1 D1 in T2 no
7186 -- D1 in T2 empty itself yes
7187 -- D2 in T2 empty itself yes
7188 -- D3 in T2 empty itself yes
7190 -- X1 in T3 X1 in T2 D3 in T3 no
7191 -- X2 in T3 X2 in T2 D1 in T3 no
7192 -- D1 in T3 empty itself yes
7193 -- D2 in T3 empty itself yes
7194 -- D3 in T3 empty itself yes
7196 -- Y in T4 X1 in T3 D3 in T3 no
7197 -- D1 in T3 empty itself yes
7198 -- D2 in T3 empty itself yes
7199 -- D3 in T3 empty itself yes
7201 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7203 -- Type derivation for tagged types is fairly straightforward. If no
7204 -- discriminants are specified by the derived type, these are inherited
7205 -- from the parent. No explicit stored discriminants are ever necessary.
7206 -- The only manipulation that is done to the tree is that of adding a
7207 -- _parent field with parent type and constrained to the same constraint
7208 -- specified for the parent in the derived type definition. For instance:
7210 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7211 -- type T1 is new R with null record;
7212 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7214 -- are changed into:
7216 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7217 -- _parent : R (D1, D2, D3);
7218 -- end record;
7220 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7221 -- _parent : T1 (X2, 88, X1);
7222 -- end record;
7224 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7225 -- ORC and ICH fields are:
7227 -- Discrim CD ORC ICH
7228 -- ^^^^^^^ ^^ ^^^ ^^^
7229 -- D1 in R empty itself no
7230 -- D2 in R empty itself no
7231 -- D3 in R empty itself no
7233 -- D1 in T1 D1 in R D1 in R no
7234 -- D2 in T1 D2 in R D2 in R no
7235 -- D3 in T1 D3 in R D3 in R no
7237 -- X1 in T2 D3 in T1 D3 in R no
7238 -- X2 in T2 D1 in T1 D1 in R no
7240 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7242 -- Regardless of whether we dealing with a tagged or untagged type
7243 -- we will transform all derived type declarations of the form
7245 -- type T is new R (...) [with ...];
7246 -- or
7247 -- subtype S is R (...);
7248 -- type T is new S [with ...];
7249 -- into
7250 -- type BT is new R [with ...];
7251 -- subtype T is BT (...);
7253 -- That is, the base derived type is constrained only if it has no
7254 -- discriminants. The reason for doing this is that GNAT's semantic model
7255 -- assumes that a base type with discriminants is unconstrained.
7257 -- Note that, strictly speaking, the above transformation is not always
7258 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7260 -- procedure B34011A is
7261 -- type REC (D : integer := 0) is record
7262 -- I : Integer;
7263 -- end record;
7265 -- package P is
7266 -- type T6 is new Rec;
7267 -- function F return T6;
7268 -- end P;
7270 -- use P;
7271 -- package Q6 is
7272 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7273 -- end Q6;
7275 -- The definition of Q6.U is illegal. However transforming Q6.U into
7277 -- type BaseU is new T6;
7278 -- subtype U is BaseU (Q6.F.I)
7280 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7281 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7282 -- the transformation described above.
7284 -- There is another instance where the above transformation is incorrect.
7285 -- Consider:
7287 -- package Pack is
7288 -- type Base (D : Integer) is tagged null record;
7289 -- procedure P (X : Base);
7291 -- type Der is new Base (2) with null record;
7292 -- procedure P (X : Der);
7293 -- end Pack;
7295 -- Then the above transformation turns this into
7297 -- type Der_Base is new Base with null record;
7298 -- -- procedure P (X : Base) is implicitly inherited here
7299 -- -- as procedure P (X : Der_Base).
7301 -- subtype Der is Der_Base (2);
7302 -- procedure P (X : Der);
7303 -- -- The overriding of P (X : Der_Base) is illegal since we
7304 -- -- have a parameter conformance problem.
7306 -- To get around this problem, after having semantically processed Der_Base
7307 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7308 -- Discriminant_Constraint from Der so that when parameter conformance is
7309 -- checked when P is overridden, no semantic errors are flagged.
7311 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7313 -- Regardless of whether we are dealing with a tagged or untagged type
7314 -- we will transform all derived type declarations of the form
7316 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7317 -- type T is new R [with ...];
7318 -- into
7319 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7321 -- The reason for such transformation is that it allows us to implement a
7322 -- very clean form of component inheritance as explained below.
7324 -- Note that this transformation is not achieved by direct tree rewriting
7325 -- and manipulation, but rather by redoing the semantic actions that the
7326 -- above transformation will entail. This is done directly in routine
7327 -- Inherit_Components.
7329 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7331 -- In both tagged and untagged derived types, regular non discriminant
7332 -- components are inherited in the derived type from the parent type. In
7333 -- the absence of discriminants component, inheritance is straightforward
7334 -- as components can simply be copied from the parent.
7336 -- If the parent has discriminants, inheriting components constrained with
7337 -- these discriminants requires caution. Consider the following example:
7339 -- type R (D1, D2 : Positive) is [tagged] record
7340 -- S : String (D1 .. D2);
7341 -- end record;
7343 -- type T1 is new R [with null record];
7344 -- type T2 (X : positive) is new R (1, X) [with null record];
7346 -- As explained in 6. above, T1 is rewritten as
7347 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7348 -- which makes the treatment for T1 and T2 identical.
7350 -- What we want when inheriting S, is that references to D1 and D2 in R are
7351 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7352 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7353 -- with either discriminant references in the derived type or expressions.
7354 -- This replacement is achieved as follows: before inheriting R's
7355 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7356 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7357 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7358 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7359 -- by String (1 .. X).
7361 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7363 -- We explain here the rules governing private type extensions relevant to
7364 -- type derivation. These rules are explained on the following example:
7366 -- type D [(...)] is new A [(...)] with private; <-- partial view
7367 -- type D [(...)] is new P [(...)] with null record; <-- full view
7369 -- Type A is called the ancestor subtype of the private extension.
7370 -- Type P is the parent type of the full view of the private extension. It
7371 -- must be A or a type derived from A.
7373 -- The rules concerning the discriminants of private type extensions are
7374 -- [7.3(10-13)]:
7376 -- o If a private extension inherits known discriminants from the ancestor
7377 -- subtype, then the full view must also inherit its discriminants from
7378 -- the ancestor subtype and the parent subtype of the full view must be
7379 -- constrained if and only if the ancestor subtype is constrained.
7381 -- o If a partial view has unknown discriminants, then the full view may
7382 -- define a definite or an indefinite subtype, with or without
7383 -- discriminants.
7385 -- o If a partial view has neither known nor unknown discriminants, then
7386 -- the full view must define a definite subtype.
7388 -- o If the ancestor subtype of a private extension has constrained
7389 -- discriminants, then the parent subtype of the full view must impose a
7390 -- statically matching constraint on those discriminants.
7392 -- This means that only the following forms of private extensions are
7393 -- allowed:
7395 -- type D is new A with private; <-- partial view
7396 -- type D is new P with null record; <-- full view
7398 -- If A has no discriminants than P has no discriminants, otherwise P must
7399 -- inherit A's discriminants.
7401 -- type D is new A (...) with private; <-- partial view
7402 -- type D is new P (:::) with null record; <-- full view
7404 -- P must inherit A's discriminants and (...) and (:::) must statically
7405 -- match.
7407 -- subtype A is R (...);
7408 -- type D is new A with private; <-- partial view
7409 -- type D is new P with null record; <-- full view
7411 -- P must have inherited R's discriminants and must be derived from A or
7412 -- any of its subtypes.
7414 -- type D (..) is new A with private; <-- partial view
7415 -- type D (..) is new P [(:::)] with null record; <-- full view
7417 -- No specific constraints on P's discriminants or constraint (:::).
7418 -- Note that A can be unconstrained, but the parent subtype P must either
7419 -- be constrained or (:::) must be present.
7421 -- type D (..) is new A [(...)] with private; <-- partial view
7422 -- type D (..) is new P [(:::)] with null record; <-- full view
7424 -- P's constraints on A's discriminants must statically match those
7425 -- imposed by (...).
7427 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7429 -- The full view of a private extension is handled exactly as described
7430 -- above. The model chose for the private view of a private extension is
7431 -- the same for what concerns discriminants (i.e. they receive the same
7432 -- treatment as in the tagged case). However, the private view of the
7433 -- private extension always inherits the components of the parent base,
7434 -- without replacing any discriminant reference. Strictly speaking this is
7435 -- incorrect. However, Gigi never uses this view to generate code so this
7436 -- is a purely semantic issue. In theory, a set of transformations similar
7437 -- to those given in 5. and 6. above could be applied to private views of
7438 -- private extensions to have the same model of component inheritance as
7439 -- for non private extensions. However, this is not done because it would
7440 -- further complicate private type processing. Semantically speaking, this
7441 -- leaves us in an uncomfortable situation. As an example consider:
7443 -- package Pack is
7444 -- type R (D : integer) is tagged record
7445 -- S : String (1 .. D);
7446 -- end record;
7447 -- procedure P (X : R);
7448 -- type T is new R (1) with private;
7449 -- private
7450 -- type T is new R (1) with null record;
7451 -- end;
7453 -- This is transformed into:
7455 -- package Pack is
7456 -- type R (D : integer) is tagged record
7457 -- S : String (1 .. D);
7458 -- end record;
7459 -- procedure P (X : R);
7460 -- type T is new R (1) with private;
7461 -- private
7462 -- type BaseT is new R with null record;
7463 -- subtype T is BaseT (1);
7464 -- end;
7466 -- (strictly speaking the above is incorrect Ada)
7468 -- From the semantic standpoint the private view of private extension T
7469 -- should be flagged as constrained since one can clearly have
7471 -- Obj : T;
7473 -- in a unit withing Pack. However, when deriving subprograms for the
7474 -- private view of private extension T, T must be seen as unconstrained
7475 -- since T has discriminants (this is a constraint of the current
7476 -- subprogram derivation model). Thus, when processing the private view of
7477 -- a private extension such as T, we first mark T as unconstrained, we
7478 -- process it, we perform program derivation and just before returning from
7479 -- Build_Derived_Record_Type we mark T as constrained.
7481 -- ??? Are there are other uncomfortable cases that we will have to
7482 -- deal with.
7484 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7486 -- Types that are derived from a visible record type and have a private
7487 -- extension present other peculiarities. They behave mostly like private
7488 -- types, but if they have primitive operations defined, these will not
7489 -- have the proper signatures for further inheritance, because other
7490 -- primitive operations will use the implicit base that we define for
7491 -- private derivations below. This affect subprogram inheritance (see
7492 -- Derive_Subprograms for details). We also derive the implicit base from
7493 -- the base type of the full view, so that the implicit base is a record
7494 -- type and not another private type, This avoids infinite loops.
7496 procedure Build_Derived_Record_Type
7497 (N : Node_Id;
7498 Parent_Type : Entity_Id;
7499 Derived_Type : Entity_Id;
7500 Derive_Subps : Boolean := True)
7502 Discriminant_Specs : constant Boolean :=
7503 Present (Discriminant_Specifications (N));
7504 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7505 Loc : constant Source_Ptr := Sloc (N);
7506 Private_Extension : constant Boolean :=
7507 Nkind (N) = N_Private_Extension_Declaration;
7508 Assoc_List : Elist_Id;
7509 Constraint_Present : Boolean;
7510 Constrs : Elist_Id;
7511 Discrim : Entity_Id;
7512 Indic : Node_Id;
7513 Inherit_Discrims : Boolean := False;
7514 Last_Discrim : Entity_Id;
7515 New_Base : Entity_Id;
7516 New_Decl : Node_Id;
7517 New_Discrs : Elist_Id;
7518 New_Indic : Node_Id;
7519 Parent_Base : Entity_Id;
7520 Save_Etype : Entity_Id;
7521 Save_Discr_Constr : Elist_Id;
7522 Save_Next_Entity : Entity_Id;
7523 Type_Def : Node_Id;
7525 Discs : Elist_Id := New_Elmt_List;
7526 -- An empty Discs list means that there were no constraints in the
7527 -- subtype indication or that there was an error processing it.
7529 begin
7530 if Ekind (Parent_Type) = E_Record_Type_With_Private
7531 and then Present (Full_View (Parent_Type))
7532 and then Has_Discriminants (Parent_Type)
7533 then
7534 Parent_Base := Base_Type (Full_View (Parent_Type));
7535 else
7536 Parent_Base := Base_Type (Parent_Type);
7537 end if;
7539 -- AI05-0115 : if this is a derivation from a private type in some
7540 -- other scope that may lead to invisible components for the derived
7541 -- type, mark it accordingly.
7543 if Is_Private_Type (Parent_Type) then
7544 if Scope (Parent_Type) = Scope (Derived_Type) then
7545 null;
7547 elsif In_Open_Scopes (Scope (Parent_Type))
7548 and then In_Private_Part (Scope (Parent_Type))
7549 then
7550 null;
7552 else
7553 Set_Has_Private_Ancestor (Derived_Type);
7554 end if;
7556 else
7557 Set_Has_Private_Ancestor
7558 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7559 end if;
7561 -- Before we start the previously documented transformations, here is
7562 -- little fix for size and alignment of tagged types. Normally when we
7563 -- derive type D from type P, we copy the size and alignment of P as the
7564 -- default for D, and in the absence of explicit representation clauses
7565 -- for D, the size and alignment are indeed the same as the parent.
7567 -- But this is wrong for tagged types, since fields may be added, and
7568 -- the default size may need to be larger, and the default alignment may
7569 -- need to be larger.
7571 -- We therefore reset the size and alignment fields in the tagged case.
7572 -- Note that the size and alignment will in any case be at least as
7573 -- large as the parent type (since the derived type has a copy of the
7574 -- parent type in the _parent field)
7576 -- The type is also marked as being tagged here, which is needed when
7577 -- processing components with a self-referential anonymous access type
7578 -- in the call to Check_Anonymous_Access_Components below. Note that
7579 -- this flag is also set later on for completeness.
7581 if Is_Tagged then
7582 Set_Is_Tagged_Type (Derived_Type);
7583 Init_Size_Align (Derived_Type);
7584 end if;
7586 -- STEP 0a: figure out what kind of derived type declaration we have
7588 if Private_Extension then
7589 Type_Def := N;
7590 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7591 Set_Default_SSO (Derived_Type);
7593 else
7594 Type_Def := Type_Definition (N);
7596 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7597 -- Parent_Base can be a private type or private extension. However,
7598 -- for tagged types with an extension the newly added fields are
7599 -- visible and hence the Derived_Type is always an E_Record_Type.
7600 -- (except that the parent may have its own private fields).
7601 -- For untagged types we preserve the Ekind of the Parent_Base.
7603 if Present (Record_Extension_Part (Type_Def)) then
7604 Set_Ekind (Derived_Type, E_Record_Type);
7605 Set_Default_SSO (Derived_Type);
7607 -- Create internal access types for components with anonymous
7608 -- access types.
7610 if Ada_Version >= Ada_2005 then
7611 Check_Anonymous_Access_Components
7612 (N, Derived_Type, Derived_Type,
7613 Component_List (Record_Extension_Part (Type_Def)));
7614 end if;
7616 else
7617 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7618 end if;
7619 end if;
7621 -- Indic can either be an N_Identifier if the subtype indication
7622 -- contains no constraint or an N_Subtype_Indication if the subtype
7623 -- indication has a constraint.
7625 Indic := Subtype_Indication (Type_Def);
7626 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7628 -- Check that the type has visible discriminants. The type may be
7629 -- a private type with unknown discriminants whose full view has
7630 -- discriminants which are invisible.
7632 if Constraint_Present then
7633 if not Has_Discriminants (Parent_Base)
7634 or else
7635 (Has_Unknown_Discriminants (Parent_Base)
7636 and then Is_Private_Type (Parent_Base))
7637 then
7638 Error_Msg_N
7639 ("invalid constraint: type has no discriminant",
7640 Constraint (Indic));
7642 Constraint_Present := False;
7643 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7645 elsif Is_Constrained (Parent_Type) then
7646 Error_Msg_N
7647 ("invalid constraint: parent type is already constrained",
7648 Constraint (Indic));
7650 Constraint_Present := False;
7651 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7652 end if;
7653 end if;
7655 -- STEP 0b: If needed, apply transformation given in point 5. above
7657 if not Private_Extension
7658 and then Has_Discriminants (Parent_Type)
7659 and then not Discriminant_Specs
7660 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7661 then
7662 -- First, we must analyze the constraint (see comment in point 5.)
7663 -- The constraint may come from the subtype indication of the full
7664 -- declaration.
7666 if Constraint_Present then
7667 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7669 -- If there is no explicit constraint, there might be one that is
7670 -- inherited from a constrained parent type. In that case verify that
7671 -- it conforms to the constraint in the partial view. In perverse
7672 -- cases the parent subtypes of the partial and full view can have
7673 -- different constraints.
7675 elsif Present (Stored_Constraint (Parent_Type)) then
7676 New_Discrs := Stored_Constraint (Parent_Type);
7678 else
7679 New_Discrs := No_Elist;
7680 end if;
7682 if Has_Discriminants (Derived_Type)
7683 and then Has_Private_Declaration (Derived_Type)
7684 and then Present (Discriminant_Constraint (Derived_Type))
7685 and then Present (New_Discrs)
7686 then
7687 -- Verify that constraints of the full view statically match
7688 -- those given in the partial view.
7690 declare
7691 C1, C2 : Elmt_Id;
7693 begin
7694 C1 := First_Elmt (New_Discrs);
7695 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7696 while Present (C1) and then Present (C2) loop
7697 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7698 or else
7699 (Is_OK_Static_Expression (Node (C1))
7700 and then Is_OK_Static_Expression (Node (C2))
7701 and then
7702 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7703 then
7704 null;
7706 else
7707 if Constraint_Present then
7708 Error_Msg_N
7709 ("constraint not conformant to previous declaration",
7710 Node (C1));
7711 else
7712 Error_Msg_N
7713 ("constraint of full view is incompatible "
7714 & "with partial view", N);
7715 end if;
7716 end if;
7718 Next_Elmt (C1);
7719 Next_Elmt (C2);
7720 end loop;
7721 end;
7722 end if;
7724 -- Insert and analyze the declaration for the unconstrained base type
7726 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7728 New_Decl :=
7729 Make_Full_Type_Declaration (Loc,
7730 Defining_Identifier => New_Base,
7731 Type_Definition =>
7732 Make_Derived_Type_Definition (Loc,
7733 Abstract_Present => Abstract_Present (Type_Def),
7734 Limited_Present => Limited_Present (Type_Def),
7735 Subtype_Indication =>
7736 New_Occurrence_Of (Parent_Base, Loc),
7737 Record_Extension_Part =>
7738 Relocate_Node (Record_Extension_Part (Type_Def)),
7739 Interface_List => Interface_List (Type_Def)));
7741 Set_Parent (New_Decl, Parent (N));
7742 Mark_Rewrite_Insertion (New_Decl);
7743 Insert_Before (N, New_Decl);
7745 -- In the extension case, make sure ancestor is frozen appropriately
7746 -- (see also non-discriminated case below).
7748 if Present (Record_Extension_Part (Type_Def))
7749 or else Is_Interface (Parent_Base)
7750 then
7751 Freeze_Before (New_Decl, Parent_Type);
7752 end if;
7754 -- Note that this call passes False for the Derive_Subps parameter
7755 -- because subprogram derivation is deferred until after creating
7756 -- the subtype (see below).
7758 Build_Derived_Type
7759 (New_Decl, Parent_Base, New_Base,
7760 Is_Completion => True, Derive_Subps => False);
7762 -- ??? This needs re-examination to determine whether the
7763 -- above call can simply be replaced by a call to Analyze.
7765 Set_Analyzed (New_Decl);
7767 -- Insert and analyze the declaration for the constrained subtype
7769 if Constraint_Present then
7770 New_Indic :=
7771 Make_Subtype_Indication (Loc,
7772 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7773 Constraint => Relocate_Node (Constraint (Indic)));
7775 else
7776 declare
7777 Constr_List : constant List_Id := New_List;
7778 C : Elmt_Id;
7779 Expr : Node_Id;
7781 begin
7782 C := First_Elmt (Discriminant_Constraint (Parent_Type));
7783 while Present (C) loop
7784 Expr := Node (C);
7786 -- It is safe here to call New_Copy_Tree since
7787 -- Force_Evaluation was called on each constraint in
7788 -- Build_Discriminant_Constraints.
7790 Append (New_Copy_Tree (Expr), To => Constr_List);
7792 Next_Elmt (C);
7793 end loop;
7795 New_Indic :=
7796 Make_Subtype_Indication (Loc,
7797 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7798 Constraint =>
7799 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
7800 end;
7801 end if;
7803 Rewrite (N,
7804 Make_Subtype_Declaration (Loc,
7805 Defining_Identifier => Derived_Type,
7806 Subtype_Indication => New_Indic));
7808 Analyze (N);
7810 -- Derivation of subprograms must be delayed until the full subtype
7811 -- has been established, to ensure proper overriding of subprograms
7812 -- inherited by full types. If the derivations occurred as part of
7813 -- the call to Build_Derived_Type above, then the check for type
7814 -- conformance would fail because earlier primitive subprograms
7815 -- could still refer to the full type prior the change to the new
7816 -- subtype and hence would not match the new base type created here.
7817 -- Subprograms are not derived, however, when Derive_Subps is False
7818 -- (since otherwise there could be redundant derivations).
7820 if Derive_Subps then
7821 Derive_Subprograms (Parent_Type, Derived_Type);
7822 end if;
7824 -- For tagged types the Discriminant_Constraint of the new base itype
7825 -- is inherited from the first subtype so that no subtype conformance
7826 -- problem arise when the first subtype overrides primitive
7827 -- operations inherited by the implicit base type.
7829 if Is_Tagged then
7830 Set_Discriminant_Constraint
7831 (New_Base, Discriminant_Constraint (Derived_Type));
7832 end if;
7834 return;
7835 end if;
7837 -- If we get here Derived_Type will have no discriminants or it will be
7838 -- a discriminated unconstrained base type.
7840 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7842 if Is_Tagged then
7844 -- The parent type is frozen for non-private extensions (RM 13.14(7))
7845 -- The declaration of a specific descendant of an interface type
7846 -- freezes the interface type (RM 13.14).
7848 if not Private_Extension or else Is_Interface (Parent_Base) then
7849 Freeze_Before (N, Parent_Type);
7850 end if;
7852 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7853 -- cannot be declared at a deeper level than its parent type is
7854 -- removed. The check on derivation within a generic body is also
7855 -- relaxed, but there's a restriction that a derived tagged type
7856 -- cannot be declared in a generic body if it's derived directly
7857 -- or indirectly from a formal type of that generic.
7859 if Ada_Version >= Ada_2005 then
7860 if Present (Enclosing_Generic_Body (Derived_Type)) then
7861 declare
7862 Ancestor_Type : Entity_Id;
7864 begin
7865 -- Check to see if any ancestor of the derived type is a
7866 -- formal type.
7868 Ancestor_Type := Parent_Type;
7869 while not Is_Generic_Type (Ancestor_Type)
7870 and then Etype (Ancestor_Type) /= Ancestor_Type
7871 loop
7872 Ancestor_Type := Etype (Ancestor_Type);
7873 end loop;
7875 -- If the derived type does have a formal type as an
7876 -- ancestor, then it's an error if the derived type is
7877 -- declared within the body of the generic unit that
7878 -- declares the formal type in its generic formal part. It's
7879 -- sufficient to check whether the ancestor type is declared
7880 -- inside the same generic body as the derived type (such as
7881 -- within a nested generic spec), in which case the
7882 -- derivation is legal. If the formal type is declared
7883 -- outside of that generic body, then it's guaranteed that
7884 -- the derived type is declared within the generic body of
7885 -- the generic unit declaring the formal type.
7887 if Is_Generic_Type (Ancestor_Type)
7888 and then Enclosing_Generic_Body (Ancestor_Type) /=
7889 Enclosing_Generic_Body (Derived_Type)
7890 then
7891 Error_Msg_NE
7892 ("parent type of& must not be descendant of formal type"
7893 & " of an enclosing generic body",
7894 Indic, Derived_Type);
7895 end if;
7896 end;
7897 end if;
7899 elsif Type_Access_Level (Derived_Type) /=
7900 Type_Access_Level (Parent_Type)
7901 and then not Is_Generic_Type (Derived_Type)
7902 then
7903 if Is_Controlled (Parent_Type) then
7904 Error_Msg_N
7905 ("controlled type must be declared at the library level",
7906 Indic);
7907 else
7908 Error_Msg_N
7909 ("type extension at deeper accessibility level than parent",
7910 Indic);
7911 end if;
7913 else
7914 declare
7915 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7916 begin
7917 if Present (GB)
7918 and then GB /= Enclosing_Generic_Body (Parent_Base)
7919 then
7920 Error_Msg_NE
7921 ("parent type of& must not be outside generic body"
7922 & " (RM 3.9.1(4))",
7923 Indic, Derived_Type);
7924 end if;
7925 end;
7926 end if;
7927 end if;
7929 -- Ada 2005 (AI-251)
7931 if Ada_Version >= Ada_2005 and then Is_Tagged then
7933 -- "The declaration of a specific descendant of an interface type
7934 -- freezes the interface type" (RM 13.14).
7936 declare
7937 Iface : Node_Id;
7938 begin
7939 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7940 Iface := First (Interface_List (Type_Def));
7941 while Present (Iface) loop
7942 Freeze_Before (N, Etype (Iface));
7943 Next (Iface);
7944 end loop;
7945 end if;
7946 end;
7947 end if;
7949 -- STEP 1b : preliminary cleanup of the full view of private types
7951 -- If the type is already marked as having discriminants, then it's the
7952 -- completion of a private type or private extension and we need to
7953 -- retain the discriminants from the partial view if the current
7954 -- declaration has Discriminant_Specifications so that we can verify
7955 -- conformance. However, we must remove any existing components that
7956 -- were inherited from the parent (and attached in Copy_And_Swap)
7957 -- because the full type inherits all appropriate components anyway, and
7958 -- we do not want the partial view's components interfering.
7960 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7961 Discrim := First_Discriminant (Derived_Type);
7962 loop
7963 Last_Discrim := Discrim;
7964 Next_Discriminant (Discrim);
7965 exit when No (Discrim);
7966 end loop;
7968 Set_Last_Entity (Derived_Type, Last_Discrim);
7970 -- In all other cases wipe out the list of inherited components (even
7971 -- inherited discriminants), it will be properly rebuilt here.
7973 else
7974 Set_First_Entity (Derived_Type, Empty);
7975 Set_Last_Entity (Derived_Type, Empty);
7976 end if;
7978 -- STEP 1c: Initialize some flags for the Derived_Type
7980 -- The following flags must be initialized here so that
7981 -- Process_Discriminants can check that discriminants of tagged types do
7982 -- not have a default initial value and that access discriminants are
7983 -- only specified for limited records. For completeness, these flags are
7984 -- also initialized along with all the other flags below.
7986 -- AI-419: Limitedness is not inherited from an interface parent, so to
7987 -- be limited in that case the type must be explicitly declared as
7988 -- limited. However, task and protected interfaces are always limited.
7990 if Limited_Present (Type_Def) then
7991 Set_Is_Limited_Record (Derived_Type);
7993 elsif Is_Limited_Record (Parent_Type)
7994 or else (Present (Full_View (Parent_Type))
7995 and then Is_Limited_Record (Full_View (Parent_Type)))
7996 then
7997 if not Is_Interface (Parent_Type)
7998 or else Is_Synchronized_Interface (Parent_Type)
7999 or else Is_Protected_Interface (Parent_Type)
8000 or else Is_Task_Interface (Parent_Type)
8001 then
8002 Set_Is_Limited_Record (Derived_Type);
8003 end if;
8004 end if;
8006 -- STEP 2a: process discriminants of derived type if any
8008 Push_Scope (Derived_Type);
8010 if Discriminant_Specs then
8011 Set_Has_Unknown_Discriminants (Derived_Type, False);
8013 -- The following call initializes fields Has_Discriminants and
8014 -- Discriminant_Constraint, unless we are processing the completion
8015 -- of a private type declaration.
8017 Check_Or_Process_Discriminants (N, Derived_Type);
8019 -- For untagged types, the constraint on the Parent_Type must be
8020 -- present and is used to rename the discriminants.
8022 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8023 Error_Msg_N ("untagged parent must have discriminants", Indic);
8025 elsif not Is_Tagged and then not Constraint_Present then
8026 Error_Msg_N
8027 ("discriminant constraint needed for derived untagged records",
8028 Indic);
8030 -- Otherwise the parent subtype must be constrained unless we have a
8031 -- private extension.
8033 elsif not Constraint_Present
8034 and then not Private_Extension
8035 and then not Is_Constrained (Parent_Type)
8036 then
8037 Error_Msg_N
8038 ("unconstrained type not allowed in this context", Indic);
8040 elsif Constraint_Present then
8041 -- The following call sets the field Corresponding_Discriminant
8042 -- for the discriminants in the Derived_Type.
8044 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8046 -- For untagged types all new discriminants must rename
8047 -- discriminants in the parent. For private extensions new
8048 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8050 Discrim := First_Discriminant (Derived_Type);
8051 while Present (Discrim) loop
8052 if not Is_Tagged
8053 and then No (Corresponding_Discriminant (Discrim))
8054 then
8055 Error_Msg_N
8056 ("new discriminants must constrain old ones", Discrim);
8058 elsif Private_Extension
8059 and then Present (Corresponding_Discriminant (Discrim))
8060 then
8061 Error_Msg_N
8062 ("only static constraints allowed for parent"
8063 & " discriminants in the partial view", Indic);
8064 exit;
8065 end if;
8067 -- If a new discriminant is used in the constraint, then its
8068 -- subtype must be statically compatible with the parent
8069 -- discriminant's subtype (3.7(15)).
8071 -- However, if the record contains an array constrained by
8072 -- the discriminant but with some different bound, the compiler
8073 -- attemps to create a smaller range for the discriminant type.
8074 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8075 -- the discriminant type is a scalar type, the check must use
8076 -- the original discriminant type in the parent declaration.
8078 declare
8079 Corr_Disc : constant Entity_Id :=
8080 Corresponding_Discriminant (Discrim);
8081 Disc_Type : constant Entity_Id := Etype (Discrim);
8082 Corr_Type : Entity_Id;
8084 begin
8085 if Present (Corr_Disc) then
8086 if Is_Scalar_Type (Disc_Type) then
8087 Corr_Type :=
8088 Entity (Discriminant_Type (Parent (Corr_Disc)));
8089 else
8090 Corr_Type := Etype (Corr_Disc);
8091 end if;
8093 if not
8094 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8095 then
8096 Error_Msg_N
8097 ("subtype must be compatible "
8098 & "with parent discriminant",
8099 Discrim);
8100 end if;
8101 end if;
8102 end;
8104 Next_Discriminant (Discrim);
8105 end loop;
8107 -- Check whether the constraints of the full view statically
8108 -- match those imposed by the parent subtype [7.3(13)].
8110 if Present (Stored_Constraint (Derived_Type)) then
8111 declare
8112 C1, C2 : Elmt_Id;
8114 begin
8115 C1 := First_Elmt (Discs);
8116 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8117 while Present (C1) and then Present (C2) loop
8118 if not
8119 Fully_Conformant_Expressions (Node (C1), Node (C2))
8120 then
8121 Error_Msg_N
8122 ("not conformant with previous declaration",
8123 Node (C1));
8124 end if;
8126 Next_Elmt (C1);
8127 Next_Elmt (C2);
8128 end loop;
8129 end;
8130 end if;
8131 end if;
8133 -- STEP 2b: No new discriminants, inherit discriminants if any
8135 else
8136 if Private_Extension then
8137 Set_Has_Unknown_Discriminants
8138 (Derived_Type,
8139 Has_Unknown_Discriminants (Parent_Type)
8140 or else Unknown_Discriminants_Present (N));
8142 -- The partial view of the parent may have unknown discriminants,
8143 -- but if the full view has discriminants and the parent type is
8144 -- in scope they must be inherited.
8146 elsif Has_Unknown_Discriminants (Parent_Type)
8147 and then
8148 (not Has_Discriminants (Parent_Type)
8149 or else not In_Open_Scopes (Scope (Parent_Type)))
8150 then
8151 Set_Has_Unknown_Discriminants (Derived_Type);
8152 end if;
8154 if not Has_Unknown_Discriminants (Derived_Type)
8155 and then not Has_Unknown_Discriminants (Parent_Base)
8156 and then Has_Discriminants (Parent_Type)
8157 then
8158 Inherit_Discrims := True;
8159 Set_Has_Discriminants
8160 (Derived_Type, True);
8161 Set_Discriminant_Constraint
8162 (Derived_Type, Discriminant_Constraint (Parent_Base));
8163 end if;
8165 -- The following test is true for private types (remember
8166 -- transformation 5. is not applied to those) and in an error
8167 -- situation.
8169 if Constraint_Present then
8170 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8171 end if;
8173 -- For now mark a new derived type as constrained only if it has no
8174 -- discriminants. At the end of Build_Derived_Record_Type we properly
8175 -- set this flag in the case of private extensions. See comments in
8176 -- point 9. just before body of Build_Derived_Record_Type.
8178 Set_Is_Constrained
8179 (Derived_Type,
8180 not (Inherit_Discrims
8181 or else Has_Unknown_Discriminants (Derived_Type)));
8182 end if;
8184 -- STEP 3: initialize fields of derived type
8186 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8187 Set_Stored_Constraint (Derived_Type, No_Elist);
8189 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8190 -- but cannot be interfaces
8192 if not Private_Extension
8193 and then Ekind (Derived_Type) /= E_Private_Type
8194 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8195 then
8196 if Interface_Present (Type_Def) then
8197 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8198 end if;
8200 Set_Interfaces (Derived_Type, No_Elist);
8201 end if;
8203 -- Fields inherited from the Parent_Type
8205 Set_Has_Specified_Layout
8206 (Derived_Type, Has_Specified_Layout (Parent_Type));
8207 Set_Is_Limited_Composite
8208 (Derived_Type, Is_Limited_Composite (Parent_Type));
8209 Set_Is_Private_Composite
8210 (Derived_Type, Is_Private_Composite (Parent_Type));
8212 -- Fields inherited from the Parent_Base
8214 Set_Has_Controlled_Component
8215 (Derived_Type, Has_Controlled_Component (Parent_Base));
8216 Set_Has_Non_Standard_Rep
8217 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8218 Set_Has_Primitive_Operations
8219 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8221 -- Fields inherited from the Parent_Base in the non-private case
8223 if Ekind (Derived_Type) = E_Record_Type then
8224 Set_Has_Complex_Representation
8225 (Derived_Type, Has_Complex_Representation (Parent_Base));
8226 end if;
8228 -- Fields inherited from the Parent_Base for record types
8230 if Is_Record_Type (Derived_Type) then
8232 declare
8233 Parent_Full : Entity_Id;
8235 begin
8236 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8237 -- Parent_Base can be a private type or private extension. Go
8238 -- to the full view here to get the E_Record_Type specific flags.
8240 if Present (Full_View (Parent_Base)) then
8241 Parent_Full := Full_View (Parent_Base);
8242 else
8243 Parent_Full := Parent_Base;
8244 end if;
8246 Set_OK_To_Reorder_Components
8247 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8248 end;
8249 end if;
8251 -- Set fields for private derived types
8253 if Is_Private_Type (Derived_Type) then
8254 Set_Depends_On_Private (Derived_Type, True);
8255 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8257 -- Inherit fields from non private record types. If this is the
8258 -- completion of a derivation from a private type, the parent itself
8259 -- is private, and the attributes come from its full view, which must
8260 -- be present.
8262 else
8263 if Is_Private_Type (Parent_Base)
8264 and then not Is_Record_Type (Parent_Base)
8265 then
8266 Set_Component_Alignment
8267 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8268 Set_C_Pass_By_Copy
8269 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8270 else
8271 Set_Component_Alignment
8272 (Derived_Type, Component_Alignment (Parent_Base));
8273 Set_C_Pass_By_Copy
8274 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8275 end if;
8276 end if;
8278 -- Set fields for tagged types
8280 if Is_Tagged then
8281 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8283 -- All tagged types defined in Ada.Finalization are controlled
8285 if Chars (Scope (Derived_Type)) = Name_Finalization
8286 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8287 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8288 then
8289 Set_Is_Controlled (Derived_Type);
8290 else
8291 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8292 end if;
8294 -- Minor optimization: there is no need to generate the class-wide
8295 -- entity associated with an underlying record view.
8297 if not Is_Underlying_Record_View (Derived_Type) then
8298 Make_Class_Wide_Type (Derived_Type);
8299 end if;
8301 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8303 if Has_Discriminants (Derived_Type)
8304 and then Constraint_Present
8305 then
8306 Set_Stored_Constraint
8307 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8308 end if;
8310 if Ada_Version >= Ada_2005 then
8311 declare
8312 Ifaces_List : Elist_Id;
8314 begin
8315 -- Checks rules 3.9.4 (13/2 and 14/2)
8317 if Comes_From_Source (Derived_Type)
8318 and then not Is_Private_Type (Derived_Type)
8319 and then Is_Interface (Parent_Type)
8320 and then not Is_Interface (Derived_Type)
8321 then
8322 if Is_Task_Interface (Parent_Type) then
8323 Error_Msg_N
8324 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8325 Derived_Type);
8327 elsif Is_Protected_Interface (Parent_Type) then
8328 Error_Msg_N
8329 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8330 Derived_Type);
8331 end if;
8332 end if;
8334 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8336 Check_Interfaces (N, Type_Def);
8338 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8339 -- not already in the parents.
8341 Collect_Interfaces
8342 (T => Derived_Type,
8343 Ifaces_List => Ifaces_List,
8344 Exclude_Parents => True);
8346 Set_Interfaces (Derived_Type, Ifaces_List);
8348 -- If the derived type is the anonymous type created for
8349 -- a declaration whose parent has a constraint, propagate
8350 -- the interface list to the source type. This must be done
8351 -- prior to the completion of the analysis of the source type
8352 -- because the components in the extension may contain current
8353 -- instances whose legality depends on some ancestor.
8355 if Is_Itype (Derived_Type) then
8356 declare
8357 Def : constant Node_Id :=
8358 Associated_Node_For_Itype (Derived_Type);
8359 begin
8360 if Present (Def)
8361 and then Nkind (Def) = N_Full_Type_Declaration
8362 then
8363 Set_Interfaces
8364 (Defining_Identifier (Def), Ifaces_List);
8365 end if;
8366 end;
8367 end if;
8368 end;
8369 end if;
8371 else
8372 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8373 Set_Has_Non_Standard_Rep
8374 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8375 end if;
8377 -- STEP 4: Inherit components from the parent base and constrain them.
8378 -- Apply the second transformation described in point 6. above.
8380 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8381 or else not Has_Discriminants (Parent_Type)
8382 or else not Is_Constrained (Parent_Type)
8383 then
8384 Constrs := Discs;
8385 else
8386 Constrs := Discriminant_Constraint (Parent_Type);
8387 end if;
8389 Assoc_List :=
8390 Inherit_Components
8391 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8393 -- STEP 5a: Copy the parent record declaration for untagged types
8395 if not Is_Tagged then
8397 -- Discriminant_Constraint (Derived_Type) has been properly
8398 -- constructed. Save it and temporarily set it to Empty because we
8399 -- do not want the call to New_Copy_Tree below to mess this list.
8401 if Has_Discriminants (Derived_Type) then
8402 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8403 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8404 else
8405 Save_Discr_Constr := No_Elist;
8406 end if;
8408 -- Save the Etype field of Derived_Type. It is correctly set now,
8409 -- but the call to New_Copy tree may remap it to point to itself,
8410 -- which is not what we want. Ditto for the Next_Entity field.
8412 Save_Etype := Etype (Derived_Type);
8413 Save_Next_Entity := Next_Entity (Derived_Type);
8415 -- Assoc_List maps all stored discriminants in the Parent_Base to
8416 -- stored discriminants in the Derived_Type. It is fundamental that
8417 -- no types or itypes with discriminants other than the stored
8418 -- discriminants appear in the entities declared inside
8419 -- Derived_Type, since the back end cannot deal with it.
8421 New_Decl :=
8422 New_Copy_Tree
8423 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8425 -- Restore the fields saved prior to the New_Copy_Tree call
8426 -- and compute the stored constraint.
8428 Set_Etype (Derived_Type, Save_Etype);
8429 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8431 if Has_Discriminants (Derived_Type) then
8432 Set_Discriminant_Constraint
8433 (Derived_Type, Save_Discr_Constr);
8434 Set_Stored_Constraint
8435 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8436 Replace_Components (Derived_Type, New_Decl);
8437 Set_Has_Implicit_Dereference
8438 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8439 end if;
8441 -- Insert the new derived type declaration
8443 Rewrite (N, New_Decl);
8445 -- STEP 5b: Complete the processing for record extensions in generics
8447 -- There is no completion for record extensions declared in the
8448 -- parameter part of a generic, so we need to complete processing for
8449 -- these generic record extensions here. The Record_Type_Definition call
8450 -- will change the Ekind of the components from E_Void to E_Component.
8452 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8453 Record_Type_Definition (Empty, Derived_Type);
8455 -- STEP 5c: Process the record extension for non private tagged types
8457 elsif not Private_Extension then
8458 Expand_Record_Extension (Derived_Type, Type_Def);
8460 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8461 -- derived type to propagate some semantic information. This led
8462 -- to other ASIS failures and has been removed.
8464 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8465 -- implemented interfaces if we are in expansion mode
8467 if Expander_Active
8468 and then Has_Interfaces (Derived_Type)
8469 then
8470 Add_Interface_Tag_Components (N, Derived_Type);
8471 end if;
8473 -- Analyze the record extension
8475 Record_Type_Definition
8476 (Record_Extension_Part (Type_Def), Derived_Type);
8477 end if;
8479 End_Scope;
8481 -- Nothing else to do if there is an error in the derivation.
8482 -- An unusual case: the full view may be derived from a type in an
8483 -- instance, when the partial view was used illegally as an actual
8484 -- in that instance, leading to a circular definition.
8486 if Etype (Derived_Type) = Any_Type
8487 or else Etype (Parent_Type) = Derived_Type
8488 then
8489 return;
8490 end if;
8492 -- Set delayed freeze and then derive subprograms, we need to do
8493 -- this in this order so that derived subprograms inherit the
8494 -- derived freeze if necessary.
8496 Set_Has_Delayed_Freeze (Derived_Type);
8498 if Derive_Subps then
8499 Derive_Subprograms (Parent_Type, Derived_Type);
8500 end if;
8502 -- If we have a private extension which defines a constrained derived
8503 -- type mark as constrained here after we have derived subprograms. See
8504 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8506 if Private_Extension and then Inherit_Discrims then
8507 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8508 Set_Is_Constrained (Derived_Type, True);
8509 Set_Discriminant_Constraint (Derived_Type, Discs);
8511 elsif Is_Constrained (Parent_Type) then
8512 Set_Is_Constrained
8513 (Derived_Type, True);
8514 Set_Discriminant_Constraint
8515 (Derived_Type, Discriminant_Constraint (Parent_Type));
8516 end if;
8517 end if;
8519 -- Update the class-wide type, which shares the now-completed entity
8520 -- list with its specific type. In case of underlying record views,
8521 -- we do not generate the corresponding class wide entity.
8523 if Is_Tagged
8524 and then not Is_Underlying_Record_View (Derived_Type)
8525 then
8526 Set_First_Entity
8527 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8528 Set_Last_Entity
8529 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8530 end if;
8532 Check_Function_Writable_Actuals (N);
8534 -- Propagate the attributes related to pragma Default_Initial_Condition
8535 -- from the parent type to the private extension. A derived type always
8536 -- inherits the default initial condition flag from the parent type. If
8537 -- the derived type carries its own Default_Initial_Condition pragma,
8538 -- the flag is later reset in Analyze_Pragma. Note that both flags are
8539 -- mutually exclusive.
8541 if Has_Inherited_Default_Init_Cond (Parent_Type)
8542 or else Present (Get_Pragma
8543 (Parent_Type, Pragma_Default_Initial_Condition))
8544 then
8545 Set_Has_Inherited_Default_Init_Cond (Derived_Type);
8547 elsif Has_Default_Init_Cond (Parent_Type) then
8548 Set_Has_Default_Init_Cond (Derived_Type);
8549 end if;
8550 end Build_Derived_Record_Type;
8552 ------------------------
8553 -- Build_Derived_Type --
8554 ------------------------
8556 procedure Build_Derived_Type
8557 (N : Node_Id;
8558 Parent_Type : Entity_Id;
8559 Derived_Type : Entity_Id;
8560 Is_Completion : Boolean;
8561 Derive_Subps : Boolean := True)
8563 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8565 begin
8566 -- Set common attributes
8568 Set_Scope (Derived_Type, Current_Scope);
8570 Set_Etype (Derived_Type, Parent_Base);
8571 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8572 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8573 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8575 Set_Size_Info (Derived_Type, Parent_Type);
8576 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8577 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8578 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8580 -- If the parent has primitive routines, set the derived type link
8582 if Has_Primitive_Operations (Parent_Type) then
8583 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8584 end if;
8586 -- If the parent type is a private subtype, the convention on the base
8587 -- type may be set in the private part, and not propagated to the
8588 -- subtype until later, so we obtain the convention from the base type.
8590 Set_Convention (Derived_Type, Convention (Parent_Base));
8592 -- Set SSO default for record or array type
8594 if (Is_Array_Type (Derived_Type)
8595 or else Is_Record_Type (Derived_Type))
8596 and then Is_Base_Type (Derived_Type)
8597 then
8598 Set_Default_SSO (Derived_Type);
8599 end if;
8601 -- Propagate invariant information. The new type has invariants if
8602 -- they are inherited from the parent type, and these invariants can
8603 -- be further inherited, so both flags are set.
8605 -- We similarly inherit predicates
8607 if Has_Predicates (Parent_Type) then
8608 Set_Has_Predicates (Derived_Type);
8609 end if;
8611 -- The derived type inherits the representation clauses of the parent.
8612 -- However, for a private type that is completed by a derivation, there
8613 -- may be operation attributes that have been specified already (stream
8614 -- attributes and External_Tag) and those must be provided. Finally, if
8615 -- the partial view is a private extension, the representation items of
8616 -- the parent have been inherited already, and should not be chained
8617 -- twice to the derived type.
8619 -- Historic note: The guard below used to check whether the parent type
8620 -- is tagged. This is no longer needed because an untagged derived type
8621 -- may carry rep items of its own as a result of certain SPARK pragmas.
8622 -- With the old guard in place, the rep items of the derived type were
8623 -- clobbered.
8625 if Present (First_Rep_Item (Derived_Type)) then
8626 declare
8627 Par_Item : constant Node_Id := First_Rep_Item (Parent_Type);
8628 Inherited : Boolean := False;
8629 Item : Node_Id;
8630 Last_Item : Node_Id;
8632 begin
8633 -- Inspect the rep item chain of the derived type and perform the
8634 -- following two functions:
8635 -- 1) Determine whether the derived type already inherited the
8636 -- rep items of the parent type.
8637 -- 2) Find the last rep item of the derived type
8639 Item := First_Rep_Item (Derived_Type);
8640 Last_Item := Item;
8641 while Present (Item) loop
8642 if Item = Par_Item then
8643 Inherited := True;
8644 exit;
8645 end if;
8647 Last_Item := Item;
8648 Item := Next_Rep_Item (Item);
8649 end loop;
8651 -- Nothing to do if the derived type already inherited the rep
8652 -- items from the parent type, otherwise append the parent rep
8653 -- item chain to that of the derived type.
8655 if not Inherited then
8656 Set_Next_Rep_Item (Last_Item, Par_Item);
8657 end if;
8658 end;
8660 -- Otherwise the derived type lacks rep items and directly inherits the
8661 -- rep items of the parent type.
8663 else
8664 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
8665 end if;
8667 -- If the parent type has delayed rep aspects, then mark the derived
8668 -- type as possibly inheriting a delayed rep aspect.
8670 if Has_Delayed_Rep_Aspects (Parent_Type) then
8671 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
8672 end if;
8674 -- Type dependent processing
8676 case Ekind (Parent_Type) is
8677 when Numeric_Kind =>
8678 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
8680 when Array_Kind =>
8681 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
8683 when E_Record_Type
8684 | E_Record_Subtype
8685 | Class_Wide_Kind =>
8686 Build_Derived_Record_Type
8687 (N, Parent_Type, Derived_Type, Derive_Subps);
8688 return;
8690 when Enumeration_Kind =>
8691 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
8693 when Access_Kind =>
8694 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
8696 when Incomplete_Or_Private_Kind =>
8697 Build_Derived_Private_Type
8698 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8700 -- For discriminated types, the derivation includes deriving
8701 -- primitive operations. For others it is done below.
8703 if Is_Tagged_Type (Parent_Type)
8704 or else Has_Discriminants (Parent_Type)
8705 or else (Present (Full_View (Parent_Type))
8706 and then Has_Discriminants (Full_View (Parent_Type)))
8707 then
8708 return;
8709 end if;
8711 when Concurrent_Kind =>
8712 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8714 when others =>
8715 raise Program_Error;
8716 end case;
8718 -- Nothing more to do if some error occurred
8720 if Etype (Derived_Type) = Any_Type then
8721 return;
8722 end if;
8724 -- Set delayed freeze and then derive subprograms, we need to do this
8725 -- in this order so that derived subprograms inherit the derived freeze
8726 -- if necessary.
8728 Set_Has_Delayed_Freeze (Derived_Type);
8730 if Derive_Subps then
8731 Derive_Subprograms (Parent_Type, Derived_Type);
8732 end if;
8734 Set_Has_Primitive_Operations
8735 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8736 end Build_Derived_Type;
8738 -----------------------
8739 -- Build_Discriminal --
8740 -----------------------
8742 procedure Build_Discriminal (Discrim : Entity_Id) is
8743 D_Minal : Entity_Id;
8744 CR_Disc : Entity_Id;
8746 begin
8747 -- A discriminal has the same name as the discriminant
8749 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8751 Set_Ekind (D_Minal, E_In_Parameter);
8752 Set_Mechanism (D_Minal, Default_Mechanism);
8753 Set_Etype (D_Minal, Etype (Discrim));
8754 Set_Scope (D_Minal, Current_Scope);
8756 Set_Discriminal (Discrim, D_Minal);
8757 Set_Discriminal_Link (D_Minal, Discrim);
8759 -- For task types, build at once the discriminants of the corresponding
8760 -- record, which are needed if discriminants are used in entry defaults
8761 -- and in family bounds.
8763 if Is_Concurrent_Type (Current_Scope)
8764 or else Is_Limited_Type (Current_Scope)
8765 then
8766 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8768 Set_Ekind (CR_Disc, E_In_Parameter);
8769 Set_Mechanism (CR_Disc, Default_Mechanism);
8770 Set_Etype (CR_Disc, Etype (Discrim));
8771 Set_Scope (CR_Disc, Current_Scope);
8772 Set_Discriminal_Link (CR_Disc, Discrim);
8773 Set_CR_Discriminant (Discrim, CR_Disc);
8774 end if;
8775 end Build_Discriminal;
8777 ------------------------------------
8778 -- Build_Discriminant_Constraints --
8779 ------------------------------------
8781 function Build_Discriminant_Constraints
8782 (T : Entity_Id;
8783 Def : Node_Id;
8784 Derived_Def : Boolean := False) return Elist_Id
8786 C : constant Node_Id := Constraint (Def);
8787 Nb_Discr : constant Nat := Number_Discriminants (T);
8789 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
8790 -- Saves the expression corresponding to a given discriminant in T
8792 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
8793 -- Return the Position number within array Discr_Expr of a discriminant
8794 -- D within the discriminant list of the discriminated type T.
8796 procedure Process_Discriminant_Expression
8797 (Expr : Node_Id;
8798 D : Entity_Id);
8799 -- If this is a discriminant constraint on a partial view, do not
8800 -- generate an overflow check on the discriminant expression. The check
8801 -- will be generated when constraining the full view. Otherwise the
8802 -- backend creates duplicate symbols for the temporaries corresponding
8803 -- to the expressions to be checked, causing spurious assembler errors.
8805 ------------------
8806 -- Pos_Of_Discr --
8807 ------------------
8809 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
8810 Disc : Entity_Id;
8812 begin
8813 Disc := First_Discriminant (T);
8814 for J in Discr_Expr'Range loop
8815 if Disc = D then
8816 return J;
8817 end if;
8819 Next_Discriminant (Disc);
8820 end loop;
8822 -- Note: Since this function is called on discriminants that are
8823 -- known to belong to the discriminated type, falling through the
8824 -- loop with no match signals an internal compiler error.
8826 raise Program_Error;
8827 end Pos_Of_Discr;
8829 -------------------------------------
8830 -- Process_Discriminant_Expression --
8831 -------------------------------------
8833 procedure Process_Discriminant_Expression
8834 (Expr : Node_Id;
8835 D : Entity_Id)
8837 BDT : constant Entity_Id := Base_Type (Etype (D));
8839 begin
8840 -- If this is a discriminant constraint on a partial view, do
8841 -- not generate an overflow on the discriminant expression. The
8842 -- check will be generated when constraining the full view.
8844 if Is_Private_Type (T)
8845 and then Present (Full_View (T))
8846 then
8847 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
8848 else
8849 Analyze_And_Resolve (Expr, BDT);
8850 end if;
8851 end Process_Discriminant_Expression;
8853 -- Declarations local to Build_Discriminant_Constraints
8855 Discr : Entity_Id;
8856 E : Entity_Id;
8857 Elist : constant Elist_Id := New_Elmt_List;
8859 Constr : Node_Id;
8860 Expr : Node_Id;
8861 Id : Node_Id;
8862 Position : Nat;
8863 Found : Boolean;
8865 Discrim_Present : Boolean := False;
8867 -- Start of processing for Build_Discriminant_Constraints
8869 begin
8870 -- The following loop will process positional associations only.
8871 -- For a positional association, the (single) discriminant is
8872 -- implicitly specified by position, in textual order (RM 3.7.2).
8874 Discr := First_Discriminant (T);
8875 Constr := First (Constraints (C));
8876 for D in Discr_Expr'Range loop
8877 exit when Nkind (Constr) = N_Discriminant_Association;
8879 if No (Constr) then
8880 Error_Msg_N ("too few discriminants given in constraint", C);
8881 return New_Elmt_List;
8883 elsif Nkind (Constr) = N_Range
8884 or else (Nkind (Constr) = N_Attribute_Reference
8885 and then
8886 Attribute_Name (Constr) = Name_Range)
8887 then
8888 Error_Msg_N
8889 ("a range is not a valid discriminant constraint", Constr);
8890 Discr_Expr (D) := Error;
8892 else
8893 Process_Discriminant_Expression (Constr, Discr);
8894 Discr_Expr (D) := Constr;
8895 end if;
8897 Next_Discriminant (Discr);
8898 Next (Constr);
8899 end loop;
8901 if No (Discr) and then Present (Constr) then
8902 Error_Msg_N ("too many discriminants given in constraint", Constr);
8903 return New_Elmt_List;
8904 end if;
8906 -- Named associations can be given in any order, but if both positional
8907 -- and named associations are used in the same discriminant constraint,
8908 -- then positional associations must occur first, at their normal
8909 -- position. Hence once a named association is used, the rest of the
8910 -- discriminant constraint must use only named associations.
8912 while Present (Constr) loop
8914 -- Positional association forbidden after a named association
8916 if Nkind (Constr) /= N_Discriminant_Association then
8917 Error_Msg_N ("positional association follows named one", Constr);
8918 return New_Elmt_List;
8920 -- Otherwise it is a named association
8922 else
8923 -- E records the type of the discriminants in the named
8924 -- association. All the discriminants specified in the same name
8925 -- association must have the same type.
8927 E := Empty;
8929 -- Search the list of discriminants in T to see if the simple name
8930 -- given in the constraint matches any of them.
8932 Id := First (Selector_Names (Constr));
8933 while Present (Id) loop
8934 Found := False;
8936 -- If Original_Discriminant is present, we are processing a
8937 -- generic instantiation and this is an instance node. We need
8938 -- to find the name of the corresponding discriminant in the
8939 -- actual record type T and not the name of the discriminant in
8940 -- the generic formal. Example:
8942 -- generic
8943 -- type G (D : int) is private;
8944 -- package P is
8945 -- subtype W is G (D => 1);
8946 -- end package;
8947 -- type Rec (X : int) is record ... end record;
8948 -- package Q is new P (G => Rec);
8950 -- At the point of the instantiation, formal type G is Rec
8951 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8952 -- which really looks like "subtype W is Rec (D => 1);" at
8953 -- the point of instantiation, we want to find the discriminant
8954 -- that corresponds to D in Rec, i.e. X.
8956 if Present (Original_Discriminant (Id))
8957 and then In_Instance
8958 then
8959 Discr := Find_Corresponding_Discriminant (Id, T);
8960 Found := True;
8962 else
8963 Discr := First_Discriminant (T);
8964 while Present (Discr) loop
8965 if Chars (Discr) = Chars (Id) then
8966 Found := True;
8967 exit;
8968 end if;
8970 Next_Discriminant (Discr);
8971 end loop;
8973 if not Found then
8974 Error_Msg_N ("& does not match any discriminant", Id);
8975 return New_Elmt_List;
8977 -- If the parent type is a generic formal, preserve the
8978 -- name of the discriminant for subsequent instances.
8979 -- see comment at the beginning of this if statement.
8981 elsif Is_Generic_Type (Root_Type (T)) then
8982 Set_Original_Discriminant (Id, Discr);
8983 end if;
8984 end if;
8986 Position := Pos_Of_Discr (T, Discr);
8988 if Present (Discr_Expr (Position)) then
8989 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8991 else
8992 -- Each discriminant specified in the same named association
8993 -- must be associated with a separate copy of the
8994 -- corresponding expression.
8996 if Present (Next (Id)) then
8997 Expr := New_Copy_Tree (Expression (Constr));
8998 Set_Parent (Expr, Parent (Expression (Constr)));
8999 else
9000 Expr := Expression (Constr);
9001 end if;
9003 Discr_Expr (Position) := Expr;
9004 Process_Discriminant_Expression (Expr, Discr);
9005 end if;
9007 -- A discriminant association with more than one discriminant
9008 -- name is only allowed if the named discriminants are all of
9009 -- the same type (RM 3.7.1(8)).
9011 if E = Empty then
9012 E := Base_Type (Etype (Discr));
9014 elsif Base_Type (Etype (Discr)) /= E then
9015 Error_Msg_N
9016 ("all discriminants in an association " &
9017 "must have the same type", Id);
9018 end if;
9020 Next (Id);
9021 end loop;
9022 end if;
9024 Next (Constr);
9025 end loop;
9027 -- A discriminant constraint must provide exactly one value for each
9028 -- discriminant of the type (RM 3.7.1(8)).
9030 for J in Discr_Expr'Range loop
9031 if No (Discr_Expr (J)) then
9032 Error_Msg_N ("too few discriminants given in constraint", C);
9033 return New_Elmt_List;
9034 end if;
9035 end loop;
9037 -- Determine if there are discriminant expressions in the constraint
9039 for J in Discr_Expr'Range loop
9040 if Denotes_Discriminant
9041 (Discr_Expr (J), Check_Concurrent => True)
9042 then
9043 Discrim_Present := True;
9044 end if;
9045 end loop;
9047 -- Build an element list consisting of the expressions given in the
9048 -- discriminant constraint and apply the appropriate checks. The list
9049 -- is constructed after resolving any named discriminant associations
9050 -- and therefore the expressions appear in the textual order of the
9051 -- discriminants.
9053 Discr := First_Discriminant (T);
9054 for J in Discr_Expr'Range loop
9055 if Discr_Expr (J) /= Error then
9056 Append_Elmt (Discr_Expr (J), Elist);
9058 -- If any of the discriminant constraints is given by a
9059 -- discriminant and we are in a derived type declaration we
9060 -- have a discriminant renaming. Establish link between new
9061 -- and old discriminant.
9063 if Denotes_Discriminant (Discr_Expr (J)) then
9064 if Derived_Def then
9065 Set_Corresponding_Discriminant
9066 (Entity (Discr_Expr (J)), Discr);
9067 end if;
9069 -- Force the evaluation of non-discriminant expressions.
9070 -- If we have found a discriminant in the constraint 3.4(26)
9071 -- and 3.8(18) demand that no range checks are performed are
9072 -- after evaluation. If the constraint is for a component
9073 -- definition that has a per-object constraint, expressions are
9074 -- evaluated but not checked either. In all other cases perform
9075 -- a range check.
9077 else
9078 if Discrim_Present then
9079 null;
9081 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9082 and then
9083 Has_Per_Object_Constraint
9084 (Defining_Identifier (Parent (Parent (Def))))
9085 then
9086 null;
9088 elsif Is_Access_Type (Etype (Discr)) then
9089 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9091 else
9092 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9093 end if;
9095 Force_Evaluation (Discr_Expr (J));
9096 end if;
9098 -- Check that the designated type of an access discriminant's
9099 -- expression is not a class-wide type unless the discriminant's
9100 -- designated type is also class-wide.
9102 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9103 and then not Is_Class_Wide_Type
9104 (Designated_Type (Etype (Discr)))
9105 and then Etype (Discr_Expr (J)) /= Any_Type
9106 and then Is_Class_Wide_Type
9107 (Designated_Type (Etype (Discr_Expr (J))))
9108 then
9109 Wrong_Type (Discr_Expr (J), Etype (Discr));
9111 elsif Is_Access_Type (Etype (Discr))
9112 and then not Is_Access_Constant (Etype (Discr))
9113 and then Is_Access_Type (Etype (Discr_Expr (J)))
9114 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9115 then
9116 Error_Msg_NE
9117 ("constraint for discriminant& must be access to variable",
9118 Def, Discr);
9119 end if;
9120 end if;
9122 Next_Discriminant (Discr);
9123 end loop;
9125 return Elist;
9126 end Build_Discriminant_Constraints;
9128 ---------------------------------
9129 -- Build_Discriminated_Subtype --
9130 ---------------------------------
9132 procedure Build_Discriminated_Subtype
9133 (T : Entity_Id;
9134 Def_Id : Entity_Id;
9135 Elist : Elist_Id;
9136 Related_Nod : Node_Id;
9137 For_Access : Boolean := False)
9139 Has_Discrs : constant Boolean := Has_Discriminants (T);
9140 Constrained : constant Boolean :=
9141 (Has_Discrs
9142 and then not Is_Empty_Elmt_List (Elist)
9143 and then not Is_Class_Wide_Type (T))
9144 or else Is_Constrained (T);
9146 begin
9147 if Ekind (T) = E_Record_Type then
9148 if For_Access then
9149 Set_Ekind (Def_Id, E_Private_Subtype);
9150 Set_Is_For_Access_Subtype (Def_Id, True);
9151 else
9152 Set_Ekind (Def_Id, E_Record_Subtype);
9153 end if;
9155 -- Inherit preelaboration flag from base, for types for which it
9156 -- may have been set: records, private types, protected types.
9158 Set_Known_To_Have_Preelab_Init
9159 (Def_Id, Known_To_Have_Preelab_Init (T));
9161 elsif Ekind (T) = E_Task_Type then
9162 Set_Ekind (Def_Id, E_Task_Subtype);
9164 elsif Ekind (T) = E_Protected_Type then
9165 Set_Ekind (Def_Id, E_Protected_Subtype);
9166 Set_Known_To_Have_Preelab_Init
9167 (Def_Id, Known_To_Have_Preelab_Init (T));
9169 elsif Is_Private_Type (T) then
9170 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9171 Set_Known_To_Have_Preelab_Init
9172 (Def_Id, Known_To_Have_Preelab_Init (T));
9174 -- Private subtypes may have private dependents
9176 Set_Private_Dependents (Def_Id, New_Elmt_List);
9178 elsif Is_Class_Wide_Type (T) then
9179 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9181 else
9182 -- Incomplete type. Attach subtype to list of dependents, to be
9183 -- completed with full view of parent type, unless is it the
9184 -- designated subtype of a record component within an init_proc.
9185 -- This last case arises for a component of an access type whose
9186 -- designated type is incomplete (e.g. a Taft Amendment type).
9187 -- The designated subtype is within an inner scope, and needs no
9188 -- elaboration, because only the access type is needed in the
9189 -- initialization procedure.
9191 Set_Ekind (Def_Id, Ekind (T));
9193 if For_Access and then Within_Init_Proc then
9194 null;
9195 else
9196 Append_Elmt (Def_Id, Private_Dependents (T));
9197 end if;
9198 end if;
9200 Set_Etype (Def_Id, T);
9201 Init_Size_Align (Def_Id);
9202 Set_Has_Discriminants (Def_Id, Has_Discrs);
9203 Set_Is_Constrained (Def_Id, Constrained);
9205 Set_First_Entity (Def_Id, First_Entity (T));
9206 Set_Last_Entity (Def_Id, Last_Entity (T));
9207 Set_Has_Implicit_Dereference
9208 (Def_Id, Has_Implicit_Dereference (T));
9210 -- If the subtype is the completion of a private declaration, there may
9211 -- have been representation clauses for the partial view, and they must
9212 -- be preserved. Build_Derived_Type chains the inherited clauses with
9213 -- the ones appearing on the extension. If this comes from a subtype
9214 -- declaration, all clauses are inherited.
9216 if No (First_Rep_Item (Def_Id)) then
9217 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9218 end if;
9220 if Is_Tagged_Type (T) then
9221 Set_Is_Tagged_Type (Def_Id);
9222 Make_Class_Wide_Type (Def_Id);
9223 end if;
9225 Set_Stored_Constraint (Def_Id, No_Elist);
9227 if Has_Discrs then
9228 Set_Discriminant_Constraint (Def_Id, Elist);
9229 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9230 end if;
9232 if Is_Tagged_Type (T) then
9234 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9235 -- concurrent record type (which has the list of primitive
9236 -- operations).
9238 if Ada_Version >= Ada_2005
9239 and then Is_Concurrent_Type (T)
9240 then
9241 Set_Corresponding_Record_Type (Def_Id,
9242 Corresponding_Record_Type (T));
9243 else
9244 Set_Direct_Primitive_Operations (Def_Id,
9245 Direct_Primitive_Operations (T));
9246 end if;
9248 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9249 end if;
9251 -- Subtypes introduced by component declarations do not need to be
9252 -- marked as delayed, and do not get freeze nodes, because the semantics
9253 -- verifies that the parents of the subtypes are frozen before the
9254 -- enclosing record is frozen.
9256 if not Is_Type (Scope (Def_Id)) then
9257 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9259 if Is_Private_Type (T)
9260 and then Present (Full_View (T))
9261 then
9262 Conditional_Delay (Def_Id, Full_View (T));
9263 else
9264 Conditional_Delay (Def_Id, T);
9265 end if;
9266 end if;
9268 if Is_Record_Type (T) then
9269 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9271 if Has_Discrs
9272 and then not Is_Empty_Elmt_List (Elist)
9273 and then not For_Access
9274 then
9275 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9276 elsif not For_Access then
9277 Set_Cloned_Subtype (Def_Id, T);
9278 end if;
9279 end if;
9280 end Build_Discriminated_Subtype;
9282 ---------------------------
9283 -- Build_Itype_Reference --
9284 ---------------------------
9286 procedure Build_Itype_Reference
9287 (Ityp : Entity_Id;
9288 Nod : Node_Id)
9290 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9291 begin
9293 -- Itype references are only created for use by the back-end
9295 if Inside_A_Generic then
9296 return;
9297 else
9298 Set_Itype (IR, Ityp);
9299 Insert_After (Nod, IR);
9300 end if;
9301 end Build_Itype_Reference;
9303 ------------------------
9304 -- Build_Scalar_Bound --
9305 ------------------------
9307 function Build_Scalar_Bound
9308 (Bound : Node_Id;
9309 Par_T : Entity_Id;
9310 Der_T : Entity_Id) return Node_Id
9312 New_Bound : Entity_Id;
9314 begin
9315 -- Note: not clear why this is needed, how can the original bound
9316 -- be unanalyzed at this point? and if it is, what business do we
9317 -- have messing around with it? and why is the base type of the
9318 -- parent type the right type for the resolution. It probably is
9319 -- not. It is OK for the new bound we are creating, but not for
9320 -- the old one??? Still if it never happens, no problem.
9322 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9324 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9325 New_Bound := New_Copy (Bound);
9326 Set_Etype (New_Bound, Der_T);
9327 Set_Analyzed (New_Bound);
9329 elsif Is_Entity_Name (Bound) then
9330 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9332 -- The following is almost certainly wrong. What business do we have
9333 -- relocating a node (Bound) that is presumably still attached to
9334 -- the tree elsewhere???
9336 else
9337 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9338 end if;
9340 Set_Etype (New_Bound, Der_T);
9341 return New_Bound;
9342 end Build_Scalar_Bound;
9344 --------------------------------
9345 -- Build_Underlying_Full_View --
9346 --------------------------------
9348 procedure Build_Underlying_Full_View
9349 (N : Node_Id;
9350 Typ : Entity_Id;
9351 Par : Entity_Id)
9353 Loc : constant Source_Ptr := Sloc (N);
9354 Subt : constant Entity_Id :=
9355 Make_Defining_Identifier
9356 (Loc, New_External_Name (Chars (Typ), 'S'));
9358 Constr : Node_Id;
9359 Indic : Node_Id;
9360 C : Node_Id;
9361 Id : Node_Id;
9363 procedure Set_Discriminant_Name (Id : Node_Id);
9364 -- If the derived type has discriminants, they may rename discriminants
9365 -- of the parent. When building the full view of the parent, we need to
9366 -- recover the names of the original discriminants if the constraint is
9367 -- given by named associations.
9369 ---------------------------
9370 -- Set_Discriminant_Name --
9371 ---------------------------
9373 procedure Set_Discriminant_Name (Id : Node_Id) is
9374 Disc : Entity_Id;
9376 begin
9377 Set_Original_Discriminant (Id, Empty);
9379 if Has_Discriminants (Typ) then
9380 Disc := First_Discriminant (Typ);
9381 while Present (Disc) loop
9382 if Chars (Disc) = Chars (Id)
9383 and then Present (Corresponding_Discriminant (Disc))
9384 then
9385 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9386 end if;
9387 Next_Discriminant (Disc);
9388 end loop;
9389 end if;
9390 end Set_Discriminant_Name;
9392 -- Start of processing for Build_Underlying_Full_View
9394 begin
9395 if Nkind (N) = N_Full_Type_Declaration then
9396 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9398 elsif Nkind (N) = N_Subtype_Declaration then
9399 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9401 elsif Nkind (N) = N_Component_Declaration then
9402 Constr :=
9403 New_Copy_Tree
9404 (Constraint (Subtype_Indication (Component_Definition (N))));
9406 else
9407 raise Program_Error;
9408 end if;
9410 C := First (Constraints (Constr));
9411 while Present (C) loop
9412 if Nkind (C) = N_Discriminant_Association then
9413 Id := First (Selector_Names (C));
9414 while Present (Id) loop
9415 Set_Discriminant_Name (Id);
9416 Next (Id);
9417 end loop;
9418 end if;
9420 Next (C);
9421 end loop;
9423 Indic :=
9424 Make_Subtype_Declaration (Loc,
9425 Defining_Identifier => Subt,
9426 Subtype_Indication =>
9427 Make_Subtype_Indication (Loc,
9428 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9429 Constraint => New_Copy_Tree (Constr)));
9431 -- If this is a component subtype for an outer itype, it is not
9432 -- a list member, so simply set the parent link for analysis: if
9433 -- the enclosing type does not need to be in a declarative list,
9434 -- neither do the components.
9436 if Is_List_Member (N)
9437 and then Nkind (N) /= N_Component_Declaration
9438 then
9439 Insert_Before (N, Indic);
9440 else
9441 Set_Parent (Indic, Parent (N));
9442 end if;
9444 Analyze (Indic);
9445 Set_Underlying_Full_View (Typ, Full_View (Subt));
9446 end Build_Underlying_Full_View;
9448 -------------------------------
9449 -- Check_Abstract_Overriding --
9450 -------------------------------
9452 procedure Check_Abstract_Overriding (T : Entity_Id) is
9453 Alias_Subp : Entity_Id;
9454 Elmt : Elmt_Id;
9455 Op_List : Elist_Id;
9456 Subp : Entity_Id;
9457 Type_Def : Node_Id;
9459 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9460 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9461 -- which has pragma Implemented already set. Check whether Subp's entity
9462 -- kind conforms to the implementation kind of the overridden routine.
9464 procedure Check_Pragma_Implemented
9465 (Subp : Entity_Id;
9466 Iface_Subp : Entity_Id);
9467 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9468 -- Iface_Subp and both entities have pragma Implemented already set on
9469 -- them. Check whether the two implementation kinds are conforming.
9471 procedure Inherit_Pragma_Implemented
9472 (Subp : Entity_Id;
9473 Iface_Subp : Entity_Id);
9474 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9475 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9476 -- Propagate the implementation kind of Iface_Subp to Subp.
9478 ------------------------------
9479 -- Check_Pragma_Implemented --
9480 ------------------------------
9482 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9483 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9484 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9485 Subp_Alias : constant Entity_Id := Alias (Subp);
9486 Contr_Typ : Entity_Id;
9487 Impl_Subp : Entity_Id;
9489 begin
9490 -- Subp must have an alias since it is a hidden entity used to link
9491 -- an interface subprogram to its overriding counterpart.
9493 pragma Assert (Present (Subp_Alias));
9495 -- Handle aliases to synchronized wrappers
9497 Impl_Subp := Subp_Alias;
9499 if Is_Primitive_Wrapper (Impl_Subp) then
9500 Impl_Subp := Wrapped_Entity (Impl_Subp);
9501 end if;
9503 -- Extract the type of the controlling formal
9505 Contr_Typ := Etype (First_Formal (Subp_Alias));
9507 if Is_Concurrent_Record_Type (Contr_Typ) then
9508 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9509 end if;
9511 -- An interface subprogram whose implementation kind is By_Entry must
9512 -- be implemented by an entry.
9514 if Impl_Kind = Name_By_Entry
9515 and then Ekind (Impl_Subp) /= E_Entry
9516 then
9517 Error_Msg_Node_2 := Iface_Alias;
9518 Error_Msg_NE
9519 ("type & must implement abstract subprogram & with an entry",
9520 Subp_Alias, Contr_Typ);
9522 elsif Impl_Kind = Name_By_Protected_Procedure then
9524 -- An interface subprogram whose implementation kind is By_
9525 -- Protected_Procedure cannot be implemented by a primitive
9526 -- procedure of a task type.
9528 if Ekind (Contr_Typ) /= E_Protected_Type then
9529 Error_Msg_Node_2 := Contr_Typ;
9530 Error_Msg_NE
9531 ("interface subprogram & cannot be implemented by a " &
9532 "primitive procedure of task type &", Subp_Alias,
9533 Iface_Alias);
9535 -- An interface subprogram whose implementation kind is By_
9536 -- Protected_Procedure must be implemented by a procedure.
9538 elsif Ekind (Impl_Subp) /= E_Procedure then
9539 Error_Msg_Node_2 := Iface_Alias;
9540 Error_Msg_NE
9541 ("type & must implement abstract subprogram & with a " &
9542 "procedure", Subp_Alias, Contr_Typ);
9544 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9545 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9546 then
9547 Error_Msg_Name_1 := Impl_Kind;
9548 Error_Msg_N
9549 ("overriding operation& must have synchronization%",
9550 Subp_Alias);
9551 end if;
9553 -- If primitive has Optional synchronization, overriding operation
9554 -- must match if it has an explicit synchronization..
9556 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9557 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9558 then
9559 Error_Msg_Name_1 := Impl_Kind;
9560 Error_Msg_N
9561 ("overriding operation& must have syncrhonization%",
9562 Subp_Alias);
9563 end if;
9564 end Check_Pragma_Implemented;
9566 ------------------------------
9567 -- Check_Pragma_Implemented --
9568 ------------------------------
9570 procedure Check_Pragma_Implemented
9571 (Subp : Entity_Id;
9572 Iface_Subp : Entity_Id)
9574 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9575 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9577 begin
9578 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9579 -- and overriding subprogram are different. In general this is an
9580 -- error except when the implementation kind of the overridden
9581 -- subprograms is By_Any or Optional.
9583 if Iface_Kind /= Subp_Kind
9584 and then Iface_Kind /= Name_By_Any
9585 and then Iface_Kind /= Name_Optional
9586 then
9587 if Iface_Kind = Name_By_Entry then
9588 Error_Msg_N
9589 ("incompatible implementation kind, overridden subprogram " &
9590 "is marked By_Entry", Subp);
9591 else
9592 Error_Msg_N
9593 ("incompatible implementation kind, overridden subprogram " &
9594 "is marked By_Protected_Procedure", Subp);
9595 end if;
9596 end if;
9597 end Check_Pragma_Implemented;
9599 --------------------------------
9600 -- Inherit_Pragma_Implemented --
9601 --------------------------------
9603 procedure Inherit_Pragma_Implemented
9604 (Subp : Entity_Id;
9605 Iface_Subp : Entity_Id)
9607 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9608 Loc : constant Source_Ptr := Sloc (Subp);
9609 Impl_Prag : Node_Id;
9611 begin
9612 -- Since the implementation kind is stored as a representation item
9613 -- rather than a flag, create a pragma node.
9615 Impl_Prag :=
9616 Make_Pragma (Loc,
9617 Chars => Name_Implemented,
9618 Pragma_Argument_Associations => New_List (
9619 Make_Pragma_Argument_Association (Loc,
9620 Expression => New_Occurrence_Of (Subp, Loc)),
9622 Make_Pragma_Argument_Association (Loc,
9623 Expression => Make_Identifier (Loc, Iface_Kind))));
9625 -- The pragma doesn't need to be analyzed because it is internally
9626 -- built. It is safe to directly register it as a rep item since we
9627 -- are only interested in the characters of the implementation kind.
9629 Record_Rep_Item (Subp, Impl_Prag);
9630 end Inherit_Pragma_Implemented;
9632 -- Start of processing for Check_Abstract_Overriding
9634 begin
9635 Op_List := Primitive_Operations (T);
9637 -- Loop to check primitive operations
9639 Elmt := First_Elmt (Op_List);
9640 while Present (Elmt) loop
9641 Subp := Node (Elmt);
9642 Alias_Subp := Alias (Subp);
9644 -- Inherited subprograms are identified by the fact that they do not
9645 -- come from source, and the associated source location is the
9646 -- location of the first subtype of the derived type.
9648 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9649 -- subprograms that "require overriding".
9651 -- Special exception, do not complain about failure to override the
9652 -- stream routines _Input and _Output, as well as the primitive
9653 -- operations used in dispatching selects since we always provide
9654 -- automatic overridings for these subprograms.
9656 -- Also ignore this rule for convention CIL since .NET libraries
9657 -- do bizarre things with interfaces???
9659 -- The partial view of T may have been a private extension, for
9660 -- which inherited functions dispatching on result are abstract.
9661 -- If the full view is a null extension, there is no need for
9662 -- overriding in Ada 2005, but wrappers need to be built for them
9663 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
9665 if Is_Null_Extension (T)
9666 and then Has_Controlling_Result (Subp)
9667 and then Ada_Version >= Ada_2005
9668 and then Present (Alias_Subp)
9669 and then not Comes_From_Source (Subp)
9670 and then not Is_Abstract_Subprogram (Alias_Subp)
9671 and then not Is_Access_Type (Etype (Subp))
9672 then
9673 null;
9675 -- Ada 2005 (AI-251): Internal entities of interfaces need no
9676 -- processing because this check is done with the aliased
9677 -- entity
9679 elsif Present (Interface_Alias (Subp)) then
9680 null;
9682 elsif (Is_Abstract_Subprogram (Subp)
9683 or else Requires_Overriding (Subp)
9684 or else
9685 (Has_Controlling_Result (Subp)
9686 and then Present (Alias_Subp)
9687 and then not Comes_From_Source (Subp)
9688 and then Sloc (Subp) = Sloc (First_Subtype (T))))
9689 and then not Is_TSS (Subp, TSS_Stream_Input)
9690 and then not Is_TSS (Subp, TSS_Stream_Output)
9691 and then not Is_Abstract_Type (T)
9692 and then Convention (T) /= Convention_CIL
9693 and then not Is_Predefined_Interface_Primitive (Subp)
9695 -- Ada 2005 (AI-251): Do not consider hidden entities associated
9696 -- with abstract interface types because the check will be done
9697 -- with the aliased entity (otherwise we generate a duplicated
9698 -- error message).
9700 and then not Present (Interface_Alias (Subp))
9701 then
9702 if Present (Alias_Subp) then
9704 -- Only perform the check for a derived subprogram when the
9705 -- type has an explicit record extension. This avoids incorrect
9706 -- flagging of abstract subprograms for the case of a type
9707 -- without an extension that is derived from a formal type
9708 -- with a tagged actual (can occur within a private part).
9710 -- Ada 2005 (AI-391): In the case of an inherited function with
9711 -- a controlling result of the type, the rule does not apply if
9712 -- the type is a null extension (unless the parent function
9713 -- itself is abstract, in which case the function must still be
9714 -- be overridden). The expander will generate an overriding
9715 -- wrapper function calling the parent subprogram (see
9716 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
9718 Type_Def := Type_Definition (Parent (T));
9720 if Nkind (Type_Def) = N_Derived_Type_Definition
9721 and then Present (Record_Extension_Part (Type_Def))
9722 and then
9723 (Ada_Version < Ada_2005
9724 or else not Is_Null_Extension (T)
9725 or else Ekind (Subp) = E_Procedure
9726 or else not Has_Controlling_Result (Subp)
9727 or else Is_Abstract_Subprogram (Alias_Subp)
9728 or else Requires_Overriding (Subp)
9729 or else Is_Access_Type (Etype (Subp)))
9730 then
9731 -- Avoid reporting error in case of abstract predefined
9732 -- primitive inherited from interface type because the
9733 -- body of internally generated predefined primitives
9734 -- of tagged types are generated later by Freeze_Type
9736 if Is_Interface (Root_Type (T))
9737 and then Is_Abstract_Subprogram (Subp)
9738 and then Is_Predefined_Dispatching_Operation (Subp)
9739 and then not Comes_From_Source (Ultimate_Alias (Subp))
9740 then
9741 null;
9743 else
9744 Error_Msg_NE
9745 ("type must be declared abstract or & overridden",
9746 T, Subp);
9748 -- Traverse the whole chain of aliased subprograms to
9749 -- complete the error notification. This is especially
9750 -- useful for traceability of the chain of entities when
9751 -- the subprogram corresponds with an interface
9752 -- subprogram (which may be defined in another package).
9754 if Present (Alias_Subp) then
9755 declare
9756 E : Entity_Id;
9758 begin
9759 E := Subp;
9760 while Present (Alias (E)) loop
9762 -- Avoid reporting redundant errors on entities
9763 -- inherited from interfaces
9765 if Sloc (E) /= Sloc (T) then
9766 Error_Msg_Sloc := Sloc (E);
9767 Error_Msg_NE
9768 ("\& has been inherited #", T, Subp);
9769 end if;
9771 E := Alias (E);
9772 end loop;
9774 Error_Msg_Sloc := Sloc (E);
9776 -- AI05-0068: report if there is an overriding
9777 -- non-abstract subprogram that is invisible.
9779 if Is_Hidden (E)
9780 and then not Is_Abstract_Subprogram (E)
9781 then
9782 Error_Msg_NE
9783 ("\& subprogram# is not visible",
9784 T, Subp);
9786 else
9787 Error_Msg_NE
9788 ("\& has been inherited from subprogram #",
9789 T, Subp);
9790 end if;
9791 end;
9792 end if;
9793 end if;
9795 -- Ada 2005 (AI-345): Protected or task type implementing
9796 -- abstract interfaces.
9798 elsif Is_Concurrent_Record_Type (T)
9799 and then Present (Interfaces (T))
9800 then
9801 -- If an inherited subprogram is implemented by a protected
9802 -- procedure or an entry, then the first parameter of the
9803 -- inherited subprogram shall be of mode OUT or IN OUT, or
9804 -- an access-to-variable parameter (RM 9.4(11.9/3))
9806 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
9807 and then Ekind (First_Formal (Subp)) = E_In_Parameter
9808 and then Ekind (Subp) /= E_Function
9809 and then not Is_Predefined_Dispatching_Operation (Subp)
9810 then
9811 Error_Msg_PT (T, Subp);
9813 -- Some other kind of overriding failure
9815 else
9816 Error_Msg_NE
9817 ("interface subprogram & must be overridden",
9818 T, Subp);
9820 -- Examine primitive operations of synchronized type,
9821 -- to find homonyms that have the wrong profile.
9823 declare
9824 Prim : Entity_Id;
9826 begin
9827 Prim :=
9828 First_Entity (Corresponding_Concurrent_Type (T));
9829 while Present (Prim) loop
9830 if Chars (Prim) = Chars (Subp) then
9831 Error_Msg_NE
9832 ("profile is not type conformant with "
9833 & "prefixed view profile of "
9834 & "inherited operation&", Prim, Subp);
9835 end if;
9837 Next_Entity (Prim);
9838 end loop;
9839 end;
9840 end if;
9841 end if;
9843 else
9844 Error_Msg_Node_2 := T;
9845 Error_Msg_N
9846 ("abstract subprogram& not allowed for type&", Subp);
9848 -- Also post unconditional warning on the type (unconditional
9849 -- so that if there are more than one of these cases, we get
9850 -- them all, and not just the first one).
9852 Error_Msg_Node_2 := Subp;
9853 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
9854 end if;
9855 end if;
9857 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
9859 -- Subp is an expander-generated procedure which maps an interface
9860 -- alias to a protected wrapper. The interface alias is flagged by
9861 -- pragma Implemented. Ensure that Subp is a procedure when the
9862 -- implementation kind is By_Protected_Procedure or an entry when
9863 -- By_Entry.
9865 if Ada_Version >= Ada_2012
9866 and then Is_Hidden (Subp)
9867 and then Present (Interface_Alias (Subp))
9868 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
9869 then
9870 Check_Pragma_Implemented (Subp);
9871 end if;
9873 -- Subp is an interface primitive which overrides another interface
9874 -- primitive marked with pragma Implemented.
9876 if Ada_Version >= Ada_2012
9877 and then Present (Overridden_Operation (Subp))
9878 and then Has_Rep_Pragma
9879 (Overridden_Operation (Subp), Name_Implemented)
9880 then
9881 -- If the overriding routine is also marked by Implemented, check
9882 -- that the two implementation kinds are conforming.
9884 if Has_Rep_Pragma (Subp, Name_Implemented) then
9885 Check_Pragma_Implemented
9886 (Subp => Subp,
9887 Iface_Subp => Overridden_Operation (Subp));
9889 -- Otherwise the overriding routine inherits the implementation
9890 -- kind from the overridden subprogram.
9892 else
9893 Inherit_Pragma_Implemented
9894 (Subp => Subp,
9895 Iface_Subp => Overridden_Operation (Subp));
9896 end if;
9897 end if;
9899 -- If the operation is a wrapper for a synchronized primitive, it
9900 -- may be called indirectly through a dispatching select. We assume
9901 -- that it will be referenced elsewhere indirectly, and suppress
9902 -- warnings about an unused entity.
9904 if Is_Primitive_Wrapper (Subp)
9905 and then Present (Wrapped_Entity (Subp))
9906 then
9907 Set_Referenced (Wrapped_Entity (Subp));
9908 end if;
9910 Next_Elmt (Elmt);
9911 end loop;
9912 end Check_Abstract_Overriding;
9914 ------------------------------------------------
9915 -- Check_Access_Discriminant_Requires_Limited --
9916 ------------------------------------------------
9918 procedure Check_Access_Discriminant_Requires_Limited
9919 (D : Node_Id;
9920 Loc : Node_Id)
9922 begin
9923 -- A discriminant_specification for an access discriminant shall appear
9924 -- only in the declaration for a task or protected type, or for a type
9925 -- with the reserved word 'limited' in its definition or in one of its
9926 -- ancestors (RM 3.7(10)).
9928 -- AI-0063: The proper condition is that type must be immutably limited,
9929 -- or else be a partial view.
9931 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
9932 if Is_Limited_View (Current_Scope)
9933 or else
9934 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
9935 and then Limited_Present (Parent (Current_Scope)))
9936 then
9937 null;
9939 else
9940 Error_Msg_N
9941 ("access discriminants allowed only for limited types", Loc);
9942 end if;
9943 end if;
9944 end Check_Access_Discriminant_Requires_Limited;
9946 -----------------------------------
9947 -- Check_Aliased_Component_Types --
9948 -----------------------------------
9950 procedure Check_Aliased_Component_Types (T : Entity_Id) is
9951 C : Entity_Id;
9953 begin
9954 -- ??? Also need to check components of record extensions, but not
9955 -- components of protected types (which are always limited).
9957 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
9958 -- types to be unconstrained. This is safe because it is illegal to
9959 -- create access subtypes to such types with explicit discriminant
9960 -- constraints.
9962 if not Is_Limited_Type (T) then
9963 if Ekind (T) = E_Record_Type then
9964 C := First_Component (T);
9965 while Present (C) loop
9966 if Is_Aliased (C)
9967 and then Has_Discriminants (Etype (C))
9968 and then not Is_Constrained (Etype (C))
9969 and then not In_Instance_Body
9970 and then Ada_Version < Ada_2005
9971 then
9972 Error_Msg_N
9973 ("aliased component must be constrained (RM 3.6(11))",
9975 end if;
9977 Next_Component (C);
9978 end loop;
9980 elsif Ekind (T) = E_Array_Type then
9981 if Has_Aliased_Components (T)
9982 and then Has_Discriminants (Component_Type (T))
9983 and then not Is_Constrained (Component_Type (T))
9984 and then not In_Instance_Body
9985 and then Ada_Version < Ada_2005
9986 then
9987 Error_Msg_N
9988 ("aliased component type must be constrained (RM 3.6(11))",
9990 end if;
9991 end if;
9992 end if;
9993 end Check_Aliased_Component_Types;
9995 ----------------------
9996 -- Check_Completion --
9997 ----------------------
9999 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10000 E : Entity_Id;
10002 procedure Post_Error;
10003 -- Post error message for lack of completion for entity E
10005 ----------------
10006 -- Post_Error --
10007 ----------------
10009 procedure Post_Error is
10011 procedure Missing_Body;
10012 -- Output missing body message
10014 ------------------
10015 -- Missing_Body --
10016 ------------------
10018 procedure Missing_Body is
10019 begin
10020 -- Spec is in same unit, so we can post on spec
10022 if In_Same_Source_Unit (Body_Id, E) then
10023 Error_Msg_N ("missing body for &", E);
10025 -- Spec is in a separate unit, so we have to post on the body
10027 else
10028 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10029 end if;
10030 end Missing_Body;
10032 -- Start of processing for Post_Error
10034 begin
10035 if not Comes_From_Source (E) then
10037 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10038 -- It may be an anonymous protected type created for a
10039 -- single variable. Post error on variable, if present.
10041 declare
10042 Var : Entity_Id;
10044 begin
10045 Var := First_Entity (Current_Scope);
10046 while Present (Var) loop
10047 exit when Etype (Var) = E
10048 and then Comes_From_Source (Var);
10050 Next_Entity (Var);
10051 end loop;
10053 if Present (Var) then
10054 E := Var;
10055 end if;
10056 end;
10057 end if;
10058 end if;
10060 -- If a generated entity has no completion, then either previous
10061 -- semantic errors have disabled the expansion phase, or else we had
10062 -- missing subunits, or else we are compiling without expansion,
10063 -- or else something is very wrong.
10065 if not Comes_From_Source (E) then
10066 pragma Assert
10067 (Serious_Errors_Detected > 0
10068 or else Configurable_Run_Time_Violations > 0
10069 or else Subunits_Missing
10070 or else not Expander_Active);
10071 return;
10073 -- Here for source entity
10075 else
10076 -- Here if no body to post the error message, so we post the error
10077 -- on the declaration that has no completion. This is not really
10078 -- the right place to post it, think about this later ???
10080 if No (Body_Id) then
10081 if Is_Type (E) then
10082 Error_Msg_NE
10083 ("missing full declaration for }", Parent (E), E);
10084 else
10085 Error_Msg_NE ("missing body for &", Parent (E), E);
10086 end if;
10088 -- Package body has no completion for a declaration that appears
10089 -- in the corresponding spec. Post error on the body, with a
10090 -- reference to the non-completed declaration.
10092 else
10093 Error_Msg_Sloc := Sloc (E);
10095 if Is_Type (E) then
10096 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10098 elsif Is_Overloadable (E)
10099 and then Current_Entity_In_Scope (E) /= E
10100 then
10101 -- It may be that the completion is mistyped and appears as
10102 -- a distinct overloading of the entity.
10104 declare
10105 Candidate : constant Entity_Id :=
10106 Current_Entity_In_Scope (E);
10107 Decl : constant Node_Id :=
10108 Unit_Declaration_Node (Candidate);
10110 begin
10111 if Is_Overloadable (Candidate)
10112 and then Ekind (Candidate) = Ekind (E)
10113 and then Nkind (Decl) = N_Subprogram_Body
10114 and then Acts_As_Spec (Decl)
10115 then
10116 Check_Type_Conformant (Candidate, E);
10118 else
10119 Missing_Body;
10120 end if;
10121 end;
10123 else
10124 Missing_Body;
10125 end if;
10126 end if;
10127 end if;
10128 end Post_Error;
10130 -- Start of processing for Check_Completion
10132 begin
10133 E := First_Entity (Current_Scope);
10134 while Present (E) loop
10135 if Is_Intrinsic_Subprogram (E) then
10136 null;
10138 -- The following situation requires special handling: a child unit
10139 -- that appears in the context clause of the body of its parent:
10141 -- procedure Parent.Child (...);
10143 -- with Parent.Child;
10144 -- package body Parent is
10146 -- Here Parent.Child appears as a local entity, but should not be
10147 -- flagged as requiring completion, because it is a compilation
10148 -- unit.
10150 -- Ignore missing completion for a subprogram that does not come from
10151 -- source (including the _Call primitive operation of RAS types,
10152 -- which has to have the flag Comes_From_Source for other purposes):
10153 -- we assume that the expander will provide the missing completion.
10154 -- In case of previous errors, other expansion actions that provide
10155 -- bodies for null procedures with not be invoked, so inhibit message
10156 -- in those cases.
10158 -- Note that E_Operator is not in the list that follows, because
10159 -- this kind is reserved for predefined operators, that are
10160 -- intrinsic and do not need completion.
10162 elsif Ekind (E) = E_Function
10163 or else Ekind (E) = E_Procedure
10164 or else Ekind (E) = E_Generic_Function
10165 or else Ekind (E) = E_Generic_Procedure
10166 then
10167 if Has_Completion (E) then
10168 null;
10170 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10171 null;
10173 elsif Is_Subprogram (E)
10174 and then (not Comes_From_Source (E)
10175 or else Chars (E) = Name_uCall)
10176 then
10177 null;
10179 elsif
10180 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10181 then
10182 null;
10184 elsif Nkind (Parent (E)) = N_Procedure_Specification
10185 and then Null_Present (Parent (E))
10186 and then Serious_Errors_Detected > 0
10187 then
10188 null;
10190 else
10191 Post_Error;
10192 end if;
10194 elsif Is_Entry (E) then
10195 if not Has_Completion (E) and then
10196 (Ekind (Scope (E)) = E_Protected_Object
10197 or else Ekind (Scope (E)) = E_Protected_Type)
10198 then
10199 Post_Error;
10200 end if;
10202 elsif Is_Package_Or_Generic_Package (E) then
10203 if Unit_Requires_Body (E) then
10204 if not Has_Completion (E)
10205 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
10206 N_Compilation_Unit
10207 then
10208 Post_Error;
10209 end if;
10211 elsif not Is_Child_Unit (E) then
10212 May_Need_Implicit_Body (E);
10213 end if;
10215 -- A formal incomplete type (Ada 2012) does not require a completion;
10216 -- other incomplete type declarations do.
10218 elsif Ekind (E) = E_Incomplete_Type
10219 and then No (Underlying_Type (E))
10220 and then not Is_Generic_Type (E)
10221 then
10222 Post_Error;
10224 elsif (Ekind (E) = E_Task_Type or else
10225 Ekind (E) = E_Protected_Type)
10226 and then not Has_Completion (E)
10227 then
10228 Post_Error;
10230 -- A single task declared in the current scope is a constant, verify
10231 -- that the body of its anonymous type is in the same scope. If the
10232 -- task is defined elsewhere, this may be a renaming declaration for
10233 -- which no completion is needed.
10235 elsif Ekind (E) = E_Constant
10236 and then Ekind (Etype (E)) = E_Task_Type
10237 and then not Has_Completion (Etype (E))
10238 and then Scope (Etype (E)) = Current_Scope
10239 then
10240 Post_Error;
10242 elsif Ekind (E) = E_Protected_Object
10243 and then not Has_Completion (Etype (E))
10244 then
10245 Post_Error;
10247 elsif Ekind (E) = E_Record_Type then
10248 if Is_Tagged_Type (E) then
10249 Check_Abstract_Overriding (E);
10250 Check_Conventions (E);
10251 end if;
10253 Check_Aliased_Component_Types (E);
10255 elsif Ekind (E) = E_Array_Type then
10256 Check_Aliased_Component_Types (E);
10258 end if;
10260 Next_Entity (E);
10261 end loop;
10262 end Check_Completion;
10264 ------------------------------------
10265 -- Check_CPP_Type_Has_No_Defaults --
10266 ------------------------------------
10268 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
10269 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
10270 Clist : Node_Id;
10271 Comp : Node_Id;
10273 begin
10274 -- Obtain the component list
10276 if Nkind (Tdef) = N_Record_Definition then
10277 Clist := Component_List (Tdef);
10278 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
10279 Clist := Component_List (Record_Extension_Part (Tdef));
10280 end if;
10282 -- Check all components to ensure no default expressions
10284 if Present (Clist) then
10285 Comp := First (Component_Items (Clist));
10286 while Present (Comp) loop
10287 if Present (Expression (Comp)) then
10288 Error_Msg_N
10289 ("component of imported 'C'P'P type cannot have "
10290 & "default expression", Expression (Comp));
10291 end if;
10293 Next (Comp);
10294 end loop;
10295 end if;
10296 end Check_CPP_Type_Has_No_Defaults;
10298 ----------------------------
10299 -- Check_Delta_Expression --
10300 ----------------------------
10302 procedure Check_Delta_Expression (E : Node_Id) is
10303 begin
10304 if not (Is_Real_Type (Etype (E))) then
10305 Wrong_Type (E, Any_Real);
10307 elsif not Is_OK_Static_Expression (E) then
10308 Flag_Non_Static_Expr
10309 ("non-static expression used for delta value!", E);
10311 elsif not UR_Is_Positive (Expr_Value_R (E)) then
10312 Error_Msg_N ("delta expression must be positive", E);
10314 else
10315 return;
10316 end if;
10318 -- If any of above errors occurred, then replace the incorrect
10319 -- expression by the real 0.1, which should prevent further errors.
10321 Rewrite (E,
10322 Make_Real_Literal (Sloc (E), Ureal_Tenth));
10323 Analyze_And_Resolve (E, Standard_Float);
10324 end Check_Delta_Expression;
10326 -----------------------------
10327 -- Check_Digits_Expression --
10328 -----------------------------
10330 procedure Check_Digits_Expression (E : Node_Id) is
10331 begin
10332 if not (Is_Integer_Type (Etype (E))) then
10333 Wrong_Type (E, Any_Integer);
10335 elsif not Is_OK_Static_Expression (E) then
10336 Flag_Non_Static_Expr
10337 ("non-static expression used for digits value!", E);
10339 elsif Expr_Value (E) <= 0 then
10340 Error_Msg_N ("digits value must be greater than zero", E);
10342 else
10343 return;
10344 end if;
10346 -- If any of above errors occurred, then replace the incorrect
10347 -- expression by the integer 1, which should prevent further errors.
10349 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
10350 Analyze_And_Resolve (E, Standard_Integer);
10352 end Check_Digits_Expression;
10354 --------------------------
10355 -- Check_Initialization --
10356 --------------------------
10358 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
10359 begin
10360 -- Special processing for limited types
10362 if Is_Limited_Type (T)
10363 and then not In_Instance
10364 and then not In_Inlined_Body
10365 then
10366 if not OK_For_Limited_Init (T, Exp) then
10368 -- In GNAT mode, this is just a warning, to allow it to be evilly
10369 -- turned off. Otherwise it is a real error.
10371 if GNAT_Mode then
10372 Error_Msg_N
10373 ("??cannot initialize entities of limited type!", Exp);
10375 elsif Ada_Version < Ada_2005 then
10377 -- The side effect removal machinery may generate illegal Ada
10378 -- code to avoid the usage of access types and 'reference in
10379 -- SPARK mode. Since this is legal code with respect to theorem
10380 -- proving, do not emit the error.
10382 if GNATprove_Mode
10383 and then Nkind (Exp) = N_Function_Call
10384 and then Nkind (Parent (Exp)) = N_Object_Declaration
10385 and then not Comes_From_Source
10386 (Defining_Identifier (Parent (Exp)))
10387 then
10388 null;
10390 else
10391 Error_Msg_N
10392 ("cannot initialize entities of limited type", Exp);
10393 Explain_Limited_Type (T, Exp);
10394 end if;
10396 else
10397 -- Specialize error message according to kind of illegal
10398 -- initial expression.
10400 if Nkind (Exp) = N_Type_Conversion
10401 and then Nkind (Expression (Exp)) = N_Function_Call
10402 then
10403 Error_Msg_N
10404 ("illegal context for call"
10405 & " to function with limited result", Exp);
10407 else
10408 Error_Msg_N
10409 ("initialization of limited object requires aggregate "
10410 & "or function call", Exp);
10411 end if;
10412 end if;
10413 end if;
10414 end if;
10416 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
10417 -- set unless we can be sure that no range check is required.
10419 if (GNATprove_Mode or not Expander_Active)
10420 and then Is_Scalar_Type (T)
10421 and then not Is_In_Range (Exp, T, Assume_Valid => True)
10422 then
10423 Set_Do_Range_Check (Exp);
10424 end if;
10425 end Check_Initialization;
10427 ----------------------
10428 -- Check_Interfaces --
10429 ----------------------
10431 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
10432 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
10434 Iface : Node_Id;
10435 Iface_Def : Node_Id;
10436 Iface_Typ : Entity_Id;
10437 Parent_Node : Node_Id;
10439 Is_Task : Boolean := False;
10440 -- Set True if parent type or any progenitor is a task interface
10442 Is_Protected : Boolean := False;
10443 -- Set True if parent type or any progenitor is a protected interface
10445 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
10446 -- Check that a progenitor is compatible with declaration.
10447 -- Error is posted on Error_Node.
10449 ------------------
10450 -- Check_Ifaces --
10451 ------------------
10453 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
10454 Iface_Id : constant Entity_Id :=
10455 Defining_Identifier (Parent (Iface_Def));
10456 Type_Def : Node_Id;
10458 begin
10459 if Nkind (N) = N_Private_Extension_Declaration then
10460 Type_Def := N;
10461 else
10462 Type_Def := Type_Definition (N);
10463 end if;
10465 if Is_Task_Interface (Iface_Id) then
10466 Is_Task := True;
10468 elsif Is_Protected_Interface (Iface_Id) then
10469 Is_Protected := True;
10470 end if;
10472 if Is_Synchronized_Interface (Iface_Id) then
10474 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
10475 -- extension derived from a synchronized interface must explicitly
10476 -- be declared synchronized, because the full view will be a
10477 -- synchronized type.
10479 if Nkind (N) = N_Private_Extension_Declaration then
10480 if not Synchronized_Present (N) then
10481 Error_Msg_NE
10482 ("private extension of& must be explicitly synchronized",
10483 N, Iface_Id);
10484 end if;
10486 -- However, by 3.9.4(16/2), a full type that is a record extension
10487 -- is never allowed to derive from a synchronized interface (note
10488 -- that interfaces must be excluded from this check, because those
10489 -- are represented by derived type definitions in some cases).
10491 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
10492 and then not Interface_Present (Type_Definition (N))
10493 then
10494 Error_Msg_N ("record extension cannot derive from synchronized"
10495 & " interface", Error_Node);
10496 end if;
10497 end if;
10499 -- Check that the characteristics of the progenitor are compatible
10500 -- with the explicit qualifier in the declaration.
10501 -- The check only applies to qualifiers that come from source.
10502 -- Limited_Present also appears in the declaration of corresponding
10503 -- records, and the check does not apply to them.
10505 if Limited_Present (Type_Def)
10506 and then not
10507 Is_Concurrent_Record_Type (Defining_Identifier (N))
10508 then
10509 if Is_Limited_Interface (Parent_Type)
10510 and then not Is_Limited_Interface (Iface_Id)
10511 then
10512 Error_Msg_NE
10513 ("progenitor& must be limited interface",
10514 Error_Node, Iface_Id);
10516 elsif
10517 (Task_Present (Iface_Def)
10518 or else Protected_Present (Iface_Def)
10519 or else Synchronized_Present (Iface_Def))
10520 and then Nkind (N) /= N_Private_Extension_Declaration
10521 and then not Error_Posted (N)
10522 then
10523 Error_Msg_NE
10524 ("progenitor& must be limited interface",
10525 Error_Node, Iface_Id);
10526 end if;
10528 -- Protected interfaces can only inherit from limited, synchronized
10529 -- or protected interfaces.
10531 elsif Nkind (N) = N_Full_Type_Declaration
10532 and then Protected_Present (Type_Def)
10533 then
10534 if Limited_Present (Iface_Def)
10535 or else Synchronized_Present (Iface_Def)
10536 or else Protected_Present (Iface_Def)
10537 then
10538 null;
10540 elsif Task_Present (Iface_Def) then
10541 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
10542 & " from task interface", Error_Node);
10544 else
10545 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
10546 & " from non-limited interface", Error_Node);
10547 end if;
10549 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
10550 -- limited and synchronized.
10552 elsif Synchronized_Present (Type_Def) then
10553 if Limited_Present (Iface_Def)
10554 or else Synchronized_Present (Iface_Def)
10555 then
10556 null;
10558 elsif Protected_Present (Iface_Def)
10559 and then Nkind (N) /= N_Private_Extension_Declaration
10560 then
10561 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10562 & " from protected interface", Error_Node);
10564 elsif Task_Present (Iface_Def)
10565 and then Nkind (N) /= N_Private_Extension_Declaration
10566 then
10567 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10568 & " from task interface", Error_Node);
10570 elsif not Is_Limited_Interface (Iface_Id) then
10571 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
10572 & " from non-limited interface", Error_Node);
10573 end if;
10575 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
10576 -- synchronized or task interfaces.
10578 elsif Nkind (N) = N_Full_Type_Declaration
10579 and then Task_Present (Type_Def)
10580 then
10581 if Limited_Present (Iface_Def)
10582 or else Synchronized_Present (Iface_Def)
10583 or else Task_Present (Iface_Def)
10584 then
10585 null;
10587 elsif Protected_Present (Iface_Def) then
10588 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
10589 & " protected interface", Error_Node);
10591 else
10592 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
10593 & " non-limited interface", Error_Node);
10594 end if;
10595 end if;
10596 end Check_Ifaces;
10598 -- Start of processing for Check_Interfaces
10600 begin
10601 if Is_Interface (Parent_Type) then
10602 if Is_Task_Interface (Parent_Type) then
10603 Is_Task := True;
10605 elsif Is_Protected_Interface (Parent_Type) then
10606 Is_Protected := True;
10607 end if;
10608 end if;
10610 if Nkind (N) = N_Private_Extension_Declaration then
10612 -- Check that progenitors are compatible with declaration
10614 Iface := First (Interface_List (Def));
10615 while Present (Iface) loop
10616 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
10618 Parent_Node := Parent (Base_Type (Iface_Typ));
10619 Iface_Def := Type_Definition (Parent_Node);
10621 if not Is_Interface (Iface_Typ) then
10622 Diagnose_Interface (Iface, Iface_Typ);
10624 else
10625 Check_Ifaces (Iface_Def, Iface);
10626 end if;
10628 Next (Iface);
10629 end loop;
10631 if Is_Task and Is_Protected then
10632 Error_Msg_N
10633 ("type cannot derive from task and protected interface", N);
10634 end if;
10636 return;
10637 end if;
10639 -- Full type declaration of derived type.
10640 -- Check compatibility with parent if it is interface type
10642 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
10643 and then Is_Interface (Parent_Type)
10644 then
10645 Parent_Node := Parent (Parent_Type);
10647 -- More detailed checks for interface varieties
10649 Check_Ifaces
10650 (Iface_Def => Type_Definition (Parent_Node),
10651 Error_Node => Subtype_Indication (Type_Definition (N)));
10652 end if;
10654 Iface := First (Interface_List (Def));
10655 while Present (Iface) loop
10656 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
10658 Parent_Node := Parent (Base_Type (Iface_Typ));
10659 Iface_Def := Type_Definition (Parent_Node);
10661 if not Is_Interface (Iface_Typ) then
10662 Diagnose_Interface (Iface, Iface_Typ);
10664 else
10665 -- "The declaration of a specific descendant of an interface
10666 -- type freezes the interface type" RM 13.14
10668 Freeze_Before (N, Iface_Typ);
10669 Check_Ifaces (Iface_Def, Error_Node => Iface);
10670 end if;
10672 Next (Iface);
10673 end loop;
10675 if Is_Task and Is_Protected then
10676 Error_Msg_N
10677 ("type cannot derive from task and protected interface", N);
10678 end if;
10679 end Check_Interfaces;
10681 ------------------------------------
10682 -- Check_Or_Process_Discriminants --
10683 ------------------------------------
10685 -- If an incomplete or private type declaration was already given for the
10686 -- type, the discriminants may have already been processed if they were
10687 -- present on the incomplete declaration. In this case a full conformance
10688 -- check has been performed in Find_Type_Name, and we then recheck here
10689 -- some properties that can't be checked on the partial view alone.
10690 -- Otherwise we call Process_Discriminants.
10692 procedure Check_Or_Process_Discriminants
10693 (N : Node_Id;
10694 T : Entity_Id;
10695 Prev : Entity_Id := Empty)
10697 begin
10698 if Has_Discriminants (T) then
10700 -- Discriminants are already set on T if they were already present
10701 -- on the partial view. Make them visible to component declarations.
10703 declare
10704 D : Entity_Id;
10705 -- Discriminant on T (full view) referencing expr on partial view
10707 Prev_D : Entity_Id;
10708 -- Entity of corresponding discriminant on partial view
10710 New_D : Node_Id;
10711 -- Discriminant specification for full view, expression is the
10712 -- syntactic copy on full view (which has been checked for
10713 -- conformance with partial view), only used here to post error
10714 -- message.
10716 begin
10717 D := First_Discriminant (T);
10718 New_D := First (Discriminant_Specifications (N));
10719 while Present (D) loop
10720 Prev_D := Current_Entity (D);
10721 Set_Current_Entity (D);
10722 Set_Is_Immediately_Visible (D);
10723 Set_Homonym (D, Prev_D);
10725 -- Handle the case where there is an untagged partial view and
10726 -- the full view is tagged: must disallow discriminants with
10727 -- defaults, unless compiling for Ada 2012, which allows a
10728 -- limited tagged type to have defaulted discriminants (see
10729 -- AI05-0214). However, suppress error here if it was already
10730 -- reported on the default expression of the partial view.
10732 if Is_Tagged_Type (T)
10733 and then Present (Expression (Parent (D)))
10734 and then (not Is_Limited_Type (Current_Scope)
10735 or else Ada_Version < Ada_2012)
10736 and then not Error_Posted (Expression (Parent (D)))
10737 then
10738 if Ada_Version >= Ada_2012 then
10739 Error_Msg_N
10740 ("discriminants of nonlimited tagged type cannot have"
10741 & " defaults",
10742 Expression (New_D));
10743 else
10744 Error_Msg_N
10745 ("discriminants of tagged type cannot have defaults",
10746 Expression (New_D));
10747 end if;
10748 end if;
10750 -- Ada 2005 (AI-230): Access discriminant allowed in
10751 -- non-limited record types.
10753 if Ada_Version < Ada_2005 then
10755 -- This restriction gets applied to the full type here. It
10756 -- has already been applied earlier to the partial view.
10758 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
10759 end if;
10761 Next_Discriminant (D);
10762 Next (New_D);
10763 end loop;
10764 end;
10766 elsif Present (Discriminant_Specifications (N)) then
10767 Process_Discriminants (N, Prev);
10768 end if;
10769 end Check_Or_Process_Discriminants;
10771 ----------------------
10772 -- Check_Real_Bound --
10773 ----------------------
10775 procedure Check_Real_Bound (Bound : Node_Id) is
10776 begin
10777 if not Is_Real_Type (Etype (Bound)) then
10778 Error_Msg_N
10779 ("bound in real type definition must be of real type", Bound);
10781 elsif not Is_OK_Static_Expression (Bound) then
10782 Flag_Non_Static_Expr
10783 ("non-static expression used for real type bound!", Bound);
10785 else
10786 return;
10787 end if;
10789 Rewrite
10790 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
10791 Analyze (Bound);
10792 Resolve (Bound, Standard_Float);
10793 end Check_Real_Bound;
10795 ------------------------------
10796 -- Complete_Private_Subtype --
10797 ------------------------------
10799 procedure Complete_Private_Subtype
10800 (Priv : Entity_Id;
10801 Full : Entity_Id;
10802 Full_Base : Entity_Id;
10803 Related_Nod : Node_Id)
10805 Save_Next_Entity : Entity_Id;
10806 Save_Homonym : Entity_Id;
10808 begin
10809 -- Set semantic attributes for (implicit) private subtype completion.
10810 -- If the full type has no discriminants, then it is a copy of the full
10811 -- view of the base. Otherwise, it is a subtype of the base with a
10812 -- possible discriminant constraint. Save and restore the original
10813 -- Next_Entity field of full to ensure that the calls to Copy_Node
10814 -- do not corrupt the entity chain.
10816 -- Note that the type of the full view is the same entity as the type of
10817 -- the partial view. In this fashion, the subtype has access to the
10818 -- correct view of the parent.
10820 Save_Next_Entity := Next_Entity (Full);
10821 Save_Homonym := Homonym (Priv);
10823 case Ekind (Full_Base) is
10824 when E_Record_Type |
10825 E_Record_Subtype |
10826 Class_Wide_Kind |
10827 Private_Kind |
10828 Task_Kind |
10829 Protected_Kind =>
10830 Copy_Node (Priv, Full);
10832 Set_Has_Discriminants
10833 (Full, Has_Discriminants (Full_Base));
10834 Set_Has_Unknown_Discriminants
10835 (Full, Has_Unknown_Discriminants (Full_Base));
10836 Set_First_Entity (Full, First_Entity (Full_Base));
10837 Set_Last_Entity (Full, Last_Entity (Full_Base));
10839 -- If the underlying base type is constrained, we know that the
10840 -- full view of the subtype is constrained as well (the converse
10841 -- is not necessarily true).
10843 if Is_Constrained (Full_Base) then
10844 Set_Is_Constrained (Full);
10845 end if;
10847 when others =>
10848 Copy_Node (Full_Base, Full);
10850 Set_Chars (Full, Chars (Priv));
10851 Conditional_Delay (Full, Priv);
10852 Set_Sloc (Full, Sloc (Priv));
10853 end case;
10855 Set_Next_Entity (Full, Save_Next_Entity);
10856 Set_Homonym (Full, Save_Homonym);
10857 Set_Associated_Node_For_Itype (Full, Related_Nod);
10859 -- Set common attributes for all subtypes: kind, convention, etc.
10861 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
10862 Set_Convention (Full, Convention (Full_Base));
10864 -- The Etype of the full view is inconsistent. Gigi needs to see the
10865 -- structural full view, which is what the current scheme gives:
10866 -- the Etype of the full view is the etype of the full base. However,
10867 -- if the full base is a derived type, the full view then looks like
10868 -- a subtype of the parent, not a subtype of the full base. If instead
10869 -- we write:
10871 -- Set_Etype (Full, Full_Base);
10873 -- then we get inconsistencies in the front-end (confusion between
10874 -- views). Several outstanding bugs are related to this ???
10876 Set_Is_First_Subtype (Full, False);
10877 Set_Scope (Full, Scope (Priv));
10878 Set_Size_Info (Full, Full_Base);
10879 Set_RM_Size (Full, RM_Size (Full_Base));
10880 Set_Is_Itype (Full);
10882 -- A subtype of a private-type-without-discriminants, whose full-view
10883 -- has discriminants with default expressions, is not constrained.
10885 if not Has_Discriminants (Priv) then
10886 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
10888 if Has_Discriminants (Full_Base) then
10889 Set_Discriminant_Constraint
10890 (Full, Discriminant_Constraint (Full_Base));
10892 -- The partial view may have been indefinite, the full view
10893 -- might not be.
10895 Set_Has_Unknown_Discriminants
10896 (Full, Has_Unknown_Discriminants (Full_Base));
10897 end if;
10898 end if;
10900 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
10901 Set_Depends_On_Private (Full, Has_Private_Component (Full));
10903 -- Freeze the private subtype entity if its parent is delayed, and not
10904 -- already frozen. We skip this processing if the type is an anonymous
10905 -- subtype of a record component, or is the corresponding record of a
10906 -- protected type, since ???
10908 if not Is_Type (Scope (Full)) then
10909 Set_Has_Delayed_Freeze (Full,
10910 Has_Delayed_Freeze (Full_Base)
10911 and then (not Is_Frozen (Full_Base)));
10912 end if;
10914 Set_Freeze_Node (Full, Empty);
10915 Set_Is_Frozen (Full, False);
10916 Set_Full_View (Priv, Full);
10918 if Has_Discriminants (Full) then
10919 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
10920 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
10922 if Has_Unknown_Discriminants (Full) then
10923 Set_Discriminant_Constraint (Full, No_Elist);
10924 end if;
10925 end if;
10927 if Ekind (Full_Base) = E_Record_Type
10928 and then Has_Discriminants (Full_Base)
10929 and then Has_Discriminants (Priv) -- might not, if errors
10930 and then not Has_Unknown_Discriminants (Priv)
10931 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
10932 then
10933 Create_Constrained_Components
10934 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
10936 -- If the full base is itself derived from private, build a congruent
10937 -- subtype of its underlying type, for use by the back end. For a
10938 -- constrained record component, the declaration cannot be placed on
10939 -- the component list, but it must nevertheless be built an analyzed, to
10940 -- supply enough information for Gigi to compute the size of component.
10942 elsif Ekind (Full_Base) in Private_Kind
10943 and then Is_Derived_Type (Full_Base)
10944 and then Has_Discriminants (Full_Base)
10945 and then (Ekind (Current_Scope) /= E_Record_Subtype)
10946 then
10947 if not Is_Itype (Priv)
10948 and then
10949 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
10950 then
10951 Build_Underlying_Full_View
10952 (Parent (Priv), Full, Etype (Full_Base));
10954 elsif Nkind (Related_Nod) = N_Component_Declaration then
10955 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
10956 end if;
10958 elsif Is_Record_Type (Full_Base) then
10960 -- Show Full is simply a renaming of Full_Base
10962 Set_Cloned_Subtype (Full, Full_Base);
10963 end if;
10965 -- It is unsafe to share the bounds of a scalar type, because the Itype
10966 -- is elaborated on demand, and if a bound is non-static then different
10967 -- orders of elaboration in different units will lead to different
10968 -- external symbols.
10970 if Is_Scalar_Type (Full_Base) then
10971 Set_Scalar_Range (Full,
10972 Make_Range (Sloc (Related_Nod),
10973 Low_Bound =>
10974 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
10975 High_Bound =>
10976 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
10978 -- This completion inherits the bounds of the full parent, but if
10979 -- the parent is an unconstrained floating point type, so is the
10980 -- completion.
10982 if Is_Floating_Point_Type (Full_Base) then
10983 Set_Includes_Infinities
10984 (Scalar_Range (Full), Has_Infinities (Full_Base));
10985 end if;
10986 end if;
10988 -- ??? It seems that a lot of fields are missing that should be copied
10989 -- from Full_Base to Full. Here are some that are introduced in a
10990 -- non-disruptive way but a cleanup is necessary.
10992 if Is_Tagged_Type (Full_Base) then
10993 Set_Is_Tagged_Type (Full);
10994 Set_Direct_Primitive_Operations (Full,
10995 Direct_Primitive_Operations (Full_Base));
10997 -- Inherit class_wide type of full_base in case the partial view was
10998 -- not tagged. Otherwise it has already been created when the private
10999 -- subtype was analyzed.
11001 if No (Class_Wide_Type (Full)) then
11002 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11003 end if;
11005 -- If this is a subtype of a protected or task type, constrain its
11006 -- corresponding record, unless this is a subtype without constraints,
11007 -- i.e. a simple renaming as with an actual subtype in an instance.
11009 elsif Is_Concurrent_Type (Full_Base) then
11010 if Has_Discriminants (Full)
11011 and then Present (Corresponding_Record_Type (Full_Base))
11012 and then
11013 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11014 then
11015 Set_Corresponding_Record_Type (Full,
11016 Constrain_Corresponding_Record
11017 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11019 else
11020 Set_Corresponding_Record_Type (Full,
11021 Corresponding_Record_Type (Full_Base));
11022 end if;
11023 end if;
11025 -- Link rep item chain, and also setting of Has_Predicates from private
11026 -- subtype to full subtype, since we will need these on the full subtype
11027 -- to create the predicate function. Note that the full subtype may
11028 -- already have rep items, inherited from the full view of the base
11029 -- type, so we must be sure not to overwrite these entries.
11031 declare
11032 Append : Boolean;
11033 Item : Node_Id;
11034 Next_Item : Node_Id;
11036 begin
11037 Item := First_Rep_Item (Full);
11039 -- If no existing rep items on full type, we can just link directly
11040 -- to the list of items on the private type.
11042 if No (Item) then
11043 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11045 -- Otherwise, search to the end of items currently linked to the full
11046 -- subtype and append the private items to the end. However, if Priv
11047 -- and Full already have the same list of rep items, then the append
11048 -- is not done, as that would create a circularity.
11050 elsif Item /= First_Rep_Item (Priv) then
11051 Append := True;
11053 loop
11054 Next_Item := Next_Rep_Item (Item);
11055 exit when No (Next_Item);
11056 Item := Next_Item;
11058 -- If the private view has aspect specifications, the full view
11059 -- inherits them. Since these aspects may already have been
11060 -- attached to the full view during derivation, do not append
11061 -- them if already present.
11063 if Item = First_Rep_Item (Priv) then
11064 Append := False;
11065 exit;
11066 end if;
11067 end loop;
11069 -- And link the private type items at the end of the chain
11071 if Append then
11072 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11073 end if;
11074 end if;
11075 end;
11077 -- Make sure Has_Predicates is set on full type if it is set on the
11078 -- private type. Note that it may already be set on the full type and
11079 -- if so, we don't want to unset it.
11081 if Has_Predicates (Priv) then
11082 Set_Has_Predicates (Full);
11083 end if;
11084 end Complete_Private_Subtype;
11086 ----------------------------
11087 -- Constant_Redeclaration --
11088 ----------------------------
11090 procedure Constant_Redeclaration
11091 (Id : Entity_Id;
11092 N : Node_Id;
11093 T : out Entity_Id)
11095 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11096 Obj_Def : constant Node_Id := Object_Definition (N);
11097 New_T : Entity_Id;
11099 procedure Check_Possible_Deferred_Completion
11100 (Prev_Id : Entity_Id;
11101 Prev_Obj_Def : Node_Id;
11102 Curr_Obj_Def : Node_Id);
11103 -- Determine whether the two object definitions describe the partial
11104 -- and the full view of a constrained deferred constant. Generate
11105 -- a subtype for the full view and verify that it statically matches
11106 -- the subtype of the partial view.
11108 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11109 -- If deferred constant is an access type initialized with an allocator,
11110 -- check whether there is an illegal recursion in the definition,
11111 -- through a default value of some record subcomponent. This is normally
11112 -- detected when generating init procs, but requires this additional
11113 -- mechanism when expansion is disabled.
11115 ----------------------------------------
11116 -- Check_Possible_Deferred_Completion --
11117 ----------------------------------------
11119 procedure Check_Possible_Deferred_Completion
11120 (Prev_Id : Entity_Id;
11121 Prev_Obj_Def : Node_Id;
11122 Curr_Obj_Def : Node_Id)
11124 begin
11125 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11126 and then Present (Constraint (Prev_Obj_Def))
11127 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11128 and then Present (Constraint (Curr_Obj_Def))
11129 then
11130 declare
11131 Loc : constant Source_Ptr := Sloc (N);
11132 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11133 Decl : constant Node_Id :=
11134 Make_Subtype_Declaration (Loc,
11135 Defining_Identifier => Def_Id,
11136 Subtype_Indication =>
11137 Relocate_Node (Curr_Obj_Def));
11139 begin
11140 Insert_Before_And_Analyze (N, Decl);
11141 Set_Etype (Id, Def_Id);
11143 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11144 Error_Msg_Sloc := Sloc (Prev_Id);
11145 Error_Msg_N ("subtype does not statically match deferred " &
11146 "declaration#", N);
11147 end if;
11148 end;
11149 end if;
11150 end Check_Possible_Deferred_Completion;
11152 ---------------------------------
11153 -- Check_Recursive_Declaration --
11154 ---------------------------------
11156 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11157 Comp : Entity_Id;
11159 begin
11160 if Is_Record_Type (Typ) then
11161 Comp := First_Component (Typ);
11162 while Present (Comp) loop
11163 if Comes_From_Source (Comp) then
11164 if Present (Expression (Parent (Comp)))
11165 and then Is_Entity_Name (Expression (Parent (Comp)))
11166 and then Entity (Expression (Parent (Comp))) = Prev
11167 then
11168 Error_Msg_Sloc := Sloc (Parent (Comp));
11169 Error_Msg_NE
11170 ("illegal circularity with declaration for&#",
11171 N, Comp);
11172 return;
11174 elsif Is_Record_Type (Etype (Comp)) then
11175 Check_Recursive_Declaration (Etype (Comp));
11176 end if;
11177 end if;
11179 Next_Component (Comp);
11180 end loop;
11181 end if;
11182 end Check_Recursive_Declaration;
11184 -- Start of processing for Constant_Redeclaration
11186 begin
11187 if Nkind (Parent (Prev)) = N_Object_Declaration then
11188 if Nkind (Object_Definition
11189 (Parent (Prev))) = N_Subtype_Indication
11190 then
11191 -- Find type of new declaration. The constraints of the two
11192 -- views must match statically, but there is no point in
11193 -- creating an itype for the full view.
11195 if Nkind (Obj_Def) = N_Subtype_Indication then
11196 Find_Type (Subtype_Mark (Obj_Def));
11197 New_T := Entity (Subtype_Mark (Obj_Def));
11199 else
11200 Find_Type (Obj_Def);
11201 New_T := Entity (Obj_Def);
11202 end if;
11204 T := Etype (Prev);
11206 else
11207 -- The full view may impose a constraint, even if the partial
11208 -- view does not, so construct the subtype.
11210 New_T := Find_Type_Of_Object (Obj_Def, N);
11211 T := New_T;
11212 end if;
11214 else
11215 -- Current declaration is illegal, diagnosed below in Enter_Name
11217 T := Empty;
11218 New_T := Any_Type;
11219 end if;
11221 -- If previous full declaration or a renaming declaration exists, or if
11222 -- a homograph is present, let Enter_Name handle it, either with an
11223 -- error or with the removal of an overridden implicit subprogram.
11224 -- The previous one is a full declaration if it has an expression
11225 -- (which in the case of an aggregate is indicated by the Init flag).
11227 if Ekind (Prev) /= E_Constant
11228 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
11229 or else Present (Expression (Parent (Prev)))
11230 or else Has_Init_Expression (Parent (Prev))
11231 or else Present (Full_View (Prev))
11232 then
11233 Enter_Name (Id);
11235 -- Verify that types of both declarations match, or else that both types
11236 -- are anonymous access types whose designated subtypes statically match
11237 -- (as allowed in Ada 2005 by AI-385).
11239 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
11240 and then
11241 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
11242 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
11243 or else Is_Access_Constant (Etype (New_T)) /=
11244 Is_Access_Constant (Etype (Prev))
11245 or else Can_Never_Be_Null (Etype (New_T)) /=
11246 Can_Never_Be_Null (Etype (Prev))
11247 or else Null_Exclusion_Present (Parent (Prev)) /=
11248 Null_Exclusion_Present (Parent (Id))
11249 or else not Subtypes_Statically_Match
11250 (Designated_Type (Etype (Prev)),
11251 Designated_Type (Etype (New_T))))
11252 then
11253 Error_Msg_Sloc := Sloc (Prev);
11254 Error_Msg_N ("type does not match declaration#", N);
11255 Set_Full_View (Prev, Id);
11256 Set_Etype (Id, Any_Type);
11258 elsif
11259 Null_Exclusion_Present (Parent (Prev))
11260 and then not Null_Exclusion_Present (N)
11261 then
11262 Error_Msg_Sloc := Sloc (Prev);
11263 Error_Msg_N ("null-exclusion does not match declaration#", N);
11264 Set_Full_View (Prev, Id);
11265 Set_Etype (Id, Any_Type);
11267 -- If so, process the full constant declaration
11269 else
11270 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
11271 -- the deferred declaration is constrained, then the subtype defined
11272 -- by the subtype_indication in the full declaration shall match it
11273 -- statically.
11275 Check_Possible_Deferred_Completion
11276 (Prev_Id => Prev,
11277 Prev_Obj_Def => Object_Definition (Parent (Prev)),
11278 Curr_Obj_Def => Obj_Def);
11280 Set_Full_View (Prev, Id);
11281 Set_Is_Public (Id, Is_Public (Prev));
11282 Set_Is_Internal (Id);
11283 Append_Entity (Id, Current_Scope);
11285 -- Check ALIASED present if present before (RM 7.4(7))
11287 if Is_Aliased (Prev)
11288 and then not Aliased_Present (N)
11289 then
11290 Error_Msg_Sloc := Sloc (Prev);
11291 Error_Msg_N ("ALIASED required (see declaration#)", N);
11292 end if;
11294 -- Check that placement is in private part and that the incomplete
11295 -- declaration appeared in the visible part.
11297 if Ekind (Current_Scope) = E_Package
11298 and then not In_Private_Part (Current_Scope)
11299 then
11300 Error_Msg_Sloc := Sloc (Prev);
11301 Error_Msg_N
11302 ("full constant for declaration#"
11303 & " must be in private part", N);
11305 elsif Ekind (Current_Scope) = E_Package
11306 and then
11307 List_Containing (Parent (Prev)) /=
11308 Visible_Declarations (Package_Specification (Current_Scope))
11309 then
11310 Error_Msg_N
11311 ("deferred constant must be declared in visible part",
11312 Parent (Prev));
11313 end if;
11315 if Is_Access_Type (T)
11316 and then Nkind (Expression (N)) = N_Allocator
11317 then
11318 Check_Recursive_Declaration (Designated_Type (T));
11319 end if;
11321 -- A deferred constant is a visible entity. If type has invariants,
11322 -- verify that the initial value satisfies them.
11324 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
11325 Insert_After (N,
11326 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
11327 end if;
11328 end if;
11329 end Constant_Redeclaration;
11331 ----------------------
11332 -- Constrain_Access --
11333 ----------------------
11335 procedure Constrain_Access
11336 (Def_Id : in out Entity_Id;
11337 S : Node_Id;
11338 Related_Nod : Node_Id)
11340 T : constant Entity_Id := Entity (Subtype_Mark (S));
11341 Desig_Type : constant Entity_Id := Designated_Type (T);
11342 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
11343 Constraint_OK : Boolean := True;
11345 begin
11346 if Is_Array_Type (Desig_Type) then
11347 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
11349 elsif (Is_Record_Type (Desig_Type)
11350 or else Is_Incomplete_Or_Private_Type (Desig_Type))
11351 and then not Is_Constrained (Desig_Type)
11352 then
11353 -- ??? The following code is a temporary bypass to ignore a
11354 -- discriminant constraint on access type if it is constraining
11355 -- the current record. Avoid creating the implicit subtype of the
11356 -- record we are currently compiling since right now, we cannot
11357 -- handle these. For now, just return the access type itself.
11359 if Desig_Type = Current_Scope
11360 and then No (Def_Id)
11361 then
11362 Set_Ekind (Desig_Subtype, E_Record_Subtype);
11363 Def_Id := Entity (Subtype_Mark (S));
11365 -- This call added to ensure that the constraint is analyzed
11366 -- (needed for a B test). Note that we still return early from
11367 -- this procedure to avoid recursive processing. ???
11369 Constrain_Discriminated_Type
11370 (Desig_Subtype, S, Related_Nod, For_Access => True);
11371 return;
11372 end if;
11374 -- Enforce rule that the constraint is illegal if there is an
11375 -- unconstrained view of the designated type. This means that the
11376 -- partial view (either a private type declaration or a derivation
11377 -- from a private type) has no discriminants. (Defect Report
11378 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
11380 -- Rule updated for Ada 2005: The private type is said to have
11381 -- a constrained partial view, given that objects of the type
11382 -- can be declared. Furthermore, the rule applies to all access
11383 -- types, unlike the rule concerning default discriminants (see
11384 -- RM 3.7.1(7/3))
11386 if (Ekind (T) = E_General_Access_Type
11387 or else Ada_Version >= Ada_2005)
11388 and then Has_Private_Declaration (Desig_Type)
11389 and then In_Open_Scopes (Scope (Desig_Type))
11390 and then Has_Discriminants (Desig_Type)
11391 then
11392 declare
11393 Pack : constant Node_Id :=
11394 Unit_Declaration_Node (Scope (Desig_Type));
11395 Decls : List_Id;
11396 Decl : Node_Id;
11398 begin
11399 if Nkind (Pack) = N_Package_Declaration then
11400 Decls := Visible_Declarations (Specification (Pack));
11401 Decl := First (Decls);
11402 while Present (Decl) loop
11403 if (Nkind (Decl) = N_Private_Type_Declaration
11404 and then
11405 Chars (Defining_Identifier (Decl)) =
11406 Chars (Desig_Type))
11408 or else
11409 (Nkind (Decl) = N_Full_Type_Declaration
11410 and then
11411 Chars (Defining_Identifier (Decl)) =
11412 Chars (Desig_Type)
11413 and then Is_Derived_Type (Desig_Type)
11414 and then
11415 Has_Private_Declaration (Etype (Desig_Type)))
11416 then
11417 if No (Discriminant_Specifications (Decl)) then
11418 Error_Msg_N
11419 ("cannot constrain access type if designated " &
11420 "type has constrained partial view", S);
11421 end if;
11423 exit;
11424 end if;
11426 Next (Decl);
11427 end loop;
11428 end if;
11429 end;
11430 end if;
11432 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
11433 For_Access => True);
11435 elsif (Is_Task_Type (Desig_Type)
11436 or else Is_Protected_Type (Desig_Type))
11437 and then not Is_Constrained (Desig_Type)
11438 then
11439 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
11441 else
11442 Error_Msg_N ("invalid constraint on access type", S);
11443 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
11444 Constraint_OK := False;
11445 end if;
11447 if No (Def_Id) then
11448 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
11449 else
11450 Set_Ekind (Def_Id, E_Access_Subtype);
11451 end if;
11453 if Constraint_OK then
11454 Set_Etype (Def_Id, Base_Type (T));
11456 if Is_Private_Type (Desig_Type) then
11457 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
11458 end if;
11459 else
11460 Set_Etype (Def_Id, Any_Type);
11461 end if;
11463 Set_Size_Info (Def_Id, T);
11464 Set_Is_Constrained (Def_Id, Constraint_OK);
11465 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
11466 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11467 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
11469 Conditional_Delay (Def_Id, T);
11471 -- AI-363 : Subtypes of general access types whose designated types have
11472 -- default discriminants are disallowed. In instances, the rule has to
11473 -- be checked against the actual, of which T is the subtype. In a
11474 -- generic body, the rule is checked assuming that the actual type has
11475 -- defaulted discriminants.
11477 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
11478 if Ekind (Base_Type (T)) = E_General_Access_Type
11479 and then Has_Defaulted_Discriminants (Desig_Type)
11480 then
11481 if Ada_Version < Ada_2005 then
11482 Error_Msg_N
11483 ("access subtype of general access type would not " &
11484 "be allowed in Ada 2005?y?", S);
11485 else
11486 Error_Msg_N
11487 ("access subtype of general access type not allowed", S);
11488 end if;
11490 Error_Msg_N ("\discriminants have defaults", S);
11492 elsif Is_Access_Type (T)
11493 and then Is_Generic_Type (Desig_Type)
11494 and then Has_Discriminants (Desig_Type)
11495 and then In_Package_Body (Current_Scope)
11496 then
11497 if Ada_Version < Ada_2005 then
11498 Error_Msg_N
11499 ("access subtype would not be allowed in generic body " &
11500 "in Ada 2005?y?", S);
11501 else
11502 Error_Msg_N
11503 ("access subtype not allowed in generic body", S);
11504 end if;
11506 Error_Msg_N
11507 ("\designated type is a discriminated formal", S);
11508 end if;
11509 end if;
11510 end Constrain_Access;
11512 ---------------------
11513 -- Constrain_Array --
11514 ---------------------
11516 procedure Constrain_Array
11517 (Def_Id : in out Entity_Id;
11518 SI : Node_Id;
11519 Related_Nod : Node_Id;
11520 Related_Id : Entity_Id;
11521 Suffix : Character)
11523 C : constant Node_Id := Constraint (SI);
11524 Number_Of_Constraints : Nat := 0;
11525 Index : Node_Id;
11526 S, T : Entity_Id;
11527 Constraint_OK : Boolean := True;
11529 begin
11530 T := Entity (Subtype_Mark (SI));
11532 if Is_Access_Type (T) then
11533 T := Designated_Type (T);
11534 end if;
11536 -- If an index constraint follows a subtype mark in a subtype indication
11537 -- then the type or subtype denoted by the subtype mark must not already
11538 -- impose an index constraint. The subtype mark must denote either an
11539 -- unconstrained array type or an access type whose designated type
11540 -- is such an array type... (RM 3.6.1)
11542 if Is_Constrained (T) then
11543 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
11544 Constraint_OK := False;
11546 else
11547 S := First (Constraints (C));
11548 while Present (S) loop
11549 Number_Of_Constraints := Number_Of_Constraints + 1;
11550 Next (S);
11551 end loop;
11553 -- In either case, the index constraint must provide a discrete
11554 -- range for each index of the array type and the type of each
11555 -- discrete range must be the same as that of the corresponding
11556 -- index. (RM 3.6.1)
11558 if Number_Of_Constraints /= Number_Dimensions (T) then
11559 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
11560 Constraint_OK := False;
11562 else
11563 S := First (Constraints (C));
11564 Index := First_Index (T);
11565 Analyze (Index);
11567 -- Apply constraints to each index type
11569 for J in 1 .. Number_Of_Constraints loop
11570 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
11571 Next (Index);
11572 Next (S);
11573 end loop;
11575 end if;
11576 end if;
11578 if No (Def_Id) then
11579 Def_Id :=
11580 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
11581 Set_Parent (Def_Id, Related_Nod);
11583 else
11584 Set_Ekind (Def_Id, E_Array_Subtype);
11585 end if;
11587 Set_Size_Info (Def_Id, (T));
11588 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11589 Set_Etype (Def_Id, Base_Type (T));
11591 if Constraint_OK then
11592 Set_First_Index (Def_Id, First (Constraints (C)));
11593 else
11594 Set_First_Index (Def_Id, First_Index (T));
11595 end if;
11597 Set_Is_Constrained (Def_Id, True);
11598 Set_Is_Aliased (Def_Id, Is_Aliased (T));
11599 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11601 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
11602 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
11604 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
11605 -- We need to initialize the attribute because if Def_Id is previously
11606 -- analyzed through a limited_with clause, it will have the attributes
11607 -- of an incomplete type, one of which is an Elist that overlaps the
11608 -- Packed_Array_Impl_Type field.
11610 Set_Packed_Array_Impl_Type (Def_Id, Empty);
11612 -- Build a freeze node if parent still needs one. Also make sure that
11613 -- the Depends_On_Private status is set because the subtype will need
11614 -- reprocessing at the time the base type does, and also we must set a
11615 -- conditional delay.
11617 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
11618 Conditional_Delay (Def_Id, T);
11619 end Constrain_Array;
11621 ------------------------------
11622 -- Constrain_Component_Type --
11623 ------------------------------
11625 function Constrain_Component_Type
11626 (Comp : Entity_Id;
11627 Constrained_Typ : Entity_Id;
11628 Related_Node : Node_Id;
11629 Typ : Entity_Id;
11630 Constraints : Elist_Id) return Entity_Id
11632 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
11633 Compon_Type : constant Entity_Id := Etype (Comp);
11635 function Build_Constrained_Array_Type
11636 (Old_Type : Entity_Id) return Entity_Id;
11637 -- If Old_Type is an array type, one of whose indexes is constrained
11638 -- by a discriminant, build an Itype whose constraint replaces the
11639 -- discriminant with its value in the constraint.
11641 function Build_Constrained_Discriminated_Type
11642 (Old_Type : Entity_Id) return Entity_Id;
11643 -- Ditto for record components
11645 function Build_Constrained_Access_Type
11646 (Old_Type : Entity_Id) return Entity_Id;
11647 -- Ditto for access types. Makes use of previous two functions, to
11648 -- constrain designated type.
11650 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
11651 -- T is an array or discriminated type, C is a list of constraints
11652 -- that apply to T. This routine builds the constrained subtype.
11654 function Is_Discriminant (Expr : Node_Id) return Boolean;
11655 -- Returns True if Expr is a discriminant
11657 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
11658 -- Find the value of discriminant Discrim in Constraint
11660 -----------------------------------
11661 -- Build_Constrained_Access_Type --
11662 -----------------------------------
11664 function Build_Constrained_Access_Type
11665 (Old_Type : Entity_Id) return Entity_Id
11667 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
11668 Itype : Entity_Id;
11669 Desig_Subtype : Entity_Id;
11670 Scop : Entity_Id;
11672 begin
11673 -- if the original access type was not embedded in the enclosing
11674 -- type definition, there is no need to produce a new access
11675 -- subtype. In fact every access type with an explicit constraint
11676 -- generates an itype whose scope is the enclosing record.
11678 if not Is_Type (Scope (Old_Type)) then
11679 return Old_Type;
11681 elsif Is_Array_Type (Desig_Type) then
11682 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
11684 elsif Has_Discriminants (Desig_Type) then
11686 -- This may be an access type to an enclosing record type for
11687 -- which we are constructing the constrained components. Return
11688 -- the enclosing record subtype. This is not always correct,
11689 -- but avoids infinite recursion. ???
11691 Desig_Subtype := Any_Type;
11693 for J in reverse 0 .. Scope_Stack.Last loop
11694 Scop := Scope_Stack.Table (J).Entity;
11696 if Is_Type (Scop)
11697 and then Base_Type (Scop) = Base_Type (Desig_Type)
11698 then
11699 Desig_Subtype := Scop;
11700 end if;
11702 exit when not Is_Type (Scop);
11703 end loop;
11705 if Desig_Subtype = Any_Type then
11706 Desig_Subtype :=
11707 Build_Constrained_Discriminated_Type (Desig_Type);
11708 end if;
11710 else
11711 return Old_Type;
11712 end if;
11714 if Desig_Subtype /= Desig_Type then
11716 -- The Related_Node better be here or else we won't be able
11717 -- to attach new itypes to a node in the tree.
11719 pragma Assert (Present (Related_Node));
11721 Itype := Create_Itype (E_Access_Subtype, Related_Node);
11723 Set_Etype (Itype, Base_Type (Old_Type));
11724 Set_Size_Info (Itype, (Old_Type));
11725 Set_Directly_Designated_Type (Itype, Desig_Subtype);
11726 Set_Depends_On_Private (Itype, Has_Private_Component
11727 (Old_Type));
11728 Set_Is_Access_Constant (Itype, Is_Access_Constant
11729 (Old_Type));
11731 -- The new itype needs freezing when it depends on a not frozen
11732 -- type and the enclosing subtype needs freezing.
11734 if Has_Delayed_Freeze (Constrained_Typ)
11735 and then not Is_Frozen (Constrained_Typ)
11736 then
11737 Conditional_Delay (Itype, Base_Type (Old_Type));
11738 end if;
11740 return Itype;
11742 else
11743 return Old_Type;
11744 end if;
11745 end Build_Constrained_Access_Type;
11747 ----------------------------------
11748 -- Build_Constrained_Array_Type --
11749 ----------------------------------
11751 function Build_Constrained_Array_Type
11752 (Old_Type : Entity_Id) return Entity_Id
11754 Lo_Expr : Node_Id;
11755 Hi_Expr : Node_Id;
11756 Old_Index : Node_Id;
11757 Range_Node : Node_Id;
11758 Constr_List : List_Id;
11760 Need_To_Create_Itype : Boolean := False;
11762 begin
11763 Old_Index := First_Index (Old_Type);
11764 while Present (Old_Index) loop
11765 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11767 if Is_Discriminant (Lo_Expr)
11768 or else Is_Discriminant (Hi_Expr)
11769 then
11770 Need_To_Create_Itype := True;
11771 end if;
11773 Next_Index (Old_Index);
11774 end loop;
11776 if Need_To_Create_Itype then
11777 Constr_List := New_List;
11779 Old_Index := First_Index (Old_Type);
11780 while Present (Old_Index) loop
11781 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
11783 if Is_Discriminant (Lo_Expr) then
11784 Lo_Expr := Get_Discr_Value (Lo_Expr);
11785 end if;
11787 if Is_Discriminant (Hi_Expr) then
11788 Hi_Expr := Get_Discr_Value (Hi_Expr);
11789 end if;
11791 Range_Node :=
11792 Make_Range
11793 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
11795 Append (Range_Node, To => Constr_List);
11797 Next_Index (Old_Index);
11798 end loop;
11800 return Build_Subtype (Old_Type, Constr_List);
11802 else
11803 return Old_Type;
11804 end if;
11805 end Build_Constrained_Array_Type;
11807 ------------------------------------------
11808 -- Build_Constrained_Discriminated_Type --
11809 ------------------------------------------
11811 function Build_Constrained_Discriminated_Type
11812 (Old_Type : Entity_Id) return Entity_Id
11814 Expr : Node_Id;
11815 Constr_List : List_Id;
11816 Old_Constraint : Elmt_Id;
11818 Need_To_Create_Itype : Boolean := False;
11820 begin
11821 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11822 while Present (Old_Constraint) loop
11823 Expr := Node (Old_Constraint);
11825 if Is_Discriminant (Expr) then
11826 Need_To_Create_Itype := True;
11827 end if;
11829 Next_Elmt (Old_Constraint);
11830 end loop;
11832 if Need_To_Create_Itype then
11833 Constr_List := New_List;
11835 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
11836 while Present (Old_Constraint) loop
11837 Expr := Node (Old_Constraint);
11839 if Is_Discriminant (Expr) then
11840 Expr := Get_Discr_Value (Expr);
11841 end if;
11843 Append (New_Copy_Tree (Expr), To => Constr_List);
11845 Next_Elmt (Old_Constraint);
11846 end loop;
11848 return Build_Subtype (Old_Type, Constr_List);
11850 else
11851 return Old_Type;
11852 end if;
11853 end Build_Constrained_Discriminated_Type;
11855 -------------------
11856 -- Build_Subtype --
11857 -------------------
11859 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
11860 Indic : Node_Id;
11861 Subtyp_Decl : Node_Id;
11862 Def_Id : Entity_Id;
11863 Btyp : Entity_Id := Base_Type (T);
11865 begin
11866 -- The Related_Node better be here or else we won't be able to
11867 -- attach new itypes to a node in the tree.
11869 pragma Assert (Present (Related_Node));
11871 -- If the view of the component's type is incomplete or private
11872 -- with unknown discriminants, then the constraint must be applied
11873 -- to the full type.
11875 if Has_Unknown_Discriminants (Btyp)
11876 and then Present (Underlying_Type (Btyp))
11877 then
11878 Btyp := Underlying_Type (Btyp);
11879 end if;
11881 Indic :=
11882 Make_Subtype_Indication (Loc,
11883 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
11884 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
11886 Def_Id := Create_Itype (Ekind (T), Related_Node);
11888 Subtyp_Decl :=
11889 Make_Subtype_Declaration (Loc,
11890 Defining_Identifier => Def_Id,
11891 Subtype_Indication => Indic);
11893 Set_Parent (Subtyp_Decl, Parent (Related_Node));
11895 -- Itypes must be analyzed with checks off (see package Itypes)
11897 Analyze (Subtyp_Decl, Suppress => All_Checks);
11899 return Def_Id;
11900 end Build_Subtype;
11902 ---------------------
11903 -- Get_Discr_Value --
11904 ---------------------
11906 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
11907 D : Entity_Id;
11908 E : Elmt_Id;
11910 begin
11911 -- The discriminant may be declared for the type, in which case we
11912 -- find it by iterating over the list of discriminants. If the
11913 -- discriminant is inherited from a parent type, it appears as the
11914 -- corresponding discriminant of the current type. This will be the
11915 -- case when constraining an inherited component whose constraint is
11916 -- given by a discriminant of the parent.
11918 D := First_Discriminant (Typ);
11919 E := First_Elmt (Constraints);
11921 while Present (D) loop
11922 if D = Entity (Discrim)
11923 or else D = CR_Discriminant (Entity (Discrim))
11924 or else Corresponding_Discriminant (D) = Entity (Discrim)
11925 then
11926 return Node (E);
11927 end if;
11929 Next_Discriminant (D);
11930 Next_Elmt (E);
11931 end loop;
11933 -- The Corresponding_Discriminant mechanism is incomplete, because
11934 -- the correspondence between new and old discriminants is not one
11935 -- to one: one new discriminant can constrain several old ones. In
11936 -- that case, scan sequentially the stored_constraint, the list of
11937 -- discriminants of the parents, and the constraints.
11939 -- Previous code checked for the present of the Stored_Constraint
11940 -- list for the derived type, but did not use it at all. Should it
11941 -- be present when the component is a discriminated task type?
11943 if Is_Derived_Type (Typ)
11944 and then Scope (Entity (Discrim)) = Etype (Typ)
11945 then
11946 D := First_Discriminant (Etype (Typ));
11947 E := First_Elmt (Constraints);
11948 while Present (D) loop
11949 if D = Entity (Discrim) then
11950 return Node (E);
11951 end if;
11953 Next_Discriminant (D);
11954 Next_Elmt (E);
11955 end loop;
11956 end if;
11958 -- Something is wrong if we did not find the value
11960 raise Program_Error;
11961 end Get_Discr_Value;
11963 ---------------------
11964 -- Is_Discriminant --
11965 ---------------------
11967 function Is_Discriminant (Expr : Node_Id) return Boolean is
11968 Discrim_Scope : Entity_Id;
11970 begin
11971 if Denotes_Discriminant (Expr) then
11972 Discrim_Scope := Scope (Entity (Expr));
11974 -- Either we have a reference to one of Typ's discriminants,
11976 pragma Assert (Discrim_Scope = Typ
11978 -- or to the discriminants of the parent type, in the case
11979 -- of a derivation of a tagged type with variants.
11981 or else Discrim_Scope = Etype (Typ)
11982 or else Full_View (Discrim_Scope) = Etype (Typ)
11984 -- or same as above for the case where the discriminants
11985 -- were declared in Typ's private view.
11987 or else (Is_Private_Type (Discrim_Scope)
11988 and then Chars (Discrim_Scope) = Chars (Typ))
11990 -- or else we are deriving from the full view and the
11991 -- discriminant is declared in the private entity.
11993 or else (Is_Private_Type (Typ)
11994 and then Chars (Discrim_Scope) = Chars (Typ))
11996 -- Or we are constrained the corresponding record of a
11997 -- synchronized type that completes a private declaration.
11999 or else (Is_Concurrent_Record_Type (Typ)
12000 and then
12001 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12003 -- or we have a class-wide type, in which case make sure the
12004 -- discriminant found belongs to the root type.
12006 or else (Is_Class_Wide_Type (Typ)
12007 and then Etype (Typ) = Discrim_Scope));
12009 return True;
12010 end if;
12012 -- In all other cases we have something wrong
12014 return False;
12015 end Is_Discriminant;
12017 -- Start of processing for Constrain_Component_Type
12019 begin
12020 if Nkind (Parent (Comp)) = N_Component_Declaration
12021 and then Comes_From_Source (Parent (Comp))
12022 and then Comes_From_Source
12023 (Subtype_Indication (Component_Definition (Parent (Comp))))
12024 and then
12025 Is_Entity_Name
12026 (Subtype_Indication (Component_Definition (Parent (Comp))))
12027 then
12028 return Compon_Type;
12030 elsif Is_Array_Type (Compon_Type) then
12031 return Build_Constrained_Array_Type (Compon_Type);
12033 elsif Has_Discriminants (Compon_Type) then
12034 return Build_Constrained_Discriminated_Type (Compon_Type);
12036 elsif Is_Access_Type (Compon_Type) then
12037 return Build_Constrained_Access_Type (Compon_Type);
12039 else
12040 return Compon_Type;
12041 end if;
12042 end Constrain_Component_Type;
12044 --------------------------
12045 -- Constrain_Concurrent --
12046 --------------------------
12048 -- For concurrent types, the associated record value type carries the same
12049 -- discriminants, so when we constrain a concurrent type, we must constrain
12050 -- the corresponding record type as well.
12052 procedure Constrain_Concurrent
12053 (Def_Id : in out Entity_Id;
12054 SI : Node_Id;
12055 Related_Nod : Node_Id;
12056 Related_Id : Entity_Id;
12057 Suffix : Character)
12059 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12060 -- case of a private subtype (needed when only doing semantic analysis).
12062 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12063 T_Val : Entity_Id;
12065 begin
12066 if Is_Access_Type (T_Ent) then
12067 T_Ent := Designated_Type (T_Ent);
12068 end if;
12070 T_Val := Corresponding_Record_Type (T_Ent);
12072 if Present (T_Val) then
12074 if No (Def_Id) then
12075 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12076 end if;
12078 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12080 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12081 Set_Corresponding_Record_Type (Def_Id,
12082 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12084 else
12085 -- If there is no associated record, expansion is disabled and this
12086 -- is a generic context. Create a subtype in any case, so that
12087 -- semantic analysis can proceed.
12089 if No (Def_Id) then
12090 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12091 end if;
12093 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12094 end if;
12095 end Constrain_Concurrent;
12097 ------------------------------------
12098 -- Constrain_Corresponding_Record --
12099 ------------------------------------
12101 function Constrain_Corresponding_Record
12102 (Prot_Subt : Entity_Id;
12103 Corr_Rec : Entity_Id;
12104 Related_Nod : Node_Id) return Entity_Id
12106 T_Sub : constant Entity_Id :=
12107 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12109 begin
12110 Set_Etype (T_Sub, Corr_Rec);
12111 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12112 Set_Is_Constrained (T_Sub, True);
12113 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12114 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12116 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12117 Set_Discriminant_Constraint
12118 (T_Sub, Discriminant_Constraint (Prot_Subt));
12119 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12120 Create_Constrained_Components
12121 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12122 end if;
12124 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12126 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12127 Conditional_Delay (T_Sub, Corr_Rec);
12129 else
12130 -- This is a component subtype: it will be frozen in the context of
12131 -- the enclosing record's init_proc, so that discriminant references
12132 -- are resolved to discriminals. (Note: we used to skip freezing
12133 -- altogether in that case, which caused errors downstream for
12134 -- components of a bit packed array type).
12136 Set_Has_Delayed_Freeze (T_Sub);
12137 end if;
12139 return T_Sub;
12140 end Constrain_Corresponding_Record;
12142 -----------------------
12143 -- Constrain_Decimal --
12144 -----------------------
12146 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12147 T : constant Entity_Id := Entity (Subtype_Mark (S));
12148 C : constant Node_Id := Constraint (S);
12149 Loc : constant Source_Ptr := Sloc (C);
12150 Range_Expr : Node_Id;
12151 Digits_Expr : Node_Id;
12152 Digits_Val : Uint;
12153 Bound_Val : Ureal;
12155 begin
12156 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12158 if Nkind (C) = N_Range_Constraint then
12159 Range_Expr := Range_Expression (C);
12160 Digits_Val := Digits_Value (T);
12162 else
12163 pragma Assert (Nkind (C) = N_Digits_Constraint);
12165 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12167 Digits_Expr := Digits_Expression (C);
12168 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12170 Check_Digits_Expression (Digits_Expr);
12171 Digits_Val := Expr_Value (Digits_Expr);
12173 if Digits_Val > Digits_Value (T) then
12174 Error_Msg_N
12175 ("digits expression is incompatible with subtype", C);
12176 Digits_Val := Digits_Value (T);
12177 end if;
12179 if Present (Range_Constraint (C)) then
12180 Range_Expr := Range_Expression (Range_Constraint (C));
12181 else
12182 Range_Expr := Empty;
12183 end if;
12184 end if;
12186 Set_Etype (Def_Id, Base_Type (T));
12187 Set_Size_Info (Def_Id, (T));
12188 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12189 Set_Delta_Value (Def_Id, Delta_Value (T));
12190 Set_Scale_Value (Def_Id, Scale_Value (T));
12191 Set_Small_Value (Def_Id, Small_Value (T));
12192 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
12193 Set_Digits_Value (Def_Id, Digits_Val);
12195 -- Manufacture range from given digits value if no range present
12197 if No (Range_Expr) then
12198 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
12199 Range_Expr :=
12200 Make_Range (Loc,
12201 Low_Bound =>
12202 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
12203 High_Bound =>
12204 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
12205 end if;
12207 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
12208 Set_Discrete_RM_Size (Def_Id);
12210 -- Unconditionally delay the freeze, since we cannot set size
12211 -- information in all cases correctly until the freeze point.
12213 Set_Has_Delayed_Freeze (Def_Id);
12214 end Constrain_Decimal;
12216 ----------------------------------
12217 -- Constrain_Discriminated_Type --
12218 ----------------------------------
12220 procedure Constrain_Discriminated_Type
12221 (Def_Id : Entity_Id;
12222 S : Node_Id;
12223 Related_Nod : Node_Id;
12224 For_Access : Boolean := False)
12226 E : constant Entity_Id := Entity (Subtype_Mark (S));
12227 T : Entity_Id;
12228 C : Node_Id;
12229 Elist : Elist_Id := New_Elmt_List;
12231 procedure Fixup_Bad_Constraint;
12232 -- This is called after finding a bad constraint, and after having
12233 -- posted an appropriate error message. The mission is to leave the
12234 -- entity T in as reasonable state as possible.
12236 --------------------------
12237 -- Fixup_Bad_Constraint --
12238 --------------------------
12240 procedure Fixup_Bad_Constraint is
12241 begin
12242 -- Set a reasonable Ekind for the entity. For an incomplete type,
12243 -- we can't do much, but for other types, we can set the proper
12244 -- corresponding subtype kind.
12246 if Ekind (T) = E_Incomplete_Type then
12247 Set_Ekind (Def_Id, Ekind (T));
12248 else
12249 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
12250 end if;
12252 -- Set Etype to the known type, to reduce chances of cascaded errors
12254 Set_Etype (Def_Id, E);
12255 Set_Error_Posted (Def_Id);
12256 end Fixup_Bad_Constraint;
12258 -- Start of processing for Constrain_Discriminated_Type
12260 begin
12261 C := Constraint (S);
12263 -- A discriminant constraint is only allowed in a subtype indication,
12264 -- after a subtype mark. This subtype mark must denote either a type
12265 -- with discriminants, or an access type whose designated type is a
12266 -- type with discriminants. A discriminant constraint specifies the
12267 -- values of these discriminants (RM 3.7.2(5)).
12269 T := Base_Type (Entity (Subtype_Mark (S)));
12271 if Is_Access_Type (T) then
12272 T := Designated_Type (T);
12273 end if;
12275 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
12276 -- Avoid generating an error for access-to-incomplete subtypes.
12278 if Ada_Version >= Ada_2005
12279 and then Ekind (T) = E_Incomplete_Type
12280 and then Nkind (Parent (S)) = N_Subtype_Declaration
12281 and then not Is_Itype (Def_Id)
12282 then
12283 -- A little sanity check, emit an error message if the type
12284 -- has discriminants to begin with. Type T may be a regular
12285 -- incomplete type or imported via a limited with clause.
12287 if Has_Discriminants (T)
12288 or else (From_Limited_With (T)
12289 and then Present (Non_Limited_View (T))
12290 and then Nkind (Parent (Non_Limited_View (T))) =
12291 N_Full_Type_Declaration
12292 and then Present (Discriminant_Specifications
12293 (Parent (Non_Limited_View (T)))))
12294 then
12295 Error_Msg_N
12296 ("(Ada 2005) incomplete subtype may not be constrained", C);
12297 else
12298 Error_Msg_N ("invalid constraint: type has no discriminant", C);
12299 end if;
12301 Fixup_Bad_Constraint;
12302 return;
12304 -- Check that the type has visible discriminants. The type may be
12305 -- a private type with unknown discriminants whose full view has
12306 -- discriminants which are invisible.
12308 elsif not Has_Discriminants (T)
12309 or else
12310 (Has_Unknown_Discriminants (T)
12311 and then Is_Private_Type (T))
12312 then
12313 Error_Msg_N ("invalid constraint: type has no discriminant", C);
12314 Fixup_Bad_Constraint;
12315 return;
12317 elsif Is_Constrained (E)
12318 or else (Ekind (E) = E_Class_Wide_Subtype
12319 and then Present (Discriminant_Constraint (E)))
12320 then
12321 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
12322 Fixup_Bad_Constraint;
12323 return;
12324 end if;
12326 -- T may be an unconstrained subtype (e.g. a generic actual).
12327 -- Constraint applies to the base type.
12329 T := Base_Type (T);
12331 Elist := Build_Discriminant_Constraints (T, S);
12333 -- If the list returned was empty we had an error in building the
12334 -- discriminant constraint. We have also already signalled an error
12335 -- in the incomplete type case
12337 if Is_Empty_Elmt_List (Elist) then
12338 Fixup_Bad_Constraint;
12339 return;
12340 end if;
12342 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
12343 end Constrain_Discriminated_Type;
12345 ---------------------------
12346 -- Constrain_Enumeration --
12347 ---------------------------
12349 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
12350 T : constant Entity_Id := Entity (Subtype_Mark (S));
12351 C : constant Node_Id := Constraint (S);
12353 begin
12354 Set_Ekind (Def_Id, E_Enumeration_Subtype);
12356 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
12358 Set_Etype (Def_Id, Base_Type (T));
12359 Set_Size_Info (Def_Id, (T));
12360 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12361 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
12363 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
12365 Set_Discrete_RM_Size (Def_Id);
12366 end Constrain_Enumeration;
12368 ----------------------
12369 -- Constrain_Float --
12370 ----------------------
12372 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
12373 T : constant Entity_Id := Entity (Subtype_Mark (S));
12374 C : Node_Id;
12375 D : Node_Id;
12376 Rais : Node_Id;
12378 begin
12379 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
12381 Set_Etype (Def_Id, Base_Type (T));
12382 Set_Size_Info (Def_Id, (T));
12383 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12385 -- Process the constraint
12387 C := Constraint (S);
12389 -- Digits constraint present
12391 if Nkind (C) = N_Digits_Constraint then
12393 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12394 Check_Restriction (No_Obsolescent_Features, C);
12396 if Warn_On_Obsolescent_Feature then
12397 Error_Msg_N
12398 ("subtype digits constraint is an " &
12399 "obsolescent feature (RM J.3(8))?j?", C);
12400 end if;
12402 D := Digits_Expression (C);
12403 Analyze_And_Resolve (D, Any_Integer);
12404 Check_Digits_Expression (D);
12405 Set_Digits_Value (Def_Id, Expr_Value (D));
12407 -- Check that digits value is in range. Obviously we can do this
12408 -- at compile time, but it is strictly a runtime check, and of
12409 -- course there is an ACVC test that checks this.
12411 if Digits_Value (Def_Id) > Digits_Value (T) then
12412 Error_Msg_Uint_1 := Digits_Value (T);
12413 Error_Msg_N ("??digits value is too large, maximum is ^", D);
12414 Rais :=
12415 Make_Raise_Constraint_Error (Sloc (D),
12416 Reason => CE_Range_Check_Failed);
12417 Insert_Action (Declaration_Node (Def_Id), Rais);
12418 end if;
12420 C := Range_Constraint (C);
12422 -- No digits constraint present
12424 else
12425 Set_Digits_Value (Def_Id, Digits_Value (T));
12426 end if;
12428 -- Range constraint present
12430 if Nkind (C) = N_Range_Constraint then
12431 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
12433 -- No range constraint present
12435 else
12436 pragma Assert (No (C));
12437 Set_Scalar_Range (Def_Id, Scalar_Range (T));
12438 end if;
12440 Set_Is_Constrained (Def_Id);
12441 end Constrain_Float;
12443 ---------------------
12444 -- Constrain_Index --
12445 ---------------------
12447 procedure Constrain_Index
12448 (Index : Node_Id;
12449 S : Node_Id;
12450 Related_Nod : Node_Id;
12451 Related_Id : Entity_Id;
12452 Suffix : Character;
12453 Suffix_Index : Nat)
12455 Def_Id : Entity_Id;
12456 R : Node_Id := Empty;
12457 T : constant Entity_Id := Etype (Index);
12459 begin
12460 if Nkind (S) = N_Range
12461 or else
12462 (Nkind (S) = N_Attribute_Reference
12463 and then Attribute_Name (S) = Name_Range)
12464 then
12465 -- A Range attribute will be transformed into N_Range by Resolve
12467 Analyze (S);
12468 Set_Etype (S, T);
12469 R := S;
12471 Process_Range_Expr_In_Decl (R, T);
12473 if not Error_Posted (S)
12474 and then
12475 (Nkind (S) /= N_Range
12476 or else not Covers (T, (Etype (Low_Bound (S))))
12477 or else not Covers (T, (Etype (High_Bound (S)))))
12478 then
12479 if Base_Type (T) /= Any_Type
12480 and then Etype (Low_Bound (S)) /= Any_Type
12481 and then Etype (High_Bound (S)) /= Any_Type
12482 then
12483 Error_Msg_N ("range expected", S);
12484 end if;
12485 end if;
12487 elsif Nkind (S) = N_Subtype_Indication then
12489 -- The parser has verified that this is a discrete indication
12491 Resolve_Discrete_Subtype_Indication (S, T);
12492 Bad_Predicated_Subtype_Use
12493 ("subtype& has predicate, not allowed in index constraint",
12494 S, Entity (Subtype_Mark (S)));
12496 R := Range_Expression (Constraint (S));
12498 -- Capture values of bounds and generate temporaries for them if
12499 -- needed, since checks may cause duplication of the expressions
12500 -- which must not be reevaluated.
12502 -- The forced evaluation removes side effects from expressions, which
12503 -- should occur also in GNATprove mode. Otherwise, we end up with
12504 -- unexpected insertions of actions at places where this is not
12505 -- supposed to occur, e.g. on default parameters of a call.
12507 if Expander_Active or GNATprove_Mode then
12508 Force_Evaluation (Low_Bound (R));
12509 Force_Evaluation (High_Bound (R));
12510 end if;
12512 elsif Nkind (S) = N_Discriminant_Association then
12514 -- Syntactically valid in subtype indication
12516 Error_Msg_N ("invalid index constraint", S);
12517 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
12518 return;
12520 -- Subtype_Mark case, no anonymous subtypes to construct
12522 else
12523 Analyze (S);
12525 if Is_Entity_Name (S) then
12526 if not Is_Type (Entity (S)) then
12527 Error_Msg_N ("expect subtype mark for index constraint", S);
12529 elsif Base_Type (Entity (S)) /= Base_Type (T) then
12530 Wrong_Type (S, Base_Type (T));
12532 -- Check error of subtype with predicate in index constraint
12534 else
12535 Bad_Predicated_Subtype_Use
12536 ("subtype& has predicate, not allowed in index constraint",
12537 S, Entity (S));
12538 end if;
12540 return;
12542 else
12543 Error_Msg_N ("invalid index constraint", S);
12544 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
12545 return;
12546 end if;
12547 end if;
12549 Def_Id :=
12550 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
12552 Set_Etype (Def_Id, Base_Type (T));
12554 if Is_Modular_Integer_Type (T) then
12555 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
12557 elsif Is_Integer_Type (T) then
12558 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
12560 else
12561 Set_Ekind (Def_Id, E_Enumeration_Subtype);
12562 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
12563 Set_First_Literal (Def_Id, First_Literal (T));
12564 end if;
12566 Set_Size_Info (Def_Id, (T));
12567 Set_RM_Size (Def_Id, RM_Size (T));
12568 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12570 Set_Scalar_Range (Def_Id, R);
12572 Set_Etype (S, Def_Id);
12573 Set_Discrete_RM_Size (Def_Id);
12574 end Constrain_Index;
12576 -----------------------
12577 -- Constrain_Integer --
12578 -----------------------
12580 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
12581 T : constant Entity_Id := Entity (Subtype_Mark (S));
12582 C : constant Node_Id := Constraint (S);
12584 begin
12585 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
12587 if Is_Modular_Integer_Type (T) then
12588 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
12589 else
12590 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
12591 end if;
12593 Set_Etype (Def_Id, Base_Type (T));
12594 Set_Size_Info (Def_Id, (T));
12595 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12596 Set_Discrete_RM_Size (Def_Id);
12597 end Constrain_Integer;
12599 ------------------------------
12600 -- Constrain_Ordinary_Fixed --
12601 ------------------------------
12603 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
12604 T : constant Entity_Id := Entity (Subtype_Mark (S));
12605 C : Node_Id;
12606 D : Node_Id;
12607 Rais : Node_Id;
12609 begin
12610 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
12611 Set_Etype (Def_Id, Base_Type (T));
12612 Set_Size_Info (Def_Id, (T));
12613 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12614 Set_Small_Value (Def_Id, Small_Value (T));
12616 -- Process the constraint
12618 C := Constraint (S);
12620 -- Delta constraint present
12622 if Nkind (C) = N_Delta_Constraint then
12624 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
12625 Check_Restriction (No_Obsolescent_Features, C);
12627 if Warn_On_Obsolescent_Feature then
12628 Error_Msg_S
12629 ("subtype delta constraint is an " &
12630 "obsolescent feature (RM J.3(7))?j?");
12631 end if;
12633 D := Delta_Expression (C);
12634 Analyze_And_Resolve (D, Any_Real);
12635 Check_Delta_Expression (D);
12636 Set_Delta_Value (Def_Id, Expr_Value_R (D));
12638 -- Check that delta value is in range. Obviously we can do this
12639 -- at compile time, but it is strictly a runtime check, and of
12640 -- course there is an ACVC test that checks this.
12642 if Delta_Value (Def_Id) < Delta_Value (T) then
12643 Error_Msg_N ("??delta value is too small", D);
12644 Rais :=
12645 Make_Raise_Constraint_Error (Sloc (D),
12646 Reason => CE_Range_Check_Failed);
12647 Insert_Action (Declaration_Node (Def_Id), Rais);
12648 end if;
12650 C := Range_Constraint (C);
12652 -- No delta constraint present
12654 else
12655 Set_Delta_Value (Def_Id, Delta_Value (T));
12656 end if;
12658 -- Range constraint present
12660 if Nkind (C) = N_Range_Constraint then
12661 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
12663 -- No range constraint present
12665 else
12666 pragma Assert (No (C));
12667 Set_Scalar_Range (Def_Id, Scalar_Range (T));
12669 end if;
12671 Set_Discrete_RM_Size (Def_Id);
12673 -- Unconditionally delay the freeze, since we cannot set size
12674 -- information in all cases correctly until the freeze point.
12676 Set_Has_Delayed_Freeze (Def_Id);
12677 end Constrain_Ordinary_Fixed;
12679 -----------------------
12680 -- Contain_Interface --
12681 -----------------------
12683 function Contain_Interface
12684 (Iface : Entity_Id;
12685 Ifaces : Elist_Id) return Boolean
12687 Iface_Elmt : Elmt_Id;
12689 begin
12690 if Present (Ifaces) then
12691 Iface_Elmt := First_Elmt (Ifaces);
12692 while Present (Iface_Elmt) loop
12693 if Node (Iface_Elmt) = Iface then
12694 return True;
12695 end if;
12697 Next_Elmt (Iface_Elmt);
12698 end loop;
12699 end if;
12701 return False;
12702 end Contain_Interface;
12704 ---------------------------
12705 -- Convert_Scalar_Bounds --
12706 ---------------------------
12708 procedure Convert_Scalar_Bounds
12709 (N : Node_Id;
12710 Parent_Type : Entity_Id;
12711 Derived_Type : Entity_Id;
12712 Loc : Source_Ptr)
12714 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
12716 Lo : Node_Id;
12717 Hi : Node_Id;
12718 Rng : Node_Id;
12720 begin
12721 -- Defend against previous errors
12723 if No (Scalar_Range (Derived_Type)) then
12724 Check_Error_Detected;
12725 return;
12726 end if;
12728 Lo := Build_Scalar_Bound
12729 (Type_Low_Bound (Derived_Type),
12730 Parent_Type, Implicit_Base);
12732 Hi := Build_Scalar_Bound
12733 (Type_High_Bound (Derived_Type),
12734 Parent_Type, Implicit_Base);
12736 Rng :=
12737 Make_Range (Loc,
12738 Low_Bound => Lo,
12739 High_Bound => Hi);
12741 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
12743 Set_Parent (Rng, N);
12744 Set_Scalar_Range (Derived_Type, Rng);
12746 -- Analyze the bounds
12748 Analyze_And_Resolve (Lo, Implicit_Base);
12749 Analyze_And_Resolve (Hi, Implicit_Base);
12751 -- Analyze the range itself, except that we do not analyze it if
12752 -- the bounds are real literals, and we have a fixed-point type.
12753 -- The reason for this is that we delay setting the bounds in this
12754 -- case till we know the final Small and Size values (see circuit
12755 -- in Freeze.Freeze_Fixed_Point_Type for further details).
12757 if Is_Fixed_Point_Type (Parent_Type)
12758 and then Nkind (Lo) = N_Real_Literal
12759 and then Nkind (Hi) = N_Real_Literal
12760 then
12761 return;
12763 -- Here we do the analysis of the range
12765 -- Note: we do this manually, since if we do a normal Analyze and
12766 -- Resolve call, there are problems with the conversions used for
12767 -- the derived type range.
12769 else
12770 Set_Etype (Rng, Implicit_Base);
12771 Set_Analyzed (Rng, True);
12772 end if;
12773 end Convert_Scalar_Bounds;
12775 -------------------
12776 -- Copy_And_Swap --
12777 -------------------
12779 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
12780 begin
12781 -- Initialize new full declaration entity by copying the pertinent
12782 -- fields of the corresponding private declaration entity.
12784 -- We temporarily set Ekind to a value appropriate for a type to
12785 -- avoid assert failures in Einfo from checking for setting type
12786 -- attributes on something that is not a type. Ekind (Priv) is an
12787 -- appropriate choice, since it allowed the attributes to be set
12788 -- in the first place. This Ekind value will be modified later.
12790 Set_Ekind (Full, Ekind (Priv));
12792 -- Also set Etype temporarily to Any_Type, again, in the absence
12793 -- of errors, it will be properly reset, and if there are errors,
12794 -- then we want a value of Any_Type to remain.
12796 Set_Etype (Full, Any_Type);
12798 -- Now start copying attributes
12800 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
12802 if Has_Discriminants (Full) then
12803 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
12804 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
12805 end if;
12807 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
12808 Set_Homonym (Full, Homonym (Priv));
12809 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
12810 Set_Is_Public (Full, Is_Public (Priv));
12811 Set_Is_Pure (Full, Is_Pure (Priv));
12812 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
12813 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
12814 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
12815 Set_Has_Pragma_Unreferenced_Objects
12816 (Full, Has_Pragma_Unreferenced_Objects
12817 (Priv));
12819 Conditional_Delay (Full, Priv);
12821 if Is_Tagged_Type (Full) then
12822 Set_Direct_Primitive_Operations (Full,
12823 Direct_Primitive_Operations (Priv));
12825 if Is_Base_Type (Priv) then
12826 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
12827 end if;
12828 end if;
12830 Set_Is_Volatile (Full, Is_Volatile (Priv));
12831 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
12832 Set_Scope (Full, Scope (Priv));
12833 Set_Next_Entity (Full, Next_Entity (Priv));
12834 Set_First_Entity (Full, First_Entity (Priv));
12835 Set_Last_Entity (Full, Last_Entity (Priv));
12837 -- If access types have been recorded for later handling, keep them in
12838 -- the full view so that they get handled when the full view freeze
12839 -- node is expanded.
12841 if Present (Freeze_Node (Priv))
12842 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
12843 then
12844 Ensure_Freeze_Node (Full);
12845 Set_Access_Types_To_Process
12846 (Freeze_Node (Full),
12847 Access_Types_To_Process (Freeze_Node (Priv)));
12848 end if;
12850 -- Swap the two entities. Now Private is the full type entity and Full
12851 -- is the private one. They will be swapped back at the end of the
12852 -- private part. This swapping ensures that the entity that is visible
12853 -- in the private part is the full declaration.
12855 Exchange_Entities (Priv, Full);
12856 Append_Entity (Full, Scope (Full));
12857 end Copy_And_Swap;
12859 -------------------------------------
12860 -- Copy_Array_Base_Type_Attributes --
12861 -------------------------------------
12863 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
12864 begin
12865 Set_Component_Alignment (T1, Component_Alignment (T2));
12866 Set_Component_Type (T1, Component_Type (T2));
12867 Set_Component_Size (T1, Component_Size (T2));
12868 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
12869 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
12870 Set_Has_Protected (T1, Has_Protected (T2));
12871 Set_Has_Task (T1, Has_Task (T2));
12872 Set_Is_Packed (T1, Is_Packed (T2));
12873 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
12874 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
12875 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
12876 end Copy_Array_Base_Type_Attributes;
12878 -----------------------------------
12879 -- Copy_Array_Subtype_Attributes --
12880 -----------------------------------
12882 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
12883 begin
12884 Set_Size_Info (T1, T2);
12886 Set_First_Index (T1, First_Index (T2));
12887 Set_Is_Aliased (T1, Is_Aliased (T2));
12888 Set_Is_Volatile (T1, Is_Volatile (T2));
12889 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
12890 Set_Is_Constrained (T1, Is_Constrained (T2));
12891 Set_Depends_On_Private (T1, Has_Private_Component (T2));
12892 Set_First_Rep_Item (T1, First_Rep_Item (T2));
12893 Set_Convention (T1, Convention (T2));
12894 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
12895 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
12896 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
12897 end Copy_Array_Subtype_Attributes;
12899 -----------------------------------
12900 -- Create_Constrained_Components --
12901 -----------------------------------
12903 procedure Create_Constrained_Components
12904 (Subt : Entity_Id;
12905 Decl_Node : Node_Id;
12906 Typ : Entity_Id;
12907 Constraints : Elist_Id)
12909 Loc : constant Source_Ptr := Sloc (Subt);
12910 Comp_List : constant Elist_Id := New_Elmt_List;
12911 Parent_Type : constant Entity_Id := Etype (Typ);
12912 Assoc_List : constant List_Id := New_List;
12913 Discr_Val : Elmt_Id;
12914 Errors : Boolean;
12915 New_C : Entity_Id;
12916 Old_C : Entity_Id;
12917 Is_Static : Boolean := True;
12919 procedure Collect_Fixed_Components (Typ : Entity_Id);
12920 -- Collect parent type components that do not appear in a variant part
12922 procedure Create_All_Components;
12923 -- Iterate over Comp_List to create the components of the subtype
12925 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
12926 -- Creates a new component from Old_Compon, copying all the fields from
12927 -- it, including its Etype, inserts the new component in the Subt entity
12928 -- chain and returns the new component.
12930 function Is_Variant_Record (T : Entity_Id) return Boolean;
12931 -- If true, and discriminants are static, collect only components from
12932 -- variants selected by discriminant values.
12934 ------------------------------
12935 -- Collect_Fixed_Components --
12936 ------------------------------
12938 procedure Collect_Fixed_Components (Typ : Entity_Id) is
12939 begin
12940 -- Build association list for discriminants, and find components of the
12941 -- variant part selected by the values of the discriminants.
12943 Old_C := First_Discriminant (Typ);
12944 Discr_Val := First_Elmt (Constraints);
12945 while Present (Old_C) loop
12946 Append_To (Assoc_List,
12947 Make_Component_Association (Loc,
12948 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
12949 Expression => New_Copy (Node (Discr_Val))));
12951 Next_Elmt (Discr_Val);
12952 Next_Discriminant (Old_C);
12953 end loop;
12955 -- The tag and the possible parent component are unconditionally in
12956 -- the subtype.
12958 if Is_Tagged_Type (Typ)
12959 or else Has_Controlled_Component (Typ)
12960 then
12961 Old_C := First_Component (Typ);
12962 while Present (Old_C) loop
12963 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
12964 Append_Elmt (Old_C, Comp_List);
12965 end if;
12967 Next_Component (Old_C);
12968 end loop;
12969 end if;
12970 end Collect_Fixed_Components;
12972 ---------------------------
12973 -- Create_All_Components --
12974 ---------------------------
12976 procedure Create_All_Components is
12977 Comp : Elmt_Id;
12979 begin
12980 Comp := First_Elmt (Comp_List);
12981 while Present (Comp) loop
12982 Old_C := Node (Comp);
12983 New_C := Create_Component (Old_C);
12985 Set_Etype
12986 (New_C,
12987 Constrain_Component_Type
12988 (Old_C, Subt, Decl_Node, Typ, Constraints));
12989 Set_Is_Public (New_C, Is_Public (Subt));
12991 Next_Elmt (Comp);
12992 end loop;
12993 end Create_All_Components;
12995 ----------------------
12996 -- Create_Component --
12997 ----------------------
12999 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13000 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13002 begin
13003 if Ekind (Old_Compon) = E_Discriminant
13004 and then Is_Completely_Hidden (Old_Compon)
13005 then
13006 -- This is a shadow discriminant created for a discriminant of
13007 -- the parent type, which needs to be present in the subtype.
13008 -- Give the shadow discriminant an internal name that cannot
13009 -- conflict with that of visible components.
13011 Set_Chars (New_Compon, New_Internal_Name ('C'));
13012 end if;
13014 -- Set the parent so we have a proper link for freezing etc. This is
13015 -- not a real parent pointer, since of course our parent does not own
13016 -- up to us and reference us, we are an illegitimate child of the
13017 -- original parent.
13019 Set_Parent (New_Compon, Parent (Old_Compon));
13021 -- If the old component's Esize was already determined and is a
13022 -- static value, then the new component simply inherits it. Otherwise
13023 -- the old component's size may require run-time determination, but
13024 -- the new component's size still might be statically determinable
13025 -- (if, for example it has a static constraint). In that case we want
13026 -- Layout_Type to recompute the component's size, so we reset its
13027 -- size and positional fields.
13029 if Frontend_Layout_On_Target
13030 and then not Known_Static_Esize (Old_Compon)
13031 then
13032 Set_Esize (New_Compon, Uint_0);
13033 Init_Normalized_First_Bit (New_Compon);
13034 Init_Normalized_Position (New_Compon);
13035 Init_Normalized_Position_Max (New_Compon);
13036 end if;
13038 -- We do not want this node marked as Comes_From_Source, since
13039 -- otherwise it would get first class status and a separate cross-
13040 -- reference line would be generated. Illegitimate children do not
13041 -- rate such recognition.
13043 Set_Comes_From_Source (New_Compon, False);
13045 -- But it is a real entity, and a birth certificate must be properly
13046 -- registered by entering it into the entity list.
13048 Enter_Name (New_Compon);
13050 return New_Compon;
13051 end Create_Component;
13053 -----------------------
13054 -- Is_Variant_Record --
13055 -----------------------
13057 function Is_Variant_Record (T : Entity_Id) return Boolean is
13058 begin
13059 return Nkind (Parent (T)) = N_Full_Type_Declaration
13060 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13061 and then Present (Component_List (Type_Definition (Parent (T))))
13062 and then
13063 Present
13064 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13065 end Is_Variant_Record;
13067 -- Start of processing for Create_Constrained_Components
13069 begin
13070 pragma Assert (Subt /= Base_Type (Subt));
13071 pragma Assert (Typ = Base_Type (Typ));
13073 Set_First_Entity (Subt, Empty);
13074 Set_Last_Entity (Subt, Empty);
13076 -- Check whether constraint is fully static, in which case we can
13077 -- optimize the list of components.
13079 Discr_Val := First_Elmt (Constraints);
13080 while Present (Discr_Val) loop
13081 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13082 Is_Static := False;
13083 exit;
13084 end if;
13086 Next_Elmt (Discr_Val);
13087 end loop;
13089 Set_Has_Static_Discriminants (Subt, Is_Static);
13091 Push_Scope (Subt);
13093 -- Inherit the discriminants of the parent type
13095 Add_Discriminants : declare
13096 Num_Disc : Int;
13097 Num_Gird : Int;
13099 begin
13100 Num_Disc := 0;
13101 Old_C := First_Discriminant (Typ);
13103 while Present (Old_C) loop
13104 Num_Disc := Num_Disc + 1;
13105 New_C := Create_Component (Old_C);
13106 Set_Is_Public (New_C, Is_Public (Subt));
13107 Next_Discriminant (Old_C);
13108 end loop;
13110 -- For an untagged derived subtype, the number of discriminants may
13111 -- be smaller than the number of inherited discriminants, because
13112 -- several of them may be renamed by a single new discriminant or
13113 -- constrained. In this case, add the hidden discriminants back into
13114 -- the subtype, because they need to be present if the optimizer of
13115 -- the GCC 4.x back-end decides to break apart assignments between
13116 -- objects using the parent view into member-wise assignments.
13118 Num_Gird := 0;
13120 if Is_Derived_Type (Typ)
13121 and then not Is_Tagged_Type (Typ)
13122 then
13123 Old_C := First_Stored_Discriminant (Typ);
13125 while Present (Old_C) loop
13126 Num_Gird := Num_Gird + 1;
13127 Next_Stored_Discriminant (Old_C);
13128 end loop;
13129 end if;
13131 if Num_Gird > Num_Disc then
13133 -- Find out multiple uses of new discriminants, and add hidden
13134 -- components for the extra renamed discriminants. We recognize
13135 -- multiple uses through the Corresponding_Discriminant of a
13136 -- new discriminant: if it constrains several old discriminants,
13137 -- this field points to the last one in the parent type. The
13138 -- stored discriminants of the derived type have the same name
13139 -- as those of the parent.
13141 declare
13142 Constr : Elmt_Id;
13143 New_Discr : Entity_Id;
13144 Old_Discr : Entity_Id;
13146 begin
13147 Constr := First_Elmt (Stored_Constraint (Typ));
13148 Old_Discr := First_Stored_Discriminant (Typ);
13149 while Present (Constr) loop
13150 if Is_Entity_Name (Node (Constr))
13151 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13152 then
13153 New_Discr := Entity (Node (Constr));
13155 if Chars (Corresponding_Discriminant (New_Discr)) /=
13156 Chars (Old_Discr)
13157 then
13158 -- The new discriminant has been used to rename a
13159 -- subsequent old discriminant. Introduce a shadow
13160 -- component for the current old discriminant.
13162 New_C := Create_Component (Old_Discr);
13163 Set_Original_Record_Component (New_C, Old_Discr);
13164 end if;
13166 else
13167 -- The constraint has eliminated the old discriminant.
13168 -- Introduce a shadow component.
13170 New_C := Create_Component (Old_Discr);
13171 Set_Original_Record_Component (New_C, Old_Discr);
13172 end if;
13174 Next_Elmt (Constr);
13175 Next_Stored_Discriminant (Old_Discr);
13176 end loop;
13177 end;
13178 end if;
13179 end Add_Discriminants;
13181 if Is_Static
13182 and then Is_Variant_Record (Typ)
13183 then
13184 Collect_Fixed_Components (Typ);
13186 Gather_Components (
13187 Typ,
13188 Component_List (Type_Definition (Parent (Typ))),
13189 Governed_By => Assoc_List,
13190 Into => Comp_List,
13191 Report_Errors => Errors);
13192 pragma Assert (not Errors);
13194 Create_All_Components;
13196 -- If the subtype declaration is created for a tagged type derivation
13197 -- with constraints, we retrieve the record definition of the parent
13198 -- type to select the components of the proper variant.
13200 elsif Is_Static
13201 and then Is_Tagged_Type (Typ)
13202 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
13203 and then
13204 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
13205 and then Is_Variant_Record (Parent_Type)
13206 then
13207 Collect_Fixed_Components (Typ);
13209 Gather_Components (
13210 Typ,
13211 Component_List (Type_Definition (Parent (Parent_Type))),
13212 Governed_By => Assoc_List,
13213 Into => Comp_List,
13214 Report_Errors => Errors);
13215 pragma Assert (not Errors);
13217 -- If the tagged derivation has a type extension, collect all the
13218 -- new components therein.
13220 if Present
13221 (Record_Extension_Part (Type_Definition (Parent (Typ))))
13222 then
13223 Old_C := First_Component (Typ);
13224 while Present (Old_C) loop
13225 if Original_Record_Component (Old_C) = Old_C
13226 and then Chars (Old_C) /= Name_uTag
13227 and then Chars (Old_C) /= Name_uParent
13228 then
13229 Append_Elmt (Old_C, Comp_List);
13230 end if;
13232 Next_Component (Old_C);
13233 end loop;
13234 end if;
13236 Create_All_Components;
13238 else
13239 -- If discriminants are not static, or if this is a multi-level type
13240 -- extension, we have to include all components of the parent type.
13242 Old_C := First_Component (Typ);
13243 while Present (Old_C) loop
13244 New_C := Create_Component (Old_C);
13246 Set_Etype
13247 (New_C,
13248 Constrain_Component_Type
13249 (Old_C, Subt, Decl_Node, Typ, Constraints));
13250 Set_Is_Public (New_C, Is_Public (Subt));
13252 Next_Component (Old_C);
13253 end loop;
13254 end if;
13256 End_Scope;
13257 end Create_Constrained_Components;
13259 ------------------------------------------
13260 -- Decimal_Fixed_Point_Type_Declaration --
13261 ------------------------------------------
13263 procedure Decimal_Fixed_Point_Type_Declaration
13264 (T : Entity_Id;
13265 Def : Node_Id)
13267 Loc : constant Source_Ptr := Sloc (Def);
13268 Digs_Expr : constant Node_Id := Digits_Expression (Def);
13269 Delta_Expr : constant Node_Id := Delta_Expression (Def);
13270 Implicit_Base : Entity_Id;
13271 Digs_Val : Uint;
13272 Delta_Val : Ureal;
13273 Scale_Val : Uint;
13274 Bound_Val : Ureal;
13276 begin
13277 Check_SPARK_05_Restriction
13278 ("decimal fixed point type is not allowed", Def);
13279 Check_Restriction (No_Fixed_Point, Def);
13281 -- Create implicit base type
13283 Implicit_Base :=
13284 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
13285 Set_Etype (Implicit_Base, Implicit_Base);
13287 -- Analyze and process delta expression
13289 Analyze_And_Resolve (Delta_Expr, Universal_Real);
13291 Check_Delta_Expression (Delta_Expr);
13292 Delta_Val := Expr_Value_R (Delta_Expr);
13294 -- Check delta is power of 10, and determine scale value from it
13296 declare
13297 Val : Ureal;
13299 begin
13300 Scale_Val := Uint_0;
13301 Val := Delta_Val;
13303 if Val < Ureal_1 then
13304 while Val < Ureal_1 loop
13305 Val := Val * Ureal_10;
13306 Scale_Val := Scale_Val + 1;
13307 end loop;
13309 if Scale_Val > 18 then
13310 Error_Msg_N ("scale exceeds maximum value of 18", Def);
13311 Scale_Val := UI_From_Int (+18);
13312 end if;
13314 else
13315 while Val > Ureal_1 loop
13316 Val := Val / Ureal_10;
13317 Scale_Val := Scale_Val - 1;
13318 end loop;
13320 if Scale_Val < -18 then
13321 Error_Msg_N ("scale is less than minimum value of -18", Def);
13322 Scale_Val := UI_From_Int (-18);
13323 end if;
13324 end if;
13326 if Val /= Ureal_1 then
13327 Error_Msg_N ("delta expression must be a power of 10", Def);
13328 Delta_Val := Ureal_10 ** (-Scale_Val);
13329 end if;
13330 end;
13332 -- Set delta, scale and small (small = delta for decimal type)
13334 Set_Delta_Value (Implicit_Base, Delta_Val);
13335 Set_Scale_Value (Implicit_Base, Scale_Val);
13336 Set_Small_Value (Implicit_Base, Delta_Val);
13338 -- Analyze and process digits expression
13340 Analyze_And_Resolve (Digs_Expr, Any_Integer);
13341 Check_Digits_Expression (Digs_Expr);
13342 Digs_Val := Expr_Value (Digs_Expr);
13344 if Digs_Val > 18 then
13345 Digs_Val := UI_From_Int (+18);
13346 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
13347 end if;
13349 Set_Digits_Value (Implicit_Base, Digs_Val);
13350 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
13352 -- Set range of base type from digits value for now. This will be
13353 -- expanded to represent the true underlying base range by Freeze.
13355 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
13357 -- Note: We leave size as zero for now, size will be set at freeze
13358 -- time. We have to do this for ordinary fixed-point, because the size
13359 -- depends on the specified small, and we might as well do the same for
13360 -- decimal fixed-point.
13362 pragma Assert (Esize (Implicit_Base) = Uint_0);
13364 -- If there are bounds given in the declaration use them as the
13365 -- bounds of the first named subtype.
13367 if Present (Real_Range_Specification (Def)) then
13368 declare
13369 RRS : constant Node_Id := Real_Range_Specification (Def);
13370 Low : constant Node_Id := Low_Bound (RRS);
13371 High : constant Node_Id := High_Bound (RRS);
13372 Low_Val : Ureal;
13373 High_Val : Ureal;
13375 begin
13376 Analyze_And_Resolve (Low, Any_Real);
13377 Analyze_And_Resolve (High, Any_Real);
13378 Check_Real_Bound (Low);
13379 Check_Real_Bound (High);
13380 Low_Val := Expr_Value_R (Low);
13381 High_Val := Expr_Value_R (High);
13383 if Low_Val < (-Bound_Val) then
13384 Error_Msg_N
13385 ("range low bound too small for digits value", Low);
13386 Low_Val := -Bound_Val;
13387 end if;
13389 if High_Val > Bound_Val then
13390 Error_Msg_N
13391 ("range high bound too large for digits value", High);
13392 High_Val := Bound_Val;
13393 end if;
13395 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
13396 end;
13398 -- If no explicit range, use range that corresponds to given
13399 -- digits value. This will end up as the final range for the
13400 -- first subtype.
13402 else
13403 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
13404 end if;
13406 -- Complete entity for first subtype
13408 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
13409 Set_Etype (T, Implicit_Base);
13410 Set_Size_Info (T, Implicit_Base);
13411 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13412 Set_Digits_Value (T, Digs_Val);
13413 Set_Delta_Value (T, Delta_Val);
13414 Set_Small_Value (T, Delta_Val);
13415 Set_Scale_Value (T, Scale_Val);
13416 Set_Is_Constrained (T);
13417 end Decimal_Fixed_Point_Type_Declaration;
13419 -----------------------------------
13420 -- Derive_Progenitor_Subprograms --
13421 -----------------------------------
13423 procedure Derive_Progenitor_Subprograms
13424 (Parent_Type : Entity_Id;
13425 Tagged_Type : Entity_Id)
13427 E : Entity_Id;
13428 Elmt : Elmt_Id;
13429 Iface : Entity_Id;
13430 Iface_Elmt : Elmt_Id;
13431 Iface_Subp : Entity_Id;
13432 New_Subp : Entity_Id := Empty;
13433 Prim_Elmt : Elmt_Id;
13434 Subp : Entity_Id;
13435 Typ : Entity_Id;
13437 begin
13438 pragma Assert (Ada_Version >= Ada_2005
13439 and then Is_Record_Type (Tagged_Type)
13440 and then Is_Tagged_Type (Tagged_Type)
13441 and then Has_Interfaces (Tagged_Type));
13443 -- Step 1: Transfer to the full-view primitives associated with the
13444 -- partial-view that cover interface primitives. Conceptually this
13445 -- work should be done later by Process_Full_View; done here to
13446 -- simplify its implementation at later stages. It can be safely
13447 -- done here because interfaces must be visible in the partial and
13448 -- private view (RM 7.3(7.3/2)).
13450 -- Small optimization: This work is only required if the parent may
13451 -- have entities whose Alias attribute reference an interface primitive.
13452 -- Such a situation may occur if the parent is an abstract type and the
13453 -- primitive has not been yet overridden or if the parent is a generic
13454 -- formal type covering interfaces.
13456 -- If the tagged type is not abstract, it cannot have abstract
13457 -- primitives (the only entities in the list of primitives of
13458 -- non-abstract tagged types that can reference abstract primitives
13459 -- through its Alias attribute are the internal entities that have
13460 -- attribute Interface_Alias, and these entities are generated later
13461 -- by Add_Internal_Interface_Entities).
13463 if In_Private_Part (Current_Scope)
13464 and then (Is_Abstract_Type (Parent_Type)
13465 or else
13466 Is_Generic_Type (Parent_Type))
13467 then
13468 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
13469 while Present (Elmt) loop
13470 Subp := Node (Elmt);
13472 -- At this stage it is not possible to have entities in the list
13473 -- of primitives that have attribute Interface_Alias.
13475 pragma Assert (No (Interface_Alias (Subp)));
13477 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
13479 if Is_Interface (Typ) then
13480 E := Find_Primitive_Covering_Interface
13481 (Tagged_Type => Tagged_Type,
13482 Iface_Prim => Subp);
13484 if Present (E)
13485 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
13486 then
13487 Replace_Elmt (Elmt, E);
13488 Remove_Homonym (Subp);
13489 end if;
13490 end if;
13492 Next_Elmt (Elmt);
13493 end loop;
13494 end if;
13496 -- Step 2: Add primitives of progenitors that are not implemented by
13497 -- parents of Tagged_Type.
13499 if Present (Interfaces (Base_Type (Tagged_Type))) then
13500 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
13501 while Present (Iface_Elmt) loop
13502 Iface := Node (Iface_Elmt);
13504 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
13505 while Present (Prim_Elmt) loop
13506 Iface_Subp := Node (Prim_Elmt);
13508 -- Exclude derivation of predefined primitives except those
13509 -- that come from source, or are inherited from one that comes
13510 -- from source. Required to catch declarations of equality
13511 -- operators of interfaces. For example:
13513 -- type Iface is interface;
13514 -- function "=" (Left, Right : Iface) return Boolean;
13516 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
13517 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
13518 then
13519 E := Find_Primitive_Covering_Interface
13520 (Tagged_Type => Tagged_Type,
13521 Iface_Prim => Iface_Subp);
13523 -- If not found we derive a new primitive leaving its alias
13524 -- attribute referencing the interface primitive.
13526 if No (E) then
13527 Derive_Subprogram
13528 (New_Subp, Iface_Subp, Tagged_Type, Iface);
13530 -- Ada 2012 (AI05-0197): If the covering primitive's name
13531 -- differs from the name of the interface primitive then it
13532 -- is a private primitive inherited from a parent type. In
13533 -- such case, given that Tagged_Type covers the interface,
13534 -- the inherited private primitive becomes visible. For such
13535 -- purpose we add a new entity that renames the inherited
13536 -- private primitive.
13538 elsif Chars (E) /= Chars (Iface_Subp) then
13539 pragma Assert (Has_Suffix (E, 'P'));
13540 Derive_Subprogram
13541 (New_Subp, Iface_Subp, Tagged_Type, Iface);
13542 Set_Alias (New_Subp, E);
13543 Set_Is_Abstract_Subprogram (New_Subp,
13544 Is_Abstract_Subprogram (E));
13546 -- Propagate to the full view interface entities associated
13547 -- with the partial view.
13549 elsif In_Private_Part (Current_Scope)
13550 and then Present (Alias (E))
13551 and then Alias (E) = Iface_Subp
13552 and then
13553 List_Containing (Parent (E)) /=
13554 Private_Declarations
13555 (Specification
13556 (Unit_Declaration_Node (Current_Scope)))
13557 then
13558 Append_Elmt (E, Primitive_Operations (Tagged_Type));
13559 end if;
13560 end if;
13562 Next_Elmt (Prim_Elmt);
13563 end loop;
13565 Next_Elmt (Iface_Elmt);
13566 end loop;
13567 end if;
13568 end Derive_Progenitor_Subprograms;
13570 -----------------------
13571 -- Derive_Subprogram --
13572 -----------------------
13574 procedure Derive_Subprogram
13575 (New_Subp : in out Entity_Id;
13576 Parent_Subp : Entity_Id;
13577 Derived_Type : Entity_Id;
13578 Parent_Type : Entity_Id;
13579 Actual_Subp : Entity_Id := Empty)
13581 Formal : Entity_Id;
13582 -- Formal parameter of parent primitive operation
13584 Formal_Of_Actual : Entity_Id;
13585 -- Formal parameter of actual operation, when the derivation is to
13586 -- create a renaming for a primitive operation of an actual in an
13587 -- instantiation.
13589 New_Formal : Entity_Id;
13590 -- Formal of inherited operation
13592 Visible_Subp : Entity_Id := Parent_Subp;
13594 function Is_Private_Overriding return Boolean;
13595 -- If Subp is a private overriding of a visible operation, the inherited
13596 -- operation derives from the overridden op (even though its body is the
13597 -- overriding one) and the inherited operation is visible now. See
13598 -- sem_disp to see the full details of the handling of the overridden
13599 -- subprogram, which is removed from the list of primitive operations of
13600 -- the type. The overridden subprogram is saved locally in Visible_Subp,
13601 -- and used to diagnose abstract operations that need overriding in the
13602 -- derived type.
13604 procedure Replace_Type (Id, New_Id : Entity_Id);
13605 -- When the type is an anonymous access type, create a new access type
13606 -- designating the derived type.
13608 procedure Set_Derived_Name;
13609 -- This procedure sets the appropriate Chars name for New_Subp. This
13610 -- is normally just a copy of the parent name. An exception arises for
13611 -- type support subprograms, where the name is changed to reflect the
13612 -- name of the derived type, e.g. if type foo is derived from type bar,
13613 -- then a procedure barDA is derived with a name fooDA.
13615 ---------------------------
13616 -- Is_Private_Overriding --
13617 ---------------------------
13619 function Is_Private_Overriding return Boolean is
13620 Prev : Entity_Id;
13622 begin
13623 -- If the parent is not a dispatching operation there is no
13624 -- need to investigate overridings
13626 if not Is_Dispatching_Operation (Parent_Subp) then
13627 return False;
13628 end if;
13630 -- The visible operation that is overridden is a homonym of the
13631 -- parent subprogram. We scan the homonym chain to find the one
13632 -- whose alias is the subprogram we are deriving.
13634 Prev := Current_Entity (Parent_Subp);
13635 while Present (Prev) loop
13636 if Ekind (Prev) = Ekind (Parent_Subp)
13637 and then Alias (Prev) = Parent_Subp
13638 and then Scope (Parent_Subp) = Scope (Prev)
13639 and then not Is_Hidden (Prev)
13640 then
13641 Visible_Subp := Prev;
13642 return True;
13643 end if;
13645 Prev := Homonym (Prev);
13646 end loop;
13648 return False;
13649 end Is_Private_Overriding;
13651 ------------------
13652 -- Replace_Type --
13653 ------------------
13655 procedure Replace_Type (Id, New_Id : Entity_Id) is
13656 Id_Type : constant Entity_Id := Etype (Id);
13657 Acc_Type : Entity_Id;
13658 Par : constant Node_Id := Parent (Derived_Type);
13660 begin
13661 -- When the type is an anonymous access type, create a new access
13662 -- type designating the derived type. This itype must be elaborated
13663 -- at the point of the derivation, not on subsequent calls that may
13664 -- be out of the proper scope for Gigi, so we insert a reference to
13665 -- it after the derivation.
13667 if Ekind (Id_Type) = E_Anonymous_Access_Type then
13668 declare
13669 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
13671 begin
13672 if Ekind (Desig_Typ) = E_Record_Type_With_Private
13673 and then Present (Full_View (Desig_Typ))
13674 and then not Is_Private_Type (Parent_Type)
13675 then
13676 Desig_Typ := Full_View (Desig_Typ);
13677 end if;
13679 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
13681 -- Ada 2005 (AI-251): Handle also derivations of abstract
13682 -- interface primitives.
13684 or else (Is_Interface (Desig_Typ)
13685 and then not Is_Class_Wide_Type (Desig_Typ))
13686 then
13687 Acc_Type := New_Copy (Id_Type);
13688 Set_Etype (Acc_Type, Acc_Type);
13689 Set_Scope (Acc_Type, New_Subp);
13691 -- Set size of anonymous access type. If we have an access
13692 -- to an unconstrained array, this is a fat pointer, so it
13693 -- is sizes at twice addtress size.
13695 if Is_Array_Type (Desig_Typ)
13696 and then not Is_Constrained (Desig_Typ)
13697 then
13698 Init_Size (Acc_Type, 2 * System_Address_Size);
13700 -- Other cases use a thin pointer
13702 else
13703 Init_Size (Acc_Type, System_Address_Size);
13704 end if;
13706 -- Set remaining characterstics of anonymous access type
13708 Init_Alignment (Acc_Type);
13709 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
13711 Set_Etype (New_Id, Acc_Type);
13712 Set_Scope (New_Id, New_Subp);
13714 -- Create a reference to it
13716 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
13718 else
13719 Set_Etype (New_Id, Id_Type);
13720 end if;
13721 end;
13723 -- In Ada2012, a formal may have an incomplete type but the type
13724 -- derivation that inherits the primitive follows the full view.
13726 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
13727 or else
13728 (Ekind (Id_Type) = E_Record_Type_With_Private
13729 and then Present (Full_View (Id_Type))
13730 and then
13731 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
13732 or else
13733 (Ada_Version >= Ada_2012
13734 and then Ekind (Id_Type) = E_Incomplete_Type
13735 and then Full_View (Id_Type) = Parent_Type)
13736 then
13737 -- Constraint checks on formals are generated during expansion,
13738 -- based on the signature of the original subprogram. The bounds
13739 -- of the derived type are not relevant, and thus we can use
13740 -- the base type for the formals. However, the return type may be
13741 -- used in a context that requires that the proper static bounds
13742 -- be used (a case statement, for example) and for those cases
13743 -- we must use the derived type (first subtype), not its base.
13745 -- If the derived_type_definition has no constraints, we know that
13746 -- the derived type has the same constraints as the first subtype
13747 -- of the parent, and we can also use it rather than its base,
13748 -- which can lead to more efficient code.
13750 if Etype (Id) = Parent_Type then
13751 if Is_Scalar_Type (Parent_Type)
13752 and then
13753 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
13754 then
13755 Set_Etype (New_Id, Derived_Type);
13757 elsif Nkind (Par) = N_Full_Type_Declaration
13758 and then
13759 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
13760 and then
13761 Is_Entity_Name
13762 (Subtype_Indication (Type_Definition (Par)))
13763 then
13764 Set_Etype (New_Id, Derived_Type);
13766 else
13767 Set_Etype (New_Id, Base_Type (Derived_Type));
13768 end if;
13770 else
13771 Set_Etype (New_Id, Base_Type (Derived_Type));
13772 end if;
13774 else
13775 Set_Etype (New_Id, Etype (Id));
13776 end if;
13777 end Replace_Type;
13779 ----------------------
13780 -- Set_Derived_Name --
13781 ----------------------
13783 procedure Set_Derived_Name is
13784 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
13785 begin
13786 if Nm = TSS_Null then
13787 Set_Chars (New_Subp, Chars (Parent_Subp));
13788 else
13789 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
13790 end if;
13791 end Set_Derived_Name;
13793 -- Start of processing for Derive_Subprogram
13795 begin
13796 New_Subp :=
13797 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
13798 Set_Ekind (New_Subp, Ekind (Parent_Subp));
13799 Set_Contract (New_Subp, Make_Contract (Sloc (New_Subp)));
13801 -- Check whether the inherited subprogram is a private operation that
13802 -- should be inherited but not yet made visible. Such subprograms can
13803 -- become visible at a later point (e.g., the private part of a public
13804 -- child unit) via Declare_Inherited_Private_Subprograms. If the
13805 -- following predicate is true, then this is not such a private
13806 -- operation and the subprogram simply inherits the name of the parent
13807 -- subprogram. Note the special check for the names of controlled
13808 -- operations, which are currently exempted from being inherited with
13809 -- a hidden name because they must be findable for generation of
13810 -- implicit run-time calls.
13812 if not Is_Hidden (Parent_Subp)
13813 or else Is_Internal (Parent_Subp)
13814 or else Is_Private_Overriding
13815 or else Is_Internal_Name (Chars (Parent_Subp))
13816 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
13817 Name_Adjust,
13818 Name_Finalize)
13819 then
13820 Set_Derived_Name;
13822 -- An inherited dispatching equality will be overridden by an internally
13823 -- generated one, or by an explicit one, so preserve its name and thus
13824 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
13825 -- private operation it may become invisible if the full view has
13826 -- progenitors, and the dispatch table will be malformed.
13827 -- We check that the type is limited to handle the anomalous declaration
13828 -- of Limited_Controlled, which is derived from a non-limited type, and
13829 -- which is handled specially elsewhere as well.
13831 elsif Chars (Parent_Subp) = Name_Op_Eq
13832 and then Is_Dispatching_Operation (Parent_Subp)
13833 and then Etype (Parent_Subp) = Standard_Boolean
13834 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
13835 and then
13836 Etype (First_Formal (Parent_Subp)) =
13837 Etype (Next_Formal (First_Formal (Parent_Subp)))
13838 then
13839 Set_Derived_Name;
13841 -- If parent is hidden, this can be a regular derivation if the
13842 -- parent is immediately visible in a non-instantiating context,
13843 -- or if we are in the private part of an instance. This test
13844 -- should still be refined ???
13846 -- The test for In_Instance_Not_Visible avoids inheriting the derived
13847 -- operation as a non-visible operation in cases where the parent
13848 -- subprogram might not be visible now, but was visible within the
13849 -- original generic, so it would be wrong to make the inherited
13850 -- subprogram non-visible now. (Not clear if this test is fully
13851 -- correct; are there any cases where we should declare the inherited
13852 -- operation as not visible to avoid it being overridden, e.g., when
13853 -- the parent type is a generic actual with private primitives ???)
13855 -- (they should be treated the same as other private inherited
13856 -- subprograms, but it's not clear how to do this cleanly). ???
13858 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
13859 and then Is_Immediately_Visible (Parent_Subp)
13860 and then not In_Instance)
13861 or else In_Instance_Not_Visible
13862 then
13863 Set_Derived_Name;
13865 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
13866 -- overrides an interface primitive because interface primitives
13867 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
13869 elsif Ada_Version >= Ada_2005
13870 and then Is_Dispatching_Operation (Parent_Subp)
13871 and then Covers_Some_Interface (Parent_Subp)
13872 then
13873 Set_Derived_Name;
13875 -- Otherwise, the type is inheriting a private operation, so enter
13876 -- it with a special name so it can't be overridden.
13878 else
13879 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
13880 end if;
13882 Set_Parent (New_Subp, Parent (Derived_Type));
13884 if Present (Actual_Subp) then
13885 Replace_Type (Actual_Subp, New_Subp);
13886 else
13887 Replace_Type (Parent_Subp, New_Subp);
13888 end if;
13890 Conditional_Delay (New_Subp, Parent_Subp);
13892 -- If we are creating a renaming for a primitive operation of an
13893 -- actual of a generic derived type, we must examine the signature
13894 -- of the actual primitive, not that of the generic formal, which for
13895 -- example may be an interface. However the name and initial value
13896 -- of the inherited operation are those of the formal primitive.
13898 Formal := First_Formal (Parent_Subp);
13900 if Present (Actual_Subp) then
13901 Formal_Of_Actual := First_Formal (Actual_Subp);
13902 else
13903 Formal_Of_Actual := Empty;
13904 end if;
13906 while Present (Formal) loop
13907 New_Formal := New_Copy (Formal);
13909 -- Normally we do not go copying parents, but in the case of
13910 -- formals, we need to link up to the declaration (which is the
13911 -- parameter specification), and it is fine to link up to the
13912 -- original formal's parameter specification in this case.
13914 Set_Parent (New_Formal, Parent (Formal));
13915 Append_Entity (New_Formal, New_Subp);
13917 if Present (Formal_Of_Actual) then
13918 Replace_Type (Formal_Of_Actual, New_Formal);
13919 Next_Formal (Formal_Of_Actual);
13920 else
13921 Replace_Type (Formal, New_Formal);
13922 end if;
13924 Next_Formal (Formal);
13925 end loop;
13927 -- If this derivation corresponds to a tagged generic actual, then
13928 -- primitive operations rename those of the actual. Otherwise the
13929 -- primitive operations rename those of the parent type, If the parent
13930 -- renames an intrinsic operator, so does the new subprogram. We except
13931 -- concatenation, which is always properly typed, and does not get
13932 -- expanded as other intrinsic operations.
13934 if No (Actual_Subp) then
13935 if Is_Intrinsic_Subprogram (Parent_Subp) then
13936 Set_Is_Intrinsic_Subprogram (New_Subp);
13938 if Present (Alias (Parent_Subp))
13939 and then Chars (Parent_Subp) /= Name_Op_Concat
13940 then
13941 Set_Alias (New_Subp, Alias (Parent_Subp));
13942 else
13943 Set_Alias (New_Subp, Parent_Subp);
13944 end if;
13946 else
13947 Set_Alias (New_Subp, Parent_Subp);
13948 end if;
13950 else
13951 Set_Alias (New_Subp, Actual_Subp);
13952 end if;
13954 -- Derived subprograms of a tagged type must inherit the convention
13955 -- of the parent subprogram (a requirement of AI-117). Derived
13956 -- subprograms of untagged types simply get convention Ada by default.
13958 -- If the derived type is a tagged generic formal type with unknown
13959 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
13961 -- However, if the type is derived from a generic formal, the further
13962 -- inherited subprogram has the convention of the non-generic ancestor.
13963 -- Otherwise there would be no way to override the operation.
13964 -- (This is subject to forthcoming ARG discussions).
13966 if Is_Tagged_Type (Derived_Type) then
13967 if Is_Generic_Type (Derived_Type)
13968 and then Has_Unknown_Discriminants (Derived_Type)
13969 then
13970 Set_Convention (New_Subp, Convention_Intrinsic);
13972 else
13973 if Is_Generic_Type (Parent_Type)
13974 and then Has_Unknown_Discriminants (Parent_Type)
13975 then
13976 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
13977 else
13978 Set_Convention (New_Subp, Convention (Parent_Subp));
13979 end if;
13980 end if;
13981 end if;
13983 -- Predefined controlled operations retain their name even if the parent
13984 -- is hidden (see above), but they are not primitive operations if the
13985 -- ancestor is not visible, for example if the parent is a private
13986 -- extension completed with a controlled extension. Note that a full
13987 -- type that is controlled can break privacy: the flag Is_Controlled is
13988 -- set on both views of the type.
13990 if Is_Controlled (Parent_Type)
13991 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
13992 Name_Adjust,
13993 Name_Finalize)
13994 and then Is_Hidden (Parent_Subp)
13995 and then not Is_Visibly_Controlled (Parent_Type)
13996 then
13997 Set_Is_Hidden (New_Subp);
13998 end if;
14000 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14001 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14003 if Ekind (Parent_Subp) = E_Procedure then
14004 Set_Is_Valued_Procedure
14005 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14006 else
14007 Set_Has_Controlling_Result
14008 (New_Subp, Has_Controlling_Result (Parent_Subp));
14009 end if;
14011 -- No_Return must be inherited properly. If this is overridden in the
14012 -- case of a dispatching operation, then a check is made in Sem_Disp
14013 -- that the overriding operation is also No_Return (no such check is
14014 -- required for the case of non-dispatching operation.
14016 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14018 -- A derived function with a controlling result is abstract. If the
14019 -- Derived_Type is a nonabstract formal generic derived type, then
14020 -- inherited operations are not abstract: the required check is done at
14021 -- instantiation time. If the derivation is for a generic actual, the
14022 -- function is not abstract unless the actual is.
14024 if Is_Generic_Type (Derived_Type)
14025 and then not Is_Abstract_Type (Derived_Type)
14026 then
14027 null;
14029 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14030 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14032 elsif Ada_Version >= Ada_2005
14033 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14034 or else (Is_Tagged_Type (Derived_Type)
14035 and then Etype (New_Subp) = Derived_Type
14036 and then not Is_Null_Extension (Derived_Type))
14037 or else (Is_Tagged_Type (Derived_Type)
14038 and then Ekind (Etype (New_Subp)) =
14039 E_Anonymous_Access_Type
14040 and then Designated_Type (Etype (New_Subp)) =
14041 Derived_Type
14042 and then not Is_Null_Extension (Derived_Type)))
14043 and then No (Actual_Subp)
14044 then
14045 if not Is_Tagged_Type (Derived_Type)
14046 or else Is_Abstract_Type (Derived_Type)
14047 or else Is_Abstract_Subprogram (Alias (New_Subp))
14048 then
14049 Set_Is_Abstract_Subprogram (New_Subp);
14050 else
14051 Set_Requires_Overriding (New_Subp);
14052 end if;
14054 elsif Ada_Version < Ada_2005
14055 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14056 or else (Is_Tagged_Type (Derived_Type)
14057 and then Etype (New_Subp) = Derived_Type
14058 and then No (Actual_Subp)))
14059 then
14060 Set_Is_Abstract_Subprogram (New_Subp);
14062 -- AI05-0097 : an inherited operation that dispatches on result is
14063 -- abstract if the derived type is abstract, even if the parent type
14064 -- is concrete and the derived type is a null extension.
14066 elsif Has_Controlling_Result (Alias (New_Subp))
14067 and then Is_Abstract_Type (Etype (New_Subp))
14068 then
14069 Set_Is_Abstract_Subprogram (New_Subp);
14071 -- Finally, if the parent type is abstract we must verify that all
14072 -- inherited operations are either non-abstract or overridden, or that
14073 -- the derived type itself is abstract (this check is performed at the
14074 -- end of a package declaration, in Check_Abstract_Overriding). A
14075 -- private overriding in the parent type will not be visible in the
14076 -- derivation if we are not in an inner package or in a child unit of
14077 -- the parent type, in which case the abstractness of the inherited
14078 -- operation is carried to the new subprogram.
14080 elsif Is_Abstract_Type (Parent_Type)
14081 and then not In_Open_Scopes (Scope (Parent_Type))
14082 and then Is_Private_Overriding
14083 and then Is_Abstract_Subprogram (Visible_Subp)
14084 then
14085 if No (Actual_Subp) then
14086 Set_Alias (New_Subp, Visible_Subp);
14087 Set_Is_Abstract_Subprogram (New_Subp, True);
14089 else
14090 -- If this is a derivation for an instance of a formal derived
14091 -- type, abstractness comes from the primitive operation of the
14092 -- actual, not from the operation inherited from the ancestor.
14094 Set_Is_Abstract_Subprogram
14095 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14096 end if;
14097 end if;
14099 New_Overloaded_Entity (New_Subp, Derived_Type);
14101 -- Check for case of a derived subprogram for the instantiation of a
14102 -- formal derived tagged type, if so mark the subprogram as dispatching
14103 -- and inherit the dispatching attributes of the actual subprogram. The
14104 -- derived subprogram is effectively renaming of the actual subprogram,
14105 -- so it needs to have the same attributes as the actual.
14107 if Present (Actual_Subp)
14108 and then Is_Dispatching_Operation (Actual_Subp)
14109 then
14110 Set_Is_Dispatching_Operation (New_Subp);
14112 if Present (DTC_Entity (Actual_Subp)) then
14113 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14114 Set_DT_Position (New_Subp, DT_Position (Actual_Subp));
14115 end if;
14116 end if;
14118 -- Indicate that a derived subprogram does not require a body and that
14119 -- it does not require processing of default expressions.
14121 Set_Has_Completion (New_Subp);
14122 Set_Default_Expressions_Processed (New_Subp);
14124 if Ekind (New_Subp) = E_Function then
14125 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14126 end if;
14127 end Derive_Subprogram;
14129 ------------------------
14130 -- Derive_Subprograms --
14131 ------------------------
14133 procedure Derive_Subprograms
14134 (Parent_Type : Entity_Id;
14135 Derived_Type : Entity_Id;
14136 Generic_Actual : Entity_Id := Empty)
14138 Op_List : constant Elist_Id :=
14139 Collect_Primitive_Operations (Parent_Type);
14141 function Check_Derived_Type return Boolean;
14142 -- Check that all the entities derived from Parent_Type are found in
14143 -- the list of primitives of Derived_Type exactly in the same order.
14145 procedure Derive_Interface_Subprogram
14146 (New_Subp : in out Entity_Id;
14147 Subp : Entity_Id;
14148 Actual_Subp : Entity_Id);
14149 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14150 -- (which is an interface primitive). If Generic_Actual is present then
14151 -- Actual_Subp is the actual subprogram corresponding with the generic
14152 -- subprogram Subp.
14154 function Check_Derived_Type return Boolean is
14155 E : Entity_Id;
14156 Elmt : Elmt_Id;
14157 List : Elist_Id;
14158 New_Subp : Entity_Id;
14159 Op_Elmt : Elmt_Id;
14160 Subp : Entity_Id;
14162 begin
14163 -- Traverse list of entities in the current scope searching for
14164 -- an incomplete type whose full-view is derived type
14166 E := First_Entity (Scope (Derived_Type));
14167 while Present (E) and then E /= Derived_Type loop
14168 if Ekind (E) = E_Incomplete_Type
14169 and then Present (Full_View (E))
14170 and then Full_View (E) = Derived_Type
14171 then
14172 -- Disable this test if Derived_Type completes an incomplete
14173 -- type because in such case more primitives can be added
14174 -- later to the list of primitives of Derived_Type by routine
14175 -- Process_Incomplete_Dependents
14177 return True;
14178 end if;
14180 E := Next_Entity (E);
14181 end loop;
14183 List := Collect_Primitive_Operations (Derived_Type);
14184 Elmt := First_Elmt (List);
14186 Op_Elmt := First_Elmt (Op_List);
14187 while Present (Op_Elmt) loop
14188 Subp := Node (Op_Elmt);
14189 New_Subp := Node (Elmt);
14191 -- At this early stage Derived_Type has no entities with attribute
14192 -- Interface_Alias. In addition, such primitives are always
14193 -- located at the end of the list of primitives of Parent_Type.
14194 -- Therefore, if found we can safely stop processing pending
14195 -- entities.
14197 exit when Present (Interface_Alias (Subp));
14199 -- Handle hidden entities
14201 if not Is_Predefined_Dispatching_Operation (Subp)
14202 and then Is_Hidden (Subp)
14203 then
14204 if Present (New_Subp)
14205 and then Primitive_Names_Match (Subp, New_Subp)
14206 then
14207 Next_Elmt (Elmt);
14208 end if;
14210 else
14211 if not Present (New_Subp)
14212 or else Ekind (Subp) /= Ekind (New_Subp)
14213 or else not Primitive_Names_Match (Subp, New_Subp)
14214 then
14215 return False;
14216 end if;
14218 Next_Elmt (Elmt);
14219 end if;
14221 Next_Elmt (Op_Elmt);
14222 end loop;
14224 return True;
14225 end Check_Derived_Type;
14227 ---------------------------------
14228 -- Derive_Interface_Subprogram --
14229 ---------------------------------
14231 procedure Derive_Interface_Subprogram
14232 (New_Subp : in out Entity_Id;
14233 Subp : Entity_Id;
14234 Actual_Subp : Entity_Id)
14236 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
14237 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
14239 begin
14240 pragma Assert (Is_Interface (Iface_Type));
14242 Derive_Subprogram
14243 (New_Subp => New_Subp,
14244 Parent_Subp => Iface_Subp,
14245 Derived_Type => Derived_Type,
14246 Parent_Type => Iface_Type,
14247 Actual_Subp => Actual_Subp);
14249 -- Given that this new interface entity corresponds with a primitive
14250 -- of the parent that was not overridden we must leave it associated
14251 -- with its parent primitive to ensure that it will share the same
14252 -- dispatch table slot when overridden.
14254 if No (Actual_Subp) then
14255 Set_Alias (New_Subp, Subp);
14257 -- For instantiations this is not needed since the previous call to
14258 -- Derive_Subprogram leaves the entity well decorated.
14260 else
14261 pragma Assert (Alias (New_Subp) = Actual_Subp);
14262 null;
14263 end if;
14264 end Derive_Interface_Subprogram;
14266 -- Local variables
14268 Alias_Subp : Entity_Id;
14269 Act_List : Elist_Id;
14270 Act_Elmt : Elmt_Id;
14271 Act_Subp : Entity_Id := Empty;
14272 Elmt : Elmt_Id;
14273 Need_Search : Boolean := False;
14274 New_Subp : Entity_Id := Empty;
14275 Parent_Base : Entity_Id;
14276 Subp : Entity_Id;
14278 -- Start of processing for Derive_Subprograms
14280 begin
14281 if Ekind (Parent_Type) = E_Record_Type_With_Private
14282 and then Has_Discriminants (Parent_Type)
14283 and then Present (Full_View (Parent_Type))
14284 then
14285 Parent_Base := Full_View (Parent_Type);
14286 else
14287 Parent_Base := Parent_Type;
14288 end if;
14290 if Present (Generic_Actual) then
14291 Act_List := Collect_Primitive_Operations (Generic_Actual);
14292 Act_Elmt := First_Elmt (Act_List);
14293 else
14294 Act_List := No_Elist;
14295 Act_Elmt := No_Elmt;
14296 end if;
14298 -- Derive primitives inherited from the parent. Note that if the generic
14299 -- actual is present, this is not really a type derivation, it is a
14300 -- completion within an instance.
14302 -- Case 1: Derived_Type does not implement interfaces
14304 if not Is_Tagged_Type (Derived_Type)
14305 or else (not Has_Interfaces (Derived_Type)
14306 and then not (Present (Generic_Actual)
14307 and then Has_Interfaces (Generic_Actual)))
14308 then
14309 Elmt := First_Elmt (Op_List);
14310 while Present (Elmt) loop
14311 Subp := Node (Elmt);
14313 -- Literals are derived earlier in the process of building the
14314 -- derived type, and are skipped here.
14316 if Ekind (Subp) = E_Enumeration_Literal then
14317 null;
14319 -- The actual is a direct descendant and the common primitive
14320 -- operations appear in the same order.
14322 -- If the generic parent type is present, the derived type is an
14323 -- instance of a formal derived type, and within the instance its
14324 -- operations are those of the actual. We derive from the formal
14325 -- type but make the inherited operations aliases of the
14326 -- corresponding operations of the actual.
14328 else
14329 pragma Assert (No (Node (Act_Elmt))
14330 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
14331 and then
14332 Type_Conformant
14333 (Subp, Node (Act_Elmt),
14334 Skip_Controlling_Formals => True)));
14336 Derive_Subprogram
14337 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
14339 if Present (Act_Elmt) then
14340 Next_Elmt (Act_Elmt);
14341 end if;
14342 end if;
14344 Next_Elmt (Elmt);
14345 end loop;
14347 -- Case 2: Derived_Type implements interfaces
14349 else
14350 -- If the parent type has no predefined primitives we remove
14351 -- predefined primitives from the list of primitives of generic
14352 -- actual to simplify the complexity of this algorithm.
14354 if Present (Generic_Actual) then
14355 declare
14356 Has_Predefined_Primitives : Boolean := False;
14358 begin
14359 -- Check if the parent type has predefined primitives
14361 Elmt := First_Elmt (Op_List);
14362 while Present (Elmt) loop
14363 Subp := Node (Elmt);
14365 if Is_Predefined_Dispatching_Operation (Subp)
14366 and then not Comes_From_Source (Ultimate_Alias (Subp))
14367 then
14368 Has_Predefined_Primitives := True;
14369 exit;
14370 end if;
14372 Next_Elmt (Elmt);
14373 end loop;
14375 -- Remove predefined primitives of Generic_Actual. We must use
14376 -- an auxiliary list because in case of tagged types the value
14377 -- returned by Collect_Primitive_Operations is the value stored
14378 -- in its Primitive_Operations attribute (and we don't want to
14379 -- modify its current contents).
14381 if not Has_Predefined_Primitives then
14382 declare
14383 Aux_List : constant Elist_Id := New_Elmt_List;
14385 begin
14386 Elmt := First_Elmt (Act_List);
14387 while Present (Elmt) loop
14388 Subp := Node (Elmt);
14390 if not Is_Predefined_Dispatching_Operation (Subp)
14391 or else Comes_From_Source (Subp)
14392 then
14393 Append_Elmt (Subp, Aux_List);
14394 end if;
14396 Next_Elmt (Elmt);
14397 end loop;
14399 Act_List := Aux_List;
14400 end;
14401 end if;
14403 Act_Elmt := First_Elmt (Act_List);
14404 Act_Subp := Node (Act_Elmt);
14405 end;
14406 end if;
14408 -- Stage 1: If the generic actual is not present we derive the
14409 -- primitives inherited from the parent type. If the generic parent
14410 -- type is present, the derived type is an instance of a formal
14411 -- derived type, and within the instance its operations are those of
14412 -- the actual. We derive from the formal type but make the inherited
14413 -- operations aliases of the corresponding operations of the actual.
14415 Elmt := First_Elmt (Op_List);
14416 while Present (Elmt) loop
14417 Subp := Node (Elmt);
14418 Alias_Subp := Ultimate_Alias (Subp);
14420 -- Do not derive internal entities of the parent that link
14421 -- interface primitives with their covering primitive. These
14422 -- entities will be added to this type when frozen.
14424 if Present (Interface_Alias (Subp)) then
14425 goto Continue;
14426 end if;
14428 -- If the generic actual is present find the corresponding
14429 -- operation in the generic actual. If the parent type is a
14430 -- direct ancestor of the derived type then, even if it is an
14431 -- interface, the operations are inherited from the primary
14432 -- dispatch table and are in the proper order. If we detect here
14433 -- that primitives are not in the same order we traverse the list
14434 -- of primitive operations of the actual to find the one that
14435 -- implements the interface primitive.
14437 if Need_Search
14438 or else
14439 (Present (Generic_Actual)
14440 and then Present (Act_Subp)
14441 and then not
14442 (Primitive_Names_Match (Subp, Act_Subp)
14443 and then
14444 Type_Conformant (Subp, Act_Subp,
14445 Skip_Controlling_Formals => True)))
14446 then
14447 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
14448 Use_Full_View => True));
14450 -- Remember that we need searching for all pending primitives
14452 Need_Search := True;
14454 -- Handle entities associated with interface primitives
14456 if Present (Alias_Subp)
14457 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
14458 and then not Is_Predefined_Dispatching_Operation (Subp)
14459 then
14460 -- Search for the primitive in the homonym chain
14462 Act_Subp :=
14463 Find_Primitive_Covering_Interface
14464 (Tagged_Type => Generic_Actual,
14465 Iface_Prim => Alias_Subp);
14467 -- Previous search may not locate primitives covering
14468 -- interfaces defined in generics units or instantiations.
14469 -- (it fails if the covering primitive has formals whose
14470 -- type is also defined in generics or instantiations).
14471 -- In such case we search in the list of primitives of the
14472 -- generic actual for the internal entity that links the
14473 -- interface primitive and the covering primitive.
14475 if No (Act_Subp)
14476 and then Is_Generic_Type (Parent_Type)
14477 then
14478 -- This code has been designed to handle only generic
14479 -- formals that implement interfaces that are defined
14480 -- in a generic unit or instantiation. If this code is
14481 -- needed for other cases we must review it because
14482 -- (given that it relies on Original_Location to locate
14483 -- the primitive of Generic_Actual that covers the
14484 -- interface) it could leave linked through attribute
14485 -- Alias entities of unrelated instantiations).
14487 pragma Assert
14488 (Is_Generic_Unit
14489 (Scope (Find_Dispatching_Type (Alias_Subp)))
14490 or else
14491 Instantiation_Depth
14492 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
14494 declare
14495 Iface_Prim_Loc : constant Source_Ptr :=
14496 Original_Location (Sloc (Alias_Subp));
14498 Elmt : Elmt_Id;
14499 Prim : Entity_Id;
14501 begin
14502 Elmt :=
14503 First_Elmt (Primitive_Operations (Generic_Actual));
14505 Search : while Present (Elmt) loop
14506 Prim := Node (Elmt);
14508 if Present (Interface_Alias (Prim))
14509 and then Original_Location
14510 (Sloc (Interface_Alias (Prim))) =
14511 Iface_Prim_Loc
14512 then
14513 Act_Subp := Alias (Prim);
14514 exit Search;
14515 end if;
14517 Next_Elmt (Elmt);
14518 end loop Search;
14519 end;
14520 end if;
14522 pragma Assert (Present (Act_Subp)
14523 or else Is_Abstract_Type (Generic_Actual)
14524 or else Serious_Errors_Detected > 0);
14526 -- Handle predefined primitives plus the rest of user-defined
14527 -- primitives
14529 else
14530 Act_Elmt := First_Elmt (Act_List);
14531 while Present (Act_Elmt) loop
14532 Act_Subp := Node (Act_Elmt);
14534 exit when Primitive_Names_Match (Subp, Act_Subp)
14535 and then Type_Conformant
14536 (Subp, Act_Subp,
14537 Skip_Controlling_Formals => True)
14538 and then No (Interface_Alias (Act_Subp));
14540 Next_Elmt (Act_Elmt);
14541 end loop;
14543 if No (Act_Elmt) then
14544 Act_Subp := Empty;
14545 end if;
14546 end if;
14547 end if;
14549 -- Case 1: If the parent is a limited interface then it has the
14550 -- predefined primitives of synchronized interfaces. However, the
14551 -- actual type may be a non-limited type and hence it does not
14552 -- have such primitives.
14554 if Present (Generic_Actual)
14555 and then not Present (Act_Subp)
14556 and then Is_Limited_Interface (Parent_Base)
14557 and then Is_Predefined_Interface_Primitive (Subp)
14558 then
14559 null;
14561 -- Case 2: Inherit entities associated with interfaces that were
14562 -- not covered by the parent type. We exclude here null interface
14563 -- primitives because they do not need special management.
14565 -- We also exclude interface operations that are renamings. If the
14566 -- subprogram is an explicit renaming of an interface primitive,
14567 -- it is a regular primitive operation, and the presence of its
14568 -- alias is not relevant: it has to be derived like any other
14569 -- primitive.
14571 elsif Present (Alias (Subp))
14572 and then Nkind (Unit_Declaration_Node (Subp)) /=
14573 N_Subprogram_Renaming_Declaration
14574 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
14575 and then not
14576 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
14577 and then Null_Present (Parent (Alias_Subp)))
14578 then
14579 -- If this is an abstract private type then we transfer the
14580 -- derivation of the interface primitive from the partial view
14581 -- to the full view. This is safe because all the interfaces
14582 -- must be visible in the partial view. Done to avoid adding
14583 -- a new interface derivation to the private part of the
14584 -- enclosing package; otherwise this new derivation would be
14585 -- decorated as hidden when the analysis of the enclosing
14586 -- package completes.
14588 if Is_Abstract_Type (Derived_Type)
14589 and then In_Private_Part (Current_Scope)
14590 and then Has_Private_Declaration (Derived_Type)
14591 then
14592 declare
14593 Partial_View : Entity_Id;
14594 Elmt : Elmt_Id;
14595 Ent : Entity_Id;
14597 begin
14598 Partial_View := First_Entity (Current_Scope);
14599 loop
14600 exit when No (Partial_View)
14601 or else (Has_Private_Declaration (Partial_View)
14602 and then
14603 Full_View (Partial_View) = Derived_Type);
14605 Next_Entity (Partial_View);
14606 end loop;
14608 -- If the partial view was not found then the source code
14609 -- has errors and the derivation is not needed.
14611 if Present (Partial_View) then
14612 Elmt :=
14613 First_Elmt (Primitive_Operations (Partial_View));
14614 while Present (Elmt) loop
14615 Ent := Node (Elmt);
14617 if Present (Alias (Ent))
14618 and then Ultimate_Alias (Ent) = Alias (Subp)
14619 then
14620 Append_Elmt
14621 (Ent, Primitive_Operations (Derived_Type));
14622 exit;
14623 end if;
14625 Next_Elmt (Elmt);
14626 end loop;
14628 -- If the interface primitive was not found in the
14629 -- partial view then this interface primitive was
14630 -- overridden. We add a derivation to activate in
14631 -- Derive_Progenitor_Subprograms the machinery to
14632 -- search for it.
14634 if No (Elmt) then
14635 Derive_Interface_Subprogram
14636 (New_Subp => New_Subp,
14637 Subp => Subp,
14638 Actual_Subp => Act_Subp);
14639 end if;
14640 end if;
14641 end;
14642 else
14643 Derive_Interface_Subprogram
14644 (New_Subp => New_Subp,
14645 Subp => Subp,
14646 Actual_Subp => Act_Subp);
14647 end if;
14649 -- Case 3: Common derivation
14651 else
14652 Derive_Subprogram
14653 (New_Subp => New_Subp,
14654 Parent_Subp => Subp,
14655 Derived_Type => Derived_Type,
14656 Parent_Type => Parent_Base,
14657 Actual_Subp => Act_Subp);
14658 end if;
14660 -- No need to update Act_Elm if we must search for the
14661 -- corresponding operation in the generic actual
14663 if not Need_Search
14664 and then Present (Act_Elmt)
14665 then
14666 Next_Elmt (Act_Elmt);
14667 Act_Subp := Node (Act_Elmt);
14668 end if;
14670 <<Continue>>
14671 Next_Elmt (Elmt);
14672 end loop;
14674 -- Inherit additional operations from progenitors. If the derived
14675 -- type is a generic actual, there are not new primitive operations
14676 -- for the type because it has those of the actual, and therefore
14677 -- nothing needs to be done. The renamings generated above are not
14678 -- primitive operations, and their purpose is simply to make the
14679 -- proper operations visible within an instantiation.
14681 if No (Generic_Actual) then
14682 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
14683 end if;
14684 end if;
14686 -- Final check: Direct descendants must have their primitives in the
14687 -- same order. We exclude from this test untagged types and instances
14688 -- of formal derived types. We skip this test if we have already
14689 -- reported serious errors in the sources.
14691 pragma Assert (not Is_Tagged_Type (Derived_Type)
14692 or else Present (Generic_Actual)
14693 or else Serious_Errors_Detected > 0
14694 or else Check_Derived_Type);
14695 end Derive_Subprograms;
14697 --------------------------------
14698 -- Derived_Standard_Character --
14699 --------------------------------
14701 procedure Derived_Standard_Character
14702 (N : Node_Id;
14703 Parent_Type : Entity_Id;
14704 Derived_Type : Entity_Id)
14706 Loc : constant Source_Ptr := Sloc (N);
14707 Def : constant Node_Id := Type_Definition (N);
14708 Indic : constant Node_Id := Subtype_Indication (Def);
14709 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
14710 Implicit_Base : constant Entity_Id :=
14711 Create_Itype
14712 (E_Enumeration_Type, N, Derived_Type, 'B');
14714 Lo : Node_Id;
14715 Hi : Node_Id;
14717 begin
14718 Discard_Node (Process_Subtype (Indic, N));
14720 Set_Etype (Implicit_Base, Parent_Base);
14721 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
14722 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
14724 Set_Is_Character_Type (Implicit_Base, True);
14725 Set_Has_Delayed_Freeze (Implicit_Base);
14727 -- The bounds of the implicit base are the bounds of the parent base.
14728 -- Note that their type is the parent base.
14730 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
14731 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
14733 Set_Scalar_Range (Implicit_Base,
14734 Make_Range (Loc,
14735 Low_Bound => Lo,
14736 High_Bound => Hi));
14738 Conditional_Delay (Derived_Type, Parent_Type);
14740 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
14741 Set_Etype (Derived_Type, Implicit_Base);
14742 Set_Size_Info (Derived_Type, Parent_Type);
14744 if Unknown_RM_Size (Derived_Type) then
14745 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
14746 end if;
14748 Set_Is_Character_Type (Derived_Type, True);
14750 if Nkind (Indic) /= N_Subtype_Indication then
14752 -- If no explicit constraint, the bounds are those
14753 -- of the parent type.
14755 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
14756 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
14757 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
14758 end if;
14760 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
14762 -- Because the implicit base is used in the conversion of the bounds, we
14763 -- have to freeze it now. This is similar to what is done for numeric
14764 -- types, and it equally suspicious, but otherwise a non-static bound
14765 -- will have a reference to an unfrozen type, which is rejected by Gigi
14766 -- (???). This requires specific care for definition of stream
14767 -- attributes. For details, see comments at the end of
14768 -- Build_Derived_Numeric_Type.
14770 Freeze_Before (N, Implicit_Base);
14771 end Derived_Standard_Character;
14773 ------------------------------
14774 -- Derived_Type_Declaration --
14775 ------------------------------
14777 procedure Derived_Type_Declaration
14778 (T : Entity_Id;
14779 N : Node_Id;
14780 Is_Completion : Boolean)
14782 Parent_Type : Entity_Id;
14784 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
14785 -- Check whether the parent type is a generic formal, or derives
14786 -- directly or indirectly from one.
14788 ------------------------
14789 -- Comes_From_Generic --
14790 ------------------------
14792 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
14793 begin
14794 if Is_Generic_Type (Typ) then
14795 return True;
14797 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
14798 return True;
14800 elsif Is_Private_Type (Typ)
14801 and then Present (Full_View (Typ))
14802 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
14803 then
14804 return True;
14806 elsif Is_Generic_Actual_Type (Typ) then
14807 return True;
14809 else
14810 return False;
14811 end if;
14812 end Comes_From_Generic;
14814 -- Local variables
14816 Def : constant Node_Id := Type_Definition (N);
14817 Iface_Def : Node_Id;
14818 Indic : constant Node_Id := Subtype_Indication (Def);
14819 Extension : constant Node_Id := Record_Extension_Part (Def);
14820 Parent_Node : Node_Id;
14821 Taggd : Boolean;
14823 -- Start of processing for Derived_Type_Declaration
14825 begin
14826 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
14828 -- Ada 2005 (AI-251): In case of interface derivation check that the
14829 -- parent is also an interface.
14831 if Interface_Present (Def) then
14832 Check_SPARK_05_Restriction ("interface is not allowed", Def);
14834 if not Is_Interface (Parent_Type) then
14835 Diagnose_Interface (Indic, Parent_Type);
14837 else
14838 Parent_Node := Parent (Base_Type (Parent_Type));
14839 Iface_Def := Type_Definition (Parent_Node);
14841 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
14842 -- other limited interfaces.
14844 if Limited_Present (Def) then
14845 if Limited_Present (Iface_Def) then
14846 null;
14848 elsif Protected_Present (Iface_Def) then
14849 Error_Msg_NE
14850 ("descendant of& must be declared"
14851 & " as a protected interface",
14852 N, Parent_Type);
14854 elsif Synchronized_Present (Iface_Def) then
14855 Error_Msg_NE
14856 ("descendant of& must be declared"
14857 & " as a synchronized interface",
14858 N, Parent_Type);
14860 elsif Task_Present (Iface_Def) then
14861 Error_Msg_NE
14862 ("descendant of& must be declared as a task interface",
14863 N, Parent_Type);
14865 else
14866 Error_Msg_N
14867 ("(Ada 2005) limited interface cannot "
14868 & "inherit from non-limited interface", Indic);
14869 end if;
14871 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
14872 -- from non-limited or limited interfaces.
14874 elsif not Protected_Present (Def)
14875 and then not Synchronized_Present (Def)
14876 and then not Task_Present (Def)
14877 then
14878 if Limited_Present (Iface_Def) then
14879 null;
14881 elsif Protected_Present (Iface_Def) then
14882 Error_Msg_NE
14883 ("descendant of& must be declared"
14884 & " as a protected interface",
14885 N, Parent_Type);
14887 elsif Synchronized_Present (Iface_Def) then
14888 Error_Msg_NE
14889 ("descendant of& must be declared"
14890 & " as a synchronized interface",
14891 N, Parent_Type);
14893 elsif Task_Present (Iface_Def) then
14894 Error_Msg_NE
14895 ("descendant of& must be declared as a task interface",
14896 N, Parent_Type);
14897 else
14898 null;
14899 end if;
14900 end if;
14901 end if;
14902 end if;
14904 if Is_Tagged_Type (Parent_Type)
14905 and then Is_Concurrent_Type (Parent_Type)
14906 and then not Is_Interface (Parent_Type)
14907 then
14908 Error_Msg_N
14909 ("parent type of a record extension cannot be "
14910 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
14911 Set_Etype (T, Any_Type);
14912 return;
14913 end if;
14915 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
14916 -- interfaces
14918 if Is_Tagged_Type (Parent_Type)
14919 and then Is_Non_Empty_List (Interface_List (Def))
14920 then
14921 declare
14922 Intf : Node_Id;
14923 T : Entity_Id;
14925 begin
14926 Intf := First (Interface_List (Def));
14927 while Present (Intf) loop
14928 T := Find_Type_Of_Subtype_Indic (Intf);
14930 if not Is_Interface (T) then
14931 Diagnose_Interface (Intf, T);
14933 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
14934 -- a limited type from having a nonlimited progenitor.
14936 elsif (Limited_Present (Def)
14937 or else (not Is_Interface (Parent_Type)
14938 and then Is_Limited_Type (Parent_Type)))
14939 and then not Is_Limited_Interface (T)
14940 then
14941 Error_Msg_NE
14942 ("progenitor interface& of limited type must be limited",
14943 N, T);
14944 end if;
14946 Next (Intf);
14947 end loop;
14948 end;
14949 end if;
14951 if Parent_Type = Any_Type
14952 or else Etype (Parent_Type) = Any_Type
14953 or else (Is_Class_Wide_Type (Parent_Type)
14954 and then Etype (Parent_Type) = T)
14955 then
14956 -- If Parent_Type is undefined or illegal, make new type into a
14957 -- subtype of Any_Type, and set a few attributes to prevent cascaded
14958 -- errors. If this is a self-definition, emit error now.
14960 if T = Parent_Type
14961 or else T = Etype (Parent_Type)
14962 then
14963 Error_Msg_N ("type cannot be used in its own definition", Indic);
14964 end if;
14966 Set_Ekind (T, Ekind (Parent_Type));
14967 Set_Etype (T, Any_Type);
14968 Set_Scalar_Range (T, Scalar_Range (Any_Type));
14970 if Is_Tagged_Type (T)
14971 and then Is_Record_Type (T)
14972 then
14973 Set_Direct_Primitive_Operations (T, New_Elmt_List);
14974 end if;
14976 return;
14977 end if;
14979 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
14980 -- an interface is special because the list of interfaces in the full
14981 -- view can be given in any order. For example:
14983 -- type A is interface;
14984 -- type B is interface and A;
14985 -- type D is new B with private;
14986 -- private
14987 -- type D is new A and B with null record; -- 1 --
14989 -- In this case we perform the following transformation of -1-:
14991 -- type D is new B and A with null record;
14993 -- If the parent of the full-view covers the parent of the partial-view
14994 -- we have two possible cases:
14996 -- 1) They have the same parent
14997 -- 2) The parent of the full-view implements some further interfaces
14999 -- In both cases we do not need to perform the transformation. In the
15000 -- first case the source program is correct and the transformation is
15001 -- not needed; in the second case the source program does not fulfill
15002 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15003 -- later.
15005 -- This transformation not only simplifies the rest of the analysis of
15006 -- this type declaration but also simplifies the correct generation of
15007 -- the object layout to the expander.
15009 if In_Private_Part (Current_Scope)
15010 and then Is_Interface (Parent_Type)
15011 then
15012 declare
15013 Iface : Node_Id;
15014 Partial_View : Entity_Id;
15015 Partial_View_Parent : Entity_Id;
15016 New_Iface : Node_Id;
15018 begin
15019 -- Look for the associated private type declaration
15021 Partial_View := First_Entity (Current_Scope);
15022 loop
15023 exit when No (Partial_View)
15024 or else (Has_Private_Declaration (Partial_View)
15025 and then Full_View (Partial_View) = T);
15027 Next_Entity (Partial_View);
15028 end loop;
15030 -- If the partial view was not found then the source code has
15031 -- errors and the transformation is not needed.
15033 if Present (Partial_View) then
15034 Partial_View_Parent := Etype (Partial_View);
15036 -- If the parent of the full-view covers the parent of the
15037 -- partial-view we have nothing else to do.
15039 if Interface_Present_In_Ancestor
15040 (Parent_Type, Partial_View_Parent)
15041 then
15042 null;
15044 -- Traverse the list of interfaces of the full-view to look
15045 -- for the parent of the partial-view and perform the tree
15046 -- transformation.
15048 else
15049 Iface := First (Interface_List (Def));
15050 while Present (Iface) loop
15051 if Etype (Iface) = Etype (Partial_View) then
15052 Rewrite (Subtype_Indication (Def),
15053 New_Copy (Subtype_Indication
15054 (Parent (Partial_View))));
15056 New_Iface :=
15057 Make_Identifier (Sloc (N), Chars (Parent_Type));
15058 Append (New_Iface, Interface_List (Def));
15060 -- Analyze the transformed code
15062 Derived_Type_Declaration (T, N, Is_Completion);
15063 return;
15064 end if;
15066 Next (Iface);
15067 end loop;
15068 end if;
15069 end if;
15070 end;
15071 end if;
15073 -- Only composite types other than array types are allowed to have
15074 -- discriminants.
15076 if Present (Discriminant_Specifications (N)) then
15077 if (Is_Elementary_Type (Parent_Type)
15078 or else Is_Array_Type (Parent_Type))
15079 and then not Error_Posted (N)
15080 then
15081 Error_Msg_N
15082 ("elementary or array type cannot have discriminants",
15083 Defining_Identifier (First (Discriminant_Specifications (N))));
15084 Set_Has_Discriminants (T, False);
15086 -- The type is allowed to have discriminants
15088 else
15089 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15090 end if;
15091 end if;
15093 -- In Ada 83, a derived type defined in a package specification cannot
15094 -- be used for further derivation until the end of its visible part.
15095 -- Note that derivation in the private part of the package is allowed.
15097 if Ada_Version = Ada_83
15098 and then Is_Derived_Type (Parent_Type)
15099 and then In_Visible_Part (Scope (Parent_Type))
15100 then
15101 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15102 Error_Msg_N
15103 ("(Ada 83): premature use of type for derivation", Indic);
15104 end if;
15105 end if;
15107 -- Check for early use of incomplete or private type
15109 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15110 Error_Msg_N ("premature derivation of incomplete type", Indic);
15111 return;
15113 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15114 and then not Comes_From_Generic (Parent_Type))
15115 or else Has_Private_Component (Parent_Type)
15116 then
15117 -- The ancestor type of a formal type can be incomplete, in which
15118 -- case only the operations of the partial view are available in the
15119 -- generic. Subsequent checks may be required when the full view is
15120 -- analyzed to verify that a derivation from a tagged type has an
15121 -- extension.
15123 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15124 null;
15126 elsif No (Underlying_Type (Parent_Type))
15127 or else Has_Private_Component (Parent_Type)
15128 then
15129 Error_Msg_N
15130 ("premature derivation of derived or private type", Indic);
15132 -- Flag the type itself as being in error, this prevents some
15133 -- nasty problems with subsequent uses of the malformed type.
15135 Set_Error_Posted (T);
15137 -- Check that within the immediate scope of an untagged partial
15138 -- view it's illegal to derive from the partial view if the
15139 -- full view is tagged. (7.3(7))
15141 -- We verify that the Parent_Type is a partial view by checking
15142 -- that it is not a Full_Type_Declaration (i.e. a private type or
15143 -- private extension declaration), to distinguish a partial view
15144 -- from a derivation from a private type which also appears as
15145 -- E_Private_Type. If the parent base type is not declared in an
15146 -- enclosing scope there is no need to check.
15148 elsif Present (Full_View (Parent_Type))
15149 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15150 and then not Is_Tagged_Type (Parent_Type)
15151 and then Is_Tagged_Type (Full_View (Parent_Type))
15152 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15153 then
15154 Error_Msg_N
15155 ("premature derivation from type with tagged full view",
15156 Indic);
15157 end if;
15158 end if;
15160 -- Check that form of derivation is appropriate
15162 Taggd := Is_Tagged_Type (Parent_Type);
15164 -- Perhaps the parent type should be changed to the class-wide type's
15165 -- specific type in this case to prevent cascading errors ???
15167 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
15168 Error_Msg_N ("parent type must not be a class-wide type", Indic);
15169 return;
15170 end if;
15172 if Present (Extension) and then not Taggd then
15173 Error_Msg_N
15174 ("type derived from untagged type cannot have extension", Indic);
15176 elsif No (Extension) and then Taggd then
15178 -- If this declaration is within a private part (or body) of a
15179 -- generic instantiation then the derivation is allowed (the parent
15180 -- type can only appear tagged in this case if it's a generic actual
15181 -- type, since it would otherwise have been rejected in the analysis
15182 -- of the generic template).
15184 if not Is_Generic_Actual_Type (Parent_Type)
15185 or else In_Visible_Part (Scope (Parent_Type))
15186 then
15187 if Is_Class_Wide_Type (Parent_Type) then
15188 Error_Msg_N
15189 ("parent type must not be a class-wide type", Indic);
15191 -- Use specific type to prevent cascaded errors.
15193 Parent_Type := Etype (Parent_Type);
15195 else
15196 Error_Msg_N
15197 ("type derived from tagged type must have extension", Indic);
15198 end if;
15199 end if;
15200 end if;
15202 -- AI-443: Synchronized formal derived types require a private
15203 -- extension. There is no point in checking the ancestor type or
15204 -- the progenitors since the construct is wrong to begin with.
15206 if Ada_Version >= Ada_2005
15207 and then Is_Generic_Type (T)
15208 and then Present (Original_Node (N))
15209 then
15210 declare
15211 Decl : constant Node_Id := Original_Node (N);
15213 begin
15214 if Nkind (Decl) = N_Formal_Type_Declaration
15215 and then Nkind (Formal_Type_Definition (Decl)) =
15216 N_Formal_Derived_Type_Definition
15217 and then Synchronized_Present (Formal_Type_Definition (Decl))
15218 and then No (Extension)
15220 -- Avoid emitting a duplicate error message
15222 and then not Error_Posted (Indic)
15223 then
15224 Error_Msg_N
15225 ("synchronized derived type must have extension", N);
15226 end if;
15227 end;
15228 end if;
15230 if Null_Exclusion_Present (Def)
15231 and then not Is_Access_Type (Parent_Type)
15232 then
15233 Error_Msg_N ("null exclusion can only apply to an access type", N);
15234 end if;
15236 -- Avoid deriving parent primitives of underlying record views
15238 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
15239 Derive_Subps => not Is_Underlying_Record_View (T));
15241 -- AI-419: The parent type of an explicitly limited derived type must
15242 -- be a limited type or a limited interface.
15244 if Limited_Present (Def) then
15245 Set_Is_Limited_Record (T);
15247 if Is_Interface (T) then
15248 Set_Is_Limited_Interface (T);
15249 end if;
15251 if not Is_Limited_Type (Parent_Type)
15252 and then
15253 (not Is_Interface (Parent_Type)
15254 or else not Is_Limited_Interface (Parent_Type))
15255 then
15256 -- AI05-0096: a derivation in the private part of an instance is
15257 -- legal if the generic formal is untagged limited, and the actual
15258 -- is non-limited.
15260 if Is_Generic_Actual_Type (Parent_Type)
15261 and then In_Private_Part (Current_Scope)
15262 and then
15263 not Is_Tagged_Type
15264 (Generic_Parent_Type (Parent (Parent_Type)))
15265 then
15266 null;
15268 else
15269 Error_Msg_NE
15270 ("parent type& of limited type must be limited",
15271 N, Parent_Type);
15272 end if;
15273 end if;
15274 end if;
15276 -- In SPARK, there are no derived type definitions other than type
15277 -- extensions of tagged record types.
15279 if No (Extension) then
15280 Check_SPARK_05_Restriction
15281 ("derived type is not allowed", Original_Node (N));
15282 end if;
15283 end Derived_Type_Declaration;
15285 ------------------------
15286 -- Diagnose_Interface --
15287 ------------------------
15289 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
15290 begin
15291 if not Is_Interface (E)
15292 and then E /= Any_Type
15293 then
15294 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
15295 end if;
15296 end Diagnose_Interface;
15298 ----------------------------------
15299 -- Enumeration_Type_Declaration --
15300 ----------------------------------
15302 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15303 Ev : Uint;
15304 L : Node_Id;
15305 R_Node : Node_Id;
15306 B_Node : Node_Id;
15308 begin
15309 -- Create identifier node representing lower bound
15311 B_Node := New_Node (N_Identifier, Sloc (Def));
15312 L := First (Literals (Def));
15313 Set_Chars (B_Node, Chars (L));
15314 Set_Entity (B_Node, L);
15315 Set_Etype (B_Node, T);
15316 Set_Is_Static_Expression (B_Node, True);
15318 R_Node := New_Node (N_Range, Sloc (Def));
15319 Set_Low_Bound (R_Node, B_Node);
15321 Set_Ekind (T, E_Enumeration_Type);
15322 Set_First_Literal (T, L);
15323 Set_Etype (T, T);
15324 Set_Is_Constrained (T);
15326 Ev := Uint_0;
15328 -- Loop through literals of enumeration type setting pos and rep values
15329 -- except that if the Ekind is already set, then it means the literal
15330 -- was already constructed (case of a derived type declaration and we
15331 -- should not disturb the Pos and Rep values.
15333 while Present (L) loop
15334 if Ekind (L) /= E_Enumeration_Literal then
15335 Set_Ekind (L, E_Enumeration_Literal);
15336 Set_Enumeration_Pos (L, Ev);
15337 Set_Enumeration_Rep (L, Ev);
15338 Set_Is_Known_Valid (L, True);
15339 end if;
15341 Set_Etype (L, T);
15342 New_Overloaded_Entity (L);
15343 Generate_Definition (L);
15344 Set_Convention (L, Convention_Intrinsic);
15346 -- Case of character literal
15348 if Nkind (L) = N_Defining_Character_Literal then
15349 Set_Is_Character_Type (T, True);
15351 -- Check violation of No_Wide_Characters
15353 if Restriction_Check_Required (No_Wide_Characters) then
15354 Get_Name_String (Chars (L));
15356 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
15357 Check_Restriction (No_Wide_Characters, L);
15358 end if;
15359 end if;
15360 end if;
15362 Ev := Ev + 1;
15363 Next (L);
15364 end loop;
15366 -- Now create a node representing upper bound
15368 B_Node := New_Node (N_Identifier, Sloc (Def));
15369 Set_Chars (B_Node, Chars (Last (Literals (Def))));
15370 Set_Entity (B_Node, Last (Literals (Def)));
15371 Set_Etype (B_Node, T);
15372 Set_Is_Static_Expression (B_Node, True);
15374 Set_High_Bound (R_Node, B_Node);
15376 -- Initialize various fields of the type. Some of this information
15377 -- may be overwritten later through rep.clauses.
15379 Set_Scalar_Range (T, R_Node);
15380 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
15381 Set_Enum_Esize (T);
15382 Set_Enum_Pos_To_Rep (T, Empty);
15384 -- Set Discard_Names if configuration pragma set, or if there is
15385 -- a parameterless pragma in the current declarative region
15387 if Global_Discard_Names or else Discard_Names (Scope (T)) then
15388 Set_Discard_Names (T);
15389 end if;
15391 -- Process end label if there is one
15393 if Present (Def) then
15394 Process_End_Label (Def, 'e', T);
15395 end if;
15396 end Enumeration_Type_Declaration;
15398 ---------------------------------
15399 -- Expand_To_Stored_Constraint --
15400 ---------------------------------
15402 function Expand_To_Stored_Constraint
15403 (Typ : Entity_Id;
15404 Constraint : Elist_Id) return Elist_Id
15406 Explicitly_Discriminated_Type : Entity_Id;
15407 Expansion : Elist_Id;
15408 Discriminant : Entity_Id;
15410 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
15411 -- Find the nearest type that actually specifies discriminants
15413 ---------------------------------
15414 -- Type_With_Explicit_Discrims --
15415 ---------------------------------
15417 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
15418 Typ : constant E := Base_Type (Id);
15420 begin
15421 if Ekind (Typ) in Incomplete_Or_Private_Kind then
15422 if Present (Full_View (Typ)) then
15423 return Type_With_Explicit_Discrims (Full_View (Typ));
15424 end if;
15426 else
15427 if Has_Discriminants (Typ) then
15428 return Typ;
15429 end if;
15430 end if;
15432 if Etype (Typ) = Typ then
15433 return Empty;
15434 elsif Has_Discriminants (Typ) then
15435 return Typ;
15436 else
15437 return Type_With_Explicit_Discrims (Etype (Typ));
15438 end if;
15440 end Type_With_Explicit_Discrims;
15442 -- Start of processing for Expand_To_Stored_Constraint
15444 begin
15445 if No (Constraint)
15446 or else Is_Empty_Elmt_List (Constraint)
15447 then
15448 return No_Elist;
15449 end if;
15451 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
15453 if No (Explicitly_Discriminated_Type) then
15454 return No_Elist;
15455 end if;
15457 Expansion := New_Elmt_List;
15459 Discriminant :=
15460 First_Stored_Discriminant (Explicitly_Discriminated_Type);
15461 while Present (Discriminant) loop
15462 Append_Elmt
15463 (Get_Discriminant_Value
15464 (Discriminant, Explicitly_Discriminated_Type, Constraint),
15465 To => Expansion);
15466 Next_Stored_Discriminant (Discriminant);
15467 end loop;
15469 return Expansion;
15470 end Expand_To_Stored_Constraint;
15472 ---------------------------
15473 -- Find_Hidden_Interface --
15474 ---------------------------
15476 function Find_Hidden_Interface
15477 (Src : Elist_Id;
15478 Dest : Elist_Id) return Entity_Id
15480 Iface : Entity_Id;
15481 Iface_Elmt : Elmt_Id;
15483 begin
15484 if Present (Src) and then Present (Dest) then
15485 Iface_Elmt := First_Elmt (Src);
15486 while Present (Iface_Elmt) loop
15487 Iface := Node (Iface_Elmt);
15489 if Is_Interface (Iface)
15490 and then not Contain_Interface (Iface, Dest)
15491 then
15492 return Iface;
15493 end if;
15495 Next_Elmt (Iface_Elmt);
15496 end loop;
15497 end if;
15499 return Empty;
15500 end Find_Hidden_Interface;
15502 --------------------
15503 -- Find_Type_Name --
15504 --------------------
15506 function Find_Type_Name (N : Node_Id) return Entity_Id is
15507 Id : constant Entity_Id := Defining_Identifier (N);
15508 Prev : Entity_Id;
15509 New_Id : Entity_Id;
15510 Prev_Par : Node_Id;
15512 procedure Check_Duplicate_Aspects;
15513 -- Check that aspects specified in a completion have not been specified
15514 -- already in the partial view. Type_Invariant and others can be
15515 -- specified on either view but never on both.
15517 procedure Tag_Mismatch;
15518 -- Diagnose a tagged partial view whose full view is untagged.
15519 -- We post the message on the full view, with a reference to
15520 -- the previous partial view. The partial view can be private
15521 -- or incomplete, and these are handled in a different manner,
15522 -- so we determine the position of the error message from the
15523 -- respective slocs of both.
15525 -----------------------------
15526 -- Check_Duplicate_Aspects --
15527 -----------------------------
15528 procedure Check_Duplicate_Aspects is
15529 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
15530 Full_Aspects : constant List_Id := Aspect_Specifications (N);
15531 F_Spec, P_Spec : Node_Id;
15533 begin
15534 if Present (Prev_Aspects) and then Present (Full_Aspects) then
15535 F_Spec := First (Full_Aspects);
15536 while Present (F_Spec) loop
15537 P_Spec := First (Prev_Aspects);
15538 while Present (P_Spec) loop
15540 Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
15541 then
15542 Error_Msg_N
15543 ("aspect already specified in private declaration",
15544 F_Spec);
15545 Remove (F_Spec);
15546 return;
15547 end if;
15549 Next (P_Spec);
15550 end loop;
15552 Next (F_Spec);
15553 end loop;
15554 end if;
15555 end Check_Duplicate_Aspects;
15557 ------------------
15558 -- Tag_Mismatch --
15559 ------------------
15561 procedure Tag_Mismatch is
15562 begin
15563 if Sloc (Prev) < Sloc (Id) then
15564 if Ada_Version >= Ada_2012
15565 and then Nkind (N) = N_Private_Type_Declaration
15566 then
15567 Error_Msg_NE
15568 ("declaration of private } must be a tagged type ", Id, Prev);
15569 else
15570 Error_Msg_NE
15571 ("full declaration of } must be a tagged type ", Id, Prev);
15572 end if;
15574 else
15575 if Ada_Version >= Ada_2012
15576 and then Nkind (N) = N_Private_Type_Declaration
15577 then
15578 Error_Msg_NE
15579 ("declaration of private } must be a tagged type ", Prev, Id);
15580 else
15581 Error_Msg_NE
15582 ("full declaration of } must be a tagged type ", Prev, Id);
15583 end if;
15584 end if;
15585 end Tag_Mismatch;
15587 -- Start of processing for Find_Type_Name
15589 begin
15590 -- Find incomplete declaration, if one was given
15592 Prev := Current_Entity_In_Scope (Id);
15594 -- New type declaration
15596 if No (Prev) then
15597 Enter_Name (Id);
15598 return Id;
15600 -- Previous declaration exists
15602 else
15603 Prev_Par := Parent (Prev);
15605 -- Error if not incomplete/private case except if previous
15606 -- declaration is implicit, etc. Enter_Name will emit error if
15607 -- appropriate.
15609 if not Is_Incomplete_Or_Private_Type (Prev) then
15610 Enter_Name (Id);
15611 New_Id := Id;
15613 -- Check invalid completion of private or incomplete type
15615 elsif not Nkind_In (N, N_Full_Type_Declaration,
15616 N_Task_Type_Declaration,
15617 N_Protected_Type_Declaration)
15618 and then
15619 (Ada_Version < Ada_2012
15620 or else not Is_Incomplete_Type (Prev)
15621 or else not Nkind_In (N, N_Private_Type_Declaration,
15622 N_Private_Extension_Declaration))
15623 then
15624 -- Completion must be a full type declarations (RM 7.3(4))
15626 Error_Msg_Sloc := Sloc (Prev);
15627 Error_Msg_NE ("invalid completion of }", Id, Prev);
15629 -- Set scope of Id to avoid cascaded errors. Entity is never
15630 -- examined again, except when saving globals in generics.
15632 Set_Scope (Id, Current_Scope);
15633 New_Id := Id;
15635 -- If this is a repeated incomplete declaration, no further
15636 -- checks are possible.
15638 if Nkind (N) = N_Incomplete_Type_Declaration then
15639 return Prev;
15640 end if;
15642 -- Case of full declaration of incomplete type
15644 elsif Ekind (Prev) = E_Incomplete_Type
15645 and then (Ada_Version < Ada_2012
15646 or else No (Full_View (Prev))
15647 or else not Is_Private_Type (Full_View (Prev)))
15648 then
15649 -- Indicate that the incomplete declaration has a matching full
15650 -- declaration. The defining occurrence of the incomplete
15651 -- declaration remains the visible one, and the procedure
15652 -- Get_Full_View dereferences it whenever the type is used.
15654 if Present (Full_View (Prev)) then
15655 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
15656 end if;
15658 Set_Full_View (Prev, Id);
15659 Append_Entity (Id, Current_Scope);
15660 Set_Is_Public (Id, Is_Public (Prev));
15661 Set_Is_Internal (Id);
15662 New_Id := Prev;
15664 -- If the incomplete view is tagged, a class_wide type has been
15665 -- created already. Use it for the private type as well, in order
15666 -- to prevent multiple incompatible class-wide types that may be
15667 -- created for self-referential anonymous access components.
15669 if Is_Tagged_Type (Prev)
15670 and then Present (Class_Wide_Type (Prev))
15671 then
15672 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
15673 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
15675 -- If the incomplete type is completed by a private declaration
15676 -- the class-wide type remains associated with the incomplete
15677 -- type, to prevent order-of-elaboration issues in gigi, else
15678 -- we associate the class-wide type with the known full view.
15680 if Nkind (N) /= N_Private_Type_Declaration then
15681 Set_Etype (Class_Wide_Type (Id), Id);
15682 end if;
15683 end if;
15685 -- Case of full declaration of private type
15687 else
15688 -- If the private type was a completion of an incomplete type then
15689 -- update Prev to reference the private type
15691 if Ada_Version >= Ada_2012
15692 and then Ekind (Prev) = E_Incomplete_Type
15693 and then Present (Full_View (Prev))
15694 and then Is_Private_Type (Full_View (Prev))
15695 then
15696 Prev := Full_View (Prev);
15697 Prev_Par := Parent (Prev);
15698 end if;
15700 if Nkind (N) = N_Full_Type_Declaration
15701 and then Nkind_In
15702 (Type_Definition (N), N_Record_Definition,
15703 N_Derived_Type_Definition)
15704 and then Interface_Present (Type_Definition (N))
15705 then
15706 Error_Msg_N
15707 ("completion of private type cannot be an interface", N);
15708 end if;
15710 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
15711 if Etype (Prev) /= Prev then
15713 -- Prev is a private subtype or a derived type, and needs
15714 -- no completion.
15716 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
15717 New_Id := Id;
15719 elsif Ekind (Prev) = E_Private_Type
15720 and then Nkind_In (N, N_Task_Type_Declaration,
15721 N_Protected_Type_Declaration)
15722 then
15723 Error_Msg_N
15724 ("completion of nonlimited type cannot be limited", N);
15726 elsif Ekind (Prev) = E_Record_Type_With_Private
15727 and then Nkind_In (N, N_Task_Type_Declaration,
15728 N_Protected_Type_Declaration)
15729 then
15730 if not Is_Limited_Record (Prev) then
15731 Error_Msg_N
15732 ("completion of nonlimited type cannot be limited", N);
15734 elsif No (Interface_List (N)) then
15735 Error_Msg_N
15736 ("completion of tagged private type must be tagged",
15738 end if;
15739 end if;
15741 -- Ada 2005 (AI-251): Private extension declaration of a task
15742 -- type or a protected type. This case arises when covering
15743 -- interface types.
15745 elsif Nkind_In (N, N_Task_Type_Declaration,
15746 N_Protected_Type_Declaration)
15747 then
15748 null;
15750 elsif Nkind (N) /= N_Full_Type_Declaration
15751 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
15752 then
15753 Error_Msg_N
15754 ("full view of private extension must be an extension", N);
15756 elsif not (Abstract_Present (Parent (Prev)))
15757 and then Abstract_Present (Type_Definition (N))
15758 then
15759 Error_Msg_N
15760 ("full view of non-abstract extension cannot be abstract", N);
15761 end if;
15763 if not In_Private_Part (Current_Scope) then
15764 Error_Msg_N
15765 ("declaration of full view must appear in private part", N);
15766 end if;
15768 if Ada_Version >= Ada_2012 then
15769 Check_Duplicate_Aspects;
15770 end if;
15772 Copy_And_Swap (Prev, Id);
15773 Set_Has_Private_Declaration (Prev);
15774 Set_Has_Private_Declaration (Id);
15776 -- Preserve aspect and iterator flags that may have been set on
15777 -- the partial view.
15779 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
15780 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
15782 -- If no error, propagate freeze_node from private to full view.
15783 -- It may have been generated for an early operational item.
15785 if Present (Freeze_Node (Id))
15786 and then Serious_Errors_Detected = 0
15787 and then No (Full_View (Id))
15788 then
15789 Set_Freeze_Node (Prev, Freeze_Node (Id));
15790 Set_Freeze_Node (Id, Empty);
15791 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
15792 end if;
15794 Set_Full_View (Id, Prev);
15795 New_Id := Prev;
15796 end if;
15798 -- Verify that full declaration conforms to partial one
15800 if Is_Incomplete_Or_Private_Type (Prev)
15801 and then Present (Discriminant_Specifications (Prev_Par))
15802 then
15803 if Present (Discriminant_Specifications (N)) then
15804 if Ekind (Prev) = E_Incomplete_Type then
15805 Check_Discriminant_Conformance (N, Prev, Prev);
15806 else
15807 Check_Discriminant_Conformance (N, Prev, Id);
15808 end if;
15810 else
15811 Error_Msg_N
15812 ("missing discriminants in full type declaration", N);
15814 -- To avoid cascaded errors on subsequent use, share the
15815 -- discriminants of the partial view.
15817 Set_Discriminant_Specifications (N,
15818 Discriminant_Specifications (Prev_Par));
15819 end if;
15820 end if;
15822 -- A prior untagged partial view can have an associated class-wide
15823 -- type due to use of the class attribute, and in this case the full
15824 -- type must also be tagged. This Ada 95 usage is deprecated in favor
15825 -- of incomplete tagged declarations, but we check for it.
15827 if Is_Type (Prev)
15828 and then (Is_Tagged_Type (Prev)
15829 or else Present (Class_Wide_Type (Prev)))
15830 then
15831 -- Ada 2012 (AI05-0162): A private type may be the completion of
15832 -- an incomplete type.
15834 if Ada_Version >= Ada_2012
15835 and then Is_Incomplete_Type (Prev)
15836 and then Nkind_In (N, N_Private_Type_Declaration,
15837 N_Private_Extension_Declaration)
15838 then
15839 -- No need to check private extensions since they are tagged
15841 if Nkind (N) = N_Private_Type_Declaration
15842 and then not Tagged_Present (N)
15843 then
15844 Tag_Mismatch;
15845 end if;
15847 -- The full declaration is either a tagged type (including
15848 -- a synchronized type that implements interfaces) or a
15849 -- type extension, otherwise this is an error.
15851 elsif Nkind_In (N, N_Task_Type_Declaration,
15852 N_Protected_Type_Declaration)
15853 then
15854 if No (Interface_List (N))
15855 and then not Error_Posted (N)
15856 then
15857 Tag_Mismatch;
15858 end if;
15860 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
15862 -- Indicate that the previous declaration (tagged incomplete
15863 -- or private declaration) requires the same on the full one.
15865 if not Tagged_Present (Type_Definition (N)) then
15866 Tag_Mismatch;
15867 Set_Is_Tagged_Type (Id);
15868 end if;
15870 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
15871 if No (Record_Extension_Part (Type_Definition (N))) then
15872 Error_Msg_NE
15873 ("full declaration of } must be a record extension",
15874 Prev, Id);
15876 -- Set some attributes to produce a usable full view
15878 Set_Is_Tagged_Type (Id);
15879 end if;
15881 else
15882 Tag_Mismatch;
15883 end if;
15884 end if;
15886 if Present (Prev)
15887 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
15888 and then Present (Premature_Use (Parent (Prev)))
15889 then
15890 Error_Msg_Sloc := Sloc (N);
15891 Error_Msg_N
15892 ("\full declaration #", Premature_Use (Parent (Prev)));
15893 end if;
15895 return New_Id;
15896 end if;
15897 end Find_Type_Name;
15899 -------------------------
15900 -- Find_Type_Of_Object --
15901 -------------------------
15903 function Find_Type_Of_Object
15904 (Obj_Def : Node_Id;
15905 Related_Nod : Node_Id) return Entity_Id
15907 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
15908 P : Node_Id := Parent (Obj_Def);
15909 T : Entity_Id;
15910 Nam : Name_Id;
15912 begin
15913 -- If the parent is a component_definition node we climb to the
15914 -- component_declaration node
15916 if Nkind (P) = N_Component_Definition then
15917 P := Parent (P);
15918 end if;
15920 -- Case of an anonymous array subtype
15922 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
15923 N_Unconstrained_Array_Definition)
15924 then
15925 T := Empty;
15926 Array_Type_Declaration (T, Obj_Def);
15928 -- Create an explicit subtype whenever possible
15930 elsif Nkind (P) /= N_Component_Declaration
15931 and then Def_Kind = N_Subtype_Indication
15932 then
15933 -- Base name of subtype on object name, which will be unique in
15934 -- the current scope.
15936 -- If this is a duplicate declaration, return base type, to avoid
15937 -- generating duplicate anonymous types.
15939 if Error_Posted (P) then
15940 Analyze (Subtype_Mark (Obj_Def));
15941 return Entity (Subtype_Mark (Obj_Def));
15942 end if;
15944 Nam :=
15945 New_External_Name
15946 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
15948 T := Make_Defining_Identifier (Sloc (P), Nam);
15950 Insert_Action (Obj_Def,
15951 Make_Subtype_Declaration (Sloc (P),
15952 Defining_Identifier => T,
15953 Subtype_Indication => Relocate_Node (Obj_Def)));
15955 -- This subtype may need freezing, and this will not be done
15956 -- automatically if the object declaration is not in declarative
15957 -- part. Since this is an object declaration, the type cannot always
15958 -- be frozen here. Deferred constants do not freeze their type
15959 -- (which often enough will be private).
15961 if Nkind (P) = N_Object_Declaration
15962 and then Constant_Present (P)
15963 and then No (Expression (P))
15964 then
15965 null;
15967 -- Here we freeze the base type of object type to catch premature use
15968 -- of discriminated private type without a full view.
15970 else
15971 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
15972 end if;
15974 -- Ada 2005 AI-406: the object definition in an object declaration
15975 -- can be an access definition.
15977 elsif Def_Kind = N_Access_Definition then
15978 T := Access_Definition (Related_Nod, Obj_Def);
15980 Set_Is_Local_Anonymous_Access
15982 V => (Ada_Version < Ada_2012)
15983 or else (Nkind (P) /= N_Object_Declaration)
15984 or else Is_Library_Level_Entity (Defining_Identifier (P)));
15986 -- Otherwise, the object definition is just a subtype_mark
15988 else
15989 T := Process_Subtype (Obj_Def, Related_Nod);
15991 -- If expansion is disabled an object definition that is an aggregate
15992 -- will not get expanded and may lead to scoping problems in the back
15993 -- end, if the object is referenced in an inner scope. In that case
15994 -- create an itype reference for the object definition now. This
15995 -- may be redundant in some cases, but harmless.
15997 if Is_Itype (T)
15998 and then Nkind (Related_Nod) = N_Object_Declaration
15999 and then ASIS_Mode
16000 then
16001 Build_Itype_Reference (T, Related_Nod);
16002 end if;
16003 end if;
16005 return T;
16006 end Find_Type_Of_Object;
16008 --------------------------------
16009 -- Find_Type_Of_Subtype_Indic --
16010 --------------------------------
16012 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16013 Typ : Entity_Id;
16015 begin
16016 -- Case of subtype mark with a constraint
16018 if Nkind (S) = N_Subtype_Indication then
16019 Find_Type (Subtype_Mark (S));
16020 Typ := Entity (Subtype_Mark (S));
16022 if not
16023 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16024 then
16025 Error_Msg_N
16026 ("incorrect constraint for this kind of type", Constraint (S));
16027 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16028 end if;
16030 -- Otherwise we have a subtype mark without a constraint
16032 elsif Error_Posted (S) then
16033 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16034 return Any_Type;
16036 else
16037 Find_Type (S);
16038 Typ := Entity (S);
16039 end if;
16041 -- Check No_Wide_Characters restriction
16043 Check_Wide_Character_Restriction (Typ, S);
16045 return Typ;
16046 end Find_Type_Of_Subtype_Indic;
16048 -------------------------------------
16049 -- Floating_Point_Type_Declaration --
16050 -------------------------------------
16052 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16053 Digs : constant Node_Id := Digits_Expression (Def);
16054 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16055 Digs_Val : Uint;
16056 Base_Typ : Entity_Id;
16057 Implicit_Base : Entity_Id;
16058 Bound : Node_Id;
16060 function Can_Derive_From (E : Entity_Id) return Boolean;
16061 -- Find if given digits value, and possibly a specified range, allows
16062 -- derivation from specified type
16064 function Find_Base_Type return Entity_Id;
16065 -- Find a predefined base type that Def can derive from, or generate
16066 -- an error and substitute Long_Long_Float if none exists.
16068 ---------------------
16069 -- Can_Derive_From --
16070 ---------------------
16072 function Can_Derive_From (E : Entity_Id) return Boolean is
16073 Spec : constant Entity_Id := Real_Range_Specification (Def);
16075 begin
16076 -- Check specified "digits" constraint
16078 if Digs_Val > Digits_Value (E) then
16079 return False;
16080 end if;
16082 -- Check for matching range, if specified
16084 if Present (Spec) then
16085 if Expr_Value_R (Type_Low_Bound (E)) >
16086 Expr_Value_R (Low_Bound (Spec))
16087 then
16088 return False;
16089 end if;
16091 if Expr_Value_R (Type_High_Bound (E)) <
16092 Expr_Value_R (High_Bound (Spec))
16093 then
16094 return False;
16095 end if;
16096 end if;
16098 return True;
16099 end Can_Derive_From;
16101 --------------------
16102 -- Find_Base_Type --
16103 --------------------
16105 function Find_Base_Type return Entity_Id is
16106 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16108 begin
16109 -- Iterate over the predefined types in order, returning the first
16110 -- one that Def can derive from.
16112 while Present (Choice) loop
16113 if Can_Derive_From (Node (Choice)) then
16114 return Node (Choice);
16115 end if;
16117 Next_Elmt (Choice);
16118 end loop;
16120 -- If we can't derive from any existing type, use Long_Long_Float
16121 -- and give appropriate message explaining the problem.
16123 if Digs_Val > Max_Digs_Val then
16124 -- It might be the case that there is a type with the requested
16125 -- range, just not the combination of digits and range.
16127 Error_Msg_N
16128 ("no predefined type has requested range and precision",
16129 Real_Range_Specification (Def));
16131 else
16132 Error_Msg_N
16133 ("range too large for any predefined type",
16134 Real_Range_Specification (Def));
16135 end if;
16137 return Standard_Long_Long_Float;
16138 end Find_Base_Type;
16140 -- Start of processing for Floating_Point_Type_Declaration
16142 begin
16143 Check_Restriction (No_Floating_Point, Def);
16145 -- Create an implicit base type
16147 Implicit_Base :=
16148 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16150 -- Analyze and verify digits value
16152 Analyze_And_Resolve (Digs, Any_Integer);
16153 Check_Digits_Expression (Digs);
16154 Digs_Val := Expr_Value (Digs);
16156 -- Process possible range spec and find correct type to derive from
16158 Process_Real_Range_Specification (Def);
16160 -- Check that requested number of digits is not too high.
16162 if Digs_Val > Max_Digs_Val then
16163 -- The check for Max_Base_Digits may be somewhat expensive, as it
16164 -- requires reading System, so only do it when necessary.
16166 declare
16167 Max_Base_Digits : constant Uint :=
16168 Expr_Value
16169 (Expression
16170 (Parent (RTE (RE_Max_Base_Digits))));
16172 begin
16173 if Digs_Val > Max_Base_Digits then
16174 Error_Msg_Uint_1 := Max_Base_Digits;
16175 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
16177 elsif No (Real_Range_Specification (Def)) then
16178 Error_Msg_Uint_1 := Max_Digs_Val;
16179 Error_Msg_N ("types with more than ^ digits need range spec "
16180 & "(RM 3.5.7(6))", Digs);
16181 end if;
16182 end;
16183 end if;
16185 -- Find a suitable type to derive from or complain and use a substitute
16187 Base_Typ := Find_Base_Type;
16189 -- If there are bounds given in the declaration use them as the bounds
16190 -- of the type, otherwise use the bounds of the predefined base type
16191 -- that was chosen based on the Digits value.
16193 if Present (Real_Range_Specification (Def)) then
16194 Set_Scalar_Range (T, Real_Range_Specification (Def));
16195 Set_Is_Constrained (T);
16197 -- The bounds of this range must be converted to machine numbers
16198 -- in accordance with RM 4.9(38).
16200 Bound := Type_Low_Bound (T);
16202 if Nkind (Bound) = N_Real_Literal then
16203 Set_Realval
16204 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
16205 Set_Is_Machine_Number (Bound);
16206 end if;
16208 Bound := Type_High_Bound (T);
16210 if Nkind (Bound) = N_Real_Literal then
16211 Set_Realval
16212 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
16213 Set_Is_Machine_Number (Bound);
16214 end if;
16216 else
16217 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
16218 end if;
16220 -- Complete definition of implicit base and declared first subtype
16222 Set_Etype (Implicit_Base, Base_Typ);
16224 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
16225 Set_Size_Info (Implicit_Base, (Base_Typ));
16226 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
16227 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
16228 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
16229 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
16231 Set_Ekind (T, E_Floating_Point_Subtype);
16232 Set_Etype (T, Implicit_Base);
16234 Set_Size_Info (T, (Implicit_Base));
16235 Set_RM_Size (T, RM_Size (Implicit_Base));
16236 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
16237 Set_Digits_Value (T, Digs_Val);
16238 end Floating_Point_Type_Declaration;
16240 ----------------------------
16241 -- Get_Discriminant_Value --
16242 ----------------------------
16244 -- This is the situation:
16246 -- There is a non-derived type
16248 -- type T0 (Dx, Dy, Dz...)
16250 -- There are zero or more levels of derivation, with each derivation
16251 -- either purely inheriting the discriminants, or defining its own.
16253 -- type Ti is new Ti-1
16254 -- or
16255 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
16256 -- or
16257 -- subtype Ti is ...
16259 -- The subtype issue is avoided by the use of Original_Record_Component,
16260 -- and the fact that derived subtypes also derive the constraints.
16262 -- This chain leads back from
16264 -- Typ_For_Constraint
16266 -- Typ_For_Constraint has discriminants, and the value for each
16267 -- discriminant is given by its corresponding Elmt of Constraints.
16269 -- Discriminant is some discriminant in this hierarchy
16271 -- We need to return its value
16273 -- We do this by recursively searching each level, and looking for
16274 -- Discriminant. Once we get to the bottom, we start backing up
16275 -- returning the value for it which may in turn be a discriminant
16276 -- further up, so on the backup we continue the substitution.
16278 function Get_Discriminant_Value
16279 (Discriminant : Entity_Id;
16280 Typ_For_Constraint : Entity_Id;
16281 Constraint : Elist_Id) return Node_Id
16283 function Root_Corresponding_Discriminant
16284 (Discr : Entity_Id) return Entity_Id;
16285 -- Given a discriminant, traverse the chain of inherited discriminants
16286 -- and return the topmost discriminant.
16288 function Search_Derivation_Levels
16289 (Ti : Entity_Id;
16290 Discrim_Values : Elist_Id;
16291 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
16292 -- This is the routine that performs the recursive search of levels
16293 -- as described above.
16295 -------------------------------------
16296 -- Root_Corresponding_Discriminant --
16297 -------------------------------------
16299 function Root_Corresponding_Discriminant
16300 (Discr : Entity_Id) return Entity_Id
16302 D : Entity_Id;
16304 begin
16305 D := Discr;
16306 while Present (Corresponding_Discriminant (D)) loop
16307 D := Corresponding_Discriminant (D);
16308 end loop;
16310 return D;
16311 end Root_Corresponding_Discriminant;
16313 ------------------------------
16314 -- Search_Derivation_Levels --
16315 ------------------------------
16317 function Search_Derivation_Levels
16318 (Ti : Entity_Id;
16319 Discrim_Values : Elist_Id;
16320 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
16322 Assoc : Elmt_Id;
16323 Disc : Entity_Id;
16324 Result : Node_Or_Entity_Id;
16325 Result_Entity : Node_Id;
16327 begin
16328 -- If inappropriate type, return Error, this happens only in
16329 -- cascaded error situations, and we want to avoid a blow up.
16331 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
16332 return Error;
16333 end if;
16335 -- Look deeper if possible. Use Stored_Constraints only for
16336 -- untagged types. For tagged types use the given constraint.
16337 -- This asymmetry needs explanation???
16339 if not Stored_Discrim_Values
16340 and then Present (Stored_Constraint (Ti))
16341 and then not Is_Tagged_Type (Ti)
16342 then
16343 Result :=
16344 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
16345 else
16346 declare
16347 Td : constant Entity_Id := Etype (Ti);
16349 begin
16350 if Td = Ti then
16351 Result := Discriminant;
16353 else
16354 if Present (Stored_Constraint (Ti)) then
16355 Result :=
16356 Search_Derivation_Levels
16357 (Td, Stored_Constraint (Ti), True);
16358 else
16359 Result :=
16360 Search_Derivation_Levels
16361 (Td, Discrim_Values, Stored_Discrim_Values);
16362 end if;
16363 end if;
16364 end;
16365 end if;
16367 -- Extra underlying places to search, if not found above. For
16368 -- concurrent types, the relevant discriminant appears in the
16369 -- corresponding record. For a type derived from a private type
16370 -- without discriminant, the full view inherits the discriminants
16371 -- of the full view of the parent.
16373 if Result = Discriminant then
16374 if Is_Concurrent_Type (Ti)
16375 and then Present (Corresponding_Record_Type (Ti))
16376 then
16377 Result :=
16378 Search_Derivation_Levels (
16379 Corresponding_Record_Type (Ti),
16380 Discrim_Values,
16381 Stored_Discrim_Values);
16383 elsif Is_Private_Type (Ti)
16384 and then not Has_Discriminants (Ti)
16385 and then Present (Full_View (Ti))
16386 and then Etype (Full_View (Ti)) /= Ti
16387 then
16388 Result :=
16389 Search_Derivation_Levels (
16390 Full_View (Ti),
16391 Discrim_Values,
16392 Stored_Discrim_Values);
16393 end if;
16394 end if;
16396 -- If Result is not a (reference to a) discriminant, return it,
16397 -- otherwise set Result_Entity to the discriminant.
16399 if Nkind (Result) = N_Defining_Identifier then
16400 pragma Assert (Result = Discriminant);
16401 Result_Entity := Result;
16403 else
16404 if not Denotes_Discriminant (Result) then
16405 return Result;
16406 end if;
16408 Result_Entity := Entity (Result);
16409 end if;
16411 -- See if this level of derivation actually has discriminants
16412 -- because tagged derivations can add them, hence the lower
16413 -- levels need not have any.
16415 if not Has_Discriminants (Ti) then
16416 return Result;
16417 end if;
16419 -- Scan Ti's discriminants for Result_Entity,
16420 -- and return its corresponding value, if any.
16422 Result_Entity := Original_Record_Component (Result_Entity);
16424 Assoc := First_Elmt (Discrim_Values);
16426 if Stored_Discrim_Values then
16427 Disc := First_Stored_Discriminant (Ti);
16428 else
16429 Disc := First_Discriminant (Ti);
16430 end if;
16432 while Present (Disc) loop
16433 pragma Assert (Present (Assoc));
16435 if Original_Record_Component (Disc) = Result_Entity then
16436 return Node (Assoc);
16437 end if;
16439 Next_Elmt (Assoc);
16441 if Stored_Discrim_Values then
16442 Next_Stored_Discriminant (Disc);
16443 else
16444 Next_Discriminant (Disc);
16445 end if;
16446 end loop;
16448 -- Could not find it
16450 return Result;
16451 end Search_Derivation_Levels;
16453 -- Local Variables
16455 Result : Node_Or_Entity_Id;
16457 -- Start of processing for Get_Discriminant_Value
16459 begin
16460 -- ??? This routine is a gigantic mess and will be deleted. For the
16461 -- time being just test for the trivial case before calling recurse.
16463 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
16464 declare
16465 D : Entity_Id;
16466 E : Elmt_Id;
16468 begin
16469 D := First_Discriminant (Typ_For_Constraint);
16470 E := First_Elmt (Constraint);
16471 while Present (D) loop
16472 if Chars (D) = Chars (Discriminant) then
16473 return Node (E);
16474 end if;
16476 Next_Discriminant (D);
16477 Next_Elmt (E);
16478 end loop;
16479 end;
16480 end if;
16482 Result := Search_Derivation_Levels
16483 (Typ_For_Constraint, Constraint, False);
16485 -- ??? hack to disappear when this routine is gone
16487 if Nkind (Result) = N_Defining_Identifier then
16488 declare
16489 D : Entity_Id;
16490 E : Elmt_Id;
16492 begin
16493 D := First_Discriminant (Typ_For_Constraint);
16494 E := First_Elmt (Constraint);
16495 while Present (D) loop
16496 if Root_Corresponding_Discriminant (D) = Discriminant then
16497 return Node (E);
16498 end if;
16500 Next_Discriminant (D);
16501 Next_Elmt (E);
16502 end loop;
16503 end;
16504 end if;
16506 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
16507 return Result;
16508 end Get_Discriminant_Value;
16510 --------------------------
16511 -- Has_Range_Constraint --
16512 --------------------------
16514 function Has_Range_Constraint (N : Node_Id) return Boolean is
16515 C : constant Node_Id := Constraint (N);
16517 begin
16518 if Nkind (C) = N_Range_Constraint then
16519 return True;
16521 elsif Nkind (C) = N_Digits_Constraint then
16522 return
16523 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
16524 or else
16525 Present (Range_Constraint (C));
16527 elsif Nkind (C) = N_Delta_Constraint then
16528 return Present (Range_Constraint (C));
16530 else
16531 return False;
16532 end if;
16533 end Has_Range_Constraint;
16535 ------------------------
16536 -- Inherit_Components --
16537 ------------------------
16539 function Inherit_Components
16540 (N : Node_Id;
16541 Parent_Base : Entity_Id;
16542 Derived_Base : Entity_Id;
16543 Is_Tagged : Boolean;
16544 Inherit_Discr : Boolean;
16545 Discs : Elist_Id) return Elist_Id
16547 Assoc_List : constant Elist_Id := New_Elmt_List;
16549 procedure Inherit_Component
16550 (Old_C : Entity_Id;
16551 Plain_Discrim : Boolean := False;
16552 Stored_Discrim : Boolean := False);
16553 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
16554 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
16555 -- True, Old_C is a stored discriminant. If they are both false then
16556 -- Old_C is a regular component.
16558 -----------------------
16559 -- Inherit_Component --
16560 -----------------------
16562 procedure Inherit_Component
16563 (Old_C : Entity_Id;
16564 Plain_Discrim : Boolean := False;
16565 Stored_Discrim : Boolean := False)
16567 procedure Set_Anonymous_Type (Id : Entity_Id);
16568 -- Id denotes the entity of an access discriminant or anonymous
16569 -- access component. Set the type of Id to either the same type of
16570 -- Old_C or create a new one depending on whether the parent and
16571 -- the child types are in the same scope.
16573 ------------------------
16574 -- Set_Anonymous_Type --
16575 ------------------------
16577 procedure Set_Anonymous_Type (Id : Entity_Id) is
16578 Old_Typ : constant Entity_Id := Etype (Old_C);
16580 begin
16581 if Scope (Parent_Base) = Scope (Derived_Base) then
16582 Set_Etype (Id, Old_Typ);
16584 -- The parent and the derived type are in two different scopes.
16585 -- Reuse the type of the original discriminant / component by
16586 -- copying it in order to preserve all attributes.
16588 else
16589 declare
16590 Typ : constant Entity_Id := New_Copy (Old_Typ);
16592 begin
16593 Set_Etype (Id, Typ);
16595 -- Since we do not generate component declarations for
16596 -- inherited components, associate the itype with the
16597 -- derived type.
16599 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
16600 Set_Scope (Typ, Derived_Base);
16601 end;
16602 end if;
16603 end Set_Anonymous_Type;
16605 -- Local variables and constants
16607 New_C : constant Entity_Id := New_Copy (Old_C);
16609 Corr_Discrim : Entity_Id;
16610 Discrim : Entity_Id;
16612 -- Start of processing for Inherit_Component
16614 begin
16615 pragma Assert (not Is_Tagged or else not Stored_Discrim);
16617 Set_Parent (New_C, Parent (Old_C));
16619 -- Regular discriminants and components must be inserted in the scope
16620 -- of the Derived_Base. Do it here.
16622 if not Stored_Discrim then
16623 Enter_Name (New_C);
16624 end if;
16626 -- For tagged types the Original_Record_Component must point to
16627 -- whatever this field was pointing to in the parent type. This has
16628 -- already been achieved by the call to New_Copy above.
16630 if not Is_Tagged then
16631 Set_Original_Record_Component (New_C, New_C);
16632 end if;
16634 -- Set the proper type of an access discriminant
16636 if Ekind (New_C) = E_Discriminant
16637 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
16638 then
16639 Set_Anonymous_Type (New_C);
16640 end if;
16642 -- If we have inherited a component then see if its Etype contains
16643 -- references to Parent_Base discriminants. In this case, replace
16644 -- these references with the constraints given in Discs. We do not
16645 -- do this for the partial view of private types because this is
16646 -- not needed (only the components of the full view will be used
16647 -- for code generation) and cause problem. We also avoid this
16648 -- transformation in some error situations.
16650 if Ekind (New_C) = E_Component then
16652 -- Set the proper type of an anonymous access component
16654 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
16655 Set_Anonymous_Type (New_C);
16657 elsif (Is_Private_Type (Derived_Base)
16658 and then not Is_Generic_Type (Derived_Base))
16659 or else (Is_Empty_Elmt_List (Discs)
16660 and then not Expander_Active)
16661 then
16662 Set_Etype (New_C, Etype (Old_C));
16664 else
16665 -- The current component introduces a circularity of the
16666 -- following kind:
16668 -- limited with Pack_2;
16669 -- package Pack_1 is
16670 -- type T_1 is tagged record
16671 -- Comp : access Pack_2.T_2;
16672 -- ...
16673 -- end record;
16674 -- end Pack_1;
16676 -- with Pack_1;
16677 -- package Pack_2 is
16678 -- type T_2 is new Pack_1.T_1 with ...;
16679 -- end Pack_2;
16681 Set_Etype
16682 (New_C,
16683 Constrain_Component_Type
16684 (Old_C, Derived_Base, N, Parent_Base, Discs));
16685 end if;
16686 end if;
16688 -- In derived tagged types it is illegal to reference a non
16689 -- discriminant component in the parent type. To catch this, mark
16690 -- these components with an Ekind of E_Void. This will be reset in
16691 -- Record_Type_Definition after processing the record extension of
16692 -- the derived type.
16694 -- If the declaration is a private extension, there is no further
16695 -- record extension to process, and the components retain their
16696 -- current kind, because they are visible at this point.
16698 if Is_Tagged and then Ekind (New_C) = E_Component
16699 and then Nkind (N) /= N_Private_Extension_Declaration
16700 then
16701 Set_Ekind (New_C, E_Void);
16702 end if;
16704 if Plain_Discrim then
16705 Set_Corresponding_Discriminant (New_C, Old_C);
16706 Build_Discriminal (New_C);
16708 -- If we are explicitly inheriting a stored discriminant it will be
16709 -- completely hidden.
16711 elsif Stored_Discrim then
16712 Set_Corresponding_Discriminant (New_C, Empty);
16713 Set_Discriminal (New_C, Empty);
16714 Set_Is_Completely_Hidden (New_C);
16716 -- Set the Original_Record_Component of each discriminant in the
16717 -- derived base to point to the corresponding stored that we just
16718 -- created.
16720 Discrim := First_Discriminant (Derived_Base);
16721 while Present (Discrim) loop
16722 Corr_Discrim := Corresponding_Discriminant (Discrim);
16724 -- Corr_Discrim could be missing in an error situation
16726 if Present (Corr_Discrim)
16727 and then Original_Record_Component (Corr_Discrim) = Old_C
16728 then
16729 Set_Original_Record_Component (Discrim, New_C);
16730 end if;
16732 Next_Discriminant (Discrim);
16733 end loop;
16735 Append_Entity (New_C, Derived_Base);
16736 end if;
16738 if not Is_Tagged then
16739 Append_Elmt (Old_C, Assoc_List);
16740 Append_Elmt (New_C, Assoc_List);
16741 end if;
16742 end Inherit_Component;
16744 -- Variables local to Inherit_Component
16746 Loc : constant Source_Ptr := Sloc (N);
16748 Parent_Discrim : Entity_Id;
16749 Stored_Discrim : Entity_Id;
16750 D : Entity_Id;
16751 Component : Entity_Id;
16753 -- Start of processing for Inherit_Components
16755 begin
16756 if not Is_Tagged then
16757 Append_Elmt (Parent_Base, Assoc_List);
16758 Append_Elmt (Derived_Base, Assoc_List);
16759 end if;
16761 -- Inherit parent discriminants if needed
16763 if Inherit_Discr then
16764 Parent_Discrim := First_Discriminant (Parent_Base);
16765 while Present (Parent_Discrim) loop
16766 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
16767 Next_Discriminant (Parent_Discrim);
16768 end loop;
16769 end if;
16771 -- Create explicit stored discrims for untagged types when necessary
16773 if not Has_Unknown_Discriminants (Derived_Base)
16774 and then Has_Discriminants (Parent_Base)
16775 and then not Is_Tagged
16776 and then
16777 (not Inherit_Discr
16778 or else First_Discriminant (Parent_Base) /=
16779 First_Stored_Discriminant (Parent_Base))
16780 then
16781 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
16782 while Present (Stored_Discrim) loop
16783 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
16784 Next_Stored_Discriminant (Stored_Discrim);
16785 end loop;
16786 end if;
16788 -- See if we can apply the second transformation for derived types, as
16789 -- explained in point 6. in the comments above Build_Derived_Record_Type
16790 -- This is achieved by appending Derived_Base discriminants into Discs,
16791 -- which has the side effect of returning a non empty Discs list to the
16792 -- caller of Inherit_Components, which is what we want. This must be
16793 -- done for private derived types if there are explicit stored
16794 -- discriminants, to ensure that we can retrieve the values of the
16795 -- constraints provided in the ancestors.
16797 if Inherit_Discr
16798 and then Is_Empty_Elmt_List (Discs)
16799 and then Present (First_Discriminant (Derived_Base))
16800 and then
16801 (not Is_Private_Type (Derived_Base)
16802 or else Is_Completely_Hidden
16803 (First_Stored_Discriminant (Derived_Base))
16804 or else Is_Generic_Type (Derived_Base))
16805 then
16806 D := First_Discriminant (Derived_Base);
16807 while Present (D) loop
16808 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
16809 Next_Discriminant (D);
16810 end loop;
16811 end if;
16813 -- Finally, inherit non-discriminant components unless they are not
16814 -- visible because defined or inherited from the full view of the
16815 -- parent. Don't inherit the _parent field of the parent type.
16817 Component := First_Entity (Parent_Base);
16818 while Present (Component) loop
16820 -- Ada 2005 (AI-251): Do not inherit components associated with
16821 -- secondary tags of the parent.
16823 if Ekind (Component) = E_Component
16824 and then Present (Related_Type (Component))
16825 then
16826 null;
16828 elsif Ekind (Component) /= E_Component
16829 or else Chars (Component) = Name_uParent
16830 then
16831 null;
16833 -- If the derived type is within the parent type's declarative
16834 -- region, then the components can still be inherited even though
16835 -- they aren't visible at this point. This can occur for cases
16836 -- such as within public child units where the components must
16837 -- become visible upon entering the child unit's private part.
16839 elsif not Is_Visible_Component (Component)
16840 and then not In_Open_Scopes (Scope (Parent_Base))
16841 then
16842 null;
16844 elsif Ekind_In (Derived_Base, E_Private_Type,
16845 E_Limited_Private_Type)
16846 then
16847 null;
16849 else
16850 Inherit_Component (Component);
16851 end if;
16853 Next_Entity (Component);
16854 end loop;
16856 -- For tagged derived types, inherited discriminants cannot be used in
16857 -- component declarations of the record extension part. To achieve this
16858 -- we mark the inherited discriminants as not visible.
16860 if Is_Tagged and then Inherit_Discr then
16861 D := First_Discriminant (Derived_Base);
16862 while Present (D) loop
16863 Set_Is_Immediately_Visible (D, False);
16864 Next_Discriminant (D);
16865 end loop;
16866 end if;
16868 return Assoc_List;
16869 end Inherit_Components;
16871 -----------------------------
16872 -- Inherit_Predicate_Flags --
16873 -----------------------------
16875 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
16876 begin
16877 Set_Has_Predicates (Subt, Has_Predicates (Par));
16878 Set_Has_Static_Predicate_Aspect
16879 (Subt, Has_Static_Predicate_Aspect (Par));
16880 Set_Has_Dynamic_Predicate_Aspect
16881 (Subt, Has_Dynamic_Predicate_Aspect (Par));
16882 end Inherit_Predicate_Flags;
16884 -----------------------
16885 -- Is_Null_Extension --
16886 -----------------------
16888 function Is_Null_Extension (T : Entity_Id) return Boolean is
16889 Type_Decl : constant Node_Id := Parent (Base_Type (T));
16890 Comp_List : Node_Id;
16891 Comp : Node_Id;
16893 begin
16894 if Nkind (Type_Decl) /= N_Full_Type_Declaration
16895 or else not Is_Tagged_Type (T)
16896 or else Nkind (Type_Definition (Type_Decl)) /=
16897 N_Derived_Type_Definition
16898 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
16899 then
16900 return False;
16901 end if;
16903 Comp_List :=
16904 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
16906 if Present (Discriminant_Specifications (Type_Decl)) then
16907 return False;
16909 elsif Present (Comp_List)
16910 and then Is_Non_Empty_List (Component_Items (Comp_List))
16911 then
16912 Comp := First (Component_Items (Comp_List));
16914 -- Only user-defined components are relevant. The component list
16915 -- may also contain a parent component and internal components
16916 -- corresponding to secondary tags, but these do not determine
16917 -- whether this is a null extension.
16919 while Present (Comp) loop
16920 if Comes_From_Source (Comp) then
16921 return False;
16922 end if;
16924 Next (Comp);
16925 end loop;
16927 return True;
16928 else
16929 return True;
16930 end if;
16931 end Is_Null_Extension;
16933 ------------------------------
16934 -- Is_Valid_Constraint_Kind --
16935 ------------------------------
16937 function Is_Valid_Constraint_Kind
16938 (T_Kind : Type_Kind;
16939 Constraint_Kind : Node_Kind) return Boolean
16941 begin
16942 case T_Kind is
16943 when Enumeration_Kind |
16944 Integer_Kind =>
16945 return Constraint_Kind = N_Range_Constraint;
16947 when Decimal_Fixed_Point_Kind =>
16948 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16949 N_Range_Constraint);
16951 when Ordinary_Fixed_Point_Kind =>
16952 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
16953 N_Range_Constraint);
16955 when Float_Kind =>
16956 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
16957 N_Range_Constraint);
16959 when Access_Kind |
16960 Array_Kind |
16961 E_Record_Type |
16962 E_Record_Subtype |
16963 Class_Wide_Kind |
16964 E_Incomplete_Type |
16965 Private_Kind |
16966 Concurrent_Kind =>
16967 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
16969 when others =>
16970 return True; -- Error will be detected later
16971 end case;
16972 end Is_Valid_Constraint_Kind;
16974 --------------------------
16975 -- Is_Visible_Component --
16976 --------------------------
16978 function Is_Visible_Component
16979 (C : Entity_Id;
16980 N : Node_Id := Empty) return Boolean
16982 Original_Comp : Entity_Id := Empty;
16983 Original_Scope : Entity_Id;
16984 Type_Scope : Entity_Id;
16986 function Is_Local_Type (Typ : Entity_Id) return Boolean;
16987 -- Check whether parent type of inherited component is declared locally,
16988 -- possibly within a nested package or instance. The current scope is
16989 -- the derived record itself.
16991 -------------------
16992 -- Is_Local_Type --
16993 -------------------
16995 function Is_Local_Type (Typ : Entity_Id) return Boolean is
16996 Scop : Entity_Id;
16998 begin
16999 Scop := Scope (Typ);
17000 while Present (Scop)
17001 and then Scop /= Standard_Standard
17002 loop
17003 if Scop = Scope (Current_Scope) then
17004 return True;
17005 end if;
17007 Scop := Scope (Scop);
17008 end loop;
17010 return False;
17011 end Is_Local_Type;
17013 -- Start of processing for Is_Visible_Component
17015 begin
17016 if Ekind_In (C, E_Component, E_Discriminant) then
17017 Original_Comp := Original_Record_Component (C);
17018 end if;
17020 if No (Original_Comp) then
17022 -- Premature usage, or previous error
17024 return False;
17026 else
17027 Original_Scope := Scope (Original_Comp);
17028 Type_Scope := Scope (Base_Type (Scope (C)));
17029 end if;
17031 -- This test only concerns tagged types
17033 if not Is_Tagged_Type (Original_Scope) then
17034 return True;
17036 -- If it is _Parent or _Tag, there is no visibility issue
17038 elsif not Comes_From_Source (Original_Comp) then
17039 return True;
17041 -- Discriminants are visible unless the (private) type has unknown
17042 -- discriminants. If the discriminant reference is inserted for a
17043 -- discriminant check on a full view it is also visible.
17045 elsif Ekind (Original_Comp) = E_Discriminant
17046 and then
17047 (not Has_Unknown_Discriminants (Original_Scope)
17048 or else (Present (N)
17049 and then Nkind (N) = N_Selected_Component
17050 and then Nkind (Prefix (N)) = N_Type_Conversion
17051 and then not Comes_From_Source (Prefix (N))))
17052 then
17053 return True;
17055 -- In the body of an instantiation, no need to check for the visibility
17056 -- of a component.
17058 elsif In_Instance_Body then
17059 return True;
17061 -- If the component has been declared in an ancestor which is currently
17062 -- a private type, then it is not visible. The same applies if the
17063 -- component's containing type is not in an open scope and the original
17064 -- component's enclosing type is a visible full view of a private type
17065 -- (which can occur in cases where an attempt is being made to reference
17066 -- a component in a sibling package that is inherited from a visible
17067 -- component of a type in an ancestor package; the component in the
17068 -- sibling package should not be visible even though the component it
17069 -- inherited from is visible). This does not apply however in the case
17070 -- where the scope of the type is a private child unit, or when the
17071 -- parent comes from a local package in which the ancestor is currently
17072 -- visible. The latter suppression of visibility is needed for cases
17073 -- that are tested in B730006.
17075 elsif Is_Private_Type (Original_Scope)
17076 or else
17077 (not Is_Private_Descendant (Type_Scope)
17078 and then not In_Open_Scopes (Type_Scope)
17079 and then Has_Private_Declaration (Original_Scope))
17080 then
17081 -- If the type derives from an entity in a formal package, there
17082 -- are no additional visible components.
17084 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17085 N_Formal_Package_Declaration
17086 then
17087 return False;
17089 -- if we are not in the private part of the current package, there
17090 -- are no additional visible components.
17092 elsif Ekind (Scope (Current_Scope)) = E_Package
17093 and then not In_Private_Part (Scope (Current_Scope))
17094 then
17095 return False;
17096 else
17097 return
17098 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17099 and then In_Open_Scopes (Scope (Original_Scope))
17100 and then Is_Local_Type (Type_Scope);
17101 end if;
17103 -- There is another weird way in which a component may be invisible when
17104 -- the private and the full view are not derived from the same ancestor.
17105 -- Here is an example :
17107 -- type A1 is tagged record F1 : integer; end record;
17108 -- type A2 is new A1 with record F2 : integer; end record;
17109 -- type T is new A1 with private;
17110 -- private
17111 -- type T is new A2 with null record;
17113 -- In this case, the full view of T inherits F1 and F2 but the private
17114 -- view inherits only F1
17116 else
17117 declare
17118 Ancestor : Entity_Id := Scope (C);
17120 begin
17121 loop
17122 if Ancestor = Original_Scope then
17123 return True;
17124 elsif Ancestor = Etype (Ancestor) then
17125 return False;
17126 end if;
17128 Ancestor := Etype (Ancestor);
17129 end loop;
17130 end;
17131 end if;
17132 end Is_Visible_Component;
17134 --------------------------
17135 -- Make_Class_Wide_Type --
17136 --------------------------
17138 procedure Make_Class_Wide_Type (T : Entity_Id) is
17139 CW_Type : Entity_Id;
17140 CW_Name : Name_Id;
17141 Next_E : Entity_Id;
17143 begin
17144 if Present (Class_Wide_Type (T)) then
17146 -- The class-wide type is a partially decorated entity created for a
17147 -- unanalyzed tagged type referenced through a limited with clause.
17148 -- When the tagged type is analyzed, its class-wide type needs to be
17149 -- redecorated. Note that we reuse the entity created by Decorate_
17150 -- Tagged_Type in order to preserve all links.
17152 if Materialize_Entity (Class_Wide_Type (T)) then
17153 CW_Type := Class_Wide_Type (T);
17154 Set_Materialize_Entity (CW_Type, False);
17156 -- The class wide type can have been defined by the partial view, in
17157 -- which case everything is already done.
17159 else
17160 return;
17161 end if;
17163 -- Default case, we need to create a new class-wide type
17165 else
17166 CW_Type :=
17167 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
17168 end if;
17170 -- Inherit root type characteristics
17172 CW_Name := Chars (CW_Type);
17173 Next_E := Next_Entity (CW_Type);
17174 Copy_Node (T, CW_Type);
17175 Set_Comes_From_Source (CW_Type, False);
17176 Set_Chars (CW_Type, CW_Name);
17177 Set_Parent (CW_Type, Parent (T));
17178 Set_Next_Entity (CW_Type, Next_E);
17180 -- Ensure we have a new freeze node for the class-wide type. The partial
17181 -- view may have freeze action of its own, requiring a proper freeze
17182 -- node, and the same freeze node cannot be shared between the two
17183 -- types.
17185 Set_Has_Delayed_Freeze (CW_Type);
17186 Set_Freeze_Node (CW_Type, Empty);
17188 -- Customize the class-wide type: It has no prim. op., it cannot be
17189 -- abstract and its Etype points back to the specific root type.
17191 Set_Ekind (CW_Type, E_Class_Wide_Type);
17192 Set_Is_Tagged_Type (CW_Type, True);
17193 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
17194 Set_Is_Abstract_Type (CW_Type, False);
17195 Set_Is_Constrained (CW_Type, False);
17196 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
17197 Set_Default_SSO (CW_Type);
17199 if Ekind (T) = E_Class_Wide_Subtype then
17200 Set_Etype (CW_Type, Etype (Base_Type (T)));
17201 else
17202 Set_Etype (CW_Type, T);
17203 end if;
17205 -- If this is the class_wide type of a constrained subtype, it does
17206 -- not have discriminants.
17208 Set_Has_Discriminants (CW_Type,
17209 Has_Discriminants (T) and then not Is_Constrained (T));
17211 Set_Has_Unknown_Discriminants (CW_Type, True);
17212 Set_Class_Wide_Type (T, CW_Type);
17213 Set_Equivalent_Type (CW_Type, Empty);
17215 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
17217 Set_Class_Wide_Type (CW_Type, CW_Type);
17218 end Make_Class_Wide_Type;
17220 ----------------
17221 -- Make_Index --
17222 ----------------
17224 procedure Make_Index
17225 (N : Node_Id;
17226 Related_Nod : Node_Id;
17227 Related_Id : Entity_Id := Empty;
17228 Suffix_Index : Nat := 1;
17229 In_Iter_Schm : Boolean := False)
17231 R : Node_Id;
17232 T : Entity_Id;
17233 Def_Id : Entity_Id := Empty;
17234 Found : Boolean := False;
17236 begin
17237 -- For a discrete range used in a constrained array definition and
17238 -- defined by a range, an implicit conversion to the predefined type
17239 -- INTEGER is assumed if each bound is either a numeric literal, a named
17240 -- number, or an attribute, and the type of both bounds (prior to the
17241 -- implicit conversion) is the type universal_integer. Otherwise, both
17242 -- bounds must be of the same discrete type, other than universal
17243 -- integer; this type must be determinable independently of the
17244 -- context, but using the fact that the type must be discrete and that
17245 -- both bounds must have the same type.
17247 -- Character literals also have a universal type in the absence of
17248 -- of additional context, and are resolved to Standard_Character.
17250 if Nkind (N) = N_Range then
17252 -- The index is given by a range constraint. The bounds are known
17253 -- to be of a consistent type.
17255 if not Is_Overloaded (N) then
17256 T := Etype (N);
17258 -- For universal bounds, choose the specific predefined type
17260 if T = Universal_Integer then
17261 T := Standard_Integer;
17263 elsif T = Any_Character then
17264 Ambiguous_Character (Low_Bound (N));
17266 T := Standard_Character;
17267 end if;
17269 -- The node may be overloaded because some user-defined operators
17270 -- are available, but if a universal interpretation exists it is
17271 -- also the selected one.
17273 elsif Universal_Interpretation (N) = Universal_Integer then
17274 T := Standard_Integer;
17276 else
17277 T := Any_Type;
17279 declare
17280 Ind : Interp_Index;
17281 It : Interp;
17283 begin
17284 Get_First_Interp (N, Ind, It);
17285 while Present (It.Typ) loop
17286 if Is_Discrete_Type (It.Typ) then
17288 if Found
17289 and then not Covers (It.Typ, T)
17290 and then not Covers (T, It.Typ)
17291 then
17292 Error_Msg_N ("ambiguous bounds in discrete range", N);
17293 exit;
17294 else
17295 T := It.Typ;
17296 Found := True;
17297 end if;
17298 end if;
17300 Get_Next_Interp (Ind, It);
17301 end loop;
17303 if T = Any_Type then
17304 Error_Msg_N ("discrete type required for range", N);
17305 Set_Etype (N, Any_Type);
17306 return;
17308 elsif T = Universal_Integer then
17309 T := Standard_Integer;
17310 end if;
17311 end;
17312 end if;
17314 if not Is_Discrete_Type (T) then
17315 Error_Msg_N ("discrete type required for range", N);
17316 Set_Etype (N, Any_Type);
17317 return;
17318 end if;
17320 if Nkind (Low_Bound (N)) = N_Attribute_Reference
17321 and then Attribute_Name (Low_Bound (N)) = Name_First
17322 and then Is_Entity_Name (Prefix (Low_Bound (N)))
17323 and then Is_Type (Entity (Prefix (Low_Bound (N))))
17324 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
17325 then
17326 -- The type of the index will be the type of the prefix, as long
17327 -- as the upper bound is 'Last of the same type.
17329 Def_Id := Entity (Prefix (Low_Bound (N)));
17331 if Nkind (High_Bound (N)) /= N_Attribute_Reference
17332 or else Attribute_Name (High_Bound (N)) /= Name_Last
17333 or else not Is_Entity_Name (Prefix (High_Bound (N)))
17334 or else Entity (Prefix (High_Bound (N))) /= Def_Id
17335 then
17336 Def_Id := Empty;
17337 end if;
17338 end if;
17340 R := N;
17341 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
17343 elsif Nkind (N) = N_Subtype_Indication then
17345 -- The index is given by a subtype with a range constraint
17347 T := Base_Type (Entity (Subtype_Mark (N)));
17349 if not Is_Discrete_Type (T) then
17350 Error_Msg_N ("discrete type required for range", N);
17351 Set_Etype (N, Any_Type);
17352 return;
17353 end if;
17355 R := Range_Expression (Constraint (N));
17357 Resolve (R, T);
17358 Process_Range_Expr_In_Decl
17359 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
17361 elsif Nkind (N) = N_Attribute_Reference then
17363 -- Catch beginner's error (use of attribute other than 'Range)
17365 if Attribute_Name (N) /= Name_Range then
17366 Error_Msg_N ("expect attribute ''Range", N);
17367 Set_Etype (N, Any_Type);
17368 return;
17369 end if;
17371 -- If the node denotes the range of a type mark, that is also the
17372 -- resulting type, and we do not need to create an Itype for it.
17374 if Is_Entity_Name (Prefix (N))
17375 and then Comes_From_Source (N)
17376 and then Is_Type (Entity (Prefix (N)))
17377 and then Is_Discrete_Type (Entity (Prefix (N)))
17378 then
17379 Def_Id := Entity (Prefix (N));
17380 end if;
17382 Analyze_And_Resolve (N);
17383 T := Etype (N);
17384 R := N;
17386 -- If none of the above, must be a subtype. We convert this to a
17387 -- range attribute reference because in the case of declared first
17388 -- named subtypes, the types in the range reference can be different
17389 -- from the type of the entity. A range attribute normalizes the
17390 -- reference and obtains the correct types for the bounds.
17392 -- This transformation is in the nature of an expansion, is only
17393 -- done if expansion is active. In particular, it is not done on
17394 -- formal generic types, because we need to retain the name of the
17395 -- original index for instantiation purposes.
17397 else
17398 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
17399 Error_Msg_N ("invalid subtype mark in discrete range ", N);
17400 Set_Etype (N, Any_Integer);
17401 return;
17403 else
17404 -- The type mark may be that of an incomplete type. It is only
17405 -- now that we can get the full view, previous analysis does
17406 -- not look specifically for a type mark.
17408 Set_Entity (N, Get_Full_View (Entity (N)));
17409 Set_Etype (N, Entity (N));
17410 Def_Id := Entity (N);
17412 if not Is_Discrete_Type (Def_Id) then
17413 Error_Msg_N ("discrete type required for index", N);
17414 Set_Etype (N, Any_Type);
17415 return;
17416 end if;
17417 end if;
17419 if Expander_Active then
17420 Rewrite (N,
17421 Make_Attribute_Reference (Sloc (N),
17422 Attribute_Name => Name_Range,
17423 Prefix => Relocate_Node (N)));
17425 -- The original was a subtype mark that does not freeze. This
17426 -- means that the rewritten version must not freeze either.
17428 Set_Must_Not_Freeze (N);
17429 Set_Must_Not_Freeze (Prefix (N));
17430 Analyze_And_Resolve (N);
17431 T := Etype (N);
17432 R := N;
17434 -- If expander is inactive, type is legal, nothing else to construct
17436 else
17437 return;
17438 end if;
17439 end if;
17441 if not Is_Discrete_Type (T) then
17442 Error_Msg_N ("discrete type required for range", N);
17443 Set_Etype (N, Any_Type);
17444 return;
17446 elsif T = Any_Type then
17447 Set_Etype (N, Any_Type);
17448 return;
17449 end if;
17451 -- We will now create the appropriate Itype to describe the range, but
17452 -- first a check. If we originally had a subtype, then we just label
17453 -- the range with this subtype. Not only is there no need to construct
17454 -- a new subtype, but it is wrong to do so for two reasons:
17456 -- 1. A legality concern, if we have a subtype, it must not freeze,
17457 -- and the Itype would cause freezing incorrectly
17459 -- 2. An efficiency concern, if we created an Itype, it would not be
17460 -- recognized as the same type for the purposes of eliminating
17461 -- checks in some circumstances.
17463 -- We signal this case by setting the subtype entity in Def_Id
17465 if No (Def_Id) then
17466 Def_Id :=
17467 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
17468 Set_Etype (Def_Id, Base_Type (T));
17470 if Is_Signed_Integer_Type (T) then
17471 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
17473 elsif Is_Modular_Integer_Type (T) then
17474 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
17476 else
17477 Set_Ekind (Def_Id, E_Enumeration_Subtype);
17478 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
17479 Set_First_Literal (Def_Id, First_Literal (T));
17480 end if;
17482 Set_Size_Info (Def_Id, (T));
17483 Set_RM_Size (Def_Id, RM_Size (T));
17484 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
17486 Set_Scalar_Range (Def_Id, R);
17487 Conditional_Delay (Def_Id, T);
17489 if Nkind (N) = N_Subtype_Indication then
17490 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
17491 end if;
17493 -- In the subtype indication case, if the immediate parent of the
17494 -- new subtype is non-static, then the subtype we create is non-
17495 -- static, even if its bounds are static.
17497 if Nkind (N) = N_Subtype_Indication
17498 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
17499 then
17500 Set_Is_Non_Static_Subtype (Def_Id);
17501 end if;
17502 end if;
17504 -- Final step is to label the index with this constructed type
17506 Set_Etype (N, Def_Id);
17507 end Make_Index;
17509 ------------------------------
17510 -- Modular_Type_Declaration --
17511 ------------------------------
17513 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17514 Mod_Expr : constant Node_Id := Expression (Def);
17515 M_Val : Uint;
17517 procedure Set_Modular_Size (Bits : Int);
17518 -- Sets RM_Size to Bits, and Esize to normal word size above this
17520 ----------------------
17521 -- Set_Modular_Size --
17522 ----------------------
17524 procedure Set_Modular_Size (Bits : Int) is
17525 begin
17526 Set_RM_Size (T, UI_From_Int (Bits));
17528 if Bits <= 8 then
17529 Init_Esize (T, 8);
17531 elsif Bits <= 16 then
17532 Init_Esize (T, 16);
17534 elsif Bits <= 32 then
17535 Init_Esize (T, 32);
17537 else
17538 Init_Esize (T, System_Max_Binary_Modulus_Power);
17539 end if;
17541 if not Non_Binary_Modulus (T)
17542 and then Esize (T) = RM_Size (T)
17543 then
17544 Set_Is_Known_Valid (T);
17545 end if;
17546 end Set_Modular_Size;
17548 -- Start of processing for Modular_Type_Declaration
17550 begin
17551 -- If the mod expression is (exactly) 2 * literal, where literal is
17552 -- 64 or less,then almost certainly the * was meant to be **. Warn.
17554 if Warn_On_Suspicious_Modulus_Value
17555 and then Nkind (Mod_Expr) = N_Op_Multiply
17556 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
17557 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
17558 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
17559 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
17560 then
17561 Error_Msg_N
17562 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
17563 end if;
17565 -- Proceed with analysis of mod expression
17567 Analyze_And_Resolve (Mod_Expr, Any_Integer);
17568 Set_Etype (T, T);
17569 Set_Ekind (T, E_Modular_Integer_Type);
17570 Init_Alignment (T);
17571 Set_Is_Constrained (T);
17573 if not Is_OK_Static_Expression (Mod_Expr) then
17574 Flag_Non_Static_Expr
17575 ("non-static expression used for modular type bound!", Mod_Expr);
17576 M_Val := 2 ** System_Max_Binary_Modulus_Power;
17577 else
17578 M_Val := Expr_Value (Mod_Expr);
17579 end if;
17581 if M_Val < 1 then
17582 Error_Msg_N ("modulus value must be positive", Mod_Expr);
17583 M_Val := 2 ** System_Max_Binary_Modulus_Power;
17584 end if;
17586 if M_Val > 2 ** Standard_Long_Integer_Size then
17587 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
17588 end if;
17590 Set_Modulus (T, M_Val);
17592 -- Create bounds for the modular type based on the modulus given in
17593 -- the type declaration and then analyze and resolve those bounds.
17595 Set_Scalar_Range (T,
17596 Make_Range (Sloc (Mod_Expr),
17597 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
17598 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
17600 -- Properly analyze the literals for the range. We do this manually
17601 -- because we can't go calling Resolve, since we are resolving these
17602 -- bounds with the type, and this type is certainly not complete yet.
17604 Set_Etype (Low_Bound (Scalar_Range (T)), T);
17605 Set_Etype (High_Bound (Scalar_Range (T)), T);
17606 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
17607 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
17609 -- Loop through powers of two to find number of bits required
17611 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
17613 -- Binary case
17615 if M_Val = 2 ** Bits then
17616 Set_Modular_Size (Bits);
17617 return;
17619 -- Non-binary case
17621 elsif M_Val < 2 ** Bits then
17622 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
17623 Set_Non_Binary_Modulus (T);
17625 if Bits > System_Max_Nonbinary_Modulus_Power then
17626 Error_Msg_Uint_1 :=
17627 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
17628 Error_Msg_F
17629 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
17630 Set_Modular_Size (System_Max_Binary_Modulus_Power);
17631 return;
17633 else
17634 -- In the non-binary case, set size as per RM 13.3(55)
17636 Set_Modular_Size (Bits);
17637 return;
17638 end if;
17639 end if;
17641 end loop;
17643 -- If we fall through, then the size exceed System.Max_Binary_Modulus
17644 -- so we just signal an error and set the maximum size.
17646 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
17647 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
17649 Set_Modular_Size (System_Max_Binary_Modulus_Power);
17650 Init_Alignment (T);
17652 end Modular_Type_Declaration;
17654 --------------------------
17655 -- New_Concatenation_Op --
17656 --------------------------
17658 procedure New_Concatenation_Op (Typ : Entity_Id) is
17659 Loc : constant Source_Ptr := Sloc (Typ);
17660 Op : Entity_Id;
17662 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
17663 -- Create abbreviated declaration for the formal of a predefined
17664 -- Operator 'Op' of type 'Typ'
17666 --------------------
17667 -- Make_Op_Formal --
17668 --------------------
17670 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
17671 Formal : Entity_Id;
17672 begin
17673 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
17674 Set_Etype (Formal, Typ);
17675 Set_Mechanism (Formal, Default_Mechanism);
17676 return Formal;
17677 end Make_Op_Formal;
17679 -- Start of processing for New_Concatenation_Op
17681 begin
17682 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
17684 Set_Ekind (Op, E_Operator);
17685 Set_Scope (Op, Current_Scope);
17686 Set_Etype (Op, Typ);
17687 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
17688 Set_Is_Immediately_Visible (Op);
17689 Set_Is_Intrinsic_Subprogram (Op);
17690 Set_Has_Completion (Op);
17691 Append_Entity (Op, Current_Scope);
17693 Set_Name_Entity_Id (Name_Op_Concat, Op);
17695 Append_Entity (Make_Op_Formal (Typ, Op), Op);
17696 Append_Entity (Make_Op_Formal (Typ, Op), Op);
17697 end New_Concatenation_Op;
17699 -------------------------
17700 -- OK_For_Limited_Init --
17701 -------------------------
17703 -- ???Check all calls of this, and compare the conditions under which it's
17704 -- called.
17706 function OK_For_Limited_Init
17707 (Typ : Entity_Id;
17708 Exp : Node_Id) return Boolean
17710 begin
17711 return Is_CPP_Constructor_Call (Exp)
17712 or else (Ada_Version >= Ada_2005
17713 and then not Debug_Flag_Dot_L
17714 and then OK_For_Limited_Init_In_05 (Typ, Exp));
17715 end OK_For_Limited_Init;
17717 -------------------------------
17718 -- OK_For_Limited_Init_In_05 --
17719 -------------------------------
17721 function OK_For_Limited_Init_In_05
17722 (Typ : Entity_Id;
17723 Exp : Node_Id) return Boolean
17725 begin
17726 -- An object of a limited interface type can be initialized with any
17727 -- expression of a nonlimited descendant type.
17729 if Is_Class_Wide_Type (Typ)
17730 and then Is_Limited_Interface (Typ)
17731 and then not Is_Limited_Type (Etype (Exp))
17732 then
17733 return True;
17734 end if;
17736 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
17737 -- case of limited aggregates (including extension aggregates), and
17738 -- function calls. The function call may have been given in prefixed
17739 -- notation, in which case the original node is an indexed component.
17740 -- If the function is parameterless, the original node was an explicit
17741 -- dereference. The function may also be parameterless, in which case
17742 -- the source node is just an identifier.
17744 case Nkind (Original_Node (Exp)) is
17745 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
17746 return True;
17748 when N_Identifier =>
17749 return Present (Entity (Original_Node (Exp)))
17750 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
17752 when N_Qualified_Expression =>
17753 return
17754 OK_For_Limited_Init_In_05
17755 (Typ, Expression (Original_Node (Exp)));
17757 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
17758 -- with a function call, the expander has rewritten the call into an
17759 -- N_Type_Conversion node to force displacement of the pointer to
17760 -- reference the component containing the secondary dispatch table.
17761 -- Otherwise a type conversion is not a legal context.
17762 -- A return statement for a build-in-place function returning a
17763 -- synchronized type also introduces an unchecked conversion.
17765 when N_Type_Conversion |
17766 N_Unchecked_Type_Conversion =>
17767 return not Comes_From_Source (Exp)
17768 and then
17769 OK_For_Limited_Init_In_05
17770 (Typ, Expression (Original_Node (Exp)));
17772 when N_Indexed_Component |
17773 N_Selected_Component |
17774 N_Explicit_Dereference =>
17775 return Nkind (Exp) = N_Function_Call;
17777 -- A use of 'Input is a function call, hence allowed. Normally the
17778 -- attribute will be changed to a call, but the attribute by itself
17779 -- can occur with -gnatc.
17781 when N_Attribute_Reference =>
17782 return Attribute_Name (Original_Node (Exp)) = Name_Input;
17784 -- For a case expression, all dependent expressions must be legal
17786 when N_Case_Expression =>
17787 declare
17788 Alt : Node_Id;
17790 begin
17791 Alt := First (Alternatives (Original_Node (Exp)));
17792 while Present (Alt) loop
17793 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
17794 return False;
17795 end if;
17797 Next (Alt);
17798 end loop;
17800 return True;
17801 end;
17803 -- For an if expression, all dependent expressions must be legal
17805 when N_If_Expression =>
17806 declare
17807 Then_Expr : constant Node_Id :=
17808 Next (First (Expressions (Original_Node (Exp))));
17809 Else_Expr : constant Node_Id := Next (Then_Expr);
17810 begin
17811 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
17812 and then
17813 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
17814 end;
17816 when others =>
17817 return False;
17818 end case;
17819 end OK_For_Limited_Init_In_05;
17821 -------------------------------------------
17822 -- Ordinary_Fixed_Point_Type_Declaration --
17823 -------------------------------------------
17825 procedure Ordinary_Fixed_Point_Type_Declaration
17826 (T : Entity_Id;
17827 Def : Node_Id)
17829 Loc : constant Source_Ptr := Sloc (Def);
17830 Delta_Expr : constant Node_Id := Delta_Expression (Def);
17831 RRS : constant Node_Id := Real_Range_Specification (Def);
17832 Implicit_Base : Entity_Id;
17833 Delta_Val : Ureal;
17834 Small_Val : Ureal;
17835 Low_Val : Ureal;
17836 High_Val : Ureal;
17838 begin
17839 Check_Restriction (No_Fixed_Point, Def);
17841 -- Create implicit base type
17843 Implicit_Base :=
17844 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
17845 Set_Etype (Implicit_Base, Implicit_Base);
17847 -- Analyze and process delta expression
17849 Analyze_And_Resolve (Delta_Expr, Any_Real);
17851 Check_Delta_Expression (Delta_Expr);
17852 Delta_Val := Expr_Value_R (Delta_Expr);
17854 Set_Delta_Value (Implicit_Base, Delta_Val);
17856 -- Compute default small from given delta, which is the largest power
17857 -- of two that does not exceed the given delta value.
17859 declare
17860 Tmp : Ureal;
17861 Scale : Int;
17863 begin
17864 Tmp := Ureal_1;
17865 Scale := 0;
17867 if Delta_Val < Ureal_1 then
17868 while Delta_Val < Tmp loop
17869 Tmp := Tmp / Ureal_2;
17870 Scale := Scale + 1;
17871 end loop;
17873 else
17874 loop
17875 Tmp := Tmp * Ureal_2;
17876 exit when Tmp > Delta_Val;
17877 Scale := Scale - 1;
17878 end loop;
17879 end if;
17881 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
17882 end;
17884 Set_Small_Value (Implicit_Base, Small_Val);
17886 -- If no range was given, set a dummy range
17888 if RRS <= Empty_Or_Error then
17889 Low_Val := -Small_Val;
17890 High_Val := Small_Val;
17892 -- Otherwise analyze and process given range
17894 else
17895 declare
17896 Low : constant Node_Id := Low_Bound (RRS);
17897 High : constant Node_Id := High_Bound (RRS);
17899 begin
17900 Analyze_And_Resolve (Low, Any_Real);
17901 Analyze_And_Resolve (High, Any_Real);
17902 Check_Real_Bound (Low);
17903 Check_Real_Bound (High);
17905 -- Obtain and set the range
17907 Low_Val := Expr_Value_R (Low);
17908 High_Val := Expr_Value_R (High);
17910 if Low_Val > High_Val then
17911 Error_Msg_NE ("??fixed point type& has null range", Def, T);
17912 end if;
17913 end;
17914 end if;
17916 -- The range for both the implicit base and the declared first subtype
17917 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
17918 -- set a temporary range in place. Note that the bounds of the base
17919 -- type will be widened to be symmetrical and to fill the available
17920 -- bits when the type is frozen.
17922 -- We could do this with all discrete types, and probably should, but
17923 -- we absolutely have to do it for fixed-point, since the end-points
17924 -- of the range and the size are determined by the small value, which
17925 -- could be reset before the freeze point.
17927 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
17928 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
17930 -- Complete definition of first subtype
17932 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
17933 Set_Etype (T, Implicit_Base);
17934 Init_Size_Align (T);
17935 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17936 Set_Small_Value (T, Small_Val);
17937 Set_Delta_Value (T, Delta_Val);
17938 Set_Is_Constrained (T);
17940 end Ordinary_Fixed_Point_Type_Declaration;
17942 ----------------------------------------
17943 -- Prepare_Private_Subtype_Completion --
17944 ----------------------------------------
17946 procedure Prepare_Private_Subtype_Completion
17947 (Id : Entity_Id;
17948 Related_Nod : Node_Id)
17950 Id_B : constant Entity_Id := Base_Type (Id);
17951 Full_B : Entity_Id := Full_View (Id_B);
17952 Full : Entity_Id;
17954 begin
17955 if Present (Full_B) then
17957 -- Get to the underlying full view if necessary
17959 if Is_Private_Type (Full_B)
17960 and then Present (Underlying_Full_View (Full_B))
17961 then
17962 Full_B := Underlying_Full_View (Full_B);
17963 end if;
17965 -- The Base_Type is already completed, we can complete the subtype
17966 -- now. We have to create a new entity with the same name, Thus we
17967 -- can't use Create_Itype.
17969 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
17970 Set_Is_Itype (Full);
17971 Set_Associated_Node_For_Itype (Full, Related_Nod);
17972 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
17973 end if;
17975 -- The parent subtype may be private, but the base might not, in some
17976 -- nested instances. In that case, the subtype does not need to be
17977 -- exchanged. It would still be nice to make private subtypes and their
17978 -- bases consistent at all times ???
17980 if Is_Private_Type (Id_B) then
17981 Append_Elmt (Id, Private_Dependents (Id_B));
17982 end if;
17983 end Prepare_Private_Subtype_Completion;
17985 ---------------------------
17986 -- Process_Discriminants --
17987 ---------------------------
17989 procedure Process_Discriminants
17990 (N : Node_Id;
17991 Prev : Entity_Id := Empty)
17993 Elist : constant Elist_Id := New_Elmt_List;
17994 Id : Node_Id;
17995 Discr : Node_Id;
17996 Discr_Number : Uint;
17997 Discr_Type : Entity_Id;
17998 Default_Present : Boolean := False;
17999 Default_Not_Present : Boolean := False;
18001 begin
18002 -- A composite type other than an array type can have discriminants.
18003 -- On entry, the current scope is the composite type.
18005 -- The discriminants are initially entered into the scope of the type
18006 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18007 -- use, as explained at the end of this procedure.
18009 Discr := First (Discriminant_Specifications (N));
18010 while Present (Discr) loop
18011 Enter_Name (Defining_Identifier (Discr));
18013 -- For navigation purposes we add a reference to the discriminant
18014 -- in the entity for the type. If the current declaration is a
18015 -- completion, place references on the partial view. Otherwise the
18016 -- type is the current scope.
18018 if Present (Prev) then
18020 -- The references go on the partial view, if present. If the
18021 -- partial view has discriminants, the references have been
18022 -- generated already.
18024 if not Has_Discriminants (Prev) then
18025 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18026 end if;
18027 else
18028 Generate_Reference
18029 (Current_Scope, Defining_Identifier (Discr), 'd');
18030 end if;
18032 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18033 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18035 -- Ada 2005 (AI-254)
18037 if Present (Access_To_Subprogram_Definition
18038 (Discriminant_Type (Discr)))
18039 and then Protected_Present (Access_To_Subprogram_Definition
18040 (Discriminant_Type (Discr)))
18041 then
18042 Discr_Type :=
18043 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18044 end if;
18046 else
18047 Find_Type (Discriminant_Type (Discr));
18048 Discr_Type := Etype (Discriminant_Type (Discr));
18050 if Error_Posted (Discriminant_Type (Discr)) then
18051 Discr_Type := Any_Type;
18052 end if;
18053 end if;
18055 -- Handling of discriminants that are access types
18057 if Is_Access_Type (Discr_Type) then
18059 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18060 -- limited record types
18062 if Ada_Version < Ada_2005 then
18063 Check_Access_Discriminant_Requires_Limited
18064 (Discr, Discriminant_Type (Discr));
18065 end if;
18067 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18068 Error_Msg_N
18069 ("(Ada 83) access discriminant not allowed", Discr);
18070 end if;
18072 -- If not access type, must be a discrete type
18074 elsif not Is_Discrete_Type (Discr_Type) then
18075 Error_Msg_N
18076 ("discriminants must have a discrete or access type",
18077 Discriminant_Type (Discr));
18078 end if;
18080 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18082 -- If a discriminant specification includes the assignment compound
18083 -- delimiter followed by an expression, the expression is the default
18084 -- expression of the discriminant; the default expression must be of
18085 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18086 -- a default expression, we do the special preanalysis, since this
18087 -- expression does not freeze (see section "Handling of Default and
18088 -- Per-Object Expressions" in spec of package Sem).
18090 if Present (Expression (Discr)) then
18091 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
18093 -- Legaity checks
18095 if Nkind (N) = N_Formal_Type_Declaration then
18096 Error_Msg_N
18097 ("discriminant defaults not allowed for formal type",
18098 Expression (Discr));
18100 -- Flag an error for a tagged type with defaulted discriminants,
18101 -- excluding limited tagged types when compiling for Ada 2012
18102 -- (see AI05-0214).
18104 elsif Is_Tagged_Type (Current_Scope)
18105 and then (not Is_Limited_Type (Current_Scope)
18106 or else Ada_Version < Ada_2012)
18107 and then Comes_From_Source (N)
18108 then
18109 -- Note: see similar test in Check_Or_Process_Discriminants, to
18110 -- handle the (illegal) case of the completion of an untagged
18111 -- view with discriminants with defaults by a tagged full view.
18112 -- We skip the check if Discr does not come from source, to
18113 -- account for the case of an untagged derived type providing
18114 -- defaults for a renamed discriminant from a private untagged
18115 -- ancestor with a tagged full view (ACATS B460006).
18117 if Ada_Version >= Ada_2012 then
18118 Error_Msg_N
18119 ("discriminants of nonlimited tagged type cannot have"
18120 & " defaults",
18121 Expression (Discr));
18122 else
18123 Error_Msg_N
18124 ("discriminants of tagged type cannot have defaults",
18125 Expression (Discr));
18126 end if;
18128 else
18129 Default_Present := True;
18130 Append_Elmt (Expression (Discr), Elist);
18132 -- Tag the defining identifiers for the discriminants with
18133 -- their corresponding default expressions from the tree.
18135 Set_Discriminant_Default_Value
18136 (Defining_Identifier (Discr), Expression (Discr));
18137 end if;
18139 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
18140 -- gets set unless we can be sure that no range check is required.
18142 if (GNATprove_Mode or not Expander_Active)
18143 and then not
18144 Is_In_Range
18145 (Expression (Discr), Discr_Type, Assume_Valid => True)
18146 then
18147 Set_Do_Range_Check (Expression (Discr));
18148 end if;
18150 -- No default discriminant value given
18152 else
18153 Default_Not_Present := True;
18154 end if;
18156 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
18157 -- Discr_Type but with the null-exclusion attribute
18159 if Ada_Version >= Ada_2005 then
18161 -- Ada 2005 (AI-231): Static checks
18163 if Can_Never_Be_Null (Discr_Type) then
18164 Null_Exclusion_Static_Checks (Discr);
18166 elsif Is_Access_Type (Discr_Type)
18167 and then Null_Exclusion_Present (Discr)
18169 -- No need to check itypes because in their case this check
18170 -- was done at their point of creation
18172 and then not Is_Itype (Discr_Type)
18173 then
18174 if Can_Never_Be_Null (Discr_Type) then
18175 Error_Msg_NE
18176 ("`NOT NULL` not allowed (& already excludes null)",
18177 Discr,
18178 Discr_Type);
18179 end if;
18181 Set_Etype (Defining_Identifier (Discr),
18182 Create_Null_Excluding_Itype
18183 (T => Discr_Type,
18184 Related_Nod => Discr));
18186 -- Check for improper null exclusion if the type is otherwise
18187 -- legal for a discriminant.
18189 elsif Null_Exclusion_Present (Discr)
18190 and then Is_Discrete_Type (Discr_Type)
18191 then
18192 Error_Msg_N
18193 ("null exclusion can only apply to an access type", Discr);
18194 end if;
18196 -- Ada 2005 (AI-402): access discriminants of nonlimited types
18197 -- can't have defaults. Synchronized types, or types that are
18198 -- explicitly limited are fine, but special tests apply to derived
18199 -- types in generics: in a generic body we have to assume the
18200 -- worst, and therefore defaults are not allowed if the parent is
18201 -- a generic formal private type (see ACATS B370001).
18203 if Is_Access_Type (Discr_Type) and then Default_Present then
18204 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
18205 or else Is_Limited_Record (Current_Scope)
18206 or else Is_Concurrent_Type (Current_Scope)
18207 or else Is_Concurrent_Record_Type (Current_Scope)
18208 or else Ekind (Current_Scope) = E_Limited_Private_Type
18209 then
18210 if not Is_Derived_Type (Current_Scope)
18211 or else not Is_Generic_Type (Etype (Current_Scope))
18212 or else not In_Package_Body (Scope (Etype (Current_Scope)))
18213 or else Limited_Present
18214 (Type_Definition (Parent (Current_Scope)))
18215 then
18216 null;
18218 else
18219 Error_Msg_N ("access discriminants of nonlimited types",
18220 Expression (Discr));
18221 Error_Msg_N ("\cannot have defaults", Expression (Discr));
18222 end if;
18224 elsif Present (Expression (Discr)) then
18225 Error_Msg_N
18226 ("(Ada 2005) access discriminants of nonlimited types",
18227 Expression (Discr));
18228 Error_Msg_N ("\cannot have defaults", Expression (Discr));
18229 end if;
18230 end if;
18231 end if;
18233 -- A discriminant cannot be effectively volatile. This check is only
18234 -- relevant when SPARK_Mode is on as it is not standard Ada legality
18235 -- rule (SPARK RM 7.1.3(6)).
18237 if SPARK_Mode = On
18238 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
18239 then
18240 Error_Msg_N ("discriminant cannot be volatile", Discr);
18241 end if;
18243 Next (Discr);
18244 end loop;
18246 -- An element list consisting of the default expressions of the
18247 -- discriminants is constructed in the above loop and used to set
18248 -- the Discriminant_Constraint attribute for the type. If an object
18249 -- is declared of this (record or task) type without any explicit
18250 -- discriminant constraint given, this element list will form the
18251 -- actual parameters for the corresponding initialization procedure
18252 -- for the type.
18254 Set_Discriminant_Constraint (Current_Scope, Elist);
18255 Set_Stored_Constraint (Current_Scope, No_Elist);
18257 -- Default expressions must be provided either for all or for none
18258 -- of the discriminants of a discriminant part. (RM 3.7.1)
18260 if Default_Present and then Default_Not_Present then
18261 Error_Msg_N
18262 ("incomplete specification of defaults for discriminants", N);
18263 end if;
18265 -- The use of the name of a discriminant is not allowed in default
18266 -- expressions of a discriminant part if the specification of the
18267 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
18269 -- To detect this, the discriminant names are entered initially with an
18270 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
18271 -- attempt to use a void entity (for example in an expression that is
18272 -- type-checked) produces the error message: premature usage. Now after
18273 -- completing the semantic analysis of the discriminant part, we can set
18274 -- the Ekind of all the discriminants appropriately.
18276 Discr := First (Discriminant_Specifications (N));
18277 Discr_Number := Uint_1;
18278 while Present (Discr) loop
18279 Id := Defining_Identifier (Discr);
18280 Set_Ekind (Id, E_Discriminant);
18281 Init_Component_Location (Id);
18282 Init_Esize (Id);
18283 Set_Discriminant_Number (Id, Discr_Number);
18285 -- Make sure this is always set, even in illegal programs
18287 Set_Corresponding_Discriminant (Id, Empty);
18289 -- Initialize the Original_Record_Component to the entity itself.
18290 -- Inherit_Components will propagate the right value to
18291 -- discriminants in derived record types.
18293 Set_Original_Record_Component (Id, Id);
18295 -- Create the discriminal for the discriminant
18297 Build_Discriminal (Id);
18299 Next (Discr);
18300 Discr_Number := Discr_Number + 1;
18301 end loop;
18303 Set_Has_Discriminants (Current_Scope);
18304 end Process_Discriminants;
18306 -----------------------
18307 -- Process_Full_View --
18308 -----------------------
18310 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
18311 Priv_Parent : Entity_Id;
18312 Full_Parent : Entity_Id;
18313 Full_Indic : Node_Id;
18315 procedure Collect_Implemented_Interfaces
18316 (Typ : Entity_Id;
18317 Ifaces : Elist_Id);
18318 -- Ada 2005: Gather all the interfaces that Typ directly or
18319 -- inherently implements. Duplicate entries are not added to
18320 -- the list Ifaces.
18322 ------------------------------------
18323 -- Collect_Implemented_Interfaces --
18324 ------------------------------------
18326 procedure Collect_Implemented_Interfaces
18327 (Typ : Entity_Id;
18328 Ifaces : Elist_Id)
18330 Iface : Entity_Id;
18331 Iface_Elmt : Elmt_Id;
18333 begin
18334 -- Abstract interfaces are only associated with tagged record types
18336 if not Is_Tagged_Type (Typ)
18337 or else not Is_Record_Type (Typ)
18338 then
18339 return;
18340 end if;
18342 -- Recursively climb to the ancestors
18344 if Etype (Typ) /= Typ
18346 -- Protect the frontend against wrong cyclic declarations like:
18348 -- type B is new A with private;
18349 -- type C is new A with private;
18350 -- private
18351 -- type B is new C with null record;
18352 -- type C is new B with null record;
18354 and then Etype (Typ) /= Priv_T
18355 and then Etype (Typ) /= Full_T
18356 then
18357 -- Keep separate the management of private type declarations
18359 if Ekind (Typ) = E_Record_Type_With_Private then
18361 -- Handle the following illegal usage:
18362 -- type Private_Type is tagged private;
18363 -- private
18364 -- type Private_Type is new Type_Implementing_Iface;
18366 if Present (Full_View (Typ))
18367 and then Etype (Typ) /= Full_View (Typ)
18368 then
18369 if Is_Interface (Etype (Typ)) then
18370 Append_Unique_Elmt (Etype (Typ), Ifaces);
18371 end if;
18373 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
18374 end if;
18376 -- Non-private types
18378 else
18379 if Is_Interface (Etype (Typ)) then
18380 Append_Unique_Elmt (Etype (Typ), Ifaces);
18381 end if;
18383 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
18384 end if;
18385 end if;
18387 -- Handle entities in the list of abstract interfaces
18389 if Present (Interfaces (Typ)) then
18390 Iface_Elmt := First_Elmt (Interfaces (Typ));
18391 while Present (Iface_Elmt) loop
18392 Iface := Node (Iface_Elmt);
18394 pragma Assert (Is_Interface (Iface));
18396 if not Contain_Interface (Iface, Ifaces) then
18397 Append_Elmt (Iface, Ifaces);
18398 Collect_Implemented_Interfaces (Iface, Ifaces);
18399 end if;
18401 Next_Elmt (Iface_Elmt);
18402 end loop;
18403 end if;
18404 end Collect_Implemented_Interfaces;
18406 -- Start of processing for Process_Full_View
18408 begin
18409 -- First some sanity checks that must be done after semantic
18410 -- decoration of the full view and thus cannot be placed with other
18411 -- similar checks in Find_Type_Name
18413 if not Is_Limited_Type (Priv_T)
18414 and then (Is_Limited_Type (Full_T)
18415 or else Is_Limited_Composite (Full_T))
18416 then
18417 if In_Instance then
18418 null;
18419 else
18420 Error_Msg_N
18421 ("completion of nonlimited type cannot be limited", Full_T);
18422 Explain_Limited_Type (Full_T, Full_T);
18423 end if;
18425 elsif Is_Abstract_Type (Full_T)
18426 and then not Is_Abstract_Type (Priv_T)
18427 then
18428 Error_Msg_N
18429 ("completion of nonabstract type cannot be abstract", Full_T);
18431 elsif Is_Tagged_Type (Priv_T)
18432 and then Is_Limited_Type (Priv_T)
18433 and then not Is_Limited_Type (Full_T)
18434 then
18435 -- If pragma CPP_Class was applied to the private declaration
18436 -- propagate the limitedness to the full-view
18438 if Is_CPP_Class (Priv_T) then
18439 Set_Is_Limited_Record (Full_T);
18441 -- GNAT allow its own definition of Limited_Controlled to disobey
18442 -- this rule in order in ease the implementation. This test is safe
18443 -- because Root_Controlled is defined in a child of System that
18444 -- normal programs are not supposed to use.
18446 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
18447 Set_Is_Limited_Composite (Full_T);
18448 else
18449 Error_Msg_N
18450 ("completion of limited tagged type must be limited", Full_T);
18451 end if;
18453 elsif Is_Generic_Type (Priv_T) then
18454 Error_Msg_N ("generic type cannot have a completion", Full_T);
18455 end if;
18457 -- Check that ancestor interfaces of private and full views are
18458 -- consistent. We omit this check for synchronized types because
18459 -- they are performed on the corresponding record type when frozen.
18461 if Ada_Version >= Ada_2005
18462 and then Is_Tagged_Type (Priv_T)
18463 and then Is_Tagged_Type (Full_T)
18464 and then not Is_Concurrent_Type (Full_T)
18465 then
18466 declare
18467 Iface : Entity_Id;
18468 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
18469 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
18471 begin
18472 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
18473 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
18475 -- Ada 2005 (AI-251): The partial view shall be a descendant of
18476 -- an interface type if and only if the full type is descendant
18477 -- of the interface type (AARM 7.3 (7.3/2)).
18479 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
18481 if Present (Iface) then
18482 Error_Msg_NE
18483 ("interface in partial view& not implemented by full type "
18484 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
18485 end if;
18487 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
18489 if Present (Iface) then
18490 Error_Msg_NE
18491 ("interface & not implemented by partial view "
18492 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
18493 end if;
18494 end;
18495 end if;
18497 if Is_Tagged_Type (Priv_T)
18498 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
18499 and then Is_Derived_Type (Full_T)
18500 then
18501 Priv_Parent := Etype (Priv_T);
18503 -- The full view of a private extension may have been transformed
18504 -- into an unconstrained derived type declaration and a subtype
18505 -- declaration (see build_derived_record_type for details).
18507 if Nkind (N) = N_Subtype_Declaration then
18508 Full_Indic := Subtype_Indication (N);
18509 Full_Parent := Etype (Base_Type (Full_T));
18510 else
18511 Full_Indic := Subtype_Indication (Type_Definition (N));
18512 Full_Parent := Etype (Full_T);
18513 end if;
18515 -- Check that the parent type of the full type is a descendant of
18516 -- the ancestor subtype given in the private extension. If either
18517 -- entity has an Etype equal to Any_Type then we had some previous
18518 -- error situation [7.3(8)].
18520 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
18521 return;
18523 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
18524 -- any order. Therefore we don't have to check that its parent must
18525 -- be a descendant of the parent of the private type declaration.
18527 elsif Is_Interface (Priv_Parent)
18528 and then Is_Interface (Full_Parent)
18529 then
18530 null;
18532 -- Ada 2005 (AI-251): If the parent of the private type declaration
18533 -- is an interface there is no need to check that it is an ancestor
18534 -- of the associated full type declaration. The required tests for
18535 -- this case are performed by Build_Derived_Record_Type.
18537 elsif not Is_Interface (Base_Type (Priv_Parent))
18538 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
18539 then
18540 Error_Msg_N
18541 ("parent of full type must descend from parent"
18542 & " of private extension", Full_Indic);
18544 -- First check a formal restriction, and then proceed with checking
18545 -- Ada rules. Since the formal restriction is not a serious error, we
18546 -- don't prevent further error detection for this check, hence the
18547 -- ELSE.
18549 else
18551 -- In formal mode, when completing a private extension the type
18552 -- named in the private part must be exactly the same as that
18553 -- named in the visible part.
18555 if Priv_Parent /= Full_Parent then
18556 Error_Msg_Name_1 := Chars (Priv_Parent);
18557 Check_SPARK_05_Restriction ("% expected", Full_Indic);
18558 end if;
18560 -- Check the rules of 7.3(10): if the private extension inherits
18561 -- known discriminants, then the full type must also inherit those
18562 -- discriminants from the same (ancestor) type, and the parent
18563 -- subtype of the full type must be constrained if and only if
18564 -- the ancestor subtype of the private extension is constrained.
18566 if No (Discriminant_Specifications (Parent (Priv_T)))
18567 and then not Has_Unknown_Discriminants (Priv_T)
18568 and then Has_Discriminants (Base_Type (Priv_Parent))
18569 then
18570 declare
18571 Priv_Indic : constant Node_Id :=
18572 Subtype_Indication (Parent (Priv_T));
18574 Priv_Constr : constant Boolean :=
18575 Is_Constrained (Priv_Parent)
18576 or else
18577 Nkind (Priv_Indic) = N_Subtype_Indication
18578 or else
18579 Is_Constrained (Entity (Priv_Indic));
18581 Full_Constr : constant Boolean :=
18582 Is_Constrained (Full_Parent)
18583 or else
18584 Nkind (Full_Indic) = N_Subtype_Indication
18585 or else
18586 Is_Constrained (Entity (Full_Indic));
18588 Priv_Discr : Entity_Id;
18589 Full_Discr : Entity_Id;
18591 begin
18592 Priv_Discr := First_Discriminant (Priv_Parent);
18593 Full_Discr := First_Discriminant (Full_Parent);
18594 while Present (Priv_Discr) and then Present (Full_Discr) loop
18595 if Original_Record_Component (Priv_Discr) =
18596 Original_Record_Component (Full_Discr)
18597 or else
18598 Corresponding_Discriminant (Priv_Discr) =
18599 Corresponding_Discriminant (Full_Discr)
18600 then
18601 null;
18602 else
18603 exit;
18604 end if;
18606 Next_Discriminant (Priv_Discr);
18607 Next_Discriminant (Full_Discr);
18608 end loop;
18610 if Present (Priv_Discr) or else Present (Full_Discr) then
18611 Error_Msg_N
18612 ("full view must inherit discriminants of the parent"
18613 & " type used in the private extension", Full_Indic);
18615 elsif Priv_Constr and then not Full_Constr then
18616 Error_Msg_N
18617 ("parent subtype of full type must be constrained",
18618 Full_Indic);
18620 elsif Full_Constr and then not Priv_Constr then
18621 Error_Msg_N
18622 ("parent subtype of full type must be unconstrained",
18623 Full_Indic);
18624 end if;
18625 end;
18627 -- Check the rules of 7.3(12): if a partial view has neither
18628 -- known or unknown discriminants, then the full type
18629 -- declaration shall define a definite subtype.
18631 elsif not Has_Unknown_Discriminants (Priv_T)
18632 and then not Has_Discriminants (Priv_T)
18633 and then not Is_Constrained (Full_T)
18634 then
18635 Error_Msg_N
18636 ("full view must define a constrained type if partial view"
18637 & " has no discriminants", Full_T);
18638 end if;
18640 -- ??????? Do we implement the following properly ?????
18641 -- If the ancestor subtype of a private extension has constrained
18642 -- discriminants, then the parent subtype of the full view shall
18643 -- impose a statically matching constraint on those discriminants
18644 -- [7.3(13)].
18645 end if;
18647 else
18648 -- For untagged types, verify that a type without discriminants is
18649 -- not completed with an unconstrained type. A separate error message
18650 -- is produced if the full type has defaulted discriminants.
18652 if not Is_Indefinite_Subtype (Priv_T)
18653 and then Is_Indefinite_Subtype (Full_T)
18654 then
18655 Error_Msg_Sloc := Sloc (Parent (Priv_T));
18656 Error_Msg_NE
18657 ("full view of& not compatible with declaration#",
18658 Full_T, Priv_T);
18660 if not Is_Tagged_Type (Full_T) then
18661 Error_Msg_N
18662 ("\one is constrained, the other unconstrained", Full_T);
18663 end if;
18664 end if;
18665 end if;
18667 -- AI-419: verify that the use of "limited" is consistent
18669 declare
18670 Orig_Decl : constant Node_Id := Original_Node (N);
18672 begin
18673 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
18674 and then not Limited_Present (Parent (Priv_T))
18675 and then not Synchronized_Present (Parent (Priv_T))
18676 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
18677 and then Nkind
18678 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
18679 and then Limited_Present (Type_Definition (Orig_Decl))
18680 then
18681 Error_Msg_N
18682 ("full view of non-limited extension cannot be limited", N);
18683 end if;
18684 end;
18686 -- Ada 2005 (AI-443): A synchronized private extension must be
18687 -- completed by a task or protected type.
18689 if Ada_Version >= Ada_2005
18690 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
18691 and then Synchronized_Present (Parent (Priv_T))
18692 and then not Is_Concurrent_Type (Full_T)
18693 then
18694 Error_Msg_N ("full view of synchronized extension must " &
18695 "be synchronized type", N);
18696 end if;
18698 -- Ada 2005 AI-363: if the full view has discriminants with
18699 -- defaults, it is illegal to declare constrained access subtypes
18700 -- whose designated type is the current type. This allows objects
18701 -- of the type that are declared in the heap to be unconstrained.
18703 if not Has_Unknown_Discriminants (Priv_T)
18704 and then not Has_Discriminants (Priv_T)
18705 and then Has_Discriminants (Full_T)
18706 and then
18707 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
18708 then
18709 Set_Has_Constrained_Partial_View (Full_T);
18710 Set_Has_Constrained_Partial_View (Priv_T);
18711 end if;
18713 -- Create a full declaration for all its subtypes recorded in
18714 -- Private_Dependents and swap them similarly to the base type. These
18715 -- are subtypes that have been define before the full declaration of
18716 -- the private type. We also swap the entry in Private_Dependents list
18717 -- so we can properly restore the private view on exit from the scope.
18719 declare
18720 Priv_Elmt : Elmt_Id;
18721 Priv_Scop : Entity_Id;
18722 Priv : Entity_Id;
18723 Full : Entity_Id;
18725 begin
18726 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
18727 while Present (Priv_Elmt) loop
18728 Priv := Node (Priv_Elmt);
18729 Priv_Scop := Scope (Priv);
18731 if Ekind_In (Priv, E_Private_Subtype,
18732 E_Limited_Private_Subtype,
18733 E_Record_Subtype_With_Private)
18734 then
18735 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
18736 Set_Is_Itype (Full);
18737 Set_Parent (Full, Parent (Priv));
18738 Set_Associated_Node_For_Itype (Full, N);
18740 -- Now we need to complete the private subtype, but since the
18741 -- base type has already been swapped, we must also swap the
18742 -- subtypes (and thus, reverse the arguments in the call to
18743 -- Complete_Private_Subtype). Also note that we may need to
18744 -- re-establish the scope of the private subtype.
18746 Copy_And_Swap (Priv, Full);
18748 if not In_Open_Scopes (Priv_Scop) then
18749 Push_Scope (Priv_Scop);
18751 else
18752 -- Reset Priv_Scop to Empty to indicate no scope was pushed
18754 Priv_Scop := Empty;
18755 end if;
18757 Complete_Private_Subtype (Full, Priv, Full_T, N);
18759 if Present (Priv_Scop) then
18760 Pop_Scope;
18761 end if;
18763 Replace_Elmt (Priv_Elmt, Full);
18764 end if;
18766 Next_Elmt (Priv_Elmt);
18767 end loop;
18768 end;
18770 -- If the private view was tagged, copy the new primitive operations
18771 -- from the private view to the full view.
18773 if Is_Tagged_Type (Full_T) then
18774 declare
18775 Disp_Typ : Entity_Id;
18776 Full_List : Elist_Id;
18777 Prim : Entity_Id;
18778 Prim_Elmt : Elmt_Id;
18779 Priv_List : Elist_Id;
18781 function Contains
18782 (E : Entity_Id;
18783 L : Elist_Id) return Boolean;
18784 -- Determine whether list L contains element E
18786 --------------
18787 -- Contains --
18788 --------------
18790 function Contains
18791 (E : Entity_Id;
18792 L : Elist_Id) return Boolean
18794 List_Elmt : Elmt_Id;
18796 begin
18797 List_Elmt := First_Elmt (L);
18798 while Present (List_Elmt) loop
18799 if Node (List_Elmt) = E then
18800 return True;
18801 end if;
18803 Next_Elmt (List_Elmt);
18804 end loop;
18806 return False;
18807 end Contains;
18809 -- Start of processing
18811 begin
18812 if Is_Tagged_Type (Priv_T) then
18813 Priv_List := Primitive_Operations (Priv_T);
18814 Prim_Elmt := First_Elmt (Priv_List);
18816 -- In the case of a concurrent type completing a private tagged
18817 -- type, primitives may have been declared in between the two
18818 -- views. These subprograms need to be wrapped the same way
18819 -- entries and protected procedures are handled because they
18820 -- cannot be directly shared by the two views.
18822 if Is_Concurrent_Type (Full_T) then
18823 declare
18824 Conc_Typ : constant Entity_Id :=
18825 Corresponding_Record_Type (Full_T);
18826 Curr_Nod : Node_Id := Parent (Conc_Typ);
18827 Wrap_Spec : Node_Id;
18829 begin
18830 while Present (Prim_Elmt) loop
18831 Prim := Node (Prim_Elmt);
18833 if Comes_From_Source (Prim)
18834 and then not Is_Abstract_Subprogram (Prim)
18835 then
18836 Wrap_Spec :=
18837 Make_Subprogram_Declaration (Sloc (Prim),
18838 Specification =>
18839 Build_Wrapper_Spec
18840 (Subp_Id => Prim,
18841 Obj_Typ => Conc_Typ,
18842 Formals =>
18843 Parameter_Specifications (
18844 Parent (Prim))));
18846 Insert_After (Curr_Nod, Wrap_Spec);
18847 Curr_Nod := Wrap_Spec;
18849 Analyze (Wrap_Spec);
18850 end if;
18852 Next_Elmt (Prim_Elmt);
18853 end loop;
18855 return;
18856 end;
18858 -- For non-concurrent types, transfer explicit primitives, but
18859 -- omit those inherited from the parent of the private view
18860 -- since they will be re-inherited later on.
18862 else
18863 Full_List := Primitive_Operations (Full_T);
18865 while Present (Prim_Elmt) loop
18866 Prim := Node (Prim_Elmt);
18868 if Comes_From_Source (Prim)
18869 and then not Contains (Prim, Full_List)
18870 then
18871 Append_Elmt (Prim, Full_List);
18872 end if;
18874 Next_Elmt (Prim_Elmt);
18875 end loop;
18876 end if;
18878 -- Untagged private view
18880 else
18881 Full_List := Primitive_Operations (Full_T);
18883 -- In this case the partial view is untagged, so here we locate
18884 -- all of the earlier primitives that need to be treated as
18885 -- dispatching (those that appear between the two views). Note
18886 -- that these additional operations must all be new operations
18887 -- (any earlier operations that override inherited operations
18888 -- of the full view will already have been inserted in the
18889 -- primitives list, marked by Check_Operation_From_Private_View
18890 -- as dispatching. Note that implicit "/=" operators are
18891 -- excluded from being added to the primitives list since they
18892 -- shouldn't be treated as dispatching (tagged "/=" is handled
18893 -- specially).
18895 Prim := Next_Entity (Full_T);
18896 while Present (Prim) and then Prim /= Priv_T loop
18897 if Ekind_In (Prim, E_Procedure, E_Function) then
18898 Disp_Typ := Find_Dispatching_Type (Prim);
18900 if Disp_Typ = Full_T
18901 and then (Chars (Prim) /= Name_Op_Ne
18902 or else Comes_From_Source (Prim))
18903 then
18904 Check_Controlling_Formals (Full_T, Prim);
18906 if not Is_Dispatching_Operation (Prim) then
18907 Append_Elmt (Prim, Full_List);
18908 Set_Is_Dispatching_Operation (Prim, True);
18909 Set_DT_Position (Prim, No_Uint);
18910 end if;
18912 elsif Is_Dispatching_Operation (Prim)
18913 and then Disp_Typ /= Full_T
18914 then
18916 -- Verify that it is not otherwise controlled by a
18917 -- formal or a return value of type T.
18919 Check_Controlling_Formals (Disp_Typ, Prim);
18920 end if;
18921 end if;
18923 Next_Entity (Prim);
18924 end loop;
18925 end if;
18927 -- For the tagged case, the two views can share the same primitive
18928 -- operations list and the same class-wide type. Update attributes
18929 -- of the class-wide type which depend on the full declaration.
18931 if Is_Tagged_Type (Priv_T) then
18932 Set_Direct_Primitive_Operations (Priv_T, Full_List);
18933 Set_Class_Wide_Type
18934 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
18936 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
18937 Set_Has_Protected
18938 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
18939 end if;
18940 end;
18941 end if;
18943 -- Ada 2005 AI 161: Check preelaborable initialization consistency
18945 if Known_To_Have_Preelab_Init (Priv_T) then
18947 -- Case where there is a pragma Preelaborable_Initialization. We
18948 -- always allow this in predefined units, which is cheating a bit,
18949 -- but it means we don't have to struggle to meet the requirements in
18950 -- the RM for having Preelaborable Initialization. Otherwise we
18951 -- require that the type meets the RM rules. But we can't check that
18952 -- yet, because of the rule about overriding Initialize, so we simply
18953 -- set a flag that will be checked at freeze time.
18955 if not In_Predefined_Unit (Full_T) then
18956 Set_Must_Have_Preelab_Init (Full_T);
18957 end if;
18958 end if;
18960 -- If pragma CPP_Class was applied to the private type declaration,
18961 -- propagate it now to the full type declaration.
18963 if Is_CPP_Class (Priv_T) then
18964 Set_Is_CPP_Class (Full_T);
18965 Set_Convention (Full_T, Convention_CPP);
18967 -- Check that components of imported CPP types do not have default
18968 -- expressions.
18970 Check_CPP_Type_Has_No_Defaults (Full_T);
18971 end if;
18973 -- If the private view has user specified stream attributes, then so has
18974 -- the full view.
18976 -- Why the test, how could these flags be already set in Full_T ???
18978 if Has_Specified_Stream_Read (Priv_T) then
18979 Set_Has_Specified_Stream_Read (Full_T);
18980 end if;
18982 if Has_Specified_Stream_Write (Priv_T) then
18983 Set_Has_Specified_Stream_Write (Full_T);
18984 end if;
18986 if Has_Specified_Stream_Input (Priv_T) then
18987 Set_Has_Specified_Stream_Input (Full_T);
18988 end if;
18990 if Has_Specified_Stream_Output (Priv_T) then
18991 Set_Has_Specified_Stream_Output (Full_T);
18992 end if;
18994 -- Propagate the attributes related to pragma Default_Initial_Condition
18995 -- from the private to the full view. Note that both flags are mutually
18996 -- exclusive.
18998 if Has_Inherited_Default_Init_Cond (Priv_T) then
18999 Set_Has_Inherited_Default_Init_Cond (Full_T);
19000 Set_Default_Init_Cond_Procedure
19001 (Full_T, Default_Init_Cond_Procedure (Priv_T));
19003 elsif Has_Default_Init_Cond (Priv_T) then
19004 Set_Has_Default_Init_Cond (Full_T);
19005 Set_Default_Init_Cond_Procedure
19006 (Full_T, Default_Init_Cond_Procedure (Priv_T));
19007 end if;
19009 -- Propagate invariants to full type
19011 if Has_Invariants (Priv_T) then
19012 Set_Has_Invariants (Full_T);
19013 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19014 end if;
19016 if Has_Inheritable_Invariants (Priv_T) then
19017 Set_Has_Inheritable_Invariants (Full_T);
19018 end if;
19020 -- Propagate predicates to full type, and predicate function if already
19021 -- defined. It is not clear that this can actually happen? the partial
19022 -- view cannot be frozen yet, and the predicate function has not been
19023 -- built. Still it is a cheap check and seems safer to make it.
19025 if Has_Predicates (Priv_T) then
19026 if Present (Predicate_Function (Priv_T)) then
19027 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
19028 end if;
19030 Set_Has_Predicates (Full_T);
19031 end if;
19032 end Process_Full_View;
19034 -----------------------------------
19035 -- Process_Incomplete_Dependents --
19036 -----------------------------------
19038 procedure Process_Incomplete_Dependents
19039 (N : Node_Id;
19040 Full_T : Entity_Id;
19041 Inc_T : Entity_Id)
19043 Inc_Elmt : Elmt_Id;
19044 Priv_Dep : Entity_Id;
19045 New_Subt : Entity_Id;
19047 Disc_Constraint : Elist_Id;
19049 begin
19050 if No (Private_Dependents (Inc_T)) then
19051 return;
19052 end if;
19054 -- Itypes that may be generated by the completion of an incomplete
19055 -- subtype are not used by the back-end and not attached to the tree.
19056 -- They are created only for constraint-checking purposes.
19058 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
19059 while Present (Inc_Elmt) loop
19060 Priv_Dep := Node (Inc_Elmt);
19062 if Ekind (Priv_Dep) = E_Subprogram_Type then
19064 -- An Access_To_Subprogram type may have a return type or a
19065 -- parameter type that is incomplete. Replace with the full view.
19067 if Etype (Priv_Dep) = Inc_T then
19068 Set_Etype (Priv_Dep, Full_T);
19069 end if;
19071 declare
19072 Formal : Entity_Id;
19074 begin
19075 Formal := First_Formal (Priv_Dep);
19076 while Present (Formal) loop
19077 if Etype (Formal) = Inc_T then
19078 Set_Etype (Formal, Full_T);
19079 end if;
19081 Next_Formal (Formal);
19082 end loop;
19083 end;
19085 elsif Is_Overloadable (Priv_Dep) then
19087 -- If a subprogram in the incomplete dependents list is primitive
19088 -- for a tagged full type then mark it as a dispatching operation,
19089 -- check whether it overrides an inherited subprogram, and check
19090 -- restrictions on its controlling formals. Note that a protected
19091 -- operation is never dispatching: only its wrapper operation
19092 -- (which has convention Ada) is.
19094 if Is_Tagged_Type (Full_T)
19095 and then Is_Primitive (Priv_Dep)
19096 and then Convention (Priv_Dep) /= Convention_Protected
19097 then
19098 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
19099 Set_Is_Dispatching_Operation (Priv_Dep);
19100 Check_Controlling_Formals (Full_T, Priv_Dep);
19101 end if;
19103 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
19105 -- Can happen during processing of a body before the completion
19106 -- of a TA type. Ignore, because spec is also on dependent list.
19108 return;
19110 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
19111 -- corresponding subtype of the full view.
19113 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
19114 Set_Subtype_Indication
19115 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
19116 Set_Etype (Priv_Dep, Full_T);
19117 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
19118 Set_Analyzed (Parent (Priv_Dep), False);
19120 -- Reanalyze the declaration, suppressing the call to
19121 -- Enter_Name to avoid duplicate names.
19123 Analyze_Subtype_Declaration
19124 (N => Parent (Priv_Dep),
19125 Skip => True);
19127 -- Dependent is a subtype
19129 else
19130 -- We build a new subtype indication using the full view of the
19131 -- incomplete parent. The discriminant constraints have been
19132 -- elaborated already at the point of the subtype declaration.
19134 New_Subt := Create_Itype (E_Void, N);
19136 if Has_Discriminants (Full_T) then
19137 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
19138 else
19139 Disc_Constraint := No_Elist;
19140 end if;
19142 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
19143 Set_Full_View (Priv_Dep, New_Subt);
19144 end if;
19146 Next_Elmt (Inc_Elmt);
19147 end loop;
19148 end Process_Incomplete_Dependents;
19150 --------------------------------
19151 -- Process_Range_Expr_In_Decl --
19152 --------------------------------
19154 procedure Process_Range_Expr_In_Decl
19155 (R : Node_Id;
19156 T : Entity_Id;
19157 Subtyp : Entity_Id := Empty;
19158 Check_List : List_Id := Empty_List;
19159 R_Check_Off : Boolean := False;
19160 In_Iter_Schm : Boolean := False)
19162 Lo, Hi : Node_Id;
19163 R_Checks : Check_Result;
19164 Insert_Node : Node_Id;
19165 Def_Id : Entity_Id;
19167 begin
19168 Analyze_And_Resolve (R, Base_Type (T));
19170 if Nkind (R) = N_Range then
19172 -- In SPARK, all ranges should be static, with the exception of the
19173 -- discrete type definition of a loop parameter specification.
19175 if not In_Iter_Schm
19176 and then not Is_OK_Static_Range (R)
19177 then
19178 Check_SPARK_05_Restriction ("range should be static", R);
19179 end if;
19181 Lo := Low_Bound (R);
19182 Hi := High_Bound (R);
19184 -- We need to ensure validity of the bounds here, because if we
19185 -- go ahead and do the expansion, then the expanded code will get
19186 -- analyzed with range checks suppressed and we miss the check.
19187 -- Validity checks on the range of a quantified expression are
19188 -- delayed until the construct is transformed into a loop.
19190 if Nkind (Parent (R)) /= N_Loop_Parameter_Specification
19191 or else Nkind (Parent (Parent (R))) /= N_Quantified_Expression
19192 then
19193 Validity_Check_Range (R);
19194 end if;
19196 -- If there were errors in the declaration, try and patch up some
19197 -- common mistakes in the bounds. The cases handled are literals
19198 -- which are Integer where the expected type is Real and vice versa.
19199 -- These corrections allow the compilation process to proceed further
19200 -- along since some basic assumptions of the format of the bounds
19201 -- are guaranteed.
19203 if Etype (R) = Any_Type then
19204 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
19205 Rewrite (Lo,
19206 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
19208 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
19209 Rewrite (Hi,
19210 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
19212 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
19213 Rewrite (Lo,
19214 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
19216 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
19217 Rewrite (Hi,
19218 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
19219 end if;
19221 Set_Etype (Lo, T);
19222 Set_Etype (Hi, T);
19223 end if;
19225 -- If the bounds of the range have been mistakenly given as string
19226 -- literals (perhaps in place of character literals), then an error
19227 -- has already been reported, but we rewrite the string literal as a
19228 -- bound of the range's type to avoid blowups in later processing
19229 -- that looks at static values.
19231 if Nkind (Lo) = N_String_Literal then
19232 Rewrite (Lo,
19233 Make_Attribute_Reference (Sloc (Lo),
19234 Attribute_Name => Name_First,
19235 Prefix => New_Occurrence_Of (T, Sloc (Lo))));
19236 Analyze_And_Resolve (Lo);
19237 end if;
19239 if Nkind (Hi) = N_String_Literal then
19240 Rewrite (Hi,
19241 Make_Attribute_Reference (Sloc (Hi),
19242 Attribute_Name => Name_First,
19243 Prefix => New_Occurrence_Of (T, Sloc (Hi))));
19244 Analyze_And_Resolve (Hi);
19245 end if;
19247 -- If bounds aren't scalar at this point then exit, avoiding
19248 -- problems with further processing of the range in this procedure.
19250 if not Is_Scalar_Type (Etype (Lo)) then
19251 return;
19252 end if;
19254 -- Resolve (actually Sem_Eval) has checked that the bounds are in
19255 -- then range of the base type. Here we check whether the bounds
19256 -- are in the range of the subtype itself. Note that if the bounds
19257 -- represent the null range the Constraint_Error exception should
19258 -- not be raised.
19260 -- ??? The following code should be cleaned up as follows
19262 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
19263 -- is done in the call to Range_Check (R, T); below
19265 -- 2. The use of R_Check_Off should be investigated and possibly
19266 -- removed, this would clean up things a bit.
19268 if Is_Null_Range (Lo, Hi) then
19269 null;
19271 else
19272 -- Capture values of bounds and generate temporaries for them
19273 -- if needed, before applying checks, since checks may cause
19274 -- duplication of the expression without forcing evaluation.
19276 -- The forced evaluation removes side effects from expressions,
19277 -- which should occur also in GNATprove mode. Otherwise, we end up
19278 -- with unexpected insertions of actions at places where this is
19279 -- not supposed to occur, e.g. on default parameters of a call.
19281 if Expander_Active or GNATprove_Mode then
19283 -- If no subtype name, then just call Force_Evaluation to
19284 -- create declarations as needed to deal with side effects.
19285 -- Also ignore calls from within a record type, where we
19286 -- have possible scoping issues.
19288 if No (Subtyp) or else Is_Record_Type (Current_Scope) then
19289 Force_Evaluation (Lo);
19290 Force_Evaluation (Hi);
19292 -- If a subtype is given, then we capture the bounds if they
19293 -- are not known at compile time, using constant identifiers
19294 -- xxx_FIRST and xxx_LAST where xxx is the name of the subtype.
19296 -- Note: we do this transformation even if expansion is not
19297 -- active, and in particular we do it in GNATprove_Mode since
19298 -- the transformation is in general required to ensure that the
19299 -- resulting tree has proper Ada semantics.
19301 -- Historical note: We used to just do Force_Evaluation calls
19302 -- in all cases, but it is better to capture the bounds with
19303 -- proper non-serialized names, since these will be accessed
19304 -- from other units, and hence may be public, and also we can
19305 -- then expand 'First and 'Last references to be references to
19306 -- these special names.
19308 else
19309 if not Compile_Time_Known_Value (Lo)
19311 -- No need to capture bounds if they already are
19312 -- references to constants.
19314 and then not (Is_Entity_Name (Lo)
19315 and then Is_Constant_Object (Entity (Lo)))
19316 then
19317 declare
19318 Loc : constant Source_Ptr := Sloc (Lo);
19319 Lov : constant Entity_Id :=
19320 Make_Defining_Identifier (Loc,
19321 Chars =>
19322 New_External_Name (Chars (Subtyp), "_FIRST"));
19323 begin
19324 Insert_Action (R,
19325 Make_Object_Declaration (Loc,
19326 Defining_Identifier => Lov,
19327 Object_Definition =>
19328 New_Occurrence_Of (Base_Type (T), Loc),
19329 Constant_Present => True,
19330 Expression => Relocate_Node (Lo)));
19331 Rewrite (Lo, New_Occurrence_Of (Lov, Loc));
19332 end;
19333 end if;
19335 if not Compile_Time_Known_Value (Hi)
19336 and then not (Is_Entity_Name (Hi)
19337 and then Is_Constant_Object (Entity (Hi)))
19338 then
19339 declare
19340 Loc : constant Source_Ptr := Sloc (Hi);
19341 Hiv : constant Entity_Id :=
19342 Make_Defining_Identifier (Loc,
19343 Chars =>
19344 New_External_Name (Chars (Subtyp), "_LAST"));
19345 begin
19346 Insert_Action (R,
19347 Make_Object_Declaration (Loc,
19348 Defining_Identifier => Hiv,
19349 Object_Definition =>
19350 New_Occurrence_Of (Base_Type (T), Loc),
19351 Constant_Present => True,
19352 Expression => Relocate_Node (Hi)));
19353 Rewrite (Hi, New_Occurrence_Of (Hiv, Loc));
19354 end;
19355 end if;
19356 end if;
19357 end if;
19359 -- We use a flag here instead of suppressing checks on the
19360 -- type because the type we check against isn't necessarily
19361 -- the place where we put the check.
19363 if not R_Check_Off then
19364 R_Checks := Get_Range_Checks (R, T);
19366 -- Look up tree to find an appropriate insertion point. We
19367 -- can't just use insert_actions because later processing
19368 -- depends on the insertion node. Prior to Ada 2012 the
19369 -- insertion point could only be a declaration or a loop, but
19370 -- quantified expressions can appear within any context in an
19371 -- expression, and the insertion point can be any statement,
19372 -- pragma, or declaration.
19374 Insert_Node := Parent (R);
19375 while Present (Insert_Node) loop
19376 exit when
19377 Nkind (Insert_Node) in N_Declaration
19378 and then
19379 not Nkind_In
19380 (Insert_Node, N_Component_Declaration,
19381 N_Loop_Parameter_Specification,
19382 N_Function_Specification,
19383 N_Procedure_Specification);
19385 exit when Nkind (Insert_Node) in N_Later_Decl_Item
19386 or else Nkind (Insert_Node) in
19387 N_Statement_Other_Than_Procedure_Call
19388 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
19389 N_Pragma);
19391 Insert_Node := Parent (Insert_Node);
19392 end loop;
19394 -- Why would Type_Decl not be present??? Without this test,
19395 -- short regression tests fail.
19397 if Present (Insert_Node) then
19399 -- Case of loop statement. Verify that the range is part
19400 -- of the subtype indication of the iteration scheme.
19402 if Nkind (Insert_Node) = N_Loop_Statement then
19403 declare
19404 Indic : Node_Id;
19406 begin
19407 Indic := Parent (R);
19408 while Present (Indic)
19409 and then Nkind (Indic) /= N_Subtype_Indication
19410 loop
19411 Indic := Parent (Indic);
19412 end loop;
19414 if Present (Indic) then
19415 Def_Id := Etype (Subtype_Mark (Indic));
19417 Insert_Range_Checks
19418 (R_Checks,
19419 Insert_Node,
19420 Def_Id,
19421 Sloc (Insert_Node),
19423 Do_Before => True);
19424 end if;
19425 end;
19427 -- Insertion before a declaration. If the declaration
19428 -- includes discriminants, the list of applicable checks
19429 -- is given by the caller.
19431 elsif Nkind (Insert_Node) in N_Declaration then
19432 Def_Id := Defining_Identifier (Insert_Node);
19434 if (Ekind (Def_Id) = E_Record_Type
19435 and then Depends_On_Discriminant (R))
19436 or else
19437 (Ekind (Def_Id) = E_Protected_Type
19438 and then Has_Discriminants (Def_Id))
19439 then
19440 Append_Range_Checks
19441 (R_Checks,
19442 Check_List, Def_Id, Sloc (Insert_Node), R);
19444 else
19445 Insert_Range_Checks
19446 (R_Checks,
19447 Insert_Node, Def_Id, Sloc (Insert_Node), R);
19449 end if;
19451 -- Insertion before a statement. Range appears in the
19452 -- context of a quantified expression. Insertion will
19453 -- take place when expression is expanded.
19455 else
19456 null;
19457 end if;
19458 end if;
19459 end if;
19460 end if;
19462 -- Case of other than an explicit N_Range node
19464 -- The forced evaluation removes side effects from expressions, which
19465 -- should occur also in GNATprove mode. Otherwise, we end up with
19466 -- unexpected insertions of actions at places where this is not
19467 -- supposed to occur, e.g. on default parameters of a call.
19469 elsif Expander_Active or GNATprove_Mode then
19470 Get_Index_Bounds (R, Lo, Hi);
19471 Force_Evaluation (Lo);
19472 Force_Evaluation (Hi);
19473 end if;
19474 end Process_Range_Expr_In_Decl;
19476 --------------------------------------
19477 -- Process_Real_Range_Specification --
19478 --------------------------------------
19480 procedure Process_Real_Range_Specification (Def : Node_Id) is
19481 Spec : constant Node_Id := Real_Range_Specification (Def);
19482 Lo : Node_Id;
19483 Hi : Node_Id;
19484 Err : Boolean := False;
19486 procedure Analyze_Bound (N : Node_Id);
19487 -- Analyze and check one bound
19489 -------------------
19490 -- Analyze_Bound --
19491 -------------------
19493 procedure Analyze_Bound (N : Node_Id) is
19494 begin
19495 Analyze_And_Resolve (N, Any_Real);
19497 if not Is_OK_Static_Expression (N) then
19498 Flag_Non_Static_Expr
19499 ("bound in real type definition is not static!", N);
19500 Err := True;
19501 end if;
19502 end Analyze_Bound;
19504 -- Start of processing for Process_Real_Range_Specification
19506 begin
19507 if Present (Spec) then
19508 Lo := Low_Bound (Spec);
19509 Hi := High_Bound (Spec);
19510 Analyze_Bound (Lo);
19511 Analyze_Bound (Hi);
19513 -- If error, clear away junk range specification
19515 if Err then
19516 Set_Real_Range_Specification (Def, Empty);
19517 end if;
19518 end if;
19519 end Process_Real_Range_Specification;
19521 ---------------------
19522 -- Process_Subtype --
19523 ---------------------
19525 function Process_Subtype
19526 (S : Node_Id;
19527 Related_Nod : Node_Id;
19528 Related_Id : Entity_Id := Empty;
19529 Suffix : Character := ' ') return Entity_Id
19531 P : Node_Id;
19532 Def_Id : Entity_Id;
19533 Error_Node : Node_Id;
19534 Full_View_Id : Entity_Id;
19535 Subtype_Mark_Id : Entity_Id;
19537 May_Have_Null_Exclusion : Boolean;
19539 procedure Check_Incomplete (T : Entity_Id);
19540 -- Called to verify that an incomplete type is not used prematurely
19542 ----------------------
19543 -- Check_Incomplete --
19544 ----------------------
19546 procedure Check_Incomplete (T : Entity_Id) is
19547 begin
19548 -- Ada 2005 (AI-412): Incomplete subtypes are legal
19550 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
19551 and then
19552 not (Ada_Version >= Ada_2005
19553 and then
19554 (Nkind (Parent (T)) = N_Subtype_Declaration
19555 or else
19556 (Nkind (Parent (T)) = N_Subtype_Indication
19557 and then Nkind (Parent (Parent (T))) =
19558 N_Subtype_Declaration)))
19559 then
19560 Error_Msg_N ("invalid use of type before its full declaration", T);
19561 end if;
19562 end Check_Incomplete;
19564 -- Start of processing for Process_Subtype
19566 begin
19567 -- Case of no constraints present
19569 if Nkind (S) /= N_Subtype_Indication then
19570 Find_Type (S);
19571 Check_Incomplete (S);
19572 P := Parent (S);
19574 -- Ada 2005 (AI-231): Static check
19576 if Ada_Version >= Ada_2005
19577 and then Present (P)
19578 and then Null_Exclusion_Present (P)
19579 and then Nkind (P) /= N_Access_To_Object_Definition
19580 and then not Is_Access_Type (Entity (S))
19581 then
19582 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
19583 end if;
19585 -- The following is ugly, can't we have a range or even a flag???
19587 May_Have_Null_Exclusion :=
19588 Nkind_In (P, N_Access_Definition,
19589 N_Access_Function_Definition,
19590 N_Access_Procedure_Definition,
19591 N_Access_To_Object_Definition,
19592 N_Allocator,
19593 N_Component_Definition)
19594 or else
19595 Nkind_In (P, N_Derived_Type_Definition,
19596 N_Discriminant_Specification,
19597 N_Formal_Object_Declaration,
19598 N_Object_Declaration,
19599 N_Object_Renaming_Declaration,
19600 N_Parameter_Specification,
19601 N_Subtype_Declaration);
19603 -- Create an Itype that is a duplicate of Entity (S) but with the
19604 -- null-exclusion attribute.
19606 if May_Have_Null_Exclusion
19607 and then Is_Access_Type (Entity (S))
19608 and then Null_Exclusion_Present (P)
19610 -- No need to check the case of an access to object definition.
19611 -- It is correct to define double not-null pointers.
19613 -- Example:
19614 -- type Not_Null_Int_Ptr is not null access Integer;
19615 -- type Acc is not null access Not_Null_Int_Ptr;
19617 and then Nkind (P) /= N_Access_To_Object_Definition
19618 then
19619 if Can_Never_Be_Null (Entity (S)) then
19620 case Nkind (Related_Nod) is
19621 when N_Full_Type_Declaration =>
19622 if Nkind (Type_Definition (Related_Nod))
19623 in N_Array_Type_Definition
19624 then
19625 Error_Node :=
19626 Subtype_Indication
19627 (Component_Definition
19628 (Type_Definition (Related_Nod)));
19629 else
19630 Error_Node :=
19631 Subtype_Indication (Type_Definition (Related_Nod));
19632 end if;
19634 when N_Subtype_Declaration =>
19635 Error_Node := Subtype_Indication (Related_Nod);
19637 when N_Object_Declaration =>
19638 Error_Node := Object_Definition (Related_Nod);
19640 when N_Component_Declaration =>
19641 Error_Node :=
19642 Subtype_Indication (Component_Definition (Related_Nod));
19644 when N_Allocator =>
19645 Error_Node := Expression (Related_Nod);
19647 when others =>
19648 pragma Assert (False);
19649 Error_Node := Related_Nod;
19650 end case;
19652 Error_Msg_NE
19653 ("`NOT NULL` not allowed (& already excludes null)",
19654 Error_Node,
19655 Entity (S));
19656 end if;
19658 Set_Etype (S,
19659 Create_Null_Excluding_Itype
19660 (T => Entity (S),
19661 Related_Nod => P));
19662 Set_Entity (S, Etype (S));
19663 end if;
19665 return Entity (S);
19667 -- Case of constraint present, so that we have an N_Subtype_Indication
19668 -- node (this node is created only if constraints are present).
19670 else
19671 Find_Type (Subtype_Mark (S));
19673 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
19674 and then not
19675 (Nkind (Parent (S)) = N_Subtype_Declaration
19676 and then Is_Itype (Defining_Identifier (Parent (S))))
19677 then
19678 Check_Incomplete (Subtype_Mark (S));
19679 end if;
19681 P := Parent (S);
19682 Subtype_Mark_Id := Entity (Subtype_Mark (S));
19684 -- Explicit subtype declaration case
19686 if Nkind (P) = N_Subtype_Declaration then
19687 Def_Id := Defining_Identifier (P);
19689 -- Explicit derived type definition case
19691 elsif Nkind (P) = N_Derived_Type_Definition then
19692 Def_Id := Defining_Identifier (Parent (P));
19694 -- Implicit case, the Def_Id must be created as an implicit type.
19695 -- The one exception arises in the case of concurrent types, array
19696 -- and access types, where other subsidiary implicit types may be
19697 -- created and must appear before the main implicit type. In these
19698 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
19699 -- has not yet been called to create Def_Id.
19701 else
19702 if Is_Array_Type (Subtype_Mark_Id)
19703 or else Is_Concurrent_Type (Subtype_Mark_Id)
19704 or else Is_Access_Type (Subtype_Mark_Id)
19705 then
19706 Def_Id := Empty;
19708 -- For the other cases, we create a new unattached Itype,
19709 -- and set the indication to ensure it gets attached later.
19711 else
19712 Def_Id :=
19713 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
19714 end if;
19715 end if;
19717 -- If the kind of constraint is invalid for this kind of type,
19718 -- then give an error, and then pretend no constraint was given.
19720 if not Is_Valid_Constraint_Kind
19721 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
19722 then
19723 Error_Msg_N
19724 ("incorrect constraint for this kind of type", Constraint (S));
19726 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
19728 -- Set Ekind of orphan itype, to prevent cascaded errors
19730 if Present (Def_Id) then
19731 Set_Ekind (Def_Id, Ekind (Any_Type));
19732 end if;
19734 -- Make recursive call, having got rid of the bogus constraint
19736 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
19737 end if;
19739 -- Remaining processing depends on type. Select on Base_Type kind to
19740 -- ensure getting to the concrete type kind in the case of a private
19741 -- subtype (needed when only doing semantic analysis).
19743 case Ekind (Base_Type (Subtype_Mark_Id)) is
19744 when Access_Kind =>
19746 -- If this is a constraint on a class-wide type, discard it.
19747 -- There is currently no way to express a partial discriminant
19748 -- constraint on a type with unknown discriminants. This is
19749 -- a pathology that the ACATS wisely decides not to test.
19751 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
19752 if Comes_From_Source (S) then
19753 Error_Msg_N
19754 ("constraint on class-wide type ignored??",
19755 Constraint (S));
19756 end if;
19758 if Nkind (P) = N_Subtype_Declaration then
19759 Set_Subtype_Indication (P,
19760 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
19761 end if;
19763 return Subtype_Mark_Id;
19764 end if;
19766 Constrain_Access (Def_Id, S, Related_Nod);
19768 if Expander_Active
19769 and then Is_Itype (Designated_Type (Def_Id))
19770 and then Nkind (Related_Nod) = N_Subtype_Declaration
19771 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
19772 then
19773 Build_Itype_Reference
19774 (Designated_Type (Def_Id), Related_Nod);
19775 end if;
19777 when Array_Kind =>
19778 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
19780 when Decimal_Fixed_Point_Kind =>
19781 Constrain_Decimal (Def_Id, S);
19783 when Enumeration_Kind =>
19784 Constrain_Enumeration (Def_Id, S);
19785 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
19787 when Ordinary_Fixed_Point_Kind =>
19788 Constrain_Ordinary_Fixed (Def_Id, S);
19790 when Float_Kind =>
19791 Constrain_Float (Def_Id, S);
19793 when Integer_Kind =>
19794 Constrain_Integer (Def_Id, S);
19795 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
19797 when E_Record_Type |
19798 E_Record_Subtype |
19799 Class_Wide_Kind |
19800 E_Incomplete_Type =>
19801 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
19803 if Ekind (Def_Id) = E_Incomplete_Type then
19804 Set_Private_Dependents (Def_Id, New_Elmt_List);
19805 end if;
19807 when Private_Kind =>
19808 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
19809 Set_Private_Dependents (Def_Id, New_Elmt_List);
19811 -- In case of an invalid constraint prevent further processing
19812 -- since the type constructed is missing expected fields.
19814 if Etype (Def_Id) = Any_Type then
19815 return Def_Id;
19816 end if;
19818 -- If the full view is that of a task with discriminants,
19819 -- we must constrain both the concurrent type and its
19820 -- corresponding record type. Otherwise we will just propagate
19821 -- the constraint to the full view, if available.
19823 if Present (Full_View (Subtype_Mark_Id))
19824 and then Has_Discriminants (Subtype_Mark_Id)
19825 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
19826 then
19827 Full_View_Id :=
19828 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
19830 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
19831 Constrain_Concurrent (Full_View_Id, S,
19832 Related_Nod, Related_Id, Suffix);
19833 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
19834 Set_Full_View (Def_Id, Full_View_Id);
19836 -- Introduce an explicit reference to the private subtype,
19837 -- to prevent scope anomalies in gigi if first use appears
19838 -- in a nested context, e.g. a later function body.
19839 -- Should this be generated in other contexts than a full
19840 -- type declaration?
19842 if Is_Itype (Def_Id)
19843 and then
19844 Nkind (Parent (P)) = N_Full_Type_Declaration
19845 then
19846 Build_Itype_Reference (Def_Id, Parent (P));
19847 end if;
19849 else
19850 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
19851 end if;
19853 when Concurrent_Kind =>
19854 Constrain_Concurrent (Def_Id, S,
19855 Related_Nod, Related_Id, Suffix);
19857 when others =>
19858 Error_Msg_N ("invalid subtype mark in subtype indication", S);
19859 end case;
19861 -- Size and Convention are always inherited from the base type
19863 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
19864 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
19866 return Def_Id;
19867 end if;
19868 end Process_Subtype;
19870 ---------------------------------------
19871 -- Check_Anonymous_Access_Components --
19872 ---------------------------------------
19874 procedure Check_Anonymous_Access_Components
19875 (Typ_Decl : Node_Id;
19876 Typ : Entity_Id;
19877 Prev : Entity_Id;
19878 Comp_List : Node_Id)
19880 Loc : constant Source_Ptr := Sloc (Typ_Decl);
19881 Anon_Access : Entity_Id;
19882 Acc_Def : Node_Id;
19883 Comp : Node_Id;
19884 Comp_Def : Node_Id;
19885 Decl : Node_Id;
19886 Type_Def : Node_Id;
19888 procedure Build_Incomplete_Type_Declaration;
19889 -- If the record type contains components that include an access to the
19890 -- current record, then create an incomplete type declaration for the
19891 -- record, to be used as the designated type of the anonymous access.
19892 -- This is done only once, and only if there is no previous partial
19893 -- view of the type.
19895 function Designates_T (Subt : Node_Id) return Boolean;
19896 -- Check whether a node designates the enclosing record type, or 'Class
19897 -- of that type
19899 function Mentions_T (Acc_Def : Node_Id) return Boolean;
19900 -- Check whether an access definition includes a reference to
19901 -- the enclosing record type. The reference can be a subtype mark
19902 -- in the access definition itself, a 'Class attribute reference, or
19903 -- recursively a reference appearing in a parameter specification
19904 -- or result definition of an access_to_subprogram definition.
19906 --------------------------------------
19907 -- Build_Incomplete_Type_Declaration --
19908 --------------------------------------
19910 procedure Build_Incomplete_Type_Declaration is
19911 Decl : Node_Id;
19912 Inc_T : Entity_Id;
19913 H : Entity_Id;
19915 -- Is_Tagged indicates whether the type is tagged. It is tagged if
19916 -- it's "is new ... with record" or else "is tagged record ...".
19918 Is_Tagged : constant Boolean :=
19919 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
19920 and then
19921 Present
19922 (Record_Extension_Part (Type_Definition (Typ_Decl))))
19923 or else
19924 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
19925 and then Tagged_Present (Type_Definition (Typ_Decl)));
19927 begin
19928 -- If there is a previous partial view, no need to create a new one
19929 -- If the partial view, given by Prev, is incomplete, If Prev is
19930 -- a private declaration, full declaration is flagged accordingly.
19932 if Prev /= Typ then
19933 if Is_Tagged then
19934 Make_Class_Wide_Type (Prev);
19935 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
19936 Set_Etype (Class_Wide_Type (Typ), Typ);
19937 end if;
19939 return;
19941 elsif Has_Private_Declaration (Typ) then
19943 -- If we refer to T'Class inside T, and T is the completion of a
19944 -- private type, then we need to make sure the class-wide type
19945 -- exists.
19947 if Is_Tagged then
19948 Make_Class_Wide_Type (Typ);
19949 end if;
19951 return;
19953 -- If there was a previous anonymous access type, the incomplete
19954 -- type declaration will have been created already.
19956 elsif Present (Current_Entity (Typ))
19957 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
19958 and then Full_View (Current_Entity (Typ)) = Typ
19959 then
19960 if Is_Tagged
19961 and then Comes_From_Source (Current_Entity (Typ))
19962 and then not Is_Tagged_Type (Current_Entity (Typ))
19963 then
19964 Make_Class_Wide_Type (Typ);
19965 Error_Msg_N
19966 ("incomplete view of tagged type should be declared tagged??",
19967 Parent (Current_Entity (Typ)));
19968 end if;
19969 return;
19971 else
19972 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
19973 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
19975 -- Type has already been inserted into the current scope. Remove
19976 -- it, and add incomplete declaration for type, so that subsequent
19977 -- anonymous access types can use it. The entity is unchained from
19978 -- the homonym list and from immediate visibility. After analysis,
19979 -- the entity in the incomplete declaration becomes immediately
19980 -- visible in the record declaration that follows.
19982 H := Current_Entity (Typ);
19984 if H = Typ then
19985 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
19986 else
19987 while Present (H)
19988 and then Homonym (H) /= Typ
19989 loop
19990 H := Homonym (Typ);
19991 end loop;
19993 Set_Homonym (H, Homonym (Typ));
19994 end if;
19996 Insert_Before (Typ_Decl, Decl);
19997 Analyze (Decl);
19998 Set_Full_View (Inc_T, Typ);
20000 if Is_Tagged then
20002 -- Create a common class-wide type for both views, and set the
20003 -- Etype of the class-wide type to the full view.
20005 Make_Class_Wide_Type (Inc_T);
20006 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
20007 Set_Etype (Class_Wide_Type (Typ), Typ);
20008 end if;
20009 end if;
20010 end Build_Incomplete_Type_Declaration;
20012 ------------------
20013 -- Designates_T --
20014 ------------------
20016 function Designates_T (Subt : Node_Id) return Boolean is
20017 Type_Id : constant Name_Id := Chars (Typ);
20019 function Names_T (Nam : Node_Id) return Boolean;
20020 -- The record type has not been introduced in the current scope
20021 -- yet, so we must examine the name of the type itself, either
20022 -- an identifier T, or an expanded name of the form P.T, where
20023 -- P denotes the current scope.
20025 -------------
20026 -- Names_T --
20027 -------------
20029 function Names_T (Nam : Node_Id) return Boolean is
20030 begin
20031 if Nkind (Nam) = N_Identifier then
20032 return Chars (Nam) = Type_Id;
20034 elsif Nkind (Nam) = N_Selected_Component then
20035 if Chars (Selector_Name (Nam)) = Type_Id then
20036 if Nkind (Prefix (Nam)) = N_Identifier then
20037 return Chars (Prefix (Nam)) = Chars (Current_Scope);
20039 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
20040 return Chars (Selector_Name (Prefix (Nam))) =
20041 Chars (Current_Scope);
20042 else
20043 return False;
20044 end if;
20046 else
20047 return False;
20048 end if;
20050 else
20051 return False;
20052 end if;
20053 end Names_T;
20055 -- Start of processing for Designates_T
20057 begin
20058 if Nkind (Subt) = N_Identifier then
20059 return Chars (Subt) = Type_Id;
20061 -- Reference can be through an expanded name which has not been
20062 -- analyzed yet, and which designates enclosing scopes.
20064 elsif Nkind (Subt) = N_Selected_Component then
20065 if Names_T (Subt) then
20066 return True;
20068 -- Otherwise it must denote an entity that is already visible.
20069 -- The access definition may name a subtype of the enclosing
20070 -- type, if there is a previous incomplete declaration for it.
20072 else
20073 Find_Selected_Component (Subt);
20074 return
20075 Is_Entity_Name (Subt)
20076 and then Scope (Entity (Subt)) = Current_Scope
20077 and then
20078 (Chars (Base_Type (Entity (Subt))) = Type_Id
20079 or else
20080 (Is_Class_Wide_Type (Entity (Subt))
20081 and then
20082 Chars (Etype (Base_Type (Entity (Subt)))) =
20083 Type_Id));
20084 end if;
20086 -- A reference to the current type may appear as the prefix of
20087 -- a 'Class attribute.
20089 elsif Nkind (Subt) = N_Attribute_Reference
20090 and then Attribute_Name (Subt) = Name_Class
20091 then
20092 return Names_T (Prefix (Subt));
20094 else
20095 return False;
20096 end if;
20097 end Designates_T;
20099 ----------------
20100 -- Mentions_T --
20101 ----------------
20103 function Mentions_T (Acc_Def : Node_Id) return Boolean is
20104 Param_Spec : Node_Id;
20106 Acc_Subprg : constant Node_Id :=
20107 Access_To_Subprogram_Definition (Acc_Def);
20109 begin
20110 if No (Acc_Subprg) then
20111 return Designates_T (Subtype_Mark (Acc_Def));
20112 end if;
20114 -- Component is an access_to_subprogram: examine its formals,
20115 -- and result definition in the case of an access_to_function.
20117 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
20118 while Present (Param_Spec) loop
20119 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
20120 and then Mentions_T (Parameter_Type (Param_Spec))
20121 then
20122 return True;
20124 elsif Designates_T (Parameter_Type (Param_Spec)) then
20125 return True;
20126 end if;
20128 Next (Param_Spec);
20129 end loop;
20131 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
20132 if Nkind (Result_Definition (Acc_Subprg)) =
20133 N_Access_Definition
20134 then
20135 return Mentions_T (Result_Definition (Acc_Subprg));
20136 else
20137 return Designates_T (Result_Definition (Acc_Subprg));
20138 end if;
20139 end if;
20141 return False;
20142 end Mentions_T;
20144 -- Start of processing for Check_Anonymous_Access_Components
20146 begin
20147 if No (Comp_List) then
20148 return;
20149 end if;
20151 Comp := First (Component_Items (Comp_List));
20152 while Present (Comp) loop
20153 if Nkind (Comp) = N_Component_Declaration
20154 and then Present
20155 (Access_Definition (Component_Definition (Comp)))
20156 and then
20157 Mentions_T (Access_Definition (Component_Definition (Comp)))
20158 then
20159 Comp_Def := Component_Definition (Comp);
20160 Acc_Def :=
20161 Access_To_Subprogram_Definition
20162 (Access_Definition (Comp_Def));
20164 Build_Incomplete_Type_Declaration;
20165 Anon_Access := Make_Temporary (Loc, 'S');
20167 -- Create a declaration for the anonymous access type: either
20168 -- an access_to_object or an access_to_subprogram.
20170 if Present (Acc_Def) then
20171 if Nkind (Acc_Def) = N_Access_Function_Definition then
20172 Type_Def :=
20173 Make_Access_Function_Definition (Loc,
20174 Parameter_Specifications =>
20175 Parameter_Specifications (Acc_Def),
20176 Result_Definition => Result_Definition (Acc_Def));
20177 else
20178 Type_Def :=
20179 Make_Access_Procedure_Definition (Loc,
20180 Parameter_Specifications =>
20181 Parameter_Specifications (Acc_Def));
20182 end if;
20184 else
20185 Type_Def :=
20186 Make_Access_To_Object_Definition (Loc,
20187 Subtype_Indication =>
20188 Relocate_Node
20189 (Subtype_Mark
20190 (Access_Definition (Comp_Def))));
20192 Set_Constant_Present
20193 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
20194 Set_All_Present
20195 (Type_Def, All_Present (Access_Definition (Comp_Def)));
20196 end if;
20198 Set_Null_Exclusion_Present
20199 (Type_Def,
20200 Null_Exclusion_Present (Access_Definition (Comp_Def)));
20202 Decl :=
20203 Make_Full_Type_Declaration (Loc,
20204 Defining_Identifier => Anon_Access,
20205 Type_Definition => Type_Def);
20207 Insert_Before (Typ_Decl, Decl);
20208 Analyze (Decl);
20210 -- If an access to subprogram, create the extra formals
20212 if Present (Acc_Def) then
20213 Create_Extra_Formals (Designated_Type (Anon_Access));
20215 -- If an access to object, preserve entity of designated type,
20216 -- for ASIS use, before rewriting the component definition.
20218 else
20219 declare
20220 Desig : Entity_Id;
20222 begin
20223 Desig := Entity (Subtype_Indication (Type_Def));
20225 -- If the access definition is to the current record,
20226 -- the visible entity at this point is an incomplete
20227 -- type. Retrieve the full view to simplify ASIS queries
20229 if Ekind (Desig) = E_Incomplete_Type then
20230 Desig := Full_View (Desig);
20231 end if;
20233 Set_Entity
20234 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
20235 end;
20236 end if;
20238 Rewrite (Comp_Def,
20239 Make_Component_Definition (Loc,
20240 Subtype_Indication =>
20241 New_Occurrence_Of (Anon_Access, Loc)));
20243 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
20244 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
20245 else
20246 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
20247 end if;
20249 Set_Is_Local_Anonymous_Access (Anon_Access);
20250 end if;
20252 Next (Comp);
20253 end loop;
20255 if Present (Variant_Part (Comp_List)) then
20256 declare
20257 V : Node_Id;
20258 begin
20259 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
20260 while Present (V) loop
20261 Check_Anonymous_Access_Components
20262 (Typ_Decl, Typ, Prev, Component_List (V));
20263 Next_Non_Pragma (V);
20264 end loop;
20265 end;
20266 end if;
20267 end Check_Anonymous_Access_Components;
20269 ----------------------------------
20270 -- Preanalyze_Assert_Expression --
20271 ----------------------------------
20273 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20274 begin
20275 In_Assertion_Expr := In_Assertion_Expr + 1;
20276 Preanalyze_Spec_Expression (N, T);
20277 In_Assertion_Expr := In_Assertion_Expr - 1;
20278 end Preanalyze_Assert_Expression;
20280 -----------------------------------
20281 -- Preanalyze_Default_Expression --
20282 -----------------------------------
20284 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20285 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20286 begin
20287 In_Default_Expr := True;
20288 Preanalyze_Spec_Expression (N, T);
20289 In_Default_Expr := Save_In_Default_Expr;
20290 end Preanalyze_Default_Expression;
20292 --------------------------------
20293 -- Preanalyze_Spec_Expression --
20294 --------------------------------
20296 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20297 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20298 begin
20299 In_Spec_Expression := True;
20300 Preanalyze_And_Resolve (N, T);
20301 In_Spec_Expression := Save_In_Spec_Expression;
20302 end Preanalyze_Spec_Expression;
20304 -----------------------------
20305 -- Record_Type_Declaration --
20306 -----------------------------
20308 procedure Record_Type_Declaration
20309 (T : Entity_Id;
20310 N : Node_Id;
20311 Prev : Entity_Id)
20313 Def : constant Node_Id := Type_Definition (N);
20314 Is_Tagged : Boolean;
20315 Tag_Comp : Entity_Id;
20317 begin
20318 -- These flags must be initialized before calling Process_Discriminants
20319 -- because this routine makes use of them.
20321 Set_Ekind (T, E_Record_Type);
20322 Set_Etype (T, T);
20323 Init_Size_Align (T);
20324 Set_Interfaces (T, No_Elist);
20325 Set_Stored_Constraint (T, No_Elist);
20326 Set_Default_SSO (T);
20328 -- Normal case
20330 if Ada_Version < Ada_2005
20331 or else not Interface_Present (Def)
20332 then
20333 if Limited_Present (Def) then
20334 Check_SPARK_05_Restriction ("limited is not allowed", N);
20335 end if;
20337 if Abstract_Present (Def) then
20338 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20339 end if;
20341 -- The flag Is_Tagged_Type might have already been set by
20342 -- Find_Type_Name if it detected an error for declaration T. This
20343 -- arises in the case of private tagged types where the full view
20344 -- omits the word tagged.
20346 Is_Tagged :=
20347 Tagged_Present (Def)
20348 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20350 Set_Is_Tagged_Type (T, Is_Tagged);
20351 Set_Is_Limited_Record (T, Limited_Present (Def));
20353 -- Type is abstract if full declaration carries keyword, or if
20354 -- previous partial view did.
20356 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20357 or else Abstract_Present (Def));
20359 else
20360 Check_SPARK_05_Restriction ("interface is not allowed", N);
20362 Is_Tagged := True;
20363 Analyze_Interface_Declaration (T, Def);
20365 if Present (Discriminant_Specifications (N)) then
20366 Error_Msg_N
20367 ("interface types cannot have discriminants",
20368 Defining_Identifier
20369 (First (Discriminant_Specifications (N))));
20370 end if;
20371 end if;
20373 -- First pass: if there are self-referential access components,
20374 -- create the required anonymous access type declarations, and if
20375 -- need be an incomplete type declaration for T itself.
20377 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
20379 if Ada_Version >= Ada_2005
20380 and then Present (Interface_List (Def))
20381 then
20382 Check_Interfaces (N, Def);
20384 declare
20385 Ifaces_List : Elist_Id;
20387 begin
20388 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
20389 -- already in the parents.
20391 Collect_Interfaces
20392 (T => T,
20393 Ifaces_List => Ifaces_List,
20394 Exclude_Parents => True);
20396 Set_Interfaces (T, Ifaces_List);
20397 end;
20398 end if;
20400 -- Records constitute a scope for the component declarations within.
20401 -- The scope is created prior to the processing of these declarations.
20402 -- Discriminants are processed first, so that they are visible when
20403 -- processing the other components. The Ekind of the record type itself
20404 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
20406 -- Enter record scope
20408 Push_Scope (T);
20410 -- If an incomplete or private type declaration was already given for
20411 -- the type, then this scope already exists, and the discriminants have
20412 -- been declared within. We must verify that the full declaration
20413 -- matches the incomplete one.
20415 Check_Or_Process_Discriminants (N, T, Prev);
20417 Set_Is_Constrained (T, not Has_Discriminants (T));
20418 Set_Has_Delayed_Freeze (T, True);
20420 -- For tagged types add a manually analyzed component corresponding
20421 -- to the component _tag, the corresponding piece of tree will be
20422 -- expanded as part of the freezing actions if it is not a CPP_Class.
20424 if Is_Tagged then
20426 -- Do not add the tag unless we are in expansion mode
20428 if Expander_Active then
20429 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
20430 Enter_Name (Tag_Comp);
20432 Set_Ekind (Tag_Comp, E_Component);
20433 Set_Is_Tag (Tag_Comp);
20434 Set_Is_Aliased (Tag_Comp);
20435 Set_Etype (Tag_Comp, RTE (RE_Tag));
20436 Set_DT_Entry_Count (Tag_Comp, No_Uint);
20437 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
20438 Init_Component_Location (Tag_Comp);
20440 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
20441 -- implemented interfaces.
20443 if Has_Interfaces (T) then
20444 Add_Interface_Tag_Components (N, T);
20445 end if;
20446 end if;
20448 Make_Class_Wide_Type (T);
20449 Set_Direct_Primitive_Operations (T, New_Elmt_List);
20450 end if;
20452 -- We must suppress range checks when processing record components in
20453 -- the presence of discriminants, since we don't want spurious checks to
20454 -- be generated during their analysis, but Suppress_Range_Checks flags
20455 -- must be reset the after processing the record definition.
20457 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
20458 -- couldn't we just use the normal range check suppression method here.
20459 -- That would seem cleaner ???
20461 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
20462 Set_Kill_Range_Checks (T, True);
20463 Record_Type_Definition (Def, Prev);
20464 Set_Kill_Range_Checks (T, False);
20465 else
20466 Record_Type_Definition (Def, Prev);
20467 end if;
20469 -- Exit from record scope
20471 End_Scope;
20473 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
20474 -- the implemented interfaces and associate them an aliased entity.
20476 if Is_Tagged
20477 and then not Is_Empty_List (Interface_List (Def))
20478 then
20479 Derive_Progenitor_Subprograms (T, T);
20480 end if;
20482 Check_Function_Writable_Actuals (N);
20483 end Record_Type_Declaration;
20485 ----------------------------
20486 -- Record_Type_Definition --
20487 ----------------------------
20489 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
20490 Component : Entity_Id;
20491 Ctrl_Components : Boolean := False;
20492 Final_Storage_Only : Boolean;
20493 T : Entity_Id;
20495 begin
20496 if Ekind (Prev_T) = E_Incomplete_Type then
20497 T := Full_View (Prev_T);
20498 else
20499 T := Prev_T;
20500 end if;
20502 -- In SPARK, tagged types and type extensions may only be declared in
20503 -- the specification of library unit packages.
20505 if Present (Def) and then Is_Tagged_Type (T) then
20506 declare
20507 Typ : Node_Id;
20508 Ctxt : Node_Id;
20510 begin
20511 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
20512 Typ := Parent (Def);
20513 else
20514 pragma Assert
20515 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
20516 Typ := Parent (Parent (Def));
20517 end if;
20519 Ctxt := Parent (Typ);
20521 if Nkind (Ctxt) = N_Package_Body
20522 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
20523 then
20524 Check_SPARK_05_Restriction
20525 ("type should be defined in package specification", Typ);
20527 elsif Nkind (Ctxt) /= N_Package_Specification
20528 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
20529 then
20530 Check_SPARK_05_Restriction
20531 ("type should be defined in library unit package", Typ);
20532 end if;
20533 end;
20534 end if;
20536 Final_Storage_Only := not Is_Controlled (T);
20538 -- Ada 2005: Check whether an explicit Limited is present in a derived
20539 -- type declaration.
20541 if Nkind (Parent (Def)) = N_Derived_Type_Definition
20542 and then Limited_Present (Parent (Def))
20543 then
20544 Set_Is_Limited_Record (T);
20545 end if;
20547 -- If the component list of a record type is defined by the reserved
20548 -- word null and there is no discriminant part, then the record type has
20549 -- no components and all records of the type are null records (RM 3.7)
20550 -- This procedure is also called to process the extension part of a
20551 -- record extension, in which case the current scope may have inherited
20552 -- components.
20554 if No (Def)
20555 or else No (Component_List (Def))
20556 or else Null_Present (Component_List (Def))
20557 then
20558 if not Is_Tagged_Type (T) then
20559 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
20560 end if;
20562 else
20563 Analyze_Declarations (Component_Items (Component_List (Def)));
20565 if Present (Variant_Part (Component_List (Def))) then
20566 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
20567 Analyze (Variant_Part (Component_List (Def)));
20568 end if;
20569 end if;
20571 -- After completing the semantic analysis of the record definition,
20572 -- record components, both new and inherited, are accessible. Set their
20573 -- kind accordingly. Exclude malformed itypes from illegal declarations,
20574 -- whose Ekind may be void.
20576 Component := First_Entity (Current_Scope);
20577 while Present (Component) loop
20578 if Ekind (Component) = E_Void
20579 and then not Is_Itype (Component)
20580 then
20581 Set_Ekind (Component, E_Component);
20582 Init_Component_Location (Component);
20583 end if;
20585 if Has_Task (Etype (Component)) then
20586 Set_Has_Task (T);
20587 end if;
20589 if Has_Protected (Etype (Component)) then
20590 Set_Has_Protected (T);
20591 end if;
20593 if Ekind (Component) /= E_Component then
20594 null;
20596 -- Do not set Has_Controlled_Component on a class-wide equivalent
20597 -- type. See Make_CW_Equivalent_Type.
20599 elsif not Is_Class_Wide_Equivalent_Type (T)
20600 and then (Has_Controlled_Component (Etype (Component))
20601 or else (Chars (Component) /= Name_uParent
20602 and then Is_Controlled (Etype (Component))))
20603 then
20604 Set_Has_Controlled_Component (T, True);
20605 Final_Storage_Only :=
20606 Final_Storage_Only
20607 and then Finalize_Storage_Only (Etype (Component));
20608 Ctrl_Components := True;
20609 end if;
20611 Next_Entity (Component);
20612 end loop;
20614 -- A Type is Finalize_Storage_Only only if all its controlled components
20615 -- are also.
20617 if Ctrl_Components then
20618 Set_Finalize_Storage_Only (T, Final_Storage_Only);
20619 end if;
20621 -- Place reference to end record on the proper entity, which may
20622 -- be a partial view.
20624 if Present (Def) then
20625 Process_End_Label (Def, 'e', Prev_T);
20626 end if;
20627 end Record_Type_Definition;
20629 ------------------------
20630 -- Replace_Components --
20631 ------------------------
20633 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
20634 function Process (N : Node_Id) return Traverse_Result;
20636 -------------
20637 -- Process --
20638 -------------
20640 function Process (N : Node_Id) return Traverse_Result is
20641 Comp : Entity_Id;
20643 begin
20644 if Nkind (N) = N_Discriminant_Specification then
20645 Comp := First_Discriminant (Typ);
20646 while Present (Comp) loop
20647 if Chars (Comp) = Chars (Defining_Identifier (N)) then
20648 Set_Defining_Identifier (N, Comp);
20649 exit;
20650 end if;
20652 Next_Discriminant (Comp);
20653 end loop;
20655 elsif Nkind (N) = N_Component_Declaration then
20656 Comp := First_Component (Typ);
20657 while Present (Comp) loop
20658 if Chars (Comp) = Chars (Defining_Identifier (N)) then
20659 Set_Defining_Identifier (N, Comp);
20660 exit;
20661 end if;
20663 Next_Component (Comp);
20664 end loop;
20665 end if;
20667 return OK;
20668 end Process;
20670 procedure Replace is new Traverse_Proc (Process);
20672 -- Start of processing for Replace_Components
20674 begin
20675 Replace (Decl);
20676 end Replace_Components;
20678 -------------------------------
20679 -- Set_Completion_Referenced --
20680 -------------------------------
20682 procedure Set_Completion_Referenced (E : Entity_Id) is
20683 begin
20684 -- If in main unit, mark entity that is a completion as referenced,
20685 -- warnings go on the partial view when needed.
20687 if In_Extended_Main_Source_Unit (E) then
20688 Set_Referenced (E);
20689 end if;
20690 end Set_Completion_Referenced;
20692 ---------------------
20693 -- Set_Default_SSO --
20694 ---------------------
20696 procedure Set_Default_SSO (T : Entity_Id) is
20697 begin
20698 case Opt.Default_SSO is
20699 when ' ' =>
20700 null;
20701 when 'L' =>
20702 Set_SSO_Set_Low_By_Default (T, True);
20703 when 'H' =>
20704 Set_SSO_Set_High_By_Default (T, True);
20705 when others =>
20706 raise Program_Error;
20707 end case;
20708 end Set_Default_SSO;
20710 ---------------------
20711 -- Set_Fixed_Range --
20712 ---------------------
20714 -- The range for fixed-point types is complicated by the fact that we
20715 -- do not know the exact end points at the time of the declaration. This
20716 -- is true for three reasons:
20718 -- A size clause may affect the fudging of the end-points.
20719 -- A small clause may affect the values of the end-points.
20720 -- We try to include the end-points if it does not affect the size.
20722 -- This means that the actual end-points must be established at the
20723 -- point when the type is frozen. Meanwhile, we first narrow the range
20724 -- as permitted (so that it will fit if necessary in a small specified
20725 -- size), and then build a range subtree with these narrowed bounds.
20726 -- Set_Fixed_Range constructs the range from real literal values, and
20727 -- sets the range as the Scalar_Range of the given fixed-point type entity.
20729 -- The parent of this range is set to point to the entity so that it is
20730 -- properly hooked into the tree (unlike normal Scalar_Range entries for
20731 -- other scalar types, which are just pointers to the range in the
20732 -- original tree, this would otherwise be an orphan).
20734 -- The tree is left unanalyzed. When the type is frozen, the processing
20735 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
20736 -- analyzed, and uses this as an indication that it should complete
20737 -- work on the range (it will know the final small and size values).
20739 procedure Set_Fixed_Range
20740 (E : Entity_Id;
20741 Loc : Source_Ptr;
20742 Lo : Ureal;
20743 Hi : Ureal)
20745 S : constant Node_Id :=
20746 Make_Range (Loc,
20747 Low_Bound => Make_Real_Literal (Loc, Lo),
20748 High_Bound => Make_Real_Literal (Loc, Hi));
20749 begin
20750 Set_Scalar_Range (E, S);
20751 Set_Parent (S, E);
20753 -- Before the freeze point, the bounds of a fixed point are universal
20754 -- and carry the corresponding type.
20756 Set_Etype (Low_Bound (S), Universal_Real);
20757 Set_Etype (High_Bound (S), Universal_Real);
20758 end Set_Fixed_Range;
20760 ----------------------------------
20761 -- Set_Scalar_Range_For_Subtype --
20762 ----------------------------------
20764 procedure Set_Scalar_Range_For_Subtype
20765 (Def_Id : Entity_Id;
20766 R : Node_Id;
20767 Subt : Entity_Id)
20769 Kind : constant Entity_Kind := Ekind (Def_Id);
20771 begin
20772 -- Defend against previous error
20774 if Nkind (R) = N_Error then
20775 return;
20776 end if;
20778 Set_Scalar_Range (Def_Id, R);
20780 -- We need to link the range into the tree before resolving it so
20781 -- that types that are referenced, including importantly the subtype
20782 -- itself, are properly frozen (Freeze_Expression requires that the
20783 -- expression be properly linked into the tree). Of course if it is
20784 -- already linked in, then we do not disturb the current link.
20786 if No (Parent (R)) then
20787 Set_Parent (R, Def_Id);
20788 end if;
20790 -- Reset the kind of the subtype during analysis of the range, to
20791 -- catch possible premature use in the bounds themselves.
20793 Set_Ekind (Def_Id, E_Void);
20794 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
20795 Set_Ekind (Def_Id, Kind);
20796 end Set_Scalar_Range_For_Subtype;
20798 --------------------------------------------------------
20799 -- Set_Stored_Constraint_From_Discriminant_Constraint --
20800 --------------------------------------------------------
20802 procedure Set_Stored_Constraint_From_Discriminant_Constraint
20803 (E : Entity_Id)
20805 begin
20806 -- Make sure set if encountered during Expand_To_Stored_Constraint
20808 Set_Stored_Constraint (E, No_Elist);
20810 -- Give it the right value
20812 if Is_Constrained (E) and then Has_Discriminants (E) then
20813 Set_Stored_Constraint (E,
20814 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
20815 end if;
20816 end Set_Stored_Constraint_From_Discriminant_Constraint;
20818 -------------------------------------
20819 -- Signed_Integer_Type_Declaration --
20820 -------------------------------------
20822 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
20823 Implicit_Base : Entity_Id;
20824 Base_Typ : Entity_Id;
20825 Lo_Val : Uint;
20826 Hi_Val : Uint;
20827 Errs : Boolean := False;
20828 Lo : Node_Id;
20829 Hi : Node_Id;
20831 function Can_Derive_From (E : Entity_Id) return Boolean;
20832 -- Determine whether given bounds allow derivation from specified type
20834 procedure Check_Bound (Expr : Node_Id);
20835 -- Check bound to make sure it is integral and static. If not, post
20836 -- appropriate error message and set Errs flag
20838 ---------------------
20839 -- Can_Derive_From --
20840 ---------------------
20842 -- Note we check both bounds against both end values, to deal with
20843 -- strange types like ones with a range of 0 .. -12341234.
20845 function Can_Derive_From (E : Entity_Id) return Boolean is
20846 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
20847 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
20848 begin
20849 return Lo <= Lo_Val and then Lo_Val <= Hi
20850 and then
20851 Lo <= Hi_Val and then Hi_Val <= Hi;
20852 end Can_Derive_From;
20854 -----------------
20855 -- Check_Bound --
20856 -----------------
20858 procedure Check_Bound (Expr : Node_Id) is
20859 begin
20860 -- If a range constraint is used as an integer type definition, each
20861 -- bound of the range must be defined by a static expression of some
20862 -- integer type, but the two bounds need not have the same integer
20863 -- type (Negative bounds are allowed.) (RM 3.5.4)
20865 if not Is_Integer_Type (Etype (Expr)) then
20866 Error_Msg_N
20867 ("integer type definition bounds must be of integer type", Expr);
20868 Errs := True;
20870 elsif not Is_OK_Static_Expression (Expr) then
20871 Flag_Non_Static_Expr
20872 ("non-static expression used for integer type bound!", Expr);
20873 Errs := True;
20875 -- The bounds are folded into literals, and we set their type to be
20876 -- universal, to avoid typing difficulties: we cannot set the type
20877 -- of the literal to the new type, because this would be a forward
20878 -- reference for the back end, and if the original type is user-
20879 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
20881 else
20882 if Is_Entity_Name (Expr) then
20883 Fold_Uint (Expr, Expr_Value (Expr), True);
20884 end if;
20886 Set_Etype (Expr, Universal_Integer);
20887 end if;
20888 end Check_Bound;
20890 -- Start of processing for Signed_Integer_Type_Declaration
20892 begin
20893 -- Create an anonymous base type
20895 Implicit_Base :=
20896 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
20898 -- Analyze and check the bounds, they can be of any integer type
20900 Lo := Low_Bound (Def);
20901 Hi := High_Bound (Def);
20903 -- Arbitrarily use Integer as the type if either bound had an error
20905 if Hi = Error or else Lo = Error then
20906 Base_Typ := Any_Integer;
20907 Set_Error_Posted (T, True);
20909 -- Here both bounds are OK expressions
20911 else
20912 Analyze_And_Resolve (Lo, Any_Integer);
20913 Analyze_And_Resolve (Hi, Any_Integer);
20915 Check_Bound (Lo);
20916 Check_Bound (Hi);
20918 if Errs then
20919 Hi := Type_High_Bound (Standard_Long_Long_Integer);
20920 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
20921 end if;
20923 -- Find type to derive from
20925 Lo_Val := Expr_Value (Lo);
20926 Hi_Val := Expr_Value (Hi);
20928 if Can_Derive_From (Standard_Short_Short_Integer) then
20929 Base_Typ := Base_Type (Standard_Short_Short_Integer);
20931 elsif Can_Derive_From (Standard_Short_Integer) then
20932 Base_Typ := Base_Type (Standard_Short_Integer);
20934 elsif Can_Derive_From (Standard_Integer) then
20935 Base_Typ := Base_Type (Standard_Integer);
20937 elsif Can_Derive_From (Standard_Long_Integer) then
20938 Base_Typ := Base_Type (Standard_Long_Integer);
20940 elsif Can_Derive_From (Standard_Long_Long_Integer) then
20941 Check_Restriction (No_Long_Long_Integers, Def);
20942 Base_Typ := Base_Type (Standard_Long_Long_Integer);
20944 else
20945 Base_Typ := Base_Type (Standard_Long_Long_Integer);
20946 Error_Msg_N ("integer type definition bounds out of range", Def);
20947 Hi := Type_High_Bound (Standard_Long_Long_Integer);
20948 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
20949 end if;
20950 end if;
20952 -- Complete both implicit base and declared first subtype entities
20954 Set_Etype (Implicit_Base, Base_Typ);
20955 Set_Size_Info (Implicit_Base, (Base_Typ));
20956 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
20957 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
20959 Set_Ekind (T, E_Signed_Integer_Subtype);
20960 Set_Etype (T, Implicit_Base);
20962 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
20964 Set_Size_Info (T, (Implicit_Base));
20965 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
20966 Set_Scalar_Range (T, Def);
20967 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
20968 Set_Is_Constrained (T);
20969 end Signed_Integer_Type_Declaration;
20971 end Sem_Ch3;