* tree.c (handle_abi_tag_attribute): Diagnose invalid arguments.
[official-gcc.git] / gcc / ada / freeze.adb
blob2864fb1e6b948c7ecc1284b905a609ae1c80341f
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-2015, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Pakd; use Exp_Pakd;
37 with Exp_Util; use Exp_Util;
38 with Exp_Tss; use Exp_Tss;
39 with Ghost; use Ghost;
40 with Layout; use Layout;
41 with Lib; use Lib;
42 with Namet; use Namet;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Mech; use Sem_Mech;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Sinfo; use Sinfo;
62 with Snames; use Snames;
63 with Stand; use Stand;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Ttypes; use Ttypes;
67 with Uintp; use Uintp;
68 with Urealp; use Urealp;
69 with Warnsw; use Warnsw;
71 package body Freeze is
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
78 -- Typ is a type that is being frozen. If no size clause is given,
79 -- but a default Esize has been computed, then this default Esize is
80 -- adjusted up if necessary to be consistent with a given alignment,
81 -- but never to a value greater than Long_Long_Integer'Size. This
82 -- is used for all discrete types and for fixed-point types.
84 procedure Build_And_Analyze_Renamed_Body
85 (Decl : Node_Id;
86 New_S : Entity_Id;
87 After : in out Node_Id);
88 -- Build body for a renaming declaration, insert in tree and analyze
90 procedure Check_Address_Clause (E : Entity_Id);
91 -- Apply legality checks to address clauses for object declarations,
92 -- at the point the object is frozen. Also ensure any initialization is
93 -- performed only after the object has been frozen.
95 procedure Check_Component_Storage_Order
96 (Encl_Type : Entity_Id;
97 Comp : Entity_Id;
98 ADC : Node_Id;
99 Comp_ADC_Present : out Boolean);
100 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
101 -- clause, verify that the component type has an explicit and compatible
102 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
103 -- entity of the component under consideration. For an Encl_Type that
104 -- does not have a Scalar_Storage_Order attribute definition clause,
105 -- verify that the component also does not have such a clause.
106 -- ADC is the attribute definition clause if present (or Empty). On return,
107 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
108 -- attribute definition clause.
110 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
111 -- When an expression function is frozen by a use of it, the expression
112 -- itself is frozen. Check that the expression does not include references
113 -- to deferred constants without completion. We report this at the freeze
114 -- point of the function, to provide a better error message.
116 -- In most cases the expression itself is frozen by the time the function
117 -- itself is frozen, because the formals will be frozen by then. However,
118 -- Attribute references to outer types are freeze points for those types;
119 -- this routine generates the required freeze nodes for them.
121 procedure Check_Strict_Alignment (E : Entity_Id);
122 -- E is a base type. If E is tagged or has a component that is aliased
123 -- or tagged or contains something this is aliased or tagged, set
124 -- Strict_Alignment.
126 procedure Check_Unsigned_Type (E : Entity_Id);
127 pragma Inline (Check_Unsigned_Type);
128 -- If E is a fixed-point or discrete type, then all the necessary work
129 -- to freeze it is completed except for possible setting of the flag
130 -- Is_Unsigned_Type, which is done by this procedure. The call has no
131 -- effect if the entity E is not a discrete or fixed-point type.
133 procedure Freeze_And_Append
134 (Ent : Entity_Id;
135 N : Node_Id;
136 Result : in out List_Id);
137 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
138 -- nodes to Result, modifying Result from No_List if necessary. N has
139 -- the same usage as in Freeze_Entity.
141 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
142 -- Freeze enumeration type. The Esize field is set as processing
143 -- proceeds (i.e. set by default when the type is declared and then
144 -- adjusted by rep clauses. What this procedure does is to make sure
145 -- that if a foreign convention is specified, and no specific size
146 -- is given, then the size must be at least Integer'Size.
148 procedure Freeze_Static_Object (E : Entity_Id);
149 -- If an object is frozen which has Is_Statically_Allocated set, then
150 -- all referenced types must also be marked with this flag. This routine
151 -- is in charge of meeting this requirement for the object entity E.
153 procedure Freeze_Subprogram (E : Entity_Id);
154 -- Perform freezing actions for a subprogram (create extra formals,
155 -- and set proper default mechanism values). Note that this routine
156 -- is not called for internal subprograms, for which neither of these
157 -- actions is needed (or desirable, we do not want for example to have
158 -- these extra formals present in initialization procedures, where they
159 -- would serve no purpose). In this call E is either a subprogram or
160 -- a subprogram type (i.e. an access to a subprogram).
162 function Is_Fully_Defined (T : Entity_Id) return Boolean;
163 -- True if T is not private and has no private components, or has a full
164 -- view. Used to determine whether the designated type of an access type
165 -- should be frozen when the access type is frozen. This is done when an
166 -- allocator is frozen, or an expression that may involve attributes of
167 -- the designated type. Otherwise freezing the access type does not freeze
168 -- the designated type.
170 procedure Process_Default_Expressions
171 (E : Entity_Id;
172 After : in out Node_Id);
173 -- This procedure is called for each subprogram to complete processing of
174 -- default expressions at the point where all types are known to be frozen.
175 -- The expressions must be analyzed in full, to make sure that all error
176 -- processing is done (they have only been pre-analyzed). If the expression
177 -- is not an entity or literal, its analysis may generate code which must
178 -- not be executed. In that case we build a function body to hold that
179 -- code. This wrapper function serves no other purpose (it used to be
180 -- called to evaluate the default, but now the default is inlined at each
181 -- point of call).
183 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
184 -- Typ is a record or array type that is being frozen. This routine sets
185 -- the default component alignment from the scope stack values if the
186 -- alignment is otherwise not specified.
188 procedure Check_Debug_Info_Needed (T : Entity_Id);
189 -- As each entity is frozen, this routine is called to deal with the
190 -- setting of Debug_Info_Needed for the entity. This flag is set if
191 -- the entity comes from source, or if we are in Debug_Generated_Code
192 -- mode or if the -gnatdV debug flag is set. However, it never sets
193 -- the flag if Debug_Info_Off is set. This procedure also ensures that
194 -- subsidiary entities have the flag set as required.
196 procedure Set_SSO_From_Default (T : Entity_Id);
197 -- T is a record or array type that is being frozen. If it is a base type,
198 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
199 -- will be set appropriately. Note that an explicit occurrence of aspect
200 -- Scalar_Storage_Order or an explicit setting of this aspect with an
201 -- attribute definition clause occurs, then these two flags are reset in
202 -- any case, so call will have no effect.
204 procedure Undelay_Type (T : Entity_Id);
205 -- T is a type of a component that we know to be an Itype. We don't want
206 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
207 -- Full_View or Corresponding_Record_Type.
209 procedure Warn_Overlay
210 (Expr : Node_Id;
211 Typ : Entity_Id;
212 Nam : Node_Id);
213 -- Expr is the expression for an address clause for entity Nam whose type
214 -- is Typ. If Typ has a default initialization, and there is no explicit
215 -- initialization in the source declaration, check whether the address
216 -- clause might cause overlaying of an entity, and emit a warning on the
217 -- side effect that the initialization will cause.
219 -------------------------------
220 -- Adjust_Esize_For_Alignment --
221 -------------------------------
223 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
224 Align : Uint;
226 begin
227 if Known_Esize (Typ) and then Known_Alignment (Typ) then
228 Align := Alignment_In_Bits (Typ);
230 if Align > Esize (Typ)
231 and then Align <= Standard_Long_Long_Integer_Size
232 then
233 Set_Esize (Typ, Align);
234 end if;
235 end if;
236 end Adjust_Esize_For_Alignment;
238 ------------------------------------
239 -- Build_And_Analyze_Renamed_Body --
240 ------------------------------------
242 procedure Build_And_Analyze_Renamed_Body
243 (Decl : Node_Id;
244 New_S : Entity_Id;
245 After : in out Node_Id)
247 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
248 Ent : constant Entity_Id := Defining_Entity (Decl);
249 Body_Node : Node_Id;
250 Renamed_Subp : Entity_Id;
252 begin
253 -- If the renamed subprogram is intrinsic, there is no need for a
254 -- wrapper body: we set the alias that will be called and expanded which
255 -- completes the declaration. This transformation is only legal if the
256 -- renamed entity has already been elaborated.
258 -- Note that it is legal for a renaming_as_body to rename an intrinsic
259 -- subprogram, as long as the renaming occurs before the new entity
260 -- is frozen (RM 8.5.4 (5)).
262 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
263 and then Is_Entity_Name (Name (Body_Decl))
264 then
265 Renamed_Subp := Entity (Name (Body_Decl));
266 else
267 Renamed_Subp := Empty;
268 end if;
270 if Present (Renamed_Subp)
271 and then Is_Intrinsic_Subprogram (Renamed_Subp)
272 and then
273 (not In_Same_Source_Unit (Renamed_Subp, Ent)
274 or else Sloc (Renamed_Subp) < Sloc (Ent))
276 -- We can make the renaming entity intrinsic if the renamed function
277 -- has an interface name, or if it is one of the shift/rotate
278 -- operations known to the compiler.
280 and then
281 (Present (Interface_Name (Renamed_Subp))
282 or else Nam_In (Chars (Renamed_Subp), Name_Rotate_Left,
283 Name_Rotate_Right,
284 Name_Shift_Left,
285 Name_Shift_Right,
286 Name_Shift_Right_Arithmetic))
287 then
288 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
290 if Present (Alias (Renamed_Subp)) then
291 Set_Alias (Ent, Alias (Renamed_Subp));
292 else
293 Set_Alias (Ent, Renamed_Subp);
294 end if;
296 Set_Is_Intrinsic_Subprogram (Ent);
297 Set_Has_Completion (Ent);
299 else
300 Body_Node := Build_Renamed_Body (Decl, New_S);
301 Insert_After (After, Body_Node);
302 Mark_Rewrite_Insertion (Body_Node);
303 Analyze (Body_Node);
304 After := Body_Node;
305 end if;
306 end Build_And_Analyze_Renamed_Body;
308 ------------------------
309 -- Build_Renamed_Body --
310 ------------------------
312 function Build_Renamed_Body
313 (Decl : Node_Id;
314 New_S : Entity_Id) return Node_Id
316 Loc : constant Source_Ptr := Sloc (New_S);
317 -- We use for the source location of the renamed body, the location of
318 -- the spec entity. It might seem more natural to use the location of
319 -- the renaming declaration itself, but that would be wrong, since then
320 -- the body we create would look as though it was created far too late,
321 -- and this could cause problems with elaboration order analysis,
322 -- particularly in connection with instantiations.
324 N : constant Node_Id := Unit_Declaration_Node (New_S);
325 Nam : constant Node_Id := Name (N);
326 Old_S : Entity_Id;
327 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
328 Actuals : List_Id := No_List;
329 Call_Node : Node_Id;
330 Call_Name : Node_Id;
331 Body_Node : Node_Id;
332 Formal : Entity_Id;
333 O_Formal : Entity_Id;
334 Param_Spec : Node_Id;
336 Pref : Node_Id := Empty;
337 -- If the renamed entity is a primitive operation given in prefix form,
338 -- the prefix is the target object and it has to be added as the first
339 -- actual in the generated call.
341 begin
342 -- Determine the entity being renamed, which is the target of the call
343 -- statement. If the name is an explicit dereference, this is a renaming
344 -- of a subprogram type rather than a subprogram. The name itself is
345 -- fully analyzed.
347 if Nkind (Nam) = N_Selected_Component then
348 Old_S := Entity (Selector_Name (Nam));
350 elsif Nkind (Nam) = N_Explicit_Dereference then
351 Old_S := Etype (Nam);
353 elsif Nkind (Nam) = N_Indexed_Component then
354 if Is_Entity_Name (Prefix (Nam)) then
355 Old_S := Entity (Prefix (Nam));
356 else
357 Old_S := Entity (Selector_Name (Prefix (Nam)));
358 end if;
360 elsif Nkind (Nam) = N_Character_Literal then
361 Old_S := Etype (New_S);
363 else
364 Old_S := Entity (Nam);
365 end if;
367 if Is_Entity_Name (Nam) then
369 -- If the renamed entity is a predefined operator, retain full name
370 -- to ensure its visibility.
372 if Ekind (Old_S) = E_Operator
373 and then Nkind (Nam) = N_Expanded_Name
374 then
375 Call_Name := New_Copy (Name (N));
376 else
377 Call_Name := New_Occurrence_Of (Old_S, Loc);
378 end if;
380 else
381 if Nkind (Nam) = N_Selected_Component
382 and then Present (First_Formal (Old_S))
383 and then
384 (Is_Controlling_Formal (First_Formal (Old_S))
385 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
386 then
388 -- Retrieve the target object, to be added as a first actual
389 -- in the call.
391 Call_Name := New_Occurrence_Of (Old_S, Loc);
392 Pref := Prefix (Nam);
394 else
395 Call_Name := New_Copy (Name (N));
396 end if;
398 -- Original name may have been overloaded, but is fully resolved now
400 Set_Is_Overloaded (Call_Name, False);
401 end if;
403 -- For simple renamings, subsequent calls can be expanded directly as
404 -- calls to the renamed entity. The body must be generated in any case
405 -- for calls that may appear elsewhere. This is not done in the case
406 -- where the subprogram is an instantiation because the actual proper
407 -- body has not been built yet.
409 if Ekind_In (Old_S, E_Function, E_Procedure)
410 and then Nkind (Decl) = N_Subprogram_Declaration
411 and then not Is_Generic_Instance (Old_S)
412 then
413 Set_Body_To_Inline (Decl, Old_S);
414 end if;
416 -- Check whether the return type is a limited view. If the subprogram
417 -- is already frozen the generated body may have a non-limited view
418 -- of the type, that must be used, because it is the one in the spec
419 -- of the renaming declaration.
421 if Ekind (Old_S) = E_Function
422 and then Is_Entity_Name (Result_Definition (Spec))
423 then
424 declare
425 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
426 begin
427 if Ekind (Ret_Type) = E_Incomplete_Type
428 and then Present (Non_Limited_View (Ret_Type))
429 then
430 Set_Result_Definition (Spec,
431 New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
432 end if;
433 end;
434 end if;
436 -- The body generated for this renaming is an internal artifact, and
437 -- does not constitute a freeze point for the called entity.
439 Set_Must_Not_Freeze (Call_Name);
441 Formal := First_Formal (Defining_Entity (Decl));
443 if Present (Pref) then
444 declare
445 Pref_Type : constant Entity_Id := Etype (Pref);
446 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
448 begin
449 -- The controlling formal may be an access parameter, or the
450 -- actual may be an access value, so adjust accordingly.
452 if Is_Access_Type (Pref_Type)
453 and then not Is_Access_Type (Form_Type)
454 then
455 Actuals := New_List
456 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
458 elsif Is_Access_Type (Form_Type)
459 and then not Is_Access_Type (Pref)
460 then
461 Actuals := New_List
462 (Make_Attribute_Reference (Loc,
463 Attribute_Name => Name_Access,
464 Prefix => Relocate_Node (Pref)));
465 else
466 Actuals := New_List (Pref);
467 end if;
468 end;
470 elsif Present (Formal) then
471 Actuals := New_List;
473 else
474 Actuals := No_List;
475 end if;
477 if Present (Formal) then
478 while Present (Formal) loop
479 Append (New_Occurrence_Of (Formal, Loc), Actuals);
480 Next_Formal (Formal);
481 end loop;
482 end if;
484 -- If the renamed entity is an entry, inherit its profile. For other
485 -- renamings as bodies, both profiles must be subtype conformant, so it
486 -- is not necessary to replace the profile given in the declaration.
487 -- However, default values that are aggregates are rewritten when
488 -- partially analyzed, so we recover the original aggregate to insure
489 -- that subsequent conformity checking works. Similarly, if the default
490 -- expression was constant-folded, recover the original expression.
492 Formal := First_Formal (Defining_Entity (Decl));
494 if Present (Formal) then
495 O_Formal := First_Formal (Old_S);
496 Param_Spec := First (Parameter_Specifications (Spec));
497 while Present (Formal) loop
498 if Is_Entry (Old_S) then
499 if Nkind (Parameter_Type (Param_Spec)) /=
500 N_Access_Definition
501 then
502 Set_Etype (Formal, Etype (O_Formal));
503 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
504 end if;
506 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
507 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
508 Nkind (Default_Value (O_Formal))
509 then
510 Set_Expression (Param_Spec,
511 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
512 end if;
514 Next_Formal (Formal);
515 Next_Formal (O_Formal);
516 Next (Param_Spec);
517 end loop;
518 end if;
520 -- If the renamed entity is a function, the generated body contains a
521 -- return statement. Otherwise, build a procedure call. If the entity is
522 -- an entry, subsequent analysis of the call will transform it into the
523 -- proper entry or protected operation call. If the renamed entity is
524 -- a character literal, return it directly.
526 if Ekind (Old_S) = E_Function
527 or else Ekind (Old_S) = E_Operator
528 or else (Ekind (Old_S) = E_Subprogram_Type
529 and then Etype (Old_S) /= Standard_Void_Type)
530 then
531 Call_Node :=
532 Make_Simple_Return_Statement (Loc,
533 Expression =>
534 Make_Function_Call (Loc,
535 Name => Call_Name,
536 Parameter_Associations => Actuals));
538 elsif Ekind (Old_S) = E_Enumeration_Literal then
539 Call_Node :=
540 Make_Simple_Return_Statement (Loc,
541 Expression => New_Occurrence_Of (Old_S, Loc));
543 elsif Nkind (Nam) = N_Character_Literal then
544 Call_Node :=
545 Make_Simple_Return_Statement (Loc,
546 Expression => Call_Name);
548 else
549 Call_Node :=
550 Make_Procedure_Call_Statement (Loc,
551 Name => Call_Name,
552 Parameter_Associations => Actuals);
553 end if;
555 -- Create entities for subprogram body and formals
557 Set_Defining_Unit_Name (Spec,
558 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
560 Param_Spec := First (Parameter_Specifications (Spec));
561 while Present (Param_Spec) loop
562 Set_Defining_Identifier (Param_Spec,
563 Make_Defining_Identifier (Loc,
564 Chars => Chars (Defining_Identifier (Param_Spec))));
565 Next (Param_Spec);
566 end loop;
568 Body_Node :=
569 Make_Subprogram_Body (Loc,
570 Specification => Spec,
571 Declarations => New_List,
572 Handled_Statement_Sequence =>
573 Make_Handled_Sequence_Of_Statements (Loc,
574 Statements => New_List (Call_Node)));
576 if Nkind (Decl) /= N_Subprogram_Declaration then
577 Rewrite (N,
578 Make_Subprogram_Declaration (Loc,
579 Specification => Specification (N)));
580 end if;
582 -- Link the body to the entity whose declaration it completes. If
583 -- the body is analyzed when the renamed entity is frozen, it may
584 -- be necessary to restore the proper scope (see package Exp_Ch13).
586 if Nkind (N) = N_Subprogram_Renaming_Declaration
587 and then Present (Corresponding_Spec (N))
588 then
589 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
590 else
591 Set_Corresponding_Spec (Body_Node, New_S);
592 end if;
594 return Body_Node;
595 end Build_Renamed_Body;
597 --------------------------
598 -- Check_Address_Clause --
599 --------------------------
601 procedure Check_Address_Clause (E : Entity_Id) is
602 Addr : constant Node_Id := Address_Clause (E);
603 Expr : Node_Id;
604 Decl : constant Node_Id := Declaration_Node (E);
605 Loc : constant Source_Ptr := Sloc (Decl);
606 Typ : constant Entity_Id := Etype (E);
607 Lhs : Node_Id;
608 Tag_Assign : Node_Id;
610 begin
611 if Present (Addr) then
612 Expr := Expression (Addr);
614 if Needs_Constant_Address (Decl, Typ) then
615 Check_Constant_Address_Clause (Expr, E);
617 -- Has_Delayed_Freeze was set on E when the address clause was
618 -- analyzed, and must remain set because we want the address
619 -- clause to be elaborated only after any entity it references
620 -- has been elaborated.
621 end if;
623 -- If Rep_Clauses are to be ignored, remove address clause from
624 -- list attached to entity, because it may be illegal for gigi,
625 -- for example by breaking order of elaboration..
627 if Ignore_Rep_Clauses then
628 declare
629 Rep : Node_Id;
631 begin
632 Rep := First_Rep_Item (E);
634 if Rep = Addr then
635 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
637 else
638 while Present (Rep)
639 and then Next_Rep_Item (Rep) /= Addr
640 loop
641 Rep := Next_Rep_Item (Rep);
642 end loop;
643 end if;
645 if Present (Rep) then
646 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
647 end if;
648 end;
650 -- And now remove the address clause
652 Kill_Rep_Clause (Addr);
654 elsif not Error_Posted (Expr)
655 and then not Needs_Finalization (Typ)
656 then
657 Warn_Overlay (Expr, Typ, Name (Addr));
658 end if;
660 if Present (Expression (Decl)) then
662 -- Capture initialization value at point of declaration,
663 -- and make explicit assignment legal, because object may
664 -- be a constant.
666 Remove_Side_Effects (Expression (Decl));
667 Lhs := New_Occurrence_Of (E, Loc);
668 Set_Assignment_OK (Lhs);
670 -- Move initialization to freeze actions (once the object has
671 -- been frozen, and the address clause alignment check has been
672 -- performed.
674 Append_Freeze_Action (E,
675 Make_Assignment_Statement (Loc,
676 Name => Lhs,
677 Expression => Expression (Decl)));
679 Set_No_Initialization (Decl);
681 -- If the objet is tagged, check whether the tag must be
682 -- reassigned expliitly.
684 Tag_Assign := Make_Tag_Assignment (Decl);
685 if Present (Tag_Assign) then
686 Append_Freeze_Action (E, Tag_Assign);
687 end if;
688 end if;
689 end if;
690 end Check_Address_Clause;
692 -----------------------------
693 -- Check_Compile_Time_Size --
694 -----------------------------
696 procedure Check_Compile_Time_Size (T : Entity_Id) is
698 procedure Set_Small_Size (T : Entity_Id; S : Uint);
699 -- Sets the compile time known size (32 bits or less) in the Esize
700 -- field, of T checking for a size clause that was given which attempts
701 -- to give a smaller size, and also checking for an alignment clause.
703 function Size_Known (T : Entity_Id) return Boolean;
704 -- Recursive function that does all the work
706 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
707 -- If T is a constrained subtype, its size is not known if any of its
708 -- discriminant constraints is not static and it is not a null record.
709 -- The test is conservative and doesn't check that the components are
710 -- in fact constrained by non-static discriminant values. Could be made
711 -- more precise ???
713 --------------------
714 -- Set_Small_Size --
715 --------------------
717 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
718 begin
719 if S > 32 then
720 return;
722 -- Check for bad size clause given
724 elsif Has_Size_Clause (T) then
725 if RM_Size (T) < S then
726 Error_Msg_Uint_1 := S;
727 Error_Msg_NE
728 ("size for& too small, minimum allowed is ^",
729 Size_Clause (T), T);
730 end if;
732 -- Set size if not set already
734 elsif Unknown_RM_Size (T) then
735 Set_RM_Size (T, S);
736 end if;
737 end Set_Small_Size;
739 ----------------
740 -- Size_Known --
741 ----------------
743 function Size_Known (T : Entity_Id) return Boolean is
744 Index : Entity_Id;
745 Comp : Entity_Id;
746 Ctyp : Entity_Id;
747 Low : Node_Id;
748 High : Node_Id;
750 begin
751 if Size_Known_At_Compile_Time (T) then
752 return True;
754 -- Always True for scalar types. This is true even for generic formal
755 -- scalar types. We used to return False in the latter case, but the
756 -- size is known at compile time, even in the template, we just do
757 -- not know the exact size but that's not the point of this routine.
759 elsif Is_Scalar_Type (T)
760 or else Is_Task_Type (T)
761 then
762 return True;
764 -- Array types
766 elsif Is_Array_Type (T) then
768 -- String literals always have known size, and we can set it
770 if Ekind (T) = E_String_Literal_Subtype then
771 Set_Small_Size (T, Component_Size (T)
772 * String_Literal_Length (T));
773 return True;
775 -- Unconstrained types never have known at compile time size
777 elsif not Is_Constrained (T) then
778 return False;
780 -- Don't do any recursion on type with error posted, since we may
781 -- have a malformed type that leads us into a loop.
783 elsif Error_Posted (T) then
784 return False;
786 -- Otherwise if component size unknown, then array size unknown
788 elsif not Size_Known (Component_Type (T)) then
789 return False;
790 end if;
792 -- Check for all indexes static, and also compute possible size
793 -- (in case it is less than 32 and may be packable).
795 declare
796 Esiz : Uint := Component_Size (T);
797 Dim : Uint;
799 begin
800 Index := First_Index (T);
801 while Present (Index) loop
802 if Nkind (Index) = N_Range then
803 Get_Index_Bounds (Index, Low, High);
805 elsif Error_Posted (Scalar_Range (Etype (Index))) then
806 return False;
808 else
809 Low := Type_Low_Bound (Etype (Index));
810 High := Type_High_Bound (Etype (Index));
811 end if;
813 if not Compile_Time_Known_Value (Low)
814 or else not Compile_Time_Known_Value (High)
815 or else Etype (Index) = Any_Type
816 then
817 return False;
819 else
820 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
822 if Dim >= 0 then
823 Esiz := Esiz * Dim;
824 else
825 Esiz := Uint_0;
826 end if;
827 end if;
829 Next_Index (Index);
830 end loop;
832 Set_Small_Size (T, Esiz);
833 return True;
834 end;
836 -- Access types always have known at compile time sizes
838 elsif Is_Access_Type (T) then
839 return True;
841 -- For non-generic private types, go to underlying type if present
843 elsif Is_Private_Type (T)
844 and then not Is_Generic_Type (T)
845 and then Present (Underlying_Type (T))
846 then
847 -- Don't do any recursion on type with error posted, since we may
848 -- have a malformed type that leads us into a loop.
850 if Error_Posted (T) then
851 return False;
852 else
853 return Size_Known (Underlying_Type (T));
854 end if;
856 -- Record types
858 elsif Is_Record_Type (T) then
860 -- A class-wide type is never considered to have a known size
862 if Is_Class_Wide_Type (T) then
863 return False;
865 -- A subtype of a variant record must not have non-static
866 -- discriminated components.
868 elsif T /= Base_Type (T)
869 and then not Static_Discriminated_Components (T)
870 then
871 return False;
873 -- Don't do any recursion on type with error posted, since we may
874 -- have a malformed type that leads us into a loop.
876 elsif Error_Posted (T) then
877 return False;
878 end if;
880 -- Now look at the components of the record
882 declare
883 -- The following two variables are used to keep track of the
884 -- size of packed records if we can tell the size of the packed
885 -- record in the front end. Packed_Size_Known is True if so far
886 -- we can figure out the size. It is initialized to True for a
887 -- packed record, unless the record has discriminants or atomic
888 -- components or independent components.
890 -- The reason we eliminate the discriminated case is that
891 -- we don't know the way the back end lays out discriminated
892 -- packed records. If Packed_Size_Known is True, then
893 -- Packed_Size is the size in bits so far.
895 Packed_Size_Known : Boolean :=
896 Is_Packed (T)
897 and then not Has_Discriminants (T)
898 and then not Has_Atomic_Components (T)
899 and then not Has_Independent_Components (T);
901 Packed_Size : Uint := Uint_0;
902 -- Size in bits so far
904 begin
905 -- Test for variant part present
907 if Has_Discriminants (T)
908 and then Present (Parent (T))
909 and then Nkind (Parent (T)) = N_Full_Type_Declaration
910 and then Nkind (Type_Definition (Parent (T))) =
911 N_Record_Definition
912 and then not Null_Present (Type_Definition (Parent (T)))
913 and then
914 Present (Variant_Part
915 (Component_List (Type_Definition (Parent (T)))))
916 then
917 -- If variant part is present, and type is unconstrained,
918 -- then we must have defaulted discriminants, or a size
919 -- clause must be present for the type, or else the size
920 -- is definitely not known at compile time.
922 if not Is_Constrained (T)
923 and then
924 No (Discriminant_Default_Value (First_Discriminant (T)))
925 and then Unknown_RM_Size (T)
926 then
927 return False;
928 end if;
929 end if;
931 -- Loop through components
933 Comp := First_Component_Or_Discriminant (T);
934 while Present (Comp) loop
935 Ctyp := Etype (Comp);
937 -- We do not know the packed size if there is a component
938 -- clause present (we possibly could, but this would only
939 -- help in the case of a record with partial rep clauses.
940 -- That's because in the case of full rep clauses, the
941 -- size gets figured out anyway by a different circuit).
943 if Present (Component_Clause (Comp)) then
944 Packed_Size_Known := False;
945 end if;
947 -- We do not know the packed size if we have a by reference
948 -- type, or an atomic type or an atomic component, or an
949 -- aliased component (because packing does not touch these).
951 if Is_Atomic (Ctyp)
952 or else Is_Atomic (Comp)
953 or else Is_By_Reference_Type (Ctyp)
954 or else Is_Aliased (Comp)
955 then
956 Packed_Size_Known := False;
957 end if;
959 -- We need to identify a component that is an array where
960 -- the index type is an enumeration type with non-standard
961 -- representation, and some bound of the type depends on a
962 -- discriminant.
964 -- This is because gigi computes the size by doing a
965 -- substitution of the appropriate discriminant value in
966 -- the size expression for the base type, and gigi is not
967 -- clever enough to evaluate the resulting expression (which
968 -- involves a call to rep_to_pos) at compile time.
970 -- It would be nice if gigi would either recognize that
971 -- this expression can be computed at compile time, or
972 -- alternatively figured out the size from the subtype
973 -- directly, where all the information is at hand ???
975 if Is_Array_Type (Etype (Comp))
976 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
977 then
978 declare
979 Ocomp : constant Entity_Id :=
980 Original_Record_Component (Comp);
981 OCtyp : constant Entity_Id := Etype (Ocomp);
982 Ind : Node_Id;
983 Indtyp : Entity_Id;
984 Lo, Hi : Node_Id;
986 begin
987 Ind := First_Index (OCtyp);
988 while Present (Ind) loop
989 Indtyp := Etype (Ind);
991 if Is_Enumeration_Type (Indtyp)
992 and then Has_Non_Standard_Rep (Indtyp)
993 then
994 Lo := Type_Low_Bound (Indtyp);
995 Hi := Type_High_Bound (Indtyp);
997 if Is_Entity_Name (Lo)
998 and then Ekind (Entity (Lo)) = E_Discriminant
999 then
1000 return False;
1002 elsif Is_Entity_Name (Hi)
1003 and then Ekind (Entity (Hi)) = E_Discriminant
1004 then
1005 return False;
1006 end if;
1007 end if;
1009 Next_Index (Ind);
1010 end loop;
1011 end;
1012 end if;
1014 -- Clearly size of record is not known if the size of one of
1015 -- the components is not known.
1017 if not Size_Known (Ctyp) then
1018 return False;
1019 end if;
1021 -- Accumulate packed size if possible
1023 if Packed_Size_Known then
1025 -- We can only deal with elementary types, since for
1026 -- non-elementary components, alignment enters into the
1027 -- picture, and we don't know enough to handle proper
1028 -- alignment in this context. Packed arrays count as
1029 -- elementary if the representation is a modular type.
1031 if Is_Elementary_Type (Ctyp)
1032 or else (Is_Array_Type (Ctyp)
1033 and then Present
1034 (Packed_Array_Impl_Type (Ctyp))
1035 and then Is_Modular_Integer_Type
1036 (Packed_Array_Impl_Type (Ctyp)))
1037 then
1038 -- Packed size unknown if we have an atomic type
1039 -- or a by reference type, since the back end
1040 -- knows how these are layed out.
1042 if Is_Atomic (Ctyp)
1043 or else Is_By_Reference_Type (Ctyp)
1044 then
1045 Packed_Size_Known := False;
1047 -- If RM_Size is known and static, then we can keep
1048 -- accumulating the packed size
1050 elsif Known_Static_RM_Size (Ctyp) then
1052 -- A little glitch, to be removed sometime ???
1053 -- gigi does not understand zero sizes yet.
1055 if RM_Size (Ctyp) = Uint_0 then
1056 Packed_Size_Known := False;
1058 -- Normal case where we can keep accumulating the
1059 -- packed array size.
1061 else
1062 Packed_Size := Packed_Size + RM_Size (Ctyp);
1063 end if;
1065 -- If we have a field whose RM_Size is not known then
1066 -- we can't figure out the packed size here.
1068 else
1069 Packed_Size_Known := False;
1070 end if;
1072 -- If we have a non-elementary type we can't figure out
1073 -- the packed array size (alignment issues).
1075 else
1076 Packed_Size_Known := False;
1077 end if;
1078 end if;
1080 Next_Component_Or_Discriminant (Comp);
1081 end loop;
1083 if Packed_Size_Known then
1084 Set_Small_Size (T, Packed_Size);
1085 end if;
1087 return True;
1088 end;
1090 -- All other cases, size not known at compile time
1092 else
1093 return False;
1094 end if;
1095 end Size_Known;
1097 -------------------------------------
1098 -- Static_Discriminated_Components --
1099 -------------------------------------
1101 function Static_Discriminated_Components
1102 (T : Entity_Id) return Boolean
1104 Constraint : Elmt_Id;
1106 begin
1107 if Has_Discriminants (T)
1108 and then Present (Discriminant_Constraint (T))
1109 and then Present (First_Component (T))
1110 then
1111 Constraint := First_Elmt (Discriminant_Constraint (T));
1112 while Present (Constraint) loop
1113 if not Compile_Time_Known_Value (Node (Constraint)) then
1114 return False;
1115 end if;
1117 Next_Elmt (Constraint);
1118 end loop;
1119 end if;
1121 return True;
1122 end Static_Discriminated_Components;
1124 -- Start of processing for Check_Compile_Time_Size
1126 begin
1127 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1128 end Check_Compile_Time_Size;
1130 -----------------------------------
1131 -- Check_Component_Storage_Order --
1132 -----------------------------------
1134 procedure Check_Component_Storage_Order
1135 (Encl_Type : Entity_Id;
1136 Comp : Entity_Id;
1137 ADC : Node_Id;
1138 Comp_ADC_Present : out Boolean)
1140 Comp_Type : Entity_Id;
1141 Comp_ADC : Node_Id;
1142 Err_Node : Node_Id;
1144 Comp_Byte_Aligned : Boolean;
1145 -- Set for the record case, True if Comp starts on a byte boundary
1146 -- (in which case it is allowed to have different storage order).
1148 Comp_SSO_Differs : Boolean;
1149 -- Set True when the component is a nested composite, and it does not
1150 -- have the same scalar storage order as Encl_Type.
1152 Component_Aliased : Boolean;
1154 begin
1155 -- Record case
1157 if Present (Comp) then
1158 Err_Node := Comp;
1159 Comp_Type := Etype (Comp);
1161 if Is_Tag (Comp) then
1162 Comp_Byte_Aligned := True;
1163 Component_Aliased := False;
1165 else
1166 -- If a component clause is present, check if the component starts
1167 -- on a storage element boundary. Otherwise conservatively assume
1168 -- it does so only in the case where the record is not packed.
1170 if Present (Component_Clause (Comp)) then
1171 Comp_Byte_Aligned :=
1172 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0;
1173 else
1174 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1175 end if;
1177 Component_Aliased := Is_Aliased (Comp);
1178 end if;
1180 -- Array case
1182 else
1183 Err_Node := Encl_Type;
1184 Comp_Type := Component_Type (Encl_Type);
1186 Component_Aliased := Has_Aliased_Components (Encl_Type);
1187 end if;
1189 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1190 -- the attribute definition clause is attached to the first subtype.
1192 Comp_Type := Base_Type (Comp_Type);
1193 Comp_ADC := Get_Attribute_Definition_Clause
1194 (First_Subtype (Comp_Type),
1195 Attribute_Scalar_Storage_Order);
1196 Comp_ADC_Present := Present (Comp_ADC);
1198 -- Case of record or array component: check storage order compatibility
1200 if Is_Record_Type (Comp_Type) or else Is_Array_Type (Comp_Type) then
1201 Comp_SSO_Differs :=
1202 Reverse_Storage_Order (Encl_Type)
1204 Reverse_Storage_Order (Comp_Type);
1206 -- Parent and extension must have same storage order
1208 if Present (Comp) and then Chars (Comp) = Name_uParent then
1209 if Comp_SSO_Differs then
1210 Error_Msg_N
1211 ("record extension must have same scalar storage order as "
1212 & "parent", Err_Node);
1213 end if;
1215 -- If enclosing composite has explicit SSO then nested composite must
1216 -- have explicit SSO as well.
1218 elsif Present (ADC) and then No (Comp_ADC) then
1219 Error_Msg_N ("nested composite must have explicit scalar "
1220 & "storage order", Err_Node);
1222 -- If component and composite SSO differs, check that component
1223 -- falls on byte boundaries and isn't packed.
1225 elsif Comp_SSO_Differs then
1227 -- Component SSO differs from enclosing composite:
1229 -- Reject if component is a packed array, as it may be represented
1230 -- as a scalar internally.
1232 if Is_Packed_Array (Comp_Type) then
1233 Error_Msg_N
1234 ("type of packed component must have same scalar "
1235 & "storage order as enclosing composite", Err_Node);
1237 -- Reject if composite is a packed array, as it may be rewritten
1238 -- into an array of scalars.
1240 elsif Is_Packed_Array (Encl_Type) then
1241 Error_Msg_N ("type of packed array must have same scalar "
1242 & "storage order as component", Err_Node);
1244 -- Reject if not byte aligned
1246 elsif Is_Record_Type (Encl_Type)
1247 and then not Comp_Byte_Aligned
1248 then
1249 Error_Msg_N
1250 ("type of non-byte-aligned component must have same scalar "
1251 & "storage order as enclosing composite", Err_Node);
1252 end if;
1253 end if;
1255 -- Enclosing type has explicit SSO: non-composite component must not
1256 -- be aliased.
1258 elsif Present (ADC) and then Component_Aliased then
1259 Error_Msg_N
1260 ("aliased component not permitted for type with "
1261 & "explicit Scalar_Storage_Order", Err_Node);
1262 end if;
1263 end Check_Component_Storage_Order;
1265 -----------------------------
1266 -- Check_Debug_Info_Needed --
1267 -----------------------------
1269 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1270 begin
1271 if Debug_Info_Off (T) then
1272 return;
1274 elsif Comes_From_Source (T)
1275 or else Debug_Generated_Code
1276 or else Debug_Flag_VV
1277 or else Needs_Debug_Info (T)
1278 then
1279 Set_Debug_Info_Needed (T);
1280 end if;
1281 end Check_Debug_Info_Needed;
1283 -------------------------------
1284 -- Check_Expression_Function --
1285 -------------------------------
1287 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1288 Decl : Node_Id;
1290 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1291 -- Function to search for deferred constant
1293 -------------------
1294 -- Find_Constant --
1295 -------------------
1297 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1298 begin
1299 -- When a constant is initialized with the result of a dispatching
1300 -- call, the constant declaration is rewritten as a renaming of the
1301 -- displaced function result. This scenario is not a premature use of
1302 -- a constant even though the Has_Completion flag is not set.
1304 if Is_Entity_Name (Nod)
1305 and then Present (Entity (Nod))
1306 and then Ekind (Entity (Nod)) = E_Constant
1307 and then Scope (Entity (Nod)) = Current_Scope
1308 and then Nkind (Declaration_Node (Entity (Nod))) =
1309 N_Object_Declaration
1310 and then not Is_Imported (Entity (Nod))
1311 and then not Has_Completion (Entity (Nod))
1312 then
1313 Error_Msg_NE
1314 ("premature use of& in call or instance", N, Entity (Nod));
1316 elsif Nkind (Nod) = N_Attribute_Reference then
1317 Analyze (Prefix (Nod));
1319 if Is_Entity_Name (Prefix (Nod))
1320 and then Is_Type (Entity (Prefix (Nod)))
1321 then
1322 Freeze_Before (N, Entity (Prefix (Nod)));
1323 end if;
1324 end if;
1326 return OK;
1327 end Find_Constant;
1329 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1331 -- Start of processing for Check_Expression_Function
1333 begin
1334 Decl := Original_Node (Unit_Declaration_Node (Nam));
1336 if Scope (Nam) = Current_Scope
1337 and then Nkind (Decl) = N_Expression_Function
1338 then
1339 Check_Deferred (Expression (Decl));
1340 end if;
1341 end Check_Expression_Function;
1343 ----------------------------
1344 -- Check_Strict_Alignment --
1345 ----------------------------
1347 procedure Check_Strict_Alignment (E : Entity_Id) is
1348 Comp : Entity_Id;
1350 begin
1351 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1352 Set_Strict_Alignment (E);
1354 elsif Is_Array_Type (E) then
1355 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1357 elsif Is_Record_Type (E) then
1358 if Is_Limited_Record (E) then
1359 Set_Strict_Alignment (E);
1360 return;
1361 end if;
1363 Comp := First_Component (E);
1364 while Present (Comp) loop
1365 if not Is_Type (Comp)
1366 and then (Strict_Alignment (Etype (Comp))
1367 or else Is_Aliased (Comp))
1368 then
1369 Set_Strict_Alignment (E);
1370 return;
1371 end if;
1373 Next_Component (Comp);
1374 end loop;
1375 end if;
1376 end Check_Strict_Alignment;
1378 -------------------------
1379 -- Check_Unsigned_Type --
1380 -------------------------
1382 procedure Check_Unsigned_Type (E : Entity_Id) is
1383 Ancestor : Entity_Id;
1384 Lo_Bound : Node_Id;
1385 Btyp : Entity_Id;
1387 begin
1388 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1389 return;
1390 end if;
1392 -- Do not attempt to analyze case where range was in error
1394 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
1395 return;
1396 end if;
1398 -- The situation that is non trivial is something like
1400 -- subtype x1 is integer range -10 .. +10;
1401 -- subtype x2 is x1 range 0 .. V1;
1402 -- subtype x3 is x2 range V2 .. V3;
1403 -- subtype x4 is x3 range V4 .. V5;
1405 -- where Vn are variables. Here the base type is signed, but we still
1406 -- know that x4 is unsigned because of the lower bound of x2.
1408 -- The only way to deal with this is to look up the ancestor chain
1410 Ancestor := E;
1411 loop
1412 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1413 return;
1414 end if;
1416 Lo_Bound := Type_Low_Bound (Ancestor);
1418 if Compile_Time_Known_Value (Lo_Bound) then
1419 if Expr_Rep_Value (Lo_Bound) >= 0 then
1420 Set_Is_Unsigned_Type (E, True);
1421 end if;
1423 return;
1425 else
1426 Ancestor := Ancestor_Subtype (Ancestor);
1428 -- If no ancestor had a static lower bound, go to base type
1430 if No (Ancestor) then
1432 -- Note: the reason we still check for a compile time known
1433 -- value for the base type is that at least in the case of
1434 -- generic formals, we can have bounds that fail this test,
1435 -- and there may be other cases in error situations.
1437 Btyp := Base_Type (E);
1439 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1440 return;
1441 end if;
1443 Lo_Bound := Type_Low_Bound (Base_Type (E));
1445 if Compile_Time_Known_Value (Lo_Bound)
1446 and then Expr_Rep_Value (Lo_Bound) >= 0
1447 then
1448 Set_Is_Unsigned_Type (E, True);
1449 end if;
1451 return;
1452 end if;
1453 end if;
1454 end loop;
1455 end Check_Unsigned_Type;
1457 -------------------------
1458 -- Is_Atomic_Aggregate --
1459 -------------------------
1461 function Is_Atomic_Aggregate
1462 (E : Entity_Id;
1463 Typ : Entity_Id) return Boolean
1465 Loc : constant Source_Ptr := Sloc (E);
1466 New_N : Node_Id;
1467 Par : Node_Id;
1468 Temp : Entity_Id;
1470 begin
1471 Par := Parent (E);
1473 -- Array may be qualified, so find outer context
1475 if Nkind (Par) = N_Qualified_Expression then
1476 Par := Parent (Par);
1477 end if;
1479 if Nkind_In (Par, N_Object_Declaration, N_Assignment_Statement)
1480 and then Comes_From_Source (Par)
1481 then
1482 Temp := Make_Temporary (Loc, 'T', E);
1483 New_N :=
1484 Make_Object_Declaration (Loc,
1485 Defining_Identifier => Temp,
1486 Object_Definition => New_Occurrence_Of (Typ, Loc),
1487 Expression => Relocate_Node (E));
1488 Insert_Before (Par, New_N);
1489 Analyze (New_N);
1491 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1492 return True;
1494 else
1495 return False;
1496 end if;
1497 end Is_Atomic_Aggregate;
1499 -----------------------------------------------
1500 -- Explode_Initialization_Compound_Statement --
1501 -----------------------------------------------
1503 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
1504 Init_Stmts : constant Node_Id := Initialization_Statements (E);
1506 begin
1507 if Present (Init_Stmts)
1508 and then Nkind (Init_Stmts) = N_Compound_Statement
1509 then
1510 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
1512 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
1513 -- just removing it, because Freeze_All may rely on this particular
1514 -- Node_Id still being present in the enclosing list to know where to
1515 -- stop freezing.
1517 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
1519 Set_Initialization_Statements (E, Empty);
1520 end if;
1521 end Explode_Initialization_Compound_Statement;
1523 ----------------
1524 -- Freeze_All --
1525 ----------------
1527 -- Note: the easy coding for this procedure would be to just build a
1528 -- single list of freeze nodes and then insert them and analyze them
1529 -- all at once. This won't work, because the analysis of earlier freeze
1530 -- nodes may recursively freeze types which would otherwise appear later
1531 -- on in the freeze list. So we must analyze and expand the freeze nodes
1532 -- as they are generated.
1534 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1535 E : Entity_Id;
1536 Decl : Node_Id;
1538 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1539 -- This is the internal recursive routine that does freezing of entities
1540 -- (but NOT the analysis of default expressions, which should not be
1541 -- recursive, we don't want to analyze those till we are sure that ALL
1542 -- the types are frozen).
1544 --------------------
1545 -- Freeze_All_Ent --
1546 --------------------
1548 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
1549 E : Entity_Id;
1550 Flist : List_Id;
1551 Lastn : Node_Id;
1553 procedure Process_Flist;
1554 -- If freeze nodes are present, insert and analyze, and reset cursor
1555 -- for next insertion.
1557 -------------------
1558 -- Process_Flist --
1559 -------------------
1561 procedure Process_Flist is
1562 begin
1563 if Is_Non_Empty_List (Flist) then
1564 Lastn := Next (After);
1565 Insert_List_After_And_Analyze (After, Flist);
1567 if Present (Lastn) then
1568 After := Prev (Lastn);
1569 else
1570 After := Last (List_Containing (After));
1571 end if;
1572 end if;
1573 end Process_Flist;
1575 -- Start or processing for Freeze_All_Ent
1577 begin
1578 E := From;
1579 while Present (E) loop
1581 -- If the entity is an inner package which is not a package
1582 -- renaming, then its entities must be frozen at this point. Note
1583 -- that such entities do NOT get frozen at the end of the nested
1584 -- package itself (only library packages freeze).
1586 -- Same is true for task declarations, where anonymous records
1587 -- created for entry parameters must be frozen.
1589 if Ekind (E) = E_Package
1590 and then No (Renamed_Object (E))
1591 and then not Is_Child_Unit (E)
1592 and then not Is_Frozen (E)
1593 then
1594 Push_Scope (E);
1595 Install_Visible_Declarations (E);
1596 Install_Private_Declarations (E);
1598 Freeze_All (First_Entity (E), After);
1600 End_Package_Scope (E);
1602 if Is_Generic_Instance (E)
1603 and then Has_Delayed_Freeze (E)
1604 then
1605 Set_Has_Delayed_Freeze (E, False);
1606 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1607 end if;
1609 elsif Ekind (E) in Task_Kind
1610 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1611 N_Single_Task_Declaration)
1612 then
1613 Push_Scope (E);
1614 Freeze_All (First_Entity (E), After);
1615 End_Scope;
1617 -- For a derived tagged type, we must ensure that all the
1618 -- primitive operations of the parent have been frozen, so that
1619 -- their addresses will be in the parent's dispatch table at the
1620 -- point it is inherited.
1622 elsif Ekind (E) = E_Record_Type
1623 and then Is_Tagged_Type (E)
1624 and then Is_Tagged_Type (Etype (E))
1625 and then Is_Derived_Type (E)
1626 then
1627 declare
1628 Prim_List : constant Elist_Id :=
1629 Primitive_Operations (Etype (E));
1631 Prim : Elmt_Id;
1632 Subp : Entity_Id;
1634 begin
1635 Prim := First_Elmt (Prim_List);
1636 while Present (Prim) loop
1637 Subp := Node (Prim);
1639 if Comes_From_Source (Subp)
1640 and then not Is_Frozen (Subp)
1641 then
1642 Flist := Freeze_Entity (Subp, After);
1643 Process_Flist;
1644 end if;
1646 Next_Elmt (Prim);
1647 end loop;
1648 end;
1649 end if;
1651 if not Is_Frozen (E) then
1652 Flist := Freeze_Entity (E, After);
1653 Process_Flist;
1655 -- If already frozen, and there are delayed aspects, this is where
1656 -- we do the visibility check for these aspects (see Sem_Ch13 spec
1657 -- for a description of how we handle aspect visibility).
1659 elsif Has_Delayed_Aspects (E) then
1661 -- Retrieve the visibility to the discriminants in order to
1662 -- analyze properly the aspects.
1664 Push_Scope_And_Install_Discriminants (E);
1666 declare
1667 Ritem : Node_Id;
1669 begin
1670 Ritem := First_Rep_Item (E);
1671 while Present (Ritem) loop
1672 if Nkind (Ritem) = N_Aspect_Specification
1673 and then Entity (Ritem) = E
1674 and then Is_Delayed_Aspect (Ritem)
1675 then
1676 Check_Aspect_At_End_Of_Declarations (Ritem);
1677 end if;
1679 Ritem := Next_Rep_Item (Ritem);
1680 end loop;
1681 end;
1683 Uninstall_Discriminants_And_Pop_Scope (E);
1684 end if;
1686 -- If an incomplete type is still not frozen, this may be a
1687 -- premature freezing because of a body declaration that follows.
1688 -- Indicate where the freezing took place. Freezing will happen
1689 -- if the body comes from source, but not if it is internally
1690 -- generated, for example as the body of a type invariant.
1692 -- If the freezing is caused by the end of the current declarative
1693 -- part, it is a Taft Amendment type, and there is no error.
1695 if not Is_Frozen (E)
1696 and then Ekind (E) = E_Incomplete_Type
1697 then
1698 declare
1699 Bod : constant Node_Id := Next (After);
1701 begin
1702 -- The presence of a body freezes all entities previously
1703 -- declared in the current list of declarations, but this
1704 -- does not apply if the body does not come from source.
1705 -- A type invariant is transformed into a subprogram body
1706 -- which is placed at the end of the private part of the
1707 -- current package, but this body does not freeze incomplete
1708 -- types that may be declared in this private part.
1710 if (Nkind_In (Bod, N_Subprogram_Body,
1711 N_Entry_Body,
1712 N_Package_Body,
1713 N_Protected_Body,
1714 N_Task_Body)
1715 or else Nkind (Bod) in N_Body_Stub)
1716 and then
1717 List_Containing (After) = List_Containing (Parent (E))
1718 and then Comes_From_Source (Bod)
1719 then
1720 Error_Msg_Sloc := Sloc (Next (After));
1721 Error_Msg_NE
1722 ("type& is frozen# before its full declaration",
1723 Parent (E), E);
1724 end if;
1725 end;
1726 end if;
1728 Next_Entity (E);
1729 end loop;
1730 end Freeze_All_Ent;
1732 -- Start of processing for Freeze_All
1734 begin
1735 Freeze_All_Ent (From, After);
1737 -- Now that all types are frozen, we can deal with default expressions
1738 -- that require us to build a default expression functions. This is the
1739 -- point at which such functions are constructed (after all types that
1740 -- might be used in such expressions have been frozen).
1742 -- For subprograms that are renaming_as_body, we create the wrapper
1743 -- bodies as needed.
1745 -- We also add finalization chains to access types whose designated
1746 -- types are controlled. This is normally done when freezing the type,
1747 -- but this misses recursive type definitions where the later members
1748 -- of the recursion introduce controlled components.
1750 -- Loop through entities
1752 E := From;
1753 while Present (E) loop
1754 if Is_Subprogram (E) then
1755 if not Default_Expressions_Processed (E) then
1756 Process_Default_Expressions (E, After);
1757 end if;
1759 if not Has_Completion (E) then
1760 Decl := Unit_Declaration_Node (E);
1762 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1763 if Error_Posted (Decl) then
1764 Set_Has_Completion (E);
1765 else
1766 Build_And_Analyze_Renamed_Body (Decl, E, After);
1767 end if;
1769 elsif Nkind (Decl) = N_Subprogram_Declaration
1770 and then Present (Corresponding_Body (Decl))
1771 and then
1772 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1773 = N_Subprogram_Renaming_Declaration
1774 then
1775 Build_And_Analyze_Renamed_Body
1776 (Decl, Corresponding_Body (Decl), After);
1777 end if;
1778 end if;
1780 elsif Ekind (E) in Task_Kind
1781 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1782 N_Single_Task_Declaration)
1783 then
1784 declare
1785 Ent : Entity_Id;
1787 begin
1788 Ent := First_Entity (E);
1789 while Present (Ent) loop
1790 if Is_Entry (Ent)
1791 and then not Default_Expressions_Processed (Ent)
1792 then
1793 Process_Default_Expressions (Ent, After);
1794 end if;
1796 Next_Entity (Ent);
1797 end loop;
1798 end;
1799 end if;
1801 -- Historical note: We used to create a finalization master for an
1802 -- access type whose designated type is not controlled, but contains
1803 -- private controlled compoments. This form of postprocessing is no
1804 -- longer needed because the finalization master is now created when
1805 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
1807 Next_Entity (E);
1808 end loop;
1809 end Freeze_All;
1811 -----------------------
1812 -- Freeze_And_Append --
1813 -----------------------
1815 procedure Freeze_And_Append
1816 (Ent : Entity_Id;
1817 N : Node_Id;
1818 Result : in out List_Id)
1820 L : constant List_Id := Freeze_Entity (Ent, N);
1821 begin
1822 if Is_Non_Empty_List (L) then
1823 if Result = No_List then
1824 Result := L;
1825 else
1826 Append_List (L, Result);
1827 end if;
1828 end if;
1829 end Freeze_And_Append;
1831 -------------------
1832 -- Freeze_Before --
1833 -------------------
1835 procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
1836 Freeze_Nodes : constant List_Id := Freeze_Entity (T, N);
1838 begin
1839 if Ekind (T) = E_Function then
1840 Check_Expression_Function (N, T);
1841 end if;
1843 if Is_Non_Empty_List (Freeze_Nodes) then
1844 Insert_Actions (N, Freeze_Nodes);
1845 end if;
1846 end Freeze_Before;
1848 -------------------
1849 -- Freeze_Entity --
1850 -------------------
1852 function Freeze_Entity (E : Entity_Id; N : Node_Id) return List_Id is
1853 GM : constant Ghost_Mode_Type := Ghost_Mode;
1854 -- Save the current Ghost mode in effect in case the entity being frozen
1855 -- sets a different mode.
1857 Loc : constant Source_Ptr := Sloc (N);
1858 Atype : Entity_Id;
1859 Comp : Entity_Id;
1860 F_Node : Node_Id;
1861 Formal : Entity_Id;
1862 Indx : Node_Id;
1864 Test_E : Entity_Id := E;
1865 -- This could use a comment ???
1867 Late_Freezing : Boolean := False;
1868 -- Used to detect attempt to freeze function declared in another unit
1870 Result : List_Id := No_List;
1871 -- List of freezing actions, left at No_List if none
1873 Has_Default_Initialization : Boolean := False;
1874 -- This flag gets set to true for a variable with default initialization
1876 procedure Add_To_Result (N : Node_Id);
1877 -- N is a freezing action to be appended to the Result
1879 function After_Last_Declaration return Boolean;
1880 -- If Loc is a freeze_entity that appears after the last declaration
1881 -- in the scope, inhibit error messages on late completion.
1883 procedure Check_Current_Instance (Comp_Decl : Node_Id);
1884 -- Check that an Access or Unchecked_Access attribute with a prefix
1885 -- which is the current instance type can only be applied when the type
1886 -- is limited.
1888 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
1889 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
1890 -- integer literal without an explicit corresponding size clause. The
1891 -- caller has checked that Utype is a modular integer type.
1893 procedure Freeze_Array_Type (Arr : Entity_Id);
1894 -- Freeze array type, including freezing index and component types
1896 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
1897 -- Create Freeze_Generic_Entity nodes for types declared in a generic
1898 -- package. Recurse on inner generic packages.
1900 function Freeze_Profile (E : Entity_Id) return Boolean;
1901 -- Freeze formals and return type of subprogram. If some type in the
1902 -- profile is a limited view, freezing of the entity will take place
1903 -- elsewhere, and the function returns False. This routine will be
1904 -- modified if and when we can implement AI05-019 efficiently ???
1906 procedure Freeze_Record_Type (Rec : Entity_Id);
1907 -- Freeze record type, including freezing component types, and freezing
1908 -- primitive operations if this is a tagged type.
1910 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
1911 -- Determine whether an arbitrary entity is subject to Boolean aspect
1912 -- Import and its value is specified as True.
1914 procedure Late_Freeze_Subprogram (E : Entity_Id);
1915 -- Following AI05-151, a function can return a limited view of a type
1916 -- declared elsewhere. In that case the function cannot be frozen at
1917 -- the end of its enclosing package. If its first use is in a different
1918 -- unit, it cannot be frozen there, but if the call is legal the full
1919 -- view of the return type is available and the subprogram can now be
1920 -- frozen. However the freeze node cannot be inserted at the point of
1921 -- call, but rather must go in the package holding the function, so that
1922 -- the backend can process it in the proper context.
1924 procedure Restore_Globals;
1925 -- Restore the values of all saved global variables
1927 procedure Wrap_Imported_Subprogram (E : Entity_Id);
1928 -- If E is an entity for an imported subprogram with pre/post-conditions
1929 -- then this procedure will create a wrapper to ensure that proper run-
1930 -- time checking of the pre/postconditions. See body for details.
1932 -------------------
1933 -- Add_To_Result --
1934 -------------------
1936 procedure Add_To_Result (N : Node_Id) is
1937 begin
1938 if No (Result) then
1939 Result := New_List (N);
1940 else
1941 Append (N, Result);
1942 end if;
1943 end Add_To_Result;
1945 ----------------------------
1946 -- After_Last_Declaration --
1947 ----------------------------
1949 function After_Last_Declaration return Boolean is
1950 Spec : constant Node_Id := Parent (Current_Scope);
1952 begin
1953 if Nkind (Spec) = N_Package_Specification then
1954 if Present (Private_Declarations (Spec)) then
1955 return Loc >= Sloc (Last (Private_Declarations (Spec)));
1956 elsif Present (Visible_Declarations (Spec)) then
1957 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
1958 else
1959 return False;
1960 end if;
1962 else
1963 return False;
1964 end if;
1965 end After_Last_Declaration;
1967 ----------------------------
1968 -- Check_Current_Instance --
1969 ----------------------------
1971 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
1973 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
1974 -- Determine whether Typ is compatible with the rules for aliased
1975 -- views of types as defined in RM 3.10 in the various dialects.
1977 function Process (N : Node_Id) return Traverse_Result;
1978 -- Process routine to apply check to given node
1980 -----------------------------
1981 -- Is_Aliased_View_Of_Type --
1982 -----------------------------
1984 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
1985 Typ_Decl : constant Node_Id := Parent (Typ);
1987 begin
1988 -- Common case
1990 if Nkind (Typ_Decl) = N_Full_Type_Declaration
1991 and then Limited_Present (Type_Definition (Typ_Decl))
1992 then
1993 return True;
1995 -- The following paragraphs describe what a legal aliased view of
1996 -- a type is in the various dialects of Ada.
1998 -- Ada 95
2000 -- The current instance of a limited type, and a formal parameter
2001 -- or generic formal object of a tagged type.
2003 -- Ada 95 limited type
2004 -- * Type with reserved word "limited"
2005 -- * A protected or task type
2006 -- * A composite type with limited component
2008 elsif Ada_Version <= Ada_95 then
2009 return Is_Limited_Type (Typ);
2011 -- Ada 2005
2013 -- The current instance of a limited tagged type, a protected
2014 -- type, a task type, or a type that has the reserved word
2015 -- "limited" in its full definition ... a formal parameter or
2016 -- generic formal object of a tagged type.
2018 -- Ada 2005 limited type
2019 -- * Type with reserved word "limited", "synchronized", "task"
2020 -- or "protected"
2021 -- * A composite type with limited component
2022 -- * A derived type whose parent is a non-interface limited type
2024 elsif Ada_Version = Ada_2005 then
2025 return
2026 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2027 or else
2028 (Is_Derived_Type (Typ)
2029 and then not Is_Interface (Etype (Typ))
2030 and then Is_Limited_Type (Etype (Typ)));
2032 -- Ada 2012 and beyond
2034 -- The current instance of an immutably limited type ... a formal
2035 -- parameter or generic formal object of a tagged type.
2037 -- Ada 2012 limited type
2038 -- * Type with reserved word "limited", "synchronized", "task"
2039 -- or "protected"
2040 -- * A composite type with limited component
2041 -- * A derived type whose parent is a non-interface limited type
2042 -- * An incomplete view
2044 -- Ada 2012 immutably limited type
2045 -- * Explicitly limited record type
2046 -- * Record extension with "limited" present
2047 -- * Non-formal limited private type that is either tagged
2048 -- or has at least one access discriminant with a default
2049 -- expression
2050 -- * Task type, protected type or synchronized interface
2051 -- * Type derived from immutably limited type
2053 else
2054 return
2055 Is_Immutably_Limited_Type (Typ)
2056 or else Is_Incomplete_Type (Typ);
2057 end if;
2058 end Is_Aliased_View_Of_Type;
2060 -------------
2061 -- Process --
2062 -------------
2064 function Process (N : Node_Id) return Traverse_Result is
2065 begin
2066 case Nkind (N) is
2067 when N_Attribute_Reference =>
2068 if Nam_In (Attribute_Name (N), Name_Access,
2069 Name_Unchecked_Access)
2070 and then Is_Entity_Name (Prefix (N))
2071 and then Is_Type (Entity (Prefix (N)))
2072 and then Entity (Prefix (N)) = E
2073 then
2074 if Ada_Version < Ada_2012 then
2075 Error_Msg_N
2076 ("current instance must be a limited type",
2077 Prefix (N));
2078 else
2079 Error_Msg_N
2080 ("current instance must be an immutably limited "
2081 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2082 end if;
2084 return Abandon;
2086 else
2087 return OK;
2088 end if;
2090 when others => return OK;
2091 end case;
2092 end Process;
2094 procedure Traverse is new Traverse_Proc (Process);
2096 -- Local variables
2098 Rec_Type : constant Entity_Id :=
2099 Scope (Defining_Identifier (Comp_Decl));
2101 -- Start of processing for Check_Current_Instance
2103 begin
2104 if not Is_Aliased_View_Of_Type (Rec_Type) then
2105 Traverse (Comp_Decl);
2106 end if;
2107 end Check_Current_Instance;
2109 ------------------------------
2110 -- Check_Suspicious_Modulus --
2111 ------------------------------
2113 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
2114 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
2116 begin
2117 if not Warn_On_Suspicious_Modulus_Value then
2118 return;
2119 end if;
2121 if Nkind (Decl) = N_Full_Type_Declaration then
2122 declare
2123 Tdef : constant Node_Id := Type_Definition (Decl);
2125 begin
2126 if Nkind (Tdef) = N_Modular_Type_Definition then
2127 declare
2128 Modulus : constant Node_Id :=
2129 Original_Node (Expression (Tdef));
2131 begin
2132 if Nkind (Modulus) = N_Integer_Literal then
2133 declare
2134 Modv : constant Uint := Intval (Modulus);
2135 Sizv : constant Uint := RM_Size (Utype);
2137 begin
2138 -- First case, modulus and size are the same. This
2139 -- happens if you have something like mod 32, with
2140 -- an explicit size of 32, this is for sure a case
2141 -- where the warning is given, since it is seems
2142 -- very unlikely that someone would want e.g. a
2143 -- five bit type stored in 32 bits. It is much
2144 -- more likely they wanted a 32-bit type.
2146 if Modv = Sizv then
2147 null;
2149 -- Second case, the modulus is 32 or 64 and no
2150 -- size clause is present. This is a less clear
2151 -- case for giving the warning, but in the case
2152 -- of 32/64 (5-bit or 6-bit types) these seem rare
2153 -- enough that it is a likely error (and in any
2154 -- case using 2**5 or 2**6 in these cases seems
2155 -- clearer. We don't include 8 or 16 here, simply
2156 -- because in practice 3-bit and 4-bit types are
2157 -- more common and too many false positives if
2158 -- we warn in these cases.
2160 elsif not Has_Size_Clause (Utype)
2161 and then (Modv = Uint_32 or else Modv = Uint_64)
2162 then
2163 null;
2165 -- No warning needed
2167 else
2168 return;
2169 end if;
2171 -- If we fall through, give warning
2173 Error_Msg_Uint_1 := Modv;
2174 Error_Msg_N
2175 ("?M?2 '*'*^' may have been intended here",
2176 Modulus);
2177 end;
2178 end if;
2179 end;
2180 end if;
2181 end;
2182 end if;
2183 end Check_Suspicious_Modulus;
2185 -----------------------
2186 -- Freeze_Array_Type --
2187 -----------------------
2189 procedure Freeze_Array_Type (Arr : Entity_Id) is
2190 FS : constant Entity_Id := First_Subtype (Arr);
2191 Ctyp : constant Entity_Id := Component_Type (Arr);
2192 Clause : Entity_Id;
2194 Non_Standard_Enum : Boolean := False;
2195 -- Set true if any of the index types is an enumeration type with a
2196 -- non-standard representation.
2198 begin
2199 Freeze_And_Append (Ctyp, N, Result);
2201 Indx := First_Index (Arr);
2202 while Present (Indx) loop
2203 Freeze_And_Append (Etype (Indx), N, Result);
2205 if Is_Enumeration_Type (Etype (Indx))
2206 and then Has_Non_Standard_Rep (Etype (Indx))
2207 then
2208 Non_Standard_Enum := True;
2209 end if;
2211 Next_Index (Indx);
2212 end loop;
2214 -- Processing that is done only for base types
2216 if Ekind (Arr) = E_Array_Type then
2218 -- Deal with default setting of reverse storage order
2220 Set_SSO_From_Default (Arr);
2222 -- Propagate flags for component type
2224 if Is_Controlled (Component_Type (Arr))
2225 or else Has_Controlled_Component (Ctyp)
2226 then
2227 Set_Has_Controlled_Component (Arr);
2228 end if;
2230 if Has_Unchecked_Union (Component_Type (Arr)) then
2231 Set_Has_Unchecked_Union (Arr);
2232 end if;
2234 -- Warn for pragma Pack overriding foreign convention
2236 if Has_Foreign_Convention (Ctyp)
2237 and then Has_Pragma_Pack (Arr)
2238 then
2239 declare
2240 CN : constant Name_Id :=
2241 Get_Convention_Name (Convention (Ctyp));
2242 PP : constant Node_Id :=
2243 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
2244 begin
2245 if Present (PP) then
2246 Error_Msg_Name_1 := CN;
2247 Error_Msg_Sloc := Sloc (Arr);
2248 Error_Msg_N
2249 ("pragma Pack affects convention % components #??", PP);
2250 Error_Msg_Name_1 := CN;
2251 Error_Msg_N
2252 ("\array components may not have % compatible "
2253 & "representation??", PP);
2254 end if;
2255 end;
2256 end if;
2258 -- If packing was requested or if the component size was
2259 -- set explicitly, then see if bit packing is required. This
2260 -- processing is only done for base types, since all of the
2261 -- representation aspects involved are type-related.
2263 -- This is not just an optimization, if we start processing the
2264 -- subtypes, they interfere with the settings on the base type
2265 -- (this is because Is_Packed has a slightly different meaning
2266 -- before and after freezing).
2268 declare
2269 Csiz : Uint;
2270 Esiz : Uint;
2272 begin
2273 if (Is_Packed (Arr) or else Has_Pragma_Pack (Arr))
2274 and then Known_Static_RM_Size (Ctyp)
2275 and then not Has_Component_Size_Clause (Arr)
2276 then
2277 Csiz := UI_Max (RM_Size (Ctyp), 1);
2279 elsif Known_Component_Size (Arr) then
2280 Csiz := Component_Size (Arr);
2282 elsif not Known_Static_Esize (Ctyp) then
2283 Csiz := Uint_0;
2285 else
2286 Esiz := Esize (Ctyp);
2288 -- We can set the component size if it is less than 16,
2289 -- rounding it up to the next storage unit size.
2291 if Esiz <= 8 then
2292 Csiz := Uint_8;
2293 elsif Esiz <= 16 then
2294 Csiz := Uint_16;
2295 else
2296 Csiz := Uint_0;
2297 end if;
2299 -- Set component size up to match alignment if it would
2300 -- otherwise be less than the alignment. This deals with
2301 -- cases of types whose alignment exceeds their size (the
2302 -- padded type cases).
2304 if Csiz /= 0 then
2305 declare
2306 A : constant Uint := Alignment_In_Bits (Ctyp);
2307 begin
2308 if Csiz < A then
2309 Csiz := A;
2310 end if;
2311 end;
2312 end if;
2313 end if;
2315 -- Case of component size that may result in packing
2317 if 1 <= Csiz and then Csiz <= 64 then
2318 declare
2319 Ent : constant Entity_Id :=
2320 First_Subtype (Arr);
2321 Pack_Pragma : constant Node_Id :=
2322 Get_Rep_Pragma (Ent, Name_Pack);
2323 Comp_Size_C : constant Node_Id :=
2324 Get_Attribute_Definition_Clause
2325 (Ent, Attribute_Component_Size);
2327 begin
2328 -- Warn if we have pack and component size so that the
2329 -- pack is ignored.
2331 -- Note: here we must check for the presence of a
2332 -- component size before checking for a Pack pragma to
2333 -- deal with the case where the array type is a derived
2334 -- type whose parent is currently private.
2336 if Present (Comp_Size_C)
2337 and then Has_Pragma_Pack (Ent)
2338 and then Warn_On_Redundant_Constructs
2339 then
2340 Error_Msg_Sloc := Sloc (Comp_Size_C);
2341 Error_Msg_NE
2342 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
2343 Error_Msg_N
2344 ("\?r?explicit component size given#!", Pack_Pragma);
2345 Set_Is_Packed (Base_Type (Ent), False);
2346 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
2347 end if;
2349 -- Set component size if not already set by a component
2350 -- size clause.
2352 if not Present (Comp_Size_C) then
2353 Set_Component_Size (Arr, Csiz);
2354 end if;
2356 -- Check for base type of 8, 16, 32 bits, where an
2357 -- unsigned subtype has a length one less than the
2358 -- base type (e.g. Natural subtype of Integer).
2360 -- In such cases, if a component size was not set
2361 -- explicitly, then generate a warning.
2363 if Has_Pragma_Pack (Arr)
2364 and then not Present (Comp_Size_C)
2365 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2366 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2367 then
2368 Error_Msg_Uint_1 := Csiz;
2370 if Present (Pack_Pragma) then
2371 Error_Msg_N
2372 ("??pragma Pack causes component size to be ^!",
2373 Pack_Pragma);
2374 Error_Msg_N
2375 ("\??use Component_Size to set desired value!",
2376 Pack_Pragma);
2377 end if;
2378 end if;
2380 -- Actual packing is not needed for 8, 16, 32, 64. Also
2381 -- not needed for 24 if alignment is 1.
2383 if Csiz = 8
2384 or else Csiz = 16
2385 or else Csiz = 32
2386 or else Csiz = 64
2387 or else (Csiz = 24 and then Alignment (Ctyp) = 1)
2388 then
2389 -- Here the array was requested to be packed, but
2390 -- the packing request had no effect, so Is_Packed
2391 -- is reset.
2393 -- Note: semantically this means that we lose track
2394 -- of the fact that a derived type inherited a pragma
2395 -- Pack that was non- effective, but that seems fine.
2397 -- We regard a Pack pragma as a request to set a
2398 -- representation characteristic, and this request
2399 -- may be ignored.
2401 Set_Is_Packed (Base_Type (Arr), False);
2402 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2404 if Known_Static_Esize (Component_Type (Arr))
2405 and then Esize (Component_Type (Arr)) = Csiz
2406 then
2407 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
2408 end if;
2410 -- In all other cases, packing is indeed needed
2412 else
2413 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2414 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
2415 Set_Is_Packed (Base_Type (Arr), True);
2416 end if;
2417 end;
2418 end if;
2419 end;
2421 -- Check for Aliased or Atomic_Components/Atomic with unsuitable
2422 -- packing or explicit component size clause given.
2424 if (Has_Aliased_Components (Arr)
2425 or else Has_Atomic_Components (Arr)
2426 or else Is_Atomic (Ctyp))
2427 and then
2428 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2429 then
2430 Alias_Atomic_Check : declare
2432 procedure Complain_CS (T : String);
2433 -- Outputs error messages for incorrect CS clause or pragma
2434 -- Pack for aliased or atomic components (T is "aliased" or
2435 -- "atomic");
2437 -----------------
2438 -- Complain_CS --
2439 -----------------
2441 procedure Complain_CS (T : String) is
2442 begin
2443 if Has_Component_Size_Clause (Arr) then
2444 Clause :=
2445 Get_Attribute_Definition_Clause
2446 (FS, Attribute_Component_Size);
2448 Error_Msg_N
2449 ("incorrect component size for "
2450 & T & " components", Clause);
2451 Error_Msg_Uint_1 := Esize (Ctyp);
2452 Error_Msg_N
2453 ("\only allowed value is^", Clause);
2455 else
2456 Error_Msg_N
2457 ("cannot pack " & T & " components",
2458 Get_Rep_Pragma (FS, Name_Pack));
2459 end if;
2460 end Complain_CS;
2462 -- Start of processing for Alias_Atomic_Check
2464 begin
2465 -- If object size of component type isn't known, we cannot
2466 -- be sure so we defer to the back end.
2468 if not Known_Static_Esize (Ctyp) then
2469 null;
2471 -- Case where component size has no effect. First check for
2472 -- object size of component type multiple of the storage
2473 -- unit size.
2475 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2477 -- OK in both packing case and component size case if RM
2478 -- size is known and static and same as the object size.
2480 and then
2481 ((Known_Static_RM_Size (Ctyp)
2482 and then Esize (Ctyp) = RM_Size (Ctyp))
2484 -- Or if we have an explicit component size clause and
2485 -- the component size and object size are equal.
2487 or else
2488 (Has_Component_Size_Clause (Arr)
2489 and then Component_Size (Arr) = Esize (Ctyp)))
2490 then
2491 null;
2493 elsif Has_Aliased_Components (Arr) then
2494 Complain_CS ("aliased");
2496 elsif Has_Atomic_Components (Arr) or else Is_Atomic (Ctyp)
2497 then
2498 Complain_CS ("atomic");
2499 end if;
2500 end Alias_Atomic_Check;
2501 end if;
2503 -- Warn for case of atomic type
2505 Clause := Get_Rep_Pragma (FS, Name_Atomic);
2507 if Present (Clause)
2508 and then not Addressable (Component_Size (FS))
2509 then
2510 Error_Msg_NE
2511 ("non-atomic components of type& may not be "
2512 & "accessible by separate tasks??", Clause, Arr);
2514 if Has_Component_Size_Clause (Arr) then
2515 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2516 (FS, Attribute_Component_Size));
2517 Error_Msg_N ("\because of component size clause#??", Clause);
2519 elsif Has_Pragma_Pack (Arr) then
2520 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2521 Error_Msg_N ("\because of pragma Pack#??", Clause);
2522 end if;
2523 end if;
2525 -- Check for scalar storage order
2527 declare
2528 Dummy : Boolean;
2529 begin
2530 Check_Component_Storage_Order
2531 (Encl_Type => Arr,
2532 Comp => Empty,
2533 ADC => Get_Attribute_Definition_Clause
2534 (First_Subtype (Arr),
2535 Attribute_Scalar_Storage_Order),
2536 Comp_ADC_Present => Dummy);
2537 end;
2539 -- Processing that is done only for subtypes
2541 else
2542 -- Acquire alignment from base type
2544 if Unknown_Alignment (Arr) then
2545 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
2546 Adjust_Esize_Alignment (Arr);
2547 end if;
2548 end if;
2550 -- Specific checks for bit-packed arrays
2552 if Is_Bit_Packed_Array (Arr) then
2554 -- Check number of elements for bit packed arrays that come from
2555 -- source and have compile time known ranges. The bit-packed
2556 -- arrays circuitry does not support arrays with more than
2557 -- Integer'Last + 1 elements, and when this restriction is
2558 -- violated, causes incorrect data access.
2560 -- For the case where this is not compile time known, a run-time
2561 -- check should be generated???
2563 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
2564 declare
2565 Elmts : Uint;
2566 Index : Node_Id;
2567 Ilen : Node_Id;
2568 Ityp : Entity_Id;
2570 begin
2571 Elmts := Uint_1;
2572 Index := First_Index (Arr);
2573 while Present (Index) loop
2574 Ityp := Etype (Index);
2576 -- Never generate an error if any index is of a generic
2577 -- type. We will check this in instances.
2579 if Is_Generic_Type (Ityp) then
2580 Elmts := Uint_0;
2581 exit;
2582 end if;
2584 Ilen :=
2585 Make_Attribute_Reference (Loc,
2586 Prefix => New_Occurrence_Of (Ityp, Loc),
2587 Attribute_Name => Name_Range_Length);
2588 Analyze_And_Resolve (Ilen);
2590 -- No attempt is made to check number of elements if not
2591 -- compile time known.
2593 if Nkind (Ilen) /= N_Integer_Literal then
2594 Elmts := Uint_0;
2595 exit;
2596 end if;
2598 Elmts := Elmts * Intval (Ilen);
2599 Next_Index (Index);
2600 end loop;
2602 if Elmts > Intval (High_Bound
2603 (Scalar_Range (Standard_Integer))) + 1
2604 then
2605 Error_Msg_N
2606 ("bit packed array type may not have "
2607 & "more than Integer''Last+1 elements", Arr);
2608 end if;
2609 end;
2610 end if;
2612 -- Check size
2614 if Known_RM_Size (Arr) then
2615 declare
2616 SizC : constant Node_Id := Size_Clause (Arr);
2617 Discard : Boolean;
2619 begin
2620 -- It is not clear if it is possible to have no size clause
2621 -- at this stage, but it is not worth worrying about. Post
2622 -- error on the entity name in the size clause if present,
2623 -- else on the type entity itself.
2625 if Present (SizC) then
2626 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
2627 else
2628 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
2629 end if;
2630 end;
2631 end if;
2632 end if;
2634 -- If any of the index types was an enumeration type with a non-
2635 -- standard rep clause, then we indicate that the array type is
2636 -- always packed (even if it is not bit packed).
2638 if Non_Standard_Enum then
2639 Set_Has_Non_Standard_Rep (Base_Type (Arr));
2640 Set_Is_Packed (Base_Type (Arr));
2641 end if;
2643 Set_Component_Alignment_If_Not_Set (Arr);
2645 -- If the array is packed, we must create the packed array type to be
2646 -- used to actually implement the type. This is only needed for real
2647 -- array types (not for string literal types, since they are present
2648 -- only for the front end).
2650 if Is_Packed (Arr)
2651 and then Ekind (Arr) /= E_String_Literal_Subtype
2652 then
2653 Create_Packed_Array_Impl_Type (Arr);
2654 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
2656 -- Make sure that we have the necessary routines to implement the
2657 -- packing, and complain now if not. Note that we only test this
2658 -- for constrained array types.
2660 if Is_Constrained (Arr)
2661 and then Is_Bit_Packed_Array (Arr)
2662 and then Present (Packed_Array_Impl_Type (Arr))
2663 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
2664 then
2665 declare
2666 CS : constant Uint := Component_Size (Arr);
2667 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
2669 begin
2670 if RE /= RE_Null
2671 and then not RTE_Available (RE)
2672 then
2673 Error_Msg_CRT
2674 ("packing of " & UI_Image (CS) & "-bit components",
2675 First_Subtype (Etype (Arr)));
2677 -- Cancel the packing
2679 Set_Is_Packed (Base_Type (Arr), False);
2680 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2681 Set_Packed_Array_Impl_Type (Arr, Empty);
2682 goto Skip_Packed;
2683 end if;
2684 end;
2685 end if;
2687 -- Size information of packed array type is copied to the array
2688 -- type, since this is really the representation. But do not
2689 -- override explicit existing size values. If the ancestor subtype
2690 -- is constrained the Packed_Array_Impl_Type will be inherited
2691 -- from it, but the size may have been provided already, and
2692 -- must not be overridden either.
2694 if not Has_Size_Clause (Arr)
2695 and then
2696 (No (Ancestor_Subtype (Arr))
2697 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
2698 then
2699 Set_Esize (Arr, Esize (Packed_Array_Impl_Type (Arr)));
2700 Set_RM_Size (Arr, RM_Size (Packed_Array_Impl_Type (Arr)));
2701 end if;
2703 if not Has_Alignment_Clause (Arr) then
2704 Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
2705 end if;
2706 end if;
2708 <<Skip_Packed>>
2710 -- For non-packed arrays set the alignment of the array to the
2711 -- alignment of the component type if it is unknown. Skip this
2712 -- in atomic case (atomic arrays may need larger alignments).
2714 if not Is_Packed (Arr)
2715 and then Unknown_Alignment (Arr)
2716 and then Known_Alignment (Ctyp)
2717 and then Known_Static_Component_Size (Arr)
2718 and then Known_Static_Esize (Ctyp)
2719 and then Esize (Ctyp) = Component_Size (Arr)
2720 and then not Is_Atomic (Arr)
2721 then
2722 Set_Alignment (Arr, Alignment (Component_Type (Arr)));
2723 end if;
2724 end Freeze_Array_Type;
2726 -----------------------------
2727 -- Freeze_Generic_Entities --
2728 -----------------------------
2730 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
2731 E : Entity_Id;
2732 F : Node_Id;
2733 Flist : List_Id;
2735 begin
2736 Flist := New_List;
2737 E := First_Entity (Pack);
2738 while Present (E) loop
2739 if Is_Type (E) and then not Is_Generic_Type (E) then
2740 F := Make_Freeze_Generic_Entity (Sloc (Pack));
2741 Set_Entity (F, E);
2742 Append_To (Flist, F);
2744 elsif Ekind (E) = E_Generic_Package then
2745 Append_List_To (Flist, Freeze_Generic_Entities (E));
2746 end if;
2748 Next_Entity (E);
2749 end loop;
2751 return Flist;
2752 end Freeze_Generic_Entities;
2754 --------------------
2755 -- Freeze_Profile --
2756 --------------------
2758 function Freeze_Profile (E : Entity_Id) return Boolean is
2759 F_Type : Entity_Id;
2760 R_Type : Entity_Id;
2761 Warn_Node : Node_Id;
2763 begin
2764 -- Loop through formals
2766 Formal := First_Formal (E);
2767 while Present (Formal) loop
2768 F_Type := Etype (Formal);
2770 -- AI05-0151: incomplete types can appear in a profile. By the
2771 -- time the entity is frozen, the full view must be available,
2772 -- unless it is a limited view.
2774 if Is_Incomplete_Type (F_Type)
2775 and then Present (Full_View (F_Type))
2776 and then not From_Limited_With (F_Type)
2777 then
2778 F_Type := Full_View (F_Type);
2779 Set_Etype (Formal, F_Type);
2780 end if;
2782 Freeze_And_Append (F_Type, N, Result);
2784 if Is_Private_Type (F_Type)
2785 and then Is_Private_Type (Base_Type (F_Type))
2786 and then No (Full_View (Base_Type (F_Type)))
2787 and then not Is_Generic_Type (F_Type)
2788 and then not Is_Derived_Type (F_Type)
2789 then
2790 -- If the type of a formal is incomplete, subprogram is being
2791 -- frozen prematurely. Within an instance (but not within a
2792 -- wrapper package) this is an artifact of our need to regard
2793 -- the end of an instantiation as a freeze point. Otherwise it
2794 -- is a definite error.
2796 if In_Instance then
2797 Set_Is_Frozen (E, False);
2798 Result := No_List;
2799 return False;
2801 elsif not After_Last_Declaration
2802 and then not Freezing_Library_Level_Tagged_Type
2803 then
2804 Error_Msg_Node_1 := F_Type;
2805 Error_Msg
2806 ("type & must be fully defined before this point", Loc);
2807 end if;
2808 end if;
2810 -- Check suspicious parameter for C function. These tests apply
2811 -- only to exported/imported subprograms.
2813 if Warn_On_Export_Import
2814 and then Comes_From_Source (E)
2815 and then (Convention (E) = Convention_C
2816 or else
2817 Convention (E) = Convention_CPP)
2818 and then (Is_Imported (E) or else Is_Exported (E))
2819 and then Convention (E) /= Convention (Formal)
2820 and then not Has_Warnings_Off (E)
2821 and then not Has_Warnings_Off (F_Type)
2822 and then not Has_Warnings_Off (Formal)
2823 then
2824 -- Qualify mention of formals with subprogram name
2826 Error_Msg_Qual_Level := 1;
2828 -- Check suspicious use of fat C pointer
2830 if Is_Access_Type (F_Type)
2831 and then Esize (F_Type) > Ttypes.System_Address_Size
2832 then
2833 Error_Msg_N
2834 ("?x?type of & does not correspond to C pointer!", Formal);
2836 -- Check suspicious return of boolean
2838 elsif Root_Type (F_Type) = Standard_Boolean
2839 and then Convention (F_Type) = Convention_Ada
2840 and then not Has_Warnings_Off (F_Type)
2841 and then not Has_Size_Clause (F_Type)
2842 and then VM_Target = No_VM
2843 then
2844 Error_Msg_N
2845 ("& is an 8-bit Ada Boolean?x?", Formal);
2846 Error_Msg_N
2847 ("\use appropriate corresponding type in C "
2848 & "(e.g. char)?x?", Formal);
2850 -- Check suspicious tagged type
2852 elsif (Is_Tagged_Type (F_Type)
2853 or else
2854 (Is_Access_Type (F_Type)
2855 and then Is_Tagged_Type (Designated_Type (F_Type))))
2856 and then Convention (E) = Convention_C
2857 then
2858 Error_Msg_N
2859 ("?x?& involves a tagged type which does not "
2860 & "correspond to any C type!", Formal);
2862 -- Check wrong convention subprogram pointer
2864 elsif Ekind (F_Type) = E_Access_Subprogram_Type
2865 and then not Has_Foreign_Convention (F_Type)
2866 then
2867 Error_Msg_N
2868 ("?x?subprogram pointer & should "
2869 & "have foreign convention!", Formal);
2870 Error_Msg_Sloc := Sloc (F_Type);
2871 Error_Msg_NE
2872 ("\?x?add Convention pragma to declaration of &#",
2873 Formal, F_Type);
2874 end if;
2876 -- Turn off name qualification after message output
2878 Error_Msg_Qual_Level := 0;
2879 end if;
2881 -- Check for unconstrained array in exported foreign convention
2882 -- case.
2884 if Has_Foreign_Convention (E)
2885 and then not Is_Imported (E)
2886 and then Is_Array_Type (F_Type)
2887 and then not Is_Constrained (F_Type)
2888 and then Warn_On_Export_Import
2890 -- Exclude VM case, since both .NET and JVM can handle
2891 -- unconstrained arrays without a problem.
2893 and then VM_Target = No_VM
2894 then
2895 Error_Msg_Qual_Level := 1;
2897 -- If this is an inherited operation, place the warning on
2898 -- the derived type declaration, rather than on the original
2899 -- subprogram.
2901 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
2902 then
2903 Warn_Node := Parent (E);
2905 if Formal = First_Formal (E) then
2906 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
2907 end if;
2908 else
2909 Warn_Node := Formal;
2910 end if;
2912 Error_Msg_NE ("?x?type of argument& is unconstrained array",
2913 Warn_Node, Formal);
2914 Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
2915 Warn_Node, Formal);
2916 Error_Msg_Qual_Level := 0;
2917 end if;
2919 if not From_Limited_With (F_Type) then
2920 if Is_Access_Type (F_Type) then
2921 F_Type := Designated_Type (F_Type);
2922 end if;
2924 -- If the formal is an anonymous_access_to_subprogram
2925 -- freeze the subprogram type as well, to prevent
2926 -- scope anomalies in gigi, because there is no other
2927 -- clear point at which it could be frozen.
2929 if Is_Itype (Etype (Formal))
2930 and then Ekind (F_Type) = E_Subprogram_Type
2931 then
2932 Freeze_And_Append (F_Type, N, Result);
2933 end if;
2934 end if;
2936 Next_Formal (Formal);
2937 end loop;
2939 -- Case of function: similar checks on return type
2941 if Ekind (E) = E_Function then
2943 -- Check whether function is declared elsewhere.
2945 Late_Freezing :=
2946 Get_Source_Unit (E) /= Get_Source_Unit (N)
2947 and then Returns_Limited_View (E)
2948 and then not In_Open_Scopes (Scope (E));
2950 -- Freeze return type
2952 R_Type := Etype (E);
2954 -- AI05-0151: the return type may have been incomplete
2955 -- at the point of declaration. Replace it with the full
2956 -- view, unless the current type is a limited view. In
2957 -- that case the full view is in a different unit, and
2958 -- gigi finds the non-limited view after the other unit
2959 -- is elaborated.
2961 if Ekind (R_Type) = E_Incomplete_Type
2962 and then Present (Full_View (R_Type))
2963 and then not From_Limited_With (R_Type)
2964 then
2965 R_Type := Full_View (R_Type);
2966 Set_Etype (E, R_Type);
2968 -- If the return type is a limited view and the non-
2969 -- limited view is still incomplete, the function has
2970 -- to be frozen at a later time.
2972 elsif Ekind (R_Type) = E_Incomplete_Type
2973 and then From_Limited_With (R_Type)
2974 and then
2975 Ekind (Non_Limited_View (R_Type)) = E_Incomplete_Type
2976 then
2977 Set_Is_Frozen (E, False);
2978 Set_Returns_Limited_View (E);
2979 return False;
2980 end if;
2982 Freeze_And_Append (R_Type, N, Result);
2984 -- Check suspicious return type for C function
2986 if Warn_On_Export_Import
2987 and then (Convention (E) = Convention_C
2988 or else
2989 Convention (E) = Convention_CPP)
2990 and then (Is_Imported (E) or else Is_Exported (E))
2991 then
2992 -- Check suspicious return of fat C pointer
2994 if Is_Access_Type (R_Type)
2995 and then Esize (R_Type) > Ttypes.System_Address_Size
2996 and then not Has_Warnings_Off (E)
2997 and then not Has_Warnings_Off (R_Type)
2998 then
2999 Error_Msg_N ("?x?return type of& does not "
3000 & "correspond to C pointer!", E);
3002 -- Check suspicious return of boolean
3004 elsif Root_Type (R_Type) = Standard_Boolean
3005 and then Convention (R_Type) = Convention_Ada
3006 and then VM_Target = No_VM
3007 and then not Has_Warnings_Off (E)
3008 and then not Has_Warnings_Off (R_Type)
3009 and then not Has_Size_Clause (R_Type)
3010 then
3011 declare
3012 N : constant Node_Id :=
3013 Result_Definition (Declaration_Node (E));
3014 begin
3015 Error_Msg_NE
3016 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
3017 Error_Msg_NE
3018 ("\use appropriate corresponding type in C "
3019 & "(e.g. char)?x?", N, E);
3020 end;
3022 -- Check suspicious return tagged type
3024 elsif (Is_Tagged_Type (R_Type)
3025 or else (Is_Access_Type (R_Type)
3026 and then
3027 Is_Tagged_Type
3028 (Designated_Type (R_Type))))
3029 and then Convention (E) = Convention_C
3030 and then not Has_Warnings_Off (E)
3031 and then not Has_Warnings_Off (R_Type)
3032 then
3033 Error_Msg_N ("?x?return type of & does not "
3034 & "correspond to C type!", E);
3036 -- Check return of wrong convention subprogram pointer
3038 elsif Ekind (R_Type) = E_Access_Subprogram_Type
3039 and then not Has_Foreign_Convention (R_Type)
3040 and then not Has_Warnings_Off (E)
3041 and then not Has_Warnings_Off (R_Type)
3042 then
3043 Error_Msg_N ("?x?& should return a foreign "
3044 & "convention subprogram pointer", E);
3045 Error_Msg_Sloc := Sloc (R_Type);
3046 Error_Msg_NE
3047 ("\?x?add Convention pragma to declaration of& #",
3048 E, R_Type);
3049 end if;
3050 end if;
3052 -- Give warning for suspicious return of a result of an
3053 -- unconstrained array type in a foreign convention function.
3055 if Has_Foreign_Convention (E)
3057 -- We are looking for a return of unconstrained array
3059 and then Is_Array_Type (R_Type)
3060 and then not Is_Constrained (R_Type)
3062 -- Exclude imported routines, the warning does not belong on
3063 -- the import, but rather on the routine definition.
3065 and then not Is_Imported (E)
3067 -- Exclude VM case, since both .NET and JVM can handle return
3068 -- of unconstrained arrays without a problem.
3070 and then VM_Target = No_VM
3072 -- Check that general warning is enabled, and that it is not
3073 -- suppressed for this particular case.
3075 and then Warn_On_Export_Import
3076 and then not Has_Warnings_Off (E)
3077 and then not Has_Warnings_Off (R_Type)
3078 then
3079 Error_Msg_N ("?x?foreign convention function& should not " &
3080 "return unconstrained array!", E);
3081 end if;
3082 end if;
3084 -- Check suspicious use of Import in pure unit
3086 if Is_Imported (E) and then Is_Pure (Cunit_Entity (Current_Sem_Unit))
3088 -- Ignore internally generated entity. This happens in some cases
3089 -- of subprograms in specs, where we generate an implied body.
3091 and then Comes_From_Source (Import_Pragma (E))
3093 -- Assume run-time knows what it is doing
3095 and then not GNAT_Mode
3097 -- Assume explicit Pure_Function means import is pure
3099 and then not Has_Pragma_Pure_Function (E)
3101 -- Don't need warning in relaxed semantics mode
3103 and then not Relaxed_RM_Semantics
3105 -- Assume convention Intrinsic is OK, since this is specialized.
3106 -- This deals with the DEC unit current_exception.ads
3108 and then Convention (E) /= Convention_Intrinsic
3110 -- Assume that ASM interface knows what it is doing. This deals
3111 -- with unsigned.ads in the AAMP back end.
3113 and then Convention (E) /= Convention_Assembler
3114 then
3115 Error_Msg_N
3116 ("pragma Import in Pure unit??", Import_Pragma (E));
3117 Error_Msg_NE
3118 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
3119 Import_Pragma (E), E);
3120 end if;
3122 return True;
3123 end Freeze_Profile;
3125 ------------------------
3126 -- Freeze_Record_Type --
3127 ------------------------
3129 procedure Freeze_Record_Type (Rec : Entity_Id) is
3130 ADC : Node_Id;
3131 Comp : Entity_Id;
3132 IR : Node_Id;
3133 Prev : Entity_Id;
3135 Junk : Boolean;
3136 pragma Warnings (Off, Junk);
3138 Rec_Pushed : Boolean := False;
3139 -- Set True if the record type scope Rec has been pushed on the scope
3140 -- stack. Needed for the analysis of delayed aspects specified to the
3141 -- components of Rec.
3143 SSO_ADC : Node_Id;
3144 -- Scalar_Storage_Order attribute definition clause for the record
3146 Unplaced_Component : Boolean := False;
3147 -- Set True if we find at least one component with no component
3148 -- clause (used to warn about useless Pack pragmas).
3150 Placed_Component : Boolean := False;
3151 -- Set True if we find at least one component with a component
3152 -- clause (used to warn about useless Bit_Order pragmas, and also
3153 -- to detect cases where Implicit_Packing may have an effect).
3155 Aliased_Component : Boolean := False;
3156 -- Set True if we find at least one component which is aliased. This
3157 -- is used to prevent Implicit_Packing of the record, since packing
3158 -- cannot modify the size of alignment of an aliased component.
3160 SSO_ADC_Component : Boolean := False;
3161 -- Set True if we find at least one component whose type has a
3162 -- Scalar_Storage_Order attribute definition clause.
3164 All_Scalar_Components : Boolean := True;
3165 -- Set False if we encounter a component of a non-scalar type
3167 Scalar_Component_Total_RM_Size : Uint := Uint_0;
3168 Scalar_Component_Total_Esize : Uint := Uint_0;
3169 -- Accumulates total RM_Size values and total Esize values of all
3170 -- scalar components. Used for processing of Implicit_Packing.
3172 function Check_Allocator (N : Node_Id) return Node_Id;
3173 -- If N is an allocator, possibly wrapped in one or more level of
3174 -- qualified expression(s), return the inner allocator node, else
3175 -- return Empty.
3177 procedure Check_Itype (Typ : Entity_Id);
3178 -- If the component subtype is an access to a constrained subtype of
3179 -- an already frozen type, make the subtype frozen as well. It might
3180 -- otherwise be frozen in the wrong scope, and a freeze node on
3181 -- subtype has no effect. Similarly, if the component subtype is a
3182 -- regular (not protected) access to subprogram, set the anonymous
3183 -- subprogram type to frozen as well, to prevent an out-of-scope
3184 -- freeze node at some eventual point of call. Protected operations
3185 -- are handled elsewhere.
3187 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3188 -- Make sure that all types mentioned in Discrete_Choices of the
3189 -- variants referenceed by the Variant_Part VP are frozen. This is
3190 -- a recursive routine to deal with nested variants.
3192 ---------------------
3193 -- Check_Allocator --
3194 ---------------------
3196 function Check_Allocator (N : Node_Id) return Node_Id is
3197 Inner : Node_Id;
3198 begin
3199 Inner := N;
3200 loop
3201 if Nkind (Inner) = N_Allocator then
3202 return Inner;
3203 elsif Nkind (Inner) = N_Qualified_Expression then
3204 Inner := Expression (Inner);
3205 else
3206 return Empty;
3207 end if;
3208 end loop;
3209 end Check_Allocator;
3211 -----------------
3212 -- Check_Itype --
3213 -----------------
3215 procedure Check_Itype (Typ : Entity_Id) is
3216 Desig : constant Entity_Id := Designated_Type (Typ);
3218 begin
3219 if not Is_Frozen (Desig)
3220 and then Is_Frozen (Base_Type (Desig))
3221 then
3222 Set_Is_Frozen (Desig);
3224 -- In addition, add an Itype_Reference to ensure that the
3225 -- access subtype is elaborated early enough. This cannot be
3226 -- done if the subtype may depend on discriminants.
3228 if Ekind (Comp) = E_Component
3229 and then Is_Itype (Etype (Comp))
3230 and then not Has_Discriminants (Rec)
3231 then
3232 IR := Make_Itype_Reference (Sloc (Comp));
3233 Set_Itype (IR, Desig);
3234 Add_To_Result (IR);
3235 end if;
3237 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
3238 and then Convention (Desig) /= Convention_Protected
3239 then
3240 Set_Is_Frozen (Desig);
3241 end if;
3242 end Check_Itype;
3244 ------------------------------------
3245 -- Freeze_Choices_In_Variant_Part --
3246 ------------------------------------
3248 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
3249 pragma Assert (Nkind (VP) = N_Variant_Part);
3251 Variant : Node_Id;
3252 Choice : Node_Id;
3253 CL : Node_Id;
3255 begin
3256 -- Loop through variants
3258 Variant := First_Non_Pragma (Variants (VP));
3259 while Present (Variant) loop
3261 -- Loop through choices, checking that all types are frozen
3263 Choice := First_Non_Pragma (Discrete_Choices (Variant));
3264 while Present (Choice) loop
3265 if Nkind (Choice) in N_Has_Etype
3266 and then Present (Etype (Choice))
3267 then
3268 Freeze_And_Append (Etype (Choice), N, Result);
3269 end if;
3271 Next_Non_Pragma (Choice);
3272 end loop;
3274 -- Check for nested variant part to process
3276 CL := Component_List (Variant);
3278 if not Null_Present (CL) then
3279 if Present (Variant_Part (CL)) then
3280 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
3281 end if;
3282 end if;
3284 Next_Non_Pragma (Variant);
3285 end loop;
3286 end Freeze_Choices_In_Variant_Part;
3288 -- Start of processing for Freeze_Record_Type
3290 begin
3291 -- Deal with delayed aspect specifications for components. The
3292 -- analysis of the aspect is required to be delayed to the freeze
3293 -- point, thus we analyze the pragma or attribute definition
3294 -- clause in the tree at this point. We also analyze the aspect
3295 -- specification node at the freeze point when the aspect doesn't
3296 -- correspond to pragma/attribute definition clause.
3298 Comp := First_Entity (Rec);
3299 while Present (Comp) loop
3300 if Ekind (Comp) = E_Component
3301 and then Has_Delayed_Aspects (Comp)
3302 then
3303 if not Rec_Pushed then
3304 Push_Scope (Rec);
3305 Rec_Pushed := True;
3307 -- The visibility to the discriminants must be restored in
3308 -- order to properly analyze the aspects.
3310 if Has_Discriminants (Rec) then
3311 Install_Discriminants (Rec);
3312 end if;
3313 end if;
3315 Analyze_Aspects_At_Freeze_Point (Comp);
3316 end if;
3318 Next_Entity (Comp);
3319 end loop;
3321 -- Pop the scope if Rec scope has been pushed on the scope stack
3322 -- during the delayed aspect analysis process.
3324 if Rec_Pushed then
3325 if Has_Discriminants (Rec) then
3326 Uninstall_Discriminants (Rec);
3327 end if;
3329 Pop_Scope;
3330 end if;
3332 -- Freeze components and embedded subtypes
3334 Comp := First_Entity (Rec);
3335 Prev := Empty;
3336 while Present (Comp) loop
3337 if Is_Aliased (Comp) then
3338 Aliased_Component := True;
3339 end if;
3341 -- Handle the component and discriminant case
3343 if Ekind_In (Comp, E_Component, E_Discriminant) then
3344 declare
3345 CC : constant Node_Id := Component_Clause (Comp);
3347 begin
3348 -- Freezing a record type freezes the type of each of its
3349 -- components. However, if the type of the component is
3350 -- part of this record, we do not want or need a separate
3351 -- Freeze_Node. Note that Is_Itype is wrong because that's
3352 -- also set in private type cases. We also can't check for
3353 -- the Scope being exactly Rec because of private types and
3354 -- record extensions.
3356 if Is_Itype (Etype (Comp))
3357 and then Is_Record_Type (Underlying_Type
3358 (Scope (Etype (Comp))))
3359 then
3360 Undelay_Type (Etype (Comp));
3361 end if;
3363 Freeze_And_Append (Etype (Comp), N, Result);
3365 -- Warn for pragma Pack overriding foreign convention
3367 if Has_Foreign_Convention (Etype (Comp))
3368 and then Has_Pragma_Pack (Rec)
3370 -- Don't warn for aliased components, since override
3371 -- cannot happen in that case.
3373 and then not Is_Aliased (Comp)
3374 then
3375 declare
3376 CN : constant Name_Id :=
3377 Get_Convention_Name (Convention (Etype (Comp)));
3378 PP : constant Node_Id :=
3379 Get_Pragma (Rec, Pragma_Pack);
3380 begin
3381 if Present (PP) then
3382 Error_Msg_Name_1 := CN;
3383 Error_Msg_Sloc := Sloc (Comp);
3384 Error_Msg_N
3385 ("pragma Pack affects convention % component#??",
3386 PP);
3387 Error_Msg_Name_1 := CN;
3388 Error_Msg_NE
3389 ("\component & may not have % compatible "
3390 & "representation??", PP, Comp);
3391 end if;
3392 end;
3393 end if;
3395 -- Check for error of component clause given for variable
3396 -- sized type. We have to delay this test till this point,
3397 -- since the component type has to be frozen for us to know
3398 -- if it is variable length.
3400 if Present (CC) then
3401 Placed_Component := True;
3403 -- We omit this test in a generic context, it will be
3404 -- applied at instantiation time.
3406 if Inside_A_Generic then
3407 null;
3409 -- Also omit this test in CodePeer mode, since we do not
3410 -- have sufficient info on size and rep clauses.
3412 elsif CodePeer_Mode then
3413 null;
3415 -- Omit check if component has a generic type. This can
3416 -- happen in an instantiation within a generic in ASIS
3417 -- mode, where we force freeze actions without full
3418 -- expansion.
3420 elsif Is_Generic_Type (Etype (Comp)) then
3421 null;
3423 -- Do the check
3425 elsif not
3426 Size_Known_At_Compile_Time
3427 (Underlying_Type (Etype (Comp)))
3428 then
3429 Error_Msg_N
3430 ("component clause not allowed for variable " &
3431 "length component", CC);
3432 end if;
3434 else
3435 Unplaced_Component := True;
3436 end if;
3438 -- Case of component requires byte alignment
3440 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
3442 -- Set the enclosing record to also require byte align
3444 Set_Must_Be_On_Byte_Boundary (Rec);
3446 -- Check for component clause that is inconsistent with
3447 -- the required byte boundary alignment.
3449 if Present (CC)
3450 and then Normalized_First_Bit (Comp) mod
3451 System_Storage_Unit /= 0
3452 then
3453 Error_Msg_N
3454 ("component & must be byte aligned",
3455 Component_Name (Component_Clause (Comp)));
3456 end if;
3457 end if;
3458 end;
3459 end if;
3461 -- Gather data for possible Implicit_Packing later. Note that at
3462 -- this stage we might be dealing with a real component, or with
3463 -- an implicit subtype declaration.
3465 if not Is_Scalar_Type (Etype (Comp)) then
3466 All_Scalar_Components := False;
3467 else
3468 Scalar_Component_Total_RM_Size :=
3469 Scalar_Component_Total_RM_Size + RM_Size (Etype (Comp));
3470 Scalar_Component_Total_Esize :=
3471 Scalar_Component_Total_Esize + Esize (Etype (Comp));
3472 end if;
3474 -- If the component is an Itype with Delayed_Freeze and is either
3475 -- a record or array subtype and its base type has not yet been
3476 -- frozen, we must remove this from the entity list of this record
3477 -- and put it on the entity list of the scope of its base type.
3478 -- Note that we know that this is not the type of a component
3479 -- since we cleared Has_Delayed_Freeze for it in the previous
3480 -- loop. Thus this must be the Designated_Type of an access type,
3481 -- which is the type of a component.
3483 if Is_Itype (Comp)
3484 and then Is_Type (Scope (Comp))
3485 and then Is_Composite_Type (Comp)
3486 and then Base_Type (Comp) /= Comp
3487 and then Has_Delayed_Freeze (Comp)
3488 and then not Is_Frozen (Base_Type (Comp))
3489 then
3490 declare
3491 Will_Be_Frozen : Boolean := False;
3492 S : Entity_Id;
3494 begin
3495 -- We have a difficult case to handle here. Suppose Rec is
3496 -- subtype being defined in a subprogram that's created as
3497 -- part of the freezing of Rec'Base. In that case, we know
3498 -- that Comp'Base must have already been frozen by the time
3499 -- we get to elaborate this because Gigi doesn't elaborate
3500 -- any bodies until it has elaborated all of the declarative
3501 -- part. But Is_Frozen will not be set at this point because
3502 -- we are processing code in lexical order.
3504 -- We detect this case by going up the Scope chain of Rec
3505 -- and seeing if we have a subprogram scope before reaching
3506 -- the top of the scope chain or that of Comp'Base. If we
3507 -- do, then mark that Comp'Base will actually be frozen. If
3508 -- so, we merely undelay it.
3510 S := Scope (Rec);
3511 while Present (S) loop
3512 if Is_Subprogram (S) then
3513 Will_Be_Frozen := True;
3514 exit;
3515 elsif S = Scope (Base_Type (Comp)) then
3516 exit;
3517 end if;
3519 S := Scope (S);
3520 end loop;
3522 if Will_Be_Frozen then
3523 Undelay_Type (Comp);
3525 else
3526 if Present (Prev) then
3527 Set_Next_Entity (Prev, Next_Entity (Comp));
3528 else
3529 Set_First_Entity (Rec, Next_Entity (Comp));
3530 end if;
3532 -- Insert in entity list of scope of base type (which
3533 -- must be an enclosing scope, because still unfrozen).
3535 Append_Entity (Comp, Scope (Base_Type (Comp)));
3536 end if;
3537 end;
3539 -- If the component is an access type with an allocator as default
3540 -- value, the designated type will be frozen by the corresponding
3541 -- expression in init_proc. In order to place the freeze node for
3542 -- the designated type before that for the current record type,
3543 -- freeze it now.
3545 -- Same process if the component is an array of access types,
3546 -- initialized with an aggregate. If the designated type is
3547 -- private, it cannot contain allocators, and it is premature
3548 -- to freeze the type, so we check for this as well.
3550 elsif Is_Access_Type (Etype (Comp))
3551 and then Present (Parent (Comp))
3552 and then Present (Expression (Parent (Comp)))
3553 then
3554 declare
3555 Alloc : constant Node_Id :=
3556 Check_Allocator (Expression (Parent (Comp)));
3558 begin
3559 if Present (Alloc) then
3561 -- If component is pointer to a class-wide type, freeze
3562 -- the specific type in the expression being allocated.
3563 -- The expression may be a subtype indication, in which
3564 -- case freeze the subtype mark.
3566 if Is_Class_Wide_Type
3567 (Designated_Type (Etype (Comp)))
3568 then
3569 if Is_Entity_Name (Expression (Alloc)) then
3570 Freeze_And_Append
3571 (Entity (Expression (Alloc)), N, Result);
3573 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
3574 then
3575 Freeze_And_Append
3576 (Entity (Subtype_Mark (Expression (Alloc))),
3577 N, Result);
3578 end if;
3580 elsif Is_Itype (Designated_Type (Etype (Comp))) then
3581 Check_Itype (Etype (Comp));
3583 else
3584 Freeze_And_Append
3585 (Designated_Type (Etype (Comp)), N, Result);
3586 end if;
3587 end if;
3588 end;
3590 elsif Is_Access_Type (Etype (Comp))
3591 and then Is_Itype (Designated_Type (Etype (Comp)))
3592 then
3593 Check_Itype (Etype (Comp));
3595 -- Freeze the designated type when initializing a component with
3596 -- an aggregate in case the aggregate contains allocators.
3598 -- type T is ...;
3599 -- type T_Ptr is access all T;
3600 -- type T_Array is array ... of T_Ptr;
3602 -- type Rec is record
3603 -- Comp : T_Array := (others => ...);
3604 -- end record;
3606 elsif Is_Array_Type (Etype (Comp))
3607 and then Is_Access_Type (Component_Type (Etype (Comp)))
3608 then
3609 declare
3610 Comp_Par : constant Node_Id := Parent (Comp);
3611 Desig_Typ : constant Entity_Id :=
3612 Designated_Type
3613 (Component_Type (Etype (Comp)));
3615 begin
3616 -- The only case when this sort of freezing is not done is
3617 -- when the designated type is class-wide and the root type
3618 -- is the record owning the component. This scenario results
3619 -- in a circularity because the class-wide type requires
3620 -- primitives that have not been created yet as the root
3621 -- type is in the process of being frozen.
3623 -- type Rec is tagged;
3624 -- type Rec_Ptr is access all Rec'Class;
3625 -- type Rec_Array is array ... of Rec_Ptr;
3627 -- type Rec is record
3628 -- Comp : Rec_Array := (others => ...);
3629 -- end record;
3631 if Is_Class_Wide_Type (Desig_Typ)
3632 and then Root_Type (Desig_Typ) = Rec
3633 then
3634 null;
3636 elsif Is_Fully_Defined (Desig_Typ)
3637 and then Present (Comp_Par)
3638 and then Nkind (Comp_Par) = N_Component_Declaration
3639 and then Present (Expression (Comp_Par))
3640 and then Nkind (Expression (Comp_Par)) = N_Aggregate
3641 then
3642 Freeze_And_Append (Desig_Typ, N, Result);
3643 end if;
3644 end;
3645 end if;
3647 Prev := Comp;
3648 Next_Entity (Comp);
3649 end loop;
3651 -- Deal with default setting of reverse storage order
3653 Set_SSO_From_Default (Rec);
3655 -- Check consistent attribute setting on component types
3657 SSO_ADC := Get_Attribute_Definition_Clause
3658 (Rec, Attribute_Scalar_Storage_Order);
3660 declare
3661 Comp_ADC_Present : Boolean;
3662 begin
3663 Comp := First_Component (Rec);
3664 while Present (Comp) loop
3665 Check_Component_Storage_Order
3666 (Encl_Type => Rec,
3667 Comp => Comp,
3668 ADC => SSO_ADC,
3669 Comp_ADC_Present => Comp_ADC_Present);
3670 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
3671 Next_Component (Comp);
3672 end loop;
3673 end;
3675 -- Now deal with reverse storage order/bit order issues
3677 if Present (SSO_ADC) then
3679 -- Check compatibility of Scalar_Storage_Order with Bit_Order, if
3680 -- the former is specified.
3682 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
3684 -- Note: report error on Rec, not on SSO_ADC, as ADC may apply
3685 -- to some ancestor type.
3687 Error_Msg_Sloc := Sloc (SSO_ADC);
3688 Error_Msg_N
3689 ("scalar storage order for& specified# inconsistent with "
3690 & "bit order", Rec);
3691 end if;
3693 -- Warn if there is an Scalar_Storage_Order attribute definition
3694 -- clause but no component clause, no component that itself has
3695 -- such an attribute definition, and no pragma Pack.
3697 if not (Placed_Component
3698 or else
3699 SSO_ADC_Component
3700 or else
3701 Is_Packed (Rec))
3702 then
3703 Error_Msg_N
3704 ("??scalar storage order specified but no component clause",
3705 SSO_ADC);
3706 end if;
3707 end if;
3709 -- Deal with Bit_Order aspect
3711 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
3713 if Present (ADC) and then Base_Type (Rec) = Rec then
3714 if not (Placed_Component
3715 or else Present (SSO_ADC)
3716 or else Is_Packed (Rec))
3717 then
3718 -- Warn if clause has no effect when no component clause is
3719 -- present, but suppress warning if the Bit_Order is required
3720 -- due to the presence of a Scalar_Storage_Order attribute.
3722 Error_Msg_N
3723 ("??bit order specification has no effect", ADC);
3724 Error_Msg_N
3725 ("\??since no component clauses were specified", ADC);
3727 -- Here is where we do the processing to adjust component clauses
3728 -- for reversed bit order, when not using reverse SSO.
3730 elsif Reverse_Bit_Order (Rec)
3731 and then not Reverse_Storage_Order (Rec)
3732 then
3733 Adjust_Record_For_Reverse_Bit_Order (Rec);
3735 -- Case where we have both an explicit Bit_Order and the same
3736 -- Scalar_Storage_Order: leave record untouched, the back-end
3737 -- will take care of required layout conversions.
3739 else
3740 null;
3742 end if;
3743 end if;
3745 -- Complete error checking on record representation clause (e.g.
3746 -- overlap of components). This is called after adjusting the
3747 -- record for reverse bit order.
3749 declare
3750 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
3751 begin
3752 if Present (RRC) then
3753 Check_Record_Representation_Clause (RRC);
3754 end if;
3755 end;
3757 -- Set OK_To_Reorder_Components depending on debug flags
3759 if Is_Base_Type (Rec) and then Convention (Rec) = Convention_Ada then
3760 if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
3761 or else
3762 (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
3763 then
3764 Set_OK_To_Reorder_Components (Rec);
3765 end if;
3766 end if;
3768 -- Check for useless pragma Pack when all components placed. We only
3769 -- do this check for record types, not subtypes, since a subtype may
3770 -- have all its components placed, and it still makes perfectly good
3771 -- sense to pack other subtypes or the parent type. We do not give
3772 -- this warning if Optimize_Alignment is set to Space, since the
3773 -- pragma Pack does have an effect in this case (it always resets
3774 -- the alignment to one).
3776 if Ekind (Rec) = E_Record_Type
3777 and then Is_Packed (Rec)
3778 and then not Unplaced_Component
3779 and then Optimize_Alignment /= 'S'
3780 then
3781 -- Reset packed status. Probably not necessary, but we do it so
3782 -- that there is no chance of the back end doing something strange
3783 -- with this redundant indication of packing.
3785 Set_Is_Packed (Rec, False);
3787 -- Give warning if redundant constructs warnings on
3789 if Warn_On_Redundant_Constructs then
3790 Error_Msg_N -- CODEFIX
3791 ("??pragma Pack has no effect, no unplaced components",
3792 Get_Rep_Pragma (Rec, Name_Pack));
3793 end if;
3794 end if;
3796 -- If this is the record corresponding to a remote type, freeze the
3797 -- remote type here since that is what we are semantically freezing.
3798 -- This prevents the freeze node for that type in an inner scope.
3800 if Ekind (Rec) = E_Record_Type then
3801 if Present (Corresponding_Remote_Type (Rec)) then
3802 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
3803 end if;
3805 -- Check for controlled components and unchecked unions.
3807 Comp := First_Component (Rec);
3808 while Present (Comp) loop
3810 -- Do not set Has_Controlled_Component on a class-wide
3811 -- equivalent type. See Make_CW_Equivalent_Type.
3813 if not Is_Class_Wide_Equivalent_Type (Rec)
3814 and then
3815 (Has_Controlled_Component (Etype (Comp))
3816 or else
3817 (Chars (Comp) /= Name_uParent
3818 and then Is_Controlled (Etype (Comp)))
3819 or else
3820 (Is_Protected_Type (Etype (Comp))
3821 and then
3822 Present (Corresponding_Record_Type (Etype (Comp)))
3823 and then
3824 Has_Controlled_Component
3825 (Corresponding_Record_Type (Etype (Comp)))))
3826 then
3827 Set_Has_Controlled_Component (Rec);
3828 end if;
3830 if Has_Unchecked_Union (Etype (Comp)) then
3831 Set_Has_Unchecked_Union (Rec);
3832 end if;
3834 -- Scan component declaration for likely misuses of current
3835 -- instance, either in a constraint or a default expression.
3837 if Has_Per_Object_Constraint (Comp) then
3838 Check_Current_Instance (Parent (Comp));
3839 end if;
3841 Next_Component (Comp);
3842 end loop;
3843 end if;
3845 -- Enforce the restriction that access attributes with a current
3846 -- instance prefix can only apply to limited types. This comment
3847 -- is floating here, but does not seem to belong here???
3849 -- Set component alignment if not otherwise already set
3851 Set_Component_Alignment_If_Not_Set (Rec);
3853 -- For first subtypes, check if there are any fixed-point fields with
3854 -- component clauses, where we must check the size. This is not done
3855 -- till the freeze point since for fixed-point types, we do not know
3856 -- the size until the type is frozen. Similar processing applies to
3857 -- bit packed arrays.
3859 if Is_First_Subtype (Rec) then
3860 Comp := First_Component (Rec);
3861 while Present (Comp) loop
3862 if Present (Component_Clause (Comp))
3863 and then (Is_Fixed_Point_Type (Etype (Comp))
3864 or else Is_Bit_Packed_Array (Etype (Comp)))
3865 then
3866 Check_Size
3867 (Component_Name (Component_Clause (Comp)),
3868 Etype (Comp),
3869 Esize (Comp),
3870 Junk);
3871 end if;
3873 Next_Component (Comp);
3874 end loop;
3875 end if;
3877 -- Generate warning for applying C or C++ convention to a record
3878 -- with discriminants. This is suppressed for the unchecked union
3879 -- case, since the whole point in this case is interface C. We also
3880 -- do not generate this within instantiations, since we will have
3881 -- generated a message on the template.
3883 if Has_Discriminants (E)
3884 and then not Is_Unchecked_Union (E)
3885 and then (Convention (E) = Convention_C
3886 or else
3887 Convention (E) = Convention_CPP)
3888 and then Comes_From_Source (E)
3889 and then not In_Instance
3890 and then not Has_Warnings_Off (E)
3891 and then not Has_Warnings_Off (Base_Type (E))
3892 then
3893 declare
3894 Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
3895 A2 : Node_Id;
3897 begin
3898 if Present (Cprag) then
3899 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
3901 if Convention (E) = Convention_C then
3902 Error_Msg_N
3903 ("?x?variant record has no direct equivalent in C",
3904 A2);
3905 else
3906 Error_Msg_N
3907 ("?x?variant record has no direct equivalent in C++",
3908 A2);
3909 end if;
3911 Error_Msg_NE
3912 ("\?x?use of convention for type& is dubious", A2, E);
3913 end if;
3914 end;
3915 end if;
3917 -- See if Size is too small as is (and implicit packing might help)
3919 if not Is_Packed (Rec)
3921 -- No implicit packing if even one component is explicitly placed
3923 and then not Placed_Component
3925 -- Or even one component is aliased
3927 and then not Aliased_Component
3929 -- Must have size clause and all scalar components
3931 and then Has_Size_Clause (Rec)
3932 and then All_Scalar_Components
3934 -- Do not try implicit packing on records with discriminants, too
3935 -- complicated, especially in the variant record case.
3937 and then not Has_Discriminants (Rec)
3939 -- We can implicitly pack if the specified size of the record is
3940 -- less than the sum of the object sizes (no point in packing if
3941 -- this is not the case).
3943 and then RM_Size (Rec) < Scalar_Component_Total_Esize
3945 -- And the total RM size cannot be greater than the specified size
3946 -- since otherwise packing will not get us where we have to be.
3948 and then RM_Size (Rec) >= Scalar_Component_Total_RM_Size
3950 -- Never do implicit packing in CodePeer or SPARK modes since
3951 -- we don't do any packing in these modes, since this generates
3952 -- over-complex code that confuses static analysis, and in
3953 -- general, neither CodePeer not GNATprove care about the
3954 -- internal representation of objects.
3956 and then not (CodePeer_Mode or GNATprove_Mode)
3957 then
3958 -- If implicit packing enabled, do it
3960 if Implicit_Packing then
3961 Set_Is_Packed (Rec);
3963 -- Otherwise flag the size clause
3965 else
3966 declare
3967 Sz : constant Node_Id := Size_Clause (Rec);
3968 begin
3969 Error_Msg_NE -- CODEFIX
3970 ("size given for& too small", Sz, Rec);
3971 Error_Msg_N -- CODEFIX
3972 ("\use explicit pragma Pack "
3973 & "or use pragma Implicit_Packing", Sz);
3974 end;
3975 end if;
3976 end if;
3978 -- The following checks are only relevant when SPARK_Mode is on as
3979 -- they are not standard Ada legality rules.
3981 if SPARK_Mode = On then
3982 if Is_Effectively_Volatile (Rec) then
3984 -- A discriminated type cannot be effectively volatile
3985 -- (SPARK RM C.6(4)).
3987 if Has_Discriminants (Rec) then
3988 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
3990 -- A tagged type cannot be effectively volatile
3991 -- (SPARK RM C.6(5)).
3993 elsif Is_Tagged_Type (Rec) then
3994 Error_Msg_N ("tagged type & cannot be volatile", Rec);
3995 end if;
3997 -- A non-effectively volatile record type cannot contain
3998 -- effectively volatile components (SPARK RM C.6(2)).
4000 else
4001 Comp := First_Component (Rec);
4002 while Present (Comp) loop
4003 if Comes_From_Source (Comp)
4004 and then Is_Effectively_Volatile (Etype (Comp))
4005 then
4006 Error_Msg_Name_1 := Chars (Rec);
4007 Error_Msg_N
4008 ("component & of non-volatile type % cannot be "
4009 & "volatile", Comp);
4010 end if;
4012 Next_Component (Comp);
4013 end loop;
4014 end if;
4015 end if;
4017 -- All done if not a full record definition
4019 if Ekind (Rec) /= E_Record_Type then
4020 return;
4021 end if;
4023 -- Finally we need to check the variant part to make sure that
4024 -- all types within choices are properly frozen as part of the
4025 -- freezing of the record type.
4027 Check_Variant_Part : declare
4028 D : constant Node_Id := Declaration_Node (Rec);
4029 T : Node_Id;
4030 C : Node_Id;
4032 begin
4033 -- Find component list
4035 C := Empty;
4037 if Nkind (D) = N_Full_Type_Declaration then
4038 T := Type_Definition (D);
4040 if Nkind (T) = N_Record_Definition then
4041 C := Component_List (T);
4043 elsif Nkind (T) = N_Derived_Type_Definition
4044 and then Present (Record_Extension_Part (T))
4045 then
4046 C := Component_List (Record_Extension_Part (T));
4047 end if;
4048 end if;
4050 -- Case of variant part present
4052 if Present (C) and then Present (Variant_Part (C)) then
4053 Freeze_Choices_In_Variant_Part (Variant_Part (C));
4054 end if;
4056 -- Note: we used to call Check_Choices here, but it is too early,
4057 -- since predicated subtypes are frozen here, but their freezing
4058 -- actions are in Analyze_Freeze_Entity, which has not been called
4059 -- yet for entities frozen within this procedure, so we moved that
4060 -- call to the Analyze_Freeze_Entity for the record type.
4062 end Check_Variant_Part;
4064 -- Check that all the primitives of an interface type are abstract
4065 -- or null procedures.
4067 if Is_Interface (Rec)
4068 and then not Error_Posted (Parent (Rec))
4069 then
4070 declare
4071 Elmt : Elmt_Id;
4072 Subp : Entity_Id;
4074 begin
4075 Elmt := First_Elmt (Primitive_Operations (Rec));
4076 while Present (Elmt) loop
4077 Subp := Node (Elmt);
4079 if not Is_Abstract_Subprogram (Subp)
4081 -- Avoid reporting the error on inherited primitives
4083 and then Comes_From_Source (Subp)
4084 then
4085 Error_Msg_Name_1 := Chars (Subp);
4087 if Ekind (Subp) = E_Procedure then
4088 if not Null_Present (Parent (Subp)) then
4089 Error_Msg_N
4090 ("interface procedure % must be abstract or null",
4091 Parent (Subp));
4092 end if;
4093 else
4094 Error_Msg_N
4095 ("interface function % must be abstract",
4096 Parent (Subp));
4097 end if;
4098 end if;
4100 Next_Elmt (Elmt);
4101 end loop;
4102 end;
4103 end if;
4104 end Freeze_Record_Type;
4106 -------------------------------
4107 -- Has_Boolean_Aspect_Import --
4108 -------------------------------
4110 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4111 Decl : constant Node_Id := Declaration_Node (E);
4112 Asp : Node_Id;
4113 Expr : Node_Id;
4115 begin
4116 if Has_Aspects (Decl) then
4117 Asp := First (Aspect_Specifications (Decl));
4118 while Present (Asp) loop
4119 Expr := Expression (Asp);
4121 -- The value of aspect Import is True when the expression is
4122 -- either missing or it is explicitly set to True.
4124 if Get_Aspect_Id (Asp) = Aspect_Import
4125 and then (No (Expr)
4126 or else (Compile_Time_Known_Value (Expr)
4127 and then Is_True (Expr_Value (Expr))))
4128 then
4129 return True;
4130 end if;
4132 Next (Asp);
4133 end loop;
4134 end if;
4136 return False;
4137 end Has_Boolean_Aspect_Import;
4139 ----------------------------
4140 -- Late_Freeze_Subprogram --
4141 ----------------------------
4143 procedure Late_Freeze_Subprogram (E : Entity_Id) is
4144 Spec : constant Node_Id :=
4145 Specification (Unit_Declaration_Node (Scope (E)));
4146 Decls : List_Id;
4148 begin
4149 if Present (Private_Declarations (Spec)) then
4150 Decls := Private_Declarations (Spec);
4151 else
4152 Decls := Visible_Declarations (Spec);
4153 end if;
4155 Append_List (Result, Decls);
4156 end Late_Freeze_Subprogram;
4158 ---------------------
4159 -- Restore_Globals --
4160 ---------------------
4162 procedure Restore_Globals is
4163 begin
4164 Ghost_Mode := GM;
4165 end Restore_Globals;
4167 ------------------------------
4168 -- Wrap_Imported_Subprogram --
4169 ------------------------------
4171 -- The issue here is that our normal approach of checking preconditions
4172 -- and postconditions does not work for imported procedures, since we
4173 -- are not generating code for the body. To get around this we create
4174 -- a wrapper, as shown by the following example:
4176 -- procedure K (A : Integer);
4177 -- pragma Import (C, K);
4179 -- The spec is rewritten by removing the effects of pragma Import, but
4180 -- leaving the convention unchanged, as though the source had said:
4182 -- procedure K (A : Integer);
4183 -- pragma Convention (C, K);
4185 -- and we create a body, added to the entity K freeze actions, which
4186 -- looks like:
4188 -- procedure K (A : Integer) is
4189 -- procedure K (A : Integer);
4190 -- pragma Import (C, K);
4191 -- begin
4192 -- K (A);
4193 -- end K;
4195 -- Now the contract applies in the normal way to the outer procedure,
4196 -- and the inner procedure has no contracts, so there is no problem
4197 -- in just calling it to get the original effect.
4199 -- In the case of a function, we create an appropriate return statement
4200 -- for the subprogram body that calls the inner procedure.
4202 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
4203 Loc : constant Source_Ptr := Sloc (E);
4204 CE : constant Name_Id := Chars (E);
4205 Spec : Node_Id;
4206 Parms : List_Id;
4207 Stmt : Node_Id;
4208 Iprag : Node_Id;
4209 Bod : Node_Id;
4210 Forml : Entity_Id;
4212 begin
4213 -- Nothing to do if not imported
4215 if not Is_Imported (E) then
4216 return;
4218 -- Test enabling conditions for wrapping
4220 elsif Is_Subprogram (E)
4221 and then Present (Contract (E))
4222 and then Present (Pre_Post_Conditions (Contract (E)))
4223 and then not GNATprove_Mode
4224 then
4225 -- Here we do the wrap
4227 -- Note on calls to Copy_Separate_Tree. The trees we are copying
4228 -- here are fully analyzed, but we definitely want fully syntactic
4229 -- unanalyzed trees in the body we construct, so that the analysis
4230 -- generates the right visibility, and that is exactly what the
4231 -- calls to Copy_Separate_Tree give us.
4233 -- Acquire copy of Inline pragma, and indicate that it does not
4234 -- come from an aspect, as it applies to an internal entity.
4236 Iprag := Copy_Separate_Tree (Import_Pragma (E));
4237 Set_From_Aspect_Specification (Iprag, False);
4239 -- Fix up spec to be not imported any more
4241 Set_Is_Imported (E, False);
4242 Set_Interface_Name (E, Empty);
4243 Set_Has_Completion (E, False);
4244 Set_Import_Pragma (E, Empty);
4246 -- Grab the subprogram declaration and specification
4248 Spec := Declaration_Node (E);
4250 -- Build parameter list that we need
4252 Parms := New_List;
4253 Forml := First_Formal (E);
4254 while Present (Forml) loop
4255 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
4256 Next_Formal (Forml);
4257 end loop;
4259 -- Build the call
4261 if Ekind_In (E, E_Function, E_Generic_Function) then
4262 Stmt :=
4263 Make_Simple_Return_Statement (Loc,
4264 Expression =>
4265 Make_Function_Call (Loc,
4266 Name => Make_Identifier (Loc, CE),
4267 Parameter_Associations => Parms));
4269 else
4270 Stmt :=
4271 Make_Procedure_Call_Statement (Loc,
4272 Name => Make_Identifier (Loc, CE),
4273 Parameter_Associations => Parms);
4274 end if;
4276 -- Now build the body
4278 Bod :=
4279 Make_Subprogram_Body (Loc,
4280 Specification =>
4281 Copy_Separate_Tree (Spec),
4282 Declarations => New_List (
4283 Make_Subprogram_Declaration (Loc,
4284 Specification =>
4285 Copy_Separate_Tree (Spec)),
4286 Iprag),
4287 Handled_Statement_Sequence =>
4288 Make_Handled_Sequence_Of_Statements (Loc,
4289 Statements => New_List (Stmt),
4290 End_Label => Make_Identifier (Loc, CE)));
4292 -- Append the body to freeze result
4294 Add_To_Result (Bod);
4295 return;
4297 -- Case of imported subprogram that does not get wrapped
4299 else
4300 -- Set Is_Public. All imported entities need an external symbol
4301 -- created for them since they are always referenced from another
4302 -- object file. Note this used to be set when we set Is_Imported
4303 -- back in Sem_Prag, but now we delay it to this point, since we
4304 -- don't want to set this flag if we wrap an imported subprogram.
4306 Set_Is_Public (E);
4307 end if;
4308 end Wrap_Imported_Subprogram;
4310 -- Start of processing for Freeze_Entity
4312 begin
4313 -- The entity being frozen may be subject to pragma Ghost with policy
4314 -- Ignore. Set the mode now to ensure that any nodes generated during
4315 -- freezing are properly flagged as ignored Ghost.
4317 Set_Ghost_Mode_For_Freeze (E, N);
4319 -- We are going to test for various reasons why this entity need not be
4320 -- frozen here, but in the case of an Itype that's defined within a
4321 -- record, that test actually applies to the record.
4323 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
4324 Test_E := Scope (E);
4325 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
4326 and then Is_Record_Type (Underlying_Type (Scope (E)))
4327 then
4328 Test_E := Underlying_Type (Scope (E));
4329 end if;
4331 -- Do not freeze if already frozen since we only need one freeze node
4333 if Is_Frozen (E) then
4334 Restore_Globals;
4335 return No_List;
4337 -- It is improper to freeze an external entity within a generic because
4338 -- its freeze node will appear in a non-valid context. The entity will
4339 -- be frozen in the proper scope after the current generic is analyzed.
4340 -- However, aspects must be analyzed because they may be queried later
4341 -- within the generic itself, and the corresponding pragma or attribute
4342 -- definition has not been analyzed yet.
4344 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
4345 if Has_Delayed_Aspects (E) then
4346 Analyze_Aspects_At_Freeze_Point (E);
4347 end if;
4349 Restore_Globals;
4350 return No_List;
4352 -- AI05-0213: A formal incomplete type does not freeze the actual. In
4353 -- the instance, the same applies to the subtype renaming the actual.
4355 elsif Is_Private_Type (E)
4356 and then Is_Generic_Actual_Type (E)
4357 and then No (Full_View (Base_Type (E)))
4358 and then Ada_Version >= Ada_2012
4359 then
4360 Restore_Globals;
4361 return No_List;
4363 -- Formal subprograms are never frozen
4365 elsif Is_Formal_Subprogram (E) then
4366 Restore_Globals;
4367 return No_List;
4369 -- Generic types are never frozen as they lack delayed semantic checks
4371 elsif Is_Generic_Type (E) then
4372 Restore_Globals;
4373 return No_List;
4375 -- Do not freeze a global entity within an inner scope created during
4376 -- expansion. A call to subprogram E within some internal procedure
4377 -- (a stream attribute for example) might require freezing E, but the
4378 -- freeze node must appear in the same declarative part as E itself.
4379 -- The two-pass elaboration mechanism in gigi guarantees that E will
4380 -- be frozen before the inner call is elaborated. We exclude constants
4381 -- from this test, because deferred constants may be frozen early, and
4382 -- must be diagnosed (e.g. in the case of a deferred constant being used
4383 -- in a default expression). If the enclosing subprogram comes from
4384 -- source, or is a generic instance, then the freeze point is the one
4385 -- mandated by the language, and we freeze the entity. A subprogram that
4386 -- is a child unit body that acts as a spec does not have a spec that
4387 -- comes from source, but can only come from source.
4389 elsif In_Open_Scopes (Scope (Test_E))
4390 and then Scope (Test_E) /= Current_Scope
4391 and then Ekind (Test_E) /= E_Constant
4392 then
4393 declare
4394 S : Entity_Id;
4396 begin
4397 S := Current_Scope;
4398 while Present (S) loop
4399 if Is_Overloadable (S) then
4400 if Comes_From_Source (S)
4401 or else Is_Generic_Instance (S)
4402 or else Is_Child_Unit (S)
4403 then
4404 exit;
4405 else
4406 Restore_Globals;
4407 return No_List;
4408 end if;
4409 end if;
4411 S := Scope (S);
4412 end loop;
4413 end;
4415 -- Similarly, an inlined instance body may make reference to global
4416 -- entities, but these references cannot be the proper freezing point
4417 -- for them, and in the absence of inlining freezing will take place in
4418 -- their own scope. Normally instance bodies are analyzed after the
4419 -- enclosing compilation, and everything has been frozen at the proper
4420 -- place, but with front-end inlining an instance body is compiled
4421 -- before the end of the enclosing scope, and as a result out-of-order
4422 -- freezing must be prevented.
4424 elsif Front_End_Inlining
4425 and then In_Instance_Body
4426 and then Present (Scope (Test_E))
4427 then
4428 declare
4429 S : Entity_Id;
4431 begin
4432 S := Scope (Test_E);
4433 while Present (S) loop
4434 if Is_Generic_Instance (S) then
4435 exit;
4436 else
4437 S := Scope (S);
4438 end if;
4439 end loop;
4441 if No (S) then
4442 Restore_Globals;
4443 return No_List;
4444 end if;
4445 end;
4447 elsif Ekind (E) = E_Generic_Package then
4448 Result := Freeze_Generic_Entities (E);
4450 Restore_Globals;
4451 return Result;
4452 end if;
4454 -- Add checks to detect proper initialization of scalars that may appear
4455 -- as subprogram parameters.
4457 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
4458 Apply_Parameter_Validity_Checks (E);
4459 end if;
4461 -- Deal with delayed aspect specifications. The analysis of the aspect
4462 -- is required to be delayed to the freeze point, thus we analyze the
4463 -- pragma or attribute definition clause in the tree at this point. We
4464 -- also analyze the aspect specification node at the freeze point when
4465 -- the aspect doesn't correspond to pragma/attribute definition clause.
4467 if Has_Delayed_Aspects (E) then
4468 Analyze_Aspects_At_Freeze_Point (E);
4469 end if;
4471 -- Here to freeze the entity
4473 Set_Is_Frozen (E);
4475 -- Case of entity being frozen is other than a type
4477 if not Is_Type (E) then
4479 -- If entity is exported or imported and does not have an external
4480 -- name, now is the time to provide the appropriate default name.
4481 -- Skip this if the entity is stubbed, since we don't need a name
4482 -- for any stubbed routine. For the case on intrinsics, if no
4483 -- external name is specified, then calls will be handled in
4484 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
4485 -- external name is provided, then Expand_Intrinsic_Call leaves
4486 -- calls in place for expansion by GIGI.
4488 if (Is_Imported (E) or else Is_Exported (E))
4489 and then No (Interface_Name (E))
4490 and then Convention (E) /= Convention_Stubbed
4491 and then Convention (E) /= Convention_Intrinsic
4492 then
4493 Set_Encoded_Interface_Name
4494 (E, Get_Default_External_Name (E));
4496 -- If entity is an atomic object appearing in a declaration and
4497 -- the expression is an aggregate, assign it to a temporary to
4498 -- ensure that the actual assignment is done atomically rather
4499 -- than component-wise (the assignment to the temp may be done
4500 -- component-wise, but that is harmless).
4502 elsif Is_Atomic (E)
4503 and then Nkind (Parent (E)) = N_Object_Declaration
4504 and then Present (Expression (Parent (E)))
4505 and then Nkind (Expression (Parent (E))) = N_Aggregate
4506 and then Is_Atomic_Aggregate (Expression (Parent (E)), Etype (E))
4507 then
4508 null;
4509 end if;
4511 -- Subprogram case
4513 if Is_Subprogram (E) then
4515 -- Check for needing to wrap imported subprogram
4517 Wrap_Imported_Subprogram (E);
4519 -- Freeze all parameter types and the return type (RM 13.14(14)).
4520 -- However skip this for internal subprograms. This is also where
4521 -- any extra formal parameters are created since we now know
4522 -- whether the subprogram will use a foreign convention.
4524 -- In Ada 2012, freezing a subprogram does not always freeze
4525 -- the corresponding profile (see AI05-019). An attribute
4526 -- reference is not a freezing point of the profile.
4527 -- Other constructs that should not freeze ???
4529 -- This processing doesn't apply to internal entities (see below)
4531 if not Is_Internal (E) then
4532 if not Freeze_Profile (E) then
4533 Restore_Globals;
4534 return Result;
4535 end if;
4536 end if;
4538 -- Must freeze its parent first if it is a derived subprogram
4540 if Present (Alias (E)) then
4541 Freeze_And_Append (Alias (E), N, Result);
4542 end if;
4544 -- We don't freeze internal subprograms, because we don't normally
4545 -- want addition of extra formals or mechanism setting to happen
4546 -- for those. However we do pass through predefined dispatching
4547 -- cases, since extra formals may be needed in some cases, such as
4548 -- for the stream 'Input function (build-in-place formals).
4550 if not Is_Internal (E)
4551 or else Is_Predefined_Dispatching_Operation (E)
4552 then
4553 Freeze_Subprogram (E);
4554 end if;
4556 if Late_Freezing then
4557 Late_Freeze_Subprogram (E);
4558 Restore_Globals;
4559 return No_List;
4560 end if;
4562 -- If warning on suspicious contracts then check for the case of
4563 -- a postcondition other than False for a No_Return subprogram.
4565 if No_Return (E)
4566 and then Warn_On_Suspicious_Contract
4567 and then Present (Contract (E))
4568 then
4569 declare
4570 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
4571 Exp : Node_Id;
4573 begin
4574 while Present (Prag) loop
4575 if Nam_In (Pragma_Name (Prag), Name_Post,
4576 Name_Postcondition,
4577 Name_Refined_Post)
4578 then
4579 Exp :=
4580 Expression
4581 (First (Pragma_Argument_Associations (Prag)));
4583 if Nkind (Exp) /= N_Identifier
4584 or else Chars (Exp) /= Name_False
4585 then
4586 Error_Msg_NE
4587 ("useless postcondition, & is marked "
4588 & "No_Return?T?", Exp, E);
4589 end if;
4590 end if;
4592 Prag := Next_Pragma (Prag);
4593 end loop;
4594 end;
4595 end if;
4597 -- Here for other than a subprogram or type
4599 else
4600 -- If entity has a type, and it is not a generic unit, then
4601 -- freeze it first (RM 13.14(10)).
4603 if Present (Etype (E))
4604 and then Ekind (E) /= E_Generic_Function
4605 then
4606 Freeze_And_Append (Etype (E), N, Result);
4608 -- For an object of an anonymous array type, aspects on the
4609 -- object declaration apply to the type itself. This is the
4610 -- case for Atomic_Components, Volatile_Components, and
4611 -- Independent_Components. In these cases analysis of the
4612 -- generated pragma will mark the anonymous types accordingly,
4613 -- and the object itself does not require a freeze node.
4615 if Ekind (E) = E_Variable
4616 and then Is_Itype (Etype (E))
4617 and then Is_Array_Type (Etype (E))
4618 and then Has_Delayed_Aspects (E)
4619 then
4620 Set_Has_Delayed_Aspects (E, False);
4621 Set_Has_Delayed_Freeze (E, False);
4622 Set_Freeze_Node (E, Empty);
4623 end if;
4624 end if;
4626 -- Special processing for objects created by object declaration
4628 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
4630 -- Abstract type allowed only for C++ imported variables or
4631 -- constants.
4633 -- Note: we inhibit this check for objects that do not come
4634 -- from source because there is at least one case (the
4635 -- expansion of x'Class'Input where x is abstract) where we
4636 -- legitimately generate an abstract object.
4638 if Is_Abstract_Type (Etype (E))
4639 and then Comes_From_Source (Parent (E))
4640 and then not (Is_Imported (E)
4641 and then Is_CPP_Class (Etype (E)))
4642 then
4643 Error_Msg_N ("type of object cannot be abstract",
4644 Object_Definition (Parent (E)));
4646 if Is_CPP_Class (Etype (E)) then
4647 Error_Msg_NE
4648 ("\} may need a cpp_constructor",
4649 Object_Definition (Parent (E)), Etype (E));
4651 elsif Present (Expression (Parent (E))) then
4652 Error_Msg_N -- CODEFIX
4653 ("\maybe a class-wide type was meant",
4654 Object_Definition (Parent (E)));
4655 end if;
4656 end if;
4658 -- For object created by object declaration, perform required
4659 -- categorization (preelaborate and pure) checks. Defer these
4660 -- checks to freeze time since pragma Import inhibits default
4661 -- initialization and thus pragma Import affects these checks.
4663 Validate_Object_Declaration (Declaration_Node (E));
4665 -- If there is an address clause, check that it is valid
4667 Check_Address_Clause (E);
4669 -- Reset Is_True_Constant for non-constant aliased object. We
4670 -- consider that the fact that a non-constant object is aliased
4671 -- may indicate that some funny business is going on, e.g. an
4672 -- aliased object is passed by reference to a procedure which
4673 -- captures the address of the object, which is later used to
4674 -- assign a new value, even though the compiler thinks that
4675 -- it is not modified. Such code is highly dubious, but we
4676 -- choose to make it "work" for non-constant aliased objects.
4677 -- Note that we used to do this for all aliased objects,
4678 -- whether or not constant, but this caused anomalies down
4679 -- the line because we ended up with static objects that
4680 -- were not Is_True_Constant. Not resetting Is_True_Constant
4681 -- for (aliased) constant objects ensures that this anomaly
4682 -- never occurs.
4684 -- However, we don't do that for internal entities. We figure
4685 -- that if we deliberately set Is_True_Constant for an internal
4686 -- entity, e.g. a dispatch table entry, then we mean it.
4688 if Ekind (E) /= E_Constant
4689 and then (Is_Aliased (E) or else Is_Aliased (Etype (E)))
4690 and then not Is_Internal_Name (Chars (E))
4691 then
4692 Set_Is_True_Constant (E, False);
4693 end if;
4695 -- If the object needs any kind of default initialization, an
4696 -- error must be issued if No_Default_Initialization applies.
4697 -- The check doesn't apply to imported objects, which are not
4698 -- ever default initialized, and is why the check is deferred
4699 -- until freezing, at which point we know if Import applies.
4700 -- Deferred constants are also exempted from this test because
4701 -- their completion is explicit, or through an import pragma.
4703 if Ekind (E) = E_Constant
4704 and then Present (Full_View (E))
4705 then
4706 null;
4708 elsif Comes_From_Source (E)
4709 and then not Is_Imported (E)
4710 and then not Has_Init_Expression (Declaration_Node (E))
4711 and then
4712 ((Has_Non_Null_Base_Init_Proc (Etype (E))
4713 and then not No_Initialization (Declaration_Node (E))
4714 and then not Is_Value_Type (Etype (E))
4715 and then not Initialization_Suppressed (Etype (E)))
4716 or else
4717 (Needs_Simple_Initialization (Etype (E))
4718 and then not Is_Internal (E)))
4719 then
4720 Has_Default_Initialization := True;
4721 Check_Restriction
4722 (No_Default_Initialization, Declaration_Node (E));
4723 end if;
4725 -- Check that a Thread_Local_Storage variable does not have
4726 -- default initialization, and any explicit initialization must
4727 -- either be the null constant or a static constant.
4729 if Has_Pragma_Thread_Local_Storage (E) then
4730 declare
4731 Decl : constant Node_Id := Declaration_Node (E);
4732 begin
4733 if Has_Default_Initialization
4734 or else
4735 (Has_Init_Expression (Decl)
4736 and then
4737 (No (Expression (Decl))
4738 or else not
4739 (Is_OK_Static_Expression (Expression (Decl))
4740 or else
4741 Nkind (Expression (Decl)) = N_Null)))
4742 then
4743 Error_Msg_NE
4744 ("Thread_Local_Storage variable& is "
4745 & "improperly initialized", Decl, E);
4746 Error_Msg_NE
4747 ("\only allowed initialization is explicit "
4748 & "NULL or static expression", Decl, E);
4749 end if;
4750 end;
4751 end if;
4753 -- For imported objects, set Is_Public unless there is also an
4754 -- address clause, which means that there is no external symbol
4755 -- needed for the Import (Is_Public may still be set for other
4756 -- unrelated reasons). Note that we delayed this processing
4757 -- till freeze time so that we can be sure not to set the flag
4758 -- if there is an address clause. If there is such a clause,
4759 -- then the only purpose of the Import pragma is to suppress
4760 -- implicit initialization.
4762 if Is_Imported (E) and then No (Address_Clause (E)) then
4763 Set_Is_Public (E);
4764 end if;
4766 -- For source objects that are not Imported and are library
4767 -- level, if no linker section pragma was given inherit the
4768 -- appropriate linker section from the corresponding type.
4770 if Comes_From_Source (E)
4771 and then not Is_Imported (E)
4772 and then Is_Library_Level_Entity (E)
4773 and then No (Linker_Section_Pragma (E))
4774 then
4775 Set_Linker_Section_Pragma
4776 (E, Linker_Section_Pragma (Etype (E)));
4777 end if;
4779 -- For convention C objects of an enumeration type, warn if
4780 -- the size is not integer size and no explicit size given.
4781 -- Skip warning for Boolean, and Character, assume programmer
4782 -- expects 8-bit sizes for these cases.
4784 if (Convention (E) = Convention_C
4785 or else
4786 Convention (E) = Convention_CPP)
4787 and then Is_Enumeration_Type (Etype (E))
4788 and then not Is_Character_Type (Etype (E))
4789 and then not Is_Boolean_Type (Etype (E))
4790 and then Esize (Etype (E)) < Standard_Integer_Size
4791 and then not Has_Size_Clause (E)
4792 then
4793 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
4794 Error_Msg_N
4795 ("??convention C enumeration object has size less than ^",
4797 Error_Msg_N ("\??use explicit size clause to set size", E);
4798 end if;
4799 end if;
4801 -- Check that a constant which has a pragma Volatile[_Components]
4802 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
4804 -- Note: Atomic[_Components] also sets Volatile[_Components]
4806 if Ekind (E) = E_Constant
4807 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
4808 and then not Is_Imported (E)
4809 and then not Has_Boolean_Aspect_Import (E)
4810 then
4811 -- Make sure we actually have a pragma, and have not merely
4812 -- inherited the indication from elsewhere (e.g. an address
4813 -- clause, which is not good enough in RM terms).
4815 if Has_Rep_Pragma (E, Name_Atomic)
4816 or else
4817 Has_Rep_Pragma (E, Name_Atomic_Components)
4818 then
4819 Error_Msg_N
4820 ("stand alone atomic constant must be " &
4821 "imported (RM C.6(13))", E);
4823 elsif Has_Rep_Pragma (E, Name_Volatile)
4824 or else
4825 Has_Rep_Pragma (E, Name_Volatile_Components)
4826 then
4827 Error_Msg_N
4828 ("stand alone volatile constant must be " &
4829 "imported (RM C.6(13))", E);
4830 end if;
4831 end if;
4833 -- Static objects require special handling
4835 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
4836 and then Is_Statically_Allocated (E)
4837 then
4838 Freeze_Static_Object (E);
4839 end if;
4841 -- Remaining step is to layout objects
4843 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
4844 or else Is_Formal (E)
4845 then
4846 Layout_Object (E);
4847 end if;
4849 -- For an object that does not have delayed freezing, and whose
4850 -- initialization actions have been captured in a compound
4851 -- statement, move them back now directly within the enclosing
4852 -- statement sequence.
4854 if Ekind_In (E, E_Constant, E_Variable)
4855 and then not Has_Delayed_Freeze (E)
4856 then
4857 Explode_Initialization_Compound_Statement (E);
4858 end if;
4859 end if;
4861 -- Case of a type or subtype being frozen
4863 else
4864 -- We used to check here that a full type must have preelaborable
4865 -- initialization if it completes a private type specified with
4866 -- pragma Preelaborable_Initialization, but that missed cases where
4867 -- the types occur within a generic package, since the freezing
4868 -- that occurs within a containing scope generally skips traversal
4869 -- of a generic unit's declarations (those will be frozen within
4870 -- instances). This check was moved to Analyze_Package_Specification.
4872 -- The type may be defined in a generic unit. This can occur when
4873 -- freezing a generic function that returns the type (which is
4874 -- defined in a parent unit). It is clearly meaningless to freeze
4875 -- this type. However, if it is a subtype, its size may be determi-
4876 -- nable and used in subsequent checks, so might as well try to
4877 -- compute it.
4879 -- In Ada 2012, Freeze_Entities is also used in the front end to
4880 -- trigger the analysis of aspect expressions, so in this case we
4881 -- want to continue the freezing process.
4883 if Present (Scope (E))
4884 and then Is_Generic_Unit (Scope (E))
4885 and then
4886 (not Has_Predicates (E)
4887 and then not Has_Delayed_Freeze (E))
4888 then
4889 Check_Compile_Time_Size (E);
4890 Restore_Globals;
4891 return No_List;
4892 end if;
4894 -- Check for error of Type_Invariant'Class applied to an untagged
4895 -- type (check delayed to freeze time when full type is available).
4897 declare
4898 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
4899 begin
4900 if Present (Prag)
4901 and then Class_Present (Prag)
4902 and then not Is_Tagged_Type (E)
4903 then
4904 Error_Msg_NE
4905 ("Type_Invariant''Class cannot be specified for &",
4906 Prag, E);
4907 Error_Msg_N
4908 ("\can only be specified for a tagged type", Prag);
4909 end if;
4910 end;
4912 -- A Ghost type cannot be effectively volatile (SPARK RM 6.9(8))
4914 if Is_Ghost_Entity (E)
4915 and then Is_Effectively_Volatile (E)
4916 then
4917 Error_Msg_N ("ghost type & cannot be volatile", E);
4918 end if;
4920 -- Deal with special cases of freezing for subtype
4922 if E /= Base_Type (E) then
4924 -- Before we do anything else, a specialized test for the case of
4925 -- a size given for an array where the array needs to be packed,
4926 -- but was not so the size cannot be honored. This is the case
4927 -- where implicit packing may apply. The reason we do this so
4928 -- early is that if we have implicit packing, the layout of the
4929 -- base type is affected, so we must do this before we freeze
4930 -- the base type.
4932 -- We could do this processing only if implicit packing is enabled
4933 -- since in all other cases, the error would be caught by the back
4934 -- end. However, we choose to do the check even if we do not have
4935 -- implicit packing enabled, since this allows us to give a more
4936 -- useful error message (advising use of pragmas Implicit_Packing
4937 -- or Pack).
4939 if Is_Array_Type (E) then
4940 declare
4941 Ctyp : constant Entity_Id := Component_Type (E);
4942 Rsiz : constant Uint := RM_Size (Ctyp);
4943 SZ : constant Node_Id := Size_Clause (E);
4944 Btyp : constant Entity_Id := Base_Type (E);
4946 Lo : Node_Id;
4947 Hi : Node_Id;
4948 Indx : Node_Id;
4950 Num_Elmts : Uint;
4951 -- Number of elements in array
4953 begin
4954 -- Check enabling conditions. These are straightforward
4955 -- except for the test for a limited composite type. This
4956 -- eliminates the rare case of a array of limited components
4957 -- where there are issues of whether or not we can go ahead
4958 -- and pack the array (since we can't freely pack and unpack
4959 -- arrays if they are limited).
4961 -- Note that we check the root type explicitly because the
4962 -- whole point is we are doing this test before we have had
4963 -- a chance to freeze the base type (and it is that freeze
4964 -- action that causes stuff to be inherited).
4966 if Has_Size_Clause (E)
4967 and then Known_Static_RM_Size (E)
4968 and then not Is_Packed (E)
4969 and then not Has_Pragma_Pack (E)
4970 and then not Has_Component_Size_Clause (E)
4971 and then Known_Static_RM_Size (Ctyp)
4972 and then RM_Size (Ctyp) < 64
4973 and then not Is_Limited_Composite (E)
4974 and then not Is_Packed (Root_Type (E))
4975 and then not Has_Component_Size_Clause (Root_Type (E))
4976 and then not (CodePeer_Mode or GNATprove_Mode)
4977 then
4978 -- Compute number of elements in array
4980 Num_Elmts := Uint_1;
4981 Indx := First_Index (E);
4982 while Present (Indx) loop
4983 Get_Index_Bounds (Indx, Lo, Hi);
4985 if not (Compile_Time_Known_Value (Lo)
4986 and then
4987 Compile_Time_Known_Value (Hi))
4988 then
4989 goto No_Implicit_Packing;
4990 end if;
4992 Num_Elmts :=
4993 Num_Elmts *
4994 UI_Max (Uint_0,
4995 Expr_Value (Hi) - Expr_Value (Lo) + 1);
4996 Next_Index (Indx);
4997 end loop;
4999 -- What we are looking for here is the situation where
5000 -- the RM_Size given would be exactly right if there was
5001 -- a pragma Pack (resulting in the component size being
5002 -- the same as the RM_Size). Furthermore, the component
5003 -- type size must be an odd size (not a multiple of
5004 -- storage unit). If the component RM size is an exact
5005 -- number of storage units that is a power of two, the
5006 -- array is not packed and has a standard representation.
5008 if RM_Size (E) = Num_Elmts * Rsiz
5009 and then Rsiz mod System_Storage_Unit /= 0
5010 then
5011 -- For implicit packing mode, just set the component
5012 -- size silently.
5014 if Implicit_Packing then
5015 Set_Component_Size (Btyp, Rsiz);
5016 Set_Is_Bit_Packed_Array (Btyp);
5017 Set_Is_Packed (Btyp);
5018 Set_Has_Non_Standard_Rep (Btyp);
5020 -- Otherwise give an error message
5022 else
5023 Error_Msg_NE
5024 ("size given for& too small", SZ, E);
5025 Error_Msg_N -- CODEFIX
5026 ("\use explicit pragma Pack "
5027 & "or use pragma Implicit_Packing", SZ);
5028 end if;
5030 elsif RM_Size (E) = Num_Elmts * Rsiz
5031 and then Implicit_Packing
5032 and then
5033 (Rsiz / System_Storage_Unit = 1
5034 or else
5035 Rsiz / System_Storage_Unit = 2
5036 or else
5037 Rsiz / System_Storage_Unit = 4)
5038 then
5039 -- Not a packed array, but indicate the desired
5040 -- component size, for the back-end.
5042 Set_Component_Size (Btyp, Rsiz);
5043 end if;
5044 end if;
5045 end;
5046 end if;
5048 <<No_Implicit_Packing>>
5050 -- If ancestor subtype present, freeze that first. Note that this
5051 -- will also get the base type frozen. Need RM reference ???
5053 Atype := Ancestor_Subtype (E);
5055 if Present (Atype) then
5056 Freeze_And_Append (Atype, N, Result);
5058 -- No ancestor subtype present
5060 else
5061 -- See if we have a nearest ancestor that has a predicate.
5062 -- That catches the case of derived type with a predicate.
5063 -- Need RM reference here ???
5065 Atype := Nearest_Ancestor (E);
5067 if Present (Atype) and then Has_Predicates (Atype) then
5068 Freeze_And_Append (Atype, N, Result);
5069 end if;
5071 -- Freeze base type before freezing the entity (RM 13.14(15))
5073 if E /= Base_Type (E) then
5074 Freeze_And_Append (Base_Type (E), N, Result);
5075 end if;
5076 end if;
5078 -- A subtype inherits all the type-related representation aspects
5079 -- from its parents (RM 13.1(8)).
5081 Inherit_Aspects_At_Freeze_Point (E);
5083 -- For a derived type, freeze its parent type first (RM 13.14(15))
5085 elsif Is_Derived_Type (E) then
5086 Freeze_And_Append (Etype (E), N, Result);
5087 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5089 -- A derived type inherits each type-related representation aspect
5090 -- of its parent type that was directly specified before the
5091 -- declaration of the derived type (RM 13.1(15)).
5093 Inherit_Aspects_At_Freeze_Point (E);
5094 end if;
5096 -- Check for incompatible size and alignment for record type
5098 if Warn_On_Size_Alignment
5099 and then Is_Record_Type (E)
5100 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5102 -- If explicit Object_Size clause given assume that the programmer
5103 -- knows what he is doing, and expects the compiler behavior.
5105 and then not Has_Object_Size_Clause (E)
5107 -- Check for size not a multiple of alignment
5109 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5110 then
5111 declare
5112 SC : constant Node_Id := Size_Clause (E);
5113 AC : constant Node_Id := Alignment_Clause (E);
5114 Loc : Node_Id;
5115 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5117 begin
5118 if Present (SC) and then Present (AC) then
5120 -- Give a warning
5122 if Sloc (SC) > Sloc (AC) then
5123 Loc := SC;
5124 Error_Msg_NE
5125 ("??size is not a multiple of alignment for &", Loc, E);
5126 Error_Msg_Sloc := Sloc (AC);
5127 Error_Msg_Uint_1 := Alignment (E);
5128 Error_Msg_N ("\??alignment of ^ specified #", Loc);
5130 else
5131 Loc := AC;
5132 Error_Msg_NE
5133 ("??size is not a multiple of alignment for &", Loc, E);
5134 Error_Msg_Sloc := Sloc (SC);
5135 Error_Msg_Uint_1 := RM_Size (E);
5136 Error_Msg_N ("\??size of ^ specified #", Loc);
5137 end if;
5139 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5140 Error_Msg_N ("\??Object_Size will be increased to ^", Loc);
5141 end if;
5142 end;
5143 end if;
5145 -- Array type
5147 if Is_Array_Type (E) then
5148 Freeze_Array_Type (E);
5150 -- For a class-wide type, the corresponding specific type is
5151 -- frozen as well (RM 13.14(15))
5153 elsif Is_Class_Wide_Type (E) then
5154 Freeze_And_Append (Root_Type (E), N, Result);
5156 -- If the base type of the class-wide type is still incomplete,
5157 -- the class-wide remains unfrozen as well. This is legal when
5158 -- E is the formal of a primitive operation of some other type
5159 -- which is being frozen.
5161 if not Is_Frozen (Root_Type (E)) then
5162 Set_Is_Frozen (E, False);
5163 Restore_Globals;
5164 return Result;
5165 end if;
5167 -- The equivalent type associated with a class-wide subtype needs
5168 -- to be frozen to ensure that its layout is done.
5170 if Ekind (E) = E_Class_Wide_Subtype
5171 and then Present (Equivalent_Type (E))
5172 then
5173 Freeze_And_Append (Equivalent_Type (E), N, Result);
5174 end if;
5176 -- Generate an itype reference for a library-level class-wide type
5177 -- at the freeze point. Otherwise the first explicit reference to
5178 -- the type may appear in an inner scope which will be rejected by
5179 -- the back-end.
5181 if Is_Itype (E)
5182 and then Is_Compilation_Unit (Scope (E))
5183 then
5184 declare
5185 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5187 begin
5188 Set_Itype (Ref, E);
5190 -- From a gigi point of view, a class-wide subtype derives
5191 -- from its record equivalent type. As a result, the itype
5192 -- reference must appear after the freeze node of the
5193 -- equivalent type or gigi will reject the reference.
5195 if Ekind (E) = E_Class_Wide_Subtype
5196 and then Present (Equivalent_Type (E))
5197 then
5198 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5199 else
5200 Add_To_Result (Ref);
5201 end if;
5202 end;
5203 end if;
5205 -- For a record type or record subtype, freeze all component types
5206 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5207 -- using Is_Record_Type, because we don't want to attempt the freeze
5208 -- for the case of a private type with record extension (we will do
5209 -- that later when the full type is frozen).
5211 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype)
5212 and then not (Present (Scope (E))
5213 and then Is_Generic_Unit (Scope (E)))
5214 then
5215 Freeze_Record_Type (E);
5217 -- For a concurrent type, freeze corresponding record type. This does
5218 -- not correspond to any specific rule in the RM, but the record type
5219 -- is essentially part of the concurrent type. Also freeze all local
5220 -- entities. This includes record types created for entry parameter
5221 -- blocks and whatever local entities may appear in the private part.
5223 elsif Is_Concurrent_Type (E) then
5224 if Present (Corresponding_Record_Type (E)) then
5225 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5226 end if;
5228 Comp := First_Entity (E);
5229 while Present (Comp) loop
5230 if Is_Type (Comp) then
5231 Freeze_And_Append (Comp, N, Result);
5233 elsif (Ekind (Comp)) /= E_Function then
5235 -- The guard on the presence of the Etype seems to be needed
5236 -- for some CodePeer (-gnatcC) cases, but not clear why???
5238 if Present (Etype (Comp)) then
5239 if Is_Itype (Etype (Comp))
5240 and then Underlying_Type (Scope (Etype (Comp))) = E
5241 then
5242 Undelay_Type (Etype (Comp));
5243 end if;
5245 Freeze_And_Append (Etype (Comp), N, Result);
5246 end if;
5247 end if;
5249 Next_Entity (Comp);
5250 end loop;
5252 -- Private types are required to point to the same freeze node as
5253 -- their corresponding full views. The freeze node itself has to
5254 -- point to the partial view of the entity (because from the partial
5255 -- view, we can retrieve the full view, but not the reverse).
5256 -- However, in order to freeze correctly, we need to freeze the full
5257 -- view. If we are freezing at the end of a scope (or within the
5258 -- scope) of the private type, the partial and full views will have
5259 -- been swapped, the full view appears first in the entity chain and
5260 -- the swapping mechanism ensures that the pointers are properly set
5261 -- (on scope exit).
5263 -- If we encounter the partial view before the full view (e.g. when
5264 -- freezing from another scope), we freeze the full view, and then
5265 -- set the pointers appropriately since we cannot rely on swapping to
5266 -- fix things up (subtypes in an outer scope might not get swapped).
5268 -- If the full view is itself private, the above requirements apply
5269 -- to the underlying full view instead of the full view. But there is
5270 -- no swapping mechanism for the underlying full view so we need to
5271 -- set the pointers appropriately in both cases.
5273 elsif Is_Incomplete_Or_Private_Type (E)
5274 and then not Is_Generic_Type (E)
5275 then
5276 -- The construction of the dispatch table associated with library
5277 -- level tagged types forces freezing of all the primitives of the
5278 -- type, which may cause premature freezing of the partial view.
5279 -- For example:
5281 -- package Pkg is
5282 -- type T is tagged private;
5283 -- type DT is new T with private;
5284 -- procedure Prim (X : in out T; Y : in out DT'Class);
5285 -- private
5286 -- type T is tagged null record;
5287 -- Obj : T;
5288 -- type DT is new T with null record;
5289 -- end;
5291 -- In this case the type will be frozen later by the usual
5292 -- mechanism: an object declaration, an instantiation, or the
5293 -- end of a declarative part.
5295 if Is_Library_Level_Tagged_Type (E)
5296 and then not Present (Full_View (E))
5297 then
5298 Set_Is_Frozen (E, False);
5299 Restore_Globals;
5300 return Result;
5302 -- Case of full view present
5304 elsif Present (Full_View (E)) then
5306 -- If full view has already been frozen, then no further
5307 -- processing is required
5309 if Is_Frozen (Full_View (E)) then
5310 Set_Has_Delayed_Freeze (E, False);
5311 Set_Freeze_Node (E, Empty);
5313 -- Otherwise freeze full view and patch the pointers so that
5314 -- the freeze node will elaborate both views in the back end.
5315 -- However, if full view is itself private, freeze underlying
5316 -- full view instead and patch the pointers so that the freeze
5317 -- node will elaborate the three views in the back end.
5319 else
5320 declare
5321 Full : Entity_Id := Full_View (E);
5323 begin
5324 if Is_Private_Type (Full)
5325 and then Present (Underlying_Full_View (Full))
5326 then
5327 Full := Underlying_Full_View (Full);
5328 end if;
5330 Freeze_And_Append (Full, N, Result);
5332 if Full /= Full_View (E)
5333 and then Has_Delayed_Freeze (Full_View (E))
5334 then
5335 F_Node := Freeze_Node (Full);
5337 if Present (F_Node) then
5338 Set_Freeze_Node (Full_View (E), F_Node);
5339 Set_Entity (F_Node, Full_View (E));
5341 else
5342 Set_Has_Delayed_Freeze (Full_View (E), False);
5343 Set_Freeze_Node (Full_View (E), Empty);
5344 end if;
5345 end if;
5347 if Has_Delayed_Freeze (E) then
5348 F_Node := Freeze_Node (Full_View (E));
5350 if Present (F_Node) then
5351 Set_Freeze_Node (E, F_Node);
5352 Set_Entity (F_Node, E);
5354 else
5355 -- {Incomplete,Private}_Subtypes with Full_Views
5356 -- constrained by discriminants.
5358 Set_Has_Delayed_Freeze (E, False);
5359 Set_Freeze_Node (E, Empty);
5360 end if;
5361 end if;
5362 end;
5363 end if;
5365 Check_Debug_Info_Needed (E);
5367 -- AI-117 requires that the convention of a partial view be the
5368 -- same as the convention of the full view. Note that this is a
5369 -- recognized breach of privacy, but it's essential for logical
5370 -- consistency of representation, and the lack of a rule in
5371 -- RM95 was an oversight.
5373 Set_Convention (E, Convention (Full_View (E)));
5375 Set_Size_Known_At_Compile_Time (E,
5376 Size_Known_At_Compile_Time (Full_View (E)));
5378 -- Size information is copied from the full view to the
5379 -- incomplete or private view for consistency.
5381 -- We skip this is the full view is not a type. This is very
5382 -- strange of course, and can only happen as a result of
5383 -- certain illegalities, such as a premature attempt to derive
5384 -- from an incomplete type.
5386 if Is_Type (Full_View (E)) then
5387 Set_Size_Info (E, Full_View (E));
5388 Set_RM_Size (E, RM_Size (Full_View (E)));
5389 end if;
5391 Restore_Globals;
5392 return Result;
5394 -- Case of underlying full view present
5396 elsif Is_Private_Type (E)
5397 and then Present (Underlying_Full_View (E))
5398 then
5399 if not Is_Frozen (Underlying_Full_View (E)) then
5400 Freeze_And_Append (Underlying_Full_View (E), N, Result);
5401 end if;
5403 -- Patch the pointers so that the freeze node will elaborate
5404 -- both views in the back end.
5406 if Has_Delayed_Freeze (E) then
5407 F_Node := Freeze_Node (Underlying_Full_View (E));
5409 if Present (F_Node) then
5410 Set_Freeze_Node (E, F_Node);
5411 Set_Entity (F_Node, E);
5413 else
5414 Set_Has_Delayed_Freeze (E, False);
5415 Set_Freeze_Node (E, Empty);
5416 end if;
5417 end if;
5419 Check_Debug_Info_Needed (E);
5421 Restore_Globals;
5422 return Result;
5424 -- Case of no full view present. If entity is derived or subtype,
5425 -- it is safe to freeze, correctness depends on the frozen status
5426 -- of parent. Otherwise it is either premature usage, or a Taft
5427 -- amendment type, so diagnosis is at the point of use and the
5428 -- type might be frozen later.
5430 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
5431 null;
5433 else
5434 Set_Is_Frozen (E, False);
5435 Restore_Globals;
5436 return No_List;
5437 end if;
5439 -- For access subprogram, freeze types of all formals, the return
5440 -- type was already frozen, since it is the Etype of the function.
5441 -- Formal types can be tagged Taft amendment types, but otherwise
5442 -- they cannot be incomplete.
5444 elsif Ekind (E) = E_Subprogram_Type then
5445 Formal := First_Formal (E);
5446 while Present (Formal) loop
5447 if Ekind (Etype (Formal)) = E_Incomplete_Type
5448 and then No (Full_View (Etype (Formal)))
5449 and then not Is_Value_Type (Etype (Formal))
5450 then
5451 if Is_Tagged_Type (Etype (Formal)) then
5452 null;
5454 -- AI05-151: Incomplete types are allowed in access to
5455 -- subprogram specifications.
5457 elsif Ada_Version < Ada_2012 then
5458 Error_Msg_NE
5459 ("invalid use of incomplete type&", E, Etype (Formal));
5460 end if;
5461 end if;
5463 Freeze_And_Append (Etype (Formal), N, Result);
5464 Next_Formal (Formal);
5465 end loop;
5467 Freeze_Subprogram (E);
5469 -- For access to a protected subprogram, freeze the equivalent type
5470 -- (however this is not set if we are not generating code or if this
5471 -- is an anonymous type used just for resolution).
5473 elsif Is_Access_Protected_Subprogram_Type (E) then
5474 if Present (Equivalent_Type (E)) then
5475 Freeze_And_Append (Equivalent_Type (E), N, Result);
5476 end if;
5477 end if;
5479 -- Generic types are never seen by the back-end, and are also not
5480 -- processed by the expander (since the expander is turned off for
5481 -- generic processing), so we never need freeze nodes for them.
5483 if Is_Generic_Type (E) then
5484 Restore_Globals;
5485 return Result;
5486 end if;
5488 -- Some special processing for non-generic types to complete
5489 -- representation details not known till the freeze point.
5491 if Is_Fixed_Point_Type (E) then
5492 Freeze_Fixed_Point_Type (E);
5494 -- Some error checks required for ordinary fixed-point type. Defer
5495 -- these till the freeze-point since we need the small and range
5496 -- values. We only do these checks for base types
5498 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
5499 if Small_Value (E) < Ureal_2_M_80 then
5500 Error_Msg_Name_1 := Name_Small;
5501 Error_Msg_N
5502 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
5504 elsif Small_Value (E) > Ureal_2_80 then
5505 Error_Msg_Name_1 := Name_Small;
5506 Error_Msg_N
5507 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
5508 end if;
5510 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
5511 Error_Msg_Name_1 := Name_First;
5512 Error_Msg_N
5513 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
5514 end if;
5516 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
5517 Error_Msg_Name_1 := Name_Last;
5518 Error_Msg_N
5519 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
5520 end if;
5521 end if;
5523 elsif Is_Enumeration_Type (E) then
5524 Freeze_Enumeration_Type (E);
5526 elsif Is_Integer_Type (E) then
5527 Adjust_Esize_For_Alignment (E);
5529 if Is_Modular_Integer_Type (E)
5530 and then Warn_On_Suspicious_Modulus_Value
5531 then
5532 Check_Suspicious_Modulus (E);
5533 end if;
5535 -- The pool applies to named and anonymous access types, but not
5536 -- to subprogram and to internal types generated for 'Access
5537 -- references.
5539 elsif Is_Access_Type (E)
5540 and then not Is_Access_Subprogram_Type (E)
5541 and then Ekind (E) /= E_Access_Attribute_Type
5542 then
5543 -- If a pragma Default_Storage_Pool applies, and this type has no
5544 -- Storage_Pool or Storage_Size clause (which must have occurred
5545 -- before the freezing point), then use the default. This applies
5546 -- only to base types.
5548 -- None of this applies to access to subprograms, for which there
5549 -- are clearly no pools.
5551 if Present (Default_Pool)
5552 and then Is_Base_Type (E)
5553 and then not Has_Storage_Size_Clause (E)
5554 and then No (Associated_Storage_Pool (E))
5555 then
5556 -- Case of pragma Default_Storage_Pool (null)
5558 if Nkind (Default_Pool) = N_Null then
5559 Set_No_Pool_Assigned (E);
5561 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
5563 else
5564 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
5565 end if;
5566 end if;
5568 -- Check restriction for standard storage pool
5570 if No (Associated_Storage_Pool (E)) then
5571 Check_Restriction (No_Standard_Storage_Pools, E);
5572 end if;
5574 -- Deal with error message for pure access type. This is not an
5575 -- error in Ada 2005 if there is no pool (see AI-366).
5577 if Is_Pure_Unit_Access_Type (E)
5578 and then (Ada_Version < Ada_2005
5579 or else not No_Pool_Assigned (E))
5580 and then not Is_Generic_Unit (Scope (E))
5581 then
5582 Error_Msg_N ("named access type not allowed in pure unit", E);
5584 if Ada_Version >= Ada_2005 then
5585 Error_Msg_N
5586 ("\would be legal if Storage_Size of 0 given??", E);
5588 elsif No_Pool_Assigned (E) then
5589 Error_Msg_N
5590 ("\would be legal in Ada 2005??", E);
5592 else
5593 Error_Msg_N
5594 ("\would be legal in Ada 2005 if "
5595 & "Storage_Size of 0 given??", E);
5596 end if;
5597 end if;
5598 end if;
5600 -- Case of composite types
5602 if Is_Composite_Type (E) then
5604 -- AI-117 requires that all new primitives of a tagged type must
5605 -- inherit the convention of the full view of the type. Inherited
5606 -- and overriding operations are defined to inherit the convention
5607 -- of their parent or overridden subprogram (also specified in
5608 -- AI-117), which will have occurred earlier (in Derive_Subprogram
5609 -- and New_Overloaded_Entity). Here we set the convention of
5610 -- primitives that are still convention Ada, which will ensure
5611 -- that any new primitives inherit the type's convention. Class-
5612 -- wide types can have a foreign convention inherited from their
5613 -- specific type, but are excluded from this since they don't have
5614 -- any associated primitives.
5616 if Is_Tagged_Type (E)
5617 and then not Is_Class_Wide_Type (E)
5618 and then Convention (E) /= Convention_Ada
5619 then
5620 declare
5621 Prim_List : constant Elist_Id := Primitive_Operations (E);
5622 Prim : Elmt_Id;
5624 begin
5625 Prim := First_Elmt (Prim_List);
5626 while Present (Prim) loop
5627 if Convention (Node (Prim)) = Convention_Ada then
5628 Set_Convention (Node (Prim), Convention (E));
5629 end if;
5631 Next_Elmt (Prim);
5632 end loop;
5633 end;
5634 end if;
5636 -- If the type is a simple storage pool type, then this is where
5637 -- we attempt to locate and validate its Allocate, Deallocate, and
5638 -- Storage_Size operations (the first is required, and the latter
5639 -- two are optional). We also verify that the full type for a
5640 -- private type is allowed to be a simple storage pool type.
5642 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
5643 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
5644 then
5645 -- If the type is marked Has_Private_Declaration, then this is
5646 -- a full type for a private type that was specified with the
5647 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
5648 -- pragma is allowed for the full type (for example, it can't
5649 -- be an array type, or a nonlimited record type).
5651 if Has_Private_Declaration (E) then
5652 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
5653 and then not Is_Private_Type (E)
5654 then
5655 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
5656 Error_Msg_N
5657 ("pragma% can only apply to full type that is an " &
5658 "explicitly limited type", E);
5659 end if;
5660 end if;
5662 Validate_Simple_Pool_Ops : declare
5663 Pool_Type : Entity_Id renames E;
5664 Address_Type : constant Entity_Id := RTE (RE_Address);
5665 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
5667 procedure Validate_Simple_Pool_Op_Formal
5668 (Pool_Op : Entity_Id;
5669 Pool_Op_Formal : in out Entity_Id;
5670 Expected_Mode : Formal_Kind;
5671 Expected_Type : Entity_Id;
5672 Formal_Name : String;
5673 OK_Formal : in out Boolean);
5674 -- Validate one formal Pool_Op_Formal of the candidate pool
5675 -- operation Pool_Op. The formal must be of Expected_Type
5676 -- and have mode Expected_Mode. OK_Formal will be set to
5677 -- False if the formal doesn't match. If OK_Formal is False
5678 -- on entry, then the formal will effectively be ignored
5679 -- (because validation of the pool op has already failed).
5680 -- Upon return, Pool_Op_Formal will be updated to the next
5681 -- formal, if any.
5683 procedure Validate_Simple_Pool_Operation
5684 (Op_Name : Name_Id);
5685 -- Search for and validate a simple pool operation with the
5686 -- name Op_Name. If the name is Allocate, then there must be
5687 -- exactly one such primitive operation for the simple pool
5688 -- type. If the name is Deallocate or Storage_Size, then
5689 -- there can be at most one such primitive operation. The
5690 -- profile of the located primitive must conform to what
5691 -- is expected for each operation.
5693 ------------------------------------
5694 -- Validate_Simple_Pool_Op_Formal --
5695 ------------------------------------
5697 procedure Validate_Simple_Pool_Op_Formal
5698 (Pool_Op : Entity_Id;
5699 Pool_Op_Formal : in out Entity_Id;
5700 Expected_Mode : Formal_Kind;
5701 Expected_Type : Entity_Id;
5702 Formal_Name : String;
5703 OK_Formal : in out Boolean)
5705 begin
5706 -- If OK_Formal is False on entry, then simply ignore
5707 -- the formal, because an earlier formal has already
5708 -- been flagged.
5710 if not OK_Formal then
5711 return;
5713 -- If no formal is passed in, then issue an error for a
5714 -- missing formal.
5716 elsif not Present (Pool_Op_Formal) then
5717 Error_Msg_NE
5718 ("simple storage pool op missing formal " &
5719 Formal_Name & " of type&", Pool_Op, Expected_Type);
5720 OK_Formal := False;
5722 return;
5723 end if;
5725 if Etype (Pool_Op_Formal) /= Expected_Type then
5727 -- If the pool type was expected for this formal, then
5728 -- this will not be considered a candidate operation
5729 -- for the simple pool, so we unset OK_Formal so that
5730 -- the op and any later formals will be ignored.
5732 if Expected_Type = Pool_Type then
5733 OK_Formal := False;
5735 return;
5737 else
5738 Error_Msg_NE
5739 ("wrong type for formal " & Formal_Name &
5740 " of simple storage pool op; expected type&",
5741 Pool_Op_Formal, Expected_Type);
5742 end if;
5743 end if;
5745 -- Issue error if formal's mode is not the expected one
5747 if Ekind (Pool_Op_Formal) /= Expected_Mode then
5748 Error_Msg_N
5749 ("wrong mode for formal of simple storage pool op",
5750 Pool_Op_Formal);
5751 end if;
5753 -- Advance to the next formal
5755 Next_Formal (Pool_Op_Formal);
5756 end Validate_Simple_Pool_Op_Formal;
5758 ------------------------------------
5759 -- Validate_Simple_Pool_Operation --
5760 ------------------------------------
5762 procedure Validate_Simple_Pool_Operation
5763 (Op_Name : Name_Id)
5765 Op : Entity_Id;
5766 Found_Op : Entity_Id := Empty;
5767 Formal : Entity_Id;
5768 Is_OK : Boolean;
5770 begin
5771 pragma Assert
5772 (Nam_In (Op_Name, Name_Allocate,
5773 Name_Deallocate,
5774 Name_Storage_Size));
5776 Error_Msg_Name_1 := Op_Name;
5778 -- For each homonym declared immediately in the scope
5779 -- of the simple storage pool type, determine whether
5780 -- the homonym is an operation of the pool type, and,
5781 -- if so, check that its profile is as expected for
5782 -- a simple pool operation of that name.
5784 Op := Get_Name_Entity_Id (Op_Name);
5785 while Present (Op) loop
5786 if Ekind_In (Op, E_Function, E_Procedure)
5787 and then Scope (Op) = Current_Scope
5788 then
5789 Formal := First_Entity (Op);
5791 Is_OK := True;
5793 -- The first parameter must be of the pool type
5794 -- in order for the operation to qualify.
5796 if Op_Name = Name_Storage_Size then
5797 Validate_Simple_Pool_Op_Formal
5798 (Op, Formal, E_In_Parameter, Pool_Type,
5799 "Pool", Is_OK);
5800 else
5801 Validate_Simple_Pool_Op_Formal
5802 (Op, Formal, E_In_Out_Parameter, Pool_Type,
5803 "Pool", Is_OK);
5804 end if;
5806 -- If another operation with this name has already
5807 -- been located for the type, then flag an error,
5808 -- since we only allow the type to have a single
5809 -- such primitive.
5811 if Present (Found_Op) and then Is_OK then
5812 Error_Msg_NE
5813 ("only one % operation allowed for " &
5814 "simple storage pool type&", Op, Pool_Type);
5815 end if;
5817 -- In the case of Allocate and Deallocate, a formal
5818 -- of type System.Address is required.
5820 if Op_Name = Name_Allocate then
5821 Validate_Simple_Pool_Op_Formal
5822 (Op, Formal, E_Out_Parameter,
5823 Address_Type, "Storage_Address", Is_OK);
5825 elsif Op_Name = Name_Deallocate then
5826 Validate_Simple_Pool_Op_Formal
5827 (Op, Formal, E_In_Parameter,
5828 Address_Type, "Storage_Address", Is_OK);
5829 end if;
5831 -- In the case of Allocate and Deallocate, formals
5832 -- of type Storage_Count are required as the third
5833 -- and fourth parameters.
5835 if Op_Name /= Name_Storage_Size then
5836 Validate_Simple_Pool_Op_Formal
5837 (Op, Formal, E_In_Parameter,
5838 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
5839 Validate_Simple_Pool_Op_Formal
5840 (Op, Formal, E_In_Parameter,
5841 Stg_Cnt_Type, "Alignment", Is_OK);
5842 end if;
5844 -- If no mismatched formals have been found (Is_OK)
5845 -- and no excess formals are present, then this
5846 -- operation has been validated, so record it.
5848 if not Present (Formal) and then Is_OK then
5849 Found_Op := Op;
5850 end if;
5851 end if;
5853 Op := Homonym (Op);
5854 end loop;
5856 -- There must be a valid Allocate operation for the type,
5857 -- so issue an error if none was found.
5859 if Op_Name = Name_Allocate
5860 and then not Present (Found_Op)
5861 then
5862 Error_Msg_N ("missing % operation for simple " &
5863 "storage pool type", Pool_Type);
5865 elsif Present (Found_Op) then
5867 -- Simple pool operations can't be abstract
5869 if Is_Abstract_Subprogram (Found_Op) then
5870 Error_Msg_N
5871 ("simple storage pool operation must not be " &
5872 "abstract", Found_Op);
5873 end if;
5875 -- The Storage_Size operation must be a function with
5876 -- Storage_Count as its result type.
5878 if Op_Name = Name_Storage_Size then
5879 if Ekind (Found_Op) = E_Procedure then
5880 Error_Msg_N
5881 ("% operation must be a function", Found_Op);
5883 elsif Etype (Found_Op) /= Stg_Cnt_Type then
5884 Error_Msg_NE
5885 ("wrong result type for%, expected type&",
5886 Found_Op, Stg_Cnt_Type);
5887 end if;
5889 -- Allocate and Deallocate must be procedures
5891 elsif Ekind (Found_Op) = E_Function then
5892 Error_Msg_N
5893 ("% operation must be a procedure", Found_Op);
5894 end if;
5895 end if;
5896 end Validate_Simple_Pool_Operation;
5898 -- Start of processing for Validate_Simple_Pool_Ops
5900 begin
5901 Validate_Simple_Pool_Operation (Name_Allocate);
5902 Validate_Simple_Pool_Operation (Name_Deallocate);
5903 Validate_Simple_Pool_Operation (Name_Storage_Size);
5904 end Validate_Simple_Pool_Ops;
5905 end if;
5906 end if;
5908 -- Now that all types from which E may depend are frozen, see if the
5909 -- size is known at compile time, if it must be unsigned, or if
5910 -- strict alignment is required
5912 Check_Compile_Time_Size (E);
5913 Check_Unsigned_Type (E);
5915 if Base_Type (E) = E then
5916 Check_Strict_Alignment (E);
5917 end if;
5919 -- Do not allow a size clause for a type which does not have a size
5920 -- that is known at compile time
5922 if Has_Size_Clause (E)
5923 and then not Size_Known_At_Compile_Time (E)
5924 then
5925 -- Suppress this message if errors posted on E, even if we are
5926 -- in all errors mode, since this is often a junk message
5928 if not Error_Posted (E) then
5929 Error_Msg_N
5930 ("size clause not allowed for variable length type",
5931 Size_Clause (E));
5932 end if;
5933 end if;
5935 -- Now we set/verify the representation information, in particular
5936 -- the size and alignment values. This processing is not required for
5937 -- generic types, since generic types do not play any part in code
5938 -- generation, and so the size and alignment values for such types
5939 -- are irrelevant. Ditto for types declared within a generic unit,
5940 -- which may have components that depend on generic parameters, and
5941 -- that will be recreated in an instance.
5943 if Inside_A_Generic then
5944 null;
5946 -- Otherwise we call the layout procedure
5948 else
5949 Layout_Type (E);
5950 end if;
5952 -- If this is an access to subprogram whose designated type is itself
5953 -- a subprogram type, the return type of this anonymous subprogram
5954 -- type must be decorated as well.
5956 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
5957 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
5958 then
5959 Layout_Type (Etype (Designated_Type (E)));
5960 end if;
5962 -- If the type has a Defaut_Value/Default_Component_Value aspect,
5963 -- this is where we analye the expression (after the type is frozen,
5964 -- since in the case of Default_Value, we are analyzing with the
5965 -- type itself, and we treat Default_Component_Value similarly for
5966 -- the sake of uniformity).
5968 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
5969 declare
5970 Nam : Name_Id;
5971 Exp : Node_Id;
5972 Typ : Entity_Id;
5974 begin
5975 if Is_Scalar_Type (E) then
5976 Nam := Name_Default_Value;
5977 Typ := E;
5978 Exp := Default_Aspect_Value (Typ);
5979 else
5980 Nam := Name_Default_Component_Value;
5981 Typ := Component_Type (E);
5982 Exp := Default_Aspect_Component_Value (E);
5983 end if;
5985 Analyze_And_Resolve (Exp, Typ);
5987 if Etype (Exp) /= Any_Type then
5988 if not Is_OK_Static_Expression (Exp) then
5989 Error_Msg_Name_1 := Nam;
5990 Flag_Non_Static_Expr
5991 ("aspect% requires static expression", Exp);
5992 end if;
5993 end if;
5994 end;
5995 end if;
5997 -- End of freeze processing for type entities
5998 end if;
6000 -- Here is where we logically freeze the current entity. If it has a
6001 -- freeze node, then this is the point at which the freeze node is
6002 -- linked into the result list.
6004 if Has_Delayed_Freeze (E) then
6006 -- If a freeze node is already allocated, use it, otherwise allocate
6007 -- a new one. The preallocation happens in the case of anonymous base
6008 -- types, where we preallocate so that we can set First_Subtype_Link.
6009 -- Note that we reset the Sloc to the current freeze location.
6011 if Present (Freeze_Node (E)) then
6012 F_Node := Freeze_Node (E);
6013 Set_Sloc (F_Node, Loc);
6015 else
6016 F_Node := New_Node (N_Freeze_Entity, Loc);
6017 Set_Freeze_Node (E, F_Node);
6018 Set_Access_Types_To_Process (F_Node, No_Elist);
6019 Set_TSS_Elist (F_Node, No_Elist);
6020 Set_Actions (F_Node, No_List);
6021 end if;
6023 Set_Entity (F_Node, E);
6024 Add_To_Result (F_Node);
6026 -- A final pass over record types with discriminants. If the type
6027 -- has an incomplete declaration, there may be constrained access
6028 -- subtypes declared elsewhere, which do not depend on the discrimi-
6029 -- nants of the type, and which are used as component types (i.e.
6030 -- the full view is a recursive type). The designated types of these
6031 -- subtypes can only be elaborated after the type itself, and they
6032 -- need an itype reference.
6034 if Ekind (E) = E_Record_Type
6035 and then Has_Discriminants (E)
6036 then
6037 declare
6038 Comp : Entity_Id;
6039 IR : Node_Id;
6040 Typ : Entity_Id;
6042 begin
6043 Comp := First_Component (E);
6044 while Present (Comp) loop
6045 Typ := Etype (Comp);
6047 if Ekind (Comp) = E_Component
6048 and then Is_Access_Type (Typ)
6049 and then Scope (Typ) /= E
6050 and then Base_Type (Designated_Type (Typ)) = E
6051 and then Is_Itype (Designated_Type (Typ))
6052 then
6053 IR := Make_Itype_Reference (Sloc (Comp));
6054 Set_Itype (IR, Designated_Type (Typ));
6055 Append (IR, Result);
6056 end if;
6058 Next_Component (Comp);
6059 end loop;
6060 end;
6061 end if;
6062 end if;
6064 -- When a type is frozen, the first subtype of the type is frozen as
6065 -- well (RM 13.14(15)). This has to be done after freezing the type,
6066 -- since obviously the first subtype depends on its own base type.
6068 if Is_Type (E) then
6069 Freeze_And_Append (First_Subtype (E), N, Result);
6071 -- If we just froze a tagged non-class wide record, then freeze the
6072 -- corresponding class-wide type. This must be done after the tagged
6073 -- type itself is frozen, because the class-wide type refers to the
6074 -- tagged type which generates the class.
6076 if Is_Tagged_Type (E)
6077 and then not Is_Class_Wide_Type (E)
6078 and then Present (Class_Wide_Type (E))
6079 then
6080 Freeze_And_Append (Class_Wide_Type (E), N, Result);
6081 end if;
6082 end if;
6084 Check_Debug_Info_Needed (E);
6086 -- Special handling for subprograms
6088 if Is_Subprogram (E) then
6090 -- If subprogram has address clause then reset Is_Public flag, since
6091 -- we do not want the backend to generate external references.
6093 if Present (Address_Clause (E))
6094 and then not Is_Library_Level_Entity (E)
6095 then
6096 Set_Is_Public (E, False);
6097 end if;
6098 end if;
6100 Restore_Globals;
6101 return Result;
6102 end Freeze_Entity;
6104 -----------------------------
6105 -- Freeze_Enumeration_Type --
6106 -----------------------------
6108 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6109 begin
6110 -- By default, if no size clause is present, an enumeration type with
6111 -- Convention C is assumed to interface to a C enum, and has integer
6112 -- size. This applies to types. For subtypes, verify that its base
6113 -- type has no size clause either. Treat other foreign conventions
6114 -- in the same way, and also make sure alignment is set right.
6116 if Has_Foreign_Convention (Typ)
6117 and then not Has_Size_Clause (Typ)
6118 and then not Has_Size_Clause (Base_Type (Typ))
6119 and then Esize (Typ) < Standard_Integer_Size
6121 -- Don't do this if Short_Enums on target
6123 and then not Target_Short_Enums
6124 then
6125 Init_Esize (Typ, Standard_Integer_Size);
6126 Set_Alignment (Typ, Alignment (Standard_Integer));
6128 -- Normal Ada case or size clause present or not Long_C_Enums on target
6130 else
6131 -- If the enumeration type interfaces to C, and it has a size clause
6132 -- that specifies less than int size, it warrants a warning. The
6133 -- user may intend the C type to be an enum or a char, so this is
6134 -- not by itself an error that the Ada compiler can detect, but it
6135 -- it is a worth a heads-up. For Boolean and Character types we
6136 -- assume that the programmer has the proper C type in mind.
6138 if Convention (Typ) = Convention_C
6139 and then Has_Size_Clause (Typ)
6140 and then Esize (Typ) /= Esize (Standard_Integer)
6141 and then not Is_Boolean_Type (Typ)
6142 and then not Is_Character_Type (Typ)
6144 -- Don't do this if Short_Enums on target
6146 and then not Target_Short_Enums
6147 then
6148 Error_Msg_N
6149 ("C enum types have the size of a C int??", Size_Clause (Typ));
6150 end if;
6152 Adjust_Esize_For_Alignment (Typ);
6153 end if;
6154 end Freeze_Enumeration_Type;
6156 -----------------------
6157 -- Freeze_Expression --
6158 -----------------------
6160 procedure Freeze_Expression (N : Node_Id) is
6161 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6162 Typ : Entity_Id;
6163 Nam : Entity_Id;
6164 Desig_Typ : Entity_Id;
6165 P : Node_Id;
6166 Parent_P : Node_Id;
6168 Freeze_Outside : Boolean := False;
6169 -- This flag is set true if the entity must be frozen outside the
6170 -- current subprogram. This happens in the case of expander generated
6171 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6172 -- not freeze all entities like other bodies, but which nevertheless
6173 -- may reference entities that have to be frozen before the body and
6174 -- obviously cannot be frozen inside the body.
6176 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6177 -- If the expression is an array aggregate, the type of the component
6178 -- expressions is also frozen. If the component type is an access type
6179 -- and the expressions include allocators, the designed type is frozen
6180 -- as well.
6182 function In_Expanded_Body (N : Node_Id) return Boolean;
6183 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6184 -- it is the handled statement sequence of an expander-generated
6185 -- subprogram (init proc, stream subprogram, or renaming as body).
6186 -- If so, this is not a freezing context.
6188 -----------------------------------------
6189 -- Find_Aggregate_Component_Desig_Type --
6190 -----------------------------------------
6192 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6193 Assoc : Node_Id;
6194 Exp : Node_Id;
6196 begin
6197 if Present (Expressions (N)) then
6198 Exp := First (Expressions (N));
6199 while Present (Exp) loop
6200 if Nkind (Exp) = N_Allocator then
6201 return Designated_Type (Component_Type (Etype (N)));
6202 end if;
6204 Next (Exp);
6205 end loop;
6206 end if;
6208 if Present (Component_Associations (N)) then
6209 Assoc := First (Component_Associations (N));
6210 while Present (Assoc) loop
6211 if Nkind (Expression (Assoc)) = N_Allocator then
6212 return Designated_Type (Component_Type (Etype (N)));
6213 end if;
6215 Next (Assoc);
6216 end loop;
6217 end if;
6219 return Empty;
6220 end Find_Aggregate_Component_Desig_Type;
6222 ----------------------
6223 -- In_Expanded_Body --
6224 ----------------------
6226 function In_Expanded_Body (N : Node_Id) return Boolean is
6227 P : Node_Id;
6228 Id : Entity_Id;
6230 begin
6231 if Nkind (N) = N_Subprogram_Body then
6232 P := N;
6233 else
6234 P := Parent (N);
6235 end if;
6237 if Nkind (P) /= N_Subprogram_Body then
6238 return False;
6240 else
6241 Id := Defining_Unit_Name (Specification (P));
6243 -- The following are expander-created bodies, or bodies that
6244 -- are not freeze points.
6246 if Nkind (Id) = N_Defining_Identifier
6247 and then (Is_Init_Proc (Id)
6248 or else Is_TSS (Id, TSS_Stream_Input)
6249 or else Is_TSS (Id, TSS_Stream_Output)
6250 or else Is_TSS (Id, TSS_Stream_Read)
6251 or else Is_TSS (Id, TSS_Stream_Write)
6252 or else Nkind_In (Original_Node (P),
6253 N_Subprogram_Renaming_Declaration,
6254 N_Expression_Function))
6255 then
6256 return True;
6257 else
6258 return False;
6259 end if;
6260 end if;
6261 end In_Expanded_Body;
6263 -- Start of processing for Freeze_Expression
6265 begin
6266 -- Immediate return if freezing is inhibited. This flag is set by the
6267 -- analyzer to stop freezing on generated expressions that would cause
6268 -- freezing if they were in the source program, but which are not
6269 -- supposed to freeze, since they are created.
6271 if Must_Not_Freeze (N) then
6272 return;
6273 end if;
6275 -- If expression is non-static, then it does not freeze in a default
6276 -- expression, see section "Handling of Default Expressions" in the
6277 -- spec of package Sem for further details. Note that we have to make
6278 -- sure that we actually have a real expression (if we have a subtype
6279 -- indication, we can't test Is_OK_Static_Expression). However, we
6280 -- exclude the case of the prefix of an attribute of a static scalar
6281 -- subtype from this early return, because static subtype attributes
6282 -- should always cause freezing, even in default expressions, but
6283 -- the attribute may not have been marked as static yet (because in
6284 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
6285 -- Freeze_Expression on the prefix).
6287 if In_Spec_Exp
6288 and then Nkind (N) in N_Subexpr
6289 and then not Is_OK_Static_Expression (N)
6290 and then (Nkind (Parent (N)) /= N_Attribute_Reference
6291 or else not (Is_Entity_Name (N)
6292 and then Is_Type (Entity (N))
6293 and then Is_OK_Static_Subtype (Entity (N))))
6294 then
6295 return;
6296 end if;
6298 -- Freeze type of expression if not frozen already
6300 Typ := Empty;
6302 if Nkind (N) in N_Has_Etype then
6303 if not Is_Frozen (Etype (N)) then
6304 Typ := Etype (N);
6306 -- Base type may be an derived numeric type that is frozen at
6307 -- the point of declaration, but first_subtype is still unfrozen.
6309 elsif not Is_Frozen (First_Subtype (Etype (N))) then
6310 Typ := First_Subtype (Etype (N));
6311 end if;
6312 end if;
6314 -- For entity name, freeze entity if not frozen already. A special
6315 -- exception occurs for an identifier that did not come from source.
6316 -- We don't let such identifiers freeze a non-internal entity, i.e.
6317 -- an entity that did come from source, since such an identifier was
6318 -- generated by the expander, and cannot have any semantic effect on
6319 -- the freezing semantics. For example, this stops the parameter of
6320 -- an initialization procedure from freezing the variable.
6322 if Is_Entity_Name (N)
6323 and then not Is_Frozen (Entity (N))
6324 and then (Nkind (N) /= N_Identifier
6325 or else Comes_From_Source (N)
6326 or else not Comes_From_Source (Entity (N)))
6327 then
6328 Nam := Entity (N);
6330 if Present (Nam) and then Ekind (Nam) = E_Function then
6331 Check_Expression_Function (N, Nam);
6332 end if;
6334 else
6335 Nam := Empty;
6336 end if;
6338 -- For an allocator freeze designated type if not frozen already
6340 -- For an aggregate whose component type is an access type, freeze the
6341 -- designated type now, so that its freeze does not appear within the
6342 -- loop that might be created in the expansion of the aggregate. If the
6343 -- designated type is a private type without full view, the expression
6344 -- cannot contain an allocator, so the type is not frozen.
6346 -- For a function, we freeze the entity when the subprogram declaration
6347 -- is frozen, but a function call may appear in an initialization proc.
6348 -- before the declaration is frozen. We need to generate the extra
6349 -- formals, if any, to ensure that the expansion of the call includes
6350 -- the proper actuals. This only applies to Ada subprograms, not to
6351 -- imported ones.
6353 Desig_Typ := Empty;
6355 case Nkind (N) is
6356 when N_Allocator =>
6357 Desig_Typ := Designated_Type (Etype (N));
6359 when N_Aggregate =>
6360 if Is_Array_Type (Etype (N))
6361 and then Is_Access_Type (Component_Type (Etype (N)))
6362 then
6364 -- Check whether aggregate includes allocators.
6366 Desig_Typ := Find_Aggregate_Component_Desig_Type;
6367 end if;
6369 when N_Selected_Component |
6370 N_Indexed_Component |
6371 N_Slice =>
6373 if Is_Access_Type (Etype (Prefix (N))) then
6374 Desig_Typ := Designated_Type (Etype (Prefix (N)));
6375 end if;
6377 when N_Identifier =>
6378 if Present (Nam)
6379 and then Ekind (Nam) = E_Function
6380 and then Nkind (Parent (N)) = N_Function_Call
6381 and then Convention (Nam) = Convention_Ada
6382 then
6383 Create_Extra_Formals (Nam);
6384 end if;
6386 when others =>
6387 null;
6388 end case;
6390 if Desig_Typ /= Empty
6391 and then (Is_Frozen (Desig_Typ)
6392 or else (not Is_Fully_Defined (Desig_Typ)))
6393 then
6394 Desig_Typ := Empty;
6395 end if;
6397 -- All done if nothing needs freezing
6399 if No (Typ)
6400 and then No (Nam)
6401 and then No (Desig_Typ)
6402 then
6403 return;
6404 end if;
6406 -- Examine the enclosing context by climbing the parent chain. The
6407 -- traversal serves two purposes - to detect scenarios where freezeing
6408 -- is not needed and to find the proper insertion point for the freeze
6409 -- nodes. Although somewhat similar to Insert_Actions, this traversal
6410 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
6411 -- the tree may result in types being frozen too early.
6413 P := N;
6414 loop
6415 Parent_P := Parent (P);
6417 -- If we don't have a parent, then we are not in a well-formed tree.
6418 -- This is an unusual case, but there are some legitimate situations
6419 -- in which this occurs, notably when the expressions in the range of
6420 -- a type declaration are resolved. We simply ignore the freeze
6421 -- request in this case. Is this right ???
6423 if No (Parent_P) then
6424 return;
6425 end if;
6427 -- See if we have got to an appropriate point in the tree
6429 case Nkind (Parent_P) is
6431 -- A special test for the exception of (RM 13.14(8)) for the case
6432 -- of per-object expressions (RM 3.8(18)) occurring in component
6433 -- definition or a discrete subtype definition. Note that we test
6434 -- for a component declaration which includes both cases we are
6435 -- interested in, and furthermore the tree does not have explicit
6436 -- nodes for either of these two constructs.
6438 when N_Component_Declaration =>
6440 -- The case we want to test for here is an identifier that is
6441 -- a per-object expression, this is either a discriminant that
6442 -- appears in a context other than the component declaration
6443 -- or it is a reference to the type of the enclosing construct.
6445 -- For either of these cases, we skip the freezing
6447 if not In_Spec_Expression
6448 and then Nkind (N) = N_Identifier
6449 and then (Present (Entity (N)))
6450 then
6451 -- We recognize the discriminant case by just looking for
6452 -- a reference to a discriminant. It can only be one for
6453 -- the enclosing construct. Skip freezing in this case.
6455 if Ekind (Entity (N)) = E_Discriminant then
6456 return;
6458 -- For the case of a reference to the enclosing record,
6459 -- (or task or protected type), we look for a type that
6460 -- matches the current scope.
6462 elsif Entity (N) = Current_Scope then
6463 return;
6464 end if;
6465 end if;
6467 -- If we have an enumeration literal that appears as the choice in
6468 -- the aggregate of an enumeration representation clause, then
6469 -- freezing does not occur (RM 13.14(10)).
6471 when N_Enumeration_Representation_Clause =>
6473 -- The case we are looking for is an enumeration literal
6475 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
6476 and then Is_Enumeration_Type (Etype (N))
6477 then
6478 -- If enumeration literal appears directly as the choice,
6479 -- do not freeze (this is the normal non-overloaded case)
6481 if Nkind (Parent (N)) = N_Component_Association
6482 and then First (Choices (Parent (N))) = N
6483 then
6484 return;
6486 -- If enumeration literal appears as the name of function
6487 -- which is the choice, then also do not freeze. This
6488 -- happens in the overloaded literal case, where the
6489 -- enumeration literal is temporarily changed to a function
6490 -- call for overloading analysis purposes.
6492 elsif Nkind (Parent (N)) = N_Function_Call
6493 and then
6494 Nkind (Parent (Parent (N))) = N_Component_Association
6495 and then
6496 First (Choices (Parent (Parent (N)))) = Parent (N)
6497 then
6498 return;
6499 end if;
6500 end if;
6502 -- Normally if the parent is a handled sequence of statements,
6503 -- then the current node must be a statement, and that is an
6504 -- appropriate place to insert a freeze node.
6506 when N_Handled_Sequence_Of_Statements =>
6508 -- An exception occurs when the sequence of statements is for
6509 -- an expander generated body that did not do the usual freeze
6510 -- all operation. In this case we usually want to freeze
6511 -- outside this body, not inside it, and we skip past the
6512 -- subprogram body that we are inside.
6514 if In_Expanded_Body (Parent_P) then
6515 declare
6516 Subp : constant Node_Id := Parent (Parent_P);
6517 Spec : Entity_Id;
6519 begin
6520 -- Freeze the entity only when it is declared inside the
6521 -- body of the expander generated procedure. This case
6522 -- is recognized by the scope of the entity or its type,
6523 -- which is either the spec for some enclosing body, or
6524 -- (in the case of init_procs, for which there are no
6525 -- separate specs) the current scope.
6527 if Nkind (Subp) = N_Subprogram_Body then
6528 Spec := Corresponding_Spec (Subp);
6530 if (Present (Typ) and then Scope (Typ) = Spec)
6531 or else
6532 (Present (Nam) and then Scope (Nam) = Spec)
6533 then
6534 exit;
6536 elsif Present (Typ)
6537 and then Scope (Typ) = Current_Scope
6538 and then Defining_Entity (Subp) = Current_Scope
6539 then
6540 exit;
6541 end if;
6542 end if;
6544 -- An expression function may act as a completion of
6545 -- a function declaration. As such, it can reference
6546 -- entities declared between the two views:
6548 -- Hidden []; -- 1
6549 -- function F return ...;
6550 -- private
6551 -- function Hidden return ...;
6552 -- function F return ... is (Hidden); -- 2
6554 -- Refering to the example above, freezing the expression
6555 -- of F (2) would place Hidden's freeze node (1) in the
6556 -- wrong place. Avoid explicit freezing and let the usual
6557 -- scenarios do the job - for example, reaching the end
6558 -- of the private declarations, or a call to F.
6560 if Nkind (Original_Node (Subp)) =
6561 N_Expression_Function
6562 then
6563 null;
6565 -- Freeze outside the body
6567 else
6568 Parent_P := Parent (Parent_P);
6569 Freeze_Outside := True;
6570 end if;
6571 end;
6573 -- Here if normal case where we are in handled statement
6574 -- sequence and want to do the insertion right there.
6576 else
6577 exit;
6578 end if;
6580 -- If parent is a body or a spec or a block, then the current node
6581 -- is a statement or declaration and we can insert the freeze node
6582 -- before it.
6584 when N_Block_Statement |
6585 N_Entry_Body |
6586 N_Package_Body |
6587 N_Package_Specification |
6588 N_Protected_Body |
6589 N_Subprogram_Body |
6590 N_Task_Body => exit;
6592 -- The expander is allowed to define types in any statements list,
6593 -- so any of the following parent nodes also mark a freezing point
6594 -- if the actual node is in a list of statements or declarations.
6596 when N_Abortable_Part |
6597 N_Accept_Alternative |
6598 N_And_Then |
6599 N_Case_Statement_Alternative |
6600 N_Compilation_Unit_Aux |
6601 N_Conditional_Entry_Call |
6602 N_Delay_Alternative |
6603 N_Elsif_Part |
6604 N_Entry_Call_Alternative |
6605 N_Exception_Handler |
6606 N_Extended_Return_Statement |
6607 N_Freeze_Entity |
6608 N_If_Statement |
6609 N_Or_Else |
6610 N_Selective_Accept |
6611 N_Triggering_Alternative =>
6613 exit when Is_List_Member (P);
6615 -- Freeze nodes produced by an expression coming from the Actions
6616 -- list of a N_Expression_With_Actions node must remain within the
6617 -- Actions list. Inserting the freeze nodes further up the tree
6618 -- may lead to use before declaration issues in the case of array
6619 -- types.
6621 when N_Expression_With_Actions =>
6622 if Is_List_Member (P)
6623 and then List_Containing (P) = Actions (Parent_P)
6624 then
6625 exit;
6626 end if;
6628 -- Note: N_Loop_Statement is a special case. A type that appears
6629 -- in the source can never be frozen in a loop (this occurs only
6630 -- because of a loop expanded by the expander), so we keep on
6631 -- going. Otherwise we terminate the search. Same is true of any
6632 -- entity which comes from source. (if they have predefined type,
6633 -- that type does not appear to come from source, but the entity
6634 -- should not be frozen here).
6636 when N_Loop_Statement =>
6637 exit when not Comes_From_Source (Etype (N))
6638 and then (No (Nam) or else not Comes_From_Source (Nam));
6640 -- For all other cases, keep looking at parents
6642 when others =>
6643 null;
6644 end case;
6646 -- We fall through the case if we did not yet find the proper
6647 -- place in the free for inserting the freeze node, so climb.
6649 P := Parent_P;
6650 end loop;
6652 -- If the expression appears in a record or an initialization procedure,
6653 -- the freeze nodes are collected and attached to the current scope, to
6654 -- be inserted and analyzed on exit from the scope, to insure that
6655 -- generated entities appear in the correct scope. If the expression is
6656 -- a default for a discriminant specification, the scope is still void.
6657 -- The expression can also appear in the discriminant part of a private
6658 -- or concurrent type.
6660 -- If the expression appears in a constrained subcomponent of an
6661 -- enclosing record declaration, the freeze nodes must be attached to
6662 -- the outer record type so they can eventually be placed in the
6663 -- enclosing declaration list.
6665 -- The other case requiring this special handling is if we are in a
6666 -- default expression, since in that case we are about to freeze a
6667 -- static type, and the freeze scope needs to be the outer scope, not
6668 -- the scope of the subprogram with the default parameter.
6670 -- For default expressions and other spec expressions in generic units,
6671 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
6672 -- placing them at the proper place, after the generic unit.
6674 if (In_Spec_Exp and not Inside_A_Generic)
6675 or else Freeze_Outside
6676 or else (Is_Type (Current_Scope)
6677 and then (not Is_Concurrent_Type (Current_Scope)
6678 or else not Has_Completion (Current_Scope)))
6679 or else Ekind (Current_Scope) = E_Void
6680 then
6681 declare
6682 N : constant Node_Id := Current_Scope;
6683 Freeze_Nodes : List_Id := No_List;
6684 Pos : Int := Scope_Stack.Last;
6686 begin
6687 if Present (Desig_Typ) then
6688 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
6689 end if;
6691 if Present (Typ) then
6692 Freeze_And_Append (Typ, N, Freeze_Nodes);
6693 end if;
6695 if Present (Nam) then
6696 Freeze_And_Append (Nam, N, Freeze_Nodes);
6697 end if;
6699 -- The current scope may be that of a constrained component of
6700 -- an enclosing record declaration, or of a loop of an enclosing
6701 -- quantified expression, which is above the current scope in the
6702 -- scope stack. Indeed in the context of a quantified expression,
6703 -- a scope is created and pushed above the current scope in order
6704 -- to emulate the loop-like behavior of the quantified expression.
6705 -- If the expression is within a top-level pragma, as for a pre-
6706 -- condition on a library-level subprogram, nothing to do.
6708 if not Is_Compilation_Unit (Current_Scope)
6709 and then (Is_Record_Type (Scope (Current_Scope))
6710 or else Nkind (Parent (Current_Scope)) =
6711 N_Quantified_Expression)
6712 then
6713 Pos := Pos - 1;
6714 end if;
6716 if Is_Non_Empty_List (Freeze_Nodes) then
6717 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
6718 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
6719 Freeze_Nodes;
6720 else
6721 Append_List (Freeze_Nodes,
6722 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
6723 end if;
6724 end if;
6725 end;
6727 return;
6728 end if;
6730 -- Now we have the right place to do the freezing. First, a special
6731 -- adjustment, if we are in spec-expression analysis mode, these freeze
6732 -- actions must not be thrown away (normally all inserted actions are
6733 -- thrown away in this mode. However, the freeze actions are from static
6734 -- expressions and one of the important reasons we are doing this
6735 -- special analysis is to get these freeze actions. Therefore we turn
6736 -- off the In_Spec_Expression mode to propagate these freeze actions.
6737 -- This also means they get properly analyzed and expanded.
6739 In_Spec_Expression := False;
6741 -- Freeze the designated type of an allocator (RM 13.14(13))
6743 if Present (Desig_Typ) then
6744 Freeze_Before (P, Desig_Typ);
6745 end if;
6747 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
6748 -- the enumeration representation clause exception in the loop above.
6750 if Present (Typ) then
6751 Freeze_Before (P, Typ);
6752 end if;
6754 -- Freeze name if one is present (RM 13.14(11))
6756 if Present (Nam) then
6757 Freeze_Before (P, Nam);
6758 end if;
6760 -- Restore In_Spec_Expression flag
6762 In_Spec_Expression := In_Spec_Exp;
6763 end Freeze_Expression;
6765 -----------------------------
6766 -- Freeze_Fixed_Point_Type --
6767 -----------------------------
6769 -- Certain fixed-point types and subtypes, including implicit base types
6770 -- and declared first subtypes, have not yet set up a range. This is
6771 -- because the range cannot be set until the Small and Size values are
6772 -- known, and these are not known till the type is frozen.
6774 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
6775 -- whose bounds are unanalyzed real literals. This routine will recognize
6776 -- this case, and transform this range node into a properly typed range
6777 -- with properly analyzed and resolved values.
6779 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
6780 Rng : constant Node_Id := Scalar_Range (Typ);
6781 Lo : constant Node_Id := Low_Bound (Rng);
6782 Hi : constant Node_Id := High_Bound (Rng);
6783 Btyp : constant Entity_Id := Base_Type (Typ);
6784 Brng : constant Node_Id := Scalar_Range (Btyp);
6785 BLo : constant Node_Id := Low_Bound (Brng);
6786 BHi : constant Node_Id := High_Bound (Brng);
6787 Small : constant Ureal := Small_Value (Typ);
6788 Loval : Ureal;
6789 Hival : Ureal;
6790 Atype : Entity_Id;
6792 Orig_Lo : Ureal;
6793 Orig_Hi : Ureal;
6794 -- Save original bounds (for shaving tests)
6796 Actual_Size : Nat;
6797 -- Actual size chosen
6799 function Fsize (Lov, Hiv : Ureal) return Nat;
6800 -- Returns size of type with given bounds. Also leaves these
6801 -- bounds set as the current bounds of the Typ.
6803 -----------
6804 -- Fsize --
6805 -----------
6807 function Fsize (Lov, Hiv : Ureal) return Nat is
6808 begin
6809 Set_Realval (Lo, Lov);
6810 Set_Realval (Hi, Hiv);
6811 return Minimum_Size (Typ);
6812 end Fsize;
6814 -- Start of processing for Freeze_Fixed_Point_Type
6816 begin
6817 -- If Esize of a subtype has not previously been set, set it now
6819 if Unknown_Esize (Typ) then
6820 Atype := Ancestor_Subtype (Typ);
6822 if Present (Atype) then
6823 Set_Esize (Typ, Esize (Atype));
6824 else
6825 Set_Esize (Typ, Esize (Base_Type (Typ)));
6826 end if;
6827 end if;
6829 -- Immediate return if the range is already analyzed. This means that
6830 -- the range is already set, and does not need to be computed by this
6831 -- routine.
6833 if Analyzed (Rng) then
6834 return;
6835 end if;
6837 -- Immediate return if either of the bounds raises Constraint_Error
6839 if Raises_Constraint_Error (Lo)
6840 or else Raises_Constraint_Error (Hi)
6841 then
6842 return;
6843 end if;
6845 Loval := Realval (Lo);
6846 Hival := Realval (Hi);
6848 Orig_Lo := Loval;
6849 Orig_Hi := Hival;
6851 -- Ordinary fixed-point case
6853 if Is_Ordinary_Fixed_Point_Type (Typ) then
6855 -- For the ordinary fixed-point case, we are allowed to fudge the
6856 -- end-points up or down by small. Generally we prefer to fudge up,
6857 -- i.e. widen the bounds for non-model numbers so that the end points
6858 -- are included. However there are cases in which this cannot be
6859 -- done, and indeed cases in which we may need to narrow the bounds.
6860 -- The following circuit makes the decision.
6862 -- Note: our terminology here is that Incl_EP means that the bounds
6863 -- are widened by Small if necessary to include the end points, and
6864 -- Excl_EP means that the bounds are narrowed by Small to exclude the
6865 -- end-points if this reduces the size.
6867 -- Note that in the Incl case, all we care about is including the
6868 -- end-points. In the Excl case, we want to narrow the bounds as
6869 -- much as permitted by the RM, to give the smallest possible size.
6871 Fudge : declare
6872 Loval_Incl_EP : Ureal;
6873 Hival_Incl_EP : Ureal;
6875 Loval_Excl_EP : Ureal;
6876 Hival_Excl_EP : Ureal;
6878 Size_Incl_EP : Nat;
6879 Size_Excl_EP : Nat;
6881 Model_Num : Ureal;
6882 First_Subt : Entity_Id;
6883 Actual_Lo : Ureal;
6884 Actual_Hi : Ureal;
6886 begin
6887 -- First step. Base types are required to be symmetrical. Right
6888 -- now, the base type range is a copy of the first subtype range.
6889 -- This will be corrected before we are done, but right away we
6890 -- need to deal with the case where both bounds are non-negative.
6891 -- In this case, we set the low bound to the negative of the high
6892 -- bound, to make sure that the size is computed to include the
6893 -- required sign. Note that we do not need to worry about the
6894 -- case of both bounds negative, because the sign will be dealt
6895 -- with anyway. Furthermore we can't just go making such a bound
6896 -- symmetrical, since in a twos-complement system, there is an
6897 -- extra negative value which could not be accommodated on the
6898 -- positive side.
6900 if Typ = Btyp
6901 and then not UR_Is_Negative (Loval)
6902 and then Hival > Loval
6903 then
6904 Loval := -Hival;
6905 Set_Realval (Lo, Loval);
6906 end if;
6908 -- Compute the fudged bounds. If the number is a model number,
6909 -- then we do nothing to include it, but we are allowed to backoff
6910 -- to the next adjacent model number when we exclude it. If it is
6911 -- not a model number then we straddle the two values with the
6912 -- model numbers on either side.
6914 Model_Num := UR_Trunc (Loval / Small) * Small;
6916 if Loval = Model_Num then
6917 Loval_Incl_EP := Model_Num;
6918 else
6919 Loval_Incl_EP := Model_Num - Small;
6920 end if;
6922 -- The low value excluding the end point is Small greater, but
6923 -- we do not do this exclusion if the low value is positive,
6924 -- since it can't help the size and could actually hurt by
6925 -- crossing the high bound.
6927 if UR_Is_Negative (Loval_Incl_EP) then
6928 Loval_Excl_EP := Loval_Incl_EP + Small;
6930 -- If the value went from negative to zero, then we have the
6931 -- case where Loval_Incl_EP is the model number just below
6932 -- zero, so we want to stick to the negative value for the
6933 -- base type to maintain the condition that the size will
6934 -- include signed values.
6936 if Typ = Btyp
6937 and then UR_Is_Zero (Loval_Excl_EP)
6938 then
6939 Loval_Excl_EP := Loval_Incl_EP;
6940 end if;
6942 else
6943 Loval_Excl_EP := Loval_Incl_EP;
6944 end if;
6946 -- Similar processing for upper bound and high value
6948 Model_Num := UR_Trunc (Hival / Small) * Small;
6950 if Hival = Model_Num then
6951 Hival_Incl_EP := Model_Num;
6952 else
6953 Hival_Incl_EP := Model_Num + Small;
6954 end if;
6956 if UR_Is_Positive (Hival_Incl_EP) then
6957 Hival_Excl_EP := Hival_Incl_EP - Small;
6958 else
6959 Hival_Excl_EP := Hival_Incl_EP;
6960 end if;
6962 -- One further adjustment is needed. In the case of subtypes, we
6963 -- cannot go outside the range of the base type, or we get
6964 -- peculiarities, and the base type range is already set. This
6965 -- only applies to the Incl values, since clearly the Excl values
6966 -- are already as restricted as they are allowed to be.
6968 if Typ /= Btyp then
6969 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
6970 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
6971 end if;
6973 -- Get size including and excluding end points
6975 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
6976 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
6978 -- No need to exclude end-points if it does not reduce size
6980 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
6981 Loval_Excl_EP := Loval_Incl_EP;
6982 end if;
6984 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
6985 Hival_Excl_EP := Hival_Incl_EP;
6986 end if;
6988 -- Now we set the actual size to be used. We want to use the
6989 -- bounds fudged up to include the end-points but only if this
6990 -- can be done without violating a specifically given size
6991 -- size clause or causing an unacceptable increase in size.
6993 -- Case of size clause given
6995 if Has_Size_Clause (Typ) then
6997 -- Use the inclusive size only if it is consistent with
6998 -- the explicitly specified size.
7000 if Size_Incl_EP <= RM_Size (Typ) then
7001 Actual_Lo := Loval_Incl_EP;
7002 Actual_Hi := Hival_Incl_EP;
7003 Actual_Size := Size_Incl_EP;
7005 -- If the inclusive size is too large, we try excluding
7006 -- the end-points (will be caught later if does not work).
7008 else
7009 Actual_Lo := Loval_Excl_EP;
7010 Actual_Hi := Hival_Excl_EP;
7011 Actual_Size := Size_Excl_EP;
7012 end if;
7014 -- Case of size clause not given
7016 else
7017 -- If we have a base type whose corresponding first subtype
7018 -- has an explicit size that is large enough to include our
7019 -- end-points, then do so. There is no point in working hard
7020 -- to get a base type whose size is smaller than the specified
7021 -- size of the first subtype.
7023 First_Subt := First_Subtype (Typ);
7025 if Has_Size_Clause (First_Subt)
7026 and then Size_Incl_EP <= Esize (First_Subt)
7027 then
7028 Actual_Size := Size_Incl_EP;
7029 Actual_Lo := Loval_Incl_EP;
7030 Actual_Hi := Hival_Incl_EP;
7032 -- If excluding the end-points makes the size smaller and
7033 -- results in a size of 8,16,32,64, then we take the smaller
7034 -- size. For the 64 case, this is compulsory. For the other
7035 -- cases, it seems reasonable. We like to include end points
7036 -- if we can, but not at the expense of moving to the next
7037 -- natural boundary of size.
7039 elsif Size_Incl_EP /= Size_Excl_EP
7040 and then Addressable (Size_Excl_EP)
7041 then
7042 Actual_Size := Size_Excl_EP;
7043 Actual_Lo := Loval_Excl_EP;
7044 Actual_Hi := Hival_Excl_EP;
7046 -- Otherwise we can definitely include the end points
7048 else
7049 Actual_Size := Size_Incl_EP;
7050 Actual_Lo := Loval_Incl_EP;
7051 Actual_Hi := Hival_Incl_EP;
7052 end if;
7054 -- One pathological case: normally we never fudge a low bound
7055 -- down, since it would seem to increase the size (if it has
7056 -- any effect), but for ranges containing single value, or no
7057 -- values, the high bound can be small too large. Consider:
7059 -- type t is delta 2.0**(-14)
7060 -- range 131072.0 .. 0;
7062 -- That lower bound is *just* outside the range of 32 bits, and
7063 -- does need fudging down in this case. Note that the bounds
7064 -- will always have crossed here, since the high bound will be
7065 -- fudged down if necessary, as in the case of:
7067 -- type t is delta 2.0**(-14)
7068 -- range 131072.0 .. 131072.0;
7070 -- So we detect the situation by looking for crossed bounds,
7071 -- and if the bounds are crossed, and the low bound is greater
7072 -- than zero, we will always back it off by small, since this
7073 -- is completely harmless.
7075 if Actual_Lo > Actual_Hi then
7076 if UR_Is_Positive (Actual_Lo) then
7077 Actual_Lo := Loval_Incl_EP - Small;
7078 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7080 -- And of course, we need to do exactly the same parallel
7081 -- fudge for flat ranges in the negative region.
7083 elsif UR_Is_Negative (Actual_Hi) then
7084 Actual_Hi := Hival_Incl_EP + Small;
7085 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7086 end if;
7087 end if;
7088 end if;
7090 Set_Realval (Lo, Actual_Lo);
7091 Set_Realval (Hi, Actual_Hi);
7092 end Fudge;
7094 -- For the decimal case, none of this fudging is required, since there
7095 -- are no end-point problems in the decimal case (the end-points are
7096 -- always included).
7098 else
7099 Actual_Size := Fsize (Loval, Hival);
7100 end if;
7102 -- At this stage, the actual size has been calculated and the proper
7103 -- required bounds are stored in the low and high bounds.
7105 if Actual_Size > 64 then
7106 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7107 Error_Msg_N
7108 ("size required (^) for type& too large, maximum allowed is 64",
7109 Typ);
7110 Actual_Size := 64;
7111 end if;
7113 -- Check size against explicit given size
7115 if Has_Size_Clause (Typ) then
7116 if Actual_Size > RM_Size (Typ) then
7117 Error_Msg_Uint_1 := RM_Size (Typ);
7118 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7119 Error_Msg_NE
7120 ("size given (^) for type& too small, minimum allowed is ^",
7121 Size_Clause (Typ), Typ);
7123 else
7124 Actual_Size := UI_To_Int (Esize (Typ));
7125 end if;
7127 -- Increase size to next natural boundary if no size clause given
7129 else
7130 if Actual_Size <= 8 then
7131 Actual_Size := 8;
7132 elsif Actual_Size <= 16 then
7133 Actual_Size := 16;
7134 elsif Actual_Size <= 32 then
7135 Actual_Size := 32;
7136 else
7137 Actual_Size := 64;
7138 end if;
7140 Init_Esize (Typ, Actual_Size);
7141 Adjust_Esize_For_Alignment (Typ);
7142 end if;
7144 -- If we have a base type, then expand the bounds so that they extend to
7145 -- the full width of the allocated size in bits, to avoid junk range
7146 -- checks on intermediate computations.
7148 if Base_Type (Typ) = Typ then
7149 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7150 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7151 end if;
7153 -- Final step is to reanalyze the bounds using the proper type
7154 -- and set the Corresponding_Integer_Value fields of the literals.
7156 Set_Etype (Lo, Empty);
7157 Set_Analyzed (Lo, False);
7158 Analyze (Lo);
7160 -- Resolve with universal fixed if the base type, and the base type if
7161 -- it is a subtype. Note we can't resolve the base type with itself,
7162 -- that would be a reference before definition.
7164 if Typ = Btyp then
7165 Resolve (Lo, Universal_Fixed);
7166 else
7167 Resolve (Lo, Btyp);
7168 end if;
7170 -- Set corresponding integer value for bound
7172 Set_Corresponding_Integer_Value
7173 (Lo, UR_To_Uint (Realval (Lo) / Small));
7175 -- Similar processing for high bound
7177 Set_Etype (Hi, Empty);
7178 Set_Analyzed (Hi, False);
7179 Analyze (Hi);
7181 if Typ = Btyp then
7182 Resolve (Hi, Universal_Fixed);
7183 else
7184 Resolve (Hi, Btyp);
7185 end if;
7187 Set_Corresponding_Integer_Value
7188 (Hi, UR_To_Uint (Realval (Hi) / Small));
7190 -- Set type of range to correspond to bounds
7192 Set_Etype (Rng, Etype (Lo));
7194 -- Set Esize to calculated size if not set already
7196 if Unknown_Esize (Typ) then
7197 Init_Esize (Typ, Actual_Size);
7198 end if;
7200 -- Set RM_Size if not already set. If already set, check value
7202 declare
7203 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7205 begin
7206 if RM_Size (Typ) /= Uint_0 then
7207 if RM_Size (Typ) < Minsiz then
7208 Error_Msg_Uint_1 := RM_Size (Typ);
7209 Error_Msg_Uint_2 := Minsiz;
7210 Error_Msg_NE
7211 ("size given (^) for type& too small, minimum allowed is ^",
7212 Size_Clause (Typ), Typ);
7213 end if;
7215 else
7216 Set_RM_Size (Typ, Minsiz);
7217 end if;
7218 end;
7220 -- Check for shaving
7222 if Comes_From_Source (Typ) then
7223 if Orig_Lo < Expr_Value_R (Lo) then
7224 Error_Msg_N
7225 ("declared low bound of type & is outside type range??", Typ);
7226 Error_Msg_N
7227 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
7228 end if;
7230 if Orig_Hi > Expr_Value_R (Hi) then
7231 Error_Msg_N
7232 ("declared high bound of type & is outside type range??", Typ);
7233 Error_Msg_N
7234 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
7235 end if;
7236 end if;
7237 end Freeze_Fixed_Point_Type;
7239 ------------------
7240 -- Freeze_Itype --
7241 ------------------
7243 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7244 L : List_Id;
7246 begin
7247 Set_Has_Delayed_Freeze (T);
7248 L := Freeze_Entity (T, N);
7250 if Is_Non_Empty_List (L) then
7251 Insert_Actions (N, L);
7252 end if;
7253 end Freeze_Itype;
7255 --------------------------
7256 -- Freeze_Static_Object --
7257 --------------------------
7259 procedure Freeze_Static_Object (E : Entity_Id) is
7261 Cannot_Be_Static : exception;
7262 -- Exception raised if the type of a static object cannot be made
7263 -- static. This happens if the type depends on non-global objects.
7265 procedure Ensure_Expression_Is_SA (N : Node_Id);
7266 -- Called to ensure that an expression used as part of a type definition
7267 -- is statically allocatable, which means that the expression type is
7268 -- statically allocatable, and the expression is either static, or a
7269 -- reference to a library level constant.
7271 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7272 -- Called to mark a type as static, checking that it is possible
7273 -- to set the type as static. If it is not possible, then the
7274 -- exception Cannot_Be_Static is raised.
7276 -----------------------------
7277 -- Ensure_Expression_Is_SA --
7278 -----------------------------
7280 procedure Ensure_Expression_Is_SA (N : Node_Id) is
7281 Ent : Entity_Id;
7283 begin
7284 Ensure_Type_Is_SA (Etype (N));
7286 if Is_OK_Static_Expression (N) then
7287 return;
7289 elsif Nkind (N) = N_Identifier then
7290 Ent := Entity (N);
7292 if Present (Ent)
7293 and then Ekind (Ent) = E_Constant
7294 and then Is_Library_Level_Entity (Ent)
7295 then
7296 return;
7297 end if;
7298 end if;
7300 raise Cannot_Be_Static;
7301 end Ensure_Expression_Is_SA;
7303 -----------------------
7304 -- Ensure_Type_Is_SA --
7305 -----------------------
7307 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7308 N : Node_Id;
7309 C : Entity_Id;
7311 begin
7312 -- If type is library level, we are all set
7314 if Is_Library_Level_Entity (Typ) then
7315 return;
7316 end if;
7318 -- We are also OK if the type already marked as statically allocated,
7319 -- which means we processed it before.
7321 if Is_Statically_Allocated (Typ) then
7322 return;
7323 end if;
7325 -- Mark type as statically allocated
7327 Set_Is_Statically_Allocated (Typ);
7329 -- Check that it is safe to statically allocate this type
7331 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
7332 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
7333 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
7335 elsif Is_Array_Type (Typ) then
7336 N := First_Index (Typ);
7337 while Present (N) loop
7338 Ensure_Type_Is_SA (Etype (N));
7339 Next_Index (N);
7340 end loop;
7342 Ensure_Type_Is_SA (Component_Type (Typ));
7344 elsif Is_Access_Type (Typ) then
7345 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
7347 declare
7348 F : Entity_Id;
7349 T : constant Entity_Id := Etype (Designated_Type (Typ));
7351 begin
7352 if T /= Standard_Void_Type then
7353 Ensure_Type_Is_SA (T);
7354 end if;
7356 F := First_Formal (Designated_Type (Typ));
7357 while Present (F) loop
7358 Ensure_Type_Is_SA (Etype (F));
7359 Next_Formal (F);
7360 end loop;
7361 end;
7363 else
7364 Ensure_Type_Is_SA (Designated_Type (Typ));
7365 end if;
7367 elsif Is_Record_Type (Typ) then
7368 C := First_Entity (Typ);
7369 while Present (C) loop
7370 if Ekind (C) = E_Discriminant
7371 or else Ekind (C) = E_Component
7372 then
7373 Ensure_Type_Is_SA (Etype (C));
7375 elsif Is_Type (C) then
7376 Ensure_Type_Is_SA (C);
7377 end if;
7379 Next_Entity (C);
7380 end loop;
7382 elsif Ekind (Typ) = E_Subprogram_Type then
7383 Ensure_Type_Is_SA (Etype (Typ));
7385 C := First_Formal (Typ);
7386 while Present (C) loop
7387 Ensure_Type_Is_SA (Etype (C));
7388 Next_Formal (C);
7389 end loop;
7391 else
7392 raise Cannot_Be_Static;
7393 end if;
7394 end Ensure_Type_Is_SA;
7396 -- Start of processing for Freeze_Static_Object
7398 begin
7399 Ensure_Type_Is_SA (Etype (E));
7401 exception
7402 when Cannot_Be_Static =>
7404 -- If the object that cannot be static is imported or exported, then
7405 -- issue an error message saying that this object cannot be imported
7406 -- or exported. If it has an address clause it is an overlay in the
7407 -- current partition and the static requirement is not relevant.
7408 -- Do not issue any error message when ignoring rep clauses.
7410 if Ignore_Rep_Clauses then
7411 null;
7413 elsif Is_Imported (E) then
7414 if No (Address_Clause (E)) then
7415 Error_Msg_N
7416 ("& cannot be imported (local type is not constant)", E);
7417 end if;
7419 -- Otherwise must be exported, something is wrong if compiler
7420 -- is marking something as statically allocated which cannot be).
7422 else pragma Assert (Is_Exported (E));
7423 Error_Msg_N
7424 ("& cannot be exported (local type is not constant)", E);
7425 end if;
7426 end Freeze_Static_Object;
7428 -----------------------
7429 -- Freeze_Subprogram --
7430 -----------------------
7432 procedure Freeze_Subprogram (E : Entity_Id) is
7433 Retype : Entity_Id;
7434 F : Entity_Id;
7436 begin
7437 -- Subprogram may not have an address clause unless it is imported
7439 if Present (Address_Clause (E)) then
7440 if not Is_Imported (E) then
7441 Error_Msg_N
7442 ("address clause can only be given " &
7443 "for imported subprogram",
7444 Name (Address_Clause (E)));
7445 end if;
7446 end if;
7448 -- Reset the Pure indication on an imported subprogram unless an
7449 -- explicit Pure_Function pragma was present or the subprogram is an
7450 -- intrinsic. We do this because otherwise it is an insidious error
7451 -- to call a non-pure function from pure unit and have calls
7452 -- mysteriously optimized away. What happens here is that the Import
7453 -- can bypass the normal check to ensure that pure units call only pure
7454 -- subprograms.
7456 -- The reason for the intrinsic exception is that in general, intrinsic
7457 -- functions (such as shifts) are pure anyway. The only exceptions are
7458 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
7459 -- in any case, so no problem arises.
7461 if Is_Imported (E)
7462 and then Is_Pure (E)
7463 and then not Has_Pragma_Pure_Function (E)
7464 and then not Is_Intrinsic_Subprogram (E)
7465 then
7466 Set_Is_Pure (E, False);
7467 end if;
7469 -- For non-foreign convention subprograms, this is where we create
7470 -- the extra formals (for accessibility level and constrained bit
7471 -- information). We delay this till the freeze point precisely so
7472 -- that we know the convention.
7474 if not Has_Foreign_Convention (E) then
7475 Create_Extra_Formals (E);
7476 Set_Mechanisms (E);
7478 -- If this is convention Ada and a Valued_Procedure, that's odd
7480 if Ekind (E) = E_Procedure
7481 and then Is_Valued_Procedure (E)
7482 and then Convention (E) = Convention_Ada
7483 and then Warn_On_Export_Import
7484 then
7485 Error_Msg_N
7486 ("??Valued_Procedure has no effect for convention Ada", E);
7487 Set_Is_Valued_Procedure (E, False);
7488 end if;
7490 -- Case of foreign convention
7492 else
7493 Set_Mechanisms (E);
7495 -- For foreign conventions, warn about return of unconstrained array
7497 if Ekind (E) = E_Function then
7498 Retype := Underlying_Type (Etype (E));
7500 -- If no return type, probably some other error, e.g. a
7501 -- missing full declaration, so ignore.
7503 if No (Retype) then
7504 null;
7506 -- If the return type is generic, we have emitted a warning
7507 -- earlier on, and there is nothing else to check here. Specific
7508 -- instantiations may lead to erroneous behavior.
7510 elsif Is_Generic_Type (Etype (E)) then
7511 null;
7513 -- Display warning if returning unconstrained array
7515 elsif Is_Array_Type (Retype)
7516 and then not Is_Constrained (Retype)
7518 -- Check appropriate warning is enabled (should we check for
7519 -- Warnings (Off) on specific entities here, probably so???)
7521 and then Warn_On_Export_Import
7523 -- Exclude the VM case, since return of unconstrained arrays
7524 -- is properly handled in both the JVM and .NET cases.
7526 and then VM_Target = No_VM
7527 then
7528 Error_Msg_N
7529 ("?x?foreign convention function& should not return " &
7530 "unconstrained array", E);
7531 return;
7532 end if;
7533 end if;
7535 -- If any of the formals for an exported foreign convention
7536 -- subprogram have defaults, then emit an appropriate warning since
7537 -- this is odd (default cannot be used from non-Ada code)
7539 if Is_Exported (E) then
7540 F := First_Formal (E);
7541 while Present (F) loop
7542 if Warn_On_Export_Import
7543 and then Present (Default_Value (F))
7544 then
7545 Error_Msg_N
7546 ("?x?parameter cannot be defaulted in non-Ada call",
7547 Default_Value (F));
7548 end if;
7550 Next_Formal (F);
7551 end loop;
7552 end if;
7553 end if;
7555 -- Pragma Inline_Always is disallowed for dispatching subprograms
7556 -- because the address of such subprograms is saved in the dispatch
7557 -- table to support dispatching calls, and dispatching calls cannot
7558 -- be inlined. This is consistent with the restriction against using
7559 -- 'Access or 'Address on an Inline_Always subprogram.
7561 if Is_Dispatching_Operation (E)
7562 and then Has_Pragma_Inline_Always (E)
7563 then
7564 Error_Msg_N
7565 ("pragma Inline_Always not allowed for dispatching subprograms", E);
7566 end if;
7568 -- Because of the implicit representation of inherited predefined
7569 -- operators in the front-end, the overriding status of the operation
7570 -- may be affected when a full view of a type is analyzed, and this is
7571 -- not captured by the analysis of the corresponding type declaration.
7572 -- Therefore the correctness of a not-overriding indicator must be
7573 -- rechecked when the subprogram is frozen.
7575 if Nkind (E) = N_Defining_Operator_Symbol
7576 and then not Error_Posted (Parent (E))
7577 then
7578 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
7579 end if;
7580 end Freeze_Subprogram;
7582 ----------------------
7583 -- Is_Fully_Defined --
7584 ----------------------
7586 function Is_Fully_Defined (T : Entity_Id) return Boolean is
7587 begin
7588 if Ekind (T) = E_Class_Wide_Type then
7589 return Is_Fully_Defined (Etype (T));
7591 elsif Is_Array_Type (T) then
7592 return Is_Fully_Defined (Component_Type (T));
7594 elsif Is_Record_Type (T)
7595 and not Is_Private_Type (T)
7596 then
7597 -- Verify that the record type has no components with private types
7598 -- without completion.
7600 declare
7601 Comp : Entity_Id;
7603 begin
7604 Comp := First_Component (T);
7605 while Present (Comp) loop
7606 if not Is_Fully_Defined (Etype (Comp)) then
7607 return False;
7608 end if;
7610 Next_Component (Comp);
7611 end loop;
7612 return True;
7613 end;
7615 -- For the designated type of an access to subprogram, all types in
7616 -- the profile must be fully defined.
7618 elsif Ekind (T) = E_Subprogram_Type then
7619 declare
7620 F : Entity_Id;
7622 begin
7623 F := First_Formal (T);
7624 while Present (F) loop
7625 if not Is_Fully_Defined (Etype (F)) then
7626 return False;
7627 end if;
7629 Next_Formal (F);
7630 end loop;
7632 return Is_Fully_Defined (Etype (T));
7633 end;
7635 else
7636 return not Is_Private_Type (T)
7637 or else Present (Full_View (Base_Type (T)));
7638 end if;
7639 end Is_Fully_Defined;
7641 ---------------------------------
7642 -- Process_Default_Expressions --
7643 ---------------------------------
7645 procedure Process_Default_Expressions
7646 (E : Entity_Id;
7647 After : in out Node_Id)
7649 Loc : constant Source_Ptr := Sloc (E);
7650 Dbody : Node_Id;
7651 Formal : Node_Id;
7652 Dcopy : Node_Id;
7653 Dnam : Entity_Id;
7655 begin
7656 Set_Default_Expressions_Processed (E);
7658 -- A subprogram instance and its associated anonymous subprogram share
7659 -- their signature. The default expression functions are defined in the
7660 -- wrapper packages for the anonymous subprogram, and should not be
7661 -- generated again for the instance.
7663 if Is_Generic_Instance (E)
7664 and then Present (Alias (E))
7665 and then Default_Expressions_Processed (Alias (E))
7666 then
7667 return;
7668 end if;
7670 Formal := First_Formal (E);
7671 while Present (Formal) loop
7672 if Present (Default_Value (Formal)) then
7674 -- We work with a copy of the default expression because we
7675 -- do not want to disturb the original, since this would mess
7676 -- up the conformance checking.
7678 Dcopy := New_Copy_Tree (Default_Value (Formal));
7680 -- The analysis of the expression may generate insert actions,
7681 -- which of course must not be executed. We wrap those actions
7682 -- in a procedure that is not called, and later on eliminated.
7683 -- The following cases have no side-effects, and are analyzed
7684 -- directly.
7686 if Nkind (Dcopy) = N_Identifier
7687 or else Nkind_In (Dcopy, N_Expanded_Name,
7688 N_Integer_Literal,
7689 N_Character_Literal,
7690 N_String_Literal,
7691 N_Real_Literal)
7692 or else (Nkind (Dcopy) = N_Attribute_Reference
7693 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
7694 or else Known_Null (Dcopy)
7695 then
7696 -- If there is no default function, we must still do a full
7697 -- analyze call on the default value, to ensure that all error
7698 -- checks are performed, e.g. those associated with static
7699 -- evaluation. Note: this branch will always be taken if the
7700 -- analyzer is turned off (but we still need the error checks).
7702 -- Note: the setting of parent here is to meet the requirement
7703 -- that we can only analyze the expression while attached to
7704 -- the tree. Really the requirement is that the parent chain
7705 -- be set, we don't actually need to be in the tree.
7707 Set_Parent (Dcopy, Declaration_Node (Formal));
7708 Analyze (Dcopy);
7710 -- Default expressions are resolved with their own type if the
7711 -- context is generic, to avoid anomalies with private types.
7713 if Ekind (Scope (E)) = E_Generic_Package then
7714 Resolve (Dcopy);
7715 else
7716 Resolve (Dcopy, Etype (Formal));
7717 end if;
7719 -- If that resolved expression will raise constraint error,
7720 -- then flag the default value as raising constraint error.
7721 -- This allows a proper error message on the calls.
7723 if Raises_Constraint_Error (Dcopy) then
7724 Set_Raises_Constraint_Error (Default_Value (Formal));
7725 end if;
7727 -- If the default is a parameterless call, we use the name of
7728 -- the called function directly, and there is no body to build.
7730 elsif Nkind (Dcopy) = N_Function_Call
7731 and then No (Parameter_Associations (Dcopy))
7732 then
7733 null;
7735 -- Else construct and analyze the body of a wrapper procedure
7736 -- that contains an object declaration to hold the expression.
7737 -- Given that this is done only to complete the analysis, it
7738 -- simpler to build a procedure than a function which might
7739 -- involve secondary stack expansion.
7741 else
7742 Dnam := Make_Temporary (Loc, 'D');
7744 Dbody :=
7745 Make_Subprogram_Body (Loc,
7746 Specification =>
7747 Make_Procedure_Specification (Loc,
7748 Defining_Unit_Name => Dnam),
7750 Declarations => New_List (
7751 Make_Object_Declaration (Loc,
7752 Defining_Identifier => Make_Temporary (Loc, 'T'),
7753 Object_Definition =>
7754 New_Occurrence_Of (Etype (Formal), Loc),
7755 Expression => New_Copy_Tree (Dcopy))),
7757 Handled_Statement_Sequence =>
7758 Make_Handled_Sequence_Of_Statements (Loc,
7759 Statements => Empty_List));
7761 Set_Scope (Dnam, Scope (E));
7762 Set_Assignment_OK (First (Declarations (Dbody)));
7763 Set_Is_Eliminated (Dnam);
7764 Insert_After (After, Dbody);
7765 Analyze (Dbody);
7766 After := Dbody;
7767 end if;
7768 end if;
7770 Next_Formal (Formal);
7771 end loop;
7772 end Process_Default_Expressions;
7774 ----------------------------------------
7775 -- Set_Component_Alignment_If_Not_Set --
7776 ----------------------------------------
7778 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
7779 begin
7780 -- Ignore if not base type, subtypes don't need anything
7782 if Typ /= Base_Type (Typ) then
7783 return;
7784 end if;
7786 -- Do not override existing representation
7788 if Is_Packed (Typ) then
7789 return;
7791 elsif Has_Specified_Layout (Typ) then
7792 return;
7794 elsif Component_Alignment (Typ) /= Calign_Default then
7795 return;
7797 else
7798 Set_Component_Alignment
7799 (Typ, Scope_Stack.Table
7800 (Scope_Stack.Last).Component_Alignment_Default);
7801 end if;
7802 end Set_Component_Alignment_If_Not_Set;
7804 --------------------------
7805 -- Set_SSO_From_Default --
7806 --------------------------
7808 procedure Set_SSO_From_Default (T : Entity_Id) is
7809 Reversed : Boolean;
7811 begin
7812 -- Set default SSO for an array or record base type, except in case of
7813 -- a type extension (which always inherits the SSO of its parent type).
7815 if Is_Base_Type (T)
7816 and then (Is_Array_Type (T)
7817 or else (Is_Record_Type (T)
7818 and then not (Is_Tagged_Type (T)
7819 and then Is_Derived_Type (T))))
7820 then
7821 Reversed :=
7822 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
7823 or else
7824 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
7826 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
7828 -- For a record type, if bit order is specified explicitly,
7829 -- then do not set SSO from default if not consistent. Note that
7830 -- we do not want to look at a Bit_Order attribute definition
7831 -- for a parent: if we were to inherit Bit_Order, then both
7832 -- SSO_Set_*_By_Default flags would have been cleared already
7833 -- (by Inherit_Aspects_At_Freeze_Point).
7835 and then not
7836 (Is_Record_Type (T)
7837 and then
7838 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
7839 and then Reverse_Bit_Order (T) /= Reversed)
7840 then
7841 -- If flags cause reverse storage order, then set the result. Note
7842 -- that we would have ignored the pragma setting the non default
7843 -- storage order in any case, hence the assertion at this point.
7845 pragma Assert
7846 (not Reversed or else Support_Nondefault_SSO_On_Target);
7848 Set_Reverse_Storage_Order (T, Reversed);
7850 -- For a record type, also set reversed bit order. Note: if a bit
7851 -- order has been specified explicitly, then this is a no-op.
7853 if Is_Record_Type (T) then
7854 Set_Reverse_Bit_Order (T, Reversed);
7855 end if;
7856 end if;
7857 end if;
7858 end Set_SSO_From_Default;
7860 ------------------
7861 -- Undelay_Type --
7862 ------------------
7864 procedure Undelay_Type (T : Entity_Id) is
7865 begin
7866 Set_Has_Delayed_Freeze (T, False);
7867 Set_Freeze_Node (T, Empty);
7869 -- Since we don't want T to have a Freeze_Node, we don't want its
7870 -- Full_View or Corresponding_Record_Type to have one either.
7872 -- ??? Fundamentally, this whole handling is unpleasant. What we really
7873 -- want is to be sure that for an Itype that's part of record R and is a
7874 -- subtype of type T, that it's frozen after the later of the freeze
7875 -- points of R and T. We have no way of doing that directly, so what we
7876 -- do is force most such Itypes to be frozen as part of freezing R via
7877 -- this procedure and only delay the ones that need to be delayed
7878 -- (mostly the designated types of access types that are defined as part
7879 -- of the record).
7881 if Is_Private_Type (T)
7882 and then Present (Full_View (T))
7883 and then Is_Itype (Full_View (T))
7884 and then Is_Record_Type (Scope (Full_View (T)))
7885 then
7886 Undelay_Type (Full_View (T));
7887 end if;
7889 if Is_Concurrent_Type (T)
7890 and then Present (Corresponding_Record_Type (T))
7891 and then Is_Itype (Corresponding_Record_Type (T))
7892 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
7893 then
7894 Undelay_Type (Corresponding_Record_Type (T));
7895 end if;
7896 end Undelay_Type;
7898 ------------------
7899 -- Warn_Overlay --
7900 ------------------
7902 procedure Warn_Overlay
7903 (Expr : Node_Id;
7904 Typ : Entity_Id;
7905 Nam : Entity_Id)
7907 Ent : constant Entity_Id := Entity (Nam);
7908 -- The object to which the address clause applies
7910 Init : Node_Id;
7911 Old : Entity_Id := Empty;
7912 Decl : Node_Id;
7914 begin
7915 -- No warning if address clause overlay warnings are off
7917 if not Address_Clause_Overlay_Warnings then
7918 return;
7919 end if;
7921 -- No warning if there is an explicit initialization
7923 Init := Original_Node (Expression (Declaration_Node (Ent)));
7925 if Present (Init) and then Comes_From_Source (Init) then
7926 return;
7927 end if;
7929 -- We only give the warning for non-imported entities of a type for
7930 -- which a non-null base init proc is defined, or for objects of access
7931 -- types with implicit null initialization, or when Normalize_Scalars
7932 -- applies and the type is scalar or a string type (the latter being
7933 -- tested for because predefined String types are initialized by inline
7934 -- code rather than by an init_proc). Note that we do not give the
7935 -- warning for Initialize_Scalars, since we suppressed initialization
7936 -- in this case. Also, do not warn if Suppress_Initialization is set.
7938 if Present (Expr)
7939 and then not Is_Imported (Ent)
7940 and then not Initialization_Suppressed (Typ)
7941 and then (Has_Non_Null_Base_Init_Proc (Typ)
7942 or else Is_Access_Type (Typ)
7943 or else (Normalize_Scalars
7944 and then (Is_Scalar_Type (Typ)
7945 or else Is_String_Type (Typ))))
7946 then
7947 if Nkind (Expr) = N_Attribute_Reference
7948 and then Is_Entity_Name (Prefix (Expr))
7949 then
7950 Old := Entity (Prefix (Expr));
7952 elsif Is_Entity_Name (Expr)
7953 and then Ekind (Entity (Expr)) = E_Constant
7954 then
7955 Decl := Declaration_Node (Entity (Expr));
7957 if Nkind (Decl) = N_Object_Declaration
7958 and then Present (Expression (Decl))
7959 and then Nkind (Expression (Decl)) = N_Attribute_Reference
7960 and then Is_Entity_Name (Prefix (Expression (Decl)))
7961 then
7962 Old := Entity (Prefix (Expression (Decl)));
7964 elsif Nkind (Expr) = N_Function_Call then
7965 return;
7966 end if;
7968 -- A function call (most likely to To_Address) is probably not an
7969 -- overlay, so skip warning. Ditto if the function call was inlined
7970 -- and transformed into an entity.
7972 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
7973 return;
7974 end if;
7976 Decl := Next (Parent (Expr));
7978 -- If a pragma Import follows, we assume that it is for the current
7979 -- target of the address clause, and skip the warning.
7981 if Present (Decl)
7982 and then Nkind (Decl) = N_Pragma
7983 and then Pragma_Name (Decl) = Name_Import
7984 then
7985 return;
7986 end if;
7988 if Present (Old) then
7989 Error_Msg_Node_2 := Old;
7990 Error_Msg_N
7991 ("default initialization of & may modify &??",
7992 Nam);
7993 else
7994 Error_Msg_N
7995 ("default initialization of & may modify overlaid storage??",
7996 Nam);
7997 end if;
7999 -- Add friendly warning if initialization comes from a packed array
8000 -- component.
8002 if Is_Record_Type (Typ) then
8003 declare
8004 Comp : Entity_Id;
8006 begin
8007 Comp := First_Component (Typ);
8008 while Present (Comp) loop
8009 if Nkind (Parent (Comp)) = N_Component_Declaration
8010 and then Present (Expression (Parent (Comp)))
8011 then
8012 exit;
8013 elsif Is_Array_Type (Etype (Comp))
8014 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
8015 then
8016 Error_Msg_NE
8017 ("\packed array component& " &
8018 "will be initialized to zero??",
8019 Nam, Comp);
8020 exit;
8021 else
8022 Next_Component (Comp);
8023 end if;
8024 end loop;
8025 end;
8026 end if;
8028 Error_Msg_N
8029 ("\use pragma Import for & to " &
8030 "suppress initialization (RM B.1(24))??",
8031 Nam);
8032 end if;
8033 end Warn_Overlay;
8035 end Freeze;