2018-05-21 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / ada / freeze.adb
blob9a67061e3fcb5a3c3be9bb11faac3ab1aeee3c41
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-2018, 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 Elists; use Elists;
33 with Errout; use Errout;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Pakd; use Exp_Pakd;
37 with Exp_Util; use Exp_Util;
38 with Exp_Tss; use Exp_Tss;
39 with Ghost; use Ghost;
40 with Layout; use Layout;
41 with Lib; use Lib;
42 with Namet; use Namet;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Mech; use Sem_Mech;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Sinfo; use Sinfo;
62 with Snames; use Snames;
63 with Stand; use Stand;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Ttypes; use Ttypes;
67 with Uintp; use Uintp;
68 with Urealp; use Urealp;
69 with Warnsw; use Warnsw;
71 package body Freeze is
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
78 -- Typ is a type that is being frozen. If no size clause is given,
79 -- but a default Esize has been computed, then this default Esize is
80 -- adjusted up if necessary to be consistent with a given alignment,
81 -- but never to a value greater than Long_Long_Integer'Size. This
82 -- is used for all discrete types and for fixed-point types.
84 procedure Build_And_Analyze_Renamed_Body
85 (Decl : Node_Id;
86 New_S : Entity_Id;
87 After : in out Node_Id);
88 -- Build body for a renaming declaration, insert in tree and analyze
90 procedure Check_Address_Clause (E : Entity_Id);
91 -- Apply legality checks to address clauses for object declarations,
92 -- at the point the object is frozen. Also ensure any initialization is
93 -- performed only after the object has been frozen.
95 procedure Check_Component_Storage_Order
96 (Encl_Type : Entity_Id;
97 Comp : Entity_Id;
98 ADC : Node_Id;
99 Comp_ADC_Present : out Boolean);
100 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
101 -- clause, verify that the component type has an explicit and compatible
102 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
103 -- entity of the component under consideration. For an Encl_Type that
104 -- does not have a Scalar_Storage_Order attribute definition clause,
105 -- verify that the component also does not have such a clause.
106 -- ADC is the attribute definition clause if present (or Empty). On return,
107 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
108 -- attribute definition clause.
110 procedure Check_Debug_Info_Needed (T : Entity_Id);
111 -- As each entity is frozen, this routine is called to deal with the
112 -- setting of Debug_Info_Needed for the entity. This flag is set if
113 -- the entity comes from source, or if we are in Debug_Generated_Code
114 -- mode or if the -gnatdV debug flag is set. However, it never sets
115 -- the flag if Debug_Info_Off is set. This procedure also ensures that
116 -- subsidiary entities have the flag set as required.
118 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
119 -- When an expression function is frozen by a use of it, the expression
120 -- itself is frozen. Check that the expression does not include references
121 -- to deferred constants without completion. We report this at the freeze
122 -- point of the function, to provide a better error message.
124 -- In most cases the expression itself is frozen by the time the function
125 -- itself is frozen, because the formals will be frozen by then. However,
126 -- Attribute references to outer types are freeze points for those types;
127 -- this routine generates the required freeze nodes for them.
129 procedure Check_Inherited_Conditions (R : Entity_Id);
130 -- For a tagged derived type, create wrappers for inherited operations
131 -- that have a class-wide condition, so it can be properly rewritten if
132 -- it involves calls to other overriding primitives.
134 procedure Check_Strict_Alignment (E : Entity_Id);
135 -- E is a base type. If E is tagged or has a component that is aliased
136 -- or tagged or contains something this is aliased or tagged, set
137 -- Strict_Alignment.
139 procedure Check_Unsigned_Type (E : Entity_Id);
140 pragma Inline (Check_Unsigned_Type);
141 -- If E is a fixed-point or discrete type, then all the necessary work
142 -- to freeze it is completed except for possible setting of the flag
143 -- Is_Unsigned_Type, which is done by this procedure. The call has no
144 -- effect if the entity E is not a discrete or fixed-point type.
146 procedure Freeze_And_Append
147 (Ent : Entity_Id;
148 N : Node_Id;
149 Result : in out List_Id);
150 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
151 -- nodes to Result, modifying Result from No_List if necessary. N has
152 -- the same usage as in Freeze_Entity.
154 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
155 -- Freeze enumeration type. The Esize field is set as processing
156 -- proceeds (i.e. set by default when the type is declared and then
157 -- adjusted by rep clauses. What this procedure does is to make sure
158 -- that if a foreign convention is specified, and no specific size
159 -- is given, then the size must be at least Integer'Size.
161 procedure Freeze_Static_Object (E : Entity_Id);
162 -- If an object is frozen which has Is_Statically_Allocated set, then
163 -- all referenced types must also be marked with this flag. This routine
164 -- is in charge of meeting this requirement for the object entity E.
166 procedure Freeze_Subprogram (E : Entity_Id);
167 -- Perform freezing actions for a subprogram (create extra formals,
168 -- and set proper default mechanism values). Note that this routine
169 -- is not called for internal subprograms, for which neither of these
170 -- actions is needed (or desirable, we do not want for example to have
171 -- these extra formals present in initialization procedures, where they
172 -- would serve no purpose). In this call E is either a subprogram or
173 -- a subprogram type (i.e. an access to a subprogram).
175 function Is_Fully_Defined (T : Entity_Id) return Boolean;
176 -- True if T is not private and has no private components, or has a full
177 -- view. Used to determine whether the designated type of an access type
178 -- should be frozen when the access type is frozen. This is done when an
179 -- allocator is frozen, or an expression that may involve attributes of
180 -- the designated type. Otherwise freezing the access type does not freeze
181 -- the designated type.
183 procedure Process_Default_Expressions
184 (E : Entity_Id;
185 After : in out Node_Id);
186 -- This procedure is called for each subprogram to complete processing of
187 -- default expressions at the point where all types are known to be frozen.
188 -- The expressions must be analyzed in full, to make sure that all error
189 -- processing is done (they have only been pre-analyzed). If the expression
190 -- is not an entity or literal, its analysis may generate code which must
191 -- not be executed. In that case we build a function body to hold that
192 -- code. This wrapper function serves no other purpose (it used to be
193 -- called to evaluate the default, but now the default is inlined at each
194 -- point of call).
196 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
197 -- Typ is a record or array type that is being frozen. This routine sets
198 -- the default component alignment from the scope stack values if the
199 -- alignment is otherwise not specified.
201 procedure Set_SSO_From_Default (T : Entity_Id);
202 -- T is a record or array type that is being frozen. If it is a base type,
203 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
204 -- will be set appropriately. Note that an explicit occurrence of aspect
205 -- Scalar_Storage_Order or an explicit setting of this aspect with an
206 -- attribute definition clause occurs, then these two flags are reset in
207 -- any case, so call will have no effect.
209 procedure Undelay_Type (T : Entity_Id);
210 -- T is a type of a component that we know to be an Itype. We don't want
211 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
212 -- Full_View or Corresponding_Record_Type.
214 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
215 -- Expr is the expression for an address clause for entity Nam whose type
216 -- is Typ. If Typ has a default initialization, and there is no explicit
217 -- initialization in the source declaration, check whether the address
218 -- clause might cause overlaying of an entity, and emit a warning on the
219 -- side effect that the initialization will cause.
221 -------------------------------
222 -- Adjust_Esize_For_Alignment --
223 -------------------------------
225 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
226 Align : Uint;
228 begin
229 if Known_Esize (Typ) and then Known_Alignment (Typ) then
230 Align := Alignment_In_Bits (Typ);
232 if Align > Esize (Typ)
233 and then Align <= Standard_Long_Long_Integer_Size
234 then
235 Set_Esize (Typ, Align);
236 end if;
237 end if;
238 end Adjust_Esize_For_Alignment;
240 ------------------------------------
241 -- Build_And_Analyze_Renamed_Body --
242 ------------------------------------
244 procedure Build_And_Analyze_Renamed_Body
245 (Decl : Node_Id;
246 New_S : Entity_Id;
247 After : in out Node_Id)
249 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
250 Ent : constant Entity_Id := Defining_Entity (Decl);
251 Body_Node : Node_Id;
252 Renamed_Subp : Entity_Id;
254 begin
255 -- If the renamed subprogram is intrinsic, there is no need for a
256 -- wrapper body: we set the alias that will be called and expanded which
257 -- completes the declaration. This transformation is only legal if the
258 -- renamed entity has already been elaborated.
260 -- Note that it is legal for a renaming_as_body to rename an intrinsic
261 -- subprogram, as long as the renaming occurs before the new entity
262 -- is frozen (RM 8.5.4 (5)).
264 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
265 and then Is_Entity_Name (Name (Body_Decl))
266 then
267 Renamed_Subp := Entity (Name (Body_Decl));
268 else
269 Renamed_Subp := Empty;
270 end if;
272 if Present (Renamed_Subp)
273 and then Is_Intrinsic_Subprogram (Renamed_Subp)
274 and then
275 (not In_Same_Source_Unit (Renamed_Subp, Ent)
276 or else Sloc (Renamed_Subp) < Sloc (Ent))
278 -- We can make the renaming entity intrinsic if the renamed function
279 -- has an interface name, or if it is one of the shift/rotate
280 -- operations known to the compiler.
282 and then
283 (Present (Interface_Name (Renamed_Subp))
284 or else Nam_In (Chars (Renamed_Subp), Name_Rotate_Left,
285 Name_Rotate_Right,
286 Name_Shift_Left,
287 Name_Shift_Right,
288 Name_Shift_Right_Arithmetic))
289 then
290 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
292 if Present (Alias (Renamed_Subp)) then
293 Set_Alias (Ent, Alias (Renamed_Subp));
294 else
295 Set_Alias (Ent, Renamed_Subp);
296 end if;
298 Set_Is_Intrinsic_Subprogram (Ent);
299 Set_Has_Completion (Ent);
301 else
302 Body_Node := Build_Renamed_Body (Decl, New_S);
303 Insert_After (After, Body_Node);
304 Mark_Rewrite_Insertion (Body_Node);
305 Analyze (Body_Node);
306 After := Body_Node;
307 end if;
308 end Build_And_Analyze_Renamed_Body;
310 ------------------------
311 -- Build_Renamed_Body --
312 ------------------------
314 function Build_Renamed_Body
315 (Decl : Node_Id;
316 New_S : Entity_Id) return Node_Id
318 Loc : constant Source_Ptr := Sloc (New_S);
319 -- We use for the source location of the renamed body, the location of
320 -- the spec entity. It might seem more natural to use the location of
321 -- the renaming declaration itself, but that would be wrong, since then
322 -- the body we create would look as though it was created far too late,
323 -- and this could cause problems with elaboration order analysis,
324 -- particularly in connection with instantiations.
326 N : constant Node_Id := Unit_Declaration_Node (New_S);
327 Nam : constant Node_Id := Name (N);
328 Old_S : Entity_Id;
329 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
330 Actuals : List_Id := No_List;
331 Call_Node : Node_Id;
332 Call_Name : Node_Id;
333 Body_Node : Node_Id;
334 Formal : Entity_Id;
335 O_Formal : Entity_Id;
336 Param_Spec : Node_Id;
338 Pref : Node_Id := Empty;
339 -- If the renamed entity is a primitive operation given in prefix form,
340 -- the prefix is the target object and it has to be added as the first
341 -- actual in the generated call.
343 begin
344 -- Determine the entity being renamed, which is the target of the call
345 -- statement. If the name is an explicit dereference, this is a renaming
346 -- of a subprogram type rather than a subprogram. The name itself is
347 -- fully analyzed.
349 if Nkind (Nam) = N_Selected_Component then
350 Old_S := Entity (Selector_Name (Nam));
352 elsif Nkind (Nam) = N_Explicit_Dereference then
353 Old_S := Etype (Nam);
355 elsif Nkind (Nam) = N_Indexed_Component then
356 if Is_Entity_Name (Prefix (Nam)) then
357 Old_S := Entity (Prefix (Nam));
358 else
359 Old_S := Entity (Selector_Name (Prefix (Nam)));
360 end if;
362 elsif Nkind (Nam) = N_Character_Literal then
363 Old_S := Etype (New_S);
365 else
366 Old_S := Entity (Nam);
367 end if;
369 if Is_Entity_Name (Nam) then
371 -- If the renamed entity is a predefined operator, retain full name
372 -- to ensure its visibility.
374 if Ekind (Old_S) = E_Operator
375 and then Nkind (Nam) = N_Expanded_Name
376 then
377 Call_Name := New_Copy (Name (N));
378 else
379 Call_Name := New_Occurrence_Of (Old_S, Loc);
380 end if;
382 else
383 if Nkind (Nam) = N_Selected_Component
384 and then Present (First_Formal (Old_S))
385 and then
386 (Is_Controlling_Formal (First_Formal (Old_S))
387 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
388 then
390 -- Retrieve the target object, to be added as a first actual
391 -- in the call.
393 Call_Name := New_Occurrence_Of (Old_S, Loc);
394 Pref := Prefix (Nam);
396 else
397 Call_Name := New_Copy (Name (N));
398 end if;
400 -- Original name may have been overloaded, but is fully resolved now
402 Set_Is_Overloaded (Call_Name, False);
403 end if;
405 -- For simple renamings, subsequent calls can be expanded directly as
406 -- calls to the renamed entity. The body must be generated in any case
407 -- for calls that may appear elsewhere. This is not done in the case
408 -- where the subprogram is an instantiation because the actual proper
409 -- body has not been built yet.
411 if Ekind_In (Old_S, E_Function, E_Procedure)
412 and then Nkind (Decl) = N_Subprogram_Declaration
413 and then not Is_Generic_Instance (Old_S)
414 then
415 Set_Body_To_Inline (Decl, Old_S);
416 end if;
418 -- Check whether the return type is a limited view. If the subprogram
419 -- is already frozen the generated body may have a non-limited view
420 -- of the type, that must be used, because it is the one in the spec
421 -- of the renaming declaration.
423 if Ekind (Old_S) = E_Function
424 and then Is_Entity_Name (Result_Definition (Spec))
425 then
426 declare
427 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
428 begin
429 if Has_Non_Limited_View (Ret_Type) then
430 Set_Result_Definition
431 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
432 end if;
433 end;
434 end if;
436 -- The body generated for this renaming is an internal artifact, and
437 -- does not constitute a freeze point for the called entity.
439 Set_Must_Not_Freeze (Call_Name);
441 Formal := First_Formal (Defining_Entity (Decl));
443 if Present (Pref) then
444 declare
445 Pref_Type : constant Entity_Id := Etype (Pref);
446 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
448 begin
449 -- The controlling formal may be an access parameter, or the
450 -- actual may be an access value, so adjust accordingly.
452 if Is_Access_Type (Pref_Type)
453 and then not Is_Access_Type (Form_Type)
454 then
455 Actuals := New_List
456 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
458 elsif Is_Access_Type (Form_Type)
459 and then not Is_Access_Type (Pref)
460 then
461 Actuals :=
462 New_List (
463 Make_Attribute_Reference (Loc,
464 Attribute_Name => Name_Access,
465 Prefix => Relocate_Node (Pref)));
466 else
467 Actuals := New_List (Pref);
468 end if;
469 end;
471 elsif Present (Formal) then
472 Actuals := New_List;
474 else
475 Actuals := No_List;
476 end if;
478 if Present (Formal) then
479 while Present (Formal) loop
480 Append (New_Occurrence_Of (Formal, Loc), Actuals);
481 Next_Formal (Formal);
482 end loop;
483 end if;
485 -- If the renamed entity is an entry, inherit its profile. For other
486 -- renamings as bodies, both profiles must be subtype conformant, so it
487 -- is not necessary to replace the profile given in the declaration.
488 -- However, default values that are aggregates are rewritten when
489 -- partially analyzed, so we recover the original aggregate to insure
490 -- that subsequent conformity checking works. Similarly, if the default
491 -- expression was constant-folded, recover the original expression.
493 Formal := First_Formal (Defining_Entity (Decl));
495 if Present (Formal) then
496 O_Formal := First_Formal (Old_S);
497 Param_Spec := First (Parameter_Specifications (Spec));
498 while Present (Formal) loop
499 if Is_Entry (Old_S) then
500 if Nkind (Parameter_Type (Param_Spec)) /=
501 N_Access_Definition
502 then
503 Set_Etype (Formal, Etype (O_Formal));
504 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
505 end if;
507 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
508 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
509 Nkind (Default_Value (O_Formal))
510 then
511 Set_Expression (Param_Spec,
512 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
513 end if;
515 Next_Formal (Formal);
516 Next_Formal (O_Formal);
517 Next (Param_Spec);
518 end loop;
519 end if;
521 -- If the renamed entity is a function, the generated body contains a
522 -- return statement. Otherwise, build a procedure call. If the entity is
523 -- an entry, subsequent analysis of the call will transform it into the
524 -- proper entry or protected operation call. If the renamed entity is
525 -- a character literal, return it directly.
527 if Ekind (Old_S) = E_Function
528 or else Ekind (Old_S) = E_Operator
529 or else (Ekind (Old_S) = E_Subprogram_Type
530 and then Etype (Old_S) /= Standard_Void_Type)
531 then
532 Call_Node :=
533 Make_Simple_Return_Statement (Loc,
534 Expression =>
535 Make_Function_Call (Loc,
536 Name => Call_Name,
537 Parameter_Associations => Actuals));
539 elsif Ekind (Old_S) = E_Enumeration_Literal then
540 Call_Node :=
541 Make_Simple_Return_Statement (Loc,
542 Expression => New_Occurrence_Of (Old_S, Loc));
544 elsif Nkind (Nam) = N_Character_Literal then
545 Call_Node :=
546 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
548 else
549 Call_Node :=
550 Make_Procedure_Call_Statement (Loc,
551 Name => Call_Name,
552 Parameter_Associations => Actuals);
553 end if;
555 -- Create entities for subprogram body and formals
557 Set_Defining_Unit_Name (Spec,
558 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
560 Param_Spec := First (Parameter_Specifications (Spec));
561 while Present (Param_Spec) loop
562 Set_Defining_Identifier (Param_Spec,
563 Make_Defining_Identifier (Loc,
564 Chars => Chars (Defining_Identifier (Param_Spec))));
565 Next (Param_Spec);
566 end loop;
568 Body_Node :=
569 Make_Subprogram_Body (Loc,
570 Specification => Spec,
571 Declarations => New_List,
572 Handled_Statement_Sequence =>
573 Make_Handled_Sequence_Of_Statements (Loc,
574 Statements => New_List (Call_Node)));
576 if Nkind (Decl) /= N_Subprogram_Declaration then
577 Rewrite (N,
578 Make_Subprogram_Declaration (Loc,
579 Specification => Specification (N)));
580 end if;
582 -- Link the body to the entity whose declaration it completes. If
583 -- the body is analyzed when the renamed entity is frozen, it may
584 -- be necessary to restore the proper scope (see package Exp_Ch13).
586 if Nkind (N) = N_Subprogram_Renaming_Declaration
587 and then Present (Corresponding_Spec (N))
588 then
589 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
590 else
591 Set_Corresponding_Spec (Body_Node, New_S);
592 end if;
594 return Body_Node;
595 end Build_Renamed_Body;
597 --------------------------
598 -- Check_Address_Clause --
599 --------------------------
601 procedure Check_Address_Clause (E : Entity_Id) is
602 Addr : constant Node_Id := Address_Clause (E);
603 Typ : constant Entity_Id := Etype (E);
604 Decl : Node_Id;
605 Expr : Node_Id;
606 Init : Node_Id;
607 Lhs : Node_Id;
608 Tag_Assign : Node_Id;
610 begin
611 if Present (Addr) then
613 -- For a deferred constant, the initialization value is on full view
615 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
616 Decl := Declaration_Node (Full_View (E));
617 else
618 Decl := Declaration_Node (E);
619 end if;
621 Expr := Expression (Addr);
623 if Needs_Constant_Address (Decl, Typ) then
624 Check_Constant_Address_Clause (Expr, E);
626 -- Has_Delayed_Freeze was set on E when the address clause was
627 -- analyzed, and must remain set because we want the address
628 -- clause to be elaborated only after any entity it references
629 -- has been elaborated.
630 end if;
632 -- If Rep_Clauses are to be ignored, remove address clause from
633 -- list attached to entity, because it may be illegal for gigi,
634 -- for example by breaking order of elaboration..
636 if Ignore_Rep_Clauses then
637 declare
638 Rep : Node_Id;
640 begin
641 Rep := First_Rep_Item (E);
643 if Rep = Addr then
644 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
646 else
647 while Present (Rep)
648 and then Next_Rep_Item (Rep) /= Addr
649 loop
650 Rep := Next_Rep_Item (Rep);
651 end loop;
652 end if;
654 if Present (Rep) then
655 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
656 end if;
657 end;
659 -- And now remove the address clause
661 Kill_Rep_Clause (Addr);
663 elsif not Error_Posted (Expr)
664 and then not Needs_Finalization (Typ)
665 then
666 Warn_Overlay (Expr, Typ, Name (Addr));
667 end if;
669 Init := Expression (Decl);
671 -- If a variable, or a non-imported constant, overlays a constant
672 -- object and has an initialization value, then the initialization
673 -- may end up writing into read-only memory. Detect the cases of
674 -- statically identical values and remove the initialization. In
675 -- the other cases, give a warning. We will give other warnings
676 -- later for the variable if it is assigned.
678 if (Ekind (E) = E_Variable
679 or else (Ekind (E) = E_Constant
680 and then not Is_Imported (E)))
681 and then Overlays_Constant (E)
682 and then Present (Init)
683 then
684 declare
685 O_Ent : Entity_Id;
686 Off : Boolean;
688 begin
689 Find_Overlaid_Entity (Addr, O_Ent, Off);
691 if Ekind (O_Ent) = E_Constant
692 and then Etype (O_Ent) = Typ
693 and then Present (Constant_Value (O_Ent))
694 and then Compile_Time_Compare
695 (Init,
696 Constant_Value (O_Ent),
697 Assume_Valid => True) = EQ
698 then
699 Set_No_Initialization (Decl);
700 return;
702 elsif Comes_From_Source (Init)
703 and then Address_Clause_Overlay_Warnings
704 then
705 Error_Msg_Sloc := Sloc (Addr);
706 Error_Msg_NE
707 ("??constant& may be modified via address clause#",
708 Decl, O_Ent);
709 end if;
710 end;
711 end if;
713 -- Remove side effects from initial expression, except in the case
714 -- of a build-in-place call, which has its own later expansion.
716 if Present (Init)
717 and then (Nkind (Init) /= N_Function_Call
718 or else not Is_Expanded_Build_In_Place_Call (Init))
719 then
720 -- Capture initialization value at point of declaration, and make
721 -- explicit assignment legal, because object may be a constant.
723 Remove_Side_Effects (Init);
724 Lhs := New_Occurrence_Of (E, Sloc (Decl));
725 Set_Assignment_OK (Lhs);
727 -- Move initialization to freeze actions, once the object has
728 -- been frozen and the address clause alignment check has been
729 -- performed.
731 Append_Freeze_Action (E,
732 Make_Assignment_Statement (Sloc (Decl),
733 Name => Lhs,
734 Expression => Expression (Decl)));
736 Set_No_Initialization (Decl);
738 -- If the objet is tagged, check whether the tag must be
739 -- reassigned explicitly.
741 Tag_Assign := Make_Tag_Assignment (Decl);
742 if Present (Tag_Assign) then
743 Append_Freeze_Action (E, Tag_Assign);
744 end if;
745 end if;
746 end if;
747 end Check_Address_Clause;
749 -----------------------------
750 -- Check_Compile_Time_Size --
751 -----------------------------
753 procedure Check_Compile_Time_Size (T : Entity_Id) is
755 procedure Set_Small_Size (T : Entity_Id; S : Uint);
756 -- Sets the compile time known size (64 bits or less) in the RM_Size
757 -- field of T, checking for a size clause that was given which attempts
758 -- to give a smaller size.
760 function Size_Known (T : Entity_Id) return Boolean;
761 -- Recursive function that does all the work
763 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
764 -- If T is a constrained subtype, its size is not known if any of its
765 -- discriminant constraints is not static and it is not a null record.
766 -- The test is conservative and doesn't check that the components are
767 -- in fact constrained by non-static discriminant values. Could be made
768 -- more precise ???
770 --------------------
771 -- Set_Small_Size --
772 --------------------
774 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
775 begin
776 if S > 64 then
777 return;
779 -- Check for bad size clause given
781 elsif Has_Size_Clause (T) then
782 if RM_Size (T) < S then
783 Error_Msg_Uint_1 := S;
784 Error_Msg_NE
785 ("size for& too small, minimum allowed is ^",
786 Size_Clause (T), T);
787 end if;
789 -- Set size if not set already
791 elsif Unknown_RM_Size (T) then
792 Set_RM_Size (T, S);
793 end if;
794 end Set_Small_Size;
796 ----------------
797 -- Size_Known --
798 ----------------
800 function Size_Known (T : Entity_Id) return Boolean is
801 Index : Entity_Id;
802 Comp : Entity_Id;
803 Ctyp : Entity_Id;
804 Low : Node_Id;
805 High : Node_Id;
807 begin
808 if Size_Known_At_Compile_Time (T) then
809 return True;
811 -- Always True for elementary types, even generic formal elementary
812 -- types. We used to return False in the latter case, but the size
813 -- is known at compile time, even in the template, we just do not
814 -- know the exact size but that's not the point of this routine.
816 elsif Is_Elementary_Type (T) or else Is_Task_Type (T) then
817 return True;
819 -- Array types
821 elsif Is_Array_Type (T) then
823 -- String literals always have known size, and we can set it
825 if Ekind (T) = E_String_Literal_Subtype then
826 Set_Small_Size
827 (T, Component_Size (T) * String_Literal_Length (T));
828 return True;
830 -- Unconstrained types never have known at compile time size
832 elsif not Is_Constrained (T) then
833 return False;
835 -- Don't do any recursion on type with error posted, since we may
836 -- have a malformed type that leads us into a loop.
838 elsif Error_Posted (T) then
839 return False;
841 -- Otherwise if component size unknown, then array size unknown
843 elsif not Size_Known (Component_Type (T)) then
844 return False;
845 end if;
847 -- Check for all indexes static, and also compute possible size
848 -- (in case it is not greater than 64 and may be packable).
850 declare
851 Size : Uint := Component_Size (T);
852 Dim : Uint;
854 begin
855 Index := First_Index (T);
856 while Present (Index) loop
857 if Nkind (Index) = N_Range then
858 Get_Index_Bounds (Index, Low, High);
860 elsif Error_Posted (Scalar_Range (Etype (Index))) then
861 return False;
863 else
864 Low := Type_Low_Bound (Etype (Index));
865 High := Type_High_Bound (Etype (Index));
866 end if;
868 if not Compile_Time_Known_Value (Low)
869 or else not Compile_Time_Known_Value (High)
870 or else Etype (Index) = Any_Type
871 then
872 return False;
874 else
875 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
877 if Dim >= 0 then
878 Size := Size * Dim;
879 else
880 Size := Uint_0;
881 end if;
882 end if;
884 Next_Index (Index);
885 end loop;
887 Set_Small_Size (T, Size);
888 return True;
889 end;
891 -- For non-generic private types, go to underlying type if present
893 elsif Is_Private_Type (T)
894 and then not Is_Generic_Type (T)
895 and then Present (Underlying_Type (T))
896 then
897 -- Don't do any recursion on type with error posted, since we may
898 -- have a malformed type that leads us into a loop.
900 if Error_Posted (T) then
901 return False;
902 else
903 return Size_Known (Underlying_Type (T));
904 end if;
906 -- Record types
908 elsif Is_Record_Type (T) then
910 -- A class-wide type is never considered to have a known size
912 if Is_Class_Wide_Type (T) then
913 return False;
915 -- A subtype of a variant record must not have non-static
916 -- discriminated components.
918 elsif T /= Base_Type (T)
919 and then not Static_Discriminated_Components (T)
920 then
921 return False;
923 -- Don't do any recursion on type with error posted, since we may
924 -- have a malformed type that leads us into a loop.
926 elsif Error_Posted (T) then
927 return False;
928 end if;
930 -- Now look at the components of the record
932 declare
933 -- The following two variables are used to keep track of the
934 -- size of packed records if we can tell the size of the packed
935 -- record in the front end. Packed_Size_Known is True if so far
936 -- we can figure out the size. It is initialized to True for a
937 -- packed record, unless the record has discriminants or atomic
938 -- components or independent components.
940 -- The reason we eliminate the discriminated case is that
941 -- we don't know the way the back end lays out discriminated
942 -- packed records. If Packed_Size_Known is True, then
943 -- Packed_Size is the size in bits so far.
945 Packed_Size_Known : Boolean :=
946 Is_Packed (T)
947 and then not Has_Discriminants (T)
948 and then not Has_Atomic_Components (T)
949 and then not Has_Independent_Components (T);
951 Packed_Size : Uint := Uint_0;
952 -- Size in bits so far
954 begin
955 -- Test for variant part present
957 if Has_Discriminants (T)
958 and then Present (Parent (T))
959 and then Nkind (Parent (T)) = N_Full_Type_Declaration
960 and then Nkind (Type_Definition (Parent (T))) =
961 N_Record_Definition
962 and then not Null_Present (Type_Definition (Parent (T)))
963 and then
964 Present (Variant_Part
965 (Component_List (Type_Definition (Parent (T)))))
966 then
967 -- If variant part is present, and type is unconstrained,
968 -- then we must have defaulted discriminants, or a size
969 -- clause must be present for the type, or else the size
970 -- is definitely not known at compile time.
972 if not Is_Constrained (T)
973 and then
974 No (Discriminant_Default_Value (First_Discriminant (T)))
975 and then Unknown_RM_Size (T)
976 then
977 return False;
978 end if;
979 end if;
981 -- Loop through components
983 Comp := First_Component_Or_Discriminant (T);
984 while Present (Comp) loop
985 Ctyp := Etype (Comp);
987 -- We do not know the packed size if there is a component
988 -- clause present (we possibly could, but this would only
989 -- help in the case of a record with partial rep clauses.
990 -- That's because in the case of full rep clauses, the
991 -- size gets figured out anyway by a different circuit).
993 if Present (Component_Clause (Comp)) then
994 Packed_Size_Known := False;
995 end if;
997 -- We do not know the packed size for an atomic/VFA type
998 -- or component, or an independent type or component, or a
999 -- by-reference type or aliased component (because packing
1000 -- does not touch these).
1002 if Is_Atomic_Or_VFA (Ctyp)
1003 or else Is_Atomic_Or_VFA (Comp)
1004 or else Is_Independent (Ctyp)
1005 or else Is_Independent (Comp)
1006 or else Is_By_Reference_Type (Ctyp)
1007 or else Is_Aliased (Comp)
1008 then
1009 Packed_Size_Known := False;
1010 end if;
1012 -- We need to identify a component that is an array where
1013 -- the index type is an enumeration type with non-standard
1014 -- representation, and some bound of the type depends on a
1015 -- discriminant.
1017 -- This is because gigi computes the size by doing a
1018 -- substitution of the appropriate discriminant value in
1019 -- the size expression for the base type, and gigi is not
1020 -- clever enough to evaluate the resulting expression (which
1021 -- involves a call to rep_to_pos) at compile time.
1023 -- It would be nice if gigi would either recognize that
1024 -- this expression can be computed at compile time, or
1025 -- alternatively figured out the size from the subtype
1026 -- directly, where all the information is at hand ???
1028 if Is_Array_Type (Etype (Comp))
1029 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1030 then
1031 declare
1032 Ocomp : constant Entity_Id :=
1033 Original_Record_Component (Comp);
1034 OCtyp : constant Entity_Id := Etype (Ocomp);
1035 Ind : Node_Id;
1036 Indtyp : Entity_Id;
1037 Lo, Hi : Node_Id;
1039 begin
1040 Ind := First_Index (OCtyp);
1041 while Present (Ind) loop
1042 Indtyp := Etype (Ind);
1044 if Is_Enumeration_Type (Indtyp)
1045 and then Has_Non_Standard_Rep (Indtyp)
1046 then
1047 Lo := Type_Low_Bound (Indtyp);
1048 Hi := Type_High_Bound (Indtyp);
1050 if Is_Entity_Name (Lo)
1051 and then Ekind (Entity (Lo)) = E_Discriminant
1052 then
1053 return False;
1055 elsif Is_Entity_Name (Hi)
1056 and then Ekind (Entity (Hi)) = E_Discriminant
1057 then
1058 return False;
1059 end if;
1060 end if;
1062 Next_Index (Ind);
1063 end loop;
1064 end;
1065 end if;
1067 -- Clearly size of record is not known if the size of one of
1068 -- the components is not known.
1070 if not Size_Known (Ctyp) then
1071 return False;
1072 end if;
1074 -- Accumulate packed size if possible
1076 if Packed_Size_Known then
1078 -- We can deal with elementary types, small packed arrays
1079 -- if the representation is a modular type and also small
1080 -- record types (if the size is not greater than 64, but
1081 -- the condition is checked by Set_Small_Size).
1083 if Is_Elementary_Type (Ctyp)
1084 or else (Is_Array_Type (Ctyp)
1085 and then Present
1086 (Packed_Array_Impl_Type (Ctyp))
1087 and then Is_Modular_Integer_Type
1088 (Packed_Array_Impl_Type (Ctyp)))
1089 or else Is_Record_Type (Ctyp)
1090 then
1091 -- If RM_Size is known and static, then we can keep
1092 -- accumulating the packed size.
1094 if Known_Static_RM_Size (Ctyp) then
1096 Packed_Size := Packed_Size + RM_Size (Ctyp);
1098 -- If we have a field whose RM_Size is not known then
1099 -- we can't figure out the packed size here.
1101 else
1102 Packed_Size_Known := False;
1103 end if;
1105 -- For other types we can't figure out the packed size
1107 else
1108 Packed_Size_Known := False;
1109 end if;
1110 end if;
1112 Next_Component_Or_Discriminant (Comp);
1113 end loop;
1115 if Packed_Size_Known then
1116 Set_Small_Size (T, Packed_Size);
1117 end if;
1119 return True;
1120 end;
1122 -- All other cases, size not known at compile time
1124 else
1125 return False;
1126 end if;
1127 end Size_Known;
1129 -------------------------------------
1130 -- Static_Discriminated_Components --
1131 -------------------------------------
1133 function Static_Discriminated_Components
1134 (T : Entity_Id) return Boolean
1136 Constraint : Elmt_Id;
1138 begin
1139 if Has_Discriminants (T)
1140 and then Present (Discriminant_Constraint (T))
1141 and then Present (First_Component (T))
1142 then
1143 Constraint := First_Elmt (Discriminant_Constraint (T));
1144 while Present (Constraint) loop
1145 if not Compile_Time_Known_Value (Node (Constraint)) then
1146 return False;
1147 end if;
1149 Next_Elmt (Constraint);
1150 end loop;
1151 end if;
1153 return True;
1154 end Static_Discriminated_Components;
1156 -- Start of processing for Check_Compile_Time_Size
1158 begin
1159 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1160 end Check_Compile_Time_Size;
1162 -----------------------------------
1163 -- Check_Component_Storage_Order --
1164 -----------------------------------
1166 procedure Check_Component_Storage_Order
1167 (Encl_Type : Entity_Id;
1168 Comp : Entity_Id;
1169 ADC : Node_Id;
1170 Comp_ADC_Present : out Boolean)
1172 Comp_Base : Entity_Id;
1173 Comp_ADC : Node_Id;
1174 Encl_Base : Entity_Id;
1175 Err_Node : Node_Id;
1177 Component_Aliased : Boolean;
1179 Comp_Byte_Aligned : Boolean := False;
1180 -- Set for the record case, True if Comp is aligned on byte boundaries
1181 -- (in which case it is allowed to have different storage order).
1183 Comp_SSO_Differs : Boolean;
1184 -- Set True when the component is a nested composite, and it does not
1185 -- have the same scalar storage order as Encl_Type.
1187 begin
1188 -- Record case
1190 if Present (Comp) then
1191 Err_Node := Comp;
1192 Comp_Base := Etype (Comp);
1194 if Is_Tag (Comp) then
1195 Comp_Byte_Aligned := True;
1196 Component_Aliased := False;
1198 else
1199 -- If a component clause is present, check if the component starts
1200 -- and ends on byte boundaries. Otherwise conservatively assume it
1201 -- does so only in the case where the record is not packed.
1203 if Present (Component_Clause (Comp)) then
1204 Comp_Byte_Aligned :=
1205 (Normalized_First_Bit (Comp) mod System_Storage_Unit = 0)
1206 and then
1207 (Esize (Comp) mod System_Storage_Unit = 0);
1208 else
1209 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1210 end if;
1212 Component_Aliased := Is_Aliased (Comp);
1213 end if;
1215 -- Array case
1217 else
1218 Err_Node := Encl_Type;
1219 Comp_Base := Component_Type (Encl_Type);
1221 Component_Aliased := Has_Aliased_Components (Encl_Type);
1222 end if;
1224 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1225 -- the attribute definition clause is attached to the first subtype.
1226 -- Also, if the base type is incomplete or private, go to full view
1227 -- if known
1229 Encl_Base := Base_Type (Encl_Type);
1230 if Present (Underlying_Type (Encl_Base)) then
1231 Encl_Base := Underlying_Type (Encl_Base);
1232 end if;
1234 Comp_Base := Base_Type (Comp_Base);
1235 if Present (Underlying_Type (Comp_Base)) then
1236 Comp_Base := Underlying_Type (Comp_Base);
1237 end if;
1239 Comp_ADC :=
1240 Get_Attribute_Definition_Clause
1241 (First_Subtype (Comp_Base), Attribute_Scalar_Storage_Order);
1242 Comp_ADC_Present := Present (Comp_ADC);
1244 -- Case of record or array component: check storage order compatibility.
1245 -- But, if the record has Complex_Representation, then it is treated as
1246 -- a scalar in the back end so the storage order is irrelevant.
1248 if (Is_Record_Type (Comp_Base)
1249 and then not Has_Complex_Representation (Comp_Base))
1250 or else Is_Array_Type (Comp_Base)
1251 then
1252 Comp_SSO_Differs :=
1253 Reverse_Storage_Order (Encl_Base) /=
1254 Reverse_Storage_Order (Comp_Base);
1256 -- Parent and extension must have same storage order
1258 if Present (Comp) and then Chars (Comp) = Name_uParent then
1259 if Comp_SSO_Differs then
1260 Error_Msg_N
1261 ("record extension must have same scalar storage order as "
1262 & "parent", Err_Node);
1263 end if;
1265 -- If component and composite SSO differs, check that component
1266 -- falls on byte boundaries and isn't bit packed.
1268 elsif Comp_SSO_Differs then
1270 -- Component SSO differs from enclosing composite:
1272 -- Reject if composite is a bit-packed array, as it is rewritten
1273 -- into an array of scalars.
1275 if Is_Bit_Packed_Array (Encl_Base) then
1276 Error_Msg_N
1277 ("type of packed array must have same scalar storage order "
1278 & "as component", Err_Node);
1280 -- Reject if not byte aligned
1282 elsif Is_Record_Type (Encl_Base)
1283 and then not Comp_Byte_Aligned
1284 then
1285 Error_Msg_N
1286 ("type of non-byte-aligned component must have same scalar "
1287 & "storage order as enclosing composite", Err_Node);
1289 -- Warn if specified only for the outer composite
1291 elsif Present (ADC) and then No (Comp_ADC) then
1292 Error_Msg_NE
1293 ("scalar storage order specified for & does not apply to "
1294 & "component?", Err_Node, Encl_Base);
1295 end if;
1296 end if;
1298 -- Enclosing type has explicit SSO: non-composite component must not
1299 -- be aliased.
1301 elsif Present (ADC) and then Component_Aliased then
1302 Error_Msg_N
1303 ("aliased component not permitted for type with explicit "
1304 & "Scalar_Storage_Order", Err_Node);
1305 end if;
1306 end Check_Component_Storage_Order;
1308 -----------------------------
1309 -- Check_Debug_Info_Needed --
1310 -----------------------------
1312 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1313 begin
1314 if Debug_Info_Off (T) then
1315 return;
1317 elsif Comes_From_Source (T)
1318 or else Debug_Generated_Code
1319 or else Debug_Flag_VV
1320 or else Needs_Debug_Info (T)
1321 then
1322 Set_Debug_Info_Needed (T);
1323 end if;
1324 end Check_Debug_Info_Needed;
1326 -------------------------------
1327 -- Check_Expression_Function --
1328 -------------------------------
1330 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1331 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1332 -- Function to search for deferred constant
1334 -------------------
1335 -- Find_Constant --
1336 -------------------
1338 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1339 begin
1340 -- When a constant is initialized with the result of a dispatching
1341 -- call, the constant declaration is rewritten as a renaming of the
1342 -- displaced function result. This scenario is not a premature use of
1343 -- a constant even though the Has_Completion flag is not set.
1345 if Is_Entity_Name (Nod)
1346 and then Present (Entity (Nod))
1347 and then Ekind (Entity (Nod)) = E_Constant
1348 and then Scope (Entity (Nod)) = Current_Scope
1349 and then Nkind (Declaration_Node (Entity (Nod))) =
1350 N_Object_Declaration
1351 and then not Is_Imported (Entity (Nod))
1352 and then not Has_Completion (Entity (Nod))
1353 and then not Is_Frozen (Entity (Nod))
1354 then
1355 Error_Msg_NE
1356 ("premature use of& in call or instance", N, Entity (Nod));
1358 elsif Nkind (Nod) = N_Attribute_Reference then
1359 Analyze (Prefix (Nod));
1361 if Is_Entity_Name (Prefix (Nod))
1362 and then Is_Type (Entity (Prefix (Nod)))
1363 then
1364 Freeze_Before (N, Entity (Prefix (Nod)));
1365 end if;
1366 end if;
1368 return OK;
1369 end Find_Constant;
1371 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1373 -- Local variables
1375 Decl : Node_Id;
1377 -- Start of processing for Check_Expression_Function
1379 begin
1380 Decl := Original_Node (Unit_Declaration_Node (Nam));
1382 -- The subprogram body created for the expression function is not
1383 -- itself a freeze point.
1385 if Scope (Nam) = Current_Scope
1386 and then Nkind (Decl) = N_Expression_Function
1387 and then Nkind (N) /= N_Subprogram_Body
1388 then
1389 Check_Deferred (Expression (Decl));
1390 end if;
1391 end Check_Expression_Function;
1393 --------------------------------
1394 -- Check_Inherited_Conditions --
1395 --------------------------------
1397 procedure Check_Inherited_Conditions (R : Entity_Id) is
1398 Prim_Ops : constant Elist_Id := Primitive_Operations (R);
1399 Decls : List_Id;
1400 Needs_Wrapper : Boolean;
1401 Op_Node : Elmt_Id;
1402 Par_Prim : Entity_Id;
1403 Prim : Entity_Id;
1405 procedure Build_Inherited_Condition_Pragmas (Subp : Entity_Id);
1406 -- Build corresponding pragmas for an operation whose ancestor has
1407 -- class-wide pre/postconditions. If the operation is inherited, the
1408 -- pragmas force the creation of a wrapper for the inherited operation.
1409 -- If the ancestor is being overridden, the pragmas are constructed only
1410 -- to verify their legality, in case they contain calls to other
1411 -- primitives that may haven been overridden.
1413 ---------------------------------------
1414 -- Build_Inherited_Condition_Pragmas --
1415 ---------------------------------------
1417 procedure Build_Inherited_Condition_Pragmas (Subp : Entity_Id) is
1418 A_Post : Node_Id;
1419 A_Pre : Node_Id;
1420 New_Prag : Node_Id;
1422 begin
1423 A_Pre := Get_Class_Wide_Pragma (Par_Prim, Pragma_Precondition);
1425 if Present (A_Pre) then
1426 New_Prag := New_Copy_Tree (A_Pre);
1427 Build_Class_Wide_Expression
1428 (Prag => New_Prag,
1429 Subp => Prim,
1430 Par_Subp => Par_Prim,
1431 Adjust_Sloc => False,
1432 Needs_Wrapper => Needs_Wrapper);
1434 if Needs_Wrapper
1435 and then not Comes_From_Source (Subp)
1436 and then Expander_Active
1437 then
1438 Append (New_Prag, Decls);
1439 end if;
1440 end if;
1442 A_Post := Get_Class_Wide_Pragma (Par_Prim, Pragma_Postcondition);
1444 if Present (A_Post) then
1445 New_Prag := New_Copy_Tree (A_Post);
1446 Build_Class_Wide_Expression
1447 (Prag => New_Prag,
1448 Subp => Prim,
1449 Par_Subp => Par_Prim,
1450 Adjust_Sloc => False,
1451 Needs_Wrapper => Needs_Wrapper);
1453 if Needs_Wrapper
1454 and then not Comes_From_Source (Subp)
1455 and then Expander_Active
1456 then
1457 Append (New_Prag, Decls);
1458 end if;
1459 end if;
1460 end Build_Inherited_Condition_Pragmas;
1462 -- Start of processing for Check_Inherited_Conditions
1464 begin
1465 Op_Node := First_Elmt (Prim_Ops);
1466 while Present (Op_Node) loop
1467 Prim := Node (Op_Node);
1469 -- Map the overridden primitive to the overriding one. This takes
1470 -- care of all overridings and is done only once.
1472 if Present (Overridden_Operation (Prim))
1473 and then Comes_From_Source (Prim)
1474 then
1475 Par_Prim := Overridden_Operation (Prim);
1476 Update_Primitives_Mapping (Par_Prim, Prim);
1477 end if;
1479 Next_Elmt (Op_Node);
1480 end loop;
1482 -- Perform validity checks on the inherited conditions of overriding
1483 -- operations, for conformance with LSP, and apply SPARK-specific
1484 -- restrictions on inherited conditions.
1486 Op_Node := First_Elmt (Prim_Ops);
1487 while Present (Op_Node) loop
1488 Prim := Node (Op_Node);
1490 if Present (Overridden_Operation (Prim))
1491 and then Comes_From_Source (Prim)
1492 then
1493 Par_Prim := Overridden_Operation (Prim);
1495 -- Analyze the contract items of the overridden operation, before
1496 -- they are rewritten as pragmas.
1498 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1500 -- In GNATprove mode this is where we can collect the inherited
1501 -- conditions, because we do not create the Check pragmas that
1502 -- normally convey the the modified class-wide conditions on
1503 -- overriding operations.
1505 if GNATprove_Mode then
1506 Collect_Inherited_Class_Wide_Conditions (Prim);
1508 -- Otherwise build the corresponding pragmas to check for legality
1509 -- of the inherited condition.
1511 else
1512 Build_Inherited_Condition_Pragmas (Prim);
1513 end if;
1514 end if;
1516 Next_Elmt (Op_Node);
1517 end loop;
1519 -- Now examine the inherited operations to check whether they require
1520 -- a wrapper to handle inherited conditions that call other primitives,
1521 -- so that LSP can be verified/enforced.
1523 Op_Node := First_Elmt (Prim_Ops);
1524 Needs_Wrapper := False;
1526 while Present (Op_Node) loop
1527 Decls := Empty_List;
1528 Prim := Node (Op_Node);
1530 if not Comes_From_Source (Prim) and then Present (Alias (Prim)) then
1531 Par_Prim := Alias (Prim);
1533 -- Analyze the contract items of the parent operation, and
1534 -- determine whether a wrapper is needed. This is determined
1535 -- when the condition is rewritten in sem_prag, using the
1536 -- mapping between overridden and overriding operations built
1537 -- in the loop above.
1539 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1540 Build_Inherited_Condition_Pragmas (Prim);
1541 end if;
1543 if Needs_Wrapper
1544 and then not Is_Abstract_Subprogram (Par_Prim)
1545 and then Expander_Active
1546 then
1547 -- We need to build a new primitive that overrides the inherited
1548 -- one, and whose inherited expression has been updated above.
1549 -- These expressions are the arguments of pragmas that are part
1550 -- of the declarations of the wrapper. The wrapper holds a single
1551 -- statement that is a call to the class-wide clone, where the
1552 -- controlling actuals are conversions to the corresponding type
1553 -- in the parent primitive:
1555 -- procedure New_Prim (F1 : T1; ...);
1556 -- procedure New_Prim (F1 : T1; ...) is
1557 -- pragma Check (Precondition, Expr);
1558 -- begin
1559 -- Par_Prim_Clone (Par_Type (F1), ...);
1560 -- end;
1562 -- If the primitive is a function the statement is a return
1563 -- statement with a call.
1565 declare
1566 Loc : constant Source_Ptr := Sloc (R);
1567 Par_R : constant Node_Id := Parent (R);
1568 New_Body : Node_Id;
1569 New_Decl : Node_Id;
1570 New_Spec : Node_Id;
1572 begin
1573 New_Spec := Build_Overriding_Spec (Par_Prim, R);
1574 New_Decl :=
1575 Make_Subprogram_Declaration (Loc,
1576 Specification => New_Spec);
1578 -- Insert the declaration and the body of the wrapper after
1579 -- type declaration that generates inherited operation. For
1580 -- a null procedure, the declaration implies a null body.
1582 if Nkind (New_Spec) = N_Procedure_Specification
1583 and then Null_Present (New_Spec)
1584 then
1585 Insert_After_And_Analyze (Par_R, New_Decl);
1587 else
1588 -- Build body as wrapper to a call to the already built
1589 -- class-wide clone.
1591 New_Body :=
1592 Build_Class_Wide_Clone_Call
1593 (Loc, Decls, Par_Prim, New_Spec);
1595 Insert_List_After_And_Analyze
1596 (Par_R, New_List (New_Decl, New_Body));
1597 end if;
1598 end;
1600 Needs_Wrapper := False;
1601 end if;
1603 Next_Elmt (Op_Node);
1604 end loop;
1605 end Check_Inherited_Conditions;
1607 ----------------------------
1608 -- Check_Strict_Alignment --
1609 ----------------------------
1611 procedure Check_Strict_Alignment (E : Entity_Id) is
1612 Comp : Entity_Id;
1614 begin
1615 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1616 Set_Strict_Alignment (E);
1618 elsif Is_Array_Type (E) then
1619 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1621 elsif Is_Record_Type (E) then
1622 if Is_Limited_Record (E) then
1623 Set_Strict_Alignment (E);
1624 return;
1625 end if;
1627 Comp := First_Component (E);
1628 while Present (Comp) loop
1629 if not Is_Type (Comp)
1630 and then (Strict_Alignment (Etype (Comp))
1631 or else Is_Aliased (Comp))
1632 then
1633 Set_Strict_Alignment (E);
1634 return;
1635 end if;
1637 Next_Component (Comp);
1638 end loop;
1639 end if;
1640 end Check_Strict_Alignment;
1642 -------------------------
1643 -- Check_Unsigned_Type --
1644 -------------------------
1646 procedure Check_Unsigned_Type (E : Entity_Id) is
1647 Ancestor : Entity_Id;
1648 Lo_Bound : Node_Id;
1649 Btyp : Entity_Id;
1651 begin
1652 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1653 return;
1654 end if;
1656 -- Do not attempt to analyze case where range was in error
1658 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
1659 return;
1660 end if;
1662 -- The situation that is nontrivial is something like:
1664 -- subtype x1 is integer range -10 .. +10;
1665 -- subtype x2 is x1 range 0 .. V1;
1666 -- subtype x3 is x2 range V2 .. V3;
1667 -- subtype x4 is x3 range V4 .. V5;
1669 -- where Vn are variables. Here the base type is signed, but we still
1670 -- know that x4 is unsigned because of the lower bound of x2.
1672 -- The only way to deal with this is to look up the ancestor chain
1674 Ancestor := E;
1675 loop
1676 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1677 return;
1678 end if;
1680 Lo_Bound := Type_Low_Bound (Ancestor);
1682 if Compile_Time_Known_Value (Lo_Bound) then
1683 if Expr_Rep_Value (Lo_Bound) >= 0 then
1684 Set_Is_Unsigned_Type (E, True);
1685 end if;
1687 return;
1689 else
1690 Ancestor := Ancestor_Subtype (Ancestor);
1692 -- If no ancestor had a static lower bound, go to base type
1694 if No (Ancestor) then
1696 -- Note: the reason we still check for a compile time known
1697 -- value for the base type is that at least in the case of
1698 -- generic formals, we can have bounds that fail this test,
1699 -- and there may be other cases in error situations.
1701 Btyp := Base_Type (E);
1703 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1704 return;
1705 end if;
1707 Lo_Bound := Type_Low_Bound (Base_Type (E));
1709 if Compile_Time_Known_Value (Lo_Bound)
1710 and then Expr_Rep_Value (Lo_Bound) >= 0
1711 then
1712 Set_Is_Unsigned_Type (E, True);
1713 end if;
1715 return;
1716 end if;
1717 end if;
1718 end loop;
1719 end Check_Unsigned_Type;
1721 -----------------------------
1722 -- Is_Atomic_VFA_Aggregate --
1723 -----------------------------
1725 function Is_Atomic_VFA_Aggregate (N : Node_Id) return Boolean is
1726 Loc : constant Source_Ptr := Sloc (N);
1727 New_N : Node_Id;
1728 Par : Node_Id;
1729 Temp : Entity_Id;
1730 Typ : Entity_Id;
1732 begin
1733 Par := Parent (N);
1735 -- Array may be qualified, so find outer context
1737 if Nkind (Par) = N_Qualified_Expression then
1738 Par := Parent (Par);
1739 end if;
1741 if not Comes_From_Source (Par) then
1742 return False;
1743 end if;
1745 case Nkind (Par) is
1746 when N_Assignment_Statement =>
1747 Typ := Etype (Name (Par));
1749 if not Is_Atomic_Or_VFA (Typ)
1750 and then not (Is_Entity_Name (Name (Par))
1751 and then Is_Atomic_Or_VFA (Entity (Name (Par))))
1752 then
1753 return False;
1754 end if;
1756 when N_Object_Declaration =>
1757 Typ := Etype (Defining_Identifier (Par));
1759 if not Is_Atomic_Or_VFA (Typ)
1760 and then not Is_Atomic_Or_VFA (Defining_Identifier (Par))
1761 then
1762 return False;
1763 end if;
1765 when others =>
1766 return False;
1767 end case;
1769 Temp := Make_Temporary (Loc, 'T', N);
1770 New_N :=
1771 Make_Object_Declaration (Loc,
1772 Defining_Identifier => Temp,
1773 Object_Definition => New_Occurrence_Of (Typ, Loc),
1774 Expression => Relocate_Node (N));
1775 Insert_Before (Par, New_N);
1776 Analyze (New_N);
1778 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1779 return True;
1780 end Is_Atomic_VFA_Aggregate;
1782 -----------------------------------------------
1783 -- Explode_Initialization_Compound_Statement --
1784 -----------------------------------------------
1786 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
1787 Init_Stmts : constant Node_Id := Initialization_Statements (E);
1789 begin
1790 if Present (Init_Stmts)
1791 and then Nkind (Init_Stmts) = N_Compound_Statement
1792 then
1793 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
1795 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
1796 -- just removing it, because Freeze_All may rely on this particular
1797 -- Node_Id still being present in the enclosing list to know where to
1798 -- stop freezing.
1800 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
1802 Set_Initialization_Statements (E, Empty);
1803 end if;
1804 end Explode_Initialization_Compound_Statement;
1806 ----------------
1807 -- Freeze_All --
1808 ----------------
1810 -- Note: the easy coding for this procedure would be to just build a
1811 -- single list of freeze nodes and then insert them and analyze them
1812 -- all at once. This won't work, because the analysis of earlier freeze
1813 -- nodes may recursively freeze types which would otherwise appear later
1814 -- on in the freeze list. So we must analyze and expand the freeze nodes
1815 -- as they are generated.
1817 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1818 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1819 -- This is the internal recursive routine that does freezing of entities
1820 -- (but NOT the analysis of default expressions, which should not be
1821 -- recursive, we don't want to analyze those till we are sure that ALL
1822 -- the types are frozen).
1824 --------------------
1825 -- Freeze_All_Ent --
1826 --------------------
1828 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
1829 E : Entity_Id;
1830 Flist : List_Id;
1831 Lastn : Node_Id;
1833 procedure Process_Flist;
1834 -- If freeze nodes are present, insert and analyze, and reset cursor
1835 -- for next insertion.
1837 -------------------
1838 -- Process_Flist --
1839 -------------------
1841 procedure Process_Flist is
1842 begin
1843 if Is_Non_Empty_List (Flist) then
1844 Lastn := Next (After);
1845 Insert_List_After_And_Analyze (After, Flist);
1847 if Present (Lastn) then
1848 After := Prev (Lastn);
1849 else
1850 After := Last (List_Containing (After));
1851 end if;
1852 end if;
1853 end Process_Flist;
1855 -- Start of processing for Freeze_All_Ent
1857 begin
1858 E := From;
1859 while Present (E) loop
1861 -- If the entity is an inner package which is not a package
1862 -- renaming, then its entities must be frozen at this point. Note
1863 -- that such entities do NOT get frozen at the end of the nested
1864 -- package itself (only library packages freeze).
1866 -- Same is true for task declarations, where anonymous records
1867 -- created for entry parameters must be frozen.
1869 if Ekind (E) = E_Package
1870 and then No (Renamed_Object (E))
1871 and then not Is_Child_Unit (E)
1872 and then not Is_Frozen (E)
1873 then
1874 Push_Scope (E);
1876 Install_Visible_Declarations (E);
1877 Install_Private_Declarations (E);
1878 Freeze_All (First_Entity (E), After);
1880 End_Package_Scope (E);
1882 if Is_Generic_Instance (E)
1883 and then Has_Delayed_Freeze (E)
1884 then
1885 Set_Has_Delayed_Freeze (E, False);
1886 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1887 end if;
1889 elsif Ekind (E) in Task_Kind
1890 and then Nkind_In (Parent (E), N_Single_Task_Declaration,
1891 N_Task_Type_Declaration)
1892 then
1893 Push_Scope (E);
1894 Freeze_All (First_Entity (E), After);
1895 End_Scope;
1897 -- For a derived tagged type, we must ensure that all the
1898 -- primitive operations of the parent have been frozen, so that
1899 -- their addresses will be in the parent's dispatch table at the
1900 -- point it is inherited.
1902 elsif Ekind (E) = E_Record_Type
1903 and then Is_Tagged_Type (E)
1904 and then Is_Tagged_Type (Etype (E))
1905 and then Is_Derived_Type (E)
1906 then
1907 declare
1908 Prim_List : constant Elist_Id :=
1909 Primitive_Operations (Etype (E));
1911 Prim : Elmt_Id;
1912 Subp : Entity_Id;
1914 begin
1915 Prim := First_Elmt (Prim_List);
1916 while Present (Prim) loop
1917 Subp := Node (Prim);
1919 if Comes_From_Source (Subp)
1920 and then not Is_Frozen (Subp)
1921 then
1922 Flist := Freeze_Entity (Subp, After);
1923 Process_Flist;
1924 end if;
1926 Next_Elmt (Prim);
1927 end loop;
1928 end;
1929 end if;
1931 if not Is_Frozen (E) then
1932 Flist := Freeze_Entity (E, After);
1933 Process_Flist;
1935 -- If already frozen, and there are delayed aspects, this is where
1936 -- we do the visibility check for these aspects (see Sem_Ch13 spec
1937 -- for a description of how we handle aspect visibility).
1939 elsif Has_Delayed_Aspects (E) then
1941 -- Retrieve the visibility to the discriminants in order to
1942 -- analyze properly the aspects.
1944 Push_Scope_And_Install_Discriminants (E);
1946 declare
1947 Ritem : Node_Id;
1949 begin
1950 Ritem := First_Rep_Item (E);
1951 while Present (Ritem) loop
1952 if Nkind (Ritem) = N_Aspect_Specification
1953 and then Entity (Ritem) = E
1954 and then Is_Delayed_Aspect (Ritem)
1955 then
1956 Check_Aspect_At_End_Of_Declarations (Ritem);
1957 end if;
1959 Ritem := Next_Rep_Item (Ritem);
1960 end loop;
1961 end;
1963 Uninstall_Discriminants_And_Pop_Scope (E);
1964 end if;
1966 -- If an incomplete type is still not frozen, this may be a
1967 -- premature freezing because of a body declaration that follows.
1968 -- Indicate where the freezing took place. Freezing will happen
1969 -- if the body comes from source, but not if it is internally
1970 -- generated, for example as the body of a type invariant.
1972 -- If the freezing is caused by the end of the current declarative
1973 -- part, it is a Taft Amendment type, and there is no error.
1975 if not Is_Frozen (E)
1976 and then Ekind (E) = E_Incomplete_Type
1977 then
1978 declare
1979 Bod : constant Node_Id := Next (After);
1981 begin
1982 -- The presence of a body freezes all entities previously
1983 -- declared in the current list of declarations, but this
1984 -- does not apply if the body does not come from source.
1985 -- A type invariant is transformed into a subprogram body
1986 -- which is placed at the end of the private part of the
1987 -- current package, but this body does not freeze incomplete
1988 -- types that may be declared in this private part.
1990 if (Nkind_In (Bod, N_Entry_Body,
1991 N_Package_Body,
1992 N_Protected_Body,
1993 N_Subprogram_Body,
1994 N_Task_Body)
1995 or else Nkind (Bod) in N_Body_Stub)
1996 and then
1997 List_Containing (After) = List_Containing (Parent (E))
1998 and then Comes_From_Source (Bod)
1999 then
2000 Error_Msg_Sloc := Sloc (Next (After));
2001 Error_Msg_NE
2002 ("type& is frozen# before its full declaration",
2003 Parent (E), E);
2004 end if;
2005 end;
2006 end if;
2008 Next_Entity (E);
2009 end loop;
2010 end Freeze_All_Ent;
2012 -- Local variables
2014 Decl : Node_Id;
2015 E : Entity_Id;
2016 Item : Entity_Id;
2018 -- Start of processing for Freeze_All
2020 begin
2021 Freeze_All_Ent (From, After);
2023 -- Now that all types are frozen, we can deal with default expressions
2024 -- that require us to build a default expression functions. This is the
2025 -- point at which such functions are constructed (after all types that
2026 -- might be used in such expressions have been frozen).
2028 -- For subprograms that are renaming_as_body, we create the wrapper
2029 -- bodies as needed.
2031 -- We also add finalization chains to access types whose designated
2032 -- types are controlled. This is normally done when freezing the type,
2033 -- but this misses recursive type definitions where the later members
2034 -- of the recursion introduce controlled components.
2036 -- Loop through entities
2038 E := From;
2039 while Present (E) loop
2040 if Is_Subprogram (E) then
2041 if not Default_Expressions_Processed (E) then
2042 Process_Default_Expressions (E, After);
2043 end if;
2045 if not Has_Completion (E) then
2046 Decl := Unit_Declaration_Node (E);
2048 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
2049 if Error_Posted (Decl) then
2050 Set_Has_Completion (E);
2051 else
2052 Build_And_Analyze_Renamed_Body (Decl, E, After);
2053 end if;
2055 elsif Nkind (Decl) = N_Subprogram_Declaration
2056 and then Present (Corresponding_Body (Decl))
2057 and then
2058 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl))) =
2059 N_Subprogram_Renaming_Declaration
2060 then
2061 Build_And_Analyze_Renamed_Body
2062 (Decl, Corresponding_Body (Decl), After);
2063 end if;
2064 end if;
2066 -- Freeze the default expressions of entries, entry families, and
2067 -- protected subprograms.
2069 elsif Is_Concurrent_Type (E) then
2070 Item := First_Entity (E);
2071 while Present (Item) loop
2072 if (Is_Entry (Item) or else Is_Subprogram (Item))
2073 and then not Default_Expressions_Processed (Item)
2074 then
2075 Process_Default_Expressions (Item, After);
2076 end if;
2078 Next_Entity (Item);
2079 end loop;
2080 end if;
2082 -- Historical note: We used to create a finalization master for an
2083 -- access type whose designated type is not controlled, but contains
2084 -- private controlled compoments. This form of postprocessing is no
2085 -- longer needed because the finalization master is now created when
2086 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
2088 Next_Entity (E);
2089 end loop;
2090 end Freeze_All;
2092 -----------------------
2093 -- Freeze_And_Append --
2094 -----------------------
2096 procedure Freeze_And_Append
2097 (Ent : Entity_Id;
2098 N : Node_Id;
2099 Result : in out List_Id)
2101 L : constant List_Id := Freeze_Entity (Ent, N);
2102 begin
2103 if Is_Non_Empty_List (L) then
2104 if Result = No_List then
2105 Result := L;
2106 else
2107 Append_List (L, Result);
2108 end if;
2109 end if;
2110 end Freeze_And_Append;
2112 -------------------
2113 -- Freeze_Before --
2114 -------------------
2116 procedure Freeze_Before
2117 (N : Node_Id;
2118 T : Entity_Id;
2119 Do_Freeze_Profile : Boolean := True)
2121 -- Freeze T, then insert the generated Freeze nodes before the node N.
2122 -- Flag Freeze_Profile is used when T is an overloadable entity, and
2123 -- indicates whether its profile should be frozen at the same time.
2125 Freeze_Nodes : constant List_Id :=
2126 Freeze_Entity (T, N, Do_Freeze_Profile);
2127 Pack : constant Entity_Id := Scope (T);
2129 begin
2130 if Ekind (T) = E_Function then
2131 Check_Expression_Function (N, T);
2132 end if;
2134 if Is_Non_Empty_List (Freeze_Nodes) then
2136 -- If the entity is a type declared in an inner package, it may be
2137 -- frozen by an outer declaration before the package itself is
2138 -- frozen. Install the package scope to analyze the freeze nodes,
2139 -- which may include generated subprograms such as predicate
2140 -- functions, etc.
2142 if Is_Type (T) and then From_Nested_Package (T) then
2143 Push_Scope (Pack);
2144 Install_Visible_Declarations (Pack);
2145 Install_Private_Declarations (Pack);
2146 Insert_Actions (N, Freeze_Nodes);
2147 End_Package_Scope (Pack);
2149 else
2150 Insert_Actions (N, Freeze_Nodes);
2151 end if;
2152 end if;
2153 end Freeze_Before;
2155 -------------------
2156 -- Freeze_Entity --
2157 -------------------
2159 -- WARNING: This routine manages Ghost regions. Return statements must be
2160 -- replaced by gotos which jump to the end of the routine and restore the
2161 -- Ghost mode.
2163 function Freeze_Entity
2164 (E : Entity_Id;
2165 N : Node_Id;
2166 Do_Freeze_Profile : Boolean := True) return List_Id
2168 Loc : constant Source_Ptr := Sloc (N);
2169 Atype : Entity_Id;
2170 Comp : Entity_Id;
2171 F_Node : Node_Id;
2172 Formal : Entity_Id;
2173 Indx : Node_Id;
2175 Has_Default_Initialization : Boolean := False;
2176 -- This flag gets set to true for a variable with default initialization
2178 Result : List_Id := No_List;
2179 -- List of freezing actions, left at No_List if none
2181 Test_E : Entity_Id := E;
2182 -- This could use a comment ???
2184 procedure Add_To_Result (N : Node_Id);
2185 -- N is a freezing action to be appended to the Result
2187 function After_Last_Declaration return Boolean;
2188 -- If Loc is a freeze_entity that appears after the last declaration
2189 -- in the scope, inhibit error messages on late completion.
2191 procedure Check_Current_Instance (Comp_Decl : Node_Id);
2192 -- Check that an Access or Unchecked_Access attribute with a prefix
2193 -- which is the current instance type can only be applied when the type
2194 -- is limited.
2196 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id);
2197 -- Give a warning for pragma Convention with language C or C++ applied
2198 -- to a discriminated record type. This is suppressed for the unchecked
2199 -- union case, since the whole point in this case is interface C. We
2200 -- also do not generate this within instantiations, since we will have
2201 -- generated a message on the template.
2203 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
2204 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
2205 -- integer literal without an explicit corresponding size clause. The
2206 -- caller has checked that Utype is a modular integer type.
2208 procedure Freeze_Array_Type (Arr : Entity_Id);
2209 -- Freeze array type, including freezing index and component types
2211 procedure Freeze_Object_Declaration (E : Entity_Id);
2212 -- Perform checks and generate freeze node if needed for a constant or
2213 -- variable declared by an object declaration.
2215 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
2216 -- Create Freeze_Generic_Entity nodes for types declared in a generic
2217 -- package. Recurse on inner generic packages.
2219 function Freeze_Profile (E : Entity_Id) return Boolean;
2220 -- Freeze formals and return type of subprogram. If some type in the
2221 -- profile is incomplete and we are in an instance, freezing of the
2222 -- entity will take place elsewhere, and the function returns False.
2224 procedure Freeze_Record_Type (Rec : Entity_Id);
2225 -- Freeze record type, including freezing component types, and freezing
2226 -- primitive operations if this is a tagged type.
2228 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2229 -- Determine whether an arbitrary entity is subject to Boolean aspect
2230 -- Import and its value is specified as True.
2232 procedure Inherit_Freeze_Node
2233 (Fnod : Node_Id;
2234 Typ : Entity_Id);
2235 -- Set type Typ's freeze node to refer to Fnode. This routine ensures
2236 -- that any attributes attached to Typ's original node are preserved.
2238 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2239 -- If E is an entity for an imported subprogram with pre/post-conditions
2240 -- then this procedure will create a wrapper to ensure that proper run-
2241 -- time checking of the pre/postconditions. See body for details.
2243 -------------------
2244 -- Add_To_Result --
2245 -------------------
2247 procedure Add_To_Result (N : Node_Id) is
2248 begin
2249 if No (Result) then
2250 Result := New_List (N);
2251 else
2252 Append (N, Result);
2253 end if;
2254 end Add_To_Result;
2256 ----------------------------
2257 -- After_Last_Declaration --
2258 ----------------------------
2260 function After_Last_Declaration return Boolean is
2261 Spec : constant Node_Id := Parent (Current_Scope);
2263 begin
2264 if Nkind (Spec) = N_Package_Specification then
2265 if Present (Private_Declarations (Spec)) then
2266 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2267 elsif Present (Visible_Declarations (Spec)) then
2268 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2269 else
2270 return False;
2271 end if;
2273 else
2274 return False;
2275 end if;
2276 end After_Last_Declaration;
2278 ----------------------------
2279 -- Check_Current_Instance --
2280 ----------------------------
2282 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2284 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2285 -- Determine whether Typ is compatible with the rules for aliased
2286 -- views of types as defined in RM 3.10 in the various dialects.
2288 function Process (N : Node_Id) return Traverse_Result;
2289 -- Process routine to apply check to given node
2291 -----------------------------
2292 -- Is_Aliased_View_Of_Type --
2293 -----------------------------
2295 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2296 Typ_Decl : constant Node_Id := Parent (Typ);
2298 begin
2299 -- Common case
2301 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2302 and then Limited_Present (Type_Definition (Typ_Decl))
2303 then
2304 return True;
2306 -- The following paragraphs describe what a legal aliased view of
2307 -- a type is in the various dialects of Ada.
2309 -- Ada 95
2311 -- The current instance of a limited type, and a formal parameter
2312 -- or generic formal object of a tagged type.
2314 -- Ada 95 limited type
2315 -- * Type with reserved word "limited"
2316 -- * A protected or task type
2317 -- * A composite type with limited component
2319 elsif Ada_Version <= Ada_95 then
2320 return Is_Limited_Type (Typ);
2322 -- Ada 2005
2324 -- The current instance of a limited tagged type, a protected
2325 -- type, a task type, or a type that has the reserved word
2326 -- "limited" in its full definition ... a formal parameter or
2327 -- generic formal object of a tagged type.
2329 -- Ada 2005 limited type
2330 -- * Type with reserved word "limited", "synchronized", "task"
2331 -- or "protected"
2332 -- * A composite type with limited component
2333 -- * A derived type whose parent is a non-interface limited type
2335 elsif Ada_Version = Ada_2005 then
2336 return
2337 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2338 or else
2339 (Is_Derived_Type (Typ)
2340 and then not Is_Interface (Etype (Typ))
2341 and then Is_Limited_Type (Etype (Typ)));
2343 -- Ada 2012 and beyond
2345 -- The current instance of an immutably limited type ... a formal
2346 -- parameter or generic formal object of a tagged type.
2348 -- Ada 2012 limited type
2349 -- * Type with reserved word "limited", "synchronized", "task"
2350 -- or "protected"
2351 -- * A composite type with limited component
2352 -- * A derived type whose parent is a non-interface limited type
2353 -- * An incomplete view
2355 -- Ada 2012 immutably limited type
2356 -- * Explicitly limited record type
2357 -- * Record extension with "limited" present
2358 -- * Non-formal limited private type that is either tagged
2359 -- or has at least one access discriminant with a default
2360 -- expression
2361 -- * Task type, protected type or synchronized interface
2362 -- * Type derived from immutably limited type
2364 else
2365 return
2366 Is_Immutably_Limited_Type (Typ)
2367 or else Is_Incomplete_Type (Typ);
2368 end if;
2369 end Is_Aliased_View_Of_Type;
2371 -------------
2372 -- Process --
2373 -------------
2375 function Process (N : Node_Id) return Traverse_Result is
2376 begin
2377 case Nkind (N) is
2378 when N_Attribute_Reference =>
2379 if Nam_In (Attribute_Name (N), Name_Access,
2380 Name_Unchecked_Access)
2381 and then Is_Entity_Name (Prefix (N))
2382 and then Is_Type (Entity (Prefix (N)))
2383 and then Entity (Prefix (N)) = E
2384 then
2385 if Ada_Version < Ada_2012 then
2386 Error_Msg_N
2387 ("current instance must be a limited type",
2388 Prefix (N));
2389 else
2390 Error_Msg_N
2391 ("current instance must be an immutably limited "
2392 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2393 end if;
2395 return Abandon;
2397 else
2398 return OK;
2399 end if;
2401 when others =>
2402 return OK;
2403 end case;
2404 end Process;
2406 procedure Traverse is new Traverse_Proc (Process);
2408 -- Local variables
2410 Rec_Type : constant Entity_Id :=
2411 Scope (Defining_Identifier (Comp_Decl));
2413 -- Start of processing for Check_Current_Instance
2415 begin
2416 if not Is_Aliased_View_Of_Type (Rec_Type) then
2417 Traverse (Comp_Decl);
2418 end if;
2419 end Check_Current_Instance;
2421 ---------------------------------
2422 -- Check_Suspicious_Convention --
2423 ---------------------------------
2425 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id) is
2426 begin
2427 if Has_Discriminants (Rec_Type)
2428 and then Is_Base_Type (Rec_Type)
2429 and then not Is_Unchecked_Union (Rec_Type)
2430 and then (Convention (Rec_Type) = Convention_C
2431 or else
2432 Convention (Rec_Type) = Convention_CPP)
2433 and then Comes_From_Source (Rec_Type)
2434 and then not In_Instance
2435 and then not Has_Warnings_Off (Rec_Type)
2436 then
2437 declare
2438 Cprag : constant Node_Id :=
2439 Get_Rep_Pragma (Rec_Type, Name_Convention);
2440 A2 : Node_Id;
2442 begin
2443 if Present (Cprag) then
2444 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
2446 if Convention (Rec_Type) = Convention_C then
2447 Error_Msg_N
2448 ("?x?discriminated record has no direct equivalent in "
2449 & "C", A2);
2450 else
2451 Error_Msg_N
2452 ("?x?discriminated record has no direct equivalent in "
2453 & "C++", A2);
2454 end if;
2456 Error_Msg_NE
2457 ("\?x?use of convention for type& is dubious",
2458 A2, Rec_Type);
2459 end if;
2460 end;
2461 end if;
2462 end Check_Suspicious_Convention;
2464 ------------------------------
2465 -- Check_Suspicious_Modulus --
2466 ------------------------------
2468 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
2469 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
2471 begin
2472 if not Warn_On_Suspicious_Modulus_Value then
2473 return;
2474 end if;
2476 if Nkind (Decl) = N_Full_Type_Declaration then
2477 declare
2478 Tdef : constant Node_Id := Type_Definition (Decl);
2480 begin
2481 if Nkind (Tdef) = N_Modular_Type_Definition then
2482 declare
2483 Modulus : constant Node_Id :=
2484 Original_Node (Expression (Tdef));
2486 begin
2487 if Nkind (Modulus) = N_Integer_Literal then
2488 declare
2489 Modv : constant Uint := Intval (Modulus);
2490 Sizv : constant Uint := RM_Size (Utype);
2492 begin
2493 -- First case, modulus and size are the same. This
2494 -- happens if you have something like mod 32, with
2495 -- an explicit size of 32, this is for sure a case
2496 -- where the warning is given, since it is seems
2497 -- very unlikely that someone would want e.g. a
2498 -- five bit type stored in 32 bits. It is much
2499 -- more likely they wanted a 32-bit type.
2501 if Modv = Sizv then
2502 null;
2504 -- Second case, the modulus is 32 or 64 and no
2505 -- size clause is present. This is a less clear
2506 -- case for giving the warning, but in the case
2507 -- of 32/64 (5-bit or 6-bit types) these seem rare
2508 -- enough that it is a likely error (and in any
2509 -- case using 2**5 or 2**6 in these cases seems
2510 -- clearer. We don't include 8 or 16 here, simply
2511 -- because in practice 3-bit and 4-bit types are
2512 -- more common and too many false positives if
2513 -- we warn in these cases.
2515 elsif not Has_Size_Clause (Utype)
2516 and then (Modv = Uint_32 or else Modv = Uint_64)
2517 then
2518 null;
2520 -- No warning needed
2522 else
2523 return;
2524 end if;
2526 -- If we fall through, give warning
2528 Error_Msg_Uint_1 := Modv;
2529 Error_Msg_N
2530 ("?M?2 '*'*^' may have been intended here",
2531 Modulus);
2532 end;
2533 end if;
2534 end;
2535 end if;
2536 end;
2537 end if;
2538 end Check_Suspicious_Modulus;
2540 -----------------------
2541 -- Freeze_Array_Type --
2542 -----------------------
2544 procedure Freeze_Array_Type (Arr : Entity_Id) is
2545 FS : constant Entity_Id := First_Subtype (Arr);
2546 Ctyp : constant Entity_Id := Component_Type (Arr);
2547 Clause : Entity_Id;
2549 Non_Standard_Enum : Boolean := False;
2550 -- Set true if any of the index types is an enumeration type with a
2551 -- non-standard representation.
2553 begin
2554 Freeze_And_Append (Ctyp, N, Result);
2556 Indx := First_Index (Arr);
2557 while Present (Indx) loop
2558 Freeze_And_Append (Etype (Indx), N, Result);
2560 if Is_Enumeration_Type (Etype (Indx))
2561 and then Has_Non_Standard_Rep (Etype (Indx))
2562 then
2563 Non_Standard_Enum := True;
2564 end if;
2566 Next_Index (Indx);
2567 end loop;
2569 -- Processing that is done only for base types
2571 if Ekind (Arr) = E_Array_Type then
2573 -- Deal with default setting of reverse storage order
2575 Set_SSO_From_Default (Arr);
2577 -- Propagate flags for component type
2579 if Is_Controlled (Component_Type (Arr))
2580 or else Has_Controlled_Component (Ctyp)
2581 then
2582 Set_Has_Controlled_Component (Arr);
2583 end if;
2585 if Has_Unchecked_Union (Component_Type (Arr)) then
2586 Set_Has_Unchecked_Union (Arr);
2587 end if;
2589 -- The array type requires its own invariant procedure in order to
2590 -- verify the component invariant over all elements. In GNATprove
2591 -- mode, the component invariants are checked by other means. They
2592 -- should not be added to the array type invariant procedure, so
2593 -- that the procedure can be used to check the array type
2594 -- invariants if any.
2596 if Has_Invariants (Component_Type (Arr))
2597 and then not GNATprove_Mode
2598 then
2599 Set_Has_Own_Invariants (Arr);
2601 -- The array type is an implementation base type. Propagate the
2602 -- same property to the first subtype.
2604 if Is_Itype (Arr) then
2605 Set_Has_Own_Invariants (First_Subtype (Arr));
2606 end if;
2607 end if;
2609 -- Warn for pragma Pack overriding foreign convention
2611 if Has_Foreign_Convention (Ctyp)
2612 and then Has_Pragma_Pack (Arr)
2613 then
2614 declare
2615 CN : constant Name_Id :=
2616 Get_Convention_Name (Convention (Ctyp));
2617 PP : constant Node_Id :=
2618 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
2619 begin
2620 if Present (PP) then
2621 Error_Msg_Name_1 := CN;
2622 Error_Msg_Sloc := Sloc (Arr);
2623 Error_Msg_N
2624 ("pragma Pack affects convention % components #??", PP);
2625 Error_Msg_Name_1 := CN;
2626 Error_Msg_N
2627 ("\array components may not have % compatible "
2628 & "representation??", PP);
2629 end if;
2630 end;
2631 end if;
2633 -- If packing was requested or if the component size was
2634 -- set explicitly, then see if bit packing is required. This
2635 -- processing is only done for base types, since all of the
2636 -- representation aspects involved are type-related.
2638 -- This is not just an optimization, if we start processing the
2639 -- subtypes, they interfere with the settings on the base type
2640 -- (this is because Is_Packed has a slightly different meaning
2641 -- before and after freezing).
2643 declare
2644 Csiz : Uint;
2645 Esiz : Uint;
2647 begin
2648 if (Is_Packed (Arr) or else Has_Pragma_Pack (Arr))
2649 and then Known_Static_RM_Size (Ctyp)
2650 and then not Has_Component_Size_Clause (Arr)
2651 then
2652 Csiz := UI_Max (RM_Size (Ctyp), 1);
2654 elsif Known_Component_Size (Arr) then
2655 Csiz := Component_Size (Arr);
2657 elsif not Known_Static_Esize (Ctyp) then
2658 Csiz := Uint_0;
2660 else
2661 Esiz := Esize (Ctyp);
2663 -- We can set the component size if it is less than 16,
2664 -- rounding it up to the next storage unit size.
2666 if Esiz <= 8 then
2667 Csiz := Uint_8;
2668 elsif Esiz <= 16 then
2669 Csiz := Uint_16;
2670 else
2671 Csiz := Uint_0;
2672 end if;
2674 -- Set component size up to match alignment if it would
2675 -- otherwise be less than the alignment. This deals with
2676 -- cases of types whose alignment exceeds their size (the
2677 -- padded type cases).
2679 if Csiz /= 0 then
2680 declare
2681 A : constant Uint := Alignment_In_Bits (Ctyp);
2682 begin
2683 if Csiz < A then
2684 Csiz := A;
2685 end if;
2686 end;
2687 end if;
2688 end if;
2690 -- Case of component size that may result in bit packing
2692 if 1 <= Csiz and then Csiz <= 64 then
2693 declare
2694 Ent : constant Entity_Id :=
2695 First_Subtype (Arr);
2696 Pack_Pragma : constant Node_Id :=
2697 Get_Rep_Pragma (Ent, Name_Pack);
2698 Comp_Size_C : constant Node_Id :=
2699 Get_Attribute_Definition_Clause
2700 (Ent, Attribute_Component_Size);
2702 begin
2703 -- Warn if we have pack and component size so that the
2704 -- pack is ignored.
2706 -- Note: here we must check for the presence of a
2707 -- component size before checking for a Pack pragma to
2708 -- deal with the case where the array type is a derived
2709 -- type whose parent is currently private.
2711 if Present (Comp_Size_C)
2712 and then Has_Pragma_Pack (Ent)
2713 and then Warn_On_Redundant_Constructs
2714 then
2715 Error_Msg_Sloc := Sloc (Comp_Size_C);
2716 Error_Msg_NE
2717 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
2718 Error_Msg_N
2719 ("\?r?explicit component size given#!", Pack_Pragma);
2720 Set_Is_Packed (Base_Type (Ent), False);
2721 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
2722 end if;
2724 -- Set component size if not already set by a component
2725 -- size clause.
2727 if not Present (Comp_Size_C) then
2728 Set_Component_Size (Arr, Csiz);
2729 end if;
2731 -- Check for base type of 8, 16, 32 bits, where an
2732 -- unsigned subtype has a length one less than the
2733 -- base type (e.g. Natural subtype of Integer).
2735 -- In such cases, if a component size was not set
2736 -- explicitly, then generate a warning.
2738 if Has_Pragma_Pack (Arr)
2739 and then not Present (Comp_Size_C)
2740 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2741 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2742 then
2743 Error_Msg_Uint_1 := Csiz;
2745 if Present (Pack_Pragma) then
2746 Error_Msg_N
2747 ("??pragma Pack causes component size to be ^!",
2748 Pack_Pragma);
2749 Error_Msg_N
2750 ("\??use Component_Size to set desired value!",
2751 Pack_Pragma);
2752 end if;
2753 end if;
2755 -- Bit packing is never needed for 8, 16, 32, 64
2757 if Addressable (Csiz) then
2759 -- If the Esize of the component is known and equal to
2760 -- the component size then even packing is not needed.
2762 if Known_Static_Esize (Component_Type (Arr))
2763 and then Esize (Component_Type (Arr)) = Csiz
2764 then
2765 -- Here the array was requested to be packed, but
2766 -- the packing request had no effect whatsoever,
2767 -- so flag Is_Packed is reset.
2769 -- Note: semantically this means that we lose track
2770 -- of the fact that a derived type inherited pragma
2771 -- Pack that was non-effective, but that is fine.
2773 -- We regard a Pack pragma as a request to set a
2774 -- representation characteristic, and this request
2775 -- may be ignored.
2777 Set_Is_Packed (Base_Type (Arr), False);
2778 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
2779 else
2780 Set_Is_Packed (Base_Type (Arr), True);
2781 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2782 end if;
2784 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2786 -- Bit packing is not needed for multiples of the storage
2787 -- unit if the type is composite because the back end can
2788 -- byte pack composite types.
2790 elsif Csiz mod System_Storage_Unit = 0
2791 and then Is_Composite_Type (Ctyp)
2792 then
2793 Set_Is_Packed (Base_Type (Arr), True);
2794 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2795 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2797 -- In all other cases, bit packing is needed
2799 else
2800 Set_Is_Packed (Base_Type (Arr), True);
2801 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2802 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
2803 end if;
2804 end;
2805 end if;
2806 end;
2808 -- Check for Aliased or Atomic_Components/Atomic/VFA with
2809 -- unsuitable packing or explicit component size clause given.
2811 if (Has_Aliased_Components (Arr)
2812 or else Has_Atomic_Components (Arr)
2813 or else Is_Atomic_Or_VFA (Ctyp))
2814 and then
2815 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2816 then
2817 Alias_Atomic_Check : declare
2819 procedure Complain_CS (T : String);
2820 -- Outputs error messages for incorrect CS clause or pragma
2821 -- Pack for aliased or atomic/VFA components (T is "aliased"
2822 -- or "atomic/vfa");
2824 -----------------
2825 -- Complain_CS --
2826 -----------------
2828 procedure Complain_CS (T : String) is
2829 begin
2830 if Has_Component_Size_Clause (Arr) then
2831 Clause :=
2832 Get_Attribute_Definition_Clause
2833 (FS, Attribute_Component_Size);
2835 Error_Msg_N
2836 ("incorrect component size for "
2837 & T & " components", Clause);
2838 Error_Msg_Uint_1 := Esize (Ctyp);
2839 Error_Msg_N
2840 ("\only allowed value is^", Clause);
2842 else
2843 Error_Msg_N
2844 ("cannot pack " & T & " components",
2845 Get_Rep_Pragma (FS, Name_Pack));
2846 end if;
2847 end Complain_CS;
2849 -- Start of processing for Alias_Atomic_Check
2851 begin
2852 -- If object size of component type isn't known, we cannot
2853 -- be sure so we defer to the back end.
2855 if not Known_Static_Esize (Ctyp) then
2856 null;
2858 -- Case where component size has no effect. First check for
2859 -- object size of component type multiple of the storage
2860 -- unit size.
2862 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2864 -- OK in both packing case and component size case if RM
2865 -- size is known and static and same as the object size.
2867 and then
2868 ((Known_Static_RM_Size (Ctyp)
2869 and then Esize (Ctyp) = RM_Size (Ctyp))
2871 -- Or if we have an explicit component size clause and
2872 -- the component size and object size are equal.
2874 or else
2875 (Has_Component_Size_Clause (Arr)
2876 and then Component_Size (Arr) = Esize (Ctyp)))
2877 then
2878 null;
2880 elsif Has_Aliased_Components (Arr) then
2881 Complain_CS ("aliased");
2883 elsif Has_Atomic_Components (Arr)
2884 or else Is_Atomic (Ctyp)
2885 then
2886 Complain_CS ("atomic");
2888 elsif Is_Volatile_Full_Access (Ctyp) then
2889 Complain_CS ("volatile full access");
2890 end if;
2891 end Alias_Atomic_Check;
2892 end if;
2894 -- Check for Independent_Components/Independent with unsuitable
2895 -- packing or explicit component size clause given.
2897 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
2898 and then
2899 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2900 then
2901 begin
2902 -- If object size of component type isn't known, we cannot
2903 -- be sure so we defer to the back end.
2905 if not Known_Static_Esize (Ctyp) then
2906 null;
2908 -- Case where component size has no effect. First check for
2909 -- object size of component type multiple of the storage
2910 -- unit size.
2912 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2914 -- OK in both packing case and component size case if RM
2915 -- size is known and multiple of the storage unit size.
2917 and then
2918 ((Known_Static_RM_Size (Ctyp)
2919 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
2921 -- Or if we have an explicit component size clause and
2922 -- the component size is larger than the object size.
2924 or else
2925 (Has_Component_Size_Clause (Arr)
2926 and then Component_Size (Arr) >= Esize (Ctyp)))
2927 then
2928 null;
2930 else
2931 if Has_Component_Size_Clause (Arr) then
2932 Clause :=
2933 Get_Attribute_Definition_Clause
2934 (FS, Attribute_Component_Size);
2936 Error_Msg_N
2937 ("incorrect component size for "
2938 & "independent components", Clause);
2939 Error_Msg_Uint_1 := Esize (Ctyp);
2940 Error_Msg_N
2941 ("\minimum allowed is^", Clause);
2943 else
2944 Error_Msg_N
2945 ("cannot pack independent components",
2946 Get_Rep_Pragma (FS, Name_Pack));
2947 end if;
2948 end if;
2949 end;
2950 end if;
2952 -- Warn for case of atomic type
2954 Clause := Get_Rep_Pragma (FS, Name_Atomic);
2956 if Present (Clause)
2957 and then not Addressable (Component_Size (FS))
2958 then
2959 Error_Msg_NE
2960 ("non-atomic components of type& may not be "
2961 & "accessible by separate tasks??", Clause, Arr);
2963 if Has_Component_Size_Clause (Arr) then
2964 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2965 (FS, Attribute_Component_Size));
2966 Error_Msg_N ("\because of component size clause#??", Clause);
2968 elsif Has_Pragma_Pack (Arr) then
2969 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2970 Error_Msg_N ("\because of pragma Pack#??", Clause);
2971 end if;
2972 end if;
2974 -- Check for scalar storage order
2976 declare
2977 Dummy : Boolean;
2978 begin
2979 Check_Component_Storage_Order
2980 (Encl_Type => Arr,
2981 Comp => Empty,
2982 ADC => Get_Attribute_Definition_Clause
2983 (First_Subtype (Arr),
2984 Attribute_Scalar_Storage_Order),
2985 Comp_ADC_Present => Dummy);
2986 end;
2988 -- Processing that is done only for subtypes
2990 else
2991 -- Acquire alignment from base type
2993 if Unknown_Alignment (Arr) then
2994 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
2995 Adjust_Esize_Alignment (Arr);
2996 end if;
2997 end if;
2999 -- Specific checks for bit-packed arrays
3001 if Is_Bit_Packed_Array (Arr) then
3003 -- Check number of elements for bit-packed arrays that come from
3004 -- source and have compile time known ranges. The bit-packed
3005 -- arrays circuitry does not support arrays with more than
3006 -- Integer'Last + 1 elements, and when this restriction is
3007 -- violated, causes incorrect data access.
3009 -- For the case where this is not compile time known, a run-time
3010 -- check should be generated???
3012 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
3013 declare
3014 Elmts : Uint;
3015 Index : Node_Id;
3016 Ilen : Node_Id;
3017 Ityp : Entity_Id;
3019 begin
3020 Elmts := Uint_1;
3021 Index := First_Index (Arr);
3022 while Present (Index) loop
3023 Ityp := Etype (Index);
3025 -- Never generate an error if any index is of a generic
3026 -- type. We will check this in instances.
3028 if Is_Generic_Type (Ityp) then
3029 Elmts := Uint_0;
3030 exit;
3031 end if;
3033 Ilen :=
3034 Make_Attribute_Reference (Loc,
3035 Prefix => New_Occurrence_Of (Ityp, Loc),
3036 Attribute_Name => Name_Range_Length);
3037 Analyze_And_Resolve (Ilen);
3039 -- No attempt is made to check number of elements if not
3040 -- compile time known.
3042 if Nkind (Ilen) /= N_Integer_Literal then
3043 Elmts := Uint_0;
3044 exit;
3045 end if;
3047 Elmts := Elmts * Intval (Ilen);
3048 Next_Index (Index);
3049 end loop;
3051 if Elmts > Intval (High_Bound
3052 (Scalar_Range (Standard_Integer))) + 1
3053 then
3054 Error_Msg_N
3055 ("bit packed array type may not have "
3056 & "more than Integer''Last+1 elements", Arr);
3057 end if;
3058 end;
3059 end if;
3061 -- Check size
3063 if Known_RM_Size (Arr) then
3064 declare
3065 SizC : constant Node_Id := Size_Clause (Arr);
3066 Discard : Boolean;
3068 begin
3069 -- It is not clear if it is possible to have no size clause
3070 -- at this stage, but it is not worth worrying about. Post
3071 -- error on the entity name in the size clause if present,
3072 -- else on the type entity itself.
3074 if Present (SizC) then
3075 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
3076 else
3077 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
3078 end if;
3079 end;
3080 end if;
3081 end if;
3083 -- If any of the index types was an enumeration type with a non-
3084 -- standard rep clause, then we indicate that the array type is
3085 -- always packed (even if it is not bit-packed).
3087 if Non_Standard_Enum then
3088 Set_Has_Non_Standard_Rep (Base_Type (Arr));
3089 Set_Is_Packed (Base_Type (Arr));
3090 end if;
3092 Set_Component_Alignment_If_Not_Set (Arr);
3094 -- If the array is packed and bit-packed or packed to eliminate holes
3095 -- in the non-contiguous enumeration index types, we must create the
3096 -- packed array type to be used to actually implement the type. This
3097 -- is only needed for real array types (not for string literal types,
3098 -- since they are present only for the front end).
3100 if Is_Packed (Arr)
3101 and then (Is_Bit_Packed_Array (Arr) or else Non_Standard_Enum)
3102 and then Ekind (Arr) /= E_String_Literal_Subtype
3103 then
3104 Create_Packed_Array_Impl_Type (Arr);
3105 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
3107 -- Make sure that we have the necessary routines to implement the
3108 -- packing, and complain now if not. Note that we only test this
3109 -- for constrained array types.
3111 if Is_Constrained (Arr)
3112 and then Is_Bit_Packed_Array (Arr)
3113 and then Present (Packed_Array_Impl_Type (Arr))
3114 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
3115 then
3116 declare
3117 CS : constant Uint := Component_Size (Arr);
3118 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
3120 begin
3121 if RE /= RE_Null
3122 and then not RTE_Available (RE)
3123 then
3124 Error_Msg_CRT
3125 ("packing of " & UI_Image (CS) & "-bit components",
3126 First_Subtype (Etype (Arr)));
3128 -- Cancel the packing
3130 Set_Is_Packed (Base_Type (Arr), False);
3131 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3132 Set_Packed_Array_Impl_Type (Arr, Empty);
3133 goto Skip_Packed;
3134 end if;
3135 end;
3136 end if;
3138 -- Size information of packed array type is copied to the array
3139 -- type, since this is really the representation. But do not
3140 -- override explicit existing size values. If the ancestor subtype
3141 -- is constrained the Packed_Array_Impl_Type will be inherited
3142 -- from it, but the size may have been provided already, and
3143 -- must not be overridden either.
3145 if not Has_Size_Clause (Arr)
3146 and then
3147 (No (Ancestor_Subtype (Arr))
3148 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
3149 then
3150 Set_Esize (Arr, Esize (Packed_Array_Impl_Type (Arr)));
3151 Set_RM_Size (Arr, RM_Size (Packed_Array_Impl_Type (Arr)));
3152 end if;
3154 if not Has_Alignment_Clause (Arr) then
3155 Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
3156 end if;
3157 end if;
3159 <<Skip_Packed>>
3161 -- For non-packed arrays set the alignment of the array to the
3162 -- alignment of the component type if it is unknown. Skip this
3163 -- in atomic/VFA case (atomic/VFA arrays may need larger alignments).
3165 if not Is_Packed (Arr)
3166 and then Unknown_Alignment (Arr)
3167 and then Known_Alignment (Ctyp)
3168 and then Known_Static_Component_Size (Arr)
3169 and then Known_Static_Esize (Ctyp)
3170 and then Esize (Ctyp) = Component_Size (Arr)
3171 and then not Is_Atomic_Or_VFA (Arr)
3172 then
3173 Set_Alignment (Arr, Alignment (Component_Type (Arr)));
3174 end if;
3176 -- A Ghost type cannot have a component of protected or task type
3177 -- (SPARK RM 6.9(19)).
3179 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
3180 Error_Msg_N
3181 ("ghost array type & cannot have concurrent component type",
3182 Arr);
3183 end if;
3184 end Freeze_Array_Type;
3186 -------------------------------
3187 -- Freeze_Object_Declaration --
3188 -------------------------------
3190 procedure Freeze_Object_Declaration (E : Entity_Id) is
3191 begin
3192 -- Abstract type allowed only for C++ imported variables or constants
3194 -- Note: we inhibit this check for objects that do not come from
3195 -- source because there is at least one case (the expansion of
3196 -- x'Class'Input where x is abstract) where we legitimately
3197 -- generate an abstract object.
3199 if Is_Abstract_Type (Etype (E))
3200 and then Comes_From_Source (Parent (E))
3201 and then not (Is_Imported (E) and then Is_CPP_Class (Etype (E)))
3202 then
3203 Error_Msg_N ("type of object cannot be abstract",
3204 Object_Definition (Parent (E)));
3206 if Is_CPP_Class (Etype (E)) then
3207 Error_Msg_NE
3208 ("\} may need a cpp_constructor",
3209 Object_Definition (Parent (E)), Etype (E));
3211 elsif Present (Expression (Parent (E))) then
3212 Error_Msg_N -- CODEFIX
3213 ("\maybe a class-wide type was meant",
3214 Object_Definition (Parent (E)));
3215 end if;
3216 end if;
3218 -- For object created by object declaration, perform required
3219 -- categorization (preelaborate and pure) checks. Defer these
3220 -- checks to freeze time since pragma Import inhibits default
3221 -- initialization and thus pragma Import affects these checks.
3223 Validate_Object_Declaration (Declaration_Node (E));
3225 -- If there is an address clause, check that it is valid
3226 -- and if need be move initialization to the freeze node.
3228 Check_Address_Clause (E);
3230 -- Similar processing is needed for aspects that may affect
3231 -- object layout, like Alignment, if there is an initialization
3232 -- expression. We don't do this if there is a pragma Linker_Section,
3233 -- because it would prevent the back end from statically initializing
3234 -- the object; we don't want elaboration code in that case.
3236 if Has_Delayed_Aspects (E)
3237 and then Expander_Active
3238 and then Is_Array_Type (Etype (E))
3239 and then Present (Expression (Parent (E)))
3240 and then No (Linker_Section_Pragma (E))
3241 then
3242 declare
3243 Decl : constant Node_Id := Parent (E);
3244 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
3246 begin
3248 -- Capture initialization value at point of declaration, and
3249 -- make explicit assignment legal, because object may be a
3250 -- constant.
3252 Remove_Side_Effects (Expression (Decl));
3253 Set_Assignment_OK (Lhs);
3255 -- Move initialization to freeze actions.
3257 Append_Freeze_Action (E,
3258 Make_Assignment_Statement (Loc,
3259 Name => Lhs,
3260 Expression => Expression (Decl)));
3262 Set_No_Initialization (Decl);
3263 -- Set_Is_Frozen (E, False);
3264 end;
3265 end if;
3267 -- Reset Is_True_Constant for non-constant aliased object. We
3268 -- consider that the fact that a non-constant object is aliased may
3269 -- indicate that some funny business is going on, e.g. an aliased
3270 -- object is passed by reference to a procedure which captures the
3271 -- address of the object, which is later used to assign a new value,
3272 -- even though the compiler thinks that it is not modified. Such
3273 -- code is highly dubious, but we choose to make it "work" for
3274 -- non-constant aliased objects.
3276 -- Note that we used to do this for all aliased objects, whether or
3277 -- not constant, but this caused anomalies down the line because we
3278 -- ended up with static objects that were not Is_True_Constant. Not
3279 -- resetting Is_True_Constant for (aliased) constant objects ensures
3280 -- that this anomaly never occurs.
3282 -- However, we don't do that for internal entities. We figure that if
3283 -- we deliberately set Is_True_Constant for an internal entity, e.g.
3284 -- a dispatch table entry, then we mean it.
3286 if Ekind (E) /= E_Constant
3287 and then (Is_Aliased (E) or else Is_Aliased (Etype (E)))
3288 and then not Is_Internal_Name (Chars (E))
3289 then
3290 Set_Is_True_Constant (E, False);
3291 end if;
3293 -- If the object needs any kind of default initialization, an error
3294 -- must be issued if No_Default_Initialization applies. The check
3295 -- doesn't apply to imported objects, which are not ever default
3296 -- initialized, and is why the check is deferred until freezing, at
3297 -- which point we know if Import applies. Deferred constants are also
3298 -- exempted from this test because their completion is explicit, or
3299 -- through an import pragma.
3301 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
3302 null;
3304 elsif Comes_From_Source (E)
3305 and then not Is_Imported (E)
3306 and then not Has_Init_Expression (Declaration_Node (E))
3307 and then
3308 ((Has_Non_Null_Base_Init_Proc (Etype (E))
3309 and then not No_Initialization (Declaration_Node (E))
3310 and then not Initialization_Suppressed (Etype (E)))
3311 or else
3312 (Needs_Simple_Initialization (Etype (E))
3313 and then not Is_Internal (E)))
3314 then
3315 Has_Default_Initialization := True;
3316 Check_Restriction
3317 (No_Default_Initialization, Declaration_Node (E));
3318 end if;
3320 -- Check that a Thread_Local_Storage variable does not have
3321 -- default initialization, and any explicit initialization must
3322 -- either be the null constant or a static constant.
3324 if Has_Pragma_Thread_Local_Storage (E) then
3325 declare
3326 Decl : constant Node_Id := Declaration_Node (E);
3327 begin
3328 if Has_Default_Initialization
3329 or else
3330 (Has_Init_Expression (Decl)
3331 and then
3332 (No (Expression (Decl))
3333 or else not
3334 (Is_OK_Static_Expression (Expression (Decl))
3335 or else Nkind (Expression (Decl)) = N_Null)))
3336 then
3337 Error_Msg_NE
3338 ("Thread_Local_Storage variable& is "
3339 & "improperly initialized", Decl, E);
3340 Error_Msg_NE
3341 ("\only allowed initialization is explicit "
3342 & "NULL or static expression", Decl, E);
3343 end if;
3344 end;
3345 end if;
3347 -- For imported objects, set Is_Public unless there is also an
3348 -- address clause, which means that there is no external symbol
3349 -- needed for the Import (Is_Public may still be set for other
3350 -- unrelated reasons). Note that we delayed this processing
3351 -- till freeze time so that we can be sure not to set the flag
3352 -- if there is an address clause. If there is such a clause,
3353 -- then the only purpose of the Import pragma is to suppress
3354 -- implicit initialization.
3356 if Is_Imported (E) and then No (Address_Clause (E)) then
3357 Set_Is_Public (E);
3358 end if;
3360 -- For source objects that are not Imported and are library
3361 -- level, if no linker section pragma was given inherit the
3362 -- appropriate linker section from the corresponding type.
3364 if Comes_From_Source (E)
3365 and then not Is_Imported (E)
3366 and then Is_Library_Level_Entity (E)
3367 and then No (Linker_Section_Pragma (E))
3368 then
3369 Set_Linker_Section_Pragma
3370 (E, Linker_Section_Pragma (Etype (E)));
3371 end if;
3373 -- For convention C objects of an enumeration type, warn if the
3374 -- size is not integer size and no explicit size given. Skip
3375 -- warning for Boolean, and Character, assume programmer expects
3376 -- 8-bit sizes for these cases.
3378 if (Convention (E) = Convention_C
3379 or else
3380 Convention (E) = Convention_CPP)
3381 and then Is_Enumeration_Type (Etype (E))
3382 and then not Is_Character_Type (Etype (E))
3383 and then not Is_Boolean_Type (Etype (E))
3384 and then Esize (Etype (E)) < Standard_Integer_Size
3385 and then not Has_Size_Clause (E)
3386 then
3387 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
3388 Error_Msg_N
3389 ("??convention C enumeration object has size less than ^", E);
3390 Error_Msg_N ("\??use explicit size clause to set size", E);
3391 end if;
3392 end Freeze_Object_Declaration;
3394 -----------------------------
3395 -- Freeze_Generic_Entities --
3396 -----------------------------
3398 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
3399 E : Entity_Id;
3400 F : Node_Id;
3401 Flist : List_Id;
3403 begin
3404 Flist := New_List;
3405 E := First_Entity (Pack);
3406 while Present (E) loop
3407 if Is_Type (E) and then not Is_Generic_Type (E) then
3408 F := Make_Freeze_Generic_Entity (Sloc (Pack));
3409 Set_Entity (F, E);
3410 Append_To (Flist, F);
3412 elsif Ekind (E) = E_Generic_Package then
3413 Append_List_To (Flist, Freeze_Generic_Entities (E));
3414 end if;
3416 Next_Entity (E);
3417 end loop;
3419 return Flist;
3420 end Freeze_Generic_Entities;
3422 --------------------
3423 -- Freeze_Profile --
3424 --------------------
3426 function Freeze_Profile (E : Entity_Id) return Boolean is
3427 F_Type : Entity_Id;
3428 R_Type : Entity_Id;
3429 Warn_Node : Node_Id;
3431 begin
3432 -- Loop through formals
3434 Formal := First_Formal (E);
3435 while Present (Formal) loop
3436 F_Type := Etype (Formal);
3438 -- AI05-0151: incomplete types can appear in a profile. By the
3439 -- time the entity is frozen, the full view must be available,
3440 -- unless it is a limited view.
3442 if Is_Incomplete_Type (F_Type)
3443 and then Present (Full_View (F_Type))
3444 and then not From_Limited_With (F_Type)
3445 then
3446 F_Type := Full_View (F_Type);
3447 Set_Etype (Formal, F_Type);
3448 end if;
3450 if not From_Limited_With (F_Type) then
3451 Freeze_And_Append (F_Type, N, Result);
3452 end if;
3454 if Is_Private_Type (F_Type)
3455 and then Is_Private_Type (Base_Type (F_Type))
3456 and then No (Full_View (Base_Type (F_Type)))
3457 and then not Is_Generic_Type (F_Type)
3458 and then not Is_Derived_Type (F_Type)
3459 then
3460 -- If the type of a formal is incomplete, subprogram is being
3461 -- frozen prematurely. Within an instance (but not within a
3462 -- wrapper package) this is an artifact of our need to regard
3463 -- the end of an instantiation as a freeze point. Otherwise it
3464 -- is a definite error.
3466 if In_Instance then
3467 Set_Is_Frozen (E, False);
3468 Result := No_List;
3469 return False;
3471 elsif not After_Last_Declaration
3472 and then not Freezing_Library_Level_Tagged_Type
3473 then
3474 Error_Msg_Node_1 := F_Type;
3475 Error_Msg
3476 ("type & must be fully defined before this point", Loc);
3477 end if;
3478 end if;
3480 -- Check suspicious parameter for C function. These tests apply
3481 -- only to exported/imported subprograms.
3483 if Warn_On_Export_Import
3484 and then Comes_From_Source (E)
3485 and then (Convention (E) = Convention_C
3486 or else
3487 Convention (E) = Convention_CPP)
3488 and then (Is_Imported (E) or else Is_Exported (E))
3489 and then Convention (E) /= Convention (Formal)
3490 and then not Has_Warnings_Off (E)
3491 and then not Has_Warnings_Off (F_Type)
3492 and then not Has_Warnings_Off (Formal)
3493 then
3494 -- Qualify mention of formals with subprogram name
3496 Error_Msg_Qual_Level := 1;
3498 -- Check suspicious use of fat C pointer
3500 if Is_Access_Type (F_Type)
3501 and then Esize (F_Type) > Ttypes.System_Address_Size
3502 then
3503 Error_Msg_N
3504 ("?x?type of & does not correspond to C pointer!", Formal);
3506 -- Check suspicious return of boolean
3508 elsif Root_Type (F_Type) = Standard_Boolean
3509 and then Convention (F_Type) = Convention_Ada
3510 and then not Has_Warnings_Off (F_Type)
3511 and then not Has_Size_Clause (F_Type)
3512 then
3513 Error_Msg_N
3514 ("& is an 8-bit Ada Boolean?x?", Formal);
3515 Error_Msg_N
3516 ("\use appropriate corresponding type in C "
3517 & "(e.g. char)?x?", Formal);
3519 -- Check suspicious tagged type
3521 elsif (Is_Tagged_Type (F_Type)
3522 or else
3523 (Is_Access_Type (F_Type)
3524 and then Is_Tagged_Type (Designated_Type (F_Type))))
3525 and then Convention (E) = Convention_C
3526 then
3527 Error_Msg_N
3528 ("?x?& involves a tagged type which does not "
3529 & "correspond to any C type!", Formal);
3531 -- Check wrong convention subprogram pointer
3533 elsif Ekind (F_Type) = E_Access_Subprogram_Type
3534 and then not Has_Foreign_Convention (F_Type)
3535 then
3536 Error_Msg_N
3537 ("?x?subprogram pointer & should "
3538 & "have foreign convention!", Formal);
3539 Error_Msg_Sloc := Sloc (F_Type);
3540 Error_Msg_NE
3541 ("\?x?add Convention pragma to declaration of &#",
3542 Formal, F_Type);
3543 end if;
3545 -- Turn off name qualification after message output
3547 Error_Msg_Qual_Level := 0;
3548 end if;
3550 -- Check for unconstrained array in exported foreign convention
3551 -- case.
3553 if Has_Foreign_Convention (E)
3554 and then not Is_Imported (E)
3555 and then Is_Array_Type (F_Type)
3556 and then not Is_Constrained (F_Type)
3557 and then Warn_On_Export_Import
3558 then
3559 Error_Msg_Qual_Level := 1;
3561 -- If this is an inherited operation, place the warning on
3562 -- the derived type declaration, rather than on the original
3563 -- subprogram.
3565 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
3566 then
3567 Warn_Node := Parent (E);
3569 if Formal = First_Formal (E) then
3570 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
3571 end if;
3572 else
3573 Warn_Node := Formal;
3574 end if;
3576 Error_Msg_NE ("?x?type of argument& is unconstrained array",
3577 Warn_Node, Formal);
3578 Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
3579 Warn_Node, Formal);
3580 Error_Msg_Qual_Level := 0;
3581 end if;
3583 if not From_Limited_With (F_Type) then
3584 if Is_Access_Type (F_Type) then
3585 F_Type := Designated_Type (F_Type);
3586 end if;
3588 -- If the formal is an anonymous_access_to_subprogram
3589 -- freeze the subprogram type as well, to prevent
3590 -- scope anomalies in gigi, because there is no other
3591 -- clear point at which it could be frozen.
3593 if Is_Itype (Etype (Formal))
3594 and then Ekind (F_Type) = E_Subprogram_Type
3595 then
3596 Freeze_And_Append (F_Type, N, Result);
3597 end if;
3598 end if;
3600 Next_Formal (Formal);
3601 end loop;
3603 -- Case of function: similar checks on return type
3605 if Ekind (E) = E_Function then
3607 -- Freeze return type
3609 R_Type := Etype (E);
3611 -- AI05-0151: the return type may have been incomplete at the
3612 -- point of declaration. Replace it with the full view, unless the
3613 -- current type is a limited view. In that case the full view is
3614 -- in a different unit, and gigi finds the non-limited view after
3615 -- the other unit is elaborated.
3617 if Ekind (R_Type) = E_Incomplete_Type
3618 and then Present (Full_View (R_Type))
3619 and then not From_Limited_With (R_Type)
3620 then
3621 R_Type := Full_View (R_Type);
3622 Set_Etype (E, R_Type);
3623 end if;
3625 Freeze_And_Append (R_Type, N, Result);
3627 -- Check suspicious return type for C function
3629 if Warn_On_Export_Import
3630 and then (Convention (E) = Convention_C
3631 or else
3632 Convention (E) = Convention_CPP)
3633 and then (Is_Imported (E) or else Is_Exported (E))
3634 then
3635 -- Check suspicious return of fat C pointer
3637 if Is_Access_Type (R_Type)
3638 and then Esize (R_Type) > Ttypes.System_Address_Size
3639 and then not Has_Warnings_Off (E)
3640 and then not Has_Warnings_Off (R_Type)
3641 then
3642 Error_Msg_N
3643 ("?x?return type of& does not correspond to C pointer!",
3646 -- Check suspicious return of boolean
3648 elsif Root_Type (R_Type) = Standard_Boolean
3649 and then Convention (R_Type) = Convention_Ada
3650 and then not Has_Warnings_Off (E)
3651 and then not Has_Warnings_Off (R_Type)
3652 and then not Has_Size_Clause (R_Type)
3653 then
3654 declare
3655 N : constant Node_Id :=
3656 Result_Definition (Declaration_Node (E));
3657 begin
3658 Error_Msg_NE
3659 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
3660 Error_Msg_NE
3661 ("\use appropriate corresponding type in C "
3662 & "(e.g. char)?x?", N, E);
3663 end;
3665 -- Check suspicious return tagged type
3667 elsif (Is_Tagged_Type (R_Type)
3668 or else (Is_Access_Type (R_Type)
3669 and then
3670 Is_Tagged_Type
3671 (Designated_Type (R_Type))))
3672 and then Convention (E) = Convention_C
3673 and then not Has_Warnings_Off (E)
3674 and then not Has_Warnings_Off (R_Type)
3675 then
3676 Error_Msg_N ("?x?return type of & does not "
3677 & "correspond to C type!", E);
3679 -- Check return of wrong convention subprogram pointer
3681 elsif Ekind (R_Type) = E_Access_Subprogram_Type
3682 and then not Has_Foreign_Convention (R_Type)
3683 and then not Has_Warnings_Off (E)
3684 and then not Has_Warnings_Off (R_Type)
3685 then
3686 Error_Msg_N ("?x?& should return a foreign "
3687 & "convention subprogram pointer", E);
3688 Error_Msg_Sloc := Sloc (R_Type);
3689 Error_Msg_NE
3690 ("\?x?add Convention pragma to declaration of& #",
3691 E, R_Type);
3692 end if;
3693 end if;
3695 -- Give warning for suspicious return of a result of an
3696 -- unconstrained array type in a foreign convention function.
3698 if Has_Foreign_Convention (E)
3700 -- We are looking for a return of unconstrained array
3702 and then Is_Array_Type (R_Type)
3703 and then not Is_Constrained (R_Type)
3705 -- Exclude imported routines, the warning does not belong on
3706 -- the import, but rather on the routine definition.
3708 and then not Is_Imported (E)
3710 -- Check that general warning is enabled, and that it is not
3711 -- suppressed for this particular case.
3713 and then Warn_On_Export_Import
3714 and then not Has_Warnings_Off (E)
3715 and then not Has_Warnings_Off (R_Type)
3716 then
3717 Error_Msg_N
3718 ("?x?foreign convention function& should not return "
3719 & "unconstrained array!", E);
3720 end if;
3721 end if;
3723 -- Check suspicious use of Import in pure unit (cases where the RM
3724 -- allows calls to be omitted).
3726 if Is_Imported (E)
3728 -- It might be suspicious if the compilation unit has the Pure
3729 -- aspect/pragma.
3731 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
3733 -- The RM allows omission of calls only in the case of
3734 -- library-level subprograms (see RM-10.2.1(18)).
3736 and then Is_Library_Level_Entity (E)
3738 -- Ignore internally generated entity. This happens in some cases
3739 -- of subprograms in specs, where we generate an implied body.
3741 and then Comes_From_Source (Import_Pragma (E))
3743 -- Assume run-time knows what it is doing
3745 and then not GNAT_Mode
3747 -- Assume explicit Pure_Function means import is pure
3749 and then not Has_Pragma_Pure_Function (E)
3751 -- Don't need warning in relaxed semantics mode
3753 and then not Relaxed_RM_Semantics
3755 -- Assume convention Intrinsic is OK, since this is specialized.
3756 -- This deals with the DEC unit current_exception.ads
3758 and then Convention (E) /= Convention_Intrinsic
3760 -- Assume that ASM interface knows what it is doing. This deals
3761 -- with e.g. unsigned.ads in the AAMP back end.
3763 and then Convention (E) /= Convention_Assembler
3764 then
3765 Error_Msg_N
3766 ("pragma Import in Pure unit??", Import_Pragma (E));
3767 Error_Msg_NE
3768 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
3769 Import_Pragma (E), E);
3770 end if;
3772 return True;
3773 end Freeze_Profile;
3775 ------------------------
3776 -- Freeze_Record_Type --
3777 ------------------------
3779 procedure Freeze_Record_Type (Rec : Entity_Id) is
3780 ADC : Node_Id;
3781 Comp : Entity_Id;
3782 IR : Node_Id;
3783 Prev : Entity_Id;
3785 Junk : Boolean;
3786 pragma Warnings (Off, Junk);
3788 Aliased_Component : Boolean := False;
3789 -- Set True if we find at least one component which is aliased. This
3790 -- is used to prevent Implicit_Packing of the record, since packing
3791 -- cannot modify the size of alignment of an aliased component.
3793 All_Elem_Components : Boolean := True;
3794 -- True if all components are of a type whose underlying type is
3795 -- elementary.
3797 All_Sized_Components : Boolean := True;
3798 -- True if all components have a known RM_Size
3800 All_Storage_Unit_Components : Boolean := True;
3801 -- True if all components have an RM_Size that is a multiple of the
3802 -- storage unit.
3804 Elem_Component_Total_Esize : Uint := Uint_0;
3805 -- Accumulates total Esize values of all elementary components. Used
3806 -- for processing of Implicit_Packing.
3808 Placed_Component : Boolean := False;
3809 -- Set True if we find at least one component with a component
3810 -- clause (used to warn about useless Bit_Order pragmas, and also
3811 -- to detect cases where Implicit_Packing may have an effect).
3813 Rec_Pushed : Boolean := False;
3814 -- Set True if the record type scope Rec has been pushed on the scope
3815 -- stack. Needed for the analysis of delayed aspects specified to the
3816 -- components of Rec.
3818 Sized_Component_Total_RM_Size : Uint := Uint_0;
3819 -- Accumulates total RM_Size values of all sized components. Used
3820 -- for processing of Implicit_Packing.
3822 Sized_Component_Total_Round_RM_Size : Uint := Uint_0;
3823 -- Accumulates total RM_Size values of all sized components, rounded
3824 -- individually to a multiple of the storage unit.
3826 SSO_ADC : Node_Id;
3827 -- Scalar_Storage_Order attribute definition clause for the record
3829 SSO_ADC_Component : Boolean := False;
3830 -- Set True if we find at least one component whose type has a
3831 -- Scalar_Storage_Order attribute definition clause.
3833 Unplaced_Component : Boolean := False;
3834 -- Set True if we find at least one component with no component
3835 -- clause (used to warn about useless Pack pragmas).
3837 function Check_Allocator (N : Node_Id) return Node_Id;
3838 -- If N is an allocator, possibly wrapped in one or more level of
3839 -- qualified expression(s), return the inner allocator node, else
3840 -- return Empty.
3842 procedure Check_Itype (Typ : Entity_Id);
3843 -- If the component subtype is an access to a constrained subtype of
3844 -- an already frozen type, make the subtype frozen as well. It might
3845 -- otherwise be frozen in the wrong scope, and a freeze node on
3846 -- subtype has no effect. Similarly, if the component subtype is a
3847 -- regular (not protected) access to subprogram, set the anonymous
3848 -- subprogram type to frozen as well, to prevent an out-of-scope
3849 -- freeze node at some eventual point of call. Protected operations
3850 -- are handled elsewhere.
3852 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3853 -- Make sure that all types mentioned in Discrete_Choices of the
3854 -- variants referenceed by the Variant_Part VP are frozen. This is
3855 -- a recursive routine to deal with nested variants.
3857 ---------------------
3858 -- Check_Allocator --
3859 ---------------------
3861 function Check_Allocator (N : Node_Id) return Node_Id is
3862 Inner : Node_Id;
3863 begin
3864 Inner := N;
3865 loop
3866 if Nkind (Inner) = N_Allocator then
3867 return Inner;
3868 elsif Nkind (Inner) = N_Qualified_Expression then
3869 Inner := Expression (Inner);
3870 else
3871 return Empty;
3872 end if;
3873 end loop;
3874 end Check_Allocator;
3876 -----------------
3877 -- Check_Itype --
3878 -----------------
3880 procedure Check_Itype (Typ : Entity_Id) is
3881 Desig : constant Entity_Id := Designated_Type (Typ);
3883 begin
3884 if not Is_Frozen (Desig)
3885 and then Is_Frozen (Base_Type (Desig))
3886 then
3887 Set_Is_Frozen (Desig);
3889 -- In addition, add an Itype_Reference to ensure that the
3890 -- access subtype is elaborated early enough. This cannot be
3891 -- done if the subtype may depend on discriminants.
3893 if Ekind (Comp) = E_Component
3894 and then Is_Itype (Etype (Comp))
3895 and then not Has_Discriminants (Rec)
3896 then
3897 IR := Make_Itype_Reference (Sloc (Comp));
3898 Set_Itype (IR, Desig);
3899 Add_To_Result (IR);
3900 end if;
3902 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
3903 and then Convention (Desig) /= Convention_Protected
3904 then
3905 Set_Is_Frozen (Desig);
3906 end if;
3907 end Check_Itype;
3909 ------------------------------------
3910 -- Freeze_Choices_In_Variant_Part --
3911 ------------------------------------
3913 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
3914 pragma Assert (Nkind (VP) = N_Variant_Part);
3916 Variant : Node_Id;
3917 Choice : Node_Id;
3918 CL : Node_Id;
3920 begin
3921 -- Loop through variants
3923 Variant := First_Non_Pragma (Variants (VP));
3924 while Present (Variant) loop
3926 -- Loop through choices, checking that all types are frozen
3928 Choice := First_Non_Pragma (Discrete_Choices (Variant));
3929 while Present (Choice) loop
3930 if Nkind (Choice) in N_Has_Etype
3931 and then Present (Etype (Choice))
3932 then
3933 Freeze_And_Append (Etype (Choice), N, Result);
3934 end if;
3936 Next_Non_Pragma (Choice);
3937 end loop;
3939 -- Check for nested variant part to process
3941 CL := Component_List (Variant);
3943 if not Null_Present (CL) then
3944 if Present (Variant_Part (CL)) then
3945 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
3946 end if;
3947 end if;
3949 Next_Non_Pragma (Variant);
3950 end loop;
3951 end Freeze_Choices_In_Variant_Part;
3953 -- Start of processing for Freeze_Record_Type
3955 begin
3956 -- Deal with delayed aspect specifications for components. The
3957 -- analysis of the aspect is required to be delayed to the freeze
3958 -- point, thus we analyze the pragma or attribute definition
3959 -- clause in the tree at this point. We also analyze the aspect
3960 -- specification node at the freeze point when the aspect doesn't
3961 -- correspond to pragma/attribute definition clause.
3963 Comp := First_Entity (Rec);
3964 while Present (Comp) loop
3965 if Ekind (Comp) = E_Component
3966 and then Has_Delayed_Aspects (Comp)
3967 then
3968 if not Rec_Pushed then
3969 Push_Scope (Rec);
3970 Rec_Pushed := True;
3972 -- The visibility to the discriminants must be restored in
3973 -- order to properly analyze the aspects.
3975 if Has_Discriminants (Rec) then
3976 Install_Discriminants (Rec);
3977 end if;
3978 end if;
3980 Analyze_Aspects_At_Freeze_Point (Comp);
3981 end if;
3983 Next_Entity (Comp);
3984 end loop;
3986 -- Pop the scope if Rec scope has been pushed on the scope stack
3987 -- during the delayed aspect analysis process.
3989 if Rec_Pushed then
3990 if Has_Discriminants (Rec) then
3991 Uninstall_Discriminants (Rec);
3992 end if;
3994 Pop_Scope;
3995 end if;
3997 -- Freeze components and embedded subtypes
3999 Comp := First_Entity (Rec);
4000 Prev := Empty;
4001 while Present (Comp) loop
4002 if Is_Aliased (Comp) then
4003 Aliased_Component := True;
4004 end if;
4006 -- Handle the component and discriminant case
4008 if Ekind_In (Comp, E_Component, E_Discriminant) then
4009 declare
4010 CC : constant Node_Id := Component_Clause (Comp);
4012 begin
4013 -- Freezing a record type freezes the type of each of its
4014 -- components. However, if the type of the component is
4015 -- part of this record, we do not want or need a separate
4016 -- Freeze_Node. Note that Is_Itype is wrong because that's
4017 -- also set in private type cases. We also can't check for
4018 -- the Scope being exactly Rec because of private types and
4019 -- record extensions.
4021 if Is_Itype (Etype (Comp))
4022 and then Is_Record_Type (Underlying_Type
4023 (Scope (Etype (Comp))))
4024 then
4025 Undelay_Type (Etype (Comp));
4026 end if;
4028 Freeze_And_Append (Etype (Comp), N, Result);
4030 -- Warn for pragma Pack overriding foreign convention
4032 if Has_Foreign_Convention (Etype (Comp))
4033 and then Has_Pragma_Pack (Rec)
4035 -- Don't warn for aliased components, since override
4036 -- cannot happen in that case.
4038 and then not Is_Aliased (Comp)
4039 then
4040 declare
4041 CN : constant Name_Id :=
4042 Get_Convention_Name (Convention (Etype (Comp)));
4043 PP : constant Node_Id :=
4044 Get_Pragma (Rec, Pragma_Pack);
4045 begin
4046 if Present (PP) then
4047 Error_Msg_Name_1 := CN;
4048 Error_Msg_Sloc := Sloc (Comp);
4049 Error_Msg_N
4050 ("pragma Pack affects convention % component#??",
4051 PP);
4052 Error_Msg_Name_1 := CN;
4053 Error_Msg_NE
4054 ("\component & may not have % compatible "
4055 & "representation??", PP, Comp);
4056 end if;
4057 end;
4058 end if;
4060 -- Check for error of component clause given for variable
4061 -- sized type. We have to delay this test till this point,
4062 -- since the component type has to be frozen for us to know
4063 -- if it is variable length.
4065 if Present (CC) then
4066 Placed_Component := True;
4068 -- We omit this test in a generic context, it will be
4069 -- applied at instantiation time.
4071 if Inside_A_Generic then
4072 null;
4074 -- Also omit this test in CodePeer mode, since we do not
4075 -- have sufficient info on size and rep clauses.
4077 elsif CodePeer_Mode then
4078 null;
4080 -- Omit check if component has a generic type. This can
4081 -- happen in an instantiation within a generic in ASIS
4082 -- mode, where we force freeze actions without full
4083 -- expansion.
4085 elsif Is_Generic_Type (Etype (Comp)) then
4086 null;
4088 -- Do the check
4090 elsif not
4091 Size_Known_At_Compile_Time
4092 (Underlying_Type (Etype (Comp)))
4093 then
4094 Error_Msg_N
4095 ("component clause not allowed for variable " &
4096 "length component", CC);
4097 end if;
4099 else
4100 Unplaced_Component := True;
4101 end if;
4103 -- Case of component requires byte alignment
4105 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
4107 -- Set the enclosing record to also require byte align
4109 Set_Must_Be_On_Byte_Boundary (Rec);
4111 -- Check for component clause that is inconsistent with
4112 -- the required byte boundary alignment.
4114 if Present (CC)
4115 and then Normalized_First_Bit (Comp) mod
4116 System_Storage_Unit /= 0
4117 then
4118 Error_Msg_N
4119 ("component & must be byte aligned",
4120 Component_Name (Component_Clause (Comp)));
4121 end if;
4122 end if;
4123 end;
4124 end if;
4126 -- Gather data for possible Implicit_Packing later. Note that at
4127 -- this stage we might be dealing with a real component, or with
4128 -- an implicit subtype declaration.
4130 if Known_Static_RM_Size (Etype (Comp)) then
4131 declare
4132 Comp_Type : constant Entity_Id := Etype (Comp);
4133 Comp_Size : constant Uint := RM_Size (Comp_Type);
4134 SSU : constant Int := Ttypes.System_Storage_Unit;
4136 begin
4137 Sized_Component_Total_RM_Size :=
4138 Sized_Component_Total_RM_Size + Comp_Size;
4140 Sized_Component_Total_Round_RM_Size :=
4141 Sized_Component_Total_Round_RM_Size +
4142 (Comp_Size + SSU - 1) / SSU * SSU;
4144 if Present (Underlying_Type (Comp_Type))
4145 and then Is_Elementary_Type (Underlying_Type (Comp_Type))
4146 then
4147 Elem_Component_Total_Esize :=
4148 Elem_Component_Total_Esize + Esize (Comp_Type);
4149 else
4150 All_Elem_Components := False;
4152 if Comp_Size mod SSU /= 0 then
4153 All_Storage_Unit_Components := False;
4154 end if;
4155 end if;
4156 end;
4157 else
4158 All_Sized_Components := False;
4159 end if;
4161 -- If the component is an Itype with Delayed_Freeze and is either
4162 -- a record or array subtype and its base type has not yet been
4163 -- frozen, we must remove this from the entity list of this record
4164 -- and put it on the entity list of the scope of its base type.
4165 -- Note that we know that this is not the type of a component
4166 -- since we cleared Has_Delayed_Freeze for it in the previous
4167 -- loop. Thus this must be the Designated_Type of an access type,
4168 -- which is the type of a component.
4170 if Is_Itype (Comp)
4171 and then Is_Type (Scope (Comp))
4172 and then Is_Composite_Type (Comp)
4173 and then Base_Type (Comp) /= Comp
4174 and then Has_Delayed_Freeze (Comp)
4175 and then not Is_Frozen (Base_Type (Comp))
4176 then
4177 declare
4178 Will_Be_Frozen : Boolean := False;
4179 S : Entity_Id;
4181 begin
4182 -- We have a difficult case to handle here. Suppose Rec is
4183 -- subtype being defined in a subprogram that's created as
4184 -- part of the freezing of Rec'Base. In that case, we know
4185 -- that Comp'Base must have already been frozen by the time
4186 -- we get to elaborate this because Gigi doesn't elaborate
4187 -- any bodies until it has elaborated all of the declarative
4188 -- part. But Is_Frozen will not be set at this point because
4189 -- we are processing code in lexical order.
4191 -- We detect this case by going up the Scope chain of Rec
4192 -- and seeing if we have a subprogram scope before reaching
4193 -- the top of the scope chain or that of Comp'Base. If we
4194 -- do, then mark that Comp'Base will actually be frozen. If
4195 -- so, we merely undelay it.
4197 S := Scope (Rec);
4198 while Present (S) loop
4199 if Is_Subprogram (S) then
4200 Will_Be_Frozen := True;
4201 exit;
4202 elsif S = Scope (Base_Type (Comp)) then
4203 exit;
4204 end if;
4206 S := Scope (S);
4207 end loop;
4209 if Will_Be_Frozen then
4210 Undelay_Type (Comp);
4212 else
4213 if Present (Prev) then
4214 Set_Next_Entity (Prev, Next_Entity (Comp));
4215 else
4216 Set_First_Entity (Rec, Next_Entity (Comp));
4217 end if;
4219 -- Insert in entity list of scope of base type (which
4220 -- must be an enclosing scope, because still unfrozen).
4222 Append_Entity (Comp, Scope (Base_Type (Comp)));
4223 end if;
4224 end;
4226 -- If the component is an access type with an allocator as default
4227 -- value, the designated type will be frozen by the corresponding
4228 -- expression in init_proc. In order to place the freeze node for
4229 -- the designated type before that for the current record type,
4230 -- freeze it now.
4232 -- Same process if the component is an array of access types,
4233 -- initialized with an aggregate. If the designated type is
4234 -- private, it cannot contain allocators, and it is premature
4235 -- to freeze the type, so we check for this as well.
4237 elsif Is_Access_Type (Etype (Comp))
4238 and then Present (Parent (Comp))
4239 and then Present (Expression (Parent (Comp)))
4240 then
4241 declare
4242 Alloc : constant Node_Id :=
4243 Check_Allocator (Expression (Parent (Comp)));
4245 begin
4246 if Present (Alloc) then
4248 -- If component is pointer to a class-wide type, freeze
4249 -- the specific type in the expression being allocated.
4250 -- The expression may be a subtype indication, in which
4251 -- case freeze the subtype mark.
4253 if Is_Class_Wide_Type
4254 (Designated_Type (Etype (Comp)))
4255 then
4256 if Is_Entity_Name (Expression (Alloc)) then
4257 Freeze_And_Append
4258 (Entity (Expression (Alloc)), N, Result);
4260 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
4261 then
4262 Freeze_And_Append
4263 (Entity (Subtype_Mark (Expression (Alloc))),
4264 N, Result);
4265 end if;
4267 elsif Is_Itype (Designated_Type (Etype (Comp))) then
4268 Check_Itype (Etype (Comp));
4270 else
4271 Freeze_And_Append
4272 (Designated_Type (Etype (Comp)), N, Result);
4273 end if;
4274 end if;
4275 end;
4277 elsif Is_Access_Type (Etype (Comp))
4278 and then Is_Itype (Designated_Type (Etype (Comp)))
4279 then
4280 Check_Itype (Etype (Comp));
4282 -- Freeze the designated type when initializing a component with
4283 -- an aggregate in case the aggregate contains allocators.
4285 -- type T is ...;
4286 -- type T_Ptr is access all T;
4287 -- type T_Array is array ... of T_Ptr;
4289 -- type Rec is record
4290 -- Comp : T_Array := (others => ...);
4291 -- end record;
4293 elsif Is_Array_Type (Etype (Comp))
4294 and then Is_Access_Type (Component_Type (Etype (Comp)))
4295 then
4296 declare
4297 Comp_Par : constant Node_Id := Parent (Comp);
4298 Desig_Typ : constant Entity_Id :=
4299 Designated_Type
4300 (Component_Type (Etype (Comp)));
4302 begin
4303 -- The only case when this sort of freezing is not done is
4304 -- when the designated type is class-wide and the root type
4305 -- is the record owning the component. This scenario results
4306 -- in a circularity because the class-wide type requires
4307 -- primitives that have not been created yet as the root
4308 -- type is in the process of being frozen.
4310 -- type Rec is tagged;
4311 -- type Rec_Ptr is access all Rec'Class;
4312 -- type Rec_Array is array ... of Rec_Ptr;
4314 -- type Rec is record
4315 -- Comp : Rec_Array := (others => ...);
4316 -- end record;
4318 if Is_Class_Wide_Type (Desig_Typ)
4319 and then Root_Type (Desig_Typ) = Rec
4320 then
4321 null;
4323 elsif Is_Fully_Defined (Desig_Typ)
4324 and then Present (Comp_Par)
4325 and then Nkind (Comp_Par) = N_Component_Declaration
4326 and then Present (Expression (Comp_Par))
4327 and then Nkind (Expression (Comp_Par)) = N_Aggregate
4328 then
4329 Freeze_And_Append (Desig_Typ, N, Result);
4330 end if;
4331 end;
4332 end if;
4334 Prev := Comp;
4335 Next_Entity (Comp);
4336 end loop;
4338 SSO_ADC :=
4339 Get_Attribute_Definition_Clause
4340 (Rec, Attribute_Scalar_Storage_Order);
4342 -- If the record type has Complex_Representation, then it is treated
4343 -- as a scalar in the back end so the storage order is irrelevant.
4345 if Has_Complex_Representation (Rec) then
4346 if Present (SSO_ADC) then
4347 Error_Msg_N
4348 ("??storage order has no effect with Complex_Representation",
4349 SSO_ADC);
4350 end if;
4352 else
4353 -- Deal with default setting of reverse storage order
4355 Set_SSO_From_Default (Rec);
4357 -- Check consistent attribute setting on component types
4359 declare
4360 Comp_ADC_Present : Boolean;
4361 begin
4362 Comp := First_Component (Rec);
4363 while Present (Comp) loop
4364 Check_Component_Storage_Order
4365 (Encl_Type => Rec,
4366 Comp => Comp,
4367 ADC => SSO_ADC,
4368 Comp_ADC_Present => Comp_ADC_Present);
4369 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
4370 Next_Component (Comp);
4371 end loop;
4372 end;
4374 -- Now deal with reverse storage order/bit order issues
4376 if Present (SSO_ADC) then
4378 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
4379 -- if the former is specified.
4381 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
4383 -- Note: report error on Rec, not on SSO_ADC, as ADC may
4384 -- apply to some ancestor type.
4386 Error_Msg_Sloc := Sloc (SSO_ADC);
4387 Error_Msg_N
4388 ("scalar storage order for& specified# inconsistent with "
4389 & "bit order", Rec);
4390 end if;
4392 -- Warn if there is a Scalar_Storage_Order attribute definition
4393 -- clause but no component clause, no component that itself has
4394 -- such an attribute definition, and no pragma Pack.
4396 if not (Placed_Component
4397 or else
4398 SSO_ADC_Component
4399 or else
4400 Is_Packed (Rec))
4401 then
4402 Error_Msg_N
4403 ("??scalar storage order specified but no component "
4404 & "clause", SSO_ADC);
4405 end if;
4406 end if;
4407 end if;
4409 -- Deal with Bit_Order aspect
4411 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
4413 if Present (ADC) and then Base_Type (Rec) = Rec then
4414 if not (Placed_Component
4415 or else Present (SSO_ADC)
4416 or else Is_Packed (Rec))
4417 then
4418 -- Warn if clause has no effect when no component clause is
4419 -- present, but suppress warning if the Bit_Order is required
4420 -- due to the presence of a Scalar_Storage_Order attribute.
4422 Error_Msg_N
4423 ("??bit order specification has no effect", ADC);
4424 Error_Msg_N
4425 ("\??since no component clauses were specified", ADC);
4427 -- Here is where we do the processing to adjust component clauses
4428 -- for reversed bit order, when not using reverse SSO. If an error
4429 -- has been reported on Rec already (such as SSO incompatible with
4430 -- bit order), don't bother adjusting as this may generate extra
4431 -- noise.
4433 elsif Reverse_Bit_Order (Rec)
4434 and then not Reverse_Storage_Order (Rec)
4435 and then not Error_Posted (Rec)
4436 then
4437 Adjust_Record_For_Reverse_Bit_Order (Rec);
4439 -- Case where we have both an explicit Bit_Order and the same
4440 -- Scalar_Storage_Order: leave record untouched, the back-end
4441 -- will take care of required layout conversions.
4443 else
4444 null;
4446 end if;
4447 end if;
4449 -- Complete error checking on record representation clause (e.g.
4450 -- overlap of components). This is called after adjusting the
4451 -- record for reverse bit order.
4453 declare
4454 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
4455 begin
4456 if Present (RRC) then
4457 Check_Record_Representation_Clause (RRC);
4458 end if;
4459 end;
4461 -- Check for useless pragma Pack when all components placed. We only
4462 -- do this check for record types, not subtypes, since a subtype may
4463 -- have all its components placed, and it still makes perfectly good
4464 -- sense to pack other subtypes or the parent type. We do not give
4465 -- this warning if Optimize_Alignment is set to Space, since the
4466 -- pragma Pack does have an effect in this case (it always resets
4467 -- the alignment to one).
4469 if Ekind (Rec) = E_Record_Type
4470 and then Is_Packed (Rec)
4471 and then not Unplaced_Component
4472 and then Optimize_Alignment /= 'S'
4473 then
4474 -- Reset packed status. Probably not necessary, but we do it so
4475 -- that there is no chance of the back end doing something strange
4476 -- with this redundant indication of packing.
4478 Set_Is_Packed (Rec, False);
4480 -- Give warning if redundant constructs warnings on
4482 if Warn_On_Redundant_Constructs then
4483 Error_Msg_N -- CODEFIX
4484 ("??pragma Pack has no effect, no unplaced components",
4485 Get_Rep_Pragma (Rec, Name_Pack));
4486 end if;
4487 end if;
4489 -- If this is the record corresponding to a remote type, freeze the
4490 -- remote type here since that is what we are semantically freezing.
4491 -- This prevents the freeze node for that type in an inner scope.
4493 if Ekind (Rec) = E_Record_Type then
4494 if Present (Corresponding_Remote_Type (Rec)) then
4495 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
4496 end if;
4498 -- Check for controlled components, unchecked unions, and type
4499 -- invariants.
4501 Comp := First_Component (Rec);
4502 while Present (Comp) loop
4504 -- Do not set Has_Controlled_Component on a class-wide
4505 -- equivalent type. See Make_CW_Equivalent_Type.
4507 if not Is_Class_Wide_Equivalent_Type (Rec)
4508 and then
4509 (Has_Controlled_Component (Etype (Comp))
4510 or else
4511 (Chars (Comp) /= Name_uParent
4512 and then Is_Controlled (Etype (Comp)))
4513 or else
4514 (Is_Protected_Type (Etype (Comp))
4515 and then
4516 Present (Corresponding_Record_Type (Etype (Comp)))
4517 and then
4518 Has_Controlled_Component
4519 (Corresponding_Record_Type (Etype (Comp)))))
4520 then
4521 Set_Has_Controlled_Component (Rec);
4522 end if;
4524 if Has_Unchecked_Union (Etype (Comp)) then
4525 Set_Has_Unchecked_Union (Rec);
4526 end if;
4528 -- The record type requires its own invariant procedure in
4529 -- order to verify the invariant of each individual component.
4530 -- Do not consider internal components such as _parent because
4531 -- parent class-wide invariants are always inherited.
4532 -- In GNATprove mode, the component invariants are checked by
4533 -- other means. They should not be added to the record type
4534 -- invariant procedure, so that the procedure can be used to
4535 -- check the recordy type invariants if any.
4537 if Comes_From_Source (Comp)
4538 and then Has_Invariants (Etype (Comp))
4539 and then not GNATprove_Mode
4540 then
4541 Set_Has_Own_Invariants (Rec);
4542 end if;
4544 -- Scan component declaration for likely misuses of current
4545 -- instance, either in a constraint or a default expression.
4547 if Has_Per_Object_Constraint (Comp) then
4548 Check_Current_Instance (Parent (Comp));
4549 end if;
4551 Next_Component (Comp);
4552 end loop;
4553 end if;
4555 -- Enforce the restriction that access attributes with a current
4556 -- instance prefix can only apply to limited types. This comment
4557 -- is floating here, but does not seem to belong here???
4559 -- Set component alignment if not otherwise already set
4561 Set_Component_Alignment_If_Not_Set (Rec);
4563 -- For first subtypes, check if there are any fixed-point fields with
4564 -- component clauses, where we must check the size. This is not done
4565 -- till the freeze point since for fixed-point types, we do not know
4566 -- the size until the type is frozen. Similar processing applies to
4567 -- bit-packed arrays.
4569 if Is_First_Subtype (Rec) then
4570 Comp := First_Component (Rec);
4571 while Present (Comp) loop
4572 if Present (Component_Clause (Comp))
4573 and then (Is_Fixed_Point_Type (Etype (Comp))
4574 or else Is_Bit_Packed_Array (Etype (Comp)))
4575 then
4576 Check_Size
4577 (Component_Name (Component_Clause (Comp)),
4578 Etype (Comp),
4579 Esize (Comp),
4580 Junk);
4581 end if;
4583 Next_Component (Comp);
4584 end loop;
4585 end if;
4587 -- See if Size is too small as is (and implicit packing might help)
4589 if not Is_Packed (Rec)
4591 -- No implicit packing if even one component is explicitly placed
4593 and then not Placed_Component
4595 -- Or even one component is aliased
4597 and then not Aliased_Component
4599 -- Must have size clause and all sized components
4601 and then Has_Size_Clause (Rec)
4602 and then All_Sized_Components
4604 -- Do not try implicit packing on records with discriminants, too
4605 -- complicated, especially in the variant record case.
4607 and then not Has_Discriminants (Rec)
4609 -- We want to implicitly pack if the specified size of the record
4610 -- is less than the sum of the object sizes (no point in packing
4611 -- if this is not the case), if we can compute it, i.e. if we have
4612 -- only elementary components. Otherwise, we have at least one
4613 -- composite component and we want to implicitly pack only if bit
4614 -- packing is required for it, as we are sure in this case that
4615 -- the back end cannot do the expected layout without packing.
4617 and then
4618 ((All_Elem_Components
4619 and then RM_Size (Rec) < Elem_Component_Total_Esize)
4620 or else
4621 (not All_Elem_Components
4622 and then not All_Storage_Unit_Components
4623 and then RM_Size (Rec) < Sized_Component_Total_Round_RM_Size))
4625 -- And the total RM size cannot be greater than the specified size
4626 -- since otherwise packing will not get us where we have to be.
4628 and then Sized_Component_Total_RM_Size <= RM_Size (Rec)
4630 -- Never do implicit packing in CodePeer or SPARK modes since
4631 -- we don't do any packing in these modes, since this generates
4632 -- over-complex code that confuses static analysis, and in
4633 -- general, neither CodePeer not GNATprove care about the
4634 -- internal representation of objects.
4636 and then not (CodePeer_Mode or GNATprove_Mode)
4637 then
4638 -- If implicit packing enabled, do it
4640 if Implicit_Packing then
4641 Set_Is_Packed (Rec);
4643 -- Otherwise flag the size clause
4645 else
4646 declare
4647 Sz : constant Node_Id := Size_Clause (Rec);
4648 begin
4649 Error_Msg_NE -- CODEFIX
4650 ("size given for& too small", Sz, Rec);
4651 Error_Msg_N -- CODEFIX
4652 ("\use explicit pragma Pack "
4653 & "or use pragma Implicit_Packing", Sz);
4654 end;
4655 end if;
4656 end if;
4658 -- The following checks are relevant only when SPARK_Mode is on as
4659 -- they are not standard Ada legality rules.
4661 if SPARK_Mode = On then
4663 -- A discriminated type cannot be effectively volatile
4664 -- (SPARK RM 7.1.3(5)).
4666 if Is_Effectively_Volatile (Rec) then
4667 if Has_Discriminants (Rec) then
4668 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
4669 end if;
4671 -- A non-effectively volatile record type cannot contain
4672 -- effectively volatile components (SPARK RM 7.1.3(6)).
4674 else
4675 Comp := First_Component (Rec);
4676 while Present (Comp) loop
4677 if Comes_From_Source (Comp)
4678 and then Is_Effectively_Volatile (Etype (Comp))
4679 then
4680 Error_Msg_Name_1 := Chars (Rec);
4681 Error_Msg_N
4682 ("component & of non-volatile type % cannot be "
4683 & "volatile", Comp);
4684 end if;
4686 Next_Component (Comp);
4687 end loop;
4688 end if;
4690 -- A type which does not yield a synchronized object cannot have
4691 -- a component that yields a synchronized object (SPARK RM 9.5).
4693 if not Yields_Synchronized_Object (Rec) then
4694 Comp := First_Component (Rec);
4695 while Present (Comp) loop
4696 if Comes_From_Source (Comp)
4697 and then Yields_Synchronized_Object (Etype (Comp))
4698 then
4699 Error_Msg_Name_1 := Chars (Rec);
4700 Error_Msg_N
4701 ("component & of non-synchronized type % cannot be "
4702 & "synchronized", Comp);
4703 end if;
4705 Next_Component (Comp);
4706 end loop;
4707 end if;
4709 -- A Ghost type cannot have a component of protected or task type
4710 -- (SPARK RM 6.9(19)).
4712 if Is_Ghost_Entity (Rec) then
4713 Comp := First_Component (Rec);
4714 while Present (Comp) loop
4715 if Comes_From_Source (Comp)
4716 and then Is_Concurrent_Type (Etype (Comp))
4717 then
4718 Error_Msg_Name_1 := Chars (Rec);
4719 Error_Msg_N
4720 ("component & of ghost type % cannot be concurrent",
4721 Comp);
4722 end if;
4724 Next_Component (Comp);
4725 end loop;
4726 end if;
4727 end if;
4729 -- Make sure that if we have an iterator aspect, then we have
4730 -- either Constant_Indexing or Variable_Indexing.
4732 declare
4733 Iterator_Aspect : Node_Id;
4735 begin
4736 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
4738 if No (Iterator_Aspect) then
4739 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
4740 end if;
4742 if Present (Iterator_Aspect) then
4743 if Has_Aspect (Rec, Aspect_Constant_Indexing)
4744 or else
4745 Has_Aspect (Rec, Aspect_Variable_Indexing)
4746 then
4747 null;
4748 else
4749 Error_Msg_N
4750 ("Iterator_Element requires indexing aspect",
4751 Iterator_Aspect);
4752 end if;
4753 end if;
4754 end;
4756 -- All done if not a full record definition
4758 if Ekind (Rec) /= E_Record_Type then
4759 return;
4760 end if;
4762 -- Finally we need to check the variant part to make sure that
4763 -- all types within choices are properly frozen as part of the
4764 -- freezing of the record type.
4766 Check_Variant_Part : declare
4767 D : constant Node_Id := Declaration_Node (Rec);
4768 T : Node_Id;
4769 C : Node_Id;
4771 begin
4772 -- Find component list
4774 C := Empty;
4776 if Nkind (D) = N_Full_Type_Declaration then
4777 T := Type_Definition (D);
4779 if Nkind (T) = N_Record_Definition then
4780 C := Component_List (T);
4782 elsif Nkind (T) = N_Derived_Type_Definition
4783 and then Present (Record_Extension_Part (T))
4784 then
4785 C := Component_List (Record_Extension_Part (T));
4786 end if;
4787 end if;
4789 -- Case of variant part present
4791 if Present (C) and then Present (Variant_Part (C)) then
4792 Freeze_Choices_In_Variant_Part (Variant_Part (C));
4793 end if;
4795 -- Note: we used to call Check_Choices here, but it is too early,
4796 -- since predicated subtypes are frozen here, but their freezing
4797 -- actions are in Analyze_Freeze_Entity, which has not been called
4798 -- yet for entities frozen within this procedure, so we moved that
4799 -- call to the Analyze_Freeze_Entity for the record type.
4801 end Check_Variant_Part;
4803 -- Check that all the primitives of an interface type are abstract
4804 -- or null procedures.
4806 if Is_Interface (Rec)
4807 and then not Error_Posted (Parent (Rec))
4808 then
4809 declare
4810 Elmt : Elmt_Id;
4811 Subp : Entity_Id;
4813 begin
4814 Elmt := First_Elmt (Primitive_Operations (Rec));
4815 while Present (Elmt) loop
4816 Subp := Node (Elmt);
4818 if not Is_Abstract_Subprogram (Subp)
4820 -- Avoid reporting the error on inherited primitives
4822 and then Comes_From_Source (Subp)
4823 then
4824 Error_Msg_Name_1 := Chars (Subp);
4826 if Ekind (Subp) = E_Procedure then
4827 if not Null_Present (Parent (Subp)) then
4828 Error_Msg_N
4829 ("interface procedure % must be abstract or null",
4830 Parent (Subp));
4831 end if;
4832 else
4833 Error_Msg_N
4834 ("interface function % must be abstract",
4835 Parent (Subp));
4836 end if;
4837 end if;
4839 Next_Elmt (Elmt);
4840 end loop;
4841 end;
4842 end if;
4844 -- For a derived tagged type, check whether inherited primitives
4845 -- might require a wrapper to handle class-wide conditions.
4847 if Is_Tagged_Type (Rec) and then Is_Derived_Type (Rec) then
4848 Check_Inherited_Conditions (Rec);
4849 end if;
4850 end Freeze_Record_Type;
4852 -------------------------------
4853 -- Has_Boolean_Aspect_Import --
4854 -------------------------------
4856 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4857 Decl : constant Node_Id := Declaration_Node (E);
4858 Asp : Node_Id;
4859 Expr : Node_Id;
4861 begin
4862 if Has_Aspects (Decl) then
4863 Asp := First (Aspect_Specifications (Decl));
4864 while Present (Asp) loop
4865 Expr := Expression (Asp);
4867 -- The value of aspect Import is True when the expression is
4868 -- either missing or it is explicitly set to True.
4870 if Get_Aspect_Id (Asp) = Aspect_Import
4871 and then (No (Expr)
4872 or else (Compile_Time_Known_Value (Expr)
4873 and then Is_True (Expr_Value (Expr))))
4874 then
4875 return True;
4876 end if;
4878 Next (Asp);
4879 end loop;
4880 end if;
4882 return False;
4883 end Has_Boolean_Aspect_Import;
4885 -------------------------
4886 -- Inherit_Freeze_Node --
4887 -------------------------
4889 procedure Inherit_Freeze_Node
4890 (Fnod : Node_Id;
4891 Typ : Entity_Id)
4893 Typ_Fnod : constant Node_Id := Freeze_Node (Typ);
4895 begin
4896 Set_Freeze_Node (Typ, Fnod);
4897 Set_Entity (Fnod, Typ);
4899 -- The input type had an existing node. Propagate relevant attributes
4900 -- from the old freeze node to the inherited freeze node.
4902 -- ??? if both freeze nodes have attributes, would they differ?
4904 if Present (Typ_Fnod) then
4906 -- Attribute Access_Types_To_Process
4908 if Present (Access_Types_To_Process (Typ_Fnod))
4909 and then No (Access_Types_To_Process (Fnod))
4910 then
4911 Set_Access_Types_To_Process (Fnod,
4912 Access_Types_To_Process (Typ_Fnod));
4913 end if;
4915 -- Attribute Actions
4917 if Present (Actions (Typ_Fnod)) and then No (Actions (Fnod)) then
4918 Set_Actions (Fnod, Actions (Typ_Fnod));
4919 end if;
4921 -- Attribute First_Subtype_Link
4923 if Present (First_Subtype_Link (Typ_Fnod))
4924 and then No (First_Subtype_Link (Fnod))
4925 then
4926 Set_First_Subtype_Link (Fnod, First_Subtype_Link (Typ_Fnod));
4927 end if;
4929 -- Attribute TSS_Elist
4931 if Present (TSS_Elist (Typ_Fnod))
4932 and then No (TSS_Elist (Fnod))
4933 then
4934 Set_TSS_Elist (Fnod, TSS_Elist (Typ_Fnod));
4935 end if;
4936 end if;
4937 end Inherit_Freeze_Node;
4939 ------------------------------
4940 -- Wrap_Imported_Subprogram --
4941 ------------------------------
4943 -- The issue here is that our normal approach of checking preconditions
4944 -- and postconditions does not work for imported procedures, since we
4945 -- are not generating code for the body. To get around this we create
4946 -- a wrapper, as shown by the following example:
4948 -- procedure K (A : Integer);
4949 -- pragma Import (C, K);
4951 -- The spec is rewritten by removing the effects of pragma Import, but
4952 -- leaving the convention unchanged, as though the source had said:
4954 -- procedure K (A : Integer);
4955 -- pragma Convention (C, K);
4957 -- and we create a body, added to the entity K freeze actions, which
4958 -- looks like:
4960 -- procedure K (A : Integer) is
4961 -- procedure K (A : Integer);
4962 -- pragma Import (C, K);
4963 -- begin
4964 -- K (A);
4965 -- end K;
4967 -- Now the contract applies in the normal way to the outer procedure,
4968 -- and the inner procedure has no contracts, so there is no problem
4969 -- in just calling it to get the original effect.
4971 -- In the case of a function, we create an appropriate return statement
4972 -- for the subprogram body that calls the inner procedure.
4974 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
4975 function Copy_Import_Pragma return Node_Id;
4976 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
4978 ------------------------
4979 -- Copy_Import_Pragma --
4980 ------------------------
4982 function Copy_Import_Pragma return Node_Id is
4984 -- The subprogram should have an import pragma, otherwise it does
4985 -- need a wrapper.
4987 Prag : constant Node_Id := Import_Pragma (E);
4988 pragma Assert (Present (Prag));
4990 -- Save all semantic fields of the pragma
4992 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
4993 Save_From : constant Boolean := From_Aspect_Specification (Prag);
4994 Save_Prag : constant Node_Id := Next_Pragma (Prag);
4995 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
4997 Result : Node_Id;
4999 begin
5000 -- Reset all semantic fields. This avoids a potential infinite
5001 -- loop when the pragma comes from an aspect as the duplication
5002 -- will copy the aspect, then copy the corresponding pragma and
5003 -- so on.
5005 Set_Corresponding_Aspect (Prag, Empty);
5006 Set_From_Aspect_Specification (Prag, False);
5007 Set_Next_Pragma (Prag, Empty);
5008 Set_Next_Rep_Item (Prag, Empty);
5010 Result := Copy_Separate_Tree (Prag);
5012 -- Restore the original semantic fields
5014 Set_Corresponding_Aspect (Prag, Save_Asp);
5015 Set_From_Aspect_Specification (Prag, Save_From);
5016 Set_Next_Pragma (Prag, Save_Prag);
5017 Set_Next_Rep_Item (Prag, Save_Rep);
5019 return Result;
5020 end Copy_Import_Pragma;
5022 -- Local variables
5024 Loc : constant Source_Ptr := Sloc (E);
5025 CE : constant Name_Id := Chars (E);
5026 Bod : Node_Id;
5027 Forml : Entity_Id;
5028 Parms : List_Id;
5029 Prag : Node_Id;
5030 Spec : Node_Id;
5031 Stmt : Node_Id;
5033 -- Start of processing for Wrap_Imported_Subprogram
5035 begin
5036 -- Nothing to do if not imported
5038 if not Is_Imported (E) then
5039 return;
5041 -- Test enabling conditions for wrapping
5043 elsif Is_Subprogram (E)
5044 and then Present (Contract (E))
5045 and then Present (Pre_Post_Conditions (Contract (E)))
5046 and then not GNATprove_Mode
5047 then
5048 -- Here we do the wrap
5050 -- Note on calls to Copy_Separate_Tree. The trees we are copying
5051 -- here are fully analyzed, but we definitely want fully syntactic
5052 -- unanalyzed trees in the body we construct, so that the analysis
5053 -- generates the right visibility, and that is exactly what the
5054 -- calls to Copy_Separate_Tree give us.
5056 Prag := Copy_Import_Pragma;
5058 -- Fix up spec so it is no longer imported and has convention Ada
5060 Set_Has_Completion (E, False);
5061 Set_Import_Pragma (E, Empty);
5062 Set_Interface_Name (E, Empty);
5063 Set_Is_Imported (E, False);
5064 Set_Convention (E, Convention_Ada);
5066 -- Grab the subprogram declaration and specification
5068 Spec := Declaration_Node (E);
5070 -- Build parameter list that we need
5072 Parms := New_List;
5073 Forml := First_Formal (E);
5074 while Present (Forml) loop
5075 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
5076 Next_Formal (Forml);
5077 end loop;
5079 -- Build the call
5081 if Ekind_In (E, E_Function, E_Generic_Function) then
5082 Stmt :=
5083 Make_Simple_Return_Statement (Loc,
5084 Expression =>
5085 Make_Function_Call (Loc,
5086 Name => Make_Identifier (Loc, CE),
5087 Parameter_Associations => Parms));
5089 else
5090 Stmt :=
5091 Make_Procedure_Call_Statement (Loc,
5092 Name => Make_Identifier (Loc, CE),
5093 Parameter_Associations => Parms);
5094 end if;
5096 -- Now build the body
5098 Bod :=
5099 Make_Subprogram_Body (Loc,
5100 Specification =>
5101 Copy_Separate_Tree (Spec),
5102 Declarations => New_List (
5103 Make_Subprogram_Declaration (Loc,
5104 Specification => Copy_Separate_Tree (Spec)),
5105 Prag),
5106 Handled_Statement_Sequence =>
5107 Make_Handled_Sequence_Of_Statements (Loc,
5108 Statements => New_List (Stmt),
5109 End_Label => Make_Identifier (Loc, CE)));
5111 -- Append the body to freeze result
5113 Add_To_Result (Bod);
5114 return;
5116 -- Case of imported subprogram that does not get wrapped
5118 else
5119 -- Set Is_Public. All imported entities need an external symbol
5120 -- created for them since they are always referenced from another
5121 -- object file. Note this used to be set when we set Is_Imported
5122 -- back in Sem_Prag, but now we delay it to this point, since we
5123 -- don't want to set this flag if we wrap an imported subprogram.
5125 Set_Is_Public (E);
5126 end if;
5127 end Wrap_Imported_Subprogram;
5129 -- Local variables
5131 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5132 -- Save the Ghost mode to restore on exit
5134 -- Start of processing for Freeze_Entity
5136 begin
5137 -- The entity being frozen may be subject to pragma Ghost. Set the mode
5138 -- now to ensure that any nodes generated during freezing are properly
5139 -- flagged as Ghost.
5141 Set_Ghost_Mode (E);
5143 -- We are going to test for various reasons why this entity need not be
5144 -- frozen here, but in the case of an Itype that's defined within a
5145 -- record, that test actually applies to the record.
5147 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
5148 Test_E := Scope (E);
5149 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
5150 and then Is_Record_Type (Underlying_Type (Scope (E)))
5151 then
5152 Test_E := Underlying_Type (Scope (E));
5153 end if;
5155 -- Do not freeze if already frozen since we only need one freeze node
5157 if Is_Frozen (E) then
5158 Result := No_List;
5159 goto Leave;
5161 elsif Ekind (E) = E_Generic_Package then
5162 Result := Freeze_Generic_Entities (E);
5163 goto Leave;
5165 -- It is improper to freeze an external entity within a generic because
5166 -- its freeze node will appear in a non-valid context. The entity will
5167 -- be frozen in the proper scope after the current generic is analyzed.
5168 -- However, aspects must be analyzed because they may be queried later
5169 -- within the generic itself, and the corresponding pragma or attribute
5170 -- definition has not been analyzed yet.
5172 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
5173 if Has_Delayed_Aspects (E) then
5174 Analyze_Aspects_At_Freeze_Point (E);
5175 end if;
5177 Result := No_List;
5178 goto Leave;
5180 -- AI05-0213: A formal incomplete type does not freeze the actual. In
5181 -- the instance, the same applies to the subtype renaming the actual.
5183 elsif Is_Private_Type (E)
5184 and then Is_Generic_Actual_Type (E)
5185 and then No (Full_View (Base_Type (E)))
5186 and then Ada_Version >= Ada_2012
5187 then
5188 Result := No_List;
5189 goto Leave;
5191 -- Formal subprograms are never frozen
5193 elsif Is_Formal_Subprogram (E) then
5194 Result := No_List;
5195 goto Leave;
5197 -- Generic types are never frozen as they lack delayed semantic checks
5199 elsif Is_Generic_Type (E) then
5200 Result := No_List;
5201 goto Leave;
5203 -- Do not freeze a global entity within an inner scope created during
5204 -- expansion. A call to subprogram E within some internal procedure
5205 -- (a stream attribute for example) might require freezing E, but the
5206 -- freeze node must appear in the same declarative part as E itself.
5207 -- The two-pass elaboration mechanism in gigi guarantees that E will
5208 -- be frozen before the inner call is elaborated. We exclude constants
5209 -- from this test, because deferred constants may be frozen early, and
5210 -- must be diagnosed (e.g. in the case of a deferred constant being used
5211 -- in a default expression). If the enclosing subprogram comes from
5212 -- source, or is a generic instance, then the freeze point is the one
5213 -- mandated by the language, and we freeze the entity. A subprogram that
5214 -- is a child unit body that acts as a spec does not have a spec that
5215 -- comes from source, but can only come from source.
5217 elsif In_Open_Scopes (Scope (Test_E))
5218 and then Scope (Test_E) /= Current_Scope
5219 and then Ekind (Test_E) /= E_Constant
5220 then
5221 declare
5222 S : Entity_Id;
5224 begin
5225 S := Current_Scope;
5226 while Present (S) loop
5227 if Is_Overloadable (S) then
5228 if Comes_From_Source (S)
5229 or else Is_Generic_Instance (S)
5230 or else Is_Child_Unit (S)
5231 then
5232 exit;
5233 else
5234 Result := No_List;
5235 goto Leave;
5236 end if;
5237 end if;
5239 S := Scope (S);
5240 end loop;
5241 end;
5243 -- Similarly, an inlined instance body may make reference to global
5244 -- entities, but these references cannot be the proper freezing point
5245 -- for them, and in the absence of inlining freezing will take place in
5246 -- their own scope. Normally instance bodies are analyzed after the
5247 -- enclosing compilation, and everything has been frozen at the proper
5248 -- place, but with front-end inlining an instance body is compiled
5249 -- before the end of the enclosing scope, and as a result out-of-order
5250 -- freezing must be prevented.
5252 elsif Front_End_Inlining
5253 and then In_Instance_Body
5254 and then Present (Scope (Test_E))
5255 then
5256 declare
5257 S : Entity_Id;
5259 begin
5260 S := Scope (Test_E);
5261 while Present (S) loop
5262 if Is_Generic_Instance (S) then
5263 exit;
5264 else
5265 S := Scope (S);
5266 end if;
5267 end loop;
5269 if No (S) then
5270 Result := No_List;
5271 goto Leave;
5272 end if;
5273 end;
5274 end if;
5276 -- Add checks to detect proper initialization of scalars that may appear
5277 -- as subprogram parameters.
5279 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
5280 Apply_Parameter_Validity_Checks (E);
5281 end if;
5283 -- Deal with delayed aspect specifications. The analysis of the aspect
5284 -- is required to be delayed to the freeze point, thus we analyze the
5285 -- pragma or attribute definition clause in the tree at this point. We
5286 -- also analyze the aspect specification node at the freeze point when
5287 -- the aspect doesn't correspond to pragma/attribute definition clause.
5288 -- In addition, a derived type may have inherited aspects that were
5289 -- delayed in the parent, so these must also be captured now.
5291 if Has_Delayed_Aspects (E)
5292 or else May_Inherit_Delayed_Rep_Aspects (E)
5293 then
5294 Analyze_Aspects_At_Freeze_Point (E);
5295 end if;
5297 -- Here to freeze the entity
5299 Set_Is_Frozen (E);
5301 -- Case of entity being frozen is other than a type
5303 if not Is_Type (E) then
5305 -- If entity is exported or imported and does not have an external
5306 -- name, now is the time to provide the appropriate default name.
5307 -- Skip this if the entity is stubbed, since we don't need a name
5308 -- for any stubbed routine. For the case on intrinsics, if no
5309 -- external name is specified, then calls will be handled in
5310 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
5311 -- external name is provided, then Expand_Intrinsic_Call leaves
5312 -- calls in place for expansion by GIGI.
5314 if (Is_Imported (E) or else Is_Exported (E))
5315 and then No (Interface_Name (E))
5316 and then Convention (E) /= Convention_Stubbed
5317 and then Convention (E) /= Convention_Intrinsic
5318 then
5319 Set_Encoded_Interface_Name
5320 (E, Get_Default_External_Name (E));
5322 -- If entity is an atomic object appearing in a declaration and
5323 -- the expression is an aggregate, assign it to a temporary to
5324 -- ensure that the actual assignment is done atomically rather
5325 -- than component-wise (the assignment to the temp may be done
5326 -- component-wise, but that is harmless).
5328 elsif Is_Atomic_Or_VFA (E)
5329 and then Nkind (Parent (E)) = N_Object_Declaration
5330 and then Present (Expression (Parent (E)))
5331 and then Nkind (Expression (Parent (E))) = N_Aggregate
5332 and then Is_Atomic_VFA_Aggregate (Expression (Parent (E)))
5333 then
5334 null;
5335 end if;
5337 -- Subprogram case
5339 if Is_Subprogram (E) then
5341 -- Check for needing to wrap imported subprogram
5343 Wrap_Imported_Subprogram (E);
5345 -- Freeze all parameter types and the return type (RM 13.14(14)).
5346 -- However skip this for internal subprograms. This is also where
5347 -- any extra formal parameters are created since we now know
5348 -- whether the subprogram will use a foreign convention.
5350 -- In Ada 2012, freezing a subprogram does not always freeze the
5351 -- corresponding profile (see AI05-019). An attribute reference
5352 -- is not a freezing point of the profile. Flag Do_Freeze_Profile
5353 -- indicates whether the profile should be frozen now.
5354 -- Other constructs that should not freeze ???
5356 -- This processing doesn't apply to internal entities (see below)
5358 if not Is_Internal (E) and then Do_Freeze_Profile then
5359 if not Freeze_Profile (E) then
5360 goto Leave;
5361 end if;
5362 end if;
5364 -- Must freeze its parent first if it is a derived subprogram
5366 if Present (Alias (E)) then
5367 Freeze_And_Append (Alias (E), N, Result);
5368 end if;
5370 -- We don't freeze internal subprograms, because we don't normally
5371 -- want addition of extra formals or mechanism setting to happen
5372 -- for those. However we do pass through predefined dispatching
5373 -- cases, since extra formals may be needed in some cases, such as
5374 -- for the stream 'Input function (build-in-place formals).
5376 if not Is_Internal (E)
5377 or else Is_Predefined_Dispatching_Operation (E)
5378 then
5379 Freeze_Subprogram (E);
5380 end if;
5382 -- If warning on suspicious contracts then check for the case of
5383 -- a postcondition other than False for a No_Return subprogram.
5385 if No_Return (E)
5386 and then Warn_On_Suspicious_Contract
5387 and then Present (Contract (E))
5388 then
5389 declare
5390 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
5391 Exp : Node_Id;
5393 begin
5394 while Present (Prag) loop
5395 if Nam_In (Pragma_Name_Unmapped (Prag),
5396 Name_Post,
5397 Name_Postcondition,
5398 Name_Refined_Post)
5399 then
5400 Exp :=
5401 Expression
5402 (First (Pragma_Argument_Associations (Prag)));
5404 if Nkind (Exp) /= N_Identifier
5405 or else Chars (Exp) /= Name_False
5406 then
5407 Error_Msg_NE
5408 ("useless postcondition, & is marked "
5409 & "No_Return?T?", Exp, E);
5410 end if;
5411 end if;
5413 Prag := Next_Pragma (Prag);
5414 end loop;
5415 end;
5416 end if;
5418 -- Here for other than a subprogram or type
5420 else
5421 -- If entity has a type, and it is not a generic unit, then
5422 -- freeze it first (RM 13.14(10)).
5424 if Present (Etype (E))
5425 and then Ekind (E) /= E_Generic_Function
5426 then
5427 Freeze_And_Append (Etype (E), N, Result);
5429 -- For an object of an anonymous array type, aspects on the
5430 -- object declaration apply to the type itself. This is the
5431 -- case for Atomic_Components, Volatile_Components, and
5432 -- Independent_Components. In these cases analysis of the
5433 -- generated pragma will mark the anonymous types accordingly,
5434 -- and the object itself does not require a freeze node.
5436 if Ekind (E) = E_Variable
5437 and then Is_Itype (Etype (E))
5438 and then Is_Array_Type (Etype (E))
5439 and then Has_Delayed_Aspects (E)
5440 then
5441 Set_Has_Delayed_Aspects (E, False);
5442 Set_Has_Delayed_Freeze (E, False);
5443 Set_Freeze_Node (E, Empty);
5444 end if;
5445 end if;
5447 -- Special processing for objects created by object declaration
5449 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
5450 Freeze_Object_Declaration (E);
5451 end if;
5453 -- Check that a constant which has a pragma Volatile[_Components]
5454 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
5456 -- Note: Atomic[_Components] also sets Volatile[_Components]
5458 if Ekind (E) = E_Constant
5459 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
5460 and then not Is_Imported (E)
5461 and then not Has_Boolean_Aspect_Import (E)
5462 then
5463 -- Make sure we actually have a pragma, and have not merely
5464 -- inherited the indication from elsewhere (e.g. an address
5465 -- clause, which is not good enough in RM terms).
5467 if Has_Rep_Pragma (E, Name_Atomic)
5468 or else
5469 Has_Rep_Pragma (E, Name_Atomic_Components)
5470 then
5471 Error_Msg_N
5472 ("stand alone atomic constant must be " &
5473 "imported (RM C.6(13))", E);
5475 elsif Has_Rep_Pragma (E, Name_Volatile)
5476 or else
5477 Has_Rep_Pragma (E, Name_Volatile_Components)
5478 then
5479 Error_Msg_N
5480 ("stand alone volatile constant must be " &
5481 "imported (RM C.6(13))", E);
5482 end if;
5483 end if;
5485 -- Static objects require special handling
5487 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
5488 and then Is_Statically_Allocated (E)
5489 then
5490 Freeze_Static_Object (E);
5491 end if;
5493 -- Remaining step is to layout objects
5495 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
5496 or else Is_Formal (E)
5497 then
5498 Layout_Object (E);
5499 end if;
5501 -- For an object that does not have delayed freezing, and whose
5502 -- initialization actions have been captured in a compound
5503 -- statement, move them back now directly within the enclosing
5504 -- statement sequence.
5506 if Ekind_In (E, E_Constant, E_Variable)
5507 and then not Has_Delayed_Freeze (E)
5508 then
5509 Explode_Initialization_Compound_Statement (E);
5510 end if;
5512 -- Do not generate a freeze node for a generic unit
5514 if Is_Generic_Unit (E) then
5515 Result := No_List;
5516 goto Leave;
5517 end if;
5518 end if;
5520 -- Case of a type or subtype being frozen
5522 else
5523 -- Verify several SPARK legality rules related to Ghost types now
5524 -- that the type is frozen.
5526 Check_Ghost_Type (E);
5528 -- We used to check here that a full type must have preelaborable
5529 -- initialization if it completes a private type specified with
5530 -- pragma Preelaborable_Initialization, but that missed cases where
5531 -- the types occur within a generic package, since the freezing
5532 -- that occurs within a containing scope generally skips traversal
5533 -- of a generic unit's declarations (those will be frozen within
5534 -- instances). This check was moved to Analyze_Package_Specification.
5536 -- The type may be defined in a generic unit. This can occur when
5537 -- freezing a generic function that returns the type (which is
5538 -- defined in a parent unit). It is clearly meaningless to freeze
5539 -- this type. However, if it is a subtype, its size may be determi-
5540 -- nable and used in subsequent checks, so might as well try to
5541 -- compute it.
5543 -- In Ada 2012, Freeze_Entities is also used in the front end to
5544 -- trigger the analysis of aspect expressions, so in this case we
5545 -- want to continue the freezing process.
5547 -- Is_Generic_Unit (Scope (E)) is dubious here, do we want instead
5548 -- In_Generic_Scope (E)???
5550 if Present (Scope (E))
5551 and then Is_Generic_Unit (Scope (E))
5552 and then
5553 (not Has_Predicates (E)
5554 and then not Has_Delayed_Freeze (E))
5555 then
5556 Check_Compile_Time_Size (E);
5557 Result := No_List;
5558 goto Leave;
5559 end if;
5561 -- Check for error of Type_Invariant'Class applied to an untagged
5562 -- type (check delayed to freeze time when full type is available).
5564 declare
5565 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
5566 begin
5567 if Present (Prag)
5568 and then Class_Present (Prag)
5569 and then not Is_Tagged_Type (E)
5570 then
5571 Error_Msg_NE
5572 ("Type_Invariant''Class cannot be specified for &", Prag, E);
5573 Error_Msg_N
5574 ("\can only be specified for a tagged type", Prag);
5575 end if;
5576 end;
5578 -- Deal with special cases of freezing for subtype
5580 if E /= Base_Type (E) then
5582 -- Before we do anything else, a specific test for the case of a
5583 -- size given for an array where the array would need to be packed
5584 -- in order for the size to be honored, but is not. This is the
5585 -- case where implicit packing may apply. The reason we do this so
5586 -- early is that, if we have implicit packing, the layout of the
5587 -- base type is affected, so we must do this before we freeze the
5588 -- base type.
5590 -- We could do this processing only if implicit packing is enabled
5591 -- since in all other cases, the error would be caught by the back
5592 -- end. However, we choose to do the check even if we do not have
5593 -- implicit packing enabled, since this allows us to give a more
5594 -- useful error message (advising use of pragma Implicit_Packing
5595 -- or pragma Pack).
5597 if Is_Array_Type (E) then
5598 declare
5599 Ctyp : constant Entity_Id := Component_Type (E);
5600 Rsiz : constant Uint := RM_Size (Ctyp);
5601 SZ : constant Node_Id := Size_Clause (E);
5602 Btyp : constant Entity_Id := Base_Type (E);
5604 Lo : Node_Id;
5605 Hi : Node_Id;
5606 Indx : Node_Id;
5608 Dim : Uint;
5609 Num_Elmts : Uint := Uint_1;
5610 -- Number of elements in array
5612 begin
5613 -- Check enabling conditions. These are straightforward
5614 -- except for the test for a limited composite type. This
5615 -- eliminates the rare case of a array of limited components
5616 -- where there are issues of whether or not we can go ahead
5617 -- and pack the array (since we can't freely pack and unpack
5618 -- arrays if they are limited).
5620 -- Note that we check the root type explicitly because the
5621 -- whole point is we are doing this test before we have had
5622 -- a chance to freeze the base type (and it is that freeze
5623 -- action that causes stuff to be inherited).
5625 -- The conditions on the size are identical to those used in
5626 -- Freeze_Array_Type to set the Is_Packed flag.
5628 if Has_Size_Clause (E)
5629 and then Known_Static_RM_Size (E)
5630 and then not Is_Packed (E)
5631 and then not Has_Pragma_Pack (E)
5632 and then not Has_Component_Size_Clause (E)
5633 and then Known_Static_RM_Size (Ctyp)
5634 and then Rsiz <= 64
5635 and then not (Addressable (Rsiz)
5636 and then Known_Static_Esize (Ctyp)
5637 and then Esize (Ctyp) = Rsiz)
5638 and then not (Rsiz mod System_Storage_Unit = 0
5639 and then Is_Composite_Type (Ctyp))
5640 and then not Is_Limited_Composite (E)
5641 and then not Is_Packed (Root_Type (E))
5642 and then not Has_Component_Size_Clause (Root_Type (E))
5643 and then not (CodePeer_Mode or GNATprove_Mode)
5644 then
5645 -- Compute number of elements in array
5647 Indx := First_Index (E);
5648 while Present (Indx) loop
5649 Get_Index_Bounds (Indx, Lo, Hi);
5651 if not (Compile_Time_Known_Value (Lo)
5652 and then
5653 Compile_Time_Known_Value (Hi))
5654 then
5655 goto No_Implicit_Packing;
5656 end if;
5658 Dim := Expr_Value (Hi) - Expr_Value (Lo) + 1;
5660 if Dim >= 0 then
5661 Num_Elmts := Num_Elmts * Dim;
5662 else
5663 Num_Elmts := Uint_0;
5664 end if;
5666 Next_Index (Indx);
5667 end loop;
5669 -- What we are looking for here is the situation where
5670 -- the RM_Size given would be exactly right if there was
5671 -- a pragma Pack, resulting in the component size being
5672 -- the RM_Size of the component type.
5674 if RM_Size (E) = Num_Elmts * Rsiz then
5676 -- For implicit packing mode, just set the component
5677 -- size and Freeze_Array_Type will do the rest.
5679 if Implicit_Packing then
5680 Set_Component_Size (Btyp, Rsiz);
5682 -- Otherwise give an error message
5684 else
5685 Error_Msg_NE
5686 ("size given for& too small", SZ, E);
5687 Error_Msg_N -- CODEFIX
5688 ("\use explicit pragma Pack or use pragma "
5689 & "Implicit_Packing", SZ);
5690 end if;
5691 end if;
5692 end if;
5693 end;
5694 end if;
5696 <<No_Implicit_Packing>>
5698 -- If ancestor subtype present, freeze that first. Note that this
5699 -- will also get the base type frozen. Need RM reference ???
5701 Atype := Ancestor_Subtype (E);
5703 if Present (Atype) then
5704 Freeze_And_Append (Atype, N, Result);
5706 -- No ancestor subtype present
5708 else
5709 -- See if we have a nearest ancestor that has a predicate.
5710 -- That catches the case of derived type with a predicate.
5711 -- Need RM reference here ???
5713 Atype := Nearest_Ancestor (E);
5715 if Present (Atype) and then Has_Predicates (Atype) then
5716 Freeze_And_Append (Atype, N, Result);
5717 end if;
5719 -- Freeze base type before freezing the entity (RM 13.14(15))
5721 if E /= Base_Type (E) then
5722 Freeze_And_Append (Base_Type (E), N, Result);
5723 end if;
5724 end if;
5726 -- A subtype inherits all the type-related representation aspects
5727 -- from its parents (RM 13.1(8)).
5729 Inherit_Aspects_At_Freeze_Point (E);
5731 -- For a derived type, freeze its parent type first (RM 13.14(15))
5733 elsif Is_Derived_Type (E) then
5734 Freeze_And_Append (Etype (E), N, Result);
5735 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5737 -- A derived type inherits each type-related representation aspect
5738 -- of its parent type that was directly specified before the
5739 -- declaration of the derived type (RM 13.1(15)).
5741 Inherit_Aspects_At_Freeze_Point (E);
5742 end if;
5744 -- Check for incompatible size and alignment for record type
5746 if Warn_On_Size_Alignment
5747 and then Is_Record_Type (E)
5748 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5750 -- If explicit Object_Size clause given assume that the programmer
5751 -- knows what he is doing, and expects the compiler behavior.
5753 and then not Has_Object_Size_Clause (E)
5755 -- Check for size not a multiple of alignment
5757 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5758 then
5759 declare
5760 SC : constant Node_Id := Size_Clause (E);
5761 AC : constant Node_Id := Alignment_Clause (E);
5762 Loc : Node_Id;
5763 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5765 begin
5766 if Present (SC) and then Present (AC) then
5768 -- Give a warning
5770 if Sloc (SC) > Sloc (AC) then
5771 Loc := SC;
5772 Error_Msg_NE
5773 ("?Z?size is not a multiple of alignment for &",
5774 Loc, E);
5775 Error_Msg_Sloc := Sloc (AC);
5776 Error_Msg_Uint_1 := Alignment (E);
5777 Error_Msg_N ("\?Z?alignment of ^ specified #", Loc);
5779 else
5780 Loc := AC;
5781 Error_Msg_NE
5782 ("?Z?size is not a multiple of alignment for &",
5783 Loc, E);
5784 Error_Msg_Sloc := Sloc (SC);
5785 Error_Msg_Uint_1 := RM_Size (E);
5786 Error_Msg_N ("\?Z?size of ^ specified #", Loc);
5787 end if;
5789 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5790 Error_Msg_N ("\?Z?Object_Size will be increased to ^", Loc);
5791 end if;
5792 end;
5793 end if;
5795 -- Array type
5797 if Is_Array_Type (E) then
5798 Freeze_Array_Type (E);
5800 -- For a class-wide type, the corresponding specific type is
5801 -- frozen as well (RM 13.14(15))
5803 elsif Is_Class_Wide_Type (E) then
5804 Freeze_And_Append (Root_Type (E), N, Result);
5806 -- If the base type of the class-wide type is still incomplete,
5807 -- the class-wide remains unfrozen as well. This is legal when
5808 -- E is the formal of a primitive operation of some other type
5809 -- which is being frozen.
5811 if not Is_Frozen (Root_Type (E)) then
5812 Set_Is_Frozen (E, False);
5813 goto Leave;
5814 end if;
5816 -- The equivalent type associated with a class-wide subtype needs
5817 -- to be frozen to ensure that its layout is done.
5819 if Ekind (E) = E_Class_Wide_Subtype
5820 and then Present (Equivalent_Type (E))
5821 then
5822 Freeze_And_Append (Equivalent_Type (E), N, Result);
5823 end if;
5825 -- Generate an itype reference for a library-level class-wide type
5826 -- at the freeze point. Otherwise the first explicit reference to
5827 -- the type may appear in an inner scope which will be rejected by
5828 -- the back-end.
5830 if Is_Itype (E)
5831 and then Is_Compilation_Unit (Scope (E))
5832 then
5833 declare
5834 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5836 begin
5837 Set_Itype (Ref, E);
5839 -- From a gigi point of view, a class-wide subtype derives
5840 -- from its record equivalent type. As a result, the itype
5841 -- reference must appear after the freeze node of the
5842 -- equivalent type or gigi will reject the reference.
5844 if Ekind (E) = E_Class_Wide_Subtype
5845 and then Present (Equivalent_Type (E))
5846 then
5847 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5848 else
5849 Add_To_Result (Ref);
5850 end if;
5851 end;
5852 end if;
5854 -- For a record type or record subtype, freeze all component types
5855 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5856 -- using Is_Record_Type, because we don't want to attempt the freeze
5857 -- for the case of a private type with record extension (we will do
5858 -- that later when the full type is frozen).
5860 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype) then
5861 if not In_Generic_Scope (E) then
5862 Freeze_Record_Type (E);
5863 end if;
5865 -- Report a warning if a discriminated record base type has a
5866 -- convention with language C or C++ applied to it. This check is
5867 -- done even within generic scopes (but not in instantiations),
5868 -- which is why we don't do it as part of Freeze_Record_Type.
5870 Check_Suspicious_Convention (E);
5872 -- For a concurrent type, freeze corresponding record type. This does
5873 -- not correspond to any specific rule in the RM, but the record type
5874 -- is essentially part of the concurrent type. Also freeze all local
5875 -- entities. This includes record types created for entry parameter
5876 -- blocks and whatever local entities may appear in the private part.
5878 elsif Is_Concurrent_Type (E) then
5879 if Present (Corresponding_Record_Type (E)) then
5880 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5881 end if;
5883 Comp := First_Entity (E);
5884 while Present (Comp) loop
5885 if Is_Type (Comp) then
5886 Freeze_And_Append (Comp, N, Result);
5888 elsif (Ekind (Comp)) /= E_Function then
5890 -- The guard on the presence of the Etype seems to be needed
5891 -- for some CodePeer (-gnatcC) cases, but not clear why???
5893 if Present (Etype (Comp)) then
5894 if Is_Itype (Etype (Comp))
5895 and then Underlying_Type (Scope (Etype (Comp))) = E
5896 then
5897 Undelay_Type (Etype (Comp));
5898 end if;
5900 Freeze_And_Append (Etype (Comp), N, Result);
5901 end if;
5902 end if;
5904 Next_Entity (Comp);
5905 end loop;
5907 -- Private types are required to point to the same freeze node as
5908 -- their corresponding full views. The freeze node itself has to
5909 -- point to the partial view of the entity (because from the partial
5910 -- view, we can retrieve the full view, but not the reverse).
5911 -- However, in order to freeze correctly, we need to freeze the full
5912 -- view. If we are freezing at the end of a scope (or within the
5913 -- scope) of the private type, the partial and full views will have
5914 -- been swapped, the full view appears first in the entity chain and
5915 -- the swapping mechanism ensures that the pointers are properly set
5916 -- (on scope exit).
5918 -- If we encounter the partial view before the full view (e.g. when
5919 -- freezing from another scope), we freeze the full view, and then
5920 -- set the pointers appropriately since we cannot rely on swapping to
5921 -- fix things up (subtypes in an outer scope might not get swapped).
5923 -- If the full view is itself private, the above requirements apply
5924 -- to the underlying full view instead of the full view. But there is
5925 -- no swapping mechanism for the underlying full view so we need to
5926 -- set the pointers appropriately in both cases.
5928 elsif Is_Incomplete_Or_Private_Type (E)
5929 and then not Is_Generic_Type (E)
5930 then
5931 -- The construction of the dispatch table associated with library
5932 -- level tagged types forces freezing of all the primitives of the
5933 -- type, which may cause premature freezing of the partial view.
5934 -- For example:
5936 -- package Pkg is
5937 -- type T is tagged private;
5938 -- type DT is new T with private;
5939 -- procedure Prim (X : in out T; Y : in out DT'Class);
5940 -- private
5941 -- type T is tagged null record;
5942 -- Obj : T;
5943 -- type DT is new T with null record;
5944 -- end;
5946 -- In this case the type will be frozen later by the usual
5947 -- mechanism: an object declaration, an instantiation, or the
5948 -- end of a declarative part.
5950 if Is_Library_Level_Tagged_Type (E)
5951 and then not Present (Full_View (E))
5952 then
5953 Set_Is_Frozen (E, False);
5954 goto Leave;
5956 -- Case of full view present
5958 elsif Present (Full_View (E)) then
5960 -- If full view has already been frozen, then no further
5961 -- processing is required
5963 if Is_Frozen (Full_View (E)) then
5964 Set_Has_Delayed_Freeze (E, False);
5965 Set_Freeze_Node (E, Empty);
5967 -- Otherwise freeze full view and patch the pointers so that
5968 -- the freeze node will elaborate both views in the back end.
5969 -- However, if full view is itself private, freeze underlying
5970 -- full view instead and patch the pointers so that the freeze
5971 -- node will elaborate the three views in the back end.
5973 else
5974 declare
5975 Full : Entity_Id := Full_View (E);
5977 begin
5978 if Is_Private_Type (Full)
5979 and then Present (Underlying_Full_View (Full))
5980 then
5981 Full := Underlying_Full_View (Full);
5982 end if;
5984 Freeze_And_Append (Full, N, Result);
5986 if Full /= Full_View (E)
5987 and then Has_Delayed_Freeze (Full_View (E))
5988 then
5989 F_Node := Freeze_Node (Full);
5991 if Present (F_Node) then
5992 Inherit_Freeze_Node
5993 (Fnod => F_Node,
5994 Typ => Full_View (E));
5995 else
5996 Set_Has_Delayed_Freeze (Full_View (E), False);
5997 Set_Freeze_Node (Full_View (E), Empty);
5998 end if;
5999 end if;
6001 if Has_Delayed_Freeze (E) then
6002 F_Node := Freeze_Node (Full_View (E));
6004 if Present (F_Node) then
6005 Inherit_Freeze_Node
6006 (Fnod => F_Node,
6007 Typ => E);
6008 else
6009 -- {Incomplete,Private}_Subtypes with Full_Views
6010 -- constrained by discriminants.
6012 Set_Has_Delayed_Freeze (E, False);
6013 Set_Freeze_Node (E, Empty);
6014 end if;
6015 end if;
6016 end;
6017 end if;
6019 Check_Debug_Info_Needed (E);
6021 -- AI-117 requires that the convention of a partial view be the
6022 -- same as the convention of the full view. Note that this is a
6023 -- recognized breach of privacy, but it's essential for logical
6024 -- consistency of representation, and the lack of a rule in
6025 -- RM95 was an oversight.
6027 Set_Convention (E, Convention (Full_View (E)));
6029 Set_Size_Known_At_Compile_Time (E,
6030 Size_Known_At_Compile_Time (Full_View (E)));
6032 -- Size information is copied from the full view to the
6033 -- incomplete or private view for consistency.
6035 -- We skip this is the full view is not a type. This is very
6036 -- strange of course, and can only happen as a result of
6037 -- certain illegalities, such as a premature attempt to derive
6038 -- from an incomplete type.
6040 if Is_Type (Full_View (E)) then
6041 Set_Size_Info (E, Full_View (E));
6042 Set_RM_Size (E, RM_Size (Full_View (E)));
6043 end if;
6045 goto Leave;
6047 -- Case of underlying full view present
6049 elsif Is_Private_Type (E)
6050 and then Present (Underlying_Full_View (E))
6051 then
6052 if not Is_Frozen (Underlying_Full_View (E)) then
6053 Freeze_And_Append (Underlying_Full_View (E), N, Result);
6054 end if;
6056 -- Patch the pointers so that the freeze node will elaborate
6057 -- both views in the back end.
6059 if Has_Delayed_Freeze (E) then
6060 F_Node := Freeze_Node (Underlying_Full_View (E));
6062 if Present (F_Node) then
6063 Inherit_Freeze_Node
6064 (Fnod => F_Node,
6065 Typ => E);
6066 else
6067 Set_Has_Delayed_Freeze (E, False);
6068 Set_Freeze_Node (E, Empty);
6069 end if;
6070 end if;
6072 Check_Debug_Info_Needed (E);
6074 goto Leave;
6076 -- Case of no full view present. If entity is derived or subtype,
6077 -- it is safe to freeze, correctness depends on the frozen status
6078 -- of parent. Otherwise it is either premature usage, or a Taft
6079 -- amendment type, so diagnosis is at the point of use and the
6080 -- type might be frozen later.
6082 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
6083 null;
6085 else
6086 Set_Is_Frozen (E, False);
6087 Result := No_List;
6088 goto Leave;
6089 end if;
6091 -- For access subprogram, freeze types of all formals, the return
6092 -- type was already frozen, since it is the Etype of the function.
6093 -- Formal types can be tagged Taft amendment types, but otherwise
6094 -- they cannot be incomplete.
6096 elsif Ekind (E) = E_Subprogram_Type then
6097 Formal := First_Formal (E);
6098 while Present (Formal) loop
6099 if Ekind (Etype (Formal)) = E_Incomplete_Type
6100 and then No (Full_View (Etype (Formal)))
6101 then
6102 if Is_Tagged_Type (Etype (Formal)) then
6103 null;
6105 -- AI05-151: Incomplete types are allowed in access to
6106 -- subprogram specifications.
6108 elsif Ada_Version < Ada_2012 then
6109 Error_Msg_NE
6110 ("invalid use of incomplete type&", E, Etype (Formal));
6111 end if;
6112 end if;
6114 Freeze_And_Append (Etype (Formal), N, Result);
6115 Next_Formal (Formal);
6116 end loop;
6118 Freeze_Subprogram (E);
6120 -- For access to a protected subprogram, freeze the equivalent type
6121 -- (however this is not set if we are not generating code or if this
6122 -- is an anonymous type used just for resolution).
6124 elsif Is_Access_Protected_Subprogram_Type (E) then
6125 if Present (Equivalent_Type (E)) then
6126 Freeze_And_Append (Equivalent_Type (E), N, Result);
6127 end if;
6128 end if;
6130 -- Generic types are never seen by the back-end, and are also not
6131 -- processed by the expander (since the expander is turned off for
6132 -- generic processing), so we never need freeze nodes for them.
6134 if Is_Generic_Type (E) then
6135 goto Leave;
6136 end if;
6138 -- Some special processing for non-generic types to complete
6139 -- representation details not known till the freeze point.
6141 if Is_Fixed_Point_Type (E) then
6142 Freeze_Fixed_Point_Type (E);
6144 -- Some error checks required for ordinary fixed-point type. Defer
6145 -- these till the freeze-point since we need the small and range
6146 -- values. We only do these checks for base types
6148 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
6149 if Small_Value (E) < Ureal_2_M_80 then
6150 Error_Msg_Name_1 := Name_Small;
6151 Error_Msg_N
6152 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
6154 elsif Small_Value (E) > Ureal_2_80 then
6155 Error_Msg_Name_1 := Name_Small;
6156 Error_Msg_N
6157 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
6158 end if;
6160 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
6161 Error_Msg_Name_1 := Name_First;
6162 Error_Msg_N
6163 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
6164 end if;
6166 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
6167 Error_Msg_Name_1 := Name_Last;
6168 Error_Msg_N
6169 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
6170 end if;
6171 end if;
6173 elsif Is_Enumeration_Type (E) then
6174 Freeze_Enumeration_Type (E);
6176 elsif Is_Integer_Type (E) then
6177 Adjust_Esize_For_Alignment (E);
6179 if Is_Modular_Integer_Type (E)
6180 and then Warn_On_Suspicious_Modulus_Value
6181 then
6182 Check_Suspicious_Modulus (E);
6183 end if;
6185 -- The pool applies to named and anonymous access types, but not
6186 -- to subprogram and to internal types generated for 'Access
6187 -- references.
6189 elsif Is_Access_Type (E)
6190 and then not Is_Access_Subprogram_Type (E)
6191 and then Ekind (E) /= E_Access_Attribute_Type
6192 then
6193 -- If a pragma Default_Storage_Pool applies, and this type has no
6194 -- Storage_Pool or Storage_Size clause (which must have occurred
6195 -- before the freezing point), then use the default. This applies
6196 -- only to base types.
6198 -- None of this applies to access to subprograms, for which there
6199 -- are clearly no pools.
6201 if Present (Default_Pool)
6202 and then Is_Base_Type (E)
6203 and then not Has_Storage_Size_Clause (E)
6204 and then No (Associated_Storage_Pool (E))
6205 then
6206 -- Case of pragma Default_Storage_Pool (null)
6208 if Nkind (Default_Pool) = N_Null then
6209 Set_No_Pool_Assigned (E);
6211 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
6213 else
6214 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
6215 end if;
6216 end if;
6218 -- Check restriction for standard storage pool
6220 if No (Associated_Storage_Pool (E)) then
6221 Check_Restriction (No_Standard_Storage_Pools, E);
6222 end if;
6224 -- Deal with error message for pure access type. This is not an
6225 -- error in Ada 2005 if there is no pool (see AI-366).
6227 if Is_Pure_Unit_Access_Type (E)
6228 and then (Ada_Version < Ada_2005
6229 or else not No_Pool_Assigned (E))
6230 and then not Is_Generic_Unit (Scope (E))
6231 then
6232 Error_Msg_N ("named access type not allowed in pure unit", E);
6234 if Ada_Version >= Ada_2005 then
6235 Error_Msg_N
6236 ("\would be legal if Storage_Size of 0 given??", E);
6238 elsif No_Pool_Assigned (E) then
6239 Error_Msg_N
6240 ("\would be legal in Ada 2005??", E);
6242 else
6243 Error_Msg_N
6244 ("\would be legal in Ada 2005 if "
6245 & "Storage_Size of 0 given??", E);
6246 end if;
6247 end if;
6248 end if;
6250 -- Case of composite types
6252 if Is_Composite_Type (E) then
6254 -- AI-117 requires that all new primitives of a tagged type must
6255 -- inherit the convention of the full view of the type. Inherited
6256 -- and overriding operations are defined to inherit the convention
6257 -- of their parent or overridden subprogram (also specified in
6258 -- AI-117), which will have occurred earlier (in Derive_Subprogram
6259 -- and New_Overloaded_Entity). Here we set the convention of
6260 -- primitives that are still convention Ada, which will ensure
6261 -- that any new primitives inherit the type's convention. Class-
6262 -- wide types can have a foreign convention inherited from their
6263 -- specific type, but are excluded from this since they don't have
6264 -- any associated primitives.
6266 if Is_Tagged_Type (E)
6267 and then not Is_Class_Wide_Type (E)
6268 and then Convention (E) /= Convention_Ada
6269 then
6270 declare
6271 Prim_List : constant Elist_Id := Primitive_Operations (E);
6272 Prim : Elmt_Id;
6274 begin
6275 Prim := First_Elmt (Prim_List);
6276 while Present (Prim) loop
6277 if Convention (Node (Prim)) = Convention_Ada then
6278 Set_Convention (Node (Prim), Convention (E));
6279 end if;
6281 Next_Elmt (Prim);
6282 end loop;
6283 end;
6284 end if;
6286 -- If the type is a simple storage pool type, then this is where
6287 -- we attempt to locate and validate its Allocate, Deallocate, and
6288 -- Storage_Size operations (the first is required, and the latter
6289 -- two are optional). We also verify that the full type for a
6290 -- private type is allowed to be a simple storage pool type.
6292 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
6293 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
6294 then
6295 -- If the type is marked Has_Private_Declaration, then this is
6296 -- a full type for a private type that was specified with the
6297 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
6298 -- pragma is allowed for the full type (for example, it can't
6299 -- be an array type, or a nonlimited record type).
6301 if Has_Private_Declaration (E) then
6302 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
6303 and then not Is_Private_Type (E)
6304 then
6305 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
6306 Error_Msg_N
6307 ("pragma% can only apply to full type that is an " &
6308 "explicitly limited type", E);
6309 end if;
6310 end if;
6312 Validate_Simple_Pool_Ops : declare
6313 Pool_Type : Entity_Id renames E;
6314 Address_Type : constant Entity_Id := RTE (RE_Address);
6315 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
6317 procedure Validate_Simple_Pool_Op_Formal
6318 (Pool_Op : Entity_Id;
6319 Pool_Op_Formal : in out Entity_Id;
6320 Expected_Mode : Formal_Kind;
6321 Expected_Type : Entity_Id;
6322 Formal_Name : String;
6323 OK_Formal : in out Boolean);
6324 -- Validate one formal Pool_Op_Formal of the candidate pool
6325 -- operation Pool_Op. The formal must be of Expected_Type
6326 -- and have mode Expected_Mode. OK_Formal will be set to
6327 -- False if the formal doesn't match. If OK_Formal is False
6328 -- on entry, then the formal will effectively be ignored
6329 -- (because validation of the pool op has already failed).
6330 -- Upon return, Pool_Op_Formal will be updated to the next
6331 -- formal, if any.
6333 procedure Validate_Simple_Pool_Operation
6334 (Op_Name : Name_Id);
6335 -- Search for and validate a simple pool operation with the
6336 -- name Op_Name. If the name is Allocate, then there must be
6337 -- exactly one such primitive operation for the simple pool
6338 -- type. If the name is Deallocate or Storage_Size, then
6339 -- there can be at most one such primitive operation. The
6340 -- profile of the located primitive must conform to what
6341 -- is expected for each operation.
6343 ------------------------------------
6344 -- Validate_Simple_Pool_Op_Formal --
6345 ------------------------------------
6347 procedure Validate_Simple_Pool_Op_Formal
6348 (Pool_Op : Entity_Id;
6349 Pool_Op_Formal : in out Entity_Id;
6350 Expected_Mode : Formal_Kind;
6351 Expected_Type : Entity_Id;
6352 Formal_Name : String;
6353 OK_Formal : in out Boolean)
6355 begin
6356 -- If OK_Formal is False on entry, then simply ignore
6357 -- the formal, because an earlier formal has already
6358 -- been flagged.
6360 if not OK_Formal then
6361 return;
6363 -- If no formal is passed in, then issue an error for a
6364 -- missing formal.
6366 elsif not Present (Pool_Op_Formal) then
6367 Error_Msg_NE
6368 ("simple storage pool op missing formal " &
6369 Formal_Name & " of type&", Pool_Op, Expected_Type);
6370 OK_Formal := False;
6372 return;
6373 end if;
6375 if Etype (Pool_Op_Formal) /= Expected_Type then
6377 -- If the pool type was expected for this formal, then
6378 -- this will not be considered a candidate operation
6379 -- for the simple pool, so we unset OK_Formal so that
6380 -- the op and any later formals will be ignored.
6382 if Expected_Type = Pool_Type then
6383 OK_Formal := False;
6385 return;
6387 else
6388 Error_Msg_NE
6389 ("wrong type for formal " & Formal_Name &
6390 " of simple storage pool op; expected type&",
6391 Pool_Op_Formal, Expected_Type);
6392 end if;
6393 end if;
6395 -- Issue error if formal's mode is not the expected one
6397 if Ekind (Pool_Op_Formal) /= Expected_Mode then
6398 Error_Msg_N
6399 ("wrong mode for formal of simple storage pool op",
6400 Pool_Op_Formal);
6401 end if;
6403 -- Advance to the next formal
6405 Next_Formal (Pool_Op_Formal);
6406 end Validate_Simple_Pool_Op_Formal;
6408 ------------------------------------
6409 -- Validate_Simple_Pool_Operation --
6410 ------------------------------------
6412 procedure Validate_Simple_Pool_Operation
6413 (Op_Name : Name_Id)
6415 Op : Entity_Id;
6416 Found_Op : Entity_Id := Empty;
6417 Formal : Entity_Id;
6418 Is_OK : Boolean;
6420 begin
6421 pragma Assert
6422 (Nam_In (Op_Name, Name_Allocate,
6423 Name_Deallocate,
6424 Name_Storage_Size));
6426 Error_Msg_Name_1 := Op_Name;
6428 -- For each homonym declared immediately in the scope
6429 -- of the simple storage pool type, determine whether
6430 -- the homonym is an operation of the pool type, and,
6431 -- if so, check that its profile is as expected for
6432 -- a simple pool operation of that name.
6434 Op := Get_Name_Entity_Id (Op_Name);
6435 while Present (Op) loop
6436 if Ekind_In (Op, E_Function, E_Procedure)
6437 and then Scope (Op) = Current_Scope
6438 then
6439 Formal := First_Entity (Op);
6441 Is_OK := True;
6443 -- The first parameter must be of the pool type
6444 -- in order for the operation to qualify.
6446 if Op_Name = Name_Storage_Size then
6447 Validate_Simple_Pool_Op_Formal
6448 (Op, Formal, E_In_Parameter, Pool_Type,
6449 "Pool", Is_OK);
6450 else
6451 Validate_Simple_Pool_Op_Formal
6452 (Op, Formal, E_In_Out_Parameter, Pool_Type,
6453 "Pool", Is_OK);
6454 end if;
6456 -- If another operation with this name has already
6457 -- been located for the type, then flag an error,
6458 -- since we only allow the type to have a single
6459 -- such primitive.
6461 if Present (Found_Op) and then Is_OK then
6462 Error_Msg_NE
6463 ("only one % operation allowed for " &
6464 "simple storage pool type&", Op, Pool_Type);
6465 end if;
6467 -- In the case of Allocate and Deallocate, a formal
6468 -- of type System.Address is required.
6470 if Op_Name = Name_Allocate then
6471 Validate_Simple_Pool_Op_Formal
6472 (Op, Formal, E_Out_Parameter,
6473 Address_Type, "Storage_Address", Is_OK);
6475 elsif Op_Name = Name_Deallocate then
6476 Validate_Simple_Pool_Op_Formal
6477 (Op, Formal, E_In_Parameter,
6478 Address_Type, "Storage_Address", Is_OK);
6479 end if;
6481 -- In the case of Allocate and Deallocate, formals
6482 -- of type Storage_Count are required as the third
6483 -- and fourth parameters.
6485 if Op_Name /= Name_Storage_Size then
6486 Validate_Simple_Pool_Op_Formal
6487 (Op, Formal, E_In_Parameter,
6488 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
6489 Validate_Simple_Pool_Op_Formal
6490 (Op, Formal, E_In_Parameter,
6491 Stg_Cnt_Type, "Alignment", Is_OK);
6492 end if;
6494 -- If no mismatched formals have been found (Is_OK)
6495 -- and no excess formals are present, then this
6496 -- operation has been validated, so record it.
6498 if not Present (Formal) and then Is_OK then
6499 Found_Op := Op;
6500 end if;
6501 end if;
6503 Op := Homonym (Op);
6504 end loop;
6506 -- There must be a valid Allocate operation for the type,
6507 -- so issue an error if none was found.
6509 if Op_Name = Name_Allocate
6510 and then not Present (Found_Op)
6511 then
6512 Error_Msg_N ("missing % operation for simple " &
6513 "storage pool type", Pool_Type);
6515 elsif Present (Found_Op) then
6517 -- Simple pool operations can't be abstract
6519 if Is_Abstract_Subprogram (Found_Op) then
6520 Error_Msg_N
6521 ("simple storage pool operation must not be " &
6522 "abstract", Found_Op);
6523 end if;
6525 -- The Storage_Size operation must be a function with
6526 -- Storage_Count as its result type.
6528 if Op_Name = Name_Storage_Size then
6529 if Ekind (Found_Op) = E_Procedure then
6530 Error_Msg_N
6531 ("% operation must be a function", Found_Op);
6533 elsif Etype (Found_Op) /= Stg_Cnt_Type then
6534 Error_Msg_NE
6535 ("wrong result type for%, expected type&",
6536 Found_Op, Stg_Cnt_Type);
6537 end if;
6539 -- Allocate and Deallocate must be procedures
6541 elsif Ekind (Found_Op) = E_Function then
6542 Error_Msg_N
6543 ("% operation must be a procedure", Found_Op);
6544 end if;
6545 end if;
6546 end Validate_Simple_Pool_Operation;
6548 -- Start of processing for Validate_Simple_Pool_Ops
6550 begin
6551 Validate_Simple_Pool_Operation (Name_Allocate);
6552 Validate_Simple_Pool_Operation (Name_Deallocate);
6553 Validate_Simple_Pool_Operation (Name_Storage_Size);
6554 end Validate_Simple_Pool_Ops;
6555 end if;
6556 end if;
6558 -- Now that all types from which E may depend are frozen, see if the
6559 -- size is known at compile time, if it must be unsigned, or if
6560 -- strict alignment is required
6562 Check_Compile_Time_Size (E);
6563 Check_Unsigned_Type (E);
6565 if Base_Type (E) = E then
6566 Check_Strict_Alignment (E);
6567 end if;
6569 -- Do not allow a size clause for a type which does not have a size
6570 -- that is known at compile time
6572 if Has_Size_Clause (E)
6573 and then not Size_Known_At_Compile_Time (E)
6574 then
6575 -- Suppress this message if errors posted on E, even if we are
6576 -- in all errors mode, since this is often a junk message
6578 if not Error_Posted (E) then
6579 Error_Msg_N
6580 ("size clause not allowed for variable length type",
6581 Size_Clause (E));
6582 end if;
6583 end if;
6585 -- Now we set/verify the representation information, in particular
6586 -- the size and alignment values. This processing is not required for
6587 -- generic types, since generic types do not play any part in code
6588 -- generation, and so the size and alignment values for such types
6589 -- are irrelevant. Ditto for types declared within a generic unit,
6590 -- which may have components that depend on generic parameters, and
6591 -- that will be recreated in an instance.
6593 if Inside_A_Generic then
6594 null;
6596 -- Otherwise we call the layout procedure
6598 else
6599 Layout_Type (E);
6600 end if;
6602 -- If this is an access to subprogram whose designated type is itself
6603 -- a subprogram type, the return type of this anonymous subprogram
6604 -- type must be decorated as well.
6606 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
6607 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
6608 then
6609 Layout_Type (Etype (Designated_Type (E)));
6610 end if;
6612 -- If the type has a Defaut_Value/Default_Component_Value aspect,
6613 -- this is where we analye the expression (after the type is frozen,
6614 -- since in the case of Default_Value, we are analyzing with the
6615 -- type itself, and we treat Default_Component_Value similarly for
6616 -- the sake of uniformity).
6618 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
6619 declare
6620 Nam : Name_Id;
6621 Exp : Node_Id;
6622 Typ : Entity_Id;
6624 begin
6625 if Is_Scalar_Type (E) then
6626 Nam := Name_Default_Value;
6627 Typ := E;
6628 Exp := Default_Aspect_Value (Typ);
6629 else
6630 Nam := Name_Default_Component_Value;
6631 Typ := Component_Type (E);
6632 Exp := Default_Aspect_Component_Value (E);
6633 end if;
6635 Analyze_And_Resolve (Exp, Typ);
6637 if Etype (Exp) /= Any_Type then
6638 if not Is_OK_Static_Expression (Exp) then
6639 Error_Msg_Name_1 := Nam;
6640 Flag_Non_Static_Expr
6641 ("aspect% requires static expression", Exp);
6642 end if;
6643 end if;
6644 end;
6645 end if;
6647 -- End of freeze processing for type entities
6648 end if;
6650 -- Here is where we logically freeze the current entity. If it has a
6651 -- freeze node, then this is the point at which the freeze node is
6652 -- linked into the result list.
6654 if Has_Delayed_Freeze (E) then
6656 -- If a freeze node is already allocated, use it, otherwise allocate
6657 -- a new one. The preallocation happens in the case of anonymous base
6658 -- types, where we preallocate so that we can set First_Subtype_Link.
6659 -- Note that we reset the Sloc to the current freeze location.
6661 if Present (Freeze_Node (E)) then
6662 F_Node := Freeze_Node (E);
6663 Set_Sloc (F_Node, Loc);
6665 else
6666 F_Node := New_Node (N_Freeze_Entity, Loc);
6667 Set_Freeze_Node (E, F_Node);
6668 Set_Access_Types_To_Process (F_Node, No_Elist);
6669 Set_TSS_Elist (F_Node, No_Elist);
6670 Set_Actions (F_Node, No_List);
6671 end if;
6673 Set_Entity (F_Node, E);
6674 Add_To_Result (F_Node);
6676 -- A final pass over record types with discriminants. If the type
6677 -- has an incomplete declaration, there may be constrained access
6678 -- subtypes declared elsewhere, which do not depend on the discrimi-
6679 -- nants of the type, and which are used as component types (i.e.
6680 -- the full view is a recursive type). The designated types of these
6681 -- subtypes can only be elaborated after the type itself, and they
6682 -- need an itype reference.
6684 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
6685 declare
6686 Comp : Entity_Id;
6687 IR : Node_Id;
6688 Typ : Entity_Id;
6690 begin
6691 Comp := First_Component (E);
6692 while Present (Comp) loop
6693 Typ := Etype (Comp);
6695 if Ekind (Comp) = E_Component
6696 and then Is_Access_Type (Typ)
6697 and then Scope (Typ) /= E
6698 and then Base_Type (Designated_Type (Typ)) = E
6699 and then Is_Itype (Designated_Type (Typ))
6700 then
6701 IR := Make_Itype_Reference (Sloc (Comp));
6702 Set_Itype (IR, Designated_Type (Typ));
6703 Append (IR, Result);
6704 end if;
6706 Next_Component (Comp);
6707 end loop;
6708 end;
6709 end if;
6710 end if;
6712 -- When a type is frozen, the first subtype of the type is frozen as
6713 -- well (RM 13.14(15)). This has to be done after freezing the type,
6714 -- since obviously the first subtype depends on its own base type.
6716 if Is_Type (E) then
6717 Freeze_And_Append (First_Subtype (E), N, Result);
6719 -- If we just froze a tagged non-class wide record, then freeze the
6720 -- corresponding class-wide type. This must be done after the tagged
6721 -- type itself is frozen, because the class-wide type refers to the
6722 -- tagged type which generates the class.
6724 if Is_Tagged_Type (E)
6725 and then not Is_Class_Wide_Type (E)
6726 and then Present (Class_Wide_Type (E))
6727 then
6728 Freeze_And_Append (Class_Wide_Type (E), N, Result);
6729 end if;
6730 end if;
6732 Check_Debug_Info_Needed (E);
6734 -- Special handling for subprograms
6736 if Is_Subprogram (E) then
6738 -- If subprogram has address clause then reset Is_Public flag, since
6739 -- we do not want the backend to generate external references.
6741 if Present (Address_Clause (E))
6742 and then not Is_Library_Level_Entity (E)
6743 then
6744 Set_Is_Public (E, False);
6745 end if;
6746 end if;
6748 <<Leave>>
6749 Restore_Ghost_Mode (Saved_GM);
6751 return Result;
6752 end Freeze_Entity;
6754 -----------------------------
6755 -- Freeze_Enumeration_Type --
6756 -----------------------------
6758 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6759 begin
6760 -- By default, if no size clause is present, an enumeration type with
6761 -- Convention C is assumed to interface to a C enum, and has integer
6762 -- size. This applies to types. For subtypes, verify that its base
6763 -- type has no size clause either. Treat other foreign conventions
6764 -- in the same way, and also make sure alignment is set right.
6766 if Has_Foreign_Convention (Typ)
6767 and then not Has_Size_Clause (Typ)
6768 and then not Has_Size_Clause (Base_Type (Typ))
6769 and then Esize (Typ) < Standard_Integer_Size
6771 -- Don't do this if Short_Enums on target
6773 and then not Target_Short_Enums
6774 then
6775 Init_Esize (Typ, Standard_Integer_Size);
6776 Set_Alignment (Typ, Alignment (Standard_Integer));
6778 -- Normal Ada case or size clause present or not Long_C_Enums on target
6780 else
6781 -- If the enumeration type interfaces to C, and it has a size clause
6782 -- that specifies less than int size, it warrants a warning. The
6783 -- user may intend the C type to be an enum or a char, so this is
6784 -- not by itself an error that the Ada compiler can detect, but it
6785 -- it is a worth a heads-up. For Boolean and Character types we
6786 -- assume that the programmer has the proper C type in mind.
6788 if Convention (Typ) = Convention_C
6789 and then Has_Size_Clause (Typ)
6790 and then Esize (Typ) /= Esize (Standard_Integer)
6791 and then not Is_Boolean_Type (Typ)
6792 and then not Is_Character_Type (Typ)
6794 -- Don't do this if Short_Enums on target
6796 and then not Target_Short_Enums
6797 then
6798 Error_Msg_N
6799 ("C enum types have the size of a C int??", Size_Clause (Typ));
6800 end if;
6802 Adjust_Esize_For_Alignment (Typ);
6803 end if;
6804 end Freeze_Enumeration_Type;
6806 -----------------------
6807 -- Freeze_Expression --
6808 -----------------------
6810 procedure Freeze_Expression (N : Node_Id) is
6811 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6812 Typ : Entity_Id;
6813 Nam : Entity_Id;
6814 Desig_Typ : Entity_Id;
6815 P : Node_Id;
6816 Parent_P : Node_Id;
6818 Freeze_Outside : Boolean := False;
6819 -- This flag is set true if the entity must be frozen outside the
6820 -- current subprogram. This happens in the case of expander generated
6821 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6822 -- not freeze all entities like other bodies, but which nevertheless
6823 -- may reference entities that have to be frozen before the body and
6824 -- obviously cannot be frozen inside the body.
6826 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6827 -- If the expression is an array aggregate, the type of the component
6828 -- expressions is also frozen. If the component type is an access type
6829 -- and the expressions include allocators, the designed type is frozen
6830 -- as well.
6832 function In_Expanded_Body (N : Node_Id) return Boolean;
6833 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6834 -- it is the handled statement sequence of an expander-generated
6835 -- subprogram (init proc, stream subprogram, or renaming as body).
6836 -- If so, this is not a freezing context.
6838 -----------------------------------------
6839 -- Find_Aggregate_Component_Desig_Type --
6840 -----------------------------------------
6842 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6843 Assoc : Node_Id;
6844 Exp : Node_Id;
6846 begin
6847 if Present (Expressions (N)) then
6848 Exp := First (Expressions (N));
6849 while Present (Exp) loop
6850 if Nkind (Exp) = N_Allocator then
6851 return Designated_Type (Component_Type (Etype (N)));
6852 end if;
6854 Next (Exp);
6855 end loop;
6856 end if;
6858 if Present (Component_Associations (N)) then
6859 Assoc := First (Component_Associations (N));
6860 while Present (Assoc) loop
6861 if Nkind (Expression (Assoc)) = N_Allocator then
6862 return Designated_Type (Component_Type (Etype (N)));
6863 end if;
6865 Next (Assoc);
6866 end loop;
6867 end if;
6869 return Empty;
6870 end Find_Aggregate_Component_Desig_Type;
6872 ----------------------
6873 -- In_Expanded_Body --
6874 ----------------------
6876 function In_Expanded_Body (N : Node_Id) return Boolean is
6877 P : Node_Id;
6878 Id : Entity_Id;
6880 begin
6881 if Nkind (N) = N_Subprogram_Body then
6882 P := N;
6883 else
6884 P := Parent (N);
6885 end if;
6887 if Nkind (P) /= N_Subprogram_Body then
6888 return False;
6890 else
6891 Id := Defining_Unit_Name (Specification (P));
6893 -- The following are expander-created bodies, or bodies that
6894 -- are not freeze points.
6896 if Nkind (Id) = N_Defining_Identifier
6897 and then (Is_Init_Proc (Id)
6898 or else Is_TSS (Id, TSS_Stream_Input)
6899 or else Is_TSS (Id, TSS_Stream_Output)
6900 or else Is_TSS (Id, TSS_Stream_Read)
6901 or else Is_TSS (Id, TSS_Stream_Write)
6902 or else Nkind_In (Original_Node (P),
6903 N_Subprogram_Renaming_Declaration,
6904 N_Expression_Function))
6905 then
6906 return True;
6907 else
6908 return False;
6909 end if;
6910 end if;
6911 end In_Expanded_Body;
6913 -- Start of processing for Freeze_Expression
6915 begin
6916 -- Immediate return if freezing is inhibited. This flag is set by the
6917 -- analyzer to stop freezing on generated expressions that would cause
6918 -- freezing if they were in the source program, but which are not
6919 -- supposed to freeze, since they are created.
6921 if Must_Not_Freeze (N) then
6922 return;
6923 end if;
6925 -- If expression is non-static, then it does not freeze in a default
6926 -- expression, see section "Handling of Default Expressions" in the
6927 -- spec of package Sem for further details. Note that we have to make
6928 -- sure that we actually have a real expression (if we have a subtype
6929 -- indication, we can't test Is_OK_Static_Expression). However, we
6930 -- exclude the case of the prefix of an attribute of a static scalar
6931 -- subtype from this early return, because static subtype attributes
6932 -- should always cause freezing, even in default expressions, but
6933 -- the attribute may not have been marked as static yet (because in
6934 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
6935 -- Freeze_Expression on the prefix).
6937 if In_Spec_Exp
6938 and then Nkind (N) in N_Subexpr
6939 and then not Is_OK_Static_Expression (N)
6940 and then (Nkind (Parent (N)) /= N_Attribute_Reference
6941 or else not (Is_Entity_Name (N)
6942 and then Is_Type (Entity (N))
6943 and then Is_OK_Static_Subtype (Entity (N))))
6944 then
6945 return;
6946 end if;
6948 -- Freeze type of expression if not frozen already
6950 Typ := Empty;
6952 if Nkind (N) in N_Has_Etype then
6953 if not Is_Frozen (Etype (N)) then
6954 Typ := Etype (N);
6956 -- Base type may be an derived numeric type that is frozen at
6957 -- the point of declaration, but first_subtype is still unfrozen.
6959 elsif not Is_Frozen (First_Subtype (Etype (N))) then
6960 Typ := First_Subtype (Etype (N));
6961 end if;
6962 end if;
6964 -- For entity name, freeze entity if not frozen already. A special
6965 -- exception occurs for an identifier that did not come from source.
6966 -- We don't let such identifiers freeze a non-internal entity, i.e.
6967 -- an entity that did come from source, since such an identifier was
6968 -- generated by the expander, and cannot have any semantic effect on
6969 -- the freezing semantics. For example, this stops the parameter of
6970 -- an initialization procedure from freezing the variable.
6972 if Is_Entity_Name (N)
6973 and then not Is_Frozen (Entity (N))
6974 and then (Nkind (N) /= N_Identifier
6975 or else Comes_From_Source (N)
6976 or else not Comes_From_Source (Entity (N)))
6977 then
6978 Nam := Entity (N);
6980 if Present (Nam) and then Ekind (Nam) = E_Function then
6981 Check_Expression_Function (N, Nam);
6982 end if;
6984 else
6985 Nam := Empty;
6986 end if;
6988 -- For an allocator freeze designated type if not frozen already
6990 -- For an aggregate whose component type is an access type, freeze the
6991 -- designated type now, so that its freeze does not appear within the
6992 -- loop that might be created in the expansion of the aggregate. If the
6993 -- designated type is a private type without full view, the expression
6994 -- cannot contain an allocator, so the type is not frozen.
6996 -- For a function, we freeze the entity when the subprogram declaration
6997 -- is frozen, but a function call may appear in an initialization proc.
6998 -- before the declaration is frozen. We need to generate the extra
6999 -- formals, if any, to ensure that the expansion of the call includes
7000 -- the proper actuals. This only applies to Ada subprograms, not to
7001 -- imported ones.
7003 Desig_Typ := Empty;
7005 case Nkind (N) is
7006 when N_Allocator =>
7007 Desig_Typ := Designated_Type (Etype (N));
7009 when N_Aggregate =>
7010 if Is_Array_Type (Etype (N))
7011 and then Is_Access_Type (Component_Type (Etype (N)))
7012 then
7014 -- Check whether aggregate includes allocators.
7016 Desig_Typ := Find_Aggregate_Component_Desig_Type;
7017 end if;
7019 when N_Indexed_Component
7020 | N_Selected_Component
7021 | N_Slice
7023 if Is_Access_Type (Etype (Prefix (N))) then
7024 Desig_Typ := Designated_Type (Etype (Prefix (N)));
7025 end if;
7027 when N_Identifier =>
7028 if Present (Nam)
7029 and then Ekind (Nam) = E_Function
7030 and then Nkind (Parent (N)) = N_Function_Call
7031 and then Convention (Nam) = Convention_Ada
7032 then
7033 Create_Extra_Formals (Nam);
7034 end if;
7036 when others =>
7037 null;
7038 end case;
7040 if Desig_Typ /= Empty
7041 and then (Is_Frozen (Desig_Typ)
7042 or else (not Is_Fully_Defined (Desig_Typ)))
7043 then
7044 Desig_Typ := Empty;
7045 end if;
7047 -- All done if nothing needs freezing
7049 if No (Typ)
7050 and then No (Nam)
7051 and then No (Desig_Typ)
7052 then
7053 return;
7054 end if;
7056 -- Examine the enclosing context by climbing the parent chain. The
7057 -- traversal serves two purposes - to detect scenarios where freezeing
7058 -- is not needed and to find the proper insertion point for the freeze
7059 -- nodes. Although somewhat similar to Insert_Actions, this traversal
7060 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
7061 -- the tree may result in types being frozen too early.
7063 P := N;
7064 loop
7065 Parent_P := Parent (P);
7067 -- If we don't have a parent, then we are not in a well-formed tree.
7068 -- This is an unusual case, but there are some legitimate situations
7069 -- in which this occurs, notably when the expressions in the range of
7070 -- a type declaration are resolved. We simply ignore the freeze
7071 -- request in this case. Is this right ???
7073 if No (Parent_P) then
7074 return;
7075 end if;
7077 -- See if we have got to an appropriate point in the tree
7079 case Nkind (Parent_P) is
7081 -- A special test for the exception of (RM 13.14(8)) for the case
7082 -- of per-object expressions (RM 3.8(18)) occurring in component
7083 -- definition or a discrete subtype definition. Note that we test
7084 -- for a component declaration which includes both cases we are
7085 -- interested in, and furthermore the tree does not have explicit
7086 -- nodes for either of these two constructs.
7088 when N_Component_Declaration =>
7090 -- The case we want to test for here is an identifier that is
7091 -- a per-object expression, this is either a discriminant that
7092 -- appears in a context other than the component declaration
7093 -- or it is a reference to the type of the enclosing construct.
7095 -- For either of these cases, we skip the freezing
7097 if not In_Spec_Expression
7098 and then Nkind (N) = N_Identifier
7099 and then (Present (Entity (N)))
7100 then
7101 -- We recognize the discriminant case by just looking for
7102 -- a reference to a discriminant. It can only be one for
7103 -- the enclosing construct. Skip freezing in this case.
7105 if Ekind (Entity (N)) = E_Discriminant then
7106 return;
7108 -- For the case of a reference to the enclosing record,
7109 -- (or task or protected type), we look for a type that
7110 -- matches the current scope.
7112 elsif Entity (N) = Current_Scope then
7113 return;
7114 end if;
7115 end if;
7117 -- If we have an enumeration literal that appears as the choice in
7118 -- the aggregate of an enumeration representation clause, then
7119 -- freezing does not occur (RM 13.14(10)).
7121 when N_Enumeration_Representation_Clause =>
7123 -- The case we are looking for is an enumeration literal
7125 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
7126 and then Is_Enumeration_Type (Etype (N))
7127 then
7128 -- If enumeration literal appears directly as the choice,
7129 -- do not freeze (this is the normal non-overloaded case)
7131 if Nkind (Parent (N)) = N_Component_Association
7132 and then First (Choices (Parent (N))) = N
7133 then
7134 return;
7136 -- If enumeration literal appears as the name of function
7137 -- which is the choice, then also do not freeze. This
7138 -- happens in the overloaded literal case, where the
7139 -- enumeration literal is temporarily changed to a function
7140 -- call for overloading analysis purposes.
7142 elsif Nkind (Parent (N)) = N_Function_Call
7143 and then
7144 Nkind (Parent (Parent (N))) = N_Component_Association
7145 and then
7146 First (Choices (Parent (Parent (N)))) = Parent (N)
7147 then
7148 return;
7149 end if;
7150 end if;
7152 -- Normally if the parent is a handled sequence of statements,
7153 -- then the current node must be a statement, and that is an
7154 -- appropriate place to insert a freeze node.
7156 when N_Handled_Sequence_Of_Statements =>
7158 -- An exception occurs when the sequence of statements is for
7159 -- an expander generated body that did not do the usual freeze
7160 -- all operation. In this case we usually want to freeze
7161 -- outside this body, not inside it, and we skip past the
7162 -- subprogram body that we are inside.
7164 if In_Expanded_Body (Parent_P) then
7165 declare
7166 Subp : constant Node_Id := Parent (Parent_P);
7167 Spec : Entity_Id;
7169 begin
7170 -- Freeze the entity only when it is declared inside the
7171 -- body of the expander generated procedure. This case
7172 -- is recognized by the scope of the entity or its type,
7173 -- which is either the spec for some enclosing body, or
7174 -- (in the case of init_procs, for which there are no
7175 -- separate specs) the current scope.
7177 if Nkind (Subp) = N_Subprogram_Body then
7178 Spec := Corresponding_Spec (Subp);
7180 if (Present (Typ) and then Scope (Typ) = Spec)
7181 or else
7182 (Present (Nam) and then Scope (Nam) = Spec)
7183 then
7184 exit;
7186 elsif Present (Typ)
7187 and then Scope (Typ) = Current_Scope
7188 and then Defining_Entity (Subp) = Current_Scope
7189 then
7190 exit;
7191 end if;
7192 end if;
7194 -- An expression function may act as a completion of
7195 -- a function declaration. As such, it can reference
7196 -- entities declared between the two views:
7198 -- Hidden []; -- 1
7199 -- function F return ...;
7200 -- private
7201 -- function Hidden return ...;
7202 -- function F return ... is (Hidden); -- 2
7204 -- Refering to the example above, freezing the expression
7205 -- of F (2) would place Hidden's freeze node (1) in the
7206 -- wrong place. Avoid explicit freezing and let the usual
7207 -- scenarios do the job - for example, reaching the end
7208 -- of the private declarations, or a call to F.
7210 if Nkind (Original_Node (Subp)) =
7211 N_Expression_Function
7212 then
7213 null;
7215 -- Freeze outside the body
7217 else
7218 Parent_P := Parent (Parent_P);
7219 Freeze_Outside := True;
7220 end if;
7221 end;
7223 -- Here if normal case where we are in handled statement
7224 -- sequence and want to do the insertion right there.
7226 else
7227 exit;
7228 end if;
7230 -- If parent is a body or a spec or a block, then the current node
7231 -- is a statement or declaration and we can insert the freeze node
7232 -- before it.
7234 when N_Block_Statement
7235 | N_Entry_Body
7236 | N_Package_Body
7237 | N_Package_Specification
7238 | N_Protected_Body
7239 | N_Subprogram_Body
7240 | N_Task_Body
7242 exit;
7244 -- The expander is allowed to define types in any statements list,
7245 -- so any of the following parent nodes also mark a freezing point
7246 -- if the actual node is in a list of statements or declarations.
7248 when N_Abortable_Part
7249 | N_Accept_Alternative
7250 | N_And_Then
7251 | N_Case_Statement_Alternative
7252 | N_Compilation_Unit_Aux
7253 | N_Conditional_Entry_Call
7254 | N_Delay_Alternative
7255 | N_Elsif_Part
7256 | N_Entry_Call_Alternative
7257 | N_Exception_Handler
7258 | N_Extended_Return_Statement
7259 | N_Freeze_Entity
7260 | N_If_Statement
7261 | N_Or_Else
7262 | N_Selective_Accept
7263 | N_Triggering_Alternative
7265 exit when Is_List_Member (P);
7267 -- Freeze nodes produced by an expression coming from the Actions
7268 -- list of a N_Expression_With_Actions node must remain within the
7269 -- Actions list. Inserting the freeze nodes further up the tree
7270 -- may lead to use before declaration issues in the case of array
7271 -- types.
7273 when N_Expression_With_Actions =>
7274 if Is_List_Member (P)
7275 and then List_Containing (P) = Actions (Parent_P)
7276 then
7277 exit;
7278 end if;
7280 -- Note: N_Loop_Statement is a special case. A type that appears
7281 -- in the source can never be frozen in a loop (this occurs only
7282 -- because of a loop expanded by the expander), so we keep on
7283 -- going. Otherwise we terminate the search. Same is true of any
7284 -- entity which comes from source. (if they have predefined type,
7285 -- that type does not appear to come from source, but the entity
7286 -- should not be frozen here).
7288 when N_Loop_Statement =>
7289 exit when not Comes_From_Source (Etype (N))
7290 and then (No (Nam) or else not Comes_From_Source (Nam));
7292 -- For all other cases, keep looking at parents
7294 when others =>
7295 null;
7296 end case;
7298 -- We fall through the case if we did not yet find the proper
7299 -- place in the free for inserting the freeze node, so climb.
7301 P := Parent_P;
7302 end loop;
7304 -- If the expression appears in a record or an initialization procedure,
7305 -- the freeze nodes are collected and attached to the current scope, to
7306 -- be inserted and analyzed on exit from the scope, to insure that
7307 -- generated entities appear in the correct scope. If the expression is
7308 -- a default for a discriminant specification, the scope is still void.
7309 -- The expression can also appear in the discriminant part of a private
7310 -- or concurrent type.
7312 -- If the expression appears in a constrained subcomponent of an
7313 -- enclosing record declaration, the freeze nodes must be attached to
7314 -- the outer record type so they can eventually be placed in the
7315 -- enclosing declaration list.
7317 -- The other case requiring this special handling is if we are in a
7318 -- default expression, since in that case we are about to freeze a
7319 -- static type, and the freeze scope needs to be the outer scope, not
7320 -- the scope of the subprogram with the default parameter.
7322 -- For default expressions and other spec expressions in generic units,
7323 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
7324 -- placing them at the proper place, after the generic unit.
7326 if (In_Spec_Exp and not Inside_A_Generic)
7327 or else Freeze_Outside
7328 or else (Is_Type (Current_Scope)
7329 and then (not Is_Concurrent_Type (Current_Scope)
7330 or else not Has_Completion (Current_Scope)))
7331 or else Ekind (Current_Scope) = E_Void
7332 then
7333 declare
7334 N : constant Node_Id := Current_Scope;
7335 Freeze_Nodes : List_Id := No_List;
7336 Pos : Int := Scope_Stack.Last;
7338 begin
7339 if Present (Desig_Typ) then
7340 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
7341 end if;
7343 if Present (Typ) then
7344 Freeze_And_Append (Typ, N, Freeze_Nodes);
7345 end if;
7347 if Present (Nam) then
7348 Freeze_And_Append (Nam, N, Freeze_Nodes);
7349 end if;
7351 -- The current scope may be that of a constrained component of
7352 -- an enclosing record declaration, or of a loop of an enclosing
7353 -- quantified expression, which is above the current scope in the
7354 -- scope stack. Indeed in the context of a quantified expression,
7355 -- a scope is created and pushed above the current scope in order
7356 -- to emulate the loop-like behavior of the quantified expression.
7357 -- If the expression is within a top-level pragma, as for a pre-
7358 -- condition on a library-level subprogram, nothing to do.
7360 if not Is_Compilation_Unit (Current_Scope)
7361 and then (Is_Record_Type (Scope (Current_Scope))
7362 or else Nkind (Parent (Current_Scope)) =
7363 N_Quantified_Expression)
7364 then
7365 Pos := Pos - 1;
7366 end if;
7368 if Is_Non_Empty_List (Freeze_Nodes) then
7369 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
7370 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
7371 Freeze_Nodes;
7372 else
7373 Append_List (Freeze_Nodes,
7374 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
7375 end if;
7376 end if;
7377 end;
7379 return;
7380 end if;
7382 -- Now we have the right place to do the freezing. First, a special
7383 -- adjustment, if we are in spec-expression analysis mode, these freeze
7384 -- actions must not be thrown away (normally all inserted actions are
7385 -- thrown away in this mode. However, the freeze actions are from static
7386 -- expressions and one of the important reasons we are doing this
7387 -- special analysis is to get these freeze actions. Therefore we turn
7388 -- off the In_Spec_Expression mode to propagate these freeze actions.
7389 -- This also means they get properly analyzed and expanded.
7391 In_Spec_Expression := False;
7393 -- Freeze the designated type of an allocator (RM 13.14(13))
7395 if Present (Desig_Typ) then
7396 Freeze_Before (P, Desig_Typ);
7397 end if;
7399 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
7400 -- the enumeration representation clause exception in the loop above.
7402 if Present (Typ) then
7403 Freeze_Before (P, Typ);
7404 end if;
7406 -- Freeze name if one is present (RM 13.14(11))
7408 if Present (Nam) then
7409 Freeze_Before (P, Nam);
7410 end if;
7412 -- Restore In_Spec_Expression flag
7414 In_Spec_Expression := In_Spec_Exp;
7415 end Freeze_Expression;
7417 -----------------------------
7418 -- Freeze_Fixed_Point_Type --
7419 -----------------------------
7421 -- Certain fixed-point types and subtypes, including implicit base types
7422 -- and declared first subtypes, have not yet set up a range. This is
7423 -- because the range cannot be set until the Small and Size values are
7424 -- known, and these are not known till the type is frozen.
7426 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
7427 -- whose bounds are unanalyzed real literals. This routine will recognize
7428 -- this case, and transform this range node into a properly typed range
7429 -- with properly analyzed and resolved values.
7431 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
7432 Rng : constant Node_Id := Scalar_Range (Typ);
7433 Lo : constant Node_Id := Low_Bound (Rng);
7434 Hi : constant Node_Id := High_Bound (Rng);
7435 Btyp : constant Entity_Id := Base_Type (Typ);
7436 Brng : constant Node_Id := Scalar_Range (Btyp);
7437 BLo : constant Node_Id := Low_Bound (Brng);
7438 BHi : constant Node_Id := High_Bound (Brng);
7439 Small : constant Ureal := Small_Value (Typ);
7440 Loval : Ureal;
7441 Hival : Ureal;
7442 Atype : Entity_Id;
7444 Orig_Lo : Ureal;
7445 Orig_Hi : Ureal;
7446 -- Save original bounds (for shaving tests)
7448 Actual_Size : Nat;
7449 -- Actual size chosen
7451 function Fsize (Lov, Hiv : Ureal) return Nat;
7452 -- Returns size of type with given bounds. Also leaves these
7453 -- bounds set as the current bounds of the Typ.
7455 -----------
7456 -- Fsize --
7457 -----------
7459 function Fsize (Lov, Hiv : Ureal) return Nat is
7460 begin
7461 Set_Realval (Lo, Lov);
7462 Set_Realval (Hi, Hiv);
7463 return Minimum_Size (Typ);
7464 end Fsize;
7466 -- Start of processing for Freeze_Fixed_Point_Type
7468 begin
7469 -- If Esize of a subtype has not previously been set, set it now
7471 if Unknown_Esize (Typ) then
7472 Atype := Ancestor_Subtype (Typ);
7474 if Present (Atype) then
7475 Set_Esize (Typ, Esize (Atype));
7476 else
7477 Set_Esize (Typ, Esize (Base_Type (Typ)));
7478 end if;
7479 end if;
7481 -- Immediate return if the range is already analyzed. This means that
7482 -- the range is already set, and does not need to be computed by this
7483 -- routine.
7485 if Analyzed (Rng) then
7486 return;
7487 end if;
7489 -- Immediate return if either of the bounds raises Constraint_Error
7491 if Raises_Constraint_Error (Lo)
7492 or else Raises_Constraint_Error (Hi)
7493 then
7494 return;
7495 end if;
7497 Loval := Realval (Lo);
7498 Hival := Realval (Hi);
7500 Orig_Lo := Loval;
7501 Orig_Hi := Hival;
7503 -- Ordinary fixed-point case
7505 if Is_Ordinary_Fixed_Point_Type (Typ) then
7507 -- For the ordinary fixed-point case, we are allowed to fudge the
7508 -- end-points up or down by small. Generally we prefer to fudge up,
7509 -- i.e. widen the bounds for non-model numbers so that the end points
7510 -- are included. However there are cases in which this cannot be
7511 -- done, and indeed cases in which we may need to narrow the bounds.
7512 -- The following circuit makes the decision.
7514 -- Note: our terminology here is that Incl_EP means that the bounds
7515 -- are widened by Small if necessary to include the end points, and
7516 -- Excl_EP means that the bounds are narrowed by Small to exclude the
7517 -- end-points if this reduces the size.
7519 -- Note that in the Incl case, all we care about is including the
7520 -- end-points. In the Excl case, we want to narrow the bounds as
7521 -- much as permitted by the RM, to give the smallest possible size.
7523 Fudge : declare
7524 Loval_Incl_EP : Ureal;
7525 Hival_Incl_EP : Ureal;
7527 Loval_Excl_EP : Ureal;
7528 Hival_Excl_EP : Ureal;
7530 Size_Incl_EP : Nat;
7531 Size_Excl_EP : Nat;
7533 Model_Num : Ureal;
7534 First_Subt : Entity_Id;
7535 Actual_Lo : Ureal;
7536 Actual_Hi : Ureal;
7538 begin
7539 -- First step. Base types are required to be symmetrical. Right
7540 -- now, the base type range is a copy of the first subtype range.
7541 -- This will be corrected before we are done, but right away we
7542 -- need to deal with the case where both bounds are non-negative.
7543 -- In this case, we set the low bound to the negative of the high
7544 -- bound, to make sure that the size is computed to include the
7545 -- required sign. Note that we do not need to worry about the
7546 -- case of both bounds negative, because the sign will be dealt
7547 -- with anyway. Furthermore we can't just go making such a bound
7548 -- symmetrical, since in a twos-complement system, there is an
7549 -- extra negative value which could not be accommodated on the
7550 -- positive side.
7552 if Typ = Btyp
7553 and then not UR_Is_Negative (Loval)
7554 and then Hival > Loval
7555 then
7556 Loval := -Hival;
7557 Set_Realval (Lo, Loval);
7558 end if;
7560 -- Compute the fudged bounds. If the number is a model number,
7561 -- then we do nothing to include it, but we are allowed to backoff
7562 -- to the next adjacent model number when we exclude it. If it is
7563 -- not a model number then we straddle the two values with the
7564 -- model numbers on either side.
7566 Model_Num := UR_Trunc (Loval / Small) * Small;
7568 if Loval = Model_Num then
7569 Loval_Incl_EP := Model_Num;
7570 else
7571 Loval_Incl_EP := Model_Num - Small;
7572 end if;
7574 -- The low value excluding the end point is Small greater, but
7575 -- we do not do this exclusion if the low value is positive,
7576 -- since it can't help the size and could actually hurt by
7577 -- crossing the high bound.
7579 if UR_Is_Negative (Loval_Incl_EP) then
7580 Loval_Excl_EP := Loval_Incl_EP + Small;
7582 -- If the value went from negative to zero, then we have the
7583 -- case where Loval_Incl_EP is the model number just below
7584 -- zero, so we want to stick to the negative value for the
7585 -- base type to maintain the condition that the size will
7586 -- include signed values.
7588 if Typ = Btyp
7589 and then UR_Is_Zero (Loval_Excl_EP)
7590 then
7591 Loval_Excl_EP := Loval_Incl_EP;
7592 end if;
7594 else
7595 Loval_Excl_EP := Loval_Incl_EP;
7596 end if;
7598 -- Similar processing for upper bound and high value
7600 Model_Num := UR_Trunc (Hival / Small) * Small;
7602 if Hival = Model_Num then
7603 Hival_Incl_EP := Model_Num;
7604 else
7605 Hival_Incl_EP := Model_Num + Small;
7606 end if;
7608 if UR_Is_Positive (Hival_Incl_EP) then
7609 Hival_Excl_EP := Hival_Incl_EP - Small;
7610 else
7611 Hival_Excl_EP := Hival_Incl_EP;
7612 end if;
7614 -- One further adjustment is needed. In the case of subtypes, we
7615 -- cannot go outside the range of the base type, or we get
7616 -- peculiarities, and the base type range is already set. This
7617 -- only applies to the Incl values, since clearly the Excl values
7618 -- are already as restricted as they are allowed to be.
7620 if Typ /= Btyp then
7621 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
7622 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
7623 end if;
7625 -- Get size including and excluding end points
7627 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
7628 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
7630 -- No need to exclude end-points if it does not reduce size
7632 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
7633 Loval_Excl_EP := Loval_Incl_EP;
7634 end if;
7636 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
7637 Hival_Excl_EP := Hival_Incl_EP;
7638 end if;
7640 -- Now we set the actual size to be used. We want to use the
7641 -- bounds fudged up to include the end-points but only if this
7642 -- can be done without violating a specifically given size
7643 -- size clause or causing an unacceptable increase in size.
7645 -- Case of size clause given
7647 if Has_Size_Clause (Typ) then
7649 -- Use the inclusive size only if it is consistent with
7650 -- the explicitly specified size.
7652 if Size_Incl_EP <= RM_Size (Typ) then
7653 Actual_Lo := Loval_Incl_EP;
7654 Actual_Hi := Hival_Incl_EP;
7655 Actual_Size := Size_Incl_EP;
7657 -- If the inclusive size is too large, we try excluding
7658 -- the end-points (will be caught later if does not work).
7660 else
7661 Actual_Lo := Loval_Excl_EP;
7662 Actual_Hi := Hival_Excl_EP;
7663 Actual_Size := Size_Excl_EP;
7664 end if;
7666 -- Case of size clause not given
7668 else
7669 -- If we have a base type whose corresponding first subtype
7670 -- has an explicit size that is large enough to include our
7671 -- end-points, then do so. There is no point in working hard
7672 -- to get a base type whose size is smaller than the specified
7673 -- size of the first subtype.
7675 First_Subt := First_Subtype (Typ);
7677 if Has_Size_Clause (First_Subt)
7678 and then Size_Incl_EP <= Esize (First_Subt)
7679 then
7680 Actual_Size := Size_Incl_EP;
7681 Actual_Lo := Loval_Incl_EP;
7682 Actual_Hi := Hival_Incl_EP;
7684 -- If excluding the end-points makes the size smaller and
7685 -- results in a size of 8,16,32,64, then we take the smaller
7686 -- size. For the 64 case, this is compulsory. For the other
7687 -- cases, it seems reasonable. We like to include end points
7688 -- if we can, but not at the expense of moving to the next
7689 -- natural boundary of size.
7691 elsif Size_Incl_EP /= Size_Excl_EP
7692 and then Addressable (Size_Excl_EP)
7693 then
7694 Actual_Size := Size_Excl_EP;
7695 Actual_Lo := Loval_Excl_EP;
7696 Actual_Hi := Hival_Excl_EP;
7698 -- Otherwise we can definitely include the end points
7700 else
7701 Actual_Size := Size_Incl_EP;
7702 Actual_Lo := Loval_Incl_EP;
7703 Actual_Hi := Hival_Incl_EP;
7704 end if;
7706 -- One pathological case: normally we never fudge a low bound
7707 -- down, since it would seem to increase the size (if it has
7708 -- any effect), but for ranges containing single value, or no
7709 -- values, the high bound can be small too large. Consider:
7711 -- type t is delta 2.0**(-14)
7712 -- range 131072.0 .. 0;
7714 -- That lower bound is *just* outside the range of 32 bits, and
7715 -- does need fudging down in this case. Note that the bounds
7716 -- will always have crossed here, since the high bound will be
7717 -- fudged down if necessary, as in the case of:
7719 -- type t is delta 2.0**(-14)
7720 -- range 131072.0 .. 131072.0;
7722 -- So we detect the situation by looking for crossed bounds,
7723 -- and if the bounds are crossed, and the low bound is greater
7724 -- than zero, we will always back it off by small, since this
7725 -- is completely harmless.
7727 if Actual_Lo > Actual_Hi then
7728 if UR_Is_Positive (Actual_Lo) then
7729 Actual_Lo := Loval_Incl_EP - Small;
7730 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7732 -- And of course, we need to do exactly the same parallel
7733 -- fudge for flat ranges in the negative region.
7735 elsif UR_Is_Negative (Actual_Hi) then
7736 Actual_Hi := Hival_Incl_EP + Small;
7737 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7738 end if;
7739 end if;
7740 end if;
7742 Set_Realval (Lo, Actual_Lo);
7743 Set_Realval (Hi, Actual_Hi);
7744 end Fudge;
7746 -- For the decimal case, none of this fudging is required, since there
7747 -- are no end-point problems in the decimal case (the end-points are
7748 -- always included).
7750 else
7751 Actual_Size := Fsize (Loval, Hival);
7752 end if;
7754 -- At this stage, the actual size has been calculated and the proper
7755 -- required bounds are stored in the low and high bounds.
7757 if Actual_Size > 64 then
7758 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7759 Error_Msg_N
7760 ("size required (^) for type& too large, maximum allowed is 64",
7761 Typ);
7762 Actual_Size := 64;
7763 end if;
7765 -- Check size against explicit given size
7767 if Has_Size_Clause (Typ) then
7768 if Actual_Size > RM_Size (Typ) then
7769 Error_Msg_Uint_1 := RM_Size (Typ);
7770 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7771 Error_Msg_NE
7772 ("size given (^) for type& too small, minimum allowed is ^",
7773 Size_Clause (Typ), Typ);
7775 else
7776 Actual_Size := UI_To_Int (Esize (Typ));
7777 end if;
7779 -- Increase size to next natural boundary if no size clause given
7781 else
7782 if Actual_Size <= 8 then
7783 Actual_Size := 8;
7784 elsif Actual_Size <= 16 then
7785 Actual_Size := 16;
7786 elsif Actual_Size <= 32 then
7787 Actual_Size := 32;
7788 else
7789 Actual_Size := 64;
7790 end if;
7792 Init_Esize (Typ, Actual_Size);
7793 Adjust_Esize_For_Alignment (Typ);
7794 end if;
7796 -- If we have a base type, then expand the bounds so that they extend to
7797 -- the full width of the allocated size in bits, to avoid junk range
7798 -- checks on intermediate computations.
7800 if Base_Type (Typ) = Typ then
7801 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7802 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7803 end if;
7805 -- Final step is to reanalyze the bounds using the proper type
7806 -- and set the Corresponding_Integer_Value fields of the literals.
7808 Set_Etype (Lo, Empty);
7809 Set_Analyzed (Lo, False);
7810 Analyze (Lo);
7812 -- Resolve with universal fixed if the base type, and the base type if
7813 -- it is a subtype. Note we can't resolve the base type with itself,
7814 -- that would be a reference before definition.
7816 if Typ = Btyp then
7817 Resolve (Lo, Universal_Fixed);
7818 else
7819 Resolve (Lo, Btyp);
7820 end if;
7822 -- Set corresponding integer value for bound
7824 Set_Corresponding_Integer_Value
7825 (Lo, UR_To_Uint (Realval (Lo) / Small));
7827 -- Similar processing for high bound
7829 Set_Etype (Hi, Empty);
7830 Set_Analyzed (Hi, False);
7831 Analyze (Hi);
7833 if Typ = Btyp then
7834 Resolve (Hi, Universal_Fixed);
7835 else
7836 Resolve (Hi, Btyp);
7837 end if;
7839 Set_Corresponding_Integer_Value
7840 (Hi, UR_To_Uint (Realval (Hi) / Small));
7842 -- Set type of range to correspond to bounds
7844 Set_Etype (Rng, Etype (Lo));
7846 -- Set Esize to calculated size if not set already
7848 if Unknown_Esize (Typ) then
7849 Init_Esize (Typ, Actual_Size);
7850 end if;
7852 -- Set RM_Size if not already set. If already set, check value
7854 declare
7855 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7857 begin
7858 if RM_Size (Typ) /= Uint_0 then
7859 if RM_Size (Typ) < Minsiz then
7860 Error_Msg_Uint_1 := RM_Size (Typ);
7861 Error_Msg_Uint_2 := Minsiz;
7862 Error_Msg_NE
7863 ("size given (^) for type& too small, minimum allowed is ^",
7864 Size_Clause (Typ), Typ);
7865 end if;
7867 else
7868 Set_RM_Size (Typ, Minsiz);
7869 end if;
7870 end;
7872 -- Check for shaving
7874 if Comes_From_Source (Typ) then
7876 -- In SPARK mode the given bounds must be strictly representable
7878 if SPARK_Mode = On then
7879 if Orig_Lo < Expr_Value_R (Lo) then
7880 Error_Msg_NE
7881 ("declared low bound of type & is outside type range",
7882 Lo, Typ);
7883 end if;
7885 if Orig_Hi > Expr_Value_R (Hi) then
7886 Error_Msg_NE
7887 ("declared high bound of type & is outside type range",
7888 Hi, Typ);
7889 end if;
7891 else
7892 if Orig_Lo < Expr_Value_R (Lo) then
7893 Error_Msg_N
7894 ("declared low bound of type & is outside type range??", Typ);
7895 Error_Msg_N
7896 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
7897 end if;
7899 if Orig_Hi > Expr_Value_R (Hi) then
7900 Error_Msg_N
7901 ("declared high bound of type & is outside type range??",
7902 Typ);
7903 Error_Msg_N
7904 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
7905 end if;
7906 end if;
7907 end if;
7908 end Freeze_Fixed_Point_Type;
7910 ------------------
7911 -- Freeze_Itype --
7912 ------------------
7914 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7915 L : List_Id;
7917 begin
7918 Set_Has_Delayed_Freeze (T);
7919 L := Freeze_Entity (T, N);
7921 if Is_Non_Empty_List (L) then
7922 Insert_Actions (N, L);
7923 end if;
7924 end Freeze_Itype;
7926 --------------------------
7927 -- Freeze_Static_Object --
7928 --------------------------
7930 procedure Freeze_Static_Object (E : Entity_Id) is
7932 Cannot_Be_Static : exception;
7933 -- Exception raised if the type of a static object cannot be made
7934 -- static. This happens if the type depends on non-global objects.
7936 procedure Ensure_Expression_Is_SA (N : Node_Id);
7937 -- Called to ensure that an expression used as part of a type definition
7938 -- is statically allocatable, which means that the expression type is
7939 -- statically allocatable, and the expression is either static, or a
7940 -- reference to a library level constant.
7942 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7943 -- Called to mark a type as static, checking that it is possible
7944 -- to set the type as static. If it is not possible, then the
7945 -- exception Cannot_Be_Static is raised.
7947 -----------------------------
7948 -- Ensure_Expression_Is_SA --
7949 -----------------------------
7951 procedure Ensure_Expression_Is_SA (N : Node_Id) is
7952 Ent : Entity_Id;
7954 begin
7955 Ensure_Type_Is_SA (Etype (N));
7957 if Is_OK_Static_Expression (N) then
7958 return;
7960 elsif Nkind (N) = N_Identifier then
7961 Ent := Entity (N);
7963 if Present (Ent)
7964 and then Ekind (Ent) = E_Constant
7965 and then Is_Library_Level_Entity (Ent)
7966 then
7967 return;
7968 end if;
7969 end if;
7971 raise Cannot_Be_Static;
7972 end Ensure_Expression_Is_SA;
7974 -----------------------
7975 -- Ensure_Type_Is_SA --
7976 -----------------------
7978 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7979 N : Node_Id;
7980 C : Entity_Id;
7982 begin
7983 -- If type is library level, we are all set
7985 if Is_Library_Level_Entity (Typ) then
7986 return;
7987 end if;
7989 -- We are also OK if the type already marked as statically allocated,
7990 -- which means we processed it before.
7992 if Is_Statically_Allocated (Typ) then
7993 return;
7994 end if;
7996 -- Mark type as statically allocated
7998 Set_Is_Statically_Allocated (Typ);
8000 -- Check that it is safe to statically allocate this type
8002 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
8003 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
8004 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
8006 elsif Is_Array_Type (Typ) then
8007 N := First_Index (Typ);
8008 while Present (N) loop
8009 Ensure_Type_Is_SA (Etype (N));
8010 Next_Index (N);
8011 end loop;
8013 Ensure_Type_Is_SA (Component_Type (Typ));
8015 elsif Is_Access_Type (Typ) then
8016 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
8018 declare
8019 F : Entity_Id;
8020 T : constant Entity_Id := Etype (Designated_Type (Typ));
8022 begin
8023 if T /= Standard_Void_Type then
8024 Ensure_Type_Is_SA (T);
8025 end if;
8027 F := First_Formal (Designated_Type (Typ));
8028 while Present (F) loop
8029 Ensure_Type_Is_SA (Etype (F));
8030 Next_Formal (F);
8031 end loop;
8032 end;
8034 else
8035 Ensure_Type_Is_SA (Designated_Type (Typ));
8036 end if;
8038 elsif Is_Record_Type (Typ) then
8039 C := First_Entity (Typ);
8040 while Present (C) loop
8041 if Ekind (C) = E_Discriminant
8042 or else Ekind (C) = E_Component
8043 then
8044 Ensure_Type_Is_SA (Etype (C));
8046 elsif Is_Type (C) then
8047 Ensure_Type_Is_SA (C);
8048 end if;
8050 Next_Entity (C);
8051 end loop;
8053 elsif Ekind (Typ) = E_Subprogram_Type then
8054 Ensure_Type_Is_SA (Etype (Typ));
8056 C := First_Formal (Typ);
8057 while Present (C) loop
8058 Ensure_Type_Is_SA (Etype (C));
8059 Next_Formal (C);
8060 end loop;
8062 else
8063 raise Cannot_Be_Static;
8064 end if;
8065 end Ensure_Type_Is_SA;
8067 -- Start of processing for Freeze_Static_Object
8069 begin
8070 Ensure_Type_Is_SA (Etype (E));
8072 exception
8073 when Cannot_Be_Static =>
8075 -- If the object that cannot be static is imported or exported, then
8076 -- issue an error message saying that this object cannot be imported
8077 -- or exported. If it has an address clause it is an overlay in the
8078 -- current partition and the static requirement is not relevant.
8079 -- Do not issue any error message when ignoring rep clauses.
8081 if Ignore_Rep_Clauses then
8082 null;
8084 elsif Is_Imported (E) then
8085 if No (Address_Clause (E)) then
8086 Error_Msg_N
8087 ("& cannot be imported (local type is not constant)", E);
8088 end if;
8090 -- Otherwise must be exported, something is wrong if compiler
8091 -- is marking something as statically allocated which cannot be).
8093 else pragma Assert (Is_Exported (E));
8094 Error_Msg_N
8095 ("& cannot be exported (local type is not constant)", E);
8096 end if;
8097 end Freeze_Static_Object;
8099 -----------------------
8100 -- Freeze_Subprogram --
8101 -----------------------
8103 procedure Freeze_Subprogram (E : Entity_Id) is
8104 procedure Set_Profile_Convention (Subp_Id : Entity_Id);
8105 -- Set the conventions of all anonymous access-to-subprogram formals and
8106 -- result subtype of subprogram Subp_Id to the convention of Subp_Id.
8108 ----------------------------
8109 -- Set_Profile_Convention --
8110 ----------------------------
8112 procedure Set_Profile_Convention (Subp_Id : Entity_Id) is
8113 Conv : constant Convention_Id := Convention (Subp_Id);
8115 procedure Set_Type_Convention (Typ : Entity_Id);
8116 -- Set the convention of anonymous access-to-subprogram type Typ and
8117 -- its designated type to Conv.
8119 -------------------------
8120 -- Set_Type_Convention --
8121 -------------------------
8123 procedure Set_Type_Convention (Typ : Entity_Id) is
8124 begin
8125 -- Set the convention on both the anonymous access-to-subprogram
8126 -- type and the subprogram type it points to because both types
8127 -- participate in conformance-related checks.
8129 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
8130 Set_Convention (Typ, Conv);
8131 Set_Convention (Designated_Type (Typ), Conv);
8132 end if;
8133 end Set_Type_Convention;
8135 -- Local variables
8137 Formal : Entity_Id;
8139 -- Start of processing for Set_Profile_Convention
8141 begin
8142 Formal := First_Formal (Subp_Id);
8143 while Present (Formal) loop
8144 Set_Type_Convention (Etype (Formal));
8145 Next_Formal (Formal);
8146 end loop;
8148 if Ekind (Subp_Id) = E_Function then
8149 Set_Type_Convention (Etype (Subp_Id));
8150 end if;
8151 end Set_Profile_Convention;
8153 -- Local variables
8155 F : Entity_Id;
8156 Retype : Entity_Id;
8158 -- Start of processing for Freeze_Subprogram
8160 begin
8161 -- Subprogram may not have an address clause unless it is imported
8163 if Present (Address_Clause (E)) then
8164 if not Is_Imported (E) then
8165 Error_Msg_N
8166 ("address clause can only be given for imported subprogram",
8167 Name (Address_Clause (E)));
8168 end if;
8169 end if;
8171 -- Reset the Pure indication on an imported subprogram unless an
8172 -- explicit Pure_Function pragma was present or the subprogram is an
8173 -- intrinsic. We do this because otherwise it is an insidious error
8174 -- to call a non-pure function from pure unit and have calls
8175 -- mysteriously optimized away. What happens here is that the Import
8176 -- can bypass the normal check to ensure that pure units call only pure
8177 -- subprograms.
8179 -- The reason for the intrinsic exception is that in general, intrinsic
8180 -- functions (such as shifts) are pure anyway. The only exceptions are
8181 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
8182 -- in any case, so no problem arises.
8184 if Is_Imported (E)
8185 and then Is_Pure (E)
8186 and then not Has_Pragma_Pure_Function (E)
8187 and then not Is_Intrinsic_Subprogram (E)
8188 then
8189 Set_Is_Pure (E, False);
8190 end if;
8192 -- We also reset the Pure indication on a subprogram with an Address
8193 -- parameter, because the parameter may be used as a pointer and the
8194 -- referenced data may change even if the address value does not.
8196 -- Note that if the programmer gave an explicit Pure_Function pragma,
8197 -- then we believe the programmer, and leave the subprogram Pure. We
8198 -- also suppress this check on run-time files.
8200 if Is_Pure (E)
8201 and then Is_Subprogram (E)
8202 and then not Has_Pragma_Pure_Function (E)
8203 and then not Is_Internal_Unit (Current_Sem_Unit)
8204 then
8205 Check_Function_With_Address_Parameter (E);
8206 end if;
8208 -- Ensure that all anonymous access-to-subprogram types inherit the
8209 -- convention of their related subprogram (RM 6.3.1 13.1/3). This is
8210 -- not done for a defaulted convention Ada because those types also
8211 -- default to Ada. Convention Protected must not be propagated when
8212 -- the subprogram is an entry because this would be illegal. The only
8213 -- way to force convention Protected on these kinds of types is to
8214 -- include keyword "protected" in the access definition.
8216 if Convention (E) /= Convention_Ada
8217 and then Convention (E) /= Convention_Protected
8218 then
8219 Set_Profile_Convention (E);
8220 end if;
8222 -- For non-foreign convention subprograms, this is where we create
8223 -- the extra formals (for accessibility level and constrained bit
8224 -- information). We delay this till the freeze point precisely so
8225 -- that we know the convention.
8227 if not Has_Foreign_Convention (E) then
8228 if No (Extra_Formals (E)) then
8229 Create_Extra_Formals (E);
8230 end if;
8232 Set_Mechanisms (E);
8234 -- If this is convention Ada and a Valued_Procedure, that's odd
8236 if Ekind (E) = E_Procedure
8237 and then Is_Valued_Procedure (E)
8238 and then Convention (E) = Convention_Ada
8239 and then Warn_On_Export_Import
8240 then
8241 Error_Msg_N
8242 ("??Valued_Procedure has no effect for convention Ada", E);
8243 Set_Is_Valued_Procedure (E, False);
8244 end if;
8246 -- Case of foreign convention
8248 else
8249 Set_Mechanisms (E);
8251 -- For foreign conventions, warn about return of unconstrained array
8253 if Ekind (E) = E_Function then
8254 Retype := Underlying_Type (Etype (E));
8256 -- If no return type, probably some other error, e.g. a
8257 -- missing full declaration, so ignore.
8259 if No (Retype) then
8260 null;
8262 -- If the return type is generic, we have emitted a warning
8263 -- earlier on, and there is nothing else to check here. Specific
8264 -- instantiations may lead to erroneous behavior.
8266 elsif Is_Generic_Type (Etype (E)) then
8267 null;
8269 -- Display warning if returning unconstrained array
8271 elsif Is_Array_Type (Retype)
8272 and then not Is_Constrained (Retype)
8274 -- Check appropriate warning is enabled (should we check for
8275 -- Warnings (Off) on specific entities here, probably so???)
8277 and then Warn_On_Export_Import
8278 then
8279 Error_Msg_N
8280 ("?x?foreign convention function& should not return " &
8281 "unconstrained array", E);
8282 return;
8283 end if;
8284 end if;
8286 -- If any of the formals for an exported foreign convention
8287 -- subprogram have defaults, then emit an appropriate warning since
8288 -- this is odd (default cannot be used from non-Ada code)
8290 if Is_Exported (E) then
8291 F := First_Formal (E);
8292 while Present (F) loop
8293 if Warn_On_Export_Import
8294 and then Present (Default_Value (F))
8295 then
8296 Error_Msg_N
8297 ("?x?parameter cannot be defaulted in non-Ada call",
8298 Default_Value (F));
8299 end if;
8301 Next_Formal (F);
8302 end loop;
8303 end if;
8304 end if;
8306 -- Pragma Inline_Always is disallowed for dispatching subprograms
8307 -- because the address of such subprograms is saved in the dispatch
8308 -- table to support dispatching calls, and dispatching calls cannot
8309 -- be inlined. This is consistent with the restriction against using
8310 -- 'Access or 'Address on an Inline_Always subprogram.
8312 if Is_Dispatching_Operation (E)
8313 and then Has_Pragma_Inline_Always (E)
8314 then
8315 Error_Msg_N
8316 ("pragma Inline_Always not allowed for dispatching subprograms", E);
8317 end if;
8319 -- Because of the implicit representation of inherited predefined
8320 -- operators in the front-end, the overriding status of the operation
8321 -- may be affected when a full view of a type is analyzed, and this is
8322 -- not captured by the analysis of the corresponding type declaration.
8323 -- Therefore the correctness of a not-overriding indicator must be
8324 -- rechecked when the subprogram is frozen.
8326 if Nkind (E) = N_Defining_Operator_Symbol
8327 and then not Error_Posted (Parent (E))
8328 then
8329 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
8330 end if;
8332 if Modify_Tree_For_C
8333 and then Nkind (Parent (E)) = N_Function_Specification
8334 and then Is_Array_Type (Etype (E))
8335 and then Is_Constrained (Etype (E))
8336 and then not Is_Unchecked_Conversion_Instance (E)
8337 and then not Rewritten_For_C (E)
8338 then
8339 Build_Procedure_Form (Unit_Declaration_Node (E));
8340 end if;
8341 end Freeze_Subprogram;
8343 ----------------------
8344 -- Is_Fully_Defined --
8345 ----------------------
8347 function Is_Fully_Defined (T : Entity_Id) return Boolean is
8348 begin
8349 if Ekind (T) = E_Class_Wide_Type then
8350 return Is_Fully_Defined (Etype (T));
8352 elsif Is_Array_Type (T) then
8353 return Is_Fully_Defined (Component_Type (T));
8355 elsif Is_Record_Type (T)
8356 and not Is_Private_Type (T)
8357 then
8358 -- Verify that the record type has no components with private types
8359 -- without completion.
8361 declare
8362 Comp : Entity_Id;
8364 begin
8365 Comp := First_Component (T);
8366 while Present (Comp) loop
8367 if not Is_Fully_Defined (Etype (Comp)) then
8368 return False;
8369 end if;
8371 Next_Component (Comp);
8372 end loop;
8373 return True;
8374 end;
8376 -- For the designated type of an access to subprogram, all types in
8377 -- the profile must be fully defined.
8379 elsif Ekind (T) = E_Subprogram_Type then
8380 declare
8381 F : Entity_Id;
8383 begin
8384 F := First_Formal (T);
8385 while Present (F) loop
8386 if not Is_Fully_Defined (Etype (F)) then
8387 return False;
8388 end if;
8390 Next_Formal (F);
8391 end loop;
8393 return Is_Fully_Defined (Etype (T));
8394 end;
8396 else
8397 return not Is_Private_Type (T)
8398 or else Present (Full_View (Base_Type (T)));
8399 end if;
8400 end Is_Fully_Defined;
8402 ---------------------------------
8403 -- Process_Default_Expressions --
8404 ---------------------------------
8406 procedure Process_Default_Expressions
8407 (E : Entity_Id;
8408 After : in out Node_Id)
8410 Loc : constant Source_Ptr := Sloc (E);
8411 Dbody : Node_Id;
8412 Formal : Node_Id;
8413 Dcopy : Node_Id;
8414 Dnam : Entity_Id;
8416 begin
8417 Set_Default_Expressions_Processed (E);
8419 -- A subprogram instance and its associated anonymous subprogram share
8420 -- their signature. The default expression functions are defined in the
8421 -- wrapper packages for the anonymous subprogram, and should not be
8422 -- generated again for the instance.
8424 if Is_Generic_Instance (E)
8425 and then Present (Alias (E))
8426 and then Default_Expressions_Processed (Alias (E))
8427 then
8428 return;
8429 end if;
8431 Formal := First_Formal (E);
8432 while Present (Formal) loop
8433 if Present (Default_Value (Formal)) then
8435 -- We work with a copy of the default expression because we
8436 -- do not want to disturb the original, since this would mess
8437 -- up the conformance checking.
8439 Dcopy := New_Copy_Tree (Default_Value (Formal));
8441 -- The analysis of the expression may generate insert actions,
8442 -- which of course must not be executed. We wrap those actions
8443 -- in a procedure that is not called, and later on eliminated.
8444 -- The following cases have no side effects, and are analyzed
8445 -- directly.
8447 if Nkind (Dcopy) = N_Identifier
8448 or else Nkind_In (Dcopy, N_Expanded_Name,
8449 N_Integer_Literal,
8450 N_Character_Literal,
8451 N_String_Literal,
8452 N_Real_Literal)
8453 or else (Nkind (Dcopy) = N_Attribute_Reference
8454 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
8455 or else Known_Null (Dcopy)
8456 then
8457 -- If there is no default function, we must still do a full
8458 -- analyze call on the default value, to ensure that all error
8459 -- checks are performed, e.g. those associated with static
8460 -- evaluation. Note: this branch will always be taken if the
8461 -- analyzer is turned off (but we still need the error checks).
8463 -- Note: the setting of parent here is to meet the requirement
8464 -- that we can only analyze the expression while attached to
8465 -- the tree. Really the requirement is that the parent chain
8466 -- be set, we don't actually need to be in the tree.
8468 Set_Parent (Dcopy, Declaration_Node (Formal));
8469 Analyze (Dcopy);
8471 -- Default expressions are resolved with their own type if the
8472 -- context is generic, to avoid anomalies with private types.
8474 if Ekind (Scope (E)) = E_Generic_Package then
8475 Resolve (Dcopy);
8476 else
8477 Resolve (Dcopy, Etype (Formal));
8478 end if;
8480 -- If that resolved expression will raise constraint error,
8481 -- then flag the default value as raising constraint error.
8482 -- This allows a proper error message on the calls.
8484 if Raises_Constraint_Error (Dcopy) then
8485 Set_Raises_Constraint_Error (Default_Value (Formal));
8486 end if;
8488 -- If the default is a parameterless call, we use the name of
8489 -- the called function directly, and there is no body to build.
8491 elsif Nkind (Dcopy) = N_Function_Call
8492 and then No (Parameter_Associations (Dcopy))
8493 then
8494 null;
8496 -- Else construct and analyze the body of a wrapper procedure
8497 -- that contains an object declaration to hold the expression.
8498 -- Given that this is done only to complete the analysis, it is
8499 -- simpler to build a procedure than a function which might
8500 -- involve secondary stack expansion.
8502 else
8503 Dnam := Make_Temporary (Loc, 'D');
8505 Dbody :=
8506 Make_Subprogram_Body (Loc,
8507 Specification =>
8508 Make_Procedure_Specification (Loc,
8509 Defining_Unit_Name => Dnam),
8511 Declarations => New_List (
8512 Make_Object_Declaration (Loc,
8513 Defining_Identifier => Make_Temporary (Loc, 'T'),
8514 Object_Definition =>
8515 New_Occurrence_Of (Etype (Formal), Loc),
8516 Expression => New_Copy_Tree (Dcopy))),
8518 Handled_Statement_Sequence =>
8519 Make_Handled_Sequence_Of_Statements (Loc,
8520 Statements => Empty_List));
8522 Set_Scope (Dnam, Scope (E));
8523 Set_Assignment_OK (First (Declarations (Dbody)));
8524 Set_Is_Eliminated (Dnam);
8525 Insert_After (After, Dbody);
8526 Analyze (Dbody);
8527 After := Dbody;
8528 end if;
8529 end if;
8531 Next_Formal (Formal);
8532 end loop;
8533 end Process_Default_Expressions;
8535 ----------------------------------------
8536 -- Set_Component_Alignment_If_Not_Set --
8537 ----------------------------------------
8539 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
8540 begin
8541 -- Ignore if not base type, subtypes don't need anything
8543 if Typ /= Base_Type (Typ) then
8544 return;
8545 end if;
8547 -- Do not override existing representation
8549 if Is_Packed (Typ) then
8550 return;
8552 elsif Has_Specified_Layout (Typ) then
8553 return;
8555 elsif Component_Alignment (Typ) /= Calign_Default then
8556 return;
8558 else
8559 Set_Component_Alignment
8560 (Typ, Scope_Stack.Table
8561 (Scope_Stack.Last).Component_Alignment_Default);
8562 end if;
8563 end Set_Component_Alignment_If_Not_Set;
8565 --------------------------
8566 -- Set_SSO_From_Default --
8567 --------------------------
8569 procedure Set_SSO_From_Default (T : Entity_Id) is
8570 Reversed : Boolean;
8572 begin
8573 -- Set default SSO for an array or record base type, except in case of
8574 -- a type extension (which always inherits the SSO of its parent type).
8576 if Is_Base_Type (T)
8577 and then (Is_Array_Type (T)
8578 or else (Is_Record_Type (T)
8579 and then not (Is_Tagged_Type (T)
8580 and then Is_Derived_Type (T))))
8581 then
8582 Reversed :=
8583 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
8584 or else
8585 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
8587 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
8589 -- For a record type, if bit order is specified explicitly,
8590 -- then do not set SSO from default if not consistent. Note that
8591 -- we do not want to look at a Bit_Order attribute definition
8592 -- for a parent: if we were to inherit Bit_Order, then both
8593 -- SSO_Set_*_By_Default flags would have been cleared already
8594 -- (by Inherit_Aspects_At_Freeze_Point).
8596 and then not
8597 (Is_Record_Type (T)
8598 and then
8599 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
8600 and then Reverse_Bit_Order (T) /= Reversed)
8601 then
8602 -- If flags cause reverse storage order, then set the result. Note
8603 -- that we would have ignored the pragma setting the non default
8604 -- storage order in any case, hence the assertion at this point.
8606 pragma Assert
8607 (not Reversed or else Support_Nondefault_SSO_On_Target);
8609 Set_Reverse_Storage_Order (T, Reversed);
8611 -- For a record type, also set reversed bit order. Note: if a bit
8612 -- order has been specified explicitly, then this is a no-op.
8614 if Is_Record_Type (T) then
8615 Set_Reverse_Bit_Order (T, Reversed);
8616 end if;
8617 end if;
8618 end if;
8619 end Set_SSO_From_Default;
8621 ------------------
8622 -- Undelay_Type --
8623 ------------------
8625 procedure Undelay_Type (T : Entity_Id) is
8626 begin
8627 Set_Has_Delayed_Freeze (T, False);
8628 Set_Freeze_Node (T, Empty);
8630 -- Since we don't want T to have a Freeze_Node, we don't want its
8631 -- Full_View or Corresponding_Record_Type to have one either.
8633 -- ??? Fundamentally, this whole handling is unpleasant. What we really
8634 -- want is to be sure that for an Itype that's part of record R and is a
8635 -- subtype of type T, that it's frozen after the later of the freeze
8636 -- points of R and T. We have no way of doing that directly, so what we
8637 -- do is force most such Itypes to be frozen as part of freezing R via
8638 -- this procedure and only delay the ones that need to be delayed
8639 -- (mostly the designated types of access types that are defined as part
8640 -- of the record).
8642 if Is_Private_Type (T)
8643 and then Present (Full_View (T))
8644 and then Is_Itype (Full_View (T))
8645 and then Is_Record_Type (Scope (Full_View (T)))
8646 then
8647 Undelay_Type (Full_View (T));
8648 end if;
8650 if Is_Concurrent_Type (T)
8651 and then Present (Corresponding_Record_Type (T))
8652 and then Is_Itype (Corresponding_Record_Type (T))
8653 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
8654 then
8655 Undelay_Type (Corresponding_Record_Type (T));
8656 end if;
8657 end Undelay_Type;
8659 ------------------
8660 -- Warn_Overlay --
8661 ------------------
8663 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Entity_Id) is
8664 Ent : constant Entity_Id := Entity (Nam);
8665 -- The object to which the address clause applies
8667 Init : Node_Id;
8668 Old : Entity_Id := Empty;
8669 Decl : Node_Id;
8671 begin
8672 -- No warning if address clause overlay warnings are off
8674 if not Address_Clause_Overlay_Warnings then
8675 return;
8676 end if;
8678 -- No warning if there is an explicit initialization
8680 Init := Original_Node (Expression (Declaration_Node (Ent)));
8682 if Present (Init) and then Comes_From_Source (Init) then
8683 return;
8684 end if;
8686 -- We only give the warning for non-imported entities of a type for
8687 -- which a non-null base init proc is defined, or for objects of access
8688 -- types with implicit null initialization, or when Normalize_Scalars
8689 -- applies and the type is scalar or a string type (the latter being
8690 -- tested for because predefined String types are initialized by inline
8691 -- code rather than by an init_proc). Note that we do not give the
8692 -- warning for Initialize_Scalars, since we suppressed initialization
8693 -- in this case. Also, do not warn if Suppress_Initialization is set
8694 -- either on the type, or on the object via pragma or aspect.
8696 if Present (Expr)
8697 and then not Is_Imported (Ent)
8698 and then not Initialization_Suppressed (Typ)
8699 and then not (Ekind (Ent) = E_Variable
8700 and then Initialization_Suppressed (Ent))
8701 and then (Has_Non_Null_Base_Init_Proc (Typ)
8702 or else Is_Access_Type (Typ)
8703 or else (Normalize_Scalars
8704 and then (Is_Scalar_Type (Typ)
8705 or else Is_String_Type (Typ))))
8706 then
8707 if Nkind (Expr) = N_Attribute_Reference
8708 and then Is_Entity_Name (Prefix (Expr))
8709 then
8710 Old := Entity (Prefix (Expr));
8712 elsif Is_Entity_Name (Expr)
8713 and then Ekind (Entity (Expr)) = E_Constant
8714 then
8715 Decl := Declaration_Node (Entity (Expr));
8717 if Nkind (Decl) = N_Object_Declaration
8718 and then Present (Expression (Decl))
8719 and then Nkind (Expression (Decl)) = N_Attribute_Reference
8720 and then Is_Entity_Name (Prefix (Expression (Decl)))
8721 then
8722 Old := Entity (Prefix (Expression (Decl)));
8724 elsif Nkind (Expr) = N_Function_Call then
8725 return;
8726 end if;
8728 -- A function call (most likely to To_Address) is probably not an
8729 -- overlay, so skip warning. Ditto if the function call was inlined
8730 -- and transformed into an entity.
8732 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
8733 return;
8734 end if;
8736 -- If a pragma Import follows, we assume that it is for the current
8737 -- target of the address clause, and skip the warning. There may be
8738 -- a source pragma or an aspect that specifies import and generates
8739 -- the corresponding pragma. These will indicate that the entity is
8740 -- imported and that is checked above so that the spurious warning
8741 -- (generated when the entity is frozen) will be suppressed. The
8742 -- pragma may be attached to the aspect, so it is not yet a list
8743 -- member.
8745 if Is_List_Member (Parent (Expr)) then
8746 Decl := Next (Parent (Expr));
8748 if Present (Decl)
8749 and then Nkind (Decl) = N_Pragma
8750 and then Pragma_Name (Decl) = Name_Import
8751 then
8752 return;
8753 end if;
8754 end if;
8756 -- Otherwise give warning message
8758 if Present (Old) then
8759 Error_Msg_Node_2 := Old;
8760 Error_Msg_N
8761 ("default initialization of & may modify &??",
8762 Nam);
8763 else
8764 Error_Msg_N
8765 ("default initialization of & may modify overlaid storage??",
8766 Nam);
8767 end if;
8769 -- Add friendly warning if initialization comes from a packed array
8770 -- component.
8772 if Is_Record_Type (Typ) then
8773 declare
8774 Comp : Entity_Id;
8776 begin
8777 Comp := First_Component (Typ);
8778 while Present (Comp) loop
8779 if Nkind (Parent (Comp)) = N_Component_Declaration
8780 and then Present (Expression (Parent (Comp)))
8781 then
8782 exit;
8783 elsif Is_Array_Type (Etype (Comp))
8784 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
8785 then
8786 Error_Msg_NE
8787 ("\packed array component& " &
8788 "will be initialized to zero??",
8789 Nam, Comp);
8790 exit;
8791 else
8792 Next_Component (Comp);
8793 end if;
8794 end loop;
8795 end;
8796 end if;
8798 Error_Msg_N
8799 ("\use pragma Import for & to " &
8800 "suppress initialization (RM B.1(24))??",
8801 Nam);
8802 end if;
8803 end Warn_Overlay;
8805 end Freeze;