* gnu/regexp/CharIndexedReader.java: Removed.
[official-gcc.git] / gcc / ada / sem_ch3.adb
blob88480d8332b5c82376ee76a29174110479bd4c17
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 0Y (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 0Y (AI-231): Ada 0Y semantics for anonymous access differs from
709 -- Ada 95 semantics. In Ada 0Y, anonymous access must specify if the
710 -- null value is allowed; in Ada 95 the null value is not allowed
712 if Extensions_Allowed then
713 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
714 else
715 Set_Can_Never_Be_Null (Anon_Type, True);
716 end if;
718 -- The anonymous access type is as public as the discriminated type or
719 -- subprogram that defines it. It is imported (for back-end purposes)
720 -- if the designated type is.
722 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
724 -- Ada 0Y (AI-50217): Propagate the attribute that indicates that the
725 -- designated type comes from the limited view (for back-end purposes).
727 Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
729 -- Ada 0Y (AI-231): Propagate the access-constant attribute
731 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
733 -- The context is either a subprogram declaration or an access
734 -- discriminant, in a private or a full type declaration. In
735 -- the case of a subprogram, If the designated type is incomplete,
736 -- the operation will be a primitive operation of the full type, to
737 -- be updated subsequently. If the type is imported through a limited
738 -- with clause, it is not a primitive operation of the type (which
739 -- is declared elsewhere in some other scope).
741 if Ekind (Desig_Type) = E_Incomplete_Type
742 and then not From_With_Type (Desig_Type)
743 and then Is_Overloadable (Current_Scope)
744 then
745 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
746 Set_Has_Delayed_Freeze (Current_Scope);
747 end if;
749 return Anon_Type;
750 end Access_Definition;
752 -----------------------------------
753 -- Access_Subprogram_Declaration --
754 -----------------------------------
756 procedure Access_Subprogram_Declaration
757 (T_Name : Entity_Id;
758 T_Def : Node_Id)
760 Formals : constant List_Id := Parameter_Specifications (T_Def);
761 Formal : Entity_Id;
763 Desig_Type : constant Entity_Id :=
764 Create_Itype (E_Subprogram_Type, Parent (T_Def));
766 begin
767 if Nkind (T_Def) = N_Access_Function_Definition then
768 Analyze (Subtype_Mark (T_Def));
769 Set_Etype (Desig_Type, Entity (Subtype_Mark (T_Def)));
771 if not (Is_Type (Etype (Desig_Type))) then
772 Error_Msg_N
773 ("expect type in function specification", Subtype_Mark (T_Def));
774 end if;
776 else
777 Set_Etype (Desig_Type, Standard_Void_Type);
778 end if;
780 if Present (Formals) then
781 New_Scope (Desig_Type);
782 Process_Formals (Formals, Parent (T_Def));
784 -- A bit of a kludge here, End_Scope requires that the parent
785 -- pointer be set to something reasonable, but Itypes don't
786 -- have parent pointers. So we set it and then unset it ???
787 -- If and when Itypes have proper parent pointers to their
788 -- declarations, this kludge can be removed.
790 Set_Parent (Desig_Type, T_Name);
791 End_Scope;
792 Set_Parent (Desig_Type, Empty);
793 end if;
795 -- The return type and/or any parameter type may be incomplete. Mark
796 -- the subprogram_type as depending on the incomplete type, so that
797 -- it can be updated when the full type declaration is seen.
799 if Present (Formals) then
800 Formal := First_Formal (Desig_Type);
802 while Present (Formal) loop
804 if Ekind (Formal) /= E_In_Parameter
805 and then Nkind (T_Def) = N_Access_Function_Definition
806 then
807 Error_Msg_N ("functions can only have IN parameters", Formal);
808 end if;
810 if Ekind (Etype (Formal)) = E_Incomplete_Type then
811 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
812 Set_Has_Delayed_Freeze (Desig_Type);
813 end if;
815 Next_Formal (Formal);
816 end loop;
817 end if;
819 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
820 and then not Has_Delayed_Freeze (Desig_Type)
821 then
822 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
823 Set_Has_Delayed_Freeze (Desig_Type);
824 end if;
826 Check_Delayed_Subprogram (Desig_Type);
828 if Protected_Present (T_Def) then
829 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
830 Set_Convention (Desig_Type, Convention_Protected);
831 else
832 Set_Ekind (T_Name, E_Access_Subprogram_Type);
833 end if;
835 Set_Etype (T_Name, T_Name);
836 Init_Size_Align (T_Name);
837 Set_Directly_Designated_Type (T_Name, Desig_Type);
839 -- Ada 0Y (AI-231): Propagate the null-excluding attribute
841 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
843 Check_Restriction (No_Access_Subprograms, T_Def);
844 end Access_Subprogram_Declaration;
846 ----------------------------
847 -- Access_Type_Declaration --
848 ----------------------------
850 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
851 S : constant Node_Id := Subtype_Indication (Def);
852 P : constant Node_Id := Parent (Def);
854 Desig : Entity_Id;
855 -- Designated type
857 begin
858 -- Check for permissible use of incomplete type
860 if Nkind (S) /= N_Subtype_Indication then
861 Analyze (S);
863 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
864 Set_Directly_Designated_Type (T, Entity (S));
865 else
866 Set_Directly_Designated_Type (T,
867 Process_Subtype (S, P, T, 'P'));
868 end if;
870 else
871 Set_Directly_Designated_Type (T,
872 Process_Subtype (S, P, T, 'P'));
873 end if;
875 if All_Present (Def) or Constant_Present (Def) then
876 Set_Ekind (T, E_General_Access_Type);
877 else
878 Set_Ekind (T, E_Access_Type);
879 end if;
881 if Base_Type (Designated_Type (T)) = T then
882 Error_Msg_N ("access type cannot designate itself", S);
883 end if;
885 Set_Etype (T, T);
887 -- If the type has appeared already in a with_type clause, it is
888 -- frozen and the pointer size is already set. Else, initialize.
890 if not From_With_Type (T) then
891 Init_Size_Align (T);
892 end if;
894 Set_Is_Access_Constant (T, Constant_Present (Def));
896 Desig := Designated_Type (T);
898 -- If designated type is an imported tagged type, indicate that the
899 -- access type is also imported, and therefore restricted in its use.
900 -- The access type may already be imported, so keep setting otherwise.
902 -- Ada 0Y (AI-50217): If the non-limited view of the designated type is
903 -- available, use it as the designated type of the access type, so that
904 -- the back-end gets a usable entity.
906 declare
907 N_Desig : Entity_Id;
909 begin
910 if From_With_Type (Desig) then
911 Set_From_With_Type (T);
913 if Ekind (Desig) = E_Incomplete_Type then
914 N_Desig := Non_Limited_View (Desig);
916 else pragma Assert (Ekind (Desig) = E_Class_Wide_Type);
917 if From_With_Type (Etype (Desig)) then
918 N_Desig := Non_Limited_View (Etype (Desig));
919 else
920 N_Desig := Etype (Desig);
921 end if;
922 end if;
924 pragma Assert (Present (N_Desig));
925 Set_Directly_Designated_Type (T, N_Desig);
926 end if;
927 end;
929 -- Note that Has_Task is always false, since the access type itself
930 -- is not a task type. See Einfo for more description on this point.
931 -- Exactly the same consideration applies to Has_Controlled_Component.
933 Set_Has_Task (T, False);
934 Set_Has_Controlled_Component (T, False);
936 -- Ada 0Y (AI-231): Propagate the null-excluding and access-constant
937 -- attributes
939 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
940 Set_Is_Access_Constant (T, Constant_Present (Def));
941 end Access_Type_Declaration;
943 -----------------------------------
944 -- Analyze_Component_Declaration --
945 -----------------------------------
947 procedure Analyze_Component_Declaration (N : Node_Id) is
948 Id : constant Entity_Id := Defining_Identifier (N);
949 T : Entity_Id;
950 P : Entity_Id;
952 begin
953 Generate_Definition (Id);
954 Enter_Name (Id);
956 if Present (Subtype_Indication (Component_Definition (N))) then
957 T := Find_Type_Of_Object
958 (Subtype_Indication (Component_Definition (N)), N);
960 -- Ada 0Y (AI-230): Access Definition case
962 else
963 pragma Assert (Present
964 (Access_Definition (Component_Definition (N))));
966 T := Access_Definition
967 (Related_Nod => N,
968 N => Access_Definition (Component_Definition (N)));
970 -- Ada 0Y (AI-230): In case of components that are anonymous access
971 -- types the level of accessibility depends on the enclosing type
972 -- declaration
974 Set_Scope (T, Current_Scope); -- Ada 0Y (AI-230)
976 -- Ada 0Y (AI-254)
978 if Present (Access_To_Subprogram_Definition
979 (Access_Definition (Component_Definition (N))))
980 and then Protected_Present (Access_To_Subprogram_Definition
981 (Access_Definition
982 (Component_Definition (N))))
983 then
984 T := Replace_Anonymous_Access_To_Protected_Subprogram (N, T);
985 end if;
986 end if;
988 -- If the subtype is a constrained subtype of the enclosing record,
989 -- (which must have a partial view) the back-end does not handle
990 -- properly the recursion. Rewrite the component declaration with
991 -- an explicit subtype indication, which is acceptable to Gigi. We
992 -- can copy the tree directly because side effects have already been
993 -- removed from discriminant constraints.
995 if Ekind (T) = E_Access_Subtype
996 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
997 and then Comes_From_Source (T)
998 and then Nkind (Parent (T)) = N_Subtype_Declaration
999 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1000 then
1001 Rewrite
1002 (Subtype_Indication (Component_Definition (N)),
1003 New_Copy_Tree (Subtype_Indication (Parent (T))));
1004 T := Find_Type_Of_Object
1005 (Subtype_Indication (Component_Definition (N)), N);
1006 end if;
1008 -- If the component declaration includes a default expression, then we
1009 -- check that the component is not of a limited type (RM 3.7(5)),
1010 -- and do the special preanalysis of the expression (see section on
1011 -- "Handling of Default and Per-Object Expressions" in the spec of
1012 -- package Sem).
1014 if Present (Expression (N)) then
1015 Analyze_Per_Use_Expression (Expression (N), T);
1016 Check_Initialization (T, Expression (N));
1017 end if;
1019 -- The parent type may be a private view with unknown discriminants,
1020 -- and thus unconstrained. Regular components must be constrained.
1022 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1023 if Is_Class_Wide_Type (T) then
1024 Error_Msg_N
1025 ("class-wide subtype with unknown discriminants" &
1026 " in component declaration",
1027 Subtype_Indication (Component_Definition (N)));
1028 else
1029 Error_Msg_N
1030 ("unconstrained subtype in component declaration",
1031 Subtype_Indication (Component_Definition (N)));
1032 end if;
1034 -- Components cannot be abstract, except for the special case of
1035 -- the _Parent field (case of extending an abstract tagged type)
1037 elsif Is_Abstract (T) and then Chars (Id) /= Name_uParent then
1038 Error_Msg_N ("type of a component cannot be abstract", N);
1039 end if;
1041 Set_Etype (Id, T);
1042 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1044 -- Ada 0Y (AI-231): Propagate the null-excluding attribute and carry
1045 -- out some static checks
1047 if Extensions_Allowed
1048 and then (Null_Exclusion_Present (Component_Definition (N))
1049 or else Can_Never_Be_Null (T))
1050 then
1051 Set_Can_Never_Be_Null (Id);
1052 Null_Exclusion_Static_Checks (N);
1053 end if;
1055 -- If this component is private (or depends on a private type),
1056 -- flag the record type to indicate that some operations are not
1057 -- available.
1059 P := Private_Component (T);
1061 if Present (P) then
1062 -- Check for circular definitions.
1064 if P = Any_Type then
1065 Set_Etype (Id, Any_Type);
1067 -- There is a gap in the visibility of operations only if the
1068 -- component type is not defined in the scope of the record type.
1070 elsif Scope (P) = Scope (Current_Scope) then
1071 null;
1073 elsif Is_Limited_Type (P) then
1074 Set_Is_Limited_Composite (Current_Scope);
1076 else
1077 Set_Is_Private_Composite (Current_Scope);
1078 end if;
1079 end if;
1081 if P /= Any_Type
1082 and then Is_Limited_Type (T)
1083 and then Chars (Id) /= Name_uParent
1084 and then Is_Tagged_Type (Current_Scope)
1085 then
1086 if Is_Derived_Type (Current_Scope)
1087 and then not Is_Limited_Record (Root_Type (Current_Scope))
1088 then
1089 Error_Msg_N
1090 ("extension of nonlimited type cannot have limited components",
1092 Explain_Limited_Type (T, N);
1093 Set_Etype (Id, Any_Type);
1094 Set_Is_Limited_Composite (Current_Scope, False);
1096 elsif not Is_Derived_Type (Current_Scope)
1097 and then not Is_Limited_Record (Current_Scope)
1098 then
1099 Error_Msg_N
1100 ("nonlimited tagged type cannot have limited components", N);
1101 Explain_Limited_Type (T, N);
1102 Set_Etype (Id, Any_Type);
1103 Set_Is_Limited_Composite (Current_Scope, False);
1104 end if;
1105 end if;
1107 Set_Original_Record_Component (Id, Id);
1108 end Analyze_Component_Declaration;
1110 --------------------------
1111 -- Analyze_Declarations --
1112 --------------------------
1114 procedure Analyze_Declarations (L : List_Id) is
1115 D : Node_Id;
1116 Next_Node : Node_Id;
1117 Freeze_From : Entity_Id := Empty;
1119 procedure Adjust_D;
1120 -- Adjust D not to include implicit label declarations, since these
1121 -- have strange Sloc values that result in elaboration check problems.
1122 -- (They have the sloc of the label as found in the source, and that
1123 -- is ahead of the current declarative part).
1125 --------------
1126 -- Adjust_D --
1127 --------------
1129 procedure Adjust_D is
1130 begin
1131 while Present (Prev (D))
1132 and then Nkind (D) = N_Implicit_Label_Declaration
1133 loop
1134 Prev (D);
1135 end loop;
1136 end Adjust_D;
1138 -- Start of processing for Analyze_Declarations
1140 begin
1141 D := First (L);
1142 while Present (D) loop
1144 -- Complete analysis of declaration
1146 Analyze (D);
1147 Next_Node := Next (D);
1149 if No (Freeze_From) then
1150 Freeze_From := First_Entity (Current_Scope);
1151 end if;
1153 -- At the end of a declarative part, freeze remaining entities
1154 -- declared in it. The end of the visible declarations of a
1155 -- package specification is not the end of a declarative part
1156 -- if private declarations are present. The end of a package
1157 -- declaration is a freezing point only if it a library package.
1158 -- A task definition or protected type definition is not a freeze
1159 -- point either. Finally, we do not freeze entities in generic
1160 -- scopes, because there is no code generated for them and freeze
1161 -- nodes will be generated for the instance.
1163 -- The end of a package instantiation is not a freeze point, but
1164 -- for now we make it one, because the generic body is inserted
1165 -- (currently) immediately after. Generic instantiations will not
1166 -- be a freeze point once delayed freezing of bodies is implemented.
1167 -- (This is needed in any case for early instantiations ???).
1169 if No (Next_Node) then
1170 if Nkind (Parent (L)) = N_Component_List
1171 or else Nkind (Parent (L)) = N_Task_Definition
1172 or else Nkind (Parent (L)) = N_Protected_Definition
1173 then
1174 null;
1176 elsif Nkind (Parent (L)) /= N_Package_Specification then
1177 if Nkind (Parent (L)) = N_Package_Body then
1178 Freeze_From := First_Entity (Current_Scope);
1179 end if;
1181 Adjust_D;
1182 Freeze_All (Freeze_From, D);
1183 Freeze_From := Last_Entity (Current_Scope);
1185 elsif Scope (Current_Scope) /= Standard_Standard
1186 and then not Is_Child_Unit (Current_Scope)
1187 and then No (Generic_Parent (Parent (L)))
1188 then
1189 null;
1191 elsif L /= Visible_Declarations (Parent (L))
1192 or else No (Private_Declarations (Parent (L)))
1193 or else Is_Empty_List (Private_Declarations (Parent (L)))
1194 then
1195 Adjust_D;
1196 Freeze_All (Freeze_From, D);
1197 Freeze_From := Last_Entity (Current_Scope);
1198 end if;
1200 -- If next node is a body then freeze all types before the body.
1201 -- An exception occurs for expander generated bodies, which can
1202 -- be recognized by their already being analyzed. The expander
1203 -- ensures that all types needed by these bodies have been frozen
1204 -- but it is not necessary to freeze all types (and would be wrong
1205 -- since it would not correspond to an RM defined freeze point).
1207 elsif not Analyzed (Next_Node)
1208 and then (Nkind (Next_Node) = N_Subprogram_Body
1209 or else Nkind (Next_Node) = N_Entry_Body
1210 or else Nkind (Next_Node) = N_Package_Body
1211 or else Nkind (Next_Node) = N_Protected_Body
1212 or else Nkind (Next_Node) = N_Task_Body
1213 or else Nkind (Next_Node) in N_Body_Stub)
1214 then
1215 Adjust_D;
1216 Freeze_All (Freeze_From, D);
1217 Freeze_From := Last_Entity (Current_Scope);
1218 end if;
1220 D := Next_Node;
1221 end loop;
1222 end Analyze_Declarations;
1224 ----------------------------------
1225 -- Analyze_Incomplete_Type_Decl --
1226 ----------------------------------
1228 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1229 F : constant Boolean := Is_Pure (Current_Scope);
1230 T : Entity_Id;
1232 begin
1233 Generate_Definition (Defining_Identifier (N));
1235 -- Process an incomplete declaration. The identifier must not have been
1236 -- declared already in the scope. However, an incomplete declaration may
1237 -- appear in the private part of a package, for a private type that has
1238 -- already been declared.
1240 -- In this case, the discriminants (if any) must match.
1242 T := Find_Type_Name (N);
1244 Set_Ekind (T, E_Incomplete_Type);
1245 Init_Size_Align (T);
1246 Set_Is_First_Subtype (T, True);
1247 Set_Etype (T, T);
1248 New_Scope (T);
1250 Set_Stored_Constraint (T, No_Elist);
1252 if Present (Discriminant_Specifications (N)) then
1253 Process_Discriminants (N);
1254 end if;
1256 End_Scope;
1258 -- If the type has discriminants, non-trivial subtypes may be
1259 -- be declared before the full view of the type. The full views
1260 -- of those subtypes will be built after the full view of the type.
1262 Set_Private_Dependents (T, New_Elmt_List);
1263 Set_Is_Pure (T, F);
1264 end Analyze_Incomplete_Type_Decl;
1266 -----------------------------
1267 -- Analyze_Itype_Reference --
1268 -----------------------------
1270 -- Nothing to do. This node is placed in the tree only for the benefit
1271 -- of Gigi processing, and has no effect on the semantic processing.
1273 procedure Analyze_Itype_Reference (N : Node_Id) is
1274 begin
1275 pragma Assert (Is_Itype (Itype (N)));
1276 null;
1277 end Analyze_Itype_Reference;
1279 --------------------------------
1280 -- Analyze_Number_Declaration --
1281 --------------------------------
1283 procedure Analyze_Number_Declaration (N : Node_Id) is
1284 Id : constant Entity_Id := Defining_Identifier (N);
1285 E : constant Node_Id := Expression (N);
1286 T : Entity_Id;
1287 Index : Interp_Index;
1288 It : Interp;
1290 begin
1291 Generate_Definition (Id);
1292 Enter_Name (Id);
1294 -- This is an optimization of a common case of an integer literal
1296 if Nkind (E) = N_Integer_Literal then
1297 Set_Is_Static_Expression (E, True);
1298 Set_Etype (E, Universal_Integer);
1300 Set_Etype (Id, Universal_Integer);
1301 Set_Ekind (Id, E_Named_Integer);
1302 Set_Is_Frozen (Id, True);
1303 return;
1304 end if;
1306 Set_Is_Pure (Id, Is_Pure (Current_Scope));
1308 -- Process expression, replacing error by integer zero, to avoid
1309 -- cascaded errors or aborts further along in the processing
1311 -- Replace Error by integer zero, which seems least likely to
1312 -- cause cascaded errors.
1314 if E = Error then
1315 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
1316 Set_Error_Posted (E);
1317 end if;
1319 Analyze (E);
1321 -- Verify that the expression is static and numeric. If
1322 -- the expression is overloaded, we apply the preference
1323 -- rule that favors root numeric types.
1325 if not Is_Overloaded (E) then
1326 T := Etype (E);
1328 else
1329 T := Any_Type;
1330 Get_First_Interp (E, Index, It);
1332 while Present (It.Typ) loop
1333 if (Is_Integer_Type (It.Typ)
1334 or else Is_Real_Type (It.Typ))
1335 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
1336 then
1337 if T = Any_Type then
1338 T := It.Typ;
1340 elsif It.Typ = Universal_Real
1341 or else It.Typ = Universal_Integer
1342 then
1343 -- Choose universal interpretation over any other.
1345 T := It.Typ;
1346 exit;
1347 end if;
1348 end if;
1350 Get_Next_Interp (Index, It);
1351 end loop;
1352 end if;
1354 if Is_Integer_Type (T) then
1355 Resolve (E, T);
1356 Set_Etype (Id, Universal_Integer);
1357 Set_Ekind (Id, E_Named_Integer);
1359 elsif Is_Real_Type (T) then
1361 -- Because the real value is converted to universal_real, this
1362 -- is a legal context for a universal fixed expression.
1364 if T = Universal_Fixed then
1365 declare
1366 Loc : constant Source_Ptr := Sloc (N);
1367 Conv : constant Node_Id := Make_Type_Conversion (Loc,
1368 Subtype_Mark =>
1369 New_Occurrence_Of (Universal_Real, Loc),
1370 Expression => Relocate_Node (E));
1372 begin
1373 Rewrite (E, Conv);
1374 Analyze (E);
1375 end;
1377 elsif T = Any_Fixed then
1378 Error_Msg_N ("illegal context for mixed mode operation", E);
1380 -- Expression is of the form : universal_fixed * integer.
1381 -- Try to resolve as universal_real.
1383 T := Universal_Real;
1384 Set_Etype (E, T);
1385 end if;
1387 Resolve (E, T);
1388 Set_Etype (Id, Universal_Real);
1389 Set_Ekind (Id, E_Named_Real);
1391 else
1392 Wrong_Type (E, Any_Numeric);
1393 Resolve (E, T);
1395 Set_Etype (Id, T);
1396 Set_Ekind (Id, E_Constant);
1397 Set_Never_Set_In_Source (Id, True);
1398 Set_Is_True_Constant (Id, True);
1399 return;
1400 end if;
1402 if Nkind (E) = N_Integer_Literal
1403 or else Nkind (E) = N_Real_Literal
1404 then
1405 Set_Etype (E, Etype (Id));
1406 end if;
1408 if not Is_OK_Static_Expression (E) then
1409 Flag_Non_Static_Expr
1410 ("non-static expression used in number declaration!", E);
1411 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
1412 Set_Etype (E, Any_Type);
1413 end if;
1414 end Analyze_Number_Declaration;
1416 --------------------------------
1417 -- Analyze_Object_Declaration --
1418 --------------------------------
1420 procedure Analyze_Object_Declaration (N : Node_Id) is
1421 Loc : constant Source_Ptr := Sloc (N);
1422 Id : constant Entity_Id := Defining_Identifier (N);
1423 T : Entity_Id;
1424 Act_T : Entity_Id;
1426 E : Node_Id := Expression (N);
1427 -- E is set to Expression (N) throughout this routine. When
1428 -- Expression (N) is modified, E is changed accordingly.
1430 Prev_Entity : Entity_Id := Empty;
1432 function Build_Default_Subtype return Entity_Id;
1433 -- If the object is limited or aliased, and if the type is unconstrained
1434 -- and there is no expression, the discriminants cannot be modified and
1435 -- the subtype of the object is constrained by the defaults, so it is
1436 -- worthile building the corresponding subtype.
1438 function Count_Tasks (T : Entity_Id) return Uint;
1439 -- This function is called when a library level object of type T
1440 -- is declared. It's function is to count the static number of
1441 -- tasks declared within the type (it is only called if Has_Tasks
1442 -- is set for T). As a side effect, if an array of tasks with
1443 -- non-static bounds or a variant record type is encountered,
1444 -- Check_Restrictions is called indicating the count is unknown.
1446 ---------------------------
1447 -- Build_Default_Subtype --
1448 ---------------------------
1450 function Build_Default_Subtype return Entity_Id is
1451 Constraints : constant List_Id := New_List;
1452 Act : Entity_Id;
1453 Decl : Node_Id;
1454 Disc : Entity_Id;
1456 begin
1457 Disc := First_Discriminant (T);
1459 if No (Discriminant_Default_Value (Disc)) then
1460 return T; -- previous error.
1461 end if;
1463 Act := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
1464 while Present (Disc) loop
1465 Append (
1466 New_Copy_Tree (
1467 Discriminant_Default_Value (Disc)), Constraints);
1468 Next_Discriminant (Disc);
1469 end loop;
1471 Decl :=
1472 Make_Subtype_Declaration (Loc,
1473 Defining_Identifier => Act,
1474 Subtype_Indication =>
1475 Make_Subtype_Indication (Loc,
1476 Subtype_Mark => New_Occurrence_Of (T, Loc),
1477 Constraint =>
1478 Make_Index_Or_Discriminant_Constraint
1479 (Loc, Constraints)));
1481 Insert_Before (N, Decl);
1482 Analyze (Decl);
1483 return Act;
1484 end Build_Default_Subtype;
1486 -----------------
1487 -- Count_Tasks --
1488 -----------------
1490 function Count_Tasks (T : Entity_Id) return Uint is
1491 C : Entity_Id;
1492 X : Node_Id;
1493 V : Uint;
1495 begin
1496 if Is_Task_Type (T) then
1497 return Uint_1;
1499 elsif Is_Record_Type (T) then
1500 if Has_Discriminants (T) then
1501 Check_Restriction (Max_Tasks, N);
1502 return Uint_0;
1504 else
1505 V := Uint_0;
1506 C := First_Component (T);
1507 while Present (C) loop
1508 V := V + Count_Tasks (Etype (C));
1509 Next_Component (C);
1510 end loop;
1512 return V;
1513 end if;
1515 elsif Is_Array_Type (T) then
1516 X := First_Index (T);
1517 V := Count_Tasks (Component_Type (T));
1518 while Present (X) loop
1519 C := Etype (X);
1521 if not Is_Static_Subtype (C) then
1522 Check_Restriction (Max_Tasks, N);
1523 return Uint_0;
1524 else
1525 V := V * (UI_Max (Uint_0,
1526 Expr_Value (Type_High_Bound (C)) -
1527 Expr_Value (Type_Low_Bound (C)) + Uint_1));
1528 end if;
1530 Next_Index (X);
1531 end loop;
1533 return V;
1535 else
1536 return Uint_0;
1537 end if;
1538 end Count_Tasks;
1540 -- Start of processing for Analyze_Object_Declaration
1542 begin
1543 -- There are three kinds of implicit types generated by an
1544 -- object declaration:
1546 -- 1. Those for generated by the original Object Definition
1548 -- 2. Those generated by the Expression
1550 -- 3. Those used to constrained the Object Definition with the
1551 -- expression constraints when it is unconstrained
1553 -- They must be generated in this order to avoid order of elaboration
1554 -- issues. Thus the first step (after entering the name) is to analyze
1555 -- the object definition.
1557 if Constant_Present (N) then
1558 Prev_Entity := Current_Entity_In_Scope (Id);
1560 -- If homograph is an implicit subprogram, it is overridden by the
1561 -- current declaration.
1563 if Present (Prev_Entity)
1564 and then Is_Overloadable (Prev_Entity)
1565 and then Is_Inherited_Operation (Prev_Entity)
1566 then
1567 Prev_Entity := Empty;
1568 end if;
1569 end if;
1571 if Present (Prev_Entity) then
1572 Constant_Redeclaration (Id, N, T);
1574 Generate_Reference (Prev_Entity, Id, 'c');
1575 Set_Completion_Referenced (Id);
1577 if Error_Posted (N) then
1578 -- Type mismatch or illegal redeclaration, Do not analyze
1579 -- expression to avoid cascaded errors.
1581 T := Find_Type_Of_Object (Object_Definition (N), N);
1582 Set_Etype (Id, T);
1583 Set_Ekind (Id, E_Variable);
1584 return;
1585 end if;
1587 -- In the normal case, enter identifier at the start to catch
1588 -- premature usage in the initialization expression.
1590 else
1591 Generate_Definition (Id);
1592 Enter_Name (Id);
1594 T := Find_Type_Of_Object (Object_Definition (N), N);
1596 if Error_Posted (Id) then
1597 Set_Etype (Id, T);
1598 Set_Ekind (Id, E_Variable);
1599 return;
1600 end if;
1601 end if;
1603 -- Ada 0Y (AI-231): Propagate the null-excluding attribute and carry
1604 -- out some static checks
1606 if Extensions_Allowed
1607 and then (Null_Exclusion_Present (N)
1608 or else Can_Never_Be_Null (T))
1609 then
1610 Set_Can_Never_Be_Null (Id);
1611 Null_Exclusion_Static_Checks (N);
1612 end if;
1614 Set_Is_Pure (Id, Is_Pure (Current_Scope));
1616 -- If deferred constant, make sure context is appropriate. We detect
1617 -- a deferred constant as a constant declaration with no expression.
1618 -- A deferred constant can appear in a package body if its completion
1619 -- is by means of an interface pragma.
1621 if Constant_Present (N)
1622 and then No (E)
1623 then
1624 if not Is_Package (Current_Scope) then
1625 Error_Msg_N
1626 ("invalid context for deferred constant declaration ('R'M 7.4)",
1628 Error_Msg_N
1629 ("\declaration requires an initialization expression",
1631 Set_Constant_Present (N, False);
1633 -- In Ada 83, deferred constant must be of private type
1635 elsif not Is_Private_Type (T) then
1636 if Ada_83 and then Comes_From_Source (N) then
1637 Error_Msg_N
1638 ("(Ada 83) deferred constant must be private type", N);
1639 end if;
1640 end if;
1642 -- If not a deferred constant, then object declaration freezes its type
1644 else
1645 Check_Fully_Declared (T, N);
1646 Freeze_Before (N, T);
1647 end if;
1649 -- If the object was created by a constrained array definition, then
1650 -- set the link in both the anonymous base type and anonymous subtype
1651 -- that are built to represent the array type to point to the object.
1653 if Nkind (Object_Definition (Declaration_Node (Id))) =
1654 N_Constrained_Array_Definition
1655 then
1656 Set_Related_Array_Object (T, Id);
1657 Set_Related_Array_Object (Base_Type (T), Id);
1658 end if;
1660 -- Special checks for protected objects not at library level
1662 if Is_Protected_Type (T)
1663 and then not Is_Library_Level_Entity (Id)
1664 then
1665 Check_Restriction (No_Local_Protected_Objects, Id);
1667 -- Protected objects with interrupt handlers must be at library level
1669 if Has_Interrupt_Handler (T) then
1670 Error_Msg_N
1671 ("interrupt object can only be declared at library level", Id);
1672 end if;
1673 end if;
1675 -- The actual subtype of the object is the nominal subtype, unless
1676 -- the nominal one is unconstrained and obtained from the expression.
1678 Act_T := T;
1680 -- Process initialization expression if present and not in error
1682 if Present (E) and then E /= Error then
1683 Analyze (E);
1685 -- In case of errors detected in the analysis of the expression,
1686 -- decorate it with the expected type to avoid cascade errors
1688 if not Present (Etype (E)) then
1689 Set_Etype (E, T);
1690 end if;
1692 -- If an initialization expression is present, then we set the
1693 -- Is_True_Constant flag. It will be reset if this is a variable
1694 -- and it is indeed modified.
1696 Set_Is_True_Constant (Id, True);
1698 if not Assignment_OK (N) then
1699 Check_Initialization (T, E);
1700 end if;
1702 Set_Etype (Id, T); -- may be overridden later on.
1703 Resolve (E, T);
1704 Check_Unset_Reference (E);
1706 if Compile_Time_Known_Value (E) then
1707 Set_Current_Value (Id, E);
1708 end if;
1710 -- Check incorrect use of dynamically tagged expressions. Note
1711 -- the use of Is_Tagged_Type (T) which seems redundant but is in
1712 -- fact important to avoid spurious errors due to expanded code
1713 -- for dispatching functions over an anonymous access type
1715 if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
1716 and then Is_Tagged_Type (T)
1717 and then not Is_Class_Wide_Type (T)
1718 then
1719 Error_Msg_N ("dynamically tagged expression not allowed!", E);
1720 end if;
1722 Apply_Scalar_Range_Check (E, T);
1723 Apply_Static_Length_Check (E, T);
1724 end if;
1726 -- Abstract type is never permitted for a variable or constant.
1727 -- Note: we inhibit this check for objects that do not come from
1728 -- source because there is at least one case (the expansion of
1729 -- x'class'input where x is abstract) where we legitimately
1730 -- generate an abstract object.
1732 if Is_Abstract (T) and then Comes_From_Source (N) then
1733 Error_Msg_N ("type of object cannot be abstract",
1734 Object_Definition (N));
1735 if Is_CPP_Class (T) then
1736 Error_Msg_NE ("\} may need a cpp_constructor",
1737 Object_Definition (N), T);
1738 end if;
1740 -- Case of unconstrained type
1742 elsif Is_Indefinite_Subtype (T) then
1744 -- Nothing to do in deferred constant case
1746 if Constant_Present (N) and then No (E) then
1747 null;
1749 -- Case of no initialization present
1751 elsif No (E) then
1752 if No_Initialization (N) then
1753 null;
1755 elsif Is_Class_Wide_Type (T) then
1756 Error_Msg_N
1757 ("initialization required in class-wide declaration ", N);
1759 else
1760 Error_Msg_N
1761 ("unconstrained subtype not allowed (need initialization)",
1762 Object_Definition (N));
1763 end if;
1765 -- Case of initialization present but in error. Set initial
1766 -- expression as absent (but do not make above complaints)
1768 elsif E = Error then
1769 Set_Expression (N, Empty);
1770 E := Empty;
1772 -- Case of initialization present
1774 else
1775 -- Not allowed in Ada 83
1777 if not Constant_Present (N) then
1778 if Ada_83
1779 and then Comes_From_Source (Object_Definition (N))
1780 then
1781 Error_Msg_N
1782 ("(Ada 83) unconstrained variable not allowed",
1783 Object_Definition (N));
1784 end if;
1785 end if;
1787 -- Now we constrain the variable from the initializing expression
1789 -- If the expression is an aggregate, it has been expanded into
1790 -- individual assignments. Retrieve the actual type from the
1791 -- expanded construct.
1793 if Is_Array_Type (T)
1794 and then No_Initialization (N)
1795 and then Nkind (Original_Node (E)) = N_Aggregate
1796 then
1797 Act_T := Etype (E);
1799 else
1800 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
1801 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
1802 end if;
1804 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
1806 if Aliased_Present (N) then
1807 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
1808 end if;
1810 Freeze_Before (N, Act_T);
1811 Freeze_Before (N, T);
1812 end if;
1814 elsif Is_Array_Type (T)
1815 and then No_Initialization (N)
1816 and then Nkind (Original_Node (E)) = N_Aggregate
1817 then
1818 if not Is_Entity_Name (Object_Definition (N)) then
1819 Act_T := Etype (E);
1820 Check_Compile_Time_Size (Act_T);
1822 if Aliased_Present (N) then
1823 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
1824 end if;
1825 end if;
1827 -- When the given object definition and the aggregate are specified
1828 -- independently, and their lengths might differ do a length check.
1829 -- This cannot happen if the aggregate is of the form (others =>...)
1831 if not Is_Constrained (T) then
1832 null;
1834 elsif Nkind (E) = N_Raise_Constraint_Error then
1836 -- Aggregate is statically illegal. Place back in declaration
1838 Set_Expression (N, E);
1839 Set_No_Initialization (N, False);
1841 elsif T = Etype (E) then
1842 null;
1844 elsif Nkind (E) = N_Aggregate
1845 and then Present (Component_Associations (E))
1846 and then Present (Choices (First (Component_Associations (E))))
1847 and then Nkind (First
1848 (Choices (First (Component_Associations (E))))) = N_Others_Choice
1849 then
1850 null;
1852 else
1853 Apply_Length_Check (E, T);
1854 end if;
1856 elsif (Is_Limited_Record (T)
1857 or else Is_Concurrent_Type (T))
1858 and then not Is_Constrained (T)
1859 and then Has_Discriminants (T)
1860 then
1861 Act_T := Build_Default_Subtype;
1862 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
1864 elsif not Is_Constrained (T)
1865 and then Has_Discriminants (T)
1866 and then Constant_Present (N)
1867 and then Nkind (E) = N_Function_Call
1868 then
1869 -- The back-end has problems with constants of a discriminated type
1870 -- with defaults, if the initial value is a function call. We
1871 -- generate an intermediate temporary for the result of the call.
1872 -- It is unclear why this should make it acceptable to gcc. ???
1874 Remove_Side_Effects (E);
1875 end if;
1877 if T = Standard_Wide_Character
1878 or else Root_Type (T) = Standard_Wide_String
1879 then
1880 Check_Restriction (No_Wide_Characters, Object_Definition (N));
1881 end if;
1883 -- Now establish the proper kind and type of the object
1885 if Constant_Present (N) then
1886 Set_Ekind (Id, E_Constant);
1887 Set_Never_Set_In_Source (Id, True);
1888 Set_Is_True_Constant (Id, True);
1890 else
1891 Set_Ekind (Id, E_Variable);
1893 -- A variable is set as shared passive if it appears in a shared
1894 -- passive package, and is at the outer level. This is not done
1895 -- for entities generated during expansion, because those are
1896 -- always manipulated locally.
1898 if Is_Shared_Passive (Current_Scope)
1899 and then Is_Library_Level_Entity (Id)
1900 and then Comes_From_Source (Id)
1901 then
1902 Set_Is_Shared_Passive (Id);
1903 Check_Shared_Var (Id, T, N);
1904 end if;
1906 -- Case of no initializing expression present. If the type is not
1907 -- fully initialized, then we set Never_Set_In_Source, since this
1908 -- is a case of a potentially uninitialized object. Note that we
1909 -- do not consider access variables to be fully initialized for
1910 -- this purpose, since it still seems dubious if someone declares
1912 -- Note that we only do this for source declarations. If the object
1913 -- is declared by a generated declaration, we assume that it is not
1914 -- appropriate to generate warnings in that case.
1916 if No (E) then
1917 if (Is_Access_Type (T)
1918 or else not Is_Fully_Initialized_Type (T))
1919 and then Comes_From_Source (N)
1920 then
1921 Set_Never_Set_In_Source (Id);
1922 end if;
1923 end if;
1924 end if;
1926 Init_Alignment (Id);
1927 Init_Esize (Id);
1929 if Aliased_Present (N) then
1930 Set_Is_Aliased (Id);
1932 if No (E)
1933 and then Is_Record_Type (T)
1934 and then not Is_Constrained (T)
1935 and then Has_Discriminants (T)
1936 then
1937 Set_Actual_Subtype (Id, Build_Default_Subtype);
1938 end if;
1939 end if;
1941 Set_Etype (Id, Act_T);
1943 if Has_Controlled_Component (Etype (Id))
1944 or else Is_Controlled (Etype (Id))
1945 then
1946 if not Is_Library_Level_Entity (Id) then
1947 Check_Restriction (No_Nested_Finalization, N);
1949 else
1950 Validate_Controlled_Object (Id);
1951 end if;
1953 -- Generate a warning when an initialization causes an obvious
1954 -- ABE violation. If the init expression is a simple aggregate
1955 -- there shouldn't be any initialize/adjust call generated. This
1956 -- will be true as soon as aggregates are built in place when
1957 -- possible. ??? at the moment we do not generate warnings for
1958 -- temporaries created for those aggregates although a
1959 -- Program_Error might be generated if compiled with -gnato
1961 if Is_Controlled (Etype (Id))
1962 and then Comes_From_Source (Id)
1963 then
1964 declare
1965 BT : constant Entity_Id := Base_Type (Etype (Id));
1967 Implicit_Call : Entity_Id;
1968 pragma Warnings (Off, Implicit_Call);
1969 -- What is this about, it is never referenced ???
1971 function Is_Aggr (N : Node_Id) return Boolean;
1972 -- Check that N is an aggregate
1974 -------------
1975 -- Is_Aggr --
1976 -------------
1978 function Is_Aggr (N : Node_Id) return Boolean is
1979 begin
1980 case Nkind (Original_Node (N)) is
1981 when N_Aggregate | N_Extension_Aggregate =>
1982 return True;
1984 when N_Qualified_Expression |
1985 N_Type_Conversion |
1986 N_Unchecked_Type_Conversion =>
1987 return Is_Aggr (Expression (Original_Node (N)));
1989 when others =>
1990 return False;
1991 end case;
1992 end Is_Aggr;
1994 begin
1995 -- If no underlying type, we already are in an error situation
1996 -- don't try to add a warning since we do not have access
1997 -- prim-op list.
1999 if No (Underlying_Type (BT)) then
2000 Implicit_Call := Empty;
2002 -- A generic type does not have usable primitive operators.
2003 -- Initialization calls are built for instances.
2005 elsif Is_Generic_Type (BT) then
2006 Implicit_Call := Empty;
2008 -- if the init expression is not an aggregate, an adjust
2009 -- call will be generated
2011 elsif Present (E) and then not Is_Aggr (E) then
2012 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2014 -- if no init expression and we are not in the deferred
2015 -- constant case, an Initialize call will be generated
2017 elsif No (E) and then not Constant_Present (N) then
2018 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2020 else
2021 Implicit_Call := Empty;
2022 end if;
2023 end;
2024 end if;
2025 end if;
2027 if Has_Task (Etype (Id)) then
2028 Check_Restriction (No_Tasking, N);
2030 if Is_Library_Level_Entity (Id) then
2031 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2033 else
2034 Check_Restriction (Max_Tasks, N);
2035 Check_Restriction (No_Task_Hierarchy, N);
2036 Check_Potentially_Blocking_Operation (N);
2037 end if;
2039 -- A rather specialized test. If we see two tasks being declared
2040 -- of the same type in the same object declaration, and the task
2041 -- has an entry with an address clause, we know that program error
2042 -- will be raised at run-time since we can't have two tasks with
2043 -- entries at the same address.
2045 if Is_Task_Type (Etype (Id))
2046 and then More_Ids (N)
2047 then
2048 declare
2049 E : Entity_Id;
2051 begin
2052 E := First_Entity (Etype (Id));
2053 while Present (E) loop
2054 if Ekind (E) = E_Entry
2055 and then Present (Get_Attribute_Definition_Clause
2056 (E, Attribute_Address))
2057 then
2058 Error_Msg_N
2059 ("?more than one task with same entry address", N);
2060 Error_Msg_N
2061 ("\?Program_Error will be raised at run time", N);
2062 Insert_Action (N,
2063 Make_Raise_Program_Error (Loc,
2064 Reason => PE_Duplicated_Entry_Address));
2065 exit;
2066 end if;
2068 Next_Entity (E);
2069 end loop;
2070 end;
2071 end if;
2072 end if;
2074 -- Some simple constant-propagation: if the expression is a constant
2075 -- string initialized with a literal, share the literal. This avoids
2076 -- a run-time copy.
2078 if Present (E)
2079 and then Is_Entity_Name (E)
2080 and then Ekind (Entity (E)) = E_Constant
2081 and then Base_Type (Etype (E)) = Standard_String
2082 then
2083 declare
2084 Val : constant Node_Id := Constant_Value (Entity (E));
2086 begin
2087 if Present (Val)
2088 and then Nkind (Val) = N_String_Literal
2089 then
2090 Rewrite (E, New_Copy (Val));
2091 end if;
2092 end;
2093 end if;
2095 -- Another optimization: if the nominal subtype is unconstrained and
2096 -- the expression is a function call that returns an unconstrained
2097 -- type, rewrite the declaration as a renaming of the result of the
2098 -- call. The exceptions below are cases where the copy is expected,
2099 -- either by the back end (Aliased case) or by the semantics, as for
2100 -- initializing controlled types or copying tags for classwide types.
2102 if Present (E)
2103 and then Nkind (E) = N_Explicit_Dereference
2104 and then Nkind (Original_Node (E)) = N_Function_Call
2105 and then not Is_Library_Level_Entity (Id)
2106 and then not Is_Constrained (T)
2107 and then not Is_Aliased (Id)
2108 and then not Is_Class_Wide_Type (T)
2109 and then not Is_Controlled (T)
2110 and then not Has_Controlled_Component (Base_Type (T))
2111 and then Expander_Active
2112 then
2113 Rewrite (N,
2114 Make_Object_Renaming_Declaration (Loc,
2115 Defining_Identifier => Id,
2116 Access_Definition => Empty,
2117 Subtype_Mark => New_Occurrence_Of
2118 (Base_Type (Etype (Id)), Loc),
2119 Name => E));
2121 Set_Renamed_Object (Id, E);
2123 -- Force generation of debugging information for the constant
2124 -- and for the renamed function call.
2126 Set_Needs_Debug_Info (Id);
2127 Set_Needs_Debug_Info (Entity (Prefix (E)));
2128 end if;
2130 if Present (Prev_Entity)
2131 and then Is_Frozen (Prev_Entity)
2132 and then not Error_Posted (Id)
2133 then
2134 Error_Msg_N ("full constant declaration appears too late", N);
2135 end if;
2137 Check_Eliminated (Id);
2138 end Analyze_Object_Declaration;
2140 ---------------------------
2141 -- Analyze_Others_Choice --
2142 ---------------------------
2144 -- Nothing to do for the others choice node itself, the semantic analysis
2145 -- of the others choice will occur as part of the processing of the parent
2147 procedure Analyze_Others_Choice (N : Node_Id) is
2148 pragma Warnings (Off, N);
2150 begin
2151 null;
2152 end Analyze_Others_Choice;
2154 --------------------------------
2155 -- Analyze_Per_Use_Expression --
2156 --------------------------------
2158 procedure Analyze_Per_Use_Expression (N : Node_Id; T : Entity_Id) is
2159 Save_In_Default_Expression : constant Boolean := In_Default_Expression;
2161 begin
2162 In_Default_Expression := True;
2163 Pre_Analyze_And_Resolve (N, T);
2164 In_Default_Expression := Save_In_Default_Expression;
2165 end Analyze_Per_Use_Expression;
2167 -------------------------------------------
2168 -- Analyze_Private_Extension_Declaration --
2169 -------------------------------------------
2171 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
2172 T : constant Entity_Id := Defining_Identifier (N);
2173 Indic : constant Node_Id := Subtype_Indication (N);
2174 Parent_Type : Entity_Id;
2175 Parent_Base : Entity_Id;
2177 begin
2178 Generate_Definition (T);
2179 Enter_Name (T);
2181 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
2182 Parent_Base := Base_Type (Parent_Type);
2184 if Parent_Type = Any_Type
2185 or else Etype (Parent_Type) = Any_Type
2186 then
2187 Set_Ekind (T, Ekind (Parent_Type));
2188 Set_Etype (T, Any_Type);
2189 return;
2191 elsif not Is_Tagged_Type (Parent_Type) then
2192 Error_Msg_N
2193 ("parent of type extension must be a tagged type ", Indic);
2194 return;
2196 elsif Ekind (Parent_Type) = E_Void
2197 or else Ekind (Parent_Type) = E_Incomplete_Type
2198 then
2199 Error_Msg_N ("premature derivation of incomplete type", Indic);
2200 return;
2201 end if;
2203 -- Perhaps the parent type should be changed to the class-wide type's
2204 -- specific type in this case to prevent cascading errors ???
2206 if Is_Class_Wide_Type (Parent_Type) then
2207 Error_Msg_N
2208 ("parent of type extension must not be a class-wide type", Indic);
2209 return;
2210 end if;
2212 if (not Is_Package (Current_Scope)
2213 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
2214 or else In_Private_Part (Current_Scope)
2216 then
2217 Error_Msg_N ("invalid context for private extension", N);
2218 end if;
2220 -- Set common attributes
2222 Set_Is_Pure (T, Is_Pure (Current_Scope));
2223 Set_Scope (T, Current_Scope);
2224 Set_Ekind (T, E_Record_Type_With_Private);
2225 Init_Size_Align (T);
2227 Set_Etype (T, Parent_Base);
2228 Set_Has_Task (T, Has_Task (Parent_Base));
2230 Set_Convention (T, Convention (Parent_Type));
2231 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
2232 Set_Is_First_Subtype (T);
2233 Make_Class_Wide_Type (T);
2235 if Unknown_Discriminants_Present (N) then
2236 Set_Discriminant_Constraint (T, No_Elist);
2237 end if;
2239 Build_Derived_Record_Type (N, Parent_Type, T);
2240 end Analyze_Private_Extension_Declaration;
2242 ---------------------------------
2243 -- Analyze_Subtype_Declaration --
2244 ---------------------------------
2246 procedure Analyze_Subtype_Declaration (N : Node_Id) is
2247 Id : constant Entity_Id := Defining_Identifier (N);
2248 T : Entity_Id;
2249 R_Checks : Check_Result;
2251 begin
2252 Generate_Definition (Id);
2253 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2254 Init_Size_Align (Id);
2256 -- The following guard condition on Enter_Name is to handle cases
2257 -- where the defining identifier has already been entered into the
2258 -- scope but the declaration as a whole needs to be analyzed.
2260 -- This case in particular happens for derived enumeration types.
2261 -- The derived enumeration type is processed as an inserted enumeration
2262 -- type declaration followed by a rewritten subtype declaration. The
2263 -- defining identifier, however, is entered into the name scope very
2264 -- early in the processing of the original type declaration and
2265 -- therefore needs to be avoided here, when the created subtype
2266 -- declaration is analyzed. (See Build_Derived_Types)
2268 -- This also happens when the full view of a private type is a
2269 -- derived type with constraints. In this case the entity has been
2270 -- introduced in the private declaration.
2272 if Present (Etype (Id))
2273 and then (Is_Private_Type (Etype (Id))
2274 or else Is_Task_Type (Etype (Id))
2275 or else Is_Rewrite_Substitution (N))
2276 then
2277 null;
2279 else
2280 Enter_Name (Id);
2281 end if;
2283 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
2285 -- Inherit common attributes
2287 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
2288 Set_Is_Volatile (Id, Is_Volatile (T));
2289 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
2290 Set_Is_Atomic (Id, Is_Atomic (T));
2292 -- In the case where there is no constraint given in the subtype
2293 -- indication, Process_Subtype just returns the Subtype_Mark,
2294 -- so its semantic attributes must be established here.
2296 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
2297 Set_Etype (Id, Base_Type (T));
2299 case Ekind (T) is
2300 when Array_Kind =>
2301 Set_Ekind (Id, E_Array_Subtype);
2302 Copy_Array_Subtype_Attributes (Id, T);
2304 when Decimal_Fixed_Point_Kind =>
2305 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
2306 Set_Digits_Value (Id, Digits_Value (T));
2307 Set_Delta_Value (Id, Delta_Value (T));
2308 Set_Scale_Value (Id, Scale_Value (T));
2309 Set_Small_Value (Id, Small_Value (T));
2310 Set_Scalar_Range (Id, Scalar_Range (T));
2311 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
2312 Set_Is_Constrained (Id, Is_Constrained (T));
2313 Set_RM_Size (Id, RM_Size (T));
2315 when Enumeration_Kind =>
2316 Set_Ekind (Id, E_Enumeration_Subtype);
2317 Set_First_Literal (Id, First_Literal (Base_Type (T)));
2318 Set_Scalar_Range (Id, Scalar_Range (T));
2319 Set_Is_Character_Type (Id, Is_Character_Type (T));
2320 Set_Is_Constrained (Id, Is_Constrained (T));
2321 Set_RM_Size (Id, RM_Size (T));
2323 when Ordinary_Fixed_Point_Kind =>
2324 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
2325 Set_Scalar_Range (Id, Scalar_Range (T));
2326 Set_Small_Value (Id, Small_Value (T));
2327 Set_Delta_Value (Id, Delta_Value (T));
2328 Set_Is_Constrained (Id, Is_Constrained (T));
2329 Set_RM_Size (Id, RM_Size (T));
2331 when Float_Kind =>
2332 Set_Ekind (Id, E_Floating_Point_Subtype);
2333 Set_Scalar_Range (Id, Scalar_Range (T));
2334 Set_Digits_Value (Id, Digits_Value (T));
2335 Set_Is_Constrained (Id, Is_Constrained (T));
2337 when Signed_Integer_Kind =>
2338 Set_Ekind (Id, E_Signed_Integer_Subtype);
2339 Set_Scalar_Range (Id, Scalar_Range (T));
2340 Set_Is_Constrained (Id, Is_Constrained (T));
2341 Set_RM_Size (Id, RM_Size (T));
2343 when Modular_Integer_Kind =>
2344 Set_Ekind (Id, E_Modular_Integer_Subtype);
2345 Set_Scalar_Range (Id, Scalar_Range (T));
2346 Set_Is_Constrained (Id, Is_Constrained (T));
2347 Set_RM_Size (Id, RM_Size (T));
2349 when Class_Wide_Kind =>
2350 Set_Ekind (Id, E_Class_Wide_Subtype);
2351 Set_First_Entity (Id, First_Entity (T));
2352 Set_Last_Entity (Id, Last_Entity (T));
2353 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
2354 Set_Cloned_Subtype (Id, T);
2355 Set_Is_Tagged_Type (Id, True);
2356 Set_Has_Unknown_Discriminants
2357 (Id, True);
2359 if Ekind (T) = E_Class_Wide_Subtype then
2360 Set_Equivalent_Type (Id, Equivalent_Type (T));
2361 end if;
2363 when E_Record_Type | E_Record_Subtype =>
2364 Set_Ekind (Id, E_Record_Subtype);
2366 if Ekind (T) = E_Record_Subtype
2367 and then Present (Cloned_Subtype (T))
2368 then
2369 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
2370 else
2371 Set_Cloned_Subtype (Id, T);
2372 end if;
2374 Set_First_Entity (Id, First_Entity (T));
2375 Set_Last_Entity (Id, Last_Entity (T));
2376 Set_Has_Discriminants (Id, Has_Discriminants (T));
2377 Set_Is_Constrained (Id, Is_Constrained (T));
2378 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
2379 Set_Has_Unknown_Discriminants
2380 (Id, Has_Unknown_Discriminants (T));
2382 if Has_Discriminants (T) then
2383 Set_Discriminant_Constraint
2384 (Id, Discriminant_Constraint (T));
2385 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2387 elsif Has_Unknown_Discriminants (Id) then
2388 Set_Discriminant_Constraint (Id, No_Elist);
2389 end if;
2391 if Is_Tagged_Type (T) then
2392 Set_Is_Tagged_Type (Id);
2393 Set_Is_Abstract (Id, Is_Abstract (T));
2394 Set_Primitive_Operations
2395 (Id, Primitive_Operations (T));
2396 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
2397 end if;
2399 when Private_Kind =>
2400 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
2401 Set_Has_Discriminants (Id, Has_Discriminants (T));
2402 Set_Is_Constrained (Id, Is_Constrained (T));
2403 Set_First_Entity (Id, First_Entity (T));
2404 Set_Last_Entity (Id, Last_Entity (T));
2405 Set_Private_Dependents (Id, New_Elmt_List);
2406 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
2407 Set_Has_Unknown_Discriminants
2408 (Id, Has_Unknown_Discriminants (T));
2410 if Is_Tagged_Type (T) then
2411 Set_Is_Tagged_Type (Id);
2412 Set_Is_Abstract (Id, Is_Abstract (T));
2413 Set_Primitive_Operations
2414 (Id, Primitive_Operations (T));
2415 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
2416 end if;
2418 -- In general the attributes of the subtype of a private
2419 -- type are the attributes of the partial view of parent.
2420 -- However, the full view may be a discriminated type,
2421 -- and the subtype must share the discriminant constraint
2422 -- to generate correct calls to initialization procedures.
2424 if Has_Discriminants (T) then
2425 Set_Discriminant_Constraint
2426 (Id, Discriminant_Constraint (T));
2427 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2429 elsif Present (Full_View (T))
2430 and then Has_Discriminants (Full_View (T))
2431 then
2432 Set_Discriminant_Constraint
2433 (Id, Discriminant_Constraint (Full_View (T)));
2434 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2436 -- This would seem semantically correct, but apparently
2437 -- confuses the back-end (4412-009). To be explained ???
2439 -- Set_Has_Discriminants (Id);
2440 end if;
2442 Prepare_Private_Subtype_Completion (Id, N);
2444 when Access_Kind =>
2445 Set_Ekind (Id, E_Access_Subtype);
2446 Set_Is_Constrained (Id, Is_Constrained (T));
2447 Set_Is_Access_Constant
2448 (Id, Is_Access_Constant (T));
2449 Set_Directly_Designated_Type
2450 (Id, Designated_Type (T));
2452 -- Ada 0Y (AI-231): Propagate the null-excluding attribute and
2453 -- carry out some static checks
2455 if Null_Exclusion_Present (N)
2456 or else Can_Never_Be_Null (T)
2457 then
2458 Set_Can_Never_Be_Null (Id);
2460 if Null_Exclusion_Present (N)
2461 and then Can_Never_Be_Null (T)
2462 then
2463 Error_Msg_N
2464 ("(Ada 0Y) null exclusion not allowed if parent "
2465 & "is already non-null", Subtype_Indication (N));
2466 end if;
2467 end if;
2469 -- A Pure library_item must not contain the declaration of a
2470 -- named access type, except within a subprogram, generic
2471 -- subprogram, task unit, or protected unit (RM 10.2.1(16)).
2473 if Comes_From_Source (Id)
2474 and then In_Pure_Unit
2475 and then not In_Subprogram_Task_Protected_Unit
2476 then
2477 Error_Msg_N
2478 ("named access types not allowed in pure unit", N);
2479 end if;
2481 when Concurrent_Kind =>
2482 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
2483 Set_Corresponding_Record_Type (Id,
2484 Corresponding_Record_Type (T));
2485 Set_First_Entity (Id, First_Entity (T));
2486 Set_First_Private_Entity (Id, First_Private_Entity (T));
2487 Set_Has_Discriminants (Id, Has_Discriminants (T));
2488 Set_Is_Constrained (Id, Is_Constrained (T));
2489 Set_Last_Entity (Id, Last_Entity (T));
2491 if Has_Discriminants (T) then
2492 Set_Discriminant_Constraint (Id,
2493 Discriminant_Constraint (T));
2494 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2495 end if;
2497 -- If the subtype name denotes an incomplete type
2498 -- an error was already reported by Process_Subtype.
2500 when E_Incomplete_Type =>
2501 Set_Etype (Id, Any_Type);
2503 when others =>
2504 raise Program_Error;
2505 end case;
2506 end if;
2508 if Etype (Id) = Any_Type then
2509 return;
2510 end if;
2512 -- Some common processing on all types
2514 Set_Size_Info (Id, T);
2515 Set_First_Rep_Item (Id, First_Rep_Item (T));
2517 T := Etype (Id);
2519 Set_Is_Immediately_Visible (Id, True);
2520 Set_Depends_On_Private (Id, Has_Private_Component (T));
2522 if Present (Generic_Parent_Type (N))
2523 and then
2524 (Nkind
2525 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
2526 or else Nkind
2527 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
2528 /= N_Formal_Private_Type_Definition)
2529 then
2530 if Is_Tagged_Type (Id) then
2531 if Is_Class_Wide_Type (Id) then
2532 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
2533 else
2534 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
2535 end if;
2537 elsif Scope (Etype (Id)) /= Standard_Standard then
2538 Derive_Subprograms (Generic_Parent_Type (N), Id);
2539 end if;
2540 end if;
2542 if Is_Private_Type (T)
2543 and then Present (Full_View (T))
2544 then
2545 Conditional_Delay (Id, Full_View (T));
2547 -- The subtypes of components or subcomponents of protected types
2548 -- do not need freeze nodes, which would otherwise appear in the
2549 -- wrong scope (before the freeze node for the protected type). The
2550 -- proper subtypes are those of the subcomponents of the corresponding
2551 -- record.
2553 elsif Ekind (Scope (Id)) /= E_Protected_Type
2554 and then Present (Scope (Scope (Id))) -- error defense!
2555 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
2556 then
2557 Conditional_Delay (Id, T);
2558 end if;
2560 -- Check that constraint_error is raised for a scalar subtype
2561 -- indication when the lower or upper bound of a non-null range
2562 -- lies outside the range of the type mark.
2564 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
2565 if Is_Scalar_Type (Etype (Id))
2566 and then Scalar_Range (Id) /=
2567 Scalar_Range (Etype (Subtype_Mark
2568 (Subtype_Indication (N))))
2569 then
2570 Apply_Range_Check
2571 (Scalar_Range (Id),
2572 Etype (Subtype_Mark (Subtype_Indication (N))));
2574 elsif Is_Array_Type (Etype (Id))
2575 and then Present (First_Index (Id))
2576 then
2577 -- This really should be a subprogram that finds the indications
2578 -- to check???
2580 if ((Nkind (First_Index (Id)) = N_Identifier
2581 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
2582 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
2583 and then
2584 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
2585 then
2586 declare
2587 Target_Typ : constant Entity_Id :=
2588 Etype
2589 (First_Index (Etype
2590 (Subtype_Mark (Subtype_Indication (N)))));
2591 begin
2592 R_Checks :=
2593 Range_Check
2594 (Scalar_Range (Etype (First_Index (Id))),
2595 Target_Typ,
2596 Etype (First_Index (Id)),
2597 Defining_Identifier (N));
2599 Insert_Range_Checks
2600 (R_Checks,
2602 Target_Typ,
2603 Sloc (Defining_Identifier (N)));
2604 end;
2605 end if;
2606 end if;
2607 end if;
2609 Check_Eliminated (Id);
2610 end Analyze_Subtype_Declaration;
2612 --------------------------------
2613 -- Analyze_Subtype_Indication --
2614 --------------------------------
2616 procedure Analyze_Subtype_Indication (N : Node_Id) is
2617 T : constant Entity_Id := Subtype_Mark (N);
2618 R : constant Node_Id := Range_Expression (Constraint (N));
2620 begin
2621 Analyze (T);
2623 if R /= Error then
2624 Analyze (R);
2625 Set_Etype (N, Etype (R));
2626 else
2627 Set_Error_Posted (R);
2628 Set_Error_Posted (T);
2629 end if;
2630 end Analyze_Subtype_Indication;
2632 ------------------------------
2633 -- Analyze_Type_Declaration --
2634 ------------------------------
2636 procedure Analyze_Type_Declaration (N : Node_Id) is
2637 Def : constant Node_Id := Type_Definition (N);
2638 Def_Id : constant Entity_Id := Defining_Identifier (N);
2639 T : Entity_Id;
2640 Prev : Entity_Id;
2642 Is_Remote : constant Boolean :=
2643 (Is_Remote_Types (Current_Scope)
2644 or else Is_Remote_Call_Interface (Current_Scope))
2645 and then not (In_Private_Part (Current_Scope)
2646 or else
2647 In_Package_Body (Current_Scope));
2649 begin
2650 Prev := Find_Type_Name (N);
2652 -- The full view, if present, now points to the current type
2654 -- Ada 0Y (AI-50217): If the type was previously decorated when imported
2655 -- through a LIMITED WITH clause, it appears as incomplete but has no
2656 -- full view.
2658 if Ekind (Prev) = E_Incomplete_Type
2659 and then Present (Full_View (Prev))
2660 then
2661 T := Full_View (Prev);
2662 else
2663 T := Prev;
2664 end if;
2666 Set_Is_Pure (T, Is_Pure (Current_Scope));
2668 -- We set the flag Is_First_Subtype here. It is needed to set the
2669 -- corresponding flag for the Implicit class-wide-type created
2670 -- during tagged types processing.
2672 Set_Is_First_Subtype (T, True);
2674 -- Only composite types other than array types are allowed to have
2675 -- discriminants.
2677 case Nkind (Def) is
2679 -- For derived types, the rule will be checked once we've figured
2680 -- out the parent type.
2682 when N_Derived_Type_Definition =>
2683 null;
2685 -- For record types, discriminants are allowed.
2687 when N_Record_Definition =>
2688 null;
2690 when others =>
2691 if Present (Discriminant_Specifications (N)) then
2692 Error_Msg_N
2693 ("elementary or array type cannot have discriminants",
2694 Defining_Identifier
2695 (First (Discriminant_Specifications (N))));
2696 end if;
2697 end case;
2699 -- Elaborate the type definition according to kind, and generate
2700 -- subsidiary (implicit) subtypes where needed. We skip this if
2701 -- it was already done (this happens during the reanalysis that
2702 -- follows a call to the high level optimizer).
2704 if not Analyzed (T) then
2705 Set_Analyzed (T);
2707 case Nkind (Def) is
2709 when N_Access_To_Subprogram_Definition =>
2710 Access_Subprogram_Declaration (T, Def);
2712 -- If this is a remote access to subprogram, we must create
2713 -- the equivalent fat pointer type, and related subprograms.
2715 if Is_Remote then
2716 Process_Remote_AST_Declaration (N);
2717 end if;
2719 -- Validate categorization rule against access type declaration
2720 -- usually a violation in Pure unit, Shared_Passive unit.
2722 Validate_Access_Type_Declaration (T, N);
2724 when N_Access_To_Object_Definition =>
2725 Access_Type_Declaration (T, Def);
2727 -- Validate categorization rule against access type declaration
2728 -- usually a violation in Pure unit, Shared_Passive unit.
2730 Validate_Access_Type_Declaration (T, N);
2732 -- If we are in a Remote_Call_Interface package and define
2733 -- a RACW, Read and Write attribute must be added.
2735 if Is_Remote
2736 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2737 then
2738 Add_RACW_Features (Def_Id);
2739 end if;
2741 -- Set no strict aliasing flag if config pragma seen
2743 if Opt.No_Strict_Aliasing then
2744 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2745 end if;
2747 when N_Array_Type_Definition =>
2748 Array_Type_Declaration (T, Def);
2750 when N_Derived_Type_Definition =>
2751 Derived_Type_Declaration (T, N, T /= Def_Id);
2753 when N_Enumeration_Type_Definition =>
2754 Enumeration_Type_Declaration (T, Def);
2756 when N_Floating_Point_Definition =>
2757 Floating_Point_Type_Declaration (T, Def);
2759 when N_Decimal_Fixed_Point_Definition =>
2760 Decimal_Fixed_Point_Type_Declaration (T, Def);
2762 when N_Ordinary_Fixed_Point_Definition =>
2763 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2765 when N_Signed_Integer_Type_Definition =>
2766 Signed_Integer_Type_Declaration (T, Def);
2768 when N_Modular_Type_Definition =>
2769 Modular_Type_Declaration (T, Def);
2771 when N_Record_Definition =>
2772 Record_Type_Declaration (T, N, Prev);
2774 when others =>
2775 raise Program_Error;
2777 end case;
2778 end if;
2780 if Etype (T) = Any_Type then
2781 return;
2782 end if;
2784 -- Some common processing for all types
2786 Set_Depends_On_Private (T, Has_Private_Component (T));
2788 -- Both the declared entity, and its anonymous base type if one
2789 -- was created, need freeze nodes allocated.
2791 declare
2792 B : constant Entity_Id := Base_Type (T);
2794 begin
2795 -- In the case where the base type is different from the first
2796 -- subtype, we pre-allocate a freeze node, and set the proper
2797 -- link to the first subtype. Freeze_Entity will use this
2798 -- preallocated freeze node when it freezes the entity.
2800 if B /= T then
2801 Ensure_Freeze_Node (B);
2802 Set_First_Subtype_Link (Freeze_Node (B), T);
2803 end if;
2805 if not From_With_Type (T) then
2806 Set_Has_Delayed_Freeze (T);
2807 end if;
2808 end;
2810 -- Case of T is the full declaration of some private type which has
2811 -- been swapped in Defining_Identifier (N).
2813 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2814 Process_Full_View (N, T, Def_Id);
2816 -- Record the reference. The form of this is a little strange,
2817 -- since the full declaration has been swapped in. So the first
2818 -- parameter here represents the entity to which a reference is
2819 -- made which is the "real" entity, i.e. the one swapped in,
2820 -- and the second parameter provides the reference location.
2822 Generate_Reference (T, T, 'c');
2823 Set_Completion_Referenced (Def_Id);
2825 -- For completion of incomplete type, process incomplete dependents
2826 -- and always mark the full type as referenced (it is the incomplete
2827 -- type that we get for any real reference).
2829 elsif Ekind (Prev) = E_Incomplete_Type then
2830 Process_Incomplete_Dependents (N, T, Prev);
2831 Generate_Reference (Prev, Def_Id, 'c');
2832 Set_Completion_Referenced (Def_Id);
2834 -- If not private type or incomplete type completion, this is a real
2835 -- definition of a new entity, so record it.
2837 else
2838 Generate_Definition (Def_Id);
2839 end if;
2841 Check_Eliminated (Def_Id);
2842 end Analyze_Type_Declaration;
2844 --------------------------
2845 -- Analyze_Variant_Part --
2846 --------------------------
2848 procedure Analyze_Variant_Part (N : Node_Id) is
2850 procedure Non_Static_Choice_Error (Choice : Node_Id);
2851 -- Error routine invoked by the generic instantiation below when
2852 -- the variant part has a non static choice.
2854 procedure Process_Declarations (Variant : Node_Id);
2855 -- Analyzes all the declarations associated with a Variant.
2856 -- Needed by the generic instantiation below.
2858 package Variant_Choices_Processing is new
2859 Generic_Choices_Processing
2860 (Get_Alternatives => Variants,
2861 Get_Choices => Discrete_Choices,
2862 Process_Empty_Choice => No_OP,
2863 Process_Non_Static_Choice => Non_Static_Choice_Error,
2864 Process_Associated_Node => Process_Declarations);
2865 use Variant_Choices_Processing;
2866 -- Instantiation of the generic choice processing package.
2868 -----------------------------
2869 -- Non_Static_Choice_Error --
2870 -----------------------------
2872 procedure Non_Static_Choice_Error (Choice : Node_Id) is
2873 begin
2874 Flag_Non_Static_Expr
2875 ("choice given in variant part is not static!", Choice);
2876 end Non_Static_Choice_Error;
2878 --------------------------
2879 -- Process_Declarations --
2880 --------------------------
2882 procedure Process_Declarations (Variant : Node_Id) is
2883 begin
2884 if not Null_Present (Component_List (Variant)) then
2885 Analyze_Declarations (Component_Items (Component_List (Variant)));
2887 if Present (Variant_Part (Component_List (Variant))) then
2888 Analyze (Variant_Part (Component_List (Variant)));
2889 end if;
2890 end if;
2891 end Process_Declarations;
2893 -- Variables local to Analyze_Case_Statement.
2895 Discr_Name : Node_Id;
2896 Discr_Type : Entity_Id;
2898 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
2899 Last_Choice : Nat;
2900 Dont_Care : Boolean;
2901 Others_Present : Boolean := False;
2903 -- Start of processing for Analyze_Variant_Part
2905 begin
2906 Discr_Name := Name (N);
2907 Analyze (Discr_Name);
2909 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
2910 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
2911 end if;
2913 Discr_Type := Etype (Entity (Discr_Name));
2915 if not Is_Discrete_Type (Discr_Type) then
2916 Error_Msg_N
2917 ("discriminant in a variant part must be of a discrete type",
2918 Name (N));
2919 return;
2920 end if;
2922 -- Call the instantiated Analyze_Choices which does the rest of the work
2924 Analyze_Choices
2925 (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
2926 end Analyze_Variant_Part;
2928 ----------------------------
2929 -- Array_Type_Declaration --
2930 ----------------------------
2932 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
2933 Component_Def : constant Node_Id := Component_Definition (Def);
2934 Element_Type : Entity_Id;
2935 Implicit_Base : Entity_Id;
2936 Index : Node_Id;
2937 Related_Id : Entity_Id := Empty;
2938 Nb_Index : Nat;
2939 P : constant Node_Id := Parent (Def);
2940 Priv : Entity_Id;
2942 begin
2943 if Nkind (Def) = N_Constrained_Array_Definition then
2944 Index := First (Discrete_Subtype_Definitions (Def));
2945 else
2946 Index := First (Subtype_Marks (Def));
2947 end if;
2949 -- Find proper names for the implicit types which may be public.
2950 -- in case of anonymous arrays we use the name of the first object
2951 -- of that type as prefix.
2953 if No (T) then
2954 Related_Id := Defining_Identifier (P);
2955 else
2956 Related_Id := T;
2957 end if;
2959 Nb_Index := 1;
2961 while Present (Index) loop
2962 Analyze (Index);
2963 Make_Index (Index, P, Related_Id, Nb_Index);
2964 Next_Index (Index);
2965 Nb_Index := Nb_Index + 1;
2966 end loop;
2968 if Present (Subtype_Indication (Component_Def)) then
2969 Element_Type := Process_Subtype (Subtype_Indication (Component_Def),
2970 P, Related_Id, 'C');
2972 -- Ada 0Y (AI-230): Access Definition case
2974 else pragma Assert (Present (Access_Definition (Component_Def)));
2975 Element_Type := Access_Definition
2976 (Related_Nod => Related_Id,
2977 N => Access_Definition (Component_Def));
2979 -- Ada 0Y (AI-230): In case of components that are anonymous access
2980 -- types the level of accessibility depends on the enclosing type
2981 -- declaration
2983 Set_Scope (Element_Type, T); -- Ada 0Y (AI-230)
2985 -- Ada 0Y (AI-254)
2987 declare
2988 CD : constant Node_Id :=
2989 Access_To_Subprogram_Definition
2990 (Access_Definition (Component_Def));
2991 begin
2992 if Present (CD) and then Protected_Present (CD) then
2993 Element_Type :=
2994 Replace_Anonymous_Access_To_Protected_Subprogram
2995 (Def, Element_Type);
2996 end if;
2997 end;
2998 end if;
3000 -- Constrained array case
3002 if No (T) then
3003 T := Create_Itype (E_Void, P, Related_Id, 'T');
3004 end if;
3006 if Nkind (Def) = N_Constrained_Array_Definition then
3008 -- Establish Implicit_Base as unconstrained base type
3010 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
3012 Init_Size_Align (Implicit_Base);
3013 Set_Etype (Implicit_Base, Implicit_Base);
3014 Set_Scope (Implicit_Base, Current_Scope);
3015 Set_Has_Delayed_Freeze (Implicit_Base);
3017 -- The constrained array type is a subtype of the unconstrained one
3019 Set_Ekind (T, E_Array_Subtype);
3020 Init_Size_Align (T);
3021 Set_Etype (T, Implicit_Base);
3022 Set_Scope (T, Current_Scope);
3023 Set_Is_Constrained (T, True);
3024 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
3025 Set_Has_Delayed_Freeze (T);
3027 -- Complete setup of implicit base type
3029 Set_First_Index (Implicit_Base, First_Index (T));
3030 Set_Component_Type (Implicit_Base, Element_Type);
3031 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
3032 Set_Component_Size (Implicit_Base, Uint_0);
3033 Set_Has_Controlled_Component
3034 (Implicit_Base, Has_Controlled_Component
3035 (Element_Type)
3036 or else
3037 Is_Controlled (Element_Type));
3038 Set_Finalize_Storage_Only
3039 (Implicit_Base, Finalize_Storage_Only
3040 (Element_Type));
3042 -- Unconstrained array case
3044 else
3045 Set_Ekind (T, E_Array_Type);
3046 Init_Size_Align (T);
3047 Set_Etype (T, T);
3048 Set_Scope (T, Current_Scope);
3049 Set_Component_Size (T, Uint_0);
3050 Set_Is_Constrained (T, False);
3051 Set_First_Index (T, First (Subtype_Marks (Def)));
3052 Set_Has_Delayed_Freeze (T, True);
3053 Set_Has_Task (T, Has_Task (Element_Type));
3054 Set_Has_Controlled_Component (T, Has_Controlled_Component
3055 (Element_Type)
3056 or else
3057 Is_Controlled (Element_Type));
3058 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
3059 (Element_Type));
3060 end if;
3062 Set_Component_Type (Base_Type (T), Element_Type);
3064 if Aliased_Present (Component_Definition (Def)) then
3065 Set_Has_Aliased_Components (Etype (T));
3066 end if;
3068 -- Ada 0Y (AI-231): Propagate the null-excluding attribute to the array
3069 -- to ensure that objects of this type are initialized
3071 if Extensions_Allowed
3072 and then (Null_Exclusion_Present (Component_Definition (Def))
3073 or else Can_Never_Be_Null (Element_Type))
3074 then
3075 Set_Can_Never_Be_Null (T);
3077 if Null_Exclusion_Present (Component_Definition (Def))
3078 and then Can_Never_Be_Null (Element_Type)
3079 then
3080 Error_Msg_N
3081 ("(Ada 0Y) already a null-excluding type",
3082 Subtype_Indication (Component_Definition (Def)));
3083 end if;
3084 end if;
3086 Priv := Private_Component (Element_Type);
3088 if Present (Priv) then
3090 -- Check for circular definitions
3092 if Priv = Any_Type then
3093 Set_Component_Type (Etype (T), Any_Type);
3095 -- There is a gap in the visibility of operations on the composite
3096 -- type only if the component type is defined in a different scope.
3098 elsif Scope (Priv) = Current_Scope then
3099 null;
3101 elsif Is_Limited_Type (Priv) then
3102 Set_Is_Limited_Composite (Etype (T));
3103 Set_Is_Limited_Composite (T);
3104 else
3105 Set_Is_Private_Composite (Etype (T));
3106 Set_Is_Private_Composite (T);
3107 end if;
3108 end if;
3110 -- Create a concatenation operator for the new type. Internal
3111 -- array types created for packed entities do not need such, they
3112 -- are compatible with the user-defined type.
3114 if Number_Dimensions (T) = 1
3115 and then not Is_Packed_Array_Type (T)
3116 then
3117 New_Concatenation_Op (T);
3118 end if;
3120 -- In the case of an unconstrained array the parser has already
3121 -- verified that all the indices are unconstrained but we still
3122 -- need to make sure that the element type is constrained.
3124 if Is_Indefinite_Subtype (Element_Type) then
3125 Error_Msg_N
3126 ("unconstrained element type in array declaration",
3127 Subtype_Indication (Component_Def));
3129 elsif Is_Abstract (Element_Type) then
3130 Error_Msg_N
3131 ("The type of a component cannot be abstract",
3132 Subtype_Indication (Component_Def));
3133 end if;
3135 end Array_Type_Declaration;
3137 ------------------------------------------------------
3138 -- Replace_Anonymous_Access_To_Protected_Subprogram --
3139 ------------------------------------------------------
3141 function Replace_Anonymous_Access_To_Protected_Subprogram
3142 (N : Node_Id;
3143 Prev_E : Entity_Id) return Entity_Id
3145 Loc : constant Source_Ptr := Sloc (N);
3147 Curr_Scope : constant Scope_Stack_Entry :=
3148 Scope_Stack.Table (Scope_Stack.Last);
3150 Anon : constant Entity_Id :=
3151 Make_Defining_Identifier (Loc,
3152 Chars => New_Internal_Name ('S'));
3154 Acc : Node_Id;
3155 Comp : Node_Id;
3156 Decl : Node_Id;
3157 P : Node_Id := Parent (N);
3159 begin
3160 Set_Is_Internal (Anon);
3162 case Nkind (N) is
3163 when N_Component_Declaration |
3164 N_Unconstrained_Array_Definition |
3165 N_Constrained_Array_Definition =>
3166 Comp := Component_Definition (N);
3167 Acc := Access_Definition (Component_Definition (N));
3169 when N_Discriminant_Specification =>
3170 Comp := Discriminant_Type (N);
3171 Acc := Discriminant_Type (N);
3173 when N_Parameter_Specification =>
3174 Comp := Parameter_Type (N);
3175 Acc := Parameter_Type (N);
3177 when others =>
3178 raise Program_Error;
3179 end case;
3181 Decl := Make_Full_Type_Declaration (Loc,
3182 Defining_Identifier => Anon,
3183 Type_Definition =>
3184 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
3186 Mark_Rewrite_Insertion (Decl);
3188 -- Insert the new declaration in the nearest enclosing scope
3190 while Present (P) and then not Has_Declarations (P) loop
3191 P := Parent (P);
3192 end loop;
3194 pragma Assert (Present (P));
3196 if Nkind (P) = N_Package_Specification then
3197 Prepend (Decl, Visible_Declarations (P));
3198 else
3199 Prepend (Decl, Declarations (P));
3200 end if;
3202 -- Replace the anonymous type with an occurrence of the new declaration.
3203 -- In all cases the rewriten node does not have the null-exclusion
3204 -- attribute because (if present) it was already inherited by the
3205 -- anonymous entity (Anon). Thus, in case of components we do not
3206 -- inherit this attribute.
3208 if Nkind (N) = N_Parameter_Specification then
3209 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
3210 Set_Etype (Defining_Identifier (N), Anon);
3211 Set_Null_Exclusion_Present (N, False);
3212 else
3213 Rewrite (Comp,
3214 Make_Component_Definition (Loc,
3215 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
3216 end if;
3218 Mark_Rewrite_Insertion (Comp);
3220 -- Temporarily remove the current scope from the stack to add the new
3221 -- declarations to the enclosing scope
3223 Scope_Stack.Decrement_Last;
3224 Analyze (Decl);
3225 Scope_Stack.Append (Curr_Scope);
3227 Set_Original_Access_Type (Anon, Prev_E);
3228 return Anon;
3229 end Replace_Anonymous_Access_To_Protected_Subprogram;
3231 -------------------------------
3232 -- Build_Derived_Access_Type --
3233 -------------------------------
3235 procedure Build_Derived_Access_Type
3236 (N : Node_Id;
3237 Parent_Type : Entity_Id;
3238 Derived_Type : Entity_Id)
3240 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
3242 Desig_Type : Entity_Id;
3243 Discr : Entity_Id;
3244 Discr_Con_Elist : Elist_Id;
3245 Discr_Con_El : Elmt_Id;
3246 Subt : Entity_Id;
3248 begin
3249 -- Set the designated type so it is available in case this is
3250 -- an access to a self-referential type, e.g. a standard list
3251 -- type with a next pointer. Will be reset after subtype is built.
3253 Set_Directly_Designated_Type
3254 (Derived_Type, Designated_Type (Parent_Type));
3256 Subt := Process_Subtype (S, N);
3258 if Nkind (S) /= N_Subtype_Indication
3259 and then Subt /= Base_Type (Subt)
3260 then
3261 Set_Ekind (Derived_Type, E_Access_Subtype);
3262 end if;
3264 if Ekind (Derived_Type) = E_Access_Subtype then
3265 declare
3266 Pbase : constant Entity_Id := Base_Type (Parent_Type);
3267 Ibase : constant Entity_Id :=
3268 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
3269 Svg_Chars : constant Name_Id := Chars (Ibase);
3270 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
3272 begin
3273 Copy_Node (Pbase, Ibase);
3275 Set_Chars (Ibase, Svg_Chars);
3276 Set_Next_Entity (Ibase, Svg_Next_E);
3277 Set_Sloc (Ibase, Sloc (Derived_Type));
3278 Set_Scope (Ibase, Scope (Derived_Type));
3279 Set_Freeze_Node (Ibase, Empty);
3280 Set_Is_Frozen (Ibase, False);
3281 Set_Comes_From_Source (Ibase, False);
3282 Set_Is_First_Subtype (Ibase, False);
3284 Set_Etype (Ibase, Pbase);
3285 Set_Etype (Derived_Type, Ibase);
3286 end;
3287 end if;
3289 Set_Directly_Designated_Type
3290 (Derived_Type, Designated_Type (Subt));
3292 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
3293 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
3294 Set_Size_Info (Derived_Type, Parent_Type);
3295 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
3296 Set_Depends_On_Private (Derived_Type,
3297 Has_Private_Component (Derived_Type));
3298 Conditional_Delay (Derived_Type, Subt);
3300 -- Ada 0Y (AI-231). Set the null-exclusion attribute
3302 if Null_Exclusion_Present (Type_Definition (N))
3303 or else Can_Never_Be_Null (Parent_Type)
3304 then
3305 Set_Can_Never_Be_Null (Derived_Type);
3306 end if;
3308 -- Note: we do not copy the Storage_Size_Variable, since
3309 -- we always go to the root type for this information.
3311 -- Apply range checks to discriminants for derived record case
3312 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
3314 Desig_Type := Designated_Type (Derived_Type);
3315 if Is_Composite_Type (Desig_Type)
3316 and then (not Is_Array_Type (Desig_Type))
3317 and then Has_Discriminants (Desig_Type)
3318 and then Base_Type (Desig_Type) /= Desig_Type
3319 then
3320 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
3321 Discr_Con_El := First_Elmt (Discr_Con_Elist);
3323 Discr := First_Discriminant (Base_Type (Desig_Type));
3324 while Present (Discr_Con_El) loop
3325 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
3326 Next_Elmt (Discr_Con_El);
3327 Next_Discriminant (Discr);
3328 end loop;
3329 end if;
3330 end Build_Derived_Access_Type;
3332 ------------------------------
3333 -- Build_Derived_Array_Type --
3334 ------------------------------
3336 procedure Build_Derived_Array_Type
3337 (N : Node_Id;
3338 Parent_Type : Entity_Id;
3339 Derived_Type : Entity_Id)
3341 Loc : constant Source_Ptr := Sloc (N);
3342 Tdef : constant Node_Id := Type_Definition (N);
3343 Indic : constant Node_Id := Subtype_Indication (Tdef);
3344 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
3345 Implicit_Base : Entity_Id;
3346 New_Indic : Node_Id;
3348 procedure Make_Implicit_Base;
3349 -- If the parent subtype is constrained, the derived type is a
3350 -- subtype of an implicit base type derived from the parent base.
3352 ------------------------
3353 -- Make_Implicit_Base --
3354 ------------------------
3356 procedure Make_Implicit_Base is
3357 begin
3358 Implicit_Base :=
3359 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
3361 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
3362 Set_Etype (Implicit_Base, Parent_Base);
3364 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
3365 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
3367 Set_Has_Delayed_Freeze (Implicit_Base, True);
3368 end Make_Implicit_Base;
3370 -- Start of processing for Build_Derived_Array_Type
3372 begin
3373 if not Is_Constrained (Parent_Type) then
3374 if Nkind (Indic) /= N_Subtype_Indication then
3375 Set_Ekind (Derived_Type, E_Array_Type);
3377 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
3378 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
3380 Set_Has_Delayed_Freeze (Derived_Type, True);
3382 else
3383 Make_Implicit_Base;
3384 Set_Etype (Derived_Type, Implicit_Base);
3386 New_Indic :=
3387 Make_Subtype_Declaration (Loc,
3388 Defining_Identifier => Derived_Type,
3389 Subtype_Indication =>
3390 Make_Subtype_Indication (Loc,
3391 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
3392 Constraint => Constraint (Indic)));
3394 Rewrite (N, New_Indic);
3395 Analyze (N);
3396 end if;
3398 else
3399 if Nkind (Indic) /= N_Subtype_Indication then
3400 Make_Implicit_Base;
3402 Set_Ekind (Derived_Type, Ekind (Parent_Type));
3403 Set_Etype (Derived_Type, Implicit_Base);
3404 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
3406 else
3407 Error_Msg_N ("illegal constraint on constrained type", Indic);
3408 end if;
3409 end if;
3411 -- If the parent type is not a derived type itself, and is
3412 -- declared in a closed scope (e.g., a subprogram), then we
3413 -- need to explicitly introduce the new type's concatenation
3414 -- operator since Derive_Subprograms will not inherit the
3415 -- parent's operator. If the parent type is unconstrained, the
3416 -- operator is of the unconstrained base type.
3418 if Number_Dimensions (Parent_Type) = 1
3419 and then not Is_Limited_Type (Parent_Type)
3420 and then not Is_Derived_Type (Parent_Type)
3421 and then not Is_Package (Scope (Base_Type (Parent_Type)))
3422 then
3423 if not Is_Constrained (Parent_Type)
3424 and then Is_Constrained (Derived_Type)
3425 then
3426 New_Concatenation_Op (Implicit_Base);
3427 else
3428 New_Concatenation_Op (Derived_Type);
3429 end if;
3430 end if;
3431 end Build_Derived_Array_Type;
3433 -----------------------------------
3434 -- Build_Derived_Concurrent_Type --
3435 -----------------------------------
3437 procedure Build_Derived_Concurrent_Type
3438 (N : Node_Id;
3439 Parent_Type : Entity_Id;
3440 Derived_Type : Entity_Id)
3442 D_Constraint : Node_Id;
3443 Disc_Spec : Node_Id;
3444 Old_Disc : Entity_Id;
3445 New_Disc : Entity_Id;
3447 Constraint_Present : constant Boolean :=
3448 Nkind (Subtype_Indication (Type_Definition (N)))
3449 = N_Subtype_Indication;
3451 begin
3452 Set_Stored_Constraint (Derived_Type, No_Elist);
3454 if Is_Task_Type (Parent_Type) then
3455 Set_Storage_Size_Variable (Derived_Type,
3456 Storage_Size_Variable (Parent_Type));
3457 end if;
3459 if Present (Discriminant_Specifications (N)) then
3460 New_Scope (Derived_Type);
3461 Check_Or_Process_Discriminants (N, Derived_Type);
3462 End_Scope;
3464 elsif Constraint_Present then
3466 -- Build constrained subtype and derive from it
3468 declare
3469 Loc : constant Source_Ptr := Sloc (N);
3470 Anon : constant Entity_Id :=
3471 Make_Defining_Identifier (Loc,
3472 New_External_Name (Chars (Derived_Type), 'T'));
3473 Decl : Node_Id;
3475 begin
3476 Decl :=
3477 Make_Subtype_Declaration (Loc,
3478 Defining_Identifier => Anon,
3479 Subtype_Indication =>
3480 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
3481 Insert_Before (N, Decl);
3482 Rewrite (Subtype_Indication (Type_Definition (N)),
3483 New_Occurrence_Of (Anon, Loc));
3484 Analyze (Decl);
3485 Set_Analyzed (Derived_Type, False);
3486 Analyze (N);
3487 return;
3488 end;
3489 end if;
3491 -- All attributes are inherited from parent. In particular,
3492 -- entries and the corresponding record type are the same.
3493 -- Discriminants may be renamed, and must be treated separately.
3495 Set_Has_Discriminants
3496 (Derived_Type, Has_Discriminants (Parent_Type));
3497 Set_Corresponding_Record_Type
3498 (Derived_Type, Corresponding_Record_Type (Parent_Type));
3500 if Constraint_Present then
3502 if not Has_Discriminants (Parent_Type) then
3503 Error_Msg_N ("untagged parent must have discriminants", N);
3505 elsif Present (Discriminant_Specifications (N)) then
3507 -- Verify that new discriminants are used to constrain
3508 -- the old ones.
3510 Old_Disc := First_Discriminant (Parent_Type);
3511 New_Disc := First_Discriminant (Derived_Type);
3512 Disc_Spec := First (Discriminant_Specifications (N));
3513 D_Constraint :=
3514 First
3515 (Constraints
3516 (Constraint (Subtype_Indication (Type_Definition (N)))));
3518 while Present (Old_Disc) and then Present (Disc_Spec) loop
3520 if Nkind (Discriminant_Type (Disc_Spec)) /=
3521 N_Access_Definition
3522 then
3523 Analyze (Discriminant_Type (Disc_Spec));
3525 if not Subtypes_Statically_Compatible (
3526 Etype (Discriminant_Type (Disc_Spec)),
3527 Etype (Old_Disc))
3528 then
3529 Error_Msg_N
3530 ("not statically compatible with parent discriminant",
3531 Discriminant_Type (Disc_Spec));
3532 end if;
3533 end if;
3535 if Nkind (D_Constraint) = N_Identifier
3536 and then Chars (D_Constraint) /=
3537 Chars (Defining_Identifier (Disc_Spec))
3538 then
3539 Error_Msg_N ("new discriminants must constrain old ones",
3540 D_Constraint);
3541 else
3542 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
3543 end if;
3545 Next_Discriminant (Old_Disc);
3546 Next_Discriminant (New_Disc);
3547 Next (Disc_Spec);
3548 end loop;
3550 if Present (Old_Disc) or else Present (Disc_Spec) then
3551 Error_Msg_N ("discriminant mismatch in derivation", N);
3552 end if;
3554 end if;
3556 elsif Present (Discriminant_Specifications (N)) then
3557 Error_Msg_N
3558 ("missing discriminant constraint in untagged derivation",
3560 end if;
3562 if Present (Discriminant_Specifications (N)) then
3564 Old_Disc := First_Discriminant (Parent_Type);
3566 while Present (Old_Disc) loop
3568 if No (Next_Entity (Old_Disc))
3569 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
3570 then
3571 Set_Next_Entity (Last_Entity (Derived_Type),
3572 Next_Entity (Old_Disc));
3573 exit;
3574 end if;
3576 Next_Discriminant (Old_Disc);
3577 end loop;
3579 else
3580 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
3581 if Has_Discriminants (Parent_Type) then
3582 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
3583 Set_Discriminant_Constraint (
3584 Derived_Type, Discriminant_Constraint (Parent_Type));
3585 end if;
3586 end if;
3588 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
3590 Set_Has_Completion (Derived_Type);
3591 end Build_Derived_Concurrent_Type;
3593 ------------------------------------
3594 -- Build_Derived_Enumeration_Type --
3595 ------------------------------------
3597 procedure Build_Derived_Enumeration_Type
3598 (N : Node_Id;
3599 Parent_Type : Entity_Id;
3600 Derived_Type : Entity_Id)
3602 Loc : constant Source_Ptr := Sloc (N);
3603 Def : constant Node_Id := Type_Definition (N);
3604 Indic : constant Node_Id := Subtype_Indication (Def);
3605 Implicit_Base : Entity_Id;
3606 Literal : Entity_Id;
3607 New_Lit : Entity_Id;
3608 Literals_List : List_Id;
3609 Type_Decl : Node_Id;
3610 Hi, Lo : Node_Id;
3611 Rang_Expr : Node_Id;
3613 begin
3614 -- Since types Standard.Character and Standard.Wide_Character do
3615 -- not have explicit literals lists we need to process types derived
3616 -- from them specially. This is handled by Derived_Standard_Character.
3617 -- If the parent type is a generic type, there are no literals either,
3618 -- and we construct the same skeletal representation as for the generic
3619 -- parent type.
3621 if Root_Type (Parent_Type) = Standard_Character
3622 or else Root_Type (Parent_Type) = Standard_Wide_Character
3623 then
3624 Derived_Standard_Character (N, Parent_Type, Derived_Type);
3626 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
3627 declare
3628 Lo : Node_Id;
3629 Hi : Node_Id;
3631 begin
3632 Lo :=
3633 Make_Attribute_Reference (Loc,
3634 Attribute_Name => Name_First,
3635 Prefix => New_Reference_To (Derived_Type, Loc));
3636 Set_Etype (Lo, Derived_Type);
3638 Hi :=
3639 Make_Attribute_Reference (Loc,
3640 Attribute_Name => Name_Last,
3641 Prefix => New_Reference_To (Derived_Type, Loc));
3642 Set_Etype (Hi, Derived_Type);
3644 Set_Scalar_Range (Derived_Type,
3645 Make_Range (Loc,
3646 Low_Bound => Lo,
3647 High_Bound => Hi));
3648 end;
3650 else
3651 -- If a constraint is present, analyze the bounds to catch
3652 -- premature usage of the derived literals.
3654 if Nkind (Indic) = N_Subtype_Indication
3655 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
3656 then
3657 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
3658 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
3659 end if;
3661 -- Introduce an implicit base type for the derived type even
3662 -- if there is no constraint attached to it, since this seems
3663 -- closer to the Ada semantics. Build a full type declaration
3664 -- tree for the derived type using the implicit base type as
3665 -- the defining identifier. The build a subtype declaration
3666 -- tree which applies the constraint (if any) have it replace
3667 -- the derived type declaration.
3669 Literal := First_Literal (Parent_Type);
3670 Literals_List := New_List;
3672 while Present (Literal)
3673 and then Ekind (Literal) = E_Enumeration_Literal
3674 loop
3675 -- Literals of the derived type have the same representation as
3676 -- those of the parent type, but this representation can be
3677 -- overridden by an explicit representation clause. Indicate
3678 -- that there is no explicit representation given yet. These
3679 -- derived literals are implicit operations of the new type,
3680 -- and can be overriden by explicit ones.
3682 if Nkind (Literal) = N_Defining_Character_Literal then
3683 New_Lit :=
3684 Make_Defining_Character_Literal (Loc, Chars (Literal));
3685 else
3686 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
3687 end if;
3689 Set_Ekind (New_Lit, E_Enumeration_Literal);
3690 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
3691 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
3692 Set_Enumeration_Rep_Expr (New_Lit, Empty);
3693 Set_Alias (New_Lit, Literal);
3694 Set_Is_Known_Valid (New_Lit, True);
3696 Append (New_Lit, Literals_List);
3697 Next_Literal (Literal);
3698 end loop;
3700 Implicit_Base :=
3701 Make_Defining_Identifier (Sloc (Derived_Type),
3702 New_External_Name (Chars (Derived_Type), 'B'));
3704 -- Indicate the proper nature of the derived type. This must
3705 -- be done before analysis of the literals, to recognize cases
3706 -- when a literal may be hidden by a previous explicit function
3707 -- definition (cf. c83031a).
3709 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
3710 Set_Etype (Derived_Type, Implicit_Base);
3712 Type_Decl :=
3713 Make_Full_Type_Declaration (Loc,
3714 Defining_Identifier => Implicit_Base,
3715 Discriminant_Specifications => No_List,
3716 Type_Definition =>
3717 Make_Enumeration_Type_Definition (Loc, Literals_List));
3719 Mark_Rewrite_Insertion (Type_Decl);
3720 Insert_Before (N, Type_Decl);
3721 Analyze (Type_Decl);
3723 -- After the implicit base is analyzed its Etype needs to be
3724 -- changed to reflect the fact that it is derived from the
3725 -- parent type which was ignored during analysis. We also set
3726 -- the size at this point.
3728 Set_Etype (Implicit_Base, Parent_Type);
3730 Set_Size_Info (Implicit_Base, Parent_Type);
3731 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
3732 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
3734 Set_Has_Non_Standard_Rep
3735 (Implicit_Base, Has_Non_Standard_Rep
3736 (Parent_Type));
3737 Set_Has_Delayed_Freeze (Implicit_Base);
3739 -- Process the subtype indication including a validation check
3740 -- on the constraint, if any. If a constraint is given, its bounds
3741 -- must be implicitly converted to the new type.
3743 if Nkind (Indic) = N_Subtype_Indication then
3745 declare
3746 R : constant Node_Id :=
3747 Range_Expression (Constraint (Indic));
3749 begin
3750 if Nkind (R) = N_Range then
3751 Hi := Build_Scalar_Bound
3752 (High_Bound (R), Parent_Type, Implicit_Base);
3753 Lo := Build_Scalar_Bound
3754 (Low_Bound (R), Parent_Type, Implicit_Base);
3756 else
3757 -- Constraint is a Range attribute. Replace with the
3758 -- explicit mention of the bounds of the prefix, which
3759 -- must be a subtype.
3761 Analyze (Prefix (R));
3762 Hi :=
3763 Convert_To (Implicit_Base,
3764 Make_Attribute_Reference (Loc,
3765 Attribute_Name => Name_Last,
3766 Prefix =>
3767 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
3769 Lo :=
3770 Convert_To (Implicit_Base,
3771 Make_Attribute_Reference (Loc,
3772 Attribute_Name => Name_First,
3773 Prefix =>
3774 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
3775 end if;
3777 end;
3779 else
3780 Hi :=
3781 Build_Scalar_Bound
3782 (Type_High_Bound (Parent_Type),
3783 Parent_Type, Implicit_Base);
3784 Lo :=
3785 Build_Scalar_Bound
3786 (Type_Low_Bound (Parent_Type),
3787 Parent_Type, Implicit_Base);
3788 end if;
3790 Rang_Expr :=
3791 Make_Range (Loc,
3792 Low_Bound => Lo,
3793 High_Bound => Hi);
3795 -- If we constructed a default range for the case where no range
3796 -- was given, then the expressions in the range must not freeze
3797 -- since they do not correspond to expressions in the source.
3799 if Nkind (Indic) /= N_Subtype_Indication then
3800 Set_Must_Not_Freeze (Lo);
3801 Set_Must_Not_Freeze (Hi);
3802 Set_Must_Not_Freeze (Rang_Expr);
3803 end if;
3805 Rewrite (N,
3806 Make_Subtype_Declaration (Loc,
3807 Defining_Identifier => Derived_Type,
3808 Subtype_Indication =>
3809 Make_Subtype_Indication (Loc,
3810 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
3811 Constraint =>
3812 Make_Range_Constraint (Loc,
3813 Range_Expression => Rang_Expr))));
3815 Analyze (N);
3817 -- If pragma Discard_Names applies on the first subtype
3818 -- of the parent type, then it must be applied on this
3819 -- subtype as well.
3821 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
3822 Set_Discard_Names (Derived_Type);
3823 end if;
3825 -- Apply a range check. Since this range expression doesn't
3826 -- have an Etype, we have to specifically pass the Source_Typ
3827 -- parameter. Is this right???
3829 if Nkind (Indic) = N_Subtype_Indication then
3830 Apply_Range_Check (Range_Expression (Constraint (Indic)),
3831 Parent_Type,
3832 Source_Typ => Entity (Subtype_Mark (Indic)));
3833 end if;
3834 end if;
3835 end Build_Derived_Enumeration_Type;
3837 --------------------------------
3838 -- Build_Derived_Numeric_Type --
3839 --------------------------------
3841 procedure Build_Derived_Numeric_Type
3842 (N : Node_Id;
3843 Parent_Type : Entity_Id;
3844 Derived_Type : Entity_Id)
3846 Loc : constant Source_Ptr := Sloc (N);
3847 Tdef : constant Node_Id := Type_Definition (N);
3848 Indic : constant Node_Id := Subtype_Indication (Tdef);
3849 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
3850 No_Constraint : constant Boolean := Nkind (Indic) /=
3851 N_Subtype_Indication;
3852 Implicit_Base : Entity_Id;
3854 Lo : Node_Id;
3855 Hi : Node_Id;
3857 begin
3858 -- Process the subtype indication including a validation check on
3859 -- the constraint if any.
3861 Discard_Node (Process_Subtype (Indic, N));
3863 -- Introduce an implicit base type for the derived type even if
3864 -- there is no constraint attached to it, since this seems closer
3865 -- to the Ada semantics.
3867 Implicit_Base :=
3868 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
3870 Set_Etype (Implicit_Base, Parent_Base);
3871 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
3872 Set_Size_Info (Implicit_Base, Parent_Base);
3873 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
3874 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
3875 Set_Parent (Implicit_Base, Parent (Derived_Type));
3877 if Is_Discrete_Or_Fixed_Point_Type (Parent_Base) then
3878 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
3879 end if;
3881 Set_Has_Delayed_Freeze (Implicit_Base);
3883 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
3884 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
3886 Set_Scalar_Range (Implicit_Base,
3887 Make_Range (Loc,
3888 Low_Bound => Lo,
3889 High_Bound => Hi));
3891 if Has_Infinities (Parent_Base) then
3892 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
3893 end if;
3895 -- The Derived_Type, which is the entity of the declaration, is
3896 -- a subtype of the implicit base. Its Ekind is a subtype, even
3897 -- in the absence of an explicit constraint.
3899 Set_Etype (Derived_Type, Implicit_Base);
3901 -- If we did not have a constraint, then the Ekind is set from the
3902 -- parent type (otherwise Process_Subtype has set the bounds)
3904 if No_Constraint then
3905 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
3906 end if;
3908 -- If we did not have a range constraint, then set the range
3909 -- from the parent type. Otherwise, the call to Process_Subtype
3910 -- has set the bounds.
3912 if No_Constraint
3913 or else not Has_Range_Constraint (Indic)
3914 then
3915 Set_Scalar_Range (Derived_Type,
3916 Make_Range (Loc,
3917 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
3918 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
3919 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
3921 if Has_Infinities (Parent_Type) then
3922 Set_Includes_Infinities (Scalar_Range (Derived_Type));
3923 end if;
3924 end if;
3926 -- Set remaining type-specific fields, depending on numeric type
3928 if Is_Modular_Integer_Type (Parent_Type) then
3929 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
3931 Set_Non_Binary_Modulus
3932 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
3934 elsif Is_Floating_Point_Type (Parent_Type) then
3936 -- Digits of base type is always copied from the digits value of
3937 -- the parent base type, but the digits of the derived type will
3938 -- already have been set if there was a constraint present.
3940 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
3941 Set_Vax_Float (Implicit_Base, Vax_Float (Parent_Base));
3943 if No_Constraint then
3944 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
3945 end if;
3947 elsif Is_Fixed_Point_Type (Parent_Type) then
3949 -- Small of base type and derived type are always copied from
3950 -- the parent base type, since smalls never change. The delta
3951 -- of the base type is also copied from the parent base type.
3952 -- However the delta of the derived type will have been set
3953 -- already if a constraint was present.
3955 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
3956 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
3957 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
3959 if No_Constraint then
3960 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
3961 end if;
3963 -- The scale and machine radix in the decimal case are always
3964 -- copied from the parent base type.
3966 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
3967 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
3968 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
3970 Set_Machine_Radix_10
3971 (Derived_Type, Machine_Radix_10 (Parent_Base));
3972 Set_Machine_Radix_10
3973 (Implicit_Base, Machine_Radix_10 (Parent_Base));
3975 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
3977 if No_Constraint then
3978 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
3980 else
3981 -- the analysis of the subtype_indication sets the
3982 -- digits value of the derived type.
3984 null;
3985 end if;
3986 end if;
3987 end if;
3989 -- The type of the bounds is that of the parent type, and they
3990 -- must be converted to the derived type.
3992 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
3994 -- The implicit_base should be frozen when the derived type is frozen,
3995 -- but note that it is used in the conversions of the bounds. For
3996 -- fixed types we delay the determination of the bounds until the proper
3997 -- freezing point. For other numeric types this is rejected by GCC, for
3998 -- reasons that are currently unclear (???), so we choose to freeze the
3999 -- implicit base now. In the case of integers and floating point types
4000 -- this is harmless because subsequent representation clauses cannot
4001 -- affect anything, but it is still baffling that we cannot use the
4002 -- same mechanism for all derived numeric types.
4004 if Is_Fixed_Point_Type (Parent_Type) then
4005 Conditional_Delay (Implicit_Base, Parent_Type);
4006 else
4007 Freeze_Before (N, Implicit_Base);
4008 end if;
4009 end Build_Derived_Numeric_Type;
4011 --------------------------------
4012 -- Build_Derived_Private_Type --
4013 --------------------------------
4015 procedure Build_Derived_Private_Type
4016 (N : Node_Id;
4017 Parent_Type : Entity_Id;
4018 Derived_Type : Entity_Id;
4019 Is_Completion : Boolean;
4020 Derive_Subps : Boolean := True)
4022 Der_Base : Entity_Id;
4023 Discr : Entity_Id;
4024 Full_Decl : Node_Id := Empty;
4025 Full_Der : Entity_Id;
4026 Full_P : Entity_Id;
4027 Last_Discr : Entity_Id;
4028 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
4029 Swapped : Boolean := False;
4031 procedure Copy_And_Build;
4032 -- Copy derived type declaration, replace parent with its full view,
4033 -- and analyze new declaration.
4035 --------------------
4036 -- Copy_And_Build --
4037 --------------------
4039 procedure Copy_And_Build is
4040 Full_N : Node_Id;
4042 begin
4043 if Ekind (Parent_Type) in Record_Kind
4044 or else (Ekind (Parent_Type) in Enumeration_Kind
4045 and then Root_Type (Parent_Type) /= Standard_Character
4046 and then Root_Type (Parent_Type) /= Standard_Wide_Character
4047 and then not Is_Generic_Type (Root_Type (Parent_Type)))
4048 then
4049 Full_N := New_Copy_Tree (N);
4050 Insert_After (N, Full_N);
4051 Build_Derived_Type (
4052 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
4054 else
4055 Build_Derived_Type (
4056 N, Parent_Type, Full_Der, True, Derive_Subps => False);
4057 end if;
4058 end Copy_And_Build;
4060 -- Start of processing for Build_Derived_Private_Type
4062 begin
4063 if Is_Tagged_Type (Parent_Type) then
4064 Build_Derived_Record_Type
4065 (N, Parent_Type, Derived_Type, Derive_Subps);
4066 return;
4068 elsif Has_Discriminants (Parent_Type) then
4070 if Present (Full_View (Parent_Type)) then
4071 if not Is_Completion then
4073 -- Copy declaration for subsequent analysis, to
4074 -- provide a completion for what is a private
4075 -- declaration. Indicate that the full type is
4076 -- internally generated.
4078 Full_Decl := New_Copy_Tree (N);
4079 Full_Der := New_Copy (Derived_Type);
4080 Set_Comes_From_Source (Full_Decl, False);
4082 Insert_After (N, Full_Decl);
4084 else
4085 -- If this is a completion, the full view being built is
4086 -- itself private. We build a subtype of the parent with
4087 -- the same constraints as this full view, to convey to the
4088 -- back end the constrained components and the size of this
4089 -- subtype. If the parent is constrained, its full view can
4090 -- serve as the underlying full view of the derived type.
4092 if No (Discriminant_Specifications (N)) then
4094 if Nkind (Subtype_Indication (Type_Definition (N)))
4095 = N_Subtype_Indication
4096 then
4097 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
4099 elsif Is_Constrained (Full_View (Parent_Type)) then
4100 Set_Underlying_Full_View (Derived_Type,
4101 Full_View (Parent_Type));
4102 end if;
4104 else
4105 -- If there are new discriminants, the parent subtype is
4106 -- constrained by them, but it is not clear how to build
4107 -- the underlying_full_view in this case ???
4109 null;
4110 end if;
4111 end if;
4112 end if;
4114 -- Build partial view of derived type from partial view of parent.
4116 Build_Derived_Record_Type
4117 (N, Parent_Type, Derived_Type, Derive_Subps);
4119 if Present (Full_View (Parent_Type))
4120 and then not Is_Completion
4121 then
4122 if not In_Open_Scopes (Par_Scope)
4123 or else not In_Same_Source_Unit (N, Parent_Type)
4124 then
4125 -- Swap partial and full views temporarily
4127 Install_Private_Declarations (Par_Scope);
4128 Install_Visible_Declarations (Par_Scope);
4129 Swapped := True;
4130 end if;
4132 -- Build full view of derived type from full view of
4133 -- parent which is now installed.
4134 -- Subprograms have been derived on the partial view,
4135 -- the completion does not derive them anew.
4137 if not Is_Tagged_Type (Parent_Type) then
4138 Build_Derived_Record_Type
4139 (Full_Decl, Parent_Type, Full_Der, False);
4140 else
4142 -- If full view of parent is tagged, the completion
4143 -- inherits the proper primitive operations.
4145 Set_Defining_Identifier (Full_Decl, Full_Der);
4146 Build_Derived_Record_Type
4147 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
4148 Set_Analyzed (Full_Decl);
4149 end if;
4151 if Swapped then
4152 Uninstall_Declarations (Par_Scope);
4154 if In_Open_Scopes (Par_Scope) then
4155 Install_Visible_Declarations (Par_Scope);
4156 end if;
4157 end if;
4159 Der_Base := Base_Type (Derived_Type);
4160 Set_Full_View (Derived_Type, Full_Der);
4161 Set_Full_View (Der_Base, Base_Type (Full_Der));
4163 -- Copy the discriminant list from full view to
4164 -- the partial views (base type and its subtype).
4165 -- Gigi requires that the partial and full views
4166 -- have the same discriminants.
4167 -- ??? Note that since the partial view is pointing
4168 -- to discriminants in the full view, their scope
4169 -- will be that of the full view. This might
4170 -- cause some front end problems and need
4171 -- adjustment?
4173 Discr := First_Discriminant (Base_Type (Full_Der));
4174 Set_First_Entity (Der_Base, Discr);
4176 loop
4177 Last_Discr := Discr;
4178 Next_Discriminant (Discr);
4179 exit when No (Discr);
4180 end loop;
4182 Set_Last_Entity (Der_Base, Last_Discr);
4184 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
4185 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
4186 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
4188 else
4189 -- If this is a completion, the derived type stays private
4190 -- and there is no need to create a further full view, except
4191 -- in the unusual case when the derivation is nested within a
4192 -- child unit, see below.
4194 null;
4195 end if;
4197 elsif Present (Full_View (Parent_Type))
4198 and then Has_Discriminants (Full_View (Parent_Type))
4199 then
4200 if Has_Unknown_Discriminants (Parent_Type)
4201 and then Nkind (Subtype_Indication (Type_Definition (N)))
4202 = N_Subtype_Indication
4203 then
4204 Error_Msg_N
4205 ("cannot constrain type with unknown discriminants",
4206 Subtype_Indication (Type_Definition (N)));
4207 return;
4208 end if;
4210 -- If full view of parent is a record type, Build full view as
4211 -- a derivation from the parent's full view. Partial view remains
4212 -- private. For code generation and linking, the full view must
4213 -- have the same public status as the partial one. This full view
4214 -- is only needed if the parent type is in an enclosing scope, so
4215 -- that the full view may actually become visible, e.g. in a child
4216 -- unit. This is both more efficient, and avoids order of freezing
4217 -- problems with the added entities.
4219 if not Is_Private_Type (Full_View (Parent_Type))
4220 and then (In_Open_Scopes (Scope (Parent_Type)))
4221 then
4222 Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
4223 Chars (Derived_Type));
4224 Set_Is_Itype (Full_Der);
4225 Set_Has_Private_Declaration (Full_Der);
4226 Set_Has_Private_Declaration (Derived_Type);
4227 Set_Associated_Node_For_Itype (Full_Der, N);
4228 Set_Parent (Full_Der, Parent (Derived_Type));
4229 Set_Full_View (Derived_Type, Full_Der);
4230 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
4231 Full_P := Full_View (Parent_Type);
4232 Exchange_Declarations (Parent_Type);
4233 Copy_And_Build;
4234 Exchange_Declarations (Full_P);
4236 else
4237 Build_Derived_Record_Type
4238 (N, Full_View (Parent_Type), Derived_Type,
4239 Derive_Subps => False);
4240 end if;
4242 -- In any case, the primitive operations are inherited from
4243 -- the parent type, not from the internal full view.
4245 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
4247 if Derive_Subps then
4248 Derive_Subprograms (Parent_Type, Derived_Type);
4249 end if;
4251 else
4252 -- Untagged type, No discriminants on either view
4254 if Nkind (Subtype_Indication (Type_Definition (N)))
4255 = N_Subtype_Indication
4256 then
4257 Error_Msg_N
4258 ("illegal constraint on type without discriminants", N);
4259 end if;
4261 if Present (Discriminant_Specifications (N))
4262 and then Present (Full_View (Parent_Type))
4263 and then not Is_Tagged_Type (Full_View (Parent_Type))
4264 then
4265 Error_Msg_N
4266 ("cannot add discriminants to untagged type", N);
4267 end if;
4269 Set_Stored_Constraint (Derived_Type, No_Elist);
4270 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4271 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
4272 Set_Has_Controlled_Component
4273 (Derived_Type, Has_Controlled_Component
4274 (Parent_Type));
4276 -- Direct controlled types do not inherit Finalize_Storage_Only flag
4278 if not Is_Controlled (Parent_Type) then
4279 Set_Finalize_Storage_Only
4280 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
4281 end if;
4283 -- Construct the implicit full view by deriving from full
4284 -- view of the parent type. In order to get proper visibility,
4285 -- we install the parent scope and its declarations.
4287 -- ??? if the parent is untagged private and its
4288 -- completion is tagged, this mechanism will not
4289 -- work because we cannot derive from the tagged
4290 -- full view unless we have an extension
4292 if Present (Full_View (Parent_Type))
4293 and then not Is_Tagged_Type (Full_View (Parent_Type))
4294 and then not Is_Completion
4295 then
4296 Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
4297 Chars (Derived_Type));
4298 Set_Is_Itype (Full_Der);
4299 Set_Has_Private_Declaration (Full_Der);
4300 Set_Has_Private_Declaration (Derived_Type);
4301 Set_Associated_Node_For_Itype (Full_Der, N);
4302 Set_Parent (Full_Der, Parent (Derived_Type));
4303 Set_Full_View (Derived_Type, Full_Der);
4305 if not In_Open_Scopes (Par_Scope) then
4306 Install_Private_Declarations (Par_Scope);
4307 Install_Visible_Declarations (Par_Scope);
4308 Copy_And_Build;
4309 Uninstall_Declarations (Par_Scope);
4311 -- If parent scope is open and in another unit, and
4312 -- parent has a completion, then the derivation is taking
4313 -- place in the visible part of a child unit. In that
4314 -- case retrieve the full view of the parent momentarily.
4316 elsif not In_Same_Source_Unit (N, Parent_Type) then
4317 Full_P := Full_View (Parent_Type);
4318 Exchange_Declarations (Parent_Type);
4319 Copy_And_Build;
4320 Exchange_Declarations (Full_P);
4322 -- Otherwise it is a local derivation.
4324 else
4325 Copy_And_Build;
4326 end if;
4328 Set_Scope (Full_Der, Current_Scope);
4329 Set_Is_First_Subtype (Full_Der,
4330 Is_First_Subtype (Derived_Type));
4331 Set_Has_Size_Clause (Full_Der, False);
4332 Set_Has_Alignment_Clause (Full_Der, False);
4333 Set_Next_Entity (Full_Der, Empty);
4334 Set_Has_Delayed_Freeze (Full_Der);
4335 Set_Is_Frozen (Full_Der, False);
4336 Set_Freeze_Node (Full_Der, Empty);
4337 Set_Depends_On_Private (Full_Der,
4338 Has_Private_Component (Full_Der));
4339 Set_Public_Status (Full_Der);
4340 end if;
4341 end if;
4343 Set_Has_Unknown_Discriminants (Derived_Type,
4344 Has_Unknown_Discriminants (Parent_Type));
4346 if Is_Private_Type (Derived_Type) then
4347 Set_Private_Dependents (Derived_Type, New_Elmt_List);
4348 end if;
4350 if Is_Private_Type (Parent_Type)
4351 and then Base_Type (Parent_Type) = Parent_Type
4352 and then In_Open_Scopes (Scope (Parent_Type))
4353 then
4354 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
4356 if Is_Child_Unit (Scope (Current_Scope))
4357 and then Is_Completion
4358 and then In_Private_Part (Current_Scope)
4359 and then Scope (Parent_Type) /= Current_Scope
4360 then
4361 -- This is the unusual case where a type completed by a private
4362 -- derivation occurs within a package nested in a child unit,
4363 -- and the parent is declared in an ancestor. In this case, the
4364 -- full view of the parent type will become visible in the body
4365 -- of the enclosing child, and only then will the current type
4366 -- be possibly non-private. We build a underlying full view that
4367 -- will be installed when the enclosing child body is compiled.
4369 declare
4370 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
4372 begin
4373 Full_Der :=
4374 Make_Defining_Identifier (Sloc (Derived_Type),
4375 Chars (Derived_Type));
4376 Set_Is_Itype (Full_Der);
4377 Set_Itype (IR, Full_Der);
4378 Insert_After (N, IR);
4380 -- The full view will be used to swap entities on entry/exit
4381 -- to the body, and must appear in the entity list for the
4382 -- package.
4384 Append_Entity (Full_Der, Scope (Derived_Type));
4385 Set_Has_Private_Declaration (Full_Der);
4386 Set_Has_Private_Declaration (Derived_Type);
4387 Set_Associated_Node_For_Itype (Full_Der, N);
4388 Set_Parent (Full_Der, Parent (Derived_Type));
4389 Full_P := Full_View (Parent_Type);
4390 Exchange_Declarations (Parent_Type);
4391 Copy_And_Build;
4392 Exchange_Declarations (Full_P);
4393 Set_Underlying_Full_View (Derived_Type, Full_Der);
4394 end;
4395 end if;
4396 end if;
4397 end Build_Derived_Private_Type;
4399 -------------------------------
4400 -- Build_Derived_Record_Type --
4401 -------------------------------
4403 -- 1. INTRODUCTION.
4405 -- Ideally we would like to use the same model of type derivation for
4406 -- tagged and untagged record types. Unfortunately this is not quite
4407 -- possible because the semantics of representation clauses is different
4408 -- for tagged and untagged records under inheritance. Consider the
4409 -- following:
4411 -- type R (...) is [tagged] record ... end record;
4412 -- type T (...) is new R (...) [with ...];
4414 -- The representation clauses of T can specify a completely different
4415 -- record layout from R's. Hence the same component can be placed in
4416 -- two very different positions in objects of type T and R. If R and T
4417 -- are tagged types, representation clauses for T can only specify the
4418 -- layout of non inherited components, thus components that are common
4419 -- in R and T have the same position in objects of type R and T.
4421 -- This has two implications. The first is that the entire tree for R's
4422 -- declaration needs to be copied for T in the untagged case, so that
4423 -- T can be viewed as a record type of its own with its own representation
4424 -- clauses. The second implication is the way we handle discriminants.
4425 -- Specifically, in the untagged case we need a way to communicate to Gigi
4426 -- what are the real discriminants in the record, while for the semantics
4427 -- we need to consider those introduced by the user to rename the
4428 -- discriminants in the parent type. This is handled by introducing the
4429 -- notion of stored discriminants. See below for more.
4431 -- Fortunately the way regular components are inherited can be handled in
4432 -- the same way in tagged and untagged types.
4434 -- To complicate things a bit more the private view of a private extension
4435 -- cannot be handled in the same way as the full view (for one thing the
4436 -- semantic rules are somewhat different). We will explain what differs
4437 -- below.
4439 -- 2. DISCRIMINANTS UNDER INHERITANCE.
4441 -- The semantic rules governing the discriminants of derived types are
4442 -- quite subtle.
4444 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
4445 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
4447 -- If parent type has discriminants, then the discriminants that are
4448 -- declared in the derived type are [3.4 (11)]:
4450 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
4451 -- there is one;
4453 -- o Otherwise, each discriminant of the parent type (implicitly
4454 -- declared in the same order with the same specifications). In this
4455 -- case, the discriminants are said to be "inherited", or if unknown in
4456 -- the parent are also unknown in the derived type.
4458 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
4460 -- o The parent subtype shall be constrained;
4462 -- o If the parent type is not a tagged type, then each discriminant of
4463 -- the derived type shall be used in the constraint defining a parent
4464 -- subtype [Implementation note: this ensures that the new discriminant
4465 -- can share storage with an existing discriminant.].
4467 -- For the derived type each discriminant of the parent type is either
4468 -- inherited, constrained to equal some new discriminant of the derived
4469 -- type, or constrained to the value of an expression.
4471 -- When inherited or constrained to equal some new discriminant, the
4472 -- parent discriminant and the discriminant of the derived type are said
4473 -- to "correspond".
4475 -- If a discriminant of the parent type is constrained to a specific value
4476 -- in the derived type definition, then the discriminant is said to be
4477 -- "specified" by that derived type definition.
4479 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES.
4481 -- We have spoken about stored discriminants in point 1 (introduction)
4482 -- above. There are two sort of stored discriminants: implicit and
4483 -- explicit. As long as the derived type inherits the same discriminants as
4484 -- the root record type, stored discriminants are the same as regular
4485 -- discriminants, and are said to be implicit. However, if any discriminant
4486 -- in the root type was renamed in the derived type, then the derived
4487 -- type will contain explicit stored discriminants. Explicit stored
4488 -- discriminants are discriminants in addition to the semantically visible
4489 -- discriminants defined for the derived type. Stored discriminants are
4490 -- used by Gigi to figure out what are the physical discriminants in
4491 -- objects of the derived type (see precise definition in einfo.ads).
4492 -- As an example, consider the following:
4494 -- type R (D1, D2, D3 : Int) is record ... end record;
4495 -- type T1 is new R;
4496 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
4497 -- type T3 is new T2;
4498 -- type T4 (Y : Int) is new T3 (Y, 99);
4500 -- The following table summarizes the discriminants and stored
4501 -- discriminants in R and T1 through T4.
4503 -- Type Discrim Stored Discrim Comment
4504 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
4505 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
4506 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
4507 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
4508 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
4510 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
4511 -- find the corresponding discriminant in the parent type, while
4512 -- Original_Record_Component (abbreviated ORC below), the actual physical
4513 -- component that is renamed. Finally the field Is_Completely_Hidden
4514 -- (abbreviated ICH below) is set for all explicit stored discriminants
4515 -- (see einfo.ads for more info). For the above example this gives:
4517 -- Discrim CD ORC ICH
4518 -- ^^^^^^^ ^^ ^^^ ^^^
4519 -- D1 in R empty itself no
4520 -- D2 in R empty itself no
4521 -- D3 in R empty itself no
4523 -- D1 in T1 D1 in R itself no
4524 -- D2 in T1 D2 in R itself no
4525 -- D3 in T1 D3 in R itself no
4527 -- X1 in T2 D3 in T1 D3 in T2 no
4528 -- X2 in T2 D1 in T1 D1 in T2 no
4529 -- D1 in T2 empty itself yes
4530 -- D2 in T2 empty itself yes
4531 -- D3 in T2 empty itself yes
4533 -- X1 in T3 X1 in T2 D3 in T3 no
4534 -- X2 in T3 X2 in T2 D1 in T3 no
4535 -- D1 in T3 empty itself yes
4536 -- D2 in T3 empty itself yes
4537 -- D3 in T3 empty itself yes
4539 -- Y in T4 X1 in T3 D3 in T3 no
4540 -- D1 in T3 empty itself yes
4541 -- D2 in T3 empty itself yes
4542 -- D3 in T3 empty itself yes
4544 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES.
4546 -- Type derivation for tagged types is fairly straightforward. if no
4547 -- discriminants are specified by the derived type, these are inherited
4548 -- from the parent. No explicit stored discriminants are ever necessary.
4549 -- The only manipulation that is done to the tree is that of adding a
4550 -- _parent field with parent type and constrained to the same constraint
4551 -- specified for the parent in the derived type definition. For instance:
4553 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
4554 -- type T1 is new R with null record;
4555 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
4557 -- are changed into :
4559 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
4560 -- _parent : R (D1, D2, D3);
4561 -- end record;
4563 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
4564 -- _parent : T1 (X2, 88, X1);
4565 -- end record;
4567 -- The discriminants actually present in R, T1 and T2 as well as their CD,
4568 -- ORC and ICH fields are:
4570 -- Discrim CD ORC ICH
4571 -- ^^^^^^^ ^^ ^^^ ^^^
4572 -- D1 in R empty itself no
4573 -- D2 in R empty itself no
4574 -- D3 in R empty itself no
4576 -- D1 in T1 D1 in R D1 in R no
4577 -- D2 in T1 D2 in R D2 in R no
4578 -- D3 in T1 D3 in R D3 in R no
4580 -- X1 in T2 D3 in T1 D3 in R no
4581 -- X2 in T2 D1 in T1 D1 in R no
4583 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS.
4585 -- Regardless of whether we dealing with a tagged or untagged type
4586 -- we will transform all derived type declarations of the form
4588 -- type T is new R (...) [with ...];
4589 -- or
4590 -- subtype S is R (...);
4591 -- type T is new S [with ...];
4592 -- into
4593 -- type BT is new R [with ...];
4594 -- subtype T is BT (...);
4596 -- That is, the base derived type is constrained only if it has no
4597 -- discriminants. The reason for doing this is that GNAT's semantic model
4598 -- assumes that a base type with discriminants is unconstrained.
4600 -- Note that, strictly speaking, the above transformation is not always
4601 -- correct. Consider for instance the following excerpt from ACVC b34011a:
4603 -- procedure B34011A is
4604 -- type REC (D : integer := 0) is record
4605 -- I : Integer;
4606 -- end record;
4608 -- package P is
4609 -- type T6 is new Rec;
4610 -- function F return T6;
4611 -- end P;
4613 -- use P;
4614 -- package Q6 is
4615 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
4616 -- end Q6;
4618 -- The definition of Q6.U is illegal. However transforming Q6.U into
4620 -- type BaseU is new T6;
4621 -- subtype U is BaseU (Q6.F.I)
4623 -- turns U into a legal subtype, which is incorrect. To avoid this problem
4624 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
4625 -- the transformation described above.
4627 -- There is another instance where the above transformation is incorrect.
4628 -- Consider:
4630 -- package Pack is
4631 -- type Base (D : Integer) is tagged null record;
4632 -- procedure P (X : Base);
4634 -- type Der is new Base (2) with null record;
4635 -- procedure P (X : Der);
4636 -- end Pack;
4638 -- Then the above transformation turns this into
4640 -- type Der_Base is new Base with null record;
4641 -- -- procedure P (X : Base) is implicitly inherited here
4642 -- -- as procedure P (X : Der_Base).
4644 -- subtype Der is Der_Base (2);
4645 -- procedure P (X : Der);
4646 -- -- The overriding of P (X : Der_Base) is illegal since we
4647 -- -- have a parameter conformance problem.
4649 -- To get around this problem, after having semantically processed Der_Base
4650 -- and the rewritten subtype declaration for Der, we copy Der_Base field
4651 -- Discriminant_Constraint from Der so that when parameter conformance is
4652 -- checked when P is overridden, no semantic errors are flagged.
4654 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS.
4656 -- Regardless of whether we are dealing with a tagged or untagged type
4657 -- we will transform all derived type declarations of the form
4659 -- type R (D1, .., Dn : ...) is [tagged] record ...;
4660 -- type T is new R [with ...];
4661 -- into
4662 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
4664 -- The reason for such transformation is that it allows us to implement a
4665 -- very clean form of component inheritance as explained below.
4667 -- Note that this transformation is not achieved by direct tree rewriting
4668 -- and manipulation, but rather by redoing the semantic actions that the
4669 -- above transformation will entail. This is done directly in routine
4670 -- Inherit_Components.
4672 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE.
4674 -- In both tagged and untagged derived types, regular non discriminant
4675 -- components are inherited in the derived type from the parent type. In
4676 -- the absence of discriminants component, inheritance is straightforward
4677 -- as components can simply be copied from the parent.
4678 -- If the parent has discriminants, inheriting components constrained with
4679 -- these discriminants requires caution. Consider the following example:
4681 -- type R (D1, D2 : Positive) is [tagged] record
4682 -- S : String (D1 .. D2);
4683 -- end record;
4685 -- type T1 is new R [with null record];
4686 -- type T2 (X : positive) is new R (1, X) [with null record];
4688 -- As explained in 6. above, T1 is rewritten as
4690 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
4692 -- which makes the treatment for T1 and T2 identical.
4694 -- What we want when inheriting S, is that references to D1 and D2 in R are
4695 -- replaced with references to their correct constraints, ie D1 and D2 in
4696 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
4697 -- with either discriminant references in the derived type or expressions.
4698 -- This replacement is achieved as follows: before inheriting R's
4699 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
4700 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
4701 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
4702 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
4703 -- by String (1 .. X).
4705 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS.
4707 -- We explain here the rules governing private type extensions relevant to
4708 -- type derivation. These rules are explained on the following example:
4710 -- type D [(...)] is new A [(...)] with private; <-- partial view
4711 -- type D [(...)] is new P [(...)] with null record; <-- full view
4713 -- Type A is called the ancestor subtype of the private extension.
4714 -- Type P is the parent type of the full view of the private extension. It
4715 -- must be A or a type derived from A.
4717 -- The rules concerning the discriminants of private type extensions are
4718 -- [7.3(10-13)]:
4720 -- o If a private extension inherits known discriminants from the ancestor
4721 -- subtype, then the full view shall also inherit its discriminants from
4722 -- the ancestor subtype and the parent subtype of the full view shall be
4723 -- constrained if and only if the ancestor subtype is constrained.
4725 -- o If a partial view has unknown discriminants, then the full view may
4726 -- define a definite or an indefinite subtype, with or without
4727 -- discriminants.
4729 -- o If a partial view has neither known nor unknown discriminants, then
4730 -- the full view shall define a definite subtype.
4732 -- o If the ancestor subtype of a private extension has constrained
4733 -- discriminants, then the parent subtype of the full view shall impose a
4734 -- statically matching constraint on those discriminants.
4736 -- This means that only the following forms of private extensions are
4737 -- allowed:
4739 -- type D is new A with private; <-- partial view
4740 -- type D is new P with null record; <-- full view
4742 -- If A has no discriminants than P has no discriminants, otherwise P must
4743 -- inherit A's discriminants.
4745 -- type D is new A (...) with private; <-- partial view
4746 -- type D is new P (:::) with null record; <-- full view
4748 -- P must inherit A's discriminants and (...) and (:::) must statically
4749 -- match.
4751 -- subtype A is R (...);
4752 -- type D is new A with private; <-- partial view
4753 -- type D is new P with null record; <-- full view
4755 -- P must have inherited R's discriminants and must be derived from A or
4756 -- any of its subtypes.
4758 -- type D (..) is new A with private; <-- partial view
4759 -- type D (..) is new P [(:::)] with null record; <-- full view
4761 -- No specific constraints on P's discriminants or constraint (:::).
4762 -- Note that A can be unconstrained, but the parent subtype P must either
4763 -- be constrained or (:::) must be present.
4765 -- type D (..) is new A [(...)] with private; <-- partial view
4766 -- type D (..) is new P [(:::)] with null record; <-- full view
4768 -- P's constraints on A's discriminants must statically match those
4769 -- imposed by (...).
4771 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS.
4773 -- The full view of a private extension is handled exactly as described
4774 -- above. The model chose for the private view of a private extension
4775 -- is the same for what concerns discriminants (ie they receive the same
4776 -- treatment as in the tagged case). However, the private view of the
4777 -- private extension always inherits the components of the parent base,
4778 -- without replacing any discriminant reference. Strictly speaking this
4779 -- is incorrect. However, Gigi never uses this view to generate code so
4780 -- this is a purely semantic issue. In theory, a set of transformations
4781 -- similar to those given in 5. and 6. above could be applied to private
4782 -- views of private extensions to have the same model of component
4783 -- inheritance as for non private extensions. However, this is not done
4784 -- because it would further complicate private type processing.
4785 -- Semantically speaking, this leaves us in an uncomfortable
4786 -- situation. As an example consider:
4788 -- package Pack is
4789 -- type R (D : integer) is tagged record
4790 -- S : String (1 .. D);
4791 -- end record;
4792 -- procedure P (X : R);
4793 -- type T is new R (1) with private;
4794 -- private
4795 -- type T is new R (1) with null record;
4796 -- end;
4798 -- This is transformed into:
4800 -- package Pack is
4801 -- type R (D : integer) is tagged record
4802 -- S : String (1 .. D);
4803 -- end record;
4804 -- procedure P (X : R);
4805 -- type T is new R (1) with private;
4806 -- private
4807 -- type BaseT is new R with null record;
4808 -- subtype T is BaseT (1);
4809 -- end;
4811 -- (strictly speaking the above is incorrect Ada).
4813 -- From the semantic standpoint the private view of private extension T
4814 -- should be flagged as constrained since one can clearly have
4816 -- Obj : T;
4818 -- in a unit withing Pack. However, when deriving subprograms for the
4819 -- private view of private extension T, T must be seen as unconstrained
4820 -- since T has discriminants (this is a constraint of the current
4821 -- subprogram derivation model). Thus, when processing the private view of
4822 -- a private extension such as T, we first mark T as unconstrained, we
4823 -- process it, we perform program derivation and just before returning from
4824 -- Build_Derived_Record_Type we mark T as constrained.
4825 -- ??? Are there are other uncomfortable cases that we will have to
4826 -- deal with.
4828 -- 10. RECORD_TYPE_WITH_PRIVATE complications.
4830 -- Types that are derived from a visible record type and have a private
4831 -- extension present other peculiarities. They behave mostly like private
4832 -- types, but if they have primitive operations defined, these will not
4833 -- have the proper signatures for further inheritance, because other
4834 -- primitive operations will use the implicit base that we define for
4835 -- private derivations below. This affect subprogram inheritance (see
4836 -- Derive_Subprograms for details). We also derive the implicit base from
4837 -- the base type of the full view, so that the implicit base is a record
4838 -- type and not another private type, This avoids infinite loops.
4840 procedure Build_Derived_Record_Type
4841 (N : Node_Id;
4842 Parent_Type : Entity_Id;
4843 Derived_Type : Entity_Id;
4844 Derive_Subps : Boolean := True)
4846 Loc : constant Source_Ptr := Sloc (N);
4847 Parent_Base : Entity_Id;
4849 Type_Def : Node_Id;
4850 Indic : Node_Id;
4852 Discrim : Entity_Id;
4853 Last_Discrim : Entity_Id;
4854 Constrs : Elist_Id;
4855 Discs : Elist_Id := New_Elmt_List;
4856 -- An empty Discs list means that there were no constraints in the
4857 -- subtype indication or that there was an error processing it.
4859 Assoc_List : Elist_Id;
4860 New_Discrs : Elist_Id;
4862 New_Base : Entity_Id;
4863 New_Decl : Node_Id;
4864 New_Indic : Node_Id;
4866 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
4867 Discriminant_Specs : constant Boolean :=
4868 Present (Discriminant_Specifications (N));
4869 Private_Extension : constant Boolean :=
4870 (Nkind (N) = N_Private_Extension_Declaration);
4872 Constraint_Present : Boolean;
4873 Inherit_Discrims : Boolean := False;
4875 Save_Etype : Entity_Id;
4876 Save_Discr_Constr : Elist_Id;
4877 Save_Next_Entity : Entity_Id;
4879 begin
4880 if Ekind (Parent_Type) = E_Record_Type_With_Private
4881 and then Present (Full_View (Parent_Type))
4882 and then Has_Discriminants (Parent_Type)
4883 then
4884 Parent_Base := Base_Type (Full_View (Parent_Type));
4885 else
4886 Parent_Base := Base_Type (Parent_Type);
4887 end if;
4889 -- Before we start the previously documented transformations, here is
4890 -- a little fix for size and alignment of tagged types. Normally when
4891 -- we derive type D from type P, we copy the size and alignment of P
4892 -- as the default for D, and in the absence of explicit representation
4893 -- clauses for D, the size and alignment are indeed the same as the
4894 -- parent.
4896 -- But this is wrong for tagged types, since fields may be added,
4897 -- and the default size may need to be larger, and the default
4898 -- alignment may need to be larger.
4900 -- We therefore reset the size and alignment fields in the tagged
4901 -- case. Note that the size and alignment will in any case be at
4902 -- least as large as the parent type (since the derived type has
4903 -- a copy of the parent type in the _parent field)
4905 if Is_Tagged then
4906 Init_Size_Align (Derived_Type);
4907 end if;
4909 -- STEP 0a: figure out what kind of derived type declaration we have.
4911 if Private_Extension then
4912 Type_Def := N;
4913 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
4915 else
4916 Type_Def := Type_Definition (N);
4918 -- Ekind (Parent_Base) in not necessarily E_Record_Type since
4919 -- Parent_Base can be a private type or private extension. However,
4920 -- for tagged types with an extension the newly added fields are
4921 -- visible and hence the Derived_Type is always an E_Record_Type.
4922 -- (except that the parent may have its own private fields).
4923 -- For untagged types we preserve the Ekind of the Parent_Base.
4925 if Present (Record_Extension_Part (Type_Def)) then
4926 Set_Ekind (Derived_Type, E_Record_Type);
4927 else
4928 Set_Ekind (Derived_Type, Ekind (Parent_Base));
4929 end if;
4930 end if;
4932 -- Indic can either be an N_Identifier if the subtype indication
4933 -- contains no constraint or an N_Subtype_Indication if the subtype
4934 -- indication has a constraint.
4936 Indic := Subtype_Indication (Type_Def);
4937 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
4939 -- Check that the type has visible discriminants. The type may be
4940 -- a private type with unknown discriminants whose full view has
4941 -- discriminants which are invisible.
4943 if Constraint_Present then
4944 if not Has_Discriminants (Parent_Base)
4945 or else
4946 (Has_Unknown_Discriminants (Parent_Base)
4947 and then Is_Private_Type (Parent_Base))
4948 then
4949 Error_Msg_N
4950 ("invalid constraint: type has no discriminant",
4951 Constraint (Indic));
4953 Constraint_Present := False;
4954 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
4956 elsif Is_Constrained (Parent_Type) then
4957 Error_Msg_N
4958 ("invalid constraint: parent type is already constrained",
4959 Constraint (Indic));
4961 Constraint_Present := False;
4962 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
4963 end if;
4964 end if;
4966 -- STEP 0b: If needed, apply transformation given in point 5. above.
4968 if not Private_Extension
4969 and then Has_Discriminants (Parent_Type)
4970 and then not Discriminant_Specs
4971 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
4972 then
4973 -- First, we must analyze the constraint (see comment in point 5.).
4975 if Constraint_Present then
4976 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
4978 if Has_Discriminants (Derived_Type)
4979 and then Has_Private_Declaration (Derived_Type)
4980 and then Present (Discriminant_Constraint (Derived_Type))
4981 then
4982 -- Verify that constraints of the full view conform to those
4983 -- given in partial view.
4985 declare
4986 C1, C2 : Elmt_Id;
4988 begin
4989 C1 := First_Elmt (New_Discrs);
4990 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
4992 while Present (C1) and then Present (C2) loop
4993 if not
4994 Fully_Conformant_Expressions (Node (C1), Node (C2))
4995 then
4996 Error_Msg_N (
4997 "constraint not conformant to previous declaration",
4998 Node (C1));
4999 end if;
5000 Next_Elmt (C1);
5001 Next_Elmt (C2);
5002 end loop;
5003 end;
5004 end if;
5005 end if;
5007 -- Insert and analyze the declaration for the unconstrained base type
5009 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
5011 New_Decl :=
5012 Make_Full_Type_Declaration (Loc,
5013 Defining_Identifier => New_Base,
5014 Type_Definition =>
5015 Make_Derived_Type_Definition (Loc,
5016 Abstract_Present => Abstract_Present (Type_Def),
5017 Subtype_Indication =>
5018 New_Occurrence_Of (Parent_Base, Loc),
5019 Record_Extension_Part =>
5020 Relocate_Node (Record_Extension_Part (Type_Def))));
5022 Set_Parent (New_Decl, Parent (N));
5023 Mark_Rewrite_Insertion (New_Decl);
5024 Insert_Before (N, New_Decl);
5026 -- Note that this call passes False for the Derive_Subps
5027 -- parameter because subprogram derivation is deferred until
5028 -- after creating the subtype (see below).
5030 Build_Derived_Type
5031 (New_Decl, Parent_Base, New_Base,
5032 Is_Completion => True, Derive_Subps => False);
5034 -- ??? This needs re-examination to determine whether the
5035 -- above call can simply be replaced by a call to Analyze.
5037 Set_Analyzed (New_Decl);
5039 -- Insert and analyze the declaration for the constrained subtype
5041 if Constraint_Present then
5042 New_Indic :=
5043 Make_Subtype_Indication (Loc,
5044 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
5045 Constraint => Relocate_Node (Constraint (Indic)));
5047 else
5048 declare
5049 Constr_List : constant List_Id := New_List;
5050 C : Elmt_Id;
5051 Expr : Node_Id;
5053 begin
5054 C := First_Elmt (Discriminant_Constraint (Parent_Type));
5055 while Present (C) loop
5056 Expr := Node (C);
5058 -- It is safe here to call New_Copy_Tree since
5059 -- Force_Evaluation was called on each constraint in
5060 -- Build_Discriminant_Constraints.
5062 Append (New_Copy_Tree (Expr), To => Constr_List);
5064 Next_Elmt (C);
5065 end loop;
5067 New_Indic :=
5068 Make_Subtype_Indication (Loc,
5069 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
5070 Constraint =>
5071 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
5072 end;
5073 end if;
5075 Rewrite (N,
5076 Make_Subtype_Declaration (Loc,
5077 Defining_Identifier => Derived_Type,
5078 Subtype_Indication => New_Indic));
5080 Analyze (N);
5082 -- Derivation of subprograms must be delayed until the
5083 -- full subtype has been established to ensure proper
5084 -- overriding of subprograms inherited by full types.
5085 -- If the derivations occurred as part of the call to
5086 -- Build_Derived_Type above, then the check for type
5087 -- conformance would fail because earlier primitive
5088 -- subprograms could still refer to the full type prior
5089 -- the change to the new subtype and hence wouldn't
5090 -- match the new base type created here.
5092 Derive_Subprograms (Parent_Type, Derived_Type);
5094 -- For tagged types the Discriminant_Constraint of the new base itype
5095 -- is inherited from the first subtype so that no subtype conformance
5096 -- problem arise when the first subtype overrides primitive
5097 -- operations inherited by the implicit base type.
5099 if Is_Tagged then
5100 Set_Discriminant_Constraint
5101 (New_Base, Discriminant_Constraint (Derived_Type));
5102 end if;
5104 return;
5105 end if;
5107 -- If we get here Derived_Type will have no discriminants or it will be
5108 -- a discriminated unconstrained base type.
5110 -- STEP 1a: perform preliminary actions/checks for derived tagged types
5112 if Is_Tagged then
5113 -- The parent type is frozen for non-private extensions (RM 13.14(7))
5115 if not Private_Extension then
5116 Freeze_Before (N, Parent_Type);
5117 end if;
5119 if Type_Access_Level (Derived_Type) /= Type_Access_Level (Parent_Type)
5120 and then not Is_Generic_Type (Derived_Type)
5121 then
5122 if Is_Controlled (Parent_Type) then
5123 Error_Msg_N
5124 ("controlled type must be declared at the library level",
5125 Indic);
5126 else
5127 Error_Msg_N
5128 ("type extension at deeper accessibility level than parent",
5129 Indic);
5130 end if;
5132 else
5133 declare
5134 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
5136 begin
5137 if Present (GB)
5138 and then GB /= Enclosing_Generic_Body (Parent_Base)
5139 then
5140 Error_Msg_NE
5141 ("parent type of& must not be outside generic body"
5142 & " ('R'M 3.9.1(4))",
5143 Indic, Derived_Type);
5144 end if;
5145 end;
5146 end if;
5147 end if;
5149 -- STEP 1b : preliminary cleanup of the full view of private types
5151 -- If the type is already marked as having discriminants, then it's the
5152 -- completion of a private type or private extension and we need to
5153 -- retain the discriminants from the partial view if the current
5154 -- declaration has Discriminant_Specifications so that we can verify
5155 -- conformance. However, we must remove any existing components that
5156 -- were inherited from the parent (and attached in Copy_And_Swap)
5157 -- because the full type inherits all appropriate components anyway, and
5158 -- we don't want the partial view's components interfering.
5160 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
5161 Discrim := First_Discriminant (Derived_Type);
5162 loop
5163 Last_Discrim := Discrim;
5164 Next_Discriminant (Discrim);
5165 exit when No (Discrim);
5166 end loop;
5168 Set_Last_Entity (Derived_Type, Last_Discrim);
5170 -- In all other cases wipe out the list of inherited components (even
5171 -- inherited discriminants), it will be properly rebuilt here.
5173 else
5174 Set_First_Entity (Derived_Type, Empty);
5175 Set_Last_Entity (Derived_Type, Empty);
5176 end if;
5178 -- STEP 1c: Initialize some flags for the Derived_Type
5180 -- The following flags must be initialized here so that
5181 -- Process_Discriminants can check that discriminants of tagged types
5182 -- do not have a default initial value and that access discriminants
5183 -- are only specified for limited records. For completeness, these
5184 -- flags are also initialized along with all the other flags below.
5186 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
5187 Set_Is_Limited_Record (Derived_Type, Is_Limited_Record (Parent_Type));
5189 -- STEP 2a: process discriminants of derived type if any.
5191 New_Scope (Derived_Type);
5193 if Discriminant_Specs then
5194 Set_Has_Unknown_Discriminants (Derived_Type, False);
5196 -- The following call initializes fields Has_Discriminants and
5197 -- Discriminant_Constraint, unless we are processing the completion
5198 -- of a private type declaration.
5200 Check_Or_Process_Discriminants (N, Derived_Type);
5202 -- For non-tagged types the constraint on the Parent_Type must be
5203 -- present and is used to rename the discriminants.
5205 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
5206 Error_Msg_N ("untagged parent must have discriminants", Indic);
5208 elsif not Is_Tagged and then not Constraint_Present then
5209 Error_Msg_N
5210 ("discriminant constraint needed for derived untagged records",
5211 Indic);
5213 -- Otherwise the parent subtype must be constrained unless we have a
5214 -- private extension.
5216 elsif not Constraint_Present
5217 and then not Private_Extension
5218 and then not Is_Constrained (Parent_Type)
5219 then
5220 Error_Msg_N
5221 ("unconstrained type not allowed in this context", Indic);
5223 elsif Constraint_Present then
5224 -- The following call sets the field Corresponding_Discriminant
5225 -- for the discriminants in the Derived_Type.
5227 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
5229 -- For untagged types all new discriminants must rename
5230 -- discriminants in the parent. For private extensions new
5231 -- discriminants cannot rename old ones (implied by [7.3(13)]).
5233 Discrim := First_Discriminant (Derived_Type);
5235 while Present (Discrim) loop
5236 if not Is_Tagged
5237 and then not Present (Corresponding_Discriminant (Discrim))
5238 then
5239 Error_Msg_N
5240 ("new discriminants must constrain old ones", Discrim);
5242 elsif Private_Extension
5243 and then Present (Corresponding_Discriminant (Discrim))
5244 then
5245 Error_Msg_N
5246 ("only static constraints allowed for parent"
5247 & " discriminants in the partial view", Indic);
5248 exit;
5249 end if;
5251 -- If a new discriminant is used in the constraint,
5252 -- then its subtype must be statically compatible
5253 -- with the parent discriminant's subtype (3.7(15)).
5255 if Present (Corresponding_Discriminant (Discrim))
5256 and then
5257 not Subtypes_Statically_Compatible
5258 (Etype (Discrim),
5259 Etype (Corresponding_Discriminant (Discrim)))
5260 then
5261 Error_Msg_N
5262 ("subtype must be compatible with parent discriminant",
5263 Discrim);
5264 end if;
5266 Next_Discriminant (Discrim);
5267 end loop;
5268 end if;
5270 -- STEP 2b: No new discriminants, inherit discriminants if any
5272 else
5273 if Private_Extension then
5274 Set_Has_Unknown_Discriminants
5275 (Derived_Type, Has_Unknown_Discriminants (Parent_Type)
5276 or else Unknown_Discriminants_Present (N));
5278 -- The partial view of the parent may have unknown discriminants,
5279 -- but if the full view has discriminants and the parent type is
5280 -- in scope they must be inherited.
5282 elsif Has_Unknown_Discriminants (Parent_Type)
5283 and then
5284 (not Has_Discriminants (Parent_Type)
5285 or else not In_Open_Scopes (Scope (Parent_Type)))
5286 then
5287 Set_Has_Unknown_Discriminants (Derived_Type);
5288 end if;
5290 if not Has_Unknown_Discriminants (Derived_Type)
5291 and then Has_Discriminants (Parent_Type)
5292 then
5293 Inherit_Discrims := True;
5294 Set_Has_Discriminants
5295 (Derived_Type, True);
5296 Set_Discriminant_Constraint
5297 (Derived_Type, Discriminant_Constraint (Parent_Base));
5298 end if;
5300 -- The following test is true for private types (remember
5301 -- transformation 5. is not applied to those) and in an error
5302 -- situation.
5304 if Constraint_Present then
5305 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
5306 end if;
5308 -- For now mark a new derived type as constrained only if it has no
5309 -- discriminants. At the end of Build_Derived_Record_Type we properly
5310 -- set this flag in the case of private extensions. See comments in
5311 -- point 9. just before body of Build_Derived_Record_Type.
5313 Set_Is_Constrained
5314 (Derived_Type,
5315 not (Inherit_Discrims
5316 or else Has_Unknown_Discriminants (Derived_Type)));
5317 end if;
5319 -- STEP 3: initialize fields of derived type.
5321 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
5322 Set_Stored_Constraint (Derived_Type, No_Elist);
5324 -- Fields inherited from the Parent_Type
5326 Set_Discard_Names
5327 (Derived_Type, Einfo.Discard_Names (Parent_Type));
5328 Set_Has_Specified_Layout
5329 (Derived_Type, Has_Specified_Layout (Parent_Type));
5330 Set_Is_Limited_Composite
5331 (Derived_Type, Is_Limited_Composite (Parent_Type));
5332 Set_Is_Limited_Record
5333 (Derived_Type, Is_Limited_Record (Parent_Type));
5334 Set_Is_Private_Composite
5335 (Derived_Type, Is_Private_Composite (Parent_Type));
5337 -- Fields inherited from the Parent_Base
5339 Set_Has_Controlled_Component
5340 (Derived_Type, Has_Controlled_Component (Parent_Base));
5341 Set_Has_Non_Standard_Rep
5342 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
5343 Set_Has_Primitive_Operations
5344 (Derived_Type, Has_Primitive_Operations (Parent_Base));
5346 -- Direct controlled types do not inherit Finalize_Storage_Only flag
5348 if not Is_Controlled (Parent_Type) then
5349 Set_Finalize_Storage_Only
5350 (Derived_Type, Finalize_Storage_Only (Parent_Type));
5351 end if;
5353 -- Set fields for private derived types.
5355 if Is_Private_Type (Derived_Type) then
5356 Set_Depends_On_Private (Derived_Type, True);
5357 Set_Private_Dependents (Derived_Type, New_Elmt_List);
5359 -- Inherit fields from non private record types. If this is the
5360 -- completion of a derivation from a private type, the parent itself
5361 -- is private, and the attributes come from its full view, which must
5362 -- be present.
5364 else
5365 if Is_Private_Type (Parent_Base)
5366 and then not Is_Record_Type (Parent_Base)
5367 then
5368 Set_Component_Alignment
5369 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
5370 Set_C_Pass_By_Copy
5371 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
5372 else
5373 Set_Component_Alignment
5374 (Derived_Type, Component_Alignment (Parent_Base));
5376 Set_C_Pass_By_Copy
5377 (Derived_Type, C_Pass_By_Copy (Parent_Base));
5378 end if;
5379 end if;
5381 -- Set fields for tagged types
5383 if Is_Tagged then
5384 Set_Primitive_Operations (Derived_Type, New_Elmt_List);
5386 -- All tagged types defined in Ada.Finalization are controlled
5388 if Chars (Scope (Derived_Type)) = Name_Finalization
5389 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
5390 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
5391 then
5392 Set_Is_Controlled (Derived_Type);
5393 else
5394 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
5395 end if;
5397 Make_Class_Wide_Type (Derived_Type);
5398 Set_Is_Abstract (Derived_Type, Abstract_Present (Type_Def));
5400 if Has_Discriminants (Derived_Type)
5401 and then Constraint_Present
5402 then
5403 Set_Stored_Constraint
5404 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
5405 end if;
5407 else
5408 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
5409 Set_Has_Non_Standard_Rep
5410 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
5411 end if;
5413 -- STEP 4: Inherit components from the parent base and constrain them.
5414 -- Apply the second transformation described in point 6. above.
5416 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
5417 or else not Has_Discriminants (Parent_Type)
5418 or else not Is_Constrained (Parent_Type)
5419 then
5420 Constrs := Discs;
5421 else
5422 Constrs := Discriminant_Constraint (Parent_Type);
5423 end if;
5425 Assoc_List := Inherit_Components (N,
5426 Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
5428 -- STEP 5a: Copy the parent record declaration for untagged types
5430 if not Is_Tagged then
5432 -- Discriminant_Constraint (Derived_Type) has been properly
5433 -- constructed. Save it and temporarily set it to Empty because we do
5434 -- not want the call to New_Copy_Tree below to mess this list.
5436 if Has_Discriminants (Derived_Type) then
5437 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
5438 Set_Discriminant_Constraint (Derived_Type, No_Elist);
5439 else
5440 Save_Discr_Constr := No_Elist;
5441 end if;
5443 -- Save the Etype field of Derived_Type. It is correctly set now, but
5444 -- the call to New_Copy tree may remap it to point to itself, which
5445 -- is not what we want. Ditto for the Next_Entity field.
5447 Save_Etype := Etype (Derived_Type);
5448 Save_Next_Entity := Next_Entity (Derived_Type);
5450 -- Assoc_List maps all stored discriminants in the Parent_Base to
5451 -- stored discriminants in the Derived_Type. It is fundamental that
5452 -- no types or itypes with discriminants other than the stored
5453 -- discriminants appear in the entities declared inside
5454 -- Derived_Type. Gigi won't like it.
5456 New_Decl :=
5457 New_Copy_Tree
5458 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
5460 -- Restore the fields saved prior to the New_Copy_Tree call
5461 -- and compute the stored constraint.
5463 Set_Etype (Derived_Type, Save_Etype);
5464 Set_Next_Entity (Derived_Type, Save_Next_Entity);
5466 if Has_Discriminants (Derived_Type) then
5467 Set_Discriminant_Constraint
5468 (Derived_Type, Save_Discr_Constr);
5469 Set_Stored_Constraint
5470 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
5471 Replace_Components (Derived_Type, New_Decl);
5472 end if;
5474 -- Insert the new derived type declaration
5476 Rewrite (N, New_Decl);
5478 -- STEP 5b: Complete the processing for record extensions in generics
5480 -- There is no completion for record extensions declared in the
5481 -- parameter part of a generic, so we need to complete processing for
5482 -- these generic record extensions here. The Record_Type_Definition call
5483 -- will change the Ekind of the components from E_Void to E_Component.
5485 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
5486 Record_Type_Definition (Empty, Derived_Type);
5488 -- STEP 5c: Process the record extension for non private tagged types.
5490 elsif not Private_Extension then
5491 -- Add the _parent field in the derived type.
5493 Expand_Derived_Record (Derived_Type, Type_Def);
5495 -- Analyze the record extension
5497 Record_Type_Definition
5498 (Record_Extension_Part (Type_Def), Derived_Type);
5499 end if;
5501 End_Scope;
5503 if Etype (Derived_Type) = Any_Type then
5504 return;
5505 end if;
5507 -- Set delayed freeze and then derive subprograms, we need to do
5508 -- this in this order so that derived subprograms inherit the
5509 -- derived freeze if necessary.
5511 Set_Has_Delayed_Freeze (Derived_Type);
5512 if Derive_Subps then
5513 Derive_Subprograms (Parent_Type, Derived_Type);
5514 end if;
5516 -- If we have a private extension which defines a constrained derived
5517 -- type mark as constrained here after we have derived subprograms. See
5518 -- comment on point 9. just above the body of Build_Derived_Record_Type.
5520 if Private_Extension and then Inherit_Discrims then
5521 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
5522 Set_Is_Constrained (Derived_Type, True);
5523 Set_Discriminant_Constraint (Derived_Type, Discs);
5525 elsif Is_Constrained (Parent_Type) then
5526 Set_Is_Constrained
5527 (Derived_Type, True);
5528 Set_Discriminant_Constraint
5529 (Derived_Type, Discriminant_Constraint (Parent_Type));
5530 end if;
5531 end if;
5533 end Build_Derived_Record_Type;
5535 ------------------------
5536 -- Build_Derived_Type --
5537 ------------------------
5539 procedure Build_Derived_Type
5540 (N : Node_Id;
5541 Parent_Type : Entity_Id;
5542 Derived_Type : Entity_Id;
5543 Is_Completion : Boolean;
5544 Derive_Subps : Boolean := True)
5546 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5548 begin
5549 -- Set common attributes
5551 Set_Scope (Derived_Type, Current_Scope);
5553 Set_Ekind (Derived_Type, Ekind (Parent_Base));
5554 Set_Etype (Derived_Type, Parent_Base);
5555 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
5557 Set_Size_Info (Derived_Type, Parent_Type);
5558 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5559 Set_Convention (Derived_Type, Convention (Parent_Type));
5560 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
5562 -- The derived type inherits the representation clauses of the parent.
5563 -- However, for a private type that is completed by a derivation, there
5564 -- may be operation attributes that have been specified already (stream
5565 -- attributes and External_Tag) and those must be provided. Finally,
5566 -- if the partial view is a private extension, the representation items
5567 -- of the parent have been inherited already, and should not be chained
5568 -- twice to the derived type.
5570 if Is_Tagged_Type (Parent_Type)
5571 and then Present (First_Rep_Item (Derived_Type))
5572 then
5573 -- The existing items are either operational items or items inherited
5574 -- from a private extension declaration.
5576 declare
5577 Rep : Node_Id := First_Rep_Item (Derived_Type);
5578 Found : Boolean := False;
5580 begin
5581 while Present (Rep) loop
5582 if Rep = First_Rep_Item (Parent_Type) then
5583 Found := True;
5584 exit;
5585 else
5586 Rep := Next_Rep_Item (Rep);
5587 end if;
5588 end loop;
5590 if not Found then
5591 Set_Next_Rep_Item
5592 (First_Rep_Item (Derived_Type), First_Rep_Item (Parent_Type));
5593 end if;
5594 end;
5596 else
5597 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
5598 end if;
5600 case Ekind (Parent_Type) is
5601 when Numeric_Kind =>
5602 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
5604 when Array_Kind =>
5605 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
5607 when E_Record_Type
5608 | E_Record_Subtype
5609 | Class_Wide_Kind =>
5610 Build_Derived_Record_Type
5611 (N, Parent_Type, Derived_Type, Derive_Subps);
5612 return;
5614 when Enumeration_Kind =>
5615 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
5617 when Access_Kind =>
5618 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
5620 when Incomplete_Or_Private_Kind =>
5621 Build_Derived_Private_Type
5622 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
5624 -- For discriminated types, the derivation includes deriving
5625 -- primitive operations. For others it is done below.
5627 if Is_Tagged_Type (Parent_Type)
5628 or else Has_Discriminants (Parent_Type)
5629 or else (Present (Full_View (Parent_Type))
5630 and then Has_Discriminants (Full_View (Parent_Type)))
5631 then
5632 return;
5633 end if;
5635 when Concurrent_Kind =>
5636 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
5638 when others =>
5639 raise Program_Error;
5640 end case;
5642 if Etype (Derived_Type) = Any_Type then
5643 return;
5644 end if;
5646 -- Set delayed freeze and then derive subprograms, we need to do
5647 -- this in this order so that derived subprograms inherit the
5648 -- derived freeze if necessary.
5650 Set_Has_Delayed_Freeze (Derived_Type);
5651 if Derive_Subps then
5652 Derive_Subprograms (Parent_Type, Derived_Type);
5653 end if;
5655 Set_Has_Primitive_Operations
5656 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
5657 end Build_Derived_Type;
5659 -----------------------
5660 -- Build_Discriminal --
5661 -----------------------
5663 procedure Build_Discriminal (Discrim : Entity_Id) is
5664 D_Minal : Entity_Id;
5665 CR_Disc : Entity_Id;
5667 begin
5668 -- A discriminal has the same names as the discriminant.
5670 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
5672 Set_Ekind (D_Minal, E_In_Parameter);
5673 Set_Mechanism (D_Minal, Default_Mechanism);
5674 Set_Etype (D_Minal, Etype (Discrim));
5676 Set_Discriminal (Discrim, D_Minal);
5677 Set_Discriminal_Link (D_Minal, Discrim);
5679 -- For task types, build at once the discriminants of the corresponding
5680 -- record, which are needed if discriminants are used in entry defaults
5681 -- and in family bounds.
5683 if Is_Concurrent_Type (Current_Scope)
5684 or else Is_Limited_Type (Current_Scope)
5685 then
5686 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
5688 Set_Ekind (CR_Disc, E_In_Parameter);
5689 Set_Mechanism (CR_Disc, Default_Mechanism);
5690 Set_Etype (CR_Disc, Etype (Discrim));
5691 Set_CR_Discriminant (Discrim, CR_Disc);
5692 end if;
5693 end Build_Discriminal;
5695 ------------------------------------
5696 -- Build_Discriminant_Constraints --
5697 ------------------------------------
5699 function Build_Discriminant_Constraints
5700 (T : Entity_Id;
5701 Def : Node_Id;
5702 Derived_Def : Boolean := False) return Elist_Id
5704 C : constant Node_Id := Constraint (Def);
5705 Nb_Discr : constant Nat := Number_Discriminants (T);
5706 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
5707 -- Saves the expression corresponding to a given discriminant in T.
5709 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
5710 -- Return the Position number within array Discr_Expr of a discriminant
5711 -- D within the discriminant list of the discriminated type T.
5713 ------------------
5714 -- Pos_Of_Discr --
5715 ------------------
5717 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
5718 Disc : Entity_Id;
5720 begin
5721 Disc := First_Discriminant (T);
5722 for J in Discr_Expr'Range loop
5723 if Disc = D then
5724 return J;
5725 end if;
5727 Next_Discriminant (Disc);
5728 end loop;
5730 -- Note: Since this function is called on discriminants that are
5731 -- known to belong to the discriminated type, falling through the
5732 -- loop with no match signals an internal compiler error.
5734 raise Program_Error;
5735 end Pos_Of_Discr;
5737 -- Declarations local to Build_Discriminant_Constraints
5739 Discr : Entity_Id;
5740 E : Entity_Id;
5741 Elist : constant Elist_Id := New_Elmt_List;
5743 Constr : Node_Id;
5744 Expr : Node_Id;
5745 Id : Node_Id;
5746 Position : Nat;
5747 Found : Boolean;
5749 Discrim_Present : Boolean := False;
5751 -- Start of processing for Build_Discriminant_Constraints
5753 begin
5754 -- The following loop will process positional associations only.
5755 -- For a positional association, the (single) discriminant is
5756 -- implicitly specified by position, in textual order (RM 3.7.2).
5758 Discr := First_Discriminant (T);
5759 Constr := First (Constraints (C));
5761 for D in Discr_Expr'Range loop
5762 exit when Nkind (Constr) = N_Discriminant_Association;
5764 if No (Constr) then
5765 Error_Msg_N ("too few discriminants given in constraint", C);
5766 return New_Elmt_List;
5768 elsif Nkind (Constr) = N_Range
5769 or else (Nkind (Constr) = N_Attribute_Reference
5770 and then
5771 Attribute_Name (Constr) = Name_Range)
5772 then
5773 Error_Msg_N
5774 ("a range is not a valid discriminant constraint", Constr);
5775 Discr_Expr (D) := Error;
5777 else
5778 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
5779 Discr_Expr (D) := Constr;
5780 end if;
5782 Next_Discriminant (Discr);
5783 Next (Constr);
5784 end loop;
5786 if No (Discr) and then Present (Constr) then
5787 Error_Msg_N ("too many discriminants given in constraint", Constr);
5788 return New_Elmt_List;
5789 end if;
5791 -- Named associations can be given in any order, but if both positional
5792 -- and named associations are used in the same discriminant constraint,
5793 -- then positional associations must occur first, at their normal
5794 -- position. Hence once a named association is used, the rest of the
5795 -- discriminant constraint must use only named associations.
5797 while Present (Constr) loop
5799 -- Positional association forbidden after a named association.
5801 if Nkind (Constr) /= N_Discriminant_Association then
5802 Error_Msg_N ("positional association follows named one", Constr);
5803 return New_Elmt_List;
5805 -- Otherwise it is a named association
5807 else
5808 -- E records the type of the discriminants in the named
5809 -- association. All the discriminants specified in the same name
5810 -- association must have the same type.
5812 E := Empty;
5814 -- Search the list of discriminants in T to see if the simple name
5815 -- given in the constraint matches any of them.
5817 Id := First (Selector_Names (Constr));
5818 while Present (Id) loop
5819 Found := False;
5821 -- If Original_Discriminant is present, we are processing a
5822 -- generic instantiation and this is an instance node. We need
5823 -- to find the name of the corresponding discriminant in the
5824 -- actual record type T and not the name of the discriminant in
5825 -- the generic formal. Example:
5827 -- generic
5828 -- type G (D : int) is private;
5829 -- package P is
5830 -- subtype W is G (D => 1);
5831 -- end package;
5832 -- type Rec (X : int) is record ... end record;
5833 -- package Q is new P (G => Rec);
5835 -- At the point of the instantiation, formal type G is Rec
5836 -- and therefore when reanalyzing "subtype W is G (D => 1);"
5837 -- which really looks like "subtype W is Rec (D => 1);" at
5838 -- the point of instantiation, we want to find the discriminant
5839 -- that corresponds to D in Rec, ie X.
5841 if Present (Original_Discriminant (Id)) then
5842 Discr := Find_Corresponding_Discriminant (Id, T);
5843 Found := True;
5845 else
5846 Discr := First_Discriminant (T);
5847 while Present (Discr) loop
5848 if Chars (Discr) = Chars (Id) then
5849 Found := True;
5850 exit;
5851 end if;
5853 Next_Discriminant (Discr);
5854 end loop;
5856 if not Found then
5857 Error_Msg_N ("& does not match any discriminant", Id);
5858 return New_Elmt_List;
5860 -- The following is only useful for the benefit of generic
5861 -- instances but it does not interfere with other
5862 -- processing for the non-generic case so we do it in all
5863 -- cases (for generics this statement is executed when
5864 -- processing the generic definition, see comment at the
5865 -- beginning of this if statement).
5867 else
5868 Set_Original_Discriminant (Id, Discr);
5869 end if;
5870 end if;
5872 Position := Pos_Of_Discr (T, Discr);
5874 if Present (Discr_Expr (Position)) then
5875 Error_Msg_N ("duplicate constraint for discriminant&", Id);
5877 else
5878 -- Each discriminant specified in the same named association
5879 -- must be associated with a separate copy of the
5880 -- corresponding expression.
5882 if Present (Next (Id)) then
5883 Expr := New_Copy_Tree (Expression (Constr));
5884 Set_Parent (Expr, Parent (Expression (Constr)));
5885 else
5886 Expr := Expression (Constr);
5887 end if;
5889 Discr_Expr (Position) := Expr;
5890 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
5891 end if;
5893 -- A discriminant association with more than one discriminant
5894 -- name is only allowed if the named discriminants are all of
5895 -- the same type (RM 3.7.1(8)).
5897 if E = Empty then
5898 E := Base_Type (Etype (Discr));
5900 elsif Base_Type (Etype (Discr)) /= E then
5901 Error_Msg_N
5902 ("all discriminants in an association " &
5903 "must have the same type", Id);
5904 end if;
5906 Next (Id);
5907 end loop;
5908 end if;
5910 Next (Constr);
5911 end loop;
5913 -- A discriminant constraint must provide exactly one value for each
5914 -- discriminant of the type (RM 3.7.1(8)).
5916 for J in Discr_Expr'Range loop
5917 if No (Discr_Expr (J)) then
5918 Error_Msg_N ("too few discriminants given in constraint", C);
5919 return New_Elmt_List;
5920 end if;
5921 end loop;
5923 -- Determine if there are discriminant expressions in the constraint.
5925 for J in Discr_Expr'Range loop
5926 if Denotes_Discriminant (Discr_Expr (J), Check_Protected => True) then
5927 Discrim_Present := True;
5928 end if;
5929 end loop;
5931 -- Build an element list consisting of the expressions given in the
5932 -- discriminant constraint and apply the appropriate checks. The list
5933 -- is constructed after resolving any named discriminant associations
5934 -- and therefore the expressions appear in the textual order of the
5935 -- discriminants.
5937 Discr := First_Discriminant (T);
5938 for J in Discr_Expr'Range loop
5939 if Discr_Expr (J) /= Error then
5941 Append_Elmt (Discr_Expr (J), Elist);
5943 -- If any of the discriminant constraints is given by a
5944 -- discriminant and we are in a derived type declaration we
5945 -- have a discriminant renaming. Establish link between new
5946 -- and old discriminant.
5948 if Denotes_Discriminant (Discr_Expr (J)) then
5949 if Derived_Def then
5950 Set_Corresponding_Discriminant
5951 (Entity (Discr_Expr (J)), Discr);
5952 end if;
5954 -- Force the evaluation of non-discriminant expressions.
5955 -- If we have found a discriminant in the constraint 3.4(26)
5956 -- and 3.8(18) demand that no range checks are performed are
5957 -- after evaluation. If the constraint is for a component
5958 -- definition that has a per-object constraint, expressions are
5959 -- evaluated but not checked either. In all other cases perform
5960 -- a range check.
5962 else
5963 if Discrim_Present then
5964 null;
5966 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
5967 and then
5968 Has_Per_Object_Constraint
5969 (Defining_Identifier (Parent (Parent (Def))))
5970 then
5971 null;
5973 elsif Is_Access_Type (Etype (Discr)) then
5974 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
5976 else
5977 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
5978 end if;
5980 Force_Evaluation (Discr_Expr (J));
5981 end if;
5983 -- Check that the designated type of an access discriminant's
5984 -- expression is not a class-wide type unless the discriminant's
5985 -- designated type is also class-wide.
5987 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
5988 and then not Is_Class_Wide_Type
5989 (Designated_Type (Etype (Discr)))
5990 and then Etype (Discr_Expr (J)) /= Any_Type
5991 and then Is_Class_Wide_Type
5992 (Designated_Type (Etype (Discr_Expr (J))))
5993 then
5994 Wrong_Type (Discr_Expr (J), Etype (Discr));
5995 end if;
5996 end if;
5998 Next_Discriminant (Discr);
5999 end loop;
6001 return Elist;
6002 end Build_Discriminant_Constraints;
6004 ---------------------------------
6005 -- Build_Discriminated_Subtype --
6006 ---------------------------------
6008 procedure Build_Discriminated_Subtype
6009 (T : Entity_Id;
6010 Def_Id : Entity_Id;
6011 Elist : Elist_Id;
6012 Related_Nod : Node_Id;
6013 For_Access : Boolean := False)
6015 Has_Discrs : constant Boolean := Has_Discriminants (T);
6016 Constrained : constant Boolean
6017 := (Has_Discrs
6018 and then not Is_Empty_Elmt_List (Elist)
6019 and then not Is_Class_Wide_Type (T))
6020 or else Is_Constrained (T);
6022 begin
6023 if Ekind (T) = E_Record_Type then
6024 if For_Access then
6025 Set_Ekind (Def_Id, E_Private_Subtype);
6026 Set_Is_For_Access_Subtype (Def_Id, True);
6027 else
6028 Set_Ekind (Def_Id, E_Record_Subtype);
6029 end if;
6031 elsif Ekind (T) = E_Task_Type then
6032 Set_Ekind (Def_Id, E_Task_Subtype);
6034 elsif Ekind (T) = E_Protected_Type then
6035 Set_Ekind (Def_Id, E_Protected_Subtype);
6037 elsif Is_Private_Type (T) then
6038 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
6040 elsif Is_Class_Wide_Type (T) then
6041 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
6043 else
6044 -- Incomplete type. Attach subtype to list of dependents, to be
6045 -- completed with full view of parent type.
6047 Set_Ekind (Def_Id, Ekind (T));
6048 Append_Elmt (Def_Id, Private_Dependents (T));
6049 end if;
6051 Set_Etype (Def_Id, T);
6052 Init_Size_Align (Def_Id);
6053 Set_Has_Discriminants (Def_Id, Has_Discrs);
6054 Set_Is_Constrained (Def_Id, Constrained);
6056 Set_First_Entity (Def_Id, First_Entity (T));
6057 Set_Last_Entity (Def_Id, Last_Entity (T));
6058 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
6060 if Is_Tagged_Type (T) then
6061 Set_Is_Tagged_Type (Def_Id);
6062 Make_Class_Wide_Type (Def_Id);
6063 end if;
6065 Set_Stored_Constraint (Def_Id, No_Elist);
6067 if Has_Discrs then
6068 Set_Discriminant_Constraint (Def_Id, Elist);
6069 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
6070 end if;
6072 if Is_Tagged_Type (T) then
6073 Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
6074 Set_Is_Abstract (Def_Id, Is_Abstract (T));
6075 end if;
6077 -- Subtypes introduced by component declarations do not need to be
6078 -- marked as delayed, and do not get freeze nodes, because the semantics
6079 -- verifies that the parents of the subtypes are frozen before the
6080 -- enclosing record is frozen.
6082 if not Is_Type (Scope (Def_Id)) then
6083 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
6085 if Is_Private_Type (T)
6086 and then Present (Full_View (T))
6087 then
6088 Conditional_Delay (Def_Id, Full_View (T));
6089 else
6090 Conditional_Delay (Def_Id, T);
6091 end if;
6092 end if;
6094 if Is_Record_Type (T) then
6095 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
6097 if Has_Discrs
6098 and then not Is_Empty_Elmt_List (Elist)
6099 and then not For_Access
6100 then
6101 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
6102 elsif not For_Access then
6103 Set_Cloned_Subtype (Def_Id, T);
6104 end if;
6105 end if;
6107 end Build_Discriminated_Subtype;
6109 ------------------------
6110 -- Build_Scalar_Bound --
6111 ------------------------
6113 function Build_Scalar_Bound
6114 (Bound : Node_Id;
6115 Par_T : Entity_Id;
6116 Der_T : Entity_Id) return Node_Id
6118 New_Bound : Entity_Id;
6120 begin
6121 -- Note: not clear why this is needed, how can the original bound
6122 -- be unanalyzed at this point? and if it is, what business do we
6123 -- have messing around with it? and why is the base type of the
6124 -- parent type the right type for the resolution. It probably is
6125 -- not! It is OK for the new bound we are creating, but not for
6126 -- the old one??? Still if it never happens, no problem!
6128 Analyze_And_Resolve (Bound, Base_Type (Par_T));
6130 if Nkind (Bound) = N_Integer_Literal
6131 or else Nkind (Bound) = N_Real_Literal
6132 then
6133 New_Bound := New_Copy (Bound);
6134 Set_Etype (New_Bound, Der_T);
6135 Set_Analyzed (New_Bound);
6137 elsif Is_Entity_Name (Bound) then
6138 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
6140 -- The following is almost certainly wrong. What business do we have
6141 -- relocating a node (Bound) that is presumably still attached to
6142 -- the tree elsewhere???
6144 else
6145 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
6146 end if;
6148 Set_Etype (New_Bound, Der_T);
6149 return New_Bound;
6150 end Build_Scalar_Bound;
6152 --------------------------------
6153 -- Build_Underlying_Full_View --
6154 --------------------------------
6156 procedure Build_Underlying_Full_View
6157 (N : Node_Id;
6158 Typ : Entity_Id;
6159 Par : Entity_Id)
6161 Loc : constant Source_Ptr := Sloc (N);
6162 Subt : constant Entity_Id :=
6163 Make_Defining_Identifier
6164 (Loc, New_External_Name (Chars (Typ), 'S'));
6166 Constr : Node_Id;
6167 Indic : Node_Id;
6168 C : Node_Id;
6169 Id : Node_Id;
6171 begin
6172 if Nkind (N) = N_Full_Type_Declaration then
6173 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
6175 -- ??? ??? is this assert right, I assume so otherwise Constr
6176 -- would not be defined below (this used to be an elsif)
6178 else pragma Assert (Nkind (N) = N_Subtype_Declaration);
6179 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
6180 end if;
6182 -- If the constraint has discriminant associations, the discriminant
6183 -- entity is already set, but it denotes a discriminant of the new
6184 -- type, not the original parent, so it must be found anew.
6186 C := First (Constraints (Constr));
6188 while Present (C) loop
6190 if Nkind (C) = N_Discriminant_Association then
6191 Id := First (Selector_Names (C));
6193 while Present (Id) loop
6194 Set_Original_Discriminant (Id, Empty);
6195 Next (Id);
6196 end loop;
6197 end if;
6199 Next (C);
6200 end loop;
6202 Indic := Make_Subtype_Declaration (Loc,
6203 Defining_Identifier => Subt,
6204 Subtype_Indication =>
6205 Make_Subtype_Indication (Loc,
6206 Subtype_Mark => New_Reference_To (Par, Loc),
6207 Constraint => New_Copy_Tree (Constr)));
6209 Insert_Before (N, Indic);
6210 Analyze (Indic);
6211 Set_Underlying_Full_View (Typ, Full_View (Subt));
6212 end Build_Underlying_Full_View;
6214 -------------------------------
6215 -- Check_Abstract_Overriding --
6216 -------------------------------
6218 procedure Check_Abstract_Overriding (T : Entity_Id) is
6219 Op_List : Elist_Id;
6220 Elmt : Elmt_Id;
6221 Subp : Entity_Id;
6222 Type_Def : Node_Id;
6224 begin
6225 Op_List := Primitive_Operations (T);
6227 -- Loop to check primitive operations
6229 Elmt := First_Elmt (Op_List);
6230 while Present (Elmt) loop
6231 Subp := Node (Elmt);
6233 -- Special exception, do not complain about failure to
6234 -- override _Input and _Output, since we always provide
6235 -- automatic overridings for these subprograms.
6237 if Is_Abstract (Subp)
6238 and then not Is_TSS (Subp, TSS_Stream_Input)
6239 and then not Is_TSS (Subp, TSS_Stream_Output)
6240 and then not Is_Abstract (T)
6241 then
6242 if Present (Alias (Subp)) then
6243 -- Only perform the check for a derived subprogram when
6244 -- the type has an explicit record extension. This avoids
6245 -- incorrectly flagging abstract subprograms for the case
6246 -- of a type without an extension derived from a formal type
6247 -- with a tagged actual (can occur within a private part).
6249 Type_Def := Type_Definition (Parent (T));
6250 if Nkind (Type_Def) = N_Derived_Type_Definition
6251 and then Present (Record_Extension_Part (Type_Def))
6252 then
6253 Error_Msg_NE
6254 ("type must be declared abstract or & overridden",
6255 T, Subp);
6256 end if;
6257 else
6258 Error_Msg_NE
6259 ("abstract subprogram not allowed for type&",
6260 Subp, T);
6261 Error_Msg_NE
6262 ("nonabstract type has abstract subprogram&",
6263 T, Subp);
6264 end if;
6265 end if;
6267 Next_Elmt (Elmt);
6268 end loop;
6269 end Check_Abstract_Overriding;
6271 ------------------------------------------------
6272 -- Check_Access_Discriminant_Requires_Limited --
6273 ------------------------------------------------
6275 procedure Check_Access_Discriminant_Requires_Limited
6276 (D : Node_Id;
6277 Loc : Node_Id)
6279 begin
6280 -- A discriminant_specification for an access discriminant
6281 -- shall appear only in the declaration for a task or protected
6282 -- type, or for a type with the reserved word 'limited' in
6283 -- its definition or in one of its ancestors. (RM 3.7(10))
6285 if Nkind (Discriminant_Type (D)) = N_Access_Definition
6286 and then not Is_Concurrent_Type (Current_Scope)
6287 and then not Is_Concurrent_Record_Type (Current_Scope)
6288 and then not Is_Limited_Record (Current_Scope)
6289 and then Ekind (Current_Scope) /= E_Limited_Private_Type
6290 then
6291 Error_Msg_N
6292 ("access discriminants allowed only for limited types", Loc);
6293 end if;
6294 end Check_Access_Discriminant_Requires_Limited;
6296 -----------------------------------
6297 -- Check_Aliased_Component_Types --
6298 -----------------------------------
6300 procedure Check_Aliased_Component_Types (T : Entity_Id) is
6301 C : Entity_Id;
6303 begin
6304 -- ??? Also need to check components of record extensions,
6305 -- but not components of protected types (which are always
6306 -- limited).
6308 if not Is_Limited_Type (T) then
6309 if Ekind (T) = E_Record_Type then
6310 C := First_Component (T);
6311 while Present (C) loop
6312 if Is_Aliased (C)
6313 and then Has_Discriminants (Etype (C))
6314 and then not Is_Constrained (Etype (C))
6315 and then not In_Instance
6316 then
6317 Error_Msg_N
6318 ("aliased component must be constrained ('R'M 3.6(11))",
6320 end if;
6322 Next_Component (C);
6323 end loop;
6325 elsif Ekind (T) = E_Array_Type then
6326 if Has_Aliased_Components (T)
6327 and then Has_Discriminants (Component_Type (T))
6328 and then not Is_Constrained (Component_Type (T))
6329 and then not In_Instance
6330 then
6331 Error_Msg_N
6332 ("aliased component type must be constrained ('R'M 3.6(11))",
6334 end if;
6335 end if;
6336 end if;
6337 end Check_Aliased_Component_Types;
6339 ----------------------
6340 -- Check_Completion --
6341 ----------------------
6343 procedure Check_Completion (Body_Id : Node_Id := Empty) is
6344 E : Entity_Id;
6346 procedure Post_Error;
6347 -- Post error message for lack of completion for entity E
6349 ----------------
6350 -- Post_Error --
6351 ----------------
6353 procedure Post_Error is
6354 begin
6355 if not Comes_From_Source (E) then
6357 if Ekind (E) = E_Task_Type
6358 or else Ekind (E) = E_Protected_Type
6359 then
6360 -- It may be an anonymous protected type created for a
6361 -- single variable. Post error on variable, if present.
6363 declare
6364 Var : Entity_Id;
6366 begin
6367 Var := First_Entity (Current_Scope);
6369 while Present (Var) loop
6370 exit when Etype (Var) = E
6371 and then Comes_From_Source (Var);
6373 Next_Entity (Var);
6374 end loop;
6376 if Present (Var) then
6377 E := Var;
6378 end if;
6379 end;
6380 end if;
6381 end if;
6383 -- If a generated entity has no completion, then either previous
6384 -- semantic errors have disabled the expansion phase, or else
6385 -- we had missing subunits, or else we are compiling without expan-
6386 -- sion, or else something is very wrong.
6388 if not Comes_From_Source (E) then
6389 pragma Assert
6390 (Serious_Errors_Detected > 0
6391 or else Configurable_Run_Time_Violations > 0
6392 or else Subunits_Missing
6393 or else not Expander_Active);
6394 return;
6396 -- Here for source entity
6398 else
6399 -- Here if no body to post the error message, so we post the error
6400 -- on the declaration that has no completion. This is not really
6401 -- the right place to post it, think about this later ???
6403 if No (Body_Id) then
6404 if Is_Type (E) then
6405 Error_Msg_NE
6406 ("missing full declaration for }", Parent (E), E);
6407 else
6408 Error_Msg_NE
6409 ("missing body for &", Parent (E), E);
6410 end if;
6412 -- Package body has no completion for a declaration that appears
6413 -- in the corresponding spec. Post error on the body, with a
6414 -- reference to the non-completed declaration.
6416 else
6417 Error_Msg_Sloc := Sloc (E);
6419 if Is_Type (E) then
6420 Error_Msg_NE
6421 ("missing full declaration for }!", Body_Id, E);
6423 elsif Is_Overloadable (E)
6424 and then Current_Entity_In_Scope (E) /= E
6425 then
6426 -- It may be that the completion is mistyped and appears
6427 -- as a distinct overloading of the entity.
6429 declare
6430 Candidate : constant Entity_Id :=
6431 Current_Entity_In_Scope (E);
6432 Decl : constant Node_Id :=
6433 Unit_Declaration_Node (Candidate);
6435 begin
6436 if Is_Overloadable (Candidate)
6437 and then Ekind (Candidate) = Ekind (E)
6438 and then Nkind (Decl) = N_Subprogram_Body
6439 and then Acts_As_Spec (Decl)
6440 then
6441 Check_Type_Conformant (Candidate, E);
6443 else
6444 Error_Msg_NE ("missing body for & declared#!",
6445 Body_Id, E);
6446 end if;
6447 end;
6448 else
6449 Error_Msg_NE ("missing body for & declared#!",
6450 Body_Id, E);
6451 end if;
6452 end if;
6453 end if;
6454 end Post_Error;
6456 -- Start processing for Check_Completion
6458 begin
6459 E := First_Entity (Current_Scope);
6460 while Present (E) loop
6461 if Is_Intrinsic_Subprogram (E) then
6462 null;
6464 -- The following situation requires special handling: a child
6465 -- unit that appears in the context clause of the body of its
6466 -- parent:
6468 -- procedure Parent.Child (...);
6470 -- with Parent.Child;
6471 -- package body Parent is
6473 -- Here Parent.Child appears as a local entity, but should not
6474 -- be flagged as requiring completion, because it is a
6475 -- compilation unit.
6477 elsif Ekind (E) = E_Function
6478 or else Ekind (E) = E_Procedure
6479 or else Ekind (E) = E_Generic_Function
6480 or else Ekind (E) = E_Generic_Procedure
6481 then
6482 if not Has_Completion (E)
6483 and then not Is_Abstract (E)
6484 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
6485 N_Compilation_Unit
6486 and then Chars (E) /= Name_uSize
6487 then
6488 Post_Error;
6489 end if;
6491 elsif Is_Entry (E) then
6492 if not Has_Completion (E) and then
6493 (Ekind (Scope (E)) = E_Protected_Object
6494 or else Ekind (Scope (E)) = E_Protected_Type)
6495 then
6496 Post_Error;
6497 end if;
6499 elsif Is_Package (E) then
6500 if Unit_Requires_Body (E) then
6501 if not Has_Completion (E)
6502 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
6503 N_Compilation_Unit
6504 then
6505 Post_Error;
6506 end if;
6508 elsif not Is_Child_Unit (E) then
6509 May_Need_Implicit_Body (E);
6510 end if;
6512 elsif Ekind (E) = E_Incomplete_Type
6513 and then No (Underlying_Type (E))
6514 then
6515 Post_Error;
6517 elsif (Ekind (E) = E_Task_Type or else
6518 Ekind (E) = E_Protected_Type)
6519 and then not Has_Completion (E)
6520 then
6521 Post_Error;
6523 -- A single task declared in the current scope is
6524 -- a constant, verify that the body of its anonymous
6525 -- type is in the same scope. If the task is defined
6526 -- elsewhere, this may be a renaming declaration for
6527 -- which no completion is needed.
6529 elsif Ekind (E) = E_Constant
6530 and then Ekind (Etype (E)) = E_Task_Type
6531 and then not Has_Completion (Etype (E))
6532 and then Scope (Etype (E)) = Current_Scope
6533 then
6534 Post_Error;
6536 elsif Ekind (E) = E_Protected_Object
6537 and then not Has_Completion (Etype (E))
6538 then
6539 Post_Error;
6541 elsif Ekind (E) = E_Record_Type then
6542 if Is_Tagged_Type (E) then
6543 Check_Abstract_Overriding (E);
6544 end if;
6546 Check_Aliased_Component_Types (E);
6548 elsif Ekind (E) = E_Array_Type then
6549 Check_Aliased_Component_Types (E);
6551 end if;
6553 Next_Entity (E);
6554 end loop;
6555 end Check_Completion;
6557 ----------------------------
6558 -- Check_Delta_Expression --
6559 ----------------------------
6561 procedure Check_Delta_Expression (E : Node_Id) is
6562 begin
6563 if not (Is_Real_Type (Etype (E))) then
6564 Wrong_Type (E, Any_Real);
6566 elsif not Is_OK_Static_Expression (E) then
6567 Flag_Non_Static_Expr
6568 ("non-static expression used for delta value!", E);
6570 elsif not UR_Is_Positive (Expr_Value_R (E)) then
6571 Error_Msg_N ("delta expression must be positive", E);
6573 else
6574 return;
6575 end if;
6577 -- If any of above errors occurred, then replace the incorrect
6578 -- expression by the real 0.1, which should prevent further errors.
6580 Rewrite (E,
6581 Make_Real_Literal (Sloc (E), Ureal_Tenth));
6582 Analyze_And_Resolve (E, Standard_Float);
6584 end Check_Delta_Expression;
6586 -----------------------------
6587 -- Check_Digits_Expression --
6588 -----------------------------
6590 procedure Check_Digits_Expression (E : Node_Id) is
6591 begin
6592 if not (Is_Integer_Type (Etype (E))) then
6593 Wrong_Type (E, Any_Integer);
6595 elsif not Is_OK_Static_Expression (E) then
6596 Flag_Non_Static_Expr
6597 ("non-static expression used for digits value!", E);
6599 elsif Expr_Value (E) <= 0 then
6600 Error_Msg_N ("digits value must be greater than zero", E);
6602 else
6603 return;
6604 end if;
6606 -- If any of above errors occurred, then replace the incorrect
6607 -- expression by the integer 1, which should prevent further errors.
6609 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
6610 Analyze_And_Resolve (E, Standard_Integer);
6612 end Check_Digits_Expression;
6614 --------------------------
6615 -- Check_Initialization --
6616 --------------------------
6618 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
6619 begin
6620 if (Is_Limited_Type (T)
6621 or else Is_Limited_Composite (T))
6622 and then not In_Instance
6623 and then not In_Inlined_Body
6624 then
6625 -- Ada 0Y (AI-287): Relax the strictness of the front-end in case of
6626 -- limited aggregates and extension aggregates.
6628 if Extensions_Allowed
6629 and then (Nkind (Exp) = N_Aggregate
6630 or else Nkind (Exp) = N_Extension_Aggregate)
6631 then
6632 null;
6633 else
6634 Error_Msg_N
6635 ("cannot initialize entities of limited type", Exp);
6636 Explain_Limited_Type (T, Exp);
6637 end if;
6638 end if;
6639 end Check_Initialization;
6641 ------------------------------------
6642 -- Check_Or_Process_Discriminants --
6643 ------------------------------------
6645 -- If an incomplete or private type declaration was already given for
6646 -- the type, the discriminants may have already been processed if they
6647 -- were present on the incomplete declaration. In this case a full
6648 -- conformance check is performed otherwise just process them.
6650 procedure Check_Or_Process_Discriminants
6651 (N : Node_Id;
6652 T : Entity_Id;
6653 Prev : Entity_Id := Empty)
6655 begin
6656 if Has_Discriminants (T) then
6658 -- Make the discriminants visible to component declarations.
6660 declare
6661 D : Entity_Id := First_Discriminant (T);
6662 Prev : Entity_Id;
6664 begin
6665 while Present (D) loop
6666 Prev := Current_Entity (D);
6667 Set_Current_Entity (D);
6668 Set_Is_Immediately_Visible (D);
6669 Set_Homonym (D, Prev);
6671 -- Ada 0Y (AI-230): Access discriminant allowed in non-limited
6672 -- record types
6674 if not Extensions_Allowed then
6676 -- This restriction gets applied to the full type here; it
6677 -- has already been applied earlier to the partial view
6679 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
6680 end if;
6682 Next_Discriminant (D);
6683 end loop;
6684 end;
6686 elsif Present (Discriminant_Specifications (N)) then
6687 Process_Discriminants (N, Prev);
6688 end if;
6689 end Check_Or_Process_Discriminants;
6691 ----------------------
6692 -- Check_Real_Bound --
6693 ----------------------
6695 procedure Check_Real_Bound (Bound : Node_Id) is
6696 begin
6697 if not Is_Real_Type (Etype (Bound)) then
6698 Error_Msg_N
6699 ("bound in real type definition must be of real type", Bound);
6701 elsif not Is_OK_Static_Expression (Bound) then
6702 Flag_Non_Static_Expr
6703 ("non-static expression used for real type bound!", Bound);
6705 else
6706 return;
6707 end if;
6709 Rewrite
6710 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
6711 Analyze (Bound);
6712 Resolve (Bound, Standard_Float);
6713 end Check_Real_Bound;
6715 ------------------------------
6716 -- Complete_Private_Subtype --
6717 ------------------------------
6719 procedure Complete_Private_Subtype
6720 (Priv : Entity_Id;
6721 Full : Entity_Id;
6722 Full_Base : Entity_Id;
6723 Related_Nod : Node_Id)
6725 Save_Next_Entity : Entity_Id;
6726 Save_Homonym : Entity_Id;
6728 begin
6729 -- Set semantic attributes for (implicit) private subtype completion.
6730 -- If the full type has no discriminants, then it is a copy of the full
6731 -- view of the base. Otherwise, it is a subtype of the base with a
6732 -- possible discriminant constraint. Save and restore the original
6733 -- Next_Entity field of full to ensure that the calls to Copy_Node
6734 -- do not corrupt the entity chain.
6736 -- Note that the type of the full view is the same entity as the
6737 -- type of the partial view. In this fashion, the subtype has
6738 -- access to the correct view of the parent.
6740 Save_Next_Entity := Next_Entity (Full);
6741 Save_Homonym := Homonym (Priv);
6743 case Ekind (Full_Base) is
6745 when E_Record_Type |
6746 E_Record_Subtype |
6747 Class_Wide_Kind |
6748 Private_Kind |
6749 Task_Kind |
6750 Protected_Kind =>
6751 Copy_Node (Priv, Full);
6753 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
6754 Set_First_Entity (Full, First_Entity (Full_Base));
6755 Set_Last_Entity (Full, Last_Entity (Full_Base));
6757 when others =>
6758 Copy_Node (Full_Base, Full);
6759 Set_Chars (Full, Chars (Priv));
6760 Conditional_Delay (Full, Priv);
6761 Set_Sloc (Full, Sloc (Priv));
6763 end case;
6765 Set_Next_Entity (Full, Save_Next_Entity);
6766 Set_Homonym (Full, Save_Homonym);
6767 Set_Associated_Node_For_Itype (Full, Related_Nod);
6769 -- Set common attributes for all subtypes.
6771 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
6773 -- The Etype of the full view is inconsistent. Gigi needs to see the
6774 -- structural full view, which is what the current scheme gives:
6775 -- the Etype of the full view is the etype of the full base. However,
6776 -- if the full base is a derived type, the full view then looks like
6777 -- a subtype of the parent, not a subtype of the full base. If instead
6778 -- we write:
6780 -- Set_Etype (Full, Full_Base);
6782 -- then we get inconsistencies in the front-end (confusion between
6783 -- views). Several outstanding bugs are related to this.
6785 Set_Is_First_Subtype (Full, False);
6786 Set_Scope (Full, Scope (Priv));
6787 Set_Size_Info (Full, Full_Base);
6788 Set_RM_Size (Full, RM_Size (Full_Base));
6789 Set_Is_Itype (Full);
6791 -- A subtype of a private-type-without-discriminants, whose full-view
6792 -- has discriminants with default expressions, is not constrained!
6794 if not Has_Discriminants (Priv) then
6795 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
6797 if Has_Discriminants (Full_Base) then
6798 Set_Discriminant_Constraint
6799 (Full, Discriminant_Constraint (Full_Base));
6800 end if;
6801 end if;
6803 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
6804 Set_Depends_On_Private (Full, Has_Private_Component (Full));
6806 -- Freeze the private subtype entity if its parent is delayed,
6807 -- and not already frozen. We skip this processing if the type
6808 -- is an anonymous subtype of a record component, or is the
6809 -- corresponding record of a protected type, since ???
6811 if not Is_Type (Scope (Full)) then
6812 Set_Has_Delayed_Freeze (Full,
6813 Has_Delayed_Freeze (Full_Base)
6814 and then (not Is_Frozen (Full_Base)));
6815 end if;
6817 Set_Freeze_Node (Full, Empty);
6818 Set_Is_Frozen (Full, False);
6819 Set_Full_View (Priv, Full);
6821 if Has_Discriminants (Full) then
6822 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
6823 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
6824 if Has_Unknown_Discriminants (Full) then
6825 Set_Discriminant_Constraint (Full, No_Elist);
6826 end if;
6827 end if;
6829 if Ekind (Full_Base) = E_Record_Type
6830 and then Has_Discriminants (Full_Base)
6831 and then Has_Discriminants (Priv) -- might not, if errors
6832 and then not Has_Unknown_Discriminants (Priv)
6833 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
6834 then
6835 Create_Constrained_Components
6836 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
6838 -- If the full base is itself derived from private, build a congruent
6839 -- subtype of its underlying type, for use by the back end. Do not
6840 -- do this for a constrained record component, where the back-end has
6841 -- the proper information and there is no place for the declaration.
6843 elsif Ekind (Full_Base) in Private_Kind
6844 and then Is_Derived_Type (Full_Base)
6845 and then Has_Discriminants (Full_Base)
6846 and then Nkind (Related_Nod) /= N_Component_Declaration
6847 and then (Ekind (Current_Scope) /= E_Record_Subtype)
6848 and then
6849 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
6850 then
6851 Build_Underlying_Full_View (Parent (Priv), Full, Etype (Full_Base));
6853 elsif Is_Record_Type (Full_Base) then
6855 -- Show Full is simply a renaming of Full_Base.
6857 Set_Cloned_Subtype (Full, Full_Base);
6858 end if;
6860 -- It is unsafe to share to bounds of a scalar type, because the
6861 -- Itype is elaborated on demand, and if a bound is non-static
6862 -- then different orders of elaboration in different units will
6863 -- lead to different external symbols.
6865 if Is_Scalar_Type (Full_Base) then
6866 Set_Scalar_Range (Full,
6867 Make_Range (Sloc (Related_Nod),
6868 Low_Bound =>
6869 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
6870 High_Bound =>
6871 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
6873 -- This completion inherits the bounds of the full parent, but if
6874 -- the parent is an unconstrained floating point type, so is the
6875 -- completion.
6877 if Is_Floating_Point_Type (Full_Base) then
6878 Set_Includes_Infinities
6879 (Scalar_Range (Full), Has_Infinities (Full_Base));
6880 end if;
6881 end if;
6883 -- ??? It seems that a lot of fields are missing that should be
6884 -- copied from Full_Base to Full. Here are some that are introduced
6885 -- in a non-disruptive way but a cleanup is necessary.
6887 if Is_Tagged_Type (Full_Base) then
6888 Set_Is_Tagged_Type (Full);
6889 Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
6890 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
6892 elsif Is_Concurrent_Type (Full_Base) then
6893 if Has_Discriminants (Full)
6894 and then Present (Corresponding_Record_Type (Full_Base))
6895 then
6896 Set_Corresponding_Record_Type (Full,
6897 Constrain_Corresponding_Record
6898 (Full, Corresponding_Record_Type (Full_Base),
6899 Related_Nod, Full_Base));
6901 else
6902 Set_Corresponding_Record_Type (Full,
6903 Corresponding_Record_Type (Full_Base));
6904 end if;
6905 end if;
6907 end Complete_Private_Subtype;
6909 ----------------------------
6910 -- Constant_Redeclaration --
6911 ----------------------------
6913 procedure Constant_Redeclaration
6914 (Id : Entity_Id;
6915 N : Node_Id;
6916 T : out Entity_Id)
6918 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
6919 Obj_Def : constant Node_Id := Object_Definition (N);
6920 New_T : Entity_Id;
6922 procedure Check_Recursive_Declaration (Typ : Entity_Id);
6923 -- If deferred constant is an access type initialized with an
6924 -- allocator, check whether there is an illegal recursion in the
6925 -- definition, through a default value of some record subcomponent.
6926 -- This is normally detected when generating init procs, but requires
6927 -- this additional mechanism when expansion is disabled.
6929 ---------------------------------
6930 -- Check_Recursive_Declaration --
6931 ---------------------------------
6933 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
6934 Comp : Entity_Id;
6936 begin
6937 if Is_Record_Type (Typ) then
6938 Comp := First_Component (Typ);
6940 while Present (Comp) loop
6941 if Comes_From_Source (Comp) then
6942 if Present (Expression (Parent (Comp)))
6943 and then Is_Entity_Name (Expression (Parent (Comp)))
6944 and then Entity (Expression (Parent (Comp))) = Prev
6945 then
6946 Error_Msg_Sloc := Sloc (Parent (Comp));
6947 Error_Msg_NE
6948 ("illegal circularity with declaration for&#",
6949 N, Comp);
6950 return;
6952 elsif Is_Record_Type (Etype (Comp)) then
6953 Check_Recursive_Declaration (Etype (Comp));
6954 end if;
6955 end if;
6957 Next_Component (Comp);
6958 end loop;
6959 end if;
6960 end Check_Recursive_Declaration;
6962 -- Start of processing for Constant_Redeclaration
6964 begin
6965 if Nkind (Parent (Prev)) = N_Object_Declaration then
6966 if Nkind (Object_Definition
6967 (Parent (Prev))) = N_Subtype_Indication
6968 then
6969 -- Find type of new declaration. The constraints of the two
6970 -- views must match statically, but there is no point in
6971 -- creating an itype for the full view.
6973 if Nkind (Obj_Def) = N_Subtype_Indication then
6974 Find_Type (Subtype_Mark (Obj_Def));
6975 New_T := Entity (Subtype_Mark (Obj_Def));
6977 else
6978 Find_Type (Obj_Def);
6979 New_T := Entity (Obj_Def);
6980 end if;
6982 T := Etype (Prev);
6984 else
6985 -- The full view may impose a constraint, even if the partial
6986 -- view does not, so construct the subtype.
6988 New_T := Find_Type_Of_Object (Obj_Def, N);
6989 T := New_T;
6990 end if;
6992 else
6993 -- Current declaration is illegal, diagnosed below in Enter_Name.
6995 T := Empty;
6996 New_T := Any_Type;
6997 end if;
6999 -- If previous full declaration exists, or if a homograph is present,
7000 -- let Enter_Name handle it, either with an error, or with the removal
7001 -- of an overridden implicit subprogram.
7003 if Ekind (Prev) /= E_Constant
7004 or else Present (Expression (Parent (Prev)))
7005 or else Present (Full_View (Prev))
7006 then
7007 Enter_Name (Id);
7009 -- Verify that types of both declarations match.
7011 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T) then
7012 Error_Msg_Sloc := Sloc (Prev);
7013 Error_Msg_N ("type does not match declaration#", N);
7014 Set_Full_View (Prev, Id);
7015 Set_Etype (Id, Any_Type);
7017 -- If so, process the full constant declaration
7019 else
7020 Set_Full_View (Prev, Id);
7021 Set_Is_Public (Id, Is_Public (Prev));
7022 Set_Is_Internal (Id);
7023 Append_Entity (Id, Current_Scope);
7025 -- Check ALIASED present if present before (RM 7.4(7))
7027 if Is_Aliased (Prev)
7028 and then not Aliased_Present (N)
7029 then
7030 Error_Msg_Sloc := Sloc (Prev);
7031 Error_Msg_N ("ALIASED required (see declaration#)", N);
7032 end if;
7034 -- Check that placement is in private part and that the incomplete
7035 -- declaration appeared in the visible part.
7037 if Ekind (Current_Scope) = E_Package
7038 and then not In_Private_Part (Current_Scope)
7039 then
7040 Error_Msg_Sloc := Sloc (Prev);
7041 Error_Msg_N ("full constant for declaration#"
7042 & " must be in private part", N);
7044 elsif Ekind (Current_Scope) = E_Package
7045 and then List_Containing (Parent (Prev))
7046 /= Visible_Declarations
7047 (Specification (Unit_Declaration_Node (Current_Scope)))
7048 then
7049 Error_Msg_N
7050 ("deferred constant must be declared in visible part",
7051 Parent (Prev));
7052 end if;
7054 if Is_Access_Type (T)
7055 and then Nkind (Expression (N)) = N_Allocator
7056 then
7057 Check_Recursive_Declaration (Designated_Type (T));
7058 end if;
7059 end if;
7060 end Constant_Redeclaration;
7062 ----------------------
7063 -- Constrain_Access --
7064 ----------------------
7066 procedure Constrain_Access
7067 (Def_Id : in out Entity_Id;
7068 S : Node_Id;
7069 Related_Nod : Node_Id)
7071 T : constant Entity_Id := Entity (Subtype_Mark (S));
7072 Desig_Type : constant Entity_Id := Designated_Type (T);
7073 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
7074 Constraint_OK : Boolean := True;
7076 begin
7077 if Is_Array_Type (Desig_Type) then
7078 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
7080 elsif (Is_Record_Type (Desig_Type)
7081 or else Is_Incomplete_Or_Private_Type (Desig_Type))
7082 and then not Is_Constrained (Desig_Type)
7083 then
7084 -- ??? The following code is a temporary kludge to ignore
7085 -- discriminant constraint on access type if
7086 -- it is constraining the current record. Avoid creating the
7087 -- implicit subtype of the record we are currently compiling
7088 -- since right now, we cannot handle these.
7089 -- For now, just return the access type itself.
7091 if Desig_Type = Current_Scope
7092 and then No (Def_Id)
7093 then
7094 Set_Ekind (Desig_Subtype, E_Record_Subtype);
7095 Def_Id := Entity (Subtype_Mark (S));
7097 -- This call added to ensure that the constraint is
7098 -- analyzed (needed for a B test). Note that we
7099 -- still return early from this procedure to avoid
7100 -- recursive processing. ???
7102 Constrain_Discriminated_Type
7103 (Desig_Subtype, S, Related_Nod, For_Access => True);
7105 return;
7106 end if;
7108 if Ekind (T) = E_General_Access_Type
7109 and then Has_Private_Declaration (Desig_Type)
7110 and then In_Open_Scopes (Scope (Desig_Type))
7111 then
7112 -- Enforce rule that the constraint is illegal if there is
7113 -- an unconstrained view of the designated type. This means
7114 -- that the partial view (either a private type declaration or
7115 -- a derivation from a private type) has no discriminants.
7116 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
7117 -- by ACATS B371001).
7119 declare
7120 Pack : constant Node_Id :=
7121 Unit_Declaration_Node (Scope (Desig_Type));
7122 Decls : List_Id;
7123 Decl : Node_Id;
7125 begin
7126 if Nkind (Pack) = N_Package_Declaration then
7127 Decls := Visible_Declarations (Specification (Pack));
7128 Decl := First (Decls);
7130 while Present (Decl) loop
7131 if (Nkind (Decl) = N_Private_Type_Declaration
7132 and then
7133 Chars (Defining_Identifier (Decl)) =
7134 Chars (Desig_Type))
7136 or else
7137 (Nkind (Decl) = N_Full_Type_Declaration
7138 and then
7139 Chars (Defining_Identifier (Decl)) =
7140 Chars (Desig_Type)
7141 and then Is_Derived_Type (Desig_Type)
7142 and then
7143 Has_Private_Declaration (Etype (Desig_Type)))
7144 then
7145 if No (Discriminant_Specifications (Decl)) then
7146 Error_Msg_N
7147 ("cannot constrain general access type " &
7148 "if designated type has unconstrained view", S);
7149 end if;
7151 exit;
7152 end if;
7154 Next (Decl);
7155 end loop;
7156 end if;
7157 end;
7158 end if;
7160 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
7161 For_Access => True);
7163 elsif (Is_Task_Type (Desig_Type)
7164 or else Is_Protected_Type (Desig_Type))
7165 and then not Is_Constrained (Desig_Type)
7166 then
7167 Constrain_Concurrent
7168 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
7170 else
7171 Error_Msg_N ("invalid constraint on access type", S);
7172 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
7173 Constraint_OK := False;
7174 end if;
7176 if No (Def_Id) then
7177 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
7178 else
7179 Set_Ekind (Def_Id, E_Access_Subtype);
7180 end if;
7182 if Constraint_OK then
7183 Set_Etype (Def_Id, Base_Type (T));
7185 if Is_Private_Type (Desig_Type) then
7186 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
7187 end if;
7188 else
7189 Set_Etype (Def_Id, Any_Type);
7190 end if;
7192 Set_Size_Info (Def_Id, T);
7193 Set_Is_Constrained (Def_Id, Constraint_OK);
7194 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
7195 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
7196 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
7198 -- Itypes created for constrained record components do not receive
7199 -- a freeze node, they are elaborated when first seen.
7201 if not Is_Record_Type (Current_Scope) then
7202 Conditional_Delay (Def_Id, T);
7203 end if;
7204 end Constrain_Access;
7206 ---------------------
7207 -- Constrain_Array --
7208 ---------------------
7210 procedure Constrain_Array
7211 (Def_Id : in out Entity_Id;
7212 SI : Node_Id;
7213 Related_Nod : Node_Id;
7214 Related_Id : Entity_Id;
7215 Suffix : Character)
7217 C : constant Node_Id := Constraint (SI);
7218 Number_Of_Constraints : Nat := 0;
7219 Index : Node_Id;
7220 S, T : Entity_Id;
7221 Constraint_OK : Boolean := True;
7223 begin
7224 T := Entity (Subtype_Mark (SI));
7226 if Ekind (T) in Access_Kind then
7227 T := Designated_Type (T);
7228 end if;
7230 -- If an index constraint follows a subtype mark in a subtype indication
7231 -- then the type or subtype denoted by the subtype mark must not already
7232 -- impose an index constraint. The subtype mark must denote either an
7233 -- unconstrained array type or an access type whose designated type
7234 -- is such an array type... (RM 3.6.1)
7236 if Is_Constrained (T) then
7237 Error_Msg_N
7238 ("array type is already constrained", Subtype_Mark (SI));
7239 Constraint_OK := False;
7241 else
7242 S := First (Constraints (C));
7244 while Present (S) loop
7245 Number_Of_Constraints := Number_Of_Constraints + 1;
7246 Next (S);
7247 end loop;
7249 -- In either case, the index constraint must provide a discrete
7250 -- range for each index of the array type and the type of each
7251 -- discrete range must be the same as that of the corresponding
7252 -- index. (RM 3.6.1)
7254 if Number_Of_Constraints /= Number_Dimensions (T) then
7255 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
7256 Constraint_OK := False;
7258 else
7259 S := First (Constraints (C));
7260 Index := First_Index (T);
7261 Analyze (Index);
7263 -- Apply constraints to each index type
7265 for J in 1 .. Number_Of_Constraints loop
7266 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
7267 Next (Index);
7268 Next (S);
7269 end loop;
7271 end if;
7272 end if;
7274 if No (Def_Id) then
7275 Def_Id :=
7276 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
7277 Set_Parent (Def_Id, Related_Nod);
7279 else
7280 Set_Ekind (Def_Id, E_Array_Subtype);
7281 end if;
7283 Set_Size_Info (Def_Id, (T));
7284 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
7285 Set_Etype (Def_Id, Base_Type (T));
7287 if Constraint_OK then
7288 Set_First_Index (Def_Id, First (Constraints (C)));
7289 end if;
7291 Set_Is_Constrained (Def_Id, True);
7292 Set_Is_Aliased (Def_Id, Is_Aliased (T));
7293 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
7295 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
7296 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
7298 -- If the subtype is not that of a record component, build a freeze
7299 -- node if parent still needs one.
7301 -- If the subtype is not that of a record component, make sure
7302 -- that the Depends_On_Private status is set (explanation ???)
7303 -- and also that a conditional delay is set.
7305 if not Is_Type (Scope (Def_Id)) then
7306 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7307 Conditional_Delay (Def_Id, T);
7308 end if;
7310 end Constrain_Array;
7312 ------------------------------
7313 -- Constrain_Component_Type --
7314 ------------------------------
7316 function Constrain_Component_Type
7317 (Compon_Type : Entity_Id;
7318 Constrained_Typ : Entity_Id;
7319 Related_Node : Node_Id;
7320 Typ : Entity_Id;
7321 Constraints : Elist_Id) return Entity_Id
7323 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
7325 function Build_Constrained_Array_Type
7326 (Old_Type : Entity_Id) return Entity_Id;
7327 -- If Old_Type is an array type, one of whose indices is
7328 -- constrained by a discriminant, build an Itype whose constraint
7329 -- replaces the discriminant with its value in the constraint.
7331 function Build_Constrained_Discriminated_Type
7332 (Old_Type : Entity_Id) return Entity_Id;
7333 -- Ditto for record components.
7335 function Build_Constrained_Access_Type
7336 (Old_Type : Entity_Id) return Entity_Id;
7337 -- Ditto for access types. Makes use of previous two functions, to
7338 -- constrain designated type.
7340 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
7341 -- T is an array or discriminated type, C is a list of constraints
7342 -- that apply to T. This routine builds the constrained subtype.
7344 function Is_Discriminant (Expr : Node_Id) return Boolean;
7345 -- Returns True if Expr is a discriminant.
7347 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
7348 -- Find the value of discriminant Discrim in Constraint.
7350 -----------------------------------
7351 -- Build_Constrained_Access_Type --
7352 -----------------------------------
7354 function Build_Constrained_Access_Type
7355 (Old_Type : Entity_Id) return Entity_Id
7357 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
7358 Itype : Entity_Id;
7359 Desig_Subtype : Entity_Id;
7360 Scop : Entity_Id;
7362 begin
7363 -- if the original access type was not embedded in the enclosing
7364 -- type definition, there is no need to produce a new access
7365 -- subtype. In fact every access type with an explicit constraint
7366 -- generates an itype whose scope is the enclosing record.
7368 if not Is_Type (Scope (Old_Type)) then
7369 return Old_Type;
7371 elsif Is_Array_Type (Desig_Type) then
7372 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
7374 elsif Has_Discriminants (Desig_Type) then
7376 -- This may be an access type to an enclosing record type for
7377 -- which we are constructing the constrained components. Return
7378 -- the enclosing record subtype. This is not always correct,
7379 -- but avoids infinite recursion. ???
7381 Desig_Subtype := Any_Type;
7383 for J in reverse 0 .. Scope_Stack.Last loop
7384 Scop := Scope_Stack.Table (J).Entity;
7386 if Is_Type (Scop)
7387 and then Base_Type (Scop) = Base_Type (Desig_Type)
7388 then
7389 Desig_Subtype := Scop;
7390 end if;
7392 exit when not Is_Type (Scop);
7393 end loop;
7395 if Desig_Subtype = Any_Type then
7396 Desig_Subtype :=
7397 Build_Constrained_Discriminated_Type (Desig_Type);
7398 end if;
7400 else
7401 return Old_Type;
7402 end if;
7404 if Desig_Subtype /= Desig_Type then
7405 -- The Related_Node better be here or else we won't be able
7406 -- to attach new itypes to a node in the tree.
7408 pragma Assert (Present (Related_Node));
7410 Itype := Create_Itype (E_Access_Subtype, Related_Node);
7412 Set_Etype (Itype, Base_Type (Old_Type));
7413 Set_Size_Info (Itype, (Old_Type));
7414 Set_Directly_Designated_Type (Itype, Desig_Subtype);
7415 Set_Depends_On_Private (Itype, Has_Private_Component
7416 (Old_Type));
7417 Set_Is_Access_Constant (Itype, Is_Access_Constant
7418 (Old_Type));
7420 -- The new itype needs freezing when it depends on a not frozen
7421 -- type and the enclosing subtype needs freezing.
7423 if Has_Delayed_Freeze (Constrained_Typ)
7424 and then not Is_Frozen (Constrained_Typ)
7425 then
7426 Conditional_Delay (Itype, Base_Type (Old_Type));
7427 end if;
7429 return Itype;
7431 else
7432 return Old_Type;
7433 end if;
7434 end Build_Constrained_Access_Type;
7436 ----------------------------------
7437 -- Build_Constrained_Array_Type --
7438 ----------------------------------
7440 function Build_Constrained_Array_Type
7441 (Old_Type : Entity_Id) return Entity_Id
7443 Lo_Expr : Node_Id;
7444 Hi_Expr : Node_Id;
7445 Old_Index : Node_Id;
7446 Range_Node : Node_Id;
7447 Constr_List : List_Id;
7449 Need_To_Create_Itype : Boolean := False;
7451 begin
7452 Old_Index := First_Index (Old_Type);
7453 while Present (Old_Index) loop
7454 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
7456 if Is_Discriminant (Lo_Expr)
7457 or else Is_Discriminant (Hi_Expr)
7458 then
7459 Need_To_Create_Itype := True;
7460 end if;
7462 Next_Index (Old_Index);
7463 end loop;
7465 if Need_To_Create_Itype then
7466 Constr_List := New_List;
7468 Old_Index := First_Index (Old_Type);
7469 while Present (Old_Index) loop
7470 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
7472 if Is_Discriminant (Lo_Expr) then
7473 Lo_Expr := Get_Discr_Value (Lo_Expr);
7474 end if;
7476 if Is_Discriminant (Hi_Expr) then
7477 Hi_Expr := Get_Discr_Value (Hi_Expr);
7478 end if;
7480 Range_Node :=
7481 Make_Range
7482 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
7484 Append (Range_Node, To => Constr_List);
7486 Next_Index (Old_Index);
7487 end loop;
7489 return Build_Subtype (Old_Type, Constr_List);
7491 else
7492 return Old_Type;
7493 end if;
7494 end Build_Constrained_Array_Type;
7496 ------------------------------------------
7497 -- Build_Constrained_Discriminated_Type --
7498 ------------------------------------------
7500 function Build_Constrained_Discriminated_Type
7501 (Old_Type : Entity_Id) return Entity_Id
7503 Expr : Node_Id;
7504 Constr_List : List_Id;
7505 Old_Constraint : Elmt_Id;
7507 Need_To_Create_Itype : Boolean := False;
7509 begin
7510 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
7511 while Present (Old_Constraint) loop
7512 Expr := Node (Old_Constraint);
7514 if Is_Discriminant (Expr) then
7515 Need_To_Create_Itype := True;
7516 end if;
7518 Next_Elmt (Old_Constraint);
7519 end loop;
7521 if Need_To_Create_Itype then
7522 Constr_List := New_List;
7524 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
7525 while Present (Old_Constraint) loop
7526 Expr := Node (Old_Constraint);
7528 if Is_Discriminant (Expr) then
7529 Expr := Get_Discr_Value (Expr);
7530 end if;
7532 Append (New_Copy_Tree (Expr), To => Constr_List);
7534 Next_Elmt (Old_Constraint);
7535 end loop;
7537 return Build_Subtype (Old_Type, Constr_List);
7539 else
7540 return Old_Type;
7541 end if;
7542 end Build_Constrained_Discriminated_Type;
7544 -------------------
7545 -- Build_Subtype --
7546 -------------------
7548 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
7549 Indic : Node_Id;
7550 Subtyp_Decl : Node_Id;
7551 Def_Id : Entity_Id;
7552 Btyp : Entity_Id := Base_Type (T);
7554 begin
7555 -- The Related_Node better be here or else we won't be able
7556 -- to attach new itypes to a node in the tree.
7558 pragma Assert (Present (Related_Node));
7560 -- If the view of the component's type is incomplete or private
7561 -- with unknown discriminants, then the constraint must be applied
7562 -- to the full type.
7564 if Has_Unknown_Discriminants (Btyp)
7565 and then Present (Underlying_Type (Btyp))
7566 then
7567 Btyp := Underlying_Type (Btyp);
7568 end if;
7570 Indic :=
7571 Make_Subtype_Indication (Loc,
7572 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7573 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
7575 Def_Id := Create_Itype (Ekind (T), Related_Node);
7577 Subtyp_Decl :=
7578 Make_Subtype_Declaration (Loc,
7579 Defining_Identifier => Def_Id,
7580 Subtype_Indication => Indic);
7582 Set_Parent (Subtyp_Decl, Parent (Related_Node));
7584 -- Itypes must be analyzed with checks off (see itypes.ads).
7586 Analyze (Subtyp_Decl, Suppress => All_Checks);
7588 return Def_Id;
7589 end Build_Subtype;
7591 ---------------------
7592 -- Get_Discr_Value --
7593 ---------------------
7595 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
7596 D : Entity_Id := First_Discriminant (Typ);
7597 E : Elmt_Id := First_Elmt (Constraints);
7598 G : Elmt_Id;
7600 begin
7601 -- The discriminant may be declared for the type, in which case we
7602 -- find it by iterating over the list of discriminants. If the
7603 -- discriminant is inherited from a parent type, it appears as the
7604 -- corresponding discriminant of the current type. This will be the
7605 -- case when constraining an inherited component whose constraint is
7606 -- given by a discriminant of the parent.
7608 while Present (D) loop
7609 if D = Entity (Discrim)
7610 or else Corresponding_Discriminant (D) = Entity (Discrim)
7611 then
7612 return Node (E);
7613 end if;
7615 Next_Discriminant (D);
7616 Next_Elmt (E);
7617 end loop;
7619 -- The corresponding_Discriminant mechanism is incomplete, because
7620 -- the correspondence between new and old discriminants is not one
7621 -- to one: one new discriminant can constrain several old ones.
7622 -- In that case, scan sequentially the stored_constraint, the list
7623 -- of discriminants of the parents, and the constraints.
7625 if Is_Derived_Type (Typ)
7626 and then Present (Stored_Constraint (Typ))
7627 and then Scope (Entity (Discrim)) = Etype (Typ)
7628 then
7629 D := First_Discriminant (Etype (Typ));
7630 E := First_Elmt (Constraints);
7631 G := First_Elmt (Stored_Constraint (Typ));
7633 while Present (D) loop
7634 if D = Entity (Discrim) then
7635 return Node (E);
7636 end if;
7638 Next_Discriminant (D);
7639 Next_Elmt (E);
7640 Next_Elmt (G);
7641 end loop;
7642 end if;
7644 -- Something is wrong if we did not find the value
7646 raise Program_Error;
7647 end Get_Discr_Value;
7649 ---------------------
7650 -- Is_Discriminant --
7651 ---------------------
7653 function Is_Discriminant (Expr : Node_Id) return Boolean is
7654 Discrim_Scope : Entity_Id;
7656 begin
7657 if Denotes_Discriminant (Expr) then
7658 Discrim_Scope := Scope (Entity (Expr));
7660 -- Either we have a reference to one of Typ's discriminants,
7662 pragma Assert (Discrim_Scope = Typ
7664 -- or to the discriminants of the parent type, in the case
7665 -- of a derivation of a tagged type with variants.
7667 or else Discrim_Scope = Etype (Typ)
7668 or else Full_View (Discrim_Scope) = Etype (Typ)
7670 -- or same as above for the case where the discriminants
7671 -- were declared in Typ's private view.
7673 or else (Is_Private_Type (Discrim_Scope)
7674 and then Chars (Discrim_Scope) = Chars (Typ))
7676 -- or else we are deriving from the full view and the
7677 -- discriminant is declared in the private entity.
7679 or else (Is_Private_Type (Typ)
7680 and then Chars (Discrim_Scope) = Chars (Typ))
7682 -- or we have a class-wide type, in which case make sure the
7683 -- discriminant found belongs to the root type.
7685 or else (Is_Class_Wide_Type (Typ)
7686 and then Etype (Typ) = Discrim_Scope));
7688 return True;
7689 end if;
7691 -- In all other cases we have something wrong.
7693 return False;
7694 end Is_Discriminant;
7696 -- Start of processing for Constrain_Component_Type
7698 begin
7699 if Is_Array_Type (Compon_Type) then
7700 return Build_Constrained_Array_Type (Compon_Type);
7702 elsif Has_Discriminants (Compon_Type) then
7703 return Build_Constrained_Discriminated_Type (Compon_Type);
7705 elsif Is_Access_Type (Compon_Type) then
7706 return Build_Constrained_Access_Type (Compon_Type);
7707 end if;
7709 return Compon_Type;
7710 end Constrain_Component_Type;
7712 --------------------------
7713 -- Constrain_Concurrent --
7714 --------------------------
7716 -- For concurrent types, the associated record value type carries the same
7717 -- discriminants, so when we constrain a concurrent type, we must constrain
7718 -- the value type as well.
7720 procedure Constrain_Concurrent
7721 (Def_Id : in out Entity_Id;
7722 SI : Node_Id;
7723 Related_Nod : Node_Id;
7724 Related_Id : Entity_Id;
7725 Suffix : Character)
7727 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
7728 T_Val : Entity_Id;
7730 begin
7731 if Ekind (T_Ent) in Access_Kind then
7732 T_Ent := Designated_Type (T_Ent);
7733 end if;
7735 T_Val := Corresponding_Record_Type (T_Ent);
7737 if Present (T_Val) then
7739 if No (Def_Id) then
7740 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
7741 end if;
7743 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
7745 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
7746 Set_Corresponding_Record_Type (Def_Id,
7747 Constrain_Corresponding_Record
7748 (Def_Id, T_Val, Related_Nod, Related_Id));
7750 else
7751 -- If there is no associated record, expansion is disabled and this
7752 -- is a generic context. Create a subtype in any case, so that
7753 -- semantic analysis can proceed.
7755 if No (Def_Id) then
7756 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
7757 end if;
7759 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
7760 end if;
7761 end Constrain_Concurrent;
7763 ------------------------------------
7764 -- Constrain_Corresponding_Record --
7765 ------------------------------------
7767 function Constrain_Corresponding_Record
7768 (Prot_Subt : Entity_Id;
7769 Corr_Rec : Entity_Id;
7770 Related_Nod : Node_Id;
7771 Related_Id : Entity_Id) return Entity_Id
7773 T_Sub : constant Entity_Id
7774 := Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
7776 begin
7777 Set_Etype (T_Sub, Corr_Rec);
7778 Init_Size_Align (T_Sub);
7779 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
7780 Set_Is_Constrained (T_Sub, True);
7781 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
7782 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
7784 Conditional_Delay (T_Sub, Corr_Rec);
7786 if Has_Discriminants (Prot_Subt) then -- False only if errors.
7787 Set_Discriminant_Constraint (T_Sub,
7788 Discriminant_Constraint (Prot_Subt));
7789 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
7790 Create_Constrained_Components (T_Sub, Related_Nod, Corr_Rec,
7791 Discriminant_Constraint (T_Sub));
7792 end if;
7794 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
7796 return T_Sub;
7797 end Constrain_Corresponding_Record;
7799 -----------------------
7800 -- Constrain_Decimal --
7801 -----------------------
7803 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
7804 T : constant Entity_Id := Entity (Subtype_Mark (S));
7805 C : constant Node_Id := Constraint (S);
7806 Loc : constant Source_Ptr := Sloc (C);
7807 Range_Expr : Node_Id;
7808 Digits_Expr : Node_Id;
7809 Digits_Val : Uint;
7810 Bound_Val : Ureal;
7812 begin
7813 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
7815 if Nkind (C) = N_Range_Constraint then
7816 Range_Expr := Range_Expression (C);
7817 Digits_Val := Digits_Value (T);
7819 else
7820 pragma Assert (Nkind (C) = N_Digits_Constraint);
7821 Digits_Expr := Digits_Expression (C);
7822 Analyze_And_Resolve (Digits_Expr, Any_Integer);
7824 Check_Digits_Expression (Digits_Expr);
7825 Digits_Val := Expr_Value (Digits_Expr);
7827 if Digits_Val > Digits_Value (T) then
7828 Error_Msg_N
7829 ("digits expression is incompatible with subtype", C);
7830 Digits_Val := Digits_Value (T);
7831 end if;
7833 if Present (Range_Constraint (C)) then
7834 Range_Expr := Range_Expression (Range_Constraint (C));
7835 else
7836 Range_Expr := Empty;
7837 end if;
7838 end if;
7840 Set_Etype (Def_Id, Base_Type (T));
7841 Set_Size_Info (Def_Id, (T));
7842 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
7843 Set_Delta_Value (Def_Id, Delta_Value (T));
7844 Set_Scale_Value (Def_Id, Scale_Value (T));
7845 Set_Small_Value (Def_Id, Small_Value (T));
7846 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
7847 Set_Digits_Value (Def_Id, Digits_Val);
7849 -- Manufacture range from given digits value if no range present
7851 if No (Range_Expr) then
7852 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
7853 Range_Expr :=
7854 Make_Range (Loc,
7855 Low_Bound =>
7856 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
7857 High_Bound =>
7858 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
7860 end if;
7862 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
7863 Set_Discrete_RM_Size (Def_Id);
7865 -- Unconditionally delay the freeze, since we cannot set size
7866 -- information in all cases correctly until the freeze point.
7868 Set_Has_Delayed_Freeze (Def_Id);
7869 end Constrain_Decimal;
7871 ----------------------------------
7872 -- Constrain_Discriminated_Type --
7873 ----------------------------------
7875 procedure Constrain_Discriminated_Type
7876 (Def_Id : Entity_Id;
7877 S : Node_Id;
7878 Related_Nod : Node_Id;
7879 For_Access : Boolean := False)
7881 E : constant Entity_Id := Entity (Subtype_Mark (S));
7882 T : Entity_Id;
7883 C : Node_Id;
7884 Elist : Elist_Id := New_Elmt_List;
7886 procedure Fixup_Bad_Constraint;
7887 -- This is called after finding a bad constraint, and after having
7888 -- posted an appropriate error message. The mission is to leave the
7889 -- entity T in as reasonable state as possible!
7891 --------------------------
7892 -- Fixup_Bad_Constraint --
7893 --------------------------
7895 procedure Fixup_Bad_Constraint is
7896 begin
7897 -- Set a reasonable Ekind for the entity. For an incomplete type,
7898 -- we can't do much, but for other types, we can set the proper
7899 -- corresponding subtype kind.
7901 if Ekind (T) = E_Incomplete_Type then
7902 Set_Ekind (Def_Id, Ekind (T));
7903 else
7904 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7905 end if;
7907 Set_Etype (Def_Id, Any_Type);
7908 Set_Error_Posted (Def_Id);
7909 end Fixup_Bad_Constraint;
7911 -- Start of processing for Constrain_Discriminated_Type
7913 begin
7914 C := Constraint (S);
7916 -- A discriminant constraint is only allowed in a subtype indication,
7917 -- after a subtype mark. This subtype mark must denote either a type
7918 -- with discriminants, or an access type whose designated type is a
7919 -- type with discriminants. A discriminant constraint specifies the
7920 -- values of these discriminants (RM 3.7.2(5)).
7922 T := Base_Type (Entity (Subtype_Mark (S)));
7924 if Ekind (T) in Access_Kind then
7925 T := Designated_Type (T);
7926 end if;
7928 -- Check that the type has visible discriminants. The type may be
7929 -- a private type with unknown discriminants whose full view has
7930 -- discriminants which are invisible.
7932 if not Has_Discriminants (T)
7933 or else
7934 (Has_Unknown_Discriminants (T)
7935 and then Is_Private_Type (T))
7936 then
7937 Error_Msg_N ("invalid constraint: type has no discriminant", C);
7938 Fixup_Bad_Constraint;
7939 return;
7941 elsif Is_Constrained (E)
7942 or else (Ekind (E) = E_Class_Wide_Subtype
7943 and then Present (Discriminant_Constraint (E)))
7944 then
7945 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
7946 Fixup_Bad_Constraint;
7947 return;
7948 end if;
7950 -- T may be an unconstrained subtype (e.g. a generic actual).
7951 -- Constraint applies to the base type.
7953 T := Base_Type (T);
7955 Elist := Build_Discriminant_Constraints (T, S);
7957 -- If the list returned was empty we had an error in building the
7958 -- discriminant constraint. We have also already signalled an error
7959 -- in the incomplete type case
7961 if Is_Empty_Elmt_List (Elist) then
7962 Fixup_Bad_Constraint;
7963 return;
7964 end if;
7966 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
7967 end Constrain_Discriminated_Type;
7969 ---------------------------
7970 -- Constrain_Enumeration --
7971 ---------------------------
7973 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
7974 T : constant Entity_Id := Entity (Subtype_Mark (S));
7975 C : constant Node_Id := Constraint (S);
7977 begin
7978 Set_Ekind (Def_Id, E_Enumeration_Subtype);
7980 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
7982 Set_Etype (Def_Id, Base_Type (T));
7983 Set_Size_Info (Def_Id, (T));
7984 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
7985 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
7987 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
7989 Set_Discrete_RM_Size (Def_Id);
7991 end Constrain_Enumeration;
7993 ----------------------
7994 -- Constrain_Float --
7995 ----------------------
7997 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
7998 T : constant Entity_Id := Entity (Subtype_Mark (S));
7999 C : Node_Id;
8000 D : Node_Id;
8001 Rais : Node_Id;
8003 begin
8004 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
8006 Set_Etype (Def_Id, Base_Type (T));
8007 Set_Size_Info (Def_Id, (T));
8008 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8010 -- Process the constraint
8012 C := Constraint (S);
8014 -- Digits constraint present
8016 if Nkind (C) = N_Digits_Constraint then
8017 if Warn_On_Obsolescent_Feature then
8018 Error_Msg_N
8019 ("subtype digits constraint is an " &
8020 "obsolescent feature ('R'M 'J.3(8))?", C);
8021 end if;
8023 D := Digits_Expression (C);
8024 Analyze_And_Resolve (D, Any_Integer);
8025 Check_Digits_Expression (D);
8026 Set_Digits_Value (Def_Id, Expr_Value (D));
8028 -- Check that digits value is in range. Obviously we can do this
8029 -- at compile time, but it is strictly a runtime check, and of
8030 -- course there is an ACVC test that checks this!
8032 if Digits_Value (Def_Id) > Digits_Value (T) then
8033 Error_Msg_Uint_1 := Digits_Value (T);
8034 Error_Msg_N ("?digits value is too large, maximum is ^", D);
8035 Rais :=
8036 Make_Raise_Constraint_Error (Sloc (D),
8037 Reason => CE_Range_Check_Failed);
8038 Insert_Action (Declaration_Node (Def_Id), Rais);
8039 end if;
8041 C := Range_Constraint (C);
8043 -- No digits constraint present
8045 else
8046 Set_Digits_Value (Def_Id, Digits_Value (T));
8047 end if;
8049 -- Range constraint present
8051 if Nkind (C) = N_Range_Constraint then
8052 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8054 -- No range constraint present
8056 else
8057 pragma Assert (No (C));
8058 Set_Scalar_Range (Def_Id, Scalar_Range (T));
8059 end if;
8061 Set_Is_Constrained (Def_Id);
8062 end Constrain_Float;
8064 ---------------------
8065 -- Constrain_Index --
8066 ---------------------
8068 procedure Constrain_Index
8069 (Index : Node_Id;
8070 S : Node_Id;
8071 Related_Nod : Node_Id;
8072 Related_Id : Entity_Id;
8073 Suffix : Character;
8074 Suffix_Index : Nat)
8076 Def_Id : Entity_Id;
8077 R : Node_Id := Empty;
8078 T : constant Entity_Id := Etype (Index);
8080 begin
8081 if Nkind (S) = N_Range
8082 or else
8083 (Nkind (S) = N_Attribute_Reference
8084 and then Attribute_Name (S) = Name_Range)
8085 then
8086 -- A Range attribute will transformed into N_Range by Resolve.
8088 Analyze (S);
8089 Set_Etype (S, T);
8090 R := S;
8092 Process_Range_Expr_In_Decl (R, T, Empty_List);
8094 if not Error_Posted (S)
8095 and then
8096 (Nkind (S) /= N_Range
8097 or else not Covers (T, (Etype (Low_Bound (S))))
8098 or else not Covers (T, (Etype (High_Bound (S)))))
8099 then
8100 if Base_Type (T) /= Any_Type
8101 and then Etype (Low_Bound (S)) /= Any_Type
8102 and then Etype (High_Bound (S)) /= Any_Type
8103 then
8104 Error_Msg_N ("range expected", S);
8105 end if;
8106 end if;
8108 elsif Nkind (S) = N_Subtype_Indication then
8109 -- the parser has verified that this is a discrete indication.
8111 Resolve_Discrete_Subtype_Indication (S, T);
8112 R := Range_Expression (Constraint (S));
8114 elsif Nkind (S) = N_Discriminant_Association then
8116 -- syntactically valid in subtype indication.
8118 Error_Msg_N ("invalid index constraint", S);
8119 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
8120 return;
8122 -- Subtype_Mark case, no anonymous subtypes to construct
8124 else
8125 Analyze (S);
8127 if Is_Entity_Name (S) then
8129 if not Is_Type (Entity (S)) then
8130 Error_Msg_N ("expect subtype mark for index constraint", S);
8132 elsif Base_Type (Entity (S)) /= Base_Type (T) then
8133 Wrong_Type (S, Base_Type (T));
8134 end if;
8136 return;
8138 else
8139 Error_Msg_N ("invalid index constraint", S);
8140 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
8141 return;
8142 end if;
8143 end if;
8145 Def_Id :=
8146 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
8148 Set_Etype (Def_Id, Base_Type (T));
8150 if Is_Modular_Integer_Type (T) then
8151 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
8153 elsif Is_Integer_Type (T) then
8154 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
8156 else
8157 Set_Ekind (Def_Id, E_Enumeration_Subtype);
8158 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
8159 end if;
8161 Set_Size_Info (Def_Id, (T));
8162 Set_RM_Size (Def_Id, RM_Size (T));
8163 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8165 Set_Scalar_Range (Def_Id, R);
8167 Set_Etype (S, Def_Id);
8168 Set_Discrete_RM_Size (Def_Id);
8169 end Constrain_Index;
8171 -----------------------
8172 -- Constrain_Integer --
8173 -----------------------
8175 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
8176 T : constant Entity_Id := Entity (Subtype_Mark (S));
8177 C : constant Node_Id := Constraint (S);
8179 begin
8180 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8182 if Is_Modular_Integer_Type (T) then
8183 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
8184 else
8185 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
8186 end if;
8188 Set_Etype (Def_Id, Base_Type (T));
8189 Set_Size_Info (Def_Id, (T));
8190 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8191 Set_Discrete_RM_Size (Def_Id);
8193 end Constrain_Integer;
8195 ------------------------------
8196 -- Constrain_Ordinary_Fixed --
8197 ------------------------------
8199 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
8200 T : constant Entity_Id := Entity (Subtype_Mark (S));
8201 C : Node_Id;
8202 D : Node_Id;
8203 Rais : Node_Id;
8205 begin
8206 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
8207 Set_Etype (Def_Id, Base_Type (T));
8208 Set_Size_Info (Def_Id, (T));
8209 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8210 Set_Small_Value (Def_Id, Small_Value (T));
8212 -- Process the constraint
8214 C := Constraint (S);
8216 -- Delta constraint present
8218 if Nkind (C) = N_Delta_Constraint then
8219 if Warn_On_Obsolescent_Feature then
8220 Error_Msg_S
8221 ("subtype delta constraint is an " &
8222 "obsolescent feature ('R'M 'J.3(7))?");
8223 end if;
8225 D := Delta_Expression (C);
8226 Analyze_And_Resolve (D, Any_Real);
8227 Check_Delta_Expression (D);
8228 Set_Delta_Value (Def_Id, Expr_Value_R (D));
8230 -- Check that delta value is in range. Obviously we can do this
8231 -- at compile time, but it is strictly a runtime check, and of
8232 -- course there is an ACVC test that checks this!
8234 if Delta_Value (Def_Id) < Delta_Value (T) then
8235 Error_Msg_N ("?delta value is too small", D);
8236 Rais :=
8237 Make_Raise_Constraint_Error (Sloc (D),
8238 Reason => CE_Range_Check_Failed);
8239 Insert_Action (Declaration_Node (Def_Id), Rais);
8240 end if;
8242 C := Range_Constraint (C);
8244 -- No delta constraint present
8246 else
8247 Set_Delta_Value (Def_Id, Delta_Value (T));
8248 end if;
8250 -- Range constraint present
8252 if Nkind (C) = N_Range_Constraint then
8253 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8255 -- No range constraint present
8257 else
8258 pragma Assert (No (C));
8259 Set_Scalar_Range (Def_Id, Scalar_Range (T));
8261 end if;
8263 Set_Discrete_RM_Size (Def_Id);
8265 -- Unconditionally delay the freeze, since we cannot set size
8266 -- information in all cases correctly until the freeze point.
8268 Set_Has_Delayed_Freeze (Def_Id);
8269 end Constrain_Ordinary_Fixed;
8271 ---------------------------
8272 -- Convert_Scalar_Bounds --
8273 ---------------------------
8275 procedure Convert_Scalar_Bounds
8276 (N : Node_Id;
8277 Parent_Type : Entity_Id;
8278 Derived_Type : Entity_Id;
8279 Loc : Source_Ptr)
8281 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
8283 Lo : Node_Id;
8284 Hi : Node_Id;
8285 Rng : Node_Id;
8287 begin
8288 Lo := Build_Scalar_Bound
8289 (Type_Low_Bound (Derived_Type),
8290 Parent_Type, Implicit_Base);
8292 Hi := Build_Scalar_Bound
8293 (Type_High_Bound (Derived_Type),
8294 Parent_Type, Implicit_Base);
8296 Rng :=
8297 Make_Range (Loc,
8298 Low_Bound => Lo,
8299 High_Bound => Hi);
8301 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
8303 Set_Parent (Rng, N);
8304 Set_Scalar_Range (Derived_Type, Rng);
8306 -- Analyze the bounds
8308 Analyze_And_Resolve (Lo, Implicit_Base);
8309 Analyze_And_Resolve (Hi, Implicit_Base);
8311 -- Analyze the range itself, except that we do not analyze it if
8312 -- the bounds are real literals, and we have a fixed-point type.
8313 -- The reason for this is that we delay setting the bounds in this
8314 -- case till we know the final Small and Size values (see circuit
8315 -- in Freeze.Freeze_Fixed_Point_Type for further details).
8317 if Is_Fixed_Point_Type (Parent_Type)
8318 and then Nkind (Lo) = N_Real_Literal
8319 and then Nkind (Hi) = N_Real_Literal
8320 then
8321 return;
8323 -- Here we do the analysis of the range.
8325 -- Note: we do this manually, since if we do a normal Analyze and
8326 -- Resolve call, there are problems with the conversions used for
8327 -- the derived type range.
8329 else
8330 Set_Etype (Rng, Implicit_Base);
8331 Set_Analyzed (Rng, True);
8332 end if;
8333 end Convert_Scalar_Bounds;
8335 -------------------
8336 -- Copy_And_Swap --
8337 -------------------
8339 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
8341 begin
8342 -- Initialize new full declaration entity by copying the pertinent
8343 -- fields of the corresponding private declaration entity.
8345 -- We temporarily set Ekind to a value appropriate for a type to
8346 -- avoid assert failures in Einfo from checking for setting type
8347 -- attributes on something that is not a type. Ekind (Priv) is an
8348 -- appropriate choice, since it allowed the attributes to be set
8349 -- in the first place. This Ekind value will be modified later.
8351 Set_Ekind (Full, Ekind (Priv));
8353 -- Also set Etype temporarily to Any_Type, again, in the absence
8354 -- of errors, it will be properly reset, and if there are errors,
8355 -- then we want a value of Any_Type to remain.
8357 Set_Etype (Full, Any_Type);
8359 -- Now start copying attributes
8361 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
8363 if Has_Discriminants (Full) then
8364 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
8365 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
8366 end if;
8368 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
8369 Set_Homonym (Full, Homonym (Priv));
8370 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
8371 Set_Is_Public (Full, Is_Public (Priv));
8372 Set_Is_Pure (Full, Is_Pure (Priv));
8373 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
8375 Conditional_Delay (Full, Priv);
8377 if Is_Tagged_Type (Full) then
8378 Set_Primitive_Operations (Full, Primitive_Operations (Priv));
8380 if Priv = Base_Type (Priv) then
8381 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
8382 end if;
8383 end if;
8385 Set_Is_Volatile (Full, Is_Volatile (Priv));
8386 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
8387 Set_Scope (Full, Scope (Priv));
8388 Set_Next_Entity (Full, Next_Entity (Priv));
8389 Set_First_Entity (Full, First_Entity (Priv));
8390 Set_Last_Entity (Full, Last_Entity (Priv));
8392 -- If access types have been recorded for later handling, keep them
8393 -- in the full view so that they get handled when the full view
8394 -- freeze node is expanded.
8396 if Present (Freeze_Node (Priv))
8397 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
8398 then
8399 Ensure_Freeze_Node (Full);
8400 Set_Access_Types_To_Process
8401 (Freeze_Node (Full),
8402 Access_Types_To_Process (Freeze_Node (Priv)));
8403 end if;
8405 -- Swap the two entities. Now Privat is the full type entity and
8406 -- Full is the private one. They will be swapped back at the end
8407 -- of the private part. This swapping ensures that the entity that
8408 -- is visible in the private part is the full declaration.
8410 Exchange_Entities (Priv, Full);
8411 Append_Entity (Full, Scope (Full));
8412 end Copy_And_Swap;
8414 -------------------------------------
8415 -- Copy_Array_Base_Type_Attributes --
8416 -------------------------------------
8418 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
8419 begin
8420 Set_Component_Alignment (T1, Component_Alignment (T2));
8421 Set_Component_Type (T1, Component_Type (T2));
8422 Set_Component_Size (T1, Component_Size (T2));
8423 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
8424 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
8425 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
8426 Set_Has_Task (T1, Has_Task (T2));
8427 Set_Is_Packed (T1, Is_Packed (T2));
8428 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
8429 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
8430 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
8431 end Copy_Array_Base_Type_Attributes;
8433 -----------------------------------
8434 -- Copy_Array_Subtype_Attributes --
8435 -----------------------------------
8437 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
8438 begin
8439 Set_Size_Info (T1, T2);
8441 Set_First_Index (T1, First_Index (T2));
8442 Set_Is_Aliased (T1, Is_Aliased (T2));
8443 Set_Is_Atomic (T1, Is_Atomic (T2));
8444 Set_Is_Volatile (T1, Is_Volatile (T2));
8445 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
8446 Set_Is_Constrained (T1, Is_Constrained (T2));
8447 Set_Depends_On_Private (T1, Has_Private_Component (T2));
8448 Set_First_Rep_Item (T1, First_Rep_Item (T2));
8449 Set_Convention (T1, Convention (T2));
8450 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
8451 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
8452 end Copy_Array_Subtype_Attributes;
8454 -----------------------------------
8455 -- Create_Constrained_Components --
8456 -----------------------------------
8458 procedure Create_Constrained_Components
8459 (Subt : Entity_Id;
8460 Decl_Node : Node_Id;
8461 Typ : Entity_Id;
8462 Constraints : Elist_Id)
8464 Loc : constant Source_Ptr := Sloc (Subt);
8465 Comp_List : constant Elist_Id := New_Elmt_List;
8466 Parent_Type : constant Entity_Id := Etype (Typ);
8467 Assoc_List : constant List_Id := New_List;
8468 Discr_Val : Elmt_Id;
8469 Errors : Boolean;
8470 New_C : Entity_Id;
8471 Old_C : Entity_Id;
8472 Is_Static : Boolean := True;
8474 procedure Collect_Fixed_Components (Typ : Entity_Id);
8475 -- Collect components of parent type that do not appear in a variant
8476 -- part.
8478 procedure Create_All_Components;
8479 -- Iterate over Comp_List to create the components of the subtype.
8481 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
8482 -- Creates a new component from Old_Compon, copying all the fields from
8483 -- it, including its Etype, inserts the new component in the Subt entity
8484 -- chain and returns the new component.
8486 function Is_Variant_Record (T : Entity_Id) return Boolean;
8487 -- If true, and discriminants are static, collect only components from
8488 -- variants selected by discriminant values.
8490 ------------------------------
8491 -- Collect_Fixed_Components --
8492 ------------------------------
8494 procedure Collect_Fixed_Components (Typ : Entity_Id) is
8495 begin
8496 -- Build association list for discriminants, and find components of
8497 -- the variant part selected by the values of the discriminants.
8499 Old_C := First_Discriminant (Typ);
8500 Discr_Val := First_Elmt (Constraints);
8502 while Present (Old_C) loop
8503 Append_To (Assoc_List,
8504 Make_Component_Association (Loc,
8505 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
8506 Expression => New_Copy (Node (Discr_Val))));
8508 Next_Elmt (Discr_Val);
8509 Next_Discriminant (Old_C);
8510 end loop;
8512 -- The tag, and the possible parent and controller components
8513 -- are unconditionally in the subtype.
8515 if Is_Tagged_Type (Typ)
8516 or else Has_Controlled_Component (Typ)
8517 then
8518 Old_C := First_Component (Typ);
8520 while Present (Old_C) loop
8521 if Chars ((Old_C)) = Name_uTag
8522 or else Chars ((Old_C)) = Name_uParent
8523 or else Chars ((Old_C)) = Name_uController
8524 then
8525 Append_Elmt (Old_C, Comp_List);
8526 end if;
8528 Next_Component (Old_C);
8529 end loop;
8530 end if;
8531 end Collect_Fixed_Components;
8533 ---------------------------
8534 -- Create_All_Components --
8535 ---------------------------
8537 procedure Create_All_Components is
8538 Comp : Elmt_Id;
8540 begin
8541 Comp := First_Elmt (Comp_List);
8543 while Present (Comp) loop
8544 Old_C := Node (Comp);
8545 New_C := Create_Component (Old_C);
8547 Set_Etype
8548 (New_C,
8549 Constrain_Component_Type
8550 (Etype (Old_C), Subt, Decl_Node, Typ, Constraints));
8551 Set_Is_Public (New_C, Is_Public (Subt));
8553 Next_Elmt (Comp);
8554 end loop;
8555 end Create_All_Components;
8557 ----------------------
8558 -- Create_Component --
8559 ----------------------
8561 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
8562 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
8564 begin
8565 -- Set the parent so we have a proper link for freezing etc. This
8566 -- is not a real parent pointer, since of course our parent does
8567 -- not own up to us and reference us, we are an illegitimate
8568 -- child of the original parent!
8570 Set_Parent (New_Compon, Parent (Old_Compon));
8572 -- We do not want this node marked as Comes_From_Source, since
8573 -- otherwise it would get first class status and a separate
8574 -- cross-reference line would be generated. Illegitimate
8575 -- children do not rate such recognition.
8577 Set_Comes_From_Source (New_Compon, False);
8579 -- But it is a real entity, and a birth certificate must be
8580 -- properly registered by entering it into the entity list.
8582 Enter_Name (New_Compon);
8583 return New_Compon;
8584 end Create_Component;
8586 -----------------------
8587 -- Is_Variant_Record --
8588 -----------------------
8590 function Is_Variant_Record (T : Entity_Id) return Boolean is
8591 begin
8592 return Nkind (Parent (T)) = N_Full_Type_Declaration
8593 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
8594 and then Present (Component_List (Type_Definition (Parent (T))))
8595 and then Present (
8596 Variant_Part (Component_List (Type_Definition (Parent (T)))));
8597 end Is_Variant_Record;
8599 -- Start of processing for Create_Constrained_Components
8601 begin
8602 pragma Assert (Subt /= Base_Type (Subt));
8603 pragma Assert (Typ = Base_Type (Typ));
8605 Set_First_Entity (Subt, Empty);
8606 Set_Last_Entity (Subt, Empty);
8608 -- Check whether constraint is fully static, in which case we can
8609 -- optimize the list of components.
8611 Discr_Val := First_Elmt (Constraints);
8613 while Present (Discr_Val) loop
8615 if not Is_OK_Static_Expression (Node (Discr_Val)) then
8616 Is_Static := False;
8617 exit;
8618 end if;
8620 Next_Elmt (Discr_Val);
8621 end loop;
8623 New_Scope (Subt);
8625 -- Inherit the discriminants of the parent type.
8627 Old_C := First_Discriminant (Typ);
8629 while Present (Old_C) loop
8630 New_C := Create_Component (Old_C);
8631 Set_Is_Public (New_C, Is_Public (Subt));
8632 Next_Discriminant (Old_C);
8633 end loop;
8635 if Is_Static
8636 and then Is_Variant_Record (Typ)
8637 then
8638 Collect_Fixed_Components (Typ);
8640 Gather_Components (
8641 Typ,
8642 Component_List (Type_Definition (Parent (Typ))),
8643 Governed_By => Assoc_List,
8644 Into => Comp_List,
8645 Report_Errors => Errors);
8646 pragma Assert (not Errors);
8648 Create_All_Components;
8650 -- If the subtype declaration is created for a tagged type derivation
8651 -- with constraints, we retrieve the record definition of the parent
8652 -- type to select the components of the proper variant.
8654 elsif Is_Static
8655 and then Is_Tagged_Type (Typ)
8656 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
8657 and then
8658 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
8659 and then Is_Variant_Record (Parent_Type)
8660 then
8661 Collect_Fixed_Components (Typ);
8663 Gather_Components (
8664 Typ,
8665 Component_List (Type_Definition (Parent (Parent_Type))),
8666 Governed_By => Assoc_List,
8667 Into => Comp_List,
8668 Report_Errors => Errors);
8669 pragma Assert (not Errors);
8671 -- If the tagged derivation has a type extension, collect all the
8672 -- new components therein.
8674 if Present (
8675 Record_Extension_Part (Type_Definition (Parent (Typ))))
8676 then
8677 Old_C := First_Component (Typ);
8679 while Present (Old_C) loop
8680 if Original_Record_Component (Old_C) = Old_C
8681 and then Chars (Old_C) /= Name_uTag
8682 and then Chars (Old_C) /= Name_uParent
8683 and then Chars (Old_C) /= Name_uController
8684 then
8685 Append_Elmt (Old_C, Comp_List);
8686 end if;
8688 Next_Component (Old_C);
8689 end loop;
8690 end if;
8692 Create_All_Components;
8694 else
8695 -- If the discriminants are not static, or if this is a multi-level
8696 -- type extension, we have to include all the components of the
8697 -- parent type.
8699 Old_C := First_Component (Typ);
8701 while Present (Old_C) loop
8702 New_C := Create_Component (Old_C);
8704 Set_Etype
8705 (New_C,
8706 Constrain_Component_Type
8707 (Etype (Old_C), Subt, Decl_Node, Typ, Constraints));
8708 Set_Is_Public (New_C, Is_Public (Subt));
8710 Next_Component (Old_C);
8711 end loop;
8712 end if;
8714 End_Scope;
8715 end Create_Constrained_Components;
8717 ------------------------------------------
8718 -- Decimal_Fixed_Point_Type_Declaration --
8719 ------------------------------------------
8721 procedure Decimal_Fixed_Point_Type_Declaration
8722 (T : Entity_Id;
8723 Def : Node_Id)
8725 Loc : constant Source_Ptr := Sloc (Def);
8726 Digs_Expr : constant Node_Id := Digits_Expression (Def);
8727 Delta_Expr : constant Node_Id := Delta_Expression (Def);
8728 Implicit_Base : Entity_Id;
8729 Digs_Val : Uint;
8730 Delta_Val : Ureal;
8731 Scale_Val : Uint;
8732 Bound_Val : Ureal;
8734 -- Start of processing for Decimal_Fixed_Point_Type_Declaration
8736 begin
8737 Check_Restriction (No_Fixed_Point, Def);
8739 -- Create implicit base type
8741 Implicit_Base :=
8742 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
8743 Set_Etype (Implicit_Base, Implicit_Base);
8745 -- Analyze and process delta expression
8747 Analyze_And_Resolve (Delta_Expr, Universal_Real);
8749 Check_Delta_Expression (Delta_Expr);
8750 Delta_Val := Expr_Value_R (Delta_Expr);
8752 -- Check delta is power of 10, and determine scale value from it
8754 declare
8755 Val : Ureal := Delta_Val;
8757 begin
8758 Scale_Val := Uint_0;
8760 if Val < Ureal_1 then
8761 while Val < Ureal_1 loop
8762 Val := Val * Ureal_10;
8763 Scale_Val := Scale_Val + 1;
8764 end loop;
8766 if Scale_Val > 18 then
8767 Error_Msg_N ("scale exceeds maximum value of 18", Def);
8768 Scale_Val := UI_From_Int (+18);
8769 end if;
8771 else
8772 while Val > Ureal_1 loop
8773 Val := Val / Ureal_10;
8774 Scale_Val := Scale_Val - 1;
8775 end loop;
8777 if Scale_Val < -18 then
8778 Error_Msg_N ("scale is less than minimum value of -18", Def);
8779 Scale_Val := UI_From_Int (-18);
8780 end if;
8781 end if;
8783 if Val /= Ureal_1 then
8784 Error_Msg_N ("delta expression must be a power of 10", Def);
8785 Delta_Val := Ureal_10 ** (-Scale_Val);
8786 end if;
8787 end;
8789 -- Set delta, scale and small (small = delta for decimal type)
8791 Set_Delta_Value (Implicit_Base, Delta_Val);
8792 Set_Scale_Value (Implicit_Base, Scale_Val);
8793 Set_Small_Value (Implicit_Base, Delta_Val);
8795 -- Analyze and process digits expression
8797 Analyze_And_Resolve (Digs_Expr, Any_Integer);
8798 Check_Digits_Expression (Digs_Expr);
8799 Digs_Val := Expr_Value (Digs_Expr);
8801 if Digs_Val > 18 then
8802 Digs_Val := UI_From_Int (+18);
8803 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
8804 end if;
8806 Set_Digits_Value (Implicit_Base, Digs_Val);
8807 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
8809 -- Set range of base type from digits value for now. This will be
8810 -- expanded to represent the true underlying base range by Freeze.
8812 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
8814 -- Set size to zero for now, size will be set at freeze time. We have
8815 -- to do this for ordinary fixed-point, because the size depends on
8816 -- the specified small, and we might as well do the same for decimal
8817 -- fixed-point.
8819 Init_Size_Align (Implicit_Base);
8821 -- If there are bounds given in the declaration use them as the
8822 -- bounds of the first named subtype.
8824 if Present (Real_Range_Specification (Def)) then
8825 declare
8826 RRS : constant Node_Id := Real_Range_Specification (Def);
8827 Low : constant Node_Id := Low_Bound (RRS);
8828 High : constant Node_Id := High_Bound (RRS);
8829 Low_Val : Ureal;
8830 High_Val : Ureal;
8832 begin
8833 Analyze_And_Resolve (Low, Any_Real);
8834 Analyze_And_Resolve (High, Any_Real);
8835 Check_Real_Bound (Low);
8836 Check_Real_Bound (High);
8837 Low_Val := Expr_Value_R (Low);
8838 High_Val := Expr_Value_R (High);
8840 if Low_Val < (-Bound_Val) then
8841 Error_Msg_N
8842 ("range low bound too small for digits value", Low);
8843 Low_Val := -Bound_Val;
8844 end if;
8846 if High_Val > Bound_Val then
8847 Error_Msg_N
8848 ("range high bound too large for digits value", High);
8849 High_Val := Bound_Val;
8850 end if;
8852 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
8853 end;
8855 -- If no explicit range, use range that corresponds to given
8856 -- digits value. This will end up as the final range for the
8857 -- first subtype.
8859 else
8860 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
8861 end if;
8863 -- Complete entity for first subtype
8865 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
8866 Set_Etype (T, Implicit_Base);
8867 Set_Size_Info (T, Implicit_Base);
8868 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
8869 Set_Digits_Value (T, Digs_Val);
8870 Set_Delta_Value (T, Delta_Val);
8871 Set_Small_Value (T, Delta_Val);
8872 Set_Scale_Value (T, Scale_Val);
8873 Set_Is_Constrained (T);
8874 end Decimal_Fixed_Point_Type_Declaration;
8876 -----------------------
8877 -- Derive_Subprogram --
8878 -----------------------
8880 procedure Derive_Subprogram
8881 (New_Subp : in out Entity_Id;
8882 Parent_Subp : Entity_Id;
8883 Derived_Type : Entity_Id;
8884 Parent_Type : Entity_Id;
8885 Actual_Subp : Entity_Id := Empty)
8887 Formal : Entity_Id;
8888 New_Formal : Entity_Id;
8889 Same_Subt : constant Boolean :=
8890 Is_Scalar_Type (Parent_Type)
8891 and then Subtypes_Statically_Compatible (Parent_Type, Derived_Type);
8892 Visible_Subp : Entity_Id := Parent_Subp;
8894 function Is_Private_Overriding return Boolean;
8895 -- If Subp is a private overriding of a visible operation, the in-
8896 -- herited operation derives from the overridden op (even though
8897 -- its body is the overriding one) and the inherited operation is
8898 -- visible now. See sem_disp to see the details of the handling of
8899 -- the overridden subprogram, which is removed from the list of
8900 -- primitive operations of the type. The overridden subprogram is
8901 -- saved locally in Visible_Subp, and used to diagnose abstract
8902 -- operations that need overriding in the derived type.
8904 procedure Replace_Type (Id, New_Id : Entity_Id);
8905 -- When the type is an anonymous access type, create a new access type
8906 -- designating the derived type.
8908 procedure Set_Derived_Name;
8909 -- This procedure sets the appropriate Chars name for New_Subp. This
8910 -- is normally just a copy of the parent name. An exception arises for
8911 -- type support subprograms, where the name is changed to reflect the
8912 -- name of the derived type, e.g. if type foo is derived from type bar,
8913 -- then a procedure barDA is derived with a name fooDA.
8915 ---------------------------
8916 -- Is_Private_Overriding --
8917 ---------------------------
8919 function Is_Private_Overriding return Boolean is
8920 Prev : Entity_Id;
8922 begin
8923 Prev := Homonym (Parent_Subp);
8925 -- The visible operation that is overriden is a homonym of
8926 -- the parent subprogram. We scan the homonym chain to find
8927 -- the one whose alias is the subprogram we are deriving.
8929 while Present (Prev) loop
8930 if Is_Dispatching_Operation (Parent_Subp)
8931 and then Present (Prev)
8932 and then Ekind (Prev) = Ekind (Parent_Subp)
8933 and then Alias (Prev) = Parent_Subp
8934 and then Scope (Parent_Subp) = Scope (Prev)
8935 and then not Is_Hidden (Prev)
8936 then
8937 Visible_Subp := Prev;
8938 return True;
8939 end if;
8941 Prev := Homonym (Prev);
8942 end loop;
8944 return False;
8945 end Is_Private_Overriding;
8947 ------------------
8948 -- Replace_Type --
8949 ------------------
8951 procedure Replace_Type (Id, New_Id : Entity_Id) is
8952 Acc_Type : Entity_Id;
8953 IR : Node_Id;
8955 begin
8956 -- When the type is an anonymous access type, create a new access
8957 -- type designating the derived type. This itype must be elaborated
8958 -- at the point of the derivation, not on subsequent calls that may
8959 -- be out of the proper scope for Gigi, so we insert a reference to
8960 -- it after the derivation.
8962 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
8963 declare
8964 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
8966 begin
8967 if Ekind (Desig_Typ) = E_Record_Type_With_Private
8968 and then Present (Full_View (Desig_Typ))
8969 and then not Is_Private_Type (Parent_Type)
8970 then
8971 Desig_Typ := Full_View (Desig_Typ);
8972 end if;
8974 if Base_Type (Desig_Typ) = Base_Type (Parent_Type) then
8975 Acc_Type := New_Copy (Etype (Id));
8976 Set_Etype (Acc_Type, Acc_Type);
8977 Set_Scope (Acc_Type, New_Subp);
8979 -- Compute size of anonymous access type.
8981 if Is_Array_Type (Desig_Typ)
8982 and then not Is_Constrained (Desig_Typ)
8983 then
8984 Init_Size (Acc_Type, 2 * System_Address_Size);
8985 else
8986 Init_Size (Acc_Type, System_Address_Size);
8987 end if;
8989 Init_Alignment (Acc_Type);
8991 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
8993 Set_Etype (New_Id, Acc_Type);
8994 Set_Scope (New_Id, New_Subp);
8996 -- Create a reference to it.
8998 IR := Make_Itype_Reference (Sloc (Parent (Derived_Type)));
8999 Set_Itype (IR, Acc_Type);
9000 Insert_After (Parent (Derived_Type), IR);
9002 else
9003 Set_Etype (New_Id, Etype (Id));
9004 end if;
9005 end;
9006 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
9007 or else
9008 (Ekind (Etype (Id)) = E_Record_Type_With_Private
9009 and then Present (Full_View (Etype (Id)))
9010 and then Base_Type (Full_View (Etype (Id))) =
9011 Base_Type (Parent_Type))
9012 then
9014 -- Constraint checks on formals are generated during expansion,
9015 -- based on the signature of the original subprogram. The bounds
9016 -- of the derived type are not relevant, and thus we can use
9017 -- the base type for the formals. However, the return type may be
9018 -- used in a context that requires that the proper static bounds
9019 -- be used (a case statement, for example) and for those cases
9020 -- we must use the derived type (first subtype), not its base.
9022 if Etype (Id) = Parent_Type
9023 and then Same_Subt
9024 then
9025 Set_Etype (New_Id, Derived_Type);
9026 else
9027 Set_Etype (New_Id, Base_Type (Derived_Type));
9028 end if;
9030 else
9031 Set_Etype (New_Id, Etype (Id));
9032 end if;
9033 end Replace_Type;
9035 ----------------------
9036 -- Set_Derived_Name --
9037 ----------------------
9039 procedure Set_Derived_Name is
9040 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
9041 begin
9042 if Nm = TSS_Null then
9043 Set_Chars (New_Subp, Chars (Parent_Subp));
9044 else
9045 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
9046 end if;
9047 end Set_Derived_Name;
9049 -- Start of processing for Derive_Subprogram
9051 begin
9052 New_Subp :=
9053 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
9054 Set_Ekind (New_Subp, Ekind (Parent_Subp));
9056 -- Check whether the inherited subprogram is a private operation that
9057 -- should be inherited but not yet made visible. Such subprograms can
9058 -- become visible at a later point (e.g., the private part of a public
9059 -- child unit) via Declare_Inherited_Private_Subprograms. If the
9060 -- following predicate is true, then this is not such a private
9061 -- operation and the subprogram simply inherits the name of the parent
9062 -- subprogram. Note the special check for the names of controlled
9063 -- operations, which are currently exempted from being inherited with
9064 -- a hidden name because they must be findable for generation of
9065 -- implicit run-time calls.
9067 if not Is_Hidden (Parent_Subp)
9068 or else Is_Internal (Parent_Subp)
9069 or else Is_Private_Overriding
9070 or else Is_Internal_Name (Chars (Parent_Subp))
9071 or else Chars (Parent_Subp) = Name_Initialize
9072 or else Chars (Parent_Subp) = Name_Adjust
9073 or else Chars (Parent_Subp) = Name_Finalize
9074 then
9075 Set_Derived_Name;
9077 -- If parent is hidden, this can be a regular derivation if the
9078 -- parent is immediately visible in a non-instantiating context,
9079 -- or if we are in the private part of an instance. This test
9080 -- should still be refined ???
9082 -- The test for In_Instance_Not_Visible avoids inheriting the
9083 -- derived operation as a non-visible operation in cases where
9084 -- the parent subprogram might not be visible now, but was
9085 -- visible within the original generic, so it would be wrong
9086 -- to make the inherited subprogram non-visible now. (Not
9087 -- clear if this test is fully correct; are there any cases
9088 -- where we should declare the inherited operation as not
9089 -- visible to avoid it being overridden, e.g., when the
9090 -- parent type is a generic actual with private primitives ???)
9092 -- (they should be treated the same as other private inherited
9093 -- subprograms, but it's not clear how to do this cleanly). ???
9095 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
9096 and then Is_Immediately_Visible (Parent_Subp)
9097 and then not In_Instance)
9098 or else In_Instance_Not_Visible
9099 then
9100 Set_Derived_Name;
9102 -- The type is inheriting a private operation, so enter
9103 -- it with a special name so it can't be overridden.
9105 else
9106 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
9107 end if;
9109 Set_Parent (New_Subp, Parent (Derived_Type));
9110 Replace_Type (Parent_Subp, New_Subp);
9111 Conditional_Delay (New_Subp, Parent_Subp);
9113 Formal := First_Formal (Parent_Subp);
9114 while Present (Formal) loop
9115 New_Formal := New_Copy (Formal);
9117 -- Normally we do not go copying parents, but in the case of
9118 -- formals, we need to link up to the declaration (which is
9119 -- the parameter specification), and it is fine to link up to
9120 -- the original formal's parameter specification in this case.
9122 Set_Parent (New_Formal, Parent (Formal));
9124 Append_Entity (New_Formal, New_Subp);
9126 Replace_Type (Formal, New_Formal);
9127 Next_Formal (Formal);
9128 end loop;
9130 -- If this derivation corresponds to a tagged generic actual, then
9131 -- primitive operations rename those of the actual. Otherwise the
9132 -- primitive operations rename those of the parent type, If the
9133 -- parent renames an intrinsic operator, so does the new subprogram.
9134 -- We except concatenation, which is always properly typed, and does
9135 -- not get expanded as other intrinsic operations.
9137 if No (Actual_Subp) then
9138 if Is_Intrinsic_Subprogram (Parent_Subp) then
9139 Set_Is_Intrinsic_Subprogram (New_Subp);
9141 if Present (Alias (Parent_Subp))
9142 and then Chars (Parent_Subp) /= Name_Op_Concat
9143 then
9144 Set_Alias (New_Subp, Alias (Parent_Subp));
9145 else
9146 Set_Alias (New_Subp, Parent_Subp);
9147 end if;
9149 else
9150 Set_Alias (New_Subp, Parent_Subp);
9151 end if;
9153 else
9154 Set_Alias (New_Subp, Actual_Subp);
9155 end if;
9157 -- Derived subprograms of a tagged type must inherit the convention
9158 -- of the parent subprogram (a requirement of AI-117). Derived
9159 -- subprograms of untagged types simply get convention Ada by default.
9161 if Is_Tagged_Type (Derived_Type) then
9162 Set_Convention (New_Subp, Convention (Parent_Subp));
9163 end if;
9165 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
9166 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
9168 if Ekind (Parent_Subp) = E_Procedure then
9169 Set_Is_Valued_Procedure
9170 (New_Subp, Is_Valued_Procedure (Parent_Subp));
9171 end if;
9173 -- A derived function with a controlling result is abstract.
9174 -- If the Derived_Type is a nonabstract formal generic derived
9175 -- type, then inherited operations are not abstract: check is
9176 -- done at instantiation time. If the derivation is for a generic
9177 -- actual, the function is not abstract unless the actual is.
9179 if Is_Generic_Type (Derived_Type)
9180 and then not Is_Abstract (Derived_Type)
9181 then
9182 null;
9184 elsif Is_Abstract (Alias (New_Subp))
9185 or else (Is_Tagged_Type (Derived_Type)
9186 and then Etype (New_Subp) = Derived_Type
9187 and then No (Actual_Subp))
9188 then
9189 Set_Is_Abstract (New_Subp);
9191 -- Finally, if the parent type is abstract we must verify that all
9192 -- inherited operations are either non-abstract or overridden, or
9193 -- that the derived type itself is abstract (this check is performed
9194 -- at the end of a package declaration, in Check_Abstract_Overriding).
9195 -- A private overriding in the parent type will not be visible in the
9196 -- derivation if we are not in an inner package or in a child unit of
9197 -- the parent type, in which case the abstractness of the inherited
9198 -- operation is carried to the new subprogram.
9200 elsif Is_Abstract (Parent_Type)
9201 and then not In_Open_Scopes (Scope (Parent_Type))
9202 and then Is_Private_Overriding
9203 and then Is_Abstract (Visible_Subp)
9204 then
9205 Set_Alias (New_Subp, Visible_Subp);
9206 Set_Is_Abstract (New_Subp);
9207 end if;
9209 New_Overloaded_Entity (New_Subp, Derived_Type);
9211 -- Check for case of a derived subprogram for the instantiation
9212 -- of a formal derived tagged type, if so mark the subprogram as
9213 -- dispatching and inherit the dispatching attributes of the
9214 -- parent subprogram. The derived subprogram is effectively a
9215 -- renaming of the actual subprogram, so it needs to have the
9216 -- same attributes as the actual.
9218 if Present (Actual_Subp)
9219 and then Is_Dispatching_Operation (Parent_Subp)
9220 then
9221 Set_Is_Dispatching_Operation (New_Subp);
9222 if Present (DTC_Entity (Parent_Subp)) then
9223 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
9224 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
9225 end if;
9226 end if;
9228 -- Indicate that a derived subprogram does not require a body
9229 -- and that it does not require processing of default expressions.
9231 Set_Has_Completion (New_Subp);
9232 Set_Default_Expressions_Processed (New_Subp);
9234 if Ekind (New_Subp) = E_Function then
9235 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
9236 end if;
9237 end Derive_Subprogram;
9239 ------------------------
9240 -- Derive_Subprograms --
9241 ------------------------
9243 procedure Derive_Subprograms
9244 (Parent_Type : Entity_Id;
9245 Derived_Type : Entity_Id;
9246 Generic_Actual : Entity_Id := Empty)
9248 Op_List : constant Elist_Id :=
9249 Collect_Primitive_Operations (Parent_Type);
9250 Act_List : Elist_Id;
9251 Act_Elmt : Elmt_Id;
9252 Elmt : Elmt_Id;
9253 Subp : Entity_Id;
9254 New_Subp : Entity_Id := Empty;
9255 Parent_Base : Entity_Id;
9257 begin
9258 if Ekind (Parent_Type) = E_Record_Type_With_Private
9259 and then Has_Discriminants (Parent_Type)
9260 and then Present (Full_View (Parent_Type))
9261 then
9262 Parent_Base := Full_View (Parent_Type);
9263 else
9264 Parent_Base := Parent_Type;
9265 end if;
9267 Elmt := First_Elmt (Op_List);
9269 if Present (Generic_Actual) then
9270 Act_List := Collect_Primitive_Operations (Generic_Actual);
9271 Act_Elmt := First_Elmt (Act_List);
9272 else
9273 Act_Elmt := No_Elmt;
9274 end if;
9276 -- Literals are derived earlier in the process of building the
9277 -- derived type, and are skipped here.
9279 while Present (Elmt) loop
9280 Subp := Node (Elmt);
9282 if Ekind (Subp) /= E_Enumeration_Literal then
9283 if No (Generic_Actual) then
9284 Derive_Subprogram
9285 (New_Subp, Subp, Derived_Type, Parent_Base);
9287 else
9288 Derive_Subprogram (New_Subp, Subp,
9289 Derived_Type, Parent_Base, Node (Act_Elmt));
9290 Next_Elmt (Act_Elmt);
9291 end if;
9292 end if;
9294 Next_Elmt (Elmt);
9295 end loop;
9296 end Derive_Subprograms;
9298 --------------------------------
9299 -- Derived_Standard_Character --
9300 --------------------------------
9302 procedure Derived_Standard_Character
9303 (N : Node_Id;
9304 Parent_Type : Entity_Id;
9305 Derived_Type : Entity_Id)
9307 Loc : constant Source_Ptr := Sloc (N);
9308 Def : constant Node_Id := Type_Definition (N);
9309 Indic : constant Node_Id := Subtype_Indication (Def);
9310 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9311 Implicit_Base : constant Entity_Id :=
9312 Create_Itype
9313 (E_Enumeration_Type, N, Derived_Type, 'B');
9315 Lo : Node_Id;
9316 Hi : Node_Id;
9318 begin
9319 Discard_Node (Process_Subtype (Indic, N));
9321 Set_Etype (Implicit_Base, Parent_Base);
9322 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
9323 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
9325 Set_Is_Character_Type (Implicit_Base, True);
9326 Set_Has_Delayed_Freeze (Implicit_Base);
9328 -- The bounds of the implicit base are the bounds of the parent base.
9329 -- Note that their type is the parent base.
9331 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
9332 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
9334 Set_Scalar_Range (Implicit_Base,
9335 Make_Range (Loc,
9336 Low_Bound => Lo,
9337 High_Bound => Hi));
9339 Conditional_Delay (Derived_Type, Parent_Type);
9341 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
9342 Set_Etype (Derived_Type, Implicit_Base);
9343 Set_Size_Info (Derived_Type, Parent_Type);
9345 if Unknown_RM_Size (Derived_Type) then
9346 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9347 end if;
9349 Set_Is_Character_Type (Derived_Type, True);
9351 if Nkind (Indic) /= N_Subtype_Indication then
9353 -- If no explicit constraint, the bounds are those
9354 -- of the parent type.
9356 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
9357 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
9358 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
9359 end if;
9361 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
9363 -- Because the implicit base is used in the conversion of the bounds,
9364 -- we have to freeze it now. This is similar to what is done for
9365 -- numeric types, and it equally suspicious, but otherwise a non-
9366 -- static bound will have a reference to an unfrozen type, which is
9367 -- rejected by Gigi (???).
9369 Freeze_Before (N, Implicit_Base);
9370 end Derived_Standard_Character;
9372 ------------------------------
9373 -- Derived_Type_Declaration --
9374 ------------------------------
9376 procedure Derived_Type_Declaration
9377 (T : Entity_Id;
9378 N : Node_Id;
9379 Is_Completion : Boolean)
9381 Def : constant Node_Id := Type_Definition (N);
9382 Indic : constant Node_Id := Subtype_Indication (Def);
9383 Extension : constant Node_Id := Record_Extension_Part (Def);
9384 Parent_Type : Entity_Id;
9385 Parent_Scope : Entity_Id;
9386 Taggd : Boolean;
9388 begin
9389 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
9391 if Parent_Type = Any_Type
9392 or else Etype (Parent_Type) = Any_Type
9393 or else (Is_Class_Wide_Type (Parent_Type)
9394 and then Etype (Parent_Type) = T)
9395 then
9396 -- If Parent_Type is undefined or illegal, make new type into
9397 -- a subtype of Any_Type, and set a few attributes to prevent
9398 -- cascaded errors. If this is a self-definition, emit error now.
9400 if T = Parent_Type
9401 or else T = Etype (Parent_Type)
9402 then
9403 Error_Msg_N ("type cannot be used in its own definition", Indic);
9404 end if;
9406 Set_Ekind (T, Ekind (Parent_Type));
9407 Set_Etype (T, Any_Type);
9408 Set_Scalar_Range (T, Scalar_Range (Any_Type));
9410 if Is_Tagged_Type (T) then
9411 Set_Primitive_Operations (T, New_Elmt_List);
9412 end if;
9414 return;
9416 elsif Is_Unchecked_Union (Parent_Type) then
9417 Error_Msg_N ("cannot derive from Unchecked_Union type", N);
9419 -- Ada 0Y (AI-231): Static check
9421 elsif Is_Access_Type (Parent_Type)
9422 and then Null_Exclusion_Present (Type_Definition (N))
9423 and then Can_Never_Be_Null (Parent_Type)
9424 then
9425 Error_Msg_N ("(Ada 0Y) null exclusion not allowed if parent is "
9426 & "already non-null", Type_Definition (N));
9427 end if;
9429 -- Only composite types other than array types are allowed to have
9430 -- discriminants.
9432 if Present (Discriminant_Specifications (N))
9433 and then (Is_Elementary_Type (Parent_Type)
9434 or else Is_Array_Type (Parent_Type))
9435 and then not Error_Posted (N)
9436 then
9437 Error_Msg_N
9438 ("elementary or array type cannot have discriminants",
9439 Defining_Identifier (First (Discriminant_Specifications (N))));
9440 Set_Has_Discriminants (T, False);
9441 end if;
9443 -- In Ada 83, a derived type defined in a package specification cannot
9444 -- be used for further derivation until the end of its visible part.
9445 -- Note that derivation in the private part of the package is allowed.
9447 if Ada_83
9448 and then Is_Derived_Type (Parent_Type)
9449 and then In_Visible_Part (Scope (Parent_Type))
9450 then
9451 if Ada_83 and then Comes_From_Source (Indic) then
9452 Error_Msg_N
9453 ("(Ada 83): premature use of type for derivation", Indic);
9454 end if;
9455 end if;
9457 -- Check for early use of incomplete or private type
9459 if Ekind (Parent_Type) = E_Void
9460 or else Ekind (Parent_Type) = E_Incomplete_Type
9461 then
9462 Error_Msg_N ("premature derivation of incomplete type", Indic);
9463 return;
9465 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
9466 and then not Is_Generic_Type (Parent_Type)
9467 and then not Is_Generic_Type (Root_Type (Parent_Type))
9468 and then not Is_Generic_Actual_Type (Parent_Type))
9469 or else Has_Private_Component (Parent_Type)
9470 then
9471 -- The ancestor type of a formal type can be incomplete, in which
9472 -- case only the operations of the partial view are available in
9473 -- the generic. Subsequent checks may be required when the full
9474 -- view is analyzed, to verify that derivation from a tagged type
9475 -- has an extension.
9477 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
9478 null;
9480 elsif No (Underlying_Type (Parent_Type))
9481 or else Has_Private_Component (Parent_Type)
9482 then
9483 Error_Msg_N
9484 ("premature derivation of derived or private type", Indic);
9486 -- Flag the type itself as being in error, this prevents some
9487 -- nasty problems with people looking at the malformed type.
9489 Set_Error_Posted (T);
9491 -- Check that within the immediate scope of an untagged partial
9492 -- view it's illegal to derive from the partial view if the
9493 -- full view is tagged. (7.3(7))
9495 -- We verify that the Parent_Type is a partial view by checking
9496 -- that it is not a Full_Type_Declaration (i.e. a private type or
9497 -- private extension declaration), to distinguish a partial view
9498 -- from a derivation from a private type which also appears as
9499 -- E_Private_Type.
9501 elsif Present (Full_View (Parent_Type))
9502 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
9503 and then not Is_Tagged_Type (Parent_Type)
9504 and then Is_Tagged_Type (Full_View (Parent_Type))
9505 then
9506 Parent_Scope := Scope (T);
9507 while Present (Parent_Scope)
9508 and then Parent_Scope /= Standard_Standard
9509 loop
9510 if Parent_Scope = Scope (Parent_Type) then
9511 Error_Msg_N
9512 ("premature derivation from type with tagged full view",
9513 Indic);
9514 end if;
9516 Parent_Scope := Scope (Parent_Scope);
9517 end loop;
9518 end if;
9519 end if;
9521 -- Check that form of derivation is appropriate
9523 Taggd := Is_Tagged_Type (Parent_Type);
9525 -- Perhaps the parent type should be changed to the class-wide type's
9526 -- specific type in this case to prevent cascading errors ???
9528 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
9529 Error_Msg_N ("parent type must not be a class-wide type", Indic);
9530 return;
9531 end if;
9533 if Present (Extension) and then not Taggd then
9534 Error_Msg_N
9535 ("type derived from untagged type cannot have extension", Indic);
9537 elsif No (Extension) and then Taggd then
9538 -- If this is within a private part (or body) of a generic
9539 -- instantiation then the derivation is allowed (the parent
9540 -- type can only appear tagged in this case if it's a generic
9541 -- actual type, since it would otherwise have been rejected
9542 -- in the analysis of the generic template).
9544 if not Is_Generic_Actual_Type (Parent_Type)
9545 or else In_Visible_Part (Scope (Parent_Type))
9546 then
9547 Error_Msg_N
9548 ("type derived from tagged type must have extension", Indic);
9549 end if;
9550 end if;
9552 Build_Derived_Type (N, Parent_Type, T, Is_Completion);
9553 end Derived_Type_Declaration;
9555 ----------------------------------
9556 -- Enumeration_Type_Declaration --
9557 ----------------------------------
9559 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
9560 Ev : Uint;
9561 L : Node_Id;
9562 R_Node : Node_Id;
9563 B_Node : Node_Id;
9565 begin
9566 -- Create identifier node representing lower bound
9568 B_Node := New_Node (N_Identifier, Sloc (Def));
9569 L := First (Literals (Def));
9570 Set_Chars (B_Node, Chars (L));
9571 Set_Entity (B_Node, L);
9572 Set_Etype (B_Node, T);
9573 Set_Is_Static_Expression (B_Node, True);
9575 R_Node := New_Node (N_Range, Sloc (Def));
9576 Set_Low_Bound (R_Node, B_Node);
9578 Set_Ekind (T, E_Enumeration_Type);
9579 Set_First_Literal (T, L);
9580 Set_Etype (T, T);
9581 Set_Is_Constrained (T);
9583 Ev := Uint_0;
9585 -- Loop through literals of enumeration type setting pos and rep values
9586 -- except that if the Ekind is already set, then it means that the
9587 -- literal was already constructed (case of a derived type declaration
9588 -- and we should not disturb the Pos and Rep values.
9590 while Present (L) loop
9591 if Ekind (L) /= E_Enumeration_Literal then
9592 Set_Ekind (L, E_Enumeration_Literal);
9593 Set_Enumeration_Pos (L, Ev);
9594 Set_Enumeration_Rep (L, Ev);
9595 Set_Is_Known_Valid (L, True);
9596 end if;
9598 Set_Etype (L, T);
9599 New_Overloaded_Entity (L);
9600 Generate_Definition (L);
9601 Set_Convention (L, Convention_Intrinsic);
9603 if Nkind (L) = N_Defining_Character_Literal then
9604 Set_Is_Character_Type (T, True);
9605 end if;
9607 Ev := Ev + 1;
9608 Next (L);
9609 end loop;
9611 -- Now create a node representing upper bound
9613 B_Node := New_Node (N_Identifier, Sloc (Def));
9614 Set_Chars (B_Node, Chars (Last (Literals (Def))));
9615 Set_Entity (B_Node, Last (Literals (Def)));
9616 Set_Etype (B_Node, T);
9617 Set_Is_Static_Expression (B_Node, True);
9619 Set_High_Bound (R_Node, B_Node);
9620 Set_Scalar_Range (T, R_Node);
9621 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
9622 Set_Enum_Esize (T);
9624 -- Set Discard_Names if configuration pragma set, or if there is
9625 -- a parameterless pragma in the current declarative region
9627 if Global_Discard_Names
9628 or else Discard_Names (Scope (T))
9629 then
9630 Set_Discard_Names (T);
9631 end if;
9633 -- Process end label if there is one
9635 if Present (Def) then
9636 Process_End_Label (Def, 'e', T);
9637 end if;
9638 end Enumeration_Type_Declaration;
9640 ---------------------------------
9641 -- Expand_To_Stored_Constraint --
9642 ---------------------------------
9644 function Expand_To_Stored_Constraint
9645 (Typ : Entity_Id;
9646 Constraint : Elist_Id) return Elist_Id
9648 Explicitly_Discriminated_Type : Entity_Id;
9649 Expansion : Elist_Id;
9650 Discriminant : Entity_Id;
9652 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
9653 -- Find the nearest type that actually specifies discriminants.
9655 ---------------------------------
9656 -- Type_With_Explicit_Discrims --
9657 ---------------------------------
9659 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
9660 Typ : constant E := Base_Type (Id);
9662 begin
9663 if Ekind (Typ) in Incomplete_Or_Private_Kind then
9664 if Present (Full_View (Typ)) then
9665 return Type_With_Explicit_Discrims (Full_View (Typ));
9666 end if;
9668 else
9669 if Has_Discriminants (Typ) then
9670 return Typ;
9671 end if;
9672 end if;
9674 if Etype (Typ) = Typ then
9675 return Empty;
9676 elsif Has_Discriminants (Typ) then
9677 return Typ;
9678 else
9679 return Type_With_Explicit_Discrims (Etype (Typ));
9680 end if;
9682 end Type_With_Explicit_Discrims;
9684 -- Start of processing for Expand_To_Stored_Constraint
9686 begin
9687 if No (Constraint)
9688 or else Is_Empty_Elmt_List (Constraint)
9689 then
9690 return No_Elist;
9691 end if;
9693 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
9695 if No (Explicitly_Discriminated_Type) then
9696 return No_Elist;
9697 end if;
9699 Expansion := New_Elmt_List;
9701 Discriminant :=
9702 First_Stored_Discriminant (Explicitly_Discriminated_Type);
9704 while Present (Discriminant) loop
9706 Append_Elmt (
9707 Get_Discriminant_Value (
9708 Discriminant, Explicitly_Discriminated_Type, Constraint),
9709 Expansion);
9711 Next_Stored_Discriminant (Discriminant);
9712 end loop;
9714 return Expansion;
9715 end Expand_To_Stored_Constraint;
9717 --------------------
9718 -- Find_Type_Name --
9719 --------------------
9721 function Find_Type_Name (N : Node_Id) return Entity_Id is
9722 Id : constant Entity_Id := Defining_Identifier (N);
9723 Prev : Entity_Id;
9724 New_Id : Entity_Id;
9725 Prev_Par : Node_Id;
9727 begin
9728 -- Find incomplete declaration, if some was given.
9730 Prev := Current_Entity_In_Scope (Id);
9732 if Present (Prev) then
9734 -- Previous declaration exists. Error if not incomplete/private case
9735 -- except if previous declaration is implicit, etc. Enter_Name will
9736 -- emit error if appropriate.
9738 Prev_Par := Parent (Prev);
9740 if not Is_Incomplete_Or_Private_Type (Prev) then
9741 Enter_Name (Id);
9742 New_Id := Id;
9744 elsif Nkind (N) /= N_Full_Type_Declaration
9745 and then Nkind (N) /= N_Task_Type_Declaration
9746 and then Nkind (N) /= N_Protected_Type_Declaration
9747 then
9748 -- Completion must be a full type declarations (RM 7.3(4))
9750 Error_Msg_Sloc := Sloc (Prev);
9751 Error_Msg_NE ("invalid completion of }", Id, Prev);
9753 -- Set scope of Id to avoid cascaded errors. Entity is never
9754 -- examined again, except when saving globals in generics.
9756 Set_Scope (Id, Current_Scope);
9757 New_Id := Id;
9759 -- Case of full declaration of incomplete type
9761 elsif Ekind (Prev) = E_Incomplete_Type then
9763 -- Indicate that the incomplete declaration has a matching
9764 -- full declaration. The defining occurrence of the incomplete
9765 -- declaration remains the visible one, and the procedure
9766 -- Get_Full_View dereferences it whenever the type is used.
9768 if Present (Full_View (Prev)) then
9769 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
9770 end if;
9772 Set_Full_View (Prev, Id);
9773 Append_Entity (Id, Current_Scope);
9774 Set_Is_Public (Id, Is_Public (Prev));
9775 Set_Is_Internal (Id);
9776 New_Id := Prev;
9778 -- Case of full declaration of private type
9780 else
9781 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
9782 if Etype (Prev) /= Prev then
9784 -- Prev is a private subtype or a derived type, and needs
9785 -- no completion.
9787 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
9788 New_Id := Id;
9790 elsif Ekind (Prev) = E_Private_Type
9791 and then
9792 (Nkind (N) = N_Task_Type_Declaration
9793 or else Nkind (N) = N_Protected_Type_Declaration)
9794 then
9795 Error_Msg_N
9796 ("completion of nonlimited type cannot be limited", N);
9797 end if;
9799 elsif Nkind (N) /= N_Full_Type_Declaration
9800 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
9801 then
9802 Error_Msg_N ("full view of private extension must be"
9803 & " an extension", N);
9805 elsif not (Abstract_Present (Parent (Prev)))
9806 and then Abstract_Present (Type_Definition (N))
9807 then
9808 Error_Msg_N ("full view of non-abstract extension cannot"
9809 & " be abstract", N);
9810 end if;
9812 if not In_Private_Part (Current_Scope) then
9813 Error_Msg_N
9814 ("declaration of full view must appear in private part", N);
9815 end if;
9817 Copy_And_Swap (Prev, Id);
9818 Set_Has_Private_Declaration (Prev);
9819 Set_Has_Private_Declaration (Id);
9821 -- If no error, propagate freeze_node from private to full view.
9822 -- It may have been generated for an early operational item.
9824 if Present (Freeze_Node (Id))
9825 and then Serious_Errors_Detected = 0
9826 and then No (Full_View (Id))
9827 then
9828 Set_Freeze_Node (Prev, Freeze_Node (Id));
9829 Set_Freeze_Node (Id, Empty);
9830 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
9831 end if;
9833 Set_Full_View (Id, Prev);
9834 New_Id := Prev;
9835 end if;
9837 -- Verify that full declaration conforms to incomplete one
9839 if Is_Incomplete_Or_Private_Type (Prev)
9840 and then Present (Discriminant_Specifications (Prev_Par))
9841 then
9842 if Present (Discriminant_Specifications (N)) then
9843 if Ekind (Prev) = E_Incomplete_Type then
9844 Check_Discriminant_Conformance (N, Prev, Prev);
9845 else
9846 Check_Discriminant_Conformance (N, Prev, Id);
9847 end if;
9849 else
9850 Error_Msg_N
9851 ("missing discriminants in full type declaration", N);
9853 -- To avoid cascaded errors on subsequent use, share the
9854 -- discriminants of the partial view.
9856 Set_Discriminant_Specifications (N,
9857 Discriminant_Specifications (Prev_Par));
9858 end if;
9859 end if;
9861 -- A prior untagged private type can have an associated
9862 -- class-wide type due to use of the class attribute,
9863 -- and in this case also the full type is required to
9864 -- be tagged.
9866 if Is_Type (Prev)
9867 and then (Is_Tagged_Type (Prev)
9868 or else Present (Class_Wide_Type (Prev)))
9869 then
9870 -- The full declaration is either a tagged record or an
9871 -- extension otherwise this is an error
9873 if Nkind (Type_Definition (N)) = N_Record_Definition then
9874 if not Tagged_Present (Type_Definition (N)) then
9875 Error_Msg_NE
9876 ("full declaration of } must be tagged", Prev, Id);
9877 Set_Is_Tagged_Type (Id);
9878 Set_Primitive_Operations (Id, New_Elmt_List);
9879 end if;
9881 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
9882 if No (Record_Extension_Part (Type_Definition (N))) then
9883 Error_Msg_NE (
9884 "full declaration of } must be a record extension",
9885 Prev, Id);
9886 Set_Is_Tagged_Type (Id);
9887 Set_Primitive_Operations (Id, New_Elmt_List);
9888 end if;
9890 else
9891 Error_Msg_NE
9892 ("full declaration of } must be a tagged type", Prev, Id);
9894 end if;
9895 end if;
9897 return New_Id;
9899 else
9900 -- New type declaration
9902 Enter_Name (Id);
9903 return Id;
9904 end if;
9905 end Find_Type_Name;
9907 -------------------------
9908 -- Find_Type_Of_Object --
9909 -------------------------
9911 function Find_Type_Of_Object
9912 (Obj_Def : Node_Id;
9913 Related_Nod : Node_Id) return Entity_Id
9915 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
9916 P : Node_Id := Parent (Obj_Def);
9917 T : Entity_Id;
9918 Nam : Name_Id;
9920 begin
9921 -- If the parent is a component_definition node we climb to the
9922 -- component_declaration node
9924 if Nkind (P) = N_Component_Definition then
9925 P := Parent (P);
9926 end if;
9928 -- Case of an anonymous array subtype
9930 if Def_Kind = N_Constrained_Array_Definition
9931 or else Def_Kind = N_Unconstrained_Array_Definition
9932 then
9933 T := Empty;
9934 Array_Type_Declaration (T, Obj_Def);
9936 -- Create an explicit subtype whenever possible.
9938 elsif Nkind (P) /= N_Component_Declaration
9939 and then Def_Kind = N_Subtype_Indication
9940 then
9941 -- Base name of subtype on object name, which will be unique in
9942 -- the current scope.
9944 -- If this is a duplicate declaration, return base type, to avoid
9945 -- generating duplicate anonymous types.
9947 if Error_Posted (P) then
9948 Analyze (Subtype_Mark (Obj_Def));
9949 return Entity (Subtype_Mark (Obj_Def));
9950 end if;
9952 Nam :=
9953 New_External_Name
9954 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
9956 T := Make_Defining_Identifier (Sloc (P), Nam);
9958 Insert_Action (Obj_Def,
9959 Make_Subtype_Declaration (Sloc (P),
9960 Defining_Identifier => T,
9961 Subtype_Indication => Relocate_Node (Obj_Def)));
9963 -- This subtype may need freezing, and this will not be done
9964 -- automatically if the object declaration is not in a
9965 -- declarative part. Since this is an object declaration, the
9966 -- type cannot always be frozen here. Deferred constants do not
9967 -- freeze their type (which often enough will be private).
9969 if Nkind (P) = N_Object_Declaration
9970 and then Constant_Present (P)
9971 and then No (Expression (P))
9972 then
9973 null;
9975 else
9976 Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
9977 end if;
9979 else
9980 T := Process_Subtype (Obj_Def, Related_Nod);
9981 end if;
9983 return T;
9984 end Find_Type_Of_Object;
9986 --------------------------------
9987 -- Find_Type_Of_Subtype_Indic --
9988 --------------------------------
9990 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
9991 Typ : Entity_Id;
9993 begin
9994 -- Case of subtype mark with a constraint
9996 if Nkind (S) = N_Subtype_Indication then
9997 Find_Type (Subtype_Mark (S));
9998 Typ := Entity (Subtype_Mark (S));
10000 if not
10001 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
10002 then
10003 Error_Msg_N
10004 ("incorrect constraint for this kind of type", Constraint (S));
10005 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
10006 end if;
10008 -- Otherwise we have a subtype mark without a constraint
10010 elsif Error_Posted (S) then
10011 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
10012 return Any_Type;
10014 else
10015 Find_Type (S);
10016 Typ := Entity (S);
10017 end if;
10019 if Typ = Standard_Wide_Character
10020 or else Typ = Standard_Wide_String
10021 then
10022 Check_Restriction (No_Wide_Characters, S);
10023 end if;
10025 return Typ;
10026 end Find_Type_Of_Subtype_Indic;
10028 -------------------------------------
10029 -- Floating_Point_Type_Declaration --
10030 -------------------------------------
10032 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
10033 Digs : constant Node_Id := Digits_Expression (Def);
10034 Digs_Val : Uint;
10035 Base_Typ : Entity_Id;
10036 Implicit_Base : Entity_Id;
10037 Bound : Node_Id;
10039 function Can_Derive_From (E : Entity_Id) return Boolean;
10040 -- Find if given digits value allows derivation from specified type
10042 ---------------------
10043 -- Can_Derive_From --
10044 ---------------------
10046 function Can_Derive_From (E : Entity_Id) return Boolean is
10047 Spec : constant Entity_Id := Real_Range_Specification (Def);
10049 begin
10050 if Digs_Val > Digits_Value (E) then
10051 return False;
10052 end if;
10054 if Present (Spec) then
10055 if Expr_Value_R (Type_Low_Bound (E)) >
10056 Expr_Value_R (Low_Bound (Spec))
10057 then
10058 return False;
10059 end if;
10061 if Expr_Value_R (Type_High_Bound (E)) <
10062 Expr_Value_R (High_Bound (Spec))
10063 then
10064 return False;
10065 end if;
10066 end if;
10068 return True;
10069 end Can_Derive_From;
10071 -- Start of processing for Floating_Point_Type_Declaration
10073 begin
10074 Check_Restriction (No_Floating_Point, Def);
10076 -- Create an implicit base type
10078 Implicit_Base :=
10079 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
10081 -- Analyze and verify digits value
10083 Analyze_And_Resolve (Digs, Any_Integer);
10084 Check_Digits_Expression (Digs);
10085 Digs_Val := Expr_Value (Digs);
10087 -- Process possible range spec and find correct type to derive from
10089 Process_Real_Range_Specification (Def);
10091 if Can_Derive_From (Standard_Short_Float) then
10092 Base_Typ := Standard_Short_Float;
10093 elsif Can_Derive_From (Standard_Float) then
10094 Base_Typ := Standard_Float;
10095 elsif Can_Derive_From (Standard_Long_Float) then
10096 Base_Typ := Standard_Long_Float;
10097 elsif Can_Derive_From (Standard_Long_Long_Float) then
10098 Base_Typ := Standard_Long_Long_Float;
10100 -- If we can't derive from any existing type, use long_long_float
10101 -- and give appropriate message explaining the problem.
10103 else
10104 Base_Typ := Standard_Long_Long_Float;
10106 if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
10107 Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
10108 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
10110 else
10111 Error_Msg_N
10112 ("range too large for any predefined type",
10113 Real_Range_Specification (Def));
10114 end if;
10115 end if;
10117 -- If there are bounds given in the declaration use them as the bounds
10118 -- of the type, otherwise use the bounds of the predefined base type
10119 -- that was chosen based on the Digits value.
10121 if Present (Real_Range_Specification (Def)) then
10122 Set_Scalar_Range (T, Real_Range_Specification (Def));
10123 Set_Is_Constrained (T);
10125 -- The bounds of this range must be converted to machine numbers
10126 -- in accordance with RM 4.9(38).
10128 Bound := Type_Low_Bound (T);
10130 if Nkind (Bound) = N_Real_Literal then
10131 Set_Realval
10132 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
10133 Set_Is_Machine_Number (Bound);
10134 end if;
10136 Bound := Type_High_Bound (T);
10138 if Nkind (Bound) = N_Real_Literal then
10139 Set_Realval
10140 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
10141 Set_Is_Machine_Number (Bound);
10142 end if;
10144 else
10145 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
10146 end if;
10148 -- Complete definition of implicit base and declared first subtype
10150 Set_Etype (Implicit_Base, Base_Typ);
10152 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
10153 Set_Size_Info (Implicit_Base, (Base_Typ));
10154 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
10155 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
10156 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
10157 Set_Vax_Float (Implicit_Base, Vax_Float (Base_Typ));
10159 Set_Ekind (T, E_Floating_Point_Subtype);
10160 Set_Etype (T, Implicit_Base);
10162 Set_Size_Info (T, (Implicit_Base));
10163 Set_RM_Size (T, RM_Size (Implicit_Base));
10164 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
10165 Set_Digits_Value (T, Digs_Val);
10167 end Floating_Point_Type_Declaration;
10169 ----------------------------
10170 -- Get_Discriminant_Value --
10171 ----------------------------
10173 -- This is the situation...
10175 -- There is a non-derived type
10177 -- type T0 (Dx, Dy, Dz...)
10179 -- There are zero or more levels of derivation, with each
10180 -- derivation either purely inheriting the discriminants, or
10181 -- defining its own.
10183 -- type Ti is new Ti-1
10184 -- or
10185 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
10186 -- or
10187 -- subtype Ti is ...
10189 -- The subtype issue is avoided by the use of
10190 -- Original_Record_Component, and the fact that derived subtypes
10191 -- also derive the constraints.
10193 -- This chain leads back from
10195 -- Typ_For_Constraint
10197 -- Typ_For_Constraint has discriminants, and the value for each
10198 -- discriminant is given by its corresponding Elmt of Constraints.
10200 -- Discriminant is some discriminant in this hierarchy.
10202 -- We need to return its value.
10204 -- We do this by recursively searching each level, and looking for
10205 -- Discriminant. Once we get to the bottom, we start backing up
10206 -- returning the value for it which may in turn be a discriminant
10207 -- further up, so on the backup we continue the substitution.
10209 function Get_Discriminant_Value
10210 (Discriminant : Entity_Id;
10211 Typ_For_Constraint : Entity_Id;
10212 Constraint : Elist_Id) return Node_Id
10214 function Search_Derivation_Levels
10215 (Ti : Entity_Id;
10216 Discrim_Values : Elist_Id;
10217 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
10218 -- This is the routine that performs the recursive search of levels
10219 -- as described above.
10221 ------------------------------
10222 -- Search_Derivation_Levels --
10223 ------------------------------
10225 function Search_Derivation_Levels
10226 (Ti : Entity_Id;
10227 Discrim_Values : Elist_Id;
10228 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
10230 Assoc : Elmt_Id;
10231 Disc : Entity_Id;
10232 Result : Node_Or_Entity_Id;
10233 Result_Entity : Node_Id;
10235 begin
10236 -- If inappropriate type, return Error, this happens only in
10237 -- cascaded error situations, and we want to avoid a blow up.
10239 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
10240 return Error;
10241 end if;
10243 -- Look deeper if possible. Use Stored_Constraints only for
10244 -- untagged types. For tagged types use the given constraint.
10245 -- This asymmetry needs explanation???
10247 if not Stored_Discrim_Values
10248 and then Present (Stored_Constraint (Ti))
10249 and then not Is_Tagged_Type (Ti)
10250 then
10251 Result :=
10252 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
10253 else
10254 declare
10255 Td : constant Entity_Id := Etype (Ti);
10257 begin
10258 if Td = Ti then
10259 Result := Discriminant;
10261 else
10262 if Present (Stored_Constraint (Ti)) then
10263 Result :=
10264 Search_Derivation_Levels
10265 (Td, Stored_Constraint (Ti), True);
10266 else
10267 Result :=
10268 Search_Derivation_Levels
10269 (Td, Discrim_Values, Stored_Discrim_Values);
10270 end if;
10271 end if;
10272 end;
10273 end if;
10275 -- Extra underlying places to search, if not found above. For
10276 -- concurrent types, the relevant discriminant appears in the
10277 -- corresponding record. For a type derived from a private type
10278 -- without discriminant, the full view inherits the discriminants
10279 -- of the full view of the parent.
10281 if Result = Discriminant then
10282 if Is_Concurrent_Type (Ti)
10283 and then Present (Corresponding_Record_Type (Ti))
10284 then
10285 Result :=
10286 Search_Derivation_Levels (
10287 Corresponding_Record_Type (Ti),
10288 Discrim_Values,
10289 Stored_Discrim_Values);
10291 elsif Is_Private_Type (Ti)
10292 and then not Has_Discriminants (Ti)
10293 and then Present (Full_View (Ti))
10294 and then Etype (Full_View (Ti)) /= Ti
10295 then
10296 Result :=
10297 Search_Derivation_Levels (
10298 Full_View (Ti),
10299 Discrim_Values,
10300 Stored_Discrim_Values);
10301 end if;
10302 end if;
10304 -- If Result is not a (reference to a) discriminant,
10305 -- return it, otherwise set Result_Entity to the discriminant.
10307 if Nkind (Result) = N_Defining_Identifier then
10309 pragma Assert (Result = Discriminant);
10311 Result_Entity := Result;
10313 else
10314 if not Denotes_Discriminant (Result) then
10315 return Result;
10316 end if;
10318 Result_Entity := Entity (Result);
10319 end if;
10321 -- See if this level of derivation actually has discriminants
10322 -- because tagged derivations can add them, hence the lower
10323 -- levels need not have any.
10325 if not Has_Discriminants (Ti) then
10326 return Result;
10327 end if;
10329 -- Scan Ti's discriminants for Result_Entity,
10330 -- and return its corresponding value, if any.
10332 Result_Entity := Original_Record_Component (Result_Entity);
10334 Assoc := First_Elmt (Discrim_Values);
10336 if Stored_Discrim_Values then
10337 Disc := First_Stored_Discriminant (Ti);
10338 else
10339 Disc := First_Discriminant (Ti);
10340 end if;
10342 while Present (Disc) loop
10344 pragma Assert (Present (Assoc));
10346 if Original_Record_Component (Disc) = Result_Entity then
10347 return Node (Assoc);
10348 end if;
10350 Next_Elmt (Assoc);
10352 if Stored_Discrim_Values then
10353 Next_Stored_Discriminant (Disc);
10354 else
10355 Next_Discriminant (Disc);
10356 end if;
10357 end loop;
10359 -- Could not find it
10361 return Result;
10362 end Search_Derivation_Levels;
10364 Result : Node_Or_Entity_Id;
10366 -- Start of processing for Get_Discriminant_Value
10368 begin
10369 -- ??? this routine is a gigantic mess and will be deleted.
10370 -- for the time being just test for the trivial case before calling
10371 -- recurse.
10373 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
10374 declare
10375 D : Entity_Id := First_Discriminant (Typ_For_Constraint);
10376 E : Elmt_Id := First_Elmt (Constraint);
10377 begin
10378 while Present (D) loop
10379 if Chars (D) = Chars (Discriminant) then
10380 return Node (E);
10381 end if;
10383 Next_Discriminant (D);
10384 Next_Elmt (E);
10385 end loop;
10386 end;
10387 end if;
10389 Result := Search_Derivation_Levels
10390 (Typ_For_Constraint, Constraint, False);
10392 -- ??? hack to disappear when this routine is gone
10394 if Nkind (Result) = N_Defining_Identifier then
10395 declare
10396 D : Entity_Id := First_Discriminant (Typ_For_Constraint);
10397 E : Elmt_Id := First_Elmt (Constraint);
10399 begin
10400 while Present (D) loop
10401 if Corresponding_Discriminant (D) = Discriminant then
10402 return Node (E);
10403 end if;
10405 Next_Discriminant (D);
10406 Next_Elmt (E);
10407 end loop;
10408 end;
10409 end if;
10411 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
10412 return Result;
10413 end Get_Discriminant_Value;
10415 --------------------------
10416 -- Has_Range_Constraint --
10417 --------------------------
10419 function Has_Range_Constraint (N : Node_Id) return Boolean is
10420 C : constant Node_Id := Constraint (N);
10422 begin
10423 if Nkind (C) = N_Range_Constraint then
10424 return True;
10426 elsif Nkind (C) = N_Digits_Constraint then
10427 return
10428 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
10429 or else
10430 Present (Range_Constraint (C));
10432 elsif Nkind (C) = N_Delta_Constraint then
10433 return Present (Range_Constraint (C));
10435 else
10436 return False;
10437 end if;
10438 end Has_Range_Constraint;
10440 ------------------------
10441 -- Inherit_Components --
10442 ------------------------
10444 function Inherit_Components
10445 (N : Node_Id;
10446 Parent_Base : Entity_Id;
10447 Derived_Base : Entity_Id;
10448 Is_Tagged : Boolean;
10449 Inherit_Discr : Boolean;
10450 Discs : Elist_Id) return Elist_Id
10452 Assoc_List : constant Elist_Id := New_Elmt_List;
10454 procedure Inherit_Component
10455 (Old_C : Entity_Id;
10456 Plain_Discrim : Boolean := False;
10457 Stored_Discrim : Boolean := False);
10458 -- Inherits component Old_C from Parent_Base to the Derived_Base.
10459 -- If Plain_Discrim is True, Old_C is a discriminant.
10460 -- If Stored_Discrim is True, Old_C is a stored discriminant.
10461 -- If they are both false then Old_C is a regular component.
10463 -----------------------
10464 -- Inherit_Component --
10465 -----------------------
10467 procedure Inherit_Component
10468 (Old_C : Entity_Id;
10469 Plain_Discrim : Boolean := False;
10470 Stored_Discrim : Boolean := False)
10472 New_C : constant Entity_Id := New_Copy (Old_C);
10474 Discrim : Entity_Id;
10475 Corr_Discrim : Entity_Id;
10477 begin
10478 pragma Assert (not Is_Tagged or else not Stored_Discrim);
10480 Set_Parent (New_C, Parent (Old_C));
10482 -- Regular discriminants and components must be inserted
10483 -- in the scope of the Derived_Base. Do it here.
10485 if not Stored_Discrim then
10486 Enter_Name (New_C);
10487 end if;
10489 -- For tagged types the Original_Record_Component must point to
10490 -- whatever this field was pointing to in the parent type. This has
10491 -- already been achieved by the call to New_Copy above.
10493 if not Is_Tagged then
10494 Set_Original_Record_Component (New_C, New_C);
10495 end if;
10497 -- If we have inherited a component then see if its Etype contains
10498 -- references to Parent_Base discriminants. In this case, replace
10499 -- these references with the constraints given in Discs. We do not
10500 -- do this for the partial view of private types because this is
10501 -- not needed (only the components of the full view will be used
10502 -- for code generation) and cause problem. We also avoid this
10503 -- transformation in some error situations.
10505 if Ekind (New_C) = E_Component then
10506 if (Is_Private_Type (Derived_Base)
10507 and then not Is_Generic_Type (Derived_Base))
10508 or else (Is_Empty_Elmt_List (Discs)
10509 and then not Expander_Active)
10510 then
10511 Set_Etype (New_C, Etype (Old_C));
10512 else
10513 Set_Etype (New_C, Constrain_Component_Type (Etype (Old_C),
10514 Derived_Base, N, Parent_Base, Discs));
10515 end if;
10516 end if;
10518 -- In derived tagged types it is illegal to reference a non
10519 -- discriminant component in the parent type. To catch this, mark
10520 -- these components with an Ekind of E_Void. This will be reset in
10521 -- Record_Type_Definition after processing the record extension of
10522 -- the derived type.
10524 if Is_Tagged and then Ekind (New_C) = E_Component then
10525 Set_Ekind (New_C, E_Void);
10526 end if;
10528 if Plain_Discrim then
10529 Set_Corresponding_Discriminant (New_C, Old_C);
10530 Build_Discriminal (New_C);
10532 -- If we are explicitly inheriting a stored discriminant it will be
10533 -- completely hidden.
10535 elsif Stored_Discrim then
10536 Set_Corresponding_Discriminant (New_C, Empty);
10537 Set_Discriminal (New_C, Empty);
10538 Set_Is_Completely_Hidden (New_C);
10540 -- Set the Original_Record_Component of each discriminant in the
10541 -- derived base to point to the corresponding stored that we just
10542 -- created.
10544 Discrim := First_Discriminant (Derived_Base);
10545 while Present (Discrim) loop
10546 Corr_Discrim := Corresponding_Discriminant (Discrim);
10548 -- Corr_Discrimm could be missing in an error situation.
10550 if Present (Corr_Discrim)
10551 and then Original_Record_Component (Corr_Discrim) = Old_C
10552 then
10553 Set_Original_Record_Component (Discrim, New_C);
10554 end if;
10556 Next_Discriminant (Discrim);
10557 end loop;
10559 Append_Entity (New_C, Derived_Base);
10560 end if;
10562 if not Is_Tagged then
10563 Append_Elmt (Old_C, Assoc_List);
10564 Append_Elmt (New_C, Assoc_List);
10565 end if;
10566 end Inherit_Component;
10568 -- Variables local to Inherit_Components.
10570 Loc : constant Source_Ptr := Sloc (N);
10572 Parent_Discrim : Entity_Id;
10573 Stored_Discrim : Entity_Id;
10574 D : Entity_Id;
10576 Component : Entity_Id;
10578 -- Start of processing for Inherit_Components
10580 begin
10581 if not Is_Tagged then
10582 Append_Elmt (Parent_Base, Assoc_List);
10583 Append_Elmt (Derived_Base, Assoc_List);
10584 end if;
10586 -- Inherit parent discriminants if needed.
10588 if Inherit_Discr then
10589 Parent_Discrim := First_Discriminant (Parent_Base);
10590 while Present (Parent_Discrim) loop
10591 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
10592 Next_Discriminant (Parent_Discrim);
10593 end loop;
10594 end if;
10596 -- Create explicit stored discrims for untagged types when necessary.
10598 if not Has_Unknown_Discriminants (Derived_Base)
10599 and then Has_Discriminants (Parent_Base)
10600 and then not Is_Tagged
10601 and then
10602 (not Inherit_Discr
10603 or else First_Discriminant (Parent_Base) /=
10604 First_Stored_Discriminant (Parent_Base))
10605 then
10606 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
10607 while Present (Stored_Discrim) loop
10608 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
10609 Next_Stored_Discriminant (Stored_Discrim);
10610 end loop;
10611 end if;
10613 -- See if we can apply the second transformation for derived types, as
10614 -- explained in point 6. in the comments above Build_Derived_Record_Type
10615 -- This is achieved by appending Derived_Base discriminants into
10616 -- Discs, which has the side effect of returning a non empty Discs
10617 -- list to the caller of Inherit_Components, which is what we want.
10618 -- This must be done for private derived types if there are explicit
10619 -- stored discriminants, to ensure that we can retrieve the values of
10620 -- the constraints provided in the ancestors.
10622 if Inherit_Discr
10623 and then Is_Empty_Elmt_List (Discs)
10624 and then Present (First_Discriminant (Derived_Base))
10625 and then
10626 (not Is_Private_Type (Derived_Base)
10627 or else Is_Completely_Hidden
10628 (First_Stored_Discriminant (Derived_Base))
10629 or else Is_Generic_Type (Derived_Base))
10630 then
10631 D := First_Discriminant (Derived_Base);
10632 while Present (D) loop
10633 Append_Elmt (New_Reference_To (D, Loc), Discs);
10634 Next_Discriminant (D);
10635 end loop;
10636 end if;
10638 -- Finally, inherit non-discriminant components unless they are not
10639 -- visible because defined or inherited from the full view of the
10640 -- parent. Don't inherit the _parent field of the parent type.
10642 Component := First_Entity (Parent_Base);
10643 while Present (Component) loop
10644 if Ekind (Component) /= E_Component
10645 or else Chars (Component) = Name_uParent
10646 then
10647 null;
10649 -- If the derived type is within the parent type's declarative
10650 -- region, then the components can still be inherited even though
10651 -- they aren't visible at this point. This can occur for cases
10652 -- such as within public child units where the components must
10653 -- become visible upon entering the child unit's private part.
10655 elsif not Is_Visible_Component (Component)
10656 and then not In_Open_Scopes (Scope (Parent_Base))
10657 then
10658 null;
10660 elsif Ekind (Derived_Base) = E_Private_Type
10661 or else Ekind (Derived_Base) = E_Limited_Private_Type
10662 then
10663 null;
10665 else
10666 Inherit_Component (Component);
10667 end if;
10669 Next_Entity (Component);
10670 end loop;
10672 -- For tagged derived types, inherited discriminants cannot be used in
10673 -- component declarations of the record extension part. To achieve this
10674 -- we mark the inherited discriminants as not visible.
10676 if Is_Tagged and then Inherit_Discr then
10677 D := First_Discriminant (Derived_Base);
10678 while Present (D) loop
10679 Set_Is_Immediately_Visible (D, False);
10680 Next_Discriminant (D);
10681 end loop;
10682 end if;
10684 return Assoc_List;
10685 end Inherit_Components;
10687 ------------------------------
10688 -- Is_Valid_Constraint_Kind --
10689 ------------------------------
10691 function Is_Valid_Constraint_Kind
10692 (T_Kind : Type_Kind;
10693 Constraint_Kind : Node_Kind) return Boolean
10695 begin
10696 case T_Kind is
10698 when Enumeration_Kind |
10699 Integer_Kind =>
10700 return Constraint_Kind = N_Range_Constraint;
10702 when Decimal_Fixed_Point_Kind =>
10703 return
10704 Constraint_Kind = N_Digits_Constraint
10705 or else
10706 Constraint_Kind = N_Range_Constraint;
10708 when Ordinary_Fixed_Point_Kind =>
10709 return
10710 Constraint_Kind = N_Delta_Constraint
10711 or else
10712 Constraint_Kind = N_Range_Constraint;
10714 when Float_Kind =>
10715 return
10716 Constraint_Kind = N_Digits_Constraint
10717 or else
10718 Constraint_Kind = N_Range_Constraint;
10720 when Access_Kind |
10721 Array_Kind |
10722 E_Record_Type |
10723 E_Record_Subtype |
10724 Class_Wide_Kind |
10725 E_Incomplete_Type |
10726 Private_Kind |
10727 Concurrent_Kind =>
10728 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
10730 when others =>
10731 return True; -- Error will be detected later.
10732 end case;
10734 end Is_Valid_Constraint_Kind;
10736 --------------------------
10737 -- Is_Visible_Component --
10738 --------------------------
10740 function Is_Visible_Component (C : Entity_Id) return Boolean is
10741 Original_Comp : Entity_Id := Empty;
10742 Original_Scope : Entity_Id;
10743 Type_Scope : Entity_Id;
10745 function Is_Local_Type (Typ : Entity_Id) return Boolean;
10746 -- Check whether parent type of inherited component is declared
10747 -- locally, possibly within a nested package or instance. The
10748 -- current scope is the derived record itself.
10750 -------------------
10751 -- Is_Local_Type --
10752 -------------------
10754 function Is_Local_Type (Typ : Entity_Id) return Boolean is
10755 Scop : Entity_Id := Scope (Typ);
10757 begin
10758 while Present (Scop)
10759 and then Scop /= Standard_Standard
10760 loop
10761 if Scop = Scope (Current_Scope) then
10762 return True;
10763 end if;
10765 Scop := Scope (Scop);
10766 end loop;
10767 return False;
10768 end Is_Local_Type;
10770 -- Start of processing for Is_Visible_Component
10772 begin
10773 if Ekind (C) = E_Component
10774 or else Ekind (C) = E_Discriminant
10775 then
10776 Original_Comp := Original_Record_Component (C);
10777 end if;
10779 if No (Original_Comp) then
10781 -- Premature usage, or previous error
10783 return False;
10785 else
10786 Original_Scope := Scope (Original_Comp);
10787 Type_Scope := Scope (Base_Type (Scope (C)));
10788 end if;
10790 -- This test only concerns tagged types
10792 if not Is_Tagged_Type (Original_Scope) then
10793 return True;
10795 -- If it is _Parent or _Tag, there is no visibility issue
10797 elsif not Comes_From_Source (Original_Comp) then
10798 return True;
10800 -- If we are in the body of an instantiation, the component is
10801 -- visible even when the parent type (possibly defined in an
10802 -- enclosing unit or in a parent unit) might not.
10804 elsif In_Instance_Body then
10805 return True;
10807 -- Discriminants are always visible.
10809 elsif Ekind (Original_Comp) = E_Discriminant
10810 and then not Has_Unknown_Discriminants (Original_Scope)
10811 then
10812 return True;
10814 -- If the component has been declared in an ancestor which is
10815 -- currently a private type, then it is not visible. The same
10816 -- applies if the component's containing type is not in an
10817 -- open scope and the original component's enclosing type
10818 -- is a visible full type of a private type (which can occur
10819 -- in cases where an attempt is being made to reference a
10820 -- component in a sibling package that is inherited from a
10821 -- visible component of a type in an ancestor package; the
10822 -- component in the sibling package should not be visible
10823 -- even though the component it inherited from is visible).
10824 -- This does not apply however in the case where the scope
10825 -- of the type is a private child unit, or when the parent
10826 -- comes from a local package in which the ancestor is
10827 -- currently visible. The latter suppression of visibility
10828 -- is needed for cases that are tested in B730006.
10830 elsif Is_Private_Type (Original_Scope)
10831 or else
10832 (not Is_Private_Descendant (Type_Scope)
10833 and then not In_Open_Scopes (Type_Scope)
10834 and then Has_Private_Declaration (Original_Scope))
10835 then
10836 -- If the type derives from an entity in a formal package, there
10837 -- are no additional visible components.
10839 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
10840 N_Formal_Package_Declaration
10841 then
10842 return False;
10844 -- if we are not in the private part of the current package, there
10845 -- are no additional visible components.
10847 elsif Ekind (Scope (Current_Scope)) = E_Package
10848 and then not In_Private_Part (Scope (Current_Scope))
10849 then
10850 return False;
10851 else
10852 return
10853 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
10854 and then Is_Local_Type (Type_Scope);
10855 end if;
10857 -- There is another weird way in which a component may be invisible
10858 -- when the private and the full view are not derived from the same
10859 -- ancestor. Here is an example :
10861 -- type A1 is tagged record F1 : integer; end record;
10862 -- type A2 is new A1 with record F2 : integer; end record;
10863 -- type T is new A1 with private;
10864 -- private
10865 -- type T is new A2 with null record;
10867 -- In this case, the full view of T inherits F1 and F2 but the
10868 -- private view inherits only F1
10870 else
10871 declare
10872 Ancestor : Entity_Id := Scope (C);
10874 begin
10875 loop
10876 if Ancestor = Original_Scope then
10877 return True;
10878 elsif Ancestor = Etype (Ancestor) then
10879 return False;
10880 end if;
10882 Ancestor := Etype (Ancestor);
10883 end loop;
10885 return True;
10886 end;
10887 end if;
10888 end Is_Visible_Component;
10890 --------------------------
10891 -- Make_Class_Wide_Type --
10892 --------------------------
10894 procedure Make_Class_Wide_Type (T : Entity_Id) is
10895 CW_Type : Entity_Id;
10896 CW_Name : Name_Id;
10897 Next_E : Entity_Id;
10899 begin
10900 -- The class wide type can have been defined by the partial view in
10901 -- which case everything is already done
10903 if Present (Class_Wide_Type (T)) then
10904 return;
10905 end if;
10907 CW_Type :=
10908 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
10910 -- Inherit root type characteristics
10912 CW_Name := Chars (CW_Type);
10913 Next_E := Next_Entity (CW_Type);
10914 Copy_Node (T, CW_Type);
10915 Set_Comes_From_Source (CW_Type, False);
10916 Set_Chars (CW_Type, CW_Name);
10917 Set_Parent (CW_Type, Parent (T));
10918 Set_Next_Entity (CW_Type, Next_E);
10919 Set_Has_Delayed_Freeze (CW_Type);
10921 -- Customize the class-wide type: It has no prim. op., it cannot be
10922 -- abstract and its Etype points back to the specific root type.
10924 Set_Ekind (CW_Type, E_Class_Wide_Type);
10925 Set_Is_Tagged_Type (CW_Type, True);
10926 Set_Primitive_Operations (CW_Type, New_Elmt_List);
10927 Set_Is_Abstract (CW_Type, False);
10928 Set_Is_Constrained (CW_Type, False);
10929 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
10930 Init_Size_Align (CW_Type);
10932 if Ekind (T) = E_Class_Wide_Subtype then
10933 Set_Etype (CW_Type, Etype (Base_Type (T)));
10934 else
10935 Set_Etype (CW_Type, T);
10936 end if;
10938 -- If this is the class_wide type of a constrained subtype, it does
10939 -- not have discriminants.
10941 Set_Has_Discriminants (CW_Type,
10942 Has_Discriminants (T) and then not Is_Constrained (T));
10944 Set_Has_Unknown_Discriminants (CW_Type, True);
10945 Set_Class_Wide_Type (T, CW_Type);
10946 Set_Equivalent_Type (CW_Type, Empty);
10948 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
10950 Set_Class_Wide_Type (CW_Type, CW_Type);
10952 end Make_Class_Wide_Type;
10954 ----------------
10955 -- Make_Index --
10956 ----------------
10958 procedure Make_Index
10959 (I : Node_Id;
10960 Related_Nod : Node_Id;
10961 Related_Id : Entity_Id := Empty;
10962 Suffix_Index : Nat := 1)
10964 R : Node_Id;
10965 T : Entity_Id;
10966 Def_Id : Entity_Id := Empty;
10967 Found : Boolean := False;
10969 begin
10970 -- For a discrete range used in a constrained array definition and
10971 -- defined by a range, an implicit conversion to the predefined type
10972 -- INTEGER is assumed if each bound is either a numeric literal, a named
10973 -- number, or an attribute, and the type of both bounds (prior to the
10974 -- implicit conversion) is the type universal_integer. Otherwise, both
10975 -- bounds must be of the same discrete type, other than universal
10976 -- integer; this type must be determinable independently of the
10977 -- context, but using the fact that the type must be discrete and that
10978 -- both bounds must have the same type.
10980 -- Character literals also have a universal type in the absence of
10981 -- of additional context, and are resolved to Standard_Character.
10983 if Nkind (I) = N_Range then
10985 -- The index is given by a range constraint. The bounds are known
10986 -- to be of a consistent type.
10988 if not Is_Overloaded (I) then
10989 T := Etype (I);
10991 -- If the bounds are universal, choose the specific predefined
10992 -- type.
10994 if T = Universal_Integer then
10995 T := Standard_Integer;
10997 elsif T = Any_Character then
10999 if not Ada_83 then
11000 Error_Msg_N
11001 ("ambiguous character literals (could be Wide_Character)",
11003 end if;
11005 T := Standard_Character;
11006 end if;
11008 else
11009 T := Any_Type;
11011 declare
11012 Ind : Interp_Index;
11013 It : Interp;
11015 begin
11016 Get_First_Interp (I, Ind, It);
11018 while Present (It.Typ) loop
11019 if Is_Discrete_Type (It.Typ) then
11021 if Found
11022 and then not Covers (It.Typ, T)
11023 and then not Covers (T, It.Typ)
11024 then
11025 Error_Msg_N ("ambiguous bounds in discrete range", I);
11026 exit;
11027 else
11028 T := It.Typ;
11029 Found := True;
11030 end if;
11031 end if;
11033 Get_Next_Interp (Ind, It);
11034 end loop;
11036 if T = Any_Type then
11037 Error_Msg_N ("discrete type required for range", I);
11038 Set_Etype (I, Any_Type);
11039 return;
11041 elsif T = Universal_Integer then
11042 T := Standard_Integer;
11043 end if;
11044 end;
11045 end if;
11047 if not Is_Discrete_Type (T) then
11048 Error_Msg_N ("discrete type required for range", I);
11049 Set_Etype (I, Any_Type);
11050 return;
11051 end if;
11053 if Nkind (Low_Bound (I)) = N_Attribute_Reference
11054 and then Attribute_Name (Low_Bound (I)) = Name_First
11055 and then Is_Entity_Name (Prefix (Low_Bound (I)))
11056 and then Is_Type (Entity (Prefix (Low_Bound (I))))
11057 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
11058 then
11059 -- The type of the index will be the type of the prefix,
11060 -- as long as the upper bound is 'Last of the same type.
11062 Def_Id := Entity (Prefix (Low_Bound (I)));
11064 if Nkind (High_Bound (I)) /= N_Attribute_Reference
11065 or else Attribute_Name (High_Bound (I)) /= Name_Last
11066 or else not Is_Entity_Name (Prefix (High_Bound (I)))
11067 or else Entity (Prefix (High_Bound (I))) /= Def_Id
11068 then
11069 Def_Id := Empty;
11070 end if;
11071 end if;
11073 R := I;
11074 Process_Range_Expr_In_Decl (R, T);
11076 elsif Nkind (I) = N_Subtype_Indication then
11078 -- The index is given by a subtype with a range constraint.
11080 T := Base_Type (Entity (Subtype_Mark (I)));
11082 if not Is_Discrete_Type (T) then
11083 Error_Msg_N ("discrete type required for range", I);
11084 Set_Etype (I, Any_Type);
11085 return;
11086 end if;
11088 R := Range_Expression (Constraint (I));
11090 Resolve (R, T);
11091 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
11093 elsif Nkind (I) = N_Attribute_Reference then
11095 -- The parser guarantees that the attribute is a RANGE attribute
11097 -- If the node denotes the range of a type mark, that is also the
11098 -- resulting type, and we do no need to create an Itype for it.
11100 if Is_Entity_Name (Prefix (I))
11101 and then Comes_From_Source (I)
11102 and then Is_Type (Entity (Prefix (I)))
11103 and then Is_Discrete_Type (Entity (Prefix (I)))
11104 then
11105 Def_Id := Entity (Prefix (I));
11106 end if;
11108 Analyze_And_Resolve (I);
11109 T := Etype (I);
11110 R := I;
11112 -- If none of the above, must be a subtype. We convert this to a
11113 -- range attribute reference because in the case of declared first
11114 -- named subtypes, the types in the range reference can be different
11115 -- from the type of the entity. A range attribute normalizes the
11116 -- reference and obtains the correct types for the bounds.
11118 -- This transformation is in the nature of an expansion, is only
11119 -- done if expansion is active. In particular, it is not done on
11120 -- formal generic types, because we need to retain the name of the
11121 -- original index for instantiation purposes.
11123 else
11124 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
11125 Error_Msg_N ("invalid subtype mark in discrete range ", I);
11126 Set_Etype (I, Any_Integer);
11127 return;
11128 else
11129 -- The type mark may be that of an incomplete type. It is only
11130 -- now that we can get the full view, previous analysis does
11131 -- not look specifically for a type mark.
11133 Set_Entity (I, Get_Full_View (Entity (I)));
11134 Set_Etype (I, Entity (I));
11135 Def_Id := Entity (I);
11137 if not Is_Discrete_Type (Def_Id) then
11138 Error_Msg_N ("discrete type required for index", I);
11139 Set_Etype (I, Any_Type);
11140 return;
11141 end if;
11142 end if;
11144 if Expander_Active then
11145 Rewrite (I,
11146 Make_Attribute_Reference (Sloc (I),
11147 Attribute_Name => Name_Range,
11148 Prefix => Relocate_Node (I)));
11150 -- The original was a subtype mark that does not freeze. This
11151 -- means that the rewritten version must not freeze either.
11153 Set_Must_Not_Freeze (I);
11154 Set_Must_Not_Freeze (Prefix (I));
11156 -- Is order critical??? if so, document why, if not
11157 -- use Analyze_And_Resolve
11159 Analyze (I);
11160 T := Etype (I);
11161 Resolve (I);
11162 R := I;
11164 -- If expander is inactive, type is legal, nothing else to construct
11166 else
11167 return;
11168 end if;
11169 end if;
11171 if not Is_Discrete_Type (T) then
11172 Error_Msg_N ("discrete type required for range", I);
11173 Set_Etype (I, Any_Type);
11174 return;
11176 elsif T = Any_Type then
11177 Set_Etype (I, Any_Type);
11178 return;
11179 end if;
11181 -- We will now create the appropriate Itype to describe the
11182 -- range, but first a check. If we originally had a subtype,
11183 -- then we just label the range with this subtype. Not only
11184 -- is there no need to construct a new subtype, but it is wrong
11185 -- to do so for two reasons:
11187 -- 1. A legality concern, if we have a subtype, it must not
11188 -- freeze, and the Itype would cause freezing incorrectly
11190 -- 2. An efficiency concern, if we created an Itype, it would
11191 -- not be recognized as the same type for the purposes of
11192 -- eliminating checks in some circumstances.
11194 -- We signal this case by setting the subtype entity in Def_Id.
11196 if No (Def_Id) then
11198 Def_Id :=
11199 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
11200 Set_Etype (Def_Id, Base_Type (T));
11202 if Is_Signed_Integer_Type (T) then
11203 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11205 elsif Is_Modular_Integer_Type (T) then
11206 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11208 else
11209 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11210 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11211 Set_First_Literal (Def_Id, First_Literal (T));
11212 end if;
11214 Set_Size_Info (Def_Id, (T));
11215 Set_RM_Size (Def_Id, RM_Size (T));
11216 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11218 Set_Scalar_Range (Def_Id, R);
11219 Conditional_Delay (Def_Id, T);
11221 -- In the subtype indication case, if the immediate parent of the
11222 -- new subtype is non-static, then the subtype we create is non-
11223 -- static, even if its bounds are static.
11225 if Nkind (I) = N_Subtype_Indication
11226 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
11227 then
11228 Set_Is_Non_Static_Subtype (Def_Id);
11229 end if;
11230 end if;
11232 -- Final step is to label the index with this constructed type
11234 Set_Etype (I, Def_Id);
11235 end Make_Index;
11237 ------------------------------
11238 -- Modular_Type_Declaration --
11239 ------------------------------
11241 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
11242 Mod_Expr : constant Node_Id := Expression (Def);
11243 M_Val : Uint;
11245 procedure Set_Modular_Size (Bits : Int);
11246 -- Sets RM_Size to Bits, and Esize to normal word size above this
11248 ----------------------
11249 -- Set_Modular_Size --
11250 ----------------------
11252 procedure Set_Modular_Size (Bits : Int) is
11253 begin
11254 Set_RM_Size (T, UI_From_Int (Bits));
11256 if Bits <= 8 then
11257 Init_Esize (T, 8);
11259 elsif Bits <= 16 then
11260 Init_Esize (T, 16);
11262 elsif Bits <= 32 then
11263 Init_Esize (T, 32);
11265 else
11266 Init_Esize (T, System_Max_Binary_Modulus_Power);
11267 end if;
11268 end Set_Modular_Size;
11270 -- Start of processing for Modular_Type_Declaration
11272 begin
11273 Analyze_And_Resolve (Mod_Expr, Any_Integer);
11274 Set_Etype (T, T);
11275 Set_Ekind (T, E_Modular_Integer_Type);
11276 Init_Alignment (T);
11277 Set_Is_Constrained (T);
11279 if not Is_OK_Static_Expression (Mod_Expr) then
11280 Flag_Non_Static_Expr
11281 ("non-static expression used for modular type bound!", Mod_Expr);
11282 M_Val := 2 ** System_Max_Binary_Modulus_Power;
11283 else
11284 M_Val := Expr_Value (Mod_Expr);
11285 end if;
11287 if M_Val < 1 then
11288 Error_Msg_N ("modulus value must be positive", Mod_Expr);
11289 M_Val := 2 ** System_Max_Binary_Modulus_Power;
11290 end if;
11292 Set_Modulus (T, M_Val);
11294 -- Create bounds for the modular type based on the modulus given in
11295 -- the type declaration and then analyze and resolve those bounds.
11297 Set_Scalar_Range (T,
11298 Make_Range (Sloc (Mod_Expr),
11299 Low_Bound =>
11300 Make_Integer_Literal (Sloc (Mod_Expr), 0),
11301 High_Bound =>
11302 Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
11304 -- Properly analyze the literals for the range. We do this manually
11305 -- because we can't go calling Resolve, since we are resolving these
11306 -- bounds with the type, and this type is certainly not complete yet!
11308 Set_Etype (Low_Bound (Scalar_Range (T)), T);
11309 Set_Etype (High_Bound (Scalar_Range (T)), T);
11310 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
11311 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
11313 -- Loop through powers of two to find number of bits required
11315 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
11317 -- Binary case
11319 if M_Val = 2 ** Bits then
11320 Set_Modular_Size (Bits);
11321 return;
11323 -- Non-binary case
11325 elsif M_Val < 2 ** Bits then
11326 Set_Non_Binary_Modulus (T);
11328 if Bits > System_Max_Nonbinary_Modulus_Power then
11329 Error_Msg_Uint_1 :=
11330 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
11331 Error_Msg_N
11332 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
11333 Set_Modular_Size (System_Max_Binary_Modulus_Power);
11334 return;
11336 else
11337 -- In the non-binary case, set size as per RM 13.3(55).
11339 Set_Modular_Size (Bits);
11340 return;
11341 end if;
11342 end if;
11344 end loop;
11346 -- If we fall through, then the size exceed System.Max_Binary_Modulus
11347 -- so we just signal an error and set the maximum size.
11349 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
11350 Error_Msg_N ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
11352 Set_Modular_Size (System_Max_Binary_Modulus_Power);
11353 Init_Alignment (T);
11355 end Modular_Type_Declaration;
11357 --------------------------
11358 -- New_Concatenation_Op --
11359 --------------------------
11361 procedure New_Concatenation_Op (Typ : Entity_Id) is
11362 Loc : constant Source_Ptr := Sloc (Typ);
11363 Op : Entity_Id;
11365 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
11366 -- Create abbreviated declaration for the formal of a predefined
11367 -- Operator 'Op' of type 'Typ'
11369 --------------------
11370 -- Make_Op_Formal --
11371 --------------------
11373 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
11374 Formal : Entity_Id;
11376 begin
11377 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
11378 Set_Etype (Formal, Typ);
11379 Set_Mechanism (Formal, Default_Mechanism);
11380 return Formal;
11381 end Make_Op_Formal;
11383 -- Start of processing for New_Concatenation_Op
11385 begin
11386 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
11388 Set_Ekind (Op, E_Operator);
11389 Set_Scope (Op, Current_Scope);
11390 Set_Etype (Op, Typ);
11391 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
11392 Set_Is_Immediately_Visible (Op);
11393 Set_Is_Intrinsic_Subprogram (Op);
11394 Set_Has_Completion (Op);
11395 Append_Entity (Op, Current_Scope);
11397 Set_Name_Entity_Id (Name_Op_Concat, Op);
11399 Append_Entity (Make_Op_Formal (Typ, Op), Op);
11400 Append_Entity (Make_Op_Formal (Typ, Op), Op);
11402 end New_Concatenation_Op;
11404 -------------------------------------------
11405 -- Ordinary_Fixed_Point_Type_Declaration --
11406 -------------------------------------------
11408 procedure Ordinary_Fixed_Point_Type_Declaration
11409 (T : Entity_Id;
11410 Def : Node_Id)
11412 Loc : constant Source_Ptr := Sloc (Def);
11413 Delta_Expr : constant Node_Id := Delta_Expression (Def);
11414 RRS : constant Node_Id := Real_Range_Specification (Def);
11415 Implicit_Base : Entity_Id;
11416 Delta_Val : Ureal;
11417 Small_Val : Ureal;
11418 Low_Val : Ureal;
11419 High_Val : Ureal;
11421 begin
11422 Check_Restriction (No_Fixed_Point, Def);
11424 -- Create implicit base type
11426 Implicit_Base :=
11427 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
11428 Set_Etype (Implicit_Base, Implicit_Base);
11430 -- Analyze and process delta expression
11432 Analyze_And_Resolve (Delta_Expr, Any_Real);
11434 Check_Delta_Expression (Delta_Expr);
11435 Delta_Val := Expr_Value_R (Delta_Expr);
11437 Set_Delta_Value (Implicit_Base, Delta_Val);
11439 -- Compute default small from given delta, which is the largest
11440 -- power of two that does not exceed the given delta value.
11442 declare
11443 Tmp : Ureal := Ureal_1;
11444 Scale : Int := 0;
11446 begin
11447 if Delta_Val < Ureal_1 then
11448 while Delta_Val < Tmp loop
11449 Tmp := Tmp / Ureal_2;
11450 Scale := Scale + 1;
11451 end loop;
11453 else
11454 loop
11455 Tmp := Tmp * Ureal_2;
11456 exit when Tmp > Delta_Val;
11457 Scale := Scale - 1;
11458 end loop;
11459 end if;
11461 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
11462 end;
11464 Set_Small_Value (Implicit_Base, Small_Val);
11466 -- If no range was given, set a dummy range
11468 if RRS <= Empty_Or_Error then
11469 Low_Val := -Small_Val;
11470 High_Val := Small_Val;
11472 -- Otherwise analyze and process given range
11474 else
11475 declare
11476 Low : constant Node_Id := Low_Bound (RRS);
11477 High : constant Node_Id := High_Bound (RRS);
11479 begin
11480 Analyze_And_Resolve (Low, Any_Real);
11481 Analyze_And_Resolve (High, Any_Real);
11482 Check_Real_Bound (Low);
11483 Check_Real_Bound (High);
11485 -- Obtain and set the range
11487 Low_Val := Expr_Value_R (Low);
11488 High_Val := Expr_Value_R (High);
11490 if Low_Val > High_Val then
11491 Error_Msg_NE ("?fixed point type& has null range", Def, T);
11492 end if;
11493 end;
11494 end if;
11496 -- The range for both the implicit base and the declared first
11497 -- subtype cannot be set yet, so we use the special routine
11498 -- Set_Fixed_Range to set a temporary range in place. Note that
11499 -- the bounds of the base type will be widened to be symmetrical
11500 -- and to fill the available bits when the type is frozen.
11502 -- We could do this with all discrete types, and probably should, but
11503 -- we absolutely have to do it for fixed-point, since the end-points
11504 -- of the range and the size are determined by the small value, which
11505 -- could be reset before the freeze point.
11507 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
11508 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11510 Init_Size_Align (Implicit_Base);
11512 -- Complete definition of first subtype
11514 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
11515 Set_Etype (T, Implicit_Base);
11516 Init_Size_Align (T);
11517 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11518 Set_Small_Value (T, Small_Val);
11519 Set_Delta_Value (T, Delta_Val);
11520 Set_Is_Constrained (T);
11522 end Ordinary_Fixed_Point_Type_Declaration;
11524 ----------------------------------------
11525 -- Prepare_Private_Subtype_Completion --
11526 ----------------------------------------
11528 procedure Prepare_Private_Subtype_Completion
11529 (Id : Entity_Id;
11530 Related_Nod : Node_Id)
11532 Id_B : constant Entity_Id := Base_Type (Id);
11533 Full_B : constant Entity_Id := Full_View (Id_B);
11534 Full : Entity_Id;
11536 begin
11537 if Present (Full_B) then
11539 -- The Base_Type is already completed, we can complete the
11540 -- subtype now. We have to create a new entity with the same name,
11541 -- Thus we can't use Create_Itype.
11542 -- This is messy, should be fixed ???
11544 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
11545 Set_Is_Itype (Full);
11546 Set_Associated_Node_For_Itype (Full, Related_Nod);
11547 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
11548 end if;
11550 -- The parent subtype may be private, but the base might not, in some
11551 -- nested instances. In that case, the subtype does not need to be
11552 -- exchanged. It would still be nice to make private subtypes and their
11553 -- bases consistent at all times ???
11555 if Is_Private_Type (Id_B) then
11556 Append_Elmt (Id, Private_Dependents (Id_B));
11557 end if;
11559 end Prepare_Private_Subtype_Completion;
11561 ---------------------------
11562 -- Process_Discriminants --
11563 ---------------------------
11565 procedure Process_Discriminants
11566 (N : Node_Id;
11567 Prev : Entity_Id := Empty)
11569 Elist : constant Elist_Id := New_Elmt_List;
11570 Id : Node_Id;
11571 Discr : Node_Id;
11572 Discr_Number : Uint;
11573 Discr_Type : Entity_Id;
11574 Default_Present : Boolean := False;
11575 Default_Not_Present : Boolean := False;
11577 begin
11578 -- A composite type other than an array type can have discriminants.
11579 -- Discriminants of non-limited types must have a discrete type.
11580 -- On entry, the current scope is the composite type.
11582 -- The discriminants are initially entered into the scope of the type
11583 -- via Enter_Name with the default Ekind of E_Void to prevent premature
11584 -- use, as explained at the end of this procedure.
11586 Discr := First (Discriminant_Specifications (N));
11587 while Present (Discr) loop
11588 Enter_Name (Defining_Identifier (Discr));
11590 -- For navigation purposes we add a reference to the discriminant
11591 -- in the entity for the type. If the current declaration is a
11592 -- completion, place references on the partial view. Otherwise the
11593 -- type is the current scope.
11595 if Present (Prev) then
11597 -- The references go on the partial view, if present. If the
11598 -- partial view has discriminants, the references have been
11599 -- generated already.
11601 if not Has_Discriminants (Prev) then
11602 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
11603 end if;
11604 else
11605 Generate_Reference
11606 (Current_Scope, Defining_Identifier (Discr), 'd');
11607 end if;
11609 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
11610 Discr_Type := Access_Definition (N, Discriminant_Type (Discr));
11612 -- Ada 0Y (AI-254)
11614 if Present (Access_To_Subprogram_Definition
11615 (Discriminant_Type (Discr)))
11616 and then Protected_Present (Access_To_Subprogram_Definition
11617 (Discriminant_Type (Discr)))
11618 then
11619 Discr_Type :=
11620 Replace_Anonymous_Access_To_Protected_Subprogram
11621 (Discr, Discr_Type);
11622 end if;
11624 else
11625 Find_Type (Discriminant_Type (Discr));
11626 Discr_Type := Etype (Discriminant_Type (Discr));
11628 if Error_Posted (Discriminant_Type (Discr)) then
11629 Discr_Type := Any_Type;
11630 end if;
11631 end if;
11633 if Is_Access_Type (Discr_Type) then
11635 -- Ada 0Y (AI-230): Access discriminant allowed in non-limited
11636 -- record types
11638 if not Extensions_Allowed then
11639 Check_Access_Discriminant_Requires_Limited
11640 (Discr, Discriminant_Type (Discr));
11641 end if;
11643 if Ada_83 and then Comes_From_Source (Discr) then
11644 Error_Msg_N
11645 ("(Ada 83) access discriminant not allowed", Discr);
11646 end if;
11648 elsif not Is_Discrete_Type (Discr_Type) then
11649 Error_Msg_N ("discriminants must have a discrete or access type",
11650 Discriminant_Type (Discr));
11651 end if;
11653 Set_Etype (Defining_Identifier (Discr), Discr_Type);
11655 -- If a discriminant specification includes the assignment compound
11656 -- delimiter followed by an expression, the expression is the default
11657 -- expression of the discriminant; the default expression must be of
11658 -- the type of the discriminant. (RM 3.7.1) Since this expression is
11659 -- a default expression, we do the special preanalysis, since this
11660 -- expression does not freeze (see "Handling of Default and Per-
11661 -- Object Expressions" in spec of package Sem).
11663 if Present (Expression (Discr)) then
11664 Analyze_Per_Use_Expression (Expression (Discr), Discr_Type);
11666 if Nkind (N) = N_Formal_Type_Declaration then
11667 Error_Msg_N
11668 ("discriminant defaults not allowed for formal type",
11669 Expression (Discr));
11671 -- Tagged types cannot have defaulted discriminants, but a
11672 -- non-tagged private type with defaulted discriminants
11673 -- can have a tagged completion.
11675 elsif Is_Tagged_Type (Current_Scope)
11676 and then Comes_From_Source (N)
11677 then
11678 Error_Msg_N
11679 ("discriminants of tagged type cannot have defaults",
11680 Expression (Discr));
11682 else
11683 Default_Present := True;
11684 Append_Elmt (Expression (Discr), Elist);
11686 -- Tag the defining identifiers for the discriminants with
11687 -- their corresponding default expressions from the tree.
11689 Set_Discriminant_Default_Value
11690 (Defining_Identifier (Discr), Expression (Discr));
11691 end if;
11693 else
11694 Default_Not_Present := True;
11695 end if;
11697 -- Ada 0Y (AI-231): Set the null-excluding attribute and carry out
11698 -- some static checks
11700 if Extensions_Allowed
11701 and then (Null_Exclusion_Present (Discr)
11702 or else Can_Never_Be_Null (Discr_Type))
11703 then
11704 Set_Can_Never_Be_Null (Defining_Identifier (Discr));
11705 Null_Exclusion_Static_Checks (Discr);
11706 end if;
11708 Next (Discr);
11709 end loop;
11711 -- An element list consisting of the default expressions of the
11712 -- discriminants is constructed in the above loop and used to set
11713 -- the Discriminant_Constraint attribute for the type. If an object
11714 -- is declared of this (record or task) type without any explicit
11715 -- discriminant constraint given, this element list will form the
11716 -- actual parameters for the corresponding initialization procedure
11717 -- for the type.
11719 Set_Discriminant_Constraint (Current_Scope, Elist);
11720 Set_Stored_Constraint (Current_Scope, No_Elist);
11722 -- Default expressions must be provided either for all or for none
11723 -- of the discriminants of a discriminant part. (RM 3.7.1)
11725 if Default_Present and then Default_Not_Present then
11726 Error_Msg_N
11727 ("incomplete specification of defaults for discriminants", N);
11728 end if;
11730 -- The use of the name of a discriminant is not allowed in default
11731 -- expressions of a discriminant part if the specification of the
11732 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
11734 -- To detect this, the discriminant names are entered initially with an
11735 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
11736 -- attempt to use a void entity (for example in an expression that is
11737 -- type-checked) produces the error message: premature usage. Now after
11738 -- completing the semantic analysis of the discriminant part, we can set
11739 -- the Ekind of all the discriminants appropriately.
11741 Discr := First (Discriminant_Specifications (N));
11742 Discr_Number := Uint_1;
11744 while Present (Discr) loop
11745 Id := Defining_Identifier (Discr);
11746 Set_Ekind (Id, E_Discriminant);
11747 Init_Component_Location (Id);
11748 Init_Esize (Id);
11749 Set_Discriminant_Number (Id, Discr_Number);
11751 -- Make sure this is always set, even in illegal programs
11753 Set_Corresponding_Discriminant (Id, Empty);
11755 -- Initialize the Original_Record_Component to the entity itself.
11756 -- Inherit_Components will propagate the right value to
11757 -- discriminants in derived record types.
11759 Set_Original_Record_Component (Id, Id);
11761 -- Create the discriminal for the discriminant.
11763 Build_Discriminal (Id);
11765 Next (Discr);
11766 Discr_Number := Discr_Number + 1;
11767 end loop;
11769 Set_Has_Discriminants (Current_Scope);
11770 end Process_Discriminants;
11772 -----------------------
11773 -- Process_Full_View --
11774 -----------------------
11776 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
11777 Priv_Parent : Entity_Id;
11778 Full_Parent : Entity_Id;
11779 Full_Indic : Node_Id;
11781 begin
11782 -- First some sanity checks that must be done after semantic
11783 -- decoration of the full view and thus cannot be placed with other
11784 -- similar checks in Find_Type_Name
11786 if not Is_Limited_Type (Priv_T)
11787 and then (Is_Limited_Type (Full_T)
11788 or else Is_Limited_Composite (Full_T))
11789 then
11790 Error_Msg_N
11791 ("completion of nonlimited type cannot be limited", Full_T);
11792 Explain_Limited_Type (Full_T, Full_T);
11794 elsif Is_Abstract (Full_T) and then not Is_Abstract (Priv_T) then
11795 Error_Msg_N
11796 ("completion of nonabstract type cannot be abstract", Full_T);
11798 elsif Is_Tagged_Type (Priv_T)
11799 and then Is_Limited_Type (Priv_T)
11800 and then not Is_Limited_Type (Full_T)
11801 then
11802 -- GNAT allow its own definition of Limited_Controlled to disobey
11803 -- this rule in order in ease the implementation. The next test is
11804 -- safe because Root_Controlled is defined in a private system child
11806 if Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
11807 Set_Is_Limited_Composite (Full_T);
11808 else
11809 Error_Msg_N
11810 ("completion of limited tagged type must be limited", Full_T);
11811 end if;
11813 elsif Is_Generic_Type (Priv_T) then
11814 Error_Msg_N ("generic type cannot have a completion", Full_T);
11815 end if;
11817 if Is_Tagged_Type (Priv_T)
11818 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
11819 and then Is_Derived_Type (Full_T)
11820 then
11821 Priv_Parent := Etype (Priv_T);
11823 -- The full view of a private extension may have been transformed
11824 -- into an unconstrained derived type declaration and a subtype
11825 -- declaration (see build_derived_record_type for details).
11827 if Nkind (N) = N_Subtype_Declaration then
11828 Full_Indic := Subtype_Indication (N);
11829 Full_Parent := Etype (Base_Type (Full_T));
11830 else
11831 Full_Indic := Subtype_Indication (Type_Definition (N));
11832 Full_Parent := Etype (Full_T);
11833 end if;
11835 -- Check that the parent type of the full type is a descendant of
11836 -- the ancestor subtype given in the private extension. If either
11837 -- entity has an Etype equal to Any_Type then we had some previous
11838 -- error situation [7.3(8)].
11840 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
11841 return;
11843 elsif not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent) then
11844 Error_Msg_N
11845 ("parent of full type must descend from parent"
11846 & " of private extension", Full_Indic);
11848 -- Check the rules of 7.3(10): if the private extension inherits
11849 -- known discriminants, then the full type must also inherit those
11850 -- discriminants from the same (ancestor) type, and the parent
11851 -- subtype of the full type must be constrained if and only if
11852 -- the ancestor subtype of the private extension is constrained.
11854 elsif not Present (Discriminant_Specifications (Parent (Priv_T)))
11855 and then not Has_Unknown_Discriminants (Priv_T)
11856 and then Has_Discriminants (Base_Type (Priv_Parent))
11857 then
11858 declare
11859 Priv_Indic : constant Node_Id :=
11860 Subtype_Indication (Parent (Priv_T));
11862 Priv_Constr : constant Boolean :=
11863 Is_Constrained (Priv_Parent)
11864 or else
11865 Nkind (Priv_Indic) = N_Subtype_Indication
11866 or else Is_Constrained (Entity (Priv_Indic));
11868 Full_Constr : constant Boolean :=
11869 Is_Constrained (Full_Parent)
11870 or else
11871 Nkind (Full_Indic) = N_Subtype_Indication
11872 or else Is_Constrained (Entity (Full_Indic));
11874 Priv_Discr : Entity_Id;
11875 Full_Discr : Entity_Id;
11877 begin
11878 Priv_Discr := First_Discriminant (Priv_Parent);
11879 Full_Discr := First_Discriminant (Full_Parent);
11881 while Present (Priv_Discr) and then Present (Full_Discr) loop
11882 if Original_Record_Component (Priv_Discr) =
11883 Original_Record_Component (Full_Discr)
11884 or else
11885 Corresponding_Discriminant (Priv_Discr) =
11886 Corresponding_Discriminant (Full_Discr)
11887 then
11888 null;
11889 else
11890 exit;
11891 end if;
11893 Next_Discriminant (Priv_Discr);
11894 Next_Discriminant (Full_Discr);
11895 end loop;
11897 if Present (Priv_Discr) or else Present (Full_Discr) then
11898 Error_Msg_N
11899 ("full view must inherit discriminants of the parent type"
11900 & " used in the private extension", Full_Indic);
11902 elsif Priv_Constr and then not Full_Constr then
11903 Error_Msg_N
11904 ("parent subtype of full type must be constrained",
11905 Full_Indic);
11907 elsif Full_Constr and then not Priv_Constr then
11908 Error_Msg_N
11909 ("parent subtype of full type must be unconstrained",
11910 Full_Indic);
11911 end if;
11912 end;
11914 -- Check the rules of 7.3(12): if a partial view has neither known
11915 -- or unknown discriminants, then the full type declaration shall
11916 -- define a definite subtype.
11918 elsif not Has_Unknown_Discriminants (Priv_T)
11919 and then not Has_Discriminants (Priv_T)
11920 and then not Is_Constrained (Full_T)
11921 then
11922 Error_Msg_N
11923 ("full view must define a constrained type if partial view"
11924 & " has no discriminants", Full_T);
11925 end if;
11927 -- ??????? Do we implement the following properly ?????
11928 -- If the ancestor subtype of a private extension has constrained
11929 -- discriminants, then the parent subtype of the full view shall
11930 -- impose a statically matching constraint on those discriminants
11931 -- [7.3(13)].
11933 else
11934 -- For untagged types, verify that a type without discriminants
11935 -- is not completed with an unconstrained type.
11937 if not Is_Indefinite_Subtype (Priv_T)
11938 and then Is_Indefinite_Subtype (Full_T)
11939 then
11940 Error_Msg_N ("full view of type must be definite subtype", Full_T);
11941 end if;
11942 end if;
11944 -- Create a full declaration for all its subtypes recorded in
11945 -- Private_Dependents and swap them similarly to the base type.
11946 -- These are subtypes that have been define before the full
11947 -- declaration of the private type. We also swap the entry in
11948 -- Private_Dependents list so we can properly restore the
11949 -- private view on exit from the scope.
11951 declare
11952 Priv_Elmt : Elmt_Id;
11953 Priv : Entity_Id;
11954 Full : Entity_Id;
11956 begin
11957 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
11958 while Present (Priv_Elmt) loop
11959 Priv := Node (Priv_Elmt);
11961 if Ekind (Priv) = E_Private_Subtype
11962 or else Ekind (Priv) = E_Limited_Private_Subtype
11963 or else Ekind (Priv) = E_Record_Subtype_With_Private
11964 then
11965 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
11966 Set_Is_Itype (Full);
11967 Set_Parent (Full, Parent (Priv));
11968 Set_Associated_Node_For_Itype (Full, N);
11970 -- Now we need to complete the private subtype, but since the
11971 -- base type has already been swapped, we must also swap the
11972 -- subtypes (and thus, reverse the arguments in the call to
11973 -- Complete_Private_Subtype).
11975 Copy_And_Swap (Priv, Full);
11976 Complete_Private_Subtype (Full, Priv, Full_T, N);
11977 Replace_Elmt (Priv_Elmt, Full);
11978 end if;
11980 Next_Elmt (Priv_Elmt);
11981 end loop;
11982 end;
11984 -- If the private view was tagged, copy the new Primitive
11985 -- operations from the private view to the full view.
11987 if Is_Tagged_Type (Full_T) then
11988 declare
11989 Priv_List : Elist_Id;
11990 Full_List : constant Elist_Id := Primitive_Operations (Full_T);
11991 P1, P2 : Elmt_Id;
11992 Prim : Entity_Id;
11993 D_Type : Entity_Id;
11995 begin
11996 if Is_Tagged_Type (Priv_T) then
11997 Priv_List := Primitive_Operations (Priv_T);
11999 P1 := First_Elmt (Priv_List);
12000 while Present (P1) loop
12001 Prim := Node (P1);
12003 -- Transfer explicit primitives, not those inherited from
12004 -- parent of partial view, which will be re-inherited on
12005 -- the full view.
12007 if Comes_From_Source (Prim) then
12008 P2 := First_Elmt (Full_List);
12009 while Present (P2) and then Node (P2) /= Prim loop
12010 Next_Elmt (P2);
12011 end loop;
12013 -- If not found, that is a new one
12015 if No (P2) then
12016 Append_Elmt (Prim, Full_List);
12017 end if;
12018 end if;
12020 Next_Elmt (P1);
12021 end loop;
12023 else
12024 -- In this case the partial view is untagged, so here we
12025 -- locate all of the earlier primitives that need to be
12026 -- treated as dispatching (those that appear between the
12027 -- two views). Note that these additional operations must
12028 -- all be new operations (any earlier operations that
12029 -- override inherited operations of the full view will
12030 -- already have been inserted in the primitives list and
12031 -- marked as dispatching by Check_Operation_From_Private_View.
12032 -- Note that implicit "/=" operators are excluded from being
12033 -- added to the primitives list since they shouldn't be
12034 -- treated as dispatching (tagged "/=" is handled specially).
12036 Prim := Next_Entity (Full_T);
12037 while Present (Prim) and then Prim /= Priv_T loop
12038 if Ekind (Prim) = E_Procedure
12039 or else
12040 Ekind (Prim) = E_Function
12041 then
12043 D_Type := Find_Dispatching_Type (Prim);
12045 if D_Type = Full_T
12046 and then (Chars (Prim) /= Name_Op_Ne
12047 or else Comes_From_Source (Prim))
12048 then
12049 Check_Controlling_Formals (Full_T, Prim);
12051 if not Is_Dispatching_Operation (Prim) then
12052 Append_Elmt (Prim, Full_List);
12053 Set_Is_Dispatching_Operation (Prim, True);
12054 Set_DT_Position (Prim, No_Uint);
12055 end if;
12057 elsif Is_Dispatching_Operation (Prim)
12058 and then D_Type /= Full_T
12059 then
12061 -- Verify that it is not otherwise controlled by
12062 -- a formal or a return value ot type T.
12064 Check_Controlling_Formals (D_Type, Prim);
12065 end if;
12066 end if;
12068 Next_Entity (Prim);
12069 end loop;
12070 end if;
12072 -- For the tagged case, the two views can share the same
12073 -- Primitive Operation list and the same class wide type.
12074 -- Update attributes of the class-wide type which depend on
12075 -- the full declaration.
12077 if Is_Tagged_Type (Priv_T) then
12078 Set_Primitive_Operations (Priv_T, Full_List);
12079 Set_Class_Wide_Type
12080 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
12082 -- Any other attributes should be propagated to C_W ???
12084 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
12086 end if;
12087 end;
12088 end if;
12089 end Process_Full_View;
12091 -----------------------------------
12092 -- Process_Incomplete_Dependents --
12093 -----------------------------------
12095 procedure Process_Incomplete_Dependents
12096 (N : Node_Id;
12097 Full_T : Entity_Id;
12098 Inc_T : Entity_Id)
12100 Inc_Elmt : Elmt_Id;
12101 Priv_Dep : Entity_Id;
12102 New_Subt : Entity_Id;
12104 Disc_Constraint : Elist_Id;
12106 begin
12107 if No (Private_Dependents (Inc_T)) then
12108 return;
12110 else
12111 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
12113 -- Itypes that may be generated by the completion of an incomplete
12114 -- subtype are not used by the back-end and not attached to the tree.
12115 -- They are created only for constraint-checking purposes.
12116 end if;
12118 while Present (Inc_Elmt) loop
12119 Priv_Dep := Node (Inc_Elmt);
12121 if Ekind (Priv_Dep) = E_Subprogram_Type then
12123 -- An Access_To_Subprogram type may have a return type or a
12124 -- parameter type that is incomplete. Replace with the full view.
12126 if Etype (Priv_Dep) = Inc_T then
12127 Set_Etype (Priv_Dep, Full_T);
12128 end if;
12130 declare
12131 Formal : Entity_Id;
12133 begin
12134 Formal := First_Formal (Priv_Dep);
12136 while Present (Formal) loop
12138 if Etype (Formal) = Inc_T then
12139 Set_Etype (Formal, Full_T);
12140 end if;
12142 Next_Formal (Formal);
12143 end loop;
12144 end;
12146 elsif Is_Overloadable (Priv_Dep) then
12148 if Is_Tagged_Type (Full_T) then
12150 -- Subprogram has an access parameter whose designated type
12151 -- was incomplete. Reexamine declaration now, because it may
12152 -- be a primitive operation of the full type.
12154 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
12155 Set_Is_Dispatching_Operation (Priv_Dep);
12156 Check_Controlling_Formals (Full_T, Priv_Dep);
12157 end if;
12159 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
12161 -- Can happen during processing of a body before the completion
12162 -- of a TA type. Ignore, because spec is also on dependent list.
12164 return;
12166 -- Dependent is a subtype
12168 else
12169 -- We build a new subtype indication using the full view of the
12170 -- incomplete parent. The discriminant constraints have been
12171 -- elaborated already at the point of the subtype declaration.
12173 New_Subt := Create_Itype (E_Void, N);
12175 if Has_Discriminants (Full_T) then
12176 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
12177 else
12178 Disc_Constraint := No_Elist;
12179 end if;
12181 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
12182 Set_Full_View (Priv_Dep, New_Subt);
12183 end if;
12185 Next_Elmt (Inc_Elmt);
12186 end loop;
12188 end Process_Incomplete_Dependents;
12190 --------------------------------
12191 -- Process_Range_Expr_In_Decl --
12192 --------------------------------
12194 procedure Process_Range_Expr_In_Decl
12195 (R : Node_Id;
12196 T : Entity_Id;
12197 Check_List : List_Id := Empty_List;
12198 R_Check_Off : Boolean := False)
12200 Lo, Hi : Node_Id;
12201 R_Checks : Check_Result;
12202 Type_Decl : Node_Id;
12203 Def_Id : Entity_Id;
12205 begin
12206 Analyze_And_Resolve (R, Base_Type (T));
12208 if Nkind (R) = N_Range then
12209 Lo := Low_Bound (R);
12210 Hi := High_Bound (R);
12212 -- If there were errors in the declaration, try and patch up some
12213 -- common mistakes in the bounds. The cases handled are literals
12214 -- which are Integer where the expected type is Real and vice versa.
12215 -- These corrections allow the compilation process to proceed further
12216 -- along since some basic assumptions of the format of the bounds
12217 -- are guaranteed.
12219 if Etype (R) = Any_Type then
12221 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
12222 Rewrite (Lo,
12223 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
12225 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
12226 Rewrite (Hi,
12227 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
12229 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
12230 Rewrite (Lo,
12231 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
12233 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
12234 Rewrite (Hi,
12235 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
12236 end if;
12238 Set_Etype (Lo, T);
12239 Set_Etype (Hi, T);
12240 end if;
12242 -- If the bounds of the range have been mistakenly given as
12243 -- string literals (perhaps in place of character literals),
12244 -- then an error has already been reported, but we rewrite
12245 -- the string literal as a bound of the range's type to
12246 -- avoid blowups in later processing that looks at static
12247 -- values.
12249 if Nkind (Lo) = N_String_Literal then
12250 Rewrite (Lo,
12251 Make_Attribute_Reference (Sloc (Lo),
12252 Attribute_Name => Name_First,
12253 Prefix => New_Reference_To (T, Sloc (Lo))));
12254 Analyze_And_Resolve (Lo);
12255 end if;
12257 if Nkind (Hi) = N_String_Literal then
12258 Rewrite (Hi,
12259 Make_Attribute_Reference (Sloc (Hi),
12260 Attribute_Name => Name_First,
12261 Prefix => New_Reference_To (T, Sloc (Hi))));
12262 Analyze_And_Resolve (Hi);
12263 end if;
12265 -- If bounds aren't scalar at this point then exit, avoiding
12266 -- problems with further processing of the range in this procedure.
12268 if not Is_Scalar_Type (Etype (Lo)) then
12269 return;
12270 end if;
12272 -- Resolve (actually Sem_Eval) has checked that the bounds are in
12273 -- then range of the base type. Here we check whether the bounds
12274 -- are in the range of the subtype itself. Note that if the bounds
12275 -- represent the null range the Constraint_Error exception should
12276 -- not be raised.
12278 -- ??? The following code should be cleaned up as follows
12279 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
12280 -- is done in the call to Range_Check (R, T); below
12281 -- 2. The use of R_Check_Off should be investigated and possibly
12282 -- removed, this would clean up things a bit.
12284 if Is_Null_Range (Lo, Hi) then
12285 null;
12287 else
12288 -- Capture values of bounds and generate temporaries for them
12289 -- if needed, before applying checks, since checks may cause
12290 -- duplication of the expression without forcing evaluation.
12292 if Expander_Active then
12293 Force_Evaluation (Lo);
12294 Force_Evaluation (Hi);
12295 end if;
12297 -- We use a flag here instead of suppressing checks on the
12298 -- type because the type we check against isn't necessarily
12299 -- the place where we put the check.
12301 if not R_Check_Off then
12302 R_Checks := Range_Check (R, T);
12303 Type_Decl := Parent (R);
12305 -- Look up tree to find an appropriate insertion point.
12306 -- This seems really junk code, and very brittle, couldn't
12307 -- we just use an insert actions call of some kind ???
12309 while Present (Type_Decl) and then not
12310 (Nkind (Type_Decl) = N_Full_Type_Declaration
12311 or else
12312 Nkind (Type_Decl) = N_Subtype_Declaration
12313 or else
12314 Nkind (Type_Decl) = N_Loop_Statement
12315 or else
12316 Nkind (Type_Decl) = N_Task_Type_Declaration
12317 or else
12318 Nkind (Type_Decl) = N_Single_Task_Declaration
12319 or else
12320 Nkind (Type_Decl) = N_Protected_Type_Declaration
12321 or else
12322 Nkind (Type_Decl) = N_Single_Protected_Declaration)
12323 loop
12324 Type_Decl := Parent (Type_Decl);
12325 end loop;
12327 -- Why would Type_Decl not be present??? Without this test,
12328 -- short regression tests fail.
12330 if Present (Type_Decl) then
12332 -- Case of loop statement (more comments ???)
12334 if Nkind (Type_Decl) = N_Loop_Statement then
12335 declare
12336 Indic : Node_Id := Parent (R);
12338 begin
12339 while Present (Indic) and then not
12340 (Nkind (Indic) = N_Subtype_Indication)
12341 loop
12342 Indic := Parent (Indic);
12343 end loop;
12345 if Present (Indic) then
12346 Def_Id := Etype (Subtype_Mark (Indic));
12348 Insert_Range_Checks
12349 (R_Checks,
12350 Type_Decl,
12351 Def_Id,
12352 Sloc (Type_Decl),
12354 Do_Before => True);
12355 end if;
12356 end;
12358 -- All other cases (more comments ???)
12360 else
12361 Def_Id := Defining_Identifier (Type_Decl);
12363 if (Ekind (Def_Id) = E_Record_Type
12364 and then Depends_On_Discriminant (R))
12365 or else
12366 (Ekind (Def_Id) = E_Protected_Type
12367 and then Has_Discriminants (Def_Id))
12368 then
12369 Append_Range_Checks
12370 (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
12372 else
12373 Insert_Range_Checks
12374 (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
12376 end if;
12377 end if;
12378 end if;
12379 end if;
12380 end if;
12382 elsif Expander_Active then
12383 Get_Index_Bounds (R, Lo, Hi);
12384 Force_Evaluation (Lo);
12385 Force_Evaluation (Hi);
12386 end if;
12387 end Process_Range_Expr_In_Decl;
12389 --------------------------------------
12390 -- Process_Real_Range_Specification --
12391 --------------------------------------
12393 procedure Process_Real_Range_Specification (Def : Node_Id) is
12394 Spec : constant Node_Id := Real_Range_Specification (Def);
12395 Lo : Node_Id;
12396 Hi : Node_Id;
12397 Err : Boolean := False;
12399 procedure Analyze_Bound (N : Node_Id);
12400 -- Analyze and check one bound
12402 -------------------
12403 -- Analyze_Bound --
12404 -------------------
12406 procedure Analyze_Bound (N : Node_Id) is
12407 begin
12408 Analyze_And_Resolve (N, Any_Real);
12410 if not Is_OK_Static_Expression (N) then
12411 Flag_Non_Static_Expr
12412 ("bound in real type definition is not static!", N);
12413 Err := True;
12414 end if;
12415 end Analyze_Bound;
12417 -- Start of processing for Process_Real_Range_Specification
12419 begin
12420 if Present (Spec) then
12421 Lo := Low_Bound (Spec);
12422 Hi := High_Bound (Spec);
12423 Analyze_Bound (Lo);
12424 Analyze_Bound (Hi);
12426 -- If error, clear away junk range specification
12428 if Err then
12429 Set_Real_Range_Specification (Def, Empty);
12430 end if;
12431 end if;
12432 end Process_Real_Range_Specification;
12434 ---------------------
12435 -- Process_Subtype --
12436 ---------------------
12438 function Process_Subtype
12439 (S : Node_Id;
12440 Related_Nod : Node_Id;
12441 Related_Id : Entity_Id := Empty;
12442 Suffix : Character := ' ') return Entity_Id
12444 P : Node_Id;
12445 Def_Id : Entity_Id;
12446 Full_View_Id : Entity_Id;
12447 Subtype_Mark_Id : Entity_Id;
12449 procedure Check_Incomplete (T : Entity_Id);
12450 -- Called to verify that an incomplete type is not used prematurely
12452 ----------------------
12453 -- Check_Incomplete --
12454 ----------------------
12456 procedure Check_Incomplete (T : Entity_Id) is
12457 begin
12458 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type then
12459 Error_Msg_N ("invalid use of type before its full declaration", T);
12460 end if;
12461 end Check_Incomplete;
12463 -- Start of processing for Process_Subtype
12465 begin
12466 -- Case of no constraints present
12468 if Nkind (S) /= N_Subtype_Indication then
12470 Find_Type (S);
12471 Check_Incomplete (S);
12473 -- Ada 0Y (AI-231): Static check
12475 if Extensions_Allowed
12476 and then Present (Parent (S))
12477 and then Null_Exclusion_Present (Parent (S))
12478 and then Nkind (Parent (S)) /= N_Access_To_Object_Definition
12479 and then not Is_Access_Type (Entity (S))
12480 then
12481 Error_Msg_N
12482 ("(Ada 0Y) null-exclusion part requires an access type", S);
12483 end if;
12484 return Entity (S);
12486 -- Case of constraint present, so that we have an N_Subtype_Indication
12487 -- node (this node is created only if constraints are present).
12489 else
12491 Find_Type (Subtype_Mark (S));
12493 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
12494 and then not
12495 (Nkind (Parent (S)) = N_Subtype_Declaration
12496 and then
12497 Is_Itype (Defining_Identifier (Parent (S))))
12498 then
12499 Check_Incomplete (Subtype_Mark (S));
12500 end if;
12502 P := Parent (S);
12503 Subtype_Mark_Id := Entity (Subtype_Mark (S));
12505 if Is_Unchecked_Union (Subtype_Mark_Id)
12506 and then Comes_From_Source (Related_Nod)
12507 then
12508 Error_Msg_N
12509 ("cannot create subtype of Unchecked_Union", Related_Nod);
12510 end if;
12512 -- Explicit subtype declaration case
12514 if Nkind (P) = N_Subtype_Declaration then
12515 Def_Id := Defining_Identifier (P);
12517 -- Explicit derived type definition case
12519 elsif Nkind (P) = N_Derived_Type_Definition then
12520 Def_Id := Defining_Identifier (Parent (P));
12522 -- Implicit case, the Def_Id must be created as an implicit type.
12523 -- The one exception arises in the case of concurrent types,
12524 -- array and access types, where other subsidiary implicit types
12525 -- may be created and must appear before the main implicit type.
12526 -- In these cases we leave Def_Id set to Empty as a signal that
12527 -- Create_Itype has not yet been called to create Def_Id.
12529 else
12530 if Is_Array_Type (Subtype_Mark_Id)
12531 or else Is_Concurrent_Type (Subtype_Mark_Id)
12532 or else Is_Access_Type (Subtype_Mark_Id)
12533 then
12534 Def_Id := Empty;
12536 -- For the other cases, we create a new unattached Itype,
12537 -- and set the indication to ensure it gets attached later.
12539 else
12540 Def_Id :=
12541 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12542 end if;
12543 end if;
12545 -- If the kind of constraint is invalid for this kind of type,
12546 -- then give an error, and then pretend no constraint was given.
12548 if not Is_Valid_Constraint_Kind
12549 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
12550 then
12551 Error_Msg_N
12552 ("incorrect constraint for this kind of type", Constraint (S));
12554 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
12556 -- Make recursive call, having got rid of the bogus constraint
12558 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
12559 end if;
12561 -- Remaining processing depends on type
12563 case Ekind (Subtype_Mark_Id) is
12565 when Access_Kind =>
12566 Constrain_Access (Def_Id, S, Related_Nod);
12568 when Array_Kind =>
12569 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
12571 when Decimal_Fixed_Point_Kind =>
12572 Constrain_Decimal (Def_Id, S);
12574 when Enumeration_Kind =>
12575 Constrain_Enumeration (Def_Id, S);
12577 when Ordinary_Fixed_Point_Kind =>
12578 Constrain_Ordinary_Fixed (Def_Id, S);
12580 when Float_Kind =>
12581 Constrain_Float (Def_Id, S);
12583 when Integer_Kind =>
12584 Constrain_Integer (Def_Id, S);
12586 when E_Record_Type |
12587 E_Record_Subtype |
12588 Class_Wide_Kind |
12589 E_Incomplete_Type =>
12590 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
12592 when Private_Kind =>
12593 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
12594 Set_Private_Dependents (Def_Id, New_Elmt_List);
12596 -- In case of an invalid constraint prevent further processing
12597 -- since the type constructed is missing expected fields.
12599 if Etype (Def_Id) = Any_Type then
12600 return Def_Id;
12601 end if;
12603 -- If the full view is that of a task with discriminants,
12604 -- we must constrain both the concurrent type and its
12605 -- corresponding record type. Otherwise we will just propagate
12606 -- the constraint to the full view, if available.
12608 if Present (Full_View (Subtype_Mark_Id))
12609 and then Has_Discriminants (Subtype_Mark_Id)
12610 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
12611 then
12612 Full_View_Id :=
12613 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12615 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
12616 Constrain_Concurrent (Full_View_Id, S,
12617 Related_Nod, Related_Id, Suffix);
12618 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
12619 Set_Full_View (Def_Id, Full_View_Id);
12621 else
12622 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
12623 end if;
12625 when Concurrent_Kind =>
12626 Constrain_Concurrent (Def_Id, S,
12627 Related_Nod, Related_Id, Suffix);
12629 when others =>
12630 Error_Msg_N ("invalid subtype mark in subtype indication", S);
12631 end case;
12633 -- Size and Convention are always inherited from the base type
12635 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
12636 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
12638 return Def_Id;
12640 end if;
12641 end Process_Subtype;
12643 -----------------------------
12644 -- Record_Type_Declaration --
12645 -----------------------------
12647 procedure Record_Type_Declaration
12648 (T : Entity_Id;
12649 N : Node_Id;
12650 Prev : Entity_Id)
12652 Def : constant Node_Id := Type_Definition (N);
12654 Is_Tagged : Boolean;
12655 Tag_Comp : Entity_Id;
12657 begin
12658 -- The flag Is_Tagged_Type might have already been set by Find_Type_Name
12659 -- if it detected an error for declaration T. This arises in the case of
12660 -- private tagged types where the full view omits the word tagged.
12662 Is_Tagged := Tagged_Present (Def)
12663 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
12665 -- Records constitute a scope for the component declarations within.
12666 -- The scope is created prior to the processing of these declarations.
12667 -- Discriminants are processed first, so that they are visible when
12668 -- processing the other components. The Ekind of the record type itself
12669 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
12671 -- Enter record scope
12673 New_Scope (T);
12675 -- These flags must be initialized before calling Process_Discriminants
12676 -- because this routine makes use of them.
12678 Set_Is_Tagged_Type (T, Is_Tagged);
12679 Set_Is_Limited_Record (T, Limited_Present (Def));
12681 -- Type is abstract if full declaration carries keyword, or if
12682 -- previous partial view did.
12684 Set_Is_Abstract (T, Is_Abstract (T) or else Abstract_Present (Def));
12686 Set_Ekind (T, E_Record_Type);
12687 Set_Etype (T, T);
12688 Init_Size_Align (T);
12690 Set_Stored_Constraint (T, No_Elist);
12692 -- If an incomplete or private type declaration was already given for
12693 -- the type, then this scope already exists, and the discriminants have
12694 -- been declared within. We must verify that the full declaration
12695 -- matches the incomplete one.
12697 Check_Or_Process_Discriminants (N, T, Prev);
12699 Set_Is_Constrained (T, not Has_Discriminants (T));
12700 Set_Has_Delayed_Freeze (T, True);
12702 -- For tagged types add a manually analyzed component corresponding
12703 -- to the component _tag, the corresponding piece of tree will be
12704 -- expanded as part of the freezing actions if it is not a CPP_Class.
12706 if Is_Tagged then
12707 -- Do not add the tag unless we are in expansion mode.
12709 if Expander_Active then
12710 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
12711 Enter_Name (Tag_Comp);
12713 Set_Is_Tag (Tag_Comp);
12714 Set_Ekind (Tag_Comp, E_Component);
12715 Set_Etype (Tag_Comp, RTE (RE_Tag));
12716 Set_DT_Entry_Count (Tag_Comp, No_Uint);
12717 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
12718 Init_Component_Location (Tag_Comp);
12719 end if;
12721 Make_Class_Wide_Type (T);
12722 Set_Primitive_Operations (T, New_Elmt_List);
12723 end if;
12725 -- We must suppress range checks when processing the components
12726 -- of a record in the presence of discriminants, since we don't
12727 -- want spurious checks to be generated during their analysis, but
12728 -- must reset the Suppress_Range_Checks flags after having processed
12729 -- the record definition.
12731 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
12732 Set_Kill_Range_Checks (T, True);
12733 Record_Type_Definition (Def, Prev);
12734 Set_Kill_Range_Checks (T, False);
12735 else
12736 Record_Type_Definition (Def, Prev);
12737 end if;
12739 -- Exit from record scope
12741 End_Scope;
12742 end Record_Type_Declaration;
12744 ----------------------------
12745 -- Record_Type_Definition --
12746 ----------------------------
12748 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
12749 Component : Entity_Id;
12750 Ctrl_Components : Boolean := False;
12751 Final_Storage_Only : Boolean;
12752 T : Entity_Id;
12754 begin
12755 if Ekind (Prev_T) = E_Incomplete_Type then
12756 T := Full_View (Prev_T);
12757 else
12758 T := Prev_T;
12759 end if;
12761 Final_Storage_Only := not Is_Controlled (T);
12763 -- If the component list of a record type is defined by the reserved
12764 -- word null and there is no discriminant part, then the record type has
12765 -- no components and all records of the type are null records (RM 3.7)
12766 -- This procedure is also called to process the extension part of a
12767 -- record extension, in which case the current scope may have inherited
12768 -- components.
12770 if No (Def)
12771 or else No (Component_List (Def))
12772 or else Null_Present (Component_List (Def))
12773 then
12774 null;
12776 else
12777 Analyze_Declarations (Component_Items (Component_List (Def)));
12779 if Present (Variant_Part (Component_List (Def))) then
12780 Analyze (Variant_Part (Component_List (Def)));
12781 end if;
12782 end if;
12784 -- After completing the semantic analysis of the record definition,
12785 -- record components, both new and inherited, are accessible. Set
12786 -- their kind accordingly.
12788 Component := First_Entity (Current_Scope);
12789 while Present (Component) loop
12791 if Ekind (Component) = E_Void then
12792 Set_Ekind (Component, E_Component);
12793 Init_Component_Location (Component);
12794 end if;
12796 if Has_Task (Etype (Component)) then
12797 Set_Has_Task (T);
12798 end if;
12800 if Ekind (Component) /= E_Component then
12801 null;
12803 elsif Has_Controlled_Component (Etype (Component))
12804 or else (Chars (Component) /= Name_uParent
12805 and then Is_Controlled (Etype (Component)))
12806 then
12807 Set_Has_Controlled_Component (T, True);
12808 Final_Storage_Only := Final_Storage_Only
12809 and then Finalize_Storage_Only (Etype (Component));
12810 Ctrl_Components := True;
12811 end if;
12813 Next_Entity (Component);
12814 end loop;
12816 -- A type is Finalize_Storage_Only only if all its controlled
12817 -- components are so.
12819 if Ctrl_Components then
12820 Set_Finalize_Storage_Only (T, Final_Storage_Only);
12821 end if;
12823 -- Place reference to end record on the proper entity, which may
12824 -- be a partial view.
12826 if Present (Def) then
12827 Process_End_Label (Def, 'e', Prev_T);
12828 end if;
12829 end Record_Type_Definition;
12831 ------------------------
12832 -- Replace_Components --
12833 ------------------------
12835 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
12836 function Process (N : Node_Id) return Traverse_Result;
12838 -------------
12839 -- Process --
12840 -------------
12842 function Process (N : Node_Id) return Traverse_Result is
12843 Comp : Entity_Id;
12845 begin
12846 if Nkind (N) = N_Discriminant_Specification then
12847 Comp := First_Discriminant (Typ);
12849 while Present (Comp) loop
12850 if Chars (Comp) = Chars (Defining_Identifier (N)) then
12851 Set_Defining_Identifier (N, Comp);
12852 exit;
12853 end if;
12855 Next_Discriminant (Comp);
12856 end loop;
12858 elsif Nkind (N) = N_Component_Declaration then
12859 Comp := First_Component (Typ);
12861 while Present (Comp) loop
12862 if Chars (Comp) = Chars (Defining_Identifier (N)) then
12863 Set_Defining_Identifier (N, Comp);
12864 exit;
12865 end if;
12867 Next_Component (Comp);
12868 end loop;
12869 end if;
12871 return OK;
12872 end Process;
12874 procedure Replace is new Traverse_Proc (Process);
12876 -- Start of processing for Replace_Components
12878 begin
12879 Replace (Decl);
12880 end Replace_Components;
12882 -------------------------------
12883 -- Set_Completion_Referenced --
12884 -------------------------------
12886 procedure Set_Completion_Referenced (E : Entity_Id) is
12887 begin
12888 -- If in main unit, mark entity that is a completion as referenced,
12889 -- warnings go on the partial view when needed.
12891 if In_Extended_Main_Source_Unit (E) then
12892 Set_Referenced (E);
12893 end if;
12894 end Set_Completion_Referenced;
12896 ---------------------
12897 -- Set_Fixed_Range --
12898 ---------------------
12900 -- The range for fixed-point types is complicated by the fact that we
12901 -- do not know the exact end points at the time of the declaration. This
12902 -- is true for three reasons:
12904 -- A size clause may affect the fudging of the end-points
12905 -- A small clause may affect the values of the end-points
12906 -- We try to include the end-points if it does not affect the size
12908 -- This means that the actual end-points must be established at the
12909 -- point when the type is frozen. Meanwhile, we first narrow the range
12910 -- as permitted (so that it will fit if necessary in a small specified
12911 -- size), and then build a range subtree with these narrowed bounds.
12913 -- Set_Fixed_Range constructs the range from real literal values, and
12914 -- sets the range as the Scalar_Range of the given fixed-point type
12915 -- entity.
12917 -- The parent of this range is set to point to the entity so that it
12918 -- is properly hooked into the tree (unlike normal Scalar_Range entries
12919 -- for other scalar types, which are just pointers to the range in the
12920 -- original tree, this would otherwise be an orphan).
12922 -- The tree is left unanalyzed. When the type is frozen, the processing
12923 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
12924 -- analyzed, and uses this as an indication that it should complete
12925 -- work on the range (it will know the final small and size values).
12927 procedure Set_Fixed_Range
12928 (E : Entity_Id;
12929 Loc : Source_Ptr;
12930 Lo : Ureal;
12931 Hi : Ureal)
12933 S : constant Node_Id :=
12934 Make_Range (Loc,
12935 Low_Bound => Make_Real_Literal (Loc, Lo),
12936 High_Bound => Make_Real_Literal (Loc, Hi));
12938 begin
12939 Set_Scalar_Range (E, S);
12940 Set_Parent (S, E);
12941 end Set_Fixed_Range;
12943 ----------------------------------
12944 -- Set_Scalar_Range_For_Subtype --
12945 ----------------------------------
12947 procedure Set_Scalar_Range_For_Subtype
12948 (Def_Id : Entity_Id;
12949 R : Node_Id;
12950 Subt : Entity_Id)
12952 Kind : constant Entity_Kind := Ekind (Def_Id);
12953 begin
12954 Set_Scalar_Range (Def_Id, R);
12956 -- We need to link the range into the tree before resolving it so
12957 -- that types that are referenced, including importantly the subtype
12958 -- itself, are properly frozen (Freeze_Expression requires that the
12959 -- expression be properly linked into the tree). Of course if it is
12960 -- already linked in, then we do not disturb the current link.
12962 if No (Parent (R)) then
12963 Set_Parent (R, Def_Id);
12964 end if;
12966 -- Reset the kind of the subtype during analysis of the range, to
12967 -- catch possible premature use in the bounds themselves.
12969 Set_Ekind (Def_Id, E_Void);
12970 Process_Range_Expr_In_Decl (R, Subt);
12971 Set_Ekind (Def_Id, Kind);
12973 end Set_Scalar_Range_For_Subtype;
12975 --------------------------------------------------------
12976 -- Set_Stored_Constraint_From_Discriminant_Constraint --
12977 --------------------------------------------------------
12979 procedure Set_Stored_Constraint_From_Discriminant_Constraint
12980 (E : Entity_Id)
12982 begin
12983 -- Make sure set if encountered during
12984 -- Expand_To_Stored_Constraint
12986 Set_Stored_Constraint (E, No_Elist);
12988 -- Give it the right value
12990 if Is_Constrained (E) and then Has_Discriminants (E) then
12991 Set_Stored_Constraint (E,
12992 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
12993 end if;
12995 end Set_Stored_Constraint_From_Discriminant_Constraint;
12997 -------------------------------------
12998 -- Signed_Integer_Type_Declaration --
12999 -------------------------------------
13001 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13002 Implicit_Base : Entity_Id;
13003 Base_Typ : Entity_Id;
13004 Lo_Val : Uint;
13005 Hi_Val : Uint;
13006 Errs : Boolean := False;
13007 Lo : Node_Id;
13008 Hi : Node_Id;
13010 function Can_Derive_From (E : Entity_Id) return Boolean;
13011 -- Determine whether given bounds allow derivation from specified type
13013 procedure Check_Bound (Expr : Node_Id);
13014 -- Check bound to make sure it is integral and static. If not, post
13015 -- appropriate error message and set Errs flag
13017 ---------------------
13018 -- Can_Derive_From --
13019 ---------------------
13021 function Can_Derive_From (E : Entity_Id) return Boolean is
13022 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
13023 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
13025 begin
13026 -- Note we check both bounds against both end values, to deal with
13027 -- strange types like ones with a range of 0 .. -12341234.
13029 return Lo <= Lo_Val and then Lo_Val <= Hi
13030 and then
13031 Lo <= Hi_Val and then Hi_Val <= Hi;
13032 end Can_Derive_From;
13034 -----------------
13035 -- Check_Bound --
13036 -----------------
13038 procedure Check_Bound (Expr : Node_Id) is
13039 begin
13040 -- If a range constraint is used as an integer type definition, each
13041 -- bound of the range must be defined by a static expression of some
13042 -- integer type, but the two bounds need not have the same integer
13043 -- type (Negative bounds are allowed.) (RM 3.5.4)
13045 if not Is_Integer_Type (Etype (Expr)) then
13046 Error_Msg_N
13047 ("integer type definition bounds must be of integer type", Expr);
13048 Errs := True;
13050 elsif not Is_OK_Static_Expression (Expr) then
13051 Flag_Non_Static_Expr
13052 ("non-static expression used for integer type bound!", Expr);
13053 Errs := True;
13055 -- The bounds are folded into literals, and we set their type to be
13056 -- universal, to avoid typing difficulties: we cannot set the type
13057 -- of the literal to the new type, because this would be a forward
13058 -- reference for the back end, and if the original type is user-
13059 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
13061 else
13062 if Is_Entity_Name (Expr) then
13063 Fold_Uint (Expr, Expr_Value (Expr), True);
13064 end if;
13066 Set_Etype (Expr, Universal_Integer);
13067 end if;
13068 end Check_Bound;
13070 -- Start of processing for Signed_Integer_Type_Declaration
13072 begin
13073 -- Create an anonymous base type
13075 Implicit_Base :=
13076 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
13078 -- Analyze and check the bounds, they can be of any integer type
13080 Lo := Low_Bound (Def);
13081 Hi := High_Bound (Def);
13083 -- Arbitrarily use Integer as the type if either bound had an error
13085 if Hi = Error or else Lo = Error then
13086 Base_Typ := Any_Integer;
13087 Set_Error_Posted (T, True);
13089 -- Here both bounds are OK expressions
13091 else
13092 Analyze_And_Resolve (Lo, Any_Integer);
13093 Analyze_And_Resolve (Hi, Any_Integer);
13095 Check_Bound (Lo);
13096 Check_Bound (Hi);
13098 if Errs then
13099 Hi := Type_High_Bound (Standard_Long_Long_Integer);
13100 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
13101 end if;
13103 -- Find type to derive from
13105 Lo_Val := Expr_Value (Lo);
13106 Hi_Val := Expr_Value (Hi);
13108 if Can_Derive_From (Standard_Short_Short_Integer) then
13109 Base_Typ := Base_Type (Standard_Short_Short_Integer);
13111 elsif Can_Derive_From (Standard_Short_Integer) then
13112 Base_Typ := Base_Type (Standard_Short_Integer);
13114 elsif Can_Derive_From (Standard_Integer) then
13115 Base_Typ := Base_Type (Standard_Integer);
13117 elsif Can_Derive_From (Standard_Long_Integer) then
13118 Base_Typ := Base_Type (Standard_Long_Integer);
13120 elsif Can_Derive_From (Standard_Long_Long_Integer) then
13121 Base_Typ := Base_Type (Standard_Long_Long_Integer);
13123 else
13124 Base_Typ := Base_Type (Standard_Long_Long_Integer);
13125 Error_Msg_N ("integer type definition bounds out of range", Def);
13126 Hi := Type_High_Bound (Standard_Long_Long_Integer);
13127 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
13128 end if;
13129 end if;
13131 -- Complete both implicit base and declared first subtype entities
13133 Set_Etype (Implicit_Base, Base_Typ);
13134 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
13135 Set_Size_Info (Implicit_Base, (Base_Typ));
13136 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
13137 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13139 Set_Ekind (T, E_Signed_Integer_Subtype);
13140 Set_Etype (T, Implicit_Base);
13142 Set_Size_Info (T, (Implicit_Base));
13143 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13144 Set_Scalar_Range (T, Def);
13145 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
13146 Set_Is_Constrained (T);
13147 end Signed_Integer_Type_Declaration;
13149 end Sem_Ch3;