2016-04-21 Arnaud Charlet <charlet@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob0560a69f564812b33b992d7b1e88c1718b3f90dc
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-2016, 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 Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Fname; use Fname;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Itypes; use Itypes;
45 with Layout; use Layout;
46 with Lib; use Lib;
47 with Lib.Xref; use Lib.Xref;
48 with Namet; use Namet;
49 with Nmake; use Nmake;
50 with Opt; use Opt;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Case; use Sem_Case;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch7; use Sem_Ch7;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
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 Build_Derived_Type
95 (N : Node_Id;
96 Parent_Type : Entity_Id;
97 Derived_Type : Entity_Id;
98 Is_Completion : Boolean;
99 Derive_Subps : Boolean := True);
100 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
101 -- the N_Full_Type_Declaration node containing the derived type definition.
102 -- Parent_Type is the entity for the parent type in the derived type
103 -- definition and Derived_Type the actual derived type. Is_Completion must
104 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
105 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
106 -- completion of a private type declaration. If Is_Completion is set to
107 -- True, N is the completion of a private type declaration and Derived_Type
108 -- is different from the defining identifier inside N (i.e. Derived_Type /=
109 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
110 -- subprograms should be derived. The only case where this parameter is
111 -- False is when Build_Derived_Type is recursively called to process an
112 -- implicit derived full type for a type derived from a private type (in
113 -- that case the subprograms must only be derived for the private view of
114 -- the type).
116 -- ??? These flags need a bit of re-examination and re-documentation:
117 -- ??? are they both necessary (both seem related to the recursion)?
119 procedure Build_Derived_Access_Type
120 (N : Node_Id;
121 Parent_Type : Entity_Id;
122 Derived_Type : Entity_Id);
123 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
124 -- create an implicit base if the parent type is constrained or if the
125 -- subtype indication has a constraint.
127 procedure Build_Derived_Array_Type
128 (N : Node_Id;
129 Parent_Type : Entity_Id;
130 Derived_Type : Entity_Id);
131 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
132 -- create an implicit base if the parent type is constrained or if the
133 -- subtype indication has a constraint.
135 procedure Build_Derived_Concurrent_Type
136 (N : Node_Id;
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
140 -- protected type, inherit entries and protected subprograms, check
141 -- legality of discriminant constraints if any.
143 procedure Build_Derived_Enumeration_Type
144 (N : Node_Id;
145 Parent_Type : Entity_Id;
146 Derived_Type : Entity_Id);
147 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
148 -- type, we must create a new list of literals. Types derived from
149 -- Character and [Wide_]Wide_Character are special-cased.
151 procedure Build_Derived_Numeric_Type
152 (N : Node_Id;
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id);
155 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
156 -- an anonymous base type, and propagate constraint to subtype if needed.
158 procedure Build_Derived_Private_Type
159 (N : Node_Id;
160 Parent_Type : Entity_Id;
161 Derived_Type : Entity_Id;
162 Is_Completion : Boolean;
163 Derive_Subps : Boolean := True);
164 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
165 -- because the parent may or may not have a completion, and the derivation
166 -- may itself be a completion.
168 procedure Build_Derived_Record_Type
169 (N : Node_Id;
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Derive_Subps : Boolean := True);
173 -- Subsidiary procedure used for tagged and untagged record types
174 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
175 -- All parameters are as in Build_Derived_Type except that N, in
176 -- addition to being an N_Full_Type_Declaration node, can also be an
177 -- N_Private_Extension_Declaration node. See the definition of this routine
178 -- for much more info. Derive_Subps indicates whether subprograms should be
179 -- derived from the parent type. The only case where Derive_Subps is False
180 -- is for an implicit derived full type for a type derived from a private
181 -- type (see Build_Derived_Type).
183 procedure Build_Discriminal (Discrim : Entity_Id);
184 -- Create the discriminal corresponding to discriminant Discrim, that is
185 -- the parameter corresponding to Discrim to be used in initialization
186 -- procedures for the type where Discrim is a discriminant. Discriminals
187 -- are not used during semantic analysis, and are not fully defined
188 -- entities until expansion. Thus they are not given a scope until
189 -- initialization procedures are built.
191 function Build_Discriminant_Constraints
192 (T : Entity_Id;
193 Def : Node_Id;
194 Derived_Def : Boolean := False) return Elist_Id;
195 -- Validate discriminant constraints and return the list of the constraints
196 -- in order of discriminant declarations, where T is the discriminated
197 -- unconstrained type. Def is the N_Subtype_Indication node where the
198 -- discriminants constraints for T are specified. Derived_Def is True
199 -- when building the discriminant constraints in a derived type definition
200 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
201 -- type and Def is the constraint "(xxx)" on T and this routine sets the
202 -- Corresponding_Discriminant field of the discriminants in the derived
203 -- type D to point to the corresponding discriminants in the parent type T.
205 procedure Build_Discriminated_Subtype
206 (T : Entity_Id;
207 Def_Id : Entity_Id;
208 Elist : Elist_Id;
209 Related_Nod : Node_Id;
210 For_Access : Boolean := False);
211 -- Subsidiary procedure to Constrain_Discriminated_Type and to
212 -- Process_Incomplete_Dependents. Given
214 -- T (a possibly discriminated base type)
215 -- Def_Id (a very partially built subtype for T),
217 -- the call completes Def_Id to be the appropriate E_*_Subtype.
219 -- The Elist is the list of discriminant constraints if any (it is set
220 -- to No_Elist if T is not a discriminated type, and to an empty list if
221 -- T has discriminants but there are no discriminant constraints). The
222 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
223 -- The For_Access says whether or not this subtype is really constraining
224 -- an access type. That is its sole purpose is the designated type of an
225 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
226 -- is built to avoid freezing T when the access subtype is frozen.
228 function Build_Scalar_Bound
229 (Bound : Node_Id;
230 Par_T : Entity_Id;
231 Der_T : Entity_Id) return Node_Id;
232 -- The bounds of a derived scalar type are conversions of the bounds of
233 -- the parent type. Optimize the representation if the bounds are literals.
234 -- Needs a more complete spec--what are the parameters exactly, and what
235 -- exactly is the returned value, and how is Bound affected???
237 procedure Build_Underlying_Full_View
238 (N : Node_Id;
239 Typ : Entity_Id;
240 Par : Entity_Id);
241 -- If the completion of a private type is itself derived from a private
242 -- type, or if the full view of a private subtype is itself private, the
243 -- back-end has no way to compute the actual size of this type. We build
244 -- an internal subtype declaration of the proper parent type to convey
245 -- this information. This extra mechanism is needed because a full
246 -- view cannot itself have a full view (it would get clobbered during
247 -- view exchanges).
249 procedure Check_Access_Discriminant_Requires_Limited
250 (D : Node_Id;
251 Loc : Node_Id);
252 -- Check the restriction that the type to which an access discriminant
253 -- belongs must be a concurrent type or a descendant of a type with
254 -- the reserved word 'limited' in its declaration.
256 procedure Check_Anonymous_Access_Components
257 (Typ_Decl : Node_Id;
258 Typ : Entity_Id;
259 Prev : Entity_Id;
260 Comp_List : Node_Id);
261 -- Ada 2005 AI-382: an access component in a record definition can refer to
262 -- the enclosing record, in which case it denotes the type itself, and not
263 -- the current instance of the type. We create an anonymous access type for
264 -- the component, and flag it as an access to a component, so accessibility
265 -- checks are properly performed on it. The declaration of the access type
266 -- is placed ahead of that of the record to prevent order-of-elaboration
267 -- circularity issues in Gigi. We create an incomplete type for the record
268 -- declaration, which is the designated type of the anonymous access.
270 procedure Check_Delta_Expression (E : Node_Id);
271 -- Check that the expression represented by E is suitable for use as a
272 -- delta expression, i.e. it is of real type and is static.
274 procedure Check_Digits_Expression (E : Node_Id);
275 -- Check that the expression represented by E is suitable for use as a
276 -- digits expression, i.e. it is of integer type, positive and static.
278 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
279 -- Validate the initialization of an object declaration. T is the required
280 -- type, and Exp is the initialization expression.
282 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
283 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
285 procedure Check_Or_Process_Discriminants
286 (N : Node_Id;
287 T : Entity_Id;
288 Prev : Entity_Id := Empty);
289 -- If N is the full declaration of the completion T of an incomplete or
290 -- private type, check its discriminants (which are already known to be
291 -- conformant with those of the partial view, see Find_Type_Name),
292 -- otherwise process them. Prev is the entity of the partial declaration,
293 -- if any.
295 procedure Check_Real_Bound (Bound : Node_Id);
296 -- Check given bound for being of real type and static. If not, post an
297 -- appropriate message, and rewrite the bound with the real literal zero.
299 procedure Constant_Redeclaration
300 (Id : Entity_Id;
301 N : Node_Id;
302 T : out Entity_Id);
303 -- Various checks on legality of full declaration of deferred constant.
304 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
305 -- node. The caller has not yet set any attributes of this entity.
307 function Contain_Interface
308 (Iface : Entity_Id;
309 Ifaces : Elist_Id) return Boolean;
310 -- Ada 2005: Determine whether Iface is present in the list Ifaces
312 procedure Convert_Scalar_Bounds
313 (N : Node_Id;
314 Parent_Type : Entity_Id;
315 Derived_Type : Entity_Id;
316 Loc : Source_Ptr);
317 -- For derived scalar types, convert the bounds in the type definition to
318 -- the derived type, and complete their analysis. Given a constraint of the
319 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
320 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
321 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
322 -- subtype are conversions of those bounds to the derived_type, so that
323 -- their typing is consistent.
325 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
326 -- Copies attributes from array base type T2 to array base type T1. Copies
327 -- only attributes that apply to base types, but not subtypes.
329 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
330 -- Copies attributes from array subtype T2 to array subtype T1. Copies
331 -- attributes that apply to both subtypes and base types.
333 procedure Create_Constrained_Components
334 (Subt : Entity_Id;
335 Decl_Node : Node_Id;
336 Typ : Entity_Id;
337 Constraints : Elist_Id);
338 -- Build the list of entities for a constrained discriminated record
339 -- subtype. If a component depends on a discriminant, replace its subtype
340 -- using the discriminant values in the discriminant constraint. Subt
341 -- is the defining identifier for the subtype whose list of constrained
342 -- entities we will create. Decl_Node is the type declaration node where
343 -- we will attach all the itypes created. Typ is the base discriminated
344 -- type for the subtype Subt. Constraints is the list of discriminant
345 -- constraints for Typ.
347 function Constrain_Component_Type
348 (Comp : Entity_Id;
349 Constrained_Typ : Entity_Id;
350 Related_Node : Node_Id;
351 Typ : Entity_Id;
352 Constraints : Elist_Id) return Entity_Id;
353 -- Given a discriminated base type Typ, a list of discriminant constraints,
354 -- Constraints, for Typ and a component Comp of Typ, create and return the
355 -- type corresponding to Etype (Comp) where all discriminant references
356 -- are replaced with the corresponding constraint. If Etype (Comp) contains
357 -- no discriminant references then it is returned as-is. Constrained_Typ
358 -- is the final constrained subtype to which the constrained component
359 -- belongs. Related_Node is the node where we attach all created itypes.
361 procedure Constrain_Access
362 (Def_Id : in out Entity_Id;
363 S : Node_Id;
364 Related_Nod : Node_Id);
365 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
366 -- an anonymous type created for a subtype indication. In that case it is
367 -- created in the procedure and attached to Related_Nod.
369 procedure Constrain_Array
370 (Def_Id : in out Entity_Id;
371 SI : Node_Id;
372 Related_Nod : Node_Id;
373 Related_Id : Entity_Id;
374 Suffix : Character);
375 -- Apply a list of index constraints to an unconstrained array type. The
376 -- first parameter is the entity for the resulting subtype. A value of
377 -- Empty for Def_Id indicates that an implicit type must be created, but
378 -- creation is delayed (and must be done by this procedure) because other
379 -- subsidiary implicit types must be created first (which is why Def_Id
380 -- is an in/out parameter). The second parameter is a subtype indication
381 -- node for the constrained array to be created (e.g. something of the
382 -- form string (1 .. 10)). Related_Nod gives the place where this type
383 -- has to be inserted in the tree. The Related_Id and Suffix parameters
384 -- are used to build the associated Implicit type name.
386 procedure Constrain_Concurrent
387 (Def_Id : in out Entity_Id;
388 SI : Node_Id;
389 Related_Nod : Node_Id;
390 Related_Id : Entity_Id;
391 Suffix : Character);
392 -- Apply list of discriminant constraints to an unconstrained concurrent
393 -- type.
395 -- SI is the N_Subtype_Indication node containing the constraint and
396 -- the unconstrained type to constrain.
398 -- Def_Id is the entity for the resulting constrained subtype. A value
399 -- of Empty for Def_Id indicates that an implicit type must be created,
400 -- but creation is delayed (and must be done by this procedure) because
401 -- other subsidiary implicit types must be created first (which is why
402 -- Def_Id is an in/out parameter).
404 -- Related_Nod gives the place where this type has to be inserted
405 -- in the tree.
407 -- The last two arguments are used to create its external name if needed.
409 function Constrain_Corresponding_Record
410 (Prot_Subt : Entity_Id;
411 Corr_Rec : Entity_Id;
412 Related_Nod : Node_Id) return Entity_Id;
413 -- When constraining a protected type or task type with discriminants,
414 -- constrain the corresponding record with the same discriminant values.
416 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
417 -- Constrain a decimal fixed point type with a digits constraint and/or a
418 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
420 procedure Constrain_Discriminated_Type
421 (Def_Id : Entity_Id;
422 S : Node_Id;
423 Related_Nod : Node_Id;
424 For_Access : Boolean := False);
425 -- Process discriminant constraints of composite type. Verify that values
426 -- have been provided for all discriminants, that the original type is
427 -- unconstrained, and that the types of the supplied expressions match
428 -- the discriminant types. The first three parameters are like in routine
429 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
430 -- of For_Access.
432 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
433 -- Constrain an enumeration type with a range constraint. This is identical
434 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
436 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
437 -- Constrain a floating point type with either a digits constraint
438 -- and/or a range constraint, building a E_Floating_Point_Subtype.
440 procedure Constrain_Index
441 (Index : Node_Id;
442 S : Node_Id;
443 Related_Nod : Node_Id;
444 Related_Id : Entity_Id;
445 Suffix : Character;
446 Suffix_Index : Nat);
447 -- Process an index constraint S in a constrained array declaration. The
448 -- constraint can be a subtype name, or a range with or without an explicit
449 -- subtype mark. The index is the corresponding index of the unconstrained
450 -- array. The Related_Id and Suffix parameters are used to build the
451 -- associated Implicit type name.
453 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
454 -- Build subtype of a signed or modular integer type
456 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
457 -- Constrain an ordinary fixed point type with a range constraint, and
458 -- build an E_Ordinary_Fixed_Point_Subtype entity.
460 procedure Copy_And_Swap (Priv, Full : Entity_Id);
461 -- Copy the Priv entity into the entity of its full declaration then swap
462 -- the two entities in such a manner that the former private type is now
463 -- seen as a full type.
465 procedure Decimal_Fixed_Point_Type_Declaration
466 (T : Entity_Id;
467 Def : Node_Id);
468 -- Create a new decimal fixed point type, and apply the constraint to
469 -- obtain a subtype of this new type.
471 procedure Complete_Private_Subtype
472 (Priv : Entity_Id;
473 Full : Entity_Id;
474 Full_Base : Entity_Id;
475 Related_Nod : Node_Id);
476 -- Complete the implicit full view of a private subtype by setting the
477 -- appropriate semantic fields. If the full view of the parent is a record
478 -- type, build constrained components of subtype.
480 procedure Derive_Progenitor_Subprograms
481 (Parent_Type : Entity_Id;
482 Tagged_Type : Entity_Id);
483 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
484 -- operations of progenitors of Tagged_Type, and replace the subsidiary
485 -- subtypes with Tagged_Type, to build the specs of the inherited interface
486 -- primitives. The derived primitives are aliased to those of the
487 -- interface. This routine takes care also of transferring to the full view
488 -- subprograms associated with the partial view of Tagged_Type that cover
489 -- interface primitives.
491 procedure Derived_Standard_Character
492 (N : Node_Id;
493 Parent_Type : Entity_Id;
494 Derived_Type : Entity_Id);
495 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
496 -- derivations from types Standard.Character and Standard.Wide_Character.
498 procedure Derived_Type_Declaration
499 (T : Entity_Id;
500 N : Node_Id;
501 Is_Completion : Boolean);
502 -- Process a derived type declaration. Build_Derived_Type is invoked
503 -- to process the actual derived type definition. Parameters N and
504 -- Is_Completion have the same meaning as in Build_Derived_Type.
505 -- T is the N_Defining_Identifier for the entity defined in the
506 -- N_Full_Type_Declaration node N, that is T is the derived type.
508 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
509 -- Insert each literal in symbol table, as an overloadable identifier. Each
510 -- enumeration type is mapped into a sequence of integers, and each literal
511 -- is defined as a constant with integer value. If any of the literals are
512 -- character literals, the type is a character type, which means that
513 -- strings are legal aggregates for arrays of components of the type.
515 function Expand_To_Stored_Constraint
516 (Typ : Entity_Id;
517 Constraint : Elist_Id) return Elist_Id;
518 -- Given a constraint (i.e. a list of expressions) on the discriminants of
519 -- Typ, expand it into a constraint on the stored discriminants and return
520 -- the new list of expressions constraining the stored discriminants.
522 function Find_Type_Of_Object
523 (Obj_Def : Node_Id;
524 Related_Nod : Node_Id) return Entity_Id;
525 -- Get type entity for object referenced by Obj_Def, attaching the implicit
526 -- types generated to Related_Nod.
528 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
529 -- Create a new float and apply the constraint to obtain subtype of it
531 function Has_Range_Constraint (N : Node_Id) return Boolean;
532 -- Given an N_Subtype_Indication node N, return True if a range constraint
533 -- is present, either directly, or as part of a digits or delta constraint.
534 -- In addition, a digits constraint in the decimal case returns True, since
535 -- it establishes a default range if no explicit range is present.
537 function Inherit_Components
538 (N : Node_Id;
539 Parent_Base : Entity_Id;
540 Derived_Base : Entity_Id;
541 Is_Tagged : Boolean;
542 Inherit_Discr : Boolean;
543 Discs : Elist_Id) return Elist_Id;
544 -- Called from Build_Derived_Record_Type to inherit the components of
545 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
546 -- For more information on derived types and component inheritance please
547 -- consult the comment above the body of Build_Derived_Record_Type.
549 -- N is the original derived type declaration
551 -- Is_Tagged is set if we are dealing with tagged types
553 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
554 -- Parent_Base, otherwise no discriminants are inherited.
556 -- Discs gives the list of constraints that apply to Parent_Base in the
557 -- derived type declaration. If Discs is set to No_Elist, then we have
558 -- the following situation:
560 -- type Parent (D1..Dn : ..) is [tagged] record ...;
561 -- type Derived is new Parent [with ...];
563 -- which gets treated as
565 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
567 -- For untagged types the returned value is an association list. The list
568 -- starts from the association (Parent_Base => Derived_Base), and then it
569 -- contains a sequence of the associations of the form
571 -- (Old_Component => New_Component),
573 -- where Old_Component is the Entity_Id of a component in Parent_Base and
574 -- New_Component is the Entity_Id of the corresponding component in
575 -- Derived_Base. For untagged records, this association list is needed when
576 -- copying the record declaration for the derived base. In the tagged case
577 -- the value returned is irrelevant.
579 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
580 -- Propagate static and dynamic predicate flags from a parent to the
581 -- subtype in a subtype declaration with and without constraints.
583 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
584 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
585 -- Determine whether subprogram Subp is a procedure subject to pragma
586 -- Extensions_Visible with value False and has at least one controlling
587 -- parameter of mode OUT.
589 function Is_Valid_Constraint_Kind
590 (T_Kind : Type_Kind;
591 Constraint_Kind : Node_Kind) return Boolean;
592 -- Returns True if it is legal to apply the given kind of constraint to the
593 -- given kind of type (index constraint to an array type, for example).
595 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
596 -- Create new modular type. Verify that modulus is in bounds
598 procedure New_Concatenation_Op (Typ : Entity_Id);
599 -- Create an abbreviated declaration for an operator in order to
600 -- materialize concatenation on array types.
602 procedure Ordinary_Fixed_Point_Type_Declaration
603 (T : Entity_Id;
604 Def : Node_Id);
605 -- Create a new ordinary fixed point type, and apply the constraint to
606 -- obtain subtype of it.
608 procedure Prepare_Private_Subtype_Completion
609 (Id : Entity_Id;
610 Related_Nod : Node_Id);
611 -- Id is a subtype of some private type. Creates the full declaration
612 -- associated with Id whenever possible, i.e. when the full declaration
613 -- of the base type is already known. Records each subtype into
614 -- Private_Dependents of the base type.
616 procedure Process_Incomplete_Dependents
617 (N : Node_Id;
618 Full_T : Entity_Id;
619 Inc_T : Entity_Id);
620 -- Process all entities that depend on an incomplete type. There include
621 -- subtypes, subprogram types that mention the incomplete type in their
622 -- profiles, and subprogram with access parameters that designate the
623 -- incomplete type.
625 -- Inc_T is the defining identifier of an incomplete type declaration, its
626 -- Ekind is E_Incomplete_Type.
628 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
630 -- Full_T is N's defining identifier.
632 -- Subtypes of incomplete types with discriminants are completed when the
633 -- parent type is. This is simpler than private subtypes, because they can
634 -- only appear in the same scope, and there is no need to exchange views.
635 -- Similarly, access_to_subprogram types may have a parameter or a return
636 -- type that is an incomplete type, and that must be replaced with the
637 -- full type.
639 -- If the full type is tagged, subprogram with access parameters that
640 -- designated the incomplete may be primitive operations of the full type,
641 -- and have to be processed accordingly.
643 procedure Process_Real_Range_Specification (Def : Node_Id);
644 -- Given the type definition for a real type, this procedure processes and
645 -- checks the real range specification of this type definition if one is
646 -- present. If errors are found, error messages are posted, and the
647 -- Real_Range_Specification of Def is reset to Empty.
649 procedure Propagate_Default_Init_Cond_Attributes
650 (From_Typ : Entity_Id;
651 To_Typ : Entity_Id;
652 Parent_To_Derivation : Boolean := False;
653 Private_To_Full_View : Boolean := False);
654 -- Subsidiary to routines Build_Derived_Type and Process_Full_View. Inherit
655 -- all attributes related to pragma Default_Initial_Condition from From_Typ
656 -- to To_Typ. Flag Parent_To_Derivation should be set when the context is
657 -- the creation of a derived type. Flag Private_To_Full_View should be set
658 -- when processing both views of a private type.
660 procedure Record_Type_Declaration
661 (T : Entity_Id;
662 N : Node_Id;
663 Prev : Entity_Id);
664 -- Process a record type declaration (for both untagged and tagged
665 -- records). Parameters T and N are exactly like in procedure
666 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
667 -- for this routine. If this is the completion of an incomplete type
668 -- declaration, Prev is the entity of the incomplete declaration, used for
669 -- cross-referencing. Otherwise Prev = T.
671 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
672 -- This routine is used to process the actual record type definition (both
673 -- for untagged and tagged records). Def is a record type definition node.
674 -- This procedure analyzes the components in this record type definition.
675 -- Prev_T is the entity for the enclosing record type. It is provided so
676 -- that its Has_Task flag can be set if any of the component have Has_Task
677 -- set. If the declaration is the completion of an incomplete type
678 -- declaration, Prev_T is the original incomplete type, whose full view is
679 -- the record type.
681 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
682 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
683 -- build a copy of the declaration tree of the parent, and we create
684 -- independently the list of components for the derived type. Semantic
685 -- information uses the component entities, but record representation
686 -- clauses are validated on the declaration tree. This procedure replaces
687 -- discriminants and components in the declaration with those that have
688 -- been created by Inherit_Components.
690 procedure Set_Fixed_Range
691 (E : Entity_Id;
692 Loc : Source_Ptr;
693 Lo : Ureal;
694 Hi : Ureal);
695 -- Build a range node with the given bounds and set it as the Scalar_Range
696 -- of the given fixed-point type entity. Loc is the source location used
697 -- for the constructed range. See body for further details.
699 procedure Set_Scalar_Range_For_Subtype
700 (Def_Id : Entity_Id;
701 R : Node_Id;
702 Subt : Entity_Id);
703 -- This routine is used to set the scalar range field for a subtype given
704 -- Def_Id, the entity for the subtype, and R, the range expression for the
705 -- scalar range. Subt provides the parent subtype to be used to analyze,
706 -- resolve, and check the given range.
708 procedure Set_Default_SSO (T : Entity_Id);
709 -- T is the entity for an array or record being declared. This procedure
710 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
711 -- to the setting of Opt.Default_SSO.
713 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
714 -- Create a new signed integer entity, and apply the constraint to obtain
715 -- the required first named subtype of this type.
717 procedure Set_Stored_Constraint_From_Discriminant_Constraint
718 (E : Entity_Id);
719 -- E is some record type. This routine computes E's Stored_Constraint
720 -- from its Discriminant_Constraint.
722 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
723 -- Check that an entity in a list of progenitors is an interface,
724 -- emit error otherwise.
726 -----------------------
727 -- Access_Definition --
728 -----------------------
730 function Access_Definition
731 (Related_Nod : Node_Id;
732 N : Node_Id) return Entity_Id
734 Anon_Type : Entity_Id;
735 Anon_Scope : Entity_Id;
736 Desig_Type : Entity_Id;
737 Enclosing_Prot_Type : Entity_Id := Empty;
739 begin
740 Check_SPARK_05_Restriction ("access type is not allowed", N);
742 if Is_Entry (Current_Scope)
743 and then Is_Task_Type (Etype (Scope (Current_Scope)))
744 then
745 Error_Msg_N ("task entries cannot have access parameters", N);
746 return Empty;
747 end if;
749 -- Ada 2005: For an object declaration the corresponding anonymous
750 -- type is declared in the current scope.
752 -- If the access definition is the return type of another access to
753 -- function, scope is the current one, because it is the one of the
754 -- current type declaration, except for the pathological case below.
756 if Nkind_In (Related_Nod, N_Object_Declaration,
757 N_Access_Function_Definition)
758 then
759 Anon_Scope := Current_Scope;
761 -- A pathological case: function returning access functions that
762 -- return access functions, etc. Each anonymous access type created
763 -- is in the enclosing scope of the outermost function.
765 declare
766 Par : Node_Id;
768 begin
769 Par := Related_Nod;
770 while Nkind_In (Par, N_Access_Function_Definition,
771 N_Access_Definition)
772 loop
773 Par := Parent (Par);
774 end loop;
776 if Nkind (Par) = N_Function_Specification then
777 Anon_Scope := Scope (Defining_Entity (Par));
778 end if;
779 end;
781 -- For the anonymous function result case, retrieve the scope of the
782 -- function specification's associated entity rather than using the
783 -- current scope. The current scope will be the function itself if the
784 -- formal part is currently being analyzed, but will be the parent scope
785 -- in the case of a parameterless function, and we always want to use
786 -- the function's parent scope. Finally, if the function is a child
787 -- unit, we must traverse the tree to retrieve the proper entity.
789 elsif Nkind (Related_Nod) = N_Function_Specification
790 and then Nkind (Parent (N)) /= N_Parameter_Specification
791 then
792 -- If the current scope is a protected type, the anonymous access
793 -- is associated with one of the protected operations, and must
794 -- be available in the scope that encloses the protected declaration.
795 -- Otherwise the type is in the scope enclosing the subprogram.
797 -- If the function has formals, The return type of a subprogram
798 -- declaration is analyzed in the scope of the subprogram (see
799 -- Process_Formals) and thus the protected type, if present, is
800 -- the scope of the current function scope.
802 if Ekind (Current_Scope) = E_Protected_Type then
803 Enclosing_Prot_Type := Current_Scope;
805 elsif Ekind (Current_Scope) = E_Function
806 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
807 then
808 Enclosing_Prot_Type := Scope (Current_Scope);
809 end if;
811 if Present (Enclosing_Prot_Type) then
812 Anon_Scope := Scope (Enclosing_Prot_Type);
814 else
815 Anon_Scope := Scope (Defining_Entity (Related_Nod));
816 end if;
818 -- For an access type definition, if the current scope is a child
819 -- unit it is the scope of the type.
821 elsif Is_Compilation_Unit (Current_Scope) then
822 Anon_Scope := Current_Scope;
824 -- For access formals, access components, and access discriminants, the
825 -- scope is that of the enclosing declaration,
827 else
828 Anon_Scope := Scope (Current_Scope);
829 end if;
831 Anon_Type :=
832 Create_Itype
833 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
835 if All_Present (N)
836 and then Ada_Version >= Ada_2005
837 then
838 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
839 end if;
841 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
842 -- the corresponding semantic routine
844 if Present (Access_To_Subprogram_Definition (N)) then
846 -- Compiler runtime units are compiled in Ada 2005 mode when building
847 -- the runtime library but must also be compilable in Ada 95 mode
848 -- (when bootstrapping the compiler).
850 Check_Compiler_Unit ("anonymous access to subprogram", N);
852 Access_Subprogram_Declaration
853 (T_Name => Anon_Type,
854 T_Def => Access_To_Subprogram_Definition (N));
856 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
857 Set_Ekind
858 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
859 else
860 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
861 end if;
863 Set_Can_Use_Internal_Rep
864 (Anon_Type, not Always_Compatible_Rep_On_Target);
866 -- If the anonymous access is associated with a protected operation,
867 -- create a reference to it after the enclosing protected definition
868 -- because the itype will be used in the subsequent bodies.
870 -- If the anonymous access itself is protected, a full type
871 -- declaratiton will be created for it, so that the equivalent
872 -- record type can be constructed. For further details, see
873 -- Replace_Anonymous_Access_To_Protected-Subprogram.
875 if Ekind (Current_Scope) = E_Protected_Type
876 and then not Protected_Present (Access_To_Subprogram_Definition (N))
877 then
878 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
879 end if;
881 return Anon_Type;
882 end if;
884 Find_Type (Subtype_Mark (N));
885 Desig_Type := Entity (Subtype_Mark (N));
887 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
888 Set_Etype (Anon_Type, Anon_Type);
890 -- Make sure the anonymous access type has size and alignment fields
891 -- set, as required by gigi. This is necessary in the case of the
892 -- Task_Body_Procedure.
894 if not Has_Private_Component (Desig_Type) then
895 Layout_Type (Anon_Type);
896 end if;
898 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
899 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
900 -- the null value is allowed. In Ada 95 the null value is never allowed.
902 if Ada_Version >= Ada_2005 then
903 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
904 else
905 Set_Can_Never_Be_Null (Anon_Type, True);
906 end if;
908 -- The anonymous access type is as public as the discriminated type or
909 -- subprogram that defines it. It is imported (for back-end purposes)
910 -- if the designated type is.
912 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
914 -- Ada 2005 (AI-231): Propagate the access-constant attribute
916 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
918 -- The context is either a subprogram declaration, object declaration,
919 -- or an access discriminant, in a private or a full type declaration.
920 -- In the case of a subprogram, if the designated type is incomplete,
921 -- the operation will be a primitive operation of the full type, to be
922 -- updated subsequently. If the type is imported through a limited_with
923 -- clause, the subprogram is not a primitive operation of the type
924 -- (which is declared elsewhere in some other scope).
926 if Ekind (Desig_Type) = E_Incomplete_Type
927 and then not From_Limited_With (Desig_Type)
928 and then Is_Overloadable (Current_Scope)
929 then
930 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
931 Set_Has_Delayed_Freeze (Current_Scope);
932 end if;
934 -- Ada 2005: If the designated type is an interface that may contain
935 -- tasks, create a Master entity for the declaration. This must be done
936 -- before expansion of the full declaration, because the declaration may
937 -- include an expression that is an allocator, whose expansion needs the
938 -- proper Master for the created tasks.
940 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
941 then
942 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
943 then
944 Build_Class_Wide_Master (Anon_Type);
946 -- Similarly, if the type is an anonymous access that designates
947 -- tasks, create a master entity for it in the current context.
949 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
950 then
951 Build_Master_Entity (Defining_Identifier (Related_Nod));
952 Build_Master_Renaming (Anon_Type);
953 end if;
954 end if;
956 -- For a private component of a protected type, it is imperative that
957 -- the back-end elaborate the type immediately after the protected
958 -- declaration, because this type will be used in the declarations
959 -- created for the component within each protected body, so we must
960 -- create an itype reference for it now.
962 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
963 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
965 -- Similarly, if the access definition is the return result of a
966 -- function, create an itype reference for it because it will be used
967 -- within the function body. For a regular function that is not a
968 -- compilation unit, insert reference after the declaration. For a
969 -- protected operation, insert it after the enclosing protected type
970 -- declaration. In either case, do not create a reference for a type
971 -- obtained through a limited_with clause, because this would introduce
972 -- semantic dependencies.
974 -- Similarly, do not create a reference if the designated type is a
975 -- generic formal, because no use of it will reach the backend.
977 elsif Nkind (Related_Nod) = N_Function_Specification
978 and then not From_Limited_With (Desig_Type)
979 and then not Is_Generic_Type (Desig_Type)
980 then
981 if Present (Enclosing_Prot_Type) then
982 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
984 elsif Is_List_Member (Parent (Related_Nod))
985 and then Nkind (Parent (N)) /= N_Parameter_Specification
986 then
987 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
988 end if;
990 -- Finally, create an itype reference for an object declaration of an
991 -- anonymous access type. This is strictly necessary only for deferred
992 -- constants, but in any case will avoid out-of-scope problems in the
993 -- back-end.
995 elsif Nkind (Related_Nod) = N_Object_Declaration then
996 Build_Itype_Reference (Anon_Type, Related_Nod);
997 end if;
999 return Anon_Type;
1000 end Access_Definition;
1002 -----------------------------------
1003 -- Access_Subprogram_Declaration --
1004 -----------------------------------
1006 procedure Access_Subprogram_Declaration
1007 (T_Name : Entity_Id;
1008 T_Def : Node_Id)
1010 procedure Check_For_Premature_Usage (Def : Node_Id);
1011 -- Check that type T_Name is not used, directly or recursively, as a
1012 -- parameter or a return type in Def. Def is either a subtype, an
1013 -- access_definition, or an access_to_subprogram_definition.
1015 -------------------------------
1016 -- Check_For_Premature_Usage --
1017 -------------------------------
1019 procedure Check_For_Premature_Usage (Def : Node_Id) is
1020 Param : Node_Id;
1022 begin
1023 -- Check for a subtype mark
1025 if Nkind (Def) in N_Has_Etype then
1026 if Etype (Def) = T_Name then
1027 Error_Msg_N
1028 ("type& cannot be used before end of its declaration", Def);
1029 end if;
1031 -- If this is not a subtype, then this is an access_definition
1033 elsif Nkind (Def) = N_Access_Definition then
1034 if Present (Access_To_Subprogram_Definition (Def)) then
1035 Check_For_Premature_Usage
1036 (Access_To_Subprogram_Definition (Def));
1037 else
1038 Check_For_Premature_Usage (Subtype_Mark (Def));
1039 end if;
1041 -- The only cases left are N_Access_Function_Definition and
1042 -- N_Access_Procedure_Definition.
1044 else
1045 if Present (Parameter_Specifications (Def)) then
1046 Param := First (Parameter_Specifications (Def));
1047 while Present (Param) loop
1048 Check_For_Premature_Usage (Parameter_Type (Param));
1049 Param := Next (Param);
1050 end loop;
1051 end if;
1053 if Nkind (Def) = N_Access_Function_Definition then
1054 Check_For_Premature_Usage (Result_Definition (Def));
1055 end if;
1056 end if;
1057 end Check_For_Premature_Usage;
1059 -- Local variables
1061 Formals : constant List_Id := Parameter_Specifications (T_Def);
1062 Formal : Entity_Id;
1063 D_Ityp : Node_Id;
1064 Desig_Type : constant Entity_Id :=
1065 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1067 -- Start of processing for Access_Subprogram_Declaration
1069 begin
1070 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1072 -- Associate the Itype node with the inner full-type declaration or
1073 -- subprogram spec or entry body. This is required to handle nested
1074 -- anonymous declarations. For example:
1076 -- procedure P
1077 -- (X : access procedure
1078 -- (Y : access procedure
1079 -- (Z : access T)))
1081 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1082 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1083 N_Private_Type_Declaration,
1084 N_Private_Extension_Declaration,
1085 N_Procedure_Specification,
1086 N_Function_Specification,
1087 N_Entry_Body)
1089 or else
1090 Nkind_In (D_Ityp, N_Object_Declaration,
1091 N_Object_Renaming_Declaration,
1092 N_Formal_Object_Declaration,
1093 N_Formal_Type_Declaration,
1094 N_Task_Type_Declaration,
1095 N_Protected_Type_Declaration))
1096 loop
1097 D_Ityp := Parent (D_Ityp);
1098 pragma Assert (D_Ityp /= Empty);
1099 end loop;
1101 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1103 if Nkind_In (D_Ityp, N_Procedure_Specification,
1104 N_Function_Specification)
1105 then
1106 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1108 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1109 N_Object_Declaration,
1110 N_Object_Renaming_Declaration,
1111 N_Formal_Type_Declaration)
1112 then
1113 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1114 end if;
1116 if Nkind (T_Def) = N_Access_Function_Definition then
1117 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1118 declare
1119 Acc : constant Node_Id := Result_Definition (T_Def);
1121 begin
1122 if Present (Access_To_Subprogram_Definition (Acc))
1123 and then
1124 Protected_Present (Access_To_Subprogram_Definition (Acc))
1125 then
1126 Set_Etype
1127 (Desig_Type,
1128 Replace_Anonymous_Access_To_Protected_Subprogram
1129 (T_Def));
1131 else
1132 Set_Etype
1133 (Desig_Type,
1134 Access_Definition (T_Def, Result_Definition (T_Def)));
1135 end if;
1136 end;
1138 else
1139 Analyze (Result_Definition (T_Def));
1141 declare
1142 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1144 begin
1145 -- If a null exclusion is imposed on the result type, then
1146 -- create a null-excluding itype (an access subtype) and use
1147 -- it as the function's Etype.
1149 if Is_Access_Type (Typ)
1150 and then Null_Exclusion_In_Return_Present (T_Def)
1151 then
1152 Set_Etype (Desig_Type,
1153 Create_Null_Excluding_Itype
1154 (T => Typ,
1155 Related_Nod => T_Def,
1156 Scope_Id => Current_Scope));
1158 else
1159 if From_Limited_With (Typ) then
1161 -- AI05-151: Incomplete types are allowed in all basic
1162 -- declarations, including access to subprograms.
1164 if Ada_Version >= Ada_2012 then
1165 null;
1167 else
1168 Error_Msg_NE
1169 ("illegal use of incomplete type&",
1170 Result_Definition (T_Def), Typ);
1171 end if;
1173 elsif Ekind (Current_Scope) = E_Package
1174 and then In_Private_Part (Current_Scope)
1175 then
1176 if Ekind (Typ) = E_Incomplete_Type then
1177 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1179 elsif Is_Class_Wide_Type (Typ)
1180 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1181 then
1182 Append_Elmt
1183 (Desig_Type, Private_Dependents (Etype (Typ)));
1184 end if;
1185 end if;
1187 Set_Etype (Desig_Type, Typ);
1188 end if;
1189 end;
1190 end if;
1192 if not (Is_Type (Etype (Desig_Type))) then
1193 Error_Msg_N
1194 ("expect type in function specification",
1195 Result_Definition (T_Def));
1196 end if;
1198 else
1199 Set_Etype (Desig_Type, Standard_Void_Type);
1200 end if;
1202 if Present (Formals) then
1203 Push_Scope (Desig_Type);
1205 -- Some special tests here. These special tests can be removed
1206 -- if and when Itypes always have proper parent pointers to their
1207 -- declarations???
1209 -- Special test 1) Link defining_identifier of formals. Required by
1210 -- First_Formal to provide its functionality.
1212 declare
1213 F : Node_Id;
1215 begin
1216 F := First (Formals);
1218 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1219 -- when it is part of an unconstrained type and subtype expansion
1220 -- is disabled. To avoid back-end problems with shared profiles,
1221 -- use previous subprogram type as the designated type, and then
1222 -- remove scope added above.
1224 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1225 then
1226 Set_Etype (T_Name, T_Name);
1227 Init_Size_Align (T_Name);
1228 Set_Directly_Designated_Type (T_Name,
1229 Scope (Defining_Identifier (F)));
1230 End_Scope;
1231 return;
1232 end if;
1234 while Present (F) loop
1235 if No (Parent (Defining_Identifier (F))) then
1236 Set_Parent (Defining_Identifier (F), F);
1237 end if;
1239 Next (F);
1240 end loop;
1241 end;
1243 Process_Formals (Formals, Parent (T_Def));
1245 -- Special test 2) End_Scope requires that the parent pointer be set
1246 -- to something reasonable, but Itypes don't have parent pointers. So
1247 -- we set it and then unset it ???
1249 Set_Parent (Desig_Type, T_Name);
1250 End_Scope;
1251 Set_Parent (Desig_Type, Empty);
1252 end if;
1254 -- Check for premature usage of the type being defined
1256 Check_For_Premature_Usage (T_Def);
1258 -- The return type and/or any parameter type may be incomplete. Mark the
1259 -- subprogram_type as depending on the incomplete type, so that it can
1260 -- be updated when the full type declaration is seen. This only applies
1261 -- to incomplete types declared in some enclosing scope, not to limited
1262 -- views from other packages.
1264 -- Prior to Ada 2012, access to functions can only have in_parameters.
1266 if Present (Formals) then
1267 Formal := First_Formal (Desig_Type);
1268 while Present (Formal) loop
1269 if Ekind (Formal) /= E_In_Parameter
1270 and then Nkind (T_Def) = N_Access_Function_Definition
1271 and then Ada_Version < Ada_2012
1272 then
1273 Error_Msg_N ("functions can only have IN parameters", Formal);
1274 end if;
1276 if Ekind (Etype (Formal)) = E_Incomplete_Type
1277 and then In_Open_Scopes (Scope (Etype (Formal)))
1278 then
1279 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1280 Set_Has_Delayed_Freeze (Desig_Type);
1281 end if;
1283 Next_Formal (Formal);
1284 end loop;
1285 end if;
1287 -- Check whether an indirect call without actuals may be possible. This
1288 -- is used when resolving calls whose result is then indexed.
1290 May_Need_Actuals (Desig_Type);
1292 -- If the return type is incomplete, this is legal as long as the type
1293 -- is declared in the current scope and will be completed in it (rather
1294 -- than being part of limited view).
1296 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1297 and then not Has_Delayed_Freeze (Desig_Type)
1298 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1299 then
1300 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1301 Set_Has_Delayed_Freeze (Desig_Type);
1302 end if;
1304 Check_Delayed_Subprogram (Desig_Type);
1306 if Protected_Present (T_Def) then
1307 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1308 Set_Convention (Desig_Type, Convention_Protected);
1309 else
1310 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1311 end if;
1313 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1315 Set_Etype (T_Name, T_Name);
1316 Init_Size_Align (T_Name);
1317 Set_Directly_Designated_Type (T_Name, Desig_Type);
1319 Generate_Reference_To_Formals (T_Name);
1321 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1323 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1325 Check_Restriction (No_Access_Subprograms, T_Def);
1326 end Access_Subprogram_Declaration;
1328 ----------------------------
1329 -- Access_Type_Declaration --
1330 ----------------------------
1332 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1333 P : constant Node_Id := Parent (Def);
1334 S : constant Node_Id := Subtype_Indication (Def);
1336 Full_Desig : Entity_Id;
1338 begin
1339 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1341 -- Check for permissible use of incomplete type
1343 if Nkind (S) /= N_Subtype_Indication then
1344 Analyze (S);
1346 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1347 Set_Directly_Designated_Type (T, Entity (S));
1349 -- If the designated type is a limited view, we cannot tell if
1350 -- the full view contains tasks, and there is no way to handle
1351 -- that full view in a client. We create a master entity for the
1352 -- scope, which will be used when a client determines that one
1353 -- is needed.
1355 if From_Limited_With (Entity (S))
1356 and then not Is_Class_Wide_Type (Entity (S))
1357 then
1358 Set_Ekind (T, E_Access_Type);
1359 Build_Master_Entity (T);
1360 Build_Master_Renaming (T);
1361 end if;
1363 else
1364 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1365 end if;
1367 -- If the access definition is of the form: ACCESS NOT NULL ..
1368 -- the subtype indication must be of an access type. Create
1369 -- a null-excluding subtype of it.
1371 if Null_Excluding_Subtype (Def) then
1372 if not Is_Access_Type (Entity (S)) then
1373 Error_Msg_N ("null exclusion must apply to access type", Def);
1375 else
1376 declare
1377 Loc : constant Source_Ptr := Sloc (S);
1378 Decl : Node_Id;
1379 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1381 begin
1382 Decl :=
1383 Make_Subtype_Declaration (Loc,
1384 Defining_Identifier => Nam,
1385 Subtype_Indication =>
1386 New_Occurrence_Of (Entity (S), Loc));
1387 Set_Null_Exclusion_Present (Decl);
1388 Insert_Before (Parent (Def), Decl);
1389 Analyze (Decl);
1390 Set_Entity (S, Nam);
1391 end;
1392 end if;
1393 end if;
1395 else
1396 Set_Directly_Designated_Type (T,
1397 Process_Subtype (S, P, T, 'P'));
1398 end if;
1400 if All_Present (Def) or Constant_Present (Def) then
1401 Set_Ekind (T, E_General_Access_Type);
1402 else
1403 Set_Ekind (T, E_Access_Type);
1404 end if;
1406 Full_Desig := Designated_Type (T);
1408 if Base_Type (Full_Desig) = T then
1409 Error_Msg_N ("access type cannot designate itself", S);
1411 -- In Ada 2005, the type may have a limited view through some unit in
1412 -- its own context, allowing the following circularity that cannot be
1413 -- detected earlier.
1415 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1416 then
1417 Error_Msg_N
1418 ("access type cannot designate its own classwide type", S);
1420 -- Clean up indication of tagged status to prevent cascaded errors
1422 Set_Is_Tagged_Type (T, False);
1423 end if;
1425 Set_Etype (T, T);
1427 -- If the type has appeared already in a with_type clause, it is frozen
1428 -- and the pointer size is already set. Else, initialize.
1430 if not From_Limited_With (T) then
1431 Init_Size_Align (T);
1432 end if;
1434 -- Note that Has_Task is always false, since the access type itself
1435 -- is not a task type. See Einfo for more description on this point.
1436 -- Exactly the same consideration applies to Has_Controlled_Component
1437 -- and to Has_Protected.
1439 Set_Has_Task (T, False);
1440 Set_Has_Controlled_Component (T, False);
1441 Set_Has_Protected (T, False);
1443 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1444 -- problems where an incomplete view of this entity has been previously
1445 -- established by a limited with and an overlaid version of this field
1446 -- (Stored_Constraint) was initialized for the incomplete view.
1448 -- This reset is performed in most cases except where the access type
1449 -- has been created for the purposes of allocating or deallocating a
1450 -- build-in-place object. Such access types have explicitly set pools
1451 -- and finalization masters.
1453 if No (Associated_Storage_Pool (T)) then
1454 Set_Finalization_Master (T, Empty);
1455 end if;
1457 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1458 -- attributes
1460 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1461 Set_Is_Access_Constant (T, Constant_Present (Def));
1462 end Access_Type_Declaration;
1464 ----------------------------------
1465 -- Add_Interface_Tag_Components --
1466 ----------------------------------
1468 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1469 Loc : constant Source_Ptr := Sloc (N);
1470 L : List_Id;
1471 Last_Tag : Node_Id;
1473 procedure Add_Tag (Iface : Entity_Id);
1474 -- Add tag for one of the progenitor interfaces
1476 -------------
1477 -- Add_Tag --
1478 -------------
1480 procedure Add_Tag (Iface : Entity_Id) is
1481 Decl : Node_Id;
1482 Def : Node_Id;
1483 Tag : Entity_Id;
1484 Offset : Entity_Id;
1486 begin
1487 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1489 -- This is a reasonable place to propagate predicates
1491 if Has_Predicates (Iface) then
1492 Set_Has_Predicates (Typ);
1493 end if;
1495 Def :=
1496 Make_Component_Definition (Loc,
1497 Aliased_Present => True,
1498 Subtype_Indication =>
1499 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1501 Tag := Make_Temporary (Loc, 'V');
1503 Decl :=
1504 Make_Component_Declaration (Loc,
1505 Defining_Identifier => Tag,
1506 Component_Definition => Def);
1508 Analyze_Component_Declaration (Decl);
1510 Set_Analyzed (Decl);
1511 Set_Ekind (Tag, E_Component);
1512 Set_Is_Tag (Tag);
1513 Set_Is_Aliased (Tag);
1514 Set_Related_Type (Tag, Iface);
1515 Init_Component_Location (Tag);
1517 pragma Assert (Is_Frozen (Iface));
1519 Set_DT_Entry_Count (Tag,
1520 DT_Entry_Count (First_Entity (Iface)));
1522 if No (Last_Tag) then
1523 Prepend (Decl, L);
1524 else
1525 Insert_After (Last_Tag, Decl);
1526 end if;
1528 Last_Tag := Decl;
1530 -- If the ancestor has discriminants we need to give special support
1531 -- to store the offset_to_top value of the secondary dispatch tables.
1532 -- For this purpose we add a supplementary component just after the
1533 -- field that contains the tag associated with each secondary DT.
1535 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1536 Def :=
1537 Make_Component_Definition (Loc,
1538 Subtype_Indication =>
1539 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1541 Offset := Make_Temporary (Loc, 'V');
1543 Decl :=
1544 Make_Component_Declaration (Loc,
1545 Defining_Identifier => Offset,
1546 Component_Definition => Def);
1548 Analyze_Component_Declaration (Decl);
1550 Set_Analyzed (Decl);
1551 Set_Ekind (Offset, E_Component);
1552 Set_Is_Aliased (Offset);
1553 Set_Related_Type (Offset, Iface);
1554 Init_Component_Location (Offset);
1555 Insert_After (Last_Tag, Decl);
1556 Last_Tag := Decl;
1557 end if;
1558 end Add_Tag;
1560 -- Local variables
1562 Elmt : Elmt_Id;
1563 Ext : Node_Id;
1564 Comp : Node_Id;
1566 -- Start of processing for Add_Interface_Tag_Components
1568 begin
1569 if not RTE_Available (RE_Interface_Tag) then
1570 Error_Msg
1571 ("(Ada 2005) interface types not supported by this run-time!",
1572 Sloc (N));
1573 return;
1574 end if;
1576 if Ekind (Typ) /= E_Record_Type
1577 or else (Is_Concurrent_Record_Type (Typ)
1578 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1579 or else (not Is_Concurrent_Record_Type (Typ)
1580 and then No (Interfaces (Typ))
1581 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1582 then
1583 return;
1584 end if;
1586 -- Find the current last tag
1588 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1589 Ext := Record_Extension_Part (Type_Definition (N));
1590 else
1591 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1592 Ext := Type_Definition (N);
1593 end if;
1595 Last_Tag := Empty;
1597 if not (Present (Component_List (Ext))) then
1598 Set_Null_Present (Ext, False);
1599 L := New_List;
1600 Set_Component_List (Ext,
1601 Make_Component_List (Loc,
1602 Component_Items => L,
1603 Null_Present => False));
1604 else
1605 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1606 L := Component_Items
1607 (Component_List
1608 (Record_Extension_Part
1609 (Type_Definition (N))));
1610 else
1611 L := Component_Items
1612 (Component_List
1613 (Type_Definition (N)));
1614 end if;
1616 -- Find the last tag component
1618 Comp := First (L);
1619 while Present (Comp) loop
1620 if Nkind (Comp) = N_Component_Declaration
1621 and then Is_Tag (Defining_Identifier (Comp))
1622 then
1623 Last_Tag := Comp;
1624 end if;
1626 Next (Comp);
1627 end loop;
1628 end if;
1630 -- At this point L references the list of components and Last_Tag
1631 -- references the current last tag (if any). Now we add the tag
1632 -- corresponding with all the interfaces that are not implemented
1633 -- by the parent.
1635 if Present (Interfaces (Typ)) then
1636 Elmt := First_Elmt (Interfaces (Typ));
1637 while Present (Elmt) loop
1638 Add_Tag (Node (Elmt));
1639 Next_Elmt (Elmt);
1640 end loop;
1641 end if;
1642 end Add_Interface_Tag_Components;
1644 -------------------------------------
1645 -- Add_Internal_Interface_Entities --
1646 -------------------------------------
1648 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1649 Elmt : Elmt_Id;
1650 Iface : Entity_Id;
1651 Iface_Elmt : Elmt_Id;
1652 Iface_Prim : Entity_Id;
1653 Ifaces_List : Elist_Id;
1654 New_Subp : Entity_Id := Empty;
1655 Prim : Entity_Id;
1656 Restore_Scope : Boolean := False;
1658 begin
1659 pragma Assert (Ada_Version >= Ada_2005
1660 and then Is_Record_Type (Tagged_Type)
1661 and then Is_Tagged_Type (Tagged_Type)
1662 and then Has_Interfaces (Tagged_Type)
1663 and then not Is_Interface (Tagged_Type));
1665 -- Ensure that the internal entities are added to the scope of the type
1667 if Scope (Tagged_Type) /= Current_Scope then
1668 Push_Scope (Scope (Tagged_Type));
1669 Restore_Scope := True;
1670 end if;
1672 Collect_Interfaces (Tagged_Type, Ifaces_List);
1674 Iface_Elmt := First_Elmt (Ifaces_List);
1675 while Present (Iface_Elmt) loop
1676 Iface := Node (Iface_Elmt);
1678 -- Originally we excluded here from this processing interfaces that
1679 -- are parents of Tagged_Type because their primitives are located
1680 -- in the primary dispatch table (and hence no auxiliary internal
1681 -- entities are required to handle secondary dispatch tables in such
1682 -- case). However, these auxiliary entities are also required to
1683 -- handle derivations of interfaces in formals of generics (see
1684 -- Derive_Subprograms).
1686 Elmt := First_Elmt (Primitive_Operations (Iface));
1687 while Present (Elmt) loop
1688 Iface_Prim := Node (Elmt);
1690 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1691 Prim :=
1692 Find_Primitive_Covering_Interface
1693 (Tagged_Type => Tagged_Type,
1694 Iface_Prim => Iface_Prim);
1696 if No (Prim) and then Serious_Errors_Detected > 0 then
1697 goto Continue;
1698 end if;
1700 pragma Assert (Present (Prim));
1702 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1703 -- differs from the name of the interface primitive then it is
1704 -- a private primitive inherited from a parent type. In such
1705 -- case, given that Tagged_Type covers the interface, the
1706 -- inherited private primitive becomes visible. For such
1707 -- purpose we add a new entity that renames the inherited
1708 -- private primitive.
1710 if Chars (Prim) /= Chars (Iface_Prim) then
1711 pragma Assert (Has_Suffix (Prim, 'P'));
1712 Derive_Subprogram
1713 (New_Subp => New_Subp,
1714 Parent_Subp => Iface_Prim,
1715 Derived_Type => Tagged_Type,
1716 Parent_Type => Iface);
1717 Set_Alias (New_Subp, Prim);
1718 Set_Is_Abstract_Subprogram
1719 (New_Subp, Is_Abstract_Subprogram (Prim));
1720 end if;
1722 Derive_Subprogram
1723 (New_Subp => New_Subp,
1724 Parent_Subp => Iface_Prim,
1725 Derived_Type => Tagged_Type,
1726 Parent_Type => Iface);
1728 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1729 -- associated with interface types. These entities are
1730 -- only registered in the list of primitives of its
1731 -- corresponding tagged type because they are only used
1732 -- to fill the contents of the secondary dispatch tables.
1733 -- Therefore they are removed from the homonym chains.
1735 Set_Is_Hidden (New_Subp);
1736 Set_Is_Internal (New_Subp);
1737 Set_Alias (New_Subp, Prim);
1738 Set_Is_Abstract_Subprogram
1739 (New_Subp, Is_Abstract_Subprogram (Prim));
1740 Set_Interface_Alias (New_Subp, Iface_Prim);
1742 -- If the returned type is an interface then propagate it to
1743 -- the returned type. Needed by the thunk to generate the code
1744 -- which displaces "this" to reference the corresponding
1745 -- secondary dispatch table in the returned object.
1747 if Is_Interface (Etype (Iface_Prim)) then
1748 Set_Etype (New_Subp, Etype (Iface_Prim));
1749 end if;
1751 -- Internal entities associated with interface types are only
1752 -- registered in the list of primitives of the tagged type.
1753 -- They are only used to fill the contents of the secondary
1754 -- dispatch tables. Therefore they are not needed in the
1755 -- homonym chains.
1757 Remove_Homonym (New_Subp);
1759 -- Hidden entities associated with interfaces must have set
1760 -- the Has_Delay_Freeze attribute to ensure that, in case
1761 -- of locally defined tagged types (or compiling with static
1762 -- dispatch tables generation disabled) the corresponding
1763 -- entry of the secondary dispatch table is filled when such
1764 -- an entity is frozen. This is an expansion activity that must
1765 -- be suppressed for ASIS because it leads to gigi elaboration
1766 -- issues in annotate mode.
1768 if not ASIS_Mode then
1769 Set_Has_Delayed_Freeze (New_Subp);
1770 end if;
1771 end if;
1773 <<Continue>>
1774 Next_Elmt (Elmt);
1775 end loop;
1777 Next_Elmt (Iface_Elmt);
1778 end loop;
1780 if Restore_Scope then
1781 Pop_Scope;
1782 end if;
1783 end Add_Internal_Interface_Entities;
1785 -----------------------------------
1786 -- Analyze_Component_Declaration --
1787 -----------------------------------
1789 procedure Analyze_Component_Declaration (N : Node_Id) is
1790 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1791 Id : constant Entity_Id := Defining_Identifier (N);
1792 E : constant Node_Id := Expression (N);
1793 Typ : constant Node_Id :=
1794 Subtype_Indication (Component_Definition (N));
1795 T : Entity_Id;
1796 P : Entity_Id;
1798 function Contains_POC (Constr : Node_Id) return Boolean;
1799 -- Determines whether a constraint uses the discriminant of a record
1800 -- type thus becoming a per-object constraint (POC).
1802 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1803 -- Typ is the type of the current component, check whether this type is
1804 -- a limited type. Used to validate declaration against that of
1805 -- enclosing record.
1807 ------------------
1808 -- Contains_POC --
1809 ------------------
1811 function Contains_POC (Constr : Node_Id) return Boolean is
1812 begin
1813 -- Prevent cascaded errors
1815 if Error_Posted (Constr) then
1816 return False;
1817 end if;
1819 case Nkind (Constr) is
1820 when N_Attribute_Reference =>
1821 return Attribute_Name (Constr) = Name_Access
1822 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1824 when N_Discriminant_Association =>
1825 return Denotes_Discriminant (Expression (Constr));
1827 when N_Identifier =>
1828 return Denotes_Discriminant (Constr);
1830 when N_Index_Or_Discriminant_Constraint =>
1831 declare
1832 IDC : Node_Id;
1834 begin
1835 IDC := First (Constraints (Constr));
1836 while Present (IDC) loop
1838 -- One per-object constraint is sufficient
1840 if Contains_POC (IDC) then
1841 return True;
1842 end if;
1844 Next (IDC);
1845 end loop;
1847 return False;
1848 end;
1850 when N_Range =>
1851 return Denotes_Discriminant (Low_Bound (Constr))
1852 or else
1853 Denotes_Discriminant (High_Bound (Constr));
1855 when N_Range_Constraint =>
1856 return Denotes_Discriminant (Range_Expression (Constr));
1858 when others =>
1859 return False;
1861 end case;
1862 end Contains_POC;
1864 ----------------------
1865 -- Is_Known_Limited --
1866 ----------------------
1868 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1869 P : constant Entity_Id := Etype (Typ);
1870 R : constant Entity_Id := Root_Type (Typ);
1872 begin
1873 if Is_Limited_Record (Typ) then
1874 return True;
1876 -- If the root type is limited (and not a limited interface)
1877 -- so is the current type
1879 elsif Is_Limited_Record (R)
1880 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1881 then
1882 return True;
1884 -- Else the type may have a limited interface progenitor, but a
1885 -- limited record parent.
1887 elsif R /= P and then Is_Limited_Record (P) then
1888 return True;
1890 else
1891 return False;
1892 end if;
1893 end Is_Known_Limited;
1895 -- Start of processing for Analyze_Component_Declaration
1897 begin
1898 Generate_Definition (Id);
1899 Enter_Name (Id);
1901 if Present (Typ) then
1902 T := Find_Type_Of_Object
1903 (Subtype_Indication (Component_Definition (N)), N);
1905 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1906 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1907 end if;
1909 -- Ada 2005 (AI-230): Access Definition case
1911 else
1912 pragma Assert (Present
1913 (Access_Definition (Component_Definition (N))));
1915 T := Access_Definition
1916 (Related_Nod => N,
1917 N => Access_Definition (Component_Definition (N)));
1918 Set_Is_Local_Anonymous_Access (T);
1920 -- Ada 2005 (AI-254)
1922 if Present (Access_To_Subprogram_Definition
1923 (Access_Definition (Component_Definition (N))))
1924 and then Protected_Present (Access_To_Subprogram_Definition
1925 (Access_Definition
1926 (Component_Definition (N))))
1927 then
1928 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1929 end if;
1930 end if;
1932 -- If the subtype is a constrained subtype of the enclosing record,
1933 -- (which must have a partial view) the back-end does not properly
1934 -- handle the recursion. Rewrite the component declaration with an
1935 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1936 -- the tree directly because side effects have already been removed from
1937 -- discriminant constraints.
1939 if Ekind (T) = E_Access_Subtype
1940 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1941 and then Comes_From_Source (T)
1942 and then Nkind (Parent (T)) = N_Subtype_Declaration
1943 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1944 then
1945 Rewrite
1946 (Subtype_Indication (Component_Definition (N)),
1947 New_Copy_Tree (Subtype_Indication (Parent (T))));
1948 T := Find_Type_Of_Object
1949 (Subtype_Indication (Component_Definition (N)), N);
1950 end if;
1952 -- If the component declaration includes a default expression, then we
1953 -- check that the component is not of a limited type (RM 3.7(5)),
1954 -- and do the special preanalysis of the expression (see section on
1955 -- "Handling of Default and Per-Object Expressions" in the spec of
1956 -- package Sem).
1958 if Present (E) then
1959 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1960 Preanalyze_Default_Expression (E, T);
1961 Check_Initialization (T, E);
1963 if Ada_Version >= Ada_2005
1964 and then Ekind (T) = E_Anonymous_Access_Type
1965 and then Etype (E) /= Any_Type
1966 then
1967 -- Check RM 3.9.2(9): "if the expected type for an expression is
1968 -- an anonymous access-to-specific tagged type, then the object
1969 -- designated by the expression shall not be dynamically tagged
1970 -- unless it is a controlling operand in a call on a dispatching
1971 -- operation"
1973 if Is_Tagged_Type (Directly_Designated_Type (T))
1974 and then
1975 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1976 and then
1977 Ekind (Directly_Designated_Type (Etype (E))) =
1978 E_Class_Wide_Type
1979 then
1980 Error_Msg_N
1981 ("access to specific tagged type required (RM 3.9.2(9))", E);
1982 end if;
1984 -- (Ada 2005: AI-230): Accessibility check for anonymous
1985 -- components
1987 if Type_Access_Level (Etype (E)) >
1988 Deepest_Type_Access_Level (T)
1989 then
1990 Error_Msg_N
1991 ("expression has deeper access level than component " &
1992 "(RM 3.10.2 (12.2))", E);
1993 end if;
1995 -- The initialization expression is a reference to an access
1996 -- discriminant. The type of the discriminant is always deeper
1997 -- than any access type.
1999 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2000 and then Is_Entity_Name (E)
2001 and then Ekind (Entity (E)) = E_In_Parameter
2002 and then Present (Discriminal_Link (Entity (E)))
2003 then
2004 Error_Msg_N
2005 ("discriminant has deeper accessibility level than target",
2007 end if;
2008 end if;
2009 end if;
2011 -- The parent type may be a private view with unknown discriminants,
2012 -- and thus unconstrained. Regular components must be constrained.
2014 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2015 if Is_Class_Wide_Type (T) then
2016 Error_Msg_N
2017 ("class-wide subtype with unknown discriminants" &
2018 " in component declaration",
2019 Subtype_Indication (Component_Definition (N)));
2020 else
2021 Error_Msg_N
2022 ("unconstrained subtype in component declaration",
2023 Subtype_Indication (Component_Definition (N)));
2024 end if;
2026 -- Components cannot be abstract, except for the special case of
2027 -- the _Parent field (case of extending an abstract tagged type)
2029 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2030 Error_Msg_N ("type of a component cannot be abstract", N);
2031 end if;
2033 Set_Etype (Id, T);
2034 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2036 -- The component declaration may have a per-object constraint, set
2037 -- the appropriate flag in the defining identifier of the subtype.
2039 if Present (Subtype_Indication (Component_Definition (N))) then
2040 declare
2041 Sindic : constant Node_Id :=
2042 Subtype_Indication (Component_Definition (N));
2043 begin
2044 if Nkind (Sindic) = N_Subtype_Indication
2045 and then Present (Constraint (Sindic))
2046 and then Contains_POC (Constraint (Sindic))
2047 then
2048 Set_Has_Per_Object_Constraint (Id);
2049 end if;
2050 end;
2051 end if;
2053 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2054 -- out some static checks.
2056 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2057 Null_Exclusion_Static_Checks (N);
2058 end if;
2060 -- If this component is private (or depends on a private type), flag the
2061 -- record type to indicate that some operations are not available.
2063 P := Private_Component (T);
2065 if Present (P) then
2067 -- Check for circular definitions
2069 if P = Any_Type then
2070 Set_Etype (Id, Any_Type);
2072 -- There is a gap in the visibility of operations only if the
2073 -- component type is not defined in the scope of the record type.
2075 elsif Scope (P) = Scope (Current_Scope) then
2076 null;
2078 elsif Is_Limited_Type (P) then
2079 Set_Is_Limited_Composite (Current_Scope);
2081 else
2082 Set_Is_Private_Composite (Current_Scope);
2083 end if;
2084 end if;
2086 if P /= Any_Type
2087 and then Is_Limited_Type (T)
2088 and then Chars (Id) /= Name_uParent
2089 and then Is_Tagged_Type (Current_Scope)
2090 then
2091 if Is_Derived_Type (Current_Scope)
2092 and then not Is_Known_Limited (Current_Scope)
2093 then
2094 Error_Msg_N
2095 ("extension of nonlimited type cannot have limited components",
2098 if Is_Interface (Root_Type (Current_Scope)) then
2099 Error_Msg_N
2100 ("\limitedness is not inherited from limited interface", N);
2101 Error_Msg_N ("\add LIMITED to type indication", N);
2102 end if;
2104 Explain_Limited_Type (T, N);
2105 Set_Etype (Id, Any_Type);
2106 Set_Is_Limited_Composite (Current_Scope, False);
2108 elsif not Is_Derived_Type (Current_Scope)
2109 and then not Is_Limited_Record (Current_Scope)
2110 and then not Is_Concurrent_Type (Current_Scope)
2111 then
2112 Error_Msg_N
2113 ("nonlimited tagged type cannot have limited components", N);
2114 Explain_Limited_Type (T, N);
2115 Set_Etype (Id, Any_Type);
2116 Set_Is_Limited_Composite (Current_Scope, False);
2117 end if;
2118 end if;
2120 -- If the component is an unconstrained task or protected type with
2121 -- discriminants, the component and the enclosing record are limited
2122 -- and the component is constrained by its default values. Compute
2123 -- its actual subtype, else it may be allocated the maximum size by
2124 -- the backend, and possibly overflow.
2126 if Is_Concurrent_Type (T)
2127 and then not Is_Constrained (T)
2128 and then Has_Discriminants (T)
2129 and then not Has_Discriminants (Current_Scope)
2130 then
2131 declare
2132 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2134 begin
2135 Set_Etype (Id, Act_T);
2137 -- Rewrite component definition to use the constrained subtype
2139 Rewrite (Component_Definition (N),
2140 Make_Component_Definition (Loc,
2141 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2142 end;
2143 end if;
2145 Set_Original_Record_Component (Id, Id);
2147 if Has_Aspects (N) then
2148 Analyze_Aspect_Specifications (N, Id);
2149 end if;
2151 Analyze_Dimension (N);
2152 end Analyze_Component_Declaration;
2154 --------------------------
2155 -- Analyze_Declarations --
2156 --------------------------
2158 procedure Analyze_Declarations (L : List_Id) is
2159 Decl : Node_Id;
2161 procedure Adjust_Decl;
2162 -- Adjust Decl not to include implicit label declarations, since these
2163 -- have strange Sloc values that result in elaboration check problems.
2164 -- (They have the sloc of the label as found in the source, and that
2165 -- is ahead of the current declarative part).
2167 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2168 -- Determine whether Body_Decl denotes the body of a late controlled
2169 -- primitive (either Initialize, Adjust or Finalize). If this is the
2170 -- case, add a proper spec if the body lacks one. The spec is inserted
2171 -- before Body_Decl and immedately analyzed.
2173 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2174 -- Spec_Id is the entity of a package that may define abstract states.
2175 -- If the states have visible refinement, remove the visibility of each
2176 -- constituent at the end of the package body declarations.
2178 -----------------
2179 -- Adjust_Decl --
2180 -----------------
2182 procedure Adjust_Decl is
2183 begin
2184 while Present (Prev (Decl))
2185 and then Nkind (Decl) = N_Implicit_Label_Declaration
2186 loop
2187 Prev (Decl);
2188 end loop;
2189 end Adjust_Decl;
2191 --------------------------------------
2192 -- Handle_Late_Controlled_Primitive --
2193 --------------------------------------
2195 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2196 Body_Spec : constant Node_Id := Specification (Body_Decl);
2197 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2198 Loc : constant Source_Ptr := Sloc (Body_Id);
2199 Params : constant List_Id :=
2200 Parameter_Specifications (Body_Spec);
2201 Spec : Node_Id;
2202 Spec_Id : Entity_Id;
2203 Typ : Node_Id;
2205 begin
2206 -- Consider only procedure bodies whose name matches one of the three
2207 -- controlled primitives.
2209 if Nkind (Body_Spec) /= N_Procedure_Specification
2210 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2211 Name_Finalize,
2212 Name_Initialize)
2213 then
2214 return;
2216 -- A controlled primitive must have exactly one formal which is not
2217 -- an anonymous access type.
2219 elsif List_Length (Params) /= 1 then
2220 return;
2221 end if;
2223 Typ := Parameter_Type (First (Params));
2225 if Nkind (Typ) = N_Access_Definition then
2226 return;
2227 end if;
2229 Find_Type (Typ);
2231 -- The type of the formal must be derived from [Limited_]Controlled
2233 if not Is_Controlled (Entity (Typ)) then
2234 return;
2235 end if;
2237 -- Check whether a specification exists for this body. We do not
2238 -- analyze the spec of the body in full, because it will be analyzed
2239 -- again when the body is properly analyzed, and we cannot create
2240 -- duplicate entries in the formals chain. We look for an explicit
2241 -- specification because the body may be an overriding operation and
2242 -- an inherited spec may be present.
2244 Spec_Id := Current_Entity (Body_Id);
2246 while Present (Spec_Id) loop
2247 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2248 and then Scope (Spec_Id) = Current_Scope
2249 and then Present (First_Formal (Spec_Id))
2250 and then No (Next_Formal (First_Formal (Spec_Id)))
2251 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2252 and then Comes_From_Source (Spec_Id)
2253 then
2254 return;
2255 end if;
2257 Spec_Id := Homonym (Spec_Id);
2258 end loop;
2260 -- At this point the body is known to be a late controlled primitive.
2261 -- Generate a matching spec and insert it before the body. Note the
2262 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2263 -- tree in this case.
2265 Spec := Copy_Separate_Tree (Body_Spec);
2267 -- Ensure that the subprogram declaration does not inherit the null
2268 -- indicator from the body as we now have a proper spec/body pair.
2270 Set_Null_Present (Spec, False);
2272 Insert_Before_And_Analyze (Body_Decl,
2273 Make_Subprogram_Declaration (Loc, Specification => Spec));
2274 end Handle_Late_Controlled_Primitive;
2276 --------------------------------
2277 -- Remove_Visible_Refinements --
2278 --------------------------------
2280 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2281 State_Elmt : Elmt_Id;
2282 begin
2283 if Present (Abstract_States (Spec_Id)) then
2284 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2285 while Present (State_Elmt) loop
2286 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2287 Next_Elmt (State_Elmt);
2288 end loop;
2289 end if;
2290 end Remove_Visible_Refinements;
2292 -- Local variables
2294 Context : Node_Id := Empty;
2295 Freeze_From : Entity_Id := Empty;
2296 Next_Decl : Node_Id;
2298 Body_Seen : Boolean := False;
2299 -- Flag set when the first body [stub] is encountered
2301 -- Start of processing for Analyze_Declarations
2303 begin
2304 if Restriction_Check_Required (SPARK_05) then
2305 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2306 end if;
2308 Decl := First (L);
2309 while Present (Decl) loop
2311 -- Package spec cannot contain a package declaration in SPARK
2313 if Nkind (Decl) = N_Package_Declaration
2314 and then Nkind (Parent (L)) = N_Package_Specification
2315 then
2316 Check_SPARK_05_Restriction
2317 ("package specification cannot contain a package declaration",
2318 Decl);
2319 end if;
2321 -- Complete analysis of declaration
2323 Analyze (Decl);
2324 Next_Decl := Next (Decl);
2326 if No (Freeze_From) then
2327 Freeze_From := First_Entity (Current_Scope);
2328 end if;
2330 -- At the end of a declarative part, freeze remaining entities
2331 -- declared in it. The end of the visible declarations of package
2332 -- specification is not the end of a declarative part if private
2333 -- declarations are present. The end of a package declaration is a
2334 -- freezing point only if it a library package. A task definition or
2335 -- protected type definition is not a freeze point either. Finally,
2336 -- we do not freeze entities in generic scopes, because there is no
2337 -- code generated for them and freeze nodes will be generated for
2338 -- the instance.
2340 -- The end of a package instantiation is not a freeze point, but
2341 -- for now we make it one, because the generic body is inserted
2342 -- (currently) immediately after. Generic instantiations will not
2343 -- be a freeze point once delayed freezing of bodies is implemented.
2344 -- (This is needed in any case for early instantiations ???).
2346 if No (Next_Decl) then
2347 if Nkind_In (Parent (L), N_Component_List,
2348 N_Task_Definition,
2349 N_Protected_Definition)
2350 then
2351 null;
2353 elsif Nkind (Parent (L)) /= N_Package_Specification then
2354 if Nkind (Parent (L)) = N_Package_Body then
2355 Freeze_From := First_Entity (Current_Scope);
2356 end if;
2358 -- There may have been several freezing points previously,
2359 -- for example object declarations or subprogram bodies, but
2360 -- at the end of a declarative part we check freezing from
2361 -- the beginning, even though entities may already be frozen,
2362 -- in order to perform visibility checks on delayed aspects.
2364 Adjust_Decl;
2365 Freeze_All (First_Entity (Current_Scope), Decl);
2366 Freeze_From := Last_Entity (Current_Scope);
2368 elsif Scope (Current_Scope) /= Standard_Standard
2369 and then not Is_Child_Unit (Current_Scope)
2370 and then No (Generic_Parent (Parent (L)))
2371 then
2372 null;
2374 elsif L /= Visible_Declarations (Parent (L))
2375 or else No (Private_Declarations (Parent (L)))
2376 or else Is_Empty_List (Private_Declarations (Parent (L)))
2377 then
2378 Adjust_Decl;
2379 Freeze_All (First_Entity (Current_Scope), Decl);
2380 Freeze_From := Last_Entity (Current_Scope);
2382 -- At the end of the visible declarations the expressions in
2383 -- aspects of all entities declared so far must be resolved.
2384 -- The entities themselves might be frozen later, and the
2385 -- generated pragmas and attribute definition clauses analyzed
2386 -- in full at that point, but name resolution must take place
2387 -- now.
2388 -- In addition to being the proper semantics, this is mandatory
2389 -- within generic units, because global name capture requires
2390 -- those expressions to be analyzed, given that the generated
2391 -- pragmas do not appear in the original generic tree.
2393 elsif Serious_Errors_Detected = 0 then
2394 declare
2395 E : Entity_Id;
2397 begin
2398 E := First_Entity (Current_Scope);
2399 while Present (E) loop
2400 Resolve_Aspect_Expressions (E);
2401 Next_Entity (E);
2402 end loop;
2403 end;
2404 end if;
2406 -- If next node is a body then freeze all types before the body.
2407 -- An exception occurs for some expander-generated bodies. If these
2408 -- are generated at places where in general language rules would not
2409 -- allow a freeze point, then we assume that the expander has
2410 -- explicitly checked that all required types are properly frozen,
2411 -- and we do not cause general freezing here. This special circuit
2412 -- is used when the encountered body is marked as having already
2413 -- been analyzed.
2415 -- In all other cases (bodies that come from source, and expander
2416 -- generated bodies that have not been analyzed yet), freeze all
2417 -- types now. Note that in the latter case, the expander must take
2418 -- care to attach the bodies at a proper place in the tree so as to
2419 -- not cause unwanted freezing at that point.
2421 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2423 -- When a controlled type is frozen, the expander generates stream
2424 -- and controlled type support routines. If the freeze is caused
2425 -- by the stand alone body of Initialize, Adjust and Finalize, the
2426 -- expander will end up using the wrong version of these routines
2427 -- as the body has not been processed yet. To remedy this, detect
2428 -- a late controlled primitive and create a proper spec for it.
2429 -- This ensures that the primitive will override its inherited
2430 -- counterpart before the freeze takes place.
2432 -- If the declaration we just processed is a body, do not attempt
2433 -- to examine Next_Decl as the late primitive idiom can only apply
2434 -- to the first encountered body.
2436 -- The spec of the late primitive is not generated in ASIS mode to
2437 -- ensure a consistent list of primitives that indicates the true
2438 -- semantic structure of the program (which is not relevant when
2439 -- generating executable code.
2441 -- ??? a cleaner approach may be possible and/or this solution
2442 -- could be extended to general-purpose late primitives, TBD.
2444 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2445 then
2446 Body_Seen := True;
2448 if Nkind (Next_Decl) = N_Subprogram_Body then
2449 Handle_Late_Controlled_Primitive (Next_Decl);
2450 end if;
2451 end if;
2453 Adjust_Decl;
2454 Freeze_All (Freeze_From, Decl);
2455 Freeze_From := Last_Entity (Current_Scope);
2456 end if;
2458 Decl := Next_Decl;
2459 end loop;
2461 -- Analyze the contracts of packages and their bodies
2463 if Present (L) then
2464 Context := Parent (L);
2466 if Nkind (Context) = N_Package_Specification then
2468 -- When a package has private declarations, its contract must be
2469 -- analyzed at the end of the said declarations. This way both the
2470 -- analysis and freeze actions are properly synchronized in case
2471 -- of private type use within the contract.
2473 if L = Private_Declarations (Context) then
2474 Analyze_Package_Contract (Defining_Entity (Context));
2476 -- Build the bodies of the default initial condition procedures
2477 -- for all types subject to pragma Default_Initial_Condition.
2478 -- From a purely Ada stand point, this is a freezing activity,
2479 -- however freezing is not available under GNATprove_Mode. To
2480 -- accomodate both scenarios, the bodies are build at the end
2481 -- of private declaration analysis.
2483 Build_Default_Init_Cond_Procedure_Bodies (L);
2485 -- Otherwise the contract is analyzed at the end of the visible
2486 -- declarations.
2488 elsif L = Visible_Declarations (Context)
2489 and then No (Private_Declarations (Context))
2490 then
2491 Analyze_Package_Contract (Defining_Entity (Context));
2492 end if;
2494 elsif Nkind (Context) = N_Package_Body then
2495 Analyze_Package_Body_Contract (Defining_Entity (Context));
2496 end if;
2498 -- Analyze the contracts of various constructs now due to the delayed
2499 -- visibility needs of their aspects and pragmas.
2501 Analyze_Contracts (L);
2503 if Nkind (Context) = N_Package_Body then
2505 -- Ensure that all abstract states and objects declared in the
2506 -- state space of a package body are utilized as constituents.
2508 Check_Unused_Body_States (Defining_Entity (Context));
2510 -- State refinements are visible up to the end of the package body
2511 -- declarations. Hide the state refinements from visibility to
2512 -- restore the original state conditions.
2514 Remove_Visible_Refinements (Corresponding_Spec (Context));
2515 end if;
2517 -- Verify that all abstract states found in any package declared in
2518 -- the input declarative list have proper refinements. The check is
2519 -- performed only when the context denotes a block, entry, package,
2520 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2522 Check_State_Refinements (Context);
2523 end if;
2524 end Analyze_Declarations;
2526 -----------------------------------
2527 -- Analyze_Full_Type_Declaration --
2528 -----------------------------------
2530 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2531 Def : constant Node_Id := Type_Definition (N);
2532 Def_Id : constant Entity_Id := Defining_Identifier (N);
2533 T : Entity_Id;
2534 Prev : Entity_Id;
2536 Is_Remote : constant Boolean :=
2537 (Is_Remote_Types (Current_Scope)
2538 or else Is_Remote_Call_Interface (Current_Scope))
2539 and then not (In_Private_Part (Current_Scope)
2540 or else In_Package_Body (Current_Scope));
2542 procedure Check_Nonoverridable_Aspects;
2543 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2544 -- be overridden, and can only be confirmed on derivation.
2546 procedure Check_Ops_From_Incomplete_Type;
2547 -- If there is a tagged incomplete partial view of the type, traverse
2548 -- the primitives of the incomplete view and change the type of any
2549 -- controlling formals and result to indicate the full view. The
2550 -- primitives will be added to the full type's primitive operations
2551 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2552 -- is called from Process_Incomplete_Dependents).
2554 ----------------------------------
2555 -- Check_Nonoverridable_Aspects --
2556 ----------------------------------
2558 procedure Check_Nonoverridable_Aspects is
2559 Prev_Aspects : constant List_Id :=
2560 Aspect_Specifications (Parent (Def_Id));
2561 Par_Type : Entity_Id;
2563 function Has_Aspect_Spec
2564 (Specs : List_Id;
2565 Aspect_Name : Name_Id) return Boolean;
2566 -- Check whether a list of aspect specifications includes an entry
2567 -- for a specific aspect. The list is either that of a partial or
2568 -- a full view.
2570 ---------------------
2571 -- Has_Aspect_Spec --
2572 ---------------------
2574 function Has_Aspect_Spec
2575 (Specs : List_Id;
2576 Aspect_Name : Name_Id) return Boolean
2578 Spec : Node_Id;
2579 begin
2580 Spec := First (Specs);
2581 while Present (Spec) loop
2582 if Chars (Identifier (Spec)) = Aspect_Name then
2583 return True;
2584 end if;
2585 Next (Spec);
2586 end loop;
2587 return False;
2588 end Has_Aspect_Spec;
2590 -- Start of processing for Check_Nonoverridable_Aspects
2592 begin
2594 -- Get parent type of derived type. Note that Prev is the entity
2595 -- in the partial declaration, but its contents are now those of
2596 -- full view, while Def_Id reflects the partial view.
2598 if Is_Private_Type (Def_Id) then
2599 Par_Type := Etype (Full_View (Def_Id));
2600 else
2601 Par_Type := Etype (Def_Id);
2602 end if;
2604 -- If there is an inherited Implicit_Dereference, verify that it is
2605 -- made explicit in the partial view.
2607 if Has_Discriminants (Base_Type (Par_Type))
2608 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2609 and then Present (Discriminant_Specifications (Parent (Prev)))
2610 and then Present (Get_Reference_Discriminant (Par_Type))
2611 then
2613 not Has_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference)
2614 then
2615 Error_Msg_N
2616 ("type does not inherit implicit dereference", Prev);
2618 else
2619 -- If one of the views has the aspect specified, verify that it
2620 -- is consistent with that of the parent.
2622 declare
2623 Par_Discr : constant Entity_Id :=
2624 Get_Reference_Discriminant (Par_Type);
2625 Cur_Discr : constant Entity_Id :=
2626 Get_Reference_Discriminant (Prev);
2627 begin
2628 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2629 Error_Msg_N ("aspect incosistent with that of parent", N);
2630 end if;
2631 end;
2632 end if;
2633 end if;
2635 -- TBD : other nonoverridable aspects.
2636 end Check_Nonoverridable_Aspects;
2638 ------------------------------------
2639 -- Check_Ops_From_Incomplete_Type --
2640 ------------------------------------
2642 procedure Check_Ops_From_Incomplete_Type is
2643 Elmt : Elmt_Id;
2644 Formal : Entity_Id;
2645 Op : Entity_Id;
2647 begin
2648 if Prev /= T
2649 and then Ekind (Prev) = E_Incomplete_Type
2650 and then Is_Tagged_Type (Prev)
2651 and then Is_Tagged_Type (T)
2652 then
2653 Elmt := First_Elmt (Primitive_Operations (Prev));
2654 while Present (Elmt) loop
2655 Op := Node (Elmt);
2657 Formal := First_Formal (Op);
2658 while Present (Formal) loop
2659 if Etype (Formal) = Prev then
2660 Set_Etype (Formal, T);
2661 end if;
2663 Next_Formal (Formal);
2664 end loop;
2666 if Etype (Op) = Prev then
2667 Set_Etype (Op, T);
2668 end if;
2670 Next_Elmt (Elmt);
2671 end loop;
2672 end if;
2673 end Check_Ops_From_Incomplete_Type;
2675 -- Start of processing for Analyze_Full_Type_Declaration
2677 begin
2678 Prev := Find_Type_Name (N);
2680 -- The full view, if present, now points to the current type. If there
2681 -- is an incomplete partial view, set a link to it, to simplify the
2682 -- retrieval of primitive operations of the type.
2684 -- Ada 2005 (AI-50217): If the type was previously decorated when
2685 -- imported through a LIMITED WITH clause, it appears as incomplete
2686 -- but has no full view.
2688 if Ekind (Prev) = E_Incomplete_Type
2689 and then Present (Full_View (Prev))
2690 then
2691 T := Full_View (Prev);
2692 Set_Incomplete_View (N, Parent (Prev));
2693 else
2694 T := Prev;
2695 end if;
2697 Set_Is_Pure (T, Is_Pure (Current_Scope));
2699 -- We set the flag Is_First_Subtype here. It is needed to set the
2700 -- corresponding flag for the Implicit class-wide-type created
2701 -- during tagged types processing.
2703 Set_Is_First_Subtype (T, True);
2705 -- Only composite types other than array types are allowed to have
2706 -- discriminants.
2708 case Nkind (Def) is
2710 -- For derived types, the rule will be checked once we've figured
2711 -- out the parent type.
2713 when N_Derived_Type_Definition =>
2714 null;
2716 -- For record types, discriminants are allowed, unless we are in
2717 -- SPARK.
2719 when N_Record_Definition =>
2720 if Present (Discriminant_Specifications (N)) then
2721 Check_SPARK_05_Restriction
2722 ("discriminant type is not allowed",
2723 Defining_Identifier
2724 (First (Discriminant_Specifications (N))));
2725 end if;
2727 when others =>
2728 if Present (Discriminant_Specifications (N)) then
2729 Error_Msg_N
2730 ("elementary or array type cannot have discriminants",
2731 Defining_Identifier
2732 (First (Discriminant_Specifications (N))));
2733 end if;
2734 end case;
2736 -- Elaborate the type definition according to kind, and generate
2737 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2738 -- already done (this happens during the reanalysis that follows a call
2739 -- to the high level optimizer).
2741 if not Analyzed (T) then
2742 Set_Analyzed (T);
2744 case Nkind (Def) is
2745 when N_Access_To_Subprogram_Definition =>
2746 Access_Subprogram_Declaration (T, Def);
2748 -- If this is a remote access to subprogram, we must create the
2749 -- equivalent fat pointer type, and related subprograms.
2751 if Is_Remote then
2752 Process_Remote_AST_Declaration (N);
2753 end if;
2755 -- Validate categorization rule against access type declaration
2756 -- usually a violation in Pure unit, Shared_Passive unit.
2758 Validate_Access_Type_Declaration (T, N);
2760 when N_Access_To_Object_Definition =>
2761 Access_Type_Declaration (T, Def);
2763 -- Validate categorization rule against access type declaration
2764 -- usually a violation in Pure unit, Shared_Passive unit.
2766 Validate_Access_Type_Declaration (T, N);
2768 -- If we are in a Remote_Call_Interface package and define a
2769 -- RACW, then calling stubs and specific stream attributes
2770 -- must be added.
2772 if Is_Remote
2773 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2774 then
2775 Add_RACW_Features (Def_Id);
2776 end if;
2778 when N_Array_Type_Definition =>
2779 Array_Type_Declaration (T, Def);
2781 when N_Derived_Type_Definition =>
2782 Derived_Type_Declaration (T, N, T /= Def_Id);
2784 when N_Enumeration_Type_Definition =>
2785 Enumeration_Type_Declaration (T, Def);
2787 when N_Floating_Point_Definition =>
2788 Floating_Point_Type_Declaration (T, Def);
2790 when N_Decimal_Fixed_Point_Definition =>
2791 Decimal_Fixed_Point_Type_Declaration (T, Def);
2793 when N_Ordinary_Fixed_Point_Definition =>
2794 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2796 when N_Signed_Integer_Type_Definition =>
2797 Signed_Integer_Type_Declaration (T, Def);
2799 when N_Modular_Type_Definition =>
2800 Modular_Type_Declaration (T, Def);
2802 when N_Record_Definition =>
2803 Record_Type_Declaration (T, N, Prev);
2805 -- If declaration has a parse error, nothing to elaborate.
2807 when N_Error =>
2808 null;
2810 when others =>
2811 raise Program_Error;
2813 end case;
2814 end if;
2816 if Etype (T) = Any_Type then
2817 return;
2818 end if;
2820 -- Controlled type is not allowed in SPARK
2822 if Is_Visibly_Controlled (T) then
2823 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2824 end if;
2826 -- A type declared within a Ghost region is automatically Ghost
2827 -- (SPARK RM 6.9(2)).
2829 if Ghost_Mode > None then
2830 Set_Is_Ghost_Entity (T);
2831 end if;
2833 -- Some common processing for all types
2835 Set_Depends_On_Private (T, Has_Private_Component (T));
2836 Check_Ops_From_Incomplete_Type;
2838 -- Both the declared entity, and its anonymous base type if one was
2839 -- created, need freeze nodes allocated.
2841 declare
2842 B : constant Entity_Id := Base_Type (T);
2844 begin
2845 -- In the case where the base type differs from the first subtype, we
2846 -- pre-allocate a freeze node, and set the proper link to the first
2847 -- subtype. Freeze_Entity will use this preallocated freeze node when
2848 -- it freezes the entity.
2850 -- This does not apply if the base type is a generic type, whose
2851 -- declaration is independent of the current derived definition.
2853 if B /= T and then not Is_Generic_Type (B) then
2854 Ensure_Freeze_Node (B);
2855 Set_First_Subtype_Link (Freeze_Node (B), T);
2856 end if;
2858 -- A type that is imported through a limited_with clause cannot
2859 -- generate any code, and thus need not be frozen. However, an access
2860 -- type with an imported designated type needs a finalization list,
2861 -- which may be referenced in some other package that has non-limited
2862 -- visibility on the designated type. Thus we must create the
2863 -- finalization list at the point the access type is frozen, to
2864 -- prevent unsatisfied references at link time.
2866 if not From_Limited_With (T) or else Is_Access_Type (T) then
2867 Set_Has_Delayed_Freeze (T);
2868 end if;
2869 end;
2871 -- Case where T is the full declaration of some private type which has
2872 -- been swapped in Defining_Identifier (N).
2874 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2875 Process_Full_View (N, T, Def_Id);
2877 -- Record the reference. The form of this is a little strange, since
2878 -- the full declaration has been swapped in. So the first parameter
2879 -- here represents the entity to which a reference is made which is
2880 -- the "real" entity, i.e. the one swapped in, and the second
2881 -- parameter provides the reference location.
2883 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2884 -- since we don't want a complaint about the full type being an
2885 -- unwanted reference to the private type
2887 declare
2888 B : constant Boolean := Has_Pragma_Unreferenced (T);
2889 begin
2890 Set_Has_Pragma_Unreferenced (T, False);
2891 Generate_Reference (T, T, 'c');
2892 Set_Has_Pragma_Unreferenced (T, B);
2893 end;
2895 Set_Completion_Referenced (Def_Id);
2897 -- For completion of incomplete type, process incomplete dependents
2898 -- and always mark the full type as referenced (it is the incomplete
2899 -- type that we get for any real reference).
2901 elsif Ekind (Prev) = E_Incomplete_Type then
2902 Process_Incomplete_Dependents (N, T, Prev);
2903 Generate_Reference (Prev, Def_Id, 'c');
2904 Set_Completion_Referenced (Def_Id);
2906 -- If not private type or incomplete type completion, this is a real
2907 -- definition of a new entity, so record it.
2909 else
2910 Generate_Definition (Def_Id);
2911 end if;
2913 -- Propagate any pending access types whose finalization masters need to
2914 -- be fully initialized from the partial to the full view. Guard against
2915 -- an illegal full view that remains unanalyzed.
2917 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2918 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2919 end if;
2921 if Chars (Scope (Def_Id)) = Name_System
2922 and then Chars (Def_Id) = Name_Address
2923 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2924 then
2925 Set_Is_Descendant_Of_Address (Def_Id);
2926 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
2927 Set_Is_Descendant_Of_Address (Prev);
2928 end if;
2930 Set_Optimize_Alignment_Flags (Def_Id);
2931 Check_Eliminated (Def_Id);
2933 -- If the declaration is a completion and aspects are present, apply
2934 -- them to the entity for the type which is currently the partial
2935 -- view, but which is the one that will be frozen.
2937 if Has_Aspects (N) then
2939 -- In most cases the partial view is a private type, and both views
2940 -- appear in different declarative parts. In the unusual case where
2941 -- the partial view is incomplete, perform the analysis on the
2942 -- full view, to prevent freezing anomalies with the corresponding
2943 -- class-wide type, which otherwise might be frozen before the
2944 -- dispatch table is built.
2946 if Prev /= Def_Id
2947 and then Ekind (Prev) /= E_Incomplete_Type
2948 then
2949 Analyze_Aspect_Specifications (N, Prev);
2951 -- Normal case
2953 else
2954 Analyze_Aspect_Specifications (N, Def_Id);
2955 end if;
2956 end if;
2958 if Is_Derived_Type (Prev)
2959 and then Def_Id /= Prev
2960 then
2961 Check_Nonoverridable_Aspects;
2962 end if;
2963 end Analyze_Full_Type_Declaration;
2965 ----------------------------------
2966 -- Analyze_Incomplete_Type_Decl --
2967 ----------------------------------
2969 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2970 F : constant Boolean := Is_Pure (Current_Scope);
2971 T : Entity_Id;
2973 begin
2974 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2976 Generate_Definition (Defining_Identifier (N));
2978 -- Process an incomplete declaration. The identifier must not have been
2979 -- declared already in the scope. However, an incomplete declaration may
2980 -- appear in the private part of a package, for a private type that has
2981 -- already been declared.
2983 -- In this case, the discriminants (if any) must match
2985 T := Find_Type_Name (N);
2987 Set_Ekind (T, E_Incomplete_Type);
2988 Init_Size_Align (T);
2989 Set_Is_First_Subtype (T, True);
2990 Set_Etype (T, T);
2992 -- An incomplete type declared within a Ghost region is automatically
2993 -- Ghost (SPARK RM 6.9(2)).
2995 if Ghost_Mode > None then
2996 Set_Is_Ghost_Entity (T);
2997 end if;
2999 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3000 -- incomplete types.
3002 if Tagged_Present (N) then
3003 Set_Is_Tagged_Type (T, True);
3004 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3005 Make_Class_Wide_Type (T);
3006 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3007 end if;
3009 Set_Stored_Constraint (T, No_Elist);
3011 if Present (Discriminant_Specifications (N)) then
3012 Push_Scope (T);
3013 Process_Discriminants (N);
3014 End_Scope;
3015 end if;
3017 -- If the type has discriminants, nontrivial subtypes may be declared
3018 -- before the full view of the type. The full views of those subtypes
3019 -- will be built after the full view of the type.
3021 Set_Private_Dependents (T, New_Elmt_List);
3022 Set_Is_Pure (T, F);
3023 end Analyze_Incomplete_Type_Decl;
3025 -----------------------------------
3026 -- Analyze_Interface_Declaration --
3027 -----------------------------------
3029 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3030 CW : constant Entity_Id := Class_Wide_Type (T);
3032 begin
3033 Set_Is_Tagged_Type (T);
3034 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3036 Set_Is_Limited_Record (T, Limited_Present (Def)
3037 or else Task_Present (Def)
3038 or else Protected_Present (Def)
3039 or else Synchronized_Present (Def));
3041 -- Type is abstract if full declaration carries keyword, or if previous
3042 -- partial view did.
3044 Set_Is_Abstract_Type (T);
3045 Set_Is_Interface (T);
3047 -- Type is a limited interface if it includes the keyword limited, task,
3048 -- protected, or synchronized.
3050 Set_Is_Limited_Interface
3051 (T, Limited_Present (Def)
3052 or else Protected_Present (Def)
3053 or else Synchronized_Present (Def)
3054 or else Task_Present (Def));
3056 Set_Interfaces (T, New_Elmt_List);
3057 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3059 -- Complete the decoration of the class-wide entity if it was already
3060 -- built (i.e. during the creation of the limited view)
3062 if Present (CW) then
3063 Set_Is_Interface (CW);
3064 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3065 end if;
3067 -- Check runtime support for synchronized interfaces
3069 if (Is_Task_Interface (T)
3070 or else Is_Protected_Interface (T)
3071 or else Is_Synchronized_Interface (T))
3072 and then not RTE_Available (RE_Select_Specific_Data)
3073 then
3074 Error_Msg_CRT ("synchronized interfaces", T);
3075 end if;
3076 end Analyze_Interface_Declaration;
3078 -----------------------------
3079 -- Analyze_Itype_Reference --
3080 -----------------------------
3082 -- Nothing to do. This node is placed in the tree only for the benefit of
3083 -- back end processing, and has no effect on the semantic processing.
3085 procedure Analyze_Itype_Reference (N : Node_Id) is
3086 begin
3087 pragma Assert (Is_Itype (Itype (N)));
3088 null;
3089 end Analyze_Itype_Reference;
3091 --------------------------------
3092 -- Analyze_Number_Declaration --
3093 --------------------------------
3095 procedure Analyze_Number_Declaration (N : Node_Id) is
3096 E : constant Node_Id := Expression (N);
3097 Id : constant Entity_Id := Defining_Identifier (N);
3098 Index : Interp_Index;
3099 It : Interp;
3100 T : Entity_Id;
3102 begin
3103 Generate_Definition (Id);
3104 Enter_Name (Id);
3106 -- A number declared within a Ghost region is automatically Ghost
3107 -- (SPARK RM 6.9(2)).
3109 if Ghost_Mode > None then
3110 Set_Is_Ghost_Entity (Id);
3111 end if;
3113 -- This is an optimization of a common case of an integer literal
3115 if Nkind (E) = N_Integer_Literal then
3116 Set_Is_Static_Expression (E, True);
3117 Set_Etype (E, Universal_Integer);
3119 Set_Etype (Id, Universal_Integer);
3120 Set_Ekind (Id, E_Named_Integer);
3121 Set_Is_Frozen (Id, True);
3122 return;
3123 end if;
3125 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3127 -- Process expression, replacing error by integer zero, to avoid
3128 -- cascaded errors or aborts further along in the processing
3130 -- Replace Error by integer zero, which seems least likely to cause
3131 -- cascaded errors.
3133 if E = Error then
3134 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3135 Set_Error_Posted (E);
3136 end if;
3138 Analyze (E);
3140 -- Verify that the expression is static and numeric. If
3141 -- the expression is overloaded, we apply the preference
3142 -- rule that favors root numeric types.
3144 if not Is_Overloaded (E) then
3145 T := Etype (E);
3146 if Has_Dynamic_Predicate_Aspect (T) then
3147 Error_Msg_N
3148 ("subtype has dynamic predicate, "
3149 & "not allowed in number declaration", N);
3150 end if;
3152 else
3153 T := Any_Type;
3155 Get_First_Interp (E, Index, It);
3156 while Present (It.Typ) loop
3157 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3158 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3159 then
3160 if T = Any_Type then
3161 T := It.Typ;
3163 elsif It.Typ = Universal_Real
3164 or else
3165 It.Typ = Universal_Integer
3166 then
3167 -- Choose universal interpretation over any other
3169 T := It.Typ;
3170 exit;
3171 end if;
3172 end if;
3174 Get_Next_Interp (Index, It);
3175 end loop;
3176 end if;
3178 if Is_Integer_Type (T) then
3179 Resolve (E, T);
3180 Set_Etype (Id, Universal_Integer);
3181 Set_Ekind (Id, E_Named_Integer);
3183 elsif Is_Real_Type (T) then
3185 -- Because the real value is converted to universal_real, this is a
3186 -- legal context for a universal fixed expression.
3188 if T = Universal_Fixed then
3189 declare
3190 Loc : constant Source_Ptr := Sloc (N);
3191 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3192 Subtype_Mark =>
3193 New_Occurrence_Of (Universal_Real, Loc),
3194 Expression => Relocate_Node (E));
3196 begin
3197 Rewrite (E, Conv);
3198 Analyze (E);
3199 end;
3201 elsif T = Any_Fixed then
3202 Error_Msg_N ("illegal context for mixed mode operation", E);
3204 -- Expression is of the form : universal_fixed * integer. Try to
3205 -- resolve as universal_real.
3207 T := Universal_Real;
3208 Set_Etype (E, T);
3209 end if;
3211 Resolve (E, T);
3212 Set_Etype (Id, Universal_Real);
3213 Set_Ekind (Id, E_Named_Real);
3215 else
3216 Wrong_Type (E, Any_Numeric);
3217 Resolve (E, T);
3219 Set_Etype (Id, T);
3220 Set_Ekind (Id, E_Constant);
3221 Set_Never_Set_In_Source (Id, True);
3222 Set_Is_True_Constant (Id, True);
3223 return;
3224 end if;
3226 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3227 Set_Etype (E, Etype (Id));
3228 end if;
3230 if not Is_OK_Static_Expression (E) then
3231 Flag_Non_Static_Expr
3232 ("non-static expression used in number declaration!", E);
3233 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3234 Set_Etype (E, Any_Type);
3235 end if;
3237 Analyze_Dimension (N);
3238 end Analyze_Number_Declaration;
3240 --------------------------------
3241 -- Analyze_Object_Declaration --
3242 --------------------------------
3244 procedure Analyze_Object_Declaration (N : Node_Id) is
3245 Loc : constant Source_Ptr := Sloc (N);
3246 Id : constant Entity_Id := Defining_Identifier (N);
3247 Act_T : Entity_Id;
3248 T : Entity_Id;
3250 E : Node_Id := Expression (N);
3251 -- E is set to Expression (N) throughout this routine. When
3252 -- Expression (N) is modified, E is changed accordingly.
3254 Prev_Entity : Entity_Id := Empty;
3256 function Count_Tasks (T : Entity_Id) return Uint;
3257 -- This function is called when a non-generic library level object of a
3258 -- task type is declared. Its function is to count the static number of
3259 -- tasks declared within the type (it is only called if Has_Task is set
3260 -- for T). As a side effect, if an array of tasks with non-static bounds
3261 -- or a variant record type is encountered, Check_Restriction is called
3262 -- indicating the count is unknown.
3264 function Delayed_Aspect_Present return Boolean;
3265 -- If the declaration has an expression that is an aggregate, and it
3266 -- has aspects that require delayed analysis, the resolution of the
3267 -- aggregate must be deferred to the freeze point of the objet. This
3268 -- special processing was created for address clauses, but it must
3269 -- also apply to Alignment. This must be done before the aspect
3270 -- specifications are analyzed because we must handle the aggregate
3271 -- before the analysis of the object declaration is complete.
3273 -- Any other relevant delayed aspects on object declarations ???
3275 -----------------
3276 -- Count_Tasks --
3277 -----------------
3279 function Count_Tasks (T : Entity_Id) return Uint is
3280 C : Entity_Id;
3281 X : Node_Id;
3282 V : Uint;
3284 begin
3285 if Is_Task_Type (T) then
3286 return Uint_1;
3288 elsif Is_Record_Type (T) then
3289 if Has_Discriminants (T) then
3290 Check_Restriction (Max_Tasks, N);
3291 return Uint_0;
3293 else
3294 V := Uint_0;
3295 C := First_Component (T);
3296 while Present (C) loop
3297 V := V + Count_Tasks (Etype (C));
3298 Next_Component (C);
3299 end loop;
3301 return V;
3302 end if;
3304 elsif Is_Array_Type (T) then
3305 X := First_Index (T);
3306 V := Count_Tasks (Component_Type (T));
3307 while Present (X) loop
3308 C := Etype (X);
3310 if not Is_OK_Static_Subtype (C) then
3311 Check_Restriction (Max_Tasks, N);
3312 return Uint_0;
3313 else
3314 V := V * (UI_Max (Uint_0,
3315 Expr_Value (Type_High_Bound (C)) -
3316 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3317 end if;
3319 Next_Index (X);
3320 end loop;
3322 return V;
3324 else
3325 return Uint_0;
3326 end if;
3327 end Count_Tasks;
3329 ----------------------------
3330 -- Delayed_Aspect_Present --
3331 ----------------------------
3333 function Delayed_Aspect_Present return Boolean is
3334 A : Node_Id;
3335 A_Id : Aspect_Id;
3337 begin
3338 if Present (Aspect_Specifications (N)) then
3339 A := First (Aspect_Specifications (N));
3340 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3341 while Present (A) loop
3342 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3343 return True;
3344 end if;
3346 Next (A);
3347 end loop;
3348 end if;
3350 return False;
3351 end Delayed_Aspect_Present;
3353 -- Local variables
3355 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
3356 Related_Id : Entity_Id;
3358 -- Start of processing for Analyze_Object_Declaration
3360 begin
3361 -- There are three kinds of implicit types generated by an
3362 -- object declaration:
3364 -- 1. Those generated by the original Object Definition
3366 -- 2. Those generated by the Expression
3368 -- 3. Those used to constrain the Object Definition with the
3369 -- expression constraints when the definition is unconstrained.
3371 -- They must be generated in this order to avoid order of elaboration
3372 -- issues. Thus the first step (after entering the name) is to analyze
3373 -- the object definition.
3375 if Constant_Present (N) then
3376 Prev_Entity := Current_Entity_In_Scope (Id);
3378 if Present (Prev_Entity)
3379 and then
3380 -- If the homograph is an implicit subprogram, it is overridden
3381 -- by the current declaration.
3383 ((Is_Overloadable (Prev_Entity)
3384 and then Is_Inherited_Operation (Prev_Entity))
3386 -- The current object is a discriminal generated for an entry
3387 -- family index. Even though the index is a constant, in this
3388 -- particular context there is no true constant redeclaration.
3389 -- Enter_Name will handle the visibility.
3391 or else
3392 (Is_Discriminal (Id)
3393 and then Ekind (Discriminal_Link (Id)) =
3394 E_Entry_Index_Parameter)
3396 -- The current object is the renaming for a generic declared
3397 -- within the instance.
3399 or else
3400 (Ekind (Prev_Entity) = E_Package
3401 and then Nkind (Parent (Prev_Entity)) =
3402 N_Package_Renaming_Declaration
3403 and then not Comes_From_Source (Prev_Entity)
3404 and then
3405 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3406 then
3407 Prev_Entity := Empty;
3408 end if;
3409 end if;
3411 -- The object declaration is Ghost when it is subject to pragma Ghost or
3412 -- completes a deferred Ghost constant. Set the mode now to ensure that
3413 -- any nodes generated during analysis and expansion are properly marked
3414 -- as Ghost.
3416 Set_Ghost_Mode (N, Prev_Entity);
3418 if Present (Prev_Entity) then
3419 Constant_Redeclaration (Id, N, T);
3421 Generate_Reference (Prev_Entity, Id, 'c');
3422 Set_Completion_Referenced (Id);
3424 if Error_Posted (N) then
3426 -- Type mismatch or illegal redeclaration; do not analyze
3427 -- expression to avoid cascaded errors.
3429 T := Find_Type_Of_Object (Object_Definition (N), N);
3430 Set_Etype (Id, T);
3431 Set_Ekind (Id, E_Variable);
3432 goto Leave;
3433 end if;
3435 -- In the normal case, enter identifier at the start to catch premature
3436 -- usage in the initialization expression.
3438 else
3439 Generate_Definition (Id);
3440 Enter_Name (Id);
3442 Mark_Coextensions (N, Object_Definition (N));
3444 T := Find_Type_Of_Object (Object_Definition (N), N);
3446 if Nkind (Object_Definition (N)) = N_Access_Definition
3447 and then Present
3448 (Access_To_Subprogram_Definition (Object_Definition (N)))
3449 and then Protected_Present
3450 (Access_To_Subprogram_Definition (Object_Definition (N)))
3451 then
3452 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3453 end if;
3455 if Error_Posted (Id) then
3456 Set_Etype (Id, T);
3457 Set_Ekind (Id, E_Variable);
3458 goto Leave;
3459 end if;
3460 end if;
3462 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3463 -- out some static checks.
3465 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3467 -- In case of aggregates we must also take care of the correct
3468 -- initialization of nested aggregates bug this is done at the
3469 -- point of the analysis of the aggregate (see sem_aggr.adb).
3471 if Present (Expression (N))
3472 and then Nkind (Expression (N)) = N_Aggregate
3473 then
3474 null;
3476 else
3477 declare
3478 Save_Typ : constant Entity_Id := Etype (Id);
3479 begin
3480 Set_Etype (Id, T); -- Temp. decoration for static checks
3481 Null_Exclusion_Static_Checks (N);
3482 Set_Etype (Id, Save_Typ);
3483 end;
3484 end if;
3485 end if;
3487 -- Object is marked pure if it is in a pure scope
3489 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3491 -- If deferred constant, make sure context is appropriate. We detect
3492 -- a deferred constant as a constant declaration with no expression.
3493 -- A deferred constant can appear in a package body if its completion
3494 -- is by means of an interface pragma.
3496 if Constant_Present (N) and then No (E) then
3498 -- A deferred constant may appear in the declarative part of the
3499 -- following constructs:
3501 -- blocks
3502 -- entry bodies
3503 -- extended return statements
3504 -- package specs
3505 -- package bodies
3506 -- subprogram bodies
3507 -- task bodies
3509 -- When declared inside a package spec, a deferred constant must be
3510 -- completed by a full constant declaration or pragma Import. In all
3511 -- other cases, the only proper completion is pragma Import. Extended
3512 -- return statements are flagged as invalid contexts because they do
3513 -- not have a declarative part and so cannot accommodate the pragma.
3515 if Ekind (Current_Scope) = E_Return_Statement then
3516 Error_Msg_N
3517 ("invalid context for deferred constant declaration (RM 7.4)",
3519 Error_Msg_N
3520 ("\declaration requires an initialization expression",
3522 Set_Constant_Present (N, False);
3524 -- In Ada 83, deferred constant must be of private type
3526 elsif not Is_Private_Type (T) then
3527 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3528 Error_Msg_N
3529 ("(Ada 83) deferred constant must be private type", N);
3530 end if;
3531 end if;
3533 -- If not a deferred constant, then the object declaration freezes
3534 -- its type, unless the object is of an anonymous type and has delayed
3535 -- aspects. In that case the type is frozen when the object itself is.
3537 else
3538 Check_Fully_Declared (T, N);
3540 if Has_Delayed_Aspects (Id)
3541 and then Is_Array_Type (T)
3542 and then Is_Itype (T)
3543 then
3544 Set_Has_Delayed_Freeze (T);
3545 else
3546 Freeze_Before (N, T);
3547 end if;
3548 end if;
3550 -- If the object was created by a constrained array definition, then
3551 -- set the link in both the anonymous base type and anonymous subtype
3552 -- that are built to represent the array type to point to the object.
3554 if Nkind (Object_Definition (Declaration_Node (Id))) =
3555 N_Constrained_Array_Definition
3556 then
3557 Set_Related_Array_Object (T, Id);
3558 Set_Related_Array_Object (Base_Type (T), Id);
3559 end if;
3561 -- Special checks for protected objects not at library level
3563 if Is_Protected_Type (T)
3564 and then not Is_Library_Level_Entity (Id)
3565 then
3566 Check_Restriction (No_Local_Protected_Objects, Id);
3568 -- Protected objects with interrupt handlers must be at library level
3570 -- Ada 2005: This test is not needed (and the corresponding clause
3571 -- in the RM is removed) because accessibility checks are sufficient
3572 -- to make handlers not at the library level illegal.
3574 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3575 -- applies to the '95 version of the language as well.
3577 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3578 Error_Msg_N
3579 ("interrupt object can only be declared at library level", Id);
3580 end if;
3581 end if;
3583 -- The actual subtype of the object is the nominal subtype, unless
3584 -- the nominal one is unconstrained and obtained from the expression.
3586 Act_T := T;
3588 -- These checks should be performed before the initialization expression
3589 -- is considered, so that the Object_Definition node is still the same
3590 -- as in source code.
3592 -- In SPARK, the nominal subtype is always given by a subtype mark
3593 -- and must not be unconstrained. (The only exception to this is the
3594 -- acceptance of declarations of constants of type String.)
3596 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3597 then
3598 Check_SPARK_05_Restriction
3599 ("subtype mark required", Object_Definition (N));
3601 elsif Is_Array_Type (T)
3602 and then not Is_Constrained (T)
3603 and then T /= Standard_String
3604 then
3605 Check_SPARK_05_Restriction
3606 ("subtype mark of constrained type expected",
3607 Object_Definition (N));
3608 end if;
3610 -- There are no aliased objects in SPARK
3612 if Aliased_Present (N) then
3613 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3614 end if;
3616 -- Process initialization expression if present and not in error
3618 if Present (E) and then E /= Error then
3620 -- Generate an error in case of CPP class-wide object initialization.
3621 -- Required because otherwise the expansion of the class-wide
3622 -- assignment would try to use 'size to initialize the object
3623 -- (primitive that is not available in CPP tagged types).
3625 if Is_Class_Wide_Type (Act_T)
3626 and then
3627 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3628 or else
3629 (Present (Full_View (Root_Type (Etype (Act_T))))
3630 and then
3631 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3632 then
3633 Error_Msg_N
3634 ("predefined assignment not available for 'C'P'P tagged types",
3636 end if;
3638 Mark_Coextensions (N, E);
3639 Analyze (E);
3641 -- In case of errors detected in the analysis of the expression,
3642 -- decorate it with the expected type to avoid cascaded errors
3644 if No (Etype (E)) then
3645 Set_Etype (E, T);
3646 end if;
3648 -- If an initialization expression is present, then we set the
3649 -- Is_True_Constant flag. It will be reset if this is a variable
3650 -- and it is indeed modified.
3652 Set_Is_True_Constant (Id, True);
3654 -- If we are analyzing a constant declaration, set its completion
3655 -- flag after analyzing and resolving the expression.
3657 if Constant_Present (N) then
3658 Set_Has_Completion (Id);
3659 end if;
3661 -- Set type and resolve (type may be overridden later on). Note:
3662 -- Ekind (Id) must still be E_Void at this point so that incorrect
3663 -- early usage within E is properly diagnosed.
3665 Set_Etype (Id, T);
3667 -- If the expression is an aggregate we must look ahead to detect
3668 -- the possible presence of an address clause, and defer resolution
3669 -- and expansion of the aggregate to the freeze point of the entity.
3671 -- This is not always legal because the aggregate may contain other
3672 -- references that need freezing, e.g. references to other entities
3673 -- with address clauses. In any case, when compiling with -gnatI the
3674 -- presence of the address clause must be ignored.
3676 if Comes_From_Source (N)
3677 and then Expander_Active
3678 and then Nkind (E) = N_Aggregate
3679 and then
3680 ((Present (Following_Address_Clause (N))
3681 and then not Ignore_Rep_Clauses)
3682 or else Delayed_Aspect_Present)
3683 then
3684 Set_Etype (E, T);
3686 else
3687 Resolve (E, T);
3688 end if;
3690 -- No further action needed if E is a call to an inlined function
3691 -- which returns an unconstrained type and it has been expanded into
3692 -- a procedure call. In that case N has been replaced by an object
3693 -- declaration without initializing expression and it has been
3694 -- analyzed (see Expand_Inlined_Call).
3696 if Back_End_Inlining
3697 and then Expander_Active
3698 and then Nkind (E) = N_Function_Call
3699 and then Nkind (Name (E)) in N_Has_Entity
3700 and then Is_Inlined (Entity (Name (E)))
3701 and then not Is_Constrained (Etype (E))
3702 and then Analyzed (N)
3703 and then No (Expression (N))
3704 then
3705 Ghost_Mode := Save_Ghost_Mode;
3706 return;
3707 end if;
3709 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3710 -- node (which was marked already-analyzed), we need to set the type
3711 -- to something other than Any_Access in order to keep gigi happy.
3713 if Etype (E) = Any_Access then
3714 Set_Etype (E, T);
3715 end if;
3717 -- If the object is an access to variable, the initialization
3718 -- expression cannot be an access to constant.
3720 if Is_Access_Type (T)
3721 and then not Is_Access_Constant (T)
3722 and then Is_Access_Type (Etype (E))
3723 and then Is_Access_Constant (Etype (E))
3724 then
3725 Error_Msg_N
3726 ("access to variable cannot be initialized with an "
3727 & "access-to-constant expression", E);
3728 end if;
3730 if not Assignment_OK (N) then
3731 Check_Initialization (T, E);
3732 end if;
3734 Check_Unset_Reference (E);
3736 -- If this is a variable, then set current value. If this is a
3737 -- declared constant of a scalar type with a static expression,
3738 -- indicate that it is always valid.
3740 if not Constant_Present (N) then
3741 if Compile_Time_Known_Value (E) then
3742 Set_Current_Value (Id, E);
3743 end if;
3745 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3746 Set_Is_Known_Valid (Id);
3747 end if;
3749 -- Deal with setting of null flags
3751 if Is_Access_Type (T) then
3752 if Known_Non_Null (E) then
3753 Set_Is_Known_Non_Null (Id, True);
3754 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3755 Set_Is_Known_Null (Id, True);
3756 end if;
3757 end if;
3759 -- Check incorrect use of dynamically tagged expressions
3761 if Is_Tagged_Type (T) then
3762 Check_Dynamically_Tagged_Expression
3763 (Expr => E,
3764 Typ => T,
3765 Related_Nod => N);
3766 end if;
3768 Apply_Scalar_Range_Check (E, T);
3769 Apply_Static_Length_Check (E, T);
3771 if Nkind (Original_Node (N)) = N_Object_Declaration
3772 and then Comes_From_Source (Original_Node (N))
3774 -- Only call test if needed
3776 and then Restriction_Check_Required (SPARK_05)
3777 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3778 then
3779 Check_SPARK_05_Restriction
3780 ("initialization expression is not appropriate", E);
3781 end if;
3783 -- A formal parameter of a specific tagged type whose related
3784 -- subprogram is subject to pragma Extensions_Visible with value
3785 -- "False" cannot be implicitly converted to a class-wide type by
3786 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
3787 -- not consider internally generated expressions.
3789 if Is_Class_Wide_Type (T)
3790 and then Comes_From_Source (E)
3791 and then Is_EVF_Expression (E)
3792 then
3793 Error_Msg_N
3794 ("formal parameter with Extensions_Visible False cannot be "
3795 & "implicitly converted to class-wide type", E);
3796 end if;
3797 end if;
3799 -- If the No_Streams restriction is set, check that the type of the
3800 -- object is not, and does not contain, any subtype derived from
3801 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3802 -- Has_Stream just for efficiency reasons. There is no point in
3803 -- spending time on a Has_Stream check if the restriction is not set.
3805 if Restriction_Check_Required (No_Streams) then
3806 if Has_Stream (T) then
3807 Check_Restriction (No_Streams, N);
3808 end if;
3809 end if;
3811 -- Deal with predicate check before we start to do major rewriting. It
3812 -- is OK to initialize and then check the initialized value, since the
3813 -- object goes out of scope if we get a predicate failure. Note that we
3814 -- do this in the analyzer and not the expander because the analyzer
3815 -- does some substantial rewriting in some cases.
3817 -- We need a predicate check if the type has predicates, and if either
3818 -- there is an initializing expression, or for default initialization
3819 -- when we have at least one case of an explicit default initial value
3820 -- and then this is not an internal declaration whose initialization
3821 -- comes later (as for an aggregate expansion).
3823 if not Suppress_Assignment_Checks (N)
3824 and then Present (Predicate_Function (T))
3825 and then not No_Initialization (N)
3826 and then
3827 (Present (E)
3828 or else
3829 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3830 then
3831 -- If the type has a static predicate and the expression is known at
3832 -- compile time, see if the expression satisfies the predicate.
3834 if Present (E) then
3835 Check_Expression_Against_Static_Predicate (E, T);
3836 end if;
3838 Insert_After (N,
3839 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3840 end if;
3842 -- Case of unconstrained type
3844 if not Is_Definite_Subtype (T) then
3846 -- In SPARK, a declaration of unconstrained type is allowed
3847 -- only for constants of type string.
3849 if Is_String_Type (T) and then not Constant_Present (N) then
3850 Check_SPARK_05_Restriction
3851 ("declaration of object of unconstrained type not allowed", N);
3852 end if;
3854 -- Nothing to do in deferred constant case
3856 if Constant_Present (N) and then No (E) then
3857 null;
3859 -- Case of no initialization present
3861 elsif No (E) then
3862 if No_Initialization (N) then
3863 null;
3865 elsif Is_Class_Wide_Type (T) then
3866 Error_Msg_N
3867 ("initialization required in class-wide declaration ", N);
3869 else
3870 Error_Msg_N
3871 ("unconstrained subtype not allowed (need initialization)",
3872 Object_Definition (N));
3874 if Is_Record_Type (T) and then Has_Discriminants (T) then
3875 Error_Msg_N
3876 ("\provide initial value or explicit discriminant values",
3877 Object_Definition (N));
3879 Error_Msg_NE
3880 ("\or give default discriminant values for type&",
3881 Object_Definition (N), T);
3883 elsif Is_Array_Type (T) then
3884 Error_Msg_N
3885 ("\provide initial value or explicit array bounds",
3886 Object_Definition (N));
3887 end if;
3888 end if;
3890 -- Case of initialization present but in error. Set initial
3891 -- expression as absent (but do not make above complaints)
3893 elsif E = Error then
3894 Set_Expression (N, Empty);
3895 E := Empty;
3897 -- Case of initialization present
3899 else
3900 -- Check restrictions in Ada 83
3902 if not Constant_Present (N) then
3904 -- Unconstrained variables not allowed in Ada 83 mode
3906 if Ada_Version = Ada_83
3907 and then Comes_From_Source (Object_Definition (N))
3908 then
3909 Error_Msg_N
3910 ("(Ada 83) unconstrained variable not allowed",
3911 Object_Definition (N));
3912 end if;
3913 end if;
3915 -- Now we constrain the variable from the initializing expression
3917 -- If the expression is an aggregate, it has been expanded into
3918 -- individual assignments. Retrieve the actual type from the
3919 -- expanded construct.
3921 if Is_Array_Type (T)
3922 and then No_Initialization (N)
3923 and then Nkind (Original_Node (E)) = N_Aggregate
3924 then
3925 Act_T := Etype (E);
3927 -- In case of class-wide interface object declarations we delay
3928 -- the generation of the equivalent record type declarations until
3929 -- its expansion because there are cases in they are not required.
3931 elsif Is_Interface (T) then
3932 null;
3934 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
3935 -- we should prevent the generation of another Itype with the
3936 -- same name as the one already generated, or we end up with
3937 -- two identical types in GNATprove.
3939 elsif GNATprove_Mode then
3940 null;
3942 -- If the type is an unchecked union, no subtype can be built from
3943 -- the expression. Rewrite declaration as a renaming, which the
3944 -- back-end can handle properly. This is a rather unusual case,
3945 -- because most unchecked_union declarations have default values
3946 -- for discriminants and are thus not indefinite.
3948 elsif Is_Unchecked_Union (T) then
3949 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
3950 Set_Ekind (Id, E_Constant);
3951 else
3952 Set_Ekind (Id, E_Variable);
3953 end if;
3955 -- An object declared within a Ghost region is automatically
3956 -- Ghost (SPARK RM 6.9(2)).
3958 if Ghost_Mode > None then
3959 Set_Is_Ghost_Entity (Id);
3961 -- The Ghost policy in effect at the point of declaration
3962 -- and at the point of completion must match
3963 -- (SPARK RM 6.9(14)).
3965 if Present (Prev_Entity)
3966 and then Is_Ghost_Entity (Prev_Entity)
3967 then
3968 Check_Ghost_Completion (Prev_Entity, Id);
3969 end if;
3970 end if;
3972 Rewrite (N,
3973 Make_Object_Renaming_Declaration (Loc,
3974 Defining_Identifier => Id,
3975 Subtype_Mark => New_Occurrence_Of (T, Loc),
3976 Name => E));
3978 Set_Renamed_Object (Id, E);
3979 Freeze_Before (N, T);
3980 Set_Is_Frozen (Id);
3982 Ghost_Mode := Save_Ghost_Mode;
3983 return;
3985 else
3986 -- Ensure that the generated subtype has a unique external name
3987 -- when the related object is public. This guarantees that the
3988 -- subtype and its bounds will not be affected by switches or
3989 -- pragmas that may offset the internal counter due to extra
3990 -- generated code.
3992 if Is_Public (Id) then
3993 Related_Id := Id;
3994 else
3995 Related_Id := Empty;
3996 end if;
3998 Expand_Subtype_From_Expr
3999 (N => N,
4000 Unc_Type => T,
4001 Subtype_Indic => Object_Definition (N),
4002 Exp => E,
4003 Related_Id => Related_Id);
4005 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4006 end if;
4008 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4010 if Aliased_Present (N) then
4011 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4012 end if;
4014 Freeze_Before (N, Act_T);
4015 Freeze_Before (N, T);
4016 end if;
4018 elsif Is_Array_Type (T)
4019 and then No_Initialization (N)
4020 and then Nkind (Original_Node (E)) = N_Aggregate
4021 then
4022 if not Is_Entity_Name (Object_Definition (N)) then
4023 Act_T := Etype (E);
4024 Check_Compile_Time_Size (Act_T);
4026 if Aliased_Present (N) then
4027 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4028 end if;
4029 end if;
4031 -- When the given object definition and the aggregate are specified
4032 -- independently, and their lengths might differ do a length check.
4033 -- This cannot happen if the aggregate is of the form (others =>...)
4035 if not Is_Constrained (T) then
4036 null;
4038 elsif Nkind (E) = N_Raise_Constraint_Error then
4040 -- Aggregate is statically illegal. Place back in declaration
4042 Set_Expression (N, E);
4043 Set_No_Initialization (N, False);
4045 elsif T = Etype (E) then
4046 null;
4048 elsif Nkind (E) = N_Aggregate
4049 and then Present (Component_Associations (E))
4050 and then Present (Choices (First (Component_Associations (E))))
4051 and then Nkind (First
4052 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4053 then
4054 null;
4056 else
4057 Apply_Length_Check (E, T);
4058 end if;
4060 -- If the type is limited unconstrained with defaulted discriminants and
4061 -- there is no expression, then the object is constrained by the
4062 -- defaults, so it is worthwhile building the corresponding subtype.
4064 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4065 and then not Is_Constrained (T)
4066 and then Has_Discriminants (T)
4067 then
4068 if No (E) then
4069 Act_T := Build_Default_Subtype (T, N);
4070 else
4071 -- Ada 2005: A limited object may be initialized by means of an
4072 -- aggregate. If the type has default discriminants it has an
4073 -- unconstrained nominal type, Its actual subtype will be obtained
4074 -- from the aggregate, and not from the default discriminants.
4076 Act_T := Etype (E);
4077 end if;
4079 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4081 elsif Nkind (E) = N_Function_Call
4082 and then Constant_Present (N)
4083 and then Has_Unconstrained_Elements (Etype (E))
4084 then
4085 -- The back-end has problems with constants of a discriminated type
4086 -- with defaults, if the initial value is a function call. We
4087 -- generate an intermediate temporary that will receive a reference
4088 -- to the result of the call. The initialization expression then
4089 -- becomes a dereference of that temporary.
4091 Remove_Side_Effects (E);
4093 -- If this is a constant declaration of an unconstrained type and
4094 -- the initialization is an aggregate, we can use the subtype of the
4095 -- aggregate for the declared entity because it is immutable.
4097 elsif not Is_Constrained (T)
4098 and then Has_Discriminants (T)
4099 and then Constant_Present (N)
4100 and then not Has_Unchecked_Union (T)
4101 and then Nkind (E) = N_Aggregate
4102 then
4103 Act_T := Etype (E);
4104 end if;
4106 -- Check No_Wide_Characters restriction
4108 Check_Wide_Character_Restriction (T, Object_Definition (N));
4110 -- Indicate this is not set in source. Certainly true for constants, and
4111 -- true for variables so far (will be reset for a variable if and when
4112 -- we encounter a modification in the source).
4114 Set_Never_Set_In_Source (Id);
4116 -- Now establish the proper kind and type of the object
4118 if Constant_Present (N) then
4119 Set_Ekind (Id, E_Constant);
4120 Set_Is_True_Constant (Id);
4122 else
4123 Set_Ekind (Id, E_Variable);
4125 -- A variable is set as shared passive if it appears in a shared
4126 -- passive package, and is at the outer level. This is not done for
4127 -- entities generated during expansion, because those are always
4128 -- manipulated locally.
4130 if Is_Shared_Passive (Current_Scope)
4131 and then Is_Library_Level_Entity (Id)
4132 and then Comes_From_Source (Id)
4133 then
4134 Set_Is_Shared_Passive (Id);
4135 Check_Shared_Var (Id, T, N);
4136 end if;
4138 -- Set Has_Initial_Value if initializing expression present. Note
4139 -- that if there is no initializing expression, we leave the state
4140 -- of this flag unchanged (usually it will be False, but notably in
4141 -- the case of exception choice variables, it will already be true).
4143 if Present (E) then
4144 Set_Has_Initial_Value (Id);
4145 end if;
4146 end if;
4148 -- Initialize alignment and size and capture alignment setting
4150 Init_Alignment (Id);
4151 Init_Esize (Id);
4152 Set_Optimize_Alignment_Flags (Id);
4154 -- An object declared within a Ghost region is automatically Ghost
4155 -- (SPARK RM 6.9(2)).
4157 if Ghost_Mode > None
4158 or else (Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity))
4159 then
4160 Set_Is_Ghost_Entity (Id);
4162 -- The Ghost policy in effect at the point of declaration and at the
4163 -- point of completion must match (SPARK RM 6.9(14)).
4165 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4166 Check_Ghost_Completion (Prev_Entity, Id);
4167 end if;
4168 end if;
4170 -- Deal with aliased case
4172 if Aliased_Present (N) then
4173 Set_Is_Aliased (Id);
4175 -- If the object is aliased and the type is unconstrained with
4176 -- defaulted discriminants and there is no expression, then the
4177 -- object is constrained by the defaults, so it is worthwhile
4178 -- building the corresponding subtype.
4180 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4181 -- unconstrained, then only establish an actual subtype if the
4182 -- nominal subtype is indefinite. In definite cases the object is
4183 -- unconstrained in Ada 2005.
4185 if No (E)
4186 and then Is_Record_Type (T)
4187 and then not Is_Constrained (T)
4188 and then Has_Discriminants (T)
4189 and then (Ada_Version < Ada_2005
4190 or else not Is_Definite_Subtype (T))
4191 then
4192 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4193 end if;
4194 end if;
4196 -- Now we can set the type of the object
4198 Set_Etype (Id, Act_T);
4200 -- Non-constant object is marked to be treated as volatile if type is
4201 -- volatile and we clear the Current_Value setting that may have been
4202 -- set above. Doing so for constants isn't required and might interfere
4203 -- with possible uses of the object as a static expression in contexts
4204 -- incompatible with volatility (e.g. as a case-statement alternative).
4206 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4207 Set_Treat_As_Volatile (Id);
4208 Set_Current_Value (Id, Empty);
4209 end if;
4211 -- Deal with controlled types
4213 if Has_Controlled_Component (Etype (Id))
4214 or else Is_Controlled (Etype (Id))
4215 then
4216 if not Is_Library_Level_Entity (Id) then
4217 Check_Restriction (No_Nested_Finalization, N);
4218 else
4219 Validate_Controlled_Object (Id);
4220 end if;
4221 end if;
4223 if Has_Task (Etype (Id)) then
4224 Check_Restriction (No_Tasking, N);
4226 -- Deal with counting max tasks
4228 -- Nothing to do if inside a generic
4230 if Inside_A_Generic then
4231 null;
4233 -- If library level entity, then count tasks
4235 elsif Is_Library_Level_Entity (Id) then
4236 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4238 -- If not library level entity, then indicate we don't know max
4239 -- tasks and also check task hierarchy restriction and blocking
4240 -- operation (since starting a task is definitely blocking).
4242 else
4243 Check_Restriction (Max_Tasks, N);
4244 Check_Restriction (No_Task_Hierarchy, N);
4245 Check_Potentially_Blocking_Operation (N);
4246 end if;
4248 -- A rather specialized test. If we see two tasks being declared
4249 -- of the same type in the same object declaration, and the task
4250 -- has an entry with an address clause, we know that program error
4251 -- will be raised at run time since we can't have two tasks with
4252 -- entries at the same address.
4254 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4255 declare
4256 E : Entity_Id;
4258 begin
4259 E := First_Entity (Etype (Id));
4260 while Present (E) loop
4261 if Ekind (E) = E_Entry
4262 and then Present (Get_Attribute_Definition_Clause
4263 (E, Attribute_Address))
4264 then
4265 Error_Msg_Warn := SPARK_Mode /= On;
4266 Error_Msg_N
4267 ("more than one task with same entry address<<", N);
4268 Error_Msg_N ("\Program_Error [<<", N);
4269 Insert_Action (N,
4270 Make_Raise_Program_Error (Loc,
4271 Reason => PE_Duplicated_Entry_Address));
4272 exit;
4273 end if;
4275 Next_Entity (E);
4276 end loop;
4277 end;
4278 end if;
4279 end if;
4281 -- Some simple constant-propagation: if the expression is a constant
4282 -- string initialized with a literal, share the literal. This avoids
4283 -- a run-time copy.
4285 if Present (E)
4286 and then Is_Entity_Name (E)
4287 and then Ekind (Entity (E)) = E_Constant
4288 and then Base_Type (Etype (E)) = Standard_String
4289 then
4290 declare
4291 Val : constant Node_Id := Constant_Value (Entity (E));
4292 begin
4293 if Present (Val) and then Nkind (Val) = N_String_Literal then
4294 Rewrite (E, New_Copy (Val));
4295 end if;
4296 end;
4297 end if;
4299 -- Another optimization: if the nominal subtype is unconstrained and
4300 -- the expression is a function call that returns an unconstrained
4301 -- type, rewrite the declaration as a renaming of the result of the
4302 -- call. The exceptions below are cases where the copy is expected,
4303 -- either by the back end (Aliased case) or by the semantics, as for
4304 -- initializing controlled types or copying tags for classwide types.
4306 if Present (E)
4307 and then Nkind (E) = N_Explicit_Dereference
4308 and then Nkind (Original_Node (E)) = N_Function_Call
4309 and then not Is_Library_Level_Entity (Id)
4310 and then not Is_Constrained (Underlying_Type (T))
4311 and then not Is_Aliased (Id)
4312 and then not Is_Class_Wide_Type (T)
4313 and then not Is_Controlled_Active (T)
4314 and then not Has_Controlled_Component (Base_Type (T))
4315 and then Expander_Active
4316 then
4317 Rewrite (N,
4318 Make_Object_Renaming_Declaration (Loc,
4319 Defining_Identifier => Id,
4320 Access_Definition => Empty,
4321 Subtype_Mark => New_Occurrence_Of
4322 (Base_Type (Etype (Id)), Loc),
4323 Name => E));
4325 Set_Renamed_Object (Id, E);
4327 -- Force generation of debugging information for the constant and for
4328 -- the renamed function call.
4330 Set_Debug_Info_Needed (Id);
4331 Set_Debug_Info_Needed (Entity (Prefix (E)));
4332 end if;
4334 if Present (Prev_Entity)
4335 and then Is_Frozen (Prev_Entity)
4336 and then not Error_Posted (Id)
4337 then
4338 Error_Msg_N ("full constant declaration appears too late", N);
4339 end if;
4341 Check_Eliminated (Id);
4343 -- Deal with setting In_Private_Part flag if in private part
4345 if Ekind (Scope (Id)) = E_Package
4346 and then In_Private_Part (Scope (Id))
4347 then
4348 Set_In_Private_Part (Id);
4349 end if;
4351 -- Check for violation of No_Local_Timing_Events
4353 if Restriction_Check_Required (No_Local_Timing_Events)
4354 and then not Is_Library_Level_Entity (Id)
4355 and then Is_RTE (Etype (Id), RE_Timing_Event)
4356 then
4357 Check_Restriction (No_Local_Timing_Events, N);
4358 end if;
4360 <<Leave>>
4361 -- Initialize the refined state of a variable here because this is a
4362 -- common destination for legal and illegal object declarations.
4364 if Ekind (Id) = E_Variable then
4365 Set_Encapsulating_State (Id, Empty);
4366 end if;
4368 if Has_Aspects (N) then
4369 Analyze_Aspect_Specifications (N, Id);
4370 end if;
4372 Analyze_Dimension (N);
4374 -- Verify whether the object declaration introduces an illegal hidden
4375 -- state within a package subject to a null abstract state.
4377 if Ekind (Id) = E_Variable then
4378 Check_No_Hidden_State (Id);
4379 end if;
4381 Ghost_Mode := Save_Ghost_Mode;
4382 end Analyze_Object_Declaration;
4384 ---------------------------
4385 -- Analyze_Others_Choice --
4386 ---------------------------
4388 -- Nothing to do for the others choice node itself, the semantic analysis
4389 -- of the others choice will occur as part of the processing of the parent
4391 procedure Analyze_Others_Choice (N : Node_Id) is
4392 pragma Warnings (Off, N);
4393 begin
4394 null;
4395 end Analyze_Others_Choice;
4397 -------------------------------------------
4398 -- Analyze_Private_Extension_Declaration --
4399 -------------------------------------------
4401 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4402 Indic : constant Node_Id := Subtype_Indication (N);
4403 T : constant Entity_Id := Defining_Identifier (N);
4404 Parent_Base : Entity_Id;
4405 Parent_Type : Entity_Id;
4407 begin
4408 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4410 if Is_Non_Empty_List (Interface_List (N)) then
4411 declare
4412 Intf : Node_Id;
4413 T : Entity_Id;
4415 begin
4416 Intf := First (Interface_List (N));
4417 while Present (Intf) loop
4418 T := Find_Type_Of_Subtype_Indic (Intf);
4420 Diagnose_Interface (Intf, T);
4421 Next (Intf);
4422 end loop;
4423 end;
4424 end if;
4426 Generate_Definition (T);
4428 -- For other than Ada 2012, just enter the name in the current scope
4430 if Ada_Version < Ada_2012 then
4431 Enter_Name (T);
4433 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4434 -- case of private type that completes an incomplete type.
4436 else
4437 declare
4438 Prev : Entity_Id;
4440 begin
4441 Prev := Find_Type_Name (N);
4443 pragma Assert (Prev = T
4444 or else (Ekind (Prev) = E_Incomplete_Type
4445 and then Present (Full_View (Prev))
4446 and then Full_View (Prev) = T));
4447 end;
4448 end if;
4450 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4451 Parent_Base := Base_Type (Parent_Type);
4453 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4454 Set_Ekind (T, Ekind (Parent_Type));
4455 Set_Etype (T, Any_Type);
4456 goto Leave;
4458 elsif not Is_Tagged_Type (Parent_Type) then
4459 Error_Msg_N
4460 ("parent of type extension must be a tagged type ", Indic);
4461 goto Leave;
4463 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4464 Error_Msg_N ("premature derivation of incomplete type", Indic);
4465 goto Leave;
4467 elsif Is_Concurrent_Type (Parent_Type) then
4468 Error_Msg_N
4469 ("parent type of a private extension cannot be "
4470 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4472 Set_Etype (T, Any_Type);
4473 Set_Ekind (T, E_Limited_Private_Type);
4474 Set_Private_Dependents (T, New_Elmt_List);
4475 Set_Error_Posted (T);
4476 goto Leave;
4477 end if;
4479 -- Perhaps the parent type should be changed to the class-wide type's
4480 -- specific type in this case to prevent cascading errors ???
4482 if Is_Class_Wide_Type (Parent_Type) then
4483 Error_Msg_N
4484 ("parent of type extension must not be a class-wide type", Indic);
4485 goto Leave;
4486 end if;
4488 if (not Is_Package_Or_Generic_Package (Current_Scope)
4489 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4490 or else In_Private_Part (Current_Scope)
4492 then
4493 Error_Msg_N ("invalid context for private extension", N);
4494 end if;
4496 -- Set common attributes
4498 Set_Is_Pure (T, Is_Pure (Current_Scope));
4499 Set_Scope (T, Current_Scope);
4500 Set_Ekind (T, E_Record_Type_With_Private);
4501 Init_Size_Align (T);
4502 Set_Default_SSO (T);
4504 Set_Etype (T, Parent_Base);
4505 Set_Has_Task (T, Has_Task (Parent_Base));
4506 Set_Has_Protected (T, Has_Task (Parent_Base));
4508 Set_Convention (T, Convention (Parent_Type));
4509 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4510 Set_Is_First_Subtype (T);
4511 Make_Class_Wide_Type (T);
4513 if Unknown_Discriminants_Present (N) then
4514 Set_Discriminant_Constraint (T, No_Elist);
4515 end if;
4517 Build_Derived_Record_Type (N, Parent_Type, T);
4519 -- Propagate inherited invariant information. The new type has
4520 -- invariants, if the parent type has inheritable invariants,
4521 -- and these invariants can in turn be inherited.
4523 if Has_Inheritable_Invariants (Parent_Type) then
4524 Set_Has_Inheritable_Invariants (T);
4525 Set_Has_Invariants (T);
4526 end if;
4528 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4529 -- synchronized formal derived type.
4531 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4532 Set_Is_Limited_Record (T);
4534 -- Formal derived type case
4536 if Is_Generic_Type (T) then
4538 -- The parent must be a tagged limited type or a synchronized
4539 -- interface.
4541 if (not Is_Tagged_Type (Parent_Type)
4542 or else not Is_Limited_Type (Parent_Type))
4543 and then
4544 (not Is_Interface (Parent_Type)
4545 or else not Is_Synchronized_Interface (Parent_Type))
4546 then
4547 Error_Msg_NE ("parent type of & must be tagged limited " &
4548 "or synchronized", N, T);
4549 end if;
4551 -- The progenitors (if any) must be limited or synchronized
4552 -- interfaces.
4554 if Present (Interfaces (T)) then
4555 declare
4556 Iface : Entity_Id;
4557 Iface_Elmt : Elmt_Id;
4559 begin
4560 Iface_Elmt := First_Elmt (Interfaces (T));
4561 while Present (Iface_Elmt) loop
4562 Iface := Node (Iface_Elmt);
4564 if not Is_Limited_Interface (Iface)
4565 and then not Is_Synchronized_Interface (Iface)
4566 then
4567 Error_Msg_NE ("progenitor & must be limited " &
4568 "or synchronized", N, Iface);
4569 end if;
4571 Next_Elmt (Iface_Elmt);
4572 end loop;
4573 end;
4574 end if;
4576 -- Regular derived extension, the parent must be a limited or
4577 -- synchronized interface.
4579 else
4580 if not Is_Interface (Parent_Type)
4581 or else (not Is_Limited_Interface (Parent_Type)
4582 and then not Is_Synchronized_Interface (Parent_Type))
4583 then
4584 Error_Msg_NE
4585 ("parent type of & must be limited interface", N, T);
4586 end if;
4587 end if;
4589 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4590 -- extension with a synchronized parent must be explicitly declared
4591 -- synchronized, because the full view will be a synchronized type.
4592 -- This must be checked before the check for limited types below,
4593 -- to ensure that types declared limited are not allowed to extend
4594 -- synchronized interfaces.
4596 elsif Is_Interface (Parent_Type)
4597 and then Is_Synchronized_Interface (Parent_Type)
4598 and then not Synchronized_Present (N)
4599 then
4600 Error_Msg_NE
4601 ("private extension of& must be explicitly synchronized",
4602 N, Parent_Type);
4604 elsif Limited_Present (N) then
4605 Set_Is_Limited_Record (T);
4607 if not Is_Limited_Type (Parent_Type)
4608 and then
4609 (not Is_Interface (Parent_Type)
4610 or else not Is_Limited_Interface (Parent_Type))
4611 then
4612 Error_Msg_NE ("parent type& of limited extension must be limited",
4613 N, Parent_Type);
4614 end if;
4615 end if;
4617 <<Leave>>
4618 if Has_Aspects (N) then
4619 Analyze_Aspect_Specifications (N, T);
4620 end if;
4621 end Analyze_Private_Extension_Declaration;
4623 ---------------------------------
4624 -- Analyze_Subtype_Declaration --
4625 ---------------------------------
4627 procedure Analyze_Subtype_Declaration
4628 (N : Node_Id;
4629 Skip : Boolean := False)
4631 Id : constant Entity_Id := Defining_Identifier (N);
4632 R_Checks : Check_Result;
4633 T : Entity_Id;
4635 begin
4636 Generate_Definition (Id);
4637 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4638 Init_Size_Align (Id);
4640 -- The following guard condition on Enter_Name is to handle cases where
4641 -- the defining identifier has already been entered into the scope but
4642 -- the declaration as a whole needs to be analyzed.
4644 -- This case in particular happens for derived enumeration types. The
4645 -- derived enumeration type is processed as an inserted enumeration type
4646 -- declaration followed by a rewritten subtype declaration. The defining
4647 -- identifier, however, is entered into the name scope very early in the
4648 -- processing of the original type declaration and therefore needs to be
4649 -- avoided here, when the created subtype declaration is analyzed. (See
4650 -- Build_Derived_Types)
4652 -- This also happens when the full view of a private type is derived
4653 -- type with constraints. In this case the entity has been introduced
4654 -- in the private declaration.
4656 -- Finally this happens in some complex cases when validity checks are
4657 -- enabled, where the same subtype declaration may be analyzed twice.
4658 -- This can happen if the subtype is created by the pre-analysis of
4659 -- an attribute tht gives the range of a loop statement, and the loop
4660 -- itself appears within an if_statement that will be rewritten during
4661 -- expansion.
4663 if Skip
4664 or else (Present (Etype (Id))
4665 and then (Is_Private_Type (Etype (Id))
4666 or else Is_Task_Type (Etype (Id))
4667 or else Is_Rewrite_Substitution (N)))
4668 then
4669 null;
4671 elsif Current_Entity (Id) = Id then
4672 null;
4674 else
4675 Enter_Name (Id);
4676 end if;
4678 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4680 -- Class-wide equivalent types of records with unknown discriminants
4681 -- involve the generation of an itype which serves as the private view
4682 -- of a constrained record subtype. In such cases the base type of the
4683 -- current subtype we are processing is the private itype. Use the full
4684 -- of the private itype when decorating various attributes.
4686 if Is_Itype (T)
4687 and then Is_Private_Type (T)
4688 and then Present (Full_View (T))
4689 then
4690 T := Full_View (T);
4691 end if;
4693 -- Inherit common attributes
4695 Set_Is_Volatile (Id, Is_Volatile (T));
4696 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4697 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4698 Set_Convention (Id, Convention (T));
4700 -- If ancestor has predicates then so does the subtype, and in addition
4701 -- we must delay the freeze to properly arrange predicate inheritance.
4703 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4704 -- in which T = ID, so the above tests and assignments do nothing???
4706 if Has_Predicates (T)
4707 or else (Present (Ancestor_Subtype (T))
4708 and then Has_Predicates (Ancestor_Subtype (T)))
4709 then
4710 Set_Has_Predicates (Id);
4711 Set_Has_Delayed_Freeze (Id);
4712 end if;
4714 -- Subtype of Boolean cannot have a constraint in SPARK
4716 if Is_Boolean_Type (T)
4717 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4718 then
4719 Check_SPARK_05_Restriction
4720 ("subtype of Boolean cannot have constraint", N);
4721 end if;
4723 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4724 declare
4725 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4726 One_Cstr : Node_Id;
4727 Low : Node_Id;
4728 High : Node_Id;
4730 begin
4731 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4732 One_Cstr := First (Constraints (Cstr));
4733 while Present (One_Cstr) loop
4735 -- Index or discriminant constraint in SPARK must be a
4736 -- subtype mark.
4738 if not
4739 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4740 then
4741 Check_SPARK_05_Restriction
4742 ("subtype mark required", One_Cstr);
4744 -- String subtype must have a lower bound of 1 in SPARK.
4745 -- Note that we do not need to test for the non-static case
4746 -- here, since that was already taken care of in
4747 -- Process_Range_Expr_In_Decl.
4749 elsif Base_Type (T) = Standard_String then
4750 Get_Index_Bounds (One_Cstr, Low, High);
4752 if Is_OK_Static_Expression (Low)
4753 and then Expr_Value (Low) /= 1
4754 then
4755 Check_SPARK_05_Restriction
4756 ("String subtype must have lower bound of 1", N);
4757 end if;
4758 end if;
4760 Next (One_Cstr);
4761 end loop;
4762 end if;
4763 end;
4764 end if;
4766 -- In the case where there is no constraint given in the subtype
4767 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4768 -- semantic attributes must be established here.
4770 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4771 Set_Etype (Id, Base_Type (T));
4773 -- Subtype of unconstrained array without constraint is not allowed
4774 -- in SPARK.
4776 if Is_Array_Type (T) and then not Is_Constrained (T) then
4777 Check_SPARK_05_Restriction
4778 ("subtype of unconstrained array must have constraint", N);
4779 end if;
4781 case Ekind (T) is
4782 when Array_Kind =>
4783 Set_Ekind (Id, E_Array_Subtype);
4784 Copy_Array_Subtype_Attributes (Id, T);
4786 when Decimal_Fixed_Point_Kind =>
4787 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4788 Set_Digits_Value (Id, Digits_Value (T));
4789 Set_Delta_Value (Id, Delta_Value (T));
4790 Set_Scale_Value (Id, Scale_Value (T));
4791 Set_Small_Value (Id, Small_Value (T));
4792 Set_Scalar_Range (Id, Scalar_Range (T));
4793 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4794 Set_Is_Constrained (Id, Is_Constrained (T));
4795 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4796 Set_RM_Size (Id, RM_Size (T));
4798 when Enumeration_Kind =>
4799 Set_Ekind (Id, E_Enumeration_Subtype);
4800 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4801 Set_Scalar_Range (Id, Scalar_Range (T));
4802 Set_Is_Character_Type (Id, Is_Character_Type (T));
4803 Set_Is_Constrained (Id, Is_Constrained (T));
4804 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4805 Set_RM_Size (Id, RM_Size (T));
4806 Inherit_Predicate_Flags (Id, T);
4808 when Ordinary_Fixed_Point_Kind =>
4809 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4810 Set_Scalar_Range (Id, Scalar_Range (T));
4811 Set_Small_Value (Id, Small_Value (T));
4812 Set_Delta_Value (Id, Delta_Value (T));
4813 Set_Is_Constrained (Id, Is_Constrained (T));
4814 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4815 Set_RM_Size (Id, RM_Size (T));
4817 when Float_Kind =>
4818 Set_Ekind (Id, E_Floating_Point_Subtype);
4819 Set_Scalar_Range (Id, Scalar_Range (T));
4820 Set_Digits_Value (Id, Digits_Value (T));
4821 Set_Is_Constrained (Id, Is_Constrained (T));
4823 -- If the floating point type has dimensions, these will be
4824 -- inherited subsequently when Analyze_Dimensions is called.
4826 when Signed_Integer_Kind =>
4827 Set_Ekind (Id, E_Signed_Integer_Subtype);
4828 Set_Scalar_Range (Id, Scalar_Range (T));
4829 Set_Is_Constrained (Id, Is_Constrained (T));
4830 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4831 Set_RM_Size (Id, RM_Size (T));
4832 Inherit_Predicate_Flags (Id, T);
4834 when Modular_Integer_Kind =>
4835 Set_Ekind (Id, E_Modular_Integer_Subtype);
4836 Set_Scalar_Range (Id, Scalar_Range (T));
4837 Set_Is_Constrained (Id, Is_Constrained (T));
4838 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4839 Set_RM_Size (Id, RM_Size (T));
4840 Inherit_Predicate_Flags (Id, T);
4842 when Class_Wide_Kind =>
4843 Set_Ekind (Id, E_Class_Wide_Subtype);
4844 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4845 Set_Cloned_Subtype (Id, T);
4846 Set_Is_Tagged_Type (Id, True);
4847 Set_Has_Unknown_Discriminants
4848 (Id, True);
4849 Set_No_Tagged_Streams_Pragma
4850 (Id, No_Tagged_Streams_Pragma (T));
4852 if Ekind (T) = E_Class_Wide_Subtype then
4853 Set_Equivalent_Type (Id, Equivalent_Type (T));
4854 end if;
4856 when E_Record_Type | E_Record_Subtype =>
4857 Set_Ekind (Id, E_Record_Subtype);
4859 if Ekind (T) = E_Record_Subtype
4860 and then Present (Cloned_Subtype (T))
4861 then
4862 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4863 else
4864 Set_Cloned_Subtype (Id, T);
4865 end if;
4867 Set_First_Entity (Id, First_Entity (T));
4868 Set_Last_Entity (Id, Last_Entity (T));
4869 Set_Has_Discriminants (Id, Has_Discriminants (T));
4870 Set_Is_Constrained (Id, Is_Constrained (T));
4871 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4872 Set_Has_Implicit_Dereference
4873 (Id, Has_Implicit_Dereference (T));
4874 Set_Has_Unknown_Discriminants
4875 (Id, Has_Unknown_Discriminants (T));
4877 if Has_Discriminants (T) then
4878 Set_Discriminant_Constraint
4879 (Id, Discriminant_Constraint (T));
4880 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4882 elsif Has_Unknown_Discriminants (Id) then
4883 Set_Discriminant_Constraint (Id, No_Elist);
4884 end if;
4886 if Is_Tagged_Type (T) then
4887 Set_Is_Tagged_Type (Id, True);
4888 Set_No_Tagged_Streams_Pragma
4889 (Id, No_Tagged_Streams_Pragma (T));
4890 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4891 Set_Direct_Primitive_Operations
4892 (Id, Direct_Primitive_Operations (T));
4893 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4895 if Is_Interface (T) then
4896 Set_Is_Interface (Id);
4897 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4898 end if;
4899 end if;
4901 when Private_Kind =>
4902 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4903 Set_Has_Discriminants (Id, Has_Discriminants (T));
4904 Set_Is_Constrained (Id, Is_Constrained (T));
4905 Set_First_Entity (Id, First_Entity (T));
4906 Set_Last_Entity (Id, Last_Entity (T));
4907 Set_Private_Dependents (Id, New_Elmt_List);
4908 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4909 Set_Has_Implicit_Dereference
4910 (Id, Has_Implicit_Dereference (T));
4911 Set_Has_Unknown_Discriminants
4912 (Id, Has_Unknown_Discriminants (T));
4913 Set_Known_To_Have_Preelab_Init
4914 (Id, Known_To_Have_Preelab_Init (T));
4916 if Is_Tagged_Type (T) then
4917 Set_Is_Tagged_Type (Id);
4918 Set_No_Tagged_Streams_Pragma (Id,
4919 No_Tagged_Streams_Pragma (T));
4920 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4921 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4922 Set_Direct_Primitive_Operations (Id,
4923 Direct_Primitive_Operations (T));
4924 end if;
4926 -- In general the attributes of the subtype of a private type
4927 -- are the attributes of the partial view of parent. However,
4928 -- the full view may be a discriminated type, and the subtype
4929 -- must share the discriminant constraint to generate correct
4930 -- calls to initialization procedures.
4932 if Has_Discriminants (T) then
4933 Set_Discriminant_Constraint
4934 (Id, Discriminant_Constraint (T));
4935 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4937 elsif Present (Full_View (T))
4938 and then Has_Discriminants (Full_View (T))
4939 then
4940 Set_Discriminant_Constraint
4941 (Id, Discriminant_Constraint (Full_View (T)));
4942 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4944 -- This would seem semantically correct, but apparently
4945 -- generates spurious errors about missing components ???
4947 -- Set_Has_Discriminants (Id);
4948 end if;
4950 Prepare_Private_Subtype_Completion (Id, N);
4952 -- If this is the subtype of a constrained private type with
4953 -- discriminants that has got a full view and we also have
4954 -- built a completion just above, show that the completion
4955 -- is a clone of the full view to the back-end.
4957 if Has_Discriminants (T)
4958 and then not Has_Unknown_Discriminants (T)
4959 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
4960 and then Present (Full_View (T))
4961 and then Present (Full_View (Id))
4962 then
4963 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
4964 end if;
4966 when Access_Kind =>
4967 Set_Ekind (Id, E_Access_Subtype);
4968 Set_Is_Constrained (Id, Is_Constrained (T));
4969 Set_Is_Access_Constant
4970 (Id, Is_Access_Constant (T));
4971 Set_Directly_Designated_Type
4972 (Id, Designated_Type (T));
4973 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4975 -- A Pure library_item must not contain the declaration of a
4976 -- named access type, except within a subprogram, generic
4977 -- subprogram, task unit, or protected unit, or if it has
4978 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4980 if Comes_From_Source (Id)
4981 and then In_Pure_Unit
4982 and then not In_Subprogram_Task_Protected_Unit
4983 and then not No_Pool_Assigned (Id)
4984 then
4985 Error_Msg_N
4986 ("named access types not allowed in pure unit", N);
4987 end if;
4989 when Concurrent_Kind =>
4990 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4991 Set_Corresponding_Record_Type (Id,
4992 Corresponding_Record_Type (T));
4993 Set_First_Entity (Id, First_Entity (T));
4994 Set_First_Private_Entity (Id, First_Private_Entity (T));
4995 Set_Has_Discriminants (Id, Has_Discriminants (T));
4996 Set_Is_Constrained (Id, Is_Constrained (T));
4997 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4998 Set_Last_Entity (Id, Last_Entity (T));
5000 if Is_Tagged_Type (T) then
5001 Set_No_Tagged_Streams_Pragma
5002 (Id, No_Tagged_Streams_Pragma (T));
5003 end if;
5005 if Has_Discriminants (T) then
5006 Set_Discriminant_Constraint
5007 (Id, Discriminant_Constraint (T));
5008 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5009 end if;
5011 when Incomplete_Kind =>
5012 if Ada_Version >= Ada_2005 then
5014 -- In Ada 2005 an incomplete type can be explicitly tagged:
5015 -- propagate indication. Note that we also have to include
5016 -- subtypes for Ada 2012 extended use of incomplete types.
5018 Set_Ekind (Id, E_Incomplete_Subtype);
5019 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5020 Set_Private_Dependents (Id, New_Elmt_List);
5022 if Is_Tagged_Type (Id) then
5023 Set_No_Tagged_Streams_Pragma
5024 (Id, No_Tagged_Streams_Pragma (T));
5025 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5026 end if;
5028 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5029 -- incomplete type visible through a limited with clause.
5031 if From_Limited_With (T)
5032 and then Present (Non_Limited_View (T))
5033 then
5034 Set_From_Limited_With (Id);
5035 Set_Non_Limited_View (Id, Non_Limited_View (T));
5037 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5038 -- to the private dependents of the original incomplete
5039 -- type for future transformation.
5041 else
5042 Append_Elmt (Id, Private_Dependents (T));
5043 end if;
5045 -- If the subtype name denotes an incomplete type an error
5046 -- was already reported by Process_Subtype.
5048 else
5049 Set_Etype (Id, Any_Type);
5050 end if;
5052 when others =>
5053 raise Program_Error;
5054 end case;
5055 end if;
5057 if Etype (Id) = Any_Type then
5058 goto Leave;
5059 end if;
5061 -- Some common processing on all types
5063 Set_Size_Info (Id, T);
5064 Set_First_Rep_Item (Id, First_Rep_Item (T));
5066 -- If the parent type is a generic actual, so is the subtype. This may
5067 -- happen in a nested instance. Why Comes_From_Source test???
5069 if not Comes_From_Source (N) then
5070 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5071 end if;
5073 -- If this is a subtype declaration for an actual in an instance,
5074 -- inherit static and dynamic predicates if any.
5076 -- If declaration has no aspect specifications, inherit predicate
5077 -- info as well. Unclear how to handle the case of both specified
5078 -- and inherited predicates ??? Other inherited aspects, such as
5079 -- invariants, should be OK, but the combination with later pragmas
5080 -- may also require special merging.
5082 if Has_Predicates (T)
5083 and then Present (Predicate_Function (T))
5085 and then
5086 ((In_Instance and then not Comes_From_Source (N))
5087 or else No (Aspect_Specifications (N)))
5088 then
5089 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5091 if Has_Static_Predicate (T) then
5092 Set_Has_Static_Predicate (Id);
5093 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5094 end if;
5095 end if;
5097 -- Remaining processing depends on characteristics of base type
5099 T := Etype (Id);
5101 Set_Is_Immediately_Visible (Id, True);
5102 Set_Depends_On_Private (Id, Has_Private_Component (T));
5103 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5105 if Is_Interface (T) then
5106 Set_Is_Interface (Id);
5107 end if;
5109 if Present (Generic_Parent_Type (N))
5110 and then
5111 (Nkind (Parent (Generic_Parent_Type (N))) /=
5112 N_Formal_Type_Declaration
5113 or else Nkind (Formal_Type_Definition
5114 (Parent (Generic_Parent_Type (N)))) /=
5115 N_Formal_Private_Type_Definition)
5116 then
5117 if Is_Tagged_Type (Id) then
5119 -- If this is a generic actual subtype for a synchronized type,
5120 -- the primitive operations are those of the corresponding record
5121 -- for which there is a separate subtype declaration.
5123 if Is_Concurrent_Type (Id) then
5124 null;
5125 elsif Is_Class_Wide_Type (Id) then
5126 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5127 else
5128 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5129 end if;
5131 elsif Scope (Etype (Id)) /= Standard_Standard then
5132 Derive_Subprograms (Generic_Parent_Type (N), Id);
5133 end if;
5134 end if;
5136 if Is_Private_Type (T) and then Present (Full_View (T)) then
5137 Conditional_Delay (Id, Full_View (T));
5139 -- The subtypes of components or subcomponents of protected types
5140 -- do not need freeze nodes, which would otherwise appear in the
5141 -- wrong scope (before the freeze node for the protected type). The
5142 -- proper subtypes are those of the subcomponents of the corresponding
5143 -- record.
5145 elsif Ekind (Scope (Id)) /= E_Protected_Type
5146 and then Present (Scope (Scope (Id))) -- error defense
5147 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5148 then
5149 Conditional_Delay (Id, T);
5150 end if;
5152 -- Check that Constraint_Error is raised for a scalar subtype indication
5153 -- when the lower or upper bound of a non-null range lies outside the
5154 -- range of the type mark.
5156 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5157 if Is_Scalar_Type (Etype (Id))
5158 and then Scalar_Range (Id) /=
5159 Scalar_Range (Etype (Subtype_Mark
5160 (Subtype_Indication (N))))
5161 then
5162 Apply_Range_Check
5163 (Scalar_Range (Id),
5164 Etype (Subtype_Mark (Subtype_Indication (N))));
5166 -- In the array case, check compatibility for each index
5168 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5169 then
5170 -- This really should be a subprogram that finds the indications
5171 -- to check???
5173 declare
5174 Subt_Index : Node_Id := First_Index (Id);
5175 Target_Index : Node_Id :=
5176 First_Index (Etype
5177 (Subtype_Mark (Subtype_Indication (N))));
5178 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5180 begin
5181 while Present (Subt_Index) loop
5182 if ((Nkind (Subt_Index) = N_Identifier
5183 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5184 or else Nkind (Subt_Index) = N_Subtype_Indication)
5185 and then
5186 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5187 then
5188 declare
5189 Target_Typ : constant Entity_Id :=
5190 Etype (Target_Index);
5191 begin
5192 R_Checks :=
5193 Get_Range_Checks
5194 (Scalar_Range (Etype (Subt_Index)),
5195 Target_Typ,
5196 Etype (Subt_Index),
5197 Defining_Identifier (N));
5199 -- Reset Has_Dynamic_Range_Check on the subtype to
5200 -- prevent elision of the index check due to a dynamic
5201 -- check generated for a preceding index (needed since
5202 -- Insert_Range_Checks tries to avoid generating
5203 -- redundant checks on a given declaration).
5205 Set_Has_Dynamic_Range_Check (N, False);
5207 Insert_Range_Checks
5208 (R_Checks,
5210 Target_Typ,
5211 Sloc (Defining_Identifier (N)));
5213 -- Record whether this index involved a dynamic check
5215 Has_Dyn_Chk :=
5216 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5217 end;
5218 end if;
5220 Next_Index (Subt_Index);
5221 Next_Index (Target_Index);
5222 end loop;
5224 -- Finally, mark whether the subtype involves dynamic checks
5226 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5227 end;
5228 end if;
5229 end if;
5231 -- A type invariant applies to any subtype in its scope, in particular
5232 -- to a generic actual.
5234 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5235 Set_Has_Invariants (Id);
5236 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5237 end if;
5239 -- Make sure that generic actual types are properly frozen. The subtype
5240 -- is marked as a generic actual type when the enclosing instance is
5241 -- analyzed, so here we identify the subtype from the tree structure.
5243 if Expander_Active
5244 and then Is_Generic_Actual_Type (Id)
5245 and then In_Instance
5246 and then not Comes_From_Source (N)
5247 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5248 and then Is_Frozen (T)
5249 then
5250 Freeze_Before (N, Id);
5251 end if;
5253 Set_Optimize_Alignment_Flags (Id);
5254 Check_Eliminated (Id);
5256 <<Leave>>
5257 if Has_Aspects (N) then
5258 Analyze_Aspect_Specifications (N, Id);
5259 end if;
5261 Analyze_Dimension (N);
5263 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5264 -- indications on composite types where the constraints are dynamic.
5265 -- Note that object declarations and aggregates generate implicit
5266 -- subtype declarations, which this covers. One special case is that the
5267 -- implicitly generated "=" for discriminated types includes an
5268 -- offending subtype declaration, which is harmless, so we ignore it
5269 -- here.
5271 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5272 declare
5273 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5274 begin
5275 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5276 and then not (Is_Internal (Id)
5277 and then Is_TSS (Scope (Id),
5278 TSS_Composite_Equality))
5279 and then not Within_Init_Proc
5280 and then not All_Composite_Constraints_Static (Cstr)
5281 then
5282 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5283 end if;
5284 end;
5285 end if;
5286 end Analyze_Subtype_Declaration;
5288 --------------------------------
5289 -- Analyze_Subtype_Indication --
5290 --------------------------------
5292 procedure Analyze_Subtype_Indication (N : Node_Id) is
5293 T : constant Entity_Id := Subtype_Mark (N);
5294 R : constant Node_Id := Range_Expression (Constraint (N));
5296 begin
5297 Analyze (T);
5299 if R /= Error then
5300 Analyze (R);
5301 Set_Etype (N, Etype (R));
5302 Resolve (R, Entity (T));
5303 else
5304 Set_Error_Posted (R);
5305 Set_Error_Posted (T);
5306 end if;
5307 end Analyze_Subtype_Indication;
5309 --------------------------
5310 -- Analyze_Variant_Part --
5311 --------------------------
5313 procedure Analyze_Variant_Part (N : Node_Id) is
5314 Discr_Name : Node_Id;
5315 Discr_Type : Entity_Id;
5317 procedure Process_Variant (A : Node_Id);
5318 -- Analyze declarations for a single variant
5320 package Analyze_Variant_Choices is
5321 new Generic_Analyze_Choices (Process_Variant);
5322 use Analyze_Variant_Choices;
5324 ---------------------
5325 -- Process_Variant --
5326 ---------------------
5328 procedure Process_Variant (A : Node_Id) is
5329 CL : constant Node_Id := Component_List (A);
5330 begin
5331 if not Null_Present (CL) then
5332 Analyze_Declarations (Component_Items (CL));
5334 if Present (Variant_Part (CL)) then
5335 Analyze (Variant_Part (CL));
5336 end if;
5337 end if;
5338 end Process_Variant;
5340 -- Start of processing for Analyze_Variant_Part
5342 begin
5343 Discr_Name := Name (N);
5344 Analyze (Discr_Name);
5346 -- If Discr_Name bad, get out (prevent cascaded errors)
5348 if Etype (Discr_Name) = Any_Type then
5349 return;
5350 end if;
5352 -- Check invalid discriminant in variant part
5354 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5355 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5356 end if;
5358 Discr_Type := Etype (Entity (Discr_Name));
5360 if not Is_Discrete_Type (Discr_Type) then
5361 Error_Msg_N
5362 ("discriminant in a variant part must be of a discrete type",
5363 Name (N));
5364 return;
5365 end if;
5367 -- Now analyze the choices, which also analyzes the declarations that
5368 -- are associated with each choice.
5370 Analyze_Choices (Variants (N), Discr_Type);
5372 -- Note: we used to instantiate and call Check_Choices here to check
5373 -- that the choices covered the discriminant, but it's too early to do
5374 -- that because of statically predicated subtypes, whose analysis may
5375 -- be deferred to their freeze point which may be as late as the freeze
5376 -- point of the containing record. So this call is now to be found in
5377 -- Freeze_Record_Declaration.
5379 end Analyze_Variant_Part;
5381 ----------------------------
5382 -- Array_Type_Declaration --
5383 ----------------------------
5385 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5386 Component_Def : constant Node_Id := Component_Definition (Def);
5387 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5388 Element_Type : Entity_Id;
5389 Implicit_Base : Entity_Id;
5390 Index : Node_Id;
5391 Related_Id : Entity_Id := Empty;
5392 Nb_Index : Nat;
5393 P : constant Node_Id := Parent (Def);
5394 Priv : Entity_Id;
5396 begin
5397 if Nkind (Def) = N_Constrained_Array_Definition then
5398 Index := First (Discrete_Subtype_Definitions (Def));
5399 else
5400 Index := First (Subtype_Marks (Def));
5401 end if;
5403 -- Find proper names for the implicit types which may be public. In case
5404 -- of anonymous arrays we use the name of the first object of that type
5405 -- as prefix.
5407 if No (T) then
5408 Related_Id := Defining_Identifier (P);
5409 else
5410 Related_Id := T;
5411 end if;
5413 Nb_Index := 1;
5414 while Present (Index) loop
5415 Analyze (Index);
5417 -- Test for odd case of trying to index a type by the type itself
5419 if Is_Entity_Name (Index) and then Entity (Index) = T then
5420 Error_Msg_N ("type& cannot be indexed by itself", Index);
5421 Set_Entity (Index, Standard_Boolean);
5422 Set_Etype (Index, Standard_Boolean);
5423 end if;
5425 -- Check SPARK restriction requiring a subtype mark
5427 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5428 Check_SPARK_05_Restriction ("subtype mark required", Index);
5429 end if;
5431 -- Add a subtype declaration for each index of private array type
5432 -- declaration whose etype is also private. For example:
5434 -- package Pkg is
5435 -- type Index is private;
5436 -- private
5437 -- type Table is array (Index) of ...
5438 -- end;
5440 -- This is currently required by the expander for the internally
5441 -- generated equality subprogram of records with variant parts in
5442 -- which the etype of some component is such private type.
5444 if Ekind (Current_Scope) = E_Package
5445 and then In_Private_Part (Current_Scope)
5446 and then Has_Private_Declaration (Etype (Index))
5447 then
5448 declare
5449 Loc : constant Source_Ptr := Sloc (Def);
5450 New_E : Entity_Id;
5451 Decl : Entity_Id;
5453 begin
5454 New_E := Make_Temporary (Loc, 'T');
5455 Set_Is_Internal (New_E);
5457 Decl :=
5458 Make_Subtype_Declaration (Loc,
5459 Defining_Identifier => New_E,
5460 Subtype_Indication =>
5461 New_Occurrence_Of (Etype (Index), Loc));
5463 Insert_Before (Parent (Def), Decl);
5464 Analyze (Decl);
5465 Set_Etype (Index, New_E);
5467 -- If the index is a range the Entity attribute is not
5468 -- available. Example:
5470 -- package Pkg is
5471 -- type T is private;
5472 -- private
5473 -- type T is new Natural;
5474 -- Table : array (T(1) .. T(10)) of Boolean;
5475 -- end Pkg;
5477 if Nkind (Index) /= N_Range then
5478 Set_Entity (Index, New_E);
5479 end if;
5480 end;
5481 end if;
5483 Make_Index (Index, P, Related_Id, Nb_Index);
5485 -- Check error of subtype with predicate for index type
5487 Bad_Predicated_Subtype_Use
5488 ("subtype& has predicate, not allowed as index subtype",
5489 Index, Etype (Index));
5491 -- Move to next index
5493 Next_Index (Index);
5494 Nb_Index := Nb_Index + 1;
5495 end loop;
5497 -- Process subtype indication if one is present
5499 if Present (Component_Typ) then
5500 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5502 Set_Etype (Component_Typ, Element_Type);
5504 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5505 Check_SPARK_05_Restriction
5506 ("subtype mark required", Component_Typ);
5507 end if;
5509 -- Ada 2005 (AI-230): Access Definition case
5511 else pragma Assert (Present (Access_Definition (Component_Def)));
5513 -- Indicate that the anonymous access type is created by the
5514 -- array type declaration.
5516 Element_Type := Access_Definition
5517 (Related_Nod => P,
5518 N => Access_Definition (Component_Def));
5519 Set_Is_Local_Anonymous_Access (Element_Type);
5521 -- Propagate the parent. This field is needed if we have to generate
5522 -- the master_id associated with an anonymous access to task type
5523 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5525 Set_Parent (Element_Type, Parent (T));
5527 -- Ada 2005 (AI-230): In case of components that are anonymous access
5528 -- types the level of accessibility depends on the enclosing type
5529 -- declaration
5531 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5533 -- Ada 2005 (AI-254)
5535 declare
5536 CD : constant Node_Id :=
5537 Access_To_Subprogram_Definition
5538 (Access_Definition (Component_Def));
5539 begin
5540 if Present (CD) and then Protected_Present (CD) then
5541 Element_Type :=
5542 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5543 end if;
5544 end;
5545 end if;
5547 -- Constrained array case
5549 if No (T) then
5550 T := Create_Itype (E_Void, P, Related_Id, 'T');
5551 end if;
5553 if Nkind (Def) = N_Constrained_Array_Definition then
5555 -- Establish Implicit_Base as unconstrained base type
5557 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5559 Set_Etype (Implicit_Base, Implicit_Base);
5560 Set_Scope (Implicit_Base, Current_Scope);
5561 Set_Has_Delayed_Freeze (Implicit_Base);
5562 Set_Default_SSO (Implicit_Base);
5564 -- The constrained array type is a subtype of the unconstrained one
5566 Set_Ekind (T, E_Array_Subtype);
5567 Init_Size_Align (T);
5568 Set_Etype (T, Implicit_Base);
5569 Set_Scope (T, Current_Scope);
5570 Set_Is_Constrained (T);
5571 Set_First_Index (T,
5572 First (Discrete_Subtype_Definitions (Def)));
5573 Set_Has_Delayed_Freeze (T);
5575 -- Complete setup of implicit base type
5577 Set_First_Index (Implicit_Base, First_Index (T));
5578 Set_Component_Type (Implicit_Base, Element_Type);
5579 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5580 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5581 Set_Component_Size (Implicit_Base, Uint_0);
5582 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5583 Set_Has_Controlled_Component (Implicit_Base,
5584 Has_Controlled_Component (Element_Type)
5585 or else Is_Controlled_Active (Element_Type));
5586 Set_Finalize_Storage_Only (Implicit_Base,
5587 Finalize_Storage_Only (Element_Type));
5589 -- Inherit the "ghostness" from the constrained array type
5591 if Ghost_Mode > None or else Is_Ghost_Entity (T) then
5592 Set_Is_Ghost_Entity (Implicit_Base);
5593 end if;
5595 -- Unconstrained array case
5597 else
5598 Set_Ekind (T, E_Array_Type);
5599 Init_Size_Align (T);
5600 Set_Etype (T, T);
5601 Set_Scope (T, Current_Scope);
5602 Set_Component_Size (T, Uint_0);
5603 Set_Is_Constrained (T, False);
5604 Set_First_Index (T, First (Subtype_Marks (Def)));
5605 Set_Has_Delayed_Freeze (T, True);
5606 Set_Has_Task (T, Has_Task (Element_Type));
5607 Set_Has_Protected (T, Has_Protected (Element_Type));
5608 Set_Has_Controlled_Component (T, Has_Controlled_Component
5609 (Element_Type)
5610 or else
5611 Is_Controlled_Active (Element_Type));
5612 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5613 (Element_Type));
5614 Set_Default_SSO (T);
5615 end if;
5617 -- Common attributes for both cases
5619 Set_Component_Type (Base_Type (T), Element_Type);
5620 Set_Packed_Array_Impl_Type (T, Empty);
5622 if Aliased_Present (Component_Definition (Def)) then
5623 Check_SPARK_05_Restriction
5624 ("aliased is not allowed", Component_Definition (Def));
5625 Set_Has_Aliased_Components (Etype (T));
5626 end if;
5628 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5629 -- array type to ensure that objects of this type are initialized.
5631 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5632 Set_Can_Never_Be_Null (T);
5634 if Null_Exclusion_Present (Component_Definition (Def))
5636 -- No need to check itypes because in their case this check was
5637 -- done at their point of creation
5639 and then not Is_Itype (Element_Type)
5640 then
5641 Error_Msg_N
5642 ("`NOT NULL` not allowed (null already excluded)",
5643 Subtype_Indication (Component_Definition (Def)));
5644 end if;
5645 end if;
5647 Priv := Private_Component (Element_Type);
5649 if Present (Priv) then
5651 -- Check for circular definitions
5653 if Priv = Any_Type then
5654 Set_Component_Type (Etype (T), Any_Type);
5656 -- There is a gap in the visibility of operations on the composite
5657 -- type only if the component type is defined in a different scope.
5659 elsif Scope (Priv) = Current_Scope then
5660 null;
5662 elsif Is_Limited_Type (Priv) then
5663 Set_Is_Limited_Composite (Etype (T));
5664 Set_Is_Limited_Composite (T);
5665 else
5666 Set_Is_Private_Composite (Etype (T));
5667 Set_Is_Private_Composite (T);
5668 end if;
5669 end if;
5671 -- A syntax error in the declaration itself may lead to an empty index
5672 -- list, in which case do a minimal patch.
5674 if No (First_Index (T)) then
5675 Error_Msg_N ("missing index definition in array type declaration", T);
5677 declare
5678 Indexes : constant List_Id :=
5679 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5680 begin
5681 Set_Discrete_Subtype_Definitions (Def, Indexes);
5682 Set_First_Index (T, First (Indexes));
5683 return;
5684 end;
5685 end if;
5687 -- Create a concatenation operator for the new type. Internal array
5688 -- types created for packed entities do not need such, they are
5689 -- compatible with the user-defined type.
5691 if Number_Dimensions (T) = 1
5692 and then not Is_Packed_Array_Impl_Type (T)
5693 then
5694 New_Concatenation_Op (T);
5695 end if;
5697 -- In the case of an unconstrained array the parser has already verified
5698 -- that all the indexes are unconstrained but we still need to make sure
5699 -- that the element type is constrained.
5701 if not Is_Definite_Subtype (Element_Type) then
5702 Error_Msg_N
5703 ("unconstrained element type in array declaration",
5704 Subtype_Indication (Component_Def));
5706 elsif Is_Abstract_Type (Element_Type) then
5707 Error_Msg_N
5708 ("the type of a component cannot be abstract",
5709 Subtype_Indication (Component_Def));
5710 end if;
5712 -- There may be an invariant declared for the component type, but
5713 -- the construction of the component invariant checking procedure
5714 -- takes place during expansion.
5715 end Array_Type_Declaration;
5717 ------------------------------------------------------
5718 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5719 ------------------------------------------------------
5721 function Replace_Anonymous_Access_To_Protected_Subprogram
5722 (N : Node_Id) return Entity_Id
5724 Loc : constant Source_Ptr := Sloc (N);
5726 Curr_Scope : constant Scope_Stack_Entry :=
5727 Scope_Stack.Table (Scope_Stack.Last);
5729 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5731 Acc : Node_Id;
5732 -- Access definition in declaration
5734 Comp : Node_Id;
5735 -- Object definition or formal definition with an access definition
5737 Decl : Node_Id;
5738 -- Declaration of anonymous access to subprogram type
5740 Spec : Node_Id;
5741 -- Original specification in access to subprogram
5743 P : Node_Id;
5745 begin
5746 Set_Is_Internal (Anon);
5748 case Nkind (N) is
5749 when N_Component_Declaration |
5750 N_Unconstrained_Array_Definition |
5751 N_Constrained_Array_Definition =>
5752 Comp := Component_Definition (N);
5753 Acc := Access_Definition (Comp);
5755 when N_Discriminant_Specification =>
5756 Comp := Discriminant_Type (N);
5757 Acc := Comp;
5759 when N_Parameter_Specification =>
5760 Comp := Parameter_Type (N);
5761 Acc := Comp;
5763 when N_Access_Function_Definition =>
5764 Comp := Result_Definition (N);
5765 Acc := Comp;
5767 when N_Object_Declaration =>
5768 Comp := Object_Definition (N);
5769 Acc := Comp;
5771 when N_Function_Specification =>
5772 Comp := Result_Definition (N);
5773 Acc := Comp;
5775 when others =>
5776 raise Program_Error;
5777 end case;
5779 Spec := Access_To_Subprogram_Definition (Acc);
5781 Decl :=
5782 Make_Full_Type_Declaration (Loc,
5783 Defining_Identifier => Anon,
5784 Type_Definition => Copy_Separate_Tree (Spec));
5786 Mark_Rewrite_Insertion (Decl);
5788 -- In ASIS mode, analyze the profile on the original node, because
5789 -- the separate copy does not provide enough links to recover the
5790 -- original tree. Analysis is limited to type annotations, within
5791 -- a temporary scope that serves as an anonymous subprogram to collect
5792 -- otherwise useless temporaries and itypes.
5794 if ASIS_Mode then
5795 declare
5796 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5798 begin
5799 if Nkind (Spec) = N_Access_Function_Definition then
5800 Set_Ekind (Typ, E_Function);
5801 else
5802 Set_Ekind (Typ, E_Procedure);
5803 end if;
5805 Set_Parent (Typ, N);
5806 Set_Scope (Typ, Current_Scope);
5807 Push_Scope (Typ);
5809 -- Nothing to do if procedure is parameterless
5811 if Present (Parameter_Specifications (Spec)) then
5812 Process_Formals (Parameter_Specifications (Spec), Spec);
5813 end if;
5815 if Nkind (Spec) = N_Access_Function_Definition then
5816 declare
5817 Def : constant Node_Id := Result_Definition (Spec);
5819 begin
5820 -- The result might itself be an anonymous access type, so
5821 -- have to recurse.
5823 if Nkind (Def) = N_Access_Definition then
5824 if Present (Access_To_Subprogram_Definition (Def)) then
5825 Set_Etype
5826 (Def,
5827 Replace_Anonymous_Access_To_Protected_Subprogram
5828 (Spec));
5829 else
5830 Find_Type (Subtype_Mark (Def));
5831 end if;
5833 else
5834 Find_Type (Def);
5835 end if;
5836 end;
5837 end if;
5839 End_Scope;
5840 end;
5841 end if;
5843 -- Insert the new declaration in the nearest enclosing scope. If the
5844 -- node is a body and N is its return type, the declaration belongs in
5845 -- the enclosing scope.
5847 P := Parent (N);
5849 if Nkind (P) = N_Subprogram_Body
5850 and then Nkind (N) = N_Function_Specification
5851 then
5852 P := Parent (P);
5853 end if;
5855 while Present (P) and then not Has_Declarations (P) loop
5856 P := Parent (P);
5857 end loop;
5859 pragma Assert (Present (P));
5861 if Nkind (P) = N_Package_Specification then
5862 Prepend (Decl, Visible_Declarations (P));
5863 else
5864 Prepend (Decl, Declarations (P));
5865 end if;
5867 -- Replace the anonymous type with an occurrence of the new declaration.
5868 -- In all cases the rewritten node does not have the null-exclusion
5869 -- attribute because (if present) it was already inherited by the
5870 -- anonymous entity (Anon). Thus, in case of components we do not
5871 -- inherit this attribute.
5873 if Nkind (N) = N_Parameter_Specification then
5874 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5875 Set_Etype (Defining_Identifier (N), Anon);
5876 Set_Null_Exclusion_Present (N, False);
5878 elsif Nkind (N) = N_Object_Declaration then
5879 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5880 Set_Etype (Defining_Identifier (N), Anon);
5882 elsif Nkind (N) = N_Access_Function_Definition then
5883 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5885 elsif Nkind (N) = N_Function_Specification then
5886 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5887 Set_Etype (Defining_Unit_Name (N), Anon);
5889 else
5890 Rewrite (Comp,
5891 Make_Component_Definition (Loc,
5892 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5893 end if;
5895 Mark_Rewrite_Insertion (Comp);
5897 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
5898 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
5899 and then not Is_Type (Current_Scope))
5900 then
5902 -- Declaration can be analyzed in the current scope.
5904 Analyze (Decl);
5906 else
5907 -- Temporarily remove the current scope (record or subprogram) from
5908 -- the stack to add the new declarations to the enclosing scope.
5909 -- The anonymous entity is an Itype with the proper attributes.
5911 Scope_Stack.Decrement_Last;
5912 Analyze (Decl);
5913 Set_Is_Itype (Anon);
5914 Set_Associated_Node_For_Itype (Anon, N);
5915 Scope_Stack.Append (Curr_Scope);
5916 end if;
5918 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5919 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5920 return Anon;
5921 end Replace_Anonymous_Access_To_Protected_Subprogram;
5923 -------------------------------
5924 -- Build_Derived_Access_Type --
5925 -------------------------------
5927 procedure Build_Derived_Access_Type
5928 (N : Node_Id;
5929 Parent_Type : Entity_Id;
5930 Derived_Type : Entity_Id)
5932 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5934 Desig_Type : Entity_Id;
5935 Discr : Entity_Id;
5936 Discr_Con_Elist : Elist_Id;
5937 Discr_Con_El : Elmt_Id;
5938 Subt : Entity_Id;
5940 begin
5941 -- Set the designated type so it is available in case this is an access
5942 -- to a self-referential type, e.g. a standard list type with a next
5943 -- pointer. Will be reset after subtype is built.
5945 Set_Directly_Designated_Type
5946 (Derived_Type, Designated_Type (Parent_Type));
5948 Subt := Process_Subtype (S, N);
5950 if Nkind (S) /= N_Subtype_Indication
5951 and then Subt /= Base_Type (Subt)
5952 then
5953 Set_Ekind (Derived_Type, E_Access_Subtype);
5954 end if;
5956 if Ekind (Derived_Type) = E_Access_Subtype then
5957 declare
5958 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5959 Ibase : constant Entity_Id :=
5960 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5961 Svg_Chars : constant Name_Id := Chars (Ibase);
5962 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5964 begin
5965 Copy_Node (Pbase, Ibase);
5967 Set_Chars (Ibase, Svg_Chars);
5968 Set_Next_Entity (Ibase, Svg_Next_E);
5969 Set_Sloc (Ibase, Sloc (Derived_Type));
5970 Set_Scope (Ibase, Scope (Derived_Type));
5971 Set_Freeze_Node (Ibase, Empty);
5972 Set_Is_Frozen (Ibase, False);
5973 Set_Comes_From_Source (Ibase, False);
5974 Set_Is_First_Subtype (Ibase, False);
5976 Set_Etype (Ibase, Pbase);
5977 Set_Etype (Derived_Type, Ibase);
5978 end;
5979 end if;
5981 Set_Directly_Designated_Type
5982 (Derived_Type, Designated_Type (Subt));
5984 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5985 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5986 Set_Size_Info (Derived_Type, Parent_Type);
5987 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5988 Set_Depends_On_Private (Derived_Type,
5989 Has_Private_Component (Derived_Type));
5990 Conditional_Delay (Derived_Type, Subt);
5992 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5993 -- that it is not redundant.
5995 if Null_Exclusion_Present (Type_Definition (N)) then
5996 Set_Can_Never_Be_Null (Derived_Type);
5998 elsif Can_Never_Be_Null (Parent_Type) then
5999 Set_Can_Never_Be_Null (Derived_Type);
6000 end if;
6002 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6003 -- the root type for this information.
6005 -- Apply range checks to discriminants for derived record case
6006 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6008 Desig_Type := Designated_Type (Derived_Type);
6010 if Is_Composite_Type (Desig_Type)
6011 and then (not Is_Array_Type (Desig_Type))
6012 and then Has_Discriminants (Desig_Type)
6013 and then Base_Type (Desig_Type) /= Desig_Type
6014 then
6015 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6016 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6018 Discr := First_Discriminant (Base_Type (Desig_Type));
6019 while Present (Discr_Con_El) loop
6020 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6021 Next_Elmt (Discr_Con_El);
6022 Next_Discriminant (Discr);
6023 end loop;
6024 end if;
6025 end Build_Derived_Access_Type;
6027 ------------------------------
6028 -- Build_Derived_Array_Type --
6029 ------------------------------
6031 procedure Build_Derived_Array_Type
6032 (N : Node_Id;
6033 Parent_Type : Entity_Id;
6034 Derived_Type : Entity_Id)
6036 Loc : constant Source_Ptr := Sloc (N);
6037 Tdef : constant Node_Id := Type_Definition (N);
6038 Indic : constant Node_Id := Subtype_Indication (Tdef);
6039 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6040 Implicit_Base : Entity_Id;
6041 New_Indic : Node_Id;
6043 procedure Make_Implicit_Base;
6044 -- If the parent subtype is constrained, the derived type is a subtype
6045 -- of an implicit base type derived from the parent base.
6047 ------------------------
6048 -- Make_Implicit_Base --
6049 ------------------------
6051 procedure Make_Implicit_Base is
6052 begin
6053 Implicit_Base :=
6054 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6056 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6057 Set_Etype (Implicit_Base, Parent_Base);
6059 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6060 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6062 Set_Has_Delayed_Freeze (Implicit_Base, True);
6064 -- Inherit the "ghostness" from the parent base type
6066 if Ghost_Mode > None or else Is_Ghost_Entity (Parent_Base) then
6067 Set_Is_Ghost_Entity (Implicit_Base);
6068 end if;
6069 end Make_Implicit_Base;
6071 -- Start of processing for Build_Derived_Array_Type
6073 begin
6074 if not Is_Constrained (Parent_Type) then
6075 if Nkind (Indic) /= N_Subtype_Indication then
6076 Set_Ekind (Derived_Type, E_Array_Type);
6078 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6079 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6081 Set_Has_Delayed_Freeze (Derived_Type, True);
6083 else
6084 Make_Implicit_Base;
6085 Set_Etype (Derived_Type, Implicit_Base);
6087 New_Indic :=
6088 Make_Subtype_Declaration (Loc,
6089 Defining_Identifier => Derived_Type,
6090 Subtype_Indication =>
6091 Make_Subtype_Indication (Loc,
6092 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6093 Constraint => Constraint (Indic)));
6095 Rewrite (N, New_Indic);
6096 Analyze (N);
6097 end if;
6099 else
6100 if Nkind (Indic) /= N_Subtype_Indication then
6101 Make_Implicit_Base;
6103 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6104 Set_Etype (Derived_Type, Implicit_Base);
6105 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6107 else
6108 Error_Msg_N ("illegal constraint on constrained type", Indic);
6109 end if;
6110 end if;
6112 -- If parent type is not a derived type itself, and is declared in
6113 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6114 -- the new type's concatenation operator since Derive_Subprograms
6115 -- will not inherit the parent's operator. If the parent type is
6116 -- unconstrained, the operator is of the unconstrained base type.
6118 if Number_Dimensions (Parent_Type) = 1
6119 and then not Is_Limited_Type (Parent_Type)
6120 and then not Is_Derived_Type (Parent_Type)
6121 and then not Is_Package_Or_Generic_Package
6122 (Scope (Base_Type (Parent_Type)))
6123 then
6124 if not Is_Constrained (Parent_Type)
6125 and then Is_Constrained (Derived_Type)
6126 then
6127 New_Concatenation_Op (Implicit_Base);
6128 else
6129 New_Concatenation_Op (Derived_Type);
6130 end if;
6131 end if;
6132 end Build_Derived_Array_Type;
6134 -----------------------------------
6135 -- Build_Derived_Concurrent_Type --
6136 -----------------------------------
6138 procedure Build_Derived_Concurrent_Type
6139 (N : Node_Id;
6140 Parent_Type : Entity_Id;
6141 Derived_Type : Entity_Id)
6143 Loc : constant Source_Ptr := Sloc (N);
6145 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6146 Corr_Decl : Node_Id;
6147 Corr_Decl_Needed : Boolean;
6148 -- If the derived type has fewer discriminants than its parent, the
6149 -- corresponding record is also a derived type, in order to account for
6150 -- the bound discriminants. We create a full type declaration for it in
6151 -- this case.
6153 Constraint_Present : constant Boolean :=
6154 Nkind (Subtype_Indication (Type_Definition (N))) =
6155 N_Subtype_Indication;
6157 D_Constraint : Node_Id;
6158 New_Constraint : Elist_Id;
6159 Old_Disc : Entity_Id;
6160 New_Disc : Entity_Id;
6161 New_N : Node_Id;
6163 begin
6164 Set_Stored_Constraint (Derived_Type, No_Elist);
6165 Corr_Decl_Needed := False;
6166 Old_Disc := Empty;
6168 if Present (Discriminant_Specifications (N))
6169 and then Constraint_Present
6170 then
6171 Old_Disc := First_Discriminant (Parent_Type);
6172 New_Disc := First (Discriminant_Specifications (N));
6173 while Present (New_Disc) and then Present (Old_Disc) loop
6174 Next_Discriminant (Old_Disc);
6175 Next (New_Disc);
6176 end loop;
6177 end if;
6179 if Present (Old_Disc) and then Expander_Active then
6181 -- The new type has fewer discriminants, so we need to create a new
6182 -- corresponding record, which is derived from the corresponding
6183 -- record of the parent, and has a stored constraint that captures
6184 -- the values of the discriminant constraints. The corresponding
6185 -- record is needed only if expander is active and code generation is
6186 -- enabled.
6188 -- The type declaration for the derived corresponding record has the
6189 -- same discriminant part and constraints as the current declaration.
6190 -- Copy the unanalyzed tree to build declaration.
6192 Corr_Decl_Needed := True;
6193 New_N := Copy_Separate_Tree (N);
6195 Corr_Decl :=
6196 Make_Full_Type_Declaration (Loc,
6197 Defining_Identifier => Corr_Record,
6198 Discriminant_Specifications =>
6199 Discriminant_Specifications (New_N),
6200 Type_Definition =>
6201 Make_Derived_Type_Definition (Loc,
6202 Subtype_Indication =>
6203 Make_Subtype_Indication (Loc,
6204 Subtype_Mark =>
6205 New_Occurrence_Of
6206 (Corresponding_Record_Type (Parent_Type), Loc),
6207 Constraint =>
6208 Constraint
6209 (Subtype_Indication (Type_Definition (New_N))))));
6210 end if;
6212 -- Copy Storage_Size and Relative_Deadline variables if task case
6214 if Is_Task_Type (Parent_Type) then
6215 Set_Storage_Size_Variable (Derived_Type,
6216 Storage_Size_Variable (Parent_Type));
6217 Set_Relative_Deadline_Variable (Derived_Type,
6218 Relative_Deadline_Variable (Parent_Type));
6219 end if;
6221 if Present (Discriminant_Specifications (N)) then
6222 Push_Scope (Derived_Type);
6223 Check_Or_Process_Discriminants (N, Derived_Type);
6225 if Constraint_Present then
6226 New_Constraint :=
6227 Expand_To_Stored_Constraint
6228 (Parent_Type,
6229 Build_Discriminant_Constraints
6230 (Parent_Type,
6231 Subtype_Indication (Type_Definition (N)), True));
6232 end if;
6234 End_Scope;
6236 elsif Constraint_Present then
6238 -- Build constrained subtype, copying the constraint, and derive
6239 -- from it to create a derived constrained type.
6241 declare
6242 Loc : constant Source_Ptr := Sloc (N);
6243 Anon : constant Entity_Id :=
6244 Make_Defining_Identifier (Loc,
6245 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6246 Decl : Node_Id;
6248 begin
6249 Decl :=
6250 Make_Subtype_Declaration (Loc,
6251 Defining_Identifier => Anon,
6252 Subtype_Indication =>
6253 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6254 Insert_Before (N, Decl);
6255 Analyze (Decl);
6257 Rewrite (Subtype_Indication (Type_Definition (N)),
6258 New_Occurrence_Of (Anon, Loc));
6259 Set_Analyzed (Derived_Type, False);
6260 Analyze (N);
6261 return;
6262 end;
6263 end if;
6265 -- By default, operations and private data are inherited from parent.
6266 -- However, in the presence of bound discriminants, a new corresponding
6267 -- record will be created, see below.
6269 Set_Has_Discriminants
6270 (Derived_Type, Has_Discriminants (Parent_Type));
6271 Set_Corresponding_Record_Type
6272 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6274 -- Is_Constrained is set according the parent subtype, but is set to
6275 -- False if the derived type is declared with new discriminants.
6277 Set_Is_Constrained
6278 (Derived_Type,
6279 (Is_Constrained (Parent_Type) or else Constraint_Present)
6280 and then not Present (Discriminant_Specifications (N)));
6282 if Constraint_Present then
6283 if not Has_Discriminants (Parent_Type) then
6284 Error_Msg_N ("untagged parent must have discriminants", N);
6286 elsif Present (Discriminant_Specifications (N)) then
6288 -- Verify that new discriminants are used to constrain old ones
6290 D_Constraint :=
6291 First
6292 (Constraints
6293 (Constraint (Subtype_Indication (Type_Definition (N)))));
6295 Old_Disc := First_Discriminant (Parent_Type);
6297 while Present (D_Constraint) loop
6298 if Nkind (D_Constraint) /= N_Discriminant_Association then
6300 -- Positional constraint. If it is a reference to a new
6301 -- discriminant, it constrains the corresponding old one.
6303 if Nkind (D_Constraint) = N_Identifier then
6304 New_Disc := First_Discriminant (Derived_Type);
6305 while Present (New_Disc) loop
6306 exit when Chars (New_Disc) = Chars (D_Constraint);
6307 Next_Discriminant (New_Disc);
6308 end loop;
6310 if Present (New_Disc) then
6311 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6312 end if;
6313 end if;
6315 Next_Discriminant (Old_Disc);
6317 -- if this is a named constraint, search by name for the old
6318 -- discriminants constrained by the new one.
6320 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6322 -- Find new discriminant with that name
6324 New_Disc := First_Discriminant (Derived_Type);
6325 while Present (New_Disc) loop
6326 exit when
6327 Chars (New_Disc) = Chars (Expression (D_Constraint));
6328 Next_Discriminant (New_Disc);
6329 end loop;
6331 if Present (New_Disc) then
6333 -- Verify that new discriminant renames some discriminant
6334 -- of the parent type, and associate the new discriminant
6335 -- with one or more old ones that it renames.
6337 declare
6338 Selector : Node_Id;
6340 begin
6341 Selector := First (Selector_Names (D_Constraint));
6342 while Present (Selector) loop
6343 Old_Disc := First_Discriminant (Parent_Type);
6344 while Present (Old_Disc) loop
6345 exit when Chars (Old_Disc) = Chars (Selector);
6346 Next_Discriminant (Old_Disc);
6347 end loop;
6349 if Present (Old_Disc) then
6350 Set_Corresponding_Discriminant
6351 (New_Disc, Old_Disc);
6352 end if;
6354 Next (Selector);
6355 end loop;
6356 end;
6357 end if;
6358 end if;
6360 Next (D_Constraint);
6361 end loop;
6363 New_Disc := First_Discriminant (Derived_Type);
6364 while Present (New_Disc) loop
6365 if No (Corresponding_Discriminant (New_Disc)) then
6366 Error_Msg_NE
6367 ("new discriminant& must constrain old one", N, New_Disc);
6369 elsif not
6370 Subtypes_Statically_Compatible
6371 (Etype (New_Disc),
6372 Etype (Corresponding_Discriminant (New_Disc)))
6373 then
6374 Error_Msg_NE
6375 ("& not statically compatible with parent discriminant",
6376 N, New_Disc);
6377 end if;
6379 Next_Discriminant (New_Disc);
6380 end loop;
6381 end if;
6383 elsif Present (Discriminant_Specifications (N)) then
6384 Error_Msg_N
6385 ("missing discriminant constraint in untagged derivation", N);
6386 end if;
6388 -- The entity chain of the derived type includes the new discriminants
6389 -- but shares operations with the parent.
6391 if Present (Discriminant_Specifications (N)) then
6392 Old_Disc := First_Discriminant (Parent_Type);
6393 while Present (Old_Disc) loop
6394 if No (Next_Entity (Old_Disc))
6395 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6396 then
6397 Set_Next_Entity
6398 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6399 exit;
6400 end if;
6402 Next_Discriminant (Old_Disc);
6403 end loop;
6405 else
6406 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6407 if Has_Discriminants (Parent_Type) then
6408 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6409 Set_Discriminant_Constraint (
6410 Derived_Type, Discriminant_Constraint (Parent_Type));
6411 end if;
6412 end if;
6414 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6416 Set_Has_Completion (Derived_Type);
6418 if Corr_Decl_Needed then
6419 Set_Stored_Constraint (Derived_Type, New_Constraint);
6420 Insert_After (N, Corr_Decl);
6421 Analyze (Corr_Decl);
6422 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6423 end if;
6424 end Build_Derived_Concurrent_Type;
6426 ------------------------------------
6427 -- Build_Derived_Enumeration_Type --
6428 ------------------------------------
6430 procedure Build_Derived_Enumeration_Type
6431 (N : Node_Id;
6432 Parent_Type : Entity_Id;
6433 Derived_Type : Entity_Id)
6435 Loc : constant Source_Ptr := Sloc (N);
6436 Def : constant Node_Id := Type_Definition (N);
6437 Indic : constant Node_Id := Subtype_Indication (Def);
6438 Implicit_Base : Entity_Id;
6439 Literal : Entity_Id;
6440 New_Lit : Entity_Id;
6441 Literals_List : List_Id;
6442 Type_Decl : Node_Id;
6443 Hi, Lo : Node_Id;
6444 Rang_Expr : Node_Id;
6446 begin
6447 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6448 -- not have explicit literals lists we need to process types derived
6449 -- from them specially. This is handled by Derived_Standard_Character.
6450 -- If the parent type is a generic type, there are no literals either,
6451 -- and we construct the same skeletal representation as for the generic
6452 -- parent type.
6454 if Is_Standard_Character_Type (Parent_Type) then
6455 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6457 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6458 declare
6459 Lo : Node_Id;
6460 Hi : Node_Id;
6462 begin
6463 if Nkind (Indic) /= N_Subtype_Indication then
6464 Lo :=
6465 Make_Attribute_Reference (Loc,
6466 Attribute_Name => Name_First,
6467 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6468 Set_Etype (Lo, Derived_Type);
6470 Hi :=
6471 Make_Attribute_Reference (Loc,
6472 Attribute_Name => Name_Last,
6473 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6474 Set_Etype (Hi, Derived_Type);
6476 Set_Scalar_Range (Derived_Type,
6477 Make_Range (Loc,
6478 Low_Bound => Lo,
6479 High_Bound => Hi));
6480 else
6482 -- Analyze subtype indication and verify compatibility
6483 -- with parent type.
6485 if Base_Type (Process_Subtype (Indic, N)) /=
6486 Base_Type (Parent_Type)
6487 then
6488 Error_Msg_N
6489 ("illegal constraint for formal discrete type", N);
6490 end if;
6491 end if;
6492 end;
6494 else
6495 -- If a constraint is present, analyze the bounds to catch
6496 -- premature usage of the derived literals.
6498 if Nkind (Indic) = N_Subtype_Indication
6499 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6500 then
6501 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6502 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6503 end if;
6505 -- Introduce an implicit base type for the derived type even if there
6506 -- is no constraint attached to it, since this seems closer to the
6507 -- Ada semantics. Build a full type declaration tree for the derived
6508 -- type using the implicit base type as the defining identifier. The
6509 -- build a subtype declaration tree which applies the constraint (if
6510 -- any) have it replace the derived type declaration.
6512 Literal := First_Literal (Parent_Type);
6513 Literals_List := New_List;
6514 while Present (Literal)
6515 and then Ekind (Literal) = E_Enumeration_Literal
6516 loop
6517 -- Literals of the derived type have the same representation as
6518 -- those of the parent type, but this representation can be
6519 -- overridden by an explicit representation clause. Indicate
6520 -- that there is no explicit representation given yet. These
6521 -- derived literals are implicit operations of the new type,
6522 -- and can be overridden by explicit ones.
6524 if Nkind (Literal) = N_Defining_Character_Literal then
6525 New_Lit :=
6526 Make_Defining_Character_Literal (Loc, Chars (Literal));
6527 else
6528 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6529 end if;
6531 Set_Ekind (New_Lit, E_Enumeration_Literal);
6532 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6533 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6534 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6535 Set_Alias (New_Lit, Literal);
6536 Set_Is_Known_Valid (New_Lit, True);
6538 Append (New_Lit, Literals_List);
6539 Next_Literal (Literal);
6540 end loop;
6542 Implicit_Base :=
6543 Make_Defining_Identifier (Sloc (Derived_Type),
6544 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6546 -- Indicate the proper nature of the derived type. This must be done
6547 -- before analysis of the literals, to recognize cases when a literal
6548 -- may be hidden by a previous explicit function definition (cf.
6549 -- c83031a).
6551 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6552 Set_Etype (Derived_Type, Implicit_Base);
6554 Type_Decl :=
6555 Make_Full_Type_Declaration (Loc,
6556 Defining_Identifier => Implicit_Base,
6557 Discriminant_Specifications => No_List,
6558 Type_Definition =>
6559 Make_Enumeration_Type_Definition (Loc, Literals_List));
6561 Mark_Rewrite_Insertion (Type_Decl);
6562 Insert_Before (N, Type_Decl);
6563 Analyze (Type_Decl);
6565 -- The anonymous base now has a full declaration, but this base
6566 -- is not a first subtype.
6568 Set_Is_First_Subtype (Implicit_Base, False);
6570 -- After the implicit base is analyzed its Etype needs to be changed
6571 -- to reflect the fact that it is derived from the parent type which
6572 -- was ignored during analysis. We also set the size at this point.
6574 Set_Etype (Implicit_Base, Parent_Type);
6576 Set_Size_Info (Implicit_Base, Parent_Type);
6577 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6578 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6580 -- Copy other flags from parent type
6582 Set_Has_Non_Standard_Rep
6583 (Implicit_Base, Has_Non_Standard_Rep
6584 (Parent_Type));
6585 Set_Has_Pragma_Ordered
6586 (Implicit_Base, Has_Pragma_Ordered
6587 (Parent_Type));
6588 Set_Has_Delayed_Freeze (Implicit_Base);
6590 -- Process the subtype indication including a validation check on the
6591 -- constraint, if any. If a constraint is given, its bounds must be
6592 -- implicitly converted to the new type.
6594 if Nkind (Indic) = N_Subtype_Indication then
6595 declare
6596 R : constant Node_Id :=
6597 Range_Expression (Constraint (Indic));
6599 begin
6600 if Nkind (R) = N_Range then
6601 Hi := Build_Scalar_Bound
6602 (High_Bound (R), Parent_Type, Implicit_Base);
6603 Lo := Build_Scalar_Bound
6604 (Low_Bound (R), Parent_Type, Implicit_Base);
6606 else
6607 -- Constraint is a Range attribute. Replace with explicit
6608 -- mention of the bounds of the prefix, which must be a
6609 -- subtype.
6611 Analyze (Prefix (R));
6612 Hi :=
6613 Convert_To (Implicit_Base,
6614 Make_Attribute_Reference (Loc,
6615 Attribute_Name => Name_Last,
6616 Prefix =>
6617 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6619 Lo :=
6620 Convert_To (Implicit_Base,
6621 Make_Attribute_Reference (Loc,
6622 Attribute_Name => Name_First,
6623 Prefix =>
6624 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6625 end if;
6626 end;
6628 else
6629 Hi :=
6630 Build_Scalar_Bound
6631 (Type_High_Bound (Parent_Type),
6632 Parent_Type, Implicit_Base);
6633 Lo :=
6634 Build_Scalar_Bound
6635 (Type_Low_Bound (Parent_Type),
6636 Parent_Type, Implicit_Base);
6637 end if;
6639 Rang_Expr :=
6640 Make_Range (Loc,
6641 Low_Bound => Lo,
6642 High_Bound => Hi);
6644 -- If we constructed a default range for the case where no range
6645 -- was given, then the expressions in the range must not freeze
6646 -- since they do not correspond to expressions in the source.
6648 if Nkind (Indic) /= N_Subtype_Indication then
6649 Set_Must_Not_Freeze (Lo);
6650 Set_Must_Not_Freeze (Hi);
6651 Set_Must_Not_Freeze (Rang_Expr);
6652 end if;
6654 Rewrite (N,
6655 Make_Subtype_Declaration (Loc,
6656 Defining_Identifier => Derived_Type,
6657 Subtype_Indication =>
6658 Make_Subtype_Indication (Loc,
6659 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6660 Constraint =>
6661 Make_Range_Constraint (Loc,
6662 Range_Expression => Rang_Expr))));
6664 Analyze (N);
6666 -- Propagate the aspects from the original type declaration to the
6667 -- declaration of the implicit base.
6669 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6671 -- Apply a range check. Since this range expression doesn't have an
6672 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6673 -- this right???
6675 if Nkind (Indic) = N_Subtype_Indication then
6676 Apply_Range_Check
6677 (Range_Expression (Constraint (Indic)), Parent_Type,
6678 Source_Typ => Entity (Subtype_Mark (Indic)));
6679 end if;
6680 end if;
6681 end Build_Derived_Enumeration_Type;
6683 --------------------------------
6684 -- Build_Derived_Numeric_Type --
6685 --------------------------------
6687 procedure Build_Derived_Numeric_Type
6688 (N : Node_Id;
6689 Parent_Type : Entity_Id;
6690 Derived_Type : Entity_Id)
6692 Loc : constant Source_Ptr := Sloc (N);
6693 Tdef : constant Node_Id := Type_Definition (N);
6694 Indic : constant Node_Id := Subtype_Indication (Tdef);
6695 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6696 No_Constraint : constant Boolean := Nkind (Indic) /=
6697 N_Subtype_Indication;
6698 Implicit_Base : Entity_Id;
6700 Lo : Node_Id;
6701 Hi : Node_Id;
6703 begin
6704 -- Process the subtype indication including a validation check on
6705 -- the constraint if any.
6707 Discard_Node (Process_Subtype (Indic, N));
6709 -- Introduce an implicit base type for the derived type even if there
6710 -- is no constraint attached to it, since this seems closer to the Ada
6711 -- semantics.
6713 Implicit_Base :=
6714 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6716 Set_Etype (Implicit_Base, Parent_Base);
6717 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6718 Set_Size_Info (Implicit_Base, Parent_Base);
6719 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6720 Set_Parent (Implicit_Base, Parent (Derived_Type));
6721 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6723 -- Set RM Size for discrete type or decimal fixed-point type
6724 -- Ordinary fixed-point is excluded, why???
6726 if Is_Discrete_Type (Parent_Base)
6727 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6728 then
6729 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6730 end if;
6732 Set_Has_Delayed_Freeze (Implicit_Base);
6734 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6735 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6737 Set_Scalar_Range (Implicit_Base,
6738 Make_Range (Loc,
6739 Low_Bound => Lo,
6740 High_Bound => Hi));
6742 if Has_Infinities (Parent_Base) then
6743 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6744 end if;
6746 -- The Derived_Type, which is the entity of the declaration, is a
6747 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6748 -- absence of an explicit constraint.
6750 Set_Etype (Derived_Type, Implicit_Base);
6752 -- If we did not have a constraint, then the Ekind is set from the
6753 -- parent type (otherwise Process_Subtype has set the bounds)
6755 if No_Constraint then
6756 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6757 end if;
6759 -- If we did not have a range constraint, then set the range from the
6760 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6762 if No_Constraint or else not Has_Range_Constraint (Indic) then
6763 Set_Scalar_Range (Derived_Type,
6764 Make_Range (Loc,
6765 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6766 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6767 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6769 if Has_Infinities (Parent_Type) then
6770 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6771 end if;
6773 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6774 end if;
6776 Set_Is_Descendant_Of_Address (Derived_Type,
6777 Is_Descendant_Of_Address (Parent_Type));
6778 Set_Is_Descendant_Of_Address (Implicit_Base,
6779 Is_Descendant_Of_Address (Parent_Type));
6781 -- Set remaining type-specific fields, depending on numeric type
6783 if Is_Modular_Integer_Type (Parent_Type) then
6784 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6786 Set_Non_Binary_Modulus
6787 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6789 Set_Is_Known_Valid
6790 (Implicit_Base, Is_Known_Valid (Parent_Base));
6792 elsif Is_Floating_Point_Type (Parent_Type) then
6794 -- Digits of base type is always copied from the digits value of
6795 -- the parent base type, but the digits of the derived type will
6796 -- already have been set if there was a constraint present.
6798 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6799 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6801 if No_Constraint then
6802 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6803 end if;
6805 elsif Is_Fixed_Point_Type (Parent_Type) then
6807 -- Small of base type and derived type are always copied from the
6808 -- parent base type, since smalls never change. The delta of the
6809 -- base type is also copied from the parent base type. However the
6810 -- delta of the derived type will have been set already if a
6811 -- constraint was present.
6813 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6814 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6815 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6817 if No_Constraint then
6818 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6819 end if;
6821 -- The scale and machine radix in the decimal case are always
6822 -- copied from the parent base type.
6824 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6825 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6826 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6828 Set_Machine_Radix_10
6829 (Derived_Type, Machine_Radix_10 (Parent_Base));
6830 Set_Machine_Radix_10
6831 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6833 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6835 if No_Constraint then
6836 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6838 else
6839 -- the analysis of the subtype_indication sets the
6840 -- digits value of the derived type.
6842 null;
6843 end if;
6844 end if;
6845 end if;
6847 if Is_Integer_Type (Parent_Type) then
6848 Set_Has_Shift_Operator
6849 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6850 end if;
6852 -- The type of the bounds is that of the parent type, and they
6853 -- must be converted to the derived type.
6855 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6857 -- The implicit_base should be frozen when the derived type is frozen,
6858 -- but note that it is used in the conversions of the bounds. For fixed
6859 -- types we delay the determination of the bounds until the proper
6860 -- freezing point. For other numeric types this is rejected by GCC, for
6861 -- reasons that are currently unclear (???), so we choose to freeze the
6862 -- implicit base now. In the case of integers and floating point types
6863 -- this is harmless because subsequent representation clauses cannot
6864 -- affect anything, but it is still baffling that we cannot use the
6865 -- same mechanism for all derived numeric types.
6867 -- There is a further complication: actually some representation
6868 -- clauses can affect the implicit base type. For example, attribute
6869 -- definition clauses for stream-oriented attributes need to set the
6870 -- corresponding TSS entries on the base type, and this normally
6871 -- cannot be done after the base type is frozen, so the circuitry in
6872 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6873 -- and not use Set_TSS in this case.
6875 -- There are also consequences for the case of delayed representation
6876 -- aspects for some cases. For example, a Size aspect is delayed and
6877 -- should not be evaluated to the freeze point. This early freezing
6878 -- means that the size attribute evaluation happens too early???
6880 if Is_Fixed_Point_Type (Parent_Type) then
6881 Conditional_Delay (Implicit_Base, Parent_Type);
6882 else
6883 Freeze_Before (N, Implicit_Base);
6884 end if;
6885 end Build_Derived_Numeric_Type;
6887 --------------------------------
6888 -- Build_Derived_Private_Type --
6889 --------------------------------
6891 procedure Build_Derived_Private_Type
6892 (N : Node_Id;
6893 Parent_Type : Entity_Id;
6894 Derived_Type : Entity_Id;
6895 Is_Completion : Boolean;
6896 Derive_Subps : Boolean := True)
6898 Loc : constant Source_Ptr := Sloc (N);
6899 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6900 Par_Scope : constant Entity_Id := Scope (Par_Base);
6901 Full_N : constant Node_Id := New_Copy_Tree (N);
6902 Full_Der : Entity_Id := New_Copy (Derived_Type);
6903 Full_P : Entity_Id;
6905 procedure Build_Full_Derivation;
6906 -- Build full derivation, i.e. derive from the full view
6908 procedure Copy_And_Build;
6909 -- Copy derived type declaration, replace parent with its full view,
6910 -- and build derivation
6912 ---------------------------
6913 -- Build_Full_Derivation --
6914 ---------------------------
6916 procedure Build_Full_Derivation is
6917 begin
6918 -- If parent scope is not open, install the declarations
6920 if not In_Open_Scopes (Par_Scope) then
6921 Install_Private_Declarations (Par_Scope);
6922 Install_Visible_Declarations (Par_Scope);
6923 Copy_And_Build;
6924 Uninstall_Declarations (Par_Scope);
6926 -- If parent scope is open and in another unit, and parent has a
6927 -- completion, then the derivation is taking place in the visible
6928 -- part of a child unit. In that case retrieve the full view of
6929 -- the parent momentarily.
6931 elsif not In_Same_Source_Unit (N, Parent_Type) then
6932 Full_P := Full_View (Parent_Type);
6933 Exchange_Declarations (Parent_Type);
6934 Copy_And_Build;
6935 Exchange_Declarations (Full_P);
6937 -- Otherwise it is a local derivation
6939 else
6940 Copy_And_Build;
6941 end if;
6942 end Build_Full_Derivation;
6944 --------------------
6945 -- Copy_And_Build --
6946 --------------------
6948 procedure Copy_And_Build is
6949 Full_Parent : Entity_Id := Parent_Type;
6951 begin
6952 -- If the parent is itself derived from another private type,
6953 -- installing the private declarations has not affected its
6954 -- privacy status, so use its own full view explicitly.
6956 if Is_Private_Type (Full_Parent)
6957 and then Present (Full_View (Full_Parent))
6958 then
6959 Full_Parent := Full_View (Full_Parent);
6960 end if;
6962 -- And its underlying full view if necessary
6964 if Is_Private_Type (Full_Parent)
6965 and then Present (Underlying_Full_View (Full_Parent))
6966 then
6967 Full_Parent := Underlying_Full_View (Full_Parent);
6968 end if;
6970 -- For record, access and most enumeration types, derivation from
6971 -- the full view requires a fully-fledged declaration. In the other
6972 -- cases, just use an itype.
6974 if Ekind (Full_Parent) in Record_Kind
6975 or else Ekind (Full_Parent) in Access_Kind
6976 or else
6977 (Ekind (Full_Parent) in Enumeration_Kind
6978 and then not Is_Standard_Character_Type (Full_Parent)
6979 and then not Is_Generic_Type (Root_Type (Full_Parent)))
6980 then
6981 -- Copy and adjust declaration to provide a completion for what
6982 -- is originally a private declaration. Indicate that full view
6983 -- is internally generated.
6985 Set_Comes_From_Source (Full_N, False);
6986 Set_Comes_From_Source (Full_Der, False);
6987 Set_Parent (Full_Der, Full_N);
6988 Set_Defining_Identifier (Full_N, Full_Der);
6990 -- If there are no constraints, adjust the subtype mark
6992 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
6993 N_Subtype_Indication
6994 then
6995 Set_Subtype_Indication
6996 (Type_Definition (Full_N),
6997 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
6998 end if;
7000 Insert_After (N, Full_N);
7002 -- Build full view of derived type from full view of parent which
7003 -- is now installed. Subprograms have been derived on the partial
7004 -- view, the completion does not derive them anew.
7006 if Ekind (Full_Parent) in Record_Kind then
7008 -- If parent type is tagged, the completion inherits the proper
7009 -- primitive operations.
7011 if Is_Tagged_Type (Parent_Type) then
7012 Build_Derived_Record_Type
7013 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7014 else
7015 Build_Derived_Record_Type
7016 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7017 end if;
7019 else
7020 Build_Derived_Type
7021 (Full_N, Full_Parent, Full_Der,
7022 Is_Completion => False, Derive_Subps => False);
7023 end if;
7025 -- The full declaration has been introduced into the tree and
7026 -- processed in the step above. It should not be analyzed again
7027 -- (when encountered later in the current list of declarations)
7028 -- to prevent spurious name conflicts. The full entity remains
7029 -- invisible.
7031 Set_Analyzed (Full_N);
7033 else
7034 Full_Der :=
7035 Make_Defining_Identifier (Sloc (Derived_Type),
7036 Chars => Chars (Derived_Type));
7037 Set_Is_Itype (Full_Der);
7038 Set_Associated_Node_For_Itype (Full_Der, N);
7039 Set_Parent (Full_Der, N);
7040 Build_Derived_Type
7041 (N, Full_Parent, Full_Der,
7042 Is_Completion => False, Derive_Subps => False);
7043 end if;
7045 Set_Has_Private_Declaration (Full_Der);
7046 Set_Has_Private_Declaration (Derived_Type);
7048 Set_Scope (Full_Der, Scope (Derived_Type));
7049 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7050 Set_Has_Size_Clause (Full_Der, False);
7051 Set_Has_Alignment_Clause (Full_Der, False);
7052 Set_Has_Delayed_Freeze (Full_Der);
7053 Set_Is_Frozen (Full_Der, False);
7054 Set_Freeze_Node (Full_Der, Empty);
7055 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7056 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7058 -- The convention on the base type may be set in the private part
7059 -- and not propagated to the subtype until later, so we obtain the
7060 -- convention from the base type of the parent.
7062 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7063 end Copy_And_Build;
7065 -- Start of processing for Build_Derived_Private_Type
7067 begin
7068 if Is_Tagged_Type (Parent_Type) then
7069 Full_P := Full_View (Parent_Type);
7071 -- A type extension of a type with unknown discriminants is an
7072 -- indefinite type that the back-end cannot handle directly.
7073 -- We treat it as a private type, and build a completion that is
7074 -- derived from the full view of the parent, and hopefully has
7075 -- known discriminants.
7077 -- If the full view of the parent type has an underlying record view,
7078 -- use it to generate the underlying record view of this derived type
7079 -- (required for chains of derivations with unknown discriminants).
7081 -- Minor optimization: we avoid the generation of useless underlying
7082 -- record view entities if the private type declaration has unknown
7083 -- discriminants but its corresponding full view has no
7084 -- discriminants.
7086 if Has_Unknown_Discriminants (Parent_Type)
7087 and then Present (Full_P)
7088 and then (Has_Discriminants (Full_P)
7089 or else Present (Underlying_Record_View (Full_P)))
7090 and then not In_Open_Scopes (Par_Scope)
7091 and then Expander_Active
7092 then
7093 declare
7094 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7095 New_Ext : constant Node_Id :=
7096 Copy_Separate_Tree
7097 (Record_Extension_Part (Type_Definition (N)));
7098 Decl : Node_Id;
7100 begin
7101 Build_Derived_Record_Type
7102 (N, Parent_Type, Derived_Type, Derive_Subps);
7104 -- Build anonymous completion, as a derivation from the full
7105 -- view of the parent. This is not a completion in the usual
7106 -- sense, because the current type is not private.
7108 Decl :=
7109 Make_Full_Type_Declaration (Loc,
7110 Defining_Identifier => Full_Der,
7111 Type_Definition =>
7112 Make_Derived_Type_Definition (Loc,
7113 Subtype_Indication =>
7114 New_Copy_Tree
7115 (Subtype_Indication (Type_Definition (N))),
7116 Record_Extension_Part => New_Ext));
7118 -- If the parent type has an underlying record view, use it
7119 -- here to build the new underlying record view.
7121 if Present (Underlying_Record_View (Full_P)) then
7122 pragma Assert
7123 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7124 = N_Identifier);
7125 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7126 Underlying_Record_View (Full_P));
7127 end if;
7129 Install_Private_Declarations (Par_Scope);
7130 Install_Visible_Declarations (Par_Scope);
7131 Insert_Before (N, Decl);
7133 -- Mark entity as an underlying record view before analysis,
7134 -- to avoid generating the list of its primitive operations
7135 -- (which is not really required for this entity) and thus
7136 -- prevent spurious errors associated with missing overriding
7137 -- of abstract primitives (overridden only for Derived_Type).
7139 Set_Ekind (Full_Der, E_Record_Type);
7140 Set_Is_Underlying_Record_View (Full_Der);
7141 Set_Default_SSO (Full_Der);
7143 Analyze (Decl);
7145 pragma Assert (Has_Discriminants (Full_Der)
7146 and then not Has_Unknown_Discriminants (Full_Der));
7148 Uninstall_Declarations (Par_Scope);
7150 -- Freeze the underlying record view, to prevent generation of
7151 -- useless dispatching information, which is simply shared with
7152 -- the real derived type.
7154 Set_Is_Frozen (Full_Der);
7156 -- If the derived type has access discriminants, create
7157 -- references to their anonymous types now, to prevent
7158 -- back-end problems when their first use is in generated
7159 -- bodies of primitives.
7161 declare
7162 E : Entity_Id;
7164 begin
7165 E := First_Entity (Full_Der);
7167 while Present (E) loop
7168 if Ekind (E) = E_Discriminant
7169 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7170 then
7171 Build_Itype_Reference (Etype (E), Decl);
7172 end if;
7174 Next_Entity (E);
7175 end loop;
7176 end;
7178 -- Set up links between real entity and underlying record view
7180 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7181 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7182 end;
7184 -- If discriminants are known, build derived record
7186 else
7187 Build_Derived_Record_Type
7188 (N, Parent_Type, Derived_Type, Derive_Subps);
7189 end if;
7191 return;
7193 elsif Has_Discriminants (Parent_Type) then
7195 -- Build partial view of derived type from partial view of parent.
7196 -- This must be done before building the full derivation because the
7197 -- second derivation will modify the discriminants of the first and
7198 -- the discriminants are chained with the rest of the components in
7199 -- the full derivation.
7201 Build_Derived_Record_Type
7202 (N, Parent_Type, Derived_Type, Derive_Subps);
7204 -- Build the full derivation if this is not the anonymous derived
7205 -- base type created by Build_Derived_Record_Type in the constrained
7206 -- case (see point 5. of its head comment) since we build it for the
7207 -- derived subtype. And skip it for protected types altogether, as
7208 -- gigi does not use these types directly.
7210 if Present (Full_View (Parent_Type))
7211 and then not Is_Itype (Derived_Type)
7212 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7213 then
7214 declare
7215 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7216 Discr : Entity_Id;
7217 Last_Discr : Entity_Id;
7219 begin
7220 -- If this is not a completion, construct the implicit full
7221 -- view by deriving from the full view of the parent type.
7222 -- But if this is a completion, the derived private type
7223 -- being built is a full view and the full derivation can
7224 -- only be its underlying full view.
7226 Build_Full_Derivation;
7228 if not Is_Completion then
7229 Set_Full_View (Derived_Type, Full_Der);
7230 else
7231 Set_Underlying_Full_View (Derived_Type, Full_Der);
7232 end if;
7234 if not Is_Base_Type (Derived_Type) then
7235 Set_Full_View (Der_Base, Base_Type (Full_Der));
7236 end if;
7238 -- Copy the discriminant list from full view to the partial
7239 -- view (base type and its subtype). Gigi requires that the
7240 -- partial and full views have the same discriminants.
7242 -- Note that since the partial view points to discriminants
7243 -- in the full view, their scope will be that of the full
7244 -- view. This might cause some front end problems and need
7245 -- adjustment???
7247 Discr := First_Discriminant (Base_Type (Full_Der));
7248 Set_First_Entity (Der_Base, Discr);
7250 loop
7251 Last_Discr := Discr;
7252 Next_Discriminant (Discr);
7253 exit when No (Discr);
7254 end loop;
7256 Set_Last_Entity (Der_Base, Last_Discr);
7257 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7258 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7260 Set_Stored_Constraint
7261 (Full_Der, Stored_Constraint (Derived_Type));
7262 end;
7263 end if;
7265 elsif Present (Full_View (Parent_Type))
7266 and then Has_Discriminants (Full_View (Parent_Type))
7267 then
7268 if Has_Unknown_Discriminants (Parent_Type)
7269 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7270 N_Subtype_Indication
7271 then
7272 Error_Msg_N
7273 ("cannot constrain type with unknown discriminants",
7274 Subtype_Indication (Type_Definition (N)));
7275 return;
7276 end if;
7278 -- If this is not a completion, construct the implicit full view by
7279 -- deriving from the full view of the parent type. But if this is a
7280 -- completion, the derived private type being built is a full view
7281 -- and the full derivation can only be its underlying full view.
7283 Build_Full_Derivation;
7285 if not Is_Completion then
7286 Set_Full_View (Derived_Type, Full_Der);
7287 else
7288 Set_Underlying_Full_View (Derived_Type, Full_Der);
7289 end if;
7291 -- In any case, the primitive operations are inherited from the
7292 -- parent type, not from the internal full view.
7294 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7296 if Derive_Subps then
7297 Derive_Subprograms (Parent_Type, Derived_Type);
7298 end if;
7300 Set_Stored_Constraint (Derived_Type, No_Elist);
7301 Set_Is_Constrained
7302 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7304 else
7305 -- Untagged type, No discriminants on either view
7307 if Nkind (Subtype_Indication (Type_Definition (N))) =
7308 N_Subtype_Indication
7309 then
7310 Error_Msg_N
7311 ("illegal constraint on type without discriminants", N);
7312 end if;
7314 if Present (Discriminant_Specifications (N))
7315 and then Present (Full_View (Parent_Type))
7316 and then not Is_Tagged_Type (Full_View (Parent_Type))
7317 then
7318 Error_Msg_N ("cannot add discriminants to untagged type", N);
7319 end if;
7321 Set_Stored_Constraint (Derived_Type, No_Elist);
7322 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7323 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7324 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7325 (Parent_Type));
7326 Set_Has_Controlled_Component
7327 (Derived_Type, Has_Controlled_Component
7328 (Parent_Type));
7330 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7332 if not Is_Controlled_Active (Parent_Type) then
7333 Set_Finalize_Storage_Only
7334 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7335 end if;
7337 -- If this is not a completion, construct the implicit full view by
7338 -- deriving from the full view of the parent type.
7340 -- ??? If the parent is untagged private and its completion is
7341 -- tagged, this mechanism will not work because we cannot derive from
7342 -- the tagged full view unless we have an extension.
7344 if Present (Full_View (Parent_Type))
7345 and then not Is_Tagged_Type (Full_View (Parent_Type))
7346 and then not Is_Completion
7347 then
7348 Build_Full_Derivation;
7349 Set_Full_View (Derived_Type, Full_Der);
7350 end if;
7351 end if;
7353 Set_Has_Unknown_Discriminants (Derived_Type,
7354 Has_Unknown_Discriminants (Parent_Type));
7356 if Is_Private_Type (Derived_Type) then
7357 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7358 end if;
7360 -- If the parent base type is in scope, add the derived type to its
7361 -- list of private dependents, because its full view may become
7362 -- visible subsequently (in a nested private part, a body, or in a
7363 -- further child unit).
7365 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7366 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7368 -- Check for unusual case where a type completed by a private
7369 -- derivation occurs within a package nested in a child unit, and
7370 -- the parent is declared in an ancestor.
7372 if Is_Child_Unit (Scope (Current_Scope))
7373 and then Is_Completion
7374 and then In_Private_Part (Current_Scope)
7375 and then Scope (Parent_Type) /= Current_Scope
7377 -- Note that if the parent has a completion in the private part,
7378 -- (which is itself a derivation from some other private type)
7379 -- it is that completion that is visible, there is no full view
7380 -- available, and no special processing is needed.
7382 and then Present (Full_View (Parent_Type))
7383 then
7384 -- In this case, the full view of the parent type will become
7385 -- visible in the body of the enclosing child, and only then will
7386 -- the current type be possibly non-private. Build an underlying
7387 -- full view that will be installed when the enclosing child body
7388 -- is compiled.
7390 if Present (Underlying_Full_View (Derived_Type)) then
7391 Full_Der := Underlying_Full_View (Derived_Type);
7392 else
7393 Build_Full_Derivation;
7394 Set_Underlying_Full_View (Derived_Type, Full_Der);
7395 end if;
7397 -- The full view will be used to swap entities on entry/exit to
7398 -- the body, and must appear in the entity list for the package.
7400 Append_Entity (Full_Der, Scope (Derived_Type));
7401 end if;
7402 end if;
7403 end Build_Derived_Private_Type;
7405 -------------------------------
7406 -- Build_Derived_Record_Type --
7407 -------------------------------
7409 -- 1. INTRODUCTION
7411 -- Ideally we would like to use the same model of type derivation for
7412 -- tagged and untagged record types. Unfortunately this is not quite
7413 -- possible because the semantics of representation clauses is different
7414 -- for tagged and untagged records under inheritance. Consider the
7415 -- following:
7417 -- type R (...) is [tagged] record ... end record;
7418 -- type T (...) is new R (...) [with ...];
7420 -- The representation clauses for T can specify a completely different
7421 -- record layout from R's. Hence the same component can be placed in two
7422 -- very different positions in objects of type T and R. If R and T are
7423 -- tagged types, representation clauses for T can only specify the layout
7424 -- of non inherited components, thus components that are common in R and T
7425 -- have the same position in objects of type R and T.
7427 -- This has two implications. The first is that the entire tree for R's
7428 -- declaration needs to be copied for T in the untagged case, so that T
7429 -- can be viewed as a record type of its own with its own representation
7430 -- clauses. The second implication is the way we handle discriminants.
7431 -- Specifically, in the untagged case we need a way to communicate to Gigi
7432 -- what are the real discriminants in the record, while for the semantics
7433 -- we need to consider those introduced by the user to rename the
7434 -- discriminants in the parent type. This is handled by introducing the
7435 -- notion of stored discriminants. See below for more.
7437 -- Fortunately the way regular components are inherited can be handled in
7438 -- the same way in tagged and untagged types.
7440 -- To complicate things a bit more the private view of a private extension
7441 -- cannot be handled in the same way as the full view (for one thing the
7442 -- semantic rules are somewhat different). We will explain what differs
7443 -- below.
7445 -- 2. DISCRIMINANTS UNDER INHERITANCE
7447 -- The semantic rules governing the discriminants of derived types are
7448 -- quite subtle.
7450 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7451 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7453 -- If parent type has discriminants, then the discriminants that are
7454 -- declared in the derived type are [3.4 (11)]:
7456 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7457 -- there is one;
7459 -- o Otherwise, each discriminant of the parent type (implicitly declared
7460 -- in the same order with the same specifications). In this case, the
7461 -- discriminants are said to be "inherited", or if unknown in the parent
7462 -- are also unknown in the derived type.
7464 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7466 -- o The parent subtype must be constrained;
7468 -- o If the parent type is not a tagged type, then each discriminant of
7469 -- the derived type must be used in the constraint defining a parent
7470 -- subtype. [Implementation note: This ensures that the new discriminant
7471 -- can share storage with an existing discriminant.]
7473 -- For the derived type each discriminant of the parent type is either
7474 -- inherited, constrained to equal some new discriminant of the derived
7475 -- type, or constrained to the value of an expression.
7477 -- When inherited or constrained to equal some new discriminant, the
7478 -- parent discriminant and the discriminant of the derived type are said
7479 -- to "correspond".
7481 -- If a discriminant of the parent type is constrained to a specific value
7482 -- in the derived type definition, then the discriminant is said to be
7483 -- "specified" by that derived type definition.
7485 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7487 -- We have spoken about stored discriminants in point 1 (introduction)
7488 -- above. There are two sort of stored discriminants: implicit and
7489 -- explicit. As long as the derived type inherits the same discriminants as
7490 -- the root record type, stored discriminants are the same as regular
7491 -- discriminants, and are said to be implicit. However, if any discriminant
7492 -- in the root type was renamed in the derived type, then the derived
7493 -- type will contain explicit stored discriminants. Explicit stored
7494 -- discriminants are discriminants in addition to the semantically visible
7495 -- discriminants defined for the derived type. Stored discriminants are
7496 -- used by Gigi to figure out what are the physical discriminants in
7497 -- objects of the derived type (see precise definition in einfo.ads).
7498 -- As an example, consider the following:
7500 -- type R (D1, D2, D3 : Int) is record ... end record;
7501 -- type T1 is new R;
7502 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7503 -- type T3 is new T2;
7504 -- type T4 (Y : Int) is new T3 (Y, 99);
7506 -- The following table summarizes the discriminants and stored
7507 -- discriminants in R and T1 through T4.
7509 -- Type Discrim Stored Discrim Comment
7510 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7511 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7512 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7513 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7514 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7516 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7517 -- find the corresponding discriminant in the parent type, while
7518 -- Original_Record_Component (abbreviated ORC below), the actual physical
7519 -- component that is renamed. Finally the field Is_Completely_Hidden
7520 -- (abbreviated ICH below) is set for all explicit stored discriminants
7521 -- (see einfo.ads for more info). For the above example this gives:
7523 -- Discrim CD ORC ICH
7524 -- ^^^^^^^ ^^ ^^^ ^^^
7525 -- D1 in R empty itself no
7526 -- D2 in R empty itself no
7527 -- D3 in R empty itself no
7529 -- D1 in T1 D1 in R itself no
7530 -- D2 in T1 D2 in R itself no
7531 -- D3 in T1 D3 in R itself no
7533 -- X1 in T2 D3 in T1 D3 in T2 no
7534 -- X2 in T2 D1 in T1 D1 in T2 no
7535 -- D1 in T2 empty itself yes
7536 -- D2 in T2 empty itself yes
7537 -- D3 in T2 empty itself yes
7539 -- X1 in T3 X1 in T2 D3 in T3 no
7540 -- X2 in T3 X2 in T2 D1 in T3 no
7541 -- D1 in T3 empty itself yes
7542 -- D2 in T3 empty itself yes
7543 -- D3 in T3 empty itself yes
7545 -- Y in T4 X1 in T3 D3 in T3 no
7546 -- D1 in T3 empty itself yes
7547 -- D2 in T3 empty itself yes
7548 -- D3 in T3 empty itself yes
7550 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7552 -- Type derivation for tagged types is fairly straightforward. If no
7553 -- discriminants are specified by the derived type, these are inherited
7554 -- from the parent. No explicit stored discriminants are ever necessary.
7555 -- The only manipulation that is done to the tree is that of adding a
7556 -- _parent field with parent type and constrained to the same constraint
7557 -- specified for the parent in the derived type definition. For instance:
7559 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7560 -- type T1 is new R with null record;
7561 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7563 -- are changed into:
7565 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7566 -- _parent : R (D1, D2, D3);
7567 -- end record;
7569 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7570 -- _parent : T1 (X2, 88, X1);
7571 -- end record;
7573 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7574 -- ORC and ICH fields are:
7576 -- Discrim CD ORC ICH
7577 -- ^^^^^^^ ^^ ^^^ ^^^
7578 -- D1 in R empty itself no
7579 -- D2 in R empty itself no
7580 -- D3 in R empty itself no
7582 -- D1 in T1 D1 in R D1 in R no
7583 -- D2 in T1 D2 in R D2 in R no
7584 -- D3 in T1 D3 in R D3 in R no
7586 -- X1 in T2 D3 in T1 D3 in R no
7587 -- X2 in T2 D1 in T1 D1 in R no
7589 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7591 -- Regardless of whether we dealing with a tagged or untagged type
7592 -- we will transform all derived type declarations of the form
7594 -- type T is new R (...) [with ...];
7595 -- or
7596 -- subtype S is R (...);
7597 -- type T is new S [with ...];
7598 -- into
7599 -- type BT is new R [with ...];
7600 -- subtype T is BT (...);
7602 -- That is, the base derived type is constrained only if it has no
7603 -- discriminants. The reason for doing this is that GNAT's semantic model
7604 -- assumes that a base type with discriminants is unconstrained.
7606 -- Note that, strictly speaking, the above transformation is not always
7607 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7609 -- procedure B34011A is
7610 -- type REC (D : integer := 0) is record
7611 -- I : Integer;
7612 -- end record;
7614 -- package P is
7615 -- type T6 is new Rec;
7616 -- function F return T6;
7617 -- end P;
7619 -- use P;
7620 -- package Q6 is
7621 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7622 -- end Q6;
7624 -- The definition of Q6.U is illegal. However transforming Q6.U into
7626 -- type BaseU is new T6;
7627 -- subtype U is BaseU (Q6.F.I)
7629 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7630 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7631 -- the transformation described above.
7633 -- There is another instance where the above transformation is incorrect.
7634 -- Consider:
7636 -- package Pack is
7637 -- type Base (D : Integer) is tagged null record;
7638 -- procedure P (X : Base);
7640 -- type Der is new Base (2) with null record;
7641 -- procedure P (X : Der);
7642 -- end Pack;
7644 -- Then the above transformation turns this into
7646 -- type Der_Base is new Base with null record;
7647 -- -- procedure P (X : Base) is implicitly inherited here
7648 -- -- as procedure P (X : Der_Base).
7650 -- subtype Der is Der_Base (2);
7651 -- procedure P (X : Der);
7652 -- -- The overriding of P (X : Der_Base) is illegal since we
7653 -- -- have a parameter conformance problem.
7655 -- To get around this problem, after having semantically processed Der_Base
7656 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7657 -- Discriminant_Constraint from Der so that when parameter conformance is
7658 -- checked when P is overridden, no semantic errors are flagged.
7660 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7662 -- Regardless of whether we are dealing with a tagged or untagged type
7663 -- we will transform all derived type declarations of the form
7665 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7666 -- type T is new R [with ...];
7667 -- into
7668 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7670 -- The reason for such transformation is that it allows us to implement a
7671 -- very clean form of component inheritance as explained below.
7673 -- Note that this transformation is not achieved by direct tree rewriting
7674 -- and manipulation, but rather by redoing the semantic actions that the
7675 -- above transformation will entail. This is done directly in routine
7676 -- Inherit_Components.
7678 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7680 -- In both tagged and untagged derived types, regular non discriminant
7681 -- components are inherited in the derived type from the parent type. In
7682 -- the absence of discriminants component, inheritance is straightforward
7683 -- as components can simply be copied from the parent.
7685 -- If the parent has discriminants, inheriting components constrained with
7686 -- these discriminants requires caution. Consider the following example:
7688 -- type R (D1, D2 : Positive) is [tagged] record
7689 -- S : String (D1 .. D2);
7690 -- end record;
7692 -- type T1 is new R [with null record];
7693 -- type T2 (X : positive) is new R (1, X) [with null record];
7695 -- As explained in 6. above, T1 is rewritten as
7696 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7697 -- which makes the treatment for T1 and T2 identical.
7699 -- What we want when inheriting S, is that references to D1 and D2 in R are
7700 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7701 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7702 -- with either discriminant references in the derived type or expressions.
7703 -- This replacement is achieved as follows: before inheriting R's
7704 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7705 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7706 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7707 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7708 -- by String (1 .. X).
7710 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7712 -- We explain here the rules governing private type extensions relevant to
7713 -- type derivation. These rules are explained on the following example:
7715 -- type D [(...)] is new A [(...)] with private; <-- partial view
7716 -- type D [(...)] is new P [(...)] with null record; <-- full view
7718 -- Type A is called the ancestor subtype of the private extension.
7719 -- Type P is the parent type of the full view of the private extension. It
7720 -- must be A or a type derived from A.
7722 -- The rules concerning the discriminants of private type extensions are
7723 -- [7.3(10-13)]:
7725 -- o If a private extension inherits known discriminants from the ancestor
7726 -- subtype, then the full view must also inherit its discriminants from
7727 -- the ancestor subtype and the parent subtype of the full view must be
7728 -- constrained if and only if the ancestor subtype is constrained.
7730 -- o If a partial view has unknown discriminants, then the full view may
7731 -- define a definite or an indefinite subtype, with or without
7732 -- discriminants.
7734 -- o If a partial view has neither known nor unknown discriminants, then
7735 -- the full view must define a definite subtype.
7737 -- o If the ancestor subtype of a private extension has constrained
7738 -- discriminants, then the parent subtype of the full view must impose a
7739 -- statically matching constraint on those discriminants.
7741 -- This means that only the following forms of private extensions are
7742 -- allowed:
7744 -- type D is new A with private; <-- partial view
7745 -- type D is new P with null record; <-- full view
7747 -- If A has no discriminants than P has no discriminants, otherwise P must
7748 -- inherit A's discriminants.
7750 -- type D is new A (...) with private; <-- partial view
7751 -- type D is new P (:::) with null record; <-- full view
7753 -- P must inherit A's discriminants and (...) and (:::) must statically
7754 -- match.
7756 -- subtype A is R (...);
7757 -- type D is new A with private; <-- partial view
7758 -- type D is new P with null record; <-- full view
7760 -- P must have inherited R's discriminants and must be derived from A or
7761 -- any of its subtypes.
7763 -- type D (..) is new A with private; <-- partial view
7764 -- type D (..) is new P [(:::)] with null record; <-- full view
7766 -- No specific constraints on P's discriminants or constraint (:::).
7767 -- Note that A can be unconstrained, but the parent subtype P must either
7768 -- be constrained or (:::) must be present.
7770 -- type D (..) is new A [(...)] with private; <-- partial view
7771 -- type D (..) is new P [(:::)] with null record; <-- full view
7773 -- P's constraints on A's discriminants must statically match those
7774 -- imposed by (...).
7776 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7778 -- The full view of a private extension is handled exactly as described
7779 -- above. The model chose for the private view of a private extension is
7780 -- the same for what concerns discriminants (i.e. they receive the same
7781 -- treatment as in the tagged case). However, the private view of the
7782 -- private extension always inherits the components of the parent base,
7783 -- without replacing any discriminant reference. Strictly speaking this is
7784 -- incorrect. However, Gigi never uses this view to generate code so this
7785 -- is a purely semantic issue. In theory, a set of transformations similar
7786 -- to those given in 5. and 6. above could be applied to private views of
7787 -- private extensions to have the same model of component inheritance as
7788 -- for non private extensions. However, this is not done because it would
7789 -- further complicate private type processing. Semantically speaking, this
7790 -- leaves us in an uncomfortable situation. As an example consider:
7792 -- package Pack is
7793 -- type R (D : integer) is tagged record
7794 -- S : String (1 .. D);
7795 -- end record;
7796 -- procedure P (X : R);
7797 -- type T is new R (1) with private;
7798 -- private
7799 -- type T is new R (1) with null record;
7800 -- end;
7802 -- This is transformed into:
7804 -- package Pack is
7805 -- type R (D : integer) is tagged record
7806 -- S : String (1 .. D);
7807 -- end record;
7808 -- procedure P (X : R);
7809 -- type T is new R (1) with private;
7810 -- private
7811 -- type BaseT is new R with null record;
7812 -- subtype T is BaseT (1);
7813 -- end;
7815 -- (strictly speaking the above is incorrect Ada)
7817 -- From the semantic standpoint the private view of private extension T
7818 -- should be flagged as constrained since one can clearly have
7820 -- Obj : T;
7822 -- in a unit withing Pack. However, when deriving subprograms for the
7823 -- private view of private extension T, T must be seen as unconstrained
7824 -- since T has discriminants (this is a constraint of the current
7825 -- subprogram derivation model). Thus, when processing the private view of
7826 -- a private extension such as T, we first mark T as unconstrained, we
7827 -- process it, we perform program derivation and just before returning from
7828 -- Build_Derived_Record_Type we mark T as constrained.
7830 -- ??? Are there are other uncomfortable cases that we will have to
7831 -- deal with.
7833 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7835 -- Types that are derived from a visible record type and have a private
7836 -- extension present other peculiarities. They behave mostly like private
7837 -- types, but if they have primitive operations defined, these will not
7838 -- have the proper signatures for further inheritance, because other
7839 -- primitive operations will use the implicit base that we define for
7840 -- private derivations below. This affect subprogram inheritance (see
7841 -- Derive_Subprograms for details). We also derive the implicit base from
7842 -- the base type of the full view, so that the implicit base is a record
7843 -- type and not another private type, This avoids infinite loops.
7845 procedure Build_Derived_Record_Type
7846 (N : Node_Id;
7847 Parent_Type : Entity_Id;
7848 Derived_Type : Entity_Id;
7849 Derive_Subps : Boolean := True)
7851 Discriminant_Specs : constant Boolean :=
7852 Present (Discriminant_Specifications (N));
7853 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7854 Loc : constant Source_Ptr := Sloc (N);
7855 Private_Extension : constant Boolean :=
7856 Nkind (N) = N_Private_Extension_Declaration;
7857 Assoc_List : Elist_Id;
7858 Constraint_Present : Boolean;
7859 Constrs : Elist_Id;
7860 Discrim : Entity_Id;
7861 Indic : Node_Id;
7862 Inherit_Discrims : Boolean := False;
7863 Last_Discrim : Entity_Id;
7864 New_Base : Entity_Id;
7865 New_Decl : Node_Id;
7866 New_Discrs : Elist_Id;
7867 New_Indic : Node_Id;
7868 Parent_Base : Entity_Id;
7869 Save_Etype : Entity_Id;
7870 Save_Discr_Constr : Elist_Id;
7871 Save_Next_Entity : Entity_Id;
7872 Type_Def : Node_Id;
7874 Discs : Elist_Id := New_Elmt_List;
7875 -- An empty Discs list means that there were no constraints in the
7876 -- subtype indication or that there was an error processing it.
7878 begin
7879 if Ekind (Parent_Type) = E_Record_Type_With_Private
7880 and then Present (Full_View (Parent_Type))
7881 and then Has_Discriminants (Parent_Type)
7882 then
7883 Parent_Base := Base_Type (Full_View (Parent_Type));
7884 else
7885 Parent_Base := Base_Type (Parent_Type);
7886 end if;
7888 -- AI05-0115 : if this is a derivation from a private type in some
7889 -- other scope that may lead to invisible components for the derived
7890 -- type, mark it accordingly.
7892 if Is_Private_Type (Parent_Type) then
7893 if Scope (Parent_Type) = Scope (Derived_Type) then
7894 null;
7896 elsif In_Open_Scopes (Scope (Parent_Type))
7897 and then In_Private_Part (Scope (Parent_Type))
7898 then
7899 null;
7901 else
7902 Set_Has_Private_Ancestor (Derived_Type);
7903 end if;
7905 else
7906 Set_Has_Private_Ancestor
7907 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7908 end if;
7910 -- Before we start the previously documented transformations, here is
7911 -- little fix for size and alignment of tagged types. Normally when we
7912 -- derive type D from type P, we copy the size and alignment of P as the
7913 -- default for D, and in the absence of explicit representation clauses
7914 -- for D, the size and alignment are indeed the same as the parent.
7916 -- But this is wrong for tagged types, since fields may be added, and
7917 -- the default size may need to be larger, and the default alignment may
7918 -- need to be larger.
7920 -- We therefore reset the size and alignment fields in the tagged case.
7921 -- Note that the size and alignment will in any case be at least as
7922 -- large as the parent type (since the derived type has a copy of the
7923 -- parent type in the _parent field)
7925 -- The type is also marked as being tagged here, which is needed when
7926 -- processing components with a self-referential anonymous access type
7927 -- in the call to Check_Anonymous_Access_Components below. Note that
7928 -- this flag is also set later on for completeness.
7930 if Is_Tagged then
7931 Set_Is_Tagged_Type (Derived_Type);
7932 Init_Size_Align (Derived_Type);
7933 end if;
7935 -- STEP 0a: figure out what kind of derived type declaration we have
7937 if Private_Extension then
7938 Type_Def := N;
7939 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7940 Set_Default_SSO (Derived_Type);
7942 else
7943 Type_Def := Type_Definition (N);
7945 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7946 -- Parent_Base can be a private type or private extension. However,
7947 -- for tagged types with an extension the newly added fields are
7948 -- visible and hence the Derived_Type is always an E_Record_Type.
7949 -- (except that the parent may have its own private fields).
7950 -- For untagged types we preserve the Ekind of the Parent_Base.
7952 if Present (Record_Extension_Part (Type_Def)) then
7953 Set_Ekind (Derived_Type, E_Record_Type);
7954 Set_Default_SSO (Derived_Type);
7956 -- Create internal access types for components with anonymous
7957 -- access types.
7959 if Ada_Version >= Ada_2005 then
7960 Check_Anonymous_Access_Components
7961 (N, Derived_Type, Derived_Type,
7962 Component_List (Record_Extension_Part (Type_Def)));
7963 end if;
7965 else
7966 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7967 end if;
7968 end if;
7970 -- Indic can either be an N_Identifier if the subtype indication
7971 -- contains no constraint or an N_Subtype_Indication if the subtype
7972 -- indication has a constraint.
7974 Indic := Subtype_Indication (Type_Def);
7975 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7977 -- Check that the type has visible discriminants. The type may be
7978 -- a private type with unknown discriminants whose full view has
7979 -- discriminants which are invisible.
7981 if Constraint_Present then
7982 if not Has_Discriminants (Parent_Base)
7983 or else
7984 (Has_Unknown_Discriminants (Parent_Base)
7985 and then Is_Private_Type (Parent_Base))
7986 then
7987 Error_Msg_N
7988 ("invalid constraint: type has no discriminant",
7989 Constraint (Indic));
7991 Constraint_Present := False;
7992 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7994 elsif Is_Constrained (Parent_Type) then
7995 Error_Msg_N
7996 ("invalid constraint: parent type is already constrained",
7997 Constraint (Indic));
7999 Constraint_Present := False;
8000 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8001 end if;
8002 end if;
8004 -- STEP 0b: If needed, apply transformation given in point 5. above
8006 if not Private_Extension
8007 and then Has_Discriminants (Parent_Type)
8008 and then not Discriminant_Specs
8009 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8010 then
8011 -- First, we must analyze the constraint (see comment in point 5.)
8012 -- The constraint may come from the subtype indication of the full
8013 -- declaration.
8015 if Constraint_Present then
8016 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8018 -- If there is no explicit constraint, there might be one that is
8019 -- inherited from a constrained parent type. In that case verify that
8020 -- it conforms to the constraint in the partial view. In perverse
8021 -- cases the parent subtypes of the partial and full view can have
8022 -- different constraints.
8024 elsif Present (Stored_Constraint (Parent_Type)) then
8025 New_Discrs := Stored_Constraint (Parent_Type);
8027 else
8028 New_Discrs := No_Elist;
8029 end if;
8031 if Has_Discriminants (Derived_Type)
8032 and then Has_Private_Declaration (Derived_Type)
8033 and then Present (Discriminant_Constraint (Derived_Type))
8034 and then Present (New_Discrs)
8035 then
8036 -- Verify that constraints of the full view statically match
8037 -- those given in the partial view.
8039 declare
8040 C1, C2 : Elmt_Id;
8042 begin
8043 C1 := First_Elmt (New_Discrs);
8044 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8045 while Present (C1) and then Present (C2) loop
8046 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8047 or else
8048 (Is_OK_Static_Expression (Node (C1))
8049 and then Is_OK_Static_Expression (Node (C2))
8050 and then
8051 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8052 then
8053 null;
8055 else
8056 if Constraint_Present then
8057 Error_Msg_N
8058 ("constraint not conformant to previous declaration",
8059 Node (C1));
8060 else
8061 Error_Msg_N
8062 ("constraint of full view is incompatible "
8063 & "with partial view", N);
8064 end if;
8065 end if;
8067 Next_Elmt (C1);
8068 Next_Elmt (C2);
8069 end loop;
8070 end;
8071 end if;
8073 -- Insert and analyze the declaration for the unconstrained base type
8075 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8077 New_Decl :=
8078 Make_Full_Type_Declaration (Loc,
8079 Defining_Identifier => New_Base,
8080 Type_Definition =>
8081 Make_Derived_Type_Definition (Loc,
8082 Abstract_Present => Abstract_Present (Type_Def),
8083 Limited_Present => Limited_Present (Type_Def),
8084 Subtype_Indication =>
8085 New_Occurrence_Of (Parent_Base, Loc),
8086 Record_Extension_Part =>
8087 Relocate_Node (Record_Extension_Part (Type_Def)),
8088 Interface_List => Interface_List (Type_Def)));
8090 Set_Parent (New_Decl, Parent (N));
8091 Mark_Rewrite_Insertion (New_Decl);
8092 Insert_Before (N, New_Decl);
8094 -- In the extension case, make sure ancestor is frozen appropriately
8095 -- (see also non-discriminated case below).
8097 if Present (Record_Extension_Part (Type_Def))
8098 or else Is_Interface (Parent_Base)
8099 then
8100 Freeze_Before (New_Decl, Parent_Type);
8101 end if;
8103 -- Note that this call passes False for the Derive_Subps parameter
8104 -- because subprogram derivation is deferred until after creating
8105 -- the subtype (see below).
8107 Build_Derived_Type
8108 (New_Decl, Parent_Base, New_Base,
8109 Is_Completion => False, Derive_Subps => False);
8111 -- ??? This needs re-examination to determine whether the
8112 -- above call can simply be replaced by a call to Analyze.
8114 Set_Analyzed (New_Decl);
8116 -- Insert and analyze the declaration for the constrained subtype
8118 if Constraint_Present then
8119 New_Indic :=
8120 Make_Subtype_Indication (Loc,
8121 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8122 Constraint => Relocate_Node (Constraint (Indic)));
8124 else
8125 declare
8126 Constr_List : constant List_Id := New_List;
8127 C : Elmt_Id;
8128 Expr : Node_Id;
8130 begin
8131 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8132 while Present (C) loop
8133 Expr := Node (C);
8135 -- It is safe here to call New_Copy_Tree since we called
8136 -- Force_Evaluation on each constraint previously
8137 -- in Build_Discriminant_Constraints.
8139 Append (New_Copy_Tree (Expr), To => Constr_List);
8141 Next_Elmt (C);
8142 end loop;
8144 New_Indic :=
8145 Make_Subtype_Indication (Loc,
8146 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8147 Constraint =>
8148 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8149 end;
8150 end if;
8152 Rewrite (N,
8153 Make_Subtype_Declaration (Loc,
8154 Defining_Identifier => Derived_Type,
8155 Subtype_Indication => New_Indic));
8157 Analyze (N);
8159 -- Derivation of subprograms must be delayed until the full subtype
8160 -- has been established, to ensure proper overriding of subprograms
8161 -- inherited by full types. If the derivations occurred as part of
8162 -- the call to Build_Derived_Type above, then the check for type
8163 -- conformance would fail because earlier primitive subprograms
8164 -- could still refer to the full type prior the change to the new
8165 -- subtype and hence would not match the new base type created here.
8166 -- Subprograms are not derived, however, when Derive_Subps is False
8167 -- (since otherwise there could be redundant derivations).
8169 if Derive_Subps then
8170 Derive_Subprograms (Parent_Type, Derived_Type);
8171 end if;
8173 -- For tagged types the Discriminant_Constraint of the new base itype
8174 -- is inherited from the first subtype so that no subtype conformance
8175 -- problem arise when the first subtype overrides primitive
8176 -- operations inherited by the implicit base type.
8178 if Is_Tagged then
8179 Set_Discriminant_Constraint
8180 (New_Base, Discriminant_Constraint (Derived_Type));
8181 end if;
8183 return;
8184 end if;
8186 -- If we get here Derived_Type will have no discriminants or it will be
8187 -- a discriminated unconstrained base type.
8189 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8191 if Is_Tagged then
8193 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8194 -- The declaration of a specific descendant of an interface type
8195 -- freezes the interface type (RM 13.14).
8197 if not Private_Extension or else Is_Interface (Parent_Base) then
8198 Freeze_Before (N, Parent_Type);
8199 end if;
8201 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8202 -- cannot be declared at a deeper level than its parent type is
8203 -- removed. The check on derivation within a generic body is also
8204 -- relaxed, but there's a restriction that a derived tagged type
8205 -- cannot be declared in a generic body if it's derived directly
8206 -- or indirectly from a formal type of that generic.
8208 if Ada_Version >= Ada_2005 then
8209 if Present (Enclosing_Generic_Body (Derived_Type)) then
8210 declare
8211 Ancestor_Type : Entity_Id;
8213 begin
8214 -- Check to see if any ancestor of the derived type is a
8215 -- formal type.
8217 Ancestor_Type := Parent_Type;
8218 while not Is_Generic_Type (Ancestor_Type)
8219 and then Etype (Ancestor_Type) /= Ancestor_Type
8220 loop
8221 Ancestor_Type := Etype (Ancestor_Type);
8222 end loop;
8224 -- If the derived type does have a formal type as an
8225 -- ancestor, then it's an error if the derived type is
8226 -- declared within the body of the generic unit that
8227 -- declares the formal type in its generic formal part. It's
8228 -- sufficient to check whether the ancestor type is declared
8229 -- inside the same generic body as the derived type (such as
8230 -- within a nested generic spec), in which case the
8231 -- derivation is legal. If the formal type is declared
8232 -- outside of that generic body, then it's guaranteed that
8233 -- the derived type is declared within the generic body of
8234 -- the generic unit declaring the formal type.
8236 if Is_Generic_Type (Ancestor_Type)
8237 and then Enclosing_Generic_Body (Ancestor_Type) /=
8238 Enclosing_Generic_Body (Derived_Type)
8239 then
8240 Error_Msg_NE
8241 ("parent type of& must not be descendant of formal type"
8242 & " of an enclosing generic body",
8243 Indic, Derived_Type);
8244 end if;
8245 end;
8246 end if;
8248 elsif Type_Access_Level (Derived_Type) /=
8249 Type_Access_Level (Parent_Type)
8250 and then not Is_Generic_Type (Derived_Type)
8251 then
8252 if Is_Controlled (Parent_Type) then
8253 Error_Msg_N
8254 ("controlled type must be declared at the library level",
8255 Indic);
8256 else
8257 Error_Msg_N
8258 ("type extension at deeper accessibility level than parent",
8259 Indic);
8260 end if;
8262 else
8263 declare
8264 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8265 begin
8266 if Present (GB)
8267 and then GB /= Enclosing_Generic_Body (Parent_Base)
8268 then
8269 Error_Msg_NE
8270 ("parent type of& must not be outside generic body"
8271 & " (RM 3.9.1(4))",
8272 Indic, Derived_Type);
8273 end if;
8274 end;
8275 end if;
8276 end if;
8278 -- Ada 2005 (AI-251)
8280 if Ada_Version >= Ada_2005 and then Is_Tagged then
8282 -- "The declaration of a specific descendant of an interface type
8283 -- freezes the interface type" (RM 13.14).
8285 declare
8286 Iface : Node_Id;
8287 begin
8288 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8289 Iface := First (Interface_List (Type_Def));
8290 while Present (Iface) loop
8291 Freeze_Before (N, Etype (Iface));
8292 Next (Iface);
8293 end loop;
8294 end if;
8295 end;
8296 end if;
8298 -- STEP 1b : preliminary cleanup of the full view of private types
8300 -- If the type is already marked as having discriminants, then it's the
8301 -- completion of a private type or private extension and we need to
8302 -- retain the discriminants from the partial view if the current
8303 -- declaration has Discriminant_Specifications so that we can verify
8304 -- conformance. However, we must remove any existing components that
8305 -- were inherited from the parent (and attached in Copy_And_Swap)
8306 -- because the full type inherits all appropriate components anyway, and
8307 -- we do not want the partial view's components interfering.
8309 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8310 Discrim := First_Discriminant (Derived_Type);
8311 loop
8312 Last_Discrim := Discrim;
8313 Next_Discriminant (Discrim);
8314 exit when No (Discrim);
8315 end loop;
8317 Set_Last_Entity (Derived_Type, Last_Discrim);
8319 -- In all other cases wipe out the list of inherited components (even
8320 -- inherited discriminants), it will be properly rebuilt here.
8322 else
8323 Set_First_Entity (Derived_Type, Empty);
8324 Set_Last_Entity (Derived_Type, Empty);
8325 end if;
8327 -- STEP 1c: Initialize some flags for the Derived_Type
8329 -- The following flags must be initialized here so that
8330 -- Process_Discriminants can check that discriminants of tagged types do
8331 -- not have a default initial value and that access discriminants are
8332 -- only specified for limited records. For completeness, these flags are
8333 -- also initialized along with all the other flags below.
8335 -- AI-419: Limitedness is not inherited from an interface parent, so to
8336 -- be limited in that case the type must be explicitly declared as
8337 -- limited. However, task and protected interfaces are always limited.
8339 if Limited_Present (Type_Def) then
8340 Set_Is_Limited_Record (Derived_Type);
8342 elsif Is_Limited_Record (Parent_Type)
8343 or else (Present (Full_View (Parent_Type))
8344 and then Is_Limited_Record (Full_View (Parent_Type)))
8345 then
8346 if not Is_Interface (Parent_Type)
8347 or else Is_Synchronized_Interface (Parent_Type)
8348 or else Is_Protected_Interface (Parent_Type)
8349 or else Is_Task_Interface (Parent_Type)
8350 then
8351 Set_Is_Limited_Record (Derived_Type);
8352 end if;
8353 end if;
8355 -- STEP 2a: process discriminants of derived type if any
8357 Push_Scope (Derived_Type);
8359 if Discriminant_Specs then
8360 Set_Has_Unknown_Discriminants (Derived_Type, False);
8362 -- The following call initializes fields Has_Discriminants and
8363 -- Discriminant_Constraint, unless we are processing the completion
8364 -- of a private type declaration.
8366 Check_Or_Process_Discriminants (N, Derived_Type);
8368 -- For untagged types, the constraint on the Parent_Type must be
8369 -- present and is used to rename the discriminants.
8371 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8372 Error_Msg_N ("untagged parent must have discriminants", Indic);
8374 elsif not Is_Tagged and then not Constraint_Present then
8375 Error_Msg_N
8376 ("discriminant constraint needed for derived untagged records",
8377 Indic);
8379 -- Otherwise the parent subtype must be constrained unless we have a
8380 -- private extension.
8382 elsif not Constraint_Present
8383 and then not Private_Extension
8384 and then not Is_Constrained (Parent_Type)
8385 then
8386 Error_Msg_N
8387 ("unconstrained type not allowed in this context", Indic);
8389 elsif Constraint_Present then
8390 -- The following call sets the field Corresponding_Discriminant
8391 -- for the discriminants in the Derived_Type.
8393 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8395 -- For untagged types all new discriminants must rename
8396 -- discriminants in the parent. For private extensions new
8397 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8399 Discrim := First_Discriminant (Derived_Type);
8400 while Present (Discrim) loop
8401 if not Is_Tagged
8402 and then No (Corresponding_Discriminant (Discrim))
8403 then
8404 Error_Msg_N
8405 ("new discriminants must constrain old ones", Discrim);
8407 elsif Private_Extension
8408 and then Present (Corresponding_Discriminant (Discrim))
8409 then
8410 Error_Msg_N
8411 ("only static constraints allowed for parent"
8412 & " discriminants in the partial view", Indic);
8413 exit;
8414 end if;
8416 -- If a new discriminant is used in the constraint, then its
8417 -- subtype must be statically compatible with the parent
8418 -- discriminant's subtype (3.7(15)).
8420 -- However, if the record contains an array constrained by
8421 -- the discriminant but with some different bound, the compiler
8422 -- attemps to create a smaller range for the discriminant type.
8423 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8424 -- the discriminant type is a scalar type, the check must use
8425 -- the original discriminant type in the parent declaration.
8427 declare
8428 Corr_Disc : constant Entity_Id :=
8429 Corresponding_Discriminant (Discrim);
8430 Disc_Type : constant Entity_Id := Etype (Discrim);
8431 Corr_Type : Entity_Id;
8433 begin
8434 if Present (Corr_Disc) then
8435 if Is_Scalar_Type (Disc_Type) then
8436 Corr_Type :=
8437 Entity (Discriminant_Type (Parent (Corr_Disc)));
8438 else
8439 Corr_Type := Etype (Corr_Disc);
8440 end if;
8442 if not
8443 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8444 then
8445 Error_Msg_N
8446 ("subtype must be compatible "
8447 & "with parent discriminant",
8448 Discrim);
8449 end if;
8450 end if;
8451 end;
8453 Next_Discriminant (Discrim);
8454 end loop;
8456 -- Check whether the constraints of the full view statically
8457 -- match those imposed by the parent subtype [7.3(13)].
8459 if Present (Stored_Constraint (Derived_Type)) then
8460 declare
8461 C1, C2 : Elmt_Id;
8463 begin
8464 C1 := First_Elmt (Discs);
8465 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8466 while Present (C1) and then Present (C2) loop
8467 if not
8468 Fully_Conformant_Expressions (Node (C1), Node (C2))
8469 then
8470 Error_Msg_N
8471 ("not conformant with previous declaration",
8472 Node (C1));
8473 end if;
8475 Next_Elmt (C1);
8476 Next_Elmt (C2);
8477 end loop;
8478 end;
8479 end if;
8480 end if;
8482 -- STEP 2b: No new discriminants, inherit discriminants if any
8484 else
8485 if Private_Extension then
8486 Set_Has_Unknown_Discriminants
8487 (Derived_Type,
8488 Has_Unknown_Discriminants (Parent_Type)
8489 or else Unknown_Discriminants_Present (N));
8491 -- The partial view of the parent may have unknown discriminants,
8492 -- but if the full view has discriminants and the parent type is
8493 -- in scope they must be inherited.
8495 elsif Has_Unknown_Discriminants (Parent_Type)
8496 and then
8497 (not Has_Discriminants (Parent_Type)
8498 or else not In_Open_Scopes (Scope (Parent_Type)))
8499 then
8500 Set_Has_Unknown_Discriminants (Derived_Type);
8501 end if;
8503 if not Has_Unknown_Discriminants (Derived_Type)
8504 and then not Has_Unknown_Discriminants (Parent_Base)
8505 and then Has_Discriminants (Parent_Type)
8506 then
8507 Inherit_Discrims := True;
8508 Set_Has_Discriminants
8509 (Derived_Type, True);
8510 Set_Discriminant_Constraint
8511 (Derived_Type, Discriminant_Constraint (Parent_Base));
8512 end if;
8514 -- The following test is true for private types (remember
8515 -- transformation 5. is not applied to those) and in an error
8516 -- situation.
8518 if Constraint_Present then
8519 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8520 end if;
8522 -- For now mark a new derived type as constrained only if it has no
8523 -- discriminants. At the end of Build_Derived_Record_Type we properly
8524 -- set this flag in the case of private extensions. See comments in
8525 -- point 9. just before body of Build_Derived_Record_Type.
8527 Set_Is_Constrained
8528 (Derived_Type,
8529 not (Inherit_Discrims
8530 or else Has_Unknown_Discriminants (Derived_Type)));
8531 end if;
8533 -- STEP 3: initialize fields of derived type
8535 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8536 Set_Stored_Constraint (Derived_Type, No_Elist);
8538 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8539 -- but cannot be interfaces
8541 if not Private_Extension
8542 and then Ekind (Derived_Type) /= E_Private_Type
8543 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8544 then
8545 if Interface_Present (Type_Def) then
8546 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8547 end if;
8549 Set_Interfaces (Derived_Type, No_Elist);
8550 end if;
8552 -- Fields inherited from the Parent_Type
8554 Set_Has_Specified_Layout
8555 (Derived_Type, Has_Specified_Layout (Parent_Type));
8556 Set_Is_Limited_Composite
8557 (Derived_Type, Is_Limited_Composite (Parent_Type));
8558 Set_Is_Private_Composite
8559 (Derived_Type, Is_Private_Composite (Parent_Type));
8561 if Is_Tagged_Type (Parent_Type) then
8562 Set_No_Tagged_Streams_Pragma
8563 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8564 end if;
8566 -- Fields inherited from the Parent_Base
8568 Set_Has_Controlled_Component
8569 (Derived_Type, Has_Controlled_Component (Parent_Base));
8570 Set_Has_Non_Standard_Rep
8571 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8572 Set_Has_Primitive_Operations
8573 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8575 -- Fields inherited from the Parent_Base in the non-private case
8577 if Ekind (Derived_Type) = E_Record_Type then
8578 Set_Has_Complex_Representation
8579 (Derived_Type, Has_Complex_Representation (Parent_Base));
8580 end if;
8582 -- Fields inherited from the Parent_Base for record types
8584 if Is_Record_Type (Derived_Type) then
8585 declare
8586 Parent_Full : Entity_Id;
8588 begin
8589 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8590 -- Parent_Base can be a private type or private extension. Go
8591 -- to the full view here to get the E_Record_Type specific flags.
8593 if Present (Full_View (Parent_Base)) then
8594 Parent_Full := Full_View (Parent_Base);
8595 else
8596 Parent_Full := Parent_Base;
8597 end if;
8599 Set_OK_To_Reorder_Components
8600 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8601 end;
8602 end if;
8604 -- Set fields for private derived types
8606 if Is_Private_Type (Derived_Type) then
8607 Set_Depends_On_Private (Derived_Type, True);
8608 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8610 -- Inherit fields from non private record types. If this is the
8611 -- completion of a derivation from a private type, the parent itself
8612 -- is private, and the attributes come from its full view, which must
8613 -- be present.
8615 else
8616 if Is_Private_Type (Parent_Base)
8617 and then not Is_Record_Type (Parent_Base)
8618 then
8619 Set_Component_Alignment
8620 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8621 Set_C_Pass_By_Copy
8622 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8623 else
8624 Set_Component_Alignment
8625 (Derived_Type, Component_Alignment (Parent_Base));
8626 Set_C_Pass_By_Copy
8627 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8628 end if;
8629 end if;
8631 -- Set fields for tagged types
8633 if Is_Tagged then
8634 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8636 -- All tagged types defined in Ada.Finalization are controlled
8638 if Chars (Scope (Derived_Type)) = Name_Finalization
8639 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8640 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8641 then
8642 Set_Is_Controlled (Derived_Type);
8643 else
8644 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8645 end if;
8647 -- Minor optimization: there is no need to generate the class-wide
8648 -- entity associated with an underlying record view.
8650 if not Is_Underlying_Record_View (Derived_Type) then
8651 Make_Class_Wide_Type (Derived_Type);
8652 end if;
8654 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8656 if Has_Discriminants (Derived_Type)
8657 and then Constraint_Present
8658 then
8659 Set_Stored_Constraint
8660 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8661 end if;
8663 if Ada_Version >= Ada_2005 then
8664 declare
8665 Ifaces_List : Elist_Id;
8667 begin
8668 -- Checks rules 3.9.4 (13/2 and 14/2)
8670 if Comes_From_Source (Derived_Type)
8671 and then not Is_Private_Type (Derived_Type)
8672 and then Is_Interface (Parent_Type)
8673 and then not Is_Interface (Derived_Type)
8674 then
8675 if Is_Task_Interface (Parent_Type) then
8676 Error_Msg_N
8677 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8678 Derived_Type);
8680 elsif Is_Protected_Interface (Parent_Type) then
8681 Error_Msg_N
8682 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8683 Derived_Type);
8684 end if;
8685 end if;
8687 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8689 Check_Interfaces (N, Type_Def);
8691 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8692 -- not already in the parents.
8694 Collect_Interfaces
8695 (T => Derived_Type,
8696 Ifaces_List => Ifaces_List,
8697 Exclude_Parents => True);
8699 Set_Interfaces (Derived_Type, Ifaces_List);
8701 -- If the derived type is the anonymous type created for
8702 -- a declaration whose parent has a constraint, propagate
8703 -- the interface list to the source type. This must be done
8704 -- prior to the completion of the analysis of the source type
8705 -- because the components in the extension may contain current
8706 -- instances whose legality depends on some ancestor.
8708 if Is_Itype (Derived_Type) then
8709 declare
8710 Def : constant Node_Id :=
8711 Associated_Node_For_Itype (Derived_Type);
8712 begin
8713 if Present (Def)
8714 and then Nkind (Def) = N_Full_Type_Declaration
8715 then
8716 Set_Interfaces
8717 (Defining_Identifier (Def), Ifaces_List);
8718 end if;
8719 end;
8720 end if;
8722 -- Propagate inherited invariant information of parents
8723 -- and progenitors
8725 if Ada_Version >= Ada_2012
8726 and then not Is_Interface (Derived_Type)
8727 then
8728 if Has_Inheritable_Invariants (Parent_Type) then
8729 Set_Has_Invariants (Derived_Type);
8730 Set_Has_Inheritable_Invariants (Derived_Type);
8732 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8733 declare
8734 AI : Elmt_Id;
8736 begin
8737 AI := First_Elmt (Ifaces_List);
8738 while Present (AI) loop
8739 if Has_Inheritable_Invariants (Node (AI)) then
8740 Set_Has_Invariants (Derived_Type);
8741 Set_Has_Inheritable_Invariants (Derived_Type);
8743 exit;
8744 end if;
8746 Next_Elmt (AI);
8747 end loop;
8748 end;
8749 end if;
8750 end if;
8752 -- A type extension is automatically Ghost when one of its
8753 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8754 -- also inherited when the parent type is Ghost, but this is
8755 -- done in Build_Derived_Type as the mechanism also handles
8756 -- untagged derivations.
8758 if Implements_Ghost_Interface (Derived_Type) then
8759 Set_Is_Ghost_Entity (Derived_Type);
8760 end if;
8761 end;
8762 end if;
8764 else
8765 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8766 Set_Has_Non_Standard_Rep
8767 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8768 end if;
8770 -- STEP 4: Inherit components from the parent base and constrain them.
8771 -- Apply the second transformation described in point 6. above.
8773 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8774 or else not Has_Discriminants (Parent_Type)
8775 or else not Is_Constrained (Parent_Type)
8776 then
8777 Constrs := Discs;
8778 else
8779 Constrs := Discriminant_Constraint (Parent_Type);
8780 end if;
8782 Assoc_List :=
8783 Inherit_Components
8784 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8786 -- STEP 5a: Copy the parent record declaration for untagged types
8788 if not Is_Tagged then
8790 -- Discriminant_Constraint (Derived_Type) has been properly
8791 -- constructed. Save it and temporarily set it to Empty because we
8792 -- do not want the call to New_Copy_Tree below to mess this list.
8794 if Has_Discriminants (Derived_Type) then
8795 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8796 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8797 else
8798 Save_Discr_Constr := No_Elist;
8799 end if;
8801 -- Save the Etype field of Derived_Type. It is correctly set now,
8802 -- but the call to New_Copy tree may remap it to point to itself,
8803 -- which is not what we want. Ditto for the Next_Entity field.
8805 Save_Etype := Etype (Derived_Type);
8806 Save_Next_Entity := Next_Entity (Derived_Type);
8808 -- Assoc_List maps all stored discriminants in the Parent_Base to
8809 -- stored discriminants in the Derived_Type. It is fundamental that
8810 -- no types or itypes with discriminants other than the stored
8811 -- discriminants appear in the entities declared inside
8812 -- Derived_Type, since the back end cannot deal with it.
8814 New_Decl :=
8815 New_Copy_Tree
8816 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8818 -- Restore the fields saved prior to the New_Copy_Tree call
8819 -- and compute the stored constraint.
8821 Set_Etype (Derived_Type, Save_Etype);
8822 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8824 if Has_Discriminants (Derived_Type) then
8825 Set_Discriminant_Constraint
8826 (Derived_Type, Save_Discr_Constr);
8827 Set_Stored_Constraint
8828 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8829 Replace_Components (Derived_Type, New_Decl);
8830 Set_Has_Implicit_Dereference
8831 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8832 end if;
8834 -- Insert the new derived type declaration
8836 Rewrite (N, New_Decl);
8838 -- STEP 5b: Complete the processing for record extensions in generics
8840 -- There is no completion for record extensions declared in the
8841 -- parameter part of a generic, so we need to complete processing for
8842 -- these generic record extensions here. The Record_Type_Definition call
8843 -- will change the Ekind of the components from E_Void to E_Component.
8845 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8846 Record_Type_Definition (Empty, Derived_Type);
8848 -- STEP 5c: Process the record extension for non private tagged types
8850 elsif not Private_Extension then
8851 Expand_Record_Extension (Derived_Type, Type_Def);
8853 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8854 -- derived type to propagate some semantic information. This led
8855 -- to other ASIS failures and has been removed.
8857 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8858 -- implemented interfaces if we are in expansion mode
8860 if Expander_Active
8861 and then Has_Interfaces (Derived_Type)
8862 then
8863 Add_Interface_Tag_Components (N, Derived_Type);
8864 end if;
8866 -- Analyze the record extension
8868 Record_Type_Definition
8869 (Record_Extension_Part (Type_Def), Derived_Type);
8870 end if;
8872 End_Scope;
8874 -- Nothing else to do if there is an error in the derivation.
8875 -- An unusual case: the full view may be derived from a type in an
8876 -- instance, when the partial view was used illegally as an actual
8877 -- in that instance, leading to a circular definition.
8879 if Etype (Derived_Type) = Any_Type
8880 or else Etype (Parent_Type) = Derived_Type
8881 then
8882 return;
8883 end if;
8885 -- Set delayed freeze and then derive subprograms, we need to do
8886 -- this in this order so that derived subprograms inherit the
8887 -- derived freeze if necessary.
8889 Set_Has_Delayed_Freeze (Derived_Type);
8891 if Derive_Subps then
8892 Derive_Subprograms (Parent_Type, Derived_Type);
8893 end if;
8895 -- If we have a private extension which defines a constrained derived
8896 -- type mark as constrained here after we have derived subprograms. See
8897 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8899 if Private_Extension and then Inherit_Discrims then
8900 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8901 Set_Is_Constrained (Derived_Type, True);
8902 Set_Discriminant_Constraint (Derived_Type, Discs);
8904 elsif Is_Constrained (Parent_Type) then
8905 Set_Is_Constrained
8906 (Derived_Type, True);
8907 Set_Discriminant_Constraint
8908 (Derived_Type, Discriminant_Constraint (Parent_Type));
8909 end if;
8910 end if;
8912 -- Update the class-wide type, which shares the now-completed entity
8913 -- list with its specific type. In case of underlying record views,
8914 -- we do not generate the corresponding class wide entity.
8916 if Is_Tagged
8917 and then not Is_Underlying_Record_View (Derived_Type)
8918 then
8919 Set_First_Entity
8920 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8921 Set_Last_Entity
8922 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8923 end if;
8925 Check_Function_Writable_Actuals (N);
8926 end Build_Derived_Record_Type;
8928 ------------------------
8929 -- Build_Derived_Type --
8930 ------------------------
8932 procedure Build_Derived_Type
8933 (N : Node_Id;
8934 Parent_Type : Entity_Id;
8935 Derived_Type : Entity_Id;
8936 Is_Completion : Boolean;
8937 Derive_Subps : Boolean := True)
8939 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8941 begin
8942 -- Set common attributes
8944 Set_Scope (Derived_Type, Current_Scope);
8946 Set_Etype (Derived_Type, Parent_Base);
8947 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8948 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8949 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8951 Set_Size_Info (Derived_Type, Parent_Type);
8952 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8953 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8954 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
8956 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8957 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8959 if Is_Tagged_Type (Derived_Type) then
8960 Set_No_Tagged_Streams_Pragma
8961 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8962 end if;
8964 -- If the parent has primitive routines, set the derived type link
8966 if Has_Primitive_Operations (Parent_Type) then
8967 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8968 end if;
8970 -- If the parent type is a private subtype, the convention on the base
8971 -- type may be set in the private part, and not propagated to the
8972 -- subtype until later, so we obtain the convention from the base type.
8974 Set_Convention (Derived_Type, Convention (Parent_Base));
8976 -- Set SSO default for record or array type
8978 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
8979 and then Is_Base_Type (Derived_Type)
8980 then
8981 Set_Default_SSO (Derived_Type);
8982 end if;
8984 -- Propagate invariant information. The new type has invariants if
8985 -- they are inherited from the parent type, and these invariants can
8986 -- be further inherited, so both flags are set.
8988 -- We similarly inherit predicates
8990 if Has_Predicates (Parent_Type) then
8991 Set_Has_Predicates (Derived_Type);
8992 end if;
8994 -- The derived type inherits the representation clauses of the parent
8996 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
8998 -- Propagate the attributes related to pragma Default_Initial_Condition
8999 -- from the parent type to the private extension. A derived type always
9000 -- inherits the default initial condition flag from the parent type. If
9001 -- the derived type carries its own Default_Initial_Condition pragma,
9002 -- the flag is later reset in Analyze_Pragma. Note that both flags are
9003 -- mutually exclusive.
9005 Propagate_Default_Init_Cond_Attributes
9006 (From_Typ => Parent_Type,
9007 To_Typ => Derived_Type,
9008 Parent_To_Derivation => True);
9010 -- If the parent type has delayed rep aspects, then mark the derived
9011 -- type as possibly inheriting a delayed rep aspect.
9013 if Has_Delayed_Rep_Aspects (Parent_Type) then
9014 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9015 end if;
9017 -- Propagate the attributes related to pragma Ghost from the parent type
9018 -- to the derived type or type extension (SPARK RM 6.9(9)).
9020 if Is_Ghost_Entity (Parent_Type) then
9021 Set_Is_Ghost_Entity (Derived_Type);
9022 end if;
9024 -- Type dependent processing
9026 case Ekind (Parent_Type) is
9027 when Numeric_Kind =>
9028 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9030 when Array_Kind =>
9031 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9033 when E_Record_Type
9034 | E_Record_Subtype
9035 | Class_Wide_Kind =>
9036 Build_Derived_Record_Type
9037 (N, Parent_Type, Derived_Type, Derive_Subps);
9038 return;
9040 when Enumeration_Kind =>
9041 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9043 when Access_Kind =>
9044 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9046 when Incomplete_Or_Private_Kind =>
9047 Build_Derived_Private_Type
9048 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9050 -- For discriminated types, the derivation includes deriving
9051 -- primitive operations. For others it is done below.
9053 if Is_Tagged_Type (Parent_Type)
9054 or else Has_Discriminants (Parent_Type)
9055 or else (Present (Full_View (Parent_Type))
9056 and then Has_Discriminants (Full_View (Parent_Type)))
9057 then
9058 return;
9059 end if;
9061 when Concurrent_Kind =>
9062 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9064 when others =>
9065 raise Program_Error;
9066 end case;
9068 -- Nothing more to do if some error occurred
9070 if Etype (Derived_Type) = Any_Type then
9071 return;
9072 end if;
9074 -- Set delayed freeze and then derive subprograms, we need to do this
9075 -- in this order so that derived subprograms inherit the derived freeze
9076 -- if necessary.
9078 Set_Has_Delayed_Freeze (Derived_Type);
9080 if Derive_Subps then
9081 Derive_Subprograms (Parent_Type, Derived_Type);
9082 end if;
9084 Set_Has_Primitive_Operations
9085 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9086 end Build_Derived_Type;
9088 -----------------------
9089 -- Build_Discriminal --
9090 -----------------------
9092 procedure Build_Discriminal (Discrim : Entity_Id) is
9093 D_Minal : Entity_Id;
9094 CR_Disc : Entity_Id;
9096 begin
9097 -- A discriminal has the same name as the discriminant
9099 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9101 Set_Ekind (D_Minal, E_In_Parameter);
9102 Set_Mechanism (D_Minal, Default_Mechanism);
9103 Set_Etype (D_Minal, Etype (Discrim));
9104 Set_Scope (D_Minal, Current_Scope);
9106 Set_Discriminal (Discrim, D_Minal);
9107 Set_Discriminal_Link (D_Minal, Discrim);
9109 -- For task types, build at once the discriminants of the corresponding
9110 -- record, which are needed if discriminants are used in entry defaults
9111 -- and in family bounds.
9113 if Is_Concurrent_Type (Current_Scope)
9114 or else
9115 Is_Limited_Type (Current_Scope)
9116 then
9117 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9119 Set_Ekind (CR_Disc, E_In_Parameter);
9120 Set_Mechanism (CR_Disc, Default_Mechanism);
9121 Set_Etype (CR_Disc, Etype (Discrim));
9122 Set_Scope (CR_Disc, Current_Scope);
9123 Set_Discriminal_Link (CR_Disc, Discrim);
9124 Set_CR_Discriminant (Discrim, CR_Disc);
9125 end if;
9126 end Build_Discriminal;
9128 ------------------------------------
9129 -- Build_Discriminant_Constraints --
9130 ------------------------------------
9132 function Build_Discriminant_Constraints
9133 (T : Entity_Id;
9134 Def : Node_Id;
9135 Derived_Def : Boolean := False) return Elist_Id
9137 C : constant Node_Id := Constraint (Def);
9138 Nb_Discr : constant Nat := Number_Discriminants (T);
9140 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9141 -- Saves the expression corresponding to a given discriminant in T
9143 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9144 -- Return the Position number within array Discr_Expr of a discriminant
9145 -- D within the discriminant list of the discriminated type T.
9147 procedure Process_Discriminant_Expression
9148 (Expr : Node_Id;
9149 D : Entity_Id);
9150 -- If this is a discriminant constraint on a partial view, do not
9151 -- generate an overflow check on the discriminant expression. The check
9152 -- will be generated when constraining the full view. Otherwise the
9153 -- backend creates duplicate symbols for the temporaries corresponding
9154 -- to the expressions to be checked, causing spurious assembler errors.
9156 ------------------
9157 -- Pos_Of_Discr --
9158 ------------------
9160 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9161 Disc : Entity_Id;
9163 begin
9164 Disc := First_Discriminant (T);
9165 for J in Discr_Expr'Range loop
9166 if Disc = D then
9167 return J;
9168 end if;
9170 Next_Discriminant (Disc);
9171 end loop;
9173 -- Note: Since this function is called on discriminants that are
9174 -- known to belong to the discriminated type, falling through the
9175 -- loop with no match signals an internal compiler error.
9177 raise Program_Error;
9178 end Pos_Of_Discr;
9180 -------------------------------------
9181 -- Process_Discriminant_Expression --
9182 -------------------------------------
9184 procedure Process_Discriminant_Expression
9185 (Expr : Node_Id;
9186 D : Entity_Id)
9188 BDT : constant Entity_Id := Base_Type (Etype (D));
9190 begin
9191 -- If this is a discriminant constraint on a partial view, do
9192 -- not generate an overflow on the discriminant expression. The
9193 -- check will be generated when constraining the full view.
9195 if Is_Private_Type (T)
9196 and then Present (Full_View (T))
9197 then
9198 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9199 else
9200 Analyze_And_Resolve (Expr, BDT);
9201 end if;
9202 end Process_Discriminant_Expression;
9204 -- Declarations local to Build_Discriminant_Constraints
9206 Discr : Entity_Id;
9207 E : Entity_Id;
9208 Elist : constant Elist_Id := New_Elmt_List;
9210 Constr : Node_Id;
9211 Expr : Node_Id;
9212 Id : Node_Id;
9213 Position : Nat;
9214 Found : Boolean;
9216 Discrim_Present : Boolean := False;
9218 -- Start of processing for Build_Discriminant_Constraints
9220 begin
9221 -- The following loop will process positional associations only.
9222 -- For a positional association, the (single) discriminant is
9223 -- implicitly specified by position, in textual order (RM 3.7.2).
9225 Discr := First_Discriminant (T);
9226 Constr := First (Constraints (C));
9227 for D in Discr_Expr'Range loop
9228 exit when Nkind (Constr) = N_Discriminant_Association;
9230 if No (Constr) then
9231 Error_Msg_N ("too few discriminants given in constraint", C);
9232 return New_Elmt_List;
9234 elsif Nkind (Constr) = N_Range
9235 or else (Nkind (Constr) = N_Attribute_Reference
9236 and then Attribute_Name (Constr) = Name_Range)
9237 then
9238 Error_Msg_N
9239 ("a range is not a valid discriminant constraint", Constr);
9240 Discr_Expr (D) := Error;
9242 else
9243 Process_Discriminant_Expression (Constr, Discr);
9244 Discr_Expr (D) := Constr;
9245 end if;
9247 Next_Discriminant (Discr);
9248 Next (Constr);
9249 end loop;
9251 if No (Discr) and then Present (Constr) then
9252 Error_Msg_N ("too many discriminants given in constraint", Constr);
9253 return New_Elmt_List;
9254 end if;
9256 -- Named associations can be given in any order, but if both positional
9257 -- and named associations are used in the same discriminant constraint,
9258 -- then positional associations must occur first, at their normal
9259 -- position. Hence once a named association is used, the rest of the
9260 -- discriminant constraint must use only named associations.
9262 while Present (Constr) loop
9264 -- Positional association forbidden after a named association
9266 if Nkind (Constr) /= N_Discriminant_Association then
9267 Error_Msg_N ("positional association follows named one", Constr);
9268 return New_Elmt_List;
9270 -- Otherwise it is a named association
9272 else
9273 -- E records the type of the discriminants in the named
9274 -- association. All the discriminants specified in the same name
9275 -- association must have the same type.
9277 E := Empty;
9279 -- Search the list of discriminants in T to see if the simple name
9280 -- given in the constraint matches any of them.
9282 Id := First (Selector_Names (Constr));
9283 while Present (Id) loop
9284 Found := False;
9286 -- If Original_Discriminant is present, we are processing a
9287 -- generic instantiation and this is an instance node. We need
9288 -- to find the name of the corresponding discriminant in the
9289 -- actual record type T and not the name of the discriminant in
9290 -- the generic formal. Example:
9292 -- generic
9293 -- type G (D : int) is private;
9294 -- package P is
9295 -- subtype W is G (D => 1);
9296 -- end package;
9297 -- type Rec (X : int) is record ... end record;
9298 -- package Q is new P (G => Rec);
9300 -- At the point of the instantiation, formal type G is Rec
9301 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9302 -- which really looks like "subtype W is Rec (D => 1);" at
9303 -- the point of instantiation, we want to find the discriminant
9304 -- that corresponds to D in Rec, i.e. X.
9306 if Present (Original_Discriminant (Id))
9307 and then In_Instance
9308 then
9309 Discr := Find_Corresponding_Discriminant (Id, T);
9310 Found := True;
9312 else
9313 Discr := First_Discriminant (T);
9314 while Present (Discr) loop
9315 if Chars (Discr) = Chars (Id) then
9316 Found := True;
9317 exit;
9318 end if;
9320 Next_Discriminant (Discr);
9321 end loop;
9323 if not Found then
9324 Error_Msg_N ("& does not match any discriminant", Id);
9325 return New_Elmt_List;
9327 -- If the parent type is a generic formal, preserve the
9328 -- name of the discriminant for subsequent instances.
9329 -- see comment at the beginning of this if statement.
9331 elsif Is_Generic_Type (Root_Type (T)) then
9332 Set_Original_Discriminant (Id, Discr);
9333 end if;
9334 end if;
9336 Position := Pos_Of_Discr (T, Discr);
9338 if Present (Discr_Expr (Position)) then
9339 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9341 else
9342 -- Each discriminant specified in the same named association
9343 -- must be associated with a separate copy of the
9344 -- corresponding expression.
9346 if Present (Next (Id)) then
9347 Expr := New_Copy_Tree (Expression (Constr));
9348 Set_Parent (Expr, Parent (Expression (Constr)));
9349 else
9350 Expr := Expression (Constr);
9351 end if;
9353 Discr_Expr (Position) := Expr;
9354 Process_Discriminant_Expression (Expr, Discr);
9355 end if;
9357 -- A discriminant association with more than one discriminant
9358 -- name is only allowed if the named discriminants are all of
9359 -- the same type (RM 3.7.1(8)).
9361 if E = Empty then
9362 E := Base_Type (Etype (Discr));
9364 elsif Base_Type (Etype (Discr)) /= E then
9365 Error_Msg_N
9366 ("all discriminants in an association " &
9367 "must have the same type", Id);
9368 end if;
9370 Next (Id);
9371 end loop;
9372 end if;
9374 Next (Constr);
9375 end loop;
9377 -- A discriminant constraint must provide exactly one value for each
9378 -- discriminant of the type (RM 3.7.1(8)).
9380 for J in Discr_Expr'Range loop
9381 if No (Discr_Expr (J)) then
9382 Error_Msg_N ("too few discriminants given in constraint", C);
9383 return New_Elmt_List;
9384 end if;
9385 end loop;
9387 -- Determine if there are discriminant expressions in the constraint
9389 for J in Discr_Expr'Range loop
9390 if Denotes_Discriminant
9391 (Discr_Expr (J), Check_Concurrent => True)
9392 then
9393 Discrim_Present := True;
9394 end if;
9395 end loop;
9397 -- Build an element list consisting of the expressions given in the
9398 -- discriminant constraint and apply the appropriate checks. The list
9399 -- is constructed after resolving any named discriminant associations
9400 -- and therefore the expressions appear in the textual order of the
9401 -- discriminants.
9403 Discr := First_Discriminant (T);
9404 for J in Discr_Expr'Range loop
9405 if Discr_Expr (J) /= Error then
9406 Append_Elmt (Discr_Expr (J), Elist);
9408 -- If any of the discriminant constraints is given by a
9409 -- discriminant and we are in a derived type declaration we
9410 -- have a discriminant renaming. Establish link between new
9411 -- and old discriminant.
9413 if Denotes_Discriminant (Discr_Expr (J)) then
9414 if Derived_Def then
9415 Set_Corresponding_Discriminant
9416 (Entity (Discr_Expr (J)), Discr);
9417 end if;
9419 -- Force the evaluation of non-discriminant expressions.
9420 -- If we have found a discriminant in the constraint 3.4(26)
9421 -- and 3.8(18) demand that no range checks are performed are
9422 -- after evaluation. If the constraint is for a component
9423 -- definition that has a per-object constraint, expressions are
9424 -- evaluated but not checked either. In all other cases perform
9425 -- a range check.
9427 else
9428 if Discrim_Present then
9429 null;
9431 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9432 and then
9433 Has_Per_Object_Constraint
9434 (Defining_Identifier (Parent (Parent (Def))))
9435 then
9436 null;
9438 elsif Is_Access_Type (Etype (Discr)) then
9439 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9441 else
9442 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9443 end if;
9445 Force_Evaluation (Discr_Expr (J));
9446 end if;
9448 -- Check that the designated type of an access discriminant's
9449 -- expression is not a class-wide type unless the discriminant's
9450 -- designated type is also class-wide.
9452 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9453 and then not Is_Class_Wide_Type
9454 (Designated_Type (Etype (Discr)))
9455 and then Etype (Discr_Expr (J)) /= Any_Type
9456 and then Is_Class_Wide_Type
9457 (Designated_Type (Etype (Discr_Expr (J))))
9458 then
9459 Wrong_Type (Discr_Expr (J), Etype (Discr));
9461 elsif Is_Access_Type (Etype (Discr))
9462 and then not Is_Access_Constant (Etype (Discr))
9463 and then Is_Access_Type (Etype (Discr_Expr (J)))
9464 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9465 then
9466 Error_Msg_NE
9467 ("constraint for discriminant& must be access to variable",
9468 Def, Discr);
9469 end if;
9470 end if;
9472 Next_Discriminant (Discr);
9473 end loop;
9475 return Elist;
9476 end Build_Discriminant_Constraints;
9478 ---------------------------------
9479 -- Build_Discriminated_Subtype --
9480 ---------------------------------
9482 procedure Build_Discriminated_Subtype
9483 (T : Entity_Id;
9484 Def_Id : Entity_Id;
9485 Elist : Elist_Id;
9486 Related_Nod : Node_Id;
9487 For_Access : Boolean := False)
9489 Has_Discrs : constant Boolean := Has_Discriminants (T);
9490 Constrained : constant Boolean :=
9491 (Has_Discrs
9492 and then not Is_Empty_Elmt_List (Elist)
9493 and then not Is_Class_Wide_Type (T))
9494 or else Is_Constrained (T);
9496 begin
9497 if Ekind (T) = E_Record_Type then
9498 if For_Access then
9499 Set_Ekind (Def_Id, E_Private_Subtype);
9500 Set_Is_For_Access_Subtype (Def_Id, True);
9501 else
9502 Set_Ekind (Def_Id, E_Record_Subtype);
9503 end if;
9505 -- Inherit preelaboration flag from base, for types for which it
9506 -- may have been set: records, private types, protected types.
9508 Set_Known_To_Have_Preelab_Init
9509 (Def_Id, Known_To_Have_Preelab_Init (T));
9511 elsif Ekind (T) = E_Task_Type then
9512 Set_Ekind (Def_Id, E_Task_Subtype);
9514 elsif Ekind (T) = E_Protected_Type then
9515 Set_Ekind (Def_Id, E_Protected_Subtype);
9516 Set_Known_To_Have_Preelab_Init
9517 (Def_Id, Known_To_Have_Preelab_Init (T));
9519 elsif Is_Private_Type (T) then
9520 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9521 Set_Known_To_Have_Preelab_Init
9522 (Def_Id, Known_To_Have_Preelab_Init (T));
9524 -- Private subtypes may have private dependents
9526 Set_Private_Dependents (Def_Id, New_Elmt_List);
9528 elsif Is_Class_Wide_Type (T) then
9529 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9531 else
9532 -- Incomplete type. Attach subtype to list of dependents, to be
9533 -- completed with full view of parent type, unless is it the
9534 -- designated subtype of a record component within an init_proc.
9535 -- This last case arises for a component of an access type whose
9536 -- designated type is incomplete (e.g. a Taft Amendment type).
9537 -- The designated subtype is within an inner scope, and needs no
9538 -- elaboration, because only the access type is needed in the
9539 -- initialization procedure.
9541 Set_Ekind (Def_Id, Ekind (T));
9543 if For_Access and then Within_Init_Proc then
9544 null;
9545 else
9546 Append_Elmt (Def_Id, Private_Dependents (T));
9547 end if;
9548 end if;
9550 Set_Etype (Def_Id, T);
9551 Init_Size_Align (Def_Id);
9552 Set_Has_Discriminants (Def_Id, Has_Discrs);
9553 Set_Is_Constrained (Def_Id, Constrained);
9555 Set_First_Entity (Def_Id, First_Entity (T));
9556 Set_Last_Entity (Def_Id, Last_Entity (T));
9557 Set_Has_Implicit_Dereference
9558 (Def_Id, Has_Implicit_Dereference (T));
9560 -- If the subtype is the completion of a private declaration, there may
9561 -- have been representation clauses for the partial view, and they must
9562 -- be preserved. Build_Derived_Type chains the inherited clauses with
9563 -- the ones appearing on the extension. If this comes from a subtype
9564 -- declaration, all clauses are inherited.
9566 if No (First_Rep_Item (Def_Id)) then
9567 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9568 end if;
9570 if Is_Tagged_Type (T) then
9571 Set_Is_Tagged_Type (Def_Id);
9572 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9573 Make_Class_Wide_Type (Def_Id);
9574 end if;
9576 Set_Stored_Constraint (Def_Id, No_Elist);
9578 if Has_Discrs then
9579 Set_Discriminant_Constraint (Def_Id, Elist);
9580 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9581 end if;
9583 if Is_Tagged_Type (T) then
9585 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9586 -- concurrent record type (which has the list of primitive
9587 -- operations).
9589 if Ada_Version >= Ada_2005
9590 and then Is_Concurrent_Type (T)
9591 then
9592 Set_Corresponding_Record_Type (Def_Id,
9593 Corresponding_Record_Type (T));
9594 else
9595 Set_Direct_Primitive_Operations (Def_Id,
9596 Direct_Primitive_Operations (T));
9597 end if;
9599 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9600 end if;
9602 -- Subtypes introduced by component declarations do not need to be
9603 -- marked as delayed, and do not get freeze nodes, because the semantics
9604 -- verifies that the parents of the subtypes are frozen before the
9605 -- enclosing record is frozen.
9607 if not Is_Type (Scope (Def_Id)) then
9608 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9610 if Is_Private_Type (T)
9611 and then Present (Full_View (T))
9612 then
9613 Conditional_Delay (Def_Id, Full_View (T));
9614 else
9615 Conditional_Delay (Def_Id, T);
9616 end if;
9617 end if;
9619 if Is_Record_Type (T) then
9620 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9622 if Has_Discrs
9623 and then not Is_Empty_Elmt_List (Elist)
9624 and then not For_Access
9625 then
9626 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9627 elsif not For_Access then
9628 Set_Cloned_Subtype (Def_Id, T);
9629 end if;
9630 end if;
9631 end Build_Discriminated_Subtype;
9633 ---------------------------
9634 -- Build_Itype_Reference --
9635 ---------------------------
9637 procedure Build_Itype_Reference
9638 (Ityp : Entity_Id;
9639 Nod : Node_Id)
9641 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9642 begin
9644 -- Itype references are only created for use by the back-end
9646 if Inside_A_Generic then
9647 return;
9648 else
9649 Set_Itype (IR, Ityp);
9650 Insert_After (Nod, IR);
9651 end if;
9652 end Build_Itype_Reference;
9654 ------------------------
9655 -- Build_Scalar_Bound --
9656 ------------------------
9658 function Build_Scalar_Bound
9659 (Bound : Node_Id;
9660 Par_T : Entity_Id;
9661 Der_T : Entity_Id) return Node_Id
9663 New_Bound : Entity_Id;
9665 begin
9666 -- Note: not clear why this is needed, how can the original bound
9667 -- be unanalyzed at this point? and if it is, what business do we
9668 -- have messing around with it? and why is the base type of the
9669 -- parent type the right type for the resolution. It probably is
9670 -- not. It is OK for the new bound we are creating, but not for
9671 -- the old one??? Still if it never happens, no problem.
9673 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9675 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9676 New_Bound := New_Copy (Bound);
9677 Set_Etype (New_Bound, Der_T);
9678 Set_Analyzed (New_Bound);
9680 elsif Is_Entity_Name (Bound) then
9681 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9683 -- The following is almost certainly wrong. What business do we have
9684 -- relocating a node (Bound) that is presumably still attached to
9685 -- the tree elsewhere???
9687 else
9688 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9689 end if;
9691 Set_Etype (New_Bound, Der_T);
9692 return New_Bound;
9693 end Build_Scalar_Bound;
9695 --------------------------------
9696 -- Build_Underlying_Full_View --
9697 --------------------------------
9699 procedure Build_Underlying_Full_View
9700 (N : Node_Id;
9701 Typ : Entity_Id;
9702 Par : Entity_Id)
9704 Loc : constant Source_Ptr := Sloc (N);
9705 Subt : constant Entity_Id :=
9706 Make_Defining_Identifier
9707 (Loc, New_External_Name (Chars (Typ), 'S'));
9709 Constr : Node_Id;
9710 Indic : Node_Id;
9711 C : Node_Id;
9712 Id : Node_Id;
9714 procedure Set_Discriminant_Name (Id : Node_Id);
9715 -- If the derived type has discriminants, they may rename discriminants
9716 -- of the parent. When building the full view of the parent, we need to
9717 -- recover the names of the original discriminants if the constraint is
9718 -- given by named associations.
9720 ---------------------------
9721 -- Set_Discriminant_Name --
9722 ---------------------------
9724 procedure Set_Discriminant_Name (Id : Node_Id) is
9725 Disc : Entity_Id;
9727 begin
9728 Set_Original_Discriminant (Id, Empty);
9730 if Has_Discriminants (Typ) then
9731 Disc := First_Discriminant (Typ);
9732 while Present (Disc) loop
9733 if Chars (Disc) = Chars (Id)
9734 and then Present (Corresponding_Discriminant (Disc))
9735 then
9736 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9737 end if;
9738 Next_Discriminant (Disc);
9739 end loop;
9740 end if;
9741 end Set_Discriminant_Name;
9743 -- Start of processing for Build_Underlying_Full_View
9745 begin
9746 if Nkind (N) = N_Full_Type_Declaration then
9747 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9749 elsif Nkind (N) = N_Subtype_Declaration then
9750 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9752 elsif Nkind (N) = N_Component_Declaration then
9753 Constr :=
9754 New_Copy_Tree
9755 (Constraint (Subtype_Indication (Component_Definition (N))));
9757 else
9758 raise Program_Error;
9759 end if;
9761 C := First (Constraints (Constr));
9762 while Present (C) loop
9763 if Nkind (C) = N_Discriminant_Association then
9764 Id := First (Selector_Names (C));
9765 while Present (Id) loop
9766 Set_Discriminant_Name (Id);
9767 Next (Id);
9768 end loop;
9769 end if;
9771 Next (C);
9772 end loop;
9774 Indic :=
9775 Make_Subtype_Declaration (Loc,
9776 Defining_Identifier => Subt,
9777 Subtype_Indication =>
9778 Make_Subtype_Indication (Loc,
9779 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9780 Constraint => New_Copy_Tree (Constr)));
9782 -- If this is a component subtype for an outer itype, it is not
9783 -- a list member, so simply set the parent link for analysis: if
9784 -- the enclosing type does not need to be in a declarative list,
9785 -- neither do the components.
9787 if Is_List_Member (N)
9788 and then Nkind (N) /= N_Component_Declaration
9789 then
9790 Insert_Before (N, Indic);
9791 else
9792 Set_Parent (Indic, Parent (N));
9793 end if;
9795 Analyze (Indic);
9796 Set_Underlying_Full_View (Typ, Full_View (Subt));
9797 end Build_Underlying_Full_View;
9799 -------------------------------
9800 -- Check_Abstract_Overriding --
9801 -------------------------------
9803 procedure Check_Abstract_Overriding (T : Entity_Id) is
9804 Alias_Subp : Entity_Id;
9805 Elmt : Elmt_Id;
9806 Op_List : Elist_Id;
9807 Subp : Entity_Id;
9808 Type_Def : Node_Id;
9810 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9811 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9812 -- which has pragma Implemented already set. Check whether Subp's entity
9813 -- kind conforms to the implementation kind of the overridden routine.
9815 procedure Check_Pragma_Implemented
9816 (Subp : Entity_Id;
9817 Iface_Subp : Entity_Id);
9818 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9819 -- Iface_Subp and both entities have pragma Implemented already set on
9820 -- them. Check whether the two implementation kinds are conforming.
9822 procedure Inherit_Pragma_Implemented
9823 (Subp : Entity_Id;
9824 Iface_Subp : Entity_Id);
9825 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9826 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9827 -- Propagate the implementation kind of Iface_Subp to Subp.
9829 ------------------------------
9830 -- Check_Pragma_Implemented --
9831 ------------------------------
9833 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9834 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9835 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9836 Subp_Alias : constant Entity_Id := Alias (Subp);
9837 Contr_Typ : Entity_Id;
9838 Impl_Subp : Entity_Id;
9840 begin
9841 -- Subp must have an alias since it is a hidden entity used to link
9842 -- an interface subprogram to its overriding counterpart.
9844 pragma Assert (Present (Subp_Alias));
9846 -- Handle aliases to synchronized wrappers
9848 Impl_Subp := Subp_Alias;
9850 if Is_Primitive_Wrapper (Impl_Subp) then
9851 Impl_Subp := Wrapped_Entity (Impl_Subp);
9852 end if;
9854 -- Extract the type of the controlling formal
9856 Contr_Typ := Etype (First_Formal (Subp_Alias));
9858 if Is_Concurrent_Record_Type (Contr_Typ) then
9859 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9860 end if;
9862 -- An interface subprogram whose implementation kind is By_Entry must
9863 -- be implemented by an entry.
9865 if Impl_Kind = Name_By_Entry
9866 and then Ekind (Impl_Subp) /= E_Entry
9867 then
9868 Error_Msg_Node_2 := Iface_Alias;
9869 Error_Msg_NE
9870 ("type & must implement abstract subprogram & with an entry",
9871 Subp_Alias, Contr_Typ);
9873 elsif Impl_Kind = Name_By_Protected_Procedure then
9875 -- An interface subprogram whose implementation kind is By_
9876 -- Protected_Procedure cannot be implemented by a primitive
9877 -- procedure of a task type.
9879 if Ekind (Contr_Typ) /= E_Protected_Type then
9880 Error_Msg_Node_2 := Contr_Typ;
9881 Error_Msg_NE
9882 ("interface subprogram & cannot be implemented by a " &
9883 "primitive procedure of task type &", Subp_Alias,
9884 Iface_Alias);
9886 -- An interface subprogram whose implementation kind is By_
9887 -- Protected_Procedure must be implemented by a procedure.
9889 elsif Ekind (Impl_Subp) /= E_Procedure then
9890 Error_Msg_Node_2 := Iface_Alias;
9891 Error_Msg_NE
9892 ("type & must implement abstract subprogram & with a " &
9893 "procedure", Subp_Alias, Contr_Typ);
9895 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9896 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9897 then
9898 Error_Msg_Name_1 := Impl_Kind;
9899 Error_Msg_N
9900 ("overriding operation& must have synchronization%",
9901 Subp_Alias);
9902 end if;
9904 -- If primitive has Optional synchronization, overriding operation
9905 -- must match if it has an explicit synchronization..
9907 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9908 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9909 then
9910 Error_Msg_Name_1 := Impl_Kind;
9911 Error_Msg_N
9912 ("overriding operation& must have syncrhonization%",
9913 Subp_Alias);
9914 end if;
9915 end Check_Pragma_Implemented;
9917 ------------------------------
9918 -- Check_Pragma_Implemented --
9919 ------------------------------
9921 procedure Check_Pragma_Implemented
9922 (Subp : Entity_Id;
9923 Iface_Subp : Entity_Id)
9925 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9926 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9928 begin
9929 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9930 -- and overriding subprogram are different. In general this is an
9931 -- error except when the implementation kind of the overridden
9932 -- subprograms is By_Any or Optional.
9934 if Iface_Kind /= Subp_Kind
9935 and then Iface_Kind /= Name_By_Any
9936 and then Iface_Kind /= Name_Optional
9937 then
9938 if Iface_Kind = Name_By_Entry then
9939 Error_Msg_N
9940 ("incompatible implementation kind, overridden subprogram " &
9941 "is marked By_Entry", Subp);
9942 else
9943 Error_Msg_N
9944 ("incompatible implementation kind, overridden subprogram " &
9945 "is marked By_Protected_Procedure", Subp);
9946 end if;
9947 end if;
9948 end Check_Pragma_Implemented;
9950 --------------------------------
9951 -- Inherit_Pragma_Implemented --
9952 --------------------------------
9954 procedure Inherit_Pragma_Implemented
9955 (Subp : Entity_Id;
9956 Iface_Subp : Entity_Id)
9958 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9959 Loc : constant Source_Ptr := Sloc (Subp);
9960 Impl_Prag : Node_Id;
9962 begin
9963 -- Since the implementation kind is stored as a representation item
9964 -- rather than a flag, create a pragma node.
9966 Impl_Prag :=
9967 Make_Pragma (Loc,
9968 Chars => Name_Implemented,
9969 Pragma_Argument_Associations => New_List (
9970 Make_Pragma_Argument_Association (Loc,
9971 Expression => New_Occurrence_Of (Subp, Loc)),
9973 Make_Pragma_Argument_Association (Loc,
9974 Expression => Make_Identifier (Loc, Iface_Kind))));
9976 -- The pragma doesn't need to be analyzed because it is internally
9977 -- built. It is safe to directly register it as a rep item since we
9978 -- are only interested in the characters of the implementation kind.
9980 Record_Rep_Item (Subp, Impl_Prag);
9981 end Inherit_Pragma_Implemented;
9983 -- Start of processing for Check_Abstract_Overriding
9985 begin
9986 Op_List := Primitive_Operations (T);
9988 -- Loop to check primitive operations
9990 Elmt := First_Elmt (Op_List);
9991 while Present (Elmt) loop
9992 Subp := Node (Elmt);
9993 Alias_Subp := Alias (Subp);
9995 -- Inherited subprograms are identified by the fact that they do not
9996 -- come from source, and the associated source location is the
9997 -- location of the first subtype of the derived type.
9999 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10000 -- subprograms that "require overriding".
10002 -- Special exception, do not complain about failure to override the
10003 -- stream routines _Input and _Output, as well as the primitive
10004 -- operations used in dispatching selects since we always provide
10005 -- automatic overridings for these subprograms.
10007 -- The partial view of T may have been a private extension, for
10008 -- which inherited functions dispatching on result are abstract.
10009 -- If the full view is a null extension, there is no need for
10010 -- overriding in Ada 2005, but wrappers need to be built for them
10011 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10013 if Is_Null_Extension (T)
10014 and then Has_Controlling_Result (Subp)
10015 and then Ada_Version >= Ada_2005
10016 and then Present (Alias_Subp)
10017 and then not Comes_From_Source (Subp)
10018 and then not Is_Abstract_Subprogram (Alias_Subp)
10019 and then not Is_Access_Type (Etype (Subp))
10020 then
10021 null;
10023 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10024 -- processing because this check is done with the aliased
10025 -- entity
10027 elsif Present (Interface_Alias (Subp)) then
10028 null;
10030 elsif (Is_Abstract_Subprogram (Subp)
10031 or else Requires_Overriding (Subp)
10032 or else
10033 (Has_Controlling_Result (Subp)
10034 and then Present (Alias_Subp)
10035 and then not Comes_From_Source (Subp)
10036 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10037 and then not Is_TSS (Subp, TSS_Stream_Input)
10038 and then not Is_TSS (Subp, TSS_Stream_Output)
10039 and then not Is_Abstract_Type (T)
10040 and then not Is_Predefined_Interface_Primitive (Subp)
10042 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10043 -- with abstract interface types because the check will be done
10044 -- with the aliased entity (otherwise we generate a duplicated
10045 -- error message).
10047 and then not Present (Interface_Alias (Subp))
10048 then
10049 if Present (Alias_Subp) then
10051 -- Only perform the check for a derived subprogram when the
10052 -- type has an explicit record extension. This avoids incorrect
10053 -- flagging of abstract subprograms for the case of a type
10054 -- without an extension that is derived from a formal type
10055 -- with a tagged actual (can occur within a private part).
10057 -- Ada 2005 (AI-391): In the case of an inherited function with
10058 -- a controlling result of the type, the rule does not apply if
10059 -- the type is a null extension (unless the parent function
10060 -- itself is abstract, in which case the function must still be
10061 -- be overridden). The expander will generate an overriding
10062 -- wrapper function calling the parent subprogram (see
10063 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10065 Type_Def := Type_Definition (Parent (T));
10067 if Nkind (Type_Def) = N_Derived_Type_Definition
10068 and then Present (Record_Extension_Part (Type_Def))
10069 and then
10070 (Ada_Version < Ada_2005
10071 or else not Is_Null_Extension (T)
10072 or else Ekind (Subp) = E_Procedure
10073 or else not Has_Controlling_Result (Subp)
10074 or else Is_Abstract_Subprogram (Alias_Subp)
10075 or else Requires_Overriding (Subp)
10076 or else Is_Access_Type (Etype (Subp)))
10077 then
10078 -- Avoid reporting error in case of abstract predefined
10079 -- primitive inherited from interface type because the
10080 -- body of internally generated predefined primitives
10081 -- of tagged types are generated later by Freeze_Type
10083 if Is_Interface (Root_Type (T))
10084 and then Is_Abstract_Subprogram (Subp)
10085 and then Is_Predefined_Dispatching_Operation (Subp)
10086 and then not Comes_From_Source (Ultimate_Alias (Subp))
10087 then
10088 null;
10090 -- A null extension is not obliged to override an inherited
10091 -- procedure subject to pragma Extensions_Visible with value
10092 -- False and at least one controlling OUT parameter
10093 -- (SPARK RM 6.1.7(6)).
10095 elsif Is_Null_Extension (T)
10096 and then Is_EVF_Procedure (Subp)
10097 then
10098 null;
10100 else
10101 Error_Msg_NE
10102 ("type must be declared abstract or & overridden",
10103 T, Subp);
10105 -- Traverse the whole chain of aliased subprograms to
10106 -- complete the error notification. This is especially
10107 -- useful for traceability of the chain of entities when
10108 -- the subprogram corresponds with an interface
10109 -- subprogram (which may be defined in another package).
10111 if Present (Alias_Subp) then
10112 declare
10113 E : Entity_Id;
10115 begin
10116 E := Subp;
10117 while Present (Alias (E)) loop
10119 -- Avoid reporting redundant errors on entities
10120 -- inherited from interfaces
10122 if Sloc (E) /= Sloc (T) then
10123 Error_Msg_Sloc := Sloc (E);
10124 Error_Msg_NE
10125 ("\& has been inherited #", T, Subp);
10126 end if;
10128 E := Alias (E);
10129 end loop;
10131 Error_Msg_Sloc := Sloc (E);
10133 -- AI05-0068: report if there is an overriding
10134 -- non-abstract subprogram that is invisible.
10136 if Is_Hidden (E)
10137 and then not Is_Abstract_Subprogram (E)
10138 then
10139 Error_Msg_NE
10140 ("\& subprogram# is not visible",
10141 T, Subp);
10143 -- Clarify the case where a non-null extension must
10144 -- override inherited procedure subject to pragma
10145 -- Extensions_Visible with value False and at least
10146 -- one controlling OUT param.
10148 elsif Is_EVF_Procedure (E) then
10149 Error_Msg_NE
10150 ("\& # is subject to Extensions_Visible False",
10151 T, Subp);
10153 else
10154 Error_Msg_NE
10155 ("\& has been inherited from subprogram #",
10156 T, Subp);
10157 end if;
10158 end;
10159 end if;
10160 end if;
10162 -- Ada 2005 (AI-345): Protected or task type implementing
10163 -- abstract interfaces.
10165 elsif Is_Concurrent_Record_Type (T)
10166 and then Present (Interfaces (T))
10167 then
10168 -- There is no need to check here RM 9.4(11.9/3) since we
10169 -- are processing the corresponding record type and the
10170 -- mode of the overriding subprograms was verified by
10171 -- Check_Conformance when the corresponding concurrent
10172 -- type declaration was analyzed.
10174 Error_Msg_NE
10175 ("interface subprogram & must be overridden", T, Subp);
10177 -- Examine primitive operations of synchronized type to find
10178 -- homonyms that have the wrong profile.
10180 declare
10181 Prim : Entity_Id;
10183 begin
10184 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10185 while Present (Prim) loop
10186 if Chars (Prim) = Chars (Subp) then
10187 Error_Msg_NE
10188 ("profile is not type conformant with prefixed "
10189 & "view profile of inherited operation&",
10190 Prim, Subp);
10191 end if;
10193 Next_Entity (Prim);
10194 end loop;
10195 end;
10196 end if;
10198 else
10199 Error_Msg_Node_2 := T;
10200 Error_Msg_N
10201 ("abstract subprogram& not allowed for type&", Subp);
10203 -- Also post unconditional warning on the type (unconditional
10204 -- so that if there are more than one of these cases, we get
10205 -- them all, and not just the first one).
10207 Error_Msg_Node_2 := Subp;
10208 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10209 end if;
10211 -- A subprogram subject to pragma Extensions_Visible with value
10212 -- "True" cannot override a subprogram subject to the same pragma
10213 -- with value "False" (SPARK RM 6.1.7(5)).
10215 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10216 and then Present (Overridden_Operation (Subp))
10217 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10218 Extensions_Visible_False
10219 then
10220 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10221 Error_Msg_N
10222 ("subprogram & with Extensions_Visible True cannot override "
10223 & "subprogram # with Extensions_Visible False", Subp);
10224 end if;
10226 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10228 -- Subp is an expander-generated procedure which maps an interface
10229 -- alias to a protected wrapper. The interface alias is flagged by
10230 -- pragma Implemented. Ensure that Subp is a procedure when the
10231 -- implementation kind is By_Protected_Procedure or an entry when
10232 -- By_Entry.
10234 if Ada_Version >= Ada_2012
10235 and then Is_Hidden (Subp)
10236 and then Present (Interface_Alias (Subp))
10237 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10238 then
10239 Check_Pragma_Implemented (Subp);
10240 end if;
10242 -- Subp is an interface primitive which overrides another interface
10243 -- primitive marked with pragma Implemented.
10245 if Ada_Version >= Ada_2012
10246 and then Present (Overridden_Operation (Subp))
10247 and then Has_Rep_Pragma
10248 (Overridden_Operation (Subp), Name_Implemented)
10249 then
10250 -- If the overriding routine is also marked by Implemented, check
10251 -- that the two implementation kinds are conforming.
10253 if Has_Rep_Pragma (Subp, Name_Implemented) then
10254 Check_Pragma_Implemented
10255 (Subp => Subp,
10256 Iface_Subp => Overridden_Operation (Subp));
10258 -- Otherwise the overriding routine inherits the implementation
10259 -- kind from the overridden subprogram.
10261 else
10262 Inherit_Pragma_Implemented
10263 (Subp => Subp,
10264 Iface_Subp => Overridden_Operation (Subp));
10265 end if;
10266 end if;
10268 -- If the operation is a wrapper for a synchronized primitive, it
10269 -- may be called indirectly through a dispatching select. We assume
10270 -- that it will be referenced elsewhere indirectly, and suppress
10271 -- warnings about an unused entity.
10273 if Is_Primitive_Wrapper (Subp)
10274 and then Present (Wrapped_Entity (Subp))
10275 then
10276 Set_Referenced (Wrapped_Entity (Subp));
10277 end if;
10279 Next_Elmt (Elmt);
10280 end loop;
10281 end Check_Abstract_Overriding;
10283 ------------------------------------------------
10284 -- Check_Access_Discriminant_Requires_Limited --
10285 ------------------------------------------------
10287 procedure Check_Access_Discriminant_Requires_Limited
10288 (D : Node_Id;
10289 Loc : Node_Id)
10291 begin
10292 -- A discriminant_specification for an access discriminant shall appear
10293 -- only in the declaration for a task or protected type, or for a type
10294 -- with the reserved word 'limited' in its definition or in one of its
10295 -- ancestors (RM 3.7(10)).
10297 -- AI-0063: The proper condition is that type must be immutably limited,
10298 -- or else be a partial view.
10300 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10301 if Is_Limited_View (Current_Scope)
10302 or else
10303 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10304 and then Limited_Present (Parent (Current_Scope)))
10305 then
10306 null;
10308 else
10309 Error_Msg_N
10310 ("access discriminants allowed only for limited types", Loc);
10311 end if;
10312 end if;
10313 end Check_Access_Discriminant_Requires_Limited;
10315 -----------------------------------
10316 -- Check_Aliased_Component_Types --
10317 -----------------------------------
10319 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10320 C : Entity_Id;
10322 begin
10323 -- ??? Also need to check components of record extensions, but not
10324 -- components of protected types (which are always limited).
10326 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10327 -- types to be unconstrained. This is safe because it is illegal to
10328 -- create access subtypes to such types with explicit discriminant
10329 -- constraints.
10331 if not Is_Limited_Type (T) then
10332 if Ekind (T) = E_Record_Type then
10333 C := First_Component (T);
10334 while Present (C) loop
10335 if Is_Aliased (C)
10336 and then Has_Discriminants (Etype (C))
10337 and then not Is_Constrained (Etype (C))
10338 and then not In_Instance_Body
10339 and then Ada_Version < Ada_2005
10340 then
10341 Error_Msg_N
10342 ("aliased component must be constrained (RM 3.6(11))",
10344 end if;
10346 Next_Component (C);
10347 end loop;
10349 elsif Ekind (T) = E_Array_Type then
10350 if Has_Aliased_Components (T)
10351 and then Has_Discriminants (Component_Type (T))
10352 and then not Is_Constrained (Component_Type (T))
10353 and then not In_Instance_Body
10354 and then Ada_Version < Ada_2005
10355 then
10356 Error_Msg_N
10357 ("aliased component type must be constrained (RM 3.6(11))",
10359 end if;
10360 end if;
10361 end if;
10362 end Check_Aliased_Component_Types;
10364 ---------------------------------------
10365 -- Check_Anonymous_Access_Components --
10366 ---------------------------------------
10368 procedure Check_Anonymous_Access_Components
10369 (Typ_Decl : Node_Id;
10370 Typ : Entity_Id;
10371 Prev : Entity_Id;
10372 Comp_List : Node_Id)
10374 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10375 Anon_Access : Entity_Id;
10376 Acc_Def : Node_Id;
10377 Comp : Node_Id;
10378 Comp_Def : Node_Id;
10379 Decl : Node_Id;
10380 Type_Def : Node_Id;
10382 procedure Build_Incomplete_Type_Declaration;
10383 -- If the record type contains components that include an access to the
10384 -- current record, then create an incomplete type declaration for the
10385 -- record, to be used as the designated type of the anonymous access.
10386 -- This is done only once, and only if there is no previous partial
10387 -- view of the type.
10389 function Designates_T (Subt : Node_Id) return Boolean;
10390 -- Check whether a node designates the enclosing record type, or 'Class
10391 -- of that type
10393 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10394 -- Check whether an access definition includes a reference to
10395 -- the enclosing record type. The reference can be a subtype mark
10396 -- in the access definition itself, a 'Class attribute reference, or
10397 -- recursively a reference appearing in a parameter specification
10398 -- or result definition of an access_to_subprogram definition.
10400 --------------------------------------
10401 -- Build_Incomplete_Type_Declaration --
10402 --------------------------------------
10404 procedure Build_Incomplete_Type_Declaration is
10405 Decl : Node_Id;
10406 Inc_T : Entity_Id;
10407 H : Entity_Id;
10409 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10410 -- it's "is new ... with record" or else "is tagged record ...".
10412 Is_Tagged : constant Boolean :=
10413 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10414 and then
10415 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10416 or else
10417 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10418 and then Tagged_Present (Type_Definition (Typ_Decl)));
10420 begin
10421 -- If there is a previous partial view, no need to create a new one
10422 -- If the partial view, given by Prev, is incomplete, If Prev is
10423 -- a private declaration, full declaration is flagged accordingly.
10425 if Prev /= Typ then
10426 if Is_Tagged then
10427 Make_Class_Wide_Type (Prev);
10428 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10429 Set_Etype (Class_Wide_Type (Typ), Typ);
10430 end if;
10432 return;
10434 elsif Has_Private_Declaration (Typ) then
10436 -- If we refer to T'Class inside T, and T is the completion of a
10437 -- private type, then make sure the class-wide type exists.
10439 if Is_Tagged then
10440 Make_Class_Wide_Type (Typ);
10441 end if;
10443 return;
10445 -- If there was a previous anonymous access type, the incomplete
10446 -- type declaration will have been created already.
10448 elsif Present (Current_Entity (Typ))
10449 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10450 and then Full_View (Current_Entity (Typ)) = Typ
10451 then
10452 if Is_Tagged
10453 and then Comes_From_Source (Current_Entity (Typ))
10454 and then not Is_Tagged_Type (Current_Entity (Typ))
10455 then
10456 Make_Class_Wide_Type (Typ);
10457 Error_Msg_N
10458 ("incomplete view of tagged type should be declared tagged??",
10459 Parent (Current_Entity (Typ)));
10460 end if;
10461 return;
10463 else
10464 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10465 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10467 -- Type has already been inserted into the current scope. Remove
10468 -- it, and add incomplete declaration for type, so that subsequent
10469 -- anonymous access types can use it. The entity is unchained from
10470 -- the homonym list and from immediate visibility. After analysis,
10471 -- the entity in the incomplete declaration becomes immediately
10472 -- visible in the record declaration that follows.
10474 H := Current_Entity (Typ);
10476 if H = Typ then
10477 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10478 else
10479 while Present (H)
10480 and then Homonym (H) /= Typ
10481 loop
10482 H := Homonym (Typ);
10483 end loop;
10485 Set_Homonym (H, Homonym (Typ));
10486 end if;
10488 Insert_Before (Typ_Decl, Decl);
10489 Analyze (Decl);
10490 Set_Full_View (Inc_T, Typ);
10492 if Is_Tagged then
10494 -- Create a common class-wide type for both views, and set the
10495 -- Etype of the class-wide type to the full view.
10497 Make_Class_Wide_Type (Inc_T);
10498 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10499 Set_Etype (Class_Wide_Type (Typ), Typ);
10500 end if;
10501 end if;
10502 end Build_Incomplete_Type_Declaration;
10504 ------------------
10505 -- Designates_T --
10506 ------------------
10508 function Designates_T (Subt : Node_Id) return Boolean is
10509 Type_Id : constant Name_Id := Chars (Typ);
10511 function Names_T (Nam : Node_Id) return Boolean;
10512 -- The record type has not been introduced in the current scope
10513 -- yet, so we must examine the name of the type itself, either
10514 -- an identifier T, or an expanded name of the form P.T, where
10515 -- P denotes the current scope.
10517 -------------
10518 -- Names_T --
10519 -------------
10521 function Names_T (Nam : Node_Id) return Boolean is
10522 begin
10523 if Nkind (Nam) = N_Identifier then
10524 return Chars (Nam) = Type_Id;
10526 elsif Nkind (Nam) = N_Selected_Component then
10527 if Chars (Selector_Name (Nam)) = Type_Id then
10528 if Nkind (Prefix (Nam)) = N_Identifier then
10529 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10531 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10532 return Chars (Selector_Name (Prefix (Nam))) =
10533 Chars (Current_Scope);
10534 else
10535 return False;
10536 end if;
10538 else
10539 return False;
10540 end if;
10542 else
10543 return False;
10544 end if;
10545 end Names_T;
10547 -- Start of processing for Designates_T
10549 begin
10550 if Nkind (Subt) = N_Identifier then
10551 return Chars (Subt) = Type_Id;
10553 -- Reference can be through an expanded name which has not been
10554 -- analyzed yet, and which designates enclosing scopes.
10556 elsif Nkind (Subt) = N_Selected_Component then
10557 if Names_T (Subt) then
10558 return True;
10560 -- Otherwise it must denote an entity that is already visible.
10561 -- The access definition may name a subtype of the enclosing
10562 -- type, if there is a previous incomplete declaration for it.
10564 else
10565 Find_Selected_Component (Subt);
10566 return
10567 Is_Entity_Name (Subt)
10568 and then Scope (Entity (Subt)) = Current_Scope
10569 and then
10570 (Chars (Base_Type (Entity (Subt))) = Type_Id
10571 or else
10572 (Is_Class_Wide_Type (Entity (Subt))
10573 and then
10574 Chars (Etype (Base_Type (Entity (Subt)))) =
10575 Type_Id));
10576 end if;
10578 -- A reference to the current type may appear as the prefix of
10579 -- a 'Class attribute.
10581 elsif Nkind (Subt) = N_Attribute_Reference
10582 and then Attribute_Name (Subt) = Name_Class
10583 then
10584 return Names_T (Prefix (Subt));
10586 else
10587 return False;
10588 end if;
10589 end Designates_T;
10591 ----------------
10592 -- Mentions_T --
10593 ----------------
10595 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10596 Param_Spec : Node_Id;
10598 Acc_Subprg : constant Node_Id :=
10599 Access_To_Subprogram_Definition (Acc_Def);
10601 begin
10602 if No (Acc_Subprg) then
10603 return Designates_T (Subtype_Mark (Acc_Def));
10604 end if;
10606 -- Component is an access_to_subprogram: examine its formals,
10607 -- and result definition in the case of an access_to_function.
10609 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10610 while Present (Param_Spec) loop
10611 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10612 and then Mentions_T (Parameter_Type (Param_Spec))
10613 then
10614 return True;
10616 elsif Designates_T (Parameter_Type (Param_Spec)) then
10617 return True;
10618 end if;
10620 Next (Param_Spec);
10621 end loop;
10623 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10624 if Nkind (Result_Definition (Acc_Subprg)) =
10625 N_Access_Definition
10626 then
10627 return Mentions_T (Result_Definition (Acc_Subprg));
10628 else
10629 return Designates_T (Result_Definition (Acc_Subprg));
10630 end if;
10631 end if;
10633 return False;
10634 end Mentions_T;
10636 -- Start of processing for Check_Anonymous_Access_Components
10638 begin
10639 if No (Comp_List) then
10640 return;
10641 end if;
10643 Comp := First (Component_Items (Comp_List));
10644 while Present (Comp) loop
10645 if Nkind (Comp) = N_Component_Declaration
10646 and then Present
10647 (Access_Definition (Component_Definition (Comp)))
10648 and then
10649 Mentions_T (Access_Definition (Component_Definition (Comp)))
10650 then
10651 Comp_Def := Component_Definition (Comp);
10652 Acc_Def :=
10653 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10655 Build_Incomplete_Type_Declaration;
10656 Anon_Access := Make_Temporary (Loc, 'S');
10658 -- Create a declaration for the anonymous access type: either
10659 -- an access_to_object or an access_to_subprogram.
10661 if Present (Acc_Def) then
10662 if Nkind (Acc_Def) = N_Access_Function_Definition then
10663 Type_Def :=
10664 Make_Access_Function_Definition (Loc,
10665 Parameter_Specifications =>
10666 Parameter_Specifications (Acc_Def),
10667 Result_Definition => Result_Definition (Acc_Def));
10668 else
10669 Type_Def :=
10670 Make_Access_Procedure_Definition (Loc,
10671 Parameter_Specifications =>
10672 Parameter_Specifications (Acc_Def));
10673 end if;
10675 else
10676 Type_Def :=
10677 Make_Access_To_Object_Definition (Loc,
10678 Subtype_Indication =>
10679 Relocate_Node
10680 (Subtype_Mark (Access_Definition (Comp_Def))));
10682 Set_Constant_Present
10683 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10684 Set_All_Present
10685 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10686 end if;
10688 Set_Null_Exclusion_Present
10689 (Type_Def,
10690 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10692 Decl :=
10693 Make_Full_Type_Declaration (Loc,
10694 Defining_Identifier => Anon_Access,
10695 Type_Definition => Type_Def);
10697 Insert_Before (Typ_Decl, Decl);
10698 Analyze (Decl);
10700 -- If an access to subprogram, create the extra formals
10702 if Present (Acc_Def) then
10703 Create_Extra_Formals (Designated_Type (Anon_Access));
10705 -- If an access to object, preserve entity of designated type,
10706 -- for ASIS use, before rewriting the component definition.
10708 else
10709 declare
10710 Desig : Entity_Id;
10712 begin
10713 Desig := Entity (Subtype_Indication (Type_Def));
10715 -- If the access definition is to the current record,
10716 -- the visible entity at this point is an incomplete
10717 -- type. Retrieve the full view to simplify ASIS queries
10719 if Ekind (Desig) = E_Incomplete_Type then
10720 Desig := Full_View (Desig);
10721 end if;
10723 Set_Entity
10724 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10725 end;
10726 end if;
10728 Rewrite (Comp_Def,
10729 Make_Component_Definition (Loc,
10730 Subtype_Indication =>
10731 New_Occurrence_Of (Anon_Access, Loc)));
10733 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10734 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10735 else
10736 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10737 end if;
10739 Set_Is_Local_Anonymous_Access (Anon_Access);
10740 end if;
10742 Next (Comp);
10743 end loop;
10745 if Present (Variant_Part (Comp_List)) then
10746 declare
10747 V : Node_Id;
10748 begin
10749 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10750 while Present (V) loop
10751 Check_Anonymous_Access_Components
10752 (Typ_Decl, Typ, Prev, Component_List (V));
10753 Next_Non_Pragma (V);
10754 end loop;
10755 end;
10756 end if;
10757 end Check_Anonymous_Access_Components;
10759 ----------------------
10760 -- Check_Completion --
10761 ----------------------
10763 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10764 E : Entity_Id;
10766 procedure Post_Error;
10767 -- Post error message for lack of completion for entity E
10769 ----------------
10770 -- Post_Error --
10771 ----------------
10773 procedure Post_Error is
10774 procedure Missing_Body;
10775 -- Output missing body message
10777 ------------------
10778 -- Missing_Body --
10779 ------------------
10781 procedure Missing_Body is
10782 begin
10783 -- Spec is in same unit, so we can post on spec
10785 if In_Same_Source_Unit (Body_Id, E) then
10786 Error_Msg_N ("missing body for &", E);
10788 -- Spec is in a separate unit, so we have to post on the body
10790 else
10791 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10792 end if;
10793 end Missing_Body;
10795 -- Start of processing for Post_Error
10797 begin
10798 if not Comes_From_Source (E) then
10799 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10801 -- It may be an anonymous protected type created for a
10802 -- single variable. Post error on variable, if present.
10804 declare
10805 Var : Entity_Id;
10807 begin
10808 Var := First_Entity (Current_Scope);
10809 while Present (Var) loop
10810 exit when Etype (Var) = E
10811 and then Comes_From_Source (Var);
10813 Next_Entity (Var);
10814 end loop;
10816 if Present (Var) then
10817 E := Var;
10818 end if;
10819 end;
10820 end if;
10821 end if;
10823 -- If a generated entity has no completion, then either previous
10824 -- semantic errors have disabled the expansion phase, or else we had
10825 -- missing subunits, or else we are compiling without expansion,
10826 -- or else something is very wrong.
10828 if not Comes_From_Source (E) then
10829 pragma Assert
10830 (Serious_Errors_Detected > 0
10831 or else Configurable_Run_Time_Violations > 0
10832 or else Subunits_Missing
10833 or else not Expander_Active);
10834 return;
10836 -- Here for source entity
10838 else
10839 -- Here if no body to post the error message, so we post the error
10840 -- on the declaration that has no completion. This is not really
10841 -- the right place to post it, think about this later ???
10843 if No (Body_Id) then
10844 if Is_Type (E) then
10845 Error_Msg_NE
10846 ("missing full declaration for }", Parent (E), E);
10847 else
10848 Error_Msg_NE ("missing body for &", Parent (E), E);
10849 end if;
10851 -- Package body has no completion for a declaration that appears
10852 -- in the corresponding spec. Post error on the body, with a
10853 -- reference to the non-completed declaration.
10855 else
10856 Error_Msg_Sloc := Sloc (E);
10858 if Is_Type (E) then
10859 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10861 elsif Is_Overloadable (E)
10862 and then Current_Entity_In_Scope (E) /= E
10863 then
10864 -- It may be that the completion is mistyped and appears as
10865 -- a distinct overloading of the entity.
10867 declare
10868 Candidate : constant Entity_Id :=
10869 Current_Entity_In_Scope (E);
10870 Decl : constant Node_Id :=
10871 Unit_Declaration_Node (Candidate);
10873 begin
10874 if Is_Overloadable (Candidate)
10875 and then Ekind (Candidate) = Ekind (E)
10876 and then Nkind (Decl) = N_Subprogram_Body
10877 and then Acts_As_Spec (Decl)
10878 then
10879 Check_Type_Conformant (Candidate, E);
10881 else
10882 Missing_Body;
10883 end if;
10884 end;
10886 else
10887 Missing_Body;
10888 end if;
10889 end if;
10890 end if;
10891 end Post_Error;
10893 -- Local variables
10895 Pack_Id : constant Entity_Id := Current_Scope;
10897 -- Start of processing for Check_Completion
10899 begin
10900 E := First_Entity (Pack_Id);
10901 while Present (E) loop
10902 if Is_Intrinsic_Subprogram (E) then
10903 null;
10905 -- The following situation requires special handling: a child unit
10906 -- that appears in the context clause of the body of its parent:
10908 -- procedure Parent.Child (...);
10910 -- with Parent.Child;
10911 -- package body Parent is
10913 -- Here Parent.Child appears as a local entity, but should not be
10914 -- flagged as requiring completion, because it is a compilation
10915 -- unit.
10917 -- Ignore missing completion for a subprogram that does not come from
10918 -- source (including the _Call primitive operation of RAS types,
10919 -- which has to have the flag Comes_From_Source for other purposes):
10920 -- we assume that the expander will provide the missing completion.
10921 -- In case of previous errors, other expansion actions that provide
10922 -- bodies for null procedures with not be invoked, so inhibit message
10923 -- in those cases.
10925 -- Note that E_Operator is not in the list that follows, because
10926 -- this kind is reserved for predefined operators, that are
10927 -- intrinsic and do not need completion.
10929 elsif Ekind_In (E, E_Function,
10930 E_Procedure,
10931 E_Generic_Function,
10932 E_Generic_Procedure)
10933 then
10934 if Has_Completion (E) then
10935 null;
10937 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10938 null;
10940 elsif Is_Subprogram (E)
10941 and then (not Comes_From_Source (E)
10942 or else Chars (E) = Name_uCall)
10943 then
10944 null;
10946 elsif
10947 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10948 then
10949 null;
10951 elsif Nkind (Parent (E)) = N_Procedure_Specification
10952 and then Null_Present (Parent (E))
10953 and then Serious_Errors_Detected > 0
10954 then
10955 null;
10957 else
10958 Post_Error;
10959 end if;
10961 elsif Is_Entry (E) then
10962 if not Has_Completion (E) and then
10963 (Ekind (Scope (E)) = E_Protected_Object
10964 or else Ekind (Scope (E)) = E_Protected_Type)
10965 then
10966 Post_Error;
10967 end if;
10969 elsif Is_Package_Or_Generic_Package (E) then
10970 if Unit_Requires_Body (E) then
10971 if not Has_Completion (E)
10972 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
10973 N_Compilation_Unit
10974 then
10975 Post_Error;
10976 end if;
10978 elsif not Is_Child_Unit (E) then
10979 May_Need_Implicit_Body (E);
10980 end if;
10982 -- A formal incomplete type (Ada 2012) does not require a completion;
10983 -- other incomplete type declarations do.
10985 elsif Ekind (E) = E_Incomplete_Type
10986 and then No (Underlying_Type (E))
10987 and then not Is_Generic_Type (E)
10988 then
10989 Post_Error;
10991 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
10992 and then not Has_Completion (E)
10993 then
10994 Post_Error;
10996 -- A single task declared in the current scope is a constant, verify
10997 -- that the body of its anonymous type is in the same scope. If the
10998 -- task is defined elsewhere, this may be a renaming declaration for
10999 -- which no completion is needed.
11001 elsif Ekind (E) = E_Constant
11002 and then Ekind (Etype (E)) = E_Task_Type
11003 and then not Has_Completion (Etype (E))
11004 and then Scope (Etype (E)) = Current_Scope
11005 then
11006 Post_Error;
11008 elsif Ekind (E) = E_Protected_Object
11009 and then not Has_Completion (Etype (E))
11010 then
11011 Post_Error;
11013 elsif Ekind (E) = E_Record_Type then
11014 if Is_Tagged_Type (E) then
11015 Check_Abstract_Overriding (E);
11016 Check_Conventions (E);
11017 end if;
11019 Check_Aliased_Component_Types (E);
11021 elsif Ekind (E) = E_Array_Type then
11022 Check_Aliased_Component_Types (E);
11024 end if;
11026 Next_Entity (E);
11027 end loop;
11028 end Check_Completion;
11030 ------------------------------------
11031 -- Check_CPP_Type_Has_No_Defaults --
11032 ------------------------------------
11034 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11035 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11036 Clist : Node_Id;
11037 Comp : Node_Id;
11039 begin
11040 -- Obtain the component list
11042 if Nkind (Tdef) = N_Record_Definition then
11043 Clist := Component_List (Tdef);
11044 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11045 Clist := Component_List (Record_Extension_Part (Tdef));
11046 end if;
11048 -- Check all components to ensure no default expressions
11050 if Present (Clist) then
11051 Comp := First (Component_Items (Clist));
11052 while Present (Comp) loop
11053 if Present (Expression (Comp)) then
11054 Error_Msg_N
11055 ("component of imported 'C'P'P type cannot have "
11056 & "default expression", Expression (Comp));
11057 end if;
11059 Next (Comp);
11060 end loop;
11061 end if;
11062 end Check_CPP_Type_Has_No_Defaults;
11064 ----------------------------
11065 -- Check_Delta_Expression --
11066 ----------------------------
11068 procedure Check_Delta_Expression (E : Node_Id) is
11069 begin
11070 if not (Is_Real_Type (Etype (E))) then
11071 Wrong_Type (E, Any_Real);
11073 elsif not Is_OK_Static_Expression (E) then
11074 Flag_Non_Static_Expr
11075 ("non-static expression used for delta value!", E);
11077 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11078 Error_Msg_N ("delta expression must be positive", E);
11080 else
11081 return;
11082 end if;
11084 -- If any of above errors occurred, then replace the incorrect
11085 -- expression by the real 0.1, which should prevent further errors.
11087 Rewrite (E,
11088 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11089 Analyze_And_Resolve (E, Standard_Float);
11090 end Check_Delta_Expression;
11092 -----------------------------
11093 -- Check_Digits_Expression --
11094 -----------------------------
11096 procedure Check_Digits_Expression (E : Node_Id) is
11097 begin
11098 if not (Is_Integer_Type (Etype (E))) then
11099 Wrong_Type (E, Any_Integer);
11101 elsif not Is_OK_Static_Expression (E) then
11102 Flag_Non_Static_Expr
11103 ("non-static expression used for digits value!", E);
11105 elsif Expr_Value (E) <= 0 then
11106 Error_Msg_N ("digits value must be greater than zero", E);
11108 else
11109 return;
11110 end if;
11112 -- If any of above errors occurred, then replace the incorrect
11113 -- expression by the integer 1, which should prevent further errors.
11115 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11116 Analyze_And_Resolve (E, Standard_Integer);
11118 end Check_Digits_Expression;
11120 --------------------------
11121 -- Check_Initialization --
11122 --------------------------
11124 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11125 begin
11126 -- Special processing for limited types
11128 if Is_Limited_Type (T)
11129 and then not In_Instance
11130 and then not In_Inlined_Body
11131 then
11132 if not OK_For_Limited_Init (T, Exp) then
11134 -- In GNAT mode, this is just a warning, to allow it to be evilly
11135 -- turned off. Otherwise it is a real error.
11137 if GNAT_Mode then
11138 Error_Msg_N
11139 ("??cannot initialize entities of limited type!", Exp);
11141 elsif Ada_Version < Ada_2005 then
11143 -- The side effect removal machinery may generate illegal Ada
11144 -- code to avoid the usage of access types and 'reference in
11145 -- SPARK mode. Since this is legal code with respect to theorem
11146 -- proving, do not emit the error.
11148 if GNATprove_Mode
11149 and then Nkind (Exp) = N_Function_Call
11150 and then Nkind (Parent (Exp)) = N_Object_Declaration
11151 and then not Comes_From_Source
11152 (Defining_Identifier (Parent (Exp)))
11153 then
11154 null;
11156 else
11157 Error_Msg_N
11158 ("cannot initialize entities of limited type", Exp);
11159 Explain_Limited_Type (T, Exp);
11160 end if;
11162 else
11163 -- Specialize error message according to kind of illegal
11164 -- initial expression.
11166 if Nkind (Exp) = N_Type_Conversion
11167 and then Nkind (Expression (Exp)) = N_Function_Call
11168 then
11169 Error_Msg_N
11170 ("illegal context for call"
11171 & " to function with limited result", Exp);
11173 else
11174 Error_Msg_N
11175 ("initialization of limited object requires aggregate "
11176 & "or function call", Exp);
11177 end if;
11178 end if;
11179 end if;
11180 end if;
11182 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11183 -- set unless we can be sure that no range check is required.
11185 if (GNATprove_Mode or not Expander_Active)
11186 and then Is_Scalar_Type (T)
11187 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11188 then
11189 Set_Do_Range_Check (Exp);
11190 end if;
11191 end Check_Initialization;
11193 ----------------------
11194 -- Check_Interfaces --
11195 ----------------------
11197 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11198 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11200 Iface : Node_Id;
11201 Iface_Def : Node_Id;
11202 Iface_Typ : Entity_Id;
11203 Parent_Node : Node_Id;
11205 Is_Task : Boolean := False;
11206 -- Set True if parent type or any progenitor is a task interface
11208 Is_Protected : Boolean := False;
11209 -- Set True if parent type or any progenitor is a protected interface
11211 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11212 -- Check that a progenitor is compatible with declaration. If an error
11213 -- message is output, it is posted on Error_Node.
11215 ------------------
11216 -- Check_Ifaces --
11217 ------------------
11219 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11220 Iface_Id : constant Entity_Id :=
11221 Defining_Identifier (Parent (Iface_Def));
11222 Type_Def : Node_Id;
11224 begin
11225 if Nkind (N) = N_Private_Extension_Declaration then
11226 Type_Def := N;
11227 else
11228 Type_Def := Type_Definition (N);
11229 end if;
11231 if Is_Task_Interface (Iface_Id) then
11232 Is_Task := True;
11234 elsif Is_Protected_Interface (Iface_Id) then
11235 Is_Protected := True;
11236 end if;
11238 if Is_Synchronized_Interface (Iface_Id) then
11240 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11241 -- extension derived from a synchronized interface must explicitly
11242 -- be declared synchronized, because the full view will be a
11243 -- synchronized type.
11245 if Nkind (N) = N_Private_Extension_Declaration then
11246 if not Synchronized_Present (N) then
11247 Error_Msg_NE
11248 ("private extension of& must be explicitly synchronized",
11249 N, Iface_Id);
11250 end if;
11252 -- However, by 3.9.4(16/2), a full type that is a record extension
11253 -- is never allowed to derive from a synchronized interface (note
11254 -- that interfaces must be excluded from this check, because those
11255 -- are represented by derived type definitions in some cases).
11257 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11258 and then not Interface_Present (Type_Definition (N))
11259 then
11260 Error_Msg_N ("record extension cannot derive from synchronized "
11261 & "interface", Error_Node);
11262 end if;
11263 end if;
11265 -- Check that the characteristics of the progenitor are compatible
11266 -- with the explicit qualifier in the declaration.
11267 -- The check only applies to qualifiers that come from source.
11268 -- Limited_Present also appears in the declaration of corresponding
11269 -- records, and the check does not apply to them.
11271 if Limited_Present (Type_Def)
11272 and then not
11273 Is_Concurrent_Record_Type (Defining_Identifier (N))
11274 then
11275 if Is_Limited_Interface (Parent_Type)
11276 and then not Is_Limited_Interface (Iface_Id)
11277 then
11278 Error_Msg_NE
11279 ("progenitor & must be limited interface",
11280 Error_Node, Iface_Id);
11282 elsif
11283 (Task_Present (Iface_Def)
11284 or else Protected_Present (Iface_Def)
11285 or else Synchronized_Present (Iface_Def))
11286 and then Nkind (N) /= N_Private_Extension_Declaration
11287 and then not Error_Posted (N)
11288 then
11289 Error_Msg_NE
11290 ("progenitor & must be limited interface",
11291 Error_Node, Iface_Id);
11292 end if;
11294 -- Protected interfaces can only inherit from limited, synchronized
11295 -- or protected interfaces.
11297 elsif Nkind (N) = N_Full_Type_Declaration
11298 and then Protected_Present (Type_Def)
11299 then
11300 if Limited_Present (Iface_Def)
11301 or else Synchronized_Present (Iface_Def)
11302 or else Protected_Present (Iface_Def)
11303 then
11304 null;
11306 elsif Task_Present (Iface_Def) then
11307 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11308 & "from task interface", Error_Node);
11310 else
11311 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11312 & "from non-limited interface", Error_Node);
11313 end if;
11315 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11316 -- limited and synchronized.
11318 elsif Synchronized_Present (Type_Def) then
11319 if Limited_Present (Iface_Def)
11320 or else Synchronized_Present (Iface_Def)
11321 then
11322 null;
11324 elsif Protected_Present (Iface_Def)
11325 and then Nkind (N) /= N_Private_Extension_Declaration
11326 then
11327 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11328 & "from protected interface", Error_Node);
11330 elsif Task_Present (Iface_Def)
11331 and then Nkind (N) /= N_Private_Extension_Declaration
11332 then
11333 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11334 & "from task interface", Error_Node);
11336 elsif not Is_Limited_Interface (Iface_Id) then
11337 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11338 & "from non-limited interface", Error_Node);
11339 end if;
11341 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11342 -- synchronized or task interfaces.
11344 elsif Nkind (N) = N_Full_Type_Declaration
11345 and then Task_Present (Type_Def)
11346 then
11347 if Limited_Present (Iface_Def)
11348 or else Synchronized_Present (Iface_Def)
11349 or else Task_Present (Iface_Def)
11350 then
11351 null;
11353 elsif Protected_Present (Iface_Def) then
11354 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11355 & "protected interface", Error_Node);
11357 else
11358 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11359 & "non-limited interface", Error_Node);
11360 end if;
11361 end if;
11362 end Check_Ifaces;
11364 -- Start of processing for Check_Interfaces
11366 begin
11367 if Is_Interface (Parent_Type) then
11368 if Is_Task_Interface (Parent_Type) then
11369 Is_Task := True;
11371 elsif Is_Protected_Interface (Parent_Type) then
11372 Is_Protected := True;
11373 end if;
11374 end if;
11376 if Nkind (N) = N_Private_Extension_Declaration then
11378 -- Check that progenitors are compatible with declaration
11380 Iface := First (Interface_List (Def));
11381 while Present (Iface) loop
11382 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11384 Parent_Node := Parent (Base_Type (Iface_Typ));
11385 Iface_Def := Type_Definition (Parent_Node);
11387 if not Is_Interface (Iface_Typ) then
11388 Diagnose_Interface (Iface, Iface_Typ);
11389 else
11390 Check_Ifaces (Iface_Def, Iface);
11391 end if;
11393 Next (Iface);
11394 end loop;
11396 if Is_Task and Is_Protected then
11397 Error_Msg_N
11398 ("type cannot derive from task and protected interface", N);
11399 end if;
11401 return;
11402 end if;
11404 -- Full type declaration of derived type.
11405 -- Check compatibility with parent if it is interface type
11407 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11408 and then Is_Interface (Parent_Type)
11409 then
11410 Parent_Node := Parent (Parent_Type);
11412 -- More detailed checks for interface varieties
11414 Check_Ifaces
11415 (Iface_Def => Type_Definition (Parent_Node),
11416 Error_Node => Subtype_Indication (Type_Definition (N)));
11417 end if;
11419 Iface := First (Interface_List (Def));
11420 while Present (Iface) loop
11421 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11423 Parent_Node := Parent (Base_Type (Iface_Typ));
11424 Iface_Def := Type_Definition (Parent_Node);
11426 if not Is_Interface (Iface_Typ) then
11427 Diagnose_Interface (Iface, Iface_Typ);
11429 else
11430 -- "The declaration of a specific descendant of an interface
11431 -- type freezes the interface type" RM 13.14
11433 Freeze_Before (N, Iface_Typ);
11434 Check_Ifaces (Iface_Def, Error_Node => Iface);
11435 end if;
11437 Next (Iface);
11438 end loop;
11440 if Is_Task and Is_Protected then
11441 Error_Msg_N
11442 ("type cannot derive from task and protected interface", N);
11443 end if;
11444 end Check_Interfaces;
11446 ------------------------------------
11447 -- Check_Or_Process_Discriminants --
11448 ------------------------------------
11450 -- If an incomplete or private type declaration was already given for the
11451 -- type, the discriminants may have already been processed if they were
11452 -- present on the incomplete declaration. In this case a full conformance
11453 -- check has been performed in Find_Type_Name, and we then recheck here
11454 -- some properties that can't be checked on the partial view alone.
11455 -- Otherwise we call Process_Discriminants.
11457 procedure Check_Or_Process_Discriminants
11458 (N : Node_Id;
11459 T : Entity_Id;
11460 Prev : Entity_Id := Empty)
11462 begin
11463 if Has_Discriminants (T) then
11465 -- Discriminants are already set on T if they were already present
11466 -- on the partial view. Make them visible to component declarations.
11468 declare
11469 D : Entity_Id;
11470 -- Discriminant on T (full view) referencing expr on partial view
11472 Prev_D : Entity_Id;
11473 -- Entity of corresponding discriminant on partial view
11475 New_D : Node_Id;
11476 -- Discriminant specification for full view, expression is
11477 -- the syntactic copy on full view (which has been checked for
11478 -- conformance with partial view), only used here to post error
11479 -- message.
11481 begin
11482 D := First_Discriminant (T);
11483 New_D := First (Discriminant_Specifications (N));
11484 while Present (D) loop
11485 Prev_D := Current_Entity (D);
11486 Set_Current_Entity (D);
11487 Set_Is_Immediately_Visible (D);
11488 Set_Homonym (D, Prev_D);
11490 -- Handle the case where there is an untagged partial view and
11491 -- the full view is tagged: must disallow discriminants with
11492 -- defaults, unless compiling for Ada 2012, which allows a
11493 -- limited tagged type to have defaulted discriminants (see
11494 -- AI05-0214). However, suppress error here if it was already
11495 -- reported on the default expression of the partial view.
11497 if Is_Tagged_Type (T)
11498 and then Present (Expression (Parent (D)))
11499 and then (not Is_Limited_Type (Current_Scope)
11500 or else Ada_Version < Ada_2012)
11501 and then not Error_Posted (Expression (Parent (D)))
11502 then
11503 if Ada_Version >= Ada_2012 then
11504 Error_Msg_N
11505 ("discriminants of nonlimited tagged type cannot have "
11506 & "defaults",
11507 Expression (New_D));
11508 else
11509 Error_Msg_N
11510 ("discriminants of tagged type cannot have defaults",
11511 Expression (New_D));
11512 end if;
11513 end if;
11515 -- Ada 2005 (AI-230): Access discriminant allowed in
11516 -- non-limited record types.
11518 if Ada_Version < Ada_2005 then
11520 -- This restriction gets applied to the full type here. It
11521 -- has already been applied earlier to the partial view.
11523 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11524 end if;
11526 Next_Discriminant (D);
11527 Next (New_D);
11528 end loop;
11529 end;
11531 elsif Present (Discriminant_Specifications (N)) then
11532 Process_Discriminants (N, Prev);
11533 end if;
11534 end Check_Or_Process_Discriminants;
11536 ----------------------
11537 -- Check_Real_Bound --
11538 ----------------------
11540 procedure Check_Real_Bound (Bound : Node_Id) is
11541 begin
11542 if not Is_Real_Type (Etype (Bound)) then
11543 Error_Msg_N
11544 ("bound in real type definition must be of real type", Bound);
11546 elsif not Is_OK_Static_Expression (Bound) then
11547 Flag_Non_Static_Expr
11548 ("non-static expression used for real type bound!", Bound);
11550 else
11551 return;
11552 end if;
11554 Rewrite
11555 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11556 Analyze (Bound);
11557 Resolve (Bound, Standard_Float);
11558 end Check_Real_Bound;
11560 ------------------------------
11561 -- Complete_Private_Subtype --
11562 ------------------------------
11564 procedure Complete_Private_Subtype
11565 (Priv : Entity_Id;
11566 Full : Entity_Id;
11567 Full_Base : Entity_Id;
11568 Related_Nod : Node_Id)
11570 Save_Next_Entity : Entity_Id;
11571 Save_Homonym : Entity_Id;
11573 begin
11574 -- Set semantic attributes for (implicit) private subtype completion.
11575 -- If the full type has no discriminants, then it is a copy of the
11576 -- full view of the base. Otherwise, it is a subtype of the base with
11577 -- a possible discriminant constraint. Save and restore the original
11578 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11579 -- not corrupt the entity chain.
11581 -- Note that the type of the full view is the same entity as the type
11582 -- of the partial view. In this fashion, the subtype has access to the
11583 -- correct view of the parent.
11585 Save_Next_Entity := Next_Entity (Full);
11586 Save_Homonym := Homonym (Priv);
11588 case Ekind (Full_Base) is
11589 when E_Record_Type |
11590 E_Record_Subtype |
11591 Class_Wide_Kind |
11592 Private_Kind |
11593 Task_Kind |
11594 Protected_Kind =>
11595 Copy_Node (Priv, Full);
11597 Set_Has_Discriminants
11598 (Full, Has_Discriminants (Full_Base));
11599 Set_Has_Unknown_Discriminants
11600 (Full, Has_Unknown_Discriminants (Full_Base));
11601 Set_First_Entity (Full, First_Entity (Full_Base));
11602 Set_Last_Entity (Full, Last_Entity (Full_Base));
11604 -- If the underlying base type is constrained, we know that the
11605 -- full view of the subtype is constrained as well (the converse
11606 -- is not necessarily true).
11608 if Is_Constrained (Full_Base) then
11609 Set_Is_Constrained (Full);
11610 end if;
11612 when others =>
11613 Copy_Node (Full_Base, Full);
11615 Set_Chars (Full, Chars (Priv));
11616 Conditional_Delay (Full, Priv);
11617 Set_Sloc (Full, Sloc (Priv));
11618 end case;
11620 Set_Next_Entity (Full, Save_Next_Entity);
11621 Set_Homonym (Full, Save_Homonym);
11622 Set_Associated_Node_For_Itype (Full, Related_Nod);
11624 -- Set common attributes for all subtypes: kind, convention, etc.
11626 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11627 Set_Convention (Full, Convention (Full_Base));
11629 -- The Etype of the full view is inconsistent. Gigi needs to see the
11630 -- structural full view, which is what the current scheme gives: the
11631 -- Etype of the full view is the etype of the full base. However, if the
11632 -- full base is a derived type, the full view then looks like a subtype
11633 -- of the parent, not a subtype of the full base. If instead we write:
11635 -- Set_Etype (Full, Full_Base);
11637 -- then we get inconsistencies in the front-end (confusion between
11638 -- views). Several outstanding bugs are related to this ???
11640 Set_Is_First_Subtype (Full, False);
11641 Set_Scope (Full, Scope (Priv));
11642 Set_Size_Info (Full, Full_Base);
11643 Set_RM_Size (Full, RM_Size (Full_Base));
11644 Set_Is_Itype (Full);
11646 -- A subtype of a private-type-without-discriminants, whose full-view
11647 -- has discriminants with default expressions, is not constrained.
11649 if not Has_Discriminants (Priv) then
11650 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11652 if Has_Discriminants (Full_Base) then
11653 Set_Discriminant_Constraint
11654 (Full, Discriminant_Constraint (Full_Base));
11656 -- The partial view may have been indefinite, the full view
11657 -- might not be.
11659 Set_Has_Unknown_Discriminants
11660 (Full, Has_Unknown_Discriminants (Full_Base));
11661 end if;
11662 end if;
11664 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11665 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11667 -- Freeze the private subtype entity if its parent is delayed, and not
11668 -- already frozen. We skip this processing if the type is an anonymous
11669 -- subtype of a record component, or is the corresponding record of a
11670 -- protected type, since these are processed when the enclosing type
11671 -- is frozen.
11673 if not Is_Type (Scope (Full)) then
11674 Set_Has_Delayed_Freeze (Full,
11675 Has_Delayed_Freeze (Full_Base)
11676 and then (not Is_Frozen (Full_Base)));
11677 end if;
11679 Set_Freeze_Node (Full, Empty);
11680 Set_Is_Frozen (Full, False);
11681 Set_Full_View (Priv, Full);
11683 if Has_Discriminants (Full) then
11684 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11685 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11687 if Has_Unknown_Discriminants (Full) then
11688 Set_Discriminant_Constraint (Full, No_Elist);
11689 end if;
11690 end if;
11692 if Ekind (Full_Base) = E_Record_Type
11693 and then Has_Discriminants (Full_Base)
11694 and then Has_Discriminants (Priv) -- might not, if errors
11695 and then not Has_Unknown_Discriminants (Priv)
11696 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11697 then
11698 Create_Constrained_Components
11699 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11701 -- If the full base is itself derived from private, build a congruent
11702 -- subtype of its underlying type, for use by the back end. For a
11703 -- constrained record component, the declaration cannot be placed on
11704 -- the component list, but it must nevertheless be built an analyzed, to
11705 -- supply enough information for Gigi to compute the size of component.
11707 elsif Ekind (Full_Base) in Private_Kind
11708 and then Is_Derived_Type (Full_Base)
11709 and then Has_Discriminants (Full_Base)
11710 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11711 then
11712 if not Is_Itype (Priv)
11713 and then
11714 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11715 then
11716 Build_Underlying_Full_View
11717 (Parent (Priv), Full, Etype (Full_Base));
11719 elsif Nkind (Related_Nod) = N_Component_Declaration then
11720 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11721 end if;
11723 elsif Is_Record_Type (Full_Base) then
11725 -- Show Full is simply a renaming of Full_Base
11727 Set_Cloned_Subtype (Full, Full_Base);
11728 end if;
11730 -- It is unsafe to share the bounds of a scalar type, because the Itype
11731 -- is elaborated on demand, and if a bound is non-static then different
11732 -- orders of elaboration in different units will lead to different
11733 -- external symbols.
11735 if Is_Scalar_Type (Full_Base) then
11736 Set_Scalar_Range (Full,
11737 Make_Range (Sloc (Related_Nod),
11738 Low_Bound =>
11739 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11740 High_Bound =>
11741 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11743 -- This completion inherits the bounds of the full parent, but if
11744 -- the parent is an unconstrained floating point type, so is the
11745 -- completion.
11747 if Is_Floating_Point_Type (Full_Base) then
11748 Set_Includes_Infinities
11749 (Scalar_Range (Full), Has_Infinities (Full_Base));
11750 end if;
11751 end if;
11753 -- ??? It seems that a lot of fields are missing that should be copied
11754 -- from Full_Base to Full. Here are some that are introduced in a
11755 -- non-disruptive way but a cleanup is necessary.
11757 if Is_Tagged_Type (Full_Base) then
11758 Set_Is_Tagged_Type (Full);
11759 Set_Direct_Primitive_Operations
11760 (Full, Direct_Primitive_Operations (Full_Base));
11761 Set_No_Tagged_Streams_Pragma
11762 (Full, No_Tagged_Streams_Pragma (Full_Base));
11764 -- Inherit class_wide type of full_base in case the partial view was
11765 -- not tagged. Otherwise it has already been created when the private
11766 -- subtype was analyzed.
11768 if No (Class_Wide_Type (Full)) then
11769 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11770 end if;
11772 -- If this is a subtype of a protected or task type, constrain its
11773 -- corresponding record, unless this is a subtype without constraints,
11774 -- i.e. a simple renaming as with an actual subtype in an instance.
11776 elsif Is_Concurrent_Type (Full_Base) then
11777 if Has_Discriminants (Full)
11778 and then Present (Corresponding_Record_Type (Full_Base))
11779 and then
11780 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11781 then
11782 Set_Corresponding_Record_Type (Full,
11783 Constrain_Corresponding_Record
11784 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11786 else
11787 Set_Corresponding_Record_Type (Full,
11788 Corresponding_Record_Type (Full_Base));
11789 end if;
11790 end if;
11792 -- Link rep item chain, and also setting of Has_Predicates from private
11793 -- subtype to full subtype, since we will need these on the full subtype
11794 -- to create the predicate function. Note that the full subtype may
11795 -- already have rep items, inherited from the full view of the base
11796 -- type, so we must be sure not to overwrite these entries.
11798 declare
11799 Append : Boolean;
11800 Item : Node_Id;
11801 Next_Item : Node_Id;
11803 begin
11804 Item := First_Rep_Item (Full);
11806 -- If no existing rep items on full type, we can just link directly
11807 -- to the list of items on the private type, if any exist.. Same if
11808 -- the rep items are only those inherited from the base
11810 if (No (Item)
11811 or else Nkind (Item) /= N_Aspect_Specification
11812 or else Entity (Item) = Full_Base)
11813 and then Present (First_Rep_Item (Priv))
11814 then
11815 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11817 -- Otherwise, search to the end of items currently linked to the full
11818 -- subtype and append the private items to the end. However, if Priv
11819 -- and Full already have the same list of rep items, then the append
11820 -- is not done, as that would create a circularity.
11822 elsif Item /= First_Rep_Item (Priv) then
11823 Append := True;
11824 loop
11825 Next_Item := Next_Rep_Item (Item);
11826 exit when No (Next_Item);
11827 Item := Next_Item;
11829 -- If the private view has aspect specifications, the full view
11830 -- inherits them. Since these aspects may already have been
11831 -- attached to the full view during derivation, do not append
11832 -- them if already present.
11834 if Item = First_Rep_Item (Priv) then
11835 Append := False;
11836 exit;
11837 end if;
11838 end loop;
11840 -- And link the private type items at the end of the chain
11842 if Append then
11843 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11844 end if;
11845 end if;
11846 end;
11848 -- Make sure Has_Predicates is set on full type if it is set on the
11849 -- private type. Note that it may already be set on the full type and
11850 -- if so, we don't want to unset it. Similarly, propagate information
11851 -- about delayed aspects, because the corresponding pragmas must be
11852 -- analyzed when one of the views is frozen. This last step is needed
11853 -- in particular when the full type is a scalar type for which an
11854 -- anonymous base type is constructed.
11856 -- The predicate functions are generated either at the freeze point
11857 -- of the type or at the end of the visible part, and we must avoid
11858 -- generating them twice.
11860 if Has_Predicates (Priv) then
11861 Set_Has_Predicates (Full);
11863 if Present (Predicate_Function (Priv))
11864 and then No (Predicate_Function (Full))
11865 then
11866 Set_Predicate_Function (Full, Predicate_Function (Priv));
11867 end if;
11868 end if;
11870 if Has_Delayed_Aspects (Priv) then
11871 Set_Has_Delayed_Aspects (Full);
11872 end if;
11873 end Complete_Private_Subtype;
11875 ----------------------------
11876 -- Constant_Redeclaration --
11877 ----------------------------
11879 procedure Constant_Redeclaration
11880 (Id : Entity_Id;
11881 N : Node_Id;
11882 T : out Entity_Id)
11884 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11885 Obj_Def : constant Node_Id := Object_Definition (N);
11886 New_T : Entity_Id;
11888 procedure Check_Possible_Deferred_Completion
11889 (Prev_Id : Entity_Id;
11890 Prev_Obj_Def : Node_Id;
11891 Curr_Obj_Def : Node_Id);
11892 -- Determine whether the two object definitions describe the partial
11893 -- and the full view of a constrained deferred constant. Generate
11894 -- a subtype for the full view and verify that it statically matches
11895 -- the subtype of the partial view.
11897 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11898 -- If deferred constant is an access type initialized with an allocator,
11899 -- check whether there is an illegal recursion in the definition,
11900 -- through a default value of some record subcomponent. This is normally
11901 -- detected when generating init procs, but requires this additional
11902 -- mechanism when expansion is disabled.
11904 ----------------------------------------
11905 -- Check_Possible_Deferred_Completion --
11906 ----------------------------------------
11908 procedure Check_Possible_Deferred_Completion
11909 (Prev_Id : Entity_Id;
11910 Prev_Obj_Def : Node_Id;
11911 Curr_Obj_Def : Node_Id)
11913 begin
11914 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11915 and then Present (Constraint (Prev_Obj_Def))
11916 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11917 and then Present (Constraint (Curr_Obj_Def))
11918 then
11919 declare
11920 Loc : constant Source_Ptr := Sloc (N);
11921 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11922 Decl : constant Node_Id :=
11923 Make_Subtype_Declaration (Loc,
11924 Defining_Identifier => Def_Id,
11925 Subtype_Indication =>
11926 Relocate_Node (Curr_Obj_Def));
11928 begin
11929 Insert_Before_And_Analyze (N, Decl);
11930 Set_Etype (Id, Def_Id);
11932 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11933 Error_Msg_Sloc := Sloc (Prev_Id);
11934 Error_Msg_N ("subtype does not statically match deferred "
11935 & "declaration #", N);
11936 end if;
11937 end;
11938 end if;
11939 end Check_Possible_Deferred_Completion;
11941 ---------------------------------
11942 -- Check_Recursive_Declaration --
11943 ---------------------------------
11945 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11946 Comp : Entity_Id;
11948 begin
11949 if Is_Record_Type (Typ) then
11950 Comp := First_Component (Typ);
11951 while Present (Comp) loop
11952 if Comes_From_Source (Comp) then
11953 if Present (Expression (Parent (Comp)))
11954 and then Is_Entity_Name (Expression (Parent (Comp)))
11955 and then Entity (Expression (Parent (Comp))) = Prev
11956 then
11957 Error_Msg_Sloc := Sloc (Parent (Comp));
11958 Error_Msg_NE
11959 ("illegal circularity with declaration for & #",
11960 N, Comp);
11961 return;
11963 elsif Is_Record_Type (Etype (Comp)) then
11964 Check_Recursive_Declaration (Etype (Comp));
11965 end if;
11966 end if;
11968 Next_Component (Comp);
11969 end loop;
11970 end if;
11971 end Check_Recursive_Declaration;
11973 -- Start of processing for Constant_Redeclaration
11975 begin
11976 if Nkind (Parent (Prev)) = N_Object_Declaration then
11977 if Nkind (Object_Definition
11978 (Parent (Prev))) = N_Subtype_Indication
11979 then
11980 -- Find type of new declaration. The constraints of the two
11981 -- views must match statically, but there is no point in
11982 -- creating an itype for the full view.
11984 if Nkind (Obj_Def) = N_Subtype_Indication then
11985 Find_Type (Subtype_Mark (Obj_Def));
11986 New_T := Entity (Subtype_Mark (Obj_Def));
11988 else
11989 Find_Type (Obj_Def);
11990 New_T := Entity (Obj_Def);
11991 end if;
11993 T := Etype (Prev);
11995 else
11996 -- The full view may impose a constraint, even if the partial
11997 -- view does not, so construct the subtype.
11999 New_T := Find_Type_Of_Object (Obj_Def, N);
12000 T := New_T;
12001 end if;
12003 else
12004 -- Current declaration is illegal, diagnosed below in Enter_Name
12006 T := Empty;
12007 New_T := Any_Type;
12008 end if;
12010 -- If previous full declaration or a renaming declaration exists, or if
12011 -- a homograph is present, let Enter_Name handle it, either with an
12012 -- error or with the removal of an overridden implicit subprogram.
12013 -- The previous one is a full declaration if it has an expression
12014 -- (which in the case of an aggregate is indicated by the Init flag).
12016 if Ekind (Prev) /= E_Constant
12017 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12018 or else Present (Expression (Parent (Prev)))
12019 or else Has_Init_Expression (Parent (Prev))
12020 or else Present (Full_View (Prev))
12021 then
12022 Enter_Name (Id);
12024 -- Verify that types of both declarations match, or else that both types
12025 -- are anonymous access types whose designated subtypes statically match
12026 -- (as allowed in Ada 2005 by AI-385).
12028 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12029 and then
12030 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12031 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12032 or else Is_Access_Constant (Etype (New_T)) /=
12033 Is_Access_Constant (Etype (Prev))
12034 or else Can_Never_Be_Null (Etype (New_T)) /=
12035 Can_Never_Be_Null (Etype (Prev))
12036 or else Null_Exclusion_Present (Parent (Prev)) /=
12037 Null_Exclusion_Present (Parent (Id))
12038 or else not Subtypes_Statically_Match
12039 (Designated_Type (Etype (Prev)),
12040 Designated_Type (Etype (New_T))))
12041 then
12042 Error_Msg_Sloc := Sloc (Prev);
12043 Error_Msg_N ("type does not match declaration#", N);
12044 Set_Full_View (Prev, Id);
12045 Set_Etype (Id, Any_Type);
12047 -- A deferred constant whose type is an anonymous array is always
12048 -- illegal (unless imported). A detailed error message might be
12049 -- helpful for Ada beginners.
12051 if Nkind (Object_Definition (Parent (Prev)))
12052 = N_Constrained_Array_Definition
12053 and then Nkind (Object_Definition (N))
12054 = N_Constrained_Array_Definition
12055 then
12056 Error_Msg_N ("\each anonymous array is a distinct type", N);
12057 Error_Msg_N ("a deferred constant must have a named type",
12058 Object_Definition (Parent (Prev)));
12059 end if;
12061 elsif
12062 Null_Exclusion_Present (Parent (Prev))
12063 and then not Null_Exclusion_Present (N)
12064 then
12065 Error_Msg_Sloc := Sloc (Prev);
12066 Error_Msg_N ("null-exclusion does not match declaration#", N);
12067 Set_Full_View (Prev, Id);
12068 Set_Etype (Id, Any_Type);
12070 -- If so, process the full constant declaration
12072 else
12073 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12074 -- the deferred declaration is constrained, then the subtype defined
12075 -- by the subtype_indication in the full declaration shall match it
12076 -- statically.
12078 Check_Possible_Deferred_Completion
12079 (Prev_Id => Prev,
12080 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12081 Curr_Obj_Def => Obj_Def);
12083 Set_Full_View (Prev, Id);
12084 Set_Is_Public (Id, Is_Public (Prev));
12085 Set_Is_Internal (Id);
12086 Append_Entity (Id, Current_Scope);
12088 -- Check ALIASED present if present before (RM 7.4(7))
12090 if Is_Aliased (Prev)
12091 and then not Aliased_Present (N)
12092 then
12093 Error_Msg_Sloc := Sloc (Prev);
12094 Error_Msg_N ("ALIASED required (see declaration #)", N);
12095 end if;
12097 -- Check that placement is in private part and that the incomplete
12098 -- declaration appeared in the visible part.
12100 if Ekind (Current_Scope) = E_Package
12101 and then not In_Private_Part (Current_Scope)
12102 then
12103 Error_Msg_Sloc := Sloc (Prev);
12104 Error_Msg_N
12105 ("full constant for declaration # must be in private part", N);
12107 elsif Ekind (Current_Scope) = E_Package
12108 and then
12109 List_Containing (Parent (Prev)) /=
12110 Visible_Declarations (Package_Specification (Current_Scope))
12111 then
12112 Error_Msg_N
12113 ("deferred constant must be declared in visible part",
12114 Parent (Prev));
12115 end if;
12117 if Is_Access_Type (T)
12118 and then Nkind (Expression (N)) = N_Allocator
12119 then
12120 Check_Recursive_Declaration (Designated_Type (T));
12121 end if;
12123 -- A deferred constant is a visible entity. If type has invariants,
12124 -- verify that the initial value satisfies them.
12126 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12127 Insert_After (N,
12128 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12129 end if;
12130 end if;
12131 end Constant_Redeclaration;
12133 ----------------------
12134 -- Constrain_Access --
12135 ----------------------
12137 procedure Constrain_Access
12138 (Def_Id : in out Entity_Id;
12139 S : Node_Id;
12140 Related_Nod : Node_Id)
12142 T : constant Entity_Id := Entity (Subtype_Mark (S));
12143 Desig_Type : constant Entity_Id := Designated_Type (T);
12144 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12145 Constraint_OK : Boolean := True;
12147 begin
12148 if Is_Array_Type (Desig_Type) then
12149 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12151 elsif (Is_Record_Type (Desig_Type)
12152 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12153 and then not Is_Constrained (Desig_Type)
12154 then
12155 -- ??? The following code is a temporary bypass to ignore a
12156 -- discriminant constraint on access type if it is constraining
12157 -- the current record. Avoid creating the implicit subtype of the
12158 -- record we are currently compiling since right now, we cannot
12159 -- handle these. For now, just return the access type itself.
12161 if Desig_Type = Current_Scope
12162 and then No (Def_Id)
12163 then
12164 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12165 Def_Id := Entity (Subtype_Mark (S));
12167 -- This call added to ensure that the constraint is analyzed
12168 -- (needed for a B test). Note that we still return early from
12169 -- this procedure to avoid recursive processing. ???
12171 Constrain_Discriminated_Type
12172 (Desig_Subtype, S, Related_Nod, For_Access => True);
12173 return;
12174 end if;
12176 -- Enforce rule that the constraint is illegal if there is an
12177 -- unconstrained view of the designated type. This means that the
12178 -- partial view (either a private type declaration or a derivation
12179 -- from a private type) has no discriminants. (Defect Report
12180 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12182 -- Rule updated for Ada 2005: The private type is said to have
12183 -- a constrained partial view, given that objects of the type
12184 -- can be declared. Furthermore, the rule applies to all access
12185 -- types, unlike the rule concerning default discriminants (see
12186 -- RM 3.7.1(7/3))
12188 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12189 and then Has_Private_Declaration (Desig_Type)
12190 and then In_Open_Scopes (Scope (Desig_Type))
12191 and then Has_Discriminants (Desig_Type)
12192 then
12193 declare
12194 Pack : constant Node_Id :=
12195 Unit_Declaration_Node (Scope (Desig_Type));
12196 Decls : List_Id;
12197 Decl : Node_Id;
12199 begin
12200 if Nkind (Pack) = N_Package_Declaration then
12201 Decls := Visible_Declarations (Specification (Pack));
12202 Decl := First (Decls);
12203 while Present (Decl) loop
12204 if (Nkind (Decl) = N_Private_Type_Declaration
12205 and then Chars (Defining_Identifier (Decl)) =
12206 Chars (Desig_Type))
12208 or else
12209 (Nkind (Decl) = N_Full_Type_Declaration
12210 and then
12211 Chars (Defining_Identifier (Decl)) =
12212 Chars (Desig_Type)
12213 and then Is_Derived_Type (Desig_Type)
12214 and then
12215 Has_Private_Declaration (Etype (Desig_Type)))
12216 then
12217 if No (Discriminant_Specifications (Decl)) then
12218 Error_Msg_N
12219 ("cannot constrain access type if designated "
12220 & "type has constrained partial view", S);
12221 end if;
12223 exit;
12224 end if;
12226 Next (Decl);
12227 end loop;
12228 end if;
12229 end;
12230 end if;
12232 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12233 For_Access => True);
12235 elsif Is_Concurrent_Type (Desig_Type)
12236 and then not Is_Constrained (Desig_Type)
12237 then
12238 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12240 else
12241 Error_Msg_N ("invalid constraint on access type", S);
12243 -- We simply ignore an invalid constraint
12245 Desig_Subtype := Desig_Type;
12246 Constraint_OK := False;
12247 end if;
12249 if No (Def_Id) then
12250 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12251 else
12252 Set_Ekind (Def_Id, E_Access_Subtype);
12253 end if;
12255 if Constraint_OK then
12256 Set_Etype (Def_Id, Base_Type (T));
12258 if Is_Private_Type (Desig_Type) then
12259 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12260 end if;
12261 else
12262 Set_Etype (Def_Id, Any_Type);
12263 end if;
12265 Set_Size_Info (Def_Id, T);
12266 Set_Is_Constrained (Def_Id, Constraint_OK);
12267 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12268 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12269 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12271 Conditional_Delay (Def_Id, T);
12273 -- AI-363 : Subtypes of general access types whose designated types have
12274 -- default discriminants are disallowed. In instances, the rule has to
12275 -- be checked against the actual, of which T is the subtype. In a
12276 -- generic body, the rule is checked assuming that the actual type has
12277 -- defaulted discriminants.
12279 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12280 if Ekind (Base_Type (T)) = E_General_Access_Type
12281 and then Has_Defaulted_Discriminants (Desig_Type)
12282 then
12283 if Ada_Version < Ada_2005 then
12284 Error_Msg_N
12285 ("access subtype of general access type would not " &
12286 "be allowed in Ada 2005?y?", S);
12287 else
12288 Error_Msg_N
12289 ("access subtype of general access type not allowed", S);
12290 end if;
12292 Error_Msg_N ("\discriminants have defaults", S);
12294 elsif Is_Access_Type (T)
12295 and then Is_Generic_Type (Desig_Type)
12296 and then Has_Discriminants (Desig_Type)
12297 and then In_Package_Body (Current_Scope)
12298 then
12299 if Ada_Version < Ada_2005 then
12300 Error_Msg_N
12301 ("access subtype would not be allowed in generic body "
12302 & "in Ada 2005?y?", S);
12303 else
12304 Error_Msg_N
12305 ("access subtype not allowed in generic body", S);
12306 end if;
12308 Error_Msg_N
12309 ("\designated type is a discriminated formal", S);
12310 end if;
12311 end if;
12312 end Constrain_Access;
12314 ---------------------
12315 -- Constrain_Array --
12316 ---------------------
12318 procedure Constrain_Array
12319 (Def_Id : in out Entity_Id;
12320 SI : Node_Id;
12321 Related_Nod : Node_Id;
12322 Related_Id : Entity_Id;
12323 Suffix : Character)
12325 C : constant Node_Id := Constraint (SI);
12326 Number_Of_Constraints : Nat := 0;
12327 Index : Node_Id;
12328 S, T : Entity_Id;
12329 Constraint_OK : Boolean := True;
12331 begin
12332 T := Entity (Subtype_Mark (SI));
12334 if Is_Access_Type (T) then
12335 T := Designated_Type (T);
12336 end if;
12338 -- If an index constraint follows a subtype mark in a subtype indication
12339 -- then the type or subtype denoted by the subtype mark must not already
12340 -- impose an index constraint. The subtype mark must denote either an
12341 -- unconstrained array type or an access type whose designated type
12342 -- is such an array type... (RM 3.6.1)
12344 if Is_Constrained (T) then
12345 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12346 Constraint_OK := False;
12348 else
12349 S := First (Constraints (C));
12350 while Present (S) loop
12351 Number_Of_Constraints := Number_Of_Constraints + 1;
12352 Next (S);
12353 end loop;
12355 -- In either case, the index constraint must provide a discrete
12356 -- range for each index of the array type and the type of each
12357 -- discrete range must be the same as that of the corresponding
12358 -- index. (RM 3.6.1)
12360 if Number_Of_Constraints /= Number_Dimensions (T) then
12361 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12362 Constraint_OK := False;
12364 else
12365 S := First (Constraints (C));
12366 Index := First_Index (T);
12367 Analyze (Index);
12369 -- Apply constraints to each index type
12371 for J in 1 .. Number_Of_Constraints loop
12372 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12373 Next (Index);
12374 Next (S);
12375 end loop;
12377 end if;
12378 end if;
12380 if No (Def_Id) then
12381 Def_Id :=
12382 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12383 Set_Parent (Def_Id, Related_Nod);
12385 else
12386 Set_Ekind (Def_Id, E_Array_Subtype);
12387 end if;
12389 Set_Size_Info (Def_Id, (T));
12390 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12391 Set_Etype (Def_Id, Base_Type (T));
12393 if Constraint_OK then
12394 Set_First_Index (Def_Id, First (Constraints (C)));
12395 else
12396 Set_First_Index (Def_Id, First_Index (T));
12397 end if;
12399 Set_Is_Constrained (Def_Id, True);
12400 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12401 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12403 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12404 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12406 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12407 -- We need to initialize the attribute because if Def_Id is previously
12408 -- analyzed through a limited_with clause, it will have the attributes
12409 -- of an incomplete type, one of which is an Elist that overlaps the
12410 -- Packed_Array_Impl_Type field.
12412 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12414 -- Build a freeze node if parent still needs one. Also make sure that
12415 -- the Depends_On_Private status is set because the subtype will need
12416 -- reprocessing at the time the base type does, and also we must set a
12417 -- conditional delay.
12419 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12420 Conditional_Delay (Def_Id, T);
12421 end Constrain_Array;
12423 ------------------------------
12424 -- Constrain_Component_Type --
12425 ------------------------------
12427 function Constrain_Component_Type
12428 (Comp : Entity_Id;
12429 Constrained_Typ : Entity_Id;
12430 Related_Node : Node_Id;
12431 Typ : Entity_Id;
12432 Constraints : Elist_Id) return Entity_Id
12434 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12435 Compon_Type : constant Entity_Id := Etype (Comp);
12437 function Build_Constrained_Array_Type
12438 (Old_Type : Entity_Id) return Entity_Id;
12439 -- If Old_Type is an array type, one of whose indexes is constrained
12440 -- by a discriminant, build an Itype whose constraint replaces the
12441 -- discriminant with its value in the constraint.
12443 function Build_Constrained_Discriminated_Type
12444 (Old_Type : Entity_Id) return Entity_Id;
12445 -- Ditto for record components
12447 function Build_Constrained_Access_Type
12448 (Old_Type : Entity_Id) return Entity_Id;
12449 -- Ditto for access types. Makes use of previous two functions, to
12450 -- constrain designated type.
12452 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12453 -- T is an array or discriminated type, C is a list of constraints
12454 -- that apply to T. This routine builds the constrained subtype.
12456 function Is_Discriminant (Expr : Node_Id) return Boolean;
12457 -- Returns True if Expr is a discriminant
12459 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12460 -- Find the value of discriminant Discrim in Constraint
12462 -----------------------------------
12463 -- Build_Constrained_Access_Type --
12464 -----------------------------------
12466 function Build_Constrained_Access_Type
12467 (Old_Type : Entity_Id) return Entity_Id
12469 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12470 Itype : Entity_Id;
12471 Desig_Subtype : Entity_Id;
12472 Scop : Entity_Id;
12474 begin
12475 -- if the original access type was not embedded in the enclosing
12476 -- type definition, there is no need to produce a new access
12477 -- subtype. In fact every access type with an explicit constraint
12478 -- generates an itype whose scope is the enclosing record.
12480 if not Is_Type (Scope (Old_Type)) then
12481 return Old_Type;
12483 elsif Is_Array_Type (Desig_Type) then
12484 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12486 elsif Has_Discriminants (Desig_Type) then
12488 -- This may be an access type to an enclosing record type for
12489 -- which we are constructing the constrained components. Return
12490 -- the enclosing record subtype. This is not always correct,
12491 -- but avoids infinite recursion. ???
12493 Desig_Subtype := Any_Type;
12495 for J in reverse 0 .. Scope_Stack.Last loop
12496 Scop := Scope_Stack.Table (J).Entity;
12498 if Is_Type (Scop)
12499 and then Base_Type (Scop) = Base_Type (Desig_Type)
12500 then
12501 Desig_Subtype := Scop;
12502 end if;
12504 exit when not Is_Type (Scop);
12505 end loop;
12507 if Desig_Subtype = Any_Type then
12508 Desig_Subtype :=
12509 Build_Constrained_Discriminated_Type (Desig_Type);
12510 end if;
12512 else
12513 return Old_Type;
12514 end if;
12516 if Desig_Subtype /= Desig_Type then
12518 -- The Related_Node better be here or else we won't be able
12519 -- to attach new itypes to a node in the tree.
12521 pragma Assert (Present (Related_Node));
12523 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12525 Set_Etype (Itype, Base_Type (Old_Type));
12526 Set_Size_Info (Itype, (Old_Type));
12527 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12528 Set_Depends_On_Private (Itype, Has_Private_Component
12529 (Old_Type));
12530 Set_Is_Access_Constant (Itype, Is_Access_Constant
12531 (Old_Type));
12533 -- The new itype needs freezing when it depends on a not frozen
12534 -- type and the enclosing subtype needs freezing.
12536 if Has_Delayed_Freeze (Constrained_Typ)
12537 and then not Is_Frozen (Constrained_Typ)
12538 then
12539 Conditional_Delay (Itype, Base_Type (Old_Type));
12540 end if;
12542 return Itype;
12544 else
12545 return Old_Type;
12546 end if;
12547 end Build_Constrained_Access_Type;
12549 ----------------------------------
12550 -- Build_Constrained_Array_Type --
12551 ----------------------------------
12553 function Build_Constrained_Array_Type
12554 (Old_Type : Entity_Id) return Entity_Id
12556 Lo_Expr : Node_Id;
12557 Hi_Expr : Node_Id;
12558 Old_Index : Node_Id;
12559 Range_Node : Node_Id;
12560 Constr_List : List_Id;
12562 Need_To_Create_Itype : Boolean := False;
12564 begin
12565 Old_Index := First_Index (Old_Type);
12566 while Present (Old_Index) loop
12567 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12569 if Is_Discriminant (Lo_Expr)
12570 or else
12571 Is_Discriminant (Hi_Expr)
12572 then
12573 Need_To_Create_Itype := True;
12574 end if;
12576 Next_Index (Old_Index);
12577 end loop;
12579 if Need_To_Create_Itype then
12580 Constr_List := New_List;
12582 Old_Index := First_Index (Old_Type);
12583 while Present (Old_Index) loop
12584 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12586 if Is_Discriminant (Lo_Expr) then
12587 Lo_Expr := Get_Discr_Value (Lo_Expr);
12588 end if;
12590 if Is_Discriminant (Hi_Expr) then
12591 Hi_Expr := Get_Discr_Value (Hi_Expr);
12592 end if;
12594 Range_Node :=
12595 Make_Range
12596 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12598 Append (Range_Node, To => Constr_List);
12600 Next_Index (Old_Index);
12601 end loop;
12603 return Build_Subtype (Old_Type, Constr_List);
12605 else
12606 return Old_Type;
12607 end if;
12608 end Build_Constrained_Array_Type;
12610 ------------------------------------------
12611 -- Build_Constrained_Discriminated_Type --
12612 ------------------------------------------
12614 function Build_Constrained_Discriminated_Type
12615 (Old_Type : Entity_Id) return Entity_Id
12617 Expr : Node_Id;
12618 Constr_List : List_Id;
12619 Old_Constraint : Elmt_Id;
12621 Need_To_Create_Itype : Boolean := False;
12623 begin
12624 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12625 while Present (Old_Constraint) loop
12626 Expr := Node (Old_Constraint);
12628 if Is_Discriminant (Expr) then
12629 Need_To_Create_Itype := True;
12630 end if;
12632 Next_Elmt (Old_Constraint);
12633 end loop;
12635 if Need_To_Create_Itype then
12636 Constr_List := New_List;
12638 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12639 while Present (Old_Constraint) loop
12640 Expr := Node (Old_Constraint);
12642 if Is_Discriminant (Expr) then
12643 Expr := Get_Discr_Value (Expr);
12644 end if;
12646 Append (New_Copy_Tree (Expr), To => Constr_List);
12648 Next_Elmt (Old_Constraint);
12649 end loop;
12651 return Build_Subtype (Old_Type, Constr_List);
12653 else
12654 return Old_Type;
12655 end if;
12656 end Build_Constrained_Discriminated_Type;
12658 -------------------
12659 -- Build_Subtype --
12660 -------------------
12662 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12663 Indic : Node_Id;
12664 Subtyp_Decl : Node_Id;
12665 Def_Id : Entity_Id;
12666 Btyp : Entity_Id := Base_Type (T);
12668 begin
12669 -- The Related_Node better be here or else we won't be able to
12670 -- attach new itypes to a node in the tree.
12672 pragma Assert (Present (Related_Node));
12674 -- If the view of the component's type is incomplete or private
12675 -- with unknown discriminants, then the constraint must be applied
12676 -- to the full type.
12678 if Has_Unknown_Discriminants (Btyp)
12679 and then Present (Underlying_Type (Btyp))
12680 then
12681 Btyp := Underlying_Type (Btyp);
12682 end if;
12684 Indic :=
12685 Make_Subtype_Indication (Loc,
12686 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12687 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12689 Def_Id := Create_Itype (Ekind (T), Related_Node);
12691 Subtyp_Decl :=
12692 Make_Subtype_Declaration (Loc,
12693 Defining_Identifier => Def_Id,
12694 Subtype_Indication => Indic);
12696 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12698 -- Itypes must be analyzed with checks off (see package Itypes)
12700 Analyze (Subtyp_Decl, Suppress => All_Checks);
12702 return Def_Id;
12703 end Build_Subtype;
12705 ---------------------
12706 -- Get_Discr_Value --
12707 ---------------------
12709 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12710 D : Entity_Id;
12711 E : Elmt_Id;
12713 begin
12714 -- The discriminant may be declared for the type, in which case we
12715 -- find it by iterating over the list of discriminants. If the
12716 -- discriminant is inherited from a parent type, it appears as the
12717 -- corresponding discriminant of the current type. This will be the
12718 -- case when constraining an inherited component whose constraint is
12719 -- given by a discriminant of the parent.
12721 D := First_Discriminant (Typ);
12722 E := First_Elmt (Constraints);
12724 while Present (D) loop
12725 if D = Entity (Discrim)
12726 or else D = CR_Discriminant (Entity (Discrim))
12727 or else Corresponding_Discriminant (D) = Entity (Discrim)
12728 then
12729 return Node (E);
12730 end if;
12732 Next_Discriminant (D);
12733 Next_Elmt (E);
12734 end loop;
12736 -- The Corresponding_Discriminant mechanism is incomplete, because
12737 -- the correspondence between new and old discriminants is not one
12738 -- to one: one new discriminant can constrain several old ones. In
12739 -- that case, scan sequentially the stored_constraint, the list of
12740 -- discriminants of the parents, and the constraints.
12742 -- Previous code checked for the present of the Stored_Constraint
12743 -- list for the derived type, but did not use it at all. Should it
12744 -- be present when the component is a discriminated task type?
12746 if Is_Derived_Type (Typ)
12747 and then Scope (Entity (Discrim)) = Etype (Typ)
12748 then
12749 D := First_Discriminant (Etype (Typ));
12750 E := First_Elmt (Constraints);
12751 while Present (D) loop
12752 if D = Entity (Discrim) then
12753 return Node (E);
12754 end if;
12756 Next_Discriminant (D);
12757 Next_Elmt (E);
12758 end loop;
12759 end if;
12761 -- Something is wrong if we did not find the value
12763 raise Program_Error;
12764 end Get_Discr_Value;
12766 ---------------------
12767 -- Is_Discriminant --
12768 ---------------------
12770 function Is_Discriminant (Expr : Node_Id) return Boolean is
12771 Discrim_Scope : Entity_Id;
12773 begin
12774 if Denotes_Discriminant (Expr) then
12775 Discrim_Scope := Scope (Entity (Expr));
12777 -- Either we have a reference to one of Typ's discriminants,
12779 pragma Assert (Discrim_Scope = Typ
12781 -- or to the discriminants of the parent type, in the case
12782 -- of a derivation of a tagged type with variants.
12784 or else Discrim_Scope = Etype (Typ)
12785 or else Full_View (Discrim_Scope) = Etype (Typ)
12787 -- or same as above for the case where the discriminants
12788 -- were declared in Typ's private view.
12790 or else (Is_Private_Type (Discrim_Scope)
12791 and then Chars (Discrim_Scope) = Chars (Typ))
12793 -- or else we are deriving from the full view and the
12794 -- discriminant is declared in the private entity.
12796 or else (Is_Private_Type (Typ)
12797 and then Chars (Discrim_Scope) = Chars (Typ))
12799 -- Or we are constrained the corresponding record of a
12800 -- synchronized type that completes a private declaration.
12802 or else (Is_Concurrent_Record_Type (Typ)
12803 and then
12804 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12806 -- or we have a class-wide type, in which case make sure the
12807 -- discriminant found belongs to the root type.
12809 or else (Is_Class_Wide_Type (Typ)
12810 and then Etype (Typ) = Discrim_Scope));
12812 return True;
12813 end if;
12815 -- In all other cases we have something wrong
12817 return False;
12818 end Is_Discriminant;
12820 -- Start of processing for Constrain_Component_Type
12822 begin
12823 if Nkind (Parent (Comp)) = N_Component_Declaration
12824 and then Comes_From_Source (Parent (Comp))
12825 and then Comes_From_Source
12826 (Subtype_Indication (Component_Definition (Parent (Comp))))
12827 and then
12828 Is_Entity_Name
12829 (Subtype_Indication (Component_Definition (Parent (Comp))))
12830 then
12831 return Compon_Type;
12833 elsif Is_Array_Type (Compon_Type) then
12834 return Build_Constrained_Array_Type (Compon_Type);
12836 elsif Has_Discriminants (Compon_Type) then
12837 return Build_Constrained_Discriminated_Type (Compon_Type);
12839 elsif Is_Access_Type (Compon_Type) then
12840 return Build_Constrained_Access_Type (Compon_Type);
12842 else
12843 return Compon_Type;
12844 end if;
12845 end Constrain_Component_Type;
12847 --------------------------
12848 -- Constrain_Concurrent --
12849 --------------------------
12851 -- For concurrent types, the associated record value type carries the same
12852 -- discriminants, so when we constrain a concurrent type, we must constrain
12853 -- the corresponding record type as well.
12855 procedure Constrain_Concurrent
12856 (Def_Id : in out Entity_Id;
12857 SI : Node_Id;
12858 Related_Nod : Node_Id;
12859 Related_Id : Entity_Id;
12860 Suffix : Character)
12862 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12863 -- case of a private subtype (needed when only doing semantic analysis).
12865 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12866 T_Val : Entity_Id;
12868 begin
12869 if Is_Access_Type (T_Ent) then
12870 T_Ent := Designated_Type (T_Ent);
12871 end if;
12873 T_Val := Corresponding_Record_Type (T_Ent);
12875 if Present (T_Val) then
12877 if No (Def_Id) then
12878 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12880 -- Elaborate itype now, as it may be used in a subsequent
12881 -- synchronized operation in another scope.
12883 if Nkind (Related_Nod) = N_Full_Type_Declaration then
12884 Build_Itype_Reference (Def_Id, Related_Nod);
12885 end if;
12886 end if;
12888 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12890 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12891 Set_Corresponding_Record_Type (Def_Id,
12892 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12894 else
12895 -- If there is no associated record, expansion is disabled and this
12896 -- is a generic context. Create a subtype in any case, so that
12897 -- semantic analysis can proceed.
12899 if No (Def_Id) then
12900 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12901 end if;
12903 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12904 end if;
12905 end Constrain_Concurrent;
12907 ------------------------------------
12908 -- Constrain_Corresponding_Record --
12909 ------------------------------------
12911 function Constrain_Corresponding_Record
12912 (Prot_Subt : Entity_Id;
12913 Corr_Rec : Entity_Id;
12914 Related_Nod : Node_Id) return Entity_Id
12916 T_Sub : constant Entity_Id :=
12917 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12919 begin
12920 Set_Etype (T_Sub, Corr_Rec);
12921 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12922 Set_Is_Constrained (T_Sub, True);
12923 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12924 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12926 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12927 Set_Discriminant_Constraint
12928 (T_Sub, Discriminant_Constraint (Prot_Subt));
12929 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12930 Create_Constrained_Components
12931 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12932 end if;
12934 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12936 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12937 Conditional_Delay (T_Sub, Corr_Rec);
12939 else
12940 -- This is a component subtype: it will be frozen in the context of
12941 -- the enclosing record's init_proc, so that discriminant references
12942 -- are resolved to discriminals. (Note: we used to skip freezing
12943 -- altogether in that case, which caused errors downstream for
12944 -- components of a bit packed array type).
12946 Set_Has_Delayed_Freeze (T_Sub);
12947 end if;
12949 return T_Sub;
12950 end Constrain_Corresponding_Record;
12952 -----------------------
12953 -- Constrain_Decimal --
12954 -----------------------
12956 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12957 T : constant Entity_Id := Entity (Subtype_Mark (S));
12958 C : constant Node_Id := Constraint (S);
12959 Loc : constant Source_Ptr := Sloc (C);
12960 Range_Expr : Node_Id;
12961 Digits_Expr : Node_Id;
12962 Digits_Val : Uint;
12963 Bound_Val : Ureal;
12965 begin
12966 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12968 if Nkind (C) = N_Range_Constraint then
12969 Range_Expr := Range_Expression (C);
12970 Digits_Val := Digits_Value (T);
12972 else
12973 pragma Assert (Nkind (C) = N_Digits_Constraint);
12975 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12977 Digits_Expr := Digits_Expression (C);
12978 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12980 Check_Digits_Expression (Digits_Expr);
12981 Digits_Val := Expr_Value (Digits_Expr);
12983 if Digits_Val > Digits_Value (T) then
12984 Error_Msg_N
12985 ("digits expression is incompatible with subtype", C);
12986 Digits_Val := Digits_Value (T);
12987 end if;
12989 if Present (Range_Constraint (C)) then
12990 Range_Expr := Range_Expression (Range_Constraint (C));
12991 else
12992 Range_Expr := Empty;
12993 end if;
12994 end if;
12996 Set_Etype (Def_Id, Base_Type (T));
12997 Set_Size_Info (Def_Id, (T));
12998 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12999 Set_Delta_Value (Def_Id, Delta_Value (T));
13000 Set_Scale_Value (Def_Id, Scale_Value (T));
13001 Set_Small_Value (Def_Id, Small_Value (T));
13002 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13003 Set_Digits_Value (Def_Id, Digits_Val);
13005 -- Manufacture range from given digits value if no range present
13007 if No (Range_Expr) then
13008 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13009 Range_Expr :=
13010 Make_Range (Loc,
13011 Low_Bound =>
13012 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13013 High_Bound =>
13014 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13015 end if;
13017 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13018 Set_Discrete_RM_Size (Def_Id);
13020 -- Unconditionally delay the freeze, since we cannot set size
13021 -- information in all cases correctly until the freeze point.
13023 Set_Has_Delayed_Freeze (Def_Id);
13024 end Constrain_Decimal;
13026 ----------------------------------
13027 -- Constrain_Discriminated_Type --
13028 ----------------------------------
13030 procedure Constrain_Discriminated_Type
13031 (Def_Id : Entity_Id;
13032 S : Node_Id;
13033 Related_Nod : Node_Id;
13034 For_Access : Boolean := False)
13036 E : constant Entity_Id := Entity (Subtype_Mark (S));
13037 T : Entity_Id;
13038 C : Node_Id;
13039 Elist : Elist_Id := New_Elmt_List;
13041 procedure Fixup_Bad_Constraint;
13042 -- This is called after finding a bad constraint, and after having
13043 -- posted an appropriate error message. The mission is to leave the
13044 -- entity T in as reasonable state as possible.
13046 --------------------------
13047 -- Fixup_Bad_Constraint --
13048 --------------------------
13050 procedure Fixup_Bad_Constraint is
13051 begin
13052 -- Set a reasonable Ekind for the entity. For an incomplete type,
13053 -- we can't do much, but for other types, we can set the proper
13054 -- corresponding subtype kind.
13056 if Ekind (T) = E_Incomplete_Type then
13057 Set_Ekind (Def_Id, Ekind (T));
13058 else
13059 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13060 end if;
13062 -- Set Etype to the known type, to reduce chances of cascaded errors
13064 Set_Etype (Def_Id, E);
13065 Set_Error_Posted (Def_Id);
13066 end Fixup_Bad_Constraint;
13068 -- Start of processing for Constrain_Discriminated_Type
13070 begin
13071 C := Constraint (S);
13073 -- A discriminant constraint is only allowed in a subtype indication,
13074 -- after a subtype mark. This subtype mark must denote either a type
13075 -- with discriminants, or an access type whose designated type is a
13076 -- type with discriminants. A discriminant constraint specifies the
13077 -- values of these discriminants (RM 3.7.2(5)).
13079 T := Base_Type (Entity (Subtype_Mark (S)));
13081 if Is_Access_Type (T) then
13082 T := Designated_Type (T);
13083 end if;
13085 -- In an instance it may be necessary to retrieve the full view of a
13086 -- type with unknown discriminants. In other contexts the constraint
13087 -- is illegal.
13089 if In_Instance
13090 and then Is_Private_Type (T)
13091 and then Has_Unknown_Discriminants (T)
13092 and then Present (Full_View (T))
13093 then
13094 T := Full_View (T);
13095 end if;
13097 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
13098 -- Avoid generating an error for access-to-incomplete subtypes.
13100 if Ada_Version >= Ada_2005
13101 and then Ekind (T) = E_Incomplete_Type
13102 and then Nkind (Parent (S)) = N_Subtype_Declaration
13103 and then not Is_Itype (Def_Id)
13104 then
13105 -- A little sanity check, emit an error message if the type
13106 -- has discriminants to begin with. Type T may be a regular
13107 -- incomplete type or imported via a limited with clause.
13109 if Has_Discriminants (T)
13110 or else (From_Limited_With (T)
13111 and then Present (Non_Limited_View (T))
13112 and then Nkind (Parent (Non_Limited_View (T))) =
13113 N_Full_Type_Declaration
13114 and then Present (Discriminant_Specifications
13115 (Parent (Non_Limited_View (T)))))
13116 then
13117 Error_Msg_N
13118 ("(Ada 2005) incomplete subtype may not be constrained", C);
13119 else
13120 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13121 end if;
13123 Fixup_Bad_Constraint;
13124 return;
13126 -- Check that the type has visible discriminants. The type may be
13127 -- a private type with unknown discriminants whose full view has
13128 -- discriminants which are invisible.
13130 elsif not Has_Discriminants (T)
13131 or else
13132 (Has_Unknown_Discriminants (T)
13133 and then Is_Private_Type (T))
13134 then
13135 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13136 Fixup_Bad_Constraint;
13137 return;
13139 elsif Is_Constrained (E)
13140 or else (Ekind (E) = E_Class_Wide_Subtype
13141 and then Present (Discriminant_Constraint (E)))
13142 then
13143 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13144 Fixup_Bad_Constraint;
13145 return;
13146 end if;
13148 -- T may be an unconstrained subtype (e.g. a generic actual).
13149 -- Constraint applies to the base type.
13151 T := Base_Type (T);
13153 Elist := Build_Discriminant_Constraints (T, S);
13155 -- If the list returned was empty we had an error in building the
13156 -- discriminant constraint. We have also already signalled an error
13157 -- in the incomplete type case
13159 if Is_Empty_Elmt_List (Elist) then
13160 Fixup_Bad_Constraint;
13161 return;
13162 end if;
13164 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13165 end Constrain_Discriminated_Type;
13167 ---------------------------
13168 -- Constrain_Enumeration --
13169 ---------------------------
13171 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13172 T : constant Entity_Id := Entity (Subtype_Mark (S));
13173 C : constant Node_Id := Constraint (S);
13175 begin
13176 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13178 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13180 Set_Etype (Def_Id, Base_Type (T));
13181 Set_Size_Info (Def_Id, (T));
13182 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13183 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13185 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13187 Set_Discrete_RM_Size (Def_Id);
13188 end Constrain_Enumeration;
13190 ----------------------
13191 -- Constrain_Float --
13192 ----------------------
13194 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13195 T : constant Entity_Id := Entity (Subtype_Mark (S));
13196 C : Node_Id;
13197 D : Node_Id;
13198 Rais : Node_Id;
13200 begin
13201 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13203 Set_Etype (Def_Id, Base_Type (T));
13204 Set_Size_Info (Def_Id, (T));
13205 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13207 -- Process the constraint
13209 C := Constraint (S);
13211 -- Digits constraint present
13213 if Nkind (C) = N_Digits_Constraint then
13215 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13216 Check_Restriction (No_Obsolescent_Features, C);
13218 if Warn_On_Obsolescent_Feature then
13219 Error_Msg_N
13220 ("subtype digits constraint is an " &
13221 "obsolescent feature (RM J.3(8))?j?", C);
13222 end if;
13224 D := Digits_Expression (C);
13225 Analyze_And_Resolve (D, Any_Integer);
13226 Check_Digits_Expression (D);
13227 Set_Digits_Value (Def_Id, Expr_Value (D));
13229 -- Check that digits value is in range. Obviously we can do this
13230 -- at compile time, but it is strictly a runtime check, and of
13231 -- course there is an ACVC test that checks this.
13233 if Digits_Value (Def_Id) > Digits_Value (T) then
13234 Error_Msg_Uint_1 := Digits_Value (T);
13235 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13236 Rais :=
13237 Make_Raise_Constraint_Error (Sloc (D),
13238 Reason => CE_Range_Check_Failed);
13239 Insert_Action (Declaration_Node (Def_Id), Rais);
13240 end if;
13242 C := Range_Constraint (C);
13244 -- No digits constraint present
13246 else
13247 Set_Digits_Value (Def_Id, Digits_Value (T));
13248 end if;
13250 -- Range constraint present
13252 if Nkind (C) = N_Range_Constraint then
13253 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13255 -- No range constraint present
13257 else
13258 pragma Assert (No (C));
13259 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13260 end if;
13262 Set_Is_Constrained (Def_Id);
13263 end Constrain_Float;
13265 ---------------------
13266 -- Constrain_Index --
13267 ---------------------
13269 procedure Constrain_Index
13270 (Index : Node_Id;
13271 S : Node_Id;
13272 Related_Nod : Node_Id;
13273 Related_Id : Entity_Id;
13274 Suffix : Character;
13275 Suffix_Index : Nat)
13277 Def_Id : Entity_Id;
13278 R : Node_Id := Empty;
13279 T : constant Entity_Id := Etype (Index);
13281 begin
13282 Def_Id :=
13283 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13284 Set_Etype (Def_Id, Base_Type (T));
13286 if Nkind (S) = N_Range
13287 or else
13288 (Nkind (S) = N_Attribute_Reference
13289 and then Attribute_Name (S) = Name_Range)
13290 then
13291 -- A Range attribute will be transformed into N_Range by Resolve
13293 Analyze (S);
13294 Set_Etype (S, T);
13295 R := S;
13297 Process_Range_Expr_In_Decl (R, T);
13299 if not Error_Posted (S)
13300 and then
13301 (Nkind (S) /= N_Range
13302 or else not Covers (T, (Etype (Low_Bound (S))))
13303 or else not Covers (T, (Etype (High_Bound (S)))))
13304 then
13305 if Base_Type (T) /= Any_Type
13306 and then Etype (Low_Bound (S)) /= Any_Type
13307 and then Etype (High_Bound (S)) /= Any_Type
13308 then
13309 Error_Msg_N ("range expected", S);
13310 end if;
13311 end if;
13313 elsif Nkind (S) = N_Subtype_Indication then
13315 -- The parser has verified that this is a discrete indication
13317 Resolve_Discrete_Subtype_Indication (S, T);
13318 Bad_Predicated_Subtype_Use
13319 ("subtype& has predicate, not allowed in index constraint",
13320 S, Entity (Subtype_Mark (S)));
13322 R := Range_Expression (Constraint (S));
13324 -- Capture values of bounds and generate temporaries for them if
13325 -- needed, since checks may cause duplication of the expressions
13326 -- which must not be reevaluated.
13328 -- The forced evaluation removes side effects from expressions, which
13329 -- should occur also in GNATprove mode. Otherwise, we end up with
13330 -- unexpected insertions of actions at places where this is not
13331 -- supposed to occur, e.g. on default parameters of a call.
13333 if Expander_Active or GNATprove_Mode then
13334 Force_Evaluation
13335 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13336 Force_Evaluation
13337 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13338 end if;
13340 elsif Nkind (S) = N_Discriminant_Association then
13342 -- Syntactically valid in subtype indication
13344 Error_Msg_N ("invalid index constraint", S);
13345 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13346 return;
13348 -- Subtype_Mark case, no anonymous subtypes to construct
13350 else
13351 Analyze (S);
13353 if Is_Entity_Name (S) then
13354 if not Is_Type (Entity (S)) then
13355 Error_Msg_N ("expect subtype mark for index constraint", S);
13357 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13358 Wrong_Type (S, Base_Type (T));
13360 -- Check error of subtype with predicate in index constraint
13362 else
13363 Bad_Predicated_Subtype_Use
13364 ("subtype& has predicate, not allowed in index constraint",
13365 S, Entity (S));
13366 end if;
13368 return;
13370 else
13371 Error_Msg_N ("invalid index constraint", S);
13372 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13373 return;
13374 end if;
13375 end if;
13377 -- Complete construction of the Itype
13379 if Is_Modular_Integer_Type (T) then
13380 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13382 elsif Is_Integer_Type (T) then
13383 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13385 else
13386 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13387 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13388 Set_First_Literal (Def_Id, First_Literal (T));
13389 end if;
13391 Set_Size_Info (Def_Id, (T));
13392 Set_RM_Size (Def_Id, RM_Size (T));
13393 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13395 Set_Scalar_Range (Def_Id, R);
13397 Set_Etype (S, Def_Id);
13398 Set_Discrete_RM_Size (Def_Id);
13399 end Constrain_Index;
13401 -----------------------
13402 -- Constrain_Integer --
13403 -----------------------
13405 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13406 T : constant Entity_Id := Entity (Subtype_Mark (S));
13407 C : constant Node_Id := Constraint (S);
13409 begin
13410 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13412 if Is_Modular_Integer_Type (T) then
13413 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13414 else
13415 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13416 end if;
13418 Set_Etype (Def_Id, Base_Type (T));
13419 Set_Size_Info (Def_Id, (T));
13420 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13421 Set_Discrete_RM_Size (Def_Id);
13422 end Constrain_Integer;
13424 ------------------------------
13425 -- Constrain_Ordinary_Fixed --
13426 ------------------------------
13428 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13429 T : constant Entity_Id := Entity (Subtype_Mark (S));
13430 C : Node_Id;
13431 D : Node_Id;
13432 Rais : Node_Id;
13434 begin
13435 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13436 Set_Etype (Def_Id, Base_Type (T));
13437 Set_Size_Info (Def_Id, (T));
13438 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13439 Set_Small_Value (Def_Id, Small_Value (T));
13441 -- Process the constraint
13443 C := Constraint (S);
13445 -- Delta constraint present
13447 if Nkind (C) = N_Delta_Constraint then
13449 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13450 Check_Restriction (No_Obsolescent_Features, C);
13452 if Warn_On_Obsolescent_Feature then
13453 Error_Msg_S
13454 ("subtype delta constraint is an " &
13455 "obsolescent feature (RM J.3(7))?j?");
13456 end if;
13458 D := Delta_Expression (C);
13459 Analyze_And_Resolve (D, Any_Real);
13460 Check_Delta_Expression (D);
13461 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13463 -- Check that delta value is in range. Obviously we can do this
13464 -- at compile time, but it is strictly a runtime check, and of
13465 -- course there is an ACVC test that checks this.
13467 if Delta_Value (Def_Id) < Delta_Value (T) then
13468 Error_Msg_N ("??delta value is too small", D);
13469 Rais :=
13470 Make_Raise_Constraint_Error (Sloc (D),
13471 Reason => CE_Range_Check_Failed);
13472 Insert_Action (Declaration_Node (Def_Id), Rais);
13473 end if;
13475 C := Range_Constraint (C);
13477 -- No delta constraint present
13479 else
13480 Set_Delta_Value (Def_Id, Delta_Value (T));
13481 end if;
13483 -- Range constraint present
13485 if Nkind (C) = N_Range_Constraint then
13486 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13488 -- No range constraint present
13490 else
13491 pragma Assert (No (C));
13492 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13493 end if;
13495 Set_Discrete_RM_Size (Def_Id);
13497 -- Unconditionally delay the freeze, since we cannot set size
13498 -- information in all cases correctly until the freeze point.
13500 Set_Has_Delayed_Freeze (Def_Id);
13501 end Constrain_Ordinary_Fixed;
13503 -----------------------
13504 -- Contain_Interface --
13505 -----------------------
13507 function Contain_Interface
13508 (Iface : Entity_Id;
13509 Ifaces : Elist_Id) return Boolean
13511 Iface_Elmt : Elmt_Id;
13513 begin
13514 if Present (Ifaces) then
13515 Iface_Elmt := First_Elmt (Ifaces);
13516 while Present (Iface_Elmt) loop
13517 if Node (Iface_Elmt) = Iface then
13518 return True;
13519 end if;
13521 Next_Elmt (Iface_Elmt);
13522 end loop;
13523 end if;
13525 return False;
13526 end Contain_Interface;
13528 ---------------------------
13529 -- Convert_Scalar_Bounds --
13530 ---------------------------
13532 procedure Convert_Scalar_Bounds
13533 (N : Node_Id;
13534 Parent_Type : Entity_Id;
13535 Derived_Type : Entity_Id;
13536 Loc : Source_Ptr)
13538 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13540 Lo : Node_Id;
13541 Hi : Node_Id;
13542 Rng : Node_Id;
13544 begin
13545 -- Defend against previous errors
13547 if No (Scalar_Range (Derived_Type)) then
13548 Check_Error_Detected;
13549 return;
13550 end if;
13552 Lo := Build_Scalar_Bound
13553 (Type_Low_Bound (Derived_Type),
13554 Parent_Type, Implicit_Base);
13556 Hi := Build_Scalar_Bound
13557 (Type_High_Bound (Derived_Type),
13558 Parent_Type, Implicit_Base);
13560 Rng :=
13561 Make_Range (Loc,
13562 Low_Bound => Lo,
13563 High_Bound => Hi);
13565 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13567 Set_Parent (Rng, N);
13568 Set_Scalar_Range (Derived_Type, Rng);
13570 -- Analyze the bounds
13572 Analyze_And_Resolve (Lo, Implicit_Base);
13573 Analyze_And_Resolve (Hi, Implicit_Base);
13575 -- Analyze the range itself, except that we do not analyze it if
13576 -- the bounds are real literals, and we have a fixed-point type.
13577 -- The reason for this is that we delay setting the bounds in this
13578 -- case till we know the final Small and Size values (see circuit
13579 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13581 if Is_Fixed_Point_Type (Parent_Type)
13582 and then Nkind (Lo) = N_Real_Literal
13583 and then Nkind (Hi) = N_Real_Literal
13584 then
13585 return;
13587 -- Here we do the analysis of the range
13589 -- Note: we do this manually, since if we do a normal Analyze and
13590 -- Resolve call, there are problems with the conversions used for
13591 -- the derived type range.
13593 else
13594 Set_Etype (Rng, Implicit_Base);
13595 Set_Analyzed (Rng, True);
13596 end if;
13597 end Convert_Scalar_Bounds;
13599 -------------------
13600 -- Copy_And_Swap --
13601 -------------------
13603 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13604 begin
13605 -- Initialize new full declaration entity by copying the pertinent
13606 -- fields of the corresponding private declaration entity.
13608 -- We temporarily set Ekind to a value appropriate for a type to
13609 -- avoid assert failures in Einfo from checking for setting type
13610 -- attributes on something that is not a type. Ekind (Priv) is an
13611 -- appropriate choice, since it allowed the attributes to be set
13612 -- in the first place. This Ekind value will be modified later.
13614 Set_Ekind (Full, Ekind (Priv));
13616 -- Also set Etype temporarily to Any_Type, again, in the absence
13617 -- of errors, it will be properly reset, and if there are errors,
13618 -- then we want a value of Any_Type to remain.
13620 Set_Etype (Full, Any_Type);
13622 -- Now start copying attributes
13624 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13626 if Has_Discriminants (Full) then
13627 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13628 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13629 end if;
13631 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13632 Set_Homonym (Full, Homonym (Priv));
13633 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13634 Set_Is_Public (Full, Is_Public (Priv));
13635 Set_Is_Pure (Full, Is_Pure (Priv));
13636 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13637 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13638 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13639 Set_Has_Pragma_Unreferenced_Objects
13640 (Full, Has_Pragma_Unreferenced_Objects
13641 (Priv));
13643 Conditional_Delay (Full, Priv);
13645 if Is_Tagged_Type (Full) then
13646 Set_Direct_Primitive_Operations
13647 (Full, Direct_Primitive_Operations (Priv));
13648 Set_No_Tagged_Streams_Pragma
13649 (Full, No_Tagged_Streams_Pragma (Priv));
13651 if Is_Base_Type (Priv) then
13652 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13653 end if;
13654 end if;
13656 Set_Is_Volatile (Full, Is_Volatile (Priv));
13657 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13658 Set_Scope (Full, Scope (Priv));
13659 Set_Next_Entity (Full, Next_Entity (Priv));
13660 Set_First_Entity (Full, First_Entity (Priv));
13661 Set_Last_Entity (Full, Last_Entity (Priv));
13663 -- If access types have been recorded for later handling, keep them in
13664 -- the full view so that they get handled when the full view freeze
13665 -- node is expanded.
13667 if Present (Freeze_Node (Priv))
13668 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13669 then
13670 Ensure_Freeze_Node (Full);
13671 Set_Access_Types_To_Process
13672 (Freeze_Node (Full),
13673 Access_Types_To_Process (Freeze_Node (Priv)));
13674 end if;
13676 -- Swap the two entities. Now Private is the full type entity and Full
13677 -- is the private one. They will be swapped back at the end of the
13678 -- private part. This swapping ensures that the entity that is visible
13679 -- in the private part is the full declaration.
13681 Exchange_Entities (Priv, Full);
13682 Append_Entity (Full, Scope (Full));
13683 end Copy_And_Swap;
13685 -------------------------------------
13686 -- Copy_Array_Base_Type_Attributes --
13687 -------------------------------------
13689 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13690 begin
13691 Set_Component_Alignment (T1, Component_Alignment (T2));
13692 Set_Component_Type (T1, Component_Type (T2));
13693 Set_Component_Size (T1, Component_Size (T2));
13694 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13695 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13696 Set_Has_Protected (T1, Has_Protected (T2));
13697 Set_Has_Task (T1, Has_Task (T2));
13698 Set_Is_Packed (T1, Is_Packed (T2));
13699 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13700 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13701 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13702 end Copy_Array_Base_Type_Attributes;
13704 -----------------------------------
13705 -- Copy_Array_Subtype_Attributes --
13706 -----------------------------------
13708 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13709 begin
13710 Set_Size_Info (T1, T2);
13712 Set_First_Index (T1, First_Index (T2));
13713 Set_Is_Aliased (T1, Is_Aliased (T2));
13714 Set_Is_Volatile (T1, Is_Volatile (T2));
13715 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13716 Set_Is_Constrained (T1, Is_Constrained (T2));
13717 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13718 Inherit_Rep_Item_Chain (T1, T2);
13719 Set_Convention (T1, Convention (T2));
13720 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13721 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13722 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13723 end Copy_Array_Subtype_Attributes;
13725 -----------------------------------
13726 -- Create_Constrained_Components --
13727 -----------------------------------
13729 procedure Create_Constrained_Components
13730 (Subt : Entity_Id;
13731 Decl_Node : Node_Id;
13732 Typ : Entity_Id;
13733 Constraints : Elist_Id)
13735 Loc : constant Source_Ptr := Sloc (Subt);
13736 Comp_List : constant Elist_Id := New_Elmt_List;
13737 Parent_Type : constant Entity_Id := Etype (Typ);
13738 Assoc_List : constant List_Id := New_List;
13739 Discr_Val : Elmt_Id;
13740 Errors : Boolean;
13741 New_C : Entity_Id;
13742 Old_C : Entity_Id;
13743 Is_Static : Boolean := True;
13745 procedure Collect_Fixed_Components (Typ : Entity_Id);
13746 -- Collect parent type components that do not appear in a variant part
13748 procedure Create_All_Components;
13749 -- Iterate over Comp_List to create the components of the subtype
13751 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13752 -- Creates a new component from Old_Compon, copying all the fields from
13753 -- it, including its Etype, inserts the new component in the Subt entity
13754 -- chain and returns the new component.
13756 function Is_Variant_Record (T : Entity_Id) return Boolean;
13757 -- If true, and discriminants are static, collect only components from
13758 -- variants selected by discriminant values.
13760 ------------------------------
13761 -- Collect_Fixed_Components --
13762 ------------------------------
13764 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13765 begin
13766 -- Build association list for discriminants, and find components of the
13767 -- variant part selected by the values of the discriminants.
13769 Old_C := First_Discriminant (Typ);
13770 Discr_Val := First_Elmt (Constraints);
13771 while Present (Old_C) loop
13772 Append_To (Assoc_List,
13773 Make_Component_Association (Loc,
13774 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13775 Expression => New_Copy (Node (Discr_Val))));
13777 Next_Elmt (Discr_Val);
13778 Next_Discriminant (Old_C);
13779 end loop;
13781 -- The tag and the possible parent component are unconditionally in
13782 -- the subtype.
13784 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13785 Old_C := First_Component (Typ);
13786 while Present (Old_C) loop
13787 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13788 Append_Elmt (Old_C, Comp_List);
13789 end if;
13791 Next_Component (Old_C);
13792 end loop;
13793 end if;
13794 end Collect_Fixed_Components;
13796 ---------------------------
13797 -- Create_All_Components --
13798 ---------------------------
13800 procedure Create_All_Components is
13801 Comp : Elmt_Id;
13803 begin
13804 Comp := First_Elmt (Comp_List);
13805 while Present (Comp) loop
13806 Old_C := Node (Comp);
13807 New_C := Create_Component (Old_C);
13809 Set_Etype
13810 (New_C,
13811 Constrain_Component_Type
13812 (Old_C, Subt, Decl_Node, Typ, Constraints));
13813 Set_Is_Public (New_C, Is_Public (Subt));
13815 Next_Elmt (Comp);
13816 end loop;
13817 end Create_All_Components;
13819 ----------------------
13820 -- Create_Component --
13821 ----------------------
13823 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13824 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13826 begin
13827 if Ekind (Old_Compon) = E_Discriminant
13828 and then Is_Completely_Hidden (Old_Compon)
13829 then
13830 -- This is a shadow discriminant created for a discriminant of
13831 -- the parent type, which needs to be present in the subtype.
13832 -- Give the shadow discriminant an internal name that cannot
13833 -- conflict with that of visible components.
13835 Set_Chars (New_Compon, New_Internal_Name ('C'));
13836 end if;
13838 -- Set the parent so we have a proper link for freezing etc. This is
13839 -- not a real parent pointer, since of course our parent does not own
13840 -- up to us and reference us, we are an illegitimate child of the
13841 -- original parent.
13843 Set_Parent (New_Compon, Parent (Old_Compon));
13845 -- If the old component's Esize was already determined and is a
13846 -- static value, then the new component simply inherits it. Otherwise
13847 -- the old component's size may require run-time determination, but
13848 -- the new component's size still might be statically determinable
13849 -- (if, for example it has a static constraint). In that case we want
13850 -- Layout_Type to recompute the component's size, so we reset its
13851 -- size and positional fields.
13853 if Frontend_Layout_On_Target
13854 and then not Known_Static_Esize (Old_Compon)
13855 then
13856 Set_Esize (New_Compon, Uint_0);
13857 Init_Normalized_First_Bit (New_Compon);
13858 Init_Normalized_Position (New_Compon);
13859 Init_Normalized_Position_Max (New_Compon);
13860 end if;
13862 -- We do not want this node marked as Comes_From_Source, since
13863 -- otherwise it would get first class status and a separate cross-
13864 -- reference line would be generated. Illegitimate children do not
13865 -- rate such recognition.
13867 Set_Comes_From_Source (New_Compon, False);
13869 -- But it is a real entity, and a birth certificate must be properly
13870 -- registered by entering it into the entity list.
13872 Enter_Name (New_Compon);
13874 return New_Compon;
13875 end Create_Component;
13877 -----------------------
13878 -- Is_Variant_Record --
13879 -----------------------
13881 function Is_Variant_Record (T : Entity_Id) return Boolean is
13882 begin
13883 return Nkind (Parent (T)) = N_Full_Type_Declaration
13884 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13885 and then Present (Component_List (Type_Definition (Parent (T))))
13886 and then
13887 Present
13888 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13889 end Is_Variant_Record;
13891 -- Start of processing for Create_Constrained_Components
13893 begin
13894 pragma Assert (Subt /= Base_Type (Subt));
13895 pragma Assert (Typ = Base_Type (Typ));
13897 Set_First_Entity (Subt, Empty);
13898 Set_Last_Entity (Subt, Empty);
13900 -- Check whether constraint is fully static, in which case we can
13901 -- optimize the list of components.
13903 Discr_Val := First_Elmt (Constraints);
13904 while Present (Discr_Val) loop
13905 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13906 Is_Static := False;
13907 exit;
13908 end if;
13910 Next_Elmt (Discr_Val);
13911 end loop;
13913 Set_Has_Static_Discriminants (Subt, Is_Static);
13915 Push_Scope (Subt);
13917 -- Inherit the discriminants of the parent type
13919 Add_Discriminants : declare
13920 Num_Disc : Nat;
13921 Num_Gird : Nat;
13923 begin
13924 Num_Disc := 0;
13925 Old_C := First_Discriminant (Typ);
13927 while Present (Old_C) loop
13928 Num_Disc := Num_Disc + 1;
13929 New_C := Create_Component (Old_C);
13930 Set_Is_Public (New_C, Is_Public (Subt));
13931 Next_Discriminant (Old_C);
13932 end loop;
13934 -- For an untagged derived subtype, the number of discriminants may
13935 -- be smaller than the number of inherited discriminants, because
13936 -- several of them may be renamed by a single new discriminant or
13937 -- constrained. In this case, add the hidden discriminants back into
13938 -- the subtype, because they need to be present if the optimizer of
13939 -- the GCC 4.x back-end decides to break apart assignments between
13940 -- objects using the parent view into member-wise assignments.
13942 Num_Gird := 0;
13944 if Is_Derived_Type (Typ)
13945 and then not Is_Tagged_Type (Typ)
13946 then
13947 Old_C := First_Stored_Discriminant (Typ);
13949 while Present (Old_C) loop
13950 Num_Gird := Num_Gird + 1;
13951 Next_Stored_Discriminant (Old_C);
13952 end loop;
13953 end if;
13955 if Num_Gird > Num_Disc then
13957 -- Find out multiple uses of new discriminants, and add hidden
13958 -- components for the extra renamed discriminants. We recognize
13959 -- multiple uses through the Corresponding_Discriminant of a
13960 -- new discriminant: if it constrains several old discriminants,
13961 -- this field points to the last one in the parent type. The
13962 -- stored discriminants of the derived type have the same name
13963 -- as those of the parent.
13965 declare
13966 Constr : Elmt_Id;
13967 New_Discr : Entity_Id;
13968 Old_Discr : Entity_Id;
13970 begin
13971 Constr := First_Elmt (Stored_Constraint (Typ));
13972 Old_Discr := First_Stored_Discriminant (Typ);
13973 while Present (Constr) loop
13974 if Is_Entity_Name (Node (Constr))
13975 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13976 then
13977 New_Discr := Entity (Node (Constr));
13979 if Chars (Corresponding_Discriminant (New_Discr)) /=
13980 Chars (Old_Discr)
13981 then
13982 -- The new discriminant has been used to rename a
13983 -- subsequent old discriminant. Introduce a shadow
13984 -- component for the current old discriminant.
13986 New_C := Create_Component (Old_Discr);
13987 Set_Original_Record_Component (New_C, Old_Discr);
13988 end if;
13990 else
13991 -- The constraint has eliminated the old discriminant.
13992 -- Introduce a shadow component.
13994 New_C := Create_Component (Old_Discr);
13995 Set_Original_Record_Component (New_C, Old_Discr);
13996 end if;
13998 Next_Elmt (Constr);
13999 Next_Stored_Discriminant (Old_Discr);
14000 end loop;
14001 end;
14002 end if;
14003 end Add_Discriminants;
14005 if Is_Static
14006 and then Is_Variant_Record (Typ)
14007 then
14008 Collect_Fixed_Components (Typ);
14010 Gather_Components (
14011 Typ,
14012 Component_List (Type_Definition (Parent (Typ))),
14013 Governed_By => Assoc_List,
14014 Into => Comp_List,
14015 Report_Errors => Errors);
14016 pragma Assert (not Errors);
14018 Create_All_Components;
14020 -- If the subtype declaration is created for a tagged type derivation
14021 -- with constraints, we retrieve the record definition of the parent
14022 -- type to select the components of the proper variant.
14024 elsif Is_Static
14025 and then Is_Tagged_Type (Typ)
14026 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14027 and then
14028 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14029 and then Is_Variant_Record (Parent_Type)
14030 then
14031 Collect_Fixed_Components (Typ);
14033 Gather_Components
14034 (Typ,
14035 Component_List (Type_Definition (Parent (Parent_Type))),
14036 Governed_By => Assoc_List,
14037 Into => Comp_List,
14038 Report_Errors => Errors);
14040 -- Note: previously there was a check at this point that no errors
14041 -- were detected. As a consequence of AI05-220 there may be an error
14042 -- if an inherited discriminant that controls a variant has a non-
14043 -- static constraint.
14045 -- If the tagged derivation has a type extension, collect all the
14046 -- new components therein.
14048 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14049 then
14050 Old_C := First_Component (Typ);
14051 while Present (Old_C) loop
14052 if Original_Record_Component (Old_C) = Old_C
14053 and then Chars (Old_C) /= Name_uTag
14054 and then Chars (Old_C) /= Name_uParent
14055 then
14056 Append_Elmt (Old_C, Comp_List);
14057 end if;
14059 Next_Component (Old_C);
14060 end loop;
14061 end if;
14063 Create_All_Components;
14065 else
14066 -- If discriminants are not static, or if this is a multi-level type
14067 -- extension, we have to include all components of the parent type.
14069 Old_C := First_Component (Typ);
14070 while Present (Old_C) loop
14071 New_C := Create_Component (Old_C);
14073 Set_Etype
14074 (New_C,
14075 Constrain_Component_Type
14076 (Old_C, Subt, Decl_Node, Typ, Constraints));
14077 Set_Is_Public (New_C, Is_Public (Subt));
14079 Next_Component (Old_C);
14080 end loop;
14081 end if;
14083 End_Scope;
14084 end Create_Constrained_Components;
14086 ------------------------------------------
14087 -- Decimal_Fixed_Point_Type_Declaration --
14088 ------------------------------------------
14090 procedure Decimal_Fixed_Point_Type_Declaration
14091 (T : Entity_Id;
14092 Def : Node_Id)
14094 Loc : constant Source_Ptr := Sloc (Def);
14095 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14096 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14097 Implicit_Base : Entity_Id;
14098 Digs_Val : Uint;
14099 Delta_Val : Ureal;
14100 Scale_Val : Uint;
14101 Bound_Val : Ureal;
14103 begin
14104 Check_SPARK_05_Restriction
14105 ("decimal fixed point type is not allowed", Def);
14106 Check_Restriction (No_Fixed_Point, Def);
14108 -- Create implicit base type
14110 Implicit_Base :=
14111 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14112 Set_Etype (Implicit_Base, Implicit_Base);
14114 -- Analyze and process delta expression
14116 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14118 Check_Delta_Expression (Delta_Expr);
14119 Delta_Val := Expr_Value_R (Delta_Expr);
14121 -- Check delta is power of 10, and determine scale value from it
14123 declare
14124 Val : Ureal;
14126 begin
14127 Scale_Val := Uint_0;
14128 Val := Delta_Val;
14130 if Val < Ureal_1 then
14131 while Val < Ureal_1 loop
14132 Val := Val * Ureal_10;
14133 Scale_Val := Scale_Val + 1;
14134 end loop;
14136 if Scale_Val > 18 then
14137 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14138 Scale_Val := UI_From_Int (+18);
14139 end if;
14141 else
14142 while Val > Ureal_1 loop
14143 Val := Val / Ureal_10;
14144 Scale_Val := Scale_Val - 1;
14145 end loop;
14147 if Scale_Val < -18 then
14148 Error_Msg_N ("scale is less than minimum value of -18", Def);
14149 Scale_Val := UI_From_Int (-18);
14150 end if;
14151 end if;
14153 if Val /= Ureal_1 then
14154 Error_Msg_N ("delta expression must be a power of 10", Def);
14155 Delta_Val := Ureal_10 ** (-Scale_Val);
14156 end if;
14157 end;
14159 -- Set delta, scale and small (small = delta for decimal type)
14161 Set_Delta_Value (Implicit_Base, Delta_Val);
14162 Set_Scale_Value (Implicit_Base, Scale_Val);
14163 Set_Small_Value (Implicit_Base, Delta_Val);
14165 -- Analyze and process digits expression
14167 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14168 Check_Digits_Expression (Digs_Expr);
14169 Digs_Val := Expr_Value (Digs_Expr);
14171 if Digs_Val > 18 then
14172 Digs_Val := UI_From_Int (+18);
14173 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14174 end if;
14176 Set_Digits_Value (Implicit_Base, Digs_Val);
14177 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14179 -- Set range of base type from digits value for now. This will be
14180 -- expanded to represent the true underlying base range by Freeze.
14182 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14184 -- Note: We leave size as zero for now, size will be set at freeze
14185 -- time. We have to do this for ordinary fixed-point, because the size
14186 -- depends on the specified small, and we might as well do the same for
14187 -- decimal fixed-point.
14189 pragma Assert (Esize (Implicit_Base) = Uint_0);
14191 -- If there are bounds given in the declaration use them as the
14192 -- bounds of the first named subtype.
14194 if Present (Real_Range_Specification (Def)) then
14195 declare
14196 RRS : constant Node_Id := Real_Range_Specification (Def);
14197 Low : constant Node_Id := Low_Bound (RRS);
14198 High : constant Node_Id := High_Bound (RRS);
14199 Low_Val : Ureal;
14200 High_Val : Ureal;
14202 begin
14203 Analyze_And_Resolve (Low, Any_Real);
14204 Analyze_And_Resolve (High, Any_Real);
14205 Check_Real_Bound (Low);
14206 Check_Real_Bound (High);
14207 Low_Val := Expr_Value_R (Low);
14208 High_Val := Expr_Value_R (High);
14210 if Low_Val < (-Bound_Val) then
14211 Error_Msg_N
14212 ("range low bound too small for digits value", Low);
14213 Low_Val := -Bound_Val;
14214 end if;
14216 if High_Val > Bound_Val then
14217 Error_Msg_N
14218 ("range high bound too large for digits value", High);
14219 High_Val := Bound_Val;
14220 end if;
14222 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14223 end;
14225 -- If no explicit range, use range that corresponds to given
14226 -- digits value. This will end up as the final range for the
14227 -- first subtype.
14229 else
14230 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14231 end if;
14233 -- Complete entity for first subtype. The inheritance of the rep item
14234 -- chain ensures that SPARK-related pragmas are not clobbered when the
14235 -- decimal fixed point type acts as a full view of a private type.
14237 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14238 Set_Etype (T, Implicit_Base);
14239 Set_Size_Info (T, Implicit_Base);
14240 Inherit_Rep_Item_Chain (T, Implicit_Base);
14241 Set_Digits_Value (T, Digs_Val);
14242 Set_Delta_Value (T, Delta_Val);
14243 Set_Small_Value (T, Delta_Val);
14244 Set_Scale_Value (T, Scale_Val);
14245 Set_Is_Constrained (T);
14246 end Decimal_Fixed_Point_Type_Declaration;
14248 -----------------------------------
14249 -- Derive_Progenitor_Subprograms --
14250 -----------------------------------
14252 procedure Derive_Progenitor_Subprograms
14253 (Parent_Type : Entity_Id;
14254 Tagged_Type : Entity_Id)
14256 E : Entity_Id;
14257 Elmt : Elmt_Id;
14258 Iface : Entity_Id;
14259 Iface_Elmt : Elmt_Id;
14260 Iface_Subp : Entity_Id;
14261 New_Subp : Entity_Id := Empty;
14262 Prim_Elmt : Elmt_Id;
14263 Subp : Entity_Id;
14264 Typ : Entity_Id;
14266 begin
14267 pragma Assert (Ada_Version >= Ada_2005
14268 and then Is_Record_Type (Tagged_Type)
14269 and then Is_Tagged_Type (Tagged_Type)
14270 and then Has_Interfaces (Tagged_Type));
14272 -- Step 1: Transfer to the full-view primitives associated with the
14273 -- partial-view that cover interface primitives. Conceptually this
14274 -- work should be done later by Process_Full_View; done here to
14275 -- simplify its implementation at later stages. It can be safely
14276 -- done here because interfaces must be visible in the partial and
14277 -- private view (RM 7.3(7.3/2)).
14279 -- Small optimization: This work is only required if the parent may
14280 -- have entities whose Alias attribute reference an interface primitive.
14281 -- Such a situation may occur if the parent is an abstract type and the
14282 -- primitive has not been yet overridden or if the parent is a generic
14283 -- formal type covering interfaces.
14285 -- If the tagged type is not abstract, it cannot have abstract
14286 -- primitives (the only entities in the list of primitives of
14287 -- non-abstract tagged types that can reference abstract primitives
14288 -- through its Alias attribute are the internal entities that have
14289 -- attribute Interface_Alias, and these entities are generated later
14290 -- by Add_Internal_Interface_Entities).
14292 if In_Private_Part (Current_Scope)
14293 and then (Is_Abstract_Type (Parent_Type)
14294 or else
14295 Is_Generic_Type (Parent_Type))
14296 then
14297 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14298 while Present (Elmt) loop
14299 Subp := Node (Elmt);
14301 -- At this stage it is not possible to have entities in the list
14302 -- of primitives that have attribute Interface_Alias.
14304 pragma Assert (No (Interface_Alias (Subp)));
14306 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14308 if Is_Interface (Typ) then
14309 E := Find_Primitive_Covering_Interface
14310 (Tagged_Type => Tagged_Type,
14311 Iface_Prim => Subp);
14313 if Present (E)
14314 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14315 then
14316 Replace_Elmt (Elmt, E);
14317 Remove_Homonym (Subp);
14318 end if;
14319 end if;
14321 Next_Elmt (Elmt);
14322 end loop;
14323 end if;
14325 -- Step 2: Add primitives of progenitors that are not implemented by
14326 -- parents of Tagged_Type.
14328 if Present (Interfaces (Base_Type (Tagged_Type))) then
14329 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14330 while Present (Iface_Elmt) loop
14331 Iface := Node (Iface_Elmt);
14333 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14334 while Present (Prim_Elmt) loop
14335 Iface_Subp := Node (Prim_Elmt);
14337 -- Exclude derivation of predefined primitives except those
14338 -- that come from source, or are inherited from one that comes
14339 -- from source. Required to catch declarations of equality
14340 -- operators of interfaces. For example:
14342 -- type Iface is interface;
14343 -- function "=" (Left, Right : Iface) return Boolean;
14345 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14346 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14347 then
14348 E := Find_Primitive_Covering_Interface
14349 (Tagged_Type => Tagged_Type,
14350 Iface_Prim => Iface_Subp);
14352 -- If not found we derive a new primitive leaving its alias
14353 -- attribute referencing the interface primitive.
14355 if No (E) then
14356 Derive_Subprogram
14357 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14359 -- Ada 2012 (AI05-0197): If the covering primitive's name
14360 -- differs from the name of the interface primitive then it
14361 -- is a private primitive inherited from a parent type. In
14362 -- such case, given that Tagged_Type covers the interface,
14363 -- the inherited private primitive becomes visible. For such
14364 -- purpose we add a new entity that renames the inherited
14365 -- private primitive.
14367 elsif Chars (E) /= Chars (Iface_Subp) then
14368 pragma Assert (Has_Suffix (E, 'P'));
14369 Derive_Subprogram
14370 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14371 Set_Alias (New_Subp, E);
14372 Set_Is_Abstract_Subprogram (New_Subp,
14373 Is_Abstract_Subprogram (E));
14375 -- Propagate to the full view interface entities associated
14376 -- with the partial view.
14378 elsif In_Private_Part (Current_Scope)
14379 and then Present (Alias (E))
14380 and then Alias (E) = Iface_Subp
14381 and then
14382 List_Containing (Parent (E)) /=
14383 Private_Declarations
14384 (Specification
14385 (Unit_Declaration_Node (Current_Scope)))
14386 then
14387 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14388 end if;
14389 end if;
14391 Next_Elmt (Prim_Elmt);
14392 end loop;
14394 Next_Elmt (Iface_Elmt);
14395 end loop;
14396 end if;
14397 end Derive_Progenitor_Subprograms;
14399 -----------------------
14400 -- Derive_Subprogram --
14401 -----------------------
14403 procedure Derive_Subprogram
14404 (New_Subp : in out Entity_Id;
14405 Parent_Subp : Entity_Id;
14406 Derived_Type : Entity_Id;
14407 Parent_Type : Entity_Id;
14408 Actual_Subp : Entity_Id := Empty)
14410 Formal : Entity_Id;
14411 -- Formal parameter of parent primitive operation
14413 Formal_Of_Actual : Entity_Id;
14414 -- Formal parameter of actual operation, when the derivation is to
14415 -- create a renaming for a primitive operation of an actual in an
14416 -- instantiation.
14418 New_Formal : Entity_Id;
14419 -- Formal of inherited operation
14421 Visible_Subp : Entity_Id := Parent_Subp;
14423 function Is_Private_Overriding return Boolean;
14424 -- If Subp is a private overriding of a visible operation, the inherited
14425 -- operation derives from the overridden op (even though its body is the
14426 -- overriding one) and the inherited operation is visible now. See
14427 -- sem_disp to see the full details of the handling of the overridden
14428 -- subprogram, which is removed from the list of primitive operations of
14429 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14430 -- and used to diagnose abstract operations that need overriding in the
14431 -- derived type.
14433 procedure Replace_Type (Id, New_Id : Entity_Id);
14434 -- When the type is an anonymous access type, create a new access type
14435 -- designating the derived type.
14437 procedure Set_Derived_Name;
14438 -- This procedure sets the appropriate Chars name for New_Subp. This
14439 -- is normally just a copy of the parent name. An exception arises for
14440 -- type support subprograms, where the name is changed to reflect the
14441 -- name of the derived type, e.g. if type foo is derived from type bar,
14442 -- then a procedure barDA is derived with a name fooDA.
14444 ---------------------------
14445 -- Is_Private_Overriding --
14446 ---------------------------
14448 function Is_Private_Overriding return Boolean is
14449 Prev : Entity_Id;
14451 begin
14452 -- If the parent is not a dispatching operation there is no
14453 -- need to investigate overridings
14455 if not Is_Dispatching_Operation (Parent_Subp) then
14456 return False;
14457 end if;
14459 -- The visible operation that is overridden is a homonym of the
14460 -- parent subprogram. We scan the homonym chain to find the one
14461 -- whose alias is the subprogram we are deriving.
14463 Prev := Current_Entity (Parent_Subp);
14464 while Present (Prev) loop
14465 if Ekind (Prev) = Ekind (Parent_Subp)
14466 and then Alias (Prev) = Parent_Subp
14467 and then Scope (Parent_Subp) = Scope (Prev)
14468 and then not Is_Hidden (Prev)
14469 then
14470 Visible_Subp := Prev;
14471 return True;
14472 end if;
14474 Prev := Homonym (Prev);
14475 end loop;
14477 return False;
14478 end Is_Private_Overriding;
14480 ------------------
14481 -- Replace_Type --
14482 ------------------
14484 procedure Replace_Type (Id, New_Id : Entity_Id) is
14485 Id_Type : constant Entity_Id := Etype (Id);
14486 Acc_Type : Entity_Id;
14487 Par : constant Node_Id := Parent (Derived_Type);
14489 begin
14490 -- When the type is an anonymous access type, create a new access
14491 -- type designating the derived type. This itype must be elaborated
14492 -- at the point of the derivation, not on subsequent calls that may
14493 -- be out of the proper scope for Gigi, so we insert a reference to
14494 -- it after the derivation.
14496 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14497 declare
14498 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14500 begin
14501 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14502 and then Present (Full_View (Desig_Typ))
14503 and then not Is_Private_Type (Parent_Type)
14504 then
14505 Desig_Typ := Full_View (Desig_Typ);
14506 end if;
14508 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14510 -- Ada 2005 (AI-251): Handle also derivations of abstract
14511 -- interface primitives.
14513 or else (Is_Interface (Desig_Typ)
14514 and then not Is_Class_Wide_Type (Desig_Typ))
14515 then
14516 Acc_Type := New_Copy (Id_Type);
14517 Set_Etype (Acc_Type, Acc_Type);
14518 Set_Scope (Acc_Type, New_Subp);
14520 -- Set size of anonymous access type. If we have an access
14521 -- to an unconstrained array, this is a fat pointer, so it
14522 -- is sizes at twice addtress size.
14524 if Is_Array_Type (Desig_Typ)
14525 and then not Is_Constrained (Desig_Typ)
14526 then
14527 Init_Size (Acc_Type, 2 * System_Address_Size);
14529 -- Other cases use a thin pointer
14531 else
14532 Init_Size (Acc_Type, System_Address_Size);
14533 end if;
14535 -- Set remaining characterstics of anonymous access type
14537 Init_Alignment (Acc_Type);
14538 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14540 Set_Etype (New_Id, Acc_Type);
14541 Set_Scope (New_Id, New_Subp);
14543 -- Create a reference to it
14545 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14547 else
14548 Set_Etype (New_Id, Id_Type);
14549 end if;
14550 end;
14552 -- In Ada2012, a formal may have an incomplete type but the type
14553 -- derivation that inherits the primitive follows the full view.
14555 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14556 or else
14557 (Ekind (Id_Type) = E_Record_Type_With_Private
14558 and then Present (Full_View (Id_Type))
14559 and then
14560 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14561 or else
14562 (Ada_Version >= Ada_2012
14563 and then Ekind (Id_Type) = E_Incomplete_Type
14564 and then Full_View (Id_Type) = Parent_Type)
14565 then
14566 -- Constraint checks on formals are generated during expansion,
14567 -- based on the signature of the original subprogram. The bounds
14568 -- of the derived type are not relevant, and thus we can use
14569 -- the base type for the formals. However, the return type may be
14570 -- used in a context that requires that the proper static bounds
14571 -- be used (a case statement, for example) and for those cases
14572 -- we must use the derived type (first subtype), not its base.
14574 -- If the derived_type_definition has no constraints, we know that
14575 -- the derived type has the same constraints as the first subtype
14576 -- of the parent, and we can also use it rather than its base,
14577 -- which can lead to more efficient code.
14579 if Etype (Id) = Parent_Type then
14580 if Is_Scalar_Type (Parent_Type)
14581 and then
14582 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14583 then
14584 Set_Etype (New_Id, Derived_Type);
14586 elsif Nkind (Par) = N_Full_Type_Declaration
14587 and then
14588 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14589 and then
14590 Is_Entity_Name
14591 (Subtype_Indication (Type_Definition (Par)))
14592 then
14593 Set_Etype (New_Id, Derived_Type);
14595 else
14596 Set_Etype (New_Id, Base_Type (Derived_Type));
14597 end if;
14599 else
14600 Set_Etype (New_Id, Base_Type (Derived_Type));
14601 end if;
14603 else
14604 Set_Etype (New_Id, Etype (Id));
14605 end if;
14606 end Replace_Type;
14608 ----------------------
14609 -- Set_Derived_Name --
14610 ----------------------
14612 procedure Set_Derived_Name is
14613 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14614 begin
14615 if Nm = TSS_Null then
14616 Set_Chars (New_Subp, Chars (Parent_Subp));
14617 else
14618 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14619 end if;
14620 end Set_Derived_Name;
14622 -- Start of processing for Derive_Subprogram
14624 begin
14625 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14626 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14628 -- Check whether the inherited subprogram is a private operation that
14629 -- should be inherited but not yet made visible. Such subprograms can
14630 -- become visible at a later point (e.g., the private part of a public
14631 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14632 -- following predicate is true, then this is not such a private
14633 -- operation and the subprogram simply inherits the name of the parent
14634 -- subprogram. Note the special check for the names of controlled
14635 -- operations, which are currently exempted from being inherited with
14636 -- a hidden name because they must be findable for generation of
14637 -- implicit run-time calls.
14639 if not Is_Hidden (Parent_Subp)
14640 or else Is_Internal (Parent_Subp)
14641 or else Is_Private_Overriding
14642 or else Is_Internal_Name (Chars (Parent_Subp))
14643 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14644 Name_Adjust,
14645 Name_Finalize)
14646 then
14647 Set_Derived_Name;
14649 -- An inherited dispatching equality will be overridden by an internally
14650 -- generated one, or by an explicit one, so preserve its name and thus
14651 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14652 -- private operation it may become invisible if the full view has
14653 -- progenitors, and the dispatch table will be malformed.
14654 -- We check that the type is limited to handle the anomalous declaration
14655 -- of Limited_Controlled, which is derived from a non-limited type, and
14656 -- which is handled specially elsewhere as well.
14658 elsif Chars (Parent_Subp) = Name_Op_Eq
14659 and then Is_Dispatching_Operation (Parent_Subp)
14660 and then Etype (Parent_Subp) = Standard_Boolean
14661 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14662 and then
14663 Etype (First_Formal (Parent_Subp)) =
14664 Etype (Next_Formal (First_Formal (Parent_Subp)))
14665 then
14666 Set_Derived_Name;
14668 -- If parent is hidden, this can be a regular derivation if the
14669 -- parent is immediately visible in a non-instantiating context,
14670 -- or if we are in the private part of an instance. This test
14671 -- should still be refined ???
14673 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14674 -- operation as a non-visible operation in cases where the parent
14675 -- subprogram might not be visible now, but was visible within the
14676 -- original generic, so it would be wrong to make the inherited
14677 -- subprogram non-visible now. (Not clear if this test is fully
14678 -- correct; are there any cases where we should declare the inherited
14679 -- operation as not visible to avoid it being overridden, e.g., when
14680 -- the parent type is a generic actual with private primitives ???)
14682 -- (they should be treated the same as other private inherited
14683 -- subprograms, but it's not clear how to do this cleanly). ???
14685 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14686 and then Is_Immediately_Visible (Parent_Subp)
14687 and then not In_Instance)
14688 or else In_Instance_Not_Visible
14689 then
14690 Set_Derived_Name;
14692 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14693 -- overrides an interface primitive because interface primitives
14694 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14696 elsif Ada_Version >= Ada_2005
14697 and then Is_Dispatching_Operation (Parent_Subp)
14698 and then Covers_Some_Interface (Parent_Subp)
14699 then
14700 Set_Derived_Name;
14702 -- Otherwise, the type is inheriting a private operation, so enter it
14703 -- with a special name so it can't be overridden.
14705 else
14706 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14707 end if;
14709 Set_Parent (New_Subp, Parent (Derived_Type));
14711 if Present (Actual_Subp) then
14712 Replace_Type (Actual_Subp, New_Subp);
14713 else
14714 Replace_Type (Parent_Subp, New_Subp);
14715 end if;
14717 Conditional_Delay (New_Subp, Parent_Subp);
14719 -- If we are creating a renaming for a primitive operation of an
14720 -- actual of a generic derived type, we must examine the signature
14721 -- of the actual primitive, not that of the generic formal, which for
14722 -- example may be an interface. However the name and initial value
14723 -- of the inherited operation are those of the formal primitive.
14725 Formal := First_Formal (Parent_Subp);
14727 if Present (Actual_Subp) then
14728 Formal_Of_Actual := First_Formal (Actual_Subp);
14729 else
14730 Formal_Of_Actual := Empty;
14731 end if;
14733 while Present (Formal) loop
14734 New_Formal := New_Copy (Formal);
14736 -- Normally we do not go copying parents, but in the case of
14737 -- formals, we need to link up to the declaration (which is the
14738 -- parameter specification), and it is fine to link up to the
14739 -- original formal's parameter specification in this case.
14741 Set_Parent (New_Formal, Parent (Formal));
14742 Append_Entity (New_Formal, New_Subp);
14744 if Present (Formal_Of_Actual) then
14745 Replace_Type (Formal_Of_Actual, New_Formal);
14746 Next_Formal (Formal_Of_Actual);
14747 else
14748 Replace_Type (Formal, New_Formal);
14749 end if;
14751 Next_Formal (Formal);
14752 end loop;
14754 -- If this derivation corresponds to a tagged generic actual, then
14755 -- primitive operations rename those of the actual. Otherwise the
14756 -- primitive operations rename those of the parent type, If the parent
14757 -- renames an intrinsic operator, so does the new subprogram. We except
14758 -- concatenation, which is always properly typed, and does not get
14759 -- expanded as other intrinsic operations.
14761 if No (Actual_Subp) then
14762 if Is_Intrinsic_Subprogram (Parent_Subp) then
14763 Set_Is_Intrinsic_Subprogram (New_Subp);
14765 if Present (Alias (Parent_Subp))
14766 and then Chars (Parent_Subp) /= Name_Op_Concat
14767 then
14768 Set_Alias (New_Subp, Alias (Parent_Subp));
14769 else
14770 Set_Alias (New_Subp, Parent_Subp);
14771 end if;
14773 else
14774 Set_Alias (New_Subp, Parent_Subp);
14775 end if;
14777 else
14778 Set_Alias (New_Subp, Actual_Subp);
14779 end if;
14781 -- Inherit the "ghostness" from the parent subprogram
14783 if Is_Ghost_Entity (Alias (New_Subp)) then
14784 Set_Is_Ghost_Entity (New_Subp);
14785 end if;
14787 -- Derived subprograms of a tagged type must inherit the convention
14788 -- of the parent subprogram (a requirement of AI-117). Derived
14789 -- subprograms of untagged types simply get convention Ada by default.
14791 -- If the derived type is a tagged generic formal type with unknown
14792 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14794 -- However, if the type is derived from a generic formal, the further
14795 -- inherited subprogram has the convention of the non-generic ancestor.
14796 -- Otherwise there would be no way to override the operation.
14797 -- (This is subject to forthcoming ARG discussions).
14799 if Is_Tagged_Type (Derived_Type) then
14800 if Is_Generic_Type (Derived_Type)
14801 and then Has_Unknown_Discriminants (Derived_Type)
14802 then
14803 Set_Convention (New_Subp, Convention_Intrinsic);
14805 else
14806 if Is_Generic_Type (Parent_Type)
14807 and then Has_Unknown_Discriminants (Parent_Type)
14808 then
14809 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14810 else
14811 Set_Convention (New_Subp, Convention (Parent_Subp));
14812 end if;
14813 end if;
14814 end if;
14816 -- Predefined controlled operations retain their name even if the parent
14817 -- is hidden (see above), but they are not primitive operations if the
14818 -- ancestor is not visible, for example if the parent is a private
14819 -- extension completed with a controlled extension. Note that a full
14820 -- type that is controlled can break privacy: the flag Is_Controlled is
14821 -- set on both views of the type.
14823 if Is_Controlled (Parent_Type)
14824 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14825 Name_Adjust,
14826 Name_Finalize)
14827 and then Is_Hidden (Parent_Subp)
14828 and then not Is_Visibly_Controlled (Parent_Type)
14829 then
14830 Set_Is_Hidden (New_Subp);
14831 end if;
14833 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14834 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14836 if Ekind (Parent_Subp) = E_Procedure then
14837 Set_Is_Valued_Procedure
14838 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14839 else
14840 Set_Has_Controlling_Result
14841 (New_Subp, Has_Controlling_Result (Parent_Subp));
14842 end if;
14844 -- No_Return must be inherited properly. If this is overridden in the
14845 -- case of a dispatching operation, then a check is made in Sem_Disp
14846 -- that the overriding operation is also No_Return (no such check is
14847 -- required for the case of non-dispatching operation.
14849 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14851 -- A derived function with a controlling result is abstract. If the
14852 -- Derived_Type is a nonabstract formal generic derived type, then
14853 -- inherited operations are not abstract: the required check is done at
14854 -- instantiation time. If the derivation is for a generic actual, the
14855 -- function is not abstract unless the actual is.
14857 if Is_Generic_Type (Derived_Type)
14858 and then not Is_Abstract_Type (Derived_Type)
14859 then
14860 null;
14862 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14863 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14865 -- A subprogram subject to pragma Extensions_Visible with value False
14866 -- requires overriding if the subprogram has at least one controlling
14867 -- OUT parameter (SPARK RM 6.1.7(6)).
14869 elsif Ada_Version >= Ada_2005
14870 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14871 or else (Is_Tagged_Type (Derived_Type)
14872 and then Etype (New_Subp) = Derived_Type
14873 and then not Is_Null_Extension (Derived_Type))
14874 or else (Is_Tagged_Type (Derived_Type)
14875 and then Ekind (Etype (New_Subp)) =
14876 E_Anonymous_Access_Type
14877 and then Designated_Type (Etype (New_Subp)) =
14878 Derived_Type
14879 and then not Is_Null_Extension (Derived_Type))
14880 or else (Comes_From_Source (Alias (New_Subp))
14881 and then Is_EVF_Procedure (Alias (New_Subp))))
14882 and then No (Actual_Subp)
14883 then
14884 if not Is_Tagged_Type (Derived_Type)
14885 or else Is_Abstract_Type (Derived_Type)
14886 or else Is_Abstract_Subprogram (Alias (New_Subp))
14887 then
14888 Set_Is_Abstract_Subprogram (New_Subp);
14889 else
14890 Set_Requires_Overriding (New_Subp);
14891 end if;
14893 elsif Ada_Version < Ada_2005
14894 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14895 or else (Is_Tagged_Type (Derived_Type)
14896 and then Etype (New_Subp) = Derived_Type
14897 and then No (Actual_Subp)))
14898 then
14899 Set_Is_Abstract_Subprogram (New_Subp);
14901 -- AI05-0097 : an inherited operation that dispatches on result is
14902 -- abstract if the derived type is abstract, even if the parent type
14903 -- is concrete and the derived type is a null extension.
14905 elsif Has_Controlling_Result (Alias (New_Subp))
14906 and then Is_Abstract_Type (Etype (New_Subp))
14907 then
14908 Set_Is_Abstract_Subprogram (New_Subp);
14910 -- Finally, if the parent type is abstract we must verify that all
14911 -- inherited operations are either non-abstract or overridden, or that
14912 -- the derived type itself is abstract (this check is performed at the
14913 -- end of a package declaration, in Check_Abstract_Overriding). A
14914 -- private overriding in the parent type will not be visible in the
14915 -- derivation if we are not in an inner package or in a child unit of
14916 -- the parent type, in which case the abstractness of the inherited
14917 -- operation is carried to the new subprogram.
14919 elsif Is_Abstract_Type (Parent_Type)
14920 and then not In_Open_Scopes (Scope (Parent_Type))
14921 and then Is_Private_Overriding
14922 and then Is_Abstract_Subprogram (Visible_Subp)
14923 then
14924 if No (Actual_Subp) then
14925 Set_Alias (New_Subp, Visible_Subp);
14926 Set_Is_Abstract_Subprogram (New_Subp, True);
14928 else
14929 -- If this is a derivation for an instance of a formal derived
14930 -- type, abstractness comes from the primitive operation of the
14931 -- actual, not from the operation inherited from the ancestor.
14933 Set_Is_Abstract_Subprogram
14934 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14935 end if;
14936 end if;
14938 New_Overloaded_Entity (New_Subp, Derived_Type);
14940 -- Check for case of a derived subprogram for the instantiation of a
14941 -- formal derived tagged type, if so mark the subprogram as dispatching
14942 -- and inherit the dispatching attributes of the actual subprogram. The
14943 -- derived subprogram is effectively renaming of the actual subprogram,
14944 -- so it needs to have the same attributes as the actual.
14946 if Present (Actual_Subp)
14947 and then Is_Dispatching_Operation (Actual_Subp)
14948 then
14949 Set_Is_Dispatching_Operation (New_Subp);
14951 if Present (DTC_Entity (Actual_Subp)) then
14952 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14953 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
14954 end if;
14955 end if;
14957 -- Indicate that a derived subprogram does not require a body and that
14958 -- it does not require processing of default expressions.
14960 Set_Has_Completion (New_Subp);
14961 Set_Default_Expressions_Processed (New_Subp);
14963 if Ekind (New_Subp) = E_Function then
14964 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14965 end if;
14966 end Derive_Subprogram;
14968 ------------------------
14969 -- Derive_Subprograms --
14970 ------------------------
14972 procedure Derive_Subprograms
14973 (Parent_Type : Entity_Id;
14974 Derived_Type : Entity_Id;
14975 Generic_Actual : Entity_Id := Empty)
14977 Op_List : constant Elist_Id :=
14978 Collect_Primitive_Operations (Parent_Type);
14980 function Check_Derived_Type return Boolean;
14981 -- Check that all the entities derived from Parent_Type are found in
14982 -- the list of primitives of Derived_Type exactly in the same order.
14984 procedure Derive_Interface_Subprogram
14985 (New_Subp : in out Entity_Id;
14986 Subp : Entity_Id;
14987 Actual_Subp : Entity_Id);
14988 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14989 -- (which is an interface primitive). If Generic_Actual is present then
14990 -- Actual_Subp is the actual subprogram corresponding with the generic
14991 -- subprogram Subp.
14993 function Check_Derived_Type return Boolean is
14994 E : Entity_Id;
14995 Elmt : Elmt_Id;
14996 List : Elist_Id;
14997 New_Subp : Entity_Id;
14998 Op_Elmt : Elmt_Id;
14999 Subp : Entity_Id;
15001 begin
15002 -- Traverse list of entities in the current scope searching for
15003 -- an incomplete type whose full-view is derived type
15005 E := First_Entity (Scope (Derived_Type));
15006 while Present (E) and then E /= Derived_Type loop
15007 if Ekind (E) = E_Incomplete_Type
15008 and then Present (Full_View (E))
15009 and then Full_View (E) = Derived_Type
15010 then
15011 -- Disable this test if Derived_Type completes an incomplete
15012 -- type because in such case more primitives can be added
15013 -- later to the list of primitives of Derived_Type by routine
15014 -- Process_Incomplete_Dependents
15016 return True;
15017 end if;
15019 E := Next_Entity (E);
15020 end loop;
15022 List := Collect_Primitive_Operations (Derived_Type);
15023 Elmt := First_Elmt (List);
15025 Op_Elmt := First_Elmt (Op_List);
15026 while Present (Op_Elmt) loop
15027 Subp := Node (Op_Elmt);
15028 New_Subp := Node (Elmt);
15030 -- At this early stage Derived_Type has no entities with attribute
15031 -- Interface_Alias. In addition, such primitives are always
15032 -- located at the end of the list of primitives of Parent_Type.
15033 -- Therefore, if found we can safely stop processing pending
15034 -- entities.
15036 exit when Present (Interface_Alias (Subp));
15038 -- Handle hidden entities
15040 if not Is_Predefined_Dispatching_Operation (Subp)
15041 and then Is_Hidden (Subp)
15042 then
15043 if Present (New_Subp)
15044 and then Primitive_Names_Match (Subp, New_Subp)
15045 then
15046 Next_Elmt (Elmt);
15047 end if;
15049 else
15050 if not Present (New_Subp)
15051 or else Ekind (Subp) /= Ekind (New_Subp)
15052 or else not Primitive_Names_Match (Subp, New_Subp)
15053 then
15054 return False;
15055 end if;
15057 Next_Elmt (Elmt);
15058 end if;
15060 Next_Elmt (Op_Elmt);
15061 end loop;
15063 return True;
15064 end Check_Derived_Type;
15066 ---------------------------------
15067 -- Derive_Interface_Subprogram --
15068 ---------------------------------
15070 procedure Derive_Interface_Subprogram
15071 (New_Subp : in out Entity_Id;
15072 Subp : Entity_Id;
15073 Actual_Subp : Entity_Id)
15075 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15076 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15078 begin
15079 pragma Assert (Is_Interface (Iface_Type));
15081 Derive_Subprogram
15082 (New_Subp => New_Subp,
15083 Parent_Subp => Iface_Subp,
15084 Derived_Type => Derived_Type,
15085 Parent_Type => Iface_Type,
15086 Actual_Subp => Actual_Subp);
15088 -- Given that this new interface entity corresponds with a primitive
15089 -- of the parent that was not overridden we must leave it associated
15090 -- with its parent primitive to ensure that it will share the same
15091 -- dispatch table slot when overridden. We must set the Alias to Subp
15092 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15093 -- (in case we inherited Subp from Iface_Type via a nonabstract
15094 -- generic formal type).
15096 if No (Actual_Subp) then
15097 Set_Alias (New_Subp, Subp);
15099 declare
15100 T : Entity_Id := Find_Dispatching_Type (Subp);
15101 begin
15102 while Etype (T) /= T loop
15103 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15104 Set_Is_Abstract_Subprogram (New_Subp, False);
15105 exit;
15106 end if;
15108 T := Etype (T);
15109 end loop;
15110 end;
15112 -- For instantiations this is not needed since the previous call to
15113 -- Derive_Subprogram leaves the entity well decorated.
15115 else
15116 pragma Assert (Alias (New_Subp) = Actual_Subp);
15117 null;
15118 end if;
15119 end Derive_Interface_Subprogram;
15121 -- Local variables
15123 Alias_Subp : Entity_Id;
15124 Act_List : Elist_Id;
15125 Act_Elmt : Elmt_Id;
15126 Act_Subp : Entity_Id := Empty;
15127 Elmt : Elmt_Id;
15128 Need_Search : Boolean := False;
15129 New_Subp : Entity_Id := Empty;
15130 Parent_Base : Entity_Id;
15131 Subp : Entity_Id;
15133 -- Start of processing for Derive_Subprograms
15135 begin
15136 if Ekind (Parent_Type) = E_Record_Type_With_Private
15137 and then Has_Discriminants (Parent_Type)
15138 and then Present (Full_View (Parent_Type))
15139 then
15140 Parent_Base := Full_View (Parent_Type);
15141 else
15142 Parent_Base := Parent_Type;
15143 end if;
15145 if Present (Generic_Actual) then
15146 Act_List := Collect_Primitive_Operations (Generic_Actual);
15147 Act_Elmt := First_Elmt (Act_List);
15148 else
15149 Act_List := No_Elist;
15150 Act_Elmt := No_Elmt;
15151 end if;
15153 -- Derive primitives inherited from the parent. Note that if the generic
15154 -- actual is present, this is not really a type derivation, it is a
15155 -- completion within an instance.
15157 -- Case 1: Derived_Type does not implement interfaces
15159 if not Is_Tagged_Type (Derived_Type)
15160 or else (not Has_Interfaces (Derived_Type)
15161 and then not (Present (Generic_Actual)
15162 and then Has_Interfaces (Generic_Actual)))
15163 then
15164 Elmt := First_Elmt (Op_List);
15165 while Present (Elmt) loop
15166 Subp := Node (Elmt);
15168 -- Literals are derived earlier in the process of building the
15169 -- derived type, and are skipped here.
15171 if Ekind (Subp) = E_Enumeration_Literal then
15172 null;
15174 -- The actual is a direct descendant and the common primitive
15175 -- operations appear in the same order.
15177 -- If the generic parent type is present, the derived type is an
15178 -- instance of a formal derived type, and within the instance its
15179 -- operations are those of the actual. We derive from the formal
15180 -- type but make the inherited operations aliases of the
15181 -- corresponding operations of the actual.
15183 else
15184 pragma Assert (No (Node (Act_Elmt))
15185 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15186 and then
15187 Type_Conformant
15188 (Subp, Node (Act_Elmt),
15189 Skip_Controlling_Formals => True)));
15191 Derive_Subprogram
15192 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15194 if Present (Act_Elmt) then
15195 Next_Elmt (Act_Elmt);
15196 end if;
15197 end if;
15199 Next_Elmt (Elmt);
15200 end loop;
15202 -- Case 2: Derived_Type implements interfaces
15204 else
15205 -- If the parent type has no predefined primitives we remove
15206 -- predefined primitives from the list of primitives of generic
15207 -- actual to simplify the complexity of this algorithm.
15209 if Present (Generic_Actual) then
15210 declare
15211 Has_Predefined_Primitives : Boolean := False;
15213 begin
15214 -- Check if the parent type has predefined primitives
15216 Elmt := First_Elmt (Op_List);
15217 while Present (Elmt) loop
15218 Subp := Node (Elmt);
15220 if Is_Predefined_Dispatching_Operation (Subp)
15221 and then not Comes_From_Source (Ultimate_Alias (Subp))
15222 then
15223 Has_Predefined_Primitives := True;
15224 exit;
15225 end if;
15227 Next_Elmt (Elmt);
15228 end loop;
15230 -- Remove predefined primitives of Generic_Actual. We must use
15231 -- an auxiliary list because in case of tagged types the value
15232 -- returned by Collect_Primitive_Operations is the value stored
15233 -- in its Primitive_Operations attribute (and we don't want to
15234 -- modify its current contents).
15236 if not Has_Predefined_Primitives then
15237 declare
15238 Aux_List : constant Elist_Id := New_Elmt_List;
15240 begin
15241 Elmt := First_Elmt (Act_List);
15242 while Present (Elmt) loop
15243 Subp := Node (Elmt);
15245 if not Is_Predefined_Dispatching_Operation (Subp)
15246 or else Comes_From_Source (Subp)
15247 then
15248 Append_Elmt (Subp, Aux_List);
15249 end if;
15251 Next_Elmt (Elmt);
15252 end loop;
15254 Act_List := Aux_List;
15255 end;
15256 end if;
15258 Act_Elmt := First_Elmt (Act_List);
15259 Act_Subp := Node (Act_Elmt);
15260 end;
15261 end if;
15263 -- Stage 1: If the generic actual is not present we derive the
15264 -- primitives inherited from the parent type. If the generic parent
15265 -- type is present, the derived type is an instance of a formal
15266 -- derived type, and within the instance its operations are those of
15267 -- the actual. We derive from the formal type but make the inherited
15268 -- operations aliases of the corresponding operations of the actual.
15270 Elmt := First_Elmt (Op_List);
15271 while Present (Elmt) loop
15272 Subp := Node (Elmt);
15273 Alias_Subp := Ultimate_Alias (Subp);
15275 -- Do not derive internal entities of the parent that link
15276 -- interface primitives with their covering primitive. These
15277 -- entities will be added to this type when frozen.
15279 if Present (Interface_Alias (Subp)) then
15280 goto Continue;
15281 end if;
15283 -- If the generic actual is present find the corresponding
15284 -- operation in the generic actual. If the parent type is a
15285 -- direct ancestor of the derived type then, even if it is an
15286 -- interface, the operations are inherited from the primary
15287 -- dispatch table and are in the proper order. If we detect here
15288 -- that primitives are not in the same order we traverse the list
15289 -- of primitive operations of the actual to find the one that
15290 -- implements the interface primitive.
15292 if Need_Search
15293 or else
15294 (Present (Generic_Actual)
15295 and then Present (Act_Subp)
15296 and then not
15297 (Primitive_Names_Match (Subp, Act_Subp)
15298 and then
15299 Type_Conformant (Subp, Act_Subp,
15300 Skip_Controlling_Formals => True)))
15301 then
15302 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15303 Use_Full_View => True));
15305 -- Remember that we need searching for all pending primitives
15307 Need_Search := True;
15309 -- Handle entities associated with interface primitives
15311 if Present (Alias_Subp)
15312 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15313 and then not Is_Predefined_Dispatching_Operation (Subp)
15314 then
15315 -- Search for the primitive in the homonym chain
15317 Act_Subp :=
15318 Find_Primitive_Covering_Interface
15319 (Tagged_Type => Generic_Actual,
15320 Iface_Prim => Alias_Subp);
15322 -- Previous search may not locate primitives covering
15323 -- interfaces defined in generics units or instantiations.
15324 -- (it fails if the covering primitive has formals whose
15325 -- type is also defined in generics or instantiations).
15326 -- In such case we search in the list of primitives of the
15327 -- generic actual for the internal entity that links the
15328 -- interface primitive and the covering primitive.
15330 if No (Act_Subp)
15331 and then Is_Generic_Type (Parent_Type)
15332 then
15333 -- This code has been designed to handle only generic
15334 -- formals that implement interfaces that are defined
15335 -- in a generic unit or instantiation. If this code is
15336 -- needed for other cases we must review it because
15337 -- (given that it relies on Original_Location to locate
15338 -- the primitive of Generic_Actual that covers the
15339 -- interface) it could leave linked through attribute
15340 -- Alias entities of unrelated instantiations).
15342 pragma Assert
15343 (Is_Generic_Unit
15344 (Scope (Find_Dispatching_Type (Alias_Subp)))
15345 or else
15346 Instantiation_Depth
15347 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15349 declare
15350 Iface_Prim_Loc : constant Source_Ptr :=
15351 Original_Location (Sloc (Alias_Subp));
15353 Elmt : Elmt_Id;
15354 Prim : Entity_Id;
15356 begin
15357 Elmt :=
15358 First_Elmt (Primitive_Operations (Generic_Actual));
15360 Search : while Present (Elmt) loop
15361 Prim := Node (Elmt);
15363 if Present (Interface_Alias (Prim))
15364 and then Original_Location
15365 (Sloc (Interface_Alias (Prim))) =
15366 Iface_Prim_Loc
15367 then
15368 Act_Subp := Alias (Prim);
15369 exit Search;
15370 end if;
15372 Next_Elmt (Elmt);
15373 end loop Search;
15374 end;
15375 end if;
15377 pragma Assert (Present (Act_Subp)
15378 or else Is_Abstract_Type (Generic_Actual)
15379 or else Serious_Errors_Detected > 0);
15381 -- Handle predefined primitives plus the rest of user-defined
15382 -- primitives
15384 else
15385 Act_Elmt := First_Elmt (Act_List);
15386 while Present (Act_Elmt) loop
15387 Act_Subp := Node (Act_Elmt);
15389 exit when Primitive_Names_Match (Subp, Act_Subp)
15390 and then Type_Conformant
15391 (Subp, Act_Subp,
15392 Skip_Controlling_Formals => True)
15393 and then No (Interface_Alias (Act_Subp));
15395 Next_Elmt (Act_Elmt);
15396 end loop;
15398 if No (Act_Elmt) then
15399 Act_Subp := Empty;
15400 end if;
15401 end if;
15402 end if;
15404 -- Case 1: If the parent is a limited interface then it has the
15405 -- predefined primitives of synchronized interfaces. However, the
15406 -- actual type may be a non-limited type and hence it does not
15407 -- have such primitives.
15409 if Present (Generic_Actual)
15410 and then not Present (Act_Subp)
15411 and then Is_Limited_Interface (Parent_Base)
15412 and then Is_Predefined_Interface_Primitive (Subp)
15413 then
15414 null;
15416 -- Case 2: Inherit entities associated with interfaces that were
15417 -- not covered by the parent type. We exclude here null interface
15418 -- primitives because they do not need special management.
15420 -- We also exclude interface operations that are renamings. If the
15421 -- subprogram is an explicit renaming of an interface primitive,
15422 -- it is a regular primitive operation, and the presence of its
15423 -- alias is not relevant: it has to be derived like any other
15424 -- primitive.
15426 elsif Present (Alias (Subp))
15427 and then Nkind (Unit_Declaration_Node (Subp)) /=
15428 N_Subprogram_Renaming_Declaration
15429 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15430 and then not
15431 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15432 and then Null_Present (Parent (Alias_Subp)))
15433 then
15434 -- If this is an abstract private type then we transfer the
15435 -- derivation of the interface primitive from the partial view
15436 -- to the full view. This is safe because all the interfaces
15437 -- must be visible in the partial view. Done to avoid adding
15438 -- a new interface derivation to the private part of the
15439 -- enclosing package; otherwise this new derivation would be
15440 -- decorated as hidden when the analysis of the enclosing
15441 -- package completes.
15443 if Is_Abstract_Type (Derived_Type)
15444 and then In_Private_Part (Current_Scope)
15445 and then Has_Private_Declaration (Derived_Type)
15446 then
15447 declare
15448 Partial_View : Entity_Id;
15449 Elmt : Elmt_Id;
15450 Ent : Entity_Id;
15452 begin
15453 Partial_View := First_Entity (Current_Scope);
15454 loop
15455 exit when No (Partial_View)
15456 or else (Has_Private_Declaration (Partial_View)
15457 and then
15458 Full_View (Partial_View) = Derived_Type);
15460 Next_Entity (Partial_View);
15461 end loop;
15463 -- If the partial view was not found then the source code
15464 -- has errors and the derivation is not needed.
15466 if Present (Partial_View) then
15467 Elmt :=
15468 First_Elmt (Primitive_Operations (Partial_View));
15469 while Present (Elmt) loop
15470 Ent := Node (Elmt);
15472 if Present (Alias (Ent))
15473 and then Ultimate_Alias (Ent) = Alias (Subp)
15474 then
15475 Append_Elmt
15476 (Ent, Primitive_Operations (Derived_Type));
15477 exit;
15478 end if;
15480 Next_Elmt (Elmt);
15481 end loop;
15483 -- If the interface primitive was not found in the
15484 -- partial view then this interface primitive was
15485 -- overridden. We add a derivation to activate in
15486 -- Derive_Progenitor_Subprograms the machinery to
15487 -- search for it.
15489 if No (Elmt) then
15490 Derive_Interface_Subprogram
15491 (New_Subp => New_Subp,
15492 Subp => Subp,
15493 Actual_Subp => Act_Subp);
15494 end if;
15495 end if;
15496 end;
15497 else
15498 Derive_Interface_Subprogram
15499 (New_Subp => New_Subp,
15500 Subp => Subp,
15501 Actual_Subp => Act_Subp);
15502 end if;
15504 -- Case 3: Common derivation
15506 else
15507 Derive_Subprogram
15508 (New_Subp => New_Subp,
15509 Parent_Subp => Subp,
15510 Derived_Type => Derived_Type,
15511 Parent_Type => Parent_Base,
15512 Actual_Subp => Act_Subp);
15513 end if;
15515 -- No need to update Act_Elm if we must search for the
15516 -- corresponding operation in the generic actual
15518 if not Need_Search
15519 and then Present (Act_Elmt)
15520 then
15521 Next_Elmt (Act_Elmt);
15522 Act_Subp := Node (Act_Elmt);
15523 end if;
15525 <<Continue>>
15526 Next_Elmt (Elmt);
15527 end loop;
15529 -- Inherit additional operations from progenitors. If the derived
15530 -- type is a generic actual, there are not new primitive operations
15531 -- for the type because it has those of the actual, and therefore
15532 -- nothing needs to be done. The renamings generated above are not
15533 -- primitive operations, and their purpose is simply to make the
15534 -- proper operations visible within an instantiation.
15536 if No (Generic_Actual) then
15537 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15538 end if;
15539 end if;
15541 -- Final check: Direct descendants must have their primitives in the
15542 -- same order. We exclude from this test untagged types and instances
15543 -- of formal derived types. We skip this test if we have already
15544 -- reported serious errors in the sources.
15546 pragma Assert (not Is_Tagged_Type (Derived_Type)
15547 or else Present (Generic_Actual)
15548 or else Serious_Errors_Detected > 0
15549 or else Check_Derived_Type);
15550 end Derive_Subprograms;
15552 --------------------------------
15553 -- Derived_Standard_Character --
15554 --------------------------------
15556 procedure Derived_Standard_Character
15557 (N : Node_Id;
15558 Parent_Type : Entity_Id;
15559 Derived_Type : Entity_Id)
15561 Loc : constant Source_Ptr := Sloc (N);
15562 Def : constant Node_Id := Type_Definition (N);
15563 Indic : constant Node_Id := Subtype_Indication (Def);
15564 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15565 Implicit_Base : constant Entity_Id :=
15566 Create_Itype
15567 (E_Enumeration_Type, N, Derived_Type, 'B');
15569 Lo : Node_Id;
15570 Hi : Node_Id;
15572 begin
15573 Discard_Node (Process_Subtype (Indic, N));
15575 Set_Etype (Implicit_Base, Parent_Base);
15576 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15577 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15579 Set_Is_Character_Type (Implicit_Base, True);
15580 Set_Has_Delayed_Freeze (Implicit_Base);
15582 -- The bounds of the implicit base are the bounds of the parent base.
15583 -- Note that their type is the parent base.
15585 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15586 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15588 Set_Scalar_Range (Implicit_Base,
15589 Make_Range (Loc,
15590 Low_Bound => Lo,
15591 High_Bound => Hi));
15593 Conditional_Delay (Derived_Type, Parent_Type);
15595 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15596 Set_Etype (Derived_Type, Implicit_Base);
15597 Set_Size_Info (Derived_Type, Parent_Type);
15599 if Unknown_RM_Size (Derived_Type) then
15600 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15601 end if;
15603 Set_Is_Character_Type (Derived_Type, True);
15605 if Nkind (Indic) /= N_Subtype_Indication then
15607 -- If no explicit constraint, the bounds are those
15608 -- of the parent type.
15610 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15611 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15612 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15613 end if;
15615 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15617 -- Because the implicit base is used in the conversion of the bounds, we
15618 -- have to freeze it now. This is similar to what is done for numeric
15619 -- types, and it equally suspicious, but otherwise a non-static bound
15620 -- will have a reference to an unfrozen type, which is rejected by Gigi
15621 -- (???). This requires specific care for definition of stream
15622 -- attributes. For details, see comments at the end of
15623 -- Build_Derived_Numeric_Type.
15625 Freeze_Before (N, Implicit_Base);
15626 end Derived_Standard_Character;
15628 ------------------------------
15629 -- Derived_Type_Declaration --
15630 ------------------------------
15632 procedure Derived_Type_Declaration
15633 (T : Entity_Id;
15634 N : Node_Id;
15635 Is_Completion : Boolean)
15637 Parent_Type : Entity_Id;
15639 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15640 -- Check whether the parent type is a generic formal, or derives
15641 -- directly or indirectly from one.
15643 ------------------------
15644 -- Comes_From_Generic --
15645 ------------------------
15647 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15648 begin
15649 if Is_Generic_Type (Typ) then
15650 return True;
15652 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15653 return True;
15655 elsif Is_Private_Type (Typ)
15656 and then Present (Full_View (Typ))
15657 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15658 then
15659 return True;
15661 elsif Is_Generic_Actual_Type (Typ) then
15662 return True;
15664 else
15665 return False;
15666 end if;
15667 end Comes_From_Generic;
15669 -- Local variables
15671 Def : constant Node_Id := Type_Definition (N);
15672 Iface_Def : Node_Id;
15673 Indic : constant Node_Id := Subtype_Indication (Def);
15674 Extension : constant Node_Id := Record_Extension_Part (Def);
15675 Parent_Node : Node_Id;
15676 Taggd : Boolean;
15678 -- Start of processing for Derived_Type_Declaration
15680 begin
15681 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15683 -- Ada 2005 (AI-251): In case of interface derivation check that the
15684 -- parent is also an interface.
15686 if Interface_Present (Def) then
15687 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15689 if not Is_Interface (Parent_Type) then
15690 Diagnose_Interface (Indic, Parent_Type);
15692 else
15693 Parent_Node := Parent (Base_Type (Parent_Type));
15694 Iface_Def := Type_Definition (Parent_Node);
15696 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15697 -- other limited interfaces.
15699 if Limited_Present (Def) then
15700 if Limited_Present (Iface_Def) then
15701 null;
15703 elsif Protected_Present (Iface_Def) then
15704 Error_Msg_NE
15705 ("descendant of & must be declared as a protected "
15706 & "interface", N, Parent_Type);
15708 elsif Synchronized_Present (Iface_Def) then
15709 Error_Msg_NE
15710 ("descendant of & must be declared as a synchronized "
15711 & "interface", N, Parent_Type);
15713 elsif Task_Present (Iface_Def) then
15714 Error_Msg_NE
15715 ("descendant of & must be declared as a task interface",
15716 N, Parent_Type);
15718 else
15719 Error_Msg_N
15720 ("(Ada 2005) limited interface cannot inherit from "
15721 & "non-limited interface", Indic);
15722 end if;
15724 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15725 -- from non-limited or limited interfaces.
15727 elsif not Protected_Present (Def)
15728 and then not Synchronized_Present (Def)
15729 and then not Task_Present (Def)
15730 then
15731 if Limited_Present (Iface_Def) then
15732 null;
15734 elsif Protected_Present (Iface_Def) then
15735 Error_Msg_NE
15736 ("descendant of & must be declared as a protected "
15737 & "interface", N, Parent_Type);
15739 elsif Synchronized_Present (Iface_Def) then
15740 Error_Msg_NE
15741 ("descendant of & must be declared as a synchronized "
15742 & "interface", N, Parent_Type);
15744 elsif Task_Present (Iface_Def) then
15745 Error_Msg_NE
15746 ("descendant of & must be declared as a task interface",
15747 N, Parent_Type);
15748 else
15749 null;
15750 end if;
15751 end if;
15752 end if;
15753 end if;
15755 if Is_Tagged_Type (Parent_Type)
15756 and then Is_Concurrent_Type (Parent_Type)
15757 and then not Is_Interface (Parent_Type)
15758 then
15759 Error_Msg_N
15760 ("parent type of a record extension cannot be a synchronized "
15761 & "tagged type (RM 3.9.1 (3/1))", N);
15762 Set_Etype (T, Any_Type);
15763 return;
15764 end if;
15766 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15767 -- interfaces
15769 if Is_Tagged_Type (Parent_Type)
15770 and then Is_Non_Empty_List (Interface_List (Def))
15771 then
15772 declare
15773 Intf : Node_Id;
15774 T : Entity_Id;
15776 begin
15777 Intf := First (Interface_List (Def));
15778 while Present (Intf) loop
15779 T := Find_Type_Of_Subtype_Indic (Intf);
15781 if not Is_Interface (T) then
15782 Diagnose_Interface (Intf, T);
15784 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15785 -- a limited type from having a nonlimited progenitor.
15787 elsif (Limited_Present (Def)
15788 or else (not Is_Interface (Parent_Type)
15789 and then Is_Limited_Type (Parent_Type)))
15790 and then not Is_Limited_Interface (T)
15791 then
15792 Error_Msg_NE
15793 ("progenitor interface& of limited type must be limited",
15794 N, T);
15795 end if;
15797 Next (Intf);
15798 end loop;
15799 end;
15800 end if;
15802 if Parent_Type = Any_Type
15803 or else Etype (Parent_Type) = Any_Type
15804 or else (Is_Class_Wide_Type (Parent_Type)
15805 and then Etype (Parent_Type) = T)
15806 then
15807 -- If Parent_Type is undefined or illegal, make new type into a
15808 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15809 -- errors. If this is a self-definition, emit error now.
15811 if T = Parent_Type or else T = Etype (Parent_Type) then
15812 Error_Msg_N ("type cannot be used in its own definition", Indic);
15813 end if;
15815 Set_Ekind (T, Ekind (Parent_Type));
15816 Set_Etype (T, Any_Type);
15817 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15819 if Is_Tagged_Type (T)
15820 and then Is_Record_Type (T)
15821 then
15822 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15823 end if;
15825 return;
15826 end if;
15828 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15829 -- an interface is special because the list of interfaces in the full
15830 -- view can be given in any order. For example:
15832 -- type A is interface;
15833 -- type B is interface and A;
15834 -- type D is new B with private;
15835 -- private
15836 -- type D is new A and B with null record; -- 1 --
15838 -- In this case we perform the following transformation of -1-:
15840 -- type D is new B and A with null record;
15842 -- If the parent of the full-view covers the parent of the partial-view
15843 -- we have two possible cases:
15845 -- 1) They have the same parent
15846 -- 2) The parent of the full-view implements some further interfaces
15848 -- In both cases we do not need to perform the transformation. In the
15849 -- first case the source program is correct and the transformation is
15850 -- not needed; in the second case the source program does not fulfill
15851 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15852 -- later.
15854 -- This transformation not only simplifies the rest of the analysis of
15855 -- this type declaration but also simplifies the correct generation of
15856 -- the object layout to the expander.
15858 if In_Private_Part (Current_Scope)
15859 and then Is_Interface (Parent_Type)
15860 then
15861 declare
15862 Iface : Node_Id;
15863 Partial_View : Entity_Id;
15864 Partial_View_Parent : Entity_Id;
15865 New_Iface : Node_Id;
15867 begin
15868 -- Look for the associated private type declaration
15870 Partial_View := First_Entity (Current_Scope);
15871 loop
15872 exit when No (Partial_View)
15873 or else (Has_Private_Declaration (Partial_View)
15874 and then Full_View (Partial_View) = T);
15876 Next_Entity (Partial_View);
15877 end loop;
15879 -- If the partial view was not found then the source code has
15880 -- errors and the transformation is not needed.
15882 if Present (Partial_View) then
15883 Partial_View_Parent := Etype (Partial_View);
15885 -- If the parent of the full-view covers the parent of the
15886 -- partial-view we have nothing else to do.
15888 if Interface_Present_In_Ancestor
15889 (Parent_Type, Partial_View_Parent)
15890 then
15891 null;
15893 -- Traverse the list of interfaces of the full-view to look
15894 -- for the parent of the partial-view and perform the tree
15895 -- transformation.
15897 else
15898 Iface := First (Interface_List (Def));
15899 while Present (Iface) loop
15900 if Etype (Iface) = Etype (Partial_View) then
15901 Rewrite (Subtype_Indication (Def),
15902 New_Copy (Subtype_Indication
15903 (Parent (Partial_View))));
15905 New_Iface :=
15906 Make_Identifier (Sloc (N), Chars (Parent_Type));
15907 Append (New_Iface, Interface_List (Def));
15909 -- Analyze the transformed code
15911 Derived_Type_Declaration (T, N, Is_Completion);
15912 return;
15913 end if;
15915 Next (Iface);
15916 end loop;
15917 end if;
15918 end if;
15919 end;
15920 end if;
15922 -- Only composite types other than array types are allowed to have
15923 -- discriminants.
15925 if Present (Discriminant_Specifications (N)) then
15926 if (Is_Elementary_Type (Parent_Type)
15927 or else
15928 Is_Array_Type (Parent_Type))
15929 and then not Error_Posted (N)
15930 then
15931 Error_Msg_N
15932 ("elementary or array type cannot have discriminants",
15933 Defining_Identifier (First (Discriminant_Specifications (N))));
15934 Set_Has_Discriminants (T, False);
15936 -- The type is allowed to have discriminants
15938 else
15939 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15940 end if;
15941 end if;
15943 -- In Ada 83, a derived type defined in a package specification cannot
15944 -- be used for further derivation until the end of its visible part.
15945 -- Note that derivation in the private part of the package is allowed.
15947 if Ada_Version = Ada_83
15948 and then Is_Derived_Type (Parent_Type)
15949 and then In_Visible_Part (Scope (Parent_Type))
15950 then
15951 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15952 Error_Msg_N
15953 ("(Ada 83): premature use of type for derivation", Indic);
15954 end if;
15955 end if;
15957 -- Check for early use of incomplete or private type
15959 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15960 Error_Msg_N ("premature derivation of incomplete type", Indic);
15961 return;
15963 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15964 and then not Comes_From_Generic (Parent_Type))
15965 or else Has_Private_Component (Parent_Type)
15966 then
15967 -- The ancestor type of a formal type can be incomplete, in which
15968 -- case only the operations of the partial view are available in the
15969 -- generic. Subsequent checks may be required when the full view is
15970 -- analyzed to verify that a derivation from a tagged type has an
15971 -- extension.
15973 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15974 null;
15976 elsif No (Underlying_Type (Parent_Type))
15977 or else Has_Private_Component (Parent_Type)
15978 then
15979 Error_Msg_N
15980 ("premature derivation of derived or private type", Indic);
15982 -- Flag the type itself as being in error, this prevents some
15983 -- nasty problems with subsequent uses of the malformed type.
15985 Set_Error_Posted (T);
15987 -- Check that within the immediate scope of an untagged partial
15988 -- view it's illegal to derive from the partial view if the
15989 -- full view is tagged. (7.3(7))
15991 -- We verify that the Parent_Type is a partial view by checking
15992 -- that it is not a Full_Type_Declaration (i.e. a private type or
15993 -- private extension declaration), to distinguish a partial view
15994 -- from a derivation from a private type which also appears as
15995 -- E_Private_Type. If the parent base type is not declared in an
15996 -- enclosing scope there is no need to check.
15998 elsif Present (Full_View (Parent_Type))
15999 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16000 and then not Is_Tagged_Type (Parent_Type)
16001 and then Is_Tagged_Type (Full_View (Parent_Type))
16002 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16003 then
16004 Error_Msg_N
16005 ("premature derivation from type with tagged full view",
16006 Indic);
16007 end if;
16008 end if;
16010 -- Check that form of derivation is appropriate
16012 Taggd := Is_Tagged_Type (Parent_Type);
16014 -- Set the parent type to the class-wide type's specific type in this
16015 -- case to prevent cascading errors
16017 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16018 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16019 Set_Etype (T, Etype (Parent_Type));
16020 return;
16021 end if;
16023 if Present (Extension) and then not Taggd then
16024 Error_Msg_N
16025 ("type derived from untagged type cannot have extension", Indic);
16027 elsif No (Extension) and then Taggd then
16029 -- If this declaration is within a private part (or body) of a
16030 -- generic instantiation then the derivation is allowed (the parent
16031 -- type can only appear tagged in this case if it's a generic actual
16032 -- type, since it would otherwise have been rejected in the analysis
16033 -- of the generic template).
16035 if not Is_Generic_Actual_Type (Parent_Type)
16036 or else In_Visible_Part (Scope (Parent_Type))
16037 then
16038 if Is_Class_Wide_Type (Parent_Type) then
16039 Error_Msg_N
16040 ("parent type must not be a class-wide type", Indic);
16042 -- Use specific type to prevent cascaded errors.
16044 Parent_Type := Etype (Parent_Type);
16046 else
16047 Error_Msg_N
16048 ("type derived from tagged type must have extension", Indic);
16049 end if;
16050 end if;
16051 end if;
16053 -- AI-443: Synchronized formal derived types require a private
16054 -- extension. There is no point in checking the ancestor type or
16055 -- the progenitors since the construct is wrong to begin with.
16057 if Ada_Version >= Ada_2005
16058 and then Is_Generic_Type (T)
16059 and then Present (Original_Node (N))
16060 then
16061 declare
16062 Decl : constant Node_Id := Original_Node (N);
16064 begin
16065 if Nkind (Decl) = N_Formal_Type_Declaration
16066 and then Nkind (Formal_Type_Definition (Decl)) =
16067 N_Formal_Derived_Type_Definition
16068 and then Synchronized_Present (Formal_Type_Definition (Decl))
16069 and then No (Extension)
16071 -- Avoid emitting a duplicate error message
16073 and then not Error_Posted (Indic)
16074 then
16075 Error_Msg_N
16076 ("synchronized derived type must have extension", N);
16077 end if;
16078 end;
16079 end if;
16081 if Null_Exclusion_Present (Def)
16082 and then not Is_Access_Type (Parent_Type)
16083 then
16084 Error_Msg_N ("null exclusion can only apply to an access type", N);
16085 end if;
16087 -- Avoid deriving parent primitives of underlying record views
16089 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16090 Derive_Subps => not Is_Underlying_Record_View (T));
16092 -- AI-419: The parent type of an explicitly limited derived type must
16093 -- be a limited type or a limited interface.
16095 if Limited_Present (Def) then
16096 Set_Is_Limited_Record (T);
16098 if Is_Interface (T) then
16099 Set_Is_Limited_Interface (T);
16100 end if;
16102 if not Is_Limited_Type (Parent_Type)
16103 and then
16104 (not Is_Interface (Parent_Type)
16105 or else not Is_Limited_Interface (Parent_Type))
16106 then
16107 -- AI05-0096: a derivation in the private part of an instance is
16108 -- legal if the generic formal is untagged limited, and the actual
16109 -- is non-limited.
16111 if Is_Generic_Actual_Type (Parent_Type)
16112 and then In_Private_Part (Current_Scope)
16113 and then
16114 not Is_Tagged_Type
16115 (Generic_Parent_Type (Parent (Parent_Type)))
16116 then
16117 null;
16119 else
16120 Error_Msg_NE
16121 ("parent type& of limited type must be limited",
16122 N, Parent_Type);
16123 end if;
16124 end if;
16125 end if;
16127 -- In SPARK, there are no derived type definitions other than type
16128 -- extensions of tagged record types.
16130 if No (Extension) then
16131 Check_SPARK_05_Restriction
16132 ("derived type is not allowed", Original_Node (N));
16133 end if;
16134 end Derived_Type_Declaration;
16136 ------------------------
16137 -- Diagnose_Interface --
16138 ------------------------
16140 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16141 begin
16142 if not Is_Interface (E) and then E /= Any_Type then
16143 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16144 end if;
16145 end Diagnose_Interface;
16147 ----------------------------------
16148 -- Enumeration_Type_Declaration --
16149 ----------------------------------
16151 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16152 Ev : Uint;
16153 L : Node_Id;
16154 R_Node : Node_Id;
16155 B_Node : Node_Id;
16157 begin
16158 -- Create identifier node representing lower bound
16160 B_Node := New_Node (N_Identifier, Sloc (Def));
16161 L := First (Literals (Def));
16162 Set_Chars (B_Node, Chars (L));
16163 Set_Entity (B_Node, L);
16164 Set_Etype (B_Node, T);
16165 Set_Is_Static_Expression (B_Node, True);
16167 R_Node := New_Node (N_Range, Sloc (Def));
16168 Set_Low_Bound (R_Node, B_Node);
16170 Set_Ekind (T, E_Enumeration_Type);
16171 Set_First_Literal (T, L);
16172 Set_Etype (T, T);
16173 Set_Is_Constrained (T);
16175 Ev := Uint_0;
16177 -- Loop through literals of enumeration type setting pos and rep values
16178 -- except that if the Ekind is already set, then it means the literal
16179 -- was already constructed (case of a derived type declaration and we
16180 -- should not disturb the Pos and Rep values.
16182 while Present (L) loop
16183 if Ekind (L) /= E_Enumeration_Literal then
16184 Set_Ekind (L, E_Enumeration_Literal);
16185 Set_Enumeration_Pos (L, Ev);
16186 Set_Enumeration_Rep (L, Ev);
16187 Set_Is_Known_Valid (L, True);
16188 end if;
16190 Set_Etype (L, T);
16191 New_Overloaded_Entity (L);
16192 Generate_Definition (L);
16193 Set_Convention (L, Convention_Intrinsic);
16195 -- Case of character literal
16197 if Nkind (L) = N_Defining_Character_Literal then
16198 Set_Is_Character_Type (T, True);
16200 -- Check violation of No_Wide_Characters
16202 if Restriction_Check_Required (No_Wide_Characters) then
16203 Get_Name_String (Chars (L));
16205 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16206 Check_Restriction (No_Wide_Characters, L);
16207 end if;
16208 end if;
16209 end if;
16211 Ev := Ev + 1;
16212 Next (L);
16213 end loop;
16215 -- Now create a node representing upper bound
16217 B_Node := New_Node (N_Identifier, Sloc (Def));
16218 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16219 Set_Entity (B_Node, Last (Literals (Def)));
16220 Set_Etype (B_Node, T);
16221 Set_Is_Static_Expression (B_Node, True);
16223 Set_High_Bound (R_Node, B_Node);
16225 -- Initialize various fields of the type. Some of this information
16226 -- may be overwritten later through rep.clauses.
16228 Set_Scalar_Range (T, R_Node);
16229 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16230 Set_Enum_Esize (T);
16231 Set_Enum_Pos_To_Rep (T, Empty);
16233 -- Set Discard_Names if configuration pragma set, or if there is
16234 -- a parameterless pragma in the current declarative region
16236 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16237 Set_Discard_Names (T);
16238 end if;
16240 -- Process end label if there is one
16242 if Present (Def) then
16243 Process_End_Label (Def, 'e', T);
16244 end if;
16245 end Enumeration_Type_Declaration;
16247 ---------------------------------
16248 -- Expand_To_Stored_Constraint --
16249 ---------------------------------
16251 function Expand_To_Stored_Constraint
16252 (Typ : Entity_Id;
16253 Constraint : Elist_Id) return Elist_Id
16255 Explicitly_Discriminated_Type : Entity_Id;
16256 Expansion : Elist_Id;
16257 Discriminant : Entity_Id;
16259 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16260 -- Find the nearest type that actually specifies discriminants
16262 ---------------------------------
16263 -- Type_With_Explicit_Discrims --
16264 ---------------------------------
16266 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16267 Typ : constant E := Base_Type (Id);
16269 begin
16270 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16271 if Present (Full_View (Typ)) then
16272 return Type_With_Explicit_Discrims (Full_View (Typ));
16273 end if;
16275 else
16276 if Has_Discriminants (Typ) then
16277 return Typ;
16278 end if;
16279 end if;
16281 if Etype (Typ) = Typ then
16282 return Empty;
16283 elsif Has_Discriminants (Typ) then
16284 return Typ;
16285 else
16286 return Type_With_Explicit_Discrims (Etype (Typ));
16287 end if;
16289 end Type_With_Explicit_Discrims;
16291 -- Start of processing for Expand_To_Stored_Constraint
16293 begin
16294 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16295 return No_Elist;
16296 end if;
16298 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16300 if No (Explicitly_Discriminated_Type) then
16301 return No_Elist;
16302 end if;
16304 Expansion := New_Elmt_List;
16306 Discriminant :=
16307 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16308 while Present (Discriminant) loop
16309 Append_Elmt
16310 (Get_Discriminant_Value
16311 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16312 To => Expansion);
16313 Next_Stored_Discriminant (Discriminant);
16314 end loop;
16316 return Expansion;
16317 end Expand_To_Stored_Constraint;
16319 ---------------------------
16320 -- Find_Hidden_Interface --
16321 ---------------------------
16323 function Find_Hidden_Interface
16324 (Src : Elist_Id;
16325 Dest : Elist_Id) return Entity_Id
16327 Iface : Entity_Id;
16328 Iface_Elmt : Elmt_Id;
16330 begin
16331 if Present (Src) and then Present (Dest) then
16332 Iface_Elmt := First_Elmt (Src);
16333 while Present (Iface_Elmt) loop
16334 Iface := Node (Iface_Elmt);
16336 if Is_Interface (Iface)
16337 and then not Contain_Interface (Iface, Dest)
16338 then
16339 return Iface;
16340 end if;
16342 Next_Elmt (Iface_Elmt);
16343 end loop;
16344 end if;
16346 return Empty;
16347 end Find_Hidden_Interface;
16349 --------------------
16350 -- Find_Type_Name --
16351 --------------------
16353 function Find_Type_Name (N : Node_Id) return Entity_Id is
16354 Id : constant Entity_Id := Defining_Identifier (N);
16355 Prev : Entity_Id;
16356 New_Id : Entity_Id;
16357 Prev_Par : Node_Id;
16359 procedure Check_Duplicate_Aspects;
16360 -- Check that aspects specified in a completion have not been specified
16361 -- already in the partial view. Type_Invariant and others can be
16362 -- specified on either view but never on both.
16364 procedure Tag_Mismatch;
16365 -- Diagnose a tagged partial view whose full view is untagged.
16366 -- We post the message on the full view, with a reference to
16367 -- the previous partial view. The partial view can be private
16368 -- or incomplete, and these are handled in a different manner,
16369 -- so we determine the position of the error message from the
16370 -- respective slocs of both.
16372 -----------------------------
16373 -- Check_Duplicate_Aspects --
16374 -----------------------------
16376 procedure Check_Duplicate_Aspects is
16377 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16378 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16379 F_Spec, P_Spec : Node_Id;
16381 begin
16382 if Present (Full_Aspects) then
16383 F_Spec := First (Full_Aspects);
16384 while Present (F_Spec) loop
16385 if Present (Prev_Aspects) then
16386 P_Spec := First (Prev_Aspects);
16387 while Present (P_Spec) loop
16388 if Chars (Identifier (P_Spec)) =
16389 Chars (Identifier (F_Spec))
16390 then
16391 Error_Msg_N
16392 ("aspect already specified in private declaration",
16393 F_Spec);
16394 Remove (F_Spec);
16395 return;
16396 end if;
16398 Next (P_Spec);
16399 end loop;
16400 end if;
16402 if Has_Discriminants (Prev)
16403 and then not Has_Unknown_Discriminants (Prev)
16404 and then Chars (Identifier (F_Spec)) =
16405 Name_Implicit_Dereference
16406 then
16407 Error_Msg_N ("cannot specify aspect " &
16408 "if partial view has known discriminants", F_Spec);
16409 end if;
16411 Next (F_Spec);
16412 end loop;
16413 end if;
16414 end Check_Duplicate_Aspects;
16416 ------------------
16417 -- Tag_Mismatch --
16418 ------------------
16420 procedure Tag_Mismatch is
16421 begin
16422 if Sloc (Prev) < Sloc (Id) then
16423 if Ada_Version >= Ada_2012
16424 and then Nkind (N) = N_Private_Type_Declaration
16425 then
16426 Error_Msg_NE
16427 ("declaration of private } must be a tagged type ", Id, Prev);
16428 else
16429 Error_Msg_NE
16430 ("full declaration of } must be a tagged type ", Id, Prev);
16431 end if;
16433 else
16434 if Ada_Version >= Ada_2012
16435 and then Nkind (N) = N_Private_Type_Declaration
16436 then
16437 Error_Msg_NE
16438 ("declaration of private } must be a tagged type ", Prev, Id);
16439 else
16440 Error_Msg_NE
16441 ("full declaration of } must be a tagged type ", Prev, Id);
16442 end if;
16443 end if;
16444 end Tag_Mismatch;
16446 -- Start of processing for Find_Type_Name
16448 begin
16449 -- Find incomplete declaration, if one was given
16451 Prev := Current_Entity_In_Scope (Id);
16453 -- New type declaration
16455 if No (Prev) then
16456 Enter_Name (Id);
16457 return Id;
16459 -- Previous declaration exists
16461 else
16462 Prev_Par := Parent (Prev);
16464 -- Error if not incomplete/private case except if previous
16465 -- declaration is implicit, etc. Enter_Name will emit error if
16466 -- appropriate.
16468 if not Is_Incomplete_Or_Private_Type (Prev) then
16469 Enter_Name (Id);
16470 New_Id := Id;
16472 -- Check invalid completion of private or incomplete type
16474 elsif not Nkind_In (N, N_Full_Type_Declaration,
16475 N_Task_Type_Declaration,
16476 N_Protected_Type_Declaration)
16477 and then
16478 (Ada_Version < Ada_2012
16479 or else not Is_Incomplete_Type (Prev)
16480 or else not Nkind_In (N, N_Private_Type_Declaration,
16481 N_Private_Extension_Declaration))
16482 then
16483 -- Completion must be a full type declarations (RM 7.3(4))
16485 Error_Msg_Sloc := Sloc (Prev);
16486 Error_Msg_NE ("invalid completion of }", Id, Prev);
16488 -- Set scope of Id to avoid cascaded errors. Entity is never
16489 -- examined again, except when saving globals in generics.
16491 Set_Scope (Id, Current_Scope);
16492 New_Id := Id;
16494 -- If this is a repeated incomplete declaration, no further
16495 -- checks are possible.
16497 if Nkind (N) = N_Incomplete_Type_Declaration then
16498 return Prev;
16499 end if;
16501 -- Case of full declaration of incomplete type
16503 elsif Ekind (Prev) = E_Incomplete_Type
16504 and then (Ada_Version < Ada_2012
16505 or else No (Full_View (Prev))
16506 or else not Is_Private_Type (Full_View (Prev)))
16507 then
16508 -- Indicate that the incomplete declaration has a matching full
16509 -- declaration. The defining occurrence of the incomplete
16510 -- declaration remains the visible one, and the procedure
16511 -- Get_Full_View dereferences it whenever the type is used.
16513 if Present (Full_View (Prev)) then
16514 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16515 end if;
16517 Set_Full_View (Prev, Id);
16518 Append_Entity (Id, Current_Scope);
16519 Set_Is_Public (Id, Is_Public (Prev));
16520 Set_Is_Internal (Id);
16521 New_Id := Prev;
16523 -- If the incomplete view is tagged, a class_wide type has been
16524 -- created already. Use it for the private type as well, in order
16525 -- to prevent multiple incompatible class-wide types that may be
16526 -- created for self-referential anonymous access components.
16528 if Is_Tagged_Type (Prev)
16529 and then Present (Class_Wide_Type (Prev))
16530 then
16531 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16532 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16534 -- The type of the classwide type is the current Id. Previously
16535 -- this was not done for private declarations because of order-
16536 -- of elaboration issues in the back-end, but gigi now handles
16537 -- this properly.
16539 Set_Etype (Class_Wide_Type (Id), Id);
16540 end if;
16542 -- Case of full declaration of private type
16544 else
16545 -- If the private type was a completion of an incomplete type then
16546 -- update Prev to reference the private type
16548 if Ada_Version >= Ada_2012
16549 and then Ekind (Prev) = E_Incomplete_Type
16550 and then Present (Full_View (Prev))
16551 and then Is_Private_Type (Full_View (Prev))
16552 then
16553 Prev := Full_View (Prev);
16554 Prev_Par := Parent (Prev);
16555 end if;
16557 if Nkind (N) = N_Full_Type_Declaration
16558 and then Nkind_In
16559 (Type_Definition (N), N_Record_Definition,
16560 N_Derived_Type_Definition)
16561 and then Interface_Present (Type_Definition (N))
16562 then
16563 Error_Msg_N
16564 ("completion of private type cannot be an interface", N);
16565 end if;
16567 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16568 if Etype (Prev) /= Prev then
16570 -- Prev is a private subtype or a derived type, and needs
16571 -- no completion.
16573 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16574 New_Id := Id;
16576 elsif Ekind (Prev) = E_Private_Type
16577 and then Nkind_In (N, N_Task_Type_Declaration,
16578 N_Protected_Type_Declaration)
16579 then
16580 Error_Msg_N
16581 ("completion of nonlimited type cannot be limited", N);
16583 elsif Ekind (Prev) = E_Record_Type_With_Private
16584 and then Nkind_In (N, N_Task_Type_Declaration,
16585 N_Protected_Type_Declaration)
16586 then
16587 if not Is_Limited_Record (Prev) then
16588 Error_Msg_N
16589 ("completion of nonlimited type cannot be limited", N);
16591 elsif No (Interface_List (N)) then
16592 Error_Msg_N
16593 ("completion of tagged private type must be tagged",
16595 end if;
16596 end if;
16598 -- Ada 2005 (AI-251): Private extension declaration of a task
16599 -- type or a protected type. This case arises when covering
16600 -- interface types.
16602 elsif Nkind_In (N, N_Task_Type_Declaration,
16603 N_Protected_Type_Declaration)
16604 then
16605 null;
16607 elsif Nkind (N) /= N_Full_Type_Declaration
16608 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16609 then
16610 Error_Msg_N
16611 ("full view of private extension must be an extension", N);
16613 elsif not (Abstract_Present (Parent (Prev)))
16614 and then Abstract_Present (Type_Definition (N))
16615 then
16616 Error_Msg_N
16617 ("full view of non-abstract extension cannot be abstract", N);
16618 end if;
16620 if not In_Private_Part (Current_Scope) then
16621 Error_Msg_N
16622 ("declaration of full view must appear in private part", N);
16623 end if;
16625 if Ada_Version >= Ada_2012 then
16626 Check_Duplicate_Aspects;
16627 end if;
16629 Copy_And_Swap (Prev, Id);
16630 Set_Has_Private_Declaration (Prev);
16631 Set_Has_Private_Declaration (Id);
16633 -- AI12-0133: Indicate whether we have a partial view with
16634 -- unknown discriminants, in which case initialization of objects
16635 -- of the type do not receive an invariant check.
16637 Set_Partial_View_Has_Unknown_Discr
16638 (Prev, Has_Unknown_Discriminants (Id));
16640 -- Preserve aspect and iterator flags that may have been set on
16641 -- the partial view.
16643 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16644 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16646 -- If no error, propagate freeze_node from private to full view.
16647 -- It may have been generated for an early operational item.
16649 if Present (Freeze_Node (Id))
16650 and then Serious_Errors_Detected = 0
16651 and then No (Full_View (Id))
16652 then
16653 Set_Freeze_Node (Prev, Freeze_Node (Id));
16654 Set_Freeze_Node (Id, Empty);
16655 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16656 end if;
16658 Set_Full_View (Id, Prev);
16659 New_Id := Prev;
16660 end if;
16662 -- Verify that full declaration conforms to partial one
16664 if Is_Incomplete_Or_Private_Type (Prev)
16665 and then Present (Discriminant_Specifications (Prev_Par))
16666 then
16667 if Present (Discriminant_Specifications (N)) then
16668 if Ekind (Prev) = E_Incomplete_Type then
16669 Check_Discriminant_Conformance (N, Prev, Prev);
16670 else
16671 Check_Discriminant_Conformance (N, Prev, Id);
16672 end if;
16674 else
16675 Error_Msg_N
16676 ("missing discriminants in full type declaration", N);
16678 -- To avoid cascaded errors on subsequent use, share the
16679 -- discriminants of the partial view.
16681 Set_Discriminant_Specifications (N,
16682 Discriminant_Specifications (Prev_Par));
16683 end if;
16684 end if;
16686 -- A prior untagged partial view can have an associated class-wide
16687 -- type due to use of the class attribute, and in this case the full
16688 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16689 -- of incomplete tagged declarations, but we check for it.
16691 if Is_Type (Prev)
16692 and then (Is_Tagged_Type (Prev)
16693 or else Present (Class_Wide_Type (Prev)))
16694 then
16695 -- Ada 2012 (AI05-0162): A private type may be the completion of
16696 -- an incomplete type.
16698 if Ada_Version >= Ada_2012
16699 and then Is_Incomplete_Type (Prev)
16700 and then Nkind_In (N, N_Private_Type_Declaration,
16701 N_Private_Extension_Declaration)
16702 then
16703 -- No need to check private extensions since they are tagged
16705 if Nkind (N) = N_Private_Type_Declaration
16706 and then not Tagged_Present (N)
16707 then
16708 Tag_Mismatch;
16709 end if;
16711 -- The full declaration is either a tagged type (including
16712 -- a synchronized type that implements interfaces) or a
16713 -- type extension, otherwise this is an error.
16715 elsif Nkind_In (N, N_Task_Type_Declaration,
16716 N_Protected_Type_Declaration)
16717 then
16718 if No (Interface_List (N)) and then not Error_Posted (N) then
16719 Tag_Mismatch;
16720 end if;
16722 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16724 -- Indicate that the previous declaration (tagged incomplete
16725 -- or private declaration) requires the same on the full one.
16727 if not Tagged_Present (Type_Definition (N)) then
16728 Tag_Mismatch;
16729 Set_Is_Tagged_Type (Id);
16730 end if;
16732 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16733 if No (Record_Extension_Part (Type_Definition (N))) then
16734 Error_Msg_NE
16735 ("full declaration of } must be a record extension",
16736 Prev, Id);
16738 -- Set some attributes to produce a usable full view
16740 Set_Is_Tagged_Type (Id);
16741 end if;
16743 else
16744 Tag_Mismatch;
16745 end if;
16746 end if;
16748 if Present (Prev)
16749 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16750 and then Present (Premature_Use (Parent (Prev)))
16751 then
16752 Error_Msg_Sloc := Sloc (N);
16753 Error_Msg_N
16754 ("\full declaration #", Premature_Use (Parent (Prev)));
16755 end if;
16757 return New_Id;
16758 end if;
16759 end Find_Type_Name;
16761 -------------------------
16762 -- Find_Type_Of_Object --
16763 -------------------------
16765 function Find_Type_Of_Object
16766 (Obj_Def : Node_Id;
16767 Related_Nod : Node_Id) return Entity_Id
16769 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16770 P : Node_Id := Parent (Obj_Def);
16771 T : Entity_Id;
16772 Nam : Name_Id;
16774 begin
16775 -- If the parent is a component_definition node we climb to the
16776 -- component_declaration node
16778 if Nkind (P) = N_Component_Definition then
16779 P := Parent (P);
16780 end if;
16782 -- Case of an anonymous array subtype
16784 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16785 N_Unconstrained_Array_Definition)
16786 then
16787 T := Empty;
16788 Array_Type_Declaration (T, Obj_Def);
16790 -- Create an explicit subtype whenever possible
16792 elsif Nkind (P) /= N_Component_Declaration
16793 and then Def_Kind = N_Subtype_Indication
16794 then
16795 -- Base name of subtype on object name, which will be unique in
16796 -- the current scope.
16798 -- If this is a duplicate declaration, return base type, to avoid
16799 -- generating duplicate anonymous types.
16801 if Error_Posted (P) then
16802 Analyze (Subtype_Mark (Obj_Def));
16803 return Entity (Subtype_Mark (Obj_Def));
16804 end if;
16806 Nam :=
16807 New_External_Name
16808 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16810 T := Make_Defining_Identifier (Sloc (P), Nam);
16812 Insert_Action (Obj_Def,
16813 Make_Subtype_Declaration (Sloc (P),
16814 Defining_Identifier => T,
16815 Subtype_Indication => Relocate_Node (Obj_Def)));
16817 -- This subtype may need freezing, and this will not be done
16818 -- automatically if the object declaration is not in declarative
16819 -- part. Since this is an object declaration, the type cannot always
16820 -- be frozen here. Deferred constants do not freeze their type
16821 -- (which often enough will be private).
16823 if Nkind (P) = N_Object_Declaration
16824 and then Constant_Present (P)
16825 and then No (Expression (P))
16826 then
16827 null;
16829 -- Here we freeze the base type of object type to catch premature use
16830 -- of discriminated private type without a full view.
16832 else
16833 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16834 end if;
16836 -- Ada 2005 AI-406: the object definition in an object declaration
16837 -- can be an access definition.
16839 elsif Def_Kind = N_Access_Definition then
16840 T := Access_Definition (Related_Nod, Obj_Def);
16842 Set_Is_Local_Anonymous_Access
16844 V => (Ada_Version < Ada_2012)
16845 or else (Nkind (P) /= N_Object_Declaration)
16846 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16848 -- Otherwise, the object definition is just a subtype_mark
16850 else
16851 T := Process_Subtype (Obj_Def, Related_Nod);
16853 -- If expansion is disabled an object definition that is an aggregate
16854 -- will not get expanded and may lead to scoping problems in the back
16855 -- end, if the object is referenced in an inner scope. In that case
16856 -- create an itype reference for the object definition now. This
16857 -- may be redundant in some cases, but harmless.
16859 if Is_Itype (T)
16860 and then Nkind (Related_Nod) = N_Object_Declaration
16861 and then ASIS_Mode
16862 then
16863 Build_Itype_Reference (T, Related_Nod);
16864 end if;
16865 end if;
16867 return T;
16868 end Find_Type_Of_Object;
16870 --------------------------------
16871 -- Find_Type_Of_Subtype_Indic --
16872 --------------------------------
16874 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16875 Typ : Entity_Id;
16877 begin
16878 -- Case of subtype mark with a constraint
16880 if Nkind (S) = N_Subtype_Indication then
16881 Find_Type (Subtype_Mark (S));
16882 Typ := Entity (Subtype_Mark (S));
16884 if not
16885 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16886 then
16887 Error_Msg_N
16888 ("incorrect constraint for this kind of type", Constraint (S));
16889 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16890 end if;
16892 -- Otherwise we have a subtype mark without a constraint
16894 elsif Error_Posted (S) then
16895 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16896 return Any_Type;
16898 else
16899 Find_Type (S);
16900 Typ := Entity (S);
16901 end if;
16903 -- Check No_Wide_Characters restriction
16905 Check_Wide_Character_Restriction (Typ, S);
16907 return Typ;
16908 end Find_Type_Of_Subtype_Indic;
16910 -------------------------------------
16911 -- Floating_Point_Type_Declaration --
16912 -------------------------------------
16914 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16915 Digs : constant Node_Id := Digits_Expression (Def);
16916 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16917 Digs_Val : Uint;
16918 Base_Typ : Entity_Id;
16919 Implicit_Base : Entity_Id;
16920 Bound : Node_Id;
16922 function Can_Derive_From (E : Entity_Id) return Boolean;
16923 -- Find if given digits value, and possibly a specified range, allows
16924 -- derivation from specified type
16926 function Find_Base_Type return Entity_Id;
16927 -- Find a predefined base type that Def can derive from, or generate
16928 -- an error and substitute Long_Long_Float if none exists.
16930 ---------------------
16931 -- Can_Derive_From --
16932 ---------------------
16934 function Can_Derive_From (E : Entity_Id) return Boolean is
16935 Spec : constant Entity_Id := Real_Range_Specification (Def);
16937 begin
16938 -- Check specified "digits" constraint
16940 if Digs_Val > Digits_Value (E) then
16941 return False;
16942 end if;
16944 -- Check for matching range, if specified
16946 if Present (Spec) then
16947 if Expr_Value_R (Type_Low_Bound (E)) >
16948 Expr_Value_R (Low_Bound (Spec))
16949 then
16950 return False;
16951 end if;
16953 if Expr_Value_R (Type_High_Bound (E)) <
16954 Expr_Value_R (High_Bound (Spec))
16955 then
16956 return False;
16957 end if;
16958 end if;
16960 return True;
16961 end Can_Derive_From;
16963 --------------------
16964 -- Find_Base_Type --
16965 --------------------
16967 function Find_Base_Type return Entity_Id is
16968 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16970 begin
16971 -- Iterate over the predefined types in order, returning the first
16972 -- one that Def can derive from.
16974 while Present (Choice) loop
16975 if Can_Derive_From (Node (Choice)) then
16976 return Node (Choice);
16977 end if;
16979 Next_Elmt (Choice);
16980 end loop;
16982 -- If we can't derive from any existing type, use Long_Long_Float
16983 -- and give appropriate message explaining the problem.
16985 if Digs_Val > Max_Digs_Val then
16986 -- It might be the case that there is a type with the requested
16987 -- range, just not the combination of digits and range.
16989 Error_Msg_N
16990 ("no predefined type has requested range and precision",
16991 Real_Range_Specification (Def));
16993 else
16994 Error_Msg_N
16995 ("range too large for any predefined type",
16996 Real_Range_Specification (Def));
16997 end if;
16999 return Standard_Long_Long_Float;
17000 end Find_Base_Type;
17002 -- Start of processing for Floating_Point_Type_Declaration
17004 begin
17005 Check_Restriction (No_Floating_Point, Def);
17007 -- Create an implicit base type
17009 Implicit_Base :=
17010 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17012 -- Analyze and verify digits value
17014 Analyze_And_Resolve (Digs, Any_Integer);
17015 Check_Digits_Expression (Digs);
17016 Digs_Val := Expr_Value (Digs);
17018 -- Process possible range spec and find correct type to derive from
17020 Process_Real_Range_Specification (Def);
17022 -- Check that requested number of digits is not too high.
17024 if Digs_Val > Max_Digs_Val then
17026 -- The check for Max_Base_Digits may be somewhat expensive, as it
17027 -- requires reading System, so only do it when necessary.
17029 declare
17030 Max_Base_Digits : constant Uint :=
17031 Expr_Value
17032 (Expression
17033 (Parent (RTE (RE_Max_Base_Digits))));
17035 begin
17036 if Digs_Val > Max_Base_Digits then
17037 Error_Msg_Uint_1 := Max_Base_Digits;
17038 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17040 elsif No (Real_Range_Specification (Def)) then
17041 Error_Msg_Uint_1 := Max_Digs_Val;
17042 Error_Msg_N ("types with more than ^ digits need range spec "
17043 & "(RM 3.5.7(6))", Digs);
17044 end if;
17045 end;
17046 end if;
17048 -- Find a suitable type to derive from or complain and use a substitute
17050 Base_Typ := Find_Base_Type;
17052 -- If there are bounds given in the declaration use them as the bounds
17053 -- of the type, otherwise use the bounds of the predefined base type
17054 -- that was chosen based on the Digits value.
17056 if Present (Real_Range_Specification (Def)) then
17057 Set_Scalar_Range (T, Real_Range_Specification (Def));
17058 Set_Is_Constrained (T);
17060 -- The bounds of this range must be converted to machine numbers
17061 -- in accordance with RM 4.9(38).
17063 Bound := Type_Low_Bound (T);
17065 if Nkind (Bound) = N_Real_Literal then
17066 Set_Realval
17067 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17068 Set_Is_Machine_Number (Bound);
17069 end if;
17071 Bound := Type_High_Bound (T);
17073 if Nkind (Bound) = N_Real_Literal then
17074 Set_Realval
17075 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17076 Set_Is_Machine_Number (Bound);
17077 end if;
17079 else
17080 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17081 end if;
17083 -- Complete definition of implicit base and declared first subtype. The
17084 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17085 -- are not clobbered when the floating point type acts as a full view of
17086 -- a private type.
17088 Set_Etype (Implicit_Base, Base_Typ);
17089 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17090 Set_Size_Info (Implicit_Base, Base_Typ);
17091 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17092 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17093 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17094 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17096 Set_Ekind (T, E_Floating_Point_Subtype);
17097 Set_Etype (T, Implicit_Base);
17098 Set_Size_Info (T, Implicit_Base);
17099 Set_RM_Size (T, RM_Size (Implicit_Base));
17100 Inherit_Rep_Item_Chain (T, Implicit_Base);
17101 Set_Digits_Value (T, Digs_Val);
17102 end Floating_Point_Type_Declaration;
17104 ----------------------------
17105 -- Get_Discriminant_Value --
17106 ----------------------------
17108 -- This is the situation:
17110 -- There is a non-derived type
17112 -- type T0 (Dx, Dy, Dz...)
17114 -- There are zero or more levels of derivation, with each derivation
17115 -- either purely inheriting the discriminants, or defining its own.
17117 -- type Ti is new Ti-1
17118 -- or
17119 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17120 -- or
17121 -- subtype Ti is ...
17123 -- The subtype issue is avoided by the use of Original_Record_Component,
17124 -- and the fact that derived subtypes also derive the constraints.
17126 -- This chain leads back from
17128 -- Typ_For_Constraint
17130 -- Typ_For_Constraint has discriminants, and the value for each
17131 -- discriminant is given by its corresponding Elmt of Constraints.
17133 -- Discriminant is some discriminant in this hierarchy
17135 -- We need to return its value
17137 -- We do this by recursively searching each level, and looking for
17138 -- Discriminant. Once we get to the bottom, we start backing up
17139 -- returning the value for it which may in turn be a discriminant
17140 -- further up, so on the backup we continue the substitution.
17142 function Get_Discriminant_Value
17143 (Discriminant : Entity_Id;
17144 Typ_For_Constraint : Entity_Id;
17145 Constraint : Elist_Id) return Node_Id
17147 function Root_Corresponding_Discriminant
17148 (Discr : Entity_Id) return Entity_Id;
17149 -- Given a discriminant, traverse the chain of inherited discriminants
17150 -- and return the topmost discriminant.
17152 function Search_Derivation_Levels
17153 (Ti : Entity_Id;
17154 Discrim_Values : Elist_Id;
17155 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17156 -- This is the routine that performs the recursive search of levels
17157 -- as described above.
17159 -------------------------------------
17160 -- Root_Corresponding_Discriminant --
17161 -------------------------------------
17163 function Root_Corresponding_Discriminant
17164 (Discr : Entity_Id) return Entity_Id
17166 D : Entity_Id;
17168 begin
17169 D := Discr;
17170 while Present (Corresponding_Discriminant (D)) loop
17171 D := Corresponding_Discriminant (D);
17172 end loop;
17174 return D;
17175 end Root_Corresponding_Discriminant;
17177 ------------------------------
17178 -- Search_Derivation_Levels --
17179 ------------------------------
17181 function Search_Derivation_Levels
17182 (Ti : Entity_Id;
17183 Discrim_Values : Elist_Id;
17184 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17186 Assoc : Elmt_Id;
17187 Disc : Entity_Id;
17188 Result : Node_Or_Entity_Id;
17189 Result_Entity : Node_Id;
17191 begin
17192 -- If inappropriate type, return Error, this happens only in
17193 -- cascaded error situations, and we want to avoid a blow up.
17195 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17196 return Error;
17197 end if;
17199 -- Look deeper if possible. Use Stored_Constraints only for
17200 -- untagged types. For tagged types use the given constraint.
17201 -- This asymmetry needs explanation???
17203 if not Stored_Discrim_Values
17204 and then Present (Stored_Constraint (Ti))
17205 and then not Is_Tagged_Type (Ti)
17206 then
17207 Result :=
17208 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17209 else
17210 declare
17211 Td : constant Entity_Id := Etype (Ti);
17213 begin
17214 if Td = Ti then
17215 Result := Discriminant;
17217 else
17218 if Present (Stored_Constraint (Ti)) then
17219 Result :=
17220 Search_Derivation_Levels
17221 (Td, Stored_Constraint (Ti), True);
17222 else
17223 Result :=
17224 Search_Derivation_Levels
17225 (Td, Discrim_Values, Stored_Discrim_Values);
17226 end if;
17227 end if;
17228 end;
17229 end if;
17231 -- Extra underlying places to search, if not found above. For
17232 -- concurrent types, the relevant discriminant appears in the
17233 -- corresponding record. For a type derived from a private type
17234 -- without discriminant, the full view inherits the discriminants
17235 -- of the full view of the parent.
17237 if Result = Discriminant then
17238 if Is_Concurrent_Type (Ti)
17239 and then Present (Corresponding_Record_Type (Ti))
17240 then
17241 Result :=
17242 Search_Derivation_Levels (
17243 Corresponding_Record_Type (Ti),
17244 Discrim_Values,
17245 Stored_Discrim_Values);
17247 elsif Is_Private_Type (Ti)
17248 and then not Has_Discriminants (Ti)
17249 and then Present (Full_View (Ti))
17250 and then Etype (Full_View (Ti)) /= Ti
17251 then
17252 Result :=
17253 Search_Derivation_Levels (
17254 Full_View (Ti),
17255 Discrim_Values,
17256 Stored_Discrim_Values);
17257 end if;
17258 end if;
17260 -- If Result is not a (reference to a) discriminant, return it,
17261 -- otherwise set Result_Entity to the discriminant.
17263 if Nkind (Result) = N_Defining_Identifier then
17264 pragma Assert (Result = Discriminant);
17265 Result_Entity := Result;
17267 else
17268 if not Denotes_Discriminant (Result) then
17269 return Result;
17270 end if;
17272 Result_Entity := Entity (Result);
17273 end if;
17275 -- See if this level of derivation actually has discriminants because
17276 -- tagged derivations can add them, hence the lower levels need not
17277 -- have any.
17279 if not Has_Discriminants (Ti) then
17280 return Result;
17281 end if;
17283 -- Scan Ti's discriminants for Result_Entity, and return its
17284 -- corresponding value, if any.
17286 Result_Entity := Original_Record_Component (Result_Entity);
17288 Assoc := First_Elmt (Discrim_Values);
17290 if Stored_Discrim_Values then
17291 Disc := First_Stored_Discriminant (Ti);
17292 else
17293 Disc := First_Discriminant (Ti);
17294 end if;
17296 while Present (Disc) loop
17297 pragma Assert (Present (Assoc));
17299 if Original_Record_Component (Disc) = Result_Entity then
17300 return Node (Assoc);
17301 end if;
17303 Next_Elmt (Assoc);
17305 if Stored_Discrim_Values then
17306 Next_Stored_Discriminant (Disc);
17307 else
17308 Next_Discriminant (Disc);
17309 end if;
17310 end loop;
17312 -- Could not find it
17314 return Result;
17315 end Search_Derivation_Levels;
17317 -- Local Variables
17319 Result : Node_Or_Entity_Id;
17321 -- Start of processing for Get_Discriminant_Value
17323 begin
17324 -- ??? This routine is a gigantic mess and will be deleted. For the
17325 -- time being just test for the trivial case before calling recurse.
17327 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17328 declare
17329 D : Entity_Id;
17330 E : Elmt_Id;
17332 begin
17333 D := First_Discriminant (Typ_For_Constraint);
17334 E := First_Elmt (Constraint);
17335 while Present (D) loop
17336 if Chars (D) = Chars (Discriminant) then
17337 return Node (E);
17338 end if;
17340 Next_Discriminant (D);
17341 Next_Elmt (E);
17342 end loop;
17343 end;
17344 end if;
17346 Result := Search_Derivation_Levels
17347 (Typ_For_Constraint, Constraint, False);
17349 -- ??? hack to disappear when this routine is gone
17351 if Nkind (Result) = N_Defining_Identifier then
17352 declare
17353 D : Entity_Id;
17354 E : Elmt_Id;
17356 begin
17357 D := First_Discriminant (Typ_For_Constraint);
17358 E := First_Elmt (Constraint);
17359 while Present (D) loop
17360 if Root_Corresponding_Discriminant (D) = Discriminant then
17361 return Node (E);
17362 end if;
17364 Next_Discriminant (D);
17365 Next_Elmt (E);
17366 end loop;
17367 end;
17368 end if;
17370 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17371 return Result;
17372 end Get_Discriminant_Value;
17374 --------------------------
17375 -- Has_Range_Constraint --
17376 --------------------------
17378 function Has_Range_Constraint (N : Node_Id) return Boolean is
17379 C : constant Node_Id := Constraint (N);
17381 begin
17382 if Nkind (C) = N_Range_Constraint then
17383 return True;
17385 elsif Nkind (C) = N_Digits_Constraint then
17386 return
17387 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17388 or else Present (Range_Constraint (C));
17390 elsif Nkind (C) = N_Delta_Constraint then
17391 return Present (Range_Constraint (C));
17393 else
17394 return False;
17395 end if;
17396 end Has_Range_Constraint;
17398 ------------------------
17399 -- Inherit_Components --
17400 ------------------------
17402 function Inherit_Components
17403 (N : Node_Id;
17404 Parent_Base : Entity_Id;
17405 Derived_Base : Entity_Id;
17406 Is_Tagged : Boolean;
17407 Inherit_Discr : Boolean;
17408 Discs : Elist_Id) return Elist_Id
17410 Assoc_List : constant Elist_Id := New_Elmt_List;
17412 procedure Inherit_Component
17413 (Old_C : Entity_Id;
17414 Plain_Discrim : Boolean := False;
17415 Stored_Discrim : Boolean := False);
17416 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17417 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17418 -- True, Old_C is a stored discriminant. If they are both false then
17419 -- Old_C is a regular component.
17421 -----------------------
17422 -- Inherit_Component --
17423 -----------------------
17425 procedure Inherit_Component
17426 (Old_C : Entity_Id;
17427 Plain_Discrim : Boolean := False;
17428 Stored_Discrim : Boolean := False)
17430 procedure Set_Anonymous_Type (Id : Entity_Id);
17431 -- Id denotes the entity of an access discriminant or anonymous
17432 -- access component. Set the type of Id to either the same type of
17433 -- Old_C or create a new one depending on whether the parent and
17434 -- the child types are in the same scope.
17436 ------------------------
17437 -- Set_Anonymous_Type --
17438 ------------------------
17440 procedure Set_Anonymous_Type (Id : Entity_Id) is
17441 Old_Typ : constant Entity_Id := Etype (Old_C);
17443 begin
17444 if Scope (Parent_Base) = Scope (Derived_Base) then
17445 Set_Etype (Id, Old_Typ);
17447 -- The parent and the derived type are in two different scopes.
17448 -- Reuse the type of the original discriminant / component by
17449 -- copying it in order to preserve all attributes.
17451 else
17452 declare
17453 Typ : constant Entity_Id := New_Copy (Old_Typ);
17455 begin
17456 Set_Etype (Id, Typ);
17458 -- Since we do not generate component declarations for
17459 -- inherited components, associate the itype with the
17460 -- derived type.
17462 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17463 Set_Scope (Typ, Derived_Base);
17464 end;
17465 end if;
17466 end Set_Anonymous_Type;
17468 -- Local variables and constants
17470 New_C : constant Entity_Id := New_Copy (Old_C);
17472 Corr_Discrim : Entity_Id;
17473 Discrim : Entity_Id;
17475 -- Start of processing for Inherit_Component
17477 begin
17478 pragma Assert (not Is_Tagged or not Stored_Discrim);
17480 Set_Parent (New_C, Parent (Old_C));
17482 -- Regular discriminants and components must be inserted in the scope
17483 -- of the Derived_Base. Do it here.
17485 if not Stored_Discrim then
17486 Enter_Name (New_C);
17487 end if;
17489 -- For tagged types the Original_Record_Component must point to
17490 -- whatever this field was pointing to in the parent type. This has
17491 -- already been achieved by the call to New_Copy above.
17493 if not Is_Tagged then
17494 Set_Original_Record_Component (New_C, New_C);
17495 end if;
17497 -- Set the proper type of an access discriminant
17499 if Ekind (New_C) = E_Discriminant
17500 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17501 then
17502 Set_Anonymous_Type (New_C);
17503 end if;
17505 -- If we have inherited a component then see if its Etype contains
17506 -- references to Parent_Base discriminants. In this case, replace
17507 -- these references with the constraints given in Discs. We do not
17508 -- do this for the partial view of private types because this is
17509 -- not needed (only the components of the full view will be used
17510 -- for code generation) and cause problem. We also avoid this
17511 -- transformation in some error situations.
17513 if Ekind (New_C) = E_Component then
17515 -- Set the proper type of an anonymous access component
17517 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17518 Set_Anonymous_Type (New_C);
17520 elsif (Is_Private_Type (Derived_Base)
17521 and then not Is_Generic_Type (Derived_Base))
17522 or else (Is_Empty_Elmt_List (Discs)
17523 and then not Expander_Active)
17524 then
17525 Set_Etype (New_C, Etype (Old_C));
17527 else
17528 -- The current component introduces a circularity of the
17529 -- following kind:
17531 -- limited with Pack_2;
17532 -- package Pack_1 is
17533 -- type T_1 is tagged record
17534 -- Comp : access Pack_2.T_2;
17535 -- ...
17536 -- end record;
17537 -- end Pack_1;
17539 -- with Pack_1;
17540 -- package Pack_2 is
17541 -- type T_2 is new Pack_1.T_1 with ...;
17542 -- end Pack_2;
17544 Set_Etype
17545 (New_C,
17546 Constrain_Component_Type
17547 (Old_C, Derived_Base, N, Parent_Base, Discs));
17548 end if;
17549 end if;
17551 -- In derived tagged types it is illegal to reference a non
17552 -- discriminant component in the parent type. To catch this, mark
17553 -- these components with an Ekind of E_Void. This will be reset in
17554 -- Record_Type_Definition after processing the record extension of
17555 -- the derived type.
17557 -- If the declaration is a private extension, there is no further
17558 -- record extension to process, and the components retain their
17559 -- current kind, because they are visible at this point.
17561 if Is_Tagged and then Ekind (New_C) = E_Component
17562 and then Nkind (N) /= N_Private_Extension_Declaration
17563 then
17564 Set_Ekind (New_C, E_Void);
17565 end if;
17567 if Plain_Discrim then
17568 Set_Corresponding_Discriminant (New_C, Old_C);
17569 Build_Discriminal (New_C);
17571 -- If we are explicitly inheriting a stored discriminant it will be
17572 -- completely hidden.
17574 elsif Stored_Discrim then
17575 Set_Corresponding_Discriminant (New_C, Empty);
17576 Set_Discriminal (New_C, Empty);
17577 Set_Is_Completely_Hidden (New_C);
17579 -- Set the Original_Record_Component of each discriminant in the
17580 -- derived base to point to the corresponding stored that we just
17581 -- created.
17583 Discrim := First_Discriminant (Derived_Base);
17584 while Present (Discrim) loop
17585 Corr_Discrim := Corresponding_Discriminant (Discrim);
17587 -- Corr_Discrim could be missing in an error situation
17589 if Present (Corr_Discrim)
17590 and then Original_Record_Component (Corr_Discrim) = Old_C
17591 then
17592 Set_Original_Record_Component (Discrim, New_C);
17593 end if;
17595 Next_Discriminant (Discrim);
17596 end loop;
17598 Append_Entity (New_C, Derived_Base);
17599 end if;
17601 if not Is_Tagged then
17602 Append_Elmt (Old_C, Assoc_List);
17603 Append_Elmt (New_C, Assoc_List);
17604 end if;
17605 end Inherit_Component;
17607 -- Variables local to Inherit_Component
17609 Loc : constant Source_Ptr := Sloc (N);
17611 Parent_Discrim : Entity_Id;
17612 Stored_Discrim : Entity_Id;
17613 D : Entity_Id;
17614 Component : Entity_Id;
17616 -- Start of processing for Inherit_Components
17618 begin
17619 if not Is_Tagged then
17620 Append_Elmt (Parent_Base, Assoc_List);
17621 Append_Elmt (Derived_Base, Assoc_List);
17622 end if;
17624 -- Inherit parent discriminants if needed
17626 if Inherit_Discr then
17627 Parent_Discrim := First_Discriminant (Parent_Base);
17628 while Present (Parent_Discrim) loop
17629 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17630 Next_Discriminant (Parent_Discrim);
17631 end loop;
17632 end if;
17634 -- Create explicit stored discrims for untagged types when necessary
17636 if not Has_Unknown_Discriminants (Derived_Base)
17637 and then Has_Discriminants (Parent_Base)
17638 and then not Is_Tagged
17639 and then
17640 (not Inherit_Discr
17641 or else First_Discriminant (Parent_Base) /=
17642 First_Stored_Discriminant (Parent_Base))
17643 then
17644 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17645 while Present (Stored_Discrim) loop
17646 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17647 Next_Stored_Discriminant (Stored_Discrim);
17648 end loop;
17649 end if;
17651 -- See if we can apply the second transformation for derived types, as
17652 -- explained in point 6. in the comments above Build_Derived_Record_Type
17653 -- This is achieved by appending Derived_Base discriminants into Discs,
17654 -- which has the side effect of returning a non empty Discs list to the
17655 -- caller of Inherit_Components, which is what we want. This must be
17656 -- done for private derived types if there are explicit stored
17657 -- discriminants, to ensure that we can retrieve the values of the
17658 -- constraints provided in the ancestors.
17660 if Inherit_Discr
17661 and then Is_Empty_Elmt_List (Discs)
17662 and then Present (First_Discriminant (Derived_Base))
17663 and then
17664 (not Is_Private_Type (Derived_Base)
17665 or else Is_Completely_Hidden
17666 (First_Stored_Discriminant (Derived_Base))
17667 or else Is_Generic_Type (Derived_Base))
17668 then
17669 D := First_Discriminant (Derived_Base);
17670 while Present (D) loop
17671 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17672 Next_Discriminant (D);
17673 end loop;
17674 end if;
17676 -- Finally, inherit non-discriminant components unless they are not
17677 -- visible because defined or inherited from the full view of the
17678 -- parent. Don't inherit the _parent field of the parent type.
17680 Component := First_Entity (Parent_Base);
17681 while Present (Component) loop
17683 -- Ada 2005 (AI-251): Do not inherit components associated with
17684 -- secondary tags of the parent.
17686 if Ekind (Component) = E_Component
17687 and then Present (Related_Type (Component))
17688 then
17689 null;
17691 elsif Ekind (Component) /= E_Component
17692 or else Chars (Component) = Name_uParent
17693 then
17694 null;
17696 -- If the derived type is within the parent type's declarative
17697 -- region, then the components can still be inherited even though
17698 -- they aren't visible at this point. This can occur for cases
17699 -- such as within public child units where the components must
17700 -- become visible upon entering the child unit's private part.
17702 elsif not Is_Visible_Component (Component)
17703 and then not In_Open_Scopes (Scope (Parent_Base))
17704 then
17705 null;
17707 elsif Ekind_In (Derived_Base, E_Private_Type,
17708 E_Limited_Private_Type)
17709 then
17710 null;
17712 else
17713 Inherit_Component (Component);
17714 end if;
17716 Next_Entity (Component);
17717 end loop;
17719 -- For tagged derived types, inherited discriminants cannot be used in
17720 -- component declarations of the record extension part. To achieve this
17721 -- we mark the inherited discriminants as not visible.
17723 if Is_Tagged and then Inherit_Discr then
17724 D := First_Discriminant (Derived_Base);
17725 while Present (D) loop
17726 Set_Is_Immediately_Visible (D, False);
17727 Next_Discriminant (D);
17728 end loop;
17729 end if;
17731 return Assoc_List;
17732 end Inherit_Components;
17734 -----------------------------
17735 -- Inherit_Predicate_Flags --
17736 -----------------------------
17738 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17739 begin
17740 Set_Has_Predicates (Subt, Has_Predicates (Par));
17741 Set_Has_Static_Predicate_Aspect
17742 (Subt, Has_Static_Predicate_Aspect (Par));
17743 Set_Has_Dynamic_Predicate_Aspect
17744 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17745 end Inherit_Predicate_Flags;
17747 ----------------------
17748 -- Is_EVF_Procedure --
17749 ----------------------
17751 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17752 Formal : Entity_Id;
17754 begin
17755 -- Examine the formals of an Extensions_Visible False procedure looking
17756 -- for a controlling OUT parameter.
17758 if Ekind (Subp) = E_Procedure
17759 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17760 then
17761 Formal := First_Formal (Subp);
17762 while Present (Formal) loop
17763 if Ekind (Formal) = E_Out_Parameter
17764 and then Is_Controlling_Formal (Formal)
17765 then
17766 return True;
17767 end if;
17769 Next_Formal (Formal);
17770 end loop;
17771 end if;
17773 return False;
17774 end Is_EVF_Procedure;
17776 -----------------------
17777 -- Is_Null_Extension --
17778 -----------------------
17780 function Is_Null_Extension (T : Entity_Id) return Boolean is
17781 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17782 Comp_List : Node_Id;
17783 Comp : Node_Id;
17785 begin
17786 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17787 or else not Is_Tagged_Type (T)
17788 or else Nkind (Type_Definition (Type_Decl)) /=
17789 N_Derived_Type_Definition
17790 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17791 then
17792 return False;
17793 end if;
17795 Comp_List :=
17796 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17798 if Present (Discriminant_Specifications (Type_Decl)) then
17799 return False;
17801 elsif Present (Comp_List)
17802 and then Is_Non_Empty_List (Component_Items (Comp_List))
17803 then
17804 Comp := First (Component_Items (Comp_List));
17806 -- Only user-defined components are relevant. The component list
17807 -- may also contain a parent component and internal components
17808 -- corresponding to secondary tags, but these do not determine
17809 -- whether this is a null extension.
17811 while Present (Comp) loop
17812 if Comes_From_Source (Comp) then
17813 return False;
17814 end if;
17816 Next (Comp);
17817 end loop;
17819 return True;
17821 else
17822 return True;
17823 end if;
17824 end Is_Null_Extension;
17826 ------------------------------
17827 -- Is_Valid_Constraint_Kind --
17828 ------------------------------
17830 function Is_Valid_Constraint_Kind
17831 (T_Kind : Type_Kind;
17832 Constraint_Kind : Node_Kind) return Boolean
17834 begin
17835 case T_Kind is
17836 when Enumeration_Kind |
17837 Integer_Kind =>
17838 return Constraint_Kind = N_Range_Constraint;
17840 when Decimal_Fixed_Point_Kind =>
17841 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17842 N_Range_Constraint);
17844 when Ordinary_Fixed_Point_Kind =>
17845 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17846 N_Range_Constraint);
17848 when Float_Kind =>
17849 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17850 N_Range_Constraint);
17852 when Access_Kind |
17853 Array_Kind |
17854 E_Record_Type |
17855 E_Record_Subtype |
17856 Class_Wide_Kind |
17857 E_Incomplete_Type |
17858 Private_Kind |
17859 Concurrent_Kind =>
17860 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17862 when others =>
17863 return True; -- Error will be detected later
17864 end case;
17865 end Is_Valid_Constraint_Kind;
17867 --------------------------
17868 -- Is_Visible_Component --
17869 --------------------------
17871 function Is_Visible_Component
17872 (C : Entity_Id;
17873 N : Node_Id := Empty) return Boolean
17875 Original_Comp : Entity_Id := Empty;
17876 Original_Type : Entity_Id;
17877 Type_Scope : Entity_Id;
17879 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17880 -- Check whether parent type of inherited component is declared locally,
17881 -- possibly within a nested package or instance. The current scope is
17882 -- the derived record itself.
17884 -------------------
17885 -- Is_Local_Type --
17886 -------------------
17888 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17889 Scop : Entity_Id;
17891 begin
17892 Scop := Scope (Typ);
17893 while Present (Scop)
17894 and then Scop /= Standard_Standard
17895 loop
17896 if Scop = Scope (Current_Scope) then
17897 return True;
17898 end if;
17900 Scop := Scope (Scop);
17901 end loop;
17903 return False;
17904 end Is_Local_Type;
17906 -- Start of processing for Is_Visible_Component
17908 begin
17909 if Ekind_In (C, E_Component, E_Discriminant) then
17910 Original_Comp := Original_Record_Component (C);
17911 end if;
17913 if No (Original_Comp) then
17915 -- Premature usage, or previous error
17917 return False;
17919 else
17920 Original_Type := Scope (Original_Comp);
17921 Type_Scope := Scope (Base_Type (Scope (C)));
17922 end if;
17924 -- This test only concerns tagged types
17926 if not Is_Tagged_Type (Original_Type) then
17927 return True;
17929 -- If it is _Parent or _Tag, there is no visibility issue
17931 elsif not Comes_From_Source (Original_Comp) then
17932 return True;
17934 -- Discriminants are visible unless the (private) type has unknown
17935 -- discriminants. If the discriminant reference is inserted for a
17936 -- discriminant check on a full view it is also visible.
17938 elsif Ekind (Original_Comp) = E_Discriminant
17939 and then
17940 (not Has_Unknown_Discriminants (Original_Type)
17941 or else (Present (N)
17942 and then Nkind (N) = N_Selected_Component
17943 and then Nkind (Prefix (N)) = N_Type_Conversion
17944 and then not Comes_From_Source (Prefix (N))))
17945 then
17946 return True;
17948 -- In the body of an instantiation, no need to check for the visibility
17949 -- of a component.
17951 elsif In_Instance_Body then
17952 return True;
17954 -- If the component has been declared in an ancestor which is currently
17955 -- a private type, then it is not visible. The same applies if the
17956 -- component's containing type is not in an open scope and the original
17957 -- component's enclosing type is a visible full view of a private type
17958 -- (which can occur in cases where an attempt is being made to reference
17959 -- a component in a sibling package that is inherited from a visible
17960 -- component of a type in an ancestor package; the component in the
17961 -- sibling package should not be visible even though the component it
17962 -- inherited from is visible). This does not apply however in the case
17963 -- where the scope of the type is a private child unit, or when the
17964 -- parent comes from a local package in which the ancestor is currently
17965 -- visible. The latter suppression of visibility is needed for cases
17966 -- that are tested in B730006.
17968 elsif Is_Private_Type (Original_Type)
17969 or else
17970 (not Is_Private_Descendant (Type_Scope)
17971 and then not In_Open_Scopes (Type_Scope)
17972 and then Has_Private_Declaration (Original_Type))
17973 then
17974 -- If the type derives from an entity in a formal package, there
17975 -- are no additional visible components.
17977 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17978 N_Formal_Package_Declaration
17979 then
17980 return False;
17982 -- if we are not in the private part of the current package, there
17983 -- are no additional visible components.
17985 elsif Ekind (Scope (Current_Scope)) = E_Package
17986 and then not In_Private_Part (Scope (Current_Scope))
17987 then
17988 return False;
17989 else
17990 return
17991 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17992 and then In_Open_Scopes (Scope (Original_Type))
17993 and then Is_Local_Type (Type_Scope);
17994 end if;
17996 -- There is another weird way in which a component may be invisible when
17997 -- the private and the full view are not derived from the same ancestor.
17998 -- Here is an example :
18000 -- type A1 is tagged record F1 : integer; end record;
18001 -- type A2 is new A1 with record F2 : integer; end record;
18002 -- type T is new A1 with private;
18003 -- private
18004 -- type T is new A2 with null record;
18006 -- In this case, the full view of T inherits F1 and F2 but the private
18007 -- view inherits only F1
18009 else
18010 declare
18011 Ancestor : Entity_Id := Scope (C);
18013 begin
18014 loop
18015 if Ancestor = Original_Type then
18016 return True;
18018 -- The ancestor may have a partial view of the original type,
18019 -- but if the full view is in scope, as in a child body, the
18020 -- component is visible.
18022 elsif In_Private_Part (Scope (Original_Type))
18023 and then Full_View (Ancestor) = Original_Type
18024 then
18025 return True;
18027 elsif Ancestor = Etype (Ancestor) then
18029 -- No further ancestors to examine
18031 return False;
18032 end if;
18034 Ancestor := Etype (Ancestor);
18035 end loop;
18036 end;
18037 end if;
18038 end Is_Visible_Component;
18040 --------------------------
18041 -- Make_Class_Wide_Type --
18042 --------------------------
18044 procedure Make_Class_Wide_Type (T : Entity_Id) is
18045 CW_Type : Entity_Id;
18046 CW_Name : Name_Id;
18047 Next_E : Entity_Id;
18049 begin
18050 if Present (Class_Wide_Type (T)) then
18052 -- The class-wide type is a partially decorated entity created for a
18053 -- unanalyzed tagged type referenced through a limited with clause.
18054 -- When the tagged type is analyzed, its class-wide type needs to be
18055 -- redecorated. Note that we reuse the entity created by Decorate_
18056 -- Tagged_Type in order to preserve all links.
18058 if Materialize_Entity (Class_Wide_Type (T)) then
18059 CW_Type := Class_Wide_Type (T);
18060 Set_Materialize_Entity (CW_Type, False);
18062 -- The class wide type can have been defined by the partial view, in
18063 -- which case everything is already done.
18065 else
18066 return;
18067 end if;
18069 -- Default case, we need to create a new class-wide type
18071 else
18072 CW_Type :=
18073 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18074 end if;
18076 -- Inherit root type characteristics
18078 CW_Name := Chars (CW_Type);
18079 Next_E := Next_Entity (CW_Type);
18080 Copy_Node (T, CW_Type);
18081 Set_Comes_From_Source (CW_Type, False);
18082 Set_Chars (CW_Type, CW_Name);
18083 Set_Parent (CW_Type, Parent (T));
18084 Set_Next_Entity (CW_Type, Next_E);
18086 -- Ensure we have a new freeze node for the class-wide type. The partial
18087 -- view may have freeze action of its own, requiring a proper freeze
18088 -- node, and the same freeze node cannot be shared between the two
18089 -- types.
18091 Set_Has_Delayed_Freeze (CW_Type);
18092 Set_Freeze_Node (CW_Type, Empty);
18094 -- Customize the class-wide type: It has no prim. op., it cannot be
18095 -- abstract and its Etype points back to the specific root type.
18097 Set_Ekind (CW_Type, E_Class_Wide_Type);
18098 Set_Is_Tagged_Type (CW_Type, True);
18099 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18100 Set_Is_Abstract_Type (CW_Type, False);
18101 Set_Is_Constrained (CW_Type, False);
18102 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18103 Set_Default_SSO (CW_Type);
18105 if Ekind (T) = E_Class_Wide_Subtype then
18106 Set_Etype (CW_Type, Etype (Base_Type (T)));
18107 else
18108 Set_Etype (CW_Type, T);
18109 end if;
18111 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18113 -- If this is the class_wide type of a constrained subtype, it does
18114 -- not have discriminants.
18116 Set_Has_Discriminants (CW_Type,
18117 Has_Discriminants (T) and then not Is_Constrained (T));
18119 Set_Has_Unknown_Discriminants (CW_Type, True);
18120 Set_Class_Wide_Type (T, CW_Type);
18121 Set_Equivalent_Type (CW_Type, Empty);
18123 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18125 Set_Class_Wide_Type (CW_Type, CW_Type);
18127 -- Inherit the "ghostness" from the root tagged type
18129 if Ghost_Mode > None or else Is_Ghost_Entity (T) then
18130 Set_Is_Ghost_Entity (CW_Type);
18131 end if;
18132 end Make_Class_Wide_Type;
18134 ----------------
18135 -- Make_Index --
18136 ----------------
18138 procedure Make_Index
18139 (N : Node_Id;
18140 Related_Nod : Node_Id;
18141 Related_Id : Entity_Id := Empty;
18142 Suffix_Index : Nat := 1;
18143 In_Iter_Schm : Boolean := False)
18145 R : Node_Id;
18146 T : Entity_Id;
18147 Def_Id : Entity_Id := Empty;
18148 Found : Boolean := False;
18150 begin
18151 -- For a discrete range used in a constrained array definition and
18152 -- defined by a range, an implicit conversion to the predefined type
18153 -- INTEGER is assumed if each bound is either a numeric literal, a named
18154 -- number, or an attribute, and the type of both bounds (prior to the
18155 -- implicit conversion) is the type universal_integer. Otherwise, both
18156 -- bounds must be of the same discrete type, other than universal
18157 -- integer; this type must be determinable independently of the
18158 -- context, but using the fact that the type must be discrete and that
18159 -- both bounds must have the same type.
18161 -- Character literals also have a universal type in the absence of
18162 -- of additional context, and are resolved to Standard_Character.
18164 if Nkind (N) = N_Range then
18166 -- The index is given by a range constraint. The bounds are known
18167 -- to be of a consistent type.
18169 if not Is_Overloaded (N) then
18170 T := Etype (N);
18172 -- For universal bounds, choose the specific predefined type
18174 if T = Universal_Integer then
18175 T := Standard_Integer;
18177 elsif T = Any_Character then
18178 Ambiguous_Character (Low_Bound (N));
18180 T := Standard_Character;
18181 end if;
18183 -- The node may be overloaded because some user-defined operators
18184 -- are available, but if a universal interpretation exists it is
18185 -- also the selected one.
18187 elsif Universal_Interpretation (N) = Universal_Integer then
18188 T := Standard_Integer;
18190 else
18191 T := Any_Type;
18193 declare
18194 Ind : Interp_Index;
18195 It : Interp;
18197 begin
18198 Get_First_Interp (N, Ind, It);
18199 while Present (It.Typ) loop
18200 if Is_Discrete_Type (It.Typ) then
18202 if Found
18203 and then not Covers (It.Typ, T)
18204 and then not Covers (T, It.Typ)
18205 then
18206 Error_Msg_N ("ambiguous bounds in discrete range", N);
18207 exit;
18208 else
18209 T := It.Typ;
18210 Found := True;
18211 end if;
18212 end if;
18214 Get_Next_Interp (Ind, It);
18215 end loop;
18217 if T = Any_Type then
18218 Error_Msg_N ("discrete type required for range", N);
18219 Set_Etype (N, Any_Type);
18220 return;
18222 elsif T = Universal_Integer then
18223 T := Standard_Integer;
18224 end if;
18225 end;
18226 end if;
18228 if not Is_Discrete_Type (T) then
18229 Error_Msg_N ("discrete type required for range", N);
18230 Set_Etype (N, Any_Type);
18231 return;
18232 end if;
18234 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18235 and then Attribute_Name (Low_Bound (N)) = Name_First
18236 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18237 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18238 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18239 then
18240 -- The type of the index will be the type of the prefix, as long
18241 -- as the upper bound is 'Last of the same type.
18243 Def_Id := Entity (Prefix (Low_Bound (N)));
18245 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18246 or else Attribute_Name (High_Bound (N)) /= Name_Last
18247 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18248 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18249 then
18250 Def_Id := Empty;
18251 end if;
18252 end if;
18254 R := N;
18255 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18257 elsif Nkind (N) = N_Subtype_Indication then
18259 -- The index is given by a subtype with a range constraint
18261 T := Base_Type (Entity (Subtype_Mark (N)));
18263 if not Is_Discrete_Type (T) then
18264 Error_Msg_N ("discrete type required for range", N);
18265 Set_Etype (N, Any_Type);
18266 return;
18267 end if;
18269 R := Range_Expression (Constraint (N));
18271 Resolve (R, T);
18272 Process_Range_Expr_In_Decl
18273 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18275 elsif Nkind (N) = N_Attribute_Reference then
18277 -- Catch beginner's error (use of attribute other than 'Range)
18279 if Attribute_Name (N) /= Name_Range then
18280 Error_Msg_N ("expect attribute ''Range", N);
18281 Set_Etype (N, Any_Type);
18282 return;
18283 end if;
18285 -- If the node denotes the range of a type mark, that is also the
18286 -- resulting type, and we do not need to create an Itype for it.
18288 if Is_Entity_Name (Prefix (N))
18289 and then Comes_From_Source (N)
18290 and then Is_Type (Entity (Prefix (N)))
18291 and then Is_Discrete_Type (Entity (Prefix (N)))
18292 then
18293 Def_Id := Entity (Prefix (N));
18294 end if;
18296 Analyze_And_Resolve (N);
18297 T := Etype (N);
18298 R := N;
18300 -- If none of the above, must be a subtype. We convert this to a
18301 -- range attribute reference because in the case of declared first
18302 -- named subtypes, the types in the range reference can be different
18303 -- from the type of the entity. A range attribute normalizes the
18304 -- reference and obtains the correct types for the bounds.
18306 -- This transformation is in the nature of an expansion, is only
18307 -- done if expansion is active. In particular, it is not done on
18308 -- formal generic types, because we need to retain the name of the
18309 -- original index for instantiation purposes.
18311 else
18312 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18313 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18314 Set_Etype (N, Any_Integer);
18315 return;
18317 else
18318 -- The type mark may be that of an incomplete type. It is only
18319 -- now that we can get the full view, previous analysis does
18320 -- not look specifically for a type mark.
18322 Set_Entity (N, Get_Full_View (Entity (N)));
18323 Set_Etype (N, Entity (N));
18324 Def_Id := Entity (N);
18326 if not Is_Discrete_Type (Def_Id) then
18327 Error_Msg_N ("discrete type required for index", N);
18328 Set_Etype (N, Any_Type);
18329 return;
18330 end if;
18331 end if;
18333 if Expander_Active then
18334 Rewrite (N,
18335 Make_Attribute_Reference (Sloc (N),
18336 Attribute_Name => Name_Range,
18337 Prefix => Relocate_Node (N)));
18339 -- The original was a subtype mark that does not freeze. This
18340 -- means that the rewritten version must not freeze either.
18342 Set_Must_Not_Freeze (N);
18343 Set_Must_Not_Freeze (Prefix (N));
18344 Analyze_And_Resolve (N);
18345 T := Etype (N);
18346 R := N;
18348 -- If expander is inactive, type is legal, nothing else to construct
18350 else
18351 return;
18352 end if;
18353 end if;
18355 if not Is_Discrete_Type (T) then
18356 Error_Msg_N ("discrete type required for range", N);
18357 Set_Etype (N, Any_Type);
18358 return;
18360 elsif T = Any_Type then
18361 Set_Etype (N, Any_Type);
18362 return;
18363 end if;
18365 -- We will now create the appropriate Itype to describe the range, but
18366 -- first a check. If we originally had a subtype, then we just label
18367 -- the range with this subtype. Not only is there no need to construct
18368 -- a new subtype, but it is wrong to do so for two reasons:
18370 -- 1. A legality concern, if we have a subtype, it must not freeze,
18371 -- and the Itype would cause freezing incorrectly
18373 -- 2. An efficiency concern, if we created an Itype, it would not be
18374 -- recognized as the same type for the purposes of eliminating
18375 -- checks in some circumstances.
18377 -- We signal this case by setting the subtype entity in Def_Id
18379 if No (Def_Id) then
18380 Def_Id :=
18381 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18382 Set_Etype (Def_Id, Base_Type (T));
18384 if Is_Signed_Integer_Type (T) then
18385 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18387 elsif Is_Modular_Integer_Type (T) then
18388 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18390 else
18391 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18392 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18393 Set_First_Literal (Def_Id, First_Literal (T));
18394 end if;
18396 Set_Size_Info (Def_Id, (T));
18397 Set_RM_Size (Def_Id, RM_Size (T));
18398 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18400 Set_Scalar_Range (Def_Id, R);
18401 Conditional_Delay (Def_Id, T);
18403 if Nkind (N) = N_Subtype_Indication then
18404 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18405 end if;
18407 -- In the subtype indication case, if the immediate parent of the
18408 -- new subtype is non-static, then the subtype we create is non-
18409 -- static, even if its bounds are static.
18411 if Nkind (N) = N_Subtype_Indication
18412 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18413 then
18414 Set_Is_Non_Static_Subtype (Def_Id);
18415 end if;
18416 end if;
18418 -- Final step is to label the index with this constructed type
18420 Set_Etype (N, Def_Id);
18421 end Make_Index;
18423 ------------------------------
18424 -- Modular_Type_Declaration --
18425 ------------------------------
18427 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18428 Mod_Expr : constant Node_Id := Expression (Def);
18429 M_Val : Uint;
18431 procedure Set_Modular_Size (Bits : Int);
18432 -- Sets RM_Size to Bits, and Esize to normal word size above this
18434 ----------------------
18435 -- Set_Modular_Size --
18436 ----------------------
18438 procedure Set_Modular_Size (Bits : Int) is
18439 begin
18440 Set_RM_Size (T, UI_From_Int (Bits));
18442 if Bits <= 8 then
18443 Init_Esize (T, 8);
18445 elsif Bits <= 16 then
18446 Init_Esize (T, 16);
18448 elsif Bits <= 32 then
18449 Init_Esize (T, 32);
18451 else
18452 Init_Esize (T, System_Max_Binary_Modulus_Power);
18453 end if;
18455 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18456 Set_Is_Known_Valid (T);
18457 end if;
18458 end Set_Modular_Size;
18460 -- Start of processing for Modular_Type_Declaration
18462 begin
18463 -- If the mod expression is (exactly) 2 * literal, where literal is
18464 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18466 if Warn_On_Suspicious_Modulus_Value
18467 and then Nkind (Mod_Expr) = N_Op_Multiply
18468 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18469 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18470 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18471 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18472 then
18473 Error_Msg_N
18474 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18475 end if;
18477 -- Proceed with analysis of mod expression
18479 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18480 Set_Etype (T, T);
18481 Set_Ekind (T, E_Modular_Integer_Type);
18482 Init_Alignment (T);
18483 Set_Is_Constrained (T);
18485 if not Is_OK_Static_Expression (Mod_Expr) then
18486 Flag_Non_Static_Expr
18487 ("non-static expression used for modular type bound!", Mod_Expr);
18488 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18489 else
18490 M_Val := Expr_Value (Mod_Expr);
18491 end if;
18493 if M_Val < 1 then
18494 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18495 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18496 end if;
18498 if M_Val > 2 ** Standard_Long_Integer_Size then
18499 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18500 end if;
18502 Set_Modulus (T, M_Val);
18504 -- Create bounds for the modular type based on the modulus given in
18505 -- the type declaration and then analyze and resolve those bounds.
18507 Set_Scalar_Range (T,
18508 Make_Range (Sloc (Mod_Expr),
18509 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18510 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18512 -- Properly analyze the literals for the range. We do this manually
18513 -- because we can't go calling Resolve, since we are resolving these
18514 -- bounds with the type, and this type is certainly not complete yet.
18516 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18517 Set_Etype (High_Bound (Scalar_Range (T)), T);
18518 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18519 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18521 -- Loop through powers of two to find number of bits required
18523 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18525 -- Binary case
18527 if M_Val = 2 ** Bits then
18528 Set_Modular_Size (Bits);
18529 return;
18531 -- Nonbinary case
18533 elsif M_Val < 2 ** Bits then
18534 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18535 Set_Non_Binary_Modulus (T);
18537 if Bits > System_Max_Nonbinary_Modulus_Power then
18538 Error_Msg_Uint_1 :=
18539 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18540 Error_Msg_F
18541 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18542 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18543 return;
18545 else
18546 -- In the nonbinary case, set size as per RM 13.3(55)
18548 Set_Modular_Size (Bits);
18549 return;
18550 end if;
18551 end if;
18553 end loop;
18555 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18556 -- so we just signal an error and set the maximum size.
18558 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18559 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18561 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18562 Init_Alignment (T);
18564 end Modular_Type_Declaration;
18566 --------------------------
18567 -- New_Concatenation_Op --
18568 --------------------------
18570 procedure New_Concatenation_Op (Typ : Entity_Id) is
18571 Loc : constant Source_Ptr := Sloc (Typ);
18572 Op : Entity_Id;
18574 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18575 -- Create abbreviated declaration for the formal of a predefined
18576 -- Operator 'Op' of type 'Typ'
18578 --------------------
18579 -- Make_Op_Formal --
18580 --------------------
18582 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18583 Formal : Entity_Id;
18584 begin
18585 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18586 Set_Etype (Formal, Typ);
18587 Set_Mechanism (Formal, Default_Mechanism);
18588 return Formal;
18589 end Make_Op_Formal;
18591 -- Start of processing for New_Concatenation_Op
18593 begin
18594 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18596 Set_Ekind (Op, E_Operator);
18597 Set_Scope (Op, Current_Scope);
18598 Set_Etype (Op, Typ);
18599 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18600 Set_Is_Immediately_Visible (Op);
18601 Set_Is_Intrinsic_Subprogram (Op);
18602 Set_Has_Completion (Op);
18603 Append_Entity (Op, Current_Scope);
18605 Set_Name_Entity_Id (Name_Op_Concat, Op);
18607 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18608 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18609 end New_Concatenation_Op;
18611 -------------------------
18612 -- OK_For_Limited_Init --
18613 -------------------------
18615 -- ???Check all calls of this, and compare the conditions under which it's
18616 -- called.
18618 function OK_For_Limited_Init
18619 (Typ : Entity_Id;
18620 Exp : Node_Id) return Boolean
18622 begin
18623 return Is_CPP_Constructor_Call (Exp)
18624 or else (Ada_Version >= Ada_2005
18625 and then not Debug_Flag_Dot_L
18626 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18627 end OK_For_Limited_Init;
18629 -------------------------------
18630 -- OK_For_Limited_Init_In_05 --
18631 -------------------------------
18633 function OK_For_Limited_Init_In_05
18634 (Typ : Entity_Id;
18635 Exp : Node_Id) return Boolean
18637 begin
18638 -- An object of a limited interface type can be initialized with any
18639 -- expression of a nonlimited descendant type.
18641 if Is_Class_Wide_Type (Typ)
18642 and then Is_Limited_Interface (Typ)
18643 and then not Is_Limited_Type (Etype (Exp))
18644 then
18645 return True;
18646 end if;
18648 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18649 -- case of limited aggregates (including extension aggregates), and
18650 -- function calls. The function call may have been given in prefixed
18651 -- notation, in which case the original node is an indexed component.
18652 -- If the function is parameterless, the original node was an explicit
18653 -- dereference. The function may also be parameterless, in which case
18654 -- the source node is just an identifier.
18656 -- A branch of a conditional expression may have been removed if the
18657 -- condition is statically known. This happens during expansion, and
18658 -- thus will not happen if previous errors were encountered. The check
18659 -- will have been performed on the chosen branch, which replaces the
18660 -- original conditional expression.
18662 if No (Exp) then
18663 return True;
18664 end if;
18666 case Nkind (Original_Node (Exp)) is
18667 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18668 return True;
18670 when N_Identifier =>
18671 return Present (Entity (Original_Node (Exp)))
18672 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18674 when N_Qualified_Expression =>
18675 return
18676 OK_For_Limited_Init_In_05
18677 (Typ, Expression (Original_Node (Exp)));
18679 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18680 -- with a function call, the expander has rewritten the call into an
18681 -- N_Type_Conversion node to force displacement of the pointer to
18682 -- reference the component containing the secondary dispatch table.
18683 -- Otherwise a type conversion is not a legal context.
18684 -- A return statement for a build-in-place function returning a
18685 -- synchronized type also introduces an unchecked conversion.
18687 when N_Type_Conversion |
18688 N_Unchecked_Type_Conversion =>
18689 return not Comes_From_Source (Exp)
18690 and then
18691 OK_For_Limited_Init_In_05
18692 (Typ, Expression (Original_Node (Exp)));
18694 when N_Indexed_Component |
18695 N_Selected_Component |
18696 N_Explicit_Dereference =>
18697 return Nkind (Exp) = N_Function_Call;
18699 -- A use of 'Input is a function call, hence allowed. Normally the
18700 -- attribute will be changed to a call, but the attribute by itself
18701 -- can occur with -gnatc.
18703 when N_Attribute_Reference =>
18704 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18706 -- For a case expression, all dependent expressions must be legal
18708 when N_Case_Expression =>
18709 declare
18710 Alt : Node_Id;
18712 begin
18713 Alt := First (Alternatives (Original_Node (Exp)));
18714 while Present (Alt) loop
18715 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18716 return False;
18717 end if;
18719 Next (Alt);
18720 end loop;
18722 return True;
18723 end;
18725 -- For an if expression, all dependent expressions must be legal
18727 when N_If_Expression =>
18728 declare
18729 Then_Expr : constant Node_Id :=
18730 Next (First (Expressions (Original_Node (Exp))));
18731 Else_Expr : constant Node_Id := Next (Then_Expr);
18732 begin
18733 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18734 and then
18735 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18736 end;
18738 when others =>
18739 return False;
18740 end case;
18741 end OK_For_Limited_Init_In_05;
18743 -------------------------------------------
18744 -- Ordinary_Fixed_Point_Type_Declaration --
18745 -------------------------------------------
18747 procedure Ordinary_Fixed_Point_Type_Declaration
18748 (T : Entity_Id;
18749 Def : Node_Id)
18751 Loc : constant Source_Ptr := Sloc (Def);
18752 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18753 RRS : constant Node_Id := Real_Range_Specification (Def);
18754 Implicit_Base : Entity_Id;
18755 Delta_Val : Ureal;
18756 Small_Val : Ureal;
18757 Low_Val : Ureal;
18758 High_Val : Ureal;
18760 begin
18761 Check_Restriction (No_Fixed_Point, Def);
18763 -- Create implicit base type
18765 Implicit_Base :=
18766 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18767 Set_Etype (Implicit_Base, Implicit_Base);
18769 -- Analyze and process delta expression
18771 Analyze_And_Resolve (Delta_Expr, Any_Real);
18773 Check_Delta_Expression (Delta_Expr);
18774 Delta_Val := Expr_Value_R (Delta_Expr);
18776 Set_Delta_Value (Implicit_Base, Delta_Val);
18778 -- Compute default small from given delta, which is the largest power
18779 -- of two that does not exceed the given delta value.
18781 declare
18782 Tmp : Ureal;
18783 Scale : Int;
18785 begin
18786 Tmp := Ureal_1;
18787 Scale := 0;
18789 if Delta_Val < Ureal_1 then
18790 while Delta_Val < Tmp loop
18791 Tmp := Tmp / Ureal_2;
18792 Scale := Scale + 1;
18793 end loop;
18795 else
18796 loop
18797 Tmp := Tmp * Ureal_2;
18798 exit when Tmp > Delta_Val;
18799 Scale := Scale - 1;
18800 end loop;
18801 end if;
18803 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18804 end;
18806 Set_Small_Value (Implicit_Base, Small_Val);
18808 -- If no range was given, set a dummy range
18810 if RRS <= Empty_Or_Error then
18811 Low_Val := -Small_Val;
18812 High_Val := Small_Val;
18814 -- Otherwise analyze and process given range
18816 else
18817 declare
18818 Low : constant Node_Id := Low_Bound (RRS);
18819 High : constant Node_Id := High_Bound (RRS);
18821 begin
18822 Analyze_And_Resolve (Low, Any_Real);
18823 Analyze_And_Resolve (High, Any_Real);
18824 Check_Real_Bound (Low);
18825 Check_Real_Bound (High);
18827 -- Obtain and set the range
18829 Low_Val := Expr_Value_R (Low);
18830 High_Val := Expr_Value_R (High);
18832 if Low_Val > High_Val then
18833 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18834 end if;
18835 end;
18836 end if;
18838 -- The range for both the implicit base and the declared first subtype
18839 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18840 -- set a temporary range in place. Note that the bounds of the base
18841 -- type will be widened to be symmetrical and to fill the available
18842 -- bits when the type is frozen.
18844 -- We could do this with all discrete types, and probably should, but
18845 -- we absolutely have to do it for fixed-point, since the end-points
18846 -- of the range and the size are determined by the small value, which
18847 -- could be reset before the freeze point.
18849 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18850 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18852 -- Complete definition of first subtype. The inheritance of the rep item
18853 -- chain ensures that SPARK-related pragmas are not clobbered when the
18854 -- ordinary fixed point type acts as a full view of a private type.
18856 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18857 Set_Etype (T, Implicit_Base);
18858 Init_Size_Align (T);
18859 Inherit_Rep_Item_Chain (T, Implicit_Base);
18860 Set_Small_Value (T, Small_Val);
18861 Set_Delta_Value (T, Delta_Val);
18862 Set_Is_Constrained (T);
18863 end Ordinary_Fixed_Point_Type_Declaration;
18865 ----------------------------------
18866 -- Preanalyze_Assert_Expression --
18867 ----------------------------------
18869 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18870 begin
18871 In_Assertion_Expr := In_Assertion_Expr + 1;
18872 Preanalyze_Spec_Expression (N, T);
18873 In_Assertion_Expr := In_Assertion_Expr - 1;
18874 end Preanalyze_Assert_Expression;
18876 -----------------------------------
18877 -- Preanalyze_Default_Expression --
18878 -----------------------------------
18880 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18881 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18882 begin
18883 In_Default_Expr := True;
18884 Preanalyze_Spec_Expression (N, T);
18885 In_Default_Expr := Save_In_Default_Expr;
18886 end Preanalyze_Default_Expression;
18888 --------------------------------
18889 -- Preanalyze_Spec_Expression --
18890 --------------------------------
18892 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18893 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18894 begin
18895 In_Spec_Expression := True;
18896 Preanalyze_And_Resolve (N, T);
18897 In_Spec_Expression := Save_In_Spec_Expression;
18898 end Preanalyze_Spec_Expression;
18900 ----------------------------------------
18901 -- Prepare_Private_Subtype_Completion --
18902 ----------------------------------------
18904 procedure Prepare_Private_Subtype_Completion
18905 (Id : Entity_Id;
18906 Related_Nod : Node_Id)
18908 Id_B : constant Entity_Id := Base_Type (Id);
18909 Full_B : Entity_Id := Full_View (Id_B);
18910 Full : Entity_Id;
18912 begin
18913 if Present (Full_B) then
18915 -- Get to the underlying full view if necessary
18917 if Is_Private_Type (Full_B)
18918 and then Present (Underlying_Full_View (Full_B))
18919 then
18920 Full_B := Underlying_Full_View (Full_B);
18921 end if;
18923 -- The Base_Type is already completed, we can complete the subtype
18924 -- now. We have to create a new entity with the same name, Thus we
18925 -- can't use Create_Itype.
18927 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18928 Set_Is_Itype (Full);
18929 Set_Associated_Node_For_Itype (Full, Related_Nod);
18930 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18931 end if;
18933 -- The parent subtype may be private, but the base might not, in some
18934 -- nested instances. In that case, the subtype does not need to be
18935 -- exchanged. It would still be nice to make private subtypes and their
18936 -- bases consistent at all times ???
18938 if Is_Private_Type (Id_B) then
18939 Append_Elmt (Id, Private_Dependents (Id_B));
18940 end if;
18941 end Prepare_Private_Subtype_Completion;
18943 ---------------------------
18944 -- Process_Discriminants --
18945 ---------------------------
18947 procedure Process_Discriminants
18948 (N : Node_Id;
18949 Prev : Entity_Id := Empty)
18951 Elist : constant Elist_Id := New_Elmt_List;
18952 Id : Node_Id;
18953 Discr : Node_Id;
18954 Discr_Number : Uint;
18955 Discr_Type : Entity_Id;
18956 Default_Present : Boolean := False;
18957 Default_Not_Present : Boolean := False;
18959 begin
18960 -- A composite type other than an array type can have discriminants.
18961 -- On entry, the current scope is the composite type.
18963 -- The discriminants are initially entered into the scope of the type
18964 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18965 -- use, as explained at the end of this procedure.
18967 Discr := First (Discriminant_Specifications (N));
18968 while Present (Discr) loop
18969 Enter_Name (Defining_Identifier (Discr));
18971 -- For navigation purposes we add a reference to the discriminant
18972 -- in the entity for the type. If the current declaration is a
18973 -- completion, place references on the partial view. Otherwise the
18974 -- type is the current scope.
18976 if Present (Prev) then
18978 -- The references go on the partial view, if present. If the
18979 -- partial view has discriminants, the references have been
18980 -- generated already.
18982 if not Has_Discriminants (Prev) then
18983 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18984 end if;
18985 else
18986 Generate_Reference
18987 (Current_Scope, Defining_Identifier (Discr), 'd');
18988 end if;
18990 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18991 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18993 -- Ada 2005 (AI-254)
18995 if Present (Access_To_Subprogram_Definition
18996 (Discriminant_Type (Discr)))
18997 and then Protected_Present (Access_To_Subprogram_Definition
18998 (Discriminant_Type (Discr)))
18999 then
19000 Discr_Type :=
19001 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19002 end if;
19004 else
19005 Find_Type (Discriminant_Type (Discr));
19006 Discr_Type := Etype (Discriminant_Type (Discr));
19008 if Error_Posted (Discriminant_Type (Discr)) then
19009 Discr_Type := Any_Type;
19010 end if;
19011 end if;
19013 -- Handling of discriminants that are access types
19015 if Is_Access_Type (Discr_Type) then
19017 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19018 -- limited record types
19020 if Ada_Version < Ada_2005 then
19021 Check_Access_Discriminant_Requires_Limited
19022 (Discr, Discriminant_Type (Discr));
19023 end if;
19025 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19026 Error_Msg_N
19027 ("(Ada 83) access discriminant not allowed", Discr);
19028 end if;
19030 -- If not access type, must be a discrete type
19032 elsif not Is_Discrete_Type (Discr_Type) then
19033 Error_Msg_N
19034 ("discriminants must have a discrete or access type",
19035 Discriminant_Type (Discr));
19036 end if;
19038 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19040 -- If a discriminant specification includes the assignment compound
19041 -- delimiter followed by an expression, the expression is the default
19042 -- expression of the discriminant; the default expression must be of
19043 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19044 -- a default expression, we do the special preanalysis, since this
19045 -- expression does not freeze (see section "Handling of Default and
19046 -- Per-Object Expressions" in spec of package Sem).
19048 if Present (Expression (Discr)) then
19049 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19051 -- Legaity checks
19053 if Nkind (N) = N_Formal_Type_Declaration then
19054 Error_Msg_N
19055 ("discriminant defaults not allowed for formal type",
19056 Expression (Discr));
19058 -- Flag an error for a tagged type with defaulted discriminants,
19059 -- excluding limited tagged types when compiling for Ada 2012
19060 -- (see AI05-0214).
19062 elsif Is_Tagged_Type (Current_Scope)
19063 and then (not Is_Limited_Type (Current_Scope)
19064 or else Ada_Version < Ada_2012)
19065 and then Comes_From_Source (N)
19066 then
19067 -- Note: see similar test in Check_Or_Process_Discriminants, to
19068 -- handle the (illegal) case of the completion of an untagged
19069 -- view with discriminants with defaults by a tagged full view.
19070 -- We skip the check if Discr does not come from source, to
19071 -- account for the case of an untagged derived type providing
19072 -- defaults for a renamed discriminant from a private untagged
19073 -- ancestor with a tagged full view (ACATS B460006).
19075 if Ada_Version >= Ada_2012 then
19076 Error_Msg_N
19077 ("discriminants of nonlimited tagged type cannot have"
19078 & " defaults",
19079 Expression (Discr));
19080 else
19081 Error_Msg_N
19082 ("discriminants of tagged type cannot have defaults",
19083 Expression (Discr));
19084 end if;
19086 else
19087 Default_Present := True;
19088 Append_Elmt (Expression (Discr), Elist);
19090 -- Tag the defining identifiers for the discriminants with
19091 -- their corresponding default expressions from the tree.
19093 Set_Discriminant_Default_Value
19094 (Defining_Identifier (Discr), Expression (Discr));
19095 end if;
19097 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19098 -- gets set unless we can be sure that no range check is required.
19100 if (GNATprove_Mode or not Expander_Active)
19101 and then not
19102 Is_In_Range
19103 (Expression (Discr), Discr_Type, Assume_Valid => True)
19104 then
19105 Set_Do_Range_Check (Expression (Discr));
19106 end if;
19108 -- No default discriminant value given
19110 else
19111 Default_Not_Present := True;
19112 end if;
19114 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19115 -- Discr_Type but with the null-exclusion attribute
19117 if Ada_Version >= Ada_2005 then
19119 -- Ada 2005 (AI-231): Static checks
19121 if Can_Never_Be_Null (Discr_Type) then
19122 Null_Exclusion_Static_Checks (Discr);
19124 elsif Is_Access_Type (Discr_Type)
19125 and then Null_Exclusion_Present (Discr)
19127 -- No need to check itypes because in their case this check
19128 -- was done at their point of creation
19130 and then not Is_Itype (Discr_Type)
19131 then
19132 if Can_Never_Be_Null (Discr_Type) then
19133 Error_Msg_NE
19134 ("`NOT NULL` not allowed (& already excludes null)",
19135 Discr,
19136 Discr_Type);
19137 end if;
19139 Set_Etype (Defining_Identifier (Discr),
19140 Create_Null_Excluding_Itype
19141 (T => Discr_Type,
19142 Related_Nod => Discr));
19144 -- Check for improper null exclusion if the type is otherwise
19145 -- legal for a discriminant.
19147 elsif Null_Exclusion_Present (Discr)
19148 and then Is_Discrete_Type (Discr_Type)
19149 then
19150 Error_Msg_N
19151 ("null exclusion can only apply to an access type", Discr);
19152 end if;
19154 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19155 -- can't have defaults. Synchronized types, or types that are
19156 -- explicitly limited are fine, but special tests apply to derived
19157 -- types in generics: in a generic body we have to assume the
19158 -- worst, and therefore defaults are not allowed if the parent is
19159 -- a generic formal private type (see ACATS B370001).
19161 if Is_Access_Type (Discr_Type) and then Default_Present then
19162 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19163 or else Is_Limited_Record (Current_Scope)
19164 or else Is_Concurrent_Type (Current_Scope)
19165 or else Is_Concurrent_Record_Type (Current_Scope)
19166 or else Ekind (Current_Scope) = E_Limited_Private_Type
19167 then
19168 if not Is_Derived_Type (Current_Scope)
19169 or else not Is_Generic_Type (Etype (Current_Scope))
19170 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19171 or else Limited_Present
19172 (Type_Definition (Parent (Current_Scope)))
19173 then
19174 null;
19176 else
19177 Error_Msg_N
19178 ("access discriminants of nonlimited types cannot "
19179 & "have defaults", Expression (Discr));
19180 end if;
19182 elsif Present (Expression (Discr)) then
19183 Error_Msg_N
19184 ("(Ada 2005) access discriminants of nonlimited types "
19185 & "cannot have defaults", Expression (Discr));
19186 end if;
19187 end if;
19188 end if;
19190 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19191 -- This check is relevant only when SPARK_Mode is on as it is not a
19192 -- standard Ada legality rule.
19194 if SPARK_Mode = On
19195 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19196 then
19197 Error_Msg_N ("discriminant cannot be volatile", Discr);
19198 end if;
19200 Next (Discr);
19201 end loop;
19203 -- An element list consisting of the default expressions of the
19204 -- discriminants is constructed in the above loop and used to set
19205 -- the Discriminant_Constraint attribute for the type. If an object
19206 -- is declared of this (record or task) type without any explicit
19207 -- discriminant constraint given, this element list will form the
19208 -- actual parameters for the corresponding initialization procedure
19209 -- for the type.
19211 Set_Discriminant_Constraint (Current_Scope, Elist);
19212 Set_Stored_Constraint (Current_Scope, No_Elist);
19214 -- Default expressions must be provided either for all or for none
19215 -- of the discriminants of a discriminant part. (RM 3.7.1)
19217 if Default_Present and then Default_Not_Present then
19218 Error_Msg_N
19219 ("incomplete specification of defaults for discriminants", N);
19220 end if;
19222 -- The use of the name of a discriminant is not allowed in default
19223 -- expressions of a discriminant part if the specification of the
19224 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19226 -- To detect this, the discriminant names are entered initially with an
19227 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19228 -- attempt to use a void entity (for example in an expression that is
19229 -- type-checked) produces the error message: premature usage. Now after
19230 -- completing the semantic analysis of the discriminant part, we can set
19231 -- the Ekind of all the discriminants appropriately.
19233 Discr := First (Discriminant_Specifications (N));
19234 Discr_Number := Uint_1;
19235 while Present (Discr) loop
19236 Id := Defining_Identifier (Discr);
19237 Set_Ekind (Id, E_Discriminant);
19238 Init_Component_Location (Id);
19239 Init_Esize (Id);
19240 Set_Discriminant_Number (Id, Discr_Number);
19242 -- Make sure this is always set, even in illegal programs
19244 Set_Corresponding_Discriminant (Id, Empty);
19246 -- Initialize the Original_Record_Component to the entity itself.
19247 -- Inherit_Components will propagate the right value to
19248 -- discriminants in derived record types.
19250 Set_Original_Record_Component (Id, Id);
19252 -- Create the discriminal for the discriminant
19254 Build_Discriminal (Id);
19256 Next (Discr);
19257 Discr_Number := Discr_Number + 1;
19258 end loop;
19260 Set_Has_Discriminants (Current_Scope);
19261 end Process_Discriminants;
19263 -----------------------
19264 -- Process_Full_View --
19265 -----------------------
19267 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19268 procedure Collect_Implemented_Interfaces
19269 (Typ : Entity_Id;
19270 Ifaces : Elist_Id);
19271 -- Ada 2005: Gather all the interfaces that Typ directly or
19272 -- inherently implements. Duplicate entries are not added to
19273 -- the list Ifaces.
19275 ------------------------------------
19276 -- Collect_Implemented_Interfaces --
19277 ------------------------------------
19279 procedure Collect_Implemented_Interfaces
19280 (Typ : Entity_Id;
19281 Ifaces : Elist_Id)
19283 Iface : Entity_Id;
19284 Iface_Elmt : Elmt_Id;
19286 begin
19287 -- Abstract interfaces are only associated with tagged record types
19289 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19290 return;
19291 end if;
19293 -- Recursively climb to the ancestors
19295 if Etype (Typ) /= Typ
19297 -- Protect the frontend against wrong cyclic declarations like:
19299 -- type B is new A with private;
19300 -- type C is new A with private;
19301 -- private
19302 -- type B is new C with null record;
19303 -- type C is new B with null record;
19305 and then Etype (Typ) /= Priv_T
19306 and then Etype (Typ) /= Full_T
19307 then
19308 -- Keep separate the management of private type declarations
19310 if Ekind (Typ) = E_Record_Type_With_Private then
19312 -- Handle the following illegal usage:
19313 -- type Private_Type is tagged private;
19314 -- private
19315 -- type Private_Type is new Type_Implementing_Iface;
19317 if Present (Full_View (Typ))
19318 and then Etype (Typ) /= Full_View (Typ)
19319 then
19320 if Is_Interface (Etype (Typ)) then
19321 Append_Unique_Elmt (Etype (Typ), Ifaces);
19322 end if;
19324 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19325 end if;
19327 -- Non-private types
19329 else
19330 if Is_Interface (Etype (Typ)) then
19331 Append_Unique_Elmt (Etype (Typ), Ifaces);
19332 end if;
19334 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19335 end if;
19336 end if;
19338 -- Handle entities in the list of abstract interfaces
19340 if Present (Interfaces (Typ)) then
19341 Iface_Elmt := First_Elmt (Interfaces (Typ));
19342 while Present (Iface_Elmt) loop
19343 Iface := Node (Iface_Elmt);
19345 pragma Assert (Is_Interface (Iface));
19347 if not Contain_Interface (Iface, Ifaces) then
19348 Append_Elmt (Iface, Ifaces);
19349 Collect_Implemented_Interfaces (Iface, Ifaces);
19350 end if;
19352 Next_Elmt (Iface_Elmt);
19353 end loop;
19354 end if;
19355 end Collect_Implemented_Interfaces;
19357 -- Local variables
19359 Full_Indic : Node_Id;
19360 Full_Parent : Entity_Id;
19361 Priv_Parent : Entity_Id;
19363 -- Start of processing for Process_Full_View
19365 begin
19366 -- First some sanity checks that must be done after semantic
19367 -- decoration of the full view and thus cannot be placed with other
19368 -- similar checks in Find_Type_Name
19370 if not Is_Limited_Type (Priv_T)
19371 and then (Is_Limited_Type (Full_T)
19372 or else Is_Limited_Composite (Full_T))
19373 then
19374 if In_Instance then
19375 null;
19376 else
19377 Error_Msg_N
19378 ("completion of nonlimited type cannot be limited", Full_T);
19379 Explain_Limited_Type (Full_T, Full_T);
19380 end if;
19382 elsif Is_Abstract_Type (Full_T)
19383 and then not Is_Abstract_Type (Priv_T)
19384 then
19385 Error_Msg_N
19386 ("completion of nonabstract type cannot be abstract", Full_T);
19388 elsif Is_Tagged_Type (Priv_T)
19389 and then Is_Limited_Type (Priv_T)
19390 and then not Is_Limited_Type (Full_T)
19391 then
19392 -- If pragma CPP_Class was applied to the private declaration
19393 -- propagate the limitedness to the full-view
19395 if Is_CPP_Class (Priv_T) then
19396 Set_Is_Limited_Record (Full_T);
19398 -- GNAT allow its own definition of Limited_Controlled to disobey
19399 -- this rule in order in ease the implementation. This test is safe
19400 -- because Root_Controlled is defined in a child of System that
19401 -- normal programs are not supposed to use.
19403 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19404 Set_Is_Limited_Composite (Full_T);
19405 else
19406 Error_Msg_N
19407 ("completion of limited tagged type must be limited", Full_T);
19408 end if;
19410 elsif Is_Generic_Type (Priv_T) then
19411 Error_Msg_N ("generic type cannot have a completion", Full_T);
19412 end if;
19414 -- Check that ancestor interfaces of private and full views are
19415 -- consistent. We omit this check for synchronized types because
19416 -- they are performed on the corresponding record type when frozen.
19418 if Ada_Version >= Ada_2005
19419 and then Is_Tagged_Type (Priv_T)
19420 and then Is_Tagged_Type (Full_T)
19421 and then not Is_Concurrent_Type (Full_T)
19422 then
19423 declare
19424 Iface : Entity_Id;
19425 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19426 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19428 begin
19429 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19430 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19432 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19433 -- an interface type if and only if the full type is descendant
19434 -- of the interface type (AARM 7.3 (7.3/2)).
19436 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19438 if Present (Iface) then
19439 Error_Msg_NE
19440 ("interface in partial view& not implemented by full type "
19441 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19442 end if;
19444 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19446 if Present (Iface) then
19447 Error_Msg_NE
19448 ("interface & not implemented by partial view "
19449 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19450 end if;
19451 end;
19452 end if;
19454 if Is_Tagged_Type (Priv_T)
19455 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19456 and then Is_Derived_Type (Full_T)
19457 then
19458 Priv_Parent := Etype (Priv_T);
19460 -- The full view of a private extension may have been transformed
19461 -- into an unconstrained derived type declaration and a subtype
19462 -- declaration (see build_derived_record_type for details).
19464 if Nkind (N) = N_Subtype_Declaration then
19465 Full_Indic := Subtype_Indication (N);
19466 Full_Parent := Etype (Base_Type (Full_T));
19467 else
19468 Full_Indic := Subtype_Indication (Type_Definition (N));
19469 Full_Parent := Etype (Full_T);
19470 end if;
19472 -- Check that the parent type of the full type is a descendant of
19473 -- the ancestor subtype given in the private extension. If either
19474 -- entity has an Etype equal to Any_Type then we had some previous
19475 -- error situation [7.3(8)].
19477 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19478 return;
19480 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19481 -- any order. Therefore we don't have to check that its parent must
19482 -- be a descendant of the parent of the private type declaration.
19484 elsif Is_Interface (Priv_Parent)
19485 and then Is_Interface (Full_Parent)
19486 then
19487 null;
19489 -- Ada 2005 (AI-251): If the parent of the private type declaration
19490 -- is an interface there is no need to check that it is an ancestor
19491 -- of the associated full type declaration. The required tests for
19492 -- this case are performed by Build_Derived_Record_Type.
19494 elsif not Is_Interface (Base_Type (Priv_Parent))
19495 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19496 then
19497 Error_Msg_N
19498 ("parent of full type must descend from parent"
19499 & " of private extension", Full_Indic);
19501 -- First check a formal restriction, and then proceed with checking
19502 -- Ada rules. Since the formal restriction is not a serious error, we
19503 -- don't prevent further error detection for this check, hence the
19504 -- ELSE.
19506 else
19507 -- In formal mode, when completing a private extension the type
19508 -- named in the private part must be exactly the same as that
19509 -- named in the visible part.
19511 if Priv_Parent /= Full_Parent then
19512 Error_Msg_Name_1 := Chars (Priv_Parent);
19513 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19514 end if;
19516 -- Check the rules of 7.3(10): if the private extension inherits
19517 -- known discriminants, then the full type must also inherit those
19518 -- discriminants from the same (ancestor) type, and the parent
19519 -- subtype of the full type must be constrained if and only if
19520 -- the ancestor subtype of the private extension is constrained.
19522 if No (Discriminant_Specifications (Parent (Priv_T)))
19523 and then not Has_Unknown_Discriminants (Priv_T)
19524 and then Has_Discriminants (Base_Type (Priv_Parent))
19525 then
19526 declare
19527 Priv_Indic : constant Node_Id :=
19528 Subtype_Indication (Parent (Priv_T));
19530 Priv_Constr : constant Boolean :=
19531 Is_Constrained (Priv_Parent)
19532 or else
19533 Nkind (Priv_Indic) = N_Subtype_Indication
19534 or else
19535 Is_Constrained (Entity (Priv_Indic));
19537 Full_Constr : constant Boolean :=
19538 Is_Constrained (Full_Parent)
19539 or else
19540 Nkind (Full_Indic) = N_Subtype_Indication
19541 or else
19542 Is_Constrained (Entity (Full_Indic));
19544 Priv_Discr : Entity_Id;
19545 Full_Discr : Entity_Id;
19547 begin
19548 Priv_Discr := First_Discriminant (Priv_Parent);
19549 Full_Discr := First_Discriminant (Full_Parent);
19550 while Present (Priv_Discr) and then Present (Full_Discr) loop
19551 if Original_Record_Component (Priv_Discr) =
19552 Original_Record_Component (Full_Discr)
19553 or else
19554 Corresponding_Discriminant (Priv_Discr) =
19555 Corresponding_Discriminant (Full_Discr)
19556 then
19557 null;
19558 else
19559 exit;
19560 end if;
19562 Next_Discriminant (Priv_Discr);
19563 Next_Discriminant (Full_Discr);
19564 end loop;
19566 if Present (Priv_Discr) or else Present (Full_Discr) then
19567 Error_Msg_N
19568 ("full view must inherit discriminants of the parent"
19569 & " type used in the private extension", Full_Indic);
19571 elsif Priv_Constr and then not Full_Constr then
19572 Error_Msg_N
19573 ("parent subtype of full type must be constrained",
19574 Full_Indic);
19576 elsif Full_Constr and then not Priv_Constr then
19577 Error_Msg_N
19578 ("parent subtype of full type must be unconstrained",
19579 Full_Indic);
19580 end if;
19581 end;
19583 -- Check the rules of 7.3(12): if a partial view has neither
19584 -- known or unknown discriminants, then the full type
19585 -- declaration shall define a definite subtype.
19587 elsif not Has_Unknown_Discriminants (Priv_T)
19588 and then not Has_Discriminants (Priv_T)
19589 and then not Is_Constrained (Full_T)
19590 then
19591 Error_Msg_N
19592 ("full view must define a constrained type if partial view"
19593 & " has no discriminants", Full_T);
19594 end if;
19596 -- ??????? Do we implement the following properly ?????
19597 -- If the ancestor subtype of a private extension has constrained
19598 -- discriminants, then the parent subtype of the full view shall
19599 -- impose a statically matching constraint on those discriminants
19600 -- [7.3(13)].
19601 end if;
19603 else
19604 -- For untagged types, verify that a type without discriminants is
19605 -- not completed with an unconstrained type. A separate error message
19606 -- is produced if the full type has defaulted discriminants.
19608 if Is_Definite_Subtype (Priv_T)
19609 and then not Is_Definite_Subtype (Full_T)
19610 then
19611 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19612 Error_Msg_NE
19613 ("full view of& not compatible with declaration#",
19614 Full_T, Priv_T);
19616 if not Is_Tagged_Type (Full_T) then
19617 Error_Msg_N
19618 ("\one is constrained, the other unconstrained", Full_T);
19619 end if;
19620 end if;
19621 end if;
19623 -- AI-419: verify that the use of "limited" is consistent
19625 declare
19626 Orig_Decl : constant Node_Id := Original_Node (N);
19628 begin
19629 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19630 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19631 and then Nkind
19632 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19633 then
19634 if not Limited_Present (Parent (Priv_T))
19635 and then not Synchronized_Present (Parent (Priv_T))
19636 and then Limited_Present (Type_Definition (Orig_Decl))
19637 then
19638 Error_Msg_N
19639 ("full view of non-limited extension cannot be limited", N);
19641 -- Conversely, if the partial view carries the limited keyword,
19642 -- the full view must as well, even if it may be redundant.
19644 elsif Limited_Present (Parent (Priv_T))
19645 and then not Limited_Present (Type_Definition (Orig_Decl))
19646 then
19647 Error_Msg_N
19648 ("full view of limited extension must be explicitly limited",
19650 end if;
19651 end if;
19652 end;
19654 -- Ada 2005 (AI-443): A synchronized private extension must be
19655 -- completed by a task or protected type.
19657 if Ada_Version >= Ada_2005
19658 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19659 and then Synchronized_Present (Parent (Priv_T))
19660 and then not Is_Concurrent_Type (Full_T)
19661 then
19662 Error_Msg_N ("full view of synchronized extension must " &
19663 "be synchronized type", N);
19664 end if;
19666 -- Ada 2005 AI-363: if the full view has discriminants with
19667 -- defaults, it is illegal to declare constrained access subtypes
19668 -- whose designated type is the current type. This allows objects
19669 -- of the type that are declared in the heap to be unconstrained.
19671 if not Has_Unknown_Discriminants (Priv_T)
19672 and then not Has_Discriminants (Priv_T)
19673 and then Has_Discriminants (Full_T)
19674 and then
19675 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19676 then
19677 Set_Has_Constrained_Partial_View (Full_T);
19678 Set_Has_Constrained_Partial_View (Priv_T);
19679 end if;
19681 -- Create a full declaration for all its subtypes recorded in
19682 -- Private_Dependents and swap them similarly to the base type. These
19683 -- are subtypes that have been define before the full declaration of
19684 -- the private type. We also swap the entry in Private_Dependents list
19685 -- so we can properly restore the private view on exit from the scope.
19687 declare
19688 Priv_Elmt : Elmt_Id;
19689 Priv_Scop : Entity_Id;
19690 Priv : Entity_Id;
19691 Full : Entity_Id;
19693 begin
19694 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19695 while Present (Priv_Elmt) loop
19696 Priv := Node (Priv_Elmt);
19697 Priv_Scop := Scope (Priv);
19699 if Ekind_In (Priv, E_Private_Subtype,
19700 E_Limited_Private_Subtype,
19701 E_Record_Subtype_With_Private)
19702 then
19703 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19704 Set_Is_Itype (Full);
19705 Set_Parent (Full, Parent (Priv));
19706 Set_Associated_Node_For_Itype (Full, N);
19708 -- Now we need to complete the private subtype, but since the
19709 -- base type has already been swapped, we must also swap the
19710 -- subtypes (and thus, reverse the arguments in the call to
19711 -- Complete_Private_Subtype). Also note that we may need to
19712 -- re-establish the scope of the private subtype.
19714 Copy_And_Swap (Priv, Full);
19716 if not In_Open_Scopes (Priv_Scop) then
19717 Push_Scope (Priv_Scop);
19719 else
19720 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19722 Priv_Scop := Empty;
19723 end if;
19725 Complete_Private_Subtype (Full, Priv, Full_T, N);
19727 if Present (Priv_Scop) then
19728 Pop_Scope;
19729 end if;
19731 Replace_Elmt (Priv_Elmt, Full);
19732 end if;
19734 Next_Elmt (Priv_Elmt);
19735 end loop;
19736 end;
19738 -- If the private view was tagged, copy the new primitive operations
19739 -- from the private view to the full view.
19741 if Is_Tagged_Type (Full_T) then
19742 declare
19743 Disp_Typ : Entity_Id;
19744 Full_List : Elist_Id;
19745 Prim : Entity_Id;
19746 Prim_Elmt : Elmt_Id;
19747 Priv_List : Elist_Id;
19749 function Contains
19750 (E : Entity_Id;
19751 L : Elist_Id) return Boolean;
19752 -- Determine whether list L contains element E
19754 --------------
19755 -- Contains --
19756 --------------
19758 function Contains
19759 (E : Entity_Id;
19760 L : Elist_Id) return Boolean
19762 List_Elmt : Elmt_Id;
19764 begin
19765 List_Elmt := First_Elmt (L);
19766 while Present (List_Elmt) loop
19767 if Node (List_Elmt) = E then
19768 return True;
19769 end if;
19771 Next_Elmt (List_Elmt);
19772 end loop;
19774 return False;
19775 end Contains;
19777 -- Start of processing
19779 begin
19780 if Is_Tagged_Type (Priv_T) then
19781 Priv_List := Primitive_Operations (Priv_T);
19782 Prim_Elmt := First_Elmt (Priv_List);
19784 -- In the case of a concurrent type completing a private tagged
19785 -- type, primitives may have been declared in between the two
19786 -- views. These subprograms need to be wrapped the same way
19787 -- entries and protected procedures are handled because they
19788 -- cannot be directly shared by the two views.
19790 if Is_Concurrent_Type (Full_T) then
19791 declare
19792 Conc_Typ : constant Entity_Id :=
19793 Corresponding_Record_Type (Full_T);
19794 Curr_Nod : Node_Id := Parent (Conc_Typ);
19795 Wrap_Spec : Node_Id;
19797 begin
19798 while Present (Prim_Elmt) loop
19799 Prim := Node (Prim_Elmt);
19801 if Comes_From_Source (Prim)
19802 and then not Is_Abstract_Subprogram (Prim)
19803 then
19804 Wrap_Spec :=
19805 Make_Subprogram_Declaration (Sloc (Prim),
19806 Specification =>
19807 Build_Wrapper_Spec
19808 (Subp_Id => Prim,
19809 Obj_Typ => Conc_Typ,
19810 Formals =>
19811 Parameter_Specifications (
19812 Parent (Prim))));
19814 Insert_After (Curr_Nod, Wrap_Spec);
19815 Curr_Nod := Wrap_Spec;
19817 Analyze (Wrap_Spec);
19818 end if;
19820 Next_Elmt (Prim_Elmt);
19821 end loop;
19823 return;
19824 end;
19826 -- For non-concurrent types, transfer explicit primitives, but
19827 -- omit those inherited from the parent of the private view
19828 -- since they will be re-inherited later on.
19830 else
19831 Full_List := Primitive_Operations (Full_T);
19833 while Present (Prim_Elmt) loop
19834 Prim := Node (Prim_Elmt);
19836 if Comes_From_Source (Prim)
19837 and then not Contains (Prim, Full_List)
19838 then
19839 Append_Elmt (Prim, Full_List);
19840 end if;
19842 Next_Elmt (Prim_Elmt);
19843 end loop;
19844 end if;
19846 -- Untagged private view
19848 else
19849 Full_List := Primitive_Operations (Full_T);
19851 -- In this case the partial view is untagged, so here we locate
19852 -- all of the earlier primitives that need to be treated as
19853 -- dispatching (those that appear between the two views). Note
19854 -- that these additional operations must all be new operations
19855 -- (any earlier operations that override inherited operations
19856 -- of the full view will already have been inserted in the
19857 -- primitives list, marked by Check_Operation_From_Private_View
19858 -- as dispatching. Note that implicit "/=" operators are
19859 -- excluded from being added to the primitives list since they
19860 -- shouldn't be treated as dispatching (tagged "/=" is handled
19861 -- specially).
19863 Prim := Next_Entity (Full_T);
19864 while Present (Prim) and then Prim /= Priv_T loop
19865 if Ekind_In (Prim, E_Procedure, E_Function) then
19866 Disp_Typ := Find_Dispatching_Type (Prim);
19868 if Disp_Typ = Full_T
19869 and then (Chars (Prim) /= Name_Op_Ne
19870 or else Comes_From_Source (Prim))
19871 then
19872 Check_Controlling_Formals (Full_T, Prim);
19874 if not Is_Dispatching_Operation (Prim) then
19875 Append_Elmt (Prim, Full_List);
19876 Set_Is_Dispatching_Operation (Prim, True);
19877 Set_DT_Position_Value (Prim, No_Uint);
19878 end if;
19880 elsif Is_Dispatching_Operation (Prim)
19881 and then Disp_Typ /= Full_T
19882 then
19884 -- Verify that it is not otherwise controlled by a
19885 -- formal or a return value of type T.
19887 Check_Controlling_Formals (Disp_Typ, Prim);
19888 end if;
19889 end if;
19891 Next_Entity (Prim);
19892 end loop;
19893 end if;
19895 -- For the tagged case, the two views can share the same primitive
19896 -- operations list and the same class-wide type. Update attributes
19897 -- of the class-wide type which depend on the full declaration.
19899 if Is_Tagged_Type (Priv_T) then
19900 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19901 Set_Class_Wide_Type
19902 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19904 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19905 Set_Has_Protected
19906 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19907 end if;
19908 end;
19909 end if;
19911 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19913 if Known_To_Have_Preelab_Init (Priv_T) then
19915 -- Case where there is a pragma Preelaborable_Initialization. We
19916 -- always allow this in predefined units, which is cheating a bit,
19917 -- but it means we don't have to struggle to meet the requirements in
19918 -- the RM for having Preelaborable Initialization. Otherwise we
19919 -- require that the type meets the RM rules. But we can't check that
19920 -- yet, because of the rule about overriding Initialize, so we simply
19921 -- set a flag that will be checked at freeze time.
19923 if not In_Predefined_Unit (Full_T) then
19924 Set_Must_Have_Preelab_Init (Full_T);
19925 end if;
19926 end if;
19928 -- If pragma CPP_Class was applied to the private type declaration,
19929 -- propagate it now to the full type declaration.
19931 if Is_CPP_Class (Priv_T) then
19932 Set_Is_CPP_Class (Full_T);
19933 Set_Convention (Full_T, Convention_CPP);
19935 -- Check that components of imported CPP types do not have default
19936 -- expressions.
19938 Check_CPP_Type_Has_No_Defaults (Full_T);
19939 end if;
19941 -- If the private view has user specified stream attributes, then so has
19942 -- the full view.
19944 -- Why the test, how could these flags be already set in Full_T ???
19946 if Has_Specified_Stream_Read (Priv_T) then
19947 Set_Has_Specified_Stream_Read (Full_T);
19948 end if;
19950 if Has_Specified_Stream_Write (Priv_T) then
19951 Set_Has_Specified_Stream_Write (Full_T);
19952 end if;
19954 if Has_Specified_Stream_Input (Priv_T) then
19955 Set_Has_Specified_Stream_Input (Full_T);
19956 end if;
19958 if Has_Specified_Stream_Output (Priv_T) then
19959 Set_Has_Specified_Stream_Output (Full_T);
19960 end if;
19962 -- Propagate the attributes related to pragma Default_Initial_Condition
19963 -- from the private to the full view. Note that both flags are mutually
19964 -- exclusive.
19966 if Has_Default_Init_Cond (Priv_T)
19967 or else Has_Inherited_Default_Init_Cond (Priv_T)
19968 then
19969 Propagate_Default_Init_Cond_Attributes
19970 (From_Typ => Priv_T,
19971 To_Typ => Full_T,
19972 Private_To_Full_View => True);
19974 -- In the case where the full view is derived from another private type,
19975 -- the attributes related to pragma Default_Initial_Condition must be
19976 -- propagated from the full to the private view to maintain consistency
19977 -- of views.
19979 -- package Pack is
19980 -- type Parent_Typ is private
19981 -- with Default_Initial_Condition ...;
19982 -- private
19983 -- type Parent_Typ is ...;
19984 -- end Pack;
19986 -- with Pack; use Pack;
19987 -- package Pack_2 is
19988 -- type Deriv_Typ is private; -- must inherit
19989 -- private
19990 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19991 -- end Pack_2;
19993 elsif Has_Default_Init_Cond (Full_T)
19994 or else Has_Inherited_Default_Init_Cond (Full_T)
19995 then
19996 Propagate_Default_Init_Cond_Attributes
19997 (From_Typ => Full_T,
19998 To_Typ => Priv_T,
19999 Private_To_Full_View => True);
20000 end if;
20002 if Is_Ghost_Entity (Priv_T) then
20004 -- The Ghost policy in effect at the point of declaration and at the
20005 -- point of completion must match (SPARK RM 6.9(14)).
20007 Check_Ghost_Completion (Priv_T, Full_T);
20009 -- Propagate the attributes related to pragma Ghost from the private
20010 -- to the full view.
20012 Mark_Full_View_As_Ghost (Priv_T, Full_T);
20013 end if;
20015 -- Propagate invariants to full type
20017 if Has_Invariants (Priv_T) then
20018 Set_Has_Invariants (Full_T);
20019 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
20020 end if;
20022 if Has_Inheritable_Invariants (Priv_T) then
20023 Set_Has_Inheritable_Invariants (Full_T);
20024 end if;
20026 -- Check hidden inheritance of class-wide type invariants
20028 if Ada_Version >= Ada_2012
20029 and then not Has_Inheritable_Invariants (Full_T)
20030 and then In_Private_Part (Current_Scope)
20031 and then Has_Interfaces (Full_T)
20032 then
20033 declare
20034 Ifaces : Elist_Id;
20035 AI : Elmt_Id;
20037 begin
20038 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
20040 AI := First_Elmt (Ifaces);
20041 while Present (AI) loop
20042 if Has_Inheritable_Invariants (Node (AI)) then
20043 Error_Msg_N
20044 ("hidden inheritance of class-wide type invariants " &
20045 "not allowed", N);
20046 exit;
20047 end if;
20049 Next_Elmt (AI);
20050 end loop;
20051 end;
20052 end if;
20054 -- Propagate predicates to full type, and predicate function if already
20055 -- defined. It is not clear that this can actually happen? the partial
20056 -- view cannot be frozen yet, and the predicate function has not been
20057 -- built. Still it is a cheap check and seems safer to make it.
20059 if Has_Predicates (Priv_T) then
20060 if Present (Predicate_Function (Priv_T)) then
20061 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20062 end if;
20064 Set_Has_Predicates (Full_T);
20065 end if;
20066 end Process_Full_View;
20068 -----------------------------------
20069 -- Process_Incomplete_Dependents --
20070 -----------------------------------
20072 procedure Process_Incomplete_Dependents
20073 (N : Node_Id;
20074 Full_T : Entity_Id;
20075 Inc_T : Entity_Id)
20077 Inc_Elmt : Elmt_Id;
20078 Priv_Dep : Entity_Id;
20079 New_Subt : Entity_Id;
20081 Disc_Constraint : Elist_Id;
20083 begin
20084 if No (Private_Dependents (Inc_T)) then
20085 return;
20086 end if;
20088 -- Itypes that may be generated by the completion of an incomplete
20089 -- subtype are not used by the back-end and not attached to the tree.
20090 -- They are created only for constraint-checking purposes.
20092 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20093 while Present (Inc_Elmt) loop
20094 Priv_Dep := Node (Inc_Elmt);
20096 if Ekind (Priv_Dep) = E_Subprogram_Type then
20098 -- An Access_To_Subprogram type may have a return type or a
20099 -- parameter type that is incomplete. Replace with the full view.
20101 if Etype (Priv_Dep) = Inc_T then
20102 Set_Etype (Priv_Dep, Full_T);
20103 end if;
20105 declare
20106 Formal : Entity_Id;
20108 begin
20109 Formal := First_Formal (Priv_Dep);
20110 while Present (Formal) loop
20111 if Etype (Formal) = Inc_T then
20112 Set_Etype (Formal, Full_T);
20113 end if;
20115 Next_Formal (Formal);
20116 end loop;
20117 end;
20119 elsif Is_Overloadable (Priv_Dep) then
20121 -- If a subprogram in the incomplete dependents list is primitive
20122 -- for a tagged full type then mark it as a dispatching operation,
20123 -- check whether it overrides an inherited subprogram, and check
20124 -- restrictions on its controlling formals. Note that a protected
20125 -- operation is never dispatching: only its wrapper operation
20126 -- (which has convention Ada) is.
20128 if Is_Tagged_Type (Full_T)
20129 and then Is_Primitive (Priv_Dep)
20130 and then Convention (Priv_Dep) /= Convention_Protected
20131 then
20132 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20133 Set_Is_Dispatching_Operation (Priv_Dep);
20134 Check_Controlling_Formals (Full_T, Priv_Dep);
20135 end if;
20137 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20139 -- Can happen during processing of a body before the completion
20140 -- of a TA type. Ignore, because spec is also on dependent list.
20142 return;
20144 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20145 -- corresponding subtype of the full view.
20147 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20148 Set_Subtype_Indication
20149 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20150 Set_Etype (Priv_Dep, Full_T);
20151 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20152 Set_Analyzed (Parent (Priv_Dep), False);
20154 -- Reanalyze the declaration, suppressing the call to
20155 -- Enter_Name to avoid duplicate names.
20157 Analyze_Subtype_Declaration
20158 (N => Parent (Priv_Dep),
20159 Skip => True);
20161 -- Dependent is a subtype
20163 else
20164 -- We build a new subtype indication using the full view of the
20165 -- incomplete parent. The discriminant constraints have been
20166 -- elaborated already at the point of the subtype declaration.
20168 New_Subt := Create_Itype (E_Void, N);
20170 if Has_Discriminants (Full_T) then
20171 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20172 else
20173 Disc_Constraint := No_Elist;
20174 end if;
20176 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20177 Set_Full_View (Priv_Dep, New_Subt);
20178 end if;
20180 Next_Elmt (Inc_Elmt);
20181 end loop;
20182 end Process_Incomplete_Dependents;
20184 --------------------------------
20185 -- Process_Range_Expr_In_Decl --
20186 --------------------------------
20188 procedure Process_Range_Expr_In_Decl
20189 (R : Node_Id;
20190 T : Entity_Id;
20191 Subtyp : Entity_Id := Empty;
20192 Check_List : List_Id := Empty_List;
20193 R_Check_Off : Boolean := False;
20194 In_Iter_Schm : Boolean := False)
20196 Lo, Hi : Node_Id;
20197 R_Checks : Check_Result;
20198 Insert_Node : Node_Id;
20199 Def_Id : Entity_Id;
20201 begin
20202 Analyze_And_Resolve (R, Base_Type (T));
20204 if Nkind (R) = N_Range then
20206 -- In SPARK, all ranges should be static, with the exception of the
20207 -- discrete type definition of a loop parameter specification.
20209 if not In_Iter_Schm
20210 and then not Is_OK_Static_Range (R)
20211 then
20212 Check_SPARK_05_Restriction ("range should be static", R);
20213 end if;
20215 Lo := Low_Bound (R);
20216 Hi := High_Bound (R);
20218 -- Validity checks on the range of a quantified expression are
20219 -- delayed until the construct is transformed into a loop.
20221 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20222 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20223 then
20224 null;
20226 -- We need to ensure validity of the bounds here, because if we
20227 -- go ahead and do the expansion, then the expanded code will get
20228 -- analyzed with range checks suppressed and we miss the check.
20230 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20231 -- the temporaries generated by routine Remove_Side_Effects by means
20232 -- of validity checks must use the same names. When a range appears
20233 -- in the parent of a generic, the range is processed with checks
20234 -- disabled as part of the generic context and with checks enabled
20235 -- for code generation purposes. This leads to link issues as the
20236 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20237 -- template sees the temporaries generated by Remove_Side_Effects.
20239 else
20240 Validity_Check_Range (R, Subtyp);
20241 end if;
20243 -- If there were errors in the declaration, try and patch up some
20244 -- common mistakes in the bounds. The cases handled are literals
20245 -- which are Integer where the expected type is Real and vice versa.
20246 -- These corrections allow the compilation process to proceed further
20247 -- along since some basic assumptions of the format of the bounds
20248 -- are guaranteed.
20250 if Etype (R) = Any_Type then
20251 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20252 Rewrite (Lo,
20253 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20255 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20256 Rewrite (Hi,
20257 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20259 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20260 Rewrite (Lo,
20261 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20263 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20264 Rewrite (Hi,
20265 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20266 end if;
20268 Set_Etype (Lo, T);
20269 Set_Etype (Hi, T);
20270 end if;
20272 -- If the bounds of the range have been mistakenly given as string
20273 -- literals (perhaps in place of character literals), then an error
20274 -- has already been reported, but we rewrite the string literal as a
20275 -- bound of the range's type to avoid blowups in later processing
20276 -- that looks at static values.
20278 if Nkind (Lo) = N_String_Literal then
20279 Rewrite (Lo,
20280 Make_Attribute_Reference (Sloc (Lo),
20281 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20282 Attribute_Name => Name_First));
20283 Analyze_And_Resolve (Lo);
20284 end if;
20286 if Nkind (Hi) = N_String_Literal then
20287 Rewrite (Hi,
20288 Make_Attribute_Reference (Sloc (Hi),
20289 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20290 Attribute_Name => Name_First));
20291 Analyze_And_Resolve (Hi);
20292 end if;
20294 -- If bounds aren't scalar at this point then exit, avoiding
20295 -- problems with further processing of the range in this procedure.
20297 if not Is_Scalar_Type (Etype (Lo)) then
20298 return;
20299 end if;
20301 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20302 -- then range of the base type. Here we check whether the bounds
20303 -- are in the range of the subtype itself. Note that if the bounds
20304 -- represent the null range the Constraint_Error exception should
20305 -- not be raised.
20307 -- ??? The following code should be cleaned up as follows
20309 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20310 -- is done in the call to Range_Check (R, T); below
20312 -- 2. The use of R_Check_Off should be investigated and possibly
20313 -- removed, this would clean up things a bit.
20315 if Is_Null_Range (Lo, Hi) then
20316 null;
20318 else
20319 -- Capture values of bounds and generate temporaries for them
20320 -- if needed, before applying checks, since checks may cause
20321 -- duplication of the expression without forcing evaluation.
20323 -- The forced evaluation removes side effects from expressions,
20324 -- which should occur also in GNATprove mode. Otherwise, we end up
20325 -- with unexpected insertions of actions at places where this is
20326 -- not supposed to occur, e.g. on default parameters of a call.
20328 if Expander_Active or GNATprove_Mode then
20330 -- Call Force_Evaluation to create declarations as needed to
20331 -- deal with side effects, and also create typ_FIRST/LAST
20332 -- entities for bounds if we have a subtype name.
20334 -- Note: we do this transformation even if expansion is not
20335 -- active if we are in GNATprove_Mode since the transformation
20336 -- is in general required to ensure that the resulting tree has
20337 -- proper Ada semantics.
20339 Force_Evaluation
20340 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20341 Force_Evaluation
20342 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20343 end if;
20345 -- We use a flag here instead of suppressing checks on the type
20346 -- because the type we check against isn't necessarily the place
20347 -- where we put the check.
20349 if not R_Check_Off then
20350 R_Checks := Get_Range_Checks (R, T);
20352 -- Look up tree to find an appropriate insertion point. We
20353 -- can't just use insert_actions because later processing
20354 -- depends on the insertion node. Prior to Ada 2012 the
20355 -- insertion point could only be a declaration or a loop, but
20356 -- quantified expressions can appear within any context in an
20357 -- expression, and the insertion point can be any statement,
20358 -- pragma, or declaration.
20360 Insert_Node := Parent (R);
20361 while Present (Insert_Node) loop
20362 exit when
20363 Nkind (Insert_Node) in N_Declaration
20364 and then
20365 not Nkind_In
20366 (Insert_Node, N_Component_Declaration,
20367 N_Loop_Parameter_Specification,
20368 N_Function_Specification,
20369 N_Procedure_Specification);
20371 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20372 or else Nkind (Insert_Node) in
20373 N_Statement_Other_Than_Procedure_Call
20374 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20375 N_Pragma);
20377 Insert_Node := Parent (Insert_Node);
20378 end loop;
20380 -- Why would Type_Decl not be present??? Without this test,
20381 -- short regression tests fail.
20383 if Present (Insert_Node) then
20385 -- Case of loop statement. Verify that the range is part
20386 -- of the subtype indication of the iteration scheme.
20388 if Nkind (Insert_Node) = N_Loop_Statement then
20389 declare
20390 Indic : Node_Id;
20392 begin
20393 Indic := Parent (R);
20394 while Present (Indic)
20395 and then Nkind (Indic) /= N_Subtype_Indication
20396 loop
20397 Indic := Parent (Indic);
20398 end loop;
20400 if Present (Indic) then
20401 Def_Id := Etype (Subtype_Mark (Indic));
20403 Insert_Range_Checks
20404 (R_Checks,
20405 Insert_Node,
20406 Def_Id,
20407 Sloc (Insert_Node),
20409 Do_Before => True);
20410 end if;
20411 end;
20413 -- Insertion before a declaration. If the declaration
20414 -- includes discriminants, the list of applicable checks
20415 -- is given by the caller.
20417 elsif Nkind (Insert_Node) in N_Declaration then
20418 Def_Id := Defining_Identifier (Insert_Node);
20420 if (Ekind (Def_Id) = E_Record_Type
20421 and then Depends_On_Discriminant (R))
20422 or else
20423 (Ekind (Def_Id) = E_Protected_Type
20424 and then Has_Discriminants (Def_Id))
20425 then
20426 Append_Range_Checks
20427 (R_Checks,
20428 Check_List, Def_Id, Sloc (Insert_Node), R);
20430 else
20431 Insert_Range_Checks
20432 (R_Checks,
20433 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20435 end if;
20437 -- Insertion before a statement. Range appears in the
20438 -- context of a quantified expression. Insertion will
20439 -- take place when expression is expanded.
20441 else
20442 null;
20443 end if;
20444 end if;
20445 end if;
20446 end if;
20448 -- Case of other than an explicit N_Range node
20450 -- The forced evaluation removes side effects from expressions, which
20451 -- should occur also in GNATprove mode. Otherwise, we end up with
20452 -- unexpected insertions of actions at places where this is not
20453 -- supposed to occur, e.g. on default parameters of a call.
20455 elsif Expander_Active or GNATprove_Mode then
20456 Get_Index_Bounds (R, Lo, Hi);
20457 Force_Evaluation (Lo);
20458 Force_Evaluation (Hi);
20459 end if;
20460 end Process_Range_Expr_In_Decl;
20462 --------------------------------------
20463 -- Process_Real_Range_Specification --
20464 --------------------------------------
20466 procedure Process_Real_Range_Specification (Def : Node_Id) is
20467 Spec : constant Node_Id := Real_Range_Specification (Def);
20468 Lo : Node_Id;
20469 Hi : Node_Id;
20470 Err : Boolean := False;
20472 procedure Analyze_Bound (N : Node_Id);
20473 -- Analyze and check one bound
20475 -------------------
20476 -- Analyze_Bound --
20477 -------------------
20479 procedure Analyze_Bound (N : Node_Id) is
20480 begin
20481 Analyze_And_Resolve (N, Any_Real);
20483 if not Is_OK_Static_Expression (N) then
20484 Flag_Non_Static_Expr
20485 ("bound in real type definition is not static!", N);
20486 Err := True;
20487 end if;
20488 end Analyze_Bound;
20490 -- Start of processing for Process_Real_Range_Specification
20492 begin
20493 if Present (Spec) then
20494 Lo := Low_Bound (Spec);
20495 Hi := High_Bound (Spec);
20496 Analyze_Bound (Lo);
20497 Analyze_Bound (Hi);
20499 -- If error, clear away junk range specification
20501 if Err then
20502 Set_Real_Range_Specification (Def, Empty);
20503 end if;
20504 end if;
20505 end Process_Real_Range_Specification;
20507 ---------------------
20508 -- Process_Subtype --
20509 ---------------------
20511 function Process_Subtype
20512 (S : Node_Id;
20513 Related_Nod : Node_Id;
20514 Related_Id : Entity_Id := Empty;
20515 Suffix : Character := ' ') return Entity_Id
20517 P : Node_Id;
20518 Def_Id : Entity_Id;
20519 Error_Node : Node_Id;
20520 Full_View_Id : Entity_Id;
20521 Subtype_Mark_Id : Entity_Id;
20523 May_Have_Null_Exclusion : Boolean;
20525 procedure Check_Incomplete (T : Entity_Id);
20526 -- Called to verify that an incomplete type is not used prematurely
20528 ----------------------
20529 -- Check_Incomplete --
20530 ----------------------
20532 procedure Check_Incomplete (T : Entity_Id) is
20533 begin
20534 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20536 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20537 and then
20538 not (Ada_Version >= Ada_2005
20539 and then
20540 (Nkind (Parent (T)) = N_Subtype_Declaration
20541 or else (Nkind (Parent (T)) = N_Subtype_Indication
20542 and then Nkind (Parent (Parent (T))) =
20543 N_Subtype_Declaration)))
20544 then
20545 Error_Msg_N ("invalid use of type before its full declaration", T);
20546 end if;
20547 end Check_Incomplete;
20549 -- Start of processing for Process_Subtype
20551 begin
20552 -- Case of no constraints present
20554 if Nkind (S) /= N_Subtype_Indication then
20555 Find_Type (S);
20556 Check_Incomplete (S);
20557 P := Parent (S);
20559 -- Ada 2005 (AI-231): Static check
20561 if Ada_Version >= Ada_2005
20562 and then Present (P)
20563 and then Null_Exclusion_Present (P)
20564 and then Nkind (P) /= N_Access_To_Object_Definition
20565 and then not Is_Access_Type (Entity (S))
20566 then
20567 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20568 end if;
20570 -- The following is ugly, can't we have a range or even a flag???
20572 May_Have_Null_Exclusion :=
20573 Nkind_In (P, N_Access_Definition,
20574 N_Access_Function_Definition,
20575 N_Access_Procedure_Definition,
20576 N_Access_To_Object_Definition,
20577 N_Allocator,
20578 N_Component_Definition)
20579 or else
20580 Nkind_In (P, N_Derived_Type_Definition,
20581 N_Discriminant_Specification,
20582 N_Formal_Object_Declaration,
20583 N_Object_Declaration,
20584 N_Object_Renaming_Declaration,
20585 N_Parameter_Specification,
20586 N_Subtype_Declaration);
20588 -- Create an Itype that is a duplicate of Entity (S) but with the
20589 -- null-exclusion attribute.
20591 if May_Have_Null_Exclusion
20592 and then Is_Access_Type (Entity (S))
20593 and then Null_Exclusion_Present (P)
20595 -- No need to check the case of an access to object definition.
20596 -- It is correct to define double not-null pointers.
20598 -- Example:
20599 -- type Not_Null_Int_Ptr is not null access Integer;
20600 -- type Acc is not null access Not_Null_Int_Ptr;
20602 and then Nkind (P) /= N_Access_To_Object_Definition
20603 then
20604 if Can_Never_Be_Null (Entity (S)) then
20605 case Nkind (Related_Nod) is
20606 when N_Full_Type_Declaration =>
20607 if Nkind (Type_Definition (Related_Nod))
20608 in N_Array_Type_Definition
20609 then
20610 Error_Node :=
20611 Subtype_Indication
20612 (Component_Definition
20613 (Type_Definition (Related_Nod)));
20614 else
20615 Error_Node :=
20616 Subtype_Indication (Type_Definition (Related_Nod));
20617 end if;
20619 when N_Subtype_Declaration =>
20620 Error_Node := Subtype_Indication (Related_Nod);
20622 when N_Object_Declaration =>
20623 Error_Node := Object_Definition (Related_Nod);
20625 when N_Component_Declaration =>
20626 Error_Node :=
20627 Subtype_Indication (Component_Definition (Related_Nod));
20629 when N_Allocator =>
20630 Error_Node := Expression (Related_Nod);
20632 when others =>
20633 pragma Assert (False);
20634 Error_Node := Related_Nod;
20635 end case;
20637 Error_Msg_NE
20638 ("`NOT NULL` not allowed (& already excludes null)",
20639 Error_Node,
20640 Entity (S));
20641 end if;
20643 Set_Etype (S,
20644 Create_Null_Excluding_Itype
20645 (T => Entity (S),
20646 Related_Nod => P));
20647 Set_Entity (S, Etype (S));
20648 end if;
20650 return Entity (S);
20652 -- Case of constraint present, so that we have an N_Subtype_Indication
20653 -- node (this node is created only if constraints are present).
20655 else
20656 Find_Type (Subtype_Mark (S));
20658 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20659 and then not
20660 (Nkind (Parent (S)) = N_Subtype_Declaration
20661 and then Is_Itype (Defining_Identifier (Parent (S))))
20662 then
20663 Check_Incomplete (Subtype_Mark (S));
20664 end if;
20666 P := Parent (S);
20667 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20669 -- Explicit subtype declaration case
20671 if Nkind (P) = N_Subtype_Declaration then
20672 Def_Id := Defining_Identifier (P);
20674 -- Explicit derived type definition case
20676 elsif Nkind (P) = N_Derived_Type_Definition then
20677 Def_Id := Defining_Identifier (Parent (P));
20679 -- Implicit case, the Def_Id must be created as an implicit type.
20680 -- The one exception arises in the case of concurrent types, array
20681 -- and access types, where other subsidiary implicit types may be
20682 -- created and must appear before the main implicit type. In these
20683 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20684 -- has not yet been called to create Def_Id.
20686 else
20687 if Is_Array_Type (Subtype_Mark_Id)
20688 or else Is_Concurrent_Type (Subtype_Mark_Id)
20689 or else Is_Access_Type (Subtype_Mark_Id)
20690 then
20691 Def_Id := Empty;
20693 -- For the other cases, we create a new unattached Itype,
20694 -- and set the indication to ensure it gets attached later.
20696 else
20697 Def_Id :=
20698 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20699 end if;
20700 end if;
20702 -- If the kind of constraint is invalid for this kind of type,
20703 -- then give an error, and then pretend no constraint was given.
20705 if not Is_Valid_Constraint_Kind
20706 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20707 then
20708 Error_Msg_N
20709 ("incorrect constraint for this kind of type", Constraint (S));
20711 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20713 -- Set Ekind of orphan itype, to prevent cascaded errors
20715 if Present (Def_Id) then
20716 Set_Ekind (Def_Id, Ekind (Any_Type));
20717 end if;
20719 -- Make recursive call, having got rid of the bogus constraint
20721 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20722 end if;
20724 -- Remaining processing depends on type. Select on Base_Type kind to
20725 -- ensure getting to the concrete type kind in the case of a private
20726 -- subtype (needed when only doing semantic analysis).
20728 case Ekind (Base_Type (Subtype_Mark_Id)) is
20729 when Access_Kind =>
20731 -- If this is a constraint on a class-wide type, discard it.
20732 -- There is currently no way to express a partial discriminant
20733 -- constraint on a type with unknown discriminants. This is
20734 -- a pathology that the ACATS wisely decides not to test.
20736 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20737 if Comes_From_Source (S) then
20738 Error_Msg_N
20739 ("constraint on class-wide type ignored??",
20740 Constraint (S));
20741 end if;
20743 if Nkind (P) = N_Subtype_Declaration then
20744 Set_Subtype_Indication (P,
20745 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20746 end if;
20748 return Subtype_Mark_Id;
20749 end if;
20751 Constrain_Access (Def_Id, S, Related_Nod);
20753 if Expander_Active
20754 and then Is_Itype (Designated_Type (Def_Id))
20755 and then Nkind (Related_Nod) = N_Subtype_Declaration
20756 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20757 then
20758 Build_Itype_Reference
20759 (Designated_Type (Def_Id), Related_Nod);
20760 end if;
20762 when Array_Kind =>
20763 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20765 when Decimal_Fixed_Point_Kind =>
20766 Constrain_Decimal (Def_Id, S);
20768 when Enumeration_Kind =>
20769 Constrain_Enumeration (Def_Id, S);
20770 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20772 when Ordinary_Fixed_Point_Kind =>
20773 Constrain_Ordinary_Fixed (Def_Id, S);
20775 when Float_Kind =>
20776 Constrain_Float (Def_Id, S);
20778 when Integer_Kind =>
20779 Constrain_Integer (Def_Id, S);
20780 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20782 when E_Record_Type |
20783 E_Record_Subtype |
20784 Class_Wide_Kind |
20785 E_Incomplete_Type =>
20786 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20788 if Ekind (Def_Id) = E_Incomplete_Type then
20789 Set_Private_Dependents (Def_Id, New_Elmt_List);
20790 end if;
20792 when Private_Kind =>
20793 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20795 -- The base type may be private but Def_Id may be a full view
20796 -- in an instance.
20798 if Is_Private_Type (Def_Id) then
20799 Set_Private_Dependents (Def_Id, New_Elmt_List);
20800 end if;
20802 -- In case of an invalid constraint prevent further processing
20803 -- since the type constructed is missing expected fields.
20805 if Etype (Def_Id) = Any_Type then
20806 return Def_Id;
20807 end if;
20809 -- If the full view is that of a task with discriminants,
20810 -- we must constrain both the concurrent type and its
20811 -- corresponding record type. Otherwise we will just propagate
20812 -- the constraint to the full view, if available.
20814 if Present (Full_View (Subtype_Mark_Id))
20815 and then Has_Discriminants (Subtype_Mark_Id)
20816 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20817 then
20818 Full_View_Id :=
20819 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20821 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20822 Constrain_Concurrent (Full_View_Id, S,
20823 Related_Nod, Related_Id, Suffix);
20824 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20825 Set_Full_View (Def_Id, Full_View_Id);
20827 -- Introduce an explicit reference to the private subtype,
20828 -- to prevent scope anomalies in gigi if first use appears
20829 -- in a nested context, e.g. a later function body.
20830 -- Should this be generated in other contexts than a full
20831 -- type declaration?
20833 if Is_Itype (Def_Id)
20834 and then
20835 Nkind (Parent (P)) = N_Full_Type_Declaration
20836 then
20837 Build_Itype_Reference (Def_Id, Parent (P));
20838 end if;
20840 else
20841 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20842 end if;
20844 when Concurrent_Kind =>
20845 Constrain_Concurrent (Def_Id, S,
20846 Related_Nod, Related_Id, Suffix);
20848 when others =>
20849 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20850 end case;
20852 -- Size and Convention are always inherited from the base type
20854 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20855 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20857 return Def_Id;
20858 end if;
20859 end Process_Subtype;
20861 --------------------------------------------
20862 -- Propagate_Default_Init_Cond_Attributes --
20863 --------------------------------------------
20865 procedure Propagate_Default_Init_Cond_Attributes
20866 (From_Typ : Entity_Id;
20867 To_Typ : Entity_Id;
20868 Parent_To_Derivation : Boolean := False;
20869 Private_To_Full_View : Boolean := False)
20871 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20872 -- Remove the default initial procedure (if any) from the rep chain of
20873 -- type Typ.
20875 ----------------------------------------
20876 -- Remove_Default_Init_Cond_Procedure --
20877 ----------------------------------------
20879 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20880 Found : Boolean := False;
20881 Prev : Entity_Id;
20882 Subp : Entity_Id;
20884 begin
20885 Prev := Typ;
20886 Subp := Subprograms_For_Type (Typ);
20887 while Present (Subp) loop
20888 if Is_Default_Init_Cond_Procedure (Subp) then
20889 Found := True;
20890 exit;
20891 end if;
20893 Prev := Subp;
20894 Subp := Subprograms_For_Type (Subp);
20895 end loop;
20897 if Found then
20898 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20899 Set_Subprograms_For_Type (Subp, Empty);
20900 end if;
20901 end Remove_Default_Init_Cond_Procedure;
20903 -- Local variables
20905 Inherit_Procedure : Boolean := False;
20907 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20909 begin
20910 if Has_Default_Init_Cond (From_Typ) then
20912 -- A derived type inherits the attributes from its parent type
20914 if Parent_To_Derivation then
20915 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20917 -- A full view shares the attributes with its private view
20919 else
20920 Set_Has_Default_Init_Cond (To_Typ);
20921 end if;
20923 Inherit_Procedure := True;
20925 -- Due to the order of expansion, a derived private type is processed
20926 -- by two routines which both attempt to set the attributes related
20927 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20928 -- Process_Full_View.
20930 -- package Pack is
20931 -- type Parent_Typ is private
20932 -- with Default_Initial_Condition ...;
20933 -- private
20934 -- type Parent_Typ is ...;
20935 -- end Pack;
20937 -- with Pack; use Pack;
20938 -- package Pack_2 is
20939 -- type Deriv_Typ is private
20940 -- with Default_Initial_Condition ...;
20941 -- private
20942 -- type Deriv_Typ is new Parent_Typ;
20943 -- end Pack_2;
20945 -- When Build_Derived_Type operates, it sets the attributes on the
20946 -- full view without taking into account that the private view may
20947 -- define its own default initial condition procedure. This becomes
20948 -- apparent in Process_Full_View which must undo some of the work by
20949 -- Build_Derived_Type and propagate the attributes from the private
20950 -- to the full view.
20952 if Private_To_Full_View then
20953 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20954 Remove_Default_Init_Cond_Procedure (To_Typ);
20955 end if;
20957 -- A type must inherit the default initial condition procedure from a
20958 -- parent type when the parent itself is inheriting the procedure or
20959 -- when it is defining one. This circuitry is also used when dealing
20960 -- with the private / full view of a type.
20962 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20963 or (Parent_To_Derivation
20964 and Present (Get_Pragma
20965 (From_Typ, Pragma_Default_Initial_Condition)))
20966 then
20967 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20968 Inherit_Procedure := True;
20969 end if;
20971 if Inherit_Procedure
20972 and then No (Default_Init_Cond_Procedure (To_Typ))
20973 then
20974 Set_Default_Init_Cond_Procedure
20975 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20976 end if;
20977 end Propagate_Default_Init_Cond_Attributes;
20979 -----------------------------
20980 -- Record_Type_Declaration --
20981 -----------------------------
20983 procedure Record_Type_Declaration
20984 (T : Entity_Id;
20985 N : Node_Id;
20986 Prev : Entity_Id)
20988 Def : constant Node_Id := Type_Definition (N);
20989 Is_Tagged : Boolean;
20990 Tag_Comp : Entity_Id;
20992 begin
20993 -- These flags must be initialized before calling Process_Discriminants
20994 -- because this routine makes use of them.
20996 Set_Ekind (T, E_Record_Type);
20997 Set_Etype (T, T);
20998 Init_Size_Align (T);
20999 Set_Interfaces (T, No_Elist);
21000 Set_Stored_Constraint (T, No_Elist);
21001 Set_Default_SSO (T);
21003 -- Normal case
21005 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21006 if Limited_Present (Def) then
21007 Check_SPARK_05_Restriction ("limited is not allowed", N);
21008 end if;
21010 if Abstract_Present (Def) then
21011 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21012 end if;
21014 -- The flag Is_Tagged_Type might have already been set by
21015 -- Find_Type_Name if it detected an error for declaration T. This
21016 -- arises in the case of private tagged types where the full view
21017 -- omits the word tagged.
21019 Is_Tagged :=
21020 Tagged_Present (Def)
21021 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21023 Set_Is_Limited_Record (T, Limited_Present (Def));
21025 if Is_Tagged then
21026 Set_Is_Tagged_Type (T, True);
21027 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21028 end if;
21030 -- Type is abstract if full declaration carries keyword, or if
21031 -- previous partial view did.
21033 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21034 or else Abstract_Present (Def));
21036 else
21037 Check_SPARK_05_Restriction ("interface is not allowed", N);
21039 Is_Tagged := True;
21040 Analyze_Interface_Declaration (T, Def);
21042 if Present (Discriminant_Specifications (N)) then
21043 Error_Msg_N
21044 ("interface types cannot have discriminants",
21045 Defining_Identifier
21046 (First (Discriminant_Specifications (N))));
21047 end if;
21048 end if;
21050 -- First pass: if there are self-referential access components,
21051 -- create the required anonymous access type declarations, and if
21052 -- need be an incomplete type declaration for T itself.
21054 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21056 if Ada_Version >= Ada_2005
21057 and then Present (Interface_List (Def))
21058 then
21059 Check_Interfaces (N, Def);
21061 declare
21062 Ifaces_List : Elist_Id;
21064 begin
21065 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21066 -- already in the parents.
21068 Collect_Interfaces
21069 (T => T,
21070 Ifaces_List => Ifaces_List,
21071 Exclude_Parents => True);
21073 Set_Interfaces (T, Ifaces_List);
21074 end;
21075 end if;
21077 -- Records constitute a scope for the component declarations within.
21078 -- The scope is created prior to the processing of these declarations.
21079 -- Discriminants are processed first, so that they are visible when
21080 -- processing the other components. The Ekind of the record type itself
21081 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21083 -- Enter record scope
21085 Push_Scope (T);
21087 -- If an incomplete or private type declaration was already given for
21088 -- the type, then this scope already exists, and the discriminants have
21089 -- been declared within. We must verify that the full declaration
21090 -- matches the incomplete one.
21092 Check_Or_Process_Discriminants (N, T, Prev);
21094 Set_Is_Constrained (T, not Has_Discriminants (T));
21095 Set_Has_Delayed_Freeze (T, True);
21097 -- For tagged types add a manually analyzed component corresponding
21098 -- to the component _tag, the corresponding piece of tree will be
21099 -- expanded as part of the freezing actions if it is not a CPP_Class.
21101 if Is_Tagged then
21103 -- Do not add the tag unless we are in expansion mode
21105 if Expander_Active then
21106 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21107 Enter_Name (Tag_Comp);
21109 Set_Ekind (Tag_Comp, E_Component);
21110 Set_Is_Tag (Tag_Comp);
21111 Set_Is_Aliased (Tag_Comp);
21112 Set_Etype (Tag_Comp, RTE (RE_Tag));
21113 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21114 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21115 Init_Component_Location (Tag_Comp);
21117 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21118 -- implemented interfaces.
21120 if Has_Interfaces (T) then
21121 Add_Interface_Tag_Components (N, T);
21122 end if;
21123 end if;
21125 Make_Class_Wide_Type (T);
21126 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21127 end if;
21129 -- We must suppress range checks when processing record components in
21130 -- the presence of discriminants, since we don't want spurious checks to
21131 -- be generated during their analysis, but Suppress_Range_Checks flags
21132 -- must be reset the after processing the record definition.
21134 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21135 -- couldn't we just use the normal range check suppression method here.
21136 -- That would seem cleaner ???
21138 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21139 Set_Kill_Range_Checks (T, True);
21140 Record_Type_Definition (Def, Prev);
21141 Set_Kill_Range_Checks (T, False);
21142 else
21143 Record_Type_Definition (Def, Prev);
21144 end if;
21146 -- Exit from record scope
21148 End_Scope;
21150 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21151 -- the implemented interfaces and associate them an aliased entity.
21153 if Is_Tagged
21154 and then not Is_Empty_List (Interface_List (Def))
21155 then
21156 Derive_Progenitor_Subprograms (T, T);
21157 end if;
21159 Check_Function_Writable_Actuals (N);
21160 end Record_Type_Declaration;
21162 ----------------------------
21163 -- Record_Type_Definition --
21164 ----------------------------
21166 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21167 Component : Entity_Id;
21168 Ctrl_Components : Boolean := False;
21169 Final_Storage_Only : Boolean;
21170 T : Entity_Id;
21172 begin
21173 if Ekind (Prev_T) = E_Incomplete_Type then
21174 T := Full_View (Prev_T);
21175 else
21176 T := Prev_T;
21177 end if;
21179 -- In SPARK, tagged types and type extensions may only be declared in
21180 -- the specification of library unit packages.
21182 if Present (Def) and then Is_Tagged_Type (T) then
21183 declare
21184 Typ : Node_Id;
21185 Ctxt : Node_Id;
21187 begin
21188 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21189 Typ := Parent (Def);
21190 else
21191 pragma Assert
21192 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21193 Typ := Parent (Parent (Def));
21194 end if;
21196 Ctxt := Parent (Typ);
21198 if Nkind (Ctxt) = N_Package_Body
21199 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21200 then
21201 Check_SPARK_05_Restriction
21202 ("type should be defined in package specification", Typ);
21204 elsif Nkind (Ctxt) /= N_Package_Specification
21205 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21206 then
21207 Check_SPARK_05_Restriction
21208 ("type should be defined in library unit package", Typ);
21209 end if;
21210 end;
21211 end if;
21213 Final_Storage_Only := not Is_Controlled_Active (T);
21215 -- Ada 2005: Check whether an explicit Limited is present in a derived
21216 -- type declaration.
21218 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21219 and then Limited_Present (Parent (Def))
21220 then
21221 Set_Is_Limited_Record (T);
21222 end if;
21224 -- If the component list of a record type is defined by the reserved
21225 -- word null and there is no discriminant part, then the record type has
21226 -- no components and all records of the type are null records (RM 3.7)
21227 -- This procedure is also called to process the extension part of a
21228 -- record extension, in which case the current scope may have inherited
21229 -- components.
21231 if No (Def)
21232 or else No (Component_List (Def))
21233 or else Null_Present (Component_List (Def))
21234 then
21235 if not Is_Tagged_Type (T) then
21236 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21237 end if;
21239 else
21240 Analyze_Declarations (Component_Items (Component_List (Def)));
21242 if Present (Variant_Part (Component_List (Def))) then
21243 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21244 Analyze (Variant_Part (Component_List (Def)));
21245 end if;
21246 end if;
21248 -- After completing the semantic analysis of the record definition,
21249 -- record components, both new and inherited, are accessible. Set their
21250 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21251 -- whose Ekind may be void.
21253 Component := First_Entity (Current_Scope);
21254 while Present (Component) loop
21255 if Ekind (Component) = E_Void
21256 and then not Is_Itype (Component)
21257 then
21258 Set_Ekind (Component, E_Component);
21259 Init_Component_Location (Component);
21260 end if;
21262 if Has_Task (Etype (Component)) then
21263 Set_Has_Task (T);
21264 end if;
21266 if Has_Protected (Etype (Component)) then
21267 Set_Has_Protected (T);
21268 end if;
21270 if Ekind (Component) /= E_Component then
21271 null;
21273 -- Do not set Has_Controlled_Component on a class-wide equivalent
21274 -- type. See Make_CW_Equivalent_Type.
21276 elsif not Is_Class_Wide_Equivalent_Type (T)
21277 and then (Has_Controlled_Component (Etype (Component))
21278 or else (Chars (Component) /= Name_uParent
21279 and then Is_Controlled_Active
21280 (Etype (Component))))
21281 then
21282 Set_Has_Controlled_Component (T, True);
21283 Final_Storage_Only :=
21284 Final_Storage_Only
21285 and then Finalize_Storage_Only (Etype (Component));
21286 Ctrl_Components := True;
21287 end if;
21289 Next_Entity (Component);
21290 end loop;
21292 -- A Type is Finalize_Storage_Only only if all its controlled components
21293 -- are also.
21295 if Ctrl_Components then
21296 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21297 end if;
21299 -- Place reference to end record on the proper entity, which may
21300 -- be a partial view.
21302 if Present (Def) then
21303 Process_End_Label (Def, 'e', Prev_T);
21304 end if;
21305 end Record_Type_Definition;
21307 ------------------------
21308 -- Replace_Components --
21309 ------------------------
21311 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21312 function Process (N : Node_Id) return Traverse_Result;
21314 -------------
21315 -- Process --
21316 -------------
21318 function Process (N : Node_Id) return Traverse_Result is
21319 Comp : Entity_Id;
21321 begin
21322 if Nkind (N) = N_Discriminant_Specification then
21323 Comp := First_Discriminant (Typ);
21324 while Present (Comp) loop
21325 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21326 Set_Defining_Identifier (N, Comp);
21327 exit;
21328 end if;
21330 Next_Discriminant (Comp);
21331 end loop;
21333 elsif Nkind (N) = N_Component_Declaration then
21334 Comp := First_Component (Typ);
21335 while Present (Comp) loop
21336 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21337 Set_Defining_Identifier (N, Comp);
21338 exit;
21339 end if;
21341 Next_Component (Comp);
21342 end loop;
21343 end if;
21345 return OK;
21346 end Process;
21348 procedure Replace is new Traverse_Proc (Process);
21350 -- Start of processing for Replace_Components
21352 begin
21353 Replace (Decl);
21354 end Replace_Components;
21356 -------------------------------
21357 -- Set_Completion_Referenced --
21358 -------------------------------
21360 procedure Set_Completion_Referenced (E : Entity_Id) is
21361 begin
21362 -- If in main unit, mark entity that is a completion as referenced,
21363 -- warnings go on the partial view when needed.
21365 if In_Extended_Main_Source_Unit (E) then
21366 Set_Referenced (E);
21367 end if;
21368 end Set_Completion_Referenced;
21370 ---------------------
21371 -- Set_Default_SSO --
21372 ---------------------
21374 procedure Set_Default_SSO (T : Entity_Id) is
21375 begin
21376 case Opt.Default_SSO is
21377 when ' ' =>
21378 null;
21379 when 'L' =>
21380 Set_SSO_Set_Low_By_Default (T, True);
21381 when 'H' =>
21382 Set_SSO_Set_High_By_Default (T, True);
21383 when others =>
21384 raise Program_Error;
21385 end case;
21386 end Set_Default_SSO;
21388 ---------------------
21389 -- Set_Fixed_Range --
21390 ---------------------
21392 -- The range for fixed-point types is complicated by the fact that we
21393 -- do not know the exact end points at the time of the declaration. This
21394 -- is true for three reasons:
21396 -- A size clause may affect the fudging of the end-points.
21397 -- A small clause may affect the values of the end-points.
21398 -- We try to include the end-points if it does not affect the size.
21400 -- This means that the actual end-points must be established at the
21401 -- point when the type is frozen. Meanwhile, we first narrow the range
21402 -- as permitted (so that it will fit if necessary in a small specified
21403 -- size), and then build a range subtree with these narrowed bounds.
21404 -- Set_Fixed_Range constructs the range from real literal values, and
21405 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21407 -- The parent of this range is set to point to the entity so that it is
21408 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21409 -- other scalar types, which are just pointers to the range in the
21410 -- original tree, this would otherwise be an orphan).
21412 -- The tree is left unanalyzed. When the type is frozen, the processing
21413 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21414 -- analyzed, and uses this as an indication that it should complete
21415 -- work on the range (it will know the final small and size values).
21417 procedure Set_Fixed_Range
21418 (E : Entity_Id;
21419 Loc : Source_Ptr;
21420 Lo : Ureal;
21421 Hi : Ureal)
21423 S : constant Node_Id :=
21424 Make_Range (Loc,
21425 Low_Bound => Make_Real_Literal (Loc, Lo),
21426 High_Bound => Make_Real_Literal (Loc, Hi));
21427 begin
21428 Set_Scalar_Range (E, S);
21429 Set_Parent (S, E);
21431 -- Before the freeze point, the bounds of a fixed point are universal
21432 -- and carry the corresponding type.
21434 Set_Etype (Low_Bound (S), Universal_Real);
21435 Set_Etype (High_Bound (S), Universal_Real);
21436 end Set_Fixed_Range;
21438 ----------------------------------
21439 -- Set_Scalar_Range_For_Subtype --
21440 ----------------------------------
21442 procedure Set_Scalar_Range_For_Subtype
21443 (Def_Id : Entity_Id;
21444 R : Node_Id;
21445 Subt : Entity_Id)
21447 Kind : constant Entity_Kind := Ekind (Def_Id);
21449 begin
21450 -- Defend against previous error
21452 if Nkind (R) = N_Error then
21453 return;
21454 end if;
21456 Set_Scalar_Range (Def_Id, R);
21458 -- We need to link the range into the tree before resolving it so
21459 -- that types that are referenced, including importantly the subtype
21460 -- itself, are properly frozen (Freeze_Expression requires that the
21461 -- expression be properly linked into the tree). Of course if it is
21462 -- already linked in, then we do not disturb the current link.
21464 if No (Parent (R)) then
21465 Set_Parent (R, Def_Id);
21466 end if;
21468 -- Reset the kind of the subtype during analysis of the range, to
21469 -- catch possible premature use in the bounds themselves.
21471 Set_Ekind (Def_Id, E_Void);
21472 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21473 Set_Ekind (Def_Id, Kind);
21474 end Set_Scalar_Range_For_Subtype;
21476 --------------------------------------------------------
21477 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21478 --------------------------------------------------------
21480 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21481 (E : Entity_Id)
21483 begin
21484 -- Make sure set if encountered during Expand_To_Stored_Constraint
21486 Set_Stored_Constraint (E, No_Elist);
21488 -- Give it the right value
21490 if Is_Constrained (E) and then Has_Discriminants (E) then
21491 Set_Stored_Constraint (E,
21492 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21493 end if;
21494 end Set_Stored_Constraint_From_Discriminant_Constraint;
21496 -------------------------------------
21497 -- Signed_Integer_Type_Declaration --
21498 -------------------------------------
21500 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21501 Implicit_Base : Entity_Id;
21502 Base_Typ : Entity_Id;
21503 Lo_Val : Uint;
21504 Hi_Val : Uint;
21505 Errs : Boolean := False;
21506 Lo : Node_Id;
21507 Hi : Node_Id;
21509 function Can_Derive_From (E : Entity_Id) return Boolean;
21510 -- Determine whether given bounds allow derivation from specified type
21512 procedure Check_Bound (Expr : Node_Id);
21513 -- Check bound to make sure it is integral and static. If not, post
21514 -- appropriate error message and set Errs flag
21516 ---------------------
21517 -- Can_Derive_From --
21518 ---------------------
21520 -- Note we check both bounds against both end values, to deal with
21521 -- strange types like ones with a range of 0 .. -12341234.
21523 function Can_Derive_From (E : Entity_Id) return Boolean is
21524 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21525 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21526 begin
21527 return Lo <= Lo_Val and then Lo_Val <= Hi
21528 and then
21529 Lo <= Hi_Val and then Hi_Val <= Hi;
21530 end Can_Derive_From;
21532 -----------------
21533 -- Check_Bound --
21534 -----------------
21536 procedure Check_Bound (Expr : Node_Id) is
21537 begin
21538 -- If a range constraint is used as an integer type definition, each
21539 -- bound of the range must be defined by a static expression of some
21540 -- integer type, but the two bounds need not have the same integer
21541 -- type (Negative bounds are allowed.) (RM 3.5.4)
21543 if not Is_Integer_Type (Etype (Expr)) then
21544 Error_Msg_N
21545 ("integer type definition bounds must be of integer type", Expr);
21546 Errs := True;
21548 elsif not Is_OK_Static_Expression (Expr) then
21549 Flag_Non_Static_Expr
21550 ("non-static expression used for integer type bound!", Expr);
21551 Errs := True;
21553 -- The bounds are folded into literals, and we set their type to be
21554 -- universal, to avoid typing difficulties: we cannot set the type
21555 -- of the literal to the new type, because this would be a forward
21556 -- reference for the back end, and if the original type is user-
21557 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21559 else
21560 if Is_Entity_Name (Expr) then
21561 Fold_Uint (Expr, Expr_Value (Expr), True);
21562 end if;
21564 Set_Etype (Expr, Universal_Integer);
21565 end if;
21566 end Check_Bound;
21568 -- Start of processing for Signed_Integer_Type_Declaration
21570 begin
21571 -- Create an anonymous base type
21573 Implicit_Base :=
21574 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21576 -- Analyze and check the bounds, they can be of any integer type
21578 Lo := Low_Bound (Def);
21579 Hi := High_Bound (Def);
21581 -- Arbitrarily use Integer as the type if either bound had an error
21583 if Hi = Error or else Lo = Error then
21584 Base_Typ := Any_Integer;
21585 Set_Error_Posted (T, True);
21587 -- Here both bounds are OK expressions
21589 else
21590 Analyze_And_Resolve (Lo, Any_Integer);
21591 Analyze_And_Resolve (Hi, Any_Integer);
21593 Check_Bound (Lo);
21594 Check_Bound (Hi);
21596 if Errs then
21597 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21598 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21599 end if;
21601 -- Find type to derive from
21603 Lo_Val := Expr_Value (Lo);
21604 Hi_Val := Expr_Value (Hi);
21606 if Can_Derive_From (Standard_Short_Short_Integer) then
21607 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21609 elsif Can_Derive_From (Standard_Short_Integer) then
21610 Base_Typ := Base_Type (Standard_Short_Integer);
21612 elsif Can_Derive_From (Standard_Integer) then
21613 Base_Typ := Base_Type (Standard_Integer);
21615 elsif Can_Derive_From (Standard_Long_Integer) then
21616 Base_Typ := Base_Type (Standard_Long_Integer);
21618 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21619 Check_Restriction (No_Long_Long_Integers, Def);
21620 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21622 else
21623 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21624 Error_Msg_N ("integer type definition bounds out of range", Def);
21625 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21626 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21627 end if;
21628 end if;
21630 -- Complete both implicit base and declared first subtype entities. The
21631 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21632 -- are not clobbered when the signed integer type acts as a full view of
21633 -- a private type.
21635 Set_Etype (Implicit_Base, Base_Typ);
21636 Set_Size_Info (Implicit_Base, Base_Typ);
21637 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21638 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21639 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21641 Set_Ekind (T, E_Signed_Integer_Subtype);
21642 Set_Etype (T, Implicit_Base);
21643 Set_Size_Info (T, Implicit_Base);
21644 Inherit_Rep_Item_Chain (T, Implicit_Base);
21645 Set_Scalar_Range (T, Def);
21646 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21647 Set_Is_Constrained (T);
21648 end Signed_Integer_Type_Declaration;
21650 end Sem_Ch3;