* lto.c (do_stream_out): Add PART parameter; open dump file.
[official-gcc.git] / gcc / ada / freeze.adb
blob3f0350a36823f7797aeef1a13de7122203481a22
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- F R E E Z E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Pakd; use Exp_Pakd;
37 with Exp_Util; use Exp_Util;
38 with Exp_Tss; use Exp_Tss;
39 with Ghost; use Ghost;
40 with Layout; use Layout;
41 with Lib; use Lib;
42 with Namet; use Namet;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Mech; use Sem_Mech;
58 with Sem_Prag; use Sem_Prag;
59 with Sem_Res; use Sem_Res;
60 with Sem_Util; use Sem_Util;
61 with Sinfo; use Sinfo;
62 with Snames; use Snames;
63 with Stand; use Stand;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Ttypes; use Ttypes;
67 with Uintp; use Uintp;
68 with Urealp; use Urealp;
69 with Warnsw; use Warnsw;
71 package body Freeze is
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
78 -- Typ is a type that is being frozen. If no size clause is given,
79 -- but a default Esize has been computed, then this default Esize is
80 -- adjusted up if necessary to be consistent with a given alignment,
81 -- but never to a value greater than Long_Long_Integer'Size. This
82 -- is used for all discrete types and for fixed-point types.
84 procedure Build_And_Analyze_Renamed_Body
85 (Decl : Node_Id;
86 New_S : Entity_Id;
87 After : in out Node_Id);
88 -- Build body for a renaming declaration, insert in tree and analyze
90 procedure Check_Address_Clause (E : Entity_Id);
91 -- Apply legality checks to address clauses for object declarations,
92 -- at the point the object is frozen. Also ensure any initialization is
93 -- performed only after the object has been frozen.
95 procedure Check_Component_Storage_Order
96 (Encl_Type : Entity_Id;
97 Comp : Entity_Id;
98 ADC : Node_Id;
99 Comp_ADC_Present : out Boolean);
100 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
101 -- clause, verify that the component type has an explicit and compatible
102 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
103 -- entity of the component under consideration. For an Encl_Type that
104 -- does not have a Scalar_Storage_Order attribute definition clause,
105 -- verify that the component also does not have such a clause.
106 -- ADC is the attribute definition clause if present (or Empty). On return,
107 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
108 -- attribute definition clause.
110 procedure Check_Debug_Info_Needed (T : Entity_Id);
111 -- As each entity is frozen, this routine is called to deal with the
112 -- setting of Debug_Info_Needed for the entity. This flag is set if
113 -- the entity comes from source, or if we are in Debug_Generated_Code
114 -- mode or if the -gnatdV debug flag is set. However, it never sets
115 -- the flag if Debug_Info_Off is set. This procedure also ensures that
116 -- subsidiary entities have the flag set as required.
118 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
119 -- When an expression function is frozen by a use of it, the expression
120 -- itself is frozen. Check that the expression does not include references
121 -- to deferred constants without completion. We report this at the freeze
122 -- point of the function, to provide a better error message.
124 -- In most cases the expression itself is frozen by the time the function
125 -- itself is frozen, because the formals will be frozen by then. However,
126 -- Attribute references to outer types are freeze points for those types;
127 -- this routine generates the required freeze nodes for them.
129 procedure Check_Inherited_Conditions (R : Entity_Id);
130 -- For a tagged derived type, create wrappers for inherited operations
131 -- that have a class-wide condition, so it can be properly rewritten if
132 -- it involves calls to other overriding primitives.
134 procedure Check_Strict_Alignment (E : Entity_Id);
135 -- E is a base type. If E is tagged or has a component that is aliased
136 -- or tagged or contains something this is aliased or tagged, set
137 -- Strict_Alignment.
139 procedure Check_Unsigned_Type (E : Entity_Id);
140 pragma Inline (Check_Unsigned_Type);
141 -- If E is a fixed-point or discrete type, then all the necessary work
142 -- to freeze it is completed except for possible setting of the flag
143 -- Is_Unsigned_Type, which is done by this procedure. The call has no
144 -- effect if the entity E is not a discrete or fixed-point type.
146 procedure Freeze_And_Append
147 (Ent : Entity_Id;
148 N : Node_Id;
149 Result : in out List_Id);
150 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
151 -- nodes to Result, modifying Result from No_List if necessary. N has
152 -- the same usage as in Freeze_Entity.
154 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
155 -- Freeze enumeration type. The Esize field is set as processing
156 -- proceeds (i.e. set by default when the type is declared and then
157 -- adjusted by rep clauses. What this procedure does is to make sure
158 -- that if a foreign convention is specified, and no specific size
159 -- is given, then the size must be at least Integer'Size.
161 procedure Freeze_Static_Object (E : Entity_Id);
162 -- If an object is frozen which has Is_Statically_Allocated set, then
163 -- all referenced types must also be marked with this flag. This routine
164 -- is in charge of meeting this requirement for the object entity E.
166 procedure Freeze_Subprogram (E : Entity_Id);
167 -- Perform freezing actions for a subprogram (create extra formals,
168 -- and set proper default mechanism values). Note that this routine
169 -- is not called for internal subprograms, for which neither of these
170 -- actions is needed (or desirable, we do not want for example to have
171 -- these extra formals present in initialization procedures, where they
172 -- would serve no purpose). In this call E is either a subprogram or
173 -- a subprogram type (i.e. an access to a subprogram).
175 function Is_Fully_Defined (T : Entity_Id) return Boolean;
176 -- True if T is not private and has no private components, or has a full
177 -- view. Used to determine whether the designated type of an access type
178 -- should be frozen when the access type is frozen. This is done when an
179 -- allocator is frozen, or an expression that may involve attributes of
180 -- the designated type. Otherwise freezing the access type does not freeze
181 -- the designated type.
183 procedure Process_Default_Expressions
184 (E : Entity_Id;
185 After : in out Node_Id);
186 -- This procedure is called for each subprogram to complete processing of
187 -- default expressions at the point where all types are known to be frozen.
188 -- The expressions must be analyzed in full, to make sure that all error
189 -- processing is done (they have only been preanalyzed). If the expression
190 -- is not an entity or literal, its analysis may generate code which must
191 -- not be executed. In that case we build a function body to hold that
192 -- code. This wrapper function serves no other purpose (it used to be
193 -- called to evaluate the default, but now the default is inlined at each
194 -- point of call).
196 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
197 -- Typ is a record or array type that is being frozen. This routine sets
198 -- the default component alignment from the scope stack values if the
199 -- alignment is otherwise not specified.
201 procedure Set_SSO_From_Default (T : Entity_Id);
202 -- T is a record or array type that is being frozen. If it is a base type,
203 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
204 -- will be set appropriately. Note that an explicit occurrence of aspect
205 -- Scalar_Storage_Order or an explicit setting of this aspect with an
206 -- attribute definition clause occurs, then these two flags are reset in
207 -- any case, so call will have no effect.
209 procedure Undelay_Type (T : Entity_Id);
210 -- T is a type of a component that we know to be an Itype. We don't want
211 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
212 -- Full_View or Corresponding_Record_Type.
214 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
215 -- Expr is the expression for an address clause for entity Nam whose type
216 -- is Typ. If Typ has a default initialization, and there is no explicit
217 -- initialization in the source declaration, check whether the address
218 -- clause might cause overlaying of an entity, and emit a warning on the
219 -- side effect that the initialization will cause.
221 -------------------------------
222 -- Adjust_Esize_For_Alignment --
223 -------------------------------
225 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
226 Align : Uint;
228 begin
229 if Known_Esize (Typ) and then Known_Alignment (Typ) then
230 Align := Alignment_In_Bits (Typ);
232 if Align > Esize (Typ)
233 and then Align <= Standard_Long_Long_Integer_Size
234 then
235 Set_Esize (Typ, Align);
236 end if;
237 end if;
238 end Adjust_Esize_For_Alignment;
240 ------------------------------------
241 -- Build_And_Analyze_Renamed_Body --
242 ------------------------------------
244 procedure Build_And_Analyze_Renamed_Body
245 (Decl : Node_Id;
246 New_S : Entity_Id;
247 After : in out Node_Id)
249 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
250 Ent : constant Entity_Id := Defining_Entity (Decl);
251 Body_Node : Node_Id;
252 Renamed_Subp : Entity_Id;
254 begin
255 -- If the renamed subprogram is intrinsic, there is no need for a
256 -- wrapper body: we set the alias that will be called and expanded which
257 -- completes the declaration. This transformation is only legal if the
258 -- renamed entity has already been elaborated.
260 -- Note that it is legal for a renaming_as_body to rename an intrinsic
261 -- subprogram, as long as the renaming occurs before the new entity
262 -- is frozen (RM 8.5.4 (5)).
264 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
265 and then Is_Entity_Name (Name (Body_Decl))
266 then
267 Renamed_Subp := Entity (Name (Body_Decl));
268 else
269 Renamed_Subp := Empty;
270 end if;
272 if Present (Renamed_Subp)
273 and then Is_Intrinsic_Subprogram (Renamed_Subp)
274 and then
275 (not In_Same_Source_Unit (Renamed_Subp, Ent)
276 or else Sloc (Renamed_Subp) < Sloc (Ent))
278 -- We can make the renaming entity intrinsic if the renamed function
279 -- has an interface name, or if it is one of the shift/rotate
280 -- operations known to the compiler.
282 and then
283 (Present (Interface_Name (Renamed_Subp))
284 or else Nam_In (Chars (Renamed_Subp), Name_Rotate_Left,
285 Name_Rotate_Right,
286 Name_Shift_Left,
287 Name_Shift_Right,
288 Name_Shift_Right_Arithmetic))
289 then
290 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
292 if Present (Alias (Renamed_Subp)) then
293 Set_Alias (Ent, Alias (Renamed_Subp));
294 else
295 Set_Alias (Ent, Renamed_Subp);
296 end if;
298 Set_Is_Intrinsic_Subprogram (Ent);
299 Set_Has_Completion (Ent);
301 else
302 Body_Node := Build_Renamed_Body (Decl, New_S);
303 Insert_After (After, Body_Node);
304 Mark_Rewrite_Insertion (Body_Node);
305 Analyze (Body_Node);
306 After := Body_Node;
307 end if;
308 end Build_And_Analyze_Renamed_Body;
310 ------------------------
311 -- Build_Renamed_Body --
312 ------------------------
314 function Build_Renamed_Body
315 (Decl : Node_Id;
316 New_S : Entity_Id) return Node_Id
318 Loc : constant Source_Ptr := Sloc (New_S);
319 -- We use for the source location of the renamed body, the location of
320 -- the spec entity. It might seem more natural to use the location of
321 -- the renaming declaration itself, but that would be wrong, since then
322 -- the body we create would look as though it was created far too late,
323 -- and this could cause problems with elaboration order analysis,
324 -- particularly in connection with instantiations.
326 N : constant Node_Id := Unit_Declaration_Node (New_S);
327 Nam : constant Node_Id := Name (N);
328 Old_S : Entity_Id;
329 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
330 Actuals : List_Id := No_List;
331 Call_Node : Node_Id;
332 Call_Name : Node_Id;
333 Body_Node : Node_Id;
334 Formal : Entity_Id;
335 O_Formal : Entity_Id;
336 Param_Spec : Node_Id;
338 Pref : Node_Id := Empty;
339 -- If the renamed entity is a primitive operation given in prefix form,
340 -- the prefix is the target object and it has to be added as the first
341 -- actual in the generated call.
343 begin
344 -- Determine the entity being renamed, which is the target of the call
345 -- statement. If the name is an explicit dereference, this is a renaming
346 -- of a subprogram type rather than a subprogram. The name itself is
347 -- fully analyzed.
349 if Nkind (Nam) = N_Selected_Component then
350 Old_S := Entity (Selector_Name (Nam));
352 elsif Nkind (Nam) = N_Explicit_Dereference then
353 Old_S := Etype (Nam);
355 elsif Nkind (Nam) = N_Indexed_Component then
356 if Is_Entity_Name (Prefix (Nam)) then
357 Old_S := Entity (Prefix (Nam));
358 else
359 Old_S := Entity (Selector_Name (Prefix (Nam)));
360 end if;
362 elsif Nkind (Nam) = N_Character_Literal then
363 Old_S := Etype (New_S);
365 else
366 Old_S := Entity (Nam);
367 end if;
369 if Is_Entity_Name (Nam) then
371 -- If the renamed entity is a predefined operator, retain full name
372 -- to ensure its visibility.
374 if Ekind (Old_S) = E_Operator
375 and then Nkind (Nam) = N_Expanded_Name
376 then
377 Call_Name := New_Copy (Name (N));
378 else
379 Call_Name := New_Occurrence_Of (Old_S, Loc);
380 end if;
382 else
383 if Nkind (Nam) = N_Selected_Component
384 and then Present (First_Formal (Old_S))
385 and then
386 (Is_Controlling_Formal (First_Formal (Old_S))
387 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
388 then
390 -- Retrieve the target object, to be added as a first actual
391 -- in the call.
393 Call_Name := New_Occurrence_Of (Old_S, Loc);
394 Pref := Prefix (Nam);
396 else
397 Call_Name := New_Copy (Name (N));
398 end if;
400 -- Original name may have been overloaded, but is fully resolved now
402 Set_Is_Overloaded (Call_Name, False);
403 end if;
405 -- For simple renamings, subsequent calls can be expanded directly as
406 -- calls to the renamed entity. The body must be generated in any case
407 -- for calls that may appear elsewhere. This is not done in the case
408 -- where the subprogram is an instantiation because the actual proper
409 -- body has not been built yet.
411 if Ekind_In (Old_S, E_Function, E_Procedure)
412 and then Nkind (Decl) = N_Subprogram_Declaration
413 and then not Is_Generic_Instance (Old_S)
414 then
415 Set_Body_To_Inline (Decl, Old_S);
416 end if;
418 -- Check whether the return type is a limited view. If the subprogram
419 -- is already frozen the generated body may have a non-limited view
420 -- of the type, that must be used, because it is the one in the spec
421 -- of the renaming declaration.
423 if Ekind (Old_S) = E_Function
424 and then Is_Entity_Name (Result_Definition (Spec))
425 then
426 declare
427 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
428 begin
429 if Has_Non_Limited_View (Ret_Type) then
430 Set_Result_Definition
431 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
432 end if;
433 end;
434 end if;
436 -- The body generated for this renaming is an internal artifact, and
437 -- does not constitute a freeze point for the called entity.
439 Set_Must_Not_Freeze (Call_Name);
441 Formal := First_Formal (Defining_Entity (Decl));
443 if Present (Pref) then
444 declare
445 Pref_Type : constant Entity_Id := Etype (Pref);
446 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
448 begin
449 -- The controlling formal may be an access parameter, or the
450 -- actual may be an access value, so adjust accordingly.
452 if Is_Access_Type (Pref_Type)
453 and then not Is_Access_Type (Form_Type)
454 then
455 Actuals := New_List
456 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
458 elsif Is_Access_Type (Form_Type)
459 and then not Is_Access_Type (Pref)
460 then
461 Actuals :=
462 New_List (
463 Make_Attribute_Reference (Loc,
464 Attribute_Name => Name_Access,
465 Prefix => Relocate_Node (Pref)));
466 else
467 Actuals := New_List (Pref);
468 end if;
469 end;
471 elsif Present (Formal) then
472 Actuals := New_List;
474 else
475 Actuals := No_List;
476 end if;
478 if Present (Formal) then
479 while Present (Formal) loop
480 Append (New_Occurrence_Of (Formal, Loc), Actuals);
481 Next_Formal (Formal);
482 end loop;
483 end if;
485 -- If the renamed entity is an entry, inherit its profile. For other
486 -- renamings as bodies, both profiles must be subtype conformant, so it
487 -- is not necessary to replace the profile given in the declaration.
488 -- However, default values that are aggregates are rewritten when
489 -- partially analyzed, so we recover the original aggregate to insure
490 -- that subsequent conformity checking works. Similarly, if the default
491 -- expression was constant-folded, recover the original expression.
493 Formal := First_Formal (Defining_Entity (Decl));
495 if Present (Formal) then
496 O_Formal := First_Formal (Old_S);
497 Param_Spec := First (Parameter_Specifications (Spec));
498 while Present (Formal) loop
499 if Is_Entry (Old_S) then
500 if Nkind (Parameter_Type (Param_Spec)) /=
501 N_Access_Definition
502 then
503 Set_Etype (Formal, Etype (O_Formal));
504 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
505 end if;
507 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
508 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
509 Nkind (Default_Value (O_Formal))
510 then
511 Set_Expression (Param_Spec,
512 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
513 end if;
515 Next_Formal (Formal);
516 Next_Formal (O_Formal);
517 Next (Param_Spec);
518 end loop;
519 end if;
521 -- If the renamed entity is a function, the generated body contains a
522 -- return statement. Otherwise, build a procedure call. If the entity is
523 -- an entry, subsequent analysis of the call will transform it into the
524 -- proper entry or protected operation call. If the renamed entity is
525 -- a character literal, return it directly.
527 if Ekind (Old_S) = E_Function
528 or else Ekind (Old_S) = E_Operator
529 or else (Ekind (Old_S) = E_Subprogram_Type
530 and then Etype (Old_S) /= Standard_Void_Type)
531 then
532 Call_Node :=
533 Make_Simple_Return_Statement (Loc,
534 Expression =>
535 Make_Function_Call (Loc,
536 Name => Call_Name,
537 Parameter_Associations => Actuals));
539 elsif Ekind (Old_S) = E_Enumeration_Literal then
540 Call_Node :=
541 Make_Simple_Return_Statement (Loc,
542 Expression => New_Occurrence_Of (Old_S, Loc));
544 elsif Nkind (Nam) = N_Character_Literal then
545 Call_Node :=
546 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
548 else
549 Call_Node :=
550 Make_Procedure_Call_Statement (Loc,
551 Name => Call_Name,
552 Parameter_Associations => Actuals);
553 end if;
555 -- Create entities for subprogram body and formals
557 Set_Defining_Unit_Name (Spec,
558 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
560 Param_Spec := First (Parameter_Specifications (Spec));
561 while Present (Param_Spec) loop
562 Set_Defining_Identifier (Param_Spec,
563 Make_Defining_Identifier (Loc,
564 Chars => Chars (Defining_Identifier (Param_Spec))));
565 Next (Param_Spec);
566 end loop;
568 Body_Node :=
569 Make_Subprogram_Body (Loc,
570 Specification => Spec,
571 Declarations => New_List,
572 Handled_Statement_Sequence =>
573 Make_Handled_Sequence_Of_Statements (Loc,
574 Statements => New_List (Call_Node)));
576 if Nkind (Decl) /= N_Subprogram_Declaration then
577 Rewrite (N,
578 Make_Subprogram_Declaration (Loc,
579 Specification => Specification (N)));
580 end if;
582 -- Link the body to the entity whose declaration it completes. If
583 -- the body is analyzed when the renamed entity is frozen, it may
584 -- be necessary to restore the proper scope (see package Exp_Ch13).
586 if Nkind (N) = N_Subprogram_Renaming_Declaration
587 and then Present (Corresponding_Spec (N))
588 then
589 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
590 else
591 Set_Corresponding_Spec (Body_Node, New_S);
592 end if;
594 return Body_Node;
595 end Build_Renamed_Body;
597 --------------------------
598 -- Check_Address_Clause --
599 --------------------------
601 procedure Check_Address_Clause (E : Entity_Id) is
602 Addr : constant Node_Id := Address_Clause (E);
603 Typ : constant Entity_Id := Etype (E);
604 Decl : Node_Id;
605 Expr : Node_Id;
606 Init : Node_Id;
607 Lhs : Node_Id;
608 Tag_Assign : Node_Id;
610 begin
611 if Present (Addr) then
613 -- For a deferred constant, the initialization value is on full view
615 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
616 Decl := Declaration_Node (Full_View (E));
617 else
618 Decl := Declaration_Node (E);
619 end if;
621 Expr := Expression (Addr);
623 if Needs_Constant_Address (Decl, Typ) then
624 Check_Constant_Address_Clause (Expr, E);
626 -- Has_Delayed_Freeze was set on E when the address clause was
627 -- analyzed, and must remain set because we want the address
628 -- clause to be elaborated only after any entity it references
629 -- has been elaborated.
630 end if;
632 -- If Rep_Clauses are to be ignored, remove address clause from
633 -- list attached to entity, because it may be illegal for gigi,
634 -- for example by breaking order of elaboration..
636 if Ignore_Rep_Clauses then
637 declare
638 Rep : Node_Id;
640 begin
641 Rep := First_Rep_Item (E);
643 if Rep = Addr then
644 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
646 else
647 while Present (Rep)
648 and then Next_Rep_Item (Rep) /= Addr
649 loop
650 Rep := Next_Rep_Item (Rep);
651 end loop;
652 end if;
654 if Present (Rep) then
655 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
656 end if;
657 end;
659 -- And now remove the address clause
661 Kill_Rep_Clause (Addr);
663 elsif not Error_Posted (Expr)
664 and then not Needs_Finalization (Typ)
665 then
666 Warn_Overlay (Expr, Typ, Name (Addr));
667 end if;
669 Init := Expression (Decl);
671 -- If a variable, or a non-imported constant, overlays a constant
672 -- object and has an initialization value, then the initialization
673 -- may end up writing into read-only memory. Detect the cases of
674 -- statically identical values and remove the initialization. In
675 -- the other cases, give a warning. We will give other warnings
676 -- later for the variable if it is assigned.
678 if (Ekind (E) = E_Variable
679 or else (Ekind (E) = E_Constant
680 and then not Is_Imported (E)))
681 and then Overlays_Constant (E)
682 and then Present (Init)
683 then
684 declare
685 O_Ent : Entity_Id;
686 Off : Boolean;
688 begin
689 Find_Overlaid_Entity (Addr, O_Ent, Off);
691 if Ekind (O_Ent) = E_Constant
692 and then Etype (O_Ent) = Typ
693 and then Present (Constant_Value (O_Ent))
694 and then Compile_Time_Compare
695 (Init,
696 Constant_Value (O_Ent),
697 Assume_Valid => True) = EQ
698 then
699 Set_No_Initialization (Decl);
700 return;
702 elsif Comes_From_Source (Init)
703 and then Address_Clause_Overlay_Warnings
704 then
705 Error_Msg_Sloc := Sloc (Addr);
706 Error_Msg_NE
707 ("??constant& may be modified via address clause#",
708 Decl, O_Ent);
709 end if;
710 end;
711 end if;
713 -- Remove side effects from initial expression, except in the case of
714 -- limited build-in-place calls and aggregates, which have their own
715 -- expansion elsewhere. This exception is necessary to avoid copying
716 -- limited objects.
718 if Present (Init) and then not Is_Limited_View (Typ) then
720 -- Capture initialization value at point of declaration, and make
721 -- explicit assignment legal, because object may be a constant.
723 Remove_Side_Effects (Init);
724 Lhs := New_Occurrence_Of (E, Sloc (Decl));
725 Set_Assignment_OK (Lhs);
727 -- Move initialization to freeze actions, once the object has
728 -- been frozen and the address clause alignment check has been
729 -- performed.
731 Append_Freeze_Action (E,
732 Make_Assignment_Statement (Sloc (Decl),
733 Name => Lhs,
734 Expression => Expression (Decl)));
736 Set_No_Initialization (Decl);
738 -- If the object is tagged, check whether the tag must be
739 -- reassigned explicitly.
741 Tag_Assign := Make_Tag_Assignment (Decl);
742 if Present (Tag_Assign) then
743 Append_Freeze_Action (E, Tag_Assign);
744 end if;
745 end if;
746 end if;
747 end Check_Address_Clause;
749 -----------------------------
750 -- Check_Compile_Time_Size --
751 -----------------------------
753 procedure Check_Compile_Time_Size (T : Entity_Id) is
755 procedure Set_Small_Size (T : Entity_Id; S : Uint);
756 -- Sets the compile time known size (64 bits or less) in the RM_Size
757 -- field of T, checking for a size clause that was given which attempts
758 -- to give a smaller size.
760 function Size_Known (T : Entity_Id) return Boolean;
761 -- Recursive function that does all the work
763 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
764 -- If T is a constrained subtype, its size is not known if any of its
765 -- discriminant constraints is not static and it is not a null record.
766 -- The test is conservative and doesn't check that the components are
767 -- in fact constrained by non-static discriminant values. Could be made
768 -- more precise ???
770 --------------------
771 -- Set_Small_Size --
772 --------------------
774 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
775 begin
776 if S > 64 then
777 return;
779 -- Check for bad size clause given
781 elsif Has_Size_Clause (T) then
782 if RM_Size (T) < S then
783 Error_Msg_Uint_1 := S;
784 Error_Msg_NE
785 ("size for& too small, minimum allowed is ^",
786 Size_Clause (T), T);
787 end if;
789 -- Set size if not set already
791 elsif Unknown_RM_Size (T) then
792 Set_RM_Size (T, S);
793 end if;
794 end Set_Small_Size;
796 ----------------
797 -- Size_Known --
798 ----------------
800 function Size_Known (T : Entity_Id) return Boolean is
801 Index : Entity_Id;
802 Comp : Entity_Id;
803 Ctyp : Entity_Id;
804 Low : Node_Id;
805 High : Node_Id;
807 begin
808 if Size_Known_At_Compile_Time (T) then
809 return True;
811 -- Always True for elementary types, even generic formal elementary
812 -- types. We used to return False in the latter case, but the size
813 -- is known at compile time, even in the template, we just do not
814 -- know the exact size but that's not the point of this routine.
816 elsif Is_Elementary_Type (T) or else Is_Task_Type (T) then
817 return True;
819 -- Array types
821 elsif Is_Array_Type (T) then
823 -- String literals always have known size, and we can set it
825 if Ekind (T) = E_String_Literal_Subtype then
826 Set_Small_Size
827 (T, Component_Size (T) * String_Literal_Length (T));
828 return True;
830 -- Unconstrained types never have known at compile time size
832 elsif not Is_Constrained (T) then
833 return False;
835 -- Don't do any recursion on type with error posted, since we may
836 -- have a malformed type that leads us into a loop.
838 elsif Error_Posted (T) then
839 return False;
841 -- Otherwise if component size unknown, then array size unknown
843 elsif not Size_Known (Component_Type (T)) then
844 return False;
845 end if;
847 -- Check for all indexes static, and also compute possible size
848 -- (in case it is not greater than 64 and may be packable).
850 declare
851 Size : Uint := Component_Size (T);
852 Dim : Uint;
854 begin
855 Index := First_Index (T);
856 while Present (Index) loop
857 if Nkind (Index) = N_Range then
858 Get_Index_Bounds (Index, Low, High);
860 elsif Error_Posted (Scalar_Range (Etype (Index))) then
861 return False;
863 else
864 Low := Type_Low_Bound (Etype (Index));
865 High := Type_High_Bound (Etype (Index));
866 end if;
868 if not Compile_Time_Known_Value (Low)
869 or else not Compile_Time_Known_Value (High)
870 or else Etype (Index) = Any_Type
871 then
872 return False;
874 else
875 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
877 if Dim >= 0 then
878 Size := Size * Dim;
879 else
880 Size := Uint_0;
881 end if;
882 end if;
884 Next_Index (Index);
885 end loop;
887 Set_Small_Size (T, Size);
888 return True;
889 end;
891 -- For non-generic private types, go to underlying type if present
893 elsif Is_Private_Type (T)
894 and then not Is_Generic_Type (T)
895 and then Present (Underlying_Type (T))
896 then
897 -- Don't do any recursion on type with error posted, since we may
898 -- have a malformed type that leads us into a loop.
900 if Error_Posted (T) then
901 return False;
902 else
903 return Size_Known (Underlying_Type (T));
904 end if;
906 -- Record types
908 elsif Is_Record_Type (T) then
910 -- A class-wide type is never considered to have a known size
912 if Is_Class_Wide_Type (T) then
913 return False;
915 -- A subtype of a variant record must not have non-static
916 -- discriminated components.
918 elsif T /= Base_Type (T)
919 and then not Static_Discriminated_Components (T)
920 then
921 return False;
923 -- Don't do any recursion on type with error posted, since we may
924 -- have a malformed type that leads us into a loop.
926 elsif Error_Posted (T) then
927 return False;
928 end if;
930 -- Now look at the components of the record
932 declare
933 -- The following two variables are used to keep track of the
934 -- size of packed records if we can tell the size of the packed
935 -- record in the front end. Packed_Size_Known is True if so far
936 -- we can figure out the size. It is initialized to True for a
937 -- packed record, unless the record has discriminants or atomic
938 -- components or independent components.
940 -- The reason we eliminate the discriminated case is that
941 -- we don't know the way the back end lays out discriminated
942 -- packed records. If Packed_Size_Known is True, then
943 -- Packed_Size is the size in bits so far.
945 Packed_Size_Known : Boolean :=
946 Is_Packed (T)
947 and then not Has_Discriminants (T)
948 and then not Has_Atomic_Components (T)
949 and then not Has_Independent_Components (T);
951 Packed_Size : Uint := Uint_0;
952 -- Size in bits so far
954 begin
955 -- Test for variant part present
957 if Has_Discriminants (T)
958 and then Present (Parent (T))
959 and then Nkind (Parent (T)) = N_Full_Type_Declaration
960 and then Nkind (Type_Definition (Parent (T))) =
961 N_Record_Definition
962 and then not Null_Present (Type_Definition (Parent (T)))
963 and then
964 Present (Variant_Part
965 (Component_List (Type_Definition (Parent (T)))))
966 then
967 -- If variant part is present, and type is unconstrained,
968 -- then we must have defaulted discriminants, or a size
969 -- clause must be present for the type, or else the size
970 -- is definitely not known at compile time.
972 if not Is_Constrained (T)
973 and then
974 No (Discriminant_Default_Value (First_Discriminant (T)))
975 and then Unknown_RM_Size (T)
976 then
977 return False;
978 end if;
979 end if;
981 -- Loop through components
983 Comp := First_Component_Or_Discriminant (T);
984 while Present (Comp) loop
985 Ctyp := Etype (Comp);
987 -- We do not know the packed size if there is a component
988 -- clause present (we possibly could, but this would only
989 -- help in the case of a record with partial rep clauses.
990 -- That's because in the case of full rep clauses, the
991 -- size gets figured out anyway by a different circuit).
993 if Present (Component_Clause (Comp)) then
994 Packed_Size_Known := False;
995 end if;
997 -- We do not know the packed size for an atomic/VFA type
998 -- or component, or an independent type or component, or a
999 -- by-reference type or aliased component (because packing
1000 -- does not touch these).
1002 if Is_Atomic_Or_VFA (Ctyp)
1003 or else Is_Atomic_Or_VFA (Comp)
1004 or else Is_Independent (Ctyp)
1005 or else Is_Independent (Comp)
1006 or else Is_By_Reference_Type (Ctyp)
1007 or else Is_Aliased (Comp)
1008 then
1009 Packed_Size_Known := False;
1010 end if;
1012 -- We need to identify a component that is an array where
1013 -- the index type is an enumeration type with non-standard
1014 -- representation, and some bound of the type depends on a
1015 -- discriminant.
1017 -- This is because gigi computes the size by doing a
1018 -- substitution of the appropriate discriminant value in
1019 -- the size expression for the base type, and gigi is not
1020 -- clever enough to evaluate the resulting expression (which
1021 -- involves a call to rep_to_pos) at compile time.
1023 -- It would be nice if gigi would either recognize that
1024 -- this expression can be computed at compile time, or
1025 -- alternatively figured out the size from the subtype
1026 -- directly, where all the information is at hand ???
1028 if Is_Array_Type (Etype (Comp))
1029 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1030 then
1031 declare
1032 Ocomp : constant Entity_Id :=
1033 Original_Record_Component (Comp);
1034 OCtyp : constant Entity_Id := Etype (Ocomp);
1035 Ind : Node_Id;
1036 Indtyp : Entity_Id;
1037 Lo, Hi : Node_Id;
1039 begin
1040 Ind := First_Index (OCtyp);
1041 while Present (Ind) loop
1042 Indtyp := Etype (Ind);
1044 if Is_Enumeration_Type (Indtyp)
1045 and then Has_Non_Standard_Rep (Indtyp)
1046 then
1047 Lo := Type_Low_Bound (Indtyp);
1048 Hi := Type_High_Bound (Indtyp);
1050 if Is_Entity_Name (Lo)
1051 and then Ekind (Entity (Lo)) = E_Discriminant
1052 then
1053 return False;
1055 elsif Is_Entity_Name (Hi)
1056 and then Ekind (Entity (Hi)) = E_Discriminant
1057 then
1058 return False;
1059 end if;
1060 end if;
1062 Next_Index (Ind);
1063 end loop;
1064 end;
1065 end if;
1067 -- Clearly size of record is not known if the size of one of
1068 -- the components is not known.
1070 if not Size_Known (Ctyp) then
1071 return False;
1072 end if;
1074 -- Accumulate packed size if possible
1076 if Packed_Size_Known then
1078 -- We can deal with elementary types, small packed arrays
1079 -- if the representation is a modular type and also small
1080 -- record types (if the size is not greater than 64, but
1081 -- the condition is checked by Set_Small_Size).
1083 if Is_Elementary_Type (Ctyp)
1084 or else (Is_Array_Type (Ctyp)
1085 and then Present
1086 (Packed_Array_Impl_Type (Ctyp))
1087 and then Is_Modular_Integer_Type
1088 (Packed_Array_Impl_Type (Ctyp)))
1089 or else Is_Record_Type (Ctyp)
1090 then
1091 -- If RM_Size is known and static, then we can keep
1092 -- accumulating the packed size.
1094 if Known_Static_RM_Size (Ctyp) then
1096 Packed_Size := Packed_Size + RM_Size (Ctyp);
1098 -- If we have a field whose RM_Size is not known then
1099 -- we can't figure out the packed size here.
1101 else
1102 Packed_Size_Known := False;
1103 end if;
1105 -- For other types we can't figure out the packed size
1107 else
1108 Packed_Size_Known := False;
1109 end if;
1110 end if;
1112 Next_Component_Or_Discriminant (Comp);
1113 end loop;
1115 if Packed_Size_Known then
1116 Set_Small_Size (T, Packed_Size);
1117 end if;
1119 return True;
1120 end;
1122 -- All other cases, size not known at compile time
1124 else
1125 return False;
1126 end if;
1127 end Size_Known;
1129 -------------------------------------
1130 -- Static_Discriminated_Components --
1131 -------------------------------------
1133 function Static_Discriminated_Components
1134 (T : Entity_Id) return Boolean
1136 Constraint : Elmt_Id;
1138 begin
1139 if Has_Discriminants (T)
1140 and then Present (Discriminant_Constraint (T))
1141 and then Present (First_Component (T))
1142 then
1143 Constraint := First_Elmt (Discriminant_Constraint (T));
1144 while Present (Constraint) loop
1145 if not Compile_Time_Known_Value (Node (Constraint)) then
1146 return False;
1147 end if;
1149 Next_Elmt (Constraint);
1150 end loop;
1151 end if;
1153 return True;
1154 end Static_Discriminated_Components;
1156 -- Start of processing for Check_Compile_Time_Size
1158 begin
1159 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1160 end Check_Compile_Time_Size;
1162 -----------------------------------
1163 -- Check_Component_Storage_Order --
1164 -----------------------------------
1166 procedure Check_Component_Storage_Order
1167 (Encl_Type : Entity_Id;
1168 Comp : Entity_Id;
1169 ADC : Node_Id;
1170 Comp_ADC_Present : out Boolean)
1172 Comp_Base : Entity_Id;
1173 Comp_ADC : Node_Id;
1174 Encl_Base : Entity_Id;
1175 Err_Node : Node_Id;
1177 Component_Aliased : Boolean;
1179 Comp_Byte_Aligned : Boolean := False;
1180 -- Set for the record case, True if Comp is aligned on byte boundaries
1181 -- (in which case it is allowed to have different storage order).
1183 Comp_SSO_Differs : Boolean;
1184 -- Set True when the component is a nested composite, and it does not
1185 -- have the same scalar storage order as Encl_Type.
1187 begin
1188 -- Record case
1190 if Present (Comp) then
1191 Err_Node := Comp;
1192 Comp_Base := Etype (Comp);
1194 if Is_Tag (Comp) then
1195 Comp_Byte_Aligned := True;
1196 Component_Aliased := False;
1198 else
1199 -- If a component clause is present, check if the component starts
1200 -- and ends on byte boundaries. Otherwise conservatively assume it
1201 -- does so only in the case where the record is not packed.
1203 if Present (Component_Clause (Comp)) then
1204 Comp_Byte_Aligned :=
1205 (Normalized_First_Bit (Comp) mod System_Storage_Unit = 0)
1206 and then
1207 (Esize (Comp) mod System_Storage_Unit = 0);
1208 else
1209 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1210 end if;
1212 Component_Aliased := Is_Aliased (Comp);
1213 end if;
1215 -- Array case
1217 else
1218 Err_Node := Encl_Type;
1219 Comp_Base := Component_Type (Encl_Type);
1221 Component_Aliased := Has_Aliased_Components (Encl_Type);
1222 end if;
1224 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1225 -- the attribute definition clause is attached to the first subtype.
1226 -- Also, if the base type is incomplete or private, go to full view
1227 -- if known
1229 Encl_Base := Base_Type (Encl_Type);
1230 if Present (Underlying_Type (Encl_Base)) then
1231 Encl_Base := Underlying_Type (Encl_Base);
1232 end if;
1234 Comp_Base := Base_Type (Comp_Base);
1235 if Present (Underlying_Type (Comp_Base)) then
1236 Comp_Base := Underlying_Type (Comp_Base);
1237 end if;
1239 Comp_ADC :=
1240 Get_Attribute_Definition_Clause
1241 (First_Subtype (Comp_Base), Attribute_Scalar_Storage_Order);
1242 Comp_ADC_Present := Present (Comp_ADC);
1244 -- Case of record or array component: check storage order compatibility.
1245 -- But, if the record has Complex_Representation, then it is treated as
1246 -- a scalar in the back end so the storage order is irrelevant.
1248 if (Is_Record_Type (Comp_Base)
1249 and then not Has_Complex_Representation (Comp_Base))
1250 or else Is_Array_Type (Comp_Base)
1251 then
1252 Comp_SSO_Differs :=
1253 Reverse_Storage_Order (Encl_Base) /=
1254 Reverse_Storage_Order (Comp_Base);
1256 -- Parent and extension must have same storage order
1258 if Present (Comp) and then Chars (Comp) = Name_uParent then
1259 if Comp_SSO_Differs then
1260 Error_Msg_N
1261 ("record extension must have same scalar storage order as "
1262 & "parent", Err_Node);
1263 end if;
1265 -- If component and composite SSO differs, check that component
1266 -- falls on byte boundaries and isn't bit packed.
1268 elsif Comp_SSO_Differs then
1270 -- Component SSO differs from enclosing composite:
1272 -- Reject if composite is a bit-packed array, as it is rewritten
1273 -- into an array of scalars.
1275 if Is_Bit_Packed_Array (Encl_Base) then
1276 Error_Msg_N
1277 ("type of packed array must have same scalar storage order "
1278 & "as component", Err_Node);
1280 -- Reject if not byte aligned
1282 elsif Is_Record_Type (Encl_Base)
1283 and then not Comp_Byte_Aligned
1284 then
1285 Error_Msg_N
1286 ("type of non-byte-aligned component must have same scalar "
1287 & "storage order as enclosing composite", Err_Node);
1289 -- Warn if specified only for the outer composite
1291 elsif Present (ADC) and then No (Comp_ADC) then
1292 Error_Msg_NE
1293 ("scalar storage order specified for & does not apply to "
1294 & "component?", Err_Node, Encl_Base);
1295 end if;
1296 end if;
1298 -- Enclosing type has explicit SSO: non-composite component must not
1299 -- be aliased.
1301 elsif Present (ADC) and then Component_Aliased then
1302 Error_Msg_N
1303 ("aliased component not permitted for type with explicit "
1304 & "Scalar_Storage_Order", Err_Node);
1305 end if;
1306 end Check_Component_Storage_Order;
1308 -----------------------------
1309 -- Check_Debug_Info_Needed --
1310 -----------------------------
1312 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1313 begin
1314 if Debug_Info_Off (T) then
1315 return;
1317 elsif Comes_From_Source (T)
1318 or else Debug_Generated_Code
1319 or else Debug_Flag_VV
1320 or else Needs_Debug_Info (T)
1321 then
1322 Set_Debug_Info_Needed (T);
1323 end if;
1324 end Check_Debug_Info_Needed;
1326 -------------------------------
1327 -- Check_Expression_Function --
1328 -------------------------------
1330 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1331 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1332 -- Function to search for deferred constant
1334 -------------------
1335 -- Find_Constant --
1336 -------------------
1338 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1339 begin
1340 -- When a constant is initialized with the result of a dispatching
1341 -- call, the constant declaration is rewritten as a renaming of the
1342 -- displaced function result. This scenario is not a premature use of
1343 -- a constant even though the Has_Completion flag is not set.
1345 if Is_Entity_Name (Nod)
1346 and then Present (Entity (Nod))
1347 and then Ekind (Entity (Nod)) = E_Constant
1348 and then Scope (Entity (Nod)) = Current_Scope
1349 and then Nkind (Declaration_Node (Entity (Nod))) =
1350 N_Object_Declaration
1351 and then not Is_Imported (Entity (Nod))
1352 and then not Has_Completion (Entity (Nod))
1353 and then not Is_Frozen (Entity (Nod))
1354 then
1355 Error_Msg_NE
1356 ("premature use of& in call or instance", N, Entity (Nod));
1358 elsif Nkind (Nod) = N_Attribute_Reference then
1359 Analyze (Prefix (Nod));
1361 if Is_Entity_Name (Prefix (Nod))
1362 and then Is_Type (Entity (Prefix (Nod)))
1363 then
1364 Freeze_Before (N, Entity (Prefix (Nod)));
1365 end if;
1366 end if;
1368 return OK;
1369 end Find_Constant;
1371 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1373 -- Local variables
1375 Decl : Node_Id;
1377 -- Start of processing for Check_Expression_Function
1379 begin
1380 Decl := Original_Node (Unit_Declaration_Node (Nam));
1382 -- The subprogram body created for the expression function is not
1383 -- itself a freeze point.
1385 if Scope (Nam) = Current_Scope
1386 and then Nkind (Decl) = N_Expression_Function
1387 and then Nkind (N) /= N_Subprogram_Body
1388 then
1389 Check_Deferred (Expression (Decl));
1390 end if;
1391 end Check_Expression_Function;
1393 --------------------------------
1394 -- Check_Inherited_Conditions --
1395 --------------------------------
1397 procedure Check_Inherited_Conditions (R : Entity_Id) is
1398 Prim_Ops : constant Elist_Id := Primitive_Operations (R);
1399 Decls : List_Id;
1400 Needs_Wrapper : Boolean;
1401 Op_Node : Elmt_Id;
1402 Par_Prim : Entity_Id;
1403 Prim : Entity_Id;
1405 procedure Build_Inherited_Condition_Pragmas (Subp : Entity_Id);
1406 -- Build corresponding pragmas for an operation whose ancestor has
1407 -- class-wide pre/postconditions. If the operation is inherited, the
1408 -- pragmas force the creation of a wrapper for the inherited operation.
1409 -- If the ancestor is being overridden, the pragmas are constructed only
1410 -- to verify their legality, in case they contain calls to other
1411 -- primitives that may haven been overridden.
1413 ---------------------------------------
1414 -- Build_Inherited_Condition_Pragmas --
1415 ---------------------------------------
1417 procedure Build_Inherited_Condition_Pragmas (Subp : Entity_Id) is
1418 A_Post : Node_Id;
1419 A_Pre : Node_Id;
1420 New_Prag : Node_Id;
1422 begin
1423 A_Pre := Get_Class_Wide_Pragma (Par_Prim, Pragma_Precondition);
1425 if Present (A_Pre) then
1426 New_Prag := New_Copy_Tree (A_Pre);
1427 Build_Class_Wide_Expression
1428 (Prag => New_Prag,
1429 Subp => Prim,
1430 Par_Subp => Par_Prim,
1431 Adjust_Sloc => False,
1432 Needs_Wrapper => Needs_Wrapper);
1434 if Needs_Wrapper
1435 and then not Comes_From_Source (Subp)
1436 and then Expander_Active
1437 then
1438 Append (New_Prag, Decls);
1439 end if;
1440 end if;
1442 A_Post := Get_Class_Wide_Pragma (Par_Prim, Pragma_Postcondition);
1444 if Present (A_Post) then
1445 New_Prag := New_Copy_Tree (A_Post);
1446 Build_Class_Wide_Expression
1447 (Prag => New_Prag,
1448 Subp => Prim,
1449 Par_Subp => Par_Prim,
1450 Adjust_Sloc => False,
1451 Needs_Wrapper => Needs_Wrapper);
1453 if Needs_Wrapper
1454 and then not Comes_From_Source (Subp)
1455 and then Expander_Active
1456 then
1457 Append (New_Prag, Decls);
1458 end if;
1459 end if;
1460 end Build_Inherited_Condition_Pragmas;
1462 -- Start of processing for Check_Inherited_Conditions
1464 begin
1465 Op_Node := First_Elmt (Prim_Ops);
1466 while Present (Op_Node) loop
1467 Prim := Node (Op_Node);
1469 -- Map the overridden primitive to the overriding one. This takes
1470 -- care of all overridings and is done only once.
1472 if Present (Overridden_Operation (Prim))
1473 and then Comes_From_Source (Prim)
1474 then
1475 Par_Prim := Overridden_Operation (Prim);
1476 Update_Primitives_Mapping (Par_Prim, Prim);
1477 end if;
1479 Next_Elmt (Op_Node);
1480 end loop;
1482 -- Perform validity checks on the inherited conditions of overriding
1483 -- operations, for conformance with LSP, and apply SPARK-specific
1484 -- restrictions on inherited conditions.
1486 Op_Node := First_Elmt (Prim_Ops);
1487 while Present (Op_Node) loop
1488 Prim := Node (Op_Node);
1490 if Present (Overridden_Operation (Prim))
1491 and then Comes_From_Source (Prim)
1492 then
1493 Par_Prim := Overridden_Operation (Prim);
1495 -- Analyze the contract items of the overridden operation, before
1496 -- they are rewritten as pragmas.
1498 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1500 -- In GNATprove mode this is where we can collect the inherited
1501 -- conditions, because we do not create the Check pragmas that
1502 -- normally convey the the modified class-wide conditions on
1503 -- overriding operations.
1505 if GNATprove_Mode then
1506 Collect_Inherited_Class_Wide_Conditions (Prim);
1508 -- Otherwise build the corresponding pragmas to check for legality
1509 -- of the inherited condition.
1511 else
1512 Build_Inherited_Condition_Pragmas (Prim);
1513 end if;
1514 end if;
1516 Next_Elmt (Op_Node);
1517 end loop;
1519 -- Now examine the inherited operations to check whether they require
1520 -- a wrapper to handle inherited conditions that call other primitives,
1521 -- so that LSP can be verified/enforced.
1523 Op_Node := First_Elmt (Prim_Ops);
1524 Needs_Wrapper := False;
1526 while Present (Op_Node) loop
1527 Decls := Empty_List;
1528 Prim := Node (Op_Node);
1530 if not Comes_From_Source (Prim) and then Present (Alias (Prim)) then
1531 Par_Prim := Alias (Prim);
1533 -- Analyze the contract items of the parent operation, and
1534 -- determine whether a wrapper is needed. This is determined
1535 -- when the condition is rewritten in sem_prag, using the
1536 -- mapping between overridden and overriding operations built
1537 -- in the loop above.
1539 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1540 Build_Inherited_Condition_Pragmas (Prim);
1541 end if;
1543 if Needs_Wrapper
1544 and then not Is_Abstract_Subprogram (Par_Prim)
1545 and then Expander_Active
1546 then
1547 -- We need to build a new primitive that overrides the inherited
1548 -- one, and whose inherited expression has been updated above.
1549 -- These expressions are the arguments of pragmas that are part
1550 -- of the declarations of the wrapper. The wrapper holds a single
1551 -- statement that is a call to the class-wide clone, where the
1552 -- controlling actuals are conversions to the corresponding type
1553 -- in the parent primitive:
1555 -- procedure New_Prim (F1 : T1; ...);
1556 -- procedure New_Prim (F1 : T1; ...) is
1557 -- pragma Check (Precondition, Expr);
1558 -- begin
1559 -- Par_Prim_Clone (Par_Type (F1), ...);
1560 -- end;
1562 -- If the primitive is a function the statement is a return
1563 -- statement with a call.
1565 declare
1566 Loc : constant Source_Ptr := Sloc (R);
1567 Par_R : constant Node_Id := Parent (R);
1568 New_Body : Node_Id;
1569 New_Decl : Node_Id;
1570 New_Spec : Node_Id;
1572 begin
1573 New_Spec := Build_Overriding_Spec (Par_Prim, R);
1574 New_Decl :=
1575 Make_Subprogram_Declaration (Loc,
1576 Specification => New_Spec);
1578 -- Insert the declaration and the body of the wrapper after
1579 -- type declaration that generates inherited operation. For
1580 -- a null procedure, the declaration implies a null body.
1582 if Nkind (New_Spec) = N_Procedure_Specification
1583 and then Null_Present (New_Spec)
1584 then
1585 Insert_After_And_Analyze (Par_R, New_Decl);
1587 else
1588 -- Build body as wrapper to a call to the already built
1589 -- class-wide clone.
1591 New_Body :=
1592 Build_Class_Wide_Clone_Call
1593 (Loc, Decls, Par_Prim, New_Spec);
1595 Insert_List_After_And_Analyze
1596 (Par_R, New_List (New_Decl, New_Body));
1597 end if;
1598 end;
1600 Needs_Wrapper := False;
1601 end if;
1603 Next_Elmt (Op_Node);
1604 end loop;
1605 end Check_Inherited_Conditions;
1607 ----------------------------
1608 -- Check_Strict_Alignment --
1609 ----------------------------
1611 procedure Check_Strict_Alignment (E : Entity_Id) is
1612 Comp : Entity_Id;
1614 begin
1615 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1616 Set_Strict_Alignment (E);
1618 elsif Is_Array_Type (E) then
1619 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1621 elsif Is_Record_Type (E) then
1622 if Is_Limited_Record (E) then
1623 Set_Strict_Alignment (E);
1624 return;
1625 end if;
1627 Comp := First_Component (E);
1628 while Present (Comp) loop
1629 if not Is_Type (Comp)
1630 and then (Strict_Alignment (Etype (Comp))
1631 or else Is_Aliased (Comp))
1632 then
1633 Set_Strict_Alignment (E);
1634 return;
1635 end if;
1637 Next_Component (Comp);
1638 end loop;
1639 end if;
1640 end Check_Strict_Alignment;
1642 -------------------------
1643 -- Check_Unsigned_Type --
1644 -------------------------
1646 procedure Check_Unsigned_Type (E : Entity_Id) is
1647 Ancestor : Entity_Id;
1648 Lo_Bound : Node_Id;
1649 Btyp : Entity_Id;
1651 begin
1652 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1653 return;
1654 end if;
1656 -- Do not attempt to analyze case where range was in error
1658 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
1659 return;
1660 end if;
1662 -- The situation that is nontrivial is something like:
1664 -- subtype x1 is integer range -10 .. +10;
1665 -- subtype x2 is x1 range 0 .. V1;
1666 -- subtype x3 is x2 range V2 .. V3;
1667 -- subtype x4 is x3 range V4 .. V5;
1669 -- where Vn are variables. Here the base type is signed, but we still
1670 -- know that x4 is unsigned because of the lower bound of x2.
1672 -- The only way to deal with this is to look up the ancestor chain
1674 Ancestor := E;
1675 loop
1676 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1677 return;
1678 end if;
1680 Lo_Bound := Type_Low_Bound (Ancestor);
1682 if Compile_Time_Known_Value (Lo_Bound) then
1683 if Expr_Rep_Value (Lo_Bound) >= 0 then
1684 Set_Is_Unsigned_Type (E, True);
1685 end if;
1687 return;
1689 else
1690 Ancestor := Ancestor_Subtype (Ancestor);
1692 -- If no ancestor had a static lower bound, go to base type
1694 if No (Ancestor) then
1696 -- Note: the reason we still check for a compile time known
1697 -- value for the base type is that at least in the case of
1698 -- generic formals, we can have bounds that fail this test,
1699 -- and there may be other cases in error situations.
1701 Btyp := Base_Type (E);
1703 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1704 return;
1705 end if;
1707 Lo_Bound := Type_Low_Bound (Base_Type (E));
1709 if Compile_Time_Known_Value (Lo_Bound)
1710 and then Expr_Rep_Value (Lo_Bound) >= 0
1711 then
1712 Set_Is_Unsigned_Type (E, True);
1713 end if;
1715 return;
1716 end if;
1717 end if;
1718 end loop;
1719 end Check_Unsigned_Type;
1721 -----------------------------
1722 -- Is_Atomic_VFA_Aggregate --
1723 -----------------------------
1725 function Is_Atomic_VFA_Aggregate (N : Node_Id) return Boolean is
1726 Loc : constant Source_Ptr := Sloc (N);
1727 New_N : Node_Id;
1728 Par : Node_Id;
1729 Temp : Entity_Id;
1730 Typ : Entity_Id;
1732 begin
1733 Par := Parent (N);
1735 -- Array may be qualified, so find outer context
1737 if Nkind (Par) = N_Qualified_Expression then
1738 Par := Parent (Par);
1739 end if;
1741 if not Comes_From_Source (Par) then
1742 return False;
1743 end if;
1745 case Nkind (Par) is
1746 when N_Assignment_Statement =>
1747 Typ := Etype (Name (Par));
1749 if not Is_Atomic_Or_VFA (Typ)
1750 and then not (Is_Entity_Name (Name (Par))
1751 and then Is_Atomic_Or_VFA (Entity (Name (Par))))
1752 then
1753 return False;
1754 end if;
1756 when N_Object_Declaration =>
1757 Typ := Etype (Defining_Identifier (Par));
1759 if not Is_Atomic_Or_VFA (Typ)
1760 and then not Is_Atomic_Or_VFA (Defining_Identifier (Par))
1761 then
1762 return False;
1763 end if;
1765 when others =>
1766 return False;
1767 end case;
1769 Temp := Make_Temporary (Loc, 'T', N);
1770 New_N :=
1771 Make_Object_Declaration (Loc,
1772 Defining_Identifier => Temp,
1773 Object_Definition => New_Occurrence_Of (Typ, Loc),
1774 Expression => Relocate_Node (N));
1775 Insert_Before (Par, New_N);
1776 Analyze (New_N);
1778 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1779 return True;
1780 end Is_Atomic_VFA_Aggregate;
1782 -----------------------------------------------
1783 -- Explode_Initialization_Compound_Statement --
1784 -----------------------------------------------
1786 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
1787 Init_Stmts : constant Node_Id := Initialization_Statements (E);
1789 begin
1790 if Present (Init_Stmts)
1791 and then Nkind (Init_Stmts) = N_Compound_Statement
1792 then
1793 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
1795 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
1796 -- just removing it, because Freeze_All may rely on this particular
1797 -- Node_Id still being present in the enclosing list to know where to
1798 -- stop freezing.
1800 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
1802 Set_Initialization_Statements (E, Empty);
1803 end if;
1804 end Explode_Initialization_Compound_Statement;
1806 ----------------
1807 -- Freeze_All --
1808 ----------------
1810 -- Note: the easy coding for this procedure would be to just build a
1811 -- single list of freeze nodes and then insert them and analyze them
1812 -- all at once. This won't work, because the analysis of earlier freeze
1813 -- nodes may recursively freeze types which would otherwise appear later
1814 -- on in the freeze list. So we must analyze and expand the freeze nodes
1815 -- as they are generated.
1817 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1818 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1819 -- This is the internal recursive routine that does freezing of entities
1820 -- (but NOT the analysis of default expressions, which should not be
1821 -- recursive, we don't want to analyze those till we are sure that ALL
1822 -- the types are frozen).
1824 --------------------
1825 -- Freeze_All_Ent --
1826 --------------------
1828 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
1829 E : Entity_Id;
1830 Flist : List_Id;
1831 Lastn : Node_Id;
1833 procedure Process_Flist;
1834 -- If freeze nodes are present, insert and analyze, and reset cursor
1835 -- for next insertion.
1837 -------------------
1838 -- Process_Flist --
1839 -------------------
1841 procedure Process_Flist is
1842 begin
1843 if Is_Non_Empty_List (Flist) then
1844 Lastn := Next (After);
1845 Insert_List_After_And_Analyze (After, Flist);
1847 if Present (Lastn) then
1848 After := Prev (Lastn);
1849 else
1850 After := Last (List_Containing (After));
1851 end if;
1852 end if;
1853 end Process_Flist;
1855 -- Start of processing for Freeze_All_Ent
1857 begin
1858 E := From;
1859 while Present (E) loop
1861 -- If the entity is an inner package which is not a package
1862 -- renaming, then its entities must be frozen at this point. Note
1863 -- that such entities do NOT get frozen at the end of the nested
1864 -- package itself (only library packages freeze).
1866 -- Same is true for task declarations, where anonymous records
1867 -- created for entry parameters must be frozen.
1869 if Ekind (E) = E_Package
1870 and then No (Renamed_Object (E))
1871 and then not Is_Child_Unit (E)
1872 and then not Is_Frozen (E)
1873 then
1874 Push_Scope (E);
1876 Install_Visible_Declarations (E);
1877 Install_Private_Declarations (E);
1878 Freeze_All (First_Entity (E), After);
1880 End_Package_Scope (E);
1882 if Is_Generic_Instance (E)
1883 and then Has_Delayed_Freeze (E)
1884 then
1885 Set_Has_Delayed_Freeze (E, False);
1886 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1887 end if;
1889 elsif Ekind (E) in Task_Kind
1890 and then Nkind_In (Parent (E), N_Single_Task_Declaration,
1891 N_Task_Type_Declaration)
1892 then
1893 Push_Scope (E);
1894 Freeze_All (First_Entity (E), After);
1895 End_Scope;
1897 -- For a derived tagged type, we must ensure that all the
1898 -- primitive operations of the parent have been frozen, so that
1899 -- their addresses will be in the parent's dispatch table at the
1900 -- point it is inherited.
1902 elsif Ekind (E) = E_Record_Type
1903 and then Is_Tagged_Type (E)
1904 and then Is_Tagged_Type (Etype (E))
1905 and then Is_Derived_Type (E)
1906 then
1907 declare
1908 Prim_List : constant Elist_Id :=
1909 Primitive_Operations (Etype (E));
1911 Prim : Elmt_Id;
1912 Subp : Entity_Id;
1914 begin
1915 Prim := First_Elmt (Prim_List);
1916 while Present (Prim) loop
1917 Subp := Node (Prim);
1919 if Comes_From_Source (Subp)
1920 and then not Is_Frozen (Subp)
1921 then
1922 Flist := Freeze_Entity (Subp, After);
1923 Process_Flist;
1924 end if;
1926 Next_Elmt (Prim);
1927 end loop;
1928 end;
1929 end if;
1931 if not Is_Frozen (E) then
1932 Flist := Freeze_Entity (E, After);
1933 Process_Flist;
1935 -- If already frozen, and there are delayed aspects, this is where
1936 -- we do the visibility check for these aspects (see Sem_Ch13 spec
1937 -- for a description of how we handle aspect visibility).
1939 elsif Has_Delayed_Aspects (E) then
1941 -- Retrieve the visibility to the discriminants in order to
1942 -- analyze properly the aspects.
1944 Push_Scope_And_Install_Discriminants (E);
1946 declare
1947 Ritem : Node_Id;
1949 begin
1950 Ritem := First_Rep_Item (E);
1951 while Present (Ritem) loop
1952 if Nkind (Ritem) = N_Aspect_Specification
1953 and then Entity (Ritem) = E
1954 and then Is_Delayed_Aspect (Ritem)
1955 then
1956 Check_Aspect_At_End_Of_Declarations (Ritem);
1957 end if;
1959 Ritem := Next_Rep_Item (Ritem);
1960 end loop;
1961 end;
1963 Uninstall_Discriminants_And_Pop_Scope (E);
1964 end if;
1966 -- If an incomplete type is still not frozen, this may be a
1967 -- premature freezing because of a body declaration that follows.
1968 -- Indicate where the freezing took place. Freezing will happen
1969 -- if the body comes from source, but not if it is internally
1970 -- generated, for example as the body of a type invariant.
1972 -- If the freezing is caused by the end of the current declarative
1973 -- part, it is a Taft Amendment type, and there is no error.
1975 if not Is_Frozen (E)
1976 and then Ekind (E) = E_Incomplete_Type
1977 then
1978 declare
1979 Bod : constant Node_Id := Next (After);
1981 begin
1982 -- The presence of a body freezes all entities previously
1983 -- declared in the current list of declarations, but this
1984 -- does not apply if the body does not come from source.
1985 -- A type invariant is transformed into a subprogram body
1986 -- which is placed at the end of the private part of the
1987 -- current package, but this body does not freeze incomplete
1988 -- types that may be declared in this private part.
1990 if (Nkind_In (Bod, N_Entry_Body,
1991 N_Package_Body,
1992 N_Protected_Body,
1993 N_Subprogram_Body,
1994 N_Task_Body)
1995 or else Nkind (Bod) in N_Body_Stub)
1996 and then
1997 List_Containing (After) = List_Containing (Parent (E))
1998 and then Comes_From_Source (Bod)
1999 then
2000 Error_Msg_Sloc := Sloc (Next (After));
2001 Error_Msg_NE
2002 ("type& is frozen# before its full declaration",
2003 Parent (E), E);
2004 end if;
2005 end;
2006 end if;
2008 Next_Entity (E);
2009 end loop;
2010 end Freeze_All_Ent;
2012 -- Local variables
2014 Decl : Node_Id;
2015 E : Entity_Id;
2016 Item : Entity_Id;
2018 -- Start of processing for Freeze_All
2020 begin
2021 Freeze_All_Ent (From, After);
2023 -- Now that all types are frozen, we can deal with default expressions
2024 -- that require us to build a default expression functions. This is the
2025 -- point at which such functions are constructed (after all types that
2026 -- might be used in such expressions have been frozen).
2028 -- For subprograms that are renaming_as_body, we create the wrapper
2029 -- bodies as needed.
2031 -- We also add finalization chains to access types whose designated
2032 -- types are controlled. This is normally done when freezing the type,
2033 -- but this misses recursive type definitions where the later members
2034 -- of the recursion introduce controlled components.
2036 -- Loop through entities
2038 E := From;
2039 while Present (E) loop
2040 if Is_Subprogram (E) then
2041 if not Default_Expressions_Processed (E) then
2042 Process_Default_Expressions (E, After);
2043 end if;
2045 if not Has_Completion (E) then
2046 Decl := Unit_Declaration_Node (E);
2048 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
2049 if Error_Posted (Decl) then
2050 Set_Has_Completion (E);
2051 else
2052 Build_And_Analyze_Renamed_Body (Decl, E, After);
2053 end if;
2055 elsif Nkind (Decl) = N_Subprogram_Declaration
2056 and then Present (Corresponding_Body (Decl))
2057 and then
2058 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl))) =
2059 N_Subprogram_Renaming_Declaration
2060 then
2061 Build_And_Analyze_Renamed_Body
2062 (Decl, Corresponding_Body (Decl), After);
2063 end if;
2064 end if;
2066 -- Freeze the default expressions of entries, entry families, and
2067 -- protected subprograms.
2069 elsif Is_Concurrent_Type (E) then
2070 Item := First_Entity (E);
2071 while Present (Item) loop
2072 if (Is_Entry (Item) or else Is_Subprogram (Item))
2073 and then not Default_Expressions_Processed (Item)
2074 then
2075 Process_Default_Expressions (Item, After);
2076 end if;
2078 Next_Entity (Item);
2079 end loop;
2080 end if;
2082 -- Historical note: We used to create a finalization master for an
2083 -- access type whose designated type is not controlled, but contains
2084 -- private controlled compoments. This form of postprocessing is no
2085 -- longer needed because the finalization master is now created when
2086 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
2088 Next_Entity (E);
2089 end loop;
2090 end Freeze_All;
2092 -----------------------
2093 -- Freeze_And_Append --
2094 -----------------------
2096 procedure Freeze_And_Append
2097 (Ent : Entity_Id;
2098 N : Node_Id;
2099 Result : in out List_Id)
2101 L : constant List_Id := Freeze_Entity (Ent, N);
2102 begin
2103 if Is_Non_Empty_List (L) then
2104 if Result = No_List then
2105 Result := L;
2106 else
2107 Append_List (L, Result);
2108 end if;
2109 end if;
2110 end Freeze_And_Append;
2112 -------------------
2113 -- Freeze_Before --
2114 -------------------
2116 procedure Freeze_Before
2117 (N : Node_Id;
2118 T : Entity_Id;
2119 Do_Freeze_Profile : Boolean := True)
2121 -- Freeze T, then insert the generated Freeze nodes before the node N.
2122 -- Flag Freeze_Profile is used when T is an overloadable entity, and
2123 -- indicates whether its profile should be frozen at the same time.
2125 Freeze_Nodes : constant List_Id :=
2126 Freeze_Entity (T, N, Do_Freeze_Profile);
2127 Pack : constant Entity_Id := Scope (T);
2129 begin
2130 if Ekind (T) = E_Function then
2131 Check_Expression_Function (N, T);
2132 end if;
2134 if Is_Non_Empty_List (Freeze_Nodes) then
2136 -- If the entity is a type declared in an inner package, it may be
2137 -- frozen by an outer declaration before the package itself is
2138 -- frozen. Install the package scope to analyze the freeze nodes,
2139 -- which may include generated subprograms such as predicate
2140 -- functions, etc.
2142 if Is_Type (T) and then From_Nested_Package (T) then
2143 Push_Scope (Pack);
2144 Install_Visible_Declarations (Pack);
2145 Install_Private_Declarations (Pack);
2146 Insert_Actions (N, Freeze_Nodes);
2147 End_Package_Scope (Pack);
2149 else
2150 Insert_Actions (N, Freeze_Nodes);
2151 end if;
2152 end if;
2153 end Freeze_Before;
2155 -------------------
2156 -- Freeze_Entity --
2157 -------------------
2159 -- WARNING: This routine manages Ghost regions. Return statements must be
2160 -- replaced by gotos which jump to the end of the routine and restore the
2161 -- Ghost mode.
2163 function Freeze_Entity
2164 (E : Entity_Id;
2165 N : Node_Id;
2166 Do_Freeze_Profile : Boolean := True) return List_Id
2168 Loc : constant Source_Ptr := Sloc (N);
2170 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2171 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2172 -- Save the Ghost-related attributes to restore on exit
2174 Atype : Entity_Id;
2175 Comp : Entity_Id;
2176 F_Node : Node_Id;
2177 Formal : Entity_Id;
2178 Indx : Node_Id;
2180 Has_Default_Initialization : Boolean := False;
2181 -- This flag gets set to true for a variable with default initialization
2183 Result : List_Id := No_List;
2184 -- List of freezing actions, left at No_List if none
2186 Test_E : Entity_Id := E;
2187 -- This could use a comment ???
2189 procedure Add_To_Result (Fnod : Node_Id);
2190 -- Add freeze action Fnod to list Result
2192 function After_Last_Declaration return Boolean;
2193 -- If Loc is a freeze_entity that appears after the last declaration
2194 -- in the scope, inhibit error messages on late completion.
2196 procedure Check_Current_Instance (Comp_Decl : Node_Id);
2197 -- Check that an Access or Unchecked_Access attribute with a prefix
2198 -- which is the current instance type can only be applied when the type
2199 -- is limited.
2201 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id);
2202 -- Give a warning for pragma Convention with language C or C++ applied
2203 -- to a discriminated record type. This is suppressed for the unchecked
2204 -- union case, since the whole point in this case is interface C. We
2205 -- also do not generate this within instantiations, since we will have
2206 -- generated a message on the template.
2208 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
2209 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
2210 -- integer literal without an explicit corresponding size clause. The
2211 -- caller has checked that Utype is a modular integer type.
2213 procedure Freeze_Array_Type (Arr : Entity_Id);
2214 -- Freeze array type, including freezing index and component types
2216 procedure Freeze_Object_Declaration (E : Entity_Id);
2217 -- Perform checks and generate freeze node if needed for a constant or
2218 -- variable declared by an object declaration.
2220 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
2221 -- Create Freeze_Generic_Entity nodes for types declared in a generic
2222 -- package. Recurse on inner generic packages.
2224 function Freeze_Profile (E : Entity_Id) return Boolean;
2225 -- Freeze formals and return type of subprogram. If some type in the
2226 -- profile is incomplete and we are in an instance, freezing of the
2227 -- entity will take place elsewhere, and the function returns False.
2229 procedure Freeze_Record_Type (Rec : Entity_Id);
2230 -- Freeze record type, including freezing component types, and freezing
2231 -- primitive operations if this is a tagged type.
2233 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2234 -- Determine whether an arbitrary entity is subject to Boolean aspect
2235 -- Import and its value is specified as True.
2237 procedure Inherit_Freeze_Node
2238 (Fnod : Node_Id;
2239 Typ : Entity_Id);
2240 -- Set type Typ's freeze node to refer to Fnode. This routine ensures
2241 -- that any attributes attached to Typ's original node are preserved.
2243 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2244 -- If E is an entity for an imported subprogram with pre/post-conditions
2245 -- then this procedure will create a wrapper to ensure that proper run-
2246 -- time checking of the pre/postconditions. See body for details.
2248 -------------------
2249 -- Add_To_Result --
2250 -------------------
2252 procedure Add_To_Result (Fnod : Node_Id) is
2253 begin
2254 -- The Ghost mode of the enclosing context is ignored, while the
2255 -- entity being frozen is living. Insert the freezing action prior
2256 -- to the start of the enclosing ignored Ghost region. As a result
2257 -- the freezeing action will be preserved when the ignored Ghost
2258 -- context is eliminated.
2260 if Saved_GM = Ignore
2261 and then Ghost_Mode /= Ignore
2262 and then Present (Ignored_Ghost_Region)
2263 then
2264 Insert_Action (Ignored_Ghost_Region, Fnod);
2266 -- Otherwise add the freezing action to the result list
2268 else
2269 Append_New_To (Result, Fnod);
2270 end if;
2271 end Add_To_Result;
2273 ----------------------------
2274 -- After_Last_Declaration --
2275 ----------------------------
2277 function After_Last_Declaration return Boolean is
2278 Spec : constant Node_Id := Parent (Current_Scope);
2280 begin
2281 if Nkind (Spec) = N_Package_Specification then
2282 if Present (Private_Declarations (Spec)) then
2283 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2284 elsif Present (Visible_Declarations (Spec)) then
2285 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2286 else
2287 return False;
2288 end if;
2290 else
2291 return False;
2292 end if;
2293 end After_Last_Declaration;
2295 ----------------------------
2296 -- Check_Current_Instance --
2297 ----------------------------
2299 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2301 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2302 -- Determine whether Typ is compatible with the rules for aliased
2303 -- views of types as defined in RM 3.10 in the various dialects.
2305 function Process (N : Node_Id) return Traverse_Result;
2306 -- Process routine to apply check to given node
2308 -----------------------------
2309 -- Is_Aliased_View_Of_Type --
2310 -----------------------------
2312 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2313 Typ_Decl : constant Node_Id := Parent (Typ);
2315 begin
2316 -- Common case
2318 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2319 and then Limited_Present (Type_Definition (Typ_Decl))
2320 then
2321 return True;
2323 -- The following paragraphs describe what a legal aliased view of
2324 -- a type is in the various dialects of Ada.
2326 -- Ada 95
2328 -- The current instance of a limited type, and a formal parameter
2329 -- or generic formal object of a tagged type.
2331 -- Ada 95 limited type
2332 -- * Type with reserved word "limited"
2333 -- * A protected or task type
2334 -- * A composite type with limited component
2336 elsif Ada_Version <= Ada_95 then
2337 return Is_Limited_Type (Typ);
2339 -- Ada 2005
2341 -- The current instance of a limited tagged type, a protected
2342 -- type, a task type, or a type that has the reserved word
2343 -- "limited" in its full definition ... a formal parameter or
2344 -- generic formal object of a tagged type.
2346 -- Ada 2005 limited type
2347 -- * Type with reserved word "limited", "synchronized", "task"
2348 -- or "protected"
2349 -- * A composite type with limited component
2350 -- * A derived type whose parent is a non-interface limited type
2352 elsif Ada_Version = Ada_2005 then
2353 return
2354 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2355 or else
2356 (Is_Derived_Type (Typ)
2357 and then not Is_Interface (Etype (Typ))
2358 and then Is_Limited_Type (Etype (Typ)));
2360 -- Ada 2012 and beyond
2362 -- The current instance of an immutably limited type ... a formal
2363 -- parameter or generic formal object of a tagged type.
2365 -- Ada 2012 limited type
2366 -- * Type with reserved word "limited", "synchronized", "task"
2367 -- or "protected"
2368 -- * A composite type with limited component
2369 -- * A derived type whose parent is a non-interface limited type
2370 -- * An incomplete view
2372 -- Ada 2012 immutably limited type
2373 -- * Explicitly limited record type
2374 -- * Record extension with "limited" present
2375 -- * Non-formal limited private type that is either tagged
2376 -- or has at least one access discriminant with a default
2377 -- expression
2378 -- * Task type, protected type or synchronized interface
2379 -- * Type derived from immutably limited type
2381 else
2382 return
2383 Is_Immutably_Limited_Type (Typ)
2384 or else Is_Incomplete_Type (Typ);
2385 end if;
2386 end Is_Aliased_View_Of_Type;
2388 -------------
2389 -- Process --
2390 -------------
2392 function Process (N : Node_Id) return Traverse_Result is
2393 begin
2394 case Nkind (N) is
2395 when N_Attribute_Reference =>
2396 if Nam_In (Attribute_Name (N), Name_Access,
2397 Name_Unchecked_Access)
2398 and then Is_Entity_Name (Prefix (N))
2399 and then Is_Type (Entity (Prefix (N)))
2400 and then Entity (Prefix (N)) = E
2401 then
2402 if Ada_Version < Ada_2012 then
2403 Error_Msg_N
2404 ("current instance must be a limited type",
2405 Prefix (N));
2406 else
2407 Error_Msg_N
2408 ("current instance must be an immutably limited "
2409 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2410 end if;
2412 return Abandon;
2414 else
2415 return OK;
2416 end if;
2418 when others =>
2419 return OK;
2420 end case;
2421 end Process;
2423 procedure Traverse is new Traverse_Proc (Process);
2425 -- Local variables
2427 Rec_Type : constant Entity_Id :=
2428 Scope (Defining_Identifier (Comp_Decl));
2430 -- Start of processing for Check_Current_Instance
2432 begin
2433 if not Is_Aliased_View_Of_Type (Rec_Type) then
2434 Traverse (Comp_Decl);
2435 end if;
2436 end Check_Current_Instance;
2438 ---------------------------------
2439 -- Check_Suspicious_Convention --
2440 ---------------------------------
2442 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id) is
2443 begin
2444 if Has_Discriminants (Rec_Type)
2445 and then Is_Base_Type (Rec_Type)
2446 and then not Is_Unchecked_Union (Rec_Type)
2447 and then (Convention (Rec_Type) = Convention_C
2448 or else
2449 Convention (Rec_Type) = Convention_CPP)
2450 and then Comes_From_Source (Rec_Type)
2451 and then not In_Instance
2452 and then not Has_Warnings_Off (Rec_Type)
2453 then
2454 declare
2455 Cprag : constant Node_Id :=
2456 Get_Rep_Pragma (Rec_Type, Name_Convention);
2457 A2 : Node_Id;
2459 begin
2460 if Present (Cprag) then
2461 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
2463 if Convention (Rec_Type) = Convention_C then
2464 Error_Msg_N
2465 ("?x?discriminated record has no direct equivalent in "
2466 & "C", A2);
2467 else
2468 Error_Msg_N
2469 ("?x?discriminated record has no direct equivalent in "
2470 & "C++", A2);
2471 end if;
2473 Error_Msg_NE
2474 ("\?x?use of convention for type& is dubious",
2475 A2, Rec_Type);
2476 end if;
2477 end;
2478 end if;
2479 end Check_Suspicious_Convention;
2481 ------------------------------
2482 -- Check_Suspicious_Modulus --
2483 ------------------------------
2485 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
2486 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
2488 begin
2489 if not Warn_On_Suspicious_Modulus_Value then
2490 return;
2491 end if;
2493 if Nkind (Decl) = N_Full_Type_Declaration then
2494 declare
2495 Tdef : constant Node_Id := Type_Definition (Decl);
2497 begin
2498 if Nkind (Tdef) = N_Modular_Type_Definition then
2499 declare
2500 Modulus : constant Node_Id :=
2501 Original_Node (Expression (Tdef));
2503 begin
2504 if Nkind (Modulus) = N_Integer_Literal then
2505 declare
2506 Modv : constant Uint := Intval (Modulus);
2507 Sizv : constant Uint := RM_Size (Utype);
2509 begin
2510 -- First case, modulus and size are the same. This
2511 -- happens if you have something like mod 32, with
2512 -- an explicit size of 32, this is for sure a case
2513 -- where the warning is given, since it is seems
2514 -- very unlikely that someone would want e.g. a
2515 -- five bit type stored in 32 bits. It is much
2516 -- more likely they wanted a 32-bit type.
2518 if Modv = Sizv then
2519 null;
2521 -- Second case, the modulus is 32 or 64 and no
2522 -- size clause is present. This is a less clear
2523 -- case for giving the warning, but in the case
2524 -- of 32/64 (5-bit or 6-bit types) these seem rare
2525 -- enough that it is a likely error (and in any
2526 -- case using 2**5 or 2**6 in these cases seems
2527 -- clearer. We don't include 8 or 16 here, simply
2528 -- because in practice 3-bit and 4-bit types are
2529 -- more common and too many false positives if
2530 -- we warn in these cases.
2532 elsif not Has_Size_Clause (Utype)
2533 and then (Modv = Uint_32 or else Modv = Uint_64)
2534 then
2535 null;
2537 -- No warning needed
2539 else
2540 return;
2541 end if;
2543 -- If we fall through, give warning
2545 Error_Msg_Uint_1 := Modv;
2546 Error_Msg_N
2547 ("?M?2 '*'*^' may have been intended here",
2548 Modulus);
2549 end;
2550 end if;
2551 end;
2552 end if;
2553 end;
2554 end if;
2555 end Check_Suspicious_Modulus;
2557 -----------------------
2558 -- Freeze_Array_Type --
2559 -----------------------
2561 procedure Freeze_Array_Type (Arr : Entity_Id) is
2562 FS : constant Entity_Id := First_Subtype (Arr);
2563 Ctyp : constant Entity_Id := Component_Type (Arr);
2564 Clause : Entity_Id;
2566 Non_Standard_Enum : Boolean := False;
2567 -- Set true if any of the index types is an enumeration type with a
2568 -- non-standard representation.
2570 begin
2571 Freeze_And_Append (Ctyp, N, Result);
2573 Indx := First_Index (Arr);
2574 while Present (Indx) loop
2575 Freeze_And_Append (Etype (Indx), N, Result);
2577 if Is_Enumeration_Type (Etype (Indx))
2578 and then Has_Non_Standard_Rep (Etype (Indx))
2579 then
2580 Non_Standard_Enum := True;
2581 end if;
2583 Next_Index (Indx);
2584 end loop;
2586 -- Processing that is done only for base types
2588 if Ekind (Arr) = E_Array_Type then
2590 -- Deal with default setting of reverse storage order
2592 Set_SSO_From_Default (Arr);
2594 -- Propagate flags for component type
2596 if Is_Controlled (Component_Type (Arr))
2597 or else Has_Controlled_Component (Ctyp)
2598 then
2599 Set_Has_Controlled_Component (Arr);
2600 end if;
2602 if Has_Unchecked_Union (Component_Type (Arr)) then
2603 Set_Has_Unchecked_Union (Arr);
2604 end if;
2606 -- The array type requires its own invariant procedure in order to
2607 -- verify the component invariant over all elements. In GNATprove
2608 -- mode, the component invariants are checked by other means. They
2609 -- should not be added to the array type invariant procedure, so
2610 -- that the procedure can be used to check the array type
2611 -- invariants if any.
2613 if Has_Invariants (Component_Type (Arr))
2614 and then not GNATprove_Mode
2615 then
2616 Set_Has_Own_Invariants (Arr);
2618 -- The array type is an implementation base type. Propagate the
2619 -- same property to the first subtype.
2621 if Is_Itype (Arr) then
2622 Set_Has_Own_Invariants (First_Subtype (Arr));
2623 end if;
2624 end if;
2626 -- Warn for pragma Pack overriding foreign convention
2628 if Has_Foreign_Convention (Ctyp)
2629 and then Has_Pragma_Pack (Arr)
2630 then
2631 declare
2632 CN : constant Name_Id :=
2633 Get_Convention_Name (Convention (Ctyp));
2634 PP : constant Node_Id :=
2635 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
2636 begin
2637 if Present (PP) then
2638 Error_Msg_Name_1 := CN;
2639 Error_Msg_Sloc := Sloc (Arr);
2640 Error_Msg_N
2641 ("pragma Pack affects convention % components #??", PP);
2642 Error_Msg_Name_1 := CN;
2643 Error_Msg_N
2644 ("\array components may not have % compatible "
2645 & "representation??", PP);
2646 end if;
2647 end;
2648 end if;
2650 -- If packing was requested or if the component size was
2651 -- set explicitly, then see if bit packing is required. This
2652 -- processing is only done for base types, since all of the
2653 -- representation aspects involved are type-related.
2655 -- This is not just an optimization, if we start processing the
2656 -- subtypes, they interfere with the settings on the base type
2657 -- (this is because Is_Packed has a slightly different meaning
2658 -- before and after freezing).
2660 declare
2661 Csiz : Uint;
2662 Esiz : Uint;
2664 begin
2665 if (Is_Packed (Arr) or else Has_Pragma_Pack (Arr))
2666 and then Known_Static_RM_Size (Ctyp)
2667 and then not Has_Component_Size_Clause (Arr)
2668 then
2669 Csiz := UI_Max (RM_Size (Ctyp), 1);
2671 elsif Known_Component_Size (Arr) then
2672 Csiz := Component_Size (Arr);
2674 elsif not Known_Static_Esize (Ctyp) then
2675 Csiz := Uint_0;
2677 else
2678 Esiz := Esize (Ctyp);
2680 -- We can set the component size if it is less than 16,
2681 -- rounding it up to the next storage unit size.
2683 if Esiz <= 8 then
2684 Csiz := Uint_8;
2685 elsif Esiz <= 16 then
2686 Csiz := Uint_16;
2687 else
2688 Csiz := Uint_0;
2689 end if;
2691 -- Set component size up to match alignment if it would
2692 -- otherwise be less than the alignment. This deals with
2693 -- cases of types whose alignment exceeds their size (the
2694 -- padded type cases).
2696 if Csiz /= 0 then
2697 declare
2698 A : constant Uint := Alignment_In_Bits (Ctyp);
2699 begin
2700 if Csiz < A then
2701 Csiz := A;
2702 end if;
2703 end;
2704 end if;
2705 end if;
2707 -- Case of component size that may result in bit packing
2709 if 1 <= Csiz and then Csiz <= 64 then
2710 declare
2711 Ent : constant Entity_Id :=
2712 First_Subtype (Arr);
2713 Pack_Pragma : constant Node_Id :=
2714 Get_Rep_Pragma (Ent, Name_Pack);
2715 Comp_Size_C : constant Node_Id :=
2716 Get_Attribute_Definition_Clause
2717 (Ent, Attribute_Component_Size);
2719 begin
2720 -- Warn if we have pack and component size so that the
2721 -- pack is ignored.
2723 -- Note: here we must check for the presence of a
2724 -- component size before checking for a Pack pragma to
2725 -- deal with the case where the array type is a derived
2726 -- type whose parent is currently private.
2728 if Present (Comp_Size_C)
2729 and then Has_Pragma_Pack (Ent)
2730 and then Warn_On_Redundant_Constructs
2731 then
2732 Error_Msg_Sloc := Sloc (Comp_Size_C);
2733 Error_Msg_NE
2734 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
2735 Error_Msg_N
2736 ("\?r?explicit component size given#!", Pack_Pragma);
2737 Set_Is_Packed (Base_Type (Ent), False);
2738 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
2739 end if;
2741 -- Set component size if not already set by a component
2742 -- size clause.
2744 if not Present (Comp_Size_C) then
2745 Set_Component_Size (Arr, Csiz);
2746 end if;
2748 -- Check for base type of 8, 16, 32 bits, where an
2749 -- unsigned subtype has a length one less than the
2750 -- base type (e.g. Natural subtype of Integer).
2752 -- In such cases, if a component size was not set
2753 -- explicitly, then generate a warning.
2755 if Has_Pragma_Pack (Arr)
2756 and then not Present (Comp_Size_C)
2757 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2758 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2759 then
2760 Error_Msg_Uint_1 := Csiz;
2762 if Present (Pack_Pragma) then
2763 Error_Msg_N
2764 ("??pragma Pack causes component size to be ^!",
2765 Pack_Pragma);
2766 Error_Msg_N
2767 ("\??use Component_Size to set desired value!",
2768 Pack_Pragma);
2769 end if;
2770 end if;
2772 -- Bit packing is never needed for 8, 16, 32, 64
2774 if Addressable (Csiz) then
2776 -- If the Esize of the component is known and equal to
2777 -- the component size then even packing is not needed.
2779 if Known_Static_Esize (Component_Type (Arr))
2780 and then Esize (Component_Type (Arr)) = Csiz
2781 then
2782 -- Here the array was requested to be packed, but
2783 -- the packing request had no effect whatsoever,
2784 -- so flag Is_Packed is reset.
2786 -- Note: semantically this means that we lose track
2787 -- of the fact that a derived type inherited pragma
2788 -- Pack that was non-effective, but that is fine.
2790 -- We regard a Pack pragma as a request to set a
2791 -- representation characteristic, and this request
2792 -- may be ignored.
2794 Set_Is_Packed (Base_Type (Arr), False);
2795 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
2796 else
2797 Set_Is_Packed (Base_Type (Arr), True);
2798 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2799 end if;
2801 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2803 -- Bit packing is not needed for multiples of the storage
2804 -- unit if the type is composite because the back end can
2805 -- byte pack composite types.
2807 elsif Csiz mod System_Storage_Unit = 0
2808 and then Is_Composite_Type (Ctyp)
2809 then
2810 Set_Is_Packed (Base_Type (Arr), True);
2811 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2812 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2814 -- In all other cases, bit packing is needed
2816 else
2817 Set_Is_Packed (Base_Type (Arr), True);
2818 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2819 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
2820 end if;
2821 end;
2822 end if;
2823 end;
2825 -- Check for Aliased or Atomic_Components/Atomic/VFA with
2826 -- unsuitable packing or explicit component size clause given.
2828 if (Has_Aliased_Components (Arr)
2829 or else Has_Atomic_Components (Arr)
2830 or else Is_Atomic_Or_VFA (Ctyp))
2831 and then
2832 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2833 then
2834 Alias_Atomic_Check : declare
2836 procedure Complain_CS (T : String);
2837 -- Outputs error messages for incorrect CS clause or pragma
2838 -- Pack for aliased or atomic/VFA components (T is "aliased"
2839 -- or "atomic/vfa");
2841 -----------------
2842 -- Complain_CS --
2843 -----------------
2845 procedure Complain_CS (T : String) is
2846 begin
2847 if Has_Component_Size_Clause (Arr) then
2848 Clause :=
2849 Get_Attribute_Definition_Clause
2850 (FS, Attribute_Component_Size);
2852 Error_Msg_N
2853 ("incorrect component size for "
2854 & T & " components", Clause);
2855 Error_Msg_Uint_1 := Esize (Ctyp);
2856 Error_Msg_N
2857 ("\only allowed value is^", Clause);
2859 else
2860 Error_Msg_N
2861 ("cannot pack " & T & " components",
2862 Get_Rep_Pragma (FS, Name_Pack));
2863 end if;
2864 end Complain_CS;
2866 -- Start of processing for Alias_Atomic_Check
2868 begin
2869 -- If object size of component type isn't known, we cannot
2870 -- be sure so we defer to the back end.
2872 if not Known_Static_Esize (Ctyp) then
2873 null;
2875 -- Case where component size has no effect. First check for
2876 -- object size of component type multiple of the storage
2877 -- unit size.
2879 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2881 -- OK in both packing case and component size case if RM
2882 -- size is known and static and same as the object size.
2884 and then
2885 ((Known_Static_RM_Size (Ctyp)
2886 and then Esize (Ctyp) = RM_Size (Ctyp))
2888 -- Or if we have an explicit component size clause and
2889 -- the component size and object size are equal.
2891 or else
2892 (Has_Component_Size_Clause (Arr)
2893 and then Component_Size (Arr) = Esize (Ctyp)))
2894 then
2895 null;
2897 elsif Has_Aliased_Components (Arr) then
2898 Complain_CS ("aliased");
2900 elsif Has_Atomic_Components (Arr)
2901 or else Is_Atomic (Ctyp)
2902 then
2903 Complain_CS ("atomic");
2905 elsif Is_Volatile_Full_Access (Ctyp) then
2906 Complain_CS ("volatile full access");
2907 end if;
2908 end Alias_Atomic_Check;
2909 end if;
2911 -- Check for Independent_Components/Independent with unsuitable
2912 -- packing or explicit component size clause given.
2914 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
2915 and then
2916 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2917 then
2918 begin
2919 -- If object size of component type isn't known, we cannot
2920 -- be sure so we defer to the back end.
2922 if not Known_Static_Esize (Ctyp) then
2923 null;
2925 -- Case where component size has no effect. First check for
2926 -- object size of component type multiple of the storage
2927 -- unit size.
2929 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2931 -- OK in both packing case and component size case if RM
2932 -- size is known and multiple of the storage unit size.
2934 and then
2935 ((Known_Static_RM_Size (Ctyp)
2936 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
2938 -- Or if we have an explicit component size clause and
2939 -- the component size is larger than the object size.
2941 or else
2942 (Has_Component_Size_Clause (Arr)
2943 and then Component_Size (Arr) >= Esize (Ctyp)))
2944 then
2945 null;
2947 else
2948 if Has_Component_Size_Clause (Arr) then
2949 Clause :=
2950 Get_Attribute_Definition_Clause
2951 (FS, Attribute_Component_Size);
2953 Error_Msg_N
2954 ("incorrect component size for "
2955 & "independent components", Clause);
2956 Error_Msg_Uint_1 := Esize (Ctyp);
2957 Error_Msg_N
2958 ("\minimum allowed is^", Clause);
2960 else
2961 Error_Msg_N
2962 ("cannot pack independent components",
2963 Get_Rep_Pragma (FS, Name_Pack));
2964 end if;
2965 end if;
2966 end;
2967 end if;
2969 -- Warn for case of atomic type
2971 Clause := Get_Rep_Pragma (FS, Name_Atomic);
2973 if Present (Clause)
2974 and then not Addressable (Component_Size (FS))
2975 then
2976 Error_Msg_NE
2977 ("non-atomic components of type& may not be "
2978 & "accessible by separate tasks??", Clause, Arr);
2980 if Has_Component_Size_Clause (Arr) then
2981 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2982 (FS, Attribute_Component_Size));
2983 Error_Msg_N ("\because of component size clause#??", Clause);
2985 elsif Has_Pragma_Pack (Arr) then
2986 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2987 Error_Msg_N ("\because of pragma Pack#??", Clause);
2988 end if;
2989 end if;
2991 -- Check for scalar storage order
2993 declare
2994 Dummy : Boolean;
2995 begin
2996 Check_Component_Storage_Order
2997 (Encl_Type => Arr,
2998 Comp => Empty,
2999 ADC => Get_Attribute_Definition_Clause
3000 (First_Subtype (Arr),
3001 Attribute_Scalar_Storage_Order),
3002 Comp_ADC_Present => Dummy);
3003 end;
3005 -- Processing that is done only for subtypes
3007 else
3008 -- Acquire alignment from base type
3010 if Unknown_Alignment (Arr) then
3011 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
3012 Adjust_Esize_Alignment (Arr);
3013 end if;
3014 end if;
3016 -- Specific checks for bit-packed arrays
3018 if Is_Bit_Packed_Array (Arr) then
3020 -- Check number of elements for bit-packed arrays that come from
3021 -- source and have compile time known ranges. The bit-packed
3022 -- arrays circuitry does not support arrays with more than
3023 -- Integer'Last + 1 elements, and when this restriction is
3024 -- violated, causes incorrect data access.
3026 -- For the case where this is not compile time known, a run-time
3027 -- check should be generated???
3029 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
3030 declare
3031 Elmts : Uint;
3032 Index : Node_Id;
3033 Ilen : Node_Id;
3034 Ityp : Entity_Id;
3036 begin
3037 Elmts := Uint_1;
3038 Index := First_Index (Arr);
3039 while Present (Index) loop
3040 Ityp := Etype (Index);
3042 -- Never generate an error if any index is of a generic
3043 -- type. We will check this in instances.
3045 if Is_Generic_Type (Ityp) then
3046 Elmts := Uint_0;
3047 exit;
3048 end if;
3050 Ilen :=
3051 Make_Attribute_Reference (Loc,
3052 Prefix => New_Occurrence_Of (Ityp, Loc),
3053 Attribute_Name => Name_Range_Length);
3054 Analyze_And_Resolve (Ilen);
3056 -- No attempt is made to check number of elements if not
3057 -- compile time known.
3059 if Nkind (Ilen) /= N_Integer_Literal then
3060 Elmts := Uint_0;
3061 exit;
3062 end if;
3064 Elmts := Elmts * Intval (Ilen);
3065 Next_Index (Index);
3066 end loop;
3068 if Elmts > Intval (High_Bound
3069 (Scalar_Range (Standard_Integer))) + 1
3070 then
3071 Error_Msg_N
3072 ("bit packed array type may not have "
3073 & "more than Integer''Last+1 elements", Arr);
3074 end if;
3075 end;
3076 end if;
3078 -- Check size
3080 if Known_RM_Size (Arr) then
3081 declare
3082 SizC : constant Node_Id := Size_Clause (Arr);
3083 Discard : Boolean;
3085 begin
3086 -- It is not clear if it is possible to have no size clause
3087 -- at this stage, but it is not worth worrying about. Post
3088 -- error on the entity name in the size clause if present,
3089 -- else on the type entity itself.
3091 if Present (SizC) then
3092 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
3093 else
3094 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
3095 end if;
3096 end;
3097 end if;
3098 end if;
3100 -- If any of the index types was an enumeration type with a non-
3101 -- standard rep clause, then we indicate that the array type is
3102 -- always packed (even if it is not bit-packed).
3104 if Non_Standard_Enum then
3105 Set_Has_Non_Standard_Rep (Base_Type (Arr));
3106 Set_Is_Packed (Base_Type (Arr));
3107 end if;
3109 Set_Component_Alignment_If_Not_Set (Arr);
3111 -- If the array is packed and bit-packed or packed to eliminate holes
3112 -- in the non-contiguous enumeration index types, we must create the
3113 -- packed array type to be used to actually implement the type. This
3114 -- is only needed for real array types (not for string literal types,
3115 -- since they are present only for the front end).
3117 if Is_Packed (Arr)
3118 and then (Is_Bit_Packed_Array (Arr) or else Non_Standard_Enum)
3119 and then Ekind (Arr) /= E_String_Literal_Subtype
3120 then
3121 Create_Packed_Array_Impl_Type (Arr);
3122 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
3124 -- Make sure that we have the necessary routines to implement the
3125 -- packing, and complain now if not. Note that we only test this
3126 -- for constrained array types.
3128 if Is_Constrained (Arr)
3129 and then Is_Bit_Packed_Array (Arr)
3130 and then Present (Packed_Array_Impl_Type (Arr))
3131 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
3132 then
3133 declare
3134 CS : constant Uint := Component_Size (Arr);
3135 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
3137 begin
3138 if RE /= RE_Null
3139 and then not RTE_Available (RE)
3140 then
3141 Error_Msg_CRT
3142 ("packing of " & UI_Image (CS) & "-bit components",
3143 First_Subtype (Etype (Arr)));
3145 -- Cancel the packing
3147 Set_Is_Packed (Base_Type (Arr), False);
3148 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3149 Set_Packed_Array_Impl_Type (Arr, Empty);
3150 goto Skip_Packed;
3151 end if;
3152 end;
3153 end if;
3155 -- Size information of packed array type is copied to the array
3156 -- type, since this is really the representation. But do not
3157 -- override explicit existing size values. If the ancestor subtype
3158 -- is constrained the Packed_Array_Impl_Type will be inherited
3159 -- from it, but the size may have been provided already, and
3160 -- must not be overridden either.
3162 if not Has_Size_Clause (Arr)
3163 and then
3164 (No (Ancestor_Subtype (Arr))
3165 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
3166 then
3167 Set_Esize (Arr, Esize (Packed_Array_Impl_Type (Arr)));
3168 Set_RM_Size (Arr, RM_Size (Packed_Array_Impl_Type (Arr)));
3169 end if;
3171 if not Has_Alignment_Clause (Arr) then
3172 Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
3173 end if;
3174 end if;
3176 <<Skip_Packed>>
3178 -- For non-packed arrays set the alignment of the array to the
3179 -- alignment of the component type if it is unknown. Skip this
3180 -- in atomic/VFA case (atomic/VFA arrays may need larger alignments).
3182 if not Is_Packed (Arr)
3183 and then Unknown_Alignment (Arr)
3184 and then Known_Alignment (Ctyp)
3185 and then Known_Static_Component_Size (Arr)
3186 and then Known_Static_Esize (Ctyp)
3187 and then Esize (Ctyp) = Component_Size (Arr)
3188 and then not Is_Atomic_Or_VFA (Arr)
3189 then
3190 Set_Alignment (Arr, Alignment (Component_Type (Arr)));
3191 end if;
3193 -- A Ghost type cannot have a component of protected or task type
3194 -- (SPARK RM 6.9(19)).
3196 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
3197 Error_Msg_N
3198 ("ghost array type & cannot have concurrent component type",
3199 Arr);
3200 end if;
3201 end Freeze_Array_Type;
3203 -------------------------------
3204 -- Freeze_Object_Declaration --
3205 -------------------------------
3207 procedure Freeze_Object_Declaration (E : Entity_Id) is
3208 procedure Check_Large_Modular_Array (Typ : Entity_Id);
3209 -- Check that the size of array type Typ can be computed without
3210 -- overflow, and generates a Storage_Error otherwise. This is only
3211 -- relevant for array types whose index is a (mod 2**64) type, where
3212 -- wrap-around arithmetic might yield a meaningless value for the
3213 -- length of the array, or its corresponding attribute.
3215 -------------------------------
3216 -- Check_Large_Modular_Array --
3217 -------------------------------
3219 procedure Check_Large_Modular_Array (Typ : Entity_Id) is
3220 Obj_Loc : constant Source_Ptr := Sloc (E);
3221 Idx_Typ : Entity_Id;
3223 begin
3224 -- Nothing to do when expansion is disabled because this routine
3225 -- generates a runtime check.
3227 if not Expander_Active then
3228 return;
3230 -- Nothing to do for String literal subtypes because their index
3231 -- cannot be a modular type.
3233 elsif Ekind (Typ) = E_String_Literal_Subtype then
3234 return;
3236 -- Nothing to do for an imported object because the object will
3237 -- be created on the exporting side.
3239 elsif Is_Imported (E) then
3240 return;
3242 -- Nothing to do for unconstrained array types. This case arises
3243 -- when the object declaration is illegal.
3245 elsif not Is_Constrained (Typ) then
3246 return;
3247 end if;
3249 Idx_Typ := Etype (First_Index (Typ));
3251 -- To prevent arithmetic overflow with large values, we raise
3252 -- Storage_Error under the following guard:
3254 -- (Arr'Last / 2 - Arr'First / 2) > (2 ** 30)
3256 -- This takes care of the boundary case, but it is preferable to
3257 -- use a smaller limit, because even on 64-bit architectures an
3258 -- array of more than 2 ** 30 bytes is likely to raise
3259 -- Storage_Error.
3261 if Is_Modular_Integer_Type (Idx_Typ)
3262 and then RM_Size (Idx_Typ) = RM_Size (Standard_Long_Long_Integer)
3263 then
3264 Insert_Action (Declaration_Node (E),
3265 Make_Raise_Storage_Error (Obj_Loc,
3266 Condition =>
3267 Make_Op_Ge (Obj_Loc,
3268 Left_Opnd =>
3269 Make_Op_Subtract (Obj_Loc,
3270 Left_Opnd =>
3271 Make_Op_Divide (Obj_Loc,
3272 Left_Opnd =>
3273 Make_Attribute_Reference (Obj_Loc,
3274 Prefix =>
3275 New_Occurrence_Of (Typ, Obj_Loc),
3276 Attribute_Name => Name_Last),
3277 Right_Opnd =>
3278 Make_Integer_Literal (Obj_Loc, Uint_2)),
3279 Right_Opnd =>
3280 Make_Op_Divide (Obj_Loc,
3281 Left_Opnd =>
3282 Make_Attribute_Reference (Obj_Loc,
3283 Prefix =>
3284 New_Occurrence_Of (Typ, Obj_Loc),
3285 Attribute_Name => Name_First),
3286 Right_Opnd =>
3287 Make_Integer_Literal (Obj_Loc, Uint_2))),
3288 Right_Opnd =>
3289 Make_Integer_Literal (Obj_Loc, (Uint_2 ** 30))),
3290 Reason => SE_Object_Too_Large));
3291 end if;
3292 end Check_Large_Modular_Array;
3294 -- Local variables
3296 Typ : constant Entity_Id := Etype (E);
3297 Def : Node_Id;
3299 -- Start of processing for Freeze_Object_Declaration
3301 begin
3302 -- Abstract type allowed only for C++ imported variables or constants
3304 -- Note: we inhibit this check for objects that do not come from
3305 -- source because there is at least one case (the expansion of
3306 -- x'Class'Input where x is abstract) where we legitimately
3307 -- generate an abstract object.
3309 if Is_Abstract_Type (Typ)
3310 and then Comes_From_Source (Parent (E))
3311 and then not (Is_Imported (E) and then Is_CPP_Class (Typ))
3312 then
3313 Def := Object_Definition (Parent (E));
3315 Error_Msg_N ("type of object cannot be abstract", Def);
3317 if Is_CPP_Class (Etype (E)) then
3318 Error_Msg_NE ("\} may need a cpp_constructor", Def, Typ);
3320 elsif Present (Expression (Parent (E))) then
3321 Error_Msg_N -- CODEFIX
3322 ("\maybe a class-wide type was meant", Def);
3323 end if;
3324 end if;
3326 -- For object created by object declaration, perform required
3327 -- categorization (preelaborate and pure) checks. Defer these
3328 -- checks to freeze time since pragma Import inhibits default
3329 -- initialization and thus pragma Import affects these checks.
3331 Validate_Object_Declaration (Declaration_Node (E));
3333 -- If there is an address clause, check that it is valid and if need
3334 -- be move initialization to the freeze node.
3336 Check_Address_Clause (E);
3338 -- Similar processing is needed for aspects that may affect object
3339 -- layout, like Alignment, if there is an initialization expression.
3340 -- We don't do this if there is a pragma Linker_Section, because it
3341 -- would prevent the back end from statically initializing the
3342 -- object; we don't want elaboration code in that case.
3344 if Has_Delayed_Aspects (E)
3345 and then Expander_Active
3346 and then Is_Array_Type (Typ)
3347 and then Present (Expression (Parent (E)))
3348 and then No (Linker_Section_Pragma (E))
3349 then
3350 declare
3351 Decl : constant Node_Id := Parent (E);
3352 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
3354 begin
3355 -- Capture initialization value at point of declaration, and
3356 -- make explicit assignment legal, because object may be a
3357 -- constant.
3359 Remove_Side_Effects (Expression (Decl));
3360 Set_Assignment_OK (Lhs);
3362 -- Move initialization to freeze actions
3364 Append_Freeze_Action (E,
3365 Make_Assignment_Statement (Loc,
3366 Name => Lhs,
3367 Expression => Expression (Decl)));
3369 Set_No_Initialization (Decl);
3370 -- Set_Is_Frozen (E, False);
3371 end;
3372 end if;
3374 -- Reset Is_True_Constant for non-constant aliased object. We
3375 -- consider that the fact that a non-constant object is aliased may
3376 -- indicate that some funny business is going on, e.g. an aliased
3377 -- object is passed by reference to a procedure which captures the
3378 -- address of the object, which is later used to assign a new value,
3379 -- even though the compiler thinks that it is not modified. Such
3380 -- code is highly dubious, but we choose to make it "work" for
3381 -- non-constant aliased objects.
3383 -- Note that we used to do this for all aliased objects, whether or
3384 -- not constant, but this caused anomalies down the line because we
3385 -- ended up with static objects that were not Is_True_Constant. Not
3386 -- resetting Is_True_Constant for (aliased) constant objects ensures
3387 -- that this anomaly never occurs.
3389 -- However, we don't do that for internal entities. We figure that if
3390 -- we deliberately set Is_True_Constant for an internal entity, e.g.
3391 -- a dispatch table entry, then we mean it.
3393 if Ekind (E) /= E_Constant
3394 and then (Is_Aliased (E) or else Is_Aliased (Typ))
3395 and then not Is_Internal_Name (Chars (E))
3396 then
3397 Set_Is_True_Constant (E, False);
3398 end if;
3400 -- If the object needs any kind of default initialization, an error
3401 -- must be issued if No_Default_Initialization applies. The check
3402 -- doesn't apply to imported objects, which are not ever default
3403 -- initialized, and is why the check is deferred until freezing, at
3404 -- which point we know if Import applies. Deferred constants are also
3405 -- exempted from this test because their completion is explicit, or
3406 -- through an import pragma.
3408 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
3409 null;
3411 elsif Comes_From_Source (E)
3412 and then not Is_Imported (E)
3413 and then not Has_Init_Expression (Declaration_Node (E))
3414 and then
3415 ((Has_Non_Null_Base_Init_Proc (Typ)
3416 and then not No_Initialization (Declaration_Node (E))
3417 and then not Initialization_Suppressed (Typ))
3418 or else
3419 (Needs_Simple_Initialization (Typ)
3420 and then not Is_Internal (E)))
3421 then
3422 Has_Default_Initialization := True;
3423 Check_Restriction
3424 (No_Default_Initialization, Declaration_Node (E));
3425 end if;
3427 -- Check that a Thread_Local_Storage variable does not have default
3428 -- initialization, and any explicit initialization must either be the
3429 -- null constant or a static constant.
3431 if Has_Pragma_Thread_Local_Storage (E) then
3432 declare
3433 Decl : constant Node_Id := Declaration_Node (E);
3434 begin
3435 if Has_Default_Initialization
3436 or else
3437 (Has_Init_Expression (Decl)
3438 and then
3439 (No (Expression (Decl))
3440 or else not
3441 (Is_OK_Static_Expression (Expression (Decl))
3442 or else Nkind (Expression (Decl)) = N_Null)))
3443 then
3444 if Nkind (Expression (Decl)) = N_Aggregate
3445 and then Compile_Time_Known_Aggregate (Expression (Decl))
3446 then
3447 null;
3448 else
3449 Error_Msg_NE
3450 ("Thread_Local_Storage variable& is improperly "
3451 & "initialized", Decl, E);
3452 Error_Msg_NE
3453 ("\only allowed initialization is explicit NULL, "
3454 & "static expression or static aggregate", Decl, E);
3455 end if;
3456 end if;
3457 end;
3458 end if;
3460 -- For imported objects, set Is_Public unless there is also an
3461 -- address clause, which means that there is no external symbol
3462 -- needed for the Import (Is_Public may still be set for other
3463 -- unrelated reasons). Note that we delayed this processing
3464 -- till freeze time so that we can be sure not to set the flag
3465 -- if there is an address clause. If there is such a clause,
3466 -- then the only purpose of the Import pragma is to suppress
3467 -- implicit initialization.
3469 if Is_Imported (E) and then No (Address_Clause (E)) then
3470 Set_Is_Public (E);
3471 end if;
3473 -- For source objects that are not Imported and are library level, if
3474 -- no linker section pragma was given inherit the appropriate linker
3475 -- section from the corresponding type.
3477 if Comes_From_Source (E)
3478 and then not Is_Imported (E)
3479 and then Is_Library_Level_Entity (E)
3480 and then No (Linker_Section_Pragma (E))
3481 then
3482 Set_Linker_Section_Pragma (E, Linker_Section_Pragma (Typ));
3483 end if;
3485 -- For convention C objects of an enumeration type, warn if the size
3486 -- is not integer size and no explicit size given. Skip warning for
3487 -- Boolean and Character, and assume programmer expects 8-bit sizes
3488 -- for these cases.
3490 if (Convention (E) = Convention_C
3491 or else
3492 Convention (E) = Convention_CPP)
3493 and then Is_Enumeration_Type (Typ)
3494 and then not Is_Character_Type (Typ)
3495 and then not Is_Boolean_Type (Typ)
3496 and then Esize (Typ) < Standard_Integer_Size
3497 and then not Has_Size_Clause (E)
3498 then
3499 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
3500 Error_Msg_N
3501 ("??convention C enumeration object has size less than ^", E);
3502 Error_Msg_N ("\??use explicit size clause to set size", E);
3503 end if;
3505 if Is_Array_Type (Typ) then
3506 Check_Large_Modular_Array (Typ);
3507 end if;
3508 end Freeze_Object_Declaration;
3510 -----------------------------
3511 -- Freeze_Generic_Entities --
3512 -----------------------------
3514 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
3515 E : Entity_Id;
3516 F : Node_Id;
3517 Flist : List_Id;
3519 begin
3520 Flist := New_List;
3521 E := First_Entity (Pack);
3522 while Present (E) loop
3523 if Is_Type (E) and then not Is_Generic_Type (E) then
3524 F := Make_Freeze_Generic_Entity (Sloc (Pack));
3525 Set_Entity (F, E);
3526 Append_To (Flist, F);
3528 elsif Ekind (E) = E_Generic_Package then
3529 Append_List_To (Flist, Freeze_Generic_Entities (E));
3530 end if;
3532 Next_Entity (E);
3533 end loop;
3535 return Flist;
3536 end Freeze_Generic_Entities;
3538 --------------------
3539 -- Freeze_Profile --
3540 --------------------
3542 function Freeze_Profile (E : Entity_Id) return Boolean is
3543 F_Type : Entity_Id;
3544 R_Type : Entity_Id;
3545 Warn_Node : Node_Id;
3547 begin
3548 -- Loop through formals
3550 Formal := First_Formal (E);
3551 while Present (Formal) loop
3552 F_Type := Etype (Formal);
3554 -- AI05-0151: incomplete types can appear in a profile. By the
3555 -- time the entity is frozen, the full view must be available,
3556 -- unless it is a limited view.
3558 if Is_Incomplete_Type (F_Type)
3559 and then Present (Full_View (F_Type))
3560 and then not From_Limited_With (F_Type)
3561 then
3562 F_Type := Full_View (F_Type);
3563 Set_Etype (Formal, F_Type);
3564 end if;
3566 if not From_Limited_With (F_Type) then
3567 Freeze_And_Append (F_Type, N, Result);
3568 end if;
3570 if Is_Private_Type (F_Type)
3571 and then Is_Private_Type (Base_Type (F_Type))
3572 and then No (Full_View (Base_Type (F_Type)))
3573 and then not Is_Generic_Type (F_Type)
3574 and then not Is_Derived_Type (F_Type)
3575 then
3576 -- If the type of a formal is incomplete, subprogram is being
3577 -- frozen prematurely. Within an instance (but not within a
3578 -- wrapper package) this is an artifact of our need to regard
3579 -- the end of an instantiation as a freeze point. Otherwise it
3580 -- is a definite error.
3582 if In_Instance then
3583 Set_Is_Frozen (E, False);
3584 Result := No_List;
3585 return False;
3587 elsif not After_Last_Declaration
3588 and then not Freezing_Library_Level_Tagged_Type
3589 then
3590 Error_Msg_Node_1 := F_Type;
3591 Error_Msg
3592 ("type & must be fully defined before this point", Loc);
3593 end if;
3594 end if;
3596 -- Check suspicious parameter for C function. These tests apply
3597 -- only to exported/imported subprograms.
3599 if Warn_On_Export_Import
3600 and then Comes_From_Source (E)
3601 and then (Convention (E) = Convention_C
3602 or else
3603 Convention (E) = Convention_CPP)
3604 and then (Is_Imported (E) or else Is_Exported (E))
3605 and then Convention (E) /= Convention (Formal)
3606 and then not Has_Warnings_Off (E)
3607 and then not Has_Warnings_Off (F_Type)
3608 and then not Has_Warnings_Off (Formal)
3609 then
3610 -- Qualify mention of formals with subprogram name
3612 Error_Msg_Qual_Level := 1;
3614 -- Check suspicious use of fat C pointer
3616 if Is_Access_Type (F_Type)
3617 and then Esize (F_Type) > Ttypes.System_Address_Size
3618 then
3619 Error_Msg_N
3620 ("?x?type of & does not correspond to C pointer!", Formal);
3622 -- Check suspicious return of boolean
3624 elsif Root_Type (F_Type) = Standard_Boolean
3625 and then Convention (F_Type) = Convention_Ada
3626 and then not Has_Warnings_Off (F_Type)
3627 and then not Has_Size_Clause (F_Type)
3628 then
3629 Error_Msg_N
3630 ("& is an 8-bit Ada Boolean?x?", Formal);
3631 Error_Msg_N
3632 ("\use appropriate corresponding type in C "
3633 & "(e.g. char)?x?", Formal);
3635 -- Check suspicious tagged type
3637 elsif (Is_Tagged_Type (F_Type)
3638 or else
3639 (Is_Access_Type (F_Type)
3640 and then Is_Tagged_Type (Designated_Type (F_Type))))
3641 and then Convention (E) = Convention_C
3642 then
3643 Error_Msg_N
3644 ("?x?& involves a tagged type which does not "
3645 & "correspond to any C type!", Formal);
3647 -- Check wrong convention subprogram pointer
3649 elsif Ekind (F_Type) = E_Access_Subprogram_Type
3650 and then not Has_Foreign_Convention (F_Type)
3651 then
3652 Error_Msg_N
3653 ("?x?subprogram pointer & should "
3654 & "have foreign convention!", Formal);
3655 Error_Msg_Sloc := Sloc (F_Type);
3656 Error_Msg_NE
3657 ("\?x?add Convention pragma to declaration of &#",
3658 Formal, F_Type);
3659 end if;
3661 -- Turn off name qualification after message output
3663 Error_Msg_Qual_Level := 0;
3664 end if;
3666 -- Check for unconstrained array in exported foreign convention
3667 -- case.
3669 if Has_Foreign_Convention (E)
3670 and then not Is_Imported (E)
3671 and then Is_Array_Type (F_Type)
3672 and then not Is_Constrained (F_Type)
3673 and then Warn_On_Export_Import
3674 then
3675 Error_Msg_Qual_Level := 1;
3677 -- If this is an inherited operation, place the warning on
3678 -- the derived type declaration, rather than on the original
3679 -- subprogram.
3681 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
3682 then
3683 Warn_Node := Parent (E);
3685 if Formal = First_Formal (E) then
3686 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
3687 end if;
3688 else
3689 Warn_Node := Formal;
3690 end if;
3692 Error_Msg_NE ("?x?type of argument& is unconstrained array",
3693 Warn_Node, Formal);
3694 Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
3695 Warn_Node, Formal);
3696 Error_Msg_Qual_Level := 0;
3697 end if;
3699 if not From_Limited_With (F_Type) then
3700 if Is_Access_Type (F_Type) then
3701 F_Type := Designated_Type (F_Type);
3702 end if;
3704 -- If the formal is an anonymous_access_to_subprogram
3705 -- freeze the subprogram type as well, to prevent
3706 -- scope anomalies in gigi, because there is no other
3707 -- clear point at which it could be frozen.
3709 if Is_Itype (Etype (Formal))
3710 and then Ekind (F_Type) = E_Subprogram_Type
3711 then
3712 Freeze_And_Append (F_Type, N, Result);
3713 end if;
3714 end if;
3716 Next_Formal (Formal);
3717 end loop;
3719 -- Case of function: similar checks on return type
3721 if Ekind (E) = E_Function then
3723 -- Freeze return type
3725 R_Type := Etype (E);
3727 -- AI05-0151: the return type may have been incomplete at the
3728 -- point of declaration. Replace it with the full view, unless the
3729 -- current type is a limited view. In that case the full view is
3730 -- in a different unit, and gigi finds the non-limited view after
3731 -- the other unit is elaborated.
3733 if Ekind (R_Type) = E_Incomplete_Type
3734 and then Present (Full_View (R_Type))
3735 and then not From_Limited_With (R_Type)
3736 then
3737 R_Type := Full_View (R_Type);
3738 Set_Etype (E, R_Type);
3739 end if;
3741 Freeze_And_Append (R_Type, N, Result);
3743 -- Check suspicious return type for C function
3745 if Warn_On_Export_Import
3746 and then (Convention (E) = Convention_C
3747 or else
3748 Convention (E) = Convention_CPP)
3749 and then (Is_Imported (E) or else Is_Exported (E))
3750 then
3751 -- Check suspicious return of fat C pointer
3753 if Is_Access_Type (R_Type)
3754 and then Esize (R_Type) > Ttypes.System_Address_Size
3755 and then not Has_Warnings_Off (E)
3756 and then not Has_Warnings_Off (R_Type)
3757 then
3758 Error_Msg_N
3759 ("?x?return type of& does not correspond to C pointer!",
3762 -- Check suspicious return of boolean
3764 elsif Root_Type (R_Type) = Standard_Boolean
3765 and then Convention (R_Type) = Convention_Ada
3766 and then not Has_Warnings_Off (E)
3767 and then not Has_Warnings_Off (R_Type)
3768 and then not Has_Size_Clause (R_Type)
3769 then
3770 declare
3771 N : constant Node_Id :=
3772 Result_Definition (Declaration_Node (E));
3773 begin
3774 Error_Msg_NE
3775 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
3776 Error_Msg_NE
3777 ("\use appropriate corresponding type in C "
3778 & "(e.g. char)?x?", N, E);
3779 end;
3781 -- Check suspicious return tagged type
3783 elsif (Is_Tagged_Type (R_Type)
3784 or else (Is_Access_Type (R_Type)
3785 and then
3786 Is_Tagged_Type
3787 (Designated_Type (R_Type))))
3788 and then Convention (E) = Convention_C
3789 and then not Has_Warnings_Off (E)
3790 and then not Has_Warnings_Off (R_Type)
3791 then
3792 Error_Msg_N ("?x?return type of & does not "
3793 & "correspond to C type!", E);
3795 -- Check return of wrong convention subprogram pointer
3797 elsif Ekind (R_Type) = E_Access_Subprogram_Type
3798 and then not Has_Foreign_Convention (R_Type)
3799 and then not Has_Warnings_Off (E)
3800 and then not Has_Warnings_Off (R_Type)
3801 then
3802 Error_Msg_N ("?x?& should return a foreign "
3803 & "convention subprogram pointer", E);
3804 Error_Msg_Sloc := Sloc (R_Type);
3805 Error_Msg_NE
3806 ("\?x?add Convention pragma to declaration of& #",
3807 E, R_Type);
3808 end if;
3809 end if;
3811 -- Give warning for suspicious return of a result of an
3812 -- unconstrained array type in a foreign convention function.
3814 if Has_Foreign_Convention (E)
3816 -- We are looking for a return of unconstrained array
3818 and then Is_Array_Type (R_Type)
3819 and then not Is_Constrained (R_Type)
3821 -- Exclude imported routines, the warning does not belong on
3822 -- the import, but rather on the routine definition.
3824 and then not Is_Imported (E)
3826 -- Check that general warning is enabled, and that it is not
3827 -- suppressed for this particular case.
3829 and then Warn_On_Export_Import
3830 and then not Has_Warnings_Off (E)
3831 and then not Has_Warnings_Off (R_Type)
3832 then
3833 Error_Msg_N
3834 ("?x?foreign convention function& should not return "
3835 & "unconstrained array!", E);
3836 end if;
3837 end if;
3839 -- Check suspicious use of Import in pure unit (cases where the RM
3840 -- allows calls to be omitted).
3842 if Is_Imported (E)
3844 -- It might be suspicious if the compilation unit has the Pure
3845 -- aspect/pragma.
3847 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
3849 -- The RM allows omission of calls only in the case of
3850 -- library-level subprograms (see RM-10.2.1(18)).
3852 and then Is_Library_Level_Entity (E)
3854 -- Ignore internally generated entity. This happens in some cases
3855 -- of subprograms in specs, where we generate an implied body.
3857 and then Comes_From_Source (Import_Pragma (E))
3859 -- Assume run-time knows what it is doing
3861 and then not GNAT_Mode
3863 -- Assume explicit Pure_Function means import is pure
3865 and then not Has_Pragma_Pure_Function (E)
3867 -- Don't need warning in relaxed semantics mode
3869 and then not Relaxed_RM_Semantics
3871 -- Assume convention Intrinsic is OK, since this is specialized.
3872 -- This deals with the DEC unit current_exception.ads
3874 and then Convention (E) /= Convention_Intrinsic
3876 -- Assume that ASM interface knows what it is doing. This deals
3877 -- with e.g. unsigned.ads in the AAMP back end.
3879 and then Convention (E) /= Convention_Assembler
3880 then
3881 Error_Msg_N
3882 ("pragma Import in Pure unit??", Import_Pragma (E));
3883 Error_Msg_NE
3884 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
3885 Import_Pragma (E), E);
3886 end if;
3888 return True;
3889 end Freeze_Profile;
3891 ------------------------
3892 -- Freeze_Record_Type --
3893 ------------------------
3895 procedure Freeze_Record_Type (Rec : Entity_Id) is
3896 ADC : Node_Id;
3897 Comp : Entity_Id;
3898 IR : Node_Id;
3899 Prev : Entity_Id;
3901 Junk : Boolean;
3902 pragma Warnings (Off, Junk);
3904 Aliased_Component : Boolean := False;
3905 -- Set True if we find at least one component which is aliased. This
3906 -- is used to prevent Implicit_Packing of the record, since packing
3907 -- cannot modify the size of alignment of an aliased component.
3909 All_Elem_Components : Boolean := True;
3910 -- True if all components are of a type whose underlying type is
3911 -- elementary.
3913 All_Sized_Components : Boolean := True;
3914 -- True if all components have a known RM_Size
3916 All_Storage_Unit_Components : Boolean := True;
3917 -- True if all components have an RM_Size that is a multiple of the
3918 -- storage unit.
3920 Elem_Component_Total_Esize : Uint := Uint_0;
3921 -- Accumulates total Esize values of all elementary components. Used
3922 -- for processing of Implicit_Packing.
3924 Placed_Component : Boolean := False;
3925 -- Set True if we find at least one component with a component
3926 -- clause (used to warn about useless Bit_Order pragmas, and also
3927 -- to detect cases where Implicit_Packing may have an effect).
3929 Rec_Pushed : Boolean := False;
3930 -- Set True if the record type scope Rec has been pushed on the scope
3931 -- stack. Needed for the analysis of delayed aspects specified to the
3932 -- components of Rec.
3934 Sized_Component_Total_RM_Size : Uint := Uint_0;
3935 -- Accumulates total RM_Size values of all sized components. Used
3936 -- for processing of Implicit_Packing.
3938 Sized_Component_Total_Round_RM_Size : Uint := Uint_0;
3939 -- Accumulates total RM_Size values of all sized components, rounded
3940 -- individually to a multiple of the storage unit.
3942 SSO_ADC : Node_Id;
3943 -- Scalar_Storage_Order attribute definition clause for the record
3945 SSO_ADC_Component : Boolean := False;
3946 -- Set True if we find at least one component whose type has a
3947 -- Scalar_Storage_Order attribute definition clause.
3949 Unplaced_Component : Boolean := False;
3950 -- Set True if we find at least one component with no component
3951 -- clause (used to warn about useless Pack pragmas).
3953 function Check_Allocator (N : Node_Id) return Node_Id;
3954 -- If N is an allocator, possibly wrapped in one or more level of
3955 -- qualified expression(s), return the inner allocator node, else
3956 -- return Empty.
3958 procedure Check_Itype (Typ : Entity_Id);
3959 -- If the component subtype is an access to a constrained subtype of
3960 -- an already frozen type, make the subtype frozen as well. It might
3961 -- otherwise be frozen in the wrong scope, and a freeze node on
3962 -- subtype has no effect. Similarly, if the component subtype is a
3963 -- regular (not protected) access to subprogram, set the anonymous
3964 -- subprogram type to frozen as well, to prevent an out-of-scope
3965 -- freeze node at some eventual point of call. Protected operations
3966 -- are handled elsewhere.
3968 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3969 -- Make sure that all types mentioned in Discrete_Choices of the
3970 -- variants referenceed by the Variant_Part VP are frozen. This is
3971 -- a recursive routine to deal with nested variants.
3973 ---------------------
3974 -- Check_Allocator --
3975 ---------------------
3977 function Check_Allocator (N : Node_Id) return Node_Id is
3978 Inner : Node_Id;
3979 begin
3980 Inner := N;
3981 loop
3982 if Nkind (Inner) = N_Allocator then
3983 return Inner;
3984 elsif Nkind (Inner) = N_Qualified_Expression then
3985 Inner := Expression (Inner);
3986 else
3987 return Empty;
3988 end if;
3989 end loop;
3990 end Check_Allocator;
3992 -----------------
3993 -- Check_Itype --
3994 -----------------
3996 procedure Check_Itype (Typ : Entity_Id) is
3997 Desig : constant Entity_Id := Designated_Type (Typ);
3999 begin
4000 if not Is_Frozen (Desig)
4001 and then Is_Frozen (Base_Type (Desig))
4002 then
4003 Set_Is_Frozen (Desig);
4005 -- In addition, add an Itype_Reference to ensure that the
4006 -- access subtype is elaborated early enough. This cannot be
4007 -- done if the subtype may depend on discriminants.
4009 if Ekind (Comp) = E_Component
4010 and then Is_Itype (Etype (Comp))
4011 and then not Has_Discriminants (Rec)
4012 then
4013 IR := Make_Itype_Reference (Sloc (Comp));
4014 Set_Itype (IR, Desig);
4015 Add_To_Result (IR);
4016 end if;
4018 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
4019 and then Convention (Desig) /= Convention_Protected
4020 then
4021 Set_Is_Frozen (Desig);
4022 end if;
4023 end Check_Itype;
4025 ------------------------------------
4026 -- Freeze_Choices_In_Variant_Part --
4027 ------------------------------------
4029 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
4030 pragma Assert (Nkind (VP) = N_Variant_Part);
4032 Variant : Node_Id;
4033 Choice : Node_Id;
4034 CL : Node_Id;
4036 begin
4037 -- Loop through variants
4039 Variant := First_Non_Pragma (Variants (VP));
4040 while Present (Variant) loop
4042 -- Loop through choices, checking that all types are frozen
4044 Choice := First_Non_Pragma (Discrete_Choices (Variant));
4045 while Present (Choice) loop
4046 if Nkind (Choice) in N_Has_Etype
4047 and then Present (Etype (Choice))
4048 then
4049 Freeze_And_Append (Etype (Choice), N, Result);
4050 end if;
4052 Next_Non_Pragma (Choice);
4053 end loop;
4055 -- Check for nested variant part to process
4057 CL := Component_List (Variant);
4059 if not Null_Present (CL) then
4060 if Present (Variant_Part (CL)) then
4061 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
4062 end if;
4063 end if;
4065 Next_Non_Pragma (Variant);
4066 end loop;
4067 end Freeze_Choices_In_Variant_Part;
4069 -- Start of processing for Freeze_Record_Type
4071 begin
4072 -- Deal with delayed aspect specifications for components. The
4073 -- analysis of the aspect is required to be delayed to the freeze
4074 -- point, thus we analyze the pragma or attribute definition
4075 -- clause in the tree at this point. We also analyze the aspect
4076 -- specification node at the freeze point when the aspect doesn't
4077 -- correspond to pragma/attribute definition clause.
4079 Comp := First_Entity (Rec);
4080 while Present (Comp) loop
4081 if Ekind (Comp) = E_Component
4082 and then Has_Delayed_Aspects (Comp)
4083 then
4084 if not Rec_Pushed then
4085 Push_Scope (Rec);
4086 Rec_Pushed := True;
4088 -- The visibility to the discriminants must be restored in
4089 -- order to properly analyze the aspects.
4091 if Has_Discriminants (Rec) then
4092 Install_Discriminants (Rec);
4093 end if;
4094 end if;
4096 Analyze_Aspects_At_Freeze_Point (Comp);
4097 end if;
4099 Next_Entity (Comp);
4100 end loop;
4102 -- Pop the scope if Rec scope has been pushed on the scope stack
4103 -- during the delayed aspect analysis process.
4105 if Rec_Pushed then
4106 if Has_Discriminants (Rec) then
4107 Uninstall_Discriminants (Rec);
4108 end if;
4110 Pop_Scope;
4111 end if;
4113 -- Freeze components and embedded subtypes
4115 Comp := First_Entity (Rec);
4116 Prev := Empty;
4117 while Present (Comp) loop
4118 if Is_Aliased (Comp) then
4119 Aliased_Component := True;
4120 end if;
4122 -- Handle the component and discriminant case
4124 if Ekind_In (Comp, E_Component, E_Discriminant) then
4125 declare
4126 CC : constant Node_Id := Component_Clause (Comp);
4128 begin
4129 -- Freezing a record type freezes the type of each of its
4130 -- components. However, if the type of the component is
4131 -- part of this record, we do not want or need a separate
4132 -- Freeze_Node. Note that Is_Itype is wrong because that's
4133 -- also set in private type cases. We also can't check for
4134 -- the Scope being exactly Rec because of private types and
4135 -- record extensions.
4137 if Is_Itype (Etype (Comp))
4138 and then Is_Record_Type (Underlying_Type
4139 (Scope (Etype (Comp))))
4140 then
4141 Undelay_Type (Etype (Comp));
4142 end if;
4144 Freeze_And_Append (Etype (Comp), N, Result);
4146 -- Warn for pragma Pack overriding foreign convention
4148 if Has_Foreign_Convention (Etype (Comp))
4149 and then Has_Pragma_Pack (Rec)
4151 -- Don't warn for aliased components, since override
4152 -- cannot happen in that case.
4154 and then not Is_Aliased (Comp)
4155 then
4156 declare
4157 CN : constant Name_Id :=
4158 Get_Convention_Name (Convention (Etype (Comp)));
4159 PP : constant Node_Id :=
4160 Get_Pragma (Rec, Pragma_Pack);
4161 begin
4162 if Present (PP) then
4163 Error_Msg_Name_1 := CN;
4164 Error_Msg_Sloc := Sloc (Comp);
4165 Error_Msg_N
4166 ("pragma Pack affects convention % component#??",
4167 PP);
4168 Error_Msg_Name_1 := CN;
4169 Error_Msg_NE
4170 ("\component & may not have % compatible "
4171 & "representation??", PP, Comp);
4172 end if;
4173 end;
4174 end if;
4176 -- Check for error of component clause given for variable
4177 -- sized type. We have to delay this test till this point,
4178 -- since the component type has to be frozen for us to know
4179 -- if it is variable length.
4181 if Present (CC) then
4182 Placed_Component := True;
4184 -- We omit this test in a generic context, it will be
4185 -- applied at instantiation time.
4187 if Inside_A_Generic then
4188 null;
4190 -- Also omit this test in CodePeer mode, since we do not
4191 -- have sufficient info on size and rep clauses.
4193 elsif CodePeer_Mode then
4194 null;
4196 -- Omit check if component has a generic type. This can
4197 -- happen in an instantiation within a generic in ASIS
4198 -- mode, where we force freeze actions without full
4199 -- expansion.
4201 elsif Is_Generic_Type (Etype (Comp)) then
4202 null;
4204 -- Do the check
4206 elsif not
4207 Size_Known_At_Compile_Time
4208 (Underlying_Type (Etype (Comp)))
4209 then
4210 Error_Msg_N
4211 ("component clause not allowed for variable " &
4212 "length component", CC);
4213 end if;
4215 else
4216 Unplaced_Component := True;
4217 end if;
4219 -- Case of component requires byte alignment
4221 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
4223 -- Set the enclosing record to also require byte align
4225 Set_Must_Be_On_Byte_Boundary (Rec);
4227 -- Check for component clause that is inconsistent with
4228 -- the required byte boundary alignment.
4230 if Present (CC)
4231 and then Normalized_First_Bit (Comp) mod
4232 System_Storage_Unit /= 0
4233 then
4234 Error_Msg_N
4235 ("component & must be byte aligned",
4236 Component_Name (Component_Clause (Comp)));
4237 end if;
4238 end if;
4239 end;
4240 end if;
4242 -- Gather data for possible Implicit_Packing later. Note that at
4243 -- this stage we might be dealing with a real component, or with
4244 -- an implicit subtype declaration.
4246 if Known_Static_RM_Size (Etype (Comp)) then
4247 declare
4248 Comp_Type : constant Entity_Id := Etype (Comp);
4249 Comp_Size : constant Uint := RM_Size (Comp_Type);
4250 SSU : constant Int := Ttypes.System_Storage_Unit;
4252 begin
4253 Sized_Component_Total_RM_Size :=
4254 Sized_Component_Total_RM_Size + Comp_Size;
4256 Sized_Component_Total_Round_RM_Size :=
4257 Sized_Component_Total_Round_RM_Size +
4258 (Comp_Size + SSU - 1) / SSU * SSU;
4260 if Present (Underlying_Type (Comp_Type))
4261 and then Is_Elementary_Type (Underlying_Type (Comp_Type))
4262 then
4263 Elem_Component_Total_Esize :=
4264 Elem_Component_Total_Esize + Esize (Comp_Type);
4265 else
4266 All_Elem_Components := False;
4268 if Comp_Size mod SSU /= 0 then
4269 All_Storage_Unit_Components := False;
4270 end if;
4271 end if;
4272 end;
4273 else
4274 All_Sized_Components := False;
4275 end if;
4277 -- If the component is an Itype with Delayed_Freeze and is either
4278 -- a record or array subtype and its base type has not yet been
4279 -- frozen, we must remove this from the entity list of this record
4280 -- and put it on the entity list of the scope of its base type.
4281 -- Note that we know that this is not the type of a component
4282 -- since we cleared Has_Delayed_Freeze for it in the previous
4283 -- loop. Thus this must be the Designated_Type of an access type,
4284 -- which is the type of a component.
4286 if Is_Itype (Comp)
4287 and then Is_Type (Scope (Comp))
4288 and then Is_Composite_Type (Comp)
4289 and then Base_Type (Comp) /= Comp
4290 and then Has_Delayed_Freeze (Comp)
4291 and then not Is_Frozen (Base_Type (Comp))
4292 then
4293 declare
4294 Will_Be_Frozen : Boolean := False;
4295 S : Entity_Id;
4297 begin
4298 -- We have a difficult case to handle here. Suppose Rec is
4299 -- subtype being defined in a subprogram that's created as
4300 -- part of the freezing of Rec'Base. In that case, we know
4301 -- that Comp'Base must have already been frozen by the time
4302 -- we get to elaborate this because Gigi doesn't elaborate
4303 -- any bodies until it has elaborated all of the declarative
4304 -- part. But Is_Frozen will not be set at this point because
4305 -- we are processing code in lexical order.
4307 -- We detect this case by going up the Scope chain of Rec
4308 -- and seeing if we have a subprogram scope before reaching
4309 -- the top of the scope chain or that of Comp'Base. If we
4310 -- do, then mark that Comp'Base will actually be frozen. If
4311 -- so, we merely undelay it.
4313 S := Scope (Rec);
4314 while Present (S) loop
4315 if Is_Subprogram (S) then
4316 Will_Be_Frozen := True;
4317 exit;
4318 elsif S = Scope (Base_Type (Comp)) then
4319 exit;
4320 end if;
4322 S := Scope (S);
4323 end loop;
4325 if Will_Be_Frozen then
4326 Undelay_Type (Comp);
4328 else
4329 if Present (Prev) then
4330 Link_Entities (Prev, Next_Entity (Comp));
4331 else
4332 Set_First_Entity (Rec, Next_Entity (Comp));
4333 end if;
4335 -- Insert in entity list of scope of base type (which
4336 -- must be an enclosing scope, because still unfrozen).
4338 Append_Entity (Comp, Scope (Base_Type (Comp)));
4339 end if;
4340 end;
4342 -- If the component is an access type with an allocator as default
4343 -- value, the designated type will be frozen by the corresponding
4344 -- expression in init_proc. In order to place the freeze node for
4345 -- the designated type before that for the current record type,
4346 -- freeze it now.
4348 -- Same process if the component is an array of access types,
4349 -- initialized with an aggregate. If the designated type is
4350 -- private, it cannot contain allocators, and it is premature
4351 -- to freeze the type, so we check for this as well.
4353 elsif Is_Access_Type (Etype (Comp))
4354 and then Present (Parent (Comp))
4355 and then Present (Expression (Parent (Comp)))
4356 then
4357 declare
4358 Alloc : constant Node_Id :=
4359 Check_Allocator (Expression (Parent (Comp)));
4361 begin
4362 if Present (Alloc) then
4364 -- If component is pointer to a class-wide type, freeze
4365 -- the specific type in the expression being allocated.
4366 -- The expression may be a subtype indication, in which
4367 -- case freeze the subtype mark.
4369 if Is_Class_Wide_Type
4370 (Designated_Type (Etype (Comp)))
4371 then
4372 if Is_Entity_Name (Expression (Alloc)) then
4373 Freeze_And_Append
4374 (Entity (Expression (Alloc)), N, Result);
4376 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
4377 then
4378 Freeze_And_Append
4379 (Entity (Subtype_Mark (Expression (Alloc))),
4380 N, Result);
4381 end if;
4383 elsif Is_Itype (Designated_Type (Etype (Comp))) then
4384 Check_Itype (Etype (Comp));
4386 else
4387 Freeze_And_Append
4388 (Designated_Type (Etype (Comp)), N, Result);
4389 end if;
4390 end if;
4391 end;
4393 elsif Is_Access_Type (Etype (Comp))
4394 and then Is_Itype (Designated_Type (Etype (Comp)))
4395 then
4396 Check_Itype (Etype (Comp));
4398 -- Freeze the designated type when initializing a component with
4399 -- an aggregate in case the aggregate contains allocators.
4401 -- type T is ...;
4402 -- type T_Ptr is access all T;
4403 -- type T_Array is array ... of T_Ptr;
4405 -- type Rec is record
4406 -- Comp : T_Array := (others => ...);
4407 -- end record;
4409 elsif Is_Array_Type (Etype (Comp))
4410 and then Is_Access_Type (Component_Type (Etype (Comp)))
4411 then
4412 declare
4413 Comp_Par : constant Node_Id := Parent (Comp);
4414 Desig_Typ : constant Entity_Id :=
4415 Designated_Type
4416 (Component_Type (Etype (Comp)));
4418 begin
4419 -- The only case when this sort of freezing is not done is
4420 -- when the designated type is class-wide and the root type
4421 -- is the record owning the component. This scenario results
4422 -- in a circularity because the class-wide type requires
4423 -- primitives that have not been created yet as the root
4424 -- type is in the process of being frozen.
4426 -- type Rec is tagged;
4427 -- type Rec_Ptr is access all Rec'Class;
4428 -- type Rec_Array is array ... of Rec_Ptr;
4430 -- type Rec is record
4431 -- Comp : Rec_Array := (others => ...);
4432 -- end record;
4434 if Is_Class_Wide_Type (Desig_Typ)
4435 and then Root_Type (Desig_Typ) = Rec
4436 then
4437 null;
4439 elsif Is_Fully_Defined (Desig_Typ)
4440 and then Present (Comp_Par)
4441 and then Nkind (Comp_Par) = N_Component_Declaration
4442 and then Present (Expression (Comp_Par))
4443 and then Nkind (Expression (Comp_Par)) = N_Aggregate
4444 then
4445 Freeze_And_Append (Desig_Typ, N, Result);
4446 end if;
4447 end;
4448 end if;
4450 Prev := Comp;
4451 Next_Entity (Comp);
4452 end loop;
4454 SSO_ADC :=
4455 Get_Attribute_Definition_Clause
4456 (Rec, Attribute_Scalar_Storage_Order);
4458 -- If the record type has Complex_Representation, then it is treated
4459 -- as a scalar in the back end so the storage order is irrelevant.
4461 if Has_Complex_Representation (Rec) then
4462 if Present (SSO_ADC) then
4463 Error_Msg_N
4464 ("??storage order has no effect with Complex_Representation",
4465 SSO_ADC);
4466 end if;
4468 else
4469 -- Deal with default setting of reverse storage order
4471 Set_SSO_From_Default (Rec);
4473 -- Check consistent attribute setting on component types
4475 declare
4476 Comp_ADC_Present : Boolean;
4477 begin
4478 Comp := First_Component (Rec);
4479 while Present (Comp) loop
4480 Check_Component_Storage_Order
4481 (Encl_Type => Rec,
4482 Comp => Comp,
4483 ADC => SSO_ADC,
4484 Comp_ADC_Present => Comp_ADC_Present);
4485 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
4486 Next_Component (Comp);
4487 end loop;
4488 end;
4490 -- Now deal with reverse storage order/bit order issues
4492 if Present (SSO_ADC) then
4494 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
4495 -- if the former is specified.
4497 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
4499 -- Note: report error on Rec, not on SSO_ADC, as ADC may
4500 -- apply to some ancestor type.
4502 Error_Msg_Sloc := Sloc (SSO_ADC);
4503 Error_Msg_N
4504 ("scalar storage order for& specified# inconsistent with "
4505 & "bit order", Rec);
4506 end if;
4508 -- Warn if there is a Scalar_Storage_Order attribute definition
4509 -- clause but no component clause, no component that itself has
4510 -- such an attribute definition, and no pragma Pack.
4512 if not (Placed_Component
4513 or else
4514 SSO_ADC_Component
4515 or else
4516 Is_Packed (Rec))
4517 then
4518 Error_Msg_N
4519 ("??scalar storage order specified but no component "
4520 & "clause", SSO_ADC);
4521 end if;
4522 end if;
4523 end if;
4525 -- Deal with Bit_Order aspect
4527 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
4529 if Present (ADC) and then Base_Type (Rec) = Rec then
4530 if not (Placed_Component
4531 or else Present (SSO_ADC)
4532 or else Is_Packed (Rec))
4533 then
4534 -- Warn if clause has no effect when no component clause is
4535 -- present, but suppress warning if the Bit_Order is required
4536 -- due to the presence of a Scalar_Storage_Order attribute.
4538 Error_Msg_N
4539 ("??bit order specification has no effect", ADC);
4540 Error_Msg_N
4541 ("\??since no component clauses were specified", ADC);
4543 -- Here is where we do the processing to adjust component clauses
4544 -- for reversed bit order, when not using reverse SSO. If an error
4545 -- has been reported on Rec already (such as SSO incompatible with
4546 -- bit order), don't bother adjusting as this may generate extra
4547 -- noise.
4549 elsif Reverse_Bit_Order (Rec)
4550 and then not Reverse_Storage_Order (Rec)
4551 and then not Error_Posted (Rec)
4552 then
4553 Adjust_Record_For_Reverse_Bit_Order (Rec);
4555 -- Case where we have both an explicit Bit_Order and the same
4556 -- Scalar_Storage_Order: leave record untouched, the back-end
4557 -- will take care of required layout conversions.
4559 else
4560 null;
4562 end if;
4563 end if;
4565 -- Complete error checking on record representation clause (e.g.
4566 -- overlap of components). This is called after adjusting the
4567 -- record for reverse bit order.
4569 declare
4570 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
4571 begin
4572 if Present (RRC) then
4573 Check_Record_Representation_Clause (RRC);
4574 end if;
4575 end;
4577 -- Check for useless pragma Pack when all components placed. We only
4578 -- do this check for record types, not subtypes, since a subtype may
4579 -- have all its components placed, and it still makes perfectly good
4580 -- sense to pack other subtypes or the parent type. We do not give
4581 -- this warning if Optimize_Alignment is set to Space, since the
4582 -- pragma Pack does have an effect in this case (it always resets
4583 -- the alignment to one).
4585 if Ekind (Rec) = E_Record_Type
4586 and then Is_Packed (Rec)
4587 and then not Unplaced_Component
4588 and then Optimize_Alignment /= 'S'
4589 then
4590 -- Reset packed status. Probably not necessary, but we do it so
4591 -- that there is no chance of the back end doing something strange
4592 -- with this redundant indication of packing.
4594 Set_Is_Packed (Rec, False);
4596 -- Give warning if redundant constructs warnings on
4598 if Warn_On_Redundant_Constructs then
4599 Error_Msg_N -- CODEFIX
4600 ("??pragma Pack has no effect, no unplaced components",
4601 Get_Rep_Pragma (Rec, Name_Pack));
4602 end if;
4603 end if;
4605 -- If this is the record corresponding to a remote type, freeze the
4606 -- remote type here since that is what we are semantically freezing.
4607 -- This prevents the freeze node for that type in an inner scope.
4609 if Ekind (Rec) = E_Record_Type then
4610 if Present (Corresponding_Remote_Type (Rec)) then
4611 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
4612 end if;
4614 -- Check for controlled components, unchecked unions, and type
4615 -- invariants.
4617 Comp := First_Component (Rec);
4618 while Present (Comp) loop
4620 -- Do not set Has_Controlled_Component on a class-wide
4621 -- equivalent type. See Make_CW_Equivalent_Type.
4623 if not Is_Class_Wide_Equivalent_Type (Rec)
4624 and then
4625 (Has_Controlled_Component (Etype (Comp))
4626 or else
4627 (Chars (Comp) /= Name_uParent
4628 and then Is_Controlled (Etype (Comp)))
4629 or else
4630 (Is_Protected_Type (Etype (Comp))
4631 and then
4632 Present (Corresponding_Record_Type (Etype (Comp)))
4633 and then
4634 Has_Controlled_Component
4635 (Corresponding_Record_Type (Etype (Comp)))))
4636 then
4637 Set_Has_Controlled_Component (Rec);
4638 end if;
4640 if Has_Unchecked_Union (Etype (Comp)) then
4641 Set_Has_Unchecked_Union (Rec);
4642 end if;
4644 -- The record type requires its own invariant procedure in
4645 -- order to verify the invariant of each individual component.
4646 -- Do not consider internal components such as _parent because
4647 -- parent class-wide invariants are always inherited.
4648 -- In GNATprove mode, the component invariants are checked by
4649 -- other means. They should not be added to the record type
4650 -- invariant procedure, so that the procedure can be used to
4651 -- check the recordy type invariants if any.
4653 if Comes_From_Source (Comp)
4654 and then Has_Invariants (Etype (Comp))
4655 and then not GNATprove_Mode
4656 then
4657 Set_Has_Own_Invariants (Rec);
4658 end if;
4660 -- Scan component declaration for likely misuses of current
4661 -- instance, either in a constraint or a default expression.
4663 if Has_Per_Object_Constraint (Comp) then
4664 Check_Current_Instance (Parent (Comp));
4665 end if;
4667 Next_Component (Comp);
4668 end loop;
4669 end if;
4671 -- Enforce the restriction that access attributes with a current
4672 -- instance prefix can only apply to limited types. This comment
4673 -- is floating here, but does not seem to belong here???
4675 -- Set component alignment if not otherwise already set
4677 Set_Component_Alignment_If_Not_Set (Rec);
4679 -- For first subtypes, check if there are any fixed-point fields with
4680 -- component clauses, where we must check the size. This is not done
4681 -- till the freeze point since for fixed-point types, we do not know
4682 -- the size until the type is frozen. Similar processing applies to
4683 -- bit-packed arrays.
4685 if Is_First_Subtype (Rec) then
4686 Comp := First_Component (Rec);
4687 while Present (Comp) loop
4688 if Present (Component_Clause (Comp))
4689 and then (Is_Fixed_Point_Type (Etype (Comp))
4690 or else Is_Bit_Packed_Array (Etype (Comp)))
4691 then
4692 Check_Size
4693 (Component_Name (Component_Clause (Comp)),
4694 Etype (Comp),
4695 Esize (Comp),
4696 Junk);
4697 end if;
4699 Next_Component (Comp);
4700 end loop;
4701 end if;
4703 -- See if Size is too small as is (and implicit packing might help)
4705 if not Is_Packed (Rec)
4707 -- No implicit packing if even one component is explicitly placed
4709 and then not Placed_Component
4711 -- Or even one component is aliased
4713 and then not Aliased_Component
4715 -- Must have size clause and all sized components
4717 and then Has_Size_Clause (Rec)
4718 and then All_Sized_Components
4720 -- Do not try implicit packing on records with discriminants, too
4721 -- complicated, especially in the variant record case.
4723 and then not Has_Discriminants (Rec)
4725 -- We want to implicitly pack if the specified size of the record
4726 -- is less than the sum of the object sizes (no point in packing
4727 -- if this is not the case), if we can compute it, i.e. if we have
4728 -- only elementary components. Otherwise, we have at least one
4729 -- composite component and we want to implicitly pack only if bit
4730 -- packing is required for it, as we are sure in this case that
4731 -- the back end cannot do the expected layout without packing.
4733 and then
4734 ((All_Elem_Components
4735 and then RM_Size (Rec) < Elem_Component_Total_Esize)
4736 or else
4737 (not All_Elem_Components
4738 and then not All_Storage_Unit_Components
4739 and then RM_Size (Rec) < Sized_Component_Total_Round_RM_Size))
4741 -- And the total RM size cannot be greater than the specified size
4742 -- since otherwise packing will not get us where we have to be.
4744 and then Sized_Component_Total_RM_Size <= RM_Size (Rec)
4746 -- Never do implicit packing in CodePeer or SPARK modes since
4747 -- we don't do any packing in these modes, since this generates
4748 -- over-complex code that confuses static analysis, and in
4749 -- general, neither CodePeer not GNATprove care about the
4750 -- internal representation of objects.
4752 and then not (CodePeer_Mode or GNATprove_Mode)
4753 then
4754 -- If implicit packing enabled, do it
4756 if Implicit_Packing then
4757 Set_Is_Packed (Rec);
4759 -- Otherwise flag the size clause
4761 else
4762 declare
4763 Sz : constant Node_Id := Size_Clause (Rec);
4764 begin
4765 Error_Msg_NE -- CODEFIX
4766 ("size given for& too small", Sz, Rec);
4767 Error_Msg_N -- CODEFIX
4768 ("\use explicit pragma Pack "
4769 & "or use pragma Implicit_Packing", Sz);
4770 end;
4771 end if;
4772 end if;
4774 -- The following checks are relevant only when SPARK_Mode is on as
4775 -- they are not standard Ada legality rules.
4777 if SPARK_Mode = On then
4779 -- A discriminated type cannot be effectively volatile
4780 -- (SPARK RM 7.1.3(5)).
4782 if Is_Effectively_Volatile (Rec) then
4783 if Has_Discriminants (Rec) then
4784 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
4785 end if;
4787 -- A non-effectively volatile record type cannot contain
4788 -- effectively volatile components (SPARK RM 7.1.3(6)).
4790 else
4791 Comp := First_Component (Rec);
4792 while Present (Comp) loop
4793 if Comes_From_Source (Comp)
4794 and then Is_Effectively_Volatile (Etype (Comp))
4795 then
4796 Error_Msg_Name_1 := Chars (Rec);
4797 Error_Msg_N
4798 ("component & of non-volatile type % cannot be "
4799 & "volatile", Comp);
4800 end if;
4802 Next_Component (Comp);
4803 end loop;
4804 end if;
4806 -- A type which does not yield a synchronized object cannot have
4807 -- a component that yields a synchronized object (SPARK RM 9.5).
4809 if not Yields_Synchronized_Object (Rec) then
4810 Comp := First_Component (Rec);
4811 while Present (Comp) loop
4812 if Comes_From_Source (Comp)
4813 and then Yields_Synchronized_Object (Etype (Comp))
4814 then
4815 Error_Msg_Name_1 := Chars (Rec);
4816 Error_Msg_N
4817 ("component & of non-synchronized type % cannot be "
4818 & "synchronized", Comp);
4819 end if;
4821 Next_Component (Comp);
4822 end loop;
4823 end if;
4825 -- A Ghost type cannot have a component of protected or task type
4826 -- (SPARK RM 6.9(19)).
4828 if Is_Ghost_Entity (Rec) then
4829 Comp := First_Component (Rec);
4830 while Present (Comp) loop
4831 if Comes_From_Source (Comp)
4832 and then Is_Concurrent_Type (Etype (Comp))
4833 then
4834 Error_Msg_Name_1 := Chars (Rec);
4835 Error_Msg_N
4836 ("component & of ghost type % cannot be concurrent",
4837 Comp);
4838 end if;
4840 Next_Component (Comp);
4841 end loop;
4842 end if;
4843 end if;
4845 -- Make sure that if we have an iterator aspect, then we have
4846 -- either Constant_Indexing or Variable_Indexing.
4848 declare
4849 Iterator_Aspect : Node_Id;
4851 begin
4852 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
4854 if No (Iterator_Aspect) then
4855 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
4856 end if;
4858 if Present (Iterator_Aspect) then
4859 if Has_Aspect (Rec, Aspect_Constant_Indexing)
4860 or else
4861 Has_Aspect (Rec, Aspect_Variable_Indexing)
4862 then
4863 null;
4864 else
4865 Error_Msg_N
4866 ("Iterator_Element requires indexing aspect",
4867 Iterator_Aspect);
4868 end if;
4869 end if;
4870 end;
4872 -- All done if not a full record definition
4874 if Ekind (Rec) /= E_Record_Type then
4875 return;
4876 end if;
4878 -- Finally we need to check the variant part to make sure that
4879 -- all types within choices are properly frozen as part of the
4880 -- freezing of the record type.
4882 Check_Variant_Part : declare
4883 D : constant Node_Id := Declaration_Node (Rec);
4884 T : Node_Id;
4885 C : Node_Id;
4887 begin
4888 -- Find component list
4890 C := Empty;
4892 if Nkind (D) = N_Full_Type_Declaration then
4893 T := Type_Definition (D);
4895 if Nkind (T) = N_Record_Definition then
4896 C := Component_List (T);
4898 elsif Nkind (T) = N_Derived_Type_Definition
4899 and then Present (Record_Extension_Part (T))
4900 then
4901 C := Component_List (Record_Extension_Part (T));
4902 end if;
4903 end if;
4905 -- Case of variant part present
4907 if Present (C) and then Present (Variant_Part (C)) then
4908 Freeze_Choices_In_Variant_Part (Variant_Part (C));
4909 end if;
4911 -- Note: we used to call Check_Choices here, but it is too early,
4912 -- since predicated subtypes are frozen here, but their freezing
4913 -- actions are in Analyze_Freeze_Entity, which has not been called
4914 -- yet for entities frozen within this procedure, so we moved that
4915 -- call to the Analyze_Freeze_Entity for the record type.
4917 end Check_Variant_Part;
4919 -- Check that all the primitives of an interface type are abstract
4920 -- or null procedures.
4922 if Is_Interface (Rec)
4923 and then not Error_Posted (Parent (Rec))
4924 then
4925 declare
4926 Elmt : Elmt_Id;
4927 Subp : Entity_Id;
4929 begin
4930 Elmt := First_Elmt (Primitive_Operations (Rec));
4931 while Present (Elmt) loop
4932 Subp := Node (Elmt);
4934 if not Is_Abstract_Subprogram (Subp)
4936 -- Avoid reporting the error on inherited primitives
4938 and then Comes_From_Source (Subp)
4939 then
4940 Error_Msg_Name_1 := Chars (Subp);
4942 if Ekind (Subp) = E_Procedure then
4943 if not Null_Present (Parent (Subp)) then
4944 Error_Msg_N
4945 ("interface procedure % must be abstract or null",
4946 Parent (Subp));
4947 end if;
4948 else
4949 Error_Msg_N
4950 ("interface function % must be abstract",
4951 Parent (Subp));
4952 end if;
4953 end if;
4955 Next_Elmt (Elmt);
4956 end loop;
4957 end;
4958 end if;
4960 -- For a derived tagged type, check whether inherited primitives
4961 -- might require a wrapper to handle class-wide conditions.
4963 if Is_Tagged_Type (Rec) and then Is_Derived_Type (Rec) then
4964 Check_Inherited_Conditions (Rec);
4965 end if;
4966 end Freeze_Record_Type;
4968 -------------------------------
4969 -- Has_Boolean_Aspect_Import --
4970 -------------------------------
4972 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4973 Decl : constant Node_Id := Declaration_Node (E);
4974 Asp : Node_Id;
4975 Expr : Node_Id;
4977 begin
4978 if Has_Aspects (Decl) then
4979 Asp := First (Aspect_Specifications (Decl));
4980 while Present (Asp) loop
4981 Expr := Expression (Asp);
4983 -- The value of aspect Import is True when the expression is
4984 -- either missing or it is explicitly set to True.
4986 if Get_Aspect_Id (Asp) = Aspect_Import
4987 and then (No (Expr)
4988 or else (Compile_Time_Known_Value (Expr)
4989 and then Is_True (Expr_Value (Expr))))
4990 then
4991 return True;
4992 end if;
4994 Next (Asp);
4995 end loop;
4996 end if;
4998 return False;
4999 end Has_Boolean_Aspect_Import;
5001 -------------------------
5002 -- Inherit_Freeze_Node --
5003 -------------------------
5005 procedure Inherit_Freeze_Node
5006 (Fnod : Node_Id;
5007 Typ : Entity_Id)
5009 Typ_Fnod : constant Node_Id := Freeze_Node (Typ);
5011 begin
5012 Set_Freeze_Node (Typ, Fnod);
5013 Set_Entity (Fnod, Typ);
5015 -- The input type had an existing node. Propagate relevant attributes
5016 -- from the old freeze node to the inherited freeze node.
5018 -- ??? if both freeze nodes have attributes, would they differ?
5020 if Present (Typ_Fnod) then
5022 -- Attribute Access_Types_To_Process
5024 if Present (Access_Types_To_Process (Typ_Fnod))
5025 and then No (Access_Types_To_Process (Fnod))
5026 then
5027 Set_Access_Types_To_Process (Fnod,
5028 Access_Types_To_Process (Typ_Fnod));
5029 end if;
5031 -- Attribute Actions
5033 if Present (Actions (Typ_Fnod)) and then No (Actions (Fnod)) then
5034 Set_Actions (Fnod, Actions (Typ_Fnod));
5035 end if;
5037 -- Attribute First_Subtype_Link
5039 if Present (First_Subtype_Link (Typ_Fnod))
5040 and then No (First_Subtype_Link (Fnod))
5041 then
5042 Set_First_Subtype_Link (Fnod, First_Subtype_Link (Typ_Fnod));
5043 end if;
5045 -- Attribute TSS_Elist
5047 if Present (TSS_Elist (Typ_Fnod))
5048 and then No (TSS_Elist (Fnod))
5049 then
5050 Set_TSS_Elist (Fnod, TSS_Elist (Typ_Fnod));
5051 end if;
5052 end if;
5053 end Inherit_Freeze_Node;
5055 ------------------------------
5056 -- Wrap_Imported_Subprogram --
5057 ------------------------------
5059 -- The issue here is that our normal approach of checking preconditions
5060 -- and postconditions does not work for imported procedures, since we
5061 -- are not generating code for the body. To get around this we create
5062 -- a wrapper, as shown by the following example:
5064 -- procedure K (A : Integer);
5065 -- pragma Import (C, K);
5067 -- The spec is rewritten by removing the effects of pragma Import, but
5068 -- leaving the convention unchanged, as though the source had said:
5070 -- procedure K (A : Integer);
5071 -- pragma Convention (C, K);
5073 -- and we create a body, added to the entity K freeze actions, which
5074 -- looks like:
5076 -- procedure K (A : Integer) is
5077 -- procedure K (A : Integer);
5078 -- pragma Import (C, K);
5079 -- begin
5080 -- K (A);
5081 -- end K;
5083 -- Now the contract applies in the normal way to the outer procedure,
5084 -- and the inner procedure has no contracts, so there is no problem
5085 -- in just calling it to get the original effect.
5087 -- In the case of a function, we create an appropriate return statement
5088 -- for the subprogram body that calls the inner procedure.
5090 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
5091 function Copy_Import_Pragma return Node_Id;
5092 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
5094 ------------------------
5095 -- Copy_Import_Pragma --
5096 ------------------------
5098 function Copy_Import_Pragma return Node_Id is
5100 -- The subprogram should have an import pragma, otherwise it does
5101 -- need a wrapper.
5103 Prag : constant Node_Id := Import_Pragma (E);
5104 pragma Assert (Present (Prag));
5106 -- Save all semantic fields of the pragma
5108 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
5109 Save_From : constant Boolean := From_Aspect_Specification (Prag);
5110 Save_Prag : constant Node_Id := Next_Pragma (Prag);
5111 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
5113 Result : Node_Id;
5115 begin
5116 -- Reset all semantic fields. This avoids a potential infinite
5117 -- loop when the pragma comes from an aspect as the duplication
5118 -- will copy the aspect, then copy the corresponding pragma and
5119 -- so on.
5121 Set_Corresponding_Aspect (Prag, Empty);
5122 Set_From_Aspect_Specification (Prag, False);
5123 Set_Next_Pragma (Prag, Empty);
5124 Set_Next_Rep_Item (Prag, Empty);
5126 Result := Copy_Separate_Tree (Prag);
5128 -- Restore the original semantic fields
5130 Set_Corresponding_Aspect (Prag, Save_Asp);
5131 Set_From_Aspect_Specification (Prag, Save_From);
5132 Set_Next_Pragma (Prag, Save_Prag);
5133 Set_Next_Rep_Item (Prag, Save_Rep);
5135 return Result;
5136 end Copy_Import_Pragma;
5138 -- Local variables
5140 Loc : constant Source_Ptr := Sloc (E);
5141 CE : constant Name_Id := Chars (E);
5142 Bod : Node_Id;
5143 Forml : Entity_Id;
5144 Parms : List_Id;
5145 Prag : Node_Id;
5146 Spec : Node_Id;
5147 Stmt : Node_Id;
5149 -- Start of processing for Wrap_Imported_Subprogram
5151 begin
5152 -- Nothing to do if not imported
5154 if not Is_Imported (E) then
5155 return;
5157 -- Test enabling conditions for wrapping
5159 elsif Is_Subprogram (E)
5160 and then Present (Contract (E))
5161 and then Present (Pre_Post_Conditions (Contract (E)))
5162 and then not GNATprove_Mode
5163 then
5164 -- Here we do the wrap
5166 -- Note on calls to Copy_Separate_Tree. The trees we are copying
5167 -- here are fully analyzed, but we definitely want fully syntactic
5168 -- unanalyzed trees in the body we construct, so that the analysis
5169 -- generates the right visibility, and that is exactly what the
5170 -- calls to Copy_Separate_Tree give us.
5172 Prag := Copy_Import_Pragma;
5174 -- Fix up spec so it is no longer imported and has convention Ada
5176 Set_Has_Completion (E, False);
5177 Set_Import_Pragma (E, Empty);
5178 Set_Interface_Name (E, Empty);
5179 Set_Is_Imported (E, False);
5180 Set_Convention (E, Convention_Ada);
5182 -- Grab the subprogram declaration and specification
5184 Spec := Declaration_Node (E);
5186 -- Build parameter list that we need
5188 Parms := New_List;
5189 Forml := First_Formal (E);
5190 while Present (Forml) loop
5191 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
5192 Next_Formal (Forml);
5193 end loop;
5195 -- Build the call
5197 -- An imported function whose result type is anonymous access
5198 -- creates a new anonymous access type when it is relocated into
5199 -- the declarations of the body generated below. As a result, the
5200 -- accessibility level of these two anonymous access types may not
5201 -- be compatible even though they are essentially the same type.
5202 -- Use an unchecked type conversion to reconcile this case. Note
5203 -- that the conversion is safe because in the named access type
5204 -- case, both the body and imported function utilize the same
5205 -- type.
5207 if Ekind_In (E, E_Function, E_Generic_Function) then
5208 Stmt :=
5209 Make_Simple_Return_Statement (Loc,
5210 Expression =>
5211 Unchecked_Convert_To (Etype (E),
5212 Make_Function_Call (Loc,
5213 Name => Make_Identifier (Loc, CE),
5214 Parameter_Associations => Parms)));
5216 else
5217 Stmt :=
5218 Make_Procedure_Call_Statement (Loc,
5219 Name => Make_Identifier (Loc, CE),
5220 Parameter_Associations => Parms);
5221 end if;
5223 -- Now build the body
5225 Bod :=
5226 Make_Subprogram_Body (Loc,
5227 Specification =>
5228 Copy_Separate_Tree (Spec),
5229 Declarations => New_List (
5230 Make_Subprogram_Declaration (Loc,
5231 Specification => Copy_Separate_Tree (Spec)),
5232 Prag),
5233 Handled_Statement_Sequence =>
5234 Make_Handled_Sequence_Of_Statements (Loc,
5235 Statements => New_List (Stmt),
5236 End_Label => Make_Identifier (Loc, CE)));
5238 -- Append the body to freeze result
5240 Add_To_Result (Bod);
5241 return;
5243 -- Case of imported subprogram that does not get wrapped
5245 else
5246 -- Set Is_Public. All imported entities need an external symbol
5247 -- created for them since they are always referenced from another
5248 -- object file. Note this used to be set when we set Is_Imported
5249 -- back in Sem_Prag, but now we delay it to this point, since we
5250 -- don't want to set this flag if we wrap an imported subprogram.
5252 Set_Is_Public (E);
5253 end if;
5254 end Wrap_Imported_Subprogram;
5256 -- Start of processing for Freeze_Entity
5258 begin
5259 -- The entity being frozen may be subject to pragma Ghost. Set the mode
5260 -- now to ensure that any nodes generated during freezing are properly
5261 -- flagged as Ghost.
5263 Set_Ghost_Mode (E);
5265 -- We are going to test for various reasons why this entity need not be
5266 -- frozen here, but in the case of an Itype that's defined within a
5267 -- record, that test actually applies to the record.
5269 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
5270 Test_E := Scope (E);
5271 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
5272 and then Is_Record_Type (Underlying_Type (Scope (E)))
5273 then
5274 Test_E := Underlying_Type (Scope (E));
5275 end if;
5277 -- Do not freeze if already frozen since we only need one freeze node
5279 if Is_Frozen (E) then
5280 Result := No_List;
5281 goto Leave;
5283 elsif Ekind (E) = E_Generic_Package then
5284 Result := Freeze_Generic_Entities (E);
5285 goto Leave;
5287 -- It is improper to freeze an external entity within a generic because
5288 -- its freeze node will appear in a non-valid context. The entity will
5289 -- be frozen in the proper scope after the current generic is analyzed.
5290 -- However, aspects must be analyzed because they may be queried later
5291 -- within the generic itself, and the corresponding pragma or attribute
5292 -- definition has not been analyzed yet. After this, indicate that the
5293 -- entity has no further delayed aspects, to prevent a later aspect
5294 -- analysis out of the scope of the generic.
5296 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
5297 if Has_Delayed_Aspects (E) then
5298 Analyze_Aspects_At_Freeze_Point (E);
5299 Set_Has_Delayed_Aspects (E, False);
5300 end if;
5302 Result := No_List;
5303 goto Leave;
5305 -- AI05-0213: A formal incomplete type does not freeze the actual. In
5306 -- the instance, the same applies to the subtype renaming the actual.
5308 elsif Is_Private_Type (E)
5309 and then Is_Generic_Actual_Type (E)
5310 and then No (Full_View (Base_Type (E)))
5311 and then Ada_Version >= Ada_2012
5312 then
5313 Result := No_List;
5314 goto Leave;
5316 -- Formal subprograms are never frozen
5318 elsif Is_Formal_Subprogram (E) then
5319 Result := No_List;
5320 goto Leave;
5322 -- Generic types are never frozen as they lack delayed semantic checks
5324 elsif Is_Generic_Type (E) then
5325 Result := No_List;
5326 goto Leave;
5328 -- Do not freeze a global entity within an inner scope created during
5329 -- expansion. A call to subprogram E within some internal procedure
5330 -- (a stream attribute for example) might require freezing E, but the
5331 -- freeze node must appear in the same declarative part as E itself.
5332 -- The two-pass elaboration mechanism in gigi guarantees that E will
5333 -- be frozen before the inner call is elaborated. We exclude constants
5334 -- from this test, because deferred constants may be frozen early, and
5335 -- must be diagnosed (e.g. in the case of a deferred constant being used
5336 -- in a default expression). If the enclosing subprogram comes from
5337 -- source, or is a generic instance, then the freeze point is the one
5338 -- mandated by the language, and we freeze the entity. A subprogram that
5339 -- is a child unit body that acts as a spec does not have a spec that
5340 -- comes from source, but can only come from source.
5342 elsif In_Open_Scopes (Scope (Test_E))
5343 and then Scope (Test_E) /= Current_Scope
5344 and then Ekind (Test_E) /= E_Constant
5345 then
5346 declare
5347 S : Entity_Id;
5349 begin
5350 S := Current_Scope;
5351 while Present (S) loop
5352 if Is_Overloadable (S) then
5353 if Comes_From_Source (S)
5354 or else Is_Generic_Instance (S)
5355 or else Is_Child_Unit (S)
5356 then
5357 exit;
5358 else
5359 Result := No_List;
5360 goto Leave;
5361 end if;
5362 end if;
5364 S := Scope (S);
5365 end loop;
5366 end;
5368 -- Similarly, an inlined instance body may make reference to global
5369 -- entities, but these references cannot be the proper freezing point
5370 -- for them, and in the absence of inlining freezing will take place in
5371 -- their own scope. Normally instance bodies are analyzed after the
5372 -- enclosing compilation, and everything has been frozen at the proper
5373 -- place, but with front-end inlining an instance body is compiled
5374 -- before the end of the enclosing scope, and as a result out-of-order
5375 -- freezing must be prevented.
5377 elsif Front_End_Inlining
5378 and then In_Instance_Body
5379 and then Present (Scope (Test_E))
5380 then
5381 declare
5382 S : Entity_Id;
5384 begin
5385 S := Scope (Test_E);
5386 while Present (S) loop
5387 if Is_Generic_Instance (S) then
5388 exit;
5389 else
5390 S := Scope (S);
5391 end if;
5392 end loop;
5394 if No (S) then
5395 Result := No_List;
5396 goto Leave;
5397 end if;
5398 end;
5399 end if;
5401 -- Add checks to detect proper initialization of scalars that may appear
5402 -- as subprogram parameters.
5404 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
5405 Apply_Parameter_Validity_Checks (E);
5406 end if;
5408 -- Deal with delayed aspect specifications. The analysis of the aspect
5409 -- is required to be delayed to the freeze point, thus we analyze the
5410 -- pragma or attribute definition clause in the tree at this point. We
5411 -- also analyze the aspect specification node at the freeze point when
5412 -- the aspect doesn't correspond to pragma/attribute definition clause.
5413 -- In addition, a derived type may have inherited aspects that were
5414 -- delayed in the parent, so these must also be captured now.
5416 if Has_Delayed_Aspects (E)
5417 or else May_Inherit_Delayed_Rep_Aspects (E)
5418 then
5419 Analyze_Aspects_At_Freeze_Point (E);
5420 end if;
5422 -- Here to freeze the entity
5424 Set_Is_Frozen (E);
5426 -- Case of entity being frozen is other than a type
5428 if not Is_Type (E) then
5430 -- If entity is exported or imported and does not have an external
5431 -- name, now is the time to provide the appropriate default name.
5432 -- Skip this if the entity is stubbed, since we don't need a name
5433 -- for any stubbed routine. For the case on intrinsics, if no
5434 -- external name is specified, then calls will be handled in
5435 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
5436 -- external name is provided, then Expand_Intrinsic_Call leaves
5437 -- calls in place for expansion by GIGI.
5439 if (Is_Imported (E) or else Is_Exported (E))
5440 and then No (Interface_Name (E))
5441 and then Convention (E) /= Convention_Stubbed
5442 and then Convention (E) /= Convention_Intrinsic
5443 then
5444 Set_Encoded_Interface_Name
5445 (E, Get_Default_External_Name (E));
5447 -- If entity is an atomic object appearing in a declaration and
5448 -- the expression is an aggregate, assign it to a temporary to
5449 -- ensure that the actual assignment is done atomically rather
5450 -- than component-wise (the assignment to the temp may be done
5451 -- component-wise, but that is harmless).
5453 elsif Is_Atomic_Or_VFA (E)
5454 and then Nkind (Parent (E)) = N_Object_Declaration
5455 and then Present (Expression (Parent (E)))
5456 and then Nkind (Expression (Parent (E))) = N_Aggregate
5457 and then Is_Atomic_VFA_Aggregate (Expression (Parent (E)))
5458 then
5459 null;
5460 end if;
5462 -- Subprogram case
5464 if Is_Subprogram (E) then
5466 -- Check for needing to wrap imported subprogram
5468 Wrap_Imported_Subprogram (E);
5470 -- Freeze all parameter types and the return type (RM 13.14(14)).
5471 -- However skip this for internal subprograms. This is also where
5472 -- any extra formal parameters are created since we now know
5473 -- whether the subprogram will use a foreign convention.
5475 -- In Ada 2012, freezing a subprogram does not always freeze the
5476 -- corresponding profile (see AI05-019). An attribute reference
5477 -- is not a freezing point of the profile. Flag Do_Freeze_Profile
5478 -- indicates whether the profile should be frozen now.
5479 -- Other constructs that should not freeze ???
5481 -- This processing doesn't apply to internal entities (see below)
5483 if not Is_Internal (E) and then Do_Freeze_Profile then
5484 if not Freeze_Profile (E) then
5485 goto Leave;
5486 end if;
5487 end if;
5489 -- Must freeze its parent first if it is a derived subprogram
5491 if Present (Alias (E)) then
5492 Freeze_And_Append (Alias (E), N, Result);
5493 end if;
5495 -- We don't freeze internal subprograms, because we don't normally
5496 -- want addition of extra formals or mechanism setting to happen
5497 -- for those. However we do pass through predefined dispatching
5498 -- cases, since extra formals may be needed in some cases, such as
5499 -- for the stream 'Input function (build-in-place formals).
5501 if not Is_Internal (E)
5502 or else Is_Predefined_Dispatching_Operation (E)
5503 then
5504 Freeze_Subprogram (E);
5505 end if;
5507 -- If warning on suspicious contracts then check for the case of
5508 -- a postcondition other than False for a No_Return subprogram.
5510 if No_Return (E)
5511 and then Warn_On_Suspicious_Contract
5512 and then Present (Contract (E))
5513 then
5514 declare
5515 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
5516 Exp : Node_Id;
5518 begin
5519 while Present (Prag) loop
5520 if Nam_In (Pragma_Name_Unmapped (Prag),
5521 Name_Post,
5522 Name_Postcondition,
5523 Name_Refined_Post)
5524 then
5525 Exp :=
5526 Expression
5527 (First (Pragma_Argument_Associations (Prag)));
5529 if Nkind (Exp) /= N_Identifier
5530 or else Chars (Exp) /= Name_False
5531 then
5532 Error_Msg_NE
5533 ("useless postcondition, & is marked "
5534 & "No_Return?T?", Exp, E);
5535 end if;
5536 end if;
5538 Prag := Next_Pragma (Prag);
5539 end loop;
5540 end;
5541 end if;
5543 -- Here for other than a subprogram or type
5545 else
5546 -- If entity has a type, and it is not a generic unit, then
5547 -- freeze it first (RM 13.14(10)).
5549 if Present (Etype (E))
5550 and then Ekind (E) /= E_Generic_Function
5551 then
5552 Freeze_And_Append (Etype (E), N, Result);
5554 -- For an object of an anonymous array type, aspects on the
5555 -- object declaration apply to the type itself. This is the
5556 -- case for Atomic_Components, Volatile_Components, and
5557 -- Independent_Components. In these cases analysis of the
5558 -- generated pragma will mark the anonymous types accordingly,
5559 -- and the object itself does not require a freeze node.
5561 if Ekind (E) = E_Variable
5562 and then Is_Itype (Etype (E))
5563 and then Is_Array_Type (Etype (E))
5564 and then Has_Delayed_Aspects (E)
5565 then
5566 Set_Has_Delayed_Aspects (E, False);
5567 Set_Has_Delayed_Freeze (E, False);
5568 Set_Freeze_Node (E, Empty);
5569 end if;
5570 end if;
5572 -- Special processing for objects created by object declaration
5574 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
5575 Freeze_Object_Declaration (E);
5576 end if;
5578 -- Check that a constant which has a pragma Volatile[_Components]
5579 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
5581 -- Note: Atomic[_Components] also sets Volatile[_Components]
5583 if Ekind (E) = E_Constant
5584 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
5585 and then not Is_Imported (E)
5586 and then not Has_Boolean_Aspect_Import (E)
5587 then
5588 -- Make sure we actually have a pragma, and have not merely
5589 -- inherited the indication from elsewhere (e.g. an address
5590 -- clause, which is not good enough in RM terms).
5592 if Has_Rep_Pragma (E, Name_Atomic)
5593 or else
5594 Has_Rep_Pragma (E, Name_Atomic_Components)
5595 then
5596 Error_Msg_N
5597 ("stand alone atomic constant must be " &
5598 "imported (RM C.6(13))", E);
5600 elsif Has_Rep_Pragma (E, Name_Volatile)
5601 or else
5602 Has_Rep_Pragma (E, Name_Volatile_Components)
5603 then
5604 Error_Msg_N
5605 ("stand alone volatile constant must be " &
5606 "imported (RM C.6(13))", E);
5607 end if;
5608 end if;
5610 -- Static objects require special handling
5612 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
5613 and then Is_Statically_Allocated (E)
5614 then
5615 Freeze_Static_Object (E);
5616 end if;
5618 -- Remaining step is to layout objects
5620 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
5621 or else Is_Formal (E)
5622 then
5623 Layout_Object (E);
5624 end if;
5626 -- For an object that does not have delayed freezing, and whose
5627 -- initialization actions have been captured in a compound
5628 -- statement, move them back now directly within the enclosing
5629 -- statement sequence.
5631 if Ekind_In (E, E_Constant, E_Variable)
5632 and then not Has_Delayed_Freeze (E)
5633 then
5634 Explode_Initialization_Compound_Statement (E);
5635 end if;
5637 -- Do not generate a freeze node for a generic unit
5639 if Is_Generic_Unit (E) then
5640 Result := No_List;
5641 goto Leave;
5642 end if;
5643 end if;
5645 -- Case of a type or subtype being frozen
5647 else
5648 -- Verify several SPARK legality rules related to Ghost types now
5649 -- that the type is frozen.
5651 Check_Ghost_Type (E);
5653 -- We used to check here that a full type must have preelaborable
5654 -- initialization if it completes a private type specified with
5655 -- pragma Preelaborable_Initialization, but that missed cases where
5656 -- the types occur within a generic package, since the freezing
5657 -- that occurs within a containing scope generally skips traversal
5658 -- of a generic unit's declarations (those will be frozen within
5659 -- instances). This check was moved to Analyze_Package_Specification.
5661 -- The type may be defined in a generic unit. This can occur when
5662 -- freezing a generic function that returns the type (which is
5663 -- defined in a parent unit). It is clearly meaningless to freeze
5664 -- this type. However, if it is a subtype, its size may be determi-
5665 -- nable and used in subsequent checks, so might as well try to
5666 -- compute it.
5668 -- In Ada 2012, Freeze_Entities is also used in the front end to
5669 -- trigger the analysis of aspect expressions, so in this case we
5670 -- want to continue the freezing process.
5672 -- Is_Generic_Unit (Scope (E)) is dubious here, do we want instead
5673 -- In_Generic_Scope (E)???
5675 if Present (Scope (E))
5676 and then Is_Generic_Unit (Scope (E))
5677 and then
5678 (not Has_Predicates (E)
5679 and then not Has_Delayed_Freeze (E))
5680 then
5681 Check_Compile_Time_Size (E);
5682 Result := No_List;
5683 goto Leave;
5684 end if;
5686 -- Check for error of Type_Invariant'Class applied to an untagged
5687 -- type (check delayed to freeze time when full type is available).
5689 declare
5690 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
5691 begin
5692 if Present (Prag)
5693 and then Class_Present (Prag)
5694 and then not Is_Tagged_Type (E)
5695 then
5696 Error_Msg_NE
5697 ("Type_Invariant''Class cannot be specified for &", Prag, E);
5698 Error_Msg_N
5699 ("\can only be specified for a tagged type", Prag);
5700 end if;
5701 end;
5703 -- Deal with special cases of freezing for subtype
5705 if E /= Base_Type (E) then
5707 -- Before we do anything else, a specific test for the case of a
5708 -- size given for an array where the array would need to be packed
5709 -- in order for the size to be honored, but is not. This is the
5710 -- case where implicit packing may apply. The reason we do this so
5711 -- early is that, if we have implicit packing, the layout of the
5712 -- base type is affected, so we must do this before we freeze the
5713 -- base type.
5715 -- We could do this processing only if implicit packing is enabled
5716 -- since in all other cases, the error would be caught by the back
5717 -- end. However, we choose to do the check even if we do not have
5718 -- implicit packing enabled, since this allows us to give a more
5719 -- useful error message (advising use of pragma Implicit_Packing
5720 -- or pragma Pack).
5722 if Is_Array_Type (E) then
5723 declare
5724 Ctyp : constant Entity_Id := Component_Type (E);
5725 Rsiz : constant Uint := RM_Size (Ctyp);
5726 SZ : constant Node_Id := Size_Clause (E);
5727 Btyp : constant Entity_Id := Base_Type (E);
5729 Lo : Node_Id;
5730 Hi : Node_Id;
5731 Indx : Node_Id;
5733 Dim : Uint;
5734 Num_Elmts : Uint := Uint_1;
5735 -- Number of elements in array
5737 begin
5738 -- Check enabling conditions. These are straightforward
5739 -- except for the test for a limited composite type. This
5740 -- eliminates the rare case of a array of limited components
5741 -- where there are issues of whether or not we can go ahead
5742 -- and pack the array (since we can't freely pack and unpack
5743 -- arrays if they are limited).
5745 -- Note that we check the root type explicitly because the
5746 -- whole point is we are doing this test before we have had
5747 -- a chance to freeze the base type (and it is that freeze
5748 -- action that causes stuff to be inherited).
5750 -- The conditions on the size are identical to those used in
5751 -- Freeze_Array_Type to set the Is_Packed flag.
5753 if Has_Size_Clause (E)
5754 and then Known_Static_RM_Size (E)
5755 and then not Is_Packed (E)
5756 and then not Has_Pragma_Pack (E)
5757 and then not Has_Component_Size_Clause (E)
5758 and then Known_Static_RM_Size (Ctyp)
5759 and then Rsiz <= 64
5760 and then not (Addressable (Rsiz)
5761 and then Known_Static_Esize (Ctyp)
5762 and then Esize (Ctyp) = Rsiz)
5763 and then not (Rsiz mod System_Storage_Unit = 0
5764 and then Is_Composite_Type (Ctyp))
5765 and then not Is_Limited_Composite (E)
5766 and then not Is_Packed (Root_Type (E))
5767 and then not Has_Component_Size_Clause (Root_Type (E))
5768 and then not (CodePeer_Mode or GNATprove_Mode)
5769 then
5770 -- Compute number of elements in array
5772 Indx := First_Index (E);
5773 while Present (Indx) loop
5774 Get_Index_Bounds (Indx, Lo, Hi);
5776 if not (Compile_Time_Known_Value (Lo)
5777 and then
5778 Compile_Time_Known_Value (Hi))
5779 then
5780 goto No_Implicit_Packing;
5781 end if;
5783 Dim := Expr_Value (Hi) - Expr_Value (Lo) + 1;
5785 if Dim >= 0 then
5786 Num_Elmts := Num_Elmts * Dim;
5787 else
5788 Num_Elmts := Uint_0;
5789 end if;
5791 Next_Index (Indx);
5792 end loop;
5794 -- What we are looking for here is the situation where
5795 -- the RM_Size given would be exactly right if there was
5796 -- a pragma Pack, resulting in the component size being
5797 -- the RM_Size of the component type.
5799 if RM_Size (E) = Num_Elmts * Rsiz then
5801 -- For implicit packing mode, just set the component
5802 -- size and Freeze_Array_Type will do the rest.
5804 if Implicit_Packing then
5805 Set_Component_Size (Btyp, Rsiz);
5807 -- Otherwise give an error message
5809 else
5810 Error_Msg_NE
5811 ("size given for& too small", SZ, E);
5812 Error_Msg_N -- CODEFIX
5813 ("\use explicit pragma Pack or use pragma "
5814 & "Implicit_Packing", SZ);
5815 end if;
5816 end if;
5817 end if;
5818 end;
5819 end if;
5821 <<No_Implicit_Packing>>
5823 -- If ancestor subtype present, freeze that first. Note that this
5824 -- will also get the base type frozen. Need RM reference ???
5826 Atype := Ancestor_Subtype (E);
5828 if Present (Atype) then
5829 Freeze_And_Append (Atype, N, Result);
5831 -- No ancestor subtype present
5833 else
5834 -- See if we have a nearest ancestor that has a predicate.
5835 -- That catches the case of derived type with a predicate.
5836 -- Need RM reference here ???
5838 Atype := Nearest_Ancestor (E);
5840 if Present (Atype) and then Has_Predicates (Atype) then
5841 Freeze_And_Append (Atype, N, Result);
5842 end if;
5844 -- Freeze base type before freezing the entity (RM 13.14(15))
5846 if E /= Base_Type (E) then
5847 Freeze_And_Append (Base_Type (E), N, Result);
5848 end if;
5849 end if;
5851 -- A subtype inherits all the type-related representation aspects
5852 -- from its parents (RM 13.1(8)).
5854 Inherit_Aspects_At_Freeze_Point (E);
5856 -- For a derived type, freeze its parent type first (RM 13.14(15))
5858 elsif Is_Derived_Type (E) then
5859 Freeze_And_Append (Etype (E), N, Result);
5860 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5862 -- A derived type inherits each type-related representation aspect
5863 -- of its parent type that was directly specified before the
5864 -- declaration of the derived type (RM 13.1(15)).
5866 Inherit_Aspects_At_Freeze_Point (E);
5867 end if;
5869 -- Check for incompatible size and alignment for record type
5871 if Warn_On_Size_Alignment
5872 and then Is_Record_Type (E)
5873 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5875 -- If explicit Object_Size clause given assume that the programmer
5876 -- knows what he is doing, and expects the compiler behavior.
5878 and then not Has_Object_Size_Clause (E)
5880 -- Check for size not a multiple of alignment
5882 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5883 then
5884 declare
5885 SC : constant Node_Id := Size_Clause (E);
5886 AC : constant Node_Id := Alignment_Clause (E);
5887 Loc : Node_Id;
5888 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5890 begin
5891 if Present (SC) and then Present (AC) then
5893 -- Give a warning
5895 if Sloc (SC) > Sloc (AC) then
5896 Loc := SC;
5897 Error_Msg_NE
5898 ("?Z?size is not a multiple of alignment for &",
5899 Loc, E);
5900 Error_Msg_Sloc := Sloc (AC);
5901 Error_Msg_Uint_1 := Alignment (E);
5902 Error_Msg_N ("\?Z?alignment of ^ specified #", Loc);
5904 else
5905 Loc := AC;
5906 Error_Msg_NE
5907 ("?Z?size is not a multiple of alignment for &",
5908 Loc, E);
5909 Error_Msg_Sloc := Sloc (SC);
5910 Error_Msg_Uint_1 := RM_Size (E);
5911 Error_Msg_N ("\?Z?size of ^ specified #", Loc);
5912 end if;
5914 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5915 Error_Msg_N ("\?Z?Object_Size will be increased to ^", Loc);
5916 end if;
5917 end;
5918 end if;
5920 -- Array type
5922 if Is_Array_Type (E) then
5923 Freeze_Array_Type (E);
5925 -- For a class-wide type, the corresponding specific type is
5926 -- frozen as well (RM 13.14(15))
5928 elsif Is_Class_Wide_Type (E) then
5929 Freeze_And_Append (Root_Type (E), N, Result);
5931 -- If the base type of the class-wide type is still incomplete,
5932 -- the class-wide remains unfrozen as well. This is legal when
5933 -- E is the formal of a primitive operation of some other type
5934 -- which is being frozen.
5936 if not Is_Frozen (Root_Type (E)) then
5937 Set_Is_Frozen (E, False);
5938 goto Leave;
5939 end if;
5941 -- The equivalent type associated with a class-wide subtype needs
5942 -- to be frozen to ensure that its layout is done.
5944 if Ekind (E) = E_Class_Wide_Subtype
5945 and then Present (Equivalent_Type (E))
5946 then
5947 Freeze_And_Append (Equivalent_Type (E), N, Result);
5948 end if;
5950 -- Generate an itype reference for a library-level class-wide type
5951 -- at the freeze point. Otherwise the first explicit reference to
5952 -- the type may appear in an inner scope which will be rejected by
5953 -- the back-end.
5955 if Is_Itype (E)
5956 and then Is_Compilation_Unit (Scope (E))
5957 then
5958 declare
5959 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5961 begin
5962 Set_Itype (Ref, E);
5964 -- From a gigi point of view, a class-wide subtype derives
5965 -- from its record equivalent type. As a result, the itype
5966 -- reference must appear after the freeze node of the
5967 -- equivalent type or gigi will reject the reference.
5969 if Ekind (E) = E_Class_Wide_Subtype
5970 and then Present (Equivalent_Type (E))
5971 then
5972 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5973 else
5974 Add_To_Result (Ref);
5975 end if;
5976 end;
5977 end if;
5979 -- For a record type or record subtype, freeze all component types
5980 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5981 -- using Is_Record_Type, because we don't want to attempt the freeze
5982 -- for the case of a private type with record extension (we will do
5983 -- that later when the full type is frozen).
5985 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype) then
5986 if not In_Generic_Scope (E) then
5987 Freeze_Record_Type (E);
5988 end if;
5990 -- Report a warning if a discriminated record base type has a
5991 -- convention with language C or C++ applied to it. This check is
5992 -- done even within generic scopes (but not in instantiations),
5993 -- which is why we don't do it as part of Freeze_Record_Type.
5995 Check_Suspicious_Convention (E);
5997 -- For a concurrent type, freeze corresponding record type. This does
5998 -- not correspond to any specific rule in the RM, but the record type
5999 -- is essentially part of the concurrent type. Also freeze all local
6000 -- entities. This includes record types created for entry parameter
6001 -- blocks and whatever local entities may appear in the private part.
6003 elsif Is_Concurrent_Type (E) then
6004 if Present (Corresponding_Record_Type (E)) then
6005 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
6006 end if;
6008 Comp := First_Entity (E);
6009 while Present (Comp) loop
6010 if Is_Type (Comp) then
6011 Freeze_And_Append (Comp, N, Result);
6013 elsif (Ekind (Comp)) /= E_Function then
6015 -- The guard on the presence of the Etype seems to be needed
6016 -- for some CodePeer (-gnatcC) cases, but not clear why???
6018 if Present (Etype (Comp)) then
6019 if Is_Itype (Etype (Comp))
6020 and then Underlying_Type (Scope (Etype (Comp))) = E
6021 then
6022 Undelay_Type (Etype (Comp));
6023 end if;
6025 Freeze_And_Append (Etype (Comp), N, Result);
6026 end if;
6027 end if;
6029 Next_Entity (Comp);
6030 end loop;
6032 -- Private types are required to point to the same freeze node as
6033 -- their corresponding full views. The freeze node itself has to
6034 -- point to the partial view of the entity (because from the partial
6035 -- view, we can retrieve the full view, but not the reverse).
6036 -- However, in order to freeze correctly, we need to freeze the full
6037 -- view. If we are freezing at the end of a scope (or within the
6038 -- scope) of the private type, the partial and full views will have
6039 -- been swapped, the full view appears first in the entity chain and
6040 -- the swapping mechanism ensures that the pointers are properly set
6041 -- (on scope exit).
6043 -- If we encounter the partial view before the full view (e.g. when
6044 -- freezing from another scope), we freeze the full view, and then
6045 -- set the pointers appropriately since we cannot rely on swapping to
6046 -- fix things up (subtypes in an outer scope might not get swapped).
6048 -- If the full view is itself private, the above requirements apply
6049 -- to the underlying full view instead of the full view. But there is
6050 -- no swapping mechanism for the underlying full view so we need to
6051 -- set the pointers appropriately in both cases.
6053 elsif Is_Incomplete_Or_Private_Type (E)
6054 and then not Is_Generic_Type (E)
6055 then
6056 -- The construction of the dispatch table associated with library
6057 -- level tagged types forces freezing of all the primitives of the
6058 -- type, which may cause premature freezing of the partial view.
6059 -- For example:
6061 -- package Pkg is
6062 -- type T is tagged private;
6063 -- type DT is new T with private;
6064 -- procedure Prim (X : in out T; Y : in out DT'Class);
6065 -- private
6066 -- type T is tagged null record;
6067 -- Obj : T;
6068 -- type DT is new T with null record;
6069 -- end;
6071 -- In this case the type will be frozen later by the usual
6072 -- mechanism: an object declaration, an instantiation, or the
6073 -- end of a declarative part.
6075 if Is_Library_Level_Tagged_Type (E)
6076 and then not Present (Full_View (E))
6077 then
6078 Set_Is_Frozen (E, False);
6079 goto Leave;
6081 -- Case of full view present
6083 elsif Present (Full_View (E)) then
6085 -- If full view has already been frozen, then no further
6086 -- processing is required
6088 if Is_Frozen (Full_View (E)) then
6089 Set_Has_Delayed_Freeze (E, False);
6090 Set_Freeze_Node (E, Empty);
6092 -- Otherwise freeze full view and patch the pointers so that
6093 -- the freeze node will elaborate both views in the back end.
6094 -- However, if full view is itself private, freeze underlying
6095 -- full view instead and patch the pointers so that the freeze
6096 -- node will elaborate the three views in the back end.
6098 else
6099 declare
6100 Full : Entity_Id := Full_View (E);
6102 begin
6103 if Is_Private_Type (Full)
6104 and then Present (Underlying_Full_View (Full))
6105 then
6106 Full := Underlying_Full_View (Full);
6107 end if;
6109 Freeze_And_Append (Full, N, Result);
6111 if Full /= Full_View (E)
6112 and then Has_Delayed_Freeze (Full_View (E))
6113 then
6114 F_Node := Freeze_Node (Full);
6116 if Present (F_Node) then
6117 Inherit_Freeze_Node
6118 (Fnod => F_Node,
6119 Typ => Full_View (E));
6120 else
6121 Set_Has_Delayed_Freeze (Full_View (E), False);
6122 Set_Freeze_Node (Full_View (E), Empty);
6123 end if;
6124 end if;
6126 if Has_Delayed_Freeze (E) then
6127 F_Node := Freeze_Node (Full_View (E));
6129 if Present (F_Node) then
6130 Inherit_Freeze_Node
6131 (Fnod => F_Node,
6132 Typ => E);
6133 else
6134 -- {Incomplete,Private}_Subtypes with Full_Views
6135 -- constrained by discriminants.
6137 Set_Has_Delayed_Freeze (E, False);
6138 Set_Freeze_Node (E, Empty);
6139 end if;
6140 end if;
6141 end;
6142 end if;
6144 Check_Debug_Info_Needed (E);
6146 -- AI-117 requires that the convention of a partial view be the
6147 -- same as the convention of the full view. Note that this is a
6148 -- recognized breach of privacy, but it's essential for logical
6149 -- consistency of representation, and the lack of a rule in
6150 -- RM95 was an oversight.
6152 Set_Convention (E, Convention (Full_View (E)));
6154 Set_Size_Known_At_Compile_Time (E,
6155 Size_Known_At_Compile_Time (Full_View (E)));
6157 -- Size information is copied from the full view to the
6158 -- incomplete or private view for consistency.
6160 -- We skip this is the full view is not a type. This is very
6161 -- strange of course, and can only happen as a result of
6162 -- certain illegalities, such as a premature attempt to derive
6163 -- from an incomplete type.
6165 if Is_Type (Full_View (E)) then
6166 Set_Size_Info (E, Full_View (E));
6167 Set_RM_Size (E, RM_Size (Full_View (E)));
6168 end if;
6170 goto Leave;
6172 -- Case of underlying full view present
6174 elsif Is_Private_Type (E)
6175 and then Present (Underlying_Full_View (E))
6176 then
6177 if not Is_Frozen (Underlying_Full_View (E)) then
6178 Freeze_And_Append (Underlying_Full_View (E), N, Result);
6179 end if;
6181 -- Patch the pointers so that the freeze node will elaborate
6182 -- both views in the back end.
6184 if Has_Delayed_Freeze (E) then
6185 F_Node := Freeze_Node (Underlying_Full_View (E));
6187 if Present (F_Node) then
6188 Inherit_Freeze_Node
6189 (Fnod => F_Node,
6190 Typ => E);
6191 else
6192 Set_Has_Delayed_Freeze (E, False);
6193 Set_Freeze_Node (E, Empty);
6194 end if;
6195 end if;
6197 Check_Debug_Info_Needed (E);
6199 goto Leave;
6201 -- Case of no full view present. If entity is derived or subtype,
6202 -- it is safe to freeze, correctness depends on the frozen status
6203 -- of parent. Otherwise it is either premature usage, or a Taft
6204 -- amendment type, so diagnosis is at the point of use and the
6205 -- type might be frozen later.
6207 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
6208 null;
6210 else
6211 Set_Is_Frozen (E, False);
6212 Result := No_List;
6213 goto Leave;
6214 end if;
6216 -- For access subprogram, freeze types of all formals, the return
6217 -- type was already frozen, since it is the Etype of the function.
6218 -- Formal types can be tagged Taft amendment types, but otherwise
6219 -- they cannot be incomplete.
6221 elsif Ekind (E) = E_Subprogram_Type then
6222 Formal := First_Formal (E);
6223 while Present (Formal) loop
6224 if Ekind (Etype (Formal)) = E_Incomplete_Type
6225 and then No (Full_View (Etype (Formal)))
6226 then
6227 if Is_Tagged_Type (Etype (Formal)) then
6228 null;
6230 -- AI05-151: Incomplete types are allowed in access to
6231 -- subprogram specifications.
6233 elsif Ada_Version < Ada_2012 then
6234 Error_Msg_NE
6235 ("invalid use of incomplete type&", E, Etype (Formal));
6236 end if;
6237 end if;
6239 Freeze_And_Append (Etype (Formal), N, Result);
6240 Next_Formal (Formal);
6241 end loop;
6243 Freeze_Subprogram (E);
6245 -- For access to a protected subprogram, freeze the equivalent type
6246 -- (however this is not set if we are not generating code or if this
6247 -- is an anonymous type used just for resolution).
6249 elsif Is_Access_Protected_Subprogram_Type (E) then
6250 if Present (Equivalent_Type (E)) then
6251 Freeze_And_Append (Equivalent_Type (E), N, Result);
6252 end if;
6253 end if;
6255 -- Generic types are never seen by the back-end, and are also not
6256 -- processed by the expander (since the expander is turned off for
6257 -- generic processing), so we never need freeze nodes for them.
6259 if Is_Generic_Type (E) then
6260 goto Leave;
6261 end if;
6263 -- Some special processing for non-generic types to complete
6264 -- representation details not known till the freeze point.
6266 if Is_Fixed_Point_Type (E) then
6267 Freeze_Fixed_Point_Type (E);
6269 -- Some error checks required for ordinary fixed-point type. Defer
6270 -- these till the freeze-point since we need the small and range
6271 -- values. We only do these checks for base types
6273 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
6274 if Small_Value (E) < Ureal_2_M_80 then
6275 Error_Msg_Name_1 := Name_Small;
6276 Error_Msg_N
6277 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
6279 elsif Small_Value (E) > Ureal_2_80 then
6280 Error_Msg_Name_1 := Name_Small;
6281 Error_Msg_N
6282 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
6283 end if;
6285 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
6286 Error_Msg_Name_1 := Name_First;
6287 Error_Msg_N
6288 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
6289 end if;
6291 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
6292 Error_Msg_Name_1 := Name_Last;
6293 Error_Msg_N
6294 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
6295 end if;
6296 end if;
6298 elsif Is_Enumeration_Type (E) then
6299 Freeze_Enumeration_Type (E);
6301 elsif Is_Integer_Type (E) then
6302 Adjust_Esize_For_Alignment (E);
6304 if Is_Modular_Integer_Type (E)
6305 and then Warn_On_Suspicious_Modulus_Value
6306 then
6307 Check_Suspicious_Modulus (E);
6308 end if;
6310 -- The pool applies to named and anonymous access types, but not
6311 -- to subprogram and to internal types generated for 'Access
6312 -- references.
6314 elsif Is_Access_Type (E)
6315 and then not Is_Access_Subprogram_Type (E)
6316 and then Ekind (E) /= E_Access_Attribute_Type
6317 then
6318 -- If a pragma Default_Storage_Pool applies, and this type has no
6319 -- Storage_Pool or Storage_Size clause (which must have occurred
6320 -- before the freezing point), then use the default. This applies
6321 -- only to base types.
6323 -- None of this applies to access to subprograms, for which there
6324 -- are clearly no pools.
6326 if Present (Default_Pool)
6327 and then Is_Base_Type (E)
6328 and then not Has_Storage_Size_Clause (E)
6329 and then No (Associated_Storage_Pool (E))
6330 then
6331 -- Case of pragma Default_Storage_Pool (null)
6333 if Nkind (Default_Pool) = N_Null then
6334 Set_No_Pool_Assigned (E);
6336 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
6338 else
6339 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
6340 end if;
6341 end if;
6343 -- Check restriction for standard storage pool
6345 if No (Associated_Storage_Pool (E)) then
6346 Check_Restriction (No_Standard_Storage_Pools, E);
6347 end if;
6349 -- Deal with error message for pure access type. This is not an
6350 -- error in Ada 2005 if there is no pool (see AI-366).
6352 if Is_Pure_Unit_Access_Type (E)
6353 and then (Ada_Version < Ada_2005
6354 or else not No_Pool_Assigned (E))
6355 and then not Is_Generic_Unit (Scope (E))
6356 then
6357 Error_Msg_N ("named access type not allowed in pure unit", E);
6359 if Ada_Version >= Ada_2005 then
6360 Error_Msg_N
6361 ("\would be legal if Storage_Size of 0 given??", E);
6363 elsif No_Pool_Assigned (E) then
6364 Error_Msg_N
6365 ("\would be legal in Ada 2005??", E);
6367 else
6368 Error_Msg_N
6369 ("\would be legal in Ada 2005 if "
6370 & "Storage_Size of 0 given??", E);
6371 end if;
6372 end if;
6373 end if;
6375 -- Case of composite types
6377 if Is_Composite_Type (E) then
6379 -- AI-117 requires that all new primitives of a tagged type must
6380 -- inherit the convention of the full view of the type. Inherited
6381 -- and overriding operations are defined to inherit the convention
6382 -- of their parent or overridden subprogram (also specified in
6383 -- AI-117), which will have occurred earlier (in Derive_Subprogram
6384 -- and New_Overloaded_Entity). Here we set the convention of
6385 -- primitives that are still convention Ada, which will ensure
6386 -- that any new primitives inherit the type's convention. Class-
6387 -- wide types can have a foreign convention inherited from their
6388 -- specific type, but are excluded from this since they don't have
6389 -- any associated primitives.
6391 if Is_Tagged_Type (E)
6392 and then not Is_Class_Wide_Type (E)
6393 and then Convention (E) /= Convention_Ada
6394 then
6395 declare
6396 Prim_List : constant Elist_Id := Primitive_Operations (E);
6397 Prim : Elmt_Id;
6399 begin
6400 Prim := First_Elmt (Prim_List);
6401 while Present (Prim) loop
6402 if Convention (Node (Prim)) = Convention_Ada then
6403 Set_Convention (Node (Prim), Convention (E));
6404 end if;
6406 Next_Elmt (Prim);
6407 end loop;
6408 end;
6409 end if;
6411 -- If the type is a simple storage pool type, then this is where
6412 -- we attempt to locate and validate its Allocate, Deallocate, and
6413 -- Storage_Size operations (the first is required, and the latter
6414 -- two are optional). We also verify that the full type for a
6415 -- private type is allowed to be a simple storage pool type.
6417 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
6418 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
6419 then
6420 -- If the type is marked Has_Private_Declaration, then this is
6421 -- a full type for a private type that was specified with the
6422 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
6423 -- pragma is allowed for the full type (for example, it can't
6424 -- be an array type, or a nonlimited record type).
6426 if Has_Private_Declaration (E) then
6427 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
6428 and then not Is_Private_Type (E)
6429 then
6430 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
6431 Error_Msg_N
6432 ("pragma% can only apply to full type that is an " &
6433 "explicitly limited type", E);
6434 end if;
6435 end if;
6437 Validate_Simple_Pool_Ops : declare
6438 Pool_Type : Entity_Id renames E;
6439 Address_Type : constant Entity_Id := RTE (RE_Address);
6440 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
6442 procedure Validate_Simple_Pool_Op_Formal
6443 (Pool_Op : Entity_Id;
6444 Pool_Op_Formal : in out Entity_Id;
6445 Expected_Mode : Formal_Kind;
6446 Expected_Type : Entity_Id;
6447 Formal_Name : String;
6448 OK_Formal : in out Boolean);
6449 -- Validate one formal Pool_Op_Formal of the candidate pool
6450 -- operation Pool_Op. The formal must be of Expected_Type
6451 -- and have mode Expected_Mode. OK_Formal will be set to
6452 -- False if the formal doesn't match. If OK_Formal is False
6453 -- on entry, then the formal will effectively be ignored
6454 -- (because validation of the pool op has already failed).
6455 -- Upon return, Pool_Op_Formal will be updated to the next
6456 -- formal, if any.
6458 procedure Validate_Simple_Pool_Operation
6459 (Op_Name : Name_Id);
6460 -- Search for and validate a simple pool operation with the
6461 -- name Op_Name. If the name is Allocate, then there must be
6462 -- exactly one such primitive operation for the simple pool
6463 -- type. If the name is Deallocate or Storage_Size, then
6464 -- there can be at most one such primitive operation. The
6465 -- profile of the located primitive must conform to what
6466 -- is expected for each operation.
6468 ------------------------------------
6469 -- Validate_Simple_Pool_Op_Formal --
6470 ------------------------------------
6472 procedure Validate_Simple_Pool_Op_Formal
6473 (Pool_Op : Entity_Id;
6474 Pool_Op_Formal : in out Entity_Id;
6475 Expected_Mode : Formal_Kind;
6476 Expected_Type : Entity_Id;
6477 Formal_Name : String;
6478 OK_Formal : in out Boolean)
6480 begin
6481 -- If OK_Formal is False on entry, then simply ignore
6482 -- the formal, because an earlier formal has already
6483 -- been flagged.
6485 if not OK_Formal then
6486 return;
6488 -- If no formal is passed in, then issue an error for a
6489 -- missing formal.
6491 elsif not Present (Pool_Op_Formal) then
6492 Error_Msg_NE
6493 ("simple storage pool op missing formal " &
6494 Formal_Name & " of type&", Pool_Op, Expected_Type);
6495 OK_Formal := False;
6497 return;
6498 end if;
6500 if Etype (Pool_Op_Formal) /= Expected_Type then
6502 -- If the pool type was expected for this formal, then
6503 -- this will not be considered a candidate operation
6504 -- for the simple pool, so we unset OK_Formal so that
6505 -- the op and any later formals will be ignored.
6507 if Expected_Type = Pool_Type then
6508 OK_Formal := False;
6510 return;
6512 else
6513 Error_Msg_NE
6514 ("wrong type for formal " & Formal_Name &
6515 " of simple storage pool op; expected type&",
6516 Pool_Op_Formal, Expected_Type);
6517 end if;
6518 end if;
6520 -- Issue error if formal's mode is not the expected one
6522 if Ekind (Pool_Op_Formal) /= Expected_Mode then
6523 Error_Msg_N
6524 ("wrong mode for formal of simple storage pool op",
6525 Pool_Op_Formal);
6526 end if;
6528 -- Advance to the next formal
6530 Next_Formal (Pool_Op_Formal);
6531 end Validate_Simple_Pool_Op_Formal;
6533 ------------------------------------
6534 -- Validate_Simple_Pool_Operation --
6535 ------------------------------------
6537 procedure Validate_Simple_Pool_Operation
6538 (Op_Name : Name_Id)
6540 Op : Entity_Id;
6541 Found_Op : Entity_Id := Empty;
6542 Formal : Entity_Id;
6543 Is_OK : Boolean;
6545 begin
6546 pragma Assert
6547 (Nam_In (Op_Name, Name_Allocate,
6548 Name_Deallocate,
6549 Name_Storage_Size));
6551 Error_Msg_Name_1 := Op_Name;
6553 -- For each homonym declared immediately in the scope
6554 -- of the simple storage pool type, determine whether
6555 -- the homonym is an operation of the pool type, and,
6556 -- if so, check that its profile is as expected for
6557 -- a simple pool operation of that name.
6559 Op := Get_Name_Entity_Id (Op_Name);
6560 while Present (Op) loop
6561 if Ekind_In (Op, E_Function, E_Procedure)
6562 and then Scope (Op) = Current_Scope
6563 then
6564 Formal := First_Entity (Op);
6566 Is_OK := True;
6568 -- The first parameter must be of the pool type
6569 -- in order for the operation to qualify.
6571 if Op_Name = Name_Storage_Size then
6572 Validate_Simple_Pool_Op_Formal
6573 (Op, Formal, E_In_Parameter, Pool_Type,
6574 "Pool", Is_OK);
6575 else
6576 Validate_Simple_Pool_Op_Formal
6577 (Op, Formal, E_In_Out_Parameter, Pool_Type,
6578 "Pool", Is_OK);
6579 end if;
6581 -- If another operation with this name has already
6582 -- been located for the type, then flag an error,
6583 -- since we only allow the type to have a single
6584 -- such primitive.
6586 if Present (Found_Op) and then Is_OK then
6587 Error_Msg_NE
6588 ("only one % operation allowed for " &
6589 "simple storage pool type&", Op, Pool_Type);
6590 end if;
6592 -- In the case of Allocate and Deallocate, a formal
6593 -- of type System.Address is required.
6595 if Op_Name = Name_Allocate then
6596 Validate_Simple_Pool_Op_Formal
6597 (Op, Formal, E_Out_Parameter,
6598 Address_Type, "Storage_Address", Is_OK);
6600 elsif Op_Name = Name_Deallocate then
6601 Validate_Simple_Pool_Op_Formal
6602 (Op, Formal, E_In_Parameter,
6603 Address_Type, "Storage_Address", Is_OK);
6604 end if;
6606 -- In the case of Allocate and Deallocate, formals
6607 -- of type Storage_Count are required as the third
6608 -- and fourth parameters.
6610 if Op_Name /= Name_Storage_Size then
6611 Validate_Simple_Pool_Op_Formal
6612 (Op, Formal, E_In_Parameter,
6613 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
6614 Validate_Simple_Pool_Op_Formal
6615 (Op, Formal, E_In_Parameter,
6616 Stg_Cnt_Type, "Alignment", Is_OK);
6617 end if;
6619 -- If no mismatched formals have been found (Is_OK)
6620 -- and no excess formals are present, then this
6621 -- operation has been validated, so record it.
6623 if not Present (Formal) and then Is_OK then
6624 Found_Op := Op;
6625 end if;
6626 end if;
6628 Op := Homonym (Op);
6629 end loop;
6631 -- There must be a valid Allocate operation for the type,
6632 -- so issue an error if none was found.
6634 if Op_Name = Name_Allocate
6635 and then not Present (Found_Op)
6636 then
6637 Error_Msg_N ("missing % operation for simple " &
6638 "storage pool type", Pool_Type);
6640 elsif Present (Found_Op) then
6642 -- Simple pool operations can't be abstract
6644 if Is_Abstract_Subprogram (Found_Op) then
6645 Error_Msg_N
6646 ("simple storage pool operation must not be " &
6647 "abstract", Found_Op);
6648 end if;
6650 -- The Storage_Size operation must be a function with
6651 -- Storage_Count as its result type.
6653 if Op_Name = Name_Storage_Size then
6654 if Ekind (Found_Op) = E_Procedure then
6655 Error_Msg_N
6656 ("% operation must be a function", Found_Op);
6658 elsif Etype (Found_Op) /= Stg_Cnt_Type then
6659 Error_Msg_NE
6660 ("wrong result type for%, expected type&",
6661 Found_Op, Stg_Cnt_Type);
6662 end if;
6664 -- Allocate and Deallocate must be procedures
6666 elsif Ekind (Found_Op) = E_Function then
6667 Error_Msg_N
6668 ("% operation must be a procedure", Found_Op);
6669 end if;
6670 end if;
6671 end Validate_Simple_Pool_Operation;
6673 -- Start of processing for Validate_Simple_Pool_Ops
6675 begin
6676 Validate_Simple_Pool_Operation (Name_Allocate);
6677 Validate_Simple_Pool_Operation (Name_Deallocate);
6678 Validate_Simple_Pool_Operation (Name_Storage_Size);
6679 end Validate_Simple_Pool_Ops;
6680 end if;
6681 end if;
6683 -- Now that all types from which E may depend are frozen, see if the
6684 -- size is known at compile time, if it must be unsigned, or if
6685 -- strict alignment is required
6687 Check_Compile_Time_Size (E);
6688 Check_Unsigned_Type (E);
6690 if Base_Type (E) = E then
6691 Check_Strict_Alignment (E);
6692 end if;
6694 -- Do not allow a size clause for a type which does not have a size
6695 -- that is known at compile time
6697 if Has_Size_Clause (E)
6698 and then not Size_Known_At_Compile_Time (E)
6699 then
6700 -- Suppress this message if errors posted on E, even if we are
6701 -- in all errors mode, since this is often a junk message
6703 if not Error_Posted (E) then
6704 Error_Msg_N
6705 ("size clause not allowed for variable length type",
6706 Size_Clause (E));
6707 end if;
6708 end if;
6710 -- Now we set/verify the representation information, in particular
6711 -- the size and alignment values. This processing is not required for
6712 -- generic types, since generic types do not play any part in code
6713 -- generation, and so the size and alignment values for such types
6714 -- are irrelevant. Ditto for types declared within a generic unit,
6715 -- which may have components that depend on generic parameters, and
6716 -- that will be recreated in an instance.
6718 if Inside_A_Generic then
6719 null;
6721 -- Otherwise we call the layout procedure
6723 else
6724 Layout_Type (E);
6725 end if;
6727 -- If this is an access to subprogram whose designated type is itself
6728 -- a subprogram type, the return type of this anonymous subprogram
6729 -- type must be decorated as well.
6731 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
6732 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
6733 then
6734 Layout_Type (Etype (Designated_Type (E)));
6735 end if;
6737 -- If the type has a Defaut_Value/Default_Component_Value aspect,
6738 -- this is where we analye the expression (after the type is frozen,
6739 -- since in the case of Default_Value, we are analyzing with the
6740 -- type itself, and we treat Default_Component_Value similarly for
6741 -- the sake of uniformity).
6743 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
6744 declare
6745 Nam : Name_Id;
6746 Exp : Node_Id;
6747 Typ : Entity_Id;
6749 begin
6750 if Is_Scalar_Type (E) then
6751 Nam := Name_Default_Value;
6752 Typ := E;
6753 Exp := Default_Aspect_Value (Typ);
6754 else
6755 Nam := Name_Default_Component_Value;
6756 Typ := Component_Type (E);
6757 Exp := Default_Aspect_Component_Value (E);
6758 end if;
6760 Analyze_And_Resolve (Exp, Typ);
6762 if Etype (Exp) /= Any_Type then
6763 if not Is_OK_Static_Expression (Exp) then
6764 Error_Msg_Name_1 := Nam;
6765 Flag_Non_Static_Expr
6766 ("aspect% requires static expression", Exp);
6767 end if;
6768 end if;
6769 end;
6770 end if;
6772 -- End of freeze processing for type entities
6773 end if;
6775 -- Here is where we logically freeze the current entity. If it has a
6776 -- freeze node, then this is the point at which the freeze node is
6777 -- linked into the result list.
6779 if Has_Delayed_Freeze (E) then
6781 -- If a freeze node is already allocated, use it, otherwise allocate
6782 -- a new one. The preallocation happens in the case of anonymous base
6783 -- types, where we preallocate so that we can set First_Subtype_Link.
6784 -- Note that we reset the Sloc to the current freeze location.
6786 if Present (Freeze_Node (E)) then
6787 F_Node := Freeze_Node (E);
6788 Set_Sloc (F_Node, Loc);
6790 else
6791 F_Node := New_Node (N_Freeze_Entity, Loc);
6792 Set_Freeze_Node (E, F_Node);
6793 Set_Access_Types_To_Process (F_Node, No_Elist);
6794 Set_TSS_Elist (F_Node, No_Elist);
6795 Set_Actions (F_Node, No_List);
6796 end if;
6798 Set_Entity (F_Node, E);
6799 Add_To_Result (F_Node);
6801 -- A final pass over record types with discriminants. If the type
6802 -- has an incomplete declaration, there may be constrained access
6803 -- subtypes declared elsewhere, which do not depend on the discrimi-
6804 -- nants of the type, and which are used as component types (i.e.
6805 -- the full view is a recursive type). The designated types of these
6806 -- subtypes can only be elaborated after the type itself, and they
6807 -- need an itype reference.
6809 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
6810 declare
6811 Comp : Entity_Id;
6812 IR : Node_Id;
6813 Typ : Entity_Id;
6815 begin
6816 Comp := First_Component (E);
6817 while Present (Comp) loop
6818 Typ := Etype (Comp);
6820 if Ekind (Comp) = E_Component
6821 and then Is_Access_Type (Typ)
6822 and then Scope (Typ) /= E
6823 and then Base_Type (Designated_Type (Typ)) = E
6824 and then Is_Itype (Designated_Type (Typ))
6825 then
6826 IR := Make_Itype_Reference (Sloc (Comp));
6827 Set_Itype (IR, Designated_Type (Typ));
6828 Append (IR, Result);
6829 end if;
6831 Next_Component (Comp);
6832 end loop;
6833 end;
6834 end if;
6835 end if;
6837 -- When a type is frozen, the first subtype of the type is frozen as
6838 -- well (RM 13.14(15)). This has to be done after freezing the type,
6839 -- since obviously the first subtype depends on its own base type.
6841 if Is_Type (E) then
6842 Freeze_And_Append (First_Subtype (E), N, Result);
6844 -- If we just froze a tagged non-class wide record, then freeze the
6845 -- corresponding class-wide type. This must be done after the tagged
6846 -- type itself is frozen, because the class-wide type refers to the
6847 -- tagged type which generates the class.
6849 if Is_Tagged_Type (E)
6850 and then not Is_Class_Wide_Type (E)
6851 and then Present (Class_Wide_Type (E))
6852 then
6853 Freeze_And_Append (Class_Wide_Type (E), N, Result);
6854 end if;
6855 end if;
6857 Check_Debug_Info_Needed (E);
6859 -- Special handling for subprograms
6861 if Is_Subprogram (E) then
6863 -- If subprogram has address clause then reset Is_Public flag, since
6864 -- we do not want the backend to generate external references.
6866 if Present (Address_Clause (E))
6867 and then not Is_Library_Level_Entity (E)
6868 then
6869 Set_Is_Public (E, False);
6870 end if;
6871 end if;
6873 <<Leave>>
6874 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6876 return Result;
6877 end Freeze_Entity;
6879 -----------------------------
6880 -- Freeze_Enumeration_Type --
6881 -----------------------------
6883 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6884 begin
6885 -- By default, if no size clause is present, an enumeration type with
6886 -- Convention C is assumed to interface to a C enum and has integer
6887 -- size, except for a boolean type because it is assumed to interface
6888 -- to _Bool introduced in C99. This applies to types. For subtypes,
6889 -- verify that its base type has no size clause either. Treat other
6890 -- foreign conventions in the same way, and also make sure alignment
6891 -- is set right.
6893 if Has_Foreign_Convention (Typ)
6894 and then not Is_Boolean_Type (Typ)
6895 and then not Has_Size_Clause (Typ)
6896 and then not Has_Size_Clause (Base_Type (Typ))
6897 and then Esize (Typ) < Standard_Integer_Size
6899 -- Don't do this if Short_Enums on target
6901 and then not Target_Short_Enums
6902 then
6903 Init_Esize (Typ, Standard_Integer_Size);
6904 Set_Alignment (Typ, Alignment (Standard_Integer));
6906 -- Normal Ada case or size clause present or not Long_C_Enums on target
6908 else
6909 -- If the enumeration type interfaces to C, and it has a size clause
6910 -- that specifies less than int size, it warrants a warning. The
6911 -- user may intend the C type to be an enum or a char, so this is
6912 -- not by itself an error that the Ada compiler can detect, but it
6913 -- it is a worth a heads-up. For Boolean and Character types we
6914 -- assume that the programmer has the proper C type in mind.
6916 if Convention (Typ) = Convention_C
6917 and then Has_Size_Clause (Typ)
6918 and then Esize (Typ) /= Esize (Standard_Integer)
6919 and then not Is_Boolean_Type (Typ)
6920 and then not Is_Character_Type (Typ)
6922 -- Don't do this if Short_Enums on target
6924 and then not Target_Short_Enums
6925 then
6926 Error_Msg_N
6927 ("C enum types have the size of a C int??", Size_Clause (Typ));
6928 end if;
6930 Adjust_Esize_For_Alignment (Typ);
6931 end if;
6932 end Freeze_Enumeration_Type;
6934 -----------------------
6935 -- Freeze_Expression --
6936 -----------------------
6938 procedure Freeze_Expression (N : Node_Id) is
6939 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6940 Typ : Entity_Id;
6941 Nam : Entity_Id;
6942 Desig_Typ : Entity_Id;
6943 P : Node_Id;
6944 Parent_P : Node_Id;
6946 Freeze_Outside : Boolean := False;
6947 -- This flag is set true if the entity must be frozen outside the
6948 -- current subprogram. This happens in the case of expander generated
6949 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6950 -- not freeze all entities like other bodies, but which nevertheless
6951 -- may reference entities that have to be frozen before the body and
6952 -- obviously cannot be frozen inside the body.
6954 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6955 -- If the expression is an array aggregate, the type of the component
6956 -- expressions is also frozen. If the component type is an access type
6957 -- and the expressions include allocators, the designed type is frozen
6958 -- as well.
6960 function In_Expanded_Body (N : Node_Id) return Boolean;
6961 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6962 -- it is the handled statement sequence of an expander-generated
6963 -- subprogram (init proc, stream subprogram, or renaming as body).
6964 -- If so, this is not a freezing context.
6966 -----------------------------------------
6967 -- Find_Aggregate_Component_Desig_Type --
6968 -----------------------------------------
6970 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6971 Assoc : Node_Id;
6972 Exp : Node_Id;
6974 begin
6975 if Present (Expressions (N)) then
6976 Exp := First (Expressions (N));
6977 while Present (Exp) loop
6978 if Nkind (Exp) = N_Allocator then
6979 return Designated_Type (Component_Type (Etype (N)));
6980 end if;
6982 Next (Exp);
6983 end loop;
6984 end if;
6986 if Present (Component_Associations (N)) then
6987 Assoc := First (Component_Associations (N));
6988 while Present (Assoc) loop
6989 if Nkind (Expression (Assoc)) = N_Allocator then
6990 return Designated_Type (Component_Type (Etype (N)));
6991 end if;
6993 Next (Assoc);
6994 end loop;
6995 end if;
6997 return Empty;
6998 end Find_Aggregate_Component_Desig_Type;
7000 ----------------------
7001 -- In_Expanded_Body --
7002 ----------------------
7004 function In_Expanded_Body (N : Node_Id) return Boolean is
7005 P : Node_Id;
7006 Id : Entity_Id;
7008 begin
7009 if Nkind (N) = N_Subprogram_Body then
7010 P := N;
7011 else
7012 P := Parent (N);
7013 end if;
7015 if Nkind (P) /= N_Subprogram_Body then
7016 return False;
7018 else
7019 Id := Defining_Unit_Name (Specification (P));
7021 -- The following are expander-created bodies, or bodies that
7022 -- are not freeze points.
7024 if Nkind (Id) = N_Defining_Identifier
7025 and then (Is_Init_Proc (Id)
7026 or else Is_TSS (Id, TSS_Stream_Input)
7027 or else Is_TSS (Id, TSS_Stream_Output)
7028 or else Is_TSS (Id, TSS_Stream_Read)
7029 or else Is_TSS (Id, TSS_Stream_Write)
7030 or else Nkind_In (Original_Node (P),
7031 N_Subprogram_Renaming_Declaration,
7032 N_Expression_Function))
7033 then
7034 return True;
7035 else
7036 return False;
7037 end if;
7038 end if;
7039 end In_Expanded_Body;
7041 -- Start of processing for Freeze_Expression
7043 begin
7044 -- Immediate return if freezing is inhibited. This flag is set by the
7045 -- analyzer to stop freezing on generated expressions that would cause
7046 -- freezing if they were in the source program, but which are not
7047 -- supposed to freeze, since they are created.
7049 if Must_Not_Freeze (N) then
7050 return;
7051 end if;
7053 -- If expression is non-static, then it does not freeze in a default
7054 -- expression, see section "Handling of Default Expressions" in the
7055 -- spec of package Sem for further details. Note that we have to make
7056 -- sure that we actually have a real expression (if we have a subtype
7057 -- indication, we can't test Is_OK_Static_Expression). However, we
7058 -- exclude the case of the prefix of an attribute of a static scalar
7059 -- subtype from this early return, because static subtype attributes
7060 -- should always cause freezing, even in default expressions, but
7061 -- the attribute may not have been marked as static yet (because in
7062 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
7063 -- Freeze_Expression on the prefix).
7065 if In_Spec_Exp
7066 and then Nkind (N) in N_Subexpr
7067 and then not Is_OK_Static_Expression (N)
7068 and then (Nkind (Parent (N)) /= N_Attribute_Reference
7069 or else not (Is_Entity_Name (N)
7070 and then Is_Type (Entity (N))
7071 and then Is_OK_Static_Subtype (Entity (N))))
7072 then
7073 return;
7074 end if;
7076 -- Freeze type of expression if not frozen already
7078 Typ := Empty;
7080 if Nkind (N) in N_Has_Etype then
7081 if not Is_Frozen (Etype (N)) then
7082 Typ := Etype (N);
7084 -- Base type may be an derived numeric type that is frozen at
7085 -- the point of declaration, but first_subtype is still unfrozen.
7087 elsif not Is_Frozen (First_Subtype (Etype (N))) then
7088 Typ := First_Subtype (Etype (N));
7089 end if;
7090 end if;
7092 -- For entity name, freeze entity if not frozen already. A special
7093 -- exception occurs for an identifier that did not come from source.
7094 -- We don't let such identifiers freeze a non-internal entity, i.e.
7095 -- an entity that did come from source, since such an identifier was
7096 -- generated by the expander, and cannot have any semantic effect on
7097 -- the freezing semantics. For example, this stops the parameter of
7098 -- an initialization procedure from freezing the variable.
7100 if Is_Entity_Name (N)
7101 and then not Is_Frozen (Entity (N))
7102 and then (Nkind (N) /= N_Identifier
7103 or else Comes_From_Source (N)
7104 or else not Comes_From_Source (Entity (N)))
7105 then
7106 Nam := Entity (N);
7108 if Present (Nam) and then Ekind (Nam) = E_Function then
7109 Check_Expression_Function (N, Nam);
7110 end if;
7112 else
7113 Nam := Empty;
7114 end if;
7116 -- For an allocator freeze designated type if not frozen already
7118 -- For an aggregate whose component type is an access type, freeze the
7119 -- designated type now, so that its freeze does not appear within the
7120 -- loop that might be created in the expansion of the aggregate. If the
7121 -- designated type is a private type without full view, the expression
7122 -- cannot contain an allocator, so the type is not frozen.
7124 -- For a function, we freeze the entity when the subprogram declaration
7125 -- is frozen, but a function call may appear in an initialization proc.
7126 -- before the declaration is frozen. We need to generate the extra
7127 -- formals, if any, to ensure that the expansion of the call includes
7128 -- the proper actuals. This only applies to Ada subprograms, not to
7129 -- imported ones.
7131 Desig_Typ := Empty;
7133 case Nkind (N) is
7134 when N_Allocator =>
7135 Desig_Typ := Designated_Type (Etype (N));
7137 when N_Aggregate =>
7138 if Is_Array_Type (Etype (N))
7139 and then Is_Access_Type (Component_Type (Etype (N)))
7140 then
7142 -- Check whether aggregate includes allocators.
7144 Desig_Typ := Find_Aggregate_Component_Desig_Type;
7145 end if;
7147 when N_Indexed_Component
7148 | N_Selected_Component
7149 | N_Slice
7151 if Is_Access_Type (Etype (Prefix (N))) then
7152 Desig_Typ := Designated_Type (Etype (Prefix (N)));
7153 end if;
7155 when N_Identifier =>
7156 if Present (Nam)
7157 and then Ekind (Nam) = E_Function
7158 and then Nkind (Parent (N)) = N_Function_Call
7159 and then Convention (Nam) = Convention_Ada
7160 then
7161 Create_Extra_Formals (Nam);
7162 end if;
7164 when others =>
7165 null;
7166 end case;
7168 if Desig_Typ /= Empty
7169 and then (Is_Frozen (Desig_Typ)
7170 or else (not Is_Fully_Defined (Desig_Typ)))
7171 then
7172 Desig_Typ := Empty;
7173 end if;
7175 -- All done if nothing needs freezing
7177 if No (Typ)
7178 and then No (Nam)
7179 and then No (Desig_Typ)
7180 then
7181 return;
7182 end if;
7184 -- Examine the enclosing context by climbing the parent chain. The
7185 -- traversal serves two purposes - to detect scenarios where freezeing
7186 -- is not needed and to find the proper insertion point for the freeze
7187 -- nodes. Although somewhat similar to Insert_Actions, this traversal
7188 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
7189 -- the tree may result in types being frozen too early.
7191 P := N;
7192 loop
7193 Parent_P := Parent (P);
7195 -- If we don't have a parent, then we are not in a well-formed tree.
7196 -- This is an unusual case, but there are some legitimate situations
7197 -- in which this occurs, notably when the expressions in the range of
7198 -- a type declaration are resolved. We simply ignore the freeze
7199 -- request in this case. Is this right ???
7201 if No (Parent_P) then
7202 return;
7203 end if;
7205 -- See if we have got to an appropriate point in the tree
7207 case Nkind (Parent_P) is
7209 -- A special test for the exception of (RM 13.14(8)) for the case
7210 -- of per-object expressions (RM 3.8(18)) occurring in component
7211 -- definition or a discrete subtype definition. Note that we test
7212 -- for a component declaration which includes both cases we are
7213 -- interested in, and furthermore the tree does not have explicit
7214 -- nodes for either of these two constructs.
7216 when N_Component_Declaration =>
7218 -- The case we want to test for here is an identifier that is
7219 -- a per-object expression, this is either a discriminant that
7220 -- appears in a context other than the component declaration
7221 -- or it is a reference to the type of the enclosing construct.
7223 -- For either of these cases, we skip the freezing
7225 if not In_Spec_Expression
7226 and then Nkind (N) = N_Identifier
7227 and then (Present (Entity (N)))
7228 then
7229 -- We recognize the discriminant case by just looking for
7230 -- a reference to a discriminant. It can only be one for
7231 -- the enclosing construct. Skip freezing in this case.
7233 if Ekind (Entity (N)) = E_Discriminant then
7234 return;
7236 -- For the case of a reference to the enclosing record,
7237 -- (or task or protected type), we look for a type that
7238 -- matches the current scope.
7240 elsif Entity (N) = Current_Scope then
7241 return;
7242 end if;
7243 end if;
7245 -- If we have an enumeration literal that appears as the choice in
7246 -- the aggregate of an enumeration representation clause, then
7247 -- freezing does not occur (RM 13.14(10)).
7249 when N_Enumeration_Representation_Clause =>
7251 -- The case we are looking for is an enumeration literal
7253 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
7254 and then Is_Enumeration_Type (Etype (N))
7255 then
7256 -- If enumeration literal appears directly as the choice,
7257 -- do not freeze (this is the normal non-overloaded case)
7259 if Nkind (Parent (N)) = N_Component_Association
7260 and then First (Choices (Parent (N))) = N
7261 then
7262 return;
7264 -- If enumeration literal appears as the name of function
7265 -- which is the choice, then also do not freeze. This
7266 -- happens in the overloaded literal case, where the
7267 -- enumeration literal is temporarily changed to a function
7268 -- call for overloading analysis purposes.
7270 elsif Nkind (Parent (N)) = N_Function_Call
7271 and then
7272 Nkind (Parent (Parent (N))) = N_Component_Association
7273 and then
7274 First (Choices (Parent (Parent (N)))) = Parent (N)
7275 then
7276 return;
7277 end if;
7278 end if;
7280 -- Normally if the parent is a handled sequence of statements,
7281 -- then the current node must be a statement, and that is an
7282 -- appropriate place to insert a freeze node.
7284 when N_Handled_Sequence_Of_Statements =>
7286 -- An exception occurs when the sequence of statements is for
7287 -- an expander generated body that did not do the usual freeze
7288 -- all operation. In this case we usually want to freeze
7289 -- outside this body, not inside it, and we skip past the
7290 -- subprogram body that we are inside.
7292 if In_Expanded_Body (Parent_P) then
7293 declare
7294 Subp : constant Node_Id := Parent (Parent_P);
7295 Spec : Entity_Id;
7297 begin
7298 -- Freeze the entity only when it is declared inside the
7299 -- body of the expander generated procedure. This case
7300 -- is recognized by the scope of the entity or its type,
7301 -- which is either the spec for some enclosing body, or
7302 -- (in the case of init_procs, for which there are no
7303 -- separate specs) the current scope.
7305 if Nkind (Subp) = N_Subprogram_Body then
7306 Spec := Corresponding_Spec (Subp);
7308 if (Present (Typ) and then Scope (Typ) = Spec)
7309 or else
7310 (Present (Nam) and then Scope (Nam) = Spec)
7311 then
7312 exit;
7314 elsif Present (Typ)
7315 and then Scope (Typ) = Current_Scope
7316 and then Defining_Entity (Subp) = Current_Scope
7317 then
7318 exit;
7319 end if;
7320 end if;
7322 -- An expression function may act as a completion of
7323 -- a function declaration. As such, it can reference
7324 -- entities declared between the two views:
7326 -- Hidden []; -- 1
7327 -- function F return ...;
7328 -- private
7329 -- function Hidden return ...;
7330 -- function F return ... is (Hidden); -- 2
7332 -- Refering to the example above, freezing the expression
7333 -- of F (2) would place Hidden's freeze node (1) in the
7334 -- wrong place. Avoid explicit freezing and let the usual
7335 -- scenarios do the job - for example, reaching the end
7336 -- of the private declarations, or a call to F.
7338 if Nkind (Original_Node (Subp)) =
7339 N_Expression_Function
7340 then
7341 null;
7343 -- Freeze outside the body
7345 else
7346 Parent_P := Parent (Parent_P);
7347 Freeze_Outside := True;
7348 end if;
7349 end;
7351 -- Here if normal case where we are in handled statement
7352 -- sequence and want to do the insertion right there.
7354 else
7355 exit;
7356 end if;
7358 -- If parent is a body or a spec or a block, then the current node
7359 -- is a statement or declaration and we can insert the freeze node
7360 -- before it.
7362 when N_Block_Statement
7363 | N_Entry_Body
7364 | N_Package_Body
7365 | N_Package_Specification
7366 | N_Protected_Body
7367 | N_Subprogram_Body
7368 | N_Task_Body
7370 exit;
7372 -- The expander is allowed to define types in any statements list,
7373 -- so any of the following parent nodes also mark a freezing point
7374 -- if the actual node is in a list of statements or declarations.
7376 when N_Abortable_Part
7377 | N_Accept_Alternative
7378 | N_And_Then
7379 | N_Case_Statement_Alternative
7380 | N_Compilation_Unit_Aux
7381 | N_Conditional_Entry_Call
7382 | N_Delay_Alternative
7383 | N_Elsif_Part
7384 | N_Entry_Call_Alternative
7385 | N_Exception_Handler
7386 | N_Extended_Return_Statement
7387 | N_Freeze_Entity
7388 | N_If_Statement
7389 | N_Or_Else
7390 | N_Selective_Accept
7391 | N_Triggering_Alternative
7393 exit when Is_List_Member (P);
7395 -- Freeze nodes produced by an expression coming from the Actions
7396 -- list of a N_Expression_With_Actions node must remain within the
7397 -- Actions list. Inserting the freeze nodes further up the tree
7398 -- may lead to use before declaration issues in the case of array
7399 -- types.
7401 when N_Expression_With_Actions =>
7402 if Is_List_Member (P)
7403 and then List_Containing (P) = Actions (Parent_P)
7404 then
7405 exit;
7406 end if;
7408 -- Note: N_Loop_Statement is a special case. A type that appears
7409 -- in the source can never be frozen in a loop (this occurs only
7410 -- because of a loop expanded by the expander), so we keep on
7411 -- going. Otherwise we terminate the search. Same is true of any
7412 -- entity which comes from source. (if they have predefined type,
7413 -- that type does not appear to come from source, but the entity
7414 -- should not be frozen here).
7416 when N_Loop_Statement =>
7417 exit when not Comes_From_Source (Etype (N))
7418 and then (No (Nam) or else not Comes_From_Source (Nam));
7420 -- For all other cases, keep looking at parents
7422 when others =>
7423 null;
7424 end case;
7426 -- We fall through the case if we did not yet find the proper
7427 -- place in the free for inserting the freeze node, so climb.
7429 P := Parent_P;
7430 end loop;
7432 -- If the expression appears in a record or an initialization procedure,
7433 -- the freeze nodes are collected and attached to the current scope, to
7434 -- be inserted and analyzed on exit from the scope, to insure that
7435 -- generated entities appear in the correct scope. If the expression is
7436 -- a default for a discriminant specification, the scope is still void.
7437 -- The expression can also appear in the discriminant part of a private
7438 -- or concurrent type.
7440 -- If the expression appears in a constrained subcomponent of an
7441 -- enclosing record declaration, the freeze nodes must be attached to
7442 -- the outer record type so they can eventually be placed in the
7443 -- enclosing declaration list.
7445 -- The other case requiring this special handling is if we are in a
7446 -- default expression, since in that case we are about to freeze a
7447 -- static type, and the freeze scope needs to be the outer scope, not
7448 -- the scope of the subprogram with the default parameter.
7450 -- For default expressions and other spec expressions in generic units,
7451 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
7452 -- placing them at the proper place, after the generic unit.
7454 if (In_Spec_Exp and not Inside_A_Generic)
7455 or else Freeze_Outside
7456 or else (Is_Type (Current_Scope)
7457 and then (not Is_Concurrent_Type (Current_Scope)
7458 or else not Has_Completion (Current_Scope)))
7459 or else Ekind (Current_Scope) = E_Void
7460 then
7461 declare
7462 N : constant Node_Id := Current_Scope;
7463 Freeze_Nodes : List_Id := No_List;
7464 Pos : Int := Scope_Stack.Last;
7466 begin
7467 if Present (Desig_Typ) then
7468 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
7469 end if;
7471 if Present (Typ) then
7472 Freeze_And_Append (Typ, N, Freeze_Nodes);
7473 end if;
7475 if Present (Nam) then
7476 Freeze_And_Append (Nam, N, Freeze_Nodes);
7477 end if;
7479 -- The current scope may be that of a constrained component of
7480 -- an enclosing record declaration, or of a loop of an enclosing
7481 -- quantified expression, which is above the current scope in the
7482 -- scope stack. Indeed in the context of a quantified expression,
7483 -- a scope is created and pushed above the current scope in order
7484 -- to emulate the loop-like behavior of the quantified expression.
7485 -- If the expression is within a top-level pragma, as for a pre-
7486 -- condition on a library-level subprogram, nothing to do.
7488 if not Is_Compilation_Unit (Current_Scope)
7489 and then (Is_Record_Type (Scope (Current_Scope))
7490 or else Nkind (Parent (Current_Scope)) =
7491 N_Quantified_Expression)
7492 then
7493 Pos := Pos - 1;
7494 end if;
7496 if Is_Non_Empty_List (Freeze_Nodes) then
7497 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
7498 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
7499 Freeze_Nodes;
7500 else
7501 Append_List (Freeze_Nodes,
7502 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
7503 end if;
7504 end if;
7505 end;
7507 return;
7508 end if;
7510 -- Now we have the right place to do the freezing. First, a special
7511 -- adjustment, if we are in spec-expression analysis mode, these freeze
7512 -- actions must not be thrown away (normally all inserted actions are
7513 -- thrown away in this mode. However, the freeze actions are from static
7514 -- expressions and one of the important reasons we are doing this
7515 -- special analysis is to get these freeze actions. Therefore we turn
7516 -- off the In_Spec_Expression mode to propagate these freeze actions.
7517 -- This also means they get properly analyzed and expanded.
7519 In_Spec_Expression := False;
7521 -- Freeze the designated type of an allocator (RM 13.14(13))
7523 if Present (Desig_Typ) then
7524 Freeze_Before (P, Desig_Typ);
7525 end if;
7527 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
7528 -- the enumeration representation clause exception in the loop above.
7530 if Present (Typ) then
7531 Freeze_Before (P, Typ);
7532 end if;
7534 -- Freeze name if one is present (RM 13.14(11))
7536 if Present (Nam) then
7537 Freeze_Before (P, Nam);
7538 end if;
7540 -- Restore In_Spec_Expression flag
7542 In_Spec_Expression := In_Spec_Exp;
7543 end Freeze_Expression;
7545 -----------------------------
7546 -- Freeze_Fixed_Point_Type --
7547 -----------------------------
7549 -- Certain fixed-point types and subtypes, including implicit base types
7550 -- and declared first subtypes, have not yet set up a range. This is
7551 -- because the range cannot be set until the Small and Size values are
7552 -- known, and these are not known till the type is frozen.
7554 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
7555 -- whose bounds are unanalyzed real literals. This routine will recognize
7556 -- this case, and transform this range node into a properly typed range
7557 -- with properly analyzed and resolved values.
7559 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
7560 Rng : constant Node_Id := Scalar_Range (Typ);
7561 Lo : constant Node_Id := Low_Bound (Rng);
7562 Hi : constant Node_Id := High_Bound (Rng);
7563 Btyp : constant Entity_Id := Base_Type (Typ);
7564 Brng : constant Node_Id := Scalar_Range (Btyp);
7565 BLo : constant Node_Id := Low_Bound (Brng);
7566 BHi : constant Node_Id := High_Bound (Brng);
7567 Small : constant Ureal := Small_Value (Typ);
7568 Loval : Ureal;
7569 Hival : Ureal;
7570 Atype : Entity_Id;
7572 Orig_Lo : Ureal;
7573 Orig_Hi : Ureal;
7574 -- Save original bounds (for shaving tests)
7576 Actual_Size : Nat;
7577 -- Actual size chosen
7579 function Fsize (Lov, Hiv : Ureal) return Nat;
7580 -- Returns size of type with given bounds. Also leaves these
7581 -- bounds set as the current bounds of the Typ.
7583 -----------
7584 -- Fsize --
7585 -----------
7587 function Fsize (Lov, Hiv : Ureal) return Nat is
7588 begin
7589 Set_Realval (Lo, Lov);
7590 Set_Realval (Hi, Hiv);
7591 return Minimum_Size (Typ);
7592 end Fsize;
7594 -- Start of processing for Freeze_Fixed_Point_Type
7596 begin
7597 -- The type, or its first subtype if we are freezing the anonymous
7598 -- base, may have a delayed Small aspect. It must be analyzed now,
7599 -- so that all characteristics of the type (size, bounds) can be
7600 -- computed and validated in the call to Minimum_Size that follows.
7602 if Has_Delayed_Aspects (First_Subtype (Typ)) then
7603 Analyze_Aspects_At_Freeze_Point (First_Subtype (Typ));
7604 Set_Has_Delayed_Aspects (First_Subtype (Typ), False);
7605 end if;
7607 -- If Esize of a subtype has not previously been set, set it now
7609 if Unknown_Esize (Typ) then
7610 Atype := Ancestor_Subtype (Typ);
7612 if Present (Atype) then
7613 Set_Esize (Typ, Esize (Atype));
7614 else
7615 Set_Esize (Typ, Esize (Base_Type (Typ)));
7616 end if;
7617 end if;
7619 -- Immediate return if the range is already analyzed. This means that
7620 -- the range is already set, and does not need to be computed by this
7621 -- routine.
7623 if Analyzed (Rng) then
7624 return;
7625 end if;
7627 -- Immediate return if either of the bounds raises Constraint_Error
7629 if Raises_Constraint_Error (Lo)
7630 or else Raises_Constraint_Error (Hi)
7631 then
7632 return;
7633 end if;
7635 Loval := Realval (Lo);
7636 Hival := Realval (Hi);
7638 Orig_Lo := Loval;
7639 Orig_Hi := Hival;
7641 -- Ordinary fixed-point case
7643 if Is_Ordinary_Fixed_Point_Type (Typ) then
7645 -- For the ordinary fixed-point case, we are allowed to fudge the
7646 -- end-points up or down by small. Generally we prefer to fudge up,
7647 -- i.e. widen the bounds for non-model numbers so that the end points
7648 -- are included. However there are cases in which this cannot be
7649 -- done, and indeed cases in which we may need to narrow the bounds.
7650 -- The following circuit makes the decision.
7652 -- Note: our terminology here is that Incl_EP means that the bounds
7653 -- are widened by Small if necessary to include the end points, and
7654 -- Excl_EP means that the bounds are narrowed by Small to exclude the
7655 -- end-points if this reduces the size.
7657 -- Note that in the Incl case, all we care about is including the
7658 -- end-points. In the Excl case, we want to narrow the bounds as
7659 -- much as permitted by the RM, to give the smallest possible size.
7661 Fudge : declare
7662 Loval_Incl_EP : Ureal;
7663 Hival_Incl_EP : Ureal;
7665 Loval_Excl_EP : Ureal;
7666 Hival_Excl_EP : Ureal;
7668 Size_Incl_EP : Nat;
7669 Size_Excl_EP : Nat;
7671 Model_Num : Ureal;
7672 First_Subt : Entity_Id;
7673 Actual_Lo : Ureal;
7674 Actual_Hi : Ureal;
7676 begin
7677 -- First step. Base types are required to be symmetrical. Right
7678 -- now, the base type range is a copy of the first subtype range.
7679 -- This will be corrected before we are done, but right away we
7680 -- need to deal with the case where both bounds are non-negative.
7681 -- In this case, we set the low bound to the negative of the high
7682 -- bound, to make sure that the size is computed to include the
7683 -- required sign. Note that we do not need to worry about the
7684 -- case of both bounds negative, because the sign will be dealt
7685 -- with anyway. Furthermore we can't just go making such a bound
7686 -- symmetrical, since in a twos-complement system, there is an
7687 -- extra negative value which could not be accommodated on the
7688 -- positive side.
7690 if Typ = Btyp
7691 and then not UR_Is_Negative (Loval)
7692 and then Hival > Loval
7693 then
7694 Loval := -Hival;
7695 Set_Realval (Lo, Loval);
7696 end if;
7698 -- Compute the fudged bounds. If the number is a model number,
7699 -- then we do nothing to include it, but we are allowed to backoff
7700 -- to the next adjacent model number when we exclude it. If it is
7701 -- not a model number then we straddle the two values with the
7702 -- model numbers on either side.
7704 Model_Num := UR_Trunc (Loval / Small) * Small;
7706 if Loval = Model_Num then
7707 Loval_Incl_EP := Model_Num;
7708 else
7709 Loval_Incl_EP := Model_Num - Small;
7710 end if;
7712 -- The low value excluding the end point is Small greater, but
7713 -- we do not do this exclusion if the low value is positive,
7714 -- since it can't help the size and could actually hurt by
7715 -- crossing the high bound.
7717 if UR_Is_Negative (Loval_Incl_EP) then
7718 Loval_Excl_EP := Loval_Incl_EP + Small;
7720 -- If the value went from negative to zero, then we have the
7721 -- case where Loval_Incl_EP is the model number just below
7722 -- zero, so we want to stick to the negative value for the
7723 -- base type to maintain the condition that the size will
7724 -- include signed values.
7726 if Typ = Btyp
7727 and then UR_Is_Zero (Loval_Excl_EP)
7728 then
7729 Loval_Excl_EP := Loval_Incl_EP;
7730 end if;
7732 else
7733 Loval_Excl_EP := Loval_Incl_EP;
7734 end if;
7736 -- Similar processing for upper bound and high value
7738 Model_Num := UR_Trunc (Hival / Small) * Small;
7740 if Hival = Model_Num then
7741 Hival_Incl_EP := Model_Num;
7742 else
7743 Hival_Incl_EP := Model_Num + Small;
7744 end if;
7746 if UR_Is_Positive (Hival_Incl_EP) then
7747 Hival_Excl_EP := Hival_Incl_EP - Small;
7748 else
7749 Hival_Excl_EP := Hival_Incl_EP;
7750 end if;
7752 -- One further adjustment is needed. In the case of subtypes, we
7753 -- cannot go outside the range of the base type, or we get
7754 -- peculiarities, and the base type range is already set. This
7755 -- only applies to the Incl values, since clearly the Excl values
7756 -- are already as restricted as they are allowed to be.
7758 if Typ /= Btyp then
7759 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
7760 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
7761 end if;
7763 -- Get size including and excluding end points
7765 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
7766 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
7768 -- No need to exclude end-points if it does not reduce size
7770 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
7771 Loval_Excl_EP := Loval_Incl_EP;
7772 end if;
7774 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
7775 Hival_Excl_EP := Hival_Incl_EP;
7776 end if;
7778 -- Now we set the actual size to be used. We want to use the
7779 -- bounds fudged up to include the end-points but only if this
7780 -- can be done without violating a specifically given size
7781 -- size clause or causing an unacceptable increase in size.
7783 -- Case of size clause given
7785 if Has_Size_Clause (Typ) then
7787 -- Use the inclusive size only if it is consistent with
7788 -- the explicitly specified size.
7790 if Size_Incl_EP <= RM_Size (Typ) then
7791 Actual_Lo := Loval_Incl_EP;
7792 Actual_Hi := Hival_Incl_EP;
7793 Actual_Size := Size_Incl_EP;
7795 -- If the inclusive size is too large, we try excluding
7796 -- the end-points (will be caught later if does not work).
7798 else
7799 Actual_Lo := Loval_Excl_EP;
7800 Actual_Hi := Hival_Excl_EP;
7801 Actual_Size := Size_Excl_EP;
7802 end if;
7804 -- Case of size clause not given
7806 else
7807 -- If we have a base type whose corresponding first subtype
7808 -- has an explicit size that is large enough to include our
7809 -- end-points, then do so. There is no point in working hard
7810 -- to get a base type whose size is smaller than the specified
7811 -- size of the first subtype.
7813 First_Subt := First_Subtype (Typ);
7815 if Has_Size_Clause (First_Subt)
7816 and then Size_Incl_EP <= Esize (First_Subt)
7817 then
7818 Actual_Size := Size_Incl_EP;
7819 Actual_Lo := Loval_Incl_EP;
7820 Actual_Hi := Hival_Incl_EP;
7822 -- If excluding the end-points makes the size smaller and
7823 -- results in a size of 8,16,32,64, then we take the smaller
7824 -- size. For the 64 case, this is compulsory. For the other
7825 -- cases, it seems reasonable. We like to include end points
7826 -- if we can, but not at the expense of moving to the next
7827 -- natural boundary of size.
7829 elsif Size_Incl_EP /= Size_Excl_EP
7830 and then Addressable (Size_Excl_EP)
7831 then
7832 Actual_Size := Size_Excl_EP;
7833 Actual_Lo := Loval_Excl_EP;
7834 Actual_Hi := Hival_Excl_EP;
7836 -- Otherwise we can definitely include the end points
7838 else
7839 Actual_Size := Size_Incl_EP;
7840 Actual_Lo := Loval_Incl_EP;
7841 Actual_Hi := Hival_Incl_EP;
7842 end if;
7844 -- One pathological case: normally we never fudge a low bound
7845 -- down, since it would seem to increase the size (if it has
7846 -- any effect), but for ranges containing single value, or no
7847 -- values, the high bound can be small too large. Consider:
7849 -- type t is delta 2.0**(-14)
7850 -- range 131072.0 .. 0;
7852 -- That lower bound is *just* outside the range of 32 bits, and
7853 -- does need fudging down in this case. Note that the bounds
7854 -- will always have crossed here, since the high bound will be
7855 -- fudged down if necessary, as in the case of:
7857 -- type t is delta 2.0**(-14)
7858 -- range 131072.0 .. 131072.0;
7860 -- So we detect the situation by looking for crossed bounds,
7861 -- and if the bounds are crossed, and the low bound is greater
7862 -- than zero, we will always back it off by small, since this
7863 -- is completely harmless.
7865 if Actual_Lo > Actual_Hi then
7866 if UR_Is_Positive (Actual_Lo) then
7867 Actual_Lo := Loval_Incl_EP - Small;
7868 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7870 -- And of course, we need to do exactly the same parallel
7871 -- fudge for flat ranges in the negative region.
7873 elsif UR_Is_Negative (Actual_Hi) then
7874 Actual_Hi := Hival_Incl_EP + Small;
7875 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7876 end if;
7877 end if;
7878 end if;
7880 Set_Realval (Lo, Actual_Lo);
7881 Set_Realval (Hi, Actual_Hi);
7882 end Fudge;
7884 -- For the decimal case, none of this fudging is required, since there
7885 -- are no end-point problems in the decimal case (the end-points are
7886 -- always included).
7888 else
7889 Actual_Size := Fsize (Loval, Hival);
7890 end if;
7892 -- At this stage, the actual size has been calculated and the proper
7893 -- required bounds are stored in the low and high bounds.
7895 if Actual_Size > 64 then
7896 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7897 Error_Msg_N
7898 ("size required (^) for type& too large, maximum allowed is 64",
7899 Typ);
7900 Actual_Size := 64;
7901 end if;
7903 -- Check size against explicit given size
7905 if Has_Size_Clause (Typ) then
7906 if Actual_Size > RM_Size (Typ) then
7907 Error_Msg_Uint_1 := RM_Size (Typ);
7908 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7909 Error_Msg_NE
7910 ("size given (^) for type& too small, minimum allowed is ^",
7911 Size_Clause (Typ), Typ);
7913 else
7914 Actual_Size := UI_To_Int (Esize (Typ));
7915 end if;
7917 -- Increase size to next natural boundary if no size clause given
7919 else
7920 if Actual_Size <= 8 then
7921 Actual_Size := 8;
7922 elsif Actual_Size <= 16 then
7923 Actual_Size := 16;
7924 elsif Actual_Size <= 32 then
7925 Actual_Size := 32;
7926 else
7927 Actual_Size := 64;
7928 end if;
7930 Init_Esize (Typ, Actual_Size);
7931 Adjust_Esize_For_Alignment (Typ);
7932 end if;
7934 -- If we have a base type, then expand the bounds so that they extend to
7935 -- the full width of the allocated size in bits, to avoid junk range
7936 -- checks on intermediate computations.
7938 if Base_Type (Typ) = Typ then
7939 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7940 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7941 end if;
7943 -- Final step is to reanalyze the bounds using the proper type
7944 -- and set the Corresponding_Integer_Value fields of the literals.
7946 Set_Etype (Lo, Empty);
7947 Set_Analyzed (Lo, False);
7948 Analyze (Lo);
7950 -- Resolve with universal fixed if the base type, and the base type if
7951 -- it is a subtype. Note we can't resolve the base type with itself,
7952 -- that would be a reference before definition.
7954 if Typ = Btyp then
7955 Resolve (Lo, Universal_Fixed);
7956 else
7957 Resolve (Lo, Btyp);
7958 end if;
7960 -- Set corresponding integer value for bound
7962 Set_Corresponding_Integer_Value
7963 (Lo, UR_To_Uint (Realval (Lo) / Small));
7965 -- Similar processing for high bound
7967 Set_Etype (Hi, Empty);
7968 Set_Analyzed (Hi, False);
7969 Analyze (Hi);
7971 if Typ = Btyp then
7972 Resolve (Hi, Universal_Fixed);
7973 else
7974 Resolve (Hi, Btyp);
7975 end if;
7977 Set_Corresponding_Integer_Value
7978 (Hi, UR_To_Uint (Realval (Hi) / Small));
7980 -- Set type of range to correspond to bounds
7982 Set_Etype (Rng, Etype (Lo));
7984 -- Set Esize to calculated size if not set already
7986 if Unknown_Esize (Typ) then
7987 Init_Esize (Typ, Actual_Size);
7988 end if;
7990 -- Set RM_Size if not already set. If already set, check value
7992 declare
7993 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7995 begin
7996 if RM_Size (Typ) /= Uint_0 then
7997 if RM_Size (Typ) < Minsiz then
7998 Error_Msg_Uint_1 := RM_Size (Typ);
7999 Error_Msg_Uint_2 := Minsiz;
8000 Error_Msg_NE
8001 ("size given (^) for type& too small, minimum allowed is ^",
8002 Size_Clause (Typ), Typ);
8003 end if;
8005 else
8006 Set_RM_Size (Typ, Minsiz);
8007 end if;
8008 end;
8010 -- Check for shaving
8012 if Comes_From_Source (Typ) then
8014 -- In SPARK mode the given bounds must be strictly representable
8016 if SPARK_Mode = On then
8017 if Orig_Lo < Expr_Value_R (Lo) then
8018 Error_Msg_NE
8019 ("declared low bound of type & is outside type range",
8020 Lo, Typ);
8021 end if;
8023 if Orig_Hi > Expr_Value_R (Hi) then
8024 Error_Msg_NE
8025 ("declared high bound of type & is outside type range",
8026 Hi, Typ);
8027 end if;
8029 else
8030 if Orig_Lo < Expr_Value_R (Lo) then
8031 Error_Msg_N
8032 ("declared low bound of type & is outside type range??", Typ);
8033 Error_Msg_N
8034 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
8035 end if;
8037 if Orig_Hi > Expr_Value_R (Hi) then
8038 Error_Msg_N
8039 ("declared high bound of type & is outside type range??",
8040 Typ);
8041 Error_Msg_N
8042 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
8043 end if;
8044 end if;
8045 end if;
8046 end Freeze_Fixed_Point_Type;
8048 ------------------
8049 -- Freeze_Itype --
8050 ------------------
8052 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
8053 L : List_Id;
8055 begin
8056 Set_Has_Delayed_Freeze (T);
8057 L := Freeze_Entity (T, N);
8059 if Is_Non_Empty_List (L) then
8060 Insert_Actions (N, L);
8061 end if;
8062 end Freeze_Itype;
8064 --------------------------
8065 -- Freeze_Static_Object --
8066 --------------------------
8068 procedure Freeze_Static_Object (E : Entity_Id) is
8070 Cannot_Be_Static : exception;
8071 -- Exception raised if the type of a static object cannot be made
8072 -- static. This happens if the type depends on non-global objects.
8074 procedure Ensure_Expression_Is_SA (N : Node_Id);
8075 -- Called to ensure that an expression used as part of a type definition
8076 -- is statically allocatable, which means that the expression type is
8077 -- statically allocatable, and the expression is either static, or a
8078 -- reference to a library level constant.
8080 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
8081 -- Called to mark a type as static, checking that it is possible
8082 -- to set the type as static. If it is not possible, then the
8083 -- exception Cannot_Be_Static is raised.
8085 -----------------------------
8086 -- Ensure_Expression_Is_SA --
8087 -----------------------------
8089 procedure Ensure_Expression_Is_SA (N : Node_Id) is
8090 Ent : Entity_Id;
8092 begin
8093 Ensure_Type_Is_SA (Etype (N));
8095 if Is_OK_Static_Expression (N) then
8096 return;
8098 elsif Nkind (N) = N_Identifier then
8099 Ent := Entity (N);
8101 if Present (Ent)
8102 and then Ekind (Ent) = E_Constant
8103 and then Is_Library_Level_Entity (Ent)
8104 then
8105 return;
8106 end if;
8107 end if;
8109 raise Cannot_Be_Static;
8110 end Ensure_Expression_Is_SA;
8112 -----------------------
8113 -- Ensure_Type_Is_SA --
8114 -----------------------
8116 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
8117 N : Node_Id;
8118 C : Entity_Id;
8120 begin
8121 -- If type is library level, we are all set
8123 if Is_Library_Level_Entity (Typ) then
8124 return;
8125 end if;
8127 -- We are also OK if the type already marked as statically allocated,
8128 -- which means we processed it before.
8130 if Is_Statically_Allocated (Typ) then
8131 return;
8132 end if;
8134 -- Mark type as statically allocated
8136 Set_Is_Statically_Allocated (Typ);
8138 -- Check that it is safe to statically allocate this type
8140 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
8141 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
8142 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
8144 elsif Is_Array_Type (Typ) then
8145 N := First_Index (Typ);
8146 while Present (N) loop
8147 Ensure_Type_Is_SA (Etype (N));
8148 Next_Index (N);
8149 end loop;
8151 Ensure_Type_Is_SA (Component_Type (Typ));
8153 elsif Is_Access_Type (Typ) then
8154 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
8156 declare
8157 F : Entity_Id;
8158 T : constant Entity_Id := Etype (Designated_Type (Typ));
8160 begin
8161 if T /= Standard_Void_Type then
8162 Ensure_Type_Is_SA (T);
8163 end if;
8165 F := First_Formal (Designated_Type (Typ));
8166 while Present (F) loop
8167 Ensure_Type_Is_SA (Etype (F));
8168 Next_Formal (F);
8169 end loop;
8170 end;
8172 else
8173 Ensure_Type_Is_SA (Designated_Type (Typ));
8174 end if;
8176 elsif Is_Record_Type (Typ) then
8177 C := First_Entity (Typ);
8178 while Present (C) loop
8179 if Ekind (C) = E_Discriminant
8180 or else Ekind (C) = E_Component
8181 then
8182 Ensure_Type_Is_SA (Etype (C));
8184 elsif Is_Type (C) then
8185 Ensure_Type_Is_SA (C);
8186 end if;
8188 Next_Entity (C);
8189 end loop;
8191 elsif Ekind (Typ) = E_Subprogram_Type then
8192 Ensure_Type_Is_SA (Etype (Typ));
8194 C := First_Formal (Typ);
8195 while Present (C) loop
8196 Ensure_Type_Is_SA (Etype (C));
8197 Next_Formal (C);
8198 end loop;
8200 else
8201 raise Cannot_Be_Static;
8202 end if;
8203 end Ensure_Type_Is_SA;
8205 -- Start of processing for Freeze_Static_Object
8207 begin
8208 Ensure_Type_Is_SA (Etype (E));
8210 exception
8211 when Cannot_Be_Static =>
8213 -- If the object that cannot be static is imported or exported, then
8214 -- issue an error message saying that this object cannot be imported
8215 -- or exported. If it has an address clause it is an overlay in the
8216 -- current partition and the static requirement is not relevant.
8217 -- Do not issue any error message when ignoring rep clauses.
8219 if Ignore_Rep_Clauses then
8220 null;
8222 elsif Is_Imported (E) then
8223 if No (Address_Clause (E)) then
8224 Error_Msg_N
8225 ("& cannot be imported (local type is not constant)", E);
8226 end if;
8228 -- Otherwise must be exported, something is wrong if compiler
8229 -- is marking something as statically allocated which cannot be).
8231 else pragma Assert (Is_Exported (E));
8232 Error_Msg_N
8233 ("& cannot be exported (local type is not constant)", E);
8234 end if;
8235 end Freeze_Static_Object;
8237 -----------------------
8238 -- Freeze_Subprogram --
8239 -----------------------
8241 procedure Freeze_Subprogram (E : Entity_Id) is
8242 procedure Set_Profile_Convention (Subp_Id : Entity_Id);
8243 -- Set the conventions of all anonymous access-to-subprogram formals and
8244 -- result subtype of subprogram Subp_Id to the convention of Subp_Id.
8246 ----------------------------
8247 -- Set_Profile_Convention --
8248 ----------------------------
8250 procedure Set_Profile_Convention (Subp_Id : Entity_Id) is
8251 Conv : constant Convention_Id := Convention (Subp_Id);
8253 procedure Set_Type_Convention (Typ : Entity_Id);
8254 -- Set the convention of anonymous access-to-subprogram type Typ and
8255 -- its designated type to Conv.
8257 -------------------------
8258 -- Set_Type_Convention --
8259 -------------------------
8261 procedure Set_Type_Convention (Typ : Entity_Id) is
8262 begin
8263 -- Set the convention on both the anonymous access-to-subprogram
8264 -- type and the subprogram type it points to because both types
8265 -- participate in conformance-related checks.
8267 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
8268 Set_Convention (Typ, Conv);
8269 Set_Convention (Designated_Type (Typ), Conv);
8270 end if;
8271 end Set_Type_Convention;
8273 -- Local variables
8275 Formal : Entity_Id;
8277 -- Start of processing for Set_Profile_Convention
8279 begin
8280 Formal := First_Formal (Subp_Id);
8281 while Present (Formal) loop
8282 Set_Type_Convention (Etype (Formal));
8283 Next_Formal (Formal);
8284 end loop;
8286 if Ekind (Subp_Id) = E_Function then
8287 Set_Type_Convention (Etype (Subp_Id));
8288 end if;
8289 end Set_Profile_Convention;
8291 -- Local variables
8293 F : Entity_Id;
8294 Retype : Entity_Id;
8296 -- Start of processing for Freeze_Subprogram
8298 begin
8299 -- Subprogram may not have an address clause unless it is imported
8301 if Present (Address_Clause (E)) then
8302 if not Is_Imported (E) then
8303 Error_Msg_N
8304 ("address clause can only be given for imported subprogram",
8305 Name (Address_Clause (E)));
8306 end if;
8307 end if;
8309 -- Reset the Pure indication on an imported subprogram unless an
8310 -- explicit Pure_Function pragma was present or the subprogram is an
8311 -- intrinsic. We do this because otherwise it is an insidious error
8312 -- to call a non-pure function from pure unit and have calls
8313 -- mysteriously optimized away. What happens here is that the Import
8314 -- can bypass the normal check to ensure that pure units call only pure
8315 -- subprograms.
8317 -- The reason for the intrinsic exception is that in general, intrinsic
8318 -- functions (such as shifts) are pure anyway. The only exceptions are
8319 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
8320 -- in any case, so no problem arises.
8322 if Is_Imported (E)
8323 and then Is_Pure (E)
8324 and then not Has_Pragma_Pure_Function (E)
8325 and then not Is_Intrinsic_Subprogram (E)
8326 then
8327 Set_Is_Pure (E, False);
8328 end if;
8330 -- We also reset the Pure indication on a subprogram with an Address
8331 -- parameter, because the parameter may be used as a pointer and the
8332 -- referenced data may change even if the address value does not.
8334 -- Note that if the programmer gave an explicit Pure_Function pragma,
8335 -- then we believe the programmer, and leave the subprogram Pure. We
8336 -- also suppress this check on run-time files.
8338 if Is_Pure (E)
8339 and then Is_Subprogram (E)
8340 and then not Has_Pragma_Pure_Function (E)
8341 and then not Is_Internal_Unit (Current_Sem_Unit)
8342 then
8343 Check_Function_With_Address_Parameter (E);
8344 end if;
8346 -- Ensure that all anonymous access-to-subprogram types inherit the
8347 -- convention of their related subprogram (RM 6.3.1 13.1/3). This is
8348 -- not done for a defaulted convention Ada because those types also
8349 -- default to Ada. Convention Protected must not be propagated when
8350 -- the subprogram is an entry because this would be illegal. The only
8351 -- way to force convention Protected on these kinds of types is to
8352 -- include keyword "protected" in the access definition.
8354 if Convention (E) /= Convention_Ada
8355 and then Convention (E) /= Convention_Protected
8356 then
8357 Set_Profile_Convention (E);
8358 end if;
8360 -- For non-foreign convention subprograms, this is where we create
8361 -- the extra formals (for accessibility level and constrained bit
8362 -- information). We delay this till the freeze point precisely so
8363 -- that we know the convention.
8365 if not Has_Foreign_Convention (E) then
8366 if No (Extra_Formals (E)) then
8367 Create_Extra_Formals (E);
8368 end if;
8370 Set_Mechanisms (E);
8372 -- If this is convention Ada and a Valued_Procedure, that's odd
8374 if Ekind (E) = E_Procedure
8375 and then Is_Valued_Procedure (E)
8376 and then Convention (E) = Convention_Ada
8377 and then Warn_On_Export_Import
8378 then
8379 Error_Msg_N
8380 ("??Valued_Procedure has no effect for convention Ada", E);
8381 Set_Is_Valued_Procedure (E, False);
8382 end if;
8384 -- Case of foreign convention
8386 else
8387 Set_Mechanisms (E);
8389 -- For foreign conventions, warn about return of unconstrained array
8391 if Ekind (E) = E_Function then
8392 Retype := Underlying_Type (Etype (E));
8394 -- If no return type, probably some other error, e.g. a
8395 -- missing full declaration, so ignore.
8397 if No (Retype) then
8398 null;
8400 -- If the return type is generic, we have emitted a warning
8401 -- earlier on, and there is nothing else to check here. Specific
8402 -- instantiations may lead to erroneous behavior.
8404 elsif Is_Generic_Type (Etype (E)) then
8405 null;
8407 -- Display warning if returning unconstrained array
8409 elsif Is_Array_Type (Retype)
8410 and then not Is_Constrained (Retype)
8412 -- Check appropriate warning is enabled (should we check for
8413 -- Warnings (Off) on specific entities here, probably so???)
8415 and then Warn_On_Export_Import
8416 then
8417 Error_Msg_N
8418 ("?x?foreign convention function& should not return " &
8419 "unconstrained array", E);
8420 return;
8421 end if;
8422 end if;
8424 -- If any of the formals for an exported foreign convention
8425 -- subprogram have defaults, then emit an appropriate warning since
8426 -- this is odd (default cannot be used from non-Ada code)
8428 if Is_Exported (E) then
8429 F := First_Formal (E);
8430 while Present (F) loop
8431 if Warn_On_Export_Import
8432 and then Present (Default_Value (F))
8433 then
8434 Error_Msg_N
8435 ("?x?parameter cannot be defaulted in non-Ada call",
8436 Default_Value (F));
8437 end if;
8439 Next_Formal (F);
8440 end loop;
8441 end if;
8442 end if;
8444 -- Pragma Inline_Always is disallowed for dispatching subprograms
8445 -- because the address of such subprograms is saved in the dispatch
8446 -- table to support dispatching calls, and dispatching calls cannot
8447 -- be inlined. This is consistent with the restriction against using
8448 -- 'Access or 'Address on an Inline_Always subprogram.
8450 if Is_Dispatching_Operation (E)
8451 and then Has_Pragma_Inline_Always (E)
8452 then
8453 Error_Msg_N
8454 ("pragma Inline_Always not allowed for dispatching subprograms", E);
8455 end if;
8457 -- Because of the implicit representation of inherited predefined
8458 -- operators in the front-end, the overriding status of the operation
8459 -- may be affected when a full view of a type is analyzed, and this is
8460 -- not captured by the analysis of the corresponding type declaration.
8461 -- Therefore the correctness of a not-overriding indicator must be
8462 -- rechecked when the subprogram is frozen.
8464 if Nkind (E) = N_Defining_Operator_Symbol
8465 and then not Error_Posted (Parent (E))
8466 then
8467 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
8468 end if;
8470 if Modify_Tree_For_C
8471 and then Nkind (Parent (E)) = N_Function_Specification
8472 and then Is_Array_Type (Etype (E))
8473 and then Is_Constrained (Etype (E))
8474 and then not Is_Unchecked_Conversion_Instance (E)
8475 and then not Rewritten_For_C (E)
8476 then
8477 Build_Procedure_Form (Unit_Declaration_Node (E));
8478 end if;
8479 end Freeze_Subprogram;
8481 ----------------------
8482 -- Is_Fully_Defined --
8483 ----------------------
8485 function Is_Fully_Defined (T : Entity_Id) return Boolean is
8486 begin
8487 if Ekind (T) = E_Class_Wide_Type then
8488 return Is_Fully_Defined (Etype (T));
8490 elsif Is_Array_Type (T) then
8491 return Is_Fully_Defined (Component_Type (T));
8493 elsif Is_Record_Type (T)
8494 and not Is_Private_Type (T)
8495 then
8496 -- Verify that the record type has no components with private types
8497 -- without completion.
8499 declare
8500 Comp : Entity_Id;
8502 begin
8503 Comp := First_Component (T);
8504 while Present (Comp) loop
8505 if not Is_Fully_Defined (Etype (Comp)) then
8506 return False;
8507 end if;
8509 Next_Component (Comp);
8510 end loop;
8511 return True;
8512 end;
8514 -- For the designated type of an access to subprogram, all types in
8515 -- the profile must be fully defined.
8517 elsif Ekind (T) = E_Subprogram_Type then
8518 declare
8519 F : Entity_Id;
8521 begin
8522 F := First_Formal (T);
8523 while Present (F) loop
8524 if not Is_Fully_Defined (Etype (F)) then
8525 return False;
8526 end if;
8528 Next_Formal (F);
8529 end loop;
8531 return Is_Fully_Defined (Etype (T));
8532 end;
8534 else
8535 return not Is_Private_Type (T)
8536 or else Present (Full_View (Base_Type (T)));
8537 end if;
8538 end Is_Fully_Defined;
8540 ---------------------------------
8541 -- Process_Default_Expressions --
8542 ---------------------------------
8544 procedure Process_Default_Expressions
8545 (E : Entity_Id;
8546 After : in out Node_Id)
8548 Loc : constant Source_Ptr := Sloc (E);
8549 Dbody : Node_Id;
8550 Formal : Node_Id;
8551 Dcopy : Node_Id;
8552 Dnam : Entity_Id;
8554 begin
8555 Set_Default_Expressions_Processed (E);
8557 -- A subprogram instance and its associated anonymous subprogram share
8558 -- their signature. The default expression functions are defined in the
8559 -- wrapper packages for the anonymous subprogram, and should not be
8560 -- generated again for the instance.
8562 if Is_Generic_Instance (E)
8563 and then Present (Alias (E))
8564 and then Default_Expressions_Processed (Alias (E))
8565 then
8566 return;
8567 end if;
8569 Formal := First_Formal (E);
8570 while Present (Formal) loop
8571 if Present (Default_Value (Formal)) then
8573 -- We work with a copy of the default expression because we
8574 -- do not want to disturb the original, since this would mess
8575 -- up the conformance checking.
8577 Dcopy := New_Copy_Tree (Default_Value (Formal));
8579 -- The analysis of the expression may generate insert actions,
8580 -- which of course must not be executed. We wrap those actions
8581 -- in a procedure that is not called, and later on eliminated.
8582 -- The following cases have no side effects, and are analyzed
8583 -- directly.
8585 if Nkind (Dcopy) = N_Identifier
8586 or else Nkind_In (Dcopy, N_Expanded_Name,
8587 N_Integer_Literal,
8588 N_Character_Literal,
8589 N_String_Literal,
8590 N_Real_Literal)
8591 or else (Nkind (Dcopy) = N_Attribute_Reference
8592 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
8593 or else Known_Null (Dcopy)
8594 then
8595 -- If there is no default function, we must still do a full
8596 -- analyze call on the default value, to ensure that all error
8597 -- checks are performed, e.g. those associated with static
8598 -- evaluation. Note: this branch will always be taken if the
8599 -- analyzer is turned off (but we still need the error checks).
8601 -- Note: the setting of parent here is to meet the requirement
8602 -- that we can only analyze the expression while attached to
8603 -- the tree. Really the requirement is that the parent chain
8604 -- be set, we don't actually need to be in the tree.
8606 Set_Parent (Dcopy, Declaration_Node (Formal));
8607 Analyze (Dcopy);
8609 -- Default expressions are resolved with their own type if the
8610 -- context is generic, to avoid anomalies with private types.
8612 if Ekind (Scope (E)) = E_Generic_Package then
8613 Resolve (Dcopy);
8614 else
8615 Resolve (Dcopy, Etype (Formal));
8616 end if;
8618 -- If that resolved expression will raise constraint error,
8619 -- then flag the default value as raising constraint error.
8620 -- This allows a proper error message on the calls.
8622 if Raises_Constraint_Error (Dcopy) then
8623 Set_Raises_Constraint_Error (Default_Value (Formal));
8624 end if;
8626 -- If the default is a parameterless call, we use the name of
8627 -- the called function directly, and there is no body to build.
8629 elsif Nkind (Dcopy) = N_Function_Call
8630 and then No (Parameter_Associations (Dcopy))
8631 then
8632 null;
8634 -- Else construct and analyze the body of a wrapper procedure
8635 -- that contains an object declaration to hold the expression.
8636 -- Given that this is done only to complete the analysis, it is
8637 -- simpler to build a procedure than a function which might
8638 -- involve secondary stack expansion.
8640 else
8641 Dnam := Make_Temporary (Loc, 'D');
8643 Dbody :=
8644 Make_Subprogram_Body (Loc,
8645 Specification =>
8646 Make_Procedure_Specification (Loc,
8647 Defining_Unit_Name => Dnam),
8649 Declarations => New_List (
8650 Make_Object_Declaration (Loc,
8651 Defining_Identifier => Make_Temporary (Loc, 'T'),
8652 Object_Definition =>
8653 New_Occurrence_Of (Etype (Formal), Loc),
8654 Expression => New_Copy_Tree (Dcopy))),
8656 Handled_Statement_Sequence =>
8657 Make_Handled_Sequence_Of_Statements (Loc,
8658 Statements => Empty_List));
8660 Set_Scope (Dnam, Scope (E));
8661 Set_Assignment_OK (First (Declarations (Dbody)));
8662 Set_Is_Eliminated (Dnam);
8663 Insert_After (After, Dbody);
8664 Analyze (Dbody);
8665 After := Dbody;
8666 end if;
8667 end if;
8669 Next_Formal (Formal);
8670 end loop;
8671 end Process_Default_Expressions;
8673 ----------------------------------------
8674 -- Set_Component_Alignment_If_Not_Set --
8675 ----------------------------------------
8677 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
8678 begin
8679 -- Ignore if not base type, subtypes don't need anything
8681 if Typ /= Base_Type (Typ) then
8682 return;
8683 end if;
8685 -- Do not override existing representation
8687 if Is_Packed (Typ) then
8688 return;
8690 elsif Has_Specified_Layout (Typ) then
8691 return;
8693 elsif Component_Alignment (Typ) /= Calign_Default then
8694 return;
8696 else
8697 Set_Component_Alignment
8698 (Typ, Scope_Stack.Table
8699 (Scope_Stack.Last).Component_Alignment_Default);
8700 end if;
8701 end Set_Component_Alignment_If_Not_Set;
8703 --------------------------
8704 -- Set_SSO_From_Default --
8705 --------------------------
8707 procedure Set_SSO_From_Default (T : Entity_Id) is
8708 Reversed : Boolean;
8710 begin
8711 -- Set default SSO for an array or record base type, except in case of
8712 -- a type extension (which always inherits the SSO of its parent type).
8714 if Is_Base_Type (T)
8715 and then (Is_Array_Type (T)
8716 or else (Is_Record_Type (T)
8717 and then not (Is_Tagged_Type (T)
8718 and then Is_Derived_Type (T))))
8719 then
8720 Reversed :=
8721 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
8722 or else
8723 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
8725 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
8727 -- For a record type, if bit order is specified explicitly,
8728 -- then do not set SSO from default if not consistent. Note that
8729 -- we do not want to look at a Bit_Order attribute definition
8730 -- for a parent: if we were to inherit Bit_Order, then both
8731 -- SSO_Set_*_By_Default flags would have been cleared already
8732 -- (by Inherit_Aspects_At_Freeze_Point).
8734 and then not
8735 (Is_Record_Type (T)
8736 and then
8737 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
8738 and then Reverse_Bit_Order (T) /= Reversed)
8739 then
8740 -- If flags cause reverse storage order, then set the result. Note
8741 -- that we would have ignored the pragma setting the non default
8742 -- storage order in any case, hence the assertion at this point.
8744 pragma Assert
8745 (not Reversed or else Support_Nondefault_SSO_On_Target);
8747 Set_Reverse_Storage_Order (T, Reversed);
8749 -- For a record type, also set reversed bit order. Note: if a bit
8750 -- order has been specified explicitly, then this is a no-op.
8752 if Is_Record_Type (T) then
8753 Set_Reverse_Bit_Order (T, Reversed);
8754 end if;
8755 end if;
8756 end if;
8757 end Set_SSO_From_Default;
8759 ------------------
8760 -- Undelay_Type --
8761 ------------------
8763 procedure Undelay_Type (T : Entity_Id) is
8764 begin
8765 Set_Has_Delayed_Freeze (T, False);
8766 Set_Freeze_Node (T, Empty);
8768 -- Since we don't want T to have a Freeze_Node, we don't want its
8769 -- Full_View or Corresponding_Record_Type to have one either.
8771 -- ??? Fundamentally, this whole handling is unpleasant. What we really
8772 -- want is to be sure that for an Itype that's part of record R and is a
8773 -- subtype of type T, that it's frozen after the later of the freeze
8774 -- points of R and T. We have no way of doing that directly, so what we
8775 -- do is force most such Itypes to be frozen as part of freezing R via
8776 -- this procedure and only delay the ones that need to be delayed
8777 -- (mostly the designated types of access types that are defined as part
8778 -- of the record).
8780 if Is_Private_Type (T)
8781 and then Present (Full_View (T))
8782 and then Is_Itype (Full_View (T))
8783 and then Is_Record_Type (Scope (Full_View (T)))
8784 then
8785 Undelay_Type (Full_View (T));
8786 end if;
8788 if Is_Concurrent_Type (T)
8789 and then Present (Corresponding_Record_Type (T))
8790 and then Is_Itype (Corresponding_Record_Type (T))
8791 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
8792 then
8793 Undelay_Type (Corresponding_Record_Type (T));
8794 end if;
8795 end Undelay_Type;
8797 ------------------
8798 -- Warn_Overlay --
8799 ------------------
8801 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Entity_Id) is
8802 Ent : constant Entity_Id := Entity (Nam);
8803 -- The object to which the address clause applies
8805 Init : Node_Id;
8806 Old : Entity_Id := Empty;
8807 Decl : Node_Id;
8809 begin
8810 -- No warning if address clause overlay warnings are off
8812 if not Address_Clause_Overlay_Warnings then
8813 return;
8814 end if;
8816 -- No warning if there is an explicit initialization
8818 Init := Original_Node (Expression (Declaration_Node (Ent)));
8820 if Present (Init) and then Comes_From_Source (Init) then
8821 return;
8822 end if;
8824 -- We only give the warning for non-imported entities of a type for
8825 -- which a non-null base init proc is defined, or for objects of access
8826 -- types with implicit null initialization, or when Normalize_Scalars
8827 -- applies and the type is scalar or a string type (the latter being
8828 -- tested for because predefined String types are initialized by inline
8829 -- code rather than by an init_proc). Note that we do not give the
8830 -- warning for Initialize_Scalars, since we suppressed initialization
8831 -- in this case. Also, do not warn if Suppress_Initialization is set
8832 -- either on the type, or on the object via pragma or aspect.
8834 if Present (Expr)
8835 and then not Is_Imported (Ent)
8836 and then not Initialization_Suppressed (Typ)
8837 and then not (Ekind (Ent) = E_Variable
8838 and then Initialization_Suppressed (Ent))
8839 and then (Has_Non_Null_Base_Init_Proc (Typ)
8840 or else Is_Access_Type (Typ)
8841 or else (Normalize_Scalars
8842 and then (Is_Scalar_Type (Typ)
8843 or else Is_String_Type (Typ))))
8844 then
8845 if Nkind (Expr) = N_Attribute_Reference
8846 and then Is_Entity_Name (Prefix (Expr))
8847 then
8848 Old := Entity (Prefix (Expr));
8850 elsif Is_Entity_Name (Expr)
8851 and then Ekind (Entity (Expr)) = E_Constant
8852 then
8853 Decl := Declaration_Node (Entity (Expr));
8855 if Nkind (Decl) = N_Object_Declaration
8856 and then Present (Expression (Decl))
8857 and then Nkind (Expression (Decl)) = N_Attribute_Reference
8858 and then Is_Entity_Name (Prefix (Expression (Decl)))
8859 then
8860 Old := Entity (Prefix (Expression (Decl)));
8862 elsif Nkind (Expr) = N_Function_Call then
8863 return;
8864 end if;
8866 -- A function call (most likely to To_Address) is probably not an
8867 -- overlay, so skip warning. Ditto if the function call was inlined
8868 -- and transformed into an entity.
8870 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
8871 return;
8872 end if;
8874 -- If a pragma Import follows, we assume that it is for the current
8875 -- target of the address clause, and skip the warning. There may be
8876 -- a source pragma or an aspect that specifies import and generates
8877 -- the corresponding pragma. These will indicate that the entity is
8878 -- imported and that is checked above so that the spurious warning
8879 -- (generated when the entity is frozen) will be suppressed. The
8880 -- pragma may be attached to the aspect, so it is not yet a list
8881 -- member.
8883 if Is_List_Member (Parent (Expr)) then
8884 Decl := Next (Parent (Expr));
8886 if Present (Decl)
8887 and then Nkind (Decl) = N_Pragma
8888 and then Pragma_Name (Decl) = Name_Import
8889 then
8890 return;
8891 end if;
8892 end if;
8894 -- Otherwise give warning message
8896 if Present (Old) then
8897 Error_Msg_Node_2 := Old;
8898 Error_Msg_N
8899 ("default initialization of & may modify &??",
8900 Nam);
8901 else
8902 Error_Msg_N
8903 ("default initialization of & may modify overlaid storage??",
8904 Nam);
8905 end if;
8907 -- Add friendly warning if initialization comes from a packed array
8908 -- component.
8910 if Is_Record_Type (Typ) then
8911 declare
8912 Comp : Entity_Id;
8914 begin
8915 Comp := First_Component (Typ);
8916 while Present (Comp) loop
8917 if Nkind (Parent (Comp)) = N_Component_Declaration
8918 and then Present (Expression (Parent (Comp)))
8919 then
8920 exit;
8921 elsif Is_Array_Type (Etype (Comp))
8922 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
8923 then
8924 Error_Msg_NE
8925 ("\packed array component& " &
8926 "will be initialized to zero??",
8927 Nam, Comp);
8928 exit;
8929 else
8930 Next_Component (Comp);
8931 end if;
8932 end loop;
8933 end;
8934 end if;
8936 Error_Msg_N
8937 ("\use pragma Import for & to " &
8938 "suppress initialization (RM B.1(24))??",
8939 Nam);
8940 end if;
8941 end Warn_Overlay;
8943 end Freeze;