hppa: Fix ICE caused by mismatched predicate and constraint in xmpyu patterns
[official-gcc.git] / gcc / ada / freeze.adb
blob757c16e68391b8e17ae722da56008d41aade3074
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-2024, 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 Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Ch3; use Exp_Ch3;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Pakd; use Exp_Pakd;
40 with Exp_Util; use Exp_Util;
41 with Exp_Tss; use Exp_Tss;
42 with Ghost; use Ghost;
43 with Layout; use Layout;
44 with Lib; use Lib;
45 with Local_Restrict;
46 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Util; use Sem_Util;
67 with Sinfo; use Sinfo;
68 with Sinfo.Nodes; use Sinfo.Nodes;
69 with Sinfo.Utils; use Sinfo.Utils;
70 with Snames; use Snames;
71 with Stand; use Stand;
72 with Stringt; use Stringt;
73 with Strub; use Strub;
74 with Targparm; use Targparm;
75 with Tbuild; use Tbuild;
76 with Ttypes; use Ttypes;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
81 package body Freeze is
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
87 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
88 -- Typ is a type that is being frozen. If no size clause is given,
89 -- but a default Esize has been computed, then this default Esize is
90 -- adjusted up if necessary to be consistent with a given alignment,
91 -- but never to a value greater than System_Max_Integer_Size. This is
92 -- used for all discrete types and for fixed-point types.
94 procedure Build_And_Analyze_Renamed_Body
95 (Decl : Node_Id;
96 New_S : Entity_Id;
97 After : in out Node_Id);
98 -- Build body for a renaming declaration, insert in tree and analyze
100 procedure Check_Address_Clause (E : Entity_Id);
101 -- Apply legality checks to address clauses for object declarations,
102 -- at the point the object is frozen. Also ensure any initialization is
103 -- performed only after the object has been frozen.
105 procedure Check_Component_Storage_Order
106 (Encl_Type : Entity_Id;
107 Comp : Entity_Id;
108 ADC : Node_Id;
109 Comp_ADC_Present : out Boolean);
110 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
111 -- clause, verify that the component type has an explicit and compatible
112 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
113 -- entity of the component under consideration. For an Encl_Type that
114 -- does not have a Scalar_Storage_Order attribute definition clause,
115 -- verify that the component also does not have such a clause.
116 -- ADC is the attribute definition clause if present (or Empty). On return,
117 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
118 -- attribute definition clause.
120 procedure Check_Debug_Info_Needed (T : Entity_Id);
121 -- As each entity is frozen, this routine is called to deal with the
122 -- setting of Debug_Info_Needed for the entity. This flag is set if
123 -- the entity comes from source, or if we are in Debug_Generated_Code
124 -- mode or if the -gnatdV debug flag is set. However, it never sets
125 -- the flag if Debug_Info_Off is set. This procedure also ensures that
126 -- subsidiary entities have the flag set as required.
128 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
129 -- When an expression function is frozen by a use of it, the expression
130 -- itself is frozen. Check that the expression does not include references
131 -- to deferred constants without completion. We report this at the freeze
132 -- point of the function, to provide a better error message.
134 -- In most cases the expression itself is frozen by the time the function
135 -- itself is frozen, because the formals will be frozen by then. However,
136 -- Attribute references to outer types are freeze points for those types;
137 -- this routine generates the required freeze nodes for them.
139 procedure Check_Strict_Alignment (E : Entity_Id);
140 -- E is a base type. If E is tagged or has a component that is aliased
141 -- or tagged or contains something this is aliased or tagged, set
142 -- Strict_Alignment.
144 procedure Check_Unsigned_Type (E : Entity_Id);
145 pragma Inline (Check_Unsigned_Type);
146 -- If E is a fixed-point or discrete type, then all the necessary work
147 -- to freeze it is completed except for possible setting of the flag
148 -- Is_Unsigned_Type, which is done by this procedure. The call has no
149 -- effect if the entity E is not a discrete or fixed-point type.
151 procedure Freeze_And_Append
152 (Ent : Entity_Id;
153 N : Node_Id;
154 Result : in out List_Id);
155 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
156 -- nodes to Result, modifying Result from No_List if necessary. N has
157 -- the same usage as in Freeze_Entity.
159 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
160 -- Freeze enumeration type. The Esize field is set as processing
161 -- proceeds (i.e. set by default when the type is declared and then
162 -- adjusted by rep clauses). What this procedure does is to make sure
163 -- that if a foreign convention is specified, and no specific size
164 -- is given, then the size must be at least Integer'Size.
166 procedure Freeze_Static_Object (E : Entity_Id);
167 -- If an object is frozen which has Is_Statically_Allocated set, then
168 -- all referenced types must also be marked with this flag. This routine
169 -- is in charge of meeting this requirement for the object entity E.
171 procedure Freeze_Subprogram (E : Entity_Id);
172 -- Perform freezing actions for a subprogram (create extra formals,
173 -- and set proper default mechanism values). Note that this routine
174 -- is not called for internal subprograms, for which neither of these
175 -- actions is needed (or desirable, we do not want for example to have
176 -- these extra formals present in initialization procedures, where they
177 -- would serve no purpose). In this call E is either a subprogram or
178 -- a subprogram type (i.e. an access to a subprogram).
180 function Is_Fully_Defined (T : Entity_Id) return Boolean;
181 -- True if T is not private and has no private components, or has a full
182 -- view. Used to determine whether the designated type of an access type
183 -- should be frozen when the access type is frozen. This is done when an
184 -- allocator is frozen, or an expression that may involve attributes of
185 -- the designated type. Otherwise freezing the access type does not freeze
186 -- the designated type.
188 function Should_Freeze_Type
189 (Typ : Entity_Id; E : Entity_Id; N : Node_Id) return Boolean;
190 -- If Typ is in the current scope, then return True.
191 -- N is a node whose source location corresponds to the freeze point.
192 -- ??? Expression functions (represented by E) shouldn't freeze types in
193 -- general, but our current expansion and freezing model requires an early
194 -- freezing when the dispatch table is needed or when building an aggregate
195 -- with a subtype of Typ, so return True also in this case.
196 -- Note that expression function completions do freeze and are
197 -- handled in Sem_Ch6.Analyze_Expression_Function.
199 ------------------------
200 -- Should_Freeze_Type --
201 ------------------------
203 function Should_Freeze_Type
204 (Typ : Entity_Id; E : Entity_Id; N : Node_Id) return Boolean
206 function Is_Dispatching_Call_Or_Aggregate
207 (N : Node_Id) return Traverse_Result;
208 -- Return Abandon if N is a dispatching call to a subprogram
209 -- declared in the same scope as Typ or an aggregate whose type
210 -- is Typ.
212 --------------------------------------
213 -- Is_Dispatching_Call_Or_Aggregate --
214 --------------------------------------
216 function Is_Dispatching_Call_Or_Aggregate
217 (N : Node_Id) return Traverse_Result is
218 begin
219 if Nkind (N) = N_Function_Call
220 and then Present (Controlling_Argument (N))
221 and then Scope (Entity (Original_Node (Name (N))))
222 = Scope (Typ)
223 then
224 return Abandon;
225 elsif Nkind (N) in N_Aggregate
226 | N_Extension_Aggregate
227 | N_Delta_Aggregate
228 and then Base_Type (Etype (N)) = Base_Type (Typ)
229 then
230 return Abandon;
231 else
232 return OK;
233 end if;
234 end Is_Dispatching_Call_Or_Aggregate;
236 -------------------------
237 -- Need_Dispatch_Table --
238 -------------------------
240 function Need_Dispatch_Table is new
241 Traverse_Func (Is_Dispatching_Call_Or_Aggregate);
242 -- Return Abandon if the input expression requires access to
243 -- Typ's dispatch table.
245 Decl : constant Node_Id :=
246 (if No (E) then E else Original_Node (Unit_Declaration_Node (E)));
248 -- Start of processing for Should_Freeze_Type
250 begin
251 return Within_Scope (Typ, Current_Scope)
252 or else (Nkind (N) = N_Subprogram_Renaming_Declaration
253 and then Present (Corresponding_Formal_Spec (N)))
254 or else (Present (Decl)
255 and then Nkind (Decl) = N_Expression_Function
256 and then Need_Dispatch_Table (Expression (Decl)) = Abandon);
257 end Should_Freeze_Type;
259 procedure Process_Default_Expressions
260 (E : Entity_Id;
261 After : in out Node_Id);
262 -- This procedure is called for each subprogram to complete processing of
263 -- default expressions at the point where all types are known to be frozen.
264 -- The expressions must be analyzed in full, to make sure that all error
265 -- processing is done (they have only been preanalyzed). If the expression
266 -- is not an entity or literal, its analysis may generate code which must
267 -- not be executed. In that case we build a function body to hold that
268 -- code. This wrapper function serves no other purpose (it used to be
269 -- called to evaluate the default, but now the default is inlined at each
270 -- point of call).
272 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
273 -- Typ is a record or array type that is being frozen. This routine sets
274 -- the default component alignment from the scope stack values if the
275 -- alignment is otherwise not specified.
277 procedure Set_SSO_From_Default (T : Entity_Id);
278 -- T is a record or array type that is being frozen. If it is a base type,
279 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
280 -- will be set appropriately. Note that an explicit occurrence of aspect
281 -- Scalar_Storage_Order or an explicit setting of this aspect with an
282 -- attribute definition clause occurs, then these two flags are reset in
283 -- any case, so call will have no effect.
285 procedure Undelay_Type (T : Entity_Id);
286 -- T is a type of a component that we know to be an Itype. We don't want
287 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
288 -- Full_View or Corresponding_Record_Type.
290 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
291 -- Expr is the expression for an address clause for the entity denoted by
292 -- Nam whose type is Typ. If Typ has a default initialization, and there is
293 -- no explicit initialization in the source declaration, check whether the
294 -- address clause might cause overlaying of an entity, and emit a warning
295 -- on the side effect that the initialization will cause.
297 -------------------------------
298 -- Adjust_Esize_For_Alignment --
299 -------------------------------
301 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
302 Align : Uint;
304 begin
305 if Known_Esize (Typ) and then Known_Alignment (Typ) then
306 Align := Alignment_In_Bits (Typ);
308 if Align > Esize (Typ) then
309 if Align > System_Max_Integer_Size then
310 pragma Assert (Serious_Errors_Detected > 0);
311 else
312 Set_Esize (Typ, Align);
313 end if;
314 end if;
315 end if;
316 end Adjust_Esize_For_Alignment;
318 ------------------------------------
319 -- Build_And_Analyze_Renamed_Body --
320 ------------------------------------
322 procedure Build_And_Analyze_Renamed_Body
323 (Decl : Node_Id;
324 New_S : Entity_Id;
325 After : in out Node_Id)
327 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
328 Ent : constant Entity_Id := Defining_Entity (Decl);
329 Body_Node : Node_Id;
330 Renamed_Subp : Entity_Id;
332 begin
333 -- If the renamed subprogram is intrinsic, there is no need for a
334 -- wrapper body: we set the alias that will be called and expanded which
335 -- completes the declaration. This transformation is only legal if the
336 -- renamed entity has already been elaborated.
338 -- Note that it is legal for a renaming_as_body to rename an intrinsic
339 -- subprogram, as long as the renaming occurs before the new entity
340 -- is frozen (RM 8.5.4 (5)).
342 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
343 and then Is_Entity_Name (Name (Body_Decl))
344 then
345 Renamed_Subp := Entity (Name (Body_Decl));
346 else
347 Renamed_Subp := Empty;
348 end if;
350 if Present (Renamed_Subp)
351 and then Is_Intrinsic_Subprogram (Renamed_Subp)
352 and then
353 (not In_Same_Source_Unit (Renamed_Subp, Ent)
354 or else Sloc (Renamed_Subp) < Sloc (Ent))
356 -- We can make the renaming entity intrinsic if the renamed function
357 -- has an interface name, or if it is one of the shift/rotate
358 -- operations known to the compiler.
360 and then
361 (Present (Interface_Name (Renamed_Subp))
362 or else Chars (Renamed_Subp) in Name_Rotate_Left
363 | Name_Rotate_Right
364 | Name_Shift_Left
365 | Name_Shift_Right
366 | Name_Shift_Right_Arithmetic)
367 then
368 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
370 if Present (Alias (Renamed_Subp)) then
371 Set_Alias (Ent, Alias (Renamed_Subp));
372 else
373 Set_Alias (Ent, Renamed_Subp);
374 end if;
376 Set_Is_Intrinsic_Subprogram (Ent);
377 Set_Has_Completion (Ent);
379 else
380 Body_Node := Build_Renamed_Body (Decl, New_S);
381 Insert_After (After, Body_Node);
382 Mark_Rewrite_Insertion (Body_Node);
383 Analyze (Body_Node);
384 After := Body_Node;
385 end if;
386 end Build_And_Analyze_Renamed_Body;
388 ------------------------
389 -- Build_Renamed_Body --
390 ------------------------
392 function Build_Renamed_Body
393 (Decl : Node_Id;
394 New_S : Entity_Id) return Node_Id
396 Loc : constant Source_Ptr := Sloc (New_S);
397 -- We use for the source location of the renamed body, the location of
398 -- the spec entity. It might seem more natural to use the location of
399 -- the renaming declaration itself, but that would be wrong, since then
400 -- the body we create would look as though it was created far too late,
401 -- and this could cause problems with elaboration order analysis,
402 -- particularly in connection with instantiations.
404 N : constant Node_Id := Unit_Declaration_Node (New_S);
405 Nam : constant Node_Id := Name (N);
406 Old_S : Entity_Id;
407 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
408 Actuals : List_Id;
409 Call_Node : Node_Id;
410 Call_Name : Node_Id;
411 Body_Node : Node_Id;
412 Formal : Entity_Id;
413 O_Formal : Entity_Id;
414 Param_Spec : Node_Id;
416 Pref : Node_Id := Empty;
417 -- If the renamed entity is a primitive operation given in prefix form,
418 -- the prefix is the target object and it has to be added as the first
419 -- actual in the generated call.
421 begin
422 -- Determine the entity being renamed, which is the target of the call
423 -- statement. If the name is an explicit dereference, this is a renaming
424 -- of a subprogram type rather than a subprogram. The name itself is
425 -- fully analyzed.
427 if Nkind (Nam) = N_Selected_Component then
428 Old_S := Entity (Selector_Name (Nam));
430 elsif Nkind (Nam) = N_Explicit_Dereference then
431 Old_S := Etype (Nam);
433 elsif Nkind (Nam) = N_Indexed_Component then
434 if Is_Entity_Name (Prefix (Nam)) then
435 Old_S := Entity (Prefix (Nam));
436 else
437 Old_S := Entity (Selector_Name (Prefix (Nam)));
438 end if;
440 elsif Nkind (Nam) = N_Character_Literal then
441 Old_S := Etype (New_S);
443 else
444 Old_S := Entity (Nam);
445 end if;
447 if Is_Entity_Name (Nam) then
449 -- If the renamed entity is a predefined operator, retain full name
450 -- to ensure its visibility.
452 if Ekind (Old_S) = E_Operator
453 and then Nkind (Nam) = N_Expanded_Name
454 then
455 Call_Name := New_Copy (Name (N));
456 else
457 Call_Name := New_Occurrence_Of (Old_S, Loc);
458 end if;
460 else
461 if Nkind (Nam) = N_Selected_Component
462 and then Present (First_Formal (Old_S))
463 and then
464 (Is_Controlling_Formal (First_Formal (Old_S))
465 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
466 then
468 -- Retrieve the target object, to be added as a first actual
469 -- in the call.
471 Call_Name := New_Occurrence_Of (Old_S, Loc);
472 Pref := Prefix (Nam);
474 else
475 Call_Name := New_Copy (Name (N));
476 end if;
478 -- Original name may have been overloaded, but is fully resolved now
480 Set_Is_Overloaded (Call_Name, False);
481 end if;
483 if Nkind (Decl) /= N_Subprogram_Declaration then
484 Rewrite (N,
485 Make_Subprogram_Declaration (Loc,
486 Specification => Specification (N)));
487 end if;
489 -- For simple renamings, subsequent calls can be expanded directly as
490 -- calls to the renamed entity. The body must be generated in any case
491 -- for calls that may appear elsewhere. This is not done in the case
492 -- where the subprogram is an instantiation because the actual proper
493 -- body has not been built yet.
495 if Ekind (Old_S) in E_Function | E_Procedure
496 and then not Is_Generic_Instance (Old_S)
497 then
498 Set_Body_To_Inline (Decl, Old_S);
499 end if;
501 -- Check whether the return type is a limited view. If the subprogram
502 -- is already frozen the generated body may have a non-limited view
503 -- of the type, that must be used, because it is the one in the spec
504 -- of the renaming declaration.
506 if Ekind (Old_S) = E_Function
507 and then Is_Entity_Name (Result_Definition (Spec))
508 then
509 declare
510 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
511 begin
512 if Has_Non_Limited_View (Ret_Type) then
513 Set_Result_Definition
514 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
515 end if;
516 end;
517 end if;
519 -- The body generated for this renaming is an internal artifact, and
520 -- does not constitute a freeze point for the called entity.
522 Set_Must_Not_Freeze (Call_Name);
524 Formal := First_Formal (Defining_Entity (Decl));
526 if Present (Pref) then
527 declare
528 Pref_Type : constant Entity_Id := Etype (Pref);
529 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
531 begin
532 -- The controlling formal may be an access parameter, or the
533 -- actual may be an access value, so adjust accordingly.
535 if Is_Access_Type (Pref_Type)
536 and then not Is_Access_Type (Form_Type)
537 then
538 Actuals := New_List
539 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
541 elsif Is_Access_Type (Form_Type)
542 and then not Is_Access_Type (Pref)
543 then
544 Actuals :=
545 New_List (
546 Make_Attribute_Reference (Loc,
547 Attribute_Name => Name_Access,
548 Prefix => Relocate_Node (Pref)));
549 else
550 Actuals := New_List (Pref);
551 end if;
552 end;
554 elsif Present (Formal) then
555 Actuals := New_List;
557 else
558 Actuals := No_List;
559 end if;
561 while Present (Formal) loop
562 Append (New_Occurrence_Of (Formal, Loc), Actuals);
563 Next_Formal (Formal);
564 end loop;
566 -- If the renamed entity is an entry, inherit its profile. For other
567 -- renamings as bodies, both profiles must be subtype conformant, so it
568 -- is not necessary to replace the profile given in the declaration.
569 -- However, default values that are aggregates are rewritten when
570 -- partially analyzed, so we recover the original aggregate to insure
571 -- that subsequent conformity checking works. Similarly, if the default
572 -- expression was constant-folded, recover the original expression.
574 Formal := First_Formal (Defining_Entity (Decl));
576 if Present (Formal) then
577 O_Formal := First_Formal (Old_S);
578 Param_Spec := First (Parameter_Specifications (Spec));
579 while Present (Formal) loop
580 if Is_Entry (Old_S) then
581 if Nkind (Parameter_Type (Param_Spec)) /=
582 N_Access_Definition
583 then
584 Set_Etype (Formal, Etype (O_Formal));
585 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
586 end if;
588 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
589 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
590 Nkind (Default_Value (O_Formal))
591 then
592 Set_Expression (Param_Spec,
593 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
594 end if;
596 Next_Formal (Formal);
597 Next_Formal (O_Formal);
598 Next (Param_Spec);
599 end loop;
600 end if;
602 -- If the renamed entity is a function, the generated body contains a
603 -- return statement. Otherwise, build a procedure call. If the entity is
604 -- an entry, subsequent analysis of the call will transform it into the
605 -- proper entry or protected operation call. If the renamed entity is
606 -- a character literal, return it directly.
608 if Ekind (Old_S) = E_Function
609 or else Ekind (Old_S) = E_Operator
610 or else (Ekind (Old_S) = E_Subprogram_Type
611 and then Etype (Old_S) /= Standard_Void_Type)
612 then
613 Call_Node :=
614 Make_Simple_Return_Statement (Loc,
615 Expression =>
616 Make_Function_Call (Loc,
617 Name => Call_Name,
618 Parameter_Associations => Actuals));
620 elsif Ekind (Old_S) = E_Enumeration_Literal then
621 Call_Node :=
622 Make_Simple_Return_Statement (Loc,
623 Expression => New_Occurrence_Of (Old_S, Loc));
625 elsif Nkind (Nam) = N_Character_Literal then
626 Call_Node :=
627 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
629 else
630 Call_Node :=
631 Make_Procedure_Call_Statement (Loc,
632 Name => Call_Name,
633 Parameter_Associations => Actuals);
634 end if;
636 -- Create entities for subprogram body and formals
638 Set_Defining_Unit_Name (Spec,
639 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
641 Param_Spec := First (Parameter_Specifications (Spec));
642 while Present (Param_Spec) loop
643 Set_Defining_Identifier (Param_Spec,
644 Make_Defining_Identifier (Loc,
645 Chars => Chars (Defining_Identifier (Param_Spec))));
646 Next (Param_Spec);
647 end loop;
649 -- In GNATprove, prefer to generate an expression function whenever
650 -- possible, to benefit from the more precise analysis in that case
651 -- (as if an implicit postcondition had been generated).
653 if GNATprove_Mode
654 and then Nkind (Call_Node) = N_Simple_Return_Statement
655 then
656 Body_Node :=
657 Make_Expression_Function (Loc,
658 Specification => Spec,
659 Expression => Expression (Call_Node));
660 else
661 Body_Node :=
662 Make_Subprogram_Body (Loc,
663 Specification => Spec,
664 Declarations => New_List,
665 Handled_Statement_Sequence =>
666 Make_Handled_Sequence_Of_Statements (Loc,
667 Statements => New_List (Call_Node)));
668 end if;
670 -- Link the body to the entity whose declaration it completes. If
671 -- the body is analyzed when the renamed entity is frozen, it may
672 -- be necessary to restore the proper scope (see package Exp_Ch13).
674 if Nkind (N) = N_Subprogram_Renaming_Declaration
675 and then Present (Corresponding_Spec (N))
676 then
677 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
678 else
679 Set_Corresponding_Spec (Body_Node, New_S);
680 end if;
682 return Body_Node;
683 end Build_Renamed_Body;
685 --------------------------
686 -- Check_Address_Clause --
687 --------------------------
689 procedure Check_Address_Clause (E : Entity_Id) is
690 Addr : constant Node_Id := Address_Clause (E);
691 Typ : constant Entity_Id := Etype (E);
692 Decl : Node_Id;
693 Expr : Node_Id;
694 Init : Node_Id;
695 Lhs : Node_Id;
696 Tag_Assign : Node_Id;
698 begin
699 if Present (Addr) then
701 -- For a deferred constant, the initialization value is on full view
703 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
704 Decl := Declaration_Node (Full_View (E));
705 else
706 Decl := Declaration_Node (E);
707 end if;
709 Expr := Expression (Addr);
711 if Needs_Constant_Address (Decl, Typ) then
712 Check_Constant_Address_Clause (Expr, E);
714 -- Has_Delayed_Freeze was set on E when the address clause was
715 -- analyzed, and must remain set because we want the address
716 -- clause to be elaborated only after any entity it references
717 -- has been elaborated.
718 end if;
720 -- If Rep_Clauses are to be ignored, remove address clause from
721 -- list attached to entity, because it may be illegal for gigi,
722 -- for example by breaking order of elaboration.
724 if Ignore_Rep_Clauses then
725 declare
726 Rep : Node_Id;
728 begin
729 Rep := First_Rep_Item (E);
731 if Rep = Addr then
732 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
734 else
735 while Present (Rep)
736 and then Next_Rep_Item (Rep) /= Addr
737 loop
738 Next_Rep_Item (Rep);
739 end loop;
740 end if;
742 if Present (Rep) then
743 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
744 end if;
745 end;
747 -- And now remove the address clause
749 Kill_Rep_Clause (Addr);
751 elsif not Error_Posted (Expr)
752 and then not Needs_Finalization (Typ)
753 then
754 Warn_Overlay (Expr, Typ, Name (Addr));
755 end if;
757 Init := Expression (Decl);
759 -- If a variable, or a non-imported constant, overlays a constant
760 -- object and has an initialization value, then the initialization
761 -- may end up writing into read-only memory. Detect the cases of
762 -- statically identical values and remove the initialization. In
763 -- the other cases, give a warning. We will give other warnings
764 -- later for the variable if it is assigned.
766 if (Ekind (E) = E_Variable
767 or else (Ekind (E) = E_Constant
768 and then not Is_Imported (E)))
769 and then Overlays_Constant (E)
770 and then Present (Init)
771 then
772 declare
773 O_Ent : Entity_Id;
774 Off : Boolean;
776 begin
777 Find_Overlaid_Entity (Addr, O_Ent, Off);
779 if Ekind (O_Ent) = E_Constant
780 and then Etype (O_Ent) = Typ
781 and then Present (Constant_Value (O_Ent))
782 and then Compile_Time_Compare
783 (Init,
784 Constant_Value (O_Ent),
785 Assume_Valid => True) = EQ
786 then
787 Set_No_Initialization (Decl);
788 return;
790 elsif Comes_From_Source (Init)
791 and then Address_Clause_Overlay_Warnings
792 then
793 Error_Msg_Sloc := Sloc (Addr);
794 Error_Msg_NE
795 ("?o?constant& may be modified via address clause#",
796 Decl, O_Ent);
797 end if;
798 end;
799 end if;
801 -- Remove side effects from initial expression, except in the case of
802 -- limited build-in-place calls and aggregates, which have their own
803 -- expansion elsewhere. This exception is necessary to avoid copying
804 -- limited objects.
806 if Present (Init)
807 and then not Is_Inherently_Limited_Type (Typ)
808 then
809 -- Capture initialization value at point of declaration, and make
810 -- explicit assignment legal, because object may be a constant.
812 Remove_Side_Effects (Init);
813 Lhs := New_Occurrence_Of (E, Sloc (Decl));
814 Set_Assignment_OK (Lhs);
816 -- Move initialization to freeze actions, once the object has
817 -- been frozen and the address clause alignment check has been
818 -- performed.
820 Append_Freeze_Action (E,
821 Make_Assignment_Statement (Sloc (Decl),
822 Name => Lhs,
823 Expression => Expression (Decl)));
825 Set_No_Initialization (Decl);
827 -- If the object is tagged, check whether the tag must be
828 -- reassigned explicitly.
830 Tag_Assign := Make_Tag_Assignment (Decl);
831 if Present (Tag_Assign) then
832 Append_Freeze_Action (E, Tag_Assign);
833 end if;
834 end if;
835 end if;
836 end Check_Address_Clause;
838 -----------------------------
839 -- Check_Compile_Time_Size --
840 -----------------------------
842 procedure Check_Compile_Time_Size (T : Entity_Id) is
844 procedure Set_Small_Size (T : Entity_Id; S : Uint);
845 -- Sets the compile time known size in the RM_Size field of T, checking
846 -- for a size clause that was given which attempts to give a small size.
848 function Size_Known (T : Entity_Id) return Boolean;
849 -- Recursive function that does all the work
851 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
852 -- If T is a constrained subtype, its size is not known if any of its
853 -- discriminant constraints is not static and it is not a null record.
854 -- The test is conservative and doesn't check that the components are
855 -- in fact constrained by non-static discriminant values. Could be made
856 -- more precise ???
858 --------------------
859 -- Set_Small_Size --
860 --------------------
862 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
863 begin
864 if S > System_Max_Integer_Size then
865 return;
867 -- Check for bad size clause given
869 elsif Has_Size_Clause (T) then
870 if RM_Size (T) < S then
871 Error_Msg_Uint_1 := S;
872 Error_Msg_NE (Size_Too_Small_Message, Size_Clause (T), T);
873 end if;
875 -- Set size if not set already. Do not set it to Uint_0, because in
876 -- some cases (notably array-of-record), the Component_Size is
877 -- No_Uint, which causes S to be Uint_0. Presumably the RM_Size and
878 -- Component_Size will eventually be set correctly by the back end.
880 elsif not Known_RM_Size (T) and then S /= Uint_0 then
881 Set_RM_Size (T, S);
882 end if;
883 end Set_Small_Size;
885 ----------------
886 -- Size_Known --
887 ----------------
889 function Size_Known (T : Entity_Id) return Boolean is
890 Comp : Entity_Id;
891 Ctyp : Entity_Id;
893 begin
894 if Size_Known_At_Compile_Time (T) then
895 return True;
897 -- Always True for elementary types, even generic formal elementary
898 -- types. We used to return False in the latter case, but the size
899 -- is known at compile time, even in the template, we just do not
900 -- know the exact size but that's not the point of this routine.
902 elsif Is_Elementary_Type (T) or else Is_Task_Type (T) then
903 return True;
905 -- Array types
907 elsif Is_Array_Type (T) then
909 -- String literals always have known size, and we can set it
911 if Ekind (T) = E_String_Literal_Subtype then
912 if Known_Component_Size (T) then
913 Set_Small_Size
914 (T, Component_Size (T) * String_Literal_Length (T));
916 else
917 -- The following is wrong, but does what previous versions
918 -- did. The Component_Size is unknown for the string in a
919 -- pragma Warnings.
920 Set_Small_Size (T, Uint_0);
921 end if;
923 return True;
925 -- Unconstrained types never have known at compile time size
927 elsif not Is_Constrained (T) then
928 return False;
930 -- Don't do any recursion on type with error posted, since we may
931 -- have a malformed type that leads us into a loop.
933 elsif Error_Posted (T) then
934 return False;
936 -- Otherwise if component size unknown, then array size unknown
938 elsif not Size_Known (Component_Type (T)) then
939 return False;
940 end if;
942 -- Check for all indexes static, and also compute possible size
943 -- (in case it is not greater than System_Max_Integer_Size and
944 -- thus may be packable).
946 declare
947 Index : Entity_Id;
948 Low : Node_Id;
949 High : Node_Id;
950 Size : Uint := Component_Size (T);
951 Dim : Uint;
953 begin
954 -- See comment in Set_Small_Size above
956 if No (Size) then
957 Size := Uint_0;
958 end if;
960 Index := First_Index (T);
961 while Present (Index) loop
962 if Nkind (Index) = N_Range then
963 Get_Index_Bounds (Index, Low, High);
965 elsif Error_Posted (Scalar_Range (Etype (Index))) then
966 return False;
968 else
969 Low := Type_Low_Bound (Etype (Index));
970 High := Type_High_Bound (Etype (Index));
971 end if;
973 if not Compile_Time_Known_Value (Low)
974 or else not Compile_Time_Known_Value (High)
975 or else Etype (Index) = Any_Type
976 then
977 return False;
979 else
980 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
982 if Dim > Uint_0 then
983 Size := Size * Dim;
984 else
985 Size := Uint_0;
986 end if;
987 end if;
989 Next_Index (Index);
990 end loop;
992 Set_Small_Size (T, Size);
993 return True;
994 end;
996 -- For non-generic private types, go to underlying type if present
998 elsif Is_Private_Type (T)
999 and then not Is_Generic_Type (T)
1000 and then Present (Underlying_Type (T))
1001 then
1002 -- Don't do any recursion on type with error posted, since we may
1003 -- have a malformed type that leads us into a loop.
1005 if Error_Posted (T) then
1006 return False;
1007 else
1008 return Size_Known (Underlying_Type (T));
1009 end if;
1011 -- Record types
1013 elsif Is_Record_Type (T) then
1015 -- A subtype of a variant record must not have non-static
1016 -- discriminated components.
1018 if T /= Base_Type (T)
1019 and then not Static_Discriminated_Components (T)
1020 then
1021 return False;
1023 -- Don't do any recursion on type with error posted, since we may
1024 -- have a malformed type that leads us into a loop.
1026 elsif Error_Posted (T) then
1027 return False;
1028 end if;
1030 -- Now look at the components of the record
1032 declare
1033 -- The following two variables are used to keep track of the
1034 -- size of packed records if we can tell the size of the packed
1035 -- record in the front end. Packed_Size_Known is True if so far
1036 -- we can figure out the size. It is initialized to True for a
1037 -- packed record, unless the record has either discriminants or
1038 -- independent components, or is a strict-alignment type, since
1039 -- it cannot be fully packed in this case.
1041 -- The reason we eliminate the discriminated case is that
1042 -- we don't know the way the back end lays out discriminated
1043 -- packed records. If Packed_Size_Known is True, then
1044 -- Packed_Size is the size in bits so far.
1046 Packed_Size_Known : Boolean :=
1047 Is_Packed (T)
1048 and then not Has_Discriminants (T)
1049 and then not Has_Independent_Components (T)
1050 and then not Strict_Alignment (T);
1052 Packed_Size : Uint := Uint_0;
1053 -- Size in bits so far
1055 begin
1056 -- Test for variant part present
1058 if Has_Discriminants (T)
1059 and then Present (Parent (T))
1060 and then Nkind (Parent (T)) = N_Full_Type_Declaration
1061 and then Nkind (Type_Definition (Parent (T))) =
1062 N_Record_Definition
1063 and then not Null_Present (Type_Definition (Parent (T)))
1064 and then
1065 Present (Variant_Part
1066 (Component_List (Type_Definition (Parent (T)))))
1067 then
1068 -- If variant part is present, and type is unconstrained,
1069 -- then we must have defaulted discriminants, or a size
1070 -- clause must be present for the type, or else the size
1071 -- is definitely not known at compile time.
1073 if not Is_Constrained (T)
1074 and then
1075 No (Discriminant_Default_Value (First_Discriminant (T)))
1076 and then not Known_RM_Size (T)
1077 and then not Known_Esize (T)
1078 then
1079 return False;
1080 end if;
1081 end if;
1083 -- Loop through components
1085 Comp := First_Component_Or_Discriminant (T);
1086 while Present (Comp) loop
1087 Ctyp := Etype (Comp);
1089 -- We do not know the packed size if there is a component
1090 -- clause present (we possibly could, but this would only
1091 -- help in the case of a record with partial rep clauses.
1092 -- That's because in the case of full rep clauses, the
1093 -- size gets figured out anyway by a different circuit).
1095 if Present (Component_Clause (Comp)) then
1096 Packed_Size_Known := False;
1097 end if;
1099 -- We do not know the packed size for an independent
1100 -- component or if it is of a strict-alignment type,
1101 -- since packing does not touch these (RM 13.2(7)).
1103 if Is_Independent (Comp)
1104 or else Is_Independent (Ctyp)
1105 or else Strict_Alignment (Ctyp)
1106 then
1107 Packed_Size_Known := False;
1108 end if;
1110 -- We need to identify a component that is an array where
1111 -- the index type is an enumeration type with non-standard
1112 -- representation, and some bound of the type depends on a
1113 -- discriminant.
1115 -- This is because gigi computes the size by doing a
1116 -- substitution of the appropriate discriminant value in
1117 -- the size expression for the base type, and gigi is not
1118 -- clever enough to evaluate the resulting expression (which
1119 -- involves a call to rep_to_pos) at compile time.
1121 -- It would be nice if gigi would either recognize that
1122 -- this expression can be computed at compile time, or
1123 -- alternatively figured out the size from the subtype
1124 -- directly, where all the information is at hand ???
1126 if Is_Array_Type (Etype (Comp))
1127 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1128 then
1129 declare
1130 Ocomp : constant Entity_Id :=
1131 Original_Record_Component (Comp);
1132 OCtyp : constant Entity_Id := Etype (Ocomp);
1133 Ind : Node_Id;
1134 Indtyp : Entity_Id;
1135 Lo, Hi : Node_Id;
1137 begin
1138 Ind := First_Index (OCtyp);
1139 while Present (Ind) loop
1140 Indtyp := Etype (Ind);
1142 if Is_Enumeration_Type (Indtyp)
1143 and then Has_Non_Standard_Rep (Indtyp)
1144 then
1145 Lo := Type_Low_Bound (Indtyp);
1146 Hi := Type_High_Bound (Indtyp);
1148 if Is_Entity_Name (Lo)
1149 and then Ekind (Entity (Lo)) = E_Discriminant
1150 then
1151 return False;
1153 elsif Is_Entity_Name (Hi)
1154 and then Ekind (Entity (Hi)) = E_Discriminant
1155 then
1156 return False;
1157 end if;
1158 end if;
1160 Next_Index (Ind);
1161 end loop;
1162 end;
1163 end if;
1165 -- Clearly size of record is not known if the size of one of
1166 -- the components is not known.
1168 if not Size_Known (Ctyp) then
1169 return False;
1170 end if;
1172 -- Accumulate packed size if possible
1174 if Packed_Size_Known then
1176 -- We can deal with elementary types, small packed arrays
1177 -- if the representation is a modular type and also small
1178 -- record types as checked by Set_Small_Size.
1180 if Is_Elementary_Type (Ctyp)
1181 or else (Is_Array_Type (Ctyp)
1182 and then Present
1183 (Packed_Array_Impl_Type (Ctyp))
1184 and then Is_Modular_Integer_Type
1185 (Packed_Array_Impl_Type (Ctyp)))
1186 or else Is_Record_Type (Ctyp)
1187 then
1188 -- If RM_Size is known and static, then we can keep
1189 -- accumulating the packed size.
1191 if Known_Static_RM_Size (Ctyp) then
1193 Packed_Size := Packed_Size + RM_Size (Ctyp);
1195 -- If we have a field whose RM_Size is not known then
1196 -- we can't figure out the packed size here.
1198 else
1199 Packed_Size_Known := False;
1200 end if;
1202 -- For other types we can't figure out the packed size
1204 else
1205 Packed_Size_Known := False;
1206 end if;
1207 end if;
1209 Next_Component_Or_Discriminant (Comp);
1210 end loop;
1212 if Packed_Size_Known then
1213 Set_Small_Size (T, Packed_Size);
1214 end if;
1216 return True;
1217 end;
1219 -- All other cases, size not known at compile time
1221 else
1222 return False;
1223 end if;
1224 end Size_Known;
1226 -------------------------------------
1227 -- Static_Discriminated_Components --
1228 -------------------------------------
1230 function Static_Discriminated_Components
1231 (T : Entity_Id) return Boolean
1233 Constraint : Elmt_Id;
1235 begin
1236 if Has_Discriminants (T)
1237 and then Present (Discriminant_Constraint (T))
1238 and then Present (First_Component (T))
1239 then
1240 Constraint := First_Elmt (Discriminant_Constraint (T));
1241 while Present (Constraint) loop
1242 if not Compile_Time_Known_Value (Node (Constraint)) then
1243 return False;
1244 end if;
1246 Next_Elmt (Constraint);
1247 end loop;
1248 end if;
1250 return True;
1251 end Static_Discriminated_Components;
1253 -- Start of processing for Check_Compile_Time_Size
1255 begin
1256 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1257 end Check_Compile_Time_Size;
1259 -----------------------------------
1260 -- Check_Component_Storage_Order --
1261 -----------------------------------
1263 procedure Check_Component_Storage_Order
1264 (Encl_Type : Entity_Id;
1265 Comp : Entity_Id;
1266 ADC : Node_Id;
1267 Comp_ADC_Present : out Boolean)
1269 Comp_Base : Entity_Id;
1270 Comp_ADC : Node_Id;
1271 Encl_Base : Entity_Id;
1272 Err_Node : Node_Id;
1274 Component_Aliased : Boolean;
1276 Comp_Byte_Aligned : Boolean := False;
1277 -- Set for the record case, True if Comp is aligned on byte boundaries
1278 -- (in which case it is allowed to have different storage order).
1280 Comp_SSO_Differs : Boolean;
1281 -- Set True when the component is a nested composite, and it does not
1282 -- have the same scalar storage order as Encl_Type.
1284 begin
1285 -- Record case
1287 if Present (Comp) then
1288 Err_Node := Comp;
1289 Comp_Base := Etype (Comp);
1291 if Is_Tag (Comp) then
1292 Comp_Byte_Aligned := True;
1293 Component_Aliased := False;
1295 else
1296 -- If a component clause is present, check if the component starts
1297 -- and ends on byte boundaries. Otherwise conservatively assume it
1298 -- does so only in the case where the record is not packed.
1300 if Present (Component_Clause (Comp)) then
1301 Comp_Byte_Aligned :=
1302 Known_Normalized_First_Bit (Comp)
1303 and then
1304 Known_Esize (Comp)
1305 and then
1306 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0
1307 and then
1308 Esize (Comp) mod System_Storage_Unit = 0;
1309 else
1310 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1311 end if;
1313 Component_Aliased := Is_Aliased (Comp);
1314 end if;
1316 -- Array case
1318 else
1319 Err_Node := Encl_Type;
1320 Comp_Base := Component_Type (Encl_Type);
1322 Component_Aliased := Has_Aliased_Components (Encl_Type);
1323 end if;
1325 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1326 -- the attribute definition clause is attached to the first subtype.
1327 -- Also, if the base type is incomplete or private, go to full view
1328 -- if known
1330 Encl_Base := Base_Type (Encl_Type);
1331 if Present (Underlying_Type (Encl_Base)) then
1332 Encl_Base := Underlying_Type (Encl_Base);
1333 end if;
1335 Comp_Base := Base_Type (Comp_Base);
1336 if Present (Underlying_Type (Comp_Base)) then
1337 Comp_Base := Underlying_Type (Comp_Base);
1338 end if;
1340 Comp_ADC :=
1341 Get_Attribute_Definition_Clause
1342 (First_Subtype (Comp_Base), Attribute_Scalar_Storage_Order);
1343 Comp_ADC_Present := Present (Comp_ADC);
1345 -- Case of record or array component: check storage order compatibility.
1346 -- But, if the record has Complex_Representation, then it is treated as
1347 -- a scalar in the back end so the storage order is irrelevant.
1349 if (Is_Record_Type (Comp_Base)
1350 and then not Has_Complex_Representation (Comp_Base))
1351 or else Is_Array_Type (Comp_Base)
1352 then
1353 Comp_SSO_Differs :=
1354 Reverse_Storage_Order (Encl_Base) /=
1355 Reverse_Storage_Order (Comp_Base);
1357 -- Parent and extension must have same storage order
1359 if Present (Comp) and then Chars (Comp) = Name_uParent then
1360 if Comp_SSO_Differs then
1361 Error_Msg_N
1362 ("record extension must have same scalar storage order as "
1363 & "parent", Err_Node);
1364 end if;
1366 -- If component and composite SSO differs, check that component
1367 -- falls on byte boundaries and isn't bit packed.
1369 elsif Comp_SSO_Differs then
1371 -- Component SSO differs from enclosing composite:
1373 -- Reject if composite is a bit-packed array, as it is rewritten
1374 -- into an array of scalars.
1376 if Is_Bit_Packed_Array (Encl_Base) then
1377 Error_Msg_N
1378 ("type of packed array must have same scalar storage order "
1379 & "as component", Err_Node);
1381 -- Reject if not byte aligned
1383 elsif Is_Record_Type (Encl_Base)
1384 and then not Comp_Byte_Aligned
1385 then
1386 if Present (Component_Clause (Comp)) then
1387 Error_Msg_N
1388 ("type of non-byte-aligned component must have same scalar"
1389 & " storage order as enclosing record", Err_Node);
1390 else
1391 Error_Msg_N
1392 ("type of packed component must have same scalar"
1393 & " storage order as enclosing record", Err_Node);
1394 end if;
1396 -- Warn if specified only for the outer composite
1398 elsif Present (ADC) and then No (Comp_ADC) then
1399 Error_Msg_NE
1400 ("scalar storage order specified for & does not apply to "
1401 & "component?", Err_Node, Encl_Base);
1402 end if;
1403 end if;
1405 -- Enclosing type has explicit SSO: non-composite component must not
1406 -- be aliased.
1408 elsif Present (ADC) and then Component_Aliased then
1409 Error_Msg_N
1410 ("aliased component not permitted for type with explicit "
1411 & "Scalar_Storage_Order", Err_Node);
1412 end if;
1413 end Check_Component_Storage_Order;
1415 -----------------------------
1416 -- Check_Debug_Info_Needed --
1417 -----------------------------
1419 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1420 begin
1421 if Debug_Info_Off (T) then
1422 return;
1424 elsif Comes_From_Source (T)
1425 or else Debug_Generated_Code
1426 or else Debug_Flag_VV
1427 or else Needs_Debug_Info (T)
1428 then
1429 Set_Debug_Info_Needed (T);
1430 end if;
1431 end Check_Debug_Info_Needed;
1433 -------------------------------
1434 -- Check_Expression_Function --
1435 -------------------------------
1437 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1438 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1439 -- Function to search for deferred constant
1441 -------------------
1442 -- Find_Constant --
1443 -------------------
1445 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1446 begin
1447 -- When a constant is initialized with the result of a dispatching
1448 -- call, the constant declaration is rewritten as a renaming of the
1449 -- displaced function result. This scenario is not a premature use of
1450 -- a constant even though the Has_Completion flag is not set.
1452 if Is_Entity_Name (Nod)
1453 and then Present (Entity (Nod))
1454 and then Ekind (Entity (Nod)) = E_Constant
1455 and then Scope (Entity (Nod)) = Current_Scope
1456 and then Nkind (Declaration_Node (Entity (Nod))) =
1457 N_Object_Declaration
1458 and then not Is_Imported (Entity (Nod))
1459 and then not Has_Completion (Entity (Nod))
1460 and then not (Present (Full_View (Entity (Nod)))
1461 and then Has_Completion (Full_View (Entity (Nod))))
1462 then
1463 Error_Msg_NE
1464 ("premature use of& in call or instance", N, Entity (Nod));
1466 elsif Nkind (Nod) = N_Attribute_Reference then
1467 Analyze (Prefix (Nod));
1469 if Is_Entity_Name (Prefix (Nod))
1470 and then Is_Type (Entity (Prefix (Nod)))
1471 then
1472 if Expander_Active then
1473 Check_Fully_Declared (Entity (Prefix (Nod)), N);
1474 end if;
1476 Freeze_Before (N, Entity (Prefix (Nod)));
1477 end if;
1478 end if;
1480 return OK;
1481 end Find_Constant;
1483 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1485 -- Local variables
1487 Decl : Node_Id;
1489 -- Start of processing for Check_Expression_Function
1491 begin
1492 Decl := Original_Node (Unit_Declaration_Node (Nam));
1494 -- The subprogram body created for the expression function is not
1495 -- itself a freeze point.
1497 if Scope (Nam) = Current_Scope
1498 and then Nkind (Decl) = N_Expression_Function
1499 and then Nkind (N) /= N_Subprogram_Body
1500 then
1501 Check_Deferred (Expression (Decl));
1502 end if;
1503 end Check_Expression_Function;
1505 --------------------------------
1506 -- Check_Inherited_Conditions --
1507 --------------------------------
1509 procedure Check_Inherited_Conditions
1510 (R : Entity_Id;
1511 Late_Overriding : Boolean := False)
1513 Prim_Ops : constant Elist_Id := Primitive_Operations (R);
1514 Decls : List_Id;
1515 Op_Node : Elmt_Id;
1516 Par_Prim : Entity_Id;
1517 Prim : Entity_Id;
1519 type Wrapper_Kind is (No_Wrapper, LSP_Wrapper, Postcond_Wrapper);
1521 Wrapper_Needed : Wrapper_Kind;
1522 -- Kind of wrapper needed by a given inherited primitive of tagged
1523 -- type R:
1524 -- * No_Wrapper: No wrapper is needed.
1525 -- * LSP_Wrapper: Wrapper that handles inherited class-wide pre/post
1526 -- conditions that call overridden primitives.
1527 -- * Postcond_Wrapper: Wrapper that handles postconditions of interface
1528 -- primitives.
1530 function Build_DTW_Body
1531 (Loc : Source_Ptr;
1532 DTW_Spec : Node_Id;
1533 DTW_Decls : List_Id;
1534 Par_Prim : Entity_Id;
1535 Wrapped_Subp : Entity_Id) return Node_Id;
1536 -- Build the body of the dispatch table wrapper containing the given
1537 -- spec and declarations; the call to the wrapped subprogram includes
1538 -- the proper type conversion.
1540 function Build_DTW_Spec (Par_Prim : Entity_Id) return Node_Id;
1541 -- Build the spec of the dispatch table wrapper
1543 procedure Build_Inherited_Condition_Pragmas
1544 (Subp : Entity_Id;
1545 LSP_Wrapper_Needed : out Boolean);
1546 -- Build corresponding pragmas for an operation whose ancestor has
1547 -- class-wide pre/postconditions. If the operation is inherited then
1548 -- Wrapper_Needed is returned True to force the creation of a wrapper
1549 -- for the inherited operation. If the ancestor is being overridden,
1550 -- the pragmas are constructed only to verify their legality, in case
1551 -- they contain calls to other primitives that may have been overridden.
1553 procedure Check_Interface_Primitives_Strub_Mode;
1554 -- Called when R is an interface type to check strub mode compatibility
1555 -- all its primitives.
1557 function Needs_Wrapper
1558 (Class_Cond : Node_Id;
1559 Subp : Entity_Id;
1560 Par_Subp : Entity_Id) return Boolean;
1561 -- Checks whether the dispatch-table wrapper (DTW) for Subp must be
1562 -- built to evaluate the given class-wide condition.
1564 --------------------
1565 -- Build_DTW_Body --
1566 --------------------
1568 function Build_DTW_Body
1569 (Loc : Source_Ptr;
1570 DTW_Spec : Node_Id;
1571 DTW_Decls : List_Id;
1572 Par_Prim : Entity_Id;
1573 Wrapped_Subp : Entity_Id) return Node_Id
1575 Actuals : constant List_Id := Empty_List;
1576 Call : Node_Id;
1577 Formal : Entity_Id := First_Formal (Par_Prim);
1578 New_F_Spec : Entity_Id := First (Parameter_Specifications (DTW_Spec));
1579 New_Formal : Entity_Id;
1581 begin
1582 -- Build parameter association for call to wrapped subprogram
1584 while Present (Formal) loop
1585 New_Formal := Defining_Identifier (New_F_Spec);
1587 -- If the controlling argument is inherited, add conversion to
1588 -- parent type for the call.
1590 if Is_Controlling_Formal (Formal) then
1591 Append_To (Actuals,
1592 Make_Type_Conversion (Loc,
1593 New_Occurrence_Of (Etype (Formal), Loc),
1594 New_Occurrence_Of (New_Formal, Loc)));
1595 else
1596 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1597 end if;
1599 Next_Formal (Formal);
1600 Next (New_F_Spec);
1601 end loop;
1603 if Ekind (Wrapped_Subp) = E_Procedure then
1604 Call :=
1605 Make_Procedure_Call_Statement (Loc,
1606 Name => New_Occurrence_Of (Wrapped_Subp, Loc),
1607 Parameter_Associations => Actuals);
1608 else
1609 Call :=
1610 Make_Simple_Return_Statement (Loc,
1611 Expression =>
1612 Make_Function_Call (Loc,
1613 Name => New_Occurrence_Of (Wrapped_Subp, Loc),
1614 Parameter_Associations => Actuals));
1615 end if;
1617 return
1618 Make_Subprogram_Body (Loc,
1619 Specification => Copy_Subprogram_Spec (DTW_Spec),
1620 Declarations => DTW_Decls,
1621 Handled_Statement_Sequence =>
1622 Make_Handled_Sequence_Of_Statements (Loc,
1623 Statements => New_List (Call),
1624 End_Label => Make_Identifier (Loc,
1625 Chars (Defining_Entity (DTW_Spec)))));
1626 end Build_DTW_Body;
1628 --------------------
1629 -- Build_DTW_Spec --
1630 --------------------
1632 function Build_DTW_Spec (Par_Prim : Entity_Id) return Node_Id is
1633 DTW_Id : Entity_Id;
1634 DTW_Spec : Node_Id;
1636 begin
1637 DTW_Spec := Build_Overriding_Spec (Par_Prim, R);
1638 DTW_Id := Defining_Entity (DTW_Spec);
1640 -- Clear the not-overriding indicator since the DTW wrapper overrides
1641 -- its wrapped subprogram; required because if present in the parent
1642 -- primitive, given that Build_Overriding_Spec inherits it, we report
1643 -- spurious errors.
1645 Set_Must_Not_Override (DTW_Spec, False);
1647 -- Add minimal decoration of fields
1649 Mutate_Ekind (DTW_Id, Ekind (Par_Prim));
1650 Set_Is_Dispatch_Table_Wrapper (DTW_Id);
1651 Set_Is_Wrapper (DTW_Id);
1653 -- The DTW wrapper is never a null procedure
1655 if Nkind (DTW_Spec) = N_Procedure_Specification then
1656 Set_Null_Present (DTW_Spec, False);
1657 end if;
1659 return DTW_Spec;
1660 end Build_DTW_Spec;
1662 ---------------------------------------
1663 -- Build_Inherited_Condition_Pragmas --
1664 ---------------------------------------
1666 procedure Build_Inherited_Condition_Pragmas
1667 (Subp : Entity_Id;
1668 LSP_Wrapper_Needed : out Boolean)
1670 Class_Pre : constant Node_Id :=
1671 Class_Preconditions (Ultimate_Alias (Subp));
1672 Class_Post : Node_Id := Class_Postconditions (Par_Prim);
1673 A_Post : Node_Id;
1674 New_Prag : Node_Id;
1676 begin
1677 LSP_Wrapper_Needed := False;
1679 if No (Class_Pre) and then No (Class_Post) then
1680 return;
1681 end if;
1683 -- For class-wide preconditions we just evaluate whether the wrapper
1684 -- is needed; there is no need to build the pragma since the check
1685 -- is performed on the caller side.
1687 if Present (Class_Pre)
1688 and then Needs_Wrapper (Class_Pre, Subp, Par_Prim)
1689 then
1690 LSP_Wrapper_Needed := True;
1691 end if;
1693 -- For class-wide postconditions we evaluate whether the wrapper is
1694 -- needed and we build the class-wide postcondition pragma to install
1695 -- it in the wrapper.
1697 if Present (Class_Post)
1698 and then Needs_Wrapper (Class_Post, Subp, Par_Prim)
1699 then
1700 LSP_Wrapper_Needed := True;
1702 -- Update the class-wide postcondition
1704 Class_Post := New_Copy_Tree (Class_Post);
1705 Build_Class_Wide_Expression
1706 (Pragma_Or_Expr => Class_Post,
1707 Subp => Subp,
1708 Par_Subp => Par_Prim,
1709 Adjust_Sloc => False);
1711 -- Install the updated class-wide postcondition in a copy of the
1712 -- pragma postcondition defined for the nearest ancestor.
1714 A_Post := Get_Class_Wide_Pragma (Par_Prim,
1715 Pragma_Postcondition);
1717 if No (A_Post) then
1718 declare
1719 Subps : constant Subprogram_List :=
1720 Inherited_Subprograms (Subp);
1721 begin
1722 for Index in Subps'Range loop
1723 A_Post := Get_Class_Wide_Pragma (Subps (Index),
1724 Pragma_Postcondition);
1725 exit when Present (A_Post);
1726 end loop;
1727 end;
1728 end if;
1730 -- A_Post can be null here if the postcondition was inlined in the
1731 -- called subprogram.
1733 if Present (A_Post) then
1734 New_Prag := New_Copy_Tree (A_Post);
1735 Rewrite
1736 (Expression (First (Pragma_Argument_Associations (New_Prag))),
1737 Class_Post);
1738 Append (New_Prag, Decls);
1739 end if;
1740 end if;
1741 end Build_Inherited_Condition_Pragmas;
1743 -------------------------------------------
1744 -- Check_Interface_Primitives_Strub_Mode --
1745 -------------------------------------------
1747 procedure Check_Interface_Primitives_Strub_Mode is
1748 Elmt : Elmt_Id;
1749 Iface_Elmt : Elmt_Id;
1750 Iface : Entity_Id;
1751 Iface_Prim : Entity_Id;
1752 Ifaces_List : Elist_Id;
1753 Op_Node : Elmt_Id;
1754 Prim : Entity_Id;
1755 Prim_Iface : Entity_Id;
1757 begin
1758 pragma Assert (Is_Interface (R));
1760 -- Collect interfaces extended by interface type R
1762 Collect_Interfaces (R, Ifaces_List);
1764 Op_Node := First_Elmt (Prim_Ops);
1765 while Present (Op_Node) loop
1766 Prim := Node (Op_Node);
1767 Prim_Iface := R;
1768 Par_Prim := Overridden_Operation (Prim);
1770 -- We only need to check entities defined in the sources
1772 -- Check that overrider and overridden primitives have the same
1773 -- strub mode.
1775 if Present (Par_Prim) then
1776 Check_Same_Strub_Mode (Prim, Par_Prim);
1778 -- No need to check internally added predefined primitives since
1779 -- they all have the same strub mode.
1781 elsif Is_Predefined_Dispatching_Operation (Prim)
1782 and then not Comes_From_Source (Prim)
1783 then
1784 null;
1786 -- Check strub mode of matching primitives of all the interface
1787 -- types, since several interface types may define primitives with
1788 -- the same profile that will be implemented by a single primitive
1789 -- of tagged types implementing R, and therefore must have the
1790 -- same strub mode.
1792 else
1793 -- If this interface primitive has been inherited this is an
1794 -- internal entity we rely on its renamed entity (which is the
1795 -- entity defined in the sources).
1797 if Present (Alias (Prim)) then
1798 Prim := Ultimate_Alias (Prim);
1799 Prim_Iface := Find_Dispatching_Type (Prim);
1800 end if;
1802 -- Search for primitives conformant with this one in the other
1803 -- interface types.
1805 Iface_Elmt := First_Elmt (Ifaces_List);
1806 while Present (Iface_Elmt) loop
1807 Iface := Node (Iface_Elmt);
1809 if Iface /= Prim_Iface then
1810 Elmt := First_Elmt (Primitive_Operations (Iface));
1811 while Present (Elmt) loop
1812 Iface_Prim := Node (Elmt);
1814 if Chars (Iface_Prim) = Chars (Prim)
1815 and then Comes_From_Source (Iface_Prim)
1816 and then Is_Interface_Conformant
1817 (Prim_Iface, Iface_Prim, Prim)
1818 then
1819 -- Check the strub mode passing the original
1820 -- primitive (instead of its alias); required
1821 -- to report the error at the right location.
1823 Check_Same_Strub_Mode (Node (Op_Node), Iface_Prim);
1824 end if;
1826 Next_Elmt (Elmt);
1827 end loop;
1828 end if;
1830 Next_Elmt (Iface_Elmt);
1831 end loop;
1832 end if;
1834 Next_Elmt (Op_Node);
1835 end loop;
1836 end Check_Interface_Primitives_Strub_Mode;
1838 -------------------
1839 -- Needs_Wrapper --
1840 -------------------
1842 function Needs_Wrapper
1843 (Class_Cond : Node_Id;
1844 Subp : Entity_Id;
1845 Par_Subp : Entity_Id) return Boolean
1847 Result : Boolean := False;
1849 function Check_Entity (N : Node_Id) return Traverse_Result;
1850 -- Check calls to overridden primitives
1852 --------------------
1853 -- Replace_Entity --
1854 --------------------
1856 function Check_Entity (N : Node_Id) return Traverse_Result is
1857 New_E : Entity_Id;
1859 begin
1860 if Nkind (N) = N_Identifier
1861 and then Present (Entity (N))
1862 and then
1863 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1864 and then
1865 (Nkind (Parent (N)) /= N_Attribute_Reference
1866 or else Attribute_Name (Parent (N)) /= Name_Class)
1867 then
1868 -- Determine whether entity has a renaming
1870 New_E := Get_Mapped_Entity (Entity (N));
1872 -- If the entity is an overridden primitive and we are not
1873 -- in GNATprove mode, we must build a wrapper for the current
1874 -- inherited operation. If the reference is the prefix of an
1875 -- attribute such as 'Result (or others ???) there is no need
1876 -- for a wrapper: the condition is just rewritten in terms of
1877 -- the inherited subprogram.
1879 if Present (New_E)
1880 and then Comes_From_Source (New_E)
1881 and then Is_Subprogram (New_E)
1882 and then Nkind (Parent (N)) /= N_Attribute_Reference
1883 and then not GNATprove_Mode
1884 then
1885 Result := True;
1886 return Abandon;
1887 end if;
1888 end if;
1890 return OK;
1891 end Check_Entity;
1893 procedure Check_Condition_Entities is
1894 new Traverse_Proc (Check_Entity);
1896 -- Start of processing for Needs_Wrapper
1898 begin
1899 Update_Primitives_Mapping (Par_Subp, Subp);
1901 Map_Formals (Par_Subp, Subp);
1902 Check_Condition_Entities (Class_Cond);
1904 return Result;
1905 end Needs_Wrapper;
1907 Wrappers_List : Elist_Id := No_Elist;
1908 -- List containing identifiers of built wrappers. Used to defer building
1909 -- and analyzing their class-wide precondition subprograms.
1911 Postcond_Candidates_List : Elist_Id := No_Elist;
1912 -- List containing inherited primitives of tagged type R that implement
1913 -- interface primitives that have postconditions.
1915 -- Start of processing for Check_Inherited_Conditions
1917 begin
1918 if Late_Overriding then
1919 Op_Node := First_Elmt (Prim_Ops);
1920 while Present (Op_Node) loop
1921 Prim := Node (Op_Node);
1923 -- Map the overridden primitive to the overriding one
1925 if Present (Overridden_Operation (Prim))
1926 and then Comes_From_Source (Prim)
1927 then
1928 Par_Prim := Overridden_Operation (Prim);
1929 Update_Primitives_Mapping (Par_Prim, Prim);
1931 -- Force discarding previous mappings of its formals
1933 Map_Formals (Par_Prim, Prim, Force_Update => True);
1934 end if;
1936 Next_Elmt (Op_Node);
1937 end loop;
1938 end if;
1940 -- For interface types we only need to check strub mode compatibility
1941 -- of their primitives (since they don't have wrappers).
1943 if Is_Interface (R) then
1944 Check_Interface_Primitives_Strub_Mode;
1945 return;
1946 end if;
1948 -- Perform validity checks on the inherited conditions of overriding
1949 -- operations, for conformance with LSP, and apply SPARK-specific
1950 -- restrictions on inherited conditions.
1952 Op_Node := First_Elmt (Prim_Ops);
1953 while Present (Op_Node) loop
1954 Prim := Node (Op_Node);
1955 Par_Prim := Overridden_Operation (Prim);
1957 if Present (Par_Prim)
1958 and then Comes_From_Source (Prim)
1959 then
1960 -- When the primitive is an LSP wrapper we climb to the parent
1961 -- primitive that has the inherited contract.
1963 if Is_Wrapper (Par_Prim)
1964 and then Present (LSP_Subprogram (Par_Prim))
1965 then
1966 Par_Prim := LSP_Subprogram (Par_Prim);
1967 end if;
1969 -- Check that overrider and overridden operations have
1970 -- the same strub mode.
1972 Check_Same_Strub_Mode (Prim, Par_Prim);
1974 -- Analyze the contract items of the overridden operation, before
1975 -- they are rewritten as pragmas.
1977 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1979 -- In GNATprove mode this is where we can collect the inherited
1980 -- conditions, because we do not create the Check pragmas that
1981 -- normally convey the modified class-wide conditions on
1982 -- overriding operations.
1984 if GNATprove_Mode then
1985 Collect_Inherited_Class_Wide_Conditions (Prim);
1986 end if;
1988 -- Check strub mode compatibility of primitives that implement
1989 -- interface primitives.
1991 elsif Present (Interface_Alias (Prim)) then
1992 Check_Same_Strub_Mode (Alias (Prim), Interface_Alias (Prim));
1993 end if;
1995 Next_Elmt (Op_Node);
1996 end loop;
1998 -- Collect inherited primitives that may need a wrapper to handle
1999 -- postconditions of interface primitives; done to improve the
2000 -- performance when checking if postcondition wrappers are needed.
2002 Op_Node := First_Elmt (Prim_Ops);
2003 while Present (Op_Node) loop
2004 Prim := Node (Op_Node);
2006 if Present (Interface_Alias (Prim))
2007 and then not Comes_From_Source (Alias (Prim))
2008 and then Present (Class_Postconditions (Interface_Alias (Prim)))
2009 then
2010 if No (Postcond_Candidates_List) then
2011 Postcond_Candidates_List := New_Elmt_List;
2012 end if;
2014 Append_Unique_Elmt (Alias (Prim), Postcond_Candidates_List);
2015 end if;
2017 Next_Elmt (Op_Node);
2018 end loop;
2020 -- Now examine the inherited operations to check whether they require
2021 -- a wrapper to handle inherited conditions that call other primitives,
2022 -- so that LSP can be verified/enforced.
2024 Op_Node := First_Elmt (Prim_Ops);
2026 while Present (Op_Node) loop
2027 Decls := Empty_List;
2028 Prim := Node (Op_Node);
2029 Wrapper_Needed := No_Wrapper;
2031 -- Skip internal entities built for mapping interface primitives
2033 if not Comes_From_Source (Prim)
2034 and then Present (Alias (Prim))
2035 and then No (Interface_Alias (Prim))
2036 then
2037 Par_Prim := Ultimate_Alias (Prim);
2039 -- When the primitive is an LSP wrapper we climb to the parent
2040 -- primitive that has the inherited contract.
2042 if Is_Wrapper (Par_Prim)
2043 and then Present (LSP_Subprogram (Par_Prim))
2044 then
2045 Par_Prim := LSP_Subprogram (Par_Prim);
2046 end if;
2048 -- Analyze the contract items of the parent operation, and
2049 -- determine whether this inherited primitive needs a LSP
2050 -- wrapper. This is determined when the condition is rewritten
2051 -- in sem_prag, using the mapping between overridden and
2052 -- overriding operations built in the loop above.
2054 declare
2055 LSP_Wrapper_Needed : Boolean;
2057 begin
2058 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
2059 Build_Inherited_Condition_Pragmas (Prim, LSP_Wrapper_Needed);
2061 if LSP_Wrapper_Needed then
2062 Wrapper_Needed := LSP_Wrapper;
2063 end if;
2064 end;
2066 -- If the LSP wrapper is not needed but the tagged type R
2067 -- implements additional interface types, and this inherited
2068 -- primitive covers an interface primitive of these additional
2069 -- interface types that has class-wide postconditions, then it
2070 -- requires a postconditions wrapper.
2072 if Wrapper_Needed = No_Wrapper
2073 and then Present (Interfaces (R))
2074 and then Present (Postcond_Candidates_List)
2075 and then Contains (Postcond_Candidates_List, Prim)
2076 then
2077 declare
2078 Elmt : Elmt_Id;
2079 Ent : Entity_Id;
2080 Iface : Entity_Id;
2081 Iface_Elmt : Elmt_Id;
2083 begin
2084 Elmt := First_Elmt (Prim_Ops);
2085 while Present (Elmt) loop
2086 Ent := Node (Elmt);
2088 -- Perform the search relying on the internal entities
2089 -- that link tagged type primitives with interface
2090 -- primitives.
2092 if Present (Interface_Alias (Ent))
2093 and then (Alias (Ent)) = Prim
2094 and then
2095 Present (Class_Postconditions (Interface_Alias (Ent)))
2096 then
2097 Iface := Find_Dispatching_Type (Interface_Alias (Ent));
2099 -- We only need to locate primitives of additional
2100 -- interfaces implemented by tagged type R (since
2101 -- inherited primitives of parent types that cover
2102 -- primitives of inherited interface types don't
2103 -- need a wrapper).
2105 Iface_Elmt := First_Elmt (Interfaces (R));
2106 while Present (Iface_Elmt) loop
2107 if Node (Iface_Elmt) = Iface then
2108 Wrapper_Needed := Postcond_Wrapper;
2109 exit;
2110 end if;
2112 Next_Elmt (Iface_Elmt);
2113 end loop;
2114 end if;
2116 Next_Elmt (Elmt);
2117 end loop;
2118 end;
2119 end if;
2120 end if;
2122 if Wrapper_Needed /= No_Wrapper
2123 and then not Is_Abstract_Subprogram (Par_Prim)
2124 and then Expander_Active
2125 then
2126 -- Build the dispatch-table wrapper (DTW). The support for
2127 -- AI12-0195 relies on two kind of wrappers: one for indirect
2128 -- calls (also used for AI12-0220), and one for putting in the
2129 -- dispatch table:
2131 -- 1) "indirect-call wrapper" (ICW) is needed anytime there are
2132 -- class-wide preconditions. Prim'Access will point directly
2133 -- at the ICW if any, or at the "pristine" body if Prim has
2134 -- no class-wide preconditions.
2136 -- 2) "dispatch-table wrapper" (DTW) is needed anytime the class
2137 -- wide preconditions *or* the class-wide postconditions are
2138 -- affected by overriding.
2140 -- The DTW holds a single statement that is a single call where
2141 -- the controlling actuals are conversions to the corresponding
2142 -- type in the parent primitive. If the primitive is a function
2143 -- the statement is a return statement with a call.
2145 declare
2146 Alias_Id : constant Entity_Id := Ultimate_Alias (Prim);
2147 Loc : constant Source_Ptr := Sloc (R);
2148 DTW_Body : Node_Id;
2149 DTW_Decl : Node_Id;
2150 DTW_Id : Entity_Id;
2151 DTW_Spec : Node_Id;
2153 Prim_Next_E : constant Entity_Id := Next_Entity (Prim);
2154 Prim_Prev_E : constant Entity_Id := Prev_Entity (Prim);
2156 begin
2157 DTW_Spec := Build_DTW_Spec (Par_Prim);
2158 DTW_Id := Defining_Entity (DTW_Spec);
2159 DTW_Decl := Make_Subprogram_Declaration (Loc,
2160 Specification => DTW_Spec);
2162 -- LSP wrappers reference the parent primitive that has the
2163 -- the class-wide pre/post condition that calls overridden
2164 -- primitives.
2166 if Wrapper_Needed = LSP_Wrapper then
2167 Set_LSP_Subprogram (DTW_Id, Par_Prim);
2168 end if;
2170 -- The spec of the wrapper has been built using the source
2171 -- location of its parent primitive; we must update it now
2172 -- (with the source location of the internal primitive built
2173 -- by Derive_Subprogram that will override this wrapper) to
2174 -- avoid inlining conflicts between internally built helpers
2175 -- for class-wide pre/postconditions of the parent and the
2176 -- helpers built for this wrapper.
2178 Set_Sloc (DTW_Id, Sloc (Prim));
2180 -- For inherited class-wide preconditions the DTW wrapper
2181 -- reuses the ICW of the parent (which checks the parent
2182 -- interpretation of the class-wide preconditions); the
2183 -- interpretation of the class-wide preconditions for the
2184 -- inherited subprogram is checked at the caller side.
2186 -- When the subprogram inherits class-wide postconditions
2187 -- the DTW also checks the interpretation of the class-wide
2188 -- postconditions for the inherited subprogram, and the body
2189 -- of the parent checks its interpretation of the parent for
2190 -- the class-wide postconditions.
2192 -- procedure Prim (F1 : T1; ...) is
2193 -- [ pragma Check (Postcondition, Expr); ]
2194 -- begin
2195 -- Par_Prim_ICW (Par_Type (F1), ...);
2196 -- end;
2198 if Present (Indirect_Call_Wrapper (Par_Prim)) then
2199 DTW_Body :=
2200 Build_DTW_Body (Loc,
2201 DTW_Spec => DTW_Spec,
2202 DTW_Decls => Decls,
2203 Par_Prim => Par_Prim,
2204 Wrapped_Subp => Indirect_Call_Wrapper (Par_Prim));
2206 -- For subprograms that only inherit class-wide postconditions
2207 -- the DTW wrapper calls the parent primitive (which on its
2208 -- body checks the interpretation of the class-wide post-
2209 -- conditions for the parent subprogram), and the DTW checks
2210 -- the interpretation of the class-wide postconditions for the
2211 -- inherited subprogram.
2213 -- procedure Prim (F1 : T1; ...) is
2214 -- pragma Check (Postcondition, Expr);
2215 -- begin
2216 -- Par_Prim (Par_Type (F1), ...);
2217 -- end;
2219 else
2220 DTW_Body :=
2221 Build_DTW_Body (Loc,
2222 DTW_Spec => DTW_Spec,
2223 DTW_Decls => Decls,
2224 Par_Prim => Par_Prim,
2225 Wrapped_Subp => Par_Prim);
2226 end if;
2228 -- Insert the declaration of the wrapper before the freezing
2229 -- node of the record type declaration to ensure that it will
2230 -- override the internal primitive built by Derive_Subprogram.
2232 if Late_Overriding then
2233 Ensure_Freeze_Node (R);
2234 Insert_Before_And_Analyze (Freeze_Node (R), DTW_Decl);
2235 else
2236 Append_Freeze_Action (R, DTW_Decl);
2237 Analyze (DTW_Decl);
2238 end if;
2240 -- The analyis of DTW_Decl has removed Prim from its scope
2241 -- chain and added DTW_Id at the end of the scope chain. Move
2242 -- DTW_Id to its correct place in the scope chain: the analysis
2243 -- of the wrapper declaration has just added DTW_Id at the end
2244 -- of the list of entities of its scope. However, given that
2245 -- this wrapper overrides Prim, we must move DTW_Id to the
2246 -- original place of Prim in its scope chain. This is required
2247 -- for wrappers of private type primitives to ensure their
2248 -- correct visibility since wrappers are built when the full
2249 -- tagged type declaration is frozen (in the private part of
2250 -- the package) but they may override primitives defined in the
2251 -- public part of the package.
2253 declare
2254 DTW_Prev_E : constant Entity_Id := Prev_Entity (DTW_Id);
2256 begin
2257 pragma Assert (Last_Entity (Current_Scope) = DTW_Id);
2258 pragma Assert
2259 (Ekind (Current_Scope) not in E_Package | E_Generic_Package
2260 or else No (First_Private_Entity (Current_Scope))
2261 or else First_Private_Entity (Current_Scope) /= DTW_Id);
2263 -- Remove DTW_Id from the end of the doubly-linked list of
2264 -- entities of this scope; no need to handle removing it
2265 -- from the beginning of the chain since such case can never
2266 -- occur for this entity.
2268 Set_Last_Entity (Current_Scope, DTW_Prev_E);
2269 Set_Next_Entity (DTW_Prev_E, Empty);
2271 -- Place DTW_Id back in the original place of its wrapped
2272 -- primitive in the list of entities of this scope.
2274 Link_Entities (Prim_Prev_E, DTW_Id);
2275 Link_Entities (DTW_Id, Prim_Next_E);
2276 end;
2278 -- Insert the body of the wrapper in the freeze actions of
2279 -- its record type declaration to ensure that it is placed
2280 -- in the scope of its declaration but not too early to cause
2281 -- premature freezing of other entities.
2283 Append_Freeze_Action (R, DTW_Body);
2284 Analyze (DTW_Body);
2286 -- Ensure correct decoration
2288 pragma Assert (Is_Dispatching_Operation (DTW_Id));
2289 pragma Assert (Present (Overridden_Operation (DTW_Id)));
2290 pragma Assert (Overridden_Operation (DTW_Id) = Alias_Id);
2292 -- Inherit dispatch table slot
2294 Set_DTC_Entity_Value (R, DTW_Id);
2295 Set_DT_Position (DTW_Id, DT_Position (Alias_Id));
2297 -- Register the wrapper in the dispatch table
2299 if Late_Overriding
2300 and then not Building_Static_DT (R)
2301 then
2302 Insert_List_After_And_Analyze (Freeze_Node (R),
2303 Register_Primitive (Loc, DTW_Id));
2304 end if;
2306 -- Defer building helpers and ICW for the DTW. Required to
2307 -- ensure uniqueness in their names because when building
2308 -- these wrappers for overlapped subprograms their homonym
2309 -- number is not definite until all these dispatch table
2310 -- wrappers of tagged type R have been analyzed.
2312 if Present (Indirect_Call_Wrapper (Par_Prim)) then
2313 Append_New_Elmt (DTW_Id, Wrappers_List);
2314 end if;
2315 end;
2316 end if;
2318 Next_Elmt (Op_Node);
2319 end loop;
2321 -- Build and analyze deferred class-wide precondition subprograms of
2322 -- built wrappers.
2324 if Present (Wrappers_List) then
2325 declare
2326 Body_N : Node_Id;
2327 CW_Subp : Entity_Id;
2328 Decl_N : Node_Id;
2329 DTW_Id : Entity_Id;
2330 Elmt : Elmt_Id;
2332 begin
2333 Elmt := First_Elmt (Wrappers_List);
2335 while Present (Elmt) loop
2336 DTW_Id := Node (Elmt);
2337 Next_Elmt (Elmt);
2339 Merge_Class_Conditions (DTW_Id);
2340 Make_Class_Precondition_Subps (DTW_Id, Late_Overriding);
2342 CW_Subp := Static_Call_Helper (DTW_Id);
2343 Decl_N := Unit_Declaration_Node (CW_Subp);
2344 Analyze (Decl_N);
2346 -- If the DTW was built for a late-overriding primitive
2347 -- its body must be analyzed now (since the tagged type
2348 -- is already frozen).
2350 if Late_Overriding then
2351 Body_N :=
2352 Unit_Declaration_Node (Corresponding_Body (Decl_N));
2353 Analyze (Body_N);
2354 end if;
2355 end loop;
2356 end;
2357 end if;
2358 end Check_Inherited_Conditions;
2360 ----------------------------
2361 -- Check_Strict_Alignment --
2362 ----------------------------
2364 procedure Check_Strict_Alignment (E : Entity_Id) is
2365 Comp : Entity_Id;
2367 begin
2368 -- Bit-packed array types do not require strict alignment, even if they
2369 -- are by-reference types, because they are accessed in a special way.
2371 if Is_By_Reference_Type (E) and then not Is_Bit_Packed_Array (E) then
2372 Set_Strict_Alignment (E);
2374 elsif Is_Array_Type (E) then
2375 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
2377 -- RM 13.2(7.1/4): Any component of a packed type that contains an
2378 -- aliased part shall be aligned according to the alignment of its
2379 -- subtype.
2381 -- Unfortunately this breaks Florist, which has had the bad habit
2382 -- of overaligning all the types it declares on 32-bit platforms,
2383 -- so make an exception if the component size is the storage unit.
2385 -- Other legacy codebases could also be affected because this was
2386 -- historically not enforced, so -gnatd_l can be used to disable it.
2388 if Has_Aliased_Components (E)
2389 and then not (Known_Component_Size (E)
2390 and then Component_Size (E) = System_Storage_Unit)
2391 and then not Debug_Flag_Underscore_L
2392 then
2393 Set_Strict_Alignment (E);
2394 end if;
2396 elsif Is_Record_Type (E) then
2397 Comp := First_Component (E);
2398 while Present (Comp) loop
2399 if not Is_Type (Comp)
2400 and then (Is_Aliased (Comp)
2401 or else Strict_Alignment (Etype (Comp)))
2402 then
2403 Set_Strict_Alignment (E);
2404 return;
2405 end if;
2407 Next_Component (Comp);
2408 end loop;
2409 end if;
2410 end Check_Strict_Alignment;
2412 -------------------------
2413 -- Check_Unsigned_Type --
2414 -------------------------
2416 procedure Check_Unsigned_Type (E : Entity_Id) is
2417 Ancestor : Entity_Id;
2418 Lo_Bound : Node_Id;
2419 Btyp : Entity_Id;
2421 begin
2422 if not Is_Discrete_Or_Fixed_Point_Type (E) then
2423 return;
2424 end if;
2426 -- Do not attempt to analyze case where range was in error
2428 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
2429 return;
2430 end if;
2432 -- The situation that is nontrivial is something like:
2434 -- subtype x1 is integer range -10 .. +10;
2435 -- subtype x2 is x1 range 0 .. V1;
2436 -- subtype x3 is x2 range V2 .. V3;
2437 -- subtype x4 is x3 range V4 .. V5;
2439 -- where Vn are variables. Here the base type is signed, but we still
2440 -- know that x4 is unsigned because of the lower bound of x2.
2442 -- The only way to deal with this is to look up the ancestor chain
2444 Ancestor := E;
2445 loop
2446 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
2447 return;
2448 end if;
2450 Lo_Bound := Type_Low_Bound (Ancestor);
2452 if Compile_Time_Known_Value (Lo_Bound) then
2453 if Expr_Rep_Value (Lo_Bound) >= 0 then
2454 Set_Is_Unsigned_Type (E, True);
2455 end if;
2457 return;
2459 else
2460 Ancestor := Ancestor_Subtype (Ancestor);
2462 -- If no ancestor had a static lower bound, go to base type
2464 if No (Ancestor) then
2466 -- Note: the reason we still check for a compile time known
2467 -- value for the base type is that at least in the case of
2468 -- generic formals, we can have bounds that fail this test,
2469 -- and there may be other cases in error situations.
2471 Btyp := Base_Type (E);
2473 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
2474 return;
2475 end if;
2477 Lo_Bound := Type_Low_Bound (Base_Type (E));
2479 if Compile_Time_Known_Value (Lo_Bound)
2480 and then Expr_Rep_Value (Lo_Bound) >= 0
2481 then
2482 Set_Is_Unsigned_Type (E, True);
2483 end if;
2485 return;
2486 end if;
2487 end if;
2488 end loop;
2489 end Check_Unsigned_Type;
2491 -----------------------------------------------
2492 -- Explode_Initialization_Compound_Statement --
2493 -----------------------------------------------
2495 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
2496 Init_Stmts : constant Node_Id := Initialization_Statements (E);
2498 begin
2499 if Present (Init_Stmts)
2500 and then Nkind (Init_Stmts) = N_Compound_Statement
2501 then
2502 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
2504 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
2505 -- just removing it, because Freeze_All may rely on this particular
2506 -- Node_Id still being present in the enclosing list to know where to
2507 -- stop freezing.
2509 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
2511 Set_Initialization_Statements (E, Empty);
2512 end if;
2513 end Explode_Initialization_Compound_Statement;
2515 ----------------
2516 -- Freeze_All --
2517 ----------------
2519 -- Note: the easy coding for this procedure would be to just build a
2520 -- single list of freeze nodes and then insert them and analyze them
2521 -- all at once. This won't work, because the analysis of earlier freeze
2522 -- nodes may recursively freeze types which would otherwise appear later
2523 -- on in the freeze list. So we must analyze and expand the freeze nodes
2524 -- as they are generated.
2526 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
2527 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
2528 -- This is the internal recursive routine that does freezing of entities
2529 -- (but NOT the analysis of default expressions, which should not be
2530 -- recursive, we don't want to analyze those till we are sure that ALL
2531 -- the types are frozen).
2533 --------------------
2534 -- Freeze_All_Ent --
2535 --------------------
2537 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
2538 E : Entity_Id;
2539 Flist : List_Id;
2541 procedure Process_Flist;
2542 -- If freeze nodes are present, insert and analyze, and reset cursor
2543 -- for next insertion.
2545 -------------------
2546 -- Process_Flist --
2547 -------------------
2549 procedure Process_Flist is
2550 Lastn : Node_Id;
2551 begin
2552 if Is_Non_Empty_List (Flist) then
2553 Lastn := Next (After);
2554 Insert_List_After_And_Analyze (After, Flist);
2556 if Present (Lastn) then
2557 After := Prev (Lastn);
2558 else
2559 After := Last (List_Containing (After));
2560 end if;
2561 end if;
2562 end Process_Flist;
2564 -- Start of processing for Freeze_All_Ent
2566 begin
2567 E := From;
2568 while Present (E) loop
2570 -- If the entity is an inner package which is not a package
2571 -- renaming, then its entities must be frozen at this point. Note
2572 -- that such entities do NOT get frozen at the end of the nested
2573 -- package itself (only library packages freeze).
2575 -- Same is true for task declarations, where anonymous records
2576 -- created for entry parameters must be frozen.
2578 if Ekind (E) = E_Package
2579 and then No (Renamed_Entity (E))
2580 and then not Is_Child_Unit (E)
2581 and then not Is_Frozen (E)
2582 then
2583 Push_Scope (E);
2585 Install_Visible_Declarations (E);
2586 Install_Private_Declarations (E);
2587 Freeze_All (First_Entity (E), After);
2589 End_Package_Scope (E);
2591 if Is_Generic_Instance (E)
2592 and then Has_Delayed_Freeze (E)
2593 then
2594 Set_Has_Delayed_Freeze (E, False);
2595 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
2596 end if;
2598 elsif Ekind (E) in Task_Kind
2599 and then Nkind (Parent (E)) in
2600 N_Single_Task_Declaration | N_Task_Type_Declaration
2601 then
2602 Push_Scope (E);
2603 Freeze_All (First_Entity (E), After);
2604 End_Scope;
2606 -- For a derived tagged type, we must ensure that all the
2607 -- primitive operations of the parent have been frozen, so that
2608 -- their addresses will be in the parent's dispatch table at the
2609 -- point it is inherited.
2611 elsif Ekind (E) = E_Record_Type
2612 and then Is_Tagged_Type (E)
2613 and then Is_Tagged_Type (Etype (E))
2614 and then Is_Derived_Type (E)
2615 then
2616 declare
2617 Prim_List : constant Elist_Id :=
2618 Primitive_Operations (Etype (E));
2620 Prim : Elmt_Id;
2621 Subp : Entity_Id;
2623 begin
2624 Prim := First_Elmt (Prim_List);
2625 while Present (Prim) loop
2626 Subp := Node (Prim);
2628 if Comes_From_Source (Subp)
2629 and then not Is_Frozen (Subp)
2630 then
2631 Flist := Freeze_Entity (Subp, After);
2632 Process_Flist;
2633 end if;
2635 Next_Elmt (Prim);
2636 end loop;
2637 end;
2638 end if;
2640 if not Is_Frozen (E) then
2641 Flist := Freeze_Entity (E, After);
2642 Process_Flist;
2644 -- If already frozen, and there are delayed aspects, this is where
2645 -- we do the visibility check for these aspects (see Sem_Ch13 spec
2646 -- for a description of how we handle aspect visibility).
2648 elsif Has_Delayed_Aspects (E) then
2649 Check_Aspects_At_End_Of_Declarations (E);
2650 end if;
2652 -- If an incomplete type is still not frozen, this may be a
2653 -- premature freezing because of a body declaration that follows.
2654 -- Indicate where the freezing took place. Freezing will happen
2655 -- if the body comes from source, but not if it is internally
2656 -- generated, for example as the body of a type invariant.
2658 -- If the freezing is caused by the end of the current declarative
2659 -- part, it is a Taft Amendment type, and there is no error.
2661 if not Is_Frozen (E)
2662 and then Ekind (E) = E_Incomplete_Type
2663 then
2664 declare
2665 Bod : constant Node_Id := Next (After);
2667 begin
2668 -- The presence of a body freezes all entities previously
2669 -- declared in the current list of declarations, but this
2670 -- does not apply if the body does not come from source.
2671 -- A type invariant is transformed into a subprogram body
2672 -- which is placed at the end of the private part of the
2673 -- current package, but this body does not freeze incomplete
2674 -- types that may be declared in this private part.
2676 if Comes_From_Source (Bod)
2677 and then Nkind (Bod) in N_Entry_Body
2678 | N_Package_Body
2679 | N_Protected_Body
2680 | N_Subprogram_Body
2681 | N_Task_Body
2682 | N_Body_Stub
2683 and then
2684 In_Same_List (After, Parent (E))
2685 then
2686 Error_Msg_Sloc := Sloc (Next (After));
2687 Error_Msg_NE
2688 ("type& is frozen# before its full declaration",
2689 Parent (E), E);
2690 end if;
2691 end;
2692 end if;
2694 Next_Entity (E);
2695 end loop;
2696 end Freeze_All_Ent;
2698 -- Local variables
2700 Decl : Node_Id;
2701 E : Entity_Id;
2702 Item : Entity_Id;
2704 -- Start of processing for Freeze_All
2706 begin
2707 Freeze_All_Ent (From, After);
2709 -- Now that all types are frozen, we can deal with default expressions
2710 -- that require us to build a default expression functions. This is the
2711 -- point at which such functions are constructed (after all types that
2712 -- might be used in such expressions have been frozen).
2714 -- For subprograms that are renaming_as_body, we create the wrapper
2715 -- bodies as needed.
2717 -- We also add finalization chains to access types whose designated
2718 -- types are controlled. This is normally done when freezing the type,
2719 -- but this misses recursive type definitions where the later members
2720 -- of the recursion introduce controlled components.
2722 -- Loop through entities
2724 E := From;
2725 while Present (E) loop
2726 if Is_Subprogram (E) then
2727 if not Default_Expressions_Processed (E) then
2728 Process_Default_Expressions (E, After);
2729 end if;
2731 -- Check subprogram renamings for the same strub-mode.
2732 -- Avoid rechecking dispatching operations, that's taken
2733 -- care of in Check_Inherited_Conditions, that covers
2734 -- inherited interface operations.
2736 Item := Alias (E);
2737 if Present (Item)
2738 and then not Is_Dispatching_Operation (E)
2739 then
2740 Check_Same_Strub_Mode (E, Item);
2741 end if;
2743 if not Has_Completion (E) then
2744 Decl := Unit_Declaration_Node (E);
2746 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
2747 if Error_Posted (Decl) then
2748 Set_Has_Completion (E);
2749 else
2750 Build_And_Analyze_Renamed_Body (Decl, E, After);
2751 end if;
2753 elsif Nkind (Decl) = N_Subprogram_Declaration
2754 and then Present (Corresponding_Body (Decl))
2755 and then
2756 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl))) =
2757 N_Subprogram_Renaming_Declaration
2758 then
2759 Build_And_Analyze_Renamed_Body
2760 (Decl, Corresponding_Body (Decl), After);
2761 end if;
2762 end if;
2764 -- Freeze the default expressions of entries, entry families, and
2765 -- protected subprograms.
2767 elsif Is_Concurrent_Type (E) then
2768 Item := First_Entity (E);
2769 while Present (Item) loop
2770 if Is_Subprogram_Or_Entry (Item)
2771 and then not Default_Expressions_Processed (Item)
2772 then
2773 Process_Default_Expressions (Item, After);
2774 end if;
2776 Next_Entity (Item);
2777 end loop;
2778 end if;
2780 -- Historical note: We used to create a finalization collection for
2781 -- access types whose designated type is not controlled, but contains
2782 -- private controlled compoments. This form of postprocessing is no
2783 -- longer needed because the finalization collection is now created
2784 -- when the access type is frozen (see Exp_Ch3.Freeze_Type).
2786 Next_Entity (E);
2787 end loop;
2788 end Freeze_All;
2790 -----------------------
2791 -- Freeze_And_Append --
2792 -----------------------
2794 procedure Freeze_And_Append
2795 (Ent : Entity_Id;
2796 N : Node_Id;
2797 Result : in out List_Id)
2799 -- Freezing an Expression_Function does not freeze its profile:
2800 -- the formals will have been frozen otherwise before the E_F
2801 -- can be called.
2803 L : constant List_Id :=
2804 Freeze_Entity
2805 (Ent, N, Do_Freeze_Profile => not Is_Expression_Function (Ent));
2806 begin
2807 if Is_Non_Empty_List (L) then
2808 if Result = No_List then
2809 Result := L;
2810 else
2811 Append_List (L, Result);
2812 end if;
2813 end if;
2814 end Freeze_And_Append;
2816 -------------------
2817 -- Freeze_Before --
2818 -------------------
2820 procedure Freeze_Before
2821 (N : Node_Id;
2822 T : Entity_Id;
2823 Do_Freeze_Profile : Boolean := True)
2825 -- Freeze T, then insert the generated Freeze nodes before the node N.
2826 -- Flag Freeze_Profile is used when T is an overloadable entity, and
2827 -- indicates whether its profile should be frozen at the same time.
2829 Freeze_Nodes : constant List_Id :=
2830 Freeze_Entity (T, N, Do_Freeze_Profile);
2831 Pack : constant Entity_Id := Scope (T);
2833 begin
2834 if Ekind (T) = E_Function then
2835 Check_Expression_Function (N, T);
2836 end if;
2838 if Is_Non_Empty_List (Freeze_Nodes) then
2840 -- If the entity is a type declared in an inner package, it may be
2841 -- frozen by an outer declaration before the package itself is
2842 -- frozen. Install the package scope to analyze the freeze nodes,
2843 -- which may include generated subprograms such as predicate
2844 -- functions, etc.
2846 if Is_Type (T) and then From_Nested_Package (T) then
2847 Push_Scope (Pack);
2848 Install_Visible_Declarations (Pack);
2849 Install_Private_Declarations (Pack);
2850 Insert_Actions (N, Freeze_Nodes);
2851 End_Package_Scope (Pack);
2853 else
2854 Insert_Actions (N, Freeze_Nodes);
2855 end if;
2856 end if;
2857 end Freeze_Before;
2859 -------------------
2860 -- Freeze_Entity --
2861 -------------------
2863 -- WARNING: This routine manages Ghost regions. Return statements must be
2864 -- replaced by gotos which jump to the end of the routine and restore the
2865 -- Ghost mode.
2867 function Freeze_Entity
2868 (E : Entity_Id;
2869 N : Node_Id;
2870 Do_Freeze_Profile : Boolean := True) return List_Id
2872 Loc : constant Source_Ptr := Sloc (N);
2874 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2875 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2876 -- Save the Ghost-related attributes to restore on exit
2878 Atype : Entity_Id;
2879 Comp : Entity_Id;
2880 F_Node : Node_Id;
2881 Formal : Entity_Id;
2882 Indx : Node_Id;
2884 Result : List_Id := No_List;
2885 -- List of freezing actions, left at No_List if none
2887 Test_E : Entity_Id := E;
2888 -- A local temporary used to test if freezing is necessary for E, since
2889 -- its value can be set to something other than E in certain cases. For
2890 -- example, E cannot be used directly in cases such as when it is an
2891 -- Itype defined within a record - since it is the location of record
2892 -- which matters.
2894 procedure Add_To_Result (Fnod : Node_Id);
2895 -- Add freeze action Fnod to list Result
2897 function After_Last_Declaration return Boolean;
2898 -- If Loc is a freeze_entity that appears after the last declaration
2899 -- in the scope, inhibit error messages on late completion.
2901 procedure Check_Current_Instance (Comp_Decl : Node_Id);
2902 -- Check that an Access or Unchecked_Access attribute with a prefix
2903 -- which is the current instance type can only be applied when the type
2904 -- is limited.
2906 procedure Check_No_Parts_Violations
2907 (Typ : Entity_Id; Aspect_No_Parts : Aspect_Id) with
2908 Pre => Aspect_No_Parts in
2909 Aspect_No_Controlled_Parts | Aspect_No_Task_Parts;
2910 -- Check that Typ does not violate the semantics of the specified
2911 -- Aspect_No_Parts (No_Controlled_Parts or No_Task_Parts) when it is
2912 -- specified on Typ or one of its ancestors.
2914 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id);
2915 -- Give a warning for pragma Convention with language C or C++ applied
2916 -- to a discriminated record type. This is suppressed for the unchecked
2917 -- union case, since the whole point in this case is interface C. We
2918 -- also do not generate this within instantiations, since we will have
2919 -- generated a message on the template.
2921 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
2922 -- Give warning for modulus of 8, 16, 32, 64 or 128 given as an explicit
2923 -- integer literal without an explicit corresponding size clause. The
2924 -- caller has checked that Utype is a modular integer type.
2926 procedure Freeze_Array_Type (Arr : Entity_Id);
2927 -- Freeze array type, including freezing index and component types
2929 procedure Freeze_Object_Declaration (E : Entity_Id);
2930 -- Perform checks and generate freeze node if needed for a constant or
2931 -- variable declared by an object declaration.
2933 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
2934 -- Create Freeze_Generic_Entity nodes for types declared in a generic
2935 -- package. Recurse on inner generic packages.
2937 function Freeze_Profile (E : Entity_Id) return Boolean;
2938 -- Freeze formals and return type of subprogram. If some type in the
2939 -- profile is incomplete and we are in an instance, freezing of the
2940 -- entity will take place elsewhere, and the function returns False.
2942 procedure Freeze_Record_Type (Rec : Entity_Id);
2943 -- Freeze record type, including freezing component types, and freezing
2944 -- primitive operations if this is a tagged type.
2946 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2947 -- Determine whether an arbitrary entity is subject to Boolean aspect
2948 -- Import and its value is specified as True.
2950 procedure Inherit_Freeze_Node
2951 (Fnod : Node_Id;
2952 Typ : Entity_Id);
2953 -- Set type Typ's freeze node to refer to Fnode. This routine ensures
2954 -- that any attributes attached to Typ's original node are preserved.
2956 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2957 -- If E is an entity for an imported subprogram with pre/post-conditions
2958 -- then this procedure will create a wrapper to ensure that proper run-
2959 -- time checking of the pre/postconditions. See body for details.
2961 -------------------
2962 -- Add_To_Result --
2963 -------------------
2965 procedure Add_To_Result (Fnod : Node_Id) is
2966 begin
2967 Append_New_To (Result, Fnod);
2968 end Add_To_Result;
2970 ----------------------------
2971 -- After_Last_Declaration --
2972 ----------------------------
2974 function After_Last_Declaration return Boolean is
2975 Spec : constant Node_Id := Parent (Current_Scope);
2977 begin
2978 if Nkind (Spec) = N_Package_Specification then
2979 if Present (Private_Declarations (Spec)) then
2980 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2981 elsif Present (Visible_Declarations (Spec)) then
2982 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2983 else
2984 return False;
2985 end if;
2987 else
2988 return False;
2989 end if;
2990 end After_Last_Declaration;
2992 ----------------------------
2993 -- Check_Current_Instance --
2994 ----------------------------
2996 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2998 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2999 -- Determine whether Typ is compatible with the rules for aliased
3000 -- views of types as defined in RM 3.10 in the various dialects.
3002 function Process (N : Node_Id) return Traverse_Result;
3003 -- Process routine to apply check to given node
3005 -----------------------------
3006 -- Is_Aliased_View_Of_Type --
3007 -----------------------------
3009 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
3010 Typ_Decl : constant Node_Id := Parent (Typ);
3012 begin
3013 -- Common case
3015 if Nkind (Typ_Decl) = N_Full_Type_Declaration
3016 and then Limited_Present (Type_Definition (Typ_Decl))
3017 then
3018 return True;
3020 -- The following paragraphs describe what a legal aliased view of
3021 -- a type is in the various dialects of Ada.
3023 -- Ada 95
3025 -- The current instance of a limited type, and a formal parameter
3026 -- or generic formal object of a tagged type.
3028 -- Ada 95 limited type
3029 -- * Type with reserved word "limited"
3030 -- * A protected or task type
3031 -- * A composite type with limited component
3033 elsif Ada_Version <= Ada_95 then
3034 return Is_Limited_Type (Typ);
3036 -- Ada 2005
3038 -- The current instance of a limited tagged type, a protected
3039 -- type, a task type, or a type that has the reserved word
3040 -- "limited" in its full definition ... a formal parameter or
3041 -- generic formal object of a tagged type.
3043 -- Ada 2005 limited type
3044 -- * Type with reserved word "limited", "synchronized", "task"
3045 -- or "protected"
3046 -- * A composite type with limited component
3047 -- * A derived type whose parent is a non-interface limited type
3049 elsif Ada_Version = Ada_2005 then
3050 return
3051 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
3052 or else
3053 (Is_Derived_Type (Typ)
3054 and then not Is_Interface (Etype (Typ))
3055 and then Is_Limited_Type (Etype (Typ)));
3057 -- Ada 2012 and beyond
3059 -- The current instance of an immutably limited type ... a formal
3060 -- parameter or generic formal object of a tagged type.
3062 -- Ada 2012 limited type
3063 -- * Type with reserved word "limited", "synchronized", "task"
3064 -- or "protected"
3065 -- * A composite type with limited component
3066 -- * A derived type whose parent is a non-interface limited type
3067 -- * An incomplete view
3069 -- Ada 2012 immutably limited type
3070 -- * Explicitly limited record type
3071 -- * Record extension with "limited" present
3072 -- * Non-formal limited private type that is either tagged
3073 -- or has at least one access discriminant with a default
3074 -- expression
3075 -- * Task type, protected type or synchronized interface
3076 -- * Type derived from immutably limited type
3078 else
3079 return
3080 Is_Immutably_Limited_Type (Typ)
3081 or else Is_Incomplete_Type (Typ);
3082 end if;
3083 end Is_Aliased_View_Of_Type;
3085 -------------
3086 -- Process --
3087 -------------
3089 function Process (N : Node_Id) return Traverse_Result is
3090 begin
3091 case Nkind (N) is
3092 when N_Attribute_Reference =>
3093 if Attribute_Name (N) in Name_Access | Name_Unchecked_Access
3094 and then Is_Entity_Name (Prefix (N))
3095 and then Entity (Prefix (N)) = E
3096 then
3097 if Ada_Version < Ada_2012 then
3098 Error_Msg_N
3099 ("current instance must be a limited type",
3100 Prefix (N));
3101 else
3102 Error_Msg_N
3103 ("current instance must be an immutably limited "
3104 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
3105 end if;
3107 return Abandon;
3109 else
3110 return OK;
3111 end if;
3113 when others =>
3114 return OK;
3115 end case;
3116 end Process;
3118 procedure Traverse is new Traverse_Proc (Process);
3120 -- Local variables
3122 Rec_Type : constant Entity_Id :=
3123 Scope (Defining_Identifier (Comp_Decl));
3125 -- Start of processing for Check_Current_Instance
3127 begin
3128 if not Is_Aliased_View_Of_Type (Rec_Type) then
3129 Traverse (Comp_Decl);
3130 end if;
3131 end Check_Current_Instance;
3133 -------------------------------
3134 -- Check_No_Parts_Violations --
3135 -------------------------------
3137 procedure Check_No_Parts_Violations
3138 (Typ : Entity_Id; Aspect_No_Parts : Aspect_Id)
3141 function Find_Aspect_No_Parts
3142 (Typ : Entity_Id) return Node_Id;
3143 -- Search for Aspect_No_Parts on a given type. When
3144 -- the aspect is not explicity specified Empty is returned.
3146 function Get_Aspect_No_Parts_Value
3147 (Typ : Entity_Id) return Entity_Id;
3148 -- Obtain the value for the Aspect_No_Parts on a given
3149 -- type. When the aspect is not explicitly specified Empty is
3150 -- returned.
3152 function Has_Aspect_No_Parts
3153 (Typ : Entity_Id) return Boolean;
3154 -- Predicate function which identifies whether No_Parts
3155 -- is explicitly specified on a given type.
3157 -------------------------------------
3158 -- Find_Aspect_No_Parts --
3159 -------------------------------------
3161 function Find_Aspect_No_Parts
3162 (Typ : Entity_Id) return Node_Id
3164 Partial_View : constant Entity_Id :=
3165 Incomplete_Or_Partial_View (Typ);
3167 Aspect_Spec : Entity_Id :=
3168 Find_Aspect (Typ, Aspect_No_Parts);
3169 Curr_Aspect_Spec : Entity_Id;
3170 begin
3172 -- Examine Typ's associated node, when present, since aspect
3173 -- specifications do not get transferred when nodes get rewritten.
3175 -- For example, this can happen in the expansion of array types
3177 if No (Aspect_Spec)
3178 and then Present (Associated_Node_For_Itype (Typ))
3179 and then Nkind (Associated_Node_For_Itype (Typ))
3180 = N_Full_Type_Declaration
3181 then
3182 Aspect_Spec :=
3183 Find_Aspect
3184 (Id => Defining_Identifier
3185 (Associated_Node_For_Itype (Typ)),
3186 A => Aspect_No_Parts);
3187 end if;
3189 -- Examine aspects specifications on private type declarations
3191 -- Should Find_Aspect be improved to handle this case ???
3193 if No (Aspect_Spec)
3194 and then Present (Partial_View)
3195 and then Present
3196 (Aspect_Specifications
3197 (Declaration_Node
3198 (Partial_View)))
3199 then
3200 Curr_Aspect_Spec :=
3201 First
3202 (Aspect_Specifications
3203 (Declaration_Node
3204 (Partial_View)));
3206 -- Search through aspects present on the private type
3208 while Present (Curr_Aspect_Spec) loop
3209 if Get_Aspect_Id (Curr_Aspect_Spec) = Aspect_No_Parts then
3210 Aspect_Spec := Curr_Aspect_Spec;
3211 exit;
3212 end if;
3214 Next (Curr_Aspect_Spec);
3215 end loop;
3217 end if;
3219 -- When errors are posted on the aspect return Empty
3221 if Error_Posted (Aspect_Spec) then
3222 return Empty;
3223 end if;
3225 return Aspect_Spec;
3226 end Find_Aspect_No_Parts;
3228 ------------------------------------------
3229 -- Get_Aspect_No_Parts_Value --
3230 ------------------------------------------
3232 function Get_Aspect_No_Parts_Value
3233 (Typ : Entity_Id) return Entity_Id
3235 Aspect_Spec : constant Entity_Id :=
3236 Find_Aspect_No_Parts (Typ);
3237 begin
3239 -- Return the value of the aspect when present
3241 if Present (Aspect_Spec) then
3243 -- No expression is the same as True
3245 if No (Expression (Aspect_Spec)) then
3246 return Standard_True;
3247 end if;
3249 -- Assume its expression has already been constant folded into
3250 -- a Boolean value and return its value.
3252 return Entity (Expression (Aspect_Spec));
3253 end if;
3255 -- Otherwise, the aspect is not specified - so return Empty
3257 return Empty;
3258 end Get_Aspect_No_Parts_Value;
3260 ------------------------------------
3261 -- Has_Aspect_No_Parts --
3262 ------------------------------------
3264 function Has_Aspect_No_Parts
3265 (Typ : Entity_Id) return Boolean
3266 is (Present (Find_Aspect_No_Parts (Typ)));
3268 -- Generic instances
3270 -------------------------------------------
3271 -- Get_Generic_Formal_Types_In_Hierarchy --
3272 -------------------------------------------
3274 function Get_Generic_Formal_Types_In_Hierarchy
3275 is new Collect_Types_In_Hierarchy (Predicate => Is_Generic_Formal);
3276 -- Return a list of all types within a given type's hierarchy which
3277 -- are generic formals.
3279 ----------------------------------------
3280 -- Get_Types_With_Aspect_In_Hierarchy --
3281 ----------------------------------------
3283 function Get_Types_With_Aspect_In_Hierarchy
3284 is new Collect_Types_In_Hierarchy
3285 (Predicate => Has_Aspect_No_Parts);
3286 -- Returns a list of all types within a given type's hierarchy which
3287 -- have the Aspect_No_Parts specified.
3289 -- Local declarations
3291 Aspect_Value : Entity_Id;
3292 Curr_Value : Entity_Id;
3293 Curr_Typ_Elmt : Elmt_Id;
3294 Curr_Body_Elmt : Elmt_Id;
3295 Curr_Formal_Elmt : Elmt_Id;
3296 Gen_Bodies : Elist_Id;
3297 Gen_Formals : Elist_Id;
3298 Scop : Entity_Id;
3299 Types_With_Aspect : Elist_Id;
3301 -- Start of processing for Check_No_Parts_Violations
3303 begin
3304 -- Nothing to check if the type is elementary or artificial
3306 if Is_Elementary_Type (Typ) or else not Comes_From_Source (Typ) then
3307 return;
3308 end if;
3310 Types_With_Aspect := Get_Types_With_Aspect_In_Hierarchy (Typ);
3312 -- Nothing to check if there are no types with No_Parts specified
3314 if Is_Empty_Elmt_List (Types_With_Aspect) then
3315 return;
3316 end if;
3318 -- Set name for all errors below
3320 Error_Msg_Name_1 := Aspect_Names (Aspect_No_Parts);
3322 -- Obtain the aspect value for No_Parts for comparison
3324 Aspect_Value :=
3325 Get_Aspect_No_Parts_Value
3326 (Node (First_Elmt (Types_With_Aspect)));
3328 -- When the value is True and there are controlled/task parts or the
3329 -- type itself is controlled/task, trigger the appropriate error.
3331 if Aspect_Value = Standard_True then
3332 if Aspect_No_Parts = Aspect_No_Controlled_Parts then
3333 if Is_Controlled (Typ) or else Has_Controlled_Component (Typ)
3334 then
3335 Error_Msg_N
3336 ("aspect % applied to controlled type &", Typ);
3337 end if;
3339 elsif Aspect_No_Parts = Aspect_No_Task_Parts then
3340 if Has_Task (Typ) then
3341 Error_Msg_N
3342 ("aspect % applied to task type &", Typ);
3343 Error_Msg_N
3344 ("\replace task components with access-to-task-type "
3345 & "components??", Typ);
3346 end if;
3348 else
3349 raise Program_Error;
3350 end if;
3351 end if;
3353 -- Move through Types_With_Aspect - checking that the value specified
3354 -- for their corresponding Aspect_No_Parts do not override each
3355 -- other.
3357 Curr_Typ_Elmt := First_Elmt (Types_With_Aspect);
3358 while Present (Curr_Typ_Elmt) loop
3359 Curr_Value :=
3360 Get_Aspect_No_Parts_Value (Node (Curr_Typ_Elmt));
3362 -- Compare the aspect value against the current type
3364 if Curr_Value /= Aspect_Value then
3365 Error_Msg_NE
3366 ("cannot override aspect % of "
3367 & "ancestor type &", Typ, Node (Curr_Typ_Elmt));
3368 return;
3369 end if;
3371 Next_Elmt (Curr_Typ_Elmt);
3372 end loop;
3374 -- Issue an error if the aspect applies to a type declared inside a
3375 -- generic body and if said type derives from or has a component
3376 -- of ageneric formal type - since those are considered to have
3377 -- controlled/task parts and have Aspect_No_Parts specified as
3378 -- False by default (RM H.4.1(4/5) is about the language-defined
3379 -- No_Controlled_Parts aspect, and we are using the same rules for
3380 -- No_Task_Parts).
3382 -- We do not check tagged types since deriving from a formal type
3383 -- within an enclosing generic unit is already illegal
3384 -- (RM 3.9.1 (4/2)).
3386 if Aspect_Value = Standard_True
3387 and then In_Generic_Body (Typ)
3388 and then not Is_Tagged_Type (Typ)
3389 then
3390 Gen_Bodies := New_Elmt_List;
3391 Gen_Formals :=
3392 Get_Generic_Formal_Types_In_Hierarchy
3393 (Typ => Typ,
3394 Examine_Components => True);
3396 -- Climb scopes collecting generic bodies
3398 Scop := Scope (Typ);
3399 while Present (Scop) and then Scop /= Standard_Standard loop
3401 -- Generic package body
3403 if Ekind (Scop) = E_Generic_Package
3404 and then In_Package_Body (Scop)
3405 then
3406 Append_Elmt (Scop, Gen_Bodies);
3408 -- Generic subprogram body
3410 elsif Is_Generic_Subprogram (Scop) then
3411 Append_Elmt (Scop, Gen_Bodies);
3412 end if;
3414 Scop := Scope (Scop);
3415 end loop;
3417 -- Warn about the improper use of Aspect_No_Parts on a type
3418 -- declaration deriving from or that has a component of a generic
3419 -- formal type within the formal type's corresponding generic
3420 -- body by moving through all formal types in Typ's hierarchy and
3421 -- checking if they are formals in any of the enclosing generic
3422 -- bodies.
3424 -- However, a special exception gets made for formal types which
3425 -- derive from a type which has Aspect_No_Parts True.
3427 -- For example:
3429 -- generic
3430 -- type Form is private;
3431 -- package G is
3432 -- type Type_A is new Form with No_Controlled_Parts; -- OK
3433 -- end;
3435 -- package body G is
3436 -- type Type_B is new Form with No_Controlled_Parts; -- ERROR
3437 -- end;
3439 -- generic
3440 -- type Form is private;
3441 -- package G is
3442 -- type Type_A is record C : Form; end record
3443 -- with No_Controlled_Parts; -- OK
3444 -- end;
3446 -- package body G is
3447 -- type Type_B is record C : Form; end record
3448 -- with No_Controlled_Parts; -- ERROR
3449 -- end;
3451 -- type Root is tagged null record with No_Controlled_Parts;
3453 -- generic
3454 -- type Form is new Root with private;
3455 -- package G is
3456 -- type Type_A is record C : Form; end record
3457 -- with No_Controlled_Parts; -- OK
3458 -- end;
3460 -- package body G is
3461 -- type Type_B is record C : Form; end record
3462 -- with No_Controlled_Parts; -- OK
3463 -- end;
3465 Curr_Formal_Elmt := First_Elmt (Gen_Formals);
3466 while Present (Curr_Formal_Elmt) loop
3468 Curr_Body_Elmt := First_Elmt (Gen_Bodies);
3469 while Present (Curr_Body_Elmt) loop
3471 -- Obtain types in the formal type's hierarchy which have
3472 -- the aspect specified.
3474 Types_With_Aspect :=
3475 Get_Types_With_Aspect_In_Hierarchy
3476 (Node (Curr_Formal_Elmt));
3478 -- We found a type declaration in a generic body where both
3479 -- Aspect_No_Parts is true and one of its ancestors is a
3480 -- generic formal type.
3482 if Scope (Node (Curr_Formal_Elmt)) =
3483 Node (Curr_Body_Elmt)
3485 -- Check that no ancestors of the formal type have
3486 -- Aspect_No_Parts True before issuing the error.
3488 and then (Is_Empty_Elmt_List (Types_With_Aspect)
3489 or else
3490 Get_Aspect_No_Parts_Value
3491 (Node (First_Elmt (Types_With_Aspect)))
3492 = Standard_False)
3493 then
3494 Error_Msg_Node_1 := Typ;
3495 Error_Msg_Node_2 := Node (Curr_Formal_Elmt);
3496 Error_Msg
3497 ("aspect % cannot be applied to "
3498 & "type & which has an ancestor or component of "
3499 & "formal type & within the formal type's "
3500 & "corresponding generic body", Sloc (Typ));
3501 end if;
3503 Next_Elmt (Curr_Body_Elmt);
3504 end loop;
3506 Next_Elmt (Curr_Formal_Elmt);
3507 end loop;
3508 end if;
3509 end Check_No_Parts_Violations;
3511 ---------------------------------
3512 -- Check_Suspicious_Convention --
3513 ---------------------------------
3515 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id) is
3516 begin
3517 if Has_Discriminants (Rec_Type)
3518 and then Is_Base_Type (Rec_Type)
3519 and then not Is_Unchecked_Union (Rec_Type)
3520 and then (Convention (Rec_Type) = Convention_C
3521 or else
3522 Convention (Rec_Type) = Convention_CPP)
3523 and then Comes_From_Source (Rec_Type)
3524 and then not In_Instance
3525 and then not Has_Warnings_Off (Rec_Type)
3526 then
3527 declare
3528 Cprag : constant Node_Id :=
3529 Get_Rep_Pragma (Rec_Type, Name_Convention);
3530 A2 : Node_Id;
3532 begin
3533 if Present (Cprag) then
3534 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
3536 if Convention (Rec_Type) = Convention_C then
3537 Error_Msg_N
3538 ("?x?discriminated record has no direct equivalent in "
3539 & "C", A2);
3540 else
3541 Error_Msg_N
3542 ("?x?discriminated record has no direct equivalent in "
3543 & "C++", A2);
3544 end if;
3546 Error_Msg_NE
3547 ("\?x?use of convention for type& is dubious",
3548 A2, Rec_Type);
3549 end if;
3550 end;
3551 end if;
3552 end Check_Suspicious_Convention;
3554 ------------------------------
3555 -- Check_Suspicious_Modulus --
3556 ------------------------------
3558 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
3559 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
3561 begin
3562 if not Warn_On_Suspicious_Modulus_Value then
3563 return;
3564 end if;
3566 if Nkind (Decl) = N_Full_Type_Declaration then
3567 declare
3568 Tdef : constant Node_Id := Type_Definition (Decl);
3570 begin
3571 if Nkind (Tdef) = N_Modular_Type_Definition then
3572 declare
3573 Modulus : constant Node_Id :=
3574 Original_Node (Expression (Tdef));
3576 begin
3577 if Nkind (Modulus) = N_Integer_Literal then
3578 declare
3579 Modv : constant Uint := Intval (Modulus);
3580 Sizv : constant Uint := RM_Size (Utype);
3582 begin
3583 -- First case, modulus and size are the same. This
3584 -- happens if you have something like mod 32, with
3585 -- an explicit size of 32, this is for sure a case
3586 -- where the warning is given, since it is seems
3587 -- very unlikely that someone would want e.g. a
3588 -- five bit type stored in 32 bits. It is much
3589 -- more likely they wanted a 32-bit type.
3591 if Modv = Sizv then
3592 null;
3594 -- Second case, the modulus is 32 or 64 and no
3595 -- size clause is present. This is a less clear
3596 -- case for giving the warning, but in the case
3597 -- of 32/64 (5-bit or 6-bit types) these seem rare
3598 -- enough that it is a likely error (and in any
3599 -- case using 2**5 or 2**6 in these cases seems
3600 -- clearer. We don't include 8 or 16 here, simply
3601 -- because in practice 3-bit and 4-bit types are
3602 -- more common and too many false positives if
3603 -- we warn in these cases.
3605 elsif not Has_Size_Clause (Utype)
3606 and then (Modv = Uint_32 or else Modv = Uint_64)
3607 then
3608 null;
3610 -- No warning needed
3612 else
3613 return;
3614 end if;
3616 -- If we fall through, give warning
3618 Error_Msg_Uint_1 := Modv;
3619 Error_Msg_N
3620 ("?.m?2 '*'*^' may have been intended here",
3621 Modulus);
3622 end;
3623 end if;
3624 end;
3625 end if;
3626 end;
3627 end if;
3628 end Check_Suspicious_Modulus;
3630 -----------------------
3631 -- Freeze_Array_Type --
3632 -----------------------
3634 procedure Freeze_Array_Type (Arr : Entity_Id) is
3635 FS : constant Entity_Id := First_Subtype (Arr);
3636 Ctyp : constant Entity_Id := Component_Type (Arr);
3638 Clause : Node_Id;
3639 -- Set to Component_Size clause or Atomic pragma, if any
3641 Non_Standard_Enum : Boolean := False;
3642 -- Set true if any of the index types is an enumeration type with a
3643 -- non-standard representation.
3645 begin
3646 Freeze_And_Append (Ctyp, N, Result);
3648 Indx := First_Index (Arr);
3649 while Present (Indx) loop
3650 Freeze_And_Append (Etype (Indx), N, Result);
3652 if Is_Enumeration_Type (Etype (Indx))
3653 and then Has_Non_Standard_Rep (Etype (Indx))
3654 then
3655 Non_Standard_Enum := True;
3656 end if;
3658 Next_Index (Indx);
3659 end loop;
3661 -- Processing that is done only for base types
3663 if Ekind (Arr) = E_Array_Type then
3665 -- Deal with default setting of reverse storage order
3667 Set_SSO_From_Default (Arr);
3669 -- Propagate flags from component type
3671 Propagate_Concurrent_Flags (Arr, Ctyp);
3672 Propagate_Controlled_Flags (Arr, Ctyp, Comp => True);
3674 if Has_Unchecked_Union (Ctyp) then
3675 Set_Has_Unchecked_Union (Arr);
3676 end if;
3678 -- The array type requires its own invariant procedure in order to
3679 -- verify the component invariant over all elements. In GNATprove
3680 -- mode, the component invariants are checked by other means. They
3681 -- should not be added to the array type invariant procedure, so
3682 -- that the procedure can be used to check the array type
3683 -- invariants if any.
3685 if Has_Invariants (Ctyp)
3686 and then not GNATprove_Mode
3687 then
3688 Set_Has_Own_Invariants (Arr);
3689 end if;
3691 -- Warn for pragma Pack overriding foreign convention
3693 if Has_Foreign_Convention (Ctyp)
3694 and then Has_Pragma_Pack (Arr)
3695 then
3696 declare
3697 CN : constant Name_Id :=
3698 Get_Convention_Name (Convention (Ctyp));
3699 PP : constant Node_Id :=
3700 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
3701 begin
3702 if Present (PP) then
3703 Error_Msg_Name_1 := CN;
3704 Error_Msg_Sloc := Sloc (Arr);
3705 Error_Msg_N
3706 ("pragma Pack affects convention % components #??", PP);
3707 Error_Msg_Name_1 := CN;
3708 Error_Msg_N
3709 ("\array components may not have % compatible "
3710 & "representation??", PP);
3711 end if;
3712 end;
3713 end if;
3715 -- Check for Aliased or Atomic or Full Access or Independent
3716 -- components with an unsuitable component size clause given.
3717 -- The main purpose is to give an error when bit packing would
3718 -- be required to honor the component size, because bit packing
3719 -- is incompatible with these aspects; when bit packing is not
3720 -- required, the final validation of the component size may be
3721 -- left to the back end.
3723 if Has_Component_Size_Clause (Arr) then
3724 CS_Check : declare
3725 procedure Complain_CS (T : String; Min : Boolean := False);
3726 -- Output an error message for an unsuitable component size
3727 -- clause for independent components (T is either "aliased"
3728 -- or "atomic" or "volatile full access" or "independent").
3730 -----------------
3731 -- Complain_CS --
3732 -----------------
3734 procedure Complain_CS (T : String; Min : Boolean := False) is
3735 begin
3736 Clause :=
3737 Get_Attribute_Definition_Clause
3738 (FS, Attribute_Component_Size);
3740 Error_Msg_N
3741 ("incorrect component size for " & T & " components",
3742 Clause);
3744 if Known_Static_Esize (Ctyp) then
3745 Error_Msg_Uint_1 := Esize (Ctyp);
3746 if Min then
3747 Error_Msg_N ("\minimum allowed value is^", Clause);
3748 else
3749 Error_Msg_N ("\only allowed value is^", Clause);
3750 end if;
3751 else
3752 Error_Msg_N
3753 ("\must be multiple of storage unit", Clause);
3754 end if;
3755 end Complain_CS;
3757 -- Start of processing for CS_Check
3759 begin
3760 -- OK if the component size and object size are equal, or
3761 -- if the component size is a multiple of the storage unit.
3763 if (if Known_Static_Esize (Ctyp)
3764 then Component_Size (Arr) = Esize (Ctyp)
3765 else Component_Size (Arr) mod System_Storage_Unit = 0)
3766 then
3767 null;
3769 elsif Has_Aliased_Components (Arr) then
3770 Complain_CS ("aliased");
3772 elsif Has_Atomic_Components (Arr)
3773 or else Is_Atomic (Ctyp)
3774 then
3775 Complain_CS ("atomic");
3777 elsif Is_Volatile_Full_Access (Ctyp) then
3778 Complain_CS ("volatile full access");
3780 -- For Independent a larger size is permitted
3782 elsif (Has_Independent_Components (Arr)
3783 or else Is_Independent (Ctyp))
3784 and then (not Known_Static_Esize (Ctyp)
3785 or else Component_Size (Arr) < Esize (Ctyp))
3786 then
3787 Complain_CS ("independent", Min => True);
3788 end if;
3789 end CS_Check;
3791 -- Check for Aliased or Atomic or Full Access or Independent
3792 -- components with an unsuitable aspect/pragma Pack given.
3793 -- The main purpose is to prevent bit packing from occurring,
3794 -- because bit packing is incompatible with these aspects; when
3795 -- bit packing cannot occur, the final handling of the packing
3796 -- may be left to the back end.
3798 elsif Is_Packed (Arr) and then Known_Static_RM_Size (Ctyp) then
3799 Pack_Check : declare
3801 procedure Complain_Pack (T : String);
3802 -- Output a warning message for an unsuitable aspect/pragma
3803 -- Pack for independent components (T is either "aliased" or
3804 -- "atomic" or "volatile full access" or "independent") and
3805 -- reset the Is_Packed flag on the array type.
3807 -------------------
3808 -- Complain_Pack --
3809 -------------------
3811 procedure Complain_Pack (T : String) is
3812 begin
3813 Error_Msg_N
3814 ("?cannot pack " & T & " components (RM 13.2(7))",
3815 Get_Rep_Pragma (FS, Name_Pack));
3817 Set_Is_Packed (Arr, False);
3818 end Complain_Pack;
3820 -- Start of processing for Pack_Check
3822 begin
3823 -- OK if the component size and object size are equal, or
3824 -- if the component size is a multiple of the storage unit.
3826 if (if Known_Static_Esize (Ctyp)
3827 then RM_Size (Ctyp) = Esize (Ctyp)
3828 else RM_Size (Ctyp) mod System_Storage_Unit = 0)
3829 then
3830 null;
3832 elsif Has_Aliased_Components (Arr) then
3833 Complain_Pack ("aliased");
3835 elsif Has_Atomic_Components (Arr)
3836 or else Is_Atomic (Ctyp)
3837 then
3838 Complain_Pack ("atomic");
3840 elsif Is_Volatile_Full_Access (Ctyp) then
3841 Complain_Pack ("volatile full access");
3843 elsif Has_Independent_Components (Arr)
3844 or else Is_Independent (Ctyp)
3845 then
3846 Complain_Pack ("independent");
3847 end if;
3848 end Pack_Check;
3849 end if;
3851 -- If packing was requested or if the component size was
3852 -- set explicitly, then see if bit packing is required. This
3853 -- processing is only done for base types, since all of the
3854 -- representation aspects involved are type-related.
3856 -- This is not just an optimization, if we start processing the
3857 -- subtypes, they interfere with the settings on the base type
3858 -- (this is because Is_Packed has a slightly different meaning
3859 -- before and after freezing).
3861 declare
3862 Csiz : Uint;
3863 Esiz : Uint;
3865 begin
3866 if Is_Packed (Arr)
3867 and then Known_Static_RM_Size (Ctyp)
3868 and then not Has_Component_Size_Clause (Arr)
3869 then
3870 Csiz := UI_Max (RM_Size (Ctyp), 1);
3872 elsif Known_Component_Size (Arr) then
3873 Csiz := Component_Size (Arr);
3875 elsif not Known_Static_Esize (Ctyp) then
3876 Csiz := Uint_0;
3878 else
3879 Esiz := Esize (Ctyp);
3881 -- We can set the component size if it is less than 16,
3882 -- rounding it up to the next storage unit size.
3884 if Esiz <= 8 then
3885 Csiz := Uint_8;
3886 elsif Esiz <= 16 then
3887 Csiz := Uint_16;
3888 else
3889 Csiz := Uint_0;
3890 end if;
3892 -- Set component size up to match alignment if it would
3893 -- otherwise be less than the alignment. This deals with
3894 -- cases of types whose alignment exceeds their size (the
3895 -- padded type cases).
3897 if Csiz /= 0 and then Known_Alignment (Ctyp) then
3898 declare
3899 A : constant Uint := Alignment_In_Bits (Ctyp);
3900 begin
3901 if Csiz < A then
3902 Csiz := A;
3903 end if;
3904 end;
3905 end if;
3906 end if;
3908 -- Case of component size that may result in bit packing
3910 if 1 <= Csiz and then Csiz <= System_Max_Integer_Size then
3911 declare
3912 Ent : constant Entity_Id :=
3913 First_Subtype (Arr);
3914 Pack_Pragma : constant Node_Id :=
3915 Get_Rep_Pragma (Ent, Name_Pack);
3916 Comp_Size_C : constant Node_Id :=
3917 Get_Attribute_Definition_Clause
3918 (Ent, Attribute_Component_Size);
3920 begin
3921 -- Warn if we have pack and component size so that the
3922 -- pack is ignored.
3924 -- Note: here we must check for the presence of a
3925 -- component size before checking for a Pack pragma to
3926 -- deal with the case where the array type is a derived
3927 -- type whose parent is currently private.
3929 if Present (Comp_Size_C)
3930 and then Has_Pragma_Pack (Ent)
3931 and then Warn_On_Redundant_Constructs
3932 then
3933 Error_Msg_Sloc := Sloc (Comp_Size_C);
3934 Error_Msg_NE
3935 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
3936 Error_Msg_N
3937 ("\?r?explicit component size given#!", Pack_Pragma);
3938 Set_Is_Packed (Base_Type (Ent), False);
3939 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
3940 end if;
3942 -- Set component size if not already set by a component
3943 -- size clause.
3945 if No (Comp_Size_C) then
3946 Set_Component_Size (Arr, Csiz);
3947 end if;
3949 -- Check for base type of 8, 16, 32 bits, where an
3950 -- unsigned subtype has a length one less than the
3951 -- base type (e.g. Natural subtype of Integer).
3953 -- In such cases, if a component size was not set
3954 -- explicitly, then generate a warning.
3956 if Has_Pragma_Pack (Arr)
3957 and then No (Comp_Size_C)
3958 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
3959 and then Known_Esize (Base_Type (Ctyp))
3960 and then Esize (Base_Type (Ctyp)) = Csiz + 1
3961 then
3962 Error_Msg_Uint_1 := Csiz;
3964 if Present (Pack_Pragma) then
3965 Error_Msg_N
3966 ("??pragma Pack causes component size to be ^!",
3967 Pack_Pragma);
3968 Error_Msg_N
3969 ("\??use Component_Size to set desired value!",
3970 Pack_Pragma);
3971 end if;
3972 end if;
3974 -- Bit packing is never needed for 8, 16, 32, 64 or 128
3976 if Addressable (Csiz) then
3978 -- If the Esize of the component is known and equal to
3979 -- the component size then even packing is not needed.
3981 if Known_Static_Esize (Ctyp)
3982 and then Esize (Ctyp) = Csiz
3983 then
3984 -- Here the array was requested to be packed, but
3985 -- the packing request had no effect whatsoever,
3986 -- so flag Is_Packed is reset.
3988 -- Note: semantically this means that we lose track
3989 -- of the fact that a derived type inherited pragma
3990 -- Pack that was non-effective, but that is fine.
3992 -- We regard a Pack pragma as a request to set a
3993 -- representation characteristic, and this request
3994 -- may be ignored.
3996 Set_Is_Packed (Base_Type (Arr), False);
3997 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
3998 else
3999 Set_Is_Packed (Base_Type (Arr), True);
4000 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
4001 end if;
4003 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
4005 -- Bit packing is not needed for multiples of the storage
4006 -- unit if the type is composite because the back end can
4007 -- byte pack composite types efficiently. That's not true
4008 -- for discrete types because every read would generate a
4009 -- lot of instructions, so we keep using the manipulation
4010 -- routines of the runtime for them.
4012 elsif Csiz mod System_Storage_Unit = 0
4013 and then Is_Composite_Type (Ctyp)
4014 then
4015 Set_Is_Packed (Base_Type (Arr), True);
4016 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
4017 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
4019 -- In all other cases, bit packing is needed
4021 else
4022 Set_Is_Packed (Base_Type (Arr), True);
4023 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
4024 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
4025 end if;
4026 end;
4027 end if;
4028 end;
4030 -- Warn for case of atomic type
4032 Clause := Get_Rep_Pragma (FS, Name_Atomic);
4034 if Present (Clause)
4035 and then not Addressable (Component_Size (FS))
4036 then
4037 Error_Msg_NE
4038 ("non-atomic components of type& may not be "
4039 & "accessible by separate tasks??", Clause, Arr);
4041 if Has_Component_Size_Clause (Arr) then
4042 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
4043 (FS, Attribute_Component_Size));
4044 Error_Msg_N ("\because of component size clause#??", Clause);
4046 elsif Has_Pragma_Pack (Arr) then
4047 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
4048 Error_Msg_N ("\because of pragma Pack#??", Clause);
4049 end if;
4050 end if;
4052 -- Check for scalar storage order
4054 declare
4055 Dummy : Boolean;
4056 begin
4057 Check_Component_Storage_Order
4058 (Encl_Type => Arr,
4059 Comp => Empty,
4060 ADC => Get_Attribute_Definition_Clause
4061 (First_Subtype (Arr),
4062 Attribute_Scalar_Storage_Order),
4063 Comp_ADC_Present => Dummy);
4064 end;
4066 -- Processing that is done only for subtypes
4068 else
4069 -- Acquire alignment from base type. Known_Alignment of the base
4070 -- type is False for Wide_String, for example.
4072 if not Known_Alignment (Arr)
4073 and then Known_Alignment (Base_Type (Arr))
4074 then
4075 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
4076 Adjust_Esize_Alignment (Arr);
4077 end if;
4078 end if;
4080 -- Specific checks for bit-packed arrays
4082 if Is_Bit_Packed_Array (Arr) then
4084 -- Check number of elements for bit-packed arrays that come from
4085 -- source and have compile time known ranges. The bit-packed
4086 -- arrays circuitry does not support arrays with more than
4087 -- Integer'Last + 1 elements, and when this restriction is
4088 -- violated, causes incorrect data access.
4090 -- For the case where this is not compile time known, a run-time
4091 -- check should be generated???
4093 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
4094 declare
4095 Elmts : Uint;
4096 Index : Node_Id;
4097 Ilen : Node_Id;
4098 Ityp : Entity_Id;
4100 begin
4101 Elmts := Uint_1;
4102 Index := First_Index (Arr);
4103 while Present (Index) loop
4104 Ityp := Etype (Index);
4106 -- Never generate an error if any index is of a generic
4107 -- type. We will check this in instances.
4109 if Is_Generic_Type (Ityp) then
4110 Elmts := Uint_0;
4111 exit;
4112 end if;
4114 Ilen :=
4115 Make_Attribute_Reference (Loc,
4116 Prefix => New_Occurrence_Of (Ityp, Loc),
4117 Attribute_Name => Name_Range_Length);
4118 Analyze_And_Resolve (Ilen);
4120 -- No attempt is made to check number of elements if not
4121 -- compile time known.
4123 if Nkind (Ilen) /= N_Integer_Literal then
4124 Elmts := Uint_0;
4125 exit;
4126 end if;
4128 Elmts := Elmts * Intval (Ilen);
4129 Next_Index (Index);
4130 end loop;
4132 if Elmts > Intval (High_Bound
4133 (Scalar_Range (Standard_Integer))) + 1
4134 then
4135 Error_Msg_N
4136 ("bit packed array type may not have "
4137 & "more than Integer''Last+1 elements", Arr);
4138 end if;
4139 end;
4140 end if;
4142 -- Check size
4144 if Known_RM_Size (Arr) then
4145 declare
4146 SizC : constant Node_Id := Size_Clause (Arr);
4147 Discard : Boolean;
4149 begin
4150 -- It is not clear if it is possible to have no size clause
4151 -- at this stage, but it is not worth worrying about. Post
4152 -- error on the entity name in the size clause if present,
4153 -- else on the type entity itself.
4155 if Present (SizC) then
4156 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
4157 else
4158 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
4159 end if;
4160 end;
4161 end if;
4162 end if;
4164 -- If any of the index types was an enumeration type with a non-
4165 -- standard rep clause, then we indicate that the array type is
4166 -- always packed (even if it is not bit-packed).
4168 if Non_Standard_Enum then
4169 Set_Has_Non_Standard_Rep (Base_Type (Arr));
4170 Set_Is_Packed (Base_Type (Arr));
4171 end if;
4173 Set_Component_Alignment_If_Not_Set (Arr);
4175 -- If the array is packed and bit-packed or packed to eliminate holes
4176 -- in the non-contiguous enumeration index types, we must create the
4177 -- packed array type to be used to actually implement the type. This
4178 -- is only needed for real array types (not for string literal types,
4179 -- since they are present only for the front end).
4181 if Is_Packed (Arr)
4182 and then (Is_Bit_Packed_Array (Arr) or else Non_Standard_Enum)
4183 and then Ekind (Arr) /= E_String_Literal_Subtype
4184 then
4185 Create_Packed_Array_Impl_Type (Arr);
4186 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
4188 -- Make sure that we have the necessary routines to implement the
4189 -- packing, and complain now if not. Note that we only test this
4190 -- for constrained array types.
4192 if Is_Constrained (Arr)
4193 and then Is_Bit_Packed_Array (Arr)
4194 and then Present (Packed_Array_Impl_Type (Arr))
4195 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
4196 then
4197 declare
4198 CS : constant Uint := Component_Size (Arr);
4199 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
4201 begin
4202 if RE /= RE_Null
4203 and then not RTE_Available (RE)
4204 then
4205 Error_Msg_CRT
4206 ("packing of " & UI_Image (CS) & "-bit components",
4207 First_Subtype (Etype (Arr)));
4209 -- Cancel the packing
4211 Set_Is_Packed (Base_Type (Arr), False);
4212 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
4213 Set_Packed_Array_Impl_Type (Arr, Empty);
4214 goto Skip_Packed;
4215 end if;
4216 end;
4217 end if;
4219 -- Size information of packed array type is copied to the array
4220 -- type, since this is really the representation. But do not
4221 -- override explicit existing size values. If the ancestor subtype
4222 -- is constrained the Packed_Array_Impl_Type will be inherited
4223 -- from it, but the size may have been provided already, and
4224 -- must not be overridden either.
4226 if not Has_Size_Clause (Arr)
4227 and then
4228 (No (Ancestor_Subtype (Arr))
4229 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
4230 then
4231 Copy_Esize (To => Arr, From => Packed_Array_Impl_Type (Arr));
4232 Copy_RM_Size (To => Arr, From => Packed_Array_Impl_Type (Arr));
4233 end if;
4235 if not Has_Alignment_Clause (Arr) then
4236 Copy_Alignment
4237 (To => Arr, From => Packed_Array_Impl_Type (Arr));
4238 end if;
4239 end if;
4241 <<Skip_Packed>>
4243 -- A Ghost type cannot have a component of protected or task type
4244 -- (SPARK RM 6.9(21)).
4246 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
4247 Error_Msg_N
4248 ("ghost array type & cannot have concurrent component type",
4249 Arr);
4250 end if;
4251 end Freeze_Array_Type;
4253 -------------------------------
4254 -- Freeze_Object_Declaration --
4255 -------------------------------
4257 procedure Freeze_Object_Declaration (E : Entity_Id) is
4258 procedure Check_Large_Modular_Array (Typ : Entity_Id);
4259 -- Check that the size of array type Typ can be computed without
4260 -- overflow, and generates a Storage_Error otherwise. This is only
4261 -- relevant for array types whose index is a modular type with
4262 -- Standard_Long_Long_Integer_Size bits: wrap-around arithmetic
4263 -- might yield a meaningless value for the length of the array,
4264 -- or its corresponding attribute.
4266 procedure Check_Pragma_Thread_Local_Storage (Var_Id : Entity_Id);
4267 -- Ensure that the initialization state of variable Var_Id subject
4268 -- to pragma Thread_Local_Storage agrees with the semantics of the
4269 -- pragma.
4271 function Has_Default_Initialization
4272 (Obj_Id : Entity_Id) return Boolean;
4273 -- Determine whether object Obj_Id default initialized
4275 -------------------------------
4276 -- Check_Large_Modular_Array --
4277 -------------------------------
4279 procedure Check_Large_Modular_Array (Typ : Entity_Id) is
4280 Obj_Loc : constant Source_Ptr := Sloc (E);
4281 Idx_Typ : Entity_Id;
4283 begin
4284 -- Nothing to do when expansion is disabled because this routine
4285 -- generates a runtime check.
4287 if not Expander_Active then
4288 return;
4290 -- Nothing to do for String literal subtypes because their index
4291 -- cannot be a modular type.
4293 elsif Ekind (Typ) = E_String_Literal_Subtype then
4294 return;
4296 -- Nothing to do for an imported object because the object will
4297 -- be created on the exporting side.
4299 elsif Is_Imported (E) then
4300 return;
4302 -- Nothing to do for unconstrained array types. This case arises
4303 -- when the object declaration is illegal.
4305 elsif not Is_Constrained (Typ) then
4306 return;
4307 end if;
4309 Idx_Typ := Etype (First_Index (Typ));
4311 -- To prevent arithmetic overflow with large values, we raise
4312 -- Storage_Error under the following guard:
4314 -- (Arr'Last / 2 - Arr'First / 2) > (2 ** 30)
4316 -- This takes care of the boundary case, but it is preferable to
4317 -- use a smaller limit, because even on 64-bit architectures an
4318 -- array of more than 2 ** 30 bytes is likely to raise
4319 -- Storage_Error.
4321 if Is_Modular_Integer_Type (Idx_Typ)
4322 and then RM_Size (Idx_Typ) = Standard_Long_Long_Integer_Size
4323 then
4324 -- Ensure that the type of the object is elaborated before
4325 -- the check itself is emitted to avoid elaboration issues
4326 -- in the code generator at the library level.
4328 if Is_Itype (Etype (E))
4329 and then In_Open_Scopes (Scope (Etype (E)))
4330 then
4331 declare
4332 Ref_Node : constant Node_Id :=
4333 Make_Itype_Reference (Obj_Loc);
4334 begin
4335 Set_Itype (Ref_Node, Etype (E));
4336 Insert_Action (Declaration_Node (E), Ref_Node);
4337 end;
4338 end if;
4340 Insert_Action (Declaration_Node (E),
4341 Make_Raise_Storage_Error (Obj_Loc,
4342 Condition =>
4343 Make_Op_Ge (Obj_Loc,
4344 Left_Opnd =>
4345 Make_Op_Subtract (Obj_Loc,
4346 Left_Opnd =>
4347 Make_Op_Divide (Obj_Loc,
4348 Left_Opnd =>
4349 Make_Attribute_Reference (Obj_Loc,
4350 Prefix =>
4351 New_Occurrence_Of (Typ, Obj_Loc),
4352 Attribute_Name => Name_Last),
4353 Right_Opnd =>
4354 Make_Integer_Literal (Obj_Loc, Uint_2)),
4355 Right_Opnd =>
4356 Make_Op_Divide (Obj_Loc,
4357 Left_Opnd =>
4358 Make_Attribute_Reference (Obj_Loc,
4359 Prefix =>
4360 New_Occurrence_Of (Typ, Obj_Loc),
4361 Attribute_Name => Name_First),
4362 Right_Opnd =>
4363 Make_Integer_Literal (Obj_Loc, Uint_2))),
4364 Right_Opnd =>
4365 Make_Integer_Literal (Obj_Loc, (Uint_2 ** 30))),
4366 Reason => SE_Object_Too_Large));
4367 end if;
4368 end Check_Large_Modular_Array;
4370 ---------------------------------------
4371 -- Check_Pragma_Thread_Local_Storage --
4372 ---------------------------------------
4374 procedure Check_Pragma_Thread_Local_Storage (Var_Id : Entity_Id) is
4375 function Has_Incompatible_Initialization
4376 (Var_Decl : Node_Id) return Boolean;
4377 -- Determine whether variable Var_Id with declaration Var_Decl is
4378 -- initialized with a value that violates the semantics of pragma
4379 -- Thread_Local_Storage.
4381 -------------------------------------
4382 -- Has_Incompatible_Initialization --
4383 -------------------------------------
4385 function Has_Incompatible_Initialization
4386 (Var_Decl : Node_Id) return Boolean
4388 Init_Expr : constant Node_Id := Expression (Var_Decl);
4390 begin
4391 -- The variable is default-initialized. This directly violates
4392 -- the semantics of the pragma.
4394 if Has_Default_Initialization (Var_Id) then
4395 return True;
4397 -- The variable has explicit initialization. In this case only
4398 -- a handful of values satisfy the semantics of the pragma.
4400 elsif Has_Init_Expression (Var_Decl)
4401 and then Present (Init_Expr)
4402 then
4403 -- "null" is a legal form of initialization
4405 if Nkind (Init_Expr) = N_Null then
4406 return False;
4408 -- A static expression is a legal form of initialization
4410 elsif Is_Static_Expression (Init_Expr) then
4411 return False;
4413 -- A static aggregate is a legal form of initialization
4415 elsif Nkind (Init_Expr) = N_Aggregate
4416 and then Compile_Time_Known_Aggregate (Init_Expr)
4417 then
4418 return False;
4420 -- All other initialization expressions violate the semantic
4421 -- of the pragma.
4423 else
4424 return True;
4425 end if;
4427 -- The variable lacks any kind of initialization, which agrees
4428 -- with the semantics of the pragma.
4430 else
4431 return False;
4432 end if;
4433 end Has_Incompatible_Initialization;
4435 -- Local declarations
4437 Var_Decl : constant Node_Id := Declaration_Node (Var_Id);
4439 -- Start of processing for Check_Pragma_Thread_Local_Storage
4441 begin
4442 -- A variable whose initialization is suppressed lacks any kind of
4443 -- initialization.
4445 if Suppress_Initialization (Var_Id) then
4446 null;
4448 -- The variable has default initialization, or is explicitly
4449 -- initialized to a value other than null, static expression,
4450 -- or a static aggregate.
4452 elsif Has_Incompatible_Initialization (Var_Decl) then
4453 Error_Msg_NE
4454 ("Thread_Local_Storage variable& is improperly initialized",
4455 Var_Decl, Var_Id);
4456 Error_Msg_NE
4457 ("\only allowed initialization is explicit NULL, static "
4458 & "expression or static aggregate", Var_Decl, Var_Id);
4459 end if;
4460 end Check_Pragma_Thread_Local_Storage;
4462 --------------------------------
4463 -- Has_Default_Initialization --
4464 --------------------------------
4466 function Has_Default_Initialization
4467 (Obj_Id : Entity_Id) return Boolean
4469 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
4470 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4472 begin
4473 return
4474 Comes_From_Source (Obj_Id)
4475 and then not Is_Imported (Obj_Id)
4476 and then not Has_Init_Expression (Obj_Decl)
4477 and then
4478 ((Has_Non_Null_Base_Init_Proc (Obj_Typ)
4479 and then not No_Initialization (Obj_Decl)
4480 and then not Initialization_Suppressed (Obj_Typ))
4481 or else
4482 (Needs_Simple_Initialization (Obj_Typ)
4483 and then not Is_Internal (Obj_Id)));
4484 end Has_Default_Initialization;
4486 -- Local variables
4488 Typ : constant Entity_Id := Etype (E);
4489 Def : Node_Id;
4491 -- Start of processing for Freeze_Object_Declaration
4493 begin
4494 -- Abstract type allowed only for C++ imported variables or constants
4496 -- Note: we inhibit this check for objects that do not come from
4497 -- source because there is at least one case (the expansion of
4498 -- x'Class'Input where x is abstract) where we legitimately
4499 -- generate an abstract object.
4501 if Is_Abstract_Type (Typ)
4502 and then Comes_From_Source (Parent (E))
4503 and then not (Is_Imported (E) and then Is_CPP_Class (Typ))
4504 then
4505 Def := Object_Definition (Parent (E));
4507 Error_Msg_N ("type of object cannot be abstract", Def);
4509 if Is_CPP_Class (Etype (E)) then
4510 Error_Msg_NE ("\} may need a cpp_constructor", Def, Typ);
4512 elsif Present (Expression (Parent (E))) then
4513 Error_Msg_N -- CODEFIX
4514 ("\maybe a class-wide type was meant", Def);
4515 end if;
4516 end if;
4518 -- For object created by object declaration, perform required
4519 -- categorization (preelaborate and pure) checks. Defer these
4520 -- checks to freeze time since pragma Import inhibits default
4521 -- initialization and thus pragma Import affects these checks.
4523 Validate_Object_Declaration (Declaration_Node (E));
4525 -- If there is an address clause, check that it is valid and if need
4526 -- be move initialization to the freeze node.
4528 Check_Address_Clause (E);
4530 -- Similar processing is needed for aspects that may affect object
4531 -- layout, like Address, if there is an initialization expression.
4532 -- We don't do this if there is a pragma Linker_Section, because it
4533 -- would prevent the back end from statically initializing the
4534 -- object; we don't want elaboration code in that case.
4536 if Has_Delayed_Aspects (E)
4537 and then Expander_Active
4538 and then Is_Array_Type (Typ)
4539 and then Present (Expression (Declaration_Node (E)))
4540 and then No (Linker_Section_Pragma (E))
4541 then
4542 declare
4543 Decl : constant Node_Id := Declaration_Node (E);
4544 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
4546 begin
4547 -- Capture initialization value at point of declaration, and
4548 -- make explicit assignment legal, because object may be a
4549 -- constant.
4551 Remove_Side_Effects (Expression (Decl));
4552 Set_Assignment_OK (Lhs);
4554 -- Move initialization to freeze actions
4556 Append_Freeze_Action (E,
4557 Make_Assignment_Statement (Loc,
4558 Name => Lhs,
4559 Expression => Expression (Decl)));
4561 Set_No_Initialization (Decl);
4562 -- Set_Is_Frozen (E, False);
4563 end;
4564 end if;
4566 -- Reset Is_True_Constant for non-constant aliased object. We
4567 -- consider that the fact that a non-constant object is aliased may
4568 -- indicate that some funny business is going on, e.g. an aliased
4569 -- object is passed by reference to a procedure which captures the
4570 -- address of the object, which is later used to assign a new value,
4571 -- even though the compiler thinks that it is not modified. Such
4572 -- code is highly dubious, but we choose to make it "work" for
4573 -- non-constant aliased objects.
4575 -- Note that we used to do this for all aliased objects, whether or
4576 -- not constant, but this caused anomalies down the line because we
4577 -- ended up with static objects that were not Is_True_Constant. Not
4578 -- resetting Is_True_Constant for (aliased) constant objects ensures
4579 -- that this anomaly never occurs.
4581 -- However, we don't do that for internal entities. We figure that if
4582 -- we deliberately set Is_True_Constant for an internal entity, e.g.
4583 -- a dispatch table entry, then we mean it.
4585 if Ekind (E) /= E_Constant
4586 and then (Is_Aliased (E) or else Is_Aliased (Typ))
4587 and then not Is_Internal_Name (Chars (E))
4588 then
4589 Set_Is_True_Constant (E, False);
4590 end if;
4592 -- If the object needs any kind of default initialization, an error
4593 -- must be issued if No_Default_Initialization applies. The check
4594 -- doesn't apply to imported objects, which are not ever default
4595 -- initialized, and is why the check is deferred until freezing, at
4596 -- which point we know if Import applies. Deferred constants are also
4597 -- exempted from this test because their completion is explicit, or
4598 -- through an import pragma.
4600 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
4601 null;
4603 elsif Has_Default_Initialization (E) then
4604 Check_Restriction
4605 (No_Default_Initialization, Declaration_Node (E));
4606 end if;
4608 -- Ensure that a variable subject to pragma Thread_Local_Storage
4610 -- * Lacks default initialization, or
4612 -- * The initialization expression is either "null", a static
4613 -- constant, or a compile-time known aggregate.
4615 if Has_Pragma_Thread_Local_Storage (E) then
4616 Check_Pragma_Thread_Local_Storage (E);
4617 end if;
4619 -- For imported objects, set Is_Public unless there is also an
4620 -- address clause, which means that there is no external symbol
4621 -- needed for the Import (Is_Public may still be set for other
4622 -- unrelated reasons). Note that we delayed this processing
4623 -- till freeze time so that we can be sure not to set the flag
4624 -- if there is an address clause. If there is such a clause,
4625 -- then the only purpose of the Import pragma is to suppress
4626 -- implicit initialization.
4628 if Is_Imported (E) and then No (Address_Clause (E)) then
4629 Set_Is_Public (E);
4630 end if;
4632 -- For source objects that are not Imported and are library level, if
4633 -- no linker section pragma was given inherit the appropriate linker
4634 -- section from the corresponding type.
4636 if Comes_From_Source (E)
4637 and then not Is_Imported (E)
4638 and then Is_Library_Level_Entity (E)
4639 and then No (Linker_Section_Pragma (E))
4640 then
4641 Set_Linker_Section_Pragma (E, Linker_Section_Pragma (Typ));
4642 end if;
4644 -- For convention C objects of an enumeration type, warn if the size
4645 -- is not integer size and no explicit size given. Skip warning for
4646 -- Boolean and Character, and assume programmer expects 8-bit sizes
4647 -- for these cases.
4649 if (Convention (E) = Convention_C
4650 or else
4651 Convention (E) = Convention_CPP)
4652 and then Is_Enumeration_Type (Typ)
4653 and then not Is_Character_Type (Typ)
4654 and then not Is_Boolean_Type (Typ)
4655 and then Esize (Typ) < Standard_Integer_Size
4656 and then not Has_Size_Clause (E)
4657 then
4658 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
4659 Error_Msg_N
4660 ("??convention C enumeration object has size less than ^", E);
4661 Error_Msg_N ("\??use explicit size clause to set size", E);
4662 end if;
4664 -- Declaring too big an array in disabled ghost code is OK
4666 if Is_Array_Type (Typ) and then not Is_Ignored_Ghost_Entity (E) then
4667 Check_Large_Modular_Array (Typ);
4668 end if;
4669 end Freeze_Object_Declaration;
4671 -----------------------------
4672 -- Freeze_Generic_Entities --
4673 -----------------------------
4675 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
4676 E : Entity_Id;
4677 F : Node_Id;
4678 Flist : List_Id;
4680 begin
4681 Flist := New_List;
4682 E := First_Entity (Pack);
4683 while Present (E) loop
4684 if Is_Type (E) and then not Is_Generic_Type (E) then
4685 F := Make_Freeze_Generic_Entity (Sloc (Pack));
4686 Set_Entity (F, E);
4687 Append_To (Flist, F);
4689 elsif Ekind (E) = E_Generic_Package then
4690 Append_List_To (Flist, Freeze_Generic_Entities (E));
4691 end if;
4693 Next_Entity (E);
4694 end loop;
4696 return Flist;
4697 end Freeze_Generic_Entities;
4699 --------------------
4700 -- Freeze_Profile --
4701 --------------------
4703 function Freeze_Profile (E : Entity_Id) return Boolean is
4704 F_Type : Entity_Id;
4705 R_Type : Entity_Id;
4706 Warn_Node : Node_Id;
4708 begin
4709 -- Loop through formals
4711 Formal := First_Formal (E);
4712 while Present (Formal) loop
4713 F_Type := Etype (Formal);
4715 -- AI05-0151: incomplete types can appear in a profile. By the
4716 -- time the entity is frozen, the full view must be available,
4717 -- unless it is a limited view.
4719 if Is_Incomplete_Type (F_Type)
4720 and then Present (Full_View (F_Type))
4721 and then not From_Limited_With (F_Type)
4722 then
4723 F_Type := Full_View (F_Type);
4724 Set_Etype (Formal, F_Type);
4725 end if;
4727 if not From_Limited_With (F_Type)
4728 and then Should_Freeze_Type (F_Type, E, N)
4729 then
4730 Freeze_And_Append (F_Type, N, Result);
4731 end if;
4733 if Is_Private_Type (F_Type)
4734 and then Is_Private_Type (Base_Type (F_Type))
4735 and then No (Full_View (Base_Type (F_Type)))
4736 and then not Is_Generic_Type (F_Type)
4737 and then not Is_Derived_Type (F_Type)
4738 then
4739 -- If the type of a formal is incomplete, subprogram is being
4740 -- frozen prematurely. Within an instance (but not within a
4741 -- wrapper package) this is an artifact of our need to regard
4742 -- the end of an instantiation as a freeze point. Otherwise it
4743 -- is a definite error.
4745 if In_Instance then
4746 Set_Is_Frozen (E, False);
4747 Result := No_List;
4748 return False;
4750 elsif not After_Last_Declaration then
4751 Error_Msg_NE
4752 ("type & must be fully defined before this point",
4754 F_Type);
4755 end if;
4756 end if;
4758 -- Check suspicious parameter for C function. These tests apply
4759 -- only to exported/imported subprograms.
4761 if Warn_On_Export_Import
4762 and then Comes_From_Source (E)
4763 and then Convention (E) in Convention_C_Family
4764 and then (Is_Imported (E) or else Is_Exported (E))
4765 and then Convention (E) /= Convention (Formal)
4766 and then not Has_Warnings_Off (E)
4767 and then not Has_Warnings_Off (F_Type)
4768 and then not Has_Warnings_Off (Formal)
4769 then
4770 -- Qualify mention of formals with subprogram name
4772 Error_Msg_Qual_Level := 1;
4774 -- Check suspicious use of fat C pointer, but do not emit
4775 -- a warning on an access to subprogram when unnesting is
4776 -- active.
4778 if Is_Access_Type (F_Type)
4779 and then Known_Esize (F_Type)
4780 and then Esize (F_Type) > Ttypes.System_Address_Size
4781 and then (not Unnest_Subprogram_Mode
4782 or else not Is_Access_Subprogram_Type (F_Type))
4783 then
4784 Error_Msg_N
4785 ("?x?type of & does not correspond to C pointer!", Formal);
4787 -- Check suspicious return of boolean
4789 elsif Root_Type (F_Type) = Standard_Boolean
4790 and then Convention (F_Type) = Convention_Ada
4791 and then not Has_Warnings_Off (F_Type)
4792 and then not Has_Size_Clause (F_Type)
4793 then
4794 Error_Msg_N
4795 ("& is an 8-bit Ada Boolean?x?", Formal);
4796 Error_Msg_N
4797 ("\use appropriate corresponding type in C "
4798 & "(e.g. char)?x?", Formal);
4800 -- Check suspicious tagged type
4802 elsif (Is_Tagged_Type (F_Type)
4803 or else
4804 (Is_Access_Type (F_Type)
4805 and then Is_Tagged_Type (Designated_Type (F_Type))))
4806 and then Convention (E) = Convention_C
4807 then
4808 Error_Msg_N
4809 ("?x?& involves a tagged type which does not "
4810 & "correspond to any C type!", Formal);
4812 -- Check wrong convention subprogram pointer
4814 elsif Ekind (F_Type) = E_Access_Subprogram_Type
4815 and then not Has_Foreign_Convention (F_Type)
4816 then
4817 Error_Msg_N
4818 ("?x?subprogram pointer & should "
4819 & "have foreign convention!", Formal);
4820 Error_Msg_Sloc := Sloc (F_Type);
4821 Error_Msg_NE
4822 ("\?x?add Convention pragma to declaration of &#",
4823 Formal, F_Type);
4824 end if;
4826 -- Turn off name qualification after message output
4828 Error_Msg_Qual_Level := 0;
4829 end if;
4831 -- Check for unconstrained array in exported foreign convention
4832 -- case.
4834 if Has_Foreign_Convention (E)
4835 and then not Is_Imported (E)
4836 and then Is_Array_Type (F_Type)
4837 and then not Is_Constrained (F_Type)
4838 and then Warn_On_Export_Import
4839 then
4840 Error_Msg_Qual_Level := 1;
4842 -- If this is an inherited operation, place the warning on
4843 -- the derived type declaration, rather than on the original
4844 -- subprogram.
4846 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
4847 then
4848 Warn_Node := Parent (E);
4850 if Formal = First_Formal (E) then
4851 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
4852 end if;
4853 else
4854 Warn_Node := Formal;
4855 end if;
4857 Error_Msg_NE ("?x?type of argument& is unconstrained array",
4858 Warn_Node, Formal);
4859 Error_Msg_N ("\?x?foreign caller must pass bounds explicitly",
4860 Warn_Node);
4861 Error_Msg_Qual_Level := 0;
4862 end if;
4864 if not From_Limited_With (F_Type) then
4865 if Is_Access_Type (F_Type) then
4866 F_Type := Designated_Type (F_Type);
4867 end if;
4868 end if;
4870 Next_Formal (Formal);
4871 end loop;
4873 -- Case of function: similar checks on return type
4875 if Ekind (E) = E_Function then
4877 -- Freeze return type
4879 R_Type := Etype (E);
4881 -- AI05-0151: the return type may have been incomplete at the
4882 -- point of declaration. Replace it with the full view, unless the
4883 -- current type is a limited view. In that case the full view is
4884 -- in a different unit, and gigi finds the non-limited view after
4885 -- the other unit is elaborated.
4887 if Ekind (R_Type) = E_Incomplete_Type
4888 and then Present (Full_View (R_Type))
4889 and then not From_Limited_With (R_Type)
4890 then
4891 R_Type := Full_View (R_Type);
4892 Set_Etype (E, R_Type);
4893 end if;
4895 if Should_Freeze_Type (R_Type, E, N) then
4896 Freeze_And_Append (R_Type, N, Result);
4897 end if;
4899 -- Check suspicious return type for C function
4901 if Warn_On_Export_Import
4902 and then Comes_From_Source (E)
4903 and then Convention (E) in Convention_C_Family
4904 and then (Is_Imported (E) or else Is_Exported (E))
4905 then
4906 -- Check suspicious return of fat C pointer
4908 if Is_Access_Type (R_Type)
4909 and then Known_Esize (R_Type)
4910 and then Esize (R_Type) > Ttypes.System_Address_Size
4911 and then not Has_Warnings_Off (E)
4912 and then not Has_Warnings_Off (R_Type)
4913 then
4914 Error_Msg_N
4915 ("?x?return type of& does not correspond to C pointer!",
4918 -- Check suspicious return of boolean
4920 elsif Root_Type (R_Type) = Standard_Boolean
4921 and then Convention (R_Type) = Convention_Ada
4922 and then not Has_Warnings_Off (E)
4923 and then not Has_Warnings_Off (R_Type)
4924 and then not Has_Size_Clause (R_Type)
4925 then
4926 declare
4927 N : constant Node_Id :=
4928 Result_Definition (Declaration_Node (E));
4929 begin
4930 Error_Msg_NE
4931 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
4932 Error_Msg_NE
4933 ("\use appropriate corresponding type in C "
4934 & "(e.g. char)?x?", N, E);
4935 end;
4937 -- Check suspicious return tagged type
4939 elsif (Is_Tagged_Type (R_Type)
4940 or else (Is_Access_Type (R_Type)
4941 and then
4942 Is_Tagged_Type
4943 (Designated_Type (R_Type))))
4944 and then Convention (E) = Convention_C
4945 and then not Has_Warnings_Off (E)
4946 and then not Has_Warnings_Off (R_Type)
4947 then
4948 Error_Msg_N ("?x?return type of & does not "
4949 & "correspond to C type!", E);
4951 -- Check return of wrong convention subprogram pointer
4953 elsif Ekind (R_Type) = E_Access_Subprogram_Type
4954 and then not Has_Foreign_Convention (R_Type)
4955 and then not Has_Warnings_Off (E)
4956 and then not Has_Warnings_Off (R_Type)
4957 then
4958 Error_Msg_N ("?x?& should return a foreign "
4959 & "convention subprogram pointer", E);
4960 Error_Msg_Sloc := Sloc (R_Type);
4961 Error_Msg_NE
4962 ("\?x?add Convention pragma to declaration of& #",
4963 E, R_Type);
4964 end if;
4965 end if;
4967 -- Give warning for suspicious return of a result of an
4968 -- unconstrained array type in a foreign convention function.
4970 if Has_Foreign_Convention (E)
4972 -- We are looking for a return of unconstrained array
4974 and then Is_Array_Type (R_Type)
4975 and then not Is_Constrained (R_Type)
4977 -- Exclude imported routines, the warning does not belong on
4978 -- the import, but rather on the routine definition.
4980 and then not Is_Imported (E)
4982 -- Check that general warning is enabled, and that it is not
4983 -- suppressed for this particular case.
4985 and then Warn_On_Export_Import
4986 and then not Has_Warnings_Off (E)
4987 and then not Has_Warnings_Off (R_Type)
4988 then
4989 Error_Msg_N
4990 ("?x?foreign convention function& should not return "
4991 & "unconstrained array!", E);
4992 end if;
4993 end if;
4995 -- Check suspicious use of Import in pure unit (cases where the RM
4996 -- allows calls to be omitted).
4998 if Is_Imported (E)
5000 -- It might be suspicious if the compilation unit has the Pure
5001 -- aspect/pragma.
5003 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
5005 -- The RM allows omission of calls only in the case of
5006 -- library-level subprograms (see RM-10.2.1(18)).
5008 and then Is_Library_Level_Entity (E)
5010 -- Ignore internally generated entity. This happens in some cases
5011 -- of subprograms in specs, where we generate an implied body.
5013 and then Comes_From_Source (Import_Pragma (E))
5015 -- Assume run-time knows what it is doing
5017 and then not GNAT_Mode
5019 -- Assume explicit Pure_Function means import is pure
5021 and then not Has_Pragma_Pure_Function (E)
5023 -- Don't need warning in relaxed semantics mode
5025 and then not Relaxed_RM_Semantics
5027 -- Assume convention Intrinsic is OK, since this is specialized.
5028 -- This deals with the DEC unit current_exception.ads
5030 and then Convention (E) /= Convention_Intrinsic
5032 -- Assume that ASM interface knows what it is doing
5034 and then Convention (E) /= Convention_Assembler
5035 then
5036 Error_Msg_N
5037 ("pragma Import in Pure unit??", Import_Pragma (E));
5038 Error_Msg_NE
5039 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
5040 Import_Pragma (E), E);
5041 end if;
5043 return True;
5044 end Freeze_Profile;
5046 ------------------------
5047 -- Freeze_Record_Type --
5048 ------------------------
5050 procedure Freeze_Record_Type (Rec : Entity_Id) is
5051 ADC : Node_Id;
5052 Comp : Entity_Id;
5053 IR : Node_Id;
5054 Prev : Entity_Id;
5056 Junk : Boolean;
5057 pragma Warnings (Off, Junk);
5059 Aliased_Component : Boolean := False;
5060 -- Set True if we find at least one component which is aliased. This
5061 -- is used to prevent Implicit_Packing of the record, since packing
5062 -- cannot modify the size of alignment of an aliased component.
5064 All_Elem_Components : Boolean := True;
5065 -- True if all components are of a type whose underlying type is
5066 -- elementary.
5068 All_Sized_Components : Boolean := True;
5069 -- True if all components have a known RM_Size
5071 All_Storage_Unit_Components : Boolean := True;
5072 -- True if all components have an RM_Size that is a multiple of the
5073 -- storage unit.
5075 Elem_Component_Total_Esize : Uint := Uint_0;
5076 -- Accumulates total Esize values of all elementary components. Used
5077 -- for processing of Implicit_Packing.
5079 Final_Storage_Only : Boolean := True;
5080 -- Used to compute the Finalize_Storage_Only flag
5082 Placed_Component : Boolean := False;
5083 -- Set True if we find at least one component with a component
5084 -- clause (used to warn about useless Bit_Order pragmas, and also
5085 -- to detect cases where Implicit_Packing may have an effect).
5087 Relaxed_Finalization : Boolean := True;
5088 -- Used to compute the Has_Relaxed_Finalization flag
5090 Sized_Component_Total_RM_Size : Uint := Uint_0;
5091 -- Accumulates total RM_Size values of all sized components. Used
5092 -- for processing of Implicit_Packing.
5094 Sized_Component_Total_Round_RM_Size : Uint := Uint_0;
5095 -- Accumulates total RM_Size values of all sized components, rounded
5096 -- individually to a multiple of the storage unit.
5098 SSO_ADC : Node_Id;
5099 -- Scalar_Storage_Order attribute definition clause for the record
5101 SSO_ADC_Component : Boolean := False;
5102 -- Set True if we find at least one component whose type has a
5103 -- Scalar_Storage_Order attribute definition clause.
5105 Unplaced_Component : Boolean := False;
5106 -- Set True if we find at least one component with no component
5107 -- clause (used to warn about useless Pack pragmas).
5109 procedure Check_Itype (Typ : Entity_Id);
5110 -- If the component subtype is an access to a constrained subtype of
5111 -- an already frozen type, make the subtype frozen as well. It might
5112 -- otherwise be frozen in the wrong scope, and a freeze node on
5113 -- subtype has no effect. Similarly, if the component subtype is a
5114 -- regular (not protected) access to subprogram, set the anonymous
5115 -- subprogram type to frozen as well, to prevent an out-of-scope
5116 -- freeze node at some eventual point of call. Protected operations
5117 -- are handled elsewhere.
5119 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
5120 -- Make sure that all types mentioned in Discrete_Choices of the
5121 -- variants referenceed by the Variant_Part VP are frozen. This is
5122 -- a recursive routine to deal with nested variants.
5124 -----------------
5125 -- Check_Itype --
5126 -----------------
5128 procedure Check_Itype (Typ : Entity_Id) is
5129 Desig : constant Entity_Id := Designated_Type (Typ);
5131 begin
5132 if not Is_Frozen (Desig)
5133 and then Is_Frozen (Base_Type (Desig))
5134 then
5135 Set_Is_Frozen (Desig);
5137 -- In addition, add an Itype_Reference to ensure that the
5138 -- access subtype is elaborated early enough. This cannot be
5139 -- done if the subtype may depend on discriminants.
5141 if Ekind (Comp) = E_Component
5142 and then Is_Itype (Etype (Comp))
5143 and then not Has_Discriminants (Rec)
5144 then
5145 IR := Make_Itype_Reference (Sloc (Comp));
5146 Set_Itype (IR, Desig);
5147 Add_To_Result (IR);
5148 end if;
5150 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
5151 and then Convention (Desig) /= Convention_Protected
5152 then
5153 Set_Is_Frozen (Desig);
5154 Create_Extra_Formals (Desig);
5155 end if;
5156 end Check_Itype;
5158 ------------------------------------
5159 -- Freeze_Choices_In_Variant_Part --
5160 ------------------------------------
5162 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
5163 pragma Assert (Nkind (VP) = N_Variant_Part);
5165 Variant : Node_Id;
5166 Choice : Node_Id;
5167 CL : Node_Id;
5169 begin
5170 -- Loop through variants
5172 Variant := First_Non_Pragma (Variants (VP));
5173 while Present (Variant) loop
5175 -- Loop through choices, checking that all types are frozen
5177 Choice := First_Non_Pragma (Discrete_Choices (Variant));
5178 while Present (Choice) loop
5179 if Nkind (Choice) in N_Has_Etype
5180 and then Present (Etype (Choice))
5181 then
5182 Freeze_And_Append (Etype (Choice), N, Result);
5183 end if;
5185 Next_Non_Pragma (Choice);
5186 end loop;
5188 -- Check for nested variant part to process
5190 CL := Component_List (Variant);
5192 if not Null_Present (CL) then
5193 if Present (Variant_Part (CL)) then
5194 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
5195 end if;
5196 end if;
5198 Next_Non_Pragma (Variant);
5199 end loop;
5200 end Freeze_Choices_In_Variant_Part;
5202 -- Start of processing for Freeze_Record_Type
5204 begin
5205 -- Freeze components and embedded subtypes
5207 Comp := First_Entity (Rec);
5208 Prev := Empty;
5209 while Present (Comp) loop
5210 if Is_Aliased (Comp) then
5211 Aliased_Component := True;
5212 end if;
5214 -- Handle the component and discriminant case
5216 if Ekind (Comp) in E_Component | E_Discriminant then
5217 declare
5218 CC : constant Node_Id := Component_Clause (Comp);
5220 begin
5221 -- Freezing a record type freezes the type of each of its
5222 -- components. However, if the type of the component is
5223 -- part of this record, we do not want or need a separate
5224 -- Freeze_Node. Note that Is_Itype is wrong because that's
5225 -- also set in private type cases. We also can't check for
5226 -- the Scope being exactly Rec because of private types and
5227 -- record extensions.
5229 if Is_Itype (Etype (Comp))
5230 and then Is_Record_Type (Underlying_Type
5231 (Scope (Etype (Comp))))
5232 then
5233 Undelay_Type (Etype (Comp));
5234 end if;
5236 Freeze_And_Append (Etype (Comp), N, Result);
5238 -- Warn for pragma Pack overriding foreign convention
5240 if Has_Foreign_Convention (Etype (Comp))
5241 and then Has_Pragma_Pack (Rec)
5243 -- Don't warn for aliased components, since override
5244 -- cannot happen in that case.
5246 and then not Is_Aliased (Comp)
5247 then
5248 declare
5249 CN : constant Name_Id :=
5250 Get_Convention_Name (Convention (Etype (Comp)));
5251 PP : constant Node_Id :=
5252 Get_Pragma (Rec, Pragma_Pack);
5253 begin
5254 if Present (PP) then
5255 Error_Msg_Name_1 := CN;
5256 Error_Msg_Sloc := Sloc (Comp);
5257 Error_Msg_N
5258 ("pragma Pack affects convention % component#??",
5259 PP);
5260 Error_Msg_Name_1 := CN;
5261 Error_Msg_NE
5262 ("\component & may not have % compatible "
5263 & "representation??", PP, Comp);
5264 end if;
5265 end;
5266 end if;
5268 -- Check for error of component clause given for variable
5269 -- sized type. We have to delay this test till this point,
5270 -- since the component type has to be frozen for us to know
5271 -- if it is variable length.
5273 if Present (CC) then
5274 Placed_Component := True;
5276 -- We omit this test in a generic context, it will be
5277 -- applied at instantiation time.
5279 if Inside_A_Generic then
5280 null;
5282 -- Also omit this test in CodePeer mode, since we do not
5283 -- have sufficient info on size and rep clauses.
5285 elsif CodePeer_Mode then
5286 null;
5288 -- Do the check
5290 elsif not
5291 Size_Known_At_Compile_Time
5292 (Underlying_Type (Etype (Comp)))
5293 then
5294 Error_Msg_N
5295 ("component clause not allowed for variable " &
5296 "length component", CC);
5297 end if;
5299 else
5300 Unplaced_Component := True;
5301 end if;
5303 -- Case of component requires byte alignment
5305 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
5307 -- Set the enclosing record to also require byte align
5309 Set_Must_Be_On_Byte_Boundary (Rec);
5311 -- Check for component clause that is inconsistent with
5312 -- the required byte boundary alignment.
5314 if Present (CC)
5315 and then Normalized_First_Bit (Comp) mod
5316 System_Storage_Unit /= 0
5317 then
5318 Error_Msg_N
5319 ("component & must be byte aligned",
5320 Component_Name (Component_Clause (Comp)));
5321 end if;
5322 end if;
5323 end;
5324 end if;
5326 -- Gather data for possible Implicit_Packing later. Note that at
5327 -- this stage we might be dealing with a real component, or with
5328 -- an implicit subtype declaration.
5330 if Known_Static_RM_Size (Etype (Comp)) then
5331 declare
5332 Comp_Type : constant Entity_Id := Etype (Comp);
5333 Comp_Size : constant Uint := RM_Size (Comp_Type);
5334 SSU : constant Int := Ttypes.System_Storage_Unit;
5336 begin
5337 Sized_Component_Total_RM_Size :=
5338 Sized_Component_Total_RM_Size + Comp_Size;
5340 Sized_Component_Total_Round_RM_Size :=
5341 Sized_Component_Total_Round_RM_Size +
5342 (Comp_Size + SSU - 1) / SSU * SSU;
5344 if Present (Underlying_Type (Comp_Type))
5345 and then Is_Elementary_Type (Underlying_Type (Comp_Type))
5346 then
5347 Elem_Component_Total_Esize :=
5348 Elem_Component_Total_Esize + Esize (Comp_Type);
5349 else
5350 All_Elem_Components := False;
5352 if Comp_Size mod SSU /= 0 then
5353 All_Storage_Unit_Components := False;
5354 end if;
5355 end if;
5356 end;
5357 else
5358 All_Sized_Components := False;
5359 end if;
5361 -- If the component is an Itype with Delayed_Freeze and is either
5362 -- a record or array subtype and its base type has not yet been
5363 -- frozen, we must remove this from the entity list of this record
5364 -- and put it on the entity list of the scope of its base type.
5365 -- Note that we know that this is not the type of a component
5366 -- since we cleared Has_Delayed_Freeze for it in the previous
5367 -- loop. Thus this must be the Designated_Type of an access type,
5368 -- which is the type of a component.
5370 if Is_Itype (Comp)
5371 and then Is_Type (Scope (Comp))
5372 and then Is_Composite_Type (Comp)
5373 and then Base_Type (Comp) /= Comp
5374 and then Has_Delayed_Freeze (Comp)
5375 and then not Is_Frozen (Base_Type (Comp))
5376 then
5377 declare
5378 Will_Be_Frozen : Boolean := False;
5379 S : Entity_Id;
5381 begin
5382 -- We have a difficult case to handle here. Suppose Rec is
5383 -- subtype being defined in a subprogram that's created as
5384 -- part of the freezing of Rec'Base. In that case, we know
5385 -- that Comp'Base must have already been frozen by the time
5386 -- we get to elaborate this because Gigi doesn't elaborate
5387 -- any bodies until it has elaborated all of the declarative
5388 -- part. But Is_Frozen will not be set at this point because
5389 -- we are processing code in lexical order.
5391 -- We detect this case by going up the Scope chain of Rec
5392 -- and seeing if we have a subprogram scope before reaching
5393 -- the top of the scope chain or that of Comp'Base. If we
5394 -- do, then mark that Comp'Base will actually be frozen. If
5395 -- so, we merely undelay it.
5397 S := Scope (Rec);
5398 while Present (S) loop
5399 if Is_Subprogram (S) then
5400 Will_Be_Frozen := True;
5401 exit;
5402 elsif S = Scope (Base_Type (Comp)) then
5403 exit;
5404 end if;
5406 S := Scope (S);
5407 end loop;
5409 if Will_Be_Frozen then
5410 Undelay_Type (Comp);
5412 else
5413 if Present (Prev) then
5414 Link_Entities (Prev, Next_Entity (Comp));
5415 else
5416 Set_First_Entity (Rec, Next_Entity (Comp));
5417 end if;
5419 -- Insert in entity list of scope of base type (which
5420 -- must be an enclosing scope, because still unfrozen).
5422 Append_Entity (Comp, Scope (Base_Type (Comp)));
5423 end if;
5424 end;
5426 -- If the component is an access type with an allocator as default
5427 -- value, the designated type will be frozen by the corresponding
5428 -- expression in init_proc. In order to place the freeze node for
5429 -- the designated type before that for the current record type,
5430 -- freeze it now.
5432 -- Same process if the component is an array of access types,
5433 -- initialized with an aggregate. If the designated type is
5434 -- private, it cannot contain allocators, and it is premature
5435 -- to freeze the type, so we check for this as well.
5437 elsif Is_Access_Type (Etype (Comp))
5438 and then Present (Parent (Comp))
5439 and then
5440 Nkind (Parent (Comp))
5441 in N_Component_Declaration | N_Discriminant_Specification
5442 and then Present (Expression (Parent (Comp)))
5443 then
5444 declare
5445 Alloc : constant Node_Id :=
5446 Unqualify (Expression (Parent (Comp)));
5448 begin
5449 if Nkind (Alloc) = N_Allocator then
5451 -- If component is pointer to a class-wide type, freeze
5452 -- the specific type in the expression being allocated.
5453 -- The expression may be a subtype indication, in which
5454 -- case freeze the subtype mark.
5456 if Is_Class_Wide_Type (Designated_Type (Etype (Comp)))
5457 then
5458 if Is_Entity_Name (Expression (Alloc)) then
5459 Freeze_And_Append
5460 (Entity (Expression (Alloc)), N, Result);
5462 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
5463 then
5464 Freeze_And_Append
5465 (Entity (Subtype_Mark (Expression (Alloc))),
5466 N, Result);
5467 end if;
5468 elsif Is_Itype (Designated_Type (Etype (Comp))) then
5469 Check_Itype (Etype (Comp));
5470 else
5471 Freeze_And_Append
5472 (Designated_Type (Etype (Comp)), N, Result);
5473 end if;
5474 end if;
5475 end;
5476 elsif Is_Access_Type (Etype (Comp))
5477 and then Is_Itype (Designated_Type (Etype (Comp)))
5478 then
5479 Check_Itype (Etype (Comp));
5481 -- Freeze the designated type when initializing a component with
5482 -- an aggregate in case the aggregate contains allocators.
5484 -- type T is ...;
5485 -- type T_Ptr is access all T;
5486 -- type T_Array is array ... of T_Ptr;
5488 -- type Rec is record
5489 -- Comp : T_Array := (others => ...);
5490 -- end record;
5492 elsif Is_Array_Type (Etype (Comp))
5493 and then Is_Access_Type (Component_Type (Etype (Comp)))
5494 then
5495 declare
5496 Comp_Par : constant Node_Id := Parent (Comp);
5497 Desig_Typ : constant Entity_Id :=
5498 Designated_Type
5499 (Component_Type (Etype (Comp)));
5501 begin
5502 -- The only case when this sort of freezing is not done is
5503 -- when the designated type is class-wide and the root type
5504 -- is the record owning the component. This scenario results
5505 -- in a circularity because the class-wide type requires
5506 -- primitives that have not been created yet as the root
5507 -- type is in the process of being frozen.
5509 -- type Rec is tagged;
5510 -- type Rec_Ptr is access all Rec'Class;
5511 -- type Rec_Array is array ... of Rec_Ptr;
5513 -- type Rec is record
5514 -- Comp : Rec_Array := (others => ...);
5515 -- end record;
5517 if Is_Class_Wide_Type (Desig_Typ)
5518 and then Root_Type (Desig_Typ) = Rec
5519 then
5520 null;
5522 elsif Is_Fully_Defined (Desig_Typ)
5523 and then Present (Comp_Par)
5524 and then Nkind (Comp_Par) = N_Component_Declaration
5525 and then Present (Expression (Comp_Par))
5526 and then Nkind (Expression (Comp_Par)) = N_Aggregate
5527 then
5528 Freeze_And_Append (Desig_Typ, N, Result);
5529 end if;
5530 end;
5531 end if;
5533 Prev := Comp;
5534 Next_Entity (Comp);
5535 end loop;
5537 SSO_ADC :=
5538 Get_Attribute_Definition_Clause
5539 (Rec, Attribute_Scalar_Storage_Order);
5541 -- If the record type has Complex_Representation, then it is treated
5542 -- as a scalar in the back end so the storage order is irrelevant.
5544 if Has_Complex_Representation (Rec) then
5545 if Present (SSO_ADC) then
5546 Error_Msg_N
5547 ("??storage order has no effect with Complex_Representation",
5548 SSO_ADC);
5549 end if;
5551 else
5552 -- Deal with default setting of reverse storage order
5554 Set_SSO_From_Default (Rec);
5556 -- Check consistent attribute setting on component types
5558 declare
5559 Comp_ADC_Present : Boolean;
5560 begin
5561 Comp := First_Component (Rec);
5562 while Present (Comp) loop
5563 Check_Component_Storage_Order
5564 (Encl_Type => Rec,
5565 Comp => Comp,
5566 ADC => SSO_ADC,
5567 Comp_ADC_Present => Comp_ADC_Present);
5568 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
5569 Next_Component (Comp);
5570 end loop;
5571 end;
5573 -- Now deal with reverse storage order/bit order issues
5575 if Present (SSO_ADC) then
5577 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
5578 -- if the former is specified.
5580 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
5582 -- Note: report error on Rec, not on SSO_ADC, as ADC may
5583 -- apply to some ancestor type.
5585 Error_Msg_Sloc := Sloc (SSO_ADC);
5586 Error_Msg_N
5587 ("scalar storage order for& specified# inconsistent with "
5588 & "bit order", Rec);
5589 end if;
5591 -- Warn if there is a Scalar_Storage_Order attribute definition
5592 -- clause but no component clause, no component that itself has
5593 -- such an attribute definition, and no pragma Pack.
5595 if not (Placed_Component
5596 or else
5597 SSO_ADC_Component
5598 or else
5599 Is_Packed (Rec))
5600 then
5601 Error_Msg_N
5602 ("??scalar storage order specified but no component "
5603 & "clause", SSO_ADC);
5604 end if;
5605 end if;
5606 end if;
5608 -- Deal with Bit_Order aspect
5610 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
5612 if Present (ADC) and then Base_Type (Rec) = Rec then
5613 if not (Placed_Component
5614 or else Present (SSO_ADC)
5615 or else Is_Packed (Rec))
5616 then
5617 -- Warn if clause has no effect when no component clause is
5618 -- present, but suppress warning if the Bit_Order is required
5619 -- due to the presence of a Scalar_Storage_Order attribute.
5621 Error_Msg_N
5622 ("??bit order specification has no effect", ADC);
5623 Error_Msg_N
5624 ("\??since no component clauses were specified", ADC);
5626 -- Here is where we do the processing to adjust component clauses
5627 -- for reversed bit order, when not using reverse SSO. If an error
5628 -- has been reported on Rec already (such as SSO incompatible with
5629 -- bit order), don't bother adjusting as this may generate extra
5630 -- noise.
5632 elsif Reverse_Bit_Order (Rec)
5633 and then not Reverse_Storage_Order (Rec)
5634 and then not Error_Posted (Rec)
5635 then
5636 Adjust_Record_For_Reverse_Bit_Order (Rec);
5638 -- Case where we have both an explicit Bit_Order and the same
5639 -- Scalar_Storage_Order: leave record untouched, the back-end
5640 -- will take care of required layout conversions.
5642 else
5643 null;
5645 end if;
5646 end if;
5648 -- Check for useless pragma Pack when all components placed. We only
5649 -- do this check for record types, not subtypes, since a subtype may
5650 -- have all its components placed, and it still makes perfectly good
5651 -- sense to pack other subtypes or the parent type. We do not give
5652 -- this warning if Optimize_Alignment is set to Space, since the
5653 -- pragma Pack does have an effect in this case (it always resets
5654 -- the alignment to one).
5656 if Ekind (Rec) = E_Record_Type
5657 and then Is_Packed (Rec)
5658 and then not Unplaced_Component
5659 and then Optimize_Alignment /= 'S'
5660 then
5661 -- Reset packed status. Probably not necessary, but we do it so
5662 -- that there is no chance of the back end doing something strange
5663 -- with this redundant indication of packing.
5665 Set_Is_Packed (Rec, False);
5667 -- Give warning if redundant constructs warnings on
5669 if Warn_On_Redundant_Constructs then
5670 Error_Msg_N -- CODEFIX
5671 ("?r?pragma Pack has no effect, no unplaced components",
5672 Get_Rep_Pragma (Rec, Name_Pack));
5673 end if;
5674 end if;
5676 -- If this is the record corresponding to a remote type, freeze the
5677 -- remote type here since that is what we are semantically freezing.
5678 -- This prevents the freeze node for that type in an inner scope.
5680 if Ekind (Rec) = E_Record_Type then
5681 if Present (Corresponding_Remote_Type (Rec)) then
5682 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
5683 end if;
5685 -- Check for tasks, protected and controlled components, unchecked
5686 -- unions, and type invariants.
5688 Comp := First_Component (Rec);
5689 while Present (Comp) loop
5690 Propagate_Concurrent_Flags (Rec, Etype (Comp));
5692 -- Do not set Has_Controlled_Component on a class-wide
5693 -- equivalent type. See Make_CW_Equivalent_Type.
5695 if not Is_Class_Wide_Equivalent_Type (Rec)
5696 and then
5697 (Has_Controlled_Component (Etype (Comp))
5698 or else
5699 (Chars (Comp) /= Name_uParent
5700 and then Is_Controlled (Etype (Comp)))
5701 or else
5702 (Is_Protected_Type (Etype (Comp))
5703 and then
5704 Present (Corresponding_Record_Type (Etype (Comp)))
5705 and then
5706 Has_Controlled_Component
5707 (Corresponding_Record_Type (Etype (Comp)))))
5708 then
5709 Set_Has_Controlled_Component (Rec);
5710 Final_Storage_Only :=
5711 Final_Storage_Only
5712 and then Finalize_Storage_Only (Etype (Comp));
5713 Relaxed_Finalization :=
5714 Relaxed_Finalization
5715 and then Has_Relaxed_Finalization (Etype (Comp));
5716 end if;
5718 if Has_Unchecked_Union (Etype (Comp)) then
5719 Set_Has_Unchecked_Union (Rec);
5720 end if;
5722 -- The record type requires its own invariant procedure in
5723 -- order to verify the invariant of each individual component.
5724 -- Do not consider internal components such as _parent because
5725 -- parent class-wide invariants are always inherited.
5726 -- In GNATprove mode, the component invariants are checked by
5727 -- other means. They should not be added to the record type
5728 -- invariant procedure, so that the procedure can be used to
5729 -- check the recordy type invariants if any.
5731 if Comes_From_Source (Comp)
5732 and then Has_Invariants (Etype (Comp))
5733 and then not GNATprove_Mode
5734 then
5735 Set_Has_Own_Invariants (Rec);
5736 end if;
5738 -- Scan component declaration for likely misuses of current
5739 -- instance, either in a constraint or a default expression.
5741 if Has_Per_Object_Constraint (Comp) then
5742 Check_Current_Instance (Parent (Comp));
5743 end if;
5745 Next_Component (Comp);
5746 end loop;
5748 -- For a type that is not directly controlled but has controlled
5749 -- components, Finalize_Storage_Only is set if all the controlled
5750 -- components are Finalize_Storage_Only. The same processing is
5751 -- appled to Has_Relaxed_Finalization.
5753 if not Is_Controlled (Rec) and then Has_Controlled_Component (Rec)
5754 then
5755 Set_Finalize_Storage_Only (Rec, Final_Storage_Only);
5756 Set_Has_Relaxed_Finalization (Rec, Relaxed_Finalization);
5757 end if;
5758 end if;
5760 -- Enforce the restriction that access attributes with a current
5761 -- instance prefix can only apply to limited types. This comment
5762 -- is floating here, but does not seem to belong here???
5764 -- Set component alignment if not otherwise already set
5766 Set_Component_Alignment_If_Not_Set (Rec);
5768 -- For first subtypes, check if there are any fixed-point fields with
5769 -- component clauses, where we must check the size. This is not done
5770 -- till the freeze point since for fixed-point types, we do not know
5771 -- the size until the type is frozen. Similar processing applies to
5772 -- bit-packed arrays.
5774 if Is_First_Subtype (Rec) then
5775 Comp := First_Component (Rec);
5776 while Present (Comp) loop
5777 if Present (Component_Clause (Comp))
5778 and then (Is_Fixed_Point_Type (Etype (Comp))
5779 or else Is_Bit_Packed_Array (Etype (Comp)))
5780 then
5781 Check_Size
5782 (Component_Name (Component_Clause (Comp)),
5783 Etype (Comp),
5784 Esize (Comp),
5785 Junk);
5786 end if;
5788 Next_Component (Comp);
5789 end loop;
5790 end if;
5792 -- See if Size is too small as is (and implicit packing might help)
5794 if not Is_Packed (Rec)
5796 -- No implicit packing if even one component is explicitly placed
5798 and then not Placed_Component
5800 -- Or even one component is aliased
5802 and then not Aliased_Component
5804 -- Must have size clause and all sized components
5806 and then Has_Size_Clause (Rec)
5807 and then All_Sized_Components
5809 -- Do not try implicit packing on records with discriminants, too
5810 -- complicated, especially in the variant record case.
5812 and then not Has_Discriminants (Rec)
5814 -- We want to implicitly pack if the specified size of the record
5815 -- is less than the sum of the object sizes (no point in packing
5816 -- if this is not the case), if we can compute it, i.e. if we have
5817 -- only elementary components. Otherwise, we have at least one
5818 -- composite component and we want to implicitly pack only if bit
5819 -- packing is required for it, as we are sure in this case that
5820 -- the back end cannot do the expected layout without packing.
5822 and then
5823 ((All_Elem_Components
5824 and then RM_Size (Rec) < Elem_Component_Total_Esize)
5825 or else
5826 (not All_Elem_Components
5827 and then not All_Storage_Unit_Components
5828 and then RM_Size (Rec) < Sized_Component_Total_Round_RM_Size))
5830 -- And the total RM size cannot be greater than the specified size
5831 -- since otherwise packing will not get us where we have to be.
5833 and then Sized_Component_Total_RM_Size <= RM_Size (Rec)
5835 -- Never do implicit packing in CodePeer or SPARK modes since
5836 -- we don't do any packing in these modes, since this generates
5837 -- over-complex code that confuses static analysis, and in
5838 -- general, neither CodePeer not GNATprove care about the
5839 -- internal representation of objects.
5841 and then not (CodePeer_Mode or GNATprove_Mode)
5842 then
5843 -- If implicit packing enabled, do it
5845 if Implicit_Packing then
5846 Set_Is_Packed (Rec);
5848 -- Otherwise flag the size clause
5850 else
5851 declare
5852 Sz : constant Node_Id := Size_Clause (Rec);
5853 begin
5854 Error_Msg_NE -- CODEFIX
5855 ("size given for& too small", Sz, Rec);
5856 Error_Msg_N -- CODEFIX
5857 ("\use explicit pragma Pack "
5858 & "or use pragma Implicit_Packing", Sz);
5859 end;
5860 end if;
5861 end if;
5863 -- Make sure that if we have an iterator aspect, then we have
5864 -- either Constant_Indexing or Variable_Indexing.
5866 declare
5867 Iterator_Aspect : Node_Id;
5869 begin
5870 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
5872 if No (Iterator_Aspect) then
5873 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
5874 end if;
5876 if Present (Iterator_Aspect) then
5877 if Has_Aspect (Rec, Aspect_Constant_Indexing)
5878 or else
5879 Has_Aspect (Rec, Aspect_Variable_Indexing)
5880 then
5881 null;
5882 else
5883 Error_Msg_N
5884 ("Iterator_Element requires indexing aspect",
5885 Iterator_Aspect);
5886 end if;
5887 end if;
5888 end;
5890 -- All done if not a full record definition
5892 if Ekind (Rec) /= E_Record_Type then
5893 return;
5894 end if;
5896 -- Finally we need to check the variant part to make sure that
5897 -- all types within choices are properly frozen as part of the
5898 -- freezing of the record type.
5900 Check_Variant_Part : declare
5901 D : constant Node_Id := Declaration_Node (Rec);
5902 T : Node_Id;
5903 C : Node_Id;
5905 begin
5906 -- Find component list
5908 C := Empty;
5910 if Nkind (D) = N_Full_Type_Declaration then
5911 T := Type_Definition (D);
5913 if Nkind (T) = N_Record_Definition then
5914 C := Component_List (T);
5916 elsif Nkind (T) = N_Derived_Type_Definition
5917 and then Present (Record_Extension_Part (T))
5918 then
5919 C := Component_List (Record_Extension_Part (T));
5920 end if;
5921 end if;
5923 -- Case of variant part present
5925 if Present (C) and then Present (Variant_Part (C)) then
5926 Freeze_Choices_In_Variant_Part (Variant_Part (C));
5927 end if;
5929 -- Note: we used to call Check_Choices here, but it is too early,
5930 -- since predicated subtypes are frozen here, but their freezing
5931 -- actions are in Analyze_Freeze_Entity, which has not been called
5932 -- yet for entities frozen within this procedure, so we moved that
5933 -- call to the Analyze_Freeze_Entity for the record type.
5935 end Check_Variant_Part;
5937 -- Check that all the primitives of an interface type are abstract
5938 -- or null procedures.
5940 if Is_Interface (Rec)
5941 and then not Error_Posted (Parent (Rec))
5942 then
5943 declare
5944 Elmt : Elmt_Id;
5945 Subp : Entity_Id;
5947 begin
5948 Elmt := First_Elmt (Primitive_Operations (Rec));
5949 while Present (Elmt) loop
5950 Subp := Node (Elmt);
5952 if not Is_Abstract_Subprogram (Subp)
5954 -- Avoid reporting the error on inherited primitives
5956 and then Comes_From_Source (Subp)
5957 then
5958 Error_Msg_Name_1 := Chars (Subp);
5960 if Ekind (Subp) = E_Procedure then
5961 if not Null_Present (Parent (Subp)) then
5962 Error_Msg_N
5963 ("interface procedure % must be abstract or null",
5964 Parent (Subp));
5965 end if;
5966 else
5967 Error_Msg_N
5968 ("interface function % must be abstract",
5969 Parent (Subp));
5970 end if;
5971 end if;
5973 Next_Elmt (Elmt);
5974 end loop;
5975 end;
5976 end if;
5977 end Freeze_Record_Type;
5979 -------------------------------
5980 -- Has_Boolean_Aspect_Import --
5981 -------------------------------
5983 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
5984 Decl : constant Node_Id := Declaration_Node (E);
5985 Asp : Node_Id;
5986 Expr : Node_Id;
5988 begin
5989 if Has_Aspects (Decl) then
5990 Asp := First (Aspect_Specifications (Decl));
5991 while Present (Asp) loop
5992 Expr := Expression (Asp);
5994 -- The value of aspect Import is True when the expression is
5995 -- either missing or it is explicitly set to True.
5997 if Get_Aspect_Id (Asp) = Aspect_Import
5998 and then (No (Expr)
5999 or else (Compile_Time_Known_Value (Expr)
6000 and then Is_True (Expr_Value (Expr))))
6001 then
6002 return True;
6003 end if;
6005 Next (Asp);
6006 end loop;
6007 end if;
6009 return False;
6010 end Has_Boolean_Aspect_Import;
6012 -------------------------
6013 -- Inherit_Freeze_Node --
6014 -------------------------
6016 procedure Inherit_Freeze_Node
6017 (Fnod : Node_Id;
6018 Typ : Entity_Id)
6020 Typ_Fnod : constant Node_Id := Freeze_Node (Typ);
6022 begin
6023 Set_Freeze_Node (Typ, Fnod);
6024 Set_Entity (Fnod, Typ);
6026 -- The input type had an existing node. Propagate relevant attributes
6027 -- from the old freeze node to the inherited freeze node.
6029 -- ??? if both freeze nodes have attributes, would they differ?
6031 if Present (Typ_Fnod) then
6033 -- Attribute Access_Types_To_Process
6035 if Present (Access_Types_To_Process (Typ_Fnod))
6036 and then No (Access_Types_To_Process (Fnod))
6037 then
6038 Set_Access_Types_To_Process (Fnod,
6039 Access_Types_To_Process (Typ_Fnod));
6040 end if;
6042 -- Attribute Actions
6044 if Present (Actions (Typ_Fnod)) and then No (Actions (Fnod)) then
6045 Set_Actions (Fnod, Actions (Typ_Fnod));
6046 end if;
6048 -- Attribute First_Subtype_Link
6050 if Present (First_Subtype_Link (Typ_Fnod))
6051 and then No (First_Subtype_Link (Fnod))
6052 then
6053 Set_First_Subtype_Link (Fnod, First_Subtype_Link (Typ_Fnod));
6054 end if;
6056 -- Attribute TSS_Elist
6058 if Present (TSS_Elist (Typ_Fnod))
6059 and then No (TSS_Elist (Fnod))
6060 then
6061 Set_TSS_Elist (Fnod, TSS_Elist (Typ_Fnod));
6062 end if;
6063 end if;
6064 end Inherit_Freeze_Node;
6066 ------------------------------
6067 -- Wrap_Imported_Subprogram --
6068 ------------------------------
6070 -- The issue here is that our normal approach of checking preconditions
6071 -- and postconditions does not work for imported procedures, since we
6072 -- are not generating code for the body. To get around this we create
6073 -- a wrapper, as shown by the following example:
6075 -- procedure K (A : Integer);
6076 -- pragma Import (C, K);
6078 -- The spec is rewritten by removing the effects of pragma Import, but
6079 -- leaving the convention unchanged, as though the source had said:
6081 -- procedure K (A : Integer);
6082 -- pragma Convention (C, K);
6084 -- and we create a body, added to the entity K freeze actions, which
6085 -- looks like:
6087 -- procedure K (A : Integer) is
6088 -- procedure K (A : Integer);
6089 -- pragma Import (C, K);
6090 -- begin
6091 -- K (A);
6092 -- end K;
6094 -- Now the contract applies in the normal way to the outer procedure,
6095 -- and the inner procedure has no contracts, so there is no problem
6096 -- in just calling it to get the original effect.
6098 -- In the case of a function, we create an appropriate return statement
6099 -- for the subprogram body that calls the inner procedure.
6101 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
6102 function Copy_Import_Pragma return Node_Id;
6103 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
6105 ------------------------
6106 -- Copy_Import_Pragma --
6107 ------------------------
6109 function Copy_Import_Pragma return Node_Id is
6111 -- The subprogram should have an import pragma, otherwise it does
6112 -- need a wrapper.
6114 Prag : constant Node_Id := Import_Pragma (E);
6115 pragma Assert (Present (Prag));
6117 -- Save all semantic fields of the pragma
6119 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
6120 Save_From : constant Boolean := From_Aspect_Specification (Prag);
6121 Save_Prag : constant Node_Id := Next_Pragma (Prag);
6122 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
6124 Result : Node_Id;
6126 begin
6127 -- Reset all semantic fields. This avoids a potential infinite
6128 -- loop when the pragma comes from an aspect as the duplication
6129 -- will copy the aspect, then copy the corresponding pragma and
6130 -- so on.
6132 Set_Corresponding_Aspect (Prag, Empty);
6133 Set_From_Aspect_Specification (Prag, False);
6134 Set_Next_Pragma (Prag, Empty);
6135 Set_Next_Rep_Item (Prag, Empty);
6137 Result := Copy_Separate_Tree (Prag);
6139 -- Restore the original semantic fields
6141 Set_Corresponding_Aspect (Prag, Save_Asp);
6142 Set_From_Aspect_Specification (Prag, Save_From);
6143 Set_Next_Pragma (Prag, Save_Prag);
6144 Set_Next_Rep_Item (Prag, Save_Rep);
6146 return Result;
6147 end Copy_Import_Pragma;
6149 -- Local variables
6151 Loc : constant Source_Ptr := Sloc (E);
6152 CE : constant Name_Id := Chars (E);
6153 Bod : Node_Id;
6154 Forml : Entity_Id;
6155 Parms : List_Id;
6156 Prag : Node_Id;
6157 Spec : Node_Id;
6158 Stmt : Node_Id;
6160 -- Start of processing for Wrap_Imported_Subprogram
6162 begin
6163 -- Nothing to do if not imported
6165 if not Is_Imported (E) then
6166 return;
6168 -- Test enabling conditions for wrapping
6170 elsif Is_Subprogram (E)
6171 and then Present (Contract (E))
6172 and then Present (Pre_Post_Conditions (Contract (E)))
6173 and then not GNATprove_Mode
6174 then
6175 -- Here we do the wrap
6177 Prag := Copy_Import_Pragma;
6179 -- Fix up spec so it is no longer imported and has convention Ada
6181 Set_Has_Completion (E, False);
6182 Set_Import_Pragma (E, Empty);
6183 Set_Interface_Name (E, Empty);
6184 Set_Is_Imported (E, False);
6185 Set_Convention (E, Convention_Ada);
6187 -- Grab the subprogram declaration and specification
6189 Spec := Declaration_Node (E);
6191 -- Build parameter list that we need
6193 Parms := New_List;
6194 Forml := First_Formal (E);
6195 while Present (Forml) loop
6196 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
6197 Next_Formal (Forml);
6198 end loop;
6200 -- Build the call
6202 -- An imported function whose result type is anonymous access
6203 -- creates a new anonymous access type when it is relocated into
6204 -- the declarations of the body generated below. As a result, the
6205 -- accessibility level of these two anonymous access types may not
6206 -- be compatible even though they are essentially the same type.
6207 -- Use an unchecked type conversion to reconcile this case. Note
6208 -- that the conversion is safe because in the named access type
6209 -- case, both the body and imported function utilize the same
6210 -- type.
6212 if Ekind (E) in E_Function | E_Generic_Function then
6213 Stmt :=
6214 Make_Simple_Return_Statement (Loc,
6215 Expression =>
6216 Unchecked_Convert_To (Etype (E),
6217 Make_Function_Call (Loc,
6218 Name => Make_Identifier (Loc, CE),
6219 Parameter_Associations => Parms)));
6221 else
6222 Stmt :=
6223 Make_Procedure_Call_Statement (Loc,
6224 Name => Make_Identifier (Loc, CE),
6225 Parameter_Associations => Parms);
6226 end if;
6228 -- Now build the body
6230 Bod :=
6231 Make_Subprogram_Body (Loc,
6232 Specification => Copy_Subprogram_Spec (Spec),
6233 Declarations => New_List (
6234 Make_Subprogram_Declaration (Loc,
6235 Specification => Copy_Subprogram_Spec (Spec)),
6236 Prag),
6237 Handled_Statement_Sequence =>
6238 Make_Handled_Sequence_Of_Statements (Loc,
6239 Statements => New_List (Stmt),
6240 End_Label => Make_Identifier (Loc, CE)));
6242 -- Append the body to freeze result
6244 Add_To_Result (Bod);
6245 return;
6247 -- Case of imported subprogram that does not get wrapped
6249 else
6250 -- Set Is_Public. All imported entities need an external symbol
6251 -- created for them since they are always referenced from another
6252 -- object file. Note this used to be set when we set Is_Imported
6253 -- back in Sem_Prag, but now we delay it to this point, since we
6254 -- don't want to set this flag if we wrap an imported subprogram.
6256 Set_Is_Public (E);
6257 end if;
6258 end Wrap_Imported_Subprogram;
6260 -- Start of processing for Freeze_Entity
6262 begin
6263 -- The entity being frozen may be subject to pragma Ghost. Set the mode
6264 -- now to ensure that any nodes generated during freezing are properly
6265 -- flagged as Ghost.
6267 Set_Ghost_Mode (E);
6269 -- We are going to test for various reasons why this entity need not be
6270 -- frozen here, but in the case of an Itype that's defined within a
6271 -- record, that test actually applies to the record.
6273 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
6274 Test_E := Scope (E);
6276 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
6277 and then Is_Record_Type (Underlying_Type (Scope (E)))
6278 then
6279 Test_E := Underlying_Type (Scope (E));
6280 end if;
6282 -- Do not freeze if already frozen since we only need one freeze node
6284 if Is_Frozen (E) then
6286 if Is_Itype (E)
6287 and then not Is_Base_Type (E)
6288 and then not Is_Frozen (Etype (E))
6289 then
6290 -- If a frozen subtype of an unfrozen type seems impossible
6291 -- then see Analyze_Protected_Definition.Undelay_Itypes.
6293 Result := Freeze_Entity
6294 (Etype (E), N, Do_Freeze_Profile => Do_Freeze_Profile);
6295 else
6296 Result := No_List;
6297 end if;
6299 goto Leave;
6301 -- Do not freeze if we are preanalyzing without freezing
6303 elsif Inside_Preanalysis_Without_Freezing > 0 then
6304 Result := No_List;
6305 goto Leave;
6307 elsif Ekind (E) = E_Generic_Package then
6308 Result := Freeze_Generic_Entities (E);
6309 goto Leave;
6311 -- It is improper to freeze an external entity within a generic because
6312 -- its freeze node will appear in a non-valid context. The entity will
6313 -- be frozen in the proper scope after the current generic is analyzed.
6314 -- However, aspects must be analyzed because they may be queried later
6315 -- within the generic itself, and the corresponding pragma or attribute
6316 -- definition has not been analyzed yet. After this, indicate that the
6317 -- entity has no further delayed aspects, to prevent a later aspect
6318 -- analysis out of the scope of the generic.
6320 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
6321 if Has_Delayed_Aspects (E) then
6322 Analyze_Aspects_At_Freeze_Point (E);
6323 Set_Has_Delayed_Aspects (E, False);
6324 end if;
6326 Result := No_List;
6327 goto Leave;
6329 -- AI05-0213: A formal incomplete type does not freeze the actual. In
6330 -- the instance, the same applies to the subtype renaming the actual.
6332 elsif Is_Private_Type (E)
6333 and then Is_Generic_Actual_Type (E)
6334 and then No (Full_View (Base_Type (E)))
6335 and then Ada_Version >= Ada_2012
6336 then
6337 Result := No_List;
6338 goto Leave;
6340 -- Formal subprograms are never frozen
6342 elsif Is_Formal_Subprogram (E) then
6343 Result := No_List;
6344 goto Leave;
6346 -- Generic types are never frozen as they lack delayed semantic checks
6348 elsif Is_Generic_Type (E) then
6349 Result := No_List;
6350 goto Leave;
6352 -- Do not freeze a global entity within an inner scope created during
6353 -- expansion. A call to subprogram E within some internal procedure
6354 -- (a stream attribute for example) might require freezing E, but the
6355 -- freeze node must appear in the same declarative part as E itself.
6356 -- The two-pass elaboration mechanism in gigi guarantees that E will
6357 -- be frozen before the inner call is elaborated. We exclude constants
6358 -- from this test, because deferred constants may be frozen early, and
6359 -- must be diagnosed (e.g. in the case of a deferred constant being used
6360 -- in a default expression). If the enclosing subprogram comes from
6361 -- source, or is a generic instance, then the freeze point is the one
6362 -- mandated by the language, and we freeze the entity. A subprogram that
6363 -- is a child unit body that acts as a spec does not have a spec that
6364 -- comes from source, but can only come from source.
6366 elsif In_Open_Scopes (Scope (Test_E))
6367 and then Scope (Test_E) /= Current_Scope
6368 and then Ekind (Test_E) /= E_Constant
6369 then
6370 -- Here we deal with the special case of the expansion of
6371 -- postconditions. Previously this was handled by the loop below,
6372 -- since these postcondition checks got isolated to a separate,
6373 -- internally generated, subprogram. Now, however, the postcondition
6374 -- checks get contained within their corresponding subprogram
6375 -- directly.
6377 if not Comes_From_Source (N)
6378 and then Nkind (N) = N_Pragma
6379 and then From_Aspect_Specification (N)
6380 and then Is_Valid_Assertion_Kind (Original_Aspect_Pragma_Name (N))
6382 -- Now, verify the placement of the pragma is within an expanded
6383 -- subprogram which contains postcondition expansion - detected
6384 -- through the presence of the "Wrapped_Statements" field.
6386 and then Present (Enclosing_Subprogram (Current_Scope))
6387 and then Present (Wrapped_Statements
6388 (Enclosing_Subprogram (Current_Scope)))
6389 then
6390 goto Leave;
6391 end if;
6393 -- Otherwise, loop through scopes checking if an enclosing scope
6394 -- comes from source or is a generic. Note that, for the purpose
6395 -- of this test, we need to consider that the internally generated
6396 -- subprogram described above comes from source too if the original
6397 -- subprogram itself does.
6399 declare
6400 S : Entity_Id;
6402 begin
6403 S := Current_Scope;
6404 while Present (S) loop
6405 if Is_Overloadable (S) then
6406 if Comes_From_Source (S)
6407 or else (Chars (S) = Name_uWrapped_Statements
6408 and then Comes_From_Source (Scope (S)))
6409 or else Is_Generic_Instance (S)
6410 or else Is_Child_Unit (S)
6411 then
6412 exit;
6413 else
6414 Result := No_List;
6415 goto Leave;
6416 end if;
6417 end if;
6419 S := Scope (S);
6420 end loop;
6421 end;
6423 -- Similarly, an inlined instance body may make reference to global
6424 -- entities, but these references cannot be the proper freezing point
6425 -- for them, and in the absence of inlining freezing will take place in
6426 -- their own scope. Normally instance bodies are analyzed after the
6427 -- enclosing compilation, and everything has been frozen at the proper
6428 -- place, but with front-end inlining an instance body is compiled
6429 -- before the end of the enclosing scope, and as a result out-of-order
6430 -- freezing must be prevented.
6432 elsif Front_End_Inlining
6433 and then In_Instance_Body
6434 and then Present (Scope (Test_E))
6435 then
6436 declare
6437 S : Entity_Id;
6439 begin
6440 S := Scope (Test_E);
6441 while Present (S) loop
6442 if Is_Generic_Instance (S) then
6443 exit;
6444 else
6445 S := Scope (S);
6446 end if;
6447 end loop;
6449 if No (S) then
6450 Result := No_List;
6451 goto Leave;
6452 end if;
6453 end;
6454 end if;
6456 -- Add checks to detect proper initialization of scalars that may appear
6457 -- as subprogram parameters.
6459 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
6460 Apply_Parameter_Validity_Checks (E);
6461 end if;
6463 -- Deal with delayed aspect specifications. The analysis of the aspect
6464 -- is required to be delayed to the freeze point, thus we analyze the
6465 -- pragma or attribute definition clause in the tree at this point. We
6466 -- also analyze the aspect specification node at the freeze point when
6467 -- the aspect doesn't correspond to pragma/attribute definition clause.
6468 -- In addition, a derived type may have inherited aspects that were
6469 -- delayed in the parent, so these must also be captured now.
6471 -- For a record type, we deal with the delayed aspect specifications on
6472 -- components first, which is consistent with the non-delayed case and
6473 -- makes it possible to have a single processing to detect conflicts.
6475 if Is_Record_Type (E) then
6476 declare
6477 Comp : Entity_Id;
6479 Rec_Pushed : Boolean := False;
6480 -- Set True if the record type E has been pushed on the scope
6481 -- stack. Needed for the analysis of delayed aspects specified
6482 -- to the components of Rec.
6484 begin
6485 Comp := First_Component (E);
6486 while Present (Comp) loop
6487 if Has_Delayed_Aspects (Comp) then
6488 if not Rec_Pushed then
6489 Push_Scope (E);
6490 Rec_Pushed := True;
6492 -- The visibility to the discriminants must be restored
6493 -- in order to properly analyze the aspects.
6495 if Has_Discriminants (E) then
6496 Install_Discriminants (E);
6497 end if;
6498 end if;
6500 Analyze_Aspects_At_Freeze_Point (Comp);
6501 end if;
6503 Next_Component (Comp);
6504 end loop;
6506 -- Pop the scope if Rec scope has been pushed on the scope stack
6507 -- during the delayed aspect analysis process.
6509 if Rec_Pushed then
6510 if Has_Discriminants (E) then
6511 Uninstall_Discriminants (E);
6512 end if;
6514 Pop_Scope;
6515 end if;
6516 end;
6517 end if;
6519 if Has_Delayed_Aspects (E) then
6520 Analyze_Aspects_At_Freeze_Point (E);
6521 end if;
6523 -- Here to freeze the entity
6525 Set_Is_Frozen (E);
6527 -- Case of entity being frozen is other than a type
6529 if not Is_Type (E) then
6531 -- If entity is exported or imported and does not have an external
6532 -- name, now is the time to provide the appropriate default name.
6533 -- Skip this if the entity is stubbed, since we don't need a name
6534 -- for any stubbed routine. For the case on intrinsics, if no
6535 -- external name is specified, then calls will be handled in
6536 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
6537 -- external name is provided, then Expand_Intrinsic_Call leaves
6538 -- calls in place for expansion by GIGI.
6540 if (Is_Imported (E) or else Is_Exported (E))
6541 and then No (Interface_Name (E))
6542 and then Convention (E) /= Convention_Stubbed
6543 and then Convention (E) /= Convention_Intrinsic
6544 then
6545 Set_Encoded_Interface_Name
6546 (E, Get_Default_External_Name (E));
6547 end if;
6549 -- Subprogram case
6551 if Is_Subprogram (E) then
6553 -- Check for needing to wrap imported subprogram
6555 if not Inside_A_Generic then
6556 Wrap_Imported_Subprogram (E);
6557 end if;
6559 -- Freeze all parameter types and the return type (RM 13.14(14)).
6560 -- However skip this for internal subprograms. This is also where
6561 -- any extra formal parameters are created since we now know
6562 -- whether the subprogram will use a foreign convention.
6564 -- In Ada 2012, freezing a subprogram does not always freeze the
6565 -- corresponding profile (see AI05-019). An attribute reference
6566 -- is not a freezing point of the profile. Similarly, we do not
6567 -- freeze the profile of primitives of a library-level tagged type
6568 -- when we are building its dispatch table. Flag Do_Freeze_Profile
6569 -- indicates whether the profile should be frozen now.
6571 -- This processing doesn't apply to internal entities (see below)
6573 if not Is_Internal (E) and then Do_Freeze_Profile then
6574 if not Freeze_Profile (E) then
6575 goto Leave;
6576 end if;
6577 end if;
6579 -- Must freeze its parent first if it is a derived subprogram
6581 if Present (Alias (E)) then
6582 Freeze_And_Append (Alias (E), N, Result);
6583 end if;
6585 -- We don't freeze internal subprograms, because we don't normally
6586 -- want addition of extra formals or mechanism setting to happen
6587 -- for those. However we do pass through predefined dispatching
6588 -- cases, since extra formals may be needed in some cases, such as
6589 -- for the stream 'Input function (build-in-place formals).
6591 if not Is_Internal (E)
6592 or else Is_Predefined_Dispatching_Operation (E)
6593 then
6594 Freeze_Subprogram (E);
6595 end if;
6597 -- If warning on suspicious contracts then check for the case of
6598 -- a postcondition other than False for a No_Return subprogram.
6600 if No_Return (E)
6601 and then Warn_On_Suspicious_Contract
6602 and then Present (Contract (E))
6603 then
6604 declare
6605 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
6606 Exp : Node_Id;
6608 begin
6609 while Present (Prag) loop
6610 if Pragma_Name_Unmapped (Prag) in Name_Post
6611 | Name_Postcondition
6612 | Name_Refined_Post
6613 then
6614 Exp :=
6615 Expression
6616 (First (Pragma_Argument_Associations (Prag)));
6618 if Nkind (Exp) /= N_Identifier
6619 or else Chars (Exp) /= Name_False
6620 then
6621 Error_Msg_NE
6622 ("useless postcondition, & is marked "
6623 & "No_Return?.t?", Exp, E);
6624 end if;
6625 end if;
6627 Prag := Next_Pragma (Prag);
6628 end loop;
6629 end;
6630 end if;
6632 -- Here for other than a subprogram or type
6634 else
6635 -- If entity has a type declared in the current scope, and it is
6636 -- not a generic unit, then freeze it first.
6638 if Present (Etype (E))
6639 and then Ekind (E) /= E_Generic_Function
6640 and then Within_Scope (Etype (E), Current_Scope)
6641 then
6642 Freeze_And_Append (Etype (E), N, Result);
6644 -- For an object of an anonymous array type, aspects on the
6645 -- object declaration apply to the type itself. This is the
6646 -- case for Atomic_Components, Volatile_Components, and
6647 -- Independent_Components. In these cases analysis of the
6648 -- generated pragma will mark the anonymous types accordingly,
6649 -- and the object itself does not require a freeze node.
6651 if Ekind (E) = E_Variable
6652 and then Is_Itype (Etype (E))
6653 and then Is_Array_Type (Etype (E))
6654 and then Has_Delayed_Aspects (E)
6655 then
6656 Set_Has_Delayed_Aspects (E, False);
6657 Set_Has_Delayed_Freeze (E, False);
6658 Set_Freeze_Node (E, Empty);
6659 end if;
6660 end if;
6662 -- Special processing for objects created by object declaration;
6663 -- we protect the call to Declaration_Node against entities of
6664 -- expressions replaced by the frontend with an N_Raise_CE node.
6666 if Ekind (E) in E_Constant | E_Variable
6667 and then Nkind (Declaration_Node (E)) = N_Object_Declaration
6668 then
6669 Freeze_Object_Declaration (E);
6670 end if;
6672 -- Check that a constant which has a pragma Volatile[_Components]
6673 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
6675 -- Note: Atomic[_Components] also sets Volatile[_Components]
6677 if Ekind (E) = E_Constant
6678 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
6679 and then not Is_Imported (E)
6680 and then not Has_Boolean_Aspect_Import (E)
6681 then
6682 -- Make sure we actually have a pragma, and have not merely
6683 -- inherited the indication from elsewhere (e.g. an address
6684 -- clause, which is not good enough in RM terms).
6686 if Has_Rep_Pragma (E, Name_Atomic)
6687 or else
6688 Has_Rep_Pragma (E, Name_Atomic_Components)
6689 then
6690 Error_Msg_N
6691 ("standalone atomic constant must be " &
6692 "imported (RM C.6(13))", E);
6694 elsif Has_Rep_Pragma (E, Name_Volatile)
6695 or else
6696 Has_Rep_Pragma (E, Name_Volatile_Components)
6697 then
6698 Error_Msg_N
6699 ("standalone volatile constant must be " &
6700 "imported (RM C.6(13))", E);
6701 end if;
6702 end if;
6704 -- Static objects require special handling
6706 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
6707 and then Is_Statically_Allocated (E)
6708 then
6709 Freeze_Static_Object (E);
6710 end if;
6712 -- Remaining step is to layout objects
6714 if Ekind (E) in E_Variable | E_Constant | E_Loop_Parameter
6715 or else Is_Formal (E)
6716 then
6717 Layout_Object (E);
6718 end if;
6720 -- For an object that does not have delayed freezing, and whose
6721 -- initialization actions have been captured in a compound
6722 -- statement, move them back now directly within the enclosing
6723 -- statement sequence.
6725 if Ekind (E) in E_Constant | E_Variable
6726 and then not Has_Delayed_Freeze (E)
6727 then
6728 Explode_Initialization_Compound_Statement (E);
6729 end if;
6731 -- Do not generate a freeze node for a generic unit
6733 if Is_Generic_Unit (E) then
6734 Result := No_List;
6735 goto Leave;
6736 end if;
6737 end if;
6739 -- Case of a type or subtype being frozen
6741 else
6742 -- Verify several SPARK legality rules related to Ghost types now
6743 -- that the type is frozen.
6745 Check_Ghost_Type (E);
6747 -- We used to check here that a full type must have preelaborable
6748 -- initialization if it completes a private type specified with
6749 -- pragma Preelaborable_Initialization, but that missed cases where
6750 -- the types occur within a generic package, since the freezing
6751 -- that occurs within a containing scope generally skips traversal
6752 -- of a generic unit's declarations (those will be frozen within
6753 -- instances). This check was moved to Analyze_Package_Specification.
6755 -- The type may be defined in a generic unit. This can occur when
6756 -- freezing a generic function that returns the type (which is
6757 -- defined in a parent unit). It is clearly meaningless to freeze
6758 -- this type. However, if it is a subtype, its size may be determi-
6759 -- nable and used in subsequent checks, so might as well try to
6760 -- compute it.
6762 -- In Ada 2012, Freeze_Entities is also used in the front end to
6763 -- trigger the analysis of aspect expressions, so in this case we
6764 -- want to continue the freezing process.
6766 -- Is_Generic_Unit (Scope (E)) is dubious here, do we want instead
6767 -- In_Generic_Scope (E)???
6769 if Present (Scope (E))
6770 and then Is_Generic_Unit (Scope (E))
6771 and then
6772 (not Has_Predicates (E)
6773 and then not Has_Delayed_Freeze (E))
6774 then
6775 Check_Compile_Time_Size (E);
6776 Result := No_List;
6777 goto Leave;
6778 end if;
6780 -- Check for error of Type_Invariant'Class applied to an untagged
6781 -- type (check delayed to freeze time when full type is available).
6783 declare
6784 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
6785 begin
6786 if Present (Prag)
6787 and then Class_Present (Prag)
6788 and then not Is_Tagged_Type (E)
6789 then
6790 Error_Msg_NE
6791 ("Type_Invariant''Class cannot be specified for &", Prag, E);
6792 Error_Msg_N
6793 ("\can only be specified for a tagged type", Prag);
6794 end if;
6795 end;
6797 -- Deal with special cases of freezing for subtype
6799 if E /= Base_Type (E) then
6801 -- Before we do anything else, a specific test for the case of a
6802 -- size given for an array where the array would need to be packed
6803 -- in order for the size to be honored, but is not. This is the
6804 -- case where implicit packing may apply. The reason we do this so
6805 -- early is that, if we have implicit packing, the layout of the
6806 -- base type is affected, so we must do this before we freeze the
6807 -- base type.
6809 -- We could do this processing only if implicit packing is enabled
6810 -- since in all other cases, the error would be caught by the back
6811 -- end. However, we choose to do the check even if we do not have
6812 -- implicit packing enabled, since this allows us to give a more
6813 -- useful error message (advising use of pragma Implicit_Packing
6814 -- or pragma Pack).
6816 if Is_Array_Type (E) then
6817 declare
6818 Ctyp : constant Entity_Id := Component_Type (E);
6819 Rsiz : constant Uint :=
6820 (if Known_RM_Size (Ctyp) then RM_Size (Ctyp) else Uint_0);
6821 SZ : constant Node_Id := Size_Clause (E);
6822 Btyp : constant Entity_Id := Base_Type (E);
6824 Lo : Node_Id;
6825 Hi : Node_Id;
6826 Indx : Node_Id;
6828 Dim : Uint;
6829 Num_Elmts : Uint := Uint_1;
6830 -- Number of elements in array
6832 begin
6833 -- Check enabling conditions. These are straightforward
6834 -- except for the test for a limited composite type. This
6835 -- eliminates the rare case of a array of limited components
6836 -- where there are issues of whether or not we can go ahead
6837 -- and pack the array (since we can't freely pack and unpack
6838 -- arrays if they are limited).
6840 -- Note that we check the root type explicitly because the
6841 -- whole point is we are doing this test before we have had
6842 -- a chance to freeze the base type (and it is that freeze
6843 -- action that causes stuff to be inherited).
6845 -- The conditions on the size are identical to those used in
6846 -- Freeze_Array_Type to set the Is_Packed flag.
6848 if Has_Size_Clause (E)
6849 and then Known_Static_RM_Size (E)
6850 and then not Is_Packed (E)
6851 and then not Has_Pragma_Pack (E)
6852 and then not Has_Component_Size_Clause (E)
6853 and then Known_Static_RM_Size (Ctyp)
6854 and then Rsiz <= System_Max_Integer_Size
6855 and then not (Addressable (Rsiz)
6856 and then Known_Static_Esize (Ctyp)
6857 and then Esize (Ctyp) = Rsiz)
6858 and then not (Rsiz mod System_Storage_Unit = 0
6859 and then Is_Composite_Type (Ctyp))
6860 and then not Is_Limited_Composite (E)
6861 and then not Is_Packed (Root_Type (E))
6862 and then not Has_Component_Size_Clause (Root_Type (E))
6863 and then not (CodePeer_Mode or GNATprove_Mode)
6864 then
6865 -- Compute number of elements in array
6867 Indx := First_Index (E);
6868 while Present (Indx) loop
6869 Get_Index_Bounds (Indx, Lo, Hi);
6871 if not (Compile_Time_Known_Value (Lo)
6872 and then
6873 Compile_Time_Known_Value (Hi))
6874 then
6875 goto No_Implicit_Packing;
6876 end if;
6878 Dim := Expr_Value (Hi) - Expr_Value (Lo) + 1;
6880 if Dim > Uint_0 then
6881 Num_Elmts := Num_Elmts * Dim;
6882 else
6883 Num_Elmts := Uint_0;
6884 end if;
6886 Next_Index (Indx);
6887 end loop;
6889 -- What we are looking for here is the situation where
6890 -- the RM_Size given would be exactly right if there was
6891 -- a pragma Pack, resulting in the component size being
6892 -- the RM_Size of the component type.
6894 if RM_Size (E) = Num_Elmts * Rsiz then
6896 -- For implicit packing mode, just set the component
6897 -- size and Freeze_Array_Type will do the rest.
6899 if Implicit_Packing then
6900 Set_Component_Size (Btyp, Rsiz);
6902 -- Otherwise give an error message, except that if the
6903 -- specified Size is zero, there is no need for pragma
6904 -- Pack. Note that size zero is not considered
6905 -- Addressable.
6907 elsif RM_Size (E) /= Uint_0 then
6908 Error_Msg_NE
6909 ("size given for& too small", SZ, E);
6910 Error_Msg_N -- CODEFIX
6911 ("\use explicit pragma Pack or use pragma "
6912 & "Implicit_Packing", SZ);
6913 end if;
6914 end if;
6915 end if;
6916 end;
6917 end if;
6919 <<No_Implicit_Packing>>
6921 -- If ancestor subtype present, freeze that first. Note that this
6922 -- will also get the base type frozen. Need RM reference ???
6924 Atype := Ancestor_Subtype (E);
6926 if Present (Atype) then
6927 Freeze_And_Append (Atype, N, Result);
6929 -- No ancestor subtype present
6931 else
6932 -- See if we have a nearest ancestor that has a predicate.
6933 -- That catches the case of derived type with a predicate.
6934 -- Need RM reference here ???
6936 Atype := Nearest_Ancestor (E);
6938 if Present (Atype) and then Has_Predicates (Atype) then
6939 Freeze_And_Append (Atype, N, Result);
6940 end if;
6942 -- Freeze base type before freezing the entity (RM 13.14(15))
6944 if E /= Base_Type (E) then
6945 Freeze_And_Append (Base_Type (E), N, Result);
6946 end if;
6947 end if;
6949 -- A subtype inherits all the type-related representation aspects
6950 -- from its parents (RM 13.1(8)).
6952 if May_Inherit_Delayed_Rep_Aspects (E) then
6953 Inherit_Delayed_Rep_Aspects (E);
6954 end if;
6956 Inherit_Aspects_At_Freeze_Point (E);
6958 -- For a derived type, freeze its parent type first (RM 13.14(15))
6960 elsif Is_Derived_Type (E) then
6961 Freeze_And_Append (Etype (E), N, Result);
6963 -- A derived type inherits each type-related representation aspect
6964 -- of its parent type that was directly specified before the
6965 -- declaration of the derived type (RM 13.1(15)).
6967 if May_Inherit_Delayed_Rep_Aspects (E) then
6968 Inherit_Delayed_Rep_Aspects (E);
6969 end if;
6971 Inherit_Aspects_At_Freeze_Point (E);
6972 end if;
6974 -- Case of array type
6976 if Is_Array_Type (E) then
6977 Freeze_Array_Type (E);
6978 end if;
6980 -- Check for incompatible size and alignment for array/record type
6982 if Warn_On_Size_Alignment
6983 and then (Is_Array_Type (E) or else Is_Record_Type (E))
6984 and then Has_Size_Clause (E)
6985 and then Has_Alignment_Clause (E)
6987 -- If explicit Object_Size clause given assume that the programmer
6988 -- knows what he is doing, and expects the compiler behavior.
6990 and then not Has_Object_Size_Clause (E)
6992 -- It does not really make sense to warn for the minimum alignment
6993 -- since the programmer could not get rid of the warning.
6995 and then Alignment (E) > 1
6997 -- Check for size not a multiple of alignment
6999 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
7000 then
7001 declare
7002 SC : constant Node_Id := Size_Clause (E);
7003 AC : constant Node_Id := Alignment_Clause (E);
7004 Loc : Node_Id;
7005 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
7007 begin
7008 if Present (SC) and then Present (AC) then
7010 -- Give a warning
7012 if Sloc (SC) > Sloc (AC) then
7013 Loc := SC;
7014 Error_Msg_NE
7015 ("?.z?size is not a multiple of alignment for &",
7016 Loc, E);
7017 Error_Msg_Sloc := Sloc (AC);
7018 Error_Msg_Uint_1 := Alignment (E);
7019 Error_Msg_N ("\?.z?alignment of ^ specified #", Loc);
7021 else
7022 Loc := AC;
7023 Error_Msg_NE
7024 ("?.z?size is not a multiple of alignment for &",
7025 Loc, E);
7026 Error_Msg_Sloc := Sloc (SC);
7027 Error_Msg_Uint_1 := RM_Size (E);
7028 Error_Msg_N ("\?.z?size of ^ specified #", Loc);
7029 end if;
7031 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
7032 Error_Msg_N ("\?.z?Object_Size will be increased to ^", Loc);
7033 end if;
7034 end;
7035 end if;
7037 -- For a class-wide type, the corresponding specific type is
7038 -- frozen as well (RM 13.14(15))
7040 if Is_Class_Wide_Type (E) then
7041 Freeze_And_Append (Root_Type (E), N, Result);
7043 -- If the base type of the class-wide type is still incomplete,
7044 -- the class-wide remains unfrozen as well. This is legal when
7045 -- E is the formal of a primitive operation of some other type
7046 -- which is being frozen.
7048 if not Is_Frozen (Root_Type (E)) then
7049 Set_Is_Frozen (E, False);
7050 goto Leave;
7051 end if;
7053 -- The equivalent type associated with a class-wide subtype needs
7054 -- to be frozen to ensure that its layout is done.
7056 if Ekind (E) = E_Class_Wide_Subtype
7057 and then Present (Equivalent_Type (E))
7058 then
7059 Freeze_And_Append (Equivalent_Type (E), N, Result);
7060 end if;
7062 -- Generate an itype reference for a library-level class-wide type
7063 -- at the freeze point. Otherwise the first explicit reference to
7064 -- the type may appear in an inner scope which will be rejected by
7065 -- the back-end.
7067 if Is_Itype (E)
7068 and then Is_Compilation_Unit (Scope (E))
7069 then
7070 declare
7071 Ref : constant Node_Id := Make_Itype_Reference (Loc);
7073 begin
7074 Set_Itype (Ref, E);
7076 -- From a gigi point of view, a class-wide subtype derives
7077 -- from its record equivalent type. As a result, the itype
7078 -- reference must appear after the freeze node of the
7079 -- equivalent type or gigi will reject the reference.
7081 if Ekind (E) = E_Class_Wide_Subtype
7082 and then Present (Equivalent_Type (E))
7083 then
7084 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
7085 else
7086 Add_To_Result (Ref);
7087 end if;
7088 end;
7089 end if;
7091 -- For a record type or record subtype, freeze all component types
7092 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
7093 -- using Is_Record_Type, because we don't want to attempt the freeze
7094 -- for the case of a private type with record extension (we will do
7095 -- that later when the full type is frozen).
7097 elsif Ekind (E) in E_Record_Type | E_Record_Subtype then
7098 if not In_Generic_Scope (E) then
7099 Freeze_Record_Type (E);
7100 end if;
7102 -- Report a warning if a discriminated record base type has a
7103 -- convention with language C or C++ applied to it. This check is
7104 -- done even within generic scopes (but not in instantiations),
7105 -- which is why we don't do it as part of Freeze_Record_Type.
7107 Check_Suspicious_Convention (E);
7109 -- For a concurrent type, freeze corresponding record type. This does
7110 -- not correspond to any specific rule in the RM, but the record type
7111 -- is essentially part of the concurrent type. Also freeze all local
7112 -- entities. This includes record types created for entry parameter
7113 -- blocks and whatever local entities may appear in the private part.
7115 elsif Is_Concurrent_Type (E) then
7116 if Present (Corresponding_Record_Type (E)) then
7117 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
7118 end if;
7120 Comp := First_Entity (E);
7121 while Present (Comp) loop
7122 if Is_Type (Comp) then
7123 Freeze_And_Append (Comp, N, Result);
7125 elsif Ekind (Comp) /= E_Function then
7127 -- The guard on the presence of the Etype seems to be needed
7128 -- for some CodePeer (-gnatcC) cases, but not clear why???
7130 if Present (Etype (Comp)) then
7131 if Is_Itype (Etype (Comp))
7132 and then Underlying_Type (Scope (Etype (Comp))) = E
7133 then
7134 Undelay_Type (Etype (Comp));
7135 end if;
7137 Freeze_And_Append (Etype (Comp), N, Result);
7138 end if;
7139 end if;
7141 Next_Entity (Comp);
7142 end loop;
7144 -- Private types are required to point to the same freeze node as
7145 -- their corresponding full views. The freeze node itself has to
7146 -- point to the partial view of the entity (because from the partial
7147 -- view, we can retrieve the full view, but not the reverse).
7148 -- However, in order to freeze correctly, we need to freeze the full
7149 -- view. If we are freezing at the end of a scope (or within the
7150 -- scope) of the private type, the partial and full views will have
7151 -- been swapped, the full view appears first in the entity chain and
7152 -- the swapping mechanism ensures that the pointers are properly set
7153 -- (on scope exit).
7155 -- If we encounter the partial view before the full view (e.g. when
7156 -- freezing from another scope), we freeze the full view, and then
7157 -- set the pointers appropriately since we cannot rely on swapping to
7158 -- fix things up (subtypes in an outer scope might not get swapped).
7160 -- If the full view is itself private, the above requirements apply
7161 -- to the underlying full view instead of the full view. But there is
7162 -- no swapping mechanism for the underlying full view so we need to
7163 -- set the pointers appropriately in both cases.
7165 elsif Is_Incomplete_Or_Private_Type (E)
7166 and then not Is_Generic_Type (E)
7167 then
7168 -- The construction of the dispatch table associated with library
7169 -- level tagged types forces freezing of all the primitives of the
7170 -- type, which may cause premature freezing of the partial view.
7171 -- For example:
7173 -- package Pkg is
7174 -- type T is tagged private;
7175 -- type DT is new T with private;
7176 -- procedure Prim (X : in out T; Y : in out DT'Class);
7177 -- private
7178 -- type T is tagged null record;
7179 -- Obj : T;
7180 -- type DT is new T with null record;
7181 -- end;
7183 -- In this case the type will be frozen later by the usual
7184 -- mechanism: an object declaration, an instantiation, or the
7185 -- end of a declarative part.
7187 if Is_Library_Level_Tagged_Type (E)
7188 and then No (Full_View (E))
7189 then
7190 Set_Is_Frozen (E, False);
7191 goto Leave;
7193 -- Case of full view present
7195 elsif Present (Full_View (E)) then
7197 -- If full view has already been frozen, then no further
7198 -- processing is required
7200 if Is_Frozen (Full_View (E)) then
7201 Set_Has_Delayed_Freeze (E, False);
7202 Set_Freeze_Node (E, Empty);
7204 -- Otherwise freeze full view and patch the pointers so that
7205 -- the freeze node will elaborate both views in the back end.
7206 -- However, if full view is itself private, freeze underlying
7207 -- full view instead and patch the pointers so that the freeze
7208 -- node will elaborate the three views in the back end.
7210 else
7211 declare
7212 Full : Entity_Id := Full_View (E);
7214 begin
7215 if Is_Private_Type (Full)
7216 and then Present (Underlying_Full_View (Full))
7217 then
7218 Full := Underlying_Full_View (Full);
7219 end if;
7221 Freeze_And_Append (Full, N, Result);
7223 if Full /= Full_View (E)
7224 and then Has_Delayed_Freeze (Full_View (E))
7225 then
7226 F_Node := Freeze_Node (Full);
7228 if Present (F_Node) then
7229 Inherit_Freeze_Node
7230 (Fnod => F_Node, Typ => Full_View (E));
7231 else
7232 Set_Has_Delayed_Freeze (Full_View (E), False);
7233 Set_Freeze_Node (Full_View (E), Empty);
7234 end if;
7235 end if;
7237 if Has_Delayed_Freeze (E) then
7238 F_Node := Freeze_Node (Full_View (E));
7240 if Present (F_Node) then
7241 Inherit_Freeze_Node (Fnod => F_Node, Typ => E);
7242 else
7243 -- {Incomplete,Private}_Subtypes with Full_Views
7244 -- constrained by discriminants.
7246 Set_Has_Delayed_Freeze (E, False);
7247 Set_Freeze_Node (E, Empty);
7248 end if;
7249 end if;
7250 end;
7251 end if;
7253 Check_Debug_Info_Needed (E);
7255 -- AI95-117 requires that the convention of a partial view be
7256 -- the same as the convention of the full view. Note that this
7257 -- is a recognized breach of privacy, but it's essential for
7258 -- logical consistency of representation, and the lack of a
7259 -- rule in RM95 was an oversight.
7261 Set_Convention (E, Convention (Full_View (E)));
7263 Set_Size_Known_At_Compile_Time (E,
7264 Size_Known_At_Compile_Time (Full_View (E)));
7266 -- Size information is copied from the full view to the
7267 -- incomplete or private view for consistency.
7269 -- We skip this is the full view is not a type. This is very
7270 -- strange of course, and can only happen as a result of
7271 -- certain illegalities, such as a premature attempt to derive
7272 -- from an incomplete type.
7274 if Is_Type (Full_View (E)) then
7275 Set_Size_Info (E, Full_View (E));
7276 Copy_RM_Size (To => E, From => Full_View (E));
7277 end if;
7279 goto Leave;
7281 -- Case of underlying full view present
7283 elsif Is_Private_Type (E)
7284 and then Present (Underlying_Full_View (E))
7285 then
7286 if not Is_Frozen (Underlying_Full_View (E)) then
7287 Freeze_And_Append (Underlying_Full_View (E), N, Result);
7288 end if;
7290 -- Patch the pointers so that the freeze node will elaborate
7291 -- both views in the back end.
7293 if Has_Delayed_Freeze (E) then
7294 F_Node := Freeze_Node (Underlying_Full_View (E));
7296 if Present (F_Node) then
7297 Inherit_Freeze_Node
7298 (Fnod => F_Node,
7299 Typ => E);
7300 else
7301 Set_Has_Delayed_Freeze (E, False);
7302 Set_Freeze_Node (E, Empty);
7303 end if;
7304 end if;
7306 Check_Debug_Info_Needed (E);
7308 goto Leave;
7310 -- Case of no full view present. If entity is subtype or derived,
7311 -- it is safe to freeze, correctness depends on the frozen status
7312 -- of parent. Otherwise it is either premature usage, or a Taft
7313 -- amendment type, so diagnosis is at the point of use and the
7314 -- type might be frozen later.
7316 elsif E /= Base_Type (E) then
7317 declare
7318 Btyp : constant Entity_Id := Base_Type (E);
7320 begin
7321 -- However, if the base type is itself private and has no
7322 -- (underlying) full view either, wait until the full type
7323 -- declaration is seen and all the full views are created.
7325 if Is_Private_Type (Btyp)
7326 and then No (Full_View (Btyp))
7327 and then No (Underlying_Full_View (Btyp))
7328 and then Has_Delayed_Freeze (Btyp)
7329 and then No (Freeze_Node (Btyp))
7330 then
7331 Set_Is_Frozen (E, False);
7332 Result := No_List;
7333 goto Leave;
7334 end if;
7335 end;
7337 elsif Is_Derived_Type (E) then
7338 null;
7340 else
7341 Set_Is_Frozen (E, False);
7342 Result := No_List;
7343 goto Leave;
7344 end if;
7346 -- For access subprogram, freeze types of all formals, the return
7347 -- type was already frozen, since it is the Etype of the function.
7348 -- Formal types can be tagged Taft amendment types, but otherwise
7349 -- they cannot be incomplete.
7351 elsif Ekind (E) = E_Subprogram_Type then
7352 Formal := First_Formal (E);
7353 while Present (Formal) loop
7354 if Ekind (Etype (Formal)) = E_Incomplete_Type
7355 and then No (Full_View (Etype (Formal)))
7356 then
7357 if Is_Tagged_Type (Etype (Formal)) then
7358 null;
7360 -- AI05-151: Incomplete types are allowed in access to
7361 -- subprogram specifications.
7363 elsif Ada_Version < Ada_2012 then
7364 Error_Msg_NE
7365 ("invalid use of incomplete type&", E, Etype (Formal));
7366 end if;
7367 end if;
7369 Freeze_And_Append (Etype (Formal), N, Result);
7370 Next_Formal (Formal);
7371 end loop;
7373 Freeze_Subprogram (E);
7375 -- For access to a protected subprogram, freeze the equivalent type
7376 -- (however this is not set if we are not generating code or if this
7377 -- is an anonymous type used just for resolution).
7379 elsif Is_Access_Protected_Subprogram_Type (E) then
7380 if Present (Equivalent_Type (E)) then
7381 Freeze_And_Append (Equivalent_Type (E), N, Result);
7382 end if;
7383 end if;
7385 -- Generic types are never seen by the back-end, and are also not
7386 -- processed by the expander (since the expander is turned off for
7387 -- generic processing), so we never need freeze nodes for them.
7389 if Is_Generic_Type (E) then
7390 goto Leave;
7391 end if;
7393 -- Some special processing for non-generic types to complete
7394 -- representation details not known till the freeze point.
7396 if Is_Fixed_Point_Type (E) then
7397 Freeze_Fixed_Point_Type (E);
7399 elsif Is_Enumeration_Type (E) then
7400 Freeze_Enumeration_Type (E);
7402 elsif Is_Integer_Type (E) then
7403 Adjust_Esize_For_Alignment (E);
7405 if Is_Modular_Integer_Type (E) then
7406 -- Standard_Address has been built with the assumption that its
7407 -- modulus was System_Address_Size, but this is not a universal
7408 -- property and may need to be corrected.
7410 if Is_RTE (E, RE_Address) then
7411 Set_Modulus (Standard_Address, Modulus (E));
7412 Set_Intval
7413 (High_Bound (Scalar_Range (Standard_Address)),
7414 Modulus (E) - 1);
7416 elsif Warn_On_Suspicious_Modulus_Value then
7417 Check_Suspicious_Modulus (E);
7418 end if;
7419 end if;
7421 -- The pool applies to named and anonymous access types, but not
7422 -- to subprogram and to internal types generated for 'Access
7423 -- references.
7425 elsif Is_Access_Object_Type (E)
7426 and then Ekind (E) /= E_Access_Attribute_Type
7427 then
7428 -- If a pragma Default_Storage_Pool applies, and this type has no
7429 -- Storage_Pool or Storage_Size clause (which must have occurred
7430 -- before the freezing point), then use the default. This applies
7431 -- only to base types.
7433 -- None of this applies to access to subprograms, for which there
7434 -- are clearly no pools.
7436 if Present (Default_Pool)
7437 and then Is_Base_Type (E)
7438 and then not Has_Storage_Size_Clause (E)
7439 and then No (Associated_Storage_Pool (E))
7440 then
7441 -- Case of pragma Default_Storage_Pool (null)
7443 if Nkind (Default_Pool) = N_Null then
7444 Set_No_Pool_Assigned (E);
7446 -- Case of pragma Default_Storage_Pool (Standard)
7448 elsif Entity (Default_Pool) = Standard_Standard then
7449 Set_Associated_Storage_Pool (E, RTE (RE_Global_Pool_Object));
7451 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
7453 else
7454 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
7455 end if;
7456 end if;
7458 -- Check restriction for standard storage pool
7460 if No (Associated_Storage_Pool (E)) then
7461 Check_Restriction (No_Standard_Storage_Pools, E);
7462 end if;
7464 -- Deal with error message for pure access type. This is not an
7465 -- error in Ada 2005 if there is no pool (see AI-366).
7467 if Is_Pure_Unit_Access_Type (E)
7468 and then (Ada_Version < Ada_2005
7469 or else not No_Pool_Assigned (E))
7470 and then not Is_Generic_Unit (Scope (E))
7471 then
7472 Error_Msg_N ("named access type not allowed in pure unit", E);
7474 if Ada_Version >= Ada_2005 then
7475 Error_Msg_N
7476 ("\would be legal if Storage_Size of 0 given??", E);
7478 elsif No_Pool_Assigned (E) then
7479 Error_Msg_N
7480 ("\would be legal in Ada 2005??", E);
7482 else
7483 Error_Msg_N
7484 ("\would be legal in Ada 2005 if "
7485 & "Storage_Size of 0 given??", E);
7486 end if;
7487 end if;
7488 end if;
7490 -- Case of composite types
7492 if Is_Composite_Type (E) then
7494 -- AI95-117 requires that all new primitives of a tagged type
7495 -- must inherit the convention of the full view of the
7496 -- type. Inherited and overriding operations are defined to
7497 -- inherit the convention of their parent or overridden
7498 -- subprogram (also specified in AI-117), which will have
7499 -- occurred earlier (in Derive_Subprogram and
7500 -- New_Overloaded_Entity). Here we set the convention of
7501 -- primitives that are still convention Ada, which will ensure
7502 -- that any new primitives inherit the type's convention. We
7503 -- don't do this for primitives that are internal to avoid
7504 -- potential problems in the case of nested subprograms and
7505 -- convention C. In addition, class-wide types can have a
7506 -- foreign convention inherited from their specific type, but
7507 -- are excluded from this since they don't have any associated
7508 -- primitives.
7510 if Is_Tagged_Type (E)
7511 and then not Is_Class_Wide_Type (E)
7512 and then Convention (E) /= Convention_Ada
7513 then
7514 declare
7515 Prim_List : constant Elist_Id := Primitive_Operations (E);
7516 Prim : Elmt_Id;
7518 begin
7519 Prim := First_Elmt (Prim_List);
7520 while Present (Prim) loop
7521 if Convention (Node (Prim)) = Convention_Ada
7522 and then Comes_From_Source (Node (Prim))
7523 then
7524 Set_Convention (Node (Prim), Convention (E));
7525 end if;
7527 Next_Elmt (Prim);
7528 end loop;
7529 end;
7530 end if;
7532 -- If the type is a simple storage pool type, then this is where
7533 -- we attempt to locate and validate its Allocate, Deallocate, and
7534 -- Storage_Size operations (the first is required, and the latter
7535 -- two are optional). We also verify that the full type for a
7536 -- private type is allowed to be a simple storage pool type.
7538 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
7539 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
7540 then
7541 -- If the type is marked Has_Private_Declaration, then this is
7542 -- a full type for a private type that was specified with the
7543 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
7544 -- pragma is allowed for the full type (for example, it can't
7545 -- be an array type, or a nonlimited record type).
7547 if Has_Private_Declaration (E) then
7548 if (not Is_Record_Type (E)
7549 or else not Is_Inherently_Limited_Type (E))
7550 and then not Is_Private_Type (E)
7551 then
7552 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
7553 Error_Msg_N
7554 ("pragma% can only apply to full type that is an " &
7555 "explicitly limited type", E);
7556 end if;
7557 end if;
7559 Validate_Simple_Pool_Ops : declare
7560 Pool_Type : Entity_Id renames E;
7561 Address_Type : constant Entity_Id := RTE (RE_Address);
7562 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
7564 procedure Validate_Simple_Pool_Op_Formal
7565 (Pool_Op : Entity_Id;
7566 Pool_Op_Formal : in out Entity_Id;
7567 Expected_Mode : Formal_Kind;
7568 Expected_Type : Entity_Id;
7569 Formal_Name : String;
7570 OK_Formal : in out Boolean);
7571 -- Validate one formal Pool_Op_Formal of the candidate pool
7572 -- operation Pool_Op. The formal must be of Expected_Type
7573 -- and have mode Expected_Mode. OK_Formal will be set to
7574 -- False if the formal doesn't match. If OK_Formal is False
7575 -- on entry, then the formal will effectively be ignored
7576 -- (because validation of the pool op has already failed).
7577 -- Upon return, Pool_Op_Formal will be updated to the next
7578 -- formal, if any.
7580 procedure Validate_Simple_Pool_Operation
7581 (Op_Name : Name_Id);
7582 -- Search for and validate a simple pool operation with the
7583 -- name Op_Name. If the name is Allocate, then there must be
7584 -- exactly one such primitive operation for the simple pool
7585 -- type. If the name is Deallocate or Storage_Size, then
7586 -- there can be at most one such primitive operation. The
7587 -- profile of the located primitive must conform to what
7588 -- is expected for each operation.
7590 ------------------------------------
7591 -- Validate_Simple_Pool_Op_Formal --
7592 ------------------------------------
7594 procedure Validate_Simple_Pool_Op_Formal
7595 (Pool_Op : Entity_Id;
7596 Pool_Op_Formal : in out Entity_Id;
7597 Expected_Mode : Formal_Kind;
7598 Expected_Type : Entity_Id;
7599 Formal_Name : String;
7600 OK_Formal : in out Boolean)
7602 begin
7603 -- If OK_Formal is False on entry, then simply ignore
7604 -- the formal, because an earlier formal has already
7605 -- been flagged.
7607 if not OK_Formal then
7608 return;
7610 -- If no formal is passed in, then issue an error for a
7611 -- missing formal.
7613 elsif No (Pool_Op_Formal) then
7614 Error_Msg_NE
7615 ("simple storage pool op missing formal " &
7616 Formal_Name & " of type&", Pool_Op, Expected_Type);
7617 OK_Formal := False;
7619 return;
7620 end if;
7622 if Etype (Pool_Op_Formal) /= Expected_Type then
7624 -- If the pool type was expected for this formal, then
7625 -- this will not be considered a candidate operation
7626 -- for the simple pool, so we unset OK_Formal so that
7627 -- the op and any later formals will be ignored.
7629 if Expected_Type = Pool_Type then
7630 OK_Formal := False;
7632 return;
7634 else
7635 Error_Msg_NE
7636 ("wrong type for formal " & Formal_Name &
7637 " of simple storage pool op; expected type&",
7638 Pool_Op_Formal, Expected_Type);
7639 end if;
7640 end if;
7642 -- Issue error if formal's mode is not the expected one
7644 if Ekind (Pool_Op_Formal) /= Expected_Mode then
7645 Error_Msg_N
7646 ("wrong mode for formal of simple storage pool op",
7647 Pool_Op_Formal);
7648 end if;
7650 -- Advance to the next formal
7652 Next_Formal (Pool_Op_Formal);
7653 end Validate_Simple_Pool_Op_Formal;
7655 ------------------------------------
7656 -- Validate_Simple_Pool_Operation --
7657 ------------------------------------
7659 procedure Validate_Simple_Pool_Operation
7660 (Op_Name : Name_Id)
7662 Op : Entity_Id;
7663 Found_Op : Entity_Id := Empty;
7664 Formal : Entity_Id;
7665 Is_OK : Boolean;
7667 begin
7668 pragma Assert
7669 (Op_Name in Name_Allocate
7670 | Name_Deallocate
7671 | Name_Storage_Size);
7673 Error_Msg_Name_1 := Op_Name;
7675 -- For each homonym declared immediately in the scope
7676 -- of the simple storage pool type, determine whether
7677 -- the homonym is an operation of the pool type, and,
7678 -- if so, check that its profile is as expected for
7679 -- a simple pool operation of that name.
7681 Op := Get_Name_Entity_Id (Op_Name);
7682 while Present (Op) loop
7683 if Ekind (Op) in E_Function | E_Procedure
7684 and then Scope (Op) = Current_Scope
7685 then
7686 Formal := First_Entity (Op);
7688 Is_OK := True;
7690 -- The first parameter must be of the pool type
7691 -- in order for the operation to qualify.
7693 if Op_Name = Name_Storage_Size then
7694 Validate_Simple_Pool_Op_Formal
7695 (Op, Formal, E_In_Parameter, Pool_Type,
7696 "Pool", Is_OK);
7697 else
7698 Validate_Simple_Pool_Op_Formal
7699 (Op, Formal, E_In_Out_Parameter, Pool_Type,
7700 "Pool", Is_OK);
7701 end if;
7703 -- If another operation with this name has already
7704 -- been located for the type, then flag an error,
7705 -- since we only allow the type to have a single
7706 -- such primitive.
7708 if Present (Found_Op) and then Is_OK then
7709 Error_Msg_NE
7710 ("only one % operation allowed for " &
7711 "simple storage pool type&", Op, Pool_Type);
7712 end if;
7714 -- In the case of Allocate and Deallocate, a formal
7715 -- of type System.Address is required.
7717 if Op_Name = Name_Allocate then
7718 Validate_Simple_Pool_Op_Formal
7719 (Op, Formal, E_Out_Parameter,
7720 Address_Type, "Storage_Address", Is_OK);
7722 elsif Op_Name = Name_Deallocate then
7723 Validate_Simple_Pool_Op_Formal
7724 (Op, Formal, E_In_Parameter,
7725 Address_Type, "Storage_Address", Is_OK);
7726 end if;
7728 -- In the case of Allocate and Deallocate, formals
7729 -- of type Storage_Count are required as the third
7730 -- and fourth parameters.
7732 if Op_Name /= Name_Storage_Size then
7733 Validate_Simple_Pool_Op_Formal
7734 (Op, Formal, E_In_Parameter,
7735 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
7736 Validate_Simple_Pool_Op_Formal
7737 (Op, Formal, E_In_Parameter,
7738 Stg_Cnt_Type, "Alignment", Is_OK);
7739 end if;
7741 -- If no mismatched formals have been found (Is_OK)
7742 -- and no excess formals are present, then this
7743 -- operation has been validated, so record it.
7745 if No (Formal) and then Is_OK then
7746 Found_Op := Op;
7747 end if;
7748 end if;
7750 Op := Homonym (Op);
7751 end loop;
7753 -- There must be a valid Allocate operation for the type,
7754 -- so issue an error if none was found.
7756 if Op_Name = Name_Allocate
7757 and then No (Found_Op)
7758 then
7759 Error_Msg_N ("missing % operation for simple " &
7760 "storage pool type", Pool_Type);
7762 elsif Present (Found_Op) then
7764 -- Simple pool operations can't be abstract
7766 if Is_Abstract_Subprogram (Found_Op) then
7767 Error_Msg_N
7768 ("simple storage pool operation must not be " &
7769 "abstract", Found_Op);
7770 end if;
7772 -- The Storage_Size operation must be a function with
7773 -- Storage_Count as its result type.
7775 if Op_Name = Name_Storage_Size then
7776 if Ekind (Found_Op) = E_Procedure then
7777 Error_Msg_N
7778 ("% operation must be a function", Found_Op);
7780 elsif Etype (Found_Op) /= Stg_Cnt_Type then
7781 Error_Msg_NE
7782 ("wrong result type for%, expected type&",
7783 Found_Op, Stg_Cnt_Type);
7784 end if;
7786 -- Allocate and Deallocate must be procedures
7788 elsif Ekind (Found_Op) = E_Function then
7789 Error_Msg_N
7790 ("% operation must be a procedure", Found_Op);
7791 end if;
7792 end if;
7793 end Validate_Simple_Pool_Operation;
7795 -- Start of processing for Validate_Simple_Pool_Ops
7797 begin
7798 Validate_Simple_Pool_Operation (Name_Allocate);
7799 Validate_Simple_Pool_Operation (Name_Deallocate);
7800 Validate_Simple_Pool_Operation (Name_Storage_Size);
7801 end Validate_Simple_Pool_Ops;
7802 end if;
7803 end if;
7805 -- Now that all types from which E may depend are frozen, see if
7806 -- strict alignment is required, a component clause on a record
7807 -- is correct, the size is known at compile time and if it must
7808 -- be unsigned, in that order.
7810 if Base_Type (E) = E then
7811 Check_Strict_Alignment (E);
7812 end if;
7814 if Ekind (E) in E_Record_Type | E_Record_Subtype then
7815 declare
7816 RC : constant Node_Id := Get_Record_Representation_Clause (E);
7817 begin
7818 if Present (RC) then
7819 Check_Record_Representation_Clause (RC);
7820 end if;
7821 end;
7822 end if;
7824 Check_Compile_Time_Size (E);
7826 Check_Unsigned_Type (E);
7828 -- Do not allow a size clause for a type which does not have a size
7829 -- that is known at compile time
7831 if (Has_Size_Clause (E) or else Has_Object_Size_Clause (E))
7832 and then not Size_Known_At_Compile_Time (E)
7833 and then not Is_Mutably_Tagged_Type (E)
7834 then
7835 -- Suppress this message if errors posted on E, even if we are
7836 -- in all errors mode, since this is often a junk message
7838 if not Error_Posted (E) then
7839 Error_Msg_N
7840 ("size clause not allowed for variable length type",
7841 Size_Clause (E));
7842 end if;
7843 end if;
7845 -- Now we set/verify the representation information, in particular
7846 -- the size and alignment values. This processing is not required for
7847 -- generic types, since generic types do not play any part in code
7848 -- generation, and so the size and alignment values for such types
7849 -- are irrelevant. Ditto for types declared within a generic unit,
7850 -- which may have components that depend on generic parameters, and
7851 -- that will be recreated in an instance.
7853 if Inside_A_Generic then
7854 null;
7856 -- Otherwise we call the layout procedure
7858 else
7859 Layout_Type (E);
7860 end if;
7862 -- If this is an access to subprogram whose designated type is itself
7863 -- a subprogram type, the return type of this anonymous subprogram
7864 -- type must be decorated as well.
7866 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
7867 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
7868 then
7869 Layout_Type (Etype (Designated_Type (E)));
7870 end if;
7872 -- If the type has a Defaut_Value/Default_Component_Value aspect,
7873 -- this is where we analyze the expression (after the type is frozen,
7874 -- since in the case of Default_Value, we are analyzing with the
7875 -- type itself, and we treat Default_Component_Value similarly for
7876 -- the sake of uniformity).
7878 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
7879 declare
7880 Nam : Name_Id;
7881 Exp : Node_Id;
7882 Typ : Entity_Id;
7884 begin
7885 if Is_Scalar_Type (E) then
7886 Nam := Name_Default_Value;
7887 Typ := E;
7888 Exp := Default_Aspect_Value (Typ);
7889 else
7890 Nam := Name_Default_Component_Value;
7891 Typ := Component_Type (E);
7892 Exp := Default_Aspect_Component_Value (E);
7893 end if;
7895 Analyze_And_Resolve (Exp, Typ);
7897 if Etype (Exp) /= Any_Type then
7898 if not Is_OK_Static_Expression (Exp) then
7899 Error_Msg_Name_1 := Nam;
7900 Flag_Non_Static_Expr
7901 ("aspect% requires static expression", Exp);
7902 end if;
7903 end if;
7904 end;
7905 end if;
7907 -- Verify at this point that No_Controlled_Parts and No_Task_Parts,
7908 -- when specified on the current type or one of its ancestors, has
7909 -- not been overridden and that no violation of the aspect has
7910 -- occurred.
7912 -- It is important that we perform the checks here after the type has
7913 -- been processed because if said type depended on a private type it
7914 -- will not have been marked controlled or having tasks.
7916 Check_No_Parts_Violations (E, Aspect_No_Controlled_Parts);
7917 Check_No_Parts_Violations (E, Aspect_No_Task_Parts);
7919 -- End of freeze processing for type entities
7920 end if;
7922 -- Here is where we logically freeze the current entity. If it has a
7923 -- freeze node, then this is the point at which the freeze node is
7924 -- linked into the result list.
7926 if Has_Delayed_Freeze (E) then
7928 -- If a freeze node is already allocated, use it, otherwise allocate
7929 -- a new one. The preallocation happens in the case of anonymous base
7930 -- types, where we preallocate so that we can set First_Subtype_Link.
7931 -- Note that we reset the Sloc to the current freeze location.
7933 if Present (Freeze_Node (E)) then
7934 F_Node := Freeze_Node (E);
7935 Set_Sloc (F_Node, Loc);
7937 else
7938 F_Node := New_Node (N_Freeze_Entity, Loc);
7939 Set_Freeze_Node (E, F_Node);
7940 Set_Access_Types_To_Process (F_Node, No_Elist);
7941 Set_TSS_Elist (F_Node, No_Elist);
7942 Set_Actions (F_Node, No_List);
7943 end if;
7945 Set_Entity (F_Node, E);
7946 Add_To_Result (F_Node);
7948 -- A final pass over record types with discriminants. If the type
7949 -- has an incomplete declaration, there may be constrained access
7950 -- subtypes declared elsewhere, which do not depend on the discrimi-
7951 -- nants of the type, and which are used as component types (i.e.
7952 -- the full view is a recursive type). The designated types of these
7953 -- subtypes can only be elaborated after the type itself, and they
7954 -- need an itype reference.
7956 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
7957 declare
7958 Comp : Entity_Id;
7959 IR : Node_Id;
7960 Typ : Entity_Id;
7962 begin
7963 Comp := First_Component (E);
7964 while Present (Comp) loop
7965 Typ := Etype (Comp);
7967 if Is_Access_Type (Typ)
7968 and then Scope (Typ) /= E
7969 and then Base_Type (Designated_Type (Typ)) = E
7970 and then Is_Itype (Designated_Type (Typ))
7971 then
7972 IR := Make_Itype_Reference (Sloc (Comp));
7973 Set_Itype (IR, Designated_Type (Typ));
7974 Append (IR, Result);
7975 end if;
7977 Next_Component (Comp);
7978 end loop;
7979 end;
7980 end if;
7981 end if;
7983 -- When a type is frozen, the first subtype of the type is frozen as
7984 -- well (RM 13.14(15)). This has to be done after freezing the type,
7985 -- since obviously the first subtype depends on its own base type.
7987 if Is_Type (E) then
7988 Freeze_And_Append (First_Subtype (E), N, Result);
7990 -- If we just froze a tagged non-class-wide record, then freeze the
7991 -- corresponding class-wide type. This must be done after the tagged
7992 -- type itself is frozen, because the class-wide type refers to the
7993 -- tagged type which generates the class.
7995 -- For a tagged type, freeze explicitly those primitive operations
7996 -- that are expression functions, which otherwise have no clear
7997 -- freeze point: these have to be frozen before the dispatch table
7998 -- for the type is built, and before any explicit call to the
7999 -- primitive, which would otherwise be the freeze point for it.
8001 if Is_Tagged_Type (E)
8002 and then not Is_Class_Wide_Type (E)
8003 and then Present (Class_Wide_Type (E))
8004 then
8005 Freeze_And_Append (Class_Wide_Type (E), N, Result);
8007 declare
8008 Ops : constant Elist_Id := Primitive_Operations (E);
8010 Elmt : Elmt_Id;
8011 Subp : Entity_Id;
8013 begin
8014 if Ops /= No_Elist then
8015 Elmt := First_Elmt (Ops);
8016 while Present (Elmt) loop
8017 Subp := Node (Elmt);
8018 if Is_Expression_Function (Subp) then
8019 Freeze_And_Append (Subp, N, Result);
8020 end if;
8022 Next_Elmt (Elmt);
8023 end loop;
8024 end if;
8025 end;
8026 end if;
8027 end if;
8029 Check_Debug_Info_Needed (E);
8031 -- If subprogram has address clause then reset Is_Public flag, since we
8032 -- do not want the backend to generate external references.
8034 if Is_Subprogram (E)
8035 and then Present (Address_Clause (E))
8036 and then not Is_Library_Level_Entity (E)
8037 then
8038 Set_Is_Public (E, False);
8039 end if;
8041 -- The Ghost mode of the enclosing context is ignored, while the
8042 -- entity being frozen is living. Insert the freezing action prior
8043 -- to the start of the enclosing ignored Ghost region. As a result
8044 -- the freezeing action will be preserved when the ignored Ghost
8045 -- context is eliminated. The insertion must take place even when
8046 -- the context is a spec expression, otherwise "Handling of Default
8047 -- and Per-Object Expressions" will suppress the insertion, and the
8048 -- freeze node will be dropped on the floor.
8050 if Saved_GM = Ignore
8051 and then Ghost_Mode /= Ignore
8052 and then Present (Ignored_Ghost_Region)
8053 then
8054 Insert_Actions
8055 (Assoc_Node => Ignored_Ghost_Region,
8056 Ins_Actions => Result,
8057 Spec_Expr_OK => True);
8059 Result := No_List;
8060 end if;
8062 <<Leave>>
8063 Restore_Ghost_Region (Saved_GM, Saved_IGR);
8065 return Result;
8066 end Freeze_Entity;
8068 -----------------------------
8069 -- Freeze_Enumeration_Type --
8070 -----------------------------
8072 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
8073 begin
8074 -- By default, if no size clause is present, an enumeration type with
8075 -- Convention C is assumed to interface to a C enum and has integer
8076 -- size, except for a boolean type because it is assumed to interface
8077 -- to _Bool introduced in C99. This applies to types. For subtypes,
8078 -- verify that its base type has no size clause either. Treat other
8079 -- foreign conventions in the same way, and also make sure alignment
8080 -- is set right.
8082 if Has_Foreign_Convention (Typ)
8083 and then not Is_Boolean_Type (Typ)
8084 and then not Has_Size_Clause (Typ)
8085 and then not Has_Size_Clause (Base_Type (Typ))
8086 and then Esize (Typ) < Standard_Integer_Size
8088 -- Don't do this if Short_Enums on target
8090 and then not Target_Short_Enums
8091 then
8092 Set_Esize (Typ, UI_From_Int (Standard_Integer_Size));
8093 Set_Alignment (Typ, Alignment (Standard_Integer));
8095 -- Normal Ada case or size clause present or not Long_C_Enums on target
8097 else
8098 -- If the enumeration type interfaces to C, and it has a size clause
8099 -- that is smaller than the size of int, it warrants a warning. The
8100 -- user may intend the C type to be a boolean or a char, so this is
8101 -- not by itself an error that the Ada compiler can detect, but it
8102 -- is worth a heads-up. For Boolean and Character types we
8103 -- assume that the programmer has the proper C type in mind.
8104 -- For explicit sizes larger than int, assume the user knows what
8105 -- he is doing and that the code is intentional.
8107 if Convention (Typ) = Convention_C
8108 and then Has_Size_Clause (Typ)
8109 and then Esize (Typ) < Standard_Integer_Size
8110 and then not Is_Boolean_Type (Typ)
8111 and then not Is_Character_Type (Typ)
8113 -- Don't do this if Short_Enums on target
8115 and then not Target_Short_Enums
8116 then
8117 Error_Msg_N
8118 ("??the size of enums in C is implementation-defined",
8119 Size_Clause (Typ));
8120 Error_Msg_N
8121 ("\??check that the C counterpart has size of " &
8122 UI_Image (Esize (Typ)),
8123 Size_Clause (Typ));
8124 end if;
8126 Adjust_Esize_For_Alignment (Typ);
8127 end if;
8129 -- Reject a very large size on a type with a non-standard representation
8130 -- because Expand_Freeze_Enumeration_Type cannot deal with it.
8132 if Has_Non_Standard_Rep (Typ)
8133 and then Known_Esize (Typ)
8134 and then Esize (Typ) > System_Max_Integer_Size
8135 then
8136 Error_Msg_N
8137 ("enumeration type with representation clause too large", Typ);
8138 Error_Msg_Uint_1 := UI_From_Int (System_Max_Integer_Size);
8139 Error_Msg_N
8140 ("\the size of such a type cannot exceed ^ bits", Typ);
8141 end if;
8142 end Freeze_Enumeration_Type;
8144 -----------------------
8145 -- Freeze_Expression --
8146 -----------------------
8148 procedure Freeze_Expression (N : Node_Id) is
8150 function Declared_In_Expanded_Body
8151 (N : Node_Id;
8152 Typ : Entity_Id;
8153 Nam : Entity_Id) return Boolean;
8154 -- Given the N_Handled_Sequence_Of_Statements node of an expander
8155 -- generated subprogram body, determines if the frozen entity is
8156 -- declared inside this body. This is recognized locating the
8157 -- enclosing subprogram of the entity Name or its Type and
8158 -- checking if it is this subprogram body.
8160 function Find_Aggregate_Component_Desig_Type return Entity_Id;
8161 -- If the expression is an array aggregate, the type of the component
8162 -- expressions is also frozen. If the component type is an access type
8163 -- and the expressions include allocators, the designed type is frozen
8164 -- as well.
8166 function In_Expanded_Body (N : Node_Id) return Boolean;
8167 -- Given an N_Handled_Sequence_Of_Statements node, determines whether it
8168 -- is the statement sequence of an expander-generated subprogram: body
8169 -- created for an expression function, for a predicate function, an init
8170 -- proc, a stream subprogram, or a renaming as body. If so, this is not
8171 -- a freezing context and the entity will be frozen at a later point.
8173 function Has_Decl_In_List
8174 (E : Entity_Id;
8175 N : Node_Id;
8176 L : List_Id) return Boolean;
8177 -- Determines whether an entity E referenced in node N is declared in
8178 -- the list L.
8180 -------------------------------
8181 -- Declared_In_Expanded_Body --
8182 -------------------------------
8184 function Declared_In_Expanded_Body
8185 (N : Node_Id;
8186 Typ : Entity_Id;
8187 Nam : Entity_Id) return Boolean
8189 pragma Assert (In_Expanded_Body (N));
8191 Subp_Body : constant Node_Id := Parent (N);
8192 Subp_Id : Entity_Id;
8193 Scop : Entity_Id;
8195 begin
8196 if Acts_As_Spec (Subp_Body) then
8197 Subp_Id := Unique_Defining_Entity (Specification (Subp_Body));
8198 else
8199 Subp_Id := Corresponding_Spec (Subp_Body);
8200 end if;
8202 if Present (Typ) then
8203 Scop := Scope (Typ);
8204 elsif Present (Nam) then
8205 Scop := Scope (Nam);
8206 else
8207 Scop := Standard_Standard;
8208 end if;
8210 while Scop /= Standard_Standard
8211 and then not Is_Subprogram (Scop)
8212 loop
8213 Scop := Scope (Scop);
8214 end loop;
8216 return Scop = Subp_Id;
8217 end Declared_In_Expanded_Body;
8219 -----------------------------------------
8220 -- Find_Aggregate_Component_Desig_Type --
8221 -----------------------------------------
8223 function Find_Aggregate_Component_Desig_Type return Entity_Id is
8224 Assoc : Node_Id;
8225 Exp : Node_Id;
8227 begin
8228 if Present (Expressions (N)) then
8229 Exp := First (Expressions (N));
8230 while Present (Exp) loop
8231 if Nkind (Exp) = N_Allocator then
8232 return Designated_Type (Component_Type (Etype (N)));
8233 end if;
8235 Next (Exp);
8236 end loop;
8237 end if;
8239 if Present (Component_Associations (N)) then
8240 Assoc := First (Component_Associations (N));
8241 while Present (Assoc) loop
8242 if Nkind (Expression (Assoc)) = N_Allocator then
8243 return Designated_Type (Component_Type (Etype (N)));
8244 end if;
8246 Next (Assoc);
8247 end loop;
8248 end if;
8250 return Empty;
8251 end Find_Aggregate_Component_Desig_Type;
8253 ----------------------
8254 -- In_Expanded_Body --
8255 ----------------------
8257 function In_Expanded_Body (N : Node_Id) return Boolean is
8258 P : constant Node_Id := Parent (N);
8259 Id : Entity_Id;
8261 begin
8262 if Nkind (P) /= N_Subprogram_Body then
8263 return False;
8265 -- Treat the generated body of an expression function like other
8266 -- bodies generated during expansion (e.g. stream subprograms) so
8267 -- that those bodies are not treated as freezing points.
8269 elsif Was_Expression_Function (P) then
8270 pragma Assert (not Comes_From_Source (P));
8271 return True;
8273 -- This is the body of a generated predicate function
8275 elsif Present (Corresponding_Spec (P))
8276 and then Is_Predicate_Function (Corresponding_Spec (P))
8277 then
8278 return True;
8280 else
8281 Id := Defining_Unit_Name (Specification (P));
8283 -- The following are expander-created bodies, or bodies that
8284 -- are not freeze points.
8286 if Nkind (Id) = N_Defining_Identifier
8287 and then (Is_Init_Proc (Id)
8288 or else Is_TSS (Id, TSS_Stream_Input)
8289 or else Is_TSS (Id, TSS_Stream_Output)
8290 or else Is_TSS (Id, TSS_Stream_Read)
8291 or else Is_TSS (Id, TSS_Stream_Write)
8292 or else Is_TSS (Id, TSS_Put_Image)
8293 or else Nkind (Original_Node (P)) =
8294 N_Subprogram_Renaming_Declaration)
8295 then
8296 return True;
8297 else
8298 return False;
8299 end if;
8300 end if;
8301 end In_Expanded_Body;
8303 ----------------------
8304 -- Has_Decl_In_List --
8305 ----------------------
8307 function Has_Decl_In_List
8308 (E : Entity_Id;
8309 N : Node_Id;
8310 L : List_Id) return Boolean
8312 Decl_Node : Node_Id;
8314 begin
8315 -- If E is an itype, pretend that it is declared in N except for a
8316 -- class-wide subtype with an equivalent type, because this latter
8317 -- type comes with a bona-fide declaration node.
8319 if Is_Itype (E) then
8320 if Ekind (E) = E_Class_Wide_Subtype
8321 and then Present (Equivalent_Type (E))
8322 then
8323 Decl_Node := Declaration_Node (Equivalent_Type (E));
8324 else
8325 Decl_Node := N;
8326 end if;
8328 else
8329 Decl_Node := Declaration_Node (E);
8330 end if;
8332 return Is_List_Member (Decl_Node)
8333 and then List_Containing (Decl_Node) = L;
8334 end Has_Decl_In_List;
8336 -- Local variables
8338 In_Spec_Exp : constant Boolean := In_Spec_Expression;
8340 Desig_Typ : Entity_Id;
8341 Nam : Entity_Id;
8342 P : Node_Id;
8343 Parent_P : Node_Id;
8344 Typ : Entity_Id;
8346 Allocator_Typ : Entity_Id := Empty;
8348 Freeze_Outside_Subp : Entity_Id := Empty;
8349 -- This entity is set if we are inside a subprogram body and the frozen
8350 -- entity is defined in the enclosing scope of this subprogram. In such
8351 -- case we must skip the subprogram body when climbing the parents chain
8352 -- to locate the correct placement for the freezing node.
8354 -- Start of processing for Freeze_Expression
8356 begin
8357 -- Immediate return if freezing is inhibited. This flag is set by the
8358 -- analyzer to stop freezing on generated expressions that would cause
8359 -- freezing if they were in the source program, but which are not
8360 -- supposed to freeze, since they are created.
8362 if Must_Not_Freeze (N) then
8363 return;
8364 end if;
8366 -- If expression is non-static, then it does not freeze in a default
8367 -- expression, see section "Handling of Default Expressions" in the
8368 -- spec of package Sem for further details. Note that we have to make
8369 -- sure that we actually have a real expression (if we have a subtype
8370 -- indication, we can't test Is_OK_Static_Expression). However, we
8371 -- exclude the case of the prefix of an attribute of a static scalar
8372 -- subtype from this early return, because static subtype attributes
8373 -- should always cause freezing, even in default expressions, but
8374 -- the attribute may not have been marked as static yet (because in
8375 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
8376 -- Freeze_Expression on the prefix).
8378 if In_Spec_Exp
8379 and then Nkind (N) in N_Subexpr
8380 and then not Is_OK_Static_Expression (N)
8381 and then (Nkind (Parent (N)) /= N_Attribute_Reference
8382 or else not (Is_Entity_Name (N)
8383 and then Is_Type (Entity (N))
8384 and then Is_OK_Static_Subtype (Entity (N))))
8385 then
8386 return;
8387 end if;
8389 -- Freeze type of expression if not frozen already
8391 Typ := Empty;
8393 if Nkind (N) in N_Has_Etype and then Present (Etype (N)) then
8394 if not Is_Frozen (Etype (N)) then
8395 Typ := Etype (N);
8397 -- Base type may be an derived numeric type that is frozen at the
8398 -- point of declaration, but first_subtype is still unfrozen.
8400 elsif not Is_Frozen (First_Subtype (Etype (N))) then
8401 Typ := First_Subtype (Etype (N));
8402 end if;
8403 end if;
8405 -- For entity name, freeze entity if not frozen already. A special
8406 -- exception occurs for an identifier that did not come from source.
8407 -- We don't let such identifiers freeze a non-internal entity, i.e.
8408 -- an entity that did come from source, since such an identifier was
8409 -- generated by the expander, and cannot have any semantic effect on
8410 -- the freezing semantics. For example, this stops the parameter of
8411 -- an initialization procedure from freezing the variable.
8413 if Is_Entity_Name (N)
8414 and then Present (Entity (N))
8415 and then not Is_Frozen (Entity (N))
8416 and then (Nkind (N) /= N_Identifier
8417 or else Comes_From_Source (N)
8418 or else not Comes_From_Source (Entity (N)))
8419 then
8420 Nam := Entity (N);
8422 if Present (Nam) and then Ekind (Nam) = E_Function then
8423 Check_Expression_Function (N, Nam);
8424 end if;
8426 else
8427 Nam := Empty;
8428 end if;
8430 -- For an allocator freeze designated type if not frozen already
8432 -- For an aggregate whose component type is an access type, freeze the
8433 -- designated type now, so that its freeze does not appear within the
8434 -- loop that might be created in the expansion of the aggregate. If the
8435 -- designated type is a private type without full view, the expression
8436 -- cannot contain an allocator, so the type is not frozen.
8438 -- For a function, we freeze the entity when the subprogram declaration
8439 -- is frozen, but a function call may appear in an initialization proc.
8440 -- before the declaration is frozen. We need to generate the extra
8441 -- formals, if any, to ensure that the expansion of the call includes
8442 -- the proper actuals. This only applies to Ada subprograms, not to
8443 -- imported ones.
8445 Desig_Typ := Empty;
8447 case Nkind (N) is
8448 when N_Allocator =>
8449 Desig_Typ := Designated_Type (Etype (N));
8451 if Nkind (Expression (N)) = N_Qualified_Expression then
8452 Allocator_Typ := Entity (Subtype_Mark (Expression (N)));
8453 end if;
8455 when N_Aggregate =>
8456 if Is_Array_Type (Etype (N))
8457 and then Is_Access_Type (Component_Type (Etype (N)))
8458 then
8459 -- Check whether aggregate includes allocators
8461 Desig_Typ := Find_Aggregate_Component_Desig_Type;
8462 end if;
8464 when N_Indexed_Component
8465 | N_Selected_Component
8466 | N_Slice
8468 if Is_Access_Type (Etype (Prefix (N))) then
8469 Desig_Typ := Designated_Type (Etype (Prefix (N)));
8470 end if;
8472 when N_Identifier =>
8473 if Present (Nam)
8474 and then Ekind (Nam) = E_Function
8475 and then Nkind (Parent (N)) = N_Function_Call
8476 and then not Has_Foreign_Convention (Nam)
8477 then
8478 Create_Extra_Formals (Nam);
8479 end if;
8481 when others =>
8482 null;
8483 end case;
8485 if Desig_Typ /= Empty
8486 and then (Is_Frozen (Desig_Typ)
8487 or else not Is_Fully_Defined (Desig_Typ))
8488 then
8489 Desig_Typ := Empty;
8490 end if;
8492 -- All done if nothing needs freezing
8494 if No (Typ)
8495 and then No (Nam)
8496 and then No (Desig_Typ)
8497 and then No (Allocator_Typ)
8498 then
8499 return;
8500 end if;
8502 -- Check if we are inside a subprogram body and the frozen entity is
8503 -- defined in the enclosing scope of this subprogram. In such case we
8504 -- must skip the subprogram when climbing the parents chain to locate
8505 -- the correct placement for the freezing node.
8507 -- This is not needed for default expressions and other spec expressions
8508 -- in generic units since the Move_Freeze_Nodes mechanism (sem_ch12.adb)
8509 -- takes care of placing them at the proper place, after the generic
8510 -- unit.
8512 if Present (Nam)
8513 and then Scope (Nam) /= Current_Scope
8514 and then not (In_Spec_Exp and then Inside_A_Generic)
8515 then
8516 declare
8517 S : Entity_Id := Current_Scope;
8519 begin
8520 while Present (S)
8521 and then In_Same_Source_Unit (Nam, S)
8522 loop
8523 if Scope (S) = Scope (Nam) then
8524 if Is_Subprogram (S) and then Has_Completion (S) then
8525 Freeze_Outside_Subp := S;
8526 end if;
8528 exit;
8529 end if;
8531 S := Scope (S);
8532 end loop;
8533 end;
8534 end if;
8536 -- Examine the enclosing context by climbing the parent chain
8538 -- If we identified that we must freeze the entity outside of a given
8539 -- subprogram then we just climb up to that subprogram checking if some
8540 -- enclosing node is marked as Must_Not_Freeze (since in such case we
8541 -- must not freeze yet this entity).
8543 P := N;
8545 if Present (Freeze_Outside_Subp) then
8546 loop
8547 -- Do not freeze the current expression if another expression in
8548 -- the chain of parents must not be frozen.
8550 if Nkind (P) in N_Subexpr and then Must_Not_Freeze (P) then
8551 return;
8552 end if;
8554 Parent_P := Parent (P);
8556 -- If we don't have a parent, then we are not in a well-formed
8557 -- tree. This is an unusual case, but there are some legitimate
8558 -- situations in which this occurs, notably when the expressions
8559 -- in the range of a type declaration are resolved. We simply
8560 -- ignore the freeze request in this case.
8562 if No (Parent_P) then
8563 return;
8564 end if;
8566 -- If the parent is a subprogram body, the candidate insertion
8567 -- point is just ahead of it.
8569 if Nkind (Parent_P) = N_Subprogram_Body
8570 and then Unique_Defining_Entity (Parent_P) =
8571 Freeze_Outside_Subp
8572 then
8573 P := Parent_P;
8574 exit;
8575 end if;
8577 P := Parent_P;
8578 end loop;
8580 -- Otherwise the traversal serves two purposes - to detect scenarios
8581 -- where freezeing is not needed and to find the proper insertion point
8582 -- for the freeze nodes. Although somewhat similar to Insert_Actions,
8583 -- this traversal is freezing semantics-sensitive. Inserting freeze
8584 -- nodes blindly in the tree may result in types being frozen too early.
8586 else
8587 loop
8588 -- Do not freeze the current expression if another expression in
8589 -- the chain of parents must not be frozen.
8591 if Nkind (P) in N_Subexpr and then Must_Not_Freeze (P) then
8592 return;
8593 end if;
8595 Parent_P := Parent (P);
8597 -- If we don't have a parent, then we are not in a well-formed
8598 -- tree. This is an unusual case, but there are some legitimate
8599 -- situations in which this occurs, notably when the expressions
8600 -- in the range of a type declaration are resolved. We simply
8601 -- ignore the freeze request in this case.
8603 if No (Parent_P) then
8604 return;
8605 end if;
8607 -- See if we have got to an appropriate point in the tree
8609 case Nkind (Parent_P) is
8611 -- A special test for the exception of (RM 13.14(8)) for the
8612 -- case of per-object expressions (RM 3.8(18)) occurring in
8613 -- component definition or a discrete subtype definition. Note
8614 -- that we test for a component declaration which includes both
8615 -- cases we are interested in, and furthermore the tree does
8616 -- not have explicit nodes for either of these two constructs.
8618 when N_Component_Declaration =>
8620 -- The case we want to test for here is an identifier that
8621 -- is a per-object expression, this is either a discriminant
8622 -- that appears in a context other than the component
8623 -- declaration or it is a reference to the type of the
8624 -- enclosing construct.
8626 -- For either of these cases, we skip the freezing
8628 if not In_Spec_Expression
8629 and then Nkind (N) = N_Identifier
8630 and then Present (Entity (N))
8631 then
8632 -- We recognize the discriminant case by just looking for
8633 -- a reference to a discriminant. It can only be one for
8634 -- the enclosing construct. Skip freezing in this case.
8636 if Ekind (Entity (N)) = E_Discriminant then
8637 return;
8639 -- For the case of a reference to the enclosing record,
8640 -- (or task or protected type), we look for a type that
8641 -- matches the current scope.
8643 elsif Entity (N) = Current_Scope then
8644 return;
8645 end if;
8646 end if;
8648 -- If we have an enumeration literal that appears as the choice
8649 -- in the aggregate of an enumeration representation clause,
8650 -- then freezing does not occur (RM 13.14(10)).
8652 when N_Enumeration_Representation_Clause =>
8654 -- The case we are looking for is an enumeration literal
8656 if Nkind (N) in N_Identifier | N_Character_Literal
8657 and then Is_Enumeration_Type (Etype (N))
8658 then
8659 -- If enumeration literal appears directly as the choice,
8660 -- do not freeze (this is the normal non-overloaded case)
8662 if Nkind (Parent (N)) = N_Component_Association
8663 and then First (Choices (Parent (N))) = N
8664 then
8665 return;
8667 -- If enumeration literal appears as the name of function
8668 -- which is the choice, then also do not freeze. This
8669 -- happens in the overloaded literal case, where the
8670 -- enumeration literal is temporarily changed to a
8671 -- function call for overloading analysis purposes.
8673 elsif Nkind (Parent (N)) = N_Function_Call
8674 and then Nkind (Parent (Parent (N))) =
8675 N_Component_Association
8676 and then First (Choices (Parent (Parent (N)))) =
8677 Parent (N)
8678 then
8679 return;
8680 end if;
8681 end if;
8683 -- Normally if the parent is a handled sequence of statements,
8684 -- then the current node must be a statement, and that is an
8685 -- appropriate place to insert a freeze node.
8687 when N_Handled_Sequence_Of_Statements =>
8689 -- An exception occurs when the sequence of statements is
8690 -- for an expander generated body that did not do the usual
8691 -- freeze all operation. In this case we usually want to
8692 -- freeze outside this body, not inside it, unless the
8693 -- entity is declared inside this expander generated body.
8695 exit when not In_Expanded_Body (Parent_P)
8696 or else Declared_In_Expanded_Body (Parent_P, Typ, Nam);
8698 -- If parent is a body or a spec or a block, then the current
8699 -- node is a statement or declaration and we can insert the
8700 -- freeze node before it.
8702 when N_Block_Statement
8703 | N_Entry_Body
8704 | N_Package_Body
8705 | N_Package_Specification
8706 | N_Protected_Body
8707 | N_Subprogram_Body
8708 | N_Task_Body
8710 exit;
8712 -- The expander is allowed to define types in any statements
8713 -- list, so any of the following parent nodes also mark a
8714 -- freezing point if the actual node is in a list of
8715 -- statements or declarations.
8717 when N_Abortable_Part
8718 | N_Accept_Alternative
8719 | N_Case_Statement_Alternative
8720 | N_Compilation_Unit_Aux
8721 | N_Conditional_Entry_Call
8722 | N_Delay_Alternative
8723 | N_Elsif_Part
8724 | N_Entry_Call_Alternative
8725 | N_Exception_Handler
8726 | N_Extended_Return_Statement
8727 | N_Freeze_Entity
8728 | N_If_Statement
8729 | N_Selective_Accept
8730 | N_Triggering_Alternative
8732 if No (Current_Subprogram) then
8733 exit when Is_List_Member (P);
8735 -- Check exceptional case documented above for an enclosing
8736 -- handled sequence of statements.
8738 else
8739 declare
8740 Par : Node_Id := Parent (Parent_P);
8742 begin
8743 while Present (Par)
8744 and then
8745 Nkind (Par) /= N_Handled_Sequence_Of_Statements
8746 and then Nkind (Parent (Par)) /= N_Subprogram_Body
8747 loop
8748 Par := Parent (Par);
8749 end loop;
8751 -- If we don't have a parent, then we are not in a
8752 -- well-formed tree and we ignore the freeze request.
8753 -- See previous comment in the enclosing loop.
8755 if No (Par) then
8756 return;
8757 end if;
8759 exit when not In_Expanded_Body (Par)
8760 or else Declared_In_Expanded_Body (Par, Typ, Nam);
8761 end;
8762 end if;
8764 -- The freeze nodes produced by an expression coming from the
8765 -- Actions list of an N_Expression_With_Actions, short-circuit
8766 -- expression or N_Case_Expression_Alternative node must remain
8767 -- within the Actions list if they freeze an entity declared in
8768 -- this list, as inserting the freeze nodes further up the tree
8769 -- may lead to use before declaration issues for the entity.
8771 when N_Case_Expression_Alternative
8772 | N_Expression_With_Actions
8773 | N_Short_Circuit
8775 exit when (Present (Nam)
8776 and then
8777 Has_Decl_In_List (Nam, P, Actions (Parent_P)))
8778 or else (Present (Typ)
8779 and then
8780 Has_Decl_In_List (Typ, P, Actions (Parent_P)));
8782 -- Likewise for an N_If_Expression and its two Actions list
8784 when N_If_Expression =>
8785 declare
8786 L1 : constant List_Id := Then_Actions (Parent_P);
8787 L2 : constant List_Id := Else_Actions (Parent_P);
8789 begin
8790 exit when (Present (Nam)
8791 and then
8792 Has_Decl_In_List (Nam, P, L1))
8793 or else (Present (Typ)
8794 and then
8795 Has_Decl_In_List (Typ, P, L1))
8796 or else (Present (Nam)
8797 and then
8798 Has_Decl_In_List (Nam, P, L2))
8799 or else (Present (Typ)
8800 and then
8801 Has_Decl_In_List (Typ, P, L2));
8802 end;
8804 -- N_Loop_Statement is a special case: a type that appears in
8805 -- the source can never be frozen in a loop (this occurs only
8806 -- because of a loop expanded by the expander), so we keep on
8807 -- going. Otherwise we terminate the search. Same is true of
8808 -- any entity which comes from source (if it has a predefined
8809 -- type, this type does not appear to come from source, but the
8810 -- entity should not be frozen here).
8812 when N_Loop_Statement =>
8813 exit when not Comes_From_Source (Etype (N))
8814 and then (No (Nam) or else not Comes_From_Source (Nam));
8816 -- For all other cases, keep looking at parents
8818 when others =>
8819 null;
8820 end case;
8822 -- We fall through the case if we did not yet find the proper
8823 -- place in the tree for inserting the freeze node, so climb.
8825 P := Parent_P;
8826 end loop;
8827 end if;
8829 -- If the expression appears in a record or an initialization procedure,
8830 -- the freeze nodes are collected and attached to the current scope, to
8831 -- be inserted and analyzed on exit from the scope, to insure that
8832 -- generated entities appear in the correct scope. If the expression is
8833 -- a default for a discriminant specification, the scope is still void.
8834 -- The expression can also appear in the discriminant part of a private
8835 -- or concurrent type.
8837 -- If the expression appears in a constrained subcomponent of an
8838 -- enclosing record declaration, the freeze nodes must be attached to
8839 -- the outer record type so they can eventually be placed in the
8840 -- enclosing declaration list.
8842 -- The other case requiring this special handling is if we are in a
8843 -- default expression, since in that case we are about to freeze a
8844 -- static type, and the freeze scope needs to be the outer scope, not
8845 -- the scope of the subprogram with the default parameter.
8847 -- For default expressions and other spec expressions in generic units,
8848 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
8849 -- placing them at the proper place, after the generic unit.
8851 if (In_Spec_Exp and not Inside_A_Generic)
8852 or else (Is_Type (Current_Scope)
8853 and then (not Is_Concurrent_Type (Current_Scope)
8854 or else not Has_Completion (Current_Scope)))
8855 or else Ekind (Current_Scope) = E_Void
8856 then
8857 declare
8858 Freeze_Nodes : List_Id := No_List;
8859 Pos : Int := Scope_Stack.Last;
8861 begin
8862 if Present (Desig_Typ) then
8863 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
8864 end if;
8866 if Present (Typ) then
8867 Freeze_And_Append (Typ, N, Freeze_Nodes);
8868 end if;
8870 if Present (Nam) then
8871 Freeze_And_Append (Nam, N, Freeze_Nodes);
8872 end if;
8874 -- The current scope may be that of a constrained component of
8875 -- an enclosing record declaration, or a block of an enclosing
8876 -- declare expression in Ada 2022, or of a loop of an enclosing
8877 -- quantified expression or aggregate with an iterated component
8878 -- in Ada 2022, which is above the current scope in the scope
8879 -- stack. Indeed in the context of a quantified expression or
8880 -- an aggregate with an iterated component, an internal scope is
8881 -- created and pushed above the current scope in order to emulate
8882 -- the loop-like behavior of the construct.
8883 -- If the expression is within a top-level pragma, as for a pre-
8884 -- condition on a library-level subprogram, nothing to do.
8886 if not Is_Compilation_Unit (Current_Scope)
8887 and then (Is_Record_Type (Scope (Current_Scope))
8888 or else (Ekind (Current_Scope) in E_Block | E_Loop
8889 and then Is_Internal (Current_Scope)))
8890 then
8891 Pos := Pos - 1;
8892 end if;
8894 if Is_Non_Empty_List (Freeze_Nodes) then
8896 -- When the current scope is transient, insert the freeze nodes
8897 -- prior to the expression that produced them. Transient scopes
8898 -- may create additional declarations when finalizing objects
8899 -- or managing the secondary stack. Inserting the freeze nodes
8900 -- of those constructs prior to the scope would result in a
8901 -- freeze-before-declaration, therefore the freeze node must
8902 -- remain interleaved with their constructs.
8904 if Scope_Is_Transient then
8905 Insert_Actions (N, Freeze_Nodes);
8907 elsif No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
8908 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
8909 Freeze_Nodes;
8910 else
8911 Append_List (Freeze_Nodes,
8912 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
8913 end if;
8914 end if;
8915 end;
8917 return;
8918 end if;
8920 -- Now we have the right place to do the freezing. First, a special
8921 -- adjustment, if we are in spec-expression analysis mode, these freeze
8922 -- actions must not be thrown away (normally all inserted actions are
8923 -- thrown away in this mode). However, the freeze actions are from
8924 -- static expressions and one of the important reasons we are doing this
8925 -- special analysis is to get these freeze actions. Therefore we turn
8926 -- off the In_Spec_Expression mode to propagate these freeze actions.
8927 -- This also means they get properly analyzed and expanded.
8929 In_Spec_Expression := False;
8931 -- Freeze the subtype mark before a qualified expression on an
8932 -- allocator as per AARM 13.14(4.a). This is needed in particular to
8933 -- generate predicate functions.
8935 if Present (Allocator_Typ) then
8936 Freeze_Before (P, Allocator_Typ);
8937 end if;
8939 -- Freeze the designated type of an allocator (RM 13.14(13))
8941 if Present (Desig_Typ) then
8942 Freeze_Before (P, Desig_Typ);
8943 end if;
8945 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
8946 -- the enumeration representation clause exception in the loop above.
8948 if Present (Typ) then
8949 Freeze_Before (P, Typ);
8950 end if;
8952 -- Freeze name if one is present (RM 13.14(11))
8954 if Present (Nam) then
8955 Freeze_Before (P, Nam);
8956 end if;
8958 -- Restore In_Spec_Expression flag
8960 In_Spec_Expression := In_Spec_Exp;
8961 end Freeze_Expression;
8963 -----------------------
8964 -- Freeze_Expr_Types --
8965 -----------------------
8967 procedure Freeze_Expr_Types
8968 (Def_Id : Entity_Id;
8969 Typ : Entity_Id;
8970 Expr : Node_Id;
8971 N : Node_Id)
8973 function Cloned_Expression return Node_Id;
8974 -- Build a duplicate of the expression of the return statement that has
8975 -- no defining entities shared with the original expression.
8977 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result;
8978 -- Freeze all types referenced in the subtree rooted at Node
8980 -----------------------
8981 -- Cloned_Expression --
8982 -----------------------
8984 function Cloned_Expression return Node_Id is
8985 function Clone_Id (Node : Node_Id) return Traverse_Result;
8986 -- Tree traversal routine that clones the defining identifier of
8987 -- iterator and loop parameter specification nodes.
8989 --------------
8990 -- Clone_Id --
8991 --------------
8993 function Clone_Id (Node : Node_Id) return Traverse_Result is
8994 begin
8995 if Nkind (Node) in
8996 N_Iterator_Specification | N_Loop_Parameter_Specification
8997 then
8998 Set_Defining_Identifier
8999 (Node, New_Copy (Defining_Identifier (Node)));
9000 end if;
9002 return OK;
9003 end Clone_Id;
9005 procedure Clone_Def_Ids is new Traverse_Proc (Clone_Id);
9007 -- Local variable
9009 Dup_Expr : constant Node_Id := New_Copy_Tree (Expr);
9011 -- Start of processing for Cloned_Expression
9013 begin
9014 -- We must duplicate the expression with semantic information to
9015 -- inherit the decoration of global entities in generic instances.
9016 -- Set the parent of the new node to be the parent of the original
9017 -- to get the proper context, which is needed for complete error
9018 -- reporting and for semantic analysis.
9020 Set_Parent (Dup_Expr, Parent (Expr));
9022 -- Replace the defining identifier of iterators and loop param
9023 -- specifications by a clone to ensure that the cloned expression
9024 -- and the original expression don't have shared identifiers;
9025 -- otherwise, as part of the preanalysis of the expression, these
9026 -- shared identifiers may be left decorated with itypes which
9027 -- will not be available in the tree passed to the backend.
9029 Clone_Def_Ids (Dup_Expr);
9031 return Dup_Expr;
9032 end Cloned_Expression;
9034 ----------------------
9035 -- Freeze_Type_Refs --
9036 ----------------------
9038 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result is
9039 procedure Check_And_Freeze_Type (Typ : Entity_Id);
9040 -- Check that Typ is fully declared and freeze it if so
9042 ---------------------------
9043 -- Check_And_Freeze_Type --
9044 ---------------------------
9046 procedure Check_And_Freeze_Type (Typ : Entity_Id) is
9047 begin
9048 -- Skip Itypes created by the preanalysis, and itypes whose
9049 -- scope is another type (i.e. component subtypes that depend
9050 -- on a discriminant),
9052 if Is_Itype (Typ)
9053 and then (Scope_Within_Or_Same (Scope (Typ), Def_Id)
9054 or else Is_Type (Scope (Typ)))
9055 then
9056 return;
9057 end if;
9059 -- This provides a better error message than generating primitives
9060 -- whose compilation fails much later. Refine the error message if
9061 -- possible.
9063 Check_Fully_Declared (Typ, Node);
9065 if Error_Posted (Node) then
9066 if Has_Private_Component (Typ)
9067 and then not Is_Private_Type (Typ)
9068 then
9069 Error_Msg_NE ("\type& has private component", Node, Typ);
9070 end if;
9072 else
9073 Freeze_Before (N, Typ);
9074 end if;
9075 end Check_And_Freeze_Type;
9077 -- Start of processing for Freeze_Type_Refs
9079 begin
9080 -- Check that a type referenced by an entity can be frozen
9082 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
9083 -- The entity itself may be a type, as in a membership test
9084 -- or an attribute reference. Freezing its own type would be
9085 -- incomplete if the entity is derived or an extension.
9087 if Is_Type (Entity (Node)) then
9088 Check_And_Freeze_Type (Entity (Node));
9090 else
9091 Check_And_Freeze_Type (Etype (Entity (Node)));
9092 end if;
9094 -- Check that the enclosing record type can be frozen
9096 if Ekind (Entity (Node)) in E_Component | E_Discriminant then
9097 Check_And_Freeze_Type (Scope (Entity (Node)));
9098 end if;
9100 -- Freezing an access type does not freeze the designated type, but
9101 -- freezing conversions between access to interfaces requires that
9102 -- the interface types themselves be frozen, so that dispatch table
9103 -- entities are properly created.
9105 -- Unclear whether a more general rule is needed ???
9107 elsif Nkind (Node) = N_Type_Conversion
9108 and then Is_Access_Type (Etype (Node))
9109 and then Is_Interface (Designated_Type (Etype (Node)))
9110 then
9111 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
9112 end if;
9114 -- An implicit dereference freezes the designated type. In the case
9115 -- of a dispatching call whose controlling argument is an access
9116 -- type, the dereference is not made explicit, so we must check for
9117 -- such a call and freeze the designated type.
9119 if Nkind (Node) in N_Has_Etype
9120 and then Present (Etype (Node))
9121 and then Is_Access_Type (Etype (Node))
9122 then
9123 if Nkind (Parent (Node)) = N_Function_Call
9124 and then Node = Controlling_Argument (Parent (Node))
9125 then
9126 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
9128 -- An explicit dereference freezes the designated type as well,
9129 -- even though that type is not attached to an entity in the
9130 -- expression.
9132 elsif Nkind (Parent (Node)) = N_Explicit_Dereference then
9133 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
9134 end if;
9136 -- An iterator specification freezes the iterator type, even though
9137 -- that type is not attached to an entity in the construct.
9139 elsif Nkind (Node) in N_Has_Etype
9140 and then Nkind (Parent (Node)) = N_Iterator_Specification
9141 and then Node = Name (Parent (Node))
9142 then
9143 declare
9144 Iter : constant Node_Id :=
9145 Find_Value_Of_Aspect (Etype (Node), Aspect_Default_Iterator);
9147 begin
9148 if Present (Iter) then
9149 Check_And_Freeze_Type (Etype (Iter));
9150 end if;
9151 end;
9152 end if;
9154 -- No point in posting several errors on the same expression
9156 if Serious_Errors_Detected > 0 then
9157 return Abandon;
9158 else
9159 return OK;
9160 end if;
9161 end Freeze_Type_Refs;
9163 procedure Freeze_References is new Traverse_Proc (Freeze_Type_Refs);
9165 -- Local variables
9167 Saved_First_Entity : constant Entity_Id := First_Entity (Def_Id);
9168 Saved_Last_Entity : constant Entity_Id := Last_Entity (Def_Id);
9169 Dup_Expr : constant Node_Id := Cloned_Expression;
9171 -- Start of processing for Freeze_Expr_Types
9173 begin
9174 -- Preanalyze a duplicate of the expression to have available the
9175 -- minimum decoration needed to locate referenced unfrozen types
9176 -- without adding any decoration to the function expression.
9178 -- This routine is also applied to expressions in the contract for
9179 -- the subprogram. If that happens when expanding the code for
9180 -- pre/postconditions during expansion of the subprogram body, the
9181 -- subprogram is already installed.
9183 if Def_Id /= Current_Scope then
9184 Push_Scope (Def_Id);
9185 Install_Formals (Def_Id);
9187 Preanalyze_Spec_Expression (Dup_Expr, Typ);
9188 End_Scope;
9189 else
9190 Preanalyze_Spec_Expression (Dup_Expr, Typ);
9191 end if;
9193 -- Restore certain attributes of Def_Id since the preanalysis may
9194 -- have introduced itypes to this scope, thus modifying attributes
9195 -- First_Entity and Last_Entity.
9197 Set_First_Entity (Def_Id, Saved_First_Entity);
9198 Set_Last_Entity (Def_Id, Saved_Last_Entity);
9200 if Present (Last_Entity (Def_Id)) then
9201 Set_Next_Entity (Last_Entity (Def_Id), Empty);
9202 end if;
9204 -- Freeze all types referenced in the expression
9206 Freeze_References (Dup_Expr);
9207 end Freeze_Expr_Types;
9209 -----------------------------
9210 -- Freeze_Fixed_Point_Type --
9211 -----------------------------
9213 -- Certain fixed-point types and subtypes, including implicit base types
9214 -- and declared first subtypes, have not yet set up a range. This is
9215 -- because the range cannot be set until the Small and Size values are
9216 -- known, and these are not known till the type is frozen.
9218 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
9219 -- whose bounds are unanalyzed real literals. This routine will recognize
9220 -- this case, and transform this range node into a properly typed range
9221 -- with properly analyzed and resolved values.
9223 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
9224 Rng : constant Node_Id := Scalar_Range (Typ);
9225 Lo : constant Node_Id := Low_Bound (Rng);
9226 Hi : constant Node_Id := High_Bound (Rng);
9227 Btyp : constant Entity_Id := Base_Type (Typ);
9228 Brng : constant Node_Id := Scalar_Range (Btyp);
9229 BLo : constant Node_Id := Low_Bound (Brng);
9230 BHi : constant Node_Id := High_Bound (Brng);
9231 Ftyp : constant Entity_Id := Underlying_Type (First_Subtype (Typ));
9233 Small : Ureal;
9234 Loval : Ureal;
9235 Hival : Ureal;
9236 Atype : Entity_Id;
9238 Orig_Lo : Ureal;
9239 Orig_Hi : Ureal;
9240 -- Save original bounds (for shaving tests)
9242 Actual_Size : Int;
9243 -- Actual size chosen
9245 function Fsize (Lov, Hiv : Ureal) return Int;
9246 -- Returns size of type with given bounds. Also leaves these
9247 -- bounds set as the current bounds of the Typ.
9249 function Larger (A, B : Ureal) return Boolean;
9250 -- Returns true if A > B with a margin of Typ'Small
9252 function Smaller (A, B : Ureal) return Boolean;
9253 -- Returns true if A < B with a margin of Typ'Small
9255 -----------
9256 -- Fsize --
9257 -----------
9259 function Fsize (Lov, Hiv : Ureal) return Int is
9260 begin
9261 Set_Realval (Lo, Lov);
9262 Set_Realval (Hi, Hiv);
9263 return Minimum_Size (Typ);
9264 end Fsize;
9266 ------------
9267 -- Larger --
9268 ------------
9270 function Larger (A, B : Ureal) return Boolean is
9271 begin
9272 return A > B and then A - Small_Value (Typ) > B;
9273 end Larger;
9275 -------------
9276 -- Smaller --
9277 -------------
9279 function Smaller (A, B : Ureal) return Boolean is
9280 begin
9281 return A < B and then A + Small_Value (Typ) < B;
9282 end Smaller;
9284 -- Start of processing for Freeze_Fixed_Point_Type
9286 begin
9287 -- The type, or its first subtype if we are freezing the anonymous
9288 -- base, may have a delayed Small aspect. It must be analyzed now,
9289 -- so that all characteristics of the type (size, bounds) can be
9290 -- computed and validated in the call to Minimum_Size that follows.
9292 if Has_Delayed_Aspects (Ftyp) then
9293 Analyze_Aspects_At_Freeze_Point (Ftyp);
9294 Set_Has_Delayed_Aspects (Ftyp, False);
9295 end if;
9297 if May_Inherit_Delayed_Rep_Aspects (Ftyp) then
9298 Inherit_Delayed_Rep_Aspects (Ftyp);
9299 Set_May_Inherit_Delayed_Rep_Aspects (Ftyp, False);
9300 end if;
9302 -- Inherit the Small value from the first subtype in any case
9304 if Typ /= Ftyp then
9305 Set_Small_Value (Typ, Small_Value (Ftyp));
9306 end if;
9308 -- If Esize of a subtype has not previously been set, set it now
9310 if not Known_Esize (Typ) then
9311 Atype := Ancestor_Subtype (Typ);
9313 if Present (Atype) then
9314 Set_Esize (Typ, Esize (Atype));
9315 else
9316 Copy_Esize (To => Typ, From => Btyp);
9317 end if;
9318 end if;
9320 -- Immediate return if the range is already analyzed. This means that
9321 -- the range is already set, and does not need to be computed by this
9322 -- routine.
9324 if Analyzed (Rng) then
9325 return;
9326 end if;
9328 -- Immediate return if either of the bounds raises Constraint_Error
9330 if Raises_Constraint_Error (Lo)
9331 or else Raises_Constraint_Error (Hi)
9332 then
9333 return;
9334 end if;
9336 Small := Small_Value (Typ);
9337 Loval := Realval (Lo);
9338 Hival := Realval (Hi);
9340 Orig_Lo := Loval;
9341 Orig_Hi := Hival;
9343 -- Ordinary fixed-point case
9345 if Is_Ordinary_Fixed_Point_Type (Typ) then
9347 -- For the ordinary fixed-point case, we are allowed to fudge the
9348 -- end-points up or down by small. Generally we prefer to fudge up,
9349 -- i.e. widen the bounds for non-model numbers so that the end points
9350 -- are included. However there are cases in which this cannot be
9351 -- done, and indeed cases in which we may need to narrow the bounds.
9352 -- The following circuit makes the decision.
9354 -- Note: our terminology here is that Incl_EP means that the bounds
9355 -- are widened by Small if necessary to include the end points, and
9356 -- Excl_EP means that the bounds are narrowed by Small to exclude the
9357 -- end-points if this reduces the size.
9359 -- Note that in the Incl case, all we care about is including the
9360 -- end-points. In the Excl case, we want to narrow the bounds as
9361 -- much as permitted by the RM, to give the smallest possible size.
9363 Fudge : declare
9364 Loval_Incl_EP : Ureal;
9365 Hival_Incl_EP : Ureal;
9367 Loval_Excl_EP : Ureal;
9368 Hival_Excl_EP : Ureal;
9370 Size_Incl_EP : Int;
9371 Size_Excl_EP : Int;
9373 Model_Num : Ureal;
9374 Actual_Lo : Ureal;
9375 Actual_Hi : Ureal;
9377 begin
9378 -- First step. Base types are required to be symmetrical. Right
9379 -- now, the base type range is a copy of the first subtype range.
9380 -- This will be corrected before we are done, but right away we
9381 -- need to deal with the case where both bounds are non-negative.
9382 -- In this case, we set the low bound to the negative of the high
9383 -- bound, to make sure that the size is computed to include the
9384 -- required sign. Note that we do not need to worry about the
9385 -- case of both bounds negative, because the sign will be dealt
9386 -- with anyway. Furthermore we can't just go making such a bound
9387 -- symmetrical, since in a twos-complement system, there is an
9388 -- extra negative value which could not be accommodated on the
9389 -- positive side.
9391 if Typ = Btyp
9392 and then not UR_Is_Negative (Loval)
9393 and then Hival > Loval
9394 then
9395 Loval := -Hival;
9396 Set_Realval (Lo, Loval);
9397 end if;
9399 -- Compute the fudged bounds. If the bound is a model number, (or
9400 -- greater if given low bound, smaller if high bound) then we do
9401 -- nothing to include it, but we are allowed to backoff to the
9402 -- next adjacent model number when we exclude it. If it is not a
9403 -- model number then we straddle the two values with the model
9404 -- numbers on either side.
9406 Model_Num := UR_Trunc (Loval / Small) * Small;
9408 if UR_Ge (Loval, Model_Num) then
9409 Loval_Incl_EP := Model_Num;
9410 else
9411 Loval_Incl_EP := Model_Num - Small;
9412 end if;
9414 -- The low value excluding the end point is Small greater, but
9415 -- we do not do this exclusion if the low value is positive,
9416 -- since it can't help the size and could actually hurt by
9417 -- crossing the high bound.
9419 if UR_Is_Negative (Loval_Incl_EP) then
9420 Loval_Excl_EP := Loval_Incl_EP + Small;
9422 -- If the value went from negative to zero, then we have the
9423 -- case where Loval_Incl_EP is the model number just below
9424 -- zero, so we want to stick to the negative value for the
9425 -- base type to maintain the condition that the size will
9426 -- include signed values.
9428 if Typ = Btyp
9429 and then UR_Is_Zero (Loval_Excl_EP)
9430 then
9431 Loval_Excl_EP := Loval_Incl_EP;
9432 end if;
9434 else
9435 Loval_Excl_EP := Loval_Incl_EP;
9436 end if;
9438 -- Similar processing for upper bound and high value
9440 Model_Num := UR_Trunc (Hival / Small) * Small;
9442 if UR_Le (Hival, Model_Num) then
9443 Hival_Incl_EP := Model_Num;
9444 else
9445 Hival_Incl_EP := Model_Num + Small;
9446 end if;
9448 if UR_Is_Positive (Hival_Incl_EP) then
9449 Hival_Excl_EP := Hival_Incl_EP - Small;
9450 else
9451 Hival_Excl_EP := Hival_Incl_EP;
9452 end if;
9454 -- One further adjustment is needed. In the case of subtypes, we
9455 -- cannot go outside the range of the base type, or we get
9456 -- peculiarities, and the base type range is already set. This
9457 -- only applies to the Incl values, since clearly the Excl values
9458 -- are already as restricted as they are allowed to be.
9460 if Typ /= Btyp then
9461 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
9462 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
9463 end if;
9465 -- Get size including and excluding end points
9467 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
9468 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
9470 -- No need to exclude end-points if it does not reduce size
9472 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
9473 Loval_Excl_EP := Loval_Incl_EP;
9474 end if;
9476 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
9477 Hival_Excl_EP := Hival_Incl_EP;
9478 end if;
9480 -- Now we set the actual size to be used. We want to use the
9481 -- bounds fudged up to include the end-points but only if this
9482 -- can be done without violating a specifically given size
9483 -- size clause or causing an unacceptable increase in size.
9485 -- Case of size clause given
9487 if Has_Size_Clause (Typ) then
9489 -- Use the inclusive size only if it is consistent with
9490 -- the explicitly specified size.
9492 if Size_Incl_EP <= RM_Size (Typ) then
9493 Actual_Lo := Loval_Incl_EP;
9494 Actual_Hi := Hival_Incl_EP;
9495 Actual_Size := Size_Incl_EP;
9497 -- If the inclusive size is too large, we try excluding
9498 -- the end-points (will be caught later if does not work).
9500 else
9501 Actual_Lo := Loval_Excl_EP;
9502 Actual_Hi := Hival_Excl_EP;
9503 Actual_Size := Size_Excl_EP;
9504 end if;
9506 -- Case of size clause not given
9508 else
9509 -- If we have a base type whose corresponding first subtype
9510 -- has an explicit size that is large enough to include our
9511 -- end-points, then do so. There is no point in working hard
9512 -- to get a base type whose size is smaller than the specified
9513 -- size of the first subtype.
9515 if Has_Size_Clause (Ftyp)
9516 and then Size_Incl_EP <= Esize (Ftyp)
9517 then
9518 Actual_Size := Size_Incl_EP;
9519 Actual_Lo := Loval_Incl_EP;
9520 Actual_Hi := Hival_Incl_EP;
9522 -- If excluding the end-points makes the size smaller and
9523 -- results in a size of 8,16,32,64, then we take the smaller
9524 -- size. For the 64 case, this is compulsory. For the other
9525 -- cases, it seems reasonable. We like to include end points
9526 -- if we can, but not at the expense of moving to the next
9527 -- natural boundary of size.
9529 elsif Size_Incl_EP /= Size_Excl_EP
9530 and then Addressable (Size_Excl_EP)
9531 then
9532 Actual_Size := Size_Excl_EP;
9533 Actual_Lo := Loval_Excl_EP;
9534 Actual_Hi := Hival_Excl_EP;
9536 -- Otherwise we can definitely include the end points
9538 else
9539 Actual_Size := Size_Incl_EP;
9540 Actual_Lo := Loval_Incl_EP;
9541 Actual_Hi := Hival_Incl_EP;
9542 end if;
9544 -- One pathological case: normally we never fudge a low bound
9545 -- down, since it would seem to increase the size (if it has
9546 -- any effect), but for ranges containing single value, or no
9547 -- values, the high bound can be small too large. Consider:
9549 -- type t is delta 2.0**(-14)
9550 -- range 131072.0 .. 0;
9552 -- That lower bound is *just* outside the range of 32 bits, and
9553 -- does need fudging down in this case. Note that the bounds
9554 -- will always have crossed here, since the high bound will be
9555 -- fudged down if necessary, as in the case of:
9557 -- type t is delta 2.0**(-14)
9558 -- range 131072.0 .. 131072.0;
9560 -- So we detect the situation by looking for crossed bounds,
9561 -- and if the bounds are crossed, and the low bound is greater
9562 -- than zero, we will always back it off by small, since this
9563 -- is completely harmless.
9565 if Actual_Lo > Actual_Hi then
9566 if UR_Is_Positive (Actual_Lo) then
9567 Actual_Lo := Loval_Incl_EP - Small;
9568 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
9570 -- And of course, we need to do exactly the same parallel
9571 -- fudge for flat ranges in the negative region.
9573 elsif UR_Is_Negative (Actual_Hi) then
9574 Actual_Hi := Hival_Incl_EP + Small;
9575 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
9576 end if;
9577 end if;
9578 end if;
9580 Set_Realval (Lo, Actual_Lo);
9581 Set_Realval (Hi, Actual_Hi);
9582 end Fudge;
9584 -- Enforce some limitations for ordinary fixed-point types. They come
9585 -- from an exact algorithm used to implement Text_IO.Fixed_IO and the
9586 -- Fore, Image and Value attributes. The requirement on the Small is
9587 -- to lie in the range 2**(-(Siz - 1)) .. 2**(Siz - 1) for a type of
9588 -- Siz bits (Siz=32,64,128) and the requirement on the bounds is to
9589 -- be smaller in magnitude than 10.0**N * 2**(Siz - 1), where N is
9590 -- given by the formula N = floor ((Siz - 1) * log 2 / log 10).
9592 -- If the bounds of a 32-bit type are too large, force 64-bit type
9594 if Actual_Size <= 32
9595 and then Small <= Ureal_2_31
9596 and then (Smaller (Expr_Value_R (Lo), Ureal_M_2_10_18)
9597 or else Larger (Expr_Value_R (Hi), Ureal_2_10_18))
9598 then
9599 Actual_Size := 33;
9600 end if;
9602 -- If the bounds of a 64-bit type are too large, force 128-bit type
9604 if System_Max_Integer_Size = 128
9605 and then Actual_Size <= 64
9606 and then Small <= Ureal_2_63
9607 and then (Smaller (Expr_Value_R (Lo), Ureal_M_9_10_36)
9608 or else Larger (Expr_Value_R (Hi), Ureal_9_10_36))
9609 then
9610 Actual_Size := 65;
9611 end if;
9613 -- Give error messages for first subtypes and not base types, as the
9614 -- bounds of base types are always maximum for their size, see below.
9616 if System_Max_Integer_Size < 128 and then Typ /= Btyp then
9618 -- See the 128-bit case below for the reason why we cannot test
9619 -- against the 2**(-63) .. 2**63 range. This quirk should have
9620 -- been kludged around as in the 128-bit case below, but it was
9621 -- not and we end up with a ludicrous range as a result???
9623 if Small < Ureal_2_M_80 then
9624 Error_Msg_Name_1 := Name_Small;
9625 Error_Msg_N
9626 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", Typ);
9628 elsif Small > Ureal_2_80 then
9629 Error_Msg_Name_1 := Name_Small;
9630 Error_Msg_N
9631 ("`&''%` too large, maximum allowed is 2.0'*'*80", Typ);
9632 end if;
9634 if Smaller (Expr_Value_R (Lo), Ureal_M_9_10_36) then
9635 Error_Msg_Name_1 := Name_First;
9636 Error_Msg_N
9637 ("`&''%` too small, minimum allowed is -9.0E+36", Typ);
9638 end if;
9640 if Larger (Expr_Value_R (Hi), Ureal_9_10_36) then
9641 Error_Msg_Name_1 := Name_Last;
9642 Error_Msg_N
9643 ("`&''%` too large, maximum allowed is 9.0E+36", Typ);
9644 end if;
9646 elsif System_Max_Integer_Size = 128 and then Typ /= Btyp then
9648 -- ACATS c35902d tests a delta equal to 2**(-(Max_Mantissa + 1))
9649 -- but we cannot really support anything smaller than Fine_Delta
9650 -- because of the way we implement I/O for fixed point types???
9652 if Small = Ureal_2_M_128 then
9653 null;
9655 elsif Small < Ureal_2_M_127 then
9656 Error_Msg_Name_1 := Name_Small;
9657 Error_Msg_N
9658 ("`&''%` too small, minimum allowed is 2.0'*'*(-127)", Typ);
9660 elsif Small > Ureal_2_127 then
9661 Error_Msg_Name_1 := Name_Small;
9662 Error_Msg_N
9663 ("`&''%` too large, maximum allowed is 2.0'*'*127", Typ);
9664 end if;
9666 if Actual_Size > 64
9667 and then (Norm_Num (Small) > Uint_2 ** 127
9668 or else Norm_Den (Small) > Uint_2 ** 127)
9669 and then Small /= Ureal_2_M_128
9670 then
9671 Error_Msg_Name_1 := Name_Small;
9672 Error_Msg_N
9673 ("`&''%` not the ratio of two 128-bit integers", Typ);
9674 end if;
9676 if Smaller (Expr_Value_R (Lo), Ureal_M_10_76) then
9677 Error_Msg_Name_1 := Name_First;
9678 Error_Msg_N
9679 ("`&''%` too small, minimum allowed is -1.0E+76", Typ);
9680 end if;
9682 if Larger (Expr_Value_R (Hi), Ureal_10_76) then
9683 Error_Msg_Name_1 := Name_Last;
9684 Error_Msg_N
9685 ("`&''%` too large, maximum allowed is 1.0E+76", Typ);
9686 end if;
9687 end if;
9689 -- For the decimal case, none of this fudging is required, since there
9690 -- are no end-point problems in the decimal case (the end-points are
9691 -- always included).
9693 else
9694 Actual_Size := Fsize (Loval, Hival);
9695 end if;
9697 -- At this stage, the actual size has been calculated and the proper
9698 -- required bounds are stored in the low and high bounds.
9700 if Actual_Size > System_Max_Integer_Size then
9701 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
9702 Error_Msg_Uint_2 := UI_From_Int (System_Max_Integer_Size);
9703 Error_Msg_N
9704 ("size required (^) for type& too large, maximum allowed is ^",
9705 Typ);
9706 Actual_Size := System_Max_Integer_Size;
9707 end if;
9709 -- Check size against explicit given size
9711 if Has_Size_Clause (Typ) then
9712 if Actual_Size > RM_Size (Typ) then
9713 Error_Msg_Uint_1 := RM_Size (Typ);
9714 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
9715 Error_Msg_NE
9716 ("size given (^) for type& too small, minimum allowed is ^",
9717 Size_Clause (Typ), Typ);
9719 else
9720 Actual_Size := UI_To_Int (Esize (Typ));
9721 end if;
9723 -- Increase size to next natural boundary if no size clause given
9725 else
9726 if Actual_Size <= 8 then
9727 Actual_Size := 8;
9728 elsif Actual_Size <= 16 then
9729 Actual_Size := 16;
9730 elsif Actual_Size <= 32 then
9731 Actual_Size := 32;
9732 elsif Actual_Size <= 64 then
9733 Actual_Size := 64;
9734 else
9735 Actual_Size := 128;
9736 end if;
9738 Set_Esize (Typ, UI_From_Int (Actual_Size));
9739 Adjust_Esize_For_Alignment (Typ);
9740 end if;
9742 -- If we have a base type, then expand the bounds so that they extend to
9743 -- the full width of the allocated size in bits, to avoid junk range
9744 -- checks on intermediate computations.
9746 if Typ = Btyp then
9747 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
9748 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
9749 end if;
9751 -- Final step is to reanalyze the bounds using the proper type
9752 -- and set the Corresponding_Integer_Value fields of the literals.
9754 Set_Etype (Lo, Empty);
9755 Set_Analyzed (Lo, False);
9756 Analyze (Lo);
9758 -- Resolve with universal fixed if the base type, and with the base
9759 -- type if we are freezing a subtype. Note we can't resolve the base
9760 -- type with itself, that would be a reference before definition.
9761 -- The resolution of the bounds of a subtype, if they are given by real
9762 -- literals, includes the setting of the Corresponding_Integer_Value,
9763 -- as for other literals of a fixed-point type.
9765 if Typ = Btyp then
9766 Resolve (Lo, Universal_Fixed);
9767 Set_Corresponding_Integer_Value
9768 (Lo, UR_To_Uint (Realval (Lo) / Small));
9769 else
9770 Resolve (Lo, Btyp);
9771 end if;
9773 -- Similar processing for high bound
9775 Set_Etype (Hi, Empty);
9776 Set_Analyzed (Hi, False);
9777 Analyze (Hi);
9779 if Typ = Btyp then
9780 Resolve (Hi, Universal_Fixed);
9781 Set_Corresponding_Integer_Value
9782 (Hi, UR_To_Uint (Realval (Hi) / Small));
9783 else
9784 Resolve (Hi, Btyp);
9785 end if;
9787 -- Set type of range to correspond to bounds
9789 Set_Etype (Rng, Etype (Lo));
9791 -- Set Esize to calculated size if not set already
9793 if not Known_Esize (Typ) then
9794 Set_Esize (Typ, UI_From_Int (Actual_Size));
9795 end if;
9797 -- Set RM_Size if not already set. If already set, check value
9799 declare
9800 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
9802 begin
9803 if Known_RM_Size (Typ) then
9804 if RM_Size (Typ) < Minsiz then
9805 Error_Msg_Uint_1 := RM_Size (Typ);
9806 Error_Msg_Uint_2 := Minsiz;
9807 Error_Msg_NE
9808 ("size given (^) for type& too small, minimum allowed is ^",
9809 Size_Clause (Typ), Typ);
9810 end if;
9812 else
9813 Set_RM_Size (Typ, Minsiz);
9814 end if;
9815 end;
9817 -- Check for shaving
9819 if Comes_From_Source (Typ) then
9821 -- In SPARK mode the given bounds must be strictly representable
9823 if SPARK_Mode = On then
9824 if Orig_Lo < Expr_Value_R (Lo) then
9825 Error_Msg_NE
9826 ("declared low bound of type & is outside type range",
9827 Lo, Typ);
9828 end if;
9830 if Orig_Hi > Expr_Value_R (Hi) then
9831 Error_Msg_NE
9832 ("declared high bound of type & is outside type range",
9833 Hi, Typ);
9834 end if;
9836 else
9837 if Orig_Lo < Expr_Value_R (Lo) then
9838 Error_Msg_N
9839 ("declared low bound of type & is outside type range??", Typ);
9840 Error_Msg_N
9841 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
9842 end if;
9844 if Orig_Hi > Expr_Value_R (Hi) then
9845 Error_Msg_N
9846 ("declared high bound of type & is outside type range??",
9847 Typ);
9848 Error_Msg_N
9849 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
9850 end if;
9851 end if;
9852 end if;
9853 end Freeze_Fixed_Point_Type;
9855 ------------------
9856 -- Freeze_Itype --
9857 ------------------
9859 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
9860 L : List_Id;
9862 begin
9863 Set_Has_Delayed_Freeze (T);
9864 L := Freeze_Entity (T, N);
9866 Insert_Actions (N, L);
9867 end Freeze_Itype;
9869 --------------------------
9870 -- Freeze_Static_Object --
9871 --------------------------
9873 procedure Freeze_Static_Object (E : Entity_Id) is
9875 Cannot_Be_Static : exception;
9876 -- Exception raised if the type of a static object cannot be made
9877 -- static. This happens if the type depends on non-global objects.
9879 procedure Ensure_Expression_Is_SA (N : Node_Id);
9880 -- Called to ensure that an expression used as part of a type definition
9881 -- is statically allocatable, which means that the expression type is
9882 -- statically allocatable, and the expression is either static, or a
9883 -- reference to a library level constant.
9885 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
9886 -- Called to mark a type as static, checking that it is possible
9887 -- to set the type as static. If it is not possible, then the
9888 -- exception Cannot_Be_Static is raised.
9890 -----------------------------
9891 -- Ensure_Expression_Is_SA --
9892 -----------------------------
9894 procedure Ensure_Expression_Is_SA (N : Node_Id) is
9895 Ent : Entity_Id;
9897 begin
9898 Ensure_Type_Is_SA (Etype (N));
9900 if Is_OK_Static_Expression (N) then
9901 return;
9903 elsif Nkind (N) = N_Identifier then
9904 Ent := Entity (N);
9906 if Present (Ent)
9907 and then Ekind (Ent) = E_Constant
9908 and then Is_Library_Level_Entity (Ent)
9909 then
9910 return;
9911 end if;
9912 end if;
9914 raise Cannot_Be_Static;
9915 end Ensure_Expression_Is_SA;
9917 -----------------------
9918 -- Ensure_Type_Is_SA --
9919 -----------------------
9921 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
9922 N : Node_Id;
9923 C : Entity_Id;
9925 begin
9926 -- If type is library level, we are all set
9928 if Is_Library_Level_Entity (Typ) then
9929 return;
9930 end if;
9932 -- We are also OK if the type already marked as statically allocated,
9933 -- which means we processed it before.
9935 if Is_Statically_Allocated (Typ) then
9936 return;
9937 end if;
9939 -- Mark type as statically allocated
9941 Set_Is_Statically_Allocated (Typ);
9943 -- Check that it is safe to statically allocate this type
9945 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
9946 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
9947 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
9949 elsif Is_Array_Type (Typ) then
9950 N := First_Index (Typ);
9951 while Present (N) loop
9952 Ensure_Type_Is_SA (Etype (N));
9953 Next_Index (N);
9954 end loop;
9956 Ensure_Type_Is_SA (Component_Type (Typ));
9958 elsif Is_Access_Type (Typ) then
9959 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
9961 declare
9962 F : Entity_Id;
9963 T : constant Entity_Id := Etype (Designated_Type (Typ));
9965 begin
9966 if T /= Standard_Void_Type then
9967 Ensure_Type_Is_SA (T);
9968 end if;
9970 F := First_Formal (Designated_Type (Typ));
9971 while Present (F) loop
9972 Ensure_Type_Is_SA (Etype (F));
9973 Next_Formal (F);
9974 end loop;
9975 end;
9977 else
9978 Ensure_Type_Is_SA (Designated_Type (Typ));
9979 end if;
9981 elsif Is_Record_Type (Typ) then
9982 C := First_Entity (Typ);
9983 while Present (C) loop
9984 if Ekind (C) = E_Discriminant
9985 or else Ekind (C) = E_Component
9986 then
9987 Ensure_Type_Is_SA (Etype (C));
9989 elsif Is_Type (C) then
9990 Ensure_Type_Is_SA (C);
9991 end if;
9993 Next_Entity (C);
9994 end loop;
9996 elsif Ekind (Typ) = E_Subprogram_Type then
9997 Ensure_Type_Is_SA (Etype (Typ));
9999 C := First_Formal (Typ);
10000 while Present (C) loop
10001 Ensure_Type_Is_SA (Etype (C));
10002 Next_Formal (C);
10003 end loop;
10005 else
10006 raise Cannot_Be_Static;
10007 end if;
10008 end Ensure_Type_Is_SA;
10010 -- Start of processing for Freeze_Static_Object
10012 begin
10013 Ensure_Type_Is_SA (Etype (E));
10015 exception
10016 when Cannot_Be_Static =>
10018 -- If the object that cannot be static is imported or exported, then
10019 -- issue an error message saying that this object cannot be imported
10020 -- or exported. If it has an address clause it is an overlay in the
10021 -- current partition and the static requirement is not relevant.
10022 -- Do not issue any error message when ignoring rep clauses.
10024 if Ignore_Rep_Clauses then
10025 null;
10027 elsif Is_Imported (E) then
10028 if No (Address_Clause (E)) then
10029 Error_Msg_N
10030 ("& cannot be imported (local type is not constant)", E);
10031 end if;
10033 -- Otherwise must be exported, something is wrong if compiler
10034 -- is marking something as statically allocated which cannot be).
10036 else pragma Assert (Is_Exported (E));
10037 Error_Msg_N
10038 ("& cannot be exported (local type is not constant)", E);
10039 end if;
10040 end Freeze_Static_Object;
10042 -----------------------
10043 -- Freeze_Subprogram --
10044 -----------------------
10046 procedure Freeze_Subprogram (E : Entity_Id) is
10048 procedure Set_Profile_Convention (Subp_Id : Entity_Id);
10049 -- Set the conventions of all anonymous access-to-subprogram formals and
10050 -- result subtype of subprogram Subp_Id to the convention of Subp_Id.
10052 ----------------------------
10053 -- Set_Profile_Convention --
10054 ----------------------------
10056 procedure Set_Profile_Convention (Subp_Id : Entity_Id) is
10057 Conv : constant Convention_Id := Convention (Subp_Id);
10059 procedure Set_Type_Convention (Typ : Entity_Id);
10060 -- Set the convention of anonymous access-to-subprogram type Typ and
10061 -- its designated type to Conv.
10063 -------------------------
10064 -- Set_Type_Convention --
10065 -------------------------
10067 procedure Set_Type_Convention (Typ : Entity_Id) is
10068 begin
10069 -- Set the convention on both the anonymous access-to-subprogram
10070 -- type and the subprogram type it points to because both types
10071 -- participate in conformance-related checks.
10073 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
10074 Set_Convention (Typ, Conv);
10075 Set_Convention (Designated_Type (Typ), Conv);
10076 end if;
10077 end Set_Type_Convention;
10079 -- Local variables
10081 Formal : Entity_Id;
10083 -- Start of processing for Set_Profile_Convention
10085 begin
10086 Formal := First_Formal (Subp_Id);
10087 while Present (Formal) loop
10088 Set_Type_Convention (Etype (Formal));
10089 Next_Formal (Formal);
10090 end loop;
10092 if Ekind (Subp_Id) = E_Function then
10093 Set_Type_Convention (Etype (Subp_Id));
10094 end if;
10095 end Set_Profile_Convention;
10097 -- Local variables
10099 F : Entity_Id;
10100 Retype : Entity_Id;
10102 -- Start of processing for Freeze_Subprogram
10104 begin
10105 -- Subprogram may not have an address clause unless it is imported
10107 if Present (Address_Clause (E)) then
10108 if not Is_Imported (E) then
10109 Error_Msg_N
10110 ("address clause can only be given for imported subprogram",
10111 Name (Address_Clause (E)));
10112 end if;
10113 end if;
10115 -- Reset the Pure indication on an imported subprogram unless an
10116 -- explicit Pure_Function pragma was present or the subprogram is an
10117 -- intrinsic. We do this because otherwise it is an insidious error
10118 -- to call a non-pure function from pure unit and have calls
10119 -- mysteriously optimized away. What happens here is that the Import
10120 -- can bypass the normal check to ensure that pure units call only pure
10121 -- subprograms.
10123 -- The reason for the intrinsic exception is that in general, intrinsic
10124 -- functions (such as shifts) are pure anyway. The only exceptions are
10125 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
10126 -- in any case, so no problem arises.
10128 if Is_Imported (E)
10129 and then Is_Pure (E)
10130 and then not Has_Pragma_Pure_Function (E)
10131 and then not Is_Intrinsic_Subprogram (E)
10132 then
10133 Set_Is_Pure (E, False);
10134 end if;
10136 -- For C++ constructors check that their external name has been given
10137 -- (either in pragma CPP_Constructor or in a pragma import).
10139 if Is_Constructor (E)
10140 and then Convention (E) = Convention_CPP
10141 and then
10142 (No (Interface_Name (E))
10143 or else String_Equal
10144 (L => Strval (Interface_Name (E)),
10145 R => Strval (Get_Default_External_Name (E))))
10146 then
10147 Error_Msg_N
10148 ("'C++ constructor must have external name or link name", E);
10149 end if;
10151 -- We also reset the Pure indication on a subprogram with an Address
10152 -- parameter, because the parameter may be used as a pointer and the
10153 -- referenced data may change even if the address value does not.
10155 -- Note that if the programmer gave an explicit Pure_Function pragma,
10156 -- then we believe the programmer, and leave the subprogram Pure. We
10157 -- also suppress this check on run-time files.
10159 if Is_Pure (E)
10160 and then Is_Subprogram (E)
10161 and then not Has_Pragma_Pure_Function (E)
10162 and then not Is_Internal_Unit (Current_Sem_Unit)
10163 then
10164 Check_Function_With_Address_Parameter (E);
10165 end if;
10167 -- Ensure that all anonymous access-to-subprogram types inherit the
10168 -- convention of their related subprogram (RM 6.3.1(13.1/5)). This is
10169 -- not done for a defaulted convention Ada because those types also
10170 -- default to Ada. Convention Protected must not be propagated when
10171 -- the subprogram is an entry because this would be illegal. The only
10172 -- way to force convention Protected on these kinds of types is to
10173 -- include keyword "protected" in the access definition. Conventions
10174 -- Entry and Intrinsic are also not propagated (specified by AI12-0207).
10176 if Convention (E) /= Convention_Ada
10177 and then Convention (E) /= Convention_Protected
10178 and then Convention (E) /= Convention_Entry
10179 and then Convention (E) /= Convention_Intrinsic
10180 then
10181 Set_Profile_Convention (E);
10182 end if;
10184 -- For non-foreign convention subprograms, this is where we create
10185 -- the extra formals (for accessibility level and constrained bit
10186 -- information). We delay this till the freeze point precisely so
10187 -- that we know the convention.
10189 if not Has_Foreign_Convention (E) then
10191 -- Extra formals of dispatching operations are added later by
10192 -- Expand_Freeze_Record_Type, which also adds extra formals to
10193 -- internal entities built to handle interface types.
10195 if not Is_Dispatching_Operation (E) then
10196 Create_Extra_Formals (E);
10198 pragma Assert
10199 ((Ekind (E) = E_Subprogram_Type
10200 and then Extra_Formals_OK (E))
10201 or else
10202 (Is_Subprogram (E)
10203 and then Extra_Formals_OK (E)
10204 and then
10205 (No (Overridden_Operation (E))
10206 or else Extra_Formals_Match_OK (E,
10207 Ultimate_Alias (Overridden_Operation (E))))));
10208 end if;
10210 Set_Mechanisms (E);
10212 -- If this is convention Ada and a Valued_Procedure, that's odd
10214 if Ekind (E) = E_Procedure
10215 and then Is_Valued_Procedure (E)
10216 and then Convention (E) = Convention_Ada
10217 and then Warn_On_Export_Import
10218 then
10219 Error_Msg_N
10220 ("??Valued_Procedure has no effect for convention Ada", E);
10221 Set_Is_Valued_Procedure (E, False);
10222 end if;
10224 -- Case of foreign convention
10226 else
10227 Set_Mechanisms (E);
10229 -- For foreign conventions, warn about return of unconstrained array
10231 if Ekind (E) = E_Function then
10232 Retype := Underlying_Type (Etype (E));
10234 -- If no return type, probably some other error, e.g. a
10235 -- missing full declaration, so ignore.
10237 if No (Retype) then
10238 null;
10240 -- If the return type is generic, we have emitted a warning
10241 -- earlier on, and there is nothing else to check here. Specific
10242 -- instantiations may lead to erroneous behavior.
10244 elsif Is_Generic_Type (Etype (E)) then
10245 null;
10247 -- Display warning if returning unconstrained array
10249 elsif Is_Array_Type (Retype)
10250 and then not Is_Constrained (Retype)
10252 -- Check appropriate warning is enabled (should we check for
10253 -- Warnings (Off) on specific entities here, probably so???)
10255 and then Warn_On_Export_Import
10256 then
10257 Error_Msg_N
10258 ("?x?foreign convention function& should not return " &
10259 "unconstrained array", E);
10260 return;
10261 end if;
10262 end if;
10264 -- If any of the formals for an exported foreign convention
10265 -- subprogram have defaults, then emit an appropriate warning since
10266 -- this is odd (default cannot be used from non-Ada code)
10268 if Is_Exported (E) then
10269 F := First_Formal (E);
10270 while Present (F) loop
10271 if Warn_On_Export_Import
10272 and then Present (Default_Value (F))
10273 then
10274 Error_Msg_N
10275 ("?x?parameter cannot be defaulted in non-Ada call",
10276 Default_Value (F));
10277 end if;
10279 Next_Formal (F);
10280 end loop;
10281 end if;
10282 end if;
10284 -- Pragma Inline_Always is disallowed for dispatching subprograms
10285 -- because the address of such subprograms is saved in the dispatch
10286 -- table to support dispatching calls, and dispatching calls cannot
10287 -- be inlined. This is consistent with the restriction against using
10288 -- 'Access or 'Address on an Inline_Always subprogram.
10290 if Is_Dispatching_Operation (E)
10291 and then Has_Pragma_Inline_Always (E)
10292 then
10293 Error_Msg_N
10294 ("pragma Inline_Always not allowed for dispatching subprograms", E);
10295 end if;
10297 if Is_Dispatching_Operation (E)
10298 and then Present (Overridden_Operation (E))
10299 then
10300 Local_Restrict.Check_Overriding
10301 (Overrider_Op => E, Overridden_Op => Overridden_Operation (E));
10302 end if;
10304 -- Because of the implicit representation of inherited predefined
10305 -- operators in the front-end, the overriding status of the operation
10306 -- may be affected when a full view of a type is analyzed, and this is
10307 -- not captured by the analysis of the corresponding type declaration.
10308 -- Therefore the correctness of a not-overriding indicator must be
10309 -- rechecked when the subprogram is frozen.
10311 if Nkind (E) = N_Defining_Operator_Symbol
10312 and then not Error_Posted (Parent (E))
10313 then
10314 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
10315 end if;
10317 Retype := Get_Fullest_View (Etype (E));
10319 if Transform_Function_Array
10320 and then Nkind (Parent (E)) = N_Function_Specification
10321 and then Is_Array_Type (Retype)
10322 and then Is_Constrained (Retype)
10323 and then not Is_Unchecked_Conversion_Instance (E)
10324 and then not Rewritten_For_C (E)
10325 then
10326 Build_Procedure_Form (Unit_Declaration_Node (E));
10327 end if;
10328 end Freeze_Subprogram;
10330 ----------------------
10331 -- Is_Fully_Defined --
10332 ----------------------
10334 function Is_Fully_Defined (T : Entity_Id) return Boolean is
10335 begin
10336 if Ekind (T) = E_Class_Wide_Type then
10337 return Is_Fully_Defined (Etype (T));
10339 elsif Is_Array_Type (T) then
10340 return Is_Fully_Defined (Component_Type (T));
10342 elsif Is_Record_Type (T)
10343 and not Is_Private_Type (T)
10344 then
10345 -- Verify that the record type has no components with private types
10346 -- without completion.
10348 declare
10349 Comp : Entity_Id;
10351 begin
10352 Comp := First_Component (T);
10353 while Present (Comp) loop
10354 if not Is_Fully_Defined (Etype (Comp)) then
10355 return False;
10356 end if;
10358 Next_Component (Comp);
10359 end loop;
10360 return True;
10361 end;
10363 -- For the designated type of an access to subprogram, all types in
10364 -- the profile must be fully defined.
10366 elsif Ekind (T) = E_Subprogram_Type then
10367 declare
10368 F : Entity_Id;
10370 begin
10371 F := First_Formal (T);
10372 while Present (F) loop
10373 if not Is_Fully_Defined (Etype (F)) then
10374 return False;
10375 end if;
10377 Next_Formal (F);
10378 end loop;
10380 return Is_Fully_Defined (Etype (T));
10381 end;
10383 else
10384 return not Is_Private_Type (T)
10385 or else Present (Full_View (Base_Type (T)));
10386 end if;
10387 end Is_Fully_Defined;
10389 ---------------------------------
10390 -- Process_Default_Expressions --
10391 ---------------------------------
10393 procedure Process_Default_Expressions
10394 (E : Entity_Id;
10395 After : in out Node_Id)
10397 Loc : constant Source_Ptr := Sloc (E);
10398 Dbody : Node_Id;
10399 Formal : Node_Id;
10400 Dcopy : Node_Id;
10401 Dnam : Entity_Id;
10403 begin
10404 Set_Default_Expressions_Processed (E);
10406 -- A subprogram instance and its associated anonymous subprogram share
10407 -- their signature. The default expression functions are defined in the
10408 -- wrapper packages for the anonymous subprogram, and should not be
10409 -- generated again for the instance.
10411 if Is_Generic_Instance (E)
10412 and then Present (Alias (E))
10413 and then Default_Expressions_Processed (Alias (E))
10414 then
10415 return;
10416 end if;
10418 Formal := First_Formal (E);
10419 while Present (Formal) loop
10420 if Present (Default_Value (Formal)) then
10422 -- We work with a copy of the default expression because we
10423 -- do not want to disturb the original, since this would mess
10424 -- up the conformance checking.
10426 Dcopy := New_Copy_Tree (Default_Value (Formal));
10428 -- The analysis of the expression may generate insert actions,
10429 -- which of course must not be executed. We wrap those actions
10430 -- in a procedure that is not called, and later on eliminated.
10431 -- The following cases have no side effects, and are analyzed
10432 -- directly.
10434 if Nkind (Dcopy) = N_Identifier
10435 or else Nkind (Dcopy) in N_Expanded_Name
10436 | N_Integer_Literal
10437 | N_Character_Literal
10438 | N_String_Literal
10439 | N_Real_Literal
10440 or else (Nkind (Dcopy) = N_Attribute_Reference
10441 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
10442 or else Known_Null (Dcopy)
10443 then
10444 -- If there is no default function, we must still do a full
10445 -- analyze call on the default value, to ensure that all error
10446 -- checks are performed, e.g. those associated with static
10447 -- evaluation. Note: this branch will always be taken if the
10448 -- analyzer is turned off (but we still need the error checks).
10450 -- Note: the setting of parent here is to meet the requirement
10451 -- that we can only analyze the expression while attached to
10452 -- the tree. Really the requirement is that the parent chain
10453 -- be set, we don't actually need to be in the tree.
10455 Set_Parent (Dcopy, Declaration_Node (Formal));
10456 Analyze (Dcopy);
10458 -- Default expressions are resolved with their own type if the
10459 -- context is generic, to avoid anomalies with private types.
10461 if Ekind (Scope (E)) = E_Generic_Package then
10462 Resolve (Dcopy);
10463 else
10464 Resolve (Dcopy, Etype (Formal));
10465 end if;
10467 -- If that resolved expression will raise constraint error,
10468 -- then flag the default value as raising constraint error.
10469 -- This allows a proper error message on the calls.
10471 if Raises_Constraint_Error (Dcopy) then
10472 Set_Raises_Constraint_Error (Default_Value (Formal));
10473 end if;
10475 -- If the default is a parameterless call, we use the name of
10476 -- the called function directly, and there is no body to build.
10478 elsif Nkind (Dcopy) = N_Function_Call
10479 and then No (Parameter_Associations (Dcopy))
10480 then
10481 null;
10483 -- Else construct and analyze the body of a wrapper procedure
10484 -- that contains an object declaration to hold the expression.
10485 -- Given that this is done only to complete the analysis, it is
10486 -- simpler to build a procedure than a function which might
10487 -- involve secondary stack expansion.
10489 else
10490 Dnam := Make_Temporary (Loc, 'D');
10492 Dbody :=
10493 Make_Subprogram_Body (Loc,
10494 Specification =>
10495 Make_Procedure_Specification (Loc,
10496 Defining_Unit_Name => Dnam),
10498 Declarations => New_List (
10499 Make_Object_Declaration (Loc,
10500 Defining_Identifier => Make_Temporary (Loc, 'T'),
10501 Object_Definition =>
10502 New_Occurrence_Of (Etype (Formal), Loc),
10503 Expression => New_Copy_Tree (Dcopy))),
10505 Handled_Statement_Sequence =>
10506 Make_Handled_Sequence_Of_Statements (Loc,
10507 Statements => Empty_List));
10509 Set_Scope (Dnam, Scope (E));
10510 Set_Assignment_OK (First (Declarations (Dbody)));
10511 Set_Is_Eliminated (Dnam);
10512 Insert_After (After, Dbody);
10513 Analyze (Dbody);
10514 After := Dbody;
10515 end if;
10516 end if;
10518 Next_Formal (Formal);
10519 end loop;
10520 end Process_Default_Expressions;
10522 ----------------------------------------
10523 -- Set_Component_Alignment_If_Not_Set --
10524 ----------------------------------------
10526 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
10527 begin
10528 -- Ignore if not base type, subtypes don't need anything
10530 if Typ /= Base_Type (Typ) then
10531 return;
10532 end if;
10534 -- Do not override existing representation
10536 if Is_Packed (Typ) then
10537 return;
10539 elsif Has_Specified_Layout (Typ) then
10540 return;
10542 elsif Component_Alignment (Typ) /= Calign_Default then
10543 return;
10545 else
10546 Set_Component_Alignment
10547 (Typ, Scope_Stack.Table
10548 (Scope_Stack.Last).Component_Alignment_Default);
10549 end if;
10550 end Set_Component_Alignment_If_Not_Set;
10552 --------------------------
10553 -- Set_SSO_From_Default --
10554 --------------------------
10556 procedure Set_SSO_From_Default (T : Entity_Id) is
10557 Reversed : Boolean;
10559 begin
10560 -- Set default SSO for an array or record base type, except in case of
10561 -- a type extension (which always inherits the SSO of its parent type).
10563 if Is_Base_Type (T)
10564 and then (Is_Array_Type (T)
10565 or else (Is_Record_Type (T)
10566 and then not (Is_Tagged_Type (T)
10567 and then Is_Derived_Type (T))))
10568 then
10569 Reversed :=
10570 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
10571 or else
10572 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
10574 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
10576 -- For a record type, if bit order is specified explicitly,
10577 -- then do not set SSO from default if not consistent. Note that
10578 -- we do not want to look at a Bit_Order attribute definition
10579 -- for a parent: if we were to inherit Bit_Order, then both
10580 -- SSO_Set_*_By_Default flags would have been cleared already
10581 -- (by Inherit_Aspects_At_Freeze_Point).
10583 and then not
10584 (Is_Record_Type (T)
10585 and then
10586 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
10587 and then Reverse_Bit_Order (T) /= Reversed)
10588 then
10589 -- If flags cause reverse storage order, then set the result. Note
10590 -- that we would have ignored the pragma setting the non default
10591 -- storage order in any case, hence the assertion at this point.
10593 pragma Assert
10594 (not Reversed or else Support_Nondefault_SSO_On_Target);
10596 Set_Reverse_Storage_Order (T, Reversed);
10598 -- For a record type, also set reversed bit order. Note: if a bit
10599 -- order has been specified explicitly, then this is a no-op.
10601 if Is_Record_Type (T) then
10602 Set_Reverse_Bit_Order (T, Reversed);
10603 end if;
10604 end if;
10605 end if;
10606 end Set_SSO_From_Default;
10608 ------------------
10609 -- Undelay_Type --
10610 ------------------
10612 procedure Undelay_Type (T : Entity_Id) is
10613 begin
10614 Set_Has_Delayed_Freeze (T, False);
10615 Set_Freeze_Node (T, Empty);
10617 -- Since we don't want T to have a Freeze_Node, we don't want its
10618 -- Full_View or Corresponding_Record_Type to have one either.
10620 -- ??? Fundamentally, this whole handling is unpleasant. What we really
10621 -- want is to be sure that for an Itype that's part of record R and is a
10622 -- subtype of type T, that it's frozen after the later of the freeze
10623 -- points of R and T. We have no way of doing that directly, so what we
10624 -- do is force most such Itypes to be frozen as part of freezing R via
10625 -- this procedure and only delay the ones that need to be delayed
10626 -- (mostly the designated types of access types that are defined as part
10627 -- of the record).
10629 if Is_Private_Type (T)
10630 and then Present (Full_View (T))
10631 and then Is_Itype (Full_View (T))
10632 and then Is_Record_Type (Scope (Full_View (T)))
10633 then
10634 Undelay_Type (Full_View (T));
10635 end if;
10637 if Is_Concurrent_Type (T)
10638 and then Present (Corresponding_Record_Type (T))
10639 and then Is_Itype (Corresponding_Record_Type (T))
10640 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
10641 then
10642 Undelay_Type (Corresponding_Record_Type (T));
10643 end if;
10644 end Undelay_Type;
10646 ------------------
10647 -- Warn_Overlay --
10648 ------------------
10650 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id) is
10651 Ent : constant Entity_Id := Entity (Nam);
10652 -- The object to which the address clause applies
10654 Init : Node_Id;
10655 Old : Entity_Id := Empty;
10656 Decl : Node_Id;
10658 begin
10659 -- No warning if address clause overlay warnings are off
10661 if not Address_Clause_Overlay_Warnings then
10662 return;
10663 end if;
10665 -- No warning if there is an explicit initialization
10667 Init := Original_Node (Expression (Declaration_Node (Ent)));
10669 if Present (Init) and then Comes_From_Source (Init) then
10670 return;
10671 end if;
10673 -- We only give the warning for non-imported entities of a type for
10674 -- which a non-null base init proc is defined, or for objects of access
10675 -- types with implicit null initialization, or when Normalize_Scalars
10676 -- applies and the type is scalar or a string type (the latter being
10677 -- tested for because predefined String types are initialized by inline
10678 -- code rather than by an init_proc). Note that we do not give the
10679 -- warning for Initialize_Scalars, since we suppressed initialization
10680 -- in this case. Also, do not warn if Suppress_Initialization is set
10681 -- either on the type, or on the object via pragma or aspect.
10683 if Present (Expr)
10684 and then not Is_Imported (Ent)
10685 and then not Initialization_Suppressed (Typ)
10686 and then not (Ekind (Ent) = E_Variable
10687 and then Initialization_Suppressed (Ent))
10688 and then (Has_Non_Null_Base_Init_Proc (Typ)
10689 or else Is_Access_Type (Typ)
10690 or else (Normalize_Scalars
10691 and then (Is_Scalar_Type (Typ)
10692 or else Is_String_Type (Typ))))
10693 then
10694 if Nkind (Expr) = N_Attribute_Reference
10695 and then Is_Entity_Name (Prefix (Expr))
10696 then
10697 Old := Entity (Prefix (Expr));
10699 elsif Is_Entity_Name (Expr)
10700 and then Ekind (Entity (Expr)) = E_Constant
10701 then
10702 Decl := Declaration_Node (Entity (Expr));
10704 if Nkind (Decl) = N_Object_Declaration
10705 and then Present (Expression (Decl))
10706 and then Nkind (Expression (Decl)) = N_Attribute_Reference
10707 and then Is_Entity_Name (Prefix (Expression (Decl)))
10708 then
10709 Old := Entity (Prefix (Expression (Decl)));
10711 elsif Nkind (Expr) = N_Function_Call then
10712 return;
10713 end if;
10715 -- A function call (most likely to To_Address) is probably not an
10716 -- overlay, so skip warning. Ditto if the function call was inlined
10717 -- and transformed into an entity.
10719 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
10720 return;
10721 end if;
10723 -- If a pragma Import follows, we assume that it is for the current
10724 -- target of the address clause, and skip the warning. There may be
10725 -- a source pragma or an aspect that specifies import and generates
10726 -- the corresponding pragma. These will indicate that the entity is
10727 -- imported and that is checked above so that the spurious warning
10728 -- (generated when the entity is frozen) will be suppressed. The
10729 -- pragma may be attached to the aspect, so it is not yet a list
10730 -- member.
10732 if Is_List_Member (Parent (Expr)) then
10733 Decl := Next (Parent (Expr));
10735 if Present (Decl)
10736 and then Nkind (Decl) = N_Pragma
10737 and then Pragma_Name (Decl) = Name_Import
10738 then
10739 return;
10740 end if;
10741 end if;
10743 -- Otherwise give warning message
10745 if Present (Old) then
10746 Error_Msg_Node_2 := Old;
10747 Error_Msg_N
10748 ("default initialization of & may modify &?o?",
10749 Nam);
10750 else
10751 Error_Msg_N
10752 ("default initialization of & may modify overlaid storage?o?",
10753 Nam);
10754 end if;
10756 -- Add friendly warning if initialization comes from a packed array
10757 -- component.
10759 if Is_Record_Type (Typ) then
10760 declare
10761 Comp : Entity_Id;
10763 begin
10764 Comp := First_Component (Typ);
10765 while Present (Comp) loop
10766 if Nkind (Parent (Comp)) = N_Component_Declaration
10767 and then Present (Expression (Parent (Comp)))
10768 then
10769 exit;
10770 elsif Is_Array_Type (Etype (Comp))
10771 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
10772 then
10773 Error_Msg_NE
10774 ("\packed array component& " &
10775 "will be initialized to zero??",
10776 Nam, Comp);
10777 exit;
10778 else
10779 Next_Component (Comp);
10780 end if;
10781 end loop;
10782 end;
10783 end if;
10785 Error_Msg_N
10786 ("\use pragma Import for & to " &
10787 "suppress initialization (RM B.1(24))??",
10788 Nam);
10789 end if;
10790 end Warn_Overlay;
10792 end Freeze;