1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- You should have received a copy of the GNU General Public License along --
19 -- with this program; see file COPYING3. If not see --
20 -- <http://www.gnu.org/licenses/>. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Ch3; use Exp_Ch3;
33 with Exp_Ch7; use Exp_Ch7;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Pakd; use Exp_Pakd;
36 with Exp_Util; use Exp_Util;
37 with Exp_Tss; use Exp_Tss;
38 with Layout; use Layout;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Nmake; use Nmake;
44 with Restrict; use Restrict;
45 with Rident; use Rident;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Eval; use Sem_Eval;
54 with Sem_Mech; use Sem_Mech;
55 with Sem_Prag; use Sem_Prag;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sinfo; use Sinfo;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Targparm; use Targparm;
62 with Tbuild; use Tbuild;
63 with Ttypes; use Ttypes;
64 with Uintp; use Uintp;
65 with Urealp; use Urealp;
67 package body Freeze is
69 -----------------------
70 -- Local Subprograms --
71 -----------------------
73 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
74 -- Typ is a type that is being frozen. If no size clause is given,
75 -- but a default Esize has been computed, then this default Esize is
76 -- adjusted up if necessary to be consistent with a given alignment,
77 -- but never to a value greater than Long_Long_Integer'Size. This
78 -- is used for all discrete types and for fixed-point types.
80 procedure Build_And_Analyze_Renamed_Body
83 After : in out Node_Id);
84 -- Build body for a renaming declaration, insert in tree and analyze
86 procedure Check_Address_Clause (E : Entity_Id);
87 -- Apply legality checks to address clauses for object declarations,
88 -- at the point the object is frozen.
90 procedure Check_Strict_Alignment (E : Entity_Id);
91 -- E is a base type. If E is tagged or has a component that is aliased
92 -- or tagged or contains something this is aliased or tagged, set
95 procedure Check_Unsigned_Type (E : Entity_Id);
96 pragma Inline (Check_Unsigned_Type);
97 -- If E is a fixed-point or discrete type, then all the necessary work
98 -- to freeze it is completed except for possible setting of the flag
99 -- Is_Unsigned_Type, which is done by this procedure. The call has no
100 -- effect if the entity E is not a discrete or fixed-point type.
102 procedure Freeze_And_Append
105 Result : in out List_Id);
106 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
107 -- nodes to Result, modifying Result from No_List if necessary.
109 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
110 -- Freeze enumeration type. The Esize field is set as processing
111 -- proceeds (i.e. set by default when the type is declared and then
112 -- adjusted by rep clauses. What this procedure does is to make sure
113 -- that if a foreign convention is specified, and no specific size
114 -- is given, then the size must be at least Integer'Size.
116 procedure Freeze_Static_Object (E : Entity_Id);
117 -- If an object is frozen which has Is_Statically_Allocated set, then
118 -- all referenced types must also be marked with this flag. This routine
119 -- is in charge of meeting this requirement for the object entity E.
121 procedure Freeze_Subprogram (E : Entity_Id);
122 -- Perform freezing actions for a subprogram (create extra formals,
123 -- and set proper default mechanism values). Note that this routine
124 -- is not called for internal subprograms, for which neither of these
125 -- actions is needed (or desirable, we do not want for example to have
126 -- these extra formals present in initialization procedures, where they
127 -- would serve no purpose). In this call E is either a subprogram or
128 -- a subprogram type (i.e. an access to a subprogram).
130 function Is_Fully_Defined (T : Entity_Id) return Boolean;
131 -- True if T is not private and has no private components, or has a full
132 -- view. Used to determine whether the designated type of an access type
133 -- should be frozen when the access type is frozen. This is done when an
134 -- allocator is frozen, or an expression that may involve attributes of
135 -- the designated type. Otherwise freezing the access type does not freeze
136 -- the designated type.
138 procedure Process_Default_Expressions
140 After : in out Node_Id);
141 -- This procedure is called for each subprogram to complete processing
142 -- of default expressions at the point where all types are known to be
143 -- frozen. The expressions must be analyzed in full, to make sure that
144 -- all error processing is done (they have only been pre-analyzed). If
145 -- the expression is not an entity or literal, its analysis may generate
146 -- code which must not be executed. In that case we build a function
147 -- body to hold that code. This wrapper function serves no other purpose
148 -- (it used to be called to evaluate the default, but now the default is
149 -- inlined at each point of call).
151 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
152 -- Typ is a record or array type that is being frozen. This routine
153 -- sets the default component alignment from the scope stack values
154 -- if the alignment is otherwise not specified.
156 procedure Check_Debug_Info_Needed (T : Entity_Id);
157 -- As each entity is frozen, this routine is called to deal with the
158 -- setting of Debug_Info_Needed for the entity. This flag is set if
159 -- the entity comes from source, or if we are in Debug_Generated_Code
160 -- mode or if the -gnatdV debug flag is set. However, it never sets
161 -- the flag if Debug_Info_Off is set. This procedure also ensures that
162 -- subsidiary entities have the flag set as required.
164 procedure Undelay_Type (T : Entity_Id);
165 -- T is a type of a component that we know to be an Itype.
166 -- We don't want this to have a Freeze_Node, so ensure it doesn't.
167 -- Do the same for any Full_View or Corresponding_Record_Type.
169 procedure Warn_Overlay
173 -- Expr is the expression for an address clause for entity Nam whose type
174 -- is Typ. If Typ has a default initialization, and there is no explicit
175 -- initialization in the source declaration, check whether the address
176 -- clause might cause overlaying of an entity, and emit a warning on the
177 -- side effect that the initialization will cause.
179 -------------------------------
180 -- Adjust_Esize_For_Alignment --
181 -------------------------------
183 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
187 if Known_Esize (Typ) and then Known_Alignment (Typ) then
188 Align := Alignment_In_Bits (Typ);
190 if Align > Esize (Typ)
191 and then Align <= Standard_Long_Long_Integer_Size
193 Set_Esize (Typ, Align);
196 end Adjust_Esize_For_Alignment;
198 ------------------------------------
199 -- Build_And_Analyze_Renamed_Body --
200 ------------------------------------
202 procedure Build_And_Analyze_Renamed_Body
205 After : in out Node_Id)
207 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
208 Ent : constant Entity_Id := Defining_Entity (Decl);
210 Renamed_Subp : Entity_Id;
214 -- If the renamed subprogram is intrinsic, there is no need for a
215 -- wrapper body: we set the alias that will be called and expanded which
216 -- completes the declaration. This transformation is only legal if the
217 -- renamed entity has already been elaborated.
219 -- Note that it is legal for a renaming_as_body to rename an intrinsic
220 -- subprogram, as long as the renaming occurs before the new entity
221 -- is frozen. See RM 8.5.4 (5).
223 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
224 and then Is_Entity_Name (Name (Body_Decl))
226 Renamed_Subp := Entity (Name (Body_Decl));
228 Renamed_Subp := Empty;
231 if Present (Renamed_Subp)
232 and then Is_Intrinsic_Subprogram (Renamed_Subp)
233 and then Present (Interface_Name (Renamed_Subp))
235 (not In_Same_Source_Unit (Renamed_Subp, Ent)
236 or else Sloc (Renamed_Subp) < Sloc (Ent))
238 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
239 if Present (Alias (Renamed_Subp)) then
240 Set_Alias (Ent, Alias (Renamed_Subp));
242 Set_Alias (Ent, Renamed_Subp);
245 Set_Is_Intrinsic_Subprogram (Ent);
246 Set_Has_Completion (Ent);
249 Body_Node := Build_Renamed_Body (Decl, New_S);
250 Insert_After (After, Body_Node);
251 Mark_Rewrite_Insertion (Body_Node);
255 end Build_And_Analyze_Renamed_Body;
257 ------------------------
258 -- Build_Renamed_Body --
259 ------------------------
261 function Build_Renamed_Body
263 New_S : Entity_Id) return Node_Id
265 Loc : constant Source_Ptr := Sloc (New_S);
266 -- We use for the source location of the renamed body, the location
267 -- of the spec entity. It might seem more natural to use the location
268 -- of the renaming declaration itself, but that would be wrong, since
269 -- then the body we create would look as though it was created far
270 -- too late, and this could cause problems with elaboration order
271 -- analysis, particularly in connection with instantiations.
273 N : constant Node_Id := Unit_Declaration_Node (New_S);
274 Nam : constant Node_Id := Name (N);
276 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
277 Actuals : List_Id := No_List;
282 O_Formal : Entity_Id;
283 Param_Spec : Node_Id;
285 Pref : Node_Id := Empty;
286 -- If the renamed entity is a primitive operation given in prefix form,
287 -- the prefix is the target object and it has to be added as the first
288 -- actual in the generated call.
291 -- Determine the entity being renamed, which is the target of the call
292 -- statement. If the name is an explicit dereference, this is a renaming
293 -- of a subprogram type rather than a subprogram. The name itself is
296 if Nkind (Nam) = N_Selected_Component then
297 Old_S := Entity (Selector_Name (Nam));
299 elsif Nkind (Nam) = N_Explicit_Dereference then
300 Old_S := Etype (Nam);
302 elsif Nkind (Nam) = N_Indexed_Component then
303 if Is_Entity_Name (Prefix (Nam)) then
304 Old_S := Entity (Prefix (Nam));
306 Old_S := Entity (Selector_Name (Prefix (Nam)));
309 elsif Nkind (Nam) = N_Character_Literal then
310 Old_S := Etype (New_S);
313 Old_S := Entity (Nam);
316 if Is_Entity_Name (Nam) then
318 -- If the renamed entity is a predefined operator, retain full name
319 -- to ensure its visibility.
321 if Ekind (Old_S) = E_Operator
322 and then Nkind (Nam) = N_Expanded_Name
324 Call_Name := New_Copy (Name (N));
326 Call_Name := New_Reference_To (Old_S, Loc);
330 if Nkind (Nam) = N_Selected_Component
331 and then Present (First_Formal (Old_S))
333 (Is_Controlling_Formal (First_Formal (Old_S))
334 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
337 -- Retrieve the target object, to be added as a first actual
340 Call_Name := New_Occurrence_Of (Old_S, Loc);
341 Pref := Prefix (Nam);
344 Call_Name := New_Copy (Name (N));
347 -- The original name may have been overloaded, but
348 -- is fully resolved now.
350 Set_Is_Overloaded (Call_Name, False);
353 -- For simple renamings, subsequent calls can be expanded directly as
354 -- calls to the renamed entity. The body must be generated in any case
355 -- for calls that may appear elsewhere.
357 if (Ekind (Old_S) = E_Function
358 or else Ekind (Old_S) = E_Procedure)
359 and then Nkind (Decl) = N_Subprogram_Declaration
361 Set_Body_To_Inline (Decl, Old_S);
364 -- The body generated for this renaming is an internal artifact, and
365 -- does not constitute a freeze point for the called entity.
367 Set_Must_Not_Freeze (Call_Name);
369 Formal := First_Formal (Defining_Entity (Decl));
371 if Present (Pref) then
373 Pref_Type : constant Entity_Id := Etype (Pref);
374 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
378 -- The controlling formal may be an access parameter, or the
379 -- actual may be an access value, so adjust accordingly.
381 if Is_Access_Type (Pref_Type)
382 and then not Is_Access_Type (Form_Type)
385 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
387 elsif Is_Access_Type (Form_Type)
388 and then not Is_Access_Type (Pref)
391 (Make_Attribute_Reference (Loc,
392 Attribute_Name => Name_Access,
393 Prefix => Relocate_Node (Pref)));
395 Actuals := New_List (Pref);
399 elsif Present (Formal) then
406 if Present (Formal) then
407 while Present (Formal) loop
408 Append (New_Reference_To (Formal, Loc), Actuals);
409 Next_Formal (Formal);
413 -- If the renamed entity is an entry, inherit its profile. For other
414 -- renamings as bodies, both profiles must be subtype conformant, so it
415 -- is not necessary to replace the profile given in the declaration.
416 -- However, default values that are aggregates are rewritten when
417 -- partially analyzed, so we recover the original aggregate to insure
418 -- that subsequent conformity checking works. Similarly, if the default
419 -- expression was constant-folded, recover the original expression.
421 Formal := First_Formal (Defining_Entity (Decl));
423 if Present (Formal) then
424 O_Formal := First_Formal (Old_S);
425 Param_Spec := First (Parameter_Specifications (Spec));
427 while Present (Formal) loop
428 if Is_Entry (Old_S) then
430 if Nkind (Parameter_Type (Param_Spec)) /=
433 Set_Etype (Formal, Etype (O_Formal));
434 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
437 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
438 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
439 Nkind (Default_Value (O_Formal))
441 Set_Expression (Param_Spec,
442 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
445 Next_Formal (Formal);
446 Next_Formal (O_Formal);
451 -- If the renamed entity is a function, the generated body contains a
452 -- return statement. Otherwise, build a procedure call. If the entity is
453 -- an entry, subsequent analysis of the call will transform it into the
454 -- proper entry or protected operation call. If the renamed entity is
455 -- a character literal, return it directly.
457 if Ekind (Old_S) = E_Function
458 or else Ekind (Old_S) = E_Operator
459 or else (Ekind (Old_S) = E_Subprogram_Type
460 and then Etype (Old_S) /= Standard_Void_Type)
463 Make_Simple_Return_Statement (Loc,
465 Make_Function_Call (Loc,
467 Parameter_Associations => Actuals));
469 elsif Ekind (Old_S) = E_Enumeration_Literal then
471 Make_Simple_Return_Statement (Loc,
472 Expression => New_Occurrence_Of (Old_S, Loc));
474 elsif Nkind (Nam) = N_Character_Literal then
476 Make_Simple_Return_Statement (Loc,
477 Expression => Call_Name);
481 Make_Procedure_Call_Statement (Loc,
483 Parameter_Associations => Actuals);
486 -- Create entities for subprogram body and formals
488 Set_Defining_Unit_Name (Spec,
489 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
491 Param_Spec := First (Parameter_Specifications (Spec));
493 while Present (Param_Spec) loop
494 Set_Defining_Identifier (Param_Spec,
495 Make_Defining_Identifier (Loc,
496 Chars => Chars (Defining_Identifier (Param_Spec))));
501 Make_Subprogram_Body (Loc,
502 Specification => Spec,
503 Declarations => New_List,
504 Handled_Statement_Sequence =>
505 Make_Handled_Sequence_Of_Statements (Loc,
506 Statements => New_List (Call_Node)));
508 if Nkind (Decl) /= N_Subprogram_Declaration then
510 Make_Subprogram_Declaration (Loc,
511 Specification => Specification (N)));
514 -- Link the body to the entity whose declaration it completes. If
515 -- the body is analyzed when the renamed entity is frozen, it may
516 -- be necessary to restore the proper scope (see package Exp_Ch13).
518 if Nkind (N) = N_Subprogram_Renaming_Declaration
519 and then Present (Corresponding_Spec (N))
521 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
523 Set_Corresponding_Spec (Body_Node, New_S);
527 end Build_Renamed_Body;
529 --------------------------
530 -- Check_Address_Clause --
531 --------------------------
533 procedure Check_Address_Clause (E : Entity_Id) is
534 Addr : constant Node_Id := Address_Clause (E);
536 Decl : constant Node_Id := Declaration_Node (E);
537 Typ : constant Entity_Id := Etype (E);
540 if Present (Addr) then
541 Expr := Expression (Addr);
543 -- If we have no initialization of any kind, then we don't need to
544 -- place any restrictions on the address clause, because the object
545 -- will be elaborated after the address clause is evaluated. This
546 -- happens if the declaration has no initial expression, or the type
547 -- has no implicit initialization, or the object is imported.
549 -- The same holds for all initialized scalar types and all access
550 -- types. Packed bit arrays of size up to 64 are represented using a
551 -- modular type with an initialization (to zero) and can be processed
552 -- like other initialized scalar types.
554 -- If the type is controlled, code to attach the object to a
555 -- finalization chain is generated at the point of declaration,
556 -- and therefore the elaboration of the object cannot be delayed:
557 -- the address expression must be a constant.
559 if (No (Expression (Decl))
560 and then not Needs_Finalization (Typ)
562 (not Has_Non_Null_Base_Init_Proc (Typ)
563 or else Is_Imported (E)))
566 (Present (Expression (Decl))
567 and then Is_Scalar_Type (Typ))
573 (Is_Bit_Packed_Array (Typ)
575 Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
579 -- Otherwise, we require the address clause to be constant because
580 -- the call to the initialization procedure (or the attach code) has
581 -- to happen at the point of the declaration.
582 -- Actually the IP call has been moved to the freeze actions
583 -- anyway, so maybe we can relax this restriction???
586 Check_Constant_Address_Clause (Expr, E);
588 -- Has_Delayed_Freeze was set on E when the address clause was
589 -- analyzed. Reset the flag now unless freeze actions were
590 -- attached to it in the mean time.
592 if No (Freeze_Node (E)) then
593 Set_Has_Delayed_Freeze (E, False);
597 if not Error_Posted (Expr)
598 and then not Needs_Finalization (Typ)
600 Warn_Overlay (Expr, Typ, Name (Addr));
603 end Check_Address_Clause;
605 -----------------------------
606 -- Check_Compile_Time_Size --
607 -----------------------------
609 procedure Check_Compile_Time_Size (T : Entity_Id) is
611 procedure Set_Small_Size (T : Entity_Id; S : Uint);
612 -- Sets the compile time known size (32 bits or less) in the Esize
613 -- field, of T checking for a size clause that was given which attempts
614 -- to give a smaller size, and also checking for an alignment clause.
616 function Size_Known (T : Entity_Id) return Boolean;
617 -- Recursive function that does all the work
619 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
620 -- If T is a constrained subtype, its size is not known if any of its
621 -- discriminant constraints is not static and it is not a null record.
622 -- The test is conservative and doesn't check that the components are
623 -- in fact constrained by non-static discriminant values. Could be made
630 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
635 -- Don't bother if alignment clause with a value other than 1 is
636 -- present, because size may be padded up to meet back end alignment
637 -- requirements, and only the back end knows the rules!
639 elsif Known_Alignment (T) and then Alignment (T) /= 1 then
642 -- Check for bad size clause given
644 elsif Has_Size_Clause (T) then
645 if RM_Size (T) < S then
646 Error_Msg_Uint_1 := S;
648 ("size for& too small, minimum allowed is ^",
651 elsif Unknown_Esize (T) then
655 -- Set sizes if not set already
658 if Unknown_Esize (T) then
662 if Unknown_RM_Size (T) then
672 function Size_Known (T : Entity_Id) return Boolean is
680 if Size_Known_At_Compile_Time (T) then
683 -- Always True for scalar types. This is true even for generic formal
684 -- scalar types. We used to return False in the latter case, but the
685 -- size is known at compile time, even in the template, we just do
686 -- not know the exact size but that's not the point of this routine.
688 elsif Is_Scalar_Type (T)
689 or else Is_Task_Type (T)
695 elsif Is_Array_Type (T) then
697 -- String literals always have known size, and we can set it
699 if Ekind (T) = E_String_Literal_Subtype then
700 Set_Small_Size (T, Component_Size (T)
701 * String_Literal_Length (T));
704 -- Unconstrained types never have known at compile time size
706 elsif not Is_Constrained (T) then
709 -- Don't do any recursion on type with error posted, since we may
710 -- have a malformed type that leads us into a loop.
712 elsif Error_Posted (T) then
715 -- Otherwise if component size unknown, then array size unknown
717 elsif not Size_Known (Component_Type (T)) then
721 -- Check for all indexes static, and also compute possible size
722 -- (in case it is less than 32 and may be packable).
725 Esiz : Uint := Component_Size (T);
729 Index := First_Index (T);
730 while Present (Index) loop
731 if Nkind (Index) = N_Range then
732 Get_Index_Bounds (Index, Low, High);
734 elsif Error_Posted (Scalar_Range (Etype (Index))) then
738 Low := Type_Low_Bound (Etype (Index));
739 High := Type_High_Bound (Etype (Index));
742 if not Compile_Time_Known_Value (Low)
743 or else not Compile_Time_Known_Value (High)
744 or else Etype (Index) = Any_Type
749 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
761 Set_Small_Size (T, Esiz);
765 -- Access types always have known at compile time sizes
767 elsif Is_Access_Type (T) then
770 -- For non-generic private types, go to underlying type if present
772 elsif Is_Private_Type (T)
773 and then not Is_Generic_Type (T)
774 and then Present (Underlying_Type (T))
776 -- Don't do any recursion on type with error posted, since we may
777 -- have a malformed type that leads us into a loop.
779 if Error_Posted (T) then
782 return Size_Known (Underlying_Type (T));
787 elsif Is_Record_Type (T) then
789 -- A class-wide type is never considered to have a known size
791 if Is_Class_Wide_Type (T) then
794 -- A subtype of a variant record must not have non-static
795 -- discriminanted components.
797 elsif T /= Base_Type (T)
798 and then not Static_Discriminated_Components (T)
802 -- Don't do any recursion on type with error posted, since we may
803 -- have a malformed type that leads us into a loop.
805 elsif Error_Posted (T) then
809 -- Now look at the components of the record
812 -- The following two variables are used to keep track of the
813 -- size of packed records if we can tell the size of the packed
814 -- record in the front end. Packed_Size_Known is True if so far
815 -- we can figure out the size. It is initialized to True for a
816 -- packed record, unless the record has discriminants. The
817 -- reason we eliminate the discriminated case is that we don't
818 -- know the way the back end lays out discriminated packed
819 -- records. If Packed_Size_Known is True, then Packed_Size is
820 -- the size in bits so far.
822 Packed_Size_Known : Boolean :=
824 and then not Has_Discriminants (T);
826 Packed_Size : Uint := Uint_0;
829 -- Test for variant part present
831 if Has_Discriminants (T)
832 and then Present (Parent (T))
833 and then Nkind (Parent (T)) = N_Full_Type_Declaration
834 and then Nkind (Type_Definition (Parent (T))) =
836 and then not Null_Present (Type_Definition (Parent (T)))
837 and then Present (Variant_Part
838 (Component_List (Type_Definition (Parent (T)))))
840 -- If variant part is present, and type is unconstrained,
841 -- then we must have defaulted discriminants, or a size
842 -- clause must be present for the type, or else the size
843 -- is definitely not known at compile time.
845 if not Is_Constrained (T)
847 No (Discriminant_Default_Value
848 (First_Discriminant (T)))
849 and then Unknown_Esize (T)
855 -- Loop through components
857 Comp := First_Component_Or_Discriminant (T);
858 while Present (Comp) loop
859 Ctyp := Etype (Comp);
861 -- We do not know the packed size if there is a component
862 -- clause present (we possibly could, but this would only
863 -- help in the case of a record with partial rep clauses.
864 -- That's because in the case of full rep clauses, the
865 -- size gets figured out anyway by a different circuit).
867 if Present (Component_Clause (Comp)) then
868 Packed_Size_Known := False;
871 -- We need to identify a component that is an array where
872 -- the index type is an enumeration type with non-standard
873 -- representation, and some bound of the type depends on a
876 -- This is because gigi computes the size by doing a
877 -- substitution of the appropriate discriminant value in
878 -- the size expression for the base type, and gigi is not
879 -- clever enough to evaluate the resulting expression (which
880 -- involves a call to rep_to_pos) at compile time.
882 -- It would be nice if gigi would either recognize that
883 -- this expression can be computed at compile time, or
884 -- alternatively figured out the size from the subtype
885 -- directly, where all the information is at hand ???
887 if Is_Array_Type (Etype (Comp))
888 and then Present (Packed_Array_Type (Etype (Comp)))
891 Ocomp : constant Entity_Id :=
892 Original_Record_Component (Comp);
893 OCtyp : constant Entity_Id := Etype (Ocomp);
899 Ind := First_Index (OCtyp);
900 while Present (Ind) loop
901 Indtyp := Etype (Ind);
903 if Is_Enumeration_Type (Indtyp)
904 and then Has_Non_Standard_Rep (Indtyp)
906 Lo := Type_Low_Bound (Indtyp);
907 Hi := Type_High_Bound (Indtyp);
909 if Is_Entity_Name (Lo)
910 and then Ekind (Entity (Lo)) = E_Discriminant
914 elsif Is_Entity_Name (Hi)
915 and then Ekind (Entity (Hi)) = E_Discriminant
926 -- Clearly size of record is not known if the size of one of
927 -- the components is not known.
929 if not Size_Known (Ctyp) then
933 -- Accumulate packed size if possible
935 if Packed_Size_Known then
937 -- We can only deal with elementary types, since for
938 -- non-elementary components, alignment enters into the
939 -- picture, and we don't know enough to handle proper
940 -- alignment in this context. Packed arrays count as
941 -- elementary if the representation is a modular type.
943 if Is_Elementary_Type (Ctyp)
944 or else (Is_Array_Type (Ctyp)
945 and then Present (Packed_Array_Type (Ctyp))
946 and then Is_Modular_Integer_Type
947 (Packed_Array_Type (Ctyp)))
949 -- If RM_Size is known and static, then we can keep
950 -- accumulating the packed size.
952 if Known_Static_RM_Size (Ctyp) then
954 -- A little glitch, to be removed sometime ???
955 -- gigi does not understand zero sizes yet.
957 if RM_Size (Ctyp) = Uint_0 then
958 Packed_Size_Known := False;
960 -- Normal case where we can keep accumulating the
961 -- packed array size.
964 Packed_Size := Packed_Size + RM_Size (Ctyp);
967 -- If we have a field whose RM_Size is not known then
968 -- we can't figure out the packed size here.
971 Packed_Size_Known := False;
974 -- If we have a non-elementary type we can't figure out
975 -- the packed array size (alignment issues).
978 Packed_Size_Known := False;
982 Next_Component_Or_Discriminant (Comp);
985 if Packed_Size_Known then
986 Set_Small_Size (T, Packed_Size);
992 -- All other cases, size not known at compile time
999 -------------------------------------
1000 -- Static_Discriminated_Components --
1001 -------------------------------------
1003 function Static_Discriminated_Components
1004 (T : Entity_Id) return Boolean
1006 Constraint : Elmt_Id;
1009 if Has_Discriminants (T)
1010 and then Present (Discriminant_Constraint (T))
1011 and then Present (First_Component (T))
1013 Constraint := First_Elmt (Discriminant_Constraint (T));
1014 while Present (Constraint) loop
1015 if not Compile_Time_Known_Value (Node (Constraint)) then
1019 Next_Elmt (Constraint);
1024 end Static_Discriminated_Components;
1026 -- Start of processing for Check_Compile_Time_Size
1029 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1030 end Check_Compile_Time_Size;
1032 -----------------------------
1033 -- Check_Debug_Info_Needed --
1034 -----------------------------
1036 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1038 if Debug_Info_Off (T) then
1041 elsif Comes_From_Source (T)
1042 or else Debug_Generated_Code
1043 or else Debug_Flag_VV
1044 or else Needs_Debug_Info (T)
1046 Set_Debug_Info_Needed (T);
1048 end Check_Debug_Info_Needed;
1050 ----------------------------
1051 -- Check_Strict_Alignment --
1052 ----------------------------
1054 procedure Check_Strict_Alignment (E : Entity_Id) is
1058 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1059 Set_Strict_Alignment (E);
1061 elsif Is_Array_Type (E) then
1062 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1064 elsif Is_Record_Type (E) then
1065 if Is_Limited_Record (E) then
1066 Set_Strict_Alignment (E);
1070 Comp := First_Component (E);
1072 while Present (Comp) loop
1073 if not Is_Type (Comp)
1074 and then (Strict_Alignment (Etype (Comp))
1075 or else Is_Aliased (Comp))
1077 Set_Strict_Alignment (E);
1081 Next_Component (Comp);
1084 end Check_Strict_Alignment;
1086 -------------------------
1087 -- Check_Unsigned_Type --
1088 -------------------------
1090 procedure Check_Unsigned_Type (E : Entity_Id) is
1091 Ancestor : Entity_Id;
1096 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1100 -- Do not attempt to analyze case where range was in error
1102 if Error_Posted (Scalar_Range (E)) then
1106 -- The situation that is non trivial is something like
1108 -- subtype x1 is integer range -10 .. +10;
1109 -- subtype x2 is x1 range 0 .. V1;
1110 -- subtype x3 is x2 range V2 .. V3;
1111 -- subtype x4 is x3 range V4 .. V5;
1113 -- where Vn are variables. Here the base type is signed, but we still
1114 -- know that x4 is unsigned because of the lower bound of x2.
1116 -- The only way to deal with this is to look up the ancestor chain
1120 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1124 Lo_Bound := Type_Low_Bound (Ancestor);
1126 if Compile_Time_Known_Value (Lo_Bound) then
1128 if Expr_Rep_Value (Lo_Bound) >= 0 then
1129 Set_Is_Unsigned_Type (E, True);
1135 Ancestor := Ancestor_Subtype (Ancestor);
1137 -- If no ancestor had a static lower bound, go to base type
1139 if No (Ancestor) then
1141 -- Note: the reason we still check for a compile time known
1142 -- value for the base type is that at least in the case of
1143 -- generic formals, we can have bounds that fail this test,
1144 -- and there may be other cases in error situations.
1146 Btyp := Base_Type (E);
1148 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1152 Lo_Bound := Type_Low_Bound (Base_Type (E));
1154 if Compile_Time_Known_Value (Lo_Bound)
1155 and then Expr_Rep_Value (Lo_Bound) >= 0
1157 Set_Is_Unsigned_Type (E, True);
1164 end Check_Unsigned_Type;
1166 -------------------------
1167 -- Is_Atomic_Aggregate --
1168 -------------------------
1170 function Is_Atomic_Aggregate
1172 Typ : Entity_Id) return Boolean
1174 Loc : constant Source_Ptr := Sloc (E);
1182 -- Array may be qualified, so find outer context
1184 if Nkind (Par) = N_Qualified_Expression then
1185 Par := Parent (Par);
1188 if Nkind_In (Par, N_Object_Declaration, N_Assignment_Statement)
1189 and then Comes_From_Source (Par)
1191 Temp := Make_Temporary (Loc, 'T', E);
1193 Make_Object_Declaration (Loc,
1194 Defining_Identifier => Temp,
1195 Object_Definition => New_Occurrence_Of (Typ, Loc),
1196 Expression => Relocate_Node (E));
1197 Insert_Before (Par, New_N);
1200 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1206 end Is_Atomic_Aggregate;
1212 -- Note: the easy coding for this procedure would be to just build a
1213 -- single list of freeze nodes and then insert them and analyze them
1214 -- all at once. This won't work, because the analysis of earlier freeze
1215 -- nodes may recursively freeze types which would otherwise appear later
1216 -- on in the freeze list. So we must analyze and expand the freeze nodes
1217 -- as they are generated.
1219 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1220 Loc : constant Source_Ptr := Sloc (After);
1224 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1225 -- This is the internal recursive routine that does freezing of entities
1226 -- (but NOT the analysis of default expressions, which should not be
1227 -- recursive, we don't want to analyze those till we are sure that ALL
1228 -- the types are frozen).
1230 --------------------
1231 -- Freeze_All_Ent --
1232 --------------------
1234 procedure Freeze_All_Ent
1236 After : in out Node_Id)
1242 procedure Process_Flist;
1243 -- If freeze nodes are present, insert and analyze, and reset cursor
1244 -- for next insertion.
1250 procedure Process_Flist is
1252 if Is_Non_Empty_List (Flist) then
1253 Lastn := Next (After);
1254 Insert_List_After_And_Analyze (After, Flist);
1256 if Present (Lastn) then
1257 After := Prev (Lastn);
1259 After := Last (List_Containing (After));
1264 -- Start or processing for Freeze_All_Ent
1268 while Present (E) loop
1270 -- If the entity is an inner package which is not a package
1271 -- renaming, then its entities must be frozen at this point. Note
1272 -- that such entities do NOT get frozen at the end of the nested
1273 -- package itself (only library packages freeze).
1275 -- Same is true for task declarations, where anonymous records
1276 -- created for entry parameters must be frozen.
1278 if Ekind (E) = E_Package
1279 and then No (Renamed_Object (E))
1280 and then not Is_Child_Unit (E)
1281 and then not Is_Frozen (E)
1284 Install_Visible_Declarations (E);
1285 Install_Private_Declarations (E);
1287 Freeze_All (First_Entity (E), After);
1289 End_Package_Scope (E);
1291 elsif Ekind (E) in Task_Kind
1293 (Nkind (Parent (E)) = N_Task_Type_Declaration
1295 Nkind (Parent (E)) = N_Single_Task_Declaration)
1298 Freeze_All (First_Entity (E), After);
1301 -- For a derived tagged type, we must ensure that all the
1302 -- primitive operations of the parent have been frozen, so that
1303 -- their addresses will be in the parent's dispatch table at the
1304 -- point it is inherited.
1306 elsif Ekind (E) = E_Record_Type
1307 and then Is_Tagged_Type (E)
1308 and then Is_Tagged_Type (Etype (E))
1309 and then Is_Derived_Type (E)
1312 Prim_List : constant Elist_Id :=
1313 Primitive_Operations (Etype (E));
1319 Prim := First_Elmt (Prim_List);
1321 while Present (Prim) loop
1322 Subp := Node (Prim);
1324 if Comes_From_Source (Subp)
1325 and then not Is_Frozen (Subp)
1327 Flist := Freeze_Entity (Subp, Loc);
1336 if not Is_Frozen (E) then
1337 Flist := Freeze_Entity (E, Loc);
1341 -- If an incomplete type is still not frozen, this may be a
1342 -- premature freezing because of a body declaration that follows.
1343 -- Indicate where the freezing took place.
1345 -- If the freezing is caused by the end of the current declarative
1346 -- part, it is a Taft Amendment type, and there is no error.
1348 if not Is_Frozen (E)
1349 and then Ekind (E) = E_Incomplete_Type
1352 Bod : constant Node_Id := Next (After);
1355 if (Nkind (Bod) = N_Subprogram_Body
1356 or else Nkind (Bod) = N_Entry_Body
1357 or else Nkind (Bod) = N_Package_Body
1358 or else Nkind (Bod) = N_Protected_Body
1359 or else Nkind (Bod) = N_Task_Body
1360 or else Nkind (Bod) in N_Body_Stub)
1362 List_Containing (After) = List_Containing (Parent (E))
1364 Error_Msg_Sloc := Sloc (Next (After));
1366 ("type& is frozen# before its full declaration",
1376 -- Start of processing for Freeze_All
1379 Freeze_All_Ent (From, After);
1381 -- Now that all types are frozen, we can deal with default expressions
1382 -- that require us to build a default expression functions. This is the
1383 -- point at which such functions are constructed (after all types that
1384 -- might be used in such expressions have been frozen).
1386 -- For subprograms that are renaming_as_body, we create the wrapper
1387 -- bodies as needed.
1389 -- We also add finalization chains to access types whose designated
1390 -- types are controlled. This is normally done when freezing the type,
1391 -- but this misses recursive type definitions where the later members
1392 -- of the recursion introduce controlled components.
1394 -- Loop through entities
1397 while Present (E) loop
1398 if Is_Subprogram (E) then
1400 if not Default_Expressions_Processed (E) then
1401 Process_Default_Expressions (E, After);
1404 if not Has_Completion (E) then
1405 Decl := Unit_Declaration_Node (E);
1407 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1408 Build_And_Analyze_Renamed_Body (Decl, E, After);
1410 elsif Nkind (Decl) = N_Subprogram_Declaration
1411 and then Present (Corresponding_Body (Decl))
1413 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1414 = N_Subprogram_Renaming_Declaration
1416 Build_And_Analyze_Renamed_Body
1417 (Decl, Corresponding_Body (Decl), After);
1421 elsif Ekind (E) in Task_Kind
1423 (Nkind (Parent (E)) = N_Task_Type_Declaration
1425 Nkind (Parent (E)) = N_Single_Task_Declaration)
1430 Ent := First_Entity (E);
1432 while Present (Ent) loop
1435 and then not Default_Expressions_Processed (Ent)
1437 Process_Default_Expressions (Ent, After);
1444 elsif Is_Access_Type (E)
1445 and then Comes_From_Source (E)
1446 and then Ekind (Directly_Designated_Type (E)) = E_Incomplete_Type
1447 and then Needs_Finalization (Designated_Type (E))
1448 and then No (Associated_Final_Chain (E))
1450 Build_Final_List (Parent (E), E);
1457 -----------------------
1458 -- Freeze_And_Append --
1459 -----------------------
1461 procedure Freeze_And_Append
1464 Result : in out List_Id)
1466 L : constant List_Id := Freeze_Entity (Ent, Loc);
1468 if Is_Non_Empty_List (L) then
1469 if Result = No_List then
1472 Append_List (L, Result);
1475 end Freeze_And_Append;
1481 procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
1482 Freeze_Nodes : constant List_Id := Freeze_Entity (T, Sloc (N));
1484 if Is_Non_Empty_List (Freeze_Nodes) then
1485 Insert_Actions (N, Freeze_Nodes);
1493 function Freeze_Entity (E : Entity_Id; Loc : Source_Ptr) return List_Id is
1494 Test_E : Entity_Id := E;
1502 Has_Default_Initialization : Boolean := False;
1503 -- This flag gets set to true for a variable with default initialization
1505 procedure Check_Current_Instance (Comp_Decl : Node_Id);
1506 -- Check that an Access or Unchecked_Access attribute with a prefix
1507 -- which is the current instance type can only be applied when the type
1510 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
1511 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
1512 -- integer literal without an explicit corresponding size clause. The
1513 -- caller has checked that Utype is a modular integer type.
1515 function After_Last_Declaration return Boolean;
1516 -- If Loc is a freeze_entity that appears after the last declaration
1517 -- in the scope, inhibit error messages on late completion.
1519 procedure Freeze_Record_Type (Rec : Entity_Id);
1520 -- Freeze each component, handle some representation clauses, and freeze
1521 -- primitive operations if this is a tagged type.
1523 ----------------------------
1524 -- After_Last_Declaration --
1525 ----------------------------
1527 function After_Last_Declaration return Boolean is
1528 Spec : constant Node_Id := Parent (Current_Scope);
1530 if Nkind (Spec) = N_Package_Specification then
1531 if Present (Private_Declarations (Spec)) then
1532 return Loc >= Sloc (Last (Private_Declarations (Spec)));
1533 elsif Present (Visible_Declarations (Spec)) then
1534 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
1541 end After_Last_Declaration;
1543 ----------------------------
1544 -- Check_Current_Instance --
1545 ----------------------------
1547 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
1549 Rec_Type : constant Entity_Id :=
1550 Scope (Defining_Identifier (Comp_Decl));
1552 Decl : constant Node_Id := Parent (Rec_Type);
1554 function Process (N : Node_Id) return Traverse_Result;
1555 -- Process routine to apply check to given node
1561 function Process (N : Node_Id) return Traverse_Result is
1564 when N_Attribute_Reference =>
1565 if (Attribute_Name (N) = Name_Access
1567 Attribute_Name (N) = Name_Unchecked_Access)
1568 and then Is_Entity_Name (Prefix (N))
1569 and then Is_Type (Entity (Prefix (N)))
1570 and then Entity (Prefix (N)) = E
1573 ("current instance must be a limited type", Prefix (N));
1579 when others => return OK;
1583 procedure Traverse is new Traverse_Proc (Process);
1585 -- Start of processing for Check_Current_Instance
1588 -- In Ada95, the (imprecise) rule is that the current instance of a
1589 -- limited type is aliased. In Ada2005, limitedness must be explicit:
1590 -- either a tagged type, or a limited record.
1592 if Is_Limited_Type (Rec_Type)
1593 and then (Ada_Version < Ada_05 or else Is_Tagged_Type (Rec_Type))
1597 elsif Nkind (Decl) = N_Full_Type_Declaration
1598 and then Limited_Present (Type_Definition (Decl))
1603 Traverse (Comp_Decl);
1605 end Check_Current_Instance;
1607 ------------------------------
1608 -- Check_Suspicious_Modulus --
1609 ------------------------------
1611 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
1612 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
1615 if Nkind (Decl) = N_Full_Type_Declaration then
1617 Tdef : constant Node_Id := Type_Definition (Decl);
1619 if Nkind (Tdef) = N_Modular_Type_Definition then
1621 Modulus : constant Node_Id :=
1622 Original_Node (Expression (Tdef));
1624 if Nkind (Modulus) = N_Integer_Literal then
1626 Modv : constant Uint := Intval (Modulus);
1627 Sizv : constant Uint := RM_Size (Utype);
1630 -- First case, modulus and size are the same. This
1631 -- happens if you have something like mod 32, with
1632 -- an explicit size of 32, this is for sure a case
1633 -- where the warning is given, since it is seems
1634 -- very unlikely that someone would want e.g. a
1635 -- five bit type stored in 32 bits. It is much
1636 -- more likely they wanted a 32-bit type.
1641 -- Second case, the modulus is 32 or 64 and no
1642 -- size clause is present. This is a less clear
1643 -- case for giving the warning, but in the case
1644 -- of 32/64 (5-bit or 6-bit types) these seem rare
1645 -- enough that it is a likely error (and in any
1646 -- case using 2**5 or 2**6 in these cases seems
1647 -- clearer. We don't include 8 or 16 here, simply
1648 -- because in practice 3-bit and 4-bit types are
1649 -- more common and too many false positives if
1650 -- we warn in these cases.
1652 elsif not Has_Size_Clause (Utype)
1653 and then (Modv = Uint_32 or else Modv = Uint_64)
1657 -- No warning needed
1663 -- If we fall through, give warning
1665 Error_Msg_Uint_1 := Modv;
1667 ("?2 '*'*^' may have been intended here",
1675 end Check_Suspicious_Modulus;
1677 ------------------------
1678 -- Freeze_Record_Type --
1679 ------------------------
1681 procedure Freeze_Record_Type (Rec : Entity_Id) is
1688 pragma Warnings (Off, Junk);
1690 Unplaced_Component : Boolean := False;
1691 -- Set True if we find at least one component with no component
1692 -- clause (used to warn about useless Pack pragmas).
1694 Placed_Component : Boolean := False;
1695 -- Set True if we find at least one component with a component
1696 -- clause (used to warn about useless Bit_Order pragmas, and also
1697 -- to detect cases where Implicit_Packing may have an effect).
1699 All_Scalar_Components : Boolean := True;
1700 -- Set False if we encounter a component of a non-scalar type
1702 Scalar_Component_Total_RM_Size : Uint := Uint_0;
1703 Scalar_Component_Total_Esize : Uint := Uint_0;
1704 -- Accumulates total RM_Size values and total Esize values of all
1705 -- scalar components. Used for processing of Implicit_Packing.
1707 function Check_Allocator (N : Node_Id) return Node_Id;
1708 -- If N is an allocator, possibly wrapped in one or more level of
1709 -- qualified expression(s), return the inner allocator node, else
1712 procedure Check_Itype (Typ : Entity_Id);
1713 -- If the component subtype is an access to a constrained subtype of
1714 -- an already frozen type, make the subtype frozen as well. It might
1715 -- otherwise be frozen in the wrong scope, and a freeze node on
1716 -- subtype has no effect. Similarly, if the component subtype is a
1717 -- regular (not protected) access to subprogram, set the anonymous
1718 -- subprogram type to frozen as well, to prevent an out-of-scope
1719 -- freeze node at some eventual point of call. Protected operations
1720 -- are handled elsewhere.
1722 ---------------------
1723 -- Check_Allocator --
1724 ---------------------
1726 function Check_Allocator (N : Node_Id) return Node_Id is
1731 if Nkind (Inner) = N_Allocator then
1733 elsif Nkind (Inner) = N_Qualified_Expression then
1734 Inner := Expression (Inner);
1739 end Check_Allocator;
1745 procedure Check_Itype (Typ : Entity_Id) is
1746 Desig : constant Entity_Id := Designated_Type (Typ);
1749 if not Is_Frozen (Desig)
1750 and then Is_Frozen (Base_Type (Desig))
1752 Set_Is_Frozen (Desig);
1754 -- In addition, add an Itype_Reference to ensure that the
1755 -- access subtype is elaborated early enough. This cannot be
1756 -- done if the subtype may depend on discriminants.
1758 if Ekind (Comp) = E_Component
1759 and then Is_Itype (Etype (Comp))
1760 and then not Has_Discriminants (Rec)
1762 IR := Make_Itype_Reference (Sloc (Comp));
1763 Set_Itype (IR, Desig);
1766 Result := New_List (IR);
1768 Append (IR, Result);
1772 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
1773 and then Convention (Desig) /= Convention_Protected
1775 Set_Is_Frozen (Desig);
1779 -- Start of processing for Freeze_Record_Type
1782 -- If this is a subtype of a controlled type, declared without a
1783 -- constraint, the _controller may not appear in the component list
1784 -- if the parent was not frozen at the point of subtype declaration.
1785 -- Inherit the _controller component now.
1787 if Rec /= Base_Type (Rec)
1788 and then Has_Controlled_Component (Rec)
1790 if Nkind (Parent (Rec)) = N_Subtype_Declaration
1791 and then Is_Entity_Name (Subtype_Indication (Parent (Rec)))
1793 Set_First_Entity (Rec, First_Entity (Base_Type (Rec)));
1795 -- If this is an internal type without a declaration, as for
1796 -- record component, the base type may not yet be frozen, and its
1797 -- controller has not been created. Add an explicit freeze node
1798 -- for the itype, so it will be frozen after the base type. This
1799 -- freeze node is used to communicate with the expander, in order
1800 -- to create the controller for the enclosing record, and it is
1801 -- deleted afterwards (see exp_ch3). It must not be created when
1802 -- expansion is off, because it might appear in the wrong context
1803 -- for the back end.
1805 elsif Is_Itype (Rec)
1806 and then Has_Delayed_Freeze (Base_Type (Rec))
1808 Nkind (Associated_Node_For_Itype (Rec)) =
1809 N_Component_Declaration
1810 and then Expander_Active
1812 Ensure_Freeze_Node (Rec);
1816 -- Freeze components and embedded subtypes
1818 Comp := First_Entity (Rec);
1820 while Present (Comp) loop
1822 -- First handle the component case
1824 if Ekind (Comp) = E_Component
1825 or else Ekind (Comp) = E_Discriminant
1828 CC : constant Node_Id := Component_Clause (Comp);
1831 -- Freezing a record type freezes the type of each of its
1832 -- components. However, if the type of the component is
1833 -- part of this record, we do not want or need a separate
1834 -- Freeze_Node. Note that Is_Itype is wrong because that's
1835 -- also set in private type cases. We also can't check for
1836 -- the Scope being exactly Rec because of private types and
1837 -- record extensions.
1839 if Is_Itype (Etype (Comp))
1840 and then Is_Record_Type (Underlying_Type
1841 (Scope (Etype (Comp))))
1843 Undelay_Type (Etype (Comp));
1846 Freeze_And_Append (Etype (Comp), Loc, Result);
1848 -- Check for error of component clause given for variable
1849 -- sized type. We have to delay this test till this point,
1850 -- since the component type has to be frozen for us to know
1851 -- if it is variable length. We omit this test in a generic
1852 -- context, it will be applied at instantiation time.
1854 if Present (CC) then
1855 Placed_Component := True;
1857 if Inside_A_Generic then
1861 Size_Known_At_Compile_Time
1862 (Underlying_Type (Etype (Comp)))
1865 ("component clause not allowed for variable " &
1866 "length component", CC);
1870 Unplaced_Component := True;
1873 -- Case of component requires byte alignment
1875 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
1877 -- Set the enclosing record to also require byte align
1879 Set_Must_Be_On_Byte_Boundary (Rec);
1881 -- Check for component clause that is inconsistent with
1882 -- the required byte boundary alignment.
1885 and then Normalized_First_Bit (Comp) mod
1886 System_Storage_Unit /= 0
1889 ("component & must be byte aligned",
1890 Component_Name (Component_Clause (Comp)));
1896 -- Gather data for possible Implicit_Packing later. Note that at
1897 -- this stage we might be dealing with a real component, or with
1898 -- an implicit subtype declaration.
1900 if not Is_Scalar_Type (Etype (Comp)) then
1901 All_Scalar_Components := False;
1903 Scalar_Component_Total_RM_Size :=
1904 Scalar_Component_Total_RM_Size + RM_Size (Etype (Comp));
1905 Scalar_Component_Total_Esize :=
1906 Scalar_Component_Total_Esize + Esize (Etype (Comp));
1909 -- If the component is an Itype with Delayed_Freeze and is either
1910 -- a record or array subtype and its base type has not yet been
1911 -- frozen, we must remove this from the entity list of this
1912 -- record and put it on the entity list of the scope of its base
1913 -- type. Note that we know that this is not the type of a
1914 -- component since we cleared Has_Delayed_Freeze for it in the
1915 -- previous loop. Thus this must be the Designated_Type of an
1916 -- access type, which is the type of a component.
1919 and then Is_Type (Scope (Comp))
1920 and then Is_Composite_Type (Comp)
1921 and then Base_Type (Comp) /= Comp
1922 and then Has_Delayed_Freeze (Comp)
1923 and then not Is_Frozen (Base_Type (Comp))
1926 Will_Be_Frozen : Boolean := False;
1930 -- We have a pretty bad kludge here. Suppose Rec is subtype
1931 -- being defined in a subprogram that's created as part of
1932 -- the freezing of Rec'Base. In that case, we know that
1933 -- Comp'Base must have already been frozen by the time we
1934 -- get to elaborate this because Gigi doesn't elaborate any
1935 -- bodies until it has elaborated all of the declarative
1936 -- part. But Is_Frozen will not be set at this point because
1937 -- we are processing code in lexical order.
1939 -- We detect this case by going up the Scope chain of Rec
1940 -- and seeing if we have a subprogram scope before reaching
1941 -- the top of the scope chain or that of Comp'Base. If we
1942 -- do, then mark that Comp'Base will actually be frozen. If
1943 -- so, we merely undelay it.
1946 while Present (S) loop
1947 if Is_Subprogram (S) then
1948 Will_Be_Frozen := True;
1950 elsif S = Scope (Base_Type (Comp)) then
1957 if Will_Be_Frozen then
1958 Undelay_Type (Comp);
1960 if Present (Prev) then
1961 Set_Next_Entity (Prev, Next_Entity (Comp));
1963 Set_First_Entity (Rec, Next_Entity (Comp));
1966 -- Insert in entity list of scope of base type (which
1967 -- must be an enclosing scope, because still unfrozen).
1969 Append_Entity (Comp, Scope (Base_Type (Comp)));
1973 -- If the component is an access type with an allocator as default
1974 -- value, the designated type will be frozen by the corresponding
1975 -- expression in init_proc. In order to place the freeze node for
1976 -- the designated type before that for the current record type,
1979 -- Same process if the component is an array of access types,
1980 -- initialized with an aggregate. If the designated type is
1981 -- private, it cannot contain allocators, and it is premature
1982 -- to freeze the type, so we check for this as well.
1984 elsif Is_Access_Type (Etype (Comp))
1985 and then Present (Parent (Comp))
1986 and then Present (Expression (Parent (Comp)))
1989 Alloc : constant Node_Id :=
1990 Check_Allocator (Expression (Parent (Comp)));
1993 if Present (Alloc) then
1995 -- If component is pointer to a classwide type, freeze
1996 -- the specific type in the expression being allocated.
1997 -- The expression may be a subtype indication, in which
1998 -- case freeze the subtype mark.
2000 if Is_Class_Wide_Type
2001 (Designated_Type (Etype (Comp)))
2003 if Is_Entity_Name (Expression (Alloc)) then
2005 (Entity (Expression (Alloc)), Loc, Result);
2007 Nkind (Expression (Alloc)) = N_Subtype_Indication
2010 (Entity (Subtype_Mark (Expression (Alloc))),
2014 elsif Is_Itype (Designated_Type (Etype (Comp))) then
2015 Check_Itype (Etype (Comp));
2019 (Designated_Type (Etype (Comp)), Loc, Result);
2024 elsif Is_Access_Type (Etype (Comp))
2025 and then Is_Itype (Designated_Type (Etype (Comp)))
2027 Check_Itype (Etype (Comp));
2029 elsif Is_Array_Type (Etype (Comp))
2030 and then Is_Access_Type (Component_Type (Etype (Comp)))
2031 and then Present (Parent (Comp))
2032 and then Nkind (Parent (Comp)) = N_Component_Declaration
2033 and then Present (Expression (Parent (Comp)))
2034 and then Nkind (Expression (Parent (Comp))) = N_Aggregate
2035 and then Is_Fully_Defined
2036 (Designated_Type (Component_Type (Etype (Comp))))
2040 (Component_Type (Etype (Comp))), Loc, Result);
2047 -- Deal with pragma Bit_Order setting non-standard bit order
2049 if Reverse_Bit_Order (Rec) and then Base_Type (Rec) = Rec then
2050 if not Placed_Component then
2052 Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
2053 Error_Msg_N ("?Bit_Order specification has no effect", ADC);
2055 ("\?since no component clauses were specified", ADC);
2057 -- Here is where we do the processing for reversed bit order
2060 Adjust_Record_For_Reverse_Bit_Order (Rec);
2064 -- Complete error checking on record representation clause (e.g.
2065 -- overlap of components). This is called after adjusting the
2066 -- record for reverse bit order.
2069 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
2071 if Present (RRC) then
2072 Check_Record_Representation_Clause (RRC);
2076 -- Set OK_To_Reorder_Components depending on debug flags
2078 if Rec = Base_Type (Rec)
2079 and then Convention (Rec) = Convention_Ada
2081 if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
2083 (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
2085 Set_OK_To_Reorder_Components (Rec);
2089 -- Check for useless pragma Pack when all components placed. We only
2090 -- do this check for record types, not subtypes, since a subtype may
2091 -- have all its components placed, and it still makes perfectly good
2092 -- sense to pack other subtypes or the parent type. We do not give
2093 -- this warning if Optimize_Alignment is set to Space, since the
2094 -- pragma Pack does have an effect in this case (it always resets
2095 -- the alignment to one).
2097 if Ekind (Rec) = E_Record_Type
2098 and then Is_Packed (Rec)
2099 and then not Unplaced_Component
2100 and then Optimize_Alignment /= 'S'
2102 -- Reset packed status. Probably not necessary, but we do it so
2103 -- that there is no chance of the back end doing something strange
2104 -- with this redundant indication of packing.
2106 Set_Is_Packed (Rec, False);
2108 -- Give warning if redundant constructs warnings on
2110 if Warn_On_Redundant_Constructs then
2111 Error_Msg_N -- CODEFIX
2112 ("?pragma Pack has no effect, no unplaced components",
2113 Get_Rep_Pragma (Rec, Name_Pack));
2117 -- If this is the record corresponding to a remote type, freeze the
2118 -- remote type here since that is what we are semantically freezing.
2119 -- This prevents the freeze node for that type in an inner scope.
2121 -- Also, Check for controlled components and unchecked unions.
2122 -- Finally, enforce the restriction that access attributes with a
2123 -- current instance prefix can only apply to limited types.
2125 if Ekind (Rec) = E_Record_Type then
2126 if Present (Corresponding_Remote_Type (Rec)) then
2128 (Corresponding_Remote_Type (Rec), Loc, Result);
2131 Comp := First_Component (Rec);
2132 while Present (Comp) loop
2134 -- Do not set Has_Controlled_Component on a class-wide
2135 -- equivalent type. See Make_CW_Equivalent_Type.
2137 if not Is_Class_Wide_Equivalent_Type (Rec)
2138 and then (Has_Controlled_Component (Etype (Comp))
2139 or else (Chars (Comp) /= Name_uParent
2140 and then Is_Controlled (Etype (Comp)))
2141 or else (Is_Protected_Type (Etype (Comp))
2143 (Corresponding_Record_Type
2145 and then Has_Controlled_Component
2146 (Corresponding_Record_Type
2149 Set_Has_Controlled_Component (Rec);
2153 if Has_Unchecked_Union (Etype (Comp)) then
2154 Set_Has_Unchecked_Union (Rec);
2157 if Has_Per_Object_Constraint (Comp) then
2159 -- Scan component declaration for likely misuses of current
2160 -- instance, either in a constraint or a default expression.
2162 Check_Current_Instance (Parent (Comp));
2165 Next_Component (Comp);
2169 Set_Component_Alignment_If_Not_Set (Rec);
2171 -- For first subtypes, check if there are any fixed-point fields with
2172 -- component clauses, where we must check the size. This is not done
2173 -- till the freeze point, since for fixed-point types, we do not know
2174 -- the size until the type is frozen. Similar processing applies to
2175 -- bit packed arrays.
2177 if Is_First_Subtype (Rec) then
2178 Comp := First_Component (Rec);
2180 while Present (Comp) loop
2181 if Present (Component_Clause (Comp))
2182 and then (Is_Fixed_Point_Type (Etype (Comp))
2184 Is_Bit_Packed_Array (Etype (Comp)))
2187 (Component_Name (Component_Clause (Comp)),
2193 Next_Component (Comp);
2197 -- Generate warning for applying C or C++ convention to a record
2198 -- with discriminants. This is suppressed for the unchecked union
2199 -- case, since the whole point in this case is interface C. We also
2200 -- do not generate this within instantiations, since we will have
2201 -- generated a message on the template.
2203 if Has_Discriminants (E)
2204 and then not Is_Unchecked_Union (E)
2205 and then (Convention (E) = Convention_C
2207 Convention (E) = Convention_CPP)
2208 and then Comes_From_Source (E)
2209 and then not In_Instance
2210 and then not Has_Warnings_Off (E)
2211 and then not Has_Warnings_Off (Base_Type (E))
2214 Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
2218 if Present (Cprag) then
2219 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
2221 if Convention (E) = Convention_C then
2223 ("?variant record has no direct equivalent in C", A2);
2226 ("?variant record has no direct equivalent in C++", A2);
2230 ("\?use of convention for type& is dubious", A2, E);
2235 -- See if Size is too small as is (and implicit packing might help)
2237 if not Is_Packed (Rec)
2239 -- No implicit packing if even one component is explicitly placed
2241 and then not Placed_Component
2243 -- Must have size clause and all scalar components
2245 and then Has_Size_Clause (Rec)
2246 and then All_Scalar_Components
2248 -- Do not try implicit packing on records with discriminants, too
2249 -- complicated, especially in the variant record case.
2251 and then not Has_Discriminants (Rec)
2253 -- We can implicitly pack if the specified size of the record is
2254 -- less than the sum of the object sizes (no point in packing if
2255 -- this is not the case).
2257 and then Esize (Rec) < Scalar_Component_Total_Esize
2259 -- And the total RM size cannot be greater than the specified size
2260 -- since otherwise packing will not get us where we have to be!
2262 and then Esize (Rec) >= Scalar_Component_Total_RM_Size
2264 -- Never do implicit packing in CodePeer mode since we don't do
2265 -- any packing ever in this mode (why not???)
2267 and then not CodePeer_Mode
2269 -- If implicit packing enabled, do it
2271 if Implicit_Packing then
2272 Set_Is_Packed (Rec);
2274 -- Otherwise flag the size clause
2278 Sz : constant Node_Id := Size_Clause (Rec);
2280 Error_Msg_NE -- CODEFIX
2281 ("size given for& too small", Sz, Rec);
2282 Error_Msg_N -- CODEFIX
2283 ("\use explicit pragma Pack "
2284 & "or use pragma Implicit_Packing", Sz);
2288 end Freeze_Record_Type;
2290 -- Start of processing for Freeze_Entity
2293 -- We are going to test for various reasons why this entity need not be
2294 -- frozen here, but in the case of an Itype that's defined within a
2295 -- record, that test actually applies to the record.
2297 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
2298 Test_E := Scope (E);
2299 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
2300 and then Is_Record_Type (Underlying_Type (Scope (E)))
2302 Test_E := Underlying_Type (Scope (E));
2305 -- Do not freeze if already frozen since we only need one freeze node
2307 if Is_Frozen (E) then
2310 -- It is improper to freeze an external entity within a generic because
2311 -- its freeze node will appear in a non-valid context. The entity will
2312 -- be frozen in the proper scope after the current generic is analyzed.
2314 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
2317 -- Do not freeze a global entity within an inner scope created during
2318 -- expansion. A call to subprogram E within some internal procedure
2319 -- (a stream attribute for example) might require freezing E, but the
2320 -- freeze node must appear in the same declarative part as E itself.
2321 -- The two-pass elaboration mechanism in gigi guarantees that E will
2322 -- be frozen before the inner call is elaborated. We exclude constants
2323 -- from this test, because deferred constants may be frozen early, and
2324 -- must be diagnosed (e.g. in the case of a deferred constant being used
2325 -- in a default expression). If the enclosing subprogram comes from
2326 -- source, or is a generic instance, then the freeze point is the one
2327 -- mandated by the language, and we freeze the entity. A subprogram that
2328 -- is a child unit body that acts as a spec does not have a spec that
2329 -- comes from source, but can only come from source.
2331 elsif In_Open_Scopes (Scope (Test_E))
2332 and then Scope (Test_E) /= Current_Scope
2333 and then Ekind (Test_E) /= E_Constant
2336 S : Entity_Id := Current_Scope;
2339 while Present (S) loop
2340 if Is_Overloadable (S) then
2341 if Comes_From_Source (S)
2342 or else Is_Generic_Instance (S)
2343 or else Is_Child_Unit (S)
2355 -- Similarly, an inlined instance body may make reference to global
2356 -- entities, but these references cannot be the proper freezing point
2357 -- for them, and in the absence of inlining freezing will take place in
2358 -- their own scope. Normally instance bodies are analyzed after the
2359 -- enclosing compilation, and everything has been frozen at the proper
2360 -- place, but with front-end inlining an instance body is compiled
2361 -- before the end of the enclosing scope, and as a result out-of-order
2362 -- freezing must be prevented.
2364 elsif Front_End_Inlining
2365 and then In_Instance_Body
2366 and then Present (Scope (Test_E))
2369 S : Entity_Id := Scope (Test_E);
2372 while Present (S) loop
2373 if Is_Generic_Instance (S) then
2386 -- Here to freeze the entity
2391 -- Case of entity being frozen is other than a type
2393 if not Is_Type (E) then
2395 -- If entity is exported or imported and does not have an external
2396 -- name, now is the time to provide the appropriate default name.
2397 -- Skip this if the entity is stubbed, since we don't need a name
2398 -- for any stubbed routine. For the case on intrinsics, if no
2399 -- external name is specified, then calls will be handled in
2400 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed; if
2401 -- an external name is provided, then Expand_Intrinsic_Call leaves
2402 -- calls in place for expansion by GIGI.
2404 if (Is_Imported (E) or else Is_Exported (E))
2405 and then No (Interface_Name (E))
2406 and then Convention (E) /= Convention_Stubbed
2407 and then Convention (E) /= Convention_Intrinsic
2409 Set_Encoded_Interface_Name
2410 (E, Get_Default_External_Name (E));
2412 -- If entity is an atomic object appearing in a declaration and
2413 -- the expression is an aggregate, assign it to a temporary to
2414 -- ensure that the actual assignment is done atomically rather
2415 -- than component-wise (the assignment to the temp may be done
2416 -- component-wise, but that is harmless).
2419 and then Nkind (Parent (E)) = N_Object_Declaration
2420 and then Present (Expression (Parent (E)))
2421 and then Nkind (Expression (Parent (E))) = N_Aggregate
2423 Is_Atomic_Aggregate (Expression (Parent (E)), Etype (E))
2428 -- For a subprogram, freeze all parameter types and also the return
2429 -- type (RM 13.14(14)). However skip this for internal subprograms.
2430 -- This is also the point where any extra formal parameters are
2431 -- created since we now know whether the subprogram will use a
2432 -- foreign convention.
2434 if Is_Subprogram (E) then
2435 if not Is_Internal (E) then
2439 Warn_Node : Node_Id;
2442 -- Loop through formals
2444 Formal := First_Formal (E);
2445 while Present (Formal) loop
2446 F_Type := Etype (Formal);
2447 Freeze_And_Append (F_Type, Loc, Result);
2449 if Is_Private_Type (F_Type)
2450 and then Is_Private_Type (Base_Type (F_Type))
2451 and then No (Full_View (Base_Type (F_Type)))
2452 and then not Is_Generic_Type (F_Type)
2453 and then not Is_Derived_Type (F_Type)
2455 -- If the type of a formal is incomplete, subprogram
2456 -- is being frozen prematurely. Within an instance
2457 -- (but not within a wrapper package) this is an
2458 -- artifact of our need to regard the end of an
2459 -- instantiation as a freeze point. Otherwise it is
2460 -- a definite error.
2463 Set_Is_Frozen (E, False);
2466 elsif not After_Last_Declaration
2467 and then not Freezing_Library_Level_Tagged_Type
2469 Error_Msg_Node_1 := F_Type;
2471 ("type& must be fully defined before this point",
2476 -- Check suspicious parameter for C function. These tests
2477 -- apply only to exported/imported subprograms.
2479 if Warn_On_Export_Import
2480 and then Comes_From_Source (E)
2481 and then (Convention (E) = Convention_C
2483 Convention (E) = Convention_CPP)
2484 and then (Is_Imported (E) or else Is_Exported (E))
2485 and then Convention (E) /= Convention (Formal)
2486 and then not Has_Warnings_Off (E)
2487 and then not Has_Warnings_Off (F_Type)
2488 and then not Has_Warnings_Off (Formal)
2490 -- Qualify mention of formals with subprogram name
2492 Error_Msg_Qual_Level := 1;
2494 -- Check suspicious use of fat C pointer
2496 if Is_Access_Type (F_Type)
2497 and then Esize (F_Type) > Ttypes.System_Address_Size
2500 ("?type of & does not correspond to C pointer!",
2503 -- Check suspicious return of boolean
2505 elsif Root_Type (F_Type) = Standard_Boolean
2506 and then Convention (F_Type) = Convention_Ada
2507 and then not Has_Warnings_Off (F_Type)
2508 and then not Has_Size_Clause (F_Type)
2509 and then VM_Target = No_VM
2511 Error_Msg_N ("& is an 8-bit Ada Boolean?", Formal);
2513 ("\use appropriate corresponding type in C "
2514 & "(e.g. char)?", Formal);
2516 -- Check suspicious tagged type
2518 elsif (Is_Tagged_Type (F_Type)
2519 or else (Is_Access_Type (F_Type)
2522 (Designated_Type (F_Type))))
2523 and then Convention (E) = Convention_C
2526 ("?& involves a tagged type which does not "
2527 & "correspond to any C type!", Formal);
2529 -- Check wrong convention subprogram pointer
2531 elsif Ekind (F_Type) = E_Access_Subprogram_Type
2532 and then not Has_Foreign_Convention (F_Type)
2535 ("?subprogram pointer & should "
2536 & "have foreign convention!", Formal);
2537 Error_Msg_Sloc := Sloc (F_Type);
2539 ("\?add Convention pragma to declaration of &#",
2543 -- Turn off name qualification after message output
2545 Error_Msg_Qual_Level := 0;
2548 -- Check for unconstrained array in exported foreign
2551 if Has_Foreign_Convention (E)
2552 and then not Is_Imported (E)
2553 and then Is_Array_Type (F_Type)
2554 and then not Is_Constrained (F_Type)
2555 and then Warn_On_Export_Import
2557 -- Exclude VM case, since both .NET and JVM can handle
2558 -- unconstrained arrays without a problem.
2560 and then VM_Target = No_VM
2562 Error_Msg_Qual_Level := 1;
2564 -- If this is an inherited operation, place the
2565 -- warning on the derived type declaration, rather
2566 -- than on the original subprogram.
2568 if Nkind (Original_Node (Parent (E))) =
2569 N_Full_Type_Declaration
2571 Warn_Node := Parent (E);
2573 if Formal = First_Formal (E) then
2575 ("?in inherited operation&", Warn_Node, E);
2578 Warn_Node := Formal;
2582 ("?type of argument& is unconstrained array",
2585 ("?foreign caller must pass bounds explicitly",
2587 Error_Msg_Qual_Level := 0;
2590 if not From_With_Type (F_Type) then
2591 if Is_Access_Type (F_Type) then
2592 F_Type := Designated_Type (F_Type);
2595 -- If the formal is an anonymous_access_to_subprogram
2596 -- freeze the subprogram type as well, to prevent
2597 -- scope anomalies in gigi, because there is no other
2598 -- clear point at which it could be frozen.
2600 if Is_Itype (Etype (Formal))
2601 and then Ekind (F_Type) = E_Subprogram_Type
2603 Freeze_And_Append (F_Type, Loc, Result);
2607 Next_Formal (Formal);
2610 -- Case of function: similar checks on return type
2612 if Ekind (E) = E_Function then
2614 -- Freeze return type
2616 R_Type := Etype (E);
2617 Freeze_And_Append (R_Type, Loc, Result);
2619 -- Check suspicious return type for C function
2621 if Warn_On_Export_Import
2622 and then (Convention (E) = Convention_C
2624 Convention (E) = Convention_CPP)
2625 and then (Is_Imported (E) or else Is_Exported (E))
2627 -- Check suspicious return of fat C pointer
2629 if Is_Access_Type (R_Type)
2630 and then Esize (R_Type) > Ttypes.System_Address_Size
2631 and then not Has_Warnings_Off (E)
2632 and then not Has_Warnings_Off (R_Type)
2635 ("?return type of& does not "
2636 & "correspond to C pointer!", E);
2638 -- Check suspicious return of boolean
2640 elsif Root_Type (R_Type) = Standard_Boolean
2641 and then Convention (R_Type) = Convention_Ada
2642 and then VM_Target = No_VM
2643 and then not Has_Warnings_Off (E)
2644 and then not Has_Warnings_Off (R_Type)
2645 and then not Has_Size_Clause (R_Type)
2648 N : constant Node_Id :=
2649 Result_Definition (Declaration_Node (E));
2652 ("return type of & is an 8-bit Ada Boolean?",
2655 ("\use appropriate corresponding type in C "
2656 & "(e.g. char)?", N, E);
2659 -- Check suspicious return tagged type
2661 elsif (Is_Tagged_Type (R_Type)
2662 or else (Is_Access_Type (R_Type)
2665 (Designated_Type (R_Type))))
2666 and then Convention (E) = Convention_C
2667 and then not Has_Warnings_Off (E)
2668 and then not Has_Warnings_Off (R_Type)
2671 ("?return type of & does not "
2672 & "correspond to C type!", E);
2674 -- Check return of wrong convention subprogram pointer
2676 elsif Ekind (R_Type) = E_Access_Subprogram_Type
2677 and then not Has_Foreign_Convention (R_Type)
2678 and then not Has_Warnings_Off (E)
2679 and then not Has_Warnings_Off (R_Type)
2682 ("?& should return a foreign "
2683 & "convention subprogram pointer", E);
2684 Error_Msg_Sloc := Sloc (R_Type);
2686 ("\?add Convention pragma to declaration of& #",
2691 -- Give warning for suspicous return of a result of an
2692 -- unconstrained array type in a foreign convention
2695 if Has_Foreign_Convention (E)
2697 -- We are looking for a return of unconstrained array
2699 and then Is_Array_Type (R_Type)
2700 and then not Is_Constrained (R_Type)
2702 -- Exclude imported routines, the warning does not
2703 -- belong on the import, but on the routine definition.
2705 and then not Is_Imported (E)
2707 -- Exclude VM case, since both .NET and JVM can handle
2708 -- return of unconstrained arrays without a problem.
2710 and then VM_Target = No_VM
2712 -- Check that general warning is enabled, and that it
2713 -- is not suppressed for this particular case.
2715 and then Warn_On_Export_Import
2716 and then not Has_Warnings_Off (E)
2717 and then not Has_Warnings_Off (R_Type)
2720 ("?foreign convention function& should not " &
2721 "return unconstrained array!", E);
2727 -- Must freeze its parent first if it is a derived subprogram
2729 if Present (Alias (E)) then
2730 Freeze_And_Append (Alias (E), Loc, Result);
2733 -- We don't freeze internal subprograms, because we don't normally
2734 -- want addition of extra formals or mechanism setting to happen
2735 -- for those. However we do pass through predefined dispatching
2736 -- cases, since extra formals may be needed in some cases, such as
2737 -- for the stream 'Input function (build-in-place formals).
2739 if not Is_Internal (E)
2740 or else Is_Predefined_Dispatching_Operation (E)
2742 Freeze_Subprogram (E);
2745 -- Here for other than a subprogram or type
2748 -- If entity has a type, and it is not a generic unit, then
2749 -- freeze it first (RM 13.14(10)).
2751 if Present (Etype (E))
2752 and then Ekind (E) /= E_Generic_Function
2754 Freeze_And_Append (Etype (E), Loc, Result);
2757 -- Special processing for objects created by object declaration
2759 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
2761 -- Abstract type allowed only for C++ imported variables or
2764 -- Note: we inhibit this check for objects that do not come
2765 -- from source because there is at least one case (the
2766 -- expansion of x'class'input where x is abstract) where we
2767 -- legitimately generate an abstract object.
2769 if Is_Abstract_Type (Etype (E))
2770 and then Comes_From_Source (Parent (E))
2771 and then not (Is_Imported (E)
2772 and then Is_CPP_Class (Etype (E)))
2774 Error_Msg_N ("type of object cannot be abstract",
2775 Object_Definition (Parent (E)));
2777 if Is_CPP_Class (Etype (E)) then
2779 ("\} may need a cpp_constructor",
2780 Object_Definition (Parent (E)), Etype (E));
2784 -- For object created by object declaration, perform required
2785 -- categorization (preelaborate and pure) checks. Defer these
2786 -- checks to freeze time since pragma Import inhibits default
2787 -- initialization and thus pragma Import affects these checks.
2789 Validate_Object_Declaration (Declaration_Node (E));
2791 -- If there is an address clause, check that it is valid
2793 Check_Address_Clause (E);
2795 -- If the object needs any kind of default initialization, an
2796 -- error must be issued if No_Default_Initialization applies.
2797 -- The check doesn't apply to imported objects, which are not
2798 -- ever default initialized, and is why the check is deferred
2799 -- until freezing, at which point we know if Import applies.
2800 -- Deferred constants are also exempted from this test because
2801 -- their completion is explicit, or through an import pragma.
2803 if Ekind (E) = E_Constant
2804 and then Present (Full_View (E))
2808 elsif Comes_From_Source (E)
2809 and then not Is_Imported (E)
2810 and then not Has_Init_Expression (Declaration_Node (E))
2812 ((Has_Non_Null_Base_Init_Proc (Etype (E))
2813 and then not No_Initialization (Declaration_Node (E))
2814 and then not Is_Value_Type (Etype (E))
2815 and then not Suppress_Init_Proc (Etype (E)))
2817 (Needs_Simple_Initialization (Etype (E))
2818 and then not Is_Internal (E)))
2820 Has_Default_Initialization := True;
2822 (No_Default_Initialization, Declaration_Node (E));
2825 -- Check that a Thread_Local_Storage variable does not have
2826 -- default initialization, and any explicit initialization must
2827 -- either be the null constant or a static constant.
2829 if Has_Pragma_Thread_Local_Storage (E) then
2831 Decl : constant Node_Id := Declaration_Node (E);
2833 if Has_Default_Initialization
2835 (Has_Init_Expression (Decl)
2837 (No (Expression (Decl))
2839 (Is_Static_Expression (Expression (Decl))
2841 Nkind (Expression (Decl)) = N_Null)))
2844 ("Thread_Local_Storage variable& is "
2845 & "improperly initialized", Decl, E);
2847 ("\only allowed initialization is explicit "
2848 & "NULL or static expression", Decl, E);
2853 -- For imported objects, set Is_Public unless there is also an
2854 -- address clause, which means that there is no external symbol
2855 -- needed for the Import (Is_Public may still be set for other
2856 -- unrelated reasons). Note that we delayed this processing
2857 -- till freeze time so that we can be sure not to set the flag
2858 -- if there is an address clause. If there is such a clause,
2859 -- then the only purpose of the Import pragma is to suppress
2860 -- implicit initialization.
2863 and then No (Address_Clause (E))
2868 -- For convention C objects of an enumeration type, warn if
2869 -- the size is not integer size and no explicit size given.
2870 -- Skip warning for Boolean, and Character, assume programmer
2871 -- expects 8-bit sizes for these cases.
2873 if (Convention (E) = Convention_C
2875 Convention (E) = Convention_CPP)
2876 and then Is_Enumeration_Type (Etype (E))
2877 and then not Is_Character_Type (Etype (E))
2878 and then not Is_Boolean_Type (Etype (E))
2879 and then Esize (Etype (E)) < Standard_Integer_Size
2880 and then not Has_Size_Clause (E)
2882 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
2884 ("?convention C enumeration object has size less than ^",
2886 Error_Msg_N ("\?use explicit size clause to set size", E);
2890 -- Check that a constant which has a pragma Volatile[_Components]
2891 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
2893 -- Note: Atomic[_Components] also sets Volatile[_Components]
2895 if Ekind (E) = E_Constant
2896 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
2897 and then not Is_Imported (E)
2899 -- Make sure we actually have a pragma, and have not merely
2900 -- inherited the indication from elsewhere (e.g. an address
2901 -- clause, which is not good enough in RM terms!)
2903 if Has_Rep_Pragma (E, Name_Atomic)
2905 Has_Rep_Pragma (E, Name_Atomic_Components)
2908 ("stand alone atomic constant must be " &
2909 "imported (RM C.6(13))", E);
2911 elsif Has_Rep_Pragma (E, Name_Volatile)
2913 Has_Rep_Pragma (E, Name_Volatile_Components)
2916 ("stand alone volatile constant must be " &
2917 "imported (RM C.6(13))", E);
2921 -- Static objects require special handling
2923 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
2924 and then Is_Statically_Allocated (E)
2926 Freeze_Static_Object (E);
2929 -- Remaining step is to layout objects
2931 if Ekind (E) = E_Variable
2933 Ekind (E) = E_Constant
2935 Ekind (E) = E_Loop_Parameter
2943 -- Case of a type or subtype being frozen
2946 -- We used to check here that a full type must have preelaborable
2947 -- initialization if it completes a private type specified with
2948 -- pragma Preelaborable_Intialization, but that missed cases where
2949 -- the types occur within a generic package, since the freezing
2950 -- that occurs within a containing scope generally skips traversal
2951 -- of a generic unit's declarations (those will be frozen within
2952 -- instances). This check was moved to Analyze_Package_Specification.
2954 -- The type may be defined in a generic unit. This can occur when
2955 -- freezing a generic function that returns the type (which is
2956 -- defined in a parent unit). It is clearly meaningless to freeze
2957 -- this type. However, if it is a subtype, its size may be determi-
2958 -- nable and used in subsequent checks, so might as well try to
2961 if Present (Scope (E))
2962 and then Is_Generic_Unit (Scope (E))
2964 Check_Compile_Time_Size (E);
2968 -- Deal with special cases of freezing for subtype
2970 if E /= Base_Type (E) then
2972 -- Before we do anything else, a specialized test for the case of
2973 -- a size given for an array where the array needs to be packed,
2974 -- but was not so the size cannot be honored. This would of course
2975 -- be caught by the backend, and indeed we don't catch all cases.
2976 -- The point is that we can give a better error message in those
2977 -- cases that we do catch with the circuitry here. Also if pragma
2978 -- Implicit_Packing is set, this is where the packing occurs.
2980 -- The reason we do this so early is that the processing in the
2981 -- automatic packing case affects the layout of the base type, so
2982 -- it must be done before we freeze the base type.
2984 if Is_Array_Type (E) then
2987 Ctyp : constant Entity_Id := Component_Type (E);
2990 -- Check enabling conditions. These are straightforward
2991 -- except for the test for a limited composite type. This
2992 -- eliminates the rare case of a array of limited components
2993 -- where there are issues of whether or not we can go ahead
2994 -- and pack the array (since we can't freely pack and unpack
2995 -- arrays if they are limited).
2997 -- Note that we check the root type explicitly because the
2998 -- whole point is we are doing this test before we have had
2999 -- a chance to freeze the base type (and it is that freeze
3000 -- action that causes stuff to be inherited).
3002 if Present (Size_Clause (E))
3003 and then Known_Static_Esize (E)
3004 and then not Is_Packed (E)
3005 and then not Has_Pragma_Pack (E)
3006 and then Number_Dimensions (E) = 1
3007 and then not Has_Component_Size_Clause (E)
3008 and then Known_Static_Esize (Ctyp)
3009 and then not Is_Limited_Composite (E)
3010 and then not Is_Packed (Root_Type (E))
3011 and then not Has_Component_Size_Clause (Root_Type (E))
3012 and then not CodePeer_Mode
3014 Get_Index_Bounds (First_Index (E), Lo, Hi);
3016 if Compile_Time_Known_Value (Lo)
3017 and then Compile_Time_Known_Value (Hi)
3018 and then Known_Static_RM_Size (Ctyp)
3019 and then RM_Size (Ctyp) < 64
3022 Lov : constant Uint := Expr_Value (Lo);
3023 Hiv : constant Uint := Expr_Value (Hi);
3024 Len : constant Uint := UI_Max
3027 Rsiz : constant Uint := RM_Size (Ctyp);
3028 SZ : constant Node_Id := Size_Clause (E);
3029 Btyp : constant Entity_Id := Base_Type (E);
3031 -- What we are looking for here is the situation where
3032 -- the RM_Size given would be exactly right if there
3033 -- was a pragma Pack (resulting in the component size
3034 -- being the same as the RM_Size). Furthermore, the
3035 -- component type size must be an odd size (not a
3036 -- multiple of storage unit). If the component RM size
3037 -- is an exact number of storage units that is a power
3038 -- of two, the array is not packed and has a standard
3042 if RM_Size (E) = Len * Rsiz
3043 and then Rsiz mod System_Storage_Unit /= 0
3045 -- For implicit packing mode, just set the
3046 -- component size silently.
3048 if Implicit_Packing then
3049 Set_Component_Size (Btyp, Rsiz);
3050 Set_Is_Bit_Packed_Array (Btyp);
3051 Set_Is_Packed (Btyp);
3052 Set_Has_Non_Standard_Rep (Btyp);
3054 -- Otherwise give an error message
3058 ("size given for& too small", SZ, E);
3059 Error_Msg_N -- CODEFIX
3060 ("\use explicit pragma Pack "
3061 & "or use pragma Implicit_Packing", SZ);
3064 elsif RM_Size (E) = Len * Rsiz
3065 and then Implicit_Packing
3067 (Rsiz / System_Storage_Unit = 1
3068 or else Rsiz / System_Storage_Unit = 2
3069 or else Rsiz / System_Storage_Unit = 4)
3072 -- Not a packed array, but indicate the desired
3073 -- component size, for the back-end.
3075 Set_Component_Size (Btyp, Rsiz);
3083 -- If ancestor subtype present, freeze that first. Note that this
3084 -- will also get the base type frozen.
3086 Atype := Ancestor_Subtype (E);
3088 if Present (Atype) then
3089 Freeze_And_Append (Atype, Loc, Result);
3091 -- Otherwise freeze the base type of the entity before freezing
3092 -- the entity itself (RM 13.14(15)).
3094 elsif E /= Base_Type (E) then
3095 Freeze_And_Append (Base_Type (E), Loc, Result);
3098 -- For a derived type, freeze its parent type first (RM 13.14(15))
3100 elsif Is_Derived_Type (E) then
3101 Freeze_And_Append (Etype (E), Loc, Result);
3102 Freeze_And_Append (First_Subtype (Etype (E)), Loc, Result);
3105 -- For array type, freeze index types and component type first
3106 -- before freezing the array (RM 13.14(15)).
3108 if Is_Array_Type (E) then
3110 Ctyp : constant Entity_Id := Component_Type (E);
3112 Non_Standard_Enum : Boolean := False;
3113 -- Set true if any of the index types is an enumeration type
3114 -- with a non-standard representation.
3117 Freeze_And_Append (Ctyp, Loc, Result);
3119 Indx := First_Index (E);
3120 while Present (Indx) loop
3121 Freeze_And_Append (Etype (Indx), Loc, Result);
3123 if Is_Enumeration_Type (Etype (Indx))
3124 and then Has_Non_Standard_Rep (Etype (Indx))
3126 Non_Standard_Enum := True;
3132 -- Processing that is done only for base types
3134 if Ekind (E) = E_Array_Type then
3136 -- Propagate flags for component type
3138 if Is_Controlled (Component_Type (E))
3139 or else Has_Controlled_Component (Ctyp)
3141 Set_Has_Controlled_Component (E);
3144 if Has_Unchecked_Union (Component_Type (E)) then
3145 Set_Has_Unchecked_Union (E);
3148 -- If packing was requested or if the component size was set
3149 -- explicitly, then see if bit packing is required. This
3150 -- processing is only done for base types, since all the
3151 -- representation aspects involved are type-related. This
3152 -- is not just an optimization, if we start processing the
3153 -- subtypes, they interfere with the settings on the base
3154 -- type (this is because Is_Packed has a slightly different
3155 -- meaning before and after freezing).
3162 if (Is_Packed (E) or else Has_Pragma_Pack (E))
3163 and then not Has_Atomic_Components (E)
3164 and then Known_Static_RM_Size (Ctyp)
3166 Csiz := UI_Max (RM_Size (Ctyp), 1);
3168 elsif Known_Component_Size (E) then
3169 Csiz := Component_Size (E);
3171 elsif not Known_Static_Esize (Ctyp) then
3175 Esiz := Esize (Ctyp);
3177 -- We can set the component size if it is less than
3178 -- 16, rounding it up to the next storage unit size.
3182 elsif Esiz <= 16 then
3188 -- Set component size up to match alignment if it
3189 -- would otherwise be less than the alignment. This
3190 -- deals with cases of types whose alignment exceeds
3191 -- their size (padded types).
3195 A : constant Uint := Alignment_In_Bits (Ctyp);
3204 -- Case of component size that may result in packing
3206 if 1 <= Csiz and then Csiz <= 64 then
3208 Ent : constant Entity_Id :=
3210 Pack_Pragma : constant Node_Id :=
3211 Get_Rep_Pragma (Ent, Name_Pack);
3212 Comp_Size_C : constant Node_Id :=
3213 Get_Attribute_Definition_Clause
3214 (Ent, Attribute_Component_Size);
3216 -- Warn if we have pack and component size so that
3217 -- the pack is ignored.
3219 -- Note: here we must check for the presence of a
3220 -- component size before checking for a Pack pragma
3221 -- to deal with the case where the array type is a
3222 -- derived type whose parent is currently private.
3224 if Present (Comp_Size_C)
3225 and then Has_Pragma_Pack (Ent)
3227 Error_Msg_Sloc := Sloc (Comp_Size_C);
3229 ("?pragma Pack for& ignored!",
3232 ("\?explicit component size given#!",
3236 -- Set component size if not already set by a
3237 -- component size clause.
3239 if not Present (Comp_Size_C) then
3240 Set_Component_Size (E, Csiz);
3243 -- Check for base type of 8, 16, 32 bits, where an
3244 -- unsigned subtype has a length one less than the
3245 -- base type (e.g. Natural subtype of Integer).
3247 -- In such cases, if a component size was not set
3248 -- explicitly, then generate a warning.
3250 if Has_Pragma_Pack (E)
3251 and then not Present (Comp_Size_C)
3253 (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
3254 and then Esize (Base_Type (Ctyp)) = Csiz + 1
3256 Error_Msg_Uint_1 := Csiz;
3258 if Present (Pack_Pragma) then
3260 ("?pragma Pack causes component size "
3261 & "to be ^!", Pack_Pragma);
3263 ("\?use Component_Size to set "
3264 & "desired value!", Pack_Pragma);
3268 -- Actual packing is not needed for 8, 16, 32, 64.
3269 -- Also not needed for 24 if alignment is 1.
3275 or else (Csiz = 24 and then Alignment (Ctyp) = 1)
3277 -- Here the array was requested to be packed,
3278 -- but the packing request had no effect, so
3279 -- Is_Packed is reset.
3281 -- Note: semantically this means that we lose
3282 -- track of the fact that a derived type
3283 -- inherited a pragma Pack that was non-
3284 -- effective, but that seems fine.
3286 -- We regard a Pack pragma as a request to set
3287 -- a representation characteristic, and this
3288 -- request may be ignored.
3290 Set_Is_Packed (Base_Type (E), False);
3292 -- In all other cases, packing is indeed needed
3295 Set_Has_Non_Standard_Rep (Base_Type (E));
3296 Set_Is_Bit_Packed_Array (Base_Type (E));
3297 Set_Is_Packed (Base_Type (E));
3303 -- Processing that is done only for subtypes
3306 -- Acquire alignment from base type
3308 if Unknown_Alignment (E) then
3309 Set_Alignment (E, Alignment (Base_Type (E)));
3310 Adjust_Esize_Alignment (E);
3314 -- For bit-packed arrays, check the size
3316 if Is_Bit_Packed_Array (E) and then Known_RM_Size (E) then
3318 SizC : constant Node_Id := Size_Clause (E);
3321 pragma Warnings (Off, Discard);
3324 -- It is not clear if it is possible to have no size
3325 -- clause at this stage, but it is not worth worrying
3326 -- about. Post error on the entity name in the size
3327 -- clause if present, else on the type entity itself.
3329 if Present (SizC) then
3330 Check_Size (Name (SizC), E, RM_Size (E), Discard);
3332 Check_Size (E, E, RM_Size (E), Discard);
3337 -- If any of the index types was an enumeration type with
3338 -- a non-standard rep clause, then we indicate that the
3339 -- array type is always packed (even if it is not bit packed).
3341 if Non_Standard_Enum then
3342 Set_Has_Non_Standard_Rep (Base_Type (E));
3343 Set_Is_Packed (Base_Type (E));
3346 Set_Component_Alignment_If_Not_Set (E);
3348 -- If the array is packed, we must create the packed array
3349 -- type to be used to actually implement the type. This is
3350 -- only needed for real array types (not for string literal
3351 -- types, since they are present only for the front end).
3354 and then Ekind (E) /= E_String_Literal_Subtype
3356 Create_Packed_Array_Type (E);
3357 Freeze_And_Append (Packed_Array_Type (E), Loc, Result);
3359 -- Size information of packed array type is copied to the
3360 -- array type, since this is really the representation. But
3361 -- do not override explicit existing size values. If the
3362 -- ancestor subtype is constrained the packed_array_type
3363 -- will be inherited from it, but the size may have been
3364 -- provided already, and must not be overridden either.
3366 if not Has_Size_Clause (E)
3368 (No (Ancestor_Subtype (E))
3369 or else not Has_Size_Clause (Ancestor_Subtype (E)))
3371 Set_Esize (E, Esize (Packed_Array_Type (E)));
3372 Set_RM_Size (E, RM_Size (Packed_Array_Type (E)));
3375 if not Has_Alignment_Clause (E) then
3376 Set_Alignment (E, Alignment (Packed_Array_Type (E)));
3380 -- For non-packed arrays set the alignment of the array to the
3381 -- alignment of the component type if it is unknown. Skip this
3382 -- in atomic case (atomic arrays may need larger alignments).
3384 if not Is_Packed (E)
3385 and then Unknown_Alignment (E)
3386 and then Known_Alignment (Ctyp)
3387 and then Known_Static_Component_Size (E)
3388 and then Known_Static_Esize (Ctyp)
3389 and then Esize (Ctyp) = Component_Size (E)
3390 and then not Is_Atomic (E)
3392 Set_Alignment (E, Alignment (Component_Type (E)));
3396 -- For a class-wide type, the corresponding specific type is
3397 -- frozen as well (RM 13.14(15))
3399 elsif Is_Class_Wide_Type (E) then
3400 Freeze_And_Append (Root_Type (E), Loc, Result);
3402 -- If the base type of the class-wide type is still incomplete,
3403 -- the class-wide remains unfrozen as well. This is legal when
3404 -- E is the formal of a primitive operation of some other type
3405 -- which is being frozen.
3407 if not Is_Frozen (Root_Type (E)) then
3408 Set_Is_Frozen (E, False);
3412 -- If the Class_Wide_Type is an Itype (when type is the anonymous
3413 -- parent of a derived type) and it is a library-level entity,
3414 -- generate an itype reference for it. Otherwise, its first
3415 -- explicit reference may be in an inner scope, which will be
3416 -- rejected by the back-end.
3419 and then Is_Compilation_Unit (Scope (E))
3422 Ref : constant Node_Id := Make_Itype_Reference (Loc);
3427 Result := New_List (Ref);
3429 Append (Ref, Result);
3434 -- The equivalent type associated with a class-wide subtype needs
3435 -- to be frozen to ensure that its layout is done.
3437 if Ekind (E) = E_Class_Wide_Subtype
3438 and then Present (Equivalent_Type (E))
3440 Freeze_And_Append (Equivalent_Type (E), Loc, Result);
3443 -- For a record (sub)type, freeze all the component types (RM
3444 -- 13.14(15). We test for E_Record_(sub)Type here, rather than using
3445 -- Is_Record_Type, because we don't want to attempt the freeze for
3446 -- the case of a private type with record extension (we will do that
3447 -- later when the full type is frozen).
3449 elsif Ekind (E) = E_Record_Type
3450 or else Ekind (E) = E_Record_Subtype
3452 Freeze_Record_Type (E);
3454 -- For a concurrent type, freeze corresponding record type. This
3455 -- does not correspond to any specific rule in the RM, but the
3456 -- record type is essentially part of the concurrent type.
3457 -- Freeze as well all local entities. This includes record types
3458 -- created for entry parameter blocks, and whatever local entities
3459 -- may appear in the private part.
3461 elsif Is_Concurrent_Type (E) then
3462 if Present (Corresponding_Record_Type (E)) then
3464 (Corresponding_Record_Type (E), Loc, Result);
3467 Comp := First_Entity (E);
3468 while Present (Comp) loop
3469 if Is_Type (Comp) then
3470 Freeze_And_Append (Comp, Loc, Result);
3472 elsif (Ekind (Comp)) /= E_Function then
3473 if Is_Itype (Etype (Comp))
3474 and then Underlying_Type (Scope (Etype (Comp))) = E
3476 Undelay_Type (Etype (Comp));
3479 Freeze_And_Append (Etype (Comp), Loc, Result);
3485 -- Private types are required to point to the same freeze node as
3486 -- their corresponding full views. The freeze node itself has to
3487 -- point to the partial view of the entity (because from the partial
3488 -- view, we can retrieve the full view, but not the reverse).
3489 -- However, in order to freeze correctly, we need to freeze the full
3490 -- view. If we are freezing at the end of a scope (or within the
3491 -- scope of the private type), the partial and full views will have
3492 -- been swapped, the full view appears first in the entity chain and
3493 -- the swapping mechanism ensures that the pointers are properly set
3496 -- If we encounter the partial view before the full view (e.g. when
3497 -- freezing from another scope), we freeze the full view, and then
3498 -- set the pointers appropriately since we cannot rely on swapping to
3499 -- fix things up (subtypes in an outer scope might not get swapped).
3501 elsif Is_Incomplete_Or_Private_Type (E)
3502 and then not Is_Generic_Type (E)
3504 -- The construction of the dispatch table associated with library
3505 -- level tagged types forces freezing of all the primitives of the
3506 -- type, which may cause premature freezing of the partial view.
3510 -- type T is tagged private;
3511 -- type DT is new T with private;
3512 -- procedure Prim (X : in out T; Y : in out DT'class);
3514 -- type T is tagged null record;
3516 -- type DT is new T with null record;
3519 -- In this case the type will be frozen later by the usual
3520 -- mechanism: an object declaration, an instantiation, or the
3521 -- end of a declarative part.
3523 if Is_Library_Level_Tagged_Type (E)
3524 and then not Present (Full_View (E))
3526 Set_Is_Frozen (E, False);
3529 -- Case of full view present
3531 elsif Present (Full_View (E)) then
3533 -- If full view has already been frozen, then no further
3534 -- processing is required
3536 if Is_Frozen (Full_View (E)) then
3538 Set_Has_Delayed_Freeze (E, False);
3539 Set_Freeze_Node (E, Empty);
3540 Check_Debug_Info_Needed (E);
3542 -- Otherwise freeze full view and patch the pointers so that
3543 -- the freeze node will elaborate both views in the back-end.
3547 Full : constant Entity_Id := Full_View (E);
3550 if Is_Private_Type (Full)
3551 and then Present (Underlying_Full_View (Full))
3554 (Underlying_Full_View (Full), Loc, Result);
3557 Freeze_And_Append (Full, Loc, Result);
3559 if Has_Delayed_Freeze (E) then
3560 F_Node := Freeze_Node (Full);
3562 if Present (F_Node) then
3563 Set_Freeze_Node (E, F_Node);
3564 Set_Entity (F_Node, E);
3567 -- {Incomplete,Private}_Subtypes with Full_Views
3568 -- constrained by discriminants.
3570 Set_Has_Delayed_Freeze (E, False);
3571 Set_Freeze_Node (E, Empty);
3576 Check_Debug_Info_Needed (E);
3579 -- AI-117 requires that the convention of a partial view be the
3580 -- same as the convention of the full view. Note that this is a
3581 -- recognized breach of privacy, but it's essential for logical
3582 -- consistency of representation, and the lack of a rule in
3583 -- RM95 was an oversight.
3585 Set_Convention (E, Convention (Full_View (E)));
3587 Set_Size_Known_At_Compile_Time (E,
3588 Size_Known_At_Compile_Time (Full_View (E)));
3590 -- Size information is copied from the full view to the
3591 -- incomplete or private view for consistency.
3593 -- We skip this is the full view is not a type. This is very
3594 -- strange of course, and can only happen as a result of
3595 -- certain illegalities, such as a premature attempt to derive
3596 -- from an incomplete type.
3598 if Is_Type (Full_View (E)) then
3599 Set_Size_Info (E, Full_View (E));
3600 Set_RM_Size (E, RM_Size (Full_View (E)));
3605 -- Case of no full view present. If entity is derived or subtype,
3606 -- it is safe to freeze, correctness depends on the frozen status
3607 -- of parent. Otherwise it is either premature usage, or a Taft
3608 -- amendment type, so diagnosis is at the point of use and the
3609 -- type might be frozen later.
3611 elsif E /= Base_Type (E)
3612 or else Is_Derived_Type (E)
3617 Set_Is_Frozen (E, False);
3621 -- For access subprogram, freeze types of all formals, the return
3622 -- type was already frozen, since it is the Etype of the function.
3623 -- Formal types can be tagged Taft amendment types, but otherwise
3624 -- they cannot be incomplete.
3626 elsif Ekind (E) = E_Subprogram_Type then
3627 Formal := First_Formal (E);
3629 while Present (Formal) loop
3630 if Ekind (Etype (Formal)) = E_Incomplete_Type
3631 and then No (Full_View (Etype (Formal)))
3632 and then not Is_Value_Type (Etype (Formal))
3634 if Is_Tagged_Type (Etype (Formal)) then
3638 ("invalid use of incomplete type&", E, Etype (Formal));
3642 Freeze_And_Append (Etype (Formal), Loc, Result);
3643 Next_Formal (Formal);
3646 Freeze_Subprogram (E);
3648 -- For access to a protected subprogram, freeze the equivalent type
3649 -- (however this is not set if we are not generating code or if this
3650 -- is an anonymous type used just for resolution).
3652 elsif Is_Access_Protected_Subprogram_Type (E) then
3653 if Present (Equivalent_Type (E)) then
3654 Freeze_And_Append (Equivalent_Type (E), Loc, Result);
3658 -- Generic types are never seen by the back-end, and are also not
3659 -- processed by the expander (since the expander is turned off for
3660 -- generic processing), so we never need freeze nodes for them.
3662 if Is_Generic_Type (E) then
3666 -- Some special processing for non-generic types to complete
3667 -- representation details not known till the freeze point.
3669 if Is_Fixed_Point_Type (E) then
3670 Freeze_Fixed_Point_Type (E);
3672 -- Some error checks required for ordinary fixed-point type. Defer
3673 -- these till the freeze-point since we need the small and range
3674 -- values. We only do these checks for base types
3676 if Is_Ordinary_Fixed_Point_Type (E)
3677 and then E = Base_Type (E)
3679 if Small_Value (E) < Ureal_2_M_80 then
3680 Error_Msg_Name_1 := Name_Small;
3682 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
3684 elsif Small_Value (E) > Ureal_2_80 then
3685 Error_Msg_Name_1 := Name_Small;
3687 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
3690 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
3691 Error_Msg_Name_1 := Name_First;
3693 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
3696 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
3697 Error_Msg_Name_1 := Name_Last;
3699 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
3703 elsif Is_Enumeration_Type (E) then
3704 Freeze_Enumeration_Type (E);
3706 elsif Is_Integer_Type (E) then
3707 Adjust_Esize_For_Alignment (E);
3709 if Is_Modular_Integer_Type (E)
3710 and then Warn_On_Suspicious_Modulus_Value
3712 Check_Suspicious_Modulus (E);
3715 elsif Is_Access_Type (E) then
3717 -- Check restriction for standard storage pool
3719 if No (Associated_Storage_Pool (E)) then
3720 Check_Restriction (No_Standard_Storage_Pools, E);
3723 -- Deal with error message for pure access type. This is not an
3724 -- error in Ada 2005 if there is no pool (see AI-366).
3726 if Is_Pure_Unit_Access_Type (E)
3727 and then (Ada_Version < Ada_05
3728 or else not No_Pool_Assigned (E))
3730 Error_Msg_N ("named access type not allowed in pure unit", E);
3732 if Ada_Version >= Ada_05 then
3734 ("\would be legal if Storage_Size of 0 given?", E);
3736 elsif No_Pool_Assigned (E) then
3738 ("\would be legal in Ada 2005?", E);
3742 ("\would be legal in Ada 2005 if "
3743 & "Storage_Size of 0 given?", E);
3748 -- Case of composite types
3750 if Is_Composite_Type (E) then
3752 -- AI-117 requires that all new primitives of a tagged type must
3753 -- inherit the convention of the full view of the type. Inherited
3754 -- and overriding operations are defined to inherit the convention
3755 -- of their parent or overridden subprogram (also specified in
3756 -- AI-117), which will have occurred earlier (in Derive_Subprogram
3757 -- and New_Overloaded_Entity). Here we set the convention of
3758 -- primitives that are still convention Ada, which will ensure
3759 -- that any new primitives inherit the type's convention. Class-
3760 -- wide types can have a foreign convention inherited from their
3761 -- specific type, but are excluded from this since they don't have
3762 -- any associated primitives.
3764 if Is_Tagged_Type (E)
3765 and then not Is_Class_Wide_Type (E)
3766 and then Convention (E) /= Convention_Ada
3769 Prim_List : constant Elist_Id := Primitive_Operations (E);
3772 Prim := First_Elmt (Prim_List);
3773 while Present (Prim) loop
3774 if Convention (Node (Prim)) = Convention_Ada then
3775 Set_Convention (Node (Prim), Convention (E));
3784 -- Now that all types from which E may depend are frozen, see if the
3785 -- size is known at compile time, if it must be unsigned, or if
3786 -- strict alignment is required
3788 Check_Compile_Time_Size (E);
3789 Check_Unsigned_Type (E);
3791 if Base_Type (E) = E then
3792 Check_Strict_Alignment (E);
3795 -- Do not allow a size clause for a type which does not have a size
3796 -- that is known at compile time
3798 if Has_Size_Clause (E)
3799 and then not Size_Known_At_Compile_Time (E)
3801 -- Suppress this message if errors posted on E, even if we are
3802 -- in all errors mode, since this is often a junk message
3804 if not Error_Posted (E) then
3806 ("size clause not allowed for variable length type",
3811 -- Remaining process is to set/verify the representation information,
3812 -- in particular the size and alignment values. This processing is
3813 -- not required for generic types, since generic types do not play
3814 -- any part in code generation, and so the size and alignment values
3815 -- for such types are irrelevant.
3817 if Is_Generic_Type (E) then
3820 -- Otherwise we call the layout procedure
3826 -- End of freeze processing for type entities
3829 -- Here is where we logically freeze the current entity. If it has a
3830 -- freeze node, then this is the point at which the freeze node is
3831 -- linked into the result list.
3833 if Has_Delayed_Freeze (E) then
3835 -- If a freeze node is already allocated, use it, otherwise allocate
3836 -- a new one. The preallocation happens in the case of anonymous base
3837 -- types, where we preallocate so that we can set First_Subtype_Link.
3838 -- Note that we reset the Sloc to the current freeze location.
3840 if Present (Freeze_Node (E)) then
3841 F_Node := Freeze_Node (E);
3842 Set_Sloc (F_Node, Loc);
3845 F_Node := New_Node (N_Freeze_Entity, Loc);
3846 Set_Freeze_Node (E, F_Node);
3847 Set_Access_Types_To_Process (F_Node, No_Elist);
3848 Set_TSS_Elist (F_Node, No_Elist);
3849 Set_Actions (F_Node, No_List);
3852 Set_Entity (F_Node, E);
3854 if Result = No_List then
3855 Result := New_List (F_Node);
3857 Append (F_Node, Result);
3860 -- A final pass over record types with discriminants. If the type
3861 -- has an incomplete declaration, there may be constrained access
3862 -- subtypes declared elsewhere, which do not depend on the discrimi-
3863 -- nants of the type, and which are used as component types (i.e.
3864 -- the full view is a recursive type). The designated types of these
3865 -- subtypes can only be elaborated after the type itself, and they
3866 -- need an itype reference.
3868 if Ekind (E) = E_Record_Type
3869 and then Has_Discriminants (E)
3877 Comp := First_Component (E);
3879 while Present (Comp) loop
3880 Typ := Etype (Comp);
3882 if Ekind (Comp) = E_Component
3883 and then Is_Access_Type (Typ)
3884 and then Scope (Typ) /= E
3885 and then Base_Type (Designated_Type (Typ)) = E
3886 and then Is_Itype (Designated_Type (Typ))
3888 IR := Make_Itype_Reference (Sloc (Comp));
3889 Set_Itype (IR, Designated_Type (Typ));
3890 Append (IR, Result);
3893 Next_Component (Comp);
3899 -- When a type is frozen, the first subtype of the type is frozen as
3900 -- well (RM 13.14(15)). This has to be done after freezing the type,
3901 -- since obviously the first subtype depends on its own base type.
3904 Freeze_And_Append (First_Subtype (E), Loc, Result);
3906 -- If we just froze a tagged non-class wide record, then freeze the
3907 -- corresponding class-wide type. This must be done after the tagged
3908 -- type itself is frozen, because the class-wide type refers to the
3909 -- tagged type which generates the class.
3911 if Is_Tagged_Type (E)
3912 and then not Is_Class_Wide_Type (E)
3913 and then Present (Class_Wide_Type (E))
3915 Freeze_And_Append (Class_Wide_Type (E), Loc, Result);
3919 Check_Debug_Info_Needed (E);
3921 -- Special handling for subprograms
3923 if Is_Subprogram (E) then
3925 -- If subprogram has address clause then reset Is_Public flag, since
3926 -- we do not want the backend to generate external references.
3928 if Present (Address_Clause (E))
3929 and then not Is_Library_Level_Entity (E)
3931 Set_Is_Public (E, False);
3933 -- If no address clause and not intrinsic, then for imported
3934 -- subprogram in main unit, generate descriptor if we are in
3935 -- Propagate_Exceptions mode.
3937 elsif Propagate_Exceptions
3938 and then Is_Imported (E)
3939 and then not Is_Intrinsic_Subprogram (E)
3940 and then Convention (E) /= Convention_Stubbed
3942 if Result = No_List then
3943 Result := Empty_List;
3951 -----------------------------
3952 -- Freeze_Enumeration_Type --
3953 -----------------------------
3955 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
3957 -- By default, if no size clause is present, an enumeration type with
3958 -- Convention C is assumed to interface to a C enum, and has integer
3959 -- size. This applies to types. For subtypes, verify that its base
3960 -- type has no size clause either.
3962 if Has_Foreign_Convention (Typ)
3963 and then not Has_Size_Clause (Typ)
3964 and then not Has_Size_Clause (Base_Type (Typ))
3965 and then Esize (Typ) < Standard_Integer_Size
3967 Init_Esize (Typ, Standard_Integer_Size);
3970 -- If the enumeration type interfaces to C, and it has a size clause
3971 -- that specifies less than int size, it warrants a warning. The
3972 -- user may intend the C type to be an enum or a char, so this is
3973 -- not by itself an error that the Ada compiler can detect, but it
3974 -- it is a worth a heads-up. For Boolean and Character types we
3975 -- assume that the programmer has the proper C type in mind.
3977 if Convention (Typ) = Convention_C
3978 and then Has_Size_Clause (Typ)
3979 and then Esize (Typ) /= Esize (Standard_Integer)
3980 and then not Is_Boolean_Type (Typ)
3981 and then not Is_Character_Type (Typ)
3984 ("C enum types have the size of a C int?", Size_Clause (Typ));
3987 Adjust_Esize_For_Alignment (Typ);
3989 end Freeze_Enumeration_Type;
3991 -----------------------
3992 -- Freeze_Expression --
3993 -----------------------
3995 procedure Freeze_Expression (N : Node_Id) is
3996 In_Spec_Exp : constant Boolean := In_Spec_Expression;
3999 Desig_Typ : Entity_Id;
4003 Freeze_Outside : Boolean := False;
4004 -- This flag is set true if the entity must be frozen outside the
4005 -- current subprogram. This happens in the case of expander generated
4006 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
4007 -- not freeze all entities like other bodies, but which nevertheless
4008 -- may reference entities that have to be frozen before the body and
4009 -- obviously cannot be frozen inside the body.
4011 function In_Exp_Body (N : Node_Id) return Boolean;
4012 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
4013 -- it is the handled statement sequence of an expander-generated
4014 -- subprogram (init proc, stream subprogram, or renaming as body).
4015 -- If so, this is not a freezing context.
4021 function In_Exp_Body (N : Node_Id) return Boolean is
4026 if Nkind (N) = N_Subprogram_Body then
4032 if Nkind (P) /= N_Subprogram_Body then
4036 Id := Defining_Unit_Name (Specification (P));
4038 if Nkind (Id) = N_Defining_Identifier
4039 and then (Is_Init_Proc (Id) or else
4040 Is_TSS (Id, TSS_Stream_Input) or else
4041 Is_TSS (Id, TSS_Stream_Output) or else
4042 Is_TSS (Id, TSS_Stream_Read) or else
4043 Is_TSS (Id, TSS_Stream_Write) or else
4044 Nkind (Original_Node (P)) =
4045 N_Subprogram_Renaming_Declaration)
4054 -- Start of processing for Freeze_Expression
4057 -- Immediate return if freezing is inhibited. This flag is set by the
4058 -- analyzer to stop freezing on generated expressions that would cause
4059 -- freezing if they were in the source program, but which are not
4060 -- supposed to freeze, since they are created.
4062 if Must_Not_Freeze (N) then
4066 -- If expression is non-static, then it does not freeze in a default
4067 -- expression, see section "Handling of Default Expressions" in the
4068 -- spec of package Sem for further details. Note that we have to
4069 -- make sure that we actually have a real expression (if we have
4070 -- a subtype indication, we can't test Is_Static_Expression!)
4073 and then Nkind (N) in N_Subexpr
4074 and then not Is_Static_Expression (N)
4079 -- Freeze type of expression if not frozen already
4083 if Nkind (N) in N_Has_Etype then
4084 if not Is_Frozen (Etype (N)) then
4087 -- Base type may be an derived numeric type that is frozen at
4088 -- the point of declaration, but first_subtype is still unfrozen.
4090 elsif not Is_Frozen (First_Subtype (Etype (N))) then
4091 Typ := First_Subtype (Etype (N));
4095 -- For entity name, freeze entity if not frozen already. A special
4096 -- exception occurs for an identifier that did not come from source.
4097 -- We don't let such identifiers freeze a non-internal entity, i.e.
4098 -- an entity that did come from source, since such an identifier was
4099 -- generated by the expander, and cannot have any semantic effect on
4100 -- the freezing semantics. For example, this stops the parameter of
4101 -- an initialization procedure from freezing the variable.
4103 if Is_Entity_Name (N)
4104 and then not Is_Frozen (Entity (N))
4105 and then (Nkind (N) /= N_Identifier
4106 or else Comes_From_Source (N)
4107 or else not Comes_From_Source (Entity (N)))
4114 -- For an allocator freeze designated type if not frozen already
4116 -- For an aggregate whose component type is an access type, freeze the
4117 -- designated type now, so that its freeze does not appear within the
4118 -- loop that might be created in the expansion of the aggregate. If the
4119 -- designated type is a private type without full view, the expression
4120 -- cannot contain an allocator, so the type is not frozen.
4122 -- For a function, we freeze the entity when the subprogram declaration
4123 -- is frozen, but a function call may appear in an initialization proc.
4124 -- before the declaration is frozen. We need to generate the extra
4125 -- formals, if any, to ensure that the expansion of the call includes
4126 -- the proper actuals. This only applies to Ada subprograms, not to
4133 Desig_Typ := Designated_Type (Etype (N));
4136 if Is_Array_Type (Etype (N))
4137 and then Is_Access_Type (Component_Type (Etype (N)))
4139 Desig_Typ := Designated_Type (Component_Type (Etype (N)));
4142 when N_Selected_Component |
4143 N_Indexed_Component |
4146 if Is_Access_Type (Etype (Prefix (N))) then
4147 Desig_Typ := Designated_Type (Etype (Prefix (N)));
4150 when N_Identifier =>
4152 and then Ekind (Nam) = E_Function
4153 and then Nkind (Parent (N)) = N_Function_Call
4154 and then Convention (Nam) = Convention_Ada
4156 Create_Extra_Formals (Nam);
4163 if Desig_Typ /= Empty
4164 and then (Is_Frozen (Desig_Typ)
4165 or else (not Is_Fully_Defined (Desig_Typ)))
4170 -- All done if nothing needs freezing
4174 and then No (Desig_Typ)
4179 -- Loop for looking at the right place to insert the freeze nodes,
4180 -- exiting from the loop when it is appropriate to insert the freeze
4181 -- node before the current node P.
4183 -- Also checks some special exceptions to the freezing rules. These
4184 -- cases result in a direct return, bypassing the freeze action.
4188 Parent_P := Parent (P);
4190 -- If we don't have a parent, then we are not in a well-formed tree.
4191 -- This is an unusual case, but there are some legitimate situations
4192 -- in which this occurs, notably when the expressions in the range of
4193 -- a type declaration are resolved. We simply ignore the freeze
4194 -- request in this case. Is this right ???
4196 if No (Parent_P) then
4200 -- See if we have got to an appropriate point in the tree
4202 case Nkind (Parent_P) is
4204 -- A special test for the exception of (RM 13.14(8)) for the case
4205 -- of per-object expressions (RM 3.8(18)) occurring in component
4206 -- definition or a discrete subtype definition. Note that we test
4207 -- for a component declaration which includes both cases we are
4208 -- interested in, and furthermore the tree does not have explicit
4209 -- nodes for either of these two constructs.
4211 when N_Component_Declaration =>
4213 -- The case we want to test for here is an identifier that is
4214 -- a per-object expression, this is either a discriminant that
4215 -- appears in a context other than the component declaration
4216 -- or it is a reference to the type of the enclosing construct.
4218 -- For either of these cases, we skip the freezing
4220 if not In_Spec_Expression
4221 and then Nkind (N) = N_Identifier
4222 and then (Present (Entity (N)))
4224 -- We recognize the discriminant case by just looking for
4225 -- a reference to a discriminant. It can only be one for
4226 -- the enclosing construct. Skip freezing in this case.
4228 if Ekind (Entity (N)) = E_Discriminant then
4231 -- For the case of a reference to the enclosing record,
4232 -- (or task or protected type), we look for a type that
4233 -- matches the current scope.
4235 elsif Entity (N) = Current_Scope then
4240 -- If we have an enumeration literal that appears as the choice in
4241 -- the aggregate of an enumeration representation clause, then
4242 -- freezing does not occur (RM 13.14(10)).
4244 when N_Enumeration_Representation_Clause =>
4246 -- The case we are looking for is an enumeration literal
4248 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
4249 and then Is_Enumeration_Type (Etype (N))
4251 -- If enumeration literal appears directly as the choice,
4252 -- do not freeze (this is the normal non-overloaded case)
4254 if Nkind (Parent (N)) = N_Component_Association
4255 and then First (Choices (Parent (N))) = N
4259 -- If enumeration literal appears as the name of function
4260 -- which is the choice, then also do not freeze. This
4261 -- happens in the overloaded literal case, where the
4262 -- enumeration literal is temporarily changed to a function
4263 -- call for overloading analysis purposes.
4265 elsif Nkind (Parent (N)) = N_Function_Call
4267 Nkind (Parent (Parent (N))) = N_Component_Association
4269 First (Choices (Parent (Parent (N)))) = Parent (N)
4275 -- Normally if the parent is a handled sequence of statements,
4276 -- then the current node must be a statement, and that is an
4277 -- appropriate place to insert a freeze node.
4279 when N_Handled_Sequence_Of_Statements =>
4281 -- An exception occurs when the sequence of statements is for
4282 -- an expander generated body that did not do the usual freeze
4283 -- all operation. In this case we usually want to freeze
4284 -- outside this body, not inside it, and we skip past the
4285 -- subprogram body that we are inside.
4287 if In_Exp_Body (Parent_P) then
4289 -- However, we *do* want to freeze at this point if we have
4290 -- an entity to freeze, and that entity is declared *inside*
4291 -- the body of the expander generated procedure. This case
4292 -- is recognized by the scope of the type, which is either
4293 -- the spec for some enclosing body, or (in the case of
4294 -- init_procs, for which there are no separate specs) the
4298 Subp : constant Node_Id := Parent (Parent_P);
4302 if Nkind (Subp) = N_Subprogram_Body then
4303 Cspc := Corresponding_Spec (Subp);
4305 if (Present (Typ) and then Scope (Typ) = Cspc)
4307 (Present (Nam) and then Scope (Nam) = Cspc)
4312 and then Scope (Typ) = Current_Scope
4313 and then Current_Scope = Defining_Entity (Subp)
4320 -- If not that exception to the exception, then this is
4321 -- where we delay the freeze till outside the body.
4323 Parent_P := Parent (Parent_P);
4324 Freeze_Outside := True;
4326 -- Here if normal case where we are in handled statement
4327 -- sequence and want to do the insertion right there.
4333 -- If parent is a body or a spec or a block, then the current node
4334 -- is a statement or declaration and we can insert the freeze node
4337 when N_Package_Specification |
4343 N_Block_Statement => exit;
4345 -- The expander is allowed to define types in any statements list,
4346 -- so any of the following parent nodes also mark a freezing point
4347 -- if the actual node is in a list of statements or declarations.
4349 when N_Exception_Handler |
4352 N_Case_Statement_Alternative |
4353 N_Compilation_Unit_Aux |
4354 N_Selective_Accept |
4355 N_Accept_Alternative |
4356 N_Delay_Alternative |
4357 N_Conditional_Entry_Call |
4358 N_Entry_Call_Alternative |
4359 N_Triggering_Alternative |
4365 exit when Is_List_Member (P);
4367 -- Note: The N_Loop_Statement is a special case. A type that
4368 -- appears in the source can never be frozen in a loop (this
4369 -- occurs only because of a loop expanded by the expander), so we
4370 -- keep on going. Otherwise we terminate the search. Same is true
4371 -- of any entity which comes from source. (if they have predefined
4372 -- type, that type does not appear to come from source, but the
4373 -- entity should not be frozen here).
4375 when N_Loop_Statement =>
4376 exit when not Comes_From_Source (Etype (N))
4377 and then (No (Nam) or else not Comes_From_Source (Nam));
4379 -- For all other cases, keep looking at parents
4385 -- We fall through the case if we did not yet find the proper
4386 -- place in the free for inserting the freeze node, so climb!
4391 -- If the expression appears in a record or an initialization procedure,
4392 -- the freeze nodes are collected and attached to the current scope, to
4393 -- be inserted and analyzed on exit from the scope, to insure that
4394 -- generated entities appear in the correct scope. If the expression is
4395 -- a default for a discriminant specification, the scope is still void.
4396 -- The expression can also appear in the discriminant part of a private
4397 -- or concurrent type.
4399 -- If the expression appears in a constrained subcomponent of an
4400 -- enclosing record declaration, the freeze nodes must be attached to
4401 -- the outer record type so they can eventually be placed in the
4402 -- enclosing declaration list.
4404 -- The other case requiring this special handling is if we are in a
4405 -- default expression, since in that case we are about to freeze a
4406 -- static type, and the freeze scope needs to be the outer scope, not
4407 -- the scope of the subprogram with the default parameter.
4409 -- For default expressions and other spec expressions in generic units,
4410 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
4411 -- placing them at the proper place, after the generic unit.
4413 if (In_Spec_Exp and not Inside_A_Generic)
4414 or else Freeze_Outside
4415 or else (Is_Type (Current_Scope)
4416 and then (not Is_Concurrent_Type (Current_Scope)
4417 or else not Has_Completion (Current_Scope)))
4418 or else Ekind (Current_Scope) = E_Void
4421 Loc : constant Source_Ptr := Sloc (Current_Scope);
4422 Freeze_Nodes : List_Id := No_List;
4423 Pos : Int := Scope_Stack.Last;
4426 if Present (Desig_Typ) then
4427 Freeze_And_Append (Desig_Typ, Loc, Freeze_Nodes);
4430 if Present (Typ) then
4431 Freeze_And_Append (Typ, Loc, Freeze_Nodes);
4434 if Present (Nam) then
4435 Freeze_And_Append (Nam, Loc, Freeze_Nodes);
4438 -- The current scope may be that of a constrained component of
4439 -- an enclosing record declaration, which is above the current
4440 -- scope in the scope stack.
4442 if Is_Record_Type (Scope (Current_Scope)) then
4446 if Is_Non_Empty_List (Freeze_Nodes) then
4447 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
4448 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
4451 Append_List (Freeze_Nodes, Scope_Stack.Table
4452 (Pos).Pending_Freeze_Actions);
4460 -- Now we have the right place to do the freezing. First, a special
4461 -- adjustment, if we are in spec-expression analysis mode, these freeze
4462 -- actions must not be thrown away (normally all inserted actions are
4463 -- thrown away in this mode. However, the freeze actions are from static
4464 -- expressions and one of the important reasons we are doing this
4465 -- special analysis is to get these freeze actions. Therefore we turn
4466 -- off the In_Spec_Expression mode to propagate these freeze actions.
4467 -- This also means they get properly analyzed and expanded.
4469 In_Spec_Expression := False;
4471 -- Freeze the designated type of an allocator (RM 13.14(13))
4473 if Present (Desig_Typ) then
4474 Freeze_Before (P, Desig_Typ);
4477 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
4478 -- the enumeration representation clause exception in the loop above.
4480 if Present (Typ) then
4481 Freeze_Before (P, Typ);
4484 -- Freeze name if one is present (RM 13.14(11))
4486 if Present (Nam) then
4487 Freeze_Before (P, Nam);
4490 -- Restore In_Spec_Expression flag
4492 In_Spec_Expression := In_Spec_Exp;
4493 end Freeze_Expression;
4495 -----------------------------
4496 -- Freeze_Fixed_Point_Type --
4497 -----------------------------
4499 -- Certain fixed-point types and subtypes, including implicit base types
4500 -- and declared first subtypes, have not yet set up a range. This is
4501 -- because the range cannot be set until the Small and Size values are
4502 -- known, and these are not known till the type is frozen.
4504 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
4505 -- whose bounds are unanalyzed real literals. This routine will recognize
4506 -- this case, and transform this range node into a properly typed range
4507 -- with properly analyzed and resolved values.
4509 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
4510 Rng : constant Node_Id := Scalar_Range (Typ);
4511 Lo : constant Node_Id := Low_Bound (Rng);
4512 Hi : constant Node_Id := High_Bound (Rng);
4513 Btyp : constant Entity_Id := Base_Type (Typ);
4514 Brng : constant Node_Id := Scalar_Range (Btyp);
4515 BLo : constant Node_Id := Low_Bound (Brng);
4516 BHi : constant Node_Id := High_Bound (Brng);
4517 Small : constant Ureal := Small_Value (Typ);
4524 function Fsize (Lov, Hiv : Ureal) return Nat;
4525 -- Returns size of type with given bounds. Also leaves these
4526 -- bounds set as the current bounds of the Typ.
4532 function Fsize (Lov, Hiv : Ureal) return Nat is
4534 Set_Realval (Lo, Lov);
4535 Set_Realval (Hi, Hiv);
4536 return Minimum_Size (Typ);
4539 -- Start of processing for Freeze_Fixed_Point_Type
4542 -- If Esize of a subtype has not previously been set, set it now
4544 if Unknown_Esize (Typ) then
4545 Atype := Ancestor_Subtype (Typ);
4547 if Present (Atype) then
4548 Set_Esize (Typ, Esize (Atype));
4550 Set_Esize (Typ, Esize (Base_Type (Typ)));
4554 -- Immediate return if the range is already analyzed. This means that
4555 -- the range is already set, and does not need to be computed by this
4558 if Analyzed (Rng) then
4562 -- Immediate return if either of the bounds raises Constraint_Error
4564 if Raises_Constraint_Error (Lo)
4565 or else Raises_Constraint_Error (Hi)
4570 Loval := Realval (Lo);
4571 Hival := Realval (Hi);
4573 -- Ordinary fixed-point case
4575 if Is_Ordinary_Fixed_Point_Type (Typ) then
4577 -- For the ordinary fixed-point case, we are allowed to fudge the
4578 -- end-points up or down by small. Generally we prefer to fudge up,
4579 -- i.e. widen the bounds for non-model numbers so that the end points
4580 -- are included. However there are cases in which this cannot be
4581 -- done, and indeed cases in which we may need to narrow the bounds.
4582 -- The following circuit makes the decision.
4584 -- Note: our terminology here is that Incl_EP means that the bounds
4585 -- are widened by Small if necessary to include the end points, and
4586 -- Excl_EP means that the bounds are narrowed by Small to exclude the
4587 -- end-points if this reduces the size.
4589 -- Note that in the Incl case, all we care about is including the
4590 -- end-points. In the Excl case, we want to narrow the bounds as
4591 -- much as permitted by the RM, to give the smallest possible size.
4594 Loval_Incl_EP : Ureal;
4595 Hival_Incl_EP : Ureal;
4597 Loval_Excl_EP : Ureal;
4598 Hival_Excl_EP : Ureal;
4604 First_Subt : Entity_Id;
4609 -- First step. Base types are required to be symmetrical. Right
4610 -- now, the base type range is a copy of the first subtype range.
4611 -- This will be corrected before we are done, but right away we
4612 -- need to deal with the case where both bounds are non-negative.
4613 -- In this case, we set the low bound to the negative of the high
4614 -- bound, to make sure that the size is computed to include the
4615 -- required sign. Note that we do not need to worry about the
4616 -- case of both bounds negative, because the sign will be dealt
4617 -- with anyway. Furthermore we can't just go making such a bound
4618 -- symmetrical, since in a twos-complement system, there is an
4619 -- extra negative value which could not be accommodated on the
4623 and then not UR_Is_Negative (Loval)
4624 and then Hival > Loval
4627 Set_Realval (Lo, Loval);
4630 -- Compute the fudged bounds. If the number is a model number,
4631 -- then we do nothing to include it, but we are allowed to backoff
4632 -- to the next adjacent model number when we exclude it. If it is
4633 -- not a model number then we straddle the two values with the
4634 -- model numbers on either side.
4636 Model_Num := UR_Trunc (Loval / Small) * Small;
4638 if Loval = Model_Num then
4639 Loval_Incl_EP := Model_Num;
4641 Loval_Incl_EP := Model_Num - Small;
4644 -- The low value excluding the end point is Small greater, but
4645 -- we do not do this exclusion if the low value is positive,
4646 -- since it can't help the size and could actually hurt by
4647 -- crossing the high bound.
4649 if UR_Is_Negative (Loval_Incl_EP) then
4650 Loval_Excl_EP := Loval_Incl_EP + Small;
4652 -- If the value went from negative to zero, then we have the
4653 -- case where Loval_Incl_EP is the model number just below
4654 -- zero, so we want to stick to the negative value for the
4655 -- base type to maintain the condition that the size will
4656 -- include signed values.
4659 and then UR_Is_Zero (Loval_Excl_EP)
4661 Loval_Excl_EP := Loval_Incl_EP;
4665 Loval_Excl_EP := Loval_Incl_EP;
4668 -- Similar processing for upper bound and high value
4670 Model_Num := UR_Trunc (Hival / Small) * Small;
4672 if Hival = Model_Num then
4673 Hival_Incl_EP := Model_Num;
4675 Hival_Incl_EP := Model_Num + Small;
4678 if UR_Is_Positive (Hival_Incl_EP) then
4679 Hival_Excl_EP := Hival_Incl_EP - Small;
4681 Hival_Excl_EP := Hival_Incl_EP;
4684 -- One further adjustment is needed. In the case of subtypes, we
4685 -- cannot go outside the range of the base type, or we get
4686 -- peculiarities, and the base type range is already set. This
4687 -- only applies to the Incl values, since clearly the Excl values
4688 -- are already as restricted as they are allowed to be.
4691 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
4692 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
4695 -- Get size including and excluding end points
4697 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
4698 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
4700 -- No need to exclude end-points if it does not reduce size
4702 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
4703 Loval_Excl_EP := Loval_Incl_EP;
4706 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
4707 Hival_Excl_EP := Hival_Incl_EP;
4710 -- Now we set the actual size to be used. We want to use the
4711 -- bounds fudged up to include the end-points but only if this
4712 -- can be done without violating a specifically given size
4713 -- size clause or causing an unacceptable increase in size.
4715 -- Case of size clause given
4717 if Has_Size_Clause (Typ) then
4719 -- Use the inclusive size only if it is consistent with
4720 -- the explicitly specified size.
4722 if Size_Incl_EP <= RM_Size (Typ) then
4723 Actual_Lo := Loval_Incl_EP;
4724 Actual_Hi := Hival_Incl_EP;
4725 Actual_Size := Size_Incl_EP;
4727 -- If the inclusive size is too large, we try excluding
4728 -- the end-points (will be caught later if does not work).
4731 Actual_Lo := Loval_Excl_EP;
4732 Actual_Hi := Hival_Excl_EP;
4733 Actual_Size := Size_Excl_EP;
4736 -- Case of size clause not given
4739 -- If we have a base type whose corresponding first subtype
4740 -- has an explicit size that is large enough to include our
4741 -- end-points, then do so. There is no point in working hard
4742 -- to get a base type whose size is smaller than the specified
4743 -- size of the first subtype.
4745 First_Subt := First_Subtype (Typ);
4747 if Has_Size_Clause (First_Subt)
4748 and then Size_Incl_EP <= Esize (First_Subt)
4750 Actual_Size := Size_Incl_EP;
4751 Actual_Lo := Loval_Incl_EP;
4752 Actual_Hi := Hival_Incl_EP;
4754 -- If excluding the end-points makes the size smaller and
4755 -- results in a size of 8,16,32,64, then we take the smaller
4756 -- size. For the 64 case, this is compulsory. For the other
4757 -- cases, it seems reasonable. We like to include end points
4758 -- if we can, but not at the expense of moving to the next
4759 -- natural boundary of size.
4761 elsif Size_Incl_EP /= Size_Excl_EP
4763 (Size_Excl_EP = 8 or else
4764 Size_Excl_EP = 16 or else
4765 Size_Excl_EP = 32 or else
4768 Actual_Size := Size_Excl_EP;
4769 Actual_Lo := Loval_Excl_EP;
4770 Actual_Hi := Hival_Excl_EP;
4772 -- Otherwise we can definitely include the end points
4775 Actual_Size := Size_Incl_EP;
4776 Actual_Lo := Loval_Incl_EP;
4777 Actual_Hi := Hival_Incl_EP;
4780 -- One pathological case: normally we never fudge a low bound
4781 -- down, since it would seem to increase the size (if it has
4782 -- any effect), but for ranges containing single value, or no
4783 -- values, the high bound can be small too large. Consider:
4785 -- type t is delta 2.0**(-14)
4786 -- range 131072.0 .. 0;
4788 -- That lower bound is *just* outside the range of 32 bits, and
4789 -- does need fudging down in this case. Note that the bounds
4790 -- will always have crossed here, since the high bound will be
4791 -- fudged down if necessary, as in the case of:
4793 -- type t is delta 2.0**(-14)
4794 -- range 131072.0 .. 131072.0;
4796 -- So we detect the situation by looking for crossed bounds,
4797 -- and if the bounds are crossed, and the low bound is greater
4798 -- than zero, we will always back it off by small, since this
4799 -- is completely harmless.
4801 if Actual_Lo > Actual_Hi then
4802 if UR_Is_Positive (Actual_Lo) then
4803 Actual_Lo := Loval_Incl_EP - Small;
4804 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
4806 -- And of course, we need to do exactly the same parallel
4807 -- fudge for flat ranges in the negative region.
4809 elsif UR_Is_Negative (Actual_Hi) then
4810 Actual_Hi := Hival_Incl_EP + Small;
4811 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
4816 Set_Realval (Lo, Actual_Lo);
4817 Set_Realval (Hi, Actual_Hi);
4820 -- For the decimal case, none of this fudging is required, since there
4821 -- are no end-point problems in the decimal case (the end-points are
4822 -- always included).
4825 Actual_Size := Fsize (Loval, Hival);
4828 -- At this stage, the actual size has been calculated and the proper
4829 -- required bounds are stored in the low and high bounds.
4831 if Actual_Size > 64 then
4832 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
4834 ("size required (^) for type& too large, maximum allowed is 64",
4839 -- Check size against explicit given size
4841 if Has_Size_Clause (Typ) then
4842 if Actual_Size > RM_Size (Typ) then
4843 Error_Msg_Uint_1 := RM_Size (Typ);
4844 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
4846 ("size given (^) for type& too small, minimum allowed is ^",
4847 Size_Clause (Typ), Typ);
4850 Actual_Size := UI_To_Int (Esize (Typ));
4853 -- Increase size to next natural boundary if no size clause given
4856 if Actual_Size <= 8 then
4858 elsif Actual_Size <= 16 then
4860 elsif Actual_Size <= 32 then
4866 Init_Esize (Typ, Actual_Size);
4867 Adjust_Esize_For_Alignment (Typ);
4870 -- If we have a base type, then expand the bounds so that they extend to
4871 -- the full width of the allocated size in bits, to avoid junk range
4872 -- checks on intermediate computations.
4874 if Base_Type (Typ) = Typ then
4875 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
4876 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
4879 -- Final step is to reanalyze the bounds using the proper type
4880 -- and set the Corresponding_Integer_Value fields of the literals.
4882 Set_Etype (Lo, Empty);
4883 Set_Analyzed (Lo, False);
4886 -- Resolve with universal fixed if the base type, and the base type if
4887 -- it is a subtype. Note we can't resolve the base type with itself,
4888 -- that would be a reference before definition.
4891 Resolve (Lo, Universal_Fixed);
4896 -- Set corresponding integer value for bound
4898 Set_Corresponding_Integer_Value
4899 (Lo, UR_To_Uint (Realval (Lo) / Small));
4901 -- Similar processing for high bound
4903 Set_Etype (Hi, Empty);
4904 Set_Analyzed (Hi, False);
4908 Resolve (Hi, Universal_Fixed);
4913 Set_Corresponding_Integer_Value
4914 (Hi, UR_To_Uint (Realval (Hi) / Small));
4916 -- Set type of range to correspond to bounds
4918 Set_Etype (Rng, Etype (Lo));
4920 -- Set Esize to calculated size if not set already
4922 if Unknown_Esize (Typ) then
4923 Init_Esize (Typ, Actual_Size);
4926 -- Set RM_Size if not already set. If already set, check value
4929 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
4932 if RM_Size (Typ) /= Uint_0 then
4933 if RM_Size (Typ) < Minsiz then
4934 Error_Msg_Uint_1 := RM_Size (Typ);
4935 Error_Msg_Uint_2 := Minsiz;
4937 ("size given (^) for type& too small, minimum allowed is ^",
4938 Size_Clause (Typ), Typ);
4942 Set_RM_Size (Typ, Minsiz);
4945 end Freeze_Fixed_Point_Type;
4951 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
4955 Set_Has_Delayed_Freeze (T);
4956 L := Freeze_Entity (T, Sloc (N));
4958 if Is_Non_Empty_List (L) then
4959 Insert_Actions (N, L);
4963 --------------------------
4964 -- Freeze_Static_Object --
4965 --------------------------
4967 procedure Freeze_Static_Object (E : Entity_Id) is
4969 Cannot_Be_Static : exception;
4970 -- Exception raised if the type of a static object cannot be made
4971 -- static. This happens if the type depends on non-global objects.
4973 procedure Ensure_Expression_Is_SA (N : Node_Id);
4974 -- Called to ensure that an expression used as part of a type definition
4975 -- is statically allocatable, which means that the expression type is
4976 -- statically allocatable, and the expression is either static, or a
4977 -- reference to a library level constant.
4979 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
4980 -- Called to mark a type as static, checking that it is possible
4981 -- to set the type as static. If it is not possible, then the
4982 -- exception Cannot_Be_Static is raised.
4984 -----------------------------
4985 -- Ensure_Expression_Is_SA --
4986 -----------------------------
4988 procedure Ensure_Expression_Is_SA (N : Node_Id) is
4992 Ensure_Type_Is_SA (Etype (N));
4994 if Is_Static_Expression (N) then
4997 elsif Nkind (N) = N_Identifier then
5001 and then Ekind (Ent) = E_Constant
5002 and then Is_Library_Level_Entity (Ent)
5008 raise Cannot_Be_Static;
5009 end Ensure_Expression_Is_SA;
5011 -----------------------
5012 -- Ensure_Type_Is_SA --
5013 -----------------------
5015 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
5020 -- If type is library level, we are all set
5022 if Is_Library_Level_Entity (Typ) then
5026 -- We are also OK if the type already marked as statically allocated,
5027 -- which means we processed it before.
5029 if Is_Statically_Allocated (Typ) then
5033 -- Mark type as statically allocated
5035 Set_Is_Statically_Allocated (Typ);
5037 -- Check that it is safe to statically allocate this type
5039 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
5040 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
5041 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
5043 elsif Is_Array_Type (Typ) then
5044 N := First_Index (Typ);
5045 while Present (N) loop
5046 Ensure_Type_Is_SA (Etype (N));
5050 Ensure_Type_Is_SA (Component_Type (Typ));
5052 elsif Is_Access_Type (Typ) then
5053 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
5057 T : constant Entity_Id := Etype (Designated_Type (Typ));
5060 if T /= Standard_Void_Type then
5061 Ensure_Type_Is_SA (T);
5064 F := First_Formal (Designated_Type (Typ));
5066 while Present (F) loop
5067 Ensure_Type_Is_SA (Etype (F));
5073 Ensure_Type_Is_SA (Designated_Type (Typ));
5076 elsif Is_Record_Type (Typ) then
5077 C := First_Entity (Typ);
5078 while Present (C) loop
5079 if Ekind (C) = E_Discriminant
5080 or else Ekind (C) = E_Component
5082 Ensure_Type_Is_SA (Etype (C));
5084 elsif Is_Type (C) then
5085 Ensure_Type_Is_SA (C);
5091 elsif Ekind (Typ) = E_Subprogram_Type then
5092 Ensure_Type_Is_SA (Etype (Typ));
5094 C := First_Formal (Typ);
5095 while Present (C) loop
5096 Ensure_Type_Is_SA (Etype (C));
5101 raise Cannot_Be_Static;
5103 end Ensure_Type_Is_SA;
5105 -- Start of processing for Freeze_Static_Object
5108 Ensure_Type_Is_SA (Etype (E));
5111 when Cannot_Be_Static =>
5113 -- If the object that cannot be static is imported or exported, then
5114 -- issue an error message saying that this object cannot be imported
5115 -- or exported. If it has an address clause it is an overlay in the
5116 -- current partition and the static requirement is not relevant.
5118 if Is_Imported (E) and then No (Address_Clause (E)) then
5120 ("& cannot be imported (local type is not constant)", E);
5122 -- Otherwise must be exported, something is wrong if compiler
5123 -- is marking something as statically allocated which cannot be).
5125 else pragma Assert (Is_Exported (E));
5127 ("& cannot be exported (local type is not constant)", E);
5129 end Freeze_Static_Object;
5131 -----------------------
5132 -- Freeze_Subprogram --
5133 -----------------------
5135 procedure Freeze_Subprogram (E : Entity_Id) is
5140 -- Subprogram may not have an address clause unless it is imported
5142 if Present (Address_Clause (E)) then
5143 if not Is_Imported (E) then
5145 ("address clause can only be given " &
5146 "for imported subprogram",
5147 Name (Address_Clause (E)));
5151 -- Reset the Pure indication on an imported subprogram unless an
5152 -- explicit Pure_Function pragma was present. We do this because
5153 -- otherwise it is an insidious error to call a non-pure function from
5154 -- pure unit and have calls mysteriously optimized away. What happens
5155 -- here is that the Import can bypass the normal check to ensure that
5156 -- pure units call only pure subprograms.
5159 and then Is_Pure (E)
5160 and then not Has_Pragma_Pure_Function (E)
5162 Set_Is_Pure (E, False);
5165 -- For non-foreign convention subprograms, this is where we create
5166 -- the extra formals (for accessibility level and constrained bit
5167 -- information). We delay this till the freeze point precisely so
5168 -- that we know the convention!
5170 if not Has_Foreign_Convention (E) then
5171 Create_Extra_Formals (E);
5174 -- If this is convention Ada and a Valued_Procedure, that's odd
5176 if Ekind (E) = E_Procedure
5177 and then Is_Valued_Procedure (E)
5178 and then Convention (E) = Convention_Ada
5179 and then Warn_On_Export_Import
5182 ("?Valued_Procedure has no effect for convention Ada", E);
5183 Set_Is_Valued_Procedure (E, False);
5186 -- Case of foreign convention
5191 -- For foreign conventions, warn about return of an
5192 -- unconstrained array.
5194 -- Note: we *do* allow a return by descriptor for the VMS case,
5195 -- though here there is probably more to be done ???
5197 if Ekind (E) = E_Function then
5198 Retype := Underlying_Type (Etype (E));
5200 -- If no return type, probably some other error, e.g. a
5201 -- missing full declaration, so ignore.
5206 -- If the return type is generic, we have emitted a warning
5207 -- earlier on, and there is nothing else to check here. Specific
5208 -- instantiations may lead to erroneous behavior.
5210 elsif Is_Generic_Type (Etype (E)) then
5213 -- Display warning if returning unconstrained array
5215 elsif Is_Array_Type (Retype)
5216 and then not Is_Constrained (Retype)
5218 -- Exclude cases where descriptor mechanism is set, since the
5219 -- VMS descriptor mechanisms allow such unconstrained returns.
5221 and then Mechanism (E) not in Descriptor_Codes
5223 -- Check appropriate warning is enabled (should we check for
5224 -- Warnings (Off) on specific entities here, probably so???)
5226 and then Warn_On_Export_Import
5228 -- Exclude the VM case, since return of unconstrained arrays
5229 -- is properly handled in both the JVM and .NET cases.
5231 and then VM_Target = No_VM
5234 ("?foreign convention function& should not return " &
5235 "unconstrained array", E);
5240 -- If any of the formals for an exported foreign convention
5241 -- subprogram have defaults, then emit an appropriate warning since
5242 -- this is odd (default cannot be used from non-Ada code)
5244 if Is_Exported (E) then
5245 F := First_Formal (E);
5246 while Present (F) loop
5247 if Warn_On_Export_Import
5248 and then Present (Default_Value (F))
5251 ("?parameter cannot be defaulted in non-Ada call",
5260 -- For VMS, descriptor mechanisms for parameters are allowed only for
5261 -- imported/exported subprograms. Moreover, the NCA descriptor is not
5262 -- allowed for parameters of exported subprograms.
5264 if OpenVMS_On_Target then
5265 if Is_Exported (E) then
5266 F := First_Formal (E);
5267 while Present (F) loop
5268 if Mechanism (F) = By_Descriptor_NCA then
5270 ("'N'C'A' descriptor for parameter not permitted", F);
5272 ("\can only be used for imported subprogram", F);
5278 elsif not Is_Imported (E) then
5279 F := First_Formal (E);
5280 while Present (F) loop
5281 if Mechanism (F) in Descriptor_Codes then
5283 ("descriptor mechanism for parameter not permitted", F);
5285 ("\can only be used for imported/exported subprogram", F);
5293 -- Pragma Inline_Always is disallowed for dispatching subprograms
5294 -- because the address of such subprograms is saved in the dispatch
5295 -- table to support dispatching calls, and dispatching calls cannot
5296 -- be inlined. This is consistent with the restriction against using
5297 -- 'Access or 'Address on an Inline_Always subprogram.
5299 if Is_Dispatching_Operation (E)
5300 and then Has_Pragma_Inline_Always (E)
5303 ("pragma Inline_Always not allowed for dispatching subprograms", E);
5306 -- Because of the implicit representation of inherited predefined
5307 -- operators in the front-end, the overriding status of the operation
5308 -- may be affected when a full view of a type is analyzed, and this is
5309 -- not captured by the analysis of the corresponding type declaration.
5310 -- Therefore the correctness of a not-overriding indicator must be
5311 -- rechecked when the subprogram is frozen.
5313 if Nkind (E) = N_Defining_Operator_Symbol
5314 and then not Error_Posted (Parent (E))
5316 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
5318 end Freeze_Subprogram;
5320 ----------------------
5321 -- Is_Fully_Defined --
5322 ----------------------
5324 function Is_Fully_Defined (T : Entity_Id) return Boolean is
5326 if Ekind (T) = E_Class_Wide_Type then
5327 return Is_Fully_Defined (Etype (T));
5329 elsif Is_Array_Type (T) then
5330 return Is_Fully_Defined (Component_Type (T));
5332 elsif Is_Record_Type (T)
5333 and not Is_Private_Type (T)
5335 -- Verify that the record type has no components with private types
5336 -- without completion.
5342 Comp := First_Component (T);
5344 while Present (Comp) loop
5345 if not Is_Fully_Defined (Etype (Comp)) then
5349 Next_Component (Comp);
5354 -- For the designated type of an access to subprogram, all types in
5355 -- the profile must be fully defined.
5357 elsif Ekind (T) = E_Subprogram_Type then
5362 F := First_Formal (T);
5363 while Present (F) loop
5364 if not Is_Fully_Defined (Etype (F)) then
5371 return Is_Fully_Defined (Etype (T));
5375 return not Is_Private_Type (T)
5376 or else Present (Full_View (Base_Type (T)));
5378 end Is_Fully_Defined;
5380 ---------------------------------
5381 -- Process_Default_Expressions --
5382 ---------------------------------
5384 procedure Process_Default_Expressions
5386 After : in out Node_Id)
5388 Loc : constant Source_Ptr := Sloc (E);
5395 Set_Default_Expressions_Processed (E);
5397 -- A subprogram instance and its associated anonymous subprogram share
5398 -- their signature. The default expression functions are defined in the
5399 -- wrapper packages for the anonymous subprogram, and should not be
5400 -- generated again for the instance.
5402 if Is_Generic_Instance (E)
5403 and then Present (Alias (E))
5404 and then Default_Expressions_Processed (Alias (E))
5409 Formal := First_Formal (E);
5410 while Present (Formal) loop
5411 if Present (Default_Value (Formal)) then
5413 -- We work with a copy of the default expression because we
5414 -- do not want to disturb the original, since this would mess
5415 -- up the conformance checking.
5417 Dcopy := New_Copy_Tree (Default_Value (Formal));
5419 -- The analysis of the expression may generate insert actions,
5420 -- which of course must not be executed. We wrap those actions
5421 -- in a procedure that is not called, and later on eliminated.
5422 -- The following cases have no side-effects, and are analyzed
5425 if Nkind (Dcopy) = N_Identifier
5426 or else Nkind (Dcopy) = N_Expanded_Name
5427 or else Nkind (Dcopy) = N_Integer_Literal
5428 or else (Nkind (Dcopy) = N_Real_Literal
5429 and then not Vax_Float (Etype (Dcopy)))
5430 or else Nkind (Dcopy) = N_Character_Literal
5431 or else Nkind (Dcopy) = N_String_Literal
5432 or else Known_Null (Dcopy)
5433 or else (Nkind (Dcopy) = N_Attribute_Reference
5435 Attribute_Name (Dcopy) = Name_Null_Parameter)
5438 -- If there is no default function, we must still do a full
5439 -- analyze call on the default value, to ensure that all error
5440 -- checks are performed, e.g. those associated with static
5441 -- evaluation. Note: this branch will always be taken if the
5442 -- analyzer is turned off (but we still need the error checks).
5444 -- Note: the setting of parent here is to meet the requirement
5445 -- that we can only analyze the expression while attached to
5446 -- the tree. Really the requirement is that the parent chain
5447 -- be set, we don't actually need to be in the tree.
5449 Set_Parent (Dcopy, Declaration_Node (Formal));
5452 -- Default expressions are resolved with their own type if the
5453 -- context is generic, to avoid anomalies with private types.
5455 if Ekind (Scope (E)) = E_Generic_Package then
5458 Resolve (Dcopy, Etype (Formal));
5461 -- If that resolved expression will raise constraint error,
5462 -- then flag the default value as raising constraint error.
5463 -- This allows a proper error message on the calls.
5465 if Raises_Constraint_Error (Dcopy) then
5466 Set_Raises_Constraint_Error (Default_Value (Formal));
5469 -- If the default is a parameterless call, we use the name of
5470 -- the called function directly, and there is no body to build.
5472 elsif Nkind (Dcopy) = N_Function_Call
5473 and then No (Parameter_Associations (Dcopy))
5477 -- Else construct and analyze the body of a wrapper procedure
5478 -- that contains an object declaration to hold the expression.
5479 -- Given that this is done only to complete the analysis, it
5480 -- simpler to build a procedure than a function which might
5481 -- involve secondary stack expansion.
5484 Dnam := Make_Temporary (Loc, 'D');
5487 Make_Subprogram_Body (Loc,
5489 Make_Procedure_Specification (Loc,
5490 Defining_Unit_Name => Dnam),
5492 Declarations => New_List (
5493 Make_Object_Declaration (Loc,
5494 Defining_Identifier =>
5495 Make_Defining_Identifier (Loc,
5496 New_Internal_Name ('T')),
5497 Object_Definition =>
5498 New_Occurrence_Of (Etype (Formal), Loc),
5499 Expression => New_Copy_Tree (Dcopy))),
5501 Handled_Statement_Sequence =>
5502 Make_Handled_Sequence_Of_Statements (Loc,
5503 Statements => New_List));
5505 Set_Scope (Dnam, Scope (E));
5506 Set_Assignment_OK (First (Declarations (Dbody)));
5507 Set_Is_Eliminated (Dnam);
5508 Insert_After (After, Dbody);
5514 Next_Formal (Formal);
5516 end Process_Default_Expressions;
5518 ----------------------------------------
5519 -- Set_Component_Alignment_If_Not_Set --
5520 ----------------------------------------
5522 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
5524 -- Ignore if not base type, subtypes don't need anything
5526 if Typ /= Base_Type (Typ) then
5530 -- Do not override existing representation
5532 if Is_Packed (Typ) then
5535 elsif Has_Specified_Layout (Typ) then
5538 elsif Component_Alignment (Typ) /= Calign_Default then
5542 Set_Component_Alignment
5543 (Typ, Scope_Stack.Table
5544 (Scope_Stack.Last).Component_Alignment_Default);
5546 end Set_Component_Alignment_If_Not_Set;
5552 procedure Undelay_Type (T : Entity_Id) is
5554 Set_Has_Delayed_Freeze (T, False);
5555 Set_Freeze_Node (T, Empty);
5557 -- Since we don't want T to have a Freeze_Node, we don't want its
5558 -- Full_View or Corresponding_Record_Type to have one either.
5560 -- ??? Fundamentally, this whole handling is a kludge. What we really
5561 -- want is to be sure that for an Itype that's part of record R and is a
5562 -- subtype of type T, that it's frozen after the later of the freeze
5563 -- points of R and T. We have no way of doing that directly, so what we
5564 -- do is force most such Itypes to be frozen as part of freezing R via
5565 -- this procedure and only delay the ones that need to be delayed
5566 -- (mostly the designated types of access types that are defined as part
5569 if Is_Private_Type (T)
5570 and then Present (Full_View (T))
5571 and then Is_Itype (Full_View (T))
5572 and then Is_Record_Type (Scope (Full_View (T)))
5574 Undelay_Type (Full_View (T));
5577 if Is_Concurrent_Type (T)
5578 and then Present (Corresponding_Record_Type (T))
5579 and then Is_Itype (Corresponding_Record_Type (T))
5580 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
5582 Undelay_Type (Corresponding_Record_Type (T));
5590 procedure Warn_Overlay
5595 Ent : constant Entity_Id := Entity (Nam);
5596 -- The object to which the address clause applies
5599 Old : Entity_Id := Empty;
5603 -- No warning if address clause overlay warnings are off
5605 if not Address_Clause_Overlay_Warnings then
5609 -- No warning if there is an explicit initialization
5611 Init := Original_Node (Expression (Declaration_Node (Ent)));
5613 if Present (Init) and then Comes_From_Source (Init) then
5617 -- We only give the warning for non-imported entities of a type for
5618 -- which a non-null base init proc is defined, or for objects of access
5619 -- types with implicit null initialization, or when Normalize_Scalars
5620 -- applies and the type is scalar or a string type (the latter being
5621 -- tested for because predefined String types are initialized by inline
5622 -- code rather than by an init_proc). Note that we do not give the
5623 -- warning for Initialize_Scalars, since we suppressed initialization
5627 and then not Is_Imported (Ent)
5628 and then (Has_Non_Null_Base_Init_Proc (Typ)
5629 or else Is_Access_Type (Typ)
5630 or else (Normalize_Scalars
5631 and then (Is_Scalar_Type (Typ)
5632 or else Is_String_Type (Typ))))
5634 if Nkind (Expr) = N_Attribute_Reference
5635 and then Is_Entity_Name (Prefix (Expr))
5637 Old := Entity (Prefix (Expr));
5639 elsif Is_Entity_Name (Expr)
5640 and then Ekind (Entity (Expr)) = E_Constant
5642 Decl := Declaration_Node (Entity (Expr));
5644 if Nkind (Decl) = N_Object_Declaration
5645 and then Present (Expression (Decl))
5646 and then Nkind (Expression (Decl)) = N_Attribute_Reference
5647 and then Is_Entity_Name (Prefix (Expression (Decl)))
5649 Old := Entity (Prefix (Expression (Decl)));
5651 elsif Nkind (Expr) = N_Function_Call then
5655 -- A function call (most likely to To_Address) is probably not an
5656 -- overlay, so skip warning. Ditto if the function call was inlined
5657 -- and transformed into an entity.
5659 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
5663 Decl := Next (Parent (Expr));
5665 -- If a pragma Import follows, we assume that it is for the current
5666 -- target of the address clause, and skip the warning.
5669 and then Nkind (Decl) = N_Pragma
5670 and then Pragma_Name (Decl) = Name_Import
5675 if Present (Old) then
5676 Error_Msg_Node_2 := Old;
5678 ("default initialization of & may modify &?",
5682 ("default initialization of & may modify overlaid storage?",
5686 -- Add friendly warning if initialization comes from a packed array
5689 if Is_Record_Type (Typ) then
5694 Comp := First_Component (Typ);
5696 while Present (Comp) loop
5697 if Nkind (Parent (Comp)) = N_Component_Declaration
5698 and then Present (Expression (Parent (Comp)))
5701 elsif Is_Array_Type (Etype (Comp))
5702 and then Present (Packed_Array_Type (Etype (Comp)))
5705 ("\packed array component& " &
5706 "will be initialized to zero?",
5710 Next_Component (Comp);
5717 ("\use pragma Import for & to " &
5718 "suppress initialization (RM B.1(24))?",