* tree-ssa-pre.c (grand_bitmap_obstack): New.
[official-gcc.git] / gcc / ada / freeze.adb
blobcee69c4776254ae60f93c1def3384308ab7ee081
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- F R E E Z E --
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 Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Ch7; use Exp_Ch7;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Pakd; use Exp_Pakd;
35 with Exp_Util; use Exp_Util;
36 with Exp_Tss; use Exp_Tss;
37 with Layout; use Layout;
38 with Lib.Xref; use Lib.Xref;
39 with Nlists; use Nlists;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
44 with Sem; use Sem;
45 with Sem_Cat; use Sem_Cat;
46 with Sem_Ch6; use Sem_Ch6;
47 with Sem_Ch7; use Sem_Ch7;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Ch13; use Sem_Ch13;
50 with Sem_Eval; use Sem_Eval;
51 with Sem_Mech; use Sem_Mech;
52 with Sem_Prag; use Sem_Prag;
53 with Sem_Res; use Sem_Res;
54 with Sem_Util; use Sem_Util;
55 with Sinfo; use Sinfo;
56 with Snames; use Snames;
57 with Stand; use Stand;
58 with Targparm; use Targparm;
59 with Tbuild; use Tbuild;
60 with Ttypes; use Ttypes;
61 with Uintp; use Uintp;
62 with Urealp; use Urealp;
64 package body Freeze is
66 -----------------------
67 -- Local Subprograms --
68 -----------------------
70 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
71 -- Typ is a type that is being frozen. If no size clause is given,
72 -- but a default Esize has been computed, then this default Esize is
73 -- adjusted up if necessary to be consistent with a given alignment,
74 -- but never to a value greater than Long_Long_Integer'Size. This
75 -- is used for all discrete types and for fixed-point types.
77 procedure Build_And_Analyze_Renamed_Body
78 (Decl : Node_Id;
79 New_S : Entity_Id;
80 After : in out Node_Id);
81 -- Build body for a renaming declaration, insert in tree and analyze.
83 procedure Check_Address_Clause (E : Entity_Id);
84 -- Apply legality checks to address clauses for object declarations,
85 -- at the point the object is frozen.
87 procedure Check_Strict_Alignment (E : Entity_Id);
88 -- E is a base type. If E is tagged or has a component that is aliased
89 -- or tagged or contains something this is aliased or tagged, set
90 -- Strict_Alignment.
92 procedure Check_Unsigned_Type (E : Entity_Id);
93 pragma Inline (Check_Unsigned_Type);
94 -- If E is a fixed-point or discrete type, then all the necessary work
95 -- to freeze it is completed except for possible setting of the flag
96 -- Is_Unsigned_Type, which is done by this procedure. The call has no
97 -- effect if the entity E is not a discrete or fixed-point type.
99 procedure Freeze_And_Append
100 (Ent : Entity_Id;
101 Loc : Source_Ptr;
102 Result : in out List_Id);
103 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
104 -- nodes to Result, modifying Result from No_List if necessary.
106 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
107 -- Freeze enumeration type. The Esize field is set as processing
108 -- proceeds (i.e. set by default when the type is declared and then
109 -- adjusted by rep clauses. What this procedure does is to make sure
110 -- that if a foreign convention is specified, and no specific size
111 -- is given, then the size must be at least Integer'Size.
113 procedure Freeze_Static_Object (E : Entity_Id);
114 -- If an object is frozen which has Is_Statically_Allocated set, then
115 -- all referenced types must also be marked with this flag. This routine
116 -- is in charge of meeting this requirement for the object entity E.
118 procedure Freeze_Subprogram (E : Entity_Id);
119 -- Perform freezing actions for a subprogram (create extra formals,
120 -- and set proper default mechanism values). Note that this routine
121 -- is not called for internal subprograms, for which neither of these
122 -- actions is needed (or desirable, we do not want for example to have
123 -- these extra formals present in initialization procedures, where they
124 -- would serve no purpose). In this call E is either a subprogram or
125 -- a subprogram type (i.e. an access to a subprogram).
127 function Is_Fully_Defined (T : Entity_Id) return Boolean;
128 -- True if T is not private and has no private components, or has a full
129 -- view. Used to determine whether the designated type of an access type
130 -- should be frozen when the access type is frozen. This is done when an
131 -- allocator is frozen, or an expression that may involve attributes of
132 -- the designated type. Otherwise freezing the access type does not freeze
133 -- the designated type.
135 procedure Process_Default_Expressions
136 (E : Entity_Id;
137 After : in out Node_Id);
138 -- This procedure is called for each subprogram to complete processing
139 -- of default expressions at the point where all types are known to be
140 -- frozen. The expressions must be analyzed in full, to make sure that
141 -- all error processing is done (they have only been pre-analyzed). If
142 -- the expression is not an entity or literal, its analysis may generate
143 -- code which must not be executed. In that case we build a function
144 -- body to hold that code. This wrapper function serves no other purpose
145 -- (it used to be called to evaluate the default, but now the default is
146 -- inlined at each point of call).
148 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
149 -- Typ is a record or array type that is being frozen. This routine
150 -- sets the default component alignment from the scope stack values
151 -- if the alignment is otherwise not specified.
153 procedure Check_Debug_Info_Needed (T : Entity_Id);
154 -- As each entity is frozen, this routine is called to deal with the
155 -- setting of Debug_Info_Needed for the entity. This flag is set if
156 -- the entity comes from source, or if we are in Debug_Generated_Code
157 -- mode or if the -gnatdV debug flag is set. However, it never sets
158 -- the flag if Debug_Info_Off is set.
160 procedure Set_Debug_Info_Needed (T : Entity_Id);
161 -- Sets the Debug_Info_Needed flag on entity T if not already set, and
162 -- also on any entities that are needed by T (for an object, the type
163 -- of the object is needed, and for a type, the subsidiary types are
164 -- needed -- see body for details). Never has any effect on T if the
165 -- Debug_Info_Off flag is set.
167 procedure Warn_Overlay
168 (Expr : Node_Id;
169 Typ : Entity_Id;
170 Nam : Node_Id);
171 -- Expr is the expression for an address clause for entity Nam whose type
172 -- is Typ. If Typ has a default initialization, and there is no explicit
173 -- initialization in the source declaration, check whether the address
174 -- clause might cause overlaying of an entity, and emit a warning on the
175 -- side effect that the initialization will cause.
177 -------------------------------
178 -- Adjust_Esize_For_Alignment --
179 -------------------------------
181 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
182 Align : Uint;
184 begin
185 if Known_Esize (Typ) and then Known_Alignment (Typ) then
186 Align := Alignment_In_Bits (Typ);
188 if Align > Esize (Typ)
189 and then Align <= Standard_Long_Long_Integer_Size
190 then
191 Set_Esize (Typ, Align);
192 end if;
193 end if;
194 end Adjust_Esize_For_Alignment;
196 ------------------------------------
197 -- Build_And_Analyze_Renamed_Body --
198 ------------------------------------
200 procedure Build_And_Analyze_Renamed_Body
201 (Decl : Node_Id;
202 New_S : Entity_Id;
203 After : in out Node_Id)
205 Body_Node : constant Node_Id := Build_Renamed_Body (Decl, New_S);
207 begin
208 Insert_After (After, Body_Node);
209 Mark_Rewrite_Insertion (Body_Node);
210 Analyze (Body_Node);
211 After := Body_Node;
212 end Build_And_Analyze_Renamed_Body;
214 ------------------------
215 -- Build_Renamed_Body --
216 ------------------------
218 function Build_Renamed_Body
219 (Decl : Node_Id;
220 New_S : Entity_Id) return Node_Id
222 Loc : constant Source_Ptr := Sloc (New_S);
223 -- We use for the source location of the renamed body, the location
224 -- of the spec entity. It might seem more natural to use the location
225 -- of the renaming declaration itself, but that would be wrong, since
226 -- then the body we create would look as though it was created far
227 -- too late, and this could cause problems with elaboration order
228 -- analysis, particularly in connection with instantiations.
230 N : constant Node_Id := Unit_Declaration_Node (New_S);
231 Nam : constant Node_Id := Name (N);
232 Old_S : Entity_Id;
233 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
234 Actuals : List_Id := No_List;
235 Call_Node : Node_Id;
236 Call_Name : Node_Id;
237 Body_Node : Node_Id;
238 Formal : Entity_Id;
239 O_Formal : Entity_Id;
240 Param_Spec : Node_Id;
242 begin
243 -- Determine the entity being renamed, which is the target of the
244 -- call statement. If the name is an explicit dereference, this is
245 -- a renaming of a subprogram type rather than a subprogram. The
246 -- name itself is fully analyzed.
248 if Nkind (Nam) = N_Selected_Component then
249 Old_S := Entity (Selector_Name (Nam));
251 elsif Nkind (Nam) = N_Explicit_Dereference then
252 Old_S := Etype (Nam);
254 elsif Nkind (Nam) = N_Indexed_Component then
255 if Is_Entity_Name (Prefix (Nam)) then
256 Old_S := Entity (Prefix (Nam));
257 else
258 Old_S := Entity (Selector_Name (Prefix (Nam)));
259 end if;
261 elsif Nkind (Nam) = N_Character_Literal then
262 Old_S := Etype (New_S);
264 else
265 Old_S := Entity (Nam);
266 end if;
268 if Is_Entity_Name (Nam) then
270 -- If the renamed entity is a predefined operator, retain full
271 -- name to ensure its visibility.
273 if Ekind (Old_S) = E_Operator
274 and then Nkind (Nam) = N_Expanded_Name
275 then
276 Call_Name := New_Copy (Name (N));
277 else
278 Call_Name := New_Reference_To (Old_S, Loc);
279 end if;
281 else
282 Call_Name := New_Copy (Name (N));
284 -- The original name may have been overloaded, but
285 -- is fully resolved now.
287 Set_Is_Overloaded (Call_Name, False);
288 end if;
290 -- For simple renamings, subsequent calls can be expanded directly
291 -- as called to the renamed entity. The body must be generated in
292 -- any case for calls they may appear elsewhere.
294 if (Ekind (Old_S) = E_Function
295 or else Ekind (Old_S) = E_Procedure)
296 and then Nkind (Decl) = N_Subprogram_Declaration
297 then
298 Set_Body_To_Inline (Decl, Old_S);
299 end if;
301 -- The body generated for this renaming is an internal artifact, and
302 -- does not constitute a freeze point for the called entity.
304 Set_Must_Not_Freeze (Call_Name);
306 Formal := First_Formal (Defining_Entity (Decl));
308 if Present (Formal) then
309 Actuals := New_List;
311 while Present (Formal) loop
312 Append (New_Reference_To (Formal, Loc), Actuals);
313 Next_Formal (Formal);
314 end loop;
315 end if;
317 -- If the renamed entity is an entry, inherit its profile. For
318 -- other renamings as bodies, both profiles must be subtype
319 -- conformant, so it is not necessary to replace the profile given
320 -- in the declaration. However, default values that are aggregates
321 -- are rewritten when partially analyzed, so we recover the original
322 -- aggregate to insure that subsequent conformity checking works.
323 -- Similarly, if the default expression was constant-folded, recover
324 -- the original expression.
326 Formal := First_Formal (Defining_Entity (Decl));
328 if Present (Formal) then
329 O_Formal := First_Formal (Old_S);
330 Param_Spec := First (Parameter_Specifications (Spec));
332 while Present (Formal) loop
333 if Is_Entry (Old_S) then
335 if Nkind (Parameter_Type (Param_Spec)) /=
336 N_Access_Definition
337 then
338 Set_Etype (Formal, Etype (O_Formal));
339 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
340 end if;
342 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
343 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
344 Nkind (Default_Value (O_Formal))
345 then
346 Set_Expression (Param_Spec,
347 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
348 end if;
350 Next_Formal (Formal);
351 Next_Formal (O_Formal);
352 Next (Param_Spec);
353 end loop;
354 end if;
356 -- If the renamed entity is a function, the generated body contains a
357 -- return statement. Otherwise, build a procedure call. If the entity is
358 -- an entry, subsequent analysis of the call will transform it into the
359 -- proper entry or protected operation call. If the renamed entity is
360 -- a character literal, return it directly.
362 if Ekind (Old_S) = E_Function
363 or else Ekind (Old_S) = E_Operator
364 or else (Ekind (Old_S) = E_Subprogram_Type
365 and then Etype (Old_S) /= Standard_Void_Type)
366 then
367 Call_Node :=
368 Make_Return_Statement (Loc,
369 Expression =>
370 Make_Function_Call (Loc,
371 Name => Call_Name,
372 Parameter_Associations => Actuals));
374 elsif Ekind (Old_S) = E_Enumeration_Literal then
375 Call_Node :=
376 Make_Return_Statement (Loc,
377 Expression => New_Occurrence_Of (Old_S, Loc));
379 elsif Nkind (Nam) = N_Character_Literal then
380 Call_Node :=
381 Make_Return_Statement (Loc,
382 Expression => Call_Name);
384 else
385 Call_Node :=
386 Make_Procedure_Call_Statement (Loc,
387 Name => Call_Name,
388 Parameter_Associations => Actuals);
389 end if;
391 -- Create entities for subprogram body and formals.
393 Set_Defining_Unit_Name (Spec,
394 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
396 Param_Spec := First (Parameter_Specifications (Spec));
398 while Present (Param_Spec) loop
399 Set_Defining_Identifier (Param_Spec,
400 Make_Defining_Identifier (Loc,
401 Chars => Chars (Defining_Identifier (Param_Spec))));
402 Next (Param_Spec);
403 end loop;
405 Body_Node :=
406 Make_Subprogram_Body (Loc,
407 Specification => Spec,
408 Declarations => New_List,
409 Handled_Statement_Sequence =>
410 Make_Handled_Sequence_Of_Statements (Loc,
411 Statements => New_List (Call_Node)));
413 if Nkind (Decl) /= N_Subprogram_Declaration then
414 Rewrite (N,
415 Make_Subprogram_Declaration (Loc,
416 Specification => Specification (N)));
417 end if;
419 -- Link the body to the entity whose declaration it completes. If
420 -- the body is analyzed when the renamed entity is frozen, it may be
421 -- necessary to restore the proper scope (see package Exp_Ch13).
423 if Nkind (N) = N_Subprogram_Renaming_Declaration
424 and then Present (Corresponding_Spec (N))
425 then
426 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
427 else
428 Set_Corresponding_Spec (Body_Node, New_S);
429 end if;
431 return Body_Node;
432 end Build_Renamed_Body;
434 --------------------------
435 -- Check_Address_Clause --
436 --------------------------
438 procedure Check_Address_Clause (E : Entity_Id) is
439 Addr : constant Node_Id := Address_Clause (E);
440 Expr : Node_Id;
441 Decl : constant Node_Id := Declaration_Node (E);
442 Typ : constant Entity_Id := Etype (E);
444 begin
445 if Present (Addr) then
446 Expr := Expression (Addr);
448 -- If we have no initialization of any kind, then we don't
449 -- need to place any restrictions on the address clause, because
450 -- the object will be elaborated after the address clause is
451 -- evaluated. This happens if the declaration has no initial
452 -- expression, or the type has no implicit initialization, or
453 -- the object is imported.
455 -- The same holds for all initialized scalar types and all
456 -- access types. Packed bit arrays of size up to 64 are
457 -- represented using a modular type with an initialization
458 -- (to zero) and can be processed like other initialized
459 -- scalar types.
461 -- If the type is controlled, code to attach the object to a
462 -- finalization chain is generated at the point of declaration,
463 -- and therefore the elaboration of the object cannot be delayed:
464 -- the address expression must be a constant.
466 if (No (Expression (Decl))
467 and then not Controlled_Type (Typ)
468 and then
469 (not Has_Non_Null_Base_Init_Proc (Typ)
470 or else Is_Imported (E)))
472 or else
473 (Present (Expression (Decl))
474 and then Is_Scalar_Type (Typ))
476 or else
477 Is_Access_Type (Typ)
479 or else
480 (Is_Bit_Packed_Array (Typ)
481 and then
482 Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
483 then
484 null;
486 -- Otherwise, we require the address clause to be constant
487 -- because the call to the initialization procedure (or the
488 -- attach code) has to happen at the point of the declaration.
490 else
491 Check_Constant_Address_Clause (Expr, E);
492 Set_Has_Delayed_Freeze (E, False);
493 end if;
495 if not Error_Posted (Expr)
496 and then not Controlled_Type (Typ)
497 then
498 Warn_Overlay (Expr, Typ, Name (Addr));
499 end if;
500 end if;
501 end Check_Address_Clause;
503 -----------------------------
504 -- Check_Compile_Time_Size --
505 -----------------------------
507 procedure Check_Compile_Time_Size (T : Entity_Id) is
509 procedure Set_Small_Size (S : Uint);
510 -- Sets the compile time known size (32 bits or less) in the Esize
511 -- field, checking for a size clause that was given which attempts
512 -- to give a smaller size.
514 function Size_Known (T : Entity_Id) return Boolean;
515 -- Recursive function that does all the work
517 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
518 -- If T is a constrained subtype, its size is not known if any of its
519 -- discriminant constraints is not static and it is not a null record.
520 -- The test is conservative and doesn't check that the components are
521 -- in fact constrained by non-static discriminant values. Could be made
522 -- more precise ???
524 --------------------
525 -- Set_Small_Size --
526 --------------------
528 procedure Set_Small_Size (S : Uint) is
529 begin
530 if S > 32 then
531 return;
533 elsif Has_Size_Clause (T) then
534 if RM_Size (T) < S then
535 Error_Msg_Uint_1 := S;
536 Error_Msg_NE
537 ("size for & is too small, minimum is ^",
538 Size_Clause (T), T);
540 elsif Unknown_Esize (T) then
541 Set_Esize (T, S);
542 end if;
544 -- Set sizes if not set already
546 else
547 if Unknown_Esize (T) then
548 Set_Esize (T, S);
549 end if;
551 if Unknown_RM_Size (T) then
552 Set_RM_Size (T, S);
553 end if;
554 end if;
555 end Set_Small_Size;
557 ----------------
558 -- Size_Known --
559 ----------------
561 function Size_Known (T : Entity_Id) return Boolean is
562 Index : Entity_Id;
563 Comp : Entity_Id;
564 Ctyp : Entity_Id;
565 Low : Node_Id;
566 High : Node_Id;
568 begin
569 if Size_Known_At_Compile_Time (T) then
570 return True;
572 elsif Is_Scalar_Type (T)
573 or else Is_Task_Type (T)
574 then
575 return not Is_Generic_Type (T);
577 elsif Is_Array_Type (T) then
578 if Ekind (T) = E_String_Literal_Subtype then
579 Set_Small_Size (Component_Size (T) * String_Literal_Length (T));
580 return True;
582 elsif not Is_Constrained (T) then
583 return False;
585 -- Don't do any recursion on type with error posted, since
586 -- we may have a malformed type that leads us into a loop
588 elsif Error_Posted (T) then
589 return False;
591 elsif not Size_Known (Component_Type (T)) then
592 return False;
593 end if;
595 -- Check for all indexes static, and also compute possible
596 -- size (in case it is less than 32 and may be packable).
598 declare
599 Esiz : Uint := Component_Size (T);
600 Dim : Uint;
602 begin
603 Index := First_Index (T);
604 while Present (Index) loop
605 if Nkind (Index) = N_Range then
606 Get_Index_Bounds (Index, Low, High);
608 elsif Error_Posted (Scalar_Range (Etype (Index))) then
609 return False;
611 else
612 Low := Type_Low_Bound (Etype (Index));
613 High := Type_High_Bound (Etype (Index));
614 end if;
616 if not Compile_Time_Known_Value (Low)
617 or else not Compile_Time_Known_Value (High)
618 or else Etype (Index) = Any_Type
619 then
620 return False;
622 else
623 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
625 if Dim >= 0 then
626 Esiz := Esiz * Dim;
627 else
628 Esiz := Uint_0;
629 end if;
630 end if;
632 Next_Index (Index);
633 end loop;
635 Set_Small_Size (Esiz);
636 return True;
637 end;
639 elsif Is_Access_Type (T) then
640 return True;
642 elsif Is_Private_Type (T)
643 and then not Is_Generic_Type (T)
644 and then Present (Underlying_Type (T))
645 then
646 -- Don't do any recursion on type with error posted, since
647 -- we may have a malformed type that leads us into a loop
649 if Error_Posted (T) then
650 return False;
651 else
652 return Size_Known (Underlying_Type (T));
653 end if;
655 elsif Is_Record_Type (T) then
657 -- A class-wide type is never considered to have a known size
659 if Is_Class_Wide_Type (T) then
660 return False;
662 -- A subtype of a variant record must not have non-static
663 -- discriminanted components.
665 elsif T /= Base_Type (T)
666 and then not Static_Discriminated_Components (T)
667 then
668 return False;
670 -- Don't do any recursion on type with error posted, since
671 -- we may have a malformed type that leads us into a loop
673 elsif Error_Posted (T) then
674 return False;
675 end if;
677 -- Now look at the components of the record
679 declare
680 -- The following two variables are used to keep track of
681 -- the size of packed records if we can tell the size of
682 -- the packed record in the front end. Packed_Size_Known
683 -- is True if so far we can figure out the size. It is
684 -- initialized to True for a packed record, unless the
685 -- record has discriminants. The reason we eliminate the
686 -- discriminated case is that we don't know the way the
687 -- back end lays out discriminated packed records. If
688 -- Packed_Size_Known is True, then Packed_Size is the
689 -- size in bits so far.
691 Packed_Size_Known : Boolean :=
692 Is_Packed (T)
693 and then not Has_Discriminants (T);
695 Packed_Size : Uint := Uint_0;
697 begin
698 -- Test for variant part present
700 if Has_Discriminants (T)
701 and then Present (Parent (T))
702 and then Nkind (Parent (T)) = N_Full_Type_Declaration
703 and then Nkind (Type_Definition (Parent (T))) =
704 N_Record_Definition
705 and then not Null_Present (Type_Definition (Parent (T)))
706 and then Present (Variant_Part
707 (Component_List (Type_Definition (Parent (T)))))
708 then
709 -- If variant part is present, and type is unconstrained,
710 -- then we must have defaulted discriminants, or a size
711 -- clause must be present for the type, or else the size
712 -- is definitely not known at compile time.
714 if not Is_Constrained (T)
715 and then
716 No (Discriminant_Default_Value
717 (First_Discriminant (T)))
718 and then Unknown_Esize (T)
719 then
720 return False;
721 end if;
722 end if;
724 -- Loop through components
726 Comp := First_Entity (T);
727 while Present (Comp) loop
728 if Ekind (Comp) = E_Component
729 or else
730 Ekind (Comp) = E_Discriminant
731 then
732 Ctyp := Etype (Comp);
734 -- We do not know the packed size if there is a
735 -- component clause present (we possibly could,
736 -- but this would only help in the case of a record
737 -- with partial rep clauses. That's because in the
738 -- case of full rep clauses, the size gets figured
739 -- out anyway by a different circuit).
741 if Present (Component_Clause (Comp)) then
742 Packed_Size_Known := False;
743 end if;
745 -- We need to identify a component that is an array
746 -- where the index type is an enumeration type with
747 -- non-standard representation, and some bound of the
748 -- type depends on a discriminant.
750 -- This is because gigi computes the size by doing a
751 -- substituation of the appropriate discriminant value
752 -- in the size expression for the base type, and gigi
753 -- is not clever enough to evaluate the resulting
754 -- expression (which involves a call to rep_to_pos)
755 -- at compile time.
757 -- It would be nice if gigi would either recognize that
758 -- this expression can be computed at compile time, or
759 -- alternatively figured out the size from the subtype
760 -- directly, where all the information is at hand ???
762 if Is_Array_Type (Etype (Comp))
763 and then Present (Packed_Array_Type (Etype (Comp)))
764 then
765 declare
766 Ocomp : constant Entity_Id :=
767 Original_Record_Component (Comp);
768 OCtyp : constant Entity_Id := Etype (Ocomp);
769 Ind : Node_Id;
770 Indtyp : Entity_Id;
771 Lo, Hi : Node_Id;
773 begin
774 Ind := First_Index (OCtyp);
775 while Present (Ind) loop
776 Indtyp := Etype (Ind);
778 if Is_Enumeration_Type (Indtyp)
779 and then Has_Non_Standard_Rep (Indtyp)
780 then
781 Lo := Type_Low_Bound (Indtyp);
782 Hi := Type_High_Bound (Indtyp);
784 if Is_Entity_Name (Lo)
785 and then
786 Ekind (Entity (Lo)) = E_Discriminant
787 then
788 return False;
790 elsif Is_Entity_Name (Hi)
791 and then
792 Ekind (Entity (Hi)) = E_Discriminant
793 then
794 return False;
795 end if;
796 end if;
798 Next_Index (Ind);
799 end loop;
800 end;
801 end if;
803 -- Clearly size of record is not known if the size of
804 -- one of the components is not known.
806 if not Size_Known (Ctyp) then
807 return False;
808 end if;
810 -- Accumulate packed size if possible
812 if Packed_Size_Known then
814 -- We can only deal with elementary types, since for
815 -- non-elementary components, alignment enters into
816 -- the picture, and we don't know enough to handle
817 -- proper alignment in this context. Packed arrays
818 -- count as elementary if the representation is a
819 -- modular type.
821 if Is_Elementary_Type (Ctyp)
822 or else (Is_Array_Type (Ctyp)
823 and then
824 Present (Packed_Array_Type (Ctyp))
825 and then
826 Is_Modular_Integer_Type
827 (Packed_Array_Type (Ctyp)))
828 then
829 -- If RM_Size is known and static, then we can
830 -- keep accumulating the packed size.
832 if Known_Static_RM_Size (Ctyp) then
834 -- A little glitch, to be removed sometime ???
835 -- gigi does not understand zero sizes yet.
837 if RM_Size (Ctyp) = Uint_0 then
838 Packed_Size_Known := False;
840 -- Normal case where we can keep accumulating
841 -- the packed array size.
843 else
844 Packed_Size := Packed_Size + RM_Size (Ctyp);
845 end if;
847 -- If we have a field whose RM_Size is not known
848 -- then we can't figure out the packed size here.
850 else
851 Packed_Size_Known := False;
852 end if;
854 -- If we have a non-elementary type we can't figure
855 -- out the packed array size (alignment issues).
857 else
858 Packed_Size_Known := False;
859 end if;
860 end if;
861 end if;
863 Next_Entity (Comp);
864 end loop;
866 if Packed_Size_Known then
867 Set_Small_Size (Packed_Size);
868 end if;
870 return True;
871 end;
873 else
874 return False;
875 end if;
876 end Size_Known;
878 -------------------------------------
879 -- Static_Discriminated_Components --
880 -------------------------------------
882 function Static_Discriminated_Components
883 (T : Entity_Id) return Boolean
885 Constraint : Elmt_Id;
887 begin
888 if Has_Discriminants (T)
889 and then Present (Discriminant_Constraint (T))
890 and then Present (First_Component (T))
891 then
892 Constraint := First_Elmt (Discriminant_Constraint (T));
893 while Present (Constraint) loop
894 if not Compile_Time_Known_Value (Node (Constraint)) then
895 return False;
896 end if;
898 Next_Elmt (Constraint);
899 end loop;
900 end if;
902 return True;
903 end Static_Discriminated_Components;
905 -- Start of processing for Check_Compile_Time_Size
907 begin
908 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
909 end Check_Compile_Time_Size;
911 -----------------------------
912 -- Check_Debug_Info_Needed --
913 -----------------------------
915 procedure Check_Debug_Info_Needed (T : Entity_Id) is
916 begin
917 if Needs_Debug_Info (T) or else Debug_Info_Off (T) then
918 return;
920 elsif Comes_From_Source (T)
921 or else Debug_Generated_Code
922 or else Debug_Flag_VV
923 then
924 Set_Debug_Info_Needed (T);
925 end if;
926 end Check_Debug_Info_Needed;
928 ----------------------------
929 -- Check_Strict_Alignment --
930 ----------------------------
932 procedure Check_Strict_Alignment (E : Entity_Id) is
933 Comp : Entity_Id;
935 begin
936 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
937 Set_Strict_Alignment (E);
939 elsif Is_Array_Type (E) then
940 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
942 elsif Is_Record_Type (E) then
943 if Is_Limited_Record (E) then
944 Set_Strict_Alignment (E);
945 return;
946 end if;
948 Comp := First_Component (E);
950 while Present (Comp) loop
951 if not Is_Type (Comp)
952 and then (Strict_Alignment (Etype (Comp))
953 or else Is_Aliased (Comp))
954 then
955 Set_Strict_Alignment (E);
956 return;
957 end if;
959 Next_Component (Comp);
960 end loop;
961 end if;
962 end Check_Strict_Alignment;
964 -------------------------
965 -- Check_Unsigned_Type --
966 -------------------------
968 procedure Check_Unsigned_Type (E : Entity_Id) is
969 Ancestor : Entity_Id;
970 Lo_Bound : Node_Id;
971 Btyp : Entity_Id;
973 begin
974 if not Is_Discrete_Or_Fixed_Point_Type (E) then
975 return;
976 end if;
978 -- Do not attempt to analyze case where range was in error
980 if Error_Posted (Scalar_Range (E)) then
981 return;
982 end if;
984 -- The situation that is non trivial is something like
986 -- subtype x1 is integer range -10 .. +10;
987 -- subtype x2 is x1 range 0 .. V1;
988 -- subtype x3 is x2 range V2 .. V3;
989 -- subtype x4 is x3 range V4 .. V5;
991 -- where Vn are variables. Here the base type is signed, but we still
992 -- know that x4 is unsigned because of the lower bound of x2.
994 -- The only way to deal with this is to look up the ancestor chain
996 Ancestor := E;
997 loop
998 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
999 return;
1000 end if;
1002 Lo_Bound := Type_Low_Bound (Ancestor);
1004 if Compile_Time_Known_Value (Lo_Bound) then
1006 if Expr_Rep_Value (Lo_Bound) >= 0 then
1007 Set_Is_Unsigned_Type (E, True);
1008 end if;
1010 return;
1012 else
1013 Ancestor := Ancestor_Subtype (Ancestor);
1015 -- If no ancestor had a static lower bound, go to base type
1017 if No (Ancestor) then
1019 -- Note: the reason we still check for a compile time known
1020 -- value for the base type is that at least in the case of
1021 -- generic formals, we can have bounds that fail this test,
1022 -- and there may be other cases in error situations.
1024 Btyp := Base_Type (E);
1026 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1027 return;
1028 end if;
1030 Lo_Bound := Type_Low_Bound (Base_Type (E));
1032 if Compile_Time_Known_Value (Lo_Bound)
1033 and then Expr_Rep_Value (Lo_Bound) >= 0
1034 then
1035 Set_Is_Unsigned_Type (E, True);
1036 end if;
1038 return;
1039 end if;
1040 end if;
1041 end loop;
1042 end Check_Unsigned_Type;
1044 -----------------------------
1045 -- Expand_Atomic_Aggregate --
1046 -----------------------------
1048 procedure Expand_Atomic_Aggregate (E : Entity_Id; Typ : Entity_Id) is
1049 Loc : constant Source_Ptr := Sloc (E);
1050 New_N : Node_Id;
1051 Temp : Entity_Id;
1053 begin
1054 if (Nkind (Parent (E)) = N_Object_Declaration
1055 or else Nkind (Parent (E)) = N_Assignment_Statement)
1056 and then Comes_From_Source (Parent (E))
1057 and then Nkind (E) = N_Aggregate
1058 then
1059 Temp :=
1060 Make_Defining_Identifier (Loc,
1061 New_Internal_Name ('T'));
1063 New_N :=
1064 Make_Object_Declaration (Loc,
1065 Defining_Identifier => Temp,
1066 Object_definition => New_Occurrence_Of (Typ, Loc),
1067 Expression => Relocate_Node (E));
1068 Insert_Before (Parent (E), New_N);
1069 Analyze (New_N);
1071 Set_Expression (Parent (E), New_Occurrence_Of (Temp, Loc));
1073 -- To prevent the temporary from being constant-folded (which
1074 -- would lead to the same piecemeal assignment on the original
1075 -- target) indicate to the back-end that the temporary is a
1076 -- variable with real storage. See description of this flag
1077 -- in Einfo, and the notes on N_Assignment_Statement and
1078 -- N_Object_Declaration in Sinfo.
1080 Set_Is_True_Constant (Temp, False);
1081 end if;
1082 end Expand_Atomic_Aggregate;
1084 ----------------
1085 -- Freeze_All --
1086 ----------------
1088 -- Note: the easy coding for this procedure would be to just build a
1089 -- single list of freeze nodes and then insert them and analyze them
1090 -- all at once. This won't work, because the analysis of earlier freeze
1091 -- nodes may recursively freeze types which would otherwise appear later
1092 -- on in the freeze list. So we must analyze and expand the freeze nodes
1093 -- as they are generated.
1095 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1096 Loc : constant Source_Ptr := Sloc (After);
1097 E : Entity_Id;
1098 Decl : Node_Id;
1100 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1101 -- This is the internal recursive routine that does freezing of
1102 -- entities (but NOT the analysis of default expressions, which
1103 -- should not be recursive, we don't want to analyze those till
1104 -- we are sure that ALL the types are frozen).
1106 --------------------
1107 -- Freeze_All_Ent --
1108 --------------------
1110 procedure Freeze_All_Ent
1111 (From : Entity_Id;
1112 After : in out Node_Id)
1114 E : Entity_Id;
1115 Flist : List_Id;
1116 Lastn : Node_Id;
1118 procedure Process_Flist;
1119 -- If freeze nodes are present, insert and analyze, and reset
1120 -- cursor for next insertion.
1122 -------------------
1123 -- Process_Flist --
1124 -------------------
1126 procedure Process_Flist is
1127 begin
1128 if Is_Non_Empty_List (Flist) then
1129 Lastn := Next (After);
1130 Insert_List_After_And_Analyze (After, Flist);
1132 if Present (Lastn) then
1133 After := Prev (Lastn);
1134 else
1135 After := Last (List_Containing (After));
1136 end if;
1137 end if;
1138 end Process_Flist;
1140 -- Start or processing for Freeze_All_Ent
1142 begin
1143 E := From;
1144 while Present (E) loop
1146 -- If the entity is an inner package which is not a package
1147 -- renaming, then its entities must be frozen at this point.
1148 -- Note that such entities do NOT get frozen at the end of
1149 -- the nested package itself (only library packages freeze).
1151 -- Same is true for task declarations, where anonymous records
1152 -- created for entry parameters must be frozen.
1154 if Ekind (E) = E_Package
1155 and then No (Renamed_Object (E))
1156 and then not Is_Child_Unit (E)
1157 and then not Is_Frozen (E)
1158 then
1159 New_Scope (E);
1160 Install_Visible_Declarations (E);
1161 Install_Private_Declarations (E);
1163 Freeze_All (First_Entity (E), After);
1165 End_Package_Scope (E);
1167 elsif Ekind (E) in Task_Kind
1168 and then
1169 (Nkind (Parent (E)) = N_Task_Type_Declaration
1170 or else
1171 Nkind (Parent (E)) = N_Single_Task_Declaration)
1172 then
1173 New_Scope (E);
1174 Freeze_All (First_Entity (E), After);
1175 End_Scope;
1177 -- For a derived tagged type, we must ensure that all the
1178 -- primitive operations of the parent have been frozen, so
1179 -- that their addresses will be in the parent's dispatch table
1180 -- at the point it is inherited.
1182 elsif Ekind (E) = E_Record_Type
1183 and then Is_Tagged_Type (E)
1184 and then Is_Tagged_Type (Etype (E))
1185 and then Is_Derived_Type (E)
1186 then
1187 declare
1188 Prim_List : constant Elist_Id :=
1189 Primitive_Operations (Etype (E));
1191 Prim : Elmt_Id;
1192 Subp : Entity_Id;
1194 begin
1195 Prim := First_Elmt (Prim_List);
1197 while Present (Prim) loop
1198 Subp := Node (Prim);
1200 if Comes_From_Source (Subp)
1201 and then not Is_Frozen (Subp)
1202 then
1203 Flist := Freeze_Entity (Subp, Loc);
1204 Process_Flist;
1205 end if;
1207 Next_Elmt (Prim);
1208 end loop;
1209 end;
1210 end if;
1212 if not Is_Frozen (E) then
1213 Flist := Freeze_Entity (E, Loc);
1214 Process_Flist;
1215 end if;
1217 -- If an incomplete type is still not frozen, this may be
1218 -- a premature freezing because of a body declaration that
1219 -- follows. Indicate where the freezing took place.
1221 -- If the freezing is caused by the end of the current
1222 -- declarative part, it is a Taft Amendment type, and there
1223 -- is no error.
1225 if not Is_Frozen (E)
1226 and then Ekind (E) = E_Incomplete_Type
1227 then
1228 declare
1229 Bod : constant Node_Id := Next (After);
1231 begin
1232 if (Nkind (Bod) = N_Subprogram_Body
1233 or else Nkind (Bod) = N_Entry_Body
1234 or else Nkind (Bod) = N_Package_Body
1235 or else Nkind (Bod) = N_Protected_Body
1236 or else Nkind (Bod) = N_Task_Body
1237 or else Nkind (Bod) in N_Body_Stub)
1238 and then
1239 List_Containing (After) = List_Containing (Parent (E))
1240 then
1241 Error_Msg_Sloc := Sloc (Next (After));
1242 Error_Msg_NE
1243 ("type& is frozen# before its full declaration",
1244 Parent (E), E);
1245 end if;
1246 end;
1247 end if;
1249 Next_Entity (E);
1250 end loop;
1251 end Freeze_All_Ent;
1253 -- Start of processing for Freeze_All
1255 begin
1256 Freeze_All_Ent (From, After);
1258 -- Now that all types are frozen, we can deal with default expressions
1259 -- that require us to build a default expression functions. This is the
1260 -- point at which such functions are constructed (after all types that
1261 -- might be used in such expressions have been frozen).
1263 -- We also add finalization chains to access types whose designated
1264 -- types are controlled. This is normally done when freezing the type,
1265 -- but this misses recursive type definitions where the later members
1266 -- of the recursion introduce controlled components (e.g. 5624-001).
1268 -- Loop through entities
1270 E := From;
1271 while Present (E) loop
1272 if Is_Subprogram (E) then
1274 if not Default_Expressions_Processed (E) then
1275 Process_Default_Expressions (E, After);
1276 end if;
1278 if not Has_Completion (E) then
1279 Decl := Unit_Declaration_Node (E);
1281 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1282 Build_And_Analyze_Renamed_Body (Decl, E, After);
1284 elsif Nkind (Decl) = N_Subprogram_Declaration
1285 and then Present (Corresponding_Body (Decl))
1286 and then
1287 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1288 = N_Subprogram_Renaming_Declaration
1289 then
1290 Build_And_Analyze_Renamed_Body
1291 (Decl, Corresponding_Body (Decl), After);
1292 end if;
1293 end if;
1295 elsif Ekind (E) in Task_Kind
1296 and then
1297 (Nkind (Parent (E)) = N_Task_Type_Declaration
1298 or else
1299 Nkind (Parent (E)) = N_Single_Task_Declaration)
1300 then
1301 declare
1302 Ent : Entity_Id;
1303 begin
1304 Ent := First_Entity (E);
1306 while Present (Ent) loop
1308 if Is_Entry (Ent)
1309 and then not Default_Expressions_Processed (Ent)
1310 then
1311 Process_Default_Expressions (Ent, After);
1312 end if;
1314 Next_Entity (Ent);
1315 end loop;
1316 end;
1318 elsif Is_Access_Type (E)
1319 and then Comes_From_Source (E)
1320 and then Ekind (Directly_Designated_Type (E)) = E_Incomplete_Type
1321 and then Controlled_Type (Designated_Type (E))
1322 and then No (Associated_Final_Chain (E))
1323 then
1324 Build_Final_List (Parent (E), E);
1325 end if;
1327 Next_Entity (E);
1328 end loop;
1329 end Freeze_All;
1331 -----------------------
1332 -- Freeze_And_Append --
1333 -----------------------
1335 procedure Freeze_And_Append
1336 (Ent : Entity_Id;
1337 Loc : Source_Ptr;
1338 Result : in out List_Id)
1340 L : constant List_Id := Freeze_Entity (Ent, Loc);
1341 begin
1342 if Is_Non_Empty_List (L) then
1343 if Result = No_List then
1344 Result := L;
1345 else
1346 Append_List (L, Result);
1347 end if;
1348 end if;
1349 end Freeze_And_Append;
1351 -------------------
1352 -- Freeze_Before --
1353 -------------------
1355 procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
1356 Freeze_Nodes : constant List_Id := Freeze_Entity (T, Sloc (N));
1357 begin
1358 if Is_Non_Empty_List (Freeze_Nodes) then
1359 Insert_Actions (N, Freeze_Nodes);
1360 end if;
1361 end Freeze_Before;
1363 -------------------
1364 -- Freeze_Entity --
1365 -------------------
1367 function Freeze_Entity (E : Entity_Id; Loc : Source_Ptr) return List_Id is
1368 Comp : Entity_Id;
1369 F_Node : Node_Id;
1370 Result : List_Id;
1371 Indx : Node_Id;
1372 Formal : Entity_Id;
1373 Atype : Entity_Id;
1375 procedure Check_Current_Instance (Comp_Decl : Node_Id);
1376 -- Check that an Access or Unchecked_Access attribute with
1377 -- a prefix which is the current instance type can only be
1378 -- applied when the type is limited.
1380 function After_Last_Declaration return Boolean;
1381 -- If Loc is a freeze_entity that appears after the last declaration
1382 -- in the scope, inhibit error messages on late completion.
1384 procedure Freeze_Record_Type (Rec : Entity_Id);
1385 -- Freeze each component, handle some representation clauses, and
1386 -- freeze primitive operations if this is a tagged type.
1388 ----------------------------
1389 -- After_Last_Declaration --
1390 ----------------------------
1392 function After_Last_Declaration return Boolean is
1393 Spec : constant Node_Id := Parent (Current_Scope);
1395 begin
1396 if Nkind (Spec) = N_Package_Specification then
1397 if Present (Private_Declarations (Spec)) then
1398 return Loc >= Sloc (Last (Private_Declarations (Spec)));
1400 elsif Present (Visible_Declarations (Spec)) then
1401 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
1402 else
1403 return False;
1404 end if;
1406 else
1407 return False;
1408 end if;
1409 end After_Last_Declaration;
1411 ----------------------------
1412 -- Check_Current_Instance --
1413 ----------------------------
1415 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
1417 function Process (N : Node_Id) return Traverse_Result;
1418 -- Process routine to apply check to given node.
1420 -------------
1421 -- Process --
1422 -------------
1424 function Process (N : Node_Id) return Traverse_Result is
1425 begin
1426 case Nkind (N) is
1427 when N_Attribute_Reference =>
1428 if (Attribute_Name (N) = Name_Access
1429 or else
1430 Attribute_Name (N) = Name_Unchecked_Access)
1431 and then Is_Entity_Name (Prefix (N))
1432 and then Is_Type (Entity (Prefix (N)))
1433 and then Entity (Prefix (N)) = E
1434 then
1435 Error_Msg_N
1436 ("current instance must be a limited type", Prefix (N));
1437 return Abandon;
1438 else
1439 return OK;
1440 end if;
1442 when others => return OK;
1443 end case;
1444 end Process;
1446 procedure Traverse is new Traverse_Proc (Process);
1448 -- Start of processing for Check_Current_Instance
1450 begin
1451 Traverse (Comp_Decl);
1452 end Check_Current_Instance;
1454 ------------------------
1455 -- Freeze_Record_Type --
1456 ------------------------
1458 procedure Freeze_Record_Type (Rec : Entity_Id) is
1459 Comp : Entity_Id;
1460 IR : Node_Id;
1461 Junk : Boolean;
1462 ADC : Node_Id;
1464 Unplaced_Component : Boolean := False;
1465 -- Set True if we find at least one component with no component
1466 -- clause (used to warn about useless Pack pragmas).
1468 Placed_Component : Boolean := False;
1469 -- Set True if we find at least one component with a component
1470 -- clause (used to warn about useless Bit_Order pragmas).
1472 procedure Check_Itype (Desig : Entity_Id);
1473 -- If the component subtype is an access to a constrained subtype
1474 -- of an already frozen type, make the subtype frozen as well. It
1475 -- might otherwise be frozen in the wrong scope, and a freeze node
1476 -- on subtype has no effect.
1478 -----------------
1479 -- Check_Itype --
1480 -----------------
1482 procedure Check_Itype (Desig : Entity_Id) is
1483 begin
1484 if not Is_Frozen (Desig)
1485 and then Is_Frozen (Base_Type (Desig))
1486 then
1487 Set_Is_Frozen (Desig);
1489 -- In addition, add an Itype_Reference to ensure that the
1490 -- access subtype is elaborated early enough. This cannot
1491 -- be done if the subtype may depend on discriminants.
1493 if Ekind (Comp) = E_Component
1494 and then Is_Itype (Etype (Comp))
1495 and then not Has_Discriminants (Rec)
1496 then
1497 IR := Make_Itype_Reference (Sloc (Comp));
1498 Set_Itype (IR, Desig);
1500 if No (Result) then
1501 Result := New_List (IR);
1502 else
1503 Append (IR, Result);
1504 end if;
1505 end if;
1506 end if;
1507 end Check_Itype;
1509 -- Start of processing for Freeze_Record_Type
1511 begin
1512 -- If this is a subtype of a controlled type, declared without
1513 -- a constraint, the _controller may not appear in the component
1514 -- list if the parent was not frozen at the point of subtype
1515 -- declaration. Inherit the _controller component now.
1517 if Rec /= Base_Type (Rec)
1518 and then Has_Controlled_Component (Rec)
1519 then
1520 if Nkind (Parent (Rec)) = N_Subtype_Declaration
1521 and then Is_Entity_Name (Subtype_Indication (Parent (Rec)))
1522 then
1523 Set_First_Entity (Rec, First_Entity (Base_Type (Rec)));
1525 -- If this is an internal type without a declaration, as for a
1526 -- record component, the base type may not yet be frozen, and its
1527 -- controller has not been created. Add an explicit freeze node
1528 -- for the itype, so it will be frozen after the base type.
1530 elsif Is_Itype (Rec)
1531 and then Has_Delayed_Freeze (Base_Type (Rec))
1532 and then
1533 Nkind (Associated_Node_For_Itype (Rec)) =
1534 N_Component_Declaration
1535 then
1536 Ensure_Freeze_Node (Rec);
1537 end if;
1538 end if;
1540 -- Freeze components and embedded subtypes
1542 Comp := First_Entity (Rec);
1543 while Present (Comp) loop
1544 if not Is_Type (Comp) then
1545 Freeze_And_Append (Etype (Comp), Loc, Result);
1546 end if;
1548 -- If the component is an access type with an allocator
1549 -- as default value, the designated type will be frozen
1550 -- by the corresponding expression in init_proc. In order
1551 -- to place the freeze node for the designated type before
1552 -- that for the current record type, freeze it now.
1554 -- Same process if the component is an array of access types,
1555 -- initialized with an aggregate. If the designated type is
1556 -- private, it cannot contain allocators, and it is premature
1557 -- to freeze the type, so we check for this as well.
1559 if Is_Access_Type (Etype (Comp))
1560 and then Present (Parent (Comp))
1561 and then Present (Expression (Parent (Comp)))
1562 and then Nkind (Expression (Parent (Comp))) = N_Allocator
1563 then
1564 declare
1565 Alloc : constant Node_Id := Expression (Parent (Comp));
1567 begin
1568 -- If component is pointer to a classwide type, freeze
1569 -- the specific type in the expression being allocated.
1570 -- The expression may be a subtype indication, in which
1571 -- case freeze the subtype mark.
1573 if Is_Class_Wide_Type (Designated_Type (Etype (Comp))) then
1574 if Is_Entity_Name (Expression (Alloc)) then
1575 Freeze_And_Append
1576 (Entity (Expression (Alloc)), Loc, Result);
1577 elsif
1578 Nkind (Expression (Alloc)) = N_Subtype_Indication
1579 then
1580 Freeze_And_Append
1581 (Entity (Subtype_Mark (Expression (Alloc))),
1582 Loc, Result);
1583 end if;
1585 elsif Is_Itype (Designated_Type (Etype (Comp))) then
1586 Check_Itype (Designated_Type (Etype (Comp)));
1588 else
1589 Freeze_And_Append
1590 (Designated_Type (Etype (Comp)), Loc, Result);
1591 end if;
1592 end;
1594 elsif Is_Access_Type (Etype (Comp))
1595 and then Is_Itype (Designated_Type (Etype (Comp)))
1596 then
1597 Check_Itype (Designated_Type (Etype (Comp)));
1599 elsif Is_Array_Type (Etype (Comp))
1600 and then Is_Access_Type (Component_Type (Etype (Comp)))
1601 and then Present (Parent (Comp))
1602 and then Nkind (Parent (Comp)) = N_Component_Declaration
1603 and then Present (Expression (Parent (Comp)))
1604 and then Nkind (Expression (Parent (Comp))) = N_Aggregate
1605 and then Is_Fully_Defined
1606 (Designated_Type (Component_Type (Etype (Comp))))
1607 then
1608 Freeze_And_Append
1609 (Designated_Type
1610 (Component_Type (Etype (Comp))), Loc, Result);
1611 end if;
1613 -- Processing for real components (exclude anonymous subtypes)
1615 if Ekind (Comp) = E_Component
1616 or else Ekind (Comp) = E_Discriminant
1617 then
1618 declare
1619 CC : constant Node_Id := Component_Clause (Comp);
1621 begin
1622 -- Check for error of component clause given for variable
1623 -- sized type. We have to delay this test till this point,
1624 -- since the component type has to be frozen for us to know
1625 -- if it is variable length. We omit this test in a generic
1626 -- context, it will be applied at instantiation time.
1628 if Present (CC) then
1629 Placed_Component := True;
1631 if Inside_A_Generic then
1632 null;
1634 elsif not Size_Known_At_Compile_Time
1635 (Underlying_Type (Etype (Comp)))
1636 then
1637 Error_Msg_N
1638 ("component clause not allowed for variable " &
1639 "length component", CC);
1640 end if;
1642 else
1643 Unplaced_Component := True;
1644 end if;
1646 -- Case of component requires byte alignment
1648 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
1650 -- Set the enclosing record to also require byte align
1652 Set_Must_Be_On_Byte_Boundary (Rec);
1654 -- Check for component clause that is inconsistent
1655 -- with the required byte boundary alignment.
1657 if Present (CC)
1658 and then Normalized_First_Bit (Comp) mod
1659 System_Storage_Unit /= 0
1660 then
1661 Error_Msg_N
1662 ("component & must be byte aligned",
1663 Component_Name (Component_Clause (Comp)));
1664 end if;
1665 end if;
1667 -- If component clause is present, then deal with the
1668 -- non-default bit order case. We cannot do this before
1669 -- the freeze point, because there is no required order
1670 -- for the component clause and the bit_order clause.
1672 -- We only do this processing for the base type, and in
1673 -- fact that's important, since otherwise if there are
1674 -- record subtypes, we could reverse the bits once for
1675 -- each subtype, which would be incorrect.
1677 if Present (CC)
1678 and then Reverse_Bit_Order (Rec)
1679 and then Ekind (E) = E_Record_Type
1680 then
1681 declare
1682 CFB : constant Uint := Component_Bit_Offset (Comp);
1683 CSZ : constant Uint := Esize (Comp);
1684 CLC : constant Node_Id := Component_Clause (Comp);
1685 Pos : constant Node_Id := Position (CLC);
1686 FB : constant Node_Id := First_Bit (CLC);
1688 Storage_Unit_Offset : constant Uint :=
1689 CFB / System_Storage_Unit;
1691 Start_Bit : constant Uint :=
1692 CFB mod System_Storage_Unit;
1694 begin
1695 -- Cases where field goes over storage unit boundary
1697 if Start_Bit + CSZ > System_Storage_Unit then
1699 -- Allow multi-byte field but generate warning
1701 if Start_Bit mod System_Storage_Unit = 0
1702 and then CSZ mod System_Storage_Unit = 0
1703 then
1704 Error_Msg_N
1705 ("multi-byte field specified with non-standard"
1706 & " Bit_Order?", CLC);
1708 if Bytes_Big_Endian then
1709 Error_Msg_N
1710 ("bytes are not reversed "
1711 & "(component is big-endian)?", CLC);
1712 else
1713 Error_Msg_N
1714 ("bytes are not reversed "
1715 & "(component is little-endian)?", CLC);
1716 end if;
1718 -- Do not allow non-contiguous field
1720 else
1721 Error_Msg_N
1722 ("attempt to specify non-contiguous field"
1723 & " not permitted", CLC);
1724 Error_Msg_N
1725 ("\(caused by non-standard Bit_Order "
1726 & "specified)", CLC);
1727 end if;
1729 -- Case where field fits in one storage unit
1731 else
1732 -- Give warning if suspicious component clause
1734 if Intval (FB) >= System_Storage_Unit then
1735 Error_Msg_N
1736 ("?Bit_Order clause does not affect " &
1737 "byte ordering", Pos);
1738 Error_Msg_Uint_1 :=
1739 Intval (Pos) + Intval (FB) /
1740 System_Storage_Unit;
1741 Error_Msg_N
1742 ("?position normalized to ^ before bit " &
1743 "order interpreted", Pos);
1744 end if;
1746 -- Here is where we fix up the Component_Bit_Offset
1747 -- value to account for the reverse bit order.
1748 -- Some examples of what needs to be done are:
1750 -- First_Bit .. Last_Bit Component_Bit_Offset
1751 -- old new old new
1753 -- 0 .. 0 7 .. 7 0 7
1754 -- 0 .. 1 6 .. 7 0 6
1755 -- 0 .. 2 5 .. 7 0 5
1756 -- 0 .. 7 0 .. 7 0 4
1758 -- 1 .. 1 6 .. 6 1 6
1759 -- 1 .. 4 3 .. 6 1 3
1760 -- 4 .. 7 0 .. 3 4 0
1762 -- The general rule is that the first bit is
1763 -- is obtained by subtracting the old ending bit
1764 -- from storage_unit - 1.
1766 Set_Component_Bit_Offset
1767 (Comp,
1768 (Storage_Unit_Offset * System_Storage_Unit) +
1769 (System_Storage_Unit - 1) -
1770 (Start_Bit + CSZ - 1));
1772 Set_Normalized_First_Bit
1773 (Comp,
1774 Component_Bit_Offset (Comp) mod
1775 System_Storage_Unit);
1776 end if;
1777 end;
1778 end if;
1779 end;
1780 end if;
1782 Next_Entity (Comp);
1783 end loop;
1785 -- Check for useless pragma Bit_Order
1787 if not Placed_Component and then Reverse_Bit_Order (Rec) then
1788 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
1789 Error_Msg_N ("?Bit_Order specification has no effect", ADC);
1790 Error_Msg_N ("\?since no component clauses were specified", ADC);
1791 end if;
1793 -- Check for useless pragma Pack when all components placed
1795 if Is_Packed (Rec)
1796 and then not Unplaced_Component
1797 and then Warn_On_Redundant_Constructs
1798 then
1799 Error_Msg_N
1800 ("?pragma Pack has no effect, no unplaced components",
1801 Get_Rep_Pragma (Rec, Name_Pack));
1802 Set_Is_Packed (Rec, False);
1803 end if;
1805 -- If this is the record corresponding to a remote type,
1806 -- freeze the remote type here since that is what we are
1807 -- semantically freezing. This prevents having the freeze
1808 -- node for that type in an inner scope.
1810 -- Also, Check for controlled components and unchecked unions.
1811 -- Finally, enforce the restriction that access attributes with
1812 -- a current instance prefix can only apply to limited types.
1814 if Ekind (Rec) = E_Record_Type then
1815 if Present (Corresponding_Remote_Type (Rec)) then
1816 Freeze_And_Append
1817 (Corresponding_Remote_Type (Rec), Loc, Result);
1818 end if;
1820 Comp := First_Component (Rec);
1821 while Present (Comp) loop
1822 if Has_Controlled_Component (Etype (Comp))
1823 or else (Chars (Comp) /= Name_uParent
1824 and then Is_Controlled (Etype (Comp)))
1825 or else (Is_Protected_Type (Etype (Comp))
1826 and then Present
1827 (Corresponding_Record_Type (Etype (Comp)))
1828 and then Has_Controlled_Component
1829 (Corresponding_Record_Type (Etype (Comp))))
1830 then
1831 Set_Has_Controlled_Component (Rec);
1832 exit;
1833 end if;
1835 if Has_Unchecked_Union (Etype (Comp)) then
1836 Set_Has_Unchecked_Union (Rec);
1837 end if;
1839 if Has_Per_Object_Constraint (Comp)
1840 and then not Is_Limited_Type (Rec)
1841 then
1842 -- Scan component declaration for likely misuses of
1843 -- current instance, either in a constraint or in a
1844 -- default expression.
1846 Check_Current_Instance (Parent (Comp));
1847 end if;
1849 Next_Component (Comp);
1850 end loop;
1851 end if;
1853 Set_Component_Alignment_If_Not_Set (Rec);
1855 -- For first subtypes, check if there are any fixed-point
1856 -- fields with component clauses, where we must check the size.
1857 -- This is not done till the freeze point, since for fixed-point
1858 -- types, we do not know the size until the type is frozen.
1859 -- Similar processing applies to bit packed arrays.
1861 if Is_First_Subtype (Rec) then
1862 Comp := First_Component (Rec);
1864 while Present (Comp) loop
1865 if Present (Component_Clause (Comp))
1866 and then (Is_Fixed_Point_Type (Etype (Comp))
1867 or else
1868 Is_Bit_Packed_Array (Etype (Comp)))
1869 then
1870 Check_Size
1871 (Component_Name (Component_Clause (Comp)),
1872 Etype (Comp),
1873 Esize (Comp),
1874 Junk);
1875 end if;
1877 Next_Component (Comp);
1878 end loop;
1879 end if;
1880 end Freeze_Record_Type;
1882 -- Start of processing for Freeze_Entity
1884 begin
1885 -- Do not freeze if already frozen since we only need one freeze node
1887 if Is_Frozen (E) then
1888 return No_List;
1890 -- It is improper to freeze an external entity within a generic
1891 -- because its freeze node will appear in a non-valid context.
1892 -- The entity will be frozen in the proper scope after the current
1893 -- generic is analyzed.
1895 elsif Inside_A_Generic and then External_Ref_In_Generic (E) then
1896 return No_List;
1898 -- Do not freeze a global entity within an inner scope created during
1899 -- expansion. A call to subprogram E within some internal procedure
1900 -- (a stream attribute for example) might require freezing E, but the
1901 -- freeze node must appear in the same declarative part as E itself.
1902 -- The two-pass elaboration mechanism in gigi guarantees that E will
1903 -- be frozen before the inner call is elaborated. We exclude constants
1904 -- from this test, because deferred constants may be frozen early, and
1905 -- must be diagnosed (see e.g. 1522-005). If the enclosing subprogram
1906 -- comes from source, or is a generic instance, then the freeze point
1907 -- is the one mandated by the language. and we freze the entity.
1909 elsif In_Open_Scopes (Scope (E))
1910 and then Scope (E) /= Current_Scope
1911 and then Ekind (E) /= E_Constant
1912 then
1913 declare
1914 S : Entity_Id := Current_Scope;
1916 begin
1917 while Present (S) loop
1918 if Is_Overloadable (S) then
1919 if Comes_From_Source (S)
1920 or else Is_Generic_Instance (S)
1921 then
1922 exit;
1923 else
1924 return No_List;
1925 end if;
1926 end if;
1928 S := Scope (S);
1929 end loop;
1930 end;
1932 -- Similarly, an inlined instance body may make reference to global
1933 -- entities, but these references cannot be the proper freezing point
1934 -- for them, and the the absence of inlining freezing will take place
1935 -- in their own scope. Normally instance bodies are analyzed after
1936 -- the enclosing compilation, and everything has been frozen at the
1937 -- proper place, but with front-end inlining an instance body is
1938 -- compiled before the end of the enclosing scope, and as a result
1939 -- out-of-order freezing must be prevented.
1941 elsif Front_End_Inlining
1942 and then In_Instance_Body
1943 and then Present (Scope (E))
1944 then
1945 declare
1946 S : Entity_Id := Scope (E);
1947 begin
1948 while Present (S) loop
1949 if Is_Generic_Instance (S) then
1950 exit;
1951 else
1952 S := Scope (S);
1953 end if;
1954 end loop;
1956 if No (S) then
1957 return No_List;
1958 end if;
1959 end;
1960 end if;
1962 -- Here to freeze the entity
1964 Result := No_List;
1965 Set_Is_Frozen (E);
1967 -- Case of entity being frozen is other than a type
1969 if not Is_Type (E) then
1971 -- If entity is exported or imported and does not have an external
1972 -- name, now is the time to provide the appropriate default name.
1973 -- Skip this if the entity is stubbed, since we don't need a name
1974 -- for any stubbed routine.
1976 if (Is_Imported (E) or else Is_Exported (E))
1977 and then No (Interface_Name (E))
1978 and then Convention (E) /= Convention_Stubbed
1979 then
1980 Set_Encoded_Interface_Name
1981 (E, Get_Default_External_Name (E));
1983 -- Special processing for atomic objects appearing in object decls
1985 elsif Is_Atomic (E)
1986 and then Nkind (Parent (E)) = N_Object_Declaration
1987 and then Present (Expression (Parent (E)))
1988 then
1989 declare
1990 Expr : constant Node_Id := Expression (Parent (E));
1992 begin
1993 -- If expression is an aggregate, assign to a temporary to
1994 -- ensure that the actual assignment is done atomically rather
1995 -- than component-wise (the assignment to the temp may be done
1996 -- component-wise, but that is harmless.
1998 if Nkind (Expr) = N_Aggregate then
1999 Expand_Atomic_Aggregate (Expr, Etype (E));
2001 -- If the expression is a reference to a record or array
2002 -- object entity, then reset Is_True_Constant to False so
2003 -- that the compiler will not optimize away the intermediate
2004 -- object, which we need in this case for the same reason
2005 -- (to ensure that the actual assignment is atomic, rather
2006 -- than component-wise).
2008 elsif Is_Entity_Name (Expr)
2009 and then (Is_Record_Type (Etype (Expr))
2010 or else
2011 Is_Array_Type (Etype (Expr)))
2012 then
2013 Set_Is_True_Constant (Entity (Expr), False);
2014 end if;
2015 end;
2016 end if;
2018 -- For a subprogram, freeze all parameter types and also the return
2019 -- type (RM 13.14(14)). However skip this for internal subprograms.
2020 -- This is also the point where any extra formal parameters are
2021 -- created since we now know whether the subprogram will use
2022 -- a foreign convention.
2024 if Is_Subprogram (E) then
2025 if not Is_Internal (E) then
2026 declare
2027 F_Type : Entity_Id;
2028 Warn_Node : Node_Id;
2030 function Is_Fat_C_Ptr_Type (T : Entity_Id) return Boolean;
2031 -- Determines if given type entity is a fat pointer type
2032 -- used as an argument type or return type to a subprogram
2033 -- with C or C++ convention set.
2035 --------------------------
2036 -- Is_Fat_C_Access_Type --
2037 --------------------------
2039 function Is_Fat_C_Ptr_Type (T : Entity_Id) return Boolean is
2040 begin
2041 return (Convention (E) = Convention_C
2042 or else
2043 Convention (E) = Convention_CPP)
2044 and then Is_Access_Type (T)
2045 and then Esize (T) > Ttypes.System_Address_Size;
2046 end Is_Fat_C_Ptr_Type;
2048 begin
2049 -- Loop through formals
2051 Formal := First_Formal (E);
2052 while Present (Formal) loop
2053 F_Type := Etype (Formal);
2054 Freeze_And_Append (F_Type, Loc, Result);
2056 if Is_Private_Type (F_Type)
2057 and then Is_Private_Type (Base_Type (F_Type))
2058 and then No (Full_View (Base_Type (F_Type)))
2059 and then not Is_Generic_Type (F_Type)
2060 and then not Is_Derived_Type (F_Type)
2061 then
2062 -- If the type of a formal is incomplete, subprogram
2063 -- is being frozen prematurely. Within an instance
2064 -- (but not within a wrapper package) this is an
2065 -- an artifact of our need to regard the end of an
2066 -- instantiation as a freeze point. Otherwise it is
2067 -- a definite error.
2069 -- and then not Is_Wrapper_Package (Current_Scope) ???
2071 if In_Instance then
2072 Set_Is_Frozen (E, False);
2073 return No_List;
2075 elsif not After_Last_Declaration then
2076 Error_Msg_Node_1 := F_Type;
2077 Error_Msg
2078 ("type& must be fully defined before this point",
2079 Loc);
2080 end if;
2081 end if;
2083 -- Check bad use of fat C pointer
2085 if Warn_On_Export_Import and then
2086 Is_Fat_C_Ptr_Type (F_Type)
2087 then
2088 Error_Msg_Qual_Level := 1;
2089 Error_Msg_N
2090 ("?type of & does not correspond to C pointer",
2091 Formal);
2092 Error_Msg_Qual_Level := 0;
2093 end if;
2095 -- Check for unconstrained array in exported foreign
2096 -- convention case.
2098 if Convention (E) in Foreign_Convention
2099 and then not Is_Imported (E)
2100 and then Is_Array_Type (F_Type)
2101 and then not Is_Constrained (F_Type)
2102 and then Warn_On_Export_Import
2103 then
2104 Error_Msg_Qual_Level := 1;
2106 -- If this is an inherited operation, place the
2107 -- warning on the derived type declaration, rather
2108 -- than on the original subprogram.
2110 if Nkind (Original_Node (Parent (E))) =
2111 N_Full_Type_Declaration
2112 then
2113 Warn_Node := Parent (E);
2115 if Formal = First_Formal (E) then
2116 Error_Msg_NE
2117 ("?in inherited operation&!", Warn_Node, E);
2118 end if;
2119 else
2120 Warn_Node := Formal;
2121 end if;
2123 Error_Msg_NE
2124 ("?type of argument& is unconstrained array",
2125 Warn_Node, Formal);
2126 Error_Msg_NE
2127 ("?foreign caller must pass bounds explicitly",
2128 Warn_Node, Formal);
2129 Error_Msg_Qual_Level := 0;
2130 end if;
2132 Next_Formal (Formal);
2133 end loop;
2135 -- Check return type
2137 if Ekind (E) = E_Function then
2138 Freeze_And_Append (Etype (E), Loc, Result);
2140 if Warn_On_Export_Import
2141 and then Is_Fat_C_Ptr_Type (Etype (E))
2142 then
2143 Error_Msg_N
2144 ("?return type of& does not correspond to C pointer",
2147 elsif Is_Array_Type (Etype (E))
2148 and then not Is_Constrained (Etype (E))
2149 and then not Is_Imported (E)
2150 and then Convention (E) in Foreign_Convention
2151 and then Warn_On_Export_Import
2152 then
2153 Error_Msg_N
2154 ("?foreign convention function& should not " &
2155 "return unconstrained array", E);
2156 end if;
2157 end if;
2158 end;
2159 end if;
2161 -- Must freeze its parent first if it is a derived subprogram
2163 if Present (Alias (E)) then
2164 Freeze_And_Append (Alias (E), Loc, Result);
2165 end if;
2167 -- If the return type requires a transient scope, and we are on
2168 -- a target allowing functions to return with a depressed stack
2169 -- pointer, then we mark the function as requiring this treatment.
2171 if Ekind (E) = E_Function
2172 and then Functions_Return_By_DSP_On_Target
2173 and then Requires_Transient_Scope (Etype (E))
2174 then
2175 Set_Function_Returns_With_DSP (E);
2176 end if;
2178 if not Is_Internal (E) then
2179 Freeze_Subprogram (E);
2180 end if;
2182 -- Here for other than a subprogram or type
2184 else
2185 -- If entity has a type, and it is not a generic unit, then
2186 -- freeze it first (RM 13.14(10))
2188 if Present (Etype (E))
2189 and then Ekind (E) /= E_Generic_Function
2190 then
2191 Freeze_And_Append (Etype (E), Loc, Result);
2192 end if;
2194 -- For object created by object declaration, perform required
2195 -- categorization (preelaborate and pure) checks. Defer these
2196 -- checks to freeze time since pragma Import inhibits default
2197 -- initialization and thus pragma Import affects these checks.
2199 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
2200 Validate_Object_Declaration (Declaration_Node (E));
2201 Check_Address_Clause (E);
2202 end if;
2204 -- Check that a constant which has a pragma Volatile[_Components]
2205 -- or Atomic[_Components] also has a pragma Import (RM C.6(13))
2207 -- Note: Atomic[_Components] also sets Volatile[_Components]
2209 if Ekind (E) = E_Constant
2210 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
2211 and then not Is_Imported (E)
2212 then
2213 -- Make sure we actually have a pragma, and have not merely
2214 -- inherited the indication from elsewhere (e.g. an address
2215 -- clause, which is not good enough in RM terms!)
2217 if Present (Get_Rep_Pragma (E, Name_Atomic))
2218 or else
2219 Present (Get_Rep_Pragma (E, Name_Atomic_Components))
2220 then
2221 Error_Msg_N
2222 ("stand alone atomic constant must be " &
2223 "imported ('R'M 'C.6(13))", E);
2225 elsif Present (Get_Rep_Pragma (E, Name_Volatile))
2226 or else
2227 Present (Get_Rep_Pragma (E, Name_Volatile_Components))
2228 then
2229 Error_Msg_N
2230 ("stand alone volatile constant must be " &
2231 "imported ('R'M 'C.6(13))", E);
2232 end if;
2233 end if;
2235 -- Static objects require special handling
2237 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
2238 and then Is_Statically_Allocated (E)
2239 then
2240 Freeze_Static_Object (E);
2241 end if;
2243 -- Remaining step is to layout objects
2245 if Ekind (E) = E_Variable
2246 or else
2247 Ekind (E) = E_Constant
2248 or else
2249 Ekind (E) = E_Loop_Parameter
2250 or else
2251 Is_Formal (E)
2252 then
2253 Layout_Object (E);
2254 end if;
2255 end if;
2257 -- Case of a type or subtype being frozen
2259 else
2260 -- The type may be defined in a generic unit. This can occur when
2261 -- freezing a generic function that returns the type (which is
2262 -- defined in a parent unit). It is clearly meaningless to freeze
2263 -- this type. However, if it is a subtype, its size may be determi-
2264 -- nable and used in subsequent checks, so might as well try to
2265 -- compute it.
2267 if Present (Scope (E))
2268 and then Is_Generic_Unit (Scope (E))
2269 then
2270 Check_Compile_Time_Size (E);
2271 return No_List;
2272 end if;
2274 -- Deal with special cases of freezing for subtype
2276 if E /= Base_Type (E) then
2278 -- If ancestor subtype present, freeze that first.
2279 -- Note that this will also get the base type frozen.
2281 Atype := Ancestor_Subtype (E);
2283 if Present (Atype) then
2284 Freeze_And_Append (Atype, Loc, Result);
2286 -- Otherwise freeze the base type of the entity before
2287 -- freezing the entity itself, (RM 13.14(15)).
2289 elsif E /= Base_Type (E) then
2290 Freeze_And_Append (Base_Type (E), Loc, Result);
2291 end if;
2293 -- For a derived type, freeze its parent type first (RM 13.14(15))
2295 elsif Is_Derived_Type (E) then
2296 Freeze_And_Append (Etype (E), Loc, Result);
2297 Freeze_And_Append (First_Subtype (Etype (E)), Loc, Result);
2298 end if;
2300 -- For array type, freeze index types and component type first
2301 -- before freezing the array (RM 13.14(15)).
2303 if Is_Array_Type (E) then
2304 declare
2305 Ctyp : constant Entity_Id := Component_Type (E);
2306 Pnod : Node_Id;
2308 Non_Standard_Enum : Boolean := False;
2309 -- Set true if any of the index types is an enumeration
2310 -- type with a non-standard representation.
2312 begin
2313 Freeze_And_Append (Ctyp, Loc, Result);
2315 Indx := First_Index (E);
2316 while Present (Indx) loop
2317 Freeze_And_Append (Etype (Indx), Loc, Result);
2319 if Is_Enumeration_Type (Etype (Indx))
2320 and then Has_Non_Standard_Rep (Etype (Indx))
2321 then
2322 Non_Standard_Enum := True;
2323 end if;
2325 Next_Index (Indx);
2326 end loop;
2328 -- Processing that is done only for base types
2330 if Ekind (E) = E_Array_Type then
2332 -- Propagate flags for component type
2334 if Is_Controlled (Component_Type (E))
2335 or else Has_Controlled_Component (Ctyp)
2336 then
2337 Set_Has_Controlled_Component (E);
2338 end if;
2340 if Has_Unchecked_Union (Component_Type (E)) then
2341 Set_Has_Unchecked_Union (E);
2342 end if;
2344 -- If packing was requested or if the component size was set
2345 -- explicitly, then see if bit packing is required. This
2346 -- processing is only done for base types, since all the
2347 -- representation aspects involved are type-related. This
2348 -- is not just an optimization, if we start processing the
2349 -- subtypes, they intefere with the settings on the base
2350 -- type (this is because Is_Packed has a slightly different
2351 -- meaning before and after freezing).
2353 declare
2354 Csiz : Uint;
2355 Esiz : Uint;
2357 begin
2358 if (Is_Packed (E) or else Has_Pragma_Pack (E))
2359 and then not Has_Atomic_Components (E)
2360 and then Known_Static_RM_Size (Ctyp)
2361 then
2362 Csiz := UI_Max (RM_Size (Ctyp), 1);
2364 elsif Known_Component_Size (E) then
2365 Csiz := Component_Size (E);
2367 elsif not Known_Static_Esize (Ctyp) then
2368 Csiz := Uint_0;
2370 else
2371 Esiz := Esize (Ctyp);
2373 -- We can set the component size if it is less than
2374 -- 16, rounding it up to the next storage unit size.
2376 if Esiz <= 8 then
2377 Csiz := Uint_8;
2378 elsif Esiz <= 16 then
2379 Csiz := Uint_16;
2380 else
2381 Csiz := Uint_0;
2382 end if;
2384 -- Set component size up to match alignment if
2385 -- it would otherwise be less than the alignment.
2386 -- This deals with cases of types whose alignment
2387 -- exceeds their sizes (padded types).
2389 if Csiz /= 0 then
2390 declare
2391 A : constant Uint := Alignment_In_Bits (Ctyp);
2393 begin
2394 if Csiz < A then
2395 Csiz := A;
2396 end if;
2397 end;
2398 end if;
2400 end if;
2402 if 1 <= Csiz and then Csiz <= 64 then
2404 -- We set the component size for all cases 1-64
2406 Set_Component_Size (Base_Type (E), Csiz);
2408 -- Check for base type of 8,16,32 bits, where the
2409 -- subtype has a length one less than the base type
2410 -- and is unsigned (e.g. Natural subtype of Integer)
2412 -- In such cases, if a component size was not set
2413 -- explicitly, then generate a warning.
2415 if Has_Pragma_Pack (E)
2416 and then not Has_Component_Size_Clause (E)
2417 and then
2418 (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2419 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2420 then
2421 Error_Msg_Uint_1 := Csiz;
2422 Pnod :=
2423 Get_Rep_Pragma (First_Subtype (E), Name_Pack);
2425 if Present (Pnod) then
2426 Error_Msg_N
2427 ("pragma Pack causes component size to be ^?",
2428 Pnod);
2429 Error_Msg_N
2430 ("\use Component_Size to set desired value",
2431 Pnod);
2432 end if;
2433 end if;
2435 -- Actual packing is not needed for 8,16,32,64
2436 -- Also not needed for 24 if alignment is 1
2438 if Csiz = 8
2439 or else Csiz = 16
2440 or else Csiz = 32
2441 or else Csiz = 64
2442 or else (Csiz = 24 and then Alignment (Ctyp) = 1)
2443 then
2444 -- Here the array was requested to be packed, but
2445 -- the packing request had no effect, so Is_Packed
2446 -- is reset.
2448 -- Note: semantically this means that we lose
2449 -- track of the fact that a derived type inherited
2450 -- a pack pragma that was non-effective, but that
2451 -- seems fine.
2453 -- We regard a Pack pragma as a request to set a
2454 -- representation characteristic, and this request
2455 -- may be ignored.
2457 Set_Is_Packed (Base_Type (E), False);
2459 -- In all other cases, packing is indeed needed
2461 else
2462 Set_Has_Non_Standard_Rep (Base_Type (E));
2463 Set_Is_Bit_Packed_Array (Base_Type (E));
2464 Set_Is_Packed (Base_Type (E));
2465 end if;
2466 end if;
2467 end;
2469 -- Processing that is done only for subtypes
2471 else
2472 -- Acquire alignment from base type
2474 if Unknown_Alignment (E) then
2475 Set_Alignment (E, Alignment (Base_Type (E)));
2476 end if;
2477 end if;
2479 -- For bit-packed arrays, check the size
2481 if Is_Bit_Packed_Array (E)
2482 and then Known_Esize (E)
2483 then
2484 declare
2485 Discard : Boolean;
2486 SizC : constant Node_Id := Size_Clause (E);
2488 begin
2489 -- It is not clear if it is possible to have no size
2490 -- clause at this stage, but this is not worth worrying
2491 -- about. Post the error on the entity name in the size
2492 -- clause if present, else on the type entity itself.
2494 if Present (SizC) then
2495 Check_Size (Name (SizC), E, Esize (E), Discard);
2496 else
2497 Check_Size (E, E, Esize (E), Discard);
2498 end if;
2499 end;
2500 end if;
2502 -- Check one common case of a size given where the array
2503 -- needs to be packed, but was not so the size cannot be
2504 -- honored. This would of course be caught by the backend,
2505 -- and indeed we don't catch all cases. The point is that
2506 -- we can give a better error message in those cases that
2507 -- we do catch with the circuitry here.
2509 declare
2510 Lo, Hi : Node_Id;
2511 Ctyp : constant Entity_Id := Component_Type (E);
2513 begin
2514 if Present (Size_Clause (E))
2515 and then Known_Static_Esize (E)
2516 and then not Is_Bit_Packed_Array (E)
2517 and then not Has_Pragma_Pack (E)
2518 and then Number_Dimensions (E) = 1
2519 and then not Has_Component_Size_Clause (E)
2520 and then Known_Static_Esize (Ctyp)
2521 then
2522 Get_Index_Bounds (First_Index (E), Lo, Hi);
2524 if Compile_Time_Known_Value (Lo)
2525 and then Compile_Time_Known_Value (Hi)
2526 and then Known_Static_RM_Size (Ctyp)
2527 and then RM_Size (Ctyp) < 64
2528 then
2529 declare
2530 Lov : constant Uint := Expr_Value (Lo);
2531 Hiv : constant Uint := Expr_Value (Hi);
2532 Len : constant Uint :=
2533 UI_Max (Uint_0, Hiv - Lov + 1);
2534 Rsiz : constant Uint := RM_Size (Ctyp);
2536 -- What we are looking for here is the situation
2537 -- where the Esize given would be exactly right
2538 -- if there was a pragma Pack (resulting in the
2539 -- component size being the same as the RM_Size).
2540 -- Furthermore, the component type size must be
2541 -- an odd size (not a multiple of storage unit)
2543 begin
2544 if Esize (E) = Len * Rsiz
2545 and then Rsiz mod System_Storage_Unit /= 0
2546 then
2547 Error_Msg_NE
2548 ("size given for& too small",
2549 Size_Clause (E), E);
2550 Error_Msg_N
2551 ("\explicit pragma Pack is required",
2552 Size_Clause (E));
2553 end if;
2554 end;
2555 end if;
2556 end if;
2557 end;
2559 -- If any of the index types was an enumeration type with
2560 -- a non-standard rep clause, then we indicate that the
2561 -- array type is always packed (even if it is not bit packed).
2563 if Non_Standard_Enum then
2564 Set_Has_Non_Standard_Rep (Base_Type (E));
2565 Set_Is_Packed (Base_Type (E));
2566 end if;
2568 Set_Component_Alignment_If_Not_Set (E);
2570 -- If the array is packed, we must create the packed array
2571 -- type to be used to actually implement the type. This is
2572 -- only needed for real array types (not for string literal
2573 -- types, since they are present only for the front end).
2575 if Is_Packed (E)
2576 and then Ekind (E) /= E_String_Literal_Subtype
2577 then
2578 Create_Packed_Array_Type (E);
2579 Freeze_And_Append (Packed_Array_Type (E), Loc, Result);
2581 -- Size information of packed array type is copied to the
2582 -- array type, since this is really the representation.
2584 Set_Size_Info (E, Packed_Array_Type (E));
2585 Set_RM_Size (E, RM_Size (Packed_Array_Type (E)));
2586 end if;
2588 -- For non-packed arrays set the alignment of the array
2589 -- to the alignment of the component type if it is unknown.
2590 -- Skip this in the atomic case, since atomic arrays may
2591 -- need larger alignments.
2593 if not Is_Packed (E)
2594 and then Unknown_Alignment (E)
2595 and then Known_Alignment (Ctyp)
2596 and then Known_Static_Component_Size (E)
2597 and then Known_Static_Esize (Ctyp)
2598 and then Esize (Ctyp) = Component_Size (E)
2599 and then not Is_Atomic (E)
2600 then
2601 Set_Alignment (E, Alignment (Component_Type (E)));
2602 end if;
2603 end;
2605 -- For a class-wide type, the corresponding specific type is
2606 -- frozen as well (RM 13.14(15))
2608 elsif Is_Class_Wide_Type (E) then
2609 Freeze_And_Append (Root_Type (E), Loc, Result);
2611 -- If the Class_Wide_Type is an Itype (when type is the anonymous
2612 -- parent of a derived type) and it is a library-level entity,
2613 -- generate an itype reference for it. Otherwise, its first
2614 -- explicit reference may be in an inner scope, which will be
2615 -- rejected by the back-end.
2617 if Is_Itype (E)
2618 and then Is_Compilation_Unit (Scope (E))
2619 then
2620 declare
2621 Ref : constant Node_Id := Make_Itype_Reference (Loc);
2623 begin
2624 Set_Itype (Ref, E);
2625 if No (Result) then
2626 Result := New_List (Ref);
2627 else
2628 Append (Ref, Result);
2629 end if;
2630 end;
2631 end if;
2633 -- The equivalent type associated with a class-wide subtype
2634 -- needs to be frozen to ensure that its layout is done.
2635 -- Class-wide subtypes are currently only frozen on targets
2636 -- requiring front-end layout (see New_Class_Wide_Subtype
2637 -- and Make_CW_Equivalent_Type in exp_util.adb).
2639 if Ekind (E) = E_Class_Wide_Subtype
2640 and then Present (Equivalent_Type (E))
2641 then
2642 Freeze_And_Append (Equivalent_Type (E), Loc, Result);
2643 end if;
2645 -- For a record (sub)type, freeze all the component types (RM
2646 -- 13.14(15). We test for E_Record_(sub)Type here, rather than
2647 -- using Is_Record_Type, because we don't want to attempt the
2648 -- freeze for the case of a private type with record extension
2649 -- (we will do that later when the full type is frozen).
2651 elsif Ekind (E) = E_Record_Type
2652 or else Ekind (E) = E_Record_Subtype
2653 then
2654 Freeze_Record_Type (E);
2656 -- For a concurrent type, freeze corresponding record type. This
2657 -- does not correpond to any specific rule in the RM, but the
2658 -- record type is essentially part of the concurrent type.
2659 -- Freeze as well all local entities. This includes record types
2660 -- created for entry parameter blocks, and whatever local entities
2661 -- may appear in the private part.
2663 elsif Is_Concurrent_Type (E) then
2664 if Present (Corresponding_Record_Type (E)) then
2665 Freeze_And_Append
2666 (Corresponding_Record_Type (E), Loc, Result);
2667 end if;
2669 Comp := First_Entity (E);
2671 while Present (Comp) loop
2672 if Is_Type (Comp) then
2673 Freeze_And_Append (Comp, Loc, Result);
2675 elsif (Ekind (Comp)) /= E_Function then
2676 Freeze_And_Append (Etype (Comp), Loc, Result);
2677 end if;
2679 Next_Entity (Comp);
2680 end loop;
2682 -- Private types are required to point to the same freeze node
2683 -- as their corresponding full views. The freeze node itself
2684 -- has to point to the partial view of the entity (because
2685 -- from the partial view, we can retrieve the full view, but
2686 -- not the reverse). However, in order to freeze correctly,
2687 -- we need to freeze the full view. If we are freezing at the
2688 -- end of a scope (or within the scope of the private type),
2689 -- the partial and full views will have been swapped, the
2690 -- full view appears first in the entity chain and the swapping
2691 -- mechanism ensures that the pointers are properly set (on
2692 -- scope exit).
2694 -- If we encounter the partial view before the full view
2695 -- (e.g. when freezing from another scope), we freeze the
2696 -- full view, and then set the pointers appropriately since
2697 -- we cannot rely on swapping to fix things up (subtypes in an
2698 -- outer scope might not get swapped).
2700 elsif Is_Incomplete_Or_Private_Type (E)
2701 and then not Is_Generic_Type (E)
2702 then
2703 -- Case of full view present
2705 if Present (Full_View (E)) then
2707 -- If full view has already been frozen, then no
2708 -- further processing is required
2710 if Is_Frozen (Full_View (E)) then
2712 Set_Has_Delayed_Freeze (E, False);
2713 Set_Freeze_Node (E, Empty);
2714 Check_Debug_Info_Needed (E);
2716 -- Otherwise freeze full view and patch the pointers
2717 -- so that the freeze node will elaborate both views
2718 -- in the back-end.
2720 else
2721 declare
2722 Full : constant Entity_Id := Full_View (E);
2724 begin
2725 if Is_Private_Type (Full)
2726 and then Present (Underlying_Full_View (Full))
2727 then
2728 Freeze_And_Append
2729 (Underlying_Full_View (Full), Loc, Result);
2730 end if;
2732 Freeze_And_Append (Full, Loc, Result);
2734 if Has_Delayed_Freeze (E) then
2735 F_Node := Freeze_Node (Full);
2737 if Present (F_Node) then
2738 Set_Freeze_Node (E, F_Node);
2739 Set_Entity (F_Node, E);
2741 else
2742 -- {Incomplete,Private}_Subtypes
2743 -- with Full_Views constrained by discriminants
2745 Set_Has_Delayed_Freeze (E, False);
2746 Set_Freeze_Node (E, Empty);
2747 end if;
2748 end if;
2749 end;
2751 Check_Debug_Info_Needed (E);
2752 end if;
2754 -- AI-117 requires that the convention of a partial view
2755 -- be the same as the convention of the full view. Note
2756 -- that this is a recognized breach of privacy, but it's
2757 -- essential for logical consistency of representation,
2758 -- and the lack of a rule in RM95 was an oversight.
2760 Set_Convention (E, Convention (Full_View (E)));
2762 Set_Size_Known_At_Compile_Time (E,
2763 Size_Known_At_Compile_Time (Full_View (E)));
2765 -- Size information is copied from the full view to the
2766 -- incomplete or private view for consistency
2768 -- We skip this is the full view is not a type. This is
2769 -- very strange of course, and can only happen as a result
2770 -- of certain illegalities, such as a premature attempt to
2771 -- derive from an incomplete type.
2773 if Is_Type (Full_View (E)) then
2774 Set_Size_Info (E, Full_View (E));
2775 Set_RM_Size (E, RM_Size (Full_View (E)));
2776 end if;
2778 return Result;
2780 -- Case of no full view present. If entity is derived or subtype,
2781 -- it is safe to freeze, correctness depends on the frozen status
2782 -- of parent. Otherwise it is either premature usage, or a Taft
2783 -- amendment type, so diagnosis is at the point of use and the
2784 -- type might be frozen later.
2786 elsif E /= Base_Type (E)
2787 or else Is_Derived_Type (E)
2788 then
2789 null;
2791 else
2792 Set_Is_Frozen (E, False);
2793 return No_List;
2794 end if;
2796 -- For access subprogram, freeze types of all formals, the return
2797 -- type was already frozen, since it is the Etype of the function.
2799 elsif Ekind (E) = E_Subprogram_Type then
2800 Formal := First_Formal (E);
2801 while Present (Formal) loop
2802 Freeze_And_Append (Etype (Formal), Loc, Result);
2803 Next_Formal (Formal);
2804 end loop;
2806 -- If the return type requires a transient scope, and we are on
2807 -- a target allowing functions to return with a depressed stack
2808 -- pointer, then we mark the function as requiring this treatment.
2810 if Functions_Return_By_DSP_On_Target
2811 and then Requires_Transient_Scope (Etype (E))
2812 then
2813 Set_Function_Returns_With_DSP (E);
2814 end if;
2816 Freeze_Subprogram (E);
2818 -- For access to a protected subprogram, freeze the equivalent
2819 -- type (however this is not set if we are not generating code)
2820 -- or if this is an anonymous type used just for resolution).
2822 elsif Ekind (E) = E_Access_Protected_Subprogram_Type
2823 and then Operating_Mode = Generate_Code
2824 and then Present (Equivalent_Type (E))
2825 then
2826 Freeze_And_Append (Equivalent_Type (E), Loc, Result);
2827 end if;
2829 -- Generic types are never seen by the back-end, and are also not
2830 -- processed by the expander (since the expander is turned off for
2831 -- generic processing), so we never need freeze nodes for them.
2833 if Is_Generic_Type (E) then
2834 return Result;
2835 end if;
2837 -- Some special processing for non-generic types to complete
2838 -- representation details not known till the freeze point.
2840 if Is_Fixed_Point_Type (E) then
2841 Freeze_Fixed_Point_Type (E);
2843 -- Some error checks required for ordinary fixed-point type.
2844 -- Defer these till the freeze-point since we need the small
2845 -- and range values. We only do these checks for base types
2847 if Is_Ordinary_Fixed_Point_Type (E)
2848 and then E = Base_Type (E)
2849 then
2850 if Small_Value (E) < Ureal_2_M_80 then
2851 Error_Msg_Name_1 := Name_Small;
2852 Error_Msg_N
2853 ("`&''%` is too small, minimum is 2.0'*'*(-80)", E);
2855 elsif Small_Value (E) > Ureal_2_80 then
2856 Error_Msg_Name_1 := Name_Small;
2857 Error_Msg_N
2858 ("`&''%` is too large, maximum is 2.0'*'*80", E);
2859 end if;
2861 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
2862 Error_Msg_Name_1 := Name_First;
2863 Error_Msg_N
2864 ("`&''%` is too small, minimum is -10.0'*'*36", E);
2865 end if;
2867 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
2868 Error_Msg_Name_1 := Name_Last;
2869 Error_Msg_N
2870 ("`&''%` is too large, maximum is 10.0'*'*36", E);
2871 end if;
2872 end if;
2874 elsif Is_Enumeration_Type (E) then
2875 Freeze_Enumeration_Type (E);
2877 elsif Is_Integer_Type (E) then
2878 Adjust_Esize_For_Alignment (E);
2880 elsif Is_Access_Type (E)
2881 and then No (Associated_Storage_Pool (E))
2882 then
2883 Check_Restriction (No_Standard_Storage_Pools, E);
2884 end if;
2886 -- If the current entity is an array or record subtype and has
2887 -- discriminants used to constrain it, it must not freeze, because
2888 -- Freeze_Entity nodes force Gigi to process the frozen type.
2890 if Is_Composite_Type (E) then
2892 if Is_Array_Type (E) then
2893 declare
2894 Index : Node_Id := First_Index (E);
2895 Expr1 : Node_Id;
2896 Expr2 : Node_Id;
2898 begin
2899 while Present (Index) loop
2900 if Etype (Index) /= Any_Type then
2901 Get_Index_Bounds (Index, Expr1, Expr2);
2903 for J in 1 .. 2 loop
2904 if Nkind (Expr1) = N_Identifier
2905 and then Ekind (Entity (Expr1)) = E_Discriminant
2906 then
2907 Set_Has_Delayed_Freeze (E, False);
2908 Set_Freeze_Node (E, Empty);
2909 Check_Debug_Info_Needed (E);
2910 return Result;
2911 end if;
2913 Expr1 := Expr2;
2914 end loop;
2915 end if;
2917 Next_Index (Index);
2918 end loop;
2919 end;
2921 elsif Has_Discriminants (E)
2922 and Is_Constrained (E)
2923 then
2924 declare
2925 Constraint : Elmt_Id;
2926 Expr : Node_Id;
2928 begin
2929 Constraint := First_Elmt (Discriminant_Constraint (E));
2930 while Present (Constraint) loop
2931 Expr := Node (Constraint);
2932 if Nkind (Expr) = N_Identifier
2933 and then Ekind (Entity (Expr)) = E_Discriminant
2934 then
2935 Set_Has_Delayed_Freeze (E, False);
2936 Set_Freeze_Node (E, Empty);
2937 Check_Debug_Info_Needed (E);
2938 return Result;
2939 end if;
2941 Next_Elmt (Constraint);
2942 end loop;
2943 end;
2944 end if;
2946 -- AI-117 requires that all new primitives of a tagged type
2947 -- must inherit the convention of the full view of the type.
2948 -- Inherited and overriding operations are defined to inherit
2949 -- the convention of their parent or overridden subprogram
2950 -- (also specified in AI-117), and that will have occurred
2951 -- earlier (in Derive_Subprogram and New_Overloaded_Entity).
2952 -- Here we set the convention of primitives that are still
2953 -- convention Ada, which will ensure that any new primitives
2954 -- inherit the type's convention. Class-wide types can have
2955 -- a foreign convention inherited from their specific type,
2956 -- but are excluded from this since they don't have any
2957 -- associated primitives.
2959 if Is_Tagged_Type (E)
2960 and then not Is_Class_Wide_Type (E)
2961 and then Convention (E) /= Convention_Ada
2962 then
2963 declare
2964 Prim_List : constant Elist_Id := Primitive_Operations (E);
2965 Prim : Elmt_Id;
2966 begin
2967 Prim := First_Elmt (Prim_List);
2968 while Present (Prim) loop
2969 if Convention (Node (Prim)) = Convention_Ada then
2970 Set_Convention (Node (Prim), Convention (E));
2971 end if;
2973 Next_Elmt (Prim);
2974 end loop;
2975 end;
2976 end if;
2977 end if;
2979 -- Generate primitive operation references for a tagged type
2981 if Is_Tagged_Type (E)
2982 and then not Is_Class_Wide_Type (E)
2983 then
2984 declare
2985 Prim_List : constant Elist_Id := Primitive_Operations (E);
2986 Prim : Elmt_Id;
2987 Ent : Entity_Id;
2989 begin
2990 Prim := First_Elmt (Prim_List);
2991 while Present (Prim) loop
2992 Ent := Node (Prim);
2994 -- If the operation is derived, get the original for
2995 -- cross-reference purposes (it is the original for
2996 -- which we want the xref, and for which the comes
2997 -- from source test needs to be performed).
2999 while Present (Alias (Ent)) loop
3000 Ent := Alias (Ent);
3001 end loop;
3003 Generate_Reference (E, Ent, 'p', Set_Ref => False);
3004 Next_Elmt (Prim);
3005 end loop;
3007 -- If we get an exception, then something peculiar has happened
3008 -- probably as a result of a previous error. Since this is only
3009 -- for non-critical cross-references, ignore the error.
3011 exception
3012 when others => null;
3013 end;
3014 end if;
3016 -- Now that all types from which E may depend are frozen, see
3017 -- if the size is known at compile time, if it must be unsigned,
3018 -- or if strict alignent is required
3020 Check_Compile_Time_Size (E);
3021 Check_Unsigned_Type (E);
3023 if Base_Type (E) = E then
3024 Check_Strict_Alignment (E);
3025 end if;
3027 -- Do not allow a size clause for a type which does not have a size
3028 -- that is known at compile time
3030 if Has_Size_Clause (E)
3031 and then not Size_Known_At_Compile_Time (E)
3032 then
3033 -- Supress this message if errors posted on E, even if we are
3034 -- in all errors mode, since this is often a junk message
3036 if not Error_Posted (E) then
3037 Error_Msg_N
3038 ("size clause not allowed for variable length type",
3039 Size_Clause (E));
3040 end if;
3041 end if;
3043 -- Remaining process is to set/verify the representation information,
3044 -- in particular the size and alignment values. This processing is
3045 -- not required for generic types, since generic types do not play
3046 -- any part in code generation, and so the size and alignment values
3047 -- for suhc types are irrelevant.
3049 if Is_Generic_Type (E) then
3050 return Result;
3052 -- Otherwise we call the layout procedure
3054 else
3055 Layout_Type (E);
3056 end if;
3058 -- End of freeze processing for type entities
3059 end if;
3061 -- Here is where we logically freeze the current entity. If it has a
3062 -- freeze node, then this is the point at which the freeze node is
3063 -- linked into the result list.
3065 if Has_Delayed_Freeze (E) then
3067 -- If a freeze node is already allocated, use it, otherwise allocate
3068 -- a new one. The preallocation happens in the case of anonymous base
3069 -- types, where we preallocate so that we can set First_Subtype_Link.
3070 -- Note that we reset the Sloc to the current freeze location.
3072 if Present (Freeze_Node (E)) then
3073 F_Node := Freeze_Node (E);
3074 Set_Sloc (F_Node, Loc);
3076 else
3077 F_Node := New_Node (N_Freeze_Entity, Loc);
3078 Set_Freeze_Node (E, F_Node);
3079 Set_Access_Types_To_Process (F_Node, No_Elist);
3080 Set_TSS_Elist (F_Node, No_Elist);
3081 Set_Actions (F_Node, No_List);
3082 end if;
3084 Set_Entity (F_Node, E);
3086 if Result = No_List then
3087 Result := New_List (F_Node);
3088 else
3089 Append (F_Node, Result);
3090 end if;
3092 -- A final pass over record types with discriminants. If the type
3093 -- has an incomplete declaration, there may be constrained access
3094 -- subtypes declared elsewhere, which do not depend on the discrimi-
3095 -- nants of the type, and which are used as component types (i.e.
3096 -- the full view is a recursive type). The designated types of these
3097 -- subtypes can only be elaborated after the type itself, and they
3098 -- need an itype reference.
3100 if Ekind (E) = E_Record_Type
3101 and then Has_Discriminants (E)
3102 then
3103 declare
3104 Comp : Entity_Id;
3105 IR : Node_Id;
3106 Typ : Entity_Id;
3108 begin
3109 Comp := First_Component (E);
3111 while Present (Comp) loop
3112 Typ := Etype (Comp);
3114 if Ekind (Comp) = E_Component
3115 and then Is_Access_Type (Typ)
3116 and then Scope (Typ) /= E
3117 and then Base_Type (Designated_Type (Typ)) = E
3118 and then Is_Itype (Designated_Type (Typ))
3119 then
3120 IR := Make_Itype_Reference (Sloc (Comp));
3121 Set_Itype (IR, Designated_Type (Typ));
3122 Append (IR, Result);
3123 end if;
3125 Next_Component (Comp);
3126 end loop;
3127 end;
3128 end if;
3129 end if;
3131 -- When a type is frozen, the first subtype of the type is frozen as
3132 -- well (RM 13.14(15)). This has to be done after freezing the type,
3133 -- since obviously the first subtype depends on its own base type.
3135 if Is_Type (E) then
3136 Freeze_And_Append (First_Subtype (E), Loc, Result);
3138 -- If we just froze a tagged non-class wide record, then freeze the
3139 -- corresponding class-wide type. This must be done after the tagged
3140 -- type itself is frozen, because the class-wide type refers to the
3141 -- tagged type which generates the class.
3143 if Is_Tagged_Type (E)
3144 and then not Is_Class_Wide_Type (E)
3145 and then Present (Class_Wide_Type (E))
3146 then
3147 Freeze_And_Append (Class_Wide_Type (E), Loc, Result);
3148 end if;
3149 end if;
3151 Check_Debug_Info_Needed (E);
3153 -- Special handling for subprograms
3155 if Is_Subprogram (E) then
3157 -- If subprogram has address clause then reset Is_Public flag, since
3158 -- we do not want the backend to generate external references.
3160 if Present (Address_Clause (E))
3161 and then not Is_Library_Level_Entity (E)
3162 then
3163 Set_Is_Public (E, False);
3165 -- If no address clause and not intrinsic, then for imported
3166 -- subprogram in main unit, generate descriptor if we are in
3167 -- Propagate_Exceptions mode.
3169 elsif Propagate_Exceptions
3170 and then Is_Imported (E)
3171 and then not Is_Intrinsic_Subprogram (E)
3172 and then Convention (E) /= Convention_Stubbed
3173 then
3174 if Result = No_List then
3175 Result := Empty_List;
3176 end if;
3178 Generate_Subprogram_Descriptor_For_Imported_Subprogram
3179 (E, Result);
3180 end if;
3181 end if;
3183 return Result;
3184 end Freeze_Entity;
3186 -----------------------------
3187 -- Freeze_Enumeration_Type --
3188 -----------------------------
3190 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
3191 begin
3192 if Has_Foreign_Convention (Typ)
3193 and then not Has_Size_Clause (Typ)
3194 and then Esize (Typ) < Standard_Integer_Size
3195 then
3196 Init_Esize (Typ, Standard_Integer_Size);
3197 else
3198 Adjust_Esize_For_Alignment (Typ);
3199 end if;
3200 end Freeze_Enumeration_Type;
3202 -----------------------
3203 -- Freeze_Expression --
3204 -----------------------
3206 procedure Freeze_Expression (N : Node_Id) is
3207 In_Def_Exp : constant Boolean := In_Default_Expression;
3208 Typ : Entity_Id;
3209 Nam : Entity_Id;
3210 Desig_Typ : Entity_Id;
3211 P : Node_Id;
3212 Parent_P : Node_Id;
3214 Freeze_Outside : Boolean := False;
3215 -- This flag is set true if the entity must be frozen outside the
3216 -- current subprogram. This happens in the case of expander generated
3217 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
3218 -- not freeze all entities like other bodies, but which nevertheless
3219 -- may reference entities that have to be frozen before the body and
3220 -- obviously cannot be frozen inside the body.
3222 function In_Exp_Body (N : Node_Id) return Boolean;
3223 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
3224 -- it is the handled statement sequence of an expander generated
3225 -- subprogram (init proc, or stream subprogram). If so, it returns
3226 -- True, otherwise False.
3228 -----------------
3229 -- In_Exp_Body --
3230 -----------------
3232 function In_Exp_Body (N : Node_Id) return Boolean is
3233 P : Node_Id;
3235 begin
3236 if Nkind (N) = N_Subprogram_Body then
3237 P := N;
3238 else
3239 P := Parent (N);
3240 end if;
3242 if Nkind (P) /= N_Subprogram_Body then
3243 return False;
3245 else
3246 P := Defining_Unit_Name (Specification (P));
3248 if Nkind (P) = N_Defining_Identifier
3249 and then (Is_Init_Proc (P) or else
3250 Is_TSS (P, TSS_Stream_Input) or else
3251 Is_TSS (P, TSS_Stream_Output) or else
3252 Is_TSS (P, TSS_Stream_Read) or else
3253 Is_TSS (P, TSS_Stream_Write))
3254 then
3255 return True;
3256 else
3257 return False;
3258 end if;
3259 end if;
3260 end In_Exp_Body;
3262 -- Start of processing for Freeze_Expression
3264 begin
3265 -- Immediate return if freezing is inhibited. This flag is set by
3266 -- the analyzer to stop freezing on generated expressions that would
3267 -- cause freezing if they were in the source program, but which are
3268 -- not supposed to freeze, since they are created.
3270 if Must_Not_Freeze (N) then
3271 return;
3272 end if;
3274 -- If expression is non-static, then it does not freeze in a default
3275 -- expression, see section "Handling of Default Expressions" in the
3276 -- spec of package Sem for further details. Note that we have to
3277 -- make sure that we actually have a real expression (if we have
3278 -- a subtype indication, we can't test Is_Static_Expression!)
3280 if In_Def_Exp
3281 and then Nkind (N) in N_Subexpr
3282 and then not Is_Static_Expression (N)
3283 then
3284 return;
3285 end if;
3287 -- Freeze type of expression if not frozen already
3289 Typ := Empty;
3291 if Nkind (N) in N_Has_Etype then
3292 if not Is_Frozen (Etype (N)) then
3293 Typ := Etype (N);
3295 -- Base type may be an derived numeric type that is frozen at
3296 -- the point of declaration, but first_subtype is still unfrozen.
3298 elsif not Is_Frozen (First_Subtype (Etype (N))) then
3299 Typ := First_Subtype (Etype (N));
3300 end if;
3301 end if;
3303 -- For entity name, freeze entity if not frozen already. A special
3304 -- exception occurs for an identifier that did not come from source.
3305 -- We don't let such identifiers freeze a non-internal entity, i.e.
3306 -- an entity that did come from source, since such an identifier was
3307 -- generated by the expander, and cannot have any semantic effect on
3308 -- the freezing semantics. For example, this stops the parameter of
3309 -- an initialization procedure from freezing the variable.
3311 if Is_Entity_Name (N)
3312 and then not Is_Frozen (Entity (N))
3313 and then (Nkind (N) /= N_Identifier
3314 or else Comes_From_Source (N)
3315 or else not Comes_From_Source (Entity (N)))
3316 then
3317 Nam := Entity (N);
3318 else
3319 Nam := Empty;
3320 end if;
3322 -- For an allocator freeze designated type if not frozen already.
3324 -- For an aggregate whose component type is an access type, freeze
3325 -- the designated type now, so that its freeze does not appear within
3326 -- the loop that might be created in the expansion of the aggregate.
3327 -- If the designated type is a private type without full view, the
3328 -- expression cannot contain an allocator, so the type is not frozen.
3330 Desig_Typ := Empty;
3332 case Nkind (N) is
3333 when N_Allocator =>
3334 Desig_Typ := Designated_Type (Etype (N));
3336 when N_Aggregate =>
3337 if Is_Array_Type (Etype (N))
3338 and then Is_Access_Type (Component_Type (Etype (N)))
3339 then
3340 Desig_Typ := Designated_Type (Component_Type (Etype (N)));
3341 end if;
3343 when N_Selected_Component |
3344 N_Indexed_Component |
3345 N_Slice =>
3347 if Is_Access_Type (Etype (Prefix (N))) then
3348 Desig_Typ := Designated_Type (Etype (Prefix (N)));
3349 end if;
3351 when others =>
3352 null;
3353 end case;
3355 if Desig_Typ /= Empty
3356 and then (Is_Frozen (Desig_Typ)
3357 or else (not Is_Fully_Defined (Desig_Typ)))
3358 then
3359 Desig_Typ := Empty;
3360 end if;
3362 -- All done if nothing needs freezing
3364 if No (Typ)
3365 and then No (Nam)
3366 and then No (Desig_Typ)
3367 then
3368 return;
3369 end if;
3371 -- Loop for looking at the right place to insert the freeze nodes
3372 -- exiting from the loop when it is appropriate to insert the freeze
3373 -- node before the current node P.
3375 -- Also checks some special exceptions to the freezing rules. These
3376 -- cases result in a direct return, bypassing the freeze action.
3378 P := N;
3379 loop
3380 Parent_P := Parent (P);
3382 -- If we don't have a parent, then we are not in a well-formed
3383 -- tree. This is an unusual case, but there are some legitimate
3384 -- situations in which this occurs, notably when the expressions
3385 -- in the range of a type declaration are resolved. We simply
3386 -- ignore the freeze request in this case. Is this right ???
3388 if No (Parent_P) then
3389 return;
3390 end if;
3392 -- See if we have got to an appropriate point in the tree
3394 case Nkind (Parent_P) is
3396 -- A special test for the exception of (RM 13.14(8)) for the
3397 -- case of per-object expressions (RM 3.8(18)) occurring in a
3398 -- component definition or a discrete subtype definition. Note
3399 -- that we test for a component declaration which includes both
3400 -- cases we are interested in, and furthermore the tree does not
3401 -- have explicit nodes for either of these two constructs.
3403 when N_Component_Declaration =>
3405 -- The case we want to test for here is an identifier that is
3406 -- a per-object expression, this is either a discriminant that
3407 -- appears in a context other than the component declaration
3408 -- or it is a reference to the type of the enclosing construct.
3410 -- For either of these cases, we skip the freezing
3412 if not In_Default_Expression
3413 and then Nkind (N) = N_Identifier
3414 and then (Present (Entity (N)))
3415 then
3416 -- We recognize the discriminant case by just looking for
3417 -- a reference to a discriminant. It can only be one for
3418 -- the enclosing construct. Skip freezing in this case.
3420 if Ekind (Entity (N)) = E_Discriminant then
3421 return;
3423 -- For the case of a reference to the enclosing record,
3424 -- (or task or protected type), we look for a type that
3425 -- matches the current scope.
3427 elsif Entity (N) = Current_Scope then
3428 return;
3429 end if;
3430 end if;
3432 -- If we have an enumeration literal that appears as the
3433 -- choice in the aggregate of an enumeration representation
3434 -- clause, then freezing does not occur (RM 13.14(10)).
3436 when N_Enumeration_Representation_Clause =>
3438 -- The case we are looking for is an enumeration literal
3440 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
3441 and then Is_Enumeration_Type (Etype (N))
3442 then
3443 -- If enumeration literal appears directly as the choice,
3444 -- do not freeze (this is the normal non-overloade case)
3446 if Nkind (Parent (N)) = N_Component_Association
3447 and then First (Choices (Parent (N))) = N
3448 then
3449 return;
3451 -- If enumeration literal appears as the name of a
3452 -- function which is the choice, then also do not freeze.
3453 -- This happens in the overloaded literal case, where the
3454 -- enumeration literal is temporarily changed to a function
3455 -- call for overloading analysis purposes.
3457 elsif Nkind (Parent (N)) = N_Function_Call
3458 and then
3459 Nkind (Parent (Parent (N))) = N_Component_Association
3460 and then
3461 First (Choices (Parent (Parent (N)))) = Parent (N)
3462 then
3463 return;
3464 end if;
3465 end if;
3467 -- Normally if the parent is a handled sequence of statements,
3468 -- then the current node must be a statement, and that is an
3469 -- appropriate place to insert a freeze node.
3471 when N_Handled_Sequence_Of_Statements =>
3473 -- An exception occurs when the sequence of statements is
3474 -- for an expander generated body that did not do the usual
3475 -- freeze all operation. In this case we usually want to
3476 -- freeze outside this body, not inside it, and we skip
3477 -- past the subprogram body that we are inside.
3479 if In_Exp_Body (Parent_P) then
3481 -- However, we *do* want to freeze at this point if we have
3482 -- an entity to freeze, and that entity is declared *inside*
3483 -- the body of the expander generated procedure. This case
3484 -- is recognized by the scope of the type, which is either
3485 -- the spec for some enclosing body, or (in the case of
3486 -- init_procs, for which there are no separate specs) the
3487 -- current scope.
3489 declare
3490 Subp : constant Node_Id := Parent (Parent_P);
3491 Cspc : Entity_Id;
3493 begin
3494 if Nkind (Subp) = N_Subprogram_Body then
3495 Cspc := Corresponding_Spec (Subp);
3497 if (Present (Typ) and then Scope (Typ) = Cspc)
3498 or else
3499 (Present (Nam) and then Scope (Nam) = Cspc)
3500 then
3501 exit;
3503 elsif Present (Typ)
3504 and then Scope (Typ) = Current_Scope
3505 and then Current_Scope = Defining_Entity (Subp)
3506 then
3507 exit;
3508 end if;
3509 end if;
3510 end;
3512 -- If not that exception to the exception, then this is
3513 -- where we delay the freeze till outside the body.
3515 Parent_P := Parent (Parent_P);
3516 Freeze_Outside := True;
3518 -- Here if normal case where we are in handled statement
3519 -- sequence and want to do the insertion right there.
3521 else
3522 exit;
3523 end if;
3525 -- If parent is a body or a spec or a block, then the current
3526 -- node is a statement or declaration and we can insert the
3527 -- freeze node before it.
3529 when N_Package_Specification |
3530 N_Package_Body |
3531 N_Subprogram_Body |
3532 N_Task_Body |
3533 N_Protected_Body |
3534 N_Entry_Body |
3535 N_Block_Statement => exit;
3537 -- The expander is allowed to define types in any statements list,
3538 -- so any of the following parent nodes also mark a freezing point
3539 -- if the actual node is in a list of statements or declarations.
3541 when N_Exception_Handler |
3542 N_If_Statement |
3543 N_Elsif_Part |
3544 N_Case_Statement_Alternative |
3545 N_Compilation_Unit_Aux |
3546 N_Selective_Accept |
3547 N_Accept_Alternative |
3548 N_Delay_Alternative |
3549 N_Conditional_Entry_Call |
3550 N_Entry_Call_Alternative |
3551 N_Triggering_Alternative |
3552 N_Abortable_Part |
3553 N_Freeze_Entity =>
3555 exit when Is_List_Member (P);
3557 -- Note: The N_Loop_Statement is a special case. A type that
3558 -- appears in the source can never be frozen in a loop (this
3559 -- occurs only because of a loop expanded by the expander),
3560 -- so we keep on going. Otherwise we terminate the search.
3561 -- Same is true of any entity which comes from source. (if they
3562 -- have a predefined type, that type does not appear to come
3563 -- from source, but the entity should not be frozen here).
3565 when N_Loop_Statement =>
3566 exit when not Comes_From_Source (Etype (N))
3567 and then (No (Nam) or else not Comes_From_Source (Nam));
3569 -- For all other cases, keep looking at parents
3571 when others =>
3572 null;
3573 end case;
3575 -- We fall through the case if we did not yet find the proper
3576 -- place in the free for inserting the freeze node, so climb!
3578 P := Parent_P;
3579 end loop;
3581 -- If the expression appears in a record or an initialization
3582 -- procedure, the freeze nodes are collected and attached to
3583 -- the current scope, to be inserted and analyzed on exit from
3584 -- the scope, to insure that generated entities appear in the
3585 -- correct scope. If the expression is a default for a discriminant
3586 -- specification, the scope is still void. The expression can also
3587 -- appear in the discriminant part of a private or concurrent type.
3589 -- The other case requiring this special handling is if we are in
3590 -- a default expression, since in that case we are about to freeze
3591 -- a static type, and the freeze scope needs to be the outer scope,
3592 -- not the scope of the subprogram with the default parameter.
3594 -- For default expressions in generic units, the Move_Freeze_Nodes
3595 -- mechanism (see sem_ch12.adb) takes care of placing them at the
3596 -- proper place, after the generic unit.
3598 if (In_Def_Exp and not Inside_A_Generic)
3599 or else Freeze_Outside
3600 or else (Is_Type (Current_Scope)
3601 and then (not Is_Concurrent_Type (Current_Scope)
3602 or else not Has_Completion (Current_Scope)))
3603 or else Ekind (Current_Scope) = E_Void
3604 then
3605 declare
3606 Loc : constant Source_Ptr := Sloc (Current_Scope);
3607 Freeze_Nodes : List_Id := No_List;
3609 begin
3610 if Present (Desig_Typ) then
3611 Freeze_And_Append (Desig_Typ, Loc, Freeze_Nodes);
3612 end if;
3614 if Present (Typ) then
3615 Freeze_And_Append (Typ, Loc, Freeze_Nodes);
3616 end if;
3618 if Present (Nam) then
3619 Freeze_And_Append (Nam, Loc, Freeze_Nodes);
3620 end if;
3622 if Is_Non_Empty_List (Freeze_Nodes) then
3623 if No (Scope_Stack.Table
3624 (Scope_Stack.Last).Pending_Freeze_Actions)
3625 then
3626 Scope_Stack.Table
3627 (Scope_Stack.Last).Pending_Freeze_Actions :=
3628 Freeze_Nodes;
3629 else
3630 Append_List (Freeze_Nodes, Scope_Stack.Table
3631 (Scope_Stack.Last).Pending_Freeze_Actions);
3632 end if;
3633 end if;
3634 end;
3636 return;
3637 end if;
3639 -- Now we have the right place to do the freezing. First, a special
3640 -- adjustment, if we are in default expression analysis mode, these
3641 -- freeze actions must not be thrown away (normally all inserted
3642 -- actions are thrown away in this mode. However, the freeze actions
3643 -- are from static expressions and one of the important reasons we
3644 -- are doing this special analysis is to get these freeze actions.
3645 -- Therefore we turn off the In_Default_Expression mode to propagate
3646 -- these freeze actions. This also means they get properly analyzed
3647 -- and expanded.
3649 In_Default_Expression := False;
3651 -- Freeze the designated type of an allocator (RM 13.14(13))
3653 if Present (Desig_Typ) then
3654 Freeze_Before (P, Desig_Typ);
3655 end if;
3657 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
3658 -- the enumeration representation clause exception in the loop above.
3660 if Present (Typ) then
3661 Freeze_Before (P, Typ);
3662 end if;
3664 -- Freeze name if one is present (RM 13.14(11))
3666 if Present (Nam) then
3667 Freeze_Before (P, Nam);
3668 end if;
3670 In_Default_Expression := In_Def_Exp;
3671 end Freeze_Expression;
3673 -----------------------------
3674 -- Freeze_Fixed_Point_Type --
3675 -----------------------------
3677 -- Certain fixed-point types and subtypes, including implicit base
3678 -- types and declared first subtypes, have not yet set up a range.
3679 -- This is because the range cannot be set until the Small and Size
3680 -- values are known, and these are not known till the type is frozen.
3682 -- To signal this case, Scalar_Range contains an unanalyzed syntactic
3683 -- range whose bounds are unanalyzed real literals. This routine will
3684 -- recognize this case, and transform this range node into a properly
3685 -- typed range with properly analyzed and resolved values.
3687 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
3688 Rng : constant Node_Id := Scalar_Range (Typ);
3689 Lo : constant Node_Id := Low_Bound (Rng);
3690 Hi : constant Node_Id := High_Bound (Rng);
3691 Btyp : constant Entity_Id := Base_Type (Typ);
3692 Brng : constant Node_Id := Scalar_Range (Btyp);
3693 BLo : constant Node_Id := Low_Bound (Brng);
3694 BHi : constant Node_Id := High_Bound (Brng);
3695 Small : constant Ureal := Small_Value (Typ);
3696 Loval : Ureal;
3697 Hival : Ureal;
3698 Atype : Entity_Id;
3700 Actual_Size : Nat;
3702 function Fsize (Lov, Hiv : Ureal) return Nat;
3703 -- Returns size of type with given bounds. Also leaves these
3704 -- bounds set as the current bounds of the Typ.
3706 -----------
3707 -- Fsize --
3708 -----------
3710 function Fsize (Lov, Hiv : Ureal) return Nat is
3711 begin
3712 Set_Realval (Lo, Lov);
3713 Set_Realval (Hi, Hiv);
3714 return Minimum_Size (Typ);
3715 end Fsize;
3717 -- Start of processing for Freeze_Fixed_Point_Type
3719 begin
3720 -- If Esize of a subtype has not previously been set, set it now
3722 if Unknown_Esize (Typ) then
3723 Atype := Ancestor_Subtype (Typ);
3725 if Present (Atype) then
3726 Set_Esize (Typ, Esize (Atype));
3727 else
3728 Set_Esize (Typ, Esize (Base_Type (Typ)));
3729 end if;
3730 end if;
3732 -- Immediate return if the range is already analyzed. This means
3733 -- that the range is already set, and does not need to be computed
3734 -- by this routine.
3736 if Analyzed (Rng) then
3737 return;
3738 end if;
3740 -- Immediate return if either of the bounds raises Constraint_Error
3742 if Raises_Constraint_Error (Lo)
3743 or else Raises_Constraint_Error (Hi)
3744 then
3745 return;
3746 end if;
3748 Loval := Realval (Lo);
3749 Hival := Realval (Hi);
3751 -- Ordinary fixed-point case
3753 if Is_Ordinary_Fixed_Point_Type (Typ) then
3755 -- For the ordinary fixed-point case, we are allowed to fudge the
3756 -- end-points up or down by small. Generally we prefer to fudge
3757 -- up, i.e. widen the bounds for non-model numbers so that the
3758 -- end points are included. However there are cases in which this
3759 -- cannot be done, and indeed cases in which we may need to narrow
3760 -- the bounds. The following circuit makes the decision.
3762 -- Note: our terminology here is that Incl_EP means that the
3763 -- bounds are widened by Small if necessary to include the end
3764 -- points, and Excl_EP means that the bounds are narrowed by
3765 -- Small to exclude the end-points if this reduces the size.
3767 -- Note that in the Incl case, all we care about is including the
3768 -- end-points. In the Excl case, we want to narrow the bounds as
3769 -- much as permitted by the RM, to give the smallest possible size.
3771 Fudge : declare
3772 Loval_Incl_EP : Ureal;
3773 Hival_Incl_EP : Ureal;
3775 Loval_Excl_EP : Ureal;
3776 Hival_Excl_EP : Ureal;
3778 Size_Incl_EP : Nat;
3779 Size_Excl_EP : Nat;
3781 Model_Num : Ureal;
3782 First_Subt : Entity_Id;
3783 Actual_Lo : Ureal;
3784 Actual_Hi : Ureal;
3786 begin
3787 -- First step. Base types are required to be symmetrical. Right
3788 -- now, the base type range is a copy of the first subtype range.
3789 -- This will be corrected before we are done, but right away we
3790 -- need to deal with the case where both bounds are non-negative.
3791 -- In this case, we set the low bound to the negative of the high
3792 -- bound, to make sure that the size is computed to include the
3793 -- required sign. Note that we do not need to worry about the
3794 -- case of both bounds negative, because the sign will be dealt
3795 -- with anyway. Furthermore we can't just go making such a bound
3796 -- symmetrical, since in a twos-complement system, there is an
3797 -- extra negative value which could not be accomodated on the
3798 -- positive side.
3800 if Typ = Btyp
3801 and then not UR_Is_Negative (Loval)
3802 and then Hival > Loval
3803 then
3804 Loval := -Hival;
3805 Set_Realval (Lo, Loval);
3806 end if;
3808 -- Compute the fudged bounds. If the number is a model number,
3809 -- then we do nothing to include it, but we are allowed to
3810 -- backoff to the next adjacent model number when we exclude
3811 -- it. If it is not a model number then we straddle the two
3812 -- values with the model numbers on either side.
3814 Model_Num := UR_Trunc (Loval / Small) * Small;
3816 if Loval = Model_Num then
3817 Loval_Incl_EP := Model_Num;
3818 else
3819 Loval_Incl_EP := Model_Num - Small;
3820 end if;
3822 -- The low value excluding the end point is Small greater, but
3823 -- we do not do this exclusion if the low value is positive,
3824 -- since it can't help the size and could actually hurt by
3825 -- crossing the high bound.
3827 if UR_Is_Negative (Loval_Incl_EP) then
3828 Loval_Excl_EP := Loval_Incl_EP + Small;
3829 else
3830 Loval_Excl_EP := Loval_Incl_EP;
3831 end if;
3833 -- Similar processing for upper bound and high value
3835 Model_Num := UR_Trunc (Hival / Small) * Small;
3837 if Hival = Model_Num then
3838 Hival_Incl_EP := Model_Num;
3839 else
3840 Hival_Incl_EP := Model_Num + Small;
3841 end if;
3843 if UR_Is_Positive (Hival_Incl_EP) then
3844 Hival_Excl_EP := Hival_Incl_EP - Small;
3845 else
3846 Hival_Excl_EP := Hival_Incl_EP;
3847 end if;
3849 -- One further adjustment is needed. In the case of subtypes,
3850 -- we cannot go outside the range of the base type, or we get
3851 -- peculiarities, and the base type range is already set. This
3852 -- only applies to the Incl values, since clearly the Excl
3853 -- values are already as restricted as they are allowed to be.
3855 if Typ /= Btyp then
3856 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
3857 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
3858 end if;
3860 -- Get size including and excluding end points
3862 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
3863 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
3865 -- No need to exclude end-points if it does not reduce size
3867 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
3868 Loval_Excl_EP := Loval_Incl_EP;
3869 end if;
3871 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
3872 Hival_Excl_EP := Hival_Incl_EP;
3873 end if;
3875 -- Now we set the actual size to be used. We want to use the
3876 -- bounds fudged up to include the end-points but only if this
3877 -- can be done without violating a specifically given size
3878 -- size clause or causing an unacceptable increase in size.
3880 -- Case of size clause given
3882 if Has_Size_Clause (Typ) then
3884 -- Use the inclusive size only if it is consistent with
3885 -- the explicitly specified size.
3887 if Size_Incl_EP <= RM_Size (Typ) then
3888 Actual_Lo := Loval_Incl_EP;
3889 Actual_Hi := Hival_Incl_EP;
3890 Actual_Size := Size_Incl_EP;
3892 -- If the inclusive size is too large, we try excluding
3893 -- the end-points (will be caught later if does not work).
3895 else
3896 Actual_Lo := Loval_Excl_EP;
3897 Actual_Hi := Hival_Excl_EP;
3898 Actual_Size := Size_Excl_EP;
3899 end if;
3901 -- Case of size clause not given
3903 else
3904 -- If we have a base type whose corresponding first subtype
3905 -- has an explicit size that is large enough to include our
3906 -- end-points, then do so. There is no point in working hard
3907 -- to get a base type whose size is smaller than the specified
3908 -- size of the first subtype.
3910 First_Subt := First_Subtype (Typ);
3912 if Has_Size_Clause (First_Subt)
3913 and then Size_Incl_EP <= Esize (First_Subt)
3914 then
3915 Actual_Size := Size_Incl_EP;
3916 Actual_Lo := Loval_Incl_EP;
3917 Actual_Hi := Hival_Incl_EP;
3919 -- If excluding the end-points makes the size smaller and
3920 -- results in a size of 8,16,32,64, then we take the smaller
3921 -- size. For the 64 case, this is compulsory. For the other
3922 -- cases, it seems reasonable. We like to include end points
3923 -- if we can, but not at the expense of moving to the next
3924 -- natural boundary of size.
3926 elsif Size_Incl_EP /= Size_Excl_EP
3927 and then
3928 (Size_Excl_EP = 8 or else
3929 Size_Excl_EP = 16 or else
3930 Size_Excl_EP = 32 or else
3931 Size_Excl_EP = 64)
3932 then
3933 Actual_Size := Size_Excl_EP;
3934 Actual_Lo := Loval_Excl_EP;
3935 Actual_Hi := Hival_Excl_EP;
3937 -- Otherwise we can definitely include the end points
3939 else
3940 Actual_Size := Size_Incl_EP;
3941 Actual_Lo := Loval_Incl_EP;
3942 Actual_Hi := Hival_Incl_EP;
3943 end if;
3945 -- One pathological case: normally we never fudge a low
3946 -- bound down, since it would seem to increase the size
3947 -- (if it has any effect), but for ranges containing a
3948 -- single value, or no values, the high bound can be
3949 -- small too large. Consider:
3951 -- type t is delta 2.0**(-14)
3952 -- range 131072.0 .. 0;
3954 -- That lower bound is *just* outside the range of 32
3955 -- bits, and does need fudging down in this case. Note
3956 -- that the bounds will always have crossed here, since
3957 -- the high bound will be fudged down if necessary, as
3958 -- in the case of:
3960 -- type t is delta 2.0**(-14)
3961 -- range 131072.0 .. 131072.0;
3963 -- So we can detect the situation by looking for crossed
3964 -- bounds, and if the bounds are crossed, and the low
3965 -- bound is greater than zero, we will always back it
3966 -- off by small, since this is completely harmless.
3968 if Actual_Lo > Actual_Hi then
3969 if UR_Is_Positive (Actual_Lo) then
3970 Actual_Lo := Loval_Incl_EP - Small;
3971 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
3973 -- And of course, we need to do exactly the same parallel
3974 -- fudge for flat ranges in the negative region.
3976 elsif UR_Is_Negative (Actual_Hi) then
3977 Actual_Hi := Hival_Incl_EP + Small;
3978 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
3979 end if;
3980 end if;
3981 end if;
3983 Set_Realval (Lo, Actual_Lo);
3984 Set_Realval (Hi, Actual_Hi);
3985 end Fudge;
3987 -- For the decimal case, none of this fudging is required, since there
3988 -- are no end-point problems in the decimal case (the end-points are
3989 -- always included).
3991 else
3992 Actual_Size := Fsize (Loval, Hival);
3993 end if;
3995 -- At this stage, the actual size has been calculated and the proper
3996 -- required bounds are stored in the low and high bounds.
3998 if Actual_Size > 64 then
3999 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
4000 Error_Msg_N
4001 ("size required (^) for type& too large, maximum is 64", Typ);
4002 Actual_Size := 64;
4003 end if;
4005 -- Check size against explicit given size
4007 if Has_Size_Clause (Typ) then
4008 if Actual_Size > RM_Size (Typ) then
4009 Error_Msg_Uint_1 := RM_Size (Typ);
4010 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
4011 Error_Msg_NE
4012 ("size given (^) for type& too small, minimum is ^",
4013 Size_Clause (Typ), Typ);
4015 else
4016 Actual_Size := UI_To_Int (Esize (Typ));
4017 end if;
4019 -- Increase size to next natural boundary if no size clause given
4021 else
4022 if Actual_Size <= 8 then
4023 Actual_Size := 8;
4024 elsif Actual_Size <= 16 then
4025 Actual_Size := 16;
4026 elsif Actual_Size <= 32 then
4027 Actual_Size := 32;
4028 else
4029 Actual_Size := 64;
4030 end if;
4032 Init_Esize (Typ, Actual_Size);
4033 Adjust_Esize_For_Alignment (Typ);
4034 end if;
4036 -- If we have a base type, then expand the bounds so that they
4037 -- extend to the full width of the allocated size in bits, to
4038 -- avoid junk range checks on intermediate computations.
4040 if Base_Type (Typ) = Typ then
4041 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
4042 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
4043 end if;
4045 -- Final step is to reanalyze the bounds using the proper type
4046 -- and set the Corresponding_Integer_Value fields of the literals.
4048 Set_Etype (Lo, Empty);
4049 Set_Analyzed (Lo, False);
4050 Analyze (Lo);
4052 -- Resolve with universal fixed if the base type, and the base
4053 -- type if it is a subtype. Note we can't resolve the base type
4054 -- with itself, that would be a reference before definition.
4056 if Typ = Btyp then
4057 Resolve (Lo, Universal_Fixed);
4058 else
4059 Resolve (Lo, Btyp);
4060 end if;
4062 -- Set corresponding integer value for bound
4064 Set_Corresponding_Integer_Value
4065 (Lo, UR_To_Uint (Realval (Lo) / Small));
4067 -- Similar processing for high bound
4069 Set_Etype (Hi, Empty);
4070 Set_Analyzed (Hi, False);
4071 Analyze (Hi);
4073 if Typ = Btyp then
4074 Resolve (Hi, Universal_Fixed);
4075 else
4076 Resolve (Hi, Btyp);
4077 end if;
4079 Set_Corresponding_Integer_Value
4080 (Hi, UR_To_Uint (Realval (Hi) / Small));
4082 -- Set type of range to correspond to bounds
4084 Set_Etype (Rng, Etype (Lo));
4086 -- Set Esize to calculated size if not set already
4088 if Unknown_Esize (Typ) then
4089 Init_Esize (Typ, Actual_Size);
4090 end if;
4092 -- Set RM_Size if not already set. If already set, check value
4094 declare
4095 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
4097 begin
4098 if RM_Size (Typ) /= Uint_0 then
4099 if RM_Size (Typ) < Minsiz then
4100 Error_Msg_Uint_1 := RM_Size (Typ);
4101 Error_Msg_Uint_2 := Minsiz;
4102 Error_Msg_NE
4103 ("size given (^) for type& too small, minimum is ^",
4104 Size_Clause (Typ), Typ);
4105 end if;
4107 else
4108 Set_RM_Size (Typ, Minsiz);
4109 end if;
4110 end;
4111 end Freeze_Fixed_Point_Type;
4113 ------------------
4114 -- Freeze_Itype --
4115 ------------------
4117 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
4118 L : List_Id;
4120 begin
4121 Set_Has_Delayed_Freeze (T);
4122 L := Freeze_Entity (T, Sloc (N));
4124 if Is_Non_Empty_List (L) then
4125 Insert_Actions (N, L);
4126 end if;
4127 end Freeze_Itype;
4129 --------------------------
4130 -- Freeze_Static_Object --
4131 --------------------------
4133 procedure Freeze_Static_Object (E : Entity_Id) is
4135 Cannot_Be_Static : exception;
4136 -- Exception raised if the type of a static object cannot be made
4137 -- static. This happens if the type depends on non-global objects.
4139 procedure Ensure_Expression_Is_SA (N : Node_Id);
4140 -- Called to ensure that an expression used as part of a type
4141 -- definition is statically allocatable, which means that the type
4142 -- of the expression is statically allocatable, and the expression
4143 -- is either static, or a reference to a library level constant.
4145 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
4146 -- Called to mark a type as static, checking that it is possible
4147 -- to set the type as static. If it is not possible, then the
4148 -- exception Cannot_Be_Static is raised.
4150 -----------------------------
4151 -- Ensure_Expression_Is_SA --
4152 -----------------------------
4154 procedure Ensure_Expression_Is_SA (N : Node_Id) is
4155 Ent : Entity_Id;
4157 begin
4158 Ensure_Type_Is_SA (Etype (N));
4160 if Is_Static_Expression (N) then
4161 return;
4163 elsif Nkind (N) = N_Identifier then
4164 Ent := Entity (N);
4166 if Present (Ent)
4167 and then Ekind (Ent) = E_Constant
4168 and then Is_Library_Level_Entity (Ent)
4169 then
4170 return;
4171 end if;
4172 end if;
4174 raise Cannot_Be_Static;
4175 end Ensure_Expression_Is_SA;
4177 -----------------------
4178 -- Ensure_Type_Is_SA --
4179 -----------------------
4181 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
4182 N : Node_Id;
4183 C : Entity_Id;
4185 begin
4186 -- If type is library level, we are all set
4188 if Is_Library_Level_Entity (Typ) then
4189 return;
4190 end if;
4192 -- We are also OK if the type is already marked as statically
4193 -- allocated, which means we processed it before.
4195 if Is_Statically_Allocated (Typ) then
4196 return;
4197 end if;
4199 -- Mark type as statically allocated
4201 Set_Is_Statically_Allocated (Typ);
4203 -- Check that it is safe to statically allocate this type
4205 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
4206 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
4207 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
4209 elsif Is_Array_Type (Typ) then
4210 N := First_Index (Typ);
4211 while Present (N) loop
4212 Ensure_Type_Is_SA (Etype (N));
4213 Next_Index (N);
4214 end loop;
4216 Ensure_Type_Is_SA (Component_Type (Typ));
4218 elsif Is_Access_Type (Typ) then
4219 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
4221 declare
4222 F : Entity_Id;
4223 T : constant Entity_Id := Etype (Designated_Type (Typ));
4225 begin
4226 if T /= Standard_Void_Type then
4227 Ensure_Type_Is_SA (T);
4228 end if;
4230 F := First_Formal (Designated_Type (Typ));
4232 while Present (F) loop
4233 Ensure_Type_Is_SA (Etype (F));
4234 Next_Formal (F);
4235 end loop;
4236 end;
4238 else
4239 Ensure_Type_Is_SA (Designated_Type (Typ));
4240 end if;
4242 elsif Is_Record_Type (Typ) then
4243 C := First_Entity (Typ);
4245 while Present (C) loop
4246 if Ekind (C) = E_Discriminant
4247 or else Ekind (C) = E_Component
4248 then
4249 Ensure_Type_Is_SA (Etype (C));
4251 elsif Is_Type (C) then
4252 Ensure_Type_Is_SA (C);
4253 end if;
4255 Next_Entity (C);
4256 end loop;
4258 elsif Ekind (Typ) = E_Subprogram_Type then
4259 Ensure_Type_Is_SA (Etype (Typ));
4261 C := First_Formal (Typ);
4262 while Present (C) loop
4263 Ensure_Type_Is_SA (Etype (C));
4264 Next_Formal (C);
4265 end loop;
4267 else
4268 raise Cannot_Be_Static;
4269 end if;
4270 end Ensure_Type_Is_SA;
4272 -- Start of processing for Freeze_Static_Object
4274 begin
4275 Ensure_Type_Is_SA (Etype (E));
4277 -- Reset True_Constant flag, since something strange is going on
4278 -- with the scoping here, and our simple value tracing may not
4279 -- be sufficient for this indication to be reliable. We kill the
4280 -- Constant_Value indication for the same reason.
4282 Set_Is_True_Constant (E, False);
4283 Set_Current_Value (E, Empty);
4285 exception
4286 when Cannot_Be_Static =>
4288 -- If the object that cannot be static is imported or exported,
4289 -- then we give an error message saying that this object cannot
4290 -- be imported or exported.
4292 if Is_Imported (E) then
4293 Error_Msg_N
4294 ("& cannot be imported (local type is not constant)", E);
4296 -- Otherwise must be exported, something is wrong if compiler
4297 -- is marking something as statically allocated which cannot be).
4299 else pragma Assert (Is_Exported (E));
4300 Error_Msg_N
4301 ("& cannot be exported (local type is not constant)", E);
4302 end if;
4303 end Freeze_Static_Object;
4305 -----------------------
4306 -- Freeze_Subprogram --
4307 -----------------------
4309 procedure Freeze_Subprogram (E : Entity_Id) is
4310 Retype : Entity_Id;
4311 F : Entity_Id;
4313 begin
4314 -- Subprogram may not have an address clause unless it is imported
4316 if Present (Address_Clause (E)) then
4317 if not Is_Imported (E) then
4318 Error_Msg_N
4319 ("address clause can only be given " &
4320 "for imported subprogram",
4321 Name (Address_Clause (E)));
4322 end if;
4323 end if;
4325 -- Reset the Pure indication on an imported subprogram unless an
4326 -- explicit Pure_Function pragma was present. We do this because
4327 -- otherwise it is an insidious error to call a non-pure function
4328 -- from a pure unit and have calls mysteriously optimized away.
4329 -- What happens here is that the Import can bypass the normal
4330 -- check to ensure that pure units call only pure subprograms.
4332 if Is_Imported (E)
4333 and then Is_Pure (E)
4334 and then not Has_Pragma_Pure_Function (E)
4335 then
4336 Set_Is_Pure (E, False);
4337 end if;
4339 -- For non-foreign convention subprograms, this is where we create
4340 -- the extra formals (for accessibility level and constrained bit
4341 -- information). We delay this till the freeze point precisely so
4342 -- that we know the convention!
4344 if not Has_Foreign_Convention (E) then
4345 Create_Extra_Formals (E);
4346 Set_Mechanisms (E);
4348 -- If this is convention Ada and a Valued_Procedure, that's odd
4350 if Ekind (E) = E_Procedure
4351 and then Is_Valued_Procedure (E)
4352 and then Convention (E) = Convention_Ada
4353 and then Warn_On_Export_Import
4354 then
4355 Error_Msg_N
4356 ("?Valued_Procedure has no effect for convention Ada", E);
4357 Set_Is_Valued_Procedure (E, False);
4358 end if;
4360 -- Case of foreign convention
4362 else
4363 Set_Mechanisms (E);
4365 -- For foreign conventions, warn about return of an
4366 -- unconstrained array.
4368 -- Note: we *do* allow a return by descriptor for the VMS case,
4369 -- though here there is probably more to be done ???
4371 if Ekind (E) = E_Function then
4372 Retype := Underlying_Type (Etype (E));
4374 -- If no return type, probably some other error, e.g. a
4375 -- missing full declaration, so ignore.
4377 if No (Retype) then
4378 null;
4380 -- If the return type is generic, we have emitted a warning
4381 -- earlier on, and there is nothing else to check here.
4382 -- Specific instantiations may lead to erroneous behavior.
4384 elsif Is_Generic_Type (Etype (E)) then
4385 null;
4387 elsif Is_Array_Type (Retype)
4388 and then not Is_Constrained (Retype)
4389 and then Mechanism (E) not in Descriptor_Codes
4390 and then Warn_On_Export_Import
4391 then
4392 Error_Msg_N
4393 ("?foreign convention function& should not return " &
4394 "unconstrained array", E);
4395 return;
4396 end if;
4397 end if;
4399 -- If any of the formals for an exported foreign convention
4400 -- subprogram have defaults, then emit an appropriate warning
4401 -- since this is odd (default cannot be used from non-Ada code)
4403 if Is_Exported (E) then
4404 F := First_Formal (E);
4405 while Present (F) loop
4406 if Warn_On_Export_Import
4407 and then Present (Default_Value (F))
4408 then
4409 Error_Msg_N
4410 ("?parameter cannot be defaulted in non-Ada call",
4411 Default_Value (F));
4412 end if;
4414 Next_Formal (F);
4415 end loop;
4416 end if;
4417 end if;
4419 -- For VMS, descriptor mechanisms for parameters are allowed only
4420 -- for imported subprograms.
4422 if OpenVMS_On_Target then
4423 if not Is_Imported (E) then
4424 F := First_Formal (E);
4425 while Present (F) loop
4426 if Mechanism (F) in Descriptor_Codes then
4427 Error_Msg_N
4428 ("descriptor mechanism for parameter not permitted", F);
4429 Error_Msg_N
4430 ("\can only be used for imported subprogram", F);
4431 end if;
4433 Next_Formal (F);
4434 end loop;
4435 end if;
4436 end if;
4437 end Freeze_Subprogram;
4439 ----------------------
4440 -- Is_Fully_Defined --
4441 ----------------------
4443 function Is_Fully_Defined (T : Entity_Id) return Boolean is
4444 begin
4445 if Ekind (T) = E_Class_Wide_Type then
4446 return Is_Fully_Defined (Etype (T));
4448 elsif Is_Array_Type (T) then
4449 return Is_Fully_Defined (Component_Type (T));
4451 elsif Is_Record_Type (T)
4452 and not Is_Private_Type (T)
4453 then
4454 -- Verify that the record type has no components with
4455 -- private types without completion.
4457 declare
4458 Comp : Entity_Id;
4460 begin
4461 Comp := First_Component (T);
4463 while Present (Comp) loop
4464 if not Is_Fully_Defined (Etype (Comp)) then
4465 return False;
4466 end if;
4468 Next_Component (Comp);
4469 end loop;
4470 return True;
4471 end;
4473 else return not Is_Private_Type (T)
4474 or else Present (Full_View (Base_Type (T)));
4475 end if;
4476 end Is_Fully_Defined;
4478 ---------------------------------
4479 -- Process_Default_Expressions --
4480 ---------------------------------
4482 procedure Process_Default_Expressions
4483 (E : Entity_Id;
4484 After : in out Node_Id)
4486 Loc : constant Source_Ptr := Sloc (E);
4487 Dbody : Node_Id;
4488 Formal : Node_Id;
4489 Dcopy : Node_Id;
4490 Dnam : Entity_Id;
4492 begin
4493 Set_Default_Expressions_Processed (E);
4495 -- A subprogram instance and its associated anonymous subprogram
4496 -- share their signature. The default expression functions are defined
4497 -- in the wrapper packages for the anonymous subprogram, and should
4498 -- not be generated again for the instance.
4500 if Is_Generic_Instance (E)
4501 and then Present (Alias (E))
4502 and then Default_Expressions_Processed (Alias (E))
4503 then
4504 return;
4505 end if;
4507 Formal := First_Formal (E);
4509 while Present (Formal) loop
4510 if Present (Default_Value (Formal)) then
4512 -- We work with a copy of the default expression because we
4513 -- do not want to disturb the original, since this would mess
4514 -- up the conformance checking.
4516 Dcopy := New_Copy_Tree (Default_Value (Formal));
4518 -- The analysis of the expression may generate insert actions,
4519 -- which of course must not be executed. We wrap those actions
4520 -- in a procedure that is not called, and later on eliminated.
4521 -- The following cases have no side-effects, and are analyzed
4522 -- directly.
4524 if Nkind (Dcopy) = N_Identifier
4525 or else Nkind (Dcopy) = N_Expanded_Name
4526 or else Nkind (Dcopy) = N_Integer_Literal
4527 or else (Nkind (Dcopy) = N_Real_Literal
4528 and then not Vax_Float (Etype (Dcopy)))
4529 or else Nkind (Dcopy) = N_Character_Literal
4530 or else Nkind (Dcopy) = N_String_Literal
4531 or else Nkind (Dcopy) = N_Null
4532 or else (Nkind (Dcopy) = N_Attribute_Reference
4533 and then
4534 Attribute_Name (Dcopy) = Name_Null_Parameter)
4535 then
4537 -- If there is no default function, we must still do a full
4538 -- analyze call on the default value, to ensure that all
4539 -- error checks are performed, e.g. those associated with
4540 -- static evaluation. Note that this branch will always be
4541 -- taken if the analyzer is turned off (but we still need the
4542 -- error checks).
4544 -- Note: the setting of parent here is to meet the requirement
4545 -- that we can only analyze the expression while attached to
4546 -- the tree. Really the requirement is that the parent chain
4547 -- be set, we don't actually need to be in the tree.
4549 Set_Parent (Dcopy, Declaration_Node (Formal));
4550 Analyze (Dcopy);
4552 -- Default expressions are resolved with their own type if the
4553 -- context is generic, to avoid anomalies with private types.
4555 if Ekind (Scope (E)) = E_Generic_Package then
4556 Resolve (Dcopy);
4557 else
4558 Resolve (Dcopy, Etype (Formal));
4559 end if;
4561 -- If that resolved expression will raise constraint error,
4562 -- then flag the default value as raising constraint error.
4563 -- This allows a proper error message on the calls.
4565 if Raises_Constraint_Error (Dcopy) then
4566 Set_Raises_Constraint_Error (Default_Value (Formal));
4567 end if;
4569 -- If the default is a parameterless call, we use the name of
4570 -- the called function directly, and there is no body to build.
4572 elsif Nkind (Dcopy) = N_Function_Call
4573 and then No (Parameter_Associations (Dcopy))
4574 then
4575 null;
4577 -- Else construct and analyze the body of a wrapper procedure
4578 -- that contains an object declaration to hold the expression.
4579 -- Given that this is done only to complete the analysis, it
4580 -- simpler to build a procedure than a function which might
4581 -- involve secondary stack expansion.
4583 else
4584 Dnam :=
4585 Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
4587 Dbody :=
4588 Make_Subprogram_Body (Loc,
4589 Specification =>
4590 Make_Procedure_Specification (Loc,
4591 Defining_Unit_Name => Dnam),
4593 Declarations => New_List (
4594 Make_Object_Declaration (Loc,
4595 Defining_Identifier =>
4596 Make_Defining_Identifier (Loc,
4597 New_Internal_Name ('T')),
4598 Object_Definition =>
4599 New_Occurrence_Of (Etype (Formal), Loc),
4600 Expression => New_Copy_Tree (Dcopy))),
4602 Handled_Statement_Sequence =>
4603 Make_Handled_Sequence_Of_Statements (Loc,
4604 Statements => New_List));
4606 Set_Scope (Dnam, Scope (E));
4607 Set_Assignment_OK (First (Declarations (Dbody)));
4608 Set_Is_Eliminated (Dnam);
4609 Insert_After (After, Dbody);
4610 Analyze (Dbody);
4611 After := Dbody;
4612 end if;
4613 end if;
4615 Next_Formal (Formal);
4616 end loop;
4618 end Process_Default_Expressions;
4620 ----------------------------------------
4621 -- Set_Component_Alignment_If_Not_Set --
4622 ----------------------------------------
4624 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
4625 begin
4626 -- Ignore if not base type, subtypes don't need anything
4628 if Typ /= Base_Type (Typ) then
4629 return;
4630 end if;
4632 -- Do not override existing representation
4634 if Is_Packed (Typ) then
4635 return;
4637 elsif Has_Specified_Layout (Typ) then
4638 return;
4640 elsif Component_Alignment (Typ) /= Calign_Default then
4641 return;
4643 else
4644 Set_Component_Alignment
4645 (Typ, Scope_Stack.Table
4646 (Scope_Stack.Last).Component_Alignment_Default);
4647 end if;
4648 end Set_Component_Alignment_If_Not_Set;
4650 ---------------------------
4651 -- Set_Debug_Info_Needed --
4652 ---------------------------
4654 procedure Set_Debug_Info_Needed (T : Entity_Id) is
4655 begin
4656 if No (T)
4657 or else Needs_Debug_Info (T)
4658 or else Debug_Info_Off (T)
4659 then
4660 return;
4661 else
4662 Set_Needs_Debug_Info (T);
4663 end if;
4665 if Is_Object (T) then
4666 Set_Debug_Info_Needed (Etype (T));
4668 elsif Is_Type (T) then
4669 Set_Debug_Info_Needed (Etype (T));
4671 if Is_Record_Type (T) then
4672 declare
4673 Ent : Entity_Id := First_Entity (T);
4674 begin
4675 while Present (Ent) loop
4676 Set_Debug_Info_Needed (Ent);
4677 Next_Entity (Ent);
4678 end loop;
4679 end;
4681 elsif Is_Array_Type (T) then
4682 Set_Debug_Info_Needed (Component_Type (T));
4684 declare
4685 Indx : Node_Id := First_Index (T);
4686 begin
4687 while Present (Indx) loop
4688 Set_Debug_Info_Needed (Etype (Indx));
4689 Indx := Next_Index (Indx);
4690 end loop;
4691 end;
4693 if Is_Packed (T) then
4694 Set_Debug_Info_Needed (Packed_Array_Type (T));
4695 end if;
4697 elsif Is_Access_Type (T) then
4698 Set_Debug_Info_Needed (Directly_Designated_Type (T));
4700 elsif Is_Private_Type (T) then
4701 Set_Debug_Info_Needed (Full_View (T));
4703 elsif Is_Protected_Type (T) then
4704 Set_Debug_Info_Needed (Corresponding_Record_Type (T));
4705 end if;
4706 end if;
4707 end Set_Debug_Info_Needed;
4709 ------------------
4710 -- Warn_Overlay --
4711 ------------------
4713 procedure Warn_Overlay
4714 (Expr : Node_Id;
4715 Typ : Entity_Id;
4716 Nam : Entity_Id)
4718 Ent : constant Entity_Id := Entity (Nam);
4719 -- The object to which the address clause applies.
4721 Init : Node_Id;
4722 Old : Entity_Id := Empty;
4723 Decl : Node_Id;
4725 begin
4726 -- No warning if address clause overlay warnings are off
4728 if not Address_Clause_Overlay_Warnings then
4729 return;
4730 end if;
4732 -- No warning if there is an explicit initialization
4734 Init := Original_Node (Expression (Declaration_Node (Ent)));
4736 if Present (Init) and then Comes_From_Source (Init) then
4737 return;
4738 end if;
4740 -- We only give the warning for non-imported entities of a type
4741 -- for which a non-null base init proc is defined (or for access
4742 -- types which have implicit null initialization).
4744 if Present (Expr)
4745 and then (Has_Non_Null_Base_Init_Proc (Typ)
4746 or else Is_Access_Type (Typ))
4747 and then not Is_Imported (Ent)
4748 then
4749 if Nkind (Expr) = N_Attribute_Reference
4750 and then Is_Entity_Name (Prefix (Expr))
4751 then
4752 Old := Entity (Prefix (Expr));
4754 elsif Is_Entity_Name (Expr)
4755 and then Ekind (Entity (Expr)) = E_Constant
4756 then
4757 Decl := Declaration_Node (Entity (Expr));
4759 if Nkind (Decl) = N_Object_Declaration
4760 and then Present (Expression (Decl))
4761 and then Nkind (Expression (Decl)) = N_Attribute_Reference
4762 and then Is_Entity_Name (Prefix (Expression (Decl)))
4763 then
4764 Old := Entity (Prefix (Expression (Decl)));
4766 elsif Nkind (Expr) = N_Function_Call then
4767 return;
4768 end if;
4770 -- A function call (most likely to To_Address) is probably not
4771 -- an overlay, so skip warning. Ditto if the function call was
4772 -- inlined and transformed into an entity.
4774 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
4775 return;
4776 end if;
4778 Decl := Next (Parent (Expr));
4780 -- If a pragma Import follows, we assume that it is for the current
4781 -- target of the address clause, and skip the warning.
4783 if Present (Decl)
4784 and then Nkind (Decl) = N_Pragma
4785 and then Chars (Decl) = Name_Import
4786 then
4787 return;
4788 end if;
4790 if Present (Old) then
4791 Error_Msg_Node_2 := Old;
4792 Error_Msg_N
4793 ("default initialization of & may modify &?",
4794 Nam);
4795 else
4796 Error_Msg_N
4797 ("default initialization of & may modify overlaid storage?",
4798 Nam);
4799 end if;
4801 -- Add friendly warning if initialization comes from a packed array
4802 -- component.
4804 if Is_Record_Type (Typ) then
4805 declare
4806 Comp : Entity_Id;
4808 begin
4809 Comp := First_Component (Typ);
4811 while Present (Comp) loop
4812 if Nkind (Parent (Comp)) = N_Component_Declaration
4813 and then Present (Expression (Parent (Comp)))
4814 then
4815 exit;
4816 elsif Is_Array_Type (Etype (Comp))
4817 and then Present (Packed_Array_Type (Etype (Comp)))
4818 then
4819 Error_Msg_NE
4820 ("packed array component& will be initialized to zero?",
4821 Nam, Comp);
4822 exit;
4823 else
4824 Next_Component (Comp);
4825 end if;
4826 end loop;
4827 end;
4828 end if;
4830 Error_Msg_N
4831 ("use pragma Import for & to " &
4832 "suppress initialization ('R'M B.1(24))?",
4833 Nam);
4834 end if;
4835 end Warn_Overlay;
4837 end Freeze;