* gcc.dg/Wtrampolines.c: XFAIL AIX.
[official-gcc.git] / gcc / ada / freeze.adb
blob96ae4e4c98c3d394115c1d77bed14fb9f960b697
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-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Pakd; use Exp_Pakd;
38 with Exp_Util; use Exp_Util;
39 with Exp_Tss; use Exp_Tss;
40 with Fname; use Fname;
41 with Ghost; use Ghost;
42 with Layout; use Layout;
43 with Lib; use Lib;
44 with Namet; use Namet;
45 with Nlists; use Nlists;
46 with Nmake; use Nmake;
47 with Opt; use Opt;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Sem; use Sem;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch7; use Sem_Ch7;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Mech; use Sem_Mech;
60 with Sem_Prag; use Sem_Prag;
61 with Sem_Res; use Sem_Res;
62 with Sem_Util; use Sem_Util;
63 with Sinfo; use Sinfo;
64 with Snames; use Snames;
65 with Stand; use Stand;
66 with Targparm; use Targparm;
67 with Tbuild; use Tbuild;
68 with Ttypes; use Ttypes;
69 with Uintp; use Uintp;
70 with Urealp; use Urealp;
71 with Warnsw; use Warnsw;
73 package body Freeze is
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
80 -- Typ is a type that is being frozen. If no size clause is given,
81 -- but a default Esize has been computed, then this default Esize is
82 -- adjusted up if necessary to be consistent with a given alignment,
83 -- but never to a value greater than Long_Long_Integer'Size. This
84 -- is used for all discrete types and for fixed-point types.
86 procedure Build_And_Analyze_Renamed_Body
87 (Decl : Node_Id;
88 New_S : Entity_Id;
89 After : in out Node_Id);
90 -- Build body for a renaming declaration, insert in tree and analyze
92 procedure Check_Address_Clause (E : Entity_Id);
93 -- Apply legality checks to address clauses for object declarations,
94 -- at the point the object is frozen. Also ensure any initialization is
95 -- performed only after the object has been frozen.
97 procedure Check_Component_Storage_Order
98 (Encl_Type : Entity_Id;
99 Comp : Entity_Id;
100 ADC : Node_Id;
101 Comp_ADC_Present : out Boolean);
102 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
103 -- clause, verify that the component type has an explicit and compatible
104 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
105 -- entity of the component under consideration. For an Encl_Type that
106 -- does not have a Scalar_Storage_Order attribute definition clause,
107 -- verify that the component also does not have such a clause.
108 -- ADC is the attribute definition clause if present (or Empty). On return,
109 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
110 -- attribute definition clause.
112 procedure Check_Debug_Info_Needed (T : Entity_Id);
113 -- As each entity is frozen, this routine is called to deal with the
114 -- setting of Debug_Info_Needed for the entity. This flag is set if
115 -- the entity comes from source, or if we are in Debug_Generated_Code
116 -- mode or if the -gnatdV debug flag is set. However, it never sets
117 -- the flag if Debug_Info_Off is set. This procedure also ensures that
118 -- subsidiary entities have the flag set as required.
120 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
121 -- When an expression function is frozen by a use of it, the expression
122 -- itself is frozen. Check that the expression does not include references
123 -- to deferred constants without completion. We report this at the freeze
124 -- point of the function, to provide a better error message.
126 -- In most cases the expression itself is frozen by the time the function
127 -- itself is frozen, because the formals will be frozen by then. However,
128 -- Attribute references to outer types are freeze points for those types;
129 -- this routine generates the required freeze nodes for them.
131 procedure Check_Inherited_Conditions (R : Entity_Id);
132 -- For a tagged derived type, create wrappers for inherited operations
133 -- that have a class-wide condition, so it can be properly rewritten if
134 -- it involves calls to other overriding primitives.
136 procedure Check_Strict_Alignment (E : Entity_Id);
137 -- E is a base type. If E is tagged or has a component that is aliased
138 -- or tagged or contains something this is aliased or tagged, set
139 -- Strict_Alignment.
141 procedure Check_Unsigned_Type (E : Entity_Id);
142 pragma Inline (Check_Unsigned_Type);
143 -- If E is a fixed-point or discrete type, then all the necessary work
144 -- to freeze it is completed except for possible setting of the flag
145 -- Is_Unsigned_Type, which is done by this procedure. The call has no
146 -- effect if the entity E is not a discrete or fixed-point type.
148 procedure Freeze_And_Append
149 (Ent : Entity_Id;
150 N : Node_Id;
151 Result : in out List_Id);
152 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
153 -- nodes to Result, modifying Result from No_List if necessary. N has
154 -- the same usage as in Freeze_Entity.
156 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
157 -- Freeze enumeration type. The Esize field is set as processing
158 -- proceeds (i.e. set by default when the type is declared and then
159 -- adjusted by rep clauses. What this procedure does is to make sure
160 -- that if a foreign convention is specified, and no specific size
161 -- is given, then the size must be at least Integer'Size.
163 procedure Freeze_Static_Object (E : Entity_Id);
164 -- If an object is frozen which has Is_Statically_Allocated set, then
165 -- all referenced types must also be marked with this flag. This routine
166 -- is in charge of meeting this requirement for the object entity E.
168 procedure Freeze_Subprogram (E : Entity_Id);
169 -- Perform freezing actions for a subprogram (create extra formals,
170 -- and set proper default mechanism values). Note that this routine
171 -- is not called for internal subprograms, for which neither of these
172 -- actions is needed (or desirable, we do not want for example to have
173 -- these extra formals present in initialization procedures, where they
174 -- would serve no purpose). In this call E is either a subprogram or
175 -- a subprogram type (i.e. an access to a subprogram).
177 function Is_Fully_Defined (T : Entity_Id) return Boolean;
178 -- True if T is not private and has no private components, or has a full
179 -- view. Used to determine whether the designated type of an access type
180 -- should be frozen when the access type is frozen. This is done when an
181 -- allocator is frozen, or an expression that may involve attributes of
182 -- the designated type. Otherwise freezing the access type does not freeze
183 -- the designated type.
185 procedure Process_Default_Expressions
186 (E : Entity_Id;
187 After : in out Node_Id);
188 -- This procedure is called for each subprogram to complete processing of
189 -- default expressions at the point where all types are known to be frozen.
190 -- The expressions must be analyzed in full, to make sure that all error
191 -- processing is done (they have only been pre-analyzed). If the expression
192 -- is not an entity or literal, its analysis may generate code which must
193 -- not be executed. In that case we build a function body to hold that
194 -- code. This wrapper function serves no other purpose (it used to be
195 -- called to evaluate the default, but now the default is inlined at each
196 -- point of call).
198 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
199 -- Typ is a record or array type that is being frozen. This routine sets
200 -- the default component alignment from the scope stack values if the
201 -- alignment is otherwise not specified.
203 procedure Set_SSO_From_Default (T : Entity_Id);
204 -- T is a record or array type that is being frozen. If it is a base type,
205 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
206 -- will be set appropriately. Note that an explicit occurrence of aspect
207 -- Scalar_Storage_Order or an explicit setting of this aspect with an
208 -- attribute definition clause occurs, then these two flags are reset in
209 -- any case, so call will have no effect.
211 procedure Undelay_Type (T : Entity_Id);
212 -- T is a type of a component that we know to be an Itype. We don't want
213 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
214 -- Full_View or Corresponding_Record_Type.
216 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
217 -- Expr is the expression for an address clause for entity Nam whose type
218 -- is Typ. If Typ has a default initialization, and there is no explicit
219 -- initialization in the source declaration, check whether the address
220 -- clause might cause overlaying of an entity, and emit a warning on the
221 -- side effect that the initialization will cause.
223 -------------------------------
224 -- Adjust_Esize_For_Alignment --
225 -------------------------------
227 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
228 Align : Uint;
230 begin
231 if Known_Esize (Typ) and then Known_Alignment (Typ) then
232 Align := Alignment_In_Bits (Typ);
234 if Align > Esize (Typ)
235 and then Align <= Standard_Long_Long_Integer_Size
236 then
237 Set_Esize (Typ, Align);
238 end if;
239 end if;
240 end Adjust_Esize_For_Alignment;
242 ------------------------------------
243 -- Build_And_Analyze_Renamed_Body --
244 ------------------------------------
246 procedure Build_And_Analyze_Renamed_Body
247 (Decl : Node_Id;
248 New_S : Entity_Id;
249 After : in out Node_Id)
251 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
252 Ent : constant Entity_Id := Defining_Entity (Decl);
253 Body_Node : Node_Id;
254 Renamed_Subp : Entity_Id;
256 begin
257 -- If the renamed subprogram is intrinsic, there is no need for a
258 -- wrapper body: we set the alias that will be called and expanded which
259 -- completes the declaration. This transformation is only legal if the
260 -- renamed entity has already been elaborated.
262 -- Note that it is legal for a renaming_as_body to rename an intrinsic
263 -- subprogram, as long as the renaming occurs before the new entity
264 -- is frozen (RM 8.5.4 (5)).
266 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
267 and then Is_Entity_Name (Name (Body_Decl))
268 then
269 Renamed_Subp := Entity (Name (Body_Decl));
270 else
271 Renamed_Subp := Empty;
272 end if;
274 if Present (Renamed_Subp)
275 and then Is_Intrinsic_Subprogram (Renamed_Subp)
276 and then
277 (not In_Same_Source_Unit (Renamed_Subp, Ent)
278 or else Sloc (Renamed_Subp) < Sloc (Ent))
280 -- We can make the renaming entity intrinsic if the renamed function
281 -- has an interface name, or if it is one of the shift/rotate
282 -- operations known to the compiler.
284 and then
285 (Present (Interface_Name (Renamed_Subp))
286 or else Nam_In (Chars (Renamed_Subp), Name_Rotate_Left,
287 Name_Rotate_Right,
288 Name_Shift_Left,
289 Name_Shift_Right,
290 Name_Shift_Right_Arithmetic))
291 then
292 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
294 if Present (Alias (Renamed_Subp)) then
295 Set_Alias (Ent, Alias (Renamed_Subp));
296 else
297 Set_Alias (Ent, Renamed_Subp);
298 end if;
300 Set_Is_Intrinsic_Subprogram (Ent);
301 Set_Has_Completion (Ent);
303 else
304 Body_Node := Build_Renamed_Body (Decl, New_S);
305 Insert_After (After, Body_Node);
306 Mark_Rewrite_Insertion (Body_Node);
307 Analyze (Body_Node);
308 After := Body_Node;
309 end if;
310 end Build_And_Analyze_Renamed_Body;
312 ------------------------
313 -- Build_Renamed_Body --
314 ------------------------
316 function Build_Renamed_Body
317 (Decl : Node_Id;
318 New_S : Entity_Id) return Node_Id
320 Loc : constant Source_Ptr := Sloc (New_S);
321 -- We use for the source location of the renamed body, the location of
322 -- the spec entity. It might seem more natural to use the location of
323 -- the renaming declaration itself, but that would be wrong, since then
324 -- the body we create would look as though it was created far too late,
325 -- and this could cause problems with elaboration order analysis,
326 -- particularly in connection with instantiations.
328 N : constant Node_Id := Unit_Declaration_Node (New_S);
329 Nam : constant Node_Id := Name (N);
330 Old_S : Entity_Id;
331 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
332 Actuals : List_Id := No_List;
333 Call_Node : Node_Id;
334 Call_Name : Node_Id;
335 Body_Node : Node_Id;
336 Formal : Entity_Id;
337 O_Formal : Entity_Id;
338 Param_Spec : Node_Id;
340 Pref : Node_Id := Empty;
341 -- If the renamed entity is a primitive operation given in prefix form,
342 -- the prefix is the target object and it has to be added as the first
343 -- actual in the generated call.
345 begin
346 -- Determine the entity being renamed, which is the target of the call
347 -- statement. If the name is an explicit dereference, this is a renaming
348 -- of a subprogram type rather than a subprogram. The name itself is
349 -- fully analyzed.
351 if Nkind (Nam) = N_Selected_Component then
352 Old_S := Entity (Selector_Name (Nam));
354 elsif Nkind (Nam) = N_Explicit_Dereference then
355 Old_S := Etype (Nam);
357 elsif Nkind (Nam) = N_Indexed_Component then
358 if Is_Entity_Name (Prefix (Nam)) then
359 Old_S := Entity (Prefix (Nam));
360 else
361 Old_S := Entity (Selector_Name (Prefix (Nam)));
362 end if;
364 elsif Nkind (Nam) = N_Character_Literal then
365 Old_S := Etype (New_S);
367 else
368 Old_S := Entity (Nam);
369 end if;
371 if Is_Entity_Name (Nam) then
373 -- If the renamed entity is a predefined operator, retain full name
374 -- to ensure its visibility.
376 if Ekind (Old_S) = E_Operator
377 and then Nkind (Nam) = N_Expanded_Name
378 then
379 Call_Name := New_Copy (Name (N));
380 else
381 Call_Name := New_Occurrence_Of (Old_S, Loc);
382 end if;
384 else
385 if Nkind (Nam) = N_Selected_Component
386 and then Present (First_Formal (Old_S))
387 and then
388 (Is_Controlling_Formal (First_Formal (Old_S))
389 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
390 then
392 -- Retrieve the target object, to be added as a first actual
393 -- in the call.
395 Call_Name := New_Occurrence_Of (Old_S, Loc);
396 Pref := Prefix (Nam);
398 else
399 Call_Name := New_Copy (Name (N));
400 end if;
402 -- Original name may have been overloaded, but is fully resolved now
404 Set_Is_Overloaded (Call_Name, False);
405 end if;
407 -- For simple renamings, subsequent calls can be expanded directly as
408 -- calls to the renamed entity. The body must be generated in any case
409 -- for calls that may appear elsewhere. This is not done in the case
410 -- where the subprogram is an instantiation because the actual proper
411 -- body has not been built yet.
413 if Ekind_In (Old_S, E_Function, E_Procedure)
414 and then Nkind (Decl) = N_Subprogram_Declaration
415 and then not Is_Generic_Instance (Old_S)
416 then
417 Set_Body_To_Inline (Decl, Old_S);
418 end if;
420 -- Check whether the return type is a limited view. If the subprogram
421 -- is already frozen the generated body may have a non-limited view
422 -- of the type, that must be used, because it is the one in the spec
423 -- of the renaming declaration.
425 if Ekind (Old_S) = E_Function
426 and then Is_Entity_Name (Result_Definition (Spec))
427 then
428 declare
429 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
430 begin
431 if Has_Non_Limited_View (Ret_Type) then
432 Set_Result_Definition
433 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
434 end if;
435 end;
436 end if;
438 -- The body generated for this renaming is an internal artifact, and
439 -- does not constitute a freeze point for the called entity.
441 Set_Must_Not_Freeze (Call_Name);
443 Formal := First_Formal (Defining_Entity (Decl));
445 if Present (Pref) then
446 declare
447 Pref_Type : constant Entity_Id := Etype (Pref);
448 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
450 begin
451 -- The controlling formal may be an access parameter, or the
452 -- actual may be an access value, so adjust accordingly.
454 if Is_Access_Type (Pref_Type)
455 and then not Is_Access_Type (Form_Type)
456 then
457 Actuals := New_List
458 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
460 elsif Is_Access_Type (Form_Type)
461 and then not Is_Access_Type (Pref)
462 then
463 Actuals :=
464 New_List (
465 Make_Attribute_Reference (Loc,
466 Attribute_Name => Name_Access,
467 Prefix => Relocate_Node (Pref)));
468 else
469 Actuals := New_List (Pref);
470 end if;
471 end;
473 elsif Present (Formal) then
474 Actuals := New_List;
476 else
477 Actuals := No_List;
478 end if;
480 if Present (Formal) then
481 while Present (Formal) loop
482 Append (New_Occurrence_Of (Formal, Loc), Actuals);
483 Next_Formal (Formal);
484 end loop;
485 end if;
487 -- If the renamed entity is an entry, inherit its profile. For other
488 -- renamings as bodies, both profiles must be subtype conformant, so it
489 -- is not necessary to replace the profile given in the declaration.
490 -- However, default values that are aggregates are rewritten when
491 -- partially analyzed, so we recover the original aggregate to insure
492 -- that subsequent conformity checking works. Similarly, if the default
493 -- expression was constant-folded, recover the original expression.
495 Formal := First_Formal (Defining_Entity (Decl));
497 if Present (Formal) then
498 O_Formal := First_Formal (Old_S);
499 Param_Spec := First (Parameter_Specifications (Spec));
500 while Present (Formal) loop
501 if Is_Entry (Old_S) then
502 if Nkind (Parameter_Type (Param_Spec)) /=
503 N_Access_Definition
504 then
505 Set_Etype (Formal, Etype (O_Formal));
506 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
507 end if;
509 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
510 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
511 Nkind (Default_Value (O_Formal))
512 then
513 Set_Expression (Param_Spec,
514 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
515 end if;
517 Next_Formal (Formal);
518 Next_Formal (O_Formal);
519 Next (Param_Spec);
520 end loop;
521 end if;
523 -- If the renamed entity is a function, the generated body contains a
524 -- return statement. Otherwise, build a procedure call. If the entity is
525 -- an entry, subsequent analysis of the call will transform it into the
526 -- proper entry or protected operation call. If the renamed entity is
527 -- a character literal, return it directly.
529 if Ekind (Old_S) = E_Function
530 or else Ekind (Old_S) = E_Operator
531 or else (Ekind (Old_S) = E_Subprogram_Type
532 and then Etype (Old_S) /= Standard_Void_Type)
533 then
534 Call_Node :=
535 Make_Simple_Return_Statement (Loc,
536 Expression =>
537 Make_Function_Call (Loc,
538 Name => Call_Name,
539 Parameter_Associations => Actuals));
541 elsif Ekind (Old_S) = E_Enumeration_Literal then
542 Call_Node :=
543 Make_Simple_Return_Statement (Loc,
544 Expression => New_Occurrence_Of (Old_S, Loc));
546 elsif Nkind (Nam) = N_Character_Literal then
547 Call_Node :=
548 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
550 else
551 Call_Node :=
552 Make_Procedure_Call_Statement (Loc,
553 Name => Call_Name,
554 Parameter_Associations => Actuals);
555 end if;
557 -- Create entities for subprogram body and formals
559 Set_Defining_Unit_Name (Spec,
560 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
562 Param_Spec := First (Parameter_Specifications (Spec));
563 while Present (Param_Spec) loop
564 Set_Defining_Identifier (Param_Spec,
565 Make_Defining_Identifier (Loc,
566 Chars => Chars (Defining_Identifier (Param_Spec))));
567 Next (Param_Spec);
568 end loop;
570 Body_Node :=
571 Make_Subprogram_Body (Loc,
572 Specification => Spec,
573 Declarations => New_List,
574 Handled_Statement_Sequence =>
575 Make_Handled_Sequence_Of_Statements (Loc,
576 Statements => New_List (Call_Node)));
578 if Nkind (Decl) /= N_Subprogram_Declaration then
579 Rewrite (N,
580 Make_Subprogram_Declaration (Loc,
581 Specification => Specification (N)));
582 end if;
584 -- Link the body to the entity whose declaration it completes. If
585 -- the body is analyzed when the renamed entity is frozen, it may
586 -- be necessary to restore the proper scope (see package Exp_Ch13).
588 if Nkind (N) = N_Subprogram_Renaming_Declaration
589 and then Present (Corresponding_Spec (N))
590 then
591 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
592 else
593 Set_Corresponding_Spec (Body_Node, New_S);
594 end if;
596 return Body_Node;
597 end Build_Renamed_Body;
599 --------------------------
600 -- Check_Address_Clause --
601 --------------------------
603 procedure Check_Address_Clause (E : Entity_Id) is
604 Addr : constant Node_Id := Address_Clause (E);
605 Typ : constant Entity_Id := Etype (E);
606 Decl : Node_Id;
607 Expr : Node_Id;
608 Init : Node_Id;
609 Lhs : Node_Id;
610 Tag_Assign : Node_Id;
612 begin
613 if Present (Addr) then
615 -- For a deferred constant, the initialization value is on full view
617 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
618 Decl := Declaration_Node (Full_View (E));
619 else
620 Decl := Declaration_Node (E);
621 end if;
623 Expr := Expression (Addr);
625 if Needs_Constant_Address (Decl, Typ) then
626 Check_Constant_Address_Clause (Expr, E);
628 -- Has_Delayed_Freeze was set on E when the address clause was
629 -- analyzed, and must remain set because we want the address
630 -- clause to be elaborated only after any entity it references
631 -- has been elaborated.
632 end if;
634 -- If Rep_Clauses are to be ignored, remove address clause from
635 -- list attached to entity, because it may be illegal for gigi,
636 -- for example by breaking order of elaboration..
638 if Ignore_Rep_Clauses then
639 declare
640 Rep : Node_Id;
642 begin
643 Rep := First_Rep_Item (E);
645 if Rep = Addr then
646 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
648 else
649 while Present (Rep)
650 and then Next_Rep_Item (Rep) /= Addr
651 loop
652 Rep := Next_Rep_Item (Rep);
653 end loop;
654 end if;
656 if Present (Rep) then
657 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
658 end if;
659 end;
661 -- And now remove the address clause
663 Kill_Rep_Clause (Addr);
665 elsif not Error_Posted (Expr)
666 and then not Needs_Finalization (Typ)
667 then
668 Warn_Overlay (Expr, Typ, Name (Addr));
669 end if;
671 Init := Expression (Decl);
673 -- If a variable, or a non-imported constant, overlays a constant
674 -- object and has an initialization value, then the initialization
675 -- may end up writing into read-only memory. Detect the cases of
676 -- statically identical values and remove the initialization. In
677 -- the other cases, give a warning. We will give other warnings
678 -- later for the variable if it is assigned.
680 if (Ekind (E) = E_Variable
681 or else (Ekind (E) = E_Constant
682 and then not Is_Imported (E)))
683 and then Overlays_Constant (E)
684 and then Present (Init)
685 then
686 declare
687 O_Ent : Entity_Id;
688 Off : Boolean;
690 begin
691 Find_Overlaid_Entity (Addr, O_Ent, Off);
693 if Ekind (O_Ent) = E_Constant
694 and then Etype (O_Ent) = Typ
695 and then Present (Constant_Value (O_Ent))
696 and then Compile_Time_Compare
697 (Init,
698 Constant_Value (O_Ent),
699 Assume_Valid => True) = EQ
700 then
701 Set_No_Initialization (Decl);
702 return;
704 elsif Comes_From_Source (Init)
705 and then Address_Clause_Overlay_Warnings
706 then
707 Error_Msg_Sloc := Sloc (Addr);
708 Error_Msg_NE
709 ("??constant& may be modified via address clause#",
710 Decl, O_Ent);
711 end if;
712 end;
713 end if;
715 if Present (Init) then
717 -- Capture initialization value at point of declaration,
718 -- and make explicit assignment legal, because object may
719 -- be a constant.
721 Remove_Side_Effects (Init);
722 Lhs := New_Occurrence_Of (E, Sloc (Decl));
723 Set_Assignment_OK (Lhs);
725 -- Move initialization to freeze actions, once the object has
726 -- been frozen and the address clause alignment check has been
727 -- performed.
729 Append_Freeze_Action (E,
730 Make_Assignment_Statement (Sloc (Decl),
731 Name => Lhs,
732 Expression => Expression (Decl)));
734 Set_No_Initialization (Decl);
736 -- If the objet is tagged, check whether the tag must be
737 -- reassigned explicitly.
739 Tag_Assign := Make_Tag_Assignment (Decl);
740 if Present (Tag_Assign) then
741 Append_Freeze_Action (E, Tag_Assign);
742 end if;
743 end if;
744 end if;
745 end Check_Address_Clause;
747 -----------------------------
748 -- Check_Compile_Time_Size --
749 -----------------------------
751 procedure Check_Compile_Time_Size (T : Entity_Id) is
753 procedure Set_Small_Size (T : Entity_Id; S : Uint);
754 -- Sets the compile time known size (64 bits or less) in the RM_Size
755 -- field of T, checking for a size clause that was given which attempts
756 -- to give a smaller size.
758 function Size_Known (T : Entity_Id) return Boolean;
759 -- Recursive function that does all the work
761 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
762 -- If T is a constrained subtype, its size is not known if any of its
763 -- discriminant constraints is not static and it is not a null record.
764 -- The test is conservative and doesn't check that the components are
765 -- in fact constrained by non-static discriminant values. Could be made
766 -- more precise ???
768 --------------------
769 -- Set_Small_Size --
770 --------------------
772 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
773 begin
774 if S > 64 then
775 return;
777 -- Check for bad size clause given
779 elsif Has_Size_Clause (T) then
780 if RM_Size (T) < S then
781 Error_Msg_Uint_1 := S;
782 Error_Msg_NE
783 ("size for& too small, minimum allowed is ^",
784 Size_Clause (T), T);
785 end if;
787 -- Set size if not set already
789 elsif Unknown_RM_Size (T) then
790 Set_RM_Size (T, S);
791 end if;
792 end Set_Small_Size;
794 ----------------
795 -- Size_Known --
796 ----------------
798 function Size_Known (T : Entity_Id) return Boolean is
799 Index : Entity_Id;
800 Comp : Entity_Id;
801 Ctyp : Entity_Id;
802 Low : Node_Id;
803 High : Node_Id;
805 begin
806 if Size_Known_At_Compile_Time (T) then
807 return True;
809 -- Always True for elementary types, even generic formal elementary
810 -- types. We used to return False in the latter case, but the size
811 -- is known at compile time, even in the template, we just do not
812 -- know the exact size but that's not the point of this routine.
814 elsif Is_Elementary_Type (T) or else Is_Task_Type (T) then
815 return True;
817 -- Array types
819 elsif Is_Array_Type (T) then
821 -- String literals always have known size, and we can set it
823 if Ekind (T) = E_String_Literal_Subtype then
824 Set_Small_Size
825 (T, Component_Size (T) * String_Literal_Length (T));
826 return True;
828 -- Unconstrained types never have known at compile time size
830 elsif not Is_Constrained (T) then
831 return False;
833 -- Don't do any recursion on type with error posted, since we may
834 -- have a malformed type that leads us into a loop.
836 elsif Error_Posted (T) then
837 return False;
839 -- Otherwise if component size unknown, then array size unknown
841 elsif not Size_Known (Component_Type (T)) then
842 return False;
843 end if;
845 -- Check for all indexes static, and also compute possible size
846 -- (in case it is not greater than 64 and may be packable).
848 declare
849 Size : Uint := Component_Size (T);
850 Dim : Uint;
852 begin
853 Index := First_Index (T);
854 while Present (Index) loop
855 if Nkind (Index) = N_Range then
856 Get_Index_Bounds (Index, Low, High);
858 elsif Error_Posted (Scalar_Range (Etype (Index))) then
859 return False;
861 else
862 Low := Type_Low_Bound (Etype (Index));
863 High := Type_High_Bound (Etype (Index));
864 end if;
866 if not Compile_Time_Known_Value (Low)
867 or else not Compile_Time_Known_Value (High)
868 or else Etype (Index) = Any_Type
869 then
870 return False;
872 else
873 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
875 if Dim >= 0 then
876 Size := Size * Dim;
877 else
878 Size := Uint_0;
879 end if;
880 end if;
882 Next_Index (Index);
883 end loop;
885 Set_Small_Size (T, Size);
886 return True;
887 end;
889 -- For non-generic private types, go to underlying type if present
891 elsif Is_Private_Type (T)
892 and then not Is_Generic_Type (T)
893 and then Present (Underlying_Type (T))
894 then
895 -- Don't do any recursion on type with error posted, since we may
896 -- have a malformed type that leads us into a loop.
898 if Error_Posted (T) then
899 return False;
900 else
901 return Size_Known (Underlying_Type (T));
902 end if;
904 -- Record types
906 elsif Is_Record_Type (T) then
908 -- A class-wide type is never considered to have a known size
910 if Is_Class_Wide_Type (T) then
911 return False;
913 -- A subtype of a variant record must not have non-static
914 -- discriminated components.
916 elsif T /= Base_Type (T)
917 and then not Static_Discriminated_Components (T)
918 then
919 return False;
921 -- Don't do any recursion on type with error posted, since we may
922 -- have a malformed type that leads us into a loop.
924 elsif Error_Posted (T) then
925 return False;
926 end if;
928 -- Now look at the components of the record
930 declare
931 -- The following two variables are used to keep track of the
932 -- size of packed records if we can tell the size of the packed
933 -- record in the front end. Packed_Size_Known is True if so far
934 -- we can figure out the size. It is initialized to True for a
935 -- packed record, unless the record has discriminants or atomic
936 -- components or independent components.
938 -- The reason we eliminate the discriminated case is that
939 -- we don't know the way the back end lays out discriminated
940 -- packed records. If Packed_Size_Known is True, then
941 -- Packed_Size is the size in bits so far.
943 Packed_Size_Known : Boolean :=
944 Is_Packed (T)
945 and then not Has_Discriminants (T)
946 and then not Has_Atomic_Components (T)
947 and then not Has_Independent_Components (T);
949 Packed_Size : Uint := Uint_0;
950 -- Size in bits so far
952 begin
953 -- Test for variant part present
955 if Has_Discriminants (T)
956 and then Present (Parent (T))
957 and then Nkind (Parent (T)) = N_Full_Type_Declaration
958 and then Nkind (Type_Definition (Parent (T))) =
959 N_Record_Definition
960 and then not Null_Present (Type_Definition (Parent (T)))
961 and then
962 Present (Variant_Part
963 (Component_List (Type_Definition (Parent (T)))))
964 then
965 -- If variant part is present, and type is unconstrained,
966 -- then we must have defaulted discriminants, or a size
967 -- clause must be present for the type, or else the size
968 -- is definitely not known at compile time.
970 if not Is_Constrained (T)
971 and then
972 No (Discriminant_Default_Value (First_Discriminant (T)))
973 and then Unknown_RM_Size (T)
974 then
975 return False;
976 end if;
977 end if;
979 -- Loop through components
981 Comp := First_Component_Or_Discriminant (T);
982 while Present (Comp) loop
983 Ctyp := Etype (Comp);
985 -- We do not know the packed size if there is a component
986 -- clause present (we possibly could, but this would only
987 -- help in the case of a record with partial rep clauses.
988 -- That's because in the case of full rep clauses, the
989 -- size gets figured out anyway by a different circuit).
991 if Present (Component_Clause (Comp)) then
992 Packed_Size_Known := False;
993 end if;
995 -- We do not know the packed size for an atomic/VFA type
996 -- or component, or an independent type or component, or a
997 -- by-reference type or aliased component (because packing
998 -- does not touch these).
1000 if Is_Atomic_Or_VFA (Ctyp)
1001 or else Is_Atomic_Or_VFA (Comp)
1002 or else Is_Independent (Ctyp)
1003 or else Is_Independent (Comp)
1004 or else Is_By_Reference_Type (Ctyp)
1005 or else Is_Aliased (Comp)
1006 then
1007 Packed_Size_Known := False;
1008 end if;
1010 -- We need to identify a component that is an array where
1011 -- the index type is an enumeration type with non-standard
1012 -- representation, and some bound of the type depends on a
1013 -- discriminant.
1015 -- This is because gigi computes the size by doing a
1016 -- substitution of the appropriate discriminant value in
1017 -- the size expression for the base type, and gigi is not
1018 -- clever enough to evaluate the resulting expression (which
1019 -- involves a call to rep_to_pos) at compile time.
1021 -- It would be nice if gigi would either recognize that
1022 -- this expression can be computed at compile time, or
1023 -- alternatively figured out the size from the subtype
1024 -- directly, where all the information is at hand ???
1026 if Is_Array_Type (Etype (Comp))
1027 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1028 then
1029 declare
1030 Ocomp : constant Entity_Id :=
1031 Original_Record_Component (Comp);
1032 OCtyp : constant Entity_Id := Etype (Ocomp);
1033 Ind : Node_Id;
1034 Indtyp : Entity_Id;
1035 Lo, Hi : Node_Id;
1037 begin
1038 Ind := First_Index (OCtyp);
1039 while Present (Ind) loop
1040 Indtyp := Etype (Ind);
1042 if Is_Enumeration_Type (Indtyp)
1043 and then Has_Non_Standard_Rep (Indtyp)
1044 then
1045 Lo := Type_Low_Bound (Indtyp);
1046 Hi := Type_High_Bound (Indtyp);
1048 if Is_Entity_Name (Lo)
1049 and then Ekind (Entity (Lo)) = E_Discriminant
1050 then
1051 return False;
1053 elsif Is_Entity_Name (Hi)
1054 and then Ekind (Entity (Hi)) = E_Discriminant
1055 then
1056 return False;
1057 end if;
1058 end if;
1060 Next_Index (Ind);
1061 end loop;
1062 end;
1063 end if;
1065 -- Clearly size of record is not known if the size of one of
1066 -- the components is not known.
1068 if not Size_Known (Ctyp) then
1069 return False;
1070 end if;
1072 -- Accumulate packed size if possible
1074 if Packed_Size_Known then
1076 -- We can deal with elementary types, small packed arrays
1077 -- if the representation is a modular type and also small
1078 -- record types (if the size is not greater than 64, but
1079 -- the condition is checked by Set_Small_Size).
1081 if Is_Elementary_Type (Ctyp)
1082 or else (Is_Array_Type (Ctyp)
1083 and then Present
1084 (Packed_Array_Impl_Type (Ctyp))
1085 and then Is_Modular_Integer_Type
1086 (Packed_Array_Impl_Type (Ctyp)))
1087 or else Is_Record_Type (Ctyp)
1088 then
1089 -- If RM_Size is known and static, then we can keep
1090 -- accumulating the packed size.
1092 if Known_Static_RM_Size (Ctyp) then
1094 Packed_Size := Packed_Size + RM_Size (Ctyp);
1096 -- If we have a field whose RM_Size is not known then
1097 -- we can't figure out the packed size here.
1099 else
1100 Packed_Size_Known := False;
1101 end if;
1103 -- For other types we can't figure out the packed size
1105 else
1106 Packed_Size_Known := False;
1107 end if;
1108 end if;
1110 Next_Component_Or_Discriminant (Comp);
1111 end loop;
1113 if Packed_Size_Known then
1114 Set_Small_Size (T, Packed_Size);
1115 end if;
1117 return True;
1118 end;
1120 -- All other cases, size not known at compile time
1122 else
1123 return False;
1124 end if;
1125 end Size_Known;
1127 -------------------------------------
1128 -- Static_Discriminated_Components --
1129 -------------------------------------
1131 function Static_Discriminated_Components
1132 (T : Entity_Id) return Boolean
1134 Constraint : Elmt_Id;
1136 begin
1137 if Has_Discriminants (T)
1138 and then Present (Discriminant_Constraint (T))
1139 and then Present (First_Component (T))
1140 then
1141 Constraint := First_Elmt (Discriminant_Constraint (T));
1142 while Present (Constraint) loop
1143 if not Compile_Time_Known_Value (Node (Constraint)) then
1144 return False;
1145 end if;
1147 Next_Elmt (Constraint);
1148 end loop;
1149 end if;
1151 return True;
1152 end Static_Discriminated_Components;
1154 -- Start of processing for Check_Compile_Time_Size
1156 begin
1157 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1158 end Check_Compile_Time_Size;
1160 -----------------------------------
1161 -- Check_Component_Storage_Order --
1162 -----------------------------------
1164 procedure Check_Component_Storage_Order
1165 (Encl_Type : Entity_Id;
1166 Comp : Entity_Id;
1167 ADC : Node_Id;
1168 Comp_ADC_Present : out Boolean)
1170 Comp_Base : Entity_Id;
1171 Comp_ADC : Node_Id;
1172 Encl_Base : Entity_Id;
1173 Err_Node : Node_Id;
1175 Component_Aliased : Boolean;
1177 Comp_Byte_Aligned : Boolean;
1178 -- Set for the record case, True if Comp starts on a byte boundary
1179 -- (in which case it is allowed to have different storage order).
1181 Comp_SSO_Differs : Boolean;
1182 -- Set True when the component is a nested composite, and it does not
1183 -- have the same scalar storage order as Encl_Type.
1185 begin
1186 -- Record case
1188 if Present (Comp) then
1189 Err_Node := Comp;
1190 Comp_Base := Etype (Comp);
1192 if Is_Tag (Comp) then
1193 Comp_Byte_Aligned := True;
1194 Component_Aliased := False;
1196 else
1197 -- If a component clause is present, check if the component starts
1198 -- on a storage element boundary. Otherwise conservatively assume
1199 -- it does so only in the case where the record is not packed.
1201 if Present (Component_Clause (Comp)) then
1202 Comp_Byte_Aligned :=
1203 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0;
1204 else
1205 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1206 end if;
1208 Component_Aliased := Is_Aliased (Comp);
1209 end if;
1211 -- Array case
1213 else
1214 Err_Node := Encl_Type;
1215 Comp_Base := Component_Type (Encl_Type);
1217 Component_Aliased := Has_Aliased_Components (Encl_Type);
1218 end if;
1220 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1221 -- the attribute definition clause is attached to the first subtype.
1222 -- Also, if the base type is incomplete or private, go to full view
1223 -- if known
1225 Encl_Base := Base_Type (Encl_Type);
1226 if Present (Underlying_Type (Encl_Base)) then
1227 Encl_Base := Underlying_Type (Encl_Base);
1228 end if;
1230 Comp_Base := Base_Type (Comp_Base);
1231 if Present (Underlying_Type (Comp_Base)) then
1232 Comp_Base := Underlying_Type (Comp_Base);
1233 end if;
1235 Comp_ADC :=
1236 Get_Attribute_Definition_Clause
1237 (First_Subtype (Comp_Base), Attribute_Scalar_Storage_Order);
1238 Comp_ADC_Present := Present (Comp_ADC);
1240 -- Case of record or array component: check storage order compatibility.
1241 -- But, if the record has Complex_Representation, then it is treated as
1242 -- a scalar in the back end so the storage order is irrelevant.
1244 if (Is_Record_Type (Comp_Base)
1245 and then not Has_Complex_Representation (Comp_Base))
1246 or else Is_Array_Type (Comp_Base)
1247 then
1248 Comp_SSO_Differs :=
1249 Reverse_Storage_Order (Encl_Base) /=
1250 Reverse_Storage_Order (Comp_Base);
1252 -- Parent and extension must have same storage order
1254 if Present (Comp) and then Chars (Comp) = Name_uParent then
1255 if Comp_SSO_Differs then
1256 Error_Msg_N
1257 ("record extension must have same scalar storage order as "
1258 & "parent", Err_Node);
1259 end if;
1261 -- If component and composite SSO differs, check that component
1262 -- falls on byte boundaries and isn't bit packed.
1264 elsif Comp_SSO_Differs then
1266 -- Component SSO differs from enclosing composite:
1268 -- Reject if component is a bit-packed array, as it is represented
1269 -- as a scalar internally.
1271 if Is_Bit_Packed_Array (Comp_Base) then
1272 Error_Msg_N
1273 ("type of packed component must have same scalar storage "
1274 & "order as enclosing composite", Err_Node);
1276 -- Reject if composite is a bit-packed array, as it is rewritten
1277 -- into an array of scalars.
1279 elsif Is_Bit_Packed_Array (Encl_Base) then
1280 Error_Msg_N
1281 ("type of packed array must have same scalar storage order "
1282 & "as component", Err_Node);
1284 -- Reject if not byte aligned
1286 elsif Is_Record_Type (Encl_Base)
1287 and then not Comp_Byte_Aligned
1288 then
1289 Error_Msg_N
1290 ("type of non-byte-aligned component must have same scalar "
1291 & "storage order as enclosing composite", Err_Node);
1293 -- Warn if specified only for the outer composite
1295 elsif Present (ADC) and then No (Comp_ADC) then
1296 Error_Msg_NE
1297 ("scalar storage order specified for & does not apply to "
1298 & "component?", Err_Node, Encl_Base);
1299 end if;
1300 end if;
1302 -- Enclosing type has explicit SSO: non-composite component must not
1303 -- be aliased.
1305 elsif Present (ADC) and then Component_Aliased then
1306 Error_Msg_N
1307 ("aliased component not permitted for type with explicit "
1308 & "Scalar_Storage_Order", Err_Node);
1309 end if;
1310 end Check_Component_Storage_Order;
1312 -----------------------------
1313 -- Check_Debug_Info_Needed --
1314 -----------------------------
1316 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1317 begin
1318 if Debug_Info_Off (T) then
1319 return;
1321 elsif Comes_From_Source (T)
1322 or else Debug_Generated_Code
1323 or else Debug_Flag_VV
1324 or else Needs_Debug_Info (T)
1325 then
1326 Set_Debug_Info_Needed (T);
1327 end if;
1328 end Check_Debug_Info_Needed;
1330 -------------------------------
1331 -- Check_Expression_Function --
1332 -------------------------------
1334 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1335 Decl : Node_Id;
1337 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1338 -- Function to search for deferred constant
1340 -------------------
1341 -- Find_Constant --
1342 -------------------
1344 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1345 begin
1346 -- When a constant is initialized with the result of a dispatching
1347 -- call, the constant declaration is rewritten as a renaming of the
1348 -- displaced function result. This scenario is not a premature use of
1349 -- a constant even though the Has_Completion flag is not set.
1351 if Is_Entity_Name (Nod)
1352 and then Present (Entity (Nod))
1353 and then Ekind (Entity (Nod)) = E_Constant
1354 and then Scope (Entity (Nod)) = Current_Scope
1355 and then Nkind (Declaration_Node (Entity (Nod))) =
1356 N_Object_Declaration
1357 and then not Is_Imported (Entity (Nod))
1358 and then not Has_Completion (Entity (Nod))
1359 then
1360 Error_Msg_NE
1361 ("premature use of& in call or instance", N, Entity (Nod));
1363 elsif Nkind (Nod) = N_Attribute_Reference then
1364 Analyze (Prefix (Nod));
1366 if Is_Entity_Name (Prefix (Nod))
1367 and then Is_Type (Entity (Prefix (Nod)))
1368 then
1369 Freeze_Before (N, Entity (Prefix (Nod)));
1370 end if;
1371 end if;
1373 return OK;
1374 end Find_Constant;
1376 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1378 -- Start of processing for Check_Expression_Function
1380 begin
1381 Decl := Original_Node (Unit_Declaration_Node (Nam));
1383 if Scope (Nam) = Current_Scope
1384 and then Nkind (Decl) = N_Expression_Function
1385 then
1386 Check_Deferred (Expression (Decl));
1387 end if;
1388 end Check_Expression_Function;
1390 --------------------------------
1391 -- Check_Inherited_Conditions --
1392 --------------------------------
1394 procedure Check_Inherited_Conditions (R : Entity_Id) is
1395 Prim_Ops : constant Elist_Id := Primitive_Operations (R);
1396 A_Post : Node_Id;
1397 A_Pre : Node_Id;
1398 Op_Node : Elmt_Id;
1399 Par_Prim : Entity_Id;
1400 Prim : Entity_Id;
1402 begin
1403 Op_Node := First_Elmt (Prim_Ops);
1404 while Present (Op_Node) loop
1405 Prim := Node (Op_Node);
1407 -- Map the overridden primitive to the overriding one. This takes
1408 -- care of all overridings and is done only once.
1410 if Present (Overridden_Operation (Prim))
1411 and then Comes_From_Source (Prim)
1412 then
1413 Update_Primitives_Mapping (Overridden_Operation (Prim), Prim);
1415 -- In SPARK mode this is where we can collect the inherited
1416 -- conditions, because we do not create the Check pragmas that
1417 -- normally convey the the modified class-wide conditions on
1418 -- overriding operations.
1420 if SPARK_Mode = On then
1422 -- Analyze the contract items of the parent operation, before
1423 -- they are rewritten when inherited.
1425 Analyze_Entry_Or_Subprogram_Contract
1426 (Overridden_Operation (Prim));
1428 -- Now verify the legality of inherited contracts for LSP
1429 -- conformance.
1431 Collect_Inherited_Class_Wide_Conditions (Prim);
1432 end if;
1433 end if;
1435 Next_Elmt (Op_Node);
1436 end loop;
1438 -- In all cases, we examine inherited operations to check whether they
1439 -- require a wrapper to handle inherited conditions that call other
1440 -- primitives, so that LSP can be verified/enforced.
1442 -- Wrapper construction TBD.
1444 Op_Node := First_Elmt (Prim_Ops);
1445 while Present (Op_Node) loop
1446 Prim := Node (Op_Node);
1447 if not Comes_From_Source (Prim) and then Present (Alias (Prim)) then
1448 Par_Prim := Alias (Prim);
1449 A_Pre := Find_Aspect (Par_Prim, Aspect_Pre);
1451 if Present (A_Pre) and then Class_Present (A_Pre) then
1452 Build_Class_Wide_Expression
1453 (Expression (A_Pre), Prim, Par_Prim, Adjust_Sloc => False);
1454 end if;
1456 A_Post := Find_Aspect (Par_Prim, Aspect_Post);
1458 if Present (A_Post) and then Class_Present (A_Post) then
1459 Build_Class_Wide_Expression
1460 (Expression (A_Post), Prim, Par_Prim, Adjust_Sloc => False);
1461 end if;
1462 end if;
1464 Next_Elmt (Op_Node);
1465 end loop;
1466 end Check_Inherited_Conditions;
1468 ----------------------------
1469 -- Check_Strict_Alignment --
1470 ----------------------------
1472 procedure Check_Strict_Alignment (E : Entity_Id) is
1473 Comp : Entity_Id;
1475 begin
1476 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1477 Set_Strict_Alignment (E);
1479 elsif Is_Array_Type (E) then
1480 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1482 elsif Is_Record_Type (E) then
1483 if Is_Limited_Record (E) then
1484 Set_Strict_Alignment (E);
1485 return;
1486 end if;
1488 Comp := First_Component (E);
1489 while Present (Comp) loop
1490 if not Is_Type (Comp)
1491 and then (Strict_Alignment (Etype (Comp))
1492 or else Is_Aliased (Comp))
1493 then
1494 Set_Strict_Alignment (E);
1495 return;
1496 end if;
1498 Next_Component (Comp);
1499 end loop;
1500 end if;
1501 end Check_Strict_Alignment;
1503 -------------------------
1504 -- Check_Unsigned_Type --
1505 -------------------------
1507 procedure Check_Unsigned_Type (E : Entity_Id) is
1508 Ancestor : Entity_Id;
1509 Lo_Bound : Node_Id;
1510 Btyp : Entity_Id;
1512 begin
1513 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1514 return;
1515 end if;
1517 -- Do not attempt to analyze case where range was in error
1519 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
1520 return;
1521 end if;
1523 -- The situation that is nontrivial is something like:
1525 -- subtype x1 is integer range -10 .. +10;
1526 -- subtype x2 is x1 range 0 .. V1;
1527 -- subtype x3 is x2 range V2 .. V3;
1528 -- subtype x4 is x3 range V4 .. V5;
1530 -- where Vn are variables. Here the base type is signed, but we still
1531 -- know that x4 is unsigned because of the lower bound of x2.
1533 -- The only way to deal with this is to look up the ancestor chain
1535 Ancestor := E;
1536 loop
1537 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1538 return;
1539 end if;
1541 Lo_Bound := Type_Low_Bound (Ancestor);
1543 if Compile_Time_Known_Value (Lo_Bound) then
1544 if Expr_Rep_Value (Lo_Bound) >= 0 then
1545 Set_Is_Unsigned_Type (E, True);
1546 end if;
1548 return;
1550 else
1551 Ancestor := Ancestor_Subtype (Ancestor);
1553 -- If no ancestor had a static lower bound, go to base type
1555 if No (Ancestor) then
1557 -- Note: the reason we still check for a compile time known
1558 -- value for the base type is that at least in the case of
1559 -- generic formals, we can have bounds that fail this test,
1560 -- and there may be other cases in error situations.
1562 Btyp := Base_Type (E);
1564 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1565 return;
1566 end if;
1568 Lo_Bound := Type_Low_Bound (Base_Type (E));
1570 if Compile_Time_Known_Value (Lo_Bound)
1571 and then Expr_Rep_Value (Lo_Bound) >= 0
1572 then
1573 Set_Is_Unsigned_Type (E, True);
1574 end if;
1576 return;
1577 end if;
1578 end if;
1579 end loop;
1580 end Check_Unsigned_Type;
1582 -----------------------------
1583 -- Is_Atomic_VFA_Aggregate --
1584 -----------------------------
1586 function Is_Atomic_VFA_Aggregate (N : Node_Id) return Boolean is
1587 Loc : constant Source_Ptr := Sloc (N);
1588 New_N : Node_Id;
1589 Par : Node_Id;
1590 Temp : Entity_Id;
1591 Typ : Entity_Id;
1593 begin
1594 Par := Parent (N);
1596 -- Array may be qualified, so find outer context
1598 if Nkind (Par) = N_Qualified_Expression then
1599 Par := Parent (Par);
1600 end if;
1602 if not Comes_From_Source (Par) then
1603 return False;
1604 end if;
1606 case Nkind (Par) is
1607 when N_Assignment_Statement =>
1608 Typ := Etype (Name (Par));
1610 if not Is_Atomic_Or_VFA (Typ)
1611 and then not (Is_Entity_Name (Name (Par))
1612 and then Is_Atomic_Or_VFA (Entity (Name (Par))))
1613 then
1614 return False;
1615 end if;
1617 when N_Object_Declaration =>
1618 Typ := Etype (Defining_Identifier (Par));
1620 if not Is_Atomic_Or_VFA (Typ)
1621 and then not Is_Atomic_Or_VFA (Defining_Identifier (Par))
1622 then
1623 return False;
1624 end if;
1626 when others =>
1627 return False;
1628 end case;
1630 Temp := Make_Temporary (Loc, 'T', N);
1631 New_N :=
1632 Make_Object_Declaration (Loc,
1633 Defining_Identifier => Temp,
1634 Object_Definition => New_Occurrence_Of (Typ, Loc),
1635 Expression => Relocate_Node (N));
1636 Insert_Before (Par, New_N);
1637 Analyze (New_N);
1639 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1640 return True;
1641 end Is_Atomic_VFA_Aggregate;
1643 -----------------------------------------------
1644 -- Explode_Initialization_Compound_Statement --
1645 -----------------------------------------------
1647 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
1648 Init_Stmts : constant Node_Id := Initialization_Statements (E);
1650 begin
1651 if Present (Init_Stmts)
1652 and then Nkind (Init_Stmts) = N_Compound_Statement
1653 then
1654 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
1656 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
1657 -- just removing it, because Freeze_All may rely on this particular
1658 -- Node_Id still being present in the enclosing list to know where to
1659 -- stop freezing.
1661 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
1663 Set_Initialization_Statements (E, Empty);
1664 end if;
1665 end Explode_Initialization_Compound_Statement;
1667 ----------------
1668 -- Freeze_All --
1669 ----------------
1671 -- Note: the easy coding for this procedure would be to just build a
1672 -- single list of freeze nodes and then insert them and analyze them
1673 -- all at once. This won't work, because the analysis of earlier freeze
1674 -- nodes may recursively freeze types which would otherwise appear later
1675 -- on in the freeze list. So we must analyze and expand the freeze nodes
1676 -- as they are generated.
1678 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1679 E : Entity_Id;
1680 Decl : Node_Id;
1682 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1683 -- This is the internal recursive routine that does freezing of entities
1684 -- (but NOT the analysis of default expressions, which should not be
1685 -- recursive, we don't want to analyze those till we are sure that ALL
1686 -- the types are frozen).
1688 --------------------
1689 -- Freeze_All_Ent --
1690 --------------------
1692 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
1693 E : Entity_Id;
1694 Flist : List_Id;
1695 Lastn : Node_Id;
1697 procedure Process_Flist;
1698 -- If freeze nodes are present, insert and analyze, and reset cursor
1699 -- for next insertion.
1701 -------------------
1702 -- Process_Flist --
1703 -------------------
1705 procedure Process_Flist is
1706 begin
1707 if Is_Non_Empty_List (Flist) then
1708 Lastn := Next (After);
1709 Insert_List_After_And_Analyze (After, Flist);
1711 if Present (Lastn) then
1712 After := Prev (Lastn);
1713 else
1714 After := Last (List_Containing (After));
1715 end if;
1716 end if;
1717 end Process_Flist;
1719 -- Start of processing for Freeze_All_Ent
1721 begin
1722 E := From;
1723 while Present (E) loop
1725 -- If the entity is an inner package which is not a package
1726 -- renaming, then its entities must be frozen at this point. Note
1727 -- that such entities do NOT get frozen at the end of the nested
1728 -- package itself (only library packages freeze).
1730 -- Same is true for task declarations, where anonymous records
1731 -- created for entry parameters must be frozen.
1733 if Ekind (E) = E_Package
1734 and then No (Renamed_Object (E))
1735 and then not Is_Child_Unit (E)
1736 and then not Is_Frozen (E)
1737 then
1738 Push_Scope (E);
1740 Install_Visible_Declarations (E);
1741 Install_Private_Declarations (E);
1742 Freeze_All (First_Entity (E), After);
1744 End_Package_Scope (E);
1746 if Is_Generic_Instance (E)
1747 and then Has_Delayed_Freeze (E)
1748 then
1749 Set_Has_Delayed_Freeze (E, False);
1750 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1751 end if;
1753 elsif Ekind (E) in Task_Kind
1754 and then Nkind_In (Parent (E), N_Single_Task_Declaration,
1755 N_Task_Type_Declaration)
1756 then
1757 Push_Scope (E);
1758 Freeze_All (First_Entity (E), After);
1759 End_Scope;
1761 -- For a derived tagged type, we must ensure that all the
1762 -- primitive operations of the parent have been frozen, so that
1763 -- their addresses will be in the parent's dispatch table at the
1764 -- point it is inherited.
1766 elsif Ekind (E) = E_Record_Type
1767 and then Is_Tagged_Type (E)
1768 and then Is_Tagged_Type (Etype (E))
1769 and then Is_Derived_Type (E)
1770 then
1771 declare
1772 Prim_List : constant Elist_Id :=
1773 Primitive_Operations (Etype (E));
1775 Prim : Elmt_Id;
1776 Subp : Entity_Id;
1778 begin
1779 Prim := First_Elmt (Prim_List);
1780 while Present (Prim) loop
1781 Subp := Node (Prim);
1783 if Comes_From_Source (Subp)
1784 and then not Is_Frozen (Subp)
1785 then
1786 Flist := Freeze_Entity (Subp, After);
1787 Process_Flist;
1788 end if;
1790 Next_Elmt (Prim);
1791 end loop;
1792 end;
1793 end if;
1795 if not Is_Frozen (E) then
1796 Flist := Freeze_Entity (E, After);
1797 Process_Flist;
1799 -- If already frozen, and there are delayed aspects, this is where
1800 -- we do the visibility check for these aspects (see Sem_Ch13 spec
1801 -- for a description of how we handle aspect visibility).
1803 elsif Has_Delayed_Aspects (E) then
1805 -- Retrieve the visibility to the discriminants in order to
1806 -- analyze properly the aspects.
1808 Push_Scope_And_Install_Discriminants (E);
1810 declare
1811 Ritem : Node_Id;
1813 begin
1814 Ritem := First_Rep_Item (E);
1815 while Present (Ritem) loop
1816 if Nkind (Ritem) = N_Aspect_Specification
1817 and then Entity (Ritem) = E
1818 and then Is_Delayed_Aspect (Ritem)
1819 then
1820 Check_Aspect_At_End_Of_Declarations (Ritem);
1821 end if;
1823 Ritem := Next_Rep_Item (Ritem);
1824 end loop;
1825 end;
1827 Uninstall_Discriminants_And_Pop_Scope (E);
1828 end if;
1830 -- If an incomplete type is still not frozen, this may be a
1831 -- premature freezing because of a body declaration that follows.
1832 -- Indicate where the freezing took place. Freezing will happen
1833 -- if the body comes from source, but not if it is internally
1834 -- generated, for example as the body of a type invariant.
1836 -- If the freezing is caused by the end of the current declarative
1837 -- part, it is a Taft Amendment type, and there is no error.
1839 if not Is_Frozen (E)
1840 and then Ekind (E) = E_Incomplete_Type
1841 then
1842 declare
1843 Bod : constant Node_Id := Next (After);
1845 begin
1846 -- The presence of a body freezes all entities previously
1847 -- declared in the current list of declarations, but this
1848 -- does not apply if the body does not come from source.
1849 -- A type invariant is transformed into a subprogram body
1850 -- which is placed at the end of the private part of the
1851 -- current package, but this body does not freeze incomplete
1852 -- types that may be declared in this private part.
1854 if (Nkind_In (Bod, N_Subprogram_Body,
1855 N_Entry_Body,
1856 N_Package_Body,
1857 N_Protected_Body,
1858 N_Task_Body)
1859 or else Nkind (Bod) in N_Body_Stub)
1860 and then
1861 List_Containing (After) = List_Containing (Parent (E))
1862 and then Comes_From_Source (Bod)
1863 then
1864 Error_Msg_Sloc := Sloc (Next (After));
1865 Error_Msg_NE
1866 ("type& is frozen# before its full declaration",
1867 Parent (E), E);
1868 end if;
1869 end;
1870 end if;
1872 Next_Entity (E);
1873 end loop;
1874 end Freeze_All_Ent;
1876 -- Start of processing for Freeze_All
1878 begin
1879 Freeze_All_Ent (From, After);
1881 -- Now that all types are frozen, we can deal with default expressions
1882 -- that require us to build a default expression functions. This is the
1883 -- point at which such functions are constructed (after all types that
1884 -- might be used in such expressions have been frozen).
1886 -- For subprograms that are renaming_as_body, we create the wrapper
1887 -- bodies as needed.
1889 -- We also add finalization chains to access types whose designated
1890 -- types are controlled. This is normally done when freezing the type,
1891 -- but this misses recursive type definitions where the later members
1892 -- of the recursion introduce controlled components.
1894 -- Loop through entities
1896 E := From;
1897 while Present (E) loop
1898 if Is_Subprogram (E) then
1899 if not Default_Expressions_Processed (E) then
1900 Process_Default_Expressions (E, After);
1901 end if;
1903 if not Has_Completion (E) then
1904 Decl := Unit_Declaration_Node (E);
1906 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1907 if Error_Posted (Decl) then
1908 Set_Has_Completion (E);
1909 else
1910 Build_And_Analyze_Renamed_Body (Decl, E, After);
1911 end if;
1913 elsif Nkind (Decl) = N_Subprogram_Declaration
1914 and then Present (Corresponding_Body (Decl))
1915 and then
1916 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1917 = N_Subprogram_Renaming_Declaration
1918 then
1919 Build_And_Analyze_Renamed_Body
1920 (Decl, Corresponding_Body (Decl), After);
1921 end if;
1922 end if;
1924 elsif Ekind (E) in Task_Kind
1925 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1926 N_Single_Task_Declaration)
1927 then
1928 declare
1929 Ent : Entity_Id;
1931 begin
1932 Ent := First_Entity (E);
1933 while Present (Ent) loop
1934 if Is_Entry (Ent)
1935 and then not Default_Expressions_Processed (Ent)
1936 then
1937 Process_Default_Expressions (Ent, After);
1938 end if;
1940 Next_Entity (Ent);
1941 end loop;
1942 end;
1943 end if;
1945 -- Historical note: We used to create a finalization master for an
1946 -- access type whose designated type is not controlled, but contains
1947 -- private controlled compoments. This form of postprocessing is no
1948 -- longer needed because the finalization master is now created when
1949 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
1951 Next_Entity (E);
1952 end loop;
1953 end Freeze_All;
1955 -----------------------
1956 -- Freeze_And_Append --
1957 -----------------------
1959 procedure Freeze_And_Append
1960 (Ent : Entity_Id;
1961 N : Node_Id;
1962 Result : in out List_Id)
1964 L : constant List_Id := Freeze_Entity (Ent, N);
1965 begin
1966 if Is_Non_Empty_List (L) then
1967 if Result = No_List then
1968 Result := L;
1969 else
1970 Append_List (L, Result);
1971 end if;
1972 end if;
1973 end Freeze_And_Append;
1975 -------------------
1976 -- Freeze_Before --
1977 -------------------
1979 procedure Freeze_Before
1980 (N : Node_Id;
1981 T : Entity_Id;
1982 Do_Freeze_Profile : Boolean := True)
1984 -- Freeze T, then insert the generated Freeze nodes before the node N.
1985 -- Flag Freeze_Profile is used when T is an overloadable entity, and
1986 -- indicates whether its profile should be frozen at the same time.
1988 Freeze_Nodes : constant List_Id :=
1989 Freeze_Entity (T, N, Do_Freeze_Profile);
1991 begin
1992 if Ekind (T) = E_Function then
1993 Check_Expression_Function (N, T);
1994 end if;
1996 if Is_Non_Empty_List (Freeze_Nodes) then
1997 Insert_Actions (N, Freeze_Nodes);
1998 end if;
1999 end Freeze_Before;
2001 -------------------
2002 -- Freeze_Entity --
2003 -------------------
2005 function Freeze_Entity
2006 (E : Entity_Id;
2007 N : Node_Id;
2008 Do_Freeze_Profile : Boolean := True) return List_Id
2010 Loc : constant Source_Ptr := Sloc (N);
2011 Atype : Entity_Id;
2012 Comp : Entity_Id;
2013 F_Node : Node_Id;
2014 Formal : Entity_Id;
2015 Indx : Node_Id;
2017 Has_Default_Initialization : Boolean := False;
2018 -- This flag gets set to true for a variable with default initialization
2020 Result : List_Id := No_List;
2021 -- List of freezing actions, left at No_List if none
2023 Test_E : Entity_Id := E;
2024 -- This could use a comment ???
2026 procedure Add_To_Result (N : Node_Id);
2027 -- N is a freezing action to be appended to the Result
2029 function After_Last_Declaration return Boolean;
2030 -- If Loc is a freeze_entity that appears after the last declaration
2031 -- in the scope, inhibit error messages on late completion.
2033 procedure Check_Current_Instance (Comp_Decl : Node_Id);
2034 -- Check that an Access or Unchecked_Access attribute with a prefix
2035 -- which is the current instance type can only be applied when the type
2036 -- is limited.
2038 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
2039 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
2040 -- integer literal without an explicit corresponding size clause. The
2041 -- caller has checked that Utype is a modular integer type.
2043 procedure Freeze_Array_Type (Arr : Entity_Id);
2044 -- Freeze array type, including freezing index and component types
2046 procedure Freeze_Object_Declaration (E : Entity_Id);
2047 -- Perform checks and generate freeze node if needed for a constant or
2048 -- variable declared by an object declaration.
2050 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
2051 -- Create Freeze_Generic_Entity nodes for types declared in a generic
2052 -- package. Recurse on inner generic packages.
2054 function Freeze_Profile (E : Entity_Id) return Boolean;
2055 -- Freeze formals and return type of subprogram. If some type in the
2056 -- profile is incomplete and we are in an instance, freezing of the
2057 -- entity will take place elsewhere, and the function returns False.
2059 procedure Freeze_Record_Type (Rec : Entity_Id);
2060 -- Freeze record type, including freezing component types, and freezing
2061 -- primitive operations if this is a tagged type.
2063 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2064 -- Determine whether an arbitrary entity is subject to Boolean aspect
2065 -- Import and its value is specified as True.
2067 function New_Freeze_Node return Node_Id;
2068 -- Create a new freeze node for entity E
2070 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2071 -- If E is an entity for an imported subprogram with pre/post-conditions
2072 -- then this procedure will create a wrapper to ensure that proper run-
2073 -- time checking of the pre/postconditions. See body for details.
2075 -------------------
2076 -- Add_To_Result --
2077 -------------------
2079 procedure Add_To_Result (N : Node_Id) is
2080 begin
2081 if No (Result) then
2082 Result := New_List (N);
2083 else
2084 Append (N, Result);
2085 end if;
2086 end Add_To_Result;
2088 ----------------------------
2089 -- After_Last_Declaration --
2090 ----------------------------
2092 function After_Last_Declaration return Boolean is
2093 Spec : constant Node_Id := Parent (Current_Scope);
2095 begin
2096 if Nkind (Spec) = N_Package_Specification then
2097 if Present (Private_Declarations (Spec)) then
2098 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2099 elsif Present (Visible_Declarations (Spec)) then
2100 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2101 else
2102 return False;
2103 end if;
2105 else
2106 return False;
2107 end if;
2108 end After_Last_Declaration;
2110 ----------------------------
2111 -- Check_Current_Instance --
2112 ----------------------------
2114 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2116 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2117 -- Determine whether Typ is compatible with the rules for aliased
2118 -- views of types as defined in RM 3.10 in the various dialects.
2120 function Process (N : Node_Id) return Traverse_Result;
2121 -- Process routine to apply check to given node
2123 -----------------------------
2124 -- Is_Aliased_View_Of_Type --
2125 -----------------------------
2127 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2128 Typ_Decl : constant Node_Id := Parent (Typ);
2130 begin
2131 -- Common case
2133 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2134 and then Limited_Present (Type_Definition (Typ_Decl))
2135 then
2136 return True;
2138 -- The following paragraphs describe what a legal aliased view of
2139 -- a type is in the various dialects of Ada.
2141 -- Ada 95
2143 -- The current instance of a limited type, and a formal parameter
2144 -- or generic formal object of a tagged type.
2146 -- Ada 95 limited type
2147 -- * Type with reserved word "limited"
2148 -- * A protected or task type
2149 -- * A composite type with limited component
2151 elsif Ada_Version <= Ada_95 then
2152 return Is_Limited_Type (Typ);
2154 -- Ada 2005
2156 -- The current instance of a limited tagged type, a protected
2157 -- type, a task type, or a type that has the reserved word
2158 -- "limited" in its full definition ... a formal parameter or
2159 -- generic formal object of a tagged type.
2161 -- Ada 2005 limited type
2162 -- * Type with reserved word "limited", "synchronized", "task"
2163 -- or "protected"
2164 -- * A composite type with limited component
2165 -- * A derived type whose parent is a non-interface limited type
2167 elsif Ada_Version = Ada_2005 then
2168 return
2169 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2170 or else
2171 (Is_Derived_Type (Typ)
2172 and then not Is_Interface (Etype (Typ))
2173 and then Is_Limited_Type (Etype (Typ)));
2175 -- Ada 2012 and beyond
2177 -- The current instance of an immutably limited type ... a formal
2178 -- parameter or generic formal object of a tagged type.
2180 -- Ada 2012 limited type
2181 -- * Type with reserved word "limited", "synchronized", "task"
2182 -- or "protected"
2183 -- * A composite type with limited component
2184 -- * A derived type whose parent is a non-interface limited type
2185 -- * An incomplete view
2187 -- Ada 2012 immutably limited type
2188 -- * Explicitly limited record type
2189 -- * Record extension with "limited" present
2190 -- * Non-formal limited private type that is either tagged
2191 -- or has at least one access discriminant with a default
2192 -- expression
2193 -- * Task type, protected type or synchronized interface
2194 -- * Type derived from immutably limited type
2196 else
2197 return
2198 Is_Immutably_Limited_Type (Typ)
2199 or else Is_Incomplete_Type (Typ);
2200 end if;
2201 end Is_Aliased_View_Of_Type;
2203 -------------
2204 -- Process --
2205 -------------
2207 function Process (N : Node_Id) return Traverse_Result is
2208 begin
2209 case Nkind (N) is
2210 when N_Attribute_Reference =>
2211 if Nam_In (Attribute_Name (N), Name_Access,
2212 Name_Unchecked_Access)
2213 and then Is_Entity_Name (Prefix (N))
2214 and then Is_Type (Entity (Prefix (N)))
2215 and then Entity (Prefix (N)) = E
2216 then
2217 if Ada_Version < Ada_2012 then
2218 Error_Msg_N
2219 ("current instance must be a limited type",
2220 Prefix (N));
2221 else
2222 Error_Msg_N
2223 ("current instance must be an immutably limited "
2224 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2225 end if;
2227 return Abandon;
2229 else
2230 return OK;
2231 end if;
2233 when others => return OK;
2234 end case;
2235 end Process;
2237 procedure Traverse is new Traverse_Proc (Process);
2239 -- Local variables
2241 Rec_Type : constant Entity_Id :=
2242 Scope (Defining_Identifier (Comp_Decl));
2244 -- Start of processing for Check_Current_Instance
2246 begin
2247 if not Is_Aliased_View_Of_Type (Rec_Type) then
2248 Traverse (Comp_Decl);
2249 end if;
2250 end Check_Current_Instance;
2252 ------------------------------
2253 -- Check_Suspicious_Modulus --
2254 ------------------------------
2256 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
2257 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
2259 begin
2260 if not Warn_On_Suspicious_Modulus_Value then
2261 return;
2262 end if;
2264 if Nkind (Decl) = N_Full_Type_Declaration then
2265 declare
2266 Tdef : constant Node_Id := Type_Definition (Decl);
2268 begin
2269 if Nkind (Tdef) = N_Modular_Type_Definition then
2270 declare
2271 Modulus : constant Node_Id :=
2272 Original_Node (Expression (Tdef));
2274 begin
2275 if Nkind (Modulus) = N_Integer_Literal then
2276 declare
2277 Modv : constant Uint := Intval (Modulus);
2278 Sizv : constant Uint := RM_Size (Utype);
2280 begin
2281 -- First case, modulus and size are the same. This
2282 -- happens if you have something like mod 32, with
2283 -- an explicit size of 32, this is for sure a case
2284 -- where the warning is given, since it is seems
2285 -- very unlikely that someone would want e.g. a
2286 -- five bit type stored in 32 bits. It is much
2287 -- more likely they wanted a 32-bit type.
2289 if Modv = Sizv then
2290 null;
2292 -- Second case, the modulus is 32 or 64 and no
2293 -- size clause is present. This is a less clear
2294 -- case for giving the warning, but in the case
2295 -- of 32/64 (5-bit or 6-bit types) these seem rare
2296 -- enough that it is a likely error (and in any
2297 -- case using 2**5 or 2**6 in these cases seems
2298 -- clearer. We don't include 8 or 16 here, simply
2299 -- because in practice 3-bit and 4-bit types are
2300 -- more common and too many false positives if
2301 -- we warn in these cases.
2303 elsif not Has_Size_Clause (Utype)
2304 and then (Modv = Uint_32 or else Modv = Uint_64)
2305 then
2306 null;
2308 -- No warning needed
2310 else
2311 return;
2312 end if;
2314 -- If we fall through, give warning
2316 Error_Msg_Uint_1 := Modv;
2317 Error_Msg_N
2318 ("?M?2 '*'*^' may have been intended here",
2319 Modulus);
2320 end;
2321 end if;
2322 end;
2323 end if;
2324 end;
2325 end if;
2326 end Check_Suspicious_Modulus;
2328 -----------------------
2329 -- Freeze_Array_Type --
2330 -----------------------
2332 procedure Freeze_Array_Type (Arr : Entity_Id) is
2333 FS : constant Entity_Id := First_Subtype (Arr);
2334 Ctyp : constant Entity_Id := Component_Type (Arr);
2335 Clause : Entity_Id;
2337 Non_Standard_Enum : Boolean := False;
2338 -- Set true if any of the index types is an enumeration type with a
2339 -- non-standard representation.
2341 begin
2342 Freeze_And_Append (Ctyp, N, Result);
2344 Indx := First_Index (Arr);
2345 while Present (Indx) loop
2346 Freeze_And_Append (Etype (Indx), N, Result);
2348 if Is_Enumeration_Type (Etype (Indx))
2349 and then Has_Non_Standard_Rep (Etype (Indx))
2350 then
2351 Non_Standard_Enum := True;
2352 end if;
2354 Next_Index (Indx);
2355 end loop;
2357 -- Processing that is done only for base types
2359 if Ekind (Arr) = E_Array_Type then
2361 -- Deal with default setting of reverse storage order
2363 Set_SSO_From_Default (Arr);
2365 -- Propagate flags for component type
2367 if Is_Controlled_Active (Component_Type (Arr))
2368 or else Has_Controlled_Component (Ctyp)
2369 then
2370 Set_Has_Controlled_Component (Arr);
2371 end if;
2373 if Has_Unchecked_Union (Component_Type (Arr)) then
2374 Set_Has_Unchecked_Union (Arr);
2375 end if;
2377 -- The array type requires its own invariant procedure in order to
2378 -- verify the component invariant over all elements.
2380 if Has_Invariants (Component_Type (Arr))
2381 or else
2382 (Is_Access_Type (Component_Type (Arr))
2383 and then Has_Invariants
2384 (Designated_Type (Component_Type (Arr))))
2385 then
2386 Set_Has_Own_Invariants (Arr);
2388 -- The array type is an implementation base type. Propagate the
2389 -- same property to the first subtype.
2391 if Is_Itype (Arr) then
2392 Set_Has_Own_Invariants (First_Subtype (Arr));
2393 end if;
2394 end if;
2396 -- Warn for pragma Pack overriding foreign convention
2398 if Has_Foreign_Convention (Ctyp)
2399 and then Has_Pragma_Pack (Arr)
2400 then
2401 declare
2402 CN : constant Name_Id :=
2403 Get_Convention_Name (Convention (Ctyp));
2404 PP : constant Node_Id :=
2405 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
2406 begin
2407 if Present (PP) then
2408 Error_Msg_Name_1 := CN;
2409 Error_Msg_Sloc := Sloc (Arr);
2410 Error_Msg_N
2411 ("pragma Pack affects convention % components #??", PP);
2412 Error_Msg_Name_1 := CN;
2413 Error_Msg_N
2414 ("\array components may not have % compatible "
2415 & "representation??", PP);
2416 end if;
2417 end;
2418 end if;
2420 -- If packing was requested or if the component size was
2421 -- set explicitly, then see if bit packing is required. This
2422 -- processing is only done for base types, since all of the
2423 -- representation aspects involved are type-related.
2425 -- This is not just an optimization, if we start processing the
2426 -- subtypes, they interfere with the settings on the base type
2427 -- (this is because Is_Packed has a slightly different meaning
2428 -- before and after freezing).
2430 declare
2431 Csiz : Uint;
2432 Esiz : Uint;
2434 begin
2435 if (Is_Packed (Arr) or else Has_Pragma_Pack (Arr))
2436 and then Known_Static_RM_Size (Ctyp)
2437 and then not Has_Component_Size_Clause (Arr)
2438 then
2439 Csiz := UI_Max (RM_Size (Ctyp), 1);
2441 elsif Known_Component_Size (Arr) then
2442 Csiz := Component_Size (Arr);
2444 elsif not Known_Static_Esize (Ctyp) then
2445 Csiz := Uint_0;
2447 else
2448 Esiz := Esize (Ctyp);
2450 -- We can set the component size if it is less than 16,
2451 -- rounding it up to the next storage unit size.
2453 if Esiz <= 8 then
2454 Csiz := Uint_8;
2455 elsif Esiz <= 16 then
2456 Csiz := Uint_16;
2457 else
2458 Csiz := Uint_0;
2459 end if;
2461 -- Set component size up to match alignment if it would
2462 -- otherwise be less than the alignment. This deals with
2463 -- cases of types whose alignment exceeds their size (the
2464 -- padded type cases).
2466 if Csiz /= 0 then
2467 declare
2468 A : constant Uint := Alignment_In_Bits (Ctyp);
2469 begin
2470 if Csiz < A then
2471 Csiz := A;
2472 end if;
2473 end;
2474 end if;
2475 end if;
2477 -- Case of component size that may result in bit packing
2479 if 1 <= Csiz and then Csiz <= 64 then
2480 declare
2481 Ent : constant Entity_Id :=
2482 First_Subtype (Arr);
2483 Pack_Pragma : constant Node_Id :=
2484 Get_Rep_Pragma (Ent, Name_Pack);
2485 Comp_Size_C : constant Node_Id :=
2486 Get_Attribute_Definition_Clause
2487 (Ent, Attribute_Component_Size);
2489 begin
2490 -- Warn if we have pack and component size so that the
2491 -- pack is ignored.
2493 -- Note: here we must check for the presence of a
2494 -- component size before checking for a Pack pragma to
2495 -- deal with the case where the array type is a derived
2496 -- type whose parent is currently private.
2498 if Present (Comp_Size_C)
2499 and then Has_Pragma_Pack (Ent)
2500 and then Warn_On_Redundant_Constructs
2501 then
2502 Error_Msg_Sloc := Sloc (Comp_Size_C);
2503 Error_Msg_NE
2504 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
2505 Error_Msg_N
2506 ("\?r?explicit component size given#!", Pack_Pragma);
2507 Set_Is_Packed (Base_Type (Ent), False);
2508 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
2509 end if;
2511 -- Set component size if not already set by a component
2512 -- size clause.
2514 if not Present (Comp_Size_C) then
2515 Set_Component_Size (Arr, Csiz);
2516 end if;
2518 -- Check for base type of 8, 16, 32 bits, where an
2519 -- unsigned subtype has a length one less than the
2520 -- base type (e.g. Natural subtype of Integer).
2522 -- In such cases, if a component size was not set
2523 -- explicitly, then generate a warning.
2525 if Has_Pragma_Pack (Arr)
2526 and then not Present (Comp_Size_C)
2527 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2528 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2529 then
2530 Error_Msg_Uint_1 := Csiz;
2532 if Present (Pack_Pragma) then
2533 Error_Msg_N
2534 ("??pragma Pack causes component size to be ^!",
2535 Pack_Pragma);
2536 Error_Msg_N
2537 ("\??use Component_Size to set desired value!",
2538 Pack_Pragma);
2539 end if;
2540 end if;
2542 -- Bit packing is never needed for 8, 16, 32, 64
2544 if Addressable (Csiz) then
2546 -- If the Esize of the component is known and equal to
2547 -- the component size then even packing is not needed.
2549 if Known_Static_Esize (Component_Type (Arr))
2550 and then Esize (Component_Type (Arr)) = Csiz
2551 then
2552 -- Here the array was requested to be packed, but
2553 -- the packing request had no effect whatsoever,
2554 -- so flag Is_Packed is reset.
2556 -- Note: semantically this means that we lose track
2557 -- of the fact that a derived type inherited pragma
2558 -- Pack that was non-effective, but that is fine.
2560 -- We regard a Pack pragma as a request to set a
2561 -- representation characteristic, and this request
2562 -- may be ignored.
2564 Set_Is_Packed (Base_Type (Arr), False);
2565 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
2566 else
2567 Set_Is_Packed (Base_Type (Arr), True);
2568 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2569 end if;
2571 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2573 -- Bit packing is not needed for multiples of the storage
2574 -- unit if the type is composite because the back end can
2575 -- byte pack composite types.
2577 elsif Csiz mod System_Storage_Unit = 0
2578 and then Is_Composite_Type (Ctyp)
2579 then
2581 Set_Is_Packed (Base_Type (Arr), True);
2582 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2583 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2585 -- In all other cases, bit packing is needed
2587 else
2588 Set_Is_Packed (Base_Type (Arr), True);
2589 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2590 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
2591 end if;
2592 end;
2593 end if;
2594 end;
2596 -- Check for Aliased or Atomic_Components/Atomic/VFA with
2597 -- unsuitable packing or explicit component size clause given.
2599 if (Has_Aliased_Components (Arr)
2600 or else Has_Atomic_Components (Arr)
2601 or else Is_Atomic_Or_VFA (Ctyp))
2602 and then
2603 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2604 then
2605 Alias_Atomic_Check : declare
2607 procedure Complain_CS (T : String);
2608 -- Outputs error messages for incorrect CS clause or pragma
2609 -- Pack for aliased or atomic/VFA components (T is "aliased"
2610 -- or "atomic/vfa");
2612 -----------------
2613 -- Complain_CS --
2614 -----------------
2616 procedure Complain_CS (T : String) is
2617 begin
2618 if Has_Component_Size_Clause (Arr) then
2619 Clause :=
2620 Get_Attribute_Definition_Clause
2621 (FS, Attribute_Component_Size);
2623 Error_Msg_N
2624 ("incorrect component size for "
2625 & T & " components", Clause);
2626 Error_Msg_Uint_1 := Esize (Ctyp);
2627 Error_Msg_N
2628 ("\only allowed value is^", Clause);
2630 else
2631 Error_Msg_N
2632 ("cannot pack " & T & " components",
2633 Get_Rep_Pragma (FS, Name_Pack));
2634 end if;
2635 end Complain_CS;
2637 -- Start of processing for Alias_Atomic_Check
2639 begin
2640 -- If object size of component type isn't known, we cannot
2641 -- be sure so we defer to the back end.
2643 if not Known_Static_Esize (Ctyp) then
2644 null;
2646 -- Case where component size has no effect. First check for
2647 -- object size of component type multiple of the storage
2648 -- unit size.
2650 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2652 -- OK in both packing case and component size case if RM
2653 -- size is known and static and same as the object size.
2655 and then
2656 ((Known_Static_RM_Size (Ctyp)
2657 and then Esize (Ctyp) = RM_Size (Ctyp))
2659 -- Or if we have an explicit component size clause and
2660 -- the component size and object size are equal.
2662 or else
2663 (Has_Component_Size_Clause (Arr)
2664 and then Component_Size (Arr) = Esize (Ctyp)))
2665 then
2666 null;
2668 elsif Has_Aliased_Components (Arr) then
2669 Complain_CS ("aliased");
2671 elsif Has_Atomic_Components (Arr)
2672 or else Is_Atomic (Ctyp)
2673 then
2674 Complain_CS ("atomic");
2676 elsif Is_Volatile_Full_Access (Ctyp) then
2677 Complain_CS ("volatile full access");
2678 end if;
2679 end Alias_Atomic_Check;
2680 end if;
2682 -- Check for Independent_Components/Independent with unsuitable
2683 -- packing or explicit component size clause given.
2685 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
2686 and then
2687 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2688 then
2689 begin
2690 -- If object size of component type isn't known, we cannot
2691 -- be sure so we defer to the back end.
2693 if not Known_Static_Esize (Ctyp) then
2694 null;
2696 -- Case where component size has no effect. First check for
2697 -- object size of component type multiple of the storage
2698 -- unit size.
2700 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2702 -- OK in both packing case and component size case if RM
2703 -- size is known and multiple of the storage unit size.
2705 and then
2706 ((Known_Static_RM_Size (Ctyp)
2707 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
2709 -- Or if we have an explicit component size clause and
2710 -- the component size is larger than the object size.
2712 or else
2713 (Has_Component_Size_Clause (Arr)
2714 and then Component_Size (Arr) >= Esize (Ctyp)))
2715 then
2716 null;
2718 else
2719 if Has_Component_Size_Clause (Arr) then
2720 Clause :=
2721 Get_Attribute_Definition_Clause
2722 (FS, Attribute_Component_Size);
2724 Error_Msg_N
2725 ("incorrect component size for "
2726 & "independent components", Clause);
2727 Error_Msg_Uint_1 := Esize (Ctyp);
2728 Error_Msg_N
2729 ("\minimum allowed is^", Clause);
2731 else
2732 Error_Msg_N
2733 ("cannot pack independent components",
2734 Get_Rep_Pragma (FS, Name_Pack));
2735 end if;
2736 end if;
2737 end;
2738 end if;
2740 -- Warn for case of atomic type
2742 Clause := Get_Rep_Pragma (FS, Name_Atomic);
2744 if Present (Clause)
2745 and then not Addressable (Component_Size (FS))
2746 then
2747 Error_Msg_NE
2748 ("non-atomic components of type& may not be "
2749 & "accessible by separate tasks??", Clause, Arr);
2751 if Has_Component_Size_Clause (Arr) then
2752 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2753 (FS, Attribute_Component_Size));
2754 Error_Msg_N ("\because of component size clause#??", Clause);
2756 elsif Has_Pragma_Pack (Arr) then
2757 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2758 Error_Msg_N ("\because of pragma Pack#??", Clause);
2759 end if;
2760 end if;
2762 -- Check for scalar storage order
2764 declare
2765 Dummy : Boolean;
2766 begin
2767 Check_Component_Storage_Order
2768 (Encl_Type => Arr,
2769 Comp => Empty,
2770 ADC => Get_Attribute_Definition_Clause
2771 (First_Subtype (Arr),
2772 Attribute_Scalar_Storage_Order),
2773 Comp_ADC_Present => Dummy);
2774 end;
2776 -- Processing that is done only for subtypes
2778 else
2779 -- Acquire alignment from base type
2781 if Unknown_Alignment (Arr) then
2782 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
2783 Adjust_Esize_Alignment (Arr);
2784 end if;
2785 end if;
2787 -- Specific checks for bit-packed arrays
2789 if Is_Bit_Packed_Array (Arr) then
2791 -- Check number of elements for bit-packed arrays that come from
2792 -- source and have compile time known ranges. The bit-packed
2793 -- arrays circuitry does not support arrays with more than
2794 -- Integer'Last + 1 elements, and when this restriction is
2795 -- violated, causes incorrect data access.
2797 -- For the case where this is not compile time known, a run-time
2798 -- check should be generated???
2800 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
2801 declare
2802 Elmts : Uint;
2803 Index : Node_Id;
2804 Ilen : Node_Id;
2805 Ityp : Entity_Id;
2807 begin
2808 Elmts := Uint_1;
2809 Index := First_Index (Arr);
2810 while Present (Index) loop
2811 Ityp := Etype (Index);
2813 -- Never generate an error if any index is of a generic
2814 -- type. We will check this in instances.
2816 if Is_Generic_Type (Ityp) then
2817 Elmts := Uint_0;
2818 exit;
2819 end if;
2821 Ilen :=
2822 Make_Attribute_Reference (Loc,
2823 Prefix => New_Occurrence_Of (Ityp, Loc),
2824 Attribute_Name => Name_Range_Length);
2825 Analyze_And_Resolve (Ilen);
2827 -- No attempt is made to check number of elements if not
2828 -- compile time known.
2830 if Nkind (Ilen) /= N_Integer_Literal then
2831 Elmts := Uint_0;
2832 exit;
2833 end if;
2835 Elmts := Elmts * Intval (Ilen);
2836 Next_Index (Index);
2837 end loop;
2839 if Elmts > Intval (High_Bound
2840 (Scalar_Range (Standard_Integer))) + 1
2841 then
2842 Error_Msg_N
2843 ("bit packed array type may not have "
2844 & "more than Integer''Last+1 elements", Arr);
2845 end if;
2846 end;
2847 end if;
2849 -- Check size
2851 if Known_RM_Size (Arr) then
2852 declare
2853 SizC : constant Node_Id := Size_Clause (Arr);
2854 Discard : Boolean;
2856 begin
2857 -- It is not clear if it is possible to have no size clause
2858 -- at this stage, but it is not worth worrying about. Post
2859 -- error on the entity name in the size clause if present,
2860 -- else on the type entity itself.
2862 if Present (SizC) then
2863 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
2864 else
2865 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
2866 end if;
2867 end;
2868 end if;
2869 end if;
2871 -- If any of the index types was an enumeration type with a non-
2872 -- standard rep clause, then we indicate that the array type is
2873 -- always packed (even if it is not bit-packed).
2875 if Non_Standard_Enum then
2876 Set_Has_Non_Standard_Rep (Base_Type (Arr));
2877 Set_Is_Packed (Base_Type (Arr));
2878 end if;
2880 Set_Component_Alignment_If_Not_Set (Arr);
2882 -- If the array is packed and bit-packed or packed to eliminate holes
2883 -- in the non-contiguous enumeration index types, we must create the
2884 -- packed array type to be used to actually implement the type. This
2885 -- is only needed for real array types (not for string literal types,
2886 -- since they are present only for the front end).
2888 if Is_Packed (Arr)
2889 and then (Is_Bit_Packed_Array (Arr) or else Non_Standard_Enum)
2890 and then Ekind (Arr) /= E_String_Literal_Subtype
2891 then
2892 Create_Packed_Array_Impl_Type (Arr);
2893 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
2895 -- Make sure that we have the necessary routines to implement the
2896 -- packing, and complain now if not. Note that we only test this
2897 -- for constrained array types.
2899 if Is_Constrained (Arr)
2900 and then Is_Bit_Packed_Array (Arr)
2901 and then Present (Packed_Array_Impl_Type (Arr))
2902 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
2903 then
2904 declare
2905 CS : constant Uint := Component_Size (Arr);
2906 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
2908 begin
2909 if RE /= RE_Null
2910 and then not RTE_Available (RE)
2911 then
2912 Error_Msg_CRT
2913 ("packing of " & UI_Image (CS) & "-bit components",
2914 First_Subtype (Etype (Arr)));
2916 -- Cancel the packing
2918 Set_Is_Packed (Base_Type (Arr), False);
2919 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2920 Set_Packed_Array_Impl_Type (Arr, Empty);
2921 goto Skip_Packed;
2922 end if;
2923 end;
2924 end if;
2926 -- Size information of packed array type is copied to the array
2927 -- type, since this is really the representation. But do not
2928 -- override explicit existing size values. If the ancestor subtype
2929 -- is constrained the Packed_Array_Impl_Type will be inherited
2930 -- from it, but the size may have been provided already, and
2931 -- must not be overridden either.
2933 if not Has_Size_Clause (Arr)
2934 and then
2935 (No (Ancestor_Subtype (Arr))
2936 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
2937 then
2938 Set_Esize (Arr, Esize (Packed_Array_Impl_Type (Arr)));
2939 Set_RM_Size (Arr, RM_Size (Packed_Array_Impl_Type (Arr)));
2940 end if;
2942 if not Has_Alignment_Clause (Arr) then
2943 Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
2944 end if;
2945 end if;
2947 <<Skip_Packed>>
2949 -- For non-packed arrays set the alignment of the array to the
2950 -- alignment of the component type if it is unknown. Skip this
2951 -- in atomic/VFA case (atomic/VFA arrays may need larger alignments).
2953 if not Is_Packed (Arr)
2954 and then Unknown_Alignment (Arr)
2955 and then Known_Alignment (Ctyp)
2956 and then Known_Static_Component_Size (Arr)
2957 and then Known_Static_Esize (Ctyp)
2958 and then Esize (Ctyp) = Component_Size (Arr)
2959 and then not Is_Atomic_Or_VFA (Arr)
2960 then
2961 Set_Alignment (Arr, Alignment (Component_Type (Arr)));
2962 end if;
2964 -- A Ghost type cannot have a component of protected or task type
2965 -- (SPARK RM 6.9(19)).
2967 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
2968 Error_Msg_N
2969 ("ghost array type & cannot have concurrent component type",
2970 Arr);
2971 end if;
2972 end Freeze_Array_Type;
2974 -------------------------------
2975 -- Freeze_Object_Declaration --
2976 -------------------------------
2978 procedure Freeze_Object_Declaration (E : Entity_Id) is
2979 begin
2980 -- Abstract type allowed only for C++ imported variables or constants
2982 -- Note: we inhibit this check for objects that do not come from
2983 -- source because there is at least one case (the expansion of
2984 -- x'Class'Input where x is abstract) where we legitimately
2985 -- generate an abstract object.
2987 if Is_Abstract_Type (Etype (E))
2988 and then Comes_From_Source (Parent (E))
2989 and then not (Is_Imported (E) and then Is_CPP_Class (Etype (E)))
2990 then
2991 Error_Msg_N ("type of object cannot be abstract",
2992 Object_Definition (Parent (E)));
2994 if Is_CPP_Class (Etype (E)) then
2995 Error_Msg_NE
2996 ("\} may need a cpp_constructor",
2997 Object_Definition (Parent (E)), Etype (E));
2999 elsif Present (Expression (Parent (E))) then
3000 Error_Msg_N -- CODEFIX
3001 ("\maybe a class-wide type was meant",
3002 Object_Definition (Parent (E)));
3003 end if;
3004 end if;
3006 -- For object created by object declaration, perform required
3007 -- categorization (preelaborate and pure) checks. Defer these
3008 -- checks to freeze time since pragma Import inhibits default
3009 -- initialization and thus pragma Import affects these checks.
3011 Validate_Object_Declaration (Declaration_Node (E));
3013 -- If there is an address clause, check that it is valid
3014 -- and if need be move initialization to the freeze node.
3016 Check_Address_Clause (E);
3018 -- Similar processing is needed for aspects that may affect
3019 -- object layout, like Alignment, if there is an initialization
3020 -- expression.
3022 if Has_Delayed_Aspects (E)
3023 and then Expander_Active
3024 and then Is_Array_Type (Etype (E))
3025 and then Present (Expression (Parent (E)))
3026 then
3027 declare
3028 Decl : constant Node_Id := Parent (E);
3029 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
3031 begin
3033 -- Capture initialization value at point of declaration, and
3034 -- make explicit assignment legal, because object may be a
3035 -- constant.
3037 Remove_Side_Effects (Expression (Decl));
3038 Set_Assignment_OK (Lhs);
3040 -- Move initialization to freeze actions.
3042 Append_Freeze_Action (E,
3043 Make_Assignment_Statement (Loc,
3044 Name => Lhs,
3045 Expression => Expression (Decl)));
3047 Set_No_Initialization (Decl);
3048 -- Set_Is_Frozen (E, False);
3049 end;
3050 end if;
3052 -- Reset Is_True_Constant for non-constant aliased object. We
3053 -- consider that the fact that a non-constant object is aliased may
3054 -- indicate that some funny business is going on, e.g. an aliased
3055 -- object is passed by reference to a procedure which captures the
3056 -- address of the object, which is later used to assign a new value,
3057 -- even though the compiler thinks that it is not modified. Such
3058 -- code is highly dubious, but we choose to make it "work" for
3059 -- non-constant aliased objects.
3061 -- Note that we used to do this for all aliased objects, whether or
3062 -- not constant, but this caused anomalies down the line because we
3063 -- ended up with static objects that were not Is_True_Constant. Not
3064 -- resetting Is_True_Constant for (aliased) constant objects ensures
3065 -- that this anomaly never occurs.
3067 -- However, we don't do that for internal entities. We figure that if
3068 -- we deliberately set Is_True_Constant for an internal entity, e.g.
3069 -- a dispatch table entry, then we mean it.
3071 if Ekind (E) /= E_Constant
3072 and then (Is_Aliased (E) or else Is_Aliased (Etype (E)))
3073 and then not Is_Internal_Name (Chars (E))
3074 then
3075 Set_Is_True_Constant (E, False);
3076 end if;
3078 -- If the object needs any kind of default initialization, an error
3079 -- must be issued if No_Default_Initialization applies. The check
3080 -- doesn't apply to imported objects, which are not ever default
3081 -- initialized, and is why the check is deferred until freezing, at
3082 -- which point we know if Import applies. Deferred constants are also
3083 -- exempted from this test because their completion is explicit, or
3084 -- through an import pragma.
3086 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
3087 null;
3089 elsif Comes_From_Source (E)
3090 and then not Is_Imported (E)
3091 and then not Has_Init_Expression (Declaration_Node (E))
3092 and then
3093 ((Has_Non_Null_Base_Init_Proc (Etype (E))
3094 and then not No_Initialization (Declaration_Node (E))
3095 and then not Initialization_Suppressed (Etype (E)))
3096 or else
3097 (Needs_Simple_Initialization (Etype (E))
3098 and then not Is_Internal (E)))
3099 then
3100 Has_Default_Initialization := True;
3101 Check_Restriction
3102 (No_Default_Initialization, Declaration_Node (E));
3103 end if;
3105 -- Check that a Thread_Local_Storage variable does not have
3106 -- default initialization, and any explicit initialization must
3107 -- either be the null constant or a static constant.
3109 if Has_Pragma_Thread_Local_Storage (E) then
3110 declare
3111 Decl : constant Node_Id := Declaration_Node (E);
3112 begin
3113 if Has_Default_Initialization
3114 or else
3115 (Has_Init_Expression (Decl)
3116 and then
3117 (No (Expression (Decl))
3118 or else not
3119 (Is_OK_Static_Expression (Expression (Decl))
3120 or else Nkind (Expression (Decl)) = N_Null)))
3121 then
3122 Error_Msg_NE
3123 ("Thread_Local_Storage variable& is "
3124 & "improperly initialized", Decl, E);
3125 Error_Msg_NE
3126 ("\only allowed initialization is explicit "
3127 & "NULL or static expression", Decl, E);
3128 end if;
3129 end;
3130 end if;
3132 -- For imported objects, set Is_Public unless there is also an
3133 -- address clause, which means that there is no external symbol
3134 -- needed for the Import (Is_Public may still be set for other
3135 -- unrelated reasons). Note that we delayed this processing
3136 -- till freeze time so that we can be sure not to set the flag
3137 -- if there is an address clause. If there is such a clause,
3138 -- then the only purpose of the Import pragma is to suppress
3139 -- implicit initialization.
3141 if Is_Imported (E) and then No (Address_Clause (E)) then
3142 Set_Is_Public (E);
3143 end if;
3145 -- For source objects that are not Imported and are library
3146 -- level, if no linker section pragma was given inherit the
3147 -- appropriate linker section from the corresponding type.
3149 if Comes_From_Source (E)
3150 and then not Is_Imported (E)
3151 and then Is_Library_Level_Entity (E)
3152 and then No (Linker_Section_Pragma (E))
3153 then
3154 Set_Linker_Section_Pragma
3155 (E, Linker_Section_Pragma (Etype (E)));
3156 end if;
3158 -- For convention C objects of an enumeration type, warn if the
3159 -- size is not integer size and no explicit size given. Skip
3160 -- warning for Boolean, and Character, assume programmer expects
3161 -- 8-bit sizes for these cases.
3163 if (Convention (E) = Convention_C
3164 or else
3165 Convention (E) = Convention_CPP)
3166 and then Is_Enumeration_Type (Etype (E))
3167 and then not Is_Character_Type (Etype (E))
3168 and then not Is_Boolean_Type (Etype (E))
3169 and then Esize (Etype (E)) < Standard_Integer_Size
3170 and then not Has_Size_Clause (E)
3171 then
3172 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
3173 Error_Msg_N
3174 ("??convention C enumeration object has size less than ^", E);
3175 Error_Msg_N ("\??use explicit size clause to set size", E);
3176 end if;
3177 end Freeze_Object_Declaration;
3179 -----------------------------
3180 -- Freeze_Generic_Entities --
3181 -----------------------------
3183 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
3184 E : Entity_Id;
3185 F : Node_Id;
3186 Flist : List_Id;
3188 begin
3189 Flist := New_List;
3190 E := First_Entity (Pack);
3191 while Present (E) loop
3192 if Is_Type (E) and then not Is_Generic_Type (E) then
3193 F := Make_Freeze_Generic_Entity (Sloc (Pack));
3194 Set_Entity (F, E);
3195 Append_To (Flist, F);
3197 elsif Ekind (E) = E_Generic_Package then
3198 Append_List_To (Flist, Freeze_Generic_Entities (E));
3199 end if;
3201 Next_Entity (E);
3202 end loop;
3204 return Flist;
3205 end Freeze_Generic_Entities;
3207 --------------------
3208 -- Freeze_Profile --
3209 --------------------
3211 function Freeze_Profile (E : Entity_Id) return Boolean is
3212 F_Type : Entity_Id;
3213 R_Type : Entity_Id;
3214 Warn_Node : Node_Id;
3216 begin
3217 -- Loop through formals
3219 Formal := First_Formal (E);
3220 while Present (Formal) loop
3221 F_Type := Etype (Formal);
3223 -- AI05-0151: incomplete types can appear in a profile. By the
3224 -- time the entity is frozen, the full view must be available,
3225 -- unless it is a limited view.
3227 if Is_Incomplete_Type (F_Type)
3228 and then Present (Full_View (F_Type))
3229 and then not From_Limited_With (F_Type)
3230 then
3231 F_Type := Full_View (F_Type);
3232 Set_Etype (Formal, F_Type);
3233 end if;
3235 if not From_Limited_With (F_Type) then
3236 Freeze_And_Append (F_Type, N, Result);
3237 end if;
3239 if Is_Private_Type (F_Type)
3240 and then Is_Private_Type (Base_Type (F_Type))
3241 and then No (Full_View (Base_Type (F_Type)))
3242 and then not Is_Generic_Type (F_Type)
3243 and then not Is_Derived_Type (F_Type)
3244 then
3245 -- If the type of a formal is incomplete, subprogram is being
3246 -- frozen prematurely. Within an instance (but not within a
3247 -- wrapper package) this is an artifact of our need to regard
3248 -- the end of an instantiation as a freeze point. Otherwise it
3249 -- is a definite error.
3251 if In_Instance then
3252 Set_Is_Frozen (E, False);
3253 Result := No_List;
3254 return False;
3256 elsif not After_Last_Declaration
3257 and then not Freezing_Library_Level_Tagged_Type
3258 then
3259 Error_Msg_Node_1 := F_Type;
3260 Error_Msg
3261 ("type & must be fully defined before this point", Loc);
3262 end if;
3263 end if;
3265 -- Check suspicious parameter for C function. These tests apply
3266 -- only to exported/imported subprograms.
3268 if Warn_On_Export_Import
3269 and then Comes_From_Source (E)
3270 and then (Convention (E) = Convention_C
3271 or else
3272 Convention (E) = Convention_CPP)
3273 and then (Is_Imported (E) or else Is_Exported (E))
3274 and then Convention (E) /= Convention (Formal)
3275 and then not Has_Warnings_Off (E)
3276 and then not Has_Warnings_Off (F_Type)
3277 and then not Has_Warnings_Off (Formal)
3278 then
3279 -- Qualify mention of formals with subprogram name
3281 Error_Msg_Qual_Level := 1;
3283 -- Check suspicious use of fat C pointer
3285 if Is_Access_Type (F_Type)
3286 and then Esize (F_Type) > Ttypes.System_Address_Size
3287 then
3288 Error_Msg_N
3289 ("?x?type of & does not correspond to C pointer!", Formal);
3291 -- Check suspicious return of boolean
3293 elsif Root_Type (F_Type) = Standard_Boolean
3294 and then Convention (F_Type) = Convention_Ada
3295 and then not Has_Warnings_Off (F_Type)
3296 and then not Has_Size_Clause (F_Type)
3297 then
3298 Error_Msg_N
3299 ("& is an 8-bit Ada Boolean?x?", Formal);
3300 Error_Msg_N
3301 ("\use appropriate corresponding type in C "
3302 & "(e.g. char)?x?", Formal);
3304 -- Check suspicious tagged type
3306 elsif (Is_Tagged_Type (F_Type)
3307 or else
3308 (Is_Access_Type (F_Type)
3309 and then Is_Tagged_Type (Designated_Type (F_Type))))
3310 and then Convention (E) = Convention_C
3311 then
3312 Error_Msg_N
3313 ("?x?& involves a tagged type which does not "
3314 & "correspond to any C type!", Formal);
3316 -- Check wrong convention subprogram pointer
3318 elsif Ekind (F_Type) = E_Access_Subprogram_Type
3319 and then not Has_Foreign_Convention (F_Type)
3320 then
3321 Error_Msg_N
3322 ("?x?subprogram pointer & should "
3323 & "have foreign convention!", Formal);
3324 Error_Msg_Sloc := Sloc (F_Type);
3325 Error_Msg_NE
3326 ("\?x?add Convention pragma to declaration of &#",
3327 Formal, F_Type);
3328 end if;
3330 -- Turn off name qualification after message output
3332 Error_Msg_Qual_Level := 0;
3333 end if;
3335 -- Check for unconstrained array in exported foreign convention
3336 -- case.
3338 if Has_Foreign_Convention (E)
3339 and then not Is_Imported (E)
3340 and then Is_Array_Type (F_Type)
3341 and then not Is_Constrained (F_Type)
3342 and then Warn_On_Export_Import
3343 then
3344 Error_Msg_Qual_Level := 1;
3346 -- If this is an inherited operation, place the warning on
3347 -- the derived type declaration, rather than on the original
3348 -- subprogram.
3350 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
3351 then
3352 Warn_Node := Parent (E);
3354 if Formal = First_Formal (E) then
3355 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
3356 end if;
3357 else
3358 Warn_Node := Formal;
3359 end if;
3361 Error_Msg_NE ("?x?type of argument& is unconstrained array",
3362 Warn_Node, Formal);
3363 Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
3364 Warn_Node, Formal);
3365 Error_Msg_Qual_Level := 0;
3366 end if;
3368 if not From_Limited_With (F_Type) then
3369 if Is_Access_Type (F_Type) then
3370 F_Type := Designated_Type (F_Type);
3371 end if;
3373 -- If the formal is an anonymous_access_to_subprogram
3374 -- freeze the subprogram type as well, to prevent
3375 -- scope anomalies in gigi, because there is no other
3376 -- clear point at which it could be frozen.
3378 if Is_Itype (Etype (Formal))
3379 and then Ekind (F_Type) = E_Subprogram_Type
3380 then
3381 Freeze_And_Append (F_Type, N, Result);
3382 end if;
3383 end if;
3385 Next_Formal (Formal);
3386 end loop;
3388 -- Case of function: similar checks on return type
3390 if Ekind (E) = E_Function then
3392 -- Freeze return type
3394 R_Type := Etype (E);
3396 -- AI05-0151: the return type may have been incomplete
3397 -- at the point of declaration. Replace it with the full
3398 -- view, unless the current type is a limited view. In
3399 -- that case the full view is in a different unit, and
3400 -- gigi finds the non-limited view after the other unit
3401 -- is elaborated.
3403 if Ekind (R_Type) = E_Incomplete_Type
3404 and then Present (Full_View (R_Type))
3405 and then not From_Limited_With (R_Type)
3406 then
3407 R_Type := Full_View (R_Type);
3408 Set_Etype (E, R_Type);
3409 end if;
3411 Freeze_And_Append (R_Type, N, Result);
3413 -- Check suspicious return type for C function
3415 if Warn_On_Export_Import
3416 and then (Convention (E) = Convention_C
3417 or else
3418 Convention (E) = Convention_CPP)
3419 and then (Is_Imported (E) or else Is_Exported (E))
3420 then
3421 -- Check suspicious return of fat C pointer
3423 if Is_Access_Type (R_Type)
3424 and then Esize (R_Type) > Ttypes.System_Address_Size
3425 and then not Has_Warnings_Off (E)
3426 and then not Has_Warnings_Off (R_Type)
3427 then
3428 Error_Msg_N ("?x?return type of& does not "
3429 & "correspond to C pointer!", E);
3431 -- Check suspicious return of boolean
3433 elsif Root_Type (R_Type) = Standard_Boolean
3434 and then Convention (R_Type) = Convention_Ada
3435 and then not Has_Warnings_Off (E)
3436 and then not Has_Warnings_Off (R_Type)
3437 and then not Has_Size_Clause (R_Type)
3438 then
3439 declare
3440 N : constant Node_Id :=
3441 Result_Definition (Declaration_Node (E));
3442 begin
3443 Error_Msg_NE
3444 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
3445 Error_Msg_NE
3446 ("\use appropriate corresponding type in C "
3447 & "(e.g. char)?x?", N, E);
3448 end;
3450 -- Check suspicious return tagged type
3452 elsif (Is_Tagged_Type (R_Type)
3453 or else (Is_Access_Type (R_Type)
3454 and then
3455 Is_Tagged_Type
3456 (Designated_Type (R_Type))))
3457 and then Convention (E) = Convention_C
3458 and then not Has_Warnings_Off (E)
3459 and then not Has_Warnings_Off (R_Type)
3460 then
3461 Error_Msg_N ("?x?return type of & does not "
3462 & "correspond to C type!", E);
3464 -- Check return of wrong convention subprogram pointer
3466 elsif Ekind (R_Type) = E_Access_Subprogram_Type
3467 and then not Has_Foreign_Convention (R_Type)
3468 and then not Has_Warnings_Off (E)
3469 and then not Has_Warnings_Off (R_Type)
3470 then
3471 Error_Msg_N ("?x?& should return a foreign "
3472 & "convention subprogram pointer", E);
3473 Error_Msg_Sloc := Sloc (R_Type);
3474 Error_Msg_NE
3475 ("\?x?add Convention pragma to declaration of& #",
3476 E, R_Type);
3477 end if;
3478 end if;
3480 -- Give warning for suspicious return of a result of an
3481 -- unconstrained array type in a foreign convention function.
3483 if Has_Foreign_Convention (E)
3485 -- We are looking for a return of unconstrained array
3487 and then Is_Array_Type (R_Type)
3488 and then not Is_Constrained (R_Type)
3490 -- Exclude imported routines, the warning does not belong on
3491 -- the import, but rather on the routine definition.
3493 and then not Is_Imported (E)
3495 -- Check that general warning is enabled, and that it is not
3496 -- suppressed for this particular case.
3498 and then Warn_On_Export_Import
3499 and then not Has_Warnings_Off (E)
3500 and then not Has_Warnings_Off (R_Type)
3501 then
3502 Error_Msg_N
3503 ("?x?foreign convention function& should not return "
3504 & "unconstrained array!", E);
3505 end if;
3506 end if;
3508 -- Check suspicious use of Import in pure unit (cases where the RM
3509 -- allows calls to be omitted).
3511 if Is_Imported (E)
3513 -- It might be suspicious if the compilation unit has the Pure
3514 -- aspect/pragma.
3516 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
3518 -- The RM allows omission of calls only in the case of
3519 -- library-level subprograms (see RM-10.2.1(18)).
3521 and then Is_Library_Level_Entity (E)
3523 -- Ignore internally generated entity. This happens in some cases
3524 -- of subprograms in specs, where we generate an implied body.
3526 and then Comes_From_Source (Import_Pragma (E))
3528 -- Assume run-time knows what it is doing
3530 and then not GNAT_Mode
3532 -- Assume explicit Pure_Function means import is pure
3534 and then not Has_Pragma_Pure_Function (E)
3536 -- Don't need warning in relaxed semantics mode
3538 and then not Relaxed_RM_Semantics
3540 -- Assume convention Intrinsic is OK, since this is specialized.
3541 -- This deals with the DEC unit current_exception.ads
3543 and then Convention (E) /= Convention_Intrinsic
3545 -- Assume that ASM interface knows what it is doing. This deals
3546 -- with e.g. unsigned.ads in the AAMP back end.
3548 and then Convention (E) /= Convention_Assembler
3549 then
3550 Error_Msg_N
3551 ("pragma Import in Pure unit??", Import_Pragma (E));
3552 Error_Msg_NE
3553 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
3554 Import_Pragma (E), E);
3555 end if;
3557 return True;
3558 end Freeze_Profile;
3560 ------------------------
3561 -- Freeze_Record_Type --
3562 ------------------------
3564 procedure Freeze_Record_Type (Rec : Entity_Id) is
3565 ADC : Node_Id;
3566 Comp : Entity_Id;
3567 IR : Node_Id;
3568 Prev : Entity_Id;
3570 Junk : Boolean;
3571 pragma Warnings (Off, Junk);
3573 Aliased_Component : Boolean := False;
3574 -- Set True if we find at least one component which is aliased. This
3575 -- is used to prevent Implicit_Packing of the record, since packing
3576 -- cannot modify the size of alignment of an aliased component.
3578 All_Elem_Components : Boolean := True;
3579 -- Set False if we encounter a component of a composite type
3581 All_Sized_Components : Boolean := True;
3582 -- Set False if we encounter a component with unknown RM_Size
3584 All_Storage_Unit_Components : Boolean := True;
3585 -- Set False if we encounter a component of a composite type whose
3586 -- RM_Size is not a multiple of the storage unit.
3588 Elem_Component_Total_Esize : Uint := Uint_0;
3589 -- Accumulates total Esize values of all elementary components. Used
3590 -- for processing of Implicit_Packing.
3592 Placed_Component : Boolean := False;
3593 -- Set True if we find at least one component with a component
3594 -- clause (used to warn about useless Bit_Order pragmas, and also
3595 -- to detect cases where Implicit_Packing may have an effect).
3597 Rec_Pushed : Boolean := False;
3598 -- Set True if the record type scope Rec has been pushed on the scope
3599 -- stack. Needed for the analysis of delayed aspects specified to the
3600 -- components of Rec.
3602 Sized_Component_Total_RM_Size : Uint := Uint_0;
3603 -- Accumulates total RM_Size values of all sized components. Used
3604 -- for processing of Implicit_Packing.
3606 SSO_ADC : Node_Id;
3607 -- Scalar_Storage_Order attribute definition clause for the record
3609 SSO_ADC_Component : Boolean := False;
3610 -- Set True if we find at least one component whose type has a
3611 -- Scalar_Storage_Order attribute definition clause.
3613 Unplaced_Component : Boolean := False;
3614 -- Set True if we find at least one component with no component
3615 -- clause (used to warn about useless Pack pragmas).
3617 function Check_Allocator (N : Node_Id) return Node_Id;
3618 -- If N is an allocator, possibly wrapped in one or more level of
3619 -- qualified expression(s), return the inner allocator node, else
3620 -- return Empty.
3622 procedure Check_Itype (Typ : Entity_Id);
3623 -- If the component subtype is an access to a constrained subtype of
3624 -- an already frozen type, make the subtype frozen as well. It might
3625 -- otherwise be frozen in the wrong scope, and a freeze node on
3626 -- subtype has no effect. Similarly, if the component subtype is a
3627 -- regular (not protected) access to subprogram, set the anonymous
3628 -- subprogram type to frozen as well, to prevent an out-of-scope
3629 -- freeze node at some eventual point of call. Protected operations
3630 -- are handled elsewhere.
3632 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3633 -- Make sure that all types mentioned in Discrete_Choices of the
3634 -- variants referenceed by the Variant_Part VP are frozen. This is
3635 -- a recursive routine to deal with nested variants.
3637 ---------------------
3638 -- Check_Allocator --
3639 ---------------------
3641 function Check_Allocator (N : Node_Id) return Node_Id is
3642 Inner : Node_Id;
3643 begin
3644 Inner := N;
3645 loop
3646 if Nkind (Inner) = N_Allocator then
3647 return Inner;
3648 elsif Nkind (Inner) = N_Qualified_Expression then
3649 Inner := Expression (Inner);
3650 else
3651 return Empty;
3652 end if;
3653 end loop;
3654 end Check_Allocator;
3656 -----------------
3657 -- Check_Itype --
3658 -----------------
3660 procedure Check_Itype (Typ : Entity_Id) is
3661 Desig : constant Entity_Id := Designated_Type (Typ);
3663 begin
3664 if not Is_Frozen (Desig)
3665 and then Is_Frozen (Base_Type (Desig))
3666 then
3667 Set_Is_Frozen (Desig);
3669 -- In addition, add an Itype_Reference to ensure that the
3670 -- access subtype is elaborated early enough. This cannot be
3671 -- done if the subtype may depend on discriminants.
3673 if Ekind (Comp) = E_Component
3674 and then Is_Itype (Etype (Comp))
3675 and then not Has_Discriminants (Rec)
3676 then
3677 IR := Make_Itype_Reference (Sloc (Comp));
3678 Set_Itype (IR, Desig);
3679 Add_To_Result (IR);
3680 end if;
3682 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
3683 and then Convention (Desig) /= Convention_Protected
3684 then
3685 Set_Is_Frozen (Desig);
3686 end if;
3687 end Check_Itype;
3689 ------------------------------------
3690 -- Freeze_Choices_In_Variant_Part --
3691 ------------------------------------
3693 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
3694 pragma Assert (Nkind (VP) = N_Variant_Part);
3696 Variant : Node_Id;
3697 Choice : Node_Id;
3698 CL : Node_Id;
3700 begin
3701 -- Loop through variants
3703 Variant := First_Non_Pragma (Variants (VP));
3704 while Present (Variant) loop
3706 -- Loop through choices, checking that all types are frozen
3708 Choice := First_Non_Pragma (Discrete_Choices (Variant));
3709 while Present (Choice) loop
3710 if Nkind (Choice) in N_Has_Etype
3711 and then Present (Etype (Choice))
3712 then
3713 Freeze_And_Append (Etype (Choice), N, Result);
3714 end if;
3716 Next_Non_Pragma (Choice);
3717 end loop;
3719 -- Check for nested variant part to process
3721 CL := Component_List (Variant);
3723 if not Null_Present (CL) then
3724 if Present (Variant_Part (CL)) then
3725 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
3726 end if;
3727 end if;
3729 Next_Non_Pragma (Variant);
3730 end loop;
3731 end Freeze_Choices_In_Variant_Part;
3733 -- Start of processing for Freeze_Record_Type
3735 begin
3736 -- Deal with delayed aspect specifications for components. The
3737 -- analysis of the aspect is required to be delayed to the freeze
3738 -- point, thus we analyze the pragma or attribute definition
3739 -- clause in the tree at this point. We also analyze the aspect
3740 -- specification node at the freeze point when the aspect doesn't
3741 -- correspond to pragma/attribute definition clause.
3743 Comp := First_Entity (Rec);
3744 while Present (Comp) loop
3745 if Ekind (Comp) = E_Component
3746 and then Has_Delayed_Aspects (Comp)
3747 then
3748 if not Rec_Pushed then
3749 Push_Scope (Rec);
3750 Rec_Pushed := True;
3752 -- The visibility to the discriminants must be restored in
3753 -- order to properly analyze the aspects.
3755 if Has_Discriminants (Rec) then
3756 Install_Discriminants (Rec);
3757 end if;
3758 end if;
3760 Analyze_Aspects_At_Freeze_Point (Comp);
3761 end if;
3763 Next_Entity (Comp);
3764 end loop;
3766 -- Pop the scope if Rec scope has been pushed on the scope stack
3767 -- during the delayed aspect analysis process.
3769 if Rec_Pushed then
3770 if Has_Discriminants (Rec) then
3771 Uninstall_Discriminants (Rec);
3772 end if;
3774 Pop_Scope;
3775 end if;
3777 -- Freeze components and embedded subtypes
3779 Comp := First_Entity (Rec);
3780 Prev := Empty;
3781 while Present (Comp) loop
3782 if Is_Aliased (Comp) then
3783 Aliased_Component := True;
3784 end if;
3786 -- Handle the component and discriminant case
3788 if Ekind_In (Comp, E_Component, E_Discriminant) then
3789 declare
3790 CC : constant Node_Id := Component_Clause (Comp);
3792 begin
3793 -- Freezing a record type freezes the type of each of its
3794 -- components. However, if the type of the component is
3795 -- part of this record, we do not want or need a separate
3796 -- Freeze_Node. Note that Is_Itype is wrong because that's
3797 -- also set in private type cases. We also can't check for
3798 -- the Scope being exactly Rec because of private types and
3799 -- record extensions.
3801 if Is_Itype (Etype (Comp))
3802 and then Is_Record_Type (Underlying_Type
3803 (Scope (Etype (Comp))))
3804 then
3805 Undelay_Type (Etype (Comp));
3806 end if;
3808 Freeze_And_Append (Etype (Comp), N, Result);
3810 -- Warn for pragma Pack overriding foreign convention
3812 if Has_Foreign_Convention (Etype (Comp))
3813 and then Has_Pragma_Pack (Rec)
3815 -- Don't warn for aliased components, since override
3816 -- cannot happen in that case.
3818 and then not Is_Aliased (Comp)
3819 then
3820 declare
3821 CN : constant Name_Id :=
3822 Get_Convention_Name (Convention (Etype (Comp)));
3823 PP : constant Node_Id :=
3824 Get_Pragma (Rec, Pragma_Pack);
3825 begin
3826 if Present (PP) then
3827 Error_Msg_Name_1 := CN;
3828 Error_Msg_Sloc := Sloc (Comp);
3829 Error_Msg_N
3830 ("pragma Pack affects convention % component#??",
3831 PP);
3832 Error_Msg_Name_1 := CN;
3833 Error_Msg_NE
3834 ("\component & may not have % compatible "
3835 & "representation??", PP, Comp);
3836 end if;
3837 end;
3838 end if;
3840 -- Check for error of component clause given for variable
3841 -- sized type. We have to delay this test till this point,
3842 -- since the component type has to be frozen for us to know
3843 -- if it is variable length.
3845 if Present (CC) then
3846 Placed_Component := True;
3848 -- We omit this test in a generic context, it will be
3849 -- applied at instantiation time.
3851 if Inside_A_Generic then
3852 null;
3854 -- Also omit this test in CodePeer mode, since we do not
3855 -- have sufficient info on size and rep clauses.
3857 elsif CodePeer_Mode then
3858 null;
3860 -- Omit check if component has a generic type. This can
3861 -- happen in an instantiation within a generic in ASIS
3862 -- mode, where we force freeze actions without full
3863 -- expansion.
3865 elsif Is_Generic_Type (Etype (Comp)) then
3866 null;
3868 -- Do the check
3870 elsif not
3871 Size_Known_At_Compile_Time
3872 (Underlying_Type (Etype (Comp)))
3873 then
3874 Error_Msg_N
3875 ("component clause not allowed for variable " &
3876 "length component", CC);
3877 end if;
3879 else
3880 Unplaced_Component := True;
3881 end if;
3883 -- Case of component requires byte alignment
3885 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
3887 -- Set the enclosing record to also require byte align
3889 Set_Must_Be_On_Byte_Boundary (Rec);
3891 -- Check for component clause that is inconsistent with
3892 -- the required byte boundary alignment.
3894 if Present (CC)
3895 and then Normalized_First_Bit (Comp) mod
3896 System_Storage_Unit /= 0
3897 then
3898 Error_Msg_N
3899 ("component & must be byte aligned",
3900 Component_Name (Component_Clause (Comp)));
3901 end if;
3902 end if;
3903 end;
3904 end if;
3906 -- Gather data for possible Implicit_Packing later. Note that at
3907 -- this stage we might be dealing with a real component, or with
3908 -- an implicit subtype declaration.
3910 if Known_Static_RM_Size (Etype (Comp)) then
3911 Sized_Component_Total_RM_Size :=
3912 Sized_Component_Total_RM_Size + RM_Size (Etype (Comp));
3914 if Is_Elementary_Type (Etype (Comp)) then
3915 Elem_Component_Total_Esize :=
3916 Elem_Component_Total_Esize + Esize (Etype (Comp));
3917 else
3918 All_Elem_Components := False;
3920 if RM_Size (Etype (Comp)) mod System_Storage_Unit /= 0 then
3921 All_Storage_Unit_Components := False;
3922 end if;
3923 end if;
3924 else
3925 All_Sized_Components := False;
3926 end if;
3928 -- If the component is an Itype with Delayed_Freeze and is either
3929 -- a record or array subtype and its base type has not yet been
3930 -- frozen, we must remove this from the entity list of this record
3931 -- and put it on the entity list of the scope of its base type.
3932 -- Note that we know that this is not the type of a component
3933 -- since we cleared Has_Delayed_Freeze for it in the previous
3934 -- loop. Thus this must be the Designated_Type of an access type,
3935 -- which is the type of a component.
3937 if Is_Itype (Comp)
3938 and then Is_Type (Scope (Comp))
3939 and then Is_Composite_Type (Comp)
3940 and then Base_Type (Comp) /= Comp
3941 and then Has_Delayed_Freeze (Comp)
3942 and then not Is_Frozen (Base_Type (Comp))
3943 then
3944 declare
3945 Will_Be_Frozen : Boolean := False;
3946 S : Entity_Id;
3948 begin
3949 -- We have a difficult case to handle here. Suppose Rec is
3950 -- subtype being defined in a subprogram that's created as
3951 -- part of the freezing of Rec'Base. In that case, we know
3952 -- that Comp'Base must have already been frozen by the time
3953 -- we get to elaborate this because Gigi doesn't elaborate
3954 -- any bodies until it has elaborated all of the declarative
3955 -- part. But Is_Frozen will not be set at this point because
3956 -- we are processing code in lexical order.
3958 -- We detect this case by going up the Scope chain of Rec
3959 -- and seeing if we have a subprogram scope before reaching
3960 -- the top of the scope chain or that of Comp'Base. If we
3961 -- do, then mark that Comp'Base will actually be frozen. If
3962 -- so, we merely undelay it.
3964 S := Scope (Rec);
3965 while Present (S) loop
3966 if Is_Subprogram (S) then
3967 Will_Be_Frozen := True;
3968 exit;
3969 elsif S = Scope (Base_Type (Comp)) then
3970 exit;
3971 end if;
3973 S := Scope (S);
3974 end loop;
3976 if Will_Be_Frozen then
3977 Undelay_Type (Comp);
3979 else
3980 if Present (Prev) then
3981 Set_Next_Entity (Prev, Next_Entity (Comp));
3982 else
3983 Set_First_Entity (Rec, Next_Entity (Comp));
3984 end if;
3986 -- Insert in entity list of scope of base type (which
3987 -- must be an enclosing scope, because still unfrozen).
3989 Append_Entity (Comp, Scope (Base_Type (Comp)));
3990 end if;
3991 end;
3993 -- If the component is an access type with an allocator as default
3994 -- value, the designated type will be frozen by the corresponding
3995 -- expression in init_proc. In order to place the freeze node for
3996 -- the designated type before that for the current record type,
3997 -- freeze it now.
3999 -- Same process if the component is an array of access types,
4000 -- initialized with an aggregate. If the designated type is
4001 -- private, it cannot contain allocators, and it is premature
4002 -- to freeze the type, so we check for this as well.
4004 elsif Is_Access_Type (Etype (Comp))
4005 and then Present (Parent (Comp))
4006 and then Present (Expression (Parent (Comp)))
4007 then
4008 declare
4009 Alloc : constant Node_Id :=
4010 Check_Allocator (Expression (Parent (Comp)));
4012 begin
4013 if Present (Alloc) then
4015 -- If component is pointer to a class-wide type, freeze
4016 -- the specific type in the expression being allocated.
4017 -- The expression may be a subtype indication, in which
4018 -- case freeze the subtype mark.
4020 if Is_Class_Wide_Type
4021 (Designated_Type (Etype (Comp)))
4022 then
4023 if Is_Entity_Name (Expression (Alloc)) then
4024 Freeze_And_Append
4025 (Entity (Expression (Alloc)), N, Result);
4027 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
4028 then
4029 Freeze_And_Append
4030 (Entity (Subtype_Mark (Expression (Alloc))),
4031 N, Result);
4032 end if;
4034 elsif Is_Itype (Designated_Type (Etype (Comp))) then
4035 Check_Itype (Etype (Comp));
4037 else
4038 Freeze_And_Append
4039 (Designated_Type (Etype (Comp)), N, Result);
4040 end if;
4041 end if;
4042 end;
4044 elsif Is_Access_Type (Etype (Comp))
4045 and then Is_Itype (Designated_Type (Etype (Comp)))
4046 then
4047 Check_Itype (Etype (Comp));
4049 -- Freeze the designated type when initializing a component with
4050 -- an aggregate in case the aggregate contains allocators.
4052 -- type T is ...;
4053 -- type T_Ptr is access all T;
4054 -- type T_Array is array ... of T_Ptr;
4056 -- type Rec is record
4057 -- Comp : T_Array := (others => ...);
4058 -- end record;
4060 elsif Is_Array_Type (Etype (Comp))
4061 and then Is_Access_Type (Component_Type (Etype (Comp)))
4062 then
4063 declare
4064 Comp_Par : constant Node_Id := Parent (Comp);
4065 Desig_Typ : constant Entity_Id :=
4066 Designated_Type
4067 (Component_Type (Etype (Comp)));
4069 begin
4070 -- The only case when this sort of freezing is not done is
4071 -- when the designated type is class-wide and the root type
4072 -- is the record owning the component. This scenario results
4073 -- in a circularity because the class-wide type requires
4074 -- primitives that have not been created yet as the root
4075 -- type is in the process of being frozen.
4077 -- type Rec is tagged;
4078 -- type Rec_Ptr is access all Rec'Class;
4079 -- type Rec_Array is array ... of Rec_Ptr;
4081 -- type Rec is record
4082 -- Comp : Rec_Array := (others => ...);
4083 -- end record;
4085 if Is_Class_Wide_Type (Desig_Typ)
4086 and then Root_Type (Desig_Typ) = Rec
4087 then
4088 null;
4090 elsif Is_Fully_Defined (Desig_Typ)
4091 and then Present (Comp_Par)
4092 and then Nkind (Comp_Par) = N_Component_Declaration
4093 and then Present (Expression (Comp_Par))
4094 and then Nkind (Expression (Comp_Par)) = N_Aggregate
4095 then
4096 Freeze_And_Append (Desig_Typ, N, Result);
4097 end if;
4098 end;
4099 end if;
4101 Prev := Comp;
4102 Next_Entity (Comp);
4103 end loop;
4105 SSO_ADC :=
4106 Get_Attribute_Definition_Clause
4107 (Rec, Attribute_Scalar_Storage_Order);
4109 -- If the record type has Complex_Representation, then it is treated
4110 -- as a scalar in the back end so the storage order is irrelevant.
4112 if Has_Complex_Representation (Rec) then
4113 if Present (SSO_ADC) then
4114 Error_Msg_N
4115 ("??storage order has no effect with Complex_Representation",
4116 SSO_ADC);
4117 end if;
4119 else
4120 -- Deal with default setting of reverse storage order
4122 Set_SSO_From_Default (Rec);
4124 -- Check consistent attribute setting on component types
4126 declare
4127 Comp_ADC_Present : Boolean;
4128 begin
4129 Comp := First_Component (Rec);
4130 while Present (Comp) loop
4131 Check_Component_Storage_Order
4132 (Encl_Type => Rec,
4133 Comp => Comp,
4134 ADC => SSO_ADC,
4135 Comp_ADC_Present => Comp_ADC_Present);
4136 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
4137 Next_Component (Comp);
4138 end loop;
4139 end;
4141 -- Now deal with reverse storage order/bit order issues
4143 if Present (SSO_ADC) then
4145 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
4146 -- if the former is specified.
4148 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
4150 -- Note: report error on Rec, not on SSO_ADC, as ADC may
4151 -- apply to some ancestor type.
4153 Error_Msg_Sloc := Sloc (SSO_ADC);
4154 Error_Msg_N
4155 ("scalar storage order for& specified# inconsistent with "
4156 & "bit order", Rec);
4157 end if;
4159 -- Warn if there is a Scalar_Storage_Order attribute definition
4160 -- clause but no component clause, no component that itself has
4161 -- such an attribute definition, and no pragma Pack.
4163 if not (Placed_Component
4164 or else
4165 SSO_ADC_Component
4166 or else
4167 Is_Packed (Rec))
4168 then
4169 Error_Msg_N
4170 ("??scalar storage order specified but no component "
4171 & "clause", SSO_ADC);
4172 end if;
4173 end if;
4174 end if;
4176 -- Deal with Bit_Order aspect
4178 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
4180 if Present (ADC) and then Base_Type (Rec) = Rec then
4181 if not (Placed_Component
4182 or else Present (SSO_ADC)
4183 or else Is_Packed (Rec))
4184 then
4185 -- Warn if clause has no effect when no component clause is
4186 -- present, but suppress warning if the Bit_Order is required
4187 -- due to the presence of a Scalar_Storage_Order attribute.
4189 Error_Msg_N
4190 ("??bit order specification has no effect", ADC);
4191 Error_Msg_N
4192 ("\??since no component clauses were specified", ADC);
4194 -- Here is where we do the processing to adjust component clauses
4195 -- for reversed bit order, when not using reverse SSO.
4197 elsif Reverse_Bit_Order (Rec)
4198 and then not Reverse_Storage_Order (Rec)
4199 then
4200 Adjust_Record_For_Reverse_Bit_Order (Rec);
4202 -- Case where we have both an explicit Bit_Order and the same
4203 -- Scalar_Storage_Order: leave record untouched, the back-end
4204 -- will take care of required layout conversions.
4206 else
4207 null;
4209 end if;
4210 end if;
4212 -- Complete error checking on record representation clause (e.g.
4213 -- overlap of components). This is called after adjusting the
4214 -- record for reverse bit order.
4216 declare
4217 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
4218 begin
4219 if Present (RRC) then
4220 Check_Record_Representation_Clause (RRC);
4221 end if;
4222 end;
4224 -- Set OK_To_Reorder_Components depending on debug flags
4226 if Is_Base_Type (Rec) and then Convention (Rec) = Convention_Ada then
4227 if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
4228 or else
4229 (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
4230 then
4231 Set_OK_To_Reorder_Components (Rec);
4232 end if;
4233 end if;
4235 -- Check for useless pragma Pack when all components placed. We only
4236 -- do this check for record types, not subtypes, since a subtype may
4237 -- have all its components placed, and it still makes perfectly good
4238 -- sense to pack other subtypes or the parent type. We do not give
4239 -- this warning if Optimize_Alignment is set to Space, since the
4240 -- pragma Pack does have an effect in this case (it always resets
4241 -- the alignment to one).
4243 if Ekind (Rec) = E_Record_Type
4244 and then Is_Packed (Rec)
4245 and then not Unplaced_Component
4246 and then Optimize_Alignment /= 'S'
4247 then
4248 -- Reset packed status. Probably not necessary, but we do it so
4249 -- that there is no chance of the back end doing something strange
4250 -- with this redundant indication of packing.
4252 Set_Is_Packed (Rec, False);
4254 -- Give warning if redundant constructs warnings on
4256 if Warn_On_Redundant_Constructs then
4257 Error_Msg_N -- CODEFIX
4258 ("??pragma Pack has no effect, no unplaced components",
4259 Get_Rep_Pragma (Rec, Name_Pack));
4260 end if;
4261 end if;
4263 -- If this is the record corresponding to a remote type, freeze the
4264 -- remote type here since that is what we are semantically freezing.
4265 -- This prevents the freeze node for that type in an inner scope.
4267 if Ekind (Rec) = E_Record_Type then
4268 if Present (Corresponding_Remote_Type (Rec)) then
4269 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
4270 end if;
4272 -- Check for controlled components, unchecked unions, and type
4273 -- invariants.
4275 Comp := First_Component (Rec);
4276 while Present (Comp) loop
4278 -- Do not set Has_Controlled_Component on a class-wide
4279 -- equivalent type. See Make_CW_Equivalent_Type.
4281 if not Is_Class_Wide_Equivalent_Type (Rec)
4282 and then
4283 (Has_Controlled_Component (Etype (Comp))
4284 or else
4285 (Chars (Comp) /= Name_uParent
4286 and then Is_Controlled_Active (Etype (Comp)))
4287 or else
4288 (Is_Protected_Type (Etype (Comp))
4289 and then
4290 Present (Corresponding_Record_Type (Etype (Comp)))
4291 and then
4292 Has_Controlled_Component
4293 (Corresponding_Record_Type (Etype (Comp)))))
4294 then
4295 Set_Has_Controlled_Component (Rec);
4296 end if;
4298 if Has_Unchecked_Union (Etype (Comp)) then
4299 Set_Has_Unchecked_Union (Rec);
4300 end if;
4302 -- The record type requires its own invariant procedure in
4303 -- order to verify the invariant of each individual component.
4304 -- Do not consider internal components such as _parent because
4305 -- parent class-wide invariants are always inherited.
4307 if Comes_From_Source (Comp)
4308 and then
4309 (Has_Invariants (Etype (Comp))
4310 or else
4311 (Is_Access_Type (Etype (Comp))
4312 and then Has_Invariants
4313 (Designated_Type (Etype (Comp)))))
4314 then
4315 Set_Has_Own_Invariants (Rec);
4316 end if;
4318 -- Scan component declaration for likely misuses of current
4319 -- instance, either in a constraint or a default expression.
4321 if Has_Per_Object_Constraint (Comp) then
4322 Check_Current_Instance (Parent (Comp));
4323 end if;
4325 Next_Component (Comp);
4326 end loop;
4327 end if;
4329 -- Enforce the restriction that access attributes with a current
4330 -- instance prefix can only apply to limited types. This comment
4331 -- is floating here, but does not seem to belong here???
4333 -- Set component alignment if not otherwise already set
4335 Set_Component_Alignment_If_Not_Set (Rec);
4337 -- For first subtypes, check if there are any fixed-point fields with
4338 -- component clauses, where we must check the size. This is not done
4339 -- till the freeze point since for fixed-point types, we do not know
4340 -- the size until the type is frozen. Similar processing applies to
4341 -- bit-packed arrays.
4343 if Is_First_Subtype (Rec) then
4344 Comp := First_Component (Rec);
4345 while Present (Comp) loop
4346 if Present (Component_Clause (Comp))
4347 and then (Is_Fixed_Point_Type (Etype (Comp))
4348 or else Is_Bit_Packed_Array (Etype (Comp)))
4349 then
4350 Check_Size
4351 (Component_Name (Component_Clause (Comp)),
4352 Etype (Comp),
4353 Esize (Comp),
4354 Junk);
4355 end if;
4357 Next_Component (Comp);
4358 end loop;
4359 end if;
4361 -- Generate warning for applying C or C++ convention to a record
4362 -- with discriminants. This is suppressed for the unchecked union
4363 -- case, since the whole point in this case is interface C. We also
4364 -- do not generate this within instantiations, since we will have
4365 -- generated a message on the template.
4367 if Has_Discriminants (E)
4368 and then not Is_Unchecked_Union (E)
4369 and then (Convention (E) = Convention_C
4370 or else
4371 Convention (E) = Convention_CPP)
4372 and then Comes_From_Source (E)
4373 and then not In_Instance
4374 and then not Has_Warnings_Off (E)
4375 and then not Has_Warnings_Off (Base_Type (E))
4376 then
4377 declare
4378 Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
4379 A2 : Node_Id;
4381 begin
4382 if Present (Cprag) then
4383 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
4385 if Convention (E) = Convention_C then
4386 Error_Msg_N
4387 ("?x?variant record has no direct equivalent in C",
4388 A2);
4389 else
4390 Error_Msg_N
4391 ("?x?variant record has no direct equivalent in C++",
4392 A2);
4393 end if;
4395 Error_Msg_NE
4396 ("\?x?use of convention for type& is dubious", A2, E);
4397 end if;
4398 end;
4399 end if;
4401 -- See if Size is too small as is (and implicit packing might help)
4403 if not Is_Packed (Rec)
4405 -- No implicit packing if even one component is explicitly placed
4407 and then not Placed_Component
4409 -- Or even one component is aliased
4411 and then not Aliased_Component
4413 -- Must have size clause and all sized components
4415 and then Has_Size_Clause (Rec)
4416 and then All_Sized_Components
4418 -- Do not try implicit packing on records with discriminants, too
4419 -- complicated, especially in the variant record case.
4421 and then not Has_Discriminants (Rec)
4423 -- We want to implicitly pack if the specified size of the record
4424 -- is less than the sum of the object sizes (no point in packing
4425 -- if this is not the case), if we can compute it, i.e. if we have
4426 -- only elementary components. Otherwise, we have at least one
4427 -- composite component and we want to implicitly pack only if bit
4428 -- packing is required for it, as we are sure in this case that
4429 -- the back end cannot do the expected layout without packing.
4431 and then
4432 ((All_Elem_Components
4433 and then RM_Size (Rec) < Elem_Component_Total_Esize)
4434 or else
4435 (not All_Elem_Components
4436 and then not All_Storage_Unit_Components))
4438 -- And the total RM size cannot be greater than the specified size
4439 -- since otherwise packing will not get us where we have to be.
4441 and then RM_Size (Rec) >= Sized_Component_Total_RM_Size
4443 -- Never do implicit packing in CodePeer or SPARK modes since
4444 -- we don't do any packing in these modes, since this generates
4445 -- over-complex code that confuses static analysis, and in
4446 -- general, neither CodePeer not GNATprove care about the
4447 -- internal representation of objects.
4449 and then not (CodePeer_Mode or GNATprove_Mode)
4450 then
4451 -- If implicit packing enabled, do it
4453 if Implicit_Packing then
4454 Set_Is_Packed (Rec);
4456 -- Otherwise flag the size clause
4458 else
4459 declare
4460 Sz : constant Node_Id := Size_Clause (Rec);
4461 begin
4462 Error_Msg_NE -- CODEFIX
4463 ("size given for& too small", Sz, Rec);
4464 Error_Msg_N -- CODEFIX
4465 ("\use explicit pragma Pack "
4466 & "or use pragma Implicit_Packing", Sz);
4467 end;
4468 end if;
4469 end if;
4471 -- The following checks are relevant only when SPARK_Mode is on as
4472 -- they are not standard Ada legality rules.
4474 if SPARK_Mode = On then
4475 if Is_Effectively_Volatile (Rec) then
4477 -- A discriminated type cannot be effectively volatile
4478 -- (SPARK RM C.6(4)).
4480 if Has_Discriminants (Rec) then
4481 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
4483 -- A tagged type cannot be effectively volatile
4484 -- (SPARK RM C.6(5)).
4486 elsif Is_Tagged_Type (Rec) then
4487 Error_Msg_N ("tagged type & cannot be volatile", Rec);
4488 end if;
4490 -- A non-effectively volatile record type cannot contain
4491 -- effectively volatile components (SPARK RM C.6(2)).
4493 else
4494 Comp := First_Component (Rec);
4495 while Present (Comp) loop
4496 if Comes_From_Source (Comp)
4497 and then Is_Effectively_Volatile (Etype (Comp))
4498 then
4499 Error_Msg_Name_1 := Chars (Rec);
4500 Error_Msg_N
4501 ("component & of non-volatile type % cannot be "
4502 & "volatile", Comp);
4503 end if;
4505 Next_Component (Comp);
4506 end loop;
4507 end if;
4509 -- A type which does not yield a synchronized object cannot have
4510 -- a component that yields a synchronized object (SPARK RM 9.5).
4512 if not Yields_Synchronized_Object (Rec) then
4513 Comp := First_Component (Rec);
4514 while Present (Comp) loop
4515 if Comes_From_Source (Comp)
4516 and then Yields_Synchronized_Object (Etype (Comp))
4517 then
4518 Error_Msg_Name_1 := Chars (Rec);
4519 Error_Msg_N
4520 ("component & of non-synchronized type % cannot be "
4521 & "synchronized", Comp);
4522 end if;
4524 Next_Component (Comp);
4525 end loop;
4526 end if;
4528 -- A Ghost type cannot have a component of protected or task type
4529 -- (SPARK RM 6.9(19)).
4531 if Is_Ghost_Entity (Rec) then
4532 Comp := First_Component (Rec);
4533 while Present (Comp) loop
4534 if Comes_From_Source (Comp)
4535 and then Is_Concurrent_Type (Etype (Comp))
4536 then
4537 Error_Msg_Name_1 := Chars (Rec);
4538 Error_Msg_N
4539 ("component & of ghost type % cannot be concurrent",
4540 Comp);
4541 end if;
4543 Next_Component (Comp);
4544 end loop;
4545 end if;
4546 end if;
4548 -- Make sure that if we have an iterator aspect, then we have
4549 -- either Constant_Indexing or Variable_Indexing.
4551 declare
4552 Iterator_Aspect : Node_Id;
4554 begin
4555 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
4557 if No (Iterator_Aspect) then
4558 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
4559 end if;
4561 if Present (Iterator_Aspect) then
4562 if Has_Aspect (Rec, Aspect_Constant_Indexing)
4563 or else
4564 Has_Aspect (Rec, Aspect_Variable_Indexing)
4565 then
4566 null;
4567 else
4568 Error_Msg_N
4569 ("Iterator_Element requires indexing aspect",
4570 Iterator_Aspect);
4571 end if;
4572 end if;
4573 end;
4575 -- All done if not a full record definition
4577 if Ekind (Rec) /= E_Record_Type then
4578 return;
4579 end if;
4581 -- Finally we need to check the variant part to make sure that
4582 -- all types within choices are properly frozen as part of the
4583 -- freezing of the record type.
4585 Check_Variant_Part : declare
4586 D : constant Node_Id := Declaration_Node (Rec);
4587 T : Node_Id;
4588 C : Node_Id;
4590 begin
4591 -- Find component list
4593 C := Empty;
4595 if Nkind (D) = N_Full_Type_Declaration then
4596 T := Type_Definition (D);
4598 if Nkind (T) = N_Record_Definition then
4599 C := Component_List (T);
4601 elsif Nkind (T) = N_Derived_Type_Definition
4602 and then Present (Record_Extension_Part (T))
4603 then
4604 C := Component_List (Record_Extension_Part (T));
4605 end if;
4606 end if;
4608 -- Case of variant part present
4610 if Present (C) and then Present (Variant_Part (C)) then
4611 Freeze_Choices_In_Variant_Part (Variant_Part (C));
4612 end if;
4614 -- Note: we used to call Check_Choices here, but it is too early,
4615 -- since predicated subtypes are frozen here, but their freezing
4616 -- actions are in Analyze_Freeze_Entity, which has not been called
4617 -- yet for entities frozen within this procedure, so we moved that
4618 -- call to the Analyze_Freeze_Entity for the record type.
4620 end Check_Variant_Part;
4622 -- Check that all the primitives of an interface type are abstract
4623 -- or null procedures.
4625 if Is_Interface (Rec)
4626 and then not Error_Posted (Parent (Rec))
4627 then
4628 declare
4629 Elmt : Elmt_Id;
4630 Subp : Entity_Id;
4632 begin
4633 Elmt := First_Elmt (Primitive_Operations (Rec));
4634 while Present (Elmt) loop
4635 Subp := Node (Elmt);
4637 if not Is_Abstract_Subprogram (Subp)
4639 -- Avoid reporting the error on inherited primitives
4641 and then Comes_From_Source (Subp)
4642 then
4643 Error_Msg_Name_1 := Chars (Subp);
4645 if Ekind (Subp) = E_Procedure then
4646 if not Null_Present (Parent (Subp)) then
4647 Error_Msg_N
4648 ("interface procedure % must be abstract or null",
4649 Parent (Subp));
4650 end if;
4651 else
4652 Error_Msg_N
4653 ("interface function % must be abstract",
4654 Parent (Subp));
4655 end if;
4656 end if;
4658 Next_Elmt (Elmt);
4659 end loop;
4660 end;
4661 end if;
4663 -- For a derived tagged type, check whether inherited primitives
4664 -- might require a wrapper to handle class-wide conditions.
4666 if Is_Tagged_Type (Rec) and then Is_Derived_Type (Rec) then
4667 Check_Inherited_Conditions (Rec);
4668 end if;
4669 end Freeze_Record_Type;
4671 -------------------------------
4672 -- Has_Boolean_Aspect_Import --
4673 -------------------------------
4675 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4676 Decl : constant Node_Id := Declaration_Node (E);
4677 Asp : Node_Id;
4678 Expr : Node_Id;
4680 begin
4681 if Has_Aspects (Decl) then
4682 Asp := First (Aspect_Specifications (Decl));
4683 while Present (Asp) loop
4684 Expr := Expression (Asp);
4686 -- The value of aspect Import is True when the expression is
4687 -- either missing or it is explicitly set to True.
4689 if Get_Aspect_Id (Asp) = Aspect_Import
4690 and then (No (Expr)
4691 or else (Compile_Time_Known_Value (Expr)
4692 and then Is_True (Expr_Value (Expr))))
4693 then
4694 return True;
4695 end if;
4697 Next (Asp);
4698 end loop;
4699 end if;
4701 return False;
4702 end Has_Boolean_Aspect_Import;
4704 ---------------------
4705 -- New_Freeze_Node --
4706 ---------------------
4708 function New_Freeze_Node return Node_Id is
4709 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4710 Result : Node_Id;
4712 begin
4713 -- Handle the case where an ignored Ghost subprogram freezes the type
4714 -- of one of its formals. The type can either be non-Ghost or checked
4715 -- Ghost. Since the freeze node for the type is generated in the
4716 -- context of the subprogram, the node will be incorrectly flagged as
4717 -- ignored Ghost and erroneously removed from the tree.
4719 -- type Typ is ...;
4720 -- procedure Ignored_Ghost_Proc (Formal : Typ) with Ghost;
4722 -- Reset the Ghost mode to "none". This preserves the freeze node.
4724 if Ghost_Mode = Ignore
4725 and then not Is_Ignored_Ghost_Entity (E)
4726 and then not Is_Ignored_Ghost_Node (E)
4727 then
4728 Ghost_Mode := None;
4729 end if;
4731 Result := New_Node (N_Freeze_Entity, Loc);
4733 Ghost_Mode := Save_Ghost_Mode;
4734 return Result;
4735 end New_Freeze_Node;
4737 ------------------------------
4738 -- Wrap_Imported_Subprogram --
4739 ------------------------------
4741 -- The issue here is that our normal approach of checking preconditions
4742 -- and postconditions does not work for imported procedures, since we
4743 -- are not generating code for the body. To get around this we create
4744 -- a wrapper, as shown by the following example:
4746 -- procedure K (A : Integer);
4747 -- pragma Import (C, K);
4749 -- The spec is rewritten by removing the effects of pragma Import, but
4750 -- leaving the convention unchanged, as though the source had said:
4752 -- procedure K (A : Integer);
4753 -- pragma Convention (C, K);
4755 -- and we create a body, added to the entity K freeze actions, which
4756 -- looks like:
4758 -- procedure K (A : Integer) is
4759 -- procedure K (A : Integer);
4760 -- pragma Import (C, K);
4761 -- begin
4762 -- K (A);
4763 -- end K;
4765 -- Now the contract applies in the normal way to the outer procedure,
4766 -- and the inner procedure has no contracts, so there is no problem
4767 -- in just calling it to get the original effect.
4769 -- In the case of a function, we create an appropriate return statement
4770 -- for the subprogram body that calls the inner procedure.
4772 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
4773 function Copy_Import_Pragma return Node_Id;
4774 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
4776 ------------------------
4777 -- Copy_Import_Pragma --
4778 ------------------------
4780 function Copy_Import_Pragma return Node_Id is
4782 -- The subprogram should have an import pragma, otherwise it does
4783 -- need a wrapper.
4785 Prag : constant Node_Id := Import_Pragma (E);
4786 pragma Assert (Present (Prag));
4788 -- Save all semantic fields of the pragma
4790 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
4791 Save_From : constant Boolean := From_Aspect_Specification (Prag);
4792 Save_Prag : constant Node_Id := Next_Pragma (Prag);
4793 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
4795 Result : Node_Id;
4797 begin
4798 -- Reset all semantic fields. This avoids a potential infinite
4799 -- loop when the pragma comes from an aspect as the duplication
4800 -- will copy the aspect, then copy the corresponding pragma and
4801 -- so on.
4803 Set_Corresponding_Aspect (Prag, Empty);
4804 Set_From_Aspect_Specification (Prag, False);
4805 Set_Next_Pragma (Prag, Empty);
4806 Set_Next_Rep_Item (Prag, Empty);
4808 Result := Copy_Separate_Tree (Prag);
4810 -- Restore the original semantic fields
4812 Set_Corresponding_Aspect (Prag, Save_Asp);
4813 Set_From_Aspect_Specification (Prag, Save_From);
4814 Set_Next_Pragma (Prag, Save_Prag);
4815 Set_Next_Rep_Item (Prag, Save_Rep);
4817 return Result;
4818 end Copy_Import_Pragma;
4820 -- Local variables
4822 Loc : constant Source_Ptr := Sloc (E);
4823 CE : constant Name_Id := Chars (E);
4824 Bod : Node_Id;
4825 Forml : Entity_Id;
4826 Parms : List_Id;
4827 Prag : Node_Id;
4828 Spec : Node_Id;
4829 Stmt : Node_Id;
4831 -- Start of processing for Wrap_Imported_Subprogram
4833 begin
4834 -- Nothing to do if not imported
4836 if not Is_Imported (E) then
4837 return;
4839 -- Test enabling conditions for wrapping
4841 elsif Is_Subprogram (E)
4842 and then Present (Contract (E))
4843 and then Present (Pre_Post_Conditions (Contract (E)))
4844 and then not GNATprove_Mode
4845 then
4846 -- Here we do the wrap
4848 -- Note on calls to Copy_Separate_Tree. The trees we are copying
4849 -- here are fully analyzed, but we definitely want fully syntactic
4850 -- unanalyzed trees in the body we construct, so that the analysis
4851 -- generates the right visibility, and that is exactly what the
4852 -- calls to Copy_Separate_Tree give us.
4854 Prag := Copy_Import_Pragma;
4856 -- Fix up spec to be not imported any more
4858 Set_Has_Completion (E, False);
4859 Set_Import_Pragma (E, Empty);
4860 Set_Interface_Name (E, Empty);
4861 Set_Is_Imported (E, False);
4863 -- Grab the subprogram declaration and specification
4865 Spec := Declaration_Node (E);
4867 -- Build parameter list that we need
4869 Parms := New_List;
4870 Forml := First_Formal (E);
4871 while Present (Forml) loop
4872 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
4873 Next_Formal (Forml);
4874 end loop;
4876 -- Build the call
4878 if Ekind_In (E, E_Function, E_Generic_Function) then
4879 Stmt :=
4880 Make_Simple_Return_Statement (Loc,
4881 Expression =>
4882 Make_Function_Call (Loc,
4883 Name => Make_Identifier (Loc, CE),
4884 Parameter_Associations => Parms));
4886 else
4887 Stmt :=
4888 Make_Procedure_Call_Statement (Loc,
4889 Name => Make_Identifier (Loc, CE),
4890 Parameter_Associations => Parms);
4891 end if;
4893 -- Now build the body
4895 Bod :=
4896 Make_Subprogram_Body (Loc,
4897 Specification =>
4898 Copy_Separate_Tree (Spec),
4899 Declarations => New_List (
4900 Make_Subprogram_Declaration (Loc,
4901 Specification => Copy_Separate_Tree (Spec)),
4902 Prag),
4903 Handled_Statement_Sequence =>
4904 Make_Handled_Sequence_Of_Statements (Loc,
4905 Statements => New_List (Stmt),
4906 End_Label => Make_Identifier (Loc, CE)));
4908 -- Append the body to freeze result
4910 Add_To_Result (Bod);
4911 return;
4913 -- Case of imported subprogram that does not get wrapped
4915 else
4916 -- Set Is_Public. All imported entities need an external symbol
4917 -- created for them since they are always referenced from another
4918 -- object file. Note this used to be set when we set Is_Imported
4919 -- back in Sem_Prag, but now we delay it to this point, since we
4920 -- don't want to set this flag if we wrap an imported subprogram.
4922 Set_Is_Public (E);
4923 end if;
4924 end Wrap_Imported_Subprogram;
4926 -- Local variables
4928 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4930 -- Start of processing for Freeze_Entity
4932 begin
4933 -- The entity being frozen may be subject to pragma Ghost. Set the mode
4934 -- now to ensure that any nodes generated during freezing are properly
4935 -- flagged as Ghost.
4937 Set_Ghost_Mode_From_Entity (E);
4939 -- We are going to test for various reasons why this entity need not be
4940 -- frozen here, but in the case of an Itype that's defined within a
4941 -- record, that test actually applies to the record.
4943 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
4944 Test_E := Scope (E);
4945 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
4946 and then Is_Record_Type (Underlying_Type (Scope (E)))
4947 then
4948 Test_E := Underlying_Type (Scope (E));
4949 end if;
4951 -- Do not freeze if already frozen since we only need one freeze node
4953 if Is_Frozen (E) then
4954 Ghost_Mode := Save_Ghost_Mode;
4955 return No_List;
4957 elsif Ekind (E) = E_Generic_Package then
4958 Result := Freeze_Generic_Entities (E);
4960 Ghost_Mode := Save_Ghost_Mode;
4961 return Result;
4963 -- It is improper to freeze an external entity within a generic because
4964 -- its freeze node will appear in a non-valid context. The entity will
4965 -- be frozen in the proper scope after the current generic is analyzed.
4966 -- However, aspects must be analyzed because they may be queried later
4967 -- within the generic itself, and the corresponding pragma or attribute
4968 -- definition has not been analyzed yet.
4970 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
4971 if Has_Delayed_Aspects (E) then
4972 Analyze_Aspects_At_Freeze_Point (E);
4973 end if;
4975 Ghost_Mode := Save_Ghost_Mode;
4976 return No_List;
4978 -- AI05-0213: A formal incomplete type does not freeze the actual. In
4979 -- the instance, the same applies to the subtype renaming the actual.
4981 elsif Is_Private_Type (E)
4982 and then Is_Generic_Actual_Type (E)
4983 and then No (Full_View (Base_Type (E)))
4984 and then Ada_Version >= Ada_2012
4985 then
4986 Ghost_Mode := Save_Ghost_Mode;
4987 return No_List;
4989 -- Formal subprograms are never frozen
4991 elsif Is_Formal_Subprogram (E) then
4992 Ghost_Mode := Save_Ghost_Mode;
4993 return No_List;
4995 -- Generic types are never frozen as they lack delayed semantic checks
4997 elsif Is_Generic_Type (E) then
4998 Ghost_Mode := Save_Ghost_Mode;
4999 return No_List;
5001 -- Do not freeze a global entity within an inner scope created during
5002 -- expansion. A call to subprogram E within some internal procedure
5003 -- (a stream attribute for example) might require freezing E, but the
5004 -- freeze node must appear in the same declarative part as E itself.
5005 -- The two-pass elaboration mechanism in gigi guarantees that E will
5006 -- be frozen before the inner call is elaborated. We exclude constants
5007 -- from this test, because deferred constants may be frozen early, and
5008 -- must be diagnosed (e.g. in the case of a deferred constant being used
5009 -- in a default expression). If the enclosing subprogram comes from
5010 -- source, or is a generic instance, then the freeze point is the one
5011 -- mandated by the language, and we freeze the entity. A subprogram that
5012 -- is a child unit body that acts as a spec does not have a spec that
5013 -- comes from source, but can only come from source.
5015 elsif In_Open_Scopes (Scope (Test_E))
5016 and then Scope (Test_E) /= Current_Scope
5017 and then Ekind (Test_E) /= E_Constant
5018 then
5019 declare
5020 S : Entity_Id;
5022 begin
5023 S := Current_Scope;
5024 while Present (S) loop
5025 if Is_Overloadable (S) then
5026 if Comes_From_Source (S)
5027 or else Is_Generic_Instance (S)
5028 or else Is_Child_Unit (S)
5029 then
5030 exit;
5031 else
5032 Ghost_Mode := Save_Ghost_Mode;
5033 return No_List;
5034 end if;
5035 end if;
5037 S := Scope (S);
5038 end loop;
5039 end;
5041 -- Similarly, an inlined instance body may make reference to global
5042 -- entities, but these references cannot be the proper freezing point
5043 -- for them, and in the absence of inlining freezing will take place in
5044 -- their own scope. Normally instance bodies are analyzed after the
5045 -- enclosing compilation, and everything has been frozen at the proper
5046 -- place, but with front-end inlining an instance body is compiled
5047 -- before the end of the enclosing scope, and as a result out-of-order
5048 -- freezing must be prevented.
5050 elsif Front_End_Inlining
5051 and then In_Instance_Body
5052 and then Present (Scope (Test_E))
5053 then
5054 declare
5055 S : Entity_Id;
5057 begin
5058 S := Scope (Test_E);
5059 while Present (S) loop
5060 if Is_Generic_Instance (S) then
5061 exit;
5062 else
5063 S := Scope (S);
5064 end if;
5065 end loop;
5067 if No (S) then
5068 Ghost_Mode := Save_Ghost_Mode;
5069 return No_List;
5070 end if;
5071 end;
5072 end if;
5074 -- Add checks to detect proper initialization of scalars that may appear
5075 -- as subprogram parameters.
5077 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
5078 Apply_Parameter_Validity_Checks (E);
5079 end if;
5081 -- Deal with delayed aspect specifications. The analysis of the aspect
5082 -- is required to be delayed to the freeze point, thus we analyze the
5083 -- pragma or attribute definition clause in the tree at this point. We
5084 -- also analyze the aspect specification node at the freeze point when
5085 -- the aspect doesn't correspond to pragma/attribute definition clause.
5087 if Has_Delayed_Aspects (E) then
5088 Analyze_Aspects_At_Freeze_Point (E);
5089 end if;
5091 -- Here to freeze the entity
5093 Set_Is_Frozen (E);
5095 -- Case of entity being frozen is other than a type
5097 if not Is_Type (E) then
5099 -- If entity is exported or imported and does not have an external
5100 -- name, now is the time to provide the appropriate default name.
5101 -- Skip this if the entity is stubbed, since we don't need a name
5102 -- for any stubbed routine. For the case on intrinsics, if no
5103 -- external name is specified, then calls will be handled in
5104 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
5105 -- external name is provided, then Expand_Intrinsic_Call leaves
5106 -- calls in place for expansion by GIGI.
5108 if (Is_Imported (E) or else Is_Exported (E))
5109 and then No (Interface_Name (E))
5110 and then Convention (E) /= Convention_Stubbed
5111 and then Convention (E) /= Convention_Intrinsic
5112 then
5113 Set_Encoded_Interface_Name
5114 (E, Get_Default_External_Name (E));
5116 -- If entity is an atomic object appearing in a declaration and
5117 -- the expression is an aggregate, assign it to a temporary to
5118 -- ensure that the actual assignment is done atomically rather
5119 -- than component-wise (the assignment to the temp may be done
5120 -- component-wise, but that is harmless).
5122 elsif Is_Atomic_Or_VFA (E)
5123 and then Nkind (Parent (E)) = N_Object_Declaration
5124 and then Present (Expression (Parent (E)))
5125 and then Nkind (Expression (Parent (E))) = N_Aggregate
5126 and then Is_Atomic_VFA_Aggregate (Expression (Parent (E)))
5127 then
5128 null;
5129 end if;
5131 -- Subprogram case
5133 if Is_Subprogram (E) then
5135 -- Check for needing to wrap imported subprogram
5137 Wrap_Imported_Subprogram (E);
5139 -- Freeze all parameter types and the return type (RM 13.14(14)).
5140 -- However skip this for internal subprograms. This is also where
5141 -- any extra formal parameters are created since we now know
5142 -- whether the subprogram will use a foreign convention.
5144 -- In Ada 2012, freezing a subprogram does not always freeze the
5145 -- corresponding profile (see AI05-019). An attribute reference
5146 -- is not a freezing point of the profile. Flag Do_Freeze_Profile
5147 -- indicates whether the profile should be frozen now.
5148 -- Other constructs that should not freeze ???
5150 -- This processing doesn't apply to internal entities (see below)
5152 if not Is_Internal (E) and then Do_Freeze_Profile then
5153 if not Freeze_Profile (E) then
5154 Ghost_Mode := Save_Ghost_Mode;
5155 return Result;
5156 end if;
5157 end if;
5159 -- Must freeze its parent first if it is a derived subprogram
5161 if Present (Alias (E)) then
5162 Freeze_And_Append (Alias (E), N, Result);
5163 end if;
5165 -- We don't freeze internal subprograms, because we don't normally
5166 -- want addition of extra formals or mechanism setting to happen
5167 -- for those. However we do pass through predefined dispatching
5168 -- cases, since extra formals may be needed in some cases, such as
5169 -- for the stream 'Input function (build-in-place formals).
5171 if not Is_Internal (E)
5172 or else Is_Predefined_Dispatching_Operation (E)
5173 then
5174 Freeze_Subprogram (E);
5175 end if;
5177 -- If warning on suspicious contracts then check for the case of
5178 -- a postcondition other than False for a No_Return subprogram.
5180 if No_Return (E)
5181 and then Warn_On_Suspicious_Contract
5182 and then Present (Contract (E))
5183 then
5184 declare
5185 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
5186 Exp : Node_Id;
5188 begin
5189 while Present (Prag) loop
5190 if Nam_In (Pragma_Name (Prag), Name_Post,
5191 Name_Postcondition,
5192 Name_Refined_Post)
5193 then
5194 Exp :=
5195 Expression
5196 (First (Pragma_Argument_Associations (Prag)));
5198 if Nkind (Exp) /= N_Identifier
5199 or else Chars (Exp) /= Name_False
5200 then
5201 Error_Msg_NE
5202 ("useless postcondition, & is marked "
5203 & "No_Return?T?", Exp, E);
5204 end if;
5205 end if;
5207 Prag := Next_Pragma (Prag);
5208 end loop;
5209 end;
5210 end if;
5212 -- Here for other than a subprogram or type
5214 else
5215 -- If entity has a type, and it is not a generic unit, then
5216 -- freeze it first (RM 13.14(10)).
5218 if Present (Etype (E))
5219 and then Ekind (E) /= E_Generic_Function
5220 then
5221 Freeze_And_Append (Etype (E), N, Result);
5223 -- For an object of an anonymous array type, aspects on the
5224 -- object declaration apply to the type itself. This is the
5225 -- case for Atomic_Components, Volatile_Components, and
5226 -- Independent_Components. In these cases analysis of the
5227 -- generated pragma will mark the anonymous types accordingly,
5228 -- and the object itself does not require a freeze node.
5230 if Ekind (E) = E_Variable
5231 and then Is_Itype (Etype (E))
5232 and then Is_Array_Type (Etype (E))
5233 and then Has_Delayed_Aspects (E)
5234 then
5235 Set_Has_Delayed_Aspects (E, False);
5236 Set_Has_Delayed_Freeze (E, False);
5237 Set_Freeze_Node (E, Empty);
5238 end if;
5239 end if;
5241 -- Special processing for objects created by object declaration
5243 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
5244 Freeze_Object_Declaration (E);
5245 end if;
5247 -- Check that a constant which has a pragma Volatile[_Components]
5248 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
5250 -- Note: Atomic[_Components] also sets Volatile[_Components]
5252 if Ekind (E) = E_Constant
5253 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
5254 and then not Is_Imported (E)
5255 and then not Has_Boolean_Aspect_Import (E)
5256 then
5257 -- Make sure we actually have a pragma, and have not merely
5258 -- inherited the indication from elsewhere (e.g. an address
5259 -- clause, which is not good enough in RM terms).
5261 if Has_Rep_Pragma (E, Name_Atomic)
5262 or else
5263 Has_Rep_Pragma (E, Name_Atomic_Components)
5264 then
5265 Error_Msg_N
5266 ("stand alone atomic constant must be " &
5267 "imported (RM C.6(13))", E);
5269 elsif Has_Rep_Pragma (E, Name_Volatile)
5270 or else
5271 Has_Rep_Pragma (E, Name_Volatile_Components)
5272 then
5273 Error_Msg_N
5274 ("stand alone volatile constant must be " &
5275 "imported (RM C.6(13))", E);
5276 end if;
5277 end if;
5279 -- Static objects require special handling
5281 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
5282 and then Is_Statically_Allocated (E)
5283 then
5284 Freeze_Static_Object (E);
5285 end if;
5287 -- Remaining step is to layout objects
5289 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
5290 or else Is_Formal (E)
5291 then
5292 Layout_Object (E);
5293 end if;
5295 -- For an object that does not have delayed freezing, and whose
5296 -- initialization actions have been captured in a compound
5297 -- statement, move them back now directly within the enclosing
5298 -- statement sequence.
5300 if Ekind_In (E, E_Constant, E_Variable)
5301 and then not Has_Delayed_Freeze (E)
5302 then
5303 Explode_Initialization_Compound_Statement (E);
5304 end if;
5305 end if;
5307 -- Case of a type or subtype being frozen
5309 else
5310 -- We used to check here that a full type must have preelaborable
5311 -- initialization if it completes a private type specified with
5312 -- pragma Preelaborable_Initialization, but that missed cases where
5313 -- the types occur within a generic package, since the freezing
5314 -- that occurs within a containing scope generally skips traversal
5315 -- of a generic unit's declarations (those will be frozen within
5316 -- instances). This check was moved to Analyze_Package_Specification.
5318 -- The type may be defined in a generic unit. This can occur when
5319 -- freezing a generic function that returns the type (which is
5320 -- defined in a parent unit). It is clearly meaningless to freeze
5321 -- this type. However, if it is a subtype, its size may be determi-
5322 -- nable and used in subsequent checks, so might as well try to
5323 -- compute it.
5325 -- In Ada 2012, Freeze_Entities is also used in the front end to
5326 -- trigger the analysis of aspect expressions, so in this case we
5327 -- want to continue the freezing process.
5329 if Present (Scope (E))
5330 and then Is_Generic_Unit (Scope (E))
5331 and then
5332 (not Has_Predicates (E)
5333 and then not Has_Delayed_Freeze (E))
5334 then
5335 Check_Compile_Time_Size (E);
5336 Ghost_Mode := Save_Ghost_Mode;
5337 return No_List;
5338 end if;
5340 -- Check for error of Type_Invariant'Class applied to an untagged
5341 -- type (check delayed to freeze time when full type is available).
5343 declare
5344 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
5345 begin
5346 if Present (Prag)
5347 and then Class_Present (Prag)
5348 and then not Is_Tagged_Type (E)
5349 then
5350 Error_Msg_NE
5351 ("Type_Invariant''Class cannot be specified for &", Prag, E);
5352 Error_Msg_N
5353 ("\can only be specified for a tagged type", Prag);
5354 end if;
5355 end;
5357 if Is_Ghost_Entity (E) then
5359 -- A Ghost type cannot be concurrent (SPARK RM 6.9(19)). Verify
5360 -- this legality rule first to five a finer-grained diagnostic.
5362 if Is_Concurrent_Type (E) then
5363 Error_Msg_N ("ghost type & cannot be concurrent", E);
5365 -- A Ghost type cannot be effectively volatile (SPARK RM 6.9(7))
5367 elsif Is_Effectively_Volatile (E) then
5368 Error_Msg_N ("ghost type & cannot be volatile", E);
5369 end if;
5370 end if;
5372 -- Deal with special cases of freezing for subtype
5374 if E /= Base_Type (E) then
5376 -- Before we do anything else, a specific test for the case of a
5377 -- size given for an array where the array would need to be packed
5378 -- in order for the size to be honored, but is not. This is the
5379 -- case where implicit packing may apply. The reason we do this so
5380 -- early is that, if we have implicit packing, the layout of the
5381 -- base type is affected, so we must do this before we freeze the
5382 -- base type.
5384 -- We could do this processing only if implicit packing is enabled
5385 -- since in all other cases, the error would be caught by the back
5386 -- end. However, we choose to do the check even if we do not have
5387 -- implicit packing enabled, since this allows us to give a more
5388 -- useful error message (advising use of pragma Implicit_Packing
5389 -- or pragma Pack).
5391 if Is_Array_Type (E) then
5392 declare
5393 Ctyp : constant Entity_Id := Component_Type (E);
5394 Rsiz : constant Uint := RM_Size (Ctyp);
5395 SZ : constant Node_Id := Size_Clause (E);
5396 Btyp : constant Entity_Id := Base_Type (E);
5398 Lo : Node_Id;
5399 Hi : Node_Id;
5400 Indx : Node_Id;
5402 Dim : Uint;
5403 Num_Elmts : Uint := Uint_1;
5404 -- Number of elements in array
5406 begin
5407 -- Check enabling conditions. These are straightforward
5408 -- except for the test for a limited composite type. This
5409 -- eliminates the rare case of a array of limited components
5410 -- where there are issues of whether or not we can go ahead
5411 -- and pack the array (since we can't freely pack and unpack
5412 -- arrays if they are limited).
5414 -- Note that we check the root type explicitly because the
5415 -- whole point is we are doing this test before we have had
5416 -- a chance to freeze the base type (and it is that freeze
5417 -- action that causes stuff to be inherited).
5419 -- The conditions on the size are identical to those used in
5420 -- Freeze_Array_Type to set the Is_Packed flag.
5422 if Has_Size_Clause (E)
5423 and then Known_Static_RM_Size (E)
5424 and then not Is_Packed (E)
5425 and then not Has_Pragma_Pack (E)
5426 and then not Has_Component_Size_Clause (E)
5427 and then Known_Static_RM_Size (Ctyp)
5428 and then Rsiz <= 64
5429 and then not (Addressable (Rsiz)
5430 and then Known_Static_Esize (Ctyp)
5431 and then Esize (Ctyp) = Rsiz)
5432 and then not (Rsiz mod System_Storage_Unit = 0
5433 and then Is_Composite_Type (Ctyp))
5434 and then not Is_Limited_Composite (E)
5435 and then not Is_Packed (Root_Type (E))
5436 and then not Has_Component_Size_Clause (Root_Type (E))
5437 and then not (CodePeer_Mode or GNATprove_Mode)
5438 then
5439 -- Compute number of elements in array
5441 Indx := First_Index (E);
5442 while Present (Indx) loop
5443 Get_Index_Bounds (Indx, Lo, Hi);
5445 if not (Compile_Time_Known_Value (Lo)
5446 and then
5447 Compile_Time_Known_Value (Hi))
5448 then
5449 goto No_Implicit_Packing;
5450 end if;
5452 Dim := Expr_Value (Hi) - Expr_Value (Lo) + 1;
5454 if Dim >= 0 then
5455 Num_Elmts := Num_Elmts * Dim;
5456 else
5457 Num_Elmts := Uint_0;
5458 end if;
5460 Next_Index (Indx);
5461 end loop;
5463 -- What we are looking for here is the situation where
5464 -- the RM_Size given would be exactly right if there was
5465 -- a pragma Pack, resulting in the component size being
5466 -- the RM_Size of the component type.
5468 if RM_Size (E) = Num_Elmts * Rsiz then
5470 -- For implicit packing mode, just set the component
5471 -- size and Freeze_Array_Type will do the rest.
5473 if Implicit_Packing then
5474 Set_Component_Size (Btyp, Rsiz);
5476 -- Otherwise give an error message
5478 else
5479 Error_Msg_NE
5480 ("size given for& too small", SZ, E);
5481 Error_Msg_N -- CODEFIX
5482 ("\use explicit pragma Pack or use pragma "
5483 & "Implicit_Packing", SZ);
5484 end if;
5485 end if;
5486 end if;
5487 end;
5488 end if;
5490 <<No_Implicit_Packing>>
5492 -- If ancestor subtype present, freeze that first. Note that this
5493 -- will also get the base type frozen. Need RM reference ???
5495 Atype := Ancestor_Subtype (E);
5497 if Present (Atype) then
5498 Freeze_And_Append (Atype, N, Result);
5500 -- No ancestor subtype present
5502 else
5503 -- See if we have a nearest ancestor that has a predicate.
5504 -- That catches the case of derived type with a predicate.
5505 -- Need RM reference here ???
5507 Atype := Nearest_Ancestor (E);
5509 if Present (Atype) and then Has_Predicates (Atype) then
5510 Freeze_And_Append (Atype, N, Result);
5511 end if;
5513 -- Freeze base type before freezing the entity (RM 13.14(15))
5515 if E /= Base_Type (E) then
5516 Freeze_And_Append (Base_Type (E), N, Result);
5517 end if;
5518 end if;
5520 -- A subtype inherits all the type-related representation aspects
5521 -- from its parents (RM 13.1(8)).
5523 Inherit_Aspects_At_Freeze_Point (E);
5525 -- For a derived type, freeze its parent type first (RM 13.14(15))
5527 elsif Is_Derived_Type (E) then
5528 Freeze_And_Append (Etype (E), N, Result);
5529 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5531 -- A derived type inherits each type-related representation aspect
5532 -- of its parent type that was directly specified before the
5533 -- declaration of the derived type (RM 13.1(15)).
5535 Inherit_Aspects_At_Freeze_Point (E);
5536 end if;
5538 -- Check for incompatible size and alignment for record type
5540 if Warn_On_Size_Alignment
5541 and then Is_Record_Type (E)
5542 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5544 -- If explicit Object_Size clause given assume that the programmer
5545 -- knows what he is doing, and expects the compiler behavior.
5547 and then not Has_Object_Size_Clause (E)
5549 -- Check for size not a multiple of alignment
5551 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5552 then
5553 declare
5554 SC : constant Node_Id := Size_Clause (E);
5555 AC : constant Node_Id := Alignment_Clause (E);
5556 Loc : Node_Id;
5557 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5559 begin
5560 if Present (SC) and then Present (AC) then
5562 -- Give a warning
5564 if Sloc (SC) > Sloc (AC) then
5565 Loc := SC;
5566 Error_Msg_NE
5567 ("?Z?size is not a multiple of alignment for &",
5568 Loc, E);
5569 Error_Msg_Sloc := Sloc (AC);
5570 Error_Msg_Uint_1 := Alignment (E);
5571 Error_Msg_N ("\?Z?alignment of ^ specified #", Loc);
5573 else
5574 Loc := AC;
5575 Error_Msg_NE
5576 ("?Z?size is not a multiple of alignment for &",
5577 Loc, E);
5578 Error_Msg_Sloc := Sloc (SC);
5579 Error_Msg_Uint_1 := RM_Size (E);
5580 Error_Msg_N ("\?Z?size of ^ specified #", Loc);
5581 end if;
5583 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5584 Error_Msg_N ("\?Z?Object_Size will be increased to ^", Loc);
5585 end if;
5586 end;
5587 end if;
5589 -- Array type
5591 if Is_Array_Type (E) then
5592 Freeze_Array_Type (E);
5594 -- For a class-wide type, the corresponding specific type is
5595 -- frozen as well (RM 13.14(15))
5597 elsif Is_Class_Wide_Type (E) then
5598 Freeze_And_Append (Root_Type (E), N, Result);
5600 -- If the base type of the class-wide type is still incomplete,
5601 -- the class-wide remains unfrozen as well. This is legal when
5602 -- E is the formal of a primitive operation of some other type
5603 -- which is being frozen.
5605 if not Is_Frozen (Root_Type (E)) then
5606 Set_Is_Frozen (E, False);
5607 Ghost_Mode := Save_Ghost_Mode;
5608 return Result;
5609 end if;
5611 -- The equivalent type associated with a class-wide subtype needs
5612 -- to be frozen to ensure that its layout is done.
5614 if Ekind (E) = E_Class_Wide_Subtype
5615 and then Present (Equivalent_Type (E))
5616 then
5617 Freeze_And_Append (Equivalent_Type (E), N, Result);
5618 end if;
5620 -- Generate an itype reference for a library-level class-wide type
5621 -- at the freeze point. Otherwise the first explicit reference to
5622 -- the type may appear in an inner scope which will be rejected by
5623 -- the back-end.
5625 if Is_Itype (E)
5626 and then Is_Compilation_Unit (Scope (E))
5627 then
5628 declare
5629 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5631 begin
5632 Set_Itype (Ref, E);
5634 -- From a gigi point of view, a class-wide subtype derives
5635 -- from its record equivalent type. As a result, the itype
5636 -- reference must appear after the freeze node of the
5637 -- equivalent type or gigi will reject the reference.
5639 if Ekind (E) = E_Class_Wide_Subtype
5640 and then Present (Equivalent_Type (E))
5641 then
5642 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5643 else
5644 Add_To_Result (Ref);
5645 end if;
5646 end;
5647 end if;
5649 -- For a record type or record subtype, freeze all component types
5650 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5651 -- using Is_Record_Type, because we don't want to attempt the freeze
5652 -- for the case of a private type with record extension (we will do
5653 -- that later when the full type is frozen).
5655 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype)
5656 and then not (Present (Scope (E))
5657 and then Is_Generic_Unit (Scope (E)))
5658 then
5659 Freeze_Record_Type (E);
5661 -- For a concurrent type, freeze corresponding record type. This does
5662 -- not correspond to any specific rule in the RM, but the record type
5663 -- is essentially part of the concurrent type. Also freeze all local
5664 -- entities. This includes record types created for entry parameter
5665 -- blocks and whatever local entities may appear in the private part.
5667 elsif Is_Concurrent_Type (E) then
5668 if Present (Corresponding_Record_Type (E)) then
5669 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5670 end if;
5672 Comp := First_Entity (E);
5673 while Present (Comp) loop
5674 if Is_Type (Comp) then
5675 Freeze_And_Append (Comp, N, Result);
5677 elsif (Ekind (Comp)) /= E_Function then
5679 -- The guard on the presence of the Etype seems to be needed
5680 -- for some CodePeer (-gnatcC) cases, but not clear why???
5682 if Present (Etype (Comp)) then
5683 if Is_Itype (Etype (Comp))
5684 and then Underlying_Type (Scope (Etype (Comp))) = E
5685 then
5686 Undelay_Type (Etype (Comp));
5687 end if;
5689 Freeze_And_Append (Etype (Comp), N, Result);
5690 end if;
5691 end if;
5693 Next_Entity (Comp);
5694 end loop;
5696 -- Private types are required to point to the same freeze node as
5697 -- their corresponding full views. The freeze node itself has to
5698 -- point to the partial view of the entity (because from the partial
5699 -- view, we can retrieve the full view, but not the reverse).
5700 -- However, in order to freeze correctly, we need to freeze the full
5701 -- view. If we are freezing at the end of a scope (or within the
5702 -- scope) of the private type, the partial and full views will have
5703 -- been swapped, the full view appears first in the entity chain and
5704 -- the swapping mechanism ensures that the pointers are properly set
5705 -- (on scope exit).
5707 -- If we encounter the partial view before the full view (e.g. when
5708 -- freezing from another scope), we freeze the full view, and then
5709 -- set the pointers appropriately since we cannot rely on swapping to
5710 -- fix things up (subtypes in an outer scope might not get swapped).
5712 -- If the full view is itself private, the above requirements apply
5713 -- to the underlying full view instead of the full view. But there is
5714 -- no swapping mechanism for the underlying full view so we need to
5715 -- set the pointers appropriately in both cases.
5717 elsif Is_Incomplete_Or_Private_Type (E)
5718 and then not Is_Generic_Type (E)
5719 then
5720 -- The construction of the dispatch table associated with library
5721 -- level tagged types forces freezing of all the primitives of the
5722 -- type, which may cause premature freezing of the partial view.
5723 -- For example:
5725 -- package Pkg is
5726 -- type T is tagged private;
5727 -- type DT is new T with private;
5728 -- procedure Prim (X : in out T; Y : in out DT'Class);
5729 -- private
5730 -- type T is tagged null record;
5731 -- Obj : T;
5732 -- type DT is new T with null record;
5733 -- end;
5735 -- In this case the type will be frozen later by the usual
5736 -- mechanism: an object declaration, an instantiation, or the
5737 -- end of a declarative part.
5739 if Is_Library_Level_Tagged_Type (E)
5740 and then not Present (Full_View (E))
5741 then
5742 Set_Is_Frozen (E, False);
5743 Ghost_Mode := Save_Ghost_Mode;
5744 return Result;
5746 -- Case of full view present
5748 elsif Present (Full_View (E)) then
5750 -- If full view has already been frozen, then no further
5751 -- processing is required
5753 if Is_Frozen (Full_View (E)) then
5754 Set_Has_Delayed_Freeze (E, False);
5755 Set_Freeze_Node (E, Empty);
5757 -- Otherwise freeze full view and patch the pointers so that
5758 -- the freeze node will elaborate both views in the back end.
5759 -- However, if full view is itself private, freeze underlying
5760 -- full view instead and patch the pointers so that the freeze
5761 -- node will elaborate the three views in the back end.
5763 else
5764 declare
5765 Full : Entity_Id := Full_View (E);
5767 begin
5768 if Is_Private_Type (Full)
5769 and then Present (Underlying_Full_View (Full))
5770 then
5771 Full := Underlying_Full_View (Full);
5772 end if;
5774 Freeze_And_Append (Full, N, Result);
5776 if Full /= Full_View (E)
5777 and then Has_Delayed_Freeze (Full_View (E))
5778 then
5779 F_Node := Freeze_Node (Full);
5781 if Present (F_Node) then
5782 Set_Freeze_Node (Full_View (E), F_Node);
5783 Set_Entity (F_Node, Full_View (E));
5785 else
5786 Set_Has_Delayed_Freeze (Full_View (E), False);
5787 Set_Freeze_Node (Full_View (E), Empty);
5788 end if;
5789 end if;
5791 if Has_Delayed_Freeze (E) then
5792 F_Node := Freeze_Node (Full_View (E));
5794 if Present (F_Node) then
5795 Set_Freeze_Node (E, F_Node);
5796 Set_Entity (F_Node, E);
5798 else
5799 -- {Incomplete,Private}_Subtypes with Full_Views
5800 -- constrained by discriminants.
5802 Set_Has_Delayed_Freeze (E, False);
5803 Set_Freeze_Node (E, Empty);
5804 end if;
5805 end if;
5806 end;
5807 end if;
5809 Check_Debug_Info_Needed (E);
5811 -- AI-117 requires that the convention of a partial view be the
5812 -- same as the convention of the full view. Note that this is a
5813 -- recognized breach of privacy, but it's essential for logical
5814 -- consistency of representation, and the lack of a rule in
5815 -- RM95 was an oversight.
5817 Set_Convention (E, Convention (Full_View (E)));
5819 Set_Size_Known_At_Compile_Time (E,
5820 Size_Known_At_Compile_Time (Full_View (E)));
5822 -- Size information is copied from the full view to the
5823 -- incomplete or private view for consistency.
5825 -- We skip this is the full view is not a type. This is very
5826 -- strange of course, and can only happen as a result of
5827 -- certain illegalities, such as a premature attempt to derive
5828 -- from an incomplete type.
5830 if Is_Type (Full_View (E)) then
5831 Set_Size_Info (E, Full_View (E));
5832 Set_RM_Size (E, RM_Size (Full_View (E)));
5833 end if;
5835 Ghost_Mode := Save_Ghost_Mode;
5836 return Result;
5838 -- Case of underlying full view present
5840 elsif Is_Private_Type (E)
5841 and then Present (Underlying_Full_View (E))
5842 then
5843 if not Is_Frozen (Underlying_Full_View (E)) then
5844 Freeze_And_Append (Underlying_Full_View (E), N, Result);
5845 end if;
5847 -- Patch the pointers so that the freeze node will elaborate
5848 -- both views in the back end.
5850 if Has_Delayed_Freeze (E) then
5851 F_Node := Freeze_Node (Underlying_Full_View (E));
5853 if Present (F_Node) then
5854 Set_Freeze_Node (E, F_Node);
5855 Set_Entity (F_Node, E);
5857 else
5858 Set_Has_Delayed_Freeze (E, False);
5859 Set_Freeze_Node (E, Empty);
5860 end if;
5861 end if;
5863 Check_Debug_Info_Needed (E);
5865 Ghost_Mode := Save_Ghost_Mode;
5866 return Result;
5868 -- Case of no full view present. If entity is derived or subtype,
5869 -- it is safe to freeze, correctness depends on the frozen status
5870 -- of parent. Otherwise it is either premature usage, or a Taft
5871 -- amendment type, so diagnosis is at the point of use and the
5872 -- type might be frozen later.
5874 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
5875 null;
5877 else
5878 Set_Is_Frozen (E, False);
5879 Ghost_Mode := Save_Ghost_Mode;
5880 return No_List;
5881 end if;
5883 -- For access subprogram, freeze types of all formals, the return
5884 -- type was already frozen, since it is the Etype of the function.
5885 -- Formal types can be tagged Taft amendment types, but otherwise
5886 -- they cannot be incomplete.
5888 elsif Ekind (E) = E_Subprogram_Type then
5889 Formal := First_Formal (E);
5890 while Present (Formal) loop
5891 if Ekind (Etype (Formal)) = E_Incomplete_Type
5892 and then No (Full_View (Etype (Formal)))
5893 then
5894 if Is_Tagged_Type (Etype (Formal)) then
5895 null;
5897 -- AI05-151: Incomplete types are allowed in access to
5898 -- subprogram specifications.
5900 elsif Ada_Version < Ada_2012 then
5901 Error_Msg_NE
5902 ("invalid use of incomplete type&", E, Etype (Formal));
5903 end if;
5904 end if;
5906 Freeze_And_Append (Etype (Formal), N, Result);
5907 Next_Formal (Formal);
5908 end loop;
5910 Freeze_Subprogram (E);
5912 -- For access to a protected subprogram, freeze the equivalent type
5913 -- (however this is not set if we are not generating code or if this
5914 -- is an anonymous type used just for resolution).
5916 elsif Is_Access_Protected_Subprogram_Type (E) then
5917 if Present (Equivalent_Type (E)) then
5918 Freeze_And_Append (Equivalent_Type (E), N, Result);
5919 end if;
5920 end if;
5922 -- Generic types are never seen by the back-end, and are also not
5923 -- processed by the expander (since the expander is turned off for
5924 -- generic processing), so we never need freeze nodes for them.
5926 if Is_Generic_Type (E) then
5927 Ghost_Mode := Save_Ghost_Mode;
5928 return Result;
5929 end if;
5931 -- Some special processing for non-generic types to complete
5932 -- representation details not known till the freeze point.
5934 if Is_Fixed_Point_Type (E) then
5935 Freeze_Fixed_Point_Type (E);
5937 -- Some error checks required for ordinary fixed-point type. Defer
5938 -- these till the freeze-point since we need the small and range
5939 -- values. We only do these checks for base types
5941 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
5942 if Small_Value (E) < Ureal_2_M_80 then
5943 Error_Msg_Name_1 := Name_Small;
5944 Error_Msg_N
5945 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
5947 elsif Small_Value (E) > Ureal_2_80 then
5948 Error_Msg_Name_1 := Name_Small;
5949 Error_Msg_N
5950 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
5951 end if;
5953 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
5954 Error_Msg_Name_1 := Name_First;
5955 Error_Msg_N
5956 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
5957 end if;
5959 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
5960 Error_Msg_Name_1 := Name_Last;
5961 Error_Msg_N
5962 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
5963 end if;
5964 end if;
5966 elsif Is_Enumeration_Type (E) then
5967 Freeze_Enumeration_Type (E);
5969 elsif Is_Integer_Type (E) then
5970 Adjust_Esize_For_Alignment (E);
5972 if Is_Modular_Integer_Type (E)
5973 and then Warn_On_Suspicious_Modulus_Value
5974 then
5975 Check_Suspicious_Modulus (E);
5976 end if;
5978 -- The pool applies to named and anonymous access types, but not
5979 -- to subprogram and to internal types generated for 'Access
5980 -- references.
5982 elsif Is_Access_Type (E)
5983 and then not Is_Access_Subprogram_Type (E)
5984 and then Ekind (E) /= E_Access_Attribute_Type
5985 then
5986 -- If a pragma Default_Storage_Pool applies, and this type has no
5987 -- Storage_Pool or Storage_Size clause (which must have occurred
5988 -- before the freezing point), then use the default. This applies
5989 -- only to base types.
5991 -- None of this applies to access to subprograms, for which there
5992 -- are clearly no pools.
5994 if Present (Default_Pool)
5995 and then Is_Base_Type (E)
5996 and then not Has_Storage_Size_Clause (E)
5997 and then No (Associated_Storage_Pool (E))
5998 then
5999 -- Case of pragma Default_Storage_Pool (null)
6001 if Nkind (Default_Pool) = N_Null then
6002 Set_No_Pool_Assigned (E);
6004 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
6006 else
6007 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
6008 end if;
6009 end if;
6011 -- Check restriction for standard storage pool
6013 if No (Associated_Storage_Pool (E)) then
6014 Check_Restriction (No_Standard_Storage_Pools, E);
6015 end if;
6017 -- Deal with error message for pure access type. This is not an
6018 -- error in Ada 2005 if there is no pool (see AI-366).
6020 if Is_Pure_Unit_Access_Type (E)
6021 and then (Ada_Version < Ada_2005
6022 or else not No_Pool_Assigned (E))
6023 and then not Is_Generic_Unit (Scope (E))
6024 then
6025 Error_Msg_N ("named access type not allowed in pure unit", E);
6027 if Ada_Version >= Ada_2005 then
6028 Error_Msg_N
6029 ("\would be legal if Storage_Size of 0 given??", E);
6031 elsif No_Pool_Assigned (E) then
6032 Error_Msg_N
6033 ("\would be legal in Ada 2005??", E);
6035 else
6036 Error_Msg_N
6037 ("\would be legal in Ada 2005 if "
6038 & "Storage_Size of 0 given??", E);
6039 end if;
6040 end if;
6041 end if;
6043 -- Case of composite types
6045 if Is_Composite_Type (E) then
6047 -- AI-117 requires that all new primitives of a tagged type must
6048 -- inherit the convention of the full view of the type. Inherited
6049 -- and overriding operations are defined to inherit the convention
6050 -- of their parent or overridden subprogram (also specified in
6051 -- AI-117), which will have occurred earlier (in Derive_Subprogram
6052 -- and New_Overloaded_Entity). Here we set the convention of
6053 -- primitives that are still convention Ada, which will ensure
6054 -- that any new primitives inherit the type's convention. Class-
6055 -- wide types can have a foreign convention inherited from their
6056 -- specific type, but are excluded from this since they don't have
6057 -- any associated primitives.
6059 if Is_Tagged_Type (E)
6060 and then not Is_Class_Wide_Type (E)
6061 and then Convention (E) /= Convention_Ada
6062 then
6063 declare
6064 Prim_List : constant Elist_Id := Primitive_Operations (E);
6065 Prim : Elmt_Id;
6067 begin
6068 Prim := First_Elmt (Prim_List);
6069 while Present (Prim) loop
6070 if Convention (Node (Prim)) = Convention_Ada then
6071 Set_Convention (Node (Prim), Convention (E));
6072 end if;
6074 Next_Elmt (Prim);
6075 end loop;
6076 end;
6077 end if;
6079 -- If the type is a simple storage pool type, then this is where
6080 -- we attempt to locate and validate its Allocate, Deallocate, and
6081 -- Storage_Size operations (the first is required, and the latter
6082 -- two are optional). We also verify that the full type for a
6083 -- private type is allowed to be a simple storage pool type.
6085 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
6086 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
6087 then
6088 -- If the type is marked Has_Private_Declaration, then this is
6089 -- a full type for a private type that was specified with the
6090 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
6091 -- pragma is allowed for the full type (for example, it can't
6092 -- be an array type, or a nonlimited record type).
6094 if Has_Private_Declaration (E) then
6095 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
6096 and then not Is_Private_Type (E)
6097 then
6098 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
6099 Error_Msg_N
6100 ("pragma% can only apply to full type that is an " &
6101 "explicitly limited type", E);
6102 end if;
6103 end if;
6105 Validate_Simple_Pool_Ops : declare
6106 Pool_Type : Entity_Id renames E;
6107 Address_Type : constant Entity_Id := RTE (RE_Address);
6108 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
6110 procedure Validate_Simple_Pool_Op_Formal
6111 (Pool_Op : Entity_Id;
6112 Pool_Op_Formal : in out Entity_Id;
6113 Expected_Mode : Formal_Kind;
6114 Expected_Type : Entity_Id;
6115 Formal_Name : String;
6116 OK_Formal : in out Boolean);
6117 -- Validate one formal Pool_Op_Formal of the candidate pool
6118 -- operation Pool_Op. The formal must be of Expected_Type
6119 -- and have mode Expected_Mode. OK_Formal will be set to
6120 -- False if the formal doesn't match. If OK_Formal is False
6121 -- on entry, then the formal will effectively be ignored
6122 -- (because validation of the pool op has already failed).
6123 -- Upon return, Pool_Op_Formal will be updated to the next
6124 -- formal, if any.
6126 procedure Validate_Simple_Pool_Operation
6127 (Op_Name : Name_Id);
6128 -- Search for and validate a simple pool operation with the
6129 -- name Op_Name. If the name is Allocate, then there must be
6130 -- exactly one such primitive operation for the simple pool
6131 -- type. If the name is Deallocate or Storage_Size, then
6132 -- there can be at most one such primitive operation. The
6133 -- profile of the located primitive must conform to what
6134 -- is expected for each operation.
6136 ------------------------------------
6137 -- Validate_Simple_Pool_Op_Formal --
6138 ------------------------------------
6140 procedure Validate_Simple_Pool_Op_Formal
6141 (Pool_Op : Entity_Id;
6142 Pool_Op_Formal : in out Entity_Id;
6143 Expected_Mode : Formal_Kind;
6144 Expected_Type : Entity_Id;
6145 Formal_Name : String;
6146 OK_Formal : in out Boolean)
6148 begin
6149 -- If OK_Formal is False on entry, then simply ignore
6150 -- the formal, because an earlier formal has already
6151 -- been flagged.
6153 if not OK_Formal then
6154 return;
6156 -- If no formal is passed in, then issue an error for a
6157 -- missing formal.
6159 elsif not Present (Pool_Op_Formal) then
6160 Error_Msg_NE
6161 ("simple storage pool op missing formal " &
6162 Formal_Name & " of type&", Pool_Op, Expected_Type);
6163 OK_Formal := False;
6165 return;
6166 end if;
6168 if Etype (Pool_Op_Formal) /= Expected_Type then
6170 -- If the pool type was expected for this formal, then
6171 -- this will not be considered a candidate operation
6172 -- for the simple pool, so we unset OK_Formal so that
6173 -- the op and any later formals will be ignored.
6175 if Expected_Type = Pool_Type then
6176 OK_Formal := False;
6178 return;
6180 else
6181 Error_Msg_NE
6182 ("wrong type for formal " & Formal_Name &
6183 " of simple storage pool op; expected type&",
6184 Pool_Op_Formal, Expected_Type);
6185 end if;
6186 end if;
6188 -- Issue error if formal's mode is not the expected one
6190 if Ekind (Pool_Op_Formal) /= Expected_Mode then
6191 Error_Msg_N
6192 ("wrong mode for formal of simple storage pool op",
6193 Pool_Op_Formal);
6194 end if;
6196 -- Advance to the next formal
6198 Next_Formal (Pool_Op_Formal);
6199 end Validate_Simple_Pool_Op_Formal;
6201 ------------------------------------
6202 -- Validate_Simple_Pool_Operation --
6203 ------------------------------------
6205 procedure Validate_Simple_Pool_Operation
6206 (Op_Name : Name_Id)
6208 Op : Entity_Id;
6209 Found_Op : Entity_Id := Empty;
6210 Formal : Entity_Id;
6211 Is_OK : Boolean;
6213 begin
6214 pragma Assert
6215 (Nam_In (Op_Name, Name_Allocate,
6216 Name_Deallocate,
6217 Name_Storage_Size));
6219 Error_Msg_Name_1 := Op_Name;
6221 -- For each homonym declared immediately in the scope
6222 -- of the simple storage pool type, determine whether
6223 -- the homonym is an operation of the pool type, and,
6224 -- if so, check that its profile is as expected for
6225 -- a simple pool operation of that name.
6227 Op := Get_Name_Entity_Id (Op_Name);
6228 while Present (Op) loop
6229 if Ekind_In (Op, E_Function, E_Procedure)
6230 and then Scope (Op) = Current_Scope
6231 then
6232 Formal := First_Entity (Op);
6234 Is_OK := True;
6236 -- The first parameter must be of the pool type
6237 -- in order for the operation to qualify.
6239 if Op_Name = Name_Storage_Size then
6240 Validate_Simple_Pool_Op_Formal
6241 (Op, Formal, E_In_Parameter, Pool_Type,
6242 "Pool", Is_OK);
6243 else
6244 Validate_Simple_Pool_Op_Formal
6245 (Op, Formal, E_In_Out_Parameter, Pool_Type,
6246 "Pool", Is_OK);
6247 end if;
6249 -- If another operation with this name has already
6250 -- been located for the type, then flag an error,
6251 -- since we only allow the type to have a single
6252 -- such primitive.
6254 if Present (Found_Op) and then Is_OK then
6255 Error_Msg_NE
6256 ("only one % operation allowed for " &
6257 "simple storage pool type&", Op, Pool_Type);
6258 end if;
6260 -- In the case of Allocate and Deallocate, a formal
6261 -- of type System.Address is required.
6263 if Op_Name = Name_Allocate then
6264 Validate_Simple_Pool_Op_Formal
6265 (Op, Formal, E_Out_Parameter,
6266 Address_Type, "Storage_Address", Is_OK);
6268 elsif Op_Name = Name_Deallocate then
6269 Validate_Simple_Pool_Op_Formal
6270 (Op, Formal, E_In_Parameter,
6271 Address_Type, "Storage_Address", Is_OK);
6272 end if;
6274 -- In the case of Allocate and Deallocate, formals
6275 -- of type Storage_Count are required as the third
6276 -- and fourth parameters.
6278 if Op_Name /= Name_Storage_Size then
6279 Validate_Simple_Pool_Op_Formal
6280 (Op, Formal, E_In_Parameter,
6281 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
6282 Validate_Simple_Pool_Op_Formal
6283 (Op, Formal, E_In_Parameter,
6284 Stg_Cnt_Type, "Alignment", Is_OK);
6285 end if;
6287 -- If no mismatched formals have been found (Is_OK)
6288 -- and no excess formals are present, then this
6289 -- operation has been validated, so record it.
6291 if not Present (Formal) and then Is_OK then
6292 Found_Op := Op;
6293 end if;
6294 end if;
6296 Op := Homonym (Op);
6297 end loop;
6299 -- There must be a valid Allocate operation for the type,
6300 -- so issue an error if none was found.
6302 if Op_Name = Name_Allocate
6303 and then not Present (Found_Op)
6304 then
6305 Error_Msg_N ("missing % operation for simple " &
6306 "storage pool type", Pool_Type);
6308 elsif Present (Found_Op) then
6310 -- Simple pool operations can't be abstract
6312 if Is_Abstract_Subprogram (Found_Op) then
6313 Error_Msg_N
6314 ("simple storage pool operation must not be " &
6315 "abstract", Found_Op);
6316 end if;
6318 -- The Storage_Size operation must be a function with
6319 -- Storage_Count as its result type.
6321 if Op_Name = Name_Storage_Size then
6322 if Ekind (Found_Op) = E_Procedure then
6323 Error_Msg_N
6324 ("% operation must be a function", Found_Op);
6326 elsif Etype (Found_Op) /= Stg_Cnt_Type then
6327 Error_Msg_NE
6328 ("wrong result type for%, expected type&",
6329 Found_Op, Stg_Cnt_Type);
6330 end if;
6332 -- Allocate and Deallocate must be procedures
6334 elsif Ekind (Found_Op) = E_Function then
6335 Error_Msg_N
6336 ("% operation must be a procedure", Found_Op);
6337 end if;
6338 end if;
6339 end Validate_Simple_Pool_Operation;
6341 -- Start of processing for Validate_Simple_Pool_Ops
6343 begin
6344 Validate_Simple_Pool_Operation (Name_Allocate);
6345 Validate_Simple_Pool_Operation (Name_Deallocate);
6346 Validate_Simple_Pool_Operation (Name_Storage_Size);
6347 end Validate_Simple_Pool_Ops;
6348 end if;
6349 end if;
6351 -- Now that all types from which E may depend are frozen, see if the
6352 -- size is known at compile time, if it must be unsigned, or if
6353 -- strict alignment is required
6355 Check_Compile_Time_Size (E);
6356 Check_Unsigned_Type (E);
6358 if Base_Type (E) = E then
6359 Check_Strict_Alignment (E);
6360 end if;
6362 -- Do not allow a size clause for a type which does not have a size
6363 -- that is known at compile time
6365 if Has_Size_Clause (E)
6366 and then not Size_Known_At_Compile_Time (E)
6367 then
6368 -- Suppress this message if errors posted on E, even if we are
6369 -- in all errors mode, since this is often a junk message
6371 if not Error_Posted (E) then
6372 Error_Msg_N
6373 ("size clause not allowed for variable length type",
6374 Size_Clause (E));
6375 end if;
6376 end if;
6378 -- Now we set/verify the representation information, in particular
6379 -- the size and alignment values. This processing is not required for
6380 -- generic types, since generic types do not play any part in code
6381 -- generation, and so the size and alignment values for such types
6382 -- are irrelevant. Ditto for types declared within a generic unit,
6383 -- which may have components that depend on generic parameters, and
6384 -- that will be recreated in an instance.
6386 if Inside_A_Generic then
6387 null;
6389 -- Otherwise we call the layout procedure
6391 else
6392 Layout_Type (E);
6393 end if;
6395 -- If this is an access to subprogram whose designated type is itself
6396 -- a subprogram type, the return type of this anonymous subprogram
6397 -- type must be decorated as well.
6399 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
6400 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
6401 then
6402 Layout_Type (Etype (Designated_Type (E)));
6403 end if;
6405 -- If the type has a Defaut_Value/Default_Component_Value aspect,
6406 -- this is where we analye the expression (after the type is frozen,
6407 -- since in the case of Default_Value, we are analyzing with the
6408 -- type itself, and we treat Default_Component_Value similarly for
6409 -- the sake of uniformity).
6411 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
6412 declare
6413 Nam : Name_Id;
6414 Exp : Node_Id;
6415 Typ : Entity_Id;
6417 begin
6418 if Is_Scalar_Type (E) then
6419 Nam := Name_Default_Value;
6420 Typ := E;
6421 Exp := Default_Aspect_Value (Typ);
6422 else
6423 Nam := Name_Default_Component_Value;
6424 Typ := Component_Type (E);
6425 Exp := Default_Aspect_Component_Value (E);
6426 end if;
6428 Analyze_And_Resolve (Exp, Typ);
6430 if Etype (Exp) /= Any_Type then
6431 if not Is_OK_Static_Expression (Exp) then
6432 Error_Msg_Name_1 := Nam;
6433 Flag_Non_Static_Expr
6434 ("aspect% requires static expression", Exp);
6435 end if;
6436 end if;
6437 end;
6438 end if;
6440 -- End of freeze processing for type entities
6441 end if;
6443 -- Here is where we logically freeze the current entity. If it has a
6444 -- freeze node, then this is the point at which the freeze node is
6445 -- linked into the result list.
6447 if Has_Delayed_Freeze (E) then
6449 -- If a freeze node is already allocated, use it, otherwise allocate
6450 -- a new one. The preallocation happens in the case of anonymous base
6451 -- types, where we preallocate so that we can set First_Subtype_Link.
6452 -- Note that we reset the Sloc to the current freeze location.
6454 if Present (Freeze_Node (E)) then
6455 F_Node := Freeze_Node (E);
6456 Set_Sloc (F_Node, Loc);
6458 else
6459 F_Node := New_Freeze_Node;
6460 Set_Freeze_Node (E, F_Node);
6461 Set_Access_Types_To_Process (F_Node, No_Elist);
6462 Set_TSS_Elist (F_Node, No_Elist);
6463 Set_Actions (F_Node, No_List);
6464 end if;
6466 Set_Entity (F_Node, E);
6467 Add_To_Result (F_Node);
6469 -- A final pass over record types with discriminants. If the type
6470 -- has an incomplete declaration, there may be constrained access
6471 -- subtypes declared elsewhere, which do not depend on the discrimi-
6472 -- nants of the type, and which are used as component types (i.e.
6473 -- the full view is a recursive type). The designated types of these
6474 -- subtypes can only be elaborated after the type itself, and they
6475 -- need an itype reference.
6477 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
6478 declare
6479 Comp : Entity_Id;
6480 IR : Node_Id;
6481 Typ : Entity_Id;
6483 begin
6484 Comp := First_Component (E);
6485 while Present (Comp) loop
6486 Typ := Etype (Comp);
6488 if Ekind (Comp) = E_Component
6489 and then Is_Access_Type (Typ)
6490 and then Scope (Typ) /= E
6491 and then Base_Type (Designated_Type (Typ)) = E
6492 and then Is_Itype (Designated_Type (Typ))
6493 then
6494 IR := Make_Itype_Reference (Sloc (Comp));
6495 Set_Itype (IR, Designated_Type (Typ));
6496 Append (IR, Result);
6497 end if;
6499 Next_Component (Comp);
6500 end loop;
6501 end;
6502 end if;
6503 end if;
6505 -- When a type is frozen, the first subtype of the type is frozen as
6506 -- well (RM 13.14(15)). This has to be done after freezing the type,
6507 -- since obviously the first subtype depends on its own base type.
6509 if Is_Type (E) then
6510 Freeze_And_Append (First_Subtype (E), N, Result);
6512 -- If we just froze a tagged non-class wide record, then freeze the
6513 -- corresponding class-wide type. This must be done after the tagged
6514 -- type itself is frozen, because the class-wide type refers to the
6515 -- tagged type which generates the class.
6517 if Is_Tagged_Type (E)
6518 and then not Is_Class_Wide_Type (E)
6519 and then Present (Class_Wide_Type (E))
6520 then
6521 Freeze_And_Append (Class_Wide_Type (E), N, Result);
6522 end if;
6523 end if;
6525 Check_Debug_Info_Needed (E);
6527 -- Special handling for subprograms
6529 if Is_Subprogram (E) then
6531 -- If subprogram has address clause then reset Is_Public flag, since
6532 -- we do not want the backend to generate external references.
6534 if Present (Address_Clause (E))
6535 and then not Is_Library_Level_Entity (E)
6536 then
6537 Set_Is_Public (E, False);
6538 end if;
6539 end if;
6541 Ghost_Mode := Save_Ghost_Mode;
6542 return Result;
6543 end Freeze_Entity;
6545 -----------------------------
6546 -- Freeze_Enumeration_Type --
6547 -----------------------------
6549 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6550 begin
6551 -- By default, if no size clause is present, an enumeration type with
6552 -- Convention C is assumed to interface to a C enum, and has integer
6553 -- size. This applies to types. For subtypes, verify that its base
6554 -- type has no size clause either. Treat other foreign conventions
6555 -- in the same way, and also make sure alignment is set right.
6557 if Has_Foreign_Convention (Typ)
6558 and then not Has_Size_Clause (Typ)
6559 and then not Has_Size_Clause (Base_Type (Typ))
6560 and then Esize (Typ) < Standard_Integer_Size
6562 -- Don't do this if Short_Enums on target
6564 and then not Target_Short_Enums
6565 then
6566 Init_Esize (Typ, Standard_Integer_Size);
6567 Set_Alignment (Typ, Alignment (Standard_Integer));
6569 -- Normal Ada case or size clause present or not Long_C_Enums on target
6571 else
6572 -- If the enumeration type interfaces to C, and it has a size clause
6573 -- that specifies less than int size, it warrants a warning. The
6574 -- user may intend the C type to be an enum or a char, so this is
6575 -- not by itself an error that the Ada compiler can detect, but it
6576 -- it is a worth a heads-up. For Boolean and Character types we
6577 -- assume that the programmer has the proper C type in mind.
6579 if Convention (Typ) = Convention_C
6580 and then Has_Size_Clause (Typ)
6581 and then Esize (Typ) /= Esize (Standard_Integer)
6582 and then not Is_Boolean_Type (Typ)
6583 and then not Is_Character_Type (Typ)
6585 -- Don't do this if Short_Enums on target
6587 and then not Target_Short_Enums
6588 then
6589 Error_Msg_N
6590 ("C enum types have the size of a C int??", Size_Clause (Typ));
6591 end if;
6593 Adjust_Esize_For_Alignment (Typ);
6594 end if;
6595 end Freeze_Enumeration_Type;
6597 -----------------------
6598 -- Freeze_Expression --
6599 -----------------------
6601 procedure Freeze_Expression (N : Node_Id) is
6602 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6603 Typ : Entity_Id;
6604 Nam : Entity_Id;
6605 Desig_Typ : Entity_Id;
6606 P : Node_Id;
6607 Parent_P : Node_Id;
6609 Freeze_Outside : Boolean := False;
6610 -- This flag is set true if the entity must be frozen outside the
6611 -- current subprogram. This happens in the case of expander generated
6612 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6613 -- not freeze all entities like other bodies, but which nevertheless
6614 -- may reference entities that have to be frozen before the body and
6615 -- obviously cannot be frozen inside the body.
6617 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6618 -- If the expression is an array aggregate, the type of the component
6619 -- expressions is also frozen. If the component type is an access type
6620 -- and the expressions include allocators, the designed type is frozen
6621 -- as well.
6623 function In_Expanded_Body (N : Node_Id) return Boolean;
6624 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6625 -- it is the handled statement sequence of an expander-generated
6626 -- subprogram (init proc, stream subprogram, or renaming as body).
6627 -- If so, this is not a freezing context.
6629 -----------------------------------------
6630 -- Find_Aggregate_Component_Desig_Type --
6631 -----------------------------------------
6633 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6634 Assoc : Node_Id;
6635 Exp : Node_Id;
6637 begin
6638 if Present (Expressions (N)) then
6639 Exp := First (Expressions (N));
6640 while Present (Exp) loop
6641 if Nkind (Exp) = N_Allocator then
6642 return Designated_Type (Component_Type (Etype (N)));
6643 end if;
6645 Next (Exp);
6646 end loop;
6647 end if;
6649 if Present (Component_Associations (N)) then
6650 Assoc := First (Component_Associations (N));
6651 while Present (Assoc) loop
6652 if Nkind (Expression (Assoc)) = N_Allocator then
6653 return Designated_Type (Component_Type (Etype (N)));
6654 end if;
6656 Next (Assoc);
6657 end loop;
6658 end if;
6660 return Empty;
6661 end Find_Aggregate_Component_Desig_Type;
6663 ----------------------
6664 -- In_Expanded_Body --
6665 ----------------------
6667 function In_Expanded_Body (N : Node_Id) return Boolean is
6668 P : Node_Id;
6669 Id : Entity_Id;
6671 begin
6672 if Nkind (N) = N_Subprogram_Body then
6673 P := N;
6674 else
6675 P := Parent (N);
6676 end if;
6678 if Nkind (P) /= N_Subprogram_Body then
6679 return False;
6681 else
6682 Id := Defining_Unit_Name (Specification (P));
6684 -- The following are expander-created bodies, or bodies that
6685 -- are not freeze points.
6687 if Nkind (Id) = N_Defining_Identifier
6688 and then (Is_Init_Proc (Id)
6689 or else Is_TSS (Id, TSS_Stream_Input)
6690 or else Is_TSS (Id, TSS_Stream_Output)
6691 or else Is_TSS (Id, TSS_Stream_Read)
6692 or else Is_TSS (Id, TSS_Stream_Write)
6693 or else Nkind_In (Original_Node (P),
6694 N_Subprogram_Renaming_Declaration,
6695 N_Expression_Function))
6696 then
6697 return True;
6698 else
6699 return False;
6700 end if;
6701 end if;
6702 end In_Expanded_Body;
6704 -- Start of processing for Freeze_Expression
6706 begin
6707 -- Immediate return if freezing is inhibited. This flag is set by the
6708 -- analyzer to stop freezing on generated expressions that would cause
6709 -- freezing if they were in the source program, but which are not
6710 -- supposed to freeze, since they are created.
6712 if Must_Not_Freeze (N) then
6713 return;
6714 end if;
6716 -- If expression is non-static, then it does not freeze in a default
6717 -- expression, see section "Handling of Default Expressions" in the
6718 -- spec of package Sem for further details. Note that we have to make
6719 -- sure that we actually have a real expression (if we have a subtype
6720 -- indication, we can't test Is_OK_Static_Expression). However, we
6721 -- exclude the case of the prefix of an attribute of a static scalar
6722 -- subtype from this early return, because static subtype attributes
6723 -- should always cause freezing, even in default expressions, but
6724 -- the attribute may not have been marked as static yet (because in
6725 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
6726 -- Freeze_Expression on the prefix).
6728 if In_Spec_Exp
6729 and then Nkind (N) in N_Subexpr
6730 and then not Is_OK_Static_Expression (N)
6731 and then (Nkind (Parent (N)) /= N_Attribute_Reference
6732 or else not (Is_Entity_Name (N)
6733 and then Is_Type (Entity (N))
6734 and then Is_OK_Static_Subtype (Entity (N))))
6735 then
6736 return;
6737 end if;
6739 -- Freeze type of expression if not frozen already
6741 Typ := Empty;
6743 if Nkind (N) in N_Has_Etype then
6744 if not Is_Frozen (Etype (N)) then
6745 Typ := Etype (N);
6747 -- Base type may be an derived numeric type that is frozen at
6748 -- the point of declaration, but first_subtype is still unfrozen.
6750 elsif not Is_Frozen (First_Subtype (Etype (N))) then
6751 Typ := First_Subtype (Etype (N));
6752 end if;
6753 end if;
6755 -- For entity name, freeze entity if not frozen already. A special
6756 -- exception occurs for an identifier that did not come from source.
6757 -- We don't let such identifiers freeze a non-internal entity, i.e.
6758 -- an entity that did come from source, since such an identifier was
6759 -- generated by the expander, and cannot have any semantic effect on
6760 -- the freezing semantics. For example, this stops the parameter of
6761 -- an initialization procedure from freezing the variable.
6763 if Is_Entity_Name (N)
6764 and then not Is_Frozen (Entity (N))
6765 and then (Nkind (N) /= N_Identifier
6766 or else Comes_From_Source (N)
6767 or else not Comes_From_Source (Entity (N)))
6768 then
6769 Nam := Entity (N);
6771 if Present (Nam) and then Ekind (Nam) = E_Function then
6772 Check_Expression_Function (N, Nam);
6773 end if;
6775 else
6776 Nam := Empty;
6777 end if;
6779 -- For an allocator freeze designated type if not frozen already
6781 -- For an aggregate whose component type is an access type, freeze the
6782 -- designated type now, so that its freeze does not appear within the
6783 -- loop that might be created in the expansion of the aggregate. If the
6784 -- designated type is a private type without full view, the expression
6785 -- cannot contain an allocator, so the type is not frozen.
6787 -- For a function, we freeze the entity when the subprogram declaration
6788 -- is frozen, but a function call may appear in an initialization proc.
6789 -- before the declaration is frozen. We need to generate the extra
6790 -- formals, if any, to ensure that the expansion of the call includes
6791 -- the proper actuals. This only applies to Ada subprograms, not to
6792 -- imported ones.
6794 Desig_Typ := Empty;
6796 case Nkind (N) is
6797 when N_Allocator =>
6798 Desig_Typ := Designated_Type (Etype (N));
6800 when N_Aggregate =>
6801 if Is_Array_Type (Etype (N))
6802 and then Is_Access_Type (Component_Type (Etype (N)))
6803 then
6805 -- Check whether aggregate includes allocators.
6807 Desig_Typ := Find_Aggregate_Component_Desig_Type;
6808 end if;
6810 when N_Selected_Component |
6811 N_Indexed_Component |
6812 N_Slice =>
6814 if Is_Access_Type (Etype (Prefix (N))) then
6815 Desig_Typ := Designated_Type (Etype (Prefix (N)));
6816 end if;
6818 when N_Identifier =>
6819 if Present (Nam)
6820 and then Ekind (Nam) = E_Function
6821 and then Nkind (Parent (N)) = N_Function_Call
6822 and then Convention (Nam) = Convention_Ada
6823 then
6824 Create_Extra_Formals (Nam);
6825 end if;
6827 when others =>
6828 null;
6829 end case;
6831 if Desig_Typ /= Empty
6832 and then (Is_Frozen (Desig_Typ)
6833 or else (not Is_Fully_Defined (Desig_Typ)))
6834 then
6835 Desig_Typ := Empty;
6836 end if;
6838 -- All done if nothing needs freezing
6840 if No (Typ)
6841 and then No (Nam)
6842 and then No (Desig_Typ)
6843 then
6844 return;
6845 end if;
6847 -- Examine the enclosing context by climbing the parent chain. The
6848 -- traversal serves two purposes - to detect scenarios where freezeing
6849 -- is not needed and to find the proper insertion point for the freeze
6850 -- nodes. Although somewhat similar to Insert_Actions, this traversal
6851 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
6852 -- the tree may result in types being frozen too early.
6854 P := N;
6855 loop
6856 Parent_P := Parent (P);
6858 -- If we don't have a parent, then we are not in a well-formed tree.
6859 -- This is an unusual case, but there are some legitimate situations
6860 -- in which this occurs, notably when the expressions in the range of
6861 -- a type declaration are resolved. We simply ignore the freeze
6862 -- request in this case. Is this right ???
6864 if No (Parent_P) then
6865 return;
6866 end if;
6868 -- See if we have got to an appropriate point in the tree
6870 case Nkind (Parent_P) is
6872 -- A special test for the exception of (RM 13.14(8)) for the case
6873 -- of per-object expressions (RM 3.8(18)) occurring in component
6874 -- definition or a discrete subtype definition. Note that we test
6875 -- for a component declaration which includes both cases we are
6876 -- interested in, and furthermore the tree does not have explicit
6877 -- nodes for either of these two constructs.
6879 when N_Component_Declaration =>
6881 -- The case we want to test for here is an identifier that is
6882 -- a per-object expression, this is either a discriminant that
6883 -- appears in a context other than the component declaration
6884 -- or it is a reference to the type of the enclosing construct.
6886 -- For either of these cases, we skip the freezing
6888 if not In_Spec_Expression
6889 and then Nkind (N) = N_Identifier
6890 and then (Present (Entity (N)))
6891 then
6892 -- We recognize the discriminant case by just looking for
6893 -- a reference to a discriminant. It can only be one for
6894 -- the enclosing construct. Skip freezing in this case.
6896 if Ekind (Entity (N)) = E_Discriminant then
6897 return;
6899 -- For the case of a reference to the enclosing record,
6900 -- (or task or protected type), we look for a type that
6901 -- matches the current scope.
6903 elsif Entity (N) = Current_Scope then
6904 return;
6905 end if;
6906 end if;
6908 -- If we have an enumeration literal that appears as the choice in
6909 -- the aggregate of an enumeration representation clause, then
6910 -- freezing does not occur (RM 13.14(10)).
6912 when N_Enumeration_Representation_Clause =>
6914 -- The case we are looking for is an enumeration literal
6916 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
6917 and then Is_Enumeration_Type (Etype (N))
6918 then
6919 -- If enumeration literal appears directly as the choice,
6920 -- do not freeze (this is the normal non-overloaded case)
6922 if Nkind (Parent (N)) = N_Component_Association
6923 and then First (Choices (Parent (N))) = N
6924 then
6925 return;
6927 -- If enumeration literal appears as the name of function
6928 -- which is the choice, then also do not freeze. This
6929 -- happens in the overloaded literal case, where the
6930 -- enumeration literal is temporarily changed to a function
6931 -- call for overloading analysis purposes.
6933 elsif Nkind (Parent (N)) = N_Function_Call
6934 and then
6935 Nkind (Parent (Parent (N))) = N_Component_Association
6936 and then
6937 First (Choices (Parent (Parent (N)))) = Parent (N)
6938 then
6939 return;
6940 end if;
6941 end if;
6943 -- Normally if the parent is a handled sequence of statements,
6944 -- then the current node must be a statement, and that is an
6945 -- appropriate place to insert a freeze node.
6947 when N_Handled_Sequence_Of_Statements =>
6949 -- An exception occurs when the sequence of statements is for
6950 -- an expander generated body that did not do the usual freeze
6951 -- all operation. In this case we usually want to freeze
6952 -- outside this body, not inside it, and we skip past the
6953 -- subprogram body that we are inside.
6955 if In_Expanded_Body (Parent_P) then
6956 declare
6957 Subp : constant Node_Id := Parent (Parent_P);
6958 Spec : Entity_Id;
6960 begin
6961 -- Freeze the entity only when it is declared inside the
6962 -- body of the expander generated procedure. This case
6963 -- is recognized by the scope of the entity or its type,
6964 -- which is either the spec for some enclosing body, or
6965 -- (in the case of init_procs, for which there are no
6966 -- separate specs) the current scope.
6968 if Nkind (Subp) = N_Subprogram_Body then
6969 Spec := Corresponding_Spec (Subp);
6971 if (Present (Typ) and then Scope (Typ) = Spec)
6972 or else
6973 (Present (Nam) and then Scope (Nam) = Spec)
6974 then
6975 exit;
6977 elsif Present (Typ)
6978 and then Scope (Typ) = Current_Scope
6979 and then Defining_Entity (Subp) = Current_Scope
6980 then
6981 exit;
6982 end if;
6983 end if;
6985 -- An expression function may act as a completion of
6986 -- a function declaration. As such, it can reference
6987 -- entities declared between the two views:
6989 -- Hidden []; -- 1
6990 -- function F return ...;
6991 -- private
6992 -- function Hidden return ...;
6993 -- function F return ... is (Hidden); -- 2
6995 -- Refering to the example above, freezing the expression
6996 -- of F (2) would place Hidden's freeze node (1) in the
6997 -- wrong place. Avoid explicit freezing and let the usual
6998 -- scenarios do the job - for example, reaching the end
6999 -- of the private declarations, or a call to F.
7001 if Nkind (Original_Node (Subp)) =
7002 N_Expression_Function
7003 then
7004 null;
7006 -- Freeze outside the body
7008 else
7009 Parent_P := Parent (Parent_P);
7010 Freeze_Outside := True;
7011 end if;
7012 end;
7014 -- Here if normal case where we are in handled statement
7015 -- sequence and want to do the insertion right there.
7017 else
7018 exit;
7019 end if;
7021 -- If parent is a body or a spec or a block, then the current node
7022 -- is a statement or declaration and we can insert the freeze node
7023 -- before it.
7025 when N_Block_Statement |
7026 N_Entry_Body |
7027 N_Package_Body |
7028 N_Package_Specification |
7029 N_Protected_Body |
7030 N_Subprogram_Body |
7031 N_Task_Body => exit;
7033 -- The expander is allowed to define types in any statements list,
7034 -- so any of the following parent nodes also mark a freezing point
7035 -- if the actual node is in a list of statements or declarations.
7037 when N_Abortable_Part |
7038 N_Accept_Alternative |
7039 N_And_Then |
7040 N_Case_Statement_Alternative |
7041 N_Compilation_Unit_Aux |
7042 N_Conditional_Entry_Call |
7043 N_Delay_Alternative |
7044 N_Elsif_Part |
7045 N_Entry_Call_Alternative |
7046 N_Exception_Handler |
7047 N_Extended_Return_Statement |
7048 N_Freeze_Entity |
7049 N_If_Statement |
7050 N_Or_Else |
7051 N_Selective_Accept |
7052 N_Triggering_Alternative =>
7054 exit when Is_List_Member (P);
7056 -- Freeze nodes produced by an expression coming from the Actions
7057 -- list of a N_Expression_With_Actions node must remain within the
7058 -- Actions list. Inserting the freeze nodes further up the tree
7059 -- may lead to use before declaration issues in the case of array
7060 -- types.
7062 when N_Expression_With_Actions =>
7063 if Is_List_Member (P)
7064 and then List_Containing (P) = Actions (Parent_P)
7065 then
7066 exit;
7067 end if;
7069 -- Note: N_Loop_Statement is a special case. A type that appears
7070 -- in the source can never be frozen in a loop (this occurs only
7071 -- because of a loop expanded by the expander), so we keep on
7072 -- going. Otherwise we terminate the search. Same is true of any
7073 -- entity which comes from source. (if they have predefined type,
7074 -- that type does not appear to come from source, but the entity
7075 -- should not be frozen here).
7077 when N_Loop_Statement =>
7078 exit when not Comes_From_Source (Etype (N))
7079 and then (No (Nam) or else not Comes_From_Source (Nam));
7081 -- For all other cases, keep looking at parents
7083 when others =>
7084 null;
7085 end case;
7087 -- We fall through the case if we did not yet find the proper
7088 -- place in the free for inserting the freeze node, so climb.
7090 P := Parent_P;
7091 end loop;
7093 -- If the expression appears in a record or an initialization procedure,
7094 -- the freeze nodes are collected and attached to the current scope, to
7095 -- be inserted and analyzed on exit from the scope, to insure that
7096 -- generated entities appear in the correct scope. If the expression is
7097 -- a default for a discriminant specification, the scope is still void.
7098 -- The expression can also appear in the discriminant part of a private
7099 -- or concurrent type.
7101 -- If the expression appears in a constrained subcomponent of an
7102 -- enclosing record declaration, the freeze nodes must be attached to
7103 -- the outer record type so they can eventually be placed in the
7104 -- enclosing declaration list.
7106 -- The other case requiring this special handling is if we are in a
7107 -- default expression, since in that case we are about to freeze a
7108 -- static type, and the freeze scope needs to be the outer scope, not
7109 -- the scope of the subprogram with the default parameter.
7111 -- For default expressions and other spec expressions in generic units,
7112 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
7113 -- placing them at the proper place, after the generic unit.
7115 if (In_Spec_Exp and not Inside_A_Generic)
7116 or else Freeze_Outside
7117 or else (Is_Type (Current_Scope)
7118 and then (not Is_Concurrent_Type (Current_Scope)
7119 or else not Has_Completion (Current_Scope)))
7120 or else Ekind (Current_Scope) = E_Void
7121 then
7122 declare
7123 N : constant Node_Id := Current_Scope;
7124 Freeze_Nodes : List_Id := No_List;
7125 Pos : Int := Scope_Stack.Last;
7127 begin
7128 if Present (Desig_Typ) then
7129 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
7130 end if;
7132 if Present (Typ) then
7133 Freeze_And_Append (Typ, N, Freeze_Nodes);
7134 end if;
7136 if Present (Nam) then
7137 Freeze_And_Append (Nam, N, Freeze_Nodes);
7138 end if;
7140 -- The current scope may be that of a constrained component of
7141 -- an enclosing record declaration, or of a loop of an enclosing
7142 -- quantified expression, which is above the current scope in the
7143 -- scope stack. Indeed in the context of a quantified expression,
7144 -- a scope is created and pushed above the current scope in order
7145 -- to emulate the loop-like behavior of the quantified expression.
7146 -- If the expression is within a top-level pragma, as for a pre-
7147 -- condition on a library-level subprogram, nothing to do.
7149 if not Is_Compilation_Unit (Current_Scope)
7150 and then (Is_Record_Type (Scope (Current_Scope))
7151 or else Nkind (Parent (Current_Scope)) =
7152 N_Quantified_Expression)
7153 then
7154 Pos := Pos - 1;
7155 end if;
7157 if Is_Non_Empty_List (Freeze_Nodes) then
7158 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
7159 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
7160 Freeze_Nodes;
7161 else
7162 Append_List (Freeze_Nodes,
7163 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
7164 end if;
7165 end if;
7166 end;
7168 return;
7169 end if;
7171 -- Now we have the right place to do the freezing. First, a special
7172 -- adjustment, if we are in spec-expression analysis mode, these freeze
7173 -- actions must not be thrown away (normally all inserted actions are
7174 -- thrown away in this mode. However, the freeze actions are from static
7175 -- expressions and one of the important reasons we are doing this
7176 -- special analysis is to get these freeze actions. Therefore we turn
7177 -- off the In_Spec_Expression mode to propagate these freeze actions.
7178 -- This also means they get properly analyzed and expanded.
7180 In_Spec_Expression := False;
7182 -- Freeze the designated type of an allocator (RM 13.14(13))
7184 if Present (Desig_Typ) then
7185 Freeze_Before (P, Desig_Typ);
7186 end if;
7188 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
7189 -- the enumeration representation clause exception in the loop above.
7191 if Present (Typ) then
7192 Freeze_Before (P, Typ);
7193 end if;
7195 -- Freeze name if one is present (RM 13.14(11))
7197 if Present (Nam) then
7198 Freeze_Before (P, Nam);
7199 end if;
7201 -- Restore In_Spec_Expression flag
7203 In_Spec_Expression := In_Spec_Exp;
7204 end Freeze_Expression;
7206 -----------------------------
7207 -- Freeze_Fixed_Point_Type --
7208 -----------------------------
7210 -- Certain fixed-point types and subtypes, including implicit base types
7211 -- and declared first subtypes, have not yet set up a range. This is
7212 -- because the range cannot be set until the Small and Size values are
7213 -- known, and these are not known till the type is frozen.
7215 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
7216 -- whose bounds are unanalyzed real literals. This routine will recognize
7217 -- this case, and transform this range node into a properly typed range
7218 -- with properly analyzed and resolved values.
7220 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
7221 Rng : constant Node_Id := Scalar_Range (Typ);
7222 Lo : constant Node_Id := Low_Bound (Rng);
7223 Hi : constant Node_Id := High_Bound (Rng);
7224 Btyp : constant Entity_Id := Base_Type (Typ);
7225 Brng : constant Node_Id := Scalar_Range (Btyp);
7226 BLo : constant Node_Id := Low_Bound (Brng);
7227 BHi : constant Node_Id := High_Bound (Brng);
7228 Small : constant Ureal := Small_Value (Typ);
7229 Loval : Ureal;
7230 Hival : Ureal;
7231 Atype : Entity_Id;
7233 Orig_Lo : Ureal;
7234 Orig_Hi : Ureal;
7235 -- Save original bounds (for shaving tests)
7237 Actual_Size : Nat;
7238 -- Actual size chosen
7240 function Fsize (Lov, Hiv : Ureal) return Nat;
7241 -- Returns size of type with given bounds. Also leaves these
7242 -- bounds set as the current bounds of the Typ.
7244 -----------
7245 -- Fsize --
7246 -----------
7248 function Fsize (Lov, Hiv : Ureal) return Nat is
7249 begin
7250 Set_Realval (Lo, Lov);
7251 Set_Realval (Hi, Hiv);
7252 return Minimum_Size (Typ);
7253 end Fsize;
7255 -- Start of processing for Freeze_Fixed_Point_Type
7257 begin
7258 -- If Esize of a subtype has not previously been set, set it now
7260 if Unknown_Esize (Typ) then
7261 Atype := Ancestor_Subtype (Typ);
7263 if Present (Atype) then
7264 Set_Esize (Typ, Esize (Atype));
7265 else
7266 Set_Esize (Typ, Esize (Base_Type (Typ)));
7267 end if;
7268 end if;
7270 -- Immediate return if the range is already analyzed. This means that
7271 -- the range is already set, and does not need to be computed by this
7272 -- routine.
7274 if Analyzed (Rng) then
7275 return;
7276 end if;
7278 -- Immediate return if either of the bounds raises Constraint_Error
7280 if Raises_Constraint_Error (Lo)
7281 or else Raises_Constraint_Error (Hi)
7282 then
7283 return;
7284 end if;
7286 Loval := Realval (Lo);
7287 Hival := Realval (Hi);
7289 Orig_Lo := Loval;
7290 Orig_Hi := Hival;
7292 -- Ordinary fixed-point case
7294 if Is_Ordinary_Fixed_Point_Type (Typ) then
7296 -- For the ordinary fixed-point case, we are allowed to fudge the
7297 -- end-points up or down by small. Generally we prefer to fudge up,
7298 -- i.e. widen the bounds for non-model numbers so that the end points
7299 -- are included. However there are cases in which this cannot be
7300 -- done, and indeed cases in which we may need to narrow the bounds.
7301 -- The following circuit makes the decision.
7303 -- Note: our terminology here is that Incl_EP means that the bounds
7304 -- are widened by Small if necessary to include the end points, and
7305 -- Excl_EP means that the bounds are narrowed by Small to exclude the
7306 -- end-points if this reduces the size.
7308 -- Note that in the Incl case, all we care about is including the
7309 -- end-points. In the Excl case, we want to narrow the bounds as
7310 -- much as permitted by the RM, to give the smallest possible size.
7312 Fudge : declare
7313 Loval_Incl_EP : Ureal;
7314 Hival_Incl_EP : Ureal;
7316 Loval_Excl_EP : Ureal;
7317 Hival_Excl_EP : Ureal;
7319 Size_Incl_EP : Nat;
7320 Size_Excl_EP : Nat;
7322 Model_Num : Ureal;
7323 First_Subt : Entity_Id;
7324 Actual_Lo : Ureal;
7325 Actual_Hi : Ureal;
7327 begin
7328 -- First step. Base types are required to be symmetrical. Right
7329 -- now, the base type range is a copy of the first subtype range.
7330 -- This will be corrected before we are done, but right away we
7331 -- need to deal with the case where both bounds are non-negative.
7332 -- In this case, we set the low bound to the negative of the high
7333 -- bound, to make sure that the size is computed to include the
7334 -- required sign. Note that we do not need to worry about the
7335 -- case of both bounds negative, because the sign will be dealt
7336 -- with anyway. Furthermore we can't just go making such a bound
7337 -- symmetrical, since in a twos-complement system, there is an
7338 -- extra negative value which could not be accommodated on the
7339 -- positive side.
7341 if Typ = Btyp
7342 and then not UR_Is_Negative (Loval)
7343 and then Hival > Loval
7344 then
7345 Loval := -Hival;
7346 Set_Realval (Lo, Loval);
7347 end if;
7349 -- Compute the fudged bounds. If the number is a model number,
7350 -- then we do nothing to include it, but we are allowed to backoff
7351 -- to the next adjacent model number when we exclude it. If it is
7352 -- not a model number then we straddle the two values with the
7353 -- model numbers on either side.
7355 Model_Num := UR_Trunc (Loval / Small) * Small;
7357 if Loval = Model_Num then
7358 Loval_Incl_EP := Model_Num;
7359 else
7360 Loval_Incl_EP := Model_Num - Small;
7361 end if;
7363 -- The low value excluding the end point is Small greater, but
7364 -- we do not do this exclusion if the low value is positive,
7365 -- since it can't help the size and could actually hurt by
7366 -- crossing the high bound.
7368 if UR_Is_Negative (Loval_Incl_EP) then
7369 Loval_Excl_EP := Loval_Incl_EP + Small;
7371 -- If the value went from negative to zero, then we have the
7372 -- case where Loval_Incl_EP is the model number just below
7373 -- zero, so we want to stick to the negative value for the
7374 -- base type to maintain the condition that the size will
7375 -- include signed values.
7377 if Typ = Btyp
7378 and then UR_Is_Zero (Loval_Excl_EP)
7379 then
7380 Loval_Excl_EP := Loval_Incl_EP;
7381 end if;
7383 else
7384 Loval_Excl_EP := Loval_Incl_EP;
7385 end if;
7387 -- Similar processing for upper bound and high value
7389 Model_Num := UR_Trunc (Hival / Small) * Small;
7391 if Hival = Model_Num then
7392 Hival_Incl_EP := Model_Num;
7393 else
7394 Hival_Incl_EP := Model_Num + Small;
7395 end if;
7397 if UR_Is_Positive (Hival_Incl_EP) then
7398 Hival_Excl_EP := Hival_Incl_EP - Small;
7399 else
7400 Hival_Excl_EP := Hival_Incl_EP;
7401 end if;
7403 -- One further adjustment is needed. In the case of subtypes, we
7404 -- cannot go outside the range of the base type, or we get
7405 -- peculiarities, and the base type range is already set. This
7406 -- only applies to the Incl values, since clearly the Excl values
7407 -- are already as restricted as they are allowed to be.
7409 if Typ /= Btyp then
7410 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
7411 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
7412 end if;
7414 -- Get size including and excluding end points
7416 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
7417 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
7419 -- No need to exclude end-points if it does not reduce size
7421 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
7422 Loval_Excl_EP := Loval_Incl_EP;
7423 end if;
7425 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
7426 Hival_Excl_EP := Hival_Incl_EP;
7427 end if;
7429 -- Now we set the actual size to be used. We want to use the
7430 -- bounds fudged up to include the end-points but only if this
7431 -- can be done without violating a specifically given size
7432 -- size clause or causing an unacceptable increase in size.
7434 -- Case of size clause given
7436 if Has_Size_Clause (Typ) then
7438 -- Use the inclusive size only if it is consistent with
7439 -- the explicitly specified size.
7441 if Size_Incl_EP <= RM_Size (Typ) then
7442 Actual_Lo := Loval_Incl_EP;
7443 Actual_Hi := Hival_Incl_EP;
7444 Actual_Size := Size_Incl_EP;
7446 -- If the inclusive size is too large, we try excluding
7447 -- the end-points (will be caught later if does not work).
7449 else
7450 Actual_Lo := Loval_Excl_EP;
7451 Actual_Hi := Hival_Excl_EP;
7452 Actual_Size := Size_Excl_EP;
7453 end if;
7455 -- Case of size clause not given
7457 else
7458 -- If we have a base type whose corresponding first subtype
7459 -- has an explicit size that is large enough to include our
7460 -- end-points, then do so. There is no point in working hard
7461 -- to get a base type whose size is smaller than the specified
7462 -- size of the first subtype.
7464 First_Subt := First_Subtype (Typ);
7466 if Has_Size_Clause (First_Subt)
7467 and then Size_Incl_EP <= Esize (First_Subt)
7468 then
7469 Actual_Size := Size_Incl_EP;
7470 Actual_Lo := Loval_Incl_EP;
7471 Actual_Hi := Hival_Incl_EP;
7473 -- If excluding the end-points makes the size smaller and
7474 -- results in a size of 8,16,32,64, then we take the smaller
7475 -- size. For the 64 case, this is compulsory. For the other
7476 -- cases, it seems reasonable. We like to include end points
7477 -- if we can, but not at the expense of moving to the next
7478 -- natural boundary of size.
7480 elsif Size_Incl_EP /= Size_Excl_EP
7481 and then Addressable (Size_Excl_EP)
7482 then
7483 Actual_Size := Size_Excl_EP;
7484 Actual_Lo := Loval_Excl_EP;
7485 Actual_Hi := Hival_Excl_EP;
7487 -- Otherwise we can definitely include the end points
7489 else
7490 Actual_Size := Size_Incl_EP;
7491 Actual_Lo := Loval_Incl_EP;
7492 Actual_Hi := Hival_Incl_EP;
7493 end if;
7495 -- One pathological case: normally we never fudge a low bound
7496 -- down, since it would seem to increase the size (if it has
7497 -- any effect), but for ranges containing single value, or no
7498 -- values, the high bound can be small too large. Consider:
7500 -- type t is delta 2.0**(-14)
7501 -- range 131072.0 .. 0;
7503 -- That lower bound is *just* outside the range of 32 bits, and
7504 -- does need fudging down in this case. Note that the bounds
7505 -- will always have crossed here, since the high bound will be
7506 -- fudged down if necessary, as in the case of:
7508 -- type t is delta 2.0**(-14)
7509 -- range 131072.0 .. 131072.0;
7511 -- So we detect the situation by looking for crossed bounds,
7512 -- and if the bounds are crossed, and the low bound is greater
7513 -- than zero, we will always back it off by small, since this
7514 -- is completely harmless.
7516 if Actual_Lo > Actual_Hi then
7517 if UR_Is_Positive (Actual_Lo) then
7518 Actual_Lo := Loval_Incl_EP - Small;
7519 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7521 -- And of course, we need to do exactly the same parallel
7522 -- fudge for flat ranges in the negative region.
7524 elsif UR_Is_Negative (Actual_Hi) then
7525 Actual_Hi := Hival_Incl_EP + Small;
7526 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7527 end if;
7528 end if;
7529 end if;
7531 Set_Realval (Lo, Actual_Lo);
7532 Set_Realval (Hi, Actual_Hi);
7533 end Fudge;
7535 -- For the decimal case, none of this fudging is required, since there
7536 -- are no end-point problems in the decimal case (the end-points are
7537 -- always included).
7539 else
7540 Actual_Size := Fsize (Loval, Hival);
7541 end if;
7543 -- At this stage, the actual size has been calculated and the proper
7544 -- required bounds are stored in the low and high bounds.
7546 if Actual_Size > 64 then
7547 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7548 Error_Msg_N
7549 ("size required (^) for type& too large, maximum allowed is 64",
7550 Typ);
7551 Actual_Size := 64;
7552 end if;
7554 -- Check size against explicit given size
7556 if Has_Size_Clause (Typ) then
7557 if Actual_Size > RM_Size (Typ) then
7558 Error_Msg_Uint_1 := RM_Size (Typ);
7559 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7560 Error_Msg_NE
7561 ("size given (^) for type& too small, minimum allowed is ^",
7562 Size_Clause (Typ), Typ);
7564 else
7565 Actual_Size := UI_To_Int (Esize (Typ));
7566 end if;
7568 -- Increase size to next natural boundary if no size clause given
7570 else
7571 if Actual_Size <= 8 then
7572 Actual_Size := 8;
7573 elsif Actual_Size <= 16 then
7574 Actual_Size := 16;
7575 elsif Actual_Size <= 32 then
7576 Actual_Size := 32;
7577 else
7578 Actual_Size := 64;
7579 end if;
7581 Init_Esize (Typ, Actual_Size);
7582 Adjust_Esize_For_Alignment (Typ);
7583 end if;
7585 -- If we have a base type, then expand the bounds so that they extend to
7586 -- the full width of the allocated size in bits, to avoid junk range
7587 -- checks on intermediate computations.
7589 if Base_Type (Typ) = Typ then
7590 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7591 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7592 end if;
7594 -- Final step is to reanalyze the bounds using the proper type
7595 -- and set the Corresponding_Integer_Value fields of the literals.
7597 Set_Etype (Lo, Empty);
7598 Set_Analyzed (Lo, False);
7599 Analyze (Lo);
7601 -- Resolve with universal fixed if the base type, and the base type if
7602 -- it is a subtype. Note we can't resolve the base type with itself,
7603 -- that would be a reference before definition.
7605 if Typ = Btyp then
7606 Resolve (Lo, Universal_Fixed);
7607 else
7608 Resolve (Lo, Btyp);
7609 end if;
7611 -- Set corresponding integer value for bound
7613 Set_Corresponding_Integer_Value
7614 (Lo, UR_To_Uint (Realval (Lo) / Small));
7616 -- Similar processing for high bound
7618 Set_Etype (Hi, Empty);
7619 Set_Analyzed (Hi, False);
7620 Analyze (Hi);
7622 if Typ = Btyp then
7623 Resolve (Hi, Universal_Fixed);
7624 else
7625 Resolve (Hi, Btyp);
7626 end if;
7628 Set_Corresponding_Integer_Value
7629 (Hi, UR_To_Uint (Realval (Hi) / Small));
7631 -- Set type of range to correspond to bounds
7633 Set_Etype (Rng, Etype (Lo));
7635 -- Set Esize to calculated size if not set already
7637 if Unknown_Esize (Typ) then
7638 Init_Esize (Typ, Actual_Size);
7639 end if;
7641 -- Set RM_Size if not already set. If already set, check value
7643 declare
7644 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7646 begin
7647 if RM_Size (Typ) /= Uint_0 then
7648 if RM_Size (Typ) < Minsiz then
7649 Error_Msg_Uint_1 := RM_Size (Typ);
7650 Error_Msg_Uint_2 := Minsiz;
7651 Error_Msg_NE
7652 ("size given (^) for type& too small, minimum allowed is ^",
7653 Size_Clause (Typ), Typ);
7654 end if;
7656 else
7657 Set_RM_Size (Typ, Minsiz);
7658 end if;
7659 end;
7661 -- Check for shaving
7663 if Comes_From_Source (Typ) then
7665 -- In SPARK mode the given bounds must be strictly representable
7667 if SPARK_Mode = On then
7668 if Orig_Lo < Expr_Value_R (Lo) then
7669 Error_Msg_NE
7670 ("declared low bound of type & is outside type range",
7671 Lo, Typ);
7672 end if;
7674 if Orig_Hi > Expr_Value_R (Hi) then
7675 Error_Msg_NE
7676 ("declared high bound of type & is outside type range",
7677 Hi, Typ);
7678 end if;
7680 else
7681 if Orig_Lo < Expr_Value_R (Lo) then
7682 Error_Msg_N
7683 ("declared low bound of type & is outside type range??", Typ);
7684 Error_Msg_N
7685 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
7686 end if;
7688 if Orig_Hi > Expr_Value_R (Hi) then
7689 Error_Msg_N
7690 ("declared high bound of type & is outside type range??",
7691 Typ);
7692 Error_Msg_N
7693 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
7694 end if;
7695 end if;
7696 end if;
7697 end Freeze_Fixed_Point_Type;
7699 ------------------
7700 -- Freeze_Itype --
7701 ------------------
7703 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7704 L : List_Id;
7706 begin
7707 Set_Has_Delayed_Freeze (T);
7708 L := Freeze_Entity (T, N);
7710 if Is_Non_Empty_List (L) then
7711 Insert_Actions (N, L);
7712 end if;
7713 end Freeze_Itype;
7715 --------------------------
7716 -- Freeze_Static_Object --
7717 --------------------------
7719 procedure Freeze_Static_Object (E : Entity_Id) is
7721 Cannot_Be_Static : exception;
7722 -- Exception raised if the type of a static object cannot be made
7723 -- static. This happens if the type depends on non-global objects.
7725 procedure Ensure_Expression_Is_SA (N : Node_Id);
7726 -- Called to ensure that an expression used as part of a type definition
7727 -- is statically allocatable, which means that the expression type is
7728 -- statically allocatable, and the expression is either static, or a
7729 -- reference to a library level constant.
7731 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7732 -- Called to mark a type as static, checking that it is possible
7733 -- to set the type as static. If it is not possible, then the
7734 -- exception Cannot_Be_Static is raised.
7736 -----------------------------
7737 -- Ensure_Expression_Is_SA --
7738 -----------------------------
7740 procedure Ensure_Expression_Is_SA (N : Node_Id) is
7741 Ent : Entity_Id;
7743 begin
7744 Ensure_Type_Is_SA (Etype (N));
7746 if Is_OK_Static_Expression (N) then
7747 return;
7749 elsif Nkind (N) = N_Identifier then
7750 Ent := Entity (N);
7752 if Present (Ent)
7753 and then Ekind (Ent) = E_Constant
7754 and then Is_Library_Level_Entity (Ent)
7755 then
7756 return;
7757 end if;
7758 end if;
7760 raise Cannot_Be_Static;
7761 end Ensure_Expression_Is_SA;
7763 -----------------------
7764 -- Ensure_Type_Is_SA --
7765 -----------------------
7767 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7768 N : Node_Id;
7769 C : Entity_Id;
7771 begin
7772 -- If type is library level, we are all set
7774 if Is_Library_Level_Entity (Typ) then
7775 return;
7776 end if;
7778 -- We are also OK if the type already marked as statically allocated,
7779 -- which means we processed it before.
7781 if Is_Statically_Allocated (Typ) then
7782 return;
7783 end if;
7785 -- Mark type as statically allocated
7787 Set_Is_Statically_Allocated (Typ);
7789 -- Check that it is safe to statically allocate this type
7791 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
7792 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
7793 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
7795 elsif Is_Array_Type (Typ) then
7796 N := First_Index (Typ);
7797 while Present (N) loop
7798 Ensure_Type_Is_SA (Etype (N));
7799 Next_Index (N);
7800 end loop;
7802 Ensure_Type_Is_SA (Component_Type (Typ));
7804 elsif Is_Access_Type (Typ) then
7805 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
7807 declare
7808 F : Entity_Id;
7809 T : constant Entity_Id := Etype (Designated_Type (Typ));
7811 begin
7812 if T /= Standard_Void_Type then
7813 Ensure_Type_Is_SA (T);
7814 end if;
7816 F := First_Formal (Designated_Type (Typ));
7817 while Present (F) loop
7818 Ensure_Type_Is_SA (Etype (F));
7819 Next_Formal (F);
7820 end loop;
7821 end;
7823 else
7824 Ensure_Type_Is_SA (Designated_Type (Typ));
7825 end if;
7827 elsif Is_Record_Type (Typ) then
7828 C := First_Entity (Typ);
7829 while Present (C) loop
7830 if Ekind (C) = E_Discriminant
7831 or else Ekind (C) = E_Component
7832 then
7833 Ensure_Type_Is_SA (Etype (C));
7835 elsif Is_Type (C) then
7836 Ensure_Type_Is_SA (C);
7837 end if;
7839 Next_Entity (C);
7840 end loop;
7842 elsif Ekind (Typ) = E_Subprogram_Type then
7843 Ensure_Type_Is_SA (Etype (Typ));
7845 C := First_Formal (Typ);
7846 while Present (C) loop
7847 Ensure_Type_Is_SA (Etype (C));
7848 Next_Formal (C);
7849 end loop;
7851 else
7852 raise Cannot_Be_Static;
7853 end if;
7854 end Ensure_Type_Is_SA;
7856 -- Start of processing for Freeze_Static_Object
7858 begin
7859 Ensure_Type_Is_SA (Etype (E));
7861 exception
7862 when Cannot_Be_Static =>
7864 -- If the object that cannot be static is imported or exported, then
7865 -- issue an error message saying that this object cannot be imported
7866 -- or exported. If it has an address clause it is an overlay in the
7867 -- current partition and the static requirement is not relevant.
7868 -- Do not issue any error message when ignoring rep clauses.
7870 if Ignore_Rep_Clauses then
7871 null;
7873 elsif Is_Imported (E) then
7874 if No (Address_Clause (E)) then
7875 Error_Msg_N
7876 ("& cannot be imported (local type is not constant)", E);
7877 end if;
7879 -- Otherwise must be exported, something is wrong if compiler
7880 -- is marking something as statically allocated which cannot be).
7882 else pragma Assert (Is_Exported (E));
7883 Error_Msg_N
7884 ("& cannot be exported (local type is not constant)", E);
7885 end if;
7886 end Freeze_Static_Object;
7888 -----------------------
7889 -- Freeze_Subprogram --
7890 -----------------------
7892 procedure Freeze_Subprogram (E : Entity_Id) is
7893 Retype : Entity_Id;
7894 F : Entity_Id;
7896 begin
7897 -- Subprogram may not have an address clause unless it is imported
7899 if Present (Address_Clause (E)) then
7900 if not Is_Imported (E) then
7901 Error_Msg_N
7902 ("address clause can only be given " &
7903 "for imported subprogram",
7904 Name (Address_Clause (E)));
7905 end if;
7906 end if;
7908 -- Reset the Pure indication on an imported subprogram unless an
7909 -- explicit Pure_Function pragma was present or the subprogram is an
7910 -- intrinsic. We do this because otherwise it is an insidious error
7911 -- to call a non-pure function from pure unit and have calls
7912 -- mysteriously optimized away. What happens here is that the Import
7913 -- can bypass the normal check to ensure that pure units call only pure
7914 -- subprograms.
7916 -- The reason for the intrinsic exception is that in general, intrinsic
7917 -- functions (such as shifts) are pure anyway. The only exceptions are
7918 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
7919 -- in any case, so no problem arises.
7921 if Is_Imported (E)
7922 and then Is_Pure (E)
7923 and then not Has_Pragma_Pure_Function (E)
7924 and then not Is_Intrinsic_Subprogram (E)
7925 then
7926 Set_Is_Pure (E, False);
7927 end if;
7929 -- We also reset the Pure indication on a subprogram with an Address
7930 -- parameter, because the parameter may be used as a pointer and the
7931 -- referenced data may change even if the address value does not.
7933 -- Note that if the programmer gave an explicit Pure_Function pragma,
7934 -- then we believe the programmer, and leave the subprogram Pure.
7935 -- We also suppress this check on run-time files.
7937 if Is_Pure (E)
7938 and then Is_Subprogram (E)
7939 and then not Has_Pragma_Pure_Function (E)
7940 and then not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
7941 then
7942 Check_Function_With_Address_Parameter (E);
7943 end if;
7945 -- For non-foreign convention subprograms, this is where we create
7946 -- the extra formals (for accessibility level and constrained bit
7947 -- information). We delay this till the freeze point precisely so
7948 -- that we know the convention.
7950 if not Has_Foreign_Convention (E) then
7951 Create_Extra_Formals (E);
7952 Set_Mechanisms (E);
7954 -- If this is convention Ada and a Valued_Procedure, that's odd
7956 if Ekind (E) = E_Procedure
7957 and then Is_Valued_Procedure (E)
7958 and then Convention (E) = Convention_Ada
7959 and then Warn_On_Export_Import
7960 then
7961 Error_Msg_N
7962 ("??Valued_Procedure has no effect for convention Ada", E);
7963 Set_Is_Valued_Procedure (E, False);
7964 end if;
7966 -- Case of foreign convention
7968 else
7969 Set_Mechanisms (E);
7971 -- For foreign conventions, warn about return of unconstrained array
7973 if Ekind (E) = E_Function then
7974 Retype := Underlying_Type (Etype (E));
7976 -- If no return type, probably some other error, e.g. a
7977 -- missing full declaration, so ignore.
7979 if No (Retype) then
7980 null;
7982 -- If the return type is generic, we have emitted a warning
7983 -- earlier on, and there is nothing else to check here. Specific
7984 -- instantiations may lead to erroneous behavior.
7986 elsif Is_Generic_Type (Etype (E)) then
7987 null;
7989 -- Display warning if returning unconstrained array
7991 elsif Is_Array_Type (Retype)
7992 and then not Is_Constrained (Retype)
7994 -- Check appropriate warning is enabled (should we check for
7995 -- Warnings (Off) on specific entities here, probably so???)
7997 and then Warn_On_Export_Import
7998 then
7999 Error_Msg_N
8000 ("?x?foreign convention function& should not return " &
8001 "unconstrained array", E);
8002 return;
8003 end if;
8004 end if;
8006 -- If any of the formals for an exported foreign convention
8007 -- subprogram have defaults, then emit an appropriate warning since
8008 -- this is odd (default cannot be used from non-Ada code)
8010 if Is_Exported (E) then
8011 F := First_Formal (E);
8012 while Present (F) loop
8013 if Warn_On_Export_Import
8014 and then Present (Default_Value (F))
8015 then
8016 Error_Msg_N
8017 ("?x?parameter cannot be defaulted in non-Ada call",
8018 Default_Value (F));
8019 end if;
8021 Next_Formal (F);
8022 end loop;
8023 end if;
8024 end if;
8026 -- Pragma Inline_Always is disallowed for dispatching subprograms
8027 -- because the address of such subprograms is saved in the dispatch
8028 -- table to support dispatching calls, and dispatching calls cannot
8029 -- be inlined. This is consistent with the restriction against using
8030 -- 'Access or 'Address on an Inline_Always subprogram.
8032 if Is_Dispatching_Operation (E)
8033 and then Has_Pragma_Inline_Always (E)
8034 then
8035 Error_Msg_N
8036 ("pragma Inline_Always not allowed for dispatching subprograms", E);
8037 end if;
8039 -- Because of the implicit representation of inherited predefined
8040 -- operators in the front-end, the overriding status of the operation
8041 -- may be affected when a full view of a type is analyzed, and this is
8042 -- not captured by the analysis of the corresponding type declaration.
8043 -- Therefore the correctness of a not-overriding indicator must be
8044 -- rechecked when the subprogram is frozen.
8046 if Nkind (E) = N_Defining_Operator_Symbol
8047 and then not Error_Posted (Parent (E))
8048 then
8049 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
8050 end if;
8052 if Modify_Tree_For_C
8053 and then Nkind (Parent (E)) = N_Function_Specification
8054 and then Is_Array_Type (Etype (E))
8055 and then Is_Constrained (Etype (E))
8056 and then not Is_Unchecked_Conversion_Instance (E)
8057 and then not Rewritten_For_C (E)
8058 then
8059 Build_Procedure_Form (Unit_Declaration_Node (E));
8060 end if;
8061 end Freeze_Subprogram;
8063 ----------------------
8064 -- Is_Fully_Defined --
8065 ----------------------
8067 function Is_Fully_Defined (T : Entity_Id) return Boolean is
8068 begin
8069 if Ekind (T) = E_Class_Wide_Type then
8070 return Is_Fully_Defined (Etype (T));
8072 elsif Is_Array_Type (T) then
8073 return Is_Fully_Defined (Component_Type (T));
8075 elsif Is_Record_Type (T)
8076 and not Is_Private_Type (T)
8077 then
8078 -- Verify that the record type has no components with private types
8079 -- without completion.
8081 declare
8082 Comp : Entity_Id;
8084 begin
8085 Comp := First_Component (T);
8086 while Present (Comp) loop
8087 if not Is_Fully_Defined (Etype (Comp)) then
8088 return False;
8089 end if;
8091 Next_Component (Comp);
8092 end loop;
8093 return True;
8094 end;
8096 -- For the designated type of an access to subprogram, all types in
8097 -- the profile must be fully defined.
8099 elsif Ekind (T) = E_Subprogram_Type then
8100 declare
8101 F : Entity_Id;
8103 begin
8104 F := First_Formal (T);
8105 while Present (F) loop
8106 if not Is_Fully_Defined (Etype (F)) then
8107 return False;
8108 end if;
8110 Next_Formal (F);
8111 end loop;
8113 return Is_Fully_Defined (Etype (T));
8114 end;
8116 else
8117 return not Is_Private_Type (T)
8118 or else Present (Full_View (Base_Type (T)));
8119 end if;
8120 end Is_Fully_Defined;
8122 ---------------------------------
8123 -- Process_Default_Expressions --
8124 ---------------------------------
8126 procedure Process_Default_Expressions
8127 (E : Entity_Id;
8128 After : in out Node_Id)
8130 Loc : constant Source_Ptr := Sloc (E);
8131 Dbody : Node_Id;
8132 Formal : Node_Id;
8133 Dcopy : Node_Id;
8134 Dnam : Entity_Id;
8136 begin
8137 Set_Default_Expressions_Processed (E);
8139 -- A subprogram instance and its associated anonymous subprogram share
8140 -- their signature. The default expression functions are defined in the
8141 -- wrapper packages for the anonymous subprogram, and should not be
8142 -- generated again for the instance.
8144 if Is_Generic_Instance (E)
8145 and then Present (Alias (E))
8146 and then Default_Expressions_Processed (Alias (E))
8147 then
8148 return;
8149 end if;
8151 Formal := First_Formal (E);
8152 while Present (Formal) loop
8153 if Present (Default_Value (Formal)) then
8155 -- We work with a copy of the default expression because we
8156 -- do not want to disturb the original, since this would mess
8157 -- up the conformance checking.
8159 Dcopy := New_Copy_Tree (Default_Value (Formal));
8161 -- The analysis of the expression may generate insert actions,
8162 -- which of course must not be executed. We wrap those actions
8163 -- in a procedure that is not called, and later on eliminated.
8164 -- The following cases have no side-effects, and are analyzed
8165 -- directly.
8167 if Nkind (Dcopy) = N_Identifier
8168 or else Nkind_In (Dcopy, N_Expanded_Name,
8169 N_Integer_Literal,
8170 N_Character_Literal,
8171 N_String_Literal,
8172 N_Real_Literal)
8173 or else (Nkind (Dcopy) = N_Attribute_Reference
8174 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
8175 or else Known_Null (Dcopy)
8176 then
8177 -- If there is no default function, we must still do a full
8178 -- analyze call on the default value, to ensure that all error
8179 -- checks are performed, e.g. those associated with static
8180 -- evaluation. Note: this branch will always be taken if the
8181 -- analyzer is turned off (but we still need the error checks).
8183 -- Note: the setting of parent here is to meet the requirement
8184 -- that we can only analyze the expression while attached to
8185 -- the tree. Really the requirement is that the parent chain
8186 -- be set, we don't actually need to be in the tree.
8188 Set_Parent (Dcopy, Declaration_Node (Formal));
8189 Analyze (Dcopy);
8191 -- Default expressions are resolved with their own type if the
8192 -- context is generic, to avoid anomalies with private types.
8194 if Ekind (Scope (E)) = E_Generic_Package then
8195 Resolve (Dcopy);
8196 else
8197 Resolve (Dcopy, Etype (Formal));
8198 end if;
8200 -- If that resolved expression will raise constraint error,
8201 -- then flag the default value as raising constraint error.
8202 -- This allows a proper error message on the calls.
8204 if Raises_Constraint_Error (Dcopy) then
8205 Set_Raises_Constraint_Error (Default_Value (Formal));
8206 end if;
8208 -- If the default is a parameterless call, we use the name of
8209 -- the called function directly, and there is no body to build.
8211 elsif Nkind (Dcopy) = N_Function_Call
8212 and then No (Parameter_Associations (Dcopy))
8213 then
8214 null;
8216 -- Else construct and analyze the body of a wrapper procedure
8217 -- that contains an object declaration to hold the expression.
8218 -- Given that this is done only to complete the analysis, it is
8219 -- simpler to build a procedure than a function which might
8220 -- involve secondary stack expansion.
8222 else
8223 Dnam := Make_Temporary (Loc, 'D');
8225 Dbody :=
8226 Make_Subprogram_Body (Loc,
8227 Specification =>
8228 Make_Procedure_Specification (Loc,
8229 Defining_Unit_Name => Dnam),
8231 Declarations => New_List (
8232 Make_Object_Declaration (Loc,
8233 Defining_Identifier => Make_Temporary (Loc, 'T'),
8234 Object_Definition =>
8235 New_Occurrence_Of (Etype (Formal), Loc),
8236 Expression => New_Copy_Tree (Dcopy))),
8238 Handled_Statement_Sequence =>
8239 Make_Handled_Sequence_Of_Statements (Loc,
8240 Statements => Empty_List));
8242 Set_Scope (Dnam, Scope (E));
8243 Set_Assignment_OK (First (Declarations (Dbody)));
8244 Set_Is_Eliminated (Dnam);
8245 Insert_After (After, Dbody);
8246 Analyze (Dbody);
8247 After := Dbody;
8248 end if;
8249 end if;
8251 Next_Formal (Formal);
8252 end loop;
8253 end Process_Default_Expressions;
8255 ----------------------------------------
8256 -- Set_Component_Alignment_If_Not_Set --
8257 ----------------------------------------
8259 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
8260 begin
8261 -- Ignore if not base type, subtypes don't need anything
8263 if Typ /= Base_Type (Typ) then
8264 return;
8265 end if;
8267 -- Do not override existing representation
8269 if Is_Packed (Typ) then
8270 return;
8272 elsif Has_Specified_Layout (Typ) then
8273 return;
8275 elsif Component_Alignment (Typ) /= Calign_Default then
8276 return;
8278 else
8279 Set_Component_Alignment
8280 (Typ, Scope_Stack.Table
8281 (Scope_Stack.Last).Component_Alignment_Default);
8282 end if;
8283 end Set_Component_Alignment_If_Not_Set;
8285 --------------------------
8286 -- Set_SSO_From_Default --
8287 --------------------------
8289 procedure Set_SSO_From_Default (T : Entity_Id) is
8290 Reversed : Boolean;
8292 begin
8293 -- Set default SSO for an array or record base type, except in case of
8294 -- a type extension (which always inherits the SSO of its parent type).
8296 if Is_Base_Type (T)
8297 and then (Is_Array_Type (T)
8298 or else (Is_Record_Type (T)
8299 and then not (Is_Tagged_Type (T)
8300 and then Is_Derived_Type (T))))
8301 then
8302 Reversed :=
8303 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
8304 or else
8305 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
8307 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
8309 -- For a record type, if bit order is specified explicitly,
8310 -- then do not set SSO from default if not consistent. Note that
8311 -- we do not want to look at a Bit_Order attribute definition
8312 -- for a parent: if we were to inherit Bit_Order, then both
8313 -- SSO_Set_*_By_Default flags would have been cleared already
8314 -- (by Inherit_Aspects_At_Freeze_Point).
8316 and then not
8317 (Is_Record_Type (T)
8318 and then
8319 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
8320 and then Reverse_Bit_Order (T) /= Reversed)
8321 then
8322 -- If flags cause reverse storage order, then set the result. Note
8323 -- that we would have ignored the pragma setting the non default
8324 -- storage order in any case, hence the assertion at this point.
8326 pragma Assert
8327 (not Reversed or else Support_Nondefault_SSO_On_Target);
8329 Set_Reverse_Storage_Order (T, Reversed);
8331 -- For a record type, also set reversed bit order. Note: if a bit
8332 -- order has been specified explicitly, then this is a no-op.
8334 if Is_Record_Type (T) then
8335 Set_Reverse_Bit_Order (T, Reversed);
8336 end if;
8337 end if;
8338 end if;
8339 end Set_SSO_From_Default;
8341 ------------------
8342 -- Undelay_Type --
8343 ------------------
8345 procedure Undelay_Type (T : Entity_Id) is
8346 begin
8347 Set_Has_Delayed_Freeze (T, False);
8348 Set_Freeze_Node (T, Empty);
8350 -- Since we don't want T to have a Freeze_Node, we don't want its
8351 -- Full_View or Corresponding_Record_Type to have one either.
8353 -- ??? Fundamentally, this whole handling is unpleasant. What we really
8354 -- want is to be sure that for an Itype that's part of record R and is a
8355 -- subtype of type T, that it's frozen after the later of the freeze
8356 -- points of R and T. We have no way of doing that directly, so what we
8357 -- do is force most such Itypes to be frozen as part of freezing R via
8358 -- this procedure and only delay the ones that need to be delayed
8359 -- (mostly the designated types of access types that are defined as part
8360 -- of the record).
8362 if Is_Private_Type (T)
8363 and then Present (Full_View (T))
8364 and then Is_Itype (Full_View (T))
8365 and then Is_Record_Type (Scope (Full_View (T)))
8366 then
8367 Undelay_Type (Full_View (T));
8368 end if;
8370 if Is_Concurrent_Type (T)
8371 and then Present (Corresponding_Record_Type (T))
8372 and then Is_Itype (Corresponding_Record_Type (T))
8373 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
8374 then
8375 Undelay_Type (Corresponding_Record_Type (T));
8376 end if;
8377 end Undelay_Type;
8379 ------------------
8380 -- Warn_Overlay --
8381 ------------------
8383 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Entity_Id) is
8384 Ent : constant Entity_Id := Entity (Nam);
8385 -- The object to which the address clause applies
8387 Init : Node_Id;
8388 Old : Entity_Id := Empty;
8389 Decl : Node_Id;
8391 begin
8392 -- No warning if address clause overlay warnings are off
8394 if not Address_Clause_Overlay_Warnings then
8395 return;
8396 end if;
8398 -- No warning if there is an explicit initialization
8400 Init := Original_Node (Expression (Declaration_Node (Ent)));
8402 if Present (Init) and then Comes_From_Source (Init) then
8403 return;
8404 end if;
8406 -- We only give the warning for non-imported entities of a type for
8407 -- which a non-null base init proc is defined, or for objects of access
8408 -- types with implicit null initialization, or when Normalize_Scalars
8409 -- applies and the type is scalar or a string type (the latter being
8410 -- tested for because predefined String types are initialized by inline
8411 -- code rather than by an init_proc). Note that we do not give the
8412 -- warning for Initialize_Scalars, since we suppressed initialization
8413 -- in this case. Also, do not warn if Suppress_Initialization is set.
8415 if Present (Expr)
8416 and then not Is_Imported (Ent)
8417 and then not Initialization_Suppressed (Typ)
8418 and then (Has_Non_Null_Base_Init_Proc (Typ)
8419 or else Is_Access_Type (Typ)
8420 or else (Normalize_Scalars
8421 and then (Is_Scalar_Type (Typ)
8422 or else Is_String_Type (Typ))))
8423 then
8424 if Nkind (Expr) = N_Attribute_Reference
8425 and then Is_Entity_Name (Prefix (Expr))
8426 then
8427 Old := Entity (Prefix (Expr));
8429 elsif Is_Entity_Name (Expr)
8430 and then Ekind (Entity (Expr)) = E_Constant
8431 then
8432 Decl := Declaration_Node (Entity (Expr));
8434 if Nkind (Decl) = N_Object_Declaration
8435 and then Present (Expression (Decl))
8436 and then Nkind (Expression (Decl)) = N_Attribute_Reference
8437 and then Is_Entity_Name (Prefix (Expression (Decl)))
8438 then
8439 Old := Entity (Prefix (Expression (Decl)));
8441 elsif Nkind (Expr) = N_Function_Call then
8442 return;
8443 end if;
8445 -- A function call (most likely to To_Address) is probably not an
8446 -- overlay, so skip warning. Ditto if the function call was inlined
8447 -- and transformed into an entity.
8449 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
8450 return;
8451 end if;
8453 -- If a pragma Import follows, we assume that it is for the current
8454 -- target of the address clause, and skip the warning. There may be
8455 -- a source pragma or an aspect that specifies import and generates
8456 -- the corresponding pragma. These will indicate that the entity is
8457 -- imported and that is checked above so that the spurious warning
8458 -- (generated when the entity is frozen) will be suppressed. The
8459 -- pragma may be attached to the aspect, so it is not yet a list
8460 -- member.
8462 if Is_List_Member (Parent (Expr)) then
8463 Decl := Next (Parent (Expr));
8465 if Present (Decl)
8466 and then Nkind (Decl) = N_Pragma
8467 and then Pragma_Name (Decl) = Name_Import
8468 then
8469 return;
8470 end if;
8471 end if;
8473 -- Otherwise give warning message
8475 if Present (Old) then
8476 Error_Msg_Node_2 := Old;
8477 Error_Msg_N
8478 ("default initialization of & may modify &??",
8479 Nam);
8480 else
8481 Error_Msg_N
8482 ("default initialization of & may modify overlaid storage??",
8483 Nam);
8484 end if;
8486 -- Add friendly warning if initialization comes from a packed array
8487 -- component.
8489 if Is_Record_Type (Typ) then
8490 declare
8491 Comp : Entity_Id;
8493 begin
8494 Comp := First_Component (Typ);
8495 while Present (Comp) loop
8496 if Nkind (Parent (Comp)) = N_Component_Declaration
8497 and then Present (Expression (Parent (Comp)))
8498 then
8499 exit;
8500 elsif Is_Array_Type (Etype (Comp))
8501 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
8502 then
8503 Error_Msg_NE
8504 ("\packed array component& " &
8505 "will be initialized to zero??",
8506 Nam, Comp);
8507 exit;
8508 else
8509 Next_Component (Comp);
8510 end if;
8511 end loop;
8512 end;
8513 end if;
8515 Error_Msg_N
8516 ("\use pragma Import for & to " &
8517 "suppress initialization (RM B.1(24))??",
8518 Nam);
8519 end if;
8520 end Warn_Overlay;
8522 end Freeze;