* builtins.def (BUILT_IN_STACK_ALLOC): Remove.
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob670ee7656a304a222a75b9f3c0c8f897973afbdc
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-2004, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Elists; use Elists;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Dist; use Exp_Dist;
35 with Exp_Tss; use Exp_Tss;
36 with Exp_Util; use Exp_Util;
37 with Freeze; use Freeze;
38 with Itypes; use Itypes;
39 with Layout; use Layout;
40 with Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Case; use Sem_Case;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch7; use Sem_Ch7;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch13; use Sem_Ch13;
55 with Sem_Disp; use Sem_Disp;
56 with Sem_Dist; use Sem_Dist;
57 with Sem_Elim; use Sem_Elim;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Mech; use Sem_Mech;
60 with Sem_Res; use Sem_Res;
61 with Sem_Smem; use Sem_Smem;
62 with Sem_Type; use Sem_Type;
63 with Sem_Util; use Sem_Util;
64 with Sem_Warn; use Sem_Warn;
65 with Stand; use Stand;
66 with Sinfo; use Sinfo;
67 with Snames; use Snames;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Uintp; use Uintp;
71 with Urealp; use Urealp;
73 package body Sem_Ch3 is
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 procedure Build_Derived_Type
80 (N : Node_Id;
81 Parent_Type : Entity_Id;
82 Derived_Type : Entity_Id;
83 Is_Completion : Boolean;
84 Derive_Subps : Boolean := True);
85 -- Create and decorate a Derived_Type given the Parent_Type entity.
86 -- N is the N_Full_Type_Declaration node containing the derived type
87 -- definition. Parent_Type is the entity for the parent type in the derived
88 -- type definition and Derived_Type the actual derived type. Is_Completion
89 -- must be set to False if Derived_Type is the N_Defining_Identifier node
90 -- in N (ie Derived_Type = Defining_Identifier (N)). In this case N is not
91 -- the completion of a private type declaration. If Is_Completion is
92 -- set to True, N is the completion of a private type declaration and
93 -- Derived_Type is different from the defining identifier inside N (i.e.
94 -- Derived_Type /= Defining_Identifier (N)). Derive_Subps indicates whether
95 -- the parent subprograms should be derived. The only case where this
96 -- parameter is False is when Build_Derived_Type is recursively called to
97 -- process an implicit derived full type for a type derived from a private
98 -- type (in that case the subprograms must only be derived for the private
99 -- view of the type).
100 -- ??? These flags need a bit of re-examination and re-documentation:
101 -- ??? are they both necessary (both seem related to the recursion)?
103 procedure Build_Derived_Access_Type
104 (N : Node_Id;
105 Parent_Type : Entity_Id;
106 Derived_Type : Entity_Id);
107 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
108 -- create an implicit base if the parent type is constrained or if the
109 -- subtype indication has a constraint.
111 procedure Build_Derived_Array_Type
112 (N : Node_Id;
113 Parent_Type : Entity_Id;
114 Derived_Type : Entity_Id);
115 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
116 -- create an implicit base if the parent type is constrained or if the
117 -- subtype indication has a constraint.
119 procedure Build_Derived_Concurrent_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 task or pro-
124 -- tected type, inherit entries and protected subprograms, check legality
125 -- of discriminant constraints if any.
127 procedure Build_Derived_Enumeration_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 enumeration
132 -- type, we must create a new list of literals. Types derived from
133 -- Character and Wide_Character are special-cased.
135 procedure Build_Derived_Numeric_Type
136 (N : Node_Id;
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
140 -- an anonymous base type, and propagate constraint to subtype if needed.
142 procedure Build_Derived_Private_Type
143 (N : Node_Id;
144 Parent_Type : Entity_Id;
145 Derived_Type : Entity_Id;
146 Is_Completion : Boolean;
147 Derive_Subps : Boolean := True);
148 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
149 -- because the parent may or may not have a completion, and the derivation
150 -- may itself be a completion.
152 procedure Build_Derived_Record_Type
153 (N : Node_Id;
154 Parent_Type : Entity_Id;
155 Derived_Type : Entity_Id;
156 Derive_Subps : Boolean := True);
157 -- Subsidiary procedure to Build_Derived_Type and
158 -- Analyze_Private_Extension_Declaration used for tagged and untagged
159 -- record types. All parameters are as in Build_Derived_Type except that
160 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
161 -- N_Private_Extension_Declaration node. See the definition of this routine
162 -- for much more info. Derive_Subps indicates whether subprograms should
163 -- be derived from the parent type. The only case where Derive_Subps is
164 -- False is for an implicit derived full type for a type derived from a
165 -- private type (see Build_Derived_Type).
167 function Inherit_Components
168 (N : Node_Id;
169 Parent_Base : Entity_Id;
170 Derived_Base : Entity_Id;
171 Is_Tagged : Boolean;
172 Inherit_Discr : Boolean;
173 Discs : Elist_Id) return Elist_Id;
174 -- Called from Build_Derived_Record_Type to inherit the components of
175 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
176 -- For more information on derived types and component inheritance please
177 -- consult the comment above the body of Build_Derived_Record_Type.
179 -- N is the original derived type declaration.
181 -- Is_Tagged is set if we are dealing with tagged types.
183 -- If Inherit_Discr is set, Derived_Base inherits its discriminants
184 -- from Parent_Base, otherwise no discriminants are inherited.
186 -- Discs gives the list of constraints that apply to Parent_Base in the
187 -- derived type declaration. If Discs is set to No_Elist, then we have
188 -- the following situation:
190 -- type Parent (D1..Dn : ..) is [tagged] record ...;
191 -- type Derived is new Parent [with ...];
193 -- which gets treated as
195 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
197 -- For untagged types the returned value is an association list. The list
198 -- starts from the association (Parent_Base => Derived_Base), and then it
199 -- contains a sequence of the associations of the form
201 -- (Old_Component => New_Component),
203 -- where Old_Component is the Entity_Id of a component in Parent_Base
204 -- and New_Component is the Entity_Id of the corresponding component
205 -- in Derived_Base. For untagged records, this association list is
206 -- needed when copying the record declaration for the derived base.
207 -- In the tagged case the value returned is irrelevant.
209 procedure Build_Discriminal (Discrim : Entity_Id);
210 -- Create the discriminal corresponding to discriminant Discrim, that is
211 -- the parameter corresponding to Discrim to be used in initialization
212 -- procedures for the type where Discrim is a discriminant. Discriminals
213 -- are not used during semantic analysis, and are not fully defined
214 -- entities until expansion. Thus they are not given a scope until
215 -- initialization procedures are built.
217 function Build_Discriminant_Constraints
218 (T : Entity_Id;
219 Def : Node_Id;
220 Derived_Def : Boolean := False) return Elist_Id;
221 -- Validate discriminant constraints, and return the list of the
222 -- constraints in order of discriminant declarations. T is the
223 -- discriminated unconstrained type. Def is the N_Subtype_Indication
224 -- node where the discriminants constraints for T are specified.
225 -- Derived_Def is True if we are building the discriminant constraints
226 -- in a derived type definition of the form "type D (...) is new T (xxx)".
227 -- In this case T is the parent type and Def is the constraint "(xxx)" on
228 -- T and this routine sets the Corresponding_Discriminant field of the
229 -- discriminants in the derived type D to point to the corresponding
230 -- discriminants in the parent type T.
232 procedure Build_Discriminated_Subtype
233 (T : Entity_Id;
234 Def_Id : Entity_Id;
235 Elist : Elist_Id;
236 Related_Nod : Node_Id;
237 For_Access : Boolean := False);
238 -- Subsidiary procedure to Constrain_Discriminated_Type and to
239 -- Process_Incomplete_Dependents. Given
241 -- T (a possibly discriminated base type)
242 -- Def_Id (a very partially built subtype for T),
244 -- the call completes Def_Id to be the appropriate E_*_Subtype.
246 -- The Elist is the list of discriminant constraints if any (it is set to
247 -- No_Elist if T is not a discriminated type, and to an empty list if
248 -- T has discriminants but there are no discriminant constraints). The
249 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
250 -- The For_Access says whether or not this subtype is really constraining
251 -- an access type. That is its sole purpose is the designated type of an
252 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
253 -- is built to avoid freezing T when the access subtype is frozen.
255 function Build_Scalar_Bound
256 (Bound : Node_Id;
257 Par_T : Entity_Id;
258 Der_T : Entity_Id) return Node_Id;
259 -- The bounds of a derived scalar type are conversions of the bounds of
260 -- the parent type. Optimize the representation if the bounds are literals.
261 -- Needs a more complete spec--what are the parameters exactly, and what
262 -- exactly is the returned value, and how is Bound affected???
264 procedure Build_Underlying_Full_View
265 (N : Node_Id;
266 Typ : Entity_Id;
267 Par : Entity_Id);
268 -- If the completion of a private type is itself derived from a private
269 -- type, or if the full view of a private subtype is itself private, the
270 -- back-end has no way to compute the actual size of this type. We build
271 -- an internal subtype declaration of the proper parent type to convey
272 -- this information. This extra mechanism is needed because a full
273 -- view cannot itself have a full view (it would get clobbered during
274 -- view exchanges).
276 procedure Check_Access_Discriminant_Requires_Limited
277 (D : Node_Id;
278 Loc : Node_Id);
279 -- Check the restriction that the type to which an access discriminant
280 -- belongs must be a concurrent type or a descendant of a type with
281 -- the reserved word 'limited' in its declaration.
283 procedure Check_Delta_Expression (E : Node_Id);
284 -- Check that the expression represented by E is suitable for use
285 -- as a delta expression, i.e. it is of real type and is static.
287 procedure Check_Digits_Expression (E : Node_Id);
288 -- Check that the expression represented by E is suitable for use as
289 -- a digits expression, i.e. it is of integer type, positive and static.
291 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
292 -- Validate the initialization of an object declaration. T is the
293 -- required type, and Exp is the initialization expression.
295 procedure Check_Or_Process_Discriminants
296 (N : Node_Id;
297 T : Entity_Id;
298 Prev : Entity_Id := Empty);
299 -- If T is the full declaration of an incomplete or private type, check
300 -- the conformance of the discriminants, otherwise process them. Prev
301 -- is the entity of the partial declaration, if any.
303 procedure Check_Real_Bound (Bound : Node_Id);
304 -- Check given bound for being of real type and static. If not, post an
305 -- appropriate message, and rewrite the bound with the real literal zero.
307 procedure Constant_Redeclaration
308 (Id : Entity_Id;
309 N : Node_Id;
310 T : out Entity_Id);
311 -- Various checks on legality of full declaration of deferred constant.
312 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
313 -- node. The caller has not yet set any attributes of this entity.
315 procedure Convert_Scalar_Bounds
316 (N : Node_Id;
317 Parent_Type : Entity_Id;
318 Derived_Type : Entity_Id;
319 Loc : Source_Ptr);
320 -- For derived scalar types, convert the bounds in the type definition
321 -- to the derived type, and complete their analysis. Given a constraint
322 -- of the form:
323 -- .. new T range Lo .. Hi;
324 -- Lo and Hi are analyzed and resolved with T'Base, the parent_type.
325 -- The bounds of the derived type (the anonymous base) are copies of
326 -- Lo and Hi. Finally, the bounds of the derived subtype are conversions
327 -- of those bounds to the derived_type, so that their typing is
328 -- consistent.
330 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
331 -- Copies attributes from array base type T2 to array base type T1.
332 -- Copies only attributes that apply to base types, but not subtypes.
334 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
335 -- Copies attributes from array subtype T2 to array subtype T1. Copies
336 -- attributes that apply to both subtypes and base types.
338 procedure Create_Constrained_Components
339 (Subt : Entity_Id;
340 Decl_Node : Node_Id;
341 Typ : Entity_Id;
342 Constraints : Elist_Id);
343 -- Build the list of entities for a constrained discriminated record
344 -- subtype. If a component depends on a discriminant, replace its subtype
345 -- using the discriminant values in the discriminant constraint.
346 -- Subt is the defining identifier for the subtype whose list of
347 -- constrained entities we will create. Decl_Node is the type declaration
348 -- node where we will attach all the itypes created. Typ is the base
349 -- discriminated type for the subtype Subt. Constraints is the list of
350 -- discriminant constraints for Typ.
352 function Constrain_Component_Type
353 (Compon_Type : Entity_Id;
354 Constrained_Typ : Entity_Id;
355 Related_Node : Node_Id;
356 Typ : Entity_Id;
357 Constraints : Elist_Id) return Entity_Id;
358 -- Given a discriminated base type Typ, a list of discriminant constraint
359 -- Constraints for Typ and the type of a component of Typ, Compon_Type,
360 -- create and return the type corresponding to Compon_type where all
361 -- discriminant references are replaced with the corresponding
362 -- constraint. If no discriminant references occur in Compon_Typ then
363 -- return it as is. Constrained_Typ is the final constrained subtype to
364 -- which the constrained Compon_Type belongs. Related_Node is the node
365 -- where we will attach all the itypes created.
367 procedure Constrain_Access
368 (Def_Id : in out Entity_Id;
369 S : Node_Id;
370 Related_Nod : Node_Id);
371 -- Apply a list of constraints to an access type. If Def_Id is empty,
372 -- it is an anonymous type created for a subtype indication. In that
373 -- case it is created in the procedure and attached to Related_Nod.
375 procedure Constrain_Array
376 (Def_Id : in out Entity_Id;
377 SI : Node_Id;
378 Related_Nod : Node_Id;
379 Related_Id : Entity_Id;
380 Suffix : Character);
381 -- Apply a list of index constraints to an unconstrained array type. The
382 -- first parameter is the entity for the resulting subtype. A value of
383 -- Empty for Def_Id indicates that an implicit type must be created, but
384 -- creation is delayed (and must be done by this procedure) because other
385 -- subsidiary implicit types must be created first (which is why Def_Id
386 -- is an in/out parameter). The second parameter is a subtype indication
387 -- node for the constrained array to be created (e.g. something of the
388 -- form string (1 .. 10)). Related_Nod gives the place where this type
389 -- has to be inserted in the tree. The Related_Id and Suffix parameters
390 -- are used to build the associated Implicit type name.
392 procedure Constrain_Concurrent
393 (Def_Id : in out Entity_Id;
394 SI : Node_Id;
395 Related_Nod : Node_Id;
396 Related_Id : Entity_Id;
397 Suffix : Character);
398 -- Apply list of discriminant constraints to an unconstrained concurrent
399 -- type.
401 -- SI is the N_Subtype_Indication node containing the constraint and
402 -- the unconstrained type to constrain.
404 -- Def_Id is the entity for the resulting constrained subtype. A
405 -- value of Empty for Def_Id indicates that an implicit type must be
406 -- created, but creation is delayed (and must be done by this procedure)
407 -- because other subsidiary implicit types must be created first (which
408 -- is why Def_Id is an in/out parameter).
410 -- Related_Nod gives the place where this type has to be inserted
411 -- in the tree
413 -- The last two arguments are used to create its external name if needed.
415 function Constrain_Corresponding_Record
416 (Prot_Subt : Entity_Id;
417 Corr_Rec : Entity_Id;
418 Related_Nod : Node_Id;
419 Related_Id : Entity_Id) return Entity_Id;
420 -- When constraining a protected type or task type with discriminants,
421 -- constrain the corresponding record with the same discriminant values.
423 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
424 -- Constrain a decimal fixed point type with a digits constraint and/or a
425 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
427 procedure Constrain_Discriminated_Type
428 (Def_Id : Entity_Id;
429 S : Node_Id;
430 Related_Nod : Node_Id;
431 For_Access : Boolean := False);
432 -- Process discriminant constraints of composite type. Verify that values
433 -- have been provided for all discriminants, that the original type is
434 -- unconstrained, and that the types of the supplied expressions match
435 -- the discriminant types. The first three parameters are like in routine
436 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
437 -- of For_Access.
439 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
440 -- Constrain an enumeration type with a range constraint. This is
441 -- identical to Constrain_Integer, but for the Ekind of the
442 -- resulting subtype.
444 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
445 -- Constrain a floating point type with either a digits constraint
446 -- and/or a range constraint, building a E_Floating_Point_Subtype.
448 procedure Constrain_Index
449 (Index : Node_Id;
450 S : Node_Id;
451 Related_Nod : Node_Id;
452 Related_Id : Entity_Id;
453 Suffix : Character;
454 Suffix_Index : Nat);
455 -- Process an index constraint in a constrained array declaration.
456 -- The constraint can be a subtype name, or a range with or without
457 -- an explicit subtype mark. The index is the corresponding index of the
458 -- unconstrained array. The Related_Id and Suffix parameters are used to
459 -- build the associated Implicit type name.
461 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
462 -- Build subtype of a signed or modular integer type.
464 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
465 -- Constrain an ordinary fixed point type with a range constraint, and
466 -- build an E_Ordinary_Fixed_Point_Subtype entity.
468 procedure Copy_And_Swap (Priv, Full : Entity_Id);
469 -- Copy the Priv entity into the entity of its full declaration
470 -- then swap the two entities in such a manner that the former private
471 -- type is now seen as a full type.
473 procedure Decimal_Fixed_Point_Type_Declaration
474 (T : Entity_Id;
475 Def : Node_Id);
476 -- Create a new decimal fixed point type, and apply the constraint to
477 -- obtain a subtype of this new type.
479 procedure Complete_Private_Subtype
480 (Priv : Entity_Id;
481 Full : Entity_Id;
482 Full_Base : Entity_Id;
483 Related_Nod : Node_Id);
484 -- Complete the implicit full view of a private subtype by setting
485 -- the appropriate semantic fields. If the full view of the parent is
486 -- a record type, build constrained components of subtype.
488 procedure Derived_Standard_Character
489 (N : Node_Id;
490 Parent_Type : Entity_Id;
491 Derived_Type : Entity_Id);
492 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
493 -- derivations from types Standard.Character and Standard.Wide_Character.
495 procedure Derived_Type_Declaration
496 (T : Entity_Id;
497 N : Node_Id;
498 Is_Completion : Boolean);
499 -- Process a derived type declaration. This routine will invoke
500 -- Build_Derived_Type to process the actual derived type definition.
501 -- Parameters N and Is_Completion have the same meaning as in
502 -- Build_Derived_Type. T is the N_Defining_Identifier for the entity
503 -- defined in the N_Full_Type_Declaration node N, that is T is the
504 -- derived type.
506 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id;
507 -- Given a subtype indication S (which is really an N_Subtype_Indication
508 -- node or a plain N_Identifier), find the type of the subtype mark.
510 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
511 -- Insert each literal in symbol table, as an overloadable identifier
512 -- Each enumeration type is mapped into a sequence of integers, and
513 -- each literal is defined as a constant with integer value. If any
514 -- of the literals are character literals, the type is a character
515 -- type, which means that strings are legal aggregates for arrays of
516 -- components of the type.
518 function Expand_To_Stored_Constraint
519 (Typ : Entity_Id;
520 Constraint : Elist_Id) return Elist_Id;
521 -- Given a Constraint (ie a list of expressions) on the discriminants of
522 -- Typ, expand it into a constraint on the stored discriminants and
523 -- return the new list of expressions constraining the stored
524 -- discriminants.
526 function Find_Type_Of_Object
527 (Obj_Def : Node_Id;
528 Related_Nod : Node_Id) return Entity_Id;
529 -- Get type entity for object referenced by Obj_Def, attaching the
530 -- implicit types generated to Related_Nod
532 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
533 -- Create a new float, and apply the constraint to obtain subtype of it
535 function Has_Range_Constraint (N : Node_Id) return Boolean;
536 -- Given an N_Subtype_Indication node N, return True if a range constraint
537 -- is present, either directly, or as part of a digits or delta constraint.
538 -- In addition, a digits constraint in the decimal case returns True, since
539 -- it establishes a default range if no explicit range is present.
541 function Is_Valid_Constraint_Kind
542 (T_Kind : Type_Kind;
543 Constraint_Kind : Node_Kind) return Boolean;
544 -- Returns True if it is legal to apply the given kind of constraint
545 -- to the given kind of type (index constraint to an array type,
546 -- for example).
548 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
549 -- Create new modular type. Verify that modulus is in bounds and is
550 -- a power of two (implementation restriction).
552 procedure New_Concatenation_Op (Typ : Entity_Id);
553 -- Create an abbreviated declaration for an operator in order to
554 -- materialize concatenation on array types.
556 procedure Ordinary_Fixed_Point_Type_Declaration
557 (T : Entity_Id;
558 Def : Node_Id);
559 -- Create a new ordinary fixed point type, and apply the constraint
560 -- to obtain subtype of it.
562 procedure Prepare_Private_Subtype_Completion
563 (Id : Entity_Id;
564 Related_Nod : Node_Id);
565 -- Id is a subtype of some private type. Creates the full declaration
566 -- associated with Id whenever possible, i.e. when the full declaration
567 -- of the base type is already known. Records each subtype into
568 -- Private_Dependents of the base type.
570 procedure Process_Incomplete_Dependents
571 (N : Node_Id;
572 Full_T : Entity_Id;
573 Inc_T : Entity_Id);
574 -- Process all entities that depend on an incomplete type. There include
575 -- subtypes, subprogram types that mention the incomplete type in their
576 -- profiles, and subprogram with access parameters that designate the
577 -- incomplete type.
579 -- Inc_T is the defining identifier of an incomplete type declaration, its
580 -- Ekind is E_Incomplete_Type.
582 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
584 -- Full_T is N's defining identifier.
586 -- Subtypes of incomplete types with discriminants are completed when the
587 -- parent type is. This is simpler than private subtypes, because they can
588 -- only appear in the same scope, and there is no need to exchange views.
589 -- Similarly, access_to_subprogram types may have a parameter or a return
590 -- type that is an incomplete type, and that must be replaced with the
591 -- full type.
593 -- If the full type is tagged, subprogram with access parameters that
594 -- designated the incomplete may be primitive operations of the full type,
595 -- and have to be processed accordingly.
597 procedure Process_Real_Range_Specification (Def : Node_Id);
598 -- Given the type definition for a real type, this procedure processes
599 -- and checks the real range specification of this type definition if
600 -- one is present. If errors are found, error messages are posted, and
601 -- the Real_Range_Specification of Def is reset to Empty.
603 procedure Record_Type_Declaration
604 (T : Entity_Id;
605 N : Node_Id;
606 Prev : Entity_Id);
607 -- Process a record type declaration (for both untagged and tagged
608 -- records). Parameters T and N are exactly like in procedure
609 -- Derived_Type_Declaration, except that no flag Is_Completion is
610 -- needed for this routine. If this is the completion of an incomplete
611 -- type declaration, Prev is the entity of the incomplete declaration,
612 -- used for cross-referencing. Otherwise Prev = T.
614 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
615 -- This routine is used to process the actual record type definition
616 -- (both for untagged and tagged records). Def is a record type
617 -- definition node. This procedure analyzes the components in this
618 -- record type definition. Prev_T is the entity for the enclosing record
619 -- type. It is provided so that its Has_Task flag can be set if any of
620 -- the component have Has_Task set. If the declaration is the completion
621 -- of an incomplete type declaration, Prev_T is the original incomplete
622 -- type, whose full view is the record type.
624 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
625 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
626 -- build a copy of the declaration tree of the parent, and we create
627 -- independently the list of components for the derived type. Semantic
628 -- information uses the component entities, but record representation
629 -- clauses are validated on the declaration tree. This procedure replaces
630 -- discriminants and components in the declaration with those that have
631 -- been created by Inherit_Components.
633 procedure Set_Fixed_Range
634 (E : Entity_Id;
635 Loc : Source_Ptr;
636 Lo : Ureal;
637 Hi : Ureal);
638 -- Build a range node with the given bounds and set it as the Scalar_Range
639 -- of the given fixed-point type entity. Loc is the source location used
640 -- for the constructed range. See body for further details.
642 procedure Set_Scalar_Range_For_Subtype
643 (Def_Id : Entity_Id;
644 R : Node_Id;
645 Subt : Entity_Id);
646 -- This routine is used to set the scalar range field for a subtype
647 -- given Def_Id, the entity for the subtype, and R, the range expression
648 -- for the scalar range. Subt provides the parent subtype to be used
649 -- to analyze, resolve, and check the given range.
651 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
652 -- Create a new signed integer entity, and apply the constraint to obtain
653 -- the required first named subtype of this type.
655 procedure Set_Stored_Constraint_From_Discriminant_Constraint
656 (E : Entity_Id);
657 -- E is some record type. This routine computes E's Stored_Constraint
658 -- from its Discriminant_Constraint.
660 -----------------------
661 -- Access_Definition --
662 -----------------------
664 function Access_Definition
665 (Related_Nod : Node_Id;
666 N : Node_Id) return Entity_Id
668 Anon_Type : constant Entity_Id :=
669 Create_Itype (E_Anonymous_Access_Type, Related_Nod,
670 Scope_Id => Scope (Current_Scope));
671 Desig_Type : Entity_Id;
673 begin
674 if Is_Entry (Current_Scope)
675 and then Is_Task_Type (Etype (Scope (Current_Scope)))
676 then
677 Error_Msg_N ("task entries cannot have access parameters", N);
678 end if;
680 -- Ada 2005 (AI-254): In case of anonymous access to subprograms
681 -- call the corresponding semantic routine
683 if Present (Access_To_Subprogram_Definition (N)) then
684 Access_Subprogram_Declaration
685 (T_Name => Anon_Type,
686 T_Def => Access_To_Subprogram_Definition (N));
688 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
689 Set_Ekind
690 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
691 else
692 Set_Ekind
693 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
694 end if;
696 return Anon_Type;
697 end if;
699 Find_Type (Subtype_Mark (N));
700 Desig_Type := Entity (Subtype_Mark (N));
702 Set_Directly_Designated_Type
703 (Anon_Type, Desig_Type);
704 Set_Etype (Anon_Type, Anon_Type);
705 Init_Size_Align (Anon_Type);
706 Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
708 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
709 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify
710 -- if the null value is allowed. In Ada 95 the null value is never
711 -- allowed.
713 if Ada_Version >= Ada_05 then
714 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
715 else
716 Set_Can_Never_Be_Null (Anon_Type, True);
717 end if;
719 -- The anonymous access type is as public as the discriminated type or
720 -- subprogram that defines it. It is imported (for back-end purposes)
721 -- if the designated type is.
723 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
725 -- Ada 2005 (AI-50217): Propagate the attribute that indicates that the
726 -- designated type comes from the limited view (for back-end purposes).
728 Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
730 -- Ada 2005 (AI-231): Propagate the access-constant attribute
732 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
734 -- The context is either a subprogram declaration or an access
735 -- discriminant, in a private or a full type declaration. In
736 -- the case of a subprogram, If the designated type is incomplete,
737 -- the operation will be a primitive operation of the full type, to
738 -- be updated subsequently. If the type is imported through a limited
739 -- with clause, it is not a primitive operation of the type (which
740 -- is declared elsewhere in some other scope).
742 if Ekind (Desig_Type) = E_Incomplete_Type
743 and then not From_With_Type (Desig_Type)
744 and then Is_Overloadable (Current_Scope)
745 then
746 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
747 Set_Has_Delayed_Freeze (Current_Scope);
748 end if;
750 return Anon_Type;
751 end Access_Definition;
753 -----------------------------------
754 -- Access_Subprogram_Declaration --
755 -----------------------------------
757 procedure Access_Subprogram_Declaration
758 (T_Name : Entity_Id;
759 T_Def : Node_Id)
761 Formals : constant List_Id := Parameter_Specifications (T_Def);
762 Formal : Entity_Id;
764 Desig_Type : constant Entity_Id :=
765 Create_Itype (E_Subprogram_Type, Parent (T_Def));
767 begin
768 if Nkind (T_Def) = N_Access_Function_Definition then
769 Analyze (Subtype_Mark (T_Def));
770 Set_Etype (Desig_Type, Entity (Subtype_Mark (T_Def)));
772 if not (Is_Type (Etype (Desig_Type))) then
773 Error_Msg_N
774 ("expect type in function specification", Subtype_Mark (T_Def));
775 end if;
777 else
778 Set_Etype (Desig_Type, Standard_Void_Type);
779 end if;
781 if Present (Formals) then
782 New_Scope (Desig_Type);
783 Process_Formals (Formals, Parent (T_Def));
785 -- A bit of a kludge here, End_Scope requires that the parent
786 -- pointer be set to something reasonable, but Itypes don't
787 -- have parent pointers. So we set it and then unset it ???
788 -- If and when Itypes have proper parent pointers to their
789 -- declarations, this kludge can be removed.
791 Set_Parent (Desig_Type, T_Name);
792 End_Scope;
793 Set_Parent (Desig_Type, Empty);
794 end if;
796 -- The return type and/or any parameter type may be incomplete. Mark
797 -- the subprogram_type as depending on the incomplete type, so that
798 -- it can be updated when the full type declaration is seen.
800 if Present (Formals) then
801 Formal := First_Formal (Desig_Type);
803 while Present (Formal) loop
805 if Ekind (Formal) /= E_In_Parameter
806 and then Nkind (T_Def) = N_Access_Function_Definition
807 then
808 Error_Msg_N ("functions can only have IN parameters", Formal);
809 end if;
811 if Ekind (Etype (Formal)) = E_Incomplete_Type then
812 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
813 Set_Has_Delayed_Freeze (Desig_Type);
814 end if;
816 Next_Formal (Formal);
817 end loop;
818 end if;
820 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
821 and then not Has_Delayed_Freeze (Desig_Type)
822 then
823 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
824 Set_Has_Delayed_Freeze (Desig_Type);
825 end if;
827 Check_Delayed_Subprogram (Desig_Type);
829 if Protected_Present (T_Def) then
830 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
831 Set_Convention (Desig_Type, Convention_Protected);
832 else
833 Set_Ekind (T_Name, E_Access_Subprogram_Type);
834 end if;
836 Set_Etype (T_Name, T_Name);
837 Init_Size_Align (T_Name);
838 Set_Directly_Designated_Type (T_Name, Desig_Type);
840 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
842 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
844 Check_Restriction (No_Access_Subprograms, T_Def);
845 end Access_Subprogram_Declaration;
847 ----------------------------
848 -- Access_Type_Declaration --
849 ----------------------------
851 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
852 S : constant Node_Id := Subtype_Indication (Def);
853 P : constant Node_Id := Parent (Def);
855 Desig : Entity_Id;
856 -- Designated type
858 begin
859 -- Check for permissible use of incomplete type
861 if Nkind (S) /= N_Subtype_Indication then
862 Analyze (S);
864 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
865 Set_Directly_Designated_Type (T, Entity (S));
866 else
867 Set_Directly_Designated_Type (T,
868 Process_Subtype (S, P, T, 'P'));
869 end if;
871 else
872 Set_Directly_Designated_Type (T,
873 Process_Subtype (S, P, T, 'P'));
874 end if;
876 if All_Present (Def) or Constant_Present (Def) then
877 Set_Ekind (T, E_General_Access_Type);
878 else
879 Set_Ekind (T, E_Access_Type);
880 end if;
882 if Base_Type (Designated_Type (T)) = T then
883 Error_Msg_N ("access type cannot designate itself", S);
884 end if;
886 Set_Etype (T, T);
888 -- If the type has appeared already in a with_type clause, it is
889 -- frozen and the pointer size is already set. Else, initialize.
891 if not From_With_Type (T) then
892 Init_Size_Align (T);
893 end if;
895 Set_Is_Access_Constant (T, Constant_Present (Def));
897 Desig := Designated_Type (T);
899 -- If designated type is an imported tagged type, indicate that the
900 -- access type is also imported, and therefore restricted in its use.
901 -- The access type may already be imported, so keep setting otherwise.
903 -- Ada 2005 (AI-50217): If the non-limited view of the designated type
904 -- is available, use it as the designated type of the access type, so
905 -- that the back-end gets a usable entity.
907 declare
908 N_Desig : Entity_Id;
910 begin
911 if From_With_Type (Desig) then
912 Set_From_With_Type (T);
914 if Ekind (Desig) = E_Incomplete_Type then
915 N_Desig := Non_Limited_View (Desig);
917 else pragma Assert (Ekind (Desig) = E_Class_Wide_Type);
918 if From_With_Type (Etype (Desig)) then
919 N_Desig := Non_Limited_View (Etype (Desig));
920 else
921 N_Desig := Etype (Desig);
922 end if;
923 end if;
925 pragma Assert (Present (N_Desig));
926 Set_Directly_Designated_Type (T, N_Desig);
927 end if;
928 end;
930 -- Note that Has_Task is always false, since the access type itself
931 -- is not a task type. See Einfo for more description on this point.
932 -- Exactly the same consideration applies to Has_Controlled_Component.
934 Set_Has_Task (T, False);
935 Set_Has_Controlled_Component (T, False);
937 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
938 -- attributes
940 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
941 Set_Is_Access_Constant (T, Constant_Present (Def));
942 end Access_Type_Declaration;
944 -----------------------------------
945 -- Analyze_Component_Declaration --
946 -----------------------------------
948 procedure Analyze_Component_Declaration (N : Node_Id) is
949 Id : constant Entity_Id := Defining_Identifier (N);
950 T : Entity_Id;
951 P : Entity_Id;
953 function Contains_POC (Constr : Node_Id) return Boolean;
954 -- Determines whether a constraint uses the discriminant of a record
955 -- type thus becoming a per-object constraint (POC).
957 ------------------
958 -- Contains_POC --
959 ------------------
961 function Contains_POC (Constr : Node_Id) return Boolean is
962 begin
963 case Nkind (Constr) is
965 when N_Attribute_Reference =>
966 return Attribute_Name (Constr) = Name_Access
968 Prefix (Constr) = Scope (Entity (Prefix (Constr)));
970 when N_Discriminant_Association =>
971 return Denotes_Discriminant (Expression (Constr));
973 when N_Identifier =>
974 return Denotes_Discriminant (Constr);
976 when N_Index_Or_Discriminant_Constraint =>
977 declare
978 IDC : Node_Id := First (Constraints (Constr));
979 begin
980 while Present (IDC) loop
982 -- One per-object constraint is sufficent
984 if Contains_POC (IDC) then
985 return True;
986 end if;
988 Next (IDC);
989 end loop;
991 return False;
992 end;
994 when N_Range =>
995 return Denotes_Discriminant (Low_Bound (Constr))
997 Denotes_Discriminant (High_Bound (Constr));
999 when N_Range_Constraint =>
1000 return Denotes_Discriminant (Range_Expression (Constr));
1002 when others =>
1003 return False;
1005 end case;
1006 end Contains_POC;
1008 -- Start of processing for Analyze_Component_Declaration
1010 begin
1011 Generate_Definition (Id);
1012 Enter_Name (Id);
1014 if Present (Subtype_Indication (Component_Definition (N))) then
1015 T := Find_Type_Of_Object
1016 (Subtype_Indication (Component_Definition (N)), N);
1018 -- Ada 2005 (AI-230): Access Definition case
1020 else
1021 pragma Assert (Present
1022 (Access_Definition (Component_Definition (N))));
1024 T := Access_Definition
1025 (Related_Nod => N,
1026 N => Access_Definition (Component_Definition (N)));
1028 -- Ada 2005 (AI-230): In case of components that are anonymous
1029 -- access types the level of accessibility depends on the enclosing
1030 -- type declaration
1032 Set_Scope (T, Current_Scope); -- Ada 2005 (AI-230)
1034 -- Ada 2005 (AI-254)
1036 if Present (Access_To_Subprogram_Definition
1037 (Access_Definition (Component_Definition (N))))
1038 and then Protected_Present (Access_To_Subprogram_Definition
1039 (Access_Definition
1040 (Component_Definition (N))))
1041 then
1042 T := Replace_Anonymous_Access_To_Protected_Subprogram (N, T);
1043 end if;
1044 end if;
1046 -- If the subtype is a constrained subtype of the enclosing record,
1047 -- (which must have a partial view) the back-end does not handle
1048 -- properly the recursion. Rewrite the component declaration with
1049 -- an explicit subtype indication, which is acceptable to Gigi. We
1050 -- can copy the tree directly because side effects have already been
1051 -- removed from discriminant constraints.
1053 if Ekind (T) = E_Access_Subtype
1054 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1055 and then Comes_From_Source (T)
1056 and then Nkind (Parent (T)) = N_Subtype_Declaration
1057 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1058 then
1059 Rewrite
1060 (Subtype_Indication (Component_Definition (N)),
1061 New_Copy_Tree (Subtype_Indication (Parent (T))));
1062 T := Find_Type_Of_Object
1063 (Subtype_Indication (Component_Definition (N)), N);
1064 end if;
1066 -- If the component declaration includes a default expression, then we
1067 -- check that the component is not of a limited type (RM 3.7(5)),
1068 -- and do the special preanalysis of the expression (see section on
1069 -- "Handling of Default and Per-Object Expressions" in the spec of
1070 -- package Sem).
1072 if Present (Expression (N)) then
1073 Analyze_Per_Use_Expression (Expression (N), T);
1074 Check_Initialization (T, Expression (N));
1075 end if;
1077 -- The parent type may be a private view with unknown discriminants,
1078 -- and thus unconstrained. Regular components must be constrained.
1080 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1081 if Is_Class_Wide_Type (T) then
1082 Error_Msg_N
1083 ("class-wide subtype with unknown discriminants" &
1084 " in component declaration",
1085 Subtype_Indication (Component_Definition (N)));
1086 else
1087 Error_Msg_N
1088 ("unconstrained subtype in component declaration",
1089 Subtype_Indication (Component_Definition (N)));
1090 end if;
1092 -- Components cannot be abstract, except for the special case of
1093 -- the _Parent field (case of extending an abstract tagged type)
1095 elsif Is_Abstract (T) and then Chars (Id) /= Name_uParent then
1096 Error_Msg_N ("type of a component cannot be abstract", N);
1097 end if;
1099 Set_Etype (Id, T);
1100 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1102 -- The component declaration may have a per-object constraint, set the
1103 -- appropriate flag in the defining identifier of the subtype.
1105 if Present (Subtype_Indication (Component_Definition (N))) then
1106 declare
1107 Sindic : constant Node_Id :=
1108 Subtype_Indication (Component_Definition (N));
1110 begin
1111 if Nkind (Sindic) = N_Subtype_Indication
1112 and then Present (Constraint (Sindic))
1113 and then Contains_POC (Constraint (Sindic))
1114 then
1115 Set_Has_Per_Object_Constraint (Id);
1116 end if;
1117 end;
1118 end if;
1120 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1121 -- out some static checks
1123 if Ada_Version >= Ada_05
1124 and then (Null_Exclusion_Present (Component_Definition (N))
1125 or else Can_Never_Be_Null (T))
1126 then
1127 Set_Can_Never_Be_Null (Id);
1128 Null_Exclusion_Static_Checks (N);
1129 end if;
1131 -- If this component is private (or depends on a private type),
1132 -- flag the record type to indicate that some operations are not
1133 -- available.
1135 P := Private_Component (T);
1137 if Present (P) then
1138 -- Check for circular definitions.
1140 if P = Any_Type then
1141 Set_Etype (Id, Any_Type);
1143 -- There is a gap in the visibility of operations only if the
1144 -- component type is not defined in the scope of the record type.
1146 elsif Scope (P) = Scope (Current_Scope) then
1147 null;
1149 elsif Is_Limited_Type (P) then
1150 Set_Is_Limited_Composite (Current_Scope);
1152 else
1153 Set_Is_Private_Composite (Current_Scope);
1154 end if;
1155 end if;
1157 if P /= Any_Type
1158 and then Is_Limited_Type (T)
1159 and then Chars (Id) /= Name_uParent
1160 and then Is_Tagged_Type (Current_Scope)
1161 then
1162 if Is_Derived_Type (Current_Scope)
1163 and then not Is_Limited_Record (Root_Type (Current_Scope))
1164 then
1165 Error_Msg_N
1166 ("extension of nonlimited type cannot have limited components",
1168 Explain_Limited_Type (T, N);
1169 Set_Etype (Id, Any_Type);
1170 Set_Is_Limited_Composite (Current_Scope, False);
1172 elsif not Is_Derived_Type (Current_Scope)
1173 and then not Is_Limited_Record (Current_Scope)
1174 then
1175 Error_Msg_N
1176 ("nonlimited tagged type cannot have limited components", N);
1177 Explain_Limited_Type (T, N);
1178 Set_Etype (Id, Any_Type);
1179 Set_Is_Limited_Composite (Current_Scope, False);
1180 end if;
1181 end if;
1183 Set_Original_Record_Component (Id, Id);
1184 end Analyze_Component_Declaration;
1186 --------------------------
1187 -- Analyze_Declarations --
1188 --------------------------
1190 procedure Analyze_Declarations (L : List_Id) is
1191 D : Node_Id;
1192 Next_Node : Node_Id;
1193 Freeze_From : Entity_Id := Empty;
1195 procedure Adjust_D;
1196 -- Adjust D not to include implicit label declarations, since these
1197 -- have strange Sloc values that result in elaboration check problems.
1198 -- (They have the sloc of the label as found in the source, and that
1199 -- is ahead of the current declarative part).
1201 --------------
1202 -- Adjust_D --
1203 --------------
1205 procedure Adjust_D is
1206 begin
1207 while Present (Prev (D))
1208 and then Nkind (D) = N_Implicit_Label_Declaration
1209 loop
1210 Prev (D);
1211 end loop;
1212 end Adjust_D;
1214 -- Start of processing for Analyze_Declarations
1216 begin
1217 D := First (L);
1218 while Present (D) loop
1220 -- Complete analysis of declaration
1222 Analyze (D);
1223 Next_Node := Next (D);
1225 if No (Freeze_From) then
1226 Freeze_From := First_Entity (Current_Scope);
1227 end if;
1229 -- At the end of a declarative part, freeze remaining entities
1230 -- declared in it. The end of the visible declarations of a
1231 -- package specification is not the end of a declarative part
1232 -- if private declarations are present. The end of a package
1233 -- declaration is a freezing point only if it a library package.
1234 -- A task definition or protected type definition is not a freeze
1235 -- point either. Finally, we do not freeze entities in generic
1236 -- scopes, because there is no code generated for them and freeze
1237 -- nodes will be generated for the instance.
1239 -- The end of a package instantiation is not a freeze point, but
1240 -- for now we make it one, because the generic body is inserted
1241 -- (currently) immediately after. Generic instantiations will not
1242 -- be a freeze point once delayed freezing of bodies is implemented.
1243 -- (This is needed in any case for early instantiations ???).
1245 if No (Next_Node) then
1246 if Nkind (Parent (L)) = N_Component_List
1247 or else Nkind (Parent (L)) = N_Task_Definition
1248 or else Nkind (Parent (L)) = N_Protected_Definition
1249 then
1250 null;
1252 elsif Nkind (Parent (L)) /= N_Package_Specification then
1253 if Nkind (Parent (L)) = N_Package_Body then
1254 Freeze_From := First_Entity (Current_Scope);
1255 end if;
1257 Adjust_D;
1258 Freeze_All (Freeze_From, D);
1259 Freeze_From := Last_Entity (Current_Scope);
1261 elsif Scope (Current_Scope) /= Standard_Standard
1262 and then not Is_Child_Unit (Current_Scope)
1263 and then No (Generic_Parent (Parent (L)))
1264 then
1265 null;
1267 elsif L /= Visible_Declarations (Parent (L))
1268 or else No (Private_Declarations (Parent (L)))
1269 or else Is_Empty_List (Private_Declarations (Parent (L)))
1270 then
1271 Adjust_D;
1272 Freeze_All (Freeze_From, D);
1273 Freeze_From := Last_Entity (Current_Scope);
1274 end if;
1276 -- If next node is a body then freeze all types before the body.
1277 -- An exception occurs for expander generated bodies, which can
1278 -- be recognized by their already being analyzed. The expander
1279 -- ensures that all types needed by these bodies have been frozen
1280 -- but it is not necessary to freeze all types (and would be wrong
1281 -- since it would not correspond to an RM defined freeze point).
1283 elsif not Analyzed (Next_Node)
1284 and then (Nkind (Next_Node) = N_Subprogram_Body
1285 or else Nkind (Next_Node) = N_Entry_Body
1286 or else Nkind (Next_Node) = N_Package_Body
1287 or else Nkind (Next_Node) = N_Protected_Body
1288 or else Nkind (Next_Node) = N_Task_Body
1289 or else Nkind (Next_Node) in N_Body_Stub)
1290 then
1291 Adjust_D;
1292 Freeze_All (Freeze_From, D);
1293 Freeze_From := Last_Entity (Current_Scope);
1294 end if;
1296 D := Next_Node;
1297 end loop;
1298 end Analyze_Declarations;
1300 ----------------------------------
1301 -- Analyze_Incomplete_Type_Decl --
1302 ----------------------------------
1304 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1305 F : constant Boolean := Is_Pure (Current_Scope);
1306 T : Entity_Id;
1308 begin
1309 Generate_Definition (Defining_Identifier (N));
1311 -- Process an incomplete declaration. The identifier must not have been
1312 -- declared already in the scope. However, an incomplete declaration may
1313 -- appear in the private part of a package, for a private type that has
1314 -- already been declared.
1316 -- In this case, the discriminants (if any) must match
1318 T := Find_Type_Name (N);
1320 Set_Ekind (T, E_Incomplete_Type);
1321 Init_Size_Align (T);
1322 Set_Is_First_Subtype (T, True);
1323 Set_Etype (T, T);
1324 New_Scope (T);
1326 Set_Stored_Constraint (T, No_Elist);
1328 if Present (Discriminant_Specifications (N)) then
1329 Process_Discriminants (N);
1330 end if;
1332 End_Scope;
1334 -- If the type has discriminants, non-trivial subtypes may be
1335 -- be declared before the full view of the type. The full views
1336 -- of those subtypes will be built after the full view of the type.
1338 Set_Private_Dependents (T, New_Elmt_List);
1339 Set_Is_Pure (T, F);
1340 end Analyze_Incomplete_Type_Decl;
1342 -----------------------------
1343 -- Analyze_Itype_Reference --
1344 -----------------------------
1346 -- Nothing to do. This node is placed in the tree only for the benefit
1347 -- of Gigi processing, and has no effect on the semantic processing.
1349 procedure Analyze_Itype_Reference (N : Node_Id) is
1350 begin
1351 pragma Assert (Is_Itype (Itype (N)));
1352 null;
1353 end Analyze_Itype_Reference;
1355 --------------------------------
1356 -- Analyze_Number_Declaration --
1357 --------------------------------
1359 procedure Analyze_Number_Declaration (N : Node_Id) is
1360 Id : constant Entity_Id := Defining_Identifier (N);
1361 E : constant Node_Id := Expression (N);
1362 T : Entity_Id;
1363 Index : Interp_Index;
1364 It : Interp;
1366 begin
1367 Generate_Definition (Id);
1368 Enter_Name (Id);
1370 -- This is an optimization of a common case of an integer literal
1372 if Nkind (E) = N_Integer_Literal then
1373 Set_Is_Static_Expression (E, True);
1374 Set_Etype (E, Universal_Integer);
1376 Set_Etype (Id, Universal_Integer);
1377 Set_Ekind (Id, E_Named_Integer);
1378 Set_Is_Frozen (Id, True);
1379 return;
1380 end if;
1382 Set_Is_Pure (Id, Is_Pure (Current_Scope));
1384 -- Process expression, replacing error by integer zero, to avoid
1385 -- cascaded errors or aborts further along in the processing
1387 -- Replace Error by integer zero, which seems least likely to
1388 -- cause cascaded errors.
1390 if E = Error then
1391 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
1392 Set_Error_Posted (E);
1393 end if;
1395 Analyze (E);
1397 -- Verify that the expression is static and numeric. If
1398 -- the expression is overloaded, we apply the preference
1399 -- rule that favors root numeric types.
1401 if not Is_Overloaded (E) then
1402 T := Etype (E);
1404 else
1405 T := Any_Type;
1406 Get_First_Interp (E, Index, It);
1408 while Present (It.Typ) loop
1409 if (Is_Integer_Type (It.Typ)
1410 or else Is_Real_Type (It.Typ))
1411 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
1412 then
1413 if T = Any_Type then
1414 T := It.Typ;
1416 elsif It.Typ = Universal_Real
1417 or else It.Typ = Universal_Integer
1418 then
1419 -- Choose universal interpretation over any other.
1421 T := It.Typ;
1422 exit;
1423 end if;
1424 end if;
1426 Get_Next_Interp (Index, It);
1427 end loop;
1428 end if;
1430 if Is_Integer_Type (T) then
1431 Resolve (E, T);
1432 Set_Etype (Id, Universal_Integer);
1433 Set_Ekind (Id, E_Named_Integer);
1435 elsif Is_Real_Type (T) then
1437 -- Because the real value is converted to universal_real, this
1438 -- is a legal context for a universal fixed expression.
1440 if T = Universal_Fixed then
1441 declare
1442 Loc : constant Source_Ptr := Sloc (N);
1443 Conv : constant Node_Id := Make_Type_Conversion (Loc,
1444 Subtype_Mark =>
1445 New_Occurrence_Of (Universal_Real, Loc),
1446 Expression => Relocate_Node (E));
1448 begin
1449 Rewrite (E, Conv);
1450 Analyze (E);
1451 end;
1453 elsif T = Any_Fixed then
1454 Error_Msg_N ("illegal context for mixed mode operation", E);
1456 -- Expression is of the form : universal_fixed * integer.
1457 -- Try to resolve as universal_real.
1459 T := Universal_Real;
1460 Set_Etype (E, T);
1461 end if;
1463 Resolve (E, T);
1464 Set_Etype (Id, Universal_Real);
1465 Set_Ekind (Id, E_Named_Real);
1467 else
1468 Wrong_Type (E, Any_Numeric);
1469 Resolve (E, T);
1471 Set_Etype (Id, T);
1472 Set_Ekind (Id, E_Constant);
1473 Set_Never_Set_In_Source (Id, True);
1474 Set_Is_True_Constant (Id, True);
1475 return;
1476 end if;
1478 if Nkind (E) = N_Integer_Literal
1479 or else Nkind (E) = N_Real_Literal
1480 then
1481 Set_Etype (E, Etype (Id));
1482 end if;
1484 if not Is_OK_Static_Expression (E) then
1485 Flag_Non_Static_Expr
1486 ("non-static expression used in number declaration!", E);
1487 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
1488 Set_Etype (E, Any_Type);
1489 end if;
1490 end Analyze_Number_Declaration;
1492 --------------------------------
1493 -- Analyze_Object_Declaration --
1494 --------------------------------
1496 procedure Analyze_Object_Declaration (N : Node_Id) is
1497 Loc : constant Source_Ptr := Sloc (N);
1498 Id : constant Entity_Id := Defining_Identifier (N);
1499 T : Entity_Id;
1500 Act_T : Entity_Id;
1502 E : Node_Id := Expression (N);
1503 -- E is set to Expression (N) throughout this routine. When
1504 -- Expression (N) is modified, E is changed accordingly.
1506 Prev_Entity : Entity_Id := Empty;
1508 function Build_Default_Subtype return Entity_Id;
1509 -- If the object is limited or aliased, and if the type is unconstrained
1510 -- and there is no expression, the discriminants cannot be modified and
1511 -- the subtype of the object is constrained by the defaults, so it is
1512 -- worthile building the corresponding subtype.
1514 function Count_Tasks (T : Entity_Id) return Uint;
1515 -- This function is called when a library level object of type T
1516 -- is declared. It's function is to count the static number of
1517 -- tasks declared within the type (it is only called if Has_Tasks
1518 -- is set for T). As a side effect, if an array of tasks with
1519 -- non-static bounds or a variant record type is encountered,
1520 -- Check_Restrictions is called indicating the count is unknown.
1522 ---------------------------
1523 -- Build_Default_Subtype --
1524 ---------------------------
1526 function Build_Default_Subtype return Entity_Id is
1527 Constraints : constant List_Id := New_List;
1528 Act : Entity_Id;
1529 Decl : Node_Id;
1530 Disc : Entity_Id;
1532 begin
1533 Disc := First_Discriminant (T);
1535 if No (Discriminant_Default_Value (Disc)) then
1536 return T; -- previous error.
1537 end if;
1539 Act := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
1540 while Present (Disc) loop
1541 Append (
1542 New_Copy_Tree (
1543 Discriminant_Default_Value (Disc)), Constraints);
1544 Next_Discriminant (Disc);
1545 end loop;
1547 Decl :=
1548 Make_Subtype_Declaration (Loc,
1549 Defining_Identifier => Act,
1550 Subtype_Indication =>
1551 Make_Subtype_Indication (Loc,
1552 Subtype_Mark => New_Occurrence_Of (T, Loc),
1553 Constraint =>
1554 Make_Index_Or_Discriminant_Constraint
1555 (Loc, Constraints)));
1557 Insert_Before (N, Decl);
1558 Analyze (Decl);
1559 return Act;
1560 end Build_Default_Subtype;
1562 -----------------
1563 -- Count_Tasks --
1564 -----------------
1566 function Count_Tasks (T : Entity_Id) return Uint is
1567 C : Entity_Id;
1568 X : Node_Id;
1569 V : Uint;
1571 begin
1572 if Is_Task_Type (T) then
1573 return Uint_1;
1575 elsif Is_Record_Type (T) then
1576 if Has_Discriminants (T) then
1577 Check_Restriction (Max_Tasks, N);
1578 return Uint_0;
1580 else
1581 V := Uint_0;
1582 C := First_Component (T);
1583 while Present (C) loop
1584 V := V + Count_Tasks (Etype (C));
1585 Next_Component (C);
1586 end loop;
1588 return V;
1589 end if;
1591 elsif Is_Array_Type (T) then
1592 X := First_Index (T);
1593 V := Count_Tasks (Component_Type (T));
1594 while Present (X) loop
1595 C := Etype (X);
1597 if not Is_Static_Subtype (C) then
1598 Check_Restriction (Max_Tasks, N);
1599 return Uint_0;
1600 else
1601 V := V * (UI_Max (Uint_0,
1602 Expr_Value (Type_High_Bound (C)) -
1603 Expr_Value (Type_Low_Bound (C)) + Uint_1));
1604 end if;
1606 Next_Index (X);
1607 end loop;
1609 return V;
1611 else
1612 return Uint_0;
1613 end if;
1614 end Count_Tasks;
1616 -- Start of processing for Analyze_Object_Declaration
1618 begin
1619 -- There are three kinds of implicit types generated by an
1620 -- object declaration:
1622 -- 1. Those for generated by the original Object Definition
1624 -- 2. Those generated by the Expression
1626 -- 3. Those used to constrained the Object Definition with the
1627 -- expression constraints when it is unconstrained
1629 -- They must be generated in this order to avoid order of elaboration
1630 -- issues. Thus the first step (after entering the name) is to analyze
1631 -- the object definition.
1633 if Constant_Present (N) then
1634 Prev_Entity := Current_Entity_In_Scope (Id);
1636 -- If homograph is an implicit subprogram, it is overridden by the
1637 -- current declaration.
1639 if Present (Prev_Entity)
1640 and then Is_Overloadable (Prev_Entity)
1641 and then Is_Inherited_Operation (Prev_Entity)
1642 then
1643 Prev_Entity := Empty;
1644 end if;
1645 end if;
1647 if Present (Prev_Entity) then
1648 Constant_Redeclaration (Id, N, T);
1650 Generate_Reference (Prev_Entity, Id, 'c');
1651 Set_Completion_Referenced (Id);
1653 if Error_Posted (N) then
1654 -- Type mismatch or illegal redeclaration, Do not analyze
1655 -- expression to avoid cascaded errors.
1657 T := Find_Type_Of_Object (Object_Definition (N), N);
1658 Set_Etype (Id, T);
1659 Set_Ekind (Id, E_Variable);
1660 return;
1661 end if;
1663 -- In the normal case, enter identifier at the start to catch
1664 -- premature usage in the initialization expression.
1666 else
1667 Generate_Definition (Id);
1668 Enter_Name (Id);
1670 T := Find_Type_Of_Object (Object_Definition (N), N);
1672 if Error_Posted (Id) then
1673 Set_Etype (Id, T);
1674 Set_Ekind (Id, E_Variable);
1675 return;
1676 end if;
1677 end if;
1679 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1680 -- out some static checks
1682 if Ada_Version >= Ada_05
1683 and then (Null_Exclusion_Present (N)
1684 or else Can_Never_Be_Null (T))
1685 then
1686 Set_Can_Never_Be_Null (Id);
1687 Null_Exclusion_Static_Checks (N);
1688 end if;
1690 Set_Is_Pure (Id, Is_Pure (Current_Scope));
1692 -- If deferred constant, make sure context is appropriate. We detect
1693 -- a deferred constant as a constant declaration with no expression.
1694 -- A deferred constant can appear in a package body if its completion
1695 -- is by means of an interface pragma.
1697 if Constant_Present (N)
1698 and then No (E)
1699 then
1700 if not Is_Package (Current_Scope) then
1701 Error_Msg_N
1702 ("invalid context for deferred constant declaration ('R'M 7.4)",
1704 Error_Msg_N
1705 ("\declaration requires an initialization expression",
1707 Set_Constant_Present (N, False);
1709 -- In Ada 83, deferred constant must be of private type
1711 elsif not Is_Private_Type (T) then
1712 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1713 Error_Msg_N
1714 ("(Ada 83) deferred constant must be private type", N);
1715 end if;
1716 end if;
1718 -- If not a deferred constant, then object declaration freezes its type
1720 else
1721 Check_Fully_Declared (T, N);
1722 Freeze_Before (N, T);
1723 end if;
1725 -- If the object was created by a constrained array definition, then
1726 -- set the link in both the anonymous base type and anonymous subtype
1727 -- that are built to represent the array type to point to the object.
1729 if Nkind (Object_Definition (Declaration_Node (Id))) =
1730 N_Constrained_Array_Definition
1731 then
1732 Set_Related_Array_Object (T, Id);
1733 Set_Related_Array_Object (Base_Type (T), Id);
1734 end if;
1736 -- Special checks for protected objects not at library level
1738 if Is_Protected_Type (T)
1739 and then not Is_Library_Level_Entity (Id)
1740 then
1741 Check_Restriction (No_Local_Protected_Objects, Id);
1743 -- Protected objects with interrupt handlers must be at library level
1745 if Has_Interrupt_Handler (T) then
1746 Error_Msg_N
1747 ("interrupt object can only be declared at library level", Id);
1748 end if;
1749 end if;
1751 -- The actual subtype of the object is the nominal subtype, unless
1752 -- the nominal one is unconstrained and obtained from the expression.
1754 Act_T := T;
1756 -- Process initialization expression if present and not in error
1758 if Present (E) and then E /= Error then
1759 Analyze (E);
1761 -- In case of errors detected in the analysis of the expression,
1762 -- decorate it with the expected type to avoid cascade errors
1764 if not Present (Etype (E)) then
1765 Set_Etype (E, T);
1766 end if;
1768 -- If an initialization expression is present, then we set the
1769 -- Is_True_Constant flag. It will be reset if this is a variable
1770 -- and it is indeed modified.
1772 Set_Is_True_Constant (Id, True);
1774 -- If we are analyzing a constant declaration, set its completion
1775 -- flag after analyzing the expression.
1777 if Constant_Present (N) then
1778 Set_Has_Completion (Id);
1779 end if;
1781 if not Assignment_OK (N) then
1782 Check_Initialization (T, E);
1783 end if;
1785 Set_Etype (Id, T); -- may be overridden later on.
1786 Resolve (E, T);
1787 Check_Unset_Reference (E);
1789 if Compile_Time_Known_Value (E) then
1790 Set_Current_Value (Id, E);
1791 end if;
1793 -- Check incorrect use of dynamically tagged expressions. Note
1794 -- the use of Is_Tagged_Type (T) which seems redundant but is in
1795 -- fact important to avoid spurious errors due to expanded code
1796 -- for dispatching functions over an anonymous access type
1798 if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
1799 and then Is_Tagged_Type (T)
1800 and then not Is_Class_Wide_Type (T)
1801 then
1802 Error_Msg_N ("dynamically tagged expression not allowed!", E);
1803 end if;
1805 Apply_Scalar_Range_Check (E, T);
1806 Apply_Static_Length_Check (E, T);
1807 end if;
1809 -- Abstract type is never permitted for a variable or constant.
1810 -- Note: we inhibit this check for objects that do not come from
1811 -- source because there is at least one case (the expansion of
1812 -- x'class'input where x is abstract) where we legitimately
1813 -- generate an abstract object.
1815 if Is_Abstract (T) and then Comes_From_Source (N) then
1816 Error_Msg_N ("type of object cannot be abstract",
1817 Object_Definition (N));
1818 if Is_CPP_Class (T) then
1819 Error_Msg_NE ("\} may need a cpp_constructor",
1820 Object_Definition (N), T);
1821 end if;
1823 -- Case of unconstrained type
1825 elsif Is_Indefinite_Subtype (T) then
1827 -- Nothing to do in deferred constant case
1829 if Constant_Present (N) and then No (E) then
1830 null;
1832 -- Case of no initialization present
1834 elsif No (E) then
1835 if No_Initialization (N) then
1836 null;
1838 elsif Is_Class_Wide_Type (T) then
1839 Error_Msg_N
1840 ("initialization required in class-wide declaration ", N);
1842 else
1843 Error_Msg_N
1844 ("unconstrained subtype not allowed (need initialization)",
1845 Object_Definition (N));
1846 end if;
1848 -- Case of initialization present but in error. Set initial
1849 -- expression as absent (but do not make above complaints)
1851 elsif E = Error then
1852 Set_Expression (N, Empty);
1853 E := Empty;
1855 -- Case of initialization present
1857 else
1858 -- Not allowed in Ada 83
1860 if not Constant_Present (N) then
1861 if Ada_Version = Ada_83
1862 and then Comes_From_Source (Object_Definition (N))
1863 then
1864 Error_Msg_N
1865 ("(Ada 83) unconstrained variable not allowed",
1866 Object_Definition (N));
1867 end if;
1868 end if;
1870 -- Now we constrain the variable from the initializing expression
1872 -- If the expression is an aggregate, it has been expanded into
1873 -- individual assignments. Retrieve the actual type from the
1874 -- expanded construct.
1876 if Is_Array_Type (T)
1877 and then No_Initialization (N)
1878 and then Nkind (Original_Node (E)) = N_Aggregate
1879 then
1880 Act_T := Etype (E);
1882 else
1883 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
1884 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
1885 end if;
1887 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
1889 if Aliased_Present (N) then
1890 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
1891 end if;
1893 Freeze_Before (N, Act_T);
1894 Freeze_Before (N, T);
1895 end if;
1897 elsif Is_Array_Type (T)
1898 and then No_Initialization (N)
1899 and then Nkind (Original_Node (E)) = N_Aggregate
1900 then
1901 if not Is_Entity_Name (Object_Definition (N)) then
1902 Act_T := Etype (E);
1903 Check_Compile_Time_Size (Act_T);
1905 if Aliased_Present (N) then
1906 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
1907 end if;
1908 end if;
1910 -- When the given object definition and the aggregate are specified
1911 -- independently, and their lengths might differ do a length check.
1912 -- This cannot happen if the aggregate is of the form (others =>...)
1914 if not Is_Constrained (T) then
1915 null;
1917 elsif Nkind (E) = N_Raise_Constraint_Error then
1919 -- Aggregate is statically illegal. Place back in declaration
1921 Set_Expression (N, E);
1922 Set_No_Initialization (N, False);
1924 elsif T = Etype (E) then
1925 null;
1927 elsif Nkind (E) = N_Aggregate
1928 and then Present (Component_Associations (E))
1929 and then Present (Choices (First (Component_Associations (E))))
1930 and then Nkind (First
1931 (Choices (First (Component_Associations (E))))) = N_Others_Choice
1932 then
1933 null;
1935 else
1936 Apply_Length_Check (E, T);
1937 end if;
1939 elsif (Is_Limited_Record (T)
1940 or else Is_Concurrent_Type (T))
1941 and then not Is_Constrained (T)
1942 and then Has_Discriminants (T)
1943 then
1944 Act_T := Build_Default_Subtype;
1945 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
1947 elsif not Is_Constrained (T)
1948 and then Has_Discriminants (T)
1949 and then Constant_Present (N)
1950 and then Nkind (E) = N_Function_Call
1951 then
1952 -- The back-end has problems with constants of a discriminated type
1953 -- with defaults, if the initial value is a function call. We
1954 -- generate an intermediate temporary for the result of the call.
1955 -- It is unclear why this should make it acceptable to gcc. ???
1957 Remove_Side_Effects (E);
1958 end if;
1960 if T = Standard_Wide_Character
1961 or else Root_Type (T) = Standard_Wide_String
1962 then
1963 Check_Restriction (No_Wide_Characters, Object_Definition (N));
1964 end if;
1966 -- Now establish the proper kind and type of the object
1968 if Constant_Present (N) then
1969 Set_Ekind (Id, E_Constant);
1970 Set_Never_Set_In_Source (Id, True);
1971 Set_Is_True_Constant (Id, True);
1973 else
1974 Set_Ekind (Id, E_Variable);
1976 -- A variable is set as shared passive if it appears in a shared
1977 -- passive package, and is at the outer level. This is not done
1978 -- for entities generated during expansion, because those are
1979 -- always manipulated locally.
1981 if Is_Shared_Passive (Current_Scope)
1982 and then Is_Library_Level_Entity (Id)
1983 and then Comes_From_Source (Id)
1984 then
1985 Set_Is_Shared_Passive (Id);
1986 Check_Shared_Var (Id, T, N);
1987 end if;
1989 -- Case of no initializing expression present. If the type is not
1990 -- fully initialized, then we set Never_Set_In_Source, since this
1991 -- is a case of a potentially uninitialized object. Note that we
1992 -- do not consider access variables to be fully initialized for
1993 -- this purpose, since it still seems dubious if someone declares
1995 -- Note that we only do this for source declarations. If the object
1996 -- is declared by a generated declaration, we assume that it is not
1997 -- appropriate to generate warnings in that case.
1999 if No (E) then
2000 if (Is_Access_Type (T)
2001 or else not Is_Fully_Initialized_Type (T))
2002 and then Comes_From_Source (N)
2003 then
2004 Set_Never_Set_In_Source (Id);
2005 end if;
2006 end if;
2007 end if;
2009 Init_Alignment (Id);
2010 Init_Esize (Id);
2012 if Aliased_Present (N) then
2013 Set_Is_Aliased (Id);
2015 if No (E)
2016 and then Is_Record_Type (T)
2017 and then not Is_Constrained (T)
2018 and then Has_Discriminants (T)
2019 then
2020 Set_Actual_Subtype (Id, Build_Default_Subtype);
2021 end if;
2022 end if;
2024 Set_Etype (Id, Act_T);
2026 if Has_Controlled_Component (Etype (Id))
2027 or else Is_Controlled (Etype (Id))
2028 then
2029 if not Is_Library_Level_Entity (Id) then
2030 Check_Restriction (No_Nested_Finalization, N);
2032 else
2033 Validate_Controlled_Object (Id);
2034 end if;
2036 -- Generate a warning when an initialization causes an obvious
2037 -- ABE violation. If the init expression is a simple aggregate
2038 -- there shouldn't be any initialize/adjust call generated. This
2039 -- will be true as soon as aggregates are built in place when
2040 -- possible. ??? at the moment we do not generate warnings for
2041 -- temporaries created for those aggregates although a
2042 -- Program_Error might be generated if compiled with -gnato
2044 if Is_Controlled (Etype (Id))
2045 and then Comes_From_Source (Id)
2046 then
2047 declare
2048 BT : constant Entity_Id := Base_Type (Etype (Id));
2050 Implicit_Call : Entity_Id;
2051 pragma Warnings (Off, Implicit_Call);
2052 -- What is this about, it is never referenced ???
2054 function Is_Aggr (N : Node_Id) return Boolean;
2055 -- Check that N is an aggregate
2057 -------------
2058 -- Is_Aggr --
2059 -------------
2061 function Is_Aggr (N : Node_Id) return Boolean is
2062 begin
2063 case Nkind (Original_Node (N)) is
2064 when N_Aggregate | N_Extension_Aggregate =>
2065 return True;
2067 when N_Qualified_Expression |
2068 N_Type_Conversion |
2069 N_Unchecked_Type_Conversion =>
2070 return Is_Aggr (Expression (Original_Node (N)));
2072 when others =>
2073 return False;
2074 end case;
2075 end Is_Aggr;
2077 begin
2078 -- If no underlying type, we already are in an error situation
2079 -- don't try to add a warning since we do not have access
2080 -- prim-op list.
2082 if No (Underlying_Type (BT)) then
2083 Implicit_Call := Empty;
2085 -- A generic type does not have usable primitive operators.
2086 -- Initialization calls are built for instances.
2088 elsif Is_Generic_Type (BT) then
2089 Implicit_Call := Empty;
2091 -- if the init expression is not an aggregate, an adjust
2092 -- call will be generated
2094 elsif Present (E) and then not Is_Aggr (E) then
2095 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2097 -- if no init expression and we are not in the deferred
2098 -- constant case, an Initialize call will be generated
2100 elsif No (E) and then not Constant_Present (N) then
2101 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2103 else
2104 Implicit_Call := Empty;
2105 end if;
2106 end;
2107 end if;
2108 end if;
2110 if Has_Task (Etype (Id)) then
2111 Check_Restriction (No_Tasking, N);
2113 if Is_Library_Level_Entity (Id) then
2114 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2116 else
2117 Check_Restriction (Max_Tasks, N);
2118 Check_Restriction (No_Task_Hierarchy, N);
2119 Check_Potentially_Blocking_Operation (N);
2120 end if;
2122 -- A rather specialized test. If we see two tasks being declared
2123 -- of the same type in the same object declaration, and the task
2124 -- has an entry with an address clause, we know that program error
2125 -- will be raised at run-time since we can't have two tasks with
2126 -- entries at the same address.
2128 if Is_Task_Type (Etype (Id))
2129 and then More_Ids (N)
2130 then
2131 declare
2132 E : Entity_Id;
2134 begin
2135 E := First_Entity (Etype (Id));
2136 while Present (E) loop
2137 if Ekind (E) = E_Entry
2138 and then Present (Get_Attribute_Definition_Clause
2139 (E, Attribute_Address))
2140 then
2141 Error_Msg_N
2142 ("?more than one task with same entry address", N);
2143 Error_Msg_N
2144 ("\?Program_Error will be raised at run time", N);
2145 Insert_Action (N,
2146 Make_Raise_Program_Error (Loc,
2147 Reason => PE_Duplicated_Entry_Address));
2148 exit;
2149 end if;
2151 Next_Entity (E);
2152 end loop;
2153 end;
2154 end if;
2155 end if;
2157 -- Some simple constant-propagation: if the expression is a constant
2158 -- string initialized with a literal, share the literal. This avoids
2159 -- a run-time copy.
2161 if Present (E)
2162 and then Is_Entity_Name (E)
2163 and then Ekind (Entity (E)) = E_Constant
2164 and then Base_Type (Etype (E)) = Standard_String
2165 then
2166 declare
2167 Val : constant Node_Id := Constant_Value (Entity (E));
2169 begin
2170 if Present (Val)
2171 and then Nkind (Val) = N_String_Literal
2172 then
2173 Rewrite (E, New_Copy (Val));
2174 end if;
2175 end;
2176 end if;
2178 -- Another optimization: if the nominal subtype is unconstrained and
2179 -- the expression is a function call that returns an unconstrained
2180 -- type, rewrite the declaration as a renaming of the result of the
2181 -- call. The exceptions below are cases where the copy is expected,
2182 -- either by the back end (Aliased case) or by the semantics, as for
2183 -- initializing controlled types or copying tags for classwide types.
2185 if Present (E)
2186 and then Nkind (E) = N_Explicit_Dereference
2187 and then Nkind (Original_Node (E)) = N_Function_Call
2188 and then not Is_Library_Level_Entity (Id)
2189 and then not Is_Constrained (T)
2190 and then not Is_Aliased (Id)
2191 and then not Is_Class_Wide_Type (T)
2192 and then not Is_Controlled (T)
2193 and then not Has_Controlled_Component (Base_Type (T))
2194 and then Expander_Active
2195 then
2196 Rewrite (N,
2197 Make_Object_Renaming_Declaration (Loc,
2198 Defining_Identifier => Id,
2199 Access_Definition => Empty,
2200 Subtype_Mark => New_Occurrence_Of
2201 (Base_Type (Etype (Id)), Loc),
2202 Name => E));
2204 Set_Renamed_Object (Id, E);
2206 -- Force generation of debugging information for the constant
2207 -- and for the renamed function call.
2209 Set_Needs_Debug_Info (Id);
2210 Set_Needs_Debug_Info (Entity (Prefix (E)));
2211 end if;
2213 if Present (Prev_Entity)
2214 and then Is_Frozen (Prev_Entity)
2215 and then not Error_Posted (Id)
2216 then
2217 Error_Msg_N ("full constant declaration appears too late", N);
2218 end if;
2220 Check_Eliminated (Id);
2221 end Analyze_Object_Declaration;
2223 ---------------------------
2224 -- Analyze_Others_Choice --
2225 ---------------------------
2227 -- Nothing to do for the others choice node itself, the semantic analysis
2228 -- of the others choice will occur as part of the processing of the parent
2230 procedure Analyze_Others_Choice (N : Node_Id) is
2231 pragma Warnings (Off, N);
2233 begin
2234 null;
2235 end Analyze_Others_Choice;
2237 --------------------------------
2238 -- Analyze_Per_Use_Expression --
2239 --------------------------------
2241 procedure Analyze_Per_Use_Expression (N : Node_Id; T : Entity_Id) is
2242 Save_In_Default_Expression : constant Boolean := In_Default_Expression;
2244 begin
2245 In_Default_Expression := True;
2246 Pre_Analyze_And_Resolve (N, T);
2247 In_Default_Expression := Save_In_Default_Expression;
2248 end Analyze_Per_Use_Expression;
2250 -------------------------------------------
2251 -- Analyze_Private_Extension_Declaration --
2252 -------------------------------------------
2254 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
2255 T : constant Entity_Id := Defining_Identifier (N);
2256 Indic : constant Node_Id := Subtype_Indication (N);
2257 Parent_Type : Entity_Id;
2258 Parent_Base : Entity_Id;
2260 begin
2261 Generate_Definition (T);
2262 Enter_Name (T);
2264 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
2265 Parent_Base := Base_Type (Parent_Type);
2267 if Parent_Type = Any_Type
2268 or else Etype (Parent_Type) = Any_Type
2269 then
2270 Set_Ekind (T, Ekind (Parent_Type));
2271 Set_Etype (T, Any_Type);
2272 return;
2274 elsif not Is_Tagged_Type (Parent_Type) then
2275 Error_Msg_N
2276 ("parent of type extension must be a tagged type ", Indic);
2277 return;
2279 elsif Ekind (Parent_Type) = E_Void
2280 or else Ekind (Parent_Type) = E_Incomplete_Type
2281 then
2282 Error_Msg_N ("premature derivation of incomplete type", Indic);
2283 return;
2284 end if;
2286 -- Perhaps the parent type should be changed to the class-wide type's
2287 -- specific type in this case to prevent cascading errors ???
2289 if Is_Class_Wide_Type (Parent_Type) then
2290 Error_Msg_N
2291 ("parent of type extension must not be a class-wide type", Indic);
2292 return;
2293 end if;
2295 if (not Is_Package (Current_Scope)
2296 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
2297 or else In_Private_Part (Current_Scope)
2299 then
2300 Error_Msg_N ("invalid context for private extension", N);
2301 end if;
2303 -- Set common attributes
2305 Set_Is_Pure (T, Is_Pure (Current_Scope));
2306 Set_Scope (T, Current_Scope);
2307 Set_Ekind (T, E_Record_Type_With_Private);
2308 Init_Size_Align (T);
2310 Set_Etype (T, Parent_Base);
2311 Set_Has_Task (T, Has_Task (Parent_Base));
2313 Set_Convention (T, Convention (Parent_Type));
2314 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
2315 Set_Is_First_Subtype (T);
2316 Make_Class_Wide_Type (T);
2318 if Unknown_Discriminants_Present (N) then
2319 Set_Discriminant_Constraint (T, No_Elist);
2320 end if;
2322 Build_Derived_Record_Type (N, Parent_Type, T);
2323 end Analyze_Private_Extension_Declaration;
2325 ---------------------------------
2326 -- Analyze_Subtype_Declaration --
2327 ---------------------------------
2329 procedure Analyze_Subtype_Declaration (N : Node_Id) is
2330 Id : constant Entity_Id := Defining_Identifier (N);
2331 T : Entity_Id;
2332 R_Checks : Check_Result;
2334 begin
2335 Generate_Definition (Id);
2336 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2337 Init_Size_Align (Id);
2339 -- The following guard condition on Enter_Name is to handle cases
2340 -- where the defining identifier has already been entered into the
2341 -- scope but the declaration as a whole needs to be analyzed.
2343 -- This case in particular happens for derived enumeration types.
2344 -- The derived enumeration type is processed as an inserted enumeration
2345 -- type declaration followed by a rewritten subtype declaration. The
2346 -- defining identifier, however, is entered into the name scope very
2347 -- early in the processing of the original type declaration and
2348 -- therefore needs to be avoided here, when the created subtype
2349 -- declaration is analyzed. (See Build_Derived_Types)
2351 -- This also happens when the full view of a private type is a
2352 -- derived type with constraints. In this case the entity has been
2353 -- introduced in the private declaration.
2355 if Present (Etype (Id))
2356 and then (Is_Private_Type (Etype (Id))
2357 or else Is_Task_Type (Etype (Id))
2358 or else Is_Rewrite_Substitution (N))
2359 then
2360 null;
2362 else
2363 Enter_Name (Id);
2364 end if;
2366 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
2368 -- Inherit common attributes
2370 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
2371 Set_Is_Volatile (Id, Is_Volatile (T));
2372 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
2373 Set_Is_Atomic (Id, Is_Atomic (T));
2375 -- In the case where there is no constraint given in the subtype
2376 -- indication, Process_Subtype just returns the Subtype_Mark,
2377 -- so its semantic attributes must be established here.
2379 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
2380 Set_Etype (Id, Base_Type (T));
2382 case Ekind (T) is
2383 when Array_Kind =>
2384 Set_Ekind (Id, E_Array_Subtype);
2385 Copy_Array_Subtype_Attributes (Id, T);
2387 when Decimal_Fixed_Point_Kind =>
2388 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
2389 Set_Digits_Value (Id, Digits_Value (T));
2390 Set_Delta_Value (Id, Delta_Value (T));
2391 Set_Scale_Value (Id, Scale_Value (T));
2392 Set_Small_Value (Id, Small_Value (T));
2393 Set_Scalar_Range (Id, Scalar_Range (T));
2394 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
2395 Set_Is_Constrained (Id, Is_Constrained (T));
2396 Set_RM_Size (Id, RM_Size (T));
2398 when Enumeration_Kind =>
2399 Set_Ekind (Id, E_Enumeration_Subtype);
2400 Set_First_Literal (Id, First_Literal (Base_Type (T)));
2401 Set_Scalar_Range (Id, Scalar_Range (T));
2402 Set_Is_Character_Type (Id, Is_Character_Type (T));
2403 Set_Is_Constrained (Id, Is_Constrained (T));
2404 Set_RM_Size (Id, RM_Size (T));
2406 when Ordinary_Fixed_Point_Kind =>
2407 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
2408 Set_Scalar_Range (Id, Scalar_Range (T));
2409 Set_Small_Value (Id, Small_Value (T));
2410 Set_Delta_Value (Id, Delta_Value (T));
2411 Set_Is_Constrained (Id, Is_Constrained (T));
2412 Set_RM_Size (Id, RM_Size (T));
2414 when Float_Kind =>
2415 Set_Ekind (Id, E_Floating_Point_Subtype);
2416 Set_Scalar_Range (Id, Scalar_Range (T));
2417 Set_Digits_Value (Id, Digits_Value (T));
2418 Set_Is_Constrained (Id, Is_Constrained (T));
2420 when Signed_Integer_Kind =>
2421 Set_Ekind (Id, E_Signed_Integer_Subtype);
2422 Set_Scalar_Range (Id, Scalar_Range (T));
2423 Set_Is_Constrained (Id, Is_Constrained (T));
2424 Set_RM_Size (Id, RM_Size (T));
2426 when Modular_Integer_Kind =>
2427 Set_Ekind (Id, E_Modular_Integer_Subtype);
2428 Set_Scalar_Range (Id, Scalar_Range (T));
2429 Set_Is_Constrained (Id, Is_Constrained (T));
2430 Set_RM_Size (Id, RM_Size (T));
2432 when Class_Wide_Kind =>
2433 Set_Ekind (Id, E_Class_Wide_Subtype);
2434 Set_First_Entity (Id, First_Entity (T));
2435 Set_Last_Entity (Id, Last_Entity (T));
2436 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
2437 Set_Cloned_Subtype (Id, T);
2438 Set_Is_Tagged_Type (Id, True);
2439 Set_Has_Unknown_Discriminants
2440 (Id, True);
2442 if Ekind (T) = E_Class_Wide_Subtype then
2443 Set_Equivalent_Type (Id, Equivalent_Type (T));
2444 end if;
2446 when E_Record_Type | E_Record_Subtype =>
2447 Set_Ekind (Id, E_Record_Subtype);
2449 if Ekind (T) = E_Record_Subtype
2450 and then Present (Cloned_Subtype (T))
2451 then
2452 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
2453 else
2454 Set_Cloned_Subtype (Id, T);
2455 end if;
2457 Set_First_Entity (Id, First_Entity (T));
2458 Set_Last_Entity (Id, Last_Entity (T));
2459 Set_Has_Discriminants (Id, Has_Discriminants (T));
2460 Set_Is_Constrained (Id, Is_Constrained (T));
2461 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
2462 Set_Has_Unknown_Discriminants
2463 (Id, Has_Unknown_Discriminants (T));
2465 if Has_Discriminants (T) then
2466 Set_Discriminant_Constraint
2467 (Id, Discriminant_Constraint (T));
2468 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2470 elsif Has_Unknown_Discriminants (Id) then
2471 Set_Discriminant_Constraint (Id, No_Elist);
2472 end if;
2474 if Is_Tagged_Type (T) then
2475 Set_Is_Tagged_Type (Id);
2476 Set_Is_Abstract (Id, Is_Abstract (T));
2477 Set_Primitive_Operations
2478 (Id, Primitive_Operations (T));
2479 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
2480 end if;
2482 when Private_Kind =>
2483 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
2484 Set_Has_Discriminants (Id, Has_Discriminants (T));
2485 Set_Is_Constrained (Id, Is_Constrained (T));
2486 Set_First_Entity (Id, First_Entity (T));
2487 Set_Last_Entity (Id, Last_Entity (T));
2488 Set_Private_Dependents (Id, New_Elmt_List);
2489 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
2490 Set_Has_Unknown_Discriminants
2491 (Id, Has_Unknown_Discriminants (T));
2493 if Is_Tagged_Type (T) then
2494 Set_Is_Tagged_Type (Id);
2495 Set_Is_Abstract (Id, Is_Abstract (T));
2496 Set_Primitive_Operations
2497 (Id, Primitive_Operations (T));
2498 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
2499 end if;
2501 -- In general the attributes of the subtype of a private
2502 -- type are the attributes of the partial view of parent.
2503 -- However, the full view may be a discriminated type,
2504 -- and the subtype must share the discriminant constraint
2505 -- to generate correct calls to initialization procedures.
2507 if Has_Discriminants (T) then
2508 Set_Discriminant_Constraint
2509 (Id, Discriminant_Constraint (T));
2510 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2512 elsif Present (Full_View (T))
2513 and then Has_Discriminants (Full_View (T))
2514 then
2515 Set_Discriminant_Constraint
2516 (Id, Discriminant_Constraint (Full_View (T)));
2517 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2519 -- This would seem semantically correct, but apparently
2520 -- confuses the back-end (4412-009). To be explained ???
2522 -- Set_Has_Discriminants (Id);
2523 end if;
2525 Prepare_Private_Subtype_Completion (Id, N);
2527 when Access_Kind =>
2528 Set_Ekind (Id, E_Access_Subtype);
2529 Set_Is_Constrained (Id, Is_Constrained (T));
2530 Set_Is_Access_Constant
2531 (Id, Is_Access_Constant (T));
2532 Set_Directly_Designated_Type
2533 (Id, Designated_Type (T));
2535 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
2536 -- and carry out some static checks
2538 if Null_Exclusion_Present (N)
2539 or else Can_Never_Be_Null (T)
2540 then
2541 Set_Can_Never_Be_Null (Id);
2543 if Null_Exclusion_Present (N)
2544 and then Can_Never_Be_Null (T)
2545 then
2546 Error_Msg_N
2547 ("(Ada 2005) null exclusion not allowed if parent "
2548 & "is already non-null", Subtype_Indication (N));
2549 end if;
2550 end if;
2552 -- A Pure library_item must not contain the declaration of a
2553 -- named access type, except within a subprogram, generic
2554 -- subprogram, task unit, or protected unit (RM 10.2.1(16)).
2556 if Comes_From_Source (Id)
2557 and then In_Pure_Unit
2558 and then not In_Subprogram_Task_Protected_Unit
2559 then
2560 Error_Msg_N
2561 ("named access types not allowed in pure unit", N);
2562 end if;
2564 when Concurrent_Kind =>
2565 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
2566 Set_Corresponding_Record_Type (Id,
2567 Corresponding_Record_Type (T));
2568 Set_First_Entity (Id, First_Entity (T));
2569 Set_First_Private_Entity (Id, First_Private_Entity (T));
2570 Set_Has_Discriminants (Id, Has_Discriminants (T));
2571 Set_Is_Constrained (Id, Is_Constrained (T));
2572 Set_Last_Entity (Id, Last_Entity (T));
2574 if Has_Discriminants (T) then
2575 Set_Discriminant_Constraint (Id,
2576 Discriminant_Constraint (T));
2577 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2578 end if;
2580 -- If the subtype name denotes an incomplete type
2581 -- an error was already reported by Process_Subtype.
2583 when E_Incomplete_Type =>
2584 Set_Etype (Id, Any_Type);
2586 when others =>
2587 raise Program_Error;
2588 end case;
2589 end if;
2591 if Etype (Id) = Any_Type then
2592 return;
2593 end if;
2595 -- Some common processing on all types
2597 Set_Size_Info (Id, T);
2598 Set_First_Rep_Item (Id, First_Rep_Item (T));
2600 T := Etype (Id);
2602 Set_Is_Immediately_Visible (Id, True);
2603 Set_Depends_On_Private (Id, Has_Private_Component (T));
2605 if Present (Generic_Parent_Type (N))
2606 and then
2607 (Nkind
2608 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
2609 or else Nkind
2610 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
2611 /= N_Formal_Private_Type_Definition)
2612 then
2613 if Is_Tagged_Type (Id) then
2614 if Is_Class_Wide_Type (Id) then
2615 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
2616 else
2617 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
2618 end if;
2620 elsif Scope (Etype (Id)) /= Standard_Standard then
2621 Derive_Subprograms (Generic_Parent_Type (N), Id);
2622 end if;
2623 end if;
2625 if Is_Private_Type (T)
2626 and then Present (Full_View (T))
2627 then
2628 Conditional_Delay (Id, Full_View (T));
2630 -- The subtypes of components or subcomponents of protected types
2631 -- do not need freeze nodes, which would otherwise appear in the
2632 -- wrong scope (before the freeze node for the protected type). The
2633 -- proper subtypes are those of the subcomponents of the corresponding
2634 -- record.
2636 elsif Ekind (Scope (Id)) /= E_Protected_Type
2637 and then Present (Scope (Scope (Id))) -- error defense!
2638 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
2639 then
2640 Conditional_Delay (Id, T);
2641 end if;
2643 -- Check that constraint_error is raised for a scalar subtype
2644 -- indication when the lower or upper bound of a non-null range
2645 -- lies outside the range of the type mark.
2647 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
2648 if Is_Scalar_Type (Etype (Id))
2649 and then Scalar_Range (Id) /=
2650 Scalar_Range (Etype (Subtype_Mark
2651 (Subtype_Indication (N))))
2652 then
2653 Apply_Range_Check
2654 (Scalar_Range (Id),
2655 Etype (Subtype_Mark (Subtype_Indication (N))));
2657 elsif Is_Array_Type (Etype (Id))
2658 and then Present (First_Index (Id))
2659 then
2660 -- This really should be a subprogram that finds the indications
2661 -- to check???
2663 if ((Nkind (First_Index (Id)) = N_Identifier
2664 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
2665 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
2666 and then
2667 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
2668 then
2669 declare
2670 Target_Typ : constant Entity_Id :=
2671 Etype
2672 (First_Index (Etype
2673 (Subtype_Mark (Subtype_Indication (N)))));
2674 begin
2675 R_Checks :=
2676 Range_Check
2677 (Scalar_Range (Etype (First_Index (Id))),
2678 Target_Typ,
2679 Etype (First_Index (Id)),
2680 Defining_Identifier (N));
2682 Insert_Range_Checks
2683 (R_Checks,
2685 Target_Typ,
2686 Sloc (Defining_Identifier (N)));
2687 end;
2688 end if;
2689 end if;
2690 end if;
2692 Check_Eliminated (Id);
2693 end Analyze_Subtype_Declaration;
2695 --------------------------------
2696 -- Analyze_Subtype_Indication --
2697 --------------------------------
2699 procedure Analyze_Subtype_Indication (N : Node_Id) is
2700 T : constant Entity_Id := Subtype_Mark (N);
2701 R : constant Node_Id := Range_Expression (Constraint (N));
2703 begin
2704 Analyze (T);
2706 if R /= Error then
2707 Analyze (R);
2708 Set_Etype (N, Etype (R));
2709 else
2710 Set_Error_Posted (R);
2711 Set_Error_Posted (T);
2712 end if;
2713 end Analyze_Subtype_Indication;
2715 ------------------------------
2716 -- Analyze_Type_Declaration --
2717 ------------------------------
2719 procedure Analyze_Type_Declaration (N : Node_Id) is
2720 Def : constant Node_Id := Type_Definition (N);
2721 Def_Id : constant Entity_Id := Defining_Identifier (N);
2722 T : Entity_Id;
2723 Prev : Entity_Id;
2725 Is_Remote : constant Boolean :=
2726 (Is_Remote_Types (Current_Scope)
2727 or else Is_Remote_Call_Interface (Current_Scope))
2728 and then not (In_Private_Part (Current_Scope)
2729 or else
2730 In_Package_Body (Current_Scope));
2732 begin
2733 Prev := Find_Type_Name (N);
2735 -- The full view, if present, now points to the current type
2737 -- Ada 2005 (AI-50217): If the type was previously decorated when
2738 -- imported through a LIMITED WITH clause, it appears as incomplete
2739 -- but has no full view.
2741 if Ekind (Prev) = E_Incomplete_Type
2742 and then Present (Full_View (Prev))
2743 then
2744 T := Full_View (Prev);
2745 else
2746 T := Prev;
2747 end if;
2749 Set_Is_Pure (T, Is_Pure (Current_Scope));
2751 -- We set the flag Is_First_Subtype here. It is needed to set the
2752 -- corresponding flag for the Implicit class-wide-type created
2753 -- during tagged types processing.
2755 Set_Is_First_Subtype (T, True);
2757 -- Only composite types other than array types are allowed to have
2758 -- discriminants.
2760 case Nkind (Def) is
2762 -- For derived types, the rule will be checked once we've figured
2763 -- out the parent type.
2765 when N_Derived_Type_Definition =>
2766 null;
2768 -- For record types, discriminants are allowed.
2770 when N_Record_Definition =>
2771 null;
2773 when others =>
2774 if Present (Discriminant_Specifications (N)) then
2775 Error_Msg_N
2776 ("elementary or array type cannot have discriminants",
2777 Defining_Identifier
2778 (First (Discriminant_Specifications (N))));
2779 end if;
2780 end case;
2782 -- Elaborate the type definition according to kind, and generate
2783 -- subsidiary (implicit) subtypes where needed. We skip this if
2784 -- it was already done (this happens during the reanalysis that
2785 -- follows a call to the high level optimizer).
2787 if not Analyzed (T) then
2788 Set_Analyzed (T);
2790 case Nkind (Def) is
2792 when N_Access_To_Subprogram_Definition =>
2793 Access_Subprogram_Declaration (T, Def);
2795 -- If this is a remote access to subprogram, we must create
2796 -- the equivalent fat pointer type, and related subprograms.
2798 if Is_Remote then
2799 Process_Remote_AST_Declaration (N);
2800 end if;
2802 -- Validate categorization rule against access type declaration
2803 -- usually a violation in Pure unit, Shared_Passive unit.
2805 Validate_Access_Type_Declaration (T, N);
2807 when N_Access_To_Object_Definition =>
2808 Access_Type_Declaration (T, Def);
2810 -- Validate categorization rule against access type declaration
2811 -- usually a violation in Pure unit, Shared_Passive unit.
2813 Validate_Access_Type_Declaration (T, N);
2815 -- If we are in a Remote_Call_Interface package and define
2816 -- a RACW, Read and Write attribute must be added.
2818 if Is_Remote
2819 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2820 then
2821 Add_RACW_Features (Def_Id);
2822 end if;
2824 -- Set no strict aliasing flag if config pragma seen
2826 if Opt.No_Strict_Aliasing then
2827 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2828 end if;
2830 when N_Array_Type_Definition =>
2831 Array_Type_Declaration (T, Def);
2833 when N_Derived_Type_Definition =>
2834 Derived_Type_Declaration (T, N, T /= Def_Id);
2836 when N_Enumeration_Type_Definition =>
2837 Enumeration_Type_Declaration (T, Def);
2839 when N_Floating_Point_Definition =>
2840 Floating_Point_Type_Declaration (T, Def);
2842 when N_Decimal_Fixed_Point_Definition =>
2843 Decimal_Fixed_Point_Type_Declaration (T, Def);
2845 when N_Ordinary_Fixed_Point_Definition =>
2846 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2848 when N_Signed_Integer_Type_Definition =>
2849 Signed_Integer_Type_Declaration (T, Def);
2851 when N_Modular_Type_Definition =>
2852 Modular_Type_Declaration (T, Def);
2854 when N_Record_Definition =>
2855 Record_Type_Declaration (T, N, Prev);
2857 when others =>
2858 raise Program_Error;
2860 end case;
2861 end if;
2863 if Etype (T) = Any_Type then
2864 return;
2865 end if;
2867 -- Some common processing for all types
2869 Set_Depends_On_Private (T, Has_Private_Component (T));
2871 -- Both the declared entity, and its anonymous base type if one
2872 -- was created, need freeze nodes allocated.
2874 declare
2875 B : constant Entity_Id := Base_Type (T);
2877 begin
2878 -- In the case where the base type is different from the first
2879 -- subtype, we pre-allocate a freeze node, and set the proper
2880 -- link to the first subtype. Freeze_Entity will use this
2881 -- preallocated freeze node when it freezes the entity.
2883 if B /= T then
2884 Ensure_Freeze_Node (B);
2885 Set_First_Subtype_Link (Freeze_Node (B), T);
2886 end if;
2888 if not From_With_Type (T) then
2889 Set_Has_Delayed_Freeze (T);
2890 end if;
2891 end;
2893 -- Case of T is the full declaration of some private type which has
2894 -- been swapped in Defining_Identifier (N).
2896 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2897 Process_Full_View (N, T, Def_Id);
2899 -- Record the reference. The form of this is a little strange,
2900 -- since the full declaration has been swapped in. So the first
2901 -- parameter here represents the entity to which a reference is
2902 -- made which is the "real" entity, i.e. the one swapped in,
2903 -- and the second parameter provides the reference location.
2905 Generate_Reference (T, T, 'c');
2906 Set_Completion_Referenced (Def_Id);
2908 -- For completion of incomplete type, process incomplete dependents
2909 -- and always mark the full type as referenced (it is the incomplete
2910 -- type that we get for any real reference).
2912 elsif Ekind (Prev) = E_Incomplete_Type then
2913 Process_Incomplete_Dependents (N, T, Prev);
2914 Generate_Reference (Prev, Def_Id, 'c');
2915 Set_Completion_Referenced (Def_Id);
2917 -- If not private type or incomplete type completion, this is a real
2918 -- definition of a new entity, so record it.
2920 else
2921 Generate_Definition (Def_Id);
2922 end if;
2924 Check_Eliminated (Def_Id);
2925 end Analyze_Type_Declaration;
2927 --------------------------
2928 -- Analyze_Variant_Part --
2929 --------------------------
2931 procedure Analyze_Variant_Part (N : Node_Id) is
2933 procedure Non_Static_Choice_Error (Choice : Node_Id);
2934 -- Error routine invoked by the generic instantiation below when
2935 -- the variant part has a non static choice.
2937 procedure Process_Declarations (Variant : Node_Id);
2938 -- Analyzes all the declarations associated with a Variant.
2939 -- Needed by the generic instantiation below.
2941 package Variant_Choices_Processing is new
2942 Generic_Choices_Processing
2943 (Get_Alternatives => Variants,
2944 Get_Choices => Discrete_Choices,
2945 Process_Empty_Choice => No_OP,
2946 Process_Non_Static_Choice => Non_Static_Choice_Error,
2947 Process_Associated_Node => Process_Declarations);
2948 use Variant_Choices_Processing;
2949 -- Instantiation of the generic choice processing package.
2951 -----------------------------
2952 -- Non_Static_Choice_Error --
2953 -----------------------------
2955 procedure Non_Static_Choice_Error (Choice : Node_Id) is
2956 begin
2957 Flag_Non_Static_Expr
2958 ("choice given in variant part is not static!", Choice);
2959 end Non_Static_Choice_Error;
2961 --------------------------
2962 -- Process_Declarations --
2963 --------------------------
2965 procedure Process_Declarations (Variant : Node_Id) is
2966 begin
2967 if not Null_Present (Component_List (Variant)) then
2968 Analyze_Declarations (Component_Items (Component_List (Variant)));
2970 if Present (Variant_Part (Component_List (Variant))) then
2971 Analyze (Variant_Part (Component_List (Variant)));
2972 end if;
2973 end if;
2974 end Process_Declarations;
2976 -- Variables local to Analyze_Case_Statement.
2978 Discr_Name : Node_Id;
2979 Discr_Type : Entity_Id;
2981 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
2982 Last_Choice : Nat;
2983 Dont_Care : Boolean;
2984 Others_Present : Boolean := False;
2986 -- Start of processing for Analyze_Variant_Part
2988 begin
2989 Discr_Name := Name (N);
2990 Analyze (Discr_Name);
2992 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
2993 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
2994 end if;
2996 Discr_Type := Etype (Entity (Discr_Name));
2998 if not Is_Discrete_Type (Discr_Type) then
2999 Error_Msg_N
3000 ("discriminant in a variant part must be of a discrete type",
3001 Name (N));
3002 return;
3003 end if;
3005 -- Call the instantiated Analyze_Choices which does the rest of the work
3007 Analyze_Choices
3008 (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
3009 end Analyze_Variant_Part;
3011 ----------------------------
3012 -- Array_Type_Declaration --
3013 ----------------------------
3015 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
3016 Component_Def : constant Node_Id := Component_Definition (Def);
3017 Element_Type : Entity_Id;
3018 Implicit_Base : Entity_Id;
3019 Index : Node_Id;
3020 Related_Id : Entity_Id := Empty;
3021 Nb_Index : Nat;
3022 P : constant Node_Id := Parent (Def);
3023 Priv : Entity_Id;
3025 begin
3026 if Nkind (Def) = N_Constrained_Array_Definition then
3027 Index := First (Discrete_Subtype_Definitions (Def));
3028 else
3029 Index := First (Subtype_Marks (Def));
3030 end if;
3032 -- Find proper names for the implicit types which may be public.
3033 -- in case of anonymous arrays we use the name of the first object
3034 -- of that type as prefix.
3036 if No (T) then
3037 Related_Id := Defining_Identifier (P);
3038 else
3039 Related_Id := T;
3040 end if;
3042 Nb_Index := 1;
3044 while Present (Index) loop
3045 Analyze (Index);
3046 Make_Index (Index, P, Related_Id, Nb_Index);
3047 Next_Index (Index);
3048 Nb_Index := Nb_Index + 1;
3049 end loop;
3051 if Present (Subtype_Indication (Component_Def)) then
3052 Element_Type := Process_Subtype (Subtype_Indication (Component_Def),
3053 P, Related_Id, 'C');
3055 -- Ada 2005 (AI-230): Access Definition case
3057 else pragma Assert (Present (Access_Definition (Component_Def)));
3058 Element_Type := Access_Definition
3059 (Related_Nod => Related_Id,
3060 N => Access_Definition (Component_Def));
3062 -- Ada 2005 (AI-230): In case of components that are anonymous
3063 -- access types the level of accessibility depends on the enclosing
3064 -- type declaration
3066 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
3068 -- Ada 2005 (AI-254)
3070 declare
3071 CD : constant Node_Id :=
3072 Access_To_Subprogram_Definition
3073 (Access_Definition (Component_Def));
3074 begin
3075 if Present (CD) and then Protected_Present (CD) then
3076 Element_Type :=
3077 Replace_Anonymous_Access_To_Protected_Subprogram
3078 (Def, Element_Type);
3079 end if;
3080 end;
3081 end if;
3083 -- Constrained array case
3085 if No (T) then
3086 T := Create_Itype (E_Void, P, Related_Id, 'T');
3087 end if;
3089 if Nkind (Def) = N_Constrained_Array_Definition then
3091 -- Establish Implicit_Base as unconstrained base type
3093 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
3095 Init_Size_Align (Implicit_Base);
3096 Set_Etype (Implicit_Base, Implicit_Base);
3097 Set_Scope (Implicit_Base, Current_Scope);
3098 Set_Has_Delayed_Freeze (Implicit_Base);
3100 -- The constrained array type is a subtype of the unconstrained one
3102 Set_Ekind (T, E_Array_Subtype);
3103 Init_Size_Align (T);
3104 Set_Etype (T, Implicit_Base);
3105 Set_Scope (T, Current_Scope);
3106 Set_Is_Constrained (T, True);
3107 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
3108 Set_Has_Delayed_Freeze (T);
3110 -- Complete setup of implicit base type
3112 Set_First_Index (Implicit_Base, First_Index (T));
3113 Set_Component_Type (Implicit_Base, Element_Type);
3114 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
3115 Set_Component_Size (Implicit_Base, Uint_0);
3116 Set_Has_Controlled_Component
3117 (Implicit_Base, Has_Controlled_Component
3118 (Element_Type)
3119 or else
3120 Is_Controlled (Element_Type));
3121 Set_Finalize_Storage_Only
3122 (Implicit_Base, Finalize_Storage_Only
3123 (Element_Type));
3125 -- Unconstrained array case
3127 else
3128 Set_Ekind (T, E_Array_Type);
3129 Init_Size_Align (T);
3130 Set_Etype (T, T);
3131 Set_Scope (T, Current_Scope);
3132 Set_Component_Size (T, Uint_0);
3133 Set_Is_Constrained (T, False);
3134 Set_First_Index (T, First (Subtype_Marks (Def)));
3135 Set_Has_Delayed_Freeze (T, True);
3136 Set_Has_Task (T, Has_Task (Element_Type));
3137 Set_Has_Controlled_Component (T, Has_Controlled_Component
3138 (Element_Type)
3139 or else
3140 Is_Controlled (Element_Type));
3141 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
3142 (Element_Type));
3143 end if;
3145 Set_Component_Type (Base_Type (T), Element_Type);
3147 if Aliased_Present (Component_Definition (Def)) then
3148 Set_Has_Aliased_Components (Etype (T));
3149 end if;
3151 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
3152 -- array to ensure that objects of this type are initialized.
3154 if Ada_Version >= Ada_05
3155 and then (Null_Exclusion_Present (Component_Definition (Def))
3156 or else Can_Never_Be_Null (Element_Type))
3157 then
3158 Set_Can_Never_Be_Null (T);
3160 if Null_Exclusion_Present (Component_Definition (Def))
3161 and then Can_Never_Be_Null (Element_Type)
3162 then
3163 Error_Msg_N
3164 ("(Ada 2005) already a null-excluding type",
3165 Subtype_Indication (Component_Definition (Def)));
3166 end if;
3167 end if;
3169 Priv := Private_Component (Element_Type);
3171 if Present (Priv) then
3173 -- Check for circular definitions
3175 if Priv = Any_Type then
3176 Set_Component_Type (Etype (T), Any_Type);
3178 -- There is a gap in the visibility of operations on the composite
3179 -- type only if the component type is defined in a different scope.
3181 elsif Scope (Priv) = Current_Scope then
3182 null;
3184 elsif Is_Limited_Type (Priv) then
3185 Set_Is_Limited_Composite (Etype (T));
3186 Set_Is_Limited_Composite (T);
3187 else
3188 Set_Is_Private_Composite (Etype (T));
3189 Set_Is_Private_Composite (T);
3190 end if;
3191 end if;
3193 -- Create a concatenation operator for the new type. Internal
3194 -- array types created for packed entities do not need such, they
3195 -- are compatible with the user-defined type.
3197 if Number_Dimensions (T) = 1
3198 and then not Is_Packed_Array_Type (T)
3199 then
3200 New_Concatenation_Op (T);
3201 end if;
3203 -- In the case of an unconstrained array the parser has already
3204 -- verified that all the indices are unconstrained but we still
3205 -- need to make sure that the element type is constrained.
3207 if Is_Indefinite_Subtype (Element_Type) then
3208 Error_Msg_N
3209 ("unconstrained element type in array declaration",
3210 Subtype_Indication (Component_Def));
3212 elsif Is_Abstract (Element_Type) then
3213 Error_Msg_N
3214 ("The type of a component cannot be abstract",
3215 Subtype_Indication (Component_Def));
3216 end if;
3218 end Array_Type_Declaration;
3220 ------------------------------------------------------
3221 -- Replace_Anonymous_Access_To_Protected_Subprogram --
3222 ------------------------------------------------------
3224 function Replace_Anonymous_Access_To_Protected_Subprogram
3225 (N : Node_Id;
3226 Prev_E : Entity_Id) return Entity_Id
3228 Loc : constant Source_Ptr := Sloc (N);
3230 Curr_Scope : constant Scope_Stack_Entry :=
3231 Scope_Stack.Table (Scope_Stack.Last);
3233 Anon : constant Entity_Id :=
3234 Make_Defining_Identifier (Loc,
3235 Chars => New_Internal_Name ('S'));
3237 Acc : Node_Id;
3238 Comp : Node_Id;
3239 Decl : Node_Id;
3240 P : Node_Id := Parent (N);
3242 begin
3243 Set_Is_Internal (Anon);
3245 case Nkind (N) is
3246 when N_Component_Declaration |
3247 N_Unconstrained_Array_Definition |
3248 N_Constrained_Array_Definition =>
3249 Comp := Component_Definition (N);
3250 Acc := Access_Definition (Component_Definition (N));
3252 when N_Discriminant_Specification =>
3253 Comp := Discriminant_Type (N);
3254 Acc := Discriminant_Type (N);
3256 when N_Parameter_Specification =>
3257 Comp := Parameter_Type (N);
3258 Acc := Parameter_Type (N);
3260 when others =>
3261 raise Program_Error;
3262 end case;
3264 Decl := Make_Full_Type_Declaration (Loc,
3265 Defining_Identifier => Anon,
3266 Type_Definition =>
3267 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
3269 Mark_Rewrite_Insertion (Decl);
3271 -- Insert the new declaration in the nearest enclosing scope
3273 while Present (P) and then not Has_Declarations (P) loop
3274 P := Parent (P);
3275 end loop;
3277 pragma Assert (Present (P));
3279 if Nkind (P) = N_Package_Specification then
3280 Prepend (Decl, Visible_Declarations (P));
3281 else
3282 Prepend (Decl, Declarations (P));
3283 end if;
3285 -- Replace the anonymous type with an occurrence of the new declaration.
3286 -- In all cases the rewriten node does not have the null-exclusion
3287 -- attribute because (if present) it was already inherited by the
3288 -- anonymous entity (Anon). Thus, in case of components we do not
3289 -- inherit this attribute.
3291 if Nkind (N) = N_Parameter_Specification then
3292 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
3293 Set_Etype (Defining_Identifier (N), Anon);
3294 Set_Null_Exclusion_Present (N, False);
3295 else
3296 Rewrite (Comp,
3297 Make_Component_Definition (Loc,
3298 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
3299 end if;
3301 Mark_Rewrite_Insertion (Comp);
3303 -- Temporarily remove the current scope from the stack to add the new
3304 -- declarations to the enclosing scope
3306 Scope_Stack.Decrement_Last;
3307 Analyze (Decl);
3308 Scope_Stack.Append (Curr_Scope);
3310 Set_Original_Access_Type (Anon, Prev_E);
3311 return Anon;
3312 end Replace_Anonymous_Access_To_Protected_Subprogram;
3314 -------------------------------
3315 -- Build_Derived_Access_Type --
3316 -------------------------------
3318 procedure Build_Derived_Access_Type
3319 (N : Node_Id;
3320 Parent_Type : Entity_Id;
3321 Derived_Type : Entity_Id)
3323 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
3325 Desig_Type : Entity_Id;
3326 Discr : Entity_Id;
3327 Discr_Con_Elist : Elist_Id;
3328 Discr_Con_El : Elmt_Id;
3329 Subt : Entity_Id;
3331 begin
3332 -- Set the designated type so it is available in case this is
3333 -- an access to a self-referential type, e.g. a standard list
3334 -- type with a next pointer. Will be reset after subtype is built.
3336 Set_Directly_Designated_Type
3337 (Derived_Type, Designated_Type (Parent_Type));
3339 Subt := Process_Subtype (S, N);
3341 if Nkind (S) /= N_Subtype_Indication
3342 and then Subt /= Base_Type (Subt)
3343 then
3344 Set_Ekind (Derived_Type, E_Access_Subtype);
3345 end if;
3347 if Ekind (Derived_Type) = E_Access_Subtype then
3348 declare
3349 Pbase : constant Entity_Id := Base_Type (Parent_Type);
3350 Ibase : constant Entity_Id :=
3351 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
3352 Svg_Chars : constant Name_Id := Chars (Ibase);
3353 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
3355 begin
3356 Copy_Node (Pbase, Ibase);
3358 Set_Chars (Ibase, Svg_Chars);
3359 Set_Next_Entity (Ibase, Svg_Next_E);
3360 Set_Sloc (Ibase, Sloc (Derived_Type));
3361 Set_Scope (Ibase, Scope (Derived_Type));
3362 Set_Freeze_Node (Ibase, Empty);
3363 Set_Is_Frozen (Ibase, False);
3364 Set_Comes_From_Source (Ibase, False);
3365 Set_Is_First_Subtype (Ibase, False);
3367 Set_Etype (Ibase, Pbase);
3368 Set_Etype (Derived_Type, Ibase);
3369 end;
3370 end if;
3372 Set_Directly_Designated_Type
3373 (Derived_Type, Designated_Type (Subt));
3375 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
3376 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
3377 Set_Size_Info (Derived_Type, Parent_Type);
3378 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
3379 Set_Depends_On_Private (Derived_Type,
3380 Has_Private_Component (Derived_Type));
3381 Conditional_Delay (Derived_Type, Subt);
3383 -- Ada 2005 (AI-231). Set the null-exclusion attribute
3385 if Null_Exclusion_Present (Type_Definition (N))
3386 or else Can_Never_Be_Null (Parent_Type)
3387 then
3388 Set_Can_Never_Be_Null (Derived_Type);
3389 end if;
3391 -- Note: we do not copy the Storage_Size_Variable, since
3392 -- we always go to the root type for this information.
3394 -- Apply range checks to discriminants for derived record case
3395 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
3397 Desig_Type := Designated_Type (Derived_Type);
3398 if Is_Composite_Type (Desig_Type)
3399 and then (not Is_Array_Type (Desig_Type))
3400 and then Has_Discriminants (Desig_Type)
3401 and then Base_Type (Desig_Type) /= Desig_Type
3402 then
3403 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
3404 Discr_Con_El := First_Elmt (Discr_Con_Elist);
3406 Discr := First_Discriminant (Base_Type (Desig_Type));
3407 while Present (Discr_Con_El) loop
3408 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
3409 Next_Elmt (Discr_Con_El);
3410 Next_Discriminant (Discr);
3411 end loop;
3412 end if;
3413 end Build_Derived_Access_Type;
3415 ------------------------------
3416 -- Build_Derived_Array_Type --
3417 ------------------------------
3419 procedure Build_Derived_Array_Type
3420 (N : Node_Id;
3421 Parent_Type : Entity_Id;
3422 Derived_Type : Entity_Id)
3424 Loc : constant Source_Ptr := Sloc (N);
3425 Tdef : constant Node_Id := Type_Definition (N);
3426 Indic : constant Node_Id := Subtype_Indication (Tdef);
3427 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
3428 Implicit_Base : Entity_Id;
3429 New_Indic : Node_Id;
3431 procedure Make_Implicit_Base;
3432 -- If the parent subtype is constrained, the derived type is a
3433 -- subtype of an implicit base type derived from the parent base.
3435 ------------------------
3436 -- Make_Implicit_Base --
3437 ------------------------
3439 procedure Make_Implicit_Base is
3440 begin
3441 Implicit_Base :=
3442 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
3444 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
3445 Set_Etype (Implicit_Base, Parent_Base);
3447 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
3448 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
3450 Set_Has_Delayed_Freeze (Implicit_Base, True);
3451 end Make_Implicit_Base;
3453 -- Start of processing for Build_Derived_Array_Type
3455 begin
3456 if not Is_Constrained (Parent_Type) then
3457 if Nkind (Indic) /= N_Subtype_Indication then
3458 Set_Ekind (Derived_Type, E_Array_Type);
3460 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
3461 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
3463 Set_Has_Delayed_Freeze (Derived_Type, True);
3465 else
3466 Make_Implicit_Base;
3467 Set_Etype (Derived_Type, Implicit_Base);
3469 New_Indic :=
3470 Make_Subtype_Declaration (Loc,
3471 Defining_Identifier => Derived_Type,
3472 Subtype_Indication =>
3473 Make_Subtype_Indication (Loc,
3474 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
3475 Constraint => Constraint (Indic)));
3477 Rewrite (N, New_Indic);
3478 Analyze (N);
3479 end if;
3481 else
3482 if Nkind (Indic) /= N_Subtype_Indication then
3483 Make_Implicit_Base;
3485 Set_Ekind (Derived_Type, Ekind (Parent_Type));
3486 Set_Etype (Derived_Type, Implicit_Base);
3487 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
3489 else
3490 Error_Msg_N ("illegal constraint on constrained type", Indic);
3491 end if;
3492 end if;
3494 -- If the parent type is not a derived type itself, and is
3495 -- declared in a closed scope (e.g., a subprogram), then we
3496 -- need to explicitly introduce the new type's concatenation
3497 -- operator since Derive_Subprograms will not inherit the
3498 -- parent's operator. If the parent type is unconstrained, the
3499 -- operator is of the unconstrained base type.
3501 if Number_Dimensions (Parent_Type) = 1
3502 and then not Is_Limited_Type (Parent_Type)
3503 and then not Is_Derived_Type (Parent_Type)
3504 and then not Is_Package (Scope (Base_Type (Parent_Type)))
3505 then
3506 if not Is_Constrained (Parent_Type)
3507 and then Is_Constrained (Derived_Type)
3508 then
3509 New_Concatenation_Op (Implicit_Base);
3510 else
3511 New_Concatenation_Op (Derived_Type);
3512 end if;
3513 end if;
3514 end Build_Derived_Array_Type;
3516 -----------------------------------
3517 -- Build_Derived_Concurrent_Type --
3518 -----------------------------------
3520 procedure Build_Derived_Concurrent_Type
3521 (N : Node_Id;
3522 Parent_Type : Entity_Id;
3523 Derived_Type : Entity_Id)
3525 D_Constraint : Node_Id;
3526 Disc_Spec : Node_Id;
3527 Old_Disc : Entity_Id;
3528 New_Disc : Entity_Id;
3530 Constraint_Present : constant Boolean :=
3531 Nkind (Subtype_Indication (Type_Definition (N)))
3532 = N_Subtype_Indication;
3534 begin
3535 Set_Stored_Constraint (Derived_Type, No_Elist);
3537 if Is_Task_Type (Parent_Type) then
3538 Set_Storage_Size_Variable (Derived_Type,
3539 Storage_Size_Variable (Parent_Type));
3540 end if;
3542 if Present (Discriminant_Specifications (N)) then
3543 New_Scope (Derived_Type);
3544 Check_Or_Process_Discriminants (N, Derived_Type);
3545 End_Scope;
3547 elsif Constraint_Present then
3549 -- Build constrained subtype and derive from it
3551 declare
3552 Loc : constant Source_Ptr := Sloc (N);
3553 Anon : constant Entity_Id :=
3554 Make_Defining_Identifier (Loc,
3555 New_External_Name (Chars (Derived_Type), 'T'));
3556 Decl : Node_Id;
3558 begin
3559 Decl :=
3560 Make_Subtype_Declaration (Loc,
3561 Defining_Identifier => Anon,
3562 Subtype_Indication =>
3563 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
3564 Insert_Before (N, Decl);
3565 Rewrite (Subtype_Indication (Type_Definition (N)),
3566 New_Occurrence_Of (Anon, Loc));
3567 Analyze (Decl);
3568 Set_Analyzed (Derived_Type, False);
3569 Analyze (N);
3570 return;
3571 end;
3572 end if;
3574 -- All attributes are inherited from parent. In particular,
3575 -- entries and the corresponding record type are the same.
3576 -- Discriminants may be renamed, and must be treated separately.
3578 Set_Has_Discriminants
3579 (Derived_Type, Has_Discriminants (Parent_Type));
3580 Set_Corresponding_Record_Type
3581 (Derived_Type, Corresponding_Record_Type (Parent_Type));
3583 if Constraint_Present then
3585 if not Has_Discriminants (Parent_Type) then
3586 Error_Msg_N ("untagged parent must have discriminants", N);
3588 elsif Present (Discriminant_Specifications (N)) then
3590 -- Verify that new discriminants are used to constrain
3591 -- the old ones.
3593 Old_Disc := First_Discriminant (Parent_Type);
3594 New_Disc := First_Discriminant (Derived_Type);
3595 Disc_Spec := First (Discriminant_Specifications (N));
3596 D_Constraint :=
3597 First
3598 (Constraints
3599 (Constraint (Subtype_Indication (Type_Definition (N)))));
3601 while Present (Old_Disc) and then Present (Disc_Spec) loop
3603 if Nkind (Discriminant_Type (Disc_Spec)) /=
3604 N_Access_Definition
3605 then
3606 Analyze (Discriminant_Type (Disc_Spec));
3608 if not Subtypes_Statically_Compatible (
3609 Etype (Discriminant_Type (Disc_Spec)),
3610 Etype (Old_Disc))
3611 then
3612 Error_Msg_N
3613 ("not statically compatible with parent discriminant",
3614 Discriminant_Type (Disc_Spec));
3615 end if;
3616 end if;
3618 if Nkind (D_Constraint) = N_Identifier
3619 and then Chars (D_Constraint) /=
3620 Chars (Defining_Identifier (Disc_Spec))
3621 then
3622 Error_Msg_N ("new discriminants must constrain old ones",
3623 D_Constraint);
3624 else
3625 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
3626 end if;
3628 Next_Discriminant (Old_Disc);
3629 Next_Discriminant (New_Disc);
3630 Next (Disc_Spec);
3631 end loop;
3633 if Present (Old_Disc) or else Present (Disc_Spec) then
3634 Error_Msg_N ("discriminant mismatch in derivation", N);
3635 end if;
3637 end if;
3639 elsif Present (Discriminant_Specifications (N)) then
3640 Error_Msg_N
3641 ("missing discriminant constraint in untagged derivation",
3643 end if;
3645 if Present (Discriminant_Specifications (N)) then
3647 Old_Disc := First_Discriminant (Parent_Type);
3649 while Present (Old_Disc) loop
3651 if No (Next_Entity (Old_Disc))
3652 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
3653 then
3654 Set_Next_Entity (Last_Entity (Derived_Type),
3655 Next_Entity (Old_Disc));
3656 exit;
3657 end if;
3659 Next_Discriminant (Old_Disc);
3660 end loop;
3662 else
3663 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
3664 if Has_Discriminants (Parent_Type) then
3665 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
3666 Set_Discriminant_Constraint (
3667 Derived_Type, Discriminant_Constraint (Parent_Type));
3668 end if;
3669 end if;
3671 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
3673 Set_Has_Completion (Derived_Type);
3674 end Build_Derived_Concurrent_Type;
3676 ------------------------------------
3677 -- Build_Derived_Enumeration_Type --
3678 ------------------------------------
3680 procedure Build_Derived_Enumeration_Type
3681 (N : Node_Id;
3682 Parent_Type : Entity_Id;
3683 Derived_Type : Entity_Id)
3685 Loc : constant Source_Ptr := Sloc (N);
3686 Def : constant Node_Id := Type_Definition (N);
3687 Indic : constant Node_Id := Subtype_Indication (Def);
3688 Implicit_Base : Entity_Id;
3689 Literal : Entity_Id;
3690 New_Lit : Entity_Id;
3691 Literals_List : List_Id;
3692 Type_Decl : Node_Id;
3693 Hi, Lo : Node_Id;
3694 Rang_Expr : Node_Id;
3696 begin
3697 -- Since types Standard.Character and Standard.Wide_Character do
3698 -- not have explicit literals lists we need to process types derived
3699 -- from them specially. This is handled by Derived_Standard_Character.
3700 -- If the parent type is a generic type, there are no literals either,
3701 -- and we construct the same skeletal representation as for the generic
3702 -- parent type.
3704 if Root_Type (Parent_Type) = Standard_Character
3705 or else Root_Type (Parent_Type) = Standard_Wide_Character
3706 then
3707 Derived_Standard_Character (N, Parent_Type, Derived_Type);
3709 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
3710 declare
3711 Lo : Node_Id;
3712 Hi : Node_Id;
3714 begin
3715 Lo :=
3716 Make_Attribute_Reference (Loc,
3717 Attribute_Name => Name_First,
3718 Prefix => New_Reference_To (Derived_Type, Loc));
3719 Set_Etype (Lo, Derived_Type);
3721 Hi :=
3722 Make_Attribute_Reference (Loc,
3723 Attribute_Name => Name_Last,
3724 Prefix => New_Reference_To (Derived_Type, Loc));
3725 Set_Etype (Hi, Derived_Type);
3727 Set_Scalar_Range (Derived_Type,
3728 Make_Range (Loc,
3729 Low_Bound => Lo,
3730 High_Bound => Hi));
3731 end;
3733 else
3734 -- If a constraint is present, analyze the bounds to catch
3735 -- premature usage of the derived literals.
3737 if Nkind (Indic) = N_Subtype_Indication
3738 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
3739 then
3740 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
3741 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
3742 end if;
3744 -- Introduce an implicit base type for the derived type even
3745 -- if there is no constraint attached to it, since this seems
3746 -- closer to the Ada semantics. Build a full type declaration
3747 -- tree for the derived type using the implicit base type as
3748 -- the defining identifier. The build a subtype declaration
3749 -- tree which applies the constraint (if any) have it replace
3750 -- the derived type declaration.
3752 Literal := First_Literal (Parent_Type);
3753 Literals_List := New_List;
3755 while Present (Literal)
3756 and then Ekind (Literal) = E_Enumeration_Literal
3757 loop
3758 -- Literals of the derived type have the same representation as
3759 -- those of the parent type, but this representation can be
3760 -- overridden by an explicit representation clause. Indicate
3761 -- that there is no explicit representation given yet. These
3762 -- derived literals are implicit operations of the new type,
3763 -- and can be overriden by explicit ones.
3765 if Nkind (Literal) = N_Defining_Character_Literal then
3766 New_Lit :=
3767 Make_Defining_Character_Literal (Loc, Chars (Literal));
3768 else
3769 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
3770 end if;
3772 Set_Ekind (New_Lit, E_Enumeration_Literal);
3773 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
3774 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
3775 Set_Enumeration_Rep_Expr (New_Lit, Empty);
3776 Set_Alias (New_Lit, Literal);
3777 Set_Is_Known_Valid (New_Lit, True);
3779 Append (New_Lit, Literals_List);
3780 Next_Literal (Literal);
3781 end loop;
3783 Implicit_Base :=
3784 Make_Defining_Identifier (Sloc (Derived_Type),
3785 New_External_Name (Chars (Derived_Type), 'B'));
3787 -- Indicate the proper nature of the derived type. This must
3788 -- be done before analysis of the literals, to recognize cases
3789 -- when a literal may be hidden by a previous explicit function
3790 -- definition (cf. c83031a).
3792 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
3793 Set_Etype (Derived_Type, Implicit_Base);
3795 Type_Decl :=
3796 Make_Full_Type_Declaration (Loc,
3797 Defining_Identifier => Implicit_Base,
3798 Discriminant_Specifications => No_List,
3799 Type_Definition =>
3800 Make_Enumeration_Type_Definition (Loc, Literals_List));
3802 Mark_Rewrite_Insertion (Type_Decl);
3803 Insert_Before (N, Type_Decl);
3804 Analyze (Type_Decl);
3806 -- After the implicit base is analyzed its Etype needs to be
3807 -- changed to reflect the fact that it is derived from the
3808 -- parent type which was ignored during analysis. We also set
3809 -- the size at this point.
3811 Set_Etype (Implicit_Base, Parent_Type);
3813 Set_Size_Info (Implicit_Base, Parent_Type);
3814 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
3815 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
3817 Set_Has_Non_Standard_Rep
3818 (Implicit_Base, Has_Non_Standard_Rep
3819 (Parent_Type));
3820 Set_Has_Delayed_Freeze (Implicit_Base);
3822 -- Process the subtype indication including a validation check
3823 -- on the constraint, if any. If a constraint is given, its bounds
3824 -- must be implicitly converted to the new type.
3826 if Nkind (Indic) = N_Subtype_Indication then
3828 declare
3829 R : constant Node_Id :=
3830 Range_Expression (Constraint (Indic));
3832 begin
3833 if Nkind (R) = N_Range then
3834 Hi := Build_Scalar_Bound
3835 (High_Bound (R), Parent_Type, Implicit_Base);
3836 Lo := Build_Scalar_Bound
3837 (Low_Bound (R), Parent_Type, Implicit_Base);
3839 else
3840 -- Constraint is a Range attribute. Replace with the
3841 -- explicit mention of the bounds of the prefix, which
3842 -- must be a subtype.
3844 Analyze (Prefix (R));
3845 Hi :=
3846 Convert_To (Implicit_Base,
3847 Make_Attribute_Reference (Loc,
3848 Attribute_Name => Name_Last,
3849 Prefix =>
3850 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
3852 Lo :=
3853 Convert_To (Implicit_Base,
3854 Make_Attribute_Reference (Loc,
3855 Attribute_Name => Name_First,
3856 Prefix =>
3857 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
3858 end if;
3860 end;
3862 else
3863 Hi :=
3864 Build_Scalar_Bound
3865 (Type_High_Bound (Parent_Type),
3866 Parent_Type, Implicit_Base);
3867 Lo :=
3868 Build_Scalar_Bound
3869 (Type_Low_Bound (Parent_Type),
3870 Parent_Type, Implicit_Base);
3871 end if;
3873 Rang_Expr :=
3874 Make_Range (Loc,
3875 Low_Bound => Lo,
3876 High_Bound => Hi);
3878 -- If we constructed a default range for the case where no range
3879 -- was given, then the expressions in the range must not freeze
3880 -- since they do not correspond to expressions in the source.
3882 if Nkind (Indic) /= N_Subtype_Indication then
3883 Set_Must_Not_Freeze (Lo);
3884 Set_Must_Not_Freeze (Hi);
3885 Set_Must_Not_Freeze (Rang_Expr);
3886 end if;
3888 Rewrite (N,
3889 Make_Subtype_Declaration (Loc,
3890 Defining_Identifier => Derived_Type,
3891 Subtype_Indication =>
3892 Make_Subtype_Indication (Loc,
3893 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
3894 Constraint =>
3895 Make_Range_Constraint (Loc,
3896 Range_Expression => Rang_Expr))));
3898 Analyze (N);
3900 -- If pragma Discard_Names applies on the first subtype
3901 -- of the parent type, then it must be applied on this
3902 -- subtype as well.
3904 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
3905 Set_Discard_Names (Derived_Type);
3906 end if;
3908 -- Apply a range check. Since this range expression doesn't
3909 -- have an Etype, we have to specifically pass the Source_Typ
3910 -- parameter. Is this right???
3912 if Nkind (Indic) = N_Subtype_Indication then
3913 Apply_Range_Check (Range_Expression (Constraint (Indic)),
3914 Parent_Type,
3915 Source_Typ => Entity (Subtype_Mark (Indic)));
3916 end if;
3917 end if;
3918 end Build_Derived_Enumeration_Type;
3920 --------------------------------
3921 -- Build_Derived_Numeric_Type --
3922 --------------------------------
3924 procedure Build_Derived_Numeric_Type
3925 (N : Node_Id;
3926 Parent_Type : Entity_Id;
3927 Derived_Type : Entity_Id)
3929 Loc : constant Source_Ptr := Sloc (N);
3930 Tdef : constant Node_Id := Type_Definition (N);
3931 Indic : constant Node_Id := Subtype_Indication (Tdef);
3932 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
3933 No_Constraint : constant Boolean := Nkind (Indic) /=
3934 N_Subtype_Indication;
3935 Implicit_Base : Entity_Id;
3937 Lo : Node_Id;
3938 Hi : Node_Id;
3940 begin
3941 -- Process the subtype indication including a validation check on
3942 -- the constraint if any.
3944 Discard_Node (Process_Subtype (Indic, N));
3946 -- Introduce an implicit base type for the derived type even if
3947 -- there is no constraint attached to it, since this seems closer
3948 -- to the Ada semantics.
3950 Implicit_Base :=
3951 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
3953 Set_Etype (Implicit_Base, Parent_Base);
3954 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
3955 Set_Size_Info (Implicit_Base, Parent_Base);
3956 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
3957 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
3958 Set_Parent (Implicit_Base, Parent (Derived_Type));
3960 if Is_Discrete_Or_Fixed_Point_Type (Parent_Base) then
3961 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
3962 end if;
3964 Set_Has_Delayed_Freeze (Implicit_Base);
3966 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
3967 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
3969 Set_Scalar_Range (Implicit_Base,
3970 Make_Range (Loc,
3971 Low_Bound => Lo,
3972 High_Bound => Hi));
3974 if Has_Infinities (Parent_Base) then
3975 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
3976 end if;
3978 -- The Derived_Type, which is the entity of the declaration, is
3979 -- a subtype of the implicit base. Its Ekind is a subtype, even
3980 -- in the absence of an explicit constraint.
3982 Set_Etype (Derived_Type, Implicit_Base);
3984 -- If we did not have a constraint, then the Ekind is set from the
3985 -- parent type (otherwise Process_Subtype has set the bounds)
3987 if No_Constraint then
3988 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
3989 end if;
3991 -- If we did not have a range constraint, then set the range
3992 -- from the parent type. Otherwise, the call to Process_Subtype
3993 -- has set the bounds.
3995 if No_Constraint
3996 or else not Has_Range_Constraint (Indic)
3997 then
3998 Set_Scalar_Range (Derived_Type,
3999 Make_Range (Loc,
4000 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
4001 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
4002 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4004 if Has_Infinities (Parent_Type) then
4005 Set_Includes_Infinities (Scalar_Range (Derived_Type));
4006 end if;
4007 end if;
4009 -- Set remaining type-specific fields, depending on numeric type
4011 if Is_Modular_Integer_Type (Parent_Type) then
4012 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
4014 Set_Non_Binary_Modulus
4015 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
4017 elsif Is_Floating_Point_Type (Parent_Type) then
4019 -- Digits of base type is always copied from the digits value of
4020 -- the parent base type, but the digits of the derived type will
4021 -- already have been set if there was a constraint present.
4023 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
4024 Set_Vax_Float (Implicit_Base, Vax_Float (Parent_Base));
4026 if No_Constraint then
4027 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
4028 end if;
4030 elsif Is_Fixed_Point_Type (Parent_Type) then
4032 -- Small of base type and derived type are always copied from
4033 -- the parent base type, since smalls never change. The delta
4034 -- of the base type is also copied from the parent base type.
4035 -- However the delta of the derived type will have been set
4036 -- already if a constraint was present.
4038 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
4039 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
4040 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
4042 if No_Constraint then
4043 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
4044 end if;
4046 -- The scale and machine radix in the decimal case are always
4047 -- copied from the parent base type.
4049 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
4050 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
4051 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
4053 Set_Machine_Radix_10
4054 (Derived_Type, Machine_Radix_10 (Parent_Base));
4055 Set_Machine_Radix_10
4056 (Implicit_Base, Machine_Radix_10 (Parent_Base));
4058 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
4060 if No_Constraint then
4061 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
4063 else
4064 -- the analysis of the subtype_indication sets the
4065 -- digits value of the derived type.
4067 null;
4068 end if;
4069 end if;
4070 end if;
4072 -- The type of the bounds is that of the parent type, and they
4073 -- must be converted to the derived type.
4075 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
4077 -- The implicit_base should be frozen when the derived type is frozen,
4078 -- but note that it is used in the conversions of the bounds. For
4079 -- fixed types we delay the determination of the bounds until the proper
4080 -- freezing point. For other numeric types this is rejected by GCC, for
4081 -- reasons that are currently unclear (???), so we choose to freeze the
4082 -- implicit base now. In the case of integers and floating point types
4083 -- this is harmless because subsequent representation clauses cannot
4084 -- affect anything, but it is still baffling that we cannot use the
4085 -- same mechanism for all derived numeric types.
4087 if Is_Fixed_Point_Type (Parent_Type) then
4088 Conditional_Delay (Implicit_Base, Parent_Type);
4089 else
4090 Freeze_Before (N, Implicit_Base);
4091 end if;
4092 end Build_Derived_Numeric_Type;
4094 --------------------------------
4095 -- Build_Derived_Private_Type --
4096 --------------------------------
4098 procedure Build_Derived_Private_Type
4099 (N : Node_Id;
4100 Parent_Type : Entity_Id;
4101 Derived_Type : Entity_Id;
4102 Is_Completion : Boolean;
4103 Derive_Subps : Boolean := True)
4105 Der_Base : Entity_Id;
4106 Discr : Entity_Id;
4107 Full_Decl : Node_Id := Empty;
4108 Full_Der : Entity_Id;
4109 Full_P : Entity_Id;
4110 Last_Discr : Entity_Id;
4111 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
4112 Swapped : Boolean := False;
4114 procedure Copy_And_Build;
4115 -- Copy derived type declaration, replace parent with its full view,
4116 -- and analyze new declaration.
4118 --------------------
4119 -- Copy_And_Build --
4120 --------------------
4122 procedure Copy_And_Build is
4123 Full_N : Node_Id;
4125 begin
4126 if Ekind (Parent_Type) in Record_Kind
4127 or else (Ekind (Parent_Type) in Enumeration_Kind
4128 and then Root_Type (Parent_Type) /= Standard_Character
4129 and then Root_Type (Parent_Type) /= Standard_Wide_Character
4130 and then not Is_Generic_Type (Root_Type (Parent_Type)))
4131 then
4132 Full_N := New_Copy_Tree (N);
4133 Insert_After (N, Full_N);
4134 Build_Derived_Type (
4135 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
4137 else
4138 Build_Derived_Type (
4139 N, Parent_Type, Full_Der, True, Derive_Subps => False);
4140 end if;
4141 end Copy_And_Build;
4143 -- Start of processing for Build_Derived_Private_Type
4145 begin
4146 if Is_Tagged_Type (Parent_Type) then
4147 Build_Derived_Record_Type
4148 (N, Parent_Type, Derived_Type, Derive_Subps);
4149 return;
4151 elsif Has_Discriminants (Parent_Type) then
4153 if Present (Full_View (Parent_Type)) then
4154 if not Is_Completion then
4156 -- Copy declaration for subsequent analysis, to
4157 -- provide a completion for what is a private
4158 -- declaration. Indicate that the full type is
4159 -- internally generated.
4161 Full_Decl := New_Copy_Tree (N);
4162 Full_Der := New_Copy (Derived_Type);
4163 Set_Comes_From_Source (Full_Decl, False);
4165 Insert_After (N, Full_Decl);
4167 else
4168 -- If this is a completion, the full view being built is
4169 -- itself private. We build a subtype of the parent with
4170 -- the same constraints as this full view, to convey to the
4171 -- back end the constrained components and the size of this
4172 -- subtype. If the parent is constrained, its full view can
4173 -- serve as the underlying full view of the derived type.
4175 if No (Discriminant_Specifications (N)) then
4177 if Nkind (Subtype_Indication (Type_Definition (N)))
4178 = N_Subtype_Indication
4179 then
4180 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
4182 elsif Is_Constrained (Full_View (Parent_Type)) then
4183 Set_Underlying_Full_View (Derived_Type,
4184 Full_View (Parent_Type));
4185 end if;
4187 else
4188 -- If there are new discriminants, the parent subtype is
4189 -- constrained by them, but it is not clear how to build
4190 -- the underlying_full_view in this case ???
4192 null;
4193 end if;
4194 end if;
4195 end if;
4197 -- Build partial view of derived type from partial view of parent.
4199 Build_Derived_Record_Type
4200 (N, Parent_Type, Derived_Type, Derive_Subps);
4202 if Present (Full_View (Parent_Type))
4203 and then not Is_Completion
4204 then
4205 if not In_Open_Scopes (Par_Scope)
4206 or else not In_Same_Source_Unit (N, Parent_Type)
4207 then
4208 -- Swap partial and full views temporarily
4210 Install_Private_Declarations (Par_Scope);
4211 Install_Visible_Declarations (Par_Scope);
4212 Swapped := True;
4213 end if;
4215 -- Build full view of derived type from full view of
4216 -- parent which is now installed.
4217 -- Subprograms have been derived on the partial view,
4218 -- the completion does not derive them anew.
4220 if not Is_Tagged_Type (Parent_Type) then
4221 Build_Derived_Record_Type
4222 (Full_Decl, Parent_Type, Full_Der, False);
4223 else
4225 -- If full view of parent is tagged, the completion
4226 -- inherits the proper primitive operations.
4228 Set_Defining_Identifier (Full_Decl, Full_Der);
4229 Build_Derived_Record_Type
4230 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
4231 Set_Analyzed (Full_Decl);
4232 end if;
4234 if Swapped then
4235 Uninstall_Declarations (Par_Scope);
4237 if In_Open_Scopes (Par_Scope) then
4238 Install_Visible_Declarations (Par_Scope);
4239 end if;
4240 end if;
4242 Der_Base := Base_Type (Derived_Type);
4243 Set_Full_View (Derived_Type, Full_Der);
4244 Set_Full_View (Der_Base, Base_Type (Full_Der));
4246 -- Copy the discriminant list from full view to
4247 -- the partial views (base type and its subtype).
4248 -- Gigi requires that the partial and full views
4249 -- have the same discriminants.
4250 -- ??? Note that since the partial view is pointing
4251 -- to discriminants in the full view, their scope
4252 -- will be that of the full view. This might
4253 -- cause some front end problems and need
4254 -- adjustment?
4256 Discr := First_Discriminant (Base_Type (Full_Der));
4257 Set_First_Entity (Der_Base, Discr);
4259 loop
4260 Last_Discr := Discr;
4261 Next_Discriminant (Discr);
4262 exit when No (Discr);
4263 end loop;
4265 Set_Last_Entity (Der_Base, Last_Discr);
4267 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
4268 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
4269 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
4271 else
4272 -- If this is a completion, the derived type stays private
4273 -- and there is no need to create a further full view, except
4274 -- in the unusual case when the derivation is nested within a
4275 -- child unit, see below.
4277 null;
4278 end if;
4280 elsif Present (Full_View (Parent_Type))
4281 and then Has_Discriminants (Full_View (Parent_Type))
4282 then
4283 if Has_Unknown_Discriminants (Parent_Type)
4284 and then Nkind (Subtype_Indication (Type_Definition (N)))
4285 = N_Subtype_Indication
4286 then
4287 Error_Msg_N
4288 ("cannot constrain type with unknown discriminants",
4289 Subtype_Indication (Type_Definition (N)));
4290 return;
4291 end if;
4293 -- If full view of parent is a record type, Build full view as
4294 -- a derivation from the parent's full view. Partial view remains
4295 -- private. For code generation and linking, the full view must
4296 -- have the same public status as the partial one. This full view
4297 -- is only needed if the parent type is in an enclosing scope, so
4298 -- that the full view may actually become visible, e.g. in a child
4299 -- unit. This is both more efficient, and avoids order of freezing
4300 -- problems with the added entities.
4302 if not Is_Private_Type (Full_View (Parent_Type))
4303 and then (In_Open_Scopes (Scope (Parent_Type)))
4304 then
4305 Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
4306 Chars (Derived_Type));
4307 Set_Is_Itype (Full_Der);
4308 Set_Has_Private_Declaration (Full_Der);
4309 Set_Has_Private_Declaration (Derived_Type);
4310 Set_Associated_Node_For_Itype (Full_Der, N);
4311 Set_Parent (Full_Der, Parent (Derived_Type));
4312 Set_Full_View (Derived_Type, Full_Der);
4313 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
4314 Full_P := Full_View (Parent_Type);
4315 Exchange_Declarations (Parent_Type);
4316 Copy_And_Build;
4317 Exchange_Declarations (Full_P);
4319 else
4320 Build_Derived_Record_Type
4321 (N, Full_View (Parent_Type), Derived_Type,
4322 Derive_Subps => False);
4323 end if;
4325 -- In any case, the primitive operations are inherited from
4326 -- the parent type, not from the internal full view.
4328 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
4330 if Derive_Subps then
4331 Derive_Subprograms (Parent_Type, Derived_Type);
4332 end if;
4334 else
4335 -- Untagged type, No discriminants on either view
4337 if Nkind (Subtype_Indication (Type_Definition (N)))
4338 = N_Subtype_Indication
4339 then
4340 Error_Msg_N
4341 ("illegal constraint on type without discriminants", N);
4342 end if;
4344 if Present (Discriminant_Specifications (N))
4345 and then Present (Full_View (Parent_Type))
4346 and then not Is_Tagged_Type (Full_View (Parent_Type))
4347 then
4348 Error_Msg_N
4349 ("cannot add discriminants to untagged type", N);
4350 end if;
4352 Set_Stored_Constraint (Derived_Type, No_Elist);
4353 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4354 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
4355 Set_Has_Controlled_Component
4356 (Derived_Type, Has_Controlled_Component
4357 (Parent_Type));
4359 -- Direct controlled types do not inherit Finalize_Storage_Only flag
4361 if not Is_Controlled (Parent_Type) then
4362 Set_Finalize_Storage_Only
4363 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
4364 end if;
4366 -- Construct the implicit full view by deriving from full
4367 -- view of the parent type. In order to get proper visibility,
4368 -- we install the parent scope and its declarations.
4370 -- ??? if the parent is untagged private and its
4371 -- completion is tagged, this mechanism will not
4372 -- work because we cannot derive from the tagged
4373 -- full view unless we have an extension
4375 if Present (Full_View (Parent_Type))
4376 and then not Is_Tagged_Type (Full_View (Parent_Type))
4377 and then not Is_Completion
4378 then
4379 Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
4380 Chars (Derived_Type));
4381 Set_Is_Itype (Full_Der);
4382 Set_Has_Private_Declaration (Full_Der);
4383 Set_Has_Private_Declaration (Derived_Type);
4384 Set_Associated_Node_For_Itype (Full_Der, N);
4385 Set_Parent (Full_Der, Parent (Derived_Type));
4386 Set_Full_View (Derived_Type, Full_Der);
4388 if not In_Open_Scopes (Par_Scope) then
4389 Install_Private_Declarations (Par_Scope);
4390 Install_Visible_Declarations (Par_Scope);
4391 Copy_And_Build;
4392 Uninstall_Declarations (Par_Scope);
4394 -- If parent scope is open and in another unit, and
4395 -- parent has a completion, then the derivation is taking
4396 -- place in the visible part of a child unit. In that
4397 -- case retrieve the full view of the parent momentarily.
4399 elsif not In_Same_Source_Unit (N, Parent_Type) then
4400 Full_P := Full_View (Parent_Type);
4401 Exchange_Declarations (Parent_Type);
4402 Copy_And_Build;
4403 Exchange_Declarations (Full_P);
4405 -- Otherwise it is a local derivation.
4407 else
4408 Copy_And_Build;
4409 end if;
4411 Set_Scope (Full_Der, Current_Scope);
4412 Set_Is_First_Subtype (Full_Der,
4413 Is_First_Subtype (Derived_Type));
4414 Set_Has_Size_Clause (Full_Der, False);
4415 Set_Has_Alignment_Clause (Full_Der, False);
4416 Set_Next_Entity (Full_Der, Empty);
4417 Set_Has_Delayed_Freeze (Full_Der);
4418 Set_Is_Frozen (Full_Der, False);
4419 Set_Freeze_Node (Full_Der, Empty);
4420 Set_Depends_On_Private (Full_Der,
4421 Has_Private_Component (Full_Der));
4422 Set_Public_Status (Full_Der);
4423 end if;
4424 end if;
4426 Set_Has_Unknown_Discriminants (Derived_Type,
4427 Has_Unknown_Discriminants (Parent_Type));
4429 if Is_Private_Type (Derived_Type) then
4430 Set_Private_Dependents (Derived_Type, New_Elmt_List);
4431 end if;
4433 if Is_Private_Type (Parent_Type)
4434 and then Base_Type (Parent_Type) = Parent_Type
4435 and then In_Open_Scopes (Scope (Parent_Type))
4436 then
4437 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
4439 if Is_Child_Unit (Scope (Current_Scope))
4440 and then Is_Completion
4441 and then In_Private_Part (Current_Scope)
4442 and then Scope (Parent_Type) /= Current_Scope
4443 then
4444 -- This is the unusual case where a type completed by a private
4445 -- derivation occurs within a package nested in a child unit,
4446 -- and the parent is declared in an ancestor. In this case, the
4447 -- full view of the parent type will become visible in the body
4448 -- of the enclosing child, and only then will the current type
4449 -- be possibly non-private. We build a underlying full view that
4450 -- will be installed when the enclosing child body is compiled.
4452 declare
4453 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
4455 begin
4456 Full_Der :=
4457 Make_Defining_Identifier (Sloc (Derived_Type),
4458 Chars (Derived_Type));
4459 Set_Is_Itype (Full_Der);
4460 Set_Itype (IR, Full_Der);
4461 Insert_After (N, IR);
4463 -- The full view will be used to swap entities on entry/exit
4464 -- to the body, and must appear in the entity list for the
4465 -- package.
4467 Append_Entity (Full_Der, Scope (Derived_Type));
4468 Set_Has_Private_Declaration (Full_Der);
4469 Set_Has_Private_Declaration (Derived_Type);
4470 Set_Associated_Node_For_Itype (Full_Der, N);
4471 Set_Parent (Full_Der, Parent (Derived_Type));
4472 Full_P := Full_View (Parent_Type);
4473 Exchange_Declarations (Parent_Type);
4474 Copy_And_Build;
4475 Exchange_Declarations (Full_P);
4476 Set_Underlying_Full_View (Derived_Type, Full_Der);
4477 end;
4478 end if;
4479 end if;
4480 end Build_Derived_Private_Type;
4482 -------------------------------
4483 -- Build_Derived_Record_Type --
4484 -------------------------------
4486 -- 1. INTRODUCTION.
4488 -- Ideally we would like to use the same model of type derivation for
4489 -- tagged and untagged record types. Unfortunately this is not quite
4490 -- possible because the semantics of representation clauses is different
4491 -- for tagged and untagged records under inheritance. Consider the
4492 -- following:
4494 -- type R (...) is [tagged] record ... end record;
4495 -- type T (...) is new R (...) [with ...];
4497 -- The representation clauses of T can specify a completely different
4498 -- record layout from R's. Hence the same component can be placed in
4499 -- two very different positions in objects of type T and R. If R and T
4500 -- are tagged types, representation clauses for T can only specify the
4501 -- layout of non inherited components, thus components that are common
4502 -- in R and T have the same position in objects of type R and T.
4504 -- This has two implications. The first is that the entire tree for R's
4505 -- declaration needs to be copied for T in the untagged case, so that
4506 -- T can be viewed as a record type of its own with its own representation
4507 -- clauses. The second implication is the way we handle discriminants.
4508 -- Specifically, in the untagged case we need a way to communicate to Gigi
4509 -- what are the real discriminants in the record, while for the semantics
4510 -- we need to consider those introduced by the user to rename the
4511 -- discriminants in the parent type. This is handled by introducing the
4512 -- notion of stored discriminants. See below for more.
4514 -- Fortunately the way regular components are inherited can be handled in
4515 -- the same way in tagged and untagged types.
4517 -- To complicate things a bit more the private view of a private extension
4518 -- cannot be handled in the same way as the full view (for one thing the
4519 -- semantic rules are somewhat different). We will explain what differs
4520 -- below.
4522 -- 2. DISCRIMINANTS UNDER INHERITANCE.
4524 -- The semantic rules governing the discriminants of derived types are
4525 -- quite subtle.
4527 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
4528 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
4530 -- If parent type has discriminants, then the discriminants that are
4531 -- declared in the derived type are [3.4 (11)]:
4533 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
4534 -- there is one;
4536 -- o Otherwise, each discriminant of the parent type (implicitly
4537 -- declared in the same order with the same specifications). In this
4538 -- case, the discriminants are said to be "inherited", or if unknown in
4539 -- the parent are also unknown in the derived type.
4541 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
4543 -- o The parent subtype shall be constrained;
4545 -- o If the parent type is not a tagged type, then each discriminant of
4546 -- the derived type shall be used in the constraint defining a parent
4547 -- subtype [Implementation note: this ensures that the new discriminant
4548 -- can share storage with an existing discriminant.].
4550 -- For the derived type each discriminant of the parent type is either
4551 -- inherited, constrained to equal some new discriminant of the derived
4552 -- type, or constrained to the value of an expression.
4554 -- When inherited or constrained to equal some new discriminant, the
4555 -- parent discriminant and the discriminant of the derived type are said
4556 -- to "correspond".
4558 -- If a discriminant of the parent type is constrained to a specific value
4559 -- in the derived type definition, then the discriminant is said to be
4560 -- "specified" by that derived type definition.
4562 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES.
4564 -- We have spoken about stored discriminants in point 1 (introduction)
4565 -- above. There are two sort of stored discriminants: implicit and
4566 -- explicit. As long as the derived type inherits the same discriminants as
4567 -- the root record type, stored discriminants are the same as regular
4568 -- discriminants, and are said to be implicit. However, if any discriminant
4569 -- in the root type was renamed in the derived type, then the derived
4570 -- type will contain explicit stored discriminants. Explicit stored
4571 -- discriminants are discriminants in addition to the semantically visible
4572 -- discriminants defined for the derived type. Stored discriminants are
4573 -- used by Gigi to figure out what are the physical discriminants in
4574 -- objects of the derived type (see precise definition in einfo.ads).
4575 -- As an example, consider the following:
4577 -- type R (D1, D2, D3 : Int) is record ... end record;
4578 -- type T1 is new R;
4579 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
4580 -- type T3 is new T2;
4581 -- type T4 (Y : Int) is new T3 (Y, 99);
4583 -- The following table summarizes the discriminants and stored
4584 -- discriminants in R and T1 through T4.
4586 -- Type Discrim Stored Discrim Comment
4587 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
4588 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
4589 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
4590 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
4591 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
4593 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
4594 -- find the corresponding discriminant in the parent type, while
4595 -- Original_Record_Component (abbreviated ORC below), the actual physical
4596 -- component that is renamed. Finally the field Is_Completely_Hidden
4597 -- (abbreviated ICH below) is set for all explicit stored discriminants
4598 -- (see einfo.ads for more info). For the above example this gives:
4600 -- Discrim CD ORC ICH
4601 -- ^^^^^^^ ^^ ^^^ ^^^
4602 -- D1 in R empty itself no
4603 -- D2 in R empty itself no
4604 -- D3 in R empty itself no
4606 -- D1 in T1 D1 in R itself no
4607 -- D2 in T1 D2 in R itself no
4608 -- D3 in T1 D3 in R itself no
4610 -- X1 in T2 D3 in T1 D3 in T2 no
4611 -- X2 in T2 D1 in T1 D1 in T2 no
4612 -- D1 in T2 empty itself yes
4613 -- D2 in T2 empty itself yes
4614 -- D3 in T2 empty itself yes
4616 -- X1 in T3 X1 in T2 D3 in T3 no
4617 -- X2 in T3 X2 in T2 D1 in T3 no
4618 -- D1 in T3 empty itself yes
4619 -- D2 in T3 empty itself yes
4620 -- D3 in T3 empty itself yes
4622 -- Y in T4 X1 in T3 D3 in T3 no
4623 -- D1 in T3 empty itself yes
4624 -- D2 in T3 empty itself yes
4625 -- D3 in T3 empty itself yes
4627 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES.
4629 -- Type derivation for tagged types is fairly straightforward. if no
4630 -- discriminants are specified by the derived type, these are inherited
4631 -- from the parent. No explicit stored discriminants are ever necessary.
4632 -- The only manipulation that is done to the tree is that of adding a
4633 -- _parent field with parent type and constrained to the same constraint
4634 -- specified for the parent in the derived type definition. For instance:
4636 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
4637 -- type T1 is new R with null record;
4638 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
4640 -- are changed into :
4642 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
4643 -- _parent : R (D1, D2, D3);
4644 -- end record;
4646 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
4647 -- _parent : T1 (X2, 88, X1);
4648 -- end record;
4650 -- The discriminants actually present in R, T1 and T2 as well as their CD,
4651 -- ORC and ICH fields are:
4653 -- Discrim CD ORC ICH
4654 -- ^^^^^^^ ^^ ^^^ ^^^
4655 -- D1 in R empty itself no
4656 -- D2 in R empty itself no
4657 -- D3 in R empty itself no
4659 -- D1 in T1 D1 in R D1 in R no
4660 -- D2 in T1 D2 in R D2 in R no
4661 -- D3 in T1 D3 in R D3 in R no
4663 -- X1 in T2 D3 in T1 D3 in R no
4664 -- X2 in T2 D1 in T1 D1 in R no
4666 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS.
4668 -- Regardless of whether we dealing with a tagged or untagged type
4669 -- we will transform all derived type declarations of the form
4671 -- type T is new R (...) [with ...];
4672 -- or
4673 -- subtype S is R (...);
4674 -- type T is new S [with ...];
4675 -- into
4676 -- type BT is new R [with ...];
4677 -- subtype T is BT (...);
4679 -- That is, the base derived type is constrained only if it has no
4680 -- discriminants. The reason for doing this is that GNAT's semantic model
4681 -- assumes that a base type with discriminants is unconstrained.
4683 -- Note that, strictly speaking, the above transformation is not always
4684 -- correct. Consider for instance the following excerpt from ACVC b34011a:
4686 -- procedure B34011A is
4687 -- type REC (D : integer := 0) is record
4688 -- I : Integer;
4689 -- end record;
4691 -- package P is
4692 -- type T6 is new Rec;
4693 -- function F return T6;
4694 -- end P;
4696 -- use P;
4697 -- package Q6 is
4698 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
4699 -- end Q6;
4701 -- The definition of Q6.U is illegal. However transforming Q6.U into
4703 -- type BaseU is new T6;
4704 -- subtype U is BaseU (Q6.F.I)
4706 -- turns U into a legal subtype, which is incorrect. To avoid this problem
4707 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
4708 -- the transformation described above.
4710 -- There is another instance where the above transformation is incorrect.
4711 -- Consider:
4713 -- package Pack is
4714 -- type Base (D : Integer) is tagged null record;
4715 -- procedure P (X : Base);
4717 -- type Der is new Base (2) with null record;
4718 -- procedure P (X : Der);
4719 -- end Pack;
4721 -- Then the above transformation turns this into
4723 -- type Der_Base is new Base with null record;
4724 -- -- procedure P (X : Base) is implicitly inherited here
4725 -- -- as procedure P (X : Der_Base).
4727 -- subtype Der is Der_Base (2);
4728 -- procedure P (X : Der);
4729 -- -- The overriding of P (X : Der_Base) is illegal since we
4730 -- -- have a parameter conformance problem.
4732 -- To get around this problem, after having semantically processed Der_Base
4733 -- and the rewritten subtype declaration for Der, we copy Der_Base field
4734 -- Discriminant_Constraint from Der so that when parameter conformance is
4735 -- checked when P is overridden, no semantic errors are flagged.
4737 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS.
4739 -- Regardless of whether we are dealing with a tagged or untagged type
4740 -- we will transform all derived type declarations of the form
4742 -- type R (D1, .., Dn : ...) is [tagged] record ...;
4743 -- type T is new R [with ...];
4744 -- into
4745 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
4747 -- The reason for such transformation is that it allows us to implement a
4748 -- very clean form of component inheritance as explained below.
4750 -- Note that this transformation is not achieved by direct tree rewriting
4751 -- and manipulation, but rather by redoing the semantic actions that the
4752 -- above transformation will entail. This is done directly in routine
4753 -- Inherit_Components.
4755 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE.
4757 -- In both tagged and untagged derived types, regular non discriminant
4758 -- components are inherited in the derived type from the parent type. In
4759 -- the absence of discriminants component, inheritance is straightforward
4760 -- as components can simply be copied from the parent.
4761 -- If the parent has discriminants, inheriting components constrained with
4762 -- these discriminants requires caution. Consider the following example:
4764 -- type R (D1, D2 : Positive) is [tagged] record
4765 -- S : String (D1 .. D2);
4766 -- end record;
4768 -- type T1 is new R [with null record];
4769 -- type T2 (X : positive) is new R (1, X) [with null record];
4771 -- As explained in 6. above, T1 is rewritten as
4773 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
4775 -- which makes the treatment for T1 and T2 identical.
4777 -- What we want when inheriting S, is that references to D1 and D2 in R are
4778 -- replaced with references to their correct constraints, ie D1 and D2 in
4779 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
4780 -- with either discriminant references in the derived type or expressions.
4781 -- This replacement is achieved as follows: before inheriting R's
4782 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
4783 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
4784 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
4785 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
4786 -- by String (1 .. X).
4788 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS.
4790 -- We explain here the rules governing private type extensions relevant to
4791 -- type derivation. These rules are explained on the following example:
4793 -- type D [(...)] is new A [(...)] with private; <-- partial view
4794 -- type D [(...)] is new P [(...)] with null record; <-- full view
4796 -- Type A is called the ancestor subtype of the private extension.
4797 -- Type P is the parent type of the full view of the private extension. It
4798 -- must be A or a type derived from A.
4800 -- The rules concerning the discriminants of private type extensions are
4801 -- [7.3(10-13)]:
4803 -- o If a private extension inherits known discriminants from the ancestor
4804 -- subtype, then the full view shall also inherit its discriminants from
4805 -- the ancestor subtype and the parent subtype of the full view shall be
4806 -- constrained if and only if the ancestor subtype is constrained.
4808 -- o If a partial view has unknown discriminants, then the full view may
4809 -- define a definite or an indefinite subtype, with or without
4810 -- discriminants.
4812 -- o If a partial view has neither known nor unknown discriminants, then
4813 -- the full view shall define a definite subtype.
4815 -- o If the ancestor subtype of a private extension has constrained
4816 -- discriminants, then the parent subtype of the full view shall impose a
4817 -- statically matching constraint on those discriminants.
4819 -- This means that only the following forms of private extensions are
4820 -- allowed:
4822 -- type D is new A with private; <-- partial view
4823 -- type D is new P with null record; <-- full view
4825 -- If A has no discriminants than P has no discriminants, otherwise P must
4826 -- inherit A's discriminants.
4828 -- type D is new A (...) with private; <-- partial view
4829 -- type D is new P (:::) with null record; <-- full view
4831 -- P must inherit A's discriminants and (...) and (:::) must statically
4832 -- match.
4834 -- subtype A is R (...);
4835 -- type D is new A with private; <-- partial view
4836 -- type D is new P with null record; <-- full view
4838 -- P must have inherited R's discriminants and must be derived from A or
4839 -- any of its subtypes.
4841 -- type D (..) is new A with private; <-- partial view
4842 -- type D (..) is new P [(:::)] with null record; <-- full view
4844 -- No specific constraints on P's discriminants or constraint (:::).
4845 -- Note that A can be unconstrained, but the parent subtype P must either
4846 -- be constrained or (:::) must be present.
4848 -- type D (..) is new A [(...)] with private; <-- partial view
4849 -- type D (..) is new P [(:::)] with null record; <-- full view
4851 -- P's constraints on A's discriminants must statically match those
4852 -- imposed by (...).
4854 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS.
4856 -- The full view of a private extension is handled exactly as described
4857 -- above. The model chose for the private view of a private extension
4858 -- is the same for what concerns discriminants (ie they receive the same
4859 -- treatment as in the tagged case). However, the private view of the
4860 -- private extension always inherits the components of the parent base,
4861 -- without replacing any discriminant reference. Strictly speaking this
4862 -- is incorrect. However, Gigi never uses this view to generate code so
4863 -- this is a purely semantic issue. In theory, a set of transformations
4864 -- similar to those given in 5. and 6. above could be applied to private
4865 -- views of private extensions to have the same model of component
4866 -- inheritance as for non private extensions. However, this is not done
4867 -- because it would further complicate private type processing.
4868 -- Semantically speaking, this leaves us in an uncomfortable
4869 -- situation. As an example consider:
4871 -- package Pack is
4872 -- type R (D : integer) is tagged record
4873 -- S : String (1 .. D);
4874 -- end record;
4875 -- procedure P (X : R);
4876 -- type T is new R (1) with private;
4877 -- private
4878 -- type T is new R (1) with null record;
4879 -- end;
4881 -- This is transformed into:
4883 -- package Pack is
4884 -- type R (D : integer) is tagged record
4885 -- S : String (1 .. D);
4886 -- end record;
4887 -- procedure P (X : R);
4888 -- type T is new R (1) with private;
4889 -- private
4890 -- type BaseT is new R with null record;
4891 -- subtype T is BaseT (1);
4892 -- end;
4894 -- (strictly speaking the above is incorrect Ada).
4896 -- From the semantic standpoint the private view of private extension T
4897 -- should be flagged as constrained since one can clearly have
4899 -- Obj : T;
4901 -- in a unit withing Pack. However, when deriving subprograms for the
4902 -- private view of private extension T, T must be seen as unconstrained
4903 -- since T has discriminants (this is a constraint of the current
4904 -- subprogram derivation model). Thus, when processing the private view of
4905 -- a private extension such as T, we first mark T as unconstrained, we
4906 -- process it, we perform program derivation and just before returning from
4907 -- Build_Derived_Record_Type we mark T as constrained.
4908 -- ??? Are there are other uncomfortable cases that we will have to
4909 -- deal with.
4911 -- 10. RECORD_TYPE_WITH_PRIVATE complications.
4913 -- Types that are derived from a visible record type and have a private
4914 -- extension present other peculiarities. They behave mostly like private
4915 -- types, but if they have primitive operations defined, these will not
4916 -- have the proper signatures for further inheritance, because other
4917 -- primitive operations will use the implicit base that we define for
4918 -- private derivations below. This affect subprogram inheritance (see
4919 -- Derive_Subprograms for details). We also derive the implicit base from
4920 -- the base type of the full view, so that the implicit base is a record
4921 -- type and not another private type, This avoids infinite loops.
4923 procedure Build_Derived_Record_Type
4924 (N : Node_Id;
4925 Parent_Type : Entity_Id;
4926 Derived_Type : Entity_Id;
4927 Derive_Subps : Boolean := True)
4929 Loc : constant Source_Ptr := Sloc (N);
4930 Parent_Base : Entity_Id;
4932 Type_Def : Node_Id;
4933 Indic : Node_Id;
4935 Discrim : Entity_Id;
4936 Last_Discrim : Entity_Id;
4937 Constrs : Elist_Id;
4938 Discs : Elist_Id := New_Elmt_List;
4939 -- An empty Discs list means that there were no constraints in the
4940 -- subtype indication or that there was an error processing it.
4942 Assoc_List : Elist_Id;
4943 New_Discrs : Elist_Id;
4945 New_Base : Entity_Id;
4946 New_Decl : Node_Id;
4947 New_Indic : Node_Id;
4949 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
4950 Discriminant_Specs : constant Boolean :=
4951 Present (Discriminant_Specifications (N));
4952 Private_Extension : constant Boolean :=
4953 (Nkind (N) = N_Private_Extension_Declaration);
4955 Constraint_Present : Boolean;
4956 Inherit_Discrims : Boolean := False;
4958 Save_Etype : Entity_Id;
4959 Save_Discr_Constr : Elist_Id;
4960 Save_Next_Entity : Entity_Id;
4962 begin
4963 if Ekind (Parent_Type) = E_Record_Type_With_Private
4964 and then Present (Full_View (Parent_Type))
4965 and then Has_Discriminants (Parent_Type)
4966 then
4967 Parent_Base := Base_Type (Full_View (Parent_Type));
4968 else
4969 Parent_Base := Base_Type (Parent_Type);
4970 end if;
4972 -- Before we start the previously documented transformations, here is
4973 -- a little fix for size and alignment of tagged types. Normally when
4974 -- we derive type D from type P, we copy the size and alignment of P
4975 -- as the default for D, and in the absence of explicit representation
4976 -- clauses for D, the size and alignment are indeed the same as the
4977 -- parent.
4979 -- But this is wrong for tagged types, since fields may be added,
4980 -- and the default size may need to be larger, and the default
4981 -- alignment may need to be larger.
4983 -- We therefore reset the size and alignment fields in the tagged
4984 -- case. Note that the size and alignment will in any case be at
4985 -- least as large as the parent type (since the derived type has
4986 -- a copy of the parent type in the _parent field)
4988 if Is_Tagged then
4989 Init_Size_Align (Derived_Type);
4990 end if;
4992 -- STEP 0a: figure out what kind of derived type declaration we have.
4994 if Private_Extension then
4995 Type_Def := N;
4996 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
4998 else
4999 Type_Def := Type_Definition (N);
5001 -- Ekind (Parent_Base) in not necessarily E_Record_Type since
5002 -- Parent_Base can be a private type or private extension. However,
5003 -- for tagged types with an extension the newly added fields are
5004 -- visible and hence the Derived_Type is always an E_Record_Type.
5005 -- (except that the parent may have its own private fields).
5006 -- For untagged types we preserve the Ekind of the Parent_Base.
5008 if Present (Record_Extension_Part (Type_Def)) then
5009 Set_Ekind (Derived_Type, E_Record_Type);
5010 else
5011 Set_Ekind (Derived_Type, Ekind (Parent_Base));
5012 end if;
5013 end if;
5015 -- Indic can either be an N_Identifier if the subtype indication
5016 -- contains no constraint or an N_Subtype_Indication if the subtype
5017 -- indication has a constraint.
5019 Indic := Subtype_Indication (Type_Def);
5020 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
5022 -- Check that the type has visible discriminants. The type may be
5023 -- a private type with unknown discriminants whose full view has
5024 -- discriminants which are invisible.
5026 if Constraint_Present then
5027 if not Has_Discriminants (Parent_Base)
5028 or else
5029 (Has_Unknown_Discriminants (Parent_Base)
5030 and then Is_Private_Type (Parent_Base))
5031 then
5032 Error_Msg_N
5033 ("invalid constraint: type has no discriminant",
5034 Constraint (Indic));
5036 Constraint_Present := False;
5037 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
5039 elsif Is_Constrained (Parent_Type) then
5040 Error_Msg_N
5041 ("invalid constraint: parent type is already constrained",
5042 Constraint (Indic));
5044 Constraint_Present := False;
5045 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
5046 end if;
5047 end if;
5049 -- STEP 0b: If needed, apply transformation given in point 5. above.
5051 if not Private_Extension
5052 and then Has_Discriminants (Parent_Type)
5053 and then not Discriminant_Specs
5054 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
5055 then
5056 -- First, we must analyze the constraint (see comment in point 5.).
5058 if Constraint_Present then
5059 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
5061 if Has_Discriminants (Derived_Type)
5062 and then Has_Private_Declaration (Derived_Type)
5063 and then Present (Discriminant_Constraint (Derived_Type))
5064 then
5065 -- Verify that constraints of the full view conform to those
5066 -- given in partial view.
5068 declare
5069 C1, C2 : Elmt_Id;
5071 begin
5072 C1 := First_Elmt (New_Discrs);
5073 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
5075 while Present (C1) and then Present (C2) loop
5076 if not
5077 Fully_Conformant_Expressions (Node (C1), Node (C2))
5078 then
5079 Error_Msg_N (
5080 "constraint not conformant to previous declaration",
5081 Node (C1));
5082 end if;
5083 Next_Elmt (C1);
5084 Next_Elmt (C2);
5085 end loop;
5086 end;
5087 end if;
5088 end if;
5090 -- Insert and analyze the declaration for the unconstrained base type
5092 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
5094 New_Decl :=
5095 Make_Full_Type_Declaration (Loc,
5096 Defining_Identifier => New_Base,
5097 Type_Definition =>
5098 Make_Derived_Type_Definition (Loc,
5099 Abstract_Present => Abstract_Present (Type_Def),
5100 Subtype_Indication =>
5101 New_Occurrence_Of (Parent_Base, Loc),
5102 Record_Extension_Part =>
5103 Relocate_Node (Record_Extension_Part (Type_Def))));
5105 Set_Parent (New_Decl, Parent (N));
5106 Mark_Rewrite_Insertion (New_Decl);
5107 Insert_Before (N, New_Decl);
5109 -- Note that this call passes False for the Derive_Subps
5110 -- parameter because subprogram derivation is deferred until
5111 -- after creating the subtype (see below).
5113 Build_Derived_Type
5114 (New_Decl, Parent_Base, New_Base,
5115 Is_Completion => True, Derive_Subps => False);
5117 -- ??? This needs re-examination to determine whether the
5118 -- above call can simply be replaced by a call to Analyze.
5120 Set_Analyzed (New_Decl);
5122 -- Insert and analyze the declaration for the constrained subtype
5124 if Constraint_Present then
5125 New_Indic :=
5126 Make_Subtype_Indication (Loc,
5127 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
5128 Constraint => Relocate_Node (Constraint (Indic)));
5130 else
5131 declare
5132 Constr_List : constant List_Id := New_List;
5133 C : Elmt_Id;
5134 Expr : Node_Id;
5136 begin
5137 C := First_Elmt (Discriminant_Constraint (Parent_Type));
5138 while Present (C) loop
5139 Expr := Node (C);
5141 -- It is safe here to call New_Copy_Tree since
5142 -- Force_Evaluation was called on each constraint in
5143 -- Build_Discriminant_Constraints.
5145 Append (New_Copy_Tree (Expr), To => Constr_List);
5147 Next_Elmt (C);
5148 end loop;
5150 New_Indic :=
5151 Make_Subtype_Indication (Loc,
5152 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
5153 Constraint =>
5154 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
5155 end;
5156 end if;
5158 Rewrite (N,
5159 Make_Subtype_Declaration (Loc,
5160 Defining_Identifier => Derived_Type,
5161 Subtype_Indication => New_Indic));
5163 Analyze (N);
5165 -- Derivation of subprograms must be delayed until the
5166 -- full subtype has been established to ensure proper
5167 -- overriding of subprograms inherited by full types.
5168 -- If the derivations occurred as part of the call to
5169 -- Build_Derived_Type above, then the check for type
5170 -- conformance would fail because earlier primitive
5171 -- subprograms could still refer to the full type prior
5172 -- the change to the new subtype and hence wouldn't
5173 -- match the new base type created here.
5175 Derive_Subprograms (Parent_Type, Derived_Type);
5177 -- For tagged types the Discriminant_Constraint of the new base itype
5178 -- is inherited from the first subtype so that no subtype conformance
5179 -- problem arise when the first subtype overrides primitive
5180 -- operations inherited by the implicit base type.
5182 if Is_Tagged then
5183 Set_Discriminant_Constraint
5184 (New_Base, Discriminant_Constraint (Derived_Type));
5185 end if;
5187 return;
5188 end if;
5190 -- If we get here Derived_Type will have no discriminants or it will be
5191 -- a discriminated unconstrained base type.
5193 -- STEP 1a: perform preliminary actions/checks for derived tagged types
5195 if Is_Tagged then
5196 -- The parent type is frozen for non-private extensions (RM 13.14(7))
5198 if not Private_Extension then
5199 Freeze_Before (N, Parent_Type);
5200 end if;
5202 if Type_Access_Level (Derived_Type) /= Type_Access_Level (Parent_Type)
5203 and then not Is_Generic_Type (Derived_Type)
5204 then
5205 if Is_Controlled (Parent_Type) then
5206 Error_Msg_N
5207 ("controlled type must be declared at the library level",
5208 Indic);
5209 else
5210 Error_Msg_N
5211 ("type extension at deeper accessibility level than parent",
5212 Indic);
5213 end if;
5215 else
5216 declare
5217 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
5219 begin
5220 if Present (GB)
5221 and then GB /= Enclosing_Generic_Body (Parent_Base)
5222 then
5223 Error_Msg_NE
5224 ("parent type of& must not be outside generic body"
5225 & " ('R'M 3.9.1(4))",
5226 Indic, Derived_Type);
5227 end if;
5228 end;
5229 end if;
5230 end if;
5232 -- STEP 1b : preliminary cleanup of the full view of private types
5234 -- If the type is already marked as having discriminants, then it's the
5235 -- completion of a private type or private extension and we need to
5236 -- retain the discriminants from the partial view if the current
5237 -- declaration has Discriminant_Specifications so that we can verify
5238 -- conformance. However, we must remove any existing components that
5239 -- were inherited from the parent (and attached in Copy_And_Swap)
5240 -- because the full type inherits all appropriate components anyway, and
5241 -- we don't want the partial view's components interfering.
5243 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
5244 Discrim := First_Discriminant (Derived_Type);
5245 loop
5246 Last_Discrim := Discrim;
5247 Next_Discriminant (Discrim);
5248 exit when No (Discrim);
5249 end loop;
5251 Set_Last_Entity (Derived_Type, Last_Discrim);
5253 -- In all other cases wipe out the list of inherited components (even
5254 -- inherited discriminants), it will be properly rebuilt here.
5256 else
5257 Set_First_Entity (Derived_Type, Empty);
5258 Set_Last_Entity (Derived_Type, Empty);
5259 end if;
5261 -- STEP 1c: Initialize some flags for the Derived_Type
5263 -- The following flags must be initialized here so that
5264 -- Process_Discriminants can check that discriminants of tagged types
5265 -- do not have a default initial value and that access discriminants
5266 -- are only specified for limited records. For completeness, these
5267 -- flags are also initialized along with all the other flags below.
5269 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
5270 Set_Is_Limited_Record (Derived_Type, Is_Limited_Record (Parent_Type));
5272 -- STEP 2a: process discriminants of derived type if any.
5274 New_Scope (Derived_Type);
5276 if Discriminant_Specs then
5277 Set_Has_Unknown_Discriminants (Derived_Type, False);
5279 -- The following call initializes fields Has_Discriminants and
5280 -- Discriminant_Constraint, unless we are processing the completion
5281 -- of a private type declaration.
5283 Check_Or_Process_Discriminants (N, Derived_Type);
5285 -- For non-tagged types the constraint on the Parent_Type must be
5286 -- present and is used to rename the discriminants.
5288 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
5289 Error_Msg_N ("untagged parent must have discriminants", Indic);
5291 elsif not Is_Tagged and then not Constraint_Present then
5292 Error_Msg_N
5293 ("discriminant constraint needed for derived untagged records",
5294 Indic);
5296 -- Otherwise the parent subtype must be constrained unless we have a
5297 -- private extension.
5299 elsif not Constraint_Present
5300 and then not Private_Extension
5301 and then not Is_Constrained (Parent_Type)
5302 then
5303 Error_Msg_N
5304 ("unconstrained type not allowed in this context", Indic);
5306 elsif Constraint_Present then
5307 -- The following call sets the field Corresponding_Discriminant
5308 -- for the discriminants in the Derived_Type.
5310 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
5312 -- For untagged types all new discriminants must rename
5313 -- discriminants in the parent. For private extensions new
5314 -- discriminants cannot rename old ones (implied by [7.3(13)]).
5316 Discrim := First_Discriminant (Derived_Type);
5318 while Present (Discrim) loop
5319 if not Is_Tagged
5320 and then not Present (Corresponding_Discriminant (Discrim))
5321 then
5322 Error_Msg_N
5323 ("new discriminants must constrain old ones", Discrim);
5325 elsif Private_Extension
5326 and then Present (Corresponding_Discriminant (Discrim))
5327 then
5328 Error_Msg_N
5329 ("only static constraints allowed for parent"
5330 & " discriminants in the partial view", Indic);
5331 exit;
5332 end if;
5334 -- If a new discriminant is used in the constraint,
5335 -- then its subtype must be statically compatible
5336 -- with the parent discriminant's subtype (3.7(15)).
5338 if Present (Corresponding_Discriminant (Discrim))
5339 and then
5340 not Subtypes_Statically_Compatible
5341 (Etype (Discrim),
5342 Etype (Corresponding_Discriminant (Discrim)))
5343 then
5344 Error_Msg_N
5345 ("subtype must be compatible with parent discriminant",
5346 Discrim);
5347 end if;
5349 Next_Discriminant (Discrim);
5350 end loop;
5352 -- Check whether the constraints of the full view statically
5353 -- match those imposed by the parent subtype [7.3(13)].
5355 if Present (Stored_Constraint (Derived_Type)) then
5356 declare
5357 C1, C2 : Elmt_Id;
5359 begin
5360 C1 := First_Elmt (Discs);
5361 C2 := First_Elmt (Stored_Constraint (Derived_Type));
5362 while Present (C1) and then Present (C2) loop
5363 if not
5364 Fully_Conformant_Expressions (Node (C1), Node (C2))
5365 then
5366 Error_Msg_N (
5367 "not conformant with previous declaration",
5368 Node (C1));
5369 end if;
5371 Next_Elmt (C1);
5372 Next_Elmt (C2);
5373 end loop;
5374 end;
5375 end if;
5376 end if;
5378 -- STEP 2b: No new discriminants, inherit discriminants if any
5380 else
5381 if Private_Extension then
5382 Set_Has_Unknown_Discriminants
5383 (Derived_Type,
5384 Has_Unknown_Discriminants (Parent_Type)
5385 or else Unknown_Discriminants_Present (N));
5387 -- The partial view of the parent may have unknown discriminants,
5388 -- but if the full view has discriminants and the parent type is
5389 -- in scope they must be inherited.
5391 elsif Has_Unknown_Discriminants (Parent_Type)
5392 and then
5393 (not Has_Discriminants (Parent_Type)
5394 or else not In_Open_Scopes (Scope (Parent_Type)))
5395 then
5396 Set_Has_Unknown_Discriminants (Derived_Type);
5397 end if;
5399 if not Has_Unknown_Discriminants (Derived_Type)
5400 and then Has_Discriminants (Parent_Type)
5401 then
5402 Inherit_Discrims := True;
5403 Set_Has_Discriminants
5404 (Derived_Type, True);
5405 Set_Discriminant_Constraint
5406 (Derived_Type, Discriminant_Constraint (Parent_Base));
5407 end if;
5409 -- The following test is true for private types (remember
5410 -- transformation 5. is not applied to those) and in an error
5411 -- situation.
5413 if Constraint_Present then
5414 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
5415 end if;
5417 -- For now mark a new derived type as constrained only if it has no
5418 -- discriminants. At the end of Build_Derived_Record_Type we properly
5419 -- set this flag in the case of private extensions. See comments in
5420 -- point 9. just before body of Build_Derived_Record_Type.
5422 Set_Is_Constrained
5423 (Derived_Type,
5424 not (Inherit_Discrims
5425 or else Has_Unknown_Discriminants (Derived_Type)));
5426 end if;
5428 -- STEP 3: initialize fields of derived type.
5430 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
5431 Set_Stored_Constraint (Derived_Type, No_Elist);
5433 -- Fields inherited from the Parent_Type
5435 Set_Discard_Names
5436 (Derived_Type, Einfo.Discard_Names (Parent_Type));
5437 Set_Has_Specified_Layout
5438 (Derived_Type, Has_Specified_Layout (Parent_Type));
5439 Set_Is_Limited_Composite
5440 (Derived_Type, Is_Limited_Composite (Parent_Type));
5441 Set_Is_Limited_Record
5442 (Derived_Type, Is_Limited_Record (Parent_Type));
5443 Set_Is_Private_Composite
5444 (Derived_Type, Is_Private_Composite (Parent_Type));
5446 -- Fields inherited from the Parent_Base
5448 Set_Has_Controlled_Component
5449 (Derived_Type, Has_Controlled_Component (Parent_Base));
5450 Set_Has_Non_Standard_Rep
5451 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
5452 Set_Has_Primitive_Operations
5453 (Derived_Type, Has_Primitive_Operations (Parent_Base));
5455 -- Direct controlled types do not inherit Finalize_Storage_Only flag
5457 if not Is_Controlled (Parent_Type) then
5458 Set_Finalize_Storage_Only
5459 (Derived_Type, Finalize_Storage_Only (Parent_Type));
5460 end if;
5462 -- Set fields for private derived types.
5464 if Is_Private_Type (Derived_Type) then
5465 Set_Depends_On_Private (Derived_Type, True);
5466 Set_Private_Dependents (Derived_Type, New_Elmt_List);
5468 -- Inherit fields from non private record types. If this is the
5469 -- completion of a derivation from a private type, the parent itself
5470 -- is private, and the attributes come from its full view, which must
5471 -- be present.
5473 else
5474 if Is_Private_Type (Parent_Base)
5475 and then not Is_Record_Type (Parent_Base)
5476 then
5477 Set_Component_Alignment
5478 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
5479 Set_C_Pass_By_Copy
5480 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
5481 else
5482 Set_Component_Alignment
5483 (Derived_Type, Component_Alignment (Parent_Base));
5485 Set_C_Pass_By_Copy
5486 (Derived_Type, C_Pass_By_Copy (Parent_Base));
5487 end if;
5488 end if;
5490 -- Set fields for tagged types
5492 if Is_Tagged then
5493 Set_Primitive_Operations (Derived_Type, New_Elmt_List);
5495 -- All tagged types defined in Ada.Finalization are controlled
5497 if Chars (Scope (Derived_Type)) = Name_Finalization
5498 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
5499 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
5500 then
5501 Set_Is_Controlled (Derived_Type);
5502 else
5503 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
5504 end if;
5506 Make_Class_Wide_Type (Derived_Type);
5507 Set_Is_Abstract (Derived_Type, Abstract_Present (Type_Def));
5509 if Has_Discriminants (Derived_Type)
5510 and then Constraint_Present
5511 then
5512 Set_Stored_Constraint
5513 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
5514 end if;
5516 else
5517 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
5518 Set_Has_Non_Standard_Rep
5519 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
5520 end if;
5522 -- STEP 4: Inherit components from the parent base and constrain them.
5523 -- Apply the second transformation described in point 6. above.
5525 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
5526 or else not Has_Discriminants (Parent_Type)
5527 or else not Is_Constrained (Parent_Type)
5528 then
5529 Constrs := Discs;
5530 else
5531 Constrs := Discriminant_Constraint (Parent_Type);
5532 end if;
5534 Assoc_List := Inherit_Components (N,
5535 Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
5537 -- STEP 5a: Copy the parent record declaration for untagged types
5539 if not Is_Tagged then
5541 -- Discriminant_Constraint (Derived_Type) has been properly
5542 -- constructed. Save it and temporarily set it to Empty because we do
5543 -- not want the call to New_Copy_Tree below to mess this list.
5545 if Has_Discriminants (Derived_Type) then
5546 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
5547 Set_Discriminant_Constraint (Derived_Type, No_Elist);
5548 else
5549 Save_Discr_Constr := No_Elist;
5550 end if;
5552 -- Save the Etype field of Derived_Type. It is correctly set now, but
5553 -- the call to New_Copy tree may remap it to point to itself, which
5554 -- is not what we want. Ditto for the Next_Entity field.
5556 Save_Etype := Etype (Derived_Type);
5557 Save_Next_Entity := Next_Entity (Derived_Type);
5559 -- Assoc_List maps all stored discriminants in the Parent_Base to
5560 -- stored discriminants in the Derived_Type. It is fundamental that
5561 -- no types or itypes with discriminants other than the stored
5562 -- discriminants appear in the entities declared inside
5563 -- Derived_Type. Gigi won't like it.
5565 New_Decl :=
5566 New_Copy_Tree
5567 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
5569 -- Restore the fields saved prior to the New_Copy_Tree call
5570 -- and compute the stored constraint.
5572 Set_Etype (Derived_Type, Save_Etype);
5573 Set_Next_Entity (Derived_Type, Save_Next_Entity);
5575 if Has_Discriminants (Derived_Type) then
5576 Set_Discriminant_Constraint
5577 (Derived_Type, Save_Discr_Constr);
5578 Set_Stored_Constraint
5579 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
5580 Replace_Components (Derived_Type, New_Decl);
5581 end if;
5583 -- Insert the new derived type declaration
5585 Rewrite (N, New_Decl);
5587 -- STEP 5b: Complete the processing for record extensions in generics
5589 -- There is no completion for record extensions declared in the
5590 -- parameter part of a generic, so we need to complete processing for
5591 -- these generic record extensions here. The Record_Type_Definition call
5592 -- will change the Ekind of the components from E_Void to E_Component.
5594 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
5595 Record_Type_Definition (Empty, Derived_Type);
5597 -- STEP 5c: Process the record extension for non private tagged types.
5599 elsif not Private_Extension then
5600 -- Add the _parent field in the derived type.
5602 Expand_Derived_Record (Derived_Type, Type_Def);
5604 -- Analyze the record extension
5606 Record_Type_Definition
5607 (Record_Extension_Part (Type_Def), Derived_Type);
5608 end if;
5610 End_Scope;
5612 if Etype (Derived_Type) = Any_Type then
5613 return;
5614 end if;
5616 -- Set delayed freeze and then derive subprograms, we need to do
5617 -- this in this order so that derived subprograms inherit the
5618 -- derived freeze if necessary.
5620 Set_Has_Delayed_Freeze (Derived_Type);
5621 if Derive_Subps then
5622 Derive_Subprograms (Parent_Type, Derived_Type);
5623 end if;
5625 -- If we have a private extension which defines a constrained derived
5626 -- type mark as constrained here after we have derived subprograms. See
5627 -- comment on point 9. just above the body of Build_Derived_Record_Type.
5629 if Private_Extension and then Inherit_Discrims then
5630 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
5631 Set_Is_Constrained (Derived_Type, True);
5632 Set_Discriminant_Constraint (Derived_Type, Discs);
5634 elsif Is_Constrained (Parent_Type) then
5635 Set_Is_Constrained
5636 (Derived_Type, True);
5637 Set_Discriminant_Constraint
5638 (Derived_Type, Discriminant_Constraint (Parent_Type));
5639 end if;
5640 end if;
5642 end Build_Derived_Record_Type;
5644 ------------------------
5645 -- Build_Derived_Type --
5646 ------------------------
5648 procedure Build_Derived_Type
5649 (N : Node_Id;
5650 Parent_Type : Entity_Id;
5651 Derived_Type : Entity_Id;
5652 Is_Completion : Boolean;
5653 Derive_Subps : Boolean := True)
5655 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5657 begin
5658 -- Set common attributes
5660 Set_Scope (Derived_Type, Current_Scope);
5662 Set_Ekind (Derived_Type, Ekind (Parent_Base));
5663 Set_Etype (Derived_Type, Parent_Base);
5664 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
5666 Set_Size_Info (Derived_Type, Parent_Type);
5667 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5668 Set_Convention (Derived_Type, Convention (Parent_Type));
5669 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
5671 -- The derived type inherits the representation clauses of the parent.
5672 -- However, for a private type that is completed by a derivation, there
5673 -- may be operation attributes that have been specified already (stream
5674 -- attributes and External_Tag) and those must be provided. Finally,
5675 -- if the partial view is a private extension, the representation items
5676 -- of the parent have been inherited already, and should not be chained
5677 -- twice to the derived type.
5679 if Is_Tagged_Type (Parent_Type)
5680 and then Present (First_Rep_Item (Derived_Type))
5681 then
5682 -- The existing items are either operational items or items inherited
5683 -- from a private extension declaration.
5685 declare
5686 Rep : Node_Id := First_Rep_Item (Derived_Type);
5687 Found : Boolean := False;
5689 begin
5690 while Present (Rep) loop
5691 if Rep = First_Rep_Item (Parent_Type) then
5692 Found := True;
5693 exit;
5694 else
5695 Rep := Next_Rep_Item (Rep);
5696 end if;
5697 end loop;
5699 if not Found then
5700 Set_Next_Rep_Item
5701 (First_Rep_Item (Derived_Type), First_Rep_Item (Parent_Type));
5702 end if;
5703 end;
5705 else
5706 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
5707 end if;
5709 case Ekind (Parent_Type) is
5710 when Numeric_Kind =>
5711 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
5713 when Array_Kind =>
5714 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
5716 when E_Record_Type
5717 | E_Record_Subtype
5718 | Class_Wide_Kind =>
5719 Build_Derived_Record_Type
5720 (N, Parent_Type, Derived_Type, Derive_Subps);
5721 return;
5723 when Enumeration_Kind =>
5724 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
5726 when Access_Kind =>
5727 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
5729 when Incomplete_Or_Private_Kind =>
5730 Build_Derived_Private_Type
5731 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
5733 -- For discriminated types, the derivation includes deriving
5734 -- primitive operations. For others it is done below.
5736 if Is_Tagged_Type (Parent_Type)
5737 or else Has_Discriminants (Parent_Type)
5738 or else (Present (Full_View (Parent_Type))
5739 and then Has_Discriminants (Full_View (Parent_Type)))
5740 then
5741 return;
5742 end if;
5744 when Concurrent_Kind =>
5745 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
5747 when others =>
5748 raise Program_Error;
5749 end case;
5751 if Etype (Derived_Type) = Any_Type then
5752 return;
5753 end if;
5755 -- Set delayed freeze and then derive subprograms, we need to do
5756 -- this in this order so that derived subprograms inherit the
5757 -- derived freeze if necessary.
5759 Set_Has_Delayed_Freeze (Derived_Type);
5760 if Derive_Subps then
5761 Derive_Subprograms (Parent_Type, Derived_Type);
5762 end if;
5764 Set_Has_Primitive_Operations
5765 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
5766 end Build_Derived_Type;
5768 -----------------------
5769 -- Build_Discriminal --
5770 -----------------------
5772 procedure Build_Discriminal (Discrim : Entity_Id) is
5773 D_Minal : Entity_Id;
5774 CR_Disc : Entity_Id;
5776 begin
5777 -- A discriminal has the same names as the discriminant.
5779 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
5781 Set_Ekind (D_Minal, E_In_Parameter);
5782 Set_Mechanism (D_Minal, Default_Mechanism);
5783 Set_Etype (D_Minal, Etype (Discrim));
5785 Set_Discriminal (Discrim, D_Minal);
5786 Set_Discriminal_Link (D_Minal, Discrim);
5788 -- For task types, build at once the discriminants of the corresponding
5789 -- record, which are needed if discriminants are used in entry defaults
5790 -- and in family bounds.
5792 if Is_Concurrent_Type (Current_Scope)
5793 or else Is_Limited_Type (Current_Scope)
5794 then
5795 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
5797 Set_Ekind (CR_Disc, E_In_Parameter);
5798 Set_Mechanism (CR_Disc, Default_Mechanism);
5799 Set_Etype (CR_Disc, Etype (Discrim));
5800 Set_CR_Discriminant (Discrim, CR_Disc);
5801 end if;
5802 end Build_Discriminal;
5804 ------------------------------------
5805 -- Build_Discriminant_Constraints --
5806 ------------------------------------
5808 function Build_Discriminant_Constraints
5809 (T : Entity_Id;
5810 Def : Node_Id;
5811 Derived_Def : Boolean := False) return Elist_Id
5813 C : constant Node_Id := Constraint (Def);
5814 Nb_Discr : constant Nat := Number_Discriminants (T);
5815 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
5816 -- Saves the expression corresponding to a given discriminant in T.
5818 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
5819 -- Return the Position number within array Discr_Expr of a discriminant
5820 -- D within the discriminant list of the discriminated type T.
5822 ------------------
5823 -- Pos_Of_Discr --
5824 ------------------
5826 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
5827 Disc : Entity_Id;
5829 begin
5830 Disc := First_Discriminant (T);
5831 for J in Discr_Expr'Range loop
5832 if Disc = D then
5833 return J;
5834 end if;
5836 Next_Discriminant (Disc);
5837 end loop;
5839 -- Note: Since this function is called on discriminants that are
5840 -- known to belong to the discriminated type, falling through the
5841 -- loop with no match signals an internal compiler error.
5843 raise Program_Error;
5844 end Pos_Of_Discr;
5846 -- Declarations local to Build_Discriminant_Constraints
5848 Discr : Entity_Id;
5849 E : Entity_Id;
5850 Elist : constant Elist_Id := New_Elmt_List;
5852 Constr : Node_Id;
5853 Expr : Node_Id;
5854 Id : Node_Id;
5855 Position : Nat;
5856 Found : Boolean;
5858 Discrim_Present : Boolean := False;
5860 -- Start of processing for Build_Discriminant_Constraints
5862 begin
5863 -- The following loop will process positional associations only.
5864 -- For a positional association, the (single) discriminant is
5865 -- implicitly specified by position, in textual order (RM 3.7.2).
5867 Discr := First_Discriminant (T);
5868 Constr := First (Constraints (C));
5870 for D in Discr_Expr'Range loop
5871 exit when Nkind (Constr) = N_Discriminant_Association;
5873 if No (Constr) then
5874 Error_Msg_N ("too few discriminants given in constraint", C);
5875 return New_Elmt_List;
5877 elsif Nkind (Constr) = N_Range
5878 or else (Nkind (Constr) = N_Attribute_Reference
5879 and then
5880 Attribute_Name (Constr) = Name_Range)
5881 then
5882 Error_Msg_N
5883 ("a range is not a valid discriminant constraint", Constr);
5884 Discr_Expr (D) := Error;
5886 else
5887 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
5888 Discr_Expr (D) := Constr;
5889 end if;
5891 Next_Discriminant (Discr);
5892 Next (Constr);
5893 end loop;
5895 if No (Discr) and then Present (Constr) then
5896 Error_Msg_N ("too many discriminants given in constraint", Constr);
5897 return New_Elmt_List;
5898 end if;
5900 -- Named associations can be given in any order, but if both positional
5901 -- and named associations are used in the same discriminant constraint,
5902 -- then positional associations must occur first, at their normal
5903 -- position. Hence once a named association is used, the rest of the
5904 -- discriminant constraint must use only named associations.
5906 while Present (Constr) loop
5908 -- Positional association forbidden after a named association.
5910 if Nkind (Constr) /= N_Discriminant_Association then
5911 Error_Msg_N ("positional association follows named one", Constr);
5912 return New_Elmt_List;
5914 -- Otherwise it is a named association
5916 else
5917 -- E records the type of the discriminants in the named
5918 -- association. All the discriminants specified in the same name
5919 -- association must have the same type.
5921 E := Empty;
5923 -- Search the list of discriminants in T to see if the simple name
5924 -- given in the constraint matches any of them.
5926 Id := First (Selector_Names (Constr));
5927 while Present (Id) loop
5928 Found := False;
5930 -- If Original_Discriminant is present, we are processing a
5931 -- generic instantiation and this is an instance node. We need
5932 -- to find the name of the corresponding discriminant in the
5933 -- actual record type T and not the name of the discriminant in
5934 -- the generic formal. Example:
5936 -- generic
5937 -- type G (D : int) is private;
5938 -- package P is
5939 -- subtype W is G (D => 1);
5940 -- end package;
5941 -- type Rec (X : int) is record ... end record;
5942 -- package Q is new P (G => Rec);
5944 -- At the point of the instantiation, formal type G is Rec
5945 -- and therefore when reanalyzing "subtype W is G (D => 1);"
5946 -- which really looks like "subtype W is Rec (D => 1);" at
5947 -- the point of instantiation, we want to find the discriminant
5948 -- that corresponds to D in Rec, ie X.
5950 if Present (Original_Discriminant (Id)) then
5951 Discr := Find_Corresponding_Discriminant (Id, T);
5952 Found := True;
5954 else
5955 Discr := First_Discriminant (T);
5956 while Present (Discr) loop
5957 if Chars (Discr) = Chars (Id) then
5958 Found := True;
5959 exit;
5960 end if;
5962 Next_Discriminant (Discr);
5963 end loop;
5965 if not Found then
5966 Error_Msg_N ("& does not match any discriminant", Id);
5967 return New_Elmt_List;
5969 -- The following is only useful for the benefit of generic
5970 -- instances but it does not interfere with other
5971 -- processing for the non-generic case so we do it in all
5972 -- cases (for generics this statement is executed when
5973 -- processing the generic definition, see comment at the
5974 -- beginning of this if statement).
5976 else
5977 Set_Original_Discriminant (Id, Discr);
5978 end if;
5979 end if;
5981 Position := Pos_Of_Discr (T, Discr);
5983 if Present (Discr_Expr (Position)) then
5984 Error_Msg_N ("duplicate constraint for discriminant&", Id);
5986 else
5987 -- Each discriminant specified in the same named association
5988 -- must be associated with a separate copy of the
5989 -- corresponding expression.
5991 if Present (Next (Id)) then
5992 Expr := New_Copy_Tree (Expression (Constr));
5993 Set_Parent (Expr, Parent (Expression (Constr)));
5994 else
5995 Expr := Expression (Constr);
5996 end if;
5998 Discr_Expr (Position) := Expr;
5999 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
6000 end if;
6002 -- A discriminant association with more than one discriminant
6003 -- name is only allowed if the named discriminants are all of
6004 -- the same type (RM 3.7.1(8)).
6006 if E = Empty then
6007 E := Base_Type (Etype (Discr));
6009 elsif Base_Type (Etype (Discr)) /= E then
6010 Error_Msg_N
6011 ("all discriminants in an association " &
6012 "must have the same type", Id);
6013 end if;
6015 Next (Id);
6016 end loop;
6017 end if;
6019 Next (Constr);
6020 end loop;
6022 -- A discriminant constraint must provide exactly one value for each
6023 -- discriminant of the type (RM 3.7.1(8)).
6025 for J in Discr_Expr'Range loop
6026 if No (Discr_Expr (J)) then
6027 Error_Msg_N ("too few discriminants given in constraint", C);
6028 return New_Elmt_List;
6029 end if;
6030 end loop;
6032 -- Determine if there are discriminant expressions in the constraint.
6034 for J in Discr_Expr'Range loop
6035 if Denotes_Discriminant (Discr_Expr (J), Check_Protected => True) then
6036 Discrim_Present := True;
6037 end if;
6038 end loop;
6040 -- Build an element list consisting of the expressions given in the
6041 -- discriminant constraint and apply the appropriate checks. The list
6042 -- is constructed after resolving any named discriminant associations
6043 -- and therefore the expressions appear in the textual order of the
6044 -- discriminants.
6046 Discr := First_Discriminant (T);
6047 for J in Discr_Expr'Range loop
6048 if Discr_Expr (J) /= Error then
6050 Append_Elmt (Discr_Expr (J), Elist);
6052 -- If any of the discriminant constraints is given by a
6053 -- discriminant and we are in a derived type declaration we
6054 -- have a discriminant renaming. Establish link between new
6055 -- and old discriminant.
6057 if Denotes_Discriminant (Discr_Expr (J)) then
6058 if Derived_Def then
6059 Set_Corresponding_Discriminant
6060 (Entity (Discr_Expr (J)), Discr);
6061 end if;
6063 -- Force the evaluation of non-discriminant expressions.
6064 -- If we have found a discriminant in the constraint 3.4(26)
6065 -- and 3.8(18) demand that no range checks are performed are
6066 -- after evaluation. If the constraint is for a component
6067 -- definition that has a per-object constraint, expressions are
6068 -- evaluated but not checked either. In all other cases perform
6069 -- a range check.
6071 else
6072 if Discrim_Present then
6073 null;
6075 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
6076 and then
6077 Has_Per_Object_Constraint
6078 (Defining_Identifier (Parent (Parent (Def))))
6079 then
6080 null;
6082 elsif Is_Access_Type (Etype (Discr)) then
6083 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
6085 else
6086 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
6087 end if;
6089 Force_Evaluation (Discr_Expr (J));
6090 end if;
6092 -- Check that the designated type of an access discriminant's
6093 -- expression is not a class-wide type unless the discriminant's
6094 -- designated type is also class-wide.
6096 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
6097 and then not Is_Class_Wide_Type
6098 (Designated_Type (Etype (Discr)))
6099 and then Etype (Discr_Expr (J)) /= Any_Type
6100 and then Is_Class_Wide_Type
6101 (Designated_Type (Etype (Discr_Expr (J))))
6102 then
6103 Wrong_Type (Discr_Expr (J), Etype (Discr));
6104 end if;
6105 end if;
6107 Next_Discriminant (Discr);
6108 end loop;
6110 return Elist;
6111 end Build_Discriminant_Constraints;
6113 ---------------------------------
6114 -- Build_Discriminated_Subtype --
6115 ---------------------------------
6117 procedure Build_Discriminated_Subtype
6118 (T : Entity_Id;
6119 Def_Id : Entity_Id;
6120 Elist : Elist_Id;
6121 Related_Nod : Node_Id;
6122 For_Access : Boolean := False)
6124 Has_Discrs : constant Boolean := Has_Discriminants (T);
6125 Constrained : constant Boolean
6126 := (Has_Discrs
6127 and then not Is_Empty_Elmt_List (Elist)
6128 and then not Is_Class_Wide_Type (T))
6129 or else Is_Constrained (T);
6131 begin
6132 if Ekind (T) = E_Record_Type then
6133 if For_Access then
6134 Set_Ekind (Def_Id, E_Private_Subtype);
6135 Set_Is_For_Access_Subtype (Def_Id, True);
6136 else
6137 Set_Ekind (Def_Id, E_Record_Subtype);
6138 end if;
6140 elsif Ekind (T) = E_Task_Type then
6141 Set_Ekind (Def_Id, E_Task_Subtype);
6143 elsif Ekind (T) = E_Protected_Type then
6144 Set_Ekind (Def_Id, E_Protected_Subtype);
6146 elsif Is_Private_Type (T) then
6147 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
6149 elsif Is_Class_Wide_Type (T) then
6150 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
6152 else
6153 -- Incomplete type. attach subtype to list of dependents, to be
6154 -- completed with full view of parent type, unless is it the
6155 -- designated subtype of a record component within an init_proc.
6156 -- This last case arises for a component of an access type whose
6157 -- designated type is incomplete (e.g. a Taft Amendment type).
6158 -- The designated subtype is within an inner scope, and needs no
6159 -- elaboration, because only the access type is needed in the
6160 -- initialization procedure.
6162 Set_Ekind (Def_Id, Ekind (T));
6164 if For_Access and then Within_Init_Proc then
6165 null;
6166 else
6167 Append_Elmt (Def_Id, Private_Dependents (T));
6168 end if;
6169 end if;
6171 Set_Etype (Def_Id, T);
6172 Init_Size_Align (Def_Id);
6173 Set_Has_Discriminants (Def_Id, Has_Discrs);
6174 Set_Is_Constrained (Def_Id, Constrained);
6176 Set_First_Entity (Def_Id, First_Entity (T));
6177 Set_Last_Entity (Def_Id, Last_Entity (T));
6178 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
6180 if Is_Tagged_Type (T) then
6181 Set_Is_Tagged_Type (Def_Id);
6182 Make_Class_Wide_Type (Def_Id);
6183 end if;
6185 Set_Stored_Constraint (Def_Id, No_Elist);
6187 if Has_Discrs then
6188 Set_Discriminant_Constraint (Def_Id, Elist);
6189 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
6190 end if;
6192 if Is_Tagged_Type (T) then
6193 Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
6194 Set_Is_Abstract (Def_Id, Is_Abstract (T));
6195 end if;
6197 -- Subtypes introduced by component declarations do not need to be
6198 -- marked as delayed, and do not get freeze nodes, because the semantics
6199 -- verifies that the parents of the subtypes are frozen before the
6200 -- enclosing record is frozen.
6202 if not Is_Type (Scope (Def_Id)) then
6203 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
6205 if Is_Private_Type (T)
6206 and then Present (Full_View (T))
6207 then
6208 Conditional_Delay (Def_Id, Full_View (T));
6209 else
6210 Conditional_Delay (Def_Id, T);
6211 end if;
6212 end if;
6214 if Is_Record_Type (T) then
6215 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
6217 if Has_Discrs
6218 and then not Is_Empty_Elmt_List (Elist)
6219 and then not For_Access
6220 then
6221 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
6222 elsif not For_Access then
6223 Set_Cloned_Subtype (Def_Id, T);
6224 end if;
6225 end if;
6227 end Build_Discriminated_Subtype;
6229 ------------------------
6230 -- Build_Scalar_Bound --
6231 ------------------------
6233 function Build_Scalar_Bound
6234 (Bound : Node_Id;
6235 Par_T : Entity_Id;
6236 Der_T : Entity_Id) return Node_Id
6238 New_Bound : Entity_Id;
6240 begin
6241 -- Note: not clear why this is needed, how can the original bound
6242 -- be unanalyzed at this point? and if it is, what business do we
6243 -- have messing around with it? and why is the base type of the
6244 -- parent type the right type for the resolution. It probably is
6245 -- not! It is OK for the new bound we are creating, but not for
6246 -- the old one??? Still if it never happens, no problem!
6248 Analyze_And_Resolve (Bound, Base_Type (Par_T));
6250 if Nkind (Bound) = N_Integer_Literal
6251 or else Nkind (Bound) = N_Real_Literal
6252 then
6253 New_Bound := New_Copy (Bound);
6254 Set_Etype (New_Bound, Der_T);
6255 Set_Analyzed (New_Bound);
6257 elsif Is_Entity_Name (Bound) then
6258 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
6260 -- The following is almost certainly wrong. What business do we have
6261 -- relocating a node (Bound) that is presumably still attached to
6262 -- the tree elsewhere???
6264 else
6265 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
6266 end if;
6268 Set_Etype (New_Bound, Der_T);
6269 return New_Bound;
6270 end Build_Scalar_Bound;
6272 --------------------------------
6273 -- Build_Underlying_Full_View --
6274 --------------------------------
6276 procedure Build_Underlying_Full_View
6277 (N : Node_Id;
6278 Typ : Entity_Id;
6279 Par : Entity_Id)
6281 Loc : constant Source_Ptr := Sloc (N);
6282 Subt : constant Entity_Id :=
6283 Make_Defining_Identifier
6284 (Loc, New_External_Name (Chars (Typ), 'S'));
6286 Constr : Node_Id;
6287 Indic : Node_Id;
6288 C : Node_Id;
6289 Id : Node_Id;
6291 begin
6292 if Nkind (N) = N_Full_Type_Declaration then
6293 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
6295 -- ??? ??? is this assert right, I assume so otherwise Constr
6296 -- would not be defined below (this used to be an elsif)
6298 else pragma Assert (Nkind (N) = N_Subtype_Declaration);
6299 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
6300 end if;
6302 -- If the constraint has discriminant associations, the discriminant
6303 -- entity is already set, but it denotes a discriminant of the new
6304 -- type, not the original parent, so it must be found anew.
6306 C := First (Constraints (Constr));
6308 while Present (C) loop
6310 if Nkind (C) = N_Discriminant_Association then
6311 Id := First (Selector_Names (C));
6313 while Present (Id) loop
6314 Set_Original_Discriminant (Id, Empty);
6315 Next (Id);
6316 end loop;
6317 end if;
6319 Next (C);
6320 end loop;
6322 Indic := Make_Subtype_Declaration (Loc,
6323 Defining_Identifier => Subt,
6324 Subtype_Indication =>
6325 Make_Subtype_Indication (Loc,
6326 Subtype_Mark => New_Reference_To (Par, Loc),
6327 Constraint => New_Copy_Tree (Constr)));
6329 -- If this is a component subtype for an outer itype, it is not
6330 -- a list member, so simply set the parent link for analysis: if
6331 -- the enclosing type does not need to be in a declarative list,
6332 -- neither do the components.
6334 if Is_List_Member (N) then
6335 Insert_Before (N, Indic);
6336 else
6337 Set_Parent (Indic, Parent (N));
6338 end if;
6340 Analyze (Indic);
6341 Set_Underlying_Full_View (Typ, Full_View (Subt));
6342 end Build_Underlying_Full_View;
6344 -------------------------------
6345 -- Check_Abstract_Overriding --
6346 -------------------------------
6348 procedure Check_Abstract_Overriding (T : Entity_Id) is
6349 Op_List : Elist_Id;
6350 Elmt : Elmt_Id;
6351 Subp : Entity_Id;
6352 Type_Def : Node_Id;
6354 begin
6355 Op_List := Primitive_Operations (T);
6357 -- Loop to check primitive operations
6359 Elmt := First_Elmt (Op_List);
6360 while Present (Elmt) loop
6361 Subp := Node (Elmt);
6363 -- Special exception, do not complain about failure to
6364 -- override _Input and _Output, since we always provide
6365 -- automatic overridings for these subprograms.
6367 if Is_Abstract (Subp)
6368 and then not Is_TSS (Subp, TSS_Stream_Input)
6369 and then not Is_TSS (Subp, TSS_Stream_Output)
6370 and then not Is_Abstract (T)
6371 then
6372 if Present (Alias (Subp)) then
6373 -- Only perform the check for a derived subprogram when
6374 -- the type has an explicit record extension. This avoids
6375 -- incorrectly flagging abstract subprograms for the case
6376 -- of a type without an extension derived from a formal type
6377 -- with a tagged actual (can occur within a private part).
6379 Type_Def := Type_Definition (Parent (T));
6380 if Nkind (Type_Def) = N_Derived_Type_Definition
6381 and then Present (Record_Extension_Part (Type_Def))
6382 then
6383 Error_Msg_NE
6384 ("type must be declared abstract or & overridden",
6385 T, Subp);
6386 end if;
6387 else
6388 Error_Msg_NE
6389 ("abstract subprogram not allowed for type&",
6390 Subp, T);
6391 Error_Msg_NE
6392 ("nonabstract type has abstract subprogram&",
6393 T, Subp);
6394 end if;
6395 end if;
6397 Next_Elmt (Elmt);
6398 end loop;
6399 end Check_Abstract_Overriding;
6401 ------------------------------------------------
6402 -- Check_Access_Discriminant_Requires_Limited --
6403 ------------------------------------------------
6405 procedure Check_Access_Discriminant_Requires_Limited
6406 (D : Node_Id;
6407 Loc : Node_Id)
6409 begin
6410 -- A discriminant_specification for an access discriminant
6411 -- shall appear only in the declaration for a task or protected
6412 -- type, or for a type with the reserved word 'limited' in
6413 -- its definition or in one of its ancestors. (RM 3.7(10))
6415 if Nkind (Discriminant_Type (D)) = N_Access_Definition
6416 and then not Is_Concurrent_Type (Current_Scope)
6417 and then not Is_Concurrent_Record_Type (Current_Scope)
6418 and then not Is_Limited_Record (Current_Scope)
6419 and then Ekind (Current_Scope) /= E_Limited_Private_Type
6420 then
6421 Error_Msg_N
6422 ("access discriminants allowed only for limited types", Loc);
6423 end if;
6424 end Check_Access_Discriminant_Requires_Limited;
6426 -----------------------------------
6427 -- Check_Aliased_Component_Types --
6428 -----------------------------------
6430 procedure Check_Aliased_Component_Types (T : Entity_Id) is
6431 C : Entity_Id;
6433 begin
6434 -- ??? Also need to check components of record extensions,
6435 -- but not components of protected types (which are always
6436 -- limited).
6438 if not Is_Limited_Type (T) then
6439 if Ekind (T) = E_Record_Type then
6440 C := First_Component (T);
6441 while Present (C) loop
6442 if Is_Aliased (C)
6443 and then Has_Discriminants (Etype (C))
6444 and then not Is_Constrained (Etype (C))
6445 and then not In_Instance
6446 then
6447 Error_Msg_N
6448 ("aliased component must be constrained ('R'M 3.6(11))",
6450 end if;
6452 Next_Component (C);
6453 end loop;
6455 elsif Ekind (T) = E_Array_Type then
6456 if Has_Aliased_Components (T)
6457 and then Has_Discriminants (Component_Type (T))
6458 and then not Is_Constrained (Component_Type (T))
6459 and then not In_Instance
6460 then
6461 Error_Msg_N
6462 ("aliased component type must be constrained ('R'M 3.6(11))",
6464 end if;
6465 end if;
6466 end if;
6467 end Check_Aliased_Component_Types;
6469 ----------------------
6470 -- Check_Completion --
6471 ----------------------
6473 procedure Check_Completion (Body_Id : Node_Id := Empty) is
6474 E : Entity_Id;
6476 procedure Post_Error;
6477 -- Post error message for lack of completion for entity E
6479 ----------------
6480 -- Post_Error --
6481 ----------------
6483 procedure Post_Error is
6484 begin
6485 if not Comes_From_Source (E) then
6487 if Ekind (E) = E_Task_Type
6488 or else Ekind (E) = E_Protected_Type
6489 then
6490 -- It may be an anonymous protected type created for a
6491 -- single variable. Post error on variable, if present.
6493 declare
6494 Var : Entity_Id;
6496 begin
6497 Var := First_Entity (Current_Scope);
6499 while Present (Var) loop
6500 exit when Etype (Var) = E
6501 and then Comes_From_Source (Var);
6503 Next_Entity (Var);
6504 end loop;
6506 if Present (Var) then
6507 E := Var;
6508 end if;
6509 end;
6510 end if;
6511 end if;
6513 -- If a generated entity has no completion, then either previous
6514 -- semantic errors have disabled the expansion phase, or else
6515 -- we had missing subunits, or else we are compiling without expan-
6516 -- sion, or else something is very wrong.
6518 if not Comes_From_Source (E) then
6519 pragma Assert
6520 (Serious_Errors_Detected > 0
6521 or else Configurable_Run_Time_Violations > 0
6522 or else Subunits_Missing
6523 or else not Expander_Active);
6524 return;
6526 -- Here for source entity
6528 else
6529 -- Here if no body to post the error message, so we post the error
6530 -- on the declaration that has no completion. This is not really
6531 -- the right place to post it, think about this later ???
6533 if No (Body_Id) then
6534 if Is_Type (E) then
6535 Error_Msg_NE
6536 ("missing full declaration for }", Parent (E), E);
6537 else
6538 Error_Msg_NE
6539 ("missing body for &", Parent (E), E);
6540 end if;
6542 -- Package body has no completion for a declaration that appears
6543 -- in the corresponding spec. Post error on the body, with a
6544 -- reference to the non-completed declaration.
6546 else
6547 Error_Msg_Sloc := Sloc (E);
6549 if Is_Type (E) then
6550 Error_Msg_NE
6551 ("missing full declaration for }!", Body_Id, E);
6553 elsif Is_Overloadable (E)
6554 and then Current_Entity_In_Scope (E) /= E
6555 then
6556 -- It may be that the completion is mistyped and appears
6557 -- as a distinct overloading of the entity.
6559 declare
6560 Candidate : constant Entity_Id :=
6561 Current_Entity_In_Scope (E);
6562 Decl : constant Node_Id :=
6563 Unit_Declaration_Node (Candidate);
6565 begin
6566 if Is_Overloadable (Candidate)
6567 and then Ekind (Candidate) = Ekind (E)
6568 and then Nkind (Decl) = N_Subprogram_Body
6569 and then Acts_As_Spec (Decl)
6570 then
6571 Check_Type_Conformant (Candidate, E);
6573 else
6574 Error_Msg_NE ("missing body for & declared#!",
6575 Body_Id, E);
6576 end if;
6577 end;
6578 else
6579 Error_Msg_NE ("missing body for & declared#!",
6580 Body_Id, E);
6581 end if;
6582 end if;
6583 end if;
6584 end Post_Error;
6586 -- Start processing for Check_Completion
6588 begin
6589 E := First_Entity (Current_Scope);
6590 while Present (E) loop
6591 if Is_Intrinsic_Subprogram (E) then
6592 null;
6594 -- The following situation requires special handling: a child
6595 -- unit that appears in the context clause of the body of its
6596 -- parent:
6598 -- procedure Parent.Child (...);
6600 -- with Parent.Child;
6601 -- package body Parent is
6603 -- Here Parent.Child appears as a local entity, but should not
6604 -- be flagged as requiring completion, because it is a
6605 -- compilation unit.
6607 elsif Ekind (E) = E_Function
6608 or else Ekind (E) = E_Procedure
6609 or else Ekind (E) = E_Generic_Function
6610 or else Ekind (E) = E_Generic_Procedure
6611 then
6612 if not Has_Completion (E)
6613 and then not Is_Abstract (E)
6614 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
6615 N_Compilation_Unit
6616 and then Chars (E) /= Name_uSize
6617 then
6618 Post_Error;
6619 end if;
6621 elsif Is_Entry (E) then
6622 if not Has_Completion (E) and then
6623 (Ekind (Scope (E)) = E_Protected_Object
6624 or else Ekind (Scope (E)) = E_Protected_Type)
6625 then
6626 Post_Error;
6627 end if;
6629 elsif Is_Package (E) then
6630 if Unit_Requires_Body (E) then
6631 if not Has_Completion (E)
6632 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
6633 N_Compilation_Unit
6634 then
6635 Post_Error;
6636 end if;
6638 elsif not Is_Child_Unit (E) then
6639 May_Need_Implicit_Body (E);
6640 end if;
6642 elsif Ekind (E) = E_Incomplete_Type
6643 and then No (Underlying_Type (E))
6644 then
6645 Post_Error;
6647 elsif (Ekind (E) = E_Task_Type or else
6648 Ekind (E) = E_Protected_Type)
6649 and then not Has_Completion (E)
6650 then
6651 Post_Error;
6653 -- A single task declared in the current scope is
6654 -- a constant, verify that the body of its anonymous
6655 -- type is in the same scope. If the task is defined
6656 -- elsewhere, this may be a renaming declaration for
6657 -- which no completion is needed.
6659 elsif Ekind (E) = E_Constant
6660 and then Ekind (Etype (E)) = E_Task_Type
6661 and then not Has_Completion (Etype (E))
6662 and then Scope (Etype (E)) = Current_Scope
6663 then
6664 Post_Error;
6666 elsif Ekind (E) = E_Protected_Object
6667 and then not Has_Completion (Etype (E))
6668 then
6669 Post_Error;
6671 elsif Ekind (E) = E_Record_Type then
6672 if Is_Tagged_Type (E) then
6673 Check_Abstract_Overriding (E);
6674 end if;
6676 Check_Aliased_Component_Types (E);
6678 elsif Ekind (E) = E_Array_Type then
6679 Check_Aliased_Component_Types (E);
6681 end if;
6683 Next_Entity (E);
6684 end loop;
6685 end Check_Completion;
6687 ----------------------------
6688 -- Check_Delta_Expression --
6689 ----------------------------
6691 procedure Check_Delta_Expression (E : Node_Id) is
6692 begin
6693 if not (Is_Real_Type (Etype (E))) then
6694 Wrong_Type (E, Any_Real);
6696 elsif not Is_OK_Static_Expression (E) then
6697 Flag_Non_Static_Expr
6698 ("non-static expression used for delta value!", E);
6700 elsif not UR_Is_Positive (Expr_Value_R (E)) then
6701 Error_Msg_N ("delta expression must be positive", E);
6703 else
6704 return;
6705 end if;
6707 -- If any of above errors occurred, then replace the incorrect
6708 -- expression by the real 0.1, which should prevent further errors.
6710 Rewrite (E,
6711 Make_Real_Literal (Sloc (E), Ureal_Tenth));
6712 Analyze_And_Resolve (E, Standard_Float);
6714 end Check_Delta_Expression;
6716 -----------------------------
6717 -- Check_Digits_Expression --
6718 -----------------------------
6720 procedure Check_Digits_Expression (E : Node_Id) is
6721 begin
6722 if not (Is_Integer_Type (Etype (E))) then
6723 Wrong_Type (E, Any_Integer);
6725 elsif not Is_OK_Static_Expression (E) then
6726 Flag_Non_Static_Expr
6727 ("non-static expression used for digits value!", E);
6729 elsif Expr_Value (E) <= 0 then
6730 Error_Msg_N ("digits value must be greater than zero", E);
6732 else
6733 return;
6734 end if;
6736 -- If any of above errors occurred, then replace the incorrect
6737 -- expression by the integer 1, which should prevent further errors.
6739 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
6740 Analyze_And_Resolve (E, Standard_Integer);
6742 end Check_Digits_Expression;
6744 --------------------------
6745 -- Check_Initialization --
6746 --------------------------
6748 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
6749 begin
6750 if (Is_Limited_Type (T)
6751 or else Is_Limited_Composite (T))
6752 and then not In_Instance
6753 and then not In_Inlined_Body
6754 then
6755 -- Ada 2005 (AI-287): Relax the strictness of the front-end in
6756 -- case of limited aggregates and extension aggregates.
6758 if Ada_Version >= Ada_05
6759 and then (Nkind (Exp) = N_Aggregate
6760 or else Nkind (Exp) = N_Extension_Aggregate)
6761 then
6762 null;
6763 else
6764 Error_Msg_N
6765 ("cannot initialize entities of limited type", Exp);
6766 Explain_Limited_Type (T, Exp);
6767 end if;
6768 end if;
6769 end Check_Initialization;
6771 ------------------------------------
6772 -- Check_Or_Process_Discriminants --
6773 ------------------------------------
6775 -- If an incomplete or private type declaration was already given for
6776 -- the type, the discriminants may have already been processed if they
6777 -- were present on the incomplete declaration. In this case a full
6778 -- conformance check is performed otherwise just process them.
6780 procedure Check_Or_Process_Discriminants
6781 (N : Node_Id;
6782 T : Entity_Id;
6783 Prev : Entity_Id := Empty)
6785 begin
6786 if Has_Discriminants (T) then
6788 -- Make the discriminants visible to component declarations.
6790 declare
6791 D : Entity_Id := First_Discriminant (T);
6792 Prev : Entity_Id;
6794 begin
6795 while Present (D) loop
6796 Prev := Current_Entity (D);
6797 Set_Current_Entity (D);
6798 Set_Is_Immediately_Visible (D);
6799 Set_Homonym (D, Prev);
6801 -- Ada 2005 (AI-230): Access discriminant allowed in
6802 -- non-limited record types.
6804 if Ada_Version < Ada_05 then
6806 -- This restriction gets applied to the full type here; it
6807 -- has already been applied earlier to the partial view
6809 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
6810 end if;
6812 Next_Discriminant (D);
6813 end loop;
6814 end;
6816 elsif Present (Discriminant_Specifications (N)) then
6817 Process_Discriminants (N, Prev);
6818 end if;
6819 end Check_Or_Process_Discriminants;
6821 ----------------------
6822 -- Check_Real_Bound --
6823 ----------------------
6825 procedure Check_Real_Bound (Bound : Node_Id) is
6826 begin
6827 if not Is_Real_Type (Etype (Bound)) then
6828 Error_Msg_N
6829 ("bound in real type definition must be of real type", Bound);
6831 elsif not Is_OK_Static_Expression (Bound) then
6832 Flag_Non_Static_Expr
6833 ("non-static expression used for real type bound!", Bound);
6835 else
6836 return;
6837 end if;
6839 Rewrite
6840 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
6841 Analyze (Bound);
6842 Resolve (Bound, Standard_Float);
6843 end Check_Real_Bound;
6845 ------------------------------
6846 -- Complete_Private_Subtype --
6847 ------------------------------
6849 procedure Complete_Private_Subtype
6850 (Priv : Entity_Id;
6851 Full : Entity_Id;
6852 Full_Base : Entity_Id;
6853 Related_Nod : Node_Id)
6855 Save_Next_Entity : Entity_Id;
6856 Save_Homonym : Entity_Id;
6858 begin
6859 -- Set semantic attributes for (implicit) private subtype completion.
6860 -- If the full type has no discriminants, then it is a copy of the full
6861 -- view of the base. Otherwise, it is a subtype of the base with a
6862 -- possible discriminant constraint. Save and restore the original
6863 -- Next_Entity field of full to ensure that the calls to Copy_Node
6864 -- do not corrupt the entity chain.
6866 -- Note that the type of the full view is the same entity as the
6867 -- type of the partial view. In this fashion, the subtype has
6868 -- access to the correct view of the parent.
6870 Save_Next_Entity := Next_Entity (Full);
6871 Save_Homonym := Homonym (Priv);
6873 case Ekind (Full_Base) is
6875 when E_Record_Type |
6876 E_Record_Subtype |
6877 Class_Wide_Kind |
6878 Private_Kind |
6879 Task_Kind |
6880 Protected_Kind =>
6881 Copy_Node (Priv, Full);
6883 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
6884 Set_First_Entity (Full, First_Entity (Full_Base));
6885 Set_Last_Entity (Full, Last_Entity (Full_Base));
6887 when others =>
6888 Copy_Node (Full_Base, Full);
6889 Set_Chars (Full, Chars (Priv));
6890 Conditional_Delay (Full, Priv);
6891 Set_Sloc (Full, Sloc (Priv));
6893 end case;
6895 Set_Next_Entity (Full, Save_Next_Entity);
6896 Set_Homonym (Full, Save_Homonym);
6897 Set_Associated_Node_For_Itype (Full, Related_Nod);
6899 -- Set common attributes for all subtypes.
6901 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
6903 -- The Etype of the full view is inconsistent. Gigi needs to see the
6904 -- structural full view, which is what the current scheme gives:
6905 -- the Etype of the full view is the etype of the full base. However,
6906 -- if the full base is a derived type, the full view then looks like
6907 -- a subtype of the parent, not a subtype of the full base. If instead
6908 -- we write:
6910 -- Set_Etype (Full, Full_Base);
6912 -- then we get inconsistencies in the front-end (confusion between
6913 -- views). Several outstanding bugs are related to this.
6915 Set_Is_First_Subtype (Full, False);
6916 Set_Scope (Full, Scope (Priv));
6917 Set_Size_Info (Full, Full_Base);
6918 Set_RM_Size (Full, RM_Size (Full_Base));
6919 Set_Is_Itype (Full);
6921 -- A subtype of a private-type-without-discriminants, whose full-view
6922 -- has discriminants with default expressions, is not constrained!
6924 if not Has_Discriminants (Priv) then
6925 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
6927 if Has_Discriminants (Full_Base) then
6928 Set_Discriminant_Constraint
6929 (Full, Discriminant_Constraint (Full_Base));
6931 -- The partial view may have been indefinite, the full view
6932 -- might not be.
6934 Set_Has_Unknown_Discriminants
6935 (Full, Has_Unknown_Discriminants (Full_Base));
6936 end if;
6937 end if;
6939 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
6940 Set_Depends_On_Private (Full, Has_Private_Component (Full));
6942 -- Freeze the private subtype entity if its parent is delayed,
6943 -- and not already frozen. We skip this processing if the type
6944 -- is an anonymous subtype of a record component, or is the
6945 -- corresponding record of a protected type, since ???
6947 if not Is_Type (Scope (Full)) then
6948 Set_Has_Delayed_Freeze (Full,
6949 Has_Delayed_Freeze (Full_Base)
6950 and then (not Is_Frozen (Full_Base)));
6951 end if;
6953 Set_Freeze_Node (Full, Empty);
6954 Set_Is_Frozen (Full, False);
6955 Set_Full_View (Priv, Full);
6957 if Has_Discriminants (Full) then
6958 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
6959 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
6960 if Has_Unknown_Discriminants (Full) then
6961 Set_Discriminant_Constraint (Full, No_Elist);
6962 end if;
6963 end if;
6965 if Ekind (Full_Base) = E_Record_Type
6966 and then Has_Discriminants (Full_Base)
6967 and then Has_Discriminants (Priv) -- might not, if errors
6968 and then not Has_Unknown_Discriminants (Priv)
6969 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
6970 then
6971 Create_Constrained_Components
6972 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
6974 -- If the full base is itself derived from private, build a congruent
6975 -- subtype of its underlying type, for use by the back end. Do not
6976 -- do this for a constrained record component, where the back-end has
6977 -- the proper information and there is no place for the declaration.
6979 elsif Ekind (Full_Base) in Private_Kind
6980 and then Is_Derived_Type (Full_Base)
6981 and then Has_Discriminants (Full_Base)
6982 and then Nkind (Related_Nod) /= N_Component_Declaration
6983 and then (Ekind (Current_Scope) /= E_Record_Subtype)
6984 and then
6985 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
6986 then
6987 Build_Underlying_Full_View (Parent (Priv), Full, Etype (Full_Base));
6989 elsif Is_Record_Type (Full_Base) then
6991 -- Show Full is simply a renaming of Full_Base.
6993 Set_Cloned_Subtype (Full, Full_Base);
6994 end if;
6996 -- It is unsafe to share to bounds of a scalar type, because the
6997 -- Itype is elaborated on demand, and if a bound is non-static
6998 -- then different orders of elaboration in different units will
6999 -- lead to different external symbols.
7001 if Is_Scalar_Type (Full_Base) then
7002 Set_Scalar_Range (Full,
7003 Make_Range (Sloc (Related_Nod),
7004 Low_Bound =>
7005 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
7006 High_Bound =>
7007 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
7009 -- This completion inherits the bounds of the full parent, but if
7010 -- the parent is an unconstrained floating point type, so is the
7011 -- completion.
7013 if Is_Floating_Point_Type (Full_Base) then
7014 Set_Includes_Infinities
7015 (Scalar_Range (Full), Has_Infinities (Full_Base));
7016 end if;
7017 end if;
7019 -- ??? It seems that a lot of fields are missing that should be
7020 -- copied from Full_Base to Full. Here are some that are introduced
7021 -- in a non-disruptive way but a cleanup is necessary.
7023 if Is_Tagged_Type (Full_Base) then
7024 Set_Is_Tagged_Type (Full);
7025 Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
7026 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
7028 elsif Is_Concurrent_Type (Full_Base) then
7029 if Has_Discriminants (Full)
7030 and then Present (Corresponding_Record_Type (Full_Base))
7031 then
7032 Set_Corresponding_Record_Type (Full,
7033 Constrain_Corresponding_Record
7034 (Full, Corresponding_Record_Type (Full_Base),
7035 Related_Nod, Full_Base));
7037 else
7038 Set_Corresponding_Record_Type (Full,
7039 Corresponding_Record_Type (Full_Base));
7040 end if;
7041 end if;
7043 end Complete_Private_Subtype;
7045 ----------------------------
7046 -- Constant_Redeclaration --
7047 ----------------------------
7049 procedure Constant_Redeclaration
7050 (Id : Entity_Id;
7051 N : Node_Id;
7052 T : out Entity_Id)
7054 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
7055 Obj_Def : constant Node_Id := Object_Definition (N);
7056 New_T : Entity_Id;
7058 procedure Check_Recursive_Declaration (Typ : Entity_Id);
7059 -- If deferred constant is an access type initialized with an
7060 -- allocator, check whether there is an illegal recursion in the
7061 -- definition, through a default value of some record subcomponent.
7062 -- This is normally detected when generating init procs, but requires
7063 -- this additional mechanism when expansion is disabled.
7065 ---------------------------------
7066 -- Check_Recursive_Declaration --
7067 ---------------------------------
7069 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
7070 Comp : Entity_Id;
7072 begin
7073 if Is_Record_Type (Typ) then
7074 Comp := First_Component (Typ);
7076 while Present (Comp) loop
7077 if Comes_From_Source (Comp) then
7078 if Present (Expression (Parent (Comp)))
7079 and then Is_Entity_Name (Expression (Parent (Comp)))
7080 and then Entity (Expression (Parent (Comp))) = Prev
7081 then
7082 Error_Msg_Sloc := Sloc (Parent (Comp));
7083 Error_Msg_NE
7084 ("illegal circularity with declaration for&#",
7085 N, Comp);
7086 return;
7088 elsif Is_Record_Type (Etype (Comp)) then
7089 Check_Recursive_Declaration (Etype (Comp));
7090 end if;
7091 end if;
7093 Next_Component (Comp);
7094 end loop;
7095 end if;
7096 end Check_Recursive_Declaration;
7098 -- Start of processing for Constant_Redeclaration
7100 begin
7101 if Nkind (Parent (Prev)) = N_Object_Declaration then
7102 if Nkind (Object_Definition
7103 (Parent (Prev))) = N_Subtype_Indication
7104 then
7105 -- Find type of new declaration. The constraints of the two
7106 -- views must match statically, but there is no point in
7107 -- creating an itype for the full view.
7109 if Nkind (Obj_Def) = N_Subtype_Indication then
7110 Find_Type (Subtype_Mark (Obj_Def));
7111 New_T := Entity (Subtype_Mark (Obj_Def));
7113 else
7114 Find_Type (Obj_Def);
7115 New_T := Entity (Obj_Def);
7116 end if;
7118 T := Etype (Prev);
7120 else
7121 -- The full view may impose a constraint, even if the partial
7122 -- view does not, so construct the subtype.
7124 New_T := Find_Type_Of_Object (Obj_Def, N);
7125 T := New_T;
7126 end if;
7128 else
7129 -- Current declaration is illegal, diagnosed below in Enter_Name.
7131 T := Empty;
7132 New_T := Any_Type;
7133 end if;
7135 -- If previous full declaration exists, or if a homograph is present,
7136 -- let Enter_Name handle it, either with an error, or with the removal
7137 -- of an overridden implicit subprogram.
7139 if Ekind (Prev) /= E_Constant
7140 or else Present (Expression (Parent (Prev)))
7141 or else Present (Full_View (Prev))
7142 then
7143 Enter_Name (Id);
7145 -- Verify that types of both declarations match.
7147 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T) then
7148 Error_Msg_Sloc := Sloc (Prev);
7149 Error_Msg_N ("type does not match declaration#", N);
7150 Set_Full_View (Prev, Id);
7151 Set_Etype (Id, Any_Type);
7153 -- If so, process the full constant declaration
7155 else
7156 Set_Full_View (Prev, Id);
7157 Set_Is_Public (Id, Is_Public (Prev));
7158 Set_Is_Internal (Id);
7159 Append_Entity (Id, Current_Scope);
7161 -- Check ALIASED present if present before (RM 7.4(7))
7163 if Is_Aliased (Prev)
7164 and then not Aliased_Present (N)
7165 then
7166 Error_Msg_Sloc := Sloc (Prev);
7167 Error_Msg_N ("ALIASED required (see declaration#)", N);
7168 end if;
7170 -- Check that placement is in private part and that the incomplete
7171 -- declaration appeared in the visible part.
7173 if Ekind (Current_Scope) = E_Package
7174 and then not In_Private_Part (Current_Scope)
7175 then
7176 Error_Msg_Sloc := Sloc (Prev);
7177 Error_Msg_N ("full constant for declaration#"
7178 & " must be in private part", N);
7180 elsif Ekind (Current_Scope) = E_Package
7181 and then List_Containing (Parent (Prev))
7182 /= Visible_Declarations
7183 (Specification (Unit_Declaration_Node (Current_Scope)))
7184 then
7185 Error_Msg_N
7186 ("deferred constant must be declared in visible part",
7187 Parent (Prev));
7188 end if;
7190 if Is_Access_Type (T)
7191 and then Nkind (Expression (N)) = N_Allocator
7192 then
7193 Check_Recursive_Declaration (Designated_Type (T));
7194 end if;
7195 end if;
7196 end Constant_Redeclaration;
7198 ----------------------
7199 -- Constrain_Access --
7200 ----------------------
7202 procedure Constrain_Access
7203 (Def_Id : in out Entity_Id;
7204 S : Node_Id;
7205 Related_Nod : Node_Id)
7207 T : constant Entity_Id := Entity (Subtype_Mark (S));
7208 Desig_Type : constant Entity_Id := Designated_Type (T);
7209 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
7210 Constraint_OK : Boolean := True;
7212 begin
7213 if Is_Array_Type (Desig_Type) then
7214 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
7216 elsif (Is_Record_Type (Desig_Type)
7217 or else Is_Incomplete_Or_Private_Type (Desig_Type))
7218 and then not Is_Constrained (Desig_Type)
7219 then
7220 -- ??? The following code is a temporary kludge to ignore
7221 -- discriminant constraint on access type if
7222 -- it is constraining the current record. Avoid creating the
7223 -- implicit subtype of the record we are currently compiling
7224 -- since right now, we cannot handle these.
7225 -- For now, just return the access type itself.
7227 if Desig_Type = Current_Scope
7228 and then No (Def_Id)
7229 then
7230 Set_Ekind (Desig_Subtype, E_Record_Subtype);
7231 Def_Id := Entity (Subtype_Mark (S));
7233 -- This call added to ensure that the constraint is
7234 -- analyzed (needed for a B test). Note that we
7235 -- still return early from this procedure to avoid
7236 -- recursive processing. ???
7238 Constrain_Discriminated_Type
7239 (Desig_Subtype, S, Related_Nod, For_Access => True);
7241 return;
7242 end if;
7244 if Ekind (T) = E_General_Access_Type
7245 and then Has_Private_Declaration (Desig_Type)
7246 and then In_Open_Scopes (Scope (Desig_Type))
7247 then
7248 -- Enforce rule that the constraint is illegal if there is
7249 -- an unconstrained view of the designated type. This means
7250 -- that the partial view (either a private type declaration or
7251 -- a derivation from a private type) has no discriminants.
7252 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
7253 -- by ACATS B371001).
7255 declare
7256 Pack : constant Node_Id :=
7257 Unit_Declaration_Node (Scope (Desig_Type));
7258 Decls : List_Id;
7259 Decl : Node_Id;
7261 begin
7262 if Nkind (Pack) = N_Package_Declaration then
7263 Decls := Visible_Declarations (Specification (Pack));
7264 Decl := First (Decls);
7266 while Present (Decl) loop
7267 if (Nkind (Decl) = N_Private_Type_Declaration
7268 and then
7269 Chars (Defining_Identifier (Decl)) =
7270 Chars (Desig_Type))
7272 or else
7273 (Nkind (Decl) = N_Full_Type_Declaration
7274 and then
7275 Chars (Defining_Identifier (Decl)) =
7276 Chars (Desig_Type)
7277 and then Is_Derived_Type (Desig_Type)
7278 and then
7279 Has_Private_Declaration (Etype (Desig_Type)))
7280 then
7281 if No (Discriminant_Specifications (Decl)) then
7282 Error_Msg_N
7283 ("cannot constrain general access type " &
7284 "if designated type has unconstrained view", S);
7285 end if;
7287 exit;
7288 end if;
7290 Next (Decl);
7291 end loop;
7292 end if;
7293 end;
7294 end if;
7296 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
7297 For_Access => True);
7299 elsif (Is_Task_Type (Desig_Type)
7300 or else Is_Protected_Type (Desig_Type))
7301 and then not Is_Constrained (Desig_Type)
7302 then
7303 Constrain_Concurrent
7304 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
7306 else
7307 Error_Msg_N ("invalid constraint on access type", S);
7308 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
7309 Constraint_OK := False;
7310 end if;
7312 if No (Def_Id) then
7313 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
7314 else
7315 Set_Ekind (Def_Id, E_Access_Subtype);
7316 end if;
7318 if Constraint_OK then
7319 Set_Etype (Def_Id, Base_Type (T));
7321 if Is_Private_Type (Desig_Type) then
7322 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
7323 end if;
7324 else
7325 Set_Etype (Def_Id, Any_Type);
7326 end if;
7328 Set_Size_Info (Def_Id, T);
7329 Set_Is_Constrained (Def_Id, Constraint_OK);
7330 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
7331 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
7332 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
7334 -- Itypes created for constrained record components do not receive
7335 -- a freeze node, they are elaborated when first seen.
7337 if not Is_Record_Type (Current_Scope) then
7338 Conditional_Delay (Def_Id, T);
7339 end if;
7340 end Constrain_Access;
7342 ---------------------
7343 -- Constrain_Array --
7344 ---------------------
7346 procedure Constrain_Array
7347 (Def_Id : in out Entity_Id;
7348 SI : Node_Id;
7349 Related_Nod : Node_Id;
7350 Related_Id : Entity_Id;
7351 Suffix : Character)
7353 C : constant Node_Id := Constraint (SI);
7354 Number_Of_Constraints : Nat := 0;
7355 Index : Node_Id;
7356 S, T : Entity_Id;
7357 Constraint_OK : Boolean := True;
7359 begin
7360 T := Entity (Subtype_Mark (SI));
7362 if Ekind (T) in Access_Kind then
7363 T := Designated_Type (T);
7364 end if;
7366 -- If an index constraint follows a subtype mark in a subtype indication
7367 -- then the type or subtype denoted by the subtype mark must not already
7368 -- impose an index constraint. The subtype mark must denote either an
7369 -- unconstrained array type or an access type whose designated type
7370 -- is such an array type... (RM 3.6.1)
7372 if Is_Constrained (T) then
7373 Error_Msg_N
7374 ("array type is already constrained", Subtype_Mark (SI));
7375 Constraint_OK := False;
7377 else
7378 S := First (Constraints (C));
7380 while Present (S) loop
7381 Number_Of_Constraints := Number_Of_Constraints + 1;
7382 Next (S);
7383 end loop;
7385 -- In either case, the index constraint must provide a discrete
7386 -- range for each index of the array type and the type of each
7387 -- discrete range must be the same as that of the corresponding
7388 -- index. (RM 3.6.1)
7390 if Number_Of_Constraints /= Number_Dimensions (T) then
7391 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
7392 Constraint_OK := False;
7394 else
7395 S := First (Constraints (C));
7396 Index := First_Index (T);
7397 Analyze (Index);
7399 -- Apply constraints to each index type
7401 for J in 1 .. Number_Of_Constraints loop
7402 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
7403 Next (Index);
7404 Next (S);
7405 end loop;
7407 end if;
7408 end if;
7410 if No (Def_Id) then
7411 Def_Id :=
7412 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
7413 Set_Parent (Def_Id, Related_Nod);
7415 else
7416 Set_Ekind (Def_Id, E_Array_Subtype);
7417 end if;
7419 Set_Size_Info (Def_Id, (T));
7420 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
7421 Set_Etype (Def_Id, Base_Type (T));
7423 if Constraint_OK then
7424 Set_First_Index (Def_Id, First (Constraints (C)));
7425 end if;
7427 Set_Is_Constrained (Def_Id, True);
7428 Set_Is_Aliased (Def_Id, Is_Aliased (T));
7429 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
7431 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
7432 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
7434 -- If the subtype is not that of a record component, build a freeze
7435 -- node if parent still needs one.
7437 -- If the subtype is not that of a record component, make sure
7438 -- that the Depends_On_Private status is set (explanation ???)
7439 -- and also that a conditional delay is set.
7441 if not Is_Type (Scope (Def_Id)) then
7442 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7443 Conditional_Delay (Def_Id, T);
7444 end if;
7446 end Constrain_Array;
7448 ------------------------------
7449 -- Constrain_Component_Type --
7450 ------------------------------
7452 function Constrain_Component_Type
7453 (Compon_Type : Entity_Id;
7454 Constrained_Typ : Entity_Id;
7455 Related_Node : Node_Id;
7456 Typ : Entity_Id;
7457 Constraints : Elist_Id) return Entity_Id
7459 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
7461 function Build_Constrained_Array_Type
7462 (Old_Type : Entity_Id) return Entity_Id;
7463 -- If Old_Type is an array type, one of whose indices is
7464 -- constrained by a discriminant, build an Itype whose constraint
7465 -- replaces the discriminant with its value in the constraint.
7467 function Build_Constrained_Discriminated_Type
7468 (Old_Type : Entity_Id) return Entity_Id;
7469 -- Ditto for record components.
7471 function Build_Constrained_Access_Type
7472 (Old_Type : Entity_Id) return Entity_Id;
7473 -- Ditto for access types. Makes use of previous two functions, to
7474 -- constrain designated type.
7476 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
7477 -- T is an array or discriminated type, C is a list of constraints
7478 -- that apply to T. This routine builds the constrained subtype.
7480 function Is_Discriminant (Expr : Node_Id) return Boolean;
7481 -- Returns True if Expr is a discriminant.
7483 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
7484 -- Find the value of discriminant Discrim in Constraint.
7486 -----------------------------------
7487 -- Build_Constrained_Access_Type --
7488 -----------------------------------
7490 function Build_Constrained_Access_Type
7491 (Old_Type : Entity_Id) return Entity_Id
7493 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
7494 Itype : Entity_Id;
7495 Desig_Subtype : Entity_Id;
7496 Scop : Entity_Id;
7498 begin
7499 -- if the original access type was not embedded in the enclosing
7500 -- type definition, there is no need to produce a new access
7501 -- subtype. In fact every access type with an explicit constraint
7502 -- generates an itype whose scope is the enclosing record.
7504 if not Is_Type (Scope (Old_Type)) then
7505 return Old_Type;
7507 elsif Is_Array_Type (Desig_Type) then
7508 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
7510 elsif Has_Discriminants (Desig_Type) then
7512 -- This may be an access type to an enclosing record type for
7513 -- which we are constructing the constrained components. Return
7514 -- the enclosing record subtype. This is not always correct,
7515 -- but avoids infinite recursion. ???
7517 Desig_Subtype := Any_Type;
7519 for J in reverse 0 .. Scope_Stack.Last loop
7520 Scop := Scope_Stack.Table (J).Entity;
7522 if Is_Type (Scop)
7523 and then Base_Type (Scop) = Base_Type (Desig_Type)
7524 then
7525 Desig_Subtype := Scop;
7526 end if;
7528 exit when not Is_Type (Scop);
7529 end loop;
7531 if Desig_Subtype = Any_Type then
7532 Desig_Subtype :=
7533 Build_Constrained_Discriminated_Type (Desig_Type);
7534 end if;
7536 else
7537 return Old_Type;
7538 end if;
7540 if Desig_Subtype /= Desig_Type then
7541 -- The Related_Node better be here or else we won't be able
7542 -- to attach new itypes to a node in the tree.
7544 pragma Assert (Present (Related_Node));
7546 Itype := Create_Itype (E_Access_Subtype, Related_Node);
7548 Set_Etype (Itype, Base_Type (Old_Type));
7549 Set_Size_Info (Itype, (Old_Type));
7550 Set_Directly_Designated_Type (Itype, Desig_Subtype);
7551 Set_Depends_On_Private (Itype, Has_Private_Component
7552 (Old_Type));
7553 Set_Is_Access_Constant (Itype, Is_Access_Constant
7554 (Old_Type));
7556 -- The new itype needs freezing when it depends on a not frozen
7557 -- type and the enclosing subtype needs freezing.
7559 if Has_Delayed_Freeze (Constrained_Typ)
7560 and then not Is_Frozen (Constrained_Typ)
7561 then
7562 Conditional_Delay (Itype, Base_Type (Old_Type));
7563 end if;
7565 return Itype;
7567 else
7568 return Old_Type;
7569 end if;
7570 end Build_Constrained_Access_Type;
7572 ----------------------------------
7573 -- Build_Constrained_Array_Type --
7574 ----------------------------------
7576 function Build_Constrained_Array_Type
7577 (Old_Type : Entity_Id) return Entity_Id
7579 Lo_Expr : Node_Id;
7580 Hi_Expr : Node_Id;
7581 Old_Index : Node_Id;
7582 Range_Node : Node_Id;
7583 Constr_List : List_Id;
7585 Need_To_Create_Itype : Boolean := False;
7587 begin
7588 Old_Index := First_Index (Old_Type);
7589 while Present (Old_Index) loop
7590 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
7592 if Is_Discriminant (Lo_Expr)
7593 or else Is_Discriminant (Hi_Expr)
7594 then
7595 Need_To_Create_Itype := True;
7596 end if;
7598 Next_Index (Old_Index);
7599 end loop;
7601 if Need_To_Create_Itype then
7602 Constr_List := New_List;
7604 Old_Index := First_Index (Old_Type);
7605 while Present (Old_Index) loop
7606 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
7608 if Is_Discriminant (Lo_Expr) then
7609 Lo_Expr := Get_Discr_Value (Lo_Expr);
7610 end if;
7612 if Is_Discriminant (Hi_Expr) then
7613 Hi_Expr := Get_Discr_Value (Hi_Expr);
7614 end if;
7616 Range_Node :=
7617 Make_Range
7618 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
7620 Append (Range_Node, To => Constr_List);
7622 Next_Index (Old_Index);
7623 end loop;
7625 return Build_Subtype (Old_Type, Constr_List);
7627 else
7628 return Old_Type;
7629 end if;
7630 end Build_Constrained_Array_Type;
7632 ------------------------------------------
7633 -- Build_Constrained_Discriminated_Type --
7634 ------------------------------------------
7636 function Build_Constrained_Discriminated_Type
7637 (Old_Type : Entity_Id) return Entity_Id
7639 Expr : Node_Id;
7640 Constr_List : List_Id;
7641 Old_Constraint : Elmt_Id;
7643 Need_To_Create_Itype : Boolean := False;
7645 begin
7646 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
7647 while Present (Old_Constraint) loop
7648 Expr := Node (Old_Constraint);
7650 if Is_Discriminant (Expr) then
7651 Need_To_Create_Itype := True;
7652 end if;
7654 Next_Elmt (Old_Constraint);
7655 end loop;
7657 if Need_To_Create_Itype then
7658 Constr_List := New_List;
7660 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
7661 while Present (Old_Constraint) loop
7662 Expr := Node (Old_Constraint);
7664 if Is_Discriminant (Expr) then
7665 Expr := Get_Discr_Value (Expr);
7666 end if;
7668 Append (New_Copy_Tree (Expr), To => Constr_List);
7670 Next_Elmt (Old_Constraint);
7671 end loop;
7673 return Build_Subtype (Old_Type, Constr_List);
7675 else
7676 return Old_Type;
7677 end if;
7678 end Build_Constrained_Discriminated_Type;
7680 -------------------
7681 -- Build_Subtype --
7682 -------------------
7684 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
7685 Indic : Node_Id;
7686 Subtyp_Decl : Node_Id;
7687 Def_Id : Entity_Id;
7688 Btyp : Entity_Id := Base_Type (T);
7690 begin
7691 -- The Related_Node better be here or else we won't be able
7692 -- to attach new itypes to a node in the tree.
7694 pragma Assert (Present (Related_Node));
7696 -- If the view of the component's type is incomplete or private
7697 -- with unknown discriminants, then the constraint must be applied
7698 -- to the full type.
7700 if Has_Unknown_Discriminants (Btyp)
7701 and then Present (Underlying_Type (Btyp))
7702 then
7703 Btyp := Underlying_Type (Btyp);
7704 end if;
7706 Indic :=
7707 Make_Subtype_Indication (Loc,
7708 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7709 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
7711 Def_Id := Create_Itype (Ekind (T), Related_Node);
7713 Subtyp_Decl :=
7714 Make_Subtype_Declaration (Loc,
7715 Defining_Identifier => Def_Id,
7716 Subtype_Indication => Indic);
7718 Set_Parent (Subtyp_Decl, Parent (Related_Node));
7720 -- Itypes must be analyzed with checks off (see itypes.ads).
7722 Analyze (Subtyp_Decl, Suppress => All_Checks);
7724 return Def_Id;
7725 end Build_Subtype;
7727 ---------------------
7728 -- Get_Discr_Value --
7729 ---------------------
7731 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
7732 D : Entity_Id := First_Discriminant (Typ);
7733 E : Elmt_Id := First_Elmt (Constraints);
7734 G : Elmt_Id;
7736 begin
7737 -- The discriminant may be declared for the type, in which case we
7738 -- find it by iterating over the list of discriminants. If the
7739 -- discriminant is inherited from a parent type, it appears as the
7740 -- corresponding discriminant of the current type. This will be the
7741 -- case when constraining an inherited component whose constraint is
7742 -- given by a discriminant of the parent.
7744 while Present (D) loop
7745 if D = Entity (Discrim)
7746 or else Corresponding_Discriminant (D) = Entity (Discrim)
7747 then
7748 return Node (E);
7749 end if;
7751 Next_Discriminant (D);
7752 Next_Elmt (E);
7753 end loop;
7755 -- The corresponding_Discriminant mechanism is incomplete, because
7756 -- the correspondence between new and old discriminants is not one
7757 -- to one: one new discriminant can constrain several old ones.
7758 -- In that case, scan sequentially the stored_constraint, the list
7759 -- of discriminants of the parents, and the constraints.
7761 if Is_Derived_Type (Typ)
7762 and then Present (Stored_Constraint (Typ))
7763 and then Scope (Entity (Discrim)) = Etype (Typ)
7764 then
7765 D := First_Discriminant (Etype (Typ));
7766 E := First_Elmt (Constraints);
7767 G := First_Elmt (Stored_Constraint (Typ));
7769 while Present (D) loop
7770 if D = Entity (Discrim) then
7771 return Node (E);
7772 end if;
7774 Next_Discriminant (D);
7775 Next_Elmt (E);
7776 Next_Elmt (G);
7777 end loop;
7778 end if;
7780 -- Something is wrong if we did not find the value
7782 raise Program_Error;
7783 end Get_Discr_Value;
7785 ---------------------
7786 -- Is_Discriminant --
7787 ---------------------
7789 function Is_Discriminant (Expr : Node_Id) return Boolean is
7790 Discrim_Scope : Entity_Id;
7792 begin
7793 if Denotes_Discriminant (Expr) then
7794 Discrim_Scope := Scope (Entity (Expr));
7796 -- Either we have a reference to one of Typ's discriminants,
7798 pragma Assert (Discrim_Scope = Typ
7800 -- or to the discriminants of the parent type, in the case
7801 -- of a derivation of a tagged type with variants.
7803 or else Discrim_Scope = Etype (Typ)
7804 or else Full_View (Discrim_Scope) = Etype (Typ)
7806 -- or same as above for the case where the discriminants
7807 -- were declared in Typ's private view.
7809 or else (Is_Private_Type (Discrim_Scope)
7810 and then Chars (Discrim_Scope) = Chars (Typ))
7812 -- or else we are deriving from the full view and the
7813 -- discriminant is declared in the private entity.
7815 or else (Is_Private_Type (Typ)
7816 and then Chars (Discrim_Scope) = Chars (Typ))
7818 -- or we have a class-wide type, in which case make sure the
7819 -- discriminant found belongs to the root type.
7821 or else (Is_Class_Wide_Type (Typ)
7822 and then Etype (Typ) = Discrim_Scope));
7824 return True;
7825 end if;
7827 -- In all other cases we have something wrong.
7829 return False;
7830 end Is_Discriminant;
7832 -- Start of processing for Constrain_Component_Type
7834 begin
7835 if Is_Array_Type (Compon_Type) then
7836 return Build_Constrained_Array_Type (Compon_Type);
7838 elsif Has_Discriminants (Compon_Type) then
7839 return Build_Constrained_Discriminated_Type (Compon_Type);
7841 elsif Is_Access_Type (Compon_Type) then
7842 return Build_Constrained_Access_Type (Compon_Type);
7843 end if;
7845 return Compon_Type;
7846 end Constrain_Component_Type;
7848 --------------------------
7849 -- Constrain_Concurrent --
7850 --------------------------
7852 -- For concurrent types, the associated record value type carries the same
7853 -- discriminants, so when we constrain a concurrent type, we must constrain
7854 -- the value type as well.
7856 procedure Constrain_Concurrent
7857 (Def_Id : in out Entity_Id;
7858 SI : Node_Id;
7859 Related_Nod : Node_Id;
7860 Related_Id : Entity_Id;
7861 Suffix : Character)
7863 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
7864 T_Val : Entity_Id;
7866 begin
7867 if Ekind (T_Ent) in Access_Kind then
7868 T_Ent := Designated_Type (T_Ent);
7869 end if;
7871 T_Val := Corresponding_Record_Type (T_Ent);
7873 if Present (T_Val) then
7875 if No (Def_Id) then
7876 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
7877 end if;
7879 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
7881 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
7882 Set_Corresponding_Record_Type (Def_Id,
7883 Constrain_Corresponding_Record
7884 (Def_Id, T_Val, Related_Nod, Related_Id));
7886 else
7887 -- If there is no associated record, expansion is disabled and this
7888 -- is a generic context. Create a subtype in any case, so that
7889 -- semantic analysis can proceed.
7891 if No (Def_Id) then
7892 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
7893 end if;
7895 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
7896 end if;
7897 end Constrain_Concurrent;
7899 ------------------------------------
7900 -- Constrain_Corresponding_Record --
7901 ------------------------------------
7903 function Constrain_Corresponding_Record
7904 (Prot_Subt : Entity_Id;
7905 Corr_Rec : Entity_Id;
7906 Related_Nod : Node_Id;
7907 Related_Id : Entity_Id) return Entity_Id
7909 T_Sub : constant Entity_Id
7910 := Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
7912 begin
7913 Set_Etype (T_Sub, Corr_Rec);
7914 Init_Size_Align (T_Sub);
7915 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
7916 Set_Is_Constrained (T_Sub, True);
7917 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
7918 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
7920 Conditional_Delay (T_Sub, Corr_Rec);
7922 if Has_Discriminants (Prot_Subt) then -- False only if errors.
7923 Set_Discriminant_Constraint (T_Sub,
7924 Discriminant_Constraint (Prot_Subt));
7925 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
7926 Create_Constrained_Components (T_Sub, Related_Nod, Corr_Rec,
7927 Discriminant_Constraint (T_Sub));
7928 end if;
7930 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
7932 return T_Sub;
7933 end Constrain_Corresponding_Record;
7935 -----------------------
7936 -- Constrain_Decimal --
7937 -----------------------
7939 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
7940 T : constant Entity_Id := Entity (Subtype_Mark (S));
7941 C : constant Node_Id := Constraint (S);
7942 Loc : constant Source_Ptr := Sloc (C);
7943 Range_Expr : Node_Id;
7944 Digits_Expr : Node_Id;
7945 Digits_Val : Uint;
7946 Bound_Val : Ureal;
7948 begin
7949 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
7951 if Nkind (C) = N_Range_Constraint then
7952 Range_Expr := Range_Expression (C);
7953 Digits_Val := Digits_Value (T);
7955 else
7956 pragma Assert (Nkind (C) = N_Digits_Constraint);
7957 Digits_Expr := Digits_Expression (C);
7958 Analyze_And_Resolve (Digits_Expr, Any_Integer);
7960 Check_Digits_Expression (Digits_Expr);
7961 Digits_Val := Expr_Value (Digits_Expr);
7963 if Digits_Val > Digits_Value (T) then
7964 Error_Msg_N
7965 ("digits expression is incompatible with subtype", C);
7966 Digits_Val := Digits_Value (T);
7967 end if;
7969 if Present (Range_Constraint (C)) then
7970 Range_Expr := Range_Expression (Range_Constraint (C));
7971 else
7972 Range_Expr := Empty;
7973 end if;
7974 end if;
7976 Set_Etype (Def_Id, Base_Type (T));
7977 Set_Size_Info (Def_Id, (T));
7978 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
7979 Set_Delta_Value (Def_Id, Delta_Value (T));
7980 Set_Scale_Value (Def_Id, Scale_Value (T));
7981 Set_Small_Value (Def_Id, Small_Value (T));
7982 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
7983 Set_Digits_Value (Def_Id, Digits_Val);
7985 -- Manufacture range from given digits value if no range present
7987 if No (Range_Expr) then
7988 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
7989 Range_Expr :=
7990 Make_Range (Loc,
7991 Low_Bound =>
7992 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
7993 High_Bound =>
7994 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
7996 end if;
7998 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
7999 Set_Discrete_RM_Size (Def_Id);
8001 -- Unconditionally delay the freeze, since we cannot set size
8002 -- information in all cases correctly until the freeze point.
8004 Set_Has_Delayed_Freeze (Def_Id);
8005 end Constrain_Decimal;
8007 ----------------------------------
8008 -- Constrain_Discriminated_Type --
8009 ----------------------------------
8011 procedure Constrain_Discriminated_Type
8012 (Def_Id : Entity_Id;
8013 S : Node_Id;
8014 Related_Nod : Node_Id;
8015 For_Access : Boolean := False)
8017 E : constant Entity_Id := Entity (Subtype_Mark (S));
8018 T : Entity_Id;
8019 C : Node_Id;
8020 Elist : Elist_Id := New_Elmt_List;
8022 procedure Fixup_Bad_Constraint;
8023 -- This is called after finding a bad constraint, and after having
8024 -- posted an appropriate error message. The mission is to leave the
8025 -- entity T in as reasonable state as possible!
8027 --------------------------
8028 -- Fixup_Bad_Constraint --
8029 --------------------------
8031 procedure Fixup_Bad_Constraint is
8032 begin
8033 -- Set a reasonable Ekind for the entity. For an incomplete type,
8034 -- we can't do much, but for other types, we can set the proper
8035 -- corresponding subtype kind.
8037 if Ekind (T) = E_Incomplete_Type then
8038 Set_Ekind (Def_Id, Ekind (T));
8039 else
8040 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8041 end if;
8043 Set_Etype (Def_Id, Any_Type);
8044 Set_Error_Posted (Def_Id);
8045 end Fixup_Bad_Constraint;
8047 -- Start of processing for Constrain_Discriminated_Type
8049 begin
8050 C := Constraint (S);
8052 -- A discriminant constraint is only allowed in a subtype indication,
8053 -- after a subtype mark. This subtype mark must denote either a type
8054 -- with discriminants, or an access type whose designated type is a
8055 -- type with discriminants. A discriminant constraint specifies the
8056 -- values of these discriminants (RM 3.7.2(5)).
8058 T := Base_Type (Entity (Subtype_Mark (S)));
8060 if Ekind (T) in Access_Kind then
8061 T := Designated_Type (T);
8062 end if;
8064 -- Check that the type has visible discriminants. The type may be
8065 -- a private type with unknown discriminants whose full view has
8066 -- discriminants which are invisible.
8068 if not Has_Discriminants (T)
8069 or else
8070 (Has_Unknown_Discriminants (T)
8071 and then Is_Private_Type (T))
8072 then
8073 Error_Msg_N ("invalid constraint: type has no discriminant", C);
8074 Fixup_Bad_Constraint;
8075 return;
8077 elsif Is_Constrained (E)
8078 or else (Ekind (E) = E_Class_Wide_Subtype
8079 and then Present (Discriminant_Constraint (E)))
8080 then
8081 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
8082 Fixup_Bad_Constraint;
8083 return;
8084 end if;
8086 -- T may be an unconstrained subtype (e.g. a generic actual).
8087 -- Constraint applies to the base type.
8089 T := Base_Type (T);
8091 Elist := Build_Discriminant_Constraints (T, S);
8093 -- If the list returned was empty we had an error in building the
8094 -- discriminant constraint. We have also already signalled an error
8095 -- in the incomplete type case
8097 if Is_Empty_Elmt_List (Elist) then
8098 Fixup_Bad_Constraint;
8099 return;
8100 end if;
8102 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
8103 end Constrain_Discriminated_Type;
8105 ---------------------------
8106 -- Constrain_Enumeration --
8107 ---------------------------
8109 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
8110 T : constant Entity_Id := Entity (Subtype_Mark (S));
8111 C : constant Node_Id := Constraint (S);
8113 begin
8114 Set_Ekind (Def_Id, E_Enumeration_Subtype);
8116 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
8118 Set_Etype (Def_Id, Base_Type (T));
8119 Set_Size_Info (Def_Id, (T));
8120 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8121 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
8123 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8125 Set_Discrete_RM_Size (Def_Id);
8127 end Constrain_Enumeration;
8129 ----------------------
8130 -- Constrain_Float --
8131 ----------------------
8133 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
8134 T : constant Entity_Id := Entity (Subtype_Mark (S));
8135 C : Node_Id;
8136 D : Node_Id;
8137 Rais : Node_Id;
8139 begin
8140 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
8142 Set_Etype (Def_Id, Base_Type (T));
8143 Set_Size_Info (Def_Id, (T));
8144 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8146 -- Process the constraint
8148 C := Constraint (S);
8150 -- Digits constraint present
8152 if Nkind (C) = N_Digits_Constraint then
8153 if Warn_On_Obsolescent_Feature then
8154 Error_Msg_N
8155 ("subtype digits constraint is an " &
8156 "obsolescent feature ('R'M 'J.3(8))?", C);
8157 end if;
8159 D := Digits_Expression (C);
8160 Analyze_And_Resolve (D, Any_Integer);
8161 Check_Digits_Expression (D);
8162 Set_Digits_Value (Def_Id, Expr_Value (D));
8164 -- Check that digits value is in range. Obviously we can do this
8165 -- at compile time, but it is strictly a runtime check, and of
8166 -- course there is an ACVC test that checks this!
8168 if Digits_Value (Def_Id) > Digits_Value (T) then
8169 Error_Msg_Uint_1 := Digits_Value (T);
8170 Error_Msg_N ("?digits value is too large, maximum is ^", D);
8171 Rais :=
8172 Make_Raise_Constraint_Error (Sloc (D),
8173 Reason => CE_Range_Check_Failed);
8174 Insert_Action (Declaration_Node (Def_Id), Rais);
8175 end if;
8177 C := Range_Constraint (C);
8179 -- No digits constraint present
8181 else
8182 Set_Digits_Value (Def_Id, Digits_Value (T));
8183 end if;
8185 -- Range constraint present
8187 if Nkind (C) = N_Range_Constraint then
8188 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8190 -- No range constraint present
8192 else
8193 pragma Assert (No (C));
8194 Set_Scalar_Range (Def_Id, Scalar_Range (T));
8195 end if;
8197 Set_Is_Constrained (Def_Id);
8198 end Constrain_Float;
8200 ---------------------
8201 -- Constrain_Index --
8202 ---------------------
8204 procedure Constrain_Index
8205 (Index : Node_Id;
8206 S : Node_Id;
8207 Related_Nod : Node_Id;
8208 Related_Id : Entity_Id;
8209 Suffix : Character;
8210 Suffix_Index : Nat)
8212 Def_Id : Entity_Id;
8213 R : Node_Id := Empty;
8214 T : constant Entity_Id := Etype (Index);
8216 begin
8217 if Nkind (S) = N_Range
8218 or else
8219 (Nkind (S) = N_Attribute_Reference
8220 and then Attribute_Name (S) = Name_Range)
8221 then
8222 -- A Range attribute will transformed into N_Range by Resolve.
8224 Analyze (S);
8225 Set_Etype (S, T);
8226 R := S;
8228 Process_Range_Expr_In_Decl (R, T, Empty_List);
8230 if not Error_Posted (S)
8231 and then
8232 (Nkind (S) /= N_Range
8233 or else not Covers (T, (Etype (Low_Bound (S))))
8234 or else not Covers (T, (Etype (High_Bound (S)))))
8235 then
8236 if Base_Type (T) /= Any_Type
8237 and then Etype (Low_Bound (S)) /= Any_Type
8238 and then Etype (High_Bound (S)) /= Any_Type
8239 then
8240 Error_Msg_N ("range expected", S);
8241 end if;
8242 end if;
8244 elsif Nkind (S) = N_Subtype_Indication then
8245 -- the parser has verified that this is a discrete indication.
8247 Resolve_Discrete_Subtype_Indication (S, T);
8248 R := Range_Expression (Constraint (S));
8250 elsif Nkind (S) = N_Discriminant_Association then
8252 -- syntactically valid in subtype indication.
8254 Error_Msg_N ("invalid index constraint", S);
8255 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
8256 return;
8258 -- Subtype_Mark case, no anonymous subtypes to construct
8260 else
8261 Analyze (S);
8263 if Is_Entity_Name (S) then
8265 if not Is_Type (Entity (S)) then
8266 Error_Msg_N ("expect subtype mark for index constraint", S);
8268 elsif Base_Type (Entity (S)) /= Base_Type (T) then
8269 Wrong_Type (S, Base_Type (T));
8270 end if;
8272 return;
8274 else
8275 Error_Msg_N ("invalid index constraint", S);
8276 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
8277 return;
8278 end if;
8279 end if;
8281 Def_Id :=
8282 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
8284 Set_Etype (Def_Id, Base_Type (T));
8286 if Is_Modular_Integer_Type (T) then
8287 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
8289 elsif Is_Integer_Type (T) then
8290 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
8292 else
8293 Set_Ekind (Def_Id, E_Enumeration_Subtype);
8294 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
8295 end if;
8297 Set_Size_Info (Def_Id, (T));
8298 Set_RM_Size (Def_Id, RM_Size (T));
8299 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8301 Set_Scalar_Range (Def_Id, R);
8303 Set_Etype (S, Def_Id);
8304 Set_Discrete_RM_Size (Def_Id);
8305 end Constrain_Index;
8307 -----------------------
8308 -- Constrain_Integer --
8309 -----------------------
8311 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
8312 T : constant Entity_Id := Entity (Subtype_Mark (S));
8313 C : constant Node_Id := Constraint (S);
8315 begin
8316 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8318 if Is_Modular_Integer_Type (T) then
8319 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
8320 else
8321 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
8322 end if;
8324 Set_Etype (Def_Id, Base_Type (T));
8325 Set_Size_Info (Def_Id, (T));
8326 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8327 Set_Discrete_RM_Size (Def_Id);
8329 end Constrain_Integer;
8331 ------------------------------
8332 -- Constrain_Ordinary_Fixed --
8333 ------------------------------
8335 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
8336 T : constant Entity_Id := Entity (Subtype_Mark (S));
8337 C : Node_Id;
8338 D : Node_Id;
8339 Rais : Node_Id;
8341 begin
8342 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
8343 Set_Etype (Def_Id, Base_Type (T));
8344 Set_Size_Info (Def_Id, (T));
8345 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8346 Set_Small_Value (Def_Id, Small_Value (T));
8348 -- Process the constraint
8350 C := Constraint (S);
8352 -- Delta constraint present
8354 if Nkind (C) = N_Delta_Constraint then
8355 if Warn_On_Obsolescent_Feature then
8356 Error_Msg_S
8357 ("subtype delta constraint is an " &
8358 "obsolescent feature ('R'M 'J.3(7))?");
8359 end if;
8361 D := Delta_Expression (C);
8362 Analyze_And_Resolve (D, Any_Real);
8363 Check_Delta_Expression (D);
8364 Set_Delta_Value (Def_Id, Expr_Value_R (D));
8366 -- Check that delta value is in range. Obviously we can do this
8367 -- at compile time, but it is strictly a runtime check, and of
8368 -- course there is an ACVC test that checks this!
8370 if Delta_Value (Def_Id) < Delta_Value (T) then
8371 Error_Msg_N ("?delta value is too small", D);
8372 Rais :=
8373 Make_Raise_Constraint_Error (Sloc (D),
8374 Reason => CE_Range_Check_Failed);
8375 Insert_Action (Declaration_Node (Def_Id), Rais);
8376 end if;
8378 C := Range_Constraint (C);
8380 -- No delta constraint present
8382 else
8383 Set_Delta_Value (Def_Id, Delta_Value (T));
8384 end if;
8386 -- Range constraint present
8388 if Nkind (C) = N_Range_Constraint then
8389 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8391 -- No range constraint present
8393 else
8394 pragma Assert (No (C));
8395 Set_Scalar_Range (Def_Id, Scalar_Range (T));
8397 end if;
8399 Set_Discrete_RM_Size (Def_Id);
8401 -- Unconditionally delay the freeze, since we cannot set size
8402 -- information in all cases correctly until the freeze point.
8404 Set_Has_Delayed_Freeze (Def_Id);
8405 end Constrain_Ordinary_Fixed;
8407 ---------------------------
8408 -- Convert_Scalar_Bounds --
8409 ---------------------------
8411 procedure Convert_Scalar_Bounds
8412 (N : Node_Id;
8413 Parent_Type : Entity_Id;
8414 Derived_Type : Entity_Id;
8415 Loc : Source_Ptr)
8417 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
8419 Lo : Node_Id;
8420 Hi : Node_Id;
8421 Rng : Node_Id;
8423 begin
8424 Lo := Build_Scalar_Bound
8425 (Type_Low_Bound (Derived_Type),
8426 Parent_Type, Implicit_Base);
8428 Hi := Build_Scalar_Bound
8429 (Type_High_Bound (Derived_Type),
8430 Parent_Type, Implicit_Base);
8432 Rng :=
8433 Make_Range (Loc,
8434 Low_Bound => Lo,
8435 High_Bound => Hi);
8437 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
8439 Set_Parent (Rng, N);
8440 Set_Scalar_Range (Derived_Type, Rng);
8442 -- Analyze the bounds
8444 Analyze_And_Resolve (Lo, Implicit_Base);
8445 Analyze_And_Resolve (Hi, Implicit_Base);
8447 -- Analyze the range itself, except that we do not analyze it if
8448 -- the bounds are real literals, and we have a fixed-point type.
8449 -- The reason for this is that we delay setting the bounds in this
8450 -- case till we know the final Small and Size values (see circuit
8451 -- in Freeze.Freeze_Fixed_Point_Type for further details).
8453 if Is_Fixed_Point_Type (Parent_Type)
8454 and then Nkind (Lo) = N_Real_Literal
8455 and then Nkind (Hi) = N_Real_Literal
8456 then
8457 return;
8459 -- Here we do the analysis of the range.
8461 -- Note: we do this manually, since if we do a normal Analyze and
8462 -- Resolve call, there are problems with the conversions used for
8463 -- the derived type range.
8465 else
8466 Set_Etype (Rng, Implicit_Base);
8467 Set_Analyzed (Rng, True);
8468 end if;
8469 end Convert_Scalar_Bounds;
8471 -------------------
8472 -- Copy_And_Swap --
8473 -------------------
8475 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
8477 begin
8478 -- Initialize new full declaration entity by copying the pertinent
8479 -- fields of the corresponding private declaration entity.
8481 -- We temporarily set Ekind to a value appropriate for a type to
8482 -- avoid assert failures in Einfo from checking for setting type
8483 -- attributes on something that is not a type. Ekind (Priv) is an
8484 -- appropriate choice, since it allowed the attributes to be set
8485 -- in the first place. This Ekind value will be modified later.
8487 Set_Ekind (Full, Ekind (Priv));
8489 -- Also set Etype temporarily to Any_Type, again, in the absence
8490 -- of errors, it will be properly reset, and if there are errors,
8491 -- then we want a value of Any_Type to remain.
8493 Set_Etype (Full, Any_Type);
8495 -- Now start copying attributes
8497 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
8499 if Has_Discriminants (Full) then
8500 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
8501 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
8502 end if;
8504 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
8505 Set_Homonym (Full, Homonym (Priv));
8506 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
8507 Set_Is_Public (Full, Is_Public (Priv));
8508 Set_Is_Pure (Full, Is_Pure (Priv));
8509 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
8511 Conditional_Delay (Full, Priv);
8513 if Is_Tagged_Type (Full) then
8514 Set_Primitive_Operations (Full, Primitive_Operations (Priv));
8516 if Priv = Base_Type (Priv) then
8517 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
8518 end if;
8519 end if;
8521 Set_Is_Volatile (Full, Is_Volatile (Priv));
8522 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
8523 Set_Scope (Full, Scope (Priv));
8524 Set_Next_Entity (Full, Next_Entity (Priv));
8525 Set_First_Entity (Full, First_Entity (Priv));
8526 Set_Last_Entity (Full, Last_Entity (Priv));
8528 -- If access types have been recorded for later handling, keep them
8529 -- in the full view so that they get handled when the full view
8530 -- freeze node is expanded.
8532 if Present (Freeze_Node (Priv))
8533 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
8534 then
8535 Ensure_Freeze_Node (Full);
8536 Set_Access_Types_To_Process
8537 (Freeze_Node (Full),
8538 Access_Types_To_Process (Freeze_Node (Priv)));
8539 end if;
8541 -- Swap the two entities. Now Privat is the full type entity and
8542 -- Full is the private one. They will be swapped back at the end
8543 -- of the private part. This swapping ensures that the entity that
8544 -- is visible in the private part is the full declaration.
8546 Exchange_Entities (Priv, Full);
8547 Append_Entity (Full, Scope (Full));
8548 end Copy_And_Swap;
8550 -------------------------------------
8551 -- Copy_Array_Base_Type_Attributes --
8552 -------------------------------------
8554 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
8555 begin
8556 Set_Component_Alignment (T1, Component_Alignment (T2));
8557 Set_Component_Type (T1, Component_Type (T2));
8558 Set_Component_Size (T1, Component_Size (T2));
8559 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
8560 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
8561 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
8562 Set_Has_Task (T1, Has_Task (T2));
8563 Set_Is_Packed (T1, Is_Packed (T2));
8564 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
8565 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
8566 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
8567 end Copy_Array_Base_Type_Attributes;
8569 -----------------------------------
8570 -- Copy_Array_Subtype_Attributes --
8571 -----------------------------------
8573 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
8574 begin
8575 Set_Size_Info (T1, T2);
8577 Set_First_Index (T1, First_Index (T2));
8578 Set_Is_Aliased (T1, Is_Aliased (T2));
8579 Set_Is_Atomic (T1, Is_Atomic (T2));
8580 Set_Is_Volatile (T1, Is_Volatile (T2));
8581 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
8582 Set_Is_Constrained (T1, Is_Constrained (T2));
8583 Set_Depends_On_Private (T1, Has_Private_Component (T2));
8584 Set_First_Rep_Item (T1, First_Rep_Item (T2));
8585 Set_Convention (T1, Convention (T2));
8586 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
8587 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
8588 end Copy_Array_Subtype_Attributes;
8590 -----------------------------------
8591 -- Create_Constrained_Components --
8592 -----------------------------------
8594 procedure Create_Constrained_Components
8595 (Subt : Entity_Id;
8596 Decl_Node : Node_Id;
8597 Typ : Entity_Id;
8598 Constraints : Elist_Id)
8600 Loc : constant Source_Ptr := Sloc (Subt);
8601 Comp_List : constant Elist_Id := New_Elmt_List;
8602 Parent_Type : constant Entity_Id := Etype (Typ);
8603 Assoc_List : constant List_Id := New_List;
8604 Discr_Val : Elmt_Id;
8605 Errors : Boolean;
8606 New_C : Entity_Id;
8607 Old_C : Entity_Id;
8608 Is_Static : Boolean := True;
8610 procedure Collect_Fixed_Components (Typ : Entity_Id);
8611 -- Collect parent type components that do not appear in a variant part
8613 procedure Create_All_Components;
8614 -- Iterate over Comp_List to create the components of the subtype.
8616 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
8617 -- Creates a new component from Old_Compon, copying all the fields from
8618 -- it, including its Etype, inserts the new component in the Subt entity
8619 -- chain and returns the new component.
8621 function Is_Variant_Record (T : Entity_Id) return Boolean;
8622 -- If true, and discriminants are static, collect only components from
8623 -- variants selected by discriminant values.
8625 ------------------------------
8626 -- Collect_Fixed_Components --
8627 ------------------------------
8629 procedure Collect_Fixed_Components (Typ : Entity_Id) is
8630 begin
8631 -- Build association list for discriminants, and find components of
8632 -- the variant part selected by the values of the discriminants.
8634 Old_C := First_Discriminant (Typ);
8635 Discr_Val := First_Elmt (Constraints);
8637 while Present (Old_C) loop
8638 Append_To (Assoc_List,
8639 Make_Component_Association (Loc,
8640 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
8641 Expression => New_Copy (Node (Discr_Val))));
8643 Next_Elmt (Discr_Val);
8644 Next_Discriminant (Old_C);
8645 end loop;
8647 -- The tag, and the possible parent and controller components
8648 -- are unconditionally in the subtype.
8650 if Is_Tagged_Type (Typ)
8651 or else Has_Controlled_Component (Typ)
8652 then
8653 Old_C := First_Component (Typ);
8655 while Present (Old_C) loop
8656 if Chars ((Old_C)) = Name_uTag
8657 or else Chars ((Old_C)) = Name_uParent
8658 or else Chars ((Old_C)) = Name_uController
8659 then
8660 Append_Elmt (Old_C, Comp_List);
8661 end if;
8663 Next_Component (Old_C);
8664 end loop;
8665 end if;
8666 end Collect_Fixed_Components;
8668 ---------------------------
8669 -- Create_All_Components --
8670 ---------------------------
8672 procedure Create_All_Components is
8673 Comp : Elmt_Id;
8675 begin
8676 Comp := First_Elmt (Comp_List);
8678 while Present (Comp) loop
8679 Old_C := Node (Comp);
8680 New_C := Create_Component (Old_C);
8682 Set_Etype
8683 (New_C,
8684 Constrain_Component_Type
8685 (Etype (Old_C), Subt, Decl_Node, Typ, Constraints));
8686 Set_Is_Public (New_C, Is_Public (Subt));
8688 Next_Elmt (Comp);
8689 end loop;
8690 end Create_All_Components;
8692 ----------------------
8693 -- Create_Component --
8694 ----------------------
8696 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
8697 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
8699 begin
8700 -- Set the parent so we have a proper link for freezing etc. This
8701 -- is not a real parent pointer, since of course our parent does
8702 -- not own up to us and reference us, we are an illegitimate
8703 -- child of the original parent!
8705 Set_Parent (New_Compon, Parent (Old_Compon));
8707 -- We do not want this node marked as Comes_From_Source, since
8708 -- otherwise it would get first class status and a separate
8709 -- cross-reference line would be generated. Illegitimate
8710 -- children do not rate such recognition.
8712 Set_Comes_From_Source (New_Compon, False);
8714 -- But it is a real entity, and a birth certificate must be
8715 -- properly registered by entering it into the entity list.
8717 Enter_Name (New_Compon);
8718 return New_Compon;
8719 end Create_Component;
8721 -----------------------
8722 -- Is_Variant_Record --
8723 -----------------------
8725 function Is_Variant_Record (T : Entity_Id) return Boolean is
8726 begin
8727 return Nkind (Parent (T)) = N_Full_Type_Declaration
8728 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
8729 and then Present (Component_List (Type_Definition (Parent (T))))
8730 and then Present (
8731 Variant_Part (Component_List (Type_Definition (Parent (T)))));
8732 end Is_Variant_Record;
8734 -- Start of processing for Create_Constrained_Components
8736 begin
8737 pragma Assert (Subt /= Base_Type (Subt));
8738 pragma Assert (Typ = Base_Type (Typ));
8740 Set_First_Entity (Subt, Empty);
8741 Set_Last_Entity (Subt, Empty);
8743 -- Check whether constraint is fully static, in which case we can
8744 -- optimize the list of components.
8746 Discr_Val := First_Elmt (Constraints);
8748 while Present (Discr_Val) loop
8750 if not Is_OK_Static_Expression (Node (Discr_Val)) then
8751 Is_Static := False;
8752 exit;
8753 end if;
8755 Next_Elmt (Discr_Val);
8756 end loop;
8758 New_Scope (Subt);
8760 -- Inherit the discriminants of the parent type.
8762 Old_C := First_Discriminant (Typ);
8764 while Present (Old_C) loop
8765 New_C := Create_Component (Old_C);
8766 Set_Is_Public (New_C, Is_Public (Subt));
8767 Next_Discriminant (Old_C);
8768 end loop;
8770 if Is_Static
8771 and then Is_Variant_Record (Typ)
8772 then
8773 Collect_Fixed_Components (Typ);
8775 Gather_Components (
8776 Typ,
8777 Component_List (Type_Definition (Parent (Typ))),
8778 Governed_By => Assoc_List,
8779 Into => Comp_List,
8780 Report_Errors => Errors);
8781 pragma Assert (not Errors);
8783 Create_All_Components;
8785 -- If the subtype declaration is created for a tagged type derivation
8786 -- with constraints, we retrieve the record definition of the parent
8787 -- type to select the components of the proper variant.
8789 elsif Is_Static
8790 and then Is_Tagged_Type (Typ)
8791 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
8792 and then
8793 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
8794 and then Is_Variant_Record (Parent_Type)
8795 then
8796 Collect_Fixed_Components (Typ);
8798 Gather_Components (
8799 Typ,
8800 Component_List (Type_Definition (Parent (Parent_Type))),
8801 Governed_By => Assoc_List,
8802 Into => Comp_List,
8803 Report_Errors => Errors);
8804 pragma Assert (not Errors);
8806 -- If the tagged derivation has a type extension, collect all the
8807 -- new components therein.
8809 if Present
8810 (Record_Extension_Part (Type_Definition (Parent (Typ))))
8811 then
8812 Old_C := First_Component (Typ);
8814 while Present (Old_C) loop
8815 if Original_Record_Component (Old_C) = Old_C
8816 and then Chars (Old_C) /= Name_uTag
8817 and then Chars (Old_C) /= Name_uParent
8818 and then Chars (Old_C) /= Name_uController
8819 then
8820 Append_Elmt (Old_C, Comp_List);
8821 end if;
8823 Next_Component (Old_C);
8824 end loop;
8825 end if;
8827 Create_All_Components;
8829 else
8830 -- If the discriminants are not static, or if this is a multi-level
8831 -- type extension, we have to include all the components of the
8832 -- parent type.
8834 Old_C := First_Component (Typ);
8836 while Present (Old_C) loop
8837 New_C := Create_Component (Old_C);
8839 Set_Etype
8840 (New_C,
8841 Constrain_Component_Type
8842 (Etype (Old_C), Subt, Decl_Node, Typ, Constraints));
8843 Set_Is_Public (New_C, Is_Public (Subt));
8845 Next_Component (Old_C);
8846 end loop;
8847 end if;
8849 End_Scope;
8850 end Create_Constrained_Components;
8852 ------------------------------------------
8853 -- Decimal_Fixed_Point_Type_Declaration --
8854 ------------------------------------------
8856 procedure Decimal_Fixed_Point_Type_Declaration
8857 (T : Entity_Id;
8858 Def : Node_Id)
8860 Loc : constant Source_Ptr := Sloc (Def);
8861 Digs_Expr : constant Node_Id := Digits_Expression (Def);
8862 Delta_Expr : constant Node_Id := Delta_Expression (Def);
8863 Implicit_Base : Entity_Id;
8864 Digs_Val : Uint;
8865 Delta_Val : Ureal;
8866 Scale_Val : Uint;
8867 Bound_Val : Ureal;
8869 -- Start of processing for Decimal_Fixed_Point_Type_Declaration
8871 begin
8872 Check_Restriction (No_Fixed_Point, Def);
8874 -- Create implicit base type
8876 Implicit_Base :=
8877 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
8878 Set_Etype (Implicit_Base, Implicit_Base);
8880 -- Analyze and process delta expression
8882 Analyze_And_Resolve (Delta_Expr, Universal_Real);
8884 Check_Delta_Expression (Delta_Expr);
8885 Delta_Val := Expr_Value_R (Delta_Expr);
8887 -- Check delta is power of 10, and determine scale value from it
8889 declare
8890 Val : Ureal := Delta_Val;
8892 begin
8893 Scale_Val := Uint_0;
8895 if Val < Ureal_1 then
8896 while Val < Ureal_1 loop
8897 Val := Val * Ureal_10;
8898 Scale_Val := Scale_Val + 1;
8899 end loop;
8901 if Scale_Val > 18 then
8902 Error_Msg_N ("scale exceeds maximum value of 18", Def);
8903 Scale_Val := UI_From_Int (+18);
8904 end if;
8906 else
8907 while Val > Ureal_1 loop
8908 Val := Val / Ureal_10;
8909 Scale_Val := Scale_Val - 1;
8910 end loop;
8912 if Scale_Val < -18 then
8913 Error_Msg_N ("scale is less than minimum value of -18", Def);
8914 Scale_Val := UI_From_Int (-18);
8915 end if;
8916 end if;
8918 if Val /= Ureal_1 then
8919 Error_Msg_N ("delta expression must be a power of 10", Def);
8920 Delta_Val := Ureal_10 ** (-Scale_Val);
8921 end if;
8922 end;
8924 -- Set delta, scale and small (small = delta for decimal type)
8926 Set_Delta_Value (Implicit_Base, Delta_Val);
8927 Set_Scale_Value (Implicit_Base, Scale_Val);
8928 Set_Small_Value (Implicit_Base, Delta_Val);
8930 -- Analyze and process digits expression
8932 Analyze_And_Resolve (Digs_Expr, Any_Integer);
8933 Check_Digits_Expression (Digs_Expr);
8934 Digs_Val := Expr_Value (Digs_Expr);
8936 if Digs_Val > 18 then
8937 Digs_Val := UI_From_Int (+18);
8938 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
8939 end if;
8941 Set_Digits_Value (Implicit_Base, Digs_Val);
8942 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
8944 -- Set range of base type from digits value for now. This will be
8945 -- expanded to represent the true underlying base range by Freeze.
8947 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
8949 -- Set size to zero for now, size will be set at freeze time. We have
8950 -- to do this for ordinary fixed-point, because the size depends on
8951 -- the specified small, and we might as well do the same for decimal
8952 -- fixed-point.
8954 Init_Size_Align (Implicit_Base);
8956 -- If there are bounds given in the declaration use them as the
8957 -- bounds of the first named subtype.
8959 if Present (Real_Range_Specification (Def)) then
8960 declare
8961 RRS : constant Node_Id := Real_Range_Specification (Def);
8962 Low : constant Node_Id := Low_Bound (RRS);
8963 High : constant Node_Id := High_Bound (RRS);
8964 Low_Val : Ureal;
8965 High_Val : Ureal;
8967 begin
8968 Analyze_And_Resolve (Low, Any_Real);
8969 Analyze_And_Resolve (High, Any_Real);
8970 Check_Real_Bound (Low);
8971 Check_Real_Bound (High);
8972 Low_Val := Expr_Value_R (Low);
8973 High_Val := Expr_Value_R (High);
8975 if Low_Val < (-Bound_Val) then
8976 Error_Msg_N
8977 ("range low bound too small for digits value", Low);
8978 Low_Val := -Bound_Val;
8979 end if;
8981 if High_Val > Bound_Val then
8982 Error_Msg_N
8983 ("range high bound too large for digits value", High);
8984 High_Val := Bound_Val;
8985 end if;
8987 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
8988 end;
8990 -- If no explicit range, use range that corresponds to given
8991 -- digits value. This will end up as the final range for the
8992 -- first subtype.
8994 else
8995 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
8996 end if;
8998 -- Complete entity for first subtype
9000 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
9001 Set_Etype (T, Implicit_Base);
9002 Set_Size_Info (T, Implicit_Base);
9003 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
9004 Set_Digits_Value (T, Digs_Val);
9005 Set_Delta_Value (T, Delta_Val);
9006 Set_Small_Value (T, Delta_Val);
9007 Set_Scale_Value (T, Scale_Val);
9008 Set_Is_Constrained (T);
9009 end Decimal_Fixed_Point_Type_Declaration;
9011 -----------------------
9012 -- Derive_Subprogram --
9013 -----------------------
9015 procedure Derive_Subprogram
9016 (New_Subp : in out Entity_Id;
9017 Parent_Subp : Entity_Id;
9018 Derived_Type : Entity_Id;
9019 Parent_Type : Entity_Id;
9020 Actual_Subp : Entity_Id := Empty)
9022 Formal : Entity_Id;
9023 New_Formal : Entity_Id;
9024 Visible_Subp : Entity_Id := Parent_Subp;
9026 function Is_Private_Overriding return Boolean;
9027 -- If Subp is a private overriding of a visible operation, the in-
9028 -- herited operation derives from the overridden op (even though
9029 -- its body is the overriding one) and the inherited operation is
9030 -- visible now. See sem_disp to see the details of the handling of
9031 -- the overridden subprogram, which is removed from the list of
9032 -- primitive operations of the type. The overridden subprogram is
9033 -- saved locally in Visible_Subp, and used to diagnose abstract
9034 -- operations that need overriding in the derived type.
9036 procedure Replace_Type (Id, New_Id : Entity_Id);
9037 -- When the type is an anonymous access type, create a new access type
9038 -- designating the derived type.
9040 procedure Set_Derived_Name;
9041 -- This procedure sets the appropriate Chars name for New_Subp. This
9042 -- is normally just a copy of the parent name. An exception arises for
9043 -- type support subprograms, where the name is changed to reflect the
9044 -- name of the derived type, e.g. if type foo is derived from type bar,
9045 -- then a procedure barDA is derived with a name fooDA.
9047 ---------------------------
9048 -- Is_Private_Overriding --
9049 ---------------------------
9051 function Is_Private_Overriding return Boolean is
9052 Prev : Entity_Id;
9054 begin
9055 Prev := Homonym (Parent_Subp);
9057 -- The visible operation that is overriden is a homonym of
9058 -- the parent subprogram. We scan the homonym chain to find
9059 -- the one whose alias is the subprogram we are deriving.
9061 while Present (Prev) loop
9062 if Is_Dispatching_Operation (Parent_Subp)
9063 and then Present (Prev)
9064 and then Ekind (Prev) = Ekind (Parent_Subp)
9065 and then Alias (Prev) = Parent_Subp
9066 and then Scope (Parent_Subp) = Scope (Prev)
9067 and then not Is_Hidden (Prev)
9068 then
9069 Visible_Subp := Prev;
9070 return True;
9071 end if;
9073 Prev := Homonym (Prev);
9074 end loop;
9076 return False;
9077 end Is_Private_Overriding;
9079 ------------------
9080 -- Replace_Type --
9081 ------------------
9083 procedure Replace_Type (Id, New_Id : Entity_Id) is
9084 Acc_Type : Entity_Id;
9085 IR : Node_Id;
9086 Par : constant Node_Id := Parent (Derived_Type);
9088 begin
9089 -- When the type is an anonymous access type, create a new access
9090 -- type designating the derived type. This itype must be elaborated
9091 -- at the point of the derivation, not on subsequent calls that may
9092 -- be out of the proper scope for Gigi, so we insert a reference to
9093 -- it after the derivation.
9095 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
9096 declare
9097 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
9099 begin
9100 if Ekind (Desig_Typ) = E_Record_Type_With_Private
9101 and then Present (Full_View (Desig_Typ))
9102 and then not Is_Private_Type (Parent_Type)
9103 then
9104 Desig_Typ := Full_View (Desig_Typ);
9105 end if;
9107 if Base_Type (Desig_Typ) = Base_Type (Parent_Type) then
9108 Acc_Type := New_Copy (Etype (Id));
9109 Set_Etype (Acc_Type, Acc_Type);
9110 Set_Scope (Acc_Type, New_Subp);
9112 -- Compute size of anonymous access type.
9114 if Is_Array_Type (Desig_Typ)
9115 and then not Is_Constrained (Desig_Typ)
9116 then
9117 Init_Size (Acc_Type, 2 * System_Address_Size);
9118 else
9119 Init_Size (Acc_Type, System_Address_Size);
9120 end if;
9122 Init_Alignment (Acc_Type);
9124 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
9126 Set_Etype (New_Id, Acc_Type);
9127 Set_Scope (New_Id, New_Subp);
9129 -- Create a reference to it
9131 IR := Make_Itype_Reference (Sloc (Parent (Derived_Type)));
9132 Set_Itype (IR, Acc_Type);
9133 Insert_After (Parent (Derived_Type), IR);
9135 else
9136 Set_Etype (New_Id, Etype (Id));
9137 end if;
9138 end;
9140 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
9141 or else
9142 (Ekind (Etype (Id)) = E_Record_Type_With_Private
9143 and then Present (Full_View (Etype (Id)))
9144 and then
9145 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
9146 then
9147 -- Constraint checks on formals are generated during expansion,
9148 -- based on the signature of the original subprogram. The bounds
9149 -- of the derived type are not relevant, and thus we can use
9150 -- the base type for the formals. However, the return type may be
9151 -- used in a context that requires that the proper static bounds
9152 -- be used (a case statement, for example) and for those cases
9153 -- we must use the derived type (first subtype), not its base.
9155 -- If the derived_type_definition has no constraints, we know that
9156 -- the derived type has the same constraints as the first subtype
9157 -- of the parent, and we can also use it rather than its base,
9158 -- which can lead to more efficient code.
9160 if Etype (Id) = Parent_Type then
9161 if Is_Scalar_Type (Parent_Type)
9162 and then
9163 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
9164 then
9165 Set_Etype (New_Id, Derived_Type);
9167 elsif Nkind (Par) = N_Full_Type_Declaration
9168 and then
9169 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
9170 and then
9171 Is_Entity_Name
9172 (Subtype_Indication (Type_Definition (Par)))
9173 then
9174 Set_Etype (New_Id, Derived_Type);
9176 else
9177 Set_Etype (New_Id, Base_Type (Derived_Type));
9178 end if;
9180 else
9181 Set_Etype (New_Id, Base_Type (Derived_Type));
9182 end if;
9184 else
9185 Set_Etype (New_Id, Etype (Id));
9186 end if;
9187 end Replace_Type;
9189 ----------------------
9190 -- Set_Derived_Name --
9191 ----------------------
9193 procedure Set_Derived_Name is
9194 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
9195 begin
9196 if Nm = TSS_Null then
9197 Set_Chars (New_Subp, Chars (Parent_Subp));
9198 else
9199 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
9200 end if;
9201 end Set_Derived_Name;
9203 -- Start of processing for Derive_Subprogram
9205 begin
9206 New_Subp :=
9207 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
9208 Set_Ekind (New_Subp, Ekind (Parent_Subp));
9210 -- Check whether the inherited subprogram is a private operation that
9211 -- should be inherited but not yet made visible. Such subprograms can
9212 -- become visible at a later point (e.g., the private part of a public
9213 -- child unit) via Declare_Inherited_Private_Subprograms. If the
9214 -- following predicate is true, then this is not such a private
9215 -- operation and the subprogram simply inherits the name of the parent
9216 -- subprogram. Note the special check for the names of controlled
9217 -- operations, which are currently exempted from being inherited with
9218 -- a hidden name because they must be findable for generation of
9219 -- implicit run-time calls.
9221 if not Is_Hidden (Parent_Subp)
9222 or else Is_Internal (Parent_Subp)
9223 or else Is_Private_Overriding
9224 or else Is_Internal_Name (Chars (Parent_Subp))
9225 or else Chars (Parent_Subp) = Name_Initialize
9226 or else Chars (Parent_Subp) = Name_Adjust
9227 or else Chars (Parent_Subp) = Name_Finalize
9228 then
9229 Set_Derived_Name;
9231 -- If parent is hidden, this can be a regular derivation if the
9232 -- parent is immediately visible in a non-instantiating context,
9233 -- or if we are in the private part of an instance. This test
9234 -- should still be refined ???
9236 -- The test for In_Instance_Not_Visible avoids inheriting the
9237 -- derived operation as a non-visible operation in cases where
9238 -- the parent subprogram might not be visible now, but was
9239 -- visible within the original generic, so it would be wrong
9240 -- to make the inherited subprogram non-visible now. (Not
9241 -- clear if this test is fully correct; are there any cases
9242 -- where we should declare the inherited operation as not
9243 -- visible to avoid it being overridden, e.g., when the
9244 -- parent type is a generic actual with private primitives ???)
9246 -- (they should be treated the same as other private inherited
9247 -- subprograms, but it's not clear how to do this cleanly). ???
9249 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
9250 and then Is_Immediately_Visible (Parent_Subp)
9251 and then not In_Instance)
9252 or else In_Instance_Not_Visible
9253 then
9254 Set_Derived_Name;
9256 -- The type is inheriting a private operation, so enter
9257 -- it with a special name so it can't be overridden.
9259 else
9260 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
9261 end if;
9263 Set_Parent (New_Subp, Parent (Derived_Type));
9264 Replace_Type (Parent_Subp, New_Subp);
9265 Conditional_Delay (New_Subp, Parent_Subp);
9267 Formal := First_Formal (Parent_Subp);
9268 while Present (Formal) loop
9269 New_Formal := New_Copy (Formal);
9271 -- Normally we do not go copying parents, but in the case of
9272 -- formals, we need to link up to the declaration (which is
9273 -- the parameter specification), and it is fine to link up to
9274 -- the original formal's parameter specification in this case.
9276 Set_Parent (New_Formal, Parent (Formal));
9278 Append_Entity (New_Formal, New_Subp);
9280 Replace_Type (Formal, New_Formal);
9281 Next_Formal (Formal);
9282 end loop;
9284 -- If this derivation corresponds to a tagged generic actual, then
9285 -- primitive operations rename those of the actual. Otherwise the
9286 -- primitive operations rename those of the parent type, If the
9287 -- parent renames an intrinsic operator, so does the new subprogram.
9288 -- We except concatenation, which is always properly typed, and does
9289 -- not get expanded as other intrinsic operations.
9291 if No (Actual_Subp) then
9292 if Is_Intrinsic_Subprogram (Parent_Subp) then
9293 Set_Is_Intrinsic_Subprogram (New_Subp);
9295 if Present (Alias (Parent_Subp))
9296 and then Chars (Parent_Subp) /= Name_Op_Concat
9297 then
9298 Set_Alias (New_Subp, Alias (Parent_Subp));
9299 else
9300 Set_Alias (New_Subp, Parent_Subp);
9301 end if;
9303 else
9304 Set_Alias (New_Subp, Parent_Subp);
9305 end if;
9307 else
9308 Set_Alias (New_Subp, Actual_Subp);
9309 end if;
9311 -- Derived subprograms of a tagged type must inherit the convention
9312 -- of the parent subprogram (a requirement of AI-117). Derived
9313 -- subprograms of untagged types simply get convention Ada by default.
9315 if Is_Tagged_Type (Derived_Type) then
9316 Set_Convention (New_Subp, Convention (Parent_Subp));
9317 end if;
9319 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
9320 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
9322 if Ekind (Parent_Subp) = E_Procedure then
9323 Set_Is_Valued_Procedure
9324 (New_Subp, Is_Valued_Procedure (Parent_Subp));
9325 end if;
9327 -- A derived function with a controlling result is abstract.
9328 -- If the Derived_Type is a nonabstract formal generic derived
9329 -- type, then inherited operations are not abstract: check is
9330 -- done at instantiation time. If the derivation is for a generic
9331 -- actual, the function is not abstract unless the actual is.
9333 if Is_Generic_Type (Derived_Type)
9334 and then not Is_Abstract (Derived_Type)
9335 then
9336 null;
9338 elsif Is_Abstract (Alias (New_Subp))
9339 or else (Is_Tagged_Type (Derived_Type)
9340 and then Etype (New_Subp) = Derived_Type
9341 and then No (Actual_Subp))
9342 then
9343 Set_Is_Abstract (New_Subp);
9345 -- Finally, if the parent type is abstract we must verify that all
9346 -- inherited operations are either non-abstract or overridden, or
9347 -- that the derived type itself is abstract (this check is performed
9348 -- at the end of a package declaration, in Check_Abstract_Overriding).
9349 -- A private overriding in the parent type will not be visible in the
9350 -- derivation if we are not in an inner package or in a child unit of
9351 -- the parent type, in which case the abstractness of the inherited
9352 -- operation is carried to the new subprogram.
9354 elsif Is_Abstract (Parent_Type)
9355 and then not In_Open_Scopes (Scope (Parent_Type))
9356 and then Is_Private_Overriding
9357 and then Is_Abstract (Visible_Subp)
9358 then
9359 Set_Alias (New_Subp, Visible_Subp);
9360 Set_Is_Abstract (New_Subp);
9361 end if;
9363 New_Overloaded_Entity (New_Subp, Derived_Type);
9365 -- Check for case of a derived subprogram for the instantiation
9366 -- of a formal derived tagged type, if so mark the subprogram as
9367 -- dispatching and inherit the dispatching attributes of the
9368 -- parent subprogram. The derived subprogram is effectively a
9369 -- renaming of the actual subprogram, so it needs to have the
9370 -- same attributes as the actual.
9372 if Present (Actual_Subp)
9373 and then Is_Dispatching_Operation (Parent_Subp)
9374 then
9375 Set_Is_Dispatching_Operation (New_Subp);
9376 if Present (DTC_Entity (Parent_Subp)) then
9377 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
9378 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
9379 end if;
9380 end if;
9382 -- Indicate that a derived subprogram does not require a body
9383 -- and that it does not require processing of default expressions.
9385 Set_Has_Completion (New_Subp);
9386 Set_Default_Expressions_Processed (New_Subp);
9388 if Ekind (New_Subp) = E_Function then
9389 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
9390 end if;
9391 end Derive_Subprogram;
9393 ------------------------
9394 -- Derive_Subprograms --
9395 ------------------------
9397 procedure Derive_Subprograms
9398 (Parent_Type : Entity_Id;
9399 Derived_Type : Entity_Id;
9400 Generic_Actual : Entity_Id := Empty)
9402 Op_List : constant Elist_Id :=
9403 Collect_Primitive_Operations (Parent_Type);
9404 Act_List : Elist_Id;
9405 Act_Elmt : Elmt_Id;
9406 Elmt : Elmt_Id;
9407 Subp : Entity_Id;
9408 New_Subp : Entity_Id := Empty;
9409 Parent_Base : Entity_Id;
9411 begin
9412 if Ekind (Parent_Type) = E_Record_Type_With_Private
9413 and then Has_Discriminants (Parent_Type)
9414 and then Present (Full_View (Parent_Type))
9415 then
9416 Parent_Base := Full_View (Parent_Type);
9417 else
9418 Parent_Base := Parent_Type;
9419 end if;
9421 Elmt := First_Elmt (Op_List);
9423 if Present (Generic_Actual) then
9424 Act_List := Collect_Primitive_Operations (Generic_Actual);
9425 Act_Elmt := First_Elmt (Act_List);
9426 else
9427 Act_Elmt := No_Elmt;
9428 end if;
9430 -- Literals are derived earlier in the process of building the
9431 -- derived type, and are skipped here.
9433 while Present (Elmt) loop
9434 Subp := Node (Elmt);
9436 if Ekind (Subp) /= E_Enumeration_Literal then
9437 if No (Generic_Actual) then
9438 Derive_Subprogram
9439 (New_Subp, Subp, Derived_Type, Parent_Base);
9441 else
9442 Derive_Subprogram (New_Subp, Subp,
9443 Derived_Type, Parent_Base, Node (Act_Elmt));
9444 Next_Elmt (Act_Elmt);
9445 end if;
9446 end if;
9448 Next_Elmt (Elmt);
9449 end loop;
9450 end Derive_Subprograms;
9452 --------------------------------
9453 -- Derived_Standard_Character --
9454 --------------------------------
9456 procedure Derived_Standard_Character
9457 (N : Node_Id;
9458 Parent_Type : Entity_Id;
9459 Derived_Type : Entity_Id)
9461 Loc : constant Source_Ptr := Sloc (N);
9462 Def : constant Node_Id := Type_Definition (N);
9463 Indic : constant Node_Id := Subtype_Indication (Def);
9464 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9465 Implicit_Base : constant Entity_Id :=
9466 Create_Itype
9467 (E_Enumeration_Type, N, Derived_Type, 'B');
9469 Lo : Node_Id;
9470 Hi : Node_Id;
9472 begin
9473 Discard_Node (Process_Subtype (Indic, N));
9475 Set_Etype (Implicit_Base, Parent_Base);
9476 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
9477 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
9479 Set_Is_Character_Type (Implicit_Base, True);
9480 Set_Has_Delayed_Freeze (Implicit_Base);
9482 -- The bounds of the implicit base are the bounds of the parent base.
9483 -- Note that their type is the parent base.
9485 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
9486 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
9488 Set_Scalar_Range (Implicit_Base,
9489 Make_Range (Loc,
9490 Low_Bound => Lo,
9491 High_Bound => Hi));
9493 Conditional_Delay (Derived_Type, Parent_Type);
9495 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
9496 Set_Etype (Derived_Type, Implicit_Base);
9497 Set_Size_Info (Derived_Type, Parent_Type);
9499 if Unknown_RM_Size (Derived_Type) then
9500 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9501 end if;
9503 Set_Is_Character_Type (Derived_Type, True);
9505 if Nkind (Indic) /= N_Subtype_Indication then
9507 -- If no explicit constraint, the bounds are those
9508 -- of the parent type.
9510 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
9511 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
9512 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
9513 end if;
9515 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
9517 -- Because the implicit base is used in the conversion of the bounds,
9518 -- we have to freeze it now. This is similar to what is done for
9519 -- numeric types, and it equally suspicious, but otherwise a non-
9520 -- static bound will have a reference to an unfrozen type, which is
9521 -- rejected by Gigi (???).
9523 Freeze_Before (N, Implicit_Base);
9524 end Derived_Standard_Character;
9526 ------------------------------
9527 -- Derived_Type_Declaration --
9528 ------------------------------
9530 procedure Derived_Type_Declaration
9531 (T : Entity_Id;
9532 N : Node_Id;
9533 Is_Completion : Boolean)
9535 Def : constant Node_Id := Type_Definition (N);
9536 Indic : constant Node_Id := Subtype_Indication (Def);
9537 Extension : constant Node_Id := Record_Extension_Part (Def);
9538 Parent_Type : Entity_Id;
9539 Parent_Scope : Entity_Id;
9540 Taggd : Boolean;
9542 begin
9543 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
9545 if Parent_Type = Any_Type
9546 or else Etype (Parent_Type) = Any_Type
9547 or else (Is_Class_Wide_Type (Parent_Type)
9548 and then Etype (Parent_Type) = T)
9549 then
9550 -- If Parent_Type is undefined or illegal, make new type into
9551 -- a subtype of Any_Type, and set a few attributes to prevent
9552 -- cascaded errors. If this is a self-definition, emit error now.
9554 if T = Parent_Type
9555 or else T = Etype (Parent_Type)
9556 then
9557 Error_Msg_N ("type cannot be used in its own definition", Indic);
9558 end if;
9560 Set_Ekind (T, Ekind (Parent_Type));
9561 Set_Etype (T, Any_Type);
9562 Set_Scalar_Range (T, Scalar_Range (Any_Type));
9564 if Is_Tagged_Type (T) then
9565 Set_Primitive_Operations (T, New_Elmt_List);
9566 end if;
9568 return;
9570 -- Ada 2005 (AI-231): Static check
9572 elsif Is_Access_Type (Parent_Type)
9573 and then Null_Exclusion_Present (Type_Definition (N))
9574 and then Can_Never_Be_Null (Parent_Type)
9575 then
9576 Error_Msg_N ("(Ada 2005) null exclusion not allowed if parent is "
9577 & "already non-null", Type_Definition (N));
9578 end if;
9580 -- Only composite types other than array types are allowed to have
9581 -- discriminants.
9583 if Present (Discriminant_Specifications (N))
9584 and then (Is_Elementary_Type (Parent_Type)
9585 or else Is_Array_Type (Parent_Type))
9586 and then not Error_Posted (N)
9587 then
9588 Error_Msg_N
9589 ("elementary or array type cannot have discriminants",
9590 Defining_Identifier (First (Discriminant_Specifications (N))));
9591 Set_Has_Discriminants (T, False);
9592 end if;
9594 -- In Ada 83, a derived type defined in a package specification cannot
9595 -- be used for further derivation until the end of its visible part.
9596 -- Note that derivation in the private part of the package is allowed.
9598 if Ada_Version = Ada_83
9599 and then Is_Derived_Type (Parent_Type)
9600 and then In_Visible_Part (Scope (Parent_Type))
9601 then
9602 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
9603 Error_Msg_N
9604 ("(Ada 83): premature use of type for derivation", Indic);
9605 end if;
9606 end if;
9608 -- Check for early use of incomplete or private type
9610 if Ekind (Parent_Type) = E_Void
9611 or else Ekind (Parent_Type) = E_Incomplete_Type
9612 then
9613 Error_Msg_N ("premature derivation of incomplete type", Indic);
9614 return;
9616 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
9617 and then not Is_Generic_Type (Parent_Type)
9618 and then not Is_Generic_Type (Root_Type (Parent_Type))
9619 and then not Is_Generic_Actual_Type (Parent_Type))
9620 or else Has_Private_Component (Parent_Type)
9621 then
9622 -- The ancestor type of a formal type can be incomplete, in which
9623 -- case only the operations of the partial view are available in
9624 -- the generic. Subsequent checks may be required when the full
9625 -- view is analyzed, to verify that derivation from a tagged type
9626 -- has an extension.
9628 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
9629 null;
9631 elsif No (Underlying_Type (Parent_Type))
9632 or else Has_Private_Component (Parent_Type)
9633 then
9634 Error_Msg_N
9635 ("premature derivation of derived or private type", Indic);
9637 -- Flag the type itself as being in error, this prevents some
9638 -- nasty problems with people looking at the malformed type.
9640 Set_Error_Posted (T);
9642 -- Check that within the immediate scope of an untagged partial
9643 -- view it's illegal to derive from the partial view if the
9644 -- full view is tagged. (7.3(7))
9646 -- We verify that the Parent_Type is a partial view by checking
9647 -- that it is not a Full_Type_Declaration (i.e. a private type or
9648 -- private extension declaration), to distinguish a partial view
9649 -- from a derivation from a private type which also appears as
9650 -- E_Private_Type.
9652 elsif Present (Full_View (Parent_Type))
9653 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
9654 and then not Is_Tagged_Type (Parent_Type)
9655 and then Is_Tagged_Type (Full_View (Parent_Type))
9656 then
9657 Parent_Scope := Scope (T);
9658 while Present (Parent_Scope)
9659 and then Parent_Scope /= Standard_Standard
9660 loop
9661 if Parent_Scope = Scope (Parent_Type) then
9662 Error_Msg_N
9663 ("premature derivation from type with tagged full view",
9664 Indic);
9665 end if;
9667 Parent_Scope := Scope (Parent_Scope);
9668 end loop;
9669 end if;
9670 end if;
9672 -- Check that form of derivation is appropriate
9674 Taggd := Is_Tagged_Type (Parent_Type);
9676 -- Perhaps the parent type should be changed to the class-wide type's
9677 -- specific type in this case to prevent cascading errors ???
9679 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
9680 Error_Msg_N ("parent type must not be a class-wide type", Indic);
9681 return;
9682 end if;
9684 if Present (Extension) and then not Taggd then
9685 Error_Msg_N
9686 ("type derived from untagged type cannot have extension", Indic);
9688 elsif No (Extension) and then Taggd then
9689 -- If this is within a private part (or body) of a generic
9690 -- instantiation then the derivation is allowed (the parent
9691 -- type can only appear tagged in this case if it's a generic
9692 -- actual type, since it would otherwise have been rejected
9693 -- in the analysis of the generic template).
9695 if not Is_Generic_Actual_Type (Parent_Type)
9696 or else In_Visible_Part (Scope (Parent_Type))
9697 then
9698 Error_Msg_N
9699 ("type derived from tagged type must have extension", Indic);
9700 end if;
9701 end if;
9703 Build_Derived_Type (N, Parent_Type, T, Is_Completion);
9704 end Derived_Type_Declaration;
9706 ----------------------------------
9707 -- Enumeration_Type_Declaration --
9708 ----------------------------------
9710 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
9711 Ev : Uint;
9712 L : Node_Id;
9713 R_Node : Node_Id;
9714 B_Node : Node_Id;
9716 begin
9717 -- Create identifier node representing lower bound
9719 B_Node := New_Node (N_Identifier, Sloc (Def));
9720 L := First (Literals (Def));
9721 Set_Chars (B_Node, Chars (L));
9722 Set_Entity (B_Node, L);
9723 Set_Etype (B_Node, T);
9724 Set_Is_Static_Expression (B_Node, True);
9726 R_Node := New_Node (N_Range, Sloc (Def));
9727 Set_Low_Bound (R_Node, B_Node);
9729 Set_Ekind (T, E_Enumeration_Type);
9730 Set_First_Literal (T, L);
9731 Set_Etype (T, T);
9732 Set_Is_Constrained (T);
9734 Ev := Uint_0;
9736 -- Loop through literals of enumeration type setting pos and rep values
9737 -- except that if the Ekind is already set, then it means that the
9738 -- literal was already constructed (case of a derived type declaration
9739 -- and we should not disturb the Pos and Rep values.
9741 while Present (L) loop
9742 if Ekind (L) /= E_Enumeration_Literal then
9743 Set_Ekind (L, E_Enumeration_Literal);
9744 Set_Enumeration_Pos (L, Ev);
9745 Set_Enumeration_Rep (L, Ev);
9746 Set_Is_Known_Valid (L, True);
9747 end if;
9749 Set_Etype (L, T);
9750 New_Overloaded_Entity (L);
9751 Generate_Definition (L);
9752 Set_Convention (L, Convention_Intrinsic);
9754 if Nkind (L) = N_Defining_Character_Literal then
9755 Set_Is_Character_Type (T, True);
9756 end if;
9758 Ev := Ev + 1;
9759 Next (L);
9760 end loop;
9762 -- Now create a node representing upper bound
9764 B_Node := New_Node (N_Identifier, Sloc (Def));
9765 Set_Chars (B_Node, Chars (Last (Literals (Def))));
9766 Set_Entity (B_Node, Last (Literals (Def)));
9767 Set_Etype (B_Node, T);
9768 Set_Is_Static_Expression (B_Node, True);
9770 Set_High_Bound (R_Node, B_Node);
9771 Set_Scalar_Range (T, R_Node);
9772 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
9773 Set_Enum_Esize (T);
9775 -- Set Discard_Names if configuration pragma set, or if there is
9776 -- a parameterless pragma in the current declarative region
9778 if Global_Discard_Names
9779 or else Discard_Names (Scope (T))
9780 then
9781 Set_Discard_Names (T);
9782 end if;
9784 -- Process end label if there is one
9786 if Present (Def) then
9787 Process_End_Label (Def, 'e', T);
9788 end if;
9789 end Enumeration_Type_Declaration;
9791 ---------------------------------
9792 -- Expand_To_Stored_Constraint --
9793 ---------------------------------
9795 function Expand_To_Stored_Constraint
9796 (Typ : Entity_Id;
9797 Constraint : Elist_Id) return Elist_Id
9799 Explicitly_Discriminated_Type : Entity_Id;
9800 Expansion : Elist_Id;
9801 Discriminant : Entity_Id;
9803 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
9804 -- Find the nearest type that actually specifies discriminants.
9806 ---------------------------------
9807 -- Type_With_Explicit_Discrims --
9808 ---------------------------------
9810 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
9811 Typ : constant E := Base_Type (Id);
9813 begin
9814 if Ekind (Typ) in Incomplete_Or_Private_Kind then
9815 if Present (Full_View (Typ)) then
9816 return Type_With_Explicit_Discrims (Full_View (Typ));
9817 end if;
9819 else
9820 if Has_Discriminants (Typ) then
9821 return Typ;
9822 end if;
9823 end if;
9825 if Etype (Typ) = Typ then
9826 return Empty;
9827 elsif Has_Discriminants (Typ) then
9828 return Typ;
9829 else
9830 return Type_With_Explicit_Discrims (Etype (Typ));
9831 end if;
9833 end Type_With_Explicit_Discrims;
9835 -- Start of processing for Expand_To_Stored_Constraint
9837 begin
9838 if No (Constraint)
9839 or else Is_Empty_Elmt_List (Constraint)
9840 then
9841 return No_Elist;
9842 end if;
9844 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
9846 if No (Explicitly_Discriminated_Type) then
9847 return No_Elist;
9848 end if;
9850 Expansion := New_Elmt_List;
9852 Discriminant :=
9853 First_Stored_Discriminant (Explicitly_Discriminated_Type);
9855 while Present (Discriminant) loop
9857 Append_Elmt (
9858 Get_Discriminant_Value (
9859 Discriminant, Explicitly_Discriminated_Type, Constraint),
9860 Expansion);
9862 Next_Stored_Discriminant (Discriminant);
9863 end loop;
9865 return Expansion;
9866 end Expand_To_Stored_Constraint;
9868 --------------------
9869 -- Find_Type_Name --
9870 --------------------
9872 function Find_Type_Name (N : Node_Id) return Entity_Id is
9873 Id : constant Entity_Id := Defining_Identifier (N);
9874 Prev : Entity_Id;
9875 New_Id : Entity_Id;
9876 Prev_Par : Node_Id;
9878 begin
9879 -- Find incomplete declaration, if some was given.
9881 Prev := Current_Entity_In_Scope (Id);
9883 if Present (Prev) then
9885 -- Previous declaration exists. Error if not incomplete/private case
9886 -- except if previous declaration is implicit, etc. Enter_Name will
9887 -- emit error if appropriate.
9889 Prev_Par := Parent (Prev);
9891 if not Is_Incomplete_Or_Private_Type (Prev) then
9892 Enter_Name (Id);
9893 New_Id := Id;
9895 elsif Nkind (N) /= N_Full_Type_Declaration
9896 and then Nkind (N) /= N_Task_Type_Declaration
9897 and then Nkind (N) /= N_Protected_Type_Declaration
9898 then
9899 -- Completion must be a full type declarations (RM 7.3(4))
9901 Error_Msg_Sloc := Sloc (Prev);
9902 Error_Msg_NE ("invalid completion of }", Id, Prev);
9904 -- Set scope of Id to avoid cascaded errors. Entity is never
9905 -- examined again, except when saving globals in generics.
9907 Set_Scope (Id, Current_Scope);
9908 New_Id := Id;
9910 -- Case of full declaration of incomplete type
9912 elsif Ekind (Prev) = E_Incomplete_Type then
9914 -- Indicate that the incomplete declaration has a matching
9915 -- full declaration. The defining occurrence of the incomplete
9916 -- declaration remains the visible one, and the procedure
9917 -- Get_Full_View dereferences it whenever the type is used.
9919 if Present (Full_View (Prev)) then
9920 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
9921 end if;
9923 Set_Full_View (Prev, Id);
9924 Append_Entity (Id, Current_Scope);
9925 Set_Is_Public (Id, Is_Public (Prev));
9926 Set_Is_Internal (Id);
9927 New_Id := Prev;
9929 -- Case of full declaration of private type
9931 else
9932 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
9933 if Etype (Prev) /= Prev then
9935 -- Prev is a private subtype or a derived type, and needs
9936 -- no completion.
9938 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
9939 New_Id := Id;
9941 elsif Ekind (Prev) = E_Private_Type
9942 and then
9943 (Nkind (N) = N_Task_Type_Declaration
9944 or else Nkind (N) = N_Protected_Type_Declaration)
9945 then
9946 Error_Msg_N
9947 ("completion of nonlimited type cannot be limited", N);
9948 end if;
9950 elsif Nkind (N) /= N_Full_Type_Declaration
9951 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
9952 then
9953 Error_Msg_N ("full view of private extension must be"
9954 & " an extension", N);
9956 elsif not (Abstract_Present (Parent (Prev)))
9957 and then Abstract_Present (Type_Definition (N))
9958 then
9959 Error_Msg_N ("full view of non-abstract extension cannot"
9960 & " be abstract", N);
9961 end if;
9963 if not In_Private_Part (Current_Scope) then
9964 Error_Msg_N
9965 ("declaration of full view must appear in private part", N);
9966 end if;
9968 Copy_And_Swap (Prev, Id);
9969 Set_Has_Private_Declaration (Prev);
9970 Set_Has_Private_Declaration (Id);
9972 -- If no error, propagate freeze_node from private to full view.
9973 -- It may have been generated for an early operational item.
9975 if Present (Freeze_Node (Id))
9976 and then Serious_Errors_Detected = 0
9977 and then No (Full_View (Id))
9978 then
9979 Set_Freeze_Node (Prev, Freeze_Node (Id));
9980 Set_Freeze_Node (Id, Empty);
9981 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
9982 end if;
9984 Set_Full_View (Id, Prev);
9985 New_Id := Prev;
9986 end if;
9988 -- Verify that full declaration conforms to incomplete one
9990 if Is_Incomplete_Or_Private_Type (Prev)
9991 and then Present (Discriminant_Specifications (Prev_Par))
9992 then
9993 if Present (Discriminant_Specifications (N)) then
9994 if Ekind (Prev) = E_Incomplete_Type then
9995 Check_Discriminant_Conformance (N, Prev, Prev);
9996 else
9997 Check_Discriminant_Conformance (N, Prev, Id);
9998 end if;
10000 else
10001 Error_Msg_N
10002 ("missing discriminants in full type declaration", N);
10004 -- To avoid cascaded errors on subsequent use, share the
10005 -- discriminants of the partial view.
10007 Set_Discriminant_Specifications (N,
10008 Discriminant_Specifications (Prev_Par));
10009 end if;
10010 end if;
10012 -- A prior untagged private type can have an associated
10013 -- class-wide type due to use of the class attribute,
10014 -- and in this case also the full type is required to
10015 -- be tagged.
10017 if Is_Type (Prev)
10018 and then (Is_Tagged_Type (Prev)
10019 or else Present (Class_Wide_Type (Prev)))
10020 then
10021 -- The full declaration is either a tagged record or an
10022 -- extension otherwise this is an error
10024 if Nkind (Type_Definition (N)) = N_Record_Definition then
10025 if not Tagged_Present (Type_Definition (N)) then
10026 Error_Msg_NE
10027 ("full declaration of } must be tagged", Prev, Id);
10028 Set_Is_Tagged_Type (Id);
10029 Set_Primitive_Operations (Id, New_Elmt_List);
10030 end if;
10032 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
10033 if No (Record_Extension_Part (Type_Definition (N))) then
10034 Error_Msg_NE (
10035 "full declaration of } must be a record extension",
10036 Prev, Id);
10037 Set_Is_Tagged_Type (Id);
10038 Set_Primitive_Operations (Id, New_Elmt_List);
10039 end if;
10041 else
10042 Error_Msg_NE
10043 ("full declaration of } must be a tagged type", Prev, Id);
10045 end if;
10046 end if;
10048 return New_Id;
10050 else
10051 -- New type declaration
10053 Enter_Name (Id);
10054 return Id;
10055 end if;
10056 end Find_Type_Name;
10058 -------------------------
10059 -- Find_Type_Of_Object --
10060 -------------------------
10062 function Find_Type_Of_Object
10063 (Obj_Def : Node_Id;
10064 Related_Nod : Node_Id) return Entity_Id
10066 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
10067 P : Node_Id := Parent (Obj_Def);
10068 T : Entity_Id;
10069 Nam : Name_Id;
10071 begin
10072 -- If the parent is a component_definition node we climb to the
10073 -- component_declaration node
10075 if Nkind (P) = N_Component_Definition then
10076 P := Parent (P);
10077 end if;
10079 -- Case of an anonymous array subtype
10081 if Def_Kind = N_Constrained_Array_Definition
10082 or else Def_Kind = N_Unconstrained_Array_Definition
10083 then
10084 T := Empty;
10085 Array_Type_Declaration (T, Obj_Def);
10087 -- Create an explicit subtype whenever possible.
10089 elsif Nkind (P) /= N_Component_Declaration
10090 and then Def_Kind = N_Subtype_Indication
10091 then
10092 -- Base name of subtype on object name, which will be unique in
10093 -- the current scope.
10095 -- If this is a duplicate declaration, return base type, to avoid
10096 -- generating duplicate anonymous types.
10098 if Error_Posted (P) then
10099 Analyze (Subtype_Mark (Obj_Def));
10100 return Entity (Subtype_Mark (Obj_Def));
10101 end if;
10103 Nam :=
10104 New_External_Name
10105 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
10107 T := Make_Defining_Identifier (Sloc (P), Nam);
10109 Insert_Action (Obj_Def,
10110 Make_Subtype_Declaration (Sloc (P),
10111 Defining_Identifier => T,
10112 Subtype_Indication => Relocate_Node (Obj_Def)));
10114 -- This subtype may need freezing, and this will not be done
10115 -- automatically if the object declaration is not in a
10116 -- declarative part. Since this is an object declaration, the
10117 -- type cannot always be frozen here. Deferred constants do not
10118 -- freeze their type (which often enough will be private).
10120 if Nkind (P) = N_Object_Declaration
10121 and then Constant_Present (P)
10122 and then No (Expression (P))
10123 then
10124 null;
10126 else
10127 Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
10128 end if;
10130 else
10131 T := Process_Subtype (Obj_Def, Related_Nod);
10132 end if;
10134 return T;
10135 end Find_Type_Of_Object;
10137 --------------------------------
10138 -- Find_Type_Of_Subtype_Indic --
10139 --------------------------------
10141 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
10142 Typ : Entity_Id;
10144 begin
10145 -- Case of subtype mark with a constraint
10147 if Nkind (S) = N_Subtype_Indication then
10148 Find_Type (Subtype_Mark (S));
10149 Typ := Entity (Subtype_Mark (S));
10151 if not
10152 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
10153 then
10154 Error_Msg_N
10155 ("incorrect constraint for this kind of type", Constraint (S));
10156 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
10157 end if;
10159 -- Otherwise we have a subtype mark without a constraint
10161 elsif Error_Posted (S) then
10162 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
10163 return Any_Type;
10165 else
10166 Find_Type (S);
10167 Typ := Entity (S);
10168 end if;
10170 if Typ = Standard_Wide_Character
10171 or else Typ = Standard_Wide_String
10172 then
10173 Check_Restriction (No_Wide_Characters, S);
10174 end if;
10176 return Typ;
10177 end Find_Type_Of_Subtype_Indic;
10179 -------------------------------------
10180 -- Floating_Point_Type_Declaration --
10181 -------------------------------------
10183 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
10184 Digs : constant Node_Id := Digits_Expression (Def);
10185 Digs_Val : Uint;
10186 Base_Typ : Entity_Id;
10187 Implicit_Base : Entity_Id;
10188 Bound : Node_Id;
10190 function Can_Derive_From (E : Entity_Id) return Boolean;
10191 -- Find if given digits value allows derivation from specified type
10193 ---------------------
10194 -- Can_Derive_From --
10195 ---------------------
10197 function Can_Derive_From (E : Entity_Id) return Boolean is
10198 Spec : constant Entity_Id := Real_Range_Specification (Def);
10200 begin
10201 if Digs_Val > Digits_Value (E) then
10202 return False;
10203 end if;
10205 if Present (Spec) then
10206 if Expr_Value_R (Type_Low_Bound (E)) >
10207 Expr_Value_R (Low_Bound (Spec))
10208 then
10209 return False;
10210 end if;
10212 if Expr_Value_R (Type_High_Bound (E)) <
10213 Expr_Value_R (High_Bound (Spec))
10214 then
10215 return False;
10216 end if;
10217 end if;
10219 return True;
10220 end Can_Derive_From;
10222 -- Start of processing for Floating_Point_Type_Declaration
10224 begin
10225 Check_Restriction (No_Floating_Point, Def);
10227 -- Create an implicit base type
10229 Implicit_Base :=
10230 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
10232 -- Analyze and verify digits value
10234 Analyze_And_Resolve (Digs, Any_Integer);
10235 Check_Digits_Expression (Digs);
10236 Digs_Val := Expr_Value (Digs);
10238 -- Process possible range spec and find correct type to derive from
10240 Process_Real_Range_Specification (Def);
10242 if Can_Derive_From (Standard_Short_Float) then
10243 Base_Typ := Standard_Short_Float;
10244 elsif Can_Derive_From (Standard_Float) then
10245 Base_Typ := Standard_Float;
10246 elsif Can_Derive_From (Standard_Long_Float) then
10247 Base_Typ := Standard_Long_Float;
10248 elsif Can_Derive_From (Standard_Long_Long_Float) then
10249 Base_Typ := Standard_Long_Long_Float;
10251 -- If we can't derive from any existing type, use long_long_float
10252 -- and give appropriate message explaining the problem.
10254 else
10255 Base_Typ := Standard_Long_Long_Float;
10257 if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
10258 Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
10259 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
10261 else
10262 Error_Msg_N
10263 ("range too large for any predefined type",
10264 Real_Range_Specification (Def));
10265 end if;
10266 end if;
10268 -- If there are bounds given in the declaration use them as the bounds
10269 -- of the type, otherwise use the bounds of the predefined base type
10270 -- that was chosen based on the Digits value.
10272 if Present (Real_Range_Specification (Def)) then
10273 Set_Scalar_Range (T, Real_Range_Specification (Def));
10274 Set_Is_Constrained (T);
10276 -- The bounds of this range must be converted to machine numbers
10277 -- in accordance with RM 4.9(38).
10279 Bound := Type_Low_Bound (T);
10281 if Nkind (Bound) = N_Real_Literal then
10282 Set_Realval
10283 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
10284 Set_Is_Machine_Number (Bound);
10285 end if;
10287 Bound := Type_High_Bound (T);
10289 if Nkind (Bound) = N_Real_Literal then
10290 Set_Realval
10291 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
10292 Set_Is_Machine_Number (Bound);
10293 end if;
10295 else
10296 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
10297 end if;
10299 -- Complete definition of implicit base and declared first subtype
10301 Set_Etype (Implicit_Base, Base_Typ);
10303 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
10304 Set_Size_Info (Implicit_Base, (Base_Typ));
10305 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
10306 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
10307 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
10308 Set_Vax_Float (Implicit_Base, Vax_Float (Base_Typ));
10310 Set_Ekind (T, E_Floating_Point_Subtype);
10311 Set_Etype (T, Implicit_Base);
10313 Set_Size_Info (T, (Implicit_Base));
10314 Set_RM_Size (T, RM_Size (Implicit_Base));
10315 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
10316 Set_Digits_Value (T, Digs_Val);
10318 end Floating_Point_Type_Declaration;
10320 ----------------------------
10321 -- Get_Discriminant_Value --
10322 ----------------------------
10324 -- This is the situation...
10326 -- There is a non-derived type
10328 -- type T0 (Dx, Dy, Dz...)
10330 -- There are zero or more levels of derivation, with each
10331 -- derivation either purely inheriting the discriminants, or
10332 -- defining its own.
10334 -- type Ti is new Ti-1
10335 -- or
10336 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
10337 -- or
10338 -- subtype Ti is ...
10340 -- The subtype issue is avoided by the use of
10341 -- Original_Record_Component, and the fact that derived subtypes
10342 -- also derive the constraints.
10344 -- This chain leads back from
10346 -- Typ_For_Constraint
10348 -- Typ_For_Constraint has discriminants, and the value for each
10349 -- discriminant is given by its corresponding Elmt of Constraints.
10351 -- Discriminant is some discriminant in this hierarchy.
10353 -- We need to return its value.
10355 -- We do this by recursively searching each level, and looking for
10356 -- Discriminant. Once we get to the bottom, we start backing up
10357 -- returning the value for it which may in turn be a discriminant
10358 -- further up, so on the backup we continue the substitution.
10360 function Get_Discriminant_Value
10361 (Discriminant : Entity_Id;
10362 Typ_For_Constraint : Entity_Id;
10363 Constraint : Elist_Id) return Node_Id
10365 function Search_Derivation_Levels
10366 (Ti : Entity_Id;
10367 Discrim_Values : Elist_Id;
10368 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
10369 -- This is the routine that performs the recursive search of levels
10370 -- as described above.
10372 ------------------------------
10373 -- Search_Derivation_Levels --
10374 ------------------------------
10376 function Search_Derivation_Levels
10377 (Ti : Entity_Id;
10378 Discrim_Values : Elist_Id;
10379 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
10381 Assoc : Elmt_Id;
10382 Disc : Entity_Id;
10383 Result : Node_Or_Entity_Id;
10384 Result_Entity : Node_Id;
10386 begin
10387 -- If inappropriate type, return Error, this happens only in
10388 -- cascaded error situations, and we want to avoid a blow up.
10390 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
10391 return Error;
10392 end if;
10394 -- Look deeper if possible. Use Stored_Constraints only for
10395 -- untagged types. For tagged types use the given constraint.
10396 -- This asymmetry needs explanation???
10398 if not Stored_Discrim_Values
10399 and then Present (Stored_Constraint (Ti))
10400 and then not Is_Tagged_Type (Ti)
10401 then
10402 Result :=
10403 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
10404 else
10405 declare
10406 Td : constant Entity_Id := Etype (Ti);
10408 begin
10409 if Td = Ti then
10410 Result := Discriminant;
10412 else
10413 if Present (Stored_Constraint (Ti)) then
10414 Result :=
10415 Search_Derivation_Levels
10416 (Td, Stored_Constraint (Ti), True);
10417 else
10418 Result :=
10419 Search_Derivation_Levels
10420 (Td, Discrim_Values, Stored_Discrim_Values);
10421 end if;
10422 end if;
10423 end;
10424 end if;
10426 -- Extra underlying places to search, if not found above. For
10427 -- concurrent types, the relevant discriminant appears in the
10428 -- corresponding record. For a type derived from a private type
10429 -- without discriminant, the full view inherits the discriminants
10430 -- of the full view of the parent.
10432 if Result = Discriminant then
10433 if Is_Concurrent_Type (Ti)
10434 and then Present (Corresponding_Record_Type (Ti))
10435 then
10436 Result :=
10437 Search_Derivation_Levels (
10438 Corresponding_Record_Type (Ti),
10439 Discrim_Values,
10440 Stored_Discrim_Values);
10442 elsif Is_Private_Type (Ti)
10443 and then not Has_Discriminants (Ti)
10444 and then Present (Full_View (Ti))
10445 and then Etype (Full_View (Ti)) /= Ti
10446 then
10447 Result :=
10448 Search_Derivation_Levels (
10449 Full_View (Ti),
10450 Discrim_Values,
10451 Stored_Discrim_Values);
10452 end if;
10453 end if;
10455 -- If Result is not a (reference to a) discriminant,
10456 -- return it, otherwise set Result_Entity to the discriminant.
10458 if Nkind (Result) = N_Defining_Identifier then
10460 pragma Assert (Result = Discriminant);
10462 Result_Entity := Result;
10464 else
10465 if not Denotes_Discriminant (Result) then
10466 return Result;
10467 end if;
10469 Result_Entity := Entity (Result);
10470 end if;
10472 -- See if this level of derivation actually has discriminants
10473 -- because tagged derivations can add them, hence the lower
10474 -- levels need not have any.
10476 if not Has_Discriminants (Ti) then
10477 return Result;
10478 end if;
10480 -- Scan Ti's discriminants for Result_Entity,
10481 -- and return its corresponding value, if any.
10483 Result_Entity := Original_Record_Component (Result_Entity);
10485 Assoc := First_Elmt (Discrim_Values);
10487 if Stored_Discrim_Values then
10488 Disc := First_Stored_Discriminant (Ti);
10489 else
10490 Disc := First_Discriminant (Ti);
10491 end if;
10493 while Present (Disc) loop
10495 pragma Assert (Present (Assoc));
10497 if Original_Record_Component (Disc) = Result_Entity then
10498 return Node (Assoc);
10499 end if;
10501 Next_Elmt (Assoc);
10503 if Stored_Discrim_Values then
10504 Next_Stored_Discriminant (Disc);
10505 else
10506 Next_Discriminant (Disc);
10507 end if;
10508 end loop;
10510 -- Could not find it
10512 return Result;
10513 end Search_Derivation_Levels;
10515 Result : Node_Or_Entity_Id;
10517 -- Start of processing for Get_Discriminant_Value
10519 begin
10520 -- ??? this routine is a gigantic mess and will be deleted.
10521 -- for the time being just test for the trivial case before calling
10522 -- recurse.
10524 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
10525 declare
10526 D : Entity_Id := First_Discriminant (Typ_For_Constraint);
10527 E : Elmt_Id := First_Elmt (Constraint);
10528 begin
10529 while Present (D) loop
10530 if Chars (D) = Chars (Discriminant) then
10531 return Node (E);
10532 end if;
10534 Next_Discriminant (D);
10535 Next_Elmt (E);
10536 end loop;
10537 end;
10538 end if;
10540 Result := Search_Derivation_Levels
10541 (Typ_For_Constraint, Constraint, False);
10543 -- ??? hack to disappear when this routine is gone
10545 if Nkind (Result) = N_Defining_Identifier then
10546 declare
10547 D : Entity_Id := First_Discriminant (Typ_For_Constraint);
10548 E : Elmt_Id := First_Elmt (Constraint);
10550 begin
10551 while Present (D) loop
10552 if Corresponding_Discriminant (D) = Discriminant then
10553 return Node (E);
10554 end if;
10556 Next_Discriminant (D);
10557 Next_Elmt (E);
10558 end loop;
10559 end;
10560 end if;
10562 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
10563 return Result;
10564 end Get_Discriminant_Value;
10566 --------------------------
10567 -- Has_Range_Constraint --
10568 --------------------------
10570 function Has_Range_Constraint (N : Node_Id) return Boolean is
10571 C : constant Node_Id := Constraint (N);
10573 begin
10574 if Nkind (C) = N_Range_Constraint then
10575 return True;
10577 elsif Nkind (C) = N_Digits_Constraint then
10578 return
10579 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
10580 or else
10581 Present (Range_Constraint (C));
10583 elsif Nkind (C) = N_Delta_Constraint then
10584 return Present (Range_Constraint (C));
10586 else
10587 return False;
10588 end if;
10589 end Has_Range_Constraint;
10591 ------------------------
10592 -- Inherit_Components --
10593 ------------------------
10595 function Inherit_Components
10596 (N : Node_Id;
10597 Parent_Base : Entity_Id;
10598 Derived_Base : Entity_Id;
10599 Is_Tagged : Boolean;
10600 Inherit_Discr : Boolean;
10601 Discs : Elist_Id) return Elist_Id
10603 Assoc_List : constant Elist_Id := New_Elmt_List;
10605 procedure Inherit_Component
10606 (Old_C : Entity_Id;
10607 Plain_Discrim : Boolean := False;
10608 Stored_Discrim : Boolean := False);
10609 -- Inherits component Old_C from Parent_Base to the Derived_Base.
10610 -- If Plain_Discrim is True, Old_C is a discriminant.
10611 -- If Stored_Discrim is True, Old_C is a stored discriminant.
10612 -- If they are both false then Old_C is a regular component.
10614 -----------------------
10615 -- Inherit_Component --
10616 -----------------------
10618 procedure Inherit_Component
10619 (Old_C : Entity_Id;
10620 Plain_Discrim : Boolean := False;
10621 Stored_Discrim : Boolean := False)
10623 New_C : constant Entity_Id := New_Copy (Old_C);
10625 Discrim : Entity_Id;
10626 Corr_Discrim : Entity_Id;
10628 begin
10629 pragma Assert (not Is_Tagged or else not Stored_Discrim);
10631 Set_Parent (New_C, Parent (Old_C));
10633 -- Regular discriminants and components must be inserted
10634 -- in the scope of the Derived_Base. Do it here.
10636 if not Stored_Discrim then
10637 Enter_Name (New_C);
10638 end if;
10640 -- For tagged types the Original_Record_Component must point to
10641 -- whatever this field was pointing to in the parent type. This has
10642 -- already been achieved by the call to New_Copy above.
10644 if not Is_Tagged then
10645 Set_Original_Record_Component (New_C, New_C);
10646 end if;
10648 -- If we have inherited a component then see if its Etype contains
10649 -- references to Parent_Base discriminants. In this case, replace
10650 -- these references with the constraints given in Discs. We do not
10651 -- do this for the partial view of private types because this is
10652 -- not needed (only the components of the full view will be used
10653 -- for code generation) and cause problem. We also avoid this
10654 -- transformation in some error situations.
10656 if Ekind (New_C) = E_Component then
10657 if (Is_Private_Type (Derived_Base)
10658 and then not Is_Generic_Type (Derived_Base))
10659 or else (Is_Empty_Elmt_List (Discs)
10660 and then not Expander_Active)
10661 then
10662 Set_Etype (New_C, Etype (Old_C));
10663 else
10664 Set_Etype (New_C, Constrain_Component_Type (Etype (Old_C),
10665 Derived_Base, N, Parent_Base, Discs));
10666 end if;
10667 end if;
10669 -- In derived tagged types it is illegal to reference a non
10670 -- discriminant component in the parent type. To catch this, mark
10671 -- these components with an Ekind of E_Void. This will be reset in
10672 -- Record_Type_Definition after processing the record extension of
10673 -- the derived type.
10675 if Is_Tagged and then Ekind (New_C) = E_Component then
10676 Set_Ekind (New_C, E_Void);
10677 end if;
10679 if Plain_Discrim then
10680 Set_Corresponding_Discriminant (New_C, Old_C);
10681 Build_Discriminal (New_C);
10683 -- If we are explicitly inheriting a stored discriminant it will be
10684 -- completely hidden.
10686 elsif Stored_Discrim then
10687 Set_Corresponding_Discriminant (New_C, Empty);
10688 Set_Discriminal (New_C, Empty);
10689 Set_Is_Completely_Hidden (New_C);
10691 -- Set the Original_Record_Component of each discriminant in the
10692 -- derived base to point to the corresponding stored that we just
10693 -- created.
10695 Discrim := First_Discriminant (Derived_Base);
10696 while Present (Discrim) loop
10697 Corr_Discrim := Corresponding_Discriminant (Discrim);
10699 -- Corr_Discrimm could be missing in an error situation.
10701 if Present (Corr_Discrim)
10702 and then Original_Record_Component (Corr_Discrim) = Old_C
10703 then
10704 Set_Original_Record_Component (Discrim, New_C);
10705 end if;
10707 Next_Discriminant (Discrim);
10708 end loop;
10710 Append_Entity (New_C, Derived_Base);
10711 end if;
10713 if not Is_Tagged then
10714 Append_Elmt (Old_C, Assoc_List);
10715 Append_Elmt (New_C, Assoc_List);
10716 end if;
10717 end Inherit_Component;
10719 -- Variables local to Inherit_Components.
10721 Loc : constant Source_Ptr := Sloc (N);
10723 Parent_Discrim : Entity_Id;
10724 Stored_Discrim : Entity_Id;
10725 D : Entity_Id;
10727 Component : Entity_Id;
10729 -- Start of processing for Inherit_Components
10731 begin
10732 if not Is_Tagged then
10733 Append_Elmt (Parent_Base, Assoc_List);
10734 Append_Elmt (Derived_Base, Assoc_List);
10735 end if;
10737 -- Inherit parent discriminants if needed.
10739 if Inherit_Discr then
10740 Parent_Discrim := First_Discriminant (Parent_Base);
10741 while Present (Parent_Discrim) loop
10742 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
10743 Next_Discriminant (Parent_Discrim);
10744 end loop;
10745 end if;
10747 -- Create explicit stored discrims for untagged types when necessary.
10749 if not Has_Unknown_Discriminants (Derived_Base)
10750 and then Has_Discriminants (Parent_Base)
10751 and then not Is_Tagged
10752 and then
10753 (not Inherit_Discr
10754 or else First_Discriminant (Parent_Base) /=
10755 First_Stored_Discriminant (Parent_Base))
10756 then
10757 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
10758 while Present (Stored_Discrim) loop
10759 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
10760 Next_Stored_Discriminant (Stored_Discrim);
10761 end loop;
10762 end if;
10764 -- See if we can apply the second transformation for derived types, as
10765 -- explained in point 6. in the comments above Build_Derived_Record_Type
10766 -- This is achieved by appending Derived_Base discriminants into
10767 -- Discs, which has the side effect of returning a non empty Discs
10768 -- list to the caller of Inherit_Components, which is what we want.
10769 -- This must be done for private derived types if there are explicit
10770 -- stored discriminants, to ensure that we can retrieve the values of
10771 -- the constraints provided in the ancestors.
10773 if Inherit_Discr
10774 and then Is_Empty_Elmt_List (Discs)
10775 and then Present (First_Discriminant (Derived_Base))
10776 and then
10777 (not Is_Private_Type (Derived_Base)
10778 or else Is_Completely_Hidden
10779 (First_Stored_Discriminant (Derived_Base))
10780 or else Is_Generic_Type (Derived_Base))
10781 then
10782 D := First_Discriminant (Derived_Base);
10783 while Present (D) loop
10784 Append_Elmt (New_Reference_To (D, Loc), Discs);
10785 Next_Discriminant (D);
10786 end loop;
10787 end if;
10789 -- Finally, inherit non-discriminant components unless they are not
10790 -- visible because defined or inherited from the full view of the
10791 -- parent. Don't inherit the _parent field of the parent type.
10793 Component := First_Entity (Parent_Base);
10794 while Present (Component) loop
10795 if Ekind (Component) /= E_Component
10796 or else Chars (Component) = Name_uParent
10797 then
10798 null;
10800 -- If the derived type is within the parent type's declarative
10801 -- region, then the components can still be inherited even though
10802 -- they aren't visible at this point. This can occur for cases
10803 -- such as within public child units where the components must
10804 -- become visible upon entering the child unit's private part.
10806 elsif not Is_Visible_Component (Component)
10807 and then not In_Open_Scopes (Scope (Parent_Base))
10808 then
10809 null;
10811 elsif Ekind (Derived_Base) = E_Private_Type
10812 or else Ekind (Derived_Base) = E_Limited_Private_Type
10813 then
10814 null;
10816 else
10817 Inherit_Component (Component);
10818 end if;
10820 Next_Entity (Component);
10821 end loop;
10823 -- For tagged derived types, inherited discriminants cannot be used in
10824 -- component declarations of the record extension part. To achieve this
10825 -- we mark the inherited discriminants as not visible.
10827 if Is_Tagged and then Inherit_Discr then
10828 D := First_Discriminant (Derived_Base);
10829 while Present (D) loop
10830 Set_Is_Immediately_Visible (D, False);
10831 Next_Discriminant (D);
10832 end loop;
10833 end if;
10835 return Assoc_List;
10836 end Inherit_Components;
10838 ------------------------------
10839 -- Is_Valid_Constraint_Kind --
10840 ------------------------------
10842 function Is_Valid_Constraint_Kind
10843 (T_Kind : Type_Kind;
10844 Constraint_Kind : Node_Kind) return Boolean
10846 begin
10847 case T_Kind is
10849 when Enumeration_Kind |
10850 Integer_Kind =>
10851 return Constraint_Kind = N_Range_Constraint;
10853 when Decimal_Fixed_Point_Kind =>
10854 return
10855 Constraint_Kind = N_Digits_Constraint
10856 or else
10857 Constraint_Kind = N_Range_Constraint;
10859 when Ordinary_Fixed_Point_Kind =>
10860 return
10861 Constraint_Kind = N_Delta_Constraint
10862 or else
10863 Constraint_Kind = N_Range_Constraint;
10865 when Float_Kind =>
10866 return
10867 Constraint_Kind = N_Digits_Constraint
10868 or else
10869 Constraint_Kind = N_Range_Constraint;
10871 when Access_Kind |
10872 Array_Kind |
10873 E_Record_Type |
10874 E_Record_Subtype |
10875 Class_Wide_Kind |
10876 E_Incomplete_Type |
10877 Private_Kind |
10878 Concurrent_Kind =>
10879 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
10881 when others =>
10882 return True; -- Error will be detected later.
10883 end case;
10885 end Is_Valid_Constraint_Kind;
10887 --------------------------
10888 -- Is_Visible_Component --
10889 --------------------------
10891 function Is_Visible_Component (C : Entity_Id) return Boolean is
10892 Original_Comp : Entity_Id := Empty;
10893 Original_Scope : Entity_Id;
10894 Type_Scope : Entity_Id;
10896 function Is_Local_Type (Typ : Entity_Id) return Boolean;
10897 -- Check whether parent type of inherited component is declared
10898 -- locally, possibly within a nested package or instance. The
10899 -- current scope is the derived record itself.
10901 -------------------
10902 -- Is_Local_Type --
10903 -------------------
10905 function Is_Local_Type (Typ : Entity_Id) return Boolean is
10906 Scop : Entity_Id := Scope (Typ);
10908 begin
10909 while Present (Scop)
10910 and then Scop /= Standard_Standard
10911 loop
10912 if Scop = Scope (Current_Scope) then
10913 return True;
10914 end if;
10916 Scop := Scope (Scop);
10917 end loop;
10918 return False;
10919 end Is_Local_Type;
10921 -- Start of processing for Is_Visible_Component
10923 begin
10924 if Ekind (C) = E_Component
10925 or else Ekind (C) = E_Discriminant
10926 then
10927 Original_Comp := Original_Record_Component (C);
10928 end if;
10930 if No (Original_Comp) then
10932 -- Premature usage, or previous error
10934 return False;
10936 else
10937 Original_Scope := Scope (Original_Comp);
10938 Type_Scope := Scope (Base_Type (Scope (C)));
10939 end if;
10941 -- This test only concerns tagged types
10943 if not Is_Tagged_Type (Original_Scope) then
10944 return True;
10946 -- If it is _Parent or _Tag, there is no visibility issue
10948 elsif not Comes_From_Source (Original_Comp) then
10949 return True;
10951 -- If we are in the body of an instantiation, the component is
10952 -- visible even when the parent type (possibly defined in an
10953 -- enclosing unit or in a parent unit) might not.
10955 elsif In_Instance_Body then
10956 return True;
10958 -- Discriminants are always visible.
10960 elsif Ekind (Original_Comp) = E_Discriminant
10961 and then not Has_Unknown_Discriminants (Original_Scope)
10962 then
10963 return True;
10965 -- If the component has been declared in an ancestor which is
10966 -- currently a private type, then it is not visible. The same
10967 -- applies if the component's containing type is not in an
10968 -- open scope and the original component's enclosing type
10969 -- is a visible full type of a private type (which can occur
10970 -- in cases where an attempt is being made to reference a
10971 -- component in a sibling package that is inherited from a
10972 -- visible component of a type in an ancestor package; the
10973 -- component in the sibling package should not be visible
10974 -- even though the component it inherited from is visible).
10975 -- This does not apply however in the case where the scope
10976 -- of the type is a private child unit, or when the parent
10977 -- comes from a local package in which the ancestor is
10978 -- currently visible. The latter suppression of visibility
10979 -- is needed for cases that are tested in B730006.
10981 elsif Is_Private_Type (Original_Scope)
10982 or else
10983 (not Is_Private_Descendant (Type_Scope)
10984 and then not In_Open_Scopes (Type_Scope)
10985 and then Has_Private_Declaration (Original_Scope))
10986 then
10987 -- If the type derives from an entity in a formal package, there
10988 -- are no additional visible components.
10990 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
10991 N_Formal_Package_Declaration
10992 then
10993 return False;
10995 -- if we are not in the private part of the current package, there
10996 -- are no additional visible components.
10998 elsif Ekind (Scope (Current_Scope)) = E_Package
10999 and then not In_Private_Part (Scope (Current_Scope))
11000 then
11001 return False;
11002 else
11003 return
11004 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
11005 and then Is_Local_Type (Type_Scope);
11006 end if;
11008 -- There is another weird way in which a component may be invisible
11009 -- when the private and the full view are not derived from the same
11010 -- ancestor. Here is an example :
11012 -- type A1 is tagged record F1 : integer; end record;
11013 -- type A2 is new A1 with record F2 : integer; end record;
11014 -- type T is new A1 with private;
11015 -- private
11016 -- type T is new A2 with null record;
11018 -- In this case, the full view of T inherits F1 and F2 but the
11019 -- private view inherits only F1
11021 else
11022 declare
11023 Ancestor : Entity_Id := Scope (C);
11025 begin
11026 loop
11027 if Ancestor = Original_Scope then
11028 return True;
11029 elsif Ancestor = Etype (Ancestor) then
11030 return False;
11031 end if;
11033 Ancestor := Etype (Ancestor);
11034 end loop;
11036 return True;
11037 end;
11038 end if;
11039 end Is_Visible_Component;
11041 --------------------------
11042 -- Make_Class_Wide_Type --
11043 --------------------------
11045 procedure Make_Class_Wide_Type (T : Entity_Id) is
11046 CW_Type : Entity_Id;
11047 CW_Name : Name_Id;
11048 Next_E : Entity_Id;
11050 begin
11051 -- The class wide type can have been defined by the partial view in
11052 -- which case everything is already done
11054 if Present (Class_Wide_Type (T)) then
11055 return;
11056 end if;
11058 CW_Type :=
11059 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
11061 -- Inherit root type characteristics
11063 CW_Name := Chars (CW_Type);
11064 Next_E := Next_Entity (CW_Type);
11065 Copy_Node (T, CW_Type);
11066 Set_Comes_From_Source (CW_Type, False);
11067 Set_Chars (CW_Type, CW_Name);
11068 Set_Parent (CW_Type, Parent (T));
11069 Set_Next_Entity (CW_Type, Next_E);
11070 Set_Has_Delayed_Freeze (CW_Type);
11072 -- Customize the class-wide type: It has no prim. op., it cannot be
11073 -- abstract and its Etype points back to the specific root type.
11075 Set_Ekind (CW_Type, E_Class_Wide_Type);
11076 Set_Is_Tagged_Type (CW_Type, True);
11077 Set_Primitive_Operations (CW_Type, New_Elmt_List);
11078 Set_Is_Abstract (CW_Type, False);
11079 Set_Is_Constrained (CW_Type, False);
11080 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
11081 Init_Size_Align (CW_Type);
11083 if Ekind (T) = E_Class_Wide_Subtype then
11084 Set_Etype (CW_Type, Etype (Base_Type (T)));
11085 else
11086 Set_Etype (CW_Type, T);
11087 end if;
11089 -- If this is the class_wide type of a constrained subtype, it does
11090 -- not have discriminants.
11092 Set_Has_Discriminants (CW_Type,
11093 Has_Discriminants (T) and then not Is_Constrained (T));
11095 Set_Has_Unknown_Discriminants (CW_Type, True);
11096 Set_Class_Wide_Type (T, CW_Type);
11097 Set_Equivalent_Type (CW_Type, Empty);
11099 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
11101 Set_Class_Wide_Type (CW_Type, CW_Type);
11103 end Make_Class_Wide_Type;
11105 ----------------
11106 -- Make_Index --
11107 ----------------
11109 procedure Make_Index
11110 (I : Node_Id;
11111 Related_Nod : Node_Id;
11112 Related_Id : Entity_Id := Empty;
11113 Suffix_Index : Nat := 1)
11115 R : Node_Id;
11116 T : Entity_Id;
11117 Def_Id : Entity_Id := Empty;
11118 Found : Boolean := False;
11120 begin
11121 -- For a discrete range used in a constrained array definition and
11122 -- defined by a range, an implicit conversion to the predefined type
11123 -- INTEGER is assumed if each bound is either a numeric literal, a named
11124 -- number, or an attribute, and the type of both bounds (prior to the
11125 -- implicit conversion) is the type universal_integer. Otherwise, both
11126 -- bounds must be of the same discrete type, other than universal
11127 -- integer; this type must be determinable independently of the
11128 -- context, but using the fact that the type must be discrete and that
11129 -- both bounds must have the same type.
11131 -- Character literals also have a universal type in the absence of
11132 -- of additional context, and are resolved to Standard_Character.
11134 if Nkind (I) = N_Range then
11136 -- The index is given by a range constraint. The bounds are known
11137 -- to be of a consistent type.
11139 if not Is_Overloaded (I) then
11140 T := Etype (I);
11142 -- If the bounds are universal, choose the specific predefined
11143 -- type.
11145 if T = Universal_Integer then
11146 T := Standard_Integer;
11148 elsif T = Any_Character then
11150 if Ada_Version >= Ada_95 then
11151 Error_Msg_N
11152 ("ambiguous character literals (could be Wide_Character)",
11154 end if;
11156 T := Standard_Character;
11157 end if;
11159 else
11160 T := Any_Type;
11162 declare
11163 Ind : Interp_Index;
11164 It : Interp;
11166 begin
11167 Get_First_Interp (I, Ind, It);
11169 while Present (It.Typ) loop
11170 if Is_Discrete_Type (It.Typ) then
11172 if Found
11173 and then not Covers (It.Typ, T)
11174 and then not Covers (T, It.Typ)
11175 then
11176 Error_Msg_N ("ambiguous bounds in discrete range", I);
11177 exit;
11178 else
11179 T := It.Typ;
11180 Found := True;
11181 end if;
11182 end if;
11184 Get_Next_Interp (Ind, It);
11185 end loop;
11187 if T = Any_Type then
11188 Error_Msg_N ("discrete type required for range", I);
11189 Set_Etype (I, Any_Type);
11190 return;
11192 elsif T = Universal_Integer then
11193 T := Standard_Integer;
11194 end if;
11195 end;
11196 end if;
11198 if not Is_Discrete_Type (T) then
11199 Error_Msg_N ("discrete type required for range", I);
11200 Set_Etype (I, Any_Type);
11201 return;
11202 end if;
11204 if Nkind (Low_Bound (I)) = N_Attribute_Reference
11205 and then Attribute_Name (Low_Bound (I)) = Name_First
11206 and then Is_Entity_Name (Prefix (Low_Bound (I)))
11207 and then Is_Type (Entity (Prefix (Low_Bound (I))))
11208 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
11209 then
11210 -- The type of the index will be the type of the prefix,
11211 -- as long as the upper bound is 'Last of the same type.
11213 Def_Id := Entity (Prefix (Low_Bound (I)));
11215 if Nkind (High_Bound (I)) /= N_Attribute_Reference
11216 or else Attribute_Name (High_Bound (I)) /= Name_Last
11217 or else not Is_Entity_Name (Prefix (High_Bound (I)))
11218 or else Entity (Prefix (High_Bound (I))) /= Def_Id
11219 then
11220 Def_Id := Empty;
11221 end if;
11222 end if;
11224 R := I;
11225 Process_Range_Expr_In_Decl (R, T);
11227 elsif Nkind (I) = N_Subtype_Indication then
11229 -- The index is given by a subtype with a range constraint.
11231 T := Base_Type (Entity (Subtype_Mark (I)));
11233 if not Is_Discrete_Type (T) then
11234 Error_Msg_N ("discrete type required for range", I);
11235 Set_Etype (I, Any_Type);
11236 return;
11237 end if;
11239 R := Range_Expression (Constraint (I));
11241 Resolve (R, T);
11242 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
11244 elsif Nkind (I) = N_Attribute_Reference then
11246 -- The parser guarantees that the attribute is a RANGE attribute
11248 -- If the node denotes the range of a type mark, that is also the
11249 -- resulting type, and we do no need to create an Itype for it.
11251 if Is_Entity_Name (Prefix (I))
11252 and then Comes_From_Source (I)
11253 and then Is_Type (Entity (Prefix (I)))
11254 and then Is_Discrete_Type (Entity (Prefix (I)))
11255 then
11256 Def_Id := Entity (Prefix (I));
11257 end if;
11259 Analyze_And_Resolve (I);
11260 T := Etype (I);
11261 R := I;
11263 -- If none of the above, must be a subtype. We convert this to a
11264 -- range attribute reference because in the case of declared first
11265 -- named subtypes, the types in the range reference can be different
11266 -- from the type of the entity. A range attribute normalizes the
11267 -- reference and obtains the correct types for the bounds.
11269 -- This transformation is in the nature of an expansion, is only
11270 -- done if expansion is active. In particular, it is not done on
11271 -- formal generic types, because we need to retain the name of the
11272 -- original index for instantiation purposes.
11274 else
11275 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
11276 Error_Msg_N ("invalid subtype mark in discrete range ", I);
11277 Set_Etype (I, Any_Integer);
11278 return;
11279 else
11280 -- The type mark may be that of an incomplete type. It is only
11281 -- now that we can get the full view, previous analysis does
11282 -- not look specifically for a type mark.
11284 Set_Entity (I, Get_Full_View (Entity (I)));
11285 Set_Etype (I, Entity (I));
11286 Def_Id := Entity (I);
11288 if not Is_Discrete_Type (Def_Id) then
11289 Error_Msg_N ("discrete type required for index", I);
11290 Set_Etype (I, Any_Type);
11291 return;
11292 end if;
11293 end if;
11295 if Expander_Active then
11296 Rewrite (I,
11297 Make_Attribute_Reference (Sloc (I),
11298 Attribute_Name => Name_Range,
11299 Prefix => Relocate_Node (I)));
11301 -- The original was a subtype mark that does not freeze. This
11302 -- means that the rewritten version must not freeze either.
11304 Set_Must_Not_Freeze (I);
11305 Set_Must_Not_Freeze (Prefix (I));
11307 -- Is order critical??? if so, document why, if not
11308 -- use Analyze_And_Resolve
11310 Analyze (I);
11311 T := Etype (I);
11312 Resolve (I);
11313 R := I;
11315 -- If expander is inactive, type is legal, nothing else to construct
11317 else
11318 return;
11319 end if;
11320 end if;
11322 if not Is_Discrete_Type (T) then
11323 Error_Msg_N ("discrete type required for range", I);
11324 Set_Etype (I, Any_Type);
11325 return;
11327 elsif T = Any_Type then
11328 Set_Etype (I, Any_Type);
11329 return;
11330 end if;
11332 -- We will now create the appropriate Itype to describe the
11333 -- range, but first a check. If we originally had a subtype,
11334 -- then we just label the range with this subtype. Not only
11335 -- is there no need to construct a new subtype, but it is wrong
11336 -- to do so for two reasons:
11338 -- 1. A legality concern, if we have a subtype, it must not
11339 -- freeze, and the Itype would cause freezing incorrectly
11341 -- 2. An efficiency concern, if we created an Itype, it would
11342 -- not be recognized as the same type for the purposes of
11343 -- eliminating checks in some circumstances.
11345 -- We signal this case by setting the subtype entity in Def_Id.
11347 if No (Def_Id) then
11349 Def_Id :=
11350 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
11351 Set_Etype (Def_Id, Base_Type (T));
11353 if Is_Signed_Integer_Type (T) then
11354 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11356 elsif Is_Modular_Integer_Type (T) then
11357 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11359 else
11360 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11361 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11362 Set_First_Literal (Def_Id, First_Literal (T));
11363 end if;
11365 Set_Size_Info (Def_Id, (T));
11366 Set_RM_Size (Def_Id, RM_Size (T));
11367 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11369 Set_Scalar_Range (Def_Id, R);
11370 Conditional_Delay (Def_Id, T);
11372 -- In the subtype indication case, if the immediate parent of the
11373 -- new subtype is non-static, then the subtype we create is non-
11374 -- static, even if its bounds are static.
11376 if Nkind (I) = N_Subtype_Indication
11377 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
11378 then
11379 Set_Is_Non_Static_Subtype (Def_Id);
11380 end if;
11381 end if;
11383 -- Final step is to label the index with this constructed type
11385 Set_Etype (I, Def_Id);
11386 end Make_Index;
11388 ------------------------------
11389 -- Modular_Type_Declaration --
11390 ------------------------------
11392 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
11393 Mod_Expr : constant Node_Id := Expression (Def);
11394 M_Val : Uint;
11396 procedure Set_Modular_Size (Bits : Int);
11397 -- Sets RM_Size to Bits, and Esize to normal word size above this
11399 ----------------------
11400 -- Set_Modular_Size --
11401 ----------------------
11403 procedure Set_Modular_Size (Bits : Int) is
11404 begin
11405 Set_RM_Size (T, UI_From_Int (Bits));
11407 if Bits <= 8 then
11408 Init_Esize (T, 8);
11410 elsif Bits <= 16 then
11411 Init_Esize (T, 16);
11413 elsif Bits <= 32 then
11414 Init_Esize (T, 32);
11416 else
11417 Init_Esize (T, System_Max_Binary_Modulus_Power);
11418 end if;
11419 end Set_Modular_Size;
11421 -- Start of processing for Modular_Type_Declaration
11423 begin
11424 Analyze_And_Resolve (Mod_Expr, Any_Integer);
11425 Set_Etype (T, T);
11426 Set_Ekind (T, E_Modular_Integer_Type);
11427 Init_Alignment (T);
11428 Set_Is_Constrained (T);
11430 if not Is_OK_Static_Expression (Mod_Expr) then
11431 Flag_Non_Static_Expr
11432 ("non-static expression used for modular type bound!", Mod_Expr);
11433 M_Val := 2 ** System_Max_Binary_Modulus_Power;
11434 else
11435 M_Val := Expr_Value (Mod_Expr);
11436 end if;
11438 if M_Val < 1 then
11439 Error_Msg_N ("modulus value must be positive", Mod_Expr);
11440 M_Val := 2 ** System_Max_Binary_Modulus_Power;
11441 end if;
11443 Set_Modulus (T, M_Val);
11445 -- Create bounds for the modular type based on the modulus given in
11446 -- the type declaration and then analyze and resolve those bounds.
11448 Set_Scalar_Range (T,
11449 Make_Range (Sloc (Mod_Expr),
11450 Low_Bound =>
11451 Make_Integer_Literal (Sloc (Mod_Expr), 0),
11452 High_Bound =>
11453 Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
11455 -- Properly analyze the literals for the range. We do this manually
11456 -- because we can't go calling Resolve, since we are resolving these
11457 -- bounds with the type, and this type is certainly not complete yet!
11459 Set_Etype (Low_Bound (Scalar_Range (T)), T);
11460 Set_Etype (High_Bound (Scalar_Range (T)), T);
11461 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
11462 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
11464 -- Loop through powers of two to find number of bits required
11466 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
11468 -- Binary case
11470 if M_Val = 2 ** Bits then
11471 Set_Modular_Size (Bits);
11472 return;
11474 -- Non-binary case
11476 elsif M_Val < 2 ** Bits then
11477 Set_Non_Binary_Modulus (T);
11479 if Bits > System_Max_Nonbinary_Modulus_Power then
11480 Error_Msg_Uint_1 :=
11481 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
11482 Error_Msg_N
11483 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
11484 Set_Modular_Size (System_Max_Binary_Modulus_Power);
11485 return;
11487 else
11488 -- In the non-binary case, set size as per RM 13.3(55).
11490 Set_Modular_Size (Bits);
11491 return;
11492 end if;
11493 end if;
11495 end loop;
11497 -- If we fall through, then the size exceed System.Max_Binary_Modulus
11498 -- so we just signal an error and set the maximum size.
11500 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
11501 Error_Msg_N ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
11503 Set_Modular_Size (System_Max_Binary_Modulus_Power);
11504 Init_Alignment (T);
11506 end Modular_Type_Declaration;
11508 --------------------------
11509 -- New_Concatenation_Op --
11510 --------------------------
11512 procedure New_Concatenation_Op (Typ : Entity_Id) is
11513 Loc : constant Source_Ptr := Sloc (Typ);
11514 Op : Entity_Id;
11516 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
11517 -- Create abbreviated declaration for the formal of a predefined
11518 -- Operator 'Op' of type 'Typ'
11520 --------------------
11521 -- Make_Op_Formal --
11522 --------------------
11524 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
11525 Formal : Entity_Id;
11527 begin
11528 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
11529 Set_Etype (Formal, Typ);
11530 Set_Mechanism (Formal, Default_Mechanism);
11531 return Formal;
11532 end Make_Op_Formal;
11534 -- Start of processing for New_Concatenation_Op
11536 begin
11537 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
11539 Set_Ekind (Op, E_Operator);
11540 Set_Scope (Op, Current_Scope);
11541 Set_Etype (Op, Typ);
11542 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
11543 Set_Is_Immediately_Visible (Op);
11544 Set_Is_Intrinsic_Subprogram (Op);
11545 Set_Has_Completion (Op);
11546 Append_Entity (Op, Current_Scope);
11548 Set_Name_Entity_Id (Name_Op_Concat, Op);
11550 Append_Entity (Make_Op_Formal (Typ, Op), Op);
11551 Append_Entity (Make_Op_Formal (Typ, Op), Op);
11553 end New_Concatenation_Op;
11555 -------------------------------------------
11556 -- Ordinary_Fixed_Point_Type_Declaration --
11557 -------------------------------------------
11559 procedure Ordinary_Fixed_Point_Type_Declaration
11560 (T : Entity_Id;
11561 Def : Node_Id)
11563 Loc : constant Source_Ptr := Sloc (Def);
11564 Delta_Expr : constant Node_Id := Delta_Expression (Def);
11565 RRS : constant Node_Id := Real_Range_Specification (Def);
11566 Implicit_Base : Entity_Id;
11567 Delta_Val : Ureal;
11568 Small_Val : Ureal;
11569 Low_Val : Ureal;
11570 High_Val : Ureal;
11572 begin
11573 Check_Restriction (No_Fixed_Point, Def);
11575 -- Create implicit base type
11577 Implicit_Base :=
11578 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
11579 Set_Etype (Implicit_Base, Implicit_Base);
11581 -- Analyze and process delta expression
11583 Analyze_And_Resolve (Delta_Expr, Any_Real);
11585 Check_Delta_Expression (Delta_Expr);
11586 Delta_Val := Expr_Value_R (Delta_Expr);
11588 Set_Delta_Value (Implicit_Base, Delta_Val);
11590 -- Compute default small from given delta, which is the largest
11591 -- power of two that does not exceed the given delta value.
11593 declare
11594 Tmp : Ureal := Ureal_1;
11595 Scale : Int := 0;
11597 begin
11598 if Delta_Val < Ureal_1 then
11599 while Delta_Val < Tmp loop
11600 Tmp := Tmp / Ureal_2;
11601 Scale := Scale + 1;
11602 end loop;
11604 else
11605 loop
11606 Tmp := Tmp * Ureal_2;
11607 exit when Tmp > Delta_Val;
11608 Scale := Scale - 1;
11609 end loop;
11610 end if;
11612 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
11613 end;
11615 Set_Small_Value (Implicit_Base, Small_Val);
11617 -- If no range was given, set a dummy range
11619 if RRS <= Empty_Or_Error then
11620 Low_Val := -Small_Val;
11621 High_Val := Small_Val;
11623 -- Otherwise analyze and process given range
11625 else
11626 declare
11627 Low : constant Node_Id := Low_Bound (RRS);
11628 High : constant Node_Id := High_Bound (RRS);
11630 begin
11631 Analyze_And_Resolve (Low, Any_Real);
11632 Analyze_And_Resolve (High, Any_Real);
11633 Check_Real_Bound (Low);
11634 Check_Real_Bound (High);
11636 -- Obtain and set the range
11638 Low_Val := Expr_Value_R (Low);
11639 High_Val := Expr_Value_R (High);
11641 if Low_Val > High_Val then
11642 Error_Msg_NE ("?fixed point type& has null range", Def, T);
11643 end if;
11644 end;
11645 end if;
11647 -- The range for both the implicit base and the declared first
11648 -- subtype cannot be set yet, so we use the special routine
11649 -- Set_Fixed_Range to set a temporary range in place. Note that
11650 -- the bounds of the base type will be widened to be symmetrical
11651 -- and to fill the available bits when the type is frozen.
11653 -- We could do this with all discrete types, and probably should, but
11654 -- we absolutely have to do it for fixed-point, since the end-points
11655 -- of the range and the size are determined by the small value, which
11656 -- could be reset before the freeze point.
11658 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
11659 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11661 Init_Size_Align (Implicit_Base);
11663 -- Complete definition of first subtype
11665 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
11666 Set_Etype (T, Implicit_Base);
11667 Init_Size_Align (T);
11668 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11669 Set_Small_Value (T, Small_Val);
11670 Set_Delta_Value (T, Delta_Val);
11671 Set_Is_Constrained (T);
11673 end Ordinary_Fixed_Point_Type_Declaration;
11675 ----------------------------------------
11676 -- Prepare_Private_Subtype_Completion --
11677 ----------------------------------------
11679 procedure Prepare_Private_Subtype_Completion
11680 (Id : Entity_Id;
11681 Related_Nod : Node_Id)
11683 Id_B : constant Entity_Id := Base_Type (Id);
11684 Full_B : constant Entity_Id := Full_View (Id_B);
11685 Full : Entity_Id;
11687 begin
11688 if Present (Full_B) then
11690 -- The Base_Type is already completed, we can complete the
11691 -- subtype now. We have to create a new entity with the same name,
11692 -- Thus we can't use Create_Itype.
11693 -- This is messy, should be fixed ???
11695 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
11696 Set_Is_Itype (Full);
11697 Set_Associated_Node_For_Itype (Full, Related_Nod);
11698 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
11699 end if;
11701 -- The parent subtype may be private, but the base might not, in some
11702 -- nested instances. In that case, the subtype does not need to be
11703 -- exchanged. It would still be nice to make private subtypes and their
11704 -- bases consistent at all times ???
11706 if Is_Private_Type (Id_B) then
11707 Append_Elmt (Id, Private_Dependents (Id_B));
11708 end if;
11710 end Prepare_Private_Subtype_Completion;
11712 ---------------------------
11713 -- Process_Discriminants --
11714 ---------------------------
11716 procedure Process_Discriminants
11717 (N : Node_Id;
11718 Prev : Entity_Id := Empty)
11720 Elist : constant Elist_Id := New_Elmt_List;
11721 Id : Node_Id;
11722 Discr : Node_Id;
11723 Discr_Number : Uint;
11724 Discr_Type : Entity_Id;
11725 Default_Present : Boolean := False;
11726 Default_Not_Present : Boolean := False;
11728 begin
11729 -- A composite type other than an array type can have discriminants.
11730 -- Discriminants of non-limited types must have a discrete type.
11731 -- On entry, the current scope is the composite type.
11733 -- The discriminants are initially entered into the scope of the type
11734 -- via Enter_Name with the default Ekind of E_Void to prevent premature
11735 -- use, as explained at the end of this procedure.
11737 Discr := First (Discriminant_Specifications (N));
11738 while Present (Discr) loop
11739 Enter_Name (Defining_Identifier (Discr));
11741 -- For navigation purposes we add a reference to the discriminant
11742 -- in the entity for the type. If the current declaration is a
11743 -- completion, place references on the partial view. Otherwise the
11744 -- type is the current scope.
11746 if Present (Prev) then
11748 -- The references go on the partial view, if present. If the
11749 -- partial view has discriminants, the references have been
11750 -- generated already.
11752 if not Has_Discriminants (Prev) then
11753 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
11754 end if;
11755 else
11756 Generate_Reference
11757 (Current_Scope, Defining_Identifier (Discr), 'd');
11758 end if;
11760 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
11761 Discr_Type := Access_Definition (N, Discriminant_Type (Discr));
11763 -- Ada 2005 (AI-254)
11765 if Present (Access_To_Subprogram_Definition
11766 (Discriminant_Type (Discr)))
11767 and then Protected_Present (Access_To_Subprogram_Definition
11768 (Discriminant_Type (Discr)))
11769 then
11770 Discr_Type :=
11771 Replace_Anonymous_Access_To_Protected_Subprogram
11772 (Discr, Discr_Type);
11773 end if;
11775 else
11776 Find_Type (Discriminant_Type (Discr));
11777 Discr_Type := Etype (Discriminant_Type (Discr));
11779 if Error_Posted (Discriminant_Type (Discr)) then
11780 Discr_Type := Any_Type;
11781 end if;
11782 end if;
11784 if Is_Access_Type (Discr_Type) then
11786 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
11787 -- record types
11789 if Ada_Version < Ada_05 then
11790 Check_Access_Discriminant_Requires_Limited
11791 (Discr, Discriminant_Type (Discr));
11792 end if;
11794 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
11795 Error_Msg_N
11796 ("(Ada 83) access discriminant not allowed", Discr);
11797 end if;
11799 elsif not Is_Discrete_Type (Discr_Type) then
11800 Error_Msg_N ("discriminants must have a discrete or access type",
11801 Discriminant_Type (Discr));
11802 end if;
11804 Set_Etype (Defining_Identifier (Discr), Discr_Type);
11806 -- If a discriminant specification includes the assignment compound
11807 -- delimiter followed by an expression, the expression is the default
11808 -- expression of the discriminant; the default expression must be of
11809 -- the type of the discriminant. (RM 3.7.1) Since this expression is
11810 -- a default expression, we do the special preanalysis, since this
11811 -- expression does not freeze (see "Handling of Default and Per-
11812 -- Object Expressions" in spec of package Sem).
11814 if Present (Expression (Discr)) then
11815 Analyze_Per_Use_Expression (Expression (Discr), Discr_Type);
11817 if Nkind (N) = N_Formal_Type_Declaration then
11818 Error_Msg_N
11819 ("discriminant defaults not allowed for formal type",
11820 Expression (Discr));
11822 -- Tagged types cannot have defaulted discriminants, but a
11823 -- non-tagged private type with defaulted discriminants
11824 -- can have a tagged completion.
11826 elsif Is_Tagged_Type (Current_Scope)
11827 and then Comes_From_Source (N)
11828 then
11829 Error_Msg_N
11830 ("discriminants of tagged type cannot have defaults",
11831 Expression (Discr));
11833 else
11834 Default_Present := True;
11835 Append_Elmt (Expression (Discr), Elist);
11837 -- Tag the defining identifiers for the discriminants with
11838 -- their corresponding default expressions from the tree.
11840 Set_Discriminant_Default_Value
11841 (Defining_Identifier (Discr), Expression (Discr));
11842 end if;
11844 else
11845 Default_Not_Present := True;
11846 end if;
11848 -- Ada 2005 (AI-231): Set the null-excluding attribute and carry
11849 -- out some static checks.
11851 if Ada_Version >= Ada_05
11852 and then (Null_Exclusion_Present (Discr)
11853 or else Can_Never_Be_Null (Discr_Type))
11854 then
11855 Set_Can_Never_Be_Null (Defining_Identifier (Discr));
11856 Null_Exclusion_Static_Checks (Discr);
11857 end if;
11859 Next (Discr);
11860 end loop;
11862 -- An element list consisting of the default expressions of the
11863 -- discriminants is constructed in the above loop and used to set
11864 -- the Discriminant_Constraint attribute for the type. If an object
11865 -- is declared of this (record or task) type without any explicit
11866 -- discriminant constraint given, this element list will form the
11867 -- actual parameters for the corresponding initialization procedure
11868 -- for the type.
11870 Set_Discriminant_Constraint (Current_Scope, Elist);
11871 Set_Stored_Constraint (Current_Scope, No_Elist);
11873 -- Default expressions must be provided either for all or for none
11874 -- of the discriminants of a discriminant part. (RM 3.7.1)
11876 if Default_Present and then Default_Not_Present then
11877 Error_Msg_N
11878 ("incomplete specification of defaults for discriminants", N);
11879 end if;
11881 -- The use of the name of a discriminant is not allowed in default
11882 -- expressions of a discriminant part if the specification of the
11883 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
11885 -- To detect this, the discriminant names are entered initially with an
11886 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
11887 -- attempt to use a void entity (for example in an expression that is
11888 -- type-checked) produces the error message: premature usage. Now after
11889 -- completing the semantic analysis of the discriminant part, we can set
11890 -- the Ekind of all the discriminants appropriately.
11892 Discr := First (Discriminant_Specifications (N));
11893 Discr_Number := Uint_1;
11895 while Present (Discr) loop
11896 Id := Defining_Identifier (Discr);
11897 Set_Ekind (Id, E_Discriminant);
11898 Init_Component_Location (Id);
11899 Init_Esize (Id);
11900 Set_Discriminant_Number (Id, Discr_Number);
11902 -- Make sure this is always set, even in illegal programs
11904 Set_Corresponding_Discriminant (Id, Empty);
11906 -- Initialize the Original_Record_Component to the entity itself.
11907 -- Inherit_Components will propagate the right value to
11908 -- discriminants in derived record types.
11910 Set_Original_Record_Component (Id, Id);
11912 -- Create the discriminal for the discriminant.
11914 Build_Discriminal (Id);
11916 Next (Discr);
11917 Discr_Number := Discr_Number + 1;
11918 end loop;
11920 Set_Has_Discriminants (Current_Scope);
11921 end Process_Discriminants;
11923 -----------------------
11924 -- Process_Full_View --
11925 -----------------------
11927 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
11928 Priv_Parent : Entity_Id;
11929 Full_Parent : Entity_Id;
11930 Full_Indic : Node_Id;
11932 begin
11933 -- First some sanity checks that must be done after semantic
11934 -- decoration of the full view and thus cannot be placed with other
11935 -- similar checks in Find_Type_Name
11937 if not Is_Limited_Type (Priv_T)
11938 and then (Is_Limited_Type (Full_T)
11939 or else Is_Limited_Composite (Full_T))
11940 then
11941 Error_Msg_N
11942 ("completion of nonlimited type cannot be limited", Full_T);
11943 Explain_Limited_Type (Full_T, Full_T);
11945 elsif Is_Abstract (Full_T) and then not Is_Abstract (Priv_T) then
11946 Error_Msg_N
11947 ("completion of nonabstract type cannot be abstract", Full_T);
11949 elsif Is_Tagged_Type (Priv_T)
11950 and then Is_Limited_Type (Priv_T)
11951 and then not Is_Limited_Type (Full_T)
11952 then
11953 -- GNAT allow its own definition of Limited_Controlled to disobey
11954 -- this rule in order in ease the implementation. The next test is
11955 -- safe because Root_Controlled is defined in a private system child
11957 if Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
11958 Set_Is_Limited_Composite (Full_T);
11959 else
11960 Error_Msg_N
11961 ("completion of limited tagged type must be limited", Full_T);
11962 end if;
11964 elsif Is_Generic_Type (Priv_T) then
11965 Error_Msg_N ("generic type cannot have a completion", Full_T);
11966 end if;
11968 if Is_Tagged_Type (Priv_T)
11969 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
11970 and then Is_Derived_Type (Full_T)
11971 then
11972 Priv_Parent := Etype (Priv_T);
11974 -- The full view of a private extension may have been transformed
11975 -- into an unconstrained derived type declaration and a subtype
11976 -- declaration (see build_derived_record_type for details).
11978 if Nkind (N) = N_Subtype_Declaration then
11979 Full_Indic := Subtype_Indication (N);
11980 Full_Parent := Etype (Base_Type (Full_T));
11981 else
11982 Full_Indic := Subtype_Indication (Type_Definition (N));
11983 Full_Parent := Etype (Full_T);
11984 end if;
11986 -- Check that the parent type of the full type is a descendant of
11987 -- the ancestor subtype given in the private extension. If either
11988 -- entity has an Etype equal to Any_Type then we had some previous
11989 -- error situation [7.3(8)].
11991 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
11992 return;
11994 elsif not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent) then
11995 Error_Msg_N
11996 ("parent of full type must descend from parent"
11997 & " of private extension", Full_Indic);
11999 -- Check the rules of 7.3(10): if the private extension inherits
12000 -- known discriminants, then the full type must also inherit those
12001 -- discriminants from the same (ancestor) type, and the parent
12002 -- subtype of the full type must be constrained if and only if
12003 -- the ancestor subtype of the private extension is constrained.
12005 elsif not Present (Discriminant_Specifications (Parent (Priv_T)))
12006 and then not Has_Unknown_Discriminants (Priv_T)
12007 and then Has_Discriminants (Base_Type (Priv_Parent))
12008 then
12009 declare
12010 Priv_Indic : constant Node_Id :=
12011 Subtype_Indication (Parent (Priv_T));
12013 Priv_Constr : constant Boolean :=
12014 Is_Constrained (Priv_Parent)
12015 or else
12016 Nkind (Priv_Indic) = N_Subtype_Indication
12017 or else Is_Constrained (Entity (Priv_Indic));
12019 Full_Constr : constant Boolean :=
12020 Is_Constrained (Full_Parent)
12021 or else
12022 Nkind (Full_Indic) = N_Subtype_Indication
12023 or else Is_Constrained (Entity (Full_Indic));
12025 Priv_Discr : Entity_Id;
12026 Full_Discr : Entity_Id;
12028 begin
12029 Priv_Discr := First_Discriminant (Priv_Parent);
12030 Full_Discr := First_Discriminant (Full_Parent);
12032 while Present (Priv_Discr) and then Present (Full_Discr) loop
12033 if Original_Record_Component (Priv_Discr) =
12034 Original_Record_Component (Full_Discr)
12035 or else
12036 Corresponding_Discriminant (Priv_Discr) =
12037 Corresponding_Discriminant (Full_Discr)
12038 then
12039 null;
12040 else
12041 exit;
12042 end if;
12044 Next_Discriminant (Priv_Discr);
12045 Next_Discriminant (Full_Discr);
12046 end loop;
12048 if Present (Priv_Discr) or else Present (Full_Discr) then
12049 Error_Msg_N
12050 ("full view must inherit discriminants of the parent type"
12051 & " used in the private extension", Full_Indic);
12053 elsif Priv_Constr and then not Full_Constr then
12054 Error_Msg_N
12055 ("parent subtype of full type must be constrained",
12056 Full_Indic);
12058 elsif Full_Constr and then not Priv_Constr then
12059 Error_Msg_N
12060 ("parent subtype of full type must be unconstrained",
12061 Full_Indic);
12062 end if;
12063 end;
12065 -- Check the rules of 7.3(12): if a partial view has neither known
12066 -- or unknown discriminants, then the full type declaration shall
12067 -- define a definite subtype.
12069 elsif not Has_Unknown_Discriminants (Priv_T)
12070 and then not Has_Discriminants (Priv_T)
12071 and then not Is_Constrained (Full_T)
12072 then
12073 Error_Msg_N
12074 ("full view must define a constrained type if partial view"
12075 & " has no discriminants", Full_T);
12076 end if;
12078 -- ??????? Do we implement the following properly ?????
12079 -- If the ancestor subtype of a private extension has constrained
12080 -- discriminants, then the parent subtype of the full view shall
12081 -- impose a statically matching constraint on those discriminants
12082 -- [7.3(13)].
12084 else
12085 -- For untagged types, verify that a type without discriminants
12086 -- is not completed with an unconstrained type.
12088 if not Is_Indefinite_Subtype (Priv_T)
12089 and then Is_Indefinite_Subtype (Full_T)
12090 then
12091 Error_Msg_N ("full view of type must be definite subtype", Full_T);
12092 end if;
12093 end if;
12095 -- Create a full declaration for all its subtypes recorded in
12096 -- Private_Dependents and swap them similarly to the base type.
12097 -- These are subtypes that have been define before the full
12098 -- declaration of the private type. We also swap the entry in
12099 -- Private_Dependents list so we can properly restore the
12100 -- private view on exit from the scope.
12102 declare
12103 Priv_Elmt : Elmt_Id;
12104 Priv : Entity_Id;
12105 Full : Entity_Id;
12107 begin
12108 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
12109 while Present (Priv_Elmt) loop
12110 Priv := Node (Priv_Elmt);
12112 if Ekind (Priv) = E_Private_Subtype
12113 or else Ekind (Priv) = E_Limited_Private_Subtype
12114 or else Ekind (Priv) = E_Record_Subtype_With_Private
12115 then
12116 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12117 Set_Is_Itype (Full);
12118 Set_Parent (Full, Parent (Priv));
12119 Set_Associated_Node_For_Itype (Full, N);
12121 -- Now we need to complete the private subtype, but since the
12122 -- base type has already been swapped, we must also swap the
12123 -- subtypes (and thus, reverse the arguments in the call to
12124 -- Complete_Private_Subtype).
12126 Copy_And_Swap (Priv, Full);
12127 Complete_Private_Subtype (Full, Priv, Full_T, N);
12128 Replace_Elmt (Priv_Elmt, Full);
12129 end if;
12131 Next_Elmt (Priv_Elmt);
12132 end loop;
12133 end;
12135 -- If the private view was tagged, copy the new Primitive
12136 -- operations from the private view to the full view.
12138 if Is_Tagged_Type (Full_T) then
12139 declare
12140 Priv_List : Elist_Id;
12141 Full_List : constant Elist_Id := Primitive_Operations (Full_T);
12142 P1, P2 : Elmt_Id;
12143 Prim : Entity_Id;
12144 D_Type : Entity_Id;
12146 begin
12147 if Is_Tagged_Type (Priv_T) then
12148 Priv_List := Primitive_Operations (Priv_T);
12150 P1 := First_Elmt (Priv_List);
12151 while Present (P1) loop
12152 Prim := Node (P1);
12154 -- Transfer explicit primitives, not those inherited from
12155 -- parent of partial view, which will be re-inherited on
12156 -- the full view.
12158 if Comes_From_Source (Prim) then
12159 P2 := First_Elmt (Full_List);
12160 while Present (P2) and then Node (P2) /= Prim loop
12161 Next_Elmt (P2);
12162 end loop;
12164 -- If not found, that is a new one
12166 if No (P2) then
12167 Append_Elmt (Prim, Full_List);
12168 end if;
12169 end if;
12171 Next_Elmt (P1);
12172 end loop;
12174 else
12175 -- In this case the partial view is untagged, so here we
12176 -- locate all of the earlier primitives that need to be
12177 -- treated as dispatching (those that appear between the
12178 -- two views). Note that these additional operations must
12179 -- all be new operations (any earlier operations that
12180 -- override inherited operations of the full view will
12181 -- already have been inserted in the primitives list and
12182 -- marked as dispatching by Check_Operation_From_Private_View.
12183 -- Note that implicit "/=" operators are excluded from being
12184 -- added to the primitives list since they shouldn't be
12185 -- treated as dispatching (tagged "/=" is handled specially).
12187 Prim := Next_Entity (Full_T);
12188 while Present (Prim) and then Prim /= Priv_T loop
12189 if Ekind (Prim) = E_Procedure
12190 or else
12191 Ekind (Prim) = E_Function
12192 then
12194 D_Type := Find_Dispatching_Type (Prim);
12196 if D_Type = Full_T
12197 and then (Chars (Prim) /= Name_Op_Ne
12198 or else Comes_From_Source (Prim))
12199 then
12200 Check_Controlling_Formals (Full_T, Prim);
12202 if not Is_Dispatching_Operation (Prim) then
12203 Append_Elmt (Prim, Full_List);
12204 Set_Is_Dispatching_Operation (Prim, True);
12205 Set_DT_Position (Prim, No_Uint);
12206 end if;
12208 elsif Is_Dispatching_Operation (Prim)
12209 and then D_Type /= Full_T
12210 then
12212 -- Verify that it is not otherwise controlled by
12213 -- a formal or a return value ot type T.
12215 Check_Controlling_Formals (D_Type, Prim);
12216 end if;
12217 end if;
12219 Next_Entity (Prim);
12220 end loop;
12221 end if;
12223 -- For the tagged case, the two views can share the same
12224 -- Primitive Operation list and the same class wide type.
12225 -- Update attributes of the class-wide type which depend on
12226 -- the full declaration.
12228 if Is_Tagged_Type (Priv_T) then
12229 Set_Primitive_Operations (Priv_T, Full_List);
12230 Set_Class_Wide_Type
12231 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
12233 -- Any other attributes should be propagated to C_W ???
12235 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
12237 end if;
12238 end;
12239 end if;
12240 end Process_Full_View;
12242 -----------------------------------
12243 -- Process_Incomplete_Dependents --
12244 -----------------------------------
12246 procedure Process_Incomplete_Dependents
12247 (N : Node_Id;
12248 Full_T : Entity_Id;
12249 Inc_T : Entity_Id)
12251 Inc_Elmt : Elmt_Id;
12252 Priv_Dep : Entity_Id;
12253 New_Subt : Entity_Id;
12255 Disc_Constraint : Elist_Id;
12257 begin
12258 if No (Private_Dependents (Inc_T)) then
12259 return;
12261 else
12262 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
12264 -- Itypes that may be generated by the completion of an incomplete
12265 -- subtype are not used by the back-end and not attached to the tree.
12266 -- They are created only for constraint-checking purposes.
12267 end if;
12269 while Present (Inc_Elmt) loop
12270 Priv_Dep := Node (Inc_Elmt);
12272 if Ekind (Priv_Dep) = E_Subprogram_Type then
12274 -- An Access_To_Subprogram type may have a return type or a
12275 -- parameter type that is incomplete. Replace with the full view.
12277 if Etype (Priv_Dep) = Inc_T then
12278 Set_Etype (Priv_Dep, Full_T);
12279 end if;
12281 declare
12282 Formal : Entity_Id;
12284 begin
12285 Formal := First_Formal (Priv_Dep);
12287 while Present (Formal) loop
12289 if Etype (Formal) = Inc_T then
12290 Set_Etype (Formal, Full_T);
12291 end if;
12293 Next_Formal (Formal);
12294 end loop;
12295 end;
12297 elsif Is_Overloadable (Priv_Dep) then
12299 if Is_Tagged_Type (Full_T) then
12301 -- Subprogram has an access parameter whose designated type
12302 -- was incomplete. Reexamine declaration now, because it may
12303 -- be a primitive operation of the full type.
12305 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
12306 Set_Is_Dispatching_Operation (Priv_Dep);
12307 Check_Controlling_Formals (Full_T, Priv_Dep);
12308 end if;
12310 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
12312 -- Can happen during processing of a body before the completion
12313 -- of a TA type. Ignore, because spec is also on dependent list.
12315 return;
12317 -- Dependent is a subtype
12319 else
12320 -- We build a new subtype indication using the full view of the
12321 -- incomplete parent. The discriminant constraints have been
12322 -- elaborated already at the point of the subtype declaration.
12324 New_Subt := Create_Itype (E_Void, N);
12326 if Has_Discriminants (Full_T) then
12327 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
12328 else
12329 Disc_Constraint := No_Elist;
12330 end if;
12332 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
12333 Set_Full_View (Priv_Dep, New_Subt);
12334 end if;
12336 Next_Elmt (Inc_Elmt);
12337 end loop;
12339 end Process_Incomplete_Dependents;
12341 --------------------------------
12342 -- Process_Range_Expr_In_Decl --
12343 --------------------------------
12345 procedure Process_Range_Expr_In_Decl
12346 (R : Node_Id;
12347 T : Entity_Id;
12348 Check_List : List_Id := Empty_List;
12349 R_Check_Off : Boolean := False)
12351 Lo, Hi : Node_Id;
12352 R_Checks : Check_Result;
12353 Type_Decl : Node_Id;
12354 Def_Id : Entity_Id;
12356 begin
12357 Analyze_And_Resolve (R, Base_Type (T));
12359 if Nkind (R) = N_Range then
12360 Lo := Low_Bound (R);
12361 Hi := High_Bound (R);
12363 -- If there were errors in the declaration, try and patch up some
12364 -- common mistakes in the bounds. The cases handled are literals
12365 -- which are Integer where the expected type is Real and vice versa.
12366 -- These corrections allow the compilation process to proceed further
12367 -- along since some basic assumptions of the format of the bounds
12368 -- are guaranteed.
12370 if Etype (R) = Any_Type then
12372 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
12373 Rewrite (Lo,
12374 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
12376 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
12377 Rewrite (Hi,
12378 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
12380 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
12381 Rewrite (Lo,
12382 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
12384 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
12385 Rewrite (Hi,
12386 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
12387 end if;
12389 Set_Etype (Lo, T);
12390 Set_Etype (Hi, T);
12391 end if;
12393 -- If the bounds of the range have been mistakenly given as
12394 -- string literals (perhaps in place of character literals),
12395 -- then an error has already been reported, but we rewrite
12396 -- the string literal as a bound of the range's type to
12397 -- avoid blowups in later processing that looks at static
12398 -- values.
12400 if Nkind (Lo) = N_String_Literal then
12401 Rewrite (Lo,
12402 Make_Attribute_Reference (Sloc (Lo),
12403 Attribute_Name => Name_First,
12404 Prefix => New_Reference_To (T, Sloc (Lo))));
12405 Analyze_And_Resolve (Lo);
12406 end if;
12408 if Nkind (Hi) = N_String_Literal then
12409 Rewrite (Hi,
12410 Make_Attribute_Reference (Sloc (Hi),
12411 Attribute_Name => Name_First,
12412 Prefix => New_Reference_To (T, Sloc (Hi))));
12413 Analyze_And_Resolve (Hi);
12414 end if;
12416 -- If bounds aren't scalar at this point then exit, avoiding
12417 -- problems with further processing of the range in this procedure.
12419 if not Is_Scalar_Type (Etype (Lo)) then
12420 return;
12421 end if;
12423 -- Resolve (actually Sem_Eval) has checked that the bounds are in
12424 -- then range of the base type. Here we check whether the bounds
12425 -- are in the range of the subtype itself. Note that if the bounds
12426 -- represent the null range the Constraint_Error exception should
12427 -- not be raised.
12429 -- ??? The following code should be cleaned up as follows
12430 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
12431 -- is done in the call to Range_Check (R, T); below
12432 -- 2. The use of R_Check_Off should be investigated and possibly
12433 -- removed, this would clean up things a bit.
12435 if Is_Null_Range (Lo, Hi) then
12436 null;
12438 else
12439 -- Capture values of bounds and generate temporaries for them
12440 -- if needed, before applying checks, since checks may cause
12441 -- duplication of the expression without forcing evaluation.
12443 if Expander_Active then
12444 Force_Evaluation (Lo);
12445 Force_Evaluation (Hi);
12446 end if;
12448 -- We use a flag here instead of suppressing checks on the
12449 -- type because the type we check against isn't necessarily
12450 -- the place where we put the check.
12452 if not R_Check_Off then
12453 R_Checks := Range_Check (R, T);
12454 Type_Decl := Parent (R);
12456 -- Look up tree to find an appropriate insertion point.
12457 -- This seems really junk code, and very brittle, couldn't
12458 -- we just use an insert actions call of some kind ???
12460 while Present (Type_Decl) and then not
12461 (Nkind (Type_Decl) = N_Full_Type_Declaration
12462 or else
12463 Nkind (Type_Decl) = N_Subtype_Declaration
12464 or else
12465 Nkind (Type_Decl) = N_Loop_Statement
12466 or else
12467 Nkind (Type_Decl) = N_Task_Type_Declaration
12468 or else
12469 Nkind (Type_Decl) = N_Single_Task_Declaration
12470 or else
12471 Nkind (Type_Decl) = N_Protected_Type_Declaration
12472 or else
12473 Nkind (Type_Decl) = N_Single_Protected_Declaration)
12474 loop
12475 Type_Decl := Parent (Type_Decl);
12476 end loop;
12478 -- Why would Type_Decl not be present??? Without this test,
12479 -- short regression tests fail.
12481 if Present (Type_Decl) then
12483 -- Case of loop statement (more comments ???)
12485 if Nkind (Type_Decl) = N_Loop_Statement then
12486 declare
12487 Indic : Node_Id := Parent (R);
12489 begin
12490 while Present (Indic) and then not
12491 (Nkind (Indic) = N_Subtype_Indication)
12492 loop
12493 Indic := Parent (Indic);
12494 end loop;
12496 if Present (Indic) then
12497 Def_Id := Etype (Subtype_Mark (Indic));
12499 Insert_Range_Checks
12500 (R_Checks,
12501 Type_Decl,
12502 Def_Id,
12503 Sloc (Type_Decl),
12505 Do_Before => True);
12506 end if;
12507 end;
12509 -- All other cases (more comments ???)
12511 else
12512 Def_Id := Defining_Identifier (Type_Decl);
12514 if (Ekind (Def_Id) = E_Record_Type
12515 and then Depends_On_Discriminant (R))
12516 or else
12517 (Ekind (Def_Id) = E_Protected_Type
12518 and then Has_Discriminants (Def_Id))
12519 then
12520 Append_Range_Checks
12521 (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
12523 else
12524 Insert_Range_Checks
12525 (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
12527 end if;
12528 end if;
12529 end if;
12530 end if;
12531 end if;
12533 elsif Expander_Active then
12534 Get_Index_Bounds (R, Lo, Hi);
12535 Force_Evaluation (Lo);
12536 Force_Evaluation (Hi);
12537 end if;
12538 end Process_Range_Expr_In_Decl;
12540 --------------------------------------
12541 -- Process_Real_Range_Specification --
12542 --------------------------------------
12544 procedure Process_Real_Range_Specification (Def : Node_Id) is
12545 Spec : constant Node_Id := Real_Range_Specification (Def);
12546 Lo : Node_Id;
12547 Hi : Node_Id;
12548 Err : Boolean := False;
12550 procedure Analyze_Bound (N : Node_Id);
12551 -- Analyze and check one bound
12553 -------------------
12554 -- Analyze_Bound --
12555 -------------------
12557 procedure Analyze_Bound (N : Node_Id) is
12558 begin
12559 Analyze_And_Resolve (N, Any_Real);
12561 if not Is_OK_Static_Expression (N) then
12562 Flag_Non_Static_Expr
12563 ("bound in real type definition is not static!", N);
12564 Err := True;
12565 end if;
12566 end Analyze_Bound;
12568 -- Start of processing for Process_Real_Range_Specification
12570 begin
12571 if Present (Spec) then
12572 Lo := Low_Bound (Spec);
12573 Hi := High_Bound (Spec);
12574 Analyze_Bound (Lo);
12575 Analyze_Bound (Hi);
12577 -- If error, clear away junk range specification
12579 if Err then
12580 Set_Real_Range_Specification (Def, Empty);
12581 end if;
12582 end if;
12583 end Process_Real_Range_Specification;
12585 ---------------------
12586 -- Process_Subtype --
12587 ---------------------
12589 function Process_Subtype
12590 (S : Node_Id;
12591 Related_Nod : Node_Id;
12592 Related_Id : Entity_Id := Empty;
12593 Suffix : Character := ' ') return Entity_Id
12595 P : Node_Id;
12596 Def_Id : Entity_Id;
12597 Full_View_Id : Entity_Id;
12598 Subtype_Mark_Id : Entity_Id;
12600 procedure Check_Incomplete (T : Entity_Id);
12601 -- Called to verify that an incomplete type is not used prematurely
12603 ----------------------
12604 -- Check_Incomplete --
12605 ----------------------
12607 procedure Check_Incomplete (T : Entity_Id) is
12608 begin
12609 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type then
12610 Error_Msg_N ("invalid use of type before its full declaration", T);
12611 end if;
12612 end Check_Incomplete;
12614 -- Start of processing for Process_Subtype
12616 begin
12617 -- Case of no constraints present
12619 if Nkind (S) /= N_Subtype_Indication then
12621 Find_Type (S);
12622 Check_Incomplete (S);
12624 -- Ada 2005 (AI-231): Static check
12626 if Ada_Version >= Ada_05
12627 and then Present (Parent (S))
12628 and then Null_Exclusion_Present (Parent (S))
12629 and then Nkind (Parent (S)) /= N_Access_To_Object_Definition
12630 and then not Is_Access_Type (Entity (S))
12631 then
12632 Error_Msg_N
12633 ("(Ada 2005) null-exclusion part requires an access type", S);
12634 end if;
12635 return Entity (S);
12637 -- Case of constraint present, so that we have an N_Subtype_Indication
12638 -- node (this node is created only if constraints are present).
12640 else
12642 Find_Type (Subtype_Mark (S));
12644 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
12645 and then not
12646 (Nkind (Parent (S)) = N_Subtype_Declaration
12647 and then
12648 Is_Itype (Defining_Identifier (Parent (S))))
12649 then
12650 Check_Incomplete (Subtype_Mark (S));
12651 end if;
12653 P := Parent (S);
12654 Subtype_Mark_Id := Entity (Subtype_Mark (S));
12656 -- Explicit subtype declaration case
12658 if Nkind (P) = N_Subtype_Declaration then
12659 Def_Id := Defining_Identifier (P);
12661 -- Explicit derived type definition case
12663 elsif Nkind (P) = N_Derived_Type_Definition then
12664 Def_Id := Defining_Identifier (Parent (P));
12666 -- Implicit case, the Def_Id must be created as an implicit type.
12667 -- The one exception arises in the case of concurrent types,
12668 -- array and access types, where other subsidiary implicit types
12669 -- may be created and must appear before the main implicit type.
12670 -- In these cases we leave Def_Id set to Empty as a signal that
12671 -- Create_Itype has not yet been called to create Def_Id.
12673 else
12674 if Is_Array_Type (Subtype_Mark_Id)
12675 or else Is_Concurrent_Type (Subtype_Mark_Id)
12676 or else Is_Access_Type (Subtype_Mark_Id)
12677 then
12678 Def_Id := Empty;
12680 -- For the other cases, we create a new unattached Itype,
12681 -- and set the indication to ensure it gets attached later.
12683 else
12684 Def_Id :=
12685 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12686 end if;
12687 end if;
12689 -- If the kind of constraint is invalid for this kind of type,
12690 -- then give an error, and then pretend no constraint was given.
12692 if not Is_Valid_Constraint_Kind
12693 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
12694 then
12695 Error_Msg_N
12696 ("incorrect constraint for this kind of type", Constraint (S));
12698 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
12700 -- Make recursive call, having got rid of the bogus constraint
12702 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
12703 end if;
12705 -- Remaining processing depends on type
12707 case Ekind (Subtype_Mark_Id) is
12709 when Access_Kind =>
12710 Constrain_Access (Def_Id, S, Related_Nod);
12712 when Array_Kind =>
12713 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
12715 when Decimal_Fixed_Point_Kind =>
12716 Constrain_Decimal (Def_Id, S);
12718 when Enumeration_Kind =>
12719 Constrain_Enumeration (Def_Id, S);
12721 when Ordinary_Fixed_Point_Kind =>
12722 Constrain_Ordinary_Fixed (Def_Id, S);
12724 when Float_Kind =>
12725 Constrain_Float (Def_Id, S);
12727 when Integer_Kind =>
12728 Constrain_Integer (Def_Id, S);
12730 when E_Record_Type |
12731 E_Record_Subtype |
12732 Class_Wide_Kind |
12733 E_Incomplete_Type =>
12734 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
12736 when Private_Kind =>
12737 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
12738 Set_Private_Dependents (Def_Id, New_Elmt_List);
12740 -- In case of an invalid constraint prevent further processing
12741 -- since the type constructed is missing expected fields.
12743 if Etype (Def_Id) = Any_Type then
12744 return Def_Id;
12745 end if;
12747 -- If the full view is that of a task with discriminants,
12748 -- we must constrain both the concurrent type and its
12749 -- corresponding record type. Otherwise we will just propagate
12750 -- the constraint to the full view, if available.
12752 if Present (Full_View (Subtype_Mark_Id))
12753 and then Has_Discriminants (Subtype_Mark_Id)
12754 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
12755 then
12756 Full_View_Id :=
12757 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12759 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
12760 Constrain_Concurrent (Full_View_Id, S,
12761 Related_Nod, Related_Id, Suffix);
12762 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
12763 Set_Full_View (Def_Id, Full_View_Id);
12765 else
12766 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
12767 end if;
12769 when Concurrent_Kind =>
12770 Constrain_Concurrent (Def_Id, S,
12771 Related_Nod, Related_Id, Suffix);
12773 when others =>
12774 Error_Msg_N ("invalid subtype mark in subtype indication", S);
12775 end case;
12777 -- Size and Convention are always inherited from the base type
12779 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
12780 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
12782 return Def_Id;
12784 end if;
12785 end Process_Subtype;
12787 -----------------------------
12788 -- Record_Type_Declaration --
12789 -----------------------------
12791 procedure Record_Type_Declaration
12792 (T : Entity_Id;
12793 N : Node_Id;
12794 Prev : Entity_Id)
12796 Def : constant Node_Id := Type_Definition (N);
12798 Is_Tagged : Boolean;
12799 Tag_Comp : Entity_Id;
12801 begin
12802 -- The flag Is_Tagged_Type might have already been set by Find_Type_Name
12803 -- if it detected an error for declaration T. This arises in the case of
12804 -- private tagged types where the full view omits the word tagged.
12806 Is_Tagged := Tagged_Present (Def)
12807 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
12809 -- Records constitute a scope for the component declarations within.
12810 -- The scope is created prior to the processing of these declarations.
12811 -- Discriminants are processed first, so that they are visible when
12812 -- processing the other components. The Ekind of the record type itself
12813 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
12815 -- Enter record scope
12817 New_Scope (T);
12819 -- These flags must be initialized before calling Process_Discriminants
12820 -- because this routine makes use of them.
12822 Set_Is_Tagged_Type (T, Is_Tagged);
12823 Set_Is_Limited_Record (T, Limited_Present (Def));
12825 -- Type is abstract if full declaration carries keyword, or if
12826 -- previous partial view did.
12828 Set_Is_Abstract (T, Is_Abstract (T) or else Abstract_Present (Def));
12830 Set_Ekind (T, E_Record_Type);
12831 Set_Etype (T, T);
12832 Init_Size_Align (T);
12834 Set_Stored_Constraint (T, No_Elist);
12836 -- If an incomplete or private type declaration was already given for
12837 -- the type, then this scope already exists, and the discriminants have
12838 -- been declared within. We must verify that the full declaration
12839 -- matches the incomplete one.
12841 Check_Or_Process_Discriminants (N, T, Prev);
12843 Set_Is_Constrained (T, not Has_Discriminants (T));
12844 Set_Has_Delayed_Freeze (T, True);
12846 -- For tagged types add a manually analyzed component corresponding
12847 -- to the component _tag, the corresponding piece of tree will be
12848 -- expanded as part of the freezing actions if it is not a CPP_Class.
12850 if Is_Tagged then
12851 -- Do not add the tag unless we are in expansion mode.
12853 if Expander_Active then
12854 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
12855 Enter_Name (Tag_Comp);
12857 Set_Is_Tag (Tag_Comp);
12858 Set_Ekind (Tag_Comp, E_Component);
12859 Set_Etype (Tag_Comp, RTE (RE_Tag));
12860 Set_DT_Entry_Count (Tag_Comp, No_Uint);
12861 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
12862 Init_Component_Location (Tag_Comp);
12863 end if;
12865 Make_Class_Wide_Type (T);
12866 Set_Primitive_Operations (T, New_Elmt_List);
12867 end if;
12869 -- We must suppress range checks when processing the components
12870 -- of a record in the presence of discriminants, since we don't
12871 -- want spurious checks to be generated during their analysis, but
12872 -- must reset the Suppress_Range_Checks flags after having processed
12873 -- the record definition.
12875 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
12876 Set_Kill_Range_Checks (T, True);
12877 Record_Type_Definition (Def, Prev);
12878 Set_Kill_Range_Checks (T, False);
12879 else
12880 Record_Type_Definition (Def, Prev);
12881 end if;
12883 -- Exit from record scope
12885 End_Scope;
12886 end Record_Type_Declaration;
12888 ----------------------------
12889 -- Record_Type_Definition --
12890 ----------------------------
12892 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
12893 Component : Entity_Id;
12894 Ctrl_Components : Boolean := False;
12895 Final_Storage_Only : Boolean;
12896 T : Entity_Id;
12898 begin
12899 if Ekind (Prev_T) = E_Incomplete_Type then
12900 T := Full_View (Prev_T);
12901 else
12902 T := Prev_T;
12903 end if;
12905 Final_Storage_Only := not Is_Controlled (T);
12907 -- If the component list of a record type is defined by the reserved
12908 -- word null and there is no discriminant part, then the record type has
12909 -- no components and all records of the type are null records (RM 3.7)
12910 -- This procedure is also called to process the extension part of a
12911 -- record extension, in which case the current scope may have inherited
12912 -- components.
12914 if No (Def)
12915 or else No (Component_List (Def))
12916 or else Null_Present (Component_List (Def))
12917 then
12918 null;
12920 else
12921 Analyze_Declarations (Component_Items (Component_List (Def)));
12923 if Present (Variant_Part (Component_List (Def))) then
12924 Analyze (Variant_Part (Component_List (Def)));
12925 end if;
12926 end if;
12928 -- After completing the semantic analysis of the record definition,
12929 -- record components, both new and inherited, are accessible. Set
12930 -- their kind accordingly.
12932 Component := First_Entity (Current_Scope);
12933 while Present (Component) loop
12935 if Ekind (Component) = E_Void then
12936 Set_Ekind (Component, E_Component);
12937 Init_Component_Location (Component);
12938 end if;
12940 if Has_Task (Etype (Component)) then
12941 Set_Has_Task (T);
12942 end if;
12944 if Ekind (Component) /= E_Component then
12945 null;
12947 elsif Has_Controlled_Component (Etype (Component))
12948 or else (Chars (Component) /= Name_uParent
12949 and then Is_Controlled (Etype (Component)))
12950 then
12951 Set_Has_Controlled_Component (T, True);
12952 Final_Storage_Only := Final_Storage_Only
12953 and then Finalize_Storage_Only (Etype (Component));
12954 Ctrl_Components := True;
12955 end if;
12957 Next_Entity (Component);
12958 end loop;
12960 -- A type is Finalize_Storage_Only only if all its controlled
12961 -- components are so.
12963 if Ctrl_Components then
12964 Set_Finalize_Storage_Only (T, Final_Storage_Only);
12965 end if;
12967 -- Place reference to end record on the proper entity, which may
12968 -- be a partial view.
12970 if Present (Def) then
12971 Process_End_Label (Def, 'e', Prev_T);
12972 end if;
12973 end Record_Type_Definition;
12975 ------------------------
12976 -- Replace_Components --
12977 ------------------------
12979 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
12980 function Process (N : Node_Id) return Traverse_Result;
12982 -------------
12983 -- Process --
12984 -------------
12986 function Process (N : Node_Id) return Traverse_Result is
12987 Comp : Entity_Id;
12989 begin
12990 if Nkind (N) = N_Discriminant_Specification then
12991 Comp := First_Discriminant (Typ);
12993 while Present (Comp) loop
12994 if Chars (Comp) = Chars (Defining_Identifier (N)) then
12995 Set_Defining_Identifier (N, Comp);
12996 exit;
12997 end if;
12999 Next_Discriminant (Comp);
13000 end loop;
13002 elsif Nkind (N) = N_Component_Declaration then
13003 Comp := First_Component (Typ);
13005 while Present (Comp) loop
13006 if Chars (Comp) = Chars (Defining_Identifier (N)) then
13007 Set_Defining_Identifier (N, Comp);
13008 exit;
13009 end if;
13011 Next_Component (Comp);
13012 end loop;
13013 end if;
13015 return OK;
13016 end Process;
13018 procedure Replace is new Traverse_Proc (Process);
13020 -- Start of processing for Replace_Components
13022 begin
13023 Replace (Decl);
13024 end Replace_Components;
13026 -------------------------------
13027 -- Set_Completion_Referenced --
13028 -------------------------------
13030 procedure Set_Completion_Referenced (E : Entity_Id) is
13031 begin
13032 -- If in main unit, mark entity that is a completion as referenced,
13033 -- warnings go on the partial view when needed.
13035 if In_Extended_Main_Source_Unit (E) then
13036 Set_Referenced (E);
13037 end if;
13038 end Set_Completion_Referenced;
13040 ---------------------
13041 -- Set_Fixed_Range --
13042 ---------------------
13044 -- The range for fixed-point types is complicated by the fact that we
13045 -- do not know the exact end points at the time of the declaration. This
13046 -- is true for three reasons:
13048 -- A size clause may affect the fudging of the end-points
13049 -- A small clause may affect the values of the end-points
13050 -- We try to include the end-points if it does not affect the size
13052 -- This means that the actual end-points must be established at the
13053 -- point when the type is frozen. Meanwhile, we first narrow the range
13054 -- as permitted (so that it will fit if necessary in a small specified
13055 -- size), and then build a range subtree with these narrowed bounds.
13057 -- Set_Fixed_Range constructs the range from real literal values, and
13058 -- sets the range as the Scalar_Range of the given fixed-point type
13059 -- entity.
13061 -- The parent of this range is set to point to the entity so that it
13062 -- is properly hooked into the tree (unlike normal Scalar_Range entries
13063 -- for other scalar types, which are just pointers to the range in the
13064 -- original tree, this would otherwise be an orphan).
13066 -- The tree is left unanalyzed. When the type is frozen, the processing
13067 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
13068 -- analyzed, and uses this as an indication that it should complete
13069 -- work on the range (it will know the final small and size values).
13071 procedure Set_Fixed_Range
13072 (E : Entity_Id;
13073 Loc : Source_Ptr;
13074 Lo : Ureal;
13075 Hi : Ureal)
13077 S : constant Node_Id :=
13078 Make_Range (Loc,
13079 Low_Bound => Make_Real_Literal (Loc, Lo),
13080 High_Bound => Make_Real_Literal (Loc, Hi));
13082 begin
13083 Set_Scalar_Range (E, S);
13084 Set_Parent (S, E);
13085 end Set_Fixed_Range;
13087 ----------------------------------
13088 -- Set_Scalar_Range_For_Subtype --
13089 ----------------------------------
13091 procedure Set_Scalar_Range_For_Subtype
13092 (Def_Id : Entity_Id;
13093 R : Node_Id;
13094 Subt : Entity_Id)
13096 Kind : constant Entity_Kind := Ekind (Def_Id);
13097 begin
13098 Set_Scalar_Range (Def_Id, R);
13100 -- We need to link the range into the tree before resolving it so
13101 -- that types that are referenced, including importantly the subtype
13102 -- itself, are properly frozen (Freeze_Expression requires that the
13103 -- expression be properly linked into the tree). Of course if it is
13104 -- already linked in, then we do not disturb the current link.
13106 if No (Parent (R)) then
13107 Set_Parent (R, Def_Id);
13108 end if;
13110 -- Reset the kind of the subtype during analysis of the range, to
13111 -- catch possible premature use in the bounds themselves.
13113 Set_Ekind (Def_Id, E_Void);
13114 Process_Range_Expr_In_Decl (R, Subt);
13115 Set_Ekind (Def_Id, Kind);
13117 end Set_Scalar_Range_For_Subtype;
13119 --------------------------------------------------------
13120 -- Set_Stored_Constraint_From_Discriminant_Constraint --
13121 --------------------------------------------------------
13123 procedure Set_Stored_Constraint_From_Discriminant_Constraint
13124 (E : Entity_Id)
13126 begin
13127 -- Make sure set if encountered during
13128 -- Expand_To_Stored_Constraint
13130 Set_Stored_Constraint (E, No_Elist);
13132 -- Give it the right value
13134 if Is_Constrained (E) and then Has_Discriminants (E) then
13135 Set_Stored_Constraint (E,
13136 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
13137 end if;
13139 end Set_Stored_Constraint_From_Discriminant_Constraint;
13141 -------------------------------------
13142 -- Signed_Integer_Type_Declaration --
13143 -------------------------------------
13145 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13146 Implicit_Base : Entity_Id;
13147 Base_Typ : Entity_Id;
13148 Lo_Val : Uint;
13149 Hi_Val : Uint;
13150 Errs : Boolean := False;
13151 Lo : Node_Id;
13152 Hi : Node_Id;
13154 function Can_Derive_From (E : Entity_Id) return Boolean;
13155 -- Determine whether given bounds allow derivation from specified type
13157 procedure Check_Bound (Expr : Node_Id);
13158 -- Check bound to make sure it is integral and static. If not, post
13159 -- appropriate error message and set Errs flag
13161 ---------------------
13162 -- Can_Derive_From --
13163 ---------------------
13165 function Can_Derive_From (E : Entity_Id) return Boolean is
13166 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
13167 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
13169 begin
13170 -- Note we check both bounds against both end values, to deal with
13171 -- strange types like ones with a range of 0 .. -12341234.
13173 return Lo <= Lo_Val and then Lo_Val <= Hi
13174 and then
13175 Lo <= Hi_Val and then Hi_Val <= Hi;
13176 end Can_Derive_From;
13178 -----------------
13179 -- Check_Bound --
13180 -----------------
13182 procedure Check_Bound (Expr : Node_Id) is
13183 begin
13184 -- If a range constraint is used as an integer type definition, each
13185 -- bound of the range must be defined by a static expression of some
13186 -- integer type, but the two bounds need not have the same integer
13187 -- type (Negative bounds are allowed.) (RM 3.5.4)
13189 if not Is_Integer_Type (Etype (Expr)) then
13190 Error_Msg_N
13191 ("integer type definition bounds must be of integer type", Expr);
13192 Errs := True;
13194 elsif not Is_OK_Static_Expression (Expr) then
13195 Flag_Non_Static_Expr
13196 ("non-static expression used for integer type bound!", Expr);
13197 Errs := True;
13199 -- The bounds are folded into literals, and we set their type to be
13200 -- universal, to avoid typing difficulties: we cannot set the type
13201 -- of the literal to the new type, because this would be a forward
13202 -- reference for the back end, and if the original type is user-
13203 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
13205 else
13206 if Is_Entity_Name (Expr) then
13207 Fold_Uint (Expr, Expr_Value (Expr), True);
13208 end if;
13210 Set_Etype (Expr, Universal_Integer);
13211 end if;
13212 end Check_Bound;
13214 -- Start of processing for Signed_Integer_Type_Declaration
13216 begin
13217 -- Create an anonymous base type
13219 Implicit_Base :=
13220 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
13222 -- Analyze and check the bounds, they can be of any integer type
13224 Lo := Low_Bound (Def);
13225 Hi := High_Bound (Def);
13227 -- Arbitrarily use Integer as the type if either bound had an error
13229 if Hi = Error or else Lo = Error then
13230 Base_Typ := Any_Integer;
13231 Set_Error_Posted (T, True);
13233 -- Here both bounds are OK expressions
13235 else
13236 Analyze_And_Resolve (Lo, Any_Integer);
13237 Analyze_And_Resolve (Hi, Any_Integer);
13239 Check_Bound (Lo);
13240 Check_Bound (Hi);
13242 if Errs then
13243 Hi := Type_High_Bound (Standard_Long_Long_Integer);
13244 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
13245 end if;
13247 -- Find type to derive from
13249 Lo_Val := Expr_Value (Lo);
13250 Hi_Val := Expr_Value (Hi);
13252 if Can_Derive_From (Standard_Short_Short_Integer) then
13253 Base_Typ := Base_Type (Standard_Short_Short_Integer);
13255 elsif Can_Derive_From (Standard_Short_Integer) then
13256 Base_Typ := Base_Type (Standard_Short_Integer);
13258 elsif Can_Derive_From (Standard_Integer) then
13259 Base_Typ := Base_Type (Standard_Integer);
13261 elsif Can_Derive_From (Standard_Long_Integer) then
13262 Base_Typ := Base_Type (Standard_Long_Integer);
13264 elsif Can_Derive_From (Standard_Long_Long_Integer) then
13265 Base_Typ := Base_Type (Standard_Long_Long_Integer);
13267 else
13268 Base_Typ := Base_Type (Standard_Long_Long_Integer);
13269 Error_Msg_N ("integer type definition bounds out of range", Def);
13270 Hi := Type_High_Bound (Standard_Long_Long_Integer);
13271 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
13272 end if;
13273 end if;
13275 -- Complete both implicit base and declared first subtype entities
13277 Set_Etype (Implicit_Base, Base_Typ);
13278 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
13279 Set_Size_Info (Implicit_Base, (Base_Typ));
13280 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
13281 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13283 Set_Ekind (T, E_Signed_Integer_Subtype);
13284 Set_Etype (T, Implicit_Base);
13286 Set_Size_Info (T, (Implicit_Base));
13287 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13288 Set_Scalar_Range (T, Def);
13289 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
13290 Set_Is_Constrained (T);
13291 end Signed_Integer_Type_Declaration;
13293 end Sem_Ch3;