2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / freeze.adb
blobc7ad86c1d41b5efda83caf425d9940b6567fc0ed
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 Has_Non_Limited_View (Ret_Type) then
428 Set_Result_Definition
429 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
430 end if;
431 end;
432 end if;
434 -- The body generated for this renaming is an internal artifact, and
435 -- does not constitute a freeze point for the called entity.
437 Set_Must_Not_Freeze (Call_Name);
439 Formal := First_Formal (Defining_Entity (Decl));
441 if Present (Pref) then
442 declare
443 Pref_Type : constant Entity_Id := Etype (Pref);
444 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
446 begin
447 -- The controlling formal may be an access parameter, or the
448 -- actual may be an access value, so adjust accordingly.
450 if Is_Access_Type (Pref_Type)
451 and then not Is_Access_Type (Form_Type)
452 then
453 Actuals := New_List
454 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
456 elsif Is_Access_Type (Form_Type)
457 and then not Is_Access_Type (Pref)
458 then
459 Actuals :=
460 New_List (
461 Make_Attribute_Reference (Loc,
462 Attribute_Name => Name_Access,
463 Prefix => Relocate_Node (Pref)));
464 else
465 Actuals := New_List (Pref);
466 end if;
467 end;
469 elsif Present (Formal) then
470 Actuals := New_List;
472 else
473 Actuals := No_List;
474 end if;
476 if Present (Formal) then
477 while Present (Formal) loop
478 Append (New_Occurrence_Of (Formal, Loc), Actuals);
479 Next_Formal (Formal);
480 end loop;
481 end if;
483 -- If the renamed entity is an entry, inherit its profile. For other
484 -- renamings as bodies, both profiles must be subtype conformant, so it
485 -- is not necessary to replace the profile given in the declaration.
486 -- However, default values that are aggregates are rewritten when
487 -- partially analyzed, so we recover the original aggregate to insure
488 -- that subsequent conformity checking works. Similarly, if the default
489 -- expression was constant-folded, recover the original expression.
491 Formal := First_Formal (Defining_Entity (Decl));
493 if Present (Formal) then
494 O_Formal := First_Formal (Old_S);
495 Param_Spec := First (Parameter_Specifications (Spec));
496 while Present (Formal) loop
497 if Is_Entry (Old_S) then
498 if Nkind (Parameter_Type (Param_Spec)) /=
499 N_Access_Definition
500 then
501 Set_Etype (Formal, Etype (O_Formal));
502 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
503 end if;
505 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
506 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
507 Nkind (Default_Value (O_Formal))
508 then
509 Set_Expression (Param_Spec,
510 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
511 end if;
513 Next_Formal (Formal);
514 Next_Formal (O_Formal);
515 Next (Param_Spec);
516 end loop;
517 end if;
519 -- If the renamed entity is a function, the generated body contains a
520 -- return statement. Otherwise, build a procedure call. If the entity is
521 -- an entry, subsequent analysis of the call will transform it into the
522 -- proper entry or protected operation call. If the renamed entity is
523 -- a character literal, return it directly.
525 if Ekind (Old_S) = E_Function
526 or else Ekind (Old_S) = E_Operator
527 or else (Ekind (Old_S) = E_Subprogram_Type
528 and then Etype (Old_S) /= Standard_Void_Type)
529 then
530 Call_Node :=
531 Make_Simple_Return_Statement (Loc,
532 Expression =>
533 Make_Function_Call (Loc,
534 Name => Call_Name,
535 Parameter_Associations => Actuals));
537 elsif Ekind (Old_S) = E_Enumeration_Literal then
538 Call_Node :=
539 Make_Simple_Return_Statement (Loc,
540 Expression => New_Occurrence_Of (Old_S, Loc));
542 elsif Nkind (Nam) = N_Character_Literal then
543 Call_Node :=
544 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
546 else
547 Call_Node :=
548 Make_Procedure_Call_Statement (Loc,
549 Name => Call_Name,
550 Parameter_Associations => Actuals);
551 end if;
553 -- Create entities for subprogram body and formals
555 Set_Defining_Unit_Name (Spec,
556 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
558 Param_Spec := First (Parameter_Specifications (Spec));
559 while Present (Param_Spec) loop
560 Set_Defining_Identifier (Param_Spec,
561 Make_Defining_Identifier (Loc,
562 Chars => Chars (Defining_Identifier (Param_Spec))));
563 Next (Param_Spec);
564 end loop;
566 Body_Node :=
567 Make_Subprogram_Body (Loc,
568 Specification => Spec,
569 Declarations => New_List,
570 Handled_Statement_Sequence =>
571 Make_Handled_Sequence_Of_Statements (Loc,
572 Statements => New_List (Call_Node)));
574 if Nkind (Decl) /= N_Subprogram_Declaration then
575 Rewrite (N,
576 Make_Subprogram_Declaration (Loc,
577 Specification => Specification (N)));
578 end if;
580 -- Link the body to the entity whose declaration it completes. If
581 -- the body is analyzed when the renamed entity is frozen, it may
582 -- be necessary to restore the proper scope (see package Exp_Ch13).
584 if Nkind (N) = N_Subprogram_Renaming_Declaration
585 and then Present (Corresponding_Spec (N))
586 then
587 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
588 else
589 Set_Corresponding_Spec (Body_Node, New_S);
590 end if;
592 return Body_Node;
593 end Build_Renamed_Body;
595 --------------------------
596 -- Check_Address_Clause --
597 --------------------------
599 procedure Check_Address_Clause (E : Entity_Id) is
600 Addr : constant Node_Id := Address_Clause (E);
601 Expr : Node_Id;
602 Decl : constant Node_Id := Declaration_Node (E);
603 Loc : constant Source_Ptr := Sloc (Decl);
604 Typ : constant Entity_Id := Etype (E);
605 Lhs : Node_Id;
606 Tag_Assign : Node_Id;
608 begin
609 if Present (Addr) then
610 Expr := Expression (Addr);
612 if Needs_Constant_Address (Decl, Typ) then
613 Check_Constant_Address_Clause (Expr, E);
615 -- Has_Delayed_Freeze was set on E when the address clause was
616 -- analyzed, and must remain set because we want the address
617 -- clause to be elaborated only after any entity it references
618 -- has been elaborated.
619 end if;
621 -- If Rep_Clauses are to be ignored, remove address clause from
622 -- list attached to entity, because it may be illegal for gigi,
623 -- for example by breaking order of elaboration..
625 if Ignore_Rep_Clauses then
626 declare
627 Rep : Node_Id;
629 begin
630 Rep := First_Rep_Item (E);
632 if Rep = Addr then
633 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
635 else
636 while Present (Rep)
637 and then Next_Rep_Item (Rep) /= Addr
638 loop
639 Rep := Next_Rep_Item (Rep);
640 end loop;
641 end if;
643 if Present (Rep) then
644 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
645 end if;
646 end;
648 -- And now remove the address clause
650 Kill_Rep_Clause (Addr);
652 elsif not Error_Posted (Expr)
653 and then not Needs_Finalization (Typ)
654 then
655 Warn_Overlay (Expr, Typ, Name (Addr));
656 end if;
658 if Present (Expression (Decl)) then
660 -- Capture initialization value at point of declaration,
661 -- and make explicit assignment legal, because object may
662 -- be a constant.
664 Remove_Side_Effects (Expression (Decl));
665 Lhs := New_Occurrence_Of (E, Loc);
666 Set_Assignment_OK (Lhs);
668 -- Move initialization to freeze actions (once the object has
669 -- been frozen, and the address clause alignment check has been
670 -- performed.
672 Append_Freeze_Action (E,
673 Make_Assignment_Statement (Loc,
674 Name => Lhs,
675 Expression => Expression (Decl)));
677 Set_No_Initialization (Decl);
679 -- If the objet is tagged, check whether the tag must be
680 -- reassigned expliitly.
682 Tag_Assign := Make_Tag_Assignment (Decl);
683 if Present (Tag_Assign) then
684 Append_Freeze_Action (E, Tag_Assign);
685 end if;
686 end if;
687 end if;
688 end Check_Address_Clause;
690 -----------------------------
691 -- Check_Compile_Time_Size --
692 -----------------------------
694 procedure Check_Compile_Time_Size (T : Entity_Id) is
696 procedure Set_Small_Size (T : Entity_Id; S : Uint);
697 -- Sets the compile time known size (32 bits or less) in the Esize
698 -- field, of T checking for a size clause that was given which attempts
699 -- to give a smaller size, and also checking for an alignment clause.
701 function Size_Known (T : Entity_Id) return Boolean;
702 -- Recursive function that does all the work
704 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
705 -- If T is a constrained subtype, its size is not known if any of its
706 -- discriminant constraints is not static and it is not a null record.
707 -- The test is conservative and doesn't check that the components are
708 -- in fact constrained by non-static discriminant values. Could be made
709 -- more precise ???
711 --------------------
712 -- Set_Small_Size --
713 --------------------
715 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
716 begin
717 if S > 32 then
718 return;
720 -- Check for bad size clause given
722 elsif Has_Size_Clause (T) then
723 if RM_Size (T) < S then
724 Error_Msg_Uint_1 := S;
725 Error_Msg_NE
726 ("size for& too small, minimum allowed is ^",
727 Size_Clause (T), T);
728 end if;
730 -- Set size if not set already
732 elsif Unknown_RM_Size (T) then
733 Set_RM_Size (T, S);
734 end if;
735 end Set_Small_Size;
737 ----------------
738 -- Size_Known --
739 ----------------
741 function Size_Known (T : Entity_Id) return Boolean is
742 Index : Entity_Id;
743 Comp : Entity_Id;
744 Ctyp : Entity_Id;
745 Low : Node_Id;
746 High : Node_Id;
748 begin
749 if Size_Known_At_Compile_Time (T) then
750 return True;
752 -- Always True for scalar types. This is true even for generic formal
753 -- scalar types. We used to return False in the latter case, but the
754 -- size is known at compile time, even in the template, we just do
755 -- not know the exact size but that's not the point of this routine.
757 elsif Is_Scalar_Type (T)
758 or else Is_Task_Type (T)
759 then
760 return True;
762 -- Array types
764 elsif Is_Array_Type (T) then
766 -- String literals always have known size, and we can set it
768 if Ekind (T) = E_String_Literal_Subtype then
769 Set_Small_Size (T, Component_Size (T)
770 * String_Literal_Length (T));
771 return True;
773 -- Unconstrained types never have known at compile time size
775 elsif not Is_Constrained (T) then
776 return False;
778 -- Don't do any recursion on type with error posted, since we may
779 -- have a malformed type that leads us into a loop.
781 elsif Error_Posted (T) then
782 return False;
784 -- Otherwise if component size unknown, then array size unknown
786 elsif not Size_Known (Component_Type (T)) then
787 return False;
788 end if;
790 -- Check for all indexes static, and also compute possible size
791 -- (in case it is less than 32 and may be packable).
793 declare
794 Esiz : Uint := Component_Size (T);
795 Dim : Uint;
797 begin
798 Index := First_Index (T);
799 while Present (Index) loop
800 if Nkind (Index) = N_Range then
801 Get_Index_Bounds (Index, Low, High);
803 elsif Error_Posted (Scalar_Range (Etype (Index))) then
804 return False;
806 else
807 Low := Type_Low_Bound (Etype (Index));
808 High := Type_High_Bound (Etype (Index));
809 end if;
811 if not Compile_Time_Known_Value (Low)
812 or else not Compile_Time_Known_Value (High)
813 or else Etype (Index) = Any_Type
814 then
815 return False;
817 else
818 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
820 if Dim >= 0 then
821 Esiz := Esiz * Dim;
822 else
823 Esiz := Uint_0;
824 end if;
825 end if;
827 Next_Index (Index);
828 end loop;
830 Set_Small_Size (T, Esiz);
831 return True;
832 end;
834 -- Access types always have known at compile time sizes
836 elsif Is_Access_Type (T) then
837 return True;
839 -- For non-generic private types, go to underlying type if present
841 elsif Is_Private_Type (T)
842 and then not Is_Generic_Type (T)
843 and then Present (Underlying_Type (T))
844 then
845 -- Don't do any recursion on type with error posted, since we may
846 -- have a malformed type that leads us into a loop.
848 if Error_Posted (T) then
849 return False;
850 else
851 return Size_Known (Underlying_Type (T));
852 end if;
854 -- Record types
856 elsif Is_Record_Type (T) then
858 -- A class-wide type is never considered to have a known size
860 if Is_Class_Wide_Type (T) then
861 return False;
863 -- A subtype of a variant record must not have non-static
864 -- discriminated components.
866 elsif T /= Base_Type (T)
867 and then not Static_Discriminated_Components (T)
868 then
869 return False;
871 -- Don't do any recursion on type with error posted, since we may
872 -- have a malformed type that leads us into a loop.
874 elsif Error_Posted (T) then
875 return False;
876 end if;
878 -- Now look at the components of the record
880 declare
881 -- The following two variables are used to keep track of the
882 -- size of packed records if we can tell the size of the packed
883 -- record in the front end. Packed_Size_Known is True if so far
884 -- we can figure out the size. It is initialized to True for a
885 -- packed record, unless the record has discriminants or atomic
886 -- components or independent components.
888 -- The reason we eliminate the discriminated case is that
889 -- we don't know the way the back end lays out discriminated
890 -- packed records. If Packed_Size_Known is True, then
891 -- Packed_Size is the size in bits so far.
893 Packed_Size_Known : Boolean :=
894 Is_Packed (T)
895 and then not Has_Discriminants (T)
896 and then not Has_Atomic_Components (T)
897 and then not Has_Independent_Components (T);
899 Packed_Size : Uint := Uint_0;
900 -- Size in bits so far
902 begin
903 -- Test for variant part present
905 if Has_Discriminants (T)
906 and then Present (Parent (T))
907 and then Nkind (Parent (T)) = N_Full_Type_Declaration
908 and then Nkind (Type_Definition (Parent (T))) =
909 N_Record_Definition
910 and then not Null_Present (Type_Definition (Parent (T)))
911 and then
912 Present (Variant_Part
913 (Component_List (Type_Definition (Parent (T)))))
914 then
915 -- If variant part is present, and type is unconstrained,
916 -- then we must have defaulted discriminants, or a size
917 -- clause must be present for the type, or else the size
918 -- is definitely not known at compile time.
920 if not Is_Constrained (T)
921 and then
922 No (Discriminant_Default_Value (First_Discriminant (T)))
923 and then Unknown_RM_Size (T)
924 then
925 return False;
926 end if;
927 end if;
929 -- Loop through components
931 Comp := First_Component_Or_Discriminant (T);
932 while Present (Comp) loop
933 Ctyp := Etype (Comp);
935 -- We do not know the packed size if there is a component
936 -- clause present (we possibly could, but this would only
937 -- help in the case of a record with partial rep clauses.
938 -- That's because in the case of full rep clauses, the
939 -- size gets figured out anyway by a different circuit).
941 if Present (Component_Clause (Comp)) then
942 Packed_Size_Known := False;
943 end if;
945 -- We do not know the packed size for an atomic/VFA type
946 -- or component, or an independent type or component, or a
947 -- by-reference type or aliased component (because packing
948 -- does not touch these).
950 if Is_Atomic_Or_VFA (Ctyp)
951 or else Is_Atomic_Or_VFA (Comp)
952 or else Is_Independent (Ctyp)
953 or else Is_Independent (Comp)
954 or else Is_By_Reference_Type (Ctyp)
955 or else Is_Aliased (Comp)
956 then
957 Packed_Size_Known := False;
958 end if;
960 -- We need to identify a component that is an array where
961 -- the index type is an enumeration type with non-standard
962 -- representation, and some bound of the type depends on a
963 -- discriminant.
965 -- This is because gigi computes the size by doing a
966 -- substitution of the appropriate discriminant value in
967 -- the size expression for the base type, and gigi is not
968 -- clever enough to evaluate the resulting expression (which
969 -- involves a call to rep_to_pos) at compile time.
971 -- It would be nice if gigi would either recognize that
972 -- this expression can be computed at compile time, or
973 -- alternatively figured out the size from the subtype
974 -- directly, where all the information is at hand ???
976 if Is_Array_Type (Etype (Comp))
977 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
978 then
979 declare
980 Ocomp : constant Entity_Id :=
981 Original_Record_Component (Comp);
982 OCtyp : constant Entity_Id := Etype (Ocomp);
983 Ind : Node_Id;
984 Indtyp : Entity_Id;
985 Lo, Hi : Node_Id;
987 begin
988 Ind := First_Index (OCtyp);
989 while Present (Ind) loop
990 Indtyp := Etype (Ind);
992 if Is_Enumeration_Type (Indtyp)
993 and then Has_Non_Standard_Rep (Indtyp)
994 then
995 Lo := Type_Low_Bound (Indtyp);
996 Hi := Type_High_Bound (Indtyp);
998 if Is_Entity_Name (Lo)
999 and then Ekind (Entity (Lo)) = E_Discriminant
1000 then
1001 return False;
1003 elsif Is_Entity_Name (Hi)
1004 and then Ekind (Entity (Hi)) = E_Discriminant
1005 then
1006 return False;
1007 end if;
1008 end if;
1010 Next_Index (Ind);
1011 end loop;
1012 end;
1013 end if;
1015 -- Clearly size of record is not known if the size of one of
1016 -- the components is not known.
1018 if not Size_Known (Ctyp) then
1019 return False;
1020 end if;
1022 -- Accumulate packed size if possible
1024 if Packed_Size_Known then
1026 -- We can only deal with elementary types, since for
1027 -- non-elementary components, alignment enters into the
1028 -- picture, and we don't know enough to handle proper
1029 -- alignment in this context. Packed arrays count as
1030 -- elementary if the representation is a modular type.
1032 if Is_Elementary_Type (Ctyp)
1033 or else (Is_Array_Type (Ctyp)
1034 and then Present
1035 (Packed_Array_Impl_Type (Ctyp))
1036 and then Is_Modular_Integer_Type
1037 (Packed_Array_Impl_Type (Ctyp)))
1038 then
1039 -- Packed size unknown if we have an atomic/VFA type
1040 -- or a by-reference type, since the back end knows
1041 -- how these are layed out.
1043 if Is_Atomic_Or_VFA (Ctyp)
1044 or else Is_By_Reference_Type (Ctyp)
1045 then
1046 Packed_Size_Known := False;
1048 -- If RM_Size is known and static, then we can keep
1049 -- accumulating the packed size
1051 elsif Known_Static_RM_Size (Ctyp) then
1053 -- A little glitch, to be removed sometime ???
1054 -- gigi does not understand zero sizes yet.
1056 if RM_Size (Ctyp) = Uint_0 then
1057 Packed_Size_Known := False;
1059 -- Normal case where we can keep accumulating the
1060 -- packed array size.
1062 else
1063 Packed_Size := Packed_Size + RM_Size (Ctyp);
1064 end if;
1066 -- If we have a field whose RM_Size is not known then
1067 -- we can't figure out the packed size here.
1069 else
1070 Packed_Size_Known := False;
1071 end if;
1073 -- If we have a non-elementary type we can't figure out
1074 -- the packed array size (alignment issues).
1076 else
1077 Packed_Size_Known := False;
1078 end if;
1079 end if;
1081 Next_Component_Or_Discriminant (Comp);
1082 end loop;
1084 if Packed_Size_Known then
1085 Set_Small_Size (T, Packed_Size);
1086 end if;
1088 return True;
1089 end;
1091 -- All other cases, size not known at compile time
1093 else
1094 return False;
1095 end if;
1096 end Size_Known;
1098 -------------------------------------
1099 -- Static_Discriminated_Components --
1100 -------------------------------------
1102 function Static_Discriminated_Components
1103 (T : Entity_Id) return Boolean
1105 Constraint : Elmt_Id;
1107 begin
1108 if Has_Discriminants (T)
1109 and then Present (Discriminant_Constraint (T))
1110 and then Present (First_Component (T))
1111 then
1112 Constraint := First_Elmt (Discriminant_Constraint (T));
1113 while Present (Constraint) loop
1114 if not Compile_Time_Known_Value (Node (Constraint)) then
1115 return False;
1116 end if;
1118 Next_Elmt (Constraint);
1119 end loop;
1120 end if;
1122 return True;
1123 end Static_Discriminated_Components;
1125 -- Start of processing for Check_Compile_Time_Size
1127 begin
1128 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1129 end Check_Compile_Time_Size;
1131 -----------------------------------
1132 -- Check_Component_Storage_Order --
1133 -----------------------------------
1135 procedure Check_Component_Storage_Order
1136 (Encl_Type : Entity_Id;
1137 Comp : Entity_Id;
1138 ADC : Node_Id;
1139 Comp_ADC_Present : out Boolean)
1141 Comp_Type : Entity_Id;
1142 Comp_ADC : Node_Id;
1143 Err_Node : Node_Id;
1145 Comp_Byte_Aligned : Boolean;
1146 -- Set for the record case, True if Comp starts on a byte boundary
1147 -- (in which case it is allowed to have different storage order).
1149 Comp_SSO_Differs : Boolean;
1150 -- Set True when the component is a nested composite, and it does not
1151 -- have the same scalar storage order as Encl_Type.
1153 Component_Aliased : Boolean;
1155 begin
1156 -- Record case
1158 if Present (Comp) then
1159 Err_Node := Comp;
1160 Comp_Type := Etype (Comp);
1162 if Is_Tag (Comp) then
1163 Comp_Byte_Aligned := True;
1164 Component_Aliased := False;
1166 else
1167 -- If a component clause is present, check if the component starts
1168 -- on a storage element boundary. Otherwise conservatively assume
1169 -- it does so only in the case where the record is not packed.
1171 if Present (Component_Clause (Comp)) then
1172 Comp_Byte_Aligned :=
1173 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0;
1174 else
1175 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1176 end if;
1178 Component_Aliased := Is_Aliased (Comp);
1179 end if;
1181 -- Array case
1183 else
1184 Err_Node := Encl_Type;
1185 Comp_Type := Component_Type (Encl_Type);
1187 Component_Aliased := Has_Aliased_Components (Encl_Type);
1188 end if;
1190 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1191 -- the attribute definition clause is attached to the first subtype.
1193 Comp_Type := Base_Type (Comp_Type);
1194 Comp_ADC := Get_Attribute_Definition_Clause
1195 (First_Subtype (Comp_Type),
1196 Attribute_Scalar_Storage_Order);
1197 Comp_ADC_Present := Present (Comp_ADC);
1199 -- Case of record or array component: check storage order compatibility
1201 if Is_Record_Type (Comp_Type) or else Is_Array_Type (Comp_Type) then
1202 Comp_SSO_Differs :=
1203 Reverse_Storage_Order (Encl_Type)
1205 Reverse_Storage_Order (Comp_Type);
1207 -- Parent and extension must have same storage order
1209 if Present (Comp) and then Chars (Comp) = Name_uParent then
1210 if Comp_SSO_Differs then
1211 Error_Msg_N
1212 ("record extension must have same scalar storage order as "
1213 & "parent", Err_Node);
1214 end if;
1216 -- If enclosing composite has explicit SSO then nested composite must
1217 -- have explicit SSO as well.
1219 elsif Present (ADC) and then No (Comp_ADC) then
1220 Error_Msg_N ("nested composite must have explicit scalar "
1221 & "storage order", Err_Node);
1223 -- If component and composite SSO differs, check that component
1224 -- falls on byte boundaries and isn't packed.
1226 elsif Comp_SSO_Differs then
1228 -- Component SSO differs from enclosing composite:
1230 -- Reject if component is a packed array, as it may be represented
1231 -- as a scalar internally.
1233 if Is_Packed_Array (Comp_Type) then
1234 Error_Msg_N
1235 ("type of packed component must have same scalar "
1236 & "storage order as enclosing composite", Err_Node);
1238 -- Reject if composite is a packed array, as it may be rewritten
1239 -- into an array of scalars.
1241 elsif Is_Packed_Array (Encl_Type) then
1242 Error_Msg_N ("type of packed array must have same scalar "
1243 & "storage order as component", Err_Node);
1245 -- Reject if not byte aligned
1247 elsif Is_Record_Type (Encl_Type)
1248 and then not Comp_Byte_Aligned
1249 then
1250 Error_Msg_N
1251 ("type of non-byte-aligned component must have same scalar "
1252 & "storage order as enclosing composite", Err_Node);
1253 end if;
1254 end if;
1256 -- Enclosing type has explicit SSO: non-composite component must not
1257 -- be aliased.
1259 elsif Present (ADC) and then Component_Aliased then
1260 Error_Msg_N
1261 ("aliased component not permitted for type with "
1262 & "explicit Scalar_Storage_Order", Err_Node);
1263 end if;
1264 end Check_Component_Storage_Order;
1266 -----------------------------
1267 -- Check_Debug_Info_Needed --
1268 -----------------------------
1270 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1271 begin
1272 if Debug_Info_Off (T) then
1273 return;
1275 elsif Comes_From_Source (T)
1276 or else Debug_Generated_Code
1277 or else Debug_Flag_VV
1278 or else Needs_Debug_Info (T)
1279 then
1280 Set_Debug_Info_Needed (T);
1281 end if;
1282 end Check_Debug_Info_Needed;
1284 -------------------------------
1285 -- Check_Expression_Function --
1286 -------------------------------
1288 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1289 Decl : Node_Id;
1291 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1292 -- Function to search for deferred constant
1294 -------------------
1295 -- Find_Constant --
1296 -------------------
1298 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1299 begin
1300 -- When a constant is initialized with the result of a dispatching
1301 -- call, the constant declaration is rewritten as a renaming of the
1302 -- displaced function result. This scenario is not a premature use of
1303 -- a constant even though the Has_Completion flag is not set.
1305 if Is_Entity_Name (Nod)
1306 and then Present (Entity (Nod))
1307 and then Ekind (Entity (Nod)) = E_Constant
1308 and then Scope (Entity (Nod)) = Current_Scope
1309 and then Nkind (Declaration_Node (Entity (Nod))) =
1310 N_Object_Declaration
1311 and then not Is_Imported (Entity (Nod))
1312 and then not Has_Completion (Entity (Nod))
1313 then
1314 Error_Msg_NE
1315 ("premature use of& in call or instance", N, Entity (Nod));
1317 elsif Nkind (Nod) = N_Attribute_Reference then
1318 Analyze (Prefix (Nod));
1320 if Is_Entity_Name (Prefix (Nod))
1321 and then Is_Type (Entity (Prefix (Nod)))
1322 then
1323 Freeze_Before (N, Entity (Prefix (Nod)));
1324 end if;
1325 end if;
1327 return OK;
1328 end Find_Constant;
1330 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1332 -- Start of processing for Check_Expression_Function
1334 begin
1335 Decl := Original_Node (Unit_Declaration_Node (Nam));
1337 if Scope (Nam) = Current_Scope
1338 and then Nkind (Decl) = N_Expression_Function
1339 then
1340 Check_Deferred (Expression (Decl));
1341 end if;
1342 end Check_Expression_Function;
1344 ----------------------------
1345 -- Check_Strict_Alignment --
1346 ----------------------------
1348 procedure Check_Strict_Alignment (E : Entity_Id) is
1349 Comp : Entity_Id;
1351 begin
1352 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1353 Set_Strict_Alignment (E);
1355 elsif Is_Array_Type (E) then
1356 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1358 elsif Is_Record_Type (E) then
1359 if Is_Limited_Record (E) then
1360 Set_Strict_Alignment (E);
1361 return;
1362 end if;
1364 Comp := First_Component (E);
1365 while Present (Comp) loop
1366 if not Is_Type (Comp)
1367 and then (Strict_Alignment (Etype (Comp))
1368 or else Is_Aliased (Comp))
1369 then
1370 Set_Strict_Alignment (E);
1371 return;
1372 end if;
1374 Next_Component (Comp);
1375 end loop;
1376 end if;
1377 end Check_Strict_Alignment;
1379 -------------------------
1380 -- Check_Unsigned_Type --
1381 -------------------------
1383 procedure Check_Unsigned_Type (E : Entity_Id) is
1384 Ancestor : Entity_Id;
1385 Lo_Bound : Node_Id;
1386 Btyp : Entity_Id;
1388 begin
1389 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1390 return;
1391 end if;
1393 -- Do not attempt to analyze case where range was in error
1395 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
1396 return;
1397 end if;
1399 -- The situation that is non trivial is something like
1401 -- subtype x1 is integer range -10 .. +10;
1402 -- subtype x2 is x1 range 0 .. V1;
1403 -- subtype x3 is x2 range V2 .. V3;
1404 -- subtype x4 is x3 range V4 .. V5;
1406 -- where Vn are variables. Here the base type is signed, but we still
1407 -- know that x4 is unsigned because of the lower bound of x2.
1409 -- The only way to deal with this is to look up the ancestor chain
1411 Ancestor := E;
1412 loop
1413 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1414 return;
1415 end if;
1417 Lo_Bound := Type_Low_Bound (Ancestor);
1419 if Compile_Time_Known_Value (Lo_Bound) then
1420 if Expr_Rep_Value (Lo_Bound) >= 0 then
1421 Set_Is_Unsigned_Type (E, True);
1422 end if;
1424 return;
1426 else
1427 Ancestor := Ancestor_Subtype (Ancestor);
1429 -- If no ancestor had a static lower bound, go to base type
1431 if No (Ancestor) then
1433 -- Note: the reason we still check for a compile time known
1434 -- value for the base type is that at least in the case of
1435 -- generic formals, we can have bounds that fail this test,
1436 -- and there may be other cases in error situations.
1438 Btyp := Base_Type (E);
1440 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1441 return;
1442 end if;
1444 Lo_Bound := Type_Low_Bound (Base_Type (E));
1446 if Compile_Time_Known_Value (Lo_Bound)
1447 and then Expr_Rep_Value (Lo_Bound) >= 0
1448 then
1449 Set_Is_Unsigned_Type (E, True);
1450 end if;
1452 return;
1453 end if;
1454 end if;
1455 end loop;
1456 end Check_Unsigned_Type;
1458 -----------------------------
1459 -- Is_Atomic_VFA_Aggregate --
1460 -----------------------------
1462 function Is_Atomic_VFA_Aggregate (N : Node_Id) return Boolean is
1463 Loc : constant Source_Ptr := Sloc (N);
1464 New_N : Node_Id;
1465 Par : Node_Id;
1466 Temp : Entity_Id;
1467 Typ : Entity_Id;
1469 begin
1470 Par := Parent (N);
1472 -- Array may be qualified, so find outer context
1474 if Nkind (Par) = N_Qualified_Expression then
1475 Par := Parent (Par);
1476 end if;
1478 if not Comes_From_Source (Par) then
1479 return False;
1480 end if;
1482 case Nkind (Par) is
1483 when N_Assignment_Statement =>
1484 Typ := Etype (Name (Par));
1486 if not Is_Atomic_Or_VFA (Typ)
1487 and then not (Is_Entity_Name (Name (Par))
1488 and then Is_Atomic_Or_VFA (Entity (Name (Par))))
1489 then
1490 return False;
1491 end if;
1493 when N_Object_Declaration =>
1494 Typ := Etype (Defining_Identifier (Par));
1496 if not Is_Atomic_Or_VFA (Typ)
1497 and then not Is_Atomic_Or_VFA (Defining_Identifier (Par))
1498 then
1499 return False;
1500 end if;
1502 when others =>
1503 return False;
1504 end case;
1506 Temp := Make_Temporary (Loc, 'T', N);
1507 New_N :=
1508 Make_Object_Declaration (Loc,
1509 Defining_Identifier => Temp,
1510 Object_Definition => New_Occurrence_Of (Typ, Loc),
1511 Expression => Relocate_Node (N));
1512 Insert_Before (Par, New_N);
1513 Analyze (New_N);
1515 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1516 return True;
1517 end Is_Atomic_VFA_Aggregate;
1519 -----------------------------------------------
1520 -- Explode_Initialization_Compound_Statement --
1521 -----------------------------------------------
1523 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
1524 Init_Stmts : constant Node_Id := Initialization_Statements (E);
1526 begin
1527 if Present (Init_Stmts)
1528 and then Nkind (Init_Stmts) = N_Compound_Statement
1529 then
1530 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
1532 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
1533 -- just removing it, because Freeze_All may rely on this particular
1534 -- Node_Id still being present in the enclosing list to know where to
1535 -- stop freezing.
1537 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
1539 Set_Initialization_Statements (E, Empty);
1540 end if;
1541 end Explode_Initialization_Compound_Statement;
1543 ----------------
1544 -- Freeze_All --
1545 ----------------
1547 -- Note: the easy coding for this procedure would be to just build a
1548 -- single list of freeze nodes and then insert them and analyze them
1549 -- all at once. This won't work, because the analysis of earlier freeze
1550 -- nodes may recursively freeze types which would otherwise appear later
1551 -- on in the freeze list. So we must analyze and expand the freeze nodes
1552 -- as they are generated.
1554 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1555 E : Entity_Id;
1556 Decl : Node_Id;
1558 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1559 -- This is the internal recursive routine that does freezing of entities
1560 -- (but NOT the analysis of default expressions, which should not be
1561 -- recursive, we don't want to analyze those till we are sure that ALL
1562 -- the types are frozen).
1564 --------------------
1565 -- Freeze_All_Ent --
1566 --------------------
1568 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
1569 E : Entity_Id;
1570 Flist : List_Id;
1571 Lastn : Node_Id;
1573 procedure Process_Flist;
1574 -- If freeze nodes are present, insert and analyze, and reset cursor
1575 -- for next insertion.
1577 -------------------
1578 -- Process_Flist --
1579 -------------------
1581 procedure Process_Flist is
1582 begin
1583 if Is_Non_Empty_List (Flist) then
1584 Lastn := Next (After);
1585 Insert_List_After_And_Analyze (After, Flist);
1587 if Present (Lastn) then
1588 After := Prev (Lastn);
1589 else
1590 After := Last (List_Containing (After));
1591 end if;
1592 end if;
1593 end Process_Flist;
1595 -- Start or processing for Freeze_All_Ent
1597 begin
1598 E := From;
1599 while Present (E) loop
1601 -- If the entity is an inner package which is not a package
1602 -- renaming, then its entities must be frozen at this point. Note
1603 -- that such entities do NOT get frozen at the end of the nested
1604 -- package itself (only library packages freeze).
1606 -- Same is true for task declarations, where anonymous records
1607 -- created for entry parameters must be frozen.
1609 if Ekind (E) = E_Package
1610 and then No (Renamed_Object (E))
1611 and then not Is_Child_Unit (E)
1612 and then not Is_Frozen (E)
1613 then
1614 Push_Scope (E);
1615 Install_Visible_Declarations (E);
1616 Install_Private_Declarations (E);
1618 Freeze_All (First_Entity (E), After);
1620 End_Package_Scope (E);
1622 if Is_Generic_Instance (E)
1623 and then Has_Delayed_Freeze (E)
1624 then
1625 Set_Has_Delayed_Freeze (E, False);
1626 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1627 end if;
1629 elsif Ekind (E) in Task_Kind
1630 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1631 N_Single_Task_Declaration)
1632 then
1633 Push_Scope (E);
1634 Freeze_All (First_Entity (E), After);
1635 End_Scope;
1637 -- For a derived tagged type, we must ensure that all the
1638 -- primitive operations of the parent have been frozen, so that
1639 -- their addresses will be in the parent's dispatch table at the
1640 -- point it is inherited.
1642 elsif Ekind (E) = E_Record_Type
1643 and then Is_Tagged_Type (E)
1644 and then Is_Tagged_Type (Etype (E))
1645 and then Is_Derived_Type (E)
1646 then
1647 declare
1648 Prim_List : constant Elist_Id :=
1649 Primitive_Operations (Etype (E));
1651 Prim : Elmt_Id;
1652 Subp : Entity_Id;
1654 begin
1655 Prim := First_Elmt (Prim_List);
1656 while Present (Prim) loop
1657 Subp := Node (Prim);
1659 if Comes_From_Source (Subp)
1660 and then not Is_Frozen (Subp)
1661 then
1662 Flist := Freeze_Entity (Subp, After);
1663 Process_Flist;
1664 end if;
1666 Next_Elmt (Prim);
1667 end loop;
1668 end;
1669 end if;
1671 if not Is_Frozen (E) then
1672 Flist := Freeze_Entity (E, After);
1673 Process_Flist;
1675 -- If already frozen, and there are delayed aspects, this is where
1676 -- we do the visibility check for these aspects (see Sem_Ch13 spec
1677 -- for a description of how we handle aspect visibility).
1679 elsif Has_Delayed_Aspects (E) then
1681 -- Retrieve the visibility to the discriminants in order to
1682 -- analyze properly the aspects.
1684 Push_Scope_And_Install_Discriminants (E);
1686 declare
1687 Ritem : Node_Id;
1689 begin
1690 Ritem := First_Rep_Item (E);
1691 while Present (Ritem) loop
1692 if Nkind (Ritem) = N_Aspect_Specification
1693 and then Entity (Ritem) = E
1694 and then Is_Delayed_Aspect (Ritem)
1695 then
1696 Check_Aspect_At_End_Of_Declarations (Ritem);
1697 end if;
1699 Ritem := Next_Rep_Item (Ritem);
1700 end loop;
1701 end;
1703 Uninstall_Discriminants_And_Pop_Scope (E);
1704 end if;
1706 -- If an incomplete type is still not frozen, this may be a
1707 -- premature freezing because of a body declaration that follows.
1708 -- Indicate where the freezing took place. Freezing will happen
1709 -- if the body comes from source, but not if it is internally
1710 -- generated, for example as the body of a type invariant.
1712 -- If the freezing is caused by the end of the current declarative
1713 -- part, it is a Taft Amendment type, and there is no error.
1715 if not Is_Frozen (E)
1716 and then Ekind (E) = E_Incomplete_Type
1717 then
1718 declare
1719 Bod : constant Node_Id := Next (After);
1721 begin
1722 -- The presence of a body freezes all entities previously
1723 -- declared in the current list of declarations, but this
1724 -- does not apply if the body does not come from source.
1725 -- A type invariant is transformed into a subprogram body
1726 -- which is placed at the end of the private part of the
1727 -- current package, but this body does not freeze incomplete
1728 -- types that may be declared in this private part.
1730 if (Nkind_In (Bod, N_Subprogram_Body,
1731 N_Entry_Body,
1732 N_Package_Body,
1733 N_Protected_Body,
1734 N_Task_Body)
1735 or else Nkind (Bod) in N_Body_Stub)
1736 and then
1737 List_Containing (After) = List_Containing (Parent (E))
1738 and then Comes_From_Source (Bod)
1739 then
1740 Error_Msg_Sloc := Sloc (Next (After));
1741 Error_Msg_NE
1742 ("type& is frozen# before its full declaration",
1743 Parent (E), E);
1744 end if;
1745 end;
1746 end if;
1748 Next_Entity (E);
1749 end loop;
1750 end Freeze_All_Ent;
1752 -- Start of processing for Freeze_All
1754 begin
1755 Freeze_All_Ent (From, After);
1757 -- Now that all types are frozen, we can deal with default expressions
1758 -- that require us to build a default expression functions. This is the
1759 -- point at which such functions are constructed (after all types that
1760 -- might be used in such expressions have been frozen).
1762 -- For subprograms that are renaming_as_body, we create the wrapper
1763 -- bodies as needed.
1765 -- We also add finalization chains to access types whose designated
1766 -- types are controlled. This is normally done when freezing the type,
1767 -- but this misses recursive type definitions where the later members
1768 -- of the recursion introduce controlled components.
1770 -- Loop through entities
1772 E := From;
1773 while Present (E) loop
1774 if Is_Subprogram (E) then
1775 if not Default_Expressions_Processed (E) then
1776 Process_Default_Expressions (E, After);
1777 end if;
1779 if not Has_Completion (E) then
1780 Decl := Unit_Declaration_Node (E);
1782 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1783 if Error_Posted (Decl) then
1784 Set_Has_Completion (E);
1785 else
1786 Build_And_Analyze_Renamed_Body (Decl, E, After);
1787 end if;
1789 elsif Nkind (Decl) = N_Subprogram_Declaration
1790 and then Present (Corresponding_Body (Decl))
1791 and then
1792 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1793 = N_Subprogram_Renaming_Declaration
1794 then
1795 Build_And_Analyze_Renamed_Body
1796 (Decl, Corresponding_Body (Decl), After);
1797 end if;
1798 end if;
1800 elsif Ekind (E) in Task_Kind
1801 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1802 N_Single_Task_Declaration)
1803 then
1804 declare
1805 Ent : Entity_Id;
1807 begin
1808 Ent := First_Entity (E);
1809 while Present (Ent) loop
1810 if Is_Entry (Ent)
1811 and then not Default_Expressions_Processed (Ent)
1812 then
1813 Process_Default_Expressions (Ent, After);
1814 end if;
1816 Next_Entity (Ent);
1817 end loop;
1818 end;
1819 end if;
1821 -- Historical note: We used to create a finalization master for an
1822 -- access type whose designated type is not controlled, but contains
1823 -- private controlled compoments. This form of postprocessing is no
1824 -- longer needed because the finalization master is now created when
1825 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
1827 Next_Entity (E);
1828 end loop;
1829 end Freeze_All;
1831 -----------------------
1832 -- Freeze_And_Append --
1833 -----------------------
1835 procedure Freeze_And_Append
1836 (Ent : Entity_Id;
1837 N : Node_Id;
1838 Result : in out List_Id)
1840 L : constant List_Id := Freeze_Entity (Ent, N);
1841 begin
1842 if Is_Non_Empty_List (L) then
1843 if Result = No_List then
1844 Result := L;
1845 else
1846 Append_List (L, Result);
1847 end if;
1848 end if;
1849 end Freeze_And_Append;
1851 -------------------
1852 -- Freeze_Before --
1853 -------------------
1855 procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
1856 Freeze_Nodes : constant List_Id := Freeze_Entity (T, N);
1858 begin
1859 if Ekind (T) = E_Function then
1860 Check_Expression_Function (N, T);
1861 end if;
1863 if Is_Non_Empty_List (Freeze_Nodes) then
1864 Insert_Actions (N, Freeze_Nodes);
1865 end if;
1866 end Freeze_Before;
1868 -------------------
1869 -- Freeze_Entity --
1870 -------------------
1872 function Freeze_Entity (E : Entity_Id; N : Node_Id) return List_Id is
1873 GM : constant Ghost_Mode_Type := Ghost_Mode;
1874 -- Save the current Ghost mode in effect in case the entity being frozen
1875 -- sets a different mode.
1877 Loc : constant Source_Ptr := Sloc (N);
1878 Atype : Entity_Id;
1879 Comp : Entity_Id;
1880 F_Node : Node_Id;
1881 Formal : Entity_Id;
1882 Indx : Node_Id;
1884 Has_Default_Initialization : Boolean := False;
1885 -- This flag gets set to true for a variable with default initialization
1887 Late_Freezing : Boolean := False;
1888 -- Used to detect attempt to freeze function declared in another unit
1890 Result : List_Id := No_List;
1891 -- List of freezing actions, left at No_List if none
1893 Test_E : Entity_Id := E;
1894 -- This could use a comment ???
1896 procedure Add_To_Result (N : Node_Id);
1897 -- N is a freezing action to be appended to the Result
1899 function After_Last_Declaration return Boolean;
1900 -- If Loc is a freeze_entity that appears after the last declaration
1901 -- in the scope, inhibit error messages on late completion.
1903 procedure Check_Current_Instance (Comp_Decl : Node_Id);
1904 -- Check that an Access or Unchecked_Access attribute with a prefix
1905 -- which is the current instance type can only be applied when the type
1906 -- is limited.
1908 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
1909 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
1910 -- integer literal without an explicit corresponding size clause. The
1911 -- caller has checked that Utype is a modular integer type.
1913 procedure Freeze_Array_Type (Arr : Entity_Id);
1914 -- Freeze array type, including freezing index and component types
1916 procedure Freeze_Object_Declaration (E : Entity_Id);
1917 -- Perform checks and generate freeze node if needed for a constant or
1918 -- variable declared by an object declaration.
1920 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
1921 -- Create Freeze_Generic_Entity nodes for types declared in a generic
1922 -- package. Recurse on inner generic packages.
1924 function Freeze_Profile (E : Entity_Id) return Boolean;
1925 -- Freeze formals and return type of subprogram. If some type in the
1926 -- profile is a limited view, freezing of the entity will take place
1927 -- elsewhere, and the function returns False. This routine will be
1928 -- modified if and when we can implement AI05-019 efficiently ???
1930 procedure Freeze_Record_Type (Rec : Entity_Id);
1931 -- Freeze record type, including freezing component types, and freezing
1932 -- primitive operations if this is a tagged type.
1934 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
1935 -- Determine whether an arbitrary entity is subject to Boolean aspect
1936 -- Import and its value is specified as True.
1938 procedure Late_Freeze_Subprogram (E : Entity_Id);
1939 -- Following AI05-151, a function can return a limited view of a type
1940 -- declared elsewhere. In that case the function cannot be frozen at
1941 -- the end of its enclosing package. If its first use is in a different
1942 -- unit, it cannot be frozen there, but if the call is legal the full
1943 -- view of the return type is available and the subprogram can now be
1944 -- frozen. However the freeze node cannot be inserted at the point of
1945 -- call, but rather must go in the package holding the function, so that
1946 -- the backend can process it in the proper context.
1948 procedure Restore_Globals;
1949 -- Restore the values of all saved global variables
1951 procedure Wrap_Imported_Subprogram (E : Entity_Id);
1952 -- If E is an entity for an imported subprogram with pre/post-conditions
1953 -- then this procedure will create a wrapper to ensure that proper run-
1954 -- time checking of the pre/postconditions. See body for details.
1956 -------------------
1957 -- Add_To_Result --
1958 -------------------
1960 procedure Add_To_Result (N : Node_Id) is
1961 begin
1962 if No (Result) then
1963 Result := New_List (N);
1964 else
1965 Append (N, Result);
1966 end if;
1967 end Add_To_Result;
1969 ----------------------------
1970 -- After_Last_Declaration --
1971 ----------------------------
1973 function After_Last_Declaration return Boolean is
1974 Spec : constant Node_Id := Parent (Current_Scope);
1976 begin
1977 if Nkind (Spec) = N_Package_Specification then
1978 if Present (Private_Declarations (Spec)) then
1979 return Loc >= Sloc (Last (Private_Declarations (Spec)));
1980 elsif Present (Visible_Declarations (Spec)) then
1981 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
1982 else
1983 return False;
1984 end if;
1986 else
1987 return False;
1988 end if;
1989 end After_Last_Declaration;
1991 ----------------------------
1992 -- Check_Current_Instance --
1993 ----------------------------
1995 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
1997 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
1998 -- Determine whether Typ is compatible with the rules for aliased
1999 -- views of types as defined in RM 3.10 in the various dialects.
2001 function Process (N : Node_Id) return Traverse_Result;
2002 -- Process routine to apply check to given node
2004 -----------------------------
2005 -- Is_Aliased_View_Of_Type --
2006 -----------------------------
2008 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2009 Typ_Decl : constant Node_Id := Parent (Typ);
2011 begin
2012 -- Common case
2014 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2015 and then Limited_Present (Type_Definition (Typ_Decl))
2016 then
2017 return True;
2019 -- The following paragraphs describe what a legal aliased view of
2020 -- a type is in the various dialects of Ada.
2022 -- Ada 95
2024 -- The current instance of a limited type, and a formal parameter
2025 -- or generic formal object of a tagged type.
2027 -- Ada 95 limited type
2028 -- * Type with reserved word "limited"
2029 -- * A protected or task type
2030 -- * A composite type with limited component
2032 elsif Ada_Version <= Ada_95 then
2033 return Is_Limited_Type (Typ);
2035 -- Ada 2005
2037 -- The current instance of a limited tagged type, a protected
2038 -- type, a task type, or a type that has the reserved word
2039 -- "limited" in its full definition ... a formal parameter or
2040 -- generic formal object of a tagged type.
2042 -- Ada 2005 limited type
2043 -- * Type with reserved word "limited", "synchronized", "task"
2044 -- or "protected"
2045 -- * A composite type with limited component
2046 -- * A derived type whose parent is a non-interface limited type
2048 elsif Ada_Version = Ada_2005 then
2049 return
2050 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2051 or else
2052 (Is_Derived_Type (Typ)
2053 and then not Is_Interface (Etype (Typ))
2054 and then Is_Limited_Type (Etype (Typ)));
2056 -- Ada 2012 and beyond
2058 -- The current instance of an immutably limited type ... a formal
2059 -- parameter or generic formal object of a tagged type.
2061 -- Ada 2012 limited type
2062 -- * Type with reserved word "limited", "synchronized", "task"
2063 -- or "protected"
2064 -- * A composite type with limited component
2065 -- * A derived type whose parent is a non-interface limited type
2066 -- * An incomplete view
2068 -- Ada 2012 immutably limited type
2069 -- * Explicitly limited record type
2070 -- * Record extension with "limited" present
2071 -- * Non-formal limited private type that is either tagged
2072 -- or has at least one access discriminant with a default
2073 -- expression
2074 -- * Task type, protected type or synchronized interface
2075 -- * Type derived from immutably limited type
2077 else
2078 return
2079 Is_Immutably_Limited_Type (Typ)
2080 or else Is_Incomplete_Type (Typ);
2081 end if;
2082 end Is_Aliased_View_Of_Type;
2084 -------------
2085 -- Process --
2086 -------------
2088 function Process (N : Node_Id) return Traverse_Result is
2089 begin
2090 case Nkind (N) is
2091 when N_Attribute_Reference =>
2092 if Nam_In (Attribute_Name (N), Name_Access,
2093 Name_Unchecked_Access)
2094 and then Is_Entity_Name (Prefix (N))
2095 and then Is_Type (Entity (Prefix (N)))
2096 and then Entity (Prefix (N)) = E
2097 then
2098 if Ada_Version < Ada_2012 then
2099 Error_Msg_N
2100 ("current instance must be a limited type",
2101 Prefix (N));
2102 else
2103 Error_Msg_N
2104 ("current instance must be an immutably limited "
2105 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2106 end if;
2108 return Abandon;
2110 else
2111 return OK;
2112 end if;
2114 when others => return OK;
2115 end case;
2116 end Process;
2118 procedure Traverse is new Traverse_Proc (Process);
2120 -- Local variables
2122 Rec_Type : constant Entity_Id :=
2123 Scope (Defining_Identifier (Comp_Decl));
2125 -- Start of processing for Check_Current_Instance
2127 begin
2128 if not Is_Aliased_View_Of_Type (Rec_Type) then
2129 Traverse (Comp_Decl);
2130 end if;
2131 end Check_Current_Instance;
2133 ------------------------------
2134 -- Check_Suspicious_Modulus --
2135 ------------------------------
2137 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
2138 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
2140 begin
2141 if not Warn_On_Suspicious_Modulus_Value then
2142 return;
2143 end if;
2145 if Nkind (Decl) = N_Full_Type_Declaration then
2146 declare
2147 Tdef : constant Node_Id := Type_Definition (Decl);
2149 begin
2150 if Nkind (Tdef) = N_Modular_Type_Definition then
2151 declare
2152 Modulus : constant Node_Id :=
2153 Original_Node (Expression (Tdef));
2155 begin
2156 if Nkind (Modulus) = N_Integer_Literal then
2157 declare
2158 Modv : constant Uint := Intval (Modulus);
2159 Sizv : constant Uint := RM_Size (Utype);
2161 begin
2162 -- First case, modulus and size are the same. This
2163 -- happens if you have something like mod 32, with
2164 -- an explicit size of 32, this is for sure a case
2165 -- where the warning is given, since it is seems
2166 -- very unlikely that someone would want e.g. a
2167 -- five bit type stored in 32 bits. It is much
2168 -- more likely they wanted a 32-bit type.
2170 if Modv = Sizv then
2171 null;
2173 -- Second case, the modulus is 32 or 64 and no
2174 -- size clause is present. This is a less clear
2175 -- case for giving the warning, but in the case
2176 -- of 32/64 (5-bit or 6-bit types) these seem rare
2177 -- enough that it is a likely error (and in any
2178 -- case using 2**5 or 2**6 in these cases seems
2179 -- clearer. We don't include 8 or 16 here, simply
2180 -- because in practice 3-bit and 4-bit types are
2181 -- more common and too many false positives if
2182 -- we warn in these cases.
2184 elsif not Has_Size_Clause (Utype)
2185 and then (Modv = Uint_32 or else Modv = Uint_64)
2186 then
2187 null;
2189 -- No warning needed
2191 else
2192 return;
2193 end if;
2195 -- If we fall through, give warning
2197 Error_Msg_Uint_1 := Modv;
2198 Error_Msg_N
2199 ("?M?2 '*'*^' may have been intended here",
2200 Modulus);
2201 end;
2202 end if;
2203 end;
2204 end if;
2205 end;
2206 end if;
2207 end Check_Suspicious_Modulus;
2209 -----------------------
2210 -- Freeze_Array_Type --
2211 -----------------------
2213 procedure Freeze_Array_Type (Arr : Entity_Id) is
2214 FS : constant Entity_Id := First_Subtype (Arr);
2215 Ctyp : constant Entity_Id := Component_Type (Arr);
2216 Clause : Entity_Id;
2218 Non_Standard_Enum : Boolean := False;
2219 -- Set true if any of the index types is an enumeration type with a
2220 -- non-standard representation.
2222 begin
2223 Freeze_And_Append (Ctyp, N, Result);
2225 Indx := First_Index (Arr);
2226 while Present (Indx) loop
2227 Freeze_And_Append (Etype (Indx), N, Result);
2229 if Is_Enumeration_Type (Etype (Indx))
2230 and then Has_Non_Standard_Rep (Etype (Indx))
2231 then
2232 Non_Standard_Enum := True;
2233 end if;
2235 Next_Index (Indx);
2236 end loop;
2238 -- Processing that is done only for base types
2240 if Ekind (Arr) = E_Array_Type then
2242 -- Deal with default setting of reverse storage order
2244 Set_SSO_From_Default (Arr);
2246 -- Propagate flags for component type
2248 if Is_Controlled_Active (Component_Type (Arr))
2249 or else Has_Controlled_Component (Ctyp)
2250 then
2251 Set_Has_Controlled_Component (Arr);
2252 end if;
2254 if Has_Unchecked_Union (Component_Type (Arr)) then
2255 Set_Has_Unchecked_Union (Arr);
2256 end if;
2258 -- Warn for pragma Pack overriding foreign convention
2260 if Has_Foreign_Convention (Ctyp)
2261 and then Has_Pragma_Pack (Arr)
2262 then
2263 declare
2264 CN : constant Name_Id :=
2265 Get_Convention_Name (Convention (Ctyp));
2266 PP : constant Node_Id :=
2267 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
2268 begin
2269 if Present (PP) then
2270 Error_Msg_Name_1 := CN;
2271 Error_Msg_Sloc := Sloc (Arr);
2272 Error_Msg_N
2273 ("pragma Pack affects convention % components #??", PP);
2274 Error_Msg_Name_1 := CN;
2275 Error_Msg_N
2276 ("\array components may not have % compatible "
2277 & "representation??", PP);
2278 end if;
2279 end;
2280 end if;
2282 -- If packing was requested or if the component size was
2283 -- set explicitly, then see if bit packing is required. This
2284 -- processing is only done for base types, since all of the
2285 -- representation aspects involved are type-related.
2287 -- This is not just an optimization, if we start processing the
2288 -- subtypes, they interfere with the settings on the base type
2289 -- (this is because Is_Packed has a slightly different meaning
2290 -- before and after freezing).
2292 declare
2293 Csiz : Uint;
2294 Esiz : Uint;
2296 begin
2297 if (Is_Packed (Arr) or else Has_Pragma_Pack (Arr))
2298 and then Known_Static_RM_Size (Ctyp)
2299 and then not Has_Component_Size_Clause (Arr)
2300 then
2301 Csiz := UI_Max (RM_Size (Ctyp), 1);
2303 elsif Known_Component_Size (Arr) then
2304 Csiz := Component_Size (Arr);
2306 elsif not Known_Static_Esize (Ctyp) then
2307 Csiz := Uint_0;
2309 else
2310 Esiz := Esize (Ctyp);
2312 -- We can set the component size if it is less than 16,
2313 -- rounding it up to the next storage unit size.
2315 if Esiz <= 8 then
2316 Csiz := Uint_8;
2317 elsif Esiz <= 16 then
2318 Csiz := Uint_16;
2319 else
2320 Csiz := Uint_0;
2321 end if;
2323 -- Set component size up to match alignment if it would
2324 -- otherwise be less than the alignment. This deals with
2325 -- cases of types whose alignment exceeds their size (the
2326 -- padded type cases).
2328 if Csiz /= 0 then
2329 declare
2330 A : constant Uint := Alignment_In_Bits (Ctyp);
2331 begin
2332 if Csiz < A then
2333 Csiz := A;
2334 end if;
2335 end;
2336 end if;
2337 end if;
2339 -- Case of component size that may result in packing
2341 if 1 <= Csiz and then Csiz <= 64 then
2342 declare
2343 Ent : constant Entity_Id :=
2344 First_Subtype (Arr);
2345 Pack_Pragma : constant Node_Id :=
2346 Get_Rep_Pragma (Ent, Name_Pack);
2347 Comp_Size_C : constant Node_Id :=
2348 Get_Attribute_Definition_Clause
2349 (Ent, Attribute_Component_Size);
2351 begin
2352 -- Warn if we have pack and component size so that the
2353 -- pack is ignored.
2355 -- Note: here we must check for the presence of a
2356 -- component size before checking for a Pack pragma to
2357 -- deal with the case where the array type is a derived
2358 -- type whose parent is currently private.
2360 if Present (Comp_Size_C)
2361 and then Has_Pragma_Pack (Ent)
2362 and then Warn_On_Redundant_Constructs
2363 then
2364 Error_Msg_Sloc := Sloc (Comp_Size_C);
2365 Error_Msg_NE
2366 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
2367 Error_Msg_N
2368 ("\?r?explicit component size given#!", Pack_Pragma);
2369 Set_Is_Packed (Base_Type (Ent), False);
2370 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
2371 end if;
2373 -- Set component size if not already set by a component
2374 -- size clause.
2376 if not Present (Comp_Size_C) then
2377 Set_Component_Size (Arr, Csiz);
2378 end if;
2380 -- Check for base type of 8, 16, 32 bits, where an
2381 -- unsigned subtype has a length one less than the
2382 -- base type (e.g. Natural subtype of Integer).
2384 -- In such cases, if a component size was not set
2385 -- explicitly, then generate a warning.
2387 if Has_Pragma_Pack (Arr)
2388 and then not Present (Comp_Size_C)
2389 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2390 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2391 then
2392 Error_Msg_Uint_1 := Csiz;
2394 if Present (Pack_Pragma) then
2395 Error_Msg_N
2396 ("??pragma Pack causes component size to be ^!",
2397 Pack_Pragma);
2398 Error_Msg_N
2399 ("\??use Component_Size to set desired value!",
2400 Pack_Pragma);
2401 end if;
2402 end if;
2404 -- Actual packing is not needed for 8, 16, 32, 64. Also
2405 -- not needed for 24 if alignment is 1.
2407 if Csiz = 8
2408 or else Csiz = 16
2409 or else Csiz = 32
2410 or else Csiz = 64
2411 or else (Csiz = 24 and then Alignment (Ctyp) = 1)
2412 then
2413 -- Here the array was requested to be packed, but
2414 -- the packing request had no effect, so Is_Packed
2415 -- is reset.
2417 -- Note: semantically this means that we lose track
2418 -- of the fact that a derived type inherited a pragma
2419 -- Pack that was non- effective, but that seems fine.
2421 -- We regard a Pack pragma as a request to set a
2422 -- representation characteristic, and this request
2423 -- may be ignored.
2425 Set_Is_Packed (Base_Type (Arr), False);
2426 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2428 if Known_Static_Esize (Component_Type (Arr))
2429 and then Esize (Component_Type (Arr)) = Csiz
2430 then
2431 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
2432 end if;
2434 -- In all other cases, packing is indeed needed
2436 else
2437 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2438 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
2439 Set_Is_Packed (Base_Type (Arr), True);
2440 end if;
2441 end;
2442 end if;
2443 end;
2445 -- Check for Aliased or Atomic_Components/Atomic/VFA with
2446 -- unsuitable packing or explicit component size clause given.
2448 if (Has_Aliased_Components (Arr)
2449 or else Has_Atomic_Components (Arr)
2450 or else Is_Atomic_Or_VFA (Ctyp))
2451 and then
2452 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2453 then
2454 Alias_Atomic_Check : declare
2456 procedure Complain_CS (T : String);
2457 -- Outputs error messages for incorrect CS clause or pragma
2458 -- Pack for aliased or atomic/VFA components (T is "aliased"
2459 -- or "atomic/vfa");
2461 -----------------
2462 -- Complain_CS --
2463 -----------------
2465 procedure Complain_CS (T : String) is
2466 begin
2467 if Has_Component_Size_Clause (Arr) then
2468 Clause :=
2469 Get_Attribute_Definition_Clause
2470 (FS, Attribute_Component_Size);
2472 Error_Msg_N
2473 ("incorrect component size for "
2474 & T & " components", Clause);
2475 Error_Msg_Uint_1 := Esize (Ctyp);
2476 Error_Msg_N
2477 ("\only allowed value is^", Clause);
2479 else
2480 Error_Msg_N
2481 ("cannot pack " & T & " components",
2482 Get_Rep_Pragma (FS, Name_Pack));
2483 end if;
2484 end Complain_CS;
2486 -- Start of processing for Alias_Atomic_Check
2488 begin
2489 -- If object size of component type isn't known, we cannot
2490 -- be sure so we defer to the back end.
2492 if not Known_Static_Esize (Ctyp) then
2493 null;
2495 -- Case where component size has no effect. First check for
2496 -- object size of component type multiple of the storage
2497 -- unit size.
2499 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2501 -- OK in both packing case and component size case if RM
2502 -- size is known and static and same as the object size.
2504 and then
2505 ((Known_Static_RM_Size (Ctyp)
2506 and then Esize (Ctyp) = RM_Size (Ctyp))
2508 -- Or if we have an explicit component size clause and
2509 -- the component size and object size are equal.
2511 or else
2512 (Has_Component_Size_Clause (Arr)
2513 and then Component_Size (Arr) = Esize (Ctyp)))
2514 then
2515 null;
2517 elsif Has_Aliased_Components (Arr) then
2518 Complain_CS ("aliased");
2520 elsif Has_Atomic_Components (Arr)
2521 or else Is_Atomic (Ctyp)
2522 then
2523 Complain_CS ("atomic");
2525 elsif Is_Volatile_Full_Access (Ctyp) then
2526 Complain_CS ("volatile full access");
2527 end if;
2528 end Alias_Atomic_Check;
2529 end if;
2531 -- Check for Independent_Components/Independent with unsuitable
2532 -- packing or explicit component size clause given.
2534 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
2535 and then
2536 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2537 then
2538 begin
2539 -- If object size of component type isn't known, we cannot
2540 -- be sure so we defer to the back end.
2542 if not Known_Static_Esize (Ctyp) then
2543 null;
2545 -- Case where component size has no effect. First check for
2546 -- object size of component type multiple of the storage
2547 -- unit size.
2549 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2551 -- OK in both packing case and component size case if RM
2552 -- size is known and multiple of the storage unit size.
2554 and then
2555 ((Known_Static_RM_Size (Ctyp)
2556 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
2558 -- Or if we have an explicit component size clause and
2559 -- the component size is larger than the object size.
2561 or else
2562 (Has_Component_Size_Clause (Arr)
2563 and then Component_Size (Arr) >= Esize (Ctyp)))
2564 then
2565 null;
2567 else
2568 if Has_Component_Size_Clause (Arr) then
2569 Clause :=
2570 Get_Attribute_Definition_Clause
2571 (FS, Attribute_Component_Size);
2573 Error_Msg_N
2574 ("incorrect component size for "
2575 & "independent components", Clause);
2576 Error_Msg_Uint_1 := Esize (Ctyp);
2577 Error_Msg_N
2578 ("\minimum allowed is^", Clause);
2580 else
2581 Error_Msg_N
2582 ("cannot pack independent components",
2583 Get_Rep_Pragma (FS, Name_Pack));
2584 end if;
2585 end if;
2586 end;
2587 end if;
2589 -- Warn for case of atomic type
2591 Clause := Get_Rep_Pragma (FS, Name_Atomic);
2593 if Present (Clause)
2594 and then not Addressable (Component_Size (FS))
2595 then
2596 Error_Msg_NE
2597 ("non-atomic components of type& may not be "
2598 & "accessible by separate tasks??", Clause, Arr);
2600 if Has_Component_Size_Clause (Arr) then
2601 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2602 (FS, Attribute_Component_Size));
2603 Error_Msg_N ("\because of component size clause#??", Clause);
2605 elsif Has_Pragma_Pack (Arr) then
2606 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2607 Error_Msg_N ("\because of pragma Pack#??", Clause);
2608 end if;
2609 end if;
2611 -- Check for scalar storage order
2613 declare
2614 Dummy : Boolean;
2615 begin
2616 Check_Component_Storage_Order
2617 (Encl_Type => Arr,
2618 Comp => Empty,
2619 ADC => Get_Attribute_Definition_Clause
2620 (First_Subtype (Arr),
2621 Attribute_Scalar_Storage_Order),
2622 Comp_ADC_Present => Dummy);
2623 end;
2625 -- Processing that is done only for subtypes
2627 else
2628 -- Acquire alignment from base type
2630 if Unknown_Alignment (Arr) then
2631 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
2632 Adjust_Esize_Alignment (Arr);
2633 end if;
2634 end if;
2636 -- Specific checks for bit-packed arrays
2638 if Is_Bit_Packed_Array (Arr) then
2640 -- Check number of elements for bit packed arrays that come from
2641 -- source and have compile time known ranges. The bit-packed
2642 -- arrays circuitry does not support arrays with more than
2643 -- Integer'Last + 1 elements, and when this restriction is
2644 -- violated, causes incorrect data access.
2646 -- For the case where this is not compile time known, a run-time
2647 -- check should be generated???
2649 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
2650 declare
2651 Elmts : Uint;
2652 Index : Node_Id;
2653 Ilen : Node_Id;
2654 Ityp : Entity_Id;
2656 begin
2657 Elmts := Uint_1;
2658 Index := First_Index (Arr);
2659 while Present (Index) loop
2660 Ityp := Etype (Index);
2662 -- Never generate an error if any index is of a generic
2663 -- type. We will check this in instances.
2665 if Is_Generic_Type (Ityp) then
2666 Elmts := Uint_0;
2667 exit;
2668 end if;
2670 Ilen :=
2671 Make_Attribute_Reference (Loc,
2672 Prefix => New_Occurrence_Of (Ityp, Loc),
2673 Attribute_Name => Name_Range_Length);
2674 Analyze_And_Resolve (Ilen);
2676 -- No attempt is made to check number of elements if not
2677 -- compile time known.
2679 if Nkind (Ilen) /= N_Integer_Literal then
2680 Elmts := Uint_0;
2681 exit;
2682 end if;
2684 Elmts := Elmts * Intval (Ilen);
2685 Next_Index (Index);
2686 end loop;
2688 if Elmts > Intval (High_Bound
2689 (Scalar_Range (Standard_Integer))) + 1
2690 then
2691 Error_Msg_N
2692 ("bit packed array type may not have "
2693 & "more than Integer''Last+1 elements", Arr);
2694 end if;
2695 end;
2696 end if;
2698 -- Check size
2700 if Known_RM_Size (Arr) then
2701 declare
2702 SizC : constant Node_Id := Size_Clause (Arr);
2703 Discard : Boolean;
2705 begin
2706 -- It is not clear if it is possible to have no size clause
2707 -- at this stage, but it is not worth worrying about. Post
2708 -- error on the entity name in the size clause if present,
2709 -- else on the type entity itself.
2711 if Present (SizC) then
2712 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
2713 else
2714 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
2715 end if;
2716 end;
2717 end if;
2718 end if;
2720 -- If any of the index types was an enumeration type with a non-
2721 -- standard rep clause, then we indicate that the array type is
2722 -- always packed (even if it is not bit packed).
2724 if Non_Standard_Enum then
2725 Set_Has_Non_Standard_Rep (Base_Type (Arr));
2726 Set_Is_Packed (Base_Type (Arr));
2727 end if;
2729 Set_Component_Alignment_If_Not_Set (Arr);
2731 -- If the array is packed, we must create the packed array type to be
2732 -- used to actually implement the type. This is only needed for real
2733 -- array types (not for string literal types, since they are present
2734 -- only for the front end).
2736 if Is_Packed (Arr)
2737 and then Ekind (Arr) /= E_String_Literal_Subtype
2738 then
2739 Create_Packed_Array_Impl_Type (Arr);
2740 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
2742 -- Make sure that we have the necessary routines to implement the
2743 -- packing, and complain now if not. Note that we only test this
2744 -- for constrained array types.
2746 if Is_Constrained (Arr)
2747 and then Is_Bit_Packed_Array (Arr)
2748 and then Present (Packed_Array_Impl_Type (Arr))
2749 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
2750 then
2751 declare
2752 CS : constant Uint := Component_Size (Arr);
2753 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
2755 begin
2756 if RE /= RE_Null
2757 and then not RTE_Available (RE)
2758 then
2759 Error_Msg_CRT
2760 ("packing of " & UI_Image (CS) & "-bit components",
2761 First_Subtype (Etype (Arr)));
2763 -- Cancel the packing
2765 Set_Is_Packed (Base_Type (Arr), False);
2766 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2767 Set_Packed_Array_Impl_Type (Arr, Empty);
2768 goto Skip_Packed;
2769 end if;
2770 end;
2771 end if;
2773 -- Size information of packed array type is copied to the array
2774 -- type, since this is really the representation. But do not
2775 -- override explicit existing size values. If the ancestor subtype
2776 -- is constrained the Packed_Array_Impl_Type will be inherited
2777 -- from it, but the size may have been provided already, and
2778 -- must not be overridden either.
2780 if not Has_Size_Clause (Arr)
2781 and then
2782 (No (Ancestor_Subtype (Arr))
2783 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
2784 then
2785 Set_Esize (Arr, Esize (Packed_Array_Impl_Type (Arr)));
2786 Set_RM_Size (Arr, RM_Size (Packed_Array_Impl_Type (Arr)));
2787 end if;
2789 if not Has_Alignment_Clause (Arr) then
2790 Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
2791 end if;
2792 end if;
2794 <<Skip_Packed>>
2796 -- For non-packed arrays set the alignment of the array to the
2797 -- alignment of the component type if it is unknown. Skip this
2798 -- in atomic/VFA case (atomic/VFA arrays may need larger alignments).
2800 if not Is_Packed (Arr)
2801 and then Unknown_Alignment (Arr)
2802 and then Known_Alignment (Ctyp)
2803 and then Known_Static_Component_Size (Arr)
2804 and then Known_Static_Esize (Ctyp)
2805 and then Esize (Ctyp) = Component_Size (Arr)
2806 and then not Is_Atomic_Or_VFA (Arr)
2807 then
2808 Set_Alignment (Arr, Alignment (Component_Type (Arr)));
2809 end if;
2810 end Freeze_Array_Type;
2812 -------------------------------
2813 -- Freeze_Object_Declaration --
2814 -------------------------------
2816 procedure Freeze_Object_Declaration (E : Entity_Id) is
2817 begin
2818 -- Abstract type allowed only for C++ imported variables or constants
2820 -- Note: we inhibit this check for objects that do not come from
2821 -- source because there is at least one case (the expansion of
2822 -- x'Class'Input where x is abstract) where we legitimately
2823 -- generate an abstract object.
2825 if Is_Abstract_Type (Etype (E))
2826 and then Comes_From_Source (Parent (E))
2827 and then not (Is_Imported (E) and then Is_CPP_Class (Etype (E)))
2828 then
2829 Error_Msg_N ("type of object cannot be abstract",
2830 Object_Definition (Parent (E)));
2832 if Is_CPP_Class (Etype (E)) then
2833 Error_Msg_NE
2834 ("\} may need a cpp_constructor",
2835 Object_Definition (Parent (E)), Etype (E));
2837 elsif Present (Expression (Parent (E))) then
2838 Error_Msg_N -- CODEFIX
2839 ("\maybe a class-wide type was meant",
2840 Object_Definition (Parent (E)));
2841 end if;
2842 end if;
2844 -- For object created by object declaration, perform required
2845 -- categorization (preelaborate and pure) checks. Defer these
2846 -- checks to freeze time since pragma Import inhibits default
2847 -- initialization and thus pragma Import affects these checks.
2849 Validate_Object_Declaration (Declaration_Node (E));
2851 -- If there is an address clause, check that it is valid
2852 -- and if need be move initialization to the freeze node.
2854 Check_Address_Clause (E);
2856 -- Similar processing is needed for aspects that may affect
2857 -- object layout, like Alignment, if there is an initialization
2858 -- expression.
2860 if Has_Delayed_Aspects (E)
2861 and then Expander_Active
2862 and then Is_Array_Type (Etype (E))
2863 and then Present (Expression (Parent (E)))
2864 then
2865 declare
2866 Decl : constant Node_Id := Parent (E);
2867 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
2869 begin
2871 -- Capture initialization value at point of declaration, and
2872 -- make explicit assignment legal, because object may be a
2873 -- constant.
2875 Remove_Side_Effects (Expression (Decl));
2876 Set_Assignment_OK (Lhs);
2878 -- Move initialization to freeze actions.
2880 Append_Freeze_Action (E,
2881 Make_Assignment_Statement (Loc,
2882 Name => Lhs,
2883 Expression => Expression (Decl)));
2885 Set_No_Initialization (Decl);
2886 -- Set_Is_Frozen (E, False);
2887 end;
2888 end if;
2890 -- Reset Is_True_Constant for non-constant aliased object. We
2891 -- consider that the fact that a non-constant object is aliased may
2892 -- indicate that some funny business is going on, e.g. an aliased
2893 -- object is passed by reference to a procedure which captures the
2894 -- address of the object, which is later used to assign a new value,
2895 -- even though the compiler thinks that it is not modified. Such
2896 -- code is highly dubious, but we choose to make it "work" for
2897 -- non-constant aliased objects.
2899 -- Note that we used to do this for all aliased objects, whether or
2900 -- not constant, but this caused anomalies down the line because we
2901 -- ended up with static objects that were not Is_True_Constant. Not
2902 -- resetting Is_True_Constant for (aliased) constant objects ensures
2903 -- that this anomaly never occurs.
2905 -- However, we don't do that for internal entities. We figure that if
2906 -- we deliberately set Is_True_Constant for an internal entity, e.g.
2907 -- a dispatch table entry, then we mean it.
2909 if Ekind (E) /= E_Constant
2910 and then (Is_Aliased (E) or else Is_Aliased (Etype (E)))
2911 and then not Is_Internal_Name (Chars (E))
2912 then
2913 Set_Is_True_Constant (E, False);
2914 end if;
2916 -- If the object needs any kind of default initialization, an error
2917 -- must be issued if No_Default_Initialization applies. The check
2918 -- doesn't apply to imported objects, which are not ever default
2919 -- initialized, and is why the check is deferred until freezing, at
2920 -- which point we know if Import applies. Deferred constants are also
2921 -- exempted from this test because their completion is explicit, or
2922 -- through an import pragma.
2924 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
2925 null;
2927 elsif Comes_From_Source (E)
2928 and then not Is_Imported (E)
2929 and then not Has_Init_Expression (Declaration_Node (E))
2930 and then
2931 ((Has_Non_Null_Base_Init_Proc (Etype (E))
2932 and then not No_Initialization (Declaration_Node (E))
2933 and then not Is_Value_Type (Etype (E))
2934 and then not Initialization_Suppressed (Etype (E)))
2935 or else
2936 (Needs_Simple_Initialization (Etype (E))
2937 and then not Is_Internal (E)))
2938 then
2939 Has_Default_Initialization := True;
2940 Check_Restriction
2941 (No_Default_Initialization, Declaration_Node (E));
2942 end if;
2944 -- Check that a Thread_Local_Storage variable does not have
2945 -- default initialization, and any explicit initialization must
2946 -- either be the null constant or a static constant.
2948 if Has_Pragma_Thread_Local_Storage (E) then
2949 declare
2950 Decl : constant Node_Id := Declaration_Node (E);
2951 begin
2952 if Has_Default_Initialization
2953 or else
2954 (Has_Init_Expression (Decl)
2955 and then
2956 (No (Expression (Decl))
2957 or else not
2958 (Is_OK_Static_Expression (Expression (Decl))
2959 or else Nkind (Expression (Decl)) = N_Null)))
2960 then
2961 Error_Msg_NE
2962 ("Thread_Local_Storage variable& is "
2963 & "improperly initialized", Decl, E);
2964 Error_Msg_NE
2965 ("\only allowed initialization is explicit "
2966 & "NULL or static expression", Decl, E);
2967 end if;
2968 end;
2969 end if;
2971 -- For imported objects, set Is_Public unless there is also an
2972 -- address clause, which means that there is no external symbol
2973 -- needed for the Import (Is_Public may still be set for other
2974 -- unrelated reasons). Note that we delayed this processing
2975 -- till freeze time so that we can be sure not to set the flag
2976 -- if there is an address clause. If there is such a clause,
2977 -- then the only purpose of the Import pragma is to suppress
2978 -- implicit initialization.
2980 if Is_Imported (E) and then No (Address_Clause (E)) then
2981 Set_Is_Public (E);
2982 end if;
2984 -- For source objects that are not Imported and are library
2985 -- level, if no linker section pragma was given inherit the
2986 -- appropriate linker section from the corresponding type.
2988 if Comes_From_Source (E)
2989 and then not Is_Imported (E)
2990 and then Is_Library_Level_Entity (E)
2991 and then No (Linker_Section_Pragma (E))
2992 then
2993 Set_Linker_Section_Pragma
2994 (E, Linker_Section_Pragma (Etype (E)));
2995 end if;
2997 -- For convention C objects of an enumeration type, warn if the
2998 -- size is not integer size and no explicit size given. Skip
2999 -- warning for Boolean, and Character, assume programmer expects
3000 -- 8-bit sizes for these cases.
3002 if (Convention (E) = Convention_C
3003 or else
3004 Convention (E) = Convention_CPP)
3005 and then Is_Enumeration_Type (Etype (E))
3006 and then not Is_Character_Type (Etype (E))
3007 and then not Is_Boolean_Type (Etype (E))
3008 and then Esize (Etype (E)) < Standard_Integer_Size
3009 and then not Has_Size_Clause (E)
3010 then
3011 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
3012 Error_Msg_N
3013 ("??convention C enumeration object has size less than ^", E);
3014 Error_Msg_N ("\??use explicit size clause to set size", E);
3015 end if;
3016 end Freeze_Object_Declaration;
3018 -----------------------------
3019 -- Freeze_Generic_Entities --
3020 -----------------------------
3022 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
3023 E : Entity_Id;
3024 F : Node_Id;
3025 Flist : List_Id;
3027 begin
3028 Flist := New_List;
3029 E := First_Entity (Pack);
3030 while Present (E) loop
3031 if Is_Type (E) and then not Is_Generic_Type (E) then
3032 F := Make_Freeze_Generic_Entity (Sloc (Pack));
3033 Set_Entity (F, E);
3034 Append_To (Flist, F);
3036 elsif Ekind (E) = E_Generic_Package then
3037 Append_List_To (Flist, Freeze_Generic_Entities (E));
3038 end if;
3040 Next_Entity (E);
3041 end loop;
3043 return Flist;
3044 end Freeze_Generic_Entities;
3046 --------------------
3047 -- Freeze_Profile --
3048 --------------------
3050 function Freeze_Profile (E : Entity_Id) return Boolean is
3051 F_Type : Entity_Id;
3052 R_Type : Entity_Id;
3053 Warn_Node : Node_Id;
3055 begin
3056 -- Loop through formals
3058 Formal := First_Formal (E);
3059 while Present (Formal) loop
3060 F_Type := Etype (Formal);
3062 -- AI05-0151: incomplete types can appear in a profile. By the
3063 -- time the entity is frozen, the full view must be available,
3064 -- unless it is a limited view.
3066 if Is_Incomplete_Type (F_Type)
3067 and then Present (Full_View (F_Type))
3068 and then not From_Limited_With (F_Type)
3069 then
3070 F_Type := Full_View (F_Type);
3071 Set_Etype (Formal, F_Type);
3072 end if;
3074 if not From_Limited_With (F_Type) then
3075 Freeze_And_Append (F_Type, N, Result);
3076 end if;
3078 if Is_Private_Type (F_Type)
3079 and then Is_Private_Type (Base_Type (F_Type))
3080 and then No (Full_View (Base_Type (F_Type)))
3081 and then not Is_Generic_Type (F_Type)
3082 and then not Is_Derived_Type (F_Type)
3083 then
3084 -- If the type of a formal is incomplete, subprogram is being
3085 -- frozen prematurely. Within an instance (but not within a
3086 -- wrapper package) this is an artifact of our need to regard
3087 -- the end of an instantiation as a freeze point. Otherwise it
3088 -- is a definite error.
3090 if In_Instance then
3091 Set_Is_Frozen (E, False);
3092 Result := No_List;
3093 return False;
3095 elsif not After_Last_Declaration
3096 and then not Freezing_Library_Level_Tagged_Type
3097 then
3098 Error_Msg_Node_1 := F_Type;
3099 Error_Msg
3100 ("type & must be fully defined before this point", Loc);
3101 end if;
3102 end if;
3104 -- Check suspicious parameter for C function. These tests apply
3105 -- only to exported/imported subprograms.
3107 if Warn_On_Export_Import
3108 and then Comes_From_Source (E)
3109 and then (Convention (E) = Convention_C
3110 or else
3111 Convention (E) = Convention_CPP)
3112 and then (Is_Imported (E) or else Is_Exported (E))
3113 and then Convention (E) /= Convention (Formal)
3114 and then not Has_Warnings_Off (E)
3115 and then not Has_Warnings_Off (F_Type)
3116 and then not Has_Warnings_Off (Formal)
3117 then
3118 -- Qualify mention of formals with subprogram name
3120 Error_Msg_Qual_Level := 1;
3122 -- Check suspicious use of fat C pointer
3124 if Is_Access_Type (F_Type)
3125 and then Esize (F_Type) > Ttypes.System_Address_Size
3126 then
3127 Error_Msg_N
3128 ("?x?type of & does not correspond to C pointer!", Formal);
3130 -- Check suspicious return of boolean
3132 elsif Root_Type (F_Type) = Standard_Boolean
3133 and then Convention (F_Type) = Convention_Ada
3134 and then not Has_Warnings_Off (F_Type)
3135 and then not Has_Size_Clause (F_Type)
3136 and then VM_Target = No_VM
3137 then
3138 Error_Msg_N
3139 ("& is an 8-bit Ada Boolean?x?", Formal);
3140 Error_Msg_N
3141 ("\use appropriate corresponding type in C "
3142 & "(e.g. char)?x?", Formal);
3144 -- Check suspicious tagged type
3146 elsif (Is_Tagged_Type (F_Type)
3147 or else
3148 (Is_Access_Type (F_Type)
3149 and then Is_Tagged_Type (Designated_Type (F_Type))))
3150 and then Convention (E) = Convention_C
3151 then
3152 Error_Msg_N
3153 ("?x?& involves a tagged type which does not "
3154 & "correspond to any C type!", Formal);
3156 -- Check wrong convention subprogram pointer
3158 elsif Ekind (F_Type) = E_Access_Subprogram_Type
3159 and then not Has_Foreign_Convention (F_Type)
3160 then
3161 Error_Msg_N
3162 ("?x?subprogram pointer & should "
3163 & "have foreign convention!", Formal);
3164 Error_Msg_Sloc := Sloc (F_Type);
3165 Error_Msg_NE
3166 ("\?x?add Convention pragma to declaration of &#",
3167 Formal, F_Type);
3168 end if;
3170 -- Turn off name qualification after message output
3172 Error_Msg_Qual_Level := 0;
3173 end if;
3175 -- Check for unconstrained array in exported foreign convention
3176 -- case.
3178 if Has_Foreign_Convention (E)
3179 and then not Is_Imported (E)
3180 and then Is_Array_Type (F_Type)
3181 and then not Is_Constrained (F_Type)
3182 and then Warn_On_Export_Import
3184 -- Exclude VM case, since both .NET and JVM can handle
3185 -- unconstrained arrays without a problem.
3187 and then VM_Target = No_VM
3188 then
3189 Error_Msg_Qual_Level := 1;
3191 -- If this is an inherited operation, place the warning on
3192 -- the derived type declaration, rather than on the original
3193 -- subprogram.
3195 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
3196 then
3197 Warn_Node := Parent (E);
3199 if Formal = First_Formal (E) then
3200 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
3201 end if;
3202 else
3203 Warn_Node := Formal;
3204 end if;
3206 Error_Msg_NE ("?x?type of argument& is unconstrained array",
3207 Warn_Node, Formal);
3208 Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
3209 Warn_Node, Formal);
3210 Error_Msg_Qual_Level := 0;
3211 end if;
3213 if not From_Limited_With (F_Type) then
3214 if Is_Access_Type (F_Type) then
3215 F_Type := Designated_Type (F_Type);
3216 end if;
3218 -- If the formal is an anonymous_access_to_subprogram
3219 -- freeze the subprogram type as well, to prevent
3220 -- scope anomalies in gigi, because there is no other
3221 -- clear point at which it could be frozen.
3223 if Is_Itype (Etype (Formal))
3224 and then Ekind (F_Type) = E_Subprogram_Type
3225 then
3226 Freeze_And_Append (F_Type, N, Result);
3227 end if;
3228 end if;
3230 Next_Formal (Formal);
3231 end loop;
3233 -- Case of function: similar checks on return type
3235 if Ekind (E) = E_Function then
3237 -- Check whether function is declared elsewhere.
3239 Late_Freezing :=
3240 Get_Source_Unit (E) /= Get_Source_Unit (N)
3241 and then Returns_Limited_View (E)
3242 and then not In_Open_Scopes (Scope (E));
3244 -- Freeze return type
3246 R_Type := Etype (E);
3248 -- AI05-0151: the return type may have been incomplete
3249 -- at the point of declaration. Replace it with the full
3250 -- view, unless the current type is a limited view. In
3251 -- that case the full view is in a different unit, and
3252 -- gigi finds the non-limited view after the other unit
3253 -- is elaborated.
3255 if Ekind (R_Type) = E_Incomplete_Type
3256 and then Present (Full_View (R_Type))
3257 and then not From_Limited_With (R_Type)
3258 then
3259 R_Type := Full_View (R_Type);
3260 Set_Etype (E, R_Type);
3262 -- If the return type is a limited view and the non-limited
3263 -- view is still incomplete, the function has to be frozen at a
3264 -- later time. If the function is abstract there is no place at
3265 -- which the full view will become available, and no code to be
3266 -- generated for it, so mark type as frozen.
3268 elsif Ekind (R_Type) = E_Incomplete_Type
3269 and then From_Limited_With (R_Type)
3270 and then Ekind (Non_Limited_View (R_Type)) = E_Incomplete_Type
3271 then
3272 if Is_Abstract_Subprogram (E) then
3273 null;
3274 else
3275 Set_Is_Frozen (E, False);
3276 Set_Returns_Limited_View (E);
3277 return False;
3278 end if;
3279 end if;
3281 Freeze_And_Append (R_Type, N, Result);
3283 -- Check suspicious return type for C function
3285 if Warn_On_Export_Import
3286 and then (Convention (E) = Convention_C
3287 or else
3288 Convention (E) = Convention_CPP)
3289 and then (Is_Imported (E) or else Is_Exported (E))
3290 then
3291 -- Check suspicious return of fat C pointer
3293 if Is_Access_Type (R_Type)
3294 and then Esize (R_Type) > Ttypes.System_Address_Size
3295 and then not Has_Warnings_Off (E)
3296 and then not Has_Warnings_Off (R_Type)
3297 then
3298 Error_Msg_N ("?x?return type of& does not "
3299 & "correspond to C pointer!", E);
3301 -- Check suspicious return of boolean
3303 elsif Root_Type (R_Type) = Standard_Boolean
3304 and then Convention (R_Type) = Convention_Ada
3305 and then VM_Target = No_VM
3306 and then not Has_Warnings_Off (E)
3307 and then not Has_Warnings_Off (R_Type)
3308 and then not Has_Size_Clause (R_Type)
3309 then
3310 declare
3311 N : constant Node_Id :=
3312 Result_Definition (Declaration_Node (E));
3313 begin
3314 Error_Msg_NE
3315 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
3316 Error_Msg_NE
3317 ("\use appropriate corresponding type in C "
3318 & "(e.g. char)?x?", N, E);
3319 end;
3321 -- Check suspicious return tagged type
3323 elsif (Is_Tagged_Type (R_Type)
3324 or else (Is_Access_Type (R_Type)
3325 and then
3326 Is_Tagged_Type
3327 (Designated_Type (R_Type))))
3328 and then Convention (E) = Convention_C
3329 and then not Has_Warnings_Off (E)
3330 and then not Has_Warnings_Off (R_Type)
3331 then
3332 Error_Msg_N ("?x?return type of & does not "
3333 & "correspond to C type!", E);
3335 -- Check return of wrong convention subprogram pointer
3337 elsif Ekind (R_Type) = E_Access_Subprogram_Type
3338 and then not Has_Foreign_Convention (R_Type)
3339 and then not Has_Warnings_Off (E)
3340 and then not Has_Warnings_Off (R_Type)
3341 then
3342 Error_Msg_N ("?x?& should return a foreign "
3343 & "convention subprogram pointer", E);
3344 Error_Msg_Sloc := Sloc (R_Type);
3345 Error_Msg_NE
3346 ("\?x?add Convention pragma to declaration of& #",
3347 E, R_Type);
3348 end if;
3349 end if;
3351 -- Give warning for suspicious return of a result of an
3352 -- unconstrained array type in a foreign convention function.
3354 if Has_Foreign_Convention (E)
3356 -- We are looking for a return of unconstrained array
3358 and then Is_Array_Type (R_Type)
3359 and then not Is_Constrained (R_Type)
3361 -- Exclude imported routines, the warning does not belong on
3362 -- the import, but rather on the routine definition.
3364 and then not Is_Imported (E)
3366 -- Exclude VM case, since both .NET and JVM can handle return
3367 -- of unconstrained arrays without a problem.
3369 and then VM_Target = No_VM
3371 -- Check that general warning is enabled, and that it is not
3372 -- suppressed for this particular case.
3374 and then Warn_On_Export_Import
3375 and then not Has_Warnings_Off (E)
3376 and then not Has_Warnings_Off (R_Type)
3377 then
3378 Error_Msg_N ("?x?foreign convention function& should not " &
3379 "return unconstrained array!", E);
3380 end if;
3381 end if;
3383 -- Check suspicious use of Import in pure unit (cases where the RM
3384 -- allows calls to be omitted).
3386 if Is_Imported (E)
3388 -- It might be suspicious if the compilation unit has the Pure
3389 -- aspect/pragma.
3391 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
3393 -- The RM allows omission of calls only in the case of
3394 -- library-level subprograms (see RM-10.2.1(18)).
3396 and then Is_Library_Level_Entity (E)
3398 -- Ignore internally generated entity. This happens in some cases
3399 -- of subprograms in specs, where we generate an implied body.
3401 and then Comes_From_Source (Import_Pragma (E))
3403 -- Assume run-time knows what it is doing
3405 and then not GNAT_Mode
3407 -- Assume explicit Pure_Function means import is pure
3409 and then not Has_Pragma_Pure_Function (E)
3411 -- Don't need warning in relaxed semantics mode
3413 and then not Relaxed_RM_Semantics
3415 -- Assume convention Intrinsic is OK, since this is specialized.
3416 -- This deals with the DEC unit current_exception.ads
3418 and then Convention (E) /= Convention_Intrinsic
3420 -- Assume that ASM interface knows what it is doing. This deals
3421 -- with unsigned.ads in the AAMP back end.
3423 and then Convention (E) /= Convention_Assembler
3424 then
3425 Error_Msg_N
3426 ("pragma Import in Pure unit??", Import_Pragma (E));
3427 Error_Msg_NE
3428 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
3429 Import_Pragma (E), E);
3430 end if;
3432 return True;
3433 end Freeze_Profile;
3435 ------------------------
3436 -- Freeze_Record_Type --
3437 ------------------------
3439 procedure Freeze_Record_Type (Rec : Entity_Id) is
3440 ADC : Node_Id;
3441 Comp : Entity_Id;
3442 IR : Node_Id;
3443 Prev : Entity_Id;
3445 Junk : Boolean;
3446 pragma Warnings (Off, Junk);
3448 Rec_Pushed : Boolean := False;
3449 -- Set True if the record type scope Rec has been pushed on the scope
3450 -- stack. Needed for the analysis of delayed aspects specified to the
3451 -- components of Rec.
3453 SSO_ADC : Node_Id;
3454 -- Scalar_Storage_Order attribute definition clause for the record
3456 Unplaced_Component : Boolean := False;
3457 -- Set True if we find at least one component with no component
3458 -- clause (used to warn about useless Pack pragmas).
3460 Placed_Component : Boolean := False;
3461 -- Set True if we find at least one component with a component
3462 -- clause (used to warn about useless Bit_Order pragmas, and also
3463 -- to detect cases where Implicit_Packing may have an effect).
3465 Aliased_Component : Boolean := False;
3466 -- Set True if we find at least one component which is aliased. This
3467 -- is used to prevent Implicit_Packing of the record, since packing
3468 -- cannot modify the size of alignment of an aliased component.
3470 SSO_ADC_Component : Boolean := False;
3471 -- Set True if we find at least one component whose type has a
3472 -- Scalar_Storage_Order attribute definition clause.
3474 All_Scalar_Components : Boolean := True;
3475 -- Set False if we encounter a component of a non-scalar type
3477 Scalar_Component_Total_RM_Size : Uint := Uint_0;
3478 Scalar_Component_Total_Esize : Uint := Uint_0;
3479 -- Accumulates total RM_Size values and total Esize values of all
3480 -- scalar components. Used for processing of Implicit_Packing.
3482 function Check_Allocator (N : Node_Id) return Node_Id;
3483 -- If N is an allocator, possibly wrapped in one or more level of
3484 -- qualified expression(s), return the inner allocator node, else
3485 -- return Empty.
3487 procedure Check_Itype (Typ : Entity_Id);
3488 -- If the component subtype is an access to a constrained subtype of
3489 -- an already frozen type, make the subtype frozen as well. It might
3490 -- otherwise be frozen in the wrong scope, and a freeze node on
3491 -- subtype has no effect. Similarly, if the component subtype is a
3492 -- regular (not protected) access to subprogram, set the anonymous
3493 -- subprogram type to frozen as well, to prevent an out-of-scope
3494 -- freeze node at some eventual point of call. Protected operations
3495 -- are handled elsewhere.
3497 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3498 -- Make sure that all types mentioned in Discrete_Choices of the
3499 -- variants referenceed by the Variant_Part VP are frozen. This is
3500 -- a recursive routine to deal with nested variants.
3502 ---------------------
3503 -- Check_Allocator --
3504 ---------------------
3506 function Check_Allocator (N : Node_Id) return Node_Id is
3507 Inner : Node_Id;
3508 begin
3509 Inner := N;
3510 loop
3511 if Nkind (Inner) = N_Allocator then
3512 return Inner;
3513 elsif Nkind (Inner) = N_Qualified_Expression then
3514 Inner := Expression (Inner);
3515 else
3516 return Empty;
3517 end if;
3518 end loop;
3519 end Check_Allocator;
3521 -----------------
3522 -- Check_Itype --
3523 -----------------
3525 procedure Check_Itype (Typ : Entity_Id) is
3526 Desig : constant Entity_Id := Designated_Type (Typ);
3528 begin
3529 if not Is_Frozen (Desig)
3530 and then Is_Frozen (Base_Type (Desig))
3531 then
3532 Set_Is_Frozen (Desig);
3534 -- In addition, add an Itype_Reference to ensure that the
3535 -- access subtype is elaborated early enough. This cannot be
3536 -- done if the subtype may depend on discriminants.
3538 if Ekind (Comp) = E_Component
3539 and then Is_Itype (Etype (Comp))
3540 and then not Has_Discriminants (Rec)
3541 then
3542 IR := Make_Itype_Reference (Sloc (Comp));
3543 Set_Itype (IR, Desig);
3544 Add_To_Result (IR);
3545 end if;
3547 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
3548 and then Convention (Desig) /= Convention_Protected
3549 then
3550 Set_Is_Frozen (Desig);
3551 end if;
3552 end Check_Itype;
3554 ------------------------------------
3555 -- Freeze_Choices_In_Variant_Part --
3556 ------------------------------------
3558 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
3559 pragma Assert (Nkind (VP) = N_Variant_Part);
3561 Variant : Node_Id;
3562 Choice : Node_Id;
3563 CL : Node_Id;
3565 begin
3566 -- Loop through variants
3568 Variant := First_Non_Pragma (Variants (VP));
3569 while Present (Variant) loop
3571 -- Loop through choices, checking that all types are frozen
3573 Choice := First_Non_Pragma (Discrete_Choices (Variant));
3574 while Present (Choice) loop
3575 if Nkind (Choice) in N_Has_Etype
3576 and then Present (Etype (Choice))
3577 then
3578 Freeze_And_Append (Etype (Choice), N, Result);
3579 end if;
3581 Next_Non_Pragma (Choice);
3582 end loop;
3584 -- Check for nested variant part to process
3586 CL := Component_List (Variant);
3588 if not Null_Present (CL) then
3589 if Present (Variant_Part (CL)) then
3590 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
3591 end if;
3592 end if;
3594 Next_Non_Pragma (Variant);
3595 end loop;
3596 end Freeze_Choices_In_Variant_Part;
3598 -- Start of processing for Freeze_Record_Type
3600 begin
3601 -- Deal with delayed aspect specifications for components. The
3602 -- analysis of the aspect is required to be delayed to the freeze
3603 -- point, thus we analyze the pragma or attribute definition
3604 -- clause in the tree at this point. We also analyze the aspect
3605 -- specification node at the freeze point when the aspect doesn't
3606 -- correspond to pragma/attribute definition clause.
3608 Comp := First_Entity (Rec);
3609 while Present (Comp) loop
3610 if Ekind (Comp) = E_Component
3611 and then Has_Delayed_Aspects (Comp)
3612 then
3613 if not Rec_Pushed then
3614 Push_Scope (Rec);
3615 Rec_Pushed := True;
3617 -- The visibility to the discriminants must be restored in
3618 -- order to properly analyze the aspects.
3620 if Has_Discriminants (Rec) then
3621 Install_Discriminants (Rec);
3622 end if;
3623 end if;
3625 Analyze_Aspects_At_Freeze_Point (Comp);
3626 end if;
3628 Next_Entity (Comp);
3629 end loop;
3631 -- Pop the scope if Rec scope has been pushed on the scope stack
3632 -- during the delayed aspect analysis process.
3634 if Rec_Pushed then
3635 if Has_Discriminants (Rec) then
3636 Uninstall_Discriminants (Rec);
3637 end if;
3639 Pop_Scope;
3640 end if;
3642 -- Freeze components and embedded subtypes
3644 Comp := First_Entity (Rec);
3645 Prev := Empty;
3646 while Present (Comp) loop
3647 if Is_Aliased (Comp) then
3648 Aliased_Component := True;
3649 end if;
3651 -- Handle the component and discriminant case
3653 if Ekind_In (Comp, E_Component, E_Discriminant) then
3654 declare
3655 CC : constant Node_Id := Component_Clause (Comp);
3657 begin
3658 -- Freezing a record type freezes the type of each of its
3659 -- components. However, if the type of the component is
3660 -- part of this record, we do not want or need a separate
3661 -- Freeze_Node. Note that Is_Itype is wrong because that's
3662 -- also set in private type cases. We also can't check for
3663 -- the Scope being exactly Rec because of private types and
3664 -- record extensions.
3666 if Is_Itype (Etype (Comp))
3667 and then Is_Record_Type (Underlying_Type
3668 (Scope (Etype (Comp))))
3669 then
3670 Undelay_Type (Etype (Comp));
3671 end if;
3673 Freeze_And_Append (Etype (Comp), N, Result);
3675 -- Warn for pragma Pack overriding foreign convention
3677 if Has_Foreign_Convention (Etype (Comp))
3678 and then Has_Pragma_Pack (Rec)
3680 -- Don't warn for aliased components, since override
3681 -- cannot happen in that case.
3683 and then not Is_Aliased (Comp)
3684 then
3685 declare
3686 CN : constant Name_Id :=
3687 Get_Convention_Name (Convention (Etype (Comp)));
3688 PP : constant Node_Id :=
3689 Get_Pragma (Rec, Pragma_Pack);
3690 begin
3691 if Present (PP) then
3692 Error_Msg_Name_1 := CN;
3693 Error_Msg_Sloc := Sloc (Comp);
3694 Error_Msg_N
3695 ("pragma Pack affects convention % component#??",
3696 PP);
3697 Error_Msg_Name_1 := CN;
3698 Error_Msg_NE
3699 ("\component & may not have % compatible "
3700 & "representation??", PP, Comp);
3701 end if;
3702 end;
3703 end if;
3705 -- Check for error of component clause given for variable
3706 -- sized type. We have to delay this test till this point,
3707 -- since the component type has to be frozen for us to know
3708 -- if it is variable length.
3710 if Present (CC) then
3711 Placed_Component := True;
3713 -- We omit this test in a generic context, it will be
3714 -- applied at instantiation time.
3716 if Inside_A_Generic then
3717 null;
3719 -- Also omit this test in CodePeer mode, since we do not
3720 -- have sufficient info on size and rep clauses.
3722 elsif CodePeer_Mode then
3723 null;
3725 -- Omit check if component has a generic type. This can
3726 -- happen in an instantiation within a generic in ASIS
3727 -- mode, where we force freeze actions without full
3728 -- expansion.
3730 elsif Is_Generic_Type (Etype (Comp)) then
3731 null;
3733 -- Do the check
3735 elsif not
3736 Size_Known_At_Compile_Time
3737 (Underlying_Type (Etype (Comp)))
3738 then
3739 Error_Msg_N
3740 ("component clause not allowed for variable " &
3741 "length component", CC);
3742 end if;
3744 else
3745 Unplaced_Component := True;
3746 end if;
3748 -- Case of component requires byte alignment
3750 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
3752 -- Set the enclosing record to also require byte align
3754 Set_Must_Be_On_Byte_Boundary (Rec);
3756 -- Check for component clause that is inconsistent with
3757 -- the required byte boundary alignment.
3759 if Present (CC)
3760 and then Normalized_First_Bit (Comp) mod
3761 System_Storage_Unit /= 0
3762 then
3763 Error_Msg_N
3764 ("component & must be byte aligned",
3765 Component_Name (Component_Clause (Comp)));
3766 end if;
3767 end if;
3768 end;
3769 end if;
3771 -- Gather data for possible Implicit_Packing later. Note that at
3772 -- this stage we might be dealing with a real component, or with
3773 -- an implicit subtype declaration.
3775 if not Is_Scalar_Type (Etype (Comp)) then
3776 All_Scalar_Components := False;
3777 else
3778 Scalar_Component_Total_RM_Size :=
3779 Scalar_Component_Total_RM_Size + RM_Size (Etype (Comp));
3780 Scalar_Component_Total_Esize :=
3781 Scalar_Component_Total_Esize + Esize (Etype (Comp));
3782 end if;
3784 -- If the component is an Itype with Delayed_Freeze and is either
3785 -- a record or array subtype and its base type has not yet been
3786 -- frozen, we must remove this from the entity list of this record
3787 -- and put it on the entity list of the scope of its base type.
3788 -- Note that we know that this is not the type of a component
3789 -- since we cleared Has_Delayed_Freeze for it in the previous
3790 -- loop. Thus this must be the Designated_Type of an access type,
3791 -- which is the type of a component.
3793 if Is_Itype (Comp)
3794 and then Is_Type (Scope (Comp))
3795 and then Is_Composite_Type (Comp)
3796 and then Base_Type (Comp) /= Comp
3797 and then Has_Delayed_Freeze (Comp)
3798 and then not Is_Frozen (Base_Type (Comp))
3799 then
3800 declare
3801 Will_Be_Frozen : Boolean := False;
3802 S : Entity_Id;
3804 begin
3805 -- We have a difficult case to handle here. Suppose Rec is
3806 -- subtype being defined in a subprogram that's created as
3807 -- part of the freezing of Rec'Base. In that case, we know
3808 -- that Comp'Base must have already been frozen by the time
3809 -- we get to elaborate this because Gigi doesn't elaborate
3810 -- any bodies until it has elaborated all of the declarative
3811 -- part. But Is_Frozen will not be set at this point because
3812 -- we are processing code in lexical order.
3814 -- We detect this case by going up the Scope chain of Rec
3815 -- and seeing if we have a subprogram scope before reaching
3816 -- the top of the scope chain or that of Comp'Base. If we
3817 -- do, then mark that Comp'Base will actually be frozen. If
3818 -- so, we merely undelay it.
3820 S := Scope (Rec);
3821 while Present (S) loop
3822 if Is_Subprogram (S) then
3823 Will_Be_Frozen := True;
3824 exit;
3825 elsif S = Scope (Base_Type (Comp)) then
3826 exit;
3827 end if;
3829 S := Scope (S);
3830 end loop;
3832 if Will_Be_Frozen then
3833 Undelay_Type (Comp);
3835 else
3836 if Present (Prev) then
3837 Set_Next_Entity (Prev, Next_Entity (Comp));
3838 else
3839 Set_First_Entity (Rec, Next_Entity (Comp));
3840 end if;
3842 -- Insert in entity list of scope of base type (which
3843 -- must be an enclosing scope, because still unfrozen).
3845 Append_Entity (Comp, Scope (Base_Type (Comp)));
3846 end if;
3847 end;
3849 -- If the component is an access type with an allocator as default
3850 -- value, the designated type will be frozen by the corresponding
3851 -- expression in init_proc. In order to place the freeze node for
3852 -- the designated type before that for the current record type,
3853 -- freeze it now.
3855 -- Same process if the component is an array of access types,
3856 -- initialized with an aggregate. If the designated type is
3857 -- private, it cannot contain allocators, and it is premature
3858 -- to freeze the type, so we check for this as well.
3860 elsif Is_Access_Type (Etype (Comp))
3861 and then Present (Parent (Comp))
3862 and then Present (Expression (Parent (Comp)))
3863 then
3864 declare
3865 Alloc : constant Node_Id :=
3866 Check_Allocator (Expression (Parent (Comp)));
3868 begin
3869 if Present (Alloc) then
3871 -- If component is pointer to a class-wide type, freeze
3872 -- the specific type in the expression being allocated.
3873 -- The expression may be a subtype indication, in which
3874 -- case freeze the subtype mark.
3876 if Is_Class_Wide_Type
3877 (Designated_Type (Etype (Comp)))
3878 then
3879 if Is_Entity_Name (Expression (Alloc)) then
3880 Freeze_And_Append
3881 (Entity (Expression (Alloc)), N, Result);
3883 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
3884 then
3885 Freeze_And_Append
3886 (Entity (Subtype_Mark (Expression (Alloc))),
3887 N, Result);
3888 end if;
3890 elsif Is_Itype (Designated_Type (Etype (Comp))) then
3891 Check_Itype (Etype (Comp));
3893 else
3894 Freeze_And_Append
3895 (Designated_Type (Etype (Comp)), N, Result);
3896 end if;
3897 end if;
3898 end;
3900 elsif Is_Access_Type (Etype (Comp))
3901 and then Is_Itype (Designated_Type (Etype (Comp)))
3902 then
3903 Check_Itype (Etype (Comp));
3905 -- Freeze the designated type when initializing a component with
3906 -- an aggregate in case the aggregate contains allocators.
3908 -- type T is ...;
3909 -- type T_Ptr is access all T;
3910 -- type T_Array is array ... of T_Ptr;
3912 -- type Rec is record
3913 -- Comp : T_Array := (others => ...);
3914 -- end record;
3916 elsif Is_Array_Type (Etype (Comp))
3917 and then Is_Access_Type (Component_Type (Etype (Comp)))
3918 then
3919 declare
3920 Comp_Par : constant Node_Id := Parent (Comp);
3921 Desig_Typ : constant Entity_Id :=
3922 Designated_Type
3923 (Component_Type (Etype (Comp)));
3925 begin
3926 -- The only case when this sort of freezing is not done is
3927 -- when the designated type is class-wide and the root type
3928 -- is the record owning the component. This scenario results
3929 -- in a circularity because the class-wide type requires
3930 -- primitives that have not been created yet as the root
3931 -- type is in the process of being frozen.
3933 -- type Rec is tagged;
3934 -- type Rec_Ptr is access all Rec'Class;
3935 -- type Rec_Array is array ... of Rec_Ptr;
3937 -- type Rec is record
3938 -- Comp : Rec_Array := (others => ...);
3939 -- end record;
3941 if Is_Class_Wide_Type (Desig_Typ)
3942 and then Root_Type (Desig_Typ) = Rec
3943 then
3944 null;
3946 elsif Is_Fully_Defined (Desig_Typ)
3947 and then Present (Comp_Par)
3948 and then Nkind (Comp_Par) = N_Component_Declaration
3949 and then Present (Expression (Comp_Par))
3950 and then Nkind (Expression (Comp_Par)) = N_Aggregate
3951 then
3952 Freeze_And_Append (Desig_Typ, N, Result);
3953 end if;
3954 end;
3955 end if;
3957 Prev := Comp;
3958 Next_Entity (Comp);
3959 end loop;
3961 -- Deal with default setting of reverse storage order
3963 Set_SSO_From_Default (Rec);
3965 -- Check consistent attribute setting on component types
3967 SSO_ADC := Get_Attribute_Definition_Clause
3968 (Rec, Attribute_Scalar_Storage_Order);
3970 declare
3971 Comp_ADC_Present : Boolean;
3972 begin
3973 Comp := First_Component (Rec);
3974 while Present (Comp) loop
3975 Check_Component_Storage_Order
3976 (Encl_Type => Rec,
3977 Comp => Comp,
3978 ADC => SSO_ADC,
3979 Comp_ADC_Present => Comp_ADC_Present);
3980 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
3981 Next_Component (Comp);
3982 end loop;
3983 end;
3985 -- Now deal with reverse storage order/bit order issues
3987 if Present (SSO_ADC) then
3989 -- Check compatibility of Scalar_Storage_Order with Bit_Order, if
3990 -- the former is specified.
3992 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
3994 -- Note: report error on Rec, not on SSO_ADC, as ADC may apply
3995 -- to some ancestor type.
3997 Error_Msg_Sloc := Sloc (SSO_ADC);
3998 Error_Msg_N
3999 ("scalar storage order for& specified# inconsistent with "
4000 & "bit order", Rec);
4001 end if;
4003 -- Warn if there is an Scalar_Storage_Order attribute definition
4004 -- clause but no component clause, no component that itself has
4005 -- such an attribute definition, and no pragma Pack.
4007 if not (Placed_Component
4008 or else
4009 SSO_ADC_Component
4010 or else
4011 Is_Packed (Rec))
4012 then
4013 Error_Msg_N
4014 ("??scalar storage order specified but no component clause",
4015 SSO_ADC);
4016 end if;
4017 end if;
4019 -- Deal with Bit_Order aspect
4021 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
4023 if Present (ADC) and then Base_Type (Rec) = Rec then
4024 if not (Placed_Component
4025 or else Present (SSO_ADC)
4026 or else Is_Packed (Rec))
4027 then
4028 -- Warn if clause has no effect when no component clause is
4029 -- present, but suppress warning if the Bit_Order is required
4030 -- due to the presence of a Scalar_Storage_Order attribute.
4032 Error_Msg_N
4033 ("??bit order specification has no effect", ADC);
4034 Error_Msg_N
4035 ("\??since no component clauses were specified", ADC);
4037 -- Here is where we do the processing to adjust component clauses
4038 -- for reversed bit order, when not using reverse SSO.
4040 elsif Reverse_Bit_Order (Rec)
4041 and then not Reverse_Storage_Order (Rec)
4042 then
4043 Adjust_Record_For_Reverse_Bit_Order (Rec);
4045 -- Case where we have both an explicit Bit_Order and the same
4046 -- Scalar_Storage_Order: leave record untouched, the back-end
4047 -- will take care of required layout conversions.
4049 else
4050 null;
4052 end if;
4053 end if;
4055 -- Complete error checking on record representation clause (e.g.
4056 -- overlap of components). This is called after adjusting the
4057 -- record for reverse bit order.
4059 declare
4060 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
4061 begin
4062 if Present (RRC) then
4063 Check_Record_Representation_Clause (RRC);
4064 end if;
4065 end;
4067 -- Set OK_To_Reorder_Components depending on debug flags
4069 if Is_Base_Type (Rec) and then Convention (Rec) = Convention_Ada then
4070 if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
4071 or else
4072 (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
4073 then
4074 Set_OK_To_Reorder_Components (Rec);
4075 end if;
4076 end if;
4078 -- Check for useless pragma Pack when all components placed. We only
4079 -- do this check for record types, not subtypes, since a subtype may
4080 -- have all its components placed, and it still makes perfectly good
4081 -- sense to pack other subtypes or the parent type. We do not give
4082 -- this warning if Optimize_Alignment is set to Space, since the
4083 -- pragma Pack does have an effect in this case (it always resets
4084 -- the alignment to one).
4086 if Ekind (Rec) = E_Record_Type
4087 and then Is_Packed (Rec)
4088 and then not Unplaced_Component
4089 and then Optimize_Alignment /= 'S'
4090 then
4091 -- Reset packed status. Probably not necessary, but we do it so
4092 -- that there is no chance of the back end doing something strange
4093 -- with this redundant indication of packing.
4095 Set_Is_Packed (Rec, False);
4097 -- Give warning if redundant constructs warnings on
4099 if Warn_On_Redundant_Constructs then
4100 Error_Msg_N -- CODEFIX
4101 ("??pragma Pack has no effect, no unplaced components",
4102 Get_Rep_Pragma (Rec, Name_Pack));
4103 end if;
4104 end if;
4106 -- If this is the record corresponding to a remote type, freeze the
4107 -- remote type here since that is what we are semantically freezing.
4108 -- This prevents the freeze node for that type in an inner scope.
4110 if Ekind (Rec) = E_Record_Type then
4111 if Present (Corresponding_Remote_Type (Rec)) then
4112 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
4113 end if;
4115 -- Check for controlled components and unchecked unions.
4117 Comp := First_Component (Rec);
4118 while Present (Comp) loop
4120 -- Do not set Has_Controlled_Component on a class-wide
4121 -- equivalent type. See Make_CW_Equivalent_Type.
4123 if not Is_Class_Wide_Equivalent_Type (Rec)
4124 and then
4125 (Has_Controlled_Component (Etype (Comp))
4126 or else
4127 (Chars (Comp) /= Name_uParent
4128 and then Is_Controlled_Active (Etype (Comp)))
4129 or else
4130 (Is_Protected_Type (Etype (Comp))
4131 and then
4132 Present (Corresponding_Record_Type (Etype (Comp)))
4133 and then
4134 Has_Controlled_Component
4135 (Corresponding_Record_Type (Etype (Comp)))))
4136 then
4137 Set_Has_Controlled_Component (Rec);
4138 end if;
4140 if Has_Unchecked_Union (Etype (Comp)) then
4141 Set_Has_Unchecked_Union (Rec);
4142 end if;
4144 -- Scan component declaration for likely misuses of current
4145 -- instance, either in a constraint or a default expression.
4147 if Has_Per_Object_Constraint (Comp) then
4148 Check_Current_Instance (Parent (Comp));
4149 end if;
4151 Next_Component (Comp);
4152 end loop;
4153 end if;
4155 -- Enforce the restriction that access attributes with a current
4156 -- instance prefix can only apply to limited types. This comment
4157 -- is floating here, but does not seem to belong here???
4159 -- Set component alignment if not otherwise already set
4161 Set_Component_Alignment_If_Not_Set (Rec);
4163 -- For first subtypes, check if there are any fixed-point fields with
4164 -- component clauses, where we must check the size. This is not done
4165 -- till the freeze point since for fixed-point types, we do not know
4166 -- the size until the type is frozen. Similar processing applies to
4167 -- bit packed arrays.
4169 if Is_First_Subtype (Rec) then
4170 Comp := First_Component (Rec);
4171 while Present (Comp) loop
4172 if Present (Component_Clause (Comp))
4173 and then (Is_Fixed_Point_Type (Etype (Comp))
4174 or else Is_Bit_Packed_Array (Etype (Comp)))
4175 then
4176 Check_Size
4177 (Component_Name (Component_Clause (Comp)),
4178 Etype (Comp),
4179 Esize (Comp),
4180 Junk);
4181 end if;
4183 Next_Component (Comp);
4184 end loop;
4185 end if;
4187 -- Generate warning for applying C or C++ convention to a record
4188 -- with discriminants. This is suppressed for the unchecked union
4189 -- case, since the whole point in this case is interface C. We also
4190 -- do not generate this within instantiations, since we will have
4191 -- generated a message on the template.
4193 if Has_Discriminants (E)
4194 and then not Is_Unchecked_Union (E)
4195 and then (Convention (E) = Convention_C
4196 or else
4197 Convention (E) = Convention_CPP)
4198 and then Comes_From_Source (E)
4199 and then not In_Instance
4200 and then not Has_Warnings_Off (E)
4201 and then not Has_Warnings_Off (Base_Type (E))
4202 then
4203 declare
4204 Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
4205 A2 : Node_Id;
4207 begin
4208 if Present (Cprag) then
4209 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
4211 if Convention (E) = Convention_C then
4212 Error_Msg_N
4213 ("?x?variant record has no direct equivalent in C",
4214 A2);
4215 else
4216 Error_Msg_N
4217 ("?x?variant record has no direct equivalent in C++",
4218 A2);
4219 end if;
4221 Error_Msg_NE
4222 ("\?x?use of convention for type& is dubious", A2, E);
4223 end if;
4224 end;
4225 end if;
4227 -- See if Size is too small as is (and implicit packing might help)
4229 if not Is_Packed (Rec)
4231 -- No implicit packing if even one component is explicitly placed
4233 and then not Placed_Component
4235 -- Or even one component is aliased
4237 and then not Aliased_Component
4239 -- Must have size clause and all scalar components
4241 and then Has_Size_Clause (Rec)
4242 and then All_Scalar_Components
4244 -- Do not try implicit packing on records with discriminants, too
4245 -- complicated, especially in the variant record case.
4247 and then not Has_Discriminants (Rec)
4249 -- We can implicitly pack if the specified size of the record is
4250 -- less than the sum of the object sizes (no point in packing if
4251 -- this is not the case).
4253 and then RM_Size (Rec) < Scalar_Component_Total_Esize
4255 -- And the total RM size cannot be greater than the specified size
4256 -- since otherwise packing will not get us where we have to be.
4258 and then RM_Size (Rec) >= Scalar_Component_Total_RM_Size
4260 -- Never do implicit packing in CodePeer or SPARK modes since
4261 -- we don't do any packing in these modes, since this generates
4262 -- over-complex code that confuses static analysis, and in
4263 -- general, neither CodePeer not GNATprove care about the
4264 -- internal representation of objects.
4266 and then not (CodePeer_Mode or GNATprove_Mode)
4267 then
4268 -- If implicit packing enabled, do it
4270 if Implicit_Packing then
4271 Set_Is_Packed (Rec);
4273 -- Otherwise flag the size clause
4275 else
4276 declare
4277 Sz : constant Node_Id := Size_Clause (Rec);
4278 begin
4279 Error_Msg_NE -- CODEFIX
4280 ("size given for& too small", Sz, Rec);
4281 Error_Msg_N -- CODEFIX
4282 ("\use explicit pragma Pack "
4283 & "or use pragma Implicit_Packing", Sz);
4284 end;
4285 end if;
4286 end if;
4288 -- The following checks are only relevant when SPARK_Mode is on as
4289 -- they are not standard Ada legality rules.
4291 if SPARK_Mode = On then
4292 if Is_Effectively_Volatile (Rec) then
4294 -- A discriminated type cannot be effectively volatile
4295 -- (SPARK RM C.6(4)).
4297 if Has_Discriminants (Rec) then
4298 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
4300 -- A tagged type cannot be effectively volatile
4301 -- (SPARK RM C.6(5)).
4303 elsif Is_Tagged_Type (Rec) then
4304 Error_Msg_N ("tagged type & cannot be volatile", Rec);
4305 end if;
4307 -- A non-effectively volatile record type cannot contain
4308 -- effectively volatile components (SPARK RM C.6(2)).
4310 else
4311 Comp := First_Component (Rec);
4312 while Present (Comp) loop
4313 if Comes_From_Source (Comp)
4314 and then Is_Effectively_Volatile (Etype (Comp))
4315 then
4316 Error_Msg_Name_1 := Chars (Rec);
4317 Error_Msg_N
4318 ("component & of non-volatile type % cannot be "
4319 & "volatile", Comp);
4320 end if;
4322 Next_Component (Comp);
4323 end loop;
4324 end if;
4325 end if;
4327 -- Make sure that if we have an iterator aspect, then we have
4328 -- either Constant_Indexing or Variable_Indexing.
4330 declare
4331 Iterator_Aspect : Node_Id;
4333 begin
4334 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
4336 if No (Iterator_Aspect) then
4337 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
4338 end if;
4340 if Present (Iterator_Aspect) then
4341 if Has_Aspect (Rec, Aspect_Constant_Indexing)
4342 or else
4343 Has_Aspect (Rec, Aspect_Variable_Indexing)
4344 then
4345 null;
4346 else
4347 Error_Msg_N
4348 ("Iterator_Element requires indexing aspect",
4349 Iterator_Aspect);
4350 end if;
4351 end if;
4352 end;
4354 -- All done if not a full record definition
4356 if Ekind (Rec) /= E_Record_Type then
4357 return;
4358 end if;
4360 -- Finally we need to check the variant part to make sure that
4361 -- all types within choices are properly frozen as part of the
4362 -- freezing of the record type.
4364 Check_Variant_Part : declare
4365 D : constant Node_Id := Declaration_Node (Rec);
4366 T : Node_Id;
4367 C : Node_Id;
4369 begin
4370 -- Find component list
4372 C := Empty;
4374 if Nkind (D) = N_Full_Type_Declaration then
4375 T := Type_Definition (D);
4377 if Nkind (T) = N_Record_Definition then
4378 C := Component_List (T);
4380 elsif Nkind (T) = N_Derived_Type_Definition
4381 and then Present (Record_Extension_Part (T))
4382 then
4383 C := Component_List (Record_Extension_Part (T));
4384 end if;
4385 end if;
4387 -- Case of variant part present
4389 if Present (C) and then Present (Variant_Part (C)) then
4390 Freeze_Choices_In_Variant_Part (Variant_Part (C));
4391 end if;
4393 -- Note: we used to call Check_Choices here, but it is too early,
4394 -- since predicated subtypes are frozen here, but their freezing
4395 -- actions are in Analyze_Freeze_Entity, which has not been called
4396 -- yet for entities frozen within this procedure, so we moved that
4397 -- call to the Analyze_Freeze_Entity for the record type.
4399 end Check_Variant_Part;
4401 -- Check that all the primitives of an interface type are abstract
4402 -- or null procedures.
4404 if Is_Interface (Rec)
4405 and then not Error_Posted (Parent (Rec))
4406 then
4407 declare
4408 Elmt : Elmt_Id;
4409 Subp : Entity_Id;
4411 begin
4412 Elmt := First_Elmt (Primitive_Operations (Rec));
4413 while Present (Elmt) loop
4414 Subp := Node (Elmt);
4416 if not Is_Abstract_Subprogram (Subp)
4418 -- Avoid reporting the error on inherited primitives
4420 and then Comes_From_Source (Subp)
4421 then
4422 Error_Msg_Name_1 := Chars (Subp);
4424 if Ekind (Subp) = E_Procedure then
4425 if not Null_Present (Parent (Subp)) then
4426 Error_Msg_N
4427 ("interface procedure % must be abstract or null",
4428 Parent (Subp));
4429 end if;
4430 else
4431 Error_Msg_N
4432 ("interface function % must be abstract",
4433 Parent (Subp));
4434 end if;
4435 end if;
4437 Next_Elmt (Elmt);
4438 end loop;
4439 end;
4440 end if;
4441 end Freeze_Record_Type;
4443 -------------------------------
4444 -- Has_Boolean_Aspect_Import --
4445 -------------------------------
4447 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4448 Decl : constant Node_Id := Declaration_Node (E);
4449 Asp : Node_Id;
4450 Expr : Node_Id;
4452 begin
4453 if Has_Aspects (Decl) then
4454 Asp := First (Aspect_Specifications (Decl));
4455 while Present (Asp) loop
4456 Expr := Expression (Asp);
4458 -- The value of aspect Import is True when the expression is
4459 -- either missing or it is explicitly set to True.
4461 if Get_Aspect_Id (Asp) = Aspect_Import
4462 and then (No (Expr)
4463 or else (Compile_Time_Known_Value (Expr)
4464 and then Is_True (Expr_Value (Expr))))
4465 then
4466 return True;
4467 end if;
4469 Next (Asp);
4470 end loop;
4471 end if;
4473 return False;
4474 end Has_Boolean_Aspect_Import;
4476 ----------------------------
4477 -- Late_Freeze_Subprogram --
4478 ----------------------------
4480 procedure Late_Freeze_Subprogram (E : Entity_Id) is
4481 Spec : constant Node_Id :=
4482 Specification (Unit_Declaration_Node (Scope (E)));
4483 Decls : List_Id;
4485 begin
4486 if Present (Private_Declarations (Spec)) then
4487 Decls := Private_Declarations (Spec);
4488 else
4489 Decls := Visible_Declarations (Spec);
4490 end if;
4492 Append_List (Result, Decls);
4493 end Late_Freeze_Subprogram;
4495 ---------------------
4496 -- Restore_Globals --
4497 ---------------------
4499 procedure Restore_Globals is
4500 begin
4501 Ghost_Mode := GM;
4502 end Restore_Globals;
4504 ------------------------------
4505 -- Wrap_Imported_Subprogram --
4506 ------------------------------
4508 -- The issue here is that our normal approach of checking preconditions
4509 -- and postconditions does not work for imported procedures, since we
4510 -- are not generating code for the body. To get around this we create
4511 -- a wrapper, as shown by the following example:
4513 -- procedure K (A : Integer);
4514 -- pragma Import (C, K);
4516 -- The spec is rewritten by removing the effects of pragma Import, but
4517 -- leaving the convention unchanged, as though the source had said:
4519 -- procedure K (A : Integer);
4520 -- pragma Convention (C, K);
4522 -- and we create a body, added to the entity K freeze actions, which
4523 -- looks like:
4525 -- procedure K (A : Integer) is
4526 -- procedure K (A : Integer);
4527 -- pragma Import (C, K);
4528 -- begin
4529 -- K (A);
4530 -- end K;
4532 -- Now the contract applies in the normal way to the outer procedure,
4533 -- and the inner procedure has no contracts, so there is no problem
4534 -- in just calling it to get the original effect.
4536 -- In the case of a function, we create an appropriate return statement
4537 -- for the subprogram body that calls the inner procedure.
4539 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
4540 Loc : constant Source_Ptr := Sloc (E);
4541 CE : constant Name_Id := Chars (E);
4542 Spec : Node_Id;
4543 Parms : List_Id;
4544 Stmt : Node_Id;
4545 Iprag : Node_Id;
4546 Bod : Node_Id;
4547 Forml : Entity_Id;
4549 begin
4550 -- Nothing to do if not imported
4552 if not Is_Imported (E) then
4553 return;
4555 -- Test enabling conditions for wrapping
4557 elsif Is_Subprogram (E)
4558 and then Present (Contract (E))
4559 and then Present (Pre_Post_Conditions (Contract (E)))
4560 and then not GNATprove_Mode
4561 then
4562 -- Here we do the wrap
4564 -- Note on calls to Copy_Separate_Tree. The trees we are copying
4565 -- here are fully analyzed, but we definitely want fully syntactic
4566 -- unanalyzed trees in the body we construct, so that the analysis
4567 -- generates the right visibility, and that is exactly what the
4568 -- calls to Copy_Separate_Tree give us.
4570 -- Acquire copy of Inline pragma, and indicate that it does not
4571 -- come from an aspect, as it applies to an internal entity.
4573 Iprag := Copy_Separate_Tree (Import_Pragma (E));
4574 Set_From_Aspect_Specification (Iprag, False);
4576 -- Fix up spec to be not imported any more
4578 Set_Is_Imported (E, False);
4579 Set_Interface_Name (E, Empty);
4580 Set_Has_Completion (E, False);
4581 Set_Import_Pragma (E, Empty);
4583 -- Grab the subprogram declaration and specification
4585 Spec := Declaration_Node (E);
4587 -- Build parameter list that we need
4589 Parms := New_List;
4590 Forml := First_Formal (E);
4591 while Present (Forml) loop
4592 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
4593 Next_Formal (Forml);
4594 end loop;
4596 -- Build the call
4598 if Ekind_In (E, E_Function, E_Generic_Function) then
4599 Stmt :=
4600 Make_Simple_Return_Statement (Loc,
4601 Expression =>
4602 Make_Function_Call (Loc,
4603 Name => Make_Identifier (Loc, CE),
4604 Parameter_Associations => Parms));
4606 else
4607 Stmt :=
4608 Make_Procedure_Call_Statement (Loc,
4609 Name => Make_Identifier (Loc, CE),
4610 Parameter_Associations => Parms);
4611 end if;
4613 -- Now build the body
4615 Bod :=
4616 Make_Subprogram_Body (Loc,
4617 Specification =>
4618 Copy_Separate_Tree (Spec),
4619 Declarations => New_List (
4620 Make_Subprogram_Declaration (Loc,
4621 Specification =>
4622 Copy_Separate_Tree (Spec)),
4623 Iprag),
4624 Handled_Statement_Sequence =>
4625 Make_Handled_Sequence_Of_Statements (Loc,
4626 Statements => New_List (Stmt),
4627 End_Label => Make_Identifier (Loc, CE)));
4629 -- Append the body to freeze result
4631 Add_To_Result (Bod);
4632 return;
4634 -- Case of imported subprogram that does not get wrapped
4636 else
4637 -- Set Is_Public. All imported entities need an external symbol
4638 -- created for them since they are always referenced from another
4639 -- object file. Note this used to be set when we set Is_Imported
4640 -- back in Sem_Prag, but now we delay it to this point, since we
4641 -- don't want to set this flag if we wrap an imported subprogram.
4643 Set_Is_Public (E);
4644 end if;
4645 end Wrap_Imported_Subprogram;
4647 -- Start of processing for Freeze_Entity
4649 begin
4650 -- The entity being frozen may be subject to pragma Ghost with policy
4651 -- Ignore. Set the mode now to ensure that any nodes generated during
4652 -- freezing are properly flagged as ignored Ghost.
4654 Set_Ghost_Mode_From_Entity (E);
4656 -- We are going to test for various reasons why this entity need not be
4657 -- frozen here, but in the case of an Itype that's defined within a
4658 -- record, that test actually applies to the record.
4660 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
4661 Test_E := Scope (E);
4662 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
4663 and then Is_Record_Type (Underlying_Type (Scope (E)))
4664 then
4665 Test_E := Underlying_Type (Scope (E));
4666 end if;
4668 -- Do not freeze if already frozen since we only need one freeze node
4670 if Is_Frozen (E) then
4671 Restore_Globals;
4672 return No_List;
4674 -- It is improper to freeze an external entity within a generic because
4675 -- its freeze node will appear in a non-valid context. The entity will
4676 -- be frozen in the proper scope after the current generic is analyzed.
4677 -- However, aspects must be analyzed because they may be queried later
4678 -- within the generic itself, and the corresponding pragma or attribute
4679 -- definition has not been analyzed yet.
4681 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
4682 if Has_Delayed_Aspects (E) then
4683 Analyze_Aspects_At_Freeze_Point (E);
4684 end if;
4686 Restore_Globals;
4687 return No_List;
4689 -- AI05-0213: A formal incomplete type does not freeze the actual. In
4690 -- the instance, the same applies to the subtype renaming the actual.
4692 elsif Is_Private_Type (E)
4693 and then Is_Generic_Actual_Type (E)
4694 and then No (Full_View (Base_Type (E)))
4695 and then Ada_Version >= Ada_2012
4696 then
4697 Restore_Globals;
4698 return No_List;
4700 -- Formal subprograms are never frozen
4702 elsif Is_Formal_Subprogram (E) then
4703 Restore_Globals;
4704 return No_List;
4706 -- Generic types are never frozen as they lack delayed semantic checks
4708 elsif Is_Generic_Type (E) then
4709 Restore_Globals;
4710 return No_List;
4712 -- Do not freeze a global entity within an inner scope created during
4713 -- expansion. A call to subprogram E within some internal procedure
4714 -- (a stream attribute for example) might require freezing E, but the
4715 -- freeze node must appear in the same declarative part as E itself.
4716 -- The two-pass elaboration mechanism in gigi guarantees that E will
4717 -- be frozen before the inner call is elaborated. We exclude constants
4718 -- from this test, because deferred constants may be frozen early, and
4719 -- must be diagnosed (e.g. in the case of a deferred constant being used
4720 -- in a default expression). If the enclosing subprogram comes from
4721 -- source, or is a generic instance, then the freeze point is the one
4722 -- mandated by the language, and we freeze the entity. A subprogram that
4723 -- is a child unit body that acts as a spec does not have a spec that
4724 -- comes from source, but can only come from source.
4726 elsif In_Open_Scopes (Scope (Test_E))
4727 and then Scope (Test_E) /= Current_Scope
4728 and then Ekind (Test_E) /= E_Constant
4729 then
4730 declare
4731 S : Entity_Id;
4733 begin
4734 S := Current_Scope;
4735 while Present (S) loop
4736 if Is_Overloadable (S) then
4737 if Comes_From_Source (S)
4738 or else Is_Generic_Instance (S)
4739 or else Is_Child_Unit (S)
4740 then
4741 exit;
4742 else
4743 Restore_Globals;
4744 return No_List;
4745 end if;
4746 end if;
4748 S := Scope (S);
4749 end loop;
4750 end;
4752 -- Similarly, an inlined instance body may make reference to global
4753 -- entities, but these references cannot be the proper freezing point
4754 -- for them, and in the absence of inlining freezing will take place in
4755 -- their own scope. Normally instance bodies are analyzed after the
4756 -- enclosing compilation, and everything has been frozen at the proper
4757 -- place, but with front-end inlining an instance body is compiled
4758 -- before the end of the enclosing scope, and as a result out-of-order
4759 -- freezing must be prevented.
4761 elsif Front_End_Inlining
4762 and then In_Instance_Body
4763 and then Present (Scope (Test_E))
4764 then
4765 declare
4766 S : Entity_Id;
4768 begin
4769 S := Scope (Test_E);
4770 while Present (S) loop
4771 if Is_Generic_Instance (S) then
4772 exit;
4773 else
4774 S := Scope (S);
4775 end if;
4776 end loop;
4778 if No (S) then
4779 Restore_Globals;
4780 return No_List;
4781 end if;
4782 end;
4784 elsif Ekind (E) = E_Generic_Package then
4785 Result := Freeze_Generic_Entities (E);
4787 Restore_Globals;
4788 return Result;
4789 end if;
4791 -- Add checks to detect proper initialization of scalars that may appear
4792 -- as subprogram parameters.
4794 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
4795 Apply_Parameter_Validity_Checks (E);
4796 end if;
4798 -- Deal with delayed aspect specifications. The analysis of the aspect
4799 -- is required to be delayed to the freeze point, thus we analyze the
4800 -- pragma or attribute definition clause in the tree at this point. We
4801 -- also analyze the aspect specification node at the freeze point when
4802 -- the aspect doesn't correspond to pragma/attribute definition clause.
4804 if Has_Delayed_Aspects (E) then
4805 Analyze_Aspects_At_Freeze_Point (E);
4806 end if;
4808 -- Here to freeze the entity
4810 Set_Is_Frozen (E);
4812 -- Case of entity being frozen is other than a type
4814 if not Is_Type (E) then
4816 -- If entity is exported or imported and does not have an external
4817 -- name, now is the time to provide the appropriate default name.
4818 -- Skip this if the entity is stubbed, since we don't need a name
4819 -- for any stubbed routine. For the case on intrinsics, if no
4820 -- external name is specified, then calls will be handled in
4821 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
4822 -- external name is provided, then Expand_Intrinsic_Call leaves
4823 -- calls in place for expansion by GIGI.
4825 if (Is_Imported (E) or else Is_Exported (E))
4826 and then No (Interface_Name (E))
4827 and then Convention (E) /= Convention_Stubbed
4828 and then Convention (E) /= Convention_Intrinsic
4829 then
4830 Set_Encoded_Interface_Name
4831 (E, Get_Default_External_Name (E));
4833 -- If entity is an atomic object appearing in a declaration and
4834 -- the expression is an aggregate, assign it to a temporary to
4835 -- ensure that the actual assignment is done atomically rather
4836 -- than component-wise (the assignment to the temp may be done
4837 -- component-wise, but that is harmless).
4839 elsif Is_Atomic_Or_VFA (E)
4840 and then Nkind (Parent (E)) = N_Object_Declaration
4841 and then Present (Expression (Parent (E)))
4842 and then Nkind (Expression (Parent (E))) = N_Aggregate
4843 and then Is_Atomic_VFA_Aggregate (Expression (Parent (E)))
4844 then
4845 null;
4846 end if;
4848 -- Subprogram case
4850 if Is_Subprogram (E) then
4852 -- Check for needing to wrap imported subprogram
4854 Wrap_Imported_Subprogram (E);
4856 -- Freeze all parameter types and the return type (RM 13.14(14)).
4857 -- However skip this for internal subprograms. This is also where
4858 -- any extra formal parameters are created since we now know
4859 -- whether the subprogram will use a foreign convention.
4861 -- In Ada 2012, freezing a subprogram does not always freeze
4862 -- the corresponding profile (see AI05-019). An attribute
4863 -- reference is not a freezing point of the profile.
4864 -- Other constructs that should not freeze ???
4866 -- This processing doesn't apply to internal entities (see below)
4868 if not Is_Internal (E) then
4869 if not Freeze_Profile (E) then
4870 Restore_Globals;
4871 return Result;
4872 end if;
4873 end if;
4875 -- Must freeze its parent first if it is a derived subprogram
4877 if Present (Alias (E)) then
4878 Freeze_And_Append (Alias (E), N, Result);
4879 end if;
4881 -- We don't freeze internal subprograms, because we don't normally
4882 -- want addition of extra formals or mechanism setting to happen
4883 -- for those. However we do pass through predefined dispatching
4884 -- cases, since extra formals may be needed in some cases, such as
4885 -- for the stream 'Input function (build-in-place formals).
4887 if not Is_Internal (E)
4888 or else Is_Predefined_Dispatching_Operation (E)
4889 then
4890 Freeze_Subprogram (E);
4891 end if;
4893 if Late_Freezing then
4894 Late_Freeze_Subprogram (E);
4895 Restore_Globals;
4896 return No_List;
4897 end if;
4899 -- If warning on suspicious contracts then check for the case of
4900 -- a postcondition other than False for a No_Return subprogram.
4902 if No_Return (E)
4903 and then Warn_On_Suspicious_Contract
4904 and then Present (Contract (E))
4905 then
4906 declare
4907 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
4908 Exp : Node_Id;
4910 begin
4911 while Present (Prag) loop
4912 if Nam_In (Pragma_Name (Prag), Name_Post,
4913 Name_Postcondition,
4914 Name_Refined_Post)
4915 then
4916 Exp :=
4917 Expression
4918 (First (Pragma_Argument_Associations (Prag)));
4920 if Nkind (Exp) /= N_Identifier
4921 or else Chars (Exp) /= Name_False
4922 then
4923 Error_Msg_NE
4924 ("useless postcondition, & is marked "
4925 & "No_Return?T?", Exp, E);
4926 end if;
4927 end if;
4929 Prag := Next_Pragma (Prag);
4930 end loop;
4931 end;
4932 end if;
4934 -- Here for other than a subprogram or type
4936 else
4937 -- If entity has a type, and it is not a generic unit, then
4938 -- freeze it first (RM 13.14(10)).
4940 if Present (Etype (E))
4941 and then Ekind (E) /= E_Generic_Function
4942 then
4943 Freeze_And_Append (Etype (E), N, Result);
4945 -- For an object of an anonymous array type, aspects on the
4946 -- object declaration apply to the type itself. This is the
4947 -- case for Atomic_Components, Volatile_Components, and
4948 -- Independent_Components. In these cases analysis of the
4949 -- generated pragma will mark the anonymous types accordingly,
4950 -- and the object itself does not require a freeze node.
4952 if Ekind (E) = E_Variable
4953 and then Is_Itype (Etype (E))
4954 and then Is_Array_Type (Etype (E))
4955 and then Has_Delayed_Aspects (E)
4956 then
4957 Set_Has_Delayed_Aspects (E, False);
4958 Set_Has_Delayed_Freeze (E, False);
4959 Set_Freeze_Node (E, Empty);
4960 end if;
4961 end if;
4963 -- Special processing for objects created by object declaration
4965 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
4966 Freeze_Object_Declaration (E);
4967 end if;
4969 -- Check that a constant which has a pragma Volatile[_Components]
4970 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
4972 -- Note: Atomic[_Components] also sets Volatile[_Components]
4974 if Ekind (E) = E_Constant
4975 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
4976 and then not Is_Imported (E)
4977 and then not Has_Boolean_Aspect_Import (E)
4978 then
4979 -- Make sure we actually have a pragma, and have not merely
4980 -- inherited the indication from elsewhere (e.g. an address
4981 -- clause, which is not good enough in RM terms).
4983 if Has_Rep_Pragma (E, Name_Atomic)
4984 or else
4985 Has_Rep_Pragma (E, Name_Atomic_Components)
4986 then
4987 Error_Msg_N
4988 ("stand alone atomic constant must be " &
4989 "imported (RM C.6(13))", E);
4991 elsif Has_Rep_Pragma (E, Name_Volatile)
4992 or else
4993 Has_Rep_Pragma (E, Name_Volatile_Components)
4994 then
4995 Error_Msg_N
4996 ("stand alone volatile constant must be " &
4997 "imported (RM C.6(13))", E);
4998 end if;
4999 end if;
5001 -- Static objects require special handling
5003 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
5004 and then Is_Statically_Allocated (E)
5005 then
5006 Freeze_Static_Object (E);
5007 end if;
5009 -- Remaining step is to layout objects
5011 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
5012 or else Is_Formal (E)
5013 then
5014 Layout_Object (E);
5015 end if;
5017 -- For an object that does not have delayed freezing, and whose
5018 -- initialization actions have been captured in a compound
5019 -- statement, move them back now directly within the enclosing
5020 -- statement sequence.
5022 if Ekind_In (E, E_Constant, E_Variable)
5023 and then not Has_Delayed_Freeze (E)
5024 then
5025 Explode_Initialization_Compound_Statement (E);
5026 end if;
5027 end if;
5029 -- Case of a type or subtype being frozen
5031 else
5032 -- We used to check here that a full type must have preelaborable
5033 -- initialization if it completes a private type specified with
5034 -- pragma Preelaborable_Initialization, but that missed cases where
5035 -- the types occur within a generic package, since the freezing
5036 -- that occurs within a containing scope generally skips traversal
5037 -- of a generic unit's declarations (those will be frozen within
5038 -- instances). This check was moved to Analyze_Package_Specification.
5040 -- The type may be defined in a generic unit. This can occur when
5041 -- freezing a generic function that returns the type (which is
5042 -- defined in a parent unit). It is clearly meaningless to freeze
5043 -- this type. However, if it is a subtype, its size may be determi-
5044 -- nable and used in subsequent checks, so might as well try to
5045 -- compute it.
5047 -- In Ada 2012, Freeze_Entities is also used in the front end to
5048 -- trigger the analysis of aspect expressions, so in this case we
5049 -- want to continue the freezing process.
5051 if Present (Scope (E))
5052 and then Is_Generic_Unit (Scope (E))
5053 and then
5054 (not Has_Predicates (E)
5055 and then not Has_Delayed_Freeze (E))
5056 then
5057 Check_Compile_Time_Size (E);
5058 Restore_Globals;
5059 return No_List;
5060 end if;
5062 -- Check for error of Type_Invariant'Class applied to an untagged
5063 -- type (check delayed to freeze time when full type is available).
5065 declare
5066 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
5067 begin
5068 if Present (Prag)
5069 and then Class_Present (Prag)
5070 and then not Is_Tagged_Type (E)
5071 then
5072 Error_Msg_NE
5073 ("Type_Invariant''Class cannot be specified for &",
5074 Prag, E);
5075 Error_Msg_N
5076 ("\can only be specified for a tagged type", Prag);
5077 end if;
5078 end;
5080 -- A Ghost type cannot be effectively volatile (SPARK RM 6.9(8))
5082 if Is_Ghost_Entity (E)
5083 and then Is_Effectively_Volatile (E)
5084 then
5085 Error_Msg_N ("ghost type & cannot be volatile", E);
5086 end if;
5088 -- Deal with special cases of freezing for subtype
5090 if E /= Base_Type (E) then
5092 -- Before we do anything else, a specialized test for the case of
5093 -- a size given for an array where the array needs to be packed,
5094 -- but was not so the size cannot be honored. This is the case
5095 -- where implicit packing may apply. The reason we do this so
5096 -- early is that if we have implicit packing, the layout of the
5097 -- base type is affected, so we must do this before we freeze
5098 -- the base type.
5100 -- We could do this processing only if implicit packing is enabled
5101 -- since in all other cases, the error would be caught by the back
5102 -- end. However, we choose to do the check even if we do not have
5103 -- implicit packing enabled, since this allows us to give a more
5104 -- useful error message (advising use of pragmas Implicit_Packing
5105 -- or Pack).
5107 if Is_Array_Type (E) then
5108 declare
5109 Ctyp : constant Entity_Id := Component_Type (E);
5110 Rsiz : constant Uint := RM_Size (Ctyp);
5111 SZ : constant Node_Id := Size_Clause (E);
5112 Btyp : constant Entity_Id := Base_Type (E);
5114 Lo : Node_Id;
5115 Hi : Node_Id;
5116 Indx : Node_Id;
5118 Num_Elmts : Uint;
5119 -- Number of elements in array
5121 begin
5122 -- Check enabling conditions. These are straightforward
5123 -- except for the test for a limited composite type. This
5124 -- eliminates the rare case of a array of limited components
5125 -- where there are issues of whether or not we can go ahead
5126 -- and pack the array (since we can't freely pack and unpack
5127 -- arrays if they are limited).
5129 -- Note that we check the root type explicitly because the
5130 -- whole point is we are doing this test before we have had
5131 -- a chance to freeze the base type (and it is that freeze
5132 -- action that causes stuff to be inherited).
5134 if Has_Size_Clause (E)
5135 and then Known_Static_RM_Size (E)
5136 and then not Is_Packed (E)
5137 and then not Has_Pragma_Pack (E)
5138 and then not Has_Component_Size_Clause (E)
5139 and then Known_Static_RM_Size (Ctyp)
5140 and then RM_Size (Ctyp) < 64
5141 and then not Is_Limited_Composite (E)
5142 and then not Is_Packed (Root_Type (E))
5143 and then not Has_Component_Size_Clause (Root_Type (E))
5144 and then not (CodePeer_Mode or GNATprove_Mode)
5145 then
5146 -- Compute number of elements in array
5148 Num_Elmts := Uint_1;
5149 Indx := First_Index (E);
5150 while Present (Indx) loop
5151 Get_Index_Bounds (Indx, Lo, Hi);
5153 if not (Compile_Time_Known_Value (Lo)
5154 and then
5155 Compile_Time_Known_Value (Hi))
5156 then
5157 goto No_Implicit_Packing;
5158 end if;
5160 Num_Elmts :=
5161 Num_Elmts *
5162 UI_Max (Uint_0,
5163 Expr_Value (Hi) - Expr_Value (Lo) + 1);
5164 Next_Index (Indx);
5165 end loop;
5167 -- What we are looking for here is the situation where
5168 -- the RM_Size given would be exactly right if there was
5169 -- a pragma Pack (resulting in the component size being
5170 -- the same as the RM_Size). Furthermore, the component
5171 -- type size must be an odd size (not a multiple of
5172 -- storage unit). If the component RM size is an exact
5173 -- number of storage units that is a power of two, the
5174 -- array is not packed and has a standard representation.
5176 if RM_Size (E) = Num_Elmts * Rsiz
5177 and then Rsiz mod System_Storage_Unit /= 0
5178 then
5179 -- For implicit packing mode, just set the component
5180 -- size silently.
5182 if Implicit_Packing then
5183 Set_Component_Size (Btyp, Rsiz);
5184 Set_Is_Bit_Packed_Array (Btyp);
5185 Set_Is_Packed (Btyp);
5186 Set_Has_Non_Standard_Rep (Btyp);
5188 -- Otherwise give an error message
5190 else
5191 Error_Msg_NE
5192 ("size given for& too small", SZ, E);
5193 Error_Msg_N -- CODEFIX
5194 ("\use explicit pragma Pack "
5195 & "or use pragma Implicit_Packing", SZ);
5196 end if;
5198 elsif RM_Size (E) = Num_Elmts * Rsiz
5199 and then Implicit_Packing
5200 and then
5201 (Rsiz / System_Storage_Unit = 1
5202 or else
5203 Rsiz / System_Storage_Unit = 2
5204 or else
5205 Rsiz / System_Storage_Unit = 4)
5206 then
5207 -- Not a packed array, but indicate the desired
5208 -- component size, for the back-end.
5210 Set_Component_Size (Btyp, Rsiz);
5211 end if;
5212 end if;
5213 end;
5214 end if;
5216 <<No_Implicit_Packing>>
5218 -- If ancestor subtype present, freeze that first. Note that this
5219 -- will also get the base type frozen. Need RM reference ???
5221 Atype := Ancestor_Subtype (E);
5223 if Present (Atype) then
5224 Freeze_And_Append (Atype, N, Result);
5226 -- No ancestor subtype present
5228 else
5229 -- See if we have a nearest ancestor that has a predicate.
5230 -- That catches the case of derived type with a predicate.
5231 -- Need RM reference here ???
5233 Atype := Nearest_Ancestor (E);
5235 if Present (Atype) and then Has_Predicates (Atype) then
5236 Freeze_And_Append (Atype, N, Result);
5237 end if;
5239 -- Freeze base type before freezing the entity (RM 13.14(15))
5241 if E /= Base_Type (E) then
5242 Freeze_And_Append (Base_Type (E), N, Result);
5243 end if;
5244 end if;
5246 -- A subtype inherits all the type-related representation aspects
5247 -- from its parents (RM 13.1(8)).
5249 Inherit_Aspects_At_Freeze_Point (E);
5251 -- For a derived type, freeze its parent type first (RM 13.14(15))
5253 elsif Is_Derived_Type (E) then
5254 Freeze_And_Append (Etype (E), N, Result);
5255 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5257 -- A derived type inherits each type-related representation aspect
5258 -- of its parent type that was directly specified before the
5259 -- declaration of the derived type (RM 13.1(15)).
5261 Inherit_Aspects_At_Freeze_Point (E);
5262 end if;
5264 -- Check for incompatible size and alignment for record type
5266 if Warn_On_Size_Alignment
5267 and then Is_Record_Type (E)
5268 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5270 -- If explicit Object_Size clause given assume that the programmer
5271 -- knows what he is doing, and expects the compiler behavior.
5273 and then not Has_Object_Size_Clause (E)
5275 -- Check for size not a multiple of alignment
5277 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5278 then
5279 declare
5280 SC : constant Node_Id := Size_Clause (E);
5281 AC : constant Node_Id := Alignment_Clause (E);
5282 Loc : Node_Id;
5283 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5285 begin
5286 if Present (SC) and then Present (AC) then
5288 -- Give a warning
5290 if Sloc (SC) > Sloc (AC) then
5291 Loc := SC;
5292 Error_Msg_NE
5293 ("?Z?size is not a multiple of alignment for &",
5294 Loc, E);
5295 Error_Msg_Sloc := Sloc (AC);
5296 Error_Msg_Uint_1 := Alignment (E);
5297 Error_Msg_N ("\?Z?alignment of ^ specified #", Loc);
5299 else
5300 Loc := AC;
5301 Error_Msg_NE
5302 ("?Z?size is not a multiple of alignment for &",
5303 Loc, E);
5304 Error_Msg_Sloc := Sloc (SC);
5305 Error_Msg_Uint_1 := RM_Size (E);
5306 Error_Msg_N ("\?Z?size of ^ specified #", Loc);
5307 end if;
5309 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5310 Error_Msg_N ("\?Z?Object_Size will be increased to ^", Loc);
5311 end if;
5312 end;
5313 end if;
5315 -- Array type
5317 if Is_Array_Type (E) then
5318 Freeze_Array_Type (E);
5320 -- For a class-wide type, the corresponding specific type is
5321 -- frozen as well (RM 13.14(15))
5323 elsif Is_Class_Wide_Type (E) then
5324 Freeze_And_Append (Root_Type (E), N, Result);
5326 -- If the base type of the class-wide type is still incomplete,
5327 -- the class-wide remains unfrozen as well. This is legal when
5328 -- E is the formal of a primitive operation of some other type
5329 -- which is being frozen.
5331 if not Is_Frozen (Root_Type (E)) then
5332 Set_Is_Frozen (E, False);
5333 Restore_Globals;
5334 return Result;
5335 end if;
5337 -- The equivalent type associated with a class-wide subtype needs
5338 -- to be frozen to ensure that its layout is done.
5340 if Ekind (E) = E_Class_Wide_Subtype
5341 and then Present (Equivalent_Type (E))
5342 then
5343 Freeze_And_Append (Equivalent_Type (E), N, Result);
5344 end if;
5346 -- Generate an itype reference for a library-level class-wide type
5347 -- at the freeze point. Otherwise the first explicit reference to
5348 -- the type may appear in an inner scope which will be rejected by
5349 -- the back-end.
5351 if Is_Itype (E)
5352 and then Is_Compilation_Unit (Scope (E))
5353 then
5354 declare
5355 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5357 begin
5358 Set_Itype (Ref, E);
5360 -- From a gigi point of view, a class-wide subtype derives
5361 -- from its record equivalent type. As a result, the itype
5362 -- reference must appear after the freeze node of the
5363 -- equivalent type or gigi will reject the reference.
5365 if Ekind (E) = E_Class_Wide_Subtype
5366 and then Present (Equivalent_Type (E))
5367 then
5368 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5369 else
5370 Add_To_Result (Ref);
5371 end if;
5372 end;
5373 end if;
5375 -- For a record type or record subtype, freeze all component types
5376 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5377 -- using Is_Record_Type, because we don't want to attempt the freeze
5378 -- for the case of a private type with record extension (we will do
5379 -- that later when the full type is frozen).
5381 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype)
5382 and then not (Present (Scope (E))
5383 and then Is_Generic_Unit (Scope (E)))
5384 then
5385 Freeze_Record_Type (E);
5387 -- For a concurrent type, freeze corresponding record type. This does
5388 -- not correspond to any specific rule in the RM, but the record type
5389 -- is essentially part of the concurrent type. Also freeze all local
5390 -- entities. This includes record types created for entry parameter
5391 -- blocks and whatever local entities may appear in the private part.
5393 elsif Is_Concurrent_Type (E) then
5394 if Present (Corresponding_Record_Type (E)) then
5395 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5396 end if;
5398 Comp := First_Entity (E);
5399 while Present (Comp) loop
5400 if Is_Type (Comp) then
5401 Freeze_And_Append (Comp, N, Result);
5403 elsif (Ekind (Comp)) /= E_Function then
5405 -- The guard on the presence of the Etype seems to be needed
5406 -- for some CodePeer (-gnatcC) cases, but not clear why???
5408 if Present (Etype (Comp)) then
5409 if Is_Itype (Etype (Comp))
5410 and then Underlying_Type (Scope (Etype (Comp))) = E
5411 then
5412 Undelay_Type (Etype (Comp));
5413 end if;
5415 Freeze_And_Append (Etype (Comp), N, Result);
5416 end if;
5417 end if;
5419 Next_Entity (Comp);
5420 end loop;
5422 -- Private types are required to point to the same freeze node as
5423 -- their corresponding full views. The freeze node itself has to
5424 -- point to the partial view of the entity (because from the partial
5425 -- view, we can retrieve the full view, but not the reverse).
5426 -- However, in order to freeze correctly, we need to freeze the full
5427 -- view. If we are freezing at the end of a scope (or within the
5428 -- scope) of the private type, the partial and full views will have
5429 -- been swapped, the full view appears first in the entity chain and
5430 -- the swapping mechanism ensures that the pointers are properly set
5431 -- (on scope exit).
5433 -- If we encounter the partial view before the full view (e.g. when
5434 -- freezing from another scope), we freeze the full view, and then
5435 -- set the pointers appropriately since we cannot rely on swapping to
5436 -- fix things up (subtypes in an outer scope might not get swapped).
5438 -- If the full view is itself private, the above requirements apply
5439 -- to the underlying full view instead of the full view. But there is
5440 -- no swapping mechanism for the underlying full view so we need to
5441 -- set the pointers appropriately in both cases.
5443 elsif Is_Incomplete_Or_Private_Type (E)
5444 and then not Is_Generic_Type (E)
5445 then
5446 -- The construction of the dispatch table associated with library
5447 -- level tagged types forces freezing of all the primitives of the
5448 -- type, which may cause premature freezing of the partial view.
5449 -- For example:
5451 -- package Pkg is
5452 -- type T is tagged private;
5453 -- type DT is new T with private;
5454 -- procedure Prim (X : in out T; Y : in out DT'Class);
5455 -- private
5456 -- type T is tagged null record;
5457 -- Obj : T;
5458 -- type DT is new T with null record;
5459 -- end;
5461 -- In this case the type will be frozen later by the usual
5462 -- mechanism: an object declaration, an instantiation, or the
5463 -- end of a declarative part.
5465 if Is_Library_Level_Tagged_Type (E)
5466 and then not Present (Full_View (E))
5467 then
5468 Set_Is_Frozen (E, False);
5469 Restore_Globals;
5470 return Result;
5472 -- Case of full view present
5474 elsif Present (Full_View (E)) then
5476 -- If full view has already been frozen, then no further
5477 -- processing is required
5479 if Is_Frozen (Full_View (E)) then
5480 Set_Has_Delayed_Freeze (E, False);
5481 Set_Freeze_Node (E, Empty);
5483 -- Otherwise freeze full view and patch the pointers so that
5484 -- the freeze node will elaborate both views in the back end.
5485 -- However, if full view is itself private, freeze underlying
5486 -- full view instead and patch the pointers so that the freeze
5487 -- node will elaborate the three views in the back end.
5489 else
5490 declare
5491 Full : Entity_Id := Full_View (E);
5493 begin
5494 if Is_Private_Type (Full)
5495 and then Present (Underlying_Full_View (Full))
5496 then
5497 Full := Underlying_Full_View (Full);
5498 end if;
5500 Freeze_And_Append (Full, N, Result);
5502 if Full /= Full_View (E)
5503 and then Has_Delayed_Freeze (Full_View (E))
5504 then
5505 F_Node := Freeze_Node (Full);
5507 if Present (F_Node) then
5508 Set_Freeze_Node (Full_View (E), F_Node);
5509 Set_Entity (F_Node, Full_View (E));
5511 else
5512 Set_Has_Delayed_Freeze (Full_View (E), False);
5513 Set_Freeze_Node (Full_View (E), Empty);
5514 end if;
5515 end if;
5517 if Has_Delayed_Freeze (E) then
5518 F_Node := Freeze_Node (Full_View (E));
5520 if Present (F_Node) then
5521 Set_Freeze_Node (E, F_Node);
5522 Set_Entity (F_Node, E);
5524 else
5525 -- {Incomplete,Private}_Subtypes with Full_Views
5526 -- constrained by discriminants.
5528 Set_Has_Delayed_Freeze (E, False);
5529 Set_Freeze_Node (E, Empty);
5530 end if;
5531 end if;
5532 end;
5533 end if;
5535 Check_Debug_Info_Needed (E);
5537 -- AI-117 requires that the convention of a partial view be the
5538 -- same as the convention of the full view. Note that this is a
5539 -- recognized breach of privacy, but it's essential for logical
5540 -- consistency of representation, and the lack of a rule in
5541 -- RM95 was an oversight.
5543 Set_Convention (E, Convention (Full_View (E)));
5545 Set_Size_Known_At_Compile_Time (E,
5546 Size_Known_At_Compile_Time (Full_View (E)));
5548 -- Size information is copied from the full view to the
5549 -- incomplete or private view for consistency.
5551 -- We skip this is the full view is not a type. This is very
5552 -- strange of course, and can only happen as a result of
5553 -- certain illegalities, such as a premature attempt to derive
5554 -- from an incomplete type.
5556 if Is_Type (Full_View (E)) then
5557 Set_Size_Info (E, Full_View (E));
5558 Set_RM_Size (E, RM_Size (Full_View (E)));
5559 end if;
5561 Restore_Globals;
5562 return Result;
5564 -- Case of underlying full view present
5566 elsif Is_Private_Type (E)
5567 and then Present (Underlying_Full_View (E))
5568 then
5569 if not Is_Frozen (Underlying_Full_View (E)) then
5570 Freeze_And_Append (Underlying_Full_View (E), N, Result);
5571 end if;
5573 -- Patch the pointers so that the freeze node will elaborate
5574 -- both views in the back end.
5576 if Has_Delayed_Freeze (E) then
5577 F_Node := Freeze_Node (Underlying_Full_View (E));
5579 if Present (F_Node) then
5580 Set_Freeze_Node (E, F_Node);
5581 Set_Entity (F_Node, E);
5583 else
5584 Set_Has_Delayed_Freeze (E, False);
5585 Set_Freeze_Node (E, Empty);
5586 end if;
5587 end if;
5589 Check_Debug_Info_Needed (E);
5591 Restore_Globals;
5592 return Result;
5594 -- Case of no full view present. If entity is derived or subtype,
5595 -- it is safe to freeze, correctness depends on the frozen status
5596 -- of parent. Otherwise it is either premature usage, or a Taft
5597 -- amendment type, so diagnosis is at the point of use and the
5598 -- type might be frozen later.
5600 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
5601 null;
5603 else
5604 Set_Is_Frozen (E, False);
5605 Restore_Globals;
5606 return No_List;
5607 end if;
5609 -- For access subprogram, freeze types of all formals, the return
5610 -- type was already frozen, since it is the Etype of the function.
5611 -- Formal types can be tagged Taft amendment types, but otherwise
5612 -- they cannot be incomplete.
5614 elsif Ekind (E) = E_Subprogram_Type then
5615 Formal := First_Formal (E);
5616 while Present (Formal) loop
5617 if Ekind (Etype (Formal)) = E_Incomplete_Type
5618 and then No (Full_View (Etype (Formal)))
5619 and then not Is_Value_Type (Etype (Formal))
5620 then
5621 if Is_Tagged_Type (Etype (Formal)) then
5622 null;
5624 -- AI05-151: Incomplete types are allowed in access to
5625 -- subprogram specifications.
5627 elsif Ada_Version < Ada_2012 then
5628 Error_Msg_NE
5629 ("invalid use of incomplete type&", E, Etype (Formal));
5630 end if;
5631 end if;
5633 Freeze_And_Append (Etype (Formal), N, Result);
5634 Next_Formal (Formal);
5635 end loop;
5637 Freeze_Subprogram (E);
5639 -- For access to a protected subprogram, freeze the equivalent type
5640 -- (however this is not set if we are not generating code or if this
5641 -- is an anonymous type used just for resolution).
5643 elsif Is_Access_Protected_Subprogram_Type (E) then
5644 if Present (Equivalent_Type (E)) then
5645 Freeze_And_Append (Equivalent_Type (E), N, Result);
5646 end if;
5647 end if;
5649 -- Generic types are never seen by the back-end, and are also not
5650 -- processed by the expander (since the expander is turned off for
5651 -- generic processing), so we never need freeze nodes for them.
5653 if Is_Generic_Type (E) then
5654 Restore_Globals;
5655 return Result;
5656 end if;
5658 -- Some special processing for non-generic types to complete
5659 -- representation details not known till the freeze point.
5661 if Is_Fixed_Point_Type (E) then
5662 Freeze_Fixed_Point_Type (E);
5664 -- Some error checks required for ordinary fixed-point type. Defer
5665 -- these till the freeze-point since we need the small and range
5666 -- values. We only do these checks for base types
5668 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
5669 if Small_Value (E) < Ureal_2_M_80 then
5670 Error_Msg_Name_1 := Name_Small;
5671 Error_Msg_N
5672 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
5674 elsif Small_Value (E) > Ureal_2_80 then
5675 Error_Msg_Name_1 := Name_Small;
5676 Error_Msg_N
5677 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
5678 end if;
5680 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
5681 Error_Msg_Name_1 := Name_First;
5682 Error_Msg_N
5683 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
5684 end if;
5686 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
5687 Error_Msg_Name_1 := Name_Last;
5688 Error_Msg_N
5689 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
5690 end if;
5691 end if;
5693 elsif Is_Enumeration_Type (E) then
5694 Freeze_Enumeration_Type (E);
5696 elsif Is_Integer_Type (E) then
5697 Adjust_Esize_For_Alignment (E);
5699 if Is_Modular_Integer_Type (E)
5700 and then Warn_On_Suspicious_Modulus_Value
5701 then
5702 Check_Suspicious_Modulus (E);
5703 end if;
5705 -- The pool applies to named and anonymous access types, but not
5706 -- to subprogram and to internal types generated for 'Access
5707 -- references.
5709 elsif Is_Access_Type (E)
5710 and then not Is_Access_Subprogram_Type (E)
5711 and then Ekind (E) /= E_Access_Attribute_Type
5712 then
5713 -- If a pragma Default_Storage_Pool applies, and this type has no
5714 -- Storage_Pool or Storage_Size clause (which must have occurred
5715 -- before the freezing point), then use the default. This applies
5716 -- only to base types.
5718 -- None of this applies to access to subprograms, for which there
5719 -- are clearly no pools.
5721 if Present (Default_Pool)
5722 and then Is_Base_Type (E)
5723 and then not Has_Storage_Size_Clause (E)
5724 and then No (Associated_Storage_Pool (E))
5725 then
5726 -- Case of pragma Default_Storage_Pool (null)
5728 if Nkind (Default_Pool) = N_Null then
5729 Set_No_Pool_Assigned (E);
5731 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
5733 else
5734 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
5735 end if;
5736 end if;
5738 -- Check restriction for standard storage pool
5740 if No (Associated_Storage_Pool (E)) then
5741 Check_Restriction (No_Standard_Storage_Pools, E);
5742 end if;
5744 -- Deal with error message for pure access type. This is not an
5745 -- error in Ada 2005 if there is no pool (see AI-366).
5747 if Is_Pure_Unit_Access_Type (E)
5748 and then (Ada_Version < Ada_2005
5749 or else not No_Pool_Assigned (E))
5750 and then not Is_Generic_Unit (Scope (E))
5751 then
5752 Error_Msg_N ("named access type not allowed in pure unit", E);
5754 if Ada_Version >= Ada_2005 then
5755 Error_Msg_N
5756 ("\would be legal if Storage_Size of 0 given??", E);
5758 elsif No_Pool_Assigned (E) then
5759 Error_Msg_N
5760 ("\would be legal in Ada 2005??", E);
5762 else
5763 Error_Msg_N
5764 ("\would be legal in Ada 2005 if "
5765 & "Storage_Size of 0 given??", E);
5766 end if;
5767 end if;
5768 end if;
5770 -- Case of composite types
5772 if Is_Composite_Type (E) then
5774 -- AI-117 requires that all new primitives of a tagged type must
5775 -- inherit the convention of the full view of the type. Inherited
5776 -- and overriding operations are defined to inherit the convention
5777 -- of their parent or overridden subprogram (also specified in
5778 -- AI-117), which will have occurred earlier (in Derive_Subprogram
5779 -- and New_Overloaded_Entity). Here we set the convention of
5780 -- primitives that are still convention Ada, which will ensure
5781 -- that any new primitives inherit the type's convention. Class-
5782 -- wide types can have a foreign convention inherited from their
5783 -- specific type, but are excluded from this since they don't have
5784 -- any associated primitives.
5786 if Is_Tagged_Type (E)
5787 and then not Is_Class_Wide_Type (E)
5788 and then Convention (E) /= Convention_Ada
5789 then
5790 declare
5791 Prim_List : constant Elist_Id := Primitive_Operations (E);
5792 Prim : Elmt_Id;
5794 begin
5795 Prim := First_Elmt (Prim_List);
5796 while Present (Prim) loop
5797 if Convention (Node (Prim)) = Convention_Ada then
5798 Set_Convention (Node (Prim), Convention (E));
5799 end if;
5801 Next_Elmt (Prim);
5802 end loop;
5803 end;
5804 end if;
5806 -- If the type is a simple storage pool type, then this is where
5807 -- we attempt to locate and validate its Allocate, Deallocate, and
5808 -- Storage_Size operations (the first is required, and the latter
5809 -- two are optional). We also verify that the full type for a
5810 -- private type is allowed to be a simple storage pool type.
5812 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
5813 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
5814 then
5815 -- If the type is marked Has_Private_Declaration, then this is
5816 -- a full type for a private type that was specified with the
5817 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
5818 -- pragma is allowed for the full type (for example, it can't
5819 -- be an array type, or a nonlimited record type).
5821 if Has_Private_Declaration (E) then
5822 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
5823 and then not Is_Private_Type (E)
5824 then
5825 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
5826 Error_Msg_N
5827 ("pragma% can only apply to full type that is an " &
5828 "explicitly limited type", E);
5829 end if;
5830 end if;
5832 Validate_Simple_Pool_Ops : declare
5833 Pool_Type : Entity_Id renames E;
5834 Address_Type : constant Entity_Id := RTE (RE_Address);
5835 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
5837 procedure Validate_Simple_Pool_Op_Formal
5838 (Pool_Op : Entity_Id;
5839 Pool_Op_Formal : in out Entity_Id;
5840 Expected_Mode : Formal_Kind;
5841 Expected_Type : Entity_Id;
5842 Formal_Name : String;
5843 OK_Formal : in out Boolean);
5844 -- Validate one formal Pool_Op_Formal of the candidate pool
5845 -- operation Pool_Op. The formal must be of Expected_Type
5846 -- and have mode Expected_Mode. OK_Formal will be set to
5847 -- False if the formal doesn't match. If OK_Formal is False
5848 -- on entry, then the formal will effectively be ignored
5849 -- (because validation of the pool op has already failed).
5850 -- Upon return, Pool_Op_Formal will be updated to the next
5851 -- formal, if any.
5853 procedure Validate_Simple_Pool_Operation
5854 (Op_Name : Name_Id);
5855 -- Search for and validate a simple pool operation with the
5856 -- name Op_Name. If the name is Allocate, then there must be
5857 -- exactly one such primitive operation for the simple pool
5858 -- type. If the name is Deallocate or Storage_Size, then
5859 -- there can be at most one such primitive operation. The
5860 -- profile of the located primitive must conform to what
5861 -- is expected for each operation.
5863 ------------------------------------
5864 -- Validate_Simple_Pool_Op_Formal --
5865 ------------------------------------
5867 procedure Validate_Simple_Pool_Op_Formal
5868 (Pool_Op : Entity_Id;
5869 Pool_Op_Formal : in out Entity_Id;
5870 Expected_Mode : Formal_Kind;
5871 Expected_Type : Entity_Id;
5872 Formal_Name : String;
5873 OK_Formal : in out Boolean)
5875 begin
5876 -- If OK_Formal is False on entry, then simply ignore
5877 -- the formal, because an earlier formal has already
5878 -- been flagged.
5880 if not OK_Formal then
5881 return;
5883 -- If no formal is passed in, then issue an error for a
5884 -- missing formal.
5886 elsif not Present (Pool_Op_Formal) then
5887 Error_Msg_NE
5888 ("simple storage pool op missing formal " &
5889 Formal_Name & " of type&", Pool_Op, Expected_Type);
5890 OK_Formal := False;
5892 return;
5893 end if;
5895 if Etype (Pool_Op_Formal) /= Expected_Type then
5897 -- If the pool type was expected for this formal, then
5898 -- this will not be considered a candidate operation
5899 -- for the simple pool, so we unset OK_Formal so that
5900 -- the op and any later formals will be ignored.
5902 if Expected_Type = Pool_Type then
5903 OK_Formal := False;
5905 return;
5907 else
5908 Error_Msg_NE
5909 ("wrong type for formal " & Formal_Name &
5910 " of simple storage pool op; expected type&",
5911 Pool_Op_Formal, Expected_Type);
5912 end if;
5913 end if;
5915 -- Issue error if formal's mode is not the expected one
5917 if Ekind (Pool_Op_Formal) /= Expected_Mode then
5918 Error_Msg_N
5919 ("wrong mode for formal of simple storage pool op",
5920 Pool_Op_Formal);
5921 end if;
5923 -- Advance to the next formal
5925 Next_Formal (Pool_Op_Formal);
5926 end Validate_Simple_Pool_Op_Formal;
5928 ------------------------------------
5929 -- Validate_Simple_Pool_Operation --
5930 ------------------------------------
5932 procedure Validate_Simple_Pool_Operation
5933 (Op_Name : Name_Id)
5935 Op : Entity_Id;
5936 Found_Op : Entity_Id := Empty;
5937 Formal : Entity_Id;
5938 Is_OK : Boolean;
5940 begin
5941 pragma Assert
5942 (Nam_In (Op_Name, Name_Allocate,
5943 Name_Deallocate,
5944 Name_Storage_Size));
5946 Error_Msg_Name_1 := Op_Name;
5948 -- For each homonym declared immediately in the scope
5949 -- of the simple storage pool type, determine whether
5950 -- the homonym is an operation of the pool type, and,
5951 -- if so, check that its profile is as expected for
5952 -- a simple pool operation of that name.
5954 Op := Get_Name_Entity_Id (Op_Name);
5955 while Present (Op) loop
5956 if Ekind_In (Op, E_Function, E_Procedure)
5957 and then Scope (Op) = Current_Scope
5958 then
5959 Formal := First_Entity (Op);
5961 Is_OK := True;
5963 -- The first parameter must be of the pool type
5964 -- in order for the operation to qualify.
5966 if Op_Name = Name_Storage_Size then
5967 Validate_Simple_Pool_Op_Formal
5968 (Op, Formal, E_In_Parameter, Pool_Type,
5969 "Pool", Is_OK);
5970 else
5971 Validate_Simple_Pool_Op_Formal
5972 (Op, Formal, E_In_Out_Parameter, Pool_Type,
5973 "Pool", Is_OK);
5974 end if;
5976 -- If another operation with this name has already
5977 -- been located for the type, then flag an error,
5978 -- since we only allow the type to have a single
5979 -- such primitive.
5981 if Present (Found_Op) and then Is_OK then
5982 Error_Msg_NE
5983 ("only one % operation allowed for " &
5984 "simple storage pool type&", Op, Pool_Type);
5985 end if;
5987 -- In the case of Allocate and Deallocate, a formal
5988 -- of type System.Address is required.
5990 if Op_Name = Name_Allocate then
5991 Validate_Simple_Pool_Op_Formal
5992 (Op, Formal, E_Out_Parameter,
5993 Address_Type, "Storage_Address", Is_OK);
5995 elsif Op_Name = Name_Deallocate then
5996 Validate_Simple_Pool_Op_Formal
5997 (Op, Formal, E_In_Parameter,
5998 Address_Type, "Storage_Address", Is_OK);
5999 end if;
6001 -- In the case of Allocate and Deallocate, formals
6002 -- of type Storage_Count are required as the third
6003 -- and fourth parameters.
6005 if Op_Name /= Name_Storage_Size then
6006 Validate_Simple_Pool_Op_Formal
6007 (Op, Formal, E_In_Parameter,
6008 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
6009 Validate_Simple_Pool_Op_Formal
6010 (Op, Formal, E_In_Parameter,
6011 Stg_Cnt_Type, "Alignment", Is_OK);
6012 end if;
6014 -- If no mismatched formals have been found (Is_OK)
6015 -- and no excess formals are present, then this
6016 -- operation has been validated, so record it.
6018 if not Present (Formal) and then Is_OK then
6019 Found_Op := Op;
6020 end if;
6021 end if;
6023 Op := Homonym (Op);
6024 end loop;
6026 -- There must be a valid Allocate operation for the type,
6027 -- so issue an error if none was found.
6029 if Op_Name = Name_Allocate
6030 and then not Present (Found_Op)
6031 then
6032 Error_Msg_N ("missing % operation for simple " &
6033 "storage pool type", Pool_Type);
6035 elsif Present (Found_Op) then
6037 -- Simple pool operations can't be abstract
6039 if Is_Abstract_Subprogram (Found_Op) then
6040 Error_Msg_N
6041 ("simple storage pool operation must not be " &
6042 "abstract", Found_Op);
6043 end if;
6045 -- The Storage_Size operation must be a function with
6046 -- Storage_Count as its result type.
6048 if Op_Name = Name_Storage_Size then
6049 if Ekind (Found_Op) = E_Procedure then
6050 Error_Msg_N
6051 ("% operation must be a function", Found_Op);
6053 elsif Etype (Found_Op) /= Stg_Cnt_Type then
6054 Error_Msg_NE
6055 ("wrong result type for%, expected type&",
6056 Found_Op, Stg_Cnt_Type);
6057 end if;
6059 -- Allocate and Deallocate must be procedures
6061 elsif Ekind (Found_Op) = E_Function then
6062 Error_Msg_N
6063 ("% operation must be a procedure", Found_Op);
6064 end if;
6065 end if;
6066 end Validate_Simple_Pool_Operation;
6068 -- Start of processing for Validate_Simple_Pool_Ops
6070 begin
6071 Validate_Simple_Pool_Operation (Name_Allocate);
6072 Validate_Simple_Pool_Operation (Name_Deallocate);
6073 Validate_Simple_Pool_Operation (Name_Storage_Size);
6074 end Validate_Simple_Pool_Ops;
6075 end if;
6076 end if;
6078 -- Now that all types from which E may depend are frozen, see if the
6079 -- size is known at compile time, if it must be unsigned, or if
6080 -- strict alignment is required
6082 Check_Compile_Time_Size (E);
6083 Check_Unsigned_Type (E);
6085 if Base_Type (E) = E then
6086 Check_Strict_Alignment (E);
6087 end if;
6089 -- Do not allow a size clause for a type which does not have a size
6090 -- that is known at compile time
6092 if Has_Size_Clause (E)
6093 and then not Size_Known_At_Compile_Time (E)
6094 then
6095 -- Suppress this message if errors posted on E, even if we are
6096 -- in all errors mode, since this is often a junk message
6098 if not Error_Posted (E) then
6099 Error_Msg_N
6100 ("size clause not allowed for variable length type",
6101 Size_Clause (E));
6102 end if;
6103 end if;
6105 -- Now we set/verify the representation information, in particular
6106 -- the size and alignment values. This processing is not required for
6107 -- generic types, since generic types do not play any part in code
6108 -- generation, and so the size and alignment values for such types
6109 -- are irrelevant. Ditto for types declared within a generic unit,
6110 -- which may have components that depend on generic parameters, and
6111 -- that will be recreated in an instance.
6113 if Inside_A_Generic then
6114 null;
6116 -- Otherwise we call the layout procedure
6118 else
6119 Layout_Type (E);
6120 end if;
6122 -- If this is an access to subprogram whose designated type is itself
6123 -- a subprogram type, the return type of this anonymous subprogram
6124 -- type must be decorated as well.
6126 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
6127 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
6128 then
6129 Layout_Type (Etype (Designated_Type (E)));
6130 end if;
6132 -- If the type has a Defaut_Value/Default_Component_Value aspect,
6133 -- this is where we analye the expression (after the type is frozen,
6134 -- since in the case of Default_Value, we are analyzing with the
6135 -- type itself, and we treat Default_Component_Value similarly for
6136 -- the sake of uniformity).
6138 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
6139 declare
6140 Nam : Name_Id;
6141 Exp : Node_Id;
6142 Typ : Entity_Id;
6144 begin
6145 if Is_Scalar_Type (E) then
6146 Nam := Name_Default_Value;
6147 Typ := E;
6148 Exp := Default_Aspect_Value (Typ);
6149 else
6150 Nam := Name_Default_Component_Value;
6151 Typ := Component_Type (E);
6152 Exp := Default_Aspect_Component_Value (E);
6153 end if;
6155 Analyze_And_Resolve (Exp, Typ);
6157 if Etype (Exp) /= Any_Type then
6158 if not Is_OK_Static_Expression (Exp) then
6159 Error_Msg_Name_1 := Nam;
6160 Flag_Non_Static_Expr
6161 ("aspect% requires static expression", Exp);
6162 end if;
6163 end if;
6164 end;
6165 end if;
6167 -- End of freeze processing for type entities
6168 end if;
6170 -- Here is where we logically freeze the current entity. If it has a
6171 -- freeze node, then this is the point at which the freeze node is
6172 -- linked into the result list.
6174 if Has_Delayed_Freeze (E) then
6176 -- If a freeze node is already allocated, use it, otherwise allocate
6177 -- a new one. The preallocation happens in the case of anonymous base
6178 -- types, where we preallocate so that we can set First_Subtype_Link.
6179 -- Note that we reset the Sloc to the current freeze location.
6181 if Present (Freeze_Node (E)) then
6182 F_Node := Freeze_Node (E);
6183 Set_Sloc (F_Node, Loc);
6185 else
6186 F_Node := New_Node (N_Freeze_Entity, Loc);
6187 Set_Freeze_Node (E, F_Node);
6188 Set_Access_Types_To_Process (F_Node, No_Elist);
6189 Set_TSS_Elist (F_Node, No_Elist);
6190 Set_Actions (F_Node, No_List);
6191 end if;
6193 Set_Entity (F_Node, E);
6194 Add_To_Result (F_Node);
6196 -- A final pass over record types with discriminants. If the type
6197 -- has an incomplete declaration, there may be constrained access
6198 -- subtypes declared elsewhere, which do not depend on the discrimi-
6199 -- nants of the type, and which are used as component types (i.e.
6200 -- the full view is a recursive type). The designated types of these
6201 -- subtypes can only be elaborated after the type itself, and they
6202 -- need an itype reference.
6204 if Ekind (E) = E_Record_Type
6205 and then Has_Discriminants (E)
6206 then
6207 declare
6208 Comp : Entity_Id;
6209 IR : Node_Id;
6210 Typ : Entity_Id;
6212 begin
6213 Comp := First_Component (E);
6214 while Present (Comp) loop
6215 Typ := Etype (Comp);
6217 if Ekind (Comp) = E_Component
6218 and then Is_Access_Type (Typ)
6219 and then Scope (Typ) /= E
6220 and then Base_Type (Designated_Type (Typ)) = E
6221 and then Is_Itype (Designated_Type (Typ))
6222 then
6223 IR := Make_Itype_Reference (Sloc (Comp));
6224 Set_Itype (IR, Designated_Type (Typ));
6225 Append (IR, Result);
6226 end if;
6228 Next_Component (Comp);
6229 end loop;
6230 end;
6231 end if;
6232 end if;
6234 -- When a type is frozen, the first subtype of the type is frozen as
6235 -- well (RM 13.14(15)). This has to be done after freezing the type,
6236 -- since obviously the first subtype depends on its own base type.
6238 if Is_Type (E) then
6239 Freeze_And_Append (First_Subtype (E), N, Result);
6241 -- If we just froze a tagged non-class wide record, then freeze the
6242 -- corresponding class-wide type. This must be done after the tagged
6243 -- type itself is frozen, because the class-wide type refers to the
6244 -- tagged type which generates the class.
6246 if Is_Tagged_Type (E)
6247 and then not Is_Class_Wide_Type (E)
6248 and then Present (Class_Wide_Type (E))
6249 then
6250 Freeze_And_Append (Class_Wide_Type (E), N, Result);
6251 end if;
6252 end if;
6254 Check_Debug_Info_Needed (E);
6256 -- Special handling for subprograms
6258 if Is_Subprogram (E) then
6260 -- If subprogram has address clause then reset Is_Public flag, since
6261 -- we do not want the backend to generate external references.
6263 if Present (Address_Clause (E))
6264 and then not Is_Library_Level_Entity (E)
6265 then
6266 Set_Is_Public (E, False);
6267 end if;
6268 end if;
6270 Restore_Globals;
6271 return Result;
6272 end Freeze_Entity;
6274 -----------------------------
6275 -- Freeze_Enumeration_Type --
6276 -----------------------------
6278 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6279 begin
6280 -- By default, if no size clause is present, an enumeration type with
6281 -- Convention C is assumed to interface to a C enum, and has integer
6282 -- size. This applies to types. For subtypes, verify that its base
6283 -- type has no size clause either. Treat other foreign conventions
6284 -- in the same way, and also make sure alignment is set right.
6286 if Has_Foreign_Convention (Typ)
6287 and then not Has_Size_Clause (Typ)
6288 and then not Has_Size_Clause (Base_Type (Typ))
6289 and then Esize (Typ) < Standard_Integer_Size
6291 -- Don't do this if Short_Enums on target
6293 and then not Target_Short_Enums
6294 then
6295 Init_Esize (Typ, Standard_Integer_Size);
6296 Set_Alignment (Typ, Alignment (Standard_Integer));
6298 -- Normal Ada case or size clause present or not Long_C_Enums on target
6300 else
6301 -- If the enumeration type interfaces to C, and it has a size clause
6302 -- that specifies less than int size, it warrants a warning. The
6303 -- user may intend the C type to be an enum or a char, so this is
6304 -- not by itself an error that the Ada compiler can detect, but it
6305 -- it is a worth a heads-up. For Boolean and Character types we
6306 -- assume that the programmer has the proper C type in mind.
6308 if Convention (Typ) = Convention_C
6309 and then Has_Size_Clause (Typ)
6310 and then Esize (Typ) /= Esize (Standard_Integer)
6311 and then not Is_Boolean_Type (Typ)
6312 and then not Is_Character_Type (Typ)
6314 -- Don't do this if Short_Enums on target
6316 and then not Target_Short_Enums
6317 then
6318 Error_Msg_N
6319 ("C enum types have the size of a C int??", Size_Clause (Typ));
6320 end if;
6322 Adjust_Esize_For_Alignment (Typ);
6323 end if;
6324 end Freeze_Enumeration_Type;
6326 -----------------------
6327 -- Freeze_Expression --
6328 -----------------------
6330 procedure Freeze_Expression (N : Node_Id) is
6331 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6332 Typ : Entity_Id;
6333 Nam : Entity_Id;
6334 Desig_Typ : Entity_Id;
6335 P : Node_Id;
6336 Parent_P : Node_Id;
6338 Freeze_Outside : Boolean := False;
6339 -- This flag is set true if the entity must be frozen outside the
6340 -- current subprogram. This happens in the case of expander generated
6341 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6342 -- not freeze all entities like other bodies, but which nevertheless
6343 -- may reference entities that have to be frozen before the body and
6344 -- obviously cannot be frozen inside the body.
6346 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6347 -- If the expression is an array aggregate, the type of the component
6348 -- expressions is also frozen. If the component type is an access type
6349 -- and the expressions include allocators, the designed type is frozen
6350 -- as well.
6352 function In_Expanded_Body (N : Node_Id) return Boolean;
6353 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6354 -- it is the handled statement sequence of an expander-generated
6355 -- subprogram (init proc, stream subprogram, or renaming as body).
6356 -- If so, this is not a freezing context.
6358 -----------------------------------------
6359 -- Find_Aggregate_Component_Desig_Type --
6360 -----------------------------------------
6362 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6363 Assoc : Node_Id;
6364 Exp : Node_Id;
6366 begin
6367 if Present (Expressions (N)) then
6368 Exp := First (Expressions (N));
6369 while Present (Exp) loop
6370 if Nkind (Exp) = N_Allocator then
6371 return Designated_Type (Component_Type (Etype (N)));
6372 end if;
6374 Next (Exp);
6375 end loop;
6376 end if;
6378 if Present (Component_Associations (N)) then
6379 Assoc := First (Component_Associations (N));
6380 while Present (Assoc) loop
6381 if Nkind (Expression (Assoc)) = N_Allocator then
6382 return Designated_Type (Component_Type (Etype (N)));
6383 end if;
6385 Next (Assoc);
6386 end loop;
6387 end if;
6389 return Empty;
6390 end Find_Aggregate_Component_Desig_Type;
6392 ----------------------
6393 -- In_Expanded_Body --
6394 ----------------------
6396 function In_Expanded_Body (N : Node_Id) return Boolean is
6397 P : Node_Id;
6398 Id : Entity_Id;
6400 begin
6401 if Nkind (N) = N_Subprogram_Body then
6402 P := N;
6403 else
6404 P := Parent (N);
6405 end if;
6407 if Nkind (P) /= N_Subprogram_Body then
6408 return False;
6410 else
6411 Id := Defining_Unit_Name (Specification (P));
6413 -- The following are expander-created bodies, or bodies that
6414 -- are not freeze points.
6416 if Nkind (Id) = N_Defining_Identifier
6417 and then (Is_Init_Proc (Id)
6418 or else Is_TSS (Id, TSS_Stream_Input)
6419 or else Is_TSS (Id, TSS_Stream_Output)
6420 or else Is_TSS (Id, TSS_Stream_Read)
6421 or else Is_TSS (Id, TSS_Stream_Write)
6422 or else Nkind_In (Original_Node (P),
6423 N_Subprogram_Renaming_Declaration,
6424 N_Expression_Function))
6425 then
6426 return True;
6427 else
6428 return False;
6429 end if;
6430 end if;
6431 end In_Expanded_Body;
6433 -- Start of processing for Freeze_Expression
6435 begin
6436 -- Immediate return if freezing is inhibited. This flag is set by the
6437 -- analyzer to stop freezing on generated expressions that would cause
6438 -- freezing if they were in the source program, but which are not
6439 -- supposed to freeze, since they are created.
6441 if Must_Not_Freeze (N) then
6442 return;
6443 end if;
6445 -- If expression is non-static, then it does not freeze in a default
6446 -- expression, see section "Handling of Default Expressions" in the
6447 -- spec of package Sem for further details. Note that we have to make
6448 -- sure that we actually have a real expression (if we have a subtype
6449 -- indication, we can't test Is_OK_Static_Expression). However, we
6450 -- exclude the case of the prefix of an attribute of a static scalar
6451 -- subtype from this early return, because static subtype attributes
6452 -- should always cause freezing, even in default expressions, but
6453 -- the attribute may not have been marked as static yet (because in
6454 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
6455 -- Freeze_Expression on the prefix).
6457 if In_Spec_Exp
6458 and then Nkind (N) in N_Subexpr
6459 and then not Is_OK_Static_Expression (N)
6460 and then (Nkind (Parent (N)) /= N_Attribute_Reference
6461 or else not (Is_Entity_Name (N)
6462 and then Is_Type (Entity (N))
6463 and then Is_OK_Static_Subtype (Entity (N))))
6464 then
6465 return;
6466 end if;
6468 -- Freeze type of expression if not frozen already
6470 Typ := Empty;
6472 if Nkind (N) in N_Has_Etype then
6473 if not Is_Frozen (Etype (N)) then
6474 Typ := Etype (N);
6476 -- Base type may be an derived numeric type that is frozen at
6477 -- the point of declaration, but first_subtype is still unfrozen.
6479 elsif not Is_Frozen (First_Subtype (Etype (N))) then
6480 Typ := First_Subtype (Etype (N));
6481 end if;
6482 end if;
6484 -- For entity name, freeze entity if not frozen already. A special
6485 -- exception occurs for an identifier that did not come from source.
6486 -- We don't let such identifiers freeze a non-internal entity, i.e.
6487 -- an entity that did come from source, since such an identifier was
6488 -- generated by the expander, and cannot have any semantic effect on
6489 -- the freezing semantics. For example, this stops the parameter of
6490 -- an initialization procedure from freezing the variable.
6492 if Is_Entity_Name (N)
6493 and then not Is_Frozen (Entity (N))
6494 and then (Nkind (N) /= N_Identifier
6495 or else Comes_From_Source (N)
6496 or else not Comes_From_Source (Entity (N)))
6497 then
6498 Nam := Entity (N);
6500 if Present (Nam) and then Ekind (Nam) = E_Function then
6501 Check_Expression_Function (N, Nam);
6502 end if;
6504 else
6505 Nam := Empty;
6506 end if;
6508 -- For an allocator freeze designated type if not frozen already
6510 -- For an aggregate whose component type is an access type, freeze the
6511 -- designated type now, so that its freeze does not appear within the
6512 -- loop that might be created in the expansion of the aggregate. If the
6513 -- designated type is a private type without full view, the expression
6514 -- cannot contain an allocator, so the type is not frozen.
6516 -- For a function, we freeze the entity when the subprogram declaration
6517 -- is frozen, but a function call may appear in an initialization proc.
6518 -- before the declaration is frozen. We need to generate the extra
6519 -- formals, if any, to ensure that the expansion of the call includes
6520 -- the proper actuals. This only applies to Ada subprograms, not to
6521 -- imported ones.
6523 Desig_Typ := Empty;
6525 case Nkind (N) is
6526 when N_Allocator =>
6527 Desig_Typ := Designated_Type (Etype (N));
6529 when N_Aggregate =>
6530 if Is_Array_Type (Etype (N))
6531 and then Is_Access_Type (Component_Type (Etype (N)))
6532 then
6534 -- Check whether aggregate includes allocators.
6536 Desig_Typ := Find_Aggregate_Component_Desig_Type;
6537 end if;
6539 when N_Selected_Component |
6540 N_Indexed_Component |
6541 N_Slice =>
6543 if Is_Access_Type (Etype (Prefix (N))) then
6544 Desig_Typ := Designated_Type (Etype (Prefix (N)));
6545 end if;
6547 when N_Identifier =>
6548 if Present (Nam)
6549 and then Ekind (Nam) = E_Function
6550 and then Nkind (Parent (N)) = N_Function_Call
6551 and then Convention (Nam) = Convention_Ada
6552 then
6553 Create_Extra_Formals (Nam);
6554 end if;
6556 when others =>
6557 null;
6558 end case;
6560 if Desig_Typ /= Empty
6561 and then (Is_Frozen (Desig_Typ)
6562 or else (not Is_Fully_Defined (Desig_Typ)))
6563 then
6564 Desig_Typ := Empty;
6565 end if;
6567 -- All done if nothing needs freezing
6569 if No (Typ)
6570 and then No (Nam)
6571 and then No (Desig_Typ)
6572 then
6573 return;
6574 end if;
6576 -- Examine the enclosing context by climbing the parent chain. The
6577 -- traversal serves two purposes - to detect scenarios where freezeing
6578 -- is not needed and to find the proper insertion point for the freeze
6579 -- nodes. Although somewhat similar to Insert_Actions, this traversal
6580 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
6581 -- the tree may result in types being frozen too early.
6583 P := N;
6584 loop
6585 Parent_P := Parent (P);
6587 -- If we don't have a parent, then we are not in a well-formed tree.
6588 -- This is an unusual case, but there are some legitimate situations
6589 -- in which this occurs, notably when the expressions in the range of
6590 -- a type declaration are resolved. We simply ignore the freeze
6591 -- request in this case. Is this right ???
6593 if No (Parent_P) then
6594 return;
6595 end if;
6597 -- See if we have got to an appropriate point in the tree
6599 case Nkind (Parent_P) is
6601 -- A special test for the exception of (RM 13.14(8)) for the case
6602 -- of per-object expressions (RM 3.8(18)) occurring in component
6603 -- definition or a discrete subtype definition. Note that we test
6604 -- for a component declaration which includes both cases we are
6605 -- interested in, and furthermore the tree does not have explicit
6606 -- nodes for either of these two constructs.
6608 when N_Component_Declaration =>
6610 -- The case we want to test for here is an identifier that is
6611 -- a per-object expression, this is either a discriminant that
6612 -- appears in a context other than the component declaration
6613 -- or it is a reference to the type of the enclosing construct.
6615 -- For either of these cases, we skip the freezing
6617 if not In_Spec_Expression
6618 and then Nkind (N) = N_Identifier
6619 and then (Present (Entity (N)))
6620 then
6621 -- We recognize the discriminant case by just looking for
6622 -- a reference to a discriminant. It can only be one for
6623 -- the enclosing construct. Skip freezing in this case.
6625 if Ekind (Entity (N)) = E_Discriminant then
6626 return;
6628 -- For the case of a reference to the enclosing record,
6629 -- (or task or protected type), we look for a type that
6630 -- matches the current scope.
6632 elsif Entity (N) = Current_Scope then
6633 return;
6634 end if;
6635 end if;
6637 -- If we have an enumeration literal that appears as the choice in
6638 -- the aggregate of an enumeration representation clause, then
6639 -- freezing does not occur (RM 13.14(10)).
6641 when N_Enumeration_Representation_Clause =>
6643 -- The case we are looking for is an enumeration literal
6645 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
6646 and then Is_Enumeration_Type (Etype (N))
6647 then
6648 -- If enumeration literal appears directly as the choice,
6649 -- do not freeze (this is the normal non-overloaded case)
6651 if Nkind (Parent (N)) = N_Component_Association
6652 and then First (Choices (Parent (N))) = N
6653 then
6654 return;
6656 -- If enumeration literal appears as the name of function
6657 -- which is the choice, then also do not freeze. This
6658 -- happens in the overloaded literal case, where the
6659 -- enumeration literal is temporarily changed to a function
6660 -- call for overloading analysis purposes.
6662 elsif Nkind (Parent (N)) = N_Function_Call
6663 and then
6664 Nkind (Parent (Parent (N))) = N_Component_Association
6665 and then
6666 First (Choices (Parent (Parent (N)))) = Parent (N)
6667 then
6668 return;
6669 end if;
6670 end if;
6672 -- Normally if the parent is a handled sequence of statements,
6673 -- then the current node must be a statement, and that is an
6674 -- appropriate place to insert a freeze node.
6676 when N_Handled_Sequence_Of_Statements =>
6678 -- An exception occurs when the sequence of statements is for
6679 -- an expander generated body that did not do the usual freeze
6680 -- all operation. In this case we usually want to freeze
6681 -- outside this body, not inside it, and we skip past the
6682 -- subprogram body that we are inside.
6684 if In_Expanded_Body (Parent_P) then
6685 declare
6686 Subp : constant Node_Id := Parent (Parent_P);
6687 Spec : Entity_Id;
6689 begin
6690 -- Freeze the entity only when it is declared inside the
6691 -- body of the expander generated procedure. This case
6692 -- is recognized by the scope of the entity or its type,
6693 -- which is either the spec for some enclosing body, or
6694 -- (in the case of init_procs, for which there are no
6695 -- separate specs) the current scope.
6697 if Nkind (Subp) = N_Subprogram_Body then
6698 Spec := Corresponding_Spec (Subp);
6700 if (Present (Typ) and then Scope (Typ) = Spec)
6701 or else
6702 (Present (Nam) and then Scope (Nam) = Spec)
6703 then
6704 exit;
6706 elsif Present (Typ)
6707 and then Scope (Typ) = Current_Scope
6708 and then Defining_Entity (Subp) = Current_Scope
6709 then
6710 exit;
6711 end if;
6712 end if;
6714 -- An expression function may act as a completion of
6715 -- a function declaration. As such, it can reference
6716 -- entities declared between the two views:
6718 -- Hidden []; -- 1
6719 -- function F return ...;
6720 -- private
6721 -- function Hidden return ...;
6722 -- function F return ... is (Hidden); -- 2
6724 -- Refering to the example above, freezing the expression
6725 -- of F (2) would place Hidden's freeze node (1) in the
6726 -- wrong place. Avoid explicit freezing and let the usual
6727 -- scenarios do the job - for example, reaching the end
6728 -- of the private declarations, or a call to F.
6730 if Nkind (Original_Node (Subp)) =
6731 N_Expression_Function
6732 then
6733 null;
6735 -- Freeze outside the body
6737 else
6738 Parent_P := Parent (Parent_P);
6739 Freeze_Outside := True;
6740 end if;
6741 end;
6743 -- Here if normal case where we are in handled statement
6744 -- sequence and want to do the insertion right there.
6746 else
6747 exit;
6748 end if;
6750 -- If parent is a body or a spec or a block, then the current node
6751 -- is a statement or declaration and we can insert the freeze node
6752 -- before it.
6754 when N_Block_Statement |
6755 N_Entry_Body |
6756 N_Package_Body |
6757 N_Package_Specification |
6758 N_Protected_Body |
6759 N_Subprogram_Body |
6760 N_Task_Body => exit;
6762 -- The expander is allowed to define types in any statements list,
6763 -- so any of the following parent nodes also mark a freezing point
6764 -- if the actual node is in a list of statements or declarations.
6766 when N_Abortable_Part |
6767 N_Accept_Alternative |
6768 N_And_Then |
6769 N_Case_Statement_Alternative |
6770 N_Compilation_Unit_Aux |
6771 N_Conditional_Entry_Call |
6772 N_Delay_Alternative |
6773 N_Elsif_Part |
6774 N_Entry_Call_Alternative |
6775 N_Exception_Handler |
6776 N_Extended_Return_Statement |
6777 N_Freeze_Entity |
6778 N_If_Statement |
6779 N_Or_Else |
6780 N_Selective_Accept |
6781 N_Triggering_Alternative =>
6783 exit when Is_List_Member (P);
6785 -- Freeze nodes produced by an expression coming from the Actions
6786 -- list of a N_Expression_With_Actions node must remain within the
6787 -- Actions list. Inserting the freeze nodes further up the tree
6788 -- may lead to use before declaration issues in the case of array
6789 -- types.
6791 when N_Expression_With_Actions =>
6792 if Is_List_Member (P)
6793 and then List_Containing (P) = Actions (Parent_P)
6794 then
6795 exit;
6796 end if;
6798 -- Note: N_Loop_Statement is a special case. A type that appears
6799 -- in the source can never be frozen in a loop (this occurs only
6800 -- because of a loop expanded by the expander), so we keep on
6801 -- going. Otherwise we terminate the search. Same is true of any
6802 -- entity which comes from source. (if they have predefined type,
6803 -- that type does not appear to come from source, but the entity
6804 -- should not be frozen here).
6806 when N_Loop_Statement =>
6807 exit when not Comes_From_Source (Etype (N))
6808 and then (No (Nam) or else not Comes_From_Source (Nam));
6810 -- For all other cases, keep looking at parents
6812 when others =>
6813 null;
6814 end case;
6816 -- We fall through the case if we did not yet find the proper
6817 -- place in the free for inserting the freeze node, so climb.
6819 P := Parent_P;
6820 end loop;
6822 -- If the expression appears in a record or an initialization procedure,
6823 -- the freeze nodes are collected and attached to the current scope, to
6824 -- be inserted and analyzed on exit from the scope, to insure that
6825 -- generated entities appear in the correct scope. If the expression is
6826 -- a default for a discriminant specification, the scope is still void.
6827 -- The expression can also appear in the discriminant part of a private
6828 -- or concurrent type.
6830 -- If the expression appears in a constrained subcomponent of an
6831 -- enclosing record declaration, the freeze nodes must be attached to
6832 -- the outer record type so they can eventually be placed in the
6833 -- enclosing declaration list.
6835 -- The other case requiring this special handling is if we are in a
6836 -- default expression, since in that case we are about to freeze a
6837 -- static type, and the freeze scope needs to be the outer scope, not
6838 -- the scope of the subprogram with the default parameter.
6840 -- For default expressions and other spec expressions in generic units,
6841 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
6842 -- placing them at the proper place, after the generic unit.
6844 if (In_Spec_Exp and not Inside_A_Generic)
6845 or else Freeze_Outside
6846 or else (Is_Type (Current_Scope)
6847 and then (not Is_Concurrent_Type (Current_Scope)
6848 or else not Has_Completion (Current_Scope)))
6849 or else Ekind (Current_Scope) = E_Void
6850 then
6851 declare
6852 N : constant Node_Id := Current_Scope;
6853 Freeze_Nodes : List_Id := No_List;
6854 Pos : Int := Scope_Stack.Last;
6856 begin
6857 if Present (Desig_Typ) then
6858 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
6859 end if;
6861 if Present (Typ) then
6862 Freeze_And_Append (Typ, N, Freeze_Nodes);
6863 end if;
6865 if Present (Nam) then
6866 Freeze_And_Append (Nam, N, Freeze_Nodes);
6867 end if;
6869 -- The current scope may be that of a constrained component of
6870 -- an enclosing record declaration, or of a loop of an enclosing
6871 -- quantified expression, which is above the current scope in the
6872 -- scope stack. Indeed in the context of a quantified expression,
6873 -- a scope is created and pushed above the current scope in order
6874 -- to emulate the loop-like behavior of the quantified expression.
6875 -- If the expression is within a top-level pragma, as for a pre-
6876 -- condition on a library-level subprogram, nothing to do.
6878 if not Is_Compilation_Unit (Current_Scope)
6879 and then (Is_Record_Type (Scope (Current_Scope))
6880 or else Nkind (Parent (Current_Scope)) =
6881 N_Quantified_Expression)
6882 then
6883 Pos := Pos - 1;
6884 end if;
6886 if Is_Non_Empty_List (Freeze_Nodes) then
6887 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
6888 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
6889 Freeze_Nodes;
6890 else
6891 Append_List (Freeze_Nodes,
6892 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
6893 end if;
6894 end if;
6895 end;
6897 return;
6898 end if;
6900 -- Now we have the right place to do the freezing. First, a special
6901 -- adjustment, if we are in spec-expression analysis mode, these freeze
6902 -- actions must not be thrown away (normally all inserted actions are
6903 -- thrown away in this mode. However, the freeze actions are from static
6904 -- expressions and one of the important reasons we are doing this
6905 -- special analysis is to get these freeze actions. Therefore we turn
6906 -- off the In_Spec_Expression mode to propagate these freeze actions.
6907 -- This also means they get properly analyzed and expanded.
6909 In_Spec_Expression := False;
6911 -- Freeze the designated type of an allocator (RM 13.14(13))
6913 if Present (Desig_Typ) then
6914 Freeze_Before (P, Desig_Typ);
6915 end if;
6917 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
6918 -- the enumeration representation clause exception in the loop above.
6920 if Present (Typ) then
6921 Freeze_Before (P, Typ);
6922 end if;
6924 -- Freeze name if one is present (RM 13.14(11))
6926 if Present (Nam) then
6927 Freeze_Before (P, Nam);
6928 end if;
6930 -- Restore In_Spec_Expression flag
6932 In_Spec_Expression := In_Spec_Exp;
6933 end Freeze_Expression;
6935 -----------------------------
6936 -- Freeze_Fixed_Point_Type --
6937 -----------------------------
6939 -- Certain fixed-point types and subtypes, including implicit base types
6940 -- and declared first subtypes, have not yet set up a range. This is
6941 -- because the range cannot be set until the Small and Size values are
6942 -- known, and these are not known till the type is frozen.
6944 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
6945 -- whose bounds are unanalyzed real literals. This routine will recognize
6946 -- this case, and transform this range node into a properly typed range
6947 -- with properly analyzed and resolved values.
6949 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
6950 Rng : constant Node_Id := Scalar_Range (Typ);
6951 Lo : constant Node_Id := Low_Bound (Rng);
6952 Hi : constant Node_Id := High_Bound (Rng);
6953 Btyp : constant Entity_Id := Base_Type (Typ);
6954 Brng : constant Node_Id := Scalar_Range (Btyp);
6955 BLo : constant Node_Id := Low_Bound (Brng);
6956 BHi : constant Node_Id := High_Bound (Brng);
6957 Small : constant Ureal := Small_Value (Typ);
6958 Loval : Ureal;
6959 Hival : Ureal;
6960 Atype : Entity_Id;
6962 Orig_Lo : Ureal;
6963 Orig_Hi : Ureal;
6964 -- Save original bounds (for shaving tests)
6966 Actual_Size : Nat;
6967 -- Actual size chosen
6969 function Fsize (Lov, Hiv : Ureal) return Nat;
6970 -- Returns size of type with given bounds. Also leaves these
6971 -- bounds set as the current bounds of the Typ.
6973 -----------
6974 -- Fsize --
6975 -----------
6977 function Fsize (Lov, Hiv : Ureal) return Nat is
6978 begin
6979 Set_Realval (Lo, Lov);
6980 Set_Realval (Hi, Hiv);
6981 return Minimum_Size (Typ);
6982 end Fsize;
6984 -- Start of processing for Freeze_Fixed_Point_Type
6986 begin
6987 -- If Esize of a subtype has not previously been set, set it now
6989 if Unknown_Esize (Typ) then
6990 Atype := Ancestor_Subtype (Typ);
6992 if Present (Atype) then
6993 Set_Esize (Typ, Esize (Atype));
6994 else
6995 Set_Esize (Typ, Esize (Base_Type (Typ)));
6996 end if;
6997 end if;
6999 -- Immediate return if the range is already analyzed. This means that
7000 -- the range is already set, and does not need to be computed by this
7001 -- routine.
7003 if Analyzed (Rng) then
7004 return;
7005 end if;
7007 -- Immediate return if either of the bounds raises Constraint_Error
7009 if Raises_Constraint_Error (Lo)
7010 or else Raises_Constraint_Error (Hi)
7011 then
7012 return;
7013 end if;
7015 Loval := Realval (Lo);
7016 Hival := Realval (Hi);
7018 Orig_Lo := Loval;
7019 Orig_Hi := Hival;
7021 -- Ordinary fixed-point case
7023 if Is_Ordinary_Fixed_Point_Type (Typ) then
7025 -- For the ordinary fixed-point case, we are allowed to fudge the
7026 -- end-points up or down by small. Generally we prefer to fudge up,
7027 -- i.e. widen the bounds for non-model numbers so that the end points
7028 -- are included. However there are cases in which this cannot be
7029 -- done, and indeed cases in which we may need to narrow the bounds.
7030 -- The following circuit makes the decision.
7032 -- Note: our terminology here is that Incl_EP means that the bounds
7033 -- are widened by Small if necessary to include the end points, and
7034 -- Excl_EP means that the bounds are narrowed by Small to exclude the
7035 -- end-points if this reduces the size.
7037 -- Note that in the Incl case, all we care about is including the
7038 -- end-points. In the Excl case, we want to narrow the bounds as
7039 -- much as permitted by the RM, to give the smallest possible size.
7041 Fudge : declare
7042 Loval_Incl_EP : Ureal;
7043 Hival_Incl_EP : Ureal;
7045 Loval_Excl_EP : Ureal;
7046 Hival_Excl_EP : Ureal;
7048 Size_Incl_EP : Nat;
7049 Size_Excl_EP : Nat;
7051 Model_Num : Ureal;
7052 First_Subt : Entity_Id;
7053 Actual_Lo : Ureal;
7054 Actual_Hi : Ureal;
7056 begin
7057 -- First step. Base types are required to be symmetrical. Right
7058 -- now, the base type range is a copy of the first subtype range.
7059 -- This will be corrected before we are done, but right away we
7060 -- need to deal with the case where both bounds are non-negative.
7061 -- In this case, we set the low bound to the negative of the high
7062 -- bound, to make sure that the size is computed to include the
7063 -- required sign. Note that we do not need to worry about the
7064 -- case of both bounds negative, because the sign will be dealt
7065 -- with anyway. Furthermore we can't just go making such a bound
7066 -- symmetrical, since in a twos-complement system, there is an
7067 -- extra negative value which could not be accommodated on the
7068 -- positive side.
7070 if Typ = Btyp
7071 and then not UR_Is_Negative (Loval)
7072 and then Hival > Loval
7073 then
7074 Loval := -Hival;
7075 Set_Realval (Lo, Loval);
7076 end if;
7078 -- Compute the fudged bounds. If the number is a model number,
7079 -- then we do nothing to include it, but we are allowed to backoff
7080 -- to the next adjacent model number when we exclude it. If it is
7081 -- not a model number then we straddle the two values with the
7082 -- model numbers on either side.
7084 Model_Num := UR_Trunc (Loval / Small) * Small;
7086 if Loval = Model_Num then
7087 Loval_Incl_EP := Model_Num;
7088 else
7089 Loval_Incl_EP := Model_Num - Small;
7090 end if;
7092 -- The low value excluding the end point is Small greater, but
7093 -- we do not do this exclusion if the low value is positive,
7094 -- since it can't help the size and could actually hurt by
7095 -- crossing the high bound.
7097 if UR_Is_Negative (Loval_Incl_EP) then
7098 Loval_Excl_EP := Loval_Incl_EP + Small;
7100 -- If the value went from negative to zero, then we have the
7101 -- case where Loval_Incl_EP is the model number just below
7102 -- zero, so we want to stick to the negative value for the
7103 -- base type to maintain the condition that the size will
7104 -- include signed values.
7106 if Typ = Btyp
7107 and then UR_Is_Zero (Loval_Excl_EP)
7108 then
7109 Loval_Excl_EP := Loval_Incl_EP;
7110 end if;
7112 else
7113 Loval_Excl_EP := Loval_Incl_EP;
7114 end if;
7116 -- Similar processing for upper bound and high value
7118 Model_Num := UR_Trunc (Hival / Small) * Small;
7120 if Hival = Model_Num then
7121 Hival_Incl_EP := Model_Num;
7122 else
7123 Hival_Incl_EP := Model_Num + Small;
7124 end if;
7126 if UR_Is_Positive (Hival_Incl_EP) then
7127 Hival_Excl_EP := Hival_Incl_EP - Small;
7128 else
7129 Hival_Excl_EP := Hival_Incl_EP;
7130 end if;
7132 -- One further adjustment is needed. In the case of subtypes, we
7133 -- cannot go outside the range of the base type, or we get
7134 -- peculiarities, and the base type range is already set. This
7135 -- only applies to the Incl values, since clearly the Excl values
7136 -- are already as restricted as they are allowed to be.
7138 if Typ /= Btyp then
7139 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
7140 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
7141 end if;
7143 -- Get size including and excluding end points
7145 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
7146 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
7148 -- No need to exclude end-points if it does not reduce size
7150 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
7151 Loval_Excl_EP := Loval_Incl_EP;
7152 end if;
7154 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
7155 Hival_Excl_EP := Hival_Incl_EP;
7156 end if;
7158 -- Now we set the actual size to be used. We want to use the
7159 -- bounds fudged up to include the end-points but only if this
7160 -- can be done without violating a specifically given size
7161 -- size clause or causing an unacceptable increase in size.
7163 -- Case of size clause given
7165 if Has_Size_Clause (Typ) then
7167 -- Use the inclusive size only if it is consistent with
7168 -- the explicitly specified size.
7170 if Size_Incl_EP <= RM_Size (Typ) then
7171 Actual_Lo := Loval_Incl_EP;
7172 Actual_Hi := Hival_Incl_EP;
7173 Actual_Size := Size_Incl_EP;
7175 -- If the inclusive size is too large, we try excluding
7176 -- the end-points (will be caught later if does not work).
7178 else
7179 Actual_Lo := Loval_Excl_EP;
7180 Actual_Hi := Hival_Excl_EP;
7181 Actual_Size := Size_Excl_EP;
7182 end if;
7184 -- Case of size clause not given
7186 else
7187 -- If we have a base type whose corresponding first subtype
7188 -- has an explicit size that is large enough to include our
7189 -- end-points, then do so. There is no point in working hard
7190 -- to get a base type whose size is smaller than the specified
7191 -- size of the first subtype.
7193 First_Subt := First_Subtype (Typ);
7195 if Has_Size_Clause (First_Subt)
7196 and then Size_Incl_EP <= Esize (First_Subt)
7197 then
7198 Actual_Size := Size_Incl_EP;
7199 Actual_Lo := Loval_Incl_EP;
7200 Actual_Hi := Hival_Incl_EP;
7202 -- If excluding the end-points makes the size smaller and
7203 -- results in a size of 8,16,32,64, then we take the smaller
7204 -- size. For the 64 case, this is compulsory. For the other
7205 -- cases, it seems reasonable. We like to include end points
7206 -- if we can, but not at the expense of moving to the next
7207 -- natural boundary of size.
7209 elsif Size_Incl_EP /= Size_Excl_EP
7210 and then Addressable (Size_Excl_EP)
7211 then
7212 Actual_Size := Size_Excl_EP;
7213 Actual_Lo := Loval_Excl_EP;
7214 Actual_Hi := Hival_Excl_EP;
7216 -- Otherwise we can definitely include the end points
7218 else
7219 Actual_Size := Size_Incl_EP;
7220 Actual_Lo := Loval_Incl_EP;
7221 Actual_Hi := Hival_Incl_EP;
7222 end if;
7224 -- One pathological case: normally we never fudge a low bound
7225 -- down, since it would seem to increase the size (if it has
7226 -- any effect), but for ranges containing single value, or no
7227 -- values, the high bound can be small too large. Consider:
7229 -- type t is delta 2.0**(-14)
7230 -- range 131072.0 .. 0;
7232 -- That lower bound is *just* outside the range of 32 bits, and
7233 -- does need fudging down in this case. Note that the bounds
7234 -- will always have crossed here, since the high bound will be
7235 -- fudged down if necessary, as in the case of:
7237 -- type t is delta 2.0**(-14)
7238 -- range 131072.0 .. 131072.0;
7240 -- So we detect the situation by looking for crossed bounds,
7241 -- and if the bounds are crossed, and the low bound is greater
7242 -- than zero, we will always back it off by small, since this
7243 -- is completely harmless.
7245 if Actual_Lo > Actual_Hi then
7246 if UR_Is_Positive (Actual_Lo) then
7247 Actual_Lo := Loval_Incl_EP - Small;
7248 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7250 -- And of course, we need to do exactly the same parallel
7251 -- fudge for flat ranges in the negative region.
7253 elsif UR_Is_Negative (Actual_Hi) then
7254 Actual_Hi := Hival_Incl_EP + Small;
7255 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7256 end if;
7257 end if;
7258 end if;
7260 Set_Realval (Lo, Actual_Lo);
7261 Set_Realval (Hi, Actual_Hi);
7262 end Fudge;
7264 -- For the decimal case, none of this fudging is required, since there
7265 -- are no end-point problems in the decimal case (the end-points are
7266 -- always included).
7268 else
7269 Actual_Size := Fsize (Loval, Hival);
7270 end if;
7272 -- At this stage, the actual size has been calculated and the proper
7273 -- required bounds are stored in the low and high bounds.
7275 if Actual_Size > 64 then
7276 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7277 Error_Msg_N
7278 ("size required (^) for type& too large, maximum allowed is 64",
7279 Typ);
7280 Actual_Size := 64;
7281 end if;
7283 -- Check size against explicit given size
7285 if Has_Size_Clause (Typ) then
7286 if Actual_Size > RM_Size (Typ) then
7287 Error_Msg_Uint_1 := RM_Size (Typ);
7288 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7289 Error_Msg_NE
7290 ("size given (^) for type& too small, minimum allowed is ^",
7291 Size_Clause (Typ), Typ);
7293 else
7294 Actual_Size := UI_To_Int (Esize (Typ));
7295 end if;
7297 -- Increase size to next natural boundary if no size clause given
7299 else
7300 if Actual_Size <= 8 then
7301 Actual_Size := 8;
7302 elsif Actual_Size <= 16 then
7303 Actual_Size := 16;
7304 elsif Actual_Size <= 32 then
7305 Actual_Size := 32;
7306 else
7307 Actual_Size := 64;
7308 end if;
7310 Init_Esize (Typ, Actual_Size);
7311 Adjust_Esize_For_Alignment (Typ);
7312 end if;
7314 -- If we have a base type, then expand the bounds so that they extend to
7315 -- the full width of the allocated size in bits, to avoid junk range
7316 -- checks on intermediate computations.
7318 if Base_Type (Typ) = Typ then
7319 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7320 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7321 end if;
7323 -- Final step is to reanalyze the bounds using the proper type
7324 -- and set the Corresponding_Integer_Value fields of the literals.
7326 Set_Etype (Lo, Empty);
7327 Set_Analyzed (Lo, False);
7328 Analyze (Lo);
7330 -- Resolve with universal fixed if the base type, and the base type if
7331 -- it is a subtype. Note we can't resolve the base type with itself,
7332 -- that would be a reference before definition.
7334 if Typ = Btyp then
7335 Resolve (Lo, Universal_Fixed);
7336 else
7337 Resolve (Lo, Btyp);
7338 end if;
7340 -- Set corresponding integer value for bound
7342 Set_Corresponding_Integer_Value
7343 (Lo, UR_To_Uint (Realval (Lo) / Small));
7345 -- Similar processing for high bound
7347 Set_Etype (Hi, Empty);
7348 Set_Analyzed (Hi, False);
7349 Analyze (Hi);
7351 if Typ = Btyp then
7352 Resolve (Hi, Universal_Fixed);
7353 else
7354 Resolve (Hi, Btyp);
7355 end if;
7357 Set_Corresponding_Integer_Value
7358 (Hi, UR_To_Uint (Realval (Hi) / Small));
7360 -- Set type of range to correspond to bounds
7362 Set_Etype (Rng, Etype (Lo));
7364 -- Set Esize to calculated size if not set already
7366 if Unknown_Esize (Typ) then
7367 Init_Esize (Typ, Actual_Size);
7368 end if;
7370 -- Set RM_Size if not already set. If already set, check value
7372 declare
7373 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7375 begin
7376 if RM_Size (Typ) /= Uint_0 then
7377 if RM_Size (Typ) < Minsiz then
7378 Error_Msg_Uint_1 := RM_Size (Typ);
7379 Error_Msg_Uint_2 := Minsiz;
7380 Error_Msg_NE
7381 ("size given (^) for type& too small, minimum allowed is ^",
7382 Size_Clause (Typ), Typ);
7383 end if;
7385 else
7386 Set_RM_Size (Typ, Minsiz);
7387 end if;
7388 end;
7390 -- Check for shaving
7392 if Comes_From_Source (Typ) then
7393 if Orig_Lo < Expr_Value_R (Lo) then
7394 Error_Msg_N
7395 ("declared low bound of type & is outside type range??", Typ);
7396 Error_Msg_N
7397 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
7398 end if;
7400 if Orig_Hi > Expr_Value_R (Hi) then
7401 Error_Msg_N
7402 ("declared high bound of type & is outside type range??", Typ);
7403 Error_Msg_N
7404 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
7405 end if;
7406 end if;
7407 end Freeze_Fixed_Point_Type;
7409 ------------------
7410 -- Freeze_Itype --
7411 ------------------
7413 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7414 L : List_Id;
7416 begin
7417 Set_Has_Delayed_Freeze (T);
7418 L := Freeze_Entity (T, N);
7420 if Is_Non_Empty_List (L) then
7421 Insert_Actions (N, L);
7422 end if;
7423 end Freeze_Itype;
7425 --------------------------
7426 -- Freeze_Static_Object --
7427 --------------------------
7429 procedure Freeze_Static_Object (E : Entity_Id) is
7431 Cannot_Be_Static : exception;
7432 -- Exception raised if the type of a static object cannot be made
7433 -- static. This happens if the type depends on non-global objects.
7435 procedure Ensure_Expression_Is_SA (N : Node_Id);
7436 -- Called to ensure that an expression used as part of a type definition
7437 -- is statically allocatable, which means that the expression type is
7438 -- statically allocatable, and the expression is either static, or a
7439 -- reference to a library level constant.
7441 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7442 -- Called to mark a type as static, checking that it is possible
7443 -- to set the type as static. If it is not possible, then the
7444 -- exception Cannot_Be_Static is raised.
7446 -----------------------------
7447 -- Ensure_Expression_Is_SA --
7448 -----------------------------
7450 procedure Ensure_Expression_Is_SA (N : Node_Id) is
7451 Ent : Entity_Id;
7453 begin
7454 Ensure_Type_Is_SA (Etype (N));
7456 if Is_OK_Static_Expression (N) then
7457 return;
7459 elsif Nkind (N) = N_Identifier then
7460 Ent := Entity (N);
7462 if Present (Ent)
7463 and then Ekind (Ent) = E_Constant
7464 and then Is_Library_Level_Entity (Ent)
7465 then
7466 return;
7467 end if;
7468 end if;
7470 raise Cannot_Be_Static;
7471 end Ensure_Expression_Is_SA;
7473 -----------------------
7474 -- Ensure_Type_Is_SA --
7475 -----------------------
7477 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7478 N : Node_Id;
7479 C : Entity_Id;
7481 begin
7482 -- If type is library level, we are all set
7484 if Is_Library_Level_Entity (Typ) then
7485 return;
7486 end if;
7488 -- We are also OK if the type already marked as statically allocated,
7489 -- which means we processed it before.
7491 if Is_Statically_Allocated (Typ) then
7492 return;
7493 end if;
7495 -- Mark type as statically allocated
7497 Set_Is_Statically_Allocated (Typ);
7499 -- Check that it is safe to statically allocate this type
7501 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
7502 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
7503 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
7505 elsif Is_Array_Type (Typ) then
7506 N := First_Index (Typ);
7507 while Present (N) loop
7508 Ensure_Type_Is_SA (Etype (N));
7509 Next_Index (N);
7510 end loop;
7512 Ensure_Type_Is_SA (Component_Type (Typ));
7514 elsif Is_Access_Type (Typ) then
7515 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
7517 declare
7518 F : Entity_Id;
7519 T : constant Entity_Id := Etype (Designated_Type (Typ));
7521 begin
7522 if T /= Standard_Void_Type then
7523 Ensure_Type_Is_SA (T);
7524 end if;
7526 F := First_Formal (Designated_Type (Typ));
7527 while Present (F) loop
7528 Ensure_Type_Is_SA (Etype (F));
7529 Next_Formal (F);
7530 end loop;
7531 end;
7533 else
7534 Ensure_Type_Is_SA (Designated_Type (Typ));
7535 end if;
7537 elsif Is_Record_Type (Typ) then
7538 C := First_Entity (Typ);
7539 while Present (C) loop
7540 if Ekind (C) = E_Discriminant
7541 or else Ekind (C) = E_Component
7542 then
7543 Ensure_Type_Is_SA (Etype (C));
7545 elsif Is_Type (C) then
7546 Ensure_Type_Is_SA (C);
7547 end if;
7549 Next_Entity (C);
7550 end loop;
7552 elsif Ekind (Typ) = E_Subprogram_Type then
7553 Ensure_Type_Is_SA (Etype (Typ));
7555 C := First_Formal (Typ);
7556 while Present (C) loop
7557 Ensure_Type_Is_SA (Etype (C));
7558 Next_Formal (C);
7559 end loop;
7561 else
7562 raise Cannot_Be_Static;
7563 end if;
7564 end Ensure_Type_Is_SA;
7566 -- Start of processing for Freeze_Static_Object
7568 begin
7569 Ensure_Type_Is_SA (Etype (E));
7571 exception
7572 when Cannot_Be_Static =>
7574 -- If the object that cannot be static is imported or exported, then
7575 -- issue an error message saying that this object cannot be imported
7576 -- or exported. If it has an address clause it is an overlay in the
7577 -- current partition and the static requirement is not relevant.
7578 -- Do not issue any error message when ignoring rep clauses.
7580 if Ignore_Rep_Clauses then
7581 null;
7583 elsif Is_Imported (E) then
7584 if No (Address_Clause (E)) then
7585 Error_Msg_N
7586 ("& cannot be imported (local type is not constant)", E);
7587 end if;
7589 -- Otherwise must be exported, something is wrong if compiler
7590 -- is marking something as statically allocated which cannot be).
7592 else pragma Assert (Is_Exported (E));
7593 Error_Msg_N
7594 ("& cannot be exported (local type is not constant)", E);
7595 end if;
7596 end Freeze_Static_Object;
7598 -----------------------
7599 -- Freeze_Subprogram --
7600 -----------------------
7602 procedure Freeze_Subprogram (E : Entity_Id) is
7603 Retype : Entity_Id;
7604 F : Entity_Id;
7606 begin
7607 -- Subprogram may not have an address clause unless it is imported
7609 if Present (Address_Clause (E)) then
7610 if not Is_Imported (E) then
7611 Error_Msg_N
7612 ("address clause can only be given " &
7613 "for imported subprogram",
7614 Name (Address_Clause (E)));
7615 end if;
7616 end if;
7618 -- Reset the Pure indication on an imported subprogram unless an
7619 -- explicit Pure_Function pragma was present or the subprogram is an
7620 -- intrinsic. We do this because otherwise it is an insidious error
7621 -- to call a non-pure function from pure unit and have calls
7622 -- mysteriously optimized away. What happens here is that the Import
7623 -- can bypass the normal check to ensure that pure units call only pure
7624 -- subprograms.
7626 -- The reason for the intrinsic exception is that in general, intrinsic
7627 -- functions (such as shifts) are pure anyway. The only exceptions are
7628 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
7629 -- in any case, so no problem arises.
7631 if Is_Imported (E)
7632 and then Is_Pure (E)
7633 and then not Has_Pragma_Pure_Function (E)
7634 and then not Is_Intrinsic_Subprogram (E)
7635 then
7636 Set_Is_Pure (E, False);
7637 end if;
7639 -- For non-foreign convention subprograms, this is where we create
7640 -- the extra formals (for accessibility level and constrained bit
7641 -- information). We delay this till the freeze point precisely so
7642 -- that we know the convention.
7644 if not Has_Foreign_Convention (E) then
7645 Create_Extra_Formals (E);
7646 Set_Mechanisms (E);
7648 -- If this is convention Ada and a Valued_Procedure, that's odd
7650 if Ekind (E) = E_Procedure
7651 and then Is_Valued_Procedure (E)
7652 and then Convention (E) = Convention_Ada
7653 and then Warn_On_Export_Import
7654 then
7655 Error_Msg_N
7656 ("??Valued_Procedure has no effect for convention Ada", E);
7657 Set_Is_Valued_Procedure (E, False);
7658 end if;
7660 -- Case of foreign convention
7662 else
7663 Set_Mechanisms (E);
7665 -- For foreign conventions, warn about return of unconstrained array
7667 if Ekind (E) = E_Function then
7668 Retype := Underlying_Type (Etype (E));
7670 -- If no return type, probably some other error, e.g. a
7671 -- missing full declaration, so ignore.
7673 if No (Retype) then
7674 null;
7676 -- If the return type is generic, we have emitted a warning
7677 -- earlier on, and there is nothing else to check here. Specific
7678 -- instantiations may lead to erroneous behavior.
7680 elsif Is_Generic_Type (Etype (E)) then
7681 null;
7683 -- Display warning if returning unconstrained array
7685 elsif Is_Array_Type (Retype)
7686 and then not Is_Constrained (Retype)
7688 -- Check appropriate warning is enabled (should we check for
7689 -- Warnings (Off) on specific entities here, probably so???)
7691 and then Warn_On_Export_Import
7693 -- Exclude the VM case, since return of unconstrained arrays
7694 -- is properly handled in both the JVM and .NET cases.
7696 and then VM_Target = No_VM
7697 then
7698 Error_Msg_N
7699 ("?x?foreign convention function& should not return " &
7700 "unconstrained array", E);
7701 return;
7702 end if;
7703 end if;
7705 -- If any of the formals for an exported foreign convention
7706 -- subprogram have defaults, then emit an appropriate warning since
7707 -- this is odd (default cannot be used from non-Ada code)
7709 if Is_Exported (E) then
7710 F := First_Formal (E);
7711 while Present (F) loop
7712 if Warn_On_Export_Import
7713 and then Present (Default_Value (F))
7714 then
7715 Error_Msg_N
7716 ("?x?parameter cannot be defaulted in non-Ada call",
7717 Default_Value (F));
7718 end if;
7720 Next_Formal (F);
7721 end loop;
7722 end if;
7723 end if;
7725 -- Pragma Inline_Always is disallowed for dispatching subprograms
7726 -- because the address of such subprograms is saved in the dispatch
7727 -- table to support dispatching calls, and dispatching calls cannot
7728 -- be inlined. This is consistent with the restriction against using
7729 -- 'Access or 'Address on an Inline_Always subprogram.
7731 if Is_Dispatching_Operation (E)
7732 and then Has_Pragma_Inline_Always (E)
7733 then
7734 Error_Msg_N
7735 ("pragma Inline_Always not allowed for dispatching subprograms", E);
7736 end if;
7738 -- Because of the implicit representation of inherited predefined
7739 -- operators in the front-end, the overriding status of the operation
7740 -- may be affected when a full view of a type is analyzed, and this is
7741 -- not captured by the analysis of the corresponding type declaration.
7742 -- Therefore the correctness of a not-overriding indicator must be
7743 -- rechecked when the subprogram is frozen.
7745 if Nkind (E) = N_Defining_Operator_Symbol
7746 and then not Error_Posted (Parent (E))
7747 then
7748 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
7749 end if;
7750 end Freeze_Subprogram;
7752 ----------------------
7753 -- Is_Fully_Defined --
7754 ----------------------
7756 function Is_Fully_Defined (T : Entity_Id) return Boolean is
7757 begin
7758 if Ekind (T) = E_Class_Wide_Type then
7759 return Is_Fully_Defined (Etype (T));
7761 elsif Is_Array_Type (T) then
7762 return Is_Fully_Defined (Component_Type (T));
7764 elsif Is_Record_Type (T)
7765 and not Is_Private_Type (T)
7766 then
7767 -- Verify that the record type has no components with private types
7768 -- without completion.
7770 declare
7771 Comp : Entity_Id;
7773 begin
7774 Comp := First_Component (T);
7775 while Present (Comp) loop
7776 if not Is_Fully_Defined (Etype (Comp)) then
7777 return False;
7778 end if;
7780 Next_Component (Comp);
7781 end loop;
7782 return True;
7783 end;
7785 -- For the designated type of an access to subprogram, all types in
7786 -- the profile must be fully defined.
7788 elsif Ekind (T) = E_Subprogram_Type then
7789 declare
7790 F : Entity_Id;
7792 begin
7793 F := First_Formal (T);
7794 while Present (F) loop
7795 if not Is_Fully_Defined (Etype (F)) then
7796 return False;
7797 end if;
7799 Next_Formal (F);
7800 end loop;
7802 return Is_Fully_Defined (Etype (T));
7803 end;
7805 else
7806 return not Is_Private_Type (T)
7807 or else Present (Full_View (Base_Type (T)));
7808 end if;
7809 end Is_Fully_Defined;
7811 ---------------------------------
7812 -- Process_Default_Expressions --
7813 ---------------------------------
7815 procedure Process_Default_Expressions
7816 (E : Entity_Id;
7817 After : in out Node_Id)
7819 Loc : constant Source_Ptr := Sloc (E);
7820 Dbody : Node_Id;
7821 Formal : Node_Id;
7822 Dcopy : Node_Id;
7823 Dnam : Entity_Id;
7825 begin
7826 Set_Default_Expressions_Processed (E);
7828 -- A subprogram instance and its associated anonymous subprogram share
7829 -- their signature. The default expression functions are defined in the
7830 -- wrapper packages for the anonymous subprogram, and should not be
7831 -- generated again for the instance.
7833 if Is_Generic_Instance (E)
7834 and then Present (Alias (E))
7835 and then Default_Expressions_Processed (Alias (E))
7836 then
7837 return;
7838 end if;
7840 Formal := First_Formal (E);
7841 while Present (Formal) loop
7842 if Present (Default_Value (Formal)) then
7844 -- We work with a copy of the default expression because we
7845 -- do not want to disturb the original, since this would mess
7846 -- up the conformance checking.
7848 Dcopy := New_Copy_Tree (Default_Value (Formal));
7850 -- The analysis of the expression may generate insert actions,
7851 -- which of course must not be executed. We wrap those actions
7852 -- in a procedure that is not called, and later on eliminated.
7853 -- The following cases have no side-effects, and are analyzed
7854 -- directly.
7856 if Nkind (Dcopy) = N_Identifier
7857 or else Nkind_In (Dcopy, N_Expanded_Name,
7858 N_Integer_Literal,
7859 N_Character_Literal,
7860 N_String_Literal,
7861 N_Real_Literal)
7862 or else (Nkind (Dcopy) = N_Attribute_Reference
7863 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
7864 or else Known_Null (Dcopy)
7865 then
7866 -- If there is no default function, we must still do a full
7867 -- analyze call on the default value, to ensure that all error
7868 -- checks are performed, e.g. those associated with static
7869 -- evaluation. Note: this branch will always be taken if the
7870 -- analyzer is turned off (but we still need the error checks).
7872 -- Note: the setting of parent here is to meet the requirement
7873 -- that we can only analyze the expression while attached to
7874 -- the tree. Really the requirement is that the parent chain
7875 -- be set, we don't actually need to be in the tree.
7877 Set_Parent (Dcopy, Declaration_Node (Formal));
7878 Analyze (Dcopy);
7880 -- Default expressions are resolved with their own type if the
7881 -- context is generic, to avoid anomalies with private types.
7883 if Ekind (Scope (E)) = E_Generic_Package then
7884 Resolve (Dcopy);
7885 else
7886 Resolve (Dcopy, Etype (Formal));
7887 end if;
7889 -- If that resolved expression will raise constraint error,
7890 -- then flag the default value as raising constraint error.
7891 -- This allows a proper error message on the calls.
7893 if Raises_Constraint_Error (Dcopy) then
7894 Set_Raises_Constraint_Error (Default_Value (Formal));
7895 end if;
7897 -- If the default is a parameterless call, we use the name of
7898 -- the called function directly, and there is no body to build.
7900 elsif Nkind (Dcopy) = N_Function_Call
7901 and then No (Parameter_Associations (Dcopy))
7902 then
7903 null;
7905 -- Else construct and analyze the body of a wrapper procedure
7906 -- that contains an object declaration to hold the expression.
7907 -- Given that this is done only to complete the analysis, it
7908 -- simpler to build a procedure than a function which might
7909 -- involve secondary stack expansion.
7911 else
7912 Dnam := Make_Temporary (Loc, 'D');
7914 Dbody :=
7915 Make_Subprogram_Body (Loc,
7916 Specification =>
7917 Make_Procedure_Specification (Loc,
7918 Defining_Unit_Name => Dnam),
7920 Declarations => New_List (
7921 Make_Object_Declaration (Loc,
7922 Defining_Identifier => Make_Temporary (Loc, 'T'),
7923 Object_Definition =>
7924 New_Occurrence_Of (Etype (Formal), Loc),
7925 Expression => New_Copy_Tree (Dcopy))),
7927 Handled_Statement_Sequence =>
7928 Make_Handled_Sequence_Of_Statements (Loc,
7929 Statements => Empty_List));
7931 Set_Scope (Dnam, Scope (E));
7932 Set_Assignment_OK (First (Declarations (Dbody)));
7933 Set_Is_Eliminated (Dnam);
7934 Insert_After (After, Dbody);
7935 Analyze (Dbody);
7936 After := Dbody;
7937 end if;
7938 end if;
7940 Next_Formal (Formal);
7941 end loop;
7942 end Process_Default_Expressions;
7944 ----------------------------------------
7945 -- Set_Component_Alignment_If_Not_Set --
7946 ----------------------------------------
7948 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
7949 begin
7950 -- Ignore if not base type, subtypes don't need anything
7952 if Typ /= Base_Type (Typ) then
7953 return;
7954 end if;
7956 -- Do not override existing representation
7958 if Is_Packed (Typ) then
7959 return;
7961 elsif Has_Specified_Layout (Typ) then
7962 return;
7964 elsif Component_Alignment (Typ) /= Calign_Default then
7965 return;
7967 else
7968 Set_Component_Alignment
7969 (Typ, Scope_Stack.Table
7970 (Scope_Stack.Last).Component_Alignment_Default);
7971 end if;
7972 end Set_Component_Alignment_If_Not_Set;
7974 --------------------------
7975 -- Set_SSO_From_Default --
7976 --------------------------
7978 procedure Set_SSO_From_Default (T : Entity_Id) is
7979 Reversed : Boolean;
7981 begin
7982 -- Set default SSO for an array or record base type, except in case of
7983 -- a type extension (which always inherits the SSO of its parent type).
7985 if Is_Base_Type (T)
7986 and then (Is_Array_Type (T)
7987 or else (Is_Record_Type (T)
7988 and then not (Is_Tagged_Type (T)
7989 and then Is_Derived_Type (T))))
7990 then
7991 Reversed :=
7992 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
7993 or else
7994 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
7996 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
7998 -- For a record type, if bit order is specified explicitly,
7999 -- then do not set SSO from default if not consistent. Note that
8000 -- we do not want to look at a Bit_Order attribute definition
8001 -- for a parent: if we were to inherit Bit_Order, then both
8002 -- SSO_Set_*_By_Default flags would have been cleared already
8003 -- (by Inherit_Aspects_At_Freeze_Point).
8005 and then not
8006 (Is_Record_Type (T)
8007 and then
8008 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
8009 and then Reverse_Bit_Order (T) /= Reversed)
8010 then
8011 -- If flags cause reverse storage order, then set the result. Note
8012 -- that we would have ignored the pragma setting the non default
8013 -- storage order in any case, hence the assertion at this point.
8015 pragma Assert
8016 (not Reversed or else Support_Nondefault_SSO_On_Target);
8018 Set_Reverse_Storage_Order (T, Reversed);
8020 -- For a record type, also set reversed bit order. Note: if a bit
8021 -- order has been specified explicitly, then this is a no-op.
8023 if Is_Record_Type (T) then
8024 Set_Reverse_Bit_Order (T, Reversed);
8025 end if;
8026 end if;
8027 end if;
8028 end Set_SSO_From_Default;
8030 ------------------
8031 -- Undelay_Type --
8032 ------------------
8034 procedure Undelay_Type (T : Entity_Id) is
8035 begin
8036 Set_Has_Delayed_Freeze (T, False);
8037 Set_Freeze_Node (T, Empty);
8039 -- Since we don't want T to have a Freeze_Node, we don't want its
8040 -- Full_View or Corresponding_Record_Type to have one either.
8042 -- ??? Fundamentally, this whole handling is unpleasant. What we really
8043 -- want is to be sure that for an Itype that's part of record R and is a
8044 -- subtype of type T, that it's frozen after the later of the freeze
8045 -- points of R and T. We have no way of doing that directly, so what we
8046 -- do is force most such Itypes to be frozen as part of freezing R via
8047 -- this procedure and only delay the ones that need to be delayed
8048 -- (mostly the designated types of access types that are defined as part
8049 -- of the record).
8051 if Is_Private_Type (T)
8052 and then Present (Full_View (T))
8053 and then Is_Itype (Full_View (T))
8054 and then Is_Record_Type (Scope (Full_View (T)))
8055 then
8056 Undelay_Type (Full_View (T));
8057 end if;
8059 if Is_Concurrent_Type (T)
8060 and then Present (Corresponding_Record_Type (T))
8061 and then Is_Itype (Corresponding_Record_Type (T))
8062 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
8063 then
8064 Undelay_Type (Corresponding_Record_Type (T));
8065 end if;
8066 end Undelay_Type;
8068 ------------------
8069 -- Warn_Overlay --
8070 ------------------
8072 procedure Warn_Overlay
8073 (Expr : Node_Id;
8074 Typ : Entity_Id;
8075 Nam : Entity_Id)
8077 Ent : constant Entity_Id := Entity (Nam);
8078 -- The object to which the address clause applies
8080 Init : Node_Id;
8081 Old : Entity_Id := Empty;
8082 Decl : Node_Id;
8084 begin
8085 -- No warning if address clause overlay warnings are off
8087 if not Address_Clause_Overlay_Warnings then
8088 return;
8089 end if;
8091 -- No warning if there is an explicit initialization
8093 Init := Original_Node (Expression (Declaration_Node (Ent)));
8095 if Present (Init) and then Comes_From_Source (Init) then
8096 return;
8097 end if;
8099 -- We only give the warning for non-imported entities of a type for
8100 -- which a non-null base init proc is defined, or for objects of access
8101 -- types with implicit null initialization, or when Normalize_Scalars
8102 -- applies and the type is scalar or a string type (the latter being
8103 -- tested for because predefined String types are initialized by inline
8104 -- code rather than by an init_proc). Note that we do not give the
8105 -- warning for Initialize_Scalars, since we suppressed initialization
8106 -- in this case. Also, do not warn if Suppress_Initialization is set.
8108 if Present (Expr)
8109 and then not Is_Imported (Ent)
8110 and then not Initialization_Suppressed (Typ)
8111 and then (Has_Non_Null_Base_Init_Proc (Typ)
8112 or else Is_Access_Type (Typ)
8113 or else (Normalize_Scalars
8114 and then (Is_Scalar_Type (Typ)
8115 or else Is_String_Type (Typ))))
8116 then
8117 if Nkind (Expr) = N_Attribute_Reference
8118 and then Is_Entity_Name (Prefix (Expr))
8119 then
8120 Old := Entity (Prefix (Expr));
8122 elsif Is_Entity_Name (Expr)
8123 and then Ekind (Entity (Expr)) = E_Constant
8124 then
8125 Decl := Declaration_Node (Entity (Expr));
8127 if Nkind (Decl) = N_Object_Declaration
8128 and then Present (Expression (Decl))
8129 and then Nkind (Expression (Decl)) = N_Attribute_Reference
8130 and then Is_Entity_Name (Prefix (Expression (Decl)))
8131 then
8132 Old := Entity (Prefix (Expression (Decl)));
8134 elsif Nkind (Expr) = N_Function_Call then
8135 return;
8136 end if;
8138 -- A function call (most likely to To_Address) is probably not an
8139 -- overlay, so skip warning. Ditto if the function call was inlined
8140 -- and transformed into an entity.
8142 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
8143 return;
8144 end if;
8146 -- If a pragma Import follows, we assume that it is for the current
8147 -- target of the address clause, and skip the warning. There may be
8148 -- a source pragma or an aspect that specifies import and generates
8149 -- the corresponding pragma. These will indicate that the entity is
8150 -- imported and that is checked above so that the spurious warning
8151 -- (generated when the entity is frozen) will be suppressed. The
8152 -- pragma may be attached to the aspect, so it is not yet a list
8153 -- member.
8155 if Is_List_Member (Parent (Expr)) then
8156 Decl := Next (Parent (Expr));
8158 if Present (Decl)
8159 and then Nkind (Decl) = N_Pragma
8160 and then Pragma_Name (Decl) = Name_Import
8161 then
8162 return;
8163 end if;
8164 end if;
8166 -- Otherwise give warning message
8168 if Present (Old) then
8169 Error_Msg_Node_2 := Old;
8170 Error_Msg_N
8171 ("default initialization of & may modify &??",
8172 Nam);
8173 else
8174 Error_Msg_N
8175 ("default initialization of & may modify overlaid storage??",
8176 Nam);
8177 end if;
8179 -- Add friendly warning if initialization comes from a packed array
8180 -- component.
8182 if Is_Record_Type (Typ) then
8183 declare
8184 Comp : Entity_Id;
8186 begin
8187 Comp := First_Component (Typ);
8188 while Present (Comp) loop
8189 if Nkind (Parent (Comp)) = N_Component_Declaration
8190 and then Present (Expression (Parent (Comp)))
8191 then
8192 exit;
8193 elsif Is_Array_Type (Etype (Comp))
8194 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
8195 then
8196 Error_Msg_NE
8197 ("\packed array component& " &
8198 "will be initialized to zero??",
8199 Nam, Comp);
8200 exit;
8201 else
8202 Next_Component (Comp);
8203 end if;
8204 end loop;
8205 end;
8206 end if;
8208 Error_Msg_N
8209 ("\use pragma Import for & to " &
8210 "suppress initialization (RM B.1(24))??",
8211 Nam);
8212 end if;
8213 end Warn_Overlay;
8215 end Freeze;