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