Fix typos in riscv register save/restore.
[official-gcc.git] / gcc / ada / freeze.adb
blobbc7694cd1701b9b55a078cdea94382b05d4ac46a
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-2017, 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_Disp; use Exp_Disp;
37 with Exp_Pakd; use Exp_Pakd;
38 with Exp_Util; use Exp_Util;
39 with Exp_Tss; use Exp_Tss;
40 with Ghost; use Ghost;
41 with Layout; use Layout;
42 with Lib; use Lib;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Mech; use Sem_Mech;
59 with Sem_Prag; use Sem_Prag;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sinfo; use Sinfo;
63 with Snames; use Snames;
64 with Stand; use Stand;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uintp; use Uintp;
69 with Urealp; use Urealp;
70 with Warnsw; use Warnsw;
72 package body Freeze is
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
79 -- Typ is a type that is being frozen. If no size clause is given,
80 -- but a default Esize has been computed, then this default Esize is
81 -- adjusted up if necessary to be consistent with a given alignment,
82 -- but never to a value greater than Long_Long_Integer'Size. This
83 -- is used for all discrete types and for fixed-point types.
85 procedure Build_And_Analyze_Renamed_Body
86 (Decl : Node_Id;
87 New_S : Entity_Id;
88 After : in out Node_Id);
89 -- Build body for a renaming declaration, insert in tree and analyze
91 procedure Check_Address_Clause (E : Entity_Id);
92 -- Apply legality checks to address clauses for object declarations,
93 -- at the point the object is frozen. Also ensure any initialization is
94 -- performed only after the object has been frozen.
96 procedure Check_Component_Storage_Order
97 (Encl_Type : Entity_Id;
98 Comp : Entity_Id;
99 ADC : Node_Id;
100 Comp_ADC_Present : out Boolean);
101 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
102 -- clause, verify that the component type has an explicit and compatible
103 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
104 -- entity of the component under consideration. For an Encl_Type that
105 -- does not have a Scalar_Storage_Order attribute definition clause,
106 -- verify that the component also does not have such a clause.
107 -- ADC is the attribute definition clause if present (or Empty). On return,
108 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
109 -- attribute definition clause.
111 procedure Check_Debug_Info_Needed (T : Entity_Id);
112 -- As each entity is frozen, this routine is called to deal with the
113 -- setting of Debug_Info_Needed for the entity. This flag is set if
114 -- the entity comes from source, or if we are in Debug_Generated_Code
115 -- mode or if the -gnatdV debug flag is set. However, it never sets
116 -- the flag if Debug_Info_Off is set. This procedure also ensures that
117 -- subsidiary entities have the flag set as required.
119 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
120 -- When an expression function is frozen by a use of it, the expression
121 -- itself is frozen. Check that the expression does not include references
122 -- to deferred constants without completion. We report this at the freeze
123 -- point of the function, to provide a better error message.
125 -- In most cases the expression itself is frozen by the time the function
126 -- itself is frozen, because the formals will be frozen by then. However,
127 -- Attribute references to outer types are freeze points for those types;
128 -- this routine generates the required freeze nodes for them.
130 procedure Check_Inherited_Conditions (R : Entity_Id);
131 -- For a tagged derived type, create wrappers for inherited operations
132 -- that have a class-wide condition, so it can be properly rewritten if
133 -- it involves calls to other overriding primitives.
135 procedure Check_Strict_Alignment (E : Entity_Id);
136 -- E is a base type. If E is tagged or has a component that is aliased
137 -- or tagged or contains something this is aliased or tagged, set
138 -- Strict_Alignment.
140 procedure Check_Unsigned_Type (E : Entity_Id);
141 pragma Inline (Check_Unsigned_Type);
142 -- If E is a fixed-point or discrete type, then all the necessary work
143 -- to freeze it is completed except for possible setting of the flag
144 -- Is_Unsigned_Type, which is done by this procedure. The call has no
145 -- effect if the entity E is not a discrete or fixed-point type.
147 procedure Freeze_And_Append
148 (Ent : Entity_Id;
149 N : Node_Id;
150 Result : in out List_Id);
151 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
152 -- nodes to Result, modifying Result from No_List if necessary. N has
153 -- the same usage as in Freeze_Entity.
155 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
156 -- Freeze enumeration type. The Esize field is set as processing
157 -- proceeds (i.e. set by default when the type is declared and then
158 -- adjusted by rep clauses. What this procedure does is to make sure
159 -- that if a foreign convention is specified, and no specific size
160 -- is given, then the size must be at least Integer'Size.
162 procedure Freeze_Static_Object (E : Entity_Id);
163 -- If an object is frozen which has Is_Statically_Allocated set, then
164 -- all referenced types must also be marked with this flag. This routine
165 -- is in charge of meeting this requirement for the object entity E.
167 procedure Freeze_Subprogram (E : Entity_Id);
168 -- Perform freezing actions for a subprogram (create extra formals,
169 -- and set proper default mechanism values). Note that this routine
170 -- is not called for internal subprograms, for which neither of these
171 -- actions is needed (or desirable, we do not want for example to have
172 -- these extra formals present in initialization procedures, where they
173 -- would serve no purpose). In this call E is either a subprogram or
174 -- a subprogram type (i.e. an access to a subprogram).
176 function Is_Fully_Defined (T : Entity_Id) return Boolean;
177 -- True if T is not private and has no private components, or has a full
178 -- view. Used to determine whether the designated type of an access type
179 -- should be frozen when the access type is frozen. This is done when an
180 -- allocator is frozen, or an expression that may involve attributes of
181 -- the designated type. Otherwise freezing the access type does not freeze
182 -- the designated type.
184 procedure Process_Default_Expressions
185 (E : Entity_Id;
186 After : in out Node_Id);
187 -- This procedure is called for each subprogram to complete processing of
188 -- default expressions at the point where all types are known to be frozen.
189 -- The expressions must be analyzed in full, to make sure that all error
190 -- processing is done (they have only been pre-analyzed). If the expression
191 -- is not an entity or literal, its analysis may generate code which must
192 -- not be executed. In that case we build a function body to hold that
193 -- code. This wrapper function serves no other purpose (it used to be
194 -- called to evaluate the default, but now the default is inlined at each
195 -- point of call).
197 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
198 -- Typ is a record or array type that is being frozen. This routine sets
199 -- the default component alignment from the scope stack values if the
200 -- alignment is otherwise not specified.
202 procedure Set_SSO_From_Default (T : Entity_Id);
203 -- T is a record or array type that is being frozen. If it is a base type,
204 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
205 -- will be set appropriately. Note that an explicit occurrence of aspect
206 -- Scalar_Storage_Order or an explicit setting of this aspect with an
207 -- attribute definition clause occurs, then these two flags are reset in
208 -- any case, so call will have no effect.
210 procedure Undelay_Type (T : Entity_Id);
211 -- T is a type of a component that we know to be an Itype. We don't want
212 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
213 -- Full_View or Corresponding_Record_Type.
215 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
216 -- Expr is the expression for an address clause for entity Nam whose type
217 -- is Typ. If Typ has a default initialization, and there is no explicit
218 -- initialization in the source declaration, check whether the address
219 -- clause might cause overlaying of an entity, and emit a warning on the
220 -- side effect that the initialization will cause.
222 -------------------------------
223 -- Adjust_Esize_For_Alignment --
224 -------------------------------
226 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
227 Align : Uint;
229 begin
230 if Known_Esize (Typ) and then Known_Alignment (Typ) then
231 Align := Alignment_In_Bits (Typ);
233 if Align > Esize (Typ)
234 and then Align <= Standard_Long_Long_Integer_Size
235 then
236 Set_Esize (Typ, Align);
237 end if;
238 end if;
239 end Adjust_Esize_For_Alignment;
241 ------------------------------------
242 -- Build_And_Analyze_Renamed_Body --
243 ------------------------------------
245 procedure Build_And_Analyze_Renamed_Body
246 (Decl : Node_Id;
247 New_S : Entity_Id;
248 After : in out Node_Id)
250 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
251 Ent : constant Entity_Id := Defining_Entity (Decl);
252 Body_Node : Node_Id;
253 Renamed_Subp : Entity_Id;
255 begin
256 -- If the renamed subprogram is intrinsic, there is no need for a
257 -- wrapper body: we set the alias that will be called and expanded which
258 -- completes the declaration. This transformation is only legal if the
259 -- renamed entity has already been elaborated.
261 -- Note that it is legal for a renaming_as_body to rename an intrinsic
262 -- subprogram, as long as the renaming occurs before the new entity
263 -- is frozen (RM 8.5.4 (5)).
265 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
266 and then Is_Entity_Name (Name (Body_Decl))
267 then
268 Renamed_Subp := Entity (Name (Body_Decl));
269 else
270 Renamed_Subp := Empty;
271 end if;
273 if Present (Renamed_Subp)
274 and then Is_Intrinsic_Subprogram (Renamed_Subp)
275 and then
276 (not In_Same_Source_Unit (Renamed_Subp, Ent)
277 or else Sloc (Renamed_Subp) < Sloc (Ent))
279 -- We can make the renaming entity intrinsic if the renamed function
280 -- has an interface name, or if it is one of the shift/rotate
281 -- operations known to the compiler.
283 and then
284 (Present (Interface_Name (Renamed_Subp))
285 or else Nam_In (Chars (Renamed_Subp), Name_Rotate_Left,
286 Name_Rotate_Right,
287 Name_Shift_Left,
288 Name_Shift_Right,
289 Name_Shift_Right_Arithmetic))
290 then
291 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
293 if Present (Alias (Renamed_Subp)) then
294 Set_Alias (Ent, Alias (Renamed_Subp));
295 else
296 Set_Alias (Ent, Renamed_Subp);
297 end if;
299 Set_Is_Intrinsic_Subprogram (Ent);
300 Set_Has_Completion (Ent);
302 else
303 Body_Node := Build_Renamed_Body (Decl, New_S);
304 Insert_After (After, Body_Node);
305 Mark_Rewrite_Insertion (Body_Node);
306 Analyze (Body_Node);
307 After := Body_Node;
308 end if;
309 end Build_And_Analyze_Renamed_Body;
311 ------------------------
312 -- Build_Renamed_Body --
313 ------------------------
315 function Build_Renamed_Body
316 (Decl : Node_Id;
317 New_S : Entity_Id) return Node_Id
319 Loc : constant Source_Ptr := Sloc (New_S);
320 -- We use for the source location of the renamed body, the location of
321 -- the spec entity. It might seem more natural to use the location of
322 -- the renaming declaration itself, but that would be wrong, since then
323 -- the body we create would look as though it was created far too late,
324 -- and this could cause problems with elaboration order analysis,
325 -- particularly in connection with instantiations.
327 N : constant Node_Id := Unit_Declaration_Node (New_S);
328 Nam : constant Node_Id := Name (N);
329 Old_S : Entity_Id;
330 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
331 Actuals : List_Id := No_List;
332 Call_Node : Node_Id;
333 Call_Name : Node_Id;
334 Body_Node : Node_Id;
335 Formal : Entity_Id;
336 O_Formal : Entity_Id;
337 Param_Spec : Node_Id;
339 Pref : Node_Id := Empty;
340 -- If the renamed entity is a primitive operation given in prefix form,
341 -- the prefix is the target object and it has to be added as the first
342 -- actual in the generated call.
344 begin
345 -- Determine the entity being renamed, which is the target of the call
346 -- statement. If the name is an explicit dereference, this is a renaming
347 -- of a subprogram type rather than a subprogram. The name itself is
348 -- fully analyzed.
350 if Nkind (Nam) = N_Selected_Component then
351 Old_S := Entity (Selector_Name (Nam));
353 elsif Nkind (Nam) = N_Explicit_Dereference then
354 Old_S := Etype (Nam);
356 elsif Nkind (Nam) = N_Indexed_Component then
357 if Is_Entity_Name (Prefix (Nam)) then
358 Old_S := Entity (Prefix (Nam));
359 else
360 Old_S := Entity (Selector_Name (Prefix (Nam)));
361 end if;
363 elsif Nkind (Nam) = N_Character_Literal then
364 Old_S := Etype (New_S);
366 else
367 Old_S := Entity (Nam);
368 end if;
370 if Is_Entity_Name (Nam) then
372 -- If the renamed entity is a predefined operator, retain full name
373 -- to ensure its visibility.
375 if Ekind (Old_S) = E_Operator
376 and then Nkind (Nam) = N_Expanded_Name
377 then
378 Call_Name := New_Copy (Name (N));
379 else
380 Call_Name := New_Occurrence_Of (Old_S, Loc);
381 end if;
383 else
384 if Nkind (Nam) = N_Selected_Component
385 and then Present (First_Formal (Old_S))
386 and then
387 (Is_Controlling_Formal (First_Formal (Old_S))
388 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
389 then
391 -- Retrieve the target object, to be added as a first actual
392 -- in the call.
394 Call_Name := New_Occurrence_Of (Old_S, Loc);
395 Pref := Prefix (Nam);
397 else
398 Call_Name := New_Copy (Name (N));
399 end if;
401 -- Original name may have been overloaded, but is fully resolved now
403 Set_Is_Overloaded (Call_Name, False);
404 end if;
406 -- For simple renamings, subsequent calls can be expanded directly as
407 -- calls to the renamed entity. The body must be generated in any case
408 -- for calls that may appear elsewhere. This is not done in the case
409 -- where the subprogram is an instantiation because the actual proper
410 -- body has not been built yet.
412 if Ekind_In (Old_S, E_Function, E_Procedure)
413 and then Nkind (Decl) = N_Subprogram_Declaration
414 and then not Is_Generic_Instance (Old_S)
415 then
416 Set_Body_To_Inline (Decl, Old_S);
417 end if;
419 -- Check whether the return type is a limited view. If the subprogram
420 -- is already frozen the generated body may have a non-limited view
421 -- of the type, that must be used, because it is the one in the spec
422 -- of the renaming declaration.
424 if Ekind (Old_S) = E_Function
425 and then Is_Entity_Name (Result_Definition (Spec))
426 then
427 declare
428 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
429 begin
430 if Has_Non_Limited_View (Ret_Type) then
431 Set_Result_Definition
432 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
433 end if;
434 end;
435 end if;
437 -- The body generated for this renaming is an internal artifact, and
438 -- does not constitute a freeze point for the called entity.
440 Set_Must_Not_Freeze (Call_Name);
442 Formal := First_Formal (Defining_Entity (Decl));
444 if Present (Pref) then
445 declare
446 Pref_Type : constant Entity_Id := Etype (Pref);
447 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
449 begin
450 -- The controlling formal may be an access parameter, or the
451 -- actual may be an access value, so adjust accordingly.
453 if Is_Access_Type (Pref_Type)
454 and then not Is_Access_Type (Form_Type)
455 then
456 Actuals := New_List
457 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
459 elsif Is_Access_Type (Form_Type)
460 and then not Is_Access_Type (Pref)
461 then
462 Actuals :=
463 New_List (
464 Make_Attribute_Reference (Loc,
465 Attribute_Name => Name_Access,
466 Prefix => Relocate_Node (Pref)));
467 else
468 Actuals := New_List (Pref);
469 end if;
470 end;
472 elsif Present (Formal) then
473 Actuals := New_List;
475 else
476 Actuals := No_List;
477 end if;
479 if Present (Formal) then
480 while Present (Formal) loop
481 Append (New_Occurrence_Of (Formal, Loc), Actuals);
482 Next_Formal (Formal);
483 end loop;
484 end if;
486 -- If the renamed entity is an entry, inherit its profile. For other
487 -- renamings as bodies, both profiles must be subtype conformant, so it
488 -- is not necessary to replace the profile given in the declaration.
489 -- However, default values that are aggregates are rewritten when
490 -- partially analyzed, so we recover the original aggregate to insure
491 -- that subsequent conformity checking works. Similarly, if the default
492 -- expression was constant-folded, recover the original expression.
494 Formal := First_Formal (Defining_Entity (Decl));
496 if Present (Formal) then
497 O_Formal := First_Formal (Old_S);
498 Param_Spec := First (Parameter_Specifications (Spec));
499 while Present (Formal) loop
500 if Is_Entry (Old_S) then
501 if Nkind (Parameter_Type (Param_Spec)) /=
502 N_Access_Definition
503 then
504 Set_Etype (Formal, Etype (O_Formal));
505 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
506 end if;
508 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
509 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
510 Nkind (Default_Value (O_Formal))
511 then
512 Set_Expression (Param_Spec,
513 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
514 end if;
516 Next_Formal (Formal);
517 Next_Formal (O_Formal);
518 Next (Param_Spec);
519 end loop;
520 end if;
522 -- If the renamed entity is a function, the generated body contains a
523 -- return statement. Otherwise, build a procedure call. If the entity is
524 -- an entry, subsequent analysis of the call will transform it into the
525 -- proper entry or protected operation call. If the renamed entity is
526 -- a character literal, return it directly.
528 if Ekind (Old_S) = E_Function
529 or else Ekind (Old_S) = E_Operator
530 or else (Ekind (Old_S) = E_Subprogram_Type
531 and then Etype (Old_S) /= Standard_Void_Type)
532 then
533 Call_Node :=
534 Make_Simple_Return_Statement (Loc,
535 Expression =>
536 Make_Function_Call (Loc,
537 Name => Call_Name,
538 Parameter_Associations => Actuals));
540 elsif Ekind (Old_S) = E_Enumeration_Literal then
541 Call_Node :=
542 Make_Simple_Return_Statement (Loc,
543 Expression => New_Occurrence_Of (Old_S, Loc));
545 elsif Nkind (Nam) = N_Character_Literal then
546 Call_Node :=
547 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
549 else
550 Call_Node :=
551 Make_Procedure_Call_Statement (Loc,
552 Name => Call_Name,
553 Parameter_Associations => Actuals);
554 end if;
556 -- Create entities for subprogram body and formals
558 Set_Defining_Unit_Name (Spec,
559 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
561 Param_Spec := First (Parameter_Specifications (Spec));
562 while Present (Param_Spec) loop
563 Set_Defining_Identifier (Param_Spec,
564 Make_Defining_Identifier (Loc,
565 Chars => Chars (Defining_Identifier (Param_Spec))));
566 Next (Param_Spec);
567 end loop;
569 Body_Node :=
570 Make_Subprogram_Body (Loc,
571 Specification => Spec,
572 Declarations => New_List,
573 Handled_Statement_Sequence =>
574 Make_Handled_Sequence_Of_Statements (Loc,
575 Statements => New_List (Call_Node)));
577 if Nkind (Decl) /= N_Subprogram_Declaration then
578 Rewrite (N,
579 Make_Subprogram_Declaration (Loc,
580 Specification => Specification (N)));
581 end if;
583 -- Link the body to the entity whose declaration it completes. If
584 -- the body is analyzed when the renamed entity is frozen, it may
585 -- be necessary to restore the proper scope (see package Exp_Ch13).
587 if Nkind (N) = N_Subprogram_Renaming_Declaration
588 and then Present (Corresponding_Spec (N))
589 then
590 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
591 else
592 Set_Corresponding_Spec (Body_Node, New_S);
593 end if;
595 return Body_Node;
596 end Build_Renamed_Body;
598 --------------------------
599 -- Check_Address_Clause --
600 --------------------------
602 procedure Check_Address_Clause (E : Entity_Id) is
603 Addr : constant Node_Id := Address_Clause (E);
604 Typ : constant Entity_Id := Etype (E);
605 Decl : Node_Id;
606 Expr : Node_Id;
607 Init : Node_Id;
608 Lhs : Node_Id;
609 Tag_Assign : Node_Id;
611 begin
612 if Present (Addr) then
614 -- For a deferred constant, the initialization value is on full view
616 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
617 Decl := Declaration_Node (Full_View (E));
618 else
619 Decl := Declaration_Node (E);
620 end if;
622 Expr := Expression (Addr);
624 if Needs_Constant_Address (Decl, Typ) then
625 Check_Constant_Address_Clause (Expr, E);
627 -- Has_Delayed_Freeze was set on E when the address clause was
628 -- analyzed, and must remain set because we want the address
629 -- clause to be elaborated only after any entity it references
630 -- has been elaborated.
631 end if;
633 -- If Rep_Clauses are to be ignored, remove address clause from
634 -- list attached to entity, because it may be illegal for gigi,
635 -- for example by breaking order of elaboration..
637 if Ignore_Rep_Clauses then
638 declare
639 Rep : Node_Id;
641 begin
642 Rep := First_Rep_Item (E);
644 if Rep = Addr then
645 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
647 else
648 while Present (Rep)
649 and then Next_Rep_Item (Rep) /= Addr
650 loop
651 Rep := Next_Rep_Item (Rep);
652 end loop;
653 end if;
655 if Present (Rep) then
656 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
657 end if;
658 end;
660 -- And now remove the address clause
662 Kill_Rep_Clause (Addr);
664 elsif not Error_Posted (Expr)
665 and then not Needs_Finalization (Typ)
666 then
667 Warn_Overlay (Expr, Typ, Name (Addr));
668 end if;
670 Init := Expression (Decl);
672 -- If a variable, or a non-imported constant, overlays a constant
673 -- object and has an initialization value, then the initialization
674 -- may end up writing into read-only memory. Detect the cases of
675 -- statically identical values and remove the initialization. In
676 -- the other cases, give a warning. We will give other warnings
677 -- later for the variable if it is assigned.
679 if (Ekind (E) = E_Variable
680 or else (Ekind (E) = E_Constant
681 and then not Is_Imported (E)))
682 and then Overlays_Constant (E)
683 and then Present (Init)
684 then
685 declare
686 O_Ent : Entity_Id;
687 Off : Boolean;
689 begin
690 Find_Overlaid_Entity (Addr, O_Ent, Off);
692 if Ekind (O_Ent) = E_Constant
693 and then Etype (O_Ent) = Typ
694 and then Present (Constant_Value (O_Ent))
695 and then Compile_Time_Compare
696 (Init,
697 Constant_Value (O_Ent),
698 Assume_Valid => True) = EQ
699 then
700 Set_No_Initialization (Decl);
701 return;
703 elsif Comes_From_Source (Init)
704 and then Address_Clause_Overlay_Warnings
705 then
706 Error_Msg_Sloc := Sloc (Addr);
707 Error_Msg_NE
708 ("??constant& may be modified via address clause#",
709 Decl, O_Ent);
710 end if;
711 end;
712 end if;
714 if Present (Init) then
716 -- Capture initialization value at point of declaration,
717 -- and make explicit assignment legal, because object may
718 -- be a constant.
720 Remove_Side_Effects (Init);
721 Lhs := New_Occurrence_Of (E, Sloc (Decl));
722 Set_Assignment_OK (Lhs);
724 -- Move initialization to freeze actions, once the object has
725 -- been frozen and the address clause alignment check has been
726 -- performed.
728 Append_Freeze_Action (E,
729 Make_Assignment_Statement (Sloc (Decl),
730 Name => Lhs,
731 Expression => Expression (Decl)));
733 Set_No_Initialization (Decl);
735 -- If the objet is tagged, check whether the tag must be
736 -- reassigned explicitly.
738 Tag_Assign := Make_Tag_Assignment (Decl);
739 if Present (Tag_Assign) then
740 Append_Freeze_Action (E, Tag_Assign);
741 end if;
742 end if;
743 end if;
744 end Check_Address_Clause;
746 -----------------------------
747 -- Check_Compile_Time_Size --
748 -----------------------------
750 procedure Check_Compile_Time_Size (T : Entity_Id) is
752 procedure Set_Small_Size (T : Entity_Id; S : Uint);
753 -- Sets the compile time known size (64 bits or less) in the RM_Size
754 -- field of T, checking for a size clause that was given which attempts
755 -- to give a smaller size.
757 function Size_Known (T : Entity_Id) return Boolean;
758 -- Recursive function that does all the work
760 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
761 -- If T is a constrained subtype, its size is not known if any of its
762 -- discriminant constraints is not static and it is not a null record.
763 -- The test is conservative and doesn't check that the components are
764 -- in fact constrained by non-static discriminant values. Could be made
765 -- more precise ???
767 --------------------
768 -- Set_Small_Size --
769 --------------------
771 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
772 begin
773 if S > 64 then
774 return;
776 -- Check for bad size clause given
778 elsif Has_Size_Clause (T) then
779 if RM_Size (T) < S then
780 Error_Msg_Uint_1 := S;
781 Error_Msg_NE
782 ("size for& too small, minimum allowed is ^",
783 Size_Clause (T), T);
784 end if;
786 -- Set size if not set already
788 elsif Unknown_RM_Size (T) then
789 Set_RM_Size (T, S);
790 end if;
791 end Set_Small_Size;
793 ----------------
794 -- Size_Known --
795 ----------------
797 function Size_Known (T : Entity_Id) return Boolean is
798 Index : Entity_Id;
799 Comp : Entity_Id;
800 Ctyp : Entity_Id;
801 Low : Node_Id;
802 High : Node_Id;
804 begin
805 if Size_Known_At_Compile_Time (T) then
806 return True;
808 -- Always True for elementary types, even generic formal elementary
809 -- types. We used to return False in the latter case, but the size
810 -- is known at compile time, even in the template, we just do not
811 -- know the exact size but that's not the point of this routine.
813 elsif Is_Elementary_Type (T) or else Is_Task_Type (T) then
814 return True;
816 -- Array types
818 elsif Is_Array_Type (T) then
820 -- String literals always have known size, and we can set it
822 if Ekind (T) = E_String_Literal_Subtype then
823 Set_Small_Size
824 (T, Component_Size (T) * String_Literal_Length (T));
825 return True;
827 -- Unconstrained types never have known at compile time size
829 elsif not Is_Constrained (T) then
830 return False;
832 -- Don't do any recursion on type with error posted, since we may
833 -- have a malformed type that leads us into a loop.
835 elsif Error_Posted (T) then
836 return False;
838 -- Otherwise if component size unknown, then array size unknown
840 elsif not Size_Known (Component_Type (T)) then
841 return False;
842 end if;
844 -- Check for all indexes static, and also compute possible size
845 -- (in case it is not greater than 64 and may be packable).
847 declare
848 Size : Uint := Component_Size (T);
849 Dim : Uint;
851 begin
852 Index := First_Index (T);
853 while Present (Index) loop
854 if Nkind (Index) = N_Range then
855 Get_Index_Bounds (Index, Low, High);
857 elsif Error_Posted (Scalar_Range (Etype (Index))) then
858 return False;
860 else
861 Low := Type_Low_Bound (Etype (Index));
862 High := Type_High_Bound (Etype (Index));
863 end if;
865 if not Compile_Time_Known_Value (Low)
866 or else not Compile_Time_Known_Value (High)
867 or else Etype (Index) = Any_Type
868 then
869 return False;
871 else
872 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
874 if Dim >= 0 then
875 Size := Size * Dim;
876 else
877 Size := Uint_0;
878 end if;
879 end if;
881 Next_Index (Index);
882 end loop;
884 Set_Small_Size (T, Size);
885 return True;
886 end;
888 -- For non-generic private types, go to underlying type if present
890 elsif Is_Private_Type (T)
891 and then not Is_Generic_Type (T)
892 and then Present (Underlying_Type (T))
893 then
894 -- Don't do any recursion on type with error posted, since we may
895 -- have a malformed type that leads us into a loop.
897 if Error_Posted (T) then
898 return False;
899 else
900 return Size_Known (Underlying_Type (T));
901 end if;
903 -- Record types
905 elsif Is_Record_Type (T) then
907 -- A class-wide type is never considered to have a known size
909 if Is_Class_Wide_Type (T) then
910 return False;
912 -- A subtype of a variant record must not have non-static
913 -- discriminated components.
915 elsif T /= Base_Type (T)
916 and then not Static_Discriminated_Components (T)
917 then
918 return False;
920 -- Don't do any recursion on type with error posted, since we may
921 -- have a malformed type that leads us into a loop.
923 elsif Error_Posted (T) then
924 return False;
925 end if;
927 -- Now look at the components of the record
929 declare
930 -- The following two variables are used to keep track of the
931 -- size of packed records if we can tell the size of the packed
932 -- record in the front end. Packed_Size_Known is True if so far
933 -- we can figure out the size. It is initialized to True for a
934 -- packed record, unless the record has discriminants or atomic
935 -- components or independent components.
937 -- The reason we eliminate the discriminated case is that
938 -- we don't know the way the back end lays out discriminated
939 -- packed records. If Packed_Size_Known is True, then
940 -- Packed_Size is the size in bits so far.
942 Packed_Size_Known : Boolean :=
943 Is_Packed (T)
944 and then not Has_Discriminants (T)
945 and then not Has_Atomic_Components (T)
946 and then not Has_Independent_Components (T);
948 Packed_Size : Uint := Uint_0;
949 -- Size in bits so far
951 begin
952 -- Test for variant part present
954 if Has_Discriminants (T)
955 and then Present (Parent (T))
956 and then Nkind (Parent (T)) = N_Full_Type_Declaration
957 and then Nkind (Type_Definition (Parent (T))) =
958 N_Record_Definition
959 and then not Null_Present (Type_Definition (Parent (T)))
960 and then
961 Present (Variant_Part
962 (Component_List (Type_Definition (Parent (T)))))
963 then
964 -- If variant part is present, and type is unconstrained,
965 -- then we must have defaulted discriminants, or a size
966 -- clause must be present for the type, or else the size
967 -- is definitely not known at compile time.
969 if not Is_Constrained (T)
970 and then
971 No (Discriminant_Default_Value (First_Discriminant (T)))
972 and then Unknown_RM_Size (T)
973 then
974 return False;
975 end if;
976 end if;
978 -- Loop through components
980 Comp := First_Component_Or_Discriminant (T);
981 while Present (Comp) loop
982 Ctyp := Etype (Comp);
984 -- We do not know the packed size if there is a component
985 -- clause present (we possibly could, but this would only
986 -- help in the case of a record with partial rep clauses.
987 -- That's because in the case of full rep clauses, the
988 -- size gets figured out anyway by a different circuit).
990 if Present (Component_Clause (Comp)) then
991 Packed_Size_Known := False;
992 end if;
994 -- We do not know the packed size for an atomic/VFA type
995 -- or component, or an independent type or component, or a
996 -- by-reference type or aliased component (because packing
997 -- does not touch these).
999 if Is_Atomic_Or_VFA (Ctyp)
1000 or else Is_Atomic_Or_VFA (Comp)
1001 or else Is_Independent (Ctyp)
1002 or else Is_Independent (Comp)
1003 or else Is_By_Reference_Type (Ctyp)
1004 or else Is_Aliased (Comp)
1005 then
1006 Packed_Size_Known := False;
1007 end if;
1009 -- We need to identify a component that is an array where
1010 -- the index type is an enumeration type with non-standard
1011 -- representation, and some bound of the type depends on a
1012 -- discriminant.
1014 -- This is because gigi computes the size by doing a
1015 -- substitution of the appropriate discriminant value in
1016 -- the size expression for the base type, and gigi is not
1017 -- clever enough to evaluate the resulting expression (which
1018 -- involves a call to rep_to_pos) at compile time.
1020 -- It would be nice if gigi would either recognize that
1021 -- this expression can be computed at compile time, or
1022 -- alternatively figured out the size from the subtype
1023 -- directly, where all the information is at hand ???
1025 if Is_Array_Type (Etype (Comp))
1026 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1027 then
1028 declare
1029 Ocomp : constant Entity_Id :=
1030 Original_Record_Component (Comp);
1031 OCtyp : constant Entity_Id := Etype (Ocomp);
1032 Ind : Node_Id;
1033 Indtyp : Entity_Id;
1034 Lo, Hi : Node_Id;
1036 begin
1037 Ind := First_Index (OCtyp);
1038 while Present (Ind) loop
1039 Indtyp := Etype (Ind);
1041 if Is_Enumeration_Type (Indtyp)
1042 and then Has_Non_Standard_Rep (Indtyp)
1043 then
1044 Lo := Type_Low_Bound (Indtyp);
1045 Hi := Type_High_Bound (Indtyp);
1047 if Is_Entity_Name (Lo)
1048 and then Ekind (Entity (Lo)) = E_Discriminant
1049 then
1050 return False;
1052 elsif Is_Entity_Name (Hi)
1053 and then Ekind (Entity (Hi)) = E_Discriminant
1054 then
1055 return False;
1056 end if;
1057 end if;
1059 Next_Index (Ind);
1060 end loop;
1061 end;
1062 end if;
1064 -- Clearly size of record is not known if the size of one of
1065 -- the components is not known.
1067 if not Size_Known (Ctyp) then
1068 return False;
1069 end if;
1071 -- Accumulate packed size if possible
1073 if Packed_Size_Known then
1075 -- We can deal with elementary types, small packed arrays
1076 -- if the representation is a modular type and also small
1077 -- record types (if the size is not greater than 64, but
1078 -- the condition is checked by Set_Small_Size).
1080 if Is_Elementary_Type (Ctyp)
1081 or else (Is_Array_Type (Ctyp)
1082 and then Present
1083 (Packed_Array_Impl_Type (Ctyp))
1084 and then Is_Modular_Integer_Type
1085 (Packed_Array_Impl_Type (Ctyp)))
1086 or else Is_Record_Type (Ctyp)
1087 then
1088 -- If RM_Size is known and static, then we can keep
1089 -- accumulating the packed size.
1091 if Known_Static_RM_Size (Ctyp) then
1093 Packed_Size := Packed_Size + RM_Size (Ctyp);
1095 -- If we have a field whose RM_Size is not known then
1096 -- we can't figure out the packed size here.
1098 else
1099 Packed_Size_Known := False;
1100 end if;
1102 -- For other types we can't figure out the packed size
1104 else
1105 Packed_Size_Known := False;
1106 end if;
1107 end if;
1109 Next_Component_Or_Discriminant (Comp);
1110 end loop;
1112 if Packed_Size_Known then
1113 Set_Small_Size (T, Packed_Size);
1114 end if;
1116 return True;
1117 end;
1119 -- All other cases, size not known at compile time
1121 else
1122 return False;
1123 end if;
1124 end Size_Known;
1126 -------------------------------------
1127 -- Static_Discriminated_Components --
1128 -------------------------------------
1130 function Static_Discriminated_Components
1131 (T : Entity_Id) return Boolean
1133 Constraint : Elmt_Id;
1135 begin
1136 if Has_Discriminants (T)
1137 and then Present (Discriminant_Constraint (T))
1138 and then Present (First_Component (T))
1139 then
1140 Constraint := First_Elmt (Discriminant_Constraint (T));
1141 while Present (Constraint) loop
1142 if not Compile_Time_Known_Value (Node (Constraint)) then
1143 return False;
1144 end if;
1146 Next_Elmt (Constraint);
1147 end loop;
1148 end if;
1150 return True;
1151 end Static_Discriminated_Components;
1153 -- Start of processing for Check_Compile_Time_Size
1155 begin
1156 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1157 end Check_Compile_Time_Size;
1159 -----------------------------------
1160 -- Check_Component_Storage_Order --
1161 -----------------------------------
1163 procedure Check_Component_Storage_Order
1164 (Encl_Type : Entity_Id;
1165 Comp : Entity_Id;
1166 ADC : Node_Id;
1167 Comp_ADC_Present : out Boolean)
1169 Comp_Base : Entity_Id;
1170 Comp_ADC : Node_Id;
1171 Encl_Base : Entity_Id;
1172 Err_Node : Node_Id;
1174 Component_Aliased : Boolean;
1176 Comp_Byte_Aligned : Boolean := False;
1177 -- Set for the record case, True if Comp is aligned on byte boundaries
1178 -- (in which case it is allowed to have different storage order).
1180 Comp_SSO_Differs : Boolean;
1181 -- Set True when the component is a nested composite, and it does not
1182 -- have the same scalar storage order as Encl_Type.
1184 begin
1185 -- Record case
1187 if Present (Comp) then
1188 Err_Node := Comp;
1189 Comp_Base := Etype (Comp);
1191 if Is_Tag (Comp) then
1192 Comp_Byte_Aligned := True;
1193 Component_Aliased := False;
1195 else
1196 -- If a component clause is present, check if the component starts
1197 -- and ends on byte boundaries. Otherwise conservatively assume it
1198 -- does so only in the case where the record is not packed.
1200 if Present (Component_Clause (Comp)) then
1201 Comp_Byte_Aligned :=
1202 (Normalized_First_Bit (Comp) mod System_Storage_Unit = 0)
1203 and then
1204 (Esize (Comp) mod System_Storage_Unit = 0);
1205 else
1206 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1207 end if;
1209 Component_Aliased := Is_Aliased (Comp);
1210 end if;
1212 -- Array case
1214 else
1215 Err_Node := Encl_Type;
1216 Comp_Base := Component_Type (Encl_Type);
1218 Component_Aliased := Has_Aliased_Components (Encl_Type);
1219 end if;
1221 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1222 -- the attribute definition clause is attached to the first subtype.
1223 -- Also, if the base type is incomplete or private, go to full view
1224 -- if known
1226 Encl_Base := Base_Type (Encl_Type);
1227 if Present (Underlying_Type (Encl_Base)) then
1228 Encl_Base := Underlying_Type (Encl_Base);
1229 end if;
1231 Comp_Base := Base_Type (Comp_Base);
1232 if Present (Underlying_Type (Comp_Base)) then
1233 Comp_Base := Underlying_Type (Comp_Base);
1234 end if;
1236 Comp_ADC :=
1237 Get_Attribute_Definition_Clause
1238 (First_Subtype (Comp_Base), Attribute_Scalar_Storage_Order);
1239 Comp_ADC_Present := Present (Comp_ADC);
1241 -- Case of record or array component: check storage order compatibility.
1242 -- But, if the record has Complex_Representation, then it is treated as
1243 -- a scalar in the back end so the storage order is irrelevant.
1245 if (Is_Record_Type (Comp_Base)
1246 and then not Has_Complex_Representation (Comp_Base))
1247 or else Is_Array_Type (Comp_Base)
1248 then
1249 Comp_SSO_Differs :=
1250 Reverse_Storage_Order (Encl_Base) /=
1251 Reverse_Storage_Order (Comp_Base);
1253 -- Parent and extension must have same storage order
1255 if Present (Comp) and then Chars (Comp) = Name_uParent then
1256 if Comp_SSO_Differs then
1257 Error_Msg_N
1258 ("record extension must have same scalar storage order as "
1259 & "parent", Err_Node);
1260 end if;
1262 -- If component and composite SSO differs, check that component
1263 -- falls on byte boundaries and isn't bit packed.
1265 elsif Comp_SSO_Differs then
1267 -- Component SSO differs from enclosing composite:
1269 -- Reject if composite is a bit-packed array, as it is rewritten
1270 -- into an array of scalars.
1272 if Is_Bit_Packed_Array (Encl_Base) then
1273 Error_Msg_N
1274 ("type of packed array must have same scalar storage order "
1275 & "as component", Err_Node);
1277 -- Reject if not byte aligned
1279 elsif Is_Record_Type (Encl_Base)
1280 and then not Comp_Byte_Aligned
1281 then
1282 Error_Msg_N
1283 ("type of non-byte-aligned component must have same scalar "
1284 & "storage order as enclosing composite", Err_Node);
1286 -- Warn if specified only for the outer composite
1288 elsif Present (ADC) and then No (Comp_ADC) then
1289 Error_Msg_NE
1290 ("scalar storage order specified for & does not apply to "
1291 & "component?", Err_Node, Encl_Base);
1292 end if;
1293 end if;
1295 -- Enclosing type has explicit SSO: non-composite component must not
1296 -- be aliased.
1298 elsif Present (ADC) and then Component_Aliased then
1299 Error_Msg_N
1300 ("aliased component not permitted for type with explicit "
1301 & "Scalar_Storage_Order", Err_Node);
1302 end if;
1303 end Check_Component_Storage_Order;
1305 -----------------------------
1306 -- Check_Debug_Info_Needed --
1307 -----------------------------
1309 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1310 begin
1311 if Debug_Info_Off (T) then
1312 return;
1314 elsif Comes_From_Source (T)
1315 or else Debug_Generated_Code
1316 or else Debug_Flag_VV
1317 or else Needs_Debug_Info (T)
1318 then
1319 Set_Debug_Info_Needed (T);
1320 end if;
1321 end Check_Debug_Info_Needed;
1323 -------------------------------
1324 -- Check_Expression_Function --
1325 -------------------------------
1327 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1328 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1329 -- Function to search for deferred constant
1331 -------------------
1332 -- Find_Constant --
1333 -------------------
1335 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1336 begin
1337 -- When a constant is initialized with the result of a dispatching
1338 -- call, the constant declaration is rewritten as a renaming of the
1339 -- displaced function result. This scenario is not a premature use of
1340 -- a constant even though the Has_Completion flag is not set.
1342 if Is_Entity_Name (Nod)
1343 and then Present (Entity (Nod))
1344 and then Ekind (Entity (Nod)) = E_Constant
1345 and then Scope (Entity (Nod)) = Current_Scope
1346 and then Nkind (Declaration_Node (Entity (Nod))) =
1347 N_Object_Declaration
1348 and then not Is_Imported (Entity (Nod))
1349 and then not Has_Completion (Entity (Nod))
1350 and then not Is_Frozen (Entity (Nod))
1351 then
1352 Error_Msg_NE
1353 ("premature use of& in call or instance", N, Entity (Nod));
1355 elsif Nkind (Nod) = N_Attribute_Reference then
1356 Analyze (Prefix (Nod));
1358 if Is_Entity_Name (Prefix (Nod))
1359 and then Is_Type (Entity (Prefix (Nod)))
1360 then
1361 Freeze_Before (N, Entity (Prefix (Nod)));
1362 end if;
1363 end if;
1365 return OK;
1366 end Find_Constant;
1368 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1370 -- Local variables
1372 Decl : Node_Id;
1374 -- Start of processing for Check_Expression_Function
1376 begin
1377 Decl := Original_Node (Unit_Declaration_Node (Nam));
1379 -- The subprogram body created for the expression function is not
1380 -- itself a freeze point.
1382 if Scope (Nam) = Current_Scope
1383 and then Nkind (Decl) = N_Expression_Function
1384 and then Nkind (N) /= N_Subprogram_Body
1385 then
1386 Check_Deferred (Expression (Decl));
1387 end if;
1388 end Check_Expression_Function;
1390 --------------------------------
1391 -- Check_Inherited_Conditions --
1392 --------------------------------
1394 procedure Check_Inherited_Conditions (R : Entity_Id) is
1395 Prim_Ops : constant Elist_Id := Primitive_Operations (R);
1396 Decls : List_Id;
1397 Needs_Wrapper : Boolean;
1398 Op_Node : Elmt_Id;
1399 Par_Prim : Entity_Id;
1400 Prim : Entity_Id;
1402 procedure Build_Inherited_Condition_Pragmas (Subp : Entity_Id);
1403 -- Build corresponding pragmas for an operation whose ancestor has
1404 -- class-wide pre/postconditions. If the operation is inherited, the
1405 -- pragmas force the creation of a wrapper for the inherited operation.
1406 -- If the ancestor is being overridden, the pragmas are constructed only
1407 -- to verify their legality, in case they contain calls to other
1408 -- primitives that may haven been overridden.
1410 ---------------------------------------
1411 -- Build_Inherited_Condition_Pragmas --
1412 ---------------------------------------
1414 procedure Build_Inherited_Condition_Pragmas (Subp : Entity_Id) is
1415 A_Post : Node_Id;
1416 A_Pre : Node_Id;
1417 New_Prag : Node_Id;
1419 begin
1420 A_Pre := Get_Class_Wide_Pragma (Par_Prim, Pragma_Precondition);
1422 if Present (A_Pre) then
1423 New_Prag := New_Copy_Tree (A_Pre);
1424 Build_Class_Wide_Expression
1425 (Prag => New_Prag,
1426 Subp => Prim,
1427 Par_Subp => Par_Prim,
1428 Adjust_Sloc => False,
1429 Needs_Wrapper => Needs_Wrapper);
1431 if Needs_Wrapper
1432 and then not Comes_From_Source (Subp)
1433 and then Expander_Active
1434 then
1435 Append (New_Prag, Decls);
1436 end if;
1437 end if;
1439 A_Post := Get_Class_Wide_Pragma (Par_Prim, Pragma_Postcondition);
1441 if Present (A_Post) then
1442 New_Prag := New_Copy_Tree (A_Post);
1443 Build_Class_Wide_Expression
1444 (Prag => New_Prag,
1445 Subp => Prim,
1446 Par_Subp => Par_Prim,
1447 Adjust_Sloc => False,
1448 Needs_Wrapper => Needs_Wrapper);
1450 if Needs_Wrapper
1451 and then not Comes_From_Source (Subp)
1452 and then Expander_Active
1453 then
1454 Append (New_Prag, Decls);
1455 end if;
1456 end if;
1457 end Build_Inherited_Condition_Pragmas;
1459 -- Start of processing for Check_Inherited_Conditions
1461 begin
1462 Op_Node := First_Elmt (Prim_Ops);
1463 while Present (Op_Node) loop
1464 Prim := Node (Op_Node);
1466 -- Map the overridden primitive to the overriding one. This takes
1467 -- care of all overridings and is done only once.
1469 if Present (Overridden_Operation (Prim))
1470 and then Comes_From_Source (Prim)
1471 then
1472 Par_Prim := Overridden_Operation (Prim);
1473 Update_Primitives_Mapping (Par_Prim, Prim);
1474 end if;
1476 Next_Elmt (Op_Node);
1477 end loop;
1479 -- Perform validity checks on the inherited conditions of overriding
1480 -- operations, for conformance with LSP, and apply SPARK-specific
1481 -- restrictions on inherited conditions.
1483 Op_Node := First_Elmt (Prim_Ops);
1484 while Present (Op_Node) loop
1485 Prim := Node (Op_Node);
1487 if Present (Overridden_Operation (Prim))
1488 and then Comes_From_Source (Prim)
1489 then
1490 Par_Prim := Overridden_Operation (Prim);
1492 -- Analyze the contract items of the overridden operation, before
1493 -- they are rewritten as pragmas.
1495 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1497 -- In GNATprove mode this is where we can collect the inherited
1498 -- conditions, because we do not create the Check pragmas that
1499 -- normally convey the the modified class-wide conditions on
1500 -- overriding operations.
1502 if GNATprove_Mode then
1503 Collect_Inherited_Class_Wide_Conditions (Prim);
1505 -- Otherwise build the corresponding pragmas to check for legality
1506 -- of the inherited condition.
1508 else
1509 Build_Inherited_Condition_Pragmas (Prim);
1510 end if;
1511 end if;
1513 Next_Elmt (Op_Node);
1514 end loop;
1516 -- Now examine the inherited operations to check whether they require
1517 -- a wrapper to handle inherited conditions that call other primitives,
1518 -- so that LSP can be verified/enforced.
1520 Op_Node := First_Elmt (Prim_Ops);
1521 Needs_Wrapper := False;
1523 while Present (Op_Node) loop
1524 Decls := Empty_List;
1525 Prim := Node (Op_Node);
1527 if not Comes_From_Source (Prim) and then Present (Alias (Prim)) then
1528 Par_Prim := Alias (Prim);
1530 -- Analyze the contract items of the parent operation, and
1531 -- determine whether a wrapper is needed. This is determined
1532 -- when the condition is rewritten in sem_prag, using the
1533 -- mapping between overridden and overriding operations built
1534 -- in the loop above.
1536 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1537 Build_Inherited_Condition_Pragmas (Prim);
1538 end if;
1540 if Needs_Wrapper
1541 and then not Is_Abstract_Subprogram (Par_Prim)
1542 and then Expander_Active
1543 then
1544 -- We need to build a new primitive that overrides the inherited
1545 -- one, and whose inherited expression has been updated above.
1546 -- These expressions are the arguments of pragmas that are part
1547 -- of the declarations of the wrapper. The wrapper holds a single
1548 -- statement that is a call to the class-wide clone, where the
1549 -- controlling actuals are conversions to the corresponding type
1550 -- in the parent primitive:
1552 -- procedure New_Prim (F1 : T1; ...);
1553 -- procedure New_Prim (F1 : T1; ...) is
1554 -- pragma Check (Precondition, Expr);
1555 -- begin
1556 -- Par_Prim_Clone (Par_Type (F1), ...);
1557 -- end;
1559 -- If the primitive is a function the statement is a return
1560 -- statement with a call.
1562 declare
1563 Loc : constant Source_Ptr := Sloc (R);
1564 Par_R : constant Node_Id := Parent (R);
1565 New_Body : Node_Id;
1566 New_Decl : Node_Id;
1567 New_Spec : Node_Id;
1569 begin
1570 New_Spec := Build_Overriding_Spec (Par_Prim, R);
1571 New_Decl :=
1572 Make_Subprogram_Declaration (Loc,
1573 Specification => New_Spec);
1575 -- Insert the declaration and the body of the wrapper after
1576 -- type declaration that generates inherited operation. For
1577 -- a null procedure, the declaration implies a null body.
1579 if Nkind (New_Spec) = N_Procedure_Specification
1580 and then Null_Present (New_Spec)
1581 then
1582 Insert_After_And_Analyze (Par_R, New_Decl);
1584 else
1585 -- Build body as wrapper to a call to the already built
1586 -- class-wide clone.
1588 New_Body :=
1589 Build_Class_Wide_Clone_Call
1590 (Loc, Decls, Par_Prim, New_Spec);
1592 Insert_List_After_And_Analyze
1593 (Par_R, New_List (New_Decl, New_Body));
1594 end if;
1595 end;
1597 Needs_Wrapper := False;
1598 end if;
1600 Next_Elmt (Op_Node);
1601 end loop;
1602 end Check_Inherited_Conditions;
1604 ----------------------------
1605 -- Check_Strict_Alignment --
1606 ----------------------------
1608 procedure Check_Strict_Alignment (E : Entity_Id) is
1609 Comp : Entity_Id;
1611 begin
1612 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1613 Set_Strict_Alignment (E);
1615 elsif Is_Array_Type (E) then
1616 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1618 elsif Is_Record_Type (E) then
1619 if Is_Limited_Record (E) then
1620 Set_Strict_Alignment (E);
1621 return;
1622 end if;
1624 Comp := First_Component (E);
1625 while Present (Comp) loop
1626 if not Is_Type (Comp)
1627 and then (Strict_Alignment (Etype (Comp))
1628 or else Is_Aliased (Comp))
1629 then
1630 Set_Strict_Alignment (E);
1631 return;
1632 end if;
1634 Next_Component (Comp);
1635 end loop;
1636 end if;
1637 end Check_Strict_Alignment;
1639 -------------------------
1640 -- Check_Unsigned_Type --
1641 -------------------------
1643 procedure Check_Unsigned_Type (E : Entity_Id) is
1644 Ancestor : Entity_Id;
1645 Lo_Bound : Node_Id;
1646 Btyp : Entity_Id;
1648 begin
1649 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1650 return;
1651 end if;
1653 -- Do not attempt to analyze case where range was in error
1655 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
1656 return;
1657 end if;
1659 -- The situation that is nontrivial is something like:
1661 -- subtype x1 is integer range -10 .. +10;
1662 -- subtype x2 is x1 range 0 .. V1;
1663 -- subtype x3 is x2 range V2 .. V3;
1664 -- subtype x4 is x3 range V4 .. V5;
1666 -- where Vn are variables. Here the base type is signed, but we still
1667 -- know that x4 is unsigned because of the lower bound of x2.
1669 -- The only way to deal with this is to look up the ancestor chain
1671 Ancestor := E;
1672 loop
1673 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1674 return;
1675 end if;
1677 Lo_Bound := Type_Low_Bound (Ancestor);
1679 if Compile_Time_Known_Value (Lo_Bound) then
1680 if Expr_Rep_Value (Lo_Bound) >= 0 then
1681 Set_Is_Unsigned_Type (E, True);
1682 end if;
1684 return;
1686 else
1687 Ancestor := Ancestor_Subtype (Ancestor);
1689 -- If no ancestor had a static lower bound, go to base type
1691 if No (Ancestor) then
1693 -- Note: the reason we still check for a compile time known
1694 -- value for the base type is that at least in the case of
1695 -- generic formals, we can have bounds that fail this test,
1696 -- and there may be other cases in error situations.
1698 Btyp := Base_Type (E);
1700 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1701 return;
1702 end if;
1704 Lo_Bound := Type_Low_Bound (Base_Type (E));
1706 if Compile_Time_Known_Value (Lo_Bound)
1707 and then Expr_Rep_Value (Lo_Bound) >= 0
1708 then
1709 Set_Is_Unsigned_Type (E, True);
1710 end if;
1712 return;
1713 end if;
1714 end if;
1715 end loop;
1716 end Check_Unsigned_Type;
1718 -----------------------------
1719 -- Is_Atomic_VFA_Aggregate --
1720 -----------------------------
1722 function Is_Atomic_VFA_Aggregate (N : Node_Id) return Boolean is
1723 Loc : constant Source_Ptr := Sloc (N);
1724 New_N : Node_Id;
1725 Par : Node_Id;
1726 Temp : Entity_Id;
1727 Typ : Entity_Id;
1729 begin
1730 Par := Parent (N);
1732 -- Array may be qualified, so find outer context
1734 if Nkind (Par) = N_Qualified_Expression then
1735 Par := Parent (Par);
1736 end if;
1738 if not Comes_From_Source (Par) then
1739 return False;
1740 end if;
1742 case Nkind (Par) is
1743 when N_Assignment_Statement =>
1744 Typ := Etype (Name (Par));
1746 if not Is_Atomic_Or_VFA (Typ)
1747 and then not (Is_Entity_Name (Name (Par))
1748 and then Is_Atomic_Or_VFA (Entity (Name (Par))))
1749 then
1750 return False;
1751 end if;
1753 when N_Object_Declaration =>
1754 Typ := Etype (Defining_Identifier (Par));
1756 if not Is_Atomic_Or_VFA (Typ)
1757 and then not Is_Atomic_Or_VFA (Defining_Identifier (Par))
1758 then
1759 return False;
1760 end if;
1762 when others =>
1763 return False;
1764 end case;
1766 Temp := Make_Temporary (Loc, 'T', N);
1767 New_N :=
1768 Make_Object_Declaration (Loc,
1769 Defining_Identifier => Temp,
1770 Object_Definition => New_Occurrence_Of (Typ, Loc),
1771 Expression => Relocate_Node (N));
1772 Insert_Before (Par, New_N);
1773 Analyze (New_N);
1775 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1776 return True;
1777 end Is_Atomic_VFA_Aggregate;
1779 -----------------------------------------------
1780 -- Explode_Initialization_Compound_Statement --
1781 -----------------------------------------------
1783 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
1784 Init_Stmts : constant Node_Id := Initialization_Statements (E);
1786 begin
1787 if Present (Init_Stmts)
1788 and then Nkind (Init_Stmts) = N_Compound_Statement
1789 then
1790 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
1792 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
1793 -- just removing it, because Freeze_All may rely on this particular
1794 -- Node_Id still being present in the enclosing list to know where to
1795 -- stop freezing.
1797 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
1799 Set_Initialization_Statements (E, Empty);
1800 end if;
1801 end Explode_Initialization_Compound_Statement;
1803 ----------------
1804 -- Freeze_All --
1805 ----------------
1807 -- Note: the easy coding for this procedure would be to just build a
1808 -- single list of freeze nodes and then insert them and analyze them
1809 -- all at once. This won't work, because the analysis of earlier freeze
1810 -- nodes may recursively freeze types which would otherwise appear later
1811 -- on in the freeze list. So we must analyze and expand the freeze nodes
1812 -- as they are generated.
1814 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1815 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1816 -- This is the internal recursive routine that does freezing of entities
1817 -- (but NOT the analysis of default expressions, which should not be
1818 -- recursive, we don't want to analyze those till we are sure that ALL
1819 -- the types are frozen).
1821 --------------------
1822 -- Freeze_All_Ent --
1823 --------------------
1825 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
1826 E : Entity_Id;
1827 Flist : List_Id;
1828 Lastn : Node_Id;
1830 procedure Process_Flist;
1831 -- If freeze nodes are present, insert and analyze, and reset cursor
1832 -- for next insertion.
1834 -------------------
1835 -- Process_Flist --
1836 -------------------
1838 procedure Process_Flist is
1839 begin
1840 if Is_Non_Empty_List (Flist) then
1841 Lastn := Next (After);
1842 Insert_List_After_And_Analyze (After, Flist);
1844 if Present (Lastn) then
1845 After := Prev (Lastn);
1846 else
1847 After := Last (List_Containing (After));
1848 end if;
1849 end if;
1850 end Process_Flist;
1852 -- Start of processing for Freeze_All_Ent
1854 begin
1855 E := From;
1856 while Present (E) loop
1858 -- If the entity is an inner package which is not a package
1859 -- renaming, then its entities must be frozen at this point. Note
1860 -- that such entities do NOT get frozen at the end of the nested
1861 -- package itself (only library packages freeze).
1863 -- Same is true for task declarations, where anonymous records
1864 -- created for entry parameters must be frozen.
1866 if Ekind (E) = E_Package
1867 and then No (Renamed_Object (E))
1868 and then not Is_Child_Unit (E)
1869 and then not Is_Frozen (E)
1870 then
1871 Push_Scope (E);
1873 Install_Visible_Declarations (E);
1874 Install_Private_Declarations (E);
1875 Freeze_All (First_Entity (E), After);
1877 End_Package_Scope (E);
1879 if Is_Generic_Instance (E)
1880 and then Has_Delayed_Freeze (E)
1881 then
1882 Set_Has_Delayed_Freeze (E, False);
1883 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1884 end if;
1886 elsif Ekind (E) in Task_Kind
1887 and then Nkind_In (Parent (E), N_Single_Task_Declaration,
1888 N_Task_Type_Declaration)
1889 then
1890 Push_Scope (E);
1891 Freeze_All (First_Entity (E), After);
1892 End_Scope;
1894 -- For a derived tagged type, we must ensure that all the
1895 -- primitive operations of the parent have been frozen, so that
1896 -- their addresses will be in the parent's dispatch table at the
1897 -- point it is inherited.
1899 elsif Ekind (E) = E_Record_Type
1900 and then Is_Tagged_Type (E)
1901 and then Is_Tagged_Type (Etype (E))
1902 and then Is_Derived_Type (E)
1903 then
1904 declare
1905 Prim_List : constant Elist_Id :=
1906 Primitive_Operations (Etype (E));
1908 Prim : Elmt_Id;
1909 Subp : Entity_Id;
1911 begin
1912 Prim := First_Elmt (Prim_List);
1913 while Present (Prim) loop
1914 Subp := Node (Prim);
1916 if Comes_From_Source (Subp)
1917 and then not Is_Frozen (Subp)
1918 then
1919 Flist := Freeze_Entity (Subp, After);
1920 Process_Flist;
1921 end if;
1923 Next_Elmt (Prim);
1924 end loop;
1925 end;
1926 end if;
1928 if not Is_Frozen (E) then
1929 Flist := Freeze_Entity (E, After);
1930 Process_Flist;
1932 -- If already frozen, and there are delayed aspects, this is where
1933 -- we do the visibility check for these aspects (see Sem_Ch13 spec
1934 -- for a description of how we handle aspect visibility).
1936 elsif Has_Delayed_Aspects (E) then
1938 -- Retrieve the visibility to the discriminants in order to
1939 -- analyze properly the aspects.
1941 Push_Scope_And_Install_Discriminants (E);
1943 declare
1944 Ritem : Node_Id;
1946 begin
1947 Ritem := First_Rep_Item (E);
1948 while Present (Ritem) loop
1949 if Nkind (Ritem) = N_Aspect_Specification
1950 and then Entity (Ritem) = E
1951 and then Is_Delayed_Aspect (Ritem)
1952 then
1953 Check_Aspect_At_End_Of_Declarations (Ritem);
1954 end if;
1956 Ritem := Next_Rep_Item (Ritem);
1957 end loop;
1958 end;
1960 Uninstall_Discriminants_And_Pop_Scope (E);
1961 end if;
1963 -- If an incomplete type is still not frozen, this may be a
1964 -- premature freezing because of a body declaration that follows.
1965 -- Indicate where the freezing took place. Freezing will happen
1966 -- if the body comes from source, but not if it is internally
1967 -- generated, for example as the body of a type invariant.
1969 -- If the freezing is caused by the end of the current declarative
1970 -- part, it is a Taft Amendment type, and there is no error.
1972 if not Is_Frozen (E)
1973 and then Ekind (E) = E_Incomplete_Type
1974 then
1975 declare
1976 Bod : constant Node_Id := Next (After);
1978 begin
1979 -- The presence of a body freezes all entities previously
1980 -- declared in the current list of declarations, but this
1981 -- does not apply if the body does not come from source.
1982 -- A type invariant is transformed into a subprogram body
1983 -- which is placed at the end of the private part of the
1984 -- current package, but this body does not freeze incomplete
1985 -- types that may be declared in this private part.
1987 if (Nkind_In (Bod, N_Entry_Body,
1988 N_Package_Body,
1989 N_Protected_Body,
1990 N_Subprogram_Body,
1991 N_Task_Body)
1992 or else Nkind (Bod) in N_Body_Stub)
1993 and then
1994 List_Containing (After) = List_Containing (Parent (E))
1995 and then Comes_From_Source (Bod)
1996 then
1997 Error_Msg_Sloc := Sloc (Next (After));
1998 Error_Msg_NE
1999 ("type& is frozen# before its full declaration",
2000 Parent (E), E);
2001 end if;
2002 end;
2003 end if;
2005 Next_Entity (E);
2006 end loop;
2007 end Freeze_All_Ent;
2009 -- Local variables
2011 Decl : Node_Id;
2012 E : Entity_Id;
2013 Item : Entity_Id;
2015 -- Start of processing for Freeze_All
2017 begin
2018 Freeze_All_Ent (From, After);
2020 -- Now that all types are frozen, we can deal with default expressions
2021 -- that require us to build a default expression functions. This is the
2022 -- point at which such functions are constructed (after all types that
2023 -- might be used in such expressions have been frozen).
2025 -- For subprograms that are renaming_as_body, we create the wrapper
2026 -- bodies as needed.
2028 -- We also add finalization chains to access types whose designated
2029 -- types are controlled. This is normally done when freezing the type,
2030 -- but this misses recursive type definitions where the later members
2031 -- of the recursion introduce controlled components.
2033 -- Loop through entities
2035 E := From;
2036 while Present (E) loop
2037 if Is_Subprogram (E) then
2038 if not Default_Expressions_Processed (E) then
2039 Process_Default_Expressions (E, After);
2040 end if;
2042 if not Has_Completion (E) then
2043 Decl := Unit_Declaration_Node (E);
2045 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
2046 if Error_Posted (Decl) then
2047 Set_Has_Completion (E);
2048 else
2049 Build_And_Analyze_Renamed_Body (Decl, E, After);
2050 end if;
2052 elsif Nkind (Decl) = N_Subprogram_Declaration
2053 and then Present (Corresponding_Body (Decl))
2054 and then
2055 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl))) =
2056 N_Subprogram_Renaming_Declaration
2057 then
2058 Build_And_Analyze_Renamed_Body
2059 (Decl, Corresponding_Body (Decl), After);
2060 end if;
2061 end if;
2063 -- Freeze the default expressions of entries, entry families, and
2064 -- protected subprograms.
2066 elsif Is_Concurrent_Type (E) then
2067 Item := First_Entity (E);
2068 while Present (Item) loop
2069 if (Is_Entry (Item) or else Is_Subprogram (Item))
2070 and then not Default_Expressions_Processed (Item)
2071 then
2072 Process_Default_Expressions (Item, After);
2073 end if;
2075 Next_Entity (Item);
2076 end loop;
2077 end if;
2079 -- Historical note: We used to create a finalization master for an
2080 -- access type whose designated type is not controlled, but contains
2081 -- private controlled compoments. This form of postprocessing is no
2082 -- longer needed because the finalization master is now created when
2083 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
2085 Next_Entity (E);
2086 end loop;
2087 end Freeze_All;
2089 -----------------------
2090 -- Freeze_And_Append --
2091 -----------------------
2093 procedure Freeze_And_Append
2094 (Ent : Entity_Id;
2095 N : Node_Id;
2096 Result : in out List_Id)
2098 L : constant List_Id := Freeze_Entity (Ent, N);
2099 begin
2100 if Is_Non_Empty_List (L) then
2101 if Result = No_List then
2102 Result := L;
2103 else
2104 Append_List (L, Result);
2105 end if;
2106 end if;
2107 end Freeze_And_Append;
2109 -------------------
2110 -- Freeze_Before --
2111 -------------------
2113 procedure Freeze_Before
2114 (N : Node_Id;
2115 T : Entity_Id;
2116 Do_Freeze_Profile : Boolean := True)
2118 -- Freeze T, then insert the generated Freeze nodes before the node N.
2119 -- Flag Freeze_Profile is used when T is an overloadable entity, and
2120 -- indicates whether its profile should be frozen at the same time.
2122 Freeze_Nodes : constant List_Id :=
2123 Freeze_Entity (T, N, Do_Freeze_Profile);
2124 Pack : constant Entity_Id := Scope (T);
2126 begin
2127 if Ekind (T) = E_Function then
2128 Check_Expression_Function (N, T);
2129 end if;
2131 if Is_Non_Empty_List (Freeze_Nodes) then
2133 -- If the entity is a type declared in an inner package, it may be
2134 -- frozen by an outer declaration before the package itself is
2135 -- frozen. Install the package scope to analyze the freeze nodes,
2136 -- which may include generated subprograms such as predicate
2137 -- functions, etc.
2139 if Is_Type (T) and then From_Nested_Package (T) then
2140 Push_Scope (Pack);
2141 Install_Visible_Declarations (Pack);
2142 Install_Private_Declarations (Pack);
2143 Insert_Actions (N, Freeze_Nodes);
2144 End_Package_Scope (Pack);
2146 else
2147 Insert_Actions (N, Freeze_Nodes);
2148 end if;
2149 end if;
2150 end Freeze_Before;
2152 -------------------
2153 -- Freeze_Entity --
2154 -------------------
2156 -- WARNING: This routine manages Ghost regions. Return statements must be
2157 -- replaced by gotos which jump to the end of the routine and restore the
2158 -- Ghost mode.
2160 function Freeze_Entity
2161 (E : Entity_Id;
2162 N : Node_Id;
2163 Do_Freeze_Profile : Boolean := True) return List_Id
2165 Loc : constant Source_Ptr := Sloc (N);
2166 Atype : Entity_Id;
2167 Comp : Entity_Id;
2168 F_Node : Node_Id;
2169 Formal : Entity_Id;
2170 Indx : Node_Id;
2172 Has_Default_Initialization : Boolean := False;
2173 -- This flag gets set to true for a variable with default initialization
2175 Result : List_Id := No_List;
2176 -- List of freezing actions, left at No_List if none
2178 Test_E : Entity_Id := E;
2179 -- This could use a comment ???
2181 procedure Add_To_Result (N : Node_Id);
2182 -- N is a freezing action to be appended to the Result
2184 function After_Last_Declaration return Boolean;
2185 -- If Loc is a freeze_entity that appears after the last declaration
2186 -- in the scope, inhibit error messages on late completion.
2188 procedure Check_Current_Instance (Comp_Decl : Node_Id);
2189 -- Check that an Access or Unchecked_Access attribute with a prefix
2190 -- which is the current instance type can only be applied when the type
2191 -- is limited.
2193 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id);
2194 -- Give a warning for pragma Convention with language C or C++ applied
2195 -- to a discriminated record type. This is suppressed for the unchecked
2196 -- union case, since the whole point in this case is interface C. We
2197 -- also do not generate this within instantiations, since we will have
2198 -- generated a message on the template.
2200 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
2201 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
2202 -- integer literal without an explicit corresponding size clause. The
2203 -- caller has checked that Utype is a modular integer type.
2205 procedure Freeze_Array_Type (Arr : Entity_Id);
2206 -- Freeze array type, including freezing index and component types
2208 procedure Freeze_Object_Declaration (E : Entity_Id);
2209 -- Perform checks and generate freeze node if needed for a constant or
2210 -- variable declared by an object declaration.
2212 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
2213 -- Create Freeze_Generic_Entity nodes for types declared in a generic
2214 -- package. Recurse on inner generic packages.
2216 function Freeze_Profile (E : Entity_Id) return Boolean;
2217 -- Freeze formals and return type of subprogram. If some type in the
2218 -- profile is incomplete and we are in an instance, freezing of the
2219 -- entity will take place elsewhere, and the function returns False.
2221 procedure Freeze_Record_Type (Rec : Entity_Id);
2222 -- Freeze record type, including freezing component types, and freezing
2223 -- primitive operations if this is a tagged type.
2225 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2226 -- Determine whether an arbitrary entity is subject to Boolean aspect
2227 -- Import and its value is specified as True.
2229 procedure Inherit_Freeze_Node
2230 (Fnod : Node_Id;
2231 Typ : Entity_Id);
2232 -- Set type Typ's freeze node to refer to Fnode. This routine ensures
2233 -- that any attributes attached to Typ's original node are preserved.
2235 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2236 -- If E is an entity for an imported subprogram with pre/post-conditions
2237 -- then this procedure will create a wrapper to ensure that proper run-
2238 -- time checking of the pre/postconditions. See body for details.
2240 -------------------
2241 -- Add_To_Result --
2242 -------------------
2244 procedure Add_To_Result (N : Node_Id) is
2245 begin
2246 if No (Result) then
2247 Result := New_List (N);
2248 else
2249 Append (N, Result);
2250 end if;
2251 end Add_To_Result;
2253 ----------------------------
2254 -- After_Last_Declaration --
2255 ----------------------------
2257 function After_Last_Declaration return Boolean is
2258 Spec : constant Node_Id := Parent (Current_Scope);
2260 begin
2261 if Nkind (Spec) = N_Package_Specification then
2262 if Present (Private_Declarations (Spec)) then
2263 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2264 elsif Present (Visible_Declarations (Spec)) then
2265 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2266 else
2267 return False;
2268 end if;
2270 else
2271 return False;
2272 end if;
2273 end After_Last_Declaration;
2275 ----------------------------
2276 -- Check_Current_Instance --
2277 ----------------------------
2279 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2281 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2282 -- Determine whether Typ is compatible with the rules for aliased
2283 -- views of types as defined in RM 3.10 in the various dialects.
2285 function Process (N : Node_Id) return Traverse_Result;
2286 -- Process routine to apply check to given node
2288 -----------------------------
2289 -- Is_Aliased_View_Of_Type --
2290 -----------------------------
2292 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2293 Typ_Decl : constant Node_Id := Parent (Typ);
2295 begin
2296 -- Common case
2298 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2299 and then Limited_Present (Type_Definition (Typ_Decl))
2300 then
2301 return True;
2303 -- The following paragraphs describe what a legal aliased view of
2304 -- a type is in the various dialects of Ada.
2306 -- Ada 95
2308 -- The current instance of a limited type, and a formal parameter
2309 -- or generic formal object of a tagged type.
2311 -- Ada 95 limited type
2312 -- * Type with reserved word "limited"
2313 -- * A protected or task type
2314 -- * A composite type with limited component
2316 elsif Ada_Version <= Ada_95 then
2317 return Is_Limited_Type (Typ);
2319 -- Ada 2005
2321 -- The current instance of a limited tagged type, a protected
2322 -- type, a task type, or a type that has the reserved word
2323 -- "limited" in its full definition ... a formal parameter or
2324 -- generic formal object of a tagged type.
2326 -- Ada 2005 limited type
2327 -- * Type with reserved word "limited", "synchronized", "task"
2328 -- or "protected"
2329 -- * A composite type with limited component
2330 -- * A derived type whose parent is a non-interface limited type
2332 elsif Ada_Version = Ada_2005 then
2333 return
2334 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2335 or else
2336 (Is_Derived_Type (Typ)
2337 and then not Is_Interface (Etype (Typ))
2338 and then Is_Limited_Type (Etype (Typ)));
2340 -- Ada 2012 and beyond
2342 -- The current instance of an immutably limited type ... a formal
2343 -- parameter or generic formal object of a tagged type.
2345 -- Ada 2012 limited type
2346 -- * Type with reserved word "limited", "synchronized", "task"
2347 -- or "protected"
2348 -- * A composite type with limited component
2349 -- * A derived type whose parent is a non-interface limited type
2350 -- * An incomplete view
2352 -- Ada 2012 immutably limited type
2353 -- * Explicitly limited record type
2354 -- * Record extension with "limited" present
2355 -- * Non-formal limited private type that is either tagged
2356 -- or has at least one access discriminant with a default
2357 -- expression
2358 -- * Task type, protected type or synchronized interface
2359 -- * Type derived from immutably limited type
2361 else
2362 return
2363 Is_Immutably_Limited_Type (Typ)
2364 or else Is_Incomplete_Type (Typ);
2365 end if;
2366 end Is_Aliased_View_Of_Type;
2368 -------------
2369 -- Process --
2370 -------------
2372 function Process (N : Node_Id) return Traverse_Result is
2373 begin
2374 case Nkind (N) is
2375 when N_Attribute_Reference =>
2376 if Nam_In (Attribute_Name (N), Name_Access,
2377 Name_Unchecked_Access)
2378 and then Is_Entity_Name (Prefix (N))
2379 and then Is_Type (Entity (Prefix (N)))
2380 and then Entity (Prefix (N)) = E
2381 then
2382 if Ada_Version < Ada_2012 then
2383 Error_Msg_N
2384 ("current instance must be a limited type",
2385 Prefix (N));
2386 else
2387 Error_Msg_N
2388 ("current instance must be an immutably limited "
2389 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2390 end if;
2392 return Abandon;
2394 else
2395 return OK;
2396 end if;
2398 when others =>
2399 return OK;
2400 end case;
2401 end Process;
2403 procedure Traverse is new Traverse_Proc (Process);
2405 -- Local variables
2407 Rec_Type : constant Entity_Id :=
2408 Scope (Defining_Identifier (Comp_Decl));
2410 -- Start of processing for Check_Current_Instance
2412 begin
2413 if not Is_Aliased_View_Of_Type (Rec_Type) then
2414 Traverse (Comp_Decl);
2415 end if;
2416 end Check_Current_Instance;
2418 ---------------------------------
2419 -- Check_Suspicious_Convention --
2420 ---------------------------------
2422 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id) is
2423 begin
2424 if Has_Discriminants (Rec_Type)
2425 and then Is_Base_Type (Rec_Type)
2426 and then not Is_Unchecked_Union (Rec_Type)
2427 and then (Convention (Rec_Type) = Convention_C
2428 or else
2429 Convention (Rec_Type) = Convention_CPP)
2430 and then Comes_From_Source (Rec_Type)
2431 and then not In_Instance
2432 and then not Has_Warnings_Off (Rec_Type)
2433 then
2434 declare
2435 Cprag : constant Node_Id :=
2436 Get_Rep_Pragma (Rec_Type, Name_Convention);
2437 A2 : Node_Id;
2439 begin
2440 if Present (Cprag) then
2441 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
2443 if Convention (Rec_Type) = Convention_C then
2444 Error_Msg_N
2445 ("?x?discriminated record has no direct equivalent in "
2446 & "C", A2);
2447 else
2448 Error_Msg_N
2449 ("?x?discriminated record has no direct equivalent in "
2450 & "C++", A2);
2451 end if;
2453 Error_Msg_NE
2454 ("\?x?use of convention for type& is dubious",
2455 A2, Rec_Type);
2456 end if;
2457 end;
2458 end if;
2459 end Check_Suspicious_Convention;
2461 ------------------------------
2462 -- Check_Suspicious_Modulus --
2463 ------------------------------
2465 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
2466 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
2468 begin
2469 if not Warn_On_Suspicious_Modulus_Value then
2470 return;
2471 end if;
2473 if Nkind (Decl) = N_Full_Type_Declaration then
2474 declare
2475 Tdef : constant Node_Id := Type_Definition (Decl);
2477 begin
2478 if Nkind (Tdef) = N_Modular_Type_Definition then
2479 declare
2480 Modulus : constant Node_Id :=
2481 Original_Node (Expression (Tdef));
2483 begin
2484 if Nkind (Modulus) = N_Integer_Literal then
2485 declare
2486 Modv : constant Uint := Intval (Modulus);
2487 Sizv : constant Uint := RM_Size (Utype);
2489 begin
2490 -- First case, modulus and size are the same. This
2491 -- happens if you have something like mod 32, with
2492 -- an explicit size of 32, this is for sure a case
2493 -- where the warning is given, since it is seems
2494 -- very unlikely that someone would want e.g. a
2495 -- five bit type stored in 32 bits. It is much
2496 -- more likely they wanted a 32-bit type.
2498 if Modv = Sizv then
2499 null;
2501 -- Second case, the modulus is 32 or 64 and no
2502 -- size clause is present. This is a less clear
2503 -- case for giving the warning, but in the case
2504 -- of 32/64 (5-bit or 6-bit types) these seem rare
2505 -- enough that it is a likely error (and in any
2506 -- case using 2**5 or 2**6 in these cases seems
2507 -- clearer. We don't include 8 or 16 here, simply
2508 -- because in practice 3-bit and 4-bit types are
2509 -- more common and too many false positives if
2510 -- we warn in these cases.
2512 elsif not Has_Size_Clause (Utype)
2513 and then (Modv = Uint_32 or else Modv = Uint_64)
2514 then
2515 null;
2517 -- No warning needed
2519 else
2520 return;
2521 end if;
2523 -- If we fall through, give warning
2525 Error_Msg_Uint_1 := Modv;
2526 Error_Msg_N
2527 ("?M?2 '*'*^' may have been intended here",
2528 Modulus);
2529 end;
2530 end if;
2531 end;
2532 end if;
2533 end;
2534 end if;
2535 end Check_Suspicious_Modulus;
2537 -----------------------
2538 -- Freeze_Array_Type --
2539 -----------------------
2541 procedure Freeze_Array_Type (Arr : Entity_Id) is
2542 FS : constant Entity_Id := First_Subtype (Arr);
2543 Ctyp : constant Entity_Id := Component_Type (Arr);
2544 Clause : Entity_Id;
2546 Non_Standard_Enum : Boolean := False;
2547 -- Set true if any of the index types is an enumeration type with a
2548 -- non-standard representation.
2550 begin
2551 Freeze_And_Append (Ctyp, N, Result);
2553 Indx := First_Index (Arr);
2554 while Present (Indx) loop
2555 Freeze_And_Append (Etype (Indx), N, Result);
2557 if Is_Enumeration_Type (Etype (Indx))
2558 and then Has_Non_Standard_Rep (Etype (Indx))
2559 then
2560 Non_Standard_Enum := True;
2561 end if;
2563 Next_Index (Indx);
2564 end loop;
2566 -- Processing that is done only for base types
2568 if Ekind (Arr) = E_Array_Type then
2570 -- Deal with default setting of reverse storage order
2572 Set_SSO_From_Default (Arr);
2574 -- Propagate flags for component type
2576 if Is_Controlled (Component_Type (Arr))
2577 or else Has_Controlled_Component (Ctyp)
2578 then
2579 Set_Has_Controlled_Component (Arr);
2580 end if;
2582 if Has_Unchecked_Union (Component_Type (Arr)) then
2583 Set_Has_Unchecked_Union (Arr);
2584 end if;
2586 -- The array type requires its own invariant procedure in order to
2587 -- verify the component invariant over all elements. In GNATprove
2588 -- mode, the component invariants are checked by other means. They
2589 -- should not be added to the array type invariant procedure, so
2590 -- that the procedure can be used to check the array type
2591 -- invariants if any.
2593 if Has_Invariants (Component_Type (Arr))
2594 and then not GNATprove_Mode
2595 then
2596 Set_Has_Own_Invariants (Arr);
2598 -- The array type is an implementation base type. Propagate the
2599 -- same property to the first subtype.
2601 if Is_Itype (Arr) then
2602 Set_Has_Own_Invariants (First_Subtype (Arr));
2603 end if;
2604 end if;
2606 -- Warn for pragma Pack overriding foreign convention
2608 if Has_Foreign_Convention (Ctyp)
2609 and then Has_Pragma_Pack (Arr)
2610 then
2611 declare
2612 CN : constant Name_Id :=
2613 Get_Convention_Name (Convention (Ctyp));
2614 PP : constant Node_Id :=
2615 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
2616 begin
2617 if Present (PP) then
2618 Error_Msg_Name_1 := CN;
2619 Error_Msg_Sloc := Sloc (Arr);
2620 Error_Msg_N
2621 ("pragma Pack affects convention % components #??", PP);
2622 Error_Msg_Name_1 := CN;
2623 Error_Msg_N
2624 ("\array components may not have % compatible "
2625 & "representation??", PP);
2626 end if;
2627 end;
2628 end if;
2630 -- If packing was requested or if the component size was
2631 -- set explicitly, then see if bit packing is required. This
2632 -- processing is only done for base types, since all of the
2633 -- representation aspects involved are type-related.
2635 -- This is not just an optimization, if we start processing the
2636 -- subtypes, they interfere with the settings on the base type
2637 -- (this is because Is_Packed has a slightly different meaning
2638 -- before and after freezing).
2640 declare
2641 Csiz : Uint;
2642 Esiz : Uint;
2644 begin
2645 if (Is_Packed (Arr) or else Has_Pragma_Pack (Arr))
2646 and then Known_Static_RM_Size (Ctyp)
2647 and then not Has_Component_Size_Clause (Arr)
2648 then
2649 Csiz := UI_Max (RM_Size (Ctyp), 1);
2651 elsif Known_Component_Size (Arr) then
2652 Csiz := Component_Size (Arr);
2654 elsif not Known_Static_Esize (Ctyp) then
2655 Csiz := Uint_0;
2657 else
2658 Esiz := Esize (Ctyp);
2660 -- We can set the component size if it is less than 16,
2661 -- rounding it up to the next storage unit size.
2663 if Esiz <= 8 then
2664 Csiz := Uint_8;
2665 elsif Esiz <= 16 then
2666 Csiz := Uint_16;
2667 else
2668 Csiz := Uint_0;
2669 end if;
2671 -- Set component size up to match alignment if it would
2672 -- otherwise be less than the alignment. This deals with
2673 -- cases of types whose alignment exceeds their size (the
2674 -- padded type cases).
2676 if Csiz /= 0 then
2677 declare
2678 A : constant Uint := Alignment_In_Bits (Ctyp);
2679 begin
2680 if Csiz < A then
2681 Csiz := A;
2682 end if;
2683 end;
2684 end if;
2685 end if;
2687 -- Case of component size that may result in bit packing
2689 if 1 <= Csiz and then Csiz <= 64 then
2690 declare
2691 Ent : constant Entity_Id :=
2692 First_Subtype (Arr);
2693 Pack_Pragma : constant Node_Id :=
2694 Get_Rep_Pragma (Ent, Name_Pack);
2695 Comp_Size_C : constant Node_Id :=
2696 Get_Attribute_Definition_Clause
2697 (Ent, Attribute_Component_Size);
2699 begin
2700 -- Warn if we have pack and component size so that the
2701 -- pack is ignored.
2703 -- Note: here we must check for the presence of a
2704 -- component size before checking for a Pack pragma to
2705 -- deal with the case where the array type is a derived
2706 -- type whose parent is currently private.
2708 if Present (Comp_Size_C)
2709 and then Has_Pragma_Pack (Ent)
2710 and then Warn_On_Redundant_Constructs
2711 then
2712 Error_Msg_Sloc := Sloc (Comp_Size_C);
2713 Error_Msg_NE
2714 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
2715 Error_Msg_N
2716 ("\?r?explicit component size given#!", Pack_Pragma);
2717 Set_Is_Packed (Base_Type (Ent), False);
2718 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
2719 end if;
2721 -- Set component size if not already set by a component
2722 -- size clause.
2724 if not Present (Comp_Size_C) then
2725 Set_Component_Size (Arr, Csiz);
2726 end if;
2728 -- Check for base type of 8, 16, 32 bits, where an
2729 -- unsigned subtype has a length one less than the
2730 -- base type (e.g. Natural subtype of Integer).
2732 -- In such cases, if a component size was not set
2733 -- explicitly, then generate a warning.
2735 if Has_Pragma_Pack (Arr)
2736 and then not Present (Comp_Size_C)
2737 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2738 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2739 then
2740 Error_Msg_Uint_1 := Csiz;
2742 if Present (Pack_Pragma) then
2743 Error_Msg_N
2744 ("??pragma Pack causes component size to be ^!",
2745 Pack_Pragma);
2746 Error_Msg_N
2747 ("\??use Component_Size to set desired value!",
2748 Pack_Pragma);
2749 end if;
2750 end if;
2752 -- Bit packing is never needed for 8, 16, 32, 64
2754 if Addressable (Csiz) then
2756 -- If the Esize of the component is known and equal to
2757 -- the component size then even packing is not needed.
2759 if Known_Static_Esize (Component_Type (Arr))
2760 and then Esize (Component_Type (Arr)) = Csiz
2761 then
2762 -- Here the array was requested to be packed, but
2763 -- the packing request had no effect whatsoever,
2764 -- so flag Is_Packed is reset.
2766 -- Note: semantically this means that we lose track
2767 -- of the fact that a derived type inherited pragma
2768 -- Pack that was non-effective, but that is fine.
2770 -- We regard a Pack pragma as a request to set a
2771 -- representation characteristic, and this request
2772 -- may be ignored.
2774 Set_Is_Packed (Base_Type (Arr), False);
2775 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
2776 else
2777 Set_Is_Packed (Base_Type (Arr), True);
2778 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2779 end if;
2781 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2783 -- Bit packing is not needed for multiples of the storage
2784 -- unit if the type is composite because the back end can
2785 -- byte pack composite types.
2787 elsif Csiz mod System_Storage_Unit = 0
2788 and then Is_Composite_Type (Ctyp)
2789 then
2790 Set_Is_Packed (Base_Type (Arr), True);
2791 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2792 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2794 -- In all other cases, bit packing is needed
2796 else
2797 Set_Is_Packed (Base_Type (Arr), True);
2798 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2799 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
2800 end if;
2801 end;
2802 end if;
2803 end;
2805 -- Check for Aliased or Atomic_Components/Atomic/VFA with
2806 -- unsuitable packing or explicit component size clause given.
2808 if (Has_Aliased_Components (Arr)
2809 or else Has_Atomic_Components (Arr)
2810 or else Is_Atomic_Or_VFA (Ctyp))
2811 and then
2812 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2813 then
2814 Alias_Atomic_Check : declare
2816 procedure Complain_CS (T : String);
2817 -- Outputs error messages for incorrect CS clause or pragma
2818 -- Pack for aliased or atomic/VFA components (T is "aliased"
2819 -- or "atomic/vfa");
2821 -----------------
2822 -- Complain_CS --
2823 -----------------
2825 procedure Complain_CS (T : String) is
2826 begin
2827 if Has_Component_Size_Clause (Arr) then
2828 Clause :=
2829 Get_Attribute_Definition_Clause
2830 (FS, Attribute_Component_Size);
2832 Error_Msg_N
2833 ("incorrect component size for "
2834 & T & " components", Clause);
2835 Error_Msg_Uint_1 := Esize (Ctyp);
2836 Error_Msg_N
2837 ("\only allowed value is^", Clause);
2839 else
2840 Error_Msg_N
2841 ("cannot pack " & T & " components",
2842 Get_Rep_Pragma (FS, Name_Pack));
2843 end if;
2844 end Complain_CS;
2846 -- Start of processing for Alias_Atomic_Check
2848 begin
2849 -- If object size of component type isn't known, we cannot
2850 -- be sure so we defer to the back end.
2852 if not Known_Static_Esize (Ctyp) then
2853 null;
2855 -- Case where component size has no effect. First check for
2856 -- object size of component type multiple of the storage
2857 -- unit size.
2859 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2861 -- OK in both packing case and component size case if RM
2862 -- size is known and static and same as the object size.
2864 and then
2865 ((Known_Static_RM_Size (Ctyp)
2866 and then Esize (Ctyp) = RM_Size (Ctyp))
2868 -- Or if we have an explicit component size clause and
2869 -- the component size and object size are equal.
2871 or else
2872 (Has_Component_Size_Clause (Arr)
2873 and then Component_Size (Arr) = Esize (Ctyp)))
2874 then
2875 null;
2877 elsif Has_Aliased_Components (Arr) then
2878 Complain_CS ("aliased");
2880 elsif Has_Atomic_Components (Arr)
2881 or else Is_Atomic (Ctyp)
2882 then
2883 Complain_CS ("atomic");
2885 elsif Is_Volatile_Full_Access (Ctyp) then
2886 Complain_CS ("volatile full access");
2887 end if;
2888 end Alias_Atomic_Check;
2889 end if;
2891 -- Check for Independent_Components/Independent with unsuitable
2892 -- packing or explicit component size clause given.
2894 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
2895 and then
2896 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2897 then
2898 begin
2899 -- If object size of component type isn't known, we cannot
2900 -- be sure so we defer to the back end.
2902 if not Known_Static_Esize (Ctyp) then
2903 null;
2905 -- Case where component size has no effect. First check for
2906 -- object size of component type multiple of the storage
2907 -- unit size.
2909 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2911 -- OK in both packing case and component size case if RM
2912 -- size is known and multiple of the storage unit size.
2914 and then
2915 ((Known_Static_RM_Size (Ctyp)
2916 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
2918 -- Or if we have an explicit component size clause and
2919 -- the component size is larger than the object size.
2921 or else
2922 (Has_Component_Size_Clause (Arr)
2923 and then Component_Size (Arr) >= Esize (Ctyp)))
2924 then
2925 null;
2927 else
2928 if Has_Component_Size_Clause (Arr) then
2929 Clause :=
2930 Get_Attribute_Definition_Clause
2931 (FS, Attribute_Component_Size);
2933 Error_Msg_N
2934 ("incorrect component size for "
2935 & "independent components", Clause);
2936 Error_Msg_Uint_1 := Esize (Ctyp);
2937 Error_Msg_N
2938 ("\minimum allowed is^", Clause);
2940 else
2941 Error_Msg_N
2942 ("cannot pack independent components",
2943 Get_Rep_Pragma (FS, Name_Pack));
2944 end if;
2945 end if;
2946 end;
2947 end if;
2949 -- Warn for case of atomic type
2951 Clause := Get_Rep_Pragma (FS, Name_Atomic);
2953 if Present (Clause)
2954 and then not Addressable (Component_Size (FS))
2955 then
2956 Error_Msg_NE
2957 ("non-atomic components of type& may not be "
2958 & "accessible by separate tasks??", Clause, Arr);
2960 if Has_Component_Size_Clause (Arr) then
2961 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2962 (FS, Attribute_Component_Size));
2963 Error_Msg_N ("\because of component size clause#??", Clause);
2965 elsif Has_Pragma_Pack (Arr) then
2966 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2967 Error_Msg_N ("\because of pragma Pack#??", Clause);
2968 end if;
2969 end if;
2971 -- Check for scalar storage order
2973 declare
2974 Dummy : Boolean;
2975 begin
2976 Check_Component_Storage_Order
2977 (Encl_Type => Arr,
2978 Comp => Empty,
2979 ADC => Get_Attribute_Definition_Clause
2980 (First_Subtype (Arr),
2981 Attribute_Scalar_Storage_Order),
2982 Comp_ADC_Present => Dummy);
2983 end;
2985 -- Processing that is done only for subtypes
2987 else
2988 -- Acquire alignment from base type
2990 if Unknown_Alignment (Arr) then
2991 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
2992 Adjust_Esize_Alignment (Arr);
2993 end if;
2994 end if;
2996 -- Specific checks for bit-packed arrays
2998 if Is_Bit_Packed_Array (Arr) then
3000 -- Check number of elements for bit-packed arrays that come from
3001 -- source and have compile time known ranges. The bit-packed
3002 -- arrays circuitry does not support arrays with more than
3003 -- Integer'Last + 1 elements, and when this restriction is
3004 -- violated, causes incorrect data access.
3006 -- For the case where this is not compile time known, a run-time
3007 -- check should be generated???
3009 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
3010 declare
3011 Elmts : Uint;
3012 Index : Node_Id;
3013 Ilen : Node_Id;
3014 Ityp : Entity_Id;
3016 begin
3017 Elmts := Uint_1;
3018 Index := First_Index (Arr);
3019 while Present (Index) loop
3020 Ityp := Etype (Index);
3022 -- Never generate an error if any index is of a generic
3023 -- type. We will check this in instances.
3025 if Is_Generic_Type (Ityp) then
3026 Elmts := Uint_0;
3027 exit;
3028 end if;
3030 Ilen :=
3031 Make_Attribute_Reference (Loc,
3032 Prefix => New_Occurrence_Of (Ityp, Loc),
3033 Attribute_Name => Name_Range_Length);
3034 Analyze_And_Resolve (Ilen);
3036 -- No attempt is made to check number of elements if not
3037 -- compile time known.
3039 if Nkind (Ilen) /= N_Integer_Literal then
3040 Elmts := Uint_0;
3041 exit;
3042 end if;
3044 Elmts := Elmts * Intval (Ilen);
3045 Next_Index (Index);
3046 end loop;
3048 if Elmts > Intval (High_Bound
3049 (Scalar_Range (Standard_Integer))) + 1
3050 then
3051 Error_Msg_N
3052 ("bit packed array type may not have "
3053 & "more than Integer''Last+1 elements", Arr);
3054 end if;
3055 end;
3056 end if;
3058 -- Check size
3060 if Known_RM_Size (Arr) then
3061 declare
3062 SizC : constant Node_Id := Size_Clause (Arr);
3063 Discard : Boolean;
3065 begin
3066 -- It is not clear if it is possible to have no size clause
3067 -- at this stage, but it is not worth worrying about. Post
3068 -- error on the entity name in the size clause if present,
3069 -- else on the type entity itself.
3071 if Present (SizC) then
3072 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
3073 else
3074 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
3075 end if;
3076 end;
3077 end if;
3078 end if;
3080 -- If any of the index types was an enumeration type with a non-
3081 -- standard rep clause, then we indicate that the array type is
3082 -- always packed (even if it is not bit-packed).
3084 if Non_Standard_Enum then
3085 Set_Has_Non_Standard_Rep (Base_Type (Arr));
3086 Set_Is_Packed (Base_Type (Arr));
3087 end if;
3089 Set_Component_Alignment_If_Not_Set (Arr);
3091 -- If the array is packed and bit-packed or packed to eliminate holes
3092 -- in the non-contiguous enumeration index types, we must create the
3093 -- packed array type to be used to actually implement the type. This
3094 -- is only needed for real array types (not for string literal types,
3095 -- since they are present only for the front end).
3097 if Is_Packed (Arr)
3098 and then (Is_Bit_Packed_Array (Arr) or else Non_Standard_Enum)
3099 and then Ekind (Arr) /= E_String_Literal_Subtype
3100 then
3101 Create_Packed_Array_Impl_Type (Arr);
3102 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
3104 -- Make sure that we have the necessary routines to implement the
3105 -- packing, and complain now if not. Note that we only test this
3106 -- for constrained array types.
3108 if Is_Constrained (Arr)
3109 and then Is_Bit_Packed_Array (Arr)
3110 and then Present (Packed_Array_Impl_Type (Arr))
3111 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
3112 then
3113 declare
3114 CS : constant Uint := Component_Size (Arr);
3115 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
3117 begin
3118 if RE /= RE_Null
3119 and then not RTE_Available (RE)
3120 then
3121 Error_Msg_CRT
3122 ("packing of " & UI_Image (CS) & "-bit components",
3123 First_Subtype (Etype (Arr)));
3125 -- Cancel the packing
3127 Set_Is_Packed (Base_Type (Arr), False);
3128 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3129 Set_Packed_Array_Impl_Type (Arr, Empty);
3130 goto Skip_Packed;
3131 end if;
3132 end;
3133 end if;
3135 -- Size information of packed array type is copied to the array
3136 -- type, since this is really the representation. But do not
3137 -- override explicit existing size values. If the ancestor subtype
3138 -- is constrained the Packed_Array_Impl_Type will be inherited
3139 -- from it, but the size may have been provided already, and
3140 -- must not be overridden either.
3142 if not Has_Size_Clause (Arr)
3143 and then
3144 (No (Ancestor_Subtype (Arr))
3145 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
3146 then
3147 Set_Esize (Arr, Esize (Packed_Array_Impl_Type (Arr)));
3148 Set_RM_Size (Arr, RM_Size (Packed_Array_Impl_Type (Arr)));
3149 end if;
3151 if not Has_Alignment_Clause (Arr) then
3152 Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
3153 end if;
3154 end if;
3156 <<Skip_Packed>>
3158 -- For non-packed arrays set the alignment of the array to the
3159 -- alignment of the component type if it is unknown. Skip this
3160 -- in atomic/VFA case (atomic/VFA arrays may need larger alignments).
3162 if not Is_Packed (Arr)
3163 and then Unknown_Alignment (Arr)
3164 and then Known_Alignment (Ctyp)
3165 and then Known_Static_Component_Size (Arr)
3166 and then Known_Static_Esize (Ctyp)
3167 and then Esize (Ctyp) = Component_Size (Arr)
3168 and then not Is_Atomic_Or_VFA (Arr)
3169 then
3170 Set_Alignment (Arr, Alignment (Component_Type (Arr)));
3171 end if;
3173 -- A Ghost type cannot have a component of protected or task type
3174 -- (SPARK RM 6.9(19)).
3176 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
3177 Error_Msg_N
3178 ("ghost array type & cannot have concurrent component type",
3179 Arr);
3180 end if;
3181 end Freeze_Array_Type;
3183 -------------------------------
3184 -- Freeze_Object_Declaration --
3185 -------------------------------
3187 procedure Freeze_Object_Declaration (E : Entity_Id) is
3188 begin
3189 -- Abstract type allowed only for C++ imported variables or constants
3191 -- Note: we inhibit this check for objects that do not come from
3192 -- source because there is at least one case (the expansion of
3193 -- x'Class'Input where x is abstract) where we legitimately
3194 -- generate an abstract object.
3196 if Is_Abstract_Type (Etype (E))
3197 and then Comes_From_Source (Parent (E))
3198 and then not (Is_Imported (E) and then Is_CPP_Class (Etype (E)))
3199 then
3200 Error_Msg_N ("type of object cannot be abstract",
3201 Object_Definition (Parent (E)));
3203 if Is_CPP_Class (Etype (E)) then
3204 Error_Msg_NE
3205 ("\} may need a cpp_constructor",
3206 Object_Definition (Parent (E)), Etype (E));
3208 elsif Present (Expression (Parent (E))) then
3209 Error_Msg_N -- CODEFIX
3210 ("\maybe a class-wide type was meant",
3211 Object_Definition (Parent (E)));
3212 end if;
3213 end if;
3215 -- For object created by object declaration, perform required
3216 -- categorization (preelaborate and pure) checks. Defer these
3217 -- checks to freeze time since pragma Import inhibits default
3218 -- initialization and thus pragma Import affects these checks.
3220 Validate_Object_Declaration (Declaration_Node (E));
3222 -- If there is an address clause, check that it is valid
3223 -- and if need be move initialization to the freeze node.
3225 Check_Address_Clause (E);
3227 -- Similar processing is needed for aspects that may affect
3228 -- object layout, like Alignment, if there is an initialization
3229 -- expression. We don't do this if there is a pragma Linker_Section,
3230 -- because it would prevent the back end from statically initializing
3231 -- the object; we don't want elaboration code in that case.
3233 if Has_Delayed_Aspects (E)
3234 and then Expander_Active
3235 and then Is_Array_Type (Etype (E))
3236 and then Present (Expression (Parent (E)))
3237 and then No (Linker_Section_Pragma (E))
3238 then
3239 declare
3240 Decl : constant Node_Id := Parent (E);
3241 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
3243 begin
3245 -- Capture initialization value at point of declaration, and
3246 -- make explicit assignment legal, because object may be a
3247 -- constant.
3249 Remove_Side_Effects (Expression (Decl));
3250 Set_Assignment_OK (Lhs);
3252 -- Move initialization to freeze actions.
3254 Append_Freeze_Action (E,
3255 Make_Assignment_Statement (Loc,
3256 Name => Lhs,
3257 Expression => Expression (Decl)));
3259 Set_No_Initialization (Decl);
3260 -- Set_Is_Frozen (E, False);
3261 end;
3262 end if;
3264 -- Reset Is_True_Constant for non-constant aliased object. We
3265 -- consider that the fact that a non-constant object is aliased may
3266 -- indicate that some funny business is going on, e.g. an aliased
3267 -- object is passed by reference to a procedure which captures the
3268 -- address of the object, which is later used to assign a new value,
3269 -- even though the compiler thinks that it is not modified. Such
3270 -- code is highly dubious, but we choose to make it "work" for
3271 -- non-constant aliased objects.
3273 -- Note that we used to do this for all aliased objects, whether or
3274 -- not constant, but this caused anomalies down the line because we
3275 -- ended up with static objects that were not Is_True_Constant. Not
3276 -- resetting Is_True_Constant for (aliased) constant objects ensures
3277 -- that this anomaly never occurs.
3279 -- However, we don't do that for internal entities. We figure that if
3280 -- we deliberately set Is_True_Constant for an internal entity, e.g.
3281 -- a dispatch table entry, then we mean it.
3283 if Ekind (E) /= E_Constant
3284 and then (Is_Aliased (E) or else Is_Aliased (Etype (E)))
3285 and then not Is_Internal_Name (Chars (E))
3286 then
3287 Set_Is_True_Constant (E, False);
3288 end if;
3290 -- If the object needs any kind of default initialization, an error
3291 -- must be issued if No_Default_Initialization applies. The check
3292 -- doesn't apply to imported objects, which are not ever default
3293 -- initialized, and is why the check is deferred until freezing, at
3294 -- which point we know if Import applies. Deferred constants are also
3295 -- exempted from this test because their completion is explicit, or
3296 -- through an import pragma.
3298 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
3299 null;
3301 elsif Comes_From_Source (E)
3302 and then not Is_Imported (E)
3303 and then not Has_Init_Expression (Declaration_Node (E))
3304 and then
3305 ((Has_Non_Null_Base_Init_Proc (Etype (E))
3306 and then not No_Initialization (Declaration_Node (E))
3307 and then not Initialization_Suppressed (Etype (E)))
3308 or else
3309 (Needs_Simple_Initialization (Etype (E))
3310 and then not Is_Internal (E)))
3311 then
3312 Has_Default_Initialization := True;
3313 Check_Restriction
3314 (No_Default_Initialization, Declaration_Node (E));
3315 end if;
3317 -- Check that a Thread_Local_Storage variable does not have
3318 -- default initialization, and any explicit initialization must
3319 -- either be the null constant or a static constant.
3321 if Has_Pragma_Thread_Local_Storage (E) then
3322 declare
3323 Decl : constant Node_Id := Declaration_Node (E);
3324 begin
3325 if Has_Default_Initialization
3326 or else
3327 (Has_Init_Expression (Decl)
3328 and then
3329 (No (Expression (Decl))
3330 or else not
3331 (Is_OK_Static_Expression (Expression (Decl))
3332 or else Nkind (Expression (Decl)) = N_Null)))
3333 then
3334 Error_Msg_NE
3335 ("Thread_Local_Storage variable& is "
3336 & "improperly initialized", Decl, E);
3337 Error_Msg_NE
3338 ("\only allowed initialization is explicit "
3339 & "NULL or static expression", Decl, E);
3340 end if;
3341 end;
3342 end if;
3344 -- For imported objects, set Is_Public unless there is also an
3345 -- address clause, which means that there is no external symbol
3346 -- needed for the Import (Is_Public may still be set for other
3347 -- unrelated reasons). Note that we delayed this processing
3348 -- till freeze time so that we can be sure not to set the flag
3349 -- if there is an address clause. If there is such a clause,
3350 -- then the only purpose of the Import pragma is to suppress
3351 -- implicit initialization.
3353 if Is_Imported (E) and then No (Address_Clause (E)) then
3354 Set_Is_Public (E);
3355 end if;
3357 -- For source objects that are not Imported and are library
3358 -- level, if no linker section pragma was given inherit the
3359 -- appropriate linker section from the corresponding type.
3361 if Comes_From_Source (E)
3362 and then not Is_Imported (E)
3363 and then Is_Library_Level_Entity (E)
3364 and then No (Linker_Section_Pragma (E))
3365 then
3366 Set_Linker_Section_Pragma
3367 (E, Linker_Section_Pragma (Etype (E)));
3368 end if;
3370 -- For convention C objects of an enumeration type, warn if the
3371 -- size is not integer size and no explicit size given. Skip
3372 -- warning for Boolean, and Character, assume programmer expects
3373 -- 8-bit sizes for these cases.
3375 if (Convention (E) = Convention_C
3376 or else
3377 Convention (E) = Convention_CPP)
3378 and then Is_Enumeration_Type (Etype (E))
3379 and then not Is_Character_Type (Etype (E))
3380 and then not Is_Boolean_Type (Etype (E))
3381 and then Esize (Etype (E)) < Standard_Integer_Size
3382 and then not Has_Size_Clause (E)
3383 then
3384 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
3385 Error_Msg_N
3386 ("??convention C enumeration object has size less than ^", E);
3387 Error_Msg_N ("\??use explicit size clause to set size", E);
3388 end if;
3389 end Freeze_Object_Declaration;
3391 -----------------------------
3392 -- Freeze_Generic_Entities --
3393 -----------------------------
3395 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
3396 E : Entity_Id;
3397 F : Node_Id;
3398 Flist : List_Id;
3400 begin
3401 Flist := New_List;
3402 E := First_Entity (Pack);
3403 while Present (E) loop
3404 if Is_Type (E) and then not Is_Generic_Type (E) then
3405 F := Make_Freeze_Generic_Entity (Sloc (Pack));
3406 Set_Entity (F, E);
3407 Append_To (Flist, F);
3409 elsif Ekind (E) = E_Generic_Package then
3410 Append_List_To (Flist, Freeze_Generic_Entities (E));
3411 end if;
3413 Next_Entity (E);
3414 end loop;
3416 return Flist;
3417 end Freeze_Generic_Entities;
3419 --------------------
3420 -- Freeze_Profile --
3421 --------------------
3423 function Freeze_Profile (E : Entity_Id) return Boolean is
3424 F_Type : Entity_Id;
3425 R_Type : Entity_Id;
3426 Warn_Node : Node_Id;
3428 begin
3429 -- Loop through formals
3431 Formal := First_Formal (E);
3432 while Present (Formal) loop
3433 F_Type := Etype (Formal);
3435 -- AI05-0151: incomplete types can appear in a profile. By the
3436 -- time the entity is frozen, the full view must be available,
3437 -- unless it is a limited view.
3439 if Is_Incomplete_Type (F_Type)
3440 and then Present (Full_View (F_Type))
3441 and then not From_Limited_With (F_Type)
3442 then
3443 F_Type := Full_View (F_Type);
3444 Set_Etype (Formal, F_Type);
3445 end if;
3447 if not From_Limited_With (F_Type) then
3448 Freeze_And_Append (F_Type, N, Result);
3449 end if;
3451 if Is_Private_Type (F_Type)
3452 and then Is_Private_Type (Base_Type (F_Type))
3453 and then No (Full_View (Base_Type (F_Type)))
3454 and then not Is_Generic_Type (F_Type)
3455 and then not Is_Derived_Type (F_Type)
3456 then
3457 -- If the type of a formal is incomplete, subprogram is being
3458 -- frozen prematurely. Within an instance (but not within a
3459 -- wrapper package) this is an artifact of our need to regard
3460 -- the end of an instantiation as a freeze point. Otherwise it
3461 -- is a definite error.
3463 if In_Instance then
3464 Set_Is_Frozen (E, False);
3465 Result := No_List;
3466 return False;
3468 elsif not After_Last_Declaration
3469 and then not Freezing_Library_Level_Tagged_Type
3470 then
3471 Error_Msg_Node_1 := F_Type;
3472 Error_Msg
3473 ("type & must be fully defined before this point", Loc);
3474 end if;
3475 end if;
3477 -- Check suspicious parameter for C function. These tests apply
3478 -- only to exported/imported subprograms.
3480 if Warn_On_Export_Import
3481 and then Comes_From_Source (E)
3482 and then (Convention (E) = Convention_C
3483 or else
3484 Convention (E) = Convention_CPP)
3485 and then (Is_Imported (E) or else Is_Exported (E))
3486 and then Convention (E) /= Convention (Formal)
3487 and then not Has_Warnings_Off (E)
3488 and then not Has_Warnings_Off (F_Type)
3489 and then not Has_Warnings_Off (Formal)
3490 then
3491 -- Qualify mention of formals with subprogram name
3493 Error_Msg_Qual_Level := 1;
3495 -- Check suspicious use of fat C pointer
3497 if Is_Access_Type (F_Type)
3498 and then Esize (F_Type) > Ttypes.System_Address_Size
3499 then
3500 Error_Msg_N
3501 ("?x?type of & does not correspond to C pointer!", Formal);
3503 -- Check suspicious return of boolean
3505 elsif Root_Type (F_Type) = Standard_Boolean
3506 and then Convention (F_Type) = Convention_Ada
3507 and then not Has_Warnings_Off (F_Type)
3508 and then not Has_Size_Clause (F_Type)
3509 then
3510 Error_Msg_N
3511 ("& is an 8-bit Ada Boolean?x?", Formal);
3512 Error_Msg_N
3513 ("\use appropriate corresponding type in C "
3514 & "(e.g. char)?x?", Formal);
3516 -- Check suspicious tagged type
3518 elsif (Is_Tagged_Type (F_Type)
3519 or else
3520 (Is_Access_Type (F_Type)
3521 and then Is_Tagged_Type (Designated_Type (F_Type))))
3522 and then Convention (E) = Convention_C
3523 then
3524 Error_Msg_N
3525 ("?x?& involves a tagged type which does not "
3526 & "correspond to any C type!", Formal);
3528 -- Check wrong convention subprogram pointer
3530 elsif Ekind (F_Type) = E_Access_Subprogram_Type
3531 and then not Has_Foreign_Convention (F_Type)
3532 then
3533 Error_Msg_N
3534 ("?x?subprogram pointer & should "
3535 & "have foreign convention!", Formal);
3536 Error_Msg_Sloc := Sloc (F_Type);
3537 Error_Msg_NE
3538 ("\?x?add Convention pragma to declaration of &#",
3539 Formal, F_Type);
3540 end if;
3542 -- Turn off name qualification after message output
3544 Error_Msg_Qual_Level := 0;
3545 end if;
3547 -- Check for unconstrained array in exported foreign convention
3548 -- case.
3550 if Has_Foreign_Convention (E)
3551 and then not Is_Imported (E)
3552 and then Is_Array_Type (F_Type)
3553 and then not Is_Constrained (F_Type)
3554 and then Warn_On_Export_Import
3555 then
3556 Error_Msg_Qual_Level := 1;
3558 -- If this is an inherited operation, place the warning on
3559 -- the derived type declaration, rather than on the original
3560 -- subprogram.
3562 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
3563 then
3564 Warn_Node := Parent (E);
3566 if Formal = First_Formal (E) then
3567 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
3568 end if;
3569 else
3570 Warn_Node := Formal;
3571 end if;
3573 Error_Msg_NE ("?x?type of argument& is unconstrained array",
3574 Warn_Node, Formal);
3575 Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
3576 Warn_Node, Formal);
3577 Error_Msg_Qual_Level := 0;
3578 end if;
3580 if not From_Limited_With (F_Type) then
3581 if Is_Access_Type (F_Type) then
3582 F_Type := Designated_Type (F_Type);
3583 end if;
3585 -- If the formal is an anonymous_access_to_subprogram
3586 -- freeze the subprogram type as well, to prevent
3587 -- scope anomalies in gigi, because there is no other
3588 -- clear point at which it could be frozen.
3590 if Is_Itype (Etype (Formal))
3591 and then Ekind (F_Type) = E_Subprogram_Type
3592 then
3593 Freeze_And_Append (F_Type, N, Result);
3594 end if;
3595 end if;
3597 Next_Formal (Formal);
3598 end loop;
3600 -- Case of function: similar checks on return type
3602 if Ekind (E) = E_Function then
3604 -- Freeze return type
3606 R_Type := Etype (E);
3608 -- AI05-0151: the return type may have been incomplete at the
3609 -- point of declaration. Replace it with the full view, unless the
3610 -- current type is a limited view. In that case the full view is
3611 -- in a different unit, and gigi finds the non-limited view after
3612 -- the other unit is elaborated.
3614 if Ekind (R_Type) = E_Incomplete_Type
3615 and then Present (Full_View (R_Type))
3616 and then not From_Limited_With (R_Type)
3617 then
3618 R_Type := Full_View (R_Type);
3619 Set_Etype (E, R_Type);
3620 end if;
3622 Freeze_And_Append (R_Type, N, Result);
3624 -- Check suspicious return type for C function
3626 if Warn_On_Export_Import
3627 and then (Convention (E) = Convention_C
3628 or else
3629 Convention (E) = Convention_CPP)
3630 and then (Is_Imported (E) or else Is_Exported (E))
3631 then
3632 -- Check suspicious return of fat C pointer
3634 if Is_Access_Type (R_Type)
3635 and then Esize (R_Type) > Ttypes.System_Address_Size
3636 and then not Has_Warnings_Off (E)
3637 and then not Has_Warnings_Off (R_Type)
3638 then
3639 Error_Msg_N
3640 ("?x?return type of& does not correspond to C pointer!",
3643 -- Check suspicious return of boolean
3645 elsif Root_Type (R_Type) = Standard_Boolean
3646 and then Convention (R_Type) = Convention_Ada
3647 and then not Has_Warnings_Off (E)
3648 and then not Has_Warnings_Off (R_Type)
3649 and then not Has_Size_Clause (R_Type)
3650 then
3651 declare
3652 N : constant Node_Id :=
3653 Result_Definition (Declaration_Node (E));
3654 begin
3655 Error_Msg_NE
3656 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
3657 Error_Msg_NE
3658 ("\use appropriate corresponding type in C "
3659 & "(e.g. char)?x?", N, E);
3660 end;
3662 -- Check suspicious return tagged type
3664 elsif (Is_Tagged_Type (R_Type)
3665 or else (Is_Access_Type (R_Type)
3666 and then
3667 Is_Tagged_Type
3668 (Designated_Type (R_Type))))
3669 and then Convention (E) = Convention_C
3670 and then not Has_Warnings_Off (E)
3671 and then not Has_Warnings_Off (R_Type)
3672 then
3673 Error_Msg_N ("?x?return type of & does not "
3674 & "correspond to C type!", E);
3676 -- Check return of wrong convention subprogram pointer
3678 elsif Ekind (R_Type) = E_Access_Subprogram_Type
3679 and then not Has_Foreign_Convention (R_Type)
3680 and then not Has_Warnings_Off (E)
3681 and then not Has_Warnings_Off (R_Type)
3682 then
3683 Error_Msg_N ("?x?& should return a foreign "
3684 & "convention subprogram pointer", E);
3685 Error_Msg_Sloc := Sloc (R_Type);
3686 Error_Msg_NE
3687 ("\?x?add Convention pragma to declaration of& #",
3688 E, R_Type);
3689 end if;
3690 end if;
3692 -- Give warning for suspicious return of a result of an
3693 -- unconstrained array type in a foreign convention function.
3695 if Has_Foreign_Convention (E)
3697 -- We are looking for a return of unconstrained array
3699 and then Is_Array_Type (R_Type)
3700 and then not Is_Constrained (R_Type)
3702 -- Exclude imported routines, the warning does not belong on
3703 -- the import, but rather on the routine definition.
3705 and then not Is_Imported (E)
3707 -- Check that general warning is enabled, and that it is not
3708 -- suppressed for this particular case.
3710 and then Warn_On_Export_Import
3711 and then not Has_Warnings_Off (E)
3712 and then not Has_Warnings_Off (R_Type)
3713 then
3714 Error_Msg_N
3715 ("?x?foreign convention function& should not return "
3716 & "unconstrained array!", E);
3717 end if;
3718 end if;
3720 -- Check suspicious use of Import in pure unit (cases where the RM
3721 -- allows calls to be omitted).
3723 if Is_Imported (E)
3725 -- It might be suspicious if the compilation unit has the Pure
3726 -- aspect/pragma.
3728 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
3730 -- The RM allows omission of calls only in the case of
3731 -- library-level subprograms (see RM-10.2.1(18)).
3733 and then Is_Library_Level_Entity (E)
3735 -- Ignore internally generated entity. This happens in some cases
3736 -- of subprograms in specs, where we generate an implied body.
3738 and then Comes_From_Source (Import_Pragma (E))
3740 -- Assume run-time knows what it is doing
3742 and then not GNAT_Mode
3744 -- Assume explicit Pure_Function means import is pure
3746 and then not Has_Pragma_Pure_Function (E)
3748 -- Don't need warning in relaxed semantics mode
3750 and then not Relaxed_RM_Semantics
3752 -- Assume convention Intrinsic is OK, since this is specialized.
3753 -- This deals with the DEC unit current_exception.ads
3755 and then Convention (E) /= Convention_Intrinsic
3757 -- Assume that ASM interface knows what it is doing. This deals
3758 -- with e.g. unsigned.ads in the AAMP back end.
3760 and then Convention (E) /= Convention_Assembler
3761 then
3762 Error_Msg_N
3763 ("pragma Import in Pure unit??", Import_Pragma (E));
3764 Error_Msg_NE
3765 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
3766 Import_Pragma (E), E);
3767 end if;
3769 return True;
3770 end Freeze_Profile;
3772 ------------------------
3773 -- Freeze_Record_Type --
3774 ------------------------
3776 procedure Freeze_Record_Type (Rec : Entity_Id) is
3777 ADC : Node_Id;
3778 Comp : Entity_Id;
3779 IR : Node_Id;
3780 Prev : Entity_Id;
3782 Junk : Boolean;
3783 pragma Warnings (Off, Junk);
3785 Aliased_Component : Boolean := False;
3786 -- Set True if we find at least one component which is aliased. This
3787 -- is used to prevent Implicit_Packing of the record, since packing
3788 -- cannot modify the size of alignment of an aliased component.
3790 All_Elem_Components : Boolean := True;
3791 -- True if all components are of a type whose underlying type is
3792 -- elementary.
3794 All_Sized_Components : Boolean := True;
3795 -- True if all components have a known RM_Size
3797 All_Storage_Unit_Components : Boolean := True;
3798 -- True if all components have an RM_Size that is a multiple of the
3799 -- storage unit.
3801 Elem_Component_Total_Esize : Uint := Uint_0;
3802 -- Accumulates total Esize values of all elementary components. Used
3803 -- for processing of Implicit_Packing.
3805 Placed_Component : Boolean := False;
3806 -- Set True if we find at least one component with a component
3807 -- clause (used to warn about useless Bit_Order pragmas, and also
3808 -- to detect cases where Implicit_Packing may have an effect).
3810 Rec_Pushed : Boolean := False;
3811 -- Set True if the record type scope Rec has been pushed on the scope
3812 -- stack. Needed for the analysis of delayed aspects specified to the
3813 -- components of Rec.
3815 Sized_Component_Total_RM_Size : Uint := Uint_0;
3816 -- Accumulates total RM_Size values of all sized components. Used
3817 -- for processing of Implicit_Packing.
3819 Sized_Component_Total_Round_RM_Size : Uint := Uint_0;
3820 -- Accumulates total RM_Size values of all sized components, rounded
3821 -- individually to a multiple of the storage unit.
3823 SSO_ADC : Node_Id;
3824 -- Scalar_Storage_Order attribute definition clause for the record
3826 SSO_ADC_Component : Boolean := False;
3827 -- Set True if we find at least one component whose type has a
3828 -- Scalar_Storage_Order attribute definition clause.
3830 Unplaced_Component : Boolean := False;
3831 -- Set True if we find at least one component with no component
3832 -- clause (used to warn about useless Pack pragmas).
3834 function Check_Allocator (N : Node_Id) return Node_Id;
3835 -- If N is an allocator, possibly wrapped in one or more level of
3836 -- qualified expression(s), return the inner allocator node, else
3837 -- return Empty.
3839 procedure Check_Itype (Typ : Entity_Id);
3840 -- If the component subtype is an access to a constrained subtype of
3841 -- an already frozen type, make the subtype frozen as well. It might
3842 -- otherwise be frozen in the wrong scope, and a freeze node on
3843 -- subtype has no effect. Similarly, if the component subtype is a
3844 -- regular (not protected) access to subprogram, set the anonymous
3845 -- subprogram type to frozen as well, to prevent an out-of-scope
3846 -- freeze node at some eventual point of call. Protected operations
3847 -- are handled elsewhere.
3849 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3850 -- Make sure that all types mentioned in Discrete_Choices of the
3851 -- variants referenceed by the Variant_Part VP are frozen. This is
3852 -- a recursive routine to deal with nested variants.
3854 ---------------------
3855 -- Check_Allocator --
3856 ---------------------
3858 function Check_Allocator (N : Node_Id) return Node_Id is
3859 Inner : Node_Id;
3860 begin
3861 Inner := N;
3862 loop
3863 if Nkind (Inner) = N_Allocator then
3864 return Inner;
3865 elsif Nkind (Inner) = N_Qualified_Expression then
3866 Inner := Expression (Inner);
3867 else
3868 return Empty;
3869 end if;
3870 end loop;
3871 end Check_Allocator;
3873 -----------------
3874 -- Check_Itype --
3875 -----------------
3877 procedure Check_Itype (Typ : Entity_Id) is
3878 Desig : constant Entity_Id := Designated_Type (Typ);
3880 begin
3881 if not Is_Frozen (Desig)
3882 and then Is_Frozen (Base_Type (Desig))
3883 then
3884 Set_Is_Frozen (Desig);
3886 -- In addition, add an Itype_Reference to ensure that the
3887 -- access subtype is elaborated early enough. This cannot be
3888 -- done if the subtype may depend on discriminants.
3890 if Ekind (Comp) = E_Component
3891 and then Is_Itype (Etype (Comp))
3892 and then not Has_Discriminants (Rec)
3893 then
3894 IR := Make_Itype_Reference (Sloc (Comp));
3895 Set_Itype (IR, Desig);
3896 Add_To_Result (IR);
3897 end if;
3899 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
3900 and then Convention (Desig) /= Convention_Protected
3901 then
3902 Set_Is_Frozen (Desig);
3903 end if;
3904 end Check_Itype;
3906 ------------------------------------
3907 -- Freeze_Choices_In_Variant_Part --
3908 ------------------------------------
3910 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
3911 pragma Assert (Nkind (VP) = N_Variant_Part);
3913 Variant : Node_Id;
3914 Choice : Node_Id;
3915 CL : Node_Id;
3917 begin
3918 -- Loop through variants
3920 Variant := First_Non_Pragma (Variants (VP));
3921 while Present (Variant) loop
3923 -- Loop through choices, checking that all types are frozen
3925 Choice := First_Non_Pragma (Discrete_Choices (Variant));
3926 while Present (Choice) loop
3927 if Nkind (Choice) in N_Has_Etype
3928 and then Present (Etype (Choice))
3929 then
3930 Freeze_And_Append (Etype (Choice), N, Result);
3931 end if;
3933 Next_Non_Pragma (Choice);
3934 end loop;
3936 -- Check for nested variant part to process
3938 CL := Component_List (Variant);
3940 if not Null_Present (CL) then
3941 if Present (Variant_Part (CL)) then
3942 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
3943 end if;
3944 end if;
3946 Next_Non_Pragma (Variant);
3947 end loop;
3948 end Freeze_Choices_In_Variant_Part;
3950 -- Start of processing for Freeze_Record_Type
3952 begin
3953 -- Deal with delayed aspect specifications for components. The
3954 -- analysis of the aspect is required to be delayed to the freeze
3955 -- point, thus we analyze the pragma or attribute definition
3956 -- clause in the tree at this point. We also analyze the aspect
3957 -- specification node at the freeze point when the aspect doesn't
3958 -- correspond to pragma/attribute definition clause.
3960 Comp := First_Entity (Rec);
3961 while Present (Comp) loop
3962 if Ekind (Comp) = E_Component
3963 and then Has_Delayed_Aspects (Comp)
3964 then
3965 if not Rec_Pushed then
3966 Push_Scope (Rec);
3967 Rec_Pushed := True;
3969 -- The visibility to the discriminants must be restored in
3970 -- order to properly analyze the aspects.
3972 if Has_Discriminants (Rec) then
3973 Install_Discriminants (Rec);
3974 end if;
3975 end if;
3977 Analyze_Aspects_At_Freeze_Point (Comp);
3978 end if;
3980 Next_Entity (Comp);
3981 end loop;
3983 -- Pop the scope if Rec scope has been pushed on the scope stack
3984 -- during the delayed aspect analysis process.
3986 if Rec_Pushed then
3987 if Has_Discriminants (Rec) then
3988 Uninstall_Discriminants (Rec);
3989 end if;
3991 Pop_Scope;
3992 end if;
3994 -- Freeze components and embedded subtypes
3996 Comp := First_Entity (Rec);
3997 Prev := Empty;
3998 while Present (Comp) loop
3999 if Is_Aliased (Comp) then
4000 Aliased_Component := True;
4001 end if;
4003 -- Handle the component and discriminant case
4005 if Ekind_In (Comp, E_Component, E_Discriminant) then
4006 declare
4007 CC : constant Node_Id := Component_Clause (Comp);
4009 begin
4010 -- Freezing a record type freezes the type of each of its
4011 -- components. However, if the type of the component is
4012 -- part of this record, we do not want or need a separate
4013 -- Freeze_Node. Note that Is_Itype is wrong because that's
4014 -- also set in private type cases. We also can't check for
4015 -- the Scope being exactly Rec because of private types and
4016 -- record extensions.
4018 if Is_Itype (Etype (Comp))
4019 and then Is_Record_Type (Underlying_Type
4020 (Scope (Etype (Comp))))
4021 then
4022 Undelay_Type (Etype (Comp));
4023 end if;
4025 Freeze_And_Append (Etype (Comp), N, Result);
4027 -- Warn for pragma Pack overriding foreign convention
4029 if Has_Foreign_Convention (Etype (Comp))
4030 and then Has_Pragma_Pack (Rec)
4032 -- Don't warn for aliased components, since override
4033 -- cannot happen in that case.
4035 and then not Is_Aliased (Comp)
4036 then
4037 declare
4038 CN : constant Name_Id :=
4039 Get_Convention_Name (Convention (Etype (Comp)));
4040 PP : constant Node_Id :=
4041 Get_Pragma (Rec, Pragma_Pack);
4042 begin
4043 if Present (PP) then
4044 Error_Msg_Name_1 := CN;
4045 Error_Msg_Sloc := Sloc (Comp);
4046 Error_Msg_N
4047 ("pragma Pack affects convention % component#??",
4048 PP);
4049 Error_Msg_Name_1 := CN;
4050 Error_Msg_NE
4051 ("\component & may not have % compatible "
4052 & "representation??", PP, Comp);
4053 end if;
4054 end;
4055 end if;
4057 -- Check for error of component clause given for variable
4058 -- sized type. We have to delay this test till this point,
4059 -- since the component type has to be frozen for us to know
4060 -- if it is variable length.
4062 if Present (CC) then
4063 Placed_Component := True;
4065 -- We omit this test in a generic context, it will be
4066 -- applied at instantiation time.
4068 if Inside_A_Generic then
4069 null;
4071 -- Also omit this test in CodePeer mode, since we do not
4072 -- have sufficient info on size and rep clauses.
4074 elsif CodePeer_Mode then
4075 null;
4077 -- Omit check if component has a generic type. This can
4078 -- happen in an instantiation within a generic in ASIS
4079 -- mode, where we force freeze actions without full
4080 -- expansion.
4082 elsif Is_Generic_Type (Etype (Comp)) then
4083 null;
4085 -- Do the check
4087 elsif not
4088 Size_Known_At_Compile_Time
4089 (Underlying_Type (Etype (Comp)))
4090 then
4091 Error_Msg_N
4092 ("component clause not allowed for variable " &
4093 "length component", CC);
4094 end if;
4096 else
4097 Unplaced_Component := True;
4098 end if;
4100 -- Case of component requires byte alignment
4102 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
4104 -- Set the enclosing record to also require byte align
4106 Set_Must_Be_On_Byte_Boundary (Rec);
4108 -- Check for component clause that is inconsistent with
4109 -- the required byte boundary alignment.
4111 if Present (CC)
4112 and then Normalized_First_Bit (Comp) mod
4113 System_Storage_Unit /= 0
4114 then
4115 Error_Msg_N
4116 ("component & must be byte aligned",
4117 Component_Name (Component_Clause (Comp)));
4118 end if;
4119 end if;
4120 end;
4121 end if;
4123 -- Gather data for possible Implicit_Packing later. Note that at
4124 -- this stage we might be dealing with a real component, or with
4125 -- an implicit subtype declaration.
4127 if Known_Static_RM_Size (Etype (Comp)) then
4128 declare
4129 Comp_Type : constant Entity_Id := Etype (Comp);
4130 Comp_Size : constant Uint := RM_Size (Comp_Type);
4131 SSU : constant Int := Ttypes.System_Storage_Unit;
4133 begin
4134 Sized_Component_Total_RM_Size :=
4135 Sized_Component_Total_RM_Size + Comp_Size;
4137 Sized_Component_Total_Round_RM_Size :=
4138 Sized_Component_Total_Round_RM_Size +
4139 (Comp_Size + SSU - 1) / SSU * SSU;
4141 if Present (Underlying_Type (Comp_Type))
4142 and then Is_Elementary_Type (Underlying_Type (Comp_Type))
4143 then
4144 Elem_Component_Total_Esize :=
4145 Elem_Component_Total_Esize + Esize (Comp_Type);
4146 else
4147 All_Elem_Components := False;
4149 if Comp_Size mod SSU /= 0 then
4150 All_Storage_Unit_Components := False;
4151 end if;
4152 end if;
4153 end;
4154 else
4155 All_Sized_Components := False;
4156 end if;
4158 -- If the component is an Itype with Delayed_Freeze and is either
4159 -- a record or array subtype and its base type has not yet been
4160 -- frozen, we must remove this from the entity list of this record
4161 -- and put it on the entity list of the scope of its base type.
4162 -- Note that we know that this is not the type of a component
4163 -- since we cleared Has_Delayed_Freeze for it in the previous
4164 -- loop. Thus this must be the Designated_Type of an access type,
4165 -- which is the type of a component.
4167 if Is_Itype (Comp)
4168 and then Is_Type (Scope (Comp))
4169 and then Is_Composite_Type (Comp)
4170 and then Base_Type (Comp) /= Comp
4171 and then Has_Delayed_Freeze (Comp)
4172 and then not Is_Frozen (Base_Type (Comp))
4173 then
4174 declare
4175 Will_Be_Frozen : Boolean := False;
4176 S : Entity_Id;
4178 begin
4179 -- We have a difficult case to handle here. Suppose Rec is
4180 -- subtype being defined in a subprogram that's created as
4181 -- part of the freezing of Rec'Base. In that case, we know
4182 -- that Comp'Base must have already been frozen by the time
4183 -- we get to elaborate this because Gigi doesn't elaborate
4184 -- any bodies until it has elaborated all of the declarative
4185 -- part. But Is_Frozen will not be set at this point because
4186 -- we are processing code in lexical order.
4188 -- We detect this case by going up the Scope chain of Rec
4189 -- and seeing if we have a subprogram scope before reaching
4190 -- the top of the scope chain or that of Comp'Base. If we
4191 -- do, then mark that Comp'Base will actually be frozen. If
4192 -- so, we merely undelay it.
4194 S := Scope (Rec);
4195 while Present (S) loop
4196 if Is_Subprogram (S) then
4197 Will_Be_Frozen := True;
4198 exit;
4199 elsif S = Scope (Base_Type (Comp)) then
4200 exit;
4201 end if;
4203 S := Scope (S);
4204 end loop;
4206 if Will_Be_Frozen then
4207 Undelay_Type (Comp);
4209 else
4210 if Present (Prev) then
4211 Set_Next_Entity (Prev, Next_Entity (Comp));
4212 else
4213 Set_First_Entity (Rec, Next_Entity (Comp));
4214 end if;
4216 -- Insert in entity list of scope of base type (which
4217 -- must be an enclosing scope, because still unfrozen).
4219 Append_Entity (Comp, Scope (Base_Type (Comp)));
4220 end if;
4221 end;
4223 -- If the component is an access type with an allocator as default
4224 -- value, the designated type will be frozen by the corresponding
4225 -- expression in init_proc. In order to place the freeze node for
4226 -- the designated type before that for the current record type,
4227 -- freeze it now.
4229 -- Same process if the component is an array of access types,
4230 -- initialized with an aggregate. If the designated type is
4231 -- private, it cannot contain allocators, and it is premature
4232 -- to freeze the type, so we check for this as well.
4234 elsif Is_Access_Type (Etype (Comp))
4235 and then Present (Parent (Comp))
4236 and then Present (Expression (Parent (Comp)))
4237 then
4238 declare
4239 Alloc : constant Node_Id :=
4240 Check_Allocator (Expression (Parent (Comp)));
4242 begin
4243 if Present (Alloc) then
4245 -- If component is pointer to a class-wide type, freeze
4246 -- the specific type in the expression being allocated.
4247 -- The expression may be a subtype indication, in which
4248 -- case freeze the subtype mark.
4250 if Is_Class_Wide_Type
4251 (Designated_Type (Etype (Comp)))
4252 then
4253 if Is_Entity_Name (Expression (Alloc)) then
4254 Freeze_And_Append
4255 (Entity (Expression (Alloc)), N, Result);
4257 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
4258 then
4259 Freeze_And_Append
4260 (Entity (Subtype_Mark (Expression (Alloc))),
4261 N, Result);
4262 end if;
4264 elsif Is_Itype (Designated_Type (Etype (Comp))) then
4265 Check_Itype (Etype (Comp));
4267 else
4268 Freeze_And_Append
4269 (Designated_Type (Etype (Comp)), N, Result);
4270 end if;
4271 end if;
4272 end;
4274 elsif Is_Access_Type (Etype (Comp))
4275 and then Is_Itype (Designated_Type (Etype (Comp)))
4276 then
4277 Check_Itype (Etype (Comp));
4279 -- Freeze the designated type when initializing a component with
4280 -- an aggregate in case the aggregate contains allocators.
4282 -- type T is ...;
4283 -- type T_Ptr is access all T;
4284 -- type T_Array is array ... of T_Ptr;
4286 -- type Rec is record
4287 -- Comp : T_Array := (others => ...);
4288 -- end record;
4290 elsif Is_Array_Type (Etype (Comp))
4291 and then Is_Access_Type (Component_Type (Etype (Comp)))
4292 then
4293 declare
4294 Comp_Par : constant Node_Id := Parent (Comp);
4295 Desig_Typ : constant Entity_Id :=
4296 Designated_Type
4297 (Component_Type (Etype (Comp)));
4299 begin
4300 -- The only case when this sort of freezing is not done is
4301 -- when the designated type is class-wide and the root type
4302 -- is the record owning the component. This scenario results
4303 -- in a circularity because the class-wide type requires
4304 -- primitives that have not been created yet as the root
4305 -- type is in the process of being frozen.
4307 -- type Rec is tagged;
4308 -- type Rec_Ptr is access all Rec'Class;
4309 -- type Rec_Array is array ... of Rec_Ptr;
4311 -- type Rec is record
4312 -- Comp : Rec_Array := (others => ...);
4313 -- end record;
4315 if Is_Class_Wide_Type (Desig_Typ)
4316 and then Root_Type (Desig_Typ) = Rec
4317 then
4318 null;
4320 elsif Is_Fully_Defined (Desig_Typ)
4321 and then Present (Comp_Par)
4322 and then Nkind (Comp_Par) = N_Component_Declaration
4323 and then Present (Expression (Comp_Par))
4324 and then Nkind (Expression (Comp_Par)) = N_Aggregate
4325 then
4326 Freeze_And_Append (Desig_Typ, N, Result);
4327 end if;
4328 end;
4329 end if;
4331 Prev := Comp;
4332 Next_Entity (Comp);
4333 end loop;
4335 SSO_ADC :=
4336 Get_Attribute_Definition_Clause
4337 (Rec, Attribute_Scalar_Storage_Order);
4339 -- If the record type has Complex_Representation, then it is treated
4340 -- as a scalar in the back end so the storage order is irrelevant.
4342 if Has_Complex_Representation (Rec) then
4343 if Present (SSO_ADC) then
4344 Error_Msg_N
4345 ("??storage order has no effect with Complex_Representation",
4346 SSO_ADC);
4347 end if;
4349 else
4350 -- Deal with default setting of reverse storage order
4352 Set_SSO_From_Default (Rec);
4354 -- Check consistent attribute setting on component types
4356 declare
4357 Comp_ADC_Present : Boolean;
4358 begin
4359 Comp := First_Component (Rec);
4360 while Present (Comp) loop
4361 Check_Component_Storage_Order
4362 (Encl_Type => Rec,
4363 Comp => Comp,
4364 ADC => SSO_ADC,
4365 Comp_ADC_Present => Comp_ADC_Present);
4366 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
4367 Next_Component (Comp);
4368 end loop;
4369 end;
4371 -- Now deal with reverse storage order/bit order issues
4373 if Present (SSO_ADC) then
4375 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
4376 -- if the former is specified.
4378 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
4380 -- Note: report error on Rec, not on SSO_ADC, as ADC may
4381 -- apply to some ancestor type.
4383 Error_Msg_Sloc := Sloc (SSO_ADC);
4384 Error_Msg_N
4385 ("scalar storage order for& specified# inconsistent with "
4386 & "bit order", Rec);
4387 end if;
4389 -- Warn if there is a Scalar_Storage_Order attribute definition
4390 -- clause but no component clause, no component that itself has
4391 -- such an attribute definition, and no pragma Pack.
4393 if not (Placed_Component
4394 or else
4395 SSO_ADC_Component
4396 or else
4397 Is_Packed (Rec))
4398 then
4399 Error_Msg_N
4400 ("??scalar storage order specified but no component "
4401 & "clause", SSO_ADC);
4402 end if;
4403 end if;
4404 end if;
4406 -- Deal with Bit_Order aspect
4408 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
4410 if Present (ADC) and then Base_Type (Rec) = Rec then
4411 if not (Placed_Component
4412 or else Present (SSO_ADC)
4413 or else Is_Packed (Rec))
4414 then
4415 -- Warn if clause has no effect when no component clause is
4416 -- present, but suppress warning if the Bit_Order is required
4417 -- due to the presence of a Scalar_Storage_Order attribute.
4419 Error_Msg_N
4420 ("??bit order specification has no effect", ADC);
4421 Error_Msg_N
4422 ("\??since no component clauses were specified", ADC);
4424 -- Here is where we do the processing to adjust component clauses
4425 -- for reversed bit order, when not using reverse SSO. If an error
4426 -- has been reported on Rec already (such as SSO incompatible with
4427 -- bit order), don't bother adjusting as this may generate extra
4428 -- noise.
4430 elsif Reverse_Bit_Order (Rec)
4431 and then not Reverse_Storage_Order (Rec)
4432 and then not Error_Posted (Rec)
4433 then
4434 Adjust_Record_For_Reverse_Bit_Order (Rec);
4436 -- Case where we have both an explicit Bit_Order and the same
4437 -- Scalar_Storage_Order: leave record untouched, the back-end
4438 -- will take care of required layout conversions.
4440 else
4441 null;
4443 end if;
4444 end if;
4446 -- Complete error checking on record representation clause (e.g.
4447 -- overlap of components). This is called after adjusting the
4448 -- record for reverse bit order.
4450 declare
4451 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
4452 begin
4453 if Present (RRC) then
4454 Check_Record_Representation_Clause (RRC);
4455 end if;
4456 end;
4458 -- Check for useless pragma Pack when all components placed. We only
4459 -- do this check for record types, not subtypes, since a subtype may
4460 -- have all its components placed, and it still makes perfectly good
4461 -- sense to pack other subtypes or the parent type. We do not give
4462 -- this warning if Optimize_Alignment is set to Space, since the
4463 -- pragma Pack does have an effect in this case (it always resets
4464 -- the alignment to one).
4466 if Ekind (Rec) = E_Record_Type
4467 and then Is_Packed (Rec)
4468 and then not Unplaced_Component
4469 and then Optimize_Alignment /= 'S'
4470 then
4471 -- Reset packed status. Probably not necessary, but we do it so
4472 -- that there is no chance of the back end doing something strange
4473 -- with this redundant indication of packing.
4475 Set_Is_Packed (Rec, False);
4477 -- Give warning if redundant constructs warnings on
4479 if Warn_On_Redundant_Constructs then
4480 Error_Msg_N -- CODEFIX
4481 ("??pragma Pack has no effect, no unplaced components",
4482 Get_Rep_Pragma (Rec, Name_Pack));
4483 end if;
4484 end if;
4486 -- If this is the record corresponding to a remote type, freeze the
4487 -- remote type here since that is what we are semantically freezing.
4488 -- This prevents the freeze node for that type in an inner scope.
4490 if Ekind (Rec) = E_Record_Type then
4491 if Present (Corresponding_Remote_Type (Rec)) then
4492 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
4493 end if;
4495 -- Check for controlled components, unchecked unions, and type
4496 -- invariants.
4498 Comp := First_Component (Rec);
4499 while Present (Comp) loop
4501 -- Do not set Has_Controlled_Component on a class-wide
4502 -- equivalent type. See Make_CW_Equivalent_Type.
4504 if not Is_Class_Wide_Equivalent_Type (Rec)
4505 and then
4506 (Has_Controlled_Component (Etype (Comp))
4507 or else
4508 (Chars (Comp) /= Name_uParent
4509 and then Is_Controlled (Etype (Comp)))
4510 or else
4511 (Is_Protected_Type (Etype (Comp))
4512 and then
4513 Present (Corresponding_Record_Type (Etype (Comp)))
4514 and then
4515 Has_Controlled_Component
4516 (Corresponding_Record_Type (Etype (Comp)))))
4517 then
4518 Set_Has_Controlled_Component (Rec);
4519 end if;
4521 if Has_Unchecked_Union (Etype (Comp)) then
4522 Set_Has_Unchecked_Union (Rec);
4523 end if;
4525 -- The record type requires its own invariant procedure in
4526 -- order to verify the invariant of each individual component.
4527 -- Do not consider internal components such as _parent because
4528 -- parent class-wide invariants are always inherited.
4529 -- In GNATprove mode, the component invariants are checked by
4530 -- other means. They should not be added to the record type
4531 -- invariant procedure, so that the procedure can be used to
4532 -- check the recordy type invariants if any.
4534 if Comes_From_Source (Comp)
4535 and then Has_Invariants (Etype (Comp))
4536 and then not GNATprove_Mode
4537 then
4538 Set_Has_Own_Invariants (Rec);
4539 end if;
4541 -- Scan component declaration for likely misuses of current
4542 -- instance, either in a constraint or a default expression.
4544 if Has_Per_Object_Constraint (Comp) then
4545 Check_Current_Instance (Parent (Comp));
4546 end if;
4548 Next_Component (Comp);
4549 end loop;
4550 end if;
4552 -- Enforce the restriction that access attributes with a current
4553 -- instance prefix can only apply to limited types. This comment
4554 -- is floating here, but does not seem to belong here???
4556 -- Set component alignment if not otherwise already set
4558 Set_Component_Alignment_If_Not_Set (Rec);
4560 -- For first subtypes, check if there are any fixed-point fields with
4561 -- component clauses, where we must check the size. This is not done
4562 -- till the freeze point since for fixed-point types, we do not know
4563 -- the size until the type is frozen. Similar processing applies to
4564 -- bit-packed arrays.
4566 if Is_First_Subtype (Rec) then
4567 Comp := First_Component (Rec);
4568 while Present (Comp) loop
4569 if Present (Component_Clause (Comp))
4570 and then (Is_Fixed_Point_Type (Etype (Comp))
4571 or else Is_Bit_Packed_Array (Etype (Comp)))
4572 then
4573 Check_Size
4574 (Component_Name (Component_Clause (Comp)),
4575 Etype (Comp),
4576 Esize (Comp),
4577 Junk);
4578 end if;
4580 Next_Component (Comp);
4581 end loop;
4582 end if;
4584 -- See if Size is too small as is (and implicit packing might help)
4586 if not Is_Packed (Rec)
4588 -- No implicit packing if even one component is explicitly placed
4590 and then not Placed_Component
4592 -- Or even one component is aliased
4594 and then not Aliased_Component
4596 -- Must have size clause and all sized components
4598 and then Has_Size_Clause (Rec)
4599 and then All_Sized_Components
4601 -- Do not try implicit packing on records with discriminants, too
4602 -- complicated, especially in the variant record case.
4604 and then not Has_Discriminants (Rec)
4606 -- We want to implicitly pack if the specified size of the record
4607 -- is less than the sum of the object sizes (no point in packing
4608 -- if this is not the case), if we can compute it, i.e. if we have
4609 -- only elementary components. Otherwise, we have at least one
4610 -- composite component and we want to implicitly pack only if bit
4611 -- packing is required for it, as we are sure in this case that
4612 -- the back end cannot do the expected layout without packing.
4614 and then
4615 ((All_Elem_Components
4616 and then RM_Size (Rec) < Elem_Component_Total_Esize)
4617 or else
4618 (not All_Elem_Components
4619 and then not All_Storage_Unit_Components
4620 and then RM_Size (Rec) < Sized_Component_Total_Round_RM_Size))
4622 -- And the total RM size cannot be greater than the specified size
4623 -- since otherwise packing will not get us where we have to be.
4625 and then Sized_Component_Total_RM_Size <= RM_Size (Rec)
4627 -- Never do implicit packing in CodePeer or SPARK modes since
4628 -- we don't do any packing in these modes, since this generates
4629 -- over-complex code that confuses static analysis, and in
4630 -- general, neither CodePeer not GNATprove care about the
4631 -- internal representation of objects.
4633 and then not (CodePeer_Mode or GNATprove_Mode)
4634 then
4635 -- If implicit packing enabled, do it
4637 if Implicit_Packing then
4638 Set_Is_Packed (Rec);
4640 -- Otherwise flag the size clause
4642 else
4643 declare
4644 Sz : constant Node_Id := Size_Clause (Rec);
4645 begin
4646 Error_Msg_NE -- CODEFIX
4647 ("size given for& too small", Sz, Rec);
4648 Error_Msg_N -- CODEFIX
4649 ("\use explicit pragma Pack "
4650 & "or use pragma Implicit_Packing", Sz);
4651 end;
4652 end if;
4653 end if;
4655 -- The following checks are relevant only when SPARK_Mode is on as
4656 -- they are not standard Ada legality rules.
4658 if SPARK_Mode = On then
4660 -- A discriminated type cannot be effectively volatile
4661 -- (SPARK RM 7.1.3(5)).
4663 if Is_Effectively_Volatile (Rec) then
4664 if Has_Discriminants (Rec) then
4665 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
4666 end if;
4668 -- A non-effectively volatile record type cannot contain
4669 -- effectively volatile components (SPARK RM 7.1.3(6)).
4671 else
4672 Comp := First_Component (Rec);
4673 while Present (Comp) loop
4674 if Comes_From_Source (Comp)
4675 and then Is_Effectively_Volatile (Etype (Comp))
4676 then
4677 Error_Msg_Name_1 := Chars (Rec);
4678 Error_Msg_N
4679 ("component & of non-volatile type % cannot be "
4680 & "volatile", Comp);
4681 end if;
4683 Next_Component (Comp);
4684 end loop;
4685 end if;
4687 -- A type which does not yield a synchronized object cannot have
4688 -- a component that yields a synchronized object (SPARK RM 9.5).
4690 if not Yields_Synchronized_Object (Rec) then
4691 Comp := First_Component (Rec);
4692 while Present (Comp) loop
4693 if Comes_From_Source (Comp)
4694 and then Yields_Synchronized_Object (Etype (Comp))
4695 then
4696 Error_Msg_Name_1 := Chars (Rec);
4697 Error_Msg_N
4698 ("component & of non-synchronized type % cannot be "
4699 & "synchronized", Comp);
4700 end if;
4702 Next_Component (Comp);
4703 end loop;
4704 end if;
4706 -- A Ghost type cannot have a component of protected or task type
4707 -- (SPARK RM 6.9(19)).
4709 if Is_Ghost_Entity (Rec) then
4710 Comp := First_Component (Rec);
4711 while Present (Comp) loop
4712 if Comes_From_Source (Comp)
4713 and then Is_Concurrent_Type (Etype (Comp))
4714 then
4715 Error_Msg_Name_1 := Chars (Rec);
4716 Error_Msg_N
4717 ("component & of ghost type % cannot be concurrent",
4718 Comp);
4719 end if;
4721 Next_Component (Comp);
4722 end loop;
4723 end if;
4724 end if;
4726 -- Make sure that if we have an iterator aspect, then we have
4727 -- either Constant_Indexing or Variable_Indexing.
4729 declare
4730 Iterator_Aspect : Node_Id;
4732 begin
4733 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
4735 if No (Iterator_Aspect) then
4736 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
4737 end if;
4739 if Present (Iterator_Aspect) then
4740 if Has_Aspect (Rec, Aspect_Constant_Indexing)
4741 or else
4742 Has_Aspect (Rec, Aspect_Variable_Indexing)
4743 then
4744 null;
4745 else
4746 Error_Msg_N
4747 ("Iterator_Element requires indexing aspect",
4748 Iterator_Aspect);
4749 end if;
4750 end if;
4751 end;
4753 -- All done if not a full record definition
4755 if Ekind (Rec) /= E_Record_Type then
4756 return;
4757 end if;
4759 -- Finally we need to check the variant part to make sure that
4760 -- all types within choices are properly frozen as part of the
4761 -- freezing of the record type.
4763 Check_Variant_Part : declare
4764 D : constant Node_Id := Declaration_Node (Rec);
4765 T : Node_Id;
4766 C : Node_Id;
4768 begin
4769 -- Find component list
4771 C := Empty;
4773 if Nkind (D) = N_Full_Type_Declaration then
4774 T := Type_Definition (D);
4776 if Nkind (T) = N_Record_Definition then
4777 C := Component_List (T);
4779 elsif Nkind (T) = N_Derived_Type_Definition
4780 and then Present (Record_Extension_Part (T))
4781 then
4782 C := Component_List (Record_Extension_Part (T));
4783 end if;
4784 end if;
4786 -- Case of variant part present
4788 if Present (C) and then Present (Variant_Part (C)) then
4789 Freeze_Choices_In_Variant_Part (Variant_Part (C));
4790 end if;
4792 -- Note: we used to call Check_Choices here, but it is too early,
4793 -- since predicated subtypes are frozen here, but their freezing
4794 -- actions are in Analyze_Freeze_Entity, which has not been called
4795 -- yet for entities frozen within this procedure, so we moved that
4796 -- call to the Analyze_Freeze_Entity for the record type.
4798 end Check_Variant_Part;
4800 -- Check that all the primitives of an interface type are abstract
4801 -- or null procedures.
4803 if Is_Interface (Rec)
4804 and then not Error_Posted (Parent (Rec))
4805 then
4806 declare
4807 Elmt : Elmt_Id;
4808 Subp : Entity_Id;
4810 begin
4811 Elmt := First_Elmt (Primitive_Operations (Rec));
4812 while Present (Elmt) loop
4813 Subp := Node (Elmt);
4815 if not Is_Abstract_Subprogram (Subp)
4817 -- Avoid reporting the error on inherited primitives
4819 and then Comes_From_Source (Subp)
4820 then
4821 Error_Msg_Name_1 := Chars (Subp);
4823 if Ekind (Subp) = E_Procedure then
4824 if not Null_Present (Parent (Subp)) then
4825 Error_Msg_N
4826 ("interface procedure % must be abstract or null",
4827 Parent (Subp));
4828 end if;
4829 else
4830 Error_Msg_N
4831 ("interface function % must be abstract",
4832 Parent (Subp));
4833 end if;
4834 end if;
4836 Next_Elmt (Elmt);
4837 end loop;
4838 end;
4839 end if;
4841 -- For a derived tagged type, check whether inherited primitives
4842 -- might require a wrapper to handle class-wide conditions.
4844 if Is_Tagged_Type (Rec) and then Is_Derived_Type (Rec) then
4845 Check_Inherited_Conditions (Rec);
4846 end if;
4847 end Freeze_Record_Type;
4849 -------------------------------
4850 -- Has_Boolean_Aspect_Import --
4851 -------------------------------
4853 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4854 Decl : constant Node_Id := Declaration_Node (E);
4855 Asp : Node_Id;
4856 Expr : Node_Id;
4858 begin
4859 if Has_Aspects (Decl) then
4860 Asp := First (Aspect_Specifications (Decl));
4861 while Present (Asp) loop
4862 Expr := Expression (Asp);
4864 -- The value of aspect Import is True when the expression is
4865 -- either missing or it is explicitly set to True.
4867 if Get_Aspect_Id (Asp) = Aspect_Import
4868 and then (No (Expr)
4869 or else (Compile_Time_Known_Value (Expr)
4870 and then Is_True (Expr_Value (Expr))))
4871 then
4872 return True;
4873 end if;
4875 Next (Asp);
4876 end loop;
4877 end if;
4879 return False;
4880 end Has_Boolean_Aspect_Import;
4882 -------------------------
4883 -- Inherit_Freeze_Node --
4884 -------------------------
4886 procedure Inherit_Freeze_Node
4887 (Fnod : Node_Id;
4888 Typ : Entity_Id)
4890 Typ_Fnod : constant Node_Id := Freeze_Node (Typ);
4892 begin
4893 Set_Freeze_Node (Typ, Fnod);
4894 Set_Entity (Fnod, Typ);
4896 -- The input type had an existing node. Propagate relevant attributes
4897 -- from the old freeze node to the inherited freeze node.
4899 -- ??? if both freeze nodes have attributes, would they differ?
4901 if Present (Typ_Fnod) then
4903 -- Attribute Access_Types_To_Process
4905 if Present (Access_Types_To_Process (Typ_Fnod))
4906 and then No (Access_Types_To_Process (Fnod))
4907 then
4908 Set_Access_Types_To_Process (Fnod,
4909 Access_Types_To_Process (Typ_Fnod));
4910 end if;
4912 -- Attribute Actions
4914 if Present (Actions (Typ_Fnod)) and then No (Actions (Fnod)) then
4915 Set_Actions (Fnod, Actions (Typ_Fnod));
4916 end if;
4918 -- Attribute First_Subtype_Link
4920 if Present (First_Subtype_Link (Typ_Fnod))
4921 and then No (First_Subtype_Link (Fnod))
4922 then
4923 Set_First_Subtype_Link (Fnod, First_Subtype_Link (Typ_Fnod));
4924 end if;
4926 -- Attribute TSS_Elist
4928 if Present (TSS_Elist (Typ_Fnod))
4929 and then No (TSS_Elist (Fnod))
4930 then
4931 Set_TSS_Elist (Fnod, TSS_Elist (Typ_Fnod));
4932 end if;
4933 end if;
4934 end Inherit_Freeze_Node;
4936 ------------------------------
4937 -- Wrap_Imported_Subprogram --
4938 ------------------------------
4940 -- The issue here is that our normal approach of checking preconditions
4941 -- and postconditions does not work for imported procedures, since we
4942 -- are not generating code for the body. To get around this we create
4943 -- a wrapper, as shown by the following example:
4945 -- procedure K (A : Integer);
4946 -- pragma Import (C, K);
4948 -- The spec is rewritten by removing the effects of pragma Import, but
4949 -- leaving the convention unchanged, as though the source had said:
4951 -- procedure K (A : Integer);
4952 -- pragma Convention (C, K);
4954 -- and we create a body, added to the entity K freeze actions, which
4955 -- looks like:
4957 -- procedure K (A : Integer) is
4958 -- procedure K (A : Integer);
4959 -- pragma Import (C, K);
4960 -- begin
4961 -- K (A);
4962 -- end K;
4964 -- Now the contract applies in the normal way to the outer procedure,
4965 -- and the inner procedure has no contracts, so there is no problem
4966 -- in just calling it to get the original effect.
4968 -- In the case of a function, we create an appropriate return statement
4969 -- for the subprogram body that calls the inner procedure.
4971 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
4972 function Copy_Import_Pragma return Node_Id;
4973 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
4975 ------------------------
4976 -- Copy_Import_Pragma --
4977 ------------------------
4979 function Copy_Import_Pragma return Node_Id is
4981 -- The subprogram should have an import pragma, otherwise it does
4982 -- need a wrapper.
4984 Prag : constant Node_Id := Import_Pragma (E);
4985 pragma Assert (Present (Prag));
4987 -- Save all semantic fields of the pragma
4989 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
4990 Save_From : constant Boolean := From_Aspect_Specification (Prag);
4991 Save_Prag : constant Node_Id := Next_Pragma (Prag);
4992 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
4994 Result : Node_Id;
4996 begin
4997 -- Reset all semantic fields. This avoids a potential infinite
4998 -- loop when the pragma comes from an aspect as the duplication
4999 -- will copy the aspect, then copy the corresponding pragma and
5000 -- so on.
5002 Set_Corresponding_Aspect (Prag, Empty);
5003 Set_From_Aspect_Specification (Prag, False);
5004 Set_Next_Pragma (Prag, Empty);
5005 Set_Next_Rep_Item (Prag, Empty);
5007 Result := Copy_Separate_Tree (Prag);
5009 -- Restore the original semantic fields
5011 Set_Corresponding_Aspect (Prag, Save_Asp);
5012 Set_From_Aspect_Specification (Prag, Save_From);
5013 Set_Next_Pragma (Prag, Save_Prag);
5014 Set_Next_Rep_Item (Prag, Save_Rep);
5016 return Result;
5017 end Copy_Import_Pragma;
5019 -- Local variables
5021 Loc : constant Source_Ptr := Sloc (E);
5022 CE : constant Name_Id := Chars (E);
5023 Bod : Node_Id;
5024 Forml : Entity_Id;
5025 Parms : List_Id;
5026 Prag : Node_Id;
5027 Spec : Node_Id;
5028 Stmt : Node_Id;
5030 -- Start of processing for Wrap_Imported_Subprogram
5032 begin
5033 -- Nothing to do if not imported
5035 if not Is_Imported (E) then
5036 return;
5038 -- Test enabling conditions for wrapping
5040 elsif Is_Subprogram (E)
5041 and then Present (Contract (E))
5042 and then Present (Pre_Post_Conditions (Contract (E)))
5043 and then not GNATprove_Mode
5044 then
5045 -- Here we do the wrap
5047 -- Note on calls to Copy_Separate_Tree. The trees we are copying
5048 -- here are fully analyzed, but we definitely want fully syntactic
5049 -- unanalyzed trees in the body we construct, so that the analysis
5050 -- generates the right visibility, and that is exactly what the
5051 -- calls to Copy_Separate_Tree give us.
5053 Prag := Copy_Import_Pragma;
5055 -- Fix up spec so it is no longer imported and has convention Ada
5057 Set_Has_Completion (E, False);
5058 Set_Import_Pragma (E, Empty);
5059 Set_Interface_Name (E, Empty);
5060 Set_Is_Imported (E, False);
5061 Set_Convention (E, Convention_Ada);
5063 -- Grab the subprogram declaration and specification
5065 Spec := Declaration_Node (E);
5067 -- Build parameter list that we need
5069 Parms := New_List;
5070 Forml := First_Formal (E);
5071 while Present (Forml) loop
5072 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
5073 Next_Formal (Forml);
5074 end loop;
5076 -- Build the call
5078 if Ekind_In (E, E_Function, E_Generic_Function) then
5079 Stmt :=
5080 Make_Simple_Return_Statement (Loc,
5081 Expression =>
5082 Make_Function_Call (Loc,
5083 Name => Make_Identifier (Loc, CE),
5084 Parameter_Associations => Parms));
5086 else
5087 Stmt :=
5088 Make_Procedure_Call_Statement (Loc,
5089 Name => Make_Identifier (Loc, CE),
5090 Parameter_Associations => Parms);
5091 end if;
5093 -- Now build the body
5095 Bod :=
5096 Make_Subprogram_Body (Loc,
5097 Specification =>
5098 Copy_Separate_Tree (Spec),
5099 Declarations => New_List (
5100 Make_Subprogram_Declaration (Loc,
5101 Specification => Copy_Separate_Tree (Spec)),
5102 Prag),
5103 Handled_Statement_Sequence =>
5104 Make_Handled_Sequence_Of_Statements (Loc,
5105 Statements => New_List (Stmt),
5106 End_Label => Make_Identifier (Loc, CE)));
5108 -- Append the body to freeze result
5110 Add_To_Result (Bod);
5111 return;
5113 -- Case of imported subprogram that does not get wrapped
5115 else
5116 -- Set Is_Public. All imported entities need an external symbol
5117 -- created for them since they are always referenced from another
5118 -- object file. Note this used to be set when we set Is_Imported
5119 -- back in Sem_Prag, but now we delay it to this point, since we
5120 -- don't want to set this flag if we wrap an imported subprogram.
5122 Set_Is_Public (E);
5123 end if;
5124 end Wrap_Imported_Subprogram;
5126 -- Local variables
5128 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5129 -- Save the Ghost mode to restore on exit
5131 -- Start of processing for Freeze_Entity
5133 begin
5134 -- The entity being frozen may be subject to pragma Ghost. Set the mode
5135 -- now to ensure that any nodes generated during freezing are properly
5136 -- flagged as Ghost.
5138 Set_Ghost_Mode (E);
5140 -- We are going to test for various reasons why this entity need not be
5141 -- frozen here, but in the case of an Itype that's defined within a
5142 -- record, that test actually applies to the record.
5144 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
5145 Test_E := Scope (E);
5146 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
5147 and then Is_Record_Type (Underlying_Type (Scope (E)))
5148 then
5149 Test_E := Underlying_Type (Scope (E));
5150 end if;
5152 -- Do not freeze if already frozen since we only need one freeze node
5154 if Is_Frozen (E) then
5155 Result := No_List;
5156 goto Leave;
5158 elsif Ekind (E) = E_Generic_Package then
5159 Result := Freeze_Generic_Entities (E);
5160 goto Leave;
5162 -- It is improper to freeze an external entity within a generic because
5163 -- its freeze node will appear in a non-valid context. The entity will
5164 -- be frozen in the proper scope after the current generic is analyzed.
5165 -- However, aspects must be analyzed because they may be queried later
5166 -- within the generic itself, and the corresponding pragma or attribute
5167 -- definition has not been analyzed yet.
5169 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
5170 if Has_Delayed_Aspects (E) then
5171 Analyze_Aspects_At_Freeze_Point (E);
5172 end if;
5174 Result := No_List;
5175 goto Leave;
5177 -- AI05-0213: A formal incomplete type does not freeze the actual. In
5178 -- the instance, the same applies to the subtype renaming the actual.
5180 elsif Is_Private_Type (E)
5181 and then Is_Generic_Actual_Type (E)
5182 and then No (Full_View (Base_Type (E)))
5183 and then Ada_Version >= Ada_2012
5184 then
5185 Result := No_List;
5186 goto Leave;
5188 -- Formal subprograms are never frozen
5190 elsif Is_Formal_Subprogram (E) then
5191 Result := No_List;
5192 goto Leave;
5194 -- Generic types are never frozen as they lack delayed semantic checks
5196 elsif Is_Generic_Type (E) then
5197 Result := No_List;
5198 goto Leave;
5200 -- Do not freeze a global entity within an inner scope created during
5201 -- expansion. A call to subprogram E within some internal procedure
5202 -- (a stream attribute for example) might require freezing E, but the
5203 -- freeze node must appear in the same declarative part as E itself.
5204 -- The two-pass elaboration mechanism in gigi guarantees that E will
5205 -- be frozen before the inner call is elaborated. We exclude constants
5206 -- from this test, because deferred constants may be frozen early, and
5207 -- must be diagnosed (e.g. in the case of a deferred constant being used
5208 -- in a default expression). If the enclosing subprogram comes from
5209 -- source, or is a generic instance, then the freeze point is the one
5210 -- mandated by the language, and we freeze the entity. A subprogram that
5211 -- is a child unit body that acts as a spec does not have a spec that
5212 -- comes from source, but can only come from source.
5214 elsif In_Open_Scopes (Scope (Test_E))
5215 and then Scope (Test_E) /= Current_Scope
5216 and then Ekind (Test_E) /= E_Constant
5217 then
5218 declare
5219 S : Entity_Id;
5221 begin
5222 S := Current_Scope;
5223 while Present (S) loop
5224 if Is_Overloadable (S) then
5225 if Comes_From_Source (S)
5226 or else Is_Generic_Instance (S)
5227 or else Is_Child_Unit (S)
5228 then
5229 exit;
5230 else
5231 Result := No_List;
5232 goto Leave;
5233 end if;
5234 end if;
5236 S := Scope (S);
5237 end loop;
5238 end;
5240 -- Similarly, an inlined instance body may make reference to global
5241 -- entities, but these references cannot be the proper freezing point
5242 -- for them, and in the absence of inlining freezing will take place in
5243 -- their own scope. Normally instance bodies are analyzed after the
5244 -- enclosing compilation, and everything has been frozen at the proper
5245 -- place, but with front-end inlining an instance body is compiled
5246 -- before the end of the enclosing scope, and as a result out-of-order
5247 -- freezing must be prevented.
5249 elsif Front_End_Inlining
5250 and then In_Instance_Body
5251 and then Present (Scope (Test_E))
5252 then
5253 declare
5254 S : Entity_Id;
5256 begin
5257 S := Scope (Test_E);
5258 while Present (S) loop
5259 if Is_Generic_Instance (S) then
5260 exit;
5261 else
5262 S := Scope (S);
5263 end if;
5264 end loop;
5266 if No (S) then
5267 Result := No_List;
5268 goto Leave;
5269 end if;
5270 end;
5271 end if;
5273 -- Add checks to detect proper initialization of scalars that may appear
5274 -- as subprogram parameters.
5276 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
5277 Apply_Parameter_Validity_Checks (E);
5278 end if;
5280 -- Deal with delayed aspect specifications. The analysis of the aspect
5281 -- is required to be delayed to the freeze point, thus we analyze the
5282 -- pragma or attribute definition clause in the tree at this point. We
5283 -- also analyze the aspect specification node at the freeze point when
5284 -- the aspect doesn't correspond to pragma/attribute definition clause.
5285 -- In addition, a derived type may have inherited aspects that were
5286 -- delayed in the parent, so these must also be captured now.
5288 if Has_Delayed_Aspects (E)
5289 or else May_Inherit_Delayed_Rep_Aspects (E)
5290 then
5291 Analyze_Aspects_At_Freeze_Point (E);
5292 end if;
5294 -- Here to freeze the entity
5296 Set_Is_Frozen (E);
5298 -- Case of entity being frozen is other than a type
5300 if not Is_Type (E) then
5302 -- If entity is exported or imported and does not have an external
5303 -- name, now is the time to provide the appropriate default name.
5304 -- Skip this if the entity is stubbed, since we don't need a name
5305 -- for any stubbed routine. For the case on intrinsics, if no
5306 -- external name is specified, then calls will be handled in
5307 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
5308 -- external name is provided, then Expand_Intrinsic_Call leaves
5309 -- calls in place for expansion by GIGI.
5311 if (Is_Imported (E) or else Is_Exported (E))
5312 and then No (Interface_Name (E))
5313 and then Convention (E) /= Convention_Stubbed
5314 and then Convention (E) /= Convention_Intrinsic
5315 then
5316 Set_Encoded_Interface_Name
5317 (E, Get_Default_External_Name (E));
5319 -- If entity is an atomic object appearing in a declaration and
5320 -- the expression is an aggregate, assign it to a temporary to
5321 -- ensure that the actual assignment is done atomically rather
5322 -- than component-wise (the assignment to the temp may be done
5323 -- component-wise, but that is harmless).
5325 elsif Is_Atomic_Or_VFA (E)
5326 and then Nkind (Parent (E)) = N_Object_Declaration
5327 and then Present (Expression (Parent (E)))
5328 and then Nkind (Expression (Parent (E))) = N_Aggregate
5329 and then Is_Atomic_VFA_Aggregate (Expression (Parent (E)))
5330 then
5331 null;
5332 end if;
5334 -- Subprogram case
5336 if Is_Subprogram (E) then
5338 -- Check for needing to wrap imported subprogram
5340 Wrap_Imported_Subprogram (E);
5342 -- Freeze all parameter types and the return type (RM 13.14(14)).
5343 -- However skip this for internal subprograms. This is also where
5344 -- any extra formal parameters are created since we now know
5345 -- whether the subprogram will use a foreign convention.
5347 -- In Ada 2012, freezing a subprogram does not always freeze the
5348 -- corresponding profile (see AI05-019). An attribute reference
5349 -- is not a freezing point of the profile. Flag Do_Freeze_Profile
5350 -- indicates whether the profile should be frozen now.
5351 -- Other constructs that should not freeze ???
5353 -- This processing doesn't apply to internal entities (see below)
5355 if not Is_Internal (E) and then Do_Freeze_Profile then
5356 if not Freeze_Profile (E) then
5357 goto Leave;
5358 end if;
5359 end if;
5361 -- Must freeze its parent first if it is a derived subprogram
5363 if Present (Alias (E)) then
5364 Freeze_And_Append (Alias (E), N, Result);
5365 end if;
5367 -- We don't freeze internal subprograms, because we don't normally
5368 -- want addition of extra formals or mechanism setting to happen
5369 -- for those. However we do pass through predefined dispatching
5370 -- cases, since extra formals may be needed in some cases, such as
5371 -- for the stream 'Input function (build-in-place formals).
5373 if not Is_Internal (E)
5374 or else Is_Predefined_Dispatching_Operation (E)
5375 then
5376 Freeze_Subprogram (E);
5377 end if;
5379 -- If warning on suspicious contracts then check for the case of
5380 -- a postcondition other than False for a No_Return subprogram.
5382 if No_Return (E)
5383 and then Warn_On_Suspicious_Contract
5384 and then Present (Contract (E))
5385 then
5386 declare
5387 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
5388 Exp : Node_Id;
5390 begin
5391 while Present (Prag) loop
5392 if Nam_In (Pragma_Name_Unmapped (Prag),
5393 Name_Post,
5394 Name_Postcondition,
5395 Name_Refined_Post)
5396 then
5397 Exp :=
5398 Expression
5399 (First (Pragma_Argument_Associations (Prag)));
5401 if Nkind (Exp) /= N_Identifier
5402 or else Chars (Exp) /= Name_False
5403 then
5404 Error_Msg_NE
5405 ("useless postcondition, & is marked "
5406 & "No_Return?T?", Exp, E);
5407 end if;
5408 end if;
5410 Prag := Next_Pragma (Prag);
5411 end loop;
5412 end;
5413 end if;
5415 -- Here for other than a subprogram or type
5417 else
5418 -- If entity has a type, and it is not a generic unit, then
5419 -- freeze it first (RM 13.14(10)).
5421 if Present (Etype (E))
5422 and then Ekind (E) /= E_Generic_Function
5423 then
5424 Freeze_And_Append (Etype (E), N, Result);
5426 -- For an object of an anonymous array type, aspects on the
5427 -- object declaration apply to the type itself. This is the
5428 -- case for Atomic_Components, Volatile_Components, and
5429 -- Independent_Components. In these cases analysis of the
5430 -- generated pragma will mark the anonymous types accordingly,
5431 -- and the object itself does not require a freeze node.
5433 if Ekind (E) = E_Variable
5434 and then Is_Itype (Etype (E))
5435 and then Is_Array_Type (Etype (E))
5436 and then Has_Delayed_Aspects (E)
5437 then
5438 Set_Has_Delayed_Aspects (E, False);
5439 Set_Has_Delayed_Freeze (E, False);
5440 Set_Freeze_Node (E, Empty);
5441 end if;
5442 end if;
5444 -- Special processing for objects created by object declaration
5446 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
5447 Freeze_Object_Declaration (E);
5448 end if;
5450 -- Check that a constant which has a pragma Volatile[_Components]
5451 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
5453 -- Note: Atomic[_Components] also sets Volatile[_Components]
5455 if Ekind (E) = E_Constant
5456 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
5457 and then not Is_Imported (E)
5458 and then not Has_Boolean_Aspect_Import (E)
5459 then
5460 -- Make sure we actually have a pragma, and have not merely
5461 -- inherited the indication from elsewhere (e.g. an address
5462 -- clause, which is not good enough in RM terms).
5464 if Has_Rep_Pragma (E, Name_Atomic)
5465 or else
5466 Has_Rep_Pragma (E, Name_Atomic_Components)
5467 then
5468 Error_Msg_N
5469 ("stand alone atomic constant must be " &
5470 "imported (RM C.6(13))", E);
5472 elsif Has_Rep_Pragma (E, Name_Volatile)
5473 or else
5474 Has_Rep_Pragma (E, Name_Volatile_Components)
5475 then
5476 Error_Msg_N
5477 ("stand alone volatile constant must be " &
5478 "imported (RM C.6(13))", E);
5479 end if;
5480 end if;
5482 -- Static objects require special handling
5484 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
5485 and then Is_Statically_Allocated (E)
5486 then
5487 Freeze_Static_Object (E);
5488 end if;
5490 -- Remaining step is to layout objects
5492 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
5493 or else Is_Formal (E)
5494 then
5495 Layout_Object (E);
5496 end if;
5498 -- For an object that does not have delayed freezing, and whose
5499 -- initialization actions have been captured in a compound
5500 -- statement, move them back now directly within the enclosing
5501 -- statement sequence.
5503 if Ekind_In (E, E_Constant, E_Variable)
5504 and then not Has_Delayed_Freeze (E)
5505 then
5506 Explode_Initialization_Compound_Statement (E);
5507 end if;
5509 -- Do not generate a freeze node for a generic unit
5511 if Is_Generic_Unit (E) then
5512 Result := No_List;
5513 goto Leave;
5514 end if;
5515 end if;
5517 -- Case of a type or subtype being frozen
5519 else
5520 -- We used to check here that a full type must have preelaborable
5521 -- initialization if it completes a private type specified with
5522 -- pragma Preelaborable_Initialization, but that missed cases where
5523 -- the types occur within a generic package, since the freezing
5524 -- that occurs within a containing scope generally skips traversal
5525 -- of a generic unit's declarations (those will be frozen within
5526 -- instances). This check was moved to Analyze_Package_Specification.
5528 -- The type may be defined in a generic unit. This can occur when
5529 -- freezing a generic function that returns the type (which is
5530 -- defined in a parent unit). It is clearly meaningless to freeze
5531 -- this type. However, if it is a subtype, its size may be determi-
5532 -- nable and used in subsequent checks, so might as well try to
5533 -- compute it.
5535 -- In Ada 2012, Freeze_Entities is also used in the front end to
5536 -- trigger the analysis of aspect expressions, so in this case we
5537 -- want to continue the freezing process.
5539 -- Is_Generic_Unit (Scope (E)) is dubious here, do we want instead
5540 -- In_Generic_Scope (E)???
5542 if Present (Scope (E))
5543 and then Is_Generic_Unit (Scope (E))
5544 and then
5545 (not Has_Predicates (E)
5546 and then not Has_Delayed_Freeze (E))
5547 then
5548 Check_Compile_Time_Size (E);
5549 Result := No_List;
5550 goto Leave;
5551 end if;
5553 -- Check for error of Type_Invariant'Class applied to an untagged
5554 -- type (check delayed to freeze time when full type is available).
5556 declare
5557 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
5558 begin
5559 if Present (Prag)
5560 and then Class_Present (Prag)
5561 and then not Is_Tagged_Type (E)
5562 then
5563 Error_Msg_NE
5564 ("Type_Invariant''Class cannot be specified for &", Prag, E);
5565 Error_Msg_N
5566 ("\can only be specified for a tagged type", Prag);
5567 end if;
5568 end;
5570 if Is_Ghost_Entity (E) then
5572 -- A Ghost type cannot be concurrent (SPARK RM 6.9(19)). Verify
5573 -- this legality rule first to five a finer-grained diagnostic.
5575 if Is_Concurrent_Type (E) then
5576 Error_Msg_N ("ghost type & cannot be concurrent", E);
5578 -- A Ghost type cannot be effectively volatile (SPARK RM 6.9(7))
5580 elsif Is_Effectively_Volatile (E) then
5581 Error_Msg_N ("ghost type & cannot be volatile", E);
5582 end if;
5583 end if;
5585 -- Deal with special cases of freezing for subtype
5587 if E /= Base_Type (E) then
5589 -- Before we do anything else, a specific test for the case of a
5590 -- size given for an array where the array would need to be packed
5591 -- in order for the size to be honored, but is not. This is the
5592 -- case where implicit packing may apply. The reason we do this so
5593 -- early is that, if we have implicit packing, the layout of the
5594 -- base type is affected, so we must do this before we freeze the
5595 -- base type.
5597 -- We could do this processing only if implicit packing is enabled
5598 -- since in all other cases, the error would be caught by the back
5599 -- end. However, we choose to do the check even if we do not have
5600 -- implicit packing enabled, since this allows us to give a more
5601 -- useful error message (advising use of pragma Implicit_Packing
5602 -- or pragma Pack).
5604 if Is_Array_Type (E) then
5605 declare
5606 Ctyp : constant Entity_Id := Component_Type (E);
5607 Rsiz : constant Uint := RM_Size (Ctyp);
5608 SZ : constant Node_Id := Size_Clause (E);
5609 Btyp : constant Entity_Id := Base_Type (E);
5611 Lo : Node_Id;
5612 Hi : Node_Id;
5613 Indx : Node_Id;
5615 Dim : Uint;
5616 Num_Elmts : Uint := Uint_1;
5617 -- Number of elements in array
5619 begin
5620 -- Check enabling conditions. These are straightforward
5621 -- except for the test for a limited composite type. This
5622 -- eliminates the rare case of a array of limited components
5623 -- where there are issues of whether or not we can go ahead
5624 -- and pack the array (since we can't freely pack and unpack
5625 -- arrays if they are limited).
5627 -- Note that we check the root type explicitly because the
5628 -- whole point is we are doing this test before we have had
5629 -- a chance to freeze the base type (and it is that freeze
5630 -- action that causes stuff to be inherited).
5632 -- The conditions on the size are identical to those used in
5633 -- Freeze_Array_Type to set the Is_Packed flag.
5635 if Has_Size_Clause (E)
5636 and then Known_Static_RM_Size (E)
5637 and then not Is_Packed (E)
5638 and then not Has_Pragma_Pack (E)
5639 and then not Has_Component_Size_Clause (E)
5640 and then Known_Static_RM_Size (Ctyp)
5641 and then Rsiz <= 64
5642 and then not (Addressable (Rsiz)
5643 and then Known_Static_Esize (Ctyp)
5644 and then Esize (Ctyp) = Rsiz)
5645 and then not (Rsiz mod System_Storage_Unit = 0
5646 and then Is_Composite_Type (Ctyp))
5647 and then not Is_Limited_Composite (E)
5648 and then not Is_Packed (Root_Type (E))
5649 and then not Has_Component_Size_Clause (Root_Type (E))
5650 and then not (CodePeer_Mode or GNATprove_Mode)
5651 then
5652 -- Compute number of elements in array
5654 Indx := First_Index (E);
5655 while Present (Indx) loop
5656 Get_Index_Bounds (Indx, Lo, Hi);
5658 if not (Compile_Time_Known_Value (Lo)
5659 and then
5660 Compile_Time_Known_Value (Hi))
5661 then
5662 goto No_Implicit_Packing;
5663 end if;
5665 Dim := Expr_Value (Hi) - Expr_Value (Lo) + 1;
5667 if Dim >= 0 then
5668 Num_Elmts := Num_Elmts * Dim;
5669 else
5670 Num_Elmts := Uint_0;
5671 end if;
5673 Next_Index (Indx);
5674 end loop;
5676 -- What we are looking for here is the situation where
5677 -- the RM_Size given would be exactly right if there was
5678 -- a pragma Pack, resulting in the component size being
5679 -- the RM_Size of the component type.
5681 if RM_Size (E) = Num_Elmts * Rsiz then
5683 -- For implicit packing mode, just set the component
5684 -- size and Freeze_Array_Type will do the rest.
5686 if Implicit_Packing then
5687 Set_Component_Size (Btyp, Rsiz);
5689 -- Otherwise give an error message
5691 else
5692 Error_Msg_NE
5693 ("size given for& too small", SZ, E);
5694 Error_Msg_N -- CODEFIX
5695 ("\use explicit pragma Pack or use pragma "
5696 & "Implicit_Packing", SZ);
5697 end if;
5698 end if;
5699 end if;
5700 end;
5701 end if;
5703 <<No_Implicit_Packing>>
5705 -- If ancestor subtype present, freeze that first. Note that this
5706 -- will also get the base type frozen. Need RM reference ???
5708 Atype := Ancestor_Subtype (E);
5710 if Present (Atype) then
5711 Freeze_And_Append (Atype, N, Result);
5713 -- No ancestor subtype present
5715 else
5716 -- See if we have a nearest ancestor that has a predicate.
5717 -- That catches the case of derived type with a predicate.
5718 -- Need RM reference here ???
5720 Atype := Nearest_Ancestor (E);
5722 if Present (Atype) and then Has_Predicates (Atype) then
5723 Freeze_And_Append (Atype, N, Result);
5724 end if;
5726 -- Freeze base type before freezing the entity (RM 13.14(15))
5728 if E /= Base_Type (E) then
5729 Freeze_And_Append (Base_Type (E), N, Result);
5730 end if;
5731 end if;
5733 -- A subtype inherits all the type-related representation aspects
5734 -- from its parents (RM 13.1(8)).
5736 Inherit_Aspects_At_Freeze_Point (E);
5738 -- For a derived type, freeze its parent type first (RM 13.14(15))
5740 elsif Is_Derived_Type (E) then
5741 Freeze_And_Append (Etype (E), N, Result);
5742 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5744 -- A derived type inherits each type-related representation aspect
5745 -- of its parent type that was directly specified before the
5746 -- declaration of the derived type (RM 13.1(15)).
5748 Inherit_Aspects_At_Freeze_Point (E);
5749 end if;
5751 -- Check for incompatible size and alignment for record type
5753 if Warn_On_Size_Alignment
5754 and then Is_Record_Type (E)
5755 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5757 -- If explicit Object_Size clause given assume that the programmer
5758 -- knows what he is doing, and expects the compiler behavior.
5760 and then not Has_Object_Size_Clause (E)
5762 -- Check for size not a multiple of alignment
5764 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5765 then
5766 declare
5767 SC : constant Node_Id := Size_Clause (E);
5768 AC : constant Node_Id := Alignment_Clause (E);
5769 Loc : Node_Id;
5770 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5772 begin
5773 if Present (SC) and then Present (AC) then
5775 -- Give a warning
5777 if Sloc (SC) > Sloc (AC) then
5778 Loc := SC;
5779 Error_Msg_NE
5780 ("?Z?size is not a multiple of alignment for &",
5781 Loc, E);
5782 Error_Msg_Sloc := Sloc (AC);
5783 Error_Msg_Uint_1 := Alignment (E);
5784 Error_Msg_N ("\?Z?alignment of ^ specified #", Loc);
5786 else
5787 Loc := AC;
5788 Error_Msg_NE
5789 ("?Z?size is not a multiple of alignment for &",
5790 Loc, E);
5791 Error_Msg_Sloc := Sloc (SC);
5792 Error_Msg_Uint_1 := RM_Size (E);
5793 Error_Msg_N ("\?Z?size of ^ specified #", Loc);
5794 end if;
5796 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5797 Error_Msg_N ("\?Z?Object_Size will be increased to ^", Loc);
5798 end if;
5799 end;
5800 end if;
5802 -- Array type
5804 if Is_Array_Type (E) then
5805 Freeze_Array_Type (E);
5807 -- For a class-wide type, the corresponding specific type is
5808 -- frozen as well (RM 13.14(15))
5810 elsif Is_Class_Wide_Type (E) then
5811 Freeze_And_Append (Root_Type (E), N, Result);
5813 -- If the base type of the class-wide type is still incomplete,
5814 -- the class-wide remains unfrozen as well. This is legal when
5815 -- E is the formal of a primitive operation of some other type
5816 -- which is being frozen.
5818 if not Is_Frozen (Root_Type (E)) then
5819 Set_Is_Frozen (E, False);
5820 goto Leave;
5821 end if;
5823 -- The equivalent type associated with a class-wide subtype needs
5824 -- to be frozen to ensure that its layout is done.
5826 if Ekind (E) = E_Class_Wide_Subtype
5827 and then Present (Equivalent_Type (E))
5828 then
5829 Freeze_And_Append (Equivalent_Type (E), N, Result);
5830 end if;
5832 -- Generate an itype reference for a library-level class-wide type
5833 -- at the freeze point. Otherwise the first explicit reference to
5834 -- the type may appear in an inner scope which will be rejected by
5835 -- the back-end.
5837 if Is_Itype (E)
5838 and then Is_Compilation_Unit (Scope (E))
5839 then
5840 declare
5841 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5843 begin
5844 Set_Itype (Ref, E);
5846 -- From a gigi point of view, a class-wide subtype derives
5847 -- from its record equivalent type. As a result, the itype
5848 -- reference must appear after the freeze node of the
5849 -- equivalent type or gigi will reject the reference.
5851 if Ekind (E) = E_Class_Wide_Subtype
5852 and then Present (Equivalent_Type (E))
5853 then
5854 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5855 else
5856 Add_To_Result (Ref);
5857 end if;
5858 end;
5859 end if;
5861 -- For a record type or record subtype, freeze all component types
5862 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5863 -- using Is_Record_Type, because we don't want to attempt the freeze
5864 -- for the case of a private type with record extension (we will do
5865 -- that later when the full type is frozen).
5867 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype) then
5868 if not In_Generic_Scope (E) then
5869 Freeze_Record_Type (E);
5870 end if;
5872 -- Report a warning if a discriminated record base type has a
5873 -- convention with language C or C++ applied to it. This check is
5874 -- done even within generic scopes (but not in instantiations),
5875 -- which is why we don't do it as part of Freeze_Record_Type.
5877 Check_Suspicious_Convention (E);
5879 -- For a concurrent type, freeze corresponding record type. This does
5880 -- not correspond to any specific rule in the RM, but the record type
5881 -- is essentially part of the concurrent type. Also freeze all local
5882 -- entities. This includes record types created for entry parameter
5883 -- blocks and whatever local entities may appear in the private part.
5885 elsif Is_Concurrent_Type (E) then
5886 if Present (Corresponding_Record_Type (E)) then
5887 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5888 end if;
5890 Comp := First_Entity (E);
5891 while Present (Comp) loop
5892 if Is_Type (Comp) then
5893 Freeze_And_Append (Comp, N, Result);
5895 elsif (Ekind (Comp)) /= E_Function then
5897 -- The guard on the presence of the Etype seems to be needed
5898 -- for some CodePeer (-gnatcC) cases, but not clear why???
5900 if Present (Etype (Comp)) then
5901 if Is_Itype (Etype (Comp))
5902 and then Underlying_Type (Scope (Etype (Comp))) = E
5903 then
5904 Undelay_Type (Etype (Comp));
5905 end if;
5907 Freeze_And_Append (Etype (Comp), N, Result);
5908 end if;
5909 end if;
5911 Next_Entity (Comp);
5912 end loop;
5914 -- Private types are required to point to the same freeze node as
5915 -- their corresponding full views. The freeze node itself has to
5916 -- point to the partial view of the entity (because from the partial
5917 -- view, we can retrieve the full view, but not the reverse).
5918 -- However, in order to freeze correctly, we need to freeze the full
5919 -- view. If we are freezing at the end of a scope (or within the
5920 -- scope) of the private type, the partial and full views will have
5921 -- been swapped, the full view appears first in the entity chain and
5922 -- the swapping mechanism ensures that the pointers are properly set
5923 -- (on scope exit).
5925 -- If we encounter the partial view before the full view (e.g. when
5926 -- freezing from another scope), we freeze the full view, and then
5927 -- set the pointers appropriately since we cannot rely on swapping to
5928 -- fix things up (subtypes in an outer scope might not get swapped).
5930 -- If the full view is itself private, the above requirements apply
5931 -- to the underlying full view instead of the full view. But there is
5932 -- no swapping mechanism for the underlying full view so we need to
5933 -- set the pointers appropriately in both cases.
5935 elsif Is_Incomplete_Or_Private_Type (E)
5936 and then not Is_Generic_Type (E)
5937 then
5938 -- The construction of the dispatch table associated with library
5939 -- level tagged types forces freezing of all the primitives of the
5940 -- type, which may cause premature freezing of the partial view.
5941 -- For example:
5943 -- package Pkg is
5944 -- type T is tagged private;
5945 -- type DT is new T with private;
5946 -- procedure Prim (X : in out T; Y : in out DT'Class);
5947 -- private
5948 -- type T is tagged null record;
5949 -- Obj : T;
5950 -- type DT is new T with null record;
5951 -- end;
5953 -- In this case the type will be frozen later by the usual
5954 -- mechanism: an object declaration, an instantiation, or the
5955 -- end of a declarative part.
5957 if Is_Library_Level_Tagged_Type (E)
5958 and then not Present (Full_View (E))
5959 then
5960 Set_Is_Frozen (E, False);
5961 goto Leave;
5963 -- Case of full view present
5965 elsif Present (Full_View (E)) then
5967 -- If full view has already been frozen, then no further
5968 -- processing is required
5970 if Is_Frozen (Full_View (E)) then
5971 Set_Has_Delayed_Freeze (E, False);
5972 Set_Freeze_Node (E, Empty);
5974 -- Otherwise freeze full view and patch the pointers so that
5975 -- the freeze node will elaborate both views in the back end.
5976 -- However, if full view is itself private, freeze underlying
5977 -- full view instead and patch the pointers so that the freeze
5978 -- node will elaborate the three views in the back end.
5980 else
5981 declare
5982 Full : Entity_Id := Full_View (E);
5984 begin
5985 if Is_Private_Type (Full)
5986 and then Present (Underlying_Full_View (Full))
5987 then
5988 Full := Underlying_Full_View (Full);
5989 end if;
5991 Freeze_And_Append (Full, N, Result);
5993 if Full /= Full_View (E)
5994 and then Has_Delayed_Freeze (Full_View (E))
5995 then
5996 F_Node := Freeze_Node (Full);
5998 if Present (F_Node) then
5999 Inherit_Freeze_Node
6000 (Fnod => F_Node,
6001 Typ => Full_View (E));
6002 else
6003 Set_Has_Delayed_Freeze (Full_View (E), False);
6004 Set_Freeze_Node (Full_View (E), Empty);
6005 end if;
6006 end if;
6008 if Has_Delayed_Freeze (E) then
6009 F_Node := Freeze_Node (Full_View (E));
6011 if Present (F_Node) then
6012 Inherit_Freeze_Node
6013 (Fnod => F_Node,
6014 Typ => E);
6015 else
6016 -- {Incomplete,Private}_Subtypes with Full_Views
6017 -- constrained by discriminants.
6019 Set_Has_Delayed_Freeze (E, False);
6020 Set_Freeze_Node (E, Empty);
6021 end if;
6022 end if;
6023 end;
6024 end if;
6026 Check_Debug_Info_Needed (E);
6028 -- AI-117 requires that the convention of a partial view be the
6029 -- same as the convention of the full view. Note that this is a
6030 -- recognized breach of privacy, but it's essential for logical
6031 -- consistency of representation, and the lack of a rule in
6032 -- RM95 was an oversight.
6034 Set_Convention (E, Convention (Full_View (E)));
6036 Set_Size_Known_At_Compile_Time (E,
6037 Size_Known_At_Compile_Time (Full_View (E)));
6039 -- Size information is copied from the full view to the
6040 -- incomplete or private view for consistency.
6042 -- We skip this is the full view is not a type. This is very
6043 -- strange of course, and can only happen as a result of
6044 -- certain illegalities, such as a premature attempt to derive
6045 -- from an incomplete type.
6047 if Is_Type (Full_View (E)) then
6048 Set_Size_Info (E, Full_View (E));
6049 Set_RM_Size (E, RM_Size (Full_View (E)));
6050 end if;
6052 goto Leave;
6054 -- Case of underlying full view present
6056 elsif Is_Private_Type (E)
6057 and then Present (Underlying_Full_View (E))
6058 then
6059 if not Is_Frozen (Underlying_Full_View (E)) then
6060 Freeze_And_Append (Underlying_Full_View (E), N, Result);
6061 end if;
6063 -- Patch the pointers so that the freeze node will elaborate
6064 -- both views in the back end.
6066 if Has_Delayed_Freeze (E) then
6067 F_Node := Freeze_Node (Underlying_Full_View (E));
6069 if Present (F_Node) then
6070 Inherit_Freeze_Node
6071 (Fnod => F_Node,
6072 Typ => E);
6073 else
6074 Set_Has_Delayed_Freeze (E, False);
6075 Set_Freeze_Node (E, Empty);
6076 end if;
6077 end if;
6079 Check_Debug_Info_Needed (E);
6081 goto Leave;
6083 -- Case of no full view present. If entity is derived or subtype,
6084 -- it is safe to freeze, correctness depends on the frozen status
6085 -- of parent. Otherwise it is either premature usage, or a Taft
6086 -- amendment type, so diagnosis is at the point of use and the
6087 -- type might be frozen later.
6089 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
6090 null;
6092 else
6093 Set_Is_Frozen (E, False);
6094 Result := No_List;
6095 goto Leave;
6096 end if;
6098 -- For access subprogram, freeze types of all formals, the return
6099 -- type was already frozen, since it is the Etype of the function.
6100 -- Formal types can be tagged Taft amendment types, but otherwise
6101 -- they cannot be incomplete.
6103 elsif Ekind (E) = E_Subprogram_Type then
6104 Formal := First_Formal (E);
6105 while Present (Formal) loop
6106 if Ekind (Etype (Formal)) = E_Incomplete_Type
6107 and then No (Full_View (Etype (Formal)))
6108 then
6109 if Is_Tagged_Type (Etype (Formal)) then
6110 null;
6112 -- AI05-151: Incomplete types are allowed in access to
6113 -- subprogram specifications.
6115 elsif Ada_Version < Ada_2012 then
6116 Error_Msg_NE
6117 ("invalid use of incomplete type&", E, Etype (Formal));
6118 end if;
6119 end if;
6121 Freeze_And_Append (Etype (Formal), N, Result);
6122 Next_Formal (Formal);
6123 end loop;
6125 Freeze_Subprogram (E);
6127 -- For access to a protected subprogram, freeze the equivalent type
6128 -- (however this is not set if we are not generating code or if this
6129 -- is an anonymous type used just for resolution).
6131 elsif Is_Access_Protected_Subprogram_Type (E) then
6132 if Present (Equivalent_Type (E)) then
6133 Freeze_And_Append (Equivalent_Type (E), N, Result);
6134 end if;
6135 end if;
6137 -- Generic types are never seen by the back-end, and are also not
6138 -- processed by the expander (since the expander is turned off for
6139 -- generic processing), so we never need freeze nodes for them.
6141 if Is_Generic_Type (E) then
6142 goto Leave;
6143 end if;
6145 -- Some special processing for non-generic types to complete
6146 -- representation details not known till the freeze point.
6148 if Is_Fixed_Point_Type (E) then
6149 Freeze_Fixed_Point_Type (E);
6151 -- Some error checks required for ordinary fixed-point type. Defer
6152 -- these till the freeze-point since we need the small and range
6153 -- values. We only do these checks for base types
6155 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
6156 if Small_Value (E) < Ureal_2_M_80 then
6157 Error_Msg_Name_1 := Name_Small;
6158 Error_Msg_N
6159 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
6161 elsif Small_Value (E) > Ureal_2_80 then
6162 Error_Msg_Name_1 := Name_Small;
6163 Error_Msg_N
6164 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
6165 end if;
6167 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
6168 Error_Msg_Name_1 := Name_First;
6169 Error_Msg_N
6170 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
6171 end if;
6173 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
6174 Error_Msg_Name_1 := Name_Last;
6175 Error_Msg_N
6176 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
6177 end if;
6178 end if;
6180 elsif Is_Enumeration_Type (E) then
6181 Freeze_Enumeration_Type (E);
6183 elsif Is_Integer_Type (E) then
6184 Adjust_Esize_For_Alignment (E);
6186 if Is_Modular_Integer_Type (E)
6187 and then Warn_On_Suspicious_Modulus_Value
6188 then
6189 Check_Suspicious_Modulus (E);
6190 end if;
6192 -- The pool applies to named and anonymous access types, but not
6193 -- to subprogram and to internal types generated for 'Access
6194 -- references.
6196 elsif Is_Access_Type (E)
6197 and then not Is_Access_Subprogram_Type (E)
6198 and then Ekind (E) /= E_Access_Attribute_Type
6199 then
6200 -- If a pragma Default_Storage_Pool applies, and this type has no
6201 -- Storage_Pool or Storage_Size clause (which must have occurred
6202 -- before the freezing point), then use the default. This applies
6203 -- only to base types.
6205 -- None of this applies to access to subprograms, for which there
6206 -- are clearly no pools.
6208 if Present (Default_Pool)
6209 and then Is_Base_Type (E)
6210 and then not Has_Storage_Size_Clause (E)
6211 and then No (Associated_Storage_Pool (E))
6212 then
6213 -- Case of pragma Default_Storage_Pool (null)
6215 if Nkind (Default_Pool) = N_Null then
6216 Set_No_Pool_Assigned (E);
6218 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
6220 else
6221 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
6222 end if;
6223 end if;
6225 -- Check restriction for standard storage pool
6227 if No (Associated_Storage_Pool (E)) then
6228 Check_Restriction (No_Standard_Storage_Pools, E);
6229 end if;
6231 -- Deal with error message for pure access type. This is not an
6232 -- error in Ada 2005 if there is no pool (see AI-366).
6234 if Is_Pure_Unit_Access_Type (E)
6235 and then (Ada_Version < Ada_2005
6236 or else not No_Pool_Assigned (E))
6237 and then not Is_Generic_Unit (Scope (E))
6238 then
6239 Error_Msg_N ("named access type not allowed in pure unit", E);
6241 if Ada_Version >= Ada_2005 then
6242 Error_Msg_N
6243 ("\would be legal if Storage_Size of 0 given??", E);
6245 elsif No_Pool_Assigned (E) then
6246 Error_Msg_N
6247 ("\would be legal in Ada 2005??", E);
6249 else
6250 Error_Msg_N
6251 ("\would be legal in Ada 2005 if "
6252 & "Storage_Size of 0 given??", E);
6253 end if;
6254 end if;
6255 end if;
6257 -- Case of composite types
6259 if Is_Composite_Type (E) then
6261 -- AI-117 requires that all new primitives of a tagged type must
6262 -- inherit the convention of the full view of the type. Inherited
6263 -- and overriding operations are defined to inherit the convention
6264 -- of their parent or overridden subprogram (also specified in
6265 -- AI-117), which will have occurred earlier (in Derive_Subprogram
6266 -- and New_Overloaded_Entity). Here we set the convention of
6267 -- primitives that are still convention Ada, which will ensure
6268 -- that any new primitives inherit the type's convention. Class-
6269 -- wide types can have a foreign convention inherited from their
6270 -- specific type, but are excluded from this since they don't have
6271 -- any associated primitives.
6273 if Is_Tagged_Type (E)
6274 and then not Is_Class_Wide_Type (E)
6275 and then Convention (E) /= Convention_Ada
6276 then
6277 declare
6278 Prim_List : constant Elist_Id := Primitive_Operations (E);
6279 Prim : Elmt_Id;
6281 begin
6282 Prim := First_Elmt (Prim_List);
6283 while Present (Prim) loop
6284 if Convention (Node (Prim)) = Convention_Ada then
6285 Set_Convention (Node (Prim), Convention (E));
6286 end if;
6288 Next_Elmt (Prim);
6289 end loop;
6290 end;
6291 end if;
6293 -- If the type is a simple storage pool type, then this is where
6294 -- we attempt to locate and validate its Allocate, Deallocate, and
6295 -- Storage_Size operations (the first is required, and the latter
6296 -- two are optional). We also verify that the full type for a
6297 -- private type is allowed to be a simple storage pool type.
6299 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
6300 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
6301 then
6302 -- If the type is marked Has_Private_Declaration, then this is
6303 -- a full type for a private type that was specified with the
6304 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
6305 -- pragma is allowed for the full type (for example, it can't
6306 -- be an array type, or a nonlimited record type).
6308 if Has_Private_Declaration (E) then
6309 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
6310 and then not Is_Private_Type (E)
6311 then
6312 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
6313 Error_Msg_N
6314 ("pragma% can only apply to full type that is an " &
6315 "explicitly limited type", E);
6316 end if;
6317 end if;
6319 Validate_Simple_Pool_Ops : declare
6320 Pool_Type : Entity_Id renames E;
6321 Address_Type : constant Entity_Id := RTE (RE_Address);
6322 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
6324 procedure Validate_Simple_Pool_Op_Formal
6325 (Pool_Op : Entity_Id;
6326 Pool_Op_Formal : in out Entity_Id;
6327 Expected_Mode : Formal_Kind;
6328 Expected_Type : Entity_Id;
6329 Formal_Name : String;
6330 OK_Formal : in out Boolean);
6331 -- Validate one formal Pool_Op_Formal of the candidate pool
6332 -- operation Pool_Op. The formal must be of Expected_Type
6333 -- and have mode Expected_Mode. OK_Formal will be set to
6334 -- False if the formal doesn't match. If OK_Formal is False
6335 -- on entry, then the formal will effectively be ignored
6336 -- (because validation of the pool op has already failed).
6337 -- Upon return, Pool_Op_Formal will be updated to the next
6338 -- formal, if any.
6340 procedure Validate_Simple_Pool_Operation
6341 (Op_Name : Name_Id);
6342 -- Search for and validate a simple pool operation with the
6343 -- name Op_Name. If the name is Allocate, then there must be
6344 -- exactly one such primitive operation for the simple pool
6345 -- type. If the name is Deallocate or Storage_Size, then
6346 -- there can be at most one such primitive operation. The
6347 -- profile of the located primitive must conform to what
6348 -- is expected for each operation.
6350 ------------------------------------
6351 -- Validate_Simple_Pool_Op_Formal --
6352 ------------------------------------
6354 procedure Validate_Simple_Pool_Op_Formal
6355 (Pool_Op : Entity_Id;
6356 Pool_Op_Formal : in out Entity_Id;
6357 Expected_Mode : Formal_Kind;
6358 Expected_Type : Entity_Id;
6359 Formal_Name : String;
6360 OK_Formal : in out Boolean)
6362 begin
6363 -- If OK_Formal is False on entry, then simply ignore
6364 -- the formal, because an earlier formal has already
6365 -- been flagged.
6367 if not OK_Formal then
6368 return;
6370 -- If no formal is passed in, then issue an error for a
6371 -- missing formal.
6373 elsif not Present (Pool_Op_Formal) then
6374 Error_Msg_NE
6375 ("simple storage pool op missing formal " &
6376 Formal_Name & " of type&", Pool_Op, Expected_Type);
6377 OK_Formal := False;
6379 return;
6380 end if;
6382 if Etype (Pool_Op_Formal) /= Expected_Type then
6384 -- If the pool type was expected for this formal, then
6385 -- this will not be considered a candidate operation
6386 -- for the simple pool, so we unset OK_Formal so that
6387 -- the op and any later formals will be ignored.
6389 if Expected_Type = Pool_Type then
6390 OK_Formal := False;
6392 return;
6394 else
6395 Error_Msg_NE
6396 ("wrong type for formal " & Formal_Name &
6397 " of simple storage pool op; expected type&",
6398 Pool_Op_Formal, Expected_Type);
6399 end if;
6400 end if;
6402 -- Issue error if formal's mode is not the expected one
6404 if Ekind (Pool_Op_Formal) /= Expected_Mode then
6405 Error_Msg_N
6406 ("wrong mode for formal of simple storage pool op",
6407 Pool_Op_Formal);
6408 end if;
6410 -- Advance to the next formal
6412 Next_Formal (Pool_Op_Formal);
6413 end Validate_Simple_Pool_Op_Formal;
6415 ------------------------------------
6416 -- Validate_Simple_Pool_Operation --
6417 ------------------------------------
6419 procedure Validate_Simple_Pool_Operation
6420 (Op_Name : Name_Id)
6422 Op : Entity_Id;
6423 Found_Op : Entity_Id := Empty;
6424 Formal : Entity_Id;
6425 Is_OK : Boolean;
6427 begin
6428 pragma Assert
6429 (Nam_In (Op_Name, Name_Allocate,
6430 Name_Deallocate,
6431 Name_Storage_Size));
6433 Error_Msg_Name_1 := Op_Name;
6435 -- For each homonym declared immediately in the scope
6436 -- of the simple storage pool type, determine whether
6437 -- the homonym is an operation of the pool type, and,
6438 -- if so, check that its profile is as expected for
6439 -- a simple pool operation of that name.
6441 Op := Get_Name_Entity_Id (Op_Name);
6442 while Present (Op) loop
6443 if Ekind_In (Op, E_Function, E_Procedure)
6444 and then Scope (Op) = Current_Scope
6445 then
6446 Formal := First_Entity (Op);
6448 Is_OK := True;
6450 -- The first parameter must be of the pool type
6451 -- in order for the operation to qualify.
6453 if Op_Name = Name_Storage_Size then
6454 Validate_Simple_Pool_Op_Formal
6455 (Op, Formal, E_In_Parameter, Pool_Type,
6456 "Pool", Is_OK);
6457 else
6458 Validate_Simple_Pool_Op_Formal
6459 (Op, Formal, E_In_Out_Parameter, Pool_Type,
6460 "Pool", Is_OK);
6461 end if;
6463 -- If another operation with this name has already
6464 -- been located for the type, then flag an error,
6465 -- since we only allow the type to have a single
6466 -- such primitive.
6468 if Present (Found_Op) and then Is_OK then
6469 Error_Msg_NE
6470 ("only one % operation allowed for " &
6471 "simple storage pool type&", Op, Pool_Type);
6472 end if;
6474 -- In the case of Allocate and Deallocate, a formal
6475 -- of type System.Address is required.
6477 if Op_Name = Name_Allocate then
6478 Validate_Simple_Pool_Op_Formal
6479 (Op, Formal, E_Out_Parameter,
6480 Address_Type, "Storage_Address", Is_OK);
6482 elsif Op_Name = Name_Deallocate then
6483 Validate_Simple_Pool_Op_Formal
6484 (Op, Formal, E_In_Parameter,
6485 Address_Type, "Storage_Address", Is_OK);
6486 end if;
6488 -- In the case of Allocate and Deallocate, formals
6489 -- of type Storage_Count are required as the third
6490 -- and fourth parameters.
6492 if Op_Name /= Name_Storage_Size then
6493 Validate_Simple_Pool_Op_Formal
6494 (Op, Formal, E_In_Parameter,
6495 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
6496 Validate_Simple_Pool_Op_Formal
6497 (Op, Formal, E_In_Parameter,
6498 Stg_Cnt_Type, "Alignment", Is_OK);
6499 end if;
6501 -- If no mismatched formals have been found (Is_OK)
6502 -- and no excess formals are present, then this
6503 -- operation has been validated, so record it.
6505 if not Present (Formal) and then Is_OK then
6506 Found_Op := Op;
6507 end if;
6508 end if;
6510 Op := Homonym (Op);
6511 end loop;
6513 -- There must be a valid Allocate operation for the type,
6514 -- so issue an error if none was found.
6516 if Op_Name = Name_Allocate
6517 and then not Present (Found_Op)
6518 then
6519 Error_Msg_N ("missing % operation for simple " &
6520 "storage pool type", Pool_Type);
6522 elsif Present (Found_Op) then
6524 -- Simple pool operations can't be abstract
6526 if Is_Abstract_Subprogram (Found_Op) then
6527 Error_Msg_N
6528 ("simple storage pool operation must not be " &
6529 "abstract", Found_Op);
6530 end if;
6532 -- The Storage_Size operation must be a function with
6533 -- Storage_Count as its result type.
6535 if Op_Name = Name_Storage_Size then
6536 if Ekind (Found_Op) = E_Procedure then
6537 Error_Msg_N
6538 ("% operation must be a function", Found_Op);
6540 elsif Etype (Found_Op) /= Stg_Cnt_Type then
6541 Error_Msg_NE
6542 ("wrong result type for%, expected type&",
6543 Found_Op, Stg_Cnt_Type);
6544 end if;
6546 -- Allocate and Deallocate must be procedures
6548 elsif Ekind (Found_Op) = E_Function then
6549 Error_Msg_N
6550 ("% operation must be a procedure", Found_Op);
6551 end if;
6552 end if;
6553 end Validate_Simple_Pool_Operation;
6555 -- Start of processing for Validate_Simple_Pool_Ops
6557 begin
6558 Validate_Simple_Pool_Operation (Name_Allocate);
6559 Validate_Simple_Pool_Operation (Name_Deallocate);
6560 Validate_Simple_Pool_Operation (Name_Storage_Size);
6561 end Validate_Simple_Pool_Ops;
6562 end if;
6563 end if;
6565 -- Now that all types from which E may depend are frozen, see if the
6566 -- size is known at compile time, if it must be unsigned, or if
6567 -- strict alignment is required
6569 Check_Compile_Time_Size (E);
6570 Check_Unsigned_Type (E);
6572 if Base_Type (E) = E then
6573 Check_Strict_Alignment (E);
6574 end if;
6576 -- Do not allow a size clause for a type which does not have a size
6577 -- that is known at compile time
6579 if Has_Size_Clause (E)
6580 and then not Size_Known_At_Compile_Time (E)
6581 then
6582 -- Suppress this message if errors posted on E, even if we are
6583 -- in all errors mode, since this is often a junk message
6585 if not Error_Posted (E) then
6586 Error_Msg_N
6587 ("size clause not allowed for variable length type",
6588 Size_Clause (E));
6589 end if;
6590 end if;
6592 -- Now we set/verify the representation information, in particular
6593 -- the size and alignment values. This processing is not required for
6594 -- generic types, since generic types do not play any part in code
6595 -- generation, and so the size and alignment values for such types
6596 -- are irrelevant. Ditto for types declared within a generic unit,
6597 -- which may have components that depend on generic parameters, and
6598 -- that will be recreated in an instance.
6600 if Inside_A_Generic then
6601 null;
6603 -- Otherwise we call the layout procedure
6605 else
6606 Layout_Type (E);
6607 end if;
6609 -- If this is an access to subprogram whose designated type is itself
6610 -- a subprogram type, the return type of this anonymous subprogram
6611 -- type must be decorated as well.
6613 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
6614 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
6615 then
6616 Layout_Type (Etype (Designated_Type (E)));
6617 end if;
6619 -- If the type has a Defaut_Value/Default_Component_Value aspect,
6620 -- this is where we analye the expression (after the type is frozen,
6621 -- since in the case of Default_Value, we are analyzing with the
6622 -- type itself, and we treat Default_Component_Value similarly for
6623 -- the sake of uniformity).
6625 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
6626 declare
6627 Nam : Name_Id;
6628 Exp : Node_Id;
6629 Typ : Entity_Id;
6631 begin
6632 if Is_Scalar_Type (E) then
6633 Nam := Name_Default_Value;
6634 Typ := E;
6635 Exp := Default_Aspect_Value (Typ);
6636 else
6637 Nam := Name_Default_Component_Value;
6638 Typ := Component_Type (E);
6639 Exp := Default_Aspect_Component_Value (E);
6640 end if;
6642 Analyze_And_Resolve (Exp, Typ);
6644 if Etype (Exp) /= Any_Type then
6645 if not Is_OK_Static_Expression (Exp) then
6646 Error_Msg_Name_1 := Nam;
6647 Flag_Non_Static_Expr
6648 ("aspect% requires static expression", Exp);
6649 end if;
6650 end if;
6651 end;
6652 end if;
6654 -- End of freeze processing for type entities
6655 end if;
6657 -- Here is where we logically freeze the current entity. If it has a
6658 -- freeze node, then this is the point at which the freeze node is
6659 -- linked into the result list.
6661 if Has_Delayed_Freeze (E) then
6663 -- If a freeze node is already allocated, use it, otherwise allocate
6664 -- a new one. The preallocation happens in the case of anonymous base
6665 -- types, where we preallocate so that we can set First_Subtype_Link.
6666 -- Note that we reset the Sloc to the current freeze location.
6668 if Present (Freeze_Node (E)) then
6669 F_Node := Freeze_Node (E);
6670 Set_Sloc (F_Node, Loc);
6672 else
6673 F_Node := New_Node (N_Freeze_Entity, Loc);
6674 Set_Freeze_Node (E, F_Node);
6675 Set_Access_Types_To_Process (F_Node, No_Elist);
6676 Set_TSS_Elist (F_Node, No_Elist);
6677 Set_Actions (F_Node, No_List);
6678 end if;
6680 Set_Entity (F_Node, E);
6681 Add_To_Result (F_Node);
6683 -- A final pass over record types with discriminants. If the type
6684 -- has an incomplete declaration, there may be constrained access
6685 -- subtypes declared elsewhere, which do not depend on the discrimi-
6686 -- nants of the type, and which are used as component types (i.e.
6687 -- the full view is a recursive type). The designated types of these
6688 -- subtypes can only be elaborated after the type itself, and they
6689 -- need an itype reference.
6691 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
6692 declare
6693 Comp : Entity_Id;
6694 IR : Node_Id;
6695 Typ : Entity_Id;
6697 begin
6698 Comp := First_Component (E);
6699 while Present (Comp) loop
6700 Typ := Etype (Comp);
6702 if Ekind (Comp) = E_Component
6703 and then Is_Access_Type (Typ)
6704 and then Scope (Typ) /= E
6705 and then Base_Type (Designated_Type (Typ)) = E
6706 and then Is_Itype (Designated_Type (Typ))
6707 then
6708 IR := Make_Itype_Reference (Sloc (Comp));
6709 Set_Itype (IR, Designated_Type (Typ));
6710 Append (IR, Result);
6711 end if;
6713 Next_Component (Comp);
6714 end loop;
6715 end;
6716 end if;
6717 end if;
6719 -- When a type is frozen, the first subtype of the type is frozen as
6720 -- well (RM 13.14(15)). This has to be done after freezing the type,
6721 -- since obviously the first subtype depends on its own base type.
6723 if Is_Type (E) then
6724 Freeze_And_Append (First_Subtype (E), N, Result);
6726 -- If we just froze a tagged non-class wide record, then freeze the
6727 -- corresponding class-wide type. This must be done after the tagged
6728 -- type itself is frozen, because the class-wide type refers to the
6729 -- tagged type which generates the class.
6731 if Is_Tagged_Type (E)
6732 and then not Is_Class_Wide_Type (E)
6733 and then Present (Class_Wide_Type (E))
6734 then
6735 Freeze_And_Append (Class_Wide_Type (E), N, Result);
6736 end if;
6737 end if;
6739 Check_Debug_Info_Needed (E);
6741 -- Special handling for subprograms
6743 if Is_Subprogram (E) then
6745 -- If subprogram has address clause then reset Is_Public flag, since
6746 -- we do not want the backend to generate external references.
6748 if Present (Address_Clause (E))
6749 and then not Is_Library_Level_Entity (E)
6750 then
6751 Set_Is_Public (E, False);
6752 end if;
6753 end if;
6755 <<Leave>>
6756 Restore_Ghost_Mode (Saved_GM);
6758 return Result;
6759 end Freeze_Entity;
6761 -----------------------------
6762 -- Freeze_Enumeration_Type --
6763 -----------------------------
6765 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6766 begin
6767 -- By default, if no size clause is present, an enumeration type with
6768 -- Convention C is assumed to interface to a C enum, and has integer
6769 -- size. This applies to types. For subtypes, verify that its base
6770 -- type has no size clause either. Treat other foreign conventions
6771 -- in the same way, and also make sure alignment is set right.
6773 if Has_Foreign_Convention (Typ)
6774 and then not Has_Size_Clause (Typ)
6775 and then not Has_Size_Clause (Base_Type (Typ))
6776 and then Esize (Typ) < Standard_Integer_Size
6778 -- Don't do this if Short_Enums on target
6780 and then not Target_Short_Enums
6781 then
6782 Init_Esize (Typ, Standard_Integer_Size);
6783 Set_Alignment (Typ, Alignment (Standard_Integer));
6785 -- Normal Ada case or size clause present or not Long_C_Enums on target
6787 else
6788 -- If the enumeration type interfaces to C, and it has a size clause
6789 -- that specifies less than int size, it warrants a warning. The
6790 -- user may intend the C type to be an enum or a char, so this is
6791 -- not by itself an error that the Ada compiler can detect, but it
6792 -- it is a worth a heads-up. For Boolean and Character types we
6793 -- assume that the programmer has the proper C type in mind.
6795 if Convention (Typ) = Convention_C
6796 and then Has_Size_Clause (Typ)
6797 and then Esize (Typ) /= Esize (Standard_Integer)
6798 and then not Is_Boolean_Type (Typ)
6799 and then not Is_Character_Type (Typ)
6801 -- Don't do this if Short_Enums on target
6803 and then not Target_Short_Enums
6804 then
6805 Error_Msg_N
6806 ("C enum types have the size of a C int??", Size_Clause (Typ));
6807 end if;
6809 Adjust_Esize_For_Alignment (Typ);
6810 end if;
6811 end Freeze_Enumeration_Type;
6813 -----------------------
6814 -- Freeze_Expression --
6815 -----------------------
6817 procedure Freeze_Expression (N : Node_Id) is
6818 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6819 Typ : Entity_Id;
6820 Nam : Entity_Id;
6821 Desig_Typ : Entity_Id;
6822 P : Node_Id;
6823 Parent_P : Node_Id;
6825 Freeze_Outside : Boolean := False;
6826 -- This flag is set true if the entity must be frozen outside the
6827 -- current subprogram. This happens in the case of expander generated
6828 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6829 -- not freeze all entities like other bodies, but which nevertheless
6830 -- may reference entities that have to be frozen before the body and
6831 -- obviously cannot be frozen inside the body.
6833 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6834 -- If the expression is an array aggregate, the type of the component
6835 -- expressions is also frozen. If the component type is an access type
6836 -- and the expressions include allocators, the designed type is frozen
6837 -- as well.
6839 function In_Expanded_Body (N : Node_Id) return Boolean;
6840 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6841 -- it is the handled statement sequence of an expander-generated
6842 -- subprogram (init proc, stream subprogram, or renaming as body).
6843 -- If so, this is not a freezing context.
6845 -----------------------------------------
6846 -- Find_Aggregate_Component_Desig_Type --
6847 -----------------------------------------
6849 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6850 Assoc : Node_Id;
6851 Exp : Node_Id;
6853 begin
6854 if Present (Expressions (N)) then
6855 Exp := First (Expressions (N));
6856 while Present (Exp) loop
6857 if Nkind (Exp) = N_Allocator then
6858 return Designated_Type (Component_Type (Etype (N)));
6859 end if;
6861 Next (Exp);
6862 end loop;
6863 end if;
6865 if Present (Component_Associations (N)) then
6866 Assoc := First (Component_Associations (N));
6867 while Present (Assoc) loop
6868 if Nkind (Expression (Assoc)) = N_Allocator then
6869 return Designated_Type (Component_Type (Etype (N)));
6870 end if;
6872 Next (Assoc);
6873 end loop;
6874 end if;
6876 return Empty;
6877 end Find_Aggregate_Component_Desig_Type;
6879 ----------------------
6880 -- In_Expanded_Body --
6881 ----------------------
6883 function In_Expanded_Body (N : Node_Id) return Boolean is
6884 P : Node_Id;
6885 Id : Entity_Id;
6887 begin
6888 if Nkind (N) = N_Subprogram_Body then
6889 P := N;
6890 else
6891 P := Parent (N);
6892 end if;
6894 if Nkind (P) /= N_Subprogram_Body then
6895 return False;
6897 else
6898 Id := Defining_Unit_Name (Specification (P));
6900 -- The following are expander-created bodies, or bodies that
6901 -- are not freeze points.
6903 if Nkind (Id) = N_Defining_Identifier
6904 and then (Is_Init_Proc (Id)
6905 or else Is_TSS (Id, TSS_Stream_Input)
6906 or else Is_TSS (Id, TSS_Stream_Output)
6907 or else Is_TSS (Id, TSS_Stream_Read)
6908 or else Is_TSS (Id, TSS_Stream_Write)
6909 or else Nkind_In (Original_Node (P),
6910 N_Subprogram_Renaming_Declaration,
6911 N_Expression_Function))
6912 then
6913 return True;
6914 else
6915 return False;
6916 end if;
6917 end if;
6918 end In_Expanded_Body;
6920 -- Start of processing for Freeze_Expression
6922 begin
6923 -- Immediate return if freezing is inhibited. This flag is set by the
6924 -- analyzer to stop freezing on generated expressions that would cause
6925 -- freezing if they were in the source program, but which are not
6926 -- supposed to freeze, since they are created.
6928 if Must_Not_Freeze (N) then
6929 return;
6930 end if;
6932 -- If expression is non-static, then it does not freeze in a default
6933 -- expression, see section "Handling of Default Expressions" in the
6934 -- spec of package Sem for further details. Note that we have to make
6935 -- sure that we actually have a real expression (if we have a subtype
6936 -- indication, we can't test Is_OK_Static_Expression). However, we
6937 -- exclude the case of the prefix of an attribute of a static scalar
6938 -- subtype from this early return, because static subtype attributes
6939 -- should always cause freezing, even in default expressions, but
6940 -- the attribute may not have been marked as static yet (because in
6941 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
6942 -- Freeze_Expression on the prefix).
6944 if In_Spec_Exp
6945 and then Nkind (N) in N_Subexpr
6946 and then not Is_OK_Static_Expression (N)
6947 and then (Nkind (Parent (N)) /= N_Attribute_Reference
6948 or else not (Is_Entity_Name (N)
6949 and then Is_Type (Entity (N))
6950 and then Is_OK_Static_Subtype (Entity (N))))
6951 then
6952 return;
6953 end if;
6955 -- Freeze type of expression if not frozen already
6957 Typ := Empty;
6959 if Nkind (N) in N_Has_Etype then
6960 if not Is_Frozen (Etype (N)) then
6961 Typ := Etype (N);
6963 -- Base type may be an derived numeric type that is frozen at
6964 -- the point of declaration, but first_subtype is still unfrozen.
6966 elsif not Is_Frozen (First_Subtype (Etype (N))) then
6967 Typ := First_Subtype (Etype (N));
6968 end if;
6969 end if;
6971 -- For entity name, freeze entity if not frozen already. A special
6972 -- exception occurs for an identifier that did not come from source.
6973 -- We don't let such identifiers freeze a non-internal entity, i.e.
6974 -- an entity that did come from source, since such an identifier was
6975 -- generated by the expander, and cannot have any semantic effect on
6976 -- the freezing semantics. For example, this stops the parameter of
6977 -- an initialization procedure from freezing the variable.
6979 if Is_Entity_Name (N)
6980 and then not Is_Frozen (Entity (N))
6981 and then (Nkind (N) /= N_Identifier
6982 or else Comes_From_Source (N)
6983 or else not Comes_From_Source (Entity (N)))
6984 then
6985 Nam := Entity (N);
6987 if Present (Nam) and then Ekind (Nam) = E_Function then
6988 Check_Expression_Function (N, Nam);
6989 end if;
6991 else
6992 Nam := Empty;
6993 end if;
6995 -- For an allocator freeze designated type if not frozen already
6997 -- For an aggregate whose component type is an access type, freeze the
6998 -- designated type now, so that its freeze does not appear within the
6999 -- loop that might be created in the expansion of the aggregate. If the
7000 -- designated type is a private type without full view, the expression
7001 -- cannot contain an allocator, so the type is not frozen.
7003 -- For a function, we freeze the entity when the subprogram declaration
7004 -- is frozen, but a function call may appear in an initialization proc.
7005 -- before the declaration is frozen. We need to generate the extra
7006 -- formals, if any, to ensure that the expansion of the call includes
7007 -- the proper actuals. This only applies to Ada subprograms, not to
7008 -- imported ones.
7010 Desig_Typ := Empty;
7012 case Nkind (N) is
7013 when N_Allocator =>
7014 Desig_Typ := Designated_Type (Etype (N));
7016 when N_Aggregate =>
7017 if Is_Array_Type (Etype (N))
7018 and then Is_Access_Type (Component_Type (Etype (N)))
7019 then
7021 -- Check whether aggregate includes allocators.
7023 Desig_Typ := Find_Aggregate_Component_Desig_Type;
7024 end if;
7026 when N_Indexed_Component
7027 | N_Selected_Component
7028 | N_Slice
7030 if Is_Access_Type (Etype (Prefix (N))) then
7031 Desig_Typ := Designated_Type (Etype (Prefix (N)));
7032 end if;
7034 when N_Identifier =>
7035 if Present (Nam)
7036 and then Ekind (Nam) = E_Function
7037 and then Nkind (Parent (N)) = N_Function_Call
7038 and then Convention (Nam) = Convention_Ada
7039 then
7040 Create_Extra_Formals (Nam);
7041 end if;
7043 when others =>
7044 null;
7045 end case;
7047 if Desig_Typ /= Empty
7048 and then (Is_Frozen (Desig_Typ)
7049 or else (not Is_Fully_Defined (Desig_Typ)))
7050 then
7051 Desig_Typ := Empty;
7052 end if;
7054 -- All done if nothing needs freezing
7056 if No (Typ)
7057 and then No (Nam)
7058 and then No (Desig_Typ)
7059 then
7060 return;
7061 end if;
7063 -- Examine the enclosing context by climbing the parent chain. The
7064 -- traversal serves two purposes - to detect scenarios where freezeing
7065 -- is not needed and to find the proper insertion point for the freeze
7066 -- nodes. Although somewhat similar to Insert_Actions, this traversal
7067 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
7068 -- the tree may result in types being frozen too early.
7070 P := N;
7071 loop
7072 Parent_P := Parent (P);
7074 -- If we don't have a parent, then we are not in a well-formed tree.
7075 -- This is an unusual case, but there are some legitimate situations
7076 -- in which this occurs, notably when the expressions in the range of
7077 -- a type declaration are resolved. We simply ignore the freeze
7078 -- request in this case. Is this right ???
7080 if No (Parent_P) then
7081 return;
7082 end if;
7084 -- See if we have got to an appropriate point in the tree
7086 case Nkind (Parent_P) is
7088 -- A special test for the exception of (RM 13.14(8)) for the case
7089 -- of per-object expressions (RM 3.8(18)) occurring in component
7090 -- definition or a discrete subtype definition. Note that we test
7091 -- for a component declaration which includes both cases we are
7092 -- interested in, and furthermore the tree does not have explicit
7093 -- nodes for either of these two constructs.
7095 when N_Component_Declaration =>
7097 -- The case we want to test for here is an identifier that is
7098 -- a per-object expression, this is either a discriminant that
7099 -- appears in a context other than the component declaration
7100 -- or it is a reference to the type of the enclosing construct.
7102 -- For either of these cases, we skip the freezing
7104 if not In_Spec_Expression
7105 and then Nkind (N) = N_Identifier
7106 and then (Present (Entity (N)))
7107 then
7108 -- We recognize the discriminant case by just looking for
7109 -- a reference to a discriminant. It can only be one for
7110 -- the enclosing construct. Skip freezing in this case.
7112 if Ekind (Entity (N)) = E_Discriminant then
7113 return;
7115 -- For the case of a reference to the enclosing record,
7116 -- (or task or protected type), we look for a type that
7117 -- matches the current scope.
7119 elsif Entity (N) = Current_Scope then
7120 return;
7121 end if;
7122 end if;
7124 -- If we have an enumeration literal that appears as the choice in
7125 -- the aggregate of an enumeration representation clause, then
7126 -- freezing does not occur (RM 13.14(10)).
7128 when N_Enumeration_Representation_Clause =>
7130 -- The case we are looking for is an enumeration literal
7132 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
7133 and then Is_Enumeration_Type (Etype (N))
7134 then
7135 -- If enumeration literal appears directly as the choice,
7136 -- do not freeze (this is the normal non-overloaded case)
7138 if Nkind (Parent (N)) = N_Component_Association
7139 and then First (Choices (Parent (N))) = N
7140 then
7141 return;
7143 -- If enumeration literal appears as the name of function
7144 -- which is the choice, then also do not freeze. This
7145 -- happens in the overloaded literal case, where the
7146 -- enumeration literal is temporarily changed to a function
7147 -- call for overloading analysis purposes.
7149 elsif Nkind (Parent (N)) = N_Function_Call
7150 and then
7151 Nkind (Parent (Parent (N))) = N_Component_Association
7152 and then
7153 First (Choices (Parent (Parent (N)))) = Parent (N)
7154 then
7155 return;
7156 end if;
7157 end if;
7159 -- Normally if the parent is a handled sequence of statements,
7160 -- then the current node must be a statement, and that is an
7161 -- appropriate place to insert a freeze node.
7163 when N_Handled_Sequence_Of_Statements =>
7165 -- An exception occurs when the sequence of statements is for
7166 -- an expander generated body that did not do the usual freeze
7167 -- all operation. In this case we usually want to freeze
7168 -- outside this body, not inside it, and we skip past the
7169 -- subprogram body that we are inside.
7171 if In_Expanded_Body (Parent_P) then
7172 declare
7173 Subp : constant Node_Id := Parent (Parent_P);
7174 Spec : Entity_Id;
7176 begin
7177 -- Freeze the entity only when it is declared inside the
7178 -- body of the expander generated procedure. This case
7179 -- is recognized by the scope of the entity or its type,
7180 -- which is either the spec for some enclosing body, or
7181 -- (in the case of init_procs, for which there are no
7182 -- separate specs) the current scope.
7184 if Nkind (Subp) = N_Subprogram_Body then
7185 Spec := Corresponding_Spec (Subp);
7187 if (Present (Typ) and then Scope (Typ) = Spec)
7188 or else
7189 (Present (Nam) and then Scope (Nam) = Spec)
7190 then
7191 exit;
7193 elsif Present (Typ)
7194 and then Scope (Typ) = Current_Scope
7195 and then Defining_Entity (Subp) = Current_Scope
7196 then
7197 exit;
7198 end if;
7199 end if;
7201 -- An expression function may act as a completion of
7202 -- a function declaration. As such, it can reference
7203 -- entities declared between the two views:
7205 -- Hidden []; -- 1
7206 -- function F return ...;
7207 -- private
7208 -- function Hidden return ...;
7209 -- function F return ... is (Hidden); -- 2
7211 -- Refering to the example above, freezing the expression
7212 -- of F (2) would place Hidden's freeze node (1) in the
7213 -- wrong place. Avoid explicit freezing and let the usual
7214 -- scenarios do the job - for example, reaching the end
7215 -- of the private declarations, or a call to F.
7217 if Nkind (Original_Node (Subp)) =
7218 N_Expression_Function
7219 then
7220 null;
7222 -- Freeze outside the body
7224 else
7225 Parent_P := Parent (Parent_P);
7226 Freeze_Outside := True;
7227 end if;
7228 end;
7230 -- Here if normal case where we are in handled statement
7231 -- sequence and want to do the insertion right there.
7233 else
7234 exit;
7235 end if;
7237 -- If parent is a body or a spec or a block, then the current node
7238 -- is a statement or declaration and we can insert the freeze node
7239 -- before it.
7241 when N_Block_Statement
7242 | N_Entry_Body
7243 | N_Package_Body
7244 | N_Package_Specification
7245 | N_Protected_Body
7246 | N_Subprogram_Body
7247 | N_Task_Body
7249 exit;
7251 -- The expander is allowed to define types in any statements list,
7252 -- so any of the following parent nodes also mark a freezing point
7253 -- if the actual node is in a list of statements or declarations.
7255 when N_Abortable_Part
7256 | N_Accept_Alternative
7257 | N_And_Then
7258 | N_Case_Statement_Alternative
7259 | N_Compilation_Unit_Aux
7260 | N_Conditional_Entry_Call
7261 | N_Delay_Alternative
7262 | N_Elsif_Part
7263 | N_Entry_Call_Alternative
7264 | N_Exception_Handler
7265 | N_Extended_Return_Statement
7266 | N_Freeze_Entity
7267 | N_If_Statement
7268 | N_Or_Else
7269 | N_Selective_Accept
7270 | N_Triggering_Alternative
7272 exit when Is_List_Member (P);
7274 -- Freeze nodes produced by an expression coming from the Actions
7275 -- list of a N_Expression_With_Actions node must remain within the
7276 -- Actions list. Inserting the freeze nodes further up the tree
7277 -- may lead to use before declaration issues in the case of array
7278 -- types.
7280 when N_Expression_With_Actions =>
7281 if Is_List_Member (P)
7282 and then List_Containing (P) = Actions (Parent_P)
7283 then
7284 exit;
7285 end if;
7287 -- Note: N_Loop_Statement is a special case. A type that appears
7288 -- in the source can never be frozen in a loop (this occurs only
7289 -- because of a loop expanded by the expander), so we keep on
7290 -- going. Otherwise we terminate the search. Same is true of any
7291 -- entity which comes from source. (if they have predefined type,
7292 -- that type does not appear to come from source, but the entity
7293 -- should not be frozen here).
7295 when N_Loop_Statement =>
7296 exit when not Comes_From_Source (Etype (N))
7297 and then (No (Nam) or else not Comes_From_Source (Nam));
7299 -- For all other cases, keep looking at parents
7301 when others =>
7302 null;
7303 end case;
7305 -- We fall through the case if we did not yet find the proper
7306 -- place in the free for inserting the freeze node, so climb.
7308 P := Parent_P;
7309 end loop;
7311 -- If the expression appears in a record or an initialization procedure,
7312 -- the freeze nodes are collected and attached to the current scope, to
7313 -- be inserted and analyzed on exit from the scope, to insure that
7314 -- generated entities appear in the correct scope. If the expression is
7315 -- a default for a discriminant specification, the scope is still void.
7316 -- The expression can also appear in the discriminant part of a private
7317 -- or concurrent type.
7319 -- If the expression appears in a constrained subcomponent of an
7320 -- enclosing record declaration, the freeze nodes must be attached to
7321 -- the outer record type so they can eventually be placed in the
7322 -- enclosing declaration list.
7324 -- The other case requiring this special handling is if we are in a
7325 -- default expression, since in that case we are about to freeze a
7326 -- static type, and the freeze scope needs to be the outer scope, not
7327 -- the scope of the subprogram with the default parameter.
7329 -- For default expressions and other spec expressions in generic units,
7330 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
7331 -- placing them at the proper place, after the generic unit.
7333 if (In_Spec_Exp and not Inside_A_Generic)
7334 or else Freeze_Outside
7335 or else (Is_Type (Current_Scope)
7336 and then (not Is_Concurrent_Type (Current_Scope)
7337 or else not Has_Completion (Current_Scope)))
7338 or else Ekind (Current_Scope) = E_Void
7339 then
7340 declare
7341 N : constant Node_Id := Current_Scope;
7342 Freeze_Nodes : List_Id := No_List;
7343 Pos : Int := Scope_Stack.Last;
7345 begin
7346 if Present (Desig_Typ) then
7347 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
7348 end if;
7350 if Present (Typ) then
7351 Freeze_And_Append (Typ, N, Freeze_Nodes);
7352 end if;
7354 if Present (Nam) then
7355 Freeze_And_Append (Nam, N, Freeze_Nodes);
7356 end if;
7358 -- The current scope may be that of a constrained component of
7359 -- an enclosing record declaration, or of a loop of an enclosing
7360 -- quantified expression, which is above the current scope in the
7361 -- scope stack. Indeed in the context of a quantified expression,
7362 -- a scope is created and pushed above the current scope in order
7363 -- to emulate the loop-like behavior of the quantified expression.
7364 -- If the expression is within a top-level pragma, as for a pre-
7365 -- condition on a library-level subprogram, nothing to do.
7367 if not Is_Compilation_Unit (Current_Scope)
7368 and then (Is_Record_Type (Scope (Current_Scope))
7369 or else Nkind (Parent (Current_Scope)) =
7370 N_Quantified_Expression)
7371 then
7372 Pos := Pos - 1;
7373 end if;
7375 if Is_Non_Empty_List (Freeze_Nodes) then
7376 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
7377 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
7378 Freeze_Nodes;
7379 else
7380 Append_List (Freeze_Nodes,
7381 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
7382 end if;
7383 end if;
7384 end;
7386 return;
7387 end if;
7389 -- Now we have the right place to do the freezing. First, a special
7390 -- adjustment, if we are in spec-expression analysis mode, these freeze
7391 -- actions must not be thrown away (normally all inserted actions are
7392 -- thrown away in this mode. However, the freeze actions are from static
7393 -- expressions and one of the important reasons we are doing this
7394 -- special analysis is to get these freeze actions. Therefore we turn
7395 -- off the In_Spec_Expression mode to propagate these freeze actions.
7396 -- This also means they get properly analyzed and expanded.
7398 In_Spec_Expression := False;
7400 -- Freeze the designated type of an allocator (RM 13.14(13))
7402 if Present (Desig_Typ) then
7403 Freeze_Before (P, Desig_Typ);
7404 end if;
7406 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
7407 -- the enumeration representation clause exception in the loop above.
7409 if Present (Typ) then
7410 Freeze_Before (P, Typ);
7411 end if;
7413 -- Freeze name if one is present (RM 13.14(11))
7415 if Present (Nam) then
7416 Freeze_Before (P, Nam);
7417 end if;
7419 -- Restore In_Spec_Expression flag
7421 In_Spec_Expression := In_Spec_Exp;
7422 end Freeze_Expression;
7424 -----------------------------
7425 -- Freeze_Fixed_Point_Type --
7426 -----------------------------
7428 -- Certain fixed-point types and subtypes, including implicit base types
7429 -- and declared first subtypes, have not yet set up a range. This is
7430 -- because the range cannot be set until the Small and Size values are
7431 -- known, and these are not known till the type is frozen.
7433 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
7434 -- whose bounds are unanalyzed real literals. This routine will recognize
7435 -- this case, and transform this range node into a properly typed range
7436 -- with properly analyzed and resolved values.
7438 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
7439 Rng : constant Node_Id := Scalar_Range (Typ);
7440 Lo : constant Node_Id := Low_Bound (Rng);
7441 Hi : constant Node_Id := High_Bound (Rng);
7442 Btyp : constant Entity_Id := Base_Type (Typ);
7443 Brng : constant Node_Id := Scalar_Range (Btyp);
7444 BLo : constant Node_Id := Low_Bound (Brng);
7445 BHi : constant Node_Id := High_Bound (Brng);
7446 Small : constant Ureal := Small_Value (Typ);
7447 Loval : Ureal;
7448 Hival : Ureal;
7449 Atype : Entity_Id;
7451 Orig_Lo : Ureal;
7452 Orig_Hi : Ureal;
7453 -- Save original bounds (for shaving tests)
7455 Actual_Size : Nat;
7456 -- Actual size chosen
7458 function Fsize (Lov, Hiv : Ureal) return Nat;
7459 -- Returns size of type with given bounds. Also leaves these
7460 -- bounds set as the current bounds of the Typ.
7462 -----------
7463 -- Fsize --
7464 -----------
7466 function Fsize (Lov, Hiv : Ureal) return Nat is
7467 begin
7468 Set_Realval (Lo, Lov);
7469 Set_Realval (Hi, Hiv);
7470 return Minimum_Size (Typ);
7471 end Fsize;
7473 -- Start of processing for Freeze_Fixed_Point_Type
7475 begin
7476 -- If Esize of a subtype has not previously been set, set it now
7478 if Unknown_Esize (Typ) then
7479 Atype := Ancestor_Subtype (Typ);
7481 if Present (Atype) then
7482 Set_Esize (Typ, Esize (Atype));
7483 else
7484 Set_Esize (Typ, Esize (Base_Type (Typ)));
7485 end if;
7486 end if;
7488 -- Immediate return if the range is already analyzed. This means that
7489 -- the range is already set, and does not need to be computed by this
7490 -- routine.
7492 if Analyzed (Rng) then
7493 return;
7494 end if;
7496 -- Immediate return if either of the bounds raises Constraint_Error
7498 if Raises_Constraint_Error (Lo)
7499 or else Raises_Constraint_Error (Hi)
7500 then
7501 return;
7502 end if;
7504 Loval := Realval (Lo);
7505 Hival := Realval (Hi);
7507 Orig_Lo := Loval;
7508 Orig_Hi := Hival;
7510 -- Ordinary fixed-point case
7512 if Is_Ordinary_Fixed_Point_Type (Typ) then
7514 -- For the ordinary fixed-point case, we are allowed to fudge the
7515 -- end-points up or down by small. Generally we prefer to fudge up,
7516 -- i.e. widen the bounds for non-model numbers so that the end points
7517 -- are included. However there are cases in which this cannot be
7518 -- done, and indeed cases in which we may need to narrow the bounds.
7519 -- The following circuit makes the decision.
7521 -- Note: our terminology here is that Incl_EP means that the bounds
7522 -- are widened by Small if necessary to include the end points, and
7523 -- Excl_EP means that the bounds are narrowed by Small to exclude the
7524 -- end-points if this reduces the size.
7526 -- Note that in the Incl case, all we care about is including the
7527 -- end-points. In the Excl case, we want to narrow the bounds as
7528 -- much as permitted by the RM, to give the smallest possible size.
7530 Fudge : declare
7531 Loval_Incl_EP : Ureal;
7532 Hival_Incl_EP : Ureal;
7534 Loval_Excl_EP : Ureal;
7535 Hival_Excl_EP : Ureal;
7537 Size_Incl_EP : Nat;
7538 Size_Excl_EP : Nat;
7540 Model_Num : Ureal;
7541 First_Subt : Entity_Id;
7542 Actual_Lo : Ureal;
7543 Actual_Hi : Ureal;
7545 begin
7546 -- First step. Base types are required to be symmetrical. Right
7547 -- now, the base type range is a copy of the first subtype range.
7548 -- This will be corrected before we are done, but right away we
7549 -- need to deal with the case where both bounds are non-negative.
7550 -- In this case, we set the low bound to the negative of the high
7551 -- bound, to make sure that the size is computed to include the
7552 -- required sign. Note that we do not need to worry about the
7553 -- case of both bounds negative, because the sign will be dealt
7554 -- with anyway. Furthermore we can't just go making such a bound
7555 -- symmetrical, since in a twos-complement system, there is an
7556 -- extra negative value which could not be accommodated on the
7557 -- positive side.
7559 if Typ = Btyp
7560 and then not UR_Is_Negative (Loval)
7561 and then Hival > Loval
7562 then
7563 Loval := -Hival;
7564 Set_Realval (Lo, Loval);
7565 end if;
7567 -- Compute the fudged bounds. If the number is a model number,
7568 -- then we do nothing to include it, but we are allowed to backoff
7569 -- to the next adjacent model number when we exclude it. If it is
7570 -- not a model number then we straddle the two values with the
7571 -- model numbers on either side.
7573 Model_Num := UR_Trunc (Loval / Small) * Small;
7575 if Loval = Model_Num then
7576 Loval_Incl_EP := Model_Num;
7577 else
7578 Loval_Incl_EP := Model_Num - Small;
7579 end if;
7581 -- The low value excluding the end point is Small greater, but
7582 -- we do not do this exclusion if the low value is positive,
7583 -- since it can't help the size and could actually hurt by
7584 -- crossing the high bound.
7586 if UR_Is_Negative (Loval_Incl_EP) then
7587 Loval_Excl_EP := Loval_Incl_EP + Small;
7589 -- If the value went from negative to zero, then we have the
7590 -- case where Loval_Incl_EP is the model number just below
7591 -- zero, so we want to stick to the negative value for the
7592 -- base type to maintain the condition that the size will
7593 -- include signed values.
7595 if Typ = Btyp
7596 and then UR_Is_Zero (Loval_Excl_EP)
7597 then
7598 Loval_Excl_EP := Loval_Incl_EP;
7599 end if;
7601 else
7602 Loval_Excl_EP := Loval_Incl_EP;
7603 end if;
7605 -- Similar processing for upper bound and high value
7607 Model_Num := UR_Trunc (Hival / Small) * Small;
7609 if Hival = Model_Num then
7610 Hival_Incl_EP := Model_Num;
7611 else
7612 Hival_Incl_EP := Model_Num + Small;
7613 end if;
7615 if UR_Is_Positive (Hival_Incl_EP) then
7616 Hival_Excl_EP := Hival_Incl_EP - Small;
7617 else
7618 Hival_Excl_EP := Hival_Incl_EP;
7619 end if;
7621 -- One further adjustment is needed. In the case of subtypes, we
7622 -- cannot go outside the range of the base type, or we get
7623 -- peculiarities, and the base type range is already set. This
7624 -- only applies to the Incl values, since clearly the Excl values
7625 -- are already as restricted as they are allowed to be.
7627 if Typ /= Btyp then
7628 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
7629 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
7630 end if;
7632 -- Get size including and excluding end points
7634 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
7635 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
7637 -- No need to exclude end-points if it does not reduce size
7639 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
7640 Loval_Excl_EP := Loval_Incl_EP;
7641 end if;
7643 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
7644 Hival_Excl_EP := Hival_Incl_EP;
7645 end if;
7647 -- Now we set the actual size to be used. We want to use the
7648 -- bounds fudged up to include the end-points but only if this
7649 -- can be done without violating a specifically given size
7650 -- size clause or causing an unacceptable increase in size.
7652 -- Case of size clause given
7654 if Has_Size_Clause (Typ) then
7656 -- Use the inclusive size only if it is consistent with
7657 -- the explicitly specified size.
7659 if Size_Incl_EP <= RM_Size (Typ) then
7660 Actual_Lo := Loval_Incl_EP;
7661 Actual_Hi := Hival_Incl_EP;
7662 Actual_Size := Size_Incl_EP;
7664 -- If the inclusive size is too large, we try excluding
7665 -- the end-points (will be caught later if does not work).
7667 else
7668 Actual_Lo := Loval_Excl_EP;
7669 Actual_Hi := Hival_Excl_EP;
7670 Actual_Size := Size_Excl_EP;
7671 end if;
7673 -- Case of size clause not given
7675 else
7676 -- If we have a base type whose corresponding first subtype
7677 -- has an explicit size that is large enough to include our
7678 -- end-points, then do so. There is no point in working hard
7679 -- to get a base type whose size is smaller than the specified
7680 -- size of the first subtype.
7682 First_Subt := First_Subtype (Typ);
7684 if Has_Size_Clause (First_Subt)
7685 and then Size_Incl_EP <= Esize (First_Subt)
7686 then
7687 Actual_Size := Size_Incl_EP;
7688 Actual_Lo := Loval_Incl_EP;
7689 Actual_Hi := Hival_Incl_EP;
7691 -- If excluding the end-points makes the size smaller and
7692 -- results in a size of 8,16,32,64, then we take the smaller
7693 -- size. For the 64 case, this is compulsory. For the other
7694 -- cases, it seems reasonable. We like to include end points
7695 -- if we can, but not at the expense of moving to the next
7696 -- natural boundary of size.
7698 elsif Size_Incl_EP /= Size_Excl_EP
7699 and then Addressable (Size_Excl_EP)
7700 then
7701 Actual_Size := Size_Excl_EP;
7702 Actual_Lo := Loval_Excl_EP;
7703 Actual_Hi := Hival_Excl_EP;
7705 -- Otherwise we can definitely include the end points
7707 else
7708 Actual_Size := Size_Incl_EP;
7709 Actual_Lo := Loval_Incl_EP;
7710 Actual_Hi := Hival_Incl_EP;
7711 end if;
7713 -- One pathological case: normally we never fudge a low bound
7714 -- down, since it would seem to increase the size (if it has
7715 -- any effect), but for ranges containing single value, or no
7716 -- values, the high bound can be small too large. Consider:
7718 -- type t is delta 2.0**(-14)
7719 -- range 131072.0 .. 0;
7721 -- That lower bound is *just* outside the range of 32 bits, and
7722 -- does need fudging down in this case. Note that the bounds
7723 -- will always have crossed here, since the high bound will be
7724 -- fudged down if necessary, as in the case of:
7726 -- type t is delta 2.0**(-14)
7727 -- range 131072.0 .. 131072.0;
7729 -- So we detect the situation by looking for crossed bounds,
7730 -- and if the bounds are crossed, and the low bound is greater
7731 -- than zero, we will always back it off by small, since this
7732 -- is completely harmless.
7734 if Actual_Lo > Actual_Hi then
7735 if UR_Is_Positive (Actual_Lo) then
7736 Actual_Lo := Loval_Incl_EP - Small;
7737 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7739 -- And of course, we need to do exactly the same parallel
7740 -- fudge for flat ranges in the negative region.
7742 elsif UR_Is_Negative (Actual_Hi) then
7743 Actual_Hi := Hival_Incl_EP + Small;
7744 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7745 end if;
7746 end if;
7747 end if;
7749 Set_Realval (Lo, Actual_Lo);
7750 Set_Realval (Hi, Actual_Hi);
7751 end Fudge;
7753 -- For the decimal case, none of this fudging is required, since there
7754 -- are no end-point problems in the decimal case (the end-points are
7755 -- always included).
7757 else
7758 Actual_Size := Fsize (Loval, Hival);
7759 end if;
7761 -- At this stage, the actual size has been calculated and the proper
7762 -- required bounds are stored in the low and high bounds.
7764 if Actual_Size > 64 then
7765 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7766 Error_Msg_N
7767 ("size required (^) for type& too large, maximum allowed is 64",
7768 Typ);
7769 Actual_Size := 64;
7770 end if;
7772 -- Check size against explicit given size
7774 if Has_Size_Clause (Typ) then
7775 if Actual_Size > RM_Size (Typ) then
7776 Error_Msg_Uint_1 := RM_Size (Typ);
7777 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7778 Error_Msg_NE
7779 ("size given (^) for type& too small, minimum allowed is ^",
7780 Size_Clause (Typ), Typ);
7782 else
7783 Actual_Size := UI_To_Int (Esize (Typ));
7784 end if;
7786 -- Increase size to next natural boundary if no size clause given
7788 else
7789 if Actual_Size <= 8 then
7790 Actual_Size := 8;
7791 elsif Actual_Size <= 16 then
7792 Actual_Size := 16;
7793 elsif Actual_Size <= 32 then
7794 Actual_Size := 32;
7795 else
7796 Actual_Size := 64;
7797 end if;
7799 Init_Esize (Typ, Actual_Size);
7800 Adjust_Esize_For_Alignment (Typ);
7801 end if;
7803 -- If we have a base type, then expand the bounds so that they extend to
7804 -- the full width of the allocated size in bits, to avoid junk range
7805 -- checks on intermediate computations.
7807 if Base_Type (Typ) = Typ then
7808 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7809 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7810 end if;
7812 -- Final step is to reanalyze the bounds using the proper type
7813 -- and set the Corresponding_Integer_Value fields of the literals.
7815 Set_Etype (Lo, Empty);
7816 Set_Analyzed (Lo, False);
7817 Analyze (Lo);
7819 -- Resolve with universal fixed if the base type, and the base type if
7820 -- it is a subtype. Note we can't resolve the base type with itself,
7821 -- that would be a reference before definition.
7823 if Typ = Btyp then
7824 Resolve (Lo, Universal_Fixed);
7825 else
7826 Resolve (Lo, Btyp);
7827 end if;
7829 -- Set corresponding integer value for bound
7831 Set_Corresponding_Integer_Value
7832 (Lo, UR_To_Uint (Realval (Lo) / Small));
7834 -- Similar processing for high bound
7836 Set_Etype (Hi, Empty);
7837 Set_Analyzed (Hi, False);
7838 Analyze (Hi);
7840 if Typ = Btyp then
7841 Resolve (Hi, Universal_Fixed);
7842 else
7843 Resolve (Hi, Btyp);
7844 end if;
7846 Set_Corresponding_Integer_Value
7847 (Hi, UR_To_Uint (Realval (Hi) / Small));
7849 -- Set type of range to correspond to bounds
7851 Set_Etype (Rng, Etype (Lo));
7853 -- Set Esize to calculated size if not set already
7855 if Unknown_Esize (Typ) then
7856 Init_Esize (Typ, Actual_Size);
7857 end if;
7859 -- Set RM_Size if not already set. If already set, check value
7861 declare
7862 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7864 begin
7865 if RM_Size (Typ) /= Uint_0 then
7866 if RM_Size (Typ) < Minsiz then
7867 Error_Msg_Uint_1 := RM_Size (Typ);
7868 Error_Msg_Uint_2 := Minsiz;
7869 Error_Msg_NE
7870 ("size given (^) for type& too small, minimum allowed is ^",
7871 Size_Clause (Typ), Typ);
7872 end if;
7874 else
7875 Set_RM_Size (Typ, Minsiz);
7876 end if;
7877 end;
7879 -- Check for shaving
7881 if Comes_From_Source (Typ) then
7883 -- In SPARK mode the given bounds must be strictly representable
7885 if SPARK_Mode = On then
7886 if Orig_Lo < Expr_Value_R (Lo) then
7887 Error_Msg_NE
7888 ("declared low bound of type & is outside type range",
7889 Lo, Typ);
7890 end if;
7892 if Orig_Hi > Expr_Value_R (Hi) then
7893 Error_Msg_NE
7894 ("declared high bound of type & is outside type range",
7895 Hi, Typ);
7896 end if;
7898 else
7899 if Orig_Lo < Expr_Value_R (Lo) then
7900 Error_Msg_N
7901 ("declared low bound of type & is outside type range??", Typ);
7902 Error_Msg_N
7903 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
7904 end if;
7906 if Orig_Hi > Expr_Value_R (Hi) then
7907 Error_Msg_N
7908 ("declared high bound of type & is outside type range??",
7909 Typ);
7910 Error_Msg_N
7911 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
7912 end if;
7913 end if;
7914 end if;
7915 end Freeze_Fixed_Point_Type;
7917 ------------------
7918 -- Freeze_Itype --
7919 ------------------
7921 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7922 L : List_Id;
7924 begin
7925 Set_Has_Delayed_Freeze (T);
7926 L := Freeze_Entity (T, N);
7928 if Is_Non_Empty_List (L) then
7929 Insert_Actions (N, L);
7930 end if;
7931 end Freeze_Itype;
7933 --------------------------
7934 -- Freeze_Static_Object --
7935 --------------------------
7937 procedure Freeze_Static_Object (E : Entity_Id) is
7939 Cannot_Be_Static : exception;
7940 -- Exception raised if the type of a static object cannot be made
7941 -- static. This happens if the type depends on non-global objects.
7943 procedure Ensure_Expression_Is_SA (N : Node_Id);
7944 -- Called to ensure that an expression used as part of a type definition
7945 -- is statically allocatable, which means that the expression type is
7946 -- statically allocatable, and the expression is either static, or a
7947 -- reference to a library level constant.
7949 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7950 -- Called to mark a type as static, checking that it is possible
7951 -- to set the type as static. If it is not possible, then the
7952 -- exception Cannot_Be_Static is raised.
7954 -----------------------------
7955 -- Ensure_Expression_Is_SA --
7956 -----------------------------
7958 procedure Ensure_Expression_Is_SA (N : Node_Id) is
7959 Ent : Entity_Id;
7961 begin
7962 Ensure_Type_Is_SA (Etype (N));
7964 if Is_OK_Static_Expression (N) then
7965 return;
7967 elsif Nkind (N) = N_Identifier then
7968 Ent := Entity (N);
7970 if Present (Ent)
7971 and then Ekind (Ent) = E_Constant
7972 and then Is_Library_Level_Entity (Ent)
7973 then
7974 return;
7975 end if;
7976 end if;
7978 raise Cannot_Be_Static;
7979 end Ensure_Expression_Is_SA;
7981 -----------------------
7982 -- Ensure_Type_Is_SA --
7983 -----------------------
7985 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7986 N : Node_Id;
7987 C : Entity_Id;
7989 begin
7990 -- If type is library level, we are all set
7992 if Is_Library_Level_Entity (Typ) then
7993 return;
7994 end if;
7996 -- We are also OK if the type already marked as statically allocated,
7997 -- which means we processed it before.
7999 if Is_Statically_Allocated (Typ) then
8000 return;
8001 end if;
8003 -- Mark type as statically allocated
8005 Set_Is_Statically_Allocated (Typ);
8007 -- Check that it is safe to statically allocate this type
8009 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
8010 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
8011 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
8013 elsif Is_Array_Type (Typ) then
8014 N := First_Index (Typ);
8015 while Present (N) loop
8016 Ensure_Type_Is_SA (Etype (N));
8017 Next_Index (N);
8018 end loop;
8020 Ensure_Type_Is_SA (Component_Type (Typ));
8022 elsif Is_Access_Type (Typ) then
8023 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
8025 declare
8026 F : Entity_Id;
8027 T : constant Entity_Id := Etype (Designated_Type (Typ));
8029 begin
8030 if T /= Standard_Void_Type then
8031 Ensure_Type_Is_SA (T);
8032 end if;
8034 F := First_Formal (Designated_Type (Typ));
8035 while Present (F) loop
8036 Ensure_Type_Is_SA (Etype (F));
8037 Next_Formal (F);
8038 end loop;
8039 end;
8041 else
8042 Ensure_Type_Is_SA (Designated_Type (Typ));
8043 end if;
8045 elsif Is_Record_Type (Typ) then
8046 C := First_Entity (Typ);
8047 while Present (C) loop
8048 if Ekind (C) = E_Discriminant
8049 or else Ekind (C) = E_Component
8050 then
8051 Ensure_Type_Is_SA (Etype (C));
8053 elsif Is_Type (C) then
8054 Ensure_Type_Is_SA (C);
8055 end if;
8057 Next_Entity (C);
8058 end loop;
8060 elsif Ekind (Typ) = E_Subprogram_Type then
8061 Ensure_Type_Is_SA (Etype (Typ));
8063 C := First_Formal (Typ);
8064 while Present (C) loop
8065 Ensure_Type_Is_SA (Etype (C));
8066 Next_Formal (C);
8067 end loop;
8069 else
8070 raise Cannot_Be_Static;
8071 end if;
8072 end Ensure_Type_Is_SA;
8074 -- Start of processing for Freeze_Static_Object
8076 begin
8077 Ensure_Type_Is_SA (Etype (E));
8079 exception
8080 when Cannot_Be_Static =>
8082 -- If the object that cannot be static is imported or exported, then
8083 -- issue an error message saying that this object cannot be imported
8084 -- or exported. If it has an address clause it is an overlay in the
8085 -- current partition and the static requirement is not relevant.
8086 -- Do not issue any error message when ignoring rep clauses.
8088 if Ignore_Rep_Clauses then
8089 null;
8091 elsif Is_Imported (E) then
8092 if No (Address_Clause (E)) then
8093 Error_Msg_N
8094 ("& cannot be imported (local type is not constant)", E);
8095 end if;
8097 -- Otherwise must be exported, something is wrong if compiler
8098 -- is marking something as statically allocated which cannot be).
8100 else pragma Assert (Is_Exported (E));
8101 Error_Msg_N
8102 ("& cannot be exported (local type is not constant)", E);
8103 end if;
8104 end Freeze_Static_Object;
8106 -----------------------
8107 -- Freeze_Subprogram --
8108 -----------------------
8110 procedure Freeze_Subprogram (E : Entity_Id) is
8111 procedure Set_Profile_Convention (Subp_Id : Entity_Id);
8112 -- Set the conventions of all anonymous access-to-subprogram formals and
8113 -- result subtype of subprogram Subp_Id to the convention of Subp_Id.
8115 ----------------------------
8116 -- Set_Profile_Convention --
8117 ----------------------------
8119 procedure Set_Profile_Convention (Subp_Id : Entity_Id) is
8120 Conv : constant Convention_Id := Convention (Subp_Id);
8122 procedure Set_Type_Convention (Typ : Entity_Id);
8123 -- Set the convention of anonymous access-to-subprogram type Typ and
8124 -- its designated type to Conv.
8126 -------------------------
8127 -- Set_Type_Convention --
8128 -------------------------
8130 procedure Set_Type_Convention (Typ : Entity_Id) is
8131 begin
8132 -- Set the convention on both the anonymous access-to-subprogram
8133 -- type and the subprogram type it points to because both types
8134 -- participate in conformance-related checks.
8136 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
8137 Set_Convention (Typ, Conv);
8138 Set_Convention (Designated_Type (Typ), Conv);
8139 end if;
8140 end Set_Type_Convention;
8142 -- Local variables
8144 Formal : Entity_Id;
8146 -- Start of processing for Set_Profile_Convention
8148 begin
8149 Formal := First_Formal (Subp_Id);
8150 while Present (Formal) loop
8151 Set_Type_Convention (Etype (Formal));
8152 Next_Formal (Formal);
8153 end loop;
8155 if Ekind (Subp_Id) = E_Function then
8156 Set_Type_Convention (Etype (Subp_Id));
8157 end if;
8158 end Set_Profile_Convention;
8160 -- Local variables
8162 F : Entity_Id;
8163 Retype : Entity_Id;
8165 -- Start of processing for Freeze_Subprogram
8167 begin
8168 -- Subprogram may not have an address clause unless it is imported
8170 if Present (Address_Clause (E)) then
8171 if not Is_Imported (E) then
8172 Error_Msg_N
8173 ("address clause can only be given for imported subprogram",
8174 Name (Address_Clause (E)));
8175 end if;
8176 end if;
8178 -- Reset the Pure indication on an imported subprogram unless an
8179 -- explicit Pure_Function pragma was present or the subprogram is an
8180 -- intrinsic. We do this because otherwise it is an insidious error
8181 -- to call a non-pure function from pure unit and have calls
8182 -- mysteriously optimized away. What happens here is that the Import
8183 -- can bypass the normal check to ensure that pure units call only pure
8184 -- subprograms.
8186 -- The reason for the intrinsic exception is that in general, intrinsic
8187 -- functions (such as shifts) are pure anyway. The only exceptions are
8188 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
8189 -- in any case, so no problem arises.
8191 if Is_Imported (E)
8192 and then Is_Pure (E)
8193 and then not Has_Pragma_Pure_Function (E)
8194 and then not Is_Intrinsic_Subprogram (E)
8195 then
8196 Set_Is_Pure (E, False);
8197 end if;
8199 -- We also reset the Pure indication on a subprogram with an Address
8200 -- parameter, because the parameter may be used as a pointer and the
8201 -- referenced data may change even if the address value does not.
8203 -- Note that if the programmer gave an explicit Pure_Function pragma,
8204 -- then we believe the programmer, and leave the subprogram Pure. We
8205 -- also suppress this check on run-time files.
8207 if Is_Pure (E)
8208 and then Is_Subprogram (E)
8209 and then not Has_Pragma_Pure_Function (E)
8210 and then not Is_Internal_Unit (Current_Sem_Unit)
8211 then
8212 Check_Function_With_Address_Parameter (E);
8213 end if;
8215 -- Ensure that all anonymous access-to-subprogram types inherit the
8216 -- convention of their related subprogram (RM 6.3.1 13.1/3). This is
8217 -- not done for a defaulted convention Ada because those types also
8218 -- default to Ada. Convention Protected must not be propagated when
8219 -- the subprogram is an entry because this would be illegal. The only
8220 -- way to force convention Protected on these kinds of types is to
8221 -- include keyword "protected" in the access definition.
8223 if Convention (E) /= Convention_Ada
8224 and then Convention (E) /= Convention_Protected
8225 then
8226 Set_Profile_Convention (E);
8227 end if;
8229 -- For non-foreign convention subprograms, this is where we create
8230 -- the extra formals (for accessibility level and constrained bit
8231 -- information). We delay this till the freeze point precisely so
8232 -- that we know the convention.
8234 if not Has_Foreign_Convention (E) then
8235 if No (Extra_Formals (E)) then
8236 Create_Extra_Formals (E);
8237 end if;
8239 Set_Mechanisms (E);
8241 -- If this is convention Ada and a Valued_Procedure, that's odd
8243 if Ekind (E) = E_Procedure
8244 and then Is_Valued_Procedure (E)
8245 and then Convention (E) = Convention_Ada
8246 and then Warn_On_Export_Import
8247 then
8248 Error_Msg_N
8249 ("??Valued_Procedure has no effect for convention Ada", E);
8250 Set_Is_Valued_Procedure (E, False);
8251 end if;
8253 -- Case of foreign convention
8255 else
8256 Set_Mechanisms (E);
8258 -- For foreign conventions, warn about return of unconstrained array
8260 if Ekind (E) = E_Function then
8261 Retype := Underlying_Type (Etype (E));
8263 -- If no return type, probably some other error, e.g. a
8264 -- missing full declaration, so ignore.
8266 if No (Retype) then
8267 null;
8269 -- If the return type is generic, we have emitted a warning
8270 -- earlier on, and there is nothing else to check here. Specific
8271 -- instantiations may lead to erroneous behavior.
8273 elsif Is_Generic_Type (Etype (E)) then
8274 null;
8276 -- Display warning if returning unconstrained array
8278 elsif Is_Array_Type (Retype)
8279 and then not Is_Constrained (Retype)
8281 -- Check appropriate warning is enabled (should we check for
8282 -- Warnings (Off) on specific entities here, probably so???)
8284 and then Warn_On_Export_Import
8285 then
8286 Error_Msg_N
8287 ("?x?foreign convention function& should not return " &
8288 "unconstrained array", E);
8289 return;
8290 end if;
8291 end if;
8293 -- If any of the formals for an exported foreign convention
8294 -- subprogram have defaults, then emit an appropriate warning since
8295 -- this is odd (default cannot be used from non-Ada code)
8297 if Is_Exported (E) then
8298 F := First_Formal (E);
8299 while Present (F) loop
8300 if Warn_On_Export_Import
8301 and then Present (Default_Value (F))
8302 then
8303 Error_Msg_N
8304 ("?x?parameter cannot be defaulted in non-Ada call",
8305 Default_Value (F));
8306 end if;
8308 Next_Formal (F);
8309 end loop;
8310 end if;
8311 end if;
8313 -- Pragma Inline_Always is disallowed for dispatching subprograms
8314 -- because the address of such subprograms is saved in the dispatch
8315 -- table to support dispatching calls, and dispatching calls cannot
8316 -- be inlined. This is consistent with the restriction against using
8317 -- 'Access or 'Address on an Inline_Always subprogram.
8319 if Is_Dispatching_Operation (E)
8320 and then Has_Pragma_Inline_Always (E)
8321 then
8322 Error_Msg_N
8323 ("pragma Inline_Always not allowed for dispatching subprograms", E);
8324 end if;
8326 -- Because of the implicit representation of inherited predefined
8327 -- operators in the front-end, the overriding status of the operation
8328 -- may be affected when a full view of a type is analyzed, and this is
8329 -- not captured by the analysis of the corresponding type declaration.
8330 -- Therefore the correctness of a not-overriding indicator must be
8331 -- rechecked when the subprogram is frozen.
8333 if Nkind (E) = N_Defining_Operator_Symbol
8334 and then not Error_Posted (Parent (E))
8335 then
8336 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
8337 end if;
8339 if Modify_Tree_For_C
8340 and then Nkind (Parent (E)) = N_Function_Specification
8341 and then Is_Array_Type (Etype (E))
8342 and then Is_Constrained (Etype (E))
8343 and then not Is_Unchecked_Conversion_Instance (E)
8344 and then not Rewritten_For_C (E)
8345 then
8346 Build_Procedure_Form (Unit_Declaration_Node (E));
8347 end if;
8348 end Freeze_Subprogram;
8350 ----------------------
8351 -- Is_Fully_Defined --
8352 ----------------------
8354 function Is_Fully_Defined (T : Entity_Id) return Boolean is
8355 begin
8356 if Ekind (T) = E_Class_Wide_Type then
8357 return Is_Fully_Defined (Etype (T));
8359 elsif Is_Array_Type (T) then
8360 return Is_Fully_Defined (Component_Type (T));
8362 elsif Is_Record_Type (T)
8363 and not Is_Private_Type (T)
8364 then
8365 -- Verify that the record type has no components with private types
8366 -- without completion.
8368 declare
8369 Comp : Entity_Id;
8371 begin
8372 Comp := First_Component (T);
8373 while Present (Comp) loop
8374 if not Is_Fully_Defined (Etype (Comp)) then
8375 return False;
8376 end if;
8378 Next_Component (Comp);
8379 end loop;
8380 return True;
8381 end;
8383 -- For the designated type of an access to subprogram, all types in
8384 -- the profile must be fully defined.
8386 elsif Ekind (T) = E_Subprogram_Type then
8387 declare
8388 F : Entity_Id;
8390 begin
8391 F := First_Formal (T);
8392 while Present (F) loop
8393 if not Is_Fully_Defined (Etype (F)) then
8394 return False;
8395 end if;
8397 Next_Formal (F);
8398 end loop;
8400 return Is_Fully_Defined (Etype (T));
8401 end;
8403 else
8404 return not Is_Private_Type (T)
8405 or else Present (Full_View (Base_Type (T)));
8406 end if;
8407 end Is_Fully_Defined;
8409 ---------------------------------
8410 -- Process_Default_Expressions --
8411 ---------------------------------
8413 procedure Process_Default_Expressions
8414 (E : Entity_Id;
8415 After : in out Node_Id)
8417 Loc : constant Source_Ptr := Sloc (E);
8418 Dbody : Node_Id;
8419 Formal : Node_Id;
8420 Dcopy : Node_Id;
8421 Dnam : Entity_Id;
8423 begin
8424 Set_Default_Expressions_Processed (E);
8426 -- A subprogram instance and its associated anonymous subprogram share
8427 -- their signature. The default expression functions are defined in the
8428 -- wrapper packages for the anonymous subprogram, and should not be
8429 -- generated again for the instance.
8431 if Is_Generic_Instance (E)
8432 and then Present (Alias (E))
8433 and then Default_Expressions_Processed (Alias (E))
8434 then
8435 return;
8436 end if;
8438 Formal := First_Formal (E);
8439 while Present (Formal) loop
8440 if Present (Default_Value (Formal)) then
8442 -- We work with a copy of the default expression because we
8443 -- do not want to disturb the original, since this would mess
8444 -- up the conformance checking.
8446 Dcopy := New_Copy_Tree (Default_Value (Formal));
8448 -- The analysis of the expression may generate insert actions,
8449 -- which of course must not be executed. We wrap those actions
8450 -- in a procedure that is not called, and later on eliminated.
8451 -- The following cases have no side effects, and are analyzed
8452 -- directly.
8454 if Nkind (Dcopy) = N_Identifier
8455 or else Nkind_In (Dcopy, N_Expanded_Name,
8456 N_Integer_Literal,
8457 N_Character_Literal,
8458 N_String_Literal,
8459 N_Real_Literal)
8460 or else (Nkind (Dcopy) = N_Attribute_Reference
8461 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
8462 or else Known_Null (Dcopy)
8463 then
8464 -- If there is no default function, we must still do a full
8465 -- analyze call on the default value, to ensure that all error
8466 -- checks are performed, e.g. those associated with static
8467 -- evaluation. Note: this branch will always be taken if the
8468 -- analyzer is turned off (but we still need the error checks).
8470 -- Note: the setting of parent here is to meet the requirement
8471 -- that we can only analyze the expression while attached to
8472 -- the tree. Really the requirement is that the parent chain
8473 -- be set, we don't actually need to be in the tree.
8475 Set_Parent (Dcopy, Declaration_Node (Formal));
8476 Analyze (Dcopy);
8478 -- Default expressions are resolved with their own type if the
8479 -- context is generic, to avoid anomalies with private types.
8481 if Ekind (Scope (E)) = E_Generic_Package then
8482 Resolve (Dcopy);
8483 else
8484 Resolve (Dcopy, Etype (Formal));
8485 end if;
8487 -- If that resolved expression will raise constraint error,
8488 -- then flag the default value as raising constraint error.
8489 -- This allows a proper error message on the calls.
8491 if Raises_Constraint_Error (Dcopy) then
8492 Set_Raises_Constraint_Error (Default_Value (Formal));
8493 end if;
8495 -- If the default is a parameterless call, we use the name of
8496 -- the called function directly, and there is no body to build.
8498 elsif Nkind (Dcopy) = N_Function_Call
8499 and then No (Parameter_Associations (Dcopy))
8500 then
8501 null;
8503 -- Else construct and analyze the body of a wrapper procedure
8504 -- that contains an object declaration to hold the expression.
8505 -- Given that this is done only to complete the analysis, it is
8506 -- simpler to build a procedure than a function which might
8507 -- involve secondary stack expansion.
8509 else
8510 Dnam := Make_Temporary (Loc, 'D');
8512 Dbody :=
8513 Make_Subprogram_Body (Loc,
8514 Specification =>
8515 Make_Procedure_Specification (Loc,
8516 Defining_Unit_Name => Dnam),
8518 Declarations => New_List (
8519 Make_Object_Declaration (Loc,
8520 Defining_Identifier => Make_Temporary (Loc, 'T'),
8521 Object_Definition =>
8522 New_Occurrence_Of (Etype (Formal), Loc),
8523 Expression => New_Copy_Tree (Dcopy))),
8525 Handled_Statement_Sequence =>
8526 Make_Handled_Sequence_Of_Statements (Loc,
8527 Statements => Empty_List));
8529 Set_Scope (Dnam, Scope (E));
8530 Set_Assignment_OK (First (Declarations (Dbody)));
8531 Set_Is_Eliminated (Dnam);
8532 Insert_After (After, Dbody);
8533 Analyze (Dbody);
8534 After := Dbody;
8535 end if;
8536 end if;
8538 Next_Formal (Formal);
8539 end loop;
8540 end Process_Default_Expressions;
8542 ----------------------------------------
8543 -- Set_Component_Alignment_If_Not_Set --
8544 ----------------------------------------
8546 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
8547 begin
8548 -- Ignore if not base type, subtypes don't need anything
8550 if Typ /= Base_Type (Typ) then
8551 return;
8552 end if;
8554 -- Do not override existing representation
8556 if Is_Packed (Typ) then
8557 return;
8559 elsif Has_Specified_Layout (Typ) then
8560 return;
8562 elsif Component_Alignment (Typ) /= Calign_Default then
8563 return;
8565 else
8566 Set_Component_Alignment
8567 (Typ, Scope_Stack.Table
8568 (Scope_Stack.Last).Component_Alignment_Default);
8569 end if;
8570 end Set_Component_Alignment_If_Not_Set;
8572 --------------------------
8573 -- Set_SSO_From_Default --
8574 --------------------------
8576 procedure Set_SSO_From_Default (T : Entity_Id) is
8577 Reversed : Boolean;
8579 begin
8580 -- Set default SSO for an array or record base type, except in case of
8581 -- a type extension (which always inherits the SSO of its parent type).
8583 if Is_Base_Type (T)
8584 and then (Is_Array_Type (T)
8585 or else (Is_Record_Type (T)
8586 and then not (Is_Tagged_Type (T)
8587 and then Is_Derived_Type (T))))
8588 then
8589 Reversed :=
8590 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
8591 or else
8592 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
8594 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
8596 -- For a record type, if bit order is specified explicitly,
8597 -- then do not set SSO from default if not consistent. Note that
8598 -- we do not want to look at a Bit_Order attribute definition
8599 -- for a parent: if we were to inherit Bit_Order, then both
8600 -- SSO_Set_*_By_Default flags would have been cleared already
8601 -- (by Inherit_Aspects_At_Freeze_Point).
8603 and then not
8604 (Is_Record_Type (T)
8605 and then
8606 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
8607 and then Reverse_Bit_Order (T) /= Reversed)
8608 then
8609 -- If flags cause reverse storage order, then set the result. Note
8610 -- that we would have ignored the pragma setting the non default
8611 -- storage order in any case, hence the assertion at this point.
8613 pragma Assert
8614 (not Reversed or else Support_Nondefault_SSO_On_Target);
8616 Set_Reverse_Storage_Order (T, Reversed);
8618 -- For a record type, also set reversed bit order. Note: if a bit
8619 -- order has been specified explicitly, then this is a no-op.
8621 if Is_Record_Type (T) then
8622 Set_Reverse_Bit_Order (T, Reversed);
8623 end if;
8624 end if;
8625 end if;
8626 end Set_SSO_From_Default;
8628 ------------------
8629 -- Undelay_Type --
8630 ------------------
8632 procedure Undelay_Type (T : Entity_Id) is
8633 begin
8634 Set_Has_Delayed_Freeze (T, False);
8635 Set_Freeze_Node (T, Empty);
8637 -- Since we don't want T to have a Freeze_Node, we don't want its
8638 -- Full_View or Corresponding_Record_Type to have one either.
8640 -- ??? Fundamentally, this whole handling is unpleasant. What we really
8641 -- want is to be sure that for an Itype that's part of record R and is a
8642 -- subtype of type T, that it's frozen after the later of the freeze
8643 -- points of R and T. We have no way of doing that directly, so what we
8644 -- do is force most such Itypes to be frozen as part of freezing R via
8645 -- this procedure and only delay the ones that need to be delayed
8646 -- (mostly the designated types of access types that are defined as part
8647 -- of the record).
8649 if Is_Private_Type (T)
8650 and then Present (Full_View (T))
8651 and then Is_Itype (Full_View (T))
8652 and then Is_Record_Type (Scope (Full_View (T)))
8653 then
8654 Undelay_Type (Full_View (T));
8655 end if;
8657 if Is_Concurrent_Type (T)
8658 and then Present (Corresponding_Record_Type (T))
8659 and then Is_Itype (Corresponding_Record_Type (T))
8660 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
8661 then
8662 Undelay_Type (Corresponding_Record_Type (T));
8663 end if;
8664 end Undelay_Type;
8666 ------------------
8667 -- Warn_Overlay --
8668 ------------------
8670 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Entity_Id) is
8671 Ent : constant Entity_Id := Entity (Nam);
8672 -- The object to which the address clause applies
8674 Init : Node_Id;
8675 Old : Entity_Id := Empty;
8676 Decl : Node_Id;
8678 begin
8679 -- No warning if address clause overlay warnings are off
8681 if not Address_Clause_Overlay_Warnings then
8682 return;
8683 end if;
8685 -- No warning if there is an explicit initialization
8687 Init := Original_Node (Expression (Declaration_Node (Ent)));
8689 if Present (Init) and then Comes_From_Source (Init) then
8690 return;
8691 end if;
8693 -- We only give the warning for non-imported entities of a type for
8694 -- which a non-null base init proc is defined, or for objects of access
8695 -- types with implicit null initialization, or when Normalize_Scalars
8696 -- applies and the type is scalar or a string type (the latter being
8697 -- tested for because predefined String types are initialized by inline
8698 -- code rather than by an init_proc). Note that we do not give the
8699 -- warning for Initialize_Scalars, since we suppressed initialization
8700 -- in this case. Also, do not warn if Suppress_Initialization is set.
8702 if Present (Expr)
8703 and then not Is_Imported (Ent)
8704 and then not Initialization_Suppressed (Typ)
8705 and then (Has_Non_Null_Base_Init_Proc (Typ)
8706 or else Is_Access_Type (Typ)
8707 or else (Normalize_Scalars
8708 and then (Is_Scalar_Type (Typ)
8709 or else Is_String_Type (Typ))))
8710 then
8711 if Nkind (Expr) = N_Attribute_Reference
8712 and then Is_Entity_Name (Prefix (Expr))
8713 then
8714 Old := Entity (Prefix (Expr));
8716 elsif Is_Entity_Name (Expr)
8717 and then Ekind (Entity (Expr)) = E_Constant
8718 then
8719 Decl := Declaration_Node (Entity (Expr));
8721 if Nkind (Decl) = N_Object_Declaration
8722 and then Present (Expression (Decl))
8723 and then Nkind (Expression (Decl)) = N_Attribute_Reference
8724 and then Is_Entity_Name (Prefix (Expression (Decl)))
8725 then
8726 Old := Entity (Prefix (Expression (Decl)));
8728 elsif Nkind (Expr) = N_Function_Call then
8729 return;
8730 end if;
8732 -- A function call (most likely to To_Address) is probably not an
8733 -- overlay, so skip warning. Ditto if the function call was inlined
8734 -- and transformed into an entity.
8736 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
8737 return;
8738 end if;
8740 -- If a pragma Import follows, we assume that it is for the current
8741 -- target of the address clause, and skip the warning. There may be
8742 -- a source pragma or an aspect that specifies import and generates
8743 -- the corresponding pragma. These will indicate that the entity is
8744 -- imported and that is checked above so that the spurious warning
8745 -- (generated when the entity is frozen) will be suppressed. The
8746 -- pragma may be attached to the aspect, so it is not yet a list
8747 -- member.
8749 if Is_List_Member (Parent (Expr)) then
8750 Decl := Next (Parent (Expr));
8752 if Present (Decl)
8753 and then Nkind (Decl) = N_Pragma
8754 and then Pragma_Name (Decl) = Name_Import
8755 then
8756 return;
8757 end if;
8758 end if;
8760 -- Otherwise give warning message
8762 if Present (Old) then
8763 Error_Msg_Node_2 := Old;
8764 Error_Msg_N
8765 ("default initialization of & may modify &??",
8766 Nam);
8767 else
8768 Error_Msg_N
8769 ("default initialization of & may modify overlaid storage??",
8770 Nam);
8771 end if;
8773 -- Add friendly warning if initialization comes from a packed array
8774 -- component.
8776 if Is_Record_Type (Typ) then
8777 declare
8778 Comp : Entity_Id;
8780 begin
8781 Comp := First_Component (Typ);
8782 while Present (Comp) loop
8783 if Nkind (Parent (Comp)) = N_Component_Declaration
8784 and then Present (Expression (Parent (Comp)))
8785 then
8786 exit;
8787 elsif Is_Array_Type (Etype (Comp))
8788 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
8789 then
8790 Error_Msg_NE
8791 ("\packed array component& " &
8792 "will be initialized to zero??",
8793 Nam, Comp);
8794 exit;
8795 else
8796 Next_Component (Comp);
8797 end if;
8798 end loop;
8799 end;
8800 end if;
8802 Error_Msg_N
8803 ("\use pragma Import for & to " &
8804 "suppress initialization (RM B.1(24))??",
8805 Nam);
8806 end if;
8807 end Warn_Overlay;
8809 end Freeze;