1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2002, Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 ------------------------------------------------------------------------------
28 with Atree; use Atree;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Ch7; use Exp_Ch7;
34 with Exp_Ch11; use Exp_Ch11;
35 with Exp_Pakd; use Exp_Pakd;
36 with Exp_Util; use Exp_Util;
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;
44 with Sem_Cat; use Sem_Cat;
45 with Sem_Ch6; use Sem_Ch6;
46 with Sem_Ch7; use Sem_Ch7;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Ch13; use Sem_Ch13;
49 with Sem_Eval; use Sem_Eval;
50 with Sem_Mech; use Sem_Mech;
51 with Sem_Prag; use Sem_Prag;
52 with Sem_Res; use Sem_Res;
53 with Sem_Util; use Sem_Util;
54 with Sinfo; use Sinfo;
55 with Snames; use Snames;
56 with Stand; use Stand;
57 with Targparm; use Targparm;
58 with Tbuild; use Tbuild;
59 with Ttypes; use Ttypes;
60 with Uintp; use Uintp;
61 with Urealp; use Urealp;
63 package body Freeze is
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
70 -- Typ is a type that is being frozen. If no size clause is given,
71 -- but a default Esize has been computed, then this default Esize is
72 -- adjusted up if necessary to be consistent with a given alignment,
73 -- but never to a value greater than Long_Long_Integer'Size. This
74 -- is used for all discrete types and for fixed-point types.
76 procedure Build_And_Analyze_Renamed_Body
79 After : in out Node_Id);
80 -- Build body for a renaming declaration, insert in tree and analyze.
82 procedure Check_Strict_Alignment (E : Entity_Id);
83 -- E is a base type. If E is tagged or has a component that is aliased
84 -- or tagged or contains something this is aliased or tagged, set
87 procedure Check_Unsigned_Type (E : Entity_Id);
88 pragma Inline (Check_Unsigned_Type);
89 -- If E is a fixed-point or discrete type, then all the necessary work
90 -- to freeze it is completed except for possible setting of the flag
91 -- Is_Unsigned_Type, which is done by this procedure. The call has no
92 -- effect if the entity E is not a discrete or fixed-point type.
94 procedure Freeze_And_Append
97 Result : in out List_Id);
98 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
99 -- nodes to Result, modifying Result from No_List if necessary.
101 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
102 -- Freeze enumeration type. The Esize field is set as processing
103 -- proceeds (i.e. set by default when the type is declared and then
104 -- adjusted by rep clauses. What this procedure does is to make sure
105 -- that if a foreign convention is specified, and no specific size
106 -- is given, then the size must be at least Integer'Size.
108 procedure Freeze_Static_Object (E : Entity_Id);
109 -- If an object is frozen which has Is_Statically_Allocated set, then
110 -- all referenced types must also be marked with this flag. This routine
111 -- is in charge of meeting this requirement for the object entity E.
113 procedure Freeze_Subprogram (E : Entity_Id);
114 -- Perform freezing actions for a subprogram (create extra formals,
115 -- and set proper default mechanism values). Note that this routine
116 -- is not called for internal subprograms, for which neither of these
117 -- actions is needed (or desirable, we do not want for example to have
118 -- these extra formals present in initialization procedures, where they
119 -- would serve no purpose). In this call E is either a subprogram or
120 -- a subprogram type (i.e. an access to a subprogram).
122 function Is_Fully_Defined (T : Entity_Id) return Boolean;
123 -- true if T is not private, or has a full view.
125 procedure Process_Default_Expressions
127 After : in out Node_Id);
128 -- This procedure is called for each subprogram to complete processing
129 -- of default expressions at the point where all types are known to be
130 -- frozen. The expressions must be analyzed in full, to make sure that
131 -- all error processing is done (they have only been pre-analyzed). If
132 -- the expression is not an entity or literal, its analysis may generate
133 -- code which must not be executed. In that case we build a function
134 -- body to hold that code. This wrapper function serves no other purpose
135 -- (it used to be called to evaluate the default, but now the default is
136 -- inlined at each point of call).
138 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
139 -- Typ is a record or array type that is being frozen. This routine
140 -- sets the default component alignment from the scope stack values
141 -- if the alignment is otherwise not specified.
143 procedure Check_Debug_Info_Needed (T : Entity_Id);
144 -- As each entity is frozen, this routine is called to deal with the
145 -- setting of Debug_Info_Needed for the entity. This flag is set if
146 -- the entity comes from source, or if we are in Debug_Generated_Code
147 -- mode or if the -gnatdV debug flag is set. However, it never sets
148 -- the flag if Debug_Info_Off is set.
150 procedure Set_Debug_Info_Needed (T : Entity_Id);
151 -- Sets the Debug_Info_Needed flag on entity T if not already set, and
152 -- also on any entities that are needed by T (for an object, the type
153 -- of the object is needed, and for a type, the subsidiary types are
154 -- needed -- see body for details). Never has any effect on T if the
155 -- Debug_Info_Off flag is set.
157 -------------------------------
158 -- Adjust_Esize_For_Alignment --
159 -------------------------------
161 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
165 if Known_Esize (Typ) and then Known_Alignment (Typ) then
166 Align := Alignment_In_Bits (Typ);
168 if Align > Esize (Typ)
169 and then Align <= Standard_Long_Long_Integer_Size
171 Set_Esize (Typ, Align);
174 end Adjust_Esize_For_Alignment;
176 ------------------------------------
177 -- Build_And_Analyze_Renamed_Body --
178 ------------------------------------
180 procedure Build_And_Analyze_Renamed_Body
183 After : in out Node_Id)
185 Body_Node : constant Node_Id := Build_Renamed_Body (Decl, New_S);
188 Insert_After (After, Body_Node);
189 Mark_Rewrite_Insertion (Body_Node);
192 end Build_And_Analyze_Renamed_Body;
194 ------------------------
195 -- Build_Renamed_Body --
196 ------------------------
198 function Build_Renamed_Body
203 Loc : constant Source_Ptr := Sloc (New_S);
204 -- We use for the source location of the renamed body, the location
205 -- of the spec entity. It might seem more natural to use the location
206 -- of the renaming declaration itself, but that would be wrong, since
207 -- then the body we create would look as though it was created far
208 -- too late, and this could cause problems with elaboration order
209 -- analysis, particularly in connection with instantiations.
211 N : constant Node_Id := Unit_Declaration_Node (New_S);
212 Nam : constant Node_Id := Name (N);
214 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
215 Actuals : List_Id := No_List;
220 O_Formal : Entity_Id;
221 Param_Spec : Node_Id;
224 -- Determine the entity being renamed, which is the target of the
225 -- call statement. If the name is an explicit dereference, this is
226 -- a renaming of a subprogram type rather than a subprogram. The
227 -- name itself is fully analyzed.
229 if Nkind (Nam) = N_Selected_Component then
230 Old_S := Entity (Selector_Name (Nam));
232 elsif Nkind (Nam) = N_Explicit_Dereference then
233 Old_S := Etype (Nam);
235 elsif Nkind (Nam) = N_Indexed_Component then
237 if Is_Entity_Name (Prefix (Nam)) then
238 Old_S := Entity (Prefix (Nam));
240 Old_S := Entity (Selector_Name (Prefix (Nam)));
243 elsif Nkind (Nam) = N_Character_Literal then
244 Old_S := Etype (New_S);
247 Old_S := Entity (Nam);
250 if Is_Entity_Name (Nam) then
252 -- If the renamed entity is a predefined operator, retain full
253 -- name to ensure its visibility.
255 if Ekind (Old_S) = E_Operator
256 and then Nkind (Nam) = N_Expanded_Name
258 Call_Name := New_Copy (Name (N));
260 Call_Name := New_Reference_To (Old_S, Loc);
264 Call_Name := New_Copy (Name (N));
266 -- The original name may have been overloaded, but
267 -- is fully resolved now.
269 Set_Is_Overloaded (Call_Name, False);
272 -- For simple renamings, subsequent calls can be expanded directly
273 -- as called to the renamed entity. The body must be generated in
274 -- any case for calls they may appear elsewhere.
276 if (Ekind (Old_S) = E_Function
277 or else Ekind (Old_S) = E_Procedure)
278 and then Nkind (Decl) = N_Subprogram_Declaration
280 Set_Body_To_Inline (Decl, Old_S);
283 -- The body generated for this renaming is an internal artifact, and
284 -- does not constitute a freeze point for the called entity.
286 Set_Must_Not_Freeze (Call_Name);
288 Formal := First_Formal (Defining_Entity (Decl));
290 if Present (Formal) then
293 while Present (Formal) loop
294 Append (New_Reference_To (Formal, Loc), Actuals);
295 Next_Formal (Formal);
299 -- If the renamed entity is an entry, inherit its profile. For
300 -- other renamings as bodies, both profiles must be subtype
301 -- conformant, so it is not necessary to replace the profile given
302 -- in the declaration. However, default values that are aggregates
303 -- are rewritten when partially analyzed, so we recover the original
304 -- aggregate to insure that subsequent conformity checking works.
305 -- Similarly, if the default expression was constant-folded, recover
306 -- the original expression.
308 Formal := First_Formal (Defining_Entity (Decl));
310 if Present (Formal) then
311 O_Formal := First_Formal (Old_S);
312 Param_Spec := First (Parameter_Specifications (Spec));
314 while Present (Formal) loop
315 if Is_Entry (Old_S) then
317 if Nkind (Parameter_Type (Param_Spec)) /=
320 Set_Etype (Formal, Etype (O_Formal));
321 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
324 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
325 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
326 Nkind (Default_Value (O_Formal))
328 Set_Expression (Param_Spec,
329 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
332 Next_Formal (Formal);
333 Next_Formal (O_Formal);
338 -- If the renamed entity is a function, the generated body contains a
339 -- return statement. Otherwise, build a procedure call. If the entity is
340 -- an entry, subsequent analysis of the call will transform it into the
341 -- proper entry or protected operation call. If the renamed entity is
342 -- a character literal, return it directly.
344 if Ekind (Old_S) = E_Function
345 or else Ekind (Old_S) = E_Operator
346 or else (Ekind (Old_S) = E_Subprogram_Type
347 and then Etype (Old_S) /= Standard_Void_Type)
350 Make_Return_Statement (Loc,
352 Make_Function_Call (Loc,
354 Parameter_Associations => Actuals));
356 elsif Ekind (Old_S) = E_Enumeration_Literal then
358 Make_Return_Statement (Loc,
359 Expression => New_Occurrence_Of (Old_S, Loc));
361 elsif Nkind (Nam) = N_Character_Literal then
363 Make_Return_Statement (Loc,
364 Expression => Call_Name);
368 Make_Procedure_Call_Statement (Loc,
370 Parameter_Associations => Actuals);
373 -- Create entities for subprogram body and formals.
375 Set_Defining_Unit_Name (Spec,
376 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
378 Param_Spec := First (Parameter_Specifications (Spec));
380 while Present (Param_Spec) loop
381 Set_Defining_Identifier (Param_Spec,
382 Make_Defining_Identifier (Loc,
383 Chars => Chars (Defining_Identifier (Param_Spec))));
388 Make_Subprogram_Body (Loc,
389 Specification => Spec,
390 Declarations => New_List,
391 Handled_Statement_Sequence =>
392 Make_Handled_Sequence_Of_Statements (Loc,
393 Statements => New_List (Call_Node)));
395 if Nkind (Decl) /= N_Subprogram_Declaration then
397 Make_Subprogram_Declaration (Loc,
398 Specification => Specification (N)));
401 -- Link the body to the entity whose declaration it completes. If
402 -- the body is analyzed when the renamed entity is frozen, it may be
403 -- necessary to restore the proper scope (see package Exp_Ch13).
405 if Nkind (N) = N_Subprogram_Renaming_Declaration
406 and then Present (Corresponding_Spec (N))
408 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
410 Set_Corresponding_Spec (Body_Node, New_S);
414 end Build_Renamed_Body;
416 -----------------------------
417 -- Check_Compile_Time_Size --
418 -----------------------------
420 procedure Check_Compile_Time_Size (T : Entity_Id) is
422 procedure Set_Small_Size (S : Uint);
423 -- Sets the compile time known size (32 bits or less) in the Esize
424 -- field, checking for a size clause that was given which attempts
425 -- to give a smaller size.
427 function Size_Known (T : Entity_Id) return Boolean;
428 -- Recursive function that does all the work
430 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
431 -- If T is a constrained subtype, its size is not known if any of its
432 -- discriminant constraints is not static and it is not a null record.
433 -- The test is conservative and doesn't check that the components are
434 -- in fact constrained by non-static discriminant values. Could be made
441 procedure Set_Small_Size (S : Uint) is
446 elsif Has_Size_Clause (T) then
447 if RM_Size (T) < S then
448 Error_Msg_Uint_1 := S;
450 ("size for & is too small, minimum is ^",
453 elsif Unknown_Esize (T) then
457 -- Set sizes if not set already
460 if Unknown_Esize (T) then
464 if Unknown_RM_Size (T) then
474 function Size_Known (T : Entity_Id) return Boolean is
482 if Size_Known_At_Compile_Time (T) then
485 elsif Is_Scalar_Type (T)
486 or else Is_Task_Type (T)
488 return not Is_Generic_Type (T);
490 elsif Is_Array_Type (T) then
492 if Ekind (T) = E_String_Literal_Subtype then
493 Set_Small_Size (Component_Size (T) * String_Literal_Length (T));
496 elsif not Is_Constrained (T) then
499 -- Don't do any recursion on type with error posted, since
500 -- we may have a malformed type that leads us into a loop
502 elsif Error_Posted (T) then
505 elsif not Size_Known (Component_Type (T)) then
509 -- Check for all indexes static, and also compute possible
510 -- size (in case it is less than 32 and may be packable).
513 Esiz : Uint := Component_Size (T);
517 Index := First_Index (T);
519 while Present (Index) loop
520 if Nkind (Index) = N_Range then
521 Get_Index_Bounds (Index, Low, High);
523 elsif Error_Posted (Scalar_Range (Etype (Index))) then
527 Low := Type_Low_Bound (Etype (Index));
528 High := Type_High_Bound (Etype (Index));
531 if not Compile_Time_Known_Value (Low)
532 or else not Compile_Time_Known_Value (High)
533 or else Etype (Index) = Any_Type
538 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
550 Set_Small_Size (Esiz);
554 elsif Is_Access_Type (T) then
557 elsif Is_Private_Type (T)
558 and then not Is_Generic_Type (T)
559 and then Present (Underlying_Type (T))
561 -- Don't do any recursion on type with error posted, since
562 -- we may have a malformed type that leads us into a loop
564 if Error_Posted (T) then
567 return Size_Known (Underlying_Type (T));
570 elsif Is_Record_Type (T) then
571 if Is_Class_Wide_Type (T) then
574 elsif T /= Base_Type (T) then
575 return Size_Known_At_Compile_Time (Base_Type (T))
576 and then Static_Discriminated_Components (T);
578 -- Don't do any recursion on type with error posted, since
579 -- we may have a malformed type that leads us into a loop
581 elsif Error_Posted (T) then
586 Packed_Size_Known : Boolean := Is_Packed (T);
587 Packed_Size : Uint := Uint_0;
590 -- Test for variant part present
592 if Has_Discriminants (T)
593 and then Present (Parent (T))
594 and then Nkind (Parent (T)) = N_Full_Type_Declaration
595 and then Nkind (Type_Definition (Parent (T))) =
597 and then not Null_Present (Type_Definition (Parent (T)))
598 and then Present (Variant_Part
599 (Component_List (Type_Definition (Parent (T)))))
601 -- If variant part is present, and type is unconstrained,
602 -- then we must have defaulted discriminants, or a size
603 -- clause must be present for the type, or else the size
604 -- is definitely not known at compile time.
606 if not Is_Constrained (T)
608 No (Discriminant_Default_Value
609 (First_Discriminant (T)))
610 and then Unknown_Esize (T)
614 -- We do not know the packed size, it is too much
615 -- trouble to figure it out.
617 Packed_Size_Known := False;
621 Comp := First_Entity (T);
623 while Present (Comp) loop
624 if Ekind (Comp) = E_Component
626 Ekind (Comp) = E_Discriminant
628 Ctyp := Etype (Comp);
630 if Present (Component_Clause (Comp)) then
631 Packed_Size_Known := False;
634 if not Size_Known (Ctyp) then
637 elsif Packed_Size_Known then
639 -- If RM_Size is known and static, then we can
640 -- keep accumulating the packed size.
642 if Known_Static_RM_Size (Ctyp) then
644 -- A little glitch, to be removed sometime ???
645 -- gigi does not understand zero sizes yet.
647 if RM_Size (Ctyp) = Uint_0 then
648 Packed_Size_Known := False;
652 Packed_Size + RM_Size (Ctyp);
654 -- If we have a field whose RM_Size is not known
655 -- then we can't figure out the packed size here.
658 Packed_Size_Known := False;
666 if Packed_Size_Known then
667 Set_Small_Size (Packed_Size);
679 -------------------------------------
680 -- Static_Discriminated_Components --
681 -------------------------------------
683 function Static_Discriminated_Components
687 Constraint : Elmt_Id;
690 if Has_Discriminants (T)
691 and then Present (Discriminant_Constraint (T))
692 and then Present (First_Component (T))
694 Constraint := First_Elmt (Discriminant_Constraint (T));
696 while Present (Constraint) loop
697 if not Compile_Time_Known_Value (Node (Constraint)) then
701 Next_Elmt (Constraint);
706 end Static_Discriminated_Components;
708 -- Start of processing for Check_Compile_Time_Size
711 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
712 end Check_Compile_Time_Size;
714 -----------------------------
715 -- Check_Debug_Info_Needed --
716 -----------------------------
718 procedure Check_Debug_Info_Needed (T : Entity_Id) is
720 if Needs_Debug_Info (T) or else Debug_Info_Off (T) then
723 elsif Comes_From_Source (T)
724 or else Debug_Generated_Code
725 or else Debug_Flag_VV
727 Set_Debug_Info_Needed (T);
729 end Check_Debug_Info_Needed;
731 ----------------------------
732 -- Check_Strict_Alignment --
733 ----------------------------
735 procedure Check_Strict_Alignment (E : Entity_Id) is
739 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
740 Set_Strict_Alignment (E);
742 elsif Is_Array_Type (E) then
743 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
745 elsif Is_Record_Type (E) then
746 if Is_Limited_Record (E) then
747 Set_Strict_Alignment (E);
751 Comp := First_Component (E);
753 while Present (Comp) loop
754 if not Is_Type (Comp)
755 and then (Strict_Alignment (Etype (Comp))
756 or else Is_Aliased (Comp))
758 Set_Strict_Alignment (E);
762 Next_Component (Comp);
765 end Check_Strict_Alignment;
767 -------------------------
768 -- Check_Unsigned_Type --
769 -------------------------
771 procedure Check_Unsigned_Type (E : Entity_Id) is
772 Ancestor : Entity_Id;
777 if not Is_Discrete_Or_Fixed_Point_Type (E) then
781 -- Do not attempt to analyze case where range was in error
783 if Error_Posted (Scalar_Range (E)) then
787 -- The situation that is non trivial is something like
789 -- subtype x1 is integer range -10 .. +10;
790 -- subtype x2 is x1 range 0 .. V1;
791 -- subtype x3 is x2 range V2 .. V3;
792 -- subtype x4 is x3 range V4 .. V5;
794 -- where Vn are variables. Here the base type is signed, but we still
795 -- know that x4 is unsigned because of the lower bound of x2.
797 -- The only way to deal with this is to look up the ancestor chain
801 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
805 Lo_Bound := Type_Low_Bound (Ancestor);
807 if Compile_Time_Known_Value (Lo_Bound) then
809 if Expr_Rep_Value (Lo_Bound) >= 0 then
810 Set_Is_Unsigned_Type (E, True);
816 Ancestor := Ancestor_Subtype (Ancestor);
818 -- If no ancestor had a static lower bound, go to base type
820 if No (Ancestor) then
822 -- Note: the reason we still check for a compile time known
823 -- value for the base type is that at least in the case of
824 -- generic formals, we can have bounds that fail this test,
825 -- and there may be other cases in error situations.
827 Btyp := Base_Type (E);
829 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
833 Lo_Bound := Type_Low_Bound (Base_Type (E));
835 if Compile_Time_Known_Value (Lo_Bound)
836 and then Expr_Rep_Value (Lo_Bound) >= 0
838 Set_Is_Unsigned_Type (E, True);
846 end Check_Unsigned_Type;
852 -- Note: the easy coding for this procedure would be to just build a
853 -- single list of freeze nodes and then insert them and analyze them
854 -- all at once. This won't work, because the analysis of earlier freeze
855 -- nodes may recursively freeze types which would otherwise appear later
856 -- on in the freeze list. So we must analyze and expand the freeze nodes
857 -- as they are generated.
859 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
860 Loc : constant Source_Ptr := Sloc (After);
864 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
865 -- This is the internal recursive routine that does freezing of
866 -- entities (but NOT the analysis of default expressions, which
867 -- should not be recursive, we don't want to analyze those till
868 -- we are sure that ALL the types are frozen).
870 procedure Freeze_All_Ent
872 After : in out Node_Id)
878 procedure Process_Flist;
879 -- If freeze nodes are present, insert and analyze, and reset
880 -- cursor for next insertion.
882 procedure Process_Flist is
884 if Is_Non_Empty_List (Flist) then
885 Lastn := Next (After);
886 Insert_List_After_And_Analyze (After, Flist);
888 if Present (Lastn) then
889 After := Prev (Lastn);
891 After := Last (List_Containing (After));
898 while Present (E) loop
900 -- If the entity is an inner package which is not a package
901 -- renaming, then its entities must be frozen at this point.
902 -- Note that such entities do NOT get frozen at the end of
903 -- the nested package itself (only library packages freeze).
905 -- Same is true for task declarations, where anonymous records
906 -- created for entry parameters must be frozen.
908 if Ekind (E) = E_Package
909 and then No (Renamed_Object (E))
910 and then not Is_Child_Unit (E)
911 and then not Is_Frozen (E)
914 Install_Visible_Declarations (E);
915 Install_Private_Declarations (E);
917 Freeze_All (First_Entity (E), After);
919 End_Package_Scope (E);
921 elsif Ekind (E) in Task_Kind
923 (Nkind (Parent (E)) = N_Task_Type_Declaration
925 Nkind (Parent (E)) = N_Single_Task_Declaration)
928 Freeze_All (First_Entity (E), After);
931 -- For a derived tagged type, we must ensure that all the
932 -- primitive operations of the parent have been frozen, so
933 -- that their addresses will be in the parent's dispatch table
934 -- at the point it is inherited.
936 elsif Ekind (E) = E_Record_Type
937 and then Is_Tagged_Type (E)
938 and then Is_Tagged_Type (Etype (E))
939 and then Is_Derived_Type (E)
942 Prim_List : constant Elist_Id :=
943 Primitive_Operations (Etype (E));
948 Prim := First_Elmt (Prim_List);
950 while Present (Prim) loop
953 if Comes_From_Source (Subp)
954 and then not Is_Frozen (Subp)
956 Flist := Freeze_Entity (Subp, Loc);
965 if not Is_Frozen (E) then
966 Flist := Freeze_Entity (E, Loc);
974 -- Start of processing for Freeze_All
977 Freeze_All_Ent (From, After);
979 -- Now that all types are frozen, we can deal with default expressions
980 -- that require us to build a default expression functions. This is the
981 -- point at which such functions are constructed (after all types that
982 -- might be used in such expressions have been frozen).
983 -- We also add finalization chains to access types whose designated
984 -- types are controlled. This is normally done when freezing the type,
985 -- but this misses recursive type definitions where the later members
986 -- of the recursion introduce controlled components (e.g. 5624-001).
988 -- Loop through entities
991 while Present (E) loop
993 if Is_Subprogram (E) then
995 if not Default_Expressions_Processed (E) then
996 Process_Default_Expressions (E, After);
999 if not Has_Completion (E) then
1000 Decl := Unit_Declaration_Node (E);
1002 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1003 Build_And_Analyze_Renamed_Body (Decl, E, After);
1005 elsif Nkind (Decl) = N_Subprogram_Declaration
1006 and then Present (Corresponding_Body (Decl))
1008 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1009 = N_Subprogram_Renaming_Declaration
1011 Build_And_Analyze_Renamed_Body
1012 (Decl, Corresponding_Body (Decl), After);
1016 elsif Ekind (E) in Task_Kind
1018 (Nkind (Parent (E)) = N_Task_Type_Declaration
1020 Nkind (Parent (E)) = N_Single_Task_Declaration)
1026 Ent := First_Entity (E);
1028 while Present (Ent) loop
1031 and then not Default_Expressions_Processed (Ent)
1033 Process_Default_Expressions (Ent, After);
1040 elsif Is_Access_Type (E)
1041 and then Comes_From_Source (E)
1042 and then Ekind (Directly_Designated_Type (E)) = E_Incomplete_Type
1043 and then Controlled_Type (Designated_Type (E))
1044 and then No (Associated_Final_Chain (E))
1046 Build_Final_List (Parent (E), E);
1054 -----------------------
1055 -- Freeze_And_Append --
1056 -----------------------
1058 procedure Freeze_And_Append
1061 Result : in out List_Id)
1063 L : constant List_Id := Freeze_Entity (Ent, Loc);
1066 if Is_Non_Empty_List (L) then
1067 if Result = No_List then
1070 Append_List (L, Result);
1073 end Freeze_And_Append;
1079 procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
1080 Freeze_Nodes : constant List_Id := Freeze_Entity (T, Sloc (N));
1084 if Is_Non_Empty_List (Freeze_Nodes) then
1085 F := First (Freeze_Nodes);
1088 Insert_Actions (N, Freeze_Nodes);
1097 function Freeze_Entity (E : Entity_Id; Loc : Source_Ptr) return List_Id is
1105 procedure Check_Current_Instance (Comp_Decl : Node_Id);
1106 -- Check that an Access or Unchecked_Access attribute with
1107 -- a prefix which is the current instance type can only be
1108 -- applied when the type is limited.
1110 function After_Last_Declaration return Boolean;
1111 -- If Loc is a freeze_entity that appears after the last declaration
1112 -- in the scope, inhibit error messages on late completion.
1114 procedure Freeze_Record_Type (Rec : Entity_Id);
1115 -- Freeze each component, handle some representation clauses, and
1116 -- freeze primitive operations if this is a tagged type.
1118 ----------------------------
1119 -- After_Last_Declaration --
1120 ----------------------------
1122 function After_Last_Declaration return Boolean is
1123 Spec : Node_Id := Parent (Current_Scope);
1126 if Nkind (Spec) = N_Package_Specification then
1127 if Present (Private_Declarations (Spec)) then
1128 return Loc >= Sloc (Last (Private_Declarations (Spec)));
1130 elsif Present (Visible_Declarations (Spec)) then
1131 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
1139 end After_Last_Declaration;
1141 ----------------------------
1142 -- Check_Current_Instance --
1143 ----------------------------
1145 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
1147 function Process (N : Node_Id) return Traverse_Result;
1148 -- Process routine to apply check to given node.
1150 function Process (N : Node_Id) return Traverse_Result is
1153 when N_Attribute_Reference =>
1154 if (Attribute_Name (N) = Name_Access
1156 Attribute_Name (N) = Name_Unchecked_Access)
1157 and then Is_Entity_Name (Prefix (N))
1158 and then Is_Type (Entity (Prefix (N)))
1159 and then Entity (Prefix (N)) = E
1162 ("current instance must be a limited type", Prefix (N));
1168 when others => return OK;
1172 procedure Traverse is new Traverse_Proc (Process);
1174 -- Start of processing for Check_Current_Instance
1177 Traverse (Comp_Decl);
1178 end Check_Current_Instance;
1180 ------------------------
1181 -- Freeze_Record_Type --
1182 ------------------------
1184 procedure Freeze_Record_Type (Rec : Entity_Id) is
1189 Unplaced_Component : Boolean := False;
1190 -- Set True if we find at least one component with no component
1191 -- clause (used to warn about useless Pack pragmas).
1193 Placed_Component : Boolean := False;
1194 -- Set True if we find at least one component with a component
1195 -- clause (used to warn about useless Bit_Order pragmas).
1198 -- Freeze components and embedded subtypes
1200 Comp := First_Entity (Rec);
1202 while Present (Comp) loop
1204 if not Is_Type (Comp) then
1205 Freeze_And_Append (Etype (Comp), Loc, Result);
1208 -- If the component is an access type with an allocator
1209 -- as default value, the designated type will be frozen
1210 -- by the corresponding expression in init_proc. In order
1211 -- to place the freeze node for the designated type before
1212 -- that for the current record type, freeze it now.
1214 -- Same process if the component is an array of access types,
1215 -- initialized with an aggregate. If the designated type is
1216 -- private, it cannot contain allocators, and it is premature
1217 -- to freeze the type, so we check for this as well.
1219 if Is_Access_Type (Etype (Comp))
1220 and then Present (Parent (Comp))
1221 and then Present (Expression (Parent (Comp)))
1222 and then Nkind (Expression (Parent (Comp))) = N_Allocator
1225 Alloc : constant Node_Id := Expression (Parent (Comp));
1228 -- If component is pointer to a classwide type, freeze
1229 -- the specific type in the expression being allocated.
1230 -- The expression may be a subtype indication, in which
1231 -- case freeze the subtype mark.
1233 if Is_Class_Wide_Type (Designated_Type (Etype (Comp))) then
1235 if Is_Entity_Name (Expression (Alloc)) then
1237 (Entity (Expression (Alloc)), Loc, Result);
1239 Nkind (Expression (Alloc)) = N_Subtype_Indication
1242 (Entity (Subtype_Mark (Expression (Alloc))),
1247 (Designated_Type (Etype (Comp)), Loc, Result);
1251 -- If this is a constrained subtype of an already frozen type,
1252 -- make the subtype frozen as well. It might otherwise be frozen
1253 -- in the wrong scope, and a freeze node on subtype has no effect.
1255 elsif Is_Access_Type (Etype (Comp))
1256 and then not Is_Frozen (Designated_Type (Etype (Comp)))
1257 and then Is_Itype (Designated_Type (Etype (Comp)))
1258 and then Is_Frozen (Base_Type (Designated_Type (Etype (Comp))))
1260 Set_Is_Frozen (Designated_Type (Etype (Comp)));
1262 elsif Is_Array_Type (Etype (Comp))
1263 and then Is_Access_Type (Component_Type (Etype (Comp)))
1264 and then Present (Parent (Comp))
1265 and then Nkind (Parent (Comp)) = N_Component_Declaration
1266 and then Present (Expression (Parent (Comp)))
1267 and then Nkind (Expression (Parent (Comp))) = N_Aggregate
1268 and then Is_Fully_Defined
1269 (Designated_Type (Component_Type (Etype (Comp))))
1273 (Component_Type (Etype (Comp))), Loc, Result);
1276 -- Processing for real components (exclude anonymous subtypes)
1278 if Ekind (Comp) = E_Component
1279 or else Ekind (Comp) = E_Discriminant
1281 -- Check for error of component clause given for variable
1282 -- sized type. We have to delay this test till this point,
1283 -- since the component type has to be frozen for us to know
1284 -- if it is variable length. We omit this test in a generic
1285 -- context, it will be applied at instantiation time.
1288 CC : constant Node_Id := Component_Clause (Comp);
1291 if Present (CC) then
1292 Placed_Component := True;
1294 if Inside_A_Generic then
1297 elsif not Size_Known_At_Compile_Time
1298 (Underlying_Type (Etype (Comp)))
1301 ("component clause not allowed for variable " &
1302 "length component", CC);
1306 Unplaced_Component := True;
1310 -- If component clause is present, then deal with the
1311 -- non-default bit order case. We cannot do this before
1312 -- the freeze point, because there is no required order
1313 -- for the component clause and the bit_order clause.
1315 -- We only do this processing for the base type, and in
1316 -- fact that's important, since otherwise if there are
1317 -- record subtypes, we could reverse the bits once for
1318 -- each subtype, which would be incorrect.
1320 if Present (Component_Clause (Comp))
1321 and then Reverse_Bit_Order (Rec)
1322 and then Ekind (E) = E_Record_Type
1325 CFB : constant Uint := Component_Bit_Offset (Comp);
1326 CSZ : constant Uint := Esize (Comp);
1327 CLC : constant Node_Id := Component_Clause (Comp);
1328 Pos : constant Node_Id := Position (CLC);
1329 FB : constant Node_Id := First_Bit (CLC);
1331 Storage_Unit_Offset : constant Uint :=
1332 CFB / System_Storage_Unit;
1334 Start_Bit : constant Uint :=
1335 CFB mod System_Storage_Unit;
1338 -- Cases where field goes over storage unit boundary
1340 if Start_Bit + CSZ > System_Storage_Unit then
1342 -- Allow multi-byte field but generate warning
1344 if Start_Bit mod System_Storage_Unit = 0
1345 and then CSZ mod System_Storage_Unit = 0
1348 ("multi-byte field specified with non-standard"
1349 & " Bit_Order?", CLC);
1351 if Bytes_Big_Endian then
1353 ("bytes are not reversed "
1354 & "(component is big-endian)?", CLC);
1357 ("bytes are not reversed "
1358 & "(component is little-endian)?", CLC);
1361 -- Do not allow non-contiguous field
1365 ("attempt to specify non-contiguous field"
1366 & " not permitted", CLC);
1368 ("\(caused by non-standard Bit_Order "
1369 & "specified)", CLC);
1372 -- Case where field fits in one storage unit
1375 -- Give warning if suspicious component clause
1377 if Intval (FB) >= System_Storage_Unit then
1379 ("?Bit_Order clause does not affect " &
1380 "byte ordering", Pos);
1382 Intval (Pos) + Intval (FB) / System_Storage_Unit;
1384 ("?position normalized to ^ before bit " &
1385 "order interpreted", Pos);
1388 -- Here is where we fix up the Component_Bit_Offset
1389 -- value to account for the reverse bit order.
1390 -- Some examples of what needs to be done are:
1392 -- First_Bit .. Last_Bit Component_Bit_Offset
1395 -- 0 .. 0 7 .. 7 0 7
1396 -- 0 .. 1 6 .. 7 0 6
1397 -- 0 .. 2 5 .. 7 0 5
1398 -- 0 .. 7 0 .. 7 0 4
1400 -- 1 .. 1 6 .. 6 1 6
1401 -- 1 .. 4 3 .. 6 1 3
1402 -- 4 .. 7 0 .. 3 4 0
1404 -- The general rule is that the first bit is
1405 -- is obtained by subtracting the old ending bit
1406 -- from storage_unit - 1.
1408 Set_Component_Bit_Offset (Comp,
1409 (Storage_Unit_Offset * System_Storage_Unit)
1410 + (System_Storage_Unit - 1)
1411 - (Start_Bit + CSZ - 1));
1413 Set_Normalized_First_Bit (Comp,
1414 Component_Bit_Offset (Comp) mod System_Storage_Unit);
1423 -- Check for useless pragma Bit_Order
1425 if not Placed_Component and then Reverse_Bit_Order (Rec) then
1426 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
1427 Error_Msg_N ("?Bit_Order specification has no effect", ADC);
1428 Error_Msg_N ("\?since no component clauses were specified", ADC);
1431 -- Check for useless pragma Pack when all components placed
1434 and then not Unplaced_Component
1435 and then Warn_On_Redundant_Constructs
1438 ("?pragma Pack has no effect, no unplaced components",
1439 Get_Rep_Pragma (Rec, Name_Pack));
1440 Set_Is_Packed (Rec, False);
1443 -- If this is the record corresponding to a remote type,
1444 -- freeze the remote type here since that is what we are
1445 -- semantically freeing. This prevents having the freeze node
1446 -- for that type in an inner scope.
1448 -- Also, Check for controlled components and unchecked unions.
1449 -- Finally, enforce the restriction that access attributes with
1450 -- a current instance prefix can only apply to limited types.
1452 if Ekind (Rec) = E_Record_Type then
1454 if Present (Corresponding_Remote_Type (Rec)) then
1456 (Corresponding_Remote_Type (Rec), Loc, Result);
1459 Comp := First_Component (Rec);
1461 while Present (Comp) loop
1462 if Has_Controlled_Component (Etype (Comp))
1463 or else (Chars (Comp) /= Name_uParent
1464 and then Is_Controlled (Etype (Comp)))
1465 or else (Is_Protected_Type (Etype (Comp))
1467 (Corresponding_Record_Type (Etype (Comp)))
1468 and then Has_Controlled_Component
1469 (Corresponding_Record_Type (Etype (Comp))))
1471 Set_Has_Controlled_Component (Rec);
1475 if Has_Unchecked_Union (Etype (Comp)) then
1476 Set_Has_Unchecked_Union (Rec);
1479 if Has_Per_Object_Constraint (Comp)
1480 and then not Is_Limited_Type (Rec)
1482 -- Scan component declaration for likely misuses of
1483 -- current instance, either in a constraint or in a
1484 -- default expression.
1486 Check_Current_Instance (Parent (Comp));
1489 Next_Component (Comp);
1493 Set_Component_Alignment_If_Not_Set (Rec);
1495 -- For first subtypes, check if there are any fixed-point
1496 -- fields with component clauses, where we must check the size.
1497 -- This is not done till the freeze point, since for fixed-point
1498 -- types, we do not know the size until the type is frozen.
1500 if Is_First_Subtype (Rec) then
1501 Comp := First_Component (Rec);
1503 while Present (Comp) loop
1504 if Present (Component_Clause (Comp))
1505 and then Is_Fixed_Point_Type (Etype (Comp))
1508 (Component_Clause (Comp),
1514 Next_Component (Comp);
1517 end Freeze_Record_Type;
1519 -- Start of processing for Freeze_Entity
1522 -- Do not freeze if already frozen since we only need one freeze node.
1524 if Is_Frozen (E) then
1527 -- It is improper to freeze an external entity within a generic
1528 -- because its freeze node will appear in a non-valid context.
1529 -- ??? We should probably freeze the entity at that point and insert
1530 -- the freeze node in a proper place but this proper place is not
1531 -- easy to find, and the proper scope is not easy to restore. For
1532 -- now, just wait to get out of the generic to freeze ???
1534 elsif Inside_A_Generic and then External_Ref_In_Generic (E) then
1537 -- Do not freeze a global entity within an inner scope created during
1538 -- expansion. A call to subprogram E within some internal procedure
1539 -- (a stream attribute for example) might require freezing E, but the
1540 -- freeze node must appear in the same declarative part as E itself.
1541 -- The two-pass elaboration mechanism in gigi guarantees that E will
1542 -- be frozen before the inner call is elaborated. We exclude constants
1543 -- from this test, because deferred constants may be frozen early, and
1544 -- must be diagnosed (see e.g. 1522-005). If the enclosing subprogram
1545 -- comes from source, or is a generic instance, then the freeze point
1546 -- is the one mandated by the language. and we freze the entity.
1548 elsif In_Open_Scopes (Scope (E))
1549 and then Scope (E) /= Current_Scope
1550 and then Ekind (E) /= E_Constant
1553 S : Entity_Id := Current_Scope;
1556 while Present (S) loop
1557 if Is_Overloadable (S) then
1558 if Comes_From_Source (S)
1559 or else Is_Generic_Instance (S)
1572 -- Here to freeze the entity
1577 -- Case of entity being frozen is other than a type
1579 if not Is_Type (E) then
1581 -- If entity is exported or imported and does not have an external
1582 -- name, now is the time to provide the appropriate default name.
1583 -- Skip this if the entity is stubbed, since we don't need a name
1584 -- for any stubbed routine.
1586 if (Is_Imported (E) or else Is_Exported (E))
1587 and then No (Interface_Name (E))
1588 and then Convention (E) /= Convention_Stubbed
1590 Set_Encoded_Interface_Name
1591 (E, Get_Default_External_Name (E));
1594 -- For a subprogram, freeze all parameter types and also the return
1595 -- type (RM 13.14(13)). However skip this for internal subprograms.
1596 -- This is also the point where any extra formal parameters are
1597 -- created since we now know whether the subprogram will use
1598 -- a foreign convention.
1600 if Is_Subprogram (E) then
1602 if not Is_Internal (E) then
1607 function Is_Fat_C_Ptr_Type (T : Entity_Id) return Boolean;
1608 -- Determines if given type entity is a fat pointer type
1609 -- used as an argument type or return type to a subprogram
1610 -- with C or C++ convention set.
1612 --------------------------
1613 -- Is_Fat_C_Access_Type --
1614 --------------------------
1616 function Is_Fat_C_Ptr_Type (T : Entity_Id) return Boolean is
1618 return (Convention (E) = Convention_C
1620 Convention (E) = Convention_CPP)
1621 and then Is_Access_Type (T)
1622 and then Esize (T) > Ttypes.System_Address_Size;
1623 end Is_Fat_C_Ptr_Type;
1626 -- Loop through formals
1628 Formal := First_Formal (E);
1630 while Present (Formal) loop
1632 F_Type := Etype (Formal);
1633 Freeze_And_Append (F_Type, Loc, Result);
1635 if Is_Private_Type (F_Type)
1636 and then Is_Private_Type (Base_Type (F_Type))
1637 and then No (Full_View (Base_Type (F_Type)))
1638 and then not Is_Generic_Type (F_Type)
1639 and then not Is_Derived_Type (F_Type)
1641 -- If the type of a formal is incomplete, subprogram
1642 -- is being frozen prematurely. Within an instance
1643 -- (but not within a wrapper package) this is an
1644 -- an artifact of our need to regard the end of an
1645 -- instantiation as a freeze point. Otherwise it is
1646 -- a definite error.
1647 -- and then not Is_Wrapper_Package (Current_Scope) ???
1650 Set_Is_Frozen (E, False);
1653 elsif not After_Last_Declaration then
1654 Error_Msg_Node_1 := F_Type;
1656 ("type& must be fully defined before this point",
1661 -- Check bad use of fat C pointer
1663 if Is_Fat_C_Ptr_Type (F_Type) then
1664 Error_Msg_Qual_Level := 1;
1666 ("?type of & does not correspond to C pointer",
1668 Error_Msg_Qual_Level := 0;
1671 -- Check for unconstrained array in exported foreign
1674 if Convention (E) in Foreign_Convention
1675 and then not Is_Imported (E)
1676 and then Is_Array_Type (F_Type)
1677 and then not Is_Constrained (F_Type)
1679 Error_Msg_Qual_Level := 1;
1681 ("?type of argument& is unconstrained array",
1684 ("?foreign caller must pass bounds explicitly",
1686 Error_Msg_Qual_Level := 0;
1689 Next_Formal (Formal);
1692 -- Check return type
1694 if Ekind (E) = E_Function then
1695 Freeze_And_Append (Etype (E), Loc, Result);
1697 if Is_Fat_C_Ptr_Type (Etype (E)) then
1699 ("?return type of& does not correspond to C pointer",
1702 elsif Is_Array_Type (Etype (E))
1703 and then not Is_Constrained (Etype (E))
1704 and then not Is_Imported (E)
1705 and then Convention (E) in Foreign_Convention
1708 ("foreign convention function may not " &
1709 "return unconstrained array", E);
1715 -- Must freeze its parent first if it is a derived subprogram
1717 if Present (Alias (E)) then
1718 Freeze_And_Append (Alias (E), Loc, Result);
1721 -- If the return type requires a transient scope, and we are on
1722 -- a target allowing functions to return with a depressed stack
1723 -- pointer, then we mark the function as requiring this treatment.
1725 if Ekind (E) = E_Function
1726 and then Functions_Return_By_DSP_On_Target
1727 and then Requires_Transient_Scope (Etype (E))
1729 Set_Function_Returns_With_DSP (E);
1732 if not Is_Internal (E) then
1733 Freeze_Subprogram (E);
1736 -- Here for other than a subprogram or type
1739 -- If entity has a type, and it is not a generic unit, then
1740 -- freeze it first (RM 13.14(10))
1742 if Present (Etype (E))
1743 and then Ekind (E) /= E_Generic_Function
1745 Freeze_And_Append (Etype (E), Loc, Result);
1748 -- For object created by object declaration, perform required
1749 -- categorization (preelaborate and pure) checks. Defer these
1750 -- checks to freeze time since pragma Import inhibits default
1751 -- initialization and thus pragma Import affects these checks.
1753 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
1754 Validate_Object_Declaration (Declaration_Node (E));
1757 -- Check that a constant which has a pragma Volatile[_Components]
1758 -- or Atomic[_Components] also has a pragma Import (RM C.6(13))
1760 -- Note: Atomic[_Components] also sets Volatile[_Components]
1762 if Ekind (E) = E_Constant
1763 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
1764 and then not Is_Imported (E)
1766 -- Make sure we actually have a pragma, and have not merely
1767 -- inherited the indication from elsewhere (e.g. an address
1768 -- clause, which is not good enough in RM terms!)
1770 if Present (Get_Rep_Pragma (E, Name_Atomic)) or else
1771 Present (Get_Rep_Pragma (E, Name_Atomic_Components)) or else
1772 Present (Get_Rep_Pragma (E, Name_Volatile)) or else
1773 Present (Get_Rep_Pragma (E, Name_Volatile_Components))
1776 ("stand alone atomic/volatile constant must be imported",
1781 -- Static objects require special handling
1783 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
1784 and then Is_Statically_Allocated (E)
1786 Freeze_Static_Object (E);
1789 -- Remaining step is to layout objects
1791 if Ekind (E) = E_Variable
1793 Ekind (E) = E_Constant
1795 Ekind (E) = E_Loop_Parameter
1803 -- Case of a type or subtype being frozen
1806 -- The type may be defined in a generic unit. This can occur when
1807 -- freezing a generic function that returns the type (which is
1808 -- defined in a parent unit). It is clearly meaningless to freeze
1809 -- this type. However, if it is a subtype, its size may be determi-
1810 -- nable and used in subsequent checks, so might as well try to
1813 if Present (Scope (E))
1814 and then Is_Generic_Unit (Scope (E))
1816 Check_Compile_Time_Size (E);
1820 -- Deal with special cases of freezing for subtype
1822 if E /= Base_Type (E) then
1824 -- If ancestor subtype present, freeze that first.
1825 -- Note that this will also get the base type frozen.
1827 Atype := Ancestor_Subtype (E);
1829 if Present (Atype) then
1830 Freeze_And_Append (Atype, Loc, Result);
1832 -- Otherwise freeze the base type of the entity before
1833 -- freezing the entity itself, (RM 13.14(14)).
1835 elsif E /= Base_Type (E) then
1836 Freeze_And_Append (Base_Type (E), Loc, Result);
1839 -- For a derived type, freeze its parent type first (RM 13.14(14))
1841 elsif Is_Derived_Type (E) then
1842 Freeze_And_Append (Etype (E), Loc, Result);
1843 Freeze_And_Append (First_Subtype (Etype (E)), Loc, Result);
1846 -- For array type, freeze index types and component type first
1847 -- before freezing the array (RM 13.14(14)).
1849 if Is_Array_Type (E) then
1851 Ctyp : constant Entity_Id := Component_Type (E);
1853 Non_Standard_Enum : Boolean := False;
1854 -- Set true if any of the index types is an enumeration
1855 -- type with a non-standard representation.
1858 Freeze_And_Append (Ctyp, Loc, Result);
1860 Indx := First_Index (E);
1861 while Present (Indx) loop
1862 Freeze_And_Append (Etype (Indx), Loc, Result);
1864 if Is_Enumeration_Type (Etype (Indx))
1865 and then Has_Non_Standard_Rep (Etype (Indx))
1867 Non_Standard_Enum := True;
1873 -- Processing that is done only for base types
1875 if Ekind (E) = E_Array_Type then
1877 -- Propagate flags for component type
1879 if Is_Controlled (Component_Type (E))
1880 or else Has_Controlled_Component (Ctyp)
1882 Set_Has_Controlled_Component (E);
1885 if Has_Unchecked_Union (Component_Type (E)) then
1886 Set_Has_Unchecked_Union (E);
1889 -- If packing was requested or if the component size was set
1890 -- explicitly, then see if bit packing is required. This
1891 -- processing is only done for base types, since all the
1892 -- representation aspects involved are type-related. This
1893 -- is not just an optimization, if we start processing the
1894 -- subtypes, they intefere with the settings on the base
1895 -- type (this is because Is_Packed has a slightly different
1896 -- meaning before and after freezing).
1903 if (Is_Packed (E) or else Has_Pragma_Pack (E))
1904 and then not Has_Atomic_Components (E)
1905 and then Known_Static_RM_Size (Ctyp)
1907 Csiz := UI_Max (RM_Size (Ctyp), 1);
1909 elsif Known_Component_Size (E) then
1910 Csiz := Component_Size (E);
1912 elsif not Known_Static_Esize (Ctyp) then
1916 Esiz := Esize (Ctyp);
1918 -- We can set the component size if it is less than
1919 -- 16, rounding it up to the next storage unit size.
1923 elsif Esiz <= 16 then
1929 -- Set component size up to match alignment if
1930 -- it would otherwise be less than the alignment.
1931 -- This deals with cases of types whose alignment
1932 -- exceeds their sizes (padded types).
1936 A : constant Uint := Alignment_In_Bits (Ctyp);
1947 if 1 <= Csiz and then Csiz <= 64 then
1949 -- We set the component size for all cases 1-64
1951 Set_Component_Size (Base_Type (E), Csiz);
1953 -- Actual packing is not needed for 8,16,32,64
1954 -- Also not needed for 24 if alignment is 1
1960 or else (Csiz = 24 and then Alignment (Ctyp) = 1)
1962 -- Here the array was requested to be packed, but
1963 -- the packing request had no effect, so Is_Packed
1966 -- Note: semantically this means that we lose
1967 -- track of the fact that a derived type inherited
1968 -- a pack pragma that was non-effective, but that
1971 -- We regard a Pack pragma as a request to set a
1972 -- representation characteristic, and this request
1975 Set_Is_Packed (Base_Type (E), False);
1977 -- In all other cases, packing is indeed needed
1980 Set_Has_Non_Standard_Rep (Base_Type (E));
1981 Set_Is_Bit_Packed_Array (Base_Type (E));
1982 Set_Is_Packed (Base_Type (E));
1987 -- Processing that is done only for subtypes
1990 -- Acquire alignment from base type
1992 if Unknown_Alignment (E) then
1993 Set_Alignment (E, Alignment (Base_Type (E)));
1997 -- Check one common case of a size given where the array
1998 -- needs to be packed, but was not so the size cannot be
1999 -- honored. This would of course be caught by the backend,
2000 -- and indeed we don't catch all cases. The point is that
2001 -- we can give a better error message in those cases that
2002 -- we do catch with the circuitry here.
2004 if Present (Size_Clause (E))
2005 and then Known_Static_Esize (E)
2006 and then not Has_Pragma_Pack (E)
2007 and then Number_Dimensions (E) = 1
2008 and then not Has_Component_Size_Clause (E)
2009 and then Known_Static_Component_Size (E)
2013 Ctyp : constant Entity_Id := Component_Type (E);
2016 Get_Index_Bounds (First_Index (E), Lo, Hi);
2018 if Compile_Time_Known_Value (Lo)
2019 and then Compile_Time_Known_Value (Hi)
2020 and then Known_Static_RM_Size (Ctyp)
2021 and then RM_Size (Ctyp) < 64
2024 Lov : constant Uint := Expr_Value (Lo);
2025 Hiv : constant Uint := Expr_Value (Hi);
2026 Len : constant Uint :=
2027 UI_Max (Uint_0, Hiv - Lov + 1);
2030 if Esize (E) < Len * Component_Size (E)
2031 and then Esize (E) = Len * RM_Size (Ctyp)
2034 ("size given for& too small",
2035 Size_Clause (E), E);
2037 ("\explicit pragma Pack is required",
2045 -- If any of the index types was an enumeration type with
2046 -- a non-standard rep clause, then we indicate that the
2047 -- array type is always packed (even if it is not bit packed).
2049 if Non_Standard_Enum then
2050 Set_Has_Non_Standard_Rep (Base_Type (E));
2051 Set_Is_Packed (Base_Type (E));
2055 Set_Component_Alignment_If_Not_Set (E);
2057 -- If the array is packed, we must create the packed array
2058 -- type to be used to actually implement the type. This is
2059 -- only needed for real array types (not for string literal
2060 -- types, since they are present only for the front end).
2063 and then Ekind (E) /= E_String_Literal_Subtype
2065 Create_Packed_Array_Type (E);
2066 Freeze_And_Append (Packed_Array_Type (E), Loc, Result);
2068 -- Size information of packed array type is copied to the
2069 -- array type, since this is really the representation.
2071 Set_Size_Info (E, Packed_Array_Type (E));
2072 Set_RM_Size (E, RM_Size (Packed_Array_Type (E)));
2075 -- For a class wide type, the corresponding specific type is
2076 -- frozen as well (RM 13.14(14))
2078 elsif Is_Class_Wide_Type (E) then
2079 Freeze_And_Append (Root_Type (E), Loc, Result);
2081 -- If the Class_Wide_Type is an Itype (when type is the anonymous
2082 -- parent of a derived type) and it is a library-level entity,
2083 -- generate an itype reference for it. Otherwise, its first
2084 -- explicit reference may be in an inner scope, which will be
2085 -- rejected by the back-end.
2088 and then Is_Compilation_Unit (Scope (E))
2092 Ref : Node_Id := Make_Itype_Reference (Loc);
2097 Result := New_List (Ref);
2099 Append (Ref, Result);
2104 -- For record (sub)type, freeze all the component types (RM
2105 -- 13.14(14). We test for E_Record_(sub)Type here, rather than
2106 -- using Is_Record_Type, because we don't want to attempt the
2107 -- freeze for the case of a private type with record extension
2108 -- (we will do that later when the full type is frozen).
2110 elsif Ekind (E) = E_Record_Type
2111 or else Ekind (E) = E_Record_Subtype
2113 Freeze_Record_Type (E);
2115 -- For a concurrent type, freeze corresponding record type. This
2116 -- does not correpond to any specific rule in the RM, but the
2117 -- record type is essentially part of the concurrent type.
2118 -- Freeze as well all local entities. This includes record types
2119 -- created for entry parameter blocks, and whatever local entities
2120 -- may appear in the private part.
2122 elsif Is_Concurrent_Type (E) then
2123 if Present (Corresponding_Record_Type (E)) then
2125 (Corresponding_Record_Type (E), Loc, Result);
2128 Comp := First_Entity (E);
2130 while Present (Comp) loop
2131 if Is_Type (Comp) then
2132 Freeze_And_Append (Comp, Loc, Result);
2134 elsif (Ekind (Comp)) /= E_Function then
2135 Freeze_And_Append (Etype (Comp), Loc, Result);
2141 -- Private types are required to point to the same freeze node
2142 -- as their corresponding full views. The freeze node itself
2143 -- has to point to the partial view of the entity (because
2144 -- from the partial view, we can retrieve the full view, but
2145 -- not the reverse). However, in order to freeze correctly,
2146 -- we need to freeze the full view. If we are freezing at the
2147 -- end of a scope (or within the scope of the private type),
2148 -- the partial and full views will have been swapped, the
2149 -- full view appears first in the entity chain and the swapping
2150 -- mechanism enusres that the pointers are properly set (on
2153 -- If we encounter the partial view before the full view
2154 -- (e.g. when freezing from another scope), we freeze the
2155 -- full view, and then set the pointers appropriately since
2156 -- we cannot rely on swapping to fix things up (subtypes in an
2157 -- outer scope might not get swapped).
2159 elsif Is_Incomplete_Or_Private_Type (E)
2160 and then not Is_Generic_Type (E)
2162 -- Case of full view present
2164 if Present (Full_View (E)) then
2166 -- If full view has already been frozen, then no
2167 -- further processing is required
2169 if Is_Frozen (Full_View (E)) then
2171 Set_Has_Delayed_Freeze (E, False);
2172 Set_Freeze_Node (E, Empty);
2173 Check_Debug_Info_Needed (E);
2175 -- Otherwise freeze full view and patch the pointers
2178 if Is_Private_Type (Full_View (E))
2179 and then Present (Underlying_Full_View (Full_View (E)))
2182 (Underlying_Full_View (Full_View (E)), Loc, Result);
2185 Freeze_And_Append (Full_View (E), Loc, Result);
2187 if Has_Delayed_Freeze (E) then
2188 F_Node := Freeze_Node (Full_View (E));
2190 if Present (F_Node) then
2191 Set_Freeze_Node (E, F_Node);
2192 Set_Entity (F_Node, E);
2194 -- {Incomplete,Private}_Subtypes
2195 -- with Full_Views constrained by discriminants
2197 Set_Has_Delayed_Freeze (E, False);
2198 Set_Freeze_Node (E, Empty);
2202 Check_Debug_Info_Needed (E);
2205 -- AI-117 requires that the convention of a partial view
2206 -- be the same as the convention of the full view. Note
2207 -- that this is a recognized breach of privacy, but it's
2208 -- essential for logical consistency of representation,
2209 -- and the lack of a rule in RM95 was an oversight.
2211 Set_Convention (E, Convention (Full_View (E)));
2213 Set_Size_Known_At_Compile_Time (E,
2214 Size_Known_At_Compile_Time (Full_View (E)));
2216 -- Size information is copied from the full view to the
2217 -- incomplete or private view for consistency
2219 -- We skip this is the full view is not a type. This is
2220 -- very strange of course, and can only happen as a result
2221 -- of certain illegalities, such as a premature attempt to
2222 -- derive from an incomplete type.
2224 if Is_Type (Full_View (E)) then
2225 Set_Size_Info (E, Full_View (E));
2226 Set_RM_Size (E, RM_Size (Full_View (E)));
2231 -- Case of no full view present. If entity is derived or subtype,
2232 -- it is safe to freeze, correctness depends on the frozen status
2233 -- of parent. Otherwise it is either premature usage, or a Taft
2234 -- amendment type, so diagnosis is at the point of use and the
2235 -- type might be frozen later.
2237 elsif E /= Base_Type (E)
2238 or else Is_Derived_Type (E)
2243 Set_Is_Frozen (E, False);
2247 -- For access subprogram, freeze types of all formals, the return
2248 -- type was already frozen, since it is the Etype of the function.
2250 elsif Ekind (E) = E_Subprogram_Type then
2251 Formal := First_Formal (E);
2252 while Present (Formal) loop
2253 Freeze_And_Append (Etype (Formal), Loc, Result);
2254 Next_Formal (Formal);
2257 -- If the return type requires a transient scope, and we are on
2258 -- a target allowing functions to return with a depressed stack
2259 -- pointer, then we mark the function as requiring this treatment.
2261 if Functions_Return_By_DSP_On_Target
2262 and then Requires_Transient_Scope (Etype (E))
2264 Set_Function_Returns_With_DSP (E);
2267 Freeze_Subprogram (E);
2269 -- For access to a protected subprogram, freeze the equivalent
2270 -- type (however this is not set if we are not generating code)
2271 -- or if this is an anonymous type used just for resolution).
2273 elsif Ekind (E) = E_Access_Protected_Subprogram_Type
2274 and then Operating_Mode = Generate_Code
2275 and then Present (Equivalent_Type (E))
2277 Freeze_And_Append (Equivalent_Type (E), Loc, Result);
2280 -- Generic types are never seen by the back-end, and are also not
2281 -- processed by the expander (since the expander is turned off for
2282 -- generic processing), so we never need freeze nodes for them.
2284 if Is_Generic_Type (E) then
2288 -- Some special processing for non-generic types to complete
2289 -- representation details not known till the freeze point.
2291 if Is_Fixed_Point_Type (E) then
2292 Freeze_Fixed_Point_Type (E);
2294 elsif Is_Enumeration_Type (E) then
2295 Freeze_Enumeration_Type (E);
2297 elsif Is_Integer_Type (E) then
2298 Adjust_Esize_For_Alignment (E);
2300 elsif Is_Access_Type (E)
2301 and then No (Associated_Storage_Pool (E))
2303 Check_Restriction (No_Standard_Storage_Pools, E);
2306 -- If the current entity is an array or record subtype and has
2307 -- discriminants used to constrain it, it must not freeze, because
2308 -- Freeze_Entity nodes force Gigi to process the frozen type.
2310 if Is_Composite_Type (E) then
2312 if Is_Array_Type (E) then
2315 Index : Node_Id := First_Index (E);
2320 while Present (Index) loop
2321 if Etype (Index) /= Any_Type then
2322 Get_Index_Bounds (Index, Expr1, Expr2);
2324 for J in 1 .. 2 loop
2325 if Nkind (Expr1) = N_Identifier
2326 and then Ekind (Entity (Expr1)) = E_Discriminant
2328 Set_Has_Delayed_Freeze (E, False);
2329 Set_Freeze_Node (E, Empty);
2330 Check_Debug_Info_Needed (E);
2342 elsif Has_Discriminants (E)
2343 and Is_Constrained (E)
2346 Constraint : Elmt_Id;
2350 Constraint := First_Elmt (Discriminant_Constraint (E));
2352 while Present (Constraint) loop
2354 Expr := Node (Constraint);
2355 if Nkind (Expr) = N_Identifier
2356 and then Ekind (Entity (Expr)) = E_Discriminant
2358 Set_Has_Delayed_Freeze (E, False);
2359 Set_Freeze_Node (E, Empty);
2360 Check_Debug_Info_Needed (E);
2364 Next_Elmt (Constraint);
2370 -- AI-117 requires that all new primitives of a tagged type
2371 -- must inherit the convention of the full view of the type.
2372 -- Inherited and overriding operations are defined to inherit
2373 -- the convention of their parent or overridden subprogram
2374 -- (also specified in AI-117), and that will have occurred
2375 -- earlier (in Derive_Subprogram and New_Overloaded_Entity).
2376 -- Here we set the convention of primitives that are still
2377 -- convention Ada, which will ensure that any new primitives
2378 -- inherit the type's convention. Class-wide types can have
2379 -- a foreign convention inherited from their specific type,
2380 -- but are excluded from this since they don't have any
2381 -- associated primitives.
2383 if Is_Tagged_Type (E)
2384 and then not Is_Class_Wide_Type (E)
2385 and then Convention (E) /= Convention_Ada
2388 Prim_List : constant Elist_Id := Primitive_Operations (E);
2392 Prim := First_Elmt (Prim_List);
2393 while Present (Prim) loop
2394 if Convention (Node (Prim)) = Convention_Ada then
2395 Set_Convention (Node (Prim), Convention (E));
2404 -- Generate primitive operation references for a tagged type
2406 if Is_Tagged_Type (E)
2407 and then not Is_Class_Wide_Type (E)
2410 Prim_List : constant Elist_Id := Primitive_Operations (E);
2415 Prim := First_Elmt (Prim_List);
2416 while Present (Prim) loop
2419 -- If the operation is derived, get the original for
2420 -- cross-reference purposes (it is the original for
2421 -- which we want the xref, and for which the comes
2422 -- from source test needs to be performed).
2424 while Present (Alias (Ent)) loop
2428 Generate_Reference (E, Ent, 'p', Set_Ref => False);
2432 -- If we get an exception, then something peculiar has happened
2433 -- probably as a result of a previous error. Since this is only
2434 -- for non-critical cross-references, ignore the error.
2437 when others => null;
2441 -- Now that all types from which E may depend are frozen, see
2442 -- if the size is known at compile time, if it must be unsigned,
2443 -- or if strict alignent is required
2445 Check_Compile_Time_Size (E);
2446 Check_Unsigned_Type (E);
2448 if Base_Type (E) = E then
2449 Check_Strict_Alignment (E);
2452 -- Do not allow a size clause for a type which does not have a size
2453 -- that is known at compile time
2455 if Has_Size_Clause (E)
2456 and then not Size_Known_At_Compile_Time (E)
2458 -- Supress this message if errors posted on E, even if we are
2459 -- in all errors mode, since this is often a junk message
2461 if not Error_Posted (E) then
2463 ("size clause not allowed for variable length type",
2468 -- Remaining process is to set/verify the representation information,
2469 -- in particular the size and alignment values. This processing is
2470 -- not required for generic types, since generic types do not play
2471 -- any part in code generation, and so the size and alignment values
2472 -- for suhc types are irrelevant.
2474 if Is_Generic_Type (E) then
2477 -- Otherwise we call the layout procedure
2483 -- End of freeze processing for type entities
2486 -- Here is where we logically freeze the current entity. If it has a
2487 -- freeze node, then this is the point at which the freeze node is
2488 -- linked into the result list.
2490 if Has_Delayed_Freeze (E) then
2492 -- If a freeze node is already allocated, use it, otherwise allocate
2493 -- a new one. The preallocation happens in the case of anonymous base
2494 -- types, where we preallocate so that we can set First_Subtype_Link.
2495 -- Note that we reset the Sloc to the current freeze location.
2497 if Present (Freeze_Node (E)) then
2498 F_Node := Freeze_Node (E);
2499 Set_Sloc (F_Node, Loc);
2502 F_Node := New_Node (N_Freeze_Entity, Loc);
2503 Set_Freeze_Node (E, F_Node);
2504 Set_Access_Types_To_Process (F_Node, No_Elist);
2505 Set_TSS_Elist (F_Node, No_Elist);
2506 Set_Actions (F_Node, No_List);
2509 Set_Entity (F_Node, E);
2511 if Result = No_List then
2512 Result := New_List (F_Node);
2514 Append (F_Node, Result);
2519 -- When a type is frozen, the first subtype of the type is frozen as
2520 -- well (RM 13.14(15)). This has to be done after freezing the type,
2521 -- since obviously the first subtype depends on its own base type.
2524 Freeze_And_Append (First_Subtype (E), Loc, Result);
2526 -- If we just froze a tagged non-class wide record, then freeze the
2527 -- corresponding class-wide type. This must be done after the tagged
2528 -- type itself is frozen, because the class-wide type refers to the
2529 -- tagged type which generates the class.
2531 if Is_Tagged_Type (E)
2532 and then not Is_Class_Wide_Type (E)
2533 and then Present (Class_Wide_Type (E))
2535 Freeze_And_Append (Class_Wide_Type (E), Loc, Result);
2539 Check_Debug_Info_Needed (E);
2541 -- Special handling for subprograms
2543 if Is_Subprogram (E) then
2545 -- If subprogram has address clause then reset Is_Public flag, since
2546 -- we do not want the backend to generate external references.
2548 if Present (Address_Clause (E))
2549 and then not Is_Library_Level_Entity (E)
2551 Set_Is_Public (E, False);
2553 -- If no address clause and not intrinsic, then for imported
2554 -- subprogram in main unit, generate descriptor if we are in
2555 -- Propagate_Exceptions mode.
2557 elsif Propagate_Exceptions
2558 and then Is_Imported (E)
2559 and then not Is_Intrinsic_Subprogram (E)
2560 and then Convention (E) /= Convention_Stubbed
2562 if Result = No_List then
2563 Result := Empty_List;
2566 Generate_Subprogram_Descriptor_For_Imported_Subprogram
2575 -----------------------------
2576 -- Freeze_Enumeration_Type --
2577 -----------------------------
2579 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
2581 if Has_Foreign_Convention (Typ)
2582 and then not Has_Size_Clause (Typ)
2583 and then Esize (Typ) < Standard_Integer_Size
2585 Init_Esize (Typ, Standard_Integer_Size);
2588 Adjust_Esize_For_Alignment (Typ);
2590 end Freeze_Enumeration_Type;
2592 -----------------------
2593 -- Freeze_Expression --
2594 -----------------------
2596 procedure Freeze_Expression (N : Node_Id) is
2597 In_Def_Exp : constant Boolean := In_Default_Expression;
2600 Desig_Typ : Entity_Id;
2604 Freeze_Outside : Boolean := False;
2605 -- This flag is set true if the entity must be frozen outside the
2606 -- current subprogram. This happens in the case of expander generated
2607 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
2608 -- not freeze all entities like other bodies, but which nevertheless
2609 -- may reference entities that have to be frozen before the body and
2610 -- obviously cannot be frozen inside the body.
2612 function In_Exp_Body (N : Node_Id) return Boolean;
2613 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
2614 -- it is the handled statement sequence of an expander generated
2615 -- subprogram (init proc, or stream subprogram). If so, it returns
2616 -- True, otherwise False.
2618 function In_Exp_Body (N : Node_Id) return Boolean is
2622 if Nkind (N) = N_Subprogram_Body then
2628 if Nkind (P) /= N_Subprogram_Body then
2632 P := Defining_Unit_Name (Specification (P));
2634 if Nkind (P) = N_Defining_Identifier
2635 and then (Chars (P) = Name_uInit_Proc or else
2636 Chars (P) = Name_uInput or else
2637 Chars (P) = Name_uOutput or else
2638 Chars (P) = Name_uRead or else
2639 Chars (P) = Name_uWrite)
2649 -- Start of processing for Freeze_Expression
2652 -- Immediate return if freezing is inhibited. This flag is set by
2653 -- the analyzer to stop freezing on generated expressions that would
2654 -- cause freezing if they were in the source program, but which are
2655 -- not supposed to freeze, since they are created.
2657 if Must_Not_Freeze (N) then
2661 -- If expression is non-static, then it does not freeze in a default
2662 -- expression, see section "Handling of Default Expressions" in the
2663 -- spec of package Sem for further details. Note that we have to
2664 -- make sure that we actually have a real expression (if we have
2665 -- a subtype indication, we can't test Is_Static_Expression!)
2668 and then Nkind (N) in N_Subexpr
2669 and then not Is_Static_Expression (N)
2674 -- Freeze type of expression if not frozen already
2676 if Nkind (N) in N_Has_Etype
2677 and then not Is_Frozen (Etype (N))
2684 -- For entity name, freeze entity if not frozen already. A special
2685 -- exception occurs for an identifier that did not come from source.
2686 -- We don't let such identifiers freeze a non-internal entity, i.e.
2687 -- an entity that did come from source, since such an identifier was
2688 -- generated by the expander, and cannot have any semantic effect on
2689 -- the freezing semantics. For example, this stops the parameter of
2690 -- an initialization procedure from freezing the variable.
2692 if Is_Entity_Name (N)
2693 and then not Is_Frozen (Entity (N))
2694 and then (Nkind (N) /= N_Identifier
2695 or else Comes_From_Source (N)
2696 or else not Comes_From_Source (Entity (N)))
2704 -- For an allocator freeze designated type if not frozen already.
2706 -- For an aggregate whose component type is an access type, freeze
2707 -- the designated type now, so that its freeze does not appear within
2708 -- the loop that might be created in the expansion of the aggregate.
2709 -- If the designated type is a private type without full view, the
2710 -- expression cannot contain an allocator, so the type is not frozen.
2716 Desig_Typ := Designated_Type (Etype (N));
2719 if Is_Array_Type (Etype (N))
2720 and then Is_Access_Type (Component_Type (Etype (N)))
2722 Desig_Typ := Designated_Type (Component_Type (Etype (N)));
2725 when N_Selected_Component |
2726 N_Indexed_Component |
2729 if Is_Access_Type (Etype (Prefix (N))) then
2730 Desig_Typ := Designated_Type (Etype (Prefix (N)));
2738 if Desig_Typ /= Empty
2739 and then (Is_Frozen (Desig_Typ)
2740 or else (not Is_Fully_Defined (Desig_Typ)))
2745 -- All done if nothing needs freezing
2749 and then No (Desig_Typ)
2754 -- Loop for looking at the right place to insert the freeze nodes
2755 -- exiting from the loop when it is appropriate to insert the freeze
2756 -- node before the current node P.
2758 -- Also checks some special exceptions to the freezing rules. These
2759 -- cases result in a direct return, bypassing the freeze action.
2763 Parent_P := Parent (P);
2765 -- If we don't have a parent, then we are not in a well-formed
2766 -- tree. This is an unusual case, but there are some legitimate
2767 -- situations in which this occurs, notably when the expressions
2768 -- in the range of a type declaration are resolved. We simply
2769 -- ignore the freeze request in this case. Is this right ???
2771 if No (Parent_P) then
2775 -- See if we have got to an appropriate point in the tree
2777 case Nkind (Parent_P) is
2779 -- A special test for the exception of (RM 13.14(8)) for the
2780 -- case of per-object expressions (RM 3.8(18)) occurring in a
2781 -- component definition or a discrete subtype definition. Note
2782 -- that we test for a component declaration which includes both
2783 -- cases we are interested in, and furthermore the tree does not
2784 -- have explicit nodes for either of these two constructs.
2786 when N_Component_Declaration =>
2788 -- The case we want to test for here is an identifier that is
2789 -- a per-object expression, this is either a discriminant that
2790 -- appears in a context other than the component declaration
2791 -- or it is a reference to the type of the enclosing construct.
2793 -- For either of these cases, we skip the freezing
2795 if not In_Default_Expression
2796 and then Nkind (N) = N_Identifier
2797 and then (Present (Entity (N)))
2799 -- We recognize the discriminant case by just looking for
2800 -- a reference to a discriminant. It can only be one for
2801 -- the enclosing construct. Skip freezing in this case.
2803 if Ekind (Entity (N)) = E_Discriminant then
2806 -- For the case of a reference to the enclosing record,
2807 -- (or task or protected type), we look for a type that
2808 -- matches the current scope.
2810 elsif Entity (N) = Current_Scope then
2815 -- If we have an enumeration literal that appears as the
2816 -- choice in the aggregate of an enumeration representation
2817 -- clause, then freezing does not occur (RM 13.14(9)).
2819 when N_Enumeration_Representation_Clause =>
2821 -- The case we are looking for is an enumeration literal
2823 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
2824 and then Is_Enumeration_Type (Etype (N))
2826 -- If enumeration literal appears directly as the choice,
2827 -- do not freeze (this is the normal non-overloade case)
2829 if Nkind (Parent (N)) = N_Component_Association
2830 and then First (Choices (Parent (N))) = N
2834 -- If enumeration literal appears as the name of a
2835 -- function which is the choice, then also do not freeze.
2836 -- This happens in the overloaded literal case, where the
2837 -- enumeration literal is temporarily changed to a function
2838 -- call for overloading analysis purposes.
2840 elsif Nkind (Parent (N)) = N_Function_Call
2842 Nkind (Parent (Parent (N))) = N_Component_Association
2844 First (Choices (Parent (Parent (N)))) = Parent (N)
2850 -- Normally if the parent is a handled sequence of statements,
2851 -- then the current node must be a statement, and that is an
2852 -- appropriate place to insert a freeze node.
2854 when N_Handled_Sequence_Of_Statements =>
2856 -- An exception occurs when the sequence of statements is
2857 -- for an expander generated body that did not do the usual
2858 -- freeze all operation. In this case we usually want to
2859 -- freeze outside this body, not inside it, and we skip
2860 -- past the subprogram body that we are inside.
2862 if In_Exp_Body (Parent_P) then
2864 -- However, we *do* want to freeze at this point if we have
2865 -- an entity to freeze, and that entity is declared *inside*
2866 -- the body of the expander generated procedure. This case
2867 -- is recognized by the scope of the type, which is either
2868 -- the spec for some enclosing body, or (in the case of
2869 -- init_procs, for which there are no separate specs) the
2873 Subp : constant Node_Id := Parent (Parent_P);
2877 if Nkind (Subp) = N_Subprogram_Body then
2878 Cspc := Corresponding_Spec (Subp);
2880 if (Present (Typ) and then Scope (Typ) = Cspc)
2882 (Present (Nam) and then Scope (Nam) = Cspc)
2887 and then Scope (Typ) = Current_Scope
2888 and then Current_Scope = Defining_Entity (Subp)
2895 -- If not that exception to the exception, then this is
2896 -- where we delay the freeze till outside the body.
2898 Parent_P := Parent (Parent_P);
2899 Freeze_Outside := True;
2901 -- Here if normal case where we are in handled statement
2902 -- sequence and want to do the insertion right there.
2908 -- If parent is a body or a spec or a block, then the current
2909 -- node is a statement or declaration and we can insert the
2910 -- freeze node before it.
2912 when N_Package_Specification |
2918 N_Block_Statement => exit;
2920 -- The expander is allowed to define types in any statements list,
2921 -- so any of the following parent nodes also mark a freezing point
2922 -- if the actual node is in a list of statements or declarations.
2924 when N_Exception_Handler |
2927 N_Case_Statement_Alternative |
2928 N_Compilation_Unit_Aux |
2929 N_Selective_Accept |
2930 N_Accept_Alternative |
2931 N_Delay_Alternative |
2932 N_Conditional_Entry_Call |
2933 N_Entry_Call_Alternative |
2934 N_Triggering_Alternative |
2938 exit when Is_List_Member (P);
2940 -- Note: The N_Loop_Statement is a special case. A type that
2941 -- appears in the source can never be frozen in a loop (this
2942 -- occurs only because of a loop expanded by the expander),
2943 -- so we keep on going. Otherwise we terminate the search.
2944 -- Same is true of any entity which comes from source. (if they
2945 -- have a predefined type, that type does not appear to come
2946 -- from source, but the entity should not be frozen here).
2948 when N_Loop_Statement =>
2949 exit when not Comes_From_Source (Etype (N))
2950 and then (No (Nam) or else not Comes_From_Source (Nam));
2952 -- For all other cases, keep looking at parents
2958 -- We fall through the case if we did not yet find the proper
2959 -- place in the free for inserting the freeze node, so climb!
2964 -- If the expression appears in a record or an initialization
2965 -- procedure, the freeze nodes are collected and attached to
2966 -- the current scope, to be inserted and analyzed on exit from
2967 -- the scope, to insure that generated entities appear in the
2968 -- correct scope. If the expression is a default for a discriminant
2969 -- specification, the scope is still void. The expression can also
2970 -- appear in the discriminant part of a private or concurrent type.
2972 -- The other case requiring this special handling is if we are in
2973 -- a default expression, since in that case we are about to freeze
2974 -- a static type, and the freeze scope needs to be the outer scope,
2975 -- not the scope of the subprogram with the default parameter.
2977 -- For default expressions in generic units, the Move_Freeze_Nodes
2978 -- mechanism (see sem_ch12.adb) takes care of placing them at the
2979 -- proper place, after the generic unit.
2981 if (In_Def_Exp and not Inside_A_Generic)
2982 or else Freeze_Outside
2983 or else (Is_Type (Current_Scope)
2984 and then (not Is_Concurrent_Type (Current_Scope)
2985 or else not Has_Completion (Current_Scope)))
2986 or else Ekind (Current_Scope) = E_Void
2989 Loc : constant Source_Ptr := Sloc (Current_Scope);
2990 Freeze_Nodes : List_Id := No_List;
2993 if Present (Desig_Typ) then
2994 Freeze_And_Append (Desig_Typ, Loc, Freeze_Nodes);
2997 if Present (Typ) then
2998 Freeze_And_Append (Typ, Loc, Freeze_Nodes);
3001 if Present (Nam) then
3002 Freeze_And_Append (Nam, Loc, Freeze_Nodes);
3005 if Is_Non_Empty_List (Freeze_Nodes) then
3007 if No (Scope_Stack.Table
3008 (Scope_Stack.Last).Pending_Freeze_Actions)
3011 (Scope_Stack.Last).Pending_Freeze_Actions :=
3014 Append_List (Freeze_Nodes, Scope_Stack.Table
3015 (Scope_Stack.Last).Pending_Freeze_Actions);
3023 -- Now we have the right place to do the freezing. First, a special
3024 -- adjustment, if we are in default expression analysis mode, these
3025 -- freeze actions must not be thrown away (normally all inserted
3026 -- actions are thrown away in this mode. However, the freeze actions
3027 -- are from static expressions and one of the important reasons we
3028 -- are doing this special analysis is to get these freeze actions.
3029 -- Therefore we turn off the In_Default_Expression mode to propagate
3030 -- these freeze actions. This also means they get properly analyzed
3033 In_Default_Expression := False;
3035 -- Freeze the designated type of an allocator (RM 13.14(12))
3037 if Present (Desig_Typ) then
3038 Freeze_Before (P, Desig_Typ);
3041 -- Freeze type of expression (RM 13.14(9)). Note that we took care of
3042 -- the enumeration representation clause exception in the loop above.
3044 if Present (Typ) then
3045 Freeze_Before (P, Typ);
3048 -- Freeze name if one is present (RM 13.14(10))
3050 if Present (Nam) then
3051 Freeze_Before (P, Nam);
3054 In_Default_Expression := In_Def_Exp;
3055 end Freeze_Expression;
3057 -----------------------------
3058 -- Freeze_Fixed_Point_Type --
3059 -----------------------------
3061 -- Certain fixed-point types and subtypes, including implicit base
3062 -- types and declared first subtypes, have not yet set up a range.
3063 -- This is because the range cannot be set until the Small and Size
3064 -- values are known, and these are not known till the type is frozen.
3066 -- To signal this case, Scalar_Range contains an unanalyzed syntactic
3067 -- range whose bounds are unanalyzed real literals. This routine will
3068 -- recognize this case, and transform this range node into a properly
3069 -- typed range with properly analyzed and resolved values.
3071 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
3072 Rng : constant Node_Id := Scalar_Range (Typ);
3073 Lo : constant Node_Id := Low_Bound (Rng);
3074 Hi : constant Node_Id := High_Bound (Rng);
3075 Btyp : constant Entity_Id := Base_Type (Typ);
3076 Brng : constant Node_Id := Scalar_Range (Btyp);
3077 BLo : constant Node_Id := Low_Bound (Brng);
3078 BHi : constant Node_Id := High_Bound (Brng);
3079 Small : constant Ureal := Small_Value (Typ);
3086 function Fsize (Lov, Hiv : Ureal) return Nat;
3087 -- Returns size of type with given bounds. Also leaves these
3088 -- bounds set as the current bounds of the Typ.
3090 function Fsize (Lov, Hiv : Ureal) return Nat is
3092 Set_Realval (Lo, Lov);
3093 Set_Realval (Hi, Hiv);
3094 return Minimum_Size (Typ);
3097 -- Start of processing for Freeze_Fixed_Point_Type;
3100 -- If Esize of a subtype has not previously been set, set it now
3102 if Unknown_Esize (Typ) then
3103 Atype := Ancestor_Subtype (Typ);
3105 if Present (Atype) then
3106 Set_Size_Info (Typ, Atype);
3108 Set_Size_Info (Typ, Base_Type (Typ));
3112 -- Immediate return if the range is already analyzed. This means
3113 -- that the range is already set, and does not need to be computed
3116 if Analyzed (Rng) then
3120 -- Immediate return if either of the bounds raises Constraint_Error
3122 if Raises_Constraint_Error (Lo)
3123 or else Raises_Constraint_Error (Hi)
3128 Loval := Realval (Lo);
3129 Hival := Realval (Hi);
3131 -- Ordinary fixed-point case
3133 if Is_Ordinary_Fixed_Point_Type (Typ) then
3135 -- For the ordinary fixed-point case, we are allowed to fudge the
3136 -- end-points up or down by small. Generally we prefer to fudge
3137 -- up, i.e. widen the bounds for non-model numbers so that the
3138 -- end points are included. However there are cases in which this
3139 -- cannot be done, and indeed cases in which we may need to narrow
3140 -- the bounds. The following circuit makes the decision.
3142 -- Note: our terminology here is that Incl_EP means that the
3143 -- bounds are widened by Small if necessary to include the end
3144 -- points, and Excl_EP means that the bounds are narrowed by
3145 -- Small to exclude the end-points if this reduces the size.
3147 -- Note that in the Incl case, all we care about is including the
3148 -- end-points. In the Excl case, we want to narrow the bounds as
3149 -- much as permitted by the RM, to give the smallest possible size.
3152 Loval_Incl_EP : Ureal;
3153 Hival_Incl_EP : Ureal;
3155 Loval_Excl_EP : Ureal;
3156 Hival_Excl_EP : Ureal;
3162 First_Subt : Entity_Id;
3167 -- First step. Base types are required to be symmetrical. Right
3168 -- now, the base type range is a copy of the first subtype range.
3169 -- This will be corrected before we are done, but right away we
3170 -- need to deal with the case where both bounds are non-negative.
3171 -- In this case, we set the low bound to the negative of the high
3172 -- bound, to make sure that the size is computed to include the
3173 -- required sign. Note that we do not need to worry about the
3174 -- case of both bounds negative, because the sign will be dealt
3175 -- with anyway. Furthermore we can't just go making such a bound
3176 -- symmetrical, since in a twos-complement system, there is an
3177 -- extra negative value which could not be accomodated on the
3181 and then not UR_Is_Negative (Loval)
3182 and then Hival > Loval
3185 Set_Realval (Lo, Loval);
3188 -- Compute the fudged bounds. If the number is a model number,
3189 -- then we do nothing to include it, but we are allowed to
3190 -- backoff to the next adjacent model number when we exclude
3191 -- it. If it is not a model number then we straddle the two
3192 -- values with the model numbers on either side.
3194 Model_Num := UR_Trunc (Loval / Small) * Small;
3196 if Loval = Model_Num then
3197 Loval_Incl_EP := Model_Num;
3199 Loval_Incl_EP := Model_Num - Small;
3202 -- The low value excluding the end point is Small greater, but
3203 -- we do not do this exclusion if the low value is positive,
3204 -- since it can't help the size and could actually hurt by
3205 -- crossing the high bound.
3207 if UR_Is_Negative (Loval_Incl_EP) then
3208 Loval_Excl_EP := Loval_Incl_EP + Small;
3210 Loval_Excl_EP := Loval_Incl_EP;
3213 -- Similar processing for upper bound and high value
3215 Model_Num := UR_Trunc (Hival / Small) * Small;
3217 if Hival = Model_Num then
3218 Hival_Incl_EP := Model_Num;
3220 Hival_Incl_EP := Model_Num + Small;
3223 if UR_Is_Positive (Hival_Incl_EP) then
3224 Hival_Excl_EP := Hival_Incl_EP - Small;
3226 Hival_Excl_EP := Hival_Incl_EP;
3229 -- One further adjustment is needed. In the case of subtypes,
3230 -- we cannot go outside the range of the base type, or we get
3231 -- peculiarities, and the base type range is already set. This
3232 -- only applies to the Incl values, since clearly the Excl
3233 -- values are already as restricted as they are allowed to be.
3236 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
3237 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
3240 -- Get size including and excluding end points
3242 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
3243 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
3245 -- No need to exclude end-points if it does not reduce size
3247 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
3248 Loval_Excl_EP := Loval_Incl_EP;
3251 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
3252 Hival_Excl_EP := Hival_Incl_EP;
3255 -- Now we set the actual size to be used. We want to use the
3256 -- bounds fudged up to include the end-points but only if this
3257 -- can be done without violating a specifically given size
3258 -- size clause or causing an unacceptable increase in size.
3260 -- Case of size clause given
3262 if Has_Size_Clause (Typ) then
3264 -- Use the inclusive size only if it is consistent with
3265 -- the explicitly specified size.
3267 if Size_Incl_EP <= RM_Size (Typ) then
3268 Actual_Lo := Loval_Incl_EP;
3269 Actual_Hi := Hival_Incl_EP;
3270 Actual_Size := Size_Incl_EP;
3272 -- If the inclusive size is too large, we try excluding
3273 -- the end-points (will be caught later if does not work).
3276 Actual_Lo := Loval_Excl_EP;
3277 Actual_Hi := Hival_Excl_EP;
3278 Actual_Size := Size_Excl_EP;
3281 -- Case of size clause not given
3284 -- If we have a base type whose corresponding first subtype
3285 -- has an explicit size that is large enough to include our
3286 -- end-points, then do so. There is no point in working hard
3287 -- to get a base type whose size is smaller than the specified
3288 -- size of the first subtype.
3290 First_Subt := First_Subtype (Typ);
3292 if Has_Size_Clause (First_Subt)
3293 and then Size_Incl_EP <= Esize (First_Subt)
3295 Actual_Size := Size_Incl_EP;
3296 Actual_Lo := Loval_Incl_EP;
3297 Actual_Hi := Hival_Incl_EP;
3299 -- If excluding the end-points makes the size smaller and
3300 -- results in a size of 8,16,32,64, then we take the smaller
3301 -- size. For the 64 case, this is compulsory. For the other
3302 -- cases, it seems reasonable. We like to include end points
3303 -- if we can, but not at the expense of moving to the next
3304 -- natural boundary of size.
3306 elsif Size_Incl_EP /= Size_Excl_EP
3308 (Size_Excl_EP = 8 or else
3309 Size_Excl_EP = 16 or else
3310 Size_Excl_EP = 32 or else
3313 Actual_Size := Size_Excl_EP;
3314 Actual_Lo := Loval_Excl_EP;
3315 Actual_Hi := Hival_Excl_EP;
3317 -- Otherwise we can definitely include the end points
3320 Actual_Size := Size_Incl_EP;
3321 Actual_Lo := Loval_Incl_EP;
3322 Actual_Hi := Hival_Incl_EP;
3325 -- One pathological case: normally we never fudge a low
3326 -- bound down, since it would seem to increase the size
3327 -- (if it has any effect), but for ranges containing a
3328 -- single value, or no values, the high bound can be
3329 -- small too large. Consider:
3331 -- type t is delta 2.0**(-14)
3332 -- range 131072.0 .. 0;
3334 -- That lower bound is *just* outside the range of 32
3335 -- bits, and does need fudging down in this case. Note
3336 -- that the bounds will always have crossed here, since
3337 -- the high bound will be fudged down if necessary, as
3340 -- type t is delta 2.0**(-14)
3341 -- range 131072.0 .. 131072.0;
3343 -- So we can detect the situation by looking for crossed
3344 -- bounds, and if the bounds are crossed, and the low
3345 -- bound is greater than zero, we will always back it
3346 -- off by small, since this is completely harmless.
3348 if Actual_Lo > Actual_Hi then
3349 if UR_Is_Positive (Actual_Lo) then
3350 Actual_Lo := Loval_Incl_EP - Small;
3351 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
3353 -- And of course, we need to do exactly the same parallel
3354 -- fudge for flat ranges in the negative region.
3356 elsif UR_Is_Negative (Actual_Hi) then
3357 Actual_Hi := Hival_Incl_EP + Small;
3358 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
3363 Set_Realval (Lo, Actual_Lo);
3364 Set_Realval (Hi, Actual_Hi);
3367 -- For the decimal case, none of this fudging is required, since there
3368 -- are no end-point problems in the decimal case (the end-points are
3369 -- always included).
3372 Actual_Size := Fsize (Loval, Hival);
3375 -- At this stage, the actual size has been calculated and the proper
3376 -- required bounds are stored in the low and high bounds.
3378 if Actual_Size > 64 then
3379 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
3381 ("size required (^) for type& too large, maximum is 64", Typ);
3385 -- Check size against explicit given size
3387 if Has_Size_Clause (Typ) then
3388 if Actual_Size > RM_Size (Typ) then
3389 Error_Msg_Uint_1 := RM_Size (Typ);
3390 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
3392 ("size given (^) for type& too small, minimum is ^",
3393 Size_Clause (Typ), Typ);
3396 Actual_Size := UI_To_Int (Esize (Typ));
3399 -- Increase size to next natural boundary if no size clause given
3402 if Actual_Size <= 8 then
3404 elsif Actual_Size <= 16 then
3406 elsif Actual_Size <= 32 then
3412 Init_Esize (Typ, Actual_Size);
3413 Adjust_Esize_For_Alignment (Typ);
3416 -- If we have a base type, then expand the bounds so that they
3417 -- extend to the full width of the allocated size in bits, to
3418 -- avoid junk range checks on intermediate computations.
3420 if Base_Type (Typ) = Typ then
3421 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
3422 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
3425 -- Final step is to reanalyze the bounds using the proper type
3426 -- and set the Corresponding_Integer_Value fields of the literals.
3428 Set_Etype (Lo, Empty);
3429 Set_Analyzed (Lo, False);
3432 -- Resolve with universal fixed if the base type, and the base
3433 -- type if it is a subtype. Note we can't resolve the base type
3434 -- with itself, that would be a reference before definition.
3437 Resolve (Lo, Universal_Fixed);
3442 -- Set corresponding integer value for bound
3444 Set_Corresponding_Integer_Value
3445 (Lo, UR_To_Uint (Realval (Lo) / Small));
3447 -- Similar processing for high bound
3449 Set_Etype (Hi, Empty);
3450 Set_Analyzed (Hi, False);
3454 Resolve (Hi, Universal_Fixed);
3459 Set_Corresponding_Integer_Value
3460 (Hi, UR_To_Uint (Realval (Hi) / Small));
3462 -- Set type of range to correspond to bounds
3464 Set_Etype (Rng, Etype (Lo));
3466 -- Set Esize to calculated size and also set RM_Size
3468 Init_Esize (Typ, Actual_Size);
3470 -- Set RM_Size if not already set. If already set, check value
3473 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
3476 if RM_Size (Typ) /= Uint_0 then
3477 if RM_Size (Typ) < Minsiz then
3478 Error_Msg_Uint_1 := RM_Size (Typ);
3479 Error_Msg_Uint_2 := Minsiz;
3481 ("size given (^) for type& too small, minimum is ^",
3482 Size_Clause (Typ), Typ);
3486 Set_RM_Size (Typ, Minsiz);
3490 end Freeze_Fixed_Point_Type;
3496 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
3500 Set_Has_Delayed_Freeze (T);
3501 L := Freeze_Entity (T, Sloc (N));
3503 if Is_Non_Empty_List (L) then
3504 Insert_Actions (N, L);
3508 --------------------------
3509 -- Freeze_Static_Object --
3510 --------------------------
3512 procedure Freeze_Static_Object (E : Entity_Id) is
3514 Cannot_Be_Static : exception;
3515 -- Exception raised if the type of a static object cannot be made
3516 -- static. This happens if the type depends on non-global objects.
3518 procedure Ensure_Expression_Is_SA (N : Node_Id);
3519 -- Called to ensure that an expression used as part of a type
3520 -- definition is statically allocatable, which means that the type
3521 -- of the expression is statically allocatable, and the expression
3522 -- is either static, or a reference to a library level constant.
3524 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
3525 -- Called to mark a type as static, checking that it is possible
3526 -- to set the type as static. If it is not possible, then the
3527 -- exception Cannot_Be_Static is raised.
3529 -----------------------------
3530 -- Ensure_Expression_Is_SA --
3531 -----------------------------
3533 procedure Ensure_Expression_Is_SA (N : Node_Id) is
3537 Ensure_Type_Is_SA (Etype (N));
3539 if Is_Static_Expression (N) then
3542 elsif Nkind (N) = N_Identifier then
3546 and then Ekind (Ent) = E_Constant
3547 and then Is_Library_Level_Entity (Ent)
3553 raise Cannot_Be_Static;
3554 end Ensure_Expression_Is_SA;
3556 -----------------------
3557 -- Ensure_Type_Is_SA --
3558 -----------------------
3560 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
3565 -- If type is library level, we are all set
3567 if Is_Library_Level_Entity (Typ) then
3571 -- We are also OK if the type is already marked as statically
3572 -- allocated, which means we processed it before.
3574 if Is_Statically_Allocated (Typ) then
3578 -- Mark type as statically allocated
3580 Set_Is_Statically_Allocated (Typ);
3582 -- Check that it is safe to statically allocate this type
3584 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
3585 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
3586 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
3588 elsif Is_Array_Type (Typ) then
3589 N := First_Index (Typ);
3590 while Present (N) loop
3591 Ensure_Type_Is_SA (Etype (N));
3595 Ensure_Type_Is_SA (Component_Type (Typ));
3597 elsif Is_Access_Type (Typ) then
3598 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
3602 T : constant Entity_Id := Etype (Designated_Type (Typ));
3605 if T /= Standard_Void_Type then
3606 Ensure_Type_Is_SA (T);
3609 F := First_Formal (Designated_Type (Typ));
3611 while Present (F) loop
3612 Ensure_Type_Is_SA (Etype (F));
3618 Ensure_Type_Is_SA (Designated_Type (Typ));
3621 elsif Is_Record_Type (Typ) then
3622 C := First_Entity (Typ);
3624 while Present (C) loop
3625 if Ekind (C) = E_Discriminant
3626 or else Ekind (C) = E_Component
3628 Ensure_Type_Is_SA (Etype (C));
3630 elsif Is_Type (C) then
3631 Ensure_Type_Is_SA (C);
3637 elsif Ekind (Typ) = E_Subprogram_Type then
3638 Ensure_Type_Is_SA (Etype (Typ));
3640 C := First_Formal (Typ);
3641 while Present (C) loop
3642 Ensure_Type_Is_SA (Etype (C));
3647 raise Cannot_Be_Static;
3649 end Ensure_Type_Is_SA;
3651 -- Start of processing for Freeze_Static_Object
3654 Ensure_Type_Is_SA (Etype (E));
3657 when Cannot_Be_Static =>
3659 -- If the object that cannot be static is imported or exported,
3660 -- then we give an error message saying that this object cannot
3661 -- be imported or exported.
3663 if Is_Imported (E) then
3665 ("& cannot be imported (local type is not constant)", E);
3667 -- Otherwise must be exported, something is wrong if compiler
3668 -- is marking something as statically allocated which cannot be).
3670 else pragma Assert (Is_Exported (E));
3672 ("& cannot be exported (local type is not constant)", E);
3674 end Freeze_Static_Object;
3676 -----------------------
3677 -- Freeze_Subprogram --
3678 -----------------------
3680 procedure Freeze_Subprogram (E : Entity_Id) is
3685 -- Subprogram may not have an address clause unless it is imported
3687 if Present (Address_Clause (E)) then
3688 if not Is_Imported (E) then
3690 ("address clause can only be given " &
3691 "for imported subprogram",
3692 Name (Address_Clause (E)));
3696 -- For non-foreign convention subprograms, this is where we create
3697 -- the extra formals (for accessibility level and constrained bit
3698 -- information). We delay this till the freeze point precisely so
3699 -- that we know the convention!
3701 if not Has_Foreign_Convention (E) then
3702 Create_Extra_Formals (E);
3705 -- If this is convention Ada and a Valued_Procedure, that's odd
3707 if Ekind (E) = E_Procedure
3708 and then Is_Valued_Procedure (E)
3709 and then Convention (E) = Convention_Ada
3712 ("?Valued_Procedure has no effect for convention Ada", E);
3713 Set_Is_Valued_Procedure (E, False);
3716 -- Case of foreign convention
3721 -- For foreign conventions, do not permit return of an
3722 -- unconstrained array.
3724 -- Note: we *do* allow a return by descriptor for the VMS case,
3725 -- though here there is probably more to be done ???
3727 if Ekind (E) = E_Function then
3728 Retype := Underlying_Type (Etype (E));
3730 -- If no return type, probably some other error, e.g. a
3731 -- missing full declaration, so ignore.
3736 -- If the return type is generic, we have emitted a warning
3737 -- earlier on, and there is nothing else to check here.
3738 -- Specific instantiations may lead to erroneous behavior.
3740 elsif Is_Generic_Type (Etype (E)) then
3743 elsif Is_Array_Type (Retype)
3744 and then not Is_Constrained (Retype)
3745 and then Mechanism (E) not in Descriptor_Codes
3748 ("convention for& does not permit returning " &
3749 "unconstrained array type", E, E);
3754 -- If any of the formals for an exported foreign convention
3755 -- subprogram have defaults, then emit an appropriate warning
3756 -- since this is odd (default cannot be used from non-Ada code)
3758 if Is_Exported (E) then
3759 F := First_Formal (E);
3760 while Present (F) loop
3761 if Present (Default_Value (F)) then
3763 ("?parameter cannot be defaulted in non-Ada call",
3772 -- For VMS, descriptor mechanisms for parameters are allowed only
3773 -- for imported subprograms.
3775 if OpenVMS_On_Target then
3776 if not Is_Imported (E) then
3777 F := First_Formal (E);
3778 while Present (F) loop
3779 if Mechanism (F) in Descriptor_Codes then
3781 ("descriptor mechanism for parameter not permitted", F);
3783 ("\can only be used for imported subprogram", F);
3791 end Freeze_Subprogram;
3793 -----------------------
3794 -- Is_Fully_Defined --
3795 -----------------------
3797 -- Should this be in Sem_Util ???
3799 function Is_Fully_Defined (T : Entity_Id) return Boolean is
3801 if Ekind (T) = E_Class_Wide_Type then
3802 return Is_Fully_Defined (Etype (T));
3804 return not Is_Private_Type (T)
3805 or else Present (Full_View (Base_Type (T)));
3807 end Is_Fully_Defined;
3809 ---------------------------------
3810 -- Process_Default_Expressions --
3811 ---------------------------------
3813 procedure Process_Default_Expressions
3815 After : in out Node_Id)
3817 Loc : constant Source_Ptr := Sloc (E);
3824 Set_Default_Expressions_Processed (E);
3826 -- A subprogram instance and its associated anonymous subprogram
3827 -- share their signature. The default expression functions are defined
3828 -- in the wrapper packages for the anonymous subprogram, and should
3829 -- not be generated again for the instance.
3831 if Is_Generic_Instance (E)
3832 and then Present (Alias (E))
3833 and then Default_Expressions_Processed (Alias (E))
3838 Formal := First_Formal (E);
3840 while Present (Formal) loop
3841 if Present (Default_Value (Formal)) then
3843 -- We work with a copy of the default expression because we
3844 -- do not want to disturb the original, since this would mess
3845 -- up the conformance checking.
3847 Dcopy := New_Copy_Tree (Default_Value (Formal));
3849 -- The analysis of the expression may generate insert actions,
3850 -- which of course must not be executed. We wrap those actions
3851 -- in a procedure that is not called, and later on eliminated.
3852 -- The following cases have no side-effects, and are analyzed
3855 if Nkind (Dcopy) = N_Identifier
3856 or else Nkind (Dcopy) = N_Expanded_Name
3857 or else Nkind (Dcopy) = N_Integer_Literal
3858 or else (Nkind (Dcopy) = N_Real_Literal
3859 and then not Vax_Float (Etype (Dcopy)))
3860 or else Nkind (Dcopy) = N_Character_Literal
3861 or else Nkind (Dcopy) = N_String_Literal
3862 or else Nkind (Dcopy) = N_Null
3863 or else (Nkind (Dcopy) = N_Attribute_Reference
3865 Attribute_Name (Dcopy) = Name_Null_Parameter)
3869 -- If there is no default function, we must still do a full
3870 -- analyze call on the default value, to ensure that all
3871 -- error checks are performed, e.g. those associated with
3872 -- static evaluation. Note that this branch will always be
3873 -- taken if the analyzer is turned off (but we still need the
3876 -- Note: the setting of parent here is to meet the requirement
3877 -- that we can only analyze the expression while attached to
3878 -- the tree. Really the requirement is that the parent chain
3879 -- be set, we don't actually need to be in the tree.
3881 Set_Parent (Dcopy, Declaration_Node (Formal));
3884 -- Default expressions are resolved with their own type if the
3885 -- context is generic, to avoid anomalies with private types.
3887 if Ekind (Scope (E)) = E_Generic_Package then
3888 Resolve (Dcopy, Etype (Dcopy));
3890 Resolve (Dcopy, Etype (Formal));
3893 -- If that resolved expression will raise constraint error,
3894 -- then flag the default value as raising constraint error.
3895 -- This allows a proper error message on the calls.
3897 if Raises_Constraint_Error (Dcopy) then
3898 Set_Raises_Constraint_Error (Default_Value (Formal));
3901 -- If the default is a parameterless call, we use the name of
3902 -- the called function directly, and there is no body to build.
3904 elsif Nkind (Dcopy) = N_Function_Call
3905 and then No (Parameter_Associations (Dcopy))
3909 -- Else construct and analyze the body of a wrapper procedure
3910 -- that contains an object declaration to hold the expression.
3911 -- Given that this is done only to complete the analysis, it
3912 -- simpler to build a procedure than a function which might
3913 -- involve secondary stack expansion.
3917 Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
3920 Make_Subprogram_Body (Loc,
3922 Make_Procedure_Specification (Loc,
3923 Defining_Unit_Name => Dnam),
3925 Declarations => New_List (
3926 Make_Object_Declaration (Loc,
3927 Defining_Identifier =>
3928 Make_Defining_Identifier (Loc,
3929 New_Internal_Name ('T')),
3930 Object_Definition =>
3931 New_Occurrence_Of (Etype (Formal), Loc),
3932 Expression => New_Copy_Tree (Dcopy))),
3934 Handled_Statement_Sequence =>
3935 Make_Handled_Sequence_Of_Statements (Loc,
3936 Statements => New_List));
3938 Set_Scope (Dnam, Scope (E));
3939 Set_Assignment_OK (First (Declarations (Dbody)));
3940 Set_Is_Eliminated (Dnam);
3941 Insert_After (After, Dbody);
3947 Next_Formal (Formal);
3950 end Process_Default_Expressions;
3952 ----------------------------------------
3953 -- Set_Component_Alignment_If_Not_Set --
3954 ----------------------------------------
3956 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
3958 -- Ignore if not base type, subtypes don't need anything
3960 if Typ /= Base_Type (Typ) then
3964 -- Do not override existing representation
3966 if Is_Packed (Typ) then
3969 elsif Has_Specified_Layout (Typ) then
3972 elsif Component_Alignment (Typ) /= Calign_Default then
3976 Set_Component_Alignment
3977 (Typ, Scope_Stack.Table
3978 (Scope_Stack.Last).Component_Alignment_Default);
3980 end Set_Component_Alignment_If_Not_Set;
3982 ---------------------------
3983 -- Set_Debug_Info_Needed --
3984 ---------------------------
3986 procedure Set_Debug_Info_Needed (T : Entity_Id) is
3989 or else Needs_Debug_Info (T)
3990 or else Debug_Info_Off (T)
3994 Set_Needs_Debug_Info (T);
3997 if Is_Object (T) then
3998 Set_Debug_Info_Needed (Etype (T));
4000 elsif Is_Type (T) then
4001 Set_Debug_Info_Needed (Etype (T));
4003 if Is_Record_Type (T) then
4005 Ent : Entity_Id := First_Entity (T);
4007 while Present (Ent) loop
4008 Set_Debug_Info_Needed (Ent);
4013 elsif Is_Array_Type (T) then
4014 Set_Debug_Info_Needed (Component_Type (T));
4017 Indx : Node_Id := First_Index (T);
4019 while Present (Indx) loop
4020 Set_Debug_Info_Needed (Etype (Indx));
4021 Indx := Next_Index (Indx);
4025 if Is_Packed (T) then
4026 Set_Debug_Info_Needed (Packed_Array_Type (T));
4029 elsif Is_Access_Type (T) then
4030 Set_Debug_Info_Needed (Directly_Designated_Type (T));
4032 elsif Is_Private_Type (T) then
4033 Set_Debug_Info_Needed (Full_View (T));
4035 elsif Is_Protected_Type (T) then
4036 Set_Debug_Info_Needed (Corresponding_Record_Type (T));
4040 end Set_Debug_Info_Needed;