1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 Free Software Foundation, Inc. --
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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
42 with Restrict; use Restrict;
43 with Rident; use Rident;
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
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
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
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
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 Undelay_Type (T : Entity_Id);
168 -- T is a type of a component that we know to be an Itype.
169 -- We don't want this to have a Freeze_Node, so ensure it doesn't.
170 -- Do the same for any Full_View or Corresponding_Record_Type.
172 procedure Warn_Overlay
176 -- Expr is the expression for an address clause for entity Nam whose type
177 -- is Typ. If Typ has a default initialization, and there is no explicit
178 -- initialization in the source declaration, check whether the address
179 -- clause might cause overlaying of an entity, and emit a warning on the
180 -- side effect that the initialization will cause.
182 -------------------------------
183 -- Adjust_Esize_For_Alignment --
184 -------------------------------
186 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
190 if Known_Esize (Typ) and then Known_Alignment (Typ) then
191 Align := Alignment_In_Bits (Typ);
193 if Align > Esize (Typ)
194 and then Align <= Standard_Long_Long_Integer_Size
196 Set_Esize (Typ, Align);
199 end Adjust_Esize_For_Alignment;
201 ------------------------------------
202 -- Build_And_Analyze_Renamed_Body --
203 ------------------------------------
205 procedure Build_And_Analyze_Renamed_Body
208 After : in out Node_Id)
210 Body_Node : constant Node_Id := Build_Renamed_Body (Decl, New_S);
213 Insert_After (After, Body_Node);
214 Mark_Rewrite_Insertion (Body_Node);
217 end Build_And_Analyze_Renamed_Body;
219 ------------------------
220 -- Build_Renamed_Body --
221 ------------------------
223 function Build_Renamed_Body
225 New_S : Entity_Id) return Node_Id
227 Loc : constant Source_Ptr := Sloc (New_S);
228 -- We use for the source location of the renamed body, the location
229 -- of the spec entity. It might seem more natural to use the location
230 -- of the renaming declaration itself, but that would be wrong, since
231 -- then the body we create would look as though it was created far
232 -- too late, and this could cause problems with elaboration order
233 -- analysis, particularly in connection with instantiations.
235 N : constant Node_Id := Unit_Declaration_Node (New_S);
236 Nam : constant Node_Id := Name (N);
238 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
239 Actuals : List_Id := No_List;
244 O_Formal : Entity_Id;
245 Param_Spec : Node_Id;
248 -- Determine the entity being renamed, which is the target of the
249 -- call statement. If the name is an explicit dereference, this is
250 -- a renaming of a subprogram type rather than a subprogram. The
251 -- name itself is fully analyzed.
253 if Nkind (Nam) = N_Selected_Component then
254 Old_S := Entity (Selector_Name (Nam));
256 elsif Nkind (Nam) = N_Explicit_Dereference then
257 Old_S := Etype (Nam);
259 elsif Nkind (Nam) = N_Indexed_Component then
260 if Is_Entity_Name (Prefix (Nam)) then
261 Old_S := Entity (Prefix (Nam));
263 Old_S := Entity (Selector_Name (Prefix (Nam)));
266 elsif Nkind (Nam) = N_Character_Literal then
267 Old_S := Etype (New_S);
270 Old_S := Entity (Nam);
273 if Is_Entity_Name (Nam) then
275 -- If the renamed entity is a predefined operator, retain full
276 -- name to ensure its visibility.
278 if Ekind (Old_S) = E_Operator
279 and then Nkind (Nam) = N_Expanded_Name
281 Call_Name := New_Copy (Name (N));
283 Call_Name := New_Reference_To (Old_S, Loc);
287 Call_Name := New_Copy (Name (N));
289 -- The original name may have been overloaded, but
290 -- is fully resolved now.
292 Set_Is_Overloaded (Call_Name, False);
295 -- For simple renamings, subsequent calls can be expanded directly
296 -- as called to the renamed entity. The body must be generated in
297 -- any case for calls they may appear elsewhere.
299 if (Ekind (Old_S) = E_Function
300 or else Ekind (Old_S) = E_Procedure)
301 and then Nkind (Decl) = N_Subprogram_Declaration
303 Set_Body_To_Inline (Decl, Old_S);
306 -- The body generated for this renaming is an internal artifact, and
307 -- does not constitute a freeze point for the called entity.
309 Set_Must_Not_Freeze (Call_Name);
311 Formal := First_Formal (Defining_Entity (Decl));
313 if Present (Formal) then
316 while Present (Formal) loop
317 Append (New_Reference_To (Formal, Loc), Actuals);
318 Next_Formal (Formal);
322 -- If the renamed entity is an entry, inherit its profile. For
323 -- other renamings as bodies, both profiles must be subtype
324 -- conformant, so it is not necessary to replace the profile given
325 -- in the declaration. However, default values that are aggregates
326 -- are rewritten when partially analyzed, so we recover the original
327 -- aggregate to insure that subsequent conformity checking works.
328 -- Similarly, if the default expression was constant-folded, recover
329 -- the original expression.
331 Formal := First_Formal (Defining_Entity (Decl));
333 if Present (Formal) then
334 O_Formal := First_Formal (Old_S);
335 Param_Spec := First (Parameter_Specifications (Spec));
337 while Present (Formal) loop
338 if Is_Entry (Old_S) then
340 if Nkind (Parameter_Type (Param_Spec)) /=
343 Set_Etype (Formal, Etype (O_Formal));
344 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
347 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
348 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
349 Nkind (Default_Value (O_Formal))
351 Set_Expression (Param_Spec,
352 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
355 Next_Formal (Formal);
356 Next_Formal (O_Formal);
361 -- If the renamed entity is a function, the generated body contains a
362 -- return statement. Otherwise, build a procedure call. If the entity is
363 -- an entry, subsequent analysis of the call will transform it into the
364 -- proper entry or protected operation call. If the renamed entity is
365 -- a character literal, return it directly.
367 if Ekind (Old_S) = E_Function
368 or else Ekind (Old_S) = E_Operator
369 or else (Ekind (Old_S) = E_Subprogram_Type
370 and then Etype (Old_S) /= Standard_Void_Type)
373 Make_Return_Statement (Loc,
375 Make_Function_Call (Loc,
377 Parameter_Associations => Actuals));
379 elsif Ekind (Old_S) = E_Enumeration_Literal then
381 Make_Return_Statement (Loc,
382 Expression => New_Occurrence_Of (Old_S, Loc));
384 elsif Nkind (Nam) = N_Character_Literal then
386 Make_Return_Statement (Loc,
387 Expression => Call_Name);
391 Make_Procedure_Call_Statement (Loc,
393 Parameter_Associations => Actuals);
396 -- Create entities for subprogram body and formals
398 Set_Defining_Unit_Name (Spec,
399 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
401 Param_Spec := First (Parameter_Specifications (Spec));
403 while Present (Param_Spec) loop
404 Set_Defining_Identifier (Param_Spec,
405 Make_Defining_Identifier (Loc,
406 Chars => Chars (Defining_Identifier (Param_Spec))));
411 Make_Subprogram_Body (Loc,
412 Specification => Spec,
413 Declarations => New_List,
414 Handled_Statement_Sequence =>
415 Make_Handled_Sequence_Of_Statements (Loc,
416 Statements => New_List (Call_Node)));
418 if Nkind (Decl) /= N_Subprogram_Declaration then
420 Make_Subprogram_Declaration (Loc,
421 Specification => Specification (N)));
424 -- Link the body to the entity whose declaration it completes. If
425 -- the body is analyzed when the renamed entity is frozen, it may be
426 -- necessary to restore the proper scope (see package Exp_Ch13).
428 if Nkind (N) = N_Subprogram_Renaming_Declaration
429 and then Present (Corresponding_Spec (N))
431 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
433 Set_Corresponding_Spec (Body_Node, New_S);
437 end Build_Renamed_Body;
439 --------------------------
440 -- Check_Address_Clause --
441 --------------------------
443 procedure Check_Address_Clause (E : Entity_Id) is
444 Addr : constant Node_Id := Address_Clause (E);
446 Decl : constant Node_Id := Declaration_Node (E);
447 Typ : constant Entity_Id := Etype (E);
450 if Present (Addr) then
451 Expr := Expression (Addr);
453 -- If we have no initialization of any kind, then we don't
454 -- need to place any restrictions on the address clause, because
455 -- the object will be elaborated after the address clause is
456 -- evaluated. This happens if the declaration has no initial
457 -- expression, or the type has no implicit initialization, or
458 -- the object is imported.
460 -- The same holds for all initialized scalar types and all
461 -- access types. Packed bit arrays of size up to 64 are
462 -- represented using a modular type with an initialization
463 -- (to zero) and can be processed like other initialized
466 -- If the type is controlled, code to attach the object to a
467 -- finalization chain is generated at the point of declaration,
468 -- and therefore the elaboration of the object cannot be delayed:
469 -- the address expression must be a constant.
471 if (No (Expression (Decl))
472 and then not Controlled_Type (Typ)
474 (not Has_Non_Null_Base_Init_Proc (Typ)
475 or else Is_Imported (E)))
478 (Present (Expression (Decl))
479 and then Is_Scalar_Type (Typ))
485 (Is_Bit_Packed_Array (Typ)
487 Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
491 -- Otherwise, we require the address clause to be constant
492 -- because the call to the initialization procedure (or the
493 -- attach code) has to happen at the point of the declaration.
496 Check_Constant_Address_Clause (Expr, E);
497 Set_Has_Delayed_Freeze (E, False);
500 if not Error_Posted (Expr)
501 and then not Controlled_Type (Typ)
503 Warn_Overlay (Expr, Typ, Name (Addr));
506 end Check_Address_Clause;
508 -----------------------------
509 -- Check_Compile_Time_Size --
510 -----------------------------
512 procedure Check_Compile_Time_Size (T : Entity_Id) is
514 procedure Set_Small_Size (T : Entity_Id; S : Uint);
515 -- Sets the compile time known size (32 bits or less) in the Esize
516 -- field, of T checking for a size clause that was given which attempts
517 -- to give a smaller size.
519 function Size_Known (T : Entity_Id) return Boolean;
520 -- Recursive function that does all the work
522 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
523 -- If T is a constrained subtype, its size is not known if any of its
524 -- discriminant constraints is not static and it is not a null record.
525 -- The test is conservative and doesn't check that the components are
526 -- in fact constrained by non-static discriminant values. Could be made
533 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
538 elsif Has_Size_Clause (T) then
539 if RM_Size (T) < S then
540 Error_Msg_Uint_1 := S;
542 ("size for & is too small, minimum is ^",
545 elsif Unknown_Esize (T) then
549 -- Set sizes if not set already
552 if Unknown_Esize (T) then
556 if Unknown_RM_Size (T) then
566 function Size_Known (T : Entity_Id) return Boolean is
574 if Size_Known_At_Compile_Time (T) then
577 elsif Is_Scalar_Type (T)
578 or else Is_Task_Type (T)
580 return not Is_Generic_Type (T);
582 elsif Is_Array_Type (T) then
583 if Ekind (T) = E_String_Literal_Subtype then
584 Set_Small_Size (T, Component_Size (T)
585 * String_Literal_Length (T));
588 elsif not Is_Constrained (T) then
591 -- Don't do any recursion on type with error posted, since
592 -- we may have a malformed type that leads us into a loop
594 elsif Error_Posted (T) then
597 elsif not Size_Known (Component_Type (T)) then
601 -- Check for all indexes static, and also compute possible
602 -- size (in case it is less than 32 and may be packable).
605 Esiz : Uint := Component_Size (T);
609 Index := First_Index (T);
610 while Present (Index) loop
611 if Nkind (Index) = N_Range then
612 Get_Index_Bounds (Index, Low, High);
614 elsif Error_Posted (Scalar_Range (Etype (Index))) then
618 Low := Type_Low_Bound (Etype (Index));
619 High := Type_High_Bound (Etype (Index));
622 if not Compile_Time_Known_Value (Low)
623 or else not Compile_Time_Known_Value (High)
624 or else Etype (Index) = Any_Type
629 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
641 Set_Small_Size (T, Esiz);
645 elsif Is_Access_Type (T) then
648 elsif Is_Private_Type (T)
649 and then not Is_Generic_Type (T)
650 and then Present (Underlying_Type (T))
652 -- Don't do any recursion on type with error posted, since
653 -- we may have a malformed type that leads us into a loop
655 if Error_Posted (T) then
658 return Size_Known (Underlying_Type (T));
661 elsif Is_Record_Type (T) then
663 -- A class-wide type is never considered to have a known size
665 if Is_Class_Wide_Type (T) then
668 -- A subtype of a variant record must not have non-static
669 -- discriminanted components.
671 elsif T /= Base_Type (T)
672 and then not Static_Discriminated_Components (T)
676 -- Don't do any recursion on type with error posted, since
677 -- we may have a malformed type that leads us into a loop
679 elsif Error_Posted (T) then
683 -- Now look at the components of the record
686 -- The following two variables are used to keep track of
687 -- the size of packed records if we can tell the size of
688 -- the packed record in the front end. Packed_Size_Known
689 -- is True if so far we can figure out the size. It is
690 -- initialized to True for a packed record, unless the
691 -- record has discriminants. The reason we eliminate the
692 -- discriminated case is that we don't know the way the
693 -- back end lays out discriminated packed records. If
694 -- Packed_Size_Known is True, then Packed_Size is the
695 -- size in bits so far.
697 Packed_Size_Known : Boolean :=
699 and then not Has_Discriminants (T);
701 Packed_Size : Uint := Uint_0;
704 -- Test for variant part present
706 if Has_Discriminants (T)
707 and then Present (Parent (T))
708 and then Nkind (Parent (T)) = N_Full_Type_Declaration
709 and then Nkind (Type_Definition (Parent (T))) =
711 and then not Null_Present (Type_Definition (Parent (T)))
712 and then Present (Variant_Part
713 (Component_List (Type_Definition (Parent (T)))))
715 -- If variant part is present, and type is unconstrained,
716 -- then we must have defaulted discriminants, or a size
717 -- clause must be present for the type, or else the size
718 -- is definitely not known at compile time.
720 if not Is_Constrained (T)
722 No (Discriminant_Default_Value
723 (First_Discriminant (T)))
724 and then Unknown_Esize (T)
730 -- Loop through components
732 Comp := First_Entity (T);
733 while Present (Comp) loop
734 if Ekind (Comp) = E_Component
736 Ekind (Comp) = E_Discriminant
738 Ctyp := Etype (Comp);
740 -- We do not know the packed size if there is a
741 -- component clause present (we possibly could,
742 -- but this would only help in the case of a record
743 -- with partial rep clauses. That's because in the
744 -- case of full rep clauses, the size gets figured
745 -- out anyway by a different circuit).
747 if Present (Component_Clause (Comp)) then
748 Packed_Size_Known := False;
751 -- We need to identify a component that is an array
752 -- where the index type is an enumeration type with
753 -- non-standard representation, and some bound of the
754 -- type depends on a discriminant.
756 -- This is because gigi computes the size by doing a
757 -- substituation of the appropriate discriminant value
758 -- in the size expression for the base type, and gigi
759 -- is not clever enough to evaluate the resulting
760 -- expression (which involves a call to rep_to_pos)
763 -- It would be nice if gigi would either recognize that
764 -- this expression can be computed at compile time, or
765 -- alternatively figured out the size from the subtype
766 -- directly, where all the information is at hand ???
768 if Is_Array_Type (Etype (Comp))
769 and then Present (Packed_Array_Type (Etype (Comp)))
772 Ocomp : constant Entity_Id :=
773 Original_Record_Component (Comp);
774 OCtyp : constant Entity_Id := Etype (Ocomp);
780 Ind := First_Index (OCtyp);
781 while Present (Ind) loop
782 Indtyp := Etype (Ind);
784 if Is_Enumeration_Type (Indtyp)
785 and then Has_Non_Standard_Rep (Indtyp)
787 Lo := Type_Low_Bound (Indtyp);
788 Hi := Type_High_Bound (Indtyp);
790 if Is_Entity_Name (Lo)
792 Ekind (Entity (Lo)) = E_Discriminant
796 elsif Is_Entity_Name (Hi)
798 Ekind (Entity (Hi)) = E_Discriminant
809 -- Clearly size of record is not known if the size of
810 -- one of the components is not known.
812 if not Size_Known (Ctyp) then
816 -- Accumulate packed size if possible
818 if Packed_Size_Known then
820 -- We can only deal with elementary types, since for
821 -- non-elementary components, alignment enters into
822 -- the picture, and we don't know enough to handle
823 -- proper alignment in this context. Packed arrays
824 -- count as elementary if the representation is a
827 if Is_Elementary_Type (Ctyp)
828 or else (Is_Array_Type (Ctyp)
830 Present (Packed_Array_Type (Ctyp))
832 Is_Modular_Integer_Type
833 (Packed_Array_Type (Ctyp)))
835 -- If RM_Size is known and static, then we can
836 -- keep accumulating the packed size.
838 if Known_Static_RM_Size (Ctyp) then
840 -- A little glitch, to be removed sometime ???
841 -- gigi does not understand zero sizes yet.
843 if RM_Size (Ctyp) = Uint_0 then
844 Packed_Size_Known := False;
846 -- Normal case where we can keep accumulating
847 -- the packed array size.
850 Packed_Size := Packed_Size + RM_Size (Ctyp);
853 -- If we have a field whose RM_Size is not known
854 -- then we can't figure out the packed size here.
857 Packed_Size_Known := False;
860 -- If we have a non-elementary type we can't figure
861 -- out the packed array size (alignment issues).
864 Packed_Size_Known := False;
872 if Packed_Size_Known then
873 Set_Small_Size (T, Packed_Size);
884 -------------------------------------
885 -- Static_Discriminated_Components --
886 -------------------------------------
888 function Static_Discriminated_Components
889 (T : Entity_Id) return Boolean
891 Constraint : Elmt_Id;
894 if Has_Discriminants (T)
895 and then Present (Discriminant_Constraint (T))
896 and then Present (First_Component (T))
898 Constraint := First_Elmt (Discriminant_Constraint (T));
899 while Present (Constraint) loop
900 if not Compile_Time_Known_Value (Node (Constraint)) then
904 Next_Elmt (Constraint);
909 end Static_Discriminated_Components;
911 -- Start of processing for Check_Compile_Time_Size
914 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
915 end Check_Compile_Time_Size;
917 -----------------------------
918 -- Check_Debug_Info_Needed --
919 -----------------------------
921 procedure Check_Debug_Info_Needed (T : Entity_Id) is
923 if Needs_Debug_Info (T) or else Debug_Info_Off (T) then
926 elsif Comes_From_Source (T)
927 or else Debug_Generated_Code
928 or else Debug_Flag_VV
930 Set_Debug_Info_Needed (T);
932 end Check_Debug_Info_Needed;
934 ----------------------------
935 -- Check_Strict_Alignment --
936 ----------------------------
938 procedure Check_Strict_Alignment (E : Entity_Id) is
942 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
943 Set_Strict_Alignment (E);
945 elsif Is_Array_Type (E) then
946 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
948 elsif Is_Record_Type (E) then
949 if Is_Limited_Record (E) then
950 Set_Strict_Alignment (E);
954 Comp := First_Component (E);
956 while Present (Comp) loop
957 if not Is_Type (Comp)
958 and then (Strict_Alignment (Etype (Comp))
959 or else Is_Aliased (Comp))
961 Set_Strict_Alignment (E);
965 Next_Component (Comp);
968 end Check_Strict_Alignment;
970 -------------------------
971 -- Check_Unsigned_Type --
972 -------------------------
974 procedure Check_Unsigned_Type (E : Entity_Id) is
975 Ancestor : Entity_Id;
980 if not Is_Discrete_Or_Fixed_Point_Type (E) then
984 -- Do not attempt to analyze case where range was in error
986 if Error_Posted (Scalar_Range (E)) then
990 -- The situation that is non trivial is something like
992 -- subtype x1 is integer range -10 .. +10;
993 -- subtype x2 is x1 range 0 .. V1;
994 -- subtype x3 is x2 range V2 .. V3;
995 -- subtype x4 is x3 range V4 .. V5;
997 -- where Vn are variables. Here the base type is signed, but we still
998 -- know that x4 is unsigned because of the lower bound of x2.
1000 -- The only way to deal with this is to look up the ancestor chain
1004 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1008 Lo_Bound := Type_Low_Bound (Ancestor);
1010 if Compile_Time_Known_Value (Lo_Bound) then
1012 if Expr_Rep_Value (Lo_Bound) >= 0 then
1013 Set_Is_Unsigned_Type (E, True);
1019 Ancestor := Ancestor_Subtype (Ancestor);
1021 -- If no ancestor had a static lower bound, go to base type
1023 if No (Ancestor) then
1025 -- Note: the reason we still check for a compile time known
1026 -- value for the base type is that at least in the case of
1027 -- generic formals, we can have bounds that fail this test,
1028 -- and there may be other cases in error situations.
1030 Btyp := Base_Type (E);
1032 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1036 Lo_Bound := Type_Low_Bound (Base_Type (E));
1038 if Compile_Time_Known_Value (Lo_Bound)
1039 and then Expr_Rep_Value (Lo_Bound) >= 0
1041 Set_Is_Unsigned_Type (E, True);
1048 end Check_Unsigned_Type;
1050 -----------------------------
1051 -- Expand_Atomic_Aggregate --
1052 -----------------------------
1054 procedure Expand_Atomic_Aggregate (E : Entity_Id; Typ : Entity_Id) is
1055 Loc : constant Source_Ptr := Sloc (E);
1060 if (Nkind (Parent (E)) = N_Object_Declaration
1061 or else Nkind (Parent (E)) = N_Assignment_Statement)
1062 and then Comes_From_Source (Parent (E))
1063 and then Nkind (E) = N_Aggregate
1066 Make_Defining_Identifier (Loc,
1067 New_Internal_Name ('T'));
1070 Make_Object_Declaration (Loc,
1071 Defining_Identifier => Temp,
1072 Object_definition => New_Occurrence_Of (Typ, Loc),
1073 Expression => Relocate_Node (E));
1074 Insert_Before (Parent (E), New_N);
1077 Set_Expression (Parent (E), New_Occurrence_Of (Temp, Loc));
1079 -- To prevent the temporary from being constant-folded (which
1080 -- would lead to the same piecemeal assignment on the original
1081 -- target) indicate to the back-end that the temporary is a
1082 -- variable with real storage. See description of this flag
1083 -- in Einfo, and the notes on N_Assignment_Statement and
1084 -- N_Object_Declaration in Sinfo.
1086 Set_Is_True_Constant (Temp, False);
1088 end Expand_Atomic_Aggregate;
1094 -- Note: the easy coding for this procedure would be to just build a
1095 -- single list of freeze nodes and then insert them and analyze them
1096 -- all at once. This won't work, because the analysis of earlier freeze
1097 -- nodes may recursively freeze types which would otherwise appear later
1098 -- on in the freeze list. So we must analyze and expand the freeze nodes
1099 -- as they are generated.
1101 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1102 Loc : constant Source_Ptr := Sloc (After);
1106 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1107 -- This is the internal recursive routine that does freezing of
1108 -- entities (but NOT the analysis of default expressions, which
1109 -- should not be recursive, we don't want to analyze those till
1110 -- we are sure that ALL the types are frozen).
1112 --------------------
1113 -- Freeze_All_Ent --
1114 --------------------
1116 procedure Freeze_All_Ent
1118 After : in out Node_Id)
1124 procedure Process_Flist;
1125 -- If freeze nodes are present, insert and analyze, and reset
1126 -- cursor for next insertion.
1132 procedure Process_Flist is
1134 if Is_Non_Empty_List (Flist) then
1135 Lastn := Next (After);
1136 Insert_List_After_And_Analyze (After, Flist);
1138 if Present (Lastn) then
1139 After := Prev (Lastn);
1141 After := Last (List_Containing (After));
1146 -- Start or processing for Freeze_All_Ent
1150 while Present (E) loop
1152 -- If the entity is an inner package which is not a package
1153 -- renaming, then its entities must be frozen at this point.
1154 -- Note that such entities do NOT get frozen at the end of
1155 -- the nested package itself (only library packages freeze).
1157 -- Same is true for task declarations, where anonymous records
1158 -- created for entry parameters must be frozen.
1160 if Ekind (E) = E_Package
1161 and then No (Renamed_Object (E))
1162 and then not Is_Child_Unit (E)
1163 and then not Is_Frozen (E)
1166 Install_Visible_Declarations (E);
1167 Install_Private_Declarations (E);
1169 Freeze_All (First_Entity (E), After);
1171 End_Package_Scope (E);
1173 elsif Ekind (E) in Task_Kind
1175 (Nkind (Parent (E)) = N_Task_Type_Declaration
1177 Nkind (Parent (E)) = N_Single_Task_Declaration)
1180 Freeze_All (First_Entity (E), After);
1183 -- For a derived tagged type, we must ensure that all the
1184 -- primitive operations of the parent have been frozen, so
1185 -- that their addresses will be in the parent's dispatch table
1186 -- at the point it is inherited.
1188 elsif Ekind (E) = E_Record_Type
1189 and then Is_Tagged_Type (E)
1190 and then Is_Tagged_Type (Etype (E))
1191 and then Is_Derived_Type (E)
1194 Prim_List : constant Elist_Id :=
1195 Primitive_Operations (Etype (E));
1201 Prim := First_Elmt (Prim_List);
1203 while Present (Prim) loop
1204 Subp := Node (Prim);
1206 if Comes_From_Source (Subp)
1207 and then not Is_Frozen (Subp)
1209 Flist := Freeze_Entity (Subp, Loc);
1218 if not Is_Frozen (E) then
1219 Flist := Freeze_Entity (E, Loc);
1223 -- If an incomplete type is still not frozen, this may be
1224 -- a premature freezing because of a body declaration that
1225 -- follows. Indicate where the freezing took place.
1227 -- If the freezing is caused by the end of the current
1228 -- declarative part, it is a Taft Amendment type, and there
1231 if not Is_Frozen (E)
1232 and then Ekind (E) = E_Incomplete_Type
1235 Bod : constant Node_Id := Next (After);
1238 if (Nkind (Bod) = N_Subprogram_Body
1239 or else Nkind (Bod) = N_Entry_Body
1240 or else Nkind (Bod) = N_Package_Body
1241 or else Nkind (Bod) = N_Protected_Body
1242 or else Nkind (Bod) = N_Task_Body
1243 or else Nkind (Bod) in N_Body_Stub)
1245 List_Containing (After) = List_Containing (Parent (E))
1247 Error_Msg_Sloc := Sloc (Next (After));
1249 ("type& is frozen# before its full declaration",
1259 -- Start of processing for Freeze_All
1262 Freeze_All_Ent (From, After);
1264 -- Now that all types are frozen, we can deal with default expressions
1265 -- that require us to build a default expression functions. This is the
1266 -- point at which such functions are constructed (after all types that
1267 -- might be used in such expressions have been frozen).
1269 -- We also add finalization chains to access types whose designated
1270 -- types are controlled. This is normally done when freezing the type,
1271 -- but this misses recursive type definitions where the later members
1272 -- of the recursion introduce controlled components (e.g. 5624-001).
1274 -- Loop through entities
1277 while Present (E) loop
1278 if Is_Subprogram (E) then
1280 if not Default_Expressions_Processed (E) then
1281 Process_Default_Expressions (E, After);
1284 if not Has_Completion (E) then
1285 Decl := Unit_Declaration_Node (E);
1287 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1288 Build_And_Analyze_Renamed_Body (Decl, E, After);
1290 elsif Nkind (Decl) = N_Subprogram_Declaration
1291 and then Present (Corresponding_Body (Decl))
1293 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1294 = N_Subprogram_Renaming_Declaration
1296 Build_And_Analyze_Renamed_Body
1297 (Decl, Corresponding_Body (Decl), After);
1301 elsif Ekind (E) in Task_Kind
1303 (Nkind (Parent (E)) = N_Task_Type_Declaration
1305 Nkind (Parent (E)) = N_Single_Task_Declaration)
1310 Ent := First_Entity (E);
1312 while Present (Ent) loop
1315 and then not Default_Expressions_Processed (Ent)
1317 Process_Default_Expressions (Ent, After);
1324 elsif Is_Access_Type (E)
1325 and then Comes_From_Source (E)
1326 and then Ekind (Directly_Designated_Type (E)) = E_Incomplete_Type
1327 and then Controlled_Type (Designated_Type (E))
1328 and then No (Associated_Final_Chain (E))
1330 Build_Final_List (Parent (E), E);
1337 -----------------------
1338 -- Freeze_And_Append --
1339 -----------------------
1341 procedure Freeze_And_Append
1344 Result : in out List_Id)
1346 L : constant List_Id := Freeze_Entity (Ent, Loc);
1348 if Is_Non_Empty_List (L) then
1349 if Result = No_List then
1352 Append_List (L, Result);
1355 end Freeze_And_Append;
1361 procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
1362 Freeze_Nodes : constant List_Id := Freeze_Entity (T, Sloc (N));
1364 if Is_Non_Empty_List (Freeze_Nodes) then
1365 Insert_Actions (N, Freeze_Nodes);
1373 function Freeze_Entity (E : Entity_Id; Loc : Source_Ptr) return List_Id is
1374 Test_E : Entity_Id := E;
1382 procedure Check_Current_Instance (Comp_Decl : Node_Id);
1383 -- Check that an Access or Unchecked_Access attribute with a prefix
1384 -- which is the current instance type can only be applied when the type
1387 function After_Last_Declaration return Boolean;
1388 -- If Loc is a freeze_entity that appears after the last declaration
1389 -- in the scope, inhibit error messages on late completion.
1391 procedure Freeze_Record_Type (Rec : Entity_Id);
1392 -- Freeze each component, handle some representation clauses, and freeze
1393 -- primitive operations if this is a tagged type.
1395 ----------------------------
1396 -- After_Last_Declaration --
1397 ----------------------------
1399 function After_Last_Declaration return Boolean is
1400 Spec : constant Node_Id := Parent (Current_Scope);
1403 if Nkind (Spec) = N_Package_Specification then
1404 if Present (Private_Declarations (Spec)) then
1405 return Loc >= Sloc (Last (Private_Declarations (Spec)));
1407 elsif Present (Visible_Declarations (Spec)) then
1408 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
1416 end After_Last_Declaration;
1418 ----------------------------
1419 -- Check_Current_Instance --
1420 ----------------------------
1422 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
1424 function Process (N : Node_Id) return Traverse_Result;
1425 -- Process routine to apply check to given node
1431 function Process (N : Node_Id) return Traverse_Result is
1434 when N_Attribute_Reference =>
1435 if (Attribute_Name (N) = Name_Access
1437 Attribute_Name (N) = Name_Unchecked_Access)
1438 and then Is_Entity_Name (Prefix (N))
1439 and then Is_Type (Entity (Prefix (N)))
1440 and then Entity (Prefix (N)) = E
1443 ("current instance must be a limited type", Prefix (N));
1449 when others => return OK;
1453 procedure Traverse is new Traverse_Proc (Process);
1455 -- Start of processing for Check_Current_Instance
1458 Traverse (Comp_Decl);
1459 end Check_Current_Instance;
1461 ------------------------
1462 -- Freeze_Record_Type --
1463 ------------------------
1465 procedure Freeze_Record_Type (Rec : Entity_Id) is
1472 Unplaced_Component : Boolean := False;
1473 -- Set True if we find at least one component with no component
1474 -- clause (used to warn about useless Pack pragmas).
1476 Placed_Component : Boolean := False;
1477 -- Set True if we find at least one component with a component
1478 -- clause (used to warn about useless Bit_Order pragmas).
1480 procedure Check_Itype (Desig : Entity_Id);
1481 -- If the component subtype is an access to a constrained subtype
1482 -- of an already frozen type, make the subtype frozen as well. It
1483 -- might otherwise be frozen in the wrong scope, and a freeze node
1484 -- on subtype has no effect.
1490 procedure Check_Itype (Desig : Entity_Id) is
1492 if not Is_Frozen (Desig)
1493 and then Is_Frozen (Base_Type (Desig))
1495 Set_Is_Frozen (Desig);
1497 -- In addition, add an Itype_Reference to ensure that the
1498 -- access subtype is elaborated early enough. This cannot
1499 -- be done if the subtype may depend on discriminants.
1501 if Ekind (Comp) = E_Component
1502 and then Is_Itype (Etype (Comp))
1503 and then not Has_Discriminants (Rec)
1505 IR := Make_Itype_Reference (Sloc (Comp));
1506 Set_Itype (IR, Desig);
1509 Result := New_List (IR);
1511 Append (IR, Result);
1517 -- Start of processing for Freeze_Record_Type
1520 -- If this is a subtype of a controlled type, declared without
1521 -- a constraint, the _controller may not appear in the component
1522 -- list if the parent was not frozen at the point of subtype
1523 -- declaration. Inherit the _controller component now.
1525 if Rec /= Base_Type (Rec)
1526 and then Has_Controlled_Component (Rec)
1528 if Nkind (Parent (Rec)) = N_Subtype_Declaration
1529 and then Is_Entity_Name (Subtype_Indication (Parent (Rec)))
1531 Set_First_Entity (Rec, First_Entity (Base_Type (Rec)));
1533 -- If this is an internal type without a declaration, as for
1534 -- record component, the base type may not yet be frozen, and its
1535 -- controller has not been created. Add an explicit freeze node
1536 -- for the itype, so it will be frozen after the base type. This
1537 -- freeze node is used to communicate with the expander, in order
1538 -- to create the controller for the enclosing record, and it is
1539 -- deleted afterwards (see exp_ch3). It must not be created when
1540 -- expansion is off, because it might appear in the wrong context
1541 -- for the back end.
1543 elsif Is_Itype (Rec)
1544 and then Has_Delayed_Freeze (Base_Type (Rec))
1546 Nkind (Associated_Node_For_Itype (Rec)) =
1547 N_Component_Declaration
1548 and then Expander_Active
1550 Ensure_Freeze_Node (Rec);
1554 -- Freeze components and embedded subtypes
1556 Comp := First_Entity (Rec);
1559 while Present (Comp) loop
1561 -- First handle the (real) component case
1563 if Ekind (Comp) = E_Component
1564 or else Ekind (Comp) = E_Discriminant
1567 CC : constant Node_Id := Component_Clause (Comp);
1570 -- Freezing a record type freezes the type of each of its
1571 -- components. However, if the type of the component is
1572 -- part of this record, we do not want or need a separate
1573 -- Freeze_Node. Note that Is_Itype is wrong because that's
1574 -- also set in private type cases. We also can't check for
1575 -- the Scope being exactly Rec because of private types and
1576 -- record extensions.
1578 if Is_Itype (Etype (Comp))
1579 and then Is_Record_Type (Underlying_Type
1580 (Scope (Etype (Comp))))
1582 Undelay_Type (Etype (Comp));
1585 Freeze_And_Append (Etype (Comp), Loc, Result);
1587 -- Check for error of component clause given for variable
1588 -- sized type. We have to delay this test till this point,
1589 -- since the component type has to be frozen for us to know
1590 -- if it is variable length. We omit this test in a generic
1591 -- context, it will be applied at instantiation time.
1593 if Present (CC) then
1594 Placed_Component := True;
1596 if Inside_A_Generic then
1599 elsif not Size_Known_At_Compile_Time
1600 (Underlying_Type (Etype (Comp)))
1603 ("component clause not allowed for variable " &
1604 "length component", CC);
1608 Unplaced_Component := True;
1611 -- Case of component requires byte alignment
1613 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
1615 -- Set the enclosing record to also require byte align
1617 Set_Must_Be_On_Byte_Boundary (Rec);
1619 -- Check for component clause that is inconsistent
1620 -- with the required byte boundary alignment.
1623 and then Normalized_First_Bit (Comp) mod
1624 System_Storage_Unit /= 0
1627 ("component & must be byte aligned",
1628 Component_Name (Component_Clause (Comp)));
1632 -- If component clause is present, then deal with the
1633 -- non-default bit order case. We cannot do this before
1634 -- the freeze point, because there is no required order
1635 -- for the component clause and the bit_order clause.
1637 -- We only do this processing for the base type, and in
1638 -- fact that's important, since otherwise if there are
1639 -- record subtypes, we could reverse the bits once for
1640 -- each subtype, which would be incorrect.
1643 and then Reverse_Bit_Order (Rec)
1644 and then Ekind (E) = E_Record_Type
1647 CFB : constant Uint := Component_Bit_Offset (Comp);
1648 CSZ : constant Uint := Esize (Comp);
1649 CLC : constant Node_Id := Component_Clause (Comp);
1650 Pos : constant Node_Id := Position (CLC);
1651 FB : constant Node_Id := First_Bit (CLC);
1653 Storage_Unit_Offset : constant Uint :=
1654 CFB / System_Storage_Unit;
1656 Start_Bit : constant Uint :=
1657 CFB mod System_Storage_Unit;
1660 -- Cases where field goes over storage unit boundary
1662 if Start_Bit + CSZ > System_Storage_Unit then
1664 -- Allow multi-byte field but generate warning
1666 if Start_Bit mod System_Storage_Unit = 0
1667 and then CSZ mod System_Storage_Unit = 0
1670 ("multi-byte field specified with non-standard"
1671 & " Bit_Order?", CLC);
1673 if Bytes_Big_Endian then
1675 ("bytes are not reversed "
1676 & "(component is big-endian)?", CLC);
1679 ("bytes are not reversed "
1680 & "(component is little-endian)?", CLC);
1683 -- Do not allow non-contiguous field
1687 ("attempt to specify non-contiguous field"
1688 & " not permitted", CLC);
1690 ("\(caused by non-standard Bit_Order "
1691 & "specified)", CLC);
1694 -- Case where field fits in one storage unit
1697 -- Give warning if suspicious component clause
1699 if Intval (FB) >= System_Storage_Unit then
1701 ("?Bit_Order clause does not affect " &
1702 "byte ordering", Pos);
1704 Intval (Pos) + Intval (FB) /
1705 System_Storage_Unit;
1707 ("?position normalized to ^ before bit " &
1708 "order interpreted", Pos);
1711 -- Here is where we fix up the Component_Bit_Offset
1712 -- value to account for the reverse bit order.
1713 -- Some examples of what needs to be done are:
1715 -- First_Bit .. Last_Bit Component_Bit_Offset
1718 -- 0 .. 0 7 .. 7 0 7
1719 -- 0 .. 1 6 .. 7 0 6
1720 -- 0 .. 2 5 .. 7 0 5
1721 -- 0 .. 7 0 .. 7 0 4
1723 -- 1 .. 1 6 .. 6 1 6
1724 -- 1 .. 4 3 .. 6 1 3
1725 -- 4 .. 7 0 .. 3 4 0
1727 -- The general rule is that the first bit is
1728 -- is obtained by subtracting the old ending bit
1729 -- from storage_unit - 1.
1731 Set_Component_Bit_Offset
1733 (Storage_Unit_Offset * System_Storage_Unit) +
1734 (System_Storage_Unit - 1) -
1735 (Start_Bit + CSZ - 1));
1737 Set_Normalized_First_Bit
1739 Component_Bit_Offset (Comp) mod
1740 System_Storage_Unit);
1747 -- If the component is an Itype with Delayed_Freeze and is either
1748 -- a record or array subtype and its base type has not yet been
1749 -- frozen, we must remove this from the entity list of this
1750 -- record and put it on the entity list of the scope of its base
1751 -- type. Note that we know that this is not the type of a
1752 -- component since we cleared Has_Delayed_Freeze for it in the
1753 -- previous loop. Thus this must be the Designated_Type of an
1754 -- access type, which is the type of a component.
1757 and then Is_Type (Scope (Comp))
1758 and then Is_Composite_Type (Comp)
1759 and then Base_Type (Comp) /= Comp
1760 and then Has_Delayed_Freeze (Comp)
1761 and then not Is_Frozen (Base_Type (Comp))
1764 Will_Be_Frozen : Boolean := False;
1765 S : Entity_Id := Scope (Rec);
1768 -- We have a pretty bad kludge here. Suppose Rec is a
1769 -- subtype being defined in a subprogram that's created
1770 -- as part of the freezing of Rec'Base. In that case,
1771 -- we know that Comp'Base must have already been frozen by
1772 -- the time we get to elaborate this because Gigi doesn't
1773 -- elaborate any bodies until it has elaborated all of the
1774 -- declarative part. But Is_Frozen will not be set at this
1775 -- point because we are processing code in lexical order.
1777 -- We detect this case by going up the Scope chain of
1778 -- Rec and seeing if we have a subprogram scope before
1779 -- reaching the top of the scope chain or that of Comp'Base.
1780 -- If we do, then mark that Comp'Base will actually be
1781 -- frozen. If so, we merely undelay it.
1783 while Present (S) loop
1784 if Is_Subprogram (S) then
1785 Will_Be_Frozen := True;
1787 elsif S = Scope (Base_Type (Comp)) then
1794 if Will_Be_Frozen then
1795 Undelay_Type (Comp);
1797 if Present (Prev) then
1798 Set_Next_Entity (Prev, Next_Entity (Comp));
1800 Set_First_Entity (Rec, Next_Entity (Comp));
1803 -- Insert in entity list of scope of base type (which
1804 -- must be an enclosing scope, because still unfrozen).
1806 Append_Entity (Comp, Scope (Base_Type (Comp)));
1810 -- If the component is an access type with an allocator as
1811 -- default value, the designated type will be frozen by the
1812 -- corresponding expression in init_proc. In order to place the
1813 -- freeze node for the designated type before that for the
1814 -- current record type, freeze it now.
1816 -- Same process if the component is an array of access types,
1817 -- initialized with an aggregate. If the designated type is
1818 -- private, it cannot contain allocators, and it is premature to
1819 -- freeze the type, so we check for this as well.
1821 elsif Is_Access_Type (Etype (Comp))
1822 and then Present (Parent (Comp))
1823 and then Present (Expression (Parent (Comp)))
1824 and then Nkind (Expression (Parent (Comp))) = N_Allocator
1827 Alloc : constant Node_Id := Expression (Parent (Comp));
1830 -- If component is pointer to a classwide type, freeze
1831 -- the specific type in the expression being allocated.
1832 -- The expression may be a subtype indication, in which
1833 -- case freeze the subtype mark.
1835 if Is_Class_Wide_Type (Designated_Type (Etype (Comp))) then
1836 if Is_Entity_Name (Expression (Alloc)) then
1838 (Entity (Expression (Alloc)), Loc, Result);
1840 Nkind (Expression (Alloc)) = N_Subtype_Indication
1843 (Entity (Subtype_Mark (Expression (Alloc))),
1847 elsif Is_Itype (Designated_Type (Etype (Comp))) then
1848 Check_Itype (Designated_Type (Etype (Comp)));
1852 (Designated_Type (Etype (Comp)), Loc, Result);
1856 elsif Is_Access_Type (Etype (Comp))
1857 and then Is_Itype (Designated_Type (Etype (Comp)))
1859 Check_Itype (Designated_Type (Etype (Comp)));
1861 elsif Is_Array_Type (Etype (Comp))
1862 and then Is_Access_Type (Component_Type (Etype (Comp)))
1863 and then Present (Parent (Comp))
1864 and then Nkind (Parent (Comp)) = N_Component_Declaration
1865 and then Present (Expression (Parent (Comp)))
1866 and then Nkind (Expression (Parent (Comp))) = N_Aggregate
1867 and then Is_Fully_Defined
1868 (Designated_Type (Component_Type (Etype (Comp))))
1872 (Component_Type (Etype (Comp))), Loc, Result);
1879 -- Check for useless pragma Bit_Order
1881 if not Placed_Component and then Reverse_Bit_Order (Rec) then
1882 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
1883 Error_Msg_N ("?Bit_Order specification has no effect", ADC);
1884 Error_Msg_N ("\?since no component clauses were specified", ADC);
1887 -- Check for useless pragma Pack when all components placed
1890 and then not Unplaced_Component
1891 and then Warn_On_Redundant_Constructs
1894 ("?pragma Pack has no effect, no unplaced components",
1895 Get_Rep_Pragma (Rec, Name_Pack));
1896 Set_Is_Packed (Rec, False);
1899 -- If this is the record corresponding to a remote type,
1900 -- freeze the remote type here since that is what we are
1901 -- semantically freezing. This prevents having the freeze
1902 -- node for that type in an inner scope.
1904 -- Also, Check for controlled components and unchecked unions.
1905 -- Finally, enforce the restriction that access attributes with
1906 -- a current instance prefix can only apply to limited types.
1908 if Ekind (Rec) = E_Record_Type then
1909 if Present (Corresponding_Remote_Type (Rec)) then
1911 (Corresponding_Remote_Type (Rec), Loc, Result);
1914 Comp := First_Component (Rec);
1915 while Present (Comp) loop
1916 if Has_Controlled_Component (Etype (Comp))
1917 or else (Chars (Comp) /= Name_uParent
1918 and then Is_Controlled (Etype (Comp)))
1919 or else (Is_Protected_Type (Etype (Comp))
1921 (Corresponding_Record_Type (Etype (Comp)))
1922 and then Has_Controlled_Component
1923 (Corresponding_Record_Type (Etype (Comp))))
1925 Set_Has_Controlled_Component (Rec);
1929 if Has_Unchecked_Union (Etype (Comp)) then
1930 Set_Has_Unchecked_Union (Rec);
1933 if Has_Per_Object_Constraint (Comp)
1934 and then not Is_Limited_Type (Rec)
1936 -- Scan component declaration for likely misuses of
1937 -- current instance, either in a constraint or in a
1938 -- default expression.
1940 Check_Current_Instance (Parent (Comp));
1943 Next_Component (Comp);
1947 Set_Component_Alignment_If_Not_Set (Rec);
1949 -- For first subtypes, check if there are any fixed-point
1950 -- fields with component clauses, where we must check the size.
1951 -- This is not done till the freeze point, since for fixed-point
1952 -- types, we do not know the size until the type is frozen.
1953 -- Similar processing applies to bit packed arrays.
1955 if Is_First_Subtype (Rec) then
1956 Comp := First_Component (Rec);
1958 while Present (Comp) loop
1959 if Present (Component_Clause (Comp))
1960 and then (Is_Fixed_Point_Type (Etype (Comp))
1962 Is_Bit_Packed_Array (Etype (Comp)))
1965 (Component_Name (Component_Clause (Comp)),
1971 Next_Component (Comp);
1974 end Freeze_Record_Type;
1976 -- Start of processing for Freeze_Entity
1979 -- We are going to test for various reasons why this entity need not be
1980 -- frozen here, but in the case of an Itype that's defined within a
1981 -- record, that test actually applies to the record.
1983 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
1984 Test_E := Scope (E);
1985 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
1986 and then Is_Record_Type (Underlying_Type (Scope (E)))
1988 Test_E := Underlying_Type (Scope (E));
1991 -- Do not freeze if already frozen since we only need one freeze node
1993 if Is_Frozen (E) then
1996 -- It is improper to freeze an external entity within a generic because
1997 -- its freeze node will appear in a non-valid context. The entity will
1998 -- be frozen in the proper scope after the current generic is analyzed.
2000 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
2003 -- Do not freeze a global entity within an inner scope created during
2004 -- expansion. A call to subprogram E within some internal procedure
2005 -- (a stream attribute for example) might require freezing E, but the
2006 -- freeze node must appear in the same declarative part as E itself.
2007 -- The two-pass elaboration mechanism in gigi guarantees that E will
2008 -- be frozen before the inner call is elaborated. We exclude constants
2009 -- from this test, because deferred constants may be frozen early, and
2010 -- must be diagnosed (see e.g. 1522-005). If the enclosing subprogram
2011 -- comes from source, or is a generic instance, then the freeze point
2012 -- is the one mandated by the language. and we freze the entity.
2014 elsif In_Open_Scopes (Scope (Test_E))
2015 and then Scope (Test_E) /= Current_Scope
2016 and then Ekind (Test_E) /= E_Constant
2019 S : Entity_Id := Current_Scope;
2022 while Present (S) loop
2023 if Is_Overloadable (S) then
2024 if Comes_From_Source (S)
2025 or else Is_Generic_Instance (S)
2037 -- Similarly, an inlined instance body may make reference to global
2038 -- entities, but these references cannot be the proper freezing point
2039 -- for them, and the the absence of inlining freezing will take place
2040 -- in their own scope. Normally instance bodies are analyzed after
2041 -- the enclosing compilation, and everything has been frozen at the
2042 -- proper place, but with front-end inlining an instance body is
2043 -- compiled before the end of the enclosing scope, and as a result
2044 -- out-of-order freezing must be prevented.
2046 elsif Front_End_Inlining
2047 and then In_Instance_Body
2048 and then Present (Scope (Test_E))
2051 S : Entity_Id := Scope (Test_E);
2054 while Present (S) loop
2055 if Is_Generic_Instance (S) then
2068 -- Here to freeze the entity
2073 -- Case of entity being frozen is other than a type
2075 if not Is_Type (E) then
2077 -- If entity is exported or imported and does not have an external
2078 -- name, now is the time to provide the appropriate default name.
2079 -- Skip this if the entity is stubbed, since we don't need a name
2080 -- for any stubbed routine.
2082 if (Is_Imported (E) or else Is_Exported (E))
2083 and then No (Interface_Name (E))
2084 and then Convention (E) /= Convention_Stubbed
2086 Set_Encoded_Interface_Name
2087 (E, Get_Default_External_Name (E));
2089 -- Special processing for atomic objects appearing in object decls
2092 and then Nkind (Parent (E)) = N_Object_Declaration
2093 and then Present (Expression (Parent (E)))
2096 Expr : constant Node_Id := Expression (Parent (E));
2099 -- If expression is an aggregate, assign to a temporary to
2100 -- ensure that the actual assignment is done atomically rather
2101 -- than component-wise (the assignment to the temp may be done
2102 -- component-wise, but that is harmless.
2104 if Nkind (Expr) = N_Aggregate then
2105 Expand_Atomic_Aggregate (Expr, Etype (E));
2107 -- If the expression is a reference to a record or array
2108 -- object entity, then reset Is_True_Constant to False so
2109 -- that the compiler will not optimize away the intermediate
2110 -- object, which we need in this case for the same reason
2111 -- (to ensure that the actual assignment is atomic, rather
2112 -- than component-wise).
2114 elsif Is_Entity_Name (Expr)
2115 and then (Is_Record_Type (Etype (Expr))
2117 Is_Array_Type (Etype (Expr)))
2119 Set_Is_True_Constant (Entity (Expr), False);
2124 -- For a subprogram, freeze all parameter types and also the return
2125 -- type (RM 13.14(14)). However skip this for internal subprograms.
2126 -- This is also the point where any extra formal parameters are
2127 -- created since we now know whether the subprogram will use
2128 -- a foreign convention.
2130 if Is_Subprogram (E) then
2131 if not Is_Internal (E) then
2134 Warn_Node : Node_Id;
2136 function Is_Fat_C_Ptr_Type (T : Entity_Id) return Boolean;
2137 -- Determines if given type entity is a fat pointer type
2138 -- used as an argument type or return type to a subprogram
2139 -- with C or C++ convention set.
2141 --------------------------
2142 -- Is_Fat_C_Access_Type --
2143 --------------------------
2145 function Is_Fat_C_Ptr_Type (T : Entity_Id) return Boolean is
2147 return (Convention (E) = Convention_C
2149 Convention (E) = Convention_CPP)
2150 and then Is_Access_Type (T)
2151 and then Esize (T) > Ttypes.System_Address_Size;
2152 end Is_Fat_C_Ptr_Type;
2155 -- Loop through formals
2157 Formal := First_Formal (E);
2158 while Present (Formal) loop
2159 F_Type := Etype (Formal);
2160 Freeze_And_Append (F_Type, Loc, Result);
2162 if Is_Private_Type (F_Type)
2163 and then Is_Private_Type (Base_Type (F_Type))
2164 and then No (Full_View (Base_Type (F_Type)))
2165 and then not Is_Generic_Type (F_Type)
2166 and then not Is_Derived_Type (F_Type)
2168 -- If the type of a formal is incomplete, subprogram
2169 -- is being frozen prematurely. Within an instance
2170 -- (but not within a wrapper package) this is an
2171 -- an artifact of our need to regard the end of an
2172 -- instantiation as a freeze point. Otherwise it is
2173 -- a definite error.
2175 -- and then not Is_Wrapper_Package (Current_Scope) ???
2178 Set_Is_Frozen (E, False);
2181 elsif not After_Last_Declaration then
2182 Error_Msg_Node_1 := F_Type;
2184 ("type& must be fully defined before this point",
2189 -- Check bad use of fat C pointer
2191 if Warn_On_Export_Import and then
2192 Is_Fat_C_Ptr_Type (F_Type)
2194 Error_Msg_Qual_Level := 1;
2196 ("?type of & does not correspond to C pointer",
2198 Error_Msg_Qual_Level := 0;
2201 -- Check for unconstrained array in exported foreign
2204 if Convention (E) in Foreign_Convention
2205 and then not Is_Imported (E)
2206 and then Is_Array_Type (F_Type)
2207 and then not Is_Constrained (F_Type)
2208 and then Warn_On_Export_Import
2210 Error_Msg_Qual_Level := 1;
2212 -- If this is an inherited operation, place the
2213 -- warning on the derived type declaration, rather
2214 -- than on the original subprogram.
2216 if Nkind (Original_Node (Parent (E))) =
2217 N_Full_Type_Declaration
2219 Warn_Node := Parent (E);
2221 if Formal = First_Formal (E) then
2223 ("?in inherited operation&!", Warn_Node, E);
2226 Warn_Node := Formal;
2230 ("?type of argument& is unconstrained array",
2233 ("?foreign caller must pass bounds explicitly",
2235 Error_Msg_Qual_Level := 0;
2238 Next_Formal (Formal);
2241 -- Check return type
2243 if Ekind (E) = E_Function then
2244 Freeze_And_Append (Etype (E), Loc, Result);
2246 if Warn_On_Export_Import
2247 and then Is_Fat_C_Ptr_Type (Etype (E))
2250 ("?return type of& does not correspond to C pointer",
2253 elsif Is_Array_Type (Etype (E))
2254 and then not Is_Constrained (Etype (E))
2255 and then not Is_Imported (E)
2256 and then Convention (E) in Foreign_Convention
2257 and then Warn_On_Export_Import
2260 ("?foreign convention function& should not " &
2261 "return unconstrained array", E);
2267 -- Must freeze its parent first if it is a derived subprogram
2269 if Present (Alias (E)) then
2270 Freeze_And_Append (Alias (E), Loc, Result);
2273 -- If the return type requires a transient scope, and we are on
2274 -- a target allowing functions to return with a depressed stack
2275 -- pointer, then we mark the function as requiring this treatment.
2277 if Ekind (E) = E_Function
2278 and then Functions_Return_By_DSP_On_Target
2279 and then Requires_Transient_Scope (Etype (E))
2281 Set_Function_Returns_With_DSP (E);
2284 if not Is_Internal (E) then
2285 Freeze_Subprogram (E);
2288 -- Here for other than a subprogram or type
2291 -- If entity has a type, and it is not a generic unit, then
2292 -- freeze it first (RM 13.14(10))
2294 if Present (Etype (E))
2295 and then Ekind (E) /= E_Generic_Function
2297 Freeze_And_Append (Etype (E), Loc, Result);
2300 -- Special processing for objects created by object declaration
2302 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
2304 -- For object created by object declaration, perform required
2305 -- categorization (preelaborate and pure) checks. Defer these
2306 -- checks to freeze time since pragma Import inhibits default
2307 -- initialization and thus pragma Import affects these checks.
2309 Validate_Object_Declaration (Declaration_Node (E));
2311 -- If there is an address clause, check it is valid
2313 Check_Address_Clause (E);
2315 -- For imported objects, set Is_Public unless there is also
2316 -- an address clause, which means that there is no external
2317 -- symbol needed for the Import (Is_Public may still be set
2318 -- for other unrelated reasons). Note that we delayed this
2319 -- processing till freeze time so that we can be sure not
2320 -- to set the flag if there is an address clause. If there
2321 -- is such a clause, then the only purpose of the import
2322 -- pragma is to suppress implicit initialization.
2325 and then not Present (Address_Clause (E))
2331 -- Check that a constant which has a pragma Volatile[_Components]
2332 -- or Atomic[_Components] also has a pragma Import (RM C.6(13))
2334 -- Note: Atomic[_Components] also sets Volatile[_Components]
2336 if Ekind (E) = E_Constant
2337 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
2338 and then not Is_Imported (E)
2340 -- Make sure we actually have a pragma, and have not merely
2341 -- inherited the indication from elsewhere (e.g. an address
2342 -- clause, which is not good enough in RM terms!)
2344 if Has_Rep_Pragma (E, Name_Atomic)
2346 Has_Rep_Pragma (E, Name_Atomic_Components)
2349 ("stand alone atomic constant must be " &
2350 "imported ('R'M 'C.6(13))", E);
2352 elsif Has_Rep_Pragma (E, Name_Volatile)
2354 Has_Rep_Pragma (E, Name_Volatile_Components)
2357 ("stand alone volatile constant must be " &
2358 "imported ('R'M 'C.6(13))", E);
2362 -- Static objects require special handling
2364 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
2365 and then Is_Statically_Allocated (E)
2367 Freeze_Static_Object (E);
2370 -- Remaining step is to layout objects
2372 if Ekind (E) = E_Variable
2374 Ekind (E) = E_Constant
2376 Ekind (E) = E_Loop_Parameter
2384 -- Case of a type or subtype being frozen
2387 -- The type may be defined in a generic unit. This can occur when
2388 -- freezing a generic function that returns the type (which is
2389 -- defined in a parent unit). It is clearly meaningless to freeze
2390 -- this type. However, if it is a subtype, its size may be determi-
2391 -- nable and used in subsequent checks, so might as well try to
2394 if Present (Scope (E))
2395 and then Is_Generic_Unit (Scope (E))
2397 Check_Compile_Time_Size (E);
2401 -- Deal with special cases of freezing for subtype
2403 if E /= Base_Type (E) then
2405 -- If ancestor subtype present, freeze that first.
2406 -- Note that this will also get the base type frozen.
2408 Atype := Ancestor_Subtype (E);
2410 if Present (Atype) then
2411 Freeze_And_Append (Atype, Loc, Result);
2413 -- Otherwise freeze the base type of the entity before
2414 -- freezing the entity itself, (RM 13.14(15)).
2416 elsif E /= Base_Type (E) then
2417 Freeze_And_Append (Base_Type (E), Loc, Result);
2420 -- For a derived type, freeze its parent type first (RM 13.14(15))
2422 elsif Is_Derived_Type (E) then
2423 Freeze_And_Append (Etype (E), Loc, Result);
2424 Freeze_And_Append (First_Subtype (Etype (E)), Loc, Result);
2427 -- For array type, freeze index types and component type first
2428 -- before freezing the array (RM 13.14(15)).
2430 if Is_Array_Type (E) then
2432 Ctyp : constant Entity_Id := Component_Type (E);
2435 Non_Standard_Enum : Boolean := False;
2436 -- Set true if any of the index types is an enumeration
2437 -- type with a non-standard representation.
2440 Freeze_And_Append (Ctyp, Loc, Result);
2442 Indx := First_Index (E);
2443 while Present (Indx) loop
2444 Freeze_And_Append (Etype (Indx), Loc, Result);
2446 if Is_Enumeration_Type (Etype (Indx))
2447 and then Has_Non_Standard_Rep (Etype (Indx))
2449 Non_Standard_Enum := True;
2455 -- Processing that is done only for base types
2457 if Ekind (E) = E_Array_Type then
2459 -- Propagate flags for component type
2461 if Is_Controlled (Component_Type (E))
2462 or else Has_Controlled_Component (Ctyp)
2464 Set_Has_Controlled_Component (E);
2467 if Has_Unchecked_Union (Component_Type (E)) then
2468 Set_Has_Unchecked_Union (E);
2471 -- If packing was requested or if the component size was set
2472 -- explicitly, then see if bit packing is required. This
2473 -- processing is only done for base types, since all the
2474 -- representation aspects involved are type-related. This
2475 -- is not just an optimization, if we start processing the
2476 -- subtypes, they intefere with the settings on the base
2477 -- type (this is because Is_Packed has a slightly different
2478 -- meaning before and after freezing).
2485 if (Is_Packed (E) or else Has_Pragma_Pack (E))
2486 and then not Has_Atomic_Components (E)
2487 and then Known_Static_RM_Size (Ctyp)
2489 Csiz := UI_Max (RM_Size (Ctyp), 1);
2491 elsif Known_Component_Size (E) then
2492 Csiz := Component_Size (E);
2494 elsif not Known_Static_Esize (Ctyp) then
2498 Esiz := Esize (Ctyp);
2500 -- We can set the component size if it is less than
2501 -- 16, rounding it up to the next storage unit size.
2505 elsif Esiz <= 16 then
2511 -- Set component size up to match alignment if
2512 -- it would otherwise be less than the alignment.
2513 -- This deals with cases of types whose alignment
2514 -- exceeds their sizes (padded types).
2518 A : constant Uint := Alignment_In_Bits (Ctyp);
2529 if 1 <= Csiz and then Csiz <= 64 then
2531 -- We set the component size for all cases 1-64
2533 Set_Component_Size (Base_Type (E), Csiz);
2535 -- Check for base type of 8,16,32 bits, where the
2536 -- subtype has a length one less than the base type
2537 -- and is unsigned (e.g. Natural subtype of Integer)
2539 -- In such cases, if a component size was not set
2540 -- explicitly, then generate a warning.
2542 if Has_Pragma_Pack (E)
2543 and then not Has_Component_Size_Clause (E)
2545 (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2546 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2548 Error_Msg_Uint_1 := Csiz;
2550 Get_Rep_Pragma (First_Subtype (E), Name_Pack);
2552 if Present (Pnod) then
2554 ("pragma Pack causes component size to be ^?",
2557 ("\use Component_Size to set desired value",
2562 -- Actual packing is not needed for 8,16,32,64
2563 -- Also not needed for 24 if alignment is 1
2569 or else (Csiz = 24 and then Alignment (Ctyp) = 1)
2571 -- Here the array was requested to be packed, but
2572 -- the packing request had no effect, so Is_Packed
2575 -- Note: semantically this means that we lose
2576 -- track of the fact that a derived type inherited
2577 -- a pack pragma that was non-effective, but that
2580 -- We regard a Pack pragma as a request to set a
2581 -- representation characteristic, and this request
2584 Set_Is_Packed (Base_Type (E), False);
2586 -- In all other cases, packing is indeed needed
2589 Set_Has_Non_Standard_Rep (Base_Type (E));
2590 Set_Is_Bit_Packed_Array (Base_Type (E));
2591 Set_Is_Packed (Base_Type (E));
2596 -- Processing that is done only for subtypes
2599 -- Acquire alignment from base type
2601 if Unknown_Alignment (E) then
2602 Set_Alignment (E, Alignment (Base_Type (E)));
2606 -- For bit-packed arrays, check the size
2608 if Is_Bit_Packed_Array (E)
2609 and then Known_Esize (E)
2613 SizC : constant Node_Id := Size_Clause (E);
2616 -- It is not clear if it is possible to have no size
2617 -- clause at this stage, but this is not worth worrying
2618 -- about. Post the error on the entity name in the size
2619 -- clause if present, else on the type entity itself.
2621 if Present (SizC) then
2622 Check_Size (Name (SizC), E, Esize (E), Discard);
2624 Check_Size (E, E, Esize (E), Discard);
2629 -- Check one common case of a size given where the array
2630 -- needs to be packed, but was not so the size cannot be
2631 -- honored. This would of course be caught by the backend,
2632 -- and indeed we don't catch all cases. The point is that
2633 -- we can give a better error message in those cases that
2634 -- we do catch with the circuitry here.
2638 Ctyp : constant Entity_Id := Component_Type (E);
2641 if Present (Size_Clause (E))
2642 and then Known_Static_Esize (E)
2643 and then not Is_Bit_Packed_Array (E)
2644 and then not Has_Pragma_Pack (E)
2645 and then Number_Dimensions (E) = 1
2646 and then not Has_Component_Size_Clause (E)
2647 and then Known_Static_Esize (Ctyp)
2649 Get_Index_Bounds (First_Index (E), Lo, Hi);
2651 if Compile_Time_Known_Value (Lo)
2652 and then Compile_Time_Known_Value (Hi)
2653 and then Known_Static_RM_Size (Ctyp)
2654 and then RM_Size (Ctyp) < 64
2657 Lov : constant Uint := Expr_Value (Lo);
2658 Hiv : constant Uint := Expr_Value (Hi);
2659 Len : constant Uint :=
2660 UI_Max (Uint_0, Hiv - Lov + 1);
2661 Rsiz : constant Uint := RM_Size (Ctyp);
2663 -- What we are looking for here is the situation
2664 -- where the Esize given would be exactly right
2665 -- if there was a pragma Pack (resulting in the
2666 -- component size being the same as the RM_Size).
2667 -- Furthermore, the component type size must be
2668 -- an odd size (not a multiple of storage unit)
2671 if Esize (E) = Len * Rsiz
2672 and then Rsiz mod System_Storage_Unit /= 0
2675 ("size given for& too small",
2676 Size_Clause (E), E);
2678 ("\explicit pragma Pack is required",
2686 -- If any of the index types was an enumeration type with
2687 -- a non-standard rep clause, then we indicate that the
2688 -- array type is always packed (even if it is not bit packed).
2690 if Non_Standard_Enum then
2691 Set_Has_Non_Standard_Rep (Base_Type (E));
2692 Set_Is_Packed (Base_Type (E));
2695 Set_Component_Alignment_If_Not_Set (E);
2697 -- If the array is packed, we must create the packed array
2698 -- type to be used to actually implement the type. This is
2699 -- only needed for real array types (not for string literal
2700 -- types, since they are present only for the front end).
2703 and then Ekind (E) /= E_String_Literal_Subtype
2705 Create_Packed_Array_Type (E);
2706 Freeze_And_Append (Packed_Array_Type (E), Loc, Result);
2708 -- Size information of packed array type is copied to the
2709 -- array type, since this is really the representation.
2711 Set_Size_Info (E, Packed_Array_Type (E));
2712 Set_RM_Size (E, RM_Size (Packed_Array_Type (E)));
2715 -- For non-packed arrays set the alignment of the array
2716 -- to the alignment of the component type if it is unknown.
2717 -- Skip this in the atomic case, since atomic arrays may
2718 -- need larger alignments.
2720 if not Is_Packed (E)
2721 and then Unknown_Alignment (E)
2722 and then Known_Alignment (Ctyp)
2723 and then Known_Static_Component_Size (E)
2724 and then Known_Static_Esize (Ctyp)
2725 and then Esize (Ctyp) = Component_Size (E)
2726 and then not Is_Atomic (E)
2728 Set_Alignment (E, Alignment (Component_Type (E)));
2732 -- For a class-wide type, the corresponding specific type is
2733 -- frozen as well (RM 13.14(15))
2735 elsif Is_Class_Wide_Type (E) then
2736 Freeze_And_Append (Root_Type (E), Loc, Result);
2738 -- If the Class_Wide_Type is an Itype (when type is the anonymous
2739 -- parent of a derived type) and it is a library-level entity,
2740 -- generate an itype reference for it. Otherwise, its first
2741 -- explicit reference may be in an inner scope, which will be
2742 -- rejected by the back-end.
2745 and then Is_Compilation_Unit (Scope (E))
2748 Ref : constant Node_Id := Make_Itype_Reference (Loc);
2753 Result := New_List (Ref);
2755 Append (Ref, Result);
2760 -- The equivalent type associated with a class-wide subtype
2761 -- needs to be frozen to ensure that its layout is done.
2762 -- Class-wide subtypes are currently only frozen on targets
2763 -- requiring front-end layout (see New_Class_Wide_Subtype
2764 -- and Make_CW_Equivalent_Type in exp_util.adb).
2766 if Ekind (E) = E_Class_Wide_Subtype
2767 and then Present (Equivalent_Type (E))
2769 Freeze_And_Append (Equivalent_Type (E), Loc, Result);
2772 -- For a record (sub)type, freeze all the component types (RM
2773 -- 13.14(15). We test for E_Record_(sub)Type here, rather than
2774 -- using Is_Record_Type, because we don't want to attempt the
2775 -- freeze for the case of a private type with record extension
2776 -- (we will do that later when the full type is frozen).
2778 elsif Ekind (E) = E_Record_Type
2779 or else Ekind (E) = E_Record_Subtype
2781 Freeze_Record_Type (E);
2783 -- For a concurrent type, freeze corresponding record type. This
2784 -- does not correpond to any specific rule in the RM, but the
2785 -- record type is essentially part of the concurrent type.
2786 -- Freeze as well all local entities. This includes record types
2787 -- created for entry parameter blocks, and whatever local entities
2788 -- may appear in the private part.
2790 elsif Is_Concurrent_Type (E) then
2791 if Present (Corresponding_Record_Type (E)) then
2793 (Corresponding_Record_Type (E), Loc, Result);
2796 Comp := First_Entity (E);
2798 while Present (Comp) loop
2799 if Is_Type (Comp) then
2800 Freeze_And_Append (Comp, Loc, Result);
2802 elsif (Ekind (Comp)) /= E_Function then
2803 if Is_Itype (Etype (Comp))
2804 and then Underlying_Type (Scope (Etype (Comp))) = E
2806 Undelay_Type (Etype (Comp));
2809 Freeze_And_Append (Etype (Comp), Loc, Result);
2815 -- Private types are required to point to the same freeze node
2816 -- as their corresponding full views. The freeze node itself
2817 -- has to point to the partial view of the entity (because
2818 -- from the partial view, we can retrieve the full view, but
2819 -- not the reverse). However, in order to freeze correctly,
2820 -- we need to freeze the full view. If we are freezing at the
2821 -- end of a scope (or within the scope of the private type),
2822 -- the partial and full views will have been swapped, the
2823 -- full view appears first in the entity chain and the swapping
2824 -- mechanism ensures that the pointers are properly set (on
2827 -- If we encounter the partial view before the full view
2828 -- (e.g. when freezing from another scope), we freeze the
2829 -- full view, and then set the pointers appropriately since
2830 -- we cannot rely on swapping to fix things up (subtypes in an
2831 -- outer scope might not get swapped).
2833 elsif Is_Incomplete_Or_Private_Type (E)
2834 and then not Is_Generic_Type (E)
2836 -- Case of full view present
2838 if Present (Full_View (E)) then
2840 -- If full view has already been frozen, then no
2841 -- further processing is required
2843 if Is_Frozen (Full_View (E)) then
2845 Set_Has_Delayed_Freeze (E, False);
2846 Set_Freeze_Node (E, Empty);
2847 Check_Debug_Info_Needed (E);
2849 -- Otherwise freeze full view and patch the pointers
2850 -- so that the freeze node will elaborate both views
2855 Full : constant Entity_Id := Full_View (E);
2858 if Is_Private_Type (Full)
2859 and then Present (Underlying_Full_View (Full))
2862 (Underlying_Full_View (Full), Loc, Result);
2865 Freeze_And_Append (Full, Loc, Result);
2867 if Has_Delayed_Freeze (E) then
2868 F_Node := Freeze_Node (Full);
2870 if Present (F_Node) then
2871 Set_Freeze_Node (E, F_Node);
2872 Set_Entity (F_Node, E);
2875 -- {Incomplete,Private}_Subtypes
2876 -- with Full_Views constrained by discriminants
2878 Set_Has_Delayed_Freeze (E, False);
2879 Set_Freeze_Node (E, Empty);
2884 Check_Debug_Info_Needed (E);
2887 -- AI-117 requires that the convention of a partial view
2888 -- be the same as the convention of the full view. Note
2889 -- that this is a recognized breach of privacy, but it's
2890 -- essential for logical consistency of representation,
2891 -- and the lack of a rule in RM95 was an oversight.
2893 Set_Convention (E, Convention (Full_View (E)));
2895 Set_Size_Known_At_Compile_Time (E,
2896 Size_Known_At_Compile_Time (Full_View (E)));
2898 -- Size information is copied from the full view to the
2899 -- incomplete or private view for consistency
2901 -- We skip this is the full view is not a type. This is
2902 -- very strange of course, and can only happen as a result
2903 -- of certain illegalities, such as a premature attempt to
2904 -- derive from an incomplete type.
2906 if Is_Type (Full_View (E)) then
2907 Set_Size_Info (E, Full_View (E));
2908 Set_RM_Size (E, RM_Size (Full_View (E)));
2913 -- Case of no full view present. If entity is derived or subtype,
2914 -- it is safe to freeze, correctness depends on the frozen status
2915 -- of parent. Otherwise it is either premature usage, or a Taft
2916 -- amendment type, so diagnosis is at the point of use and the
2917 -- type might be frozen later.
2919 elsif E /= Base_Type (E)
2920 or else Is_Derived_Type (E)
2925 Set_Is_Frozen (E, False);
2929 -- For access subprogram, freeze types of all formals, the return
2930 -- type was already frozen, since it is the Etype of the function.
2932 elsif Ekind (E) = E_Subprogram_Type then
2933 Formal := First_Formal (E);
2934 while Present (Formal) loop
2935 Freeze_And_Append (Etype (Formal), Loc, Result);
2936 Next_Formal (Formal);
2939 -- If the return type requires a transient scope, and we are on
2940 -- a target allowing functions to return with a depressed stack
2941 -- pointer, then we mark the function as requiring this treatment.
2943 if Functions_Return_By_DSP_On_Target
2944 and then Requires_Transient_Scope (Etype (E))
2946 Set_Function_Returns_With_DSP (E);
2949 Freeze_Subprogram (E);
2951 -- For access to a protected subprogram, freeze the equivalent
2952 -- type (however this is not set if we are not generating code)
2953 -- or if this is an anonymous type used just for resolution).
2955 elsif Ekind (E) = E_Access_Protected_Subprogram_Type
2956 and then Operating_Mode = Generate_Code
2957 and then Present (Equivalent_Type (E))
2959 Freeze_And_Append (Equivalent_Type (E), Loc, Result);
2962 -- Generic types are never seen by the back-end, and are also not
2963 -- processed by the expander (since the expander is turned off for
2964 -- generic processing), so we never need freeze nodes for them.
2966 if Is_Generic_Type (E) then
2970 -- Some special processing for non-generic types to complete
2971 -- representation details not known till the freeze point.
2973 if Is_Fixed_Point_Type (E) then
2974 Freeze_Fixed_Point_Type (E);
2976 -- Some error checks required for ordinary fixed-point type.
2977 -- Defer these till the freeze-point since we need the small
2978 -- and range values. We only do these checks for base types
2980 if Is_Ordinary_Fixed_Point_Type (E)
2981 and then E = Base_Type (E)
2983 if Small_Value (E) < Ureal_2_M_80 then
2984 Error_Msg_Name_1 := Name_Small;
2986 ("`&''%` is too small, minimum is 2.0'*'*(-80)", E);
2988 elsif Small_Value (E) > Ureal_2_80 then
2989 Error_Msg_Name_1 := Name_Small;
2991 ("`&''%` is too large, maximum is 2.0'*'*80", E);
2994 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
2995 Error_Msg_Name_1 := Name_First;
2997 ("`&''%` is too small, minimum is -10.0'*'*36", E);
3000 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
3001 Error_Msg_Name_1 := Name_Last;
3003 ("`&''%` is too large, maximum is 10.0'*'*36", E);
3007 elsif Is_Enumeration_Type (E) then
3008 Freeze_Enumeration_Type (E);
3010 elsif Is_Integer_Type (E) then
3011 Adjust_Esize_For_Alignment (E);
3013 elsif Is_Access_Type (E) then
3015 -- Check restriction for standard storage pool
3017 if No (Associated_Storage_Pool (E)) then
3018 Check_Restriction (No_Standard_Storage_Pools, E);
3021 -- Deal with error message for pure access type. This is not an
3022 -- error in Ada 2005 if there is no pool (see AI-366).
3024 if Is_Pure_Unit_Access_Type (E)
3025 and then (Ada_Version < Ada_05
3026 or else not No_Pool_Assigned (E))
3028 Error_Msg_N ("named access type not allowed in pure unit", E);
3032 -- Case of composite types
3034 if Is_Composite_Type (E) then
3036 -- AI-117 requires that all new primitives of a tagged type must
3037 -- inherit the convention of the full view of the type. Inherited
3038 -- and overriding operations are defined to inherit the convention
3039 -- of their parent or overridden subprogram (also specified in
3040 -- AI-117), and that will have occurred earlier (in
3041 -- Derive_Subprogram and New_Overloaded_Entity). Here we set the
3042 -- convention of primitives that are still convention Ada, which
3043 -- will ensure that any new primitives inherit the type's
3044 -- convention. Class-wide types can have a foreign convention
3045 -- inherited from their specific type, but are excluded from this
3046 -- since they don't have any associated primitives.
3048 if Is_Tagged_Type (E)
3049 and then not Is_Class_Wide_Type (E)
3050 and then Convention (E) /= Convention_Ada
3053 Prim_List : constant Elist_Id := Primitive_Operations (E);
3056 Prim := First_Elmt (Prim_List);
3057 while Present (Prim) loop
3058 if Convention (Node (Prim)) = Convention_Ada then
3059 Set_Convention (Node (Prim), Convention (E));
3068 -- Generate primitive operation references for a tagged type
3070 if Is_Tagged_Type (E)
3071 and then not Is_Class_Wide_Type (E)
3074 Prim_List : Elist_Id;
3079 -- Ada 2005 (AI-345): In case of concurrent type generate
3080 -- reference to the wrapper that allow us to dispatch calls
3081 -- through their implemented abstract interface types.
3083 -- The check for Present here is to protect against previously
3084 -- reported critical errors.
3086 if Is_Concurrent_Type (E)
3087 and then Present (Corresponding_Record_Type (E))
3089 pragma Assert (not Is_Empty_Elmt_List
3090 (Abstract_Interfaces
3091 (Corresponding_Record_Type (E))));
3093 Prim_List := Primitive_Operations
3094 (Corresponding_Record_Type (E));
3096 Prim_List := Primitive_Operations (E);
3099 -- Loop to generate references for primitive operations
3101 Prim := First_Elmt (Prim_List);
3102 while Present (Prim) loop
3105 -- If the operation is derived, get the original for cross-
3106 -- reference purposes (it is the original for which we want
3107 -- the xref, and for which the comes from source test needs
3108 -- to be performed).
3110 while Present (Alias (Ent)) loop
3114 Generate_Reference (E, Ent, 'p', Set_Ref => False);
3118 -- If we get an exception, then something peculiar has happened
3119 -- probably as a result of a previous error. Since this is only
3120 -- for non-critical cross-references, ignore the error.
3123 when others => null;
3127 -- Now that all types from which E may depend are frozen, see
3128 -- if the size is known at compile time, if it must be unsigned,
3129 -- or if strict alignent is required
3131 Check_Compile_Time_Size (E);
3132 Check_Unsigned_Type (E);
3134 if Base_Type (E) = E then
3135 Check_Strict_Alignment (E);
3138 -- Do not allow a size clause for a type which does not have a size
3139 -- that is known at compile time
3141 if Has_Size_Clause (E)
3142 and then not Size_Known_At_Compile_Time (E)
3144 -- Supress this message if errors posted on E, even if we are
3145 -- in all errors mode, since this is often a junk message
3147 if not Error_Posted (E) then
3149 ("size clause not allowed for variable length type",
3154 -- Remaining process is to set/verify the representation information,
3155 -- in particular the size and alignment values. This processing is
3156 -- not required for generic types, since generic types do not play
3157 -- any part in code generation, and so the size and alignment values
3158 -- for such types are irrelevant.
3160 if Is_Generic_Type (E) then
3163 -- Otherwise we call the layout procedure
3169 -- End of freeze processing for type entities
3172 -- Here is where we logically freeze the current entity. If it has a
3173 -- freeze node, then this is the point at which the freeze node is
3174 -- linked into the result list.
3176 if Has_Delayed_Freeze (E) then
3178 -- If a freeze node is already allocated, use it, otherwise allocate
3179 -- a new one. The preallocation happens in the case of anonymous base
3180 -- types, where we preallocate so that we can set First_Subtype_Link.
3181 -- Note that we reset the Sloc to the current freeze location.
3183 if Present (Freeze_Node (E)) then
3184 F_Node := Freeze_Node (E);
3185 Set_Sloc (F_Node, Loc);
3188 F_Node := New_Node (N_Freeze_Entity, Loc);
3189 Set_Freeze_Node (E, F_Node);
3190 Set_Access_Types_To_Process (F_Node, No_Elist);
3191 Set_TSS_Elist (F_Node, No_Elist);
3192 Set_Actions (F_Node, No_List);
3195 Set_Entity (F_Node, E);
3197 if Result = No_List then
3198 Result := New_List (F_Node);
3200 Append (F_Node, Result);
3203 -- A final pass over record types with discriminants. If the type
3204 -- has an incomplete declaration, there may be constrained access
3205 -- subtypes declared elsewhere, which do not depend on the discrimi-
3206 -- nants of the type, and which are used as component types (i.e.
3207 -- the full view is a recursive type). The designated types of these
3208 -- subtypes can only be elaborated after the type itself, and they
3209 -- need an itype reference.
3211 if Ekind (E) = E_Record_Type
3212 and then Has_Discriminants (E)
3220 Comp := First_Component (E);
3222 while Present (Comp) loop
3223 Typ := Etype (Comp);
3225 if Ekind (Comp) = E_Component
3226 and then Is_Access_Type (Typ)
3227 and then Scope (Typ) /= E
3228 and then Base_Type (Designated_Type (Typ)) = E
3229 and then Is_Itype (Designated_Type (Typ))
3231 IR := Make_Itype_Reference (Sloc (Comp));
3232 Set_Itype (IR, Designated_Type (Typ));
3233 Append (IR, Result);
3236 Next_Component (Comp);
3242 -- When a type is frozen, the first subtype of the type is frozen as
3243 -- well (RM 13.14(15)). This has to be done after freezing the type,
3244 -- since obviously the first subtype depends on its own base type.
3247 Freeze_And_Append (First_Subtype (E), Loc, Result);
3249 -- If we just froze a tagged non-class wide record, then freeze the
3250 -- corresponding class-wide type. This must be done after the tagged
3251 -- type itself is frozen, because the class-wide type refers to the
3252 -- tagged type which generates the class.
3254 if Is_Tagged_Type (E)
3255 and then not Is_Class_Wide_Type (E)
3256 and then Present (Class_Wide_Type (E))
3258 Freeze_And_Append (Class_Wide_Type (E), Loc, Result);
3262 Check_Debug_Info_Needed (E);
3264 -- Special handling for subprograms
3266 if Is_Subprogram (E) then
3268 -- If subprogram has address clause then reset Is_Public flag, since
3269 -- we do not want the backend to generate external references.
3271 if Present (Address_Clause (E))
3272 and then not Is_Library_Level_Entity (E)
3274 Set_Is_Public (E, False);
3276 -- If no address clause and not intrinsic, then for imported
3277 -- subprogram in main unit, generate descriptor if we are in
3278 -- Propagate_Exceptions mode.
3280 elsif Propagate_Exceptions
3281 and then Is_Imported (E)
3282 and then not Is_Intrinsic_Subprogram (E)
3283 and then Convention (E) /= Convention_Stubbed
3285 if Result = No_List then
3286 Result := Empty_List;
3289 Generate_Subprogram_Descriptor_For_Imported_Subprogram
3297 -----------------------------
3298 -- Freeze_Enumeration_Type --
3299 -----------------------------
3301 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
3303 if Has_Foreign_Convention (Typ)
3304 and then not Has_Size_Clause (Typ)
3305 and then Esize (Typ) < Standard_Integer_Size
3307 Init_Esize (Typ, Standard_Integer_Size);
3309 Adjust_Esize_For_Alignment (Typ);
3311 end Freeze_Enumeration_Type;
3313 -----------------------
3314 -- Freeze_Expression --
3315 -----------------------
3317 procedure Freeze_Expression (N : Node_Id) is
3318 In_Def_Exp : constant Boolean := In_Default_Expression;
3321 Desig_Typ : Entity_Id;
3325 Freeze_Outside : Boolean := False;
3326 -- This flag is set true if the entity must be frozen outside the
3327 -- current subprogram. This happens in the case of expander generated
3328 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
3329 -- not freeze all entities like other bodies, but which nevertheless
3330 -- may reference entities that have to be frozen before the body and
3331 -- obviously cannot be frozen inside the body.
3333 function In_Exp_Body (N : Node_Id) return Boolean;
3334 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
3335 -- it is the handled statement sequence of an expander-generated
3336 -- subprogram (init proc, or stream subprogram). If so, it returns
3337 -- True, otherwise False.
3343 function In_Exp_Body (N : Node_Id) return Boolean is
3347 if Nkind (N) = N_Subprogram_Body then
3353 if Nkind (P) /= N_Subprogram_Body then
3357 P := Defining_Unit_Name (Specification (P));
3359 if Nkind (P) = N_Defining_Identifier
3360 and then (Is_Init_Proc (P) or else
3361 Is_TSS (P, TSS_Stream_Input) or else
3362 Is_TSS (P, TSS_Stream_Output) or else
3363 Is_TSS (P, TSS_Stream_Read) or else
3364 Is_TSS (P, TSS_Stream_Write))
3373 -- Start of processing for Freeze_Expression
3376 -- Immediate return if freezing is inhibited. This flag is set by the
3377 -- analyzer to stop freezing on generated expressions that would cause
3378 -- freezing if they were in the source program, but which are not
3379 -- supposed to freeze, since they are created.
3381 if Must_Not_Freeze (N) then
3385 -- If expression is non-static, then it does not freeze in a default
3386 -- expression, see section "Handling of Default Expressions" in the
3387 -- spec of package Sem for further details. Note that we have to
3388 -- make sure that we actually have a real expression (if we have
3389 -- a subtype indication, we can't test Is_Static_Expression!)
3392 and then Nkind (N) in N_Subexpr
3393 and then not Is_Static_Expression (N)
3398 -- Freeze type of expression if not frozen already
3402 if Nkind (N) in N_Has_Etype then
3403 if not Is_Frozen (Etype (N)) then
3406 -- Base type may be an derived numeric type that is frozen at
3407 -- the point of declaration, but first_subtype is still unfrozen.
3409 elsif not Is_Frozen (First_Subtype (Etype (N))) then
3410 Typ := First_Subtype (Etype (N));
3414 -- For entity name, freeze entity if not frozen already. A special
3415 -- exception occurs for an identifier that did not come from source.
3416 -- We don't let such identifiers freeze a non-internal entity, i.e.
3417 -- an entity that did come from source, since such an identifier was
3418 -- generated by the expander, and cannot have any semantic effect on
3419 -- the freezing semantics. For example, this stops the parameter of
3420 -- an initialization procedure from freezing the variable.
3422 if Is_Entity_Name (N)
3423 and then not Is_Frozen (Entity (N))
3424 and then (Nkind (N) /= N_Identifier
3425 or else Comes_From_Source (N)
3426 or else not Comes_From_Source (Entity (N)))
3433 -- For an allocator freeze designated type if not frozen already
3435 -- For an aggregate whose component type is an access type, freeze
3436 -- the designated type now, so that its freeze does not appear within
3437 -- the loop that might be created in the expansion of the aggregate.
3438 -- If the designated type is a private type without full view, the
3439 -- expression cannot contain an allocator, so the type is not frozen.
3445 Desig_Typ := Designated_Type (Etype (N));
3448 if Is_Array_Type (Etype (N))
3449 and then Is_Access_Type (Component_Type (Etype (N)))
3451 Desig_Typ := Designated_Type (Component_Type (Etype (N)));
3454 when N_Selected_Component |
3455 N_Indexed_Component |
3458 if Is_Access_Type (Etype (Prefix (N))) then
3459 Desig_Typ := Designated_Type (Etype (Prefix (N)));
3466 if Desig_Typ /= Empty
3467 and then (Is_Frozen (Desig_Typ)
3468 or else (not Is_Fully_Defined (Desig_Typ)))
3473 -- All done if nothing needs freezing
3477 and then No (Desig_Typ)
3482 -- Loop for looking at the right place to insert the freeze nodes
3483 -- exiting from the loop when it is appropriate to insert the freeze
3484 -- node before the current node P.
3486 -- Also checks some special exceptions to the freezing rules. These
3487 -- cases result in a direct return, bypassing the freeze action.
3491 Parent_P := Parent (P);
3493 -- If we don't have a parent, then we are not in a well-formed
3494 -- tree. This is an unusual case, but there are some legitimate
3495 -- situations in which this occurs, notably when the expressions
3496 -- in the range of a type declaration are resolved. We simply
3497 -- ignore the freeze request in this case. Is this right ???
3499 if No (Parent_P) then
3503 -- See if we have got to an appropriate point in the tree
3505 case Nkind (Parent_P) is
3507 -- A special test for the exception of (RM 13.14(8)) for the case
3508 -- of per-object expressions (RM 3.8(18)) occurring in component
3509 -- definition or a discrete subtype definition. Note that we test
3510 -- for a component declaration which includes both cases we are
3511 -- interested in, and furthermore the tree does not have explicit
3512 -- nodes for either of these two constructs.
3514 when N_Component_Declaration =>
3516 -- The case we want to test for here is an identifier that is
3517 -- a per-object expression, this is either a discriminant that
3518 -- appears in a context other than the component declaration
3519 -- or it is a reference to the type of the enclosing construct.
3521 -- For either of these cases, we skip the freezing
3523 if not In_Default_Expression
3524 and then Nkind (N) = N_Identifier
3525 and then (Present (Entity (N)))
3527 -- We recognize the discriminant case by just looking for
3528 -- a reference to a discriminant. It can only be one for
3529 -- the enclosing construct. Skip freezing in this case.
3531 if Ekind (Entity (N)) = E_Discriminant then
3534 -- For the case of a reference to the enclosing record,
3535 -- (or task or protected type), we look for a type that
3536 -- matches the current scope.
3538 elsif Entity (N) = Current_Scope then
3543 -- If we have an enumeration literal that appears as the choice in
3544 -- the aggregate of an enumeration representation clause, then
3545 -- freezing does not occur (RM 13.14(10)).
3547 when N_Enumeration_Representation_Clause =>
3549 -- The case we are looking for is an enumeration literal
3551 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
3552 and then Is_Enumeration_Type (Etype (N))
3554 -- If enumeration literal appears directly as the choice,
3555 -- do not freeze (this is the normal non-overloade case)
3557 if Nkind (Parent (N)) = N_Component_Association
3558 and then First (Choices (Parent (N))) = N
3562 -- If enumeration literal appears as the name of a
3563 -- function which is the choice, then also do not freeze.
3564 -- This happens in the overloaded literal case, where the
3565 -- enumeration literal is temporarily changed to a function
3566 -- call for overloading analysis purposes.
3568 elsif Nkind (Parent (N)) = N_Function_Call
3570 Nkind (Parent (Parent (N))) = N_Component_Association
3572 First (Choices (Parent (Parent (N)))) = Parent (N)
3578 -- Normally if the parent is a handled sequence of statements,
3579 -- then the current node must be a statement, and that is an
3580 -- appropriate place to insert a freeze node.
3582 when N_Handled_Sequence_Of_Statements =>
3584 -- An exception occurs when the sequence of statements is for
3585 -- an expander generated body that did not do the usual freeze
3586 -- all operation. In this case we usually want to freeze
3587 -- outside this body, not inside it, and we skip past the
3588 -- subprogram body that we are inside.
3590 if In_Exp_Body (Parent_P) then
3592 -- However, we *do* want to freeze at this point if we have
3593 -- an entity to freeze, and that entity is declared *inside*
3594 -- the body of the expander generated procedure. This case
3595 -- is recognized by the scope of the type, which is either
3596 -- the spec for some enclosing body, or (in the case of
3597 -- init_procs, for which there are no separate specs) the
3601 Subp : constant Node_Id := Parent (Parent_P);
3605 if Nkind (Subp) = N_Subprogram_Body then
3606 Cspc := Corresponding_Spec (Subp);
3608 if (Present (Typ) and then Scope (Typ) = Cspc)
3610 (Present (Nam) and then Scope (Nam) = Cspc)
3615 and then Scope (Typ) = Current_Scope
3616 and then Current_Scope = Defining_Entity (Subp)
3623 -- If not that exception to the exception, then this is
3624 -- where we delay the freeze till outside the body.
3626 Parent_P := Parent (Parent_P);
3627 Freeze_Outside := True;
3629 -- Here if normal case where we are in handled statement
3630 -- sequence and want to do the insertion right there.
3636 -- If parent is a body or a spec or a block, then the current
3637 -- node is a statement or declaration and we can insert the
3638 -- freeze node before it.
3640 when N_Package_Specification |
3646 N_Block_Statement => exit;
3648 -- The expander is allowed to define types in any statements list,
3649 -- so any of the following parent nodes also mark a freezing point
3650 -- if the actual node is in a list of statements or declarations.
3652 when N_Exception_Handler |
3655 N_Case_Statement_Alternative |
3656 N_Compilation_Unit_Aux |
3657 N_Selective_Accept |
3658 N_Accept_Alternative |
3659 N_Delay_Alternative |
3660 N_Conditional_Entry_Call |
3661 N_Entry_Call_Alternative |
3662 N_Triggering_Alternative |
3666 exit when Is_List_Member (P);
3668 -- Note: The N_Loop_Statement is a special case. A type that
3669 -- appears in the source can never be frozen in a loop (this
3670 -- occurs only because of a loop expanded by the expander), so we
3671 -- keep on going. Otherwise we terminate the search. Same is true
3672 -- of any entity which comes from source. (if they have a
3673 -- predefined type, that type does not appear to come from source,
3674 -- but the entity should not be frozen here).
3676 when N_Loop_Statement =>
3677 exit when not Comes_From_Source (Etype (N))
3678 and then (No (Nam) or else not Comes_From_Source (Nam));
3680 -- For all other cases, keep looking at parents
3686 -- We fall through the case if we did not yet find the proper
3687 -- place in the free for inserting the freeze node, so climb!
3692 -- If the expression appears in a record or an initialization procedure,
3693 -- the freeze nodes are collected and attached to the current scope, to
3694 -- be inserted and analyzed on exit from the scope, to insure that
3695 -- generated entities appear in the correct scope. If the expression is
3696 -- a default for a discriminant specification, the scope is still void.
3697 -- The expression can also appear in the discriminant part of a private
3698 -- or concurrent type.
3700 -- If the expression appears in a constrained subcomponent of an
3701 -- enclosing record declaration, the freeze nodes must be attached to
3702 -- the outer record type so they can eventually be placed in the
3703 -- enclosing declaration list.
3705 -- The other case requiring this special handling is if we are in
3706 -- a default expression, since in that case we are about to freeze
3707 -- a static type, and the freeze scope needs to be the outer scope,
3708 -- not the scope of the subprogram with the default parameter.
3710 -- For default expressions in generic units, the Move_Freeze_Nodes
3711 -- mechanism (see sem_ch12.adb) takes care of placing them at the
3712 -- proper place, after the generic unit.
3714 if (In_Def_Exp and not Inside_A_Generic)
3715 or else Freeze_Outside
3716 or else (Is_Type (Current_Scope)
3717 and then (not Is_Concurrent_Type (Current_Scope)
3718 or else not Has_Completion (Current_Scope)))
3719 or else Ekind (Current_Scope) = E_Void
3722 Loc : constant Source_Ptr := Sloc (Current_Scope);
3723 Freeze_Nodes : List_Id := No_List;
3724 Pos : Int := Scope_Stack.Last;
3727 if Present (Desig_Typ) then
3728 Freeze_And_Append (Desig_Typ, Loc, Freeze_Nodes);
3731 if Present (Typ) then
3732 Freeze_And_Append (Typ, Loc, Freeze_Nodes);
3735 if Present (Nam) then
3736 Freeze_And_Append (Nam, Loc, Freeze_Nodes);
3739 -- The current scope may be that of a constrained component of
3740 -- an enclosing record declaration, which is above the current
3741 -- scope in the scope stack.
3743 if Is_Record_Type (Scope (Current_Scope)) then
3747 if Is_Non_Empty_List (Freeze_Nodes) then
3748 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
3749 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
3752 Append_List (Freeze_Nodes, Scope_Stack.Table
3753 (Pos).Pending_Freeze_Actions);
3761 -- Now we have the right place to do the freezing. First, a special
3762 -- adjustment, if we are in default expression analysis mode, these
3763 -- freeze actions must not be thrown away (normally all inserted
3764 -- actions are thrown away in this mode. However, the freeze actions
3765 -- are from static expressions and one of the important reasons we
3766 -- are doing this special analysis is to get these freeze actions.
3767 -- Therefore we turn off the In_Default_Expression mode to propagate
3768 -- these freeze actions. This also means they get properly analyzed
3771 In_Default_Expression := False;
3773 -- Freeze the designated type of an allocator (RM 13.14(13))
3775 if Present (Desig_Typ) then
3776 Freeze_Before (P, Desig_Typ);
3779 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
3780 -- the enumeration representation clause exception in the loop above.
3782 if Present (Typ) then
3783 Freeze_Before (P, Typ);
3786 -- Freeze name if one is present (RM 13.14(11))
3788 if Present (Nam) then
3789 Freeze_Before (P, Nam);
3792 In_Default_Expression := In_Def_Exp;
3793 end Freeze_Expression;
3795 -----------------------------
3796 -- Freeze_Fixed_Point_Type --
3797 -----------------------------
3799 -- Certain fixed-point types and subtypes, including implicit base types
3800 -- and declared first subtypes, have not yet set up a range. This is
3801 -- because the range cannot be set until the Small and Size values are
3802 -- known, and these are not known till the type is frozen.
3804 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
3805 -- whose bounds are unanalyzed real literals. This routine will recognize
3806 -- this case, and transform this range node into a properly typed range
3807 -- with properly analyzed and resolved values.
3809 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
3810 Rng : constant Node_Id := Scalar_Range (Typ);
3811 Lo : constant Node_Id := Low_Bound (Rng);
3812 Hi : constant Node_Id := High_Bound (Rng);
3813 Btyp : constant Entity_Id := Base_Type (Typ);
3814 Brng : constant Node_Id := Scalar_Range (Btyp);
3815 BLo : constant Node_Id := Low_Bound (Brng);
3816 BHi : constant Node_Id := High_Bound (Brng);
3817 Small : constant Ureal := Small_Value (Typ);
3824 function Fsize (Lov, Hiv : Ureal) return Nat;
3825 -- Returns size of type with given bounds. Also leaves these
3826 -- bounds set as the current bounds of the Typ.
3832 function Fsize (Lov, Hiv : Ureal) return Nat is
3834 Set_Realval (Lo, Lov);
3835 Set_Realval (Hi, Hiv);
3836 return Minimum_Size (Typ);
3839 -- Start of processing for Freeze_Fixed_Point_Type
3842 -- If Esize of a subtype has not previously been set, set it now
3844 if Unknown_Esize (Typ) then
3845 Atype := Ancestor_Subtype (Typ);
3847 if Present (Atype) then
3848 Set_Esize (Typ, Esize (Atype));
3850 Set_Esize (Typ, Esize (Base_Type (Typ)));
3854 -- Immediate return if the range is already analyzed. This means
3855 -- that the range is already set, and does not need to be computed
3858 if Analyzed (Rng) then
3862 -- Immediate return if either of the bounds raises Constraint_Error
3864 if Raises_Constraint_Error (Lo)
3865 or else Raises_Constraint_Error (Hi)
3870 Loval := Realval (Lo);
3871 Hival := Realval (Hi);
3873 -- Ordinary fixed-point case
3875 if Is_Ordinary_Fixed_Point_Type (Typ) then
3877 -- For the ordinary fixed-point case, we are allowed to fudge the
3878 -- end-points up or down by small. Generally we prefer to fudge
3879 -- up, i.e. widen the bounds for non-model numbers so that the
3880 -- end points are included. However there are cases in which this
3881 -- cannot be done, and indeed cases in which we may need to narrow
3882 -- the bounds. The following circuit makes the decision.
3884 -- Note: our terminology here is that Incl_EP means that the
3885 -- bounds are widened by Small if necessary to include the end
3886 -- points, and Excl_EP means that the bounds are narrowed by
3887 -- Small to exclude the end-points if this reduces the size.
3889 -- Note that in the Incl case, all we care about is including the
3890 -- end-points. In the Excl case, we want to narrow the bounds as
3891 -- much as permitted by the RM, to give the smallest possible size.
3894 Loval_Incl_EP : Ureal;
3895 Hival_Incl_EP : Ureal;
3897 Loval_Excl_EP : Ureal;
3898 Hival_Excl_EP : Ureal;
3904 First_Subt : Entity_Id;
3909 -- First step. Base types are required to be symmetrical. Right
3910 -- now, the base type range is a copy of the first subtype range.
3911 -- This will be corrected before we are done, but right away we
3912 -- need to deal with the case where both bounds are non-negative.
3913 -- In this case, we set the low bound to the negative of the high
3914 -- bound, to make sure that the size is computed to include the
3915 -- required sign. Note that we do not need to worry about the
3916 -- case of both bounds negative, because the sign will be dealt
3917 -- with anyway. Furthermore we can't just go making such a bound
3918 -- symmetrical, since in a twos-complement system, there is an
3919 -- extra negative value which could not be accomodated on the
3923 and then not UR_Is_Negative (Loval)
3924 and then Hival > Loval
3927 Set_Realval (Lo, Loval);
3930 -- Compute the fudged bounds. If the number is a model number,
3931 -- then we do nothing to include it, but we are allowed to backoff
3932 -- to the next adjacent model number when we exclude it. If it is
3933 -- not a model number then we straddle the two values with the
3934 -- model numbers on either side.
3936 Model_Num := UR_Trunc (Loval / Small) * Small;
3938 if Loval = Model_Num then
3939 Loval_Incl_EP := Model_Num;
3941 Loval_Incl_EP := Model_Num - Small;
3944 -- The low value excluding the end point is Small greater, but
3945 -- we do not do this exclusion if the low value is positive,
3946 -- since it can't help the size and could actually hurt by
3947 -- crossing the high bound.
3949 if UR_Is_Negative (Loval_Incl_EP) then
3950 Loval_Excl_EP := Loval_Incl_EP + Small;
3952 Loval_Excl_EP := Loval_Incl_EP;
3955 -- Similar processing for upper bound and high value
3957 Model_Num := UR_Trunc (Hival / Small) * Small;
3959 if Hival = Model_Num then
3960 Hival_Incl_EP := Model_Num;
3962 Hival_Incl_EP := Model_Num + Small;
3965 if UR_Is_Positive (Hival_Incl_EP) then
3966 Hival_Excl_EP := Hival_Incl_EP - Small;
3968 Hival_Excl_EP := Hival_Incl_EP;
3971 -- One further adjustment is needed. In the case of subtypes,
3972 -- we cannot go outside the range of the base type, or we get
3973 -- peculiarities, and the base type range is already set. This
3974 -- only applies to the Incl values, since clearly the Excl
3975 -- values are already as restricted as they are allowed to be.
3978 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
3979 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
3982 -- Get size including and excluding end points
3984 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
3985 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
3987 -- No need to exclude end-points if it does not reduce size
3989 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
3990 Loval_Excl_EP := Loval_Incl_EP;
3993 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
3994 Hival_Excl_EP := Hival_Incl_EP;
3997 -- Now we set the actual size to be used. We want to use the
3998 -- bounds fudged up to include the end-points but only if this
3999 -- can be done without violating a specifically given size
4000 -- size clause or causing an unacceptable increase in size.
4002 -- Case of size clause given
4004 if Has_Size_Clause (Typ) then
4006 -- Use the inclusive size only if it is consistent with
4007 -- the explicitly specified size.
4009 if Size_Incl_EP <= RM_Size (Typ) then
4010 Actual_Lo := Loval_Incl_EP;
4011 Actual_Hi := Hival_Incl_EP;
4012 Actual_Size := Size_Incl_EP;
4014 -- If the inclusive size is too large, we try excluding
4015 -- the end-points (will be caught later if does not work).
4018 Actual_Lo := Loval_Excl_EP;
4019 Actual_Hi := Hival_Excl_EP;
4020 Actual_Size := Size_Excl_EP;
4023 -- Case of size clause not given
4026 -- If we have a base type whose corresponding first subtype
4027 -- has an explicit size that is large enough to include our
4028 -- end-points, then do so. There is no point in working hard
4029 -- to get a base type whose size is smaller than the specified
4030 -- size of the first subtype.
4032 First_Subt := First_Subtype (Typ);
4034 if Has_Size_Clause (First_Subt)
4035 and then Size_Incl_EP <= Esize (First_Subt)
4037 Actual_Size := Size_Incl_EP;
4038 Actual_Lo := Loval_Incl_EP;
4039 Actual_Hi := Hival_Incl_EP;
4041 -- If excluding the end-points makes the size smaller and
4042 -- results in a size of 8,16,32,64, then we take the smaller
4043 -- size. For the 64 case, this is compulsory. For the other
4044 -- cases, it seems reasonable. We like to include end points
4045 -- if we can, but not at the expense of moving to the next
4046 -- natural boundary of size.
4048 elsif Size_Incl_EP /= Size_Excl_EP
4050 (Size_Excl_EP = 8 or else
4051 Size_Excl_EP = 16 or else
4052 Size_Excl_EP = 32 or else
4055 Actual_Size := Size_Excl_EP;
4056 Actual_Lo := Loval_Excl_EP;
4057 Actual_Hi := Hival_Excl_EP;
4059 -- Otherwise we can definitely include the end points
4062 Actual_Size := Size_Incl_EP;
4063 Actual_Lo := Loval_Incl_EP;
4064 Actual_Hi := Hival_Incl_EP;
4067 -- One pathological case: normally we never fudge a low bound
4068 -- down, since it would seem to increase the size (if it has
4069 -- any effect), but for ranges containing single value, or no
4070 -- values, the high bound can be small too large. Consider:
4072 -- type t is delta 2.0**(-14)
4073 -- range 131072.0 .. 0;
4075 -- That lower bound is *just* outside the range of 32 bits, and
4076 -- does need fudging down in this case. Note that the bounds
4077 -- will always have crossed here, since the high bound will be
4078 -- fudged down if necessary, as in the case of:
4080 -- type t is delta 2.0**(-14)
4081 -- range 131072.0 .. 131072.0;
4083 -- So we detect the situation by looking for crossed bounds,
4084 -- and if the bounds are crossed, and the low bound is greater
4085 -- than zero, we will always back it off by small, since this
4086 -- is completely harmless.
4088 if Actual_Lo > Actual_Hi then
4089 if UR_Is_Positive (Actual_Lo) then
4090 Actual_Lo := Loval_Incl_EP - Small;
4091 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
4093 -- And of course, we need to do exactly the same parallel
4094 -- fudge for flat ranges in the negative region.
4096 elsif UR_Is_Negative (Actual_Hi) then
4097 Actual_Hi := Hival_Incl_EP + Small;
4098 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
4103 Set_Realval (Lo, Actual_Lo);
4104 Set_Realval (Hi, Actual_Hi);
4107 -- For the decimal case, none of this fudging is required, since there
4108 -- are no end-point problems in the decimal case (the end-points are
4109 -- always included).
4112 Actual_Size := Fsize (Loval, Hival);
4115 -- At this stage, the actual size has been calculated and the proper
4116 -- required bounds are stored in the low and high bounds.
4118 if Actual_Size > 64 then
4119 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
4121 ("size required (^) for type& too large, maximum is 64", Typ);
4125 -- Check size against explicit given size
4127 if Has_Size_Clause (Typ) then
4128 if Actual_Size > RM_Size (Typ) then
4129 Error_Msg_Uint_1 := RM_Size (Typ);
4130 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
4132 ("size given (^) for type& too small, minimum is ^",
4133 Size_Clause (Typ), Typ);
4136 Actual_Size := UI_To_Int (Esize (Typ));
4139 -- Increase size to next natural boundary if no size clause given
4142 if Actual_Size <= 8 then
4144 elsif Actual_Size <= 16 then
4146 elsif Actual_Size <= 32 then
4152 Init_Esize (Typ, Actual_Size);
4153 Adjust_Esize_For_Alignment (Typ);
4156 -- If we have a base type, then expand the bounds so that they extend to
4157 -- the full width of the allocated size in bits, to avoid junk range
4158 -- checks on intermediate computations.
4160 if Base_Type (Typ) = Typ then
4161 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
4162 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
4165 -- Final step is to reanalyze the bounds using the proper type
4166 -- and set the Corresponding_Integer_Value fields of the literals.
4168 Set_Etype (Lo, Empty);
4169 Set_Analyzed (Lo, False);
4172 -- Resolve with universal fixed if the base type, and the base type if
4173 -- it is a subtype. Note we can't resolve the base type with itself,
4174 -- that would be a reference before definition.
4177 Resolve (Lo, Universal_Fixed);
4182 -- Set corresponding integer value for bound
4184 Set_Corresponding_Integer_Value
4185 (Lo, UR_To_Uint (Realval (Lo) / Small));
4187 -- Similar processing for high bound
4189 Set_Etype (Hi, Empty);
4190 Set_Analyzed (Hi, False);
4194 Resolve (Hi, Universal_Fixed);
4199 Set_Corresponding_Integer_Value
4200 (Hi, UR_To_Uint (Realval (Hi) / Small));
4202 -- Set type of range to correspond to bounds
4204 Set_Etype (Rng, Etype (Lo));
4206 -- Set Esize to calculated size if not set already
4208 if Unknown_Esize (Typ) then
4209 Init_Esize (Typ, Actual_Size);
4212 -- Set RM_Size if not already set. If already set, check value
4215 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
4218 if RM_Size (Typ) /= Uint_0 then
4219 if RM_Size (Typ) < Minsiz then
4220 Error_Msg_Uint_1 := RM_Size (Typ);
4221 Error_Msg_Uint_2 := Minsiz;
4223 ("size given (^) for type& too small, minimum is ^",
4224 Size_Clause (Typ), Typ);
4228 Set_RM_Size (Typ, Minsiz);
4231 end Freeze_Fixed_Point_Type;
4237 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
4241 Set_Has_Delayed_Freeze (T);
4242 L := Freeze_Entity (T, Sloc (N));
4244 if Is_Non_Empty_List (L) then
4245 Insert_Actions (N, L);
4249 --------------------------
4250 -- Freeze_Static_Object --
4251 --------------------------
4253 procedure Freeze_Static_Object (E : Entity_Id) is
4255 Cannot_Be_Static : exception;
4256 -- Exception raised if the type of a static object cannot be made
4257 -- static. This happens if the type depends on non-global objects.
4259 procedure Ensure_Expression_Is_SA (N : Node_Id);
4260 -- Called to ensure that an expression used as part of a type
4261 -- definition is statically allocatable, which means that the type
4262 -- of the expression is statically allocatable, and the expression
4263 -- is either static, or a reference to a library level constant.
4265 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
4266 -- Called to mark a type as static, checking that it is possible
4267 -- to set the type as static. If it is not possible, then the
4268 -- exception Cannot_Be_Static is raised.
4270 -----------------------------
4271 -- Ensure_Expression_Is_SA --
4272 -----------------------------
4274 procedure Ensure_Expression_Is_SA (N : Node_Id) is
4278 Ensure_Type_Is_SA (Etype (N));
4280 if Is_Static_Expression (N) then
4283 elsif Nkind (N) = N_Identifier then
4287 and then Ekind (Ent) = E_Constant
4288 and then Is_Library_Level_Entity (Ent)
4294 raise Cannot_Be_Static;
4295 end Ensure_Expression_Is_SA;
4297 -----------------------
4298 -- Ensure_Type_Is_SA --
4299 -----------------------
4301 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
4306 -- If type is library level, we are all set
4308 if Is_Library_Level_Entity (Typ) then
4312 -- We are also OK if the type is already marked as statically
4313 -- allocated, which means we processed it before.
4315 if Is_Statically_Allocated (Typ) then
4319 -- Mark type as statically allocated
4321 Set_Is_Statically_Allocated (Typ);
4323 -- Check that it is safe to statically allocate this type
4325 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
4326 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
4327 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
4329 elsif Is_Array_Type (Typ) then
4330 N := First_Index (Typ);
4331 while Present (N) loop
4332 Ensure_Type_Is_SA (Etype (N));
4336 Ensure_Type_Is_SA (Component_Type (Typ));
4338 elsif Is_Access_Type (Typ) then
4339 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
4343 T : constant Entity_Id := Etype (Designated_Type (Typ));
4346 if T /= Standard_Void_Type then
4347 Ensure_Type_Is_SA (T);
4350 F := First_Formal (Designated_Type (Typ));
4352 while Present (F) loop
4353 Ensure_Type_Is_SA (Etype (F));
4359 Ensure_Type_Is_SA (Designated_Type (Typ));
4362 elsif Is_Record_Type (Typ) then
4363 C := First_Entity (Typ);
4365 while Present (C) loop
4366 if Ekind (C) = E_Discriminant
4367 or else Ekind (C) = E_Component
4369 Ensure_Type_Is_SA (Etype (C));
4371 elsif Is_Type (C) then
4372 Ensure_Type_Is_SA (C);
4378 elsif Ekind (Typ) = E_Subprogram_Type then
4379 Ensure_Type_Is_SA (Etype (Typ));
4381 C := First_Formal (Typ);
4382 while Present (C) loop
4383 Ensure_Type_Is_SA (Etype (C));
4388 raise Cannot_Be_Static;
4390 end Ensure_Type_Is_SA;
4392 -- Start of processing for Freeze_Static_Object
4395 Ensure_Type_Is_SA (Etype (E));
4397 -- Reset True_Constant flag, since something strange is going on with
4398 -- the scoping here, and our simple value tracing may not be sufficient
4399 -- for this indication to be reliable. We kill the Constant_Value
4400 -- indication for the same reason.
4402 Set_Is_True_Constant (E, False);
4403 Set_Current_Value (E, Empty);
4406 when Cannot_Be_Static =>
4408 -- If the object that cannot be static is imported or exported,
4409 -- then we give an error message saying that this object cannot
4410 -- be imported or exported.
4412 if Is_Imported (E) then
4414 ("& cannot be imported (local type is not constant)", E);
4416 -- Otherwise must be exported, something is wrong if compiler
4417 -- is marking something as statically allocated which cannot be).
4419 else pragma Assert (Is_Exported (E));
4421 ("& cannot be exported (local type is not constant)", E);
4423 end Freeze_Static_Object;
4425 -----------------------
4426 -- Freeze_Subprogram --
4427 -----------------------
4429 procedure Freeze_Subprogram (E : Entity_Id) is
4434 -- Subprogram may not have an address clause unless it is imported
4436 if Present (Address_Clause (E)) then
4437 if not Is_Imported (E) then
4439 ("address clause can only be given " &
4440 "for imported subprogram",
4441 Name (Address_Clause (E)));
4445 -- Reset the Pure indication on an imported subprogram unless an
4446 -- explicit Pure_Function pragma was present. We do this because
4447 -- otherwise it is an insidious error to call a non-pure function
4448 -- from pure unit and have calls mysteriously optimized away. What
4449 -- happens here is that the Import can bypass the normal check to
4450 -- ensure that pure units call only pure subprograms.
4453 and then Is_Pure (E)
4454 and then not Has_Pragma_Pure_Function (E)
4456 Set_Is_Pure (E, False);
4459 -- For non-foreign convention subprograms, this is where we create
4460 -- the extra formals (for accessibility level and constrained bit
4461 -- information). We delay this till the freeze point precisely so
4462 -- that we know the convention!
4464 if not Has_Foreign_Convention (E) then
4465 Create_Extra_Formals (E);
4468 -- If this is convention Ada and a Valued_Procedure, that's odd
4470 if Ekind (E) = E_Procedure
4471 and then Is_Valued_Procedure (E)
4472 and then Convention (E) = Convention_Ada
4473 and then Warn_On_Export_Import
4476 ("?Valued_Procedure has no effect for convention Ada", E);
4477 Set_Is_Valued_Procedure (E, False);
4480 -- Case of foreign convention
4485 -- For foreign conventions, warn about return of an
4486 -- unconstrained array.
4488 -- Note: we *do* allow a return by descriptor for the VMS case,
4489 -- though here there is probably more to be done ???
4491 if Ekind (E) = E_Function then
4492 Retype := Underlying_Type (Etype (E));
4494 -- If no return type, probably some other error, e.g. a
4495 -- missing full declaration, so ignore.
4500 -- If the return type is generic, we have emitted a warning
4501 -- earlier on, and there is nothing else to check here. Specific
4502 -- instantiations may lead to erroneous behavior.
4504 elsif Is_Generic_Type (Etype (E)) then
4507 elsif Is_Array_Type (Retype)
4508 and then not Is_Constrained (Retype)
4509 and then Mechanism (E) not in Descriptor_Codes
4510 and then Warn_On_Export_Import
4513 ("?foreign convention function& should not return " &
4514 "unconstrained array", E);
4519 -- If any of the formals for an exported foreign convention
4520 -- subprogram have defaults, then emit an appropriate warning since
4521 -- this is odd (default cannot be used from non-Ada code)
4523 if Is_Exported (E) then
4524 F := First_Formal (E);
4525 while Present (F) loop
4526 if Warn_On_Export_Import
4527 and then Present (Default_Value (F))
4530 ("?parameter cannot be defaulted in non-Ada call",
4539 -- For VMS, descriptor mechanisms for parameters are allowed only
4540 -- for imported subprograms.
4542 if OpenVMS_On_Target then
4543 if not Is_Imported (E) then
4544 F := First_Formal (E);
4545 while Present (F) loop
4546 if Mechanism (F) in Descriptor_Codes then
4548 ("descriptor mechanism for parameter not permitted", F);
4550 ("\can only be used for imported subprogram", F);
4558 -- Pragma Inline_Always is disallowed for dispatching subprograms
4559 -- because the address of such subprograms is saved in the dispatch
4560 -- table to support dispatching calls, and dispatching calls cannot
4561 -- be inlined. This is consistent with the restriction against using
4562 -- 'Access or 'Address on an Inline_Always subprogram.
4564 if Is_Dispatching_Operation (E) and then Is_Always_Inlined (E) then
4566 ("pragma Inline_Always not allowed for dispatching subprograms", E);
4568 end Freeze_Subprogram;
4570 ----------------------
4571 -- Is_Fully_Defined --
4572 ----------------------
4574 function Is_Fully_Defined (T : Entity_Id) return Boolean is
4576 if Ekind (T) = E_Class_Wide_Type then
4577 return Is_Fully_Defined (Etype (T));
4579 elsif Is_Array_Type (T) then
4580 return Is_Fully_Defined (Component_Type (T));
4582 elsif Is_Record_Type (T)
4583 and not Is_Private_Type (T)
4585 -- Verify that the record type has no components with
4586 -- private types without completion.
4592 Comp := First_Component (T);
4594 while Present (Comp) loop
4595 if not Is_Fully_Defined (Etype (Comp)) then
4599 Next_Component (Comp);
4604 else return not Is_Private_Type (T)
4605 or else Present (Full_View (Base_Type (T)));
4607 end Is_Fully_Defined;
4609 ---------------------------------
4610 -- Process_Default_Expressions --
4611 ---------------------------------
4613 procedure Process_Default_Expressions
4615 After : in out Node_Id)
4617 Loc : constant Source_Ptr := Sloc (E);
4624 Set_Default_Expressions_Processed (E);
4626 -- A subprogram instance and its associated anonymous subprogram
4627 -- share their signature. The default expression functions are defined
4628 -- in the wrapper packages for the anonymous subprogram, and should
4629 -- not be generated again for the instance.
4631 if Is_Generic_Instance (E)
4632 and then Present (Alias (E))
4633 and then Default_Expressions_Processed (Alias (E))
4638 Formal := First_Formal (E);
4640 while Present (Formal) loop
4641 if Present (Default_Value (Formal)) then
4643 -- We work with a copy of the default expression because we
4644 -- do not want to disturb the original, since this would mess
4645 -- up the conformance checking.
4647 Dcopy := New_Copy_Tree (Default_Value (Formal));
4649 -- The analysis of the expression may generate insert actions,
4650 -- which of course must not be executed. We wrap those actions
4651 -- in a procedure that is not called, and later on eliminated.
4652 -- The following cases have no side-effects, and are analyzed
4655 if Nkind (Dcopy) = N_Identifier
4656 or else Nkind (Dcopy) = N_Expanded_Name
4657 or else Nkind (Dcopy) = N_Integer_Literal
4658 or else (Nkind (Dcopy) = N_Real_Literal
4659 and then not Vax_Float (Etype (Dcopy)))
4660 or else Nkind (Dcopy) = N_Character_Literal
4661 or else Nkind (Dcopy) = N_String_Literal
4662 or else Nkind (Dcopy) = N_Null
4663 or else (Nkind (Dcopy) = N_Attribute_Reference
4665 Attribute_Name (Dcopy) = Name_Null_Parameter)
4668 -- If there is no default function, we must still do a full
4669 -- analyze call on the default value, to ensure that all
4670 -- error checks are performed, e.g. those associated with
4671 -- static evaluation. Note that this branch will always be
4672 -- taken if the analyzer is turned off (but we still need the
4675 -- Note: the setting of parent here is to meet the requirement
4676 -- that we can only analyze the expression while attached to
4677 -- the tree. Really the requirement is that the parent chain
4678 -- be set, we don't actually need to be in the tree.
4680 Set_Parent (Dcopy, Declaration_Node (Formal));
4683 -- Default expressions are resolved with their own type if the
4684 -- context is generic, to avoid anomalies with private types.
4686 if Ekind (Scope (E)) = E_Generic_Package then
4689 Resolve (Dcopy, Etype (Formal));
4692 -- If that resolved expression will raise constraint error,
4693 -- then flag the default value as raising constraint error.
4694 -- This allows a proper error message on the calls.
4696 if Raises_Constraint_Error (Dcopy) then
4697 Set_Raises_Constraint_Error (Default_Value (Formal));
4700 -- If the default is a parameterless call, we use the name of
4701 -- the called function directly, and there is no body to build.
4703 elsif Nkind (Dcopy) = N_Function_Call
4704 and then No (Parameter_Associations (Dcopy))
4708 -- Else construct and analyze the body of a wrapper procedure
4709 -- that contains an object declaration to hold the expression.
4710 -- Given that this is done only to complete the analysis, it
4711 -- simpler to build a procedure than a function which might
4712 -- involve secondary stack expansion.
4716 Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
4719 Make_Subprogram_Body (Loc,
4721 Make_Procedure_Specification (Loc,
4722 Defining_Unit_Name => Dnam),
4724 Declarations => New_List (
4725 Make_Object_Declaration (Loc,
4726 Defining_Identifier =>
4727 Make_Defining_Identifier (Loc,
4728 New_Internal_Name ('T')),
4729 Object_Definition =>
4730 New_Occurrence_Of (Etype (Formal), Loc),
4731 Expression => New_Copy_Tree (Dcopy))),
4733 Handled_Statement_Sequence =>
4734 Make_Handled_Sequence_Of_Statements (Loc,
4735 Statements => New_List));
4737 Set_Scope (Dnam, Scope (E));
4738 Set_Assignment_OK (First (Declarations (Dbody)));
4739 Set_Is_Eliminated (Dnam);
4740 Insert_After (After, Dbody);
4746 Next_Formal (Formal);
4749 end Process_Default_Expressions;
4751 ----------------------------------------
4752 -- Set_Component_Alignment_If_Not_Set --
4753 ----------------------------------------
4755 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
4757 -- Ignore if not base type, subtypes don't need anything
4759 if Typ /= Base_Type (Typ) then
4763 -- Do not override existing representation
4765 if Is_Packed (Typ) then
4768 elsif Has_Specified_Layout (Typ) then
4771 elsif Component_Alignment (Typ) /= Calign_Default then
4775 Set_Component_Alignment
4776 (Typ, Scope_Stack.Table
4777 (Scope_Stack.Last).Component_Alignment_Default);
4779 end Set_Component_Alignment_If_Not_Set;
4781 ---------------------------
4782 -- Set_Debug_Info_Needed --
4783 ---------------------------
4785 procedure Set_Debug_Info_Needed (T : Entity_Id) is
4788 or else Needs_Debug_Info (T)
4789 or else Debug_Info_Off (T)
4793 Set_Needs_Debug_Info (T);
4796 if Is_Object (T) then
4797 Set_Debug_Info_Needed (Etype (T));
4799 elsif Is_Type (T) then
4800 Set_Debug_Info_Needed (Etype (T));
4802 if Is_Record_Type (T) then
4804 Ent : Entity_Id := First_Entity (T);
4806 while Present (Ent) loop
4807 Set_Debug_Info_Needed (Ent);
4812 elsif Is_Array_Type (T) then
4813 Set_Debug_Info_Needed (Component_Type (T));
4816 Indx : Node_Id := First_Index (T);
4818 while Present (Indx) loop
4819 Set_Debug_Info_Needed (Etype (Indx));
4820 Indx := Next_Index (Indx);
4824 if Is_Packed (T) then
4825 Set_Debug_Info_Needed (Packed_Array_Type (T));
4828 elsif Is_Access_Type (T) then
4829 Set_Debug_Info_Needed (Directly_Designated_Type (T));
4831 elsif Is_Private_Type (T) then
4832 Set_Debug_Info_Needed (Full_View (T));
4834 elsif Is_Protected_Type (T) then
4835 Set_Debug_Info_Needed (Corresponding_Record_Type (T));
4838 end Set_Debug_Info_Needed;
4844 procedure Undelay_Type (T : Entity_Id) is
4846 Set_Has_Delayed_Freeze (T, False);
4847 Set_Freeze_Node (T, Empty);
4849 -- Since we don't want T to have a Freeze_Node, we don't want its
4850 -- Full_View or Corresponding_Record_Type to have one either.
4852 -- ??? Fundamentally, this whole handling is a kludge. What we really
4853 -- want is to be sure that for an Itype that's part of record R and is
4854 -- a subtype of type T, that it's frozen after the later of the freeze
4855 -- points of R and T. We have no way of doing that directly, so what we
4856 -- do is force most such Itypes to be frozen as part of freezing R via
4857 -- this procedure and only delay the ones that need to be delayed
4858 -- (mostly the designated types of access types that are defined as
4859 -- part of the record).
4861 if Is_Private_Type (T)
4862 and then Present (Full_View (T))
4863 and then Is_Itype (Full_View (T))
4864 and then Is_Record_Type (Scope (Full_View (T)))
4866 Undelay_Type (Full_View (T));
4869 if Is_Concurrent_Type (T)
4870 and then Present (Corresponding_Record_Type (T))
4871 and then Is_Itype (Corresponding_Record_Type (T))
4872 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
4874 Undelay_Type (Corresponding_Record_Type (T));
4882 procedure Warn_Overlay
4887 Ent : constant Entity_Id := Entity (Nam);
4888 -- The object to which the address clause applies
4891 Old : Entity_Id := Empty;
4895 -- No warning if address clause overlay warnings are off
4897 if not Address_Clause_Overlay_Warnings then
4901 -- No warning if there is an explicit initialization
4903 Init := Original_Node (Expression (Declaration_Node (Ent)));
4905 if Present (Init) and then Comes_From_Source (Init) then
4909 -- We only give the warning for non-imported entities of a type for
4910 -- which a non-null base init proc is defined (or for access types which
4911 -- have implicit null initialization).
4914 and then (Has_Non_Null_Base_Init_Proc (Typ)
4915 or else Is_Access_Type (Typ))
4916 and then not Is_Imported (Ent)
4918 if Nkind (Expr) = N_Attribute_Reference
4919 and then Is_Entity_Name (Prefix (Expr))
4921 Old := Entity (Prefix (Expr));
4923 elsif Is_Entity_Name (Expr)
4924 and then Ekind (Entity (Expr)) = E_Constant
4926 Decl := Declaration_Node (Entity (Expr));
4928 if Nkind (Decl) = N_Object_Declaration
4929 and then Present (Expression (Decl))
4930 and then Nkind (Expression (Decl)) = N_Attribute_Reference
4931 and then Is_Entity_Name (Prefix (Expression (Decl)))
4933 Old := Entity (Prefix (Expression (Decl)));
4935 elsif Nkind (Expr) = N_Function_Call then
4939 -- A function call (most likely to To_Address) is probably not
4940 -- an overlay, so skip warning. Ditto if the function call was
4941 -- inlined and transformed into an entity.
4943 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
4947 Decl := Next (Parent (Expr));
4949 -- If a pragma Import follows, we assume that it is for the current
4950 -- target of the address clause, and skip the warning.
4953 and then Nkind (Decl) = N_Pragma
4954 and then Chars (Decl) = Name_Import
4959 if Present (Old) then
4960 Error_Msg_Node_2 := Old;
4962 ("default initialization of & may modify &?",
4966 ("default initialization of & may modify overlaid storage?",
4970 -- Add friendly warning if initialization comes from a packed array
4973 if Is_Record_Type (Typ) then
4978 Comp := First_Component (Typ);
4980 while Present (Comp) loop
4981 if Nkind (Parent (Comp)) = N_Component_Declaration
4982 and then Present (Expression (Parent (Comp)))
4985 elsif Is_Array_Type (Etype (Comp))
4986 and then Present (Packed_Array_Type (Etype (Comp)))
4989 ("packed array component& will be initialized to zero?",
4993 Next_Component (Comp);
5000 ("use pragma Import for & to " &
5001 "suppress initialization ('R'M B.1(24))?",