Mark ChangeLog
[official-gcc.git] / gcc / ada / freeze.adb
blobb0e1565758b7d0449dade9f62f744ff0e2b94489
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-2005, 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 Undelay_Type (T : Entity_Id);
168 -- T is a type of a component that we know to be an Itype.
169 -- We don't want this to have a Freeze_Node, so ensure it doesn't.
170 -- Do the same for any Full_View or Corresponding_Record_Type.
172 procedure Warn_Overlay
173 (Expr : Node_Id;
174 Typ : Entity_Id;
175 Nam : Node_Id);
176 -- Expr is the expression for an address clause for entity Nam whose type
177 -- is Typ. If Typ has a default initialization, and there is no explicit
178 -- initialization in the source declaration, check whether the address
179 -- clause might cause overlaying of an entity, and emit a warning on the
180 -- side effect that the initialization will cause.
182 -------------------------------
183 -- Adjust_Esize_For_Alignment --
184 -------------------------------
186 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
187 Align : Uint;
189 begin
190 if Known_Esize (Typ) and then Known_Alignment (Typ) then
191 Align := Alignment_In_Bits (Typ);
193 if Align > Esize (Typ)
194 and then Align <= Standard_Long_Long_Integer_Size
195 then
196 Set_Esize (Typ, Align);
197 end if;
198 end if;
199 end Adjust_Esize_For_Alignment;
201 ------------------------------------
202 -- Build_And_Analyze_Renamed_Body --
203 ------------------------------------
205 procedure Build_And_Analyze_Renamed_Body
206 (Decl : Node_Id;
207 New_S : Entity_Id;
208 After : in out Node_Id)
210 Body_Node : constant Node_Id := Build_Renamed_Body (Decl, New_S);
212 begin
213 Insert_After (After, Body_Node);
214 Mark_Rewrite_Insertion (Body_Node);
215 Analyze (Body_Node);
216 After := Body_Node;
217 end Build_And_Analyze_Renamed_Body;
219 ------------------------
220 -- Build_Renamed_Body --
221 ------------------------
223 function Build_Renamed_Body
224 (Decl : Node_Id;
225 New_S : Entity_Id) return Node_Id
227 Loc : constant Source_Ptr := Sloc (New_S);
228 -- We use for the source location of the renamed body, the location
229 -- of the spec entity. It might seem more natural to use the location
230 -- of the renaming declaration itself, but that would be wrong, since
231 -- then the body we create would look as though it was created far
232 -- too late, and this could cause problems with elaboration order
233 -- analysis, particularly in connection with instantiations.
235 N : constant Node_Id := Unit_Declaration_Node (New_S);
236 Nam : constant Node_Id := Name (N);
237 Old_S : Entity_Id;
238 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
239 Actuals : List_Id := No_List;
240 Call_Node : Node_Id;
241 Call_Name : Node_Id;
242 Body_Node : Node_Id;
243 Formal : Entity_Id;
244 O_Formal : Entity_Id;
245 Param_Spec : Node_Id;
247 begin
248 -- Determine the entity being renamed, which is the target of the
249 -- call statement. If the name is an explicit dereference, this is
250 -- a renaming of a subprogram type rather than a subprogram. The
251 -- name itself is fully analyzed.
253 if Nkind (Nam) = N_Selected_Component then
254 Old_S := Entity (Selector_Name (Nam));
256 elsif Nkind (Nam) = N_Explicit_Dereference then
257 Old_S := Etype (Nam);
259 elsif Nkind (Nam) = N_Indexed_Component then
260 if Is_Entity_Name (Prefix (Nam)) then
261 Old_S := Entity (Prefix (Nam));
262 else
263 Old_S := Entity (Selector_Name (Prefix (Nam)));
264 end if;
266 elsif Nkind (Nam) = N_Character_Literal then
267 Old_S := Etype (New_S);
269 else
270 Old_S := Entity (Nam);
271 end if;
273 if Is_Entity_Name (Nam) then
275 -- If the renamed entity is a predefined operator, retain full
276 -- name to ensure its visibility.
278 if Ekind (Old_S) = E_Operator
279 and then Nkind (Nam) = N_Expanded_Name
280 then
281 Call_Name := New_Copy (Name (N));
282 else
283 Call_Name := New_Reference_To (Old_S, Loc);
284 end if;
286 else
287 Call_Name := New_Copy (Name (N));
289 -- The original name may have been overloaded, but
290 -- is fully resolved now.
292 Set_Is_Overloaded (Call_Name, False);
293 end if;
295 -- For simple renamings, subsequent calls can be expanded directly
296 -- as called to the renamed entity. The body must be generated in
297 -- any case for calls they may appear elsewhere.
299 if (Ekind (Old_S) = E_Function
300 or else Ekind (Old_S) = E_Procedure)
301 and then Nkind (Decl) = N_Subprogram_Declaration
302 then
303 Set_Body_To_Inline (Decl, Old_S);
304 end if;
306 -- The body generated for this renaming is an internal artifact, and
307 -- does not constitute a freeze point for the called entity.
309 Set_Must_Not_Freeze (Call_Name);
311 Formal := First_Formal (Defining_Entity (Decl));
313 if Present (Formal) then
314 Actuals := New_List;
316 while Present (Formal) loop
317 Append (New_Reference_To (Formal, Loc), Actuals);
318 Next_Formal (Formal);
319 end loop;
320 end if;
322 -- If the renamed entity is an entry, inherit its profile. For
323 -- other renamings as bodies, both profiles must be subtype
324 -- conformant, so it is not necessary to replace the profile given
325 -- in the declaration. However, default values that are aggregates
326 -- are rewritten when partially analyzed, so we recover the original
327 -- aggregate to insure that subsequent conformity checking works.
328 -- Similarly, if the default expression was constant-folded, recover
329 -- the original expression.
331 Formal := First_Formal (Defining_Entity (Decl));
333 if Present (Formal) then
334 O_Formal := First_Formal (Old_S);
335 Param_Spec := First (Parameter_Specifications (Spec));
337 while Present (Formal) loop
338 if Is_Entry (Old_S) then
340 if Nkind (Parameter_Type (Param_Spec)) /=
341 N_Access_Definition
342 then
343 Set_Etype (Formal, Etype (O_Formal));
344 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
345 end if;
347 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
348 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
349 Nkind (Default_Value (O_Formal))
350 then
351 Set_Expression (Param_Spec,
352 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
353 end if;
355 Next_Formal (Formal);
356 Next_Formal (O_Formal);
357 Next (Param_Spec);
358 end loop;
359 end if;
361 -- If the renamed entity is a function, the generated body contains a
362 -- return statement. Otherwise, build a procedure call. If the entity is
363 -- an entry, subsequent analysis of the call will transform it into the
364 -- proper entry or protected operation call. If the renamed entity is
365 -- a character literal, return it directly.
367 if Ekind (Old_S) = E_Function
368 or else Ekind (Old_S) = E_Operator
369 or else (Ekind (Old_S) = E_Subprogram_Type
370 and then Etype (Old_S) /= Standard_Void_Type)
371 then
372 Call_Node :=
373 Make_Return_Statement (Loc,
374 Expression =>
375 Make_Function_Call (Loc,
376 Name => Call_Name,
377 Parameter_Associations => Actuals));
379 elsif Ekind (Old_S) = E_Enumeration_Literal then
380 Call_Node :=
381 Make_Return_Statement (Loc,
382 Expression => New_Occurrence_Of (Old_S, Loc));
384 elsif Nkind (Nam) = N_Character_Literal then
385 Call_Node :=
386 Make_Return_Statement (Loc,
387 Expression => Call_Name);
389 else
390 Call_Node :=
391 Make_Procedure_Call_Statement (Loc,
392 Name => Call_Name,
393 Parameter_Associations => Actuals);
394 end if;
396 -- Create entities for subprogram body and formals.
398 Set_Defining_Unit_Name (Spec,
399 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
401 Param_Spec := First (Parameter_Specifications (Spec));
403 while Present (Param_Spec) loop
404 Set_Defining_Identifier (Param_Spec,
405 Make_Defining_Identifier (Loc,
406 Chars => Chars (Defining_Identifier (Param_Spec))));
407 Next (Param_Spec);
408 end loop;
410 Body_Node :=
411 Make_Subprogram_Body (Loc,
412 Specification => Spec,
413 Declarations => New_List,
414 Handled_Statement_Sequence =>
415 Make_Handled_Sequence_Of_Statements (Loc,
416 Statements => New_List (Call_Node)));
418 if Nkind (Decl) /= N_Subprogram_Declaration then
419 Rewrite (N,
420 Make_Subprogram_Declaration (Loc,
421 Specification => Specification (N)));
422 end if;
424 -- Link the body to the entity whose declaration it completes. If
425 -- the body is analyzed when the renamed entity is frozen, it may be
426 -- necessary to restore the proper scope (see package Exp_Ch13).
428 if Nkind (N) = N_Subprogram_Renaming_Declaration
429 and then Present (Corresponding_Spec (N))
430 then
431 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
432 else
433 Set_Corresponding_Spec (Body_Node, New_S);
434 end if;
436 return Body_Node;
437 end Build_Renamed_Body;
439 --------------------------
440 -- Check_Address_Clause --
441 --------------------------
443 procedure Check_Address_Clause (E : Entity_Id) is
444 Addr : constant Node_Id := Address_Clause (E);
445 Expr : Node_Id;
446 Decl : constant Node_Id := Declaration_Node (E);
447 Typ : constant Entity_Id := Etype (E);
449 begin
450 if Present (Addr) then
451 Expr := Expression (Addr);
453 -- If we have no initialization of any kind, then we don't
454 -- need to place any restrictions on the address clause, because
455 -- the object will be elaborated after the address clause is
456 -- evaluated. This happens if the declaration has no initial
457 -- expression, or the type has no implicit initialization, or
458 -- the object is imported.
460 -- The same holds for all initialized scalar types and all
461 -- access types. Packed bit arrays of size up to 64 are
462 -- represented using a modular type with an initialization
463 -- (to zero) and can be processed like other initialized
464 -- scalar types.
466 -- If the type is controlled, code to attach the object to a
467 -- finalization chain is generated at the point of declaration,
468 -- and therefore the elaboration of the object cannot be delayed:
469 -- the address expression must be a constant.
471 if (No (Expression (Decl))
472 and then not Controlled_Type (Typ)
473 and then
474 (not Has_Non_Null_Base_Init_Proc (Typ)
475 or else Is_Imported (E)))
477 or else
478 (Present (Expression (Decl))
479 and then Is_Scalar_Type (Typ))
481 or else
482 Is_Access_Type (Typ)
484 or else
485 (Is_Bit_Packed_Array (Typ)
486 and then
487 Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
488 then
489 null;
491 -- Otherwise, we require the address clause to be constant
492 -- because the call to the initialization procedure (or the
493 -- attach code) has to happen at the point of the declaration.
495 else
496 Check_Constant_Address_Clause (Expr, E);
497 Set_Has_Delayed_Freeze (E, False);
498 end if;
500 if not Error_Posted (Expr)
501 and then not Controlled_Type (Typ)
502 then
503 Warn_Overlay (Expr, Typ, Name (Addr));
504 end if;
505 end if;
506 end Check_Address_Clause;
508 -----------------------------
509 -- Check_Compile_Time_Size --
510 -----------------------------
512 procedure Check_Compile_Time_Size (T : Entity_Id) is
514 procedure Set_Small_Size (T : Entity_Id; S : Uint);
515 -- Sets the compile time known size (32 bits or less) in the Esize
516 -- field, of T checking for a size clause that was given which attempts
517 -- to give a smaller size.
519 function Size_Known (T : Entity_Id) return Boolean;
520 -- Recursive function that does all the work
522 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
523 -- If T is a constrained subtype, its size is not known if any of its
524 -- discriminant constraints is not static and it is not a null record.
525 -- The test is conservative and doesn't check that the components are
526 -- in fact constrained by non-static discriminant values. Could be made
527 -- more precise ???
529 --------------------
530 -- Set_Small_Size --
531 --------------------
533 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
534 begin
535 if S > 32 then
536 return;
538 elsif Has_Size_Clause (T) then
539 if RM_Size (T) < S then
540 Error_Msg_Uint_1 := S;
541 Error_Msg_NE
542 ("size for & is too small, minimum is ^",
543 Size_Clause (T), T);
545 elsif Unknown_Esize (T) then
546 Set_Esize (T, S);
547 end if;
549 -- Set sizes if not set already
551 else
552 if Unknown_Esize (T) then
553 Set_Esize (T, S);
554 end if;
556 if Unknown_RM_Size (T) then
557 Set_RM_Size (T, S);
558 end if;
559 end if;
560 end Set_Small_Size;
562 ----------------
563 -- Size_Known --
564 ----------------
566 function Size_Known (T : Entity_Id) return Boolean is
567 Index : Entity_Id;
568 Comp : Entity_Id;
569 Ctyp : Entity_Id;
570 Low : Node_Id;
571 High : Node_Id;
573 begin
574 if Size_Known_At_Compile_Time (T) then
575 return True;
577 elsif Is_Scalar_Type (T)
578 or else Is_Task_Type (T)
579 then
580 return not Is_Generic_Type (T);
582 elsif Is_Array_Type (T) then
583 if Ekind (T) = E_String_Literal_Subtype then
584 Set_Small_Size (T, Component_Size (T)
585 * String_Literal_Length (T));
586 return True;
588 elsif not Is_Constrained (T) then
589 return False;
591 -- Don't do any recursion on type with error posted, since
592 -- we may have a malformed type that leads us into a loop
594 elsif Error_Posted (T) then
595 return False;
597 elsif not Size_Known (Component_Type (T)) then
598 return False;
599 end if;
601 -- Check for all indexes static, and also compute possible
602 -- size (in case it is less than 32 and may be packable).
604 declare
605 Esiz : Uint := Component_Size (T);
606 Dim : Uint;
608 begin
609 Index := First_Index (T);
610 while Present (Index) loop
611 if Nkind (Index) = N_Range then
612 Get_Index_Bounds (Index, Low, High);
614 elsif Error_Posted (Scalar_Range (Etype (Index))) then
615 return False;
617 else
618 Low := Type_Low_Bound (Etype (Index));
619 High := Type_High_Bound (Etype (Index));
620 end if;
622 if not Compile_Time_Known_Value (Low)
623 or else not Compile_Time_Known_Value (High)
624 or else Etype (Index) = Any_Type
625 then
626 return False;
628 else
629 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
631 if Dim >= 0 then
632 Esiz := Esiz * Dim;
633 else
634 Esiz := Uint_0;
635 end if;
636 end if;
638 Next_Index (Index);
639 end loop;
641 Set_Small_Size (T, Esiz);
642 return True;
643 end;
645 elsif Is_Access_Type (T) then
646 return True;
648 elsif Is_Private_Type (T)
649 and then not Is_Generic_Type (T)
650 and then Present (Underlying_Type (T))
651 then
652 -- Don't do any recursion on type with error posted, since
653 -- we may have a malformed type that leads us into a loop
655 if Error_Posted (T) then
656 return False;
657 else
658 return Size_Known (Underlying_Type (T));
659 end if;
661 elsif Is_Record_Type (T) then
663 -- A class-wide type is never considered to have a known size
665 if Is_Class_Wide_Type (T) then
666 return False;
668 -- A subtype of a variant record must not have non-static
669 -- discriminanted components.
671 elsif T /= Base_Type (T)
672 and then not Static_Discriminated_Components (T)
673 then
674 return False;
676 -- Don't do any recursion on type with error posted, since
677 -- we may have a malformed type that leads us into a loop
679 elsif Error_Posted (T) then
680 return False;
681 end if;
683 -- Now look at the components of the record
685 declare
686 -- The following two variables are used to keep track of
687 -- the size of packed records if we can tell the size of
688 -- the packed record in the front end. Packed_Size_Known
689 -- is True if so far we can figure out the size. It is
690 -- initialized to True for a packed record, unless the
691 -- record has discriminants. The reason we eliminate the
692 -- discriminated case is that we don't know the way the
693 -- back end lays out discriminated packed records. If
694 -- Packed_Size_Known is True, then Packed_Size is the
695 -- size in bits so far.
697 Packed_Size_Known : Boolean :=
698 Is_Packed (T)
699 and then not Has_Discriminants (T);
701 Packed_Size : Uint := Uint_0;
703 begin
704 -- Test for variant part present
706 if Has_Discriminants (T)
707 and then Present (Parent (T))
708 and then Nkind (Parent (T)) = N_Full_Type_Declaration
709 and then Nkind (Type_Definition (Parent (T))) =
710 N_Record_Definition
711 and then not Null_Present (Type_Definition (Parent (T)))
712 and then Present (Variant_Part
713 (Component_List (Type_Definition (Parent (T)))))
714 then
715 -- If variant part is present, and type is unconstrained,
716 -- then we must have defaulted discriminants, or a size
717 -- clause must be present for the type, or else the size
718 -- is definitely not known at compile time.
720 if not Is_Constrained (T)
721 and then
722 No (Discriminant_Default_Value
723 (First_Discriminant (T)))
724 and then Unknown_Esize (T)
725 then
726 return False;
727 end if;
728 end if;
730 -- Loop through components
732 Comp := First_Entity (T);
733 while Present (Comp) loop
734 if Ekind (Comp) = E_Component
735 or else
736 Ekind (Comp) = E_Discriminant
737 then
738 Ctyp := Etype (Comp);
740 -- We do not know the packed size if there is a
741 -- component clause present (we possibly could,
742 -- but this would only help in the case of a record
743 -- with partial rep clauses. That's because in the
744 -- case of full rep clauses, the size gets figured
745 -- out anyway by a different circuit).
747 if Present (Component_Clause (Comp)) then
748 Packed_Size_Known := False;
749 end if;
751 -- We need to identify a component that is an array
752 -- where the index type is an enumeration type with
753 -- non-standard representation, and some bound of the
754 -- type depends on a discriminant.
756 -- This is because gigi computes the size by doing a
757 -- substituation of the appropriate discriminant value
758 -- in the size expression for the base type, and gigi
759 -- is not clever enough to evaluate the resulting
760 -- expression (which involves a call to rep_to_pos)
761 -- at compile time.
763 -- It would be nice if gigi would either recognize that
764 -- this expression can be computed at compile time, or
765 -- alternatively figured out the size from the subtype
766 -- directly, where all the information is at hand ???
768 if Is_Array_Type (Etype (Comp))
769 and then Present (Packed_Array_Type (Etype (Comp)))
770 then
771 declare
772 Ocomp : constant Entity_Id :=
773 Original_Record_Component (Comp);
774 OCtyp : constant Entity_Id := Etype (Ocomp);
775 Ind : Node_Id;
776 Indtyp : Entity_Id;
777 Lo, Hi : Node_Id;
779 begin
780 Ind := First_Index (OCtyp);
781 while Present (Ind) loop
782 Indtyp := Etype (Ind);
784 if Is_Enumeration_Type (Indtyp)
785 and then Has_Non_Standard_Rep (Indtyp)
786 then
787 Lo := Type_Low_Bound (Indtyp);
788 Hi := Type_High_Bound (Indtyp);
790 if Is_Entity_Name (Lo)
791 and then
792 Ekind (Entity (Lo)) = E_Discriminant
793 then
794 return False;
796 elsif Is_Entity_Name (Hi)
797 and then
798 Ekind (Entity (Hi)) = E_Discriminant
799 then
800 return False;
801 end if;
802 end if;
804 Next_Index (Ind);
805 end loop;
806 end;
807 end if;
809 -- Clearly size of record is not known if the size of
810 -- one of the components is not known.
812 if not Size_Known (Ctyp) then
813 return False;
814 end if;
816 -- Accumulate packed size if possible
818 if Packed_Size_Known then
820 -- We can only deal with elementary types, since for
821 -- non-elementary components, alignment enters into
822 -- the picture, and we don't know enough to handle
823 -- proper alignment in this context. Packed arrays
824 -- count as elementary if the representation is a
825 -- modular type.
827 if Is_Elementary_Type (Ctyp)
828 or else (Is_Array_Type (Ctyp)
829 and then
830 Present (Packed_Array_Type (Ctyp))
831 and then
832 Is_Modular_Integer_Type
833 (Packed_Array_Type (Ctyp)))
834 then
835 -- If RM_Size is known and static, then we can
836 -- keep accumulating the packed size.
838 if Known_Static_RM_Size (Ctyp) then
840 -- A little glitch, to be removed sometime ???
841 -- gigi does not understand zero sizes yet.
843 if RM_Size (Ctyp) = Uint_0 then
844 Packed_Size_Known := False;
846 -- Normal case where we can keep accumulating
847 -- the packed array size.
849 else
850 Packed_Size := Packed_Size + RM_Size (Ctyp);
851 end if;
853 -- If we have a field whose RM_Size is not known
854 -- then we can't figure out the packed size here.
856 else
857 Packed_Size_Known := False;
858 end if;
860 -- If we have a non-elementary type we can't figure
861 -- out the packed array size (alignment issues).
863 else
864 Packed_Size_Known := False;
865 end if;
866 end if;
867 end if;
869 Next_Entity (Comp);
870 end loop;
872 if Packed_Size_Known then
873 Set_Small_Size (T, Packed_Size);
874 end if;
876 return True;
877 end;
879 else
880 return False;
881 end if;
882 end Size_Known;
884 -------------------------------------
885 -- Static_Discriminated_Components --
886 -------------------------------------
888 function Static_Discriminated_Components
889 (T : Entity_Id) return Boolean
891 Constraint : Elmt_Id;
893 begin
894 if Has_Discriminants (T)
895 and then Present (Discriminant_Constraint (T))
896 and then Present (First_Component (T))
897 then
898 Constraint := First_Elmt (Discriminant_Constraint (T));
899 while Present (Constraint) loop
900 if not Compile_Time_Known_Value (Node (Constraint)) then
901 return False;
902 end if;
904 Next_Elmt (Constraint);
905 end loop;
906 end if;
908 return True;
909 end Static_Discriminated_Components;
911 -- Start of processing for Check_Compile_Time_Size
913 begin
914 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
915 end Check_Compile_Time_Size;
917 -----------------------------
918 -- Check_Debug_Info_Needed --
919 -----------------------------
921 procedure Check_Debug_Info_Needed (T : Entity_Id) is
922 begin
923 if Needs_Debug_Info (T) or else Debug_Info_Off (T) then
924 return;
926 elsif Comes_From_Source (T)
927 or else Debug_Generated_Code
928 or else Debug_Flag_VV
929 then
930 Set_Debug_Info_Needed (T);
931 end if;
932 end Check_Debug_Info_Needed;
934 ----------------------------
935 -- Check_Strict_Alignment --
936 ----------------------------
938 procedure Check_Strict_Alignment (E : Entity_Id) is
939 Comp : Entity_Id;
941 begin
942 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
943 Set_Strict_Alignment (E);
945 elsif Is_Array_Type (E) then
946 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
948 elsif Is_Record_Type (E) then
949 if Is_Limited_Record (E) then
950 Set_Strict_Alignment (E);
951 return;
952 end if;
954 Comp := First_Component (E);
956 while Present (Comp) loop
957 if not Is_Type (Comp)
958 and then (Strict_Alignment (Etype (Comp))
959 or else Is_Aliased (Comp))
960 then
961 Set_Strict_Alignment (E);
962 return;
963 end if;
965 Next_Component (Comp);
966 end loop;
967 end if;
968 end Check_Strict_Alignment;
970 -------------------------
971 -- Check_Unsigned_Type --
972 -------------------------
974 procedure Check_Unsigned_Type (E : Entity_Id) is
975 Ancestor : Entity_Id;
976 Lo_Bound : Node_Id;
977 Btyp : Entity_Id;
979 begin
980 if not Is_Discrete_Or_Fixed_Point_Type (E) then
981 return;
982 end if;
984 -- Do not attempt to analyze case where range was in error
986 if Error_Posted (Scalar_Range (E)) then
987 return;
988 end if;
990 -- The situation that is non trivial is something like
992 -- subtype x1 is integer range -10 .. +10;
993 -- subtype x2 is x1 range 0 .. V1;
994 -- subtype x3 is x2 range V2 .. V3;
995 -- subtype x4 is x3 range V4 .. V5;
997 -- where Vn are variables. Here the base type is signed, but we still
998 -- know that x4 is unsigned because of the lower bound of x2.
1000 -- The only way to deal with this is to look up the ancestor chain
1002 Ancestor := E;
1003 loop
1004 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1005 return;
1006 end if;
1008 Lo_Bound := Type_Low_Bound (Ancestor);
1010 if Compile_Time_Known_Value (Lo_Bound) then
1012 if Expr_Rep_Value (Lo_Bound) >= 0 then
1013 Set_Is_Unsigned_Type (E, True);
1014 end if;
1016 return;
1018 else
1019 Ancestor := Ancestor_Subtype (Ancestor);
1021 -- If no ancestor had a static lower bound, go to base type
1023 if No (Ancestor) then
1025 -- Note: the reason we still check for a compile time known
1026 -- value for the base type is that at least in the case of
1027 -- generic formals, we can have bounds that fail this test,
1028 -- and there may be other cases in error situations.
1030 Btyp := Base_Type (E);
1032 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1033 return;
1034 end if;
1036 Lo_Bound := Type_Low_Bound (Base_Type (E));
1038 if Compile_Time_Known_Value (Lo_Bound)
1039 and then Expr_Rep_Value (Lo_Bound) >= 0
1040 then
1041 Set_Is_Unsigned_Type (E, True);
1042 end if;
1044 return;
1045 end if;
1046 end if;
1047 end loop;
1048 end Check_Unsigned_Type;
1050 -----------------------------
1051 -- Expand_Atomic_Aggregate --
1052 -----------------------------
1054 procedure Expand_Atomic_Aggregate (E : Entity_Id; Typ : Entity_Id) is
1055 Loc : constant Source_Ptr := Sloc (E);
1056 New_N : Node_Id;
1057 Temp : Entity_Id;
1059 begin
1060 if (Nkind (Parent (E)) = N_Object_Declaration
1061 or else Nkind (Parent (E)) = N_Assignment_Statement)
1062 and then Comes_From_Source (Parent (E))
1063 and then Nkind (E) = N_Aggregate
1064 then
1065 Temp :=
1066 Make_Defining_Identifier (Loc,
1067 New_Internal_Name ('T'));
1069 New_N :=
1070 Make_Object_Declaration (Loc,
1071 Defining_Identifier => Temp,
1072 Object_definition => New_Occurrence_Of (Typ, Loc),
1073 Expression => Relocate_Node (E));
1074 Insert_Before (Parent (E), New_N);
1075 Analyze (New_N);
1077 Set_Expression (Parent (E), New_Occurrence_Of (Temp, Loc));
1079 -- To prevent the temporary from being constant-folded (which
1080 -- would lead to the same piecemeal assignment on the original
1081 -- target) indicate to the back-end that the temporary is a
1082 -- variable with real storage. See description of this flag
1083 -- in Einfo, and the notes on N_Assignment_Statement and
1084 -- N_Object_Declaration in Sinfo.
1086 Set_Is_True_Constant (Temp, False);
1087 end if;
1088 end Expand_Atomic_Aggregate;
1090 ----------------
1091 -- Freeze_All --
1092 ----------------
1094 -- Note: the easy coding for this procedure would be to just build a
1095 -- single list of freeze nodes and then insert them and analyze them
1096 -- all at once. This won't work, because the analysis of earlier freeze
1097 -- nodes may recursively freeze types which would otherwise appear later
1098 -- on in the freeze list. So we must analyze and expand the freeze nodes
1099 -- as they are generated.
1101 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1102 Loc : constant Source_Ptr := Sloc (After);
1103 E : Entity_Id;
1104 Decl : Node_Id;
1106 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1107 -- This is the internal recursive routine that does freezing of
1108 -- entities (but NOT the analysis of default expressions, which
1109 -- should not be recursive, we don't want to analyze those till
1110 -- we are sure that ALL the types are frozen).
1112 --------------------
1113 -- Freeze_All_Ent --
1114 --------------------
1116 procedure Freeze_All_Ent
1117 (From : Entity_Id;
1118 After : in out Node_Id)
1120 E : Entity_Id;
1121 Flist : List_Id;
1122 Lastn : Node_Id;
1124 procedure Process_Flist;
1125 -- If freeze nodes are present, insert and analyze, and reset
1126 -- cursor for next insertion.
1128 -------------------
1129 -- Process_Flist --
1130 -------------------
1132 procedure Process_Flist is
1133 begin
1134 if Is_Non_Empty_List (Flist) then
1135 Lastn := Next (After);
1136 Insert_List_After_And_Analyze (After, Flist);
1138 if Present (Lastn) then
1139 After := Prev (Lastn);
1140 else
1141 After := Last (List_Containing (After));
1142 end if;
1143 end if;
1144 end Process_Flist;
1146 -- Start or processing for Freeze_All_Ent
1148 begin
1149 E := From;
1150 while Present (E) loop
1152 -- If the entity is an inner package which is not a package
1153 -- renaming, then its entities must be frozen at this point.
1154 -- Note that such entities do NOT get frozen at the end of
1155 -- the nested package itself (only library packages freeze).
1157 -- Same is true for task declarations, where anonymous records
1158 -- created for entry parameters must be frozen.
1160 if Ekind (E) = E_Package
1161 and then No (Renamed_Object (E))
1162 and then not Is_Child_Unit (E)
1163 and then not Is_Frozen (E)
1164 then
1165 New_Scope (E);
1166 Install_Visible_Declarations (E);
1167 Install_Private_Declarations (E);
1169 Freeze_All (First_Entity (E), After);
1171 End_Package_Scope (E);
1173 elsif Ekind (E) in Task_Kind
1174 and then
1175 (Nkind (Parent (E)) = N_Task_Type_Declaration
1176 or else
1177 Nkind (Parent (E)) = N_Single_Task_Declaration)
1178 then
1179 New_Scope (E);
1180 Freeze_All (First_Entity (E), After);
1181 End_Scope;
1183 -- For a derived tagged type, we must ensure that all the
1184 -- primitive operations of the parent have been frozen, so
1185 -- that their addresses will be in the parent's dispatch table
1186 -- at the point it is inherited.
1188 elsif Ekind (E) = E_Record_Type
1189 and then Is_Tagged_Type (E)
1190 and then Is_Tagged_Type (Etype (E))
1191 and then Is_Derived_Type (E)
1192 then
1193 declare
1194 Prim_List : constant Elist_Id :=
1195 Primitive_Operations (Etype (E));
1197 Prim : Elmt_Id;
1198 Subp : Entity_Id;
1200 begin
1201 Prim := First_Elmt (Prim_List);
1203 while Present (Prim) loop
1204 Subp := Node (Prim);
1206 if Comes_From_Source (Subp)
1207 and then not Is_Frozen (Subp)
1208 then
1209 Flist := Freeze_Entity (Subp, Loc);
1210 Process_Flist;
1211 end if;
1213 Next_Elmt (Prim);
1214 end loop;
1215 end;
1216 end if;
1218 if not Is_Frozen (E) then
1219 Flist := Freeze_Entity (E, Loc);
1220 Process_Flist;
1221 end if;
1223 -- If an incomplete type is still not frozen, this may be
1224 -- a premature freezing because of a body declaration that
1225 -- follows. Indicate where the freezing took place.
1227 -- If the freezing is caused by the end of the current
1228 -- declarative part, it is a Taft Amendment type, and there
1229 -- is no error.
1231 if not Is_Frozen (E)
1232 and then Ekind (E) = E_Incomplete_Type
1233 then
1234 declare
1235 Bod : constant Node_Id := Next (After);
1237 begin
1238 if (Nkind (Bod) = N_Subprogram_Body
1239 or else Nkind (Bod) = N_Entry_Body
1240 or else Nkind (Bod) = N_Package_Body
1241 or else Nkind (Bod) = N_Protected_Body
1242 or else Nkind (Bod) = N_Task_Body
1243 or else Nkind (Bod) in N_Body_Stub)
1244 and then
1245 List_Containing (After) = List_Containing (Parent (E))
1246 then
1247 Error_Msg_Sloc := Sloc (Next (After));
1248 Error_Msg_NE
1249 ("type& is frozen# before its full declaration",
1250 Parent (E), E);
1251 end if;
1252 end;
1253 end if;
1255 Next_Entity (E);
1256 end loop;
1257 end Freeze_All_Ent;
1259 -- Start of processing for Freeze_All
1261 begin
1262 Freeze_All_Ent (From, After);
1264 -- Now that all types are frozen, we can deal with default expressions
1265 -- that require us to build a default expression functions. This is the
1266 -- point at which such functions are constructed (after all types that
1267 -- might be used in such expressions have been frozen).
1269 -- We also add finalization chains to access types whose designated
1270 -- types are controlled. This is normally done when freezing the type,
1271 -- but this misses recursive type definitions where the later members
1272 -- of the recursion introduce controlled components (e.g. 5624-001).
1274 -- Loop through entities
1276 E := From;
1277 while Present (E) loop
1278 if Is_Subprogram (E) then
1280 if not Default_Expressions_Processed (E) then
1281 Process_Default_Expressions (E, After);
1282 end if;
1284 if not Has_Completion (E) then
1285 Decl := Unit_Declaration_Node (E);
1287 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1288 Build_And_Analyze_Renamed_Body (Decl, E, After);
1290 elsif Nkind (Decl) = N_Subprogram_Declaration
1291 and then Present (Corresponding_Body (Decl))
1292 and then
1293 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1294 = N_Subprogram_Renaming_Declaration
1295 then
1296 Build_And_Analyze_Renamed_Body
1297 (Decl, Corresponding_Body (Decl), After);
1298 end if;
1299 end if;
1301 elsif Ekind (E) in Task_Kind
1302 and then
1303 (Nkind (Parent (E)) = N_Task_Type_Declaration
1304 or else
1305 Nkind (Parent (E)) = N_Single_Task_Declaration)
1306 then
1307 declare
1308 Ent : Entity_Id;
1309 begin
1310 Ent := First_Entity (E);
1312 while Present (Ent) loop
1314 if Is_Entry (Ent)
1315 and then not Default_Expressions_Processed (Ent)
1316 then
1317 Process_Default_Expressions (Ent, After);
1318 end if;
1320 Next_Entity (Ent);
1321 end loop;
1322 end;
1324 elsif Is_Access_Type (E)
1325 and then Comes_From_Source (E)
1326 and then Ekind (Directly_Designated_Type (E)) = E_Incomplete_Type
1327 and then Controlled_Type (Designated_Type (E))
1328 and then No (Associated_Final_Chain (E))
1329 then
1330 Build_Final_List (Parent (E), E);
1331 end if;
1333 Next_Entity (E);
1334 end loop;
1335 end Freeze_All;
1337 -----------------------
1338 -- Freeze_And_Append --
1339 -----------------------
1341 procedure Freeze_And_Append
1342 (Ent : Entity_Id;
1343 Loc : Source_Ptr;
1344 Result : in out List_Id)
1346 L : constant List_Id := Freeze_Entity (Ent, Loc);
1347 begin
1348 if Is_Non_Empty_List (L) then
1349 if Result = No_List then
1350 Result := L;
1351 else
1352 Append_List (L, Result);
1353 end if;
1354 end if;
1355 end Freeze_And_Append;
1357 -------------------
1358 -- Freeze_Before --
1359 -------------------
1361 procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
1362 Freeze_Nodes : constant List_Id := Freeze_Entity (T, Sloc (N));
1363 begin
1364 if Is_Non_Empty_List (Freeze_Nodes) then
1365 Insert_Actions (N, Freeze_Nodes);
1366 end if;
1367 end Freeze_Before;
1369 -------------------
1370 -- Freeze_Entity --
1371 -------------------
1373 function Freeze_Entity (E : Entity_Id; Loc : Source_Ptr) return List_Id is
1374 Test_E : Entity_Id := E;
1375 Comp : Entity_Id;
1376 F_Node : Node_Id;
1377 Result : List_Id;
1378 Indx : Node_Id;
1379 Formal : Entity_Id;
1380 Atype : Entity_Id;
1382 procedure Check_Current_Instance (Comp_Decl : Node_Id);
1383 -- Check that an Access or Unchecked_Access attribute with
1384 -- a prefix which is the current instance type can only be
1385 -- applied when the type is limited.
1387 function After_Last_Declaration return Boolean;
1388 -- If Loc is a freeze_entity that appears after the last declaration
1389 -- in the scope, inhibit error messages on late completion.
1391 procedure Freeze_Record_Type (Rec : Entity_Id);
1392 -- Freeze each component, handle some representation clauses, and
1393 -- freeze primitive operations if this is a tagged type.
1395 ----------------------------
1396 -- After_Last_Declaration --
1397 ----------------------------
1399 function After_Last_Declaration return Boolean is
1400 Spec : constant Node_Id := Parent (Current_Scope);
1402 begin
1403 if Nkind (Spec) = N_Package_Specification then
1404 if Present (Private_Declarations (Spec)) then
1405 return Loc >= Sloc (Last (Private_Declarations (Spec)));
1407 elsif Present (Visible_Declarations (Spec)) then
1408 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
1409 else
1410 return False;
1411 end if;
1413 else
1414 return False;
1415 end if;
1416 end After_Last_Declaration;
1418 ----------------------------
1419 -- Check_Current_Instance --
1420 ----------------------------
1422 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
1424 function Process (N : Node_Id) return Traverse_Result;
1425 -- Process routine to apply check to given node.
1427 -------------
1428 -- Process --
1429 -------------
1431 function Process (N : Node_Id) return Traverse_Result is
1432 begin
1433 case Nkind (N) is
1434 when N_Attribute_Reference =>
1435 if (Attribute_Name (N) = Name_Access
1436 or else
1437 Attribute_Name (N) = Name_Unchecked_Access)
1438 and then Is_Entity_Name (Prefix (N))
1439 and then Is_Type (Entity (Prefix (N)))
1440 and then Entity (Prefix (N)) = E
1441 then
1442 Error_Msg_N
1443 ("current instance must be a limited type", Prefix (N));
1444 return Abandon;
1445 else
1446 return OK;
1447 end if;
1449 when others => return OK;
1450 end case;
1451 end Process;
1453 procedure Traverse is new Traverse_Proc (Process);
1455 -- Start of processing for Check_Current_Instance
1457 begin
1458 Traverse (Comp_Decl);
1459 end Check_Current_Instance;
1461 ------------------------
1462 -- Freeze_Record_Type --
1463 ------------------------
1465 procedure Freeze_Record_Type (Rec : Entity_Id) is
1466 Comp : Entity_Id;
1467 IR : Node_Id;
1468 Junk : Boolean;
1469 ADC : Node_Id;
1470 Prev : Entity_Id;
1472 Unplaced_Component : Boolean := False;
1473 -- Set True if we find at least one component with no component
1474 -- clause (used to warn about useless Pack pragmas).
1476 Placed_Component : Boolean := False;
1477 -- Set True if we find at least one component with a component
1478 -- clause (used to warn about useless Bit_Order pragmas).
1480 procedure Check_Itype (Desig : Entity_Id);
1481 -- If the component subtype is an access to a constrained subtype
1482 -- of an already frozen type, make the subtype frozen as well. It
1483 -- might otherwise be frozen in the wrong scope, and a freeze node
1484 -- on subtype has no effect.
1486 -----------------
1487 -- Check_Itype --
1488 -----------------
1490 procedure Check_Itype (Desig : Entity_Id) is
1491 begin
1492 if not Is_Frozen (Desig)
1493 and then Is_Frozen (Base_Type (Desig))
1494 then
1495 Set_Is_Frozen (Desig);
1497 -- In addition, add an Itype_Reference to ensure that the
1498 -- access subtype is elaborated early enough. This cannot
1499 -- be done if the subtype may depend on discriminants.
1501 if Ekind (Comp) = E_Component
1502 and then Is_Itype (Etype (Comp))
1503 and then not Has_Discriminants (Rec)
1504 then
1505 IR := Make_Itype_Reference (Sloc (Comp));
1506 Set_Itype (IR, Desig);
1508 if No (Result) then
1509 Result := New_List (IR);
1510 else
1511 Append (IR, Result);
1512 end if;
1513 end if;
1514 end if;
1515 end Check_Itype;
1517 -- Start of processing for Freeze_Record_Type
1519 begin
1520 -- If this is a subtype of a controlled type, declared without
1521 -- a constraint, the _controller may not appear in the component
1522 -- list if the parent was not frozen at the point of subtype
1523 -- declaration. Inherit the _controller component now.
1525 if Rec /= Base_Type (Rec)
1526 and then Has_Controlled_Component (Rec)
1527 then
1528 if Nkind (Parent (Rec)) = N_Subtype_Declaration
1529 and then Is_Entity_Name (Subtype_Indication (Parent (Rec)))
1530 then
1531 Set_First_Entity (Rec, First_Entity (Base_Type (Rec)));
1533 -- If this is an internal type without a declaration, as for a
1534 -- record component, the base type may not yet be frozen, and its
1535 -- controller has not been created. Add an explicit freeze node
1536 -- for the itype, so it will be frozen after the base type.
1538 elsif Is_Itype (Rec)
1539 and then Has_Delayed_Freeze (Base_Type (Rec))
1540 and then
1541 Nkind (Associated_Node_For_Itype (Rec)) =
1542 N_Component_Declaration
1543 then
1544 Ensure_Freeze_Node (Rec);
1545 end if;
1546 end if;
1548 -- Freeze components and embedded subtypes.
1550 Comp := First_Entity (Rec);
1551 Prev := Empty;
1553 while Present (Comp) loop
1555 -- First handle the (real) component case.
1557 if Ekind (Comp) = E_Component
1558 or else Ekind (Comp) = E_Discriminant
1559 then
1560 declare
1561 CC : constant Node_Id := Component_Clause (Comp);
1563 begin
1564 -- Freezing a record type freezes the type of each of its
1565 -- components. However, if the type of the component is
1566 -- part of this record, we do not want or need a separate
1567 -- Freeze_Node. Note that Is_Itype is wrong because that's
1568 -- also set in private type cases. We also can't check
1569 -- for the Scope being exactly Rec because of private types
1570 -- and record extensions.
1571 if Is_Itype (Etype (Comp))
1572 and then Is_Record_Type (Underlying_Type
1573 (Scope (Etype (Comp))))
1574 then
1575 Undelay_Type (Etype (Comp));
1576 end if;
1578 Freeze_And_Append (Etype (Comp), Loc, Result);
1580 -- Check for error of component clause given for variable
1581 -- sized type. We have to delay this test till this point,
1582 -- since the component type has to be frozen for us to know
1583 -- if it is variable length. We omit this test in a generic
1584 -- context, it will be applied at instantiation time.
1586 if Present (CC) then
1587 Placed_Component := True;
1589 if Inside_A_Generic then
1590 null;
1592 elsif not Size_Known_At_Compile_Time
1593 (Underlying_Type (Etype (Comp)))
1594 then
1595 Error_Msg_N
1596 ("component clause not allowed for variable " &
1597 "length component", CC);
1598 end if;
1600 else
1601 Unplaced_Component := True;
1602 end if;
1604 -- Case of component requires byte alignment
1606 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
1608 -- Set the enclosing record to also require byte align
1610 Set_Must_Be_On_Byte_Boundary (Rec);
1612 -- Check for component clause that is inconsistent
1613 -- with the required byte boundary alignment.
1615 if Present (CC)
1616 and then Normalized_First_Bit (Comp) mod
1617 System_Storage_Unit /= 0
1618 then
1619 Error_Msg_N
1620 ("component & must be byte aligned",
1621 Component_Name (Component_Clause (Comp)));
1622 end if;
1623 end if;
1625 -- If component clause is present, then deal with the
1626 -- non-default bit order case. We cannot do this before
1627 -- the freeze point, because there is no required order
1628 -- for the component clause and the bit_order clause.
1630 -- We only do this processing for the base type, and in
1631 -- fact that's important, since otherwise if there are
1632 -- record subtypes, we could reverse the bits once for
1633 -- each subtype, which would be incorrect.
1635 if Present (CC)
1636 and then Reverse_Bit_Order (Rec)
1637 and then Ekind (E) = E_Record_Type
1638 then
1639 declare
1640 CFB : constant Uint := Component_Bit_Offset (Comp);
1641 CSZ : constant Uint := Esize (Comp);
1642 CLC : constant Node_Id := Component_Clause (Comp);
1643 Pos : constant Node_Id := Position (CLC);
1644 FB : constant Node_Id := First_Bit (CLC);
1646 Storage_Unit_Offset : constant Uint :=
1647 CFB / System_Storage_Unit;
1649 Start_Bit : constant Uint :=
1650 CFB mod System_Storage_Unit;
1652 begin
1653 -- Cases where field goes over storage unit boundary
1655 if Start_Bit + CSZ > System_Storage_Unit then
1657 -- Allow multi-byte field but generate warning
1659 if Start_Bit mod System_Storage_Unit = 0
1660 and then CSZ mod System_Storage_Unit = 0
1661 then
1662 Error_Msg_N
1663 ("multi-byte field specified with non-standard"
1664 & " Bit_Order?", CLC);
1666 if Bytes_Big_Endian then
1667 Error_Msg_N
1668 ("bytes are not reversed "
1669 & "(component is big-endian)?", CLC);
1670 else
1671 Error_Msg_N
1672 ("bytes are not reversed "
1673 & "(component is little-endian)?", CLC);
1674 end if;
1676 -- Do not allow non-contiguous field
1678 else
1679 Error_Msg_N
1680 ("attempt to specify non-contiguous field"
1681 & " not permitted", CLC);
1682 Error_Msg_N
1683 ("\(caused by non-standard Bit_Order "
1684 & "specified)", CLC);
1685 end if;
1687 -- Case where field fits in one storage unit
1689 else
1690 -- Give warning if suspicious component clause
1692 if Intval (FB) >= System_Storage_Unit then
1693 Error_Msg_N
1694 ("?Bit_Order clause does not affect " &
1695 "byte ordering", Pos);
1696 Error_Msg_Uint_1 :=
1697 Intval (Pos) + Intval (FB) /
1698 System_Storage_Unit;
1699 Error_Msg_N
1700 ("?position normalized to ^ before bit " &
1701 "order interpreted", Pos);
1702 end if;
1704 -- Here is where we fix up the Component_Bit_Offset
1705 -- value to account for the reverse bit order.
1706 -- Some examples of what needs to be done are:
1708 -- First_Bit .. Last_Bit Component_Bit_Offset
1709 -- old new old new
1711 -- 0 .. 0 7 .. 7 0 7
1712 -- 0 .. 1 6 .. 7 0 6
1713 -- 0 .. 2 5 .. 7 0 5
1714 -- 0 .. 7 0 .. 7 0 4
1716 -- 1 .. 1 6 .. 6 1 6
1717 -- 1 .. 4 3 .. 6 1 3
1718 -- 4 .. 7 0 .. 3 4 0
1720 -- The general rule is that the first bit is
1721 -- is obtained by subtracting the old ending bit
1722 -- from storage_unit - 1.
1724 Set_Component_Bit_Offset
1725 (Comp,
1726 (Storage_Unit_Offset * System_Storage_Unit) +
1727 (System_Storage_Unit - 1) -
1728 (Start_Bit + CSZ - 1));
1730 Set_Normalized_First_Bit
1731 (Comp,
1732 Component_Bit_Offset (Comp) mod
1733 System_Storage_Unit);
1734 end if;
1735 end;
1736 end if;
1737 end;
1738 end if;
1740 -- If the component is an Itype with Delayed_Freeze and is
1741 -- either a record or array subtype and its base type has not
1742 -- yet been frozen, we must remove this from the entity list
1743 -- of this record and put it on the entity list of the scope of
1744 -- its base type. Note that we know that this is not the type
1745 -- of a component since we cleared Has_Delayed_Freeze for it
1746 -- in the previous loop. Thus this must be the Designated_Type
1747 -- of an access type, which is the type of a component.
1748 if Is_Itype (Comp)
1749 and then Is_Type (Scope (Comp))
1750 and then Is_Composite_Type (Comp)
1751 and then Base_Type (Comp) /= Comp
1752 and then Has_Delayed_Freeze (Comp)
1753 and then not Is_Frozen (Base_Type (Comp))
1754 then
1755 declare
1756 Will_Be_Frozen : Boolean := False;
1757 S : Entity_Id := Scope (Rec);
1759 begin
1760 -- We have a pretty bad kludge here. Suppose Rec is a
1761 -- subtype being defined in a subprogram that's created
1762 -- as part of the freezing of Rec'Base. In that case,
1763 -- we know that Comp'Base must have already been frozen by
1764 -- the time we get to elaborate this because Gigi doesn't
1765 -- elaborate any bodies until it has elaborated all of the
1766 -- declarative part. But Is_Frozen will not be set at this
1767 -- point because we are processing code in lexical order.
1769 -- We detect this case by going up the Scope chain of
1770 -- Rec and seeing if we have a subprogram scope before
1771 -- reaching the top of the scope chain or that of Comp'Base.
1772 -- If we do, then mark that Comp'Base will actually be
1773 -- frozen. If so, we merely undelay it.
1774 while Present (S) loop
1775 if Is_Subprogram (S) then
1776 Will_Be_Frozen := True;
1777 exit;
1778 elsif S = Scope (Base_Type (Comp)) then
1779 exit;
1780 end if;
1782 S := Scope (S);
1783 end loop;
1785 if Will_Be_Frozen then
1786 Undelay_Type (Comp);
1787 else
1788 if Present (Prev) then
1789 Set_Next_Entity (Prev, Next_Entity (Comp));
1790 else
1791 Set_First_Entity (Rec, Next_Entity (Comp));
1792 end if;
1794 -- Insert in entity list of scope of base type (which
1795 -- must be an enclosing scope, because still unfrozen).
1797 Append_Entity (Comp, Scope (Base_Type (Comp)));
1798 end if;
1799 end;
1801 -- If the component is an access type with an allocator
1802 -- as default value, the designated type will be frozen
1803 -- by the corresponding expression in init_proc. In order
1804 -- to place the freeze node for the designated type before
1805 -- that for the current record type, freeze it now.
1807 -- Same process if the component is an array of access types,
1808 -- initialized with an aggregate. If the designated type is
1809 -- private, it cannot contain allocators, and it is premature
1810 -- to freeze the type, so we check for this as well.
1812 elsif Is_Access_Type (Etype (Comp))
1813 and then Present (Parent (Comp))
1814 and then Present (Expression (Parent (Comp)))
1815 and then Nkind (Expression (Parent (Comp))) = N_Allocator
1816 then
1817 declare
1818 Alloc : constant Node_Id := Expression (Parent (Comp));
1820 begin
1821 -- If component is pointer to a classwide type, freeze
1822 -- the specific type in the expression being allocated.
1823 -- The expression may be a subtype indication, in which
1824 -- case freeze the subtype mark.
1826 if Is_Class_Wide_Type (Designated_Type (Etype (Comp))) then
1827 if Is_Entity_Name (Expression (Alloc)) then
1828 Freeze_And_Append
1829 (Entity (Expression (Alloc)), Loc, Result);
1830 elsif
1831 Nkind (Expression (Alloc)) = N_Subtype_Indication
1832 then
1833 Freeze_And_Append
1834 (Entity (Subtype_Mark (Expression (Alloc))),
1835 Loc, Result);
1836 end if;
1838 elsif Is_Itype (Designated_Type (Etype (Comp))) then
1839 Check_Itype (Designated_Type (Etype (Comp)));
1841 else
1842 Freeze_And_Append
1843 (Designated_Type (Etype (Comp)), Loc, Result);
1844 end if;
1845 end;
1847 elsif Is_Access_Type (Etype (Comp))
1848 and then Is_Itype (Designated_Type (Etype (Comp)))
1849 then
1850 Check_Itype (Designated_Type (Etype (Comp)));
1852 elsif Is_Array_Type (Etype (Comp))
1853 and then Is_Access_Type (Component_Type (Etype (Comp)))
1854 and then Present (Parent (Comp))
1855 and then Nkind (Parent (Comp)) = N_Component_Declaration
1856 and then Present (Expression (Parent (Comp)))
1857 and then Nkind (Expression (Parent (Comp))) = N_Aggregate
1858 and then Is_Fully_Defined
1859 (Designated_Type (Component_Type (Etype (Comp))))
1860 then
1861 Freeze_And_Append
1862 (Designated_Type
1863 (Component_Type (Etype (Comp))), Loc, Result);
1864 end if;
1866 Prev := Comp;
1867 Next_Entity (Comp);
1868 end loop;
1870 -- Check for useless pragma Bit_Order
1872 if not Placed_Component and then Reverse_Bit_Order (Rec) then
1873 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
1874 Error_Msg_N ("?Bit_Order specification has no effect", ADC);
1875 Error_Msg_N ("\?since no component clauses were specified", ADC);
1876 end if;
1878 -- Check for useless pragma Pack when all components placed
1880 if Is_Packed (Rec)
1881 and then not Unplaced_Component
1882 and then Warn_On_Redundant_Constructs
1883 then
1884 Error_Msg_N
1885 ("?pragma Pack has no effect, no unplaced components",
1886 Get_Rep_Pragma (Rec, Name_Pack));
1887 Set_Is_Packed (Rec, False);
1888 end if;
1890 -- If this is the record corresponding to a remote type,
1891 -- freeze the remote type here since that is what we are
1892 -- semantically freezing. This prevents having the freeze
1893 -- node for that type in an inner scope.
1895 -- Also, Check for controlled components and unchecked unions.
1896 -- Finally, enforce the restriction that access attributes with
1897 -- a current instance prefix can only apply to limited types.
1899 if Ekind (Rec) = E_Record_Type then
1900 if Present (Corresponding_Remote_Type (Rec)) then
1901 Freeze_And_Append
1902 (Corresponding_Remote_Type (Rec), Loc, Result);
1903 end if;
1905 Comp := First_Component (Rec);
1906 while Present (Comp) loop
1907 if Has_Controlled_Component (Etype (Comp))
1908 or else (Chars (Comp) /= Name_uParent
1909 and then Is_Controlled (Etype (Comp)))
1910 or else (Is_Protected_Type (Etype (Comp))
1911 and then Present
1912 (Corresponding_Record_Type (Etype (Comp)))
1913 and then Has_Controlled_Component
1914 (Corresponding_Record_Type (Etype (Comp))))
1915 then
1916 Set_Has_Controlled_Component (Rec);
1917 exit;
1918 end if;
1920 if Has_Unchecked_Union (Etype (Comp)) then
1921 Set_Has_Unchecked_Union (Rec);
1922 end if;
1924 if Has_Per_Object_Constraint (Comp)
1925 and then not Is_Limited_Type (Rec)
1926 then
1927 -- Scan component declaration for likely misuses of
1928 -- current instance, either in a constraint or in a
1929 -- default expression.
1931 Check_Current_Instance (Parent (Comp));
1932 end if;
1934 Next_Component (Comp);
1935 end loop;
1936 end if;
1938 Set_Component_Alignment_If_Not_Set (Rec);
1940 -- For first subtypes, check if there are any fixed-point
1941 -- fields with component clauses, where we must check the size.
1942 -- This is not done till the freeze point, since for fixed-point
1943 -- types, we do not know the size until the type is frozen.
1944 -- Similar processing applies to bit packed arrays.
1946 if Is_First_Subtype (Rec) then
1947 Comp := First_Component (Rec);
1949 while Present (Comp) loop
1950 if Present (Component_Clause (Comp))
1951 and then (Is_Fixed_Point_Type (Etype (Comp))
1952 or else
1953 Is_Bit_Packed_Array (Etype (Comp)))
1954 then
1955 Check_Size
1956 (Component_Name (Component_Clause (Comp)),
1957 Etype (Comp),
1958 Esize (Comp),
1959 Junk);
1960 end if;
1962 Next_Component (Comp);
1963 end loop;
1964 end if;
1965 end Freeze_Record_Type;
1967 -- Start of processing for Freeze_Entity
1969 begin
1970 -- We are going to test for various reasons why this entity need
1971 -- not be frozen here, but in the case of an Itype that's defined
1972 -- within a record, that test actually applies to the record.
1973 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
1974 Test_E := Scope (E);
1975 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
1976 and then Is_Record_Type (Underlying_Type (Scope (E)))
1977 then
1978 Test_E := Underlying_Type (Scope (E));
1979 end if;
1981 -- Do not freeze if already frozen since we only need one freeze node
1983 if Is_Frozen (E) then
1984 return No_List;
1986 -- It is improper to freeze an external entity within a generic
1987 -- because its freeze node will appear in a non-valid context.
1988 -- The entity will be frozen in the proper scope after the current
1989 -- generic is analyzed.
1991 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
1992 return No_List;
1994 -- Do not freeze a global entity within an inner scope created during
1995 -- expansion. A call to subprogram E within some internal procedure
1996 -- (a stream attribute for example) might require freezing E, but the
1997 -- freeze node must appear in the same declarative part as E itself.
1998 -- The two-pass elaboration mechanism in gigi guarantees that E will
1999 -- be frozen before the inner call is elaborated. We exclude constants
2000 -- from this test, because deferred constants may be frozen early, and
2001 -- must be diagnosed (see e.g. 1522-005). If the enclosing subprogram
2002 -- comes from source, or is a generic instance, then the freeze point
2003 -- is the one mandated by the language. and we freze the entity.
2005 elsif In_Open_Scopes (Scope (Test_E))
2006 and then Scope (Test_E) /= Current_Scope
2007 and then Ekind (Test_E) /= E_Constant
2008 then
2009 declare
2010 S : Entity_Id := Current_Scope;
2012 begin
2013 while Present (S) loop
2014 if Is_Overloadable (S) then
2015 if Comes_From_Source (S)
2016 or else Is_Generic_Instance (S)
2017 then
2018 exit;
2019 else
2020 return No_List;
2021 end if;
2022 end if;
2024 S := Scope (S);
2025 end loop;
2026 end;
2028 -- Similarly, an inlined instance body may make reference to global
2029 -- entities, but these references cannot be the proper freezing point
2030 -- for them, and the the absence of inlining freezing will take place
2031 -- in their own scope. Normally instance bodies are analyzed after
2032 -- the enclosing compilation, and everything has been frozen at the
2033 -- proper place, but with front-end inlining an instance body is
2034 -- compiled before the end of the enclosing scope, and as a result
2035 -- out-of-order freezing must be prevented.
2037 elsif Front_End_Inlining
2038 and then In_Instance_Body
2039 and then Present (Scope (Test_E))
2040 then
2041 declare
2042 S : Entity_Id := Scope (Test_E);
2043 begin
2044 while Present (S) loop
2045 if Is_Generic_Instance (S) then
2046 exit;
2047 else
2048 S := Scope (S);
2049 end if;
2050 end loop;
2052 if No (S) then
2053 return No_List;
2054 end if;
2055 end;
2056 end if;
2058 -- Here to freeze the entity
2060 Result := No_List;
2061 Set_Is_Frozen (E);
2063 -- Case of entity being frozen is other than a type
2065 if not Is_Type (E) then
2067 -- If entity is exported or imported and does not have an external
2068 -- name, now is the time to provide the appropriate default name.
2069 -- Skip this if the entity is stubbed, since we don't need a name
2070 -- for any stubbed routine.
2072 if (Is_Imported (E) or else Is_Exported (E))
2073 and then No (Interface_Name (E))
2074 and then Convention (E) /= Convention_Stubbed
2075 then
2076 Set_Encoded_Interface_Name
2077 (E, Get_Default_External_Name (E));
2079 -- Special processing for atomic objects appearing in object decls
2081 elsif Is_Atomic (E)
2082 and then Nkind (Parent (E)) = N_Object_Declaration
2083 and then Present (Expression (Parent (E)))
2084 then
2085 declare
2086 Expr : constant Node_Id := Expression (Parent (E));
2088 begin
2089 -- If expression is an aggregate, assign to a temporary to
2090 -- ensure that the actual assignment is done atomically rather
2091 -- than component-wise (the assignment to the temp may be done
2092 -- component-wise, but that is harmless.
2094 if Nkind (Expr) = N_Aggregate then
2095 Expand_Atomic_Aggregate (Expr, Etype (E));
2097 -- If the expression is a reference to a record or array
2098 -- object entity, then reset Is_True_Constant to False so
2099 -- that the compiler will not optimize away the intermediate
2100 -- object, which we need in this case for the same reason
2101 -- (to ensure that the actual assignment is atomic, rather
2102 -- than component-wise).
2104 elsif Is_Entity_Name (Expr)
2105 and then (Is_Record_Type (Etype (Expr))
2106 or else
2107 Is_Array_Type (Etype (Expr)))
2108 then
2109 Set_Is_True_Constant (Entity (Expr), False);
2110 end if;
2111 end;
2112 end if;
2114 -- For a subprogram, freeze all parameter types and also the return
2115 -- type (RM 13.14(14)). However skip this for internal subprograms.
2116 -- This is also the point where any extra formal parameters are
2117 -- created since we now know whether the subprogram will use
2118 -- a foreign convention.
2120 if Is_Subprogram (E) then
2121 if not Is_Internal (E) then
2122 declare
2123 F_Type : Entity_Id;
2124 Warn_Node : Node_Id;
2126 function Is_Fat_C_Ptr_Type (T : Entity_Id) return Boolean;
2127 -- Determines if given type entity is a fat pointer type
2128 -- used as an argument type or return type to a subprogram
2129 -- with C or C++ convention set.
2131 --------------------------
2132 -- Is_Fat_C_Access_Type --
2133 --------------------------
2135 function Is_Fat_C_Ptr_Type (T : Entity_Id) return Boolean is
2136 begin
2137 return (Convention (E) = Convention_C
2138 or else
2139 Convention (E) = Convention_CPP)
2140 and then Is_Access_Type (T)
2141 and then Esize (T) > Ttypes.System_Address_Size;
2142 end Is_Fat_C_Ptr_Type;
2144 begin
2145 -- Loop through formals
2147 Formal := First_Formal (E);
2148 while Present (Formal) loop
2149 F_Type := Etype (Formal);
2150 Freeze_And_Append (F_Type, Loc, Result);
2152 if Is_Private_Type (F_Type)
2153 and then Is_Private_Type (Base_Type (F_Type))
2154 and then No (Full_View (Base_Type (F_Type)))
2155 and then not Is_Generic_Type (F_Type)
2156 and then not Is_Derived_Type (F_Type)
2157 then
2158 -- If the type of a formal is incomplete, subprogram
2159 -- is being frozen prematurely. Within an instance
2160 -- (but not within a wrapper package) this is an
2161 -- an artifact of our need to regard the end of an
2162 -- instantiation as a freeze point. Otherwise it is
2163 -- a definite error.
2165 -- and then not Is_Wrapper_Package (Current_Scope) ???
2167 if In_Instance then
2168 Set_Is_Frozen (E, False);
2169 return No_List;
2171 elsif not After_Last_Declaration then
2172 Error_Msg_Node_1 := F_Type;
2173 Error_Msg
2174 ("type& must be fully defined before this point",
2175 Loc);
2176 end if;
2177 end if;
2179 -- Check bad use of fat C pointer
2181 if Warn_On_Export_Import and then
2182 Is_Fat_C_Ptr_Type (F_Type)
2183 then
2184 Error_Msg_Qual_Level := 1;
2185 Error_Msg_N
2186 ("?type of & does not correspond to C pointer",
2187 Formal);
2188 Error_Msg_Qual_Level := 0;
2189 end if;
2191 -- Check for unconstrained array in exported foreign
2192 -- convention case.
2194 if Convention (E) in Foreign_Convention
2195 and then not Is_Imported (E)
2196 and then Is_Array_Type (F_Type)
2197 and then not Is_Constrained (F_Type)
2198 and then Warn_On_Export_Import
2199 then
2200 Error_Msg_Qual_Level := 1;
2202 -- If this is an inherited operation, place the
2203 -- warning on the derived type declaration, rather
2204 -- than on the original subprogram.
2206 if Nkind (Original_Node (Parent (E))) =
2207 N_Full_Type_Declaration
2208 then
2209 Warn_Node := Parent (E);
2211 if Formal = First_Formal (E) then
2212 Error_Msg_NE
2213 ("?in inherited operation&!", Warn_Node, E);
2214 end if;
2215 else
2216 Warn_Node := Formal;
2217 end if;
2219 Error_Msg_NE
2220 ("?type of argument& is unconstrained array",
2221 Warn_Node, Formal);
2222 Error_Msg_NE
2223 ("?foreign caller must pass bounds explicitly",
2224 Warn_Node, Formal);
2225 Error_Msg_Qual_Level := 0;
2226 end if;
2228 Next_Formal (Formal);
2229 end loop;
2231 -- Check return type
2233 if Ekind (E) = E_Function then
2234 Freeze_And_Append (Etype (E), Loc, Result);
2236 if Warn_On_Export_Import
2237 and then Is_Fat_C_Ptr_Type (Etype (E))
2238 then
2239 Error_Msg_N
2240 ("?return type of& does not correspond to C pointer",
2243 elsif Is_Array_Type (Etype (E))
2244 and then not Is_Constrained (Etype (E))
2245 and then not Is_Imported (E)
2246 and then Convention (E) in Foreign_Convention
2247 and then Warn_On_Export_Import
2248 then
2249 Error_Msg_N
2250 ("?foreign convention function& should not " &
2251 "return unconstrained array", E);
2252 end if;
2253 end if;
2254 end;
2255 end if;
2257 -- Must freeze its parent first if it is a derived subprogram
2259 if Present (Alias (E)) then
2260 Freeze_And_Append (Alias (E), Loc, Result);
2261 end if;
2263 -- If the return type requires a transient scope, and we are on
2264 -- a target allowing functions to return with a depressed stack
2265 -- pointer, then we mark the function as requiring this treatment.
2267 if Ekind (E) = E_Function
2268 and then Functions_Return_By_DSP_On_Target
2269 and then Requires_Transient_Scope (Etype (E))
2270 then
2271 Set_Function_Returns_With_DSP (E);
2272 end if;
2274 if not Is_Internal (E) then
2275 Freeze_Subprogram (E);
2276 end if;
2278 -- Here for other than a subprogram or type
2280 else
2281 -- If entity has a type, and it is not a generic unit, then
2282 -- freeze it first (RM 13.14(10))
2284 if Present (Etype (E))
2285 and then Ekind (E) /= E_Generic_Function
2286 then
2287 Freeze_And_Append (Etype (E), Loc, Result);
2288 end if;
2290 -- Special processing for objects created by object declaration
2292 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
2294 -- For object created by object declaration, perform required
2295 -- categorization (preelaborate and pure) checks. Defer these
2296 -- checks to freeze time since pragma Import inhibits default
2297 -- initialization and thus pragma Import affects these checks.
2299 Validate_Object_Declaration (Declaration_Node (E));
2301 -- If there is an address clause, check it is valid
2303 Check_Address_Clause (E);
2305 -- For imported objects, set Is_Public unless there is also
2306 -- an address clause, which means that there is no external
2307 -- symbol needed for the Import (Is_Public may still be set
2308 -- for other unrelated reasons). Note that we delayed this
2309 -- processing till freeze time so that we can be sure not
2310 -- to set the flag if there is an address clause. If there
2311 -- is such a clause, then the only purpose of the import
2312 -- pragma is to suppress implicit initialization.
2314 if Is_Imported (E)
2315 and then not Present (Address_Clause (E))
2316 then
2317 Set_Is_Public (E);
2318 end if;
2319 end if;
2321 -- Check that a constant which has a pragma Volatile[_Components]
2322 -- or Atomic[_Components] also has a pragma Import (RM C.6(13))
2324 -- Note: Atomic[_Components] also sets Volatile[_Components]
2326 if Ekind (E) = E_Constant
2327 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
2328 and then not Is_Imported (E)
2329 then
2330 -- Make sure we actually have a pragma, and have not merely
2331 -- inherited the indication from elsewhere (e.g. an address
2332 -- clause, which is not good enough in RM terms!)
2334 if Has_Rep_Pragma (E, Name_Atomic)
2335 or else
2336 Has_Rep_Pragma (E, Name_Atomic_Components)
2337 then
2338 Error_Msg_N
2339 ("stand alone atomic constant must be " &
2340 "imported ('R'M 'C.6(13))", E);
2342 elsif Has_Rep_Pragma (E, Name_Volatile)
2343 or else
2344 Has_Rep_Pragma (E, Name_Volatile_Components)
2345 then
2346 Error_Msg_N
2347 ("stand alone volatile constant must be " &
2348 "imported ('R'M 'C.6(13))", E);
2349 end if;
2350 end if;
2352 -- Static objects require special handling
2354 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
2355 and then Is_Statically_Allocated (E)
2356 then
2357 Freeze_Static_Object (E);
2358 end if;
2360 -- Remaining step is to layout objects
2362 if Ekind (E) = E_Variable
2363 or else
2364 Ekind (E) = E_Constant
2365 or else
2366 Ekind (E) = E_Loop_Parameter
2367 or else
2368 Is_Formal (E)
2369 then
2370 Layout_Object (E);
2371 end if;
2372 end if;
2374 -- Case of a type or subtype being frozen
2376 else
2377 -- The type may be defined in a generic unit. This can occur when
2378 -- freezing a generic function that returns the type (which is
2379 -- defined in a parent unit). It is clearly meaningless to freeze
2380 -- this type. However, if it is a subtype, its size may be determi-
2381 -- nable and used in subsequent checks, so might as well try to
2382 -- compute it.
2384 if Present (Scope (E))
2385 and then Is_Generic_Unit (Scope (E))
2386 then
2387 Check_Compile_Time_Size (E);
2388 return No_List;
2389 end if;
2391 -- Deal with special cases of freezing for subtype
2393 if E /= Base_Type (E) then
2395 -- If ancestor subtype present, freeze that first.
2396 -- Note that this will also get the base type frozen.
2398 Atype := Ancestor_Subtype (E);
2400 if Present (Atype) then
2401 Freeze_And_Append (Atype, Loc, Result);
2403 -- Otherwise freeze the base type of the entity before
2404 -- freezing the entity itself, (RM 13.14(15)).
2406 elsif E /= Base_Type (E) then
2407 Freeze_And_Append (Base_Type (E), Loc, Result);
2408 end if;
2410 -- For a derived type, freeze its parent type first (RM 13.14(15))
2412 elsif Is_Derived_Type (E) then
2413 Freeze_And_Append (Etype (E), Loc, Result);
2414 Freeze_And_Append (First_Subtype (Etype (E)), Loc, Result);
2415 end if;
2417 -- For array type, freeze index types and component type first
2418 -- before freezing the array (RM 13.14(15)).
2420 if Is_Array_Type (E) then
2421 declare
2422 Ctyp : constant Entity_Id := Component_Type (E);
2423 Pnod : Node_Id;
2425 Non_Standard_Enum : Boolean := False;
2426 -- Set true if any of the index types is an enumeration
2427 -- type with a non-standard representation.
2429 begin
2430 Freeze_And_Append (Ctyp, Loc, Result);
2432 Indx := First_Index (E);
2433 while Present (Indx) loop
2434 Freeze_And_Append (Etype (Indx), Loc, Result);
2436 if Is_Enumeration_Type (Etype (Indx))
2437 and then Has_Non_Standard_Rep (Etype (Indx))
2438 then
2439 Non_Standard_Enum := True;
2440 end if;
2442 Next_Index (Indx);
2443 end loop;
2445 -- Processing that is done only for base types
2447 if Ekind (E) = E_Array_Type then
2449 -- Propagate flags for component type
2451 if Is_Controlled (Component_Type (E))
2452 or else Has_Controlled_Component (Ctyp)
2453 then
2454 Set_Has_Controlled_Component (E);
2455 end if;
2457 if Has_Unchecked_Union (Component_Type (E)) then
2458 Set_Has_Unchecked_Union (E);
2459 end if;
2461 -- If packing was requested or if the component size was set
2462 -- explicitly, then see if bit packing is required. This
2463 -- processing is only done for base types, since all the
2464 -- representation aspects involved are type-related. This
2465 -- is not just an optimization, if we start processing the
2466 -- subtypes, they intefere with the settings on the base
2467 -- type (this is because Is_Packed has a slightly different
2468 -- meaning before and after freezing).
2470 declare
2471 Csiz : Uint;
2472 Esiz : Uint;
2474 begin
2475 if (Is_Packed (E) or else Has_Pragma_Pack (E))
2476 and then not Has_Atomic_Components (E)
2477 and then Known_Static_RM_Size (Ctyp)
2478 then
2479 Csiz := UI_Max (RM_Size (Ctyp), 1);
2481 elsif Known_Component_Size (E) then
2482 Csiz := Component_Size (E);
2484 elsif not Known_Static_Esize (Ctyp) then
2485 Csiz := Uint_0;
2487 else
2488 Esiz := Esize (Ctyp);
2490 -- We can set the component size if it is less than
2491 -- 16, rounding it up to the next storage unit size.
2493 if Esiz <= 8 then
2494 Csiz := Uint_8;
2495 elsif Esiz <= 16 then
2496 Csiz := Uint_16;
2497 else
2498 Csiz := Uint_0;
2499 end if;
2501 -- Set component size up to match alignment if
2502 -- it would otherwise be less than the alignment.
2503 -- This deals with cases of types whose alignment
2504 -- exceeds their sizes (padded types).
2506 if Csiz /= 0 then
2507 declare
2508 A : constant Uint := Alignment_In_Bits (Ctyp);
2510 begin
2511 if Csiz < A then
2512 Csiz := A;
2513 end if;
2514 end;
2515 end if;
2517 end if;
2519 if 1 <= Csiz and then Csiz <= 64 then
2521 -- We set the component size for all cases 1-64
2523 Set_Component_Size (Base_Type (E), Csiz);
2525 -- Check for base type of 8,16,32 bits, where the
2526 -- subtype has a length one less than the base type
2527 -- and is unsigned (e.g. Natural subtype of Integer)
2529 -- In such cases, if a component size was not set
2530 -- explicitly, then generate a warning.
2532 if Has_Pragma_Pack (E)
2533 and then not Has_Component_Size_Clause (E)
2534 and then
2535 (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2536 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2537 then
2538 Error_Msg_Uint_1 := Csiz;
2539 Pnod :=
2540 Get_Rep_Pragma (First_Subtype (E), Name_Pack);
2542 if Present (Pnod) then
2543 Error_Msg_N
2544 ("pragma Pack causes component size to be ^?",
2545 Pnod);
2546 Error_Msg_N
2547 ("\use Component_Size to set desired value",
2548 Pnod);
2549 end if;
2550 end if;
2552 -- Actual packing is not needed for 8,16,32,64
2553 -- Also not needed for 24 if alignment is 1
2555 if Csiz = 8
2556 or else Csiz = 16
2557 or else Csiz = 32
2558 or else Csiz = 64
2559 or else (Csiz = 24 and then Alignment (Ctyp) = 1)
2560 then
2561 -- Here the array was requested to be packed, but
2562 -- the packing request had no effect, so Is_Packed
2563 -- is reset.
2565 -- Note: semantically this means that we lose
2566 -- track of the fact that a derived type inherited
2567 -- a pack pragma that was non-effective, but that
2568 -- seems fine.
2570 -- We regard a Pack pragma as a request to set a
2571 -- representation characteristic, and this request
2572 -- may be ignored.
2574 Set_Is_Packed (Base_Type (E), False);
2576 -- In all other cases, packing is indeed needed
2578 else
2579 Set_Has_Non_Standard_Rep (Base_Type (E));
2580 Set_Is_Bit_Packed_Array (Base_Type (E));
2581 Set_Is_Packed (Base_Type (E));
2582 end if;
2583 end if;
2584 end;
2586 -- Processing that is done only for subtypes
2588 else
2589 -- Acquire alignment from base type
2591 if Unknown_Alignment (E) then
2592 Set_Alignment (E, Alignment (Base_Type (E)));
2593 end if;
2594 end if;
2596 -- For bit-packed arrays, check the size
2598 if Is_Bit_Packed_Array (E)
2599 and then Known_Esize (E)
2600 then
2601 declare
2602 Discard : Boolean;
2603 SizC : constant Node_Id := Size_Clause (E);
2605 begin
2606 -- It is not clear if it is possible to have no size
2607 -- clause at this stage, but this is not worth worrying
2608 -- about. Post the error on the entity name in the size
2609 -- clause if present, else on the type entity itself.
2611 if Present (SizC) then
2612 Check_Size (Name (SizC), E, Esize (E), Discard);
2613 else
2614 Check_Size (E, E, Esize (E), Discard);
2615 end if;
2616 end;
2617 end if;
2619 -- Check one common case of a size given where the array
2620 -- needs to be packed, but was not so the size cannot be
2621 -- honored. This would of course be caught by the backend,
2622 -- and indeed we don't catch all cases. The point is that
2623 -- we can give a better error message in those cases that
2624 -- we do catch with the circuitry here.
2626 declare
2627 Lo, Hi : Node_Id;
2628 Ctyp : constant Entity_Id := Component_Type (E);
2630 begin
2631 if Present (Size_Clause (E))
2632 and then Known_Static_Esize (E)
2633 and then not Is_Bit_Packed_Array (E)
2634 and then not Has_Pragma_Pack (E)
2635 and then Number_Dimensions (E) = 1
2636 and then not Has_Component_Size_Clause (E)
2637 and then Known_Static_Esize (Ctyp)
2638 then
2639 Get_Index_Bounds (First_Index (E), Lo, Hi);
2641 if Compile_Time_Known_Value (Lo)
2642 and then Compile_Time_Known_Value (Hi)
2643 and then Known_Static_RM_Size (Ctyp)
2644 and then RM_Size (Ctyp) < 64
2645 then
2646 declare
2647 Lov : constant Uint := Expr_Value (Lo);
2648 Hiv : constant Uint := Expr_Value (Hi);
2649 Len : constant Uint :=
2650 UI_Max (Uint_0, Hiv - Lov + 1);
2651 Rsiz : constant Uint := RM_Size (Ctyp);
2653 -- What we are looking for here is the situation
2654 -- where the Esize given would be exactly right
2655 -- if there was a pragma Pack (resulting in the
2656 -- component size being the same as the RM_Size).
2657 -- Furthermore, the component type size must be
2658 -- an odd size (not a multiple of storage unit)
2660 begin
2661 if Esize (E) = Len * Rsiz
2662 and then Rsiz mod System_Storage_Unit /= 0
2663 then
2664 Error_Msg_NE
2665 ("size given for& too small",
2666 Size_Clause (E), E);
2667 Error_Msg_N
2668 ("\explicit pragma Pack is required",
2669 Size_Clause (E));
2670 end if;
2671 end;
2672 end if;
2673 end if;
2674 end;
2676 -- If any of the index types was an enumeration type with
2677 -- a non-standard rep clause, then we indicate that the
2678 -- array type is always packed (even if it is not bit packed).
2680 if Non_Standard_Enum then
2681 Set_Has_Non_Standard_Rep (Base_Type (E));
2682 Set_Is_Packed (Base_Type (E));
2683 end if;
2685 Set_Component_Alignment_If_Not_Set (E);
2687 -- If the array is packed, we must create the packed array
2688 -- type to be used to actually implement the type. This is
2689 -- only needed for real array types (not for string literal
2690 -- types, since they are present only for the front end).
2692 if Is_Packed (E)
2693 and then Ekind (E) /= E_String_Literal_Subtype
2694 then
2695 Create_Packed_Array_Type (E);
2696 Freeze_And_Append (Packed_Array_Type (E), Loc, Result);
2698 -- Size information of packed array type is copied to the
2699 -- array type, since this is really the representation.
2701 Set_Size_Info (E, Packed_Array_Type (E));
2702 Set_RM_Size (E, RM_Size (Packed_Array_Type (E)));
2703 end if;
2705 -- For non-packed arrays set the alignment of the array
2706 -- to the alignment of the component type if it is unknown.
2707 -- Skip this in the atomic case, since atomic arrays may
2708 -- need larger alignments.
2710 if not Is_Packed (E)
2711 and then Unknown_Alignment (E)
2712 and then Known_Alignment (Ctyp)
2713 and then Known_Static_Component_Size (E)
2714 and then Known_Static_Esize (Ctyp)
2715 and then Esize (Ctyp) = Component_Size (E)
2716 and then not Is_Atomic (E)
2717 then
2718 Set_Alignment (E, Alignment (Component_Type (E)));
2719 end if;
2720 end;
2722 -- For a class-wide type, the corresponding specific type is
2723 -- frozen as well (RM 13.14(15))
2725 elsif Is_Class_Wide_Type (E) then
2726 Freeze_And_Append (Root_Type (E), Loc, Result);
2728 -- If the Class_Wide_Type is an Itype (when type is the anonymous
2729 -- parent of a derived type) and it is a library-level entity,
2730 -- generate an itype reference for it. Otherwise, its first
2731 -- explicit reference may be in an inner scope, which will be
2732 -- rejected by the back-end.
2734 if Is_Itype (E)
2735 and then Is_Compilation_Unit (Scope (E))
2736 then
2737 declare
2738 Ref : constant Node_Id := Make_Itype_Reference (Loc);
2740 begin
2741 Set_Itype (Ref, E);
2742 if No (Result) then
2743 Result := New_List (Ref);
2744 else
2745 Append (Ref, Result);
2746 end if;
2747 end;
2748 end if;
2750 -- The equivalent type associated with a class-wide subtype
2751 -- needs to be frozen to ensure that its layout is done.
2752 -- Class-wide subtypes are currently only frozen on targets
2753 -- requiring front-end layout (see New_Class_Wide_Subtype
2754 -- and Make_CW_Equivalent_Type in exp_util.adb).
2756 if Ekind (E) = E_Class_Wide_Subtype
2757 and then Present (Equivalent_Type (E))
2758 then
2759 Freeze_And_Append (Equivalent_Type (E), Loc, Result);
2760 end if;
2762 -- For a record (sub)type, freeze all the component types (RM
2763 -- 13.14(15). We test for E_Record_(sub)Type here, rather than
2764 -- using Is_Record_Type, because we don't want to attempt the
2765 -- freeze for the case of a private type with record extension
2766 -- (we will do that later when the full type is frozen).
2768 elsif Ekind (E) = E_Record_Type
2769 or else Ekind (E) = E_Record_Subtype
2770 then
2771 Freeze_Record_Type (E);
2773 -- For a concurrent type, freeze corresponding record type. This
2774 -- does not correpond to any specific rule in the RM, but the
2775 -- record type is essentially part of the concurrent type.
2776 -- Freeze as well all local entities. This includes record types
2777 -- created for entry parameter blocks, and whatever local entities
2778 -- may appear in the private part.
2780 elsif Is_Concurrent_Type (E) then
2781 if Present (Corresponding_Record_Type (E)) then
2782 Freeze_And_Append
2783 (Corresponding_Record_Type (E), Loc, Result);
2784 end if;
2786 Comp := First_Entity (E);
2788 while Present (Comp) loop
2789 if Is_Type (Comp) then
2790 Freeze_And_Append (Comp, Loc, Result);
2792 elsif (Ekind (Comp)) /= E_Function then
2793 if Is_Itype (Etype (Comp))
2794 and then Underlying_Type (Scope (Etype (Comp))) = E
2795 then
2796 Undelay_Type (Etype (Comp));
2797 end if;
2799 Freeze_And_Append (Etype (Comp), Loc, Result);
2800 end if;
2802 Next_Entity (Comp);
2803 end loop;
2805 -- Private types are required to point to the same freeze node
2806 -- as their corresponding full views. The freeze node itself
2807 -- has to point to the partial view of the entity (because
2808 -- from the partial view, we can retrieve the full view, but
2809 -- not the reverse). However, in order to freeze correctly,
2810 -- we need to freeze the full view. If we are freezing at the
2811 -- end of a scope (or within the scope of the private type),
2812 -- the partial and full views will have been swapped, the
2813 -- full view appears first in the entity chain and the swapping
2814 -- mechanism ensures that the pointers are properly set (on
2815 -- scope exit).
2817 -- If we encounter the partial view before the full view
2818 -- (e.g. when freezing from another scope), we freeze the
2819 -- full view, and then set the pointers appropriately since
2820 -- we cannot rely on swapping to fix things up (subtypes in an
2821 -- outer scope might not get swapped).
2823 elsif Is_Incomplete_Or_Private_Type (E)
2824 and then not Is_Generic_Type (E)
2825 then
2826 -- Case of full view present
2828 if Present (Full_View (E)) then
2830 -- If full view has already been frozen, then no
2831 -- further processing is required
2833 if Is_Frozen (Full_View (E)) then
2835 Set_Has_Delayed_Freeze (E, False);
2836 Set_Freeze_Node (E, Empty);
2837 Check_Debug_Info_Needed (E);
2839 -- Otherwise freeze full view and patch the pointers
2840 -- so that the freeze node will elaborate both views
2841 -- in the back-end.
2843 else
2844 declare
2845 Full : constant Entity_Id := Full_View (E);
2847 begin
2848 if Is_Private_Type (Full)
2849 and then Present (Underlying_Full_View (Full))
2850 then
2851 Freeze_And_Append
2852 (Underlying_Full_View (Full), Loc, Result);
2853 end if;
2855 Freeze_And_Append (Full, Loc, Result);
2857 if Has_Delayed_Freeze (E) then
2858 F_Node := Freeze_Node (Full);
2860 if Present (F_Node) then
2861 Set_Freeze_Node (E, F_Node);
2862 Set_Entity (F_Node, E);
2864 else
2865 -- {Incomplete,Private}_Subtypes
2866 -- with Full_Views constrained by discriminants
2868 Set_Has_Delayed_Freeze (E, False);
2869 Set_Freeze_Node (E, Empty);
2870 end if;
2871 end if;
2872 end;
2874 Check_Debug_Info_Needed (E);
2875 end if;
2877 -- AI-117 requires that the convention of a partial view
2878 -- be the same as the convention of the full view. Note
2879 -- that this is a recognized breach of privacy, but it's
2880 -- essential for logical consistency of representation,
2881 -- and the lack of a rule in RM95 was an oversight.
2883 Set_Convention (E, Convention (Full_View (E)));
2885 Set_Size_Known_At_Compile_Time (E,
2886 Size_Known_At_Compile_Time (Full_View (E)));
2888 -- Size information is copied from the full view to the
2889 -- incomplete or private view for consistency
2891 -- We skip this is the full view is not a type. This is
2892 -- very strange of course, and can only happen as a result
2893 -- of certain illegalities, such as a premature attempt to
2894 -- derive from an incomplete type.
2896 if Is_Type (Full_View (E)) then
2897 Set_Size_Info (E, Full_View (E));
2898 Set_RM_Size (E, RM_Size (Full_View (E)));
2899 end if;
2901 return Result;
2903 -- Case of no full view present. If entity is derived or subtype,
2904 -- it is safe to freeze, correctness depends on the frozen status
2905 -- of parent. Otherwise it is either premature usage, or a Taft
2906 -- amendment type, so diagnosis is at the point of use and the
2907 -- type might be frozen later.
2909 elsif E /= Base_Type (E)
2910 or else Is_Derived_Type (E)
2911 then
2912 null;
2914 else
2915 Set_Is_Frozen (E, False);
2916 return No_List;
2917 end if;
2919 -- For access subprogram, freeze types of all formals, the return
2920 -- type was already frozen, since it is the Etype of the function.
2922 elsif Ekind (E) = E_Subprogram_Type then
2923 Formal := First_Formal (E);
2924 while Present (Formal) loop
2925 Freeze_And_Append (Etype (Formal), Loc, Result);
2926 Next_Formal (Formal);
2927 end loop;
2929 -- If the return type requires a transient scope, and we are on
2930 -- a target allowing functions to return with a depressed stack
2931 -- pointer, then we mark the function as requiring this treatment.
2933 if Functions_Return_By_DSP_On_Target
2934 and then Requires_Transient_Scope (Etype (E))
2935 then
2936 Set_Function_Returns_With_DSP (E);
2937 end if;
2939 Freeze_Subprogram (E);
2941 -- For access to a protected subprogram, freeze the equivalent
2942 -- type (however this is not set if we are not generating code)
2943 -- or if this is an anonymous type used just for resolution).
2945 elsif Ekind (E) = E_Access_Protected_Subprogram_Type
2946 and then Operating_Mode = Generate_Code
2947 and then Present (Equivalent_Type (E))
2948 then
2949 Freeze_And_Append (Equivalent_Type (E), Loc, Result);
2950 end if;
2952 -- Generic types are never seen by the back-end, and are also not
2953 -- processed by the expander (since the expander is turned off for
2954 -- generic processing), so we never need freeze nodes for them.
2956 if Is_Generic_Type (E) then
2957 return Result;
2958 end if;
2960 -- Some special processing for non-generic types to complete
2961 -- representation details not known till the freeze point.
2963 if Is_Fixed_Point_Type (E) then
2964 Freeze_Fixed_Point_Type (E);
2966 -- Some error checks required for ordinary fixed-point type.
2967 -- Defer these till the freeze-point since we need the small
2968 -- and range values. We only do these checks for base types
2970 if Is_Ordinary_Fixed_Point_Type (E)
2971 and then E = Base_Type (E)
2972 then
2973 if Small_Value (E) < Ureal_2_M_80 then
2974 Error_Msg_Name_1 := Name_Small;
2975 Error_Msg_N
2976 ("`&''%` is too small, minimum is 2.0'*'*(-80)", E);
2978 elsif Small_Value (E) > Ureal_2_80 then
2979 Error_Msg_Name_1 := Name_Small;
2980 Error_Msg_N
2981 ("`&''%` is too large, maximum is 2.0'*'*80", E);
2982 end if;
2984 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
2985 Error_Msg_Name_1 := Name_First;
2986 Error_Msg_N
2987 ("`&''%` is too small, minimum is -10.0'*'*36", E);
2988 end if;
2990 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
2991 Error_Msg_Name_1 := Name_Last;
2992 Error_Msg_N
2993 ("`&''%` is too large, maximum is 10.0'*'*36", E);
2994 end if;
2995 end if;
2997 elsif Is_Enumeration_Type (E) then
2998 Freeze_Enumeration_Type (E);
3000 elsif Is_Integer_Type (E) then
3001 Adjust_Esize_For_Alignment (E);
3003 elsif Is_Access_Type (E)
3004 and then No (Associated_Storage_Pool (E))
3005 then
3006 Check_Restriction (No_Standard_Storage_Pools, E);
3007 end if;
3009 if Is_Composite_Type (E) then
3011 -- AI-117 requires that all new primitives of a tagged type
3012 -- must inherit the convention of the full view of the type.
3013 -- Inherited and overriding operations are defined to inherit
3014 -- the convention of their parent or overridden subprogram
3015 -- (also specified in AI-117), and that will have occurred
3016 -- earlier (in Derive_Subprogram and New_Overloaded_Entity).
3017 -- Here we set the convention of primitives that are still
3018 -- convention Ada, which will ensure that any new primitives
3019 -- inherit the type's convention. Class-wide types can have
3020 -- a foreign convention inherited from their specific type,
3021 -- but are excluded from this since they don't have any
3022 -- associated primitives.
3024 if Is_Tagged_Type (E)
3025 and then not Is_Class_Wide_Type (E)
3026 and then Convention (E) /= Convention_Ada
3027 then
3028 declare
3029 Prim_List : constant Elist_Id := Primitive_Operations (E);
3030 Prim : Elmt_Id;
3031 begin
3032 Prim := First_Elmt (Prim_List);
3033 while Present (Prim) loop
3034 if Convention (Node (Prim)) = Convention_Ada then
3035 Set_Convention (Node (Prim), Convention (E));
3036 end if;
3038 Next_Elmt (Prim);
3039 end loop;
3040 end;
3041 end if;
3042 end if;
3044 -- Generate primitive operation references for a tagged type
3046 if Is_Tagged_Type (E)
3047 and then not Is_Class_Wide_Type (E)
3048 then
3049 declare
3050 Prim_List : constant Elist_Id := Primitive_Operations (E);
3051 Prim : Elmt_Id;
3052 Ent : Entity_Id;
3054 begin
3055 Prim := First_Elmt (Prim_List);
3056 while Present (Prim) loop
3057 Ent := Node (Prim);
3059 -- If the operation is derived, get the original for
3060 -- cross-reference purposes (it is the original for
3061 -- which we want the xref, and for which the comes
3062 -- from source test needs to be performed).
3064 while Present (Alias (Ent)) loop
3065 Ent := Alias (Ent);
3066 end loop;
3068 Generate_Reference (E, Ent, 'p', Set_Ref => False);
3069 Next_Elmt (Prim);
3070 end loop;
3072 -- If we get an exception, then something peculiar has happened
3073 -- probably as a result of a previous error. Since this is only
3074 -- for non-critical cross-references, ignore the error.
3076 exception
3077 when others => null;
3078 end;
3079 end if;
3081 -- Now that all types from which E may depend are frozen, see
3082 -- if the size is known at compile time, if it must be unsigned,
3083 -- or if strict alignent is required
3085 Check_Compile_Time_Size (E);
3086 Check_Unsigned_Type (E);
3088 if Base_Type (E) = E then
3089 Check_Strict_Alignment (E);
3090 end if;
3092 -- Do not allow a size clause for a type which does not have a size
3093 -- that is known at compile time
3095 if Has_Size_Clause (E)
3096 and then not Size_Known_At_Compile_Time (E)
3097 then
3098 -- Supress this message if errors posted on E, even if we are
3099 -- in all errors mode, since this is often a junk message
3101 if not Error_Posted (E) then
3102 Error_Msg_N
3103 ("size clause not allowed for variable length type",
3104 Size_Clause (E));
3105 end if;
3106 end if;
3108 -- Remaining process is to set/verify the representation information,
3109 -- in particular the size and alignment values. This processing is
3110 -- not required for generic types, since generic types do not play
3111 -- any part in code generation, and so the size and alignment values
3112 -- for such types are irrelevant.
3114 if Is_Generic_Type (E) then
3115 return Result;
3117 -- Otherwise we call the layout procedure
3119 else
3120 Layout_Type (E);
3121 end if;
3123 -- End of freeze processing for type entities
3124 end if;
3126 -- Here is where we logically freeze the current entity. If it has a
3127 -- freeze node, then this is the point at which the freeze node is
3128 -- linked into the result list.
3130 if Has_Delayed_Freeze (E) then
3132 -- If a freeze node is already allocated, use it, otherwise allocate
3133 -- a new one. The preallocation happens in the case of anonymous base
3134 -- types, where we preallocate so that we can set First_Subtype_Link.
3135 -- Note that we reset the Sloc to the current freeze location.
3137 if Present (Freeze_Node (E)) then
3138 F_Node := Freeze_Node (E);
3139 Set_Sloc (F_Node, Loc);
3141 else
3142 F_Node := New_Node (N_Freeze_Entity, Loc);
3143 Set_Freeze_Node (E, F_Node);
3144 Set_Access_Types_To_Process (F_Node, No_Elist);
3145 Set_TSS_Elist (F_Node, No_Elist);
3146 Set_Actions (F_Node, No_List);
3147 end if;
3149 Set_Entity (F_Node, E);
3151 if Result = No_List then
3152 Result := New_List (F_Node);
3153 else
3154 Append (F_Node, Result);
3155 end if;
3157 -- A final pass over record types with discriminants. If the type
3158 -- has an incomplete declaration, there may be constrained access
3159 -- subtypes declared elsewhere, which do not depend on the discrimi-
3160 -- nants of the type, and which are used as component types (i.e.
3161 -- the full view is a recursive type). The designated types of these
3162 -- subtypes can only be elaborated after the type itself, and they
3163 -- need an itype reference.
3165 if Ekind (E) = E_Record_Type
3166 and then Has_Discriminants (E)
3167 then
3168 declare
3169 Comp : Entity_Id;
3170 IR : Node_Id;
3171 Typ : Entity_Id;
3173 begin
3174 Comp := First_Component (E);
3176 while Present (Comp) loop
3177 Typ := Etype (Comp);
3179 if Ekind (Comp) = E_Component
3180 and then Is_Access_Type (Typ)
3181 and then Scope (Typ) /= E
3182 and then Base_Type (Designated_Type (Typ)) = E
3183 and then Is_Itype (Designated_Type (Typ))
3184 then
3185 IR := Make_Itype_Reference (Sloc (Comp));
3186 Set_Itype (IR, Designated_Type (Typ));
3187 Append (IR, Result);
3188 end if;
3190 Next_Component (Comp);
3191 end loop;
3192 end;
3193 end if;
3194 end if;
3196 -- When a type is frozen, the first subtype of the type is frozen as
3197 -- well (RM 13.14(15)). This has to be done after freezing the type,
3198 -- since obviously the first subtype depends on its own base type.
3200 if Is_Type (E) then
3201 Freeze_And_Append (First_Subtype (E), Loc, Result);
3203 -- If we just froze a tagged non-class wide record, then freeze the
3204 -- corresponding class-wide type. This must be done after the tagged
3205 -- type itself is frozen, because the class-wide type refers to the
3206 -- tagged type which generates the class.
3208 if Is_Tagged_Type (E)
3209 and then not Is_Class_Wide_Type (E)
3210 and then Present (Class_Wide_Type (E))
3211 then
3212 Freeze_And_Append (Class_Wide_Type (E), Loc, Result);
3213 end if;
3214 end if;
3216 Check_Debug_Info_Needed (E);
3218 -- Special handling for subprograms
3220 if Is_Subprogram (E) then
3222 -- If subprogram has address clause then reset Is_Public flag, since
3223 -- we do not want the backend to generate external references.
3225 if Present (Address_Clause (E))
3226 and then not Is_Library_Level_Entity (E)
3227 then
3228 Set_Is_Public (E, False);
3230 -- If no address clause and not intrinsic, then for imported
3231 -- subprogram in main unit, generate descriptor if we are in
3232 -- Propagate_Exceptions mode.
3234 elsif Propagate_Exceptions
3235 and then Is_Imported (E)
3236 and then not Is_Intrinsic_Subprogram (E)
3237 and then Convention (E) /= Convention_Stubbed
3238 then
3239 if Result = No_List then
3240 Result := Empty_List;
3241 end if;
3243 Generate_Subprogram_Descriptor_For_Imported_Subprogram
3244 (E, Result);
3245 end if;
3246 end if;
3248 return Result;
3249 end Freeze_Entity;
3251 -----------------------------
3252 -- Freeze_Enumeration_Type --
3253 -----------------------------
3255 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
3256 begin
3257 if Has_Foreign_Convention (Typ)
3258 and then not Has_Size_Clause (Typ)
3259 and then Esize (Typ) < Standard_Integer_Size
3260 then
3261 Init_Esize (Typ, Standard_Integer_Size);
3262 else
3263 Adjust_Esize_For_Alignment (Typ);
3264 end if;
3265 end Freeze_Enumeration_Type;
3267 -----------------------
3268 -- Freeze_Expression --
3269 -----------------------
3271 procedure Freeze_Expression (N : Node_Id) is
3272 In_Def_Exp : constant Boolean := In_Default_Expression;
3273 Typ : Entity_Id;
3274 Nam : Entity_Id;
3275 Desig_Typ : Entity_Id;
3276 P : Node_Id;
3277 Parent_P : Node_Id;
3279 Freeze_Outside : Boolean := False;
3280 -- This flag is set true if the entity must be frozen outside the
3281 -- current subprogram. This happens in the case of expander generated
3282 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
3283 -- not freeze all entities like other bodies, but which nevertheless
3284 -- may reference entities that have to be frozen before the body and
3285 -- obviously cannot be frozen inside the body.
3287 function In_Exp_Body (N : Node_Id) return Boolean;
3288 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
3289 -- it is the handled statement sequence of an expander generated
3290 -- subprogram (init proc, or stream subprogram). If so, it returns
3291 -- True, otherwise False.
3293 -----------------
3294 -- In_Exp_Body --
3295 -----------------
3297 function In_Exp_Body (N : Node_Id) return Boolean is
3298 P : Node_Id;
3300 begin
3301 if Nkind (N) = N_Subprogram_Body then
3302 P := N;
3303 else
3304 P := Parent (N);
3305 end if;
3307 if Nkind (P) /= N_Subprogram_Body then
3308 return False;
3310 else
3311 P := Defining_Unit_Name (Specification (P));
3313 if Nkind (P) = N_Defining_Identifier
3314 and then (Is_Init_Proc (P) or else
3315 Is_TSS (P, TSS_Stream_Input) or else
3316 Is_TSS (P, TSS_Stream_Output) or else
3317 Is_TSS (P, TSS_Stream_Read) or else
3318 Is_TSS (P, TSS_Stream_Write))
3319 then
3320 return True;
3321 else
3322 return False;
3323 end if;
3324 end if;
3325 end In_Exp_Body;
3327 -- Start of processing for Freeze_Expression
3329 begin
3330 -- Immediate return if freezing is inhibited. This flag is set by
3331 -- the analyzer to stop freezing on generated expressions that would
3332 -- cause freezing if they were in the source program, but which are
3333 -- not supposed to freeze, since they are created.
3335 if Must_Not_Freeze (N) then
3336 return;
3337 end if;
3339 -- If expression is non-static, then it does not freeze in a default
3340 -- expression, see section "Handling of Default Expressions" in the
3341 -- spec of package Sem for further details. Note that we have to
3342 -- make sure that we actually have a real expression (if we have
3343 -- a subtype indication, we can't test Is_Static_Expression!)
3345 if In_Def_Exp
3346 and then Nkind (N) in N_Subexpr
3347 and then not Is_Static_Expression (N)
3348 then
3349 return;
3350 end if;
3352 -- Freeze type of expression if not frozen already
3354 Typ := Empty;
3356 if Nkind (N) in N_Has_Etype then
3357 if not Is_Frozen (Etype (N)) then
3358 Typ := Etype (N);
3360 -- Base type may be an derived numeric type that is frozen at
3361 -- the point of declaration, but first_subtype is still unfrozen.
3363 elsif not Is_Frozen (First_Subtype (Etype (N))) then
3364 Typ := First_Subtype (Etype (N));
3365 end if;
3366 end if;
3368 -- For entity name, freeze entity if not frozen already. A special
3369 -- exception occurs for an identifier that did not come from source.
3370 -- We don't let such identifiers freeze a non-internal entity, i.e.
3371 -- an entity that did come from source, since such an identifier was
3372 -- generated by the expander, and cannot have any semantic effect on
3373 -- the freezing semantics. For example, this stops the parameter of
3374 -- an initialization procedure from freezing the variable.
3376 if Is_Entity_Name (N)
3377 and then not Is_Frozen (Entity (N))
3378 and then (Nkind (N) /= N_Identifier
3379 or else Comes_From_Source (N)
3380 or else not Comes_From_Source (Entity (N)))
3381 then
3382 Nam := Entity (N);
3383 else
3384 Nam := Empty;
3385 end if;
3387 -- For an allocator freeze designated type if not frozen already.
3389 -- For an aggregate whose component type is an access type, freeze
3390 -- the designated type now, so that its freeze does not appear within
3391 -- the loop that might be created in the expansion of the aggregate.
3392 -- If the designated type is a private type without full view, the
3393 -- expression cannot contain an allocator, so the type is not frozen.
3395 Desig_Typ := Empty;
3397 case Nkind (N) is
3398 when N_Allocator =>
3399 Desig_Typ := Designated_Type (Etype (N));
3401 when N_Aggregate =>
3402 if Is_Array_Type (Etype (N))
3403 and then Is_Access_Type (Component_Type (Etype (N)))
3404 then
3405 Desig_Typ := Designated_Type (Component_Type (Etype (N)));
3406 end if;
3408 when N_Selected_Component |
3409 N_Indexed_Component |
3410 N_Slice =>
3412 if Is_Access_Type (Etype (Prefix (N))) then
3413 Desig_Typ := Designated_Type (Etype (Prefix (N)));
3414 end if;
3416 when others =>
3417 null;
3418 end case;
3420 if Desig_Typ /= Empty
3421 and then (Is_Frozen (Desig_Typ)
3422 or else (not Is_Fully_Defined (Desig_Typ)))
3423 then
3424 Desig_Typ := Empty;
3425 end if;
3427 -- All done if nothing needs freezing
3429 if No (Typ)
3430 and then No (Nam)
3431 and then No (Desig_Typ)
3432 then
3433 return;
3434 end if;
3436 -- Loop for looking at the right place to insert the freeze nodes
3437 -- exiting from the loop when it is appropriate to insert the freeze
3438 -- node before the current node P.
3440 -- Also checks some special exceptions to the freezing rules. These
3441 -- cases result in a direct return, bypassing the freeze action.
3443 P := N;
3444 loop
3445 Parent_P := Parent (P);
3447 -- If we don't have a parent, then we are not in a well-formed
3448 -- tree. This is an unusual case, but there are some legitimate
3449 -- situations in which this occurs, notably when the expressions
3450 -- in the range of a type declaration are resolved. We simply
3451 -- ignore the freeze request in this case. Is this right ???
3453 if No (Parent_P) then
3454 return;
3455 end if;
3457 -- See if we have got to an appropriate point in the tree
3459 case Nkind (Parent_P) is
3461 -- A special test for the exception of (RM 13.14(8)) for the
3462 -- case of per-object expressions (RM 3.8(18)) occurring in a
3463 -- component definition or a discrete subtype definition. Note
3464 -- that we test for a component declaration which includes both
3465 -- cases we are interested in, and furthermore the tree does not
3466 -- have explicit nodes for either of these two constructs.
3468 when N_Component_Declaration =>
3470 -- The case we want to test for here is an identifier that is
3471 -- a per-object expression, this is either a discriminant that
3472 -- appears in a context other than the component declaration
3473 -- or it is a reference to the type of the enclosing construct.
3475 -- For either of these cases, we skip the freezing
3477 if not In_Default_Expression
3478 and then Nkind (N) = N_Identifier
3479 and then (Present (Entity (N)))
3480 then
3481 -- We recognize the discriminant case by just looking for
3482 -- a reference to a discriminant. It can only be one for
3483 -- the enclosing construct. Skip freezing in this case.
3485 if Ekind (Entity (N)) = E_Discriminant then
3486 return;
3488 -- For the case of a reference to the enclosing record,
3489 -- (or task or protected type), we look for a type that
3490 -- matches the current scope.
3492 elsif Entity (N) = Current_Scope then
3493 return;
3494 end if;
3495 end if;
3497 -- If we have an enumeration literal that appears as the
3498 -- choice in the aggregate of an enumeration representation
3499 -- clause, then freezing does not occur (RM 13.14(10)).
3501 when N_Enumeration_Representation_Clause =>
3503 -- The case we are looking for is an enumeration literal
3505 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
3506 and then Is_Enumeration_Type (Etype (N))
3507 then
3508 -- If enumeration literal appears directly as the choice,
3509 -- do not freeze (this is the normal non-overloade case)
3511 if Nkind (Parent (N)) = N_Component_Association
3512 and then First (Choices (Parent (N))) = N
3513 then
3514 return;
3516 -- If enumeration literal appears as the name of a
3517 -- function which is the choice, then also do not freeze.
3518 -- This happens in the overloaded literal case, where the
3519 -- enumeration literal is temporarily changed to a function
3520 -- call for overloading analysis purposes.
3522 elsif Nkind (Parent (N)) = N_Function_Call
3523 and then
3524 Nkind (Parent (Parent (N))) = N_Component_Association
3525 and then
3526 First (Choices (Parent (Parent (N)))) = Parent (N)
3527 then
3528 return;
3529 end if;
3530 end if;
3532 -- Normally if the parent is a handled sequence of statements,
3533 -- then the current node must be a statement, and that is an
3534 -- appropriate place to insert a freeze node.
3536 when N_Handled_Sequence_Of_Statements =>
3538 -- An exception occurs when the sequence of statements is
3539 -- for an expander generated body that did not do the usual
3540 -- freeze all operation. In this case we usually want to
3541 -- freeze outside this body, not inside it, and we skip
3542 -- past the subprogram body that we are inside.
3544 if In_Exp_Body (Parent_P) then
3546 -- However, we *do* want to freeze at this point if we have
3547 -- an entity to freeze, and that entity is declared *inside*
3548 -- the body of the expander generated procedure. This case
3549 -- is recognized by the scope of the type, which is either
3550 -- the spec for some enclosing body, or (in the case of
3551 -- init_procs, for which there are no separate specs) the
3552 -- current scope.
3554 declare
3555 Subp : constant Node_Id := Parent (Parent_P);
3556 Cspc : Entity_Id;
3558 begin
3559 if Nkind (Subp) = N_Subprogram_Body then
3560 Cspc := Corresponding_Spec (Subp);
3562 if (Present (Typ) and then Scope (Typ) = Cspc)
3563 or else
3564 (Present (Nam) and then Scope (Nam) = Cspc)
3565 then
3566 exit;
3568 elsif Present (Typ)
3569 and then Scope (Typ) = Current_Scope
3570 and then Current_Scope = Defining_Entity (Subp)
3571 then
3572 exit;
3573 end if;
3574 end if;
3575 end;
3577 -- If not that exception to the exception, then this is
3578 -- where we delay the freeze till outside the body.
3580 Parent_P := Parent (Parent_P);
3581 Freeze_Outside := True;
3583 -- Here if normal case where we are in handled statement
3584 -- sequence and want to do the insertion right there.
3586 else
3587 exit;
3588 end if;
3590 -- If parent is a body or a spec or a block, then the current
3591 -- node is a statement or declaration and we can insert the
3592 -- freeze node before it.
3594 when N_Package_Specification |
3595 N_Package_Body |
3596 N_Subprogram_Body |
3597 N_Task_Body |
3598 N_Protected_Body |
3599 N_Entry_Body |
3600 N_Block_Statement => exit;
3602 -- The expander is allowed to define types in any statements list,
3603 -- so any of the following parent nodes also mark a freezing point
3604 -- if the actual node is in a list of statements or declarations.
3606 when N_Exception_Handler |
3607 N_If_Statement |
3608 N_Elsif_Part |
3609 N_Case_Statement_Alternative |
3610 N_Compilation_Unit_Aux |
3611 N_Selective_Accept |
3612 N_Accept_Alternative |
3613 N_Delay_Alternative |
3614 N_Conditional_Entry_Call |
3615 N_Entry_Call_Alternative |
3616 N_Triggering_Alternative |
3617 N_Abortable_Part |
3618 N_Freeze_Entity =>
3620 exit when Is_List_Member (P);
3622 -- Note: The N_Loop_Statement is a special case. A type that
3623 -- appears in the source can never be frozen in a loop (this
3624 -- occurs only because of a loop expanded by the expander),
3625 -- so we keep on going. Otherwise we terminate the search.
3626 -- Same is true of any entity which comes from source. (if they
3627 -- have a predefined type, that type does not appear to come
3628 -- from source, but the entity should not be frozen here).
3630 when N_Loop_Statement =>
3631 exit when not Comes_From_Source (Etype (N))
3632 and then (No (Nam) or else not Comes_From_Source (Nam));
3634 -- For all other cases, keep looking at parents
3636 when others =>
3637 null;
3638 end case;
3640 -- We fall through the case if we did not yet find the proper
3641 -- place in the free for inserting the freeze node, so climb!
3643 P := Parent_P;
3644 end loop;
3646 -- If the expression appears in a record or an initialization
3647 -- procedure, the freeze nodes are collected and attached to
3648 -- the current scope, to be inserted and analyzed on exit from
3649 -- the scope, to insure that generated entities appear in the
3650 -- correct scope. If the expression is a default for a discriminant
3651 -- specification, the scope is still void. The expression can also
3652 -- appear in the discriminant part of a private or concurrent type.
3654 -- The other case requiring this special handling is if we are in
3655 -- a default expression, since in that case we are about to freeze
3656 -- a static type, and the freeze scope needs to be the outer scope,
3657 -- not the scope of the subprogram with the default parameter.
3659 -- For default expressions in generic units, the Move_Freeze_Nodes
3660 -- mechanism (see sem_ch12.adb) takes care of placing them at the
3661 -- proper place, after the generic unit.
3663 if (In_Def_Exp and not Inside_A_Generic)
3664 or else Freeze_Outside
3665 or else (Is_Type (Current_Scope)
3666 and then (not Is_Concurrent_Type (Current_Scope)
3667 or else not Has_Completion (Current_Scope)))
3668 or else Ekind (Current_Scope) = E_Void
3669 then
3670 declare
3671 Loc : constant Source_Ptr := Sloc (Current_Scope);
3672 Freeze_Nodes : List_Id := No_List;
3674 begin
3675 if Present (Desig_Typ) then
3676 Freeze_And_Append (Desig_Typ, Loc, Freeze_Nodes);
3677 end if;
3679 if Present (Typ) then
3680 Freeze_And_Append (Typ, Loc, Freeze_Nodes);
3681 end if;
3683 if Present (Nam) then
3684 Freeze_And_Append (Nam, Loc, Freeze_Nodes);
3685 end if;
3687 if Is_Non_Empty_List (Freeze_Nodes) then
3688 if No (Scope_Stack.Table
3689 (Scope_Stack.Last).Pending_Freeze_Actions)
3690 then
3691 Scope_Stack.Table
3692 (Scope_Stack.Last).Pending_Freeze_Actions :=
3693 Freeze_Nodes;
3694 else
3695 Append_List (Freeze_Nodes, Scope_Stack.Table
3696 (Scope_Stack.Last).Pending_Freeze_Actions);
3697 end if;
3698 end if;
3699 end;
3701 return;
3702 end if;
3704 -- Now we have the right place to do the freezing. First, a special
3705 -- adjustment, if we are in default expression analysis mode, these
3706 -- freeze actions must not be thrown away (normally all inserted
3707 -- actions are thrown away in this mode. However, the freeze actions
3708 -- are from static expressions and one of the important reasons we
3709 -- are doing this special analysis is to get these freeze actions.
3710 -- Therefore we turn off the In_Default_Expression mode to propagate
3711 -- these freeze actions. This also means they get properly analyzed
3712 -- and expanded.
3714 In_Default_Expression := False;
3716 -- Freeze the designated type of an allocator (RM 13.14(13))
3718 if Present (Desig_Typ) then
3719 Freeze_Before (P, Desig_Typ);
3720 end if;
3722 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
3723 -- the enumeration representation clause exception in the loop above.
3725 if Present (Typ) then
3726 Freeze_Before (P, Typ);
3727 end if;
3729 -- Freeze name if one is present (RM 13.14(11))
3731 if Present (Nam) then
3732 Freeze_Before (P, Nam);
3733 end if;
3735 In_Default_Expression := In_Def_Exp;
3736 end Freeze_Expression;
3738 -----------------------------
3739 -- Freeze_Fixed_Point_Type --
3740 -----------------------------
3742 -- Certain fixed-point types and subtypes, including implicit base
3743 -- types and declared first subtypes, have not yet set up a range.
3744 -- This is because the range cannot be set until the Small and Size
3745 -- values are known, and these are not known till the type is frozen.
3747 -- To signal this case, Scalar_Range contains an unanalyzed syntactic
3748 -- range whose bounds are unanalyzed real literals. This routine will
3749 -- recognize this case, and transform this range node into a properly
3750 -- typed range with properly analyzed and resolved values.
3752 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
3753 Rng : constant Node_Id := Scalar_Range (Typ);
3754 Lo : constant Node_Id := Low_Bound (Rng);
3755 Hi : constant Node_Id := High_Bound (Rng);
3756 Btyp : constant Entity_Id := Base_Type (Typ);
3757 Brng : constant Node_Id := Scalar_Range (Btyp);
3758 BLo : constant Node_Id := Low_Bound (Brng);
3759 BHi : constant Node_Id := High_Bound (Brng);
3760 Small : constant Ureal := Small_Value (Typ);
3761 Loval : Ureal;
3762 Hival : Ureal;
3763 Atype : Entity_Id;
3765 Actual_Size : Nat;
3767 function Fsize (Lov, Hiv : Ureal) return Nat;
3768 -- Returns size of type with given bounds. Also leaves these
3769 -- bounds set as the current bounds of the Typ.
3771 -----------
3772 -- Fsize --
3773 -----------
3775 function Fsize (Lov, Hiv : Ureal) return Nat is
3776 begin
3777 Set_Realval (Lo, Lov);
3778 Set_Realval (Hi, Hiv);
3779 return Minimum_Size (Typ);
3780 end Fsize;
3782 -- Start of processing for Freeze_Fixed_Point_Type
3784 begin
3785 -- If Esize of a subtype has not previously been set, set it now
3787 if Unknown_Esize (Typ) then
3788 Atype := Ancestor_Subtype (Typ);
3790 if Present (Atype) then
3791 Set_Esize (Typ, Esize (Atype));
3792 else
3793 Set_Esize (Typ, Esize (Base_Type (Typ)));
3794 end if;
3795 end if;
3797 -- Immediate return if the range is already analyzed. This means
3798 -- that the range is already set, and does not need to be computed
3799 -- by this routine.
3801 if Analyzed (Rng) then
3802 return;
3803 end if;
3805 -- Immediate return if either of the bounds raises Constraint_Error
3807 if Raises_Constraint_Error (Lo)
3808 or else Raises_Constraint_Error (Hi)
3809 then
3810 return;
3811 end if;
3813 Loval := Realval (Lo);
3814 Hival := Realval (Hi);
3816 -- Ordinary fixed-point case
3818 if Is_Ordinary_Fixed_Point_Type (Typ) then
3820 -- For the ordinary fixed-point case, we are allowed to fudge the
3821 -- end-points up or down by small. Generally we prefer to fudge
3822 -- up, i.e. widen the bounds for non-model numbers so that the
3823 -- end points are included. However there are cases in which this
3824 -- cannot be done, and indeed cases in which we may need to narrow
3825 -- the bounds. The following circuit makes the decision.
3827 -- Note: our terminology here is that Incl_EP means that the
3828 -- bounds are widened by Small if necessary to include the end
3829 -- points, and Excl_EP means that the bounds are narrowed by
3830 -- Small to exclude the end-points if this reduces the size.
3832 -- Note that in the Incl case, all we care about is including the
3833 -- end-points. In the Excl case, we want to narrow the bounds as
3834 -- much as permitted by the RM, to give the smallest possible size.
3836 Fudge : declare
3837 Loval_Incl_EP : Ureal;
3838 Hival_Incl_EP : Ureal;
3840 Loval_Excl_EP : Ureal;
3841 Hival_Excl_EP : Ureal;
3843 Size_Incl_EP : Nat;
3844 Size_Excl_EP : Nat;
3846 Model_Num : Ureal;
3847 First_Subt : Entity_Id;
3848 Actual_Lo : Ureal;
3849 Actual_Hi : Ureal;
3851 begin
3852 -- First step. Base types are required to be symmetrical. Right
3853 -- now, the base type range is a copy of the first subtype range.
3854 -- This will be corrected before we are done, but right away we
3855 -- need to deal with the case where both bounds are non-negative.
3856 -- In this case, we set the low bound to the negative of the high
3857 -- bound, to make sure that the size is computed to include the
3858 -- required sign. Note that we do not need to worry about the
3859 -- case of both bounds negative, because the sign will be dealt
3860 -- with anyway. Furthermore we can't just go making such a bound
3861 -- symmetrical, since in a twos-complement system, there is an
3862 -- extra negative value which could not be accomodated on the
3863 -- positive side.
3865 if Typ = Btyp
3866 and then not UR_Is_Negative (Loval)
3867 and then Hival > Loval
3868 then
3869 Loval := -Hival;
3870 Set_Realval (Lo, Loval);
3871 end if;
3873 -- Compute the fudged bounds. If the number is a model number,
3874 -- then we do nothing to include it, but we are allowed to
3875 -- backoff to the next adjacent model number when we exclude
3876 -- it. If it is not a model number then we straddle the two
3877 -- values with the model numbers on either side.
3879 Model_Num := UR_Trunc (Loval / Small) * Small;
3881 if Loval = Model_Num then
3882 Loval_Incl_EP := Model_Num;
3883 else
3884 Loval_Incl_EP := Model_Num - Small;
3885 end if;
3887 -- The low value excluding the end point is Small greater, but
3888 -- we do not do this exclusion if the low value is positive,
3889 -- since it can't help the size and could actually hurt by
3890 -- crossing the high bound.
3892 if UR_Is_Negative (Loval_Incl_EP) then
3893 Loval_Excl_EP := Loval_Incl_EP + Small;
3894 else
3895 Loval_Excl_EP := Loval_Incl_EP;
3896 end if;
3898 -- Similar processing for upper bound and high value
3900 Model_Num := UR_Trunc (Hival / Small) * Small;
3902 if Hival = Model_Num then
3903 Hival_Incl_EP := Model_Num;
3904 else
3905 Hival_Incl_EP := Model_Num + Small;
3906 end if;
3908 if UR_Is_Positive (Hival_Incl_EP) then
3909 Hival_Excl_EP := Hival_Incl_EP - Small;
3910 else
3911 Hival_Excl_EP := Hival_Incl_EP;
3912 end if;
3914 -- One further adjustment is needed. In the case of subtypes,
3915 -- we cannot go outside the range of the base type, or we get
3916 -- peculiarities, and the base type range is already set. This
3917 -- only applies to the Incl values, since clearly the Excl
3918 -- values are already as restricted as they are allowed to be.
3920 if Typ /= Btyp then
3921 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
3922 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
3923 end if;
3925 -- Get size including and excluding end points
3927 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
3928 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
3930 -- No need to exclude end-points if it does not reduce size
3932 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
3933 Loval_Excl_EP := Loval_Incl_EP;
3934 end if;
3936 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
3937 Hival_Excl_EP := Hival_Incl_EP;
3938 end if;
3940 -- Now we set the actual size to be used. We want to use the
3941 -- bounds fudged up to include the end-points but only if this
3942 -- can be done without violating a specifically given size
3943 -- size clause or causing an unacceptable increase in size.
3945 -- Case of size clause given
3947 if Has_Size_Clause (Typ) then
3949 -- Use the inclusive size only if it is consistent with
3950 -- the explicitly specified size.
3952 if Size_Incl_EP <= RM_Size (Typ) then
3953 Actual_Lo := Loval_Incl_EP;
3954 Actual_Hi := Hival_Incl_EP;
3955 Actual_Size := Size_Incl_EP;
3957 -- If the inclusive size is too large, we try excluding
3958 -- the end-points (will be caught later if does not work).
3960 else
3961 Actual_Lo := Loval_Excl_EP;
3962 Actual_Hi := Hival_Excl_EP;
3963 Actual_Size := Size_Excl_EP;
3964 end if;
3966 -- Case of size clause not given
3968 else
3969 -- If we have a base type whose corresponding first subtype
3970 -- has an explicit size that is large enough to include our
3971 -- end-points, then do so. There is no point in working hard
3972 -- to get a base type whose size is smaller than the specified
3973 -- size of the first subtype.
3975 First_Subt := First_Subtype (Typ);
3977 if Has_Size_Clause (First_Subt)
3978 and then Size_Incl_EP <= Esize (First_Subt)
3979 then
3980 Actual_Size := Size_Incl_EP;
3981 Actual_Lo := Loval_Incl_EP;
3982 Actual_Hi := Hival_Incl_EP;
3984 -- If excluding the end-points makes the size smaller and
3985 -- results in a size of 8,16,32,64, then we take the smaller
3986 -- size. For the 64 case, this is compulsory. For the other
3987 -- cases, it seems reasonable. We like to include end points
3988 -- if we can, but not at the expense of moving to the next
3989 -- natural boundary of size.
3991 elsif Size_Incl_EP /= Size_Excl_EP
3992 and then
3993 (Size_Excl_EP = 8 or else
3994 Size_Excl_EP = 16 or else
3995 Size_Excl_EP = 32 or else
3996 Size_Excl_EP = 64)
3997 then
3998 Actual_Size := Size_Excl_EP;
3999 Actual_Lo := Loval_Excl_EP;
4000 Actual_Hi := Hival_Excl_EP;
4002 -- Otherwise we can definitely include the end points
4004 else
4005 Actual_Size := Size_Incl_EP;
4006 Actual_Lo := Loval_Incl_EP;
4007 Actual_Hi := Hival_Incl_EP;
4008 end if;
4010 -- One pathological case: normally we never fudge a low
4011 -- bound down, since it would seem to increase the size
4012 -- (if it has any effect), but for ranges containing a
4013 -- single value, or no values, the high bound can be
4014 -- small too large. Consider:
4016 -- type t is delta 2.0**(-14)
4017 -- range 131072.0 .. 0;
4019 -- That lower bound is *just* outside the range of 32
4020 -- bits, and does need fudging down in this case. Note
4021 -- that the bounds will always have crossed here, since
4022 -- the high bound will be fudged down if necessary, as
4023 -- in the case of:
4025 -- type t is delta 2.0**(-14)
4026 -- range 131072.0 .. 131072.0;
4028 -- So we can detect the situation by looking for crossed
4029 -- bounds, and if the bounds are crossed, and the low
4030 -- bound is greater than zero, we will always back it
4031 -- off by small, since this is completely harmless.
4033 if Actual_Lo > Actual_Hi then
4034 if UR_Is_Positive (Actual_Lo) then
4035 Actual_Lo := Loval_Incl_EP - Small;
4036 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
4038 -- And of course, we need to do exactly the same parallel
4039 -- fudge for flat ranges in the negative region.
4041 elsif UR_Is_Negative (Actual_Hi) then
4042 Actual_Hi := Hival_Incl_EP + Small;
4043 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
4044 end if;
4045 end if;
4046 end if;
4048 Set_Realval (Lo, Actual_Lo);
4049 Set_Realval (Hi, Actual_Hi);
4050 end Fudge;
4052 -- For the decimal case, none of this fudging is required, since there
4053 -- are no end-point problems in the decimal case (the end-points are
4054 -- always included).
4056 else
4057 Actual_Size := Fsize (Loval, Hival);
4058 end if;
4060 -- At this stage, the actual size has been calculated and the proper
4061 -- required bounds are stored in the low and high bounds.
4063 if Actual_Size > 64 then
4064 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
4065 Error_Msg_N
4066 ("size required (^) for type& too large, maximum is 64", Typ);
4067 Actual_Size := 64;
4068 end if;
4070 -- Check size against explicit given size
4072 if Has_Size_Clause (Typ) then
4073 if Actual_Size > RM_Size (Typ) then
4074 Error_Msg_Uint_1 := RM_Size (Typ);
4075 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
4076 Error_Msg_NE
4077 ("size given (^) for type& too small, minimum is ^",
4078 Size_Clause (Typ), Typ);
4080 else
4081 Actual_Size := UI_To_Int (Esize (Typ));
4082 end if;
4084 -- Increase size to next natural boundary if no size clause given
4086 else
4087 if Actual_Size <= 8 then
4088 Actual_Size := 8;
4089 elsif Actual_Size <= 16 then
4090 Actual_Size := 16;
4091 elsif Actual_Size <= 32 then
4092 Actual_Size := 32;
4093 else
4094 Actual_Size := 64;
4095 end if;
4097 Init_Esize (Typ, Actual_Size);
4098 Adjust_Esize_For_Alignment (Typ);
4099 end if;
4101 -- If we have a base type, then expand the bounds so that they
4102 -- extend to the full width of the allocated size in bits, to
4103 -- avoid junk range checks on intermediate computations.
4105 if Base_Type (Typ) = Typ then
4106 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
4107 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
4108 end if;
4110 -- Final step is to reanalyze the bounds using the proper type
4111 -- and set the Corresponding_Integer_Value fields of the literals.
4113 Set_Etype (Lo, Empty);
4114 Set_Analyzed (Lo, False);
4115 Analyze (Lo);
4117 -- Resolve with universal fixed if the base type, and the base
4118 -- type if it is a subtype. Note we can't resolve the base type
4119 -- with itself, that would be a reference before definition.
4121 if Typ = Btyp then
4122 Resolve (Lo, Universal_Fixed);
4123 else
4124 Resolve (Lo, Btyp);
4125 end if;
4127 -- Set corresponding integer value for bound
4129 Set_Corresponding_Integer_Value
4130 (Lo, UR_To_Uint (Realval (Lo) / Small));
4132 -- Similar processing for high bound
4134 Set_Etype (Hi, Empty);
4135 Set_Analyzed (Hi, False);
4136 Analyze (Hi);
4138 if Typ = Btyp then
4139 Resolve (Hi, Universal_Fixed);
4140 else
4141 Resolve (Hi, Btyp);
4142 end if;
4144 Set_Corresponding_Integer_Value
4145 (Hi, UR_To_Uint (Realval (Hi) / Small));
4147 -- Set type of range to correspond to bounds
4149 Set_Etype (Rng, Etype (Lo));
4151 -- Set Esize to calculated size if not set already
4153 if Unknown_Esize (Typ) then
4154 Init_Esize (Typ, Actual_Size);
4155 end if;
4157 -- Set RM_Size if not already set. If already set, check value
4159 declare
4160 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
4162 begin
4163 if RM_Size (Typ) /= Uint_0 then
4164 if RM_Size (Typ) < Minsiz then
4165 Error_Msg_Uint_1 := RM_Size (Typ);
4166 Error_Msg_Uint_2 := Minsiz;
4167 Error_Msg_NE
4168 ("size given (^) for type& too small, minimum is ^",
4169 Size_Clause (Typ), Typ);
4170 end if;
4172 else
4173 Set_RM_Size (Typ, Minsiz);
4174 end if;
4175 end;
4176 end Freeze_Fixed_Point_Type;
4178 ------------------
4179 -- Freeze_Itype --
4180 ------------------
4182 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
4183 L : List_Id;
4185 begin
4186 Set_Has_Delayed_Freeze (T);
4187 L := Freeze_Entity (T, Sloc (N));
4189 if Is_Non_Empty_List (L) then
4190 Insert_Actions (N, L);
4191 end if;
4192 end Freeze_Itype;
4194 --------------------------
4195 -- Freeze_Static_Object --
4196 --------------------------
4198 procedure Freeze_Static_Object (E : Entity_Id) is
4200 Cannot_Be_Static : exception;
4201 -- Exception raised if the type of a static object cannot be made
4202 -- static. This happens if the type depends on non-global objects.
4204 procedure Ensure_Expression_Is_SA (N : Node_Id);
4205 -- Called to ensure that an expression used as part of a type
4206 -- definition is statically allocatable, which means that the type
4207 -- of the expression is statically allocatable, and the expression
4208 -- is either static, or a reference to a library level constant.
4210 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
4211 -- Called to mark a type as static, checking that it is possible
4212 -- to set the type as static. If it is not possible, then the
4213 -- exception Cannot_Be_Static is raised.
4215 -----------------------------
4216 -- Ensure_Expression_Is_SA --
4217 -----------------------------
4219 procedure Ensure_Expression_Is_SA (N : Node_Id) is
4220 Ent : Entity_Id;
4222 begin
4223 Ensure_Type_Is_SA (Etype (N));
4225 if Is_Static_Expression (N) then
4226 return;
4228 elsif Nkind (N) = N_Identifier then
4229 Ent := Entity (N);
4231 if Present (Ent)
4232 and then Ekind (Ent) = E_Constant
4233 and then Is_Library_Level_Entity (Ent)
4234 then
4235 return;
4236 end if;
4237 end if;
4239 raise Cannot_Be_Static;
4240 end Ensure_Expression_Is_SA;
4242 -----------------------
4243 -- Ensure_Type_Is_SA --
4244 -----------------------
4246 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
4247 N : Node_Id;
4248 C : Entity_Id;
4250 begin
4251 -- If type is library level, we are all set
4253 if Is_Library_Level_Entity (Typ) then
4254 return;
4255 end if;
4257 -- We are also OK if the type is already marked as statically
4258 -- allocated, which means we processed it before.
4260 if Is_Statically_Allocated (Typ) then
4261 return;
4262 end if;
4264 -- Mark type as statically allocated
4266 Set_Is_Statically_Allocated (Typ);
4268 -- Check that it is safe to statically allocate this type
4270 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
4271 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
4272 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
4274 elsif Is_Array_Type (Typ) then
4275 N := First_Index (Typ);
4276 while Present (N) loop
4277 Ensure_Type_Is_SA (Etype (N));
4278 Next_Index (N);
4279 end loop;
4281 Ensure_Type_Is_SA (Component_Type (Typ));
4283 elsif Is_Access_Type (Typ) then
4284 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
4286 declare
4287 F : Entity_Id;
4288 T : constant Entity_Id := Etype (Designated_Type (Typ));
4290 begin
4291 if T /= Standard_Void_Type then
4292 Ensure_Type_Is_SA (T);
4293 end if;
4295 F := First_Formal (Designated_Type (Typ));
4297 while Present (F) loop
4298 Ensure_Type_Is_SA (Etype (F));
4299 Next_Formal (F);
4300 end loop;
4301 end;
4303 else
4304 Ensure_Type_Is_SA (Designated_Type (Typ));
4305 end if;
4307 elsif Is_Record_Type (Typ) then
4308 C := First_Entity (Typ);
4310 while Present (C) loop
4311 if Ekind (C) = E_Discriminant
4312 or else Ekind (C) = E_Component
4313 then
4314 Ensure_Type_Is_SA (Etype (C));
4316 elsif Is_Type (C) then
4317 Ensure_Type_Is_SA (C);
4318 end if;
4320 Next_Entity (C);
4321 end loop;
4323 elsif Ekind (Typ) = E_Subprogram_Type then
4324 Ensure_Type_Is_SA (Etype (Typ));
4326 C := First_Formal (Typ);
4327 while Present (C) loop
4328 Ensure_Type_Is_SA (Etype (C));
4329 Next_Formal (C);
4330 end loop;
4332 else
4333 raise Cannot_Be_Static;
4334 end if;
4335 end Ensure_Type_Is_SA;
4337 -- Start of processing for Freeze_Static_Object
4339 begin
4340 Ensure_Type_Is_SA (Etype (E));
4342 -- Reset True_Constant flag, since something strange is going on
4343 -- with the scoping here, and our simple value tracing may not
4344 -- be sufficient for this indication to be reliable. We kill the
4345 -- Constant_Value indication for the same reason.
4347 Set_Is_True_Constant (E, False);
4348 Set_Current_Value (E, Empty);
4350 exception
4351 when Cannot_Be_Static =>
4353 -- If the object that cannot be static is imported or exported,
4354 -- then we give an error message saying that this object cannot
4355 -- be imported or exported.
4357 if Is_Imported (E) then
4358 Error_Msg_N
4359 ("& cannot be imported (local type is not constant)", E);
4361 -- Otherwise must be exported, something is wrong if compiler
4362 -- is marking something as statically allocated which cannot be).
4364 else pragma Assert (Is_Exported (E));
4365 Error_Msg_N
4366 ("& cannot be exported (local type is not constant)", E);
4367 end if;
4368 end Freeze_Static_Object;
4370 -----------------------
4371 -- Freeze_Subprogram --
4372 -----------------------
4374 procedure Freeze_Subprogram (E : Entity_Id) is
4375 Retype : Entity_Id;
4376 F : Entity_Id;
4378 begin
4379 -- Subprogram may not have an address clause unless it is imported
4381 if Present (Address_Clause (E)) then
4382 if not Is_Imported (E) then
4383 Error_Msg_N
4384 ("address clause can only be given " &
4385 "for imported subprogram",
4386 Name (Address_Clause (E)));
4387 end if;
4388 end if;
4390 -- Reset the Pure indication on an imported subprogram unless an
4391 -- explicit Pure_Function pragma was present. We do this because
4392 -- otherwise it is an insidious error to call a non-pure function
4393 -- from a pure unit and have calls mysteriously optimized away.
4394 -- What happens here is that the Import can bypass the normal
4395 -- check to ensure that pure units call only pure subprograms.
4397 if Is_Imported (E)
4398 and then Is_Pure (E)
4399 and then not Has_Pragma_Pure_Function (E)
4400 then
4401 Set_Is_Pure (E, False);
4402 end if;
4404 -- For non-foreign convention subprograms, this is where we create
4405 -- the extra formals (for accessibility level and constrained bit
4406 -- information). We delay this till the freeze point precisely so
4407 -- that we know the convention!
4409 if not Has_Foreign_Convention (E) then
4410 Create_Extra_Formals (E);
4411 Set_Mechanisms (E);
4413 -- If this is convention Ada and a Valued_Procedure, that's odd
4415 if Ekind (E) = E_Procedure
4416 and then Is_Valued_Procedure (E)
4417 and then Convention (E) = Convention_Ada
4418 and then Warn_On_Export_Import
4419 then
4420 Error_Msg_N
4421 ("?Valued_Procedure has no effect for convention Ada", E);
4422 Set_Is_Valued_Procedure (E, False);
4423 end if;
4425 -- Case of foreign convention
4427 else
4428 Set_Mechanisms (E);
4430 -- For foreign conventions, warn about return of an
4431 -- unconstrained array.
4433 -- Note: we *do* allow a return by descriptor for the VMS case,
4434 -- though here there is probably more to be done ???
4436 if Ekind (E) = E_Function then
4437 Retype := Underlying_Type (Etype (E));
4439 -- If no return type, probably some other error, e.g. a
4440 -- missing full declaration, so ignore.
4442 if No (Retype) then
4443 null;
4445 -- If the return type is generic, we have emitted a warning
4446 -- earlier on, and there is nothing else to check here.
4447 -- Specific instantiations may lead to erroneous behavior.
4449 elsif Is_Generic_Type (Etype (E)) then
4450 null;
4452 elsif Is_Array_Type (Retype)
4453 and then not Is_Constrained (Retype)
4454 and then Mechanism (E) not in Descriptor_Codes
4455 and then Warn_On_Export_Import
4456 then
4457 Error_Msg_N
4458 ("?foreign convention function& should not return " &
4459 "unconstrained array", E);
4460 return;
4461 end if;
4462 end if;
4464 -- If any of the formals for an exported foreign convention
4465 -- subprogram have defaults, then emit an appropriate warning
4466 -- since this is odd (default cannot be used from non-Ada code)
4468 if Is_Exported (E) then
4469 F := First_Formal (E);
4470 while Present (F) loop
4471 if Warn_On_Export_Import
4472 and then Present (Default_Value (F))
4473 then
4474 Error_Msg_N
4475 ("?parameter cannot be defaulted in non-Ada call",
4476 Default_Value (F));
4477 end if;
4479 Next_Formal (F);
4480 end loop;
4481 end if;
4482 end if;
4484 -- For VMS, descriptor mechanisms for parameters are allowed only
4485 -- for imported subprograms.
4487 if OpenVMS_On_Target then
4488 if not Is_Imported (E) then
4489 F := First_Formal (E);
4490 while Present (F) loop
4491 if Mechanism (F) in Descriptor_Codes then
4492 Error_Msg_N
4493 ("descriptor mechanism for parameter not permitted", F);
4494 Error_Msg_N
4495 ("\can only be used for imported subprogram", F);
4496 end if;
4498 Next_Formal (F);
4499 end loop;
4500 end if;
4501 end if;
4502 end Freeze_Subprogram;
4504 ----------------------
4505 -- Is_Fully_Defined --
4506 ----------------------
4508 function Is_Fully_Defined (T : Entity_Id) return Boolean is
4509 begin
4510 if Ekind (T) = E_Class_Wide_Type then
4511 return Is_Fully_Defined (Etype (T));
4513 elsif Is_Array_Type (T) then
4514 return Is_Fully_Defined (Component_Type (T));
4516 elsif Is_Record_Type (T)
4517 and not Is_Private_Type (T)
4518 then
4519 -- Verify that the record type has no components with
4520 -- private types without completion.
4522 declare
4523 Comp : Entity_Id;
4525 begin
4526 Comp := First_Component (T);
4528 while Present (Comp) loop
4529 if not Is_Fully_Defined (Etype (Comp)) then
4530 return False;
4531 end if;
4533 Next_Component (Comp);
4534 end loop;
4535 return True;
4536 end;
4538 else return not Is_Private_Type (T)
4539 or else Present (Full_View (Base_Type (T)));
4540 end if;
4541 end Is_Fully_Defined;
4543 ---------------------------------
4544 -- Process_Default_Expressions --
4545 ---------------------------------
4547 procedure Process_Default_Expressions
4548 (E : Entity_Id;
4549 After : in out Node_Id)
4551 Loc : constant Source_Ptr := Sloc (E);
4552 Dbody : Node_Id;
4553 Formal : Node_Id;
4554 Dcopy : Node_Id;
4555 Dnam : Entity_Id;
4557 begin
4558 Set_Default_Expressions_Processed (E);
4560 -- A subprogram instance and its associated anonymous subprogram
4561 -- share their signature. The default expression functions are defined
4562 -- in the wrapper packages for the anonymous subprogram, and should
4563 -- not be generated again for the instance.
4565 if Is_Generic_Instance (E)
4566 and then Present (Alias (E))
4567 and then Default_Expressions_Processed (Alias (E))
4568 then
4569 return;
4570 end if;
4572 Formal := First_Formal (E);
4574 while Present (Formal) loop
4575 if Present (Default_Value (Formal)) then
4577 -- We work with a copy of the default expression because we
4578 -- do not want to disturb the original, since this would mess
4579 -- up the conformance checking.
4581 Dcopy := New_Copy_Tree (Default_Value (Formal));
4583 -- The analysis of the expression may generate insert actions,
4584 -- which of course must not be executed. We wrap those actions
4585 -- in a procedure that is not called, and later on eliminated.
4586 -- The following cases have no side-effects, and are analyzed
4587 -- directly.
4589 if Nkind (Dcopy) = N_Identifier
4590 or else Nkind (Dcopy) = N_Expanded_Name
4591 or else Nkind (Dcopy) = N_Integer_Literal
4592 or else (Nkind (Dcopy) = N_Real_Literal
4593 and then not Vax_Float (Etype (Dcopy)))
4594 or else Nkind (Dcopy) = N_Character_Literal
4595 or else Nkind (Dcopy) = N_String_Literal
4596 or else Nkind (Dcopy) = N_Null
4597 or else (Nkind (Dcopy) = N_Attribute_Reference
4598 and then
4599 Attribute_Name (Dcopy) = Name_Null_Parameter)
4600 then
4602 -- If there is no default function, we must still do a full
4603 -- analyze call on the default value, to ensure that all
4604 -- error checks are performed, e.g. those associated with
4605 -- static evaluation. Note that this branch will always be
4606 -- taken if the analyzer is turned off (but we still need the
4607 -- error checks).
4609 -- Note: the setting of parent here is to meet the requirement
4610 -- that we can only analyze the expression while attached to
4611 -- the tree. Really the requirement is that the parent chain
4612 -- be set, we don't actually need to be in the tree.
4614 Set_Parent (Dcopy, Declaration_Node (Formal));
4615 Analyze (Dcopy);
4617 -- Default expressions are resolved with their own type if the
4618 -- context is generic, to avoid anomalies with private types.
4620 if Ekind (Scope (E)) = E_Generic_Package then
4621 Resolve (Dcopy);
4622 else
4623 Resolve (Dcopy, Etype (Formal));
4624 end if;
4626 -- If that resolved expression will raise constraint error,
4627 -- then flag the default value as raising constraint error.
4628 -- This allows a proper error message on the calls.
4630 if Raises_Constraint_Error (Dcopy) then
4631 Set_Raises_Constraint_Error (Default_Value (Formal));
4632 end if;
4634 -- If the default is a parameterless call, we use the name of
4635 -- the called function directly, and there is no body to build.
4637 elsif Nkind (Dcopy) = N_Function_Call
4638 and then No (Parameter_Associations (Dcopy))
4639 then
4640 null;
4642 -- Else construct and analyze the body of a wrapper procedure
4643 -- that contains an object declaration to hold the expression.
4644 -- Given that this is done only to complete the analysis, it
4645 -- simpler to build a procedure than a function which might
4646 -- involve secondary stack expansion.
4648 else
4649 Dnam :=
4650 Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
4652 Dbody :=
4653 Make_Subprogram_Body (Loc,
4654 Specification =>
4655 Make_Procedure_Specification (Loc,
4656 Defining_Unit_Name => Dnam),
4658 Declarations => New_List (
4659 Make_Object_Declaration (Loc,
4660 Defining_Identifier =>
4661 Make_Defining_Identifier (Loc,
4662 New_Internal_Name ('T')),
4663 Object_Definition =>
4664 New_Occurrence_Of (Etype (Formal), Loc),
4665 Expression => New_Copy_Tree (Dcopy))),
4667 Handled_Statement_Sequence =>
4668 Make_Handled_Sequence_Of_Statements (Loc,
4669 Statements => New_List));
4671 Set_Scope (Dnam, Scope (E));
4672 Set_Assignment_OK (First (Declarations (Dbody)));
4673 Set_Is_Eliminated (Dnam);
4674 Insert_After (After, Dbody);
4675 Analyze (Dbody);
4676 After := Dbody;
4677 end if;
4678 end if;
4680 Next_Formal (Formal);
4681 end loop;
4683 end Process_Default_Expressions;
4685 ----------------------------------------
4686 -- Set_Component_Alignment_If_Not_Set --
4687 ----------------------------------------
4689 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
4690 begin
4691 -- Ignore if not base type, subtypes don't need anything
4693 if Typ /= Base_Type (Typ) then
4694 return;
4695 end if;
4697 -- Do not override existing representation
4699 if Is_Packed (Typ) then
4700 return;
4702 elsif Has_Specified_Layout (Typ) then
4703 return;
4705 elsif Component_Alignment (Typ) /= Calign_Default then
4706 return;
4708 else
4709 Set_Component_Alignment
4710 (Typ, Scope_Stack.Table
4711 (Scope_Stack.Last).Component_Alignment_Default);
4712 end if;
4713 end Set_Component_Alignment_If_Not_Set;
4715 ---------------------------
4716 -- Set_Debug_Info_Needed --
4717 ---------------------------
4719 procedure Set_Debug_Info_Needed (T : Entity_Id) is
4720 begin
4721 if No (T)
4722 or else Needs_Debug_Info (T)
4723 or else Debug_Info_Off (T)
4724 then
4725 return;
4726 else
4727 Set_Needs_Debug_Info (T);
4728 end if;
4730 if Is_Object (T) then
4731 Set_Debug_Info_Needed (Etype (T));
4733 elsif Is_Type (T) then
4734 Set_Debug_Info_Needed (Etype (T));
4736 if Is_Record_Type (T) then
4737 declare
4738 Ent : Entity_Id := First_Entity (T);
4739 begin
4740 while Present (Ent) loop
4741 Set_Debug_Info_Needed (Ent);
4742 Next_Entity (Ent);
4743 end loop;
4744 end;
4746 elsif Is_Array_Type (T) then
4747 Set_Debug_Info_Needed (Component_Type (T));
4749 declare
4750 Indx : Node_Id := First_Index (T);
4751 begin
4752 while Present (Indx) loop
4753 Set_Debug_Info_Needed (Etype (Indx));
4754 Indx := Next_Index (Indx);
4755 end loop;
4756 end;
4758 if Is_Packed (T) then
4759 Set_Debug_Info_Needed (Packed_Array_Type (T));
4760 end if;
4762 elsif Is_Access_Type (T) then
4763 Set_Debug_Info_Needed (Directly_Designated_Type (T));
4765 elsif Is_Private_Type (T) then
4766 Set_Debug_Info_Needed (Full_View (T));
4768 elsif Is_Protected_Type (T) then
4769 Set_Debug_Info_Needed (Corresponding_Record_Type (T));
4770 end if;
4771 end if;
4772 end Set_Debug_Info_Needed;
4774 ------------------
4775 -- Undelay_Type --
4776 ------------------
4778 procedure Undelay_Type (T : Entity_Id) is
4779 begin
4780 Set_Has_Delayed_Freeze (T, False);
4781 Set_Freeze_Node (T, Empty);
4783 -- Since we don't want T to have a Freeze_Node, we don't want its
4784 -- Full_View or Corresponding_Record_Type to have one either.
4786 -- ??? Fundamentally, this whole handling is a kludge. What we really
4787 -- want is to be sure that for an Itype that's part of record R and
4788 -- is a subtype of type T, that it's frozen after the later of the
4789 -- freeze points of R and T. We have no way of doing that directly,
4790 -- so what we do is force most such Itypes to be frozen as part of
4791 -- freezing R via this procedure and only delay the ones that need
4792 -- to be delayed (mostly the designated types of access types that are
4793 -- defined as part of the record).
4795 if Is_Private_Type (T)
4796 and then Present (Full_View (T))
4797 and then Is_Itype (Full_View (T))
4798 and then Is_Record_Type (Scope (Full_View (T)))
4799 then
4800 Undelay_Type (Full_View (T));
4801 end if;
4803 if Is_Concurrent_Type (T)
4804 and then Present (Corresponding_Record_Type (T))
4805 and then Is_Itype (Corresponding_Record_Type (T))
4806 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
4807 then
4808 Undelay_Type (Corresponding_Record_Type (T));
4809 end if;
4810 end Undelay_Type;
4812 ------------------
4813 -- Warn_Overlay --
4814 ------------------
4816 procedure Warn_Overlay
4817 (Expr : Node_Id;
4818 Typ : Entity_Id;
4819 Nam : Entity_Id)
4821 Ent : constant Entity_Id := Entity (Nam);
4822 -- The object to which the address clause applies.
4824 Init : Node_Id;
4825 Old : Entity_Id := Empty;
4826 Decl : Node_Id;
4828 begin
4829 -- No warning if address clause overlay warnings are off
4831 if not Address_Clause_Overlay_Warnings then
4832 return;
4833 end if;
4835 -- No warning if there is an explicit initialization
4837 Init := Original_Node (Expression (Declaration_Node (Ent)));
4839 if Present (Init) and then Comes_From_Source (Init) then
4840 return;
4841 end if;
4843 -- We only give the warning for non-imported entities of a type
4844 -- for which a non-null base init proc is defined (or for access
4845 -- types which have implicit null initialization).
4847 if Present (Expr)
4848 and then (Has_Non_Null_Base_Init_Proc (Typ)
4849 or else Is_Access_Type (Typ))
4850 and then not Is_Imported (Ent)
4851 then
4852 if Nkind (Expr) = N_Attribute_Reference
4853 and then Is_Entity_Name (Prefix (Expr))
4854 then
4855 Old := Entity (Prefix (Expr));
4857 elsif Is_Entity_Name (Expr)
4858 and then Ekind (Entity (Expr)) = E_Constant
4859 then
4860 Decl := Declaration_Node (Entity (Expr));
4862 if Nkind (Decl) = N_Object_Declaration
4863 and then Present (Expression (Decl))
4864 and then Nkind (Expression (Decl)) = N_Attribute_Reference
4865 and then Is_Entity_Name (Prefix (Expression (Decl)))
4866 then
4867 Old := Entity (Prefix (Expression (Decl)));
4869 elsif Nkind (Expr) = N_Function_Call then
4870 return;
4871 end if;
4873 -- A function call (most likely to To_Address) is probably not
4874 -- an overlay, so skip warning. Ditto if the function call was
4875 -- inlined and transformed into an entity.
4877 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
4878 return;
4879 end if;
4881 Decl := Next (Parent (Expr));
4883 -- If a pragma Import follows, we assume that it is for the current
4884 -- target of the address clause, and skip the warning.
4886 if Present (Decl)
4887 and then Nkind (Decl) = N_Pragma
4888 and then Chars (Decl) = Name_Import
4889 then
4890 return;
4891 end if;
4893 if Present (Old) then
4894 Error_Msg_Node_2 := Old;
4895 Error_Msg_N
4896 ("default initialization of & may modify &?",
4897 Nam);
4898 else
4899 Error_Msg_N
4900 ("default initialization of & may modify overlaid storage?",
4901 Nam);
4902 end if;
4904 -- Add friendly warning if initialization comes from a packed array
4905 -- component.
4907 if Is_Record_Type (Typ) then
4908 declare
4909 Comp : Entity_Id;
4911 begin
4912 Comp := First_Component (Typ);
4914 while Present (Comp) loop
4915 if Nkind (Parent (Comp)) = N_Component_Declaration
4916 and then Present (Expression (Parent (Comp)))
4917 then
4918 exit;
4919 elsif Is_Array_Type (Etype (Comp))
4920 and then Present (Packed_Array_Type (Etype (Comp)))
4921 then
4922 Error_Msg_NE
4923 ("packed array component& will be initialized to zero?",
4924 Nam, Comp);
4925 exit;
4926 else
4927 Next_Component (Comp);
4928 end if;
4929 end loop;
4930 end;
4931 end if;
4933 Error_Msg_N
4934 ("use pragma Import for & to " &
4935 "suppress initialization ('R'M B.1(24))?",
4936 Nam);
4937 end if;
4938 end Warn_Overlay;
4940 end Freeze;