2016-06-16 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / ada / freeze.adb
blob046fd1516a8a9976e0522d96754ad91625c2374d
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 Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Pakd; use Exp_Pakd;
37 with Exp_Util; use Exp_Util;
38 with Exp_Tss; use Exp_Tss;
39 with Fname; use Fname;
40 with Ghost; use Ghost;
41 with Layout; use Layout;
42 with Lib; use Lib;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Mech; use Sem_Mech;
59 with Sem_Prag; use Sem_Prag;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sinfo; use Sinfo;
63 with Snames; use Snames;
64 with Stand; use Stand;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uintp; use Uintp;
69 with Urealp; use Urealp;
70 with Warnsw; use Warnsw;
72 package body Freeze is
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
79 -- Typ is a type that is being frozen. If no size clause is given,
80 -- but a default Esize has been computed, then this default Esize is
81 -- adjusted up if necessary to be consistent with a given alignment,
82 -- but never to a value greater than Long_Long_Integer'Size. This
83 -- is used for all discrete types and for fixed-point types.
85 procedure Build_And_Analyze_Renamed_Body
86 (Decl : Node_Id;
87 New_S : Entity_Id;
88 After : in out Node_Id);
89 -- Build body for a renaming declaration, insert in tree and analyze
91 procedure Check_Address_Clause (E : Entity_Id);
92 -- Apply legality checks to address clauses for object declarations,
93 -- at the point the object is frozen. Also ensure any initialization is
94 -- performed only after the object has been frozen.
96 procedure Check_Component_Storage_Order
97 (Encl_Type : Entity_Id;
98 Comp : Entity_Id;
99 ADC : Node_Id;
100 Comp_ADC_Present : out Boolean);
101 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
102 -- clause, verify that the component type has an explicit and compatible
103 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
104 -- entity of the component under consideration. For an Encl_Type that
105 -- does not have a Scalar_Storage_Order attribute definition clause,
106 -- verify that the component also does not have such a clause.
107 -- ADC is the attribute definition clause if present (or Empty). On return,
108 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
109 -- attribute definition clause.
111 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
112 -- When an expression function is frozen by a use of it, the expression
113 -- itself is frozen. Check that the expression does not include references
114 -- to deferred constants without completion. We report this at the freeze
115 -- point of the function, to provide a better error message.
117 -- In most cases the expression itself is frozen by the time the function
118 -- itself is frozen, because the formals will be frozen by then. However,
119 -- Attribute references to outer types are freeze points for those types;
120 -- this routine generates the required freeze nodes for them.
122 procedure Check_Strict_Alignment (E : Entity_Id);
123 -- E is a base type. If E is tagged or has a component that is aliased
124 -- or tagged or contains something this is aliased or tagged, set
125 -- Strict_Alignment.
127 procedure Check_Unsigned_Type (E : Entity_Id);
128 pragma Inline (Check_Unsigned_Type);
129 -- If E is a fixed-point or discrete type, then all the necessary work
130 -- to freeze it is completed except for possible setting of the flag
131 -- Is_Unsigned_Type, which is done by this procedure. The call has no
132 -- effect if the entity E is not a discrete or fixed-point type.
134 procedure Freeze_And_Append
135 (Ent : Entity_Id;
136 N : Node_Id;
137 Result : in out List_Id);
138 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
139 -- nodes to Result, modifying Result from No_List if necessary. N has
140 -- the same usage as in Freeze_Entity.
142 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
143 -- Freeze enumeration type. The Esize field is set as processing
144 -- proceeds (i.e. set by default when the type is declared and then
145 -- adjusted by rep clauses. What this procedure does is to make sure
146 -- that if a foreign convention is specified, and no specific size
147 -- is given, then the size must be at least Integer'Size.
149 procedure Freeze_Static_Object (E : Entity_Id);
150 -- If an object is frozen which has Is_Statically_Allocated set, then
151 -- all referenced types must also be marked with this flag. This routine
152 -- is in charge of meeting this requirement for the object entity E.
154 procedure Freeze_Subprogram (E : Entity_Id);
155 -- Perform freezing actions for a subprogram (create extra formals,
156 -- and set proper default mechanism values). Note that this routine
157 -- is not called for internal subprograms, for which neither of these
158 -- actions is needed (or desirable, we do not want for example to have
159 -- these extra formals present in initialization procedures, where they
160 -- would serve no purpose). In this call E is either a subprogram or
161 -- a subprogram type (i.e. an access to a subprogram).
163 function Is_Fully_Defined (T : Entity_Id) return Boolean;
164 -- True if T is not private and has no private components, or has a full
165 -- view. Used to determine whether the designated type of an access type
166 -- should be frozen when the access type is frozen. This is done when an
167 -- allocator is frozen, or an expression that may involve attributes of
168 -- the designated type. Otherwise freezing the access type does not freeze
169 -- the designated type.
171 procedure Process_Default_Expressions
172 (E : Entity_Id;
173 After : in out Node_Id);
174 -- This procedure is called for each subprogram to complete processing of
175 -- default expressions at the point where all types are known to be frozen.
176 -- The expressions must be analyzed in full, to make sure that all error
177 -- processing is done (they have only been pre-analyzed). If the expression
178 -- is not an entity or literal, its analysis may generate code which must
179 -- not be executed. In that case we build a function body to hold that
180 -- code. This wrapper function serves no other purpose (it used to be
181 -- called to evaluate the default, but now the default is inlined at each
182 -- point of call).
184 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
185 -- Typ is a record or array type that is being frozen. This routine sets
186 -- the default component alignment from the scope stack values if the
187 -- alignment is otherwise not specified.
189 procedure Check_Debug_Info_Needed (T : Entity_Id);
190 -- As each entity is frozen, this routine is called to deal with the
191 -- setting of Debug_Info_Needed for the entity. This flag is set if
192 -- the entity comes from source, or if we are in Debug_Generated_Code
193 -- mode or if the -gnatdV debug flag is set. However, it never sets
194 -- the flag if Debug_Info_Off is set. This procedure also ensures that
195 -- subsidiary entities have the flag set as required.
197 procedure Set_SSO_From_Default (T : Entity_Id);
198 -- T is a record or array type that is being frozen. If it is a base type,
199 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
200 -- will be set appropriately. Note that an explicit occurrence of aspect
201 -- Scalar_Storage_Order or an explicit setting of this aspect with an
202 -- attribute definition clause occurs, then these two flags are reset in
203 -- any case, so call will have no effect.
205 procedure Undelay_Type (T : Entity_Id);
206 -- T is a type of a component that we know to be an Itype. We don't want
207 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
208 -- Full_View or Corresponding_Record_Type.
210 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
211 -- Expr is the expression for an address clause for entity Nam whose type
212 -- is Typ. If Typ has a default initialization, and there is no explicit
213 -- initialization in the source declaration, check whether the address
214 -- clause might cause overlaying of an entity, and emit a warning on the
215 -- side effect that the initialization will cause.
217 -------------------------------
218 -- Adjust_Esize_For_Alignment --
219 -------------------------------
221 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
222 Align : Uint;
224 begin
225 if Known_Esize (Typ) and then Known_Alignment (Typ) then
226 Align := Alignment_In_Bits (Typ);
228 if Align > Esize (Typ)
229 and then Align <= Standard_Long_Long_Integer_Size
230 then
231 Set_Esize (Typ, Align);
232 end if;
233 end if;
234 end Adjust_Esize_For_Alignment;
236 ------------------------------------
237 -- Build_And_Analyze_Renamed_Body --
238 ------------------------------------
240 procedure Build_And_Analyze_Renamed_Body
241 (Decl : Node_Id;
242 New_S : Entity_Id;
243 After : in out Node_Id)
245 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
246 Ent : constant Entity_Id := Defining_Entity (Decl);
247 Body_Node : Node_Id;
248 Renamed_Subp : Entity_Id;
250 begin
251 -- If the renamed subprogram is intrinsic, there is no need for a
252 -- wrapper body: we set the alias that will be called and expanded which
253 -- completes the declaration. This transformation is only legal if the
254 -- renamed entity has already been elaborated.
256 -- Note that it is legal for a renaming_as_body to rename an intrinsic
257 -- subprogram, as long as the renaming occurs before the new entity
258 -- is frozen (RM 8.5.4 (5)).
260 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
261 and then Is_Entity_Name (Name (Body_Decl))
262 then
263 Renamed_Subp := Entity (Name (Body_Decl));
264 else
265 Renamed_Subp := Empty;
266 end if;
268 if Present (Renamed_Subp)
269 and then Is_Intrinsic_Subprogram (Renamed_Subp)
270 and then
271 (not In_Same_Source_Unit (Renamed_Subp, Ent)
272 or else Sloc (Renamed_Subp) < Sloc (Ent))
274 -- We can make the renaming entity intrinsic if the renamed function
275 -- has an interface name, or if it is one of the shift/rotate
276 -- operations known to the compiler.
278 and then
279 (Present (Interface_Name (Renamed_Subp))
280 or else Nam_In (Chars (Renamed_Subp), Name_Rotate_Left,
281 Name_Rotate_Right,
282 Name_Shift_Left,
283 Name_Shift_Right,
284 Name_Shift_Right_Arithmetic))
285 then
286 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
288 if Present (Alias (Renamed_Subp)) then
289 Set_Alias (Ent, Alias (Renamed_Subp));
290 else
291 Set_Alias (Ent, Renamed_Subp);
292 end if;
294 Set_Is_Intrinsic_Subprogram (Ent);
295 Set_Has_Completion (Ent);
297 else
298 Body_Node := Build_Renamed_Body (Decl, New_S);
299 Insert_After (After, Body_Node);
300 Mark_Rewrite_Insertion (Body_Node);
301 Analyze (Body_Node);
302 After := Body_Node;
303 end if;
304 end Build_And_Analyze_Renamed_Body;
306 ------------------------
307 -- Build_Renamed_Body --
308 ------------------------
310 function Build_Renamed_Body
311 (Decl : Node_Id;
312 New_S : Entity_Id) return Node_Id
314 Loc : constant Source_Ptr := Sloc (New_S);
315 -- We use for the source location of the renamed body, the location of
316 -- the spec entity. It might seem more natural to use the location of
317 -- the renaming declaration itself, but that would be wrong, since then
318 -- the body we create would look as though it was created far too late,
319 -- and this could cause problems with elaboration order analysis,
320 -- particularly in connection with instantiations.
322 N : constant Node_Id := Unit_Declaration_Node (New_S);
323 Nam : constant Node_Id := Name (N);
324 Old_S : Entity_Id;
325 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
326 Actuals : List_Id := No_List;
327 Call_Node : Node_Id;
328 Call_Name : Node_Id;
329 Body_Node : Node_Id;
330 Formal : Entity_Id;
331 O_Formal : Entity_Id;
332 Param_Spec : Node_Id;
334 Pref : Node_Id := Empty;
335 -- If the renamed entity is a primitive operation given in prefix form,
336 -- the prefix is the target object and it has to be added as the first
337 -- actual in the generated call.
339 begin
340 -- Determine the entity being renamed, which is the target of the call
341 -- statement. If the name is an explicit dereference, this is a renaming
342 -- of a subprogram type rather than a subprogram. The name itself is
343 -- fully analyzed.
345 if Nkind (Nam) = N_Selected_Component then
346 Old_S := Entity (Selector_Name (Nam));
348 elsif Nkind (Nam) = N_Explicit_Dereference then
349 Old_S := Etype (Nam);
351 elsif Nkind (Nam) = N_Indexed_Component then
352 if Is_Entity_Name (Prefix (Nam)) then
353 Old_S := Entity (Prefix (Nam));
354 else
355 Old_S := Entity (Selector_Name (Prefix (Nam)));
356 end if;
358 elsif Nkind (Nam) = N_Character_Literal then
359 Old_S := Etype (New_S);
361 else
362 Old_S := Entity (Nam);
363 end if;
365 if Is_Entity_Name (Nam) then
367 -- If the renamed entity is a predefined operator, retain full name
368 -- to ensure its visibility.
370 if Ekind (Old_S) = E_Operator
371 and then Nkind (Nam) = N_Expanded_Name
372 then
373 Call_Name := New_Copy (Name (N));
374 else
375 Call_Name := New_Occurrence_Of (Old_S, Loc);
376 end if;
378 else
379 if Nkind (Nam) = N_Selected_Component
380 and then Present (First_Formal (Old_S))
381 and then
382 (Is_Controlling_Formal (First_Formal (Old_S))
383 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
384 then
386 -- Retrieve the target object, to be added as a first actual
387 -- in the call.
389 Call_Name := New_Occurrence_Of (Old_S, Loc);
390 Pref := Prefix (Nam);
392 else
393 Call_Name := New_Copy (Name (N));
394 end if;
396 -- Original name may have been overloaded, but is fully resolved now
398 Set_Is_Overloaded (Call_Name, False);
399 end if;
401 -- For simple renamings, subsequent calls can be expanded directly as
402 -- calls to the renamed entity. The body must be generated in any case
403 -- for calls that may appear elsewhere. This is not done in the case
404 -- where the subprogram is an instantiation because the actual proper
405 -- body has not been built yet.
407 if Ekind_In (Old_S, E_Function, E_Procedure)
408 and then Nkind (Decl) = N_Subprogram_Declaration
409 and then not Is_Generic_Instance (Old_S)
410 then
411 Set_Body_To_Inline (Decl, Old_S);
412 end if;
414 -- Check whether the return type is a limited view. If the subprogram
415 -- is already frozen the generated body may have a non-limited view
416 -- of the type, that must be used, because it is the one in the spec
417 -- of the renaming declaration.
419 if Ekind (Old_S) = E_Function
420 and then Is_Entity_Name (Result_Definition (Spec))
421 then
422 declare
423 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
424 begin
425 if Has_Non_Limited_View (Ret_Type) then
426 Set_Result_Definition
427 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
428 end if;
429 end;
430 end if;
432 -- The body generated for this renaming is an internal artifact, and
433 -- does not constitute a freeze point for the called entity.
435 Set_Must_Not_Freeze (Call_Name);
437 Formal := First_Formal (Defining_Entity (Decl));
439 if Present (Pref) then
440 declare
441 Pref_Type : constant Entity_Id := Etype (Pref);
442 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
444 begin
445 -- The controlling formal may be an access parameter, or the
446 -- actual may be an access value, so adjust accordingly.
448 if Is_Access_Type (Pref_Type)
449 and then not Is_Access_Type (Form_Type)
450 then
451 Actuals := New_List
452 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
454 elsif Is_Access_Type (Form_Type)
455 and then not Is_Access_Type (Pref)
456 then
457 Actuals :=
458 New_List (
459 Make_Attribute_Reference (Loc,
460 Attribute_Name => Name_Access,
461 Prefix => Relocate_Node (Pref)));
462 else
463 Actuals := New_List (Pref);
464 end if;
465 end;
467 elsif Present (Formal) then
468 Actuals := New_List;
470 else
471 Actuals := No_List;
472 end if;
474 if Present (Formal) then
475 while Present (Formal) loop
476 Append (New_Occurrence_Of (Formal, Loc), Actuals);
477 Next_Formal (Formal);
478 end loop;
479 end if;
481 -- If the renamed entity is an entry, inherit its profile. For other
482 -- renamings as bodies, both profiles must be subtype conformant, so it
483 -- is not necessary to replace the profile given in the declaration.
484 -- However, default values that are aggregates are rewritten when
485 -- partially analyzed, so we recover the original aggregate to insure
486 -- that subsequent conformity checking works. Similarly, if the default
487 -- expression was constant-folded, recover the original expression.
489 Formal := First_Formal (Defining_Entity (Decl));
491 if Present (Formal) then
492 O_Formal := First_Formal (Old_S);
493 Param_Spec := First (Parameter_Specifications (Spec));
494 while Present (Formal) loop
495 if Is_Entry (Old_S) then
496 if Nkind (Parameter_Type (Param_Spec)) /=
497 N_Access_Definition
498 then
499 Set_Etype (Formal, Etype (O_Formal));
500 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
501 end if;
503 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
504 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
505 Nkind (Default_Value (O_Formal))
506 then
507 Set_Expression (Param_Spec,
508 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
509 end if;
511 Next_Formal (Formal);
512 Next_Formal (O_Formal);
513 Next (Param_Spec);
514 end loop;
515 end if;
517 -- If the renamed entity is a function, the generated body contains a
518 -- return statement. Otherwise, build a procedure call. If the entity is
519 -- an entry, subsequent analysis of the call will transform it into the
520 -- proper entry or protected operation call. If the renamed entity is
521 -- a character literal, return it directly.
523 if Ekind (Old_S) = E_Function
524 or else Ekind (Old_S) = E_Operator
525 or else (Ekind (Old_S) = E_Subprogram_Type
526 and then Etype (Old_S) /= Standard_Void_Type)
527 then
528 Call_Node :=
529 Make_Simple_Return_Statement (Loc,
530 Expression =>
531 Make_Function_Call (Loc,
532 Name => Call_Name,
533 Parameter_Associations => Actuals));
535 elsif Ekind (Old_S) = E_Enumeration_Literal then
536 Call_Node :=
537 Make_Simple_Return_Statement (Loc,
538 Expression => New_Occurrence_Of (Old_S, Loc));
540 elsif Nkind (Nam) = N_Character_Literal then
541 Call_Node :=
542 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
544 else
545 Call_Node :=
546 Make_Procedure_Call_Statement (Loc,
547 Name => Call_Name,
548 Parameter_Associations => Actuals);
549 end if;
551 -- Create entities for subprogram body and formals
553 Set_Defining_Unit_Name (Spec,
554 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
556 Param_Spec := First (Parameter_Specifications (Spec));
557 while Present (Param_Spec) loop
558 Set_Defining_Identifier (Param_Spec,
559 Make_Defining_Identifier (Loc,
560 Chars => Chars (Defining_Identifier (Param_Spec))));
561 Next (Param_Spec);
562 end loop;
564 Body_Node :=
565 Make_Subprogram_Body (Loc,
566 Specification => Spec,
567 Declarations => New_List,
568 Handled_Statement_Sequence =>
569 Make_Handled_Sequence_Of_Statements (Loc,
570 Statements => New_List (Call_Node)));
572 if Nkind (Decl) /= N_Subprogram_Declaration then
573 Rewrite (N,
574 Make_Subprogram_Declaration (Loc,
575 Specification => Specification (N)));
576 end if;
578 -- Link the body to the entity whose declaration it completes. If
579 -- the body is analyzed when the renamed entity is frozen, it may
580 -- be necessary to restore the proper scope (see package Exp_Ch13).
582 if Nkind (N) = N_Subprogram_Renaming_Declaration
583 and then Present (Corresponding_Spec (N))
584 then
585 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
586 else
587 Set_Corresponding_Spec (Body_Node, New_S);
588 end if;
590 return Body_Node;
591 end Build_Renamed_Body;
593 --------------------------
594 -- Check_Address_Clause --
595 --------------------------
597 procedure Check_Address_Clause (E : Entity_Id) is
598 Addr : constant Node_Id := Address_Clause (E);
599 Typ : constant Entity_Id := Etype (E);
600 Decl : Node_Id;
601 Expr : Node_Id;
602 Init : Node_Id;
603 Lhs : Node_Id;
604 Tag_Assign : Node_Id;
606 begin
607 if Present (Addr) then
609 -- For a deferred constant, the initialization value is on full view
611 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
612 Decl := Declaration_Node (Full_View (E));
613 else
614 Decl := Declaration_Node (E);
615 end if;
617 Expr := Expression (Addr);
619 if Needs_Constant_Address (Decl, Typ) then
620 Check_Constant_Address_Clause (Expr, E);
622 -- Has_Delayed_Freeze was set on E when the address clause was
623 -- analyzed, and must remain set because we want the address
624 -- clause to be elaborated only after any entity it references
625 -- has been elaborated.
626 end if;
628 -- If Rep_Clauses are to be ignored, remove address clause from
629 -- list attached to entity, because it may be illegal for gigi,
630 -- for example by breaking order of elaboration..
632 if Ignore_Rep_Clauses then
633 declare
634 Rep : Node_Id;
636 begin
637 Rep := First_Rep_Item (E);
639 if Rep = Addr then
640 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
642 else
643 while Present (Rep)
644 and then Next_Rep_Item (Rep) /= Addr
645 loop
646 Rep := Next_Rep_Item (Rep);
647 end loop;
648 end if;
650 if Present (Rep) then
651 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
652 end if;
653 end;
655 -- And now remove the address clause
657 Kill_Rep_Clause (Addr);
659 elsif not Error_Posted (Expr)
660 and then not Needs_Finalization (Typ)
661 then
662 Warn_Overlay (Expr, Typ, Name (Addr));
663 end if;
665 Init := Expression (Decl);
667 -- If a variable, or a non-imported constant, overlays a constant
668 -- object and has an initialization value, then the initialization
669 -- may end up writing into read-only memory. Detect the cases of
670 -- statically identical values and remove the initialization. In
671 -- the other cases, give a warning. We will give other warnings
672 -- later for the variable if it is assigned.
674 if (Ekind (E) = E_Variable
675 or else (Ekind (E) = E_Constant
676 and then not Is_Imported (E)))
677 and then Overlays_Constant (E)
678 and then Present (Init)
679 then
680 declare
681 O_Ent : Entity_Id;
682 Off : Boolean;
684 begin
685 Find_Overlaid_Entity (Addr, O_Ent, Off);
687 if Ekind (O_Ent) = E_Constant
688 and then Etype (O_Ent) = Typ
689 and then Present (Constant_Value (O_Ent))
690 and then Compile_Time_Compare
691 (Init,
692 Constant_Value (O_Ent),
693 Assume_Valid => True) = EQ
694 then
695 Set_No_Initialization (Decl);
696 return;
698 elsif Comes_From_Source (Init)
699 and then Address_Clause_Overlay_Warnings
700 then
701 Error_Msg_Sloc := Sloc (Addr);
702 Error_Msg_NE
703 ("??constant& may be modified via address clause#",
704 Decl, O_Ent);
705 end if;
706 end;
707 end if;
709 if Present (Init) then
711 -- Capture initialization value at point of declaration,
712 -- and make explicit assignment legal, because object may
713 -- be a constant.
715 Remove_Side_Effects (Init);
716 Lhs := New_Occurrence_Of (E, Sloc (Decl));
717 Set_Assignment_OK (Lhs);
719 -- Move initialization to freeze actions, once the object has
720 -- been frozen and the address clause alignment check has been
721 -- performed.
723 Append_Freeze_Action (E,
724 Make_Assignment_Statement (Sloc (Decl),
725 Name => Lhs,
726 Expression => Expression (Decl)));
728 Set_No_Initialization (Decl);
730 -- If the objet is tagged, check whether the tag must be
731 -- reassigned explicitly.
733 Tag_Assign := Make_Tag_Assignment (Decl);
734 if Present (Tag_Assign) then
735 Append_Freeze_Action (E, Tag_Assign);
736 end if;
737 end if;
738 end if;
739 end Check_Address_Clause;
741 -----------------------------
742 -- Check_Compile_Time_Size --
743 -----------------------------
745 procedure Check_Compile_Time_Size (T : Entity_Id) is
747 procedure Set_Small_Size (T : Entity_Id; S : Uint);
748 -- Sets the compile time known size (64 bits or less) in the RM_Size
749 -- field of T, checking for a size clause that was given which attempts
750 -- to give a smaller size.
752 function Size_Known (T : Entity_Id) return Boolean;
753 -- Recursive function that does all the work
755 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
756 -- If T is a constrained subtype, its size is not known if any of its
757 -- discriminant constraints is not static and it is not a null record.
758 -- The test is conservative and doesn't check that the components are
759 -- in fact constrained by non-static discriminant values. Could be made
760 -- more precise ???
762 --------------------
763 -- Set_Small_Size --
764 --------------------
766 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
767 begin
768 if S > 64 then
769 return;
771 -- Check for bad size clause given
773 elsif Has_Size_Clause (T) then
774 if RM_Size (T) < S then
775 Error_Msg_Uint_1 := S;
776 Error_Msg_NE
777 ("size for& too small, minimum allowed is ^",
778 Size_Clause (T), T);
779 end if;
781 -- Set size if not set already
783 elsif Unknown_RM_Size (T) then
784 Set_RM_Size (T, S);
785 end if;
786 end Set_Small_Size;
788 ----------------
789 -- Size_Known --
790 ----------------
792 function Size_Known (T : Entity_Id) return Boolean is
793 Index : Entity_Id;
794 Comp : Entity_Id;
795 Ctyp : Entity_Id;
796 Low : Node_Id;
797 High : Node_Id;
799 begin
800 if Size_Known_At_Compile_Time (T) then
801 return True;
803 -- Always True for elementary types, even generic formal elementary
804 -- types. We used to return False in the latter case, but the size
805 -- is known at compile time, even in the template, we just do not
806 -- know the exact size but that's not the point of this routine.
808 elsif Is_Elementary_Type (T) or else Is_Task_Type (T) then
809 return True;
811 -- Array types
813 elsif Is_Array_Type (T) then
815 -- String literals always have known size, and we can set it
817 if Ekind (T) = E_String_Literal_Subtype then
818 Set_Small_Size
819 (T, Component_Size (T) * String_Literal_Length (T));
820 return True;
822 -- Unconstrained types never have known at compile time size
824 elsif not Is_Constrained (T) then
825 return False;
827 -- Don't do any recursion on type with error posted, since we may
828 -- have a malformed type that leads us into a loop.
830 elsif Error_Posted (T) then
831 return False;
833 -- Otherwise if component size unknown, then array size unknown
835 elsif not Size_Known (Component_Type (T)) then
836 return False;
837 end if;
839 -- Check for all indexes static, and also compute possible size
840 -- (in case it is not greater than 64 and may be packable).
842 declare
843 Size : Uint := Component_Size (T);
844 Dim : Uint;
846 begin
847 Index := First_Index (T);
848 while Present (Index) loop
849 if Nkind (Index) = N_Range then
850 Get_Index_Bounds (Index, Low, High);
852 elsif Error_Posted (Scalar_Range (Etype (Index))) then
853 return False;
855 else
856 Low := Type_Low_Bound (Etype (Index));
857 High := Type_High_Bound (Etype (Index));
858 end if;
860 if not Compile_Time_Known_Value (Low)
861 or else not Compile_Time_Known_Value (High)
862 or else Etype (Index) = Any_Type
863 then
864 return False;
866 else
867 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
869 if Dim >= 0 then
870 Size := Size * Dim;
871 else
872 Size := Uint_0;
873 end if;
874 end if;
876 Next_Index (Index);
877 end loop;
879 Set_Small_Size (T, Size);
880 return True;
881 end;
883 -- For non-generic private types, go to underlying type if present
885 elsif Is_Private_Type (T)
886 and then not Is_Generic_Type (T)
887 and then Present (Underlying_Type (T))
888 then
889 -- Don't do any recursion on type with error posted, since we may
890 -- have a malformed type that leads us into a loop.
892 if Error_Posted (T) then
893 return False;
894 else
895 return Size_Known (Underlying_Type (T));
896 end if;
898 -- Record types
900 elsif Is_Record_Type (T) then
902 -- A class-wide type is never considered to have a known size
904 if Is_Class_Wide_Type (T) then
905 return False;
907 -- A subtype of a variant record must not have non-static
908 -- discriminated components.
910 elsif T /= Base_Type (T)
911 and then not Static_Discriminated_Components (T)
912 then
913 return False;
915 -- Don't do any recursion on type with error posted, since we may
916 -- have a malformed type that leads us into a loop.
918 elsif Error_Posted (T) then
919 return False;
920 end if;
922 -- Now look at the components of the record
924 declare
925 -- The following two variables are used to keep track of the
926 -- size of packed records if we can tell the size of the packed
927 -- record in the front end. Packed_Size_Known is True if so far
928 -- we can figure out the size. It is initialized to True for a
929 -- packed record, unless the record has discriminants or atomic
930 -- components or independent components.
932 -- The reason we eliminate the discriminated case is that
933 -- we don't know the way the back end lays out discriminated
934 -- packed records. If Packed_Size_Known is True, then
935 -- Packed_Size is the size in bits so far.
937 Packed_Size_Known : Boolean :=
938 Is_Packed (T)
939 and then not Has_Discriminants (T)
940 and then not Has_Atomic_Components (T)
941 and then not Has_Independent_Components (T);
943 Packed_Size : Uint := Uint_0;
944 -- Size in bits so far
946 begin
947 -- Test for variant part present
949 if Has_Discriminants (T)
950 and then Present (Parent (T))
951 and then Nkind (Parent (T)) = N_Full_Type_Declaration
952 and then Nkind (Type_Definition (Parent (T))) =
953 N_Record_Definition
954 and then not Null_Present (Type_Definition (Parent (T)))
955 and then
956 Present (Variant_Part
957 (Component_List (Type_Definition (Parent (T)))))
958 then
959 -- If variant part is present, and type is unconstrained,
960 -- then we must have defaulted discriminants, or a size
961 -- clause must be present for the type, or else the size
962 -- is definitely not known at compile time.
964 if not Is_Constrained (T)
965 and then
966 No (Discriminant_Default_Value (First_Discriminant (T)))
967 and then Unknown_RM_Size (T)
968 then
969 return False;
970 end if;
971 end if;
973 -- Loop through components
975 Comp := First_Component_Or_Discriminant (T);
976 while Present (Comp) loop
977 Ctyp := Etype (Comp);
979 -- We do not know the packed size if there is a component
980 -- clause present (we possibly could, but this would only
981 -- help in the case of a record with partial rep clauses.
982 -- That's because in the case of full rep clauses, the
983 -- size gets figured out anyway by a different circuit).
985 if Present (Component_Clause (Comp)) then
986 Packed_Size_Known := False;
987 end if;
989 -- We do not know the packed size for an atomic/VFA type
990 -- or component, or an independent type or component, or a
991 -- by-reference type or aliased component (because packing
992 -- does not touch these).
994 if Is_Atomic_Or_VFA (Ctyp)
995 or else Is_Atomic_Or_VFA (Comp)
996 or else Is_Independent (Ctyp)
997 or else Is_Independent (Comp)
998 or else Is_By_Reference_Type (Ctyp)
999 or else Is_Aliased (Comp)
1000 then
1001 Packed_Size_Known := False;
1002 end if;
1004 -- We need to identify a component that is an array where
1005 -- the index type is an enumeration type with non-standard
1006 -- representation, and some bound of the type depends on a
1007 -- discriminant.
1009 -- This is because gigi computes the size by doing a
1010 -- substitution of the appropriate discriminant value in
1011 -- the size expression for the base type, and gigi is not
1012 -- clever enough to evaluate the resulting expression (which
1013 -- involves a call to rep_to_pos) at compile time.
1015 -- It would be nice if gigi would either recognize that
1016 -- this expression can be computed at compile time, or
1017 -- alternatively figured out the size from the subtype
1018 -- directly, where all the information is at hand ???
1020 if Is_Array_Type (Etype (Comp))
1021 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1022 then
1023 declare
1024 Ocomp : constant Entity_Id :=
1025 Original_Record_Component (Comp);
1026 OCtyp : constant Entity_Id := Etype (Ocomp);
1027 Ind : Node_Id;
1028 Indtyp : Entity_Id;
1029 Lo, Hi : Node_Id;
1031 begin
1032 Ind := First_Index (OCtyp);
1033 while Present (Ind) loop
1034 Indtyp := Etype (Ind);
1036 if Is_Enumeration_Type (Indtyp)
1037 and then Has_Non_Standard_Rep (Indtyp)
1038 then
1039 Lo := Type_Low_Bound (Indtyp);
1040 Hi := Type_High_Bound (Indtyp);
1042 if Is_Entity_Name (Lo)
1043 and then Ekind (Entity (Lo)) = E_Discriminant
1044 then
1045 return False;
1047 elsif Is_Entity_Name (Hi)
1048 and then Ekind (Entity (Hi)) = E_Discriminant
1049 then
1050 return False;
1051 end if;
1052 end if;
1054 Next_Index (Ind);
1055 end loop;
1056 end;
1057 end if;
1059 -- Clearly size of record is not known if the size of one of
1060 -- the components is not known.
1062 if not Size_Known (Ctyp) then
1063 return False;
1064 end if;
1066 -- Accumulate packed size if possible
1068 if Packed_Size_Known then
1070 -- We can deal with elementary types, small packed arrays
1071 -- if the representation is a modular type and also small
1072 -- record types (if the size is not greater than 64, but
1073 -- the condition is checked by Set_Small_Size).
1075 if Is_Elementary_Type (Ctyp)
1076 or else (Is_Array_Type (Ctyp)
1077 and then Present
1078 (Packed_Array_Impl_Type (Ctyp))
1079 and then Is_Modular_Integer_Type
1080 (Packed_Array_Impl_Type (Ctyp)))
1081 or else Is_Record_Type (Ctyp)
1082 then
1083 -- If RM_Size is known and static, then we can keep
1084 -- accumulating the packed size.
1086 if Known_Static_RM_Size (Ctyp) then
1088 Packed_Size := Packed_Size + RM_Size (Ctyp);
1090 -- If we have a field whose RM_Size is not known then
1091 -- we can't figure out the packed size here.
1093 else
1094 Packed_Size_Known := False;
1095 end if;
1097 -- For other types we can't figure out the packed size
1099 else
1100 Packed_Size_Known := False;
1101 end if;
1102 end if;
1104 Next_Component_Or_Discriminant (Comp);
1105 end loop;
1107 if Packed_Size_Known then
1108 Set_Small_Size (T, Packed_Size);
1109 end if;
1111 return True;
1112 end;
1114 -- All other cases, size not known at compile time
1116 else
1117 return False;
1118 end if;
1119 end Size_Known;
1121 -------------------------------------
1122 -- Static_Discriminated_Components --
1123 -------------------------------------
1125 function Static_Discriminated_Components
1126 (T : Entity_Id) return Boolean
1128 Constraint : Elmt_Id;
1130 begin
1131 if Has_Discriminants (T)
1132 and then Present (Discriminant_Constraint (T))
1133 and then Present (First_Component (T))
1134 then
1135 Constraint := First_Elmt (Discriminant_Constraint (T));
1136 while Present (Constraint) loop
1137 if not Compile_Time_Known_Value (Node (Constraint)) then
1138 return False;
1139 end if;
1141 Next_Elmt (Constraint);
1142 end loop;
1143 end if;
1145 return True;
1146 end Static_Discriminated_Components;
1148 -- Start of processing for Check_Compile_Time_Size
1150 begin
1151 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1152 end Check_Compile_Time_Size;
1154 -----------------------------------
1155 -- Check_Component_Storage_Order --
1156 -----------------------------------
1158 procedure Check_Component_Storage_Order
1159 (Encl_Type : Entity_Id;
1160 Comp : Entity_Id;
1161 ADC : Node_Id;
1162 Comp_ADC_Present : out Boolean)
1164 Comp_Base : Entity_Id;
1165 Comp_ADC : Node_Id;
1166 Encl_Base : Entity_Id;
1167 Err_Node : Node_Id;
1169 Component_Aliased : Boolean;
1171 Comp_Byte_Aligned : Boolean;
1172 -- Set for the record case, True if Comp starts on a byte boundary
1173 -- (in which case it is allowed to have different storage order).
1175 Comp_SSO_Differs : Boolean;
1176 -- Set True when the component is a nested composite, and it does not
1177 -- have the same scalar storage order as Encl_Type.
1179 begin
1180 -- Record case
1182 if Present (Comp) then
1183 Err_Node := Comp;
1184 Comp_Base := Etype (Comp);
1186 if Is_Tag (Comp) then
1187 Comp_Byte_Aligned := True;
1188 Component_Aliased := False;
1190 else
1191 -- If a component clause is present, check if the component starts
1192 -- on a storage element boundary. Otherwise conservatively assume
1193 -- it does so only in the case where the record is not packed.
1195 if Present (Component_Clause (Comp)) then
1196 Comp_Byte_Aligned :=
1197 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0;
1198 else
1199 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1200 end if;
1202 Component_Aliased := Is_Aliased (Comp);
1203 end if;
1205 -- Array case
1207 else
1208 Err_Node := Encl_Type;
1209 Comp_Base := Component_Type (Encl_Type);
1211 Component_Aliased := Has_Aliased_Components (Encl_Type);
1212 end if;
1214 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1215 -- the attribute definition clause is attached to the first subtype.
1216 -- Also, if the base type is incomplete or private, go to full view
1217 -- if known
1219 Encl_Base := Base_Type (Encl_Type);
1220 if Present (Underlying_Type (Encl_Base)) then
1221 Encl_Base := Underlying_Type (Encl_Base);
1222 end if;
1224 Comp_Base := Base_Type (Comp_Base);
1225 if Present (Underlying_Type (Comp_Base)) then
1226 Comp_Base := Underlying_Type (Comp_Base);
1227 end if;
1229 Comp_ADC :=
1230 Get_Attribute_Definition_Clause
1231 (First_Subtype (Comp_Base), Attribute_Scalar_Storage_Order);
1232 Comp_ADC_Present := Present (Comp_ADC);
1234 -- Case of record or array component: check storage order compatibility.
1235 -- But, if the record has Complex_Representation, then it is treated as
1236 -- a scalar in the back end so the storage order is irrelevant.
1238 if (Is_Record_Type (Comp_Base)
1239 and then not Has_Complex_Representation (Comp_Base))
1240 or else Is_Array_Type (Comp_Base)
1241 then
1242 Comp_SSO_Differs :=
1243 Reverse_Storage_Order (Encl_Base) /=
1244 Reverse_Storage_Order (Comp_Base);
1246 -- Parent and extension must have same storage order
1248 if Present (Comp) and then Chars (Comp) = Name_uParent then
1249 if Comp_SSO_Differs then
1250 Error_Msg_N
1251 ("record extension must have same scalar storage order as "
1252 & "parent", Err_Node);
1253 end if;
1255 -- If component and composite SSO differs, check that component
1256 -- falls on byte boundaries and isn't bit packed.
1258 elsif Comp_SSO_Differs then
1260 -- Component SSO differs from enclosing composite:
1262 -- Reject if component is a bit-packed array, as it is represented
1263 -- as a scalar internally.
1265 if Is_Bit_Packed_Array (Comp_Base) then
1266 Error_Msg_N
1267 ("type of packed component must have same scalar storage "
1268 & "order as enclosing composite", Err_Node);
1270 -- Reject if composite is a bit-packed array, as it is rewritten
1271 -- into an array of scalars.
1273 elsif Is_Bit_Packed_Array (Encl_Base) then
1274 Error_Msg_N
1275 ("type of packed array must have same scalar storage order "
1276 & "as component", Err_Node);
1278 -- Reject if not byte aligned
1280 elsif Is_Record_Type (Encl_Base)
1281 and then not Comp_Byte_Aligned
1282 then
1283 Error_Msg_N
1284 ("type of non-byte-aligned component must have same scalar "
1285 & "storage order as enclosing composite", Err_Node);
1287 -- Warn if specified only for the outer composite
1289 elsif Present (ADC) and then No (Comp_ADC) then
1290 Error_Msg_NE
1291 ("scalar storage order specified for & does not apply to "
1292 & "component?", Err_Node, Encl_Base);
1293 end if;
1294 end if;
1296 -- Enclosing type has explicit SSO: non-composite component must not
1297 -- be aliased.
1299 elsif Present (ADC) and then Component_Aliased then
1300 Error_Msg_N
1301 ("aliased component not permitted for type with explicit "
1302 & "Scalar_Storage_Order", Err_Node);
1303 end if;
1304 end Check_Component_Storage_Order;
1306 -----------------------------
1307 -- Check_Debug_Info_Needed --
1308 -----------------------------
1310 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1311 begin
1312 if Debug_Info_Off (T) then
1313 return;
1315 elsif Comes_From_Source (T)
1316 or else Debug_Generated_Code
1317 or else Debug_Flag_VV
1318 or else Needs_Debug_Info (T)
1319 then
1320 Set_Debug_Info_Needed (T);
1321 end if;
1322 end Check_Debug_Info_Needed;
1324 -------------------------------
1325 -- Check_Expression_Function --
1326 -------------------------------
1328 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1329 Decl : Node_Id;
1331 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1332 -- Function to search for deferred constant
1334 -------------------
1335 -- Find_Constant --
1336 -------------------
1338 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1339 begin
1340 -- When a constant is initialized with the result of a dispatching
1341 -- call, the constant declaration is rewritten as a renaming of the
1342 -- displaced function result. This scenario is not a premature use of
1343 -- a constant even though the Has_Completion flag is not set.
1345 if Is_Entity_Name (Nod)
1346 and then Present (Entity (Nod))
1347 and then Ekind (Entity (Nod)) = E_Constant
1348 and then Scope (Entity (Nod)) = Current_Scope
1349 and then Nkind (Declaration_Node (Entity (Nod))) =
1350 N_Object_Declaration
1351 and then not Is_Imported (Entity (Nod))
1352 and then not Has_Completion (Entity (Nod))
1353 then
1354 Error_Msg_NE
1355 ("premature use of& in call or instance", N, Entity (Nod));
1357 elsif Nkind (Nod) = N_Attribute_Reference then
1358 Analyze (Prefix (Nod));
1360 if Is_Entity_Name (Prefix (Nod))
1361 and then Is_Type (Entity (Prefix (Nod)))
1362 then
1363 Freeze_Before (N, Entity (Prefix (Nod)));
1364 end if;
1365 end if;
1367 return OK;
1368 end Find_Constant;
1370 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1372 -- Start of processing for Check_Expression_Function
1374 begin
1375 Decl := Original_Node (Unit_Declaration_Node (Nam));
1377 if Scope (Nam) = Current_Scope
1378 and then Nkind (Decl) = N_Expression_Function
1379 then
1380 Check_Deferred (Expression (Decl));
1381 end if;
1382 end Check_Expression_Function;
1384 ----------------------------
1385 -- Check_Strict_Alignment --
1386 ----------------------------
1388 procedure Check_Strict_Alignment (E : Entity_Id) is
1389 Comp : Entity_Id;
1391 begin
1392 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1393 Set_Strict_Alignment (E);
1395 elsif Is_Array_Type (E) then
1396 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1398 elsif Is_Record_Type (E) then
1399 if Is_Limited_Record (E) then
1400 Set_Strict_Alignment (E);
1401 return;
1402 end if;
1404 Comp := First_Component (E);
1405 while Present (Comp) loop
1406 if not Is_Type (Comp)
1407 and then (Strict_Alignment (Etype (Comp))
1408 or else Is_Aliased (Comp))
1409 then
1410 Set_Strict_Alignment (E);
1411 return;
1412 end if;
1414 Next_Component (Comp);
1415 end loop;
1416 end if;
1417 end Check_Strict_Alignment;
1419 -------------------------
1420 -- Check_Unsigned_Type --
1421 -------------------------
1423 procedure Check_Unsigned_Type (E : Entity_Id) is
1424 Ancestor : Entity_Id;
1425 Lo_Bound : Node_Id;
1426 Btyp : Entity_Id;
1428 begin
1429 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1430 return;
1431 end if;
1433 -- Do not attempt to analyze case where range was in error
1435 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
1436 return;
1437 end if;
1439 -- The situation that is nontrivial is something like:
1441 -- subtype x1 is integer range -10 .. +10;
1442 -- subtype x2 is x1 range 0 .. V1;
1443 -- subtype x3 is x2 range V2 .. V3;
1444 -- subtype x4 is x3 range V4 .. V5;
1446 -- where Vn are variables. Here the base type is signed, but we still
1447 -- know that x4 is unsigned because of the lower bound of x2.
1449 -- The only way to deal with this is to look up the ancestor chain
1451 Ancestor := E;
1452 loop
1453 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1454 return;
1455 end if;
1457 Lo_Bound := Type_Low_Bound (Ancestor);
1459 if Compile_Time_Known_Value (Lo_Bound) then
1460 if Expr_Rep_Value (Lo_Bound) >= 0 then
1461 Set_Is_Unsigned_Type (E, True);
1462 end if;
1464 return;
1466 else
1467 Ancestor := Ancestor_Subtype (Ancestor);
1469 -- If no ancestor had a static lower bound, go to base type
1471 if No (Ancestor) then
1473 -- Note: the reason we still check for a compile time known
1474 -- value for the base type is that at least in the case of
1475 -- generic formals, we can have bounds that fail this test,
1476 -- and there may be other cases in error situations.
1478 Btyp := Base_Type (E);
1480 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1481 return;
1482 end if;
1484 Lo_Bound := Type_Low_Bound (Base_Type (E));
1486 if Compile_Time_Known_Value (Lo_Bound)
1487 and then Expr_Rep_Value (Lo_Bound) >= 0
1488 then
1489 Set_Is_Unsigned_Type (E, True);
1490 end if;
1492 return;
1493 end if;
1494 end if;
1495 end loop;
1496 end Check_Unsigned_Type;
1498 -----------------------------
1499 -- Is_Atomic_VFA_Aggregate --
1500 -----------------------------
1502 function Is_Atomic_VFA_Aggregate (N : Node_Id) return Boolean is
1503 Loc : constant Source_Ptr := Sloc (N);
1504 New_N : Node_Id;
1505 Par : Node_Id;
1506 Temp : Entity_Id;
1507 Typ : Entity_Id;
1509 begin
1510 Par := Parent (N);
1512 -- Array may be qualified, so find outer context
1514 if Nkind (Par) = N_Qualified_Expression then
1515 Par := Parent (Par);
1516 end if;
1518 if not Comes_From_Source (Par) then
1519 return False;
1520 end if;
1522 case Nkind (Par) is
1523 when N_Assignment_Statement =>
1524 Typ := Etype (Name (Par));
1526 if not Is_Atomic_Or_VFA (Typ)
1527 and then not (Is_Entity_Name (Name (Par))
1528 and then Is_Atomic_Or_VFA (Entity (Name (Par))))
1529 then
1530 return False;
1531 end if;
1533 when N_Object_Declaration =>
1534 Typ := Etype (Defining_Identifier (Par));
1536 if not Is_Atomic_Or_VFA (Typ)
1537 and then not Is_Atomic_Or_VFA (Defining_Identifier (Par))
1538 then
1539 return False;
1540 end if;
1542 when others =>
1543 return False;
1544 end case;
1546 Temp := Make_Temporary (Loc, 'T', N);
1547 New_N :=
1548 Make_Object_Declaration (Loc,
1549 Defining_Identifier => Temp,
1550 Object_Definition => New_Occurrence_Of (Typ, Loc),
1551 Expression => Relocate_Node (N));
1552 Insert_Before (Par, New_N);
1553 Analyze (New_N);
1555 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1556 return True;
1557 end Is_Atomic_VFA_Aggregate;
1559 -----------------------------------------------
1560 -- Explode_Initialization_Compound_Statement --
1561 -----------------------------------------------
1563 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
1564 Init_Stmts : constant Node_Id := Initialization_Statements (E);
1566 begin
1567 if Present (Init_Stmts)
1568 and then Nkind (Init_Stmts) = N_Compound_Statement
1569 then
1570 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
1572 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
1573 -- just removing it, because Freeze_All may rely on this particular
1574 -- Node_Id still being present in the enclosing list to know where to
1575 -- stop freezing.
1577 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
1579 Set_Initialization_Statements (E, Empty);
1580 end if;
1581 end Explode_Initialization_Compound_Statement;
1583 ----------------
1584 -- Freeze_All --
1585 ----------------
1587 -- Note: the easy coding for this procedure would be to just build a
1588 -- single list of freeze nodes and then insert them and analyze them
1589 -- all at once. This won't work, because the analysis of earlier freeze
1590 -- nodes may recursively freeze types which would otherwise appear later
1591 -- on in the freeze list. So we must analyze and expand the freeze nodes
1592 -- as they are generated.
1594 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1595 E : Entity_Id;
1596 Decl : Node_Id;
1598 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1599 -- This is the internal recursive routine that does freezing of entities
1600 -- (but NOT the analysis of default expressions, which should not be
1601 -- recursive, we don't want to analyze those till we are sure that ALL
1602 -- the types are frozen).
1604 --------------------
1605 -- Freeze_All_Ent --
1606 --------------------
1608 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
1609 E : Entity_Id;
1610 Flist : List_Id;
1611 Lastn : Node_Id;
1613 procedure Process_Flist;
1614 -- If freeze nodes are present, insert and analyze, and reset cursor
1615 -- for next insertion.
1617 -------------------
1618 -- Process_Flist --
1619 -------------------
1621 procedure Process_Flist is
1622 begin
1623 if Is_Non_Empty_List (Flist) then
1624 Lastn := Next (After);
1625 Insert_List_After_And_Analyze (After, Flist);
1627 if Present (Lastn) then
1628 After := Prev (Lastn);
1629 else
1630 After := Last (List_Containing (After));
1631 end if;
1632 end if;
1633 end Process_Flist;
1635 -- Start of processing for Freeze_All_Ent
1637 begin
1638 E := From;
1639 while Present (E) loop
1641 -- If the entity is an inner package which is not a package
1642 -- renaming, then its entities must be frozen at this point. Note
1643 -- that such entities do NOT get frozen at the end of the nested
1644 -- package itself (only library packages freeze).
1646 -- Same is true for task declarations, where anonymous records
1647 -- created for entry parameters must be frozen.
1649 if Ekind (E) = E_Package
1650 and then No (Renamed_Object (E))
1651 and then not Is_Child_Unit (E)
1652 and then not Is_Frozen (E)
1653 then
1654 Push_Scope (E);
1655 Install_Visible_Declarations (E);
1656 Install_Private_Declarations (E);
1658 Freeze_All (First_Entity (E), After);
1660 End_Package_Scope (E);
1662 if Is_Generic_Instance (E)
1663 and then Has_Delayed_Freeze (E)
1664 then
1665 Set_Has_Delayed_Freeze (E, False);
1666 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1667 end if;
1669 elsif Ekind (E) in Task_Kind
1670 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1671 N_Single_Task_Declaration)
1672 then
1673 Push_Scope (E);
1674 Freeze_All (First_Entity (E), After);
1675 End_Scope;
1677 -- For a derived tagged type, we must ensure that all the
1678 -- primitive operations of the parent have been frozen, so that
1679 -- their addresses will be in the parent's dispatch table at the
1680 -- point it is inherited.
1682 elsif Ekind (E) = E_Record_Type
1683 and then Is_Tagged_Type (E)
1684 and then Is_Tagged_Type (Etype (E))
1685 and then Is_Derived_Type (E)
1686 then
1687 declare
1688 Prim_List : constant Elist_Id :=
1689 Primitive_Operations (Etype (E));
1691 Prim : Elmt_Id;
1692 Subp : Entity_Id;
1694 begin
1695 Prim := First_Elmt (Prim_List);
1696 while Present (Prim) loop
1697 Subp := Node (Prim);
1699 if Comes_From_Source (Subp)
1700 and then not Is_Frozen (Subp)
1701 then
1702 Flist := Freeze_Entity (Subp, After);
1703 Process_Flist;
1704 end if;
1706 Next_Elmt (Prim);
1707 end loop;
1708 end;
1709 end if;
1711 if not Is_Frozen (E) then
1712 Flist := Freeze_Entity (E, After);
1713 Process_Flist;
1715 -- If already frozen, and there are delayed aspects, this is where
1716 -- we do the visibility check for these aspects (see Sem_Ch13 spec
1717 -- for a description of how we handle aspect visibility).
1719 elsif Has_Delayed_Aspects (E) then
1721 -- Retrieve the visibility to the discriminants in order to
1722 -- analyze properly the aspects.
1724 Push_Scope_And_Install_Discriminants (E);
1726 declare
1727 Ritem : Node_Id;
1729 begin
1730 Ritem := First_Rep_Item (E);
1731 while Present (Ritem) loop
1732 if Nkind (Ritem) = N_Aspect_Specification
1733 and then Entity (Ritem) = E
1734 and then Is_Delayed_Aspect (Ritem)
1735 then
1736 Check_Aspect_At_End_Of_Declarations (Ritem);
1737 end if;
1739 Ritem := Next_Rep_Item (Ritem);
1740 end loop;
1741 end;
1743 Uninstall_Discriminants_And_Pop_Scope (E);
1744 end if;
1746 -- If an incomplete type is still not frozen, this may be a
1747 -- premature freezing because of a body declaration that follows.
1748 -- Indicate where the freezing took place. Freezing will happen
1749 -- if the body comes from source, but not if it is internally
1750 -- generated, for example as the body of a type invariant.
1752 -- If the freezing is caused by the end of the current declarative
1753 -- part, it is a Taft Amendment type, and there is no error.
1755 if not Is_Frozen (E)
1756 and then Ekind (E) = E_Incomplete_Type
1757 then
1758 declare
1759 Bod : constant Node_Id := Next (After);
1761 begin
1762 -- The presence of a body freezes all entities previously
1763 -- declared in the current list of declarations, but this
1764 -- does not apply if the body does not come from source.
1765 -- A type invariant is transformed into a subprogram body
1766 -- which is placed at the end of the private part of the
1767 -- current package, but this body does not freeze incomplete
1768 -- types that may be declared in this private part.
1770 if (Nkind_In (Bod, N_Subprogram_Body,
1771 N_Entry_Body,
1772 N_Package_Body,
1773 N_Protected_Body,
1774 N_Task_Body)
1775 or else Nkind (Bod) in N_Body_Stub)
1776 and then
1777 List_Containing (After) = List_Containing (Parent (E))
1778 and then Comes_From_Source (Bod)
1779 then
1780 Error_Msg_Sloc := Sloc (Next (After));
1781 Error_Msg_NE
1782 ("type& is frozen# before its full declaration",
1783 Parent (E), E);
1784 end if;
1785 end;
1786 end if;
1788 Next_Entity (E);
1789 end loop;
1790 end Freeze_All_Ent;
1792 -- Start of processing for Freeze_All
1794 begin
1795 Freeze_All_Ent (From, After);
1797 -- Now that all types are frozen, we can deal with default expressions
1798 -- that require us to build a default expression functions. This is the
1799 -- point at which such functions are constructed (after all types that
1800 -- might be used in such expressions have been frozen).
1802 -- For subprograms that are renaming_as_body, we create the wrapper
1803 -- bodies as needed.
1805 -- We also add finalization chains to access types whose designated
1806 -- types are controlled. This is normally done when freezing the type,
1807 -- but this misses recursive type definitions where the later members
1808 -- of the recursion introduce controlled components.
1810 -- Loop through entities
1812 E := From;
1813 while Present (E) loop
1814 if Is_Subprogram (E) then
1815 if not Default_Expressions_Processed (E) then
1816 Process_Default_Expressions (E, After);
1817 end if;
1819 if not Has_Completion (E) then
1820 Decl := Unit_Declaration_Node (E);
1822 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1823 if Error_Posted (Decl) then
1824 Set_Has_Completion (E);
1825 else
1826 Build_And_Analyze_Renamed_Body (Decl, E, After);
1827 end if;
1829 elsif Nkind (Decl) = N_Subprogram_Declaration
1830 and then Present (Corresponding_Body (Decl))
1831 and then
1832 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1833 = N_Subprogram_Renaming_Declaration
1834 then
1835 Build_And_Analyze_Renamed_Body
1836 (Decl, Corresponding_Body (Decl), After);
1837 end if;
1838 end if;
1840 elsif Ekind (E) in Task_Kind
1841 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1842 N_Single_Task_Declaration)
1843 then
1844 declare
1845 Ent : Entity_Id;
1847 begin
1848 Ent := First_Entity (E);
1849 while Present (Ent) loop
1850 if Is_Entry (Ent)
1851 and then not Default_Expressions_Processed (Ent)
1852 then
1853 Process_Default_Expressions (Ent, After);
1854 end if;
1856 Next_Entity (Ent);
1857 end loop;
1858 end;
1859 end if;
1861 -- Historical note: We used to create a finalization master for an
1862 -- access type whose designated type is not controlled, but contains
1863 -- private controlled compoments. This form of postprocessing is no
1864 -- longer needed because the finalization master is now created when
1865 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
1867 Next_Entity (E);
1868 end loop;
1869 end Freeze_All;
1871 -----------------------
1872 -- Freeze_And_Append --
1873 -----------------------
1875 procedure Freeze_And_Append
1876 (Ent : Entity_Id;
1877 N : Node_Id;
1878 Result : in out List_Id)
1880 L : constant List_Id := Freeze_Entity (Ent, N);
1881 begin
1882 if Is_Non_Empty_List (L) then
1883 if Result = No_List then
1884 Result := L;
1885 else
1886 Append_List (L, Result);
1887 end if;
1888 end if;
1889 end Freeze_And_Append;
1891 -------------------
1892 -- Freeze_Before --
1893 -------------------
1895 procedure Freeze_Before
1896 (N : Node_Id;
1897 T : Entity_Id;
1898 Do_Freeze_Profile : Boolean := True)
1900 -- Freeze T, then insert the generated Freeze nodes before the node N.
1901 -- Flag Freeze_Profile is used when T is an overloadable entity, and
1902 -- indicates whether its profile should be frozen at the same time.
1904 Freeze_Nodes : constant List_Id :=
1905 Freeze_Entity (T, N, Do_Freeze_Profile);
1907 begin
1908 if Ekind (T) = E_Function then
1909 Check_Expression_Function (N, T);
1910 end if;
1912 if Is_Non_Empty_List (Freeze_Nodes) then
1913 Insert_Actions (N, Freeze_Nodes);
1914 end if;
1915 end Freeze_Before;
1917 -------------------
1918 -- Freeze_Entity --
1919 -------------------
1921 function Freeze_Entity
1922 (E : Entity_Id;
1923 N : Node_Id;
1924 Do_Freeze_Profile : Boolean := True) return List_Id
1926 Loc : constant Source_Ptr := Sloc (N);
1927 Atype : Entity_Id;
1928 Comp : Entity_Id;
1929 F_Node : Node_Id;
1930 Formal : Entity_Id;
1931 Indx : Node_Id;
1933 Has_Default_Initialization : Boolean := False;
1934 -- This flag gets set to true for a variable with default initialization
1936 Result : List_Id := No_List;
1937 -- List of freezing actions, left at No_List if none
1939 Test_E : Entity_Id := E;
1940 -- This could use a comment ???
1942 procedure Add_To_Result (N : Node_Id);
1943 -- N is a freezing action to be appended to the Result
1945 function After_Last_Declaration return Boolean;
1946 -- If Loc is a freeze_entity that appears after the last declaration
1947 -- in the scope, inhibit error messages on late completion.
1949 procedure Check_Current_Instance (Comp_Decl : Node_Id);
1950 -- Check that an Access or Unchecked_Access attribute with a prefix
1951 -- which is the current instance type can only be applied when the type
1952 -- is limited.
1954 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
1955 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
1956 -- integer literal without an explicit corresponding size clause. The
1957 -- caller has checked that Utype is a modular integer type.
1959 procedure Freeze_Array_Type (Arr : Entity_Id);
1960 -- Freeze array type, including freezing index and component types
1962 procedure Freeze_Object_Declaration (E : Entity_Id);
1963 -- Perform checks and generate freeze node if needed for a constant or
1964 -- variable declared by an object declaration.
1966 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
1967 -- Create Freeze_Generic_Entity nodes for types declared in a generic
1968 -- package. Recurse on inner generic packages.
1970 function Freeze_Profile (E : Entity_Id) return Boolean;
1971 -- Freeze formals and return type of subprogram. If some type in the
1972 -- profile is incomplete and we are in an instance, freezing of the
1973 -- entity will take place elsewhere, and the function returns False.
1975 procedure Freeze_Record_Type (Rec : Entity_Id);
1976 -- Freeze record type, including freezing component types, and freezing
1977 -- primitive operations if this is a tagged type.
1979 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
1980 -- Determine whether an arbitrary entity is subject to Boolean aspect
1981 -- Import and its value is specified as True.
1983 function New_Freeze_Node return Node_Id;
1984 -- Create a new freeze node for entity E
1986 procedure Wrap_Imported_Subprogram (E : Entity_Id);
1987 -- If E is an entity for an imported subprogram with pre/post-conditions
1988 -- then this procedure will create a wrapper to ensure that proper run-
1989 -- time checking of the pre/postconditions. See body for details.
1991 -------------------
1992 -- Add_To_Result --
1993 -------------------
1995 procedure Add_To_Result (N : Node_Id) is
1996 begin
1997 if No (Result) then
1998 Result := New_List (N);
1999 else
2000 Append (N, Result);
2001 end if;
2002 end Add_To_Result;
2004 ----------------------------
2005 -- After_Last_Declaration --
2006 ----------------------------
2008 function After_Last_Declaration return Boolean is
2009 Spec : constant Node_Id := Parent (Current_Scope);
2011 begin
2012 if Nkind (Spec) = N_Package_Specification then
2013 if Present (Private_Declarations (Spec)) then
2014 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2015 elsif Present (Visible_Declarations (Spec)) then
2016 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2017 else
2018 return False;
2019 end if;
2021 else
2022 return False;
2023 end if;
2024 end After_Last_Declaration;
2026 ----------------------------
2027 -- Check_Current_Instance --
2028 ----------------------------
2030 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2032 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2033 -- Determine whether Typ is compatible with the rules for aliased
2034 -- views of types as defined in RM 3.10 in the various dialects.
2036 function Process (N : Node_Id) return Traverse_Result;
2037 -- Process routine to apply check to given node
2039 -----------------------------
2040 -- Is_Aliased_View_Of_Type --
2041 -----------------------------
2043 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2044 Typ_Decl : constant Node_Id := Parent (Typ);
2046 begin
2047 -- Common case
2049 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2050 and then Limited_Present (Type_Definition (Typ_Decl))
2051 then
2052 return True;
2054 -- The following paragraphs describe what a legal aliased view of
2055 -- a type is in the various dialects of Ada.
2057 -- Ada 95
2059 -- The current instance of a limited type, and a formal parameter
2060 -- or generic formal object of a tagged type.
2062 -- Ada 95 limited type
2063 -- * Type with reserved word "limited"
2064 -- * A protected or task type
2065 -- * A composite type with limited component
2067 elsif Ada_Version <= Ada_95 then
2068 return Is_Limited_Type (Typ);
2070 -- Ada 2005
2072 -- The current instance of a limited tagged type, a protected
2073 -- type, a task type, or a type that has the reserved word
2074 -- "limited" in its full definition ... a formal parameter or
2075 -- generic formal object of a tagged type.
2077 -- Ada 2005 limited type
2078 -- * Type with reserved word "limited", "synchronized", "task"
2079 -- or "protected"
2080 -- * A composite type with limited component
2081 -- * A derived type whose parent is a non-interface limited type
2083 elsif Ada_Version = Ada_2005 then
2084 return
2085 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2086 or else
2087 (Is_Derived_Type (Typ)
2088 and then not Is_Interface (Etype (Typ))
2089 and then Is_Limited_Type (Etype (Typ)));
2091 -- Ada 2012 and beyond
2093 -- The current instance of an immutably limited type ... a formal
2094 -- parameter or generic formal object of a tagged type.
2096 -- Ada 2012 limited type
2097 -- * Type with reserved word "limited", "synchronized", "task"
2098 -- or "protected"
2099 -- * A composite type with limited component
2100 -- * A derived type whose parent is a non-interface limited type
2101 -- * An incomplete view
2103 -- Ada 2012 immutably limited type
2104 -- * Explicitly limited record type
2105 -- * Record extension with "limited" present
2106 -- * Non-formal limited private type that is either tagged
2107 -- or has at least one access discriminant with a default
2108 -- expression
2109 -- * Task type, protected type or synchronized interface
2110 -- * Type derived from immutably limited type
2112 else
2113 return
2114 Is_Immutably_Limited_Type (Typ)
2115 or else Is_Incomplete_Type (Typ);
2116 end if;
2117 end Is_Aliased_View_Of_Type;
2119 -------------
2120 -- Process --
2121 -------------
2123 function Process (N : Node_Id) return Traverse_Result is
2124 begin
2125 case Nkind (N) is
2126 when N_Attribute_Reference =>
2127 if Nam_In (Attribute_Name (N), Name_Access,
2128 Name_Unchecked_Access)
2129 and then Is_Entity_Name (Prefix (N))
2130 and then Is_Type (Entity (Prefix (N)))
2131 and then Entity (Prefix (N)) = E
2132 then
2133 if Ada_Version < Ada_2012 then
2134 Error_Msg_N
2135 ("current instance must be a limited type",
2136 Prefix (N));
2137 else
2138 Error_Msg_N
2139 ("current instance must be an immutably limited "
2140 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2141 end if;
2143 return Abandon;
2145 else
2146 return OK;
2147 end if;
2149 when others => return OK;
2150 end case;
2151 end Process;
2153 procedure Traverse is new Traverse_Proc (Process);
2155 -- Local variables
2157 Rec_Type : constant Entity_Id :=
2158 Scope (Defining_Identifier (Comp_Decl));
2160 -- Start of processing for Check_Current_Instance
2162 begin
2163 if not Is_Aliased_View_Of_Type (Rec_Type) then
2164 Traverse (Comp_Decl);
2165 end if;
2166 end Check_Current_Instance;
2168 ------------------------------
2169 -- Check_Suspicious_Modulus --
2170 ------------------------------
2172 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
2173 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
2175 begin
2176 if not Warn_On_Suspicious_Modulus_Value then
2177 return;
2178 end if;
2180 if Nkind (Decl) = N_Full_Type_Declaration then
2181 declare
2182 Tdef : constant Node_Id := Type_Definition (Decl);
2184 begin
2185 if Nkind (Tdef) = N_Modular_Type_Definition then
2186 declare
2187 Modulus : constant Node_Id :=
2188 Original_Node (Expression (Tdef));
2190 begin
2191 if Nkind (Modulus) = N_Integer_Literal then
2192 declare
2193 Modv : constant Uint := Intval (Modulus);
2194 Sizv : constant Uint := RM_Size (Utype);
2196 begin
2197 -- First case, modulus and size are the same. This
2198 -- happens if you have something like mod 32, with
2199 -- an explicit size of 32, this is for sure a case
2200 -- where the warning is given, since it is seems
2201 -- very unlikely that someone would want e.g. a
2202 -- five bit type stored in 32 bits. It is much
2203 -- more likely they wanted a 32-bit type.
2205 if Modv = Sizv then
2206 null;
2208 -- Second case, the modulus is 32 or 64 and no
2209 -- size clause is present. This is a less clear
2210 -- case for giving the warning, but in the case
2211 -- of 32/64 (5-bit or 6-bit types) these seem rare
2212 -- enough that it is a likely error (and in any
2213 -- case using 2**5 or 2**6 in these cases seems
2214 -- clearer. We don't include 8 or 16 here, simply
2215 -- because in practice 3-bit and 4-bit types are
2216 -- more common and too many false positives if
2217 -- we warn in these cases.
2219 elsif not Has_Size_Clause (Utype)
2220 and then (Modv = Uint_32 or else Modv = Uint_64)
2221 then
2222 null;
2224 -- No warning needed
2226 else
2227 return;
2228 end if;
2230 -- If we fall through, give warning
2232 Error_Msg_Uint_1 := Modv;
2233 Error_Msg_N
2234 ("?M?2 '*'*^' may have been intended here",
2235 Modulus);
2236 end;
2237 end if;
2238 end;
2239 end if;
2240 end;
2241 end if;
2242 end Check_Suspicious_Modulus;
2244 -----------------------
2245 -- Freeze_Array_Type --
2246 -----------------------
2248 procedure Freeze_Array_Type (Arr : Entity_Id) is
2249 FS : constant Entity_Id := First_Subtype (Arr);
2250 Ctyp : constant Entity_Id := Component_Type (Arr);
2251 Clause : Entity_Id;
2253 Non_Standard_Enum : Boolean := False;
2254 -- Set true if any of the index types is an enumeration type with a
2255 -- non-standard representation.
2257 begin
2258 Freeze_And_Append (Ctyp, N, Result);
2260 Indx := First_Index (Arr);
2261 while Present (Indx) loop
2262 Freeze_And_Append (Etype (Indx), N, Result);
2264 if Is_Enumeration_Type (Etype (Indx))
2265 and then Has_Non_Standard_Rep (Etype (Indx))
2266 then
2267 Non_Standard_Enum := True;
2268 end if;
2270 Next_Index (Indx);
2271 end loop;
2273 -- Processing that is done only for base types
2275 if Ekind (Arr) = E_Array_Type then
2277 -- Deal with default setting of reverse storage order
2279 Set_SSO_From_Default (Arr);
2281 -- Propagate flags for component type
2283 if Is_Controlled_Active (Component_Type (Arr))
2284 or else Has_Controlled_Component (Ctyp)
2285 then
2286 Set_Has_Controlled_Component (Arr);
2287 end if;
2289 if Has_Unchecked_Union (Component_Type (Arr)) then
2290 Set_Has_Unchecked_Union (Arr);
2291 end if;
2293 -- Warn for pragma Pack overriding foreign convention
2295 if Has_Foreign_Convention (Ctyp)
2296 and then Has_Pragma_Pack (Arr)
2297 then
2298 declare
2299 CN : constant Name_Id :=
2300 Get_Convention_Name (Convention (Ctyp));
2301 PP : constant Node_Id :=
2302 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
2303 begin
2304 if Present (PP) then
2305 Error_Msg_Name_1 := CN;
2306 Error_Msg_Sloc := Sloc (Arr);
2307 Error_Msg_N
2308 ("pragma Pack affects convention % components #??", PP);
2309 Error_Msg_Name_1 := CN;
2310 Error_Msg_N
2311 ("\array components may not have % compatible "
2312 & "representation??", PP);
2313 end if;
2314 end;
2315 end if;
2317 -- If packing was requested or if the component size was
2318 -- set explicitly, then see if bit packing is required. This
2319 -- processing is only done for base types, since all of the
2320 -- representation aspects involved are type-related.
2322 -- This is not just an optimization, if we start processing the
2323 -- subtypes, they interfere with the settings on the base type
2324 -- (this is because Is_Packed has a slightly different meaning
2325 -- before and after freezing).
2327 declare
2328 Csiz : Uint;
2329 Esiz : Uint;
2331 begin
2332 if (Is_Packed (Arr) or else Has_Pragma_Pack (Arr))
2333 and then Known_Static_RM_Size (Ctyp)
2334 and then not Has_Component_Size_Clause (Arr)
2335 then
2336 Csiz := UI_Max (RM_Size (Ctyp), 1);
2338 elsif Known_Component_Size (Arr) then
2339 Csiz := Component_Size (Arr);
2341 elsif not Known_Static_Esize (Ctyp) then
2342 Csiz := Uint_0;
2344 else
2345 Esiz := Esize (Ctyp);
2347 -- We can set the component size if it is less than 16,
2348 -- rounding it up to the next storage unit size.
2350 if Esiz <= 8 then
2351 Csiz := Uint_8;
2352 elsif Esiz <= 16 then
2353 Csiz := Uint_16;
2354 else
2355 Csiz := Uint_0;
2356 end if;
2358 -- Set component size up to match alignment if it would
2359 -- otherwise be less than the alignment. This deals with
2360 -- cases of types whose alignment exceeds their size (the
2361 -- padded type cases).
2363 if Csiz /= 0 then
2364 declare
2365 A : constant Uint := Alignment_In_Bits (Ctyp);
2366 begin
2367 if Csiz < A then
2368 Csiz := A;
2369 end if;
2370 end;
2371 end if;
2372 end if;
2374 -- Case of component size that may result in bit packing
2376 if 1 <= Csiz and then Csiz <= 64 then
2377 declare
2378 Ent : constant Entity_Id :=
2379 First_Subtype (Arr);
2380 Pack_Pragma : constant Node_Id :=
2381 Get_Rep_Pragma (Ent, Name_Pack);
2382 Comp_Size_C : constant Node_Id :=
2383 Get_Attribute_Definition_Clause
2384 (Ent, Attribute_Component_Size);
2386 begin
2387 -- Warn if we have pack and component size so that the
2388 -- pack is ignored.
2390 -- Note: here we must check for the presence of a
2391 -- component size before checking for a Pack pragma to
2392 -- deal with the case where the array type is a derived
2393 -- type whose parent is currently private.
2395 if Present (Comp_Size_C)
2396 and then Has_Pragma_Pack (Ent)
2397 and then Warn_On_Redundant_Constructs
2398 then
2399 Error_Msg_Sloc := Sloc (Comp_Size_C);
2400 Error_Msg_NE
2401 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
2402 Error_Msg_N
2403 ("\?r?explicit component size given#!", Pack_Pragma);
2404 Set_Is_Packed (Base_Type (Ent), False);
2405 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
2406 end if;
2408 -- Set component size if not already set by a component
2409 -- size clause.
2411 if not Present (Comp_Size_C) then
2412 Set_Component_Size (Arr, Csiz);
2413 end if;
2415 -- Check for base type of 8, 16, 32 bits, where an
2416 -- unsigned subtype has a length one less than the
2417 -- base type (e.g. Natural subtype of Integer).
2419 -- In such cases, if a component size was not set
2420 -- explicitly, then generate a warning.
2422 if Has_Pragma_Pack (Arr)
2423 and then not Present (Comp_Size_C)
2424 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2425 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2426 then
2427 Error_Msg_Uint_1 := Csiz;
2429 if Present (Pack_Pragma) then
2430 Error_Msg_N
2431 ("??pragma Pack causes component size to be ^!",
2432 Pack_Pragma);
2433 Error_Msg_N
2434 ("\??use Component_Size to set desired value!",
2435 Pack_Pragma);
2436 end if;
2437 end if;
2439 -- Bit packing is never needed for 8, 16, 32, 64
2441 if Addressable (Csiz) then
2442 -- If the Esize of the component is known and equal to
2443 -- the component size then even packing is not needed.
2445 if Known_Static_Esize (Component_Type (Arr))
2446 and then Esize (Component_Type (Arr)) = Csiz
2447 then
2448 -- Here the array was requested to be packed, but
2449 -- the packing request had no effect whatsoever,
2450 -- so flag Is_Packed is reset.
2452 -- Note: semantically this means that we lose track
2453 -- of the fact that a derived type inherited pragma
2454 -- Pack that was non-effective, but that is fine.
2456 -- We regard a Pack pragma as a request to set a
2457 -- representation characteristic, and this request
2458 -- may be ignored.
2460 Set_Is_Packed (Base_Type (Arr), False);
2461 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
2462 else
2463 Set_Is_Packed (Base_Type (Arr), True);
2464 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2465 end if;
2467 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2469 -- Bit packing is not needed for multiples of the storage
2470 -- unit if the type is composite because the back end can
2471 -- byte pack composite types.
2473 elsif Csiz mod System_Storage_Unit = 0
2474 and then Is_Composite_Type (Ctyp)
2475 then
2477 Set_Is_Packed (Base_Type (Arr), True);
2478 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2479 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2481 -- In all other cases, bit packing is needed
2483 else
2484 Set_Is_Packed (Base_Type (Arr), True);
2485 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2486 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
2487 end if;
2488 end;
2489 end if;
2490 end;
2492 -- Check for Aliased or Atomic_Components/Atomic/VFA with
2493 -- unsuitable packing or explicit component size clause given.
2495 if (Has_Aliased_Components (Arr)
2496 or else Has_Atomic_Components (Arr)
2497 or else Is_Atomic_Or_VFA (Ctyp))
2498 and then
2499 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2500 then
2501 Alias_Atomic_Check : declare
2503 procedure Complain_CS (T : String);
2504 -- Outputs error messages for incorrect CS clause or pragma
2505 -- Pack for aliased or atomic/VFA components (T is "aliased"
2506 -- or "atomic/vfa");
2508 -----------------
2509 -- Complain_CS --
2510 -----------------
2512 procedure Complain_CS (T : String) is
2513 begin
2514 if Has_Component_Size_Clause (Arr) then
2515 Clause :=
2516 Get_Attribute_Definition_Clause
2517 (FS, Attribute_Component_Size);
2519 Error_Msg_N
2520 ("incorrect component size for "
2521 & T & " components", Clause);
2522 Error_Msg_Uint_1 := Esize (Ctyp);
2523 Error_Msg_N
2524 ("\only allowed value is^", Clause);
2526 else
2527 Error_Msg_N
2528 ("cannot pack " & T & " components",
2529 Get_Rep_Pragma (FS, Name_Pack));
2530 end if;
2531 end Complain_CS;
2533 -- Start of processing for Alias_Atomic_Check
2535 begin
2536 -- If object size of component type isn't known, we cannot
2537 -- be sure so we defer to the back end.
2539 if not Known_Static_Esize (Ctyp) then
2540 null;
2542 -- Case where component size has no effect. First check for
2543 -- object size of component type multiple of the storage
2544 -- unit size.
2546 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2548 -- OK in both packing case and component size case if RM
2549 -- size is known and static and same as the object size.
2551 and then
2552 ((Known_Static_RM_Size (Ctyp)
2553 and then Esize (Ctyp) = RM_Size (Ctyp))
2555 -- Or if we have an explicit component size clause and
2556 -- the component size and object size are equal.
2558 or else
2559 (Has_Component_Size_Clause (Arr)
2560 and then Component_Size (Arr) = Esize (Ctyp)))
2561 then
2562 null;
2564 elsif Has_Aliased_Components (Arr) then
2565 Complain_CS ("aliased");
2567 elsif Has_Atomic_Components (Arr)
2568 or else Is_Atomic (Ctyp)
2569 then
2570 Complain_CS ("atomic");
2572 elsif Is_Volatile_Full_Access (Ctyp) then
2573 Complain_CS ("volatile full access");
2574 end if;
2575 end Alias_Atomic_Check;
2576 end if;
2578 -- Check for Independent_Components/Independent with unsuitable
2579 -- packing or explicit component size clause given.
2581 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
2582 and then
2583 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2584 then
2585 begin
2586 -- If object size of component type isn't known, we cannot
2587 -- be sure so we defer to the back end.
2589 if not Known_Static_Esize (Ctyp) then
2590 null;
2592 -- Case where component size has no effect. First check for
2593 -- object size of component type multiple of the storage
2594 -- unit size.
2596 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2598 -- OK in both packing case and component size case if RM
2599 -- size is known and multiple of the storage unit size.
2601 and then
2602 ((Known_Static_RM_Size (Ctyp)
2603 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
2605 -- Or if we have an explicit component size clause and
2606 -- the component size is larger than the object size.
2608 or else
2609 (Has_Component_Size_Clause (Arr)
2610 and then Component_Size (Arr) >= Esize (Ctyp)))
2611 then
2612 null;
2614 else
2615 if Has_Component_Size_Clause (Arr) then
2616 Clause :=
2617 Get_Attribute_Definition_Clause
2618 (FS, Attribute_Component_Size);
2620 Error_Msg_N
2621 ("incorrect component size for "
2622 & "independent components", Clause);
2623 Error_Msg_Uint_1 := Esize (Ctyp);
2624 Error_Msg_N
2625 ("\minimum allowed is^", Clause);
2627 else
2628 Error_Msg_N
2629 ("cannot pack independent components",
2630 Get_Rep_Pragma (FS, Name_Pack));
2631 end if;
2632 end if;
2633 end;
2634 end if;
2636 -- Warn for case of atomic type
2638 Clause := Get_Rep_Pragma (FS, Name_Atomic);
2640 if Present (Clause)
2641 and then not Addressable (Component_Size (FS))
2642 then
2643 Error_Msg_NE
2644 ("non-atomic components of type& may not be "
2645 & "accessible by separate tasks??", Clause, Arr);
2647 if Has_Component_Size_Clause (Arr) then
2648 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2649 (FS, Attribute_Component_Size));
2650 Error_Msg_N ("\because of component size clause#??", Clause);
2652 elsif Has_Pragma_Pack (Arr) then
2653 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2654 Error_Msg_N ("\because of pragma Pack#??", Clause);
2655 end if;
2656 end if;
2658 -- Check for scalar storage order
2660 declare
2661 Dummy : Boolean;
2662 begin
2663 Check_Component_Storage_Order
2664 (Encl_Type => Arr,
2665 Comp => Empty,
2666 ADC => Get_Attribute_Definition_Clause
2667 (First_Subtype (Arr),
2668 Attribute_Scalar_Storage_Order),
2669 Comp_ADC_Present => Dummy);
2670 end;
2672 -- Processing that is done only for subtypes
2674 else
2675 -- Acquire alignment from base type
2677 if Unknown_Alignment (Arr) then
2678 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
2679 Adjust_Esize_Alignment (Arr);
2680 end if;
2681 end if;
2683 -- Specific checks for bit-packed arrays
2685 if Is_Bit_Packed_Array (Arr) then
2687 -- Check number of elements for bit packed arrays that come from
2688 -- source and have compile time known ranges. The bit-packed
2689 -- arrays circuitry does not support arrays with more than
2690 -- Integer'Last + 1 elements, and when this restriction is
2691 -- violated, causes incorrect data access.
2693 -- For the case where this is not compile time known, a run-time
2694 -- check should be generated???
2696 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
2697 declare
2698 Elmts : Uint;
2699 Index : Node_Id;
2700 Ilen : Node_Id;
2701 Ityp : Entity_Id;
2703 begin
2704 Elmts := Uint_1;
2705 Index := First_Index (Arr);
2706 while Present (Index) loop
2707 Ityp := Etype (Index);
2709 -- Never generate an error if any index is of a generic
2710 -- type. We will check this in instances.
2712 if Is_Generic_Type (Ityp) then
2713 Elmts := Uint_0;
2714 exit;
2715 end if;
2717 Ilen :=
2718 Make_Attribute_Reference (Loc,
2719 Prefix => New_Occurrence_Of (Ityp, Loc),
2720 Attribute_Name => Name_Range_Length);
2721 Analyze_And_Resolve (Ilen);
2723 -- No attempt is made to check number of elements if not
2724 -- compile time known.
2726 if Nkind (Ilen) /= N_Integer_Literal then
2727 Elmts := Uint_0;
2728 exit;
2729 end if;
2731 Elmts := Elmts * Intval (Ilen);
2732 Next_Index (Index);
2733 end loop;
2735 if Elmts > Intval (High_Bound
2736 (Scalar_Range (Standard_Integer))) + 1
2737 then
2738 Error_Msg_N
2739 ("bit packed array type may not have "
2740 & "more than Integer''Last+1 elements", Arr);
2741 end if;
2742 end;
2743 end if;
2745 -- Check size
2747 if Known_RM_Size (Arr) then
2748 declare
2749 SizC : constant Node_Id := Size_Clause (Arr);
2750 Discard : Boolean;
2752 begin
2753 -- It is not clear if it is possible to have no size clause
2754 -- at this stage, but it is not worth worrying about. Post
2755 -- error on the entity name in the size clause if present,
2756 -- else on the type entity itself.
2758 if Present (SizC) then
2759 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
2760 else
2761 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
2762 end if;
2763 end;
2764 end if;
2765 end if;
2767 -- If any of the index types was an enumeration type with a non-
2768 -- standard rep clause, then we indicate that the array type is
2769 -- always packed (even if it is not bit packed).
2771 if Non_Standard_Enum then
2772 Set_Has_Non_Standard_Rep (Base_Type (Arr));
2773 Set_Is_Packed (Base_Type (Arr));
2774 end if;
2776 Set_Component_Alignment_If_Not_Set (Arr);
2778 -- If the array is packed and bit packed or packed to eliminate holes
2779 -- in the non-contiguous enumeration index types, we must create the
2780 -- packed array type to be used to actually implement the type. This
2781 -- is only needed for real array types (not for string literal types,
2782 -- since they are present only for the front end).
2784 if Is_Packed (Arr)
2785 and then (Is_Bit_Packed_Array (Arr) or else Non_Standard_Enum)
2786 and then Ekind (Arr) /= E_String_Literal_Subtype
2787 then
2788 Create_Packed_Array_Impl_Type (Arr);
2789 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
2791 -- Make sure that we have the necessary routines to implement the
2792 -- packing, and complain now if not. Note that we only test this
2793 -- for constrained array types.
2795 if Is_Constrained (Arr)
2796 and then Is_Bit_Packed_Array (Arr)
2797 and then Present (Packed_Array_Impl_Type (Arr))
2798 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
2799 then
2800 declare
2801 CS : constant Uint := Component_Size (Arr);
2802 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
2804 begin
2805 if RE /= RE_Null
2806 and then not RTE_Available (RE)
2807 then
2808 Error_Msg_CRT
2809 ("packing of " & UI_Image (CS) & "-bit components",
2810 First_Subtype (Etype (Arr)));
2812 -- Cancel the packing
2814 Set_Is_Packed (Base_Type (Arr), False);
2815 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2816 Set_Packed_Array_Impl_Type (Arr, Empty);
2817 goto Skip_Packed;
2818 end if;
2819 end;
2820 end if;
2822 -- Size information of packed array type is copied to the array
2823 -- type, since this is really the representation. But do not
2824 -- override explicit existing size values. If the ancestor subtype
2825 -- is constrained the Packed_Array_Impl_Type will be inherited
2826 -- from it, but the size may have been provided already, and
2827 -- must not be overridden either.
2829 if not Has_Size_Clause (Arr)
2830 and then
2831 (No (Ancestor_Subtype (Arr))
2832 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
2833 then
2834 Set_Esize (Arr, Esize (Packed_Array_Impl_Type (Arr)));
2835 Set_RM_Size (Arr, RM_Size (Packed_Array_Impl_Type (Arr)));
2836 end if;
2838 if not Has_Alignment_Clause (Arr) then
2839 Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
2840 end if;
2841 end if;
2843 <<Skip_Packed>>
2845 -- For non-packed arrays set the alignment of the array to the
2846 -- alignment of the component type if it is unknown. Skip this
2847 -- in atomic/VFA case (atomic/VFA arrays may need larger alignments).
2849 if not Is_Packed (Arr)
2850 and then Unknown_Alignment (Arr)
2851 and then Known_Alignment (Ctyp)
2852 and then Known_Static_Component_Size (Arr)
2853 and then Known_Static_Esize (Ctyp)
2854 and then Esize (Ctyp) = Component_Size (Arr)
2855 and then not Is_Atomic_Or_VFA (Arr)
2856 then
2857 Set_Alignment (Arr, Alignment (Component_Type (Arr)));
2858 end if;
2860 -- A Ghost type cannot have a component of protected or task type
2861 -- (SPARK RM 6.9(19)).
2863 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
2864 Error_Msg_N
2865 ("ghost array type & cannot have concurrent component type",
2866 Arr);
2867 end if;
2868 end Freeze_Array_Type;
2870 -------------------------------
2871 -- Freeze_Object_Declaration --
2872 -------------------------------
2874 procedure Freeze_Object_Declaration (E : Entity_Id) is
2875 begin
2876 -- Abstract type allowed only for C++ imported variables or constants
2878 -- Note: we inhibit this check for objects that do not come from
2879 -- source because there is at least one case (the expansion of
2880 -- x'Class'Input where x is abstract) where we legitimately
2881 -- generate an abstract object.
2883 if Is_Abstract_Type (Etype (E))
2884 and then Comes_From_Source (Parent (E))
2885 and then not (Is_Imported (E) and then Is_CPP_Class (Etype (E)))
2886 then
2887 Error_Msg_N ("type of object cannot be abstract",
2888 Object_Definition (Parent (E)));
2890 if Is_CPP_Class (Etype (E)) then
2891 Error_Msg_NE
2892 ("\} may need a cpp_constructor",
2893 Object_Definition (Parent (E)), Etype (E));
2895 elsif Present (Expression (Parent (E))) then
2896 Error_Msg_N -- CODEFIX
2897 ("\maybe a class-wide type was meant",
2898 Object_Definition (Parent (E)));
2899 end if;
2900 end if;
2902 -- For object created by object declaration, perform required
2903 -- categorization (preelaborate and pure) checks. Defer these
2904 -- checks to freeze time since pragma Import inhibits default
2905 -- initialization and thus pragma Import affects these checks.
2907 Validate_Object_Declaration (Declaration_Node (E));
2909 -- If there is an address clause, check that it is valid
2910 -- and if need be move initialization to the freeze node.
2912 Check_Address_Clause (E);
2914 -- Similar processing is needed for aspects that may affect
2915 -- object layout, like Alignment, if there is an initialization
2916 -- expression.
2918 if Has_Delayed_Aspects (E)
2919 and then Expander_Active
2920 and then Is_Array_Type (Etype (E))
2921 and then Present (Expression (Parent (E)))
2922 then
2923 declare
2924 Decl : constant Node_Id := Parent (E);
2925 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
2927 begin
2929 -- Capture initialization value at point of declaration, and
2930 -- make explicit assignment legal, because object may be a
2931 -- constant.
2933 Remove_Side_Effects (Expression (Decl));
2934 Set_Assignment_OK (Lhs);
2936 -- Move initialization to freeze actions.
2938 Append_Freeze_Action (E,
2939 Make_Assignment_Statement (Loc,
2940 Name => Lhs,
2941 Expression => Expression (Decl)));
2943 Set_No_Initialization (Decl);
2944 -- Set_Is_Frozen (E, False);
2945 end;
2946 end if;
2948 -- Reset Is_True_Constant for non-constant aliased object. We
2949 -- consider that the fact that a non-constant object is aliased may
2950 -- indicate that some funny business is going on, e.g. an aliased
2951 -- object is passed by reference to a procedure which captures the
2952 -- address of the object, which is later used to assign a new value,
2953 -- even though the compiler thinks that it is not modified. Such
2954 -- code is highly dubious, but we choose to make it "work" for
2955 -- non-constant aliased objects.
2957 -- Note that we used to do this for all aliased objects, whether or
2958 -- not constant, but this caused anomalies down the line because we
2959 -- ended up with static objects that were not Is_True_Constant. Not
2960 -- resetting Is_True_Constant for (aliased) constant objects ensures
2961 -- that this anomaly never occurs.
2963 -- However, we don't do that for internal entities. We figure that if
2964 -- we deliberately set Is_True_Constant for an internal entity, e.g.
2965 -- a dispatch table entry, then we mean it.
2967 if Ekind (E) /= E_Constant
2968 and then (Is_Aliased (E) or else Is_Aliased (Etype (E)))
2969 and then not Is_Internal_Name (Chars (E))
2970 then
2971 Set_Is_True_Constant (E, False);
2972 end if;
2974 -- If the object needs any kind of default initialization, an error
2975 -- must be issued if No_Default_Initialization applies. The check
2976 -- doesn't apply to imported objects, which are not ever default
2977 -- initialized, and is why the check is deferred until freezing, at
2978 -- which point we know if Import applies. Deferred constants are also
2979 -- exempted from this test because their completion is explicit, or
2980 -- through an import pragma.
2982 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
2983 null;
2985 elsif Comes_From_Source (E)
2986 and then not Is_Imported (E)
2987 and then not Has_Init_Expression (Declaration_Node (E))
2988 and then
2989 ((Has_Non_Null_Base_Init_Proc (Etype (E))
2990 and then not No_Initialization (Declaration_Node (E))
2991 and then not Initialization_Suppressed (Etype (E)))
2992 or else
2993 (Needs_Simple_Initialization (Etype (E))
2994 and then not Is_Internal (E)))
2995 then
2996 Has_Default_Initialization := True;
2997 Check_Restriction
2998 (No_Default_Initialization, Declaration_Node (E));
2999 end if;
3001 -- Check that a Thread_Local_Storage variable does not have
3002 -- default initialization, and any explicit initialization must
3003 -- either be the null constant or a static constant.
3005 if Has_Pragma_Thread_Local_Storage (E) then
3006 declare
3007 Decl : constant Node_Id := Declaration_Node (E);
3008 begin
3009 if Has_Default_Initialization
3010 or else
3011 (Has_Init_Expression (Decl)
3012 and then
3013 (No (Expression (Decl))
3014 or else not
3015 (Is_OK_Static_Expression (Expression (Decl))
3016 or else Nkind (Expression (Decl)) = N_Null)))
3017 then
3018 Error_Msg_NE
3019 ("Thread_Local_Storage variable& is "
3020 & "improperly initialized", Decl, E);
3021 Error_Msg_NE
3022 ("\only allowed initialization is explicit "
3023 & "NULL or static expression", Decl, E);
3024 end if;
3025 end;
3026 end if;
3028 -- For imported objects, set Is_Public unless there is also an
3029 -- address clause, which means that there is no external symbol
3030 -- needed for the Import (Is_Public may still be set for other
3031 -- unrelated reasons). Note that we delayed this processing
3032 -- till freeze time so that we can be sure not to set the flag
3033 -- if there is an address clause. If there is such a clause,
3034 -- then the only purpose of the Import pragma is to suppress
3035 -- implicit initialization.
3037 if Is_Imported (E) and then No (Address_Clause (E)) then
3038 Set_Is_Public (E);
3039 end if;
3041 -- For source objects that are not Imported and are library
3042 -- level, if no linker section pragma was given inherit the
3043 -- appropriate linker section from the corresponding type.
3045 if Comes_From_Source (E)
3046 and then not Is_Imported (E)
3047 and then Is_Library_Level_Entity (E)
3048 and then No (Linker_Section_Pragma (E))
3049 then
3050 Set_Linker_Section_Pragma
3051 (E, Linker_Section_Pragma (Etype (E)));
3052 end if;
3054 -- For convention C objects of an enumeration type, warn if the
3055 -- size is not integer size and no explicit size given. Skip
3056 -- warning for Boolean, and Character, assume programmer expects
3057 -- 8-bit sizes for these cases.
3059 if (Convention (E) = Convention_C
3060 or else
3061 Convention (E) = Convention_CPP)
3062 and then Is_Enumeration_Type (Etype (E))
3063 and then not Is_Character_Type (Etype (E))
3064 and then not Is_Boolean_Type (Etype (E))
3065 and then Esize (Etype (E)) < Standard_Integer_Size
3066 and then not Has_Size_Clause (E)
3067 then
3068 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
3069 Error_Msg_N
3070 ("??convention C enumeration object has size less than ^", E);
3071 Error_Msg_N ("\??use explicit size clause to set size", E);
3072 end if;
3073 end Freeze_Object_Declaration;
3075 -----------------------------
3076 -- Freeze_Generic_Entities --
3077 -----------------------------
3079 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
3080 E : Entity_Id;
3081 F : Node_Id;
3082 Flist : List_Id;
3084 begin
3085 Flist := New_List;
3086 E := First_Entity (Pack);
3087 while Present (E) loop
3088 if Is_Type (E) and then not Is_Generic_Type (E) then
3089 F := Make_Freeze_Generic_Entity (Sloc (Pack));
3090 Set_Entity (F, E);
3091 Append_To (Flist, F);
3093 elsif Ekind (E) = E_Generic_Package then
3094 Append_List_To (Flist, Freeze_Generic_Entities (E));
3095 end if;
3097 Next_Entity (E);
3098 end loop;
3100 return Flist;
3101 end Freeze_Generic_Entities;
3103 --------------------
3104 -- Freeze_Profile --
3105 --------------------
3107 function Freeze_Profile (E : Entity_Id) return Boolean is
3108 F_Type : Entity_Id;
3109 R_Type : Entity_Id;
3110 Warn_Node : Node_Id;
3112 begin
3113 -- Loop through formals
3115 Formal := First_Formal (E);
3116 while Present (Formal) loop
3117 F_Type := Etype (Formal);
3119 -- AI05-0151: incomplete types can appear in a profile. By the
3120 -- time the entity is frozen, the full view must be available,
3121 -- unless it is a limited view.
3123 if Is_Incomplete_Type (F_Type)
3124 and then Present (Full_View (F_Type))
3125 and then not From_Limited_With (F_Type)
3126 then
3127 F_Type := Full_View (F_Type);
3128 Set_Etype (Formal, F_Type);
3129 end if;
3131 if not From_Limited_With (F_Type) then
3132 Freeze_And_Append (F_Type, N, Result);
3133 end if;
3135 if Is_Private_Type (F_Type)
3136 and then Is_Private_Type (Base_Type (F_Type))
3137 and then No (Full_View (Base_Type (F_Type)))
3138 and then not Is_Generic_Type (F_Type)
3139 and then not Is_Derived_Type (F_Type)
3140 then
3141 -- If the type of a formal is incomplete, subprogram is being
3142 -- frozen prematurely. Within an instance (but not within a
3143 -- wrapper package) this is an artifact of our need to regard
3144 -- the end of an instantiation as a freeze point. Otherwise it
3145 -- is a definite error.
3147 if In_Instance then
3148 Set_Is_Frozen (E, False);
3149 Result := No_List;
3150 return False;
3152 elsif not After_Last_Declaration
3153 and then not Freezing_Library_Level_Tagged_Type
3154 then
3155 Error_Msg_Node_1 := F_Type;
3156 Error_Msg
3157 ("type & must be fully defined before this point", Loc);
3158 end if;
3159 end if;
3161 -- Check suspicious parameter for C function. These tests apply
3162 -- only to exported/imported subprograms.
3164 if Warn_On_Export_Import
3165 and then Comes_From_Source (E)
3166 and then (Convention (E) = Convention_C
3167 or else
3168 Convention (E) = Convention_CPP)
3169 and then (Is_Imported (E) or else Is_Exported (E))
3170 and then Convention (E) /= Convention (Formal)
3171 and then not Has_Warnings_Off (E)
3172 and then not Has_Warnings_Off (F_Type)
3173 and then not Has_Warnings_Off (Formal)
3174 then
3175 -- Qualify mention of formals with subprogram name
3177 Error_Msg_Qual_Level := 1;
3179 -- Check suspicious use of fat C pointer
3181 if Is_Access_Type (F_Type)
3182 and then Esize (F_Type) > Ttypes.System_Address_Size
3183 then
3184 Error_Msg_N
3185 ("?x?type of & does not correspond to C pointer!", Formal);
3187 -- Check suspicious return of boolean
3189 elsif Root_Type (F_Type) = Standard_Boolean
3190 and then Convention (F_Type) = Convention_Ada
3191 and then not Has_Warnings_Off (F_Type)
3192 and then not Has_Size_Clause (F_Type)
3193 then
3194 Error_Msg_N
3195 ("& is an 8-bit Ada Boolean?x?", Formal);
3196 Error_Msg_N
3197 ("\use appropriate corresponding type in C "
3198 & "(e.g. char)?x?", Formal);
3200 -- Check suspicious tagged type
3202 elsif (Is_Tagged_Type (F_Type)
3203 or else
3204 (Is_Access_Type (F_Type)
3205 and then Is_Tagged_Type (Designated_Type (F_Type))))
3206 and then Convention (E) = Convention_C
3207 then
3208 Error_Msg_N
3209 ("?x?& involves a tagged type which does not "
3210 & "correspond to any C type!", Formal);
3212 -- Check wrong convention subprogram pointer
3214 elsif Ekind (F_Type) = E_Access_Subprogram_Type
3215 and then not Has_Foreign_Convention (F_Type)
3216 then
3217 Error_Msg_N
3218 ("?x?subprogram pointer & should "
3219 & "have foreign convention!", Formal);
3220 Error_Msg_Sloc := Sloc (F_Type);
3221 Error_Msg_NE
3222 ("\?x?add Convention pragma to declaration of &#",
3223 Formal, F_Type);
3224 end if;
3226 -- Turn off name qualification after message output
3228 Error_Msg_Qual_Level := 0;
3229 end if;
3231 -- Check for unconstrained array in exported foreign convention
3232 -- case.
3234 if Has_Foreign_Convention (E)
3235 and then not Is_Imported (E)
3236 and then Is_Array_Type (F_Type)
3237 and then not Is_Constrained (F_Type)
3238 and then Warn_On_Export_Import
3239 then
3240 Error_Msg_Qual_Level := 1;
3242 -- If this is an inherited operation, place the warning on
3243 -- the derived type declaration, rather than on the original
3244 -- subprogram.
3246 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
3247 then
3248 Warn_Node := Parent (E);
3250 if Formal = First_Formal (E) then
3251 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
3252 end if;
3253 else
3254 Warn_Node := Formal;
3255 end if;
3257 Error_Msg_NE ("?x?type of argument& is unconstrained array",
3258 Warn_Node, Formal);
3259 Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
3260 Warn_Node, Formal);
3261 Error_Msg_Qual_Level := 0;
3262 end if;
3264 if not From_Limited_With (F_Type) then
3265 if Is_Access_Type (F_Type) then
3266 F_Type := Designated_Type (F_Type);
3267 end if;
3269 -- If the formal is an anonymous_access_to_subprogram
3270 -- freeze the subprogram type as well, to prevent
3271 -- scope anomalies in gigi, because there is no other
3272 -- clear point at which it could be frozen.
3274 if Is_Itype (Etype (Formal))
3275 and then Ekind (F_Type) = E_Subprogram_Type
3276 then
3277 Freeze_And_Append (F_Type, N, Result);
3278 end if;
3279 end if;
3281 Next_Formal (Formal);
3282 end loop;
3284 -- Case of function: similar checks on return type
3286 if Ekind (E) = E_Function then
3288 -- Freeze return type
3290 R_Type := Etype (E);
3292 -- AI05-0151: the return type may have been incomplete
3293 -- at the point of declaration. Replace it with the full
3294 -- view, unless the current type is a limited view. In
3295 -- that case the full view is in a different unit, and
3296 -- gigi finds the non-limited view after the other unit
3297 -- is elaborated.
3299 if Ekind (R_Type) = E_Incomplete_Type
3300 and then Present (Full_View (R_Type))
3301 and then not From_Limited_With (R_Type)
3302 then
3303 R_Type := Full_View (R_Type);
3304 Set_Etype (E, R_Type);
3305 end if;
3307 Freeze_And_Append (R_Type, N, Result);
3309 -- Check suspicious return type for C function
3311 if Warn_On_Export_Import
3312 and then (Convention (E) = Convention_C
3313 or else
3314 Convention (E) = Convention_CPP)
3315 and then (Is_Imported (E) or else Is_Exported (E))
3316 then
3317 -- Check suspicious return of fat C pointer
3319 if Is_Access_Type (R_Type)
3320 and then Esize (R_Type) > Ttypes.System_Address_Size
3321 and then not Has_Warnings_Off (E)
3322 and then not Has_Warnings_Off (R_Type)
3323 then
3324 Error_Msg_N ("?x?return type of& does not "
3325 & "correspond to C pointer!", E);
3327 -- Check suspicious return of boolean
3329 elsif Root_Type (R_Type) = Standard_Boolean
3330 and then Convention (R_Type) = Convention_Ada
3331 and then not Has_Warnings_Off (E)
3332 and then not Has_Warnings_Off (R_Type)
3333 and then not Has_Size_Clause (R_Type)
3334 then
3335 declare
3336 N : constant Node_Id :=
3337 Result_Definition (Declaration_Node (E));
3338 begin
3339 Error_Msg_NE
3340 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
3341 Error_Msg_NE
3342 ("\use appropriate corresponding type in C "
3343 & "(e.g. char)?x?", N, E);
3344 end;
3346 -- Check suspicious return tagged type
3348 elsif (Is_Tagged_Type (R_Type)
3349 or else (Is_Access_Type (R_Type)
3350 and then
3351 Is_Tagged_Type
3352 (Designated_Type (R_Type))))
3353 and then Convention (E) = Convention_C
3354 and then not Has_Warnings_Off (E)
3355 and then not Has_Warnings_Off (R_Type)
3356 then
3357 Error_Msg_N ("?x?return type of & does not "
3358 & "correspond to C type!", E);
3360 -- Check return of wrong convention subprogram pointer
3362 elsif Ekind (R_Type) = E_Access_Subprogram_Type
3363 and then not Has_Foreign_Convention (R_Type)
3364 and then not Has_Warnings_Off (E)
3365 and then not Has_Warnings_Off (R_Type)
3366 then
3367 Error_Msg_N ("?x?& should return a foreign "
3368 & "convention subprogram pointer", E);
3369 Error_Msg_Sloc := Sloc (R_Type);
3370 Error_Msg_NE
3371 ("\?x?add Convention pragma to declaration of& #",
3372 E, R_Type);
3373 end if;
3374 end if;
3376 -- Give warning for suspicious return of a result of an
3377 -- unconstrained array type in a foreign convention function.
3379 if Has_Foreign_Convention (E)
3381 -- We are looking for a return of unconstrained array
3383 and then Is_Array_Type (R_Type)
3384 and then not Is_Constrained (R_Type)
3386 -- Exclude imported routines, the warning does not belong on
3387 -- the import, but rather on the routine definition.
3389 and then not Is_Imported (E)
3391 -- Check that general warning is enabled, and that it is not
3392 -- suppressed for this particular case.
3394 and then Warn_On_Export_Import
3395 and then not Has_Warnings_Off (E)
3396 and then not Has_Warnings_Off (R_Type)
3397 then
3398 Error_Msg_N
3399 ("?x?foreign convention function& should not return "
3400 & "unconstrained array!", E);
3401 end if;
3402 end if;
3404 -- Check suspicious use of Import in pure unit (cases where the RM
3405 -- allows calls to be omitted).
3407 if Is_Imported (E)
3409 -- It might be suspicious if the compilation unit has the Pure
3410 -- aspect/pragma.
3412 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
3414 -- The RM allows omission of calls only in the case of
3415 -- library-level subprograms (see RM-10.2.1(18)).
3417 and then Is_Library_Level_Entity (E)
3419 -- Ignore internally generated entity. This happens in some cases
3420 -- of subprograms in specs, where we generate an implied body.
3422 and then Comes_From_Source (Import_Pragma (E))
3424 -- Assume run-time knows what it is doing
3426 and then not GNAT_Mode
3428 -- Assume explicit Pure_Function means import is pure
3430 and then not Has_Pragma_Pure_Function (E)
3432 -- Don't need warning in relaxed semantics mode
3434 and then not Relaxed_RM_Semantics
3436 -- Assume convention Intrinsic is OK, since this is specialized.
3437 -- This deals with the DEC unit current_exception.ads
3439 and then Convention (E) /= Convention_Intrinsic
3441 -- Assume that ASM interface knows what it is doing. This deals
3442 -- with e.g. unsigned.ads in the AAMP back end.
3444 and then Convention (E) /= Convention_Assembler
3445 then
3446 Error_Msg_N
3447 ("pragma Import in Pure unit??", Import_Pragma (E));
3448 Error_Msg_NE
3449 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
3450 Import_Pragma (E), E);
3451 end if;
3453 return True;
3454 end Freeze_Profile;
3456 ------------------------
3457 -- Freeze_Record_Type --
3458 ------------------------
3460 procedure Freeze_Record_Type (Rec : Entity_Id) is
3461 ADC : Node_Id;
3462 Comp : Entity_Id;
3463 IR : Node_Id;
3464 Prev : Entity_Id;
3466 Junk : Boolean;
3467 pragma Warnings (Off, Junk);
3469 Rec_Pushed : Boolean := False;
3470 -- Set True if the record type scope Rec has been pushed on the scope
3471 -- stack. Needed for the analysis of delayed aspects specified to the
3472 -- components of Rec.
3474 SSO_ADC : Node_Id;
3475 -- Scalar_Storage_Order attribute definition clause for the record
3477 Unplaced_Component : Boolean := False;
3478 -- Set True if we find at least one component with no component
3479 -- clause (used to warn about useless Pack pragmas).
3481 Placed_Component : Boolean := False;
3482 -- Set True if we find at least one component with a component
3483 -- clause (used to warn about useless Bit_Order pragmas, and also
3484 -- to detect cases where Implicit_Packing may have an effect).
3486 Aliased_Component : Boolean := False;
3487 -- Set True if we find at least one component which is aliased. This
3488 -- is used to prevent Implicit_Packing of the record, since packing
3489 -- cannot modify the size of alignment of an aliased component.
3491 SSO_ADC_Component : Boolean := False;
3492 -- Set True if we find at least one component whose type has a
3493 -- Scalar_Storage_Order attribute definition clause.
3495 All_Elem_Components : Boolean := True;
3496 -- Set False if we encounter a component of a composite type
3498 All_Sized_Components : Boolean := True;
3499 -- Set False if we encounter a component with unknown RM_Size
3501 All_Storage_Unit_Components : Boolean := True;
3502 -- Set False if we encounter a component of a composite type whose
3503 -- RM_Size is not a multiple of the storage unit.
3505 Elem_Component_Total_Esize : Uint := Uint_0;
3506 -- Accumulates total Esize values of all elementary components. Used
3507 -- for processing of Implicit_Packing.
3509 Sized_Component_Total_RM_Size : Uint := Uint_0;
3510 -- Accumulates total RM_Size values of all sized components. Used
3511 -- for processing of Implicit_Packing.
3513 function Check_Allocator (N : Node_Id) return Node_Id;
3514 -- If N is an allocator, possibly wrapped in one or more level of
3515 -- qualified expression(s), return the inner allocator node, else
3516 -- return Empty.
3518 procedure Check_Itype (Typ : Entity_Id);
3519 -- If the component subtype is an access to a constrained subtype of
3520 -- an already frozen type, make the subtype frozen as well. It might
3521 -- otherwise be frozen in the wrong scope, and a freeze node on
3522 -- subtype has no effect. Similarly, if the component subtype is a
3523 -- regular (not protected) access to subprogram, set the anonymous
3524 -- subprogram type to frozen as well, to prevent an out-of-scope
3525 -- freeze node at some eventual point of call. Protected operations
3526 -- are handled elsewhere.
3528 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3529 -- Make sure that all types mentioned in Discrete_Choices of the
3530 -- variants referenceed by the Variant_Part VP are frozen. This is
3531 -- a recursive routine to deal with nested variants.
3533 ---------------------
3534 -- Check_Allocator --
3535 ---------------------
3537 function Check_Allocator (N : Node_Id) return Node_Id is
3538 Inner : Node_Id;
3539 begin
3540 Inner := N;
3541 loop
3542 if Nkind (Inner) = N_Allocator then
3543 return Inner;
3544 elsif Nkind (Inner) = N_Qualified_Expression then
3545 Inner := Expression (Inner);
3546 else
3547 return Empty;
3548 end if;
3549 end loop;
3550 end Check_Allocator;
3552 -----------------
3553 -- Check_Itype --
3554 -----------------
3556 procedure Check_Itype (Typ : Entity_Id) is
3557 Desig : constant Entity_Id := Designated_Type (Typ);
3559 begin
3560 if not Is_Frozen (Desig)
3561 and then Is_Frozen (Base_Type (Desig))
3562 then
3563 Set_Is_Frozen (Desig);
3565 -- In addition, add an Itype_Reference to ensure that the
3566 -- access subtype is elaborated early enough. This cannot be
3567 -- done if the subtype may depend on discriminants.
3569 if Ekind (Comp) = E_Component
3570 and then Is_Itype (Etype (Comp))
3571 and then not Has_Discriminants (Rec)
3572 then
3573 IR := Make_Itype_Reference (Sloc (Comp));
3574 Set_Itype (IR, Desig);
3575 Add_To_Result (IR);
3576 end if;
3578 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
3579 and then Convention (Desig) /= Convention_Protected
3580 then
3581 Set_Is_Frozen (Desig);
3582 end if;
3583 end Check_Itype;
3585 ------------------------------------
3586 -- Freeze_Choices_In_Variant_Part --
3587 ------------------------------------
3589 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
3590 pragma Assert (Nkind (VP) = N_Variant_Part);
3592 Variant : Node_Id;
3593 Choice : Node_Id;
3594 CL : Node_Id;
3596 begin
3597 -- Loop through variants
3599 Variant := First_Non_Pragma (Variants (VP));
3600 while Present (Variant) loop
3602 -- Loop through choices, checking that all types are frozen
3604 Choice := First_Non_Pragma (Discrete_Choices (Variant));
3605 while Present (Choice) loop
3606 if Nkind (Choice) in N_Has_Etype
3607 and then Present (Etype (Choice))
3608 then
3609 Freeze_And_Append (Etype (Choice), N, Result);
3610 end if;
3612 Next_Non_Pragma (Choice);
3613 end loop;
3615 -- Check for nested variant part to process
3617 CL := Component_List (Variant);
3619 if not Null_Present (CL) then
3620 if Present (Variant_Part (CL)) then
3621 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
3622 end if;
3623 end if;
3625 Next_Non_Pragma (Variant);
3626 end loop;
3627 end Freeze_Choices_In_Variant_Part;
3629 -- Start of processing for Freeze_Record_Type
3631 begin
3632 -- Deal with delayed aspect specifications for components. The
3633 -- analysis of the aspect is required to be delayed to the freeze
3634 -- point, thus we analyze the pragma or attribute definition
3635 -- clause in the tree at this point. We also analyze the aspect
3636 -- specification node at the freeze point when the aspect doesn't
3637 -- correspond to pragma/attribute definition clause.
3639 Comp := First_Entity (Rec);
3640 while Present (Comp) loop
3641 if Ekind (Comp) = E_Component
3642 and then Has_Delayed_Aspects (Comp)
3643 then
3644 if not Rec_Pushed then
3645 Push_Scope (Rec);
3646 Rec_Pushed := True;
3648 -- The visibility to the discriminants must be restored in
3649 -- order to properly analyze the aspects.
3651 if Has_Discriminants (Rec) then
3652 Install_Discriminants (Rec);
3653 end if;
3654 end if;
3656 Analyze_Aspects_At_Freeze_Point (Comp);
3657 end if;
3659 Next_Entity (Comp);
3660 end loop;
3662 -- Pop the scope if Rec scope has been pushed on the scope stack
3663 -- during the delayed aspect analysis process.
3665 if Rec_Pushed then
3666 if Has_Discriminants (Rec) then
3667 Uninstall_Discriminants (Rec);
3668 end if;
3670 Pop_Scope;
3671 end if;
3673 -- Freeze components and embedded subtypes
3675 Comp := First_Entity (Rec);
3676 Prev := Empty;
3677 while Present (Comp) loop
3678 if Is_Aliased (Comp) then
3679 Aliased_Component := True;
3680 end if;
3682 -- Handle the component and discriminant case
3684 if Ekind_In (Comp, E_Component, E_Discriminant) then
3685 declare
3686 CC : constant Node_Id := Component_Clause (Comp);
3688 begin
3689 -- Freezing a record type freezes the type of each of its
3690 -- components. However, if the type of the component is
3691 -- part of this record, we do not want or need a separate
3692 -- Freeze_Node. Note that Is_Itype is wrong because that's
3693 -- also set in private type cases. We also can't check for
3694 -- the Scope being exactly Rec because of private types and
3695 -- record extensions.
3697 if Is_Itype (Etype (Comp))
3698 and then Is_Record_Type (Underlying_Type
3699 (Scope (Etype (Comp))))
3700 then
3701 Undelay_Type (Etype (Comp));
3702 end if;
3704 Freeze_And_Append (Etype (Comp), N, Result);
3706 -- Warn for pragma Pack overriding foreign convention
3708 if Has_Foreign_Convention (Etype (Comp))
3709 and then Has_Pragma_Pack (Rec)
3711 -- Don't warn for aliased components, since override
3712 -- cannot happen in that case.
3714 and then not Is_Aliased (Comp)
3715 then
3716 declare
3717 CN : constant Name_Id :=
3718 Get_Convention_Name (Convention (Etype (Comp)));
3719 PP : constant Node_Id :=
3720 Get_Pragma (Rec, Pragma_Pack);
3721 begin
3722 if Present (PP) then
3723 Error_Msg_Name_1 := CN;
3724 Error_Msg_Sloc := Sloc (Comp);
3725 Error_Msg_N
3726 ("pragma Pack affects convention % component#??",
3727 PP);
3728 Error_Msg_Name_1 := CN;
3729 Error_Msg_NE
3730 ("\component & may not have % compatible "
3731 & "representation??", PP, Comp);
3732 end if;
3733 end;
3734 end if;
3736 -- Check for error of component clause given for variable
3737 -- sized type. We have to delay this test till this point,
3738 -- since the component type has to be frozen for us to know
3739 -- if it is variable length.
3741 if Present (CC) then
3742 Placed_Component := True;
3744 -- We omit this test in a generic context, it will be
3745 -- applied at instantiation time.
3747 if Inside_A_Generic then
3748 null;
3750 -- Also omit this test in CodePeer mode, since we do not
3751 -- have sufficient info on size and rep clauses.
3753 elsif CodePeer_Mode then
3754 null;
3756 -- Omit check if component has a generic type. This can
3757 -- happen in an instantiation within a generic in ASIS
3758 -- mode, where we force freeze actions without full
3759 -- expansion.
3761 elsif Is_Generic_Type (Etype (Comp)) then
3762 null;
3764 -- Do the check
3766 elsif not
3767 Size_Known_At_Compile_Time
3768 (Underlying_Type (Etype (Comp)))
3769 then
3770 Error_Msg_N
3771 ("component clause not allowed for variable " &
3772 "length component", CC);
3773 end if;
3775 else
3776 Unplaced_Component := True;
3777 end if;
3779 -- Case of component requires byte alignment
3781 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
3783 -- Set the enclosing record to also require byte align
3785 Set_Must_Be_On_Byte_Boundary (Rec);
3787 -- Check for component clause that is inconsistent with
3788 -- the required byte boundary alignment.
3790 if Present (CC)
3791 and then Normalized_First_Bit (Comp) mod
3792 System_Storage_Unit /= 0
3793 then
3794 Error_Msg_N
3795 ("component & must be byte aligned",
3796 Component_Name (Component_Clause (Comp)));
3797 end if;
3798 end if;
3799 end;
3800 end if;
3802 -- Gather data for possible Implicit_Packing later. Note that at
3803 -- this stage we might be dealing with a real component, or with
3804 -- an implicit subtype declaration.
3806 if Known_Static_RM_Size (Etype (Comp)) then
3807 Sized_Component_Total_RM_Size :=
3808 Sized_Component_Total_RM_Size + RM_Size (Etype (Comp));
3810 if Is_Elementary_Type (Etype (Comp)) then
3811 Elem_Component_Total_Esize :=
3812 Elem_Component_Total_Esize + Esize (Etype (Comp));
3813 else
3814 All_Elem_Components := False;
3816 if RM_Size (Etype (Comp)) mod System_Storage_Unit /= 0 then
3817 All_Storage_Unit_Components := False;
3818 end if;
3819 end if;
3820 else
3821 All_Sized_Components := False;
3822 end if;
3824 -- If the component is an Itype with Delayed_Freeze and is either
3825 -- a record or array subtype and its base type has not yet been
3826 -- frozen, we must remove this from the entity list of this record
3827 -- and put it on the entity list of the scope of its base type.
3828 -- Note that we know that this is not the type of a component
3829 -- since we cleared Has_Delayed_Freeze for it in the previous
3830 -- loop. Thus this must be the Designated_Type of an access type,
3831 -- which is the type of a component.
3833 if Is_Itype (Comp)
3834 and then Is_Type (Scope (Comp))
3835 and then Is_Composite_Type (Comp)
3836 and then Base_Type (Comp) /= Comp
3837 and then Has_Delayed_Freeze (Comp)
3838 and then not Is_Frozen (Base_Type (Comp))
3839 then
3840 declare
3841 Will_Be_Frozen : Boolean := False;
3842 S : Entity_Id;
3844 begin
3845 -- We have a difficult case to handle here. Suppose Rec is
3846 -- subtype being defined in a subprogram that's created as
3847 -- part of the freezing of Rec'Base. In that case, we know
3848 -- that Comp'Base must have already been frozen by the time
3849 -- we get to elaborate this because Gigi doesn't elaborate
3850 -- any bodies until it has elaborated all of the declarative
3851 -- part. But Is_Frozen will not be set at this point because
3852 -- we are processing code in lexical order.
3854 -- We detect this case by going up the Scope chain of Rec
3855 -- and seeing if we have a subprogram scope before reaching
3856 -- the top of the scope chain or that of Comp'Base. If we
3857 -- do, then mark that Comp'Base will actually be frozen. If
3858 -- so, we merely undelay it.
3860 S := Scope (Rec);
3861 while Present (S) loop
3862 if Is_Subprogram (S) then
3863 Will_Be_Frozen := True;
3864 exit;
3865 elsif S = Scope (Base_Type (Comp)) then
3866 exit;
3867 end if;
3869 S := Scope (S);
3870 end loop;
3872 if Will_Be_Frozen then
3873 Undelay_Type (Comp);
3875 else
3876 if Present (Prev) then
3877 Set_Next_Entity (Prev, Next_Entity (Comp));
3878 else
3879 Set_First_Entity (Rec, Next_Entity (Comp));
3880 end if;
3882 -- Insert in entity list of scope of base type (which
3883 -- must be an enclosing scope, because still unfrozen).
3885 Append_Entity (Comp, Scope (Base_Type (Comp)));
3886 end if;
3887 end;
3889 -- If the component is an access type with an allocator as default
3890 -- value, the designated type will be frozen by the corresponding
3891 -- expression in init_proc. In order to place the freeze node for
3892 -- the designated type before that for the current record type,
3893 -- freeze it now.
3895 -- Same process if the component is an array of access types,
3896 -- initialized with an aggregate. If the designated type is
3897 -- private, it cannot contain allocators, and it is premature
3898 -- to freeze the type, so we check for this as well.
3900 elsif Is_Access_Type (Etype (Comp))
3901 and then Present (Parent (Comp))
3902 and then Present (Expression (Parent (Comp)))
3903 then
3904 declare
3905 Alloc : constant Node_Id :=
3906 Check_Allocator (Expression (Parent (Comp)));
3908 begin
3909 if Present (Alloc) then
3911 -- If component is pointer to a class-wide type, freeze
3912 -- the specific type in the expression being allocated.
3913 -- The expression may be a subtype indication, in which
3914 -- case freeze the subtype mark.
3916 if Is_Class_Wide_Type
3917 (Designated_Type (Etype (Comp)))
3918 then
3919 if Is_Entity_Name (Expression (Alloc)) then
3920 Freeze_And_Append
3921 (Entity (Expression (Alloc)), N, Result);
3923 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
3924 then
3925 Freeze_And_Append
3926 (Entity (Subtype_Mark (Expression (Alloc))),
3927 N, Result);
3928 end if;
3930 elsif Is_Itype (Designated_Type (Etype (Comp))) then
3931 Check_Itype (Etype (Comp));
3933 else
3934 Freeze_And_Append
3935 (Designated_Type (Etype (Comp)), N, Result);
3936 end if;
3937 end if;
3938 end;
3940 elsif Is_Access_Type (Etype (Comp))
3941 and then Is_Itype (Designated_Type (Etype (Comp)))
3942 then
3943 Check_Itype (Etype (Comp));
3945 -- Freeze the designated type when initializing a component with
3946 -- an aggregate in case the aggregate contains allocators.
3948 -- type T is ...;
3949 -- type T_Ptr is access all T;
3950 -- type T_Array is array ... of T_Ptr;
3952 -- type Rec is record
3953 -- Comp : T_Array := (others => ...);
3954 -- end record;
3956 elsif Is_Array_Type (Etype (Comp))
3957 and then Is_Access_Type (Component_Type (Etype (Comp)))
3958 then
3959 declare
3960 Comp_Par : constant Node_Id := Parent (Comp);
3961 Desig_Typ : constant Entity_Id :=
3962 Designated_Type
3963 (Component_Type (Etype (Comp)));
3965 begin
3966 -- The only case when this sort of freezing is not done is
3967 -- when the designated type is class-wide and the root type
3968 -- is the record owning the component. This scenario results
3969 -- in a circularity because the class-wide type requires
3970 -- primitives that have not been created yet as the root
3971 -- type is in the process of being frozen.
3973 -- type Rec is tagged;
3974 -- type Rec_Ptr is access all Rec'Class;
3975 -- type Rec_Array is array ... of Rec_Ptr;
3977 -- type Rec is record
3978 -- Comp : Rec_Array := (others => ...);
3979 -- end record;
3981 if Is_Class_Wide_Type (Desig_Typ)
3982 and then Root_Type (Desig_Typ) = Rec
3983 then
3984 null;
3986 elsif Is_Fully_Defined (Desig_Typ)
3987 and then Present (Comp_Par)
3988 and then Nkind (Comp_Par) = N_Component_Declaration
3989 and then Present (Expression (Comp_Par))
3990 and then Nkind (Expression (Comp_Par)) = N_Aggregate
3991 then
3992 Freeze_And_Append (Desig_Typ, N, Result);
3993 end if;
3994 end;
3995 end if;
3997 Prev := Comp;
3998 Next_Entity (Comp);
3999 end loop;
4001 SSO_ADC :=
4002 Get_Attribute_Definition_Clause
4003 (Rec, Attribute_Scalar_Storage_Order);
4005 -- If the record type has Complex_Representation, then it is treated
4006 -- as a scalar in the back end so the storage order is irrelevant.
4008 if Has_Complex_Representation (Rec) then
4009 if Present (SSO_ADC) then
4010 Error_Msg_N
4011 ("??storage order has no effect with Complex_Representation",
4012 SSO_ADC);
4013 end if;
4015 else
4016 -- Deal with default setting of reverse storage order
4018 Set_SSO_From_Default (Rec);
4020 -- Check consistent attribute setting on component types
4022 declare
4023 Comp_ADC_Present : Boolean;
4024 begin
4025 Comp := First_Component (Rec);
4026 while Present (Comp) loop
4027 Check_Component_Storage_Order
4028 (Encl_Type => Rec,
4029 Comp => Comp,
4030 ADC => SSO_ADC,
4031 Comp_ADC_Present => Comp_ADC_Present);
4032 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
4033 Next_Component (Comp);
4034 end loop;
4035 end;
4037 -- Now deal with reverse storage order/bit order issues
4039 if Present (SSO_ADC) then
4041 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
4042 -- if the former is specified.
4044 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
4046 -- Note: report error on Rec, not on SSO_ADC, as ADC may
4047 -- apply to some ancestor type.
4049 Error_Msg_Sloc := Sloc (SSO_ADC);
4050 Error_Msg_N
4051 ("scalar storage order for& specified# inconsistent with "
4052 & "bit order", Rec);
4053 end if;
4055 -- Warn if there is a Scalar_Storage_Order attribute definition
4056 -- clause but no component clause, no component that itself has
4057 -- such an attribute definition, and no pragma Pack.
4059 if not (Placed_Component
4060 or else
4061 SSO_ADC_Component
4062 or else
4063 Is_Packed (Rec))
4064 then
4065 Error_Msg_N
4066 ("??scalar storage order specified but no component "
4067 & "clause", SSO_ADC);
4068 end if;
4069 end if;
4070 end if;
4072 -- Deal with Bit_Order aspect
4074 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
4076 if Present (ADC) and then Base_Type (Rec) = Rec then
4077 if not (Placed_Component
4078 or else Present (SSO_ADC)
4079 or else Is_Packed (Rec))
4080 then
4081 -- Warn if clause has no effect when no component clause is
4082 -- present, but suppress warning if the Bit_Order is required
4083 -- due to the presence of a Scalar_Storage_Order attribute.
4085 Error_Msg_N
4086 ("??bit order specification has no effect", ADC);
4087 Error_Msg_N
4088 ("\??since no component clauses were specified", ADC);
4090 -- Here is where we do the processing to adjust component clauses
4091 -- for reversed bit order, when not using reverse SSO.
4093 elsif Reverse_Bit_Order (Rec)
4094 and then not Reverse_Storage_Order (Rec)
4095 then
4096 Adjust_Record_For_Reverse_Bit_Order (Rec);
4098 -- Case where we have both an explicit Bit_Order and the same
4099 -- Scalar_Storage_Order: leave record untouched, the back-end
4100 -- will take care of required layout conversions.
4102 else
4103 null;
4105 end if;
4106 end if;
4108 -- Complete error checking on record representation clause (e.g.
4109 -- overlap of components). This is called after adjusting the
4110 -- record for reverse bit order.
4112 declare
4113 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
4114 begin
4115 if Present (RRC) then
4116 Check_Record_Representation_Clause (RRC);
4117 end if;
4118 end;
4120 -- Set OK_To_Reorder_Components depending on debug flags
4122 if Is_Base_Type (Rec) and then Convention (Rec) = Convention_Ada then
4123 if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
4124 or else
4125 (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
4126 then
4127 Set_OK_To_Reorder_Components (Rec);
4128 end if;
4129 end if;
4131 -- Check for useless pragma Pack when all components placed. We only
4132 -- do this check for record types, not subtypes, since a subtype may
4133 -- have all its components placed, and it still makes perfectly good
4134 -- sense to pack other subtypes or the parent type. We do not give
4135 -- this warning if Optimize_Alignment is set to Space, since the
4136 -- pragma Pack does have an effect in this case (it always resets
4137 -- the alignment to one).
4139 if Ekind (Rec) = E_Record_Type
4140 and then Is_Packed (Rec)
4141 and then not Unplaced_Component
4142 and then Optimize_Alignment /= 'S'
4143 then
4144 -- Reset packed status. Probably not necessary, but we do it so
4145 -- that there is no chance of the back end doing something strange
4146 -- with this redundant indication of packing.
4148 Set_Is_Packed (Rec, False);
4150 -- Give warning if redundant constructs warnings on
4152 if Warn_On_Redundant_Constructs then
4153 Error_Msg_N -- CODEFIX
4154 ("??pragma Pack has no effect, no unplaced components",
4155 Get_Rep_Pragma (Rec, Name_Pack));
4156 end if;
4157 end if;
4159 -- If this is the record corresponding to a remote type, freeze the
4160 -- remote type here since that is what we are semantically freezing.
4161 -- This prevents the freeze node for that type in an inner scope.
4163 if Ekind (Rec) = E_Record_Type then
4164 if Present (Corresponding_Remote_Type (Rec)) then
4165 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
4166 end if;
4168 -- Check for controlled components and unchecked unions.
4170 Comp := First_Component (Rec);
4171 while Present (Comp) loop
4173 -- Do not set Has_Controlled_Component on a class-wide
4174 -- equivalent type. See Make_CW_Equivalent_Type.
4176 if not Is_Class_Wide_Equivalent_Type (Rec)
4177 and then
4178 (Has_Controlled_Component (Etype (Comp))
4179 or else
4180 (Chars (Comp) /= Name_uParent
4181 and then Is_Controlled_Active (Etype (Comp)))
4182 or else
4183 (Is_Protected_Type (Etype (Comp))
4184 and then
4185 Present (Corresponding_Record_Type (Etype (Comp)))
4186 and then
4187 Has_Controlled_Component
4188 (Corresponding_Record_Type (Etype (Comp)))))
4189 then
4190 Set_Has_Controlled_Component (Rec);
4191 end if;
4193 if Has_Unchecked_Union (Etype (Comp)) then
4194 Set_Has_Unchecked_Union (Rec);
4195 end if;
4197 -- Scan component declaration for likely misuses of current
4198 -- instance, either in a constraint or a default expression.
4200 if Has_Per_Object_Constraint (Comp) then
4201 Check_Current_Instance (Parent (Comp));
4202 end if;
4204 Next_Component (Comp);
4205 end loop;
4206 end if;
4208 -- Enforce the restriction that access attributes with a current
4209 -- instance prefix can only apply to limited types. This comment
4210 -- is floating here, but does not seem to belong here???
4212 -- Set component alignment if not otherwise already set
4214 Set_Component_Alignment_If_Not_Set (Rec);
4216 -- For first subtypes, check if there are any fixed-point fields with
4217 -- component clauses, where we must check the size. This is not done
4218 -- till the freeze point since for fixed-point types, we do not know
4219 -- the size until the type is frozen. Similar processing applies to
4220 -- bit packed arrays.
4222 if Is_First_Subtype (Rec) then
4223 Comp := First_Component (Rec);
4224 while Present (Comp) loop
4225 if Present (Component_Clause (Comp))
4226 and then (Is_Fixed_Point_Type (Etype (Comp))
4227 or else Is_Bit_Packed_Array (Etype (Comp)))
4228 then
4229 Check_Size
4230 (Component_Name (Component_Clause (Comp)),
4231 Etype (Comp),
4232 Esize (Comp),
4233 Junk);
4234 end if;
4236 Next_Component (Comp);
4237 end loop;
4238 end if;
4240 -- Generate warning for applying C or C++ convention to a record
4241 -- with discriminants. This is suppressed for the unchecked union
4242 -- case, since the whole point in this case is interface C. We also
4243 -- do not generate this within instantiations, since we will have
4244 -- generated a message on the template.
4246 if Has_Discriminants (E)
4247 and then not Is_Unchecked_Union (E)
4248 and then (Convention (E) = Convention_C
4249 or else
4250 Convention (E) = Convention_CPP)
4251 and then Comes_From_Source (E)
4252 and then not In_Instance
4253 and then not Has_Warnings_Off (E)
4254 and then not Has_Warnings_Off (Base_Type (E))
4255 then
4256 declare
4257 Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
4258 A2 : Node_Id;
4260 begin
4261 if Present (Cprag) then
4262 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
4264 if Convention (E) = Convention_C then
4265 Error_Msg_N
4266 ("?x?variant record has no direct equivalent in C",
4267 A2);
4268 else
4269 Error_Msg_N
4270 ("?x?variant record has no direct equivalent in C++",
4271 A2);
4272 end if;
4274 Error_Msg_NE
4275 ("\?x?use of convention for type& is dubious", A2, E);
4276 end if;
4277 end;
4278 end if;
4280 -- See if Size is too small as is (and implicit packing might help)
4282 if not Is_Packed (Rec)
4284 -- No implicit packing if even one component is explicitly placed
4286 and then not Placed_Component
4288 -- Or even one component is aliased
4290 and then not Aliased_Component
4292 -- Must have size clause and all sized components
4294 and then Has_Size_Clause (Rec)
4295 and then All_Sized_Components
4297 -- Do not try implicit packing on records with discriminants, too
4298 -- complicated, especially in the variant record case.
4300 and then not Has_Discriminants (Rec)
4302 -- We want to implicitly pack if the specified size of the record
4303 -- is less than the sum of the object sizes (no point in packing
4304 -- if this is not the case) if we can compute it, i.e. if we have
4305 -- only elementary components. Otherwise, we have at least one
4306 -- composite component and we want to implicit pack only if bit
4307 -- packing is required for it, as we are sure in this case that
4308 -- the back end cannot do the expected layout without packing.
4310 and then ((All_Elem_Components
4311 and then RM_Size (Rec) < Elem_Component_Total_Esize)
4312 or else (not All_Elem_Components
4313 and then not All_Storage_Unit_Components))
4315 -- And the total RM size cannot be greater than the specified size
4316 -- since otherwise packing will not get us where we have to be.
4318 and then RM_Size (Rec) >= Sized_Component_Total_RM_Size
4320 -- Never do implicit packing in CodePeer or SPARK modes since
4321 -- we don't do any packing in these modes, since this generates
4322 -- over-complex code that confuses static analysis, and in
4323 -- general, neither CodePeer not GNATprove care about the
4324 -- internal representation of objects.
4326 and then not (CodePeer_Mode or GNATprove_Mode)
4327 then
4328 -- If implicit packing enabled, do it
4330 if Implicit_Packing then
4331 Set_Is_Packed (Rec);
4333 -- Otherwise flag the size clause
4335 else
4336 declare
4337 Sz : constant Node_Id := Size_Clause (Rec);
4338 begin
4339 Error_Msg_NE -- CODEFIX
4340 ("size given for& too small", Sz, Rec);
4341 Error_Msg_N -- CODEFIX
4342 ("\use explicit pragma Pack "
4343 & "or use pragma Implicit_Packing", Sz);
4344 end;
4345 end if;
4346 end if;
4348 -- The following checks are relevant only when SPARK_Mode is on as
4349 -- they are not standard Ada legality rules.
4351 if SPARK_Mode = On then
4352 if Is_Effectively_Volatile (Rec) then
4354 -- A discriminated type cannot be effectively volatile
4355 -- (SPARK RM C.6(4)).
4357 if Has_Discriminants (Rec) then
4358 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
4360 -- A tagged type cannot be effectively volatile
4361 -- (SPARK RM C.6(5)).
4363 elsif Is_Tagged_Type (Rec) then
4364 Error_Msg_N ("tagged type & cannot be volatile", Rec);
4365 end if;
4367 -- A non-effectively volatile record type cannot contain
4368 -- effectively volatile components (SPARK RM C.6(2)).
4370 else
4371 Comp := First_Component (Rec);
4372 while Present (Comp) loop
4373 if Comes_From_Source (Comp)
4374 and then Is_Effectively_Volatile (Etype (Comp))
4375 then
4376 Error_Msg_Name_1 := Chars (Rec);
4377 Error_Msg_N
4378 ("component & of non-volatile type % cannot be "
4379 & "volatile", Comp);
4380 end if;
4382 Next_Component (Comp);
4383 end loop;
4384 end if;
4386 -- A type which does not yield a synchronized object cannot have
4387 -- a component that yields a synchronized object (SPARK RM 9.5).
4389 if not Yields_Synchronized_Object (Rec) then
4390 Comp := First_Component (Rec);
4391 while Present (Comp) loop
4392 if Comes_From_Source (Comp)
4393 and then Yields_Synchronized_Object (Etype (Comp))
4394 then
4395 Error_Msg_Name_1 := Chars (Rec);
4396 Error_Msg_N
4397 ("component & of non-synchronized type % cannot be "
4398 & "synchronized", Comp);
4399 end if;
4401 Next_Component (Comp);
4402 end loop;
4403 end if;
4405 -- A Ghost type cannot have a component of protected or task type
4406 -- (SPARK RM 6.9(19)).
4408 if Is_Ghost_Entity (Rec) then
4409 Comp := First_Component (Rec);
4410 while Present (Comp) loop
4411 if Comes_From_Source (Comp)
4412 and then Is_Concurrent_Type (Etype (Comp))
4413 then
4414 Error_Msg_Name_1 := Chars (Rec);
4415 Error_Msg_N
4416 ("component & of ghost type % cannot be concurrent",
4417 Comp);
4418 end if;
4420 Next_Component (Comp);
4421 end loop;
4422 end if;
4423 end if;
4425 -- Make sure that if we have an iterator aspect, then we have
4426 -- either Constant_Indexing or Variable_Indexing.
4428 declare
4429 Iterator_Aspect : Node_Id;
4431 begin
4432 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
4434 if No (Iterator_Aspect) then
4435 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
4436 end if;
4438 if Present (Iterator_Aspect) then
4439 if Has_Aspect (Rec, Aspect_Constant_Indexing)
4440 or else
4441 Has_Aspect (Rec, Aspect_Variable_Indexing)
4442 then
4443 null;
4444 else
4445 Error_Msg_N
4446 ("Iterator_Element requires indexing aspect",
4447 Iterator_Aspect);
4448 end if;
4449 end if;
4450 end;
4452 -- All done if not a full record definition
4454 if Ekind (Rec) /= E_Record_Type then
4455 return;
4456 end if;
4458 -- Finally we need to check the variant part to make sure that
4459 -- all types within choices are properly frozen as part of the
4460 -- freezing of the record type.
4462 Check_Variant_Part : declare
4463 D : constant Node_Id := Declaration_Node (Rec);
4464 T : Node_Id;
4465 C : Node_Id;
4467 begin
4468 -- Find component list
4470 C := Empty;
4472 if Nkind (D) = N_Full_Type_Declaration then
4473 T := Type_Definition (D);
4475 if Nkind (T) = N_Record_Definition then
4476 C := Component_List (T);
4478 elsif Nkind (T) = N_Derived_Type_Definition
4479 and then Present (Record_Extension_Part (T))
4480 then
4481 C := Component_List (Record_Extension_Part (T));
4482 end if;
4483 end if;
4485 -- Case of variant part present
4487 if Present (C) and then Present (Variant_Part (C)) then
4488 Freeze_Choices_In_Variant_Part (Variant_Part (C));
4489 end if;
4491 -- Note: we used to call Check_Choices here, but it is too early,
4492 -- since predicated subtypes are frozen here, but their freezing
4493 -- actions are in Analyze_Freeze_Entity, which has not been called
4494 -- yet for entities frozen within this procedure, so we moved that
4495 -- call to the Analyze_Freeze_Entity for the record type.
4497 end Check_Variant_Part;
4499 -- Check that all the primitives of an interface type are abstract
4500 -- or null procedures.
4502 if Is_Interface (Rec)
4503 and then not Error_Posted (Parent (Rec))
4504 then
4505 declare
4506 Elmt : Elmt_Id;
4507 Subp : Entity_Id;
4509 begin
4510 Elmt := First_Elmt (Primitive_Operations (Rec));
4511 while Present (Elmt) loop
4512 Subp := Node (Elmt);
4514 if not Is_Abstract_Subprogram (Subp)
4516 -- Avoid reporting the error on inherited primitives
4518 and then Comes_From_Source (Subp)
4519 then
4520 Error_Msg_Name_1 := Chars (Subp);
4522 if Ekind (Subp) = E_Procedure then
4523 if not Null_Present (Parent (Subp)) then
4524 Error_Msg_N
4525 ("interface procedure % must be abstract or null",
4526 Parent (Subp));
4527 end if;
4528 else
4529 Error_Msg_N
4530 ("interface function % must be abstract",
4531 Parent (Subp));
4532 end if;
4533 end if;
4535 Next_Elmt (Elmt);
4536 end loop;
4537 end;
4538 end if;
4539 end Freeze_Record_Type;
4541 -------------------------------
4542 -- Has_Boolean_Aspect_Import --
4543 -------------------------------
4545 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4546 Decl : constant Node_Id := Declaration_Node (E);
4547 Asp : Node_Id;
4548 Expr : Node_Id;
4550 begin
4551 if Has_Aspects (Decl) then
4552 Asp := First (Aspect_Specifications (Decl));
4553 while Present (Asp) loop
4554 Expr := Expression (Asp);
4556 -- The value of aspect Import is True when the expression is
4557 -- either missing or it is explicitly set to True.
4559 if Get_Aspect_Id (Asp) = Aspect_Import
4560 and then (No (Expr)
4561 or else (Compile_Time_Known_Value (Expr)
4562 and then Is_True (Expr_Value (Expr))))
4563 then
4564 return True;
4565 end if;
4567 Next (Asp);
4568 end loop;
4569 end if;
4571 return False;
4572 end Has_Boolean_Aspect_Import;
4574 ---------------------
4575 -- New_Freeze_Node --
4576 ---------------------
4578 function New_Freeze_Node return Node_Id is
4579 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4580 Result : Node_Id;
4582 begin
4583 -- Handle the case where an ignored Ghost subprogram freezes the type
4584 -- of one of its formals. The type can either be non-Ghost or checked
4585 -- Ghost. Since the freeze node for the type is generated in the
4586 -- context of the subprogram, the node will be incorrectly flagged as
4587 -- ignored Ghost and erroneously removed from the tree.
4589 -- type Typ is ...;
4590 -- procedure Ignored_Ghost_Proc (Formal : Typ) with Ghost;
4592 -- Reset the Ghost mode to "none". This preserves the freeze node.
4594 if Ghost_Mode = Ignore
4595 and then not Is_Ignored_Ghost_Entity (E)
4596 and then not Is_Ignored_Ghost_Node (E)
4597 then
4598 Ghost_Mode := None;
4599 end if;
4601 Result := New_Node (N_Freeze_Entity, Loc);
4603 Ghost_Mode := Save_Ghost_Mode;
4604 return Result;
4605 end New_Freeze_Node;
4607 ------------------------------
4608 -- Wrap_Imported_Subprogram --
4609 ------------------------------
4611 -- The issue here is that our normal approach of checking preconditions
4612 -- and postconditions does not work for imported procedures, since we
4613 -- are not generating code for the body. To get around this we create
4614 -- a wrapper, as shown by the following example:
4616 -- procedure K (A : Integer);
4617 -- pragma Import (C, K);
4619 -- The spec is rewritten by removing the effects of pragma Import, but
4620 -- leaving the convention unchanged, as though the source had said:
4622 -- procedure K (A : Integer);
4623 -- pragma Convention (C, K);
4625 -- and we create a body, added to the entity K freeze actions, which
4626 -- looks like:
4628 -- procedure K (A : Integer) is
4629 -- procedure K (A : Integer);
4630 -- pragma Import (C, K);
4631 -- begin
4632 -- K (A);
4633 -- end K;
4635 -- Now the contract applies in the normal way to the outer procedure,
4636 -- and the inner procedure has no contracts, so there is no problem
4637 -- in just calling it to get the original effect.
4639 -- In the case of a function, we create an appropriate return statement
4640 -- for the subprogram body that calls the inner procedure.
4642 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
4643 function Copy_Import_Pragma return Node_Id;
4644 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
4646 ------------------------
4647 -- Copy_Import_Pragma --
4648 ------------------------
4650 function Copy_Import_Pragma return Node_Id is
4652 -- The subprogram should have an import pragma, otherwise it does
4653 -- need a wrapper.
4655 Prag : constant Node_Id := Import_Pragma (E);
4656 pragma Assert (Present (Prag));
4658 -- Save all semantic fields of the pragma
4660 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
4661 Save_From : constant Boolean := From_Aspect_Specification (Prag);
4662 Save_Prag : constant Node_Id := Next_Pragma (Prag);
4663 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
4665 Result : Node_Id;
4667 begin
4668 -- Reset all semantic fields. This avoids a potential infinite
4669 -- loop when the pragma comes from an aspect as the duplication
4670 -- will copy the aspect, then copy the corresponding pragma and
4671 -- so on.
4673 Set_Corresponding_Aspect (Prag, Empty);
4674 Set_From_Aspect_Specification (Prag, False);
4675 Set_Next_Pragma (Prag, Empty);
4676 Set_Next_Rep_Item (Prag, Empty);
4678 Result := Copy_Separate_Tree (Prag);
4680 -- Restore the original semantic fields
4682 Set_Corresponding_Aspect (Prag, Save_Asp);
4683 Set_From_Aspect_Specification (Prag, Save_From);
4684 Set_Next_Pragma (Prag, Save_Prag);
4685 Set_Next_Rep_Item (Prag, Save_Rep);
4687 return Result;
4688 end Copy_Import_Pragma;
4690 -- Local variables
4692 Loc : constant Source_Ptr := Sloc (E);
4693 CE : constant Name_Id := Chars (E);
4694 Bod : Node_Id;
4695 Forml : Entity_Id;
4696 Parms : List_Id;
4697 Prag : Node_Id;
4698 Spec : Node_Id;
4699 Stmt : Node_Id;
4701 -- Start of processing for Wrap_Imported_Subprogram
4703 begin
4704 -- Nothing to do if not imported
4706 if not Is_Imported (E) then
4707 return;
4709 -- Test enabling conditions for wrapping
4711 elsif Is_Subprogram (E)
4712 and then Present (Contract (E))
4713 and then Present (Pre_Post_Conditions (Contract (E)))
4714 and then not GNATprove_Mode
4715 then
4716 -- Here we do the wrap
4718 -- Note on calls to Copy_Separate_Tree. The trees we are copying
4719 -- here are fully analyzed, but we definitely want fully syntactic
4720 -- unanalyzed trees in the body we construct, so that the analysis
4721 -- generates the right visibility, and that is exactly what the
4722 -- calls to Copy_Separate_Tree give us.
4724 Prag := Copy_Import_Pragma;
4726 -- Fix up spec to be not imported any more
4728 Set_Has_Completion (E, False);
4729 Set_Import_Pragma (E, Empty);
4730 Set_Interface_Name (E, Empty);
4731 Set_Is_Imported (E, False);
4733 -- Grab the subprogram declaration and specification
4735 Spec := Declaration_Node (E);
4737 -- Build parameter list that we need
4739 Parms := New_List;
4740 Forml := First_Formal (E);
4741 while Present (Forml) loop
4742 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
4743 Next_Formal (Forml);
4744 end loop;
4746 -- Build the call
4748 if Ekind_In (E, E_Function, E_Generic_Function) then
4749 Stmt :=
4750 Make_Simple_Return_Statement (Loc,
4751 Expression =>
4752 Make_Function_Call (Loc,
4753 Name => Make_Identifier (Loc, CE),
4754 Parameter_Associations => Parms));
4756 else
4757 Stmt :=
4758 Make_Procedure_Call_Statement (Loc,
4759 Name => Make_Identifier (Loc, CE),
4760 Parameter_Associations => Parms);
4761 end if;
4763 -- Now build the body
4765 Bod :=
4766 Make_Subprogram_Body (Loc,
4767 Specification =>
4768 Copy_Separate_Tree (Spec),
4769 Declarations => New_List (
4770 Make_Subprogram_Declaration (Loc,
4771 Specification => Copy_Separate_Tree (Spec)),
4772 Prag),
4773 Handled_Statement_Sequence =>
4774 Make_Handled_Sequence_Of_Statements (Loc,
4775 Statements => New_List (Stmt),
4776 End_Label => Make_Identifier (Loc, CE)));
4778 -- Append the body to freeze result
4780 Add_To_Result (Bod);
4781 return;
4783 -- Case of imported subprogram that does not get wrapped
4785 else
4786 -- Set Is_Public. All imported entities need an external symbol
4787 -- created for them since they are always referenced from another
4788 -- object file. Note this used to be set when we set Is_Imported
4789 -- back in Sem_Prag, but now we delay it to this point, since we
4790 -- don't want to set this flag if we wrap an imported subprogram.
4792 Set_Is_Public (E);
4793 end if;
4794 end Wrap_Imported_Subprogram;
4796 -- Local variables
4798 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4800 -- Start of processing for Freeze_Entity
4802 begin
4803 -- The entity being frozen may be subject to pragma Ghost. Set the mode
4804 -- now to ensure that any nodes generated during freezing are properly
4805 -- flagged as Ghost.
4807 Set_Ghost_Mode_From_Entity (E);
4809 -- We are going to test for various reasons why this entity need not be
4810 -- frozen here, but in the case of an Itype that's defined within a
4811 -- record, that test actually applies to the record.
4813 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
4814 Test_E := Scope (E);
4815 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
4816 and then Is_Record_Type (Underlying_Type (Scope (E)))
4817 then
4818 Test_E := Underlying_Type (Scope (E));
4819 end if;
4821 -- Do not freeze if already frozen since we only need one freeze node
4823 if Is_Frozen (E) then
4824 Ghost_Mode := Save_Ghost_Mode;
4825 return No_List;
4827 -- It is improper to freeze an external entity within a generic because
4828 -- its freeze node will appear in a non-valid context. The entity will
4829 -- be frozen in the proper scope after the current generic is analyzed.
4830 -- However, aspects must be analyzed because they may be queried later
4831 -- within the generic itself, and the corresponding pragma or attribute
4832 -- definition has not been analyzed yet.
4834 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
4835 if Has_Delayed_Aspects (E) then
4836 Analyze_Aspects_At_Freeze_Point (E);
4837 end if;
4839 Ghost_Mode := Save_Ghost_Mode;
4840 return No_List;
4842 -- AI05-0213: A formal incomplete type does not freeze the actual. In
4843 -- the instance, the same applies to the subtype renaming the actual.
4845 elsif Is_Private_Type (E)
4846 and then Is_Generic_Actual_Type (E)
4847 and then No (Full_View (Base_Type (E)))
4848 and then Ada_Version >= Ada_2012
4849 then
4850 Ghost_Mode := Save_Ghost_Mode;
4851 return No_List;
4853 -- Formal subprograms are never frozen
4855 elsif Is_Formal_Subprogram (E) then
4856 Ghost_Mode := Save_Ghost_Mode;
4857 return No_List;
4859 -- Generic types are never frozen as they lack delayed semantic checks
4861 elsif Is_Generic_Type (E) then
4862 Ghost_Mode := Save_Ghost_Mode;
4863 return No_List;
4865 -- Do not freeze a global entity within an inner scope created during
4866 -- expansion. A call to subprogram E within some internal procedure
4867 -- (a stream attribute for example) might require freezing E, but the
4868 -- freeze node must appear in the same declarative part as E itself.
4869 -- The two-pass elaboration mechanism in gigi guarantees that E will
4870 -- be frozen before the inner call is elaborated. We exclude constants
4871 -- from this test, because deferred constants may be frozen early, and
4872 -- must be diagnosed (e.g. in the case of a deferred constant being used
4873 -- in a default expression). If the enclosing subprogram comes from
4874 -- source, or is a generic instance, then the freeze point is the one
4875 -- mandated by the language, and we freeze the entity. A subprogram that
4876 -- is a child unit body that acts as a spec does not have a spec that
4877 -- comes from source, but can only come from source.
4879 elsif In_Open_Scopes (Scope (Test_E))
4880 and then Scope (Test_E) /= Current_Scope
4881 and then Ekind (Test_E) /= E_Constant
4882 then
4883 declare
4884 S : Entity_Id;
4886 begin
4887 S := Current_Scope;
4888 while Present (S) loop
4889 if Is_Overloadable (S) then
4890 if Comes_From_Source (S)
4891 or else Is_Generic_Instance (S)
4892 or else Is_Child_Unit (S)
4893 then
4894 exit;
4895 else
4896 Ghost_Mode := Save_Ghost_Mode;
4897 return No_List;
4898 end if;
4899 end if;
4901 S := Scope (S);
4902 end loop;
4903 end;
4905 -- Similarly, an inlined instance body may make reference to global
4906 -- entities, but these references cannot be the proper freezing point
4907 -- for them, and in the absence of inlining freezing will take place in
4908 -- their own scope. Normally instance bodies are analyzed after the
4909 -- enclosing compilation, and everything has been frozen at the proper
4910 -- place, but with front-end inlining an instance body is compiled
4911 -- before the end of the enclosing scope, and as a result out-of-order
4912 -- freezing must be prevented.
4914 elsif Front_End_Inlining
4915 and then In_Instance_Body
4916 and then Present (Scope (Test_E))
4917 then
4918 declare
4919 S : Entity_Id;
4921 begin
4922 S := Scope (Test_E);
4923 while Present (S) loop
4924 if Is_Generic_Instance (S) then
4925 exit;
4926 else
4927 S := Scope (S);
4928 end if;
4929 end loop;
4931 if No (S) then
4932 Ghost_Mode := Save_Ghost_Mode;
4933 return No_List;
4934 end if;
4935 end;
4937 elsif Ekind (E) = E_Generic_Package then
4938 Result := Freeze_Generic_Entities (E);
4940 Ghost_Mode := Save_Ghost_Mode;
4941 return Result;
4942 end if;
4944 -- Add checks to detect proper initialization of scalars that may appear
4945 -- as subprogram parameters.
4947 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
4948 Apply_Parameter_Validity_Checks (E);
4949 end if;
4951 -- Deal with delayed aspect specifications. The analysis of the aspect
4952 -- is required to be delayed to the freeze point, thus we analyze the
4953 -- pragma or attribute definition clause in the tree at this point. We
4954 -- also analyze the aspect specification node at the freeze point when
4955 -- the aspect doesn't correspond to pragma/attribute definition clause.
4957 if Has_Delayed_Aspects (E) then
4958 Analyze_Aspects_At_Freeze_Point (E);
4959 end if;
4961 -- Here to freeze the entity
4963 Set_Is_Frozen (E);
4965 -- Case of entity being frozen is other than a type
4967 if not Is_Type (E) then
4969 -- If entity is exported or imported and does not have an external
4970 -- name, now is the time to provide the appropriate default name.
4971 -- Skip this if the entity is stubbed, since we don't need a name
4972 -- for any stubbed routine. For the case on intrinsics, if no
4973 -- external name is specified, then calls will be handled in
4974 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
4975 -- external name is provided, then Expand_Intrinsic_Call leaves
4976 -- calls in place for expansion by GIGI.
4978 if (Is_Imported (E) or else Is_Exported (E))
4979 and then No (Interface_Name (E))
4980 and then Convention (E) /= Convention_Stubbed
4981 and then Convention (E) /= Convention_Intrinsic
4982 then
4983 Set_Encoded_Interface_Name
4984 (E, Get_Default_External_Name (E));
4986 -- If entity is an atomic object appearing in a declaration and
4987 -- the expression is an aggregate, assign it to a temporary to
4988 -- ensure that the actual assignment is done atomically rather
4989 -- than component-wise (the assignment to the temp may be done
4990 -- component-wise, but that is harmless).
4992 elsif Is_Atomic_Or_VFA (E)
4993 and then Nkind (Parent (E)) = N_Object_Declaration
4994 and then Present (Expression (Parent (E)))
4995 and then Nkind (Expression (Parent (E))) = N_Aggregate
4996 and then Is_Atomic_VFA_Aggregate (Expression (Parent (E)))
4997 then
4998 null;
4999 end if;
5001 -- Subprogram case
5003 if Is_Subprogram (E) then
5005 -- Check for needing to wrap imported subprogram
5007 Wrap_Imported_Subprogram (E);
5009 -- Freeze all parameter types and the return type (RM 13.14(14)).
5010 -- However skip this for internal subprograms. This is also where
5011 -- any extra formal parameters are created since we now know
5012 -- whether the subprogram will use a foreign convention.
5014 -- In Ada 2012, freezing a subprogram does not always freeze the
5015 -- corresponding profile (see AI05-019). An attribute reference
5016 -- is not a freezing point of the profile. Flag Do_Freeze_Profile
5017 -- indicates whether the profile should be frozen now.
5018 -- Other constructs that should not freeze ???
5020 -- This processing doesn't apply to internal entities (see below)
5022 -- Disable this mechanism for now, to fix regressions in ASIS and
5023 -- various ACATS tests. Implementation of AI05-019 remains
5024 -- unsolved ???
5026 if not Is_Internal (E)
5027 and then (Do_Freeze_Profile or else True)
5028 then
5029 if not Freeze_Profile (E) then
5030 Ghost_Mode := Save_Ghost_Mode;
5031 return Result;
5032 end if;
5033 end if;
5035 -- Must freeze its parent first if it is a derived subprogram
5037 if Present (Alias (E)) then
5038 Freeze_And_Append (Alias (E), N, Result);
5039 end if;
5041 -- We don't freeze internal subprograms, because we don't normally
5042 -- want addition of extra formals or mechanism setting to happen
5043 -- for those. However we do pass through predefined dispatching
5044 -- cases, since extra formals may be needed in some cases, such as
5045 -- for the stream 'Input function (build-in-place formals).
5047 if not Is_Internal (E)
5048 or else Is_Predefined_Dispatching_Operation (E)
5049 then
5050 Freeze_Subprogram (E);
5051 end if;
5053 -- If warning on suspicious contracts then check for the case of
5054 -- a postcondition other than False for a No_Return subprogram.
5056 if No_Return (E)
5057 and then Warn_On_Suspicious_Contract
5058 and then Present (Contract (E))
5059 then
5060 declare
5061 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
5062 Exp : Node_Id;
5064 begin
5065 while Present (Prag) loop
5066 if Nam_In (Pragma_Name (Prag), Name_Post,
5067 Name_Postcondition,
5068 Name_Refined_Post)
5069 then
5070 Exp :=
5071 Expression
5072 (First (Pragma_Argument_Associations (Prag)));
5074 if Nkind (Exp) /= N_Identifier
5075 or else Chars (Exp) /= Name_False
5076 then
5077 Error_Msg_NE
5078 ("useless postcondition, & is marked "
5079 & "No_Return?T?", Exp, E);
5080 end if;
5081 end if;
5083 Prag := Next_Pragma (Prag);
5084 end loop;
5085 end;
5086 end if;
5088 -- Here for other than a subprogram or type
5090 else
5091 -- If entity has a type, and it is not a generic unit, then
5092 -- freeze it first (RM 13.14(10)).
5094 if Present (Etype (E))
5095 and then Ekind (E) /= E_Generic_Function
5096 then
5097 Freeze_And_Append (Etype (E), N, Result);
5099 -- For an object of an anonymous array type, aspects on the
5100 -- object declaration apply to the type itself. This is the
5101 -- case for Atomic_Components, Volatile_Components, and
5102 -- Independent_Components. In these cases analysis of the
5103 -- generated pragma will mark the anonymous types accordingly,
5104 -- and the object itself does not require a freeze node.
5106 if Ekind (E) = E_Variable
5107 and then Is_Itype (Etype (E))
5108 and then Is_Array_Type (Etype (E))
5109 and then Has_Delayed_Aspects (E)
5110 then
5111 Set_Has_Delayed_Aspects (E, False);
5112 Set_Has_Delayed_Freeze (E, False);
5113 Set_Freeze_Node (E, Empty);
5114 end if;
5115 end if;
5117 -- Special processing for objects created by object declaration
5119 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
5120 Freeze_Object_Declaration (E);
5121 end if;
5123 -- Check that a constant which has a pragma Volatile[_Components]
5124 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
5126 -- Note: Atomic[_Components] also sets Volatile[_Components]
5128 if Ekind (E) = E_Constant
5129 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
5130 and then not Is_Imported (E)
5131 and then not Has_Boolean_Aspect_Import (E)
5132 then
5133 -- Make sure we actually have a pragma, and have not merely
5134 -- inherited the indication from elsewhere (e.g. an address
5135 -- clause, which is not good enough in RM terms).
5137 if Has_Rep_Pragma (E, Name_Atomic)
5138 or else
5139 Has_Rep_Pragma (E, Name_Atomic_Components)
5140 then
5141 Error_Msg_N
5142 ("stand alone atomic constant must be " &
5143 "imported (RM C.6(13))", E);
5145 elsif Has_Rep_Pragma (E, Name_Volatile)
5146 or else
5147 Has_Rep_Pragma (E, Name_Volatile_Components)
5148 then
5149 Error_Msg_N
5150 ("stand alone volatile constant must be " &
5151 "imported (RM C.6(13))", E);
5152 end if;
5153 end if;
5155 -- Static objects require special handling
5157 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
5158 and then Is_Statically_Allocated (E)
5159 then
5160 Freeze_Static_Object (E);
5161 end if;
5163 -- Remaining step is to layout objects
5165 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
5166 or else Is_Formal (E)
5167 then
5168 Layout_Object (E);
5169 end if;
5171 -- For an object that does not have delayed freezing, and whose
5172 -- initialization actions have been captured in a compound
5173 -- statement, move them back now directly within the enclosing
5174 -- statement sequence.
5176 if Ekind_In (E, E_Constant, E_Variable)
5177 and then not Has_Delayed_Freeze (E)
5178 then
5179 Explode_Initialization_Compound_Statement (E);
5180 end if;
5181 end if;
5183 -- Case of a type or subtype being frozen
5185 else
5186 -- We used to check here that a full type must have preelaborable
5187 -- initialization if it completes a private type specified with
5188 -- pragma Preelaborable_Initialization, but that missed cases where
5189 -- the types occur within a generic package, since the freezing
5190 -- that occurs within a containing scope generally skips traversal
5191 -- of a generic unit's declarations (those will be frozen within
5192 -- instances). This check was moved to Analyze_Package_Specification.
5194 -- The type may be defined in a generic unit. This can occur when
5195 -- freezing a generic function that returns the type (which is
5196 -- defined in a parent unit). It is clearly meaningless to freeze
5197 -- this type. However, if it is a subtype, its size may be determi-
5198 -- nable and used in subsequent checks, so might as well try to
5199 -- compute it.
5201 -- In Ada 2012, Freeze_Entities is also used in the front end to
5202 -- trigger the analysis of aspect expressions, so in this case we
5203 -- want to continue the freezing process.
5205 if Present (Scope (E))
5206 and then Is_Generic_Unit (Scope (E))
5207 and then
5208 (not Has_Predicates (E)
5209 and then not Has_Delayed_Freeze (E))
5210 then
5211 Check_Compile_Time_Size (E);
5212 Ghost_Mode := Save_Ghost_Mode;
5213 return No_List;
5214 end if;
5216 -- Check for error of Type_Invariant'Class applied to an untagged
5217 -- type (check delayed to freeze time when full type is available).
5219 declare
5220 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
5221 begin
5222 if Present (Prag)
5223 and then Class_Present (Prag)
5224 and then not Is_Tagged_Type (E)
5225 then
5226 Error_Msg_NE
5227 ("Type_Invariant''Class cannot be specified for &",
5228 Prag, E);
5229 Error_Msg_N
5230 ("\can only be specified for a tagged type", Prag);
5231 end if;
5232 end;
5234 if Is_Ghost_Entity (E) then
5236 -- A Ghost type cannot be concurrent (SPARK RM 6.9(19)). Verify
5237 -- this legality rule first to five a finer-grained diagnostic.
5239 if Is_Concurrent_Type (E) then
5240 Error_Msg_N ("ghost type & cannot be concurrent", E);
5242 -- A Ghost type cannot be effectively volatile (SPARK RM 6.9(7))
5244 elsif Is_Effectively_Volatile (E) then
5245 Error_Msg_N ("ghost type & cannot be volatile", E);
5246 end if;
5247 end if;
5249 -- Deal with special cases of freezing for subtype
5251 if E /= Base_Type (E) then
5253 -- Before we do anything else, a specific test for the case of a
5254 -- size given for an array where the array would need to be packed
5255 -- in order for the size to be honored, but is not. This is the
5256 -- case where implicit packing may apply. The reason we do this so
5257 -- early is that, if we have implicit packing, the layout of the
5258 -- base type is affected, so we must do this before we freeze the
5259 -- base type.
5261 -- We could do this processing only if implicit packing is enabled
5262 -- since in all other cases, the error would be caught by the back
5263 -- end. However, we choose to do the check even if we do not have
5264 -- implicit packing enabled, since this allows us to give a more
5265 -- useful error message (advising use of pragma Implicit_Packing
5266 -- or pragma Pack).
5268 if Is_Array_Type (E) then
5269 declare
5270 Ctyp : constant Entity_Id := Component_Type (E);
5271 Rsiz : constant Uint := RM_Size (Ctyp);
5272 SZ : constant Node_Id := Size_Clause (E);
5273 Btyp : constant Entity_Id := Base_Type (E);
5275 Lo : Node_Id;
5276 Hi : Node_Id;
5277 Indx : Node_Id;
5279 Dim : Uint;
5280 Num_Elmts : Uint := Uint_1;
5281 -- Number of elements in array
5283 begin
5284 -- Check enabling conditions. These are straightforward
5285 -- except for the test for a limited composite type. This
5286 -- eliminates the rare case of a array of limited components
5287 -- where there are issues of whether or not we can go ahead
5288 -- and pack the array (since we can't freely pack and unpack
5289 -- arrays if they are limited).
5291 -- Note that we check the root type explicitly because the
5292 -- whole point is we are doing this test before we have had
5293 -- a chance to freeze the base type (and it is that freeze
5294 -- action that causes stuff to be inherited).
5296 -- The conditions on the size are identical to those used in
5297 -- Freeze_Array_Type to set the Is_Packed flag.
5299 if Has_Size_Clause (E)
5300 and then Known_Static_RM_Size (E)
5301 and then not Is_Packed (E)
5302 and then not Has_Pragma_Pack (E)
5303 and then not Has_Component_Size_Clause (E)
5304 and then Known_Static_RM_Size (Ctyp)
5305 and then Rsiz <= 64
5306 and then not (Addressable (Rsiz)
5307 and then Known_Static_Esize (Ctyp)
5308 and then Esize (Ctyp) = Rsiz)
5309 and then not (Rsiz mod System_Storage_Unit = 0
5310 and then Is_Composite_Type (Ctyp))
5311 and then not Is_Limited_Composite (E)
5312 and then not Is_Packed (Root_Type (E))
5313 and then not Has_Component_Size_Clause (Root_Type (E))
5314 and then not (CodePeer_Mode or GNATprove_Mode)
5315 then
5316 -- Compute number of elements in array
5318 Indx := First_Index (E);
5319 while Present (Indx) loop
5320 Get_Index_Bounds (Indx, Lo, Hi);
5322 if not (Compile_Time_Known_Value (Lo)
5323 and then
5324 Compile_Time_Known_Value (Hi))
5325 then
5326 goto No_Implicit_Packing;
5327 end if;
5329 Dim := Expr_Value (Hi) - Expr_Value (Lo) + 1;
5331 if Dim >= 0 then
5332 Num_Elmts := Num_Elmts * Dim;
5333 else
5334 Num_Elmts := Uint_0;
5335 end if;
5337 Next_Index (Indx);
5338 end loop;
5340 -- What we are looking for here is the situation where
5341 -- the RM_Size given would be exactly right if there was
5342 -- a pragma Pack, resulting in the component size being
5343 -- the RM_Size of the component type.
5345 if RM_Size (E) = Num_Elmts * Rsiz then
5346 -- For implicit packing mode, just set the component
5347 -- size and Freeze_Array_Type will do the rest.
5349 if Implicit_Packing then
5350 Set_Component_Size (Btyp, Rsiz);
5352 -- Otherwise give an error message
5354 else
5355 Error_Msg_NE
5356 ("size given for& too small", SZ, E);
5357 Error_Msg_N -- CODEFIX
5358 ("\use explicit pragma Pack "
5359 & "or use pragma Implicit_Packing", SZ);
5360 end if;
5361 end if;
5362 end if;
5363 end;
5364 end if;
5366 <<No_Implicit_Packing>>
5368 -- If ancestor subtype present, freeze that first. Note that this
5369 -- will also get the base type frozen. Need RM reference ???
5371 Atype := Ancestor_Subtype (E);
5373 if Present (Atype) then
5374 Freeze_And_Append (Atype, N, Result);
5376 -- No ancestor subtype present
5378 else
5379 -- See if we have a nearest ancestor that has a predicate.
5380 -- That catches the case of derived type with a predicate.
5381 -- Need RM reference here ???
5383 Atype := Nearest_Ancestor (E);
5385 if Present (Atype) and then Has_Predicates (Atype) then
5386 Freeze_And_Append (Atype, N, Result);
5387 end if;
5389 -- Freeze base type before freezing the entity (RM 13.14(15))
5391 if E /= Base_Type (E) then
5392 Freeze_And_Append (Base_Type (E), N, Result);
5393 end if;
5394 end if;
5396 -- A subtype inherits all the type-related representation aspects
5397 -- from its parents (RM 13.1(8)).
5399 Inherit_Aspects_At_Freeze_Point (E);
5401 -- For a derived type, freeze its parent type first (RM 13.14(15))
5403 elsif Is_Derived_Type (E) then
5404 Freeze_And_Append (Etype (E), N, Result);
5405 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5407 -- A derived type inherits each type-related representation aspect
5408 -- of its parent type that was directly specified before the
5409 -- declaration of the derived type (RM 13.1(15)).
5411 Inherit_Aspects_At_Freeze_Point (E);
5412 end if;
5414 -- Check for incompatible size and alignment for record type
5416 if Warn_On_Size_Alignment
5417 and then Is_Record_Type (E)
5418 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5420 -- If explicit Object_Size clause given assume that the programmer
5421 -- knows what he is doing, and expects the compiler behavior.
5423 and then not Has_Object_Size_Clause (E)
5425 -- Check for size not a multiple of alignment
5427 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5428 then
5429 declare
5430 SC : constant Node_Id := Size_Clause (E);
5431 AC : constant Node_Id := Alignment_Clause (E);
5432 Loc : Node_Id;
5433 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5435 begin
5436 if Present (SC) and then Present (AC) then
5438 -- Give a warning
5440 if Sloc (SC) > Sloc (AC) then
5441 Loc := SC;
5442 Error_Msg_NE
5443 ("?Z?size is not a multiple of alignment for &",
5444 Loc, E);
5445 Error_Msg_Sloc := Sloc (AC);
5446 Error_Msg_Uint_1 := Alignment (E);
5447 Error_Msg_N ("\?Z?alignment of ^ specified #", Loc);
5449 else
5450 Loc := AC;
5451 Error_Msg_NE
5452 ("?Z?size is not a multiple of alignment for &",
5453 Loc, E);
5454 Error_Msg_Sloc := Sloc (SC);
5455 Error_Msg_Uint_1 := RM_Size (E);
5456 Error_Msg_N ("\?Z?size of ^ specified #", Loc);
5457 end if;
5459 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5460 Error_Msg_N ("\?Z?Object_Size will be increased to ^", Loc);
5461 end if;
5462 end;
5463 end if;
5465 -- Array type
5467 if Is_Array_Type (E) then
5468 Freeze_Array_Type (E);
5470 -- For a class-wide type, the corresponding specific type is
5471 -- frozen as well (RM 13.14(15))
5473 elsif Is_Class_Wide_Type (E) then
5474 Freeze_And_Append (Root_Type (E), N, Result);
5476 -- If the base type of the class-wide type is still incomplete,
5477 -- the class-wide remains unfrozen as well. This is legal when
5478 -- E is the formal of a primitive operation of some other type
5479 -- which is being frozen.
5481 if not Is_Frozen (Root_Type (E)) then
5482 Set_Is_Frozen (E, False);
5483 Ghost_Mode := Save_Ghost_Mode;
5484 return Result;
5485 end if;
5487 -- The equivalent type associated with a class-wide subtype needs
5488 -- to be frozen to ensure that its layout is done.
5490 if Ekind (E) = E_Class_Wide_Subtype
5491 and then Present (Equivalent_Type (E))
5492 then
5493 Freeze_And_Append (Equivalent_Type (E), N, Result);
5494 end if;
5496 -- Generate an itype reference for a library-level class-wide type
5497 -- at the freeze point. Otherwise the first explicit reference to
5498 -- the type may appear in an inner scope which will be rejected by
5499 -- the back-end.
5501 if Is_Itype (E)
5502 and then Is_Compilation_Unit (Scope (E))
5503 then
5504 declare
5505 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5507 begin
5508 Set_Itype (Ref, E);
5510 -- From a gigi point of view, a class-wide subtype derives
5511 -- from its record equivalent type. As a result, the itype
5512 -- reference must appear after the freeze node of the
5513 -- equivalent type or gigi will reject the reference.
5515 if Ekind (E) = E_Class_Wide_Subtype
5516 and then Present (Equivalent_Type (E))
5517 then
5518 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5519 else
5520 Add_To_Result (Ref);
5521 end if;
5522 end;
5523 end if;
5525 -- For a record type or record subtype, freeze all component types
5526 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5527 -- using Is_Record_Type, because we don't want to attempt the freeze
5528 -- for the case of a private type with record extension (we will do
5529 -- that later when the full type is frozen).
5531 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype)
5532 and then not (Present (Scope (E))
5533 and then Is_Generic_Unit (Scope (E)))
5534 then
5535 Freeze_Record_Type (E);
5537 -- For a concurrent type, freeze corresponding record type. This does
5538 -- not correspond to any specific rule in the RM, but the record type
5539 -- is essentially part of the concurrent type. Also freeze all local
5540 -- entities. This includes record types created for entry parameter
5541 -- blocks and whatever local entities may appear in the private part.
5543 elsif Is_Concurrent_Type (E) then
5544 if Present (Corresponding_Record_Type (E)) then
5545 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5546 end if;
5548 Comp := First_Entity (E);
5549 while Present (Comp) loop
5550 if Is_Type (Comp) then
5551 Freeze_And_Append (Comp, N, Result);
5553 elsif (Ekind (Comp)) /= E_Function then
5555 -- The guard on the presence of the Etype seems to be needed
5556 -- for some CodePeer (-gnatcC) cases, but not clear why???
5558 if Present (Etype (Comp)) then
5559 if Is_Itype (Etype (Comp))
5560 and then Underlying_Type (Scope (Etype (Comp))) = E
5561 then
5562 Undelay_Type (Etype (Comp));
5563 end if;
5565 Freeze_And_Append (Etype (Comp), N, Result);
5566 end if;
5567 end if;
5569 Next_Entity (Comp);
5570 end loop;
5572 -- Private types are required to point to the same freeze node as
5573 -- their corresponding full views. The freeze node itself has to
5574 -- point to the partial view of the entity (because from the partial
5575 -- view, we can retrieve the full view, but not the reverse).
5576 -- However, in order to freeze correctly, we need to freeze the full
5577 -- view. If we are freezing at the end of a scope (or within the
5578 -- scope) of the private type, the partial and full views will have
5579 -- been swapped, the full view appears first in the entity chain and
5580 -- the swapping mechanism ensures that the pointers are properly set
5581 -- (on scope exit).
5583 -- If we encounter the partial view before the full view (e.g. when
5584 -- freezing from another scope), we freeze the full view, and then
5585 -- set the pointers appropriately since we cannot rely on swapping to
5586 -- fix things up (subtypes in an outer scope might not get swapped).
5588 -- If the full view is itself private, the above requirements apply
5589 -- to the underlying full view instead of the full view. But there is
5590 -- no swapping mechanism for the underlying full view so we need to
5591 -- set the pointers appropriately in both cases.
5593 elsif Is_Incomplete_Or_Private_Type (E)
5594 and then not Is_Generic_Type (E)
5595 then
5596 -- The construction of the dispatch table associated with library
5597 -- level tagged types forces freezing of all the primitives of the
5598 -- type, which may cause premature freezing of the partial view.
5599 -- For example:
5601 -- package Pkg is
5602 -- type T is tagged private;
5603 -- type DT is new T with private;
5604 -- procedure Prim (X : in out T; Y : in out DT'Class);
5605 -- private
5606 -- type T is tagged null record;
5607 -- Obj : T;
5608 -- type DT is new T with null record;
5609 -- end;
5611 -- In this case the type will be frozen later by the usual
5612 -- mechanism: an object declaration, an instantiation, or the
5613 -- end of a declarative part.
5615 if Is_Library_Level_Tagged_Type (E)
5616 and then not Present (Full_View (E))
5617 then
5618 Set_Is_Frozen (E, False);
5619 Ghost_Mode := Save_Ghost_Mode;
5620 return Result;
5622 -- Case of full view present
5624 elsif Present (Full_View (E)) then
5626 -- If full view has already been frozen, then no further
5627 -- processing is required
5629 if Is_Frozen (Full_View (E)) then
5630 Set_Has_Delayed_Freeze (E, False);
5631 Set_Freeze_Node (E, Empty);
5633 -- Otherwise freeze full view and patch the pointers so that
5634 -- the freeze node will elaborate both views in the back end.
5635 -- However, if full view is itself private, freeze underlying
5636 -- full view instead and patch the pointers so that the freeze
5637 -- node will elaborate the three views in the back end.
5639 else
5640 declare
5641 Full : Entity_Id := Full_View (E);
5643 begin
5644 if Is_Private_Type (Full)
5645 and then Present (Underlying_Full_View (Full))
5646 then
5647 Full := Underlying_Full_View (Full);
5648 end if;
5650 Freeze_And_Append (Full, N, Result);
5652 if Full /= Full_View (E)
5653 and then Has_Delayed_Freeze (Full_View (E))
5654 then
5655 F_Node := Freeze_Node (Full);
5657 if Present (F_Node) then
5658 Set_Freeze_Node (Full_View (E), F_Node);
5659 Set_Entity (F_Node, Full_View (E));
5661 else
5662 Set_Has_Delayed_Freeze (Full_View (E), False);
5663 Set_Freeze_Node (Full_View (E), Empty);
5664 end if;
5665 end if;
5667 if Has_Delayed_Freeze (E) then
5668 F_Node := Freeze_Node (Full_View (E));
5670 if Present (F_Node) then
5671 Set_Freeze_Node (E, F_Node);
5672 Set_Entity (F_Node, E);
5674 else
5675 -- {Incomplete,Private}_Subtypes with Full_Views
5676 -- constrained by discriminants.
5678 Set_Has_Delayed_Freeze (E, False);
5679 Set_Freeze_Node (E, Empty);
5680 end if;
5681 end if;
5682 end;
5683 end if;
5685 Check_Debug_Info_Needed (E);
5687 -- AI-117 requires that the convention of a partial view be the
5688 -- same as the convention of the full view. Note that this is a
5689 -- recognized breach of privacy, but it's essential for logical
5690 -- consistency of representation, and the lack of a rule in
5691 -- RM95 was an oversight.
5693 Set_Convention (E, Convention (Full_View (E)));
5695 Set_Size_Known_At_Compile_Time (E,
5696 Size_Known_At_Compile_Time (Full_View (E)));
5698 -- Size information is copied from the full view to the
5699 -- incomplete or private view for consistency.
5701 -- We skip this is the full view is not a type. This is very
5702 -- strange of course, and can only happen as a result of
5703 -- certain illegalities, such as a premature attempt to derive
5704 -- from an incomplete type.
5706 if Is_Type (Full_View (E)) then
5707 Set_Size_Info (E, Full_View (E));
5708 Set_RM_Size (E, RM_Size (Full_View (E)));
5709 end if;
5711 Ghost_Mode := Save_Ghost_Mode;
5712 return Result;
5714 -- Case of underlying full view present
5716 elsif Is_Private_Type (E)
5717 and then Present (Underlying_Full_View (E))
5718 then
5719 if not Is_Frozen (Underlying_Full_View (E)) then
5720 Freeze_And_Append (Underlying_Full_View (E), N, Result);
5721 end if;
5723 -- Patch the pointers so that the freeze node will elaborate
5724 -- both views in the back end.
5726 if Has_Delayed_Freeze (E) then
5727 F_Node := Freeze_Node (Underlying_Full_View (E));
5729 if Present (F_Node) then
5730 Set_Freeze_Node (E, F_Node);
5731 Set_Entity (F_Node, E);
5733 else
5734 Set_Has_Delayed_Freeze (E, False);
5735 Set_Freeze_Node (E, Empty);
5736 end if;
5737 end if;
5739 Check_Debug_Info_Needed (E);
5741 Ghost_Mode := Save_Ghost_Mode;
5742 return Result;
5744 -- Case of no full view present. If entity is derived or subtype,
5745 -- it is safe to freeze, correctness depends on the frozen status
5746 -- of parent. Otherwise it is either premature usage, or a Taft
5747 -- amendment type, so diagnosis is at the point of use and the
5748 -- type might be frozen later.
5750 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
5751 null;
5753 else
5754 Set_Is_Frozen (E, False);
5755 Ghost_Mode := Save_Ghost_Mode;
5756 return No_List;
5757 end if;
5759 -- For access subprogram, freeze types of all formals, the return
5760 -- type was already frozen, since it is the Etype of the function.
5761 -- Formal types can be tagged Taft amendment types, but otherwise
5762 -- they cannot be incomplete.
5764 elsif Ekind (E) = E_Subprogram_Type then
5765 Formal := First_Formal (E);
5766 while Present (Formal) loop
5767 if Ekind (Etype (Formal)) = E_Incomplete_Type
5768 and then No (Full_View (Etype (Formal)))
5769 then
5770 if Is_Tagged_Type (Etype (Formal)) then
5771 null;
5773 -- AI05-151: Incomplete types are allowed in access to
5774 -- subprogram specifications.
5776 elsif Ada_Version < Ada_2012 then
5777 Error_Msg_NE
5778 ("invalid use of incomplete type&", E, Etype (Formal));
5779 end if;
5780 end if;
5782 Freeze_And_Append (Etype (Formal), N, Result);
5783 Next_Formal (Formal);
5784 end loop;
5786 Freeze_Subprogram (E);
5788 -- For access to a protected subprogram, freeze the equivalent type
5789 -- (however this is not set if we are not generating code or if this
5790 -- is an anonymous type used just for resolution).
5792 elsif Is_Access_Protected_Subprogram_Type (E) then
5793 if Present (Equivalent_Type (E)) then
5794 Freeze_And_Append (Equivalent_Type (E), N, Result);
5795 end if;
5796 end if;
5798 -- Generic types are never seen by the back-end, and are also not
5799 -- processed by the expander (since the expander is turned off for
5800 -- generic processing), so we never need freeze nodes for them.
5802 if Is_Generic_Type (E) then
5803 Ghost_Mode := Save_Ghost_Mode;
5804 return Result;
5805 end if;
5807 -- Some special processing for non-generic types to complete
5808 -- representation details not known till the freeze point.
5810 if Is_Fixed_Point_Type (E) then
5811 Freeze_Fixed_Point_Type (E);
5813 -- Some error checks required for ordinary fixed-point type. Defer
5814 -- these till the freeze-point since we need the small and range
5815 -- values. We only do these checks for base types
5817 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
5818 if Small_Value (E) < Ureal_2_M_80 then
5819 Error_Msg_Name_1 := Name_Small;
5820 Error_Msg_N
5821 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
5823 elsif Small_Value (E) > Ureal_2_80 then
5824 Error_Msg_Name_1 := Name_Small;
5825 Error_Msg_N
5826 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
5827 end if;
5829 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
5830 Error_Msg_Name_1 := Name_First;
5831 Error_Msg_N
5832 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
5833 end if;
5835 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
5836 Error_Msg_Name_1 := Name_Last;
5837 Error_Msg_N
5838 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
5839 end if;
5840 end if;
5842 elsif Is_Enumeration_Type (E) then
5843 Freeze_Enumeration_Type (E);
5845 elsif Is_Integer_Type (E) then
5846 Adjust_Esize_For_Alignment (E);
5848 if Is_Modular_Integer_Type (E)
5849 and then Warn_On_Suspicious_Modulus_Value
5850 then
5851 Check_Suspicious_Modulus (E);
5852 end if;
5854 -- The pool applies to named and anonymous access types, but not
5855 -- to subprogram and to internal types generated for 'Access
5856 -- references.
5858 elsif Is_Access_Type (E)
5859 and then not Is_Access_Subprogram_Type (E)
5860 and then Ekind (E) /= E_Access_Attribute_Type
5861 then
5862 -- If a pragma Default_Storage_Pool applies, and this type has no
5863 -- Storage_Pool or Storage_Size clause (which must have occurred
5864 -- before the freezing point), then use the default. This applies
5865 -- only to base types.
5867 -- None of this applies to access to subprograms, for which there
5868 -- are clearly no pools.
5870 if Present (Default_Pool)
5871 and then Is_Base_Type (E)
5872 and then not Has_Storage_Size_Clause (E)
5873 and then No (Associated_Storage_Pool (E))
5874 then
5875 -- Case of pragma Default_Storage_Pool (null)
5877 if Nkind (Default_Pool) = N_Null then
5878 Set_No_Pool_Assigned (E);
5880 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
5882 else
5883 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
5884 end if;
5885 end if;
5887 -- Check restriction for standard storage pool
5889 if No (Associated_Storage_Pool (E)) then
5890 Check_Restriction (No_Standard_Storage_Pools, E);
5891 end if;
5893 -- Deal with error message for pure access type. This is not an
5894 -- error in Ada 2005 if there is no pool (see AI-366).
5896 if Is_Pure_Unit_Access_Type (E)
5897 and then (Ada_Version < Ada_2005
5898 or else not No_Pool_Assigned (E))
5899 and then not Is_Generic_Unit (Scope (E))
5900 then
5901 Error_Msg_N ("named access type not allowed in pure unit", E);
5903 if Ada_Version >= Ada_2005 then
5904 Error_Msg_N
5905 ("\would be legal if Storage_Size of 0 given??", E);
5907 elsif No_Pool_Assigned (E) then
5908 Error_Msg_N
5909 ("\would be legal in Ada 2005??", E);
5911 else
5912 Error_Msg_N
5913 ("\would be legal in Ada 2005 if "
5914 & "Storage_Size of 0 given??", E);
5915 end if;
5916 end if;
5917 end if;
5919 -- Case of composite types
5921 if Is_Composite_Type (E) then
5923 -- AI-117 requires that all new primitives of a tagged type must
5924 -- inherit the convention of the full view of the type. Inherited
5925 -- and overriding operations are defined to inherit the convention
5926 -- of their parent or overridden subprogram (also specified in
5927 -- AI-117), which will have occurred earlier (in Derive_Subprogram
5928 -- and New_Overloaded_Entity). Here we set the convention of
5929 -- primitives that are still convention Ada, which will ensure
5930 -- that any new primitives inherit the type's convention. Class-
5931 -- wide types can have a foreign convention inherited from their
5932 -- specific type, but are excluded from this since they don't have
5933 -- any associated primitives.
5935 if Is_Tagged_Type (E)
5936 and then not Is_Class_Wide_Type (E)
5937 and then Convention (E) /= Convention_Ada
5938 then
5939 declare
5940 Prim_List : constant Elist_Id := Primitive_Operations (E);
5941 Prim : Elmt_Id;
5943 begin
5944 Prim := First_Elmt (Prim_List);
5945 while Present (Prim) loop
5946 if Convention (Node (Prim)) = Convention_Ada then
5947 Set_Convention (Node (Prim), Convention (E));
5948 end if;
5950 Next_Elmt (Prim);
5951 end loop;
5952 end;
5953 end if;
5955 -- If the type is a simple storage pool type, then this is where
5956 -- we attempt to locate and validate its Allocate, Deallocate, and
5957 -- Storage_Size operations (the first is required, and the latter
5958 -- two are optional). We also verify that the full type for a
5959 -- private type is allowed to be a simple storage pool type.
5961 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
5962 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
5963 then
5964 -- If the type is marked Has_Private_Declaration, then this is
5965 -- a full type for a private type that was specified with the
5966 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
5967 -- pragma is allowed for the full type (for example, it can't
5968 -- be an array type, or a nonlimited record type).
5970 if Has_Private_Declaration (E) then
5971 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
5972 and then not Is_Private_Type (E)
5973 then
5974 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
5975 Error_Msg_N
5976 ("pragma% can only apply to full type that is an " &
5977 "explicitly limited type", E);
5978 end if;
5979 end if;
5981 Validate_Simple_Pool_Ops : declare
5982 Pool_Type : Entity_Id renames E;
5983 Address_Type : constant Entity_Id := RTE (RE_Address);
5984 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
5986 procedure Validate_Simple_Pool_Op_Formal
5987 (Pool_Op : Entity_Id;
5988 Pool_Op_Formal : in out Entity_Id;
5989 Expected_Mode : Formal_Kind;
5990 Expected_Type : Entity_Id;
5991 Formal_Name : String;
5992 OK_Formal : in out Boolean);
5993 -- Validate one formal Pool_Op_Formal of the candidate pool
5994 -- operation Pool_Op. The formal must be of Expected_Type
5995 -- and have mode Expected_Mode. OK_Formal will be set to
5996 -- False if the formal doesn't match. If OK_Formal is False
5997 -- on entry, then the formal will effectively be ignored
5998 -- (because validation of the pool op has already failed).
5999 -- Upon return, Pool_Op_Formal will be updated to the next
6000 -- formal, if any.
6002 procedure Validate_Simple_Pool_Operation
6003 (Op_Name : Name_Id);
6004 -- Search for and validate a simple pool operation with the
6005 -- name Op_Name. If the name is Allocate, then there must be
6006 -- exactly one such primitive operation for the simple pool
6007 -- type. If the name is Deallocate or Storage_Size, then
6008 -- there can be at most one such primitive operation. The
6009 -- profile of the located primitive must conform to what
6010 -- is expected for each operation.
6012 ------------------------------------
6013 -- Validate_Simple_Pool_Op_Formal --
6014 ------------------------------------
6016 procedure Validate_Simple_Pool_Op_Formal
6017 (Pool_Op : Entity_Id;
6018 Pool_Op_Formal : in out Entity_Id;
6019 Expected_Mode : Formal_Kind;
6020 Expected_Type : Entity_Id;
6021 Formal_Name : String;
6022 OK_Formal : in out Boolean)
6024 begin
6025 -- If OK_Formal is False on entry, then simply ignore
6026 -- the formal, because an earlier formal has already
6027 -- been flagged.
6029 if not OK_Formal then
6030 return;
6032 -- If no formal is passed in, then issue an error for a
6033 -- missing formal.
6035 elsif not Present (Pool_Op_Formal) then
6036 Error_Msg_NE
6037 ("simple storage pool op missing formal " &
6038 Formal_Name & " of type&", Pool_Op, Expected_Type);
6039 OK_Formal := False;
6041 return;
6042 end if;
6044 if Etype (Pool_Op_Formal) /= Expected_Type then
6046 -- If the pool type was expected for this formal, then
6047 -- this will not be considered a candidate operation
6048 -- for the simple pool, so we unset OK_Formal so that
6049 -- the op and any later formals will be ignored.
6051 if Expected_Type = Pool_Type then
6052 OK_Formal := False;
6054 return;
6056 else
6057 Error_Msg_NE
6058 ("wrong type for formal " & Formal_Name &
6059 " of simple storage pool op; expected type&",
6060 Pool_Op_Formal, Expected_Type);
6061 end if;
6062 end if;
6064 -- Issue error if formal's mode is not the expected one
6066 if Ekind (Pool_Op_Formal) /= Expected_Mode then
6067 Error_Msg_N
6068 ("wrong mode for formal of simple storage pool op",
6069 Pool_Op_Formal);
6070 end if;
6072 -- Advance to the next formal
6074 Next_Formal (Pool_Op_Formal);
6075 end Validate_Simple_Pool_Op_Formal;
6077 ------------------------------------
6078 -- Validate_Simple_Pool_Operation --
6079 ------------------------------------
6081 procedure Validate_Simple_Pool_Operation
6082 (Op_Name : Name_Id)
6084 Op : Entity_Id;
6085 Found_Op : Entity_Id := Empty;
6086 Formal : Entity_Id;
6087 Is_OK : Boolean;
6089 begin
6090 pragma Assert
6091 (Nam_In (Op_Name, Name_Allocate,
6092 Name_Deallocate,
6093 Name_Storage_Size));
6095 Error_Msg_Name_1 := Op_Name;
6097 -- For each homonym declared immediately in the scope
6098 -- of the simple storage pool type, determine whether
6099 -- the homonym is an operation of the pool type, and,
6100 -- if so, check that its profile is as expected for
6101 -- a simple pool operation of that name.
6103 Op := Get_Name_Entity_Id (Op_Name);
6104 while Present (Op) loop
6105 if Ekind_In (Op, E_Function, E_Procedure)
6106 and then Scope (Op) = Current_Scope
6107 then
6108 Formal := First_Entity (Op);
6110 Is_OK := True;
6112 -- The first parameter must be of the pool type
6113 -- in order for the operation to qualify.
6115 if Op_Name = Name_Storage_Size then
6116 Validate_Simple_Pool_Op_Formal
6117 (Op, Formal, E_In_Parameter, Pool_Type,
6118 "Pool", Is_OK);
6119 else
6120 Validate_Simple_Pool_Op_Formal
6121 (Op, Formal, E_In_Out_Parameter, Pool_Type,
6122 "Pool", Is_OK);
6123 end if;
6125 -- If another operation with this name has already
6126 -- been located for the type, then flag an error,
6127 -- since we only allow the type to have a single
6128 -- such primitive.
6130 if Present (Found_Op) and then Is_OK then
6131 Error_Msg_NE
6132 ("only one % operation allowed for " &
6133 "simple storage pool type&", Op, Pool_Type);
6134 end if;
6136 -- In the case of Allocate and Deallocate, a formal
6137 -- of type System.Address is required.
6139 if Op_Name = Name_Allocate then
6140 Validate_Simple_Pool_Op_Formal
6141 (Op, Formal, E_Out_Parameter,
6142 Address_Type, "Storage_Address", Is_OK);
6144 elsif Op_Name = Name_Deallocate then
6145 Validate_Simple_Pool_Op_Formal
6146 (Op, Formal, E_In_Parameter,
6147 Address_Type, "Storage_Address", Is_OK);
6148 end if;
6150 -- In the case of Allocate and Deallocate, formals
6151 -- of type Storage_Count are required as the third
6152 -- and fourth parameters.
6154 if Op_Name /= Name_Storage_Size then
6155 Validate_Simple_Pool_Op_Formal
6156 (Op, Formal, E_In_Parameter,
6157 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
6158 Validate_Simple_Pool_Op_Formal
6159 (Op, Formal, E_In_Parameter,
6160 Stg_Cnt_Type, "Alignment", Is_OK);
6161 end if;
6163 -- If no mismatched formals have been found (Is_OK)
6164 -- and no excess formals are present, then this
6165 -- operation has been validated, so record it.
6167 if not Present (Formal) and then Is_OK then
6168 Found_Op := Op;
6169 end if;
6170 end if;
6172 Op := Homonym (Op);
6173 end loop;
6175 -- There must be a valid Allocate operation for the type,
6176 -- so issue an error if none was found.
6178 if Op_Name = Name_Allocate
6179 and then not Present (Found_Op)
6180 then
6181 Error_Msg_N ("missing % operation for simple " &
6182 "storage pool type", Pool_Type);
6184 elsif Present (Found_Op) then
6186 -- Simple pool operations can't be abstract
6188 if Is_Abstract_Subprogram (Found_Op) then
6189 Error_Msg_N
6190 ("simple storage pool operation must not be " &
6191 "abstract", Found_Op);
6192 end if;
6194 -- The Storage_Size operation must be a function with
6195 -- Storage_Count as its result type.
6197 if Op_Name = Name_Storage_Size then
6198 if Ekind (Found_Op) = E_Procedure then
6199 Error_Msg_N
6200 ("% operation must be a function", Found_Op);
6202 elsif Etype (Found_Op) /= Stg_Cnt_Type then
6203 Error_Msg_NE
6204 ("wrong result type for%, expected type&",
6205 Found_Op, Stg_Cnt_Type);
6206 end if;
6208 -- Allocate and Deallocate must be procedures
6210 elsif Ekind (Found_Op) = E_Function then
6211 Error_Msg_N
6212 ("% operation must be a procedure", Found_Op);
6213 end if;
6214 end if;
6215 end Validate_Simple_Pool_Operation;
6217 -- Start of processing for Validate_Simple_Pool_Ops
6219 begin
6220 Validate_Simple_Pool_Operation (Name_Allocate);
6221 Validate_Simple_Pool_Operation (Name_Deallocate);
6222 Validate_Simple_Pool_Operation (Name_Storage_Size);
6223 end Validate_Simple_Pool_Ops;
6224 end if;
6225 end if;
6227 -- Now that all types from which E may depend are frozen, see if the
6228 -- size is known at compile time, if it must be unsigned, or if
6229 -- strict alignment is required
6231 Check_Compile_Time_Size (E);
6232 Check_Unsigned_Type (E);
6234 if Base_Type (E) = E then
6235 Check_Strict_Alignment (E);
6236 end if;
6238 -- Do not allow a size clause for a type which does not have a size
6239 -- that is known at compile time
6241 if Has_Size_Clause (E)
6242 and then not Size_Known_At_Compile_Time (E)
6243 then
6244 -- Suppress this message if errors posted on E, even if we are
6245 -- in all errors mode, since this is often a junk message
6247 if not Error_Posted (E) then
6248 Error_Msg_N
6249 ("size clause not allowed for variable length type",
6250 Size_Clause (E));
6251 end if;
6252 end if;
6254 -- Now we set/verify the representation information, in particular
6255 -- the size and alignment values. This processing is not required for
6256 -- generic types, since generic types do not play any part in code
6257 -- generation, and so the size and alignment values for such types
6258 -- are irrelevant. Ditto for types declared within a generic unit,
6259 -- which may have components that depend on generic parameters, and
6260 -- that will be recreated in an instance.
6262 if Inside_A_Generic then
6263 null;
6265 -- Otherwise we call the layout procedure
6267 else
6268 Layout_Type (E);
6269 end if;
6271 -- If this is an access to subprogram whose designated type is itself
6272 -- a subprogram type, the return type of this anonymous subprogram
6273 -- type must be decorated as well.
6275 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
6276 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
6277 then
6278 Layout_Type (Etype (Designated_Type (E)));
6279 end if;
6281 -- If the type has a Defaut_Value/Default_Component_Value aspect,
6282 -- this is where we analye the expression (after the type is frozen,
6283 -- since in the case of Default_Value, we are analyzing with the
6284 -- type itself, and we treat Default_Component_Value similarly for
6285 -- the sake of uniformity).
6287 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
6288 declare
6289 Nam : Name_Id;
6290 Exp : Node_Id;
6291 Typ : Entity_Id;
6293 begin
6294 if Is_Scalar_Type (E) then
6295 Nam := Name_Default_Value;
6296 Typ := E;
6297 Exp := Default_Aspect_Value (Typ);
6298 else
6299 Nam := Name_Default_Component_Value;
6300 Typ := Component_Type (E);
6301 Exp := Default_Aspect_Component_Value (E);
6302 end if;
6304 Analyze_And_Resolve (Exp, Typ);
6306 if Etype (Exp) /= Any_Type then
6307 if not Is_OK_Static_Expression (Exp) then
6308 Error_Msg_Name_1 := Nam;
6309 Flag_Non_Static_Expr
6310 ("aspect% requires static expression", Exp);
6311 end if;
6312 end if;
6313 end;
6314 end if;
6316 -- End of freeze processing for type entities
6317 end if;
6319 -- Here is where we logically freeze the current entity. If it has a
6320 -- freeze node, then this is the point at which the freeze node is
6321 -- linked into the result list.
6323 if Has_Delayed_Freeze (E) then
6325 -- If a freeze node is already allocated, use it, otherwise allocate
6326 -- a new one. The preallocation happens in the case of anonymous base
6327 -- types, where we preallocate so that we can set First_Subtype_Link.
6328 -- Note that we reset the Sloc to the current freeze location.
6330 if Present (Freeze_Node (E)) then
6331 F_Node := Freeze_Node (E);
6332 Set_Sloc (F_Node, Loc);
6334 else
6335 F_Node := New_Freeze_Node;
6336 Set_Freeze_Node (E, F_Node);
6337 Set_Access_Types_To_Process (F_Node, No_Elist);
6338 Set_TSS_Elist (F_Node, No_Elist);
6339 Set_Actions (F_Node, No_List);
6340 end if;
6342 Set_Entity (F_Node, E);
6343 Add_To_Result (F_Node);
6345 -- A final pass over record types with discriminants. If the type
6346 -- has an incomplete declaration, there may be constrained access
6347 -- subtypes declared elsewhere, which do not depend on the discrimi-
6348 -- nants of the type, and which are used as component types (i.e.
6349 -- the full view is a recursive type). The designated types of these
6350 -- subtypes can only be elaborated after the type itself, and they
6351 -- need an itype reference.
6353 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
6354 declare
6355 Comp : Entity_Id;
6356 IR : Node_Id;
6357 Typ : Entity_Id;
6359 begin
6360 Comp := First_Component (E);
6361 while Present (Comp) loop
6362 Typ := Etype (Comp);
6364 if Ekind (Comp) = E_Component
6365 and then Is_Access_Type (Typ)
6366 and then Scope (Typ) /= E
6367 and then Base_Type (Designated_Type (Typ)) = E
6368 and then Is_Itype (Designated_Type (Typ))
6369 then
6370 IR := Make_Itype_Reference (Sloc (Comp));
6371 Set_Itype (IR, Designated_Type (Typ));
6372 Append (IR, Result);
6373 end if;
6375 Next_Component (Comp);
6376 end loop;
6377 end;
6378 end if;
6379 end if;
6381 -- When a type is frozen, the first subtype of the type is frozen as
6382 -- well (RM 13.14(15)). This has to be done after freezing the type,
6383 -- since obviously the first subtype depends on its own base type.
6385 if Is_Type (E) then
6386 Freeze_And_Append (First_Subtype (E), N, Result);
6388 -- If we just froze a tagged non-class wide record, then freeze the
6389 -- corresponding class-wide type. This must be done after the tagged
6390 -- type itself is frozen, because the class-wide type refers to the
6391 -- tagged type which generates the class.
6393 if Is_Tagged_Type (E)
6394 and then not Is_Class_Wide_Type (E)
6395 and then Present (Class_Wide_Type (E))
6396 then
6397 Freeze_And_Append (Class_Wide_Type (E), N, Result);
6398 end if;
6399 end if;
6401 Check_Debug_Info_Needed (E);
6403 -- Special handling for subprograms
6405 if Is_Subprogram (E) then
6407 -- If subprogram has address clause then reset Is_Public flag, since
6408 -- we do not want the backend to generate external references.
6410 if Present (Address_Clause (E))
6411 and then not Is_Library_Level_Entity (E)
6412 then
6413 Set_Is_Public (E, False);
6414 end if;
6415 end if;
6417 Ghost_Mode := Save_Ghost_Mode;
6418 return Result;
6419 end Freeze_Entity;
6421 -----------------------------
6422 -- Freeze_Enumeration_Type --
6423 -----------------------------
6425 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6426 begin
6427 -- By default, if no size clause is present, an enumeration type with
6428 -- Convention C is assumed to interface to a C enum, and has integer
6429 -- size. This applies to types. For subtypes, verify that its base
6430 -- type has no size clause either. Treat other foreign conventions
6431 -- in the same way, and also make sure alignment is set right.
6433 if Has_Foreign_Convention (Typ)
6434 and then not Has_Size_Clause (Typ)
6435 and then not Has_Size_Clause (Base_Type (Typ))
6436 and then Esize (Typ) < Standard_Integer_Size
6438 -- Don't do this if Short_Enums on target
6440 and then not Target_Short_Enums
6441 then
6442 Init_Esize (Typ, Standard_Integer_Size);
6443 Set_Alignment (Typ, Alignment (Standard_Integer));
6445 -- Normal Ada case or size clause present or not Long_C_Enums on target
6447 else
6448 -- If the enumeration type interfaces to C, and it has a size clause
6449 -- that specifies less than int size, it warrants a warning. The
6450 -- user may intend the C type to be an enum or a char, so this is
6451 -- not by itself an error that the Ada compiler can detect, but it
6452 -- it is a worth a heads-up. For Boolean and Character types we
6453 -- assume that the programmer has the proper C type in mind.
6455 if Convention (Typ) = Convention_C
6456 and then Has_Size_Clause (Typ)
6457 and then Esize (Typ) /= Esize (Standard_Integer)
6458 and then not Is_Boolean_Type (Typ)
6459 and then not Is_Character_Type (Typ)
6461 -- Don't do this if Short_Enums on target
6463 and then not Target_Short_Enums
6464 then
6465 Error_Msg_N
6466 ("C enum types have the size of a C int??", Size_Clause (Typ));
6467 end if;
6469 Adjust_Esize_For_Alignment (Typ);
6470 end if;
6471 end Freeze_Enumeration_Type;
6473 -----------------------
6474 -- Freeze_Expression --
6475 -----------------------
6477 procedure Freeze_Expression (N : Node_Id) is
6478 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6479 Typ : Entity_Id;
6480 Nam : Entity_Id;
6481 Desig_Typ : Entity_Id;
6482 P : Node_Id;
6483 Parent_P : Node_Id;
6485 Freeze_Outside : Boolean := False;
6486 -- This flag is set true if the entity must be frozen outside the
6487 -- current subprogram. This happens in the case of expander generated
6488 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6489 -- not freeze all entities like other bodies, but which nevertheless
6490 -- may reference entities that have to be frozen before the body and
6491 -- obviously cannot be frozen inside the body.
6493 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6494 -- If the expression is an array aggregate, the type of the component
6495 -- expressions is also frozen. If the component type is an access type
6496 -- and the expressions include allocators, the designed type is frozen
6497 -- as well.
6499 function In_Expanded_Body (N : Node_Id) return Boolean;
6500 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6501 -- it is the handled statement sequence of an expander-generated
6502 -- subprogram (init proc, stream subprogram, or renaming as body).
6503 -- If so, this is not a freezing context.
6505 -----------------------------------------
6506 -- Find_Aggregate_Component_Desig_Type --
6507 -----------------------------------------
6509 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6510 Assoc : Node_Id;
6511 Exp : Node_Id;
6513 begin
6514 if Present (Expressions (N)) then
6515 Exp := First (Expressions (N));
6516 while Present (Exp) loop
6517 if Nkind (Exp) = N_Allocator then
6518 return Designated_Type (Component_Type (Etype (N)));
6519 end if;
6521 Next (Exp);
6522 end loop;
6523 end if;
6525 if Present (Component_Associations (N)) then
6526 Assoc := First (Component_Associations (N));
6527 while Present (Assoc) loop
6528 if Nkind (Expression (Assoc)) = N_Allocator then
6529 return Designated_Type (Component_Type (Etype (N)));
6530 end if;
6532 Next (Assoc);
6533 end loop;
6534 end if;
6536 return Empty;
6537 end Find_Aggregate_Component_Desig_Type;
6539 ----------------------
6540 -- In_Expanded_Body --
6541 ----------------------
6543 function In_Expanded_Body (N : Node_Id) return Boolean is
6544 P : Node_Id;
6545 Id : Entity_Id;
6547 begin
6548 if Nkind (N) = N_Subprogram_Body then
6549 P := N;
6550 else
6551 P := Parent (N);
6552 end if;
6554 if Nkind (P) /= N_Subprogram_Body then
6555 return False;
6557 else
6558 Id := Defining_Unit_Name (Specification (P));
6560 -- The following are expander-created bodies, or bodies that
6561 -- are not freeze points.
6563 if Nkind (Id) = N_Defining_Identifier
6564 and then (Is_Init_Proc (Id)
6565 or else Is_TSS (Id, TSS_Stream_Input)
6566 or else Is_TSS (Id, TSS_Stream_Output)
6567 or else Is_TSS (Id, TSS_Stream_Read)
6568 or else Is_TSS (Id, TSS_Stream_Write)
6569 or else Nkind_In (Original_Node (P),
6570 N_Subprogram_Renaming_Declaration,
6571 N_Expression_Function))
6572 then
6573 return True;
6574 else
6575 return False;
6576 end if;
6577 end if;
6578 end In_Expanded_Body;
6580 -- Start of processing for Freeze_Expression
6582 begin
6583 -- Immediate return if freezing is inhibited. This flag is set by the
6584 -- analyzer to stop freezing on generated expressions that would cause
6585 -- freezing if they were in the source program, but which are not
6586 -- supposed to freeze, since they are created.
6588 if Must_Not_Freeze (N) then
6589 return;
6590 end if;
6592 -- If expression is non-static, then it does not freeze in a default
6593 -- expression, see section "Handling of Default Expressions" in the
6594 -- spec of package Sem for further details. Note that we have to make
6595 -- sure that we actually have a real expression (if we have a subtype
6596 -- indication, we can't test Is_OK_Static_Expression). However, we
6597 -- exclude the case of the prefix of an attribute of a static scalar
6598 -- subtype from this early return, because static subtype attributes
6599 -- should always cause freezing, even in default expressions, but
6600 -- the attribute may not have been marked as static yet (because in
6601 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
6602 -- Freeze_Expression on the prefix).
6604 if In_Spec_Exp
6605 and then Nkind (N) in N_Subexpr
6606 and then not Is_OK_Static_Expression (N)
6607 and then (Nkind (Parent (N)) /= N_Attribute_Reference
6608 or else not (Is_Entity_Name (N)
6609 and then Is_Type (Entity (N))
6610 and then Is_OK_Static_Subtype (Entity (N))))
6611 then
6612 return;
6613 end if;
6615 -- Freeze type of expression if not frozen already
6617 Typ := Empty;
6619 if Nkind (N) in N_Has_Etype then
6620 if not Is_Frozen (Etype (N)) then
6621 Typ := Etype (N);
6623 -- Base type may be an derived numeric type that is frozen at
6624 -- the point of declaration, but first_subtype is still unfrozen.
6626 elsif not Is_Frozen (First_Subtype (Etype (N))) then
6627 Typ := First_Subtype (Etype (N));
6628 end if;
6629 end if;
6631 -- For entity name, freeze entity if not frozen already. A special
6632 -- exception occurs for an identifier that did not come from source.
6633 -- We don't let such identifiers freeze a non-internal entity, i.e.
6634 -- an entity that did come from source, since such an identifier was
6635 -- generated by the expander, and cannot have any semantic effect on
6636 -- the freezing semantics. For example, this stops the parameter of
6637 -- an initialization procedure from freezing the variable.
6639 if Is_Entity_Name (N)
6640 and then not Is_Frozen (Entity (N))
6641 and then (Nkind (N) /= N_Identifier
6642 or else Comes_From_Source (N)
6643 or else not Comes_From_Source (Entity (N)))
6644 then
6645 Nam := Entity (N);
6647 if Present (Nam) and then Ekind (Nam) = E_Function then
6648 Check_Expression_Function (N, Nam);
6649 end if;
6651 else
6652 Nam := Empty;
6653 end if;
6655 -- For an allocator freeze designated type if not frozen already
6657 -- For an aggregate whose component type is an access type, freeze the
6658 -- designated type now, so that its freeze does not appear within the
6659 -- loop that might be created in the expansion of the aggregate. If the
6660 -- designated type is a private type without full view, the expression
6661 -- cannot contain an allocator, so the type is not frozen.
6663 -- For a function, we freeze the entity when the subprogram declaration
6664 -- is frozen, but a function call may appear in an initialization proc.
6665 -- before the declaration is frozen. We need to generate the extra
6666 -- formals, if any, to ensure that the expansion of the call includes
6667 -- the proper actuals. This only applies to Ada subprograms, not to
6668 -- imported ones.
6670 Desig_Typ := Empty;
6672 case Nkind (N) is
6673 when N_Allocator =>
6674 Desig_Typ := Designated_Type (Etype (N));
6676 when N_Aggregate =>
6677 if Is_Array_Type (Etype (N))
6678 and then Is_Access_Type (Component_Type (Etype (N)))
6679 then
6681 -- Check whether aggregate includes allocators.
6683 Desig_Typ := Find_Aggregate_Component_Desig_Type;
6684 end if;
6686 when N_Selected_Component |
6687 N_Indexed_Component |
6688 N_Slice =>
6690 if Is_Access_Type (Etype (Prefix (N))) then
6691 Desig_Typ := Designated_Type (Etype (Prefix (N)));
6692 end if;
6694 when N_Identifier =>
6695 if Present (Nam)
6696 and then Ekind (Nam) = E_Function
6697 and then Nkind (Parent (N)) = N_Function_Call
6698 and then Convention (Nam) = Convention_Ada
6699 then
6700 Create_Extra_Formals (Nam);
6701 end if;
6703 when others =>
6704 null;
6705 end case;
6707 if Desig_Typ /= Empty
6708 and then (Is_Frozen (Desig_Typ)
6709 or else (not Is_Fully_Defined (Desig_Typ)))
6710 then
6711 Desig_Typ := Empty;
6712 end if;
6714 -- All done if nothing needs freezing
6716 if No (Typ)
6717 and then No (Nam)
6718 and then No (Desig_Typ)
6719 then
6720 return;
6721 end if;
6723 -- Examine the enclosing context by climbing the parent chain. The
6724 -- traversal serves two purposes - to detect scenarios where freezeing
6725 -- is not needed and to find the proper insertion point for the freeze
6726 -- nodes. Although somewhat similar to Insert_Actions, this traversal
6727 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
6728 -- the tree may result in types being frozen too early.
6730 P := N;
6731 loop
6732 Parent_P := Parent (P);
6734 -- If we don't have a parent, then we are not in a well-formed tree.
6735 -- This is an unusual case, but there are some legitimate situations
6736 -- in which this occurs, notably when the expressions in the range of
6737 -- a type declaration are resolved. We simply ignore the freeze
6738 -- request in this case. Is this right ???
6740 if No (Parent_P) then
6741 return;
6742 end if;
6744 -- See if we have got to an appropriate point in the tree
6746 case Nkind (Parent_P) is
6748 -- A special test for the exception of (RM 13.14(8)) for the case
6749 -- of per-object expressions (RM 3.8(18)) occurring in component
6750 -- definition or a discrete subtype definition. Note that we test
6751 -- for a component declaration which includes both cases we are
6752 -- interested in, and furthermore the tree does not have explicit
6753 -- nodes for either of these two constructs.
6755 when N_Component_Declaration =>
6757 -- The case we want to test for here is an identifier that is
6758 -- a per-object expression, this is either a discriminant that
6759 -- appears in a context other than the component declaration
6760 -- or it is a reference to the type of the enclosing construct.
6762 -- For either of these cases, we skip the freezing
6764 if not In_Spec_Expression
6765 and then Nkind (N) = N_Identifier
6766 and then (Present (Entity (N)))
6767 then
6768 -- We recognize the discriminant case by just looking for
6769 -- a reference to a discriminant. It can only be one for
6770 -- the enclosing construct. Skip freezing in this case.
6772 if Ekind (Entity (N)) = E_Discriminant then
6773 return;
6775 -- For the case of a reference to the enclosing record,
6776 -- (or task or protected type), we look for a type that
6777 -- matches the current scope.
6779 elsif Entity (N) = Current_Scope then
6780 return;
6781 end if;
6782 end if;
6784 -- If we have an enumeration literal that appears as the choice in
6785 -- the aggregate of an enumeration representation clause, then
6786 -- freezing does not occur (RM 13.14(10)).
6788 when N_Enumeration_Representation_Clause =>
6790 -- The case we are looking for is an enumeration literal
6792 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
6793 and then Is_Enumeration_Type (Etype (N))
6794 then
6795 -- If enumeration literal appears directly as the choice,
6796 -- do not freeze (this is the normal non-overloaded case)
6798 if Nkind (Parent (N)) = N_Component_Association
6799 and then First (Choices (Parent (N))) = N
6800 then
6801 return;
6803 -- If enumeration literal appears as the name of function
6804 -- which is the choice, then also do not freeze. This
6805 -- happens in the overloaded literal case, where the
6806 -- enumeration literal is temporarily changed to a function
6807 -- call for overloading analysis purposes.
6809 elsif Nkind (Parent (N)) = N_Function_Call
6810 and then
6811 Nkind (Parent (Parent (N))) = N_Component_Association
6812 and then
6813 First (Choices (Parent (Parent (N)))) = Parent (N)
6814 then
6815 return;
6816 end if;
6817 end if;
6819 -- Normally if the parent is a handled sequence of statements,
6820 -- then the current node must be a statement, and that is an
6821 -- appropriate place to insert a freeze node.
6823 when N_Handled_Sequence_Of_Statements =>
6825 -- An exception occurs when the sequence of statements is for
6826 -- an expander generated body that did not do the usual freeze
6827 -- all operation. In this case we usually want to freeze
6828 -- outside this body, not inside it, and we skip past the
6829 -- subprogram body that we are inside.
6831 if In_Expanded_Body (Parent_P) then
6832 declare
6833 Subp : constant Node_Id := Parent (Parent_P);
6834 Spec : Entity_Id;
6836 begin
6837 -- Freeze the entity only when it is declared inside the
6838 -- body of the expander generated procedure. This case
6839 -- is recognized by the scope of the entity or its type,
6840 -- which is either the spec for some enclosing body, or
6841 -- (in the case of init_procs, for which there are no
6842 -- separate specs) the current scope.
6844 if Nkind (Subp) = N_Subprogram_Body then
6845 Spec := Corresponding_Spec (Subp);
6847 if (Present (Typ) and then Scope (Typ) = Spec)
6848 or else
6849 (Present (Nam) and then Scope (Nam) = Spec)
6850 then
6851 exit;
6853 elsif Present (Typ)
6854 and then Scope (Typ) = Current_Scope
6855 and then Defining_Entity (Subp) = Current_Scope
6856 then
6857 exit;
6858 end if;
6859 end if;
6861 -- An expression function may act as a completion of
6862 -- a function declaration. As such, it can reference
6863 -- entities declared between the two views:
6865 -- Hidden []; -- 1
6866 -- function F return ...;
6867 -- private
6868 -- function Hidden return ...;
6869 -- function F return ... is (Hidden); -- 2
6871 -- Refering to the example above, freezing the expression
6872 -- of F (2) would place Hidden's freeze node (1) in the
6873 -- wrong place. Avoid explicit freezing and let the usual
6874 -- scenarios do the job - for example, reaching the end
6875 -- of the private declarations, or a call to F.
6877 if Nkind (Original_Node (Subp)) =
6878 N_Expression_Function
6879 then
6880 null;
6882 -- Freeze outside the body
6884 else
6885 Parent_P := Parent (Parent_P);
6886 Freeze_Outside := True;
6887 end if;
6888 end;
6890 -- Here if normal case where we are in handled statement
6891 -- sequence and want to do the insertion right there.
6893 else
6894 exit;
6895 end if;
6897 -- If parent is a body or a spec or a block, then the current node
6898 -- is a statement or declaration and we can insert the freeze node
6899 -- before it.
6901 when N_Block_Statement |
6902 N_Entry_Body |
6903 N_Package_Body |
6904 N_Package_Specification |
6905 N_Protected_Body |
6906 N_Subprogram_Body |
6907 N_Task_Body => exit;
6909 -- The expander is allowed to define types in any statements list,
6910 -- so any of the following parent nodes also mark a freezing point
6911 -- if the actual node is in a list of statements or declarations.
6913 when N_Abortable_Part |
6914 N_Accept_Alternative |
6915 N_And_Then |
6916 N_Case_Statement_Alternative |
6917 N_Compilation_Unit_Aux |
6918 N_Conditional_Entry_Call |
6919 N_Delay_Alternative |
6920 N_Elsif_Part |
6921 N_Entry_Call_Alternative |
6922 N_Exception_Handler |
6923 N_Extended_Return_Statement |
6924 N_Freeze_Entity |
6925 N_If_Statement |
6926 N_Or_Else |
6927 N_Selective_Accept |
6928 N_Triggering_Alternative =>
6930 exit when Is_List_Member (P);
6932 -- Freeze nodes produced by an expression coming from the Actions
6933 -- list of a N_Expression_With_Actions node must remain within the
6934 -- Actions list. Inserting the freeze nodes further up the tree
6935 -- may lead to use before declaration issues in the case of array
6936 -- types.
6938 when N_Expression_With_Actions =>
6939 if Is_List_Member (P)
6940 and then List_Containing (P) = Actions (Parent_P)
6941 then
6942 exit;
6943 end if;
6945 -- Note: N_Loop_Statement is a special case. A type that appears
6946 -- in the source can never be frozen in a loop (this occurs only
6947 -- because of a loop expanded by the expander), so we keep on
6948 -- going. Otherwise we terminate the search. Same is true of any
6949 -- entity which comes from source. (if they have predefined type,
6950 -- that type does not appear to come from source, but the entity
6951 -- should not be frozen here).
6953 when N_Loop_Statement =>
6954 exit when not Comes_From_Source (Etype (N))
6955 and then (No (Nam) or else not Comes_From_Source (Nam));
6957 -- For all other cases, keep looking at parents
6959 when others =>
6960 null;
6961 end case;
6963 -- We fall through the case if we did not yet find the proper
6964 -- place in the free for inserting the freeze node, so climb.
6966 P := Parent_P;
6967 end loop;
6969 -- If the expression appears in a record or an initialization procedure,
6970 -- the freeze nodes are collected and attached to the current scope, to
6971 -- be inserted and analyzed on exit from the scope, to insure that
6972 -- generated entities appear in the correct scope. If the expression is
6973 -- a default for a discriminant specification, the scope is still void.
6974 -- The expression can also appear in the discriminant part of a private
6975 -- or concurrent type.
6977 -- If the expression appears in a constrained subcomponent of an
6978 -- enclosing record declaration, the freeze nodes must be attached to
6979 -- the outer record type so they can eventually be placed in the
6980 -- enclosing declaration list.
6982 -- The other case requiring this special handling is if we are in a
6983 -- default expression, since in that case we are about to freeze a
6984 -- static type, and the freeze scope needs to be the outer scope, not
6985 -- the scope of the subprogram with the default parameter.
6987 -- For default expressions and other spec expressions in generic units,
6988 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
6989 -- placing them at the proper place, after the generic unit.
6991 if (In_Spec_Exp and not Inside_A_Generic)
6992 or else Freeze_Outside
6993 or else (Is_Type (Current_Scope)
6994 and then (not Is_Concurrent_Type (Current_Scope)
6995 or else not Has_Completion (Current_Scope)))
6996 or else Ekind (Current_Scope) = E_Void
6997 then
6998 declare
6999 N : constant Node_Id := Current_Scope;
7000 Freeze_Nodes : List_Id := No_List;
7001 Pos : Int := Scope_Stack.Last;
7003 begin
7004 if Present (Desig_Typ) then
7005 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
7006 end if;
7008 if Present (Typ) then
7009 Freeze_And_Append (Typ, N, Freeze_Nodes);
7010 end if;
7012 if Present (Nam) then
7013 Freeze_And_Append (Nam, N, Freeze_Nodes);
7014 end if;
7016 -- The current scope may be that of a constrained component of
7017 -- an enclosing record declaration, or of a loop of an enclosing
7018 -- quantified expression, which is above the current scope in the
7019 -- scope stack. Indeed in the context of a quantified expression,
7020 -- a scope is created and pushed above the current scope in order
7021 -- to emulate the loop-like behavior of the quantified expression.
7022 -- If the expression is within a top-level pragma, as for a pre-
7023 -- condition on a library-level subprogram, nothing to do.
7025 if not Is_Compilation_Unit (Current_Scope)
7026 and then (Is_Record_Type (Scope (Current_Scope))
7027 or else Nkind (Parent (Current_Scope)) =
7028 N_Quantified_Expression)
7029 then
7030 Pos := Pos - 1;
7031 end if;
7033 if Is_Non_Empty_List (Freeze_Nodes) then
7034 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
7035 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
7036 Freeze_Nodes;
7037 else
7038 Append_List (Freeze_Nodes,
7039 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
7040 end if;
7041 end if;
7042 end;
7044 return;
7045 end if;
7047 -- Now we have the right place to do the freezing. First, a special
7048 -- adjustment, if we are in spec-expression analysis mode, these freeze
7049 -- actions must not be thrown away (normally all inserted actions are
7050 -- thrown away in this mode. However, the freeze actions are from static
7051 -- expressions and one of the important reasons we are doing this
7052 -- special analysis is to get these freeze actions. Therefore we turn
7053 -- off the In_Spec_Expression mode to propagate these freeze actions.
7054 -- This also means they get properly analyzed and expanded.
7056 In_Spec_Expression := False;
7058 -- Freeze the designated type of an allocator (RM 13.14(13))
7060 if Present (Desig_Typ) then
7061 Freeze_Before (P, Desig_Typ);
7062 end if;
7064 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
7065 -- the enumeration representation clause exception in the loop above.
7067 if Present (Typ) then
7068 Freeze_Before (P, Typ);
7069 end if;
7071 -- Freeze name if one is present (RM 13.14(11))
7073 if Present (Nam) then
7074 Freeze_Before (P, Nam);
7075 end if;
7077 -- Restore In_Spec_Expression flag
7079 In_Spec_Expression := In_Spec_Exp;
7080 end Freeze_Expression;
7082 -----------------------------
7083 -- Freeze_Fixed_Point_Type --
7084 -----------------------------
7086 -- Certain fixed-point types and subtypes, including implicit base types
7087 -- and declared first subtypes, have not yet set up a range. This is
7088 -- because the range cannot be set until the Small and Size values are
7089 -- known, and these are not known till the type is frozen.
7091 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
7092 -- whose bounds are unanalyzed real literals. This routine will recognize
7093 -- this case, and transform this range node into a properly typed range
7094 -- with properly analyzed and resolved values.
7096 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
7097 Rng : constant Node_Id := Scalar_Range (Typ);
7098 Lo : constant Node_Id := Low_Bound (Rng);
7099 Hi : constant Node_Id := High_Bound (Rng);
7100 Btyp : constant Entity_Id := Base_Type (Typ);
7101 Brng : constant Node_Id := Scalar_Range (Btyp);
7102 BLo : constant Node_Id := Low_Bound (Brng);
7103 BHi : constant Node_Id := High_Bound (Brng);
7104 Small : constant Ureal := Small_Value (Typ);
7105 Loval : Ureal;
7106 Hival : Ureal;
7107 Atype : Entity_Id;
7109 Orig_Lo : Ureal;
7110 Orig_Hi : Ureal;
7111 -- Save original bounds (for shaving tests)
7113 Actual_Size : Nat;
7114 -- Actual size chosen
7116 function Fsize (Lov, Hiv : Ureal) return Nat;
7117 -- Returns size of type with given bounds. Also leaves these
7118 -- bounds set as the current bounds of the Typ.
7120 -----------
7121 -- Fsize --
7122 -----------
7124 function Fsize (Lov, Hiv : Ureal) return Nat is
7125 begin
7126 Set_Realval (Lo, Lov);
7127 Set_Realval (Hi, Hiv);
7128 return Minimum_Size (Typ);
7129 end Fsize;
7131 -- Start of processing for Freeze_Fixed_Point_Type
7133 begin
7134 -- If Esize of a subtype has not previously been set, set it now
7136 if Unknown_Esize (Typ) then
7137 Atype := Ancestor_Subtype (Typ);
7139 if Present (Atype) then
7140 Set_Esize (Typ, Esize (Atype));
7141 else
7142 Set_Esize (Typ, Esize (Base_Type (Typ)));
7143 end if;
7144 end if;
7146 -- Immediate return if the range is already analyzed. This means that
7147 -- the range is already set, and does not need to be computed by this
7148 -- routine.
7150 if Analyzed (Rng) then
7151 return;
7152 end if;
7154 -- Immediate return if either of the bounds raises Constraint_Error
7156 if Raises_Constraint_Error (Lo)
7157 or else Raises_Constraint_Error (Hi)
7158 then
7159 return;
7160 end if;
7162 Loval := Realval (Lo);
7163 Hival := Realval (Hi);
7165 Orig_Lo := Loval;
7166 Orig_Hi := Hival;
7168 -- Ordinary fixed-point case
7170 if Is_Ordinary_Fixed_Point_Type (Typ) then
7172 -- For the ordinary fixed-point case, we are allowed to fudge the
7173 -- end-points up or down by small. Generally we prefer to fudge up,
7174 -- i.e. widen the bounds for non-model numbers so that the end points
7175 -- are included. However there are cases in which this cannot be
7176 -- done, and indeed cases in which we may need to narrow the bounds.
7177 -- The following circuit makes the decision.
7179 -- Note: our terminology here is that Incl_EP means that the bounds
7180 -- are widened by Small if necessary to include the end points, and
7181 -- Excl_EP means that the bounds are narrowed by Small to exclude the
7182 -- end-points if this reduces the size.
7184 -- Note that in the Incl case, all we care about is including the
7185 -- end-points. In the Excl case, we want to narrow the bounds as
7186 -- much as permitted by the RM, to give the smallest possible size.
7188 Fudge : declare
7189 Loval_Incl_EP : Ureal;
7190 Hival_Incl_EP : Ureal;
7192 Loval_Excl_EP : Ureal;
7193 Hival_Excl_EP : Ureal;
7195 Size_Incl_EP : Nat;
7196 Size_Excl_EP : Nat;
7198 Model_Num : Ureal;
7199 First_Subt : Entity_Id;
7200 Actual_Lo : Ureal;
7201 Actual_Hi : Ureal;
7203 begin
7204 -- First step. Base types are required to be symmetrical. Right
7205 -- now, the base type range is a copy of the first subtype range.
7206 -- This will be corrected before we are done, but right away we
7207 -- need to deal with the case where both bounds are non-negative.
7208 -- In this case, we set the low bound to the negative of the high
7209 -- bound, to make sure that the size is computed to include the
7210 -- required sign. Note that we do not need to worry about the
7211 -- case of both bounds negative, because the sign will be dealt
7212 -- with anyway. Furthermore we can't just go making such a bound
7213 -- symmetrical, since in a twos-complement system, there is an
7214 -- extra negative value which could not be accommodated on the
7215 -- positive side.
7217 if Typ = Btyp
7218 and then not UR_Is_Negative (Loval)
7219 and then Hival > Loval
7220 then
7221 Loval := -Hival;
7222 Set_Realval (Lo, Loval);
7223 end if;
7225 -- Compute the fudged bounds. If the number is a model number,
7226 -- then we do nothing to include it, but we are allowed to backoff
7227 -- to the next adjacent model number when we exclude it. If it is
7228 -- not a model number then we straddle the two values with the
7229 -- model numbers on either side.
7231 Model_Num := UR_Trunc (Loval / Small) * Small;
7233 if Loval = Model_Num then
7234 Loval_Incl_EP := Model_Num;
7235 else
7236 Loval_Incl_EP := Model_Num - Small;
7237 end if;
7239 -- The low value excluding the end point is Small greater, but
7240 -- we do not do this exclusion if the low value is positive,
7241 -- since it can't help the size and could actually hurt by
7242 -- crossing the high bound.
7244 if UR_Is_Negative (Loval_Incl_EP) then
7245 Loval_Excl_EP := Loval_Incl_EP + Small;
7247 -- If the value went from negative to zero, then we have the
7248 -- case where Loval_Incl_EP is the model number just below
7249 -- zero, so we want to stick to the negative value for the
7250 -- base type to maintain the condition that the size will
7251 -- include signed values.
7253 if Typ = Btyp
7254 and then UR_Is_Zero (Loval_Excl_EP)
7255 then
7256 Loval_Excl_EP := Loval_Incl_EP;
7257 end if;
7259 else
7260 Loval_Excl_EP := Loval_Incl_EP;
7261 end if;
7263 -- Similar processing for upper bound and high value
7265 Model_Num := UR_Trunc (Hival / Small) * Small;
7267 if Hival = Model_Num then
7268 Hival_Incl_EP := Model_Num;
7269 else
7270 Hival_Incl_EP := Model_Num + Small;
7271 end if;
7273 if UR_Is_Positive (Hival_Incl_EP) then
7274 Hival_Excl_EP := Hival_Incl_EP - Small;
7275 else
7276 Hival_Excl_EP := Hival_Incl_EP;
7277 end if;
7279 -- One further adjustment is needed. In the case of subtypes, we
7280 -- cannot go outside the range of the base type, or we get
7281 -- peculiarities, and the base type range is already set. This
7282 -- only applies to the Incl values, since clearly the Excl values
7283 -- are already as restricted as they are allowed to be.
7285 if Typ /= Btyp then
7286 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
7287 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
7288 end if;
7290 -- Get size including and excluding end points
7292 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
7293 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
7295 -- No need to exclude end-points if it does not reduce size
7297 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
7298 Loval_Excl_EP := Loval_Incl_EP;
7299 end if;
7301 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
7302 Hival_Excl_EP := Hival_Incl_EP;
7303 end if;
7305 -- Now we set the actual size to be used. We want to use the
7306 -- bounds fudged up to include the end-points but only if this
7307 -- can be done without violating a specifically given size
7308 -- size clause or causing an unacceptable increase in size.
7310 -- Case of size clause given
7312 if Has_Size_Clause (Typ) then
7314 -- Use the inclusive size only if it is consistent with
7315 -- the explicitly specified size.
7317 if Size_Incl_EP <= RM_Size (Typ) then
7318 Actual_Lo := Loval_Incl_EP;
7319 Actual_Hi := Hival_Incl_EP;
7320 Actual_Size := Size_Incl_EP;
7322 -- If the inclusive size is too large, we try excluding
7323 -- the end-points (will be caught later if does not work).
7325 else
7326 Actual_Lo := Loval_Excl_EP;
7327 Actual_Hi := Hival_Excl_EP;
7328 Actual_Size := Size_Excl_EP;
7329 end if;
7331 -- Case of size clause not given
7333 else
7334 -- If we have a base type whose corresponding first subtype
7335 -- has an explicit size that is large enough to include our
7336 -- end-points, then do so. There is no point in working hard
7337 -- to get a base type whose size is smaller than the specified
7338 -- size of the first subtype.
7340 First_Subt := First_Subtype (Typ);
7342 if Has_Size_Clause (First_Subt)
7343 and then Size_Incl_EP <= Esize (First_Subt)
7344 then
7345 Actual_Size := Size_Incl_EP;
7346 Actual_Lo := Loval_Incl_EP;
7347 Actual_Hi := Hival_Incl_EP;
7349 -- If excluding the end-points makes the size smaller and
7350 -- results in a size of 8,16,32,64, then we take the smaller
7351 -- size. For the 64 case, this is compulsory. For the other
7352 -- cases, it seems reasonable. We like to include end points
7353 -- if we can, but not at the expense of moving to the next
7354 -- natural boundary of size.
7356 elsif Size_Incl_EP /= Size_Excl_EP
7357 and then Addressable (Size_Excl_EP)
7358 then
7359 Actual_Size := Size_Excl_EP;
7360 Actual_Lo := Loval_Excl_EP;
7361 Actual_Hi := Hival_Excl_EP;
7363 -- Otherwise we can definitely include the end points
7365 else
7366 Actual_Size := Size_Incl_EP;
7367 Actual_Lo := Loval_Incl_EP;
7368 Actual_Hi := Hival_Incl_EP;
7369 end if;
7371 -- One pathological case: normally we never fudge a low bound
7372 -- down, since it would seem to increase the size (if it has
7373 -- any effect), but for ranges containing single value, or no
7374 -- values, the high bound can be small too large. Consider:
7376 -- type t is delta 2.0**(-14)
7377 -- range 131072.0 .. 0;
7379 -- That lower bound is *just* outside the range of 32 bits, and
7380 -- does need fudging down in this case. Note that the bounds
7381 -- will always have crossed here, since the high bound will be
7382 -- fudged down if necessary, as in the case of:
7384 -- type t is delta 2.0**(-14)
7385 -- range 131072.0 .. 131072.0;
7387 -- So we detect the situation by looking for crossed bounds,
7388 -- and if the bounds are crossed, and the low bound is greater
7389 -- than zero, we will always back it off by small, since this
7390 -- is completely harmless.
7392 if Actual_Lo > Actual_Hi then
7393 if UR_Is_Positive (Actual_Lo) then
7394 Actual_Lo := Loval_Incl_EP - Small;
7395 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7397 -- And of course, we need to do exactly the same parallel
7398 -- fudge for flat ranges in the negative region.
7400 elsif UR_Is_Negative (Actual_Hi) then
7401 Actual_Hi := Hival_Incl_EP + Small;
7402 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7403 end if;
7404 end if;
7405 end if;
7407 Set_Realval (Lo, Actual_Lo);
7408 Set_Realval (Hi, Actual_Hi);
7409 end Fudge;
7411 -- For the decimal case, none of this fudging is required, since there
7412 -- are no end-point problems in the decimal case (the end-points are
7413 -- always included).
7415 else
7416 Actual_Size := Fsize (Loval, Hival);
7417 end if;
7419 -- At this stage, the actual size has been calculated and the proper
7420 -- required bounds are stored in the low and high bounds.
7422 if Actual_Size > 64 then
7423 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7424 Error_Msg_N
7425 ("size required (^) for type& too large, maximum allowed is 64",
7426 Typ);
7427 Actual_Size := 64;
7428 end if;
7430 -- Check size against explicit given size
7432 if Has_Size_Clause (Typ) then
7433 if Actual_Size > RM_Size (Typ) then
7434 Error_Msg_Uint_1 := RM_Size (Typ);
7435 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7436 Error_Msg_NE
7437 ("size given (^) for type& too small, minimum allowed is ^",
7438 Size_Clause (Typ), Typ);
7440 else
7441 Actual_Size := UI_To_Int (Esize (Typ));
7442 end if;
7444 -- Increase size to next natural boundary if no size clause given
7446 else
7447 if Actual_Size <= 8 then
7448 Actual_Size := 8;
7449 elsif Actual_Size <= 16 then
7450 Actual_Size := 16;
7451 elsif Actual_Size <= 32 then
7452 Actual_Size := 32;
7453 else
7454 Actual_Size := 64;
7455 end if;
7457 Init_Esize (Typ, Actual_Size);
7458 Adjust_Esize_For_Alignment (Typ);
7459 end if;
7461 -- If we have a base type, then expand the bounds so that they extend to
7462 -- the full width of the allocated size in bits, to avoid junk range
7463 -- checks on intermediate computations.
7465 if Base_Type (Typ) = Typ then
7466 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7467 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7468 end if;
7470 -- Final step is to reanalyze the bounds using the proper type
7471 -- and set the Corresponding_Integer_Value fields of the literals.
7473 Set_Etype (Lo, Empty);
7474 Set_Analyzed (Lo, False);
7475 Analyze (Lo);
7477 -- Resolve with universal fixed if the base type, and the base type if
7478 -- it is a subtype. Note we can't resolve the base type with itself,
7479 -- that would be a reference before definition.
7481 if Typ = Btyp then
7482 Resolve (Lo, Universal_Fixed);
7483 else
7484 Resolve (Lo, Btyp);
7485 end if;
7487 -- Set corresponding integer value for bound
7489 Set_Corresponding_Integer_Value
7490 (Lo, UR_To_Uint (Realval (Lo) / Small));
7492 -- Similar processing for high bound
7494 Set_Etype (Hi, Empty);
7495 Set_Analyzed (Hi, False);
7496 Analyze (Hi);
7498 if Typ = Btyp then
7499 Resolve (Hi, Universal_Fixed);
7500 else
7501 Resolve (Hi, Btyp);
7502 end if;
7504 Set_Corresponding_Integer_Value
7505 (Hi, UR_To_Uint (Realval (Hi) / Small));
7507 -- Set type of range to correspond to bounds
7509 Set_Etype (Rng, Etype (Lo));
7511 -- Set Esize to calculated size if not set already
7513 if Unknown_Esize (Typ) then
7514 Init_Esize (Typ, Actual_Size);
7515 end if;
7517 -- Set RM_Size if not already set. If already set, check value
7519 declare
7520 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7522 begin
7523 if RM_Size (Typ) /= Uint_0 then
7524 if RM_Size (Typ) < Minsiz then
7525 Error_Msg_Uint_1 := RM_Size (Typ);
7526 Error_Msg_Uint_2 := Minsiz;
7527 Error_Msg_NE
7528 ("size given (^) for type& too small, minimum allowed is ^",
7529 Size_Clause (Typ), Typ);
7530 end if;
7532 else
7533 Set_RM_Size (Typ, Minsiz);
7534 end if;
7535 end;
7537 -- Check for shaving
7539 if Comes_From_Source (Typ) then
7540 if Orig_Lo < Expr_Value_R (Lo) then
7541 Error_Msg_N
7542 ("declared low bound of type & is outside type range??", Typ);
7543 Error_Msg_N
7544 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
7545 end if;
7547 if Orig_Hi > Expr_Value_R (Hi) then
7548 Error_Msg_N
7549 ("declared high bound of type & is outside type range??", Typ);
7550 Error_Msg_N
7551 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
7552 end if;
7553 end if;
7554 end Freeze_Fixed_Point_Type;
7556 ------------------
7557 -- Freeze_Itype --
7558 ------------------
7560 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7561 L : List_Id;
7563 begin
7564 Set_Has_Delayed_Freeze (T);
7565 L := Freeze_Entity (T, N);
7567 if Is_Non_Empty_List (L) then
7568 Insert_Actions (N, L);
7569 end if;
7570 end Freeze_Itype;
7572 --------------------------
7573 -- Freeze_Static_Object --
7574 --------------------------
7576 procedure Freeze_Static_Object (E : Entity_Id) is
7578 Cannot_Be_Static : exception;
7579 -- Exception raised if the type of a static object cannot be made
7580 -- static. This happens if the type depends on non-global objects.
7582 procedure Ensure_Expression_Is_SA (N : Node_Id);
7583 -- Called to ensure that an expression used as part of a type definition
7584 -- is statically allocatable, which means that the expression type is
7585 -- statically allocatable, and the expression is either static, or a
7586 -- reference to a library level constant.
7588 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7589 -- Called to mark a type as static, checking that it is possible
7590 -- to set the type as static. If it is not possible, then the
7591 -- exception Cannot_Be_Static is raised.
7593 -----------------------------
7594 -- Ensure_Expression_Is_SA --
7595 -----------------------------
7597 procedure Ensure_Expression_Is_SA (N : Node_Id) is
7598 Ent : Entity_Id;
7600 begin
7601 Ensure_Type_Is_SA (Etype (N));
7603 if Is_OK_Static_Expression (N) then
7604 return;
7606 elsif Nkind (N) = N_Identifier then
7607 Ent := Entity (N);
7609 if Present (Ent)
7610 and then Ekind (Ent) = E_Constant
7611 and then Is_Library_Level_Entity (Ent)
7612 then
7613 return;
7614 end if;
7615 end if;
7617 raise Cannot_Be_Static;
7618 end Ensure_Expression_Is_SA;
7620 -----------------------
7621 -- Ensure_Type_Is_SA --
7622 -----------------------
7624 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7625 N : Node_Id;
7626 C : Entity_Id;
7628 begin
7629 -- If type is library level, we are all set
7631 if Is_Library_Level_Entity (Typ) then
7632 return;
7633 end if;
7635 -- We are also OK if the type already marked as statically allocated,
7636 -- which means we processed it before.
7638 if Is_Statically_Allocated (Typ) then
7639 return;
7640 end if;
7642 -- Mark type as statically allocated
7644 Set_Is_Statically_Allocated (Typ);
7646 -- Check that it is safe to statically allocate this type
7648 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
7649 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
7650 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
7652 elsif Is_Array_Type (Typ) then
7653 N := First_Index (Typ);
7654 while Present (N) loop
7655 Ensure_Type_Is_SA (Etype (N));
7656 Next_Index (N);
7657 end loop;
7659 Ensure_Type_Is_SA (Component_Type (Typ));
7661 elsif Is_Access_Type (Typ) then
7662 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
7664 declare
7665 F : Entity_Id;
7666 T : constant Entity_Id := Etype (Designated_Type (Typ));
7668 begin
7669 if T /= Standard_Void_Type then
7670 Ensure_Type_Is_SA (T);
7671 end if;
7673 F := First_Formal (Designated_Type (Typ));
7674 while Present (F) loop
7675 Ensure_Type_Is_SA (Etype (F));
7676 Next_Formal (F);
7677 end loop;
7678 end;
7680 else
7681 Ensure_Type_Is_SA (Designated_Type (Typ));
7682 end if;
7684 elsif Is_Record_Type (Typ) then
7685 C := First_Entity (Typ);
7686 while Present (C) loop
7687 if Ekind (C) = E_Discriminant
7688 or else Ekind (C) = E_Component
7689 then
7690 Ensure_Type_Is_SA (Etype (C));
7692 elsif Is_Type (C) then
7693 Ensure_Type_Is_SA (C);
7694 end if;
7696 Next_Entity (C);
7697 end loop;
7699 elsif Ekind (Typ) = E_Subprogram_Type then
7700 Ensure_Type_Is_SA (Etype (Typ));
7702 C := First_Formal (Typ);
7703 while Present (C) loop
7704 Ensure_Type_Is_SA (Etype (C));
7705 Next_Formal (C);
7706 end loop;
7708 else
7709 raise Cannot_Be_Static;
7710 end if;
7711 end Ensure_Type_Is_SA;
7713 -- Start of processing for Freeze_Static_Object
7715 begin
7716 Ensure_Type_Is_SA (Etype (E));
7718 exception
7719 when Cannot_Be_Static =>
7721 -- If the object that cannot be static is imported or exported, then
7722 -- issue an error message saying that this object cannot be imported
7723 -- or exported. If it has an address clause it is an overlay in the
7724 -- current partition and the static requirement is not relevant.
7725 -- Do not issue any error message when ignoring rep clauses.
7727 if Ignore_Rep_Clauses then
7728 null;
7730 elsif Is_Imported (E) then
7731 if No (Address_Clause (E)) then
7732 Error_Msg_N
7733 ("& cannot be imported (local type is not constant)", E);
7734 end if;
7736 -- Otherwise must be exported, something is wrong if compiler
7737 -- is marking something as statically allocated which cannot be).
7739 else pragma Assert (Is_Exported (E));
7740 Error_Msg_N
7741 ("& cannot be exported (local type is not constant)", E);
7742 end if;
7743 end Freeze_Static_Object;
7745 -----------------------
7746 -- Freeze_Subprogram --
7747 -----------------------
7749 procedure Freeze_Subprogram (E : Entity_Id) is
7750 Retype : Entity_Id;
7751 F : Entity_Id;
7753 begin
7754 -- Subprogram may not have an address clause unless it is imported
7756 if Present (Address_Clause (E)) then
7757 if not Is_Imported (E) then
7758 Error_Msg_N
7759 ("address clause can only be given " &
7760 "for imported subprogram",
7761 Name (Address_Clause (E)));
7762 end if;
7763 end if;
7765 -- Reset the Pure indication on an imported subprogram unless an
7766 -- explicit Pure_Function pragma was present or the subprogram is an
7767 -- intrinsic. We do this because otherwise it is an insidious error
7768 -- to call a non-pure function from pure unit and have calls
7769 -- mysteriously optimized away. What happens here is that the Import
7770 -- can bypass the normal check to ensure that pure units call only pure
7771 -- subprograms.
7773 -- The reason for the intrinsic exception is that in general, intrinsic
7774 -- functions (such as shifts) are pure anyway. The only exceptions are
7775 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
7776 -- in any case, so no problem arises.
7778 if Is_Imported (E)
7779 and then Is_Pure (E)
7780 and then not Has_Pragma_Pure_Function (E)
7781 and then not Is_Intrinsic_Subprogram (E)
7782 then
7783 Set_Is_Pure (E, False);
7784 end if;
7786 -- We also reset the Pure indication on a subprogram with an Address
7787 -- parameter, because the parameter may be used as a pointer and the
7788 -- referenced data may change even if the address value does not.
7790 -- Note that if the programmer gave an explicit Pure_Function pragma,
7791 -- then we believe the programmer, and leave the subprogram Pure.
7792 -- We also suppress this check on run-time files.
7794 if Is_Pure (E)
7795 and then Is_Subprogram (E)
7796 and then not Has_Pragma_Pure_Function (E)
7797 and then not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
7798 then
7799 Check_Function_With_Address_Parameter (E);
7800 end if;
7802 -- For non-foreign convention subprograms, this is where we create
7803 -- the extra formals (for accessibility level and constrained bit
7804 -- information). We delay this till the freeze point precisely so
7805 -- that we know the convention.
7807 if not Has_Foreign_Convention (E) then
7808 Create_Extra_Formals (E);
7809 Set_Mechanisms (E);
7811 -- If this is convention Ada and a Valued_Procedure, that's odd
7813 if Ekind (E) = E_Procedure
7814 and then Is_Valued_Procedure (E)
7815 and then Convention (E) = Convention_Ada
7816 and then Warn_On_Export_Import
7817 then
7818 Error_Msg_N
7819 ("??Valued_Procedure has no effect for convention Ada", E);
7820 Set_Is_Valued_Procedure (E, False);
7821 end if;
7823 -- Case of foreign convention
7825 else
7826 Set_Mechanisms (E);
7828 -- For foreign conventions, warn about return of unconstrained array
7830 if Ekind (E) = E_Function then
7831 Retype := Underlying_Type (Etype (E));
7833 -- If no return type, probably some other error, e.g. a
7834 -- missing full declaration, so ignore.
7836 if No (Retype) then
7837 null;
7839 -- If the return type is generic, we have emitted a warning
7840 -- earlier on, and there is nothing else to check here. Specific
7841 -- instantiations may lead to erroneous behavior.
7843 elsif Is_Generic_Type (Etype (E)) then
7844 null;
7846 -- Display warning if returning unconstrained array
7848 elsif Is_Array_Type (Retype)
7849 and then not Is_Constrained (Retype)
7851 -- Check appropriate warning is enabled (should we check for
7852 -- Warnings (Off) on specific entities here, probably so???)
7854 and then Warn_On_Export_Import
7855 then
7856 Error_Msg_N
7857 ("?x?foreign convention function& should not return " &
7858 "unconstrained array", E);
7859 return;
7860 end if;
7861 end if;
7863 -- If any of the formals for an exported foreign convention
7864 -- subprogram have defaults, then emit an appropriate warning since
7865 -- this is odd (default cannot be used from non-Ada code)
7867 if Is_Exported (E) then
7868 F := First_Formal (E);
7869 while Present (F) loop
7870 if Warn_On_Export_Import
7871 and then Present (Default_Value (F))
7872 then
7873 Error_Msg_N
7874 ("?x?parameter cannot be defaulted in non-Ada call",
7875 Default_Value (F));
7876 end if;
7878 Next_Formal (F);
7879 end loop;
7880 end if;
7881 end if;
7883 -- Pragma Inline_Always is disallowed for dispatching subprograms
7884 -- because the address of such subprograms is saved in the dispatch
7885 -- table to support dispatching calls, and dispatching calls cannot
7886 -- be inlined. This is consistent with the restriction against using
7887 -- 'Access or 'Address on an Inline_Always subprogram.
7889 if Is_Dispatching_Operation (E)
7890 and then Has_Pragma_Inline_Always (E)
7891 then
7892 Error_Msg_N
7893 ("pragma Inline_Always not allowed for dispatching subprograms", E);
7894 end if;
7896 -- Because of the implicit representation of inherited predefined
7897 -- operators in the front-end, the overriding status of the operation
7898 -- may be affected when a full view of a type is analyzed, and this is
7899 -- not captured by the analysis of the corresponding type declaration.
7900 -- Therefore the correctness of a not-overriding indicator must be
7901 -- rechecked when the subprogram is frozen.
7903 if Nkind (E) = N_Defining_Operator_Symbol
7904 and then not Error_Posted (Parent (E))
7905 then
7906 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
7907 end if;
7909 if Modify_Tree_For_C
7910 and then Nkind (Parent (E)) = N_Function_Specification
7911 and then Is_Array_Type (Etype (E))
7912 and then Is_Constrained (Etype (E))
7913 and then not Is_Unchecked_Conversion_Instance (E)
7914 and then not Rewritten_For_C (E)
7915 then
7916 Build_Procedure_Form (Unit_Declaration_Node (E));
7917 end if;
7918 end Freeze_Subprogram;
7920 ----------------------
7921 -- Is_Fully_Defined --
7922 ----------------------
7924 function Is_Fully_Defined (T : Entity_Id) return Boolean is
7925 begin
7926 if Ekind (T) = E_Class_Wide_Type then
7927 return Is_Fully_Defined (Etype (T));
7929 elsif Is_Array_Type (T) then
7930 return Is_Fully_Defined (Component_Type (T));
7932 elsif Is_Record_Type (T)
7933 and not Is_Private_Type (T)
7934 then
7935 -- Verify that the record type has no components with private types
7936 -- without completion.
7938 declare
7939 Comp : Entity_Id;
7941 begin
7942 Comp := First_Component (T);
7943 while Present (Comp) loop
7944 if not Is_Fully_Defined (Etype (Comp)) then
7945 return False;
7946 end if;
7948 Next_Component (Comp);
7949 end loop;
7950 return True;
7951 end;
7953 -- For the designated type of an access to subprogram, all types in
7954 -- the profile must be fully defined.
7956 elsif Ekind (T) = E_Subprogram_Type then
7957 declare
7958 F : Entity_Id;
7960 begin
7961 F := First_Formal (T);
7962 while Present (F) loop
7963 if not Is_Fully_Defined (Etype (F)) then
7964 return False;
7965 end if;
7967 Next_Formal (F);
7968 end loop;
7970 return Is_Fully_Defined (Etype (T));
7971 end;
7973 else
7974 return not Is_Private_Type (T)
7975 or else Present (Full_View (Base_Type (T)));
7976 end if;
7977 end Is_Fully_Defined;
7979 ---------------------------------
7980 -- Process_Default_Expressions --
7981 ---------------------------------
7983 procedure Process_Default_Expressions
7984 (E : Entity_Id;
7985 After : in out Node_Id)
7987 Loc : constant Source_Ptr := Sloc (E);
7988 Dbody : Node_Id;
7989 Formal : Node_Id;
7990 Dcopy : Node_Id;
7991 Dnam : Entity_Id;
7993 begin
7994 Set_Default_Expressions_Processed (E);
7996 -- A subprogram instance and its associated anonymous subprogram share
7997 -- their signature. The default expression functions are defined in the
7998 -- wrapper packages for the anonymous subprogram, and should not be
7999 -- generated again for the instance.
8001 if Is_Generic_Instance (E)
8002 and then Present (Alias (E))
8003 and then Default_Expressions_Processed (Alias (E))
8004 then
8005 return;
8006 end if;
8008 Formal := First_Formal (E);
8009 while Present (Formal) loop
8010 if Present (Default_Value (Formal)) then
8012 -- We work with a copy of the default expression because we
8013 -- do not want to disturb the original, since this would mess
8014 -- up the conformance checking.
8016 Dcopy := New_Copy_Tree (Default_Value (Formal));
8018 -- The analysis of the expression may generate insert actions,
8019 -- which of course must not be executed. We wrap those actions
8020 -- in a procedure that is not called, and later on eliminated.
8021 -- The following cases have no side-effects, and are analyzed
8022 -- directly.
8024 if Nkind (Dcopy) = N_Identifier
8025 or else Nkind_In (Dcopy, N_Expanded_Name,
8026 N_Integer_Literal,
8027 N_Character_Literal,
8028 N_String_Literal,
8029 N_Real_Literal)
8030 or else (Nkind (Dcopy) = N_Attribute_Reference
8031 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
8032 or else Known_Null (Dcopy)
8033 then
8034 -- If there is no default function, we must still do a full
8035 -- analyze call on the default value, to ensure that all error
8036 -- checks are performed, e.g. those associated with static
8037 -- evaluation. Note: this branch will always be taken if the
8038 -- analyzer is turned off (but we still need the error checks).
8040 -- Note: the setting of parent here is to meet the requirement
8041 -- that we can only analyze the expression while attached to
8042 -- the tree. Really the requirement is that the parent chain
8043 -- be set, we don't actually need to be in the tree.
8045 Set_Parent (Dcopy, Declaration_Node (Formal));
8046 Analyze (Dcopy);
8048 -- Default expressions are resolved with their own type if the
8049 -- context is generic, to avoid anomalies with private types.
8051 if Ekind (Scope (E)) = E_Generic_Package then
8052 Resolve (Dcopy);
8053 else
8054 Resolve (Dcopy, Etype (Formal));
8055 end if;
8057 -- If that resolved expression will raise constraint error,
8058 -- then flag the default value as raising constraint error.
8059 -- This allows a proper error message on the calls.
8061 if Raises_Constraint_Error (Dcopy) then
8062 Set_Raises_Constraint_Error (Default_Value (Formal));
8063 end if;
8065 -- If the default is a parameterless call, we use the name of
8066 -- the called function directly, and there is no body to build.
8068 elsif Nkind (Dcopy) = N_Function_Call
8069 and then No (Parameter_Associations (Dcopy))
8070 then
8071 null;
8073 -- Else construct and analyze the body of a wrapper procedure
8074 -- that contains an object declaration to hold the expression.
8075 -- Given that this is done only to complete the analysis, it is
8076 -- simpler to build a procedure than a function which might
8077 -- involve secondary stack expansion.
8079 else
8080 Dnam := Make_Temporary (Loc, 'D');
8082 Dbody :=
8083 Make_Subprogram_Body (Loc,
8084 Specification =>
8085 Make_Procedure_Specification (Loc,
8086 Defining_Unit_Name => Dnam),
8088 Declarations => New_List (
8089 Make_Object_Declaration (Loc,
8090 Defining_Identifier => Make_Temporary (Loc, 'T'),
8091 Object_Definition =>
8092 New_Occurrence_Of (Etype (Formal), Loc),
8093 Expression => New_Copy_Tree (Dcopy))),
8095 Handled_Statement_Sequence =>
8096 Make_Handled_Sequence_Of_Statements (Loc,
8097 Statements => Empty_List));
8099 Set_Scope (Dnam, Scope (E));
8100 Set_Assignment_OK (First (Declarations (Dbody)));
8101 Set_Is_Eliminated (Dnam);
8102 Insert_After (After, Dbody);
8103 Analyze (Dbody);
8104 After := Dbody;
8105 end if;
8106 end if;
8108 Next_Formal (Formal);
8109 end loop;
8110 end Process_Default_Expressions;
8112 ----------------------------------------
8113 -- Set_Component_Alignment_If_Not_Set --
8114 ----------------------------------------
8116 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
8117 begin
8118 -- Ignore if not base type, subtypes don't need anything
8120 if Typ /= Base_Type (Typ) then
8121 return;
8122 end if;
8124 -- Do not override existing representation
8126 if Is_Packed (Typ) then
8127 return;
8129 elsif Has_Specified_Layout (Typ) then
8130 return;
8132 elsif Component_Alignment (Typ) /= Calign_Default then
8133 return;
8135 else
8136 Set_Component_Alignment
8137 (Typ, Scope_Stack.Table
8138 (Scope_Stack.Last).Component_Alignment_Default);
8139 end if;
8140 end Set_Component_Alignment_If_Not_Set;
8142 --------------------------
8143 -- Set_SSO_From_Default --
8144 --------------------------
8146 procedure Set_SSO_From_Default (T : Entity_Id) is
8147 Reversed : Boolean;
8149 begin
8150 -- Set default SSO for an array or record base type, except in case of
8151 -- a type extension (which always inherits the SSO of its parent type).
8153 if Is_Base_Type (T)
8154 and then (Is_Array_Type (T)
8155 or else (Is_Record_Type (T)
8156 and then not (Is_Tagged_Type (T)
8157 and then Is_Derived_Type (T))))
8158 then
8159 Reversed :=
8160 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
8161 or else
8162 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
8164 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
8166 -- For a record type, if bit order is specified explicitly,
8167 -- then do not set SSO from default if not consistent. Note that
8168 -- we do not want to look at a Bit_Order attribute definition
8169 -- for a parent: if we were to inherit Bit_Order, then both
8170 -- SSO_Set_*_By_Default flags would have been cleared already
8171 -- (by Inherit_Aspects_At_Freeze_Point).
8173 and then not
8174 (Is_Record_Type (T)
8175 and then
8176 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
8177 and then Reverse_Bit_Order (T) /= Reversed)
8178 then
8179 -- If flags cause reverse storage order, then set the result. Note
8180 -- that we would have ignored the pragma setting the non default
8181 -- storage order in any case, hence the assertion at this point.
8183 pragma Assert
8184 (not Reversed or else Support_Nondefault_SSO_On_Target);
8186 Set_Reverse_Storage_Order (T, Reversed);
8188 -- For a record type, also set reversed bit order. Note: if a bit
8189 -- order has been specified explicitly, then this is a no-op.
8191 if Is_Record_Type (T) then
8192 Set_Reverse_Bit_Order (T, Reversed);
8193 end if;
8194 end if;
8195 end if;
8196 end Set_SSO_From_Default;
8198 ------------------
8199 -- Undelay_Type --
8200 ------------------
8202 procedure Undelay_Type (T : Entity_Id) is
8203 begin
8204 Set_Has_Delayed_Freeze (T, False);
8205 Set_Freeze_Node (T, Empty);
8207 -- Since we don't want T to have a Freeze_Node, we don't want its
8208 -- Full_View or Corresponding_Record_Type to have one either.
8210 -- ??? Fundamentally, this whole handling is unpleasant. What we really
8211 -- want is to be sure that for an Itype that's part of record R and is a
8212 -- subtype of type T, that it's frozen after the later of the freeze
8213 -- points of R and T. We have no way of doing that directly, so what we
8214 -- do is force most such Itypes to be frozen as part of freezing R via
8215 -- this procedure and only delay the ones that need to be delayed
8216 -- (mostly the designated types of access types that are defined as part
8217 -- of the record).
8219 if Is_Private_Type (T)
8220 and then Present (Full_View (T))
8221 and then Is_Itype (Full_View (T))
8222 and then Is_Record_Type (Scope (Full_View (T)))
8223 then
8224 Undelay_Type (Full_View (T));
8225 end if;
8227 if Is_Concurrent_Type (T)
8228 and then Present (Corresponding_Record_Type (T))
8229 and then Is_Itype (Corresponding_Record_Type (T))
8230 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
8231 then
8232 Undelay_Type (Corresponding_Record_Type (T));
8233 end if;
8234 end Undelay_Type;
8236 ------------------
8237 -- Warn_Overlay --
8238 ------------------
8240 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Entity_Id) is
8241 Ent : constant Entity_Id := Entity (Nam);
8242 -- The object to which the address clause applies
8244 Init : Node_Id;
8245 Old : Entity_Id := Empty;
8246 Decl : Node_Id;
8248 begin
8249 -- No warning if address clause overlay warnings are off
8251 if not Address_Clause_Overlay_Warnings then
8252 return;
8253 end if;
8255 -- No warning if there is an explicit initialization
8257 Init := Original_Node (Expression (Declaration_Node (Ent)));
8259 if Present (Init) and then Comes_From_Source (Init) then
8260 return;
8261 end if;
8263 -- We only give the warning for non-imported entities of a type for
8264 -- which a non-null base init proc is defined, or for objects of access
8265 -- types with implicit null initialization, or when Normalize_Scalars
8266 -- applies and the type is scalar or a string type (the latter being
8267 -- tested for because predefined String types are initialized by inline
8268 -- code rather than by an init_proc). Note that we do not give the
8269 -- warning for Initialize_Scalars, since we suppressed initialization
8270 -- in this case. Also, do not warn if Suppress_Initialization is set.
8272 if Present (Expr)
8273 and then not Is_Imported (Ent)
8274 and then not Initialization_Suppressed (Typ)
8275 and then (Has_Non_Null_Base_Init_Proc (Typ)
8276 or else Is_Access_Type (Typ)
8277 or else (Normalize_Scalars
8278 and then (Is_Scalar_Type (Typ)
8279 or else Is_String_Type (Typ))))
8280 then
8281 if Nkind (Expr) = N_Attribute_Reference
8282 and then Is_Entity_Name (Prefix (Expr))
8283 then
8284 Old := Entity (Prefix (Expr));
8286 elsif Is_Entity_Name (Expr)
8287 and then Ekind (Entity (Expr)) = E_Constant
8288 then
8289 Decl := Declaration_Node (Entity (Expr));
8291 if Nkind (Decl) = N_Object_Declaration
8292 and then Present (Expression (Decl))
8293 and then Nkind (Expression (Decl)) = N_Attribute_Reference
8294 and then Is_Entity_Name (Prefix (Expression (Decl)))
8295 then
8296 Old := Entity (Prefix (Expression (Decl)));
8298 elsif Nkind (Expr) = N_Function_Call then
8299 return;
8300 end if;
8302 -- A function call (most likely to To_Address) is probably not an
8303 -- overlay, so skip warning. Ditto if the function call was inlined
8304 -- and transformed into an entity.
8306 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
8307 return;
8308 end if;
8310 -- If a pragma Import follows, we assume that it is for the current
8311 -- target of the address clause, and skip the warning. There may be
8312 -- a source pragma or an aspect that specifies import and generates
8313 -- the corresponding pragma. These will indicate that the entity is
8314 -- imported and that is checked above so that the spurious warning
8315 -- (generated when the entity is frozen) will be suppressed. The
8316 -- pragma may be attached to the aspect, so it is not yet a list
8317 -- member.
8319 if Is_List_Member (Parent (Expr)) then
8320 Decl := Next (Parent (Expr));
8322 if Present (Decl)
8323 and then Nkind (Decl) = N_Pragma
8324 and then Pragma_Name (Decl) = Name_Import
8325 then
8326 return;
8327 end if;
8328 end if;
8330 -- Otherwise give warning message
8332 if Present (Old) then
8333 Error_Msg_Node_2 := Old;
8334 Error_Msg_N
8335 ("default initialization of & may modify &??",
8336 Nam);
8337 else
8338 Error_Msg_N
8339 ("default initialization of & may modify overlaid storage??",
8340 Nam);
8341 end if;
8343 -- Add friendly warning if initialization comes from a packed array
8344 -- component.
8346 if Is_Record_Type (Typ) then
8347 declare
8348 Comp : Entity_Id;
8350 begin
8351 Comp := First_Component (Typ);
8352 while Present (Comp) loop
8353 if Nkind (Parent (Comp)) = N_Component_Declaration
8354 and then Present (Expression (Parent (Comp)))
8355 then
8356 exit;
8357 elsif Is_Array_Type (Etype (Comp))
8358 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
8359 then
8360 Error_Msg_NE
8361 ("\packed array component& " &
8362 "will be initialized to zero??",
8363 Nam, Comp);
8364 exit;
8365 else
8366 Next_Component (Comp);
8367 end if;
8368 end loop;
8369 end;
8370 end if;
8372 Error_Msg_N
8373 ("\use pragma Import for & to " &
8374 "suppress initialization (RM B.1(24))??",
8375 Nam);
8376 end if;
8377 end Warn_Overlay;
8379 end Freeze;