freeze.adb: Freeze profile in ASIS mode.
[official-gcc.git] / gcc / ada / freeze.adb
blob2f6ab82eb80c5cb077defde377ffae2af0308309
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 (32 bits or less) in the Esize
749 -- field, of T checking for a size clause that was given which attempts
750 -- to give a smaller size, and also checking for an alignment clause.
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 > 32 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 scalar types. This is true even for generic formal
804 -- scalar types. We used to return False in the latter case, but the
805 -- size is known at compile time, even in the template, we just do
806 -- not know the exact size but that's not the point of this routine.
808 elsif Is_Scalar_Type (T)
809 or else Is_Task_Type (T)
810 then
811 return True;
813 -- Array types
815 elsif Is_Array_Type (T) then
817 -- String literals always have known size, and we can set it
819 if Ekind (T) = E_String_Literal_Subtype then
820 Set_Small_Size (T, Component_Size (T)
821 * String_Literal_Length (T));
822 return True;
824 -- Unconstrained types never have known at compile time size
826 elsif not Is_Constrained (T) then
827 return False;
829 -- Don't do any recursion on type with error posted, since we may
830 -- have a malformed type that leads us into a loop.
832 elsif Error_Posted (T) then
833 return False;
835 -- Otherwise if component size unknown, then array size unknown
837 elsif not Size_Known (Component_Type (T)) then
838 return False;
839 end if;
841 -- Check for all indexes static, and also compute possible size
842 -- (in case it is less than 32 and may be packable).
844 declare
845 Esiz : Uint := Component_Size (T);
846 Dim : Uint;
848 begin
849 Index := First_Index (T);
850 while Present (Index) loop
851 if Nkind (Index) = N_Range then
852 Get_Index_Bounds (Index, Low, High);
854 elsif Error_Posted (Scalar_Range (Etype (Index))) then
855 return False;
857 else
858 Low := Type_Low_Bound (Etype (Index));
859 High := Type_High_Bound (Etype (Index));
860 end if;
862 if not Compile_Time_Known_Value (Low)
863 or else not Compile_Time_Known_Value (High)
864 or else Etype (Index) = Any_Type
865 then
866 return False;
868 else
869 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
871 if Dim >= 0 then
872 Esiz := Esiz * Dim;
873 else
874 Esiz := Uint_0;
875 end if;
876 end if;
878 Next_Index (Index);
879 end loop;
881 Set_Small_Size (T, Esiz);
882 return True;
883 end;
885 -- Access types always have known at compile time sizes
887 elsif Is_Access_Type (T) then
888 return True;
890 -- For non-generic private types, go to underlying type if present
892 elsif Is_Private_Type (T)
893 and then not Is_Generic_Type (T)
894 and then Present (Underlying_Type (T))
895 then
896 -- Don't do any recursion on type with error posted, since we may
897 -- have a malformed type that leads us into a loop.
899 if Error_Posted (T) then
900 return False;
901 else
902 return Size_Known (Underlying_Type (T));
903 end if;
905 -- Record types
907 elsif Is_Record_Type (T) then
909 -- A class-wide type is never considered to have a known size
911 if Is_Class_Wide_Type (T) then
912 return False;
914 -- A subtype of a variant record must not have non-static
915 -- discriminated components.
917 elsif T /= Base_Type (T)
918 and then not Static_Discriminated_Components (T)
919 then
920 return False;
922 -- Don't do any recursion on type with error posted, since we may
923 -- have a malformed type that leads us into a loop.
925 elsif Error_Posted (T) then
926 return False;
927 end if;
929 -- Now look at the components of the record
931 declare
932 -- The following two variables are used to keep track of the
933 -- size of packed records if we can tell the size of the packed
934 -- record in the front end. Packed_Size_Known is True if so far
935 -- we can figure out the size. It is initialized to True for a
936 -- packed record, unless the record has discriminants or atomic
937 -- components or independent components.
939 -- The reason we eliminate the discriminated case is that
940 -- we don't know the way the back end lays out discriminated
941 -- packed records. If Packed_Size_Known is True, then
942 -- Packed_Size is the size in bits so far.
944 Packed_Size_Known : Boolean :=
945 Is_Packed (T)
946 and then not Has_Discriminants (T)
947 and then not Has_Atomic_Components (T)
948 and then not Has_Independent_Components (T);
950 Packed_Size : Uint := Uint_0;
951 -- Size in bits so far
953 begin
954 -- Test for variant part present
956 if Has_Discriminants (T)
957 and then Present (Parent (T))
958 and then Nkind (Parent (T)) = N_Full_Type_Declaration
959 and then Nkind (Type_Definition (Parent (T))) =
960 N_Record_Definition
961 and then not Null_Present (Type_Definition (Parent (T)))
962 and then
963 Present (Variant_Part
964 (Component_List (Type_Definition (Parent (T)))))
965 then
966 -- If variant part is present, and type is unconstrained,
967 -- then we must have defaulted discriminants, or a size
968 -- clause must be present for the type, or else the size
969 -- is definitely not known at compile time.
971 if not Is_Constrained (T)
972 and then
973 No (Discriminant_Default_Value (First_Discriminant (T)))
974 and then Unknown_RM_Size (T)
975 then
976 return False;
977 end if;
978 end if;
980 -- Loop through components
982 Comp := First_Component_Or_Discriminant (T);
983 while Present (Comp) loop
984 Ctyp := Etype (Comp);
986 -- We do not know the packed size if there is a component
987 -- clause present (we possibly could, but this would only
988 -- help in the case of a record with partial rep clauses.
989 -- That's because in the case of full rep clauses, the
990 -- size gets figured out anyway by a different circuit).
992 if Present (Component_Clause (Comp)) then
993 Packed_Size_Known := False;
994 end if;
996 -- We do not know the packed size for an atomic/VFA type
997 -- or component, or an independent type or component, or a
998 -- by-reference type or aliased component (because packing
999 -- does not touch these).
1001 if Is_Atomic_Or_VFA (Ctyp)
1002 or else Is_Atomic_Or_VFA (Comp)
1003 or else Is_Independent (Ctyp)
1004 or else Is_Independent (Comp)
1005 or else Is_By_Reference_Type (Ctyp)
1006 or else Is_Aliased (Comp)
1007 then
1008 Packed_Size_Known := False;
1009 end if;
1011 -- We need to identify a component that is an array where
1012 -- the index type is an enumeration type with non-standard
1013 -- representation, and some bound of the type depends on a
1014 -- discriminant.
1016 -- This is because gigi computes the size by doing a
1017 -- substitution of the appropriate discriminant value in
1018 -- the size expression for the base type, and gigi is not
1019 -- clever enough to evaluate the resulting expression (which
1020 -- involves a call to rep_to_pos) at compile time.
1022 -- It would be nice if gigi would either recognize that
1023 -- this expression can be computed at compile time, or
1024 -- alternatively figured out the size from the subtype
1025 -- directly, where all the information is at hand ???
1027 if Is_Array_Type (Etype (Comp))
1028 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1029 then
1030 declare
1031 Ocomp : constant Entity_Id :=
1032 Original_Record_Component (Comp);
1033 OCtyp : constant Entity_Id := Etype (Ocomp);
1034 Ind : Node_Id;
1035 Indtyp : Entity_Id;
1036 Lo, Hi : Node_Id;
1038 begin
1039 Ind := First_Index (OCtyp);
1040 while Present (Ind) loop
1041 Indtyp := Etype (Ind);
1043 if Is_Enumeration_Type (Indtyp)
1044 and then Has_Non_Standard_Rep (Indtyp)
1045 then
1046 Lo := Type_Low_Bound (Indtyp);
1047 Hi := Type_High_Bound (Indtyp);
1049 if Is_Entity_Name (Lo)
1050 and then Ekind (Entity (Lo)) = E_Discriminant
1051 then
1052 return False;
1054 elsif Is_Entity_Name (Hi)
1055 and then Ekind (Entity (Hi)) = E_Discriminant
1056 then
1057 return False;
1058 end if;
1059 end if;
1061 Next_Index (Ind);
1062 end loop;
1063 end;
1064 end if;
1066 -- Clearly size of record is not known if the size of one of
1067 -- the components is not known.
1069 if not Size_Known (Ctyp) then
1070 return False;
1071 end if;
1073 -- Accumulate packed size if possible
1075 if Packed_Size_Known then
1077 -- We can only deal with elementary types, since for
1078 -- non-elementary components, alignment enters into the
1079 -- picture, and we don't know enough to handle proper
1080 -- alignment in this context. Packed arrays count as
1081 -- elementary if the representation is a modular type.
1083 if Is_Elementary_Type (Ctyp)
1084 or else (Is_Array_Type (Ctyp)
1085 and then Present
1086 (Packed_Array_Impl_Type (Ctyp))
1087 and then Is_Modular_Integer_Type
1088 (Packed_Array_Impl_Type (Ctyp)))
1089 then
1090 -- Packed size unknown if we have an atomic/VFA type
1091 -- or a by-reference type, since the back end knows
1092 -- how these are layed out.
1094 if Is_Atomic_Or_VFA (Ctyp)
1095 or else Is_By_Reference_Type (Ctyp)
1096 then
1097 Packed_Size_Known := False;
1099 -- If RM_Size is known and static, then we can keep
1100 -- accumulating the packed size
1102 elsif Known_Static_RM_Size (Ctyp) then
1104 -- A little glitch, to be removed sometime ???
1105 -- gigi does not understand zero sizes yet.
1107 if RM_Size (Ctyp) = Uint_0 then
1108 Packed_Size_Known := False;
1110 -- Normal case where we can keep accumulating the
1111 -- packed array size.
1113 else
1114 Packed_Size := Packed_Size + RM_Size (Ctyp);
1115 end if;
1117 -- If we have a field whose RM_Size is not known then
1118 -- we can't figure out the packed size here.
1120 else
1121 Packed_Size_Known := False;
1122 end if;
1124 -- If we have a non-elementary type we can't figure out
1125 -- the packed array size (alignment issues).
1127 else
1128 Packed_Size_Known := False;
1129 end if;
1130 end if;
1132 Next_Component_Or_Discriminant (Comp);
1133 end loop;
1135 if Packed_Size_Known then
1136 Set_Small_Size (T, Packed_Size);
1137 end if;
1139 return True;
1140 end;
1142 -- All other cases, size not known at compile time
1144 else
1145 return False;
1146 end if;
1147 end Size_Known;
1149 -------------------------------------
1150 -- Static_Discriminated_Components --
1151 -------------------------------------
1153 function Static_Discriminated_Components
1154 (T : Entity_Id) return Boolean
1156 Constraint : Elmt_Id;
1158 begin
1159 if Has_Discriminants (T)
1160 and then Present (Discriminant_Constraint (T))
1161 and then Present (First_Component (T))
1162 then
1163 Constraint := First_Elmt (Discriminant_Constraint (T));
1164 while Present (Constraint) loop
1165 if not Compile_Time_Known_Value (Node (Constraint)) then
1166 return False;
1167 end if;
1169 Next_Elmt (Constraint);
1170 end loop;
1171 end if;
1173 return True;
1174 end Static_Discriminated_Components;
1176 -- Start of processing for Check_Compile_Time_Size
1178 begin
1179 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1180 end Check_Compile_Time_Size;
1182 -----------------------------------
1183 -- Check_Component_Storage_Order --
1184 -----------------------------------
1186 procedure Check_Component_Storage_Order
1187 (Encl_Type : Entity_Id;
1188 Comp : Entity_Id;
1189 ADC : Node_Id;
1190 Comp_ADC_Present : out Boolean)
1192 Comp_Type : Entity_Id;
1193 Comp_ADC : Node_Id;
1194 Err_Node : Node_Id;
1196 Comp_Byte_Aligned : Boolean;
1197 -- Set for the record case, True if Comp starts on a byte boundary
1198 -- (in which case it is allowed to have different storage order).
1200 Comp_SSO_Differs : Boolean;
1201 -- Set True when the component is a nested composite, and it does not
1202 -- have the same scalar storage order as Encl_Type.
1204 Component_Aliased : Boolean;
1206 begin
1207 -- Record case
1209 if Present (Comp) then
1210 Err_Node := Comp;
1211 Comp_Type := Etype (Comp);
1213 if Is_Tag (Comp) then
1214 Comp_Byte_Aligned := True;
1215 Component_Aliased := False;
1217 else
1218 -- If a component clause is present, check if the component starts
1219 -- on a storage element boundary. Otherwise conservatively assume
1220 -- it does so only in the case where the record is not packed.
1222 if Present (Component_Clause (Comp)) then
1223 Comp_Byte_Aligned :=
1224 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0;
1225 else
1226 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1227 end if;
1229 Component_Aliased := Is_Aliased (Comp);
1230 end if;
1232 -- Array case
1234 else
1235 Err_Node := Encl_Type;
1236 Comp_Type := Component_Type (Encl_Type);
1238 Component_Aliased := Has_Aliased_Components (Encl_Type);
1239 end if;
1241 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1242 -- the attribute definition clause is attached to the first subtype.
1244 Comp_Type := Base_Type (Comp_Type);
1245 Comp_ADC := Get_Attribute_Definition_Clause
1246 (First_Subtype (Comp_Type),
1247 Attribute_Scalar_Storage_Order);
1248 Comp_ADC_Present := Present (Comp_ADC);
1250 -- Case of record or array component: check storage order compatibility.
1251 -- But, if the record has Complex_Representation, then it is treated as
1252 -- a scalar in the back end so the storage order is irrelevant.
1254 if (Is_Record_Type (Comp_Type)
1255 and then not Has_Complex_Representation (Comp_Type))
1256 or else Is_Array_Type (Comp_Type)
1257 then
1258 Comp_SSO_Differs :=
1259 Reverse_Storage_Order (Encl_Type)
1261 Reverse_Storage_Order (Comp_Type);
1263 -- Parent and extension must have same storage order
1265 if Present (Comp) and then Chars (Comp) = Name_uParent then
1266 if Comp_SSO_Differs then
1267 Error_Msg_N
1268 ("record extension must have same scalar storage order as "
1269 & "parent", Err_Node);
1270 end if;
1272 -- If enclosing composite has explicit SSO then nested composite must
1273 -- have explicit SSO as well.
1275 elsif Present (ADC) and then No (Comp_ADC) then
1276 Error_Msg_N ("nested composite must have explicit scalar "
1277 & "storage order", Err_Node);
1279 -- If component and composite SSO differs, check that component
1280 -- falls on byte boundaries and isn't packed.
1282 elsif Comp_SSO_Differs then
1284 -- Component SSO differs from enclosing composite:
1286 -- Reject if component is a packed array, as it may be represented
1287 -- as a scalar internally.
1289 if Is_Packed_Array (Comp_Type) then
1290 Error_Msg_N
1291 ("type of packed component must have same scalar "
1292 & "storage order as enclosing composite", Err_Node);
1294 -- Reject if composite is a packed array, as it may be rewritten
1295 -- into an array of scalars.
1297 elsif Is_Packed_Array (Encl_Type) then
1298 Error_Msg_N ("type of packed array must have same scalar "
1299 & "storage order as component", Err_Node);
1301 -- Reject if not byte aligned
1303 elsif Is_Record_Type (Encl_Type)
1304 and then not Comp_Byte_Aligned
1305 then
1306 Error_Msg_N
1307 ("type of non-byte-aligned component must have same scalar "
1308 & "storage order as enclosing composite", Err_Node);
1309 end if;
1310 end if;
1312 -- Enclosing type has explicit SSO: non-composite component must not
1313 -- be aliased.
1315 elsif Present (ADC) and then Component_Aliased then
1316 Error_Msg_N
1317 ("aliased component not permitted for type with "
1318 & "explicit Scalar_Storage_Order", Err_Node);
1319 end if;
1320 end Check_Component_Storage_Order;
1322 -----------------------------
1323 -- Check_Debug_Info_Needed --
1324 -----------------------------
1326 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1327 begin
1328 if Debug_Info_Off (T) then
1329 return;
1331 elsif Comes_From_Source (T)
1332 or else Debug_Generated_Code
1333 or else Debug_Flag_VV
1334 or else Needs_Debug_Info (T)
1335 then
1336 Set_Debug_Info_Needed (T);
1337 end if;
1338 end Check_Debug_Info_Needed;
1340 -------------------------------
1341 -- Check_Expression_Function --
1342 -------------------------------
1344 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1345 Decl : Node_Id;
1347 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1348 -- Function to search for deferred constant
1350 -------------------
1351 -- Find_Constant --
1352 -------------------
1354 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1355 begin
1356 -- When a constant is initialized with the result of a dispatching
1357 -- call, the constant declaration is rewritten as a renaming of the
1358 -- displaced function result. This scenario is not a premature use of
1359 -- a constant even though the Has_Completion flag is not set.
1361 if Is_Entity_Name (Nod)
1362 and then Present (Entity (Nod))
1363 and then Ekind (Entity (Nod)) = E_Constant
1364 and then Scope (Entity (Nod)) = Current_Scope
1365 and then Nkind (Declaration_Node (Entity (Nod))) =
1366 N_Object_Declaration
1367 and then not Is_Imported (Entity (Nod))
1368 and then not Has_Completion (Entity (Nod))
1369 then
1370 Error_Msg_NE
1371 ("premature use of& in call or instance", N, Entity (Nod));
1373 elsif Nkind (Nod) = N_Attribute_Reference then
1374 Analyze (Prefix (Nod));
1376 if Is_Entity_Name (Prefix (Nod))
1377 and then Is_Type (Entity (Prefix (Nod)))
1378 then
1379 Freeze_Before (N, Entity (Prefix (Nod)));
1380 end if;
1381 end if;
1383 return OK;
1384 end Find_Constant;
1386 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1388 -- Start of processing for Check_Expression_Function
1390 begin
1391 Decl := Original_Node (Unit_Declaration_Node (Nam));
1393 if Scope (Nam) = Current_Scope
1394 and then Nkind (Decl) = N_Expression_Function
1395 then
1396 Check_Deferred (Expression (Decl));
1397 end if;
1398 end Check_Expression_Function;
1400 ----------------------------
1401 -- Check_Strict_Alignment --
1402 ----------------------------
1404 procedure Check_Strict_Alignment (E : Entity_Id) is
1405 Comp : Entity_Id;
1407 begin
1408 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1409 Set_Strict_Alignment (E);
1411 elsif Is_Array_Type (E) then
1412 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1414 elsif Is_Record_Type (E) then
1415 if Is_Limited_Record (E) then
1416 Set_Strict_Alignment (E);
1417 return;
1418 end if;
1420 Comp := First_Component (E);
1421 while Present (Comp) loop
1422 if not Is_Type (Comp)
1423 and then (Strict_Alignment (Etype (Comp))
1424 or else Is_Aliased (Comp))
1425 then
1426 Set_Strict_Alignment (E);
1427 return;
1428 end if;
1430 Next_Component (Comp);
1431 end loop;
1432 end if;
1433 end Check_Strict_Alignment;
1435 -------------------------
1436 -- Check_Unsigned_Type --
1437 -------------------------
1439 procedure Check_Unsigned_Type (E : Entity_Id) is
1440 Ancestor : Entity_Id;
1441 Lo_Bound : Node_Id;
1442 Btyp : Entity_Id;
1444 begin
1445 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1446 return;
1447 end if;
1449 -- Do not attempt to analyze case where range was in error
1451 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
1452 return;
1453 end if;
1455 -- The situation that is nontrivial is something like:
1457 -- subtype x1 is integer range -10 .. +10;
1458 -- subtype x2 is x1 range 0 .. V1;
1459 -- subtype x3 is x2 range V2 .. V3;
1460 -- subtype x4 is x3 range V4 .. V5;
1462 -- where Vn are variables. Here the base type is signed, but we still
1463 -- know that x4 is unsigned because of the lower bound of x2.
1465 -- The only way to deal with this is to look up the ancestor chain
1467 Ancestor := E;
1468 loop
1469 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1470 return;
1471 end if;
1473 Lo_Bound := Type_Low_Bound (Ancestor);
1475 if Compile_Time_Known_Value (Lo_Bound) then
1476 if Expr_Rep_Value (Lo_Bound) >= 0 then
1477 Set_Is_Unsigned_Type (E, True);
1478 end if;
1480 return;
1482 else
1483 Ancestor := Ancestor_Subtype (Ancestor);
1485 -- If no ancestor had a static lower bound, go to base type
1487 if No (Ancestor) then
1489 -- Note: the reason we still check for a compile time known
1490 -- value for the base type is that at least in the case of
1491 -- generic formals, we can have bounds that fail this test,
1492 -- and there may be other cases in error situations.
1494 Btyp := Base_Type (E);
1496 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1497 return;
1498 end if;
1500 Lo_Bound := Type_Low_Bound (Base_Type (E));
1502 if Compile_Time_Known_Value (Lo_Bound)
1503 and then Expr_Rep_Value (Lo_Bound) >= 0
1504 then
1505 Set_Is_Unsigned_Type (E, True);
1506 end if;
1508 return;
1509 end if;
1510 end if;
1511 end loop;
1512 end Check_Unsigned_Type;
1514 -----------------------------
1515 -- Is_Atomic_VFA_Aggregate --
1516 -----------------------------
1518 function Is_Atomic_VFA_Aggregate (N : Node_Id) return Boolean is
1519 Loc : constant Source_Ptr := Sloc (N);
1520 New_N : Node_Id;
1521 Par : Node_Id;
1522 Temp : Entity_Id;
1523 Typ : Entity_Id;
1525 begin
1526 Par := Parent (N);
1528 -- Array may be qualified, so find outer context
1530 if Nkind (Par) = N_Qualified_Expression then
1531 Par := Parent (Par);
1532 end if;
1534 if not Comes_From_Source (Par) then
1535 return False;
1536 end if;
1538 case Nkind (Par) is
1539 when N_Assignment_Statement =>
1540 Typ := Etype (Name (Par));
1542 if not Is_Atomic_Or_VFA (Typ)
1543 and then not (Is_Entity_Name (Name (Par))
1544 and then Is_Atomic_Or_VFA (Entity (Name (Par))))
1545 then
1546 return False;
1547 end if;
1549 when N_Object_Declaration =>
1550 Typ := Etype (Defining_Identifier (Par));
1552 if not Is_Atomic_Or_VFA (Typ)
1553 and then not Is_Atomic_Or_VFA (Defining_Identifier (Par))
1554 then
1555 return False;
1556 end if;
1558 when others =>
1559 return False;
1560 end case;
1562 Temp := Make_Temporary (Loc, 'T', N);
1563 New_N :=
1564 Make_Object_Declaration (Loc,
1565 Defining_Identifier => Temp,
1566 Object_Definition => New_Occurrence_Of (Typ, Loc),
1567 Expression => Relocate_Node (N));
1568 Insert_Before (Par, New_N);
1569 Analyze (New_N);
1571 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1572 return True;
1573 end Is_Atomic_VFA_Aggregate;
1575 -----------------------------------------------
1576 -- Explode_Initialization_Compound_Statement --
1577 -----------------------------------------------
1579 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
1580 Init_Stmts : constant Node_Id := Initialization_Statements (E);
1582 begin
1583 if Present (Init_Stmts)
1584 and then Nkind (Init_Stmts) = N_Compound_Statement
1585 then
1586 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
1588 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
1589 -- just removing it, because Freeze_All may rely on this particular
1590 -- Node_Id still being present in the enclosing list to know where to
1591 -- stop freezing.
1593 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
1595 Set_Initialization_Statements (E, Empty);
1596 end if;
1597 end Explode_Initialization_Compound_Statement;
1599 ----------------
1600 -- Freeze_All --
1601 ----------------
1603 -- Note: the easy coding for this procedure would be to just build a
1604 -- single list of freeze nodes and then insert them and analyze them
1605 -- all at once. This won't work, because the analysis of earlier freeze
1606 -- nodes may recursively freeze types which would otherwise appear later
1607 -- on in the freeze list. So we must analyze and expand the freeze nodes
1608 -- as they are generated.
1610 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1611 E : Entity_Id;
1612 Decl : Node_Id;
1614 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1615 -- This is the internal recursive routine that does freezing of entities
1616 -- (but NOT the analysis of default expressions, which should not be
1617 -- recursive, we don't want to analyze those till we are sure that ALL
1618 -- the types are frozen).
1620 --------------------
1621 -- Freeze_All_Ent --
1622 --------------------
1624 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
1625 E : Entity_Id;
1626 Flist : List_Id;
1627 Lastn : Node_Id;
1629 procedure Process_Flist;
1630 -- If freeze nodes are present, insert and analyze, and reset cursor
1631 -- for next insertion.
1633 -------------------
1634 -- Process_Flist --
1635 -------------------
1637 procedure Process_Flist is
1638 begin
1639 if Is_Non_Empty_List (Flist) then
1640 Lastn := Next (After);
1641 Insert_List_After_And_Analyze (After, Flist);
1643 if Present (Lastn) then
1644 After := Prev (Lastn);
1645 else
1646 After := Last (List_Containing (After));
1647 end if;
1648 end if;
1649 end Process_Flist;
1651 -- Start of processing for Freeze_All_Ent
1653 begin
1654 E := From;
1655 while Present (E) loop
1657 -- If the entity is an inner package which is not a package
1658 -- renaming, then its entities must be frozen at this point. Note
1659 -- that such entities do NOT get frozen at the end of the nested
1660 -- package itself (only library packages freeze).
1662 -- Same is true for task declarations, where anonymous records
1663 -- created for entry parameters must be frozen.
1665 if Ekind (E) = E_Package
1666 and then No (Renamed_Object (E))
1667 and then not Is_Child_Unit (E)
1668 and then not Is_Frozen (E)
1669 then
1670 Push_Scope (E);
1671 Install_Visible_Declarations (E);
1672 Install_Private_Declarations (E);
1674 Freeze_All (First_Entity (E), After);
1676 End_Package_Scope (E);
1678 if Is_Generic_Instance (E)
1679 and then Has_Delayed_Freeze (E)
1680 then
1681 Set_Has_Delayed_Freeze (E, False);
1682 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1683 end if;
1685 elsif Ekind (E) in Task_Kind
1686 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1687 N_Single_Task_Declaration)
1688 then
1689 Push_Scope (E);
1690 Freeze_All (First_Entity (E), After);
1691 End_Scope;
1693 -- For a derived tagged type, we must ensure that all the
1694 -- primitive operations of the parent have been frozen, so that
1695 -- their addresses will be in the parent's dispatch table at the
1696 -- point it is inherited.
1698 elsif Ekind (E) = E_Record_Type
1699 and then Is_Tagged_Type (E)
1700 and then Is_Tagged_Type (Etype (E))
1701 and then Is_Derived_Type (E)
1702 then
1703 declare
1704 Prim_List : constant Elist_Id :=
1705 Primitive_Operations (Etype (E));
1707 Prim : Elmt_Id;
1708 Subp : Entity_Id;
1710 begin
1711 Prim := First_Elmt (Prim_List);
1712 while Present (Prim) loop
1713 Subp := Node (Prim);
1715 if Comes_From_Source (Subp)
1716 and then not Is_Frozen (Subp)
1717 then
1718 Flist := Freeze_Entity (Subp, After);
1719 Process_Flist;
1720 end if;
1722 Next_Elmt (Prim);
1723 end loop;
1724 end;
1725 end if;
1727 if not Is_Frozen (E) then
1728 Flist := Freeze_Entity (E, After);
1729 Process_Flist;
1731 -- If already frozen, and there are delayed aspects, this is where
1732 -- we do the visibility check for these aspects (see Sem_Ch13 spec
1733 -- for a description of how we handle aspect visibility).
1735 elsif Has_Delayed_Aspects (E) then
1737 -- Retrieve the visibility to the discriminants in order to
1738 -- analyze properly the aspects.
1740 Push_Scope_And_Install_Discriminants (E);
1742 declare
1743 Ritem : Node_Id;
1745 begin
1746 Ritem := First_Rep_Item (E);
1747 while Present (Ritem) loop
1748 if Nkind (Ritem) = N_Aspect_Specification
1749 and then Entity (Ritem) = E
1750 and then Is_Delayed_Aspect (Ritem)
1751 then
1752 Check_Aspect_At_End_Of_Declarations (Ritem);
1753 end if;
1755 Ritem := Next_Rep_Item (Ritem);
1756 end loop;
1757 end;
1759 Uninstall_Discriminants_And_Pop_Scope (E);
1760 end if;
1762 -- If an incomplete type is still not frozen, this may be a
1763 -- premature freezing because of a body declaration that follows.
1764 -- Indicate where the freezing took place. Freezing will happen
1765 -- if the body comes from source, but not if it is internally
1766 -- generated, for example as the body of a type invariant.
1768 -- If the freezing is caused by the end of the current declarative
1769 -- part, it is a Taft Amendment type, and there is no error.
1771 if not Is_Frozen (E)
1772 and then Ekind (E) = E_Incomplete_Type
1773 then
1774 declare
1775 Bod : constant Node_Id := Next (After);
1777 begin
1778 -- The presence of a body freezes all entities previously
1779 -- declared in the current list of declarations, but this
1780 -- does not apply if the body does not come from source.
1781 -- A type invariant is transformed into a subprogram body
1782 -- which is placed at the end of the private part of the
1783 -- current package, but this body does not freeze incomplete
1784 -- types that may be declared in this private part.
1786 if (Nkind_In (Bod, N_Subprogram_Body,
1787 N_Entry_Body,
1788 N_Package_Body,
1789 N_Protected_Body,
1790 N_Task_Body)
1791 or else Nkind (Bod) in N_Body_Stub)
1792 and then
1793 List_Containing (After) = List_Containing (Parent (E))
1794 and then Comes_From_Source (Bod)
1795 then
1796 Error_Msg_Sloc := Sloc (Next (After));
1797 Error_Msg_NE
1798 ("type& is frozen# before its full declaration",
1799 Parent (E), E);
1800 end if;
1801 end;
1802 end if;
1804 Next_Entity (E);
1805 end loop;
1806 end Freeze_All_Ent;
1808 -- Start of processing for Freeze_All
1810 begin
1811 Freeze_All_Ent (From, After);
1813 -- Now that all types are frozen, we can deal with default expressions
1814 -- that require us to build a default expression functions. This is the
1815 -- point at which such functions are constructed (after all types that
1816 -- might be used in such expressions have been frozen).
1818 -- For subprograms that are renaming_as_body, we create the wrapper
1819 -- bodies as needed.
1821 -- We also add finalization chains to access types whose designated
1822 -- types are controlled. This is normally done when freezing the type,
1823 -- but this misses recursive type definitions where the later members
1824 -- of the recursion introduce controlled components.
1826 -- Loop through entities
1828 E := From;
1829 while Present (E) loop
1830 if Is_Subprogram (E) then
1831 if not Default_Expressions_Processed (E) then
1832 Process_Default_Expressions (E, After);
1833 end if;
1835 if not Has_Completion (E) then
1836 Decl := Unit_Declaration_Node (E);
1838 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1839 if Error_Posted (Decl) then
1840 Set_Has_Completion (E);
1841 else
1842 Build_And_Analyze_Renamed_Body (Decl, E, After);
1843 end if;
1845 elsif Nkind (Decl) = N_Subprogram_Declaration
1846 and then Present (Corresponding_Body (Decl))
1847 and then
1848 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1849 = N_Subprogram_Renaming_Declaration
1850 then
1851 Build_And_Analyze_Renamed_Body
1852 (Decl, Corresponding_Body (Decl), After);
1853 end if;
1854 end if;
1856 elsif Ekind (E) in Task_Kind
1857 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1858 N_Single_Task_Declaration)
1859 then
1860 declare
1861 Ent : Entity_Id;
1863 begin
1864 Ent := First_Entity (E);
1865 while Present (Ent) loop
1866 if Is_Entry (Ent)
1867 and then not Default_Expressions_Processed (Ent)
1868 then
1869 Process_Default_Expressions (Ent, After);
1870 end if;
1872 Next_Entity (Ent);
1873 end loop;
1874 end;
1875 end if;
1877 -- Historical note: We used to create a finalization master for an
1878 -- access type whose designated type is not controlled, but contains
1879 -- private controlled compoments. This form of postprocessing is no
1880 -- longer needed because the finalization master is now created when
1881 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
1883 Next_Entity (E);
1884 end loop;
1885 end Freeze_All;
1887 -----------------------
1888 -- Freeze_And_Append --
1889 -----------------------
1891 procedure Freeze_And_Append
1892 (Ent : Entity_Id;
1893 N : Node_Id;
1894 Result : in out List_Id)
1896 L : constant List_Id := Freeze_Entity (Ent, N);
1897 begin
1898 if Is_Non_Empty_List (L) then
1899 if Result = No_List then
1900 Result := L;
1901 else
1902 Append_List (L, Result);
1903 end if;
1904 end if;
1905 end Freeze_And_Append;
1907 -------------------
1908 -- Freeze_Before --
1909 -------------------
1911 procedure Freeze_Before
1912 (N : Node_Id;
1913 T : Entity_Id;
1914 Do_Freeze_Profile : Boolean := True)
1916 -- Freeze T, then insert the generated Freeze nodes before the node N.
1917 -- Flag Freeze_Profile is used when T is an overloadable entity, and
1918 -- indicates whether its profile should be frozen at the same time.
1920 Freeze_Nodes : constant List_Id :=
1921 Freeze_Entity (T, N, Do_Freeze_Profile);
1923 begin
1924 if Ekind (T) = E_Function then
1925 Check_Expression_Function (N, T);
1926 end if;
1928 if Is_Non_Empty_List (Freeze_Nodes) then
1929 Insert_Actions (N, Freeze_Nodes);
1930 end if;
1931 end Freeze_Before;
1933 -------------------
1934 -- Freeze_Entity --
1935 -------------------
1937 function Freeze_Entity
1938 (E : Entity_Id;
1939 N : Node_Id;
1940 Do_Freeze_Profile : Boolean := True) return List_Id
1942 Loc : constant Source_Ptr := Sloc (N);
1943 Atype : Entity_Id;
1944 Comp : Entity_Id;
1945 F_Node : Node_Id;
1946 Formal : Entity_Id;
1947 Indx : Node_Id;
1949 Has_Default_Initialization : Boolean := False;
1950 -- This flag gets set to true for a variable with default initialization
1952 Late_Freezing : Boolean := False;
1953 -- Used to detect attempt to freeze function declared in another unit
1955 Result : List_Id := No_List;
1956 -- List of freezing actions, left at No_List if none
1958 Test_E : Entity_Id := E;
1959 -- This could use a comment ???
1961 procedure Add_To_Result (N : Node_Id);
1962 -- N is a freezing action to be appended to the Result
1964 function After_Last_Declaration return Boolean;
1965 -- If Loc is a freeze_entity that appears after the last declaration
1966 -- in the scope, inhibit error messages on late completion.
1968 procedure Check_Current_Instance (Comp_Decl : Node_Id);
1969 -- Check that an Access or Unchecked_Access attribute with a prefix
1970 -- which is the current instance type can only be applied when the type
1971 -- is limited.
1973 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
1974 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
1975 -- integer literal without an explicit corresponding size clause. The
1976 -- caller has checked that Utype is a modular integer type.
1978 procedure Freeze_Array_Type (Arr : Entity_Id);
1979 -- Freeze array type, including freezing index and component types
1981 procedure Freeze_Object_Declaration (E : Entity_Id);
1982 -- Perform checks and generate freeze node if needed for a constant or
1983 -- variable declared by an object declaration.
1985 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
1986 -- Create Freeze_Generic_Entity nodes for types declared in a generic
1987 -- package. Recurse on inner generic packages.
1989 function Freeze_Profile (E : Entity_Id) return Boolean;
1990 -- Freeze formals and return type of subprogram. If some type in the
1991 -- profile is a limited view, freezing of the entity will take place
1992 -- elsewhere, and the function returns False. This routine will be
1993 -- modified if and when we can implement AI05-019 efficiently ???
1995 procedure Freeze_Record_Type (Rec : Entity_Id);
1996 -- Freeze record type, including freezing component types, and freezing
1997 -- primitive operations if this is a tagged type.
1999 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2000 -- Determine whether an arbitrary entity is subject to Boolean aspect
2001 -- Import and its value is specified as True.
2003 procedure Late_Freeze_Subprogram (E : Entity_Id);
2004 -- Following AI05-151, a function can return a limited view of a type
2005 -- declared elsewhere. In that case the function cannot be frozen at
2006 -- the end of its enclosing package. If its first use is in a different
2007 -- unit, it cannot be frozen there, but if the call is legal the full
2008 -- view of the return type is available and the subprogram can now be
2009 -- frozen. However the freeze node cannot be inserted at the point of
2010 -- call, but rather must go in the package holding the function, so that
2011 -- the backend can process it in the proper context.
2013 function New_Freeze_Node return Node_Id;
2014 -- Create a new freeze node for entity E
2016 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2017 -- If E is an entity for an imported subprogram with pre/post-conditions
2018 -- then this procedure will create a wrapper to ensure that proper run-
2019 -- time checking of the pre/postconditions. See body for details.
2021 -------------------
2022 -- Add_To_Result --
2023 -------------------
2025 procedure Add_To_Result (N : Node_Id) is
2026 begin
2027 if No (Result) then
2028 Result := New_List (N);
2029 else
2030 Append (N, Result);
2031 end if;
2032 end Add_To_Result;
2034 ----------------------------
2035 -- After_Last_Declaration --
2036 ----------------------------
2038 function After_Last_Declaration return Boolean is
2039 Spec : constant Node_Id := Parent (Current_Scope);
2041 begin
2042 if Nkind (Spec) = N_Package_Specification then
2043 if Present (Private_Declarations (Spec)) then
2044 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2045 elsif Present (Visible_Declarations (Spec)) then
2046 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2047 else
2048 return False;
2049 end if;
2051 else
2052 return False;
2053 end if;
2054 end After_Last_Declaration;
2056 ----------------------------
2057 -- Check_Current_Instance --
2058 ----------------------------
2060 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2062 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2063 -- Determine whether Typ is compatible with the rules for aliased
2064 -- views of types as defined in RM 3.10 in the various dialects.
2066 function Process (N : Node_Id) return Traverse_Result;
2067 -- Process routine to apply check to given node
2069 -----------------------------
2070 -- Is_Aliased_View_Of_Type --
2071 -----------------------------
2073 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2074 Typ_Decl : constant Node_Id := Parent (Typ);
2076 begin
2077 -- Common case
2079 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2080 and then Limited_Present (Type_Definition (Typ_Decl))
2081 then
2082 return True;
2084 -- The following paragraphs describe what a legal aliased view of
2085 -- a type is in the various dialects of Ada.
2087 -- Ada 95
2089 -- The current instance of a limited type, and a formal parameter
2090 -- or generic formal object of a tagged type.
2092 -- Ada 95 limited type
2093 -- * Type with reserved word "limited"
2094 -- * A protected or task type
2095 -- * A composite type with limited component
2097 elsif Ada_Version <= Ada_95 then
2098 return Is_Limited_Type (Typ);
2100 -- Ada 2005
2102 -- The current instance of a limited tagged type, a protected
2103 -- type, a task type, or a type that has the reserved word
2104 -- "limited" in its full definition ... a formal parameter or
2105 -- generic formal object of a tagged type.
2107 -- Ada 2005 limited type
2108 -- * Type with reserved word "limited", "synchronized", "task"
2109 -- or "protected"
2110 -- * A composite type with limited component
2111 -- * A derived type whose parent is a non-interface limited type
2113 elsif Ada_Version = Ada_2005 then
2114 return
2115 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2116 or else
2117 (Is_Derived_Type (Typ)
2118 and then not Is_Interface (Etype (Typ))
2119 and then Is_Limited_Type (Etype (Typ)));
2121 -- Ada 2012 and beyond
2123 -- The current instance of an immutably limited type ... a formal
2124 -- parameter or generic formal object of a tagged type.
2126 -- Ada 2012 limited type
2127 -- * Type with reserved word "limited", "synchronized", "task"
2128 -- or "protected"
2129 -- * A composite type with limited component
2130 -- * A derived type whose parent is a non-interface limited type
2131 -- * An incomplete view
2133 -- Ada 2012 immutably limited type
2134 -- * Explicitly limited record type
2135 -- * Record extension with "limited" present
2136 -- * Non-formal limited private type that is either tagged
2137 -- or has at least one access discriminant with a default
2138 -- expression
2139 -- * Task type, protected type or synchronized interface
2140 -- * Type derived from immutably limited type
2142 else
2143 return
2144 Is_Immutably_Limited_Type (Typ)
2145 or else Is_Incomplete_Type (Typ);
2146 end if;
2147 end Is_Aliased_View_Of_Type;
2149 -------------
2150 -- Process --
2151 -------------
2153 function Process (N : Node_Id) return Traverse_Result is
2154 begin
2155 case Nkind (N) is
2156 when N_Attribute_Reference =>
2157 if Nam_In (Attribute_Name (N), Name_Access,
2158 Name_Unchecked_Access)
2159 and then Is_Entity_Name (Prefix (N))
2160 and then Is_Type (Entity (Prefix (N)))
2161 and then Entity (Prefix (N)) = E
2162 then
2163 if Ada_Version < Ada_2012 then
2164 Error_Msg_N
2165 ("current instance must be a limited type",
2166 Prefix (N));
2167 else
2168 Error_Msg_N
2169 ("current instance must be an immutably limited "
2170 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2171 end if;
2173 return Abandon;
2175 else
2176 return OK;
2177 end if;
2179 when others => return OK;
2180 end case;
2181 end Process;
2183 procedure Traverse is new Traverse_Proc (Process);
2185 -- Local variables
2187 Rec_Type : constant Entity_Id :=
2188 Scope (Defining_Identifier (Comp_Decl));
2190 -- Start of processing for Check_Current_Instance
2192 begin
2193 if not Is_Aliased_View_Of_Type (Rec_Type) then
2194 Traverse (Comp_Decl);
2195 end if;
2196 end Check_Current_Instance;
2198 ------------------------------
2199 -- Check_Suspicious_Modulus --
2200 ------------------------------
2202 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
2203 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
2205 begin
2206 if not Warn_On_Suspicious_Modulus_Value then
2207 return;
2208 end if;
2210 if Nkind (Decl) = N_Full_Type_Declaration then
2211 declare
2212 Tdef : constant Node_Id := Type_Definition (Decl);
2214 begin
2215 if Nkind (Tdef) = N_Modular_Type_Definition then
2216 declare
2217 Modulus : constant Node_Id :=
2218 Original_Node (Expression (Tdef));
2220 begin
2221 if Nkind (Modulus) = N_Integer_Literal then
2222 declare
2223 Modv : constant Uint := Intval (Modulus);
2224 Sizv : constant Uint := RM_Size (Utype);
2226 begin
2227 -- First case, modulus and size are the same. This
2228 -- happens if you have something like mod 32, with
2229 -- an explicit size of 32, this is for sure a case
2230 -- where the warning is given, since it is seems
2231 -- very unlikely that someone would want e.g. a
2232 -- five bit type stored in 32 bits. It is much
2233 -- more likely they wanted a 32-bit type.
2235 if Modv = Sizv then
2236 null;
2238 -- Second case, the modulus is 32 or 64 and no
2239 -- size clause is present. This is a less clear
2240 -- case for giving the warning, but in the case
2241 -- of 32/64 (5-bit or 6-bit types) these seem rare
2242 -- enough that it is a likely error (and in any
2243 -- case using 2**5 or 2**6 in these cases seems
2244 -- clearer. We don't include 8 or 16 here, simply
2245 -- because in practice 3-bit and 4-bit types are
2246 -- more common and too many false positives if
2247 -- we warn in these cases.
2249 elsif not Has_Size_Clause (Utype)
2250 and then (Modv = Uint_32 or else Modv = Uint_64)
2251 then
2252 null;
2254 -- No warning needed
2256 else
2257 return;
2258 end if;
2260 -- If we fall through, give warning
2262 Error_Msg_Uint_1 := Modv;
2263 Error_Msg_N
2264 ("?M?2 '*'*^' may have been intended here",
2265 Modulus);
2266 end;
2267 end if;
2268 end;
2269 end if;
2270 end;
2271 end if;
2272 end Check_Suspicious_Modulus;
2274 -----------------------
2275 -- Freeze_Array_Type --
2276 -----------------------
2278 procedure Freeze_Array_Type (Arr : Entity_Id) is
2279 FS : constant Entity_Id := First_Subtype (Arr);
2280 Ctyp : constant Entity_Id := Component_Type (Arr);
2281 Clause : Entity_Id;
2283 Non_Standard_Enum : Boolean := False;
2284 -- Set true if any of the index types is an enumeration type with a
2285 -- non-standard representation.
2287 begin
2288 Freeze_And_Append (Ctyp, N, Result);
2290 Indx := First_Index (Arr);
2291 while Present (Indx) loop
2292 Freeze_And_Append (Etype (Indx), N, Result);
2294 if Is_Enumeration_Type (Etype (Indx))
2295 and then Has_Non_Standard_Rep (Etype (Indx))
2296 then
2297 Non_Standard_Enum := True;
2298 end if;
2300 Next_Index (Indx);
2301 end loop;
2303 -- Processing that is done only for base types
2305 if Ekind (Arr) = E_Array_Type then
2307 -- Deal with default setting of reverse storage order
2309 Set_SSO_From_Default (Arr);
2311 -- Propagate flags for component type
2313 if Is_Controlled_Active (Component_Type (Arr))
2314 or else Has_Controlled_Component (Ctyp)
2315 then
2316 Set_Has_Controlled_Component (Arr);
2317 end if;
2319 if Has_Unchecked_Union (Component_Type (Arr)) then
2320 Set_Has_Unchecked_Union (Arr);
2321 end if;
2323 -- Warn for pragma Pack overriding foreign convention
2325 if Has_Foreign_Convention (Ctyp)
2326 and then Has_Pragma_Pack (Arr)
2327 then
2328 declare
2329 CN : constant Name_Id :=
2330 Get_Convention_Name (Convention (Ctyp));
2331 PP : constant Node_Id :=
2332 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
2333 begin
2334 if Present (PP) then
2335 Error_Msg_Name_1 := CN;
2336 Error_Msg_Sloc := Sloc (Arr);
2337 Error_Msg_N
2338 ("pragma Pack affects convention % components #??", PP);
2339 Error_Msg_Name_1 := CN;
2340 Error_Msg_N
2341 ("\array components may not have % compatible "
2342 & "representation??", PP);
2343 end if;
2344 end;
2345 end if;
2347 -- If packing was requested or if the component size was
2348 -- set explicitly, then see if bit packing is required. This
2349 -- processing is only done for base types, since all of the
2350 -- representation aspects involved are type-related.
2352 -- This is not just an optimization, if we start processing the
2353 -- subtypes, they interfere with the settings on the base type
2354 -- (this is because Is_Packed has a slightly different meaning
2355 -- before and after freezing).
2357 declare
2358 Csiz : Uint;
2359 Esiz : Uint;
2361 begin
2362 if (Is_Packed (Arr) or else Has_Pragma_Pack (Arr))
2363 and then Known_Static_RM_Size (Ctyp)
2364 and then not Has_Component_Size_Clause (Arr)
2365 then
2366 Csiz := UI_Max (RM_Size (Ctyp), 1);
2368 elsif Known_Component_Size (Arr) then
2369 Csiz := Component_Size (Arr);
2371 elsif not Known_Static_Esize (Ctyp) then
2372 Csiz := Uint_0;
2374 else
2375 Esiz := Esize (Ctyp);
2377 -- We can set the component size if it is less than 16,
2378 -- rounding it up to the next storage unit size.
2380 if Esiz <= 8 then
2381 Csiz := Uint_8;
2382 elsif Esiz <= 16 then
2383 Csiz := Uint_16;
2384 else
2385 Csiz := Uint_0;
2386 end if;
2388 -- Set component size up to match alignment if it would
2389 -- otherwise be less than the alignment. This deals with
2390 -- cases of types whose alignment exceeds their size (the
2391 -- padded type cases).
2393 if Csiz /= 0 then
2394 declare
2395 A : constant Uint := Alignment_In_Bits (Ctyp);
2396 begin
2397 if Csiz < A then
2398 Csiz := A;
2399 end if;
2400 end;
2401 end if;
2402 end if;
2404 -- Case of component size that may result in packing
2406 if 1 <= Csiz and then Csiz <= 64 then
2407 declare
2408 Ent : constant Entity_Id :=
2409 First_Subtype (Arr);
2410 Pack_Pragma : constant Node_Id :=
2411 Get_Rep_Pragma (Ent, Name_Pack);
2412 Comp_Size_C : constant Node_Id :=
2413 Get_Attribute_Definition_Clause
2414 (Ent, Attribute_Component_Size);
2416 begin
2417 -- Warn if we have pack and component size so that the
2418 -- pack is ignored.
2420 -- Note: here we must check for the presence of a
2421 -- component size before checking for a Pack pragma to
2422 -- deal with the case where the array type is a derived
2423 -- type whose parent is currently private.
2425 if Present (Comp_Size_C)
2426 and then Has_Pragma_Pack (Ent)
2427 and then Warn_On_Redundant_Constructs
2428 then
2429 Error_Msg_Sloc := Sloc (Comp_Size_C);
2430 Error_Msg_NE
2431 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
2432 Error_Msg_N
2433 ("\?r?explicit component size given#!", Pack_Pragma);
2434 Set_Is_Packed (Base_Type (Ent), False);
2435 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
2436 end if;
2438 -- Set component size if not already set by a component
2439 -- size clause.
2441 if not Present (Comp_Size_C) then
2442 Set_Component_Size (Arr, Csiz);
2443 end if;
2445 -- Check for base type of 8, 16, 32 bits, where an
2446 -- unsigned subtype has a length one less than the
2447 -- base type (e.g. Natural subtype of Integer).
2449 -- In such cases, if a component size was not set
2450 -- explicitly, then generate a warning.
2452 if Has_Pragma_Pack (Arr)
2453 and then not Present (Comp_Size_C)
2454 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2455 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2456 then
2457 Error_Msg_Uint_1 := Csiz;
2459 if Present (Pack_Pragma) then
2460 Error_Msg_N
2461 ("??pragma Pack causes component size to be ^!",
2462 Pack_Pragma);
2463 Error_Msg_N
2464 ("\??use Component_Size to set desired value!",
2465 Pack_Pragma);
2466 end if;
2467 end if;
2469 -- Actual packing is not needed for 8, 16, 32, 64. Also
2470 -- not needed for 24 if alignment is 1.
2472 if Csiz = 8
2473 or else Csiz = 16
2474 or else Csiz = 32
2475 or else Csiz = 64
2476 or else (Csiz = 24 and then Alignment (Ctyp) = 1)
2477 then
2478 -- Here the array was requested to be packed, but
2479 -- the packing request had no effect, so Is_Packed
2480 -- is reset.
2482 -- Note: semantically this means that we lose track
2483 -- of the fact that a derived type inherited a pragma
2484 -- Pack that was non- effective, but that seems fine.
2486 -- We regard a Pack pragma as a request to set a
2487 -- representation characteristic, and this request
2488 -- may be ignored.
2490 Set_Is_Packed (Base_Type (Arr), False);
2491 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2493 if Known_Static_Esize (Component_Type (Arr))
2494 and then Esize (Component_Type (Arr)) = Csiz
2495 then
2496 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
2497 end if;
2499 -- In all other cases, packing is indeed needed
2501 else
2502 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2503 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
2504 Set_Is_Packed (Base_Type (Arr), True);
2505 end if;
2506 end;
2507 end if;
2508 end;
2510 -- Check for Aliased or Atomic_Components/Atomic/VFA with
2511 -- unsuitable packing or explicit component size clause given.
2513 if (Has_Aliased_Components (Arr)
2514 or else Has_Atomic_Components (Arr)
2515 or else Is_Atomic_Or_VFA (Ctyp))
2516 and then
2517 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2518 then
2519 Alias_Atomic_Check : declare
2521 procedure Complain_CS (T : String);
2522 -- Outputs error messages for incorrect CS clause or pragma
2523 -- Pack for aliased or atomic/VFA components (T is "aliased"
2524 -- or "atomic/vfa");
2526 -----------------
2527 -- Complain_CS --
2528 -----------------
2530 procedure Complain_CS (T : String) is
2531 begin
2532 if Has_Component_Size_Clause (Arr) then
2533 Clause :=
2534 Get_Attribute_Definition_Clause
2535 (FS, Attribute_Component_Size);
2537 Error_Msg_N
2538 ("incorrect component size for "
2539 & T & " components", Clause);
2540 Error_Msg_Uint_1 := Esize (Ctyp);
2541 Error_Msg_N
2542 ("\only allowed value is^", Clause);
2544 else
2545 Error_Msg_N
2546 ("cannot pack " & T & " components",
2547 Get_Rep_Pragma (FS, Name_Pack));
2548 end if;
2549 end Complain_CS;
2551 -- Start of processing for Alias_Atomic_Check
2553 begin
2554 -- If object size of component type isn't known, we cannot
2555 -- be sure so we defer to the back end.
2557 if not Known_Static_Esize (Ctyp) then
2558 null;
2560 -- Case where component size has no effect. First check for
2561 -- object size of component type multiple of the storage
2562 -- unit size.
2564 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2566 -- OK in both packing case and component size case if RM
2567 -- size is known and static and same as the object size.
2569 and then
2570 ((Known_Static_RM_Size (Ctyp)
2571 and then Esize (Ctyp) = RM_Size (Ctyp))
2573 -- Or if we have an explicit component size clause and
2574 -- the component size and object size are equal.
2576 or else
2577 (Has_Component_Size_Clause (Arr)
2578 and then Component_Size (Arr) = Esize (Ctyp)))
2579 then
2580 null;
2582 elsif Has_Aliased_Components (Arr) then
2583 Complain_CS ("aliased");
2585 elsif Has_Atomic_Components (Arr)
2586 or else Is_Atomic (Ctyp)
2587 then
2588 Complain_CS ("atomic");
2590 elsif Is_Volatile_Full_Access (Ctyp) then
2591 Complain_CS ("volatile full access");
2592 end if;
2593 end Alias_Atomic_Check;
2594 end if;
2596 -- Check for Independent_Components/Independent with unsuitable
2597 -- packing or explicit component size clause given.
2599 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
2600 and then
2601 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2602 then
2603 begin
2604 -- If object size of component type isn't known, we cannot
2605 -- be sure so we defer to the back end.
2607 if not Known_Static_Esize (Ctyp) then
2608 null;
2610 -- Case where component size has no effect. First check for
2611 -- object size of component type multiple of the storage
2612 -- unit size.
2614 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2616 -- OK in both packing case and component size case if RM
2617 -- size is known and multiple of the storage unit size.
2619 and then
2620 ((Known_Static_RM_Size (Ctyp)
2621 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
2623 -- Or if we have an explicit component size clause and
2624 -- the component size is larger than the object size.
2626 or else
2627 (Has_Component_Size_Clause (Arr)
2628 and then Component_Size (Arr) >= Esize (Ctyp)))
2629 then
2630 null;
2632 else
2633 if Has_Component_Size_Clause (Arr) then
2634 Clause :=
2635 Get_Attribute_Definition_Clause
2636 (FS, Attribute_Component_Size);
2638 Error_Msg_N
2639 ("incorrect component size for "
2640 & "independent components", Clause);
2641 Error_Msg_Uint_1 := Esize (Ctyp);
2642 Error_Msg_N
2643 ("\minimum allowed is^", Clause);
2645 else
2646 Error_Msg_N
2647 ("cannot pack independent components",
2648 Get_Rep_Pragma (FS, Name_Pack));
2649 end if;
2650 end if;
2651 end;
2652 end if;
2654 -- Warn for case of atomic type
2656 Clause := Get_Rep_Pragma (FS, Name_Atomic);
2658 if Present (Clause)
2659 and then not Addressable (Component_Size (FS))
2660 then
2661 Error_Msg_NE
2662 ("non-atomic components of type& may not be "
2663 & "accessible by separate tasks??", Clause, Arr);
2665 if Has_Component_Size_Clause (Arr) then
2666 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2667 (FS, Attribute_Component_Size));
2668 Error_Msg_N ("\because of component size clause#??", Clause);
2670 elsif Has_Pragma_Pack (Arr) then
2671 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2672 Error_Msg_N ("\because of pragma Pack#??", Clause);
2673 end if;
2674 end if;
2676 -- Check for scalar storage order
2678 declare
2679 Dummy : Boolean;
2680 begin
2681 Check_Component_Storage_Order
2682 (Encl_Type => Arr,
2683 Comp => Empty,
2684 ADC => Get_Attribute_Definition_Clause
2685 (First_Subtype (Arr),
2686 Attribute_Scalar_Storage_Order),
2687 Comp_ADC_Present => Dummy);
2688 end;
2690 -- Processing that is done only for subtypes
2692 else
2693 -- Acquire alignment from base type
2695 if Unknown_Alignment (Arr) then
2696 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
2697 Adjust_Esize_Alignment (Arr);
2698 end if;
2699 end if;
2701 -- Specific checks for bit-packed arrays
2703 if Is_Bit_Packed_Array (Arr) then
2705 -- Check number of elements for bit packed arrays that come from
2706 -- source and have compile time known ranges. The bit-packed
2707 -- arrays circuitry does not support arrays with more than
2708 -- Integer'Last + 1 elements, and when this restriction is
2709 -- violated, causes incorrect data access.
2711 -- For the case where this is not compile time known, a run-time
2712 -- check should be generated???
2714 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
2715 declare
2716 Elmts : Uint;
2717 Index : Node_Id;
2718 Ilen : Node_Id;
2719 Ityp : Entity_Id;
2721 begin
2722 Elmts := Uint_1;
2723 Index := First_Index (Arr);
2724 while Present (Index) loop
2725 Ityp := Etype (Index);
2727 -- Never generate an error if any index is of a generic
2728 -- type. We will check this in instances.
2730 if Is_Generic_Type (Ityp) then
2731 Elmts := Uint_0;
2732 exit;
2733 end if;
2735 Ilen :=
2736 Make_Attribute_Reference (Loc,
2737 Prefix => New_Occurrence_Of (Ityp, Loc),
2738 Attribute_Name => Name_Range_Length);
2739 Analyze_And_Resolve (Ilen);
2741 -- No attempt is made to check number of elements if not
2742 -- compile time known.
2744 if Nkind (Ilen) /= N_Integer_Literal then
2745 Elmts := Uint_0;
2746 exit;
2747 end if;
2749 Elmts := Elmts * Intval (Ilen);
2750 Next_Index (Index);
2751 end loop;
2753 if Elmts > Intval (High_Bound
2754 (Scalar_Range (Standard_Integer))) + 1
2755 then
2756 Error_Msg_N
2757 ("bit packed array type may not have "
2758 & "more than Integer''Last+1 elements", Arr);
2759 end if;
2760 end;
2761 end if;
2763 -- Check size
2765 if Known_RM_Size (Arr) then
2766 declare
2767 SizC : constant Node_Id := Size_Clause (Arr);
2768 Discard : Boolean;
2770 begin
2771 -- It is not clear if it is possible to have no size clause
2772 -- at this stage, but it is not worth worrying about. Post
2773 -- error on the entity name in the size clause if present,
2774 -- else on the type entity itself.
2776 if Present (SizC) then
2777 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
2778 else
2779 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
2780 end if;
2781 end;
2782 end if;
2783 end if;
2785 -- If any of the index types was an enumeration type with a non-
2786 -- standard rep clause, then we indicate that the array type is
2787 -- always packed (even if it is not bit packed).
2789 if Non_Standard_Enum then
2790 Set_Has_Non_Standard_Rep (Base_Type (Arr));
2791 Set_Is_Packed (Base_Type (Arr));
2792 end if;
2794 Set_Component_Alignment_If_Not_Set (Arr);
2796 -- If the array is packed, we must create the packed array type to be
2797 -- used to actually implement the type. This is only needed for real
2798 -- array types (not for string literal types, since they are present
2799 -- only for the front end).
2801 if Is_Packed (Arr)
2802 and then Ekind (Arr) /= E_String_Literal_Subtype
2803 then
2804 Create_Packed_Array_Impl_Type (Arr);
2805 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
2807 -- Make sure that we have the necessary routines to implement the
2808 -- packing, and complain now if not. Note that we only test this
2809 -- for constrained array types.
2811 if Is_Constrained (Arr)
2812 and then Is_Bit_Packed_Array (Arr)
2813 and then Present (Packed_Array_Impl_Type (Arr))
2814 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
2815 then
2816 declare
2817 CS : constant Uint := Component_Size (Arr);
2818 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
2820 begin
2821 if RE /= RE_Null
2822 and then not RTE_Available (RE)
2823 then
2824 Error_Msg_CRT
2825 ("packing of " & UI_Image (CS) & "-bit components",
2826 First_Subtype (Etype (Arr)));
2828 -- Cancel the packing
2830 Set_Is_Packed (Base_Type (Arr), False);
2831 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2832 Set_Packed_Array_Impl_Type (Arr, Empty);
2833 goto Skip_Packed;
2834 end if;
2835 end;
2836 end if;
2838 -- Size information of packed array type is copied to the array
2839 -- type, since this is really the representation. But do not
2840 -- override explicit existing size values. If the ancestor subtype
2841 -- is constrained the Packed_Array_Impl_Type will be inherited
2842 -- from it, but the size may have been provided already, and
2843 -- must not be overridden either.
2845 if not Has_Size_Clause (Arr)
2846 and then
2847 (No (Ancestor_Subtype (Arr))
2848 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
2849 then
2850 Set_Esize (Arr, Esize (Packed_Array_Impl_Type (Arr)));
2851 Set_RM_Size (Arr, RM_Size (Packed_Array_Impl_Type (Arr)));
2852 end if;
2854 if not Has_Alignment_Clause (Arr) then
2855 Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
2856 end if;
2857 end if;
2859 <<Skip_Packed>>
2861 -- For non-packed arrays set the alignment of the array to the
2862 -- alignment of the component type if it is unknown. Skip this
2863 -- in atomic/VFA case (atomic/VFA arrays may need larger alignments).
2865 if not Is_Packed (Arr)
2866 and then Unknown_Alignment (Arr)
2867 and then Known_Alignment (Ctyp)
2868 and then Known_Static_Component_Size (Arr)
2869 and then Known_Static_Esize (Ctyp)
2870 and then Esize (Ctyp) = Component_Size (Arr)
2871 and then not Is_Atomic_Or_VFA (Arr)
2872 then
2873 Set_Alignment (Arr, Alignment (Component_Type (Arr)));
2874 end if;
2876 -- A Ghost type cannot have a component of protected or task type
2877 -- (SPARK RM 6.9(19)).
2879 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
2880 Error_Msg_N
2881 ("ghost array type & cannot have concurrent component type",
2882 Arr);
2883 end if;
2884 end Freeze_Array_Type;
2886 -------------------------------
2887 -- Freeze_Object_Declaration --
2888 -------------------------------
2890 procedure Freeze_Object_Declaration (E : Entity_Id) is
2891 begin
2892 -- Abstract type allowed only for C++ imported variables or constants
2894 -- Note: we inhibit this check for objects that do not come from
2895 -- source because there is at least one case (the expansion of
2896 -- x'Class'Input where x is abstract) where we legitimately
2897 -- generate an abstract object.
2899 if Is_Abstract_Type (Etype (E))
2900 and then Comes_From_Source (Parent (E))
2901 and then not (Is_Imported (E) and then Is_CPP_Class (Etype (E)))
2902 then
2903 Error_Msg_N ("type of object cannot be abstract",
2904 Object_Definition (Parent (E)));
2906 if Is_CPP_Class (Etype (E)) then
2907 Error_Msg_NE
2908 ("\} may need a cpp_constructor",
2909 Object_Definition (Parent (E)), Etype (E));
2911 elsif Present (Expression (Parent (E))) then
2912 Error_Msg_N -- CODEFIX
2913 ("\maybe a class-wide type was meant",
2914 Object_Definition (Parent (E)));
2915 end if;
2916 end if;
2918 -- For object created by object declaration, perform required
2919 -- categorization (preelaborate and pure) checks. Defer these
2920 -- checks to freeze time since pragma Import inhibits default
2921 -- initialization and thus pragma Import affects these checks.
2923 Validate_Object_Declaration (Declaration_Node (E));
2925 -- If there is an address clause, check that it is valid
2926 -- and if need be move initialization to the freeze node.
2928 Check_Address_Clause (E);
2930 -- Similar processing is needed for aspects that may affect
2931 -- object layout, like Alignment, if there is an initialization
2932 -- expression.
2934 if Has_Delayed_Aspects (E)
2935 and then Expander_Active
2936 and then Is_Array_Type (Etype (E))
2937 and then Present (Expression (Parent (E)))
2938 then
2939 declare
2940 Decl : constant Node_Id := Parent (E);
2941 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
2943 begin
2945 -- Capture initialization value at point of declaration, and
2946 -- make explicit assignment legal, because object may be a
2947 -- constant.
2949 Remove_Side_Effects (Expression (Decl));
2950 Set_Assignment_OK (Lhs);
2952 -- Move initialization to freeze actions.
2954 Append_Freeze_Action (E,
2955 Make_Assignment_Statement (Loc,
2956 Name => Lhs,
2957 Expression => Expression (Decl)));
2959 Set_No_Initialization (Decl);
2960 -- Set_Is_Frozen (E, False);
2961 end;
2962 end if;
2964 -- Reset Is_True_Constant for non-constant aliased object. We
2965 -- consider that the fact that a non-constant object is aliased may
2966 -- indicate that some funny business is going on, e.g. an aliased
2967 -- object is passed by reference to a procedure which captures the
2968 -- address of the object, which is later used to assign a new value,
2969 -- even though the compiler thinks that it is not modified. Such
2970 -- code is highly dubious, but we choose to make it "work" for
2971 -- non-constant aliased objects.
2973 -- Note that we used to do this for all aliased objects, whether or
2974 -- not constant, but this caused anomalies down the line because we
2975 -- ended up with static objects that were not Is_True_Constant. Not
2976 -- resetting Is_True_Constant for (aliased) constant objects ensures
2977 -- that this anomaly never occurs.
2979 -- However, we don't do that for internal entities. We figure that if
2980 -- we deliberately set Is_True_Constant for an internal entity, e.g.
2981 -- a dispatch table entry, then we mean it.
2983 if Ekind (E) /= E_Constant
2984 and then (Is_Aliased (E) or else Is_Aliased (Etype (E)))
2985 and then not Is_Internal_Name (Chars (E))
2986 then
2987 Set_Is_True_Constant (E, False);
2988 end if;
2990 -- If the object needs any kind of default initialization, an error
2991 -- must be issued if No_Default_Initialization applies. The check
2992 -- doesn't apply to imported objects, which are not ever default
2993 -- initialized, and is why the check is deferred until freezing, at
2994 -- which point we know if Import applies. Deferred constants are also
2995 -- exempted from this test because their completion is explicit, or
2996 -- through an import pragma.
2998 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
2999 null;
3001 elsif Comes_From_Source (E)
3002 and then not Is_Imported (E)
3003 and then not Has_Init_Expression (Declaration_Node (E))
3004 and then
3005 ((Has_Non_Null_Base_Init_Proc (Etype (E))
3006 and then not No_Initialization (Declaration_Node (E))
3007 and then not Initialization_Suppressed (Etype (E)))
3008 or else
3009 (Needs_Simple_Initialization (Etype (E))
3010 and then not Is_Internal (E)))
3011 then
3012 Has_Default_Initialization := True;
3013 Check_Restriction
3014 (No_Default_Initialization, Declaration_Node (E));
3015 end if;
3017 -- Check that a Thread_Local_Storage variable does not have
3018 -- default initialization, and any explicit initialization must
3019 -- either be the null constant or a static constant.
3021 if Has_Pragma_Thread_Local_Storage (E) then
3022 declare
3023 Decl : constant Node_Id := Declaration_Node (E);
3024 begin
3025 if Has_Default_Initialization
3026 or else
3027 (Has_Init_Expression (Decl)
3028 and then
3029 (No (Expression (Decl))
3030 or else not
3031 (Is_OK_Static_Expression (Expression (Decl))
3032 or else Nkind (Expression (Decl)) = N_Null)))
3033 then
3034 Error_Msg_NE
3035 ("Thread_Local_Storage variable& is "
3036 & "improperly initialized", Decl, E);
3037 Error_Msg_NE
3038 ("\only allowed initialization is explicit "
3039 & "NULL or static expression", Decl, E);
3040 end if;
3041 end;
3042 end if;
3044 -- For imported objects, set Is_Public unless there is also an
3045 -- address clause, which means that there is no external symbol
3046 -- needed for the Import (Is_Public may still be set for other
3047 -- unrelated reasons). Note that we delayed this processing
3048 -- till freeze time so that we can be sure not to set the flag
3049 -- if there is an address clause. If there is such a clause,
3050 -- then the only purpose of the Import pragma is to suppress
3051 -- implicit initialization.
3053 if Is_Imported (E) and then No (Address_Clause (E)) then
3054 Set_Is_Public (E);
3055 end if;
3057 -- For source objects that are not Imported and are library
3058 -- level, if no linker section pragma was given inherit the
3059 -- appropriate linker section from the corresponding type.
3061 if Comes_From_Source (E)
3062 and then not Is_Imported (E)
3063 and then Is_Library_Level_Entity (E)
3064 and then No (Linker_Section_Pragma (E))
3065 then
3066 Set_Linker_Section_Pragma
3067 (E, Linker_Section_Pragma (Etype (E)));
3068 end if;
3070 -- For convention C objects of an enumeration type, warn if the
3071 -- size is not integer size and no explicit size given. Skip
3072 -- warning for Boolean, and Character, assume programmer expects
3073 -- 8-bit sizes for these cases.
3075 if (Convention (E) = Convention_C
3076 or else
3077 Convention (E) = Convention_CPP)
3078 and then Is_Enumeration_Type (Etype (E))
3079 and then not Is_Character_Type (Etype (E))
3080 and then not Is_Boolean_Type (Etype (E))
3081 and then Esize (Etype (E)) < Standard_Integer_Size
3082 and then not Has_Size_Clause (E)
3083 then
3084 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
3085 Error_Msg_N
3086 ("??convention C enumeration object has size less than ^", E);
3087 Error_Msg_N ("\??use explicit size clause to set size", E);
3088 end if;
3089 end Freeze_Object_Declaration;
3091 -----------------------------
3092 -- Freeze_Generic_Entities --
3093 -----------------------------
3095 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
3096 E : Entity_Id;
3097 F : Node_Id;
3098 Flist : List_Id;
3100 begin
3101 Flist := New_List;
3102 E := First_Entity (Pack);
3103 while Present (E) loop
3104 if Is_Type (E) and then not Is_Generic_Type (E) then
3105 F := Make_Freeze_Generic_Entity (Sloc (Pack));
3106 Set_Entity (F, E);
3107 Append_To (Flist, F);
3109 elsif Ekind (E) = E_Generic_Package then
3110 Append_List_To (Flist, Freeze_Generic_Entities (E));
3111 end if;
3113 Next_Entity (E);
3114 end loop;
3116 return Flist;
3117 end Freeze_Generic_Entities;
3119 --------------------
3120 -- Freeze_Profile --
3121 --------------------
3123 function Freeze_Profile (E : Entity_Id) return Boolean is
3124 F_Type : Entity_Id;
3125 R_Type : Entity_Id;
3126 Warn_Node : Node_Id;
3128 begin
3129 -- Loop through formals
3131 Formal := First_Formal (E);
3132 while Present (Formal) loop
3133 F_Type := Etype (Formal);
3135 -- AI05-0151: incomplete types can appear in a profile. By the
3136 -- time the entity is frozen, the full view must be available,
3137 -- unless it is a limited view.
3139 if Is_Incomplete_Type (F_Type)
3140 and then Present (Full_View (F_Type))
3141 and then not From_Limited_With (F_Type)
3142 then
3143 F_Type := Full_View (F_Type);
3144 Set_Etype (Formal, F_Type);
3145 end if;
3147 if not From_Limited_With (F_Type) then
3148 Freeze_And_Append (F_Type, N, Result);
3149 end if;
3151 if Is_Private_Type (F_Type)
3152 and then Is_Private_Type (Base_Type (F_Type))
3153 and then No (Full_View (Base_Type (F_Type)))
3154 and then not Is_Generic_Type (F_Type)
3155 and then not Is_Derived_Type (F_Type)
3156 then
3157 -- If the type of a formal is incomplete, subprogram is being
3158 -- frozen prematurely. Within an instance (but not within a
3159 -- wrapper package) this is an artifact of our need to regard
3160 -- the end of an instantiation as a freeze point. Otherwise it
3161 -- is a definite error.
3163 if In_Instance then
3164 Set_Is_Frozen (E, False);
3165 Result := No_List;
3166 return False;
3168 elsif not After_Last_Declaration
3169 and then not Freezing_Library_Level_Tagged_Type
3170 then
3171 Error_Msg_Node_1 := F_Type;
3172 Error_Msg
3173 ("type & must be fully defined before this point", Loc);
3174 end if;
3175 end if;
3177 -- Check suspicious parameter for C function. These tests apply
3178 -- only to exported/imported subprograms.
3180 if Warn_On_Export_Import
3181 and then Comes_From_Source (E)
3182 and then (Convention (E) = Convention_C
3183 or else
3184 Convention (E) = Convention_CPP)
3185 and then (Is_Imported (E) or else Is_Exported (E))
3186 and then Convention (E) /= Convention (Formal)
3187 and then not Has_Warnings_Off (E)
3188 and then not Has_Warnings_Off (F_Type)
3189 and then not Has_Warnings_Off (Formal)
3190 then
3191 -- Qualify mention of formals with subprogram name
3193 Error_Msg_Qual_Level := 1;
3195 -- Check suspicious use of fat C pointer
3197 if Is_Access_Type (F_Type)
3198 and then Esize (F_Type) > Ttypes.System_Address_Size
3199 then
3200 Error_Msg_N
3201 ("?x?type of & does not correspond to C pointer!", Formal);
3203 -- Check suspicious return of boolean
3205 elsif Root_Type (F_Type) = Standard_Boolean
3206 and then Convention (F_Type) = Convention_Ada
3207 and then not Has_Warnings_Off (F_Type)
3208 and then not Has_Size_Clause (F_Type)
3209 then
3210 Error_Msg_N
3211 ("& is an 8-bit Ada Boolean?x?", Formal);
3212 Error_Msg_N
3213 ("\use appropriate corresponding type in C "
3214 & "(e.g. char)?x?", Formal);
3216 -- Check suspicious tagged type
3218 elsif (Is_Tagged_Type (F_Type)
3219 or else
3220 (Is_Access_Type (F_Type)
3221 and then Is_Tagged_Type (Designated_Type (F_Type))))
3222 and then Convention (E) = Convention_C
3223 then
3224 Error_Msg_N
3225 ("?x?& involves a tagged type which does not "
3226 & "correspond to any C type!", Formal);
3228 -- Check wrong convention subprogram pointer
3230 elsif Ekind (F_Type) = E_Access_Subprogram_Type
3231 and then not Has_Foreign_Convention (F_Type)
3232 then
3233 Error_Msg_N
3234 ("?x?subprogram pointer & should "
3235 & "have foreign convention!", Formal);
3236 Error_Msg_Sloc := Sloc (F_Type);
3237 Error_Msg_NE
3238 ("\?x?add Convention pragma to declaration of &#",
3239 Formal, F_Type);
3240 end if;
3242 -- Turn off name qualification after message output
3244 Error_Msg_Qual_Level := 0;
3245 end if;
3247 -- Check for unconstrained array in exported foreign convention
3248 -- case.
3250 if Has_Foreign_Convention (E)
3251 and then not Is_Imported (E)
3252 and then Is_Array_Type (F_Type)
3253 and then not Is_Constrained (F_Type)
3254 and then Warn_On_Export_Import
3255 then
3256 Error_Msg_Qual_Level := 1;
3258 -- If this is an inherited operation, place the warning on
3259 -- the derived type declaration, rather than on the original
3260 -- subprogram.
3262 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
3263 then
3264 Warn_Node := Parent (E);
3266 if Formal = First_Formal (E) then
3267 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
3268 end if;
3269 else
3270 Warn_Node := Formal;
3271 end if;
3273 Error_Msg_NE ("?x?type of argument& is unconstrained array",
3274 Warn_Node, Formal);
3275 Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
3276 Warn_Node, Formal);
3277 Error_Msg_Qual_Level := 0;
3278 end if;
3280 if not From_Limited_With (F_Type) then
3281 if Is_Access_Type (F_Type) then
3282 F_Type := Designated_Type (F_Type);
3283 end if;
3285 -- If the formal is an anonymous_access_to_subprogram
3286 -- freeze the subprogram type as well, to prevent
3287 -- scope anomalies in gigi, because there is no other
3288 -- clear point at which it could be frozen.
3290 if Is_Itype (Etype (Formal))
3291 and then Ekind (F_Type) = E_Subprogram_Type
3292 then
3293 Freeze_And_Append (F_Type, N, Result);
3294 end if;
3295 end if;
3297 Next_Formal (Formal);
3298 end loop;
3300 -- Case of function: similar checks on return type
3302 if Ekind (E) = E_Function then
3304 -- Check whether function is declared elsewhere. Previous code
3305 -- used Get_Source_Unit on both arguments, but the values are
3306 -- equal in the case of a parent and a child unit.
3307 -- Confusion with subunits in code ????
3309 Late_Freezing :=
3310 not In_Same_Extended_Unit (E, N)
3311 and then Returns_Limited_View (E);
3313 -- Freeze return type
3315 R_Type := Etype (E);
3317 -- AI05-0151: the return type may have been incomplete
3318 -- at the point of declaration. Replace it with the full
3319 -- view, unless the current type is a limited view. In
3320 -- that case the full view is in a different unit, and
3321 -- gigi finds the non-limited view after the other unit
3322 -- is elaborated.
3324 if Ekind (R_Type) = E_Incomplete_Type
3325 and then Present (Full_View (R_Type))
3326 and then not From_Limited_With (R_Type)
3327 then
3328 R_Type := Full_View (R_Type);
3329 Set_Etype (E, R_Type);
3331 -- If the return type is a limited view and the non-limited
3332 -- view is still incomplete, the function has to be frozen at a
3333 -- later time. If the function is abstract there is no place at
3334 -- which the full view will become available, and no code to be
3335 -- generated for it, so mark type as frozen.
3337 elsif Ekind (R_Type) = E_Incomplete_Type
3338 and then From_Limited_With (R_Type)
3339 and then Ekind (Non_Limited_View (R_Type)) = E_Incomplete_Type
3340 then
3341 if Is_Abstract_Subprogram (E) then
3342 null;
3343 else
3344 Set_Is_Frozen (E, False);
3345 Set_Returns_Limited_View (E);
3346 return False;
3347 end if;
3348 end if;
3350 Freeze_And_Append (R_Type, N, Result);
3352 -- Check suspicious return type for C function
3354 if Warn_On_Export_Import
3355 and then (Convention (E) = Convention_C
3356 or else
3357 Convention (E) = Convention_CPP)
3358 and then (Is_Imported (E) or else Is_Exported (E))
3359 then
3360 -- Check suspicious return of fat C pointer
3362 if Is_Access_Type (R_Type)
3363 and then Esize (R_Type) > Ttypes.System_Address_Size
3364 and then not Has_Warnings_Off (E)
3365 and then not Has_Warnings_Off (R_Type)
3366 then
3367 Error_Msg_N ("?x?return type of& does not "
3368 & "correspond to C pointer!", E);
3370 -- Check suspicious return of boolean
3372 elsif Root_Type (R_Type) = Standard_Boolean
3373 and then Convention (R_Type) = Convention_Ada
3374 and then not Has_Warnings_Off (E)
3375 and then not Has_Warnings_Off (R_Type)
3376 and then not Has_Size_Clause (R_Type)
3377 then
3378 declare
3379 N : constant Node_Id :=
3380 Result_Definition (Declaration_Node (E));
3381 begin
3382 Error_Msg_NE
3383 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
3384 Error_Msg_NE
3385 ("\use appropriate corresponding type in C "
3386 & "(e.g. char)?x?", N, E);
3387 end;
3389 -- Check suspicious return tagged type
3391 elsif (Is_Tagged_Type (R_Type)
3392 or else (Is_Access_Type (R_Type)
3393 and then
3394 Is_Tagged_Type
3395 (Designated_Type (R_Type))))
3396 and then Convention (E) = Convention_C
3397 and then not Has_Warnings_Off (E)
3398 and then not Has_Warnings_Off (R_Type)
3399 then
3400 Error_Msg_N ("?x?return type of & does not "
3401 & "correspond to C type!", E);
3403 -- Check return of wrong convention subprogram pointer
3405 elsif Ekind (R_Type) = E_Access_Subprogram_Type
3406 and then not Has_Foreign_Convention (R_Type)
3407 and then not Has_Warnings_Off (E)
3408 and then not Has_Warnings_Off (R_Type)
3409 then
3410 Error_Msg_N ("?x?& should return a foreign "
3411 & "convention subprogram pointer", E);
3412 Error_Msg_Sloc := Sloc (R_Type);
3413 Error_Msg_NE
3414 ("\?x?add Convention pragma to declaration of& #",
3415 E, R_Type);
3416 end if;
3417 end if;
3419 -- Give warning for suspicious return of a result of an
3420 -- unconstrained array type in a foreign convention function.
3422 if Has_Foreign_Convention (E)
3424 -- We are looking for a return of unconstrained array
3426 and then Is_Array_Type (R_Type)
3427 and then not Is_Constrained (R_Type)
3429 -- Exclude imported routines, the warning does not belong on
3430 -- the import, but rather on the routine definition.
3432 and then not Is_Imported (E)
3434 -- Check that general warning is enabled, and that it is not
3435 -- suppressed for this particular case.
3437 and then Warn_On_Export_Import
3438 and then not Has_Warnings_Off (E)
3439 and then not Has_Warnings_Off (R_Type)
3440 then
3441 Error_Msg_N
3442 ("?x?foreign convention function& should not return "
3443 & "unconstrained array!", E);
3444 end if;
3445 end if;
3447 -- Check suspicious use of Import in pure unit (cases where the RM
3448 -- allows calls to be omitted).
3450 if Is_Imported (E)
3452 -- It might be suspicious if the compilation unit has the Pure
3453 -- aspect/pragma.
3455 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
3457 -- The RM allows omission of calls only in the case of
3458 -- library-level subprograms (see RM-10.2.1(18)).
3460 and then Is_Library_Level_Entity (E)
3462 -- Ignore internally generated entity. This happens in some cases
3463 -- of subprograms in specs, where we generate an implied body.
3465 and then Comes_From_Source (Import_Pragma (E))
3467 -- Assume run-time knows what it is doing
3469 and then not GNAT_Mode
3471 -- Assume explicit Pure_Function means import is pure
3473 and then not Has_Pragma_Pure_Function (E)
3475 -- Don't need warning in relaxed semantics mode
3477 and then not Relaxed_RM_Semantics
3479 -- Assume convention Intrinsic is OK, since this is specialized.
3480 -- This deals with the DEC unit current_exception.ads
3482 and then Convention (E) /= Convention_Intrinsic
3484 -- Assume that ASM interface knows what it is doing. This deals
3485 -- with unsigned.ads in the AAMP back end.
3487 and then Convention (E) /= Convention_Assembler
3488 then
3489 Error_Msg_N
3490 ("pragma Import in Pure unit??", Import_Pragma (E));
3491 Error_Msg_NE
3492 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
3493 Import_Pragma (E), E);
3494 end if;
3496 return True;
3497 end Freeze_Profile;
3499 ------------------------
3500 -- Freeze_Record_Type --
3501 ------------------------
3503 procedure Freeze_Record_Type (Rec : Entity_Id) is
3504 ADC : Node_Id;
3505 Comp : Entity_Id;
3506 IR : Node_Id;
3507 Prev : Entity_Id;
3509 Junk : Boolean;
3510 pragma Warnings (Off, Junk);
3512 Rec_Pushed : Boolean := False;
3513 -- Set True if the record type scope Rec has been pushed on the scope
3514 -- stack. Needed for the analysis of delayed aspects specified to the
3515 -- components of Rec.
3517 SSO_ADC : Node_Id;
3518 -- Scalar_Storage_Order attribute definition clause for the record
3520 Unplaced_Component : Boolean := False;
3521 -- Set True if we find at least one component with no component
3522 -- clause (used to warn about useless Pack pragmas).
3524 Placed_Component : Boolean := False;
3525 -- Set True if we find at least one component with a component
3526 -- clause (used to warn about useless Bit_Order pragmas, and also
3527 -- to detect cases where Implicit_Packing may have an effect).
3529 Aliased_Component : Boolean := False;
3530 -- Set True if we find at least one component which is aliased. This
3531 -- is used to prevent Implicit_Packing of the record, since packing
3532 -- cannot modify the size of alignment of an aliased component.
3534 SSO_ADC_Component : Boolean := False;
3535 -- Set True if we find at least one component whose type has a
3536 -- Scalar_Storage_Order attribute definition clause.
3538 All_Scalar_Components : Boolean := True;
3539 -- Set False if we encounter a component of a non-scalar type
3541 Scalar_Component_Total_RM_Size : Uint := Uint_0;
3542 Scalar_Component_Total_Esize : Uint := Uint_0;
3543 -- Accumulates total RM_Size values and total Esize values of all
3544 -- scalar components. Used for processing of Implicit_Packing.
3546 function Check_Allocator (N : Node_Id) return Node_Id;
3547 -- If N is an allocator, possibly wrapped in one or more level of
3548 -- qualified expression(s), return the inner allocator node, else
3549 -- return Empty.
3551 procedure Check_Itype (Typ : Entity_Id);
3552 -- If the component subtype is an access to a constrained subtype of
3553 -- an already frozen type, make the subtype frozen as well. It might
3554 -- otherwise be frozen in the wrong scope, and a freeze node on
3555 -- subtype has no effect. Similarly, if the component subtype is a
3556 -- regular (not protected) access to subprogram, set the anonymous
3557 -- subprogram type to frozen as well, to prevent an out-of-scope
3558 -- freeze node at some eventual point of call. Protected operations
3559 -- are handled elsewhere.
3561 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3562 -- Make sure that all types mentioned in Discrete_Choices of the
3563 -- variants referenceed by the Variant_Part VP are frozen. This is
3564 -- a recursive routine to deal with nested variants.
3566 ---------------------
3567 -- Check_Allocator --
3568 ---------------------
3570 function Check_Allocator (N : Node_Id) return Node_Id is
3571 Inner : Node_Id;
3572 begin
3573 Inner := N;
3574 loop
3575 if Nkind (Inner) = N_Allocator then
3576 return Inner;
3577 elsif Nkind (Inner) = N_Qualified_Expression then
3578 Inner := Expression (Inner);
3579 else
3580 return Empty;
3581 end if;
3582 end loop;
3583 end Check_Allocator;
3585 -----------------
3586 -- Check_Itype --
3587 -----------------
3589 procedure Check_Itype (Typ : Entity_Id) is
3590 Desig : constant Entity_Id := Designated_Type (Typ);
3592 begin
3593 if not Is_Frozen (Desig)
3594 and then Is_Frozen (Base_Type (Desig))
3595 then
3596 Set_Is_Frozen (Desig);
3598 -- In addition, add an Itype_Reference to ensure that the
3599 -- access subtype is elaborated early enough. This cannot be
3600 -- done if the subtype may depend on discriminants.
3602 if Ekind (Comp) = E_Component
3603 and then Is_Itype (Etype (Comp))
3604 and then not Has_Discriminants (Rec)
3605 then
3606 IR := Make_Itype_Reference (Sloc (Comp));
3607 Set_Itype (IR, Desig);
3608 Add_To_Result (IR);
3609 end if;
3611 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
3612 and then Convention (Desig) /= Convention_Protected
3613 then
3614 Set_Is_Frozen (Desig);
3615 end if;
3616 end Check_Itype;
3618 ------------------------------------
3619 -- Freeze_Choices_In_Variant_Part --
3620 ------------------------------------
3622 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
3623 pragma Assert (Nkind (VP) = N_Variant_Part);
3625 Variant : Node_Id;
3626 Choice : Node_Id;
3627 CL : Node_Id;
3629 begin
3630 -- Loop through variants
3632 Variant := First_Non_Pragma (Variants (VP));
3633 while Present (Variant) loop
3635 -- Loop through choices, checking that all types are frozen
3637 Choice := First_Non_Pragma (Discrete_Choices (Variant));
3638 while Present (Choice) loop
3639 if Nkind (Choice) in N_Has_Etype
3640 and then Present (Etype (Choice))
3641 then
3642 Freeze_And_Append (Etype (Choice), N, Result);
3643 end if;
3645 Next_Non_Pragma (Choice);
3646 end loop;
3648 -- Check for nested variant part to process
3650 CL := Component_List (Variant);
3652 if not Null_Present (CL) then
3653 if Present (Variant_Part (CL)) then
3654 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
3655 end if;
3656 end if;
3658 Next_Non_Pragma (Variant);
3659 end loop;
3660 end Freeze_Choices_In_Variant_Part;
3662 -- Start of processing for Freeze_Record_Type
3664 begin
3665 -- Deal with delayed aspect specifications for components. The
3666 -- analysis of the aspect is required to be delayed to the freeze
3667 -- point, thus we analyze the pragma or attribute definition
3668 -- clause in the tree at this point. We also analyze the aspect
3669 -- specification node at the freeze point when the aspect doesn't
3670 -- correspond to pragma/attribute definition clause.
3672 Comp := First_Entity (Rec);
3673 while Present (Comp) loop
3674 if Ekind (Comp) = E_Component
3675 and then Has_Delayed_Aspects (Comp)
3676 then
3677 if not Rec_Pushed then
3678 Push_Scope (Rec);
3679 Rec_Pushed := True;
3681 -- The visibility to the discriminants must be restored in
3682 -- order to properly analyze the aspects.
3684 if Has_Discriminants (Rec) then
3685 Install_Discriminants (Rec);
3686 end if;
3687 end if;
3689 Analyze_Aspects_At_Freeze_Point (Comp);
3690 end if;
3692 Next_Entity (Comp);
3693 end loop;
3695 -- Pop the scope if Rec scope has been pushed on the scope stack
3696 -- during the delayed aspect analysis process.
3698 if Rec_Pushed then
3699 if Has_Discriminants (Rec) then
3700 Uninstall_Discriminants (Rec);
3701 end if;
3703 Pop_Scope;
3704 end if;
3706 -- Freeze components and embedded subtypes
3708 Comp := First_Entity (Rec);
3709 Prev := Empty;
3710 while Present (Comp) loop
3711 if Is_Aliased (Comp) then
3712 Aliased_Component := True;
3713 end if;
3715 -- Handle the component and discriminant case
3717 if Ekind_In (Comp, E_Component, E_Discriminant) then
3718 declare
3719 CC : constant Node_Id := Component_Clause (Comp);
3721 begin
3722 -- Freezing a record type freezes the type of each of its
3723 -- components. However, if the type of the component is
3724 -- part of this record, we do not want or need a separate
3725 -- Freeze_Node. Note that Is_Itype is wrong because that's
3726 -- also set in private type cases. We also can't check for
3727 -- the Scope being exactly Rec because of private types and
3728 -- record extensions.
3730 if Is_Itype (Etype (Comp))
3731 and then Is_Record_Type (Underlying_Type
3732 (Scope (Etype (Comp))))
3733 then
3734 Undelay_Type (Etype (Comp));
3735 end if;
3737 Freeze_And_Append (Etype (Comp), N, Result);
3739 -- Warn for pragma Pack overriding foreign convention
3741 if Has_Foreign_Convention (Etype (Comp))
3742 and then Has_Pragma_Pack (Rec)
3744 -- Don't warn for aliased components, since override
3745 -- cannot happen in that case.
3747 and then not Is_Aliased (Comp)
3748 then
3749 declare
3750 CN : constant Name_Id :=
3751 Get_Convention_Name (Convention (Etype (Comp)));
3752 PP : constant Node_Id :=
3753 Get_Pragma (Rec, Pragma_Pack);
3754 begin
3755 if Present (PP) then
3756 Error_Msg_Name_1 := CN;
3757 Error_Msg_Sloc := Sloc (Comp);
3758 Error_Msg_N
3759 ("pragma Pack affects convention % component#??",
3760 PP);
3761 Error_Msg_Name_1 := CN;
3762 Error_Msg_NE
3763 ("\component & may not have % compatible "
3764 & "representation??", PP, Comp);
3765 end if;
3766 end;
3767 end if;
3769 -- Check for error of component clause given for variable
3770 -- sized type. We have to delay this test till this point,
3771 -- since the component type has to be frozen for us to know
3772 -- if it is variable length.
3774 if Present (CC) then
3775 Placed_Component := True;
3777 -- We omit this test in a generic context, it will be
3778 -- applied at instantiation time.
3780 if Inside_A_Generic then
3781 null;
3783 -- Also omit this test in CodePeer mode, since we do not
3784 -- have sufficient info on size and rep clauses.
3786 elsif CodePeer_Mode then
3787 null;
3789 -- Omit check if component has a generic type. This can
3790 -- happen in an instantiation within a generic in ASIS
3791 -- mode, where we force freeze actions without full
3792 -- expansion.
3794 elsif Is_Generic_Type (Etype (Comp)) then
3795 null;
3797 -- Do the check
3799 elsif not
3800 Size_Known_At_Compile_Time
3801 (Underlying_Type (Etype (Comp)))
3802 then
3803 Error_Msg_N
3804 ("component clause not allowed for variable " &
3805 "length component", CC);
3806 end if;
3808 else
3809 Unplaced_Component := True;
3810 end if;
3812 -- Case of component requires byte alignment
3814 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
3816 -- Set the enclosing record to also require byte align
3818 Set_Must_Be_On_Byte_Boundary (Rec);
3820 -- Check for component clause that is inconsistent with
3821 -- the required byte boundary alignment.
3823 if Present (CC)
3824 and then Normalized_First_Bit (Comp) mod
3825 System_Storage_Unit /= 0
3826 then
3827 Error_Msg_N
3828 ("component & must be byte aligned",
3829 Component_Name (Component_Clause (Comp)));
3830 end if;
3831 end if;
3832 end;
3833 end if;
3835 -- Gather data for possible Implicit_Packing later. Note that at
3836 -- this stage we might be dealing with a real component, or with
3837 -- an implicit subtype declaration.
3839 if not Is_Scalar_Type (Etype (Comp)) then
3840 All_Scalar_Components := False;
3841 else
3842 Scalar_Component_Total_RM_Size :=
3843 Scalar_Component_Total_RM_Size + RM_Size (Etype (Comp));
3844 Scalar_Component_Total_Esize :=
3845 Scalar_Component_Total_Esize + Esize (Etype (Comp));
3846 end if;
3848 -- If the component is an Itype with Delayed_Freeze and is either
3849 -- a record or array subtype and its base type has not yet been
3850 -- frozen, we must remove this from the entity list of this record
3851 -- and put it on the entity list of the scope of its base type.
3852 -- Note that we know that this is not the type of a component
3853 -- since we cleared Has_Delayed_Freeze for it in the previous
3854 -- loop. Thus this must be the Designated_Type of an access type,
3855 -- which is the type of a component.
3857 if Is_Itype (Comp)
3858 and then Is_Type (Scope (Comp))
3859 and then Is_Composite_Type (Comp)
3860 and then Base_Type (Comp) /= Comp
3861 and then Has_Delayed_Freeze (Comp)
3862 and then not Is_Frozen (Base_Type (Comp))
3863 then
3864 declare
3865 Will_Be_Frozen : Boolean := False;
3866 S : Entity_Id;
3868 begin
3869 -- We have a difficult case to handle here. Suppose Rec is
3870 -- subtype being defined in a subprogram that's created as
3871 -- part of the freezing of Rec'Base. In that case, we know
3872 -- that Comp'Base must have already been frozen by the time
3873 -- we get to elaborate this because Gigi doesn't elaborate
3874 -- any bodies until it has elaborated all of the declarative
3875 -- part. But Is_Frozen will not be set at this point because
3876 -- we are processing code in lexical order.
3878 -- We detect this case by going up the Scope chain of Rec
3879 -- and seeing if we have a subprogram scope before reaching
3880 -- the top of the scope chain or that of Comp'Base. If we
3881 -- do, then mark that Comp'Base will actually be frozen. If
3882 -- so, we merely undelay it.
3884 S := Scope (Rec);
3885 while Present (S) loop
3886 if Is_Subprogram (S) then
3887 Will_Be_Frozen := True;
3888 exit;
3889 elsif S = Scope (Base_Type (Comp)) then
3890 exit;
3891 end if;
3893 S := Scope (S);
3894 end loop;
3896 if Will_Be_Frozen then
3897 Undelay_Type (Comp);
3899 else
3900 if Present (Prev) then
3901 Set_Next_Entity (Prev, Next_Entity (Comp));
3902 else
3903 Set_First_Entity (Rec, Next_Entity (Comp));
3904 end if;
3906 -- Insert in entity list of scope of base type (which
3907 -- must be an enclosing scope, because still unfrozen).
3909 Append_Entity (Comp, Scope (Base_Type (Comp)));
3910 end if;
3911 end;
3913 -- If the component is an access type with an allocator as default
3914 -- value, the designated type will be frozen by the corresponding
3915 -- expression in init_proc. In order to place the freeze node for
3916 -- the designated type before that for the current record type,
3917 -- freeze it now.
3919 -- Same process if the component is an array of access types,
3920 -- initialized with an aggregate. If the designated type is
3921 -- private, it cannot contain allocators, and it is premature
3922 -- to freeze the type, so we check for this as well.
3924 elsif Is_Access_Type (Etype (Comp))
3925 and then Present (Parent (Comp))
3926 and then Present (Expression (Parent (Comp)))
3927 then
3928 declare
3929 Alloc : constant Node_Id :=
3930 Check_Allocator (Expression (Parent (Comp)));
3932 begin
3933 if Present (Alloc) then
3935 -- If component is pointer to a class-wide type, freeze
3936 -- the specific type in the expression being allocated.
3937 -- The expression may be a subtype indication, in which
3938 -- case freeze the subtype mark.
3940 if Is_Class_Wide_Type
3941 (Designated_Type (Etype (Comp)))
3942 then
3943 if Is_Entity_Name (Expression (Alloc)) then
3944 Freeze_And_Append
3945 (Entity (Expression (Alloc)), N, Result);
3947 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
3948 then
3949 Freeze_And_Append
3950 (Entity (Subtype_Mark (Expression (Alloc))),
3951 N, Result);
3952 end if;
3954 elsif Is_Itype (Designated_Type (Etype (Comp))) then
3955 Check_Itype (Etype (Comp));
3957 else
3958 Freeze_And_Append
3959 (Designated_Type (Etype (Comp)), N, Result);
3960 end if;
3961 end if;
3962 end;
3964 elsif Is_Access_Type (Etype (Comp))
3965 and then Is_Itype (Designated_Type (Etype (Comp)))
3966 then
3967 Check_Itype (Etype (Comp));
3969 -- Freeze the designated type when initializing a component with
3970 -- an aggregate in case the aggregate contains allocators.
3972 -- type T is ...;
3973 -- type T_Ptr is access all T;
3974 -- type T_Array is array ... of T_Ptr;
3976 -- type Rec is record
3977 -- Comp : T_Array := (others => ...);
3978 -- end record;
3980 elsif Is_Array_Type (Etype (Comp))
3981 and then Is_Access_Type (Component_Type (Etype (Comp)))
3982 then
3983 declare
3984 Comp_Par : constant Node_Id := Parent (Comp);
3985 Desig_Typ : constant Entity_Id :=
3986 Designated_Type
3987 (Component_Type (Etype (Comp)));
3989 begin
3990 -- The only case when this sort of freezing is not done is
3991 -- when the designated type is class-wide and the root type
3992 -- is the record owning the component. This scenario results
3993 -- in a circularity because the class-wide type requires
3994 -- primitives that have not been created yet as the root
3995 -- type is in the process of being frozen.
3997 -- type Rec is tagged;
3998 -- type Rec_Ptr is access all Rec'Class;
3999 -- type Rec_Array is array ... of Rec_Ptr;
4001 -- type Rec is record
4002 -- Comp : Rec_Array := (others => ...);
4003 -- end record;
4005 if Is_Class_Wide_Type (Desig_Typ)
4006 and then Root_Type (Desig_Typ) = Rec
4007 then
4008 null;
4010 elsif Is_Fully_Defined (Desig_Typ)
4011 and then Present (Comp_Par)
4012 and then Nkind (Comp_Par) = N_Component_Declaration
4013 and then Present (Expression (Comp_Par))
4014 and then Nkind (Expression (Comp_Par)) = N_Aggregate
4015 then
4016 Freeze_And_Append (Desig_Typ, N, Result);
4017 end if;
4018 end;
4019 end if;
4021 Prev := Comp;
4022 Next_Entity (Comp);
4023 end loop;
4025 SSO_ADC :=
4026 Get_Attribute_Definition_Clause
4027 (Rec, Attribute_Scalar_Storage_Order);
4029 -- If the record type has Complex_Representation, then it is treated
4030 -- as a scalar in the back end so the storage order is irrelevant.
4032 if Has_Complex_Representation (Rec) then
4033 if Present (SSO_ADC) then
4034 Error_Msg_N
4035 ("??storage order has no effect with Complex_Representation",
4036 SSO_ADC);
4037 end if;
4039 else
4040 -- Deal with default setting of reverse storage order
4042 Set_SSO_From_Default (Rec);
4044 -- Check consistent attribute setting on component types
4046 declare
4047 Comp_ADC_Present : Boolean;
4048 begin
4049 Comp := First_Component (Rec);
4050 while Present (Comp) loop
4051 Check_Component_Storage_Order
4052 (Encl_Type => Rec,
4053 Comp => Comp,
4054 ADC => SSO_ADC,
4055 Comp_ADC_Present => Comp_ADC_Present);
4056 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
4057 Next_Component (Comp);
4058 end loop;
4059 end;
4061 -- Now deal with reverse storage order/bit order issues
4063 if Present (SSO_ADC) then
4065 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
4066 -- if the former is specified.
4068 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
4070 -- Note: report error on Rec, not on SSO_ADC, as ADC may
4071 -- apply to some ancestor type.
4073 Error_Msg_Sloc := Sloc (SSO_ADC);
4074 Error_Msg_N
4075 ("scalar storage order for& specified# inconsistent with "
4076 & "bit order", Rec);
4077 end if;
4079 -- Warn if there is a Scalar_Storage_Order attribute definition
4080 -- clause but no component clause, no component that itself has
4081 -- such an attribute definition, and no pragma Pack.
4083 if not (Placed_Component
4084 or else
4085 SSO_ADC_Component
4086 or else
4087 Is_Packed (Rec))
4088 then
4089 Error_Msg_N
4090 ("??scalar storage order specified but no component "
4091 & "clause", SSO_ADC);
4092 end if;
4093 end if;
4094 end if;
4096 -- Deal with Bit_Order aspect
4098 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
4100 if Present (ADC) and then Base_Type (Rec) = Rec then
4101 if not (Placed_Component
4102 or else Present (SSO_ADC)
4103 or else Is_Packed (Rec))
4104 then
4105 -- Warn if clause has no effect when no component clause is
4106 -- present, but suppress warning if the Bit_Order is required
4107 -- due to the presence of a Scalar_Storage_Order attribute.
4109 Error_Msg_N
4110 ("??bit order specification has no effect", ADC);
4111 Error_Msg_N
4112 ("\??since no component clauses were specified", ADC);
4114 -- Here is where we do the processing to adjust component clauses
4115 -- for reversed bit order, when not using reverse SSO.
4117 elsif Reverse_Bit_Order (Rec)
4118 and then not Reverse_Storage_Order (Rec)
4119 then
4120 Adjust_Record_For_Reverse_Bit_Order (Rec);
4122 -- Case where we have both an explicit Bit_Order and the same
4123 -- Scalar_Storage_Order: leave record untouched, the back-end
4124 -- will take care of required layout conversions.
4126 else
4127 null;
4129 end if;
4130 end if;
4132 -- Complete error checking on record representation clause (e.g.
4133 -- overlap of components). This is called after adjusting the
4134 -- record for reverse bit order.
4136 declare
4137 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
4138 begin
4139 if Present (RRC) then
4140 Check_Record_Representation_Clause (RRC);
4141 end if;
4142 end;
4144 -- Set OK_To_Reorder_Components depending on debug flags
4146 if Is_Base_Type (Rec) and then Convention (Rec) = Convention_Ada then
4147 if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
4148 or else
4149 (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
4150 then
4151 Set_OK_To_Reorder_Components (Rec);
4152 end if;
4153 end if;
4155 -- Check for useless pragma Pack when all components placed. We only
4156 -- do this check for record types, not subtypes, since a subtype may
4157 -- have all its components placed, and it still makes perfectly good
4158 -- sense to pack other subtypes or the parent type. We do not give
4159 -- this warning if Optimize_Alignment is set to Space, since the
4160 -- pragma Pack does have an effect in this case (it always resets
4161 -- the alignment to one).
4163 if Ekind (Rec) = E_Record_Type
4164 and then Is_Packed (Rec)
4165 and then not Unplaced_Component
4166 and then Optimize_Alignment /= 'S'
4167 then
4168 -- Reset packed status. Probably not necessary, but we do it so
4169 -- that there is no chance of the back end doing something strange
4170 -- with this redundant indication of packing.
4172 Set_Is_Packed (Rec, False);
4174 -- Give warning if redundant constructs warnings on
4176 if Warn_On_Redundant_Constructs then
4177 Error_Msg_N -- CODEFIX
4178 ("??pragma Pack has no effect, no unplaced components",
4179 Get_Rep_Pragma (Rec, Name_Pack));
4180 end if;
4181 end if;
4183 -- If this is the record corresponding to a remote type, freeze the
4184 -- remote type here since that is what we are semantically freezing.
4185 -- This prevents the freeze node for that type in an inner scope.
4187 if Ekind (Rec) = E_Record_Type then
4188 if Present (Corresponding_Remote_Type (Rec)) then
4189 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
4190 end if;
4192 -- Check for controlled components and unchecked unions.
4194 Comp := First_Component (Rec);
4195 while Present (Comp) loop
4197 -- Do not set Has_Controlled_Component on a class-wide
4198 -- equivalent type. See Make_CW_Equivalent_Type.
4200 if not Is_Class_Wide_Equivalent_Type (Rec)
4201 and then
4202 (Has_Controlled_Component (Etype (Comp))
4203 or else
4204 (Chars (Comp) /= Name_uParent
4205 and then Is_Controlled_Active (Etype (Comp)))
4206 or else
4207 (Is_Protected_Type (Etype (Comp))
4208 and then
4209 Present (Corresponding_Record_Type (Etype (Comp)))
4210 and then
4211 Has_Controlled_Component
4212 (Corresponding_Record_Type (Etype (Comp)))))
4213 then
4214 Set_Has_Controlled_Component (Rec);
4215 end if;
4217 if Has_Unchecked_Union (Etype (Comp)) then
4218 Set_Has_Unchecked_Union (Rec);
4219 end if;
4221 -- Scan component declaration for likely misuses of current
4222 -- instance, either in a constraint or a default expression.
4224 if Has_Per_Object_Constraint (Comp) then
4225 Check_Current_Instance (Parent (Comp));
4226 end if;
4228 Next_Component (Comp);
4229 end loop;
4230 end if;
4232 -- Enforce the restriction that access attributes with a current
4233 -- instance prefix can only apply to limited types. This comment
4234 -- is floating here, but does not seem to belong here???
4236 -- Set component alignment if not otherwise already set
4238 Set_Component_Alignment_If_Not_Set (Rec);
4240 -- For first subtypes, check if there are any fixed-point fields with
4241 -- component clauses, where we must check the size. This is not done
4242 -- till the freeze point since for fixed-point types, we do not know
4243 -- the size until the type is frozen. Similar processing applies to
4244 -- bit packed arrays.
4246 if Is_First_Subtype (Rec) then
4247 Comp := First_Component (Rec);
4248 while Present (Comp) loop
4249 if Present (Component_Clause (Comp))
4250 and then (Is_Fixed_Point_Type (Etype (Comp))
4251 or else Is_Bit_Packed_Array (Etype (Comp)))
4252 then
4253 Check_Size
4254 (Component_Name (Component_Clause (Comp)),
4255 Etype (Comp),
4256 Esize (Comp),
4257 Junk);
4258 end if;
4260 Next_Component (Comp);
4261 end loop;
4262 end if;
4264 -- Generate warning for applying C or C++ convention to a record
4265 -- with discriminants. This is suppressed for the unchecked union
4266 -- case, since the whole point in this case is interface C. We also
4267 -- do not generate this within instantiations, since we will have
4268 -- generated a message on the template.
4270 if Has_Discriminants (E)
4271 and then not Is_Unchecked_Union (E)
4272 and then (Convention (E) = Convention_C
4273 or else
4274 Convention (E) = Convention_CPP)
4275 and then Comes_From_Source (E)
4276 and then not In_Instance
4277 and then not Has_Warnings_Off (E)
4278 and then not Has_Warnings_Off (Base_Type (E))
4279 then
4280 declare
4281 Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
4282 A2 : Node_Id;
4284 begin
4285 if Present (Cprag) then
4286 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
4288 if Convention (E) = Convention_C then
4289 Error_Msg_N
4290 ("?x?variant record has no direct equivalent in C",
4291 A2);
4292 else
4293 Error_Msg_N
4294 ("?x?variant record has no direct equivalent in C++",
4295 A2);
4296 end if;
4298 Error_Msg_NE
4299 ("\?x?use of convention for type& is dubious", A2, E);
4300 end if;
4301 end;
4302 end if;
4304 -- See if Size is too small as is (and implicit packing might help)
4306 if not Is_Packed (Rec)
4308 -- No implicit packing if even one component is explicitly placed
4310 and then not Placed_Component
4312 -- Or even one component is aliased
4314 and then not Aliased_Component
4316 -- Must have size clause and all scalar components
4318 and then Has_Size_Clause (Rec)
4319 and then All_Scalar_Components
4321 -- Do not try implicit packing on records with discriminants, too
4322 -- complicated, especially in the variant record case.
4324 and then not Has_Discriminants (Rec)
4326 -- We can implicitly pack if the specified size of the record is
4327 -- less than the sum of the object sizes (no point in packing if
4328 -- this is not the case).
4330 and then RM_Size (Rec) < Scalar_Component_Total_Esize
4332 -- And the total RM size cannot be greater than the specified size
4333 -- since otherwise packing will not get us where we have to be.
4335 and then RM_Size (Rec) >= Scalar_Component_Total_RM_Size
4337 -- Never do implicit packing in CodePeer or SPARK modes since
4338 -- we don't do any packing in these modes, since this generates
4339 -- over-complex code that confuses static analysis, and in
4340 -- general, neither CodePeer not GNATprove care about the
4341 -- internal representation of objects.
4343 and then not (CodePeer_Mode or GNATprove_Mode)
4344 then
4345 -- If implicit packing enabled, do it
4347 if Implicit_Packing then
4348 Set_Is_Packed (Rec);
4350 -- Otherwise flag the size clause
4352 else
4353 declare
4354 Sz : constant Node_Id := Size_Clause (Rec);
4355 begin
4356 Error_Msg_NE -- CODEFIX
4357 ("size given for& too small", Sz, Rec);
4358 Error_Msg_N -- CODEFIX
4359 ("\use explicit pragma Pack "
4360 & "or use pragma Implicit_Packing", Sz);
4361 end;
4362 end if;
4363 end if;
4365 -- The following checks are relevant only when SPARK_Mode is on as
4366 -- they are not standard Ada legality rules.
4368 if SPARK_Mode = On then
4369 if Is_Effectively_Volatile (Rec) then
4371 -- A discriminated type cannot be effectively volatile
4372 -- (SPARK RM C.6(4)).
4374 if Has_Discriminants (Rec) then
4375 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
4377 -- A tagged type cannot be effectively volatile
4378 -- (SPARK RM C.6(5)).
4380 elsif Is_Tagged_Type (Rec) then
4381 Error_Msg_N ("tagged type & cannot be volatile", Rec);
4382 end if;
4384 -- A non-effectively volatile record type cannot contain
4385 -- effectively volatile components (SPARK RM C.6(2)).
4387 else
4388 Comp := First_Component (Rec);
4389 while Present (Comp) loop
4390 if Comes_From_Source (Comp)
4391 and then Is_Effectively_Volatile (Etype (Comp))
4392 then
4393 Error_Msg_Name_1 := Chars (Rec);
4394 Error_Msg_N
4395 ("component & of non-volatile type % cannot be "
4396 & "volatile", Comp);
4397 end if;
4399 Next_Component (Comp);
4400 end loop;
4401 end if;
4403 -- A type which does not yield a synchronized object cannot have
4404 -- a component that yields a synchronized object (SPARK RM 9.5).
4406 if not Yields_Synchronized_Object (Rec) then
4407 Comp := First_Component (Rec);
4408 while Present (Comp) loop
4409 if Comes_From_Source (Comp)
4410 and then Yields_Synchronized_Object (Etype (Comp))
4411 then
4412 Error_Msg_Name_1 := Chars (Rec);
4413 Error_Msg_N
4414 ("component & of non-synchronized type % cannot be "
4415 & "synchronized", Comp);
4416 end if;
4418 Next_Component (Comp);
4419 end loop;
4420 end if;
4422 -- A Ghost type cannot have a component of protected or task type
4423 -- (SPARK RM 6.9(19)).
4425 if Is_Ghost_Entity (Rec) then
4426 Comp := First_Component (Rec);
4427 while Present (Comp) loop
4428 if Comes_From_Source (Comp)
4429 and then Is_Concurrent_Type (Etype (Comp))
4430 then
4431 Error_Msg_Name_1 := Chars (Rec);
4432 Error_Msg_N
4433 ("component & of ghost type % cannot be concurrent",
4434 Comp);
4435 end if;
4437 Next_Component (Comp);
4438 end loop;
4439 end if;
4440 end if;
4442 -- Make sure that if we have an iterator aspect, then we have
4443 -- either Constant_Indexing or Variable_Indexing.
4445 declare
4446 Iterator_Aspect : Node_Id;
4448 begin
4449 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
4451 if No (Iterator_Aspect) then
4452 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
4453 end if;
4455 if Present (Iterator_Aspect) then
4456 if Has_Aspect (Rec, Aspect_Constant_Indexing)
4457 or else
4458 Has_Aspect (Rec, Aspect_Variable_Indexing)
4459 then
4460 null;
4461 else
4462 Error_Msg_N
4463 ("Iterator_Element requires indexing aspect",
4464 Iterator_Aspect);
4465 end if;
4466 end if;
4467 end;
4469 -- All done if not a full record definition
4471 if Ekind (Rec) /= E_Record_Type then
4472 return;
4473 end if;
4475 -- Finally we need to check the variant part to make sure that
4476 -- all types within choices are properly frozen as part of the
4477 -- freezing of the record type.
4479 Check_Variant_Part : declare
4480 D : constant Node_Id := Declaration_Node (Rec);
4481 T : Node_Id;
4482 C : Node_Id;
4484 begin
4485 -- Find component list
4487 C := Empty;
4489 if Nkind (D) = N_Full_Type_Declaration then
4490 T := Type_Definition (D);
4492 if Nkind (T) = N_Record_Definition then
4493 C := Component_List (T);
4495 elsif Nkind (T) = N_Derived_Type_Definition
4496 and then Present (Record_Extension_Part (T))
4497 then
4498 C := Component_List (Record_Extension_Part (T));
4499 end if;
4500 end if;
4502 -- Case of variant part present
4504 if Present (C) and then Present (Variant_Part (C)) then
4505 Freeze_Choices_In_Variant_Part (Variant_Part (C));
4506 end if;
4508 -- Note: we used to call Check_Choices here, but it is too early,
4509 -- since predicated subtypes are frozen here, but their freezing
4510 -- actions are in Analyze_Freeze_Entity, which has not been called
4511 -- yet for entities frozen within this procedure, so we moved that
4512 -- call to the Analyze_Freeze_Entity for the record type.
4514 end Check_Variant_Part;
4516 -- Check that all the primitives of an interface type are abstract
4517 -- or null procedures.
4519 if Is_Interface (Rec)
4520 and then not Error_Posted (Parent (Rec))
4521 then
4522 declare
4523 Elmt : Elmt_Id;
4524 Subp : Entity_Id;
4526 begin
4527 Elmt := First_Elmt (Primitive_Operations (Rec));
4528 while Present (Elmt) loop
4529 Subp := Node (Elmt);
4531 if not Is_Abstract_Subprogram (Subp)
4533 -- Avoid reporting the error on inherited primitives
4535 and then Comes_From_Source (Subp)
4536 then
4537 Error_Msg_Name_1 := Chars (Subp);
4539 if Ekind (Subp) = E_Procedure then
4540 if not Null_Present (Parent (Subp)) then
4541 Error_Msg_N
4542 ("interface procedure % must be abstract or null",
4543 Parent (Subp));
4544 end if;
4545 else
4546 Error_Msg_N
4547 ("interface function % must be abstract",
4548 Parent (Subp));
4549 end if;
4550 end if;
4552 Next_Elmt (Elmt);
4553 end loop;
4554 end;
4555 end if;
4556 end Freeze_Record_Type;
4558 -------------------------------
4559 -- Has_Boolean_Aspect_Import --
4560 -------------------------------
4562 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4563 Decl : constant Node_Id := Declaration_Node (E);
4564 Asp : Node_Id;
4565 Expr : Node_Id;
4567 begin
4568 if Has_Aspects (Decl) then
4569 Asp := First (Aspect_Specifications (Decl));
4570 while Present (Asp) loop
4571 Expr := Expression (Asp);
4573 -- The value of aspect Import is True when the expression is
4574 -- either missing or it is explicitly set to True.
4576 if Get_Aspect_Id (Asp) = Aspect_Import
4577 and then (No (Expr)
4578 or else (Compile_Time_Known_Value (Expr)
4579 and then Is_True (Expr_Value (Expr))))
4580 then
4581 return True;
4582 end if;
4584 Next (Asp);
4585 end loop;
4586 end if;
4588 return False;
4589 end Has_Boolean_Aspect_Import;
4591 ----------------------------
4592 -- Late_Freeze_Subprogram --
4593 ----------------------------
4595 procedure Late_Freeze_Subprogram (E : Entity_Id) is
4596 Spec : constant Node_Id :=
4597 Specification (Unit_Declaration_Node (Scope (E)));
4598 Decls : List_Id;
4600 begin
4601 if Present (Private_Declarations (Spec)) then
4602 Decls := Private_Declarations (Spec);
4603 else
4604 Decls := Visible_Declarations (Spec);
4605 end if;
4607 Append_List (Result, Decls);
4608 end Late_Freeze_Subprogram;
4610 ---------------------
4611 -- New_Freeze_Node --
4612 ---------------------
4614 function New_Freeze_Node return Node_Id is
4615 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4616 Result : Node_Id;
4618 begin
4619 -- Handle the case where an ignored Ghost subprogram freezes the type
4620 -- of one of its formals. The type can either be non-Ghost or checked
4621 -- Ghost. Since the freeze node for the type is generated in the
4622 -- context of the subprogram, the node will be incorrectly flagged as
4623 -- ignored Ghost and erroneously removed from the tree.
4625 -- type Typ is ...;
4626 -- procedure Ignored_Ghost_Proc (Formal : Typ) with Ghost;
4628 -- Reset the Ghost mode to "none". This preserves the freeze node.
4630 if Ghost_Mode = Ignore
4631 and then not Is_Ignored_Ghost_Entity (E)
4632 and then not Is_Ignored_Ghost_Node (E)
4633 then
4634 Ghost_Mode := None;
4635 end if;
4637 Result := New_Node (N_Freeze_Entity, Loc);
4639 Ghost_Mode := Save_Ghost_Mode;
4640 return Result;
4641 end New_Freeze_Node;
4643 ------------------------------
4644 -- Wrap_Imported_Subprogram --
4645 ------------------------------
4647 -- The issue here is that our normal approach of checking preconditions
4648 -- and postconditions does not work for imported procedures, since we
4649 -- are not generating code for the body. To get around this we create
4650 -- a wrapper, as shown by the following example:
4652 -- procedure K (A : Integer);
4653 -- pragma Import (C, K);
4655 -- The spec is rewritten by removing the effects of pragma Import, but
4656 -- leaving the convention unchanged, as though the source had said:
4658 -- procedure K (A : Integer);
4659 -- pragma Convention (C, K);
4661 -- and we create a body, added to the entity K freeze actions, which
4662 -- looks like:
4664 -- procedure K (A : Integer) is
4665 -- procedure K (A : Integer);
4666 -- pragma Import (C, K);
4667 -- begin
4668 -- K (A);
4669 -- end K;
4671 -- Now the contract applies in the normal way to the outer procedure,
4672 -- and the inner procedure has no contracts, so there is no problem
4673 -- in just calling it to get the original effect.
4675 -- In the case of a function, we create an appropriate return statement
4676 -- for the subprogram body that calls the inner procedure.
4678 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
4679 Loc : constant Source_Ptr := Sloc (E);
4680 CE : constant Name_Id := Chars (E);
4681 Spec : Node_Id;
4682 Parms : List_Id;
4683 Stmt : Node_Id;
4684 Iprag : Node_Id;
4685 Bod : Node_Id;
4686 Forml : Entity_Id;
4688 begin
4689 -- Nothing to do if not imported
4691 if not Is_Imported (E) then
4692 return;
4694 -- Test enabling conditions for wrapping
4696 elsif Is_Subprogram (E)
4697 and then Present (Contract (E))
4698 and then Present (Pre_Post_Conditions (Contract (E)))
4699 and then not GNATprove_Mode
4700 then
4701 -- Here we do the wrap
4703 -- Note on calls to Copy_Separate_Tree. The trees we are copying
4704 -- here are fully analyzed, but we definitely want fully syntactic
4705 -- unanalyzed trees in the body we construct, so that the analysis
4706 -- generates the right visibility, and that is exactly what the
4707 -- calls to Copy_Separate_Tree give us.
4709 -- Acquire copy of Inline pragma, and indicate that it does not
4710 -- come from an aspect, as it applies to an internal entity.
4712 Iprag := Copy_Separate_Tree (Import_Pragma (E));
4713 Set_From_Aspect_Specification (Iprag, False);
4715 -- Fix up spec to be not imported any more
4717 Set_Is_Imported (E, False);
4718 Set_Interface_Name (E, Empty);
4719 Set_Has_Completion (E, False);
4720 Set_Import_Pragma (E, Empty);
4722 -- Grab the subprogram declaration and specification
4724 Spec := Declaration_Node (E);
4726 -- Build parameter list that we need
4728 Parms := New_List;
4729 Forml := First_Formal (E);
4730 while Present (Forml) loop
4731 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
4732 Next_Formal (Forml);
4733 end loop;
4735 -- Build the call
4737 if Ekind_In (E, E_Function, E_Generic_Function) then
4738 Stmt :=
4739 Make_Simple_Return_Statement (Loc,
4740 Expression =>
4741 Make_Function_Call (Loc,
4742 Name => Make_Identifier (Loc, CE),
4743 Parameter_Associations => Parms));
4745 else
4746 Stmt :=
4747 Make_Procedure_Call_Statement (Loc,
4748 Name => Make_Identifier (Loc, CE),
4749 Parameter_Associations => Parms);
4750 end if;
4752 -- Now build the body
4754 Bod :=
4755 Make_Subprogram_Body (Loc,
4756 Specification =>
4757 Copy_Separate_Tree (Spec),
4758 Declarations => New_List (
4759 Make_Subprogram_Declaration (Loc,
4760 Specification =>
4761 Copy_Separate_Tree (Spec)),
4762 Iprag),
4763 Handled_Statement_Sequence =>
4764 Make_Handled_Sequence_Of_Statements (Loc,
4765 Statements => New_List (Stmt),
4766 End_Label => Make_Identifier (Loc, CE)));
4768 -- Append the body to freeze result
4770 Add_To_Result (Bod);
4771 return;
4773 -- Case of imported subprogram that does not get wrapped
4775 else
4776 -- Set Is_Public. All imported entities need an external symbol
4777 -- created for them since they are always referenced from another
4778 -- object file. Note this used to be set when we set Is_Imported
4779 -- back in Sem_Prag, but now we delay it to this point, since we
4780 -- don't want to set this flag if we wrap an imported subprogram.
4782 Set_Is_Public (E);
4783 end if;
4784 end Wrap_Imported_Subprogram;
4786 -- Local variables
4788 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4790 -- Start of processing for Freeze_Entity
4792 begin
4793 -- The entity being frozen may be subject to pragma Ghost. Set the mode
4794 -- now to ensure that any nodes generated during freezing are properly
4795 -- flagged as Ghost.
4797 Set_Ghost_Mode_From_Entity (E);
4799 -- We are going to test for various reasons why this entity need not be
4800 -- frozen here, but in the case of an Itype that's defined within a
4801 -- record, that test actually applies to the record.
4803 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
4804 Test_E := Scope (E);
4805 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
4806 and then Is_Record_Type (Underlying_Type (Scope (E)))
4807 then
4808 Test_E := Underlying_Type (Scope (E));
4809 end if;
4811 -- Do not freeze if already frozen since we only need one freeze node
4813 if Is_Frozen (E) then
4814 Ghost_Mode := Save_Ghost_Mode;
4815 return No_List;
4817 -- It is improper to freeze an external entity within a generic because
4818 -- its freeze node will appear in a non-valid context. The entity will
4819 -- be frozen in the proper scope after the current generic is analyzed.
4820 -- However, aspects must be analyzed because they may be queried later
4821 -- within the generic itself, and the corresponding pragma or attribute
4822 -- definition has not been analyzed yet.
4824 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
4825 if Has_Delayed_Aspects (E) then
4826 Analyze_Aspects_At_Freeze_Point (E);
4827 end if;
4829 Ghost_Mode := Save_Ghost_Mode;
4830 return No_List;
4832 -- AI05-0213: A formal incomplete type does not freeze the actual. In
4833 -- the instance, the same applies to the subtype renaming the actual.
4835 elsif Is_Private_Type (E)
4836 and then Is_Generic_Actual_Type (E)
4837 and then No (Full_View (Base_Type (E)))
4838 and then Ada_Version >= Ada_2012
4839 then
4840 Ghost_Mode := Save_Ghost_Mode;
4841 return No_List;
4843 -- Formal subprograms are never frozen
4845 elsif Is_Formal_Subprogram (E) then
4846 Ghost_Mode := Save_Ghost_Mode;
4847 return No_List;
4849 -- Generic types are never frozen as they lack delayed semantic checks
4851 elsif Is_Generic_Type (E) then
4852 Ghost_Mode := Save_Ghost_Mode;
4853 return No_List;
4855 -- Do not freeze a global entity within an inner scope created during
4856 -- expansion. A call to subprogram E within some internal procedure
4857 -- (a stream attribute for example) might require freezing E, but the
4858 -- freeze node must appear in the same declarative part as E itself.
4859 -- The two-pass elaboration mechanism in gigi guarantees that E will
4860 -- be frozen before the inner call is elaborated. We exclude constants
4861 -- from this test, because deferred constants may be frozen early, and
4862 -- must be diagnosed (e.g. in the case of a deferred constant being used
4863 -- in a default expression). If the enclosing subprogram comes from
4864 -- source, or is a generic instance, then the freeze point is the one
4865 -- mandated by the language, and we freeze the entity. A subprogram that
4866 -- is a child unit body that acts as a spec does not have a spec that
4867 -- comes from source, but can only come from source.
4869 elsif In_Open_Scopes (Scope (Test_E))
4870 and then Scope (Test_E) /= Current_Scope
4871 and then Ekind (Test_E) /= E_Constant
4872 then
4873 declare
4874 S : Entity_Id;
4876 begin
4877 S := Current_Scope;
4878 while Present (S) loop
4879 if Is_Overloadable (S) then
4880 if Comes_From_Source (S)
4881 or else Is_Generic_Instance (S)
4882 or else Is_Child_Unit (S)
4883 then
4884 exit;
4885 else
4886 Ghost_Mode := Save_Ghost_Mode;
4887 return No_List;
4888 end if;
4889 end if;
4891 S := Scope (S);
4892 end loop;
4893 end;
4895 -- Similarly, an inlined instance body may make reference to global
4896 -- entities, but these references cannot be the proper freezing point
4897 -- for them, and in the absence of inlining freezing will take place in
4898 -- their own scope. Normally instance bodies are analyzed after the
4899 -- enclosing compilation, and everything has been frozen at the proper
4900 -- place, but with front-end inlining an instance body is compiled
4901 -- before the end of the enclosing scope, and as a result out-of-order
4902 -- freezing must be prevented.
4904 elsif Front_End_Inlining
4905 and then In_Instance_Body
4906 and then Present (Scope (Test_E))
4907 then
4908 declare
4909 S : Entity_Id;
4911 begin
4912 S := Scope (Test_E);
4913 while Present (S) loop
4914 if Is_Generic_Instance (S) then
4915 exit;
4916 else
4917 S := Scope (S);
4918 end if;
4919 end loop;
4921 if No (S) then
4922 Ghost_Mode := Save_Ghost_Mode;
4923 return No_List;
4924 end if;
4925 end;
4927 elsif Ekind (E) = E_Generic_Package then
4928 Result := Freeze_Generic_Entities (E);
4930 Ghost_Mode := Save_Ghost_Mode;
4931 return Result;
4932 end if;
4934 -- Add checks to detect proper initialization of scalars that may appear
4935 -- as subprogram parameters.
4937 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
4938 Apply_Parameter_Validity_Checks (E);
4939 end if;
4941 -- Deal with delayed aspect specifications. The analysis of the aspect
4942 -- is required to be delayed to the freeze point, thus we analyze the
4943 -- pragma or attribute definition clause in the tree at this point. We
4944 -- also analyze the aspect specification node at the freeze point when
4945 -- the aspect doesn't correspond to pragma/attribute definition clause.
4947 if Has_Delayed_Aspects (E) then
4948 Analyze_Aspects_At_Freeze_Point (E);
4949 end if;
4951 -- Here to freeze the entity
4953 Set_Is_Frozen (E);
4955 -- Case of entity being frozen is other than a type
4957 if not Is_Type (E) then
4959 -- If entity is exported or imported and does not have an external
4960 -- name, now is the time to provide the appropriate default name.
4961 -- Skip this if the entity is stubbed, since we don't need a name
4962 -- for any stubbed routine. For the case on intrinsics, if no
4963 -- external name is specified, then calls will be handled in
4964 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
4965 -- external name is provided, then Expand_Intrinsic_Call leaves
4966 -- calls in place for expansion by GIGI.
4968 if (Is_Imported (E) or else Is_Exported (E))
4969 and then No (Interface_Name (E))
4970 and then Convention (E) /= Convention_Stubbed
4971 and then Convention (E) /= Convention_Intrinsic
4972 then
4973 Set_Encoded_Interface_Name
4974 (E, Get_Default_External_Name (E));
4976 -- If entity is an atomic object appearing in a declaration and
4977 -- the expression is an aggregate, assign it to a temporary to
4978 -- ensure that the actual assignment is done atomically rather
4979 -- than component-wise (the assignment to the temp may be done
4980 -- component-wise, but that is harmless).
4982 elsif Is_Atomic_Or_VFA (E)
4983 and then Nkind (Parent (E)) = N_Object_Declaration
4984 and then Present (Expression (Parent (E)))
4985 and then Nkind (Expression (Parent (E))) = N_Aggregate
4986 and then Is_Atomic_VFA_Aggregate (Expression (Parent (E)))
4987 then
4988 null;
4989 end if;
4991 -- Subprogram case
4993 if Is_Subprogram (E) then
4995 -- Check for needing to wrap imported subprogram
4997 Wrap_Imported_Subprogram (E);
4999 -- Freeze all parameter types and the return type (RM 13.14(14)).
5000 -- However skip this for internal subprograms. This is also where
5001 -- any extra formal parameters are created since we now know
5002 -- whether the subprogram will use a foreign convention.
5004 -- In Ada 2012, freezing a subprogram does not always freeze the
5005 -- corresponding profile (see AI05-019). An attribute reference
5006 -- is not a freezing point of the profile. Flag Do_Freeze_Profile
5007 -- indicates whether the profile should be frozen now.
5008 -- Other constructs that should not freeze ???
5010 -- This processing doesn't apply to internal entities (see below)
5011 -- In ASIS mode the profile is frozen unconditionally, to prevent
5012 -- backend anomalies.
5014 if not Is_Internal (E)
5015 and then (Do_Freeze_Profile or ASIS_Mode)
5016 then
5017 if not Freeze_Profile (E) then
5018 Ghost_Mode := Save_Ghost_Mode;
5019 return Result;
5020 end if;
5021 end if;
5023 -- Must freeze its parent first if it is a derived subprogram
5025 if Present (Alias (E)) then
5026 Freeze_And_Append (Alias (E), N, Result);
5027 end if;
5029 -- We don't freeze internal subprograms, because we don't normally
5030 -- want addition of extra formals or mechanism setting to happen
5031 -- for those. However we do pass through predefined dispatching
5032 -- cases, since extra formals may be needed in some cases, such as
5033 -- for the stream 'Input function (build-in-place formals).
5035 if not Is_Internal (E)
5036 or else Is_Predefined_Dispatching_Operation (E)
5037 then
5038 Freeze_Subprogram (E);
5039 end if;
5041 if Late_Freezing then
5042 Late_Freeze_Subprogram (E);
5043 Ghost_Mode := Save_Ghost_Mode;
5044 return No_List;
5045 end if;
5047 -- If warning on suspicious contracts then check for the case of
5048 -- a postcondition other than False for a No_Return subprogram.
5050 if No_Return (E)
5051 and then Warn_On_Suspicious_Contract
5052 and then Present (Contract (E))
5053 then
5054 declare
5055 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
5056 Exp : Node_Id;
5058 begin
5059 while Present (Prag) loop
5060 if Nam_In (Pragma_Name (Prag), Name_Post,
5061 Name_Postcondition,
5062 Name_Refined_Post)
5063 then
5064 Exp :=
5065 Expression
5066 (First (Pragma_Argument_Associations (Prag)));
5068 if Nkind (Exp) /= N_Identifier
5069 or else Chars (Exp) /= Name_False
5070 then
5071 Error_Msg_NE
5072 ("useless postcondition, & is marked "
5073 & "No_Return?T?", Exp, E);
5074 end if;
5075 end if;
5077 Prag := Next_Pragma (Prag);
5078 end loop;
5079 end;
5080 end if;
5082 -- Here for other than a subprogram or type
5084 else
5085 -- If entity has a type, and it is not a generic unit, then
5086 -- freeze it first (RM 13.14(10)).
5088 if Present (Etype (E))
5089 and then Ekind (E) /= E_Generic_Function
5090 then
5091 Freeze_And_Append (Etype (E), N, Result);
5093 -- For an object of an anonymous array type, aspects on the
5094 -- object declaration apply to the type itself. This is the
5095 -- case for Atomic_Components, Volatile_Components, and
5096 -- Independent_Components. In these cases analysis of the
5097 -- generated pragma will mark the anonymous types accordingly,
5098 -- and the object itself does not require a freeze node.
5100 if Ekind (E) = E_Variable
5101 and then Is_Itype (Etype (E))
5102 and then Is_Array_Type (Etype (E))
5103 and then Has_Delayed_Aspects (E)
5104 then
5105 Set_Has_Delayed_Aspects (E, False);
5106 Set_Has_Delayed_Freeze (E, False);
5107 Set_Freeze_Node (E, Empty);
5108 end if;
5109 end if;
5111 -- Special processing for objects created by object declaration
5113 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
5114 Freeze_Object_Declaration (E);
5115 end if;
5117 -- Check that a constant which has a pragma Volatile[_Components]
5118 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
5120 -- Note: Atomic[_Components] also sets Volatile[_Components]
5122 if Ekind (E) = E_Constant
5123 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
5124 and then not Is_Imported (E)
5125 and then not Has_Boolean_Aspect_Import (E)
5126 then
5127 -- Make sure we actually have a pragma, and have not merely
5128 -- inherited the indication from elsewhere (e.g. an address
5129 -- clause, which is not good enough in RM terms).
5131 if Has_Rep_Pragma (E, Name_Atomic)
5132 or else
5133 Has_Rep_Pragma (E, Name_Atomic_Components)
5134 then
5135 Error_Msg_N
5136 ("stand alone atomic constant must be " &
5137 "imported (RM C.6(13))", E);
5139 elsif Has_Rep_Pragma (E, Name_Volatile)
5140 or else
5141 Has_Rep_Pragma (E, Name_Volatile_Components)
5142 then
5143 Error_Msg_N
5144 ("stand alone volatile constant must be " &
5145 "imported (RM C.6(13))", E);
5146 end if;
5147 end if;
5149 -- Static objects require special handling
5151 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
5152 and then Is_Statically_Allocated (E)
5153 then
5154 Freeze_Static_Object (E);
5155 end if;
5157 -- Remaining step is to layout objects
5159 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
5160 or else Is_Formal (E)
5161 then
5162 Layout_Object (E);
5163 end if;
5165 -- For an object that does not have delayed freezing, and whose
5166 -- initialization actions have been captured in a compound
5167 -- statement, move them back now directly within the enclosing
5168 -- statement sequence.
5170 if Ekind_In (E, E_Constant, E_Variable)
5171 and then not Has_Delayed_Freeze (E)
5172 then
5173 Explode_Initialization_Compound_Statement (E);
5174 end if;
5175 end if;
5177 -- Case of a type or subtype being frozen
5179 else
5180 -- We used to check here that a full type must have preelaborable
5181 -- initialization if it completes a private type specified with
5182 -- pragma Preelaborable_Initialization, but that missed cases where
5183 -- the types occur within a generic package, since the freezing
5184 -- that occurs within a containing scope generally skips traversal
5185 -- of a generic unit's declarations (those will be frozen within
5186 -- instances). This check was moved to Analyze_Package_Specification.
5188 -- The type may be defined in a generic unit. This can occur when
5189 -- freezing a generic function that returns the type (which is
5190 -- defined in a parent unit). It is clearly meaningless to freeze
5191 -- this type. However, if it is a subtype, its size may be determi-
5192 -- nable and used in subsequent checks, so might as well try to
5193 -- compute it.
5195 -- In Ada 2012, Freeze_Entities is also used in the front end to
5196 -- trigger the analysis of aspect expressions, so in this case we
5197 -- want to continue the freezing process.
5199 if Present (Scope (E))
5200 and then Is_Generic_Unit (Scope (E))
5201 and then
5202 (not Has_Predicates (E)
5203 and then not Has_Delayed_Freeze (E))
5204 then
5205 Check_Compile_Time_Size (E);
5206 Ghost_Mode := Save_Ghost_Mode;
5207 return No_List;
5208 end if;
5210 -- Check for error of Type_Invariant'Class applied to an untagged
5211 -- type (check delayed to freeze time when full type is available).
5213 declare
5214 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
5215 begin
5216 if Present (Prag)
5217 and then Class_Present (Prag)
5218 and then not Is_Tagged_Type (E)
5219 then
5220 Error_Msg_NE
5221 ("Type_Invariant''Class cannot be specified for &",
5222 Prag, E);
5223 Error_Msg_N
5224 ("\can only be specified for a tagged type", Prag);
5225 end if;
5226 end;
5228 if Is_Ghost_Entity (E) then
5230 -- A Ghost type cannot be concurrent (SPARK RM 6.9(19)). Verify
5231 -- this legality rule first to five a finer-grained diagnostic.
5233 if Is_Concurrent_Type (E) then
5234 Error_Msg_N ("ghost type & cannot be concurrent", E);
5236 -- A Ghost type cannot be effectively volatile (SPARK RM 6.9(7))
5238 elsif Is_Effectively_Volatile (E) then
5239 Error_Msg_N ("ghost type & cannot be volatile", E);
5240 end if;
5241 end if;
5243 -- Deal with special cases of freezing for subtype
5245 if E /= Base_Type (E) then
5247 -- Before we do anything else, a specialized test for the case of
5248 -- a size given for an array where the array needs to be packed,
5249 -- but was not so the size cannot be honored. This is the case
5250 -- where implicit packing may apply. The reason we do this so
5251 -- early is that if we have implicit packing, the layout of the
5252 -- base type is affected, so we must do this before we freeze
5253 -- the base type.
5255 -- We could do this processing only if implicit packing is enabled
5256 -- since in all other cases, the error would be caught by the back
5257 -- end. However, we choose to do the check even if we do not have
5258 -- implicit packing enabled, since this allows us to give a more
5259 -- useful error message (advising use of pragmas Implicit_Packing
5260 -- or Pack).
5262 if Is_Array_Type (E) then
5263 declare
5264 Ctyp : constant Entity_Id := Component_Type (E);
5265 Rsiz : constant Uint := RM_Size (Ctyp);
5266 SZ : constant Node_Id := Size_Clause (E);
5267 Btyp : constant Entity_Id := Base_Type (E);
5269 Lo : Node_Id;
5270 Hi : Node_Id;
5271 Indx : Node_Id;
5273 Num_Elmts : Uint;
5274 -- Number of elements in array
5276 begin
5277 -- Check enabling conditions. These are straightforward
5278 -- except for the test for a limited composite type. This
5279 -- eliminates the rare case of a array of limited components
5280 -- where there are issues of whether or not we can go ahead
5281 -- and pack the array (since we can't freely pack and unpack
5282 -- arrays if they are limited).
5284 -- Note that we check the root type explicitly because the
5285 -- whole point is we are doing this test before we have had
5286 -- a chance to freeze the base type (and it is that freeze
5287 -- action that causes stuff to be inherited).
5289 if Has_Size_Clause (E)
5290 and then Known_Static_RM_Size (E)
5291 and then not Is_Packed (E)
5292 and then not Has_Pragma_Pack (E)
5293 and then not Has_Component_Size_Clause (E)
5294 and then Known_Static_RM_Size (Ctyp)
5295 and then RM_Size (Ctyp) < 64
5296 and then not Is_Limited_Composite (E)
5297 and then not Is_Packed (Root_Type (E))
5298 and then not Has_Component_Size_Clause (Root_Type (E))
5299 and then not (CodePeer_Mode or GNATprove_Mode)
5300 then
5301 -- Compute number of elements in array
5303 Num_Elmts := Uint_1;
5304 Indx := First_Index (E);
5305 while Present (Indx) loop
5306 Get_Index_Bounds (Indx, Lo, Hi);
5308 if not (Compile_Time_Known_Value (Lo)
5309 and then
5310 Compile_Time_Known_Value (Hi))
5311 then
5312 goto No_Implicit_Packing;
5313 end if;
5315 Num_Elmts :=
5316 Num_Elmts *
5317 UI_Max (Uint_0,
5318 Expr_Value (Hi) - Expr_Value (Lo) + 1);
5319 Next_Index (Indx);
5320 end loop;
5322 -- What we are looking for here is the situation where
5323 -- the RM_Size given would be exactly right if there was
5324 -- a pragma Pack (resulting in the component size being
5325 -- the same as the RM_Size). Furthermore, the component
5326 -- type size must be an odd size (not a multiple of
5327 -- storage unit). If the component RM size is an exact
5328 -- number of storage units that is a power of two, the
5329 -- array is not packed and has a standard representation.
5331 if RM_Size (E) = Num_Elmts * Rsiz
5332 and then Rsiz mod System_Storage_Unit /= 0
5333 then
5334 -- For implicit packing mode, just set the component
5335 -- size silently.
5337 if Implicit_Packing then
5338 Set_Component_Size (Btyp, Rsiz);
5339 Set_Is_Bit_Packed_Array (Btyp);
5340 Set_Is_Packed (Btyp);
5341 Set_Has_Non_Standard_Rep (Btyp);
5343 -- Otherwise give an error message
5345 else
5346 Error_Msg_NE
5347 ("size given for& too small", SZ, E);
5348 Error_Msg_N -- CODEFIX
5349 ("\use explicit pragma Pack "
5350 & "or use pragma Implicit_Packing", SZ);
5351 end if;
5353 elsif RM_Size (E) = Num_Elmts * Rsiz
5354 and then Implicit_Packing
5355 and then
5356 (Rsiz / System_Storage_Unit = 1
5357 or else
5358 Rsiz / System_Storage_Unit = 2
5359 or else
5360 Rsiz / System_Storage_Unit = 4)
5361 then
5362 -- Not a packed array, but indicate the desired
5363 -- component size, for the back-end.
5365 Set_Component_Size (Btyp, Rsiz);
5366 end if;
5367 end if;
5368 end;
5369 end if;
5371 <<No_Implicit_Packing>>
5373 -- If ancestor subtype present, freeze that first. Note that this
5374 -- will also get the base type frozen. Need RM reference ???
5376 Atype := Ancestor_Subtype (E);
5378 if Present (Atype) then
5379 Freeze_And_Append (Atype, N, Result);
5381 -- No ancestor subtype present
5383 else
5384 -- See if we have a nearest ancestor that has a predicate.
5385 -- That catches the case of derived type with a predicate.
5386 -- Need RM reference here ???
5388 Atype := Nearest_Ancestor (E);
5390 if Present (Atype) and then Has_Predicates (Atype) then
5391 Freeze_And_Append (Atype, N, Result);
5392 end if;
5394 -- Freeze base type before freezing the entity (RM 13.14(15))
5396 if E /= Base_Type (E) then
5397 Freeze_And_Append (Base_Type (E), N, Result);
5398 end if;
5399 end if;
5401 -- A subtype inherits all the type-related representation aspects
5402 -- from its parents (RM 13.1(8)).
5404 Inherit_Aspects_At_Freeze_Point (E);
5406 -- For a derived type, freeze its parent type first (RM 13.14(15))
5408 elsif Is_Derived_Type (E) then
5409 Freeze_And_Append (Etype (E), N, Result);
5410 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5412 -- A derived type inherits each type-related representation aspect
5413 -- of its parent type that was directly specified before the
5414 -- declaration of the derived type (RM 13.1(15)).
5416 Inherit_Aspects_At_Freeze_Point (E);
5417 end if;
5419 -- Check for incompatible size and alignment for record type
5421 if Warn_On_Size_Alignment
5422 and then Is_Record_Type (E)
5423 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5425 -- If explicit Object_Size clause given assume that the programmer
5426 -- knows what he is doing, and expects the compiler behavior.
5428 and then not Has_Object_Size_Clause (E)
5430 -- Check for size not a multiple of alignment
5432 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5433 then
5434 declare
5435 SC : constant Node_Id := Size_Clause (E);
5436 AC : constant Node_Id := Alignment_Clause (E);
5437 Loc : Node_Id;
5438 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5440 begin
5441 if Present (SC) and then Present (AC) then
5443 -- Give a warning
5445 if Sloc (SC) > Sloc (AC) then
5446 Loc := SC;
5447 Error_Msg_NE
5448 ("?Z?size is not a multiple of alignment for &",
5449 Loc, E);
5450 Error_Msg_Sloc := Sloc (AC);
5451 Error_Msg_Uint_1 := Alignment (E);
5452 Error_Msg_N ("\?Z?alignment of ^ specified #", Loc);
5454 else
5455 Loc := AC;
5456 Error_Msg_NE
5457 ("?Z?size is not a multiple of alignment for &",
5458 Loc, E);
5459 Error_Msg_Sloc := Sloc (SC);
5460 Error_Msg_Uint_1 := RM_Size (E);
5461 Error_Msg_N ("\?Z?size of ^ specified #", Loc);
5462 end if;
5464 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5465 Error_Msg_N ("\?Z?Object_Size will be increased to ^", Loc);
5466 end if;
5467 end;
5468 end if;
5470 -- Array type
5472 if Is_Array_Type (E) then
5473 Freeze_Array_Type (E);
5475 -- For a class-wide type, the corresponding specific type is
5476 -- frozen as well (RM 13.14(15))
5478 elsif Is_Class_Wide_Type (E) then
5479 Freeze_And_Append (Root_Type (E), N, Result);
5481 -- If the base type of the class-wide type is still incomplete,
5482 -- the class-wide remains unfrozen as well. This is legal when
5483 -- E is the formal of a primitive operation of some other type
5484 -- which is being frozen.
5486 if not Is_Frozen (Root_Type (E)) then
5487 Set_Is_Frozen (E, False);
5488 Ghost_Mode := Save_Ghost_Mode;
5489 return Result;
5490 end if;
5492 -- The equivalent type associated with a class-wide subtype needs
5493 -- to be frozen to ensure that its layout is done.
5495 if Ekind (E) = E_Class_Wide_Subtype
5496 and then Present (Equivalent_Type (E))
5497 then
5498 Freeze_And_Append (Equivalent_Type (E), N, Result);
5499 end if;
5501 -- Generate an itype reference for a library-level class-wide type
5502 -- at the freeze point. Otherwise the first explicit reference to
5503 -- the type may appear in an inner scope which will be rejected by
5504 -- the back-end.
5506 if Is_Itype (E)
5507 and then Is_Compilation_Unit (Scope (E))
5508 then
5509 declare
5510 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5512 begin
5513 Set_Itype (Ref, E);
5515 -- From a gigi point of view, a class-wide subtype derives
5516 -- from its record equivalent type. As a result, the itype
5517 -- reference must appear after the freeze node of the
5518 -- equivalent type or gigi will reject the reference.
5520 if Ekind (E) = E_Class_Wide_Subtype
5521 and then Present (Equivalent_Type (E))
5522 then
5523 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5524 else
5525 Add_To_Result (Ref);
5526 end if;
5527 end;
5528 end if;
5530 -- For a record type or record subtype, freeze all component types
5531 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5532 -- using Is_Record_Type, because we don't want to attempt the freeze
5533 -- for the case of a private type with record extension (we will do
5534 -- that later when the full type is frozen).
5536 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype)
5537 and then not (Present (Scope (E))
5538 and then Is_Generic_Unit (Scope (E)))
5539 then
5540 Freeze_Record_Type (E);
5542 -- For a concurrent type, freeze corresponding record type. This does
5543 -- not correspond to any specific rule in the RM, but the record type
5544 -- is essentially part of the concurrent type. Also freeze all local
5545 -- entities. This includes record types created for entry parameter
5546 -- blocks and whatever local entities may appear in the private part.
5548 elsif Is_Concurrent_Type (E) then
5549 if Present (Corresponding_Record_Type (E)) then
5550 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5551 end if;
5553 Comp := First_Entity (E);
5554 while Present (Comp) loop
5555 if Is_Type (Comp) then
5556 Freeze_And_Append (Comp, N, Result);
5558 elsif (Ekind (Comp)) /= E_Function then
5560 -- The guard on the presence of the Etype seems to be needed
5561 -- for some CodePeer (-gnatcC) cases, but not clear why???
5563 if Present (Etype (Comp)) then
5564 if Is_Itype (Etype (Comp))
5565 and then Underlying_Type (Scope (Etype (Comp))) = E
5566 then
5567 Undelay_Type (Etype (Comp));
5568 end if;
5570 Freeze_And_Append (Etype (Comp), N, Result);
5571 end if;
5572 end if;
5574 Next_Entity (Comp);
5575 end loop;
5577 -- Private types are required to point to the same freeze node as
5578 -- their corresponding full views. The freeze node itself has to
5579 -- point to the partial view of the entity (because from the partial
5580 -- view, we can retrieve the full view, but not the reverse).
5581 -- However, in order to freeze correctly, we need to freeze the full
5582 -- view. If we are freezing at the end of a scope (or within the
5583 -- scope) of the private type, the partial and full views will have
5584 -- been swapped, the full view appears first in the entity chain and
5585 -- the swapping mechanism ensures that the pointers are properly set
5586 -- (on scope exit).
5588 -- If we encounter the partial view before the full view (e.g. when
5589 -- freezing from another scope), we freeze the full view, and then
5590 -- set the pointers appropriately since we cannot rely on swapping to
5591 -- fix things up (subtypes in an outer scope might not get swapped).
5593 -- If the full view is itself private, the above requirements apply
5594 -- to the underlying full view instead of the full view. But there is
5595 -- no swapping mechanism for the underlying full view so we need to
5596 -- set the pointers appropriately in both cases.
5598 elsif Is_Incomplete_Or_Private_Type (E)
5599 and then not Is_Generic_Type (E)
5600 then
5601 -- The construction of the dispatch table associated with library
5602 -- level tagged types forces freezing of all the primitives of the
5603 -- type, which may cause premature freezing of the partial view.
5604 -- For example:
5606 -- package Pkg is
5607 -- type T is tagged private;
5608 -- type DT is new T with private;
5609 -- procedure Prim (X : in out T; Y : in out DT'Class);
5610 -- private
5611 -- type T is tagged null record;
5612 -- Obj : T;
5613 -- type DT is new T with null record;
5614 -- end;
5616 -- In this case the type will be frozen later by the usual
5617 -- mechanism: an object declaration, an instantiation, or the
5618 -- end of a declarative part.
5620 if Is_Library_Level_Tagged_Type (E)
5621 and then not Present (Full_View (E))
5622 then
5623 Set_Is_Frozen (E, False);
5624 Ghost_Mode := Save_Ghost_Mode;
5625 return Result;
5627 -- Case of full view present
5629 elsif Present (Full_View (E)) then
5631 -- If full view has already been frozen, then no further
5632 -- processing is required
5634 if Is_Frozen (Full_View (E)) then
5635 Set_Has_Delayed_Freeze (E, False);
5636 Set_Freeze_Node (E, Empty);
5638 -- Otherwise freeze full view and patch the pointers so that
5639 -- the freeze node will elaborate both views in the back end.
5640 -- However, if full view is itself private, freeze underlying
5641 -- full view instead and patch the pointers so that the freeze
5642 -- node will elaborate the three views in the back end.
5644 else
5645 declare
5646 Full : Entity_Id := Full_View (E);
5648 begin
5649 if Is_Private_Type (Full)
5650 and then Present (Underlying_Full_View (Full))
5651 then
5652 Full := Underlying_Full_View (Full);
5653 end if;
5655 Freeze_And_Append (Full, N, Result);
5657 if Full /= Full_View (E)
5658 and then Has_Delayed_Freeze (Full_View (E))
5659 then
5660 F_Node := Freeze_Node (Full);
5662 if Present (F_Node) then
5663 Set_Freeze_Node (Full_View (E), F_Node);
5664 Set_Entity (F_Node, Full_View (E));
5666 else
5667 Set_Has_Delayed_Freeze (Full_View (E), False);
5668 Set_Freeze_Node (Full_View (E), Empty);
5669 end if;
5670 end if;
5672 if Has_Delayed_Freeze (E) then
5673 F_Node := Freeze_Node (Full_View (E));
5675 if Present (F_Node) then
5676 Set_Freeze_Node (E, F_Node);
5677 Set_Entity (F_Node, E);
5679 else
5680 -- {Incomplete,Private}_Subtypes with Full_Views
5681 -- constrained by discriminants.
5683 Set_Has_Delayed_Freeze (E, False);
5684 Set_Freeze_Node (E, Empty);
5685 end if;
5686 end if;
5687 end;
5688 end if;
5690 Check_Debug_Info_Needed (E);
5692 -- AI-117 requires that the convention of a partial view be the
5693 -- same as the convention of the full view. Note that this is a
5694 -- recognized breach of privacy, but it's essential for logical
5695 -- consistency of representation, and the lack of a rule in
5696 -- RM95 was an oversight.
5698 Set_Convention (E, Convention (Full_View (E)));
5700 Set_Size_Known_At_Compile_Time (E,
5701 Size_Known_At_Compile_Time (Full_View (E)));
5703 -- Size information is copied from the full view to the
5704 -- incomplete or private view for consistency.
5706 -- We skip this is the full view is not a type. This is very
5707 -- strange of course, and can only happen as a result of
5708 -- certain illegalities, such as a premature attempt to derive
5709 -- from an incomplete type.
5711 if Is_Type (Full_View (E)) then
5712 Set_Size_Info (E, Full_View (E));
5713 Set_RM_Size (E, RM_Size (Full_View (E)));
5714 end if;
5716 Ghost_Mode := Save_Ghost_Mode;
5717 return Result;
5719 -- Case of underlying full view present
5721 elsif Is_Private_Type (E)
5722 and then Present (Underlying_Full_View (E))
5723 then
5724 if not Is_Frozen (Underlying_Full_View (E)) then
5725 Freeze_And_Append (Underlying_Full_View (E), N, Result);
5726 end if;
5728 -- Patch the pointers so that the freeze node will elaborate
5729 -- both views in the back end.
5731 if Has_Delayed_Freeze (E) then
5732 F_Node := Freeze_Node (Underlying_Full_View (E));
5734 if Present (F_Node) then
5735 Set_Freeze_Node (E, F_Node);
5736 Set_Entity (F_Node, E);
5738 else
5739 Set_Has_Delayed_Freeze (E, False);
5740 Set_Freeze_Node (E, Empty);
5741 end if;
5742 end if;
5744 Check_Debug_Info_Needed (E);
5746 Ghost_Mode := Save_Ghost_Mode;
5747 return Result;
5749 -- Case of no full view present. If entity is derived or subtype,
5750 -- it is safe to freeze, correctness depends on the frozen status
5751 -- of parent. Otherwise it is either premature usage, or a Taft
5752 -- amendment type, so diagnosis is at the point of use and the
5753 -- type might be frozen later.
5755 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
5756 null;
5758 else
5759 Set_Is_Frozen (E, False);
5760 Ghost_Mode := Save_Ghost_Mode;
5761 return No_List;
5762 end if;
5764 -- For access subprogram, freeze types of all formals, the return
5765 -- type was already frozen, since it is the Etype of the function.
5766 -- Formal types can be tagged Taft amendment types, but otherwise
5767 -- they cannot be incomplete.
5769 elsif Ekind (E) = E_Subprogram_Type then
5770 Formal := First_Formal (E);
5771 while Present (Formal) loop
5772 if Ekind (Etype (Formal)) = E_Incomplete_Type
5773 and then No (Full_View (Etype (Formal)))
5774 then
5775 if Is_Tagged_Type (Etype (Formal)) then
5776 null;
5778 -- AI05-151: Incomplete types are allowed in access to
5779 -- subprogram specifications.
5781 elsif Ada_Version < Ada_2012 then
5782 Error_Msg_NE
5783 ("invalid use of incomplete type&", E, Etype (Formal));
5784 end if;
5785 end if;
5787 Freeze_And_Append (Etype (Formal), N, Result);
5788 Next_Formal (Formal);
5789 end loop;
5791 Freeze_Subprogram (E);
5793 -- For access to a protected subprogram, freeze the equivalent type
5794 -- (however this is not set if we are not generating code or if this
5795 -- is an anonymous type used just for resolution).
5797 elsif Is_Access_Protected_Subprogram_Type (E) then
5798 if Present (Equivalent_Type (E)) then
5799 Freeze_And_Append (Equivalent_Type (E), N, Result);
5800 end if;
5801 end if;
5803 -- Generic types are never seen by the back-end, and are also not
5804 -- processed by the expander (since the expander is turned off for
5805 -- generic processing), so we never need freeze nodes for them.
5807 if Is_Generic_Type (E) then
5808 Ghost_Mode := Save_Ghost_Mode;
5809 return Result;
5810 end if;
5812 -- Some special processing for non-generic types to complete
5813 -- representation details not known till the freeze point.
5815 if Is_Fixed_Point_Type (E) then
5816 Freeze_Fixed_Point_Type (E);
5818 -- Some error checks required for ordinary fixed-point type. Defer
5819 -- these till the freeze-point since we need the small and range
5820 -- values. We only do these checks for base types
5822 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
5823 if Small_Value (E) < Ureal_2_M_80 then
5824 Error_Msg_Name_1 := Name_Small;
5825 Error_Msg_N
5826 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
5828 elsif Small_Value (E) > Ureal_2_80 then
5829 Error_Msg_Name_1 := Name_Small;
5830 Error_Msg_N
5831 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
5832 end if;
5834 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
5835 Error_Msg_Name_1 := Name_First;
5836 Error_Msg_N
5837 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
5838 end if;
5840 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
5841 Error_Msg_Name_1 := Name_Last;
5842 Error_Msg_N
5843 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
5844 end if;
5845 end if;
5847 elsif Is_Enumeration_Type (E) then
5848 Freeze_Enumeration_Type (E);
5850 elsif Is_Integer_Type (E) then
5851 Adjust_Esize_For_Alignment (E);
5853 if Is_Modular_Integer_Type (E)
5854 and then Warn_On_Suspicious_Modulus_Value
5855 then
5856 Check_Suspicious_Modulus (E);
5857 end if;
5859 -- The pool applies to named and anonymous access types, but not
5860 -- to subprogram and to internal types generated for 'Access
5861 -- references.
5863 elsif Is_Access_Type (E)
5864 and then not Is_Access_Subprogram_Type (E)
5865 and then Ekind (E) /= E_Access_Attribute_Type
5866 then
5867 -- If a pragma Default_Storage_Pool applies, and this type has no
5868 -- Storage_Pool or Storage_Size clause (which must have occurred
5869 -- before the freezing point), then use the default. This applies
5870 -- only to base types.
5872 -- None of this applies to access to subprograms, for which there
5873 -- are clearly no pools.
5875 if Present (Default_Pool)
5876 and then Is_Base_Type (E)
5877 and then not Has_Storage_Size_Clause (E)
5878 and then No (Associated_Storage_Pool (E))
5879 then
5880 -- Case of pragma Default_Storage_Pool (null)
5882 if Nkind (Default_Pool) = N_Null then
5883 Set_No_Pool_Assigned (E);
5885 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
5887 else
5888 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
5889 end if;
5890 end if;
5892 -- Check restriction for standard storage pool
5894 if No (Associated_Storage_Pool (E)) then
5895 Check_Restriction (No_Standard_Storage_Pools, E);
5896 end if;
5898 -- Deal with error message for pure access type. This is not an
5899 -- error in Ada 2005 if there is no pool (see AI-366).
5901 if Is_Pure_Unit_Access_Type (E)
5902 and then (Ada_Version < Ada_2005
5903 or else not No_Pool_Assigned (E))
5904 and then not Is_Generic_Unit (Scope (E))
5905 then
5906 Error_Msg_N ("named access type not allowed in pure unit", E);
5908 if Ada_Version >= Ada_2005 then
5909 Error_Msg_N
5910 ("\would be legal if Storage_Size of 0 given??", E);
5912 elsif No_Pool_Assigned (E) then
5913 Error_Msg_N
5914 ("\would be legal in Ada 2005??", E);
5916 else
5917 Error_Msg_N
5918 ("\would be legal in Ada 2005 if "
5919 & "Storage_Size of 0 given??", E);
5920 end if;
5921 end if;
5922 end if;
5924 -- Case of composite types
5926 if Is_Composite_Type (E) then
5928 -- AI-117 requires that all new primitives of a tagged type must
5929 -- inherit the convention of the full view of the type. Inherited
5930 -- and overriding operations are defined to inherit the convention
5931 -- of their parent or overridden subprogram (also specified in
5932 -- AI-117), which will have occurred earlier (in Derive_Subprogram
5933 -- and New_Overloaded_Entity). Here we set the convention of
5934 -- primitives that are still convention Ada, which will ensure
5935 -- that any new primitives inherit the type's convention. Class-
5936 -- wide types can have a foreign convention inherited from their
5937 -- specific type, but are excluded from this since they don't have
5938 -- any associated primitives.
5940 if Is_Tagged_Type (E)
5941 and then not Is_Class_Wide_Type (E)
5942 and then Convention (E) /= Convention_Ada
5943 then
5944 declare
5945 Prim_List : constant Elist_Id := Primitive_Operations (E);
5946 Prim : Elmt_Id;
5948 begin
5949 Prim := First_Elmt (Prim_List);
5950 while Present (Prim) loop
5951 if Convention (Node (Prim)) = Convention_Ada then
5952 Set_Convention (Node (Prim), Convention (E));
5953 end if;
5955 Next_Elmt (Prim);
5956 end loop;
5957 end;
5958 end if;
5960 -- If the type is a simple storage pool type, then this is where
5961 -- we attempt to locate and validate its Allocate, Deallocate, and
5962 -- Storage_Size operations (the first is required, and the latter
5963 -- two are optional). We also verify that the full type for a
5964 -- private type is allowed to be a simple storage pool type.
5966 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
5967 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
5968 then
5969 -- If the type is marked Has_Private_Declaration, then this is
5970 -- a full type for a private type that was specified with the
5971 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
5972 -- pragma is allowed for the full type (for example, it can't
5973 -- be an array type, or a nonlimited record type).
5975 if Has_Private_Declaration (E) then
5976 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
5977 and then not Is_Private_Type (E)
5978 then
5979 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
5980 Error_Msg_N
5981 ("pragma% can only apply to full type that is an " &
5982 "explicitly limited type", E);
5983 end if;
5984 end if;
5986 Validate_Simple_Pool_Ops : declare
5987 Pool_Type : Entity_Id renames E;
5988 Address_Type : constant Entity_Id := RTE (RE_Address);
5989 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
5991 procedure Validate_Simple_Pool_Op_Formal
5992 (Pool_Op : Entity_Id;
5993 Pool_Op_Formal : in out Entity_Id;
5994 Expected_Mode : Formal_Kind;
5995 Expected_Type : Entity_Id;
5996 Formal_Name : String;
5997 OK_Formal : in out Boolean);
5998 -- Validate one formal Pool_Op_Formal of the candidate pool
5999 -- operation Pool_Op. The formal must be of Expected_Type
6000 -- and have mode Expected_Mode. OK_Formal will be set to
6001 -- False if the formal doesn't match. If OK_Formal is False
6002 -- on entry, then the formal will effectively be ignored
6003 -- (because validation of the pool op has already failed).
6004 -- Upon return, Pool_Op_Formal will be updated to the next
6005 -- formal, if any.
6007 procedure Validate_Simple_Pool_Operation
6008 (Op_Name : Name_Id);
6009 -- Search for and validate a simple pool operation with the
6010 -- name Op_Name. If the name is Allocate, then there must be
6011 -- exactly one such primitive operation for the simple pool
6012 -- type. If the name is Deallocate or Storage_Size, then
6013 -- there can be at most one such primitive operation. The
6014 -- profile of the located primitive must conform to what
6015 -- is expected for each operation.
6017 ------------------------------------
6018 -- Validate_Simple_Pool_Op_Formal --
6019 ------------------------------------
6021 procedure Validate_Simple_Pool_Op_Formal
6022 (Pool_Op : Entity_Id;
6023 Pool_Op_Formal : in out Entity_Id;
6024 Expected_Mode : Formal_Kind;
6025 Expected_Type : Entity_Id;
6026 Formal_Name : String;
6027 OK_Formal : in out Boolean)
6029 begin
6030 -- If OK_Formal is False on entry, then simply ignore
6031 -- the formal, because an earlier formal has already
6032 -- been flagged.
6034 if not OK_Formal then
6035 return;
6037 -- If no formal is passed in, then issue an error for a
6038 -- missing formal.
6040 elsif not Present (Pool_Op_Formal) then
6041 Error_Msg_NE
6042 ("simple storage pool op missing formal " &
6043 Formal_Name & " of type&", Pool_Op, Expected_Type);
6044 OK_Formal := False;
6046 return;
6047 end if;
6049 if Etype (Pool_Op_Formal) /= Expected_Type then
6051 -- If the pool type was expected for this formal, then
6052 -- this will not be considered a candidate operation
6053 -- for the simple pool, so we unset OK_Formal so that
6054 -- the op and any later formals will be ignored.
6056 if Expected_Type = Pool_Type then
6057 OK_Formal := False;
6059 return;
6061 else
6062 Error_Msg_NE
6063 ("wrong type for formal " & Formal_Name &
6064 " of simple storage pool op; expected type&",
6065 Pool_Op_Formal, Expected_Type);
6066 end if;
6067 end if;
6069 -- Issue error if formal's mode is not the expected one
6071 if Ekind (Pool_Op_Formal) /= Expected_Mode then
6072 Error_Msg_N
6073 ("wrong mode for formal of simple storage pool op",
6074 Pool_Op_Formal);
6075 end if;
6077 -- Advance to the next formal
6079 Next_Formal (Pool_Op_Formal);
6080 end Validate_Simple_Pool_Op_Formal;
6082 ------------------------------------
6083 -- Validate_Simple_Pool_Operation --
6084 ------------------------------------
6086 procedure Validate_Simple_Pool_Operation
6087 (Op_Name : Name_Id)
6089 Op : Entity_Id;
6090 Found_Op : Entity_Id := Empty;
6091 Formal : Entity_Id;
6092 Is_OK : Boolean;
6094 begin
6095 pragma Assert
6096 (Nam_In (Op_Name, Name_Allocate,
6097 Name_Deallocate,
6098 Name_Storage_Size));
6100 Error_Msg_Name_1 := Op_Name;
6102 -- For each homonym declared immediately in the scope
6103 -- of the simple storage pool type, determine whether
6104 -- the homonym is an operation of the pool type, and,
6105 -- if so, check that its profile is as expected for
6106 -- a simple pool operation of that name.
6108 Op := Get_Name_Entity_Id (Op_Name);
6109 while Present (Op) loop
6110 if Ekind_In (Op, E_Function, E_Procedure)
6111 and then Scope (Op) = Current_Scope
6112 then
6113 Formal := First_Entity (Op);
6115 Is_OK := True;
6117 -- The first parameter must be of the pool type
6118 -- in order for the operation to qualify.
6120 if Op_Name = Name_Storage_Size then
6121 Validate_Simple_Pool_Op_Formal
6122 (Op, Formal, E_In_Parameter, Pool_Type,
6123 "Pool", Is_OK);
6124 else
6125 Validate_Simple_Pool_Op_Formal
6126 (Op, Formal, E_In_Out_Parameter, Pool_Type,
6127 "Pool", Is_OK);
6128 end if;
6130 -- If another operation with this name has already
6131 -- been located for the type, then flag an error,
6132 -- since we only allow the type to have a single
6133 -- such primitive.
6135 if Present (Found_Op) and then Is_OK then
6136 Error_Msg_NE
6137 ("only one % operation allowed for " &
6138 "simple storage pool type&", Op, Pool_Type);
6139 end if;
6141 -- In the case of Allocate and Deallocate, a formal
6142 -- of type System.Address is required.
6144 if Op_Name = Name_Allocate then
6145 Validate_Simple_Pool_Op_Formal
6146 (Op, Formal, E_Out_Parameter,
6147 Address_Type, "Storage_Address", Is_OK);
6149 elsif Op_Name = Name_Deallocate then
6150 Validate_Simple_Pool_Op_Formal
6151 (Op, Formal, E_In_Parameter,
6152 Address_Type, "Storage_Address", Is_OK);
6153 end if;
6155 -- In the case of Allocate and Deallocate, formals
6156 -- of type Storage_Count are required as the third
6157 -- and fourth parameters.
6159 if Op_Name /= Name_Storage_Size then
6160 Validate_Simple_Pool_Op_Formal
6161 (Op, Formal, E_In_Parameter,
6162 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
6163 Validate_Simple_Pool_Op_Formal
6164 (Op, Formal, E_In_Parameter,
6165 Stg_Cnt_Type, "Alignment", Is_OK);
6166 end if;
6168 -- If no mismatched formals have been found (Is_OK)
6169 -- and no excess formals are present, then this
6170 -- operation has been validated, so record it.
6172 if not Present (Formal) and then Is_OK then
6173 Found_Op := Op;
6174 end if;
6175 end if;
6177 Op := Homonym (Op);
6178 end loop;
6180 -- There must be a valid Allocate operation for the type,
6181 -- so issue an error if none was found.
6183 if Op_Name = Name_Allocate
6184 and then not Present (Found_Op)
6185 then
6186 Error_Msg_N ("missing % operation for simple " &
6187 "storage pool type", Pool_Type);
6189 elsif Present (Found_Op) then
6191 -- Simple pool operations can't be abstract
6193 if Is_Abstract_Subprogram (Found_Op) then
6194 Error_Msg_N
6195 ("simple storage pool operation must not be " &
6196 "abstract", Found_Op);
6197 end if;
6199 -- The Storage_Size operation must be a function with
6200 -- Storage_Count as its result type.
6202 if Op_Name = Name_Storage_Size then
6203 if Ekind (Found_Op) = E_Procedure then
6204 Error_Msg_N
6205 ("% operation must be a function", Found_Op);
6207 elsif Etype (Found_Op) /= Stg_Cnt_Type then
6208 Error_Msg_NE
6209 ("wrong result type for%, expected type&",
6210 Found_Op, Stg_Cnt_Type);
6211 end if;
6213 -- Allocate and Deallocate must be procedures
6215 elsif Ekind (Found_Op) = E_Function then
6216 Error_Msg_N
6217 ("% operation must be a procedure", Found_Op);
6218 end if;
6219 end if;
6220 end Validate_Simple_Pool_Operation;
6222 -- Start of processing for Validate_Simple_Pool_Ops
6224 begin
6225 Validate_Simple_Pool_Operation (Name_Allocate);
6226 Validate_Simple_Pool_Operation (Name_Deallocate);
6227 Validate_Simple_Pool_Operation (Name_Storage_Size);
6228 end Validate_Simple_Pool_Ops;
6229 end if;
6230 end if;
6232 -- Now that all types from which E may depend are frozen, see if the
6233 -- size is known at compile time, if it must be unsigned, or if
6234 -- strict alignment is required
6236 Check_Compile_Time_Size (E);
6237 Check_Unsigned_Type (E);
6239 if Base_Type (E) = E then
6240 Check_Strict_Alignment (E);
6241 end if;
6243 -- Do not allow a size clause for a type which does not have a size
6244 -- that is known at compile time
6246 if Has_Size_Clause (E)
6247 and then not Size_Known_At_Compile_Time (E)
6248 then
6249 -- Suppress this message if errors posted on E, even if we are
6250 -- in all errors mode, since this is often a junk message
6252 if not Error_Posted (E) then
6253 Error_Msg_N
6254 ("size clause not allowed for variable length type",
6255 Size_Clause (E));
6256 end if;
6257 end if;
6259 -- Now we set/verify the representation information, in particular
6260 -- the size and alignment values. This processing is not required for
6261 -- generic types, since generic types do not play any part in code
6262 -- generation, and so the size and alignment values for such types
6263 -- are irrelevant. Ditto for types declared within a generic unit,
6264 -- which may have components that depend on generic parameters, and
6265 -- that will be recreated in an instance.
6267 if Inside_A_Generic then
6268 null;
6270 -- Otherwise we call the layout procedure
6272 else
6273 Layout_Type (E);
6274 end if;
6276 -- If this is an access to subprogram whose designated type is itself
6277 -- a subprogram type, the return type of this anonymous subprogram
6278 -- type must be decorated as well.
6280 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
6281 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
6282 then
6283 Layout_Type (Etype (Designated_Type (E)));
6284 end if;
6286 -- If the type has a Defaut_Value/Default_Component_Value aspect,
6287 -- this is where we analye the expression (after the type is frozen,
6288 -- since in the case of Default_Value, we are analyzing with the
6289 -- type itself, and we treat Default_Component_Value similarly for
6290 -- the sake of uniformity).
6292 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
6293 declare
6294 Nam : Name_Id;
6295 Exp : Node_Id;
6296 Typ : Entity_Id;
6298 begin
6299 if Is_Scalar_Type (E) then
6300 Nam := Name_Default_Value;
6301 Typ := E;
6302 Exp := Default_Aspect_Value (Typ);
6303 else
6304 Nam := Name_Default_Component_Value;
6305 Typ := Component_Type (E);
6306 Exp := Default_Aspect_Component_Value (E);
6307 end if;
6309 Analyze_And_Resolve (Exp, Typ);
6311 if Etype (Exp) /= Any_Type then
6312 if not Is_OK_Static_Expression (Exp) then
6313 Error_Msg_Name_1 := Nam;
6314 Flag_Non_Static_Expr
6315 ("aspect% requires static expression", Exp);
6316 end if;
6317 end if;
6318 end;
6319 end if;
6321 -- End of freeze processing for type entities
6322 end if;
6324 -- Here is where we logically freeze the current entity. If it has a
6325 -- freeze node, then this is the point at which the freeze node is
6326 -- linked into the result list.
6328 if Has_Delayed_Freeze (E) then
6330 -- If a freeze node is already allocated, use it, otherwise allocate
6331 -- a new one. The preallocation happens in the case of anonymous base
6332 -- types, where we preallocate so that we can set First_Subtype_Link.
6333 -- Note that we reset the Sloc to the current freeze location.
6335 if Present (Freeze_Node (E)) then
6336 F_Node := Freeze_Node (E);
6337 Set_Sloc (F_Node, Loc);
6339 else
6340 F_Node := New_Freeze_Node;
6341 Set_Freeze_Node (E, F_Node);
6342 Set_Access_Types_To_Process (F_Node, No_Elist);
6343 Set_TSS_Elist (F_Node, No_Elist);
6344 Set_Actions (F_Node, No_List);
6345 end if;
6347 Set_Entity (F_Node, E);
6348 Add_To_Result (F_Node);
6350 -- A final pass over record types with discriminants. If the type
6351 -- has an incomplete declaration, there may be constrained access
6352 -- subtypes declared elsewhere, which do not depend on the discrimi-
6353 -- nants of the type, and which are used as component types (i.e.
6354 -- the full view is a recursive type). The designated types of these
6355 -- subtypes can only be elaborated after the type itself, and they
6356 -- need an itype reference.
6358 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
6359 declare
6360 Comp : Entity_Id;
6361 IR : Node_Id;
6362 Typ : Entity_Id;
6364 begin
6365 Comp := First_Component (E);
6366 while Present (Comp) loop
6367 Typ := Etype (Comp);
6369 if Ekind (Comp) = E_Component
6370 and then Is_Access_Type (Typ)
6371 and then Scope (Typ) /= E
6372 and then Base_Type (Designated_Type (Typ)) = E
6373 and then Is_Itype (Designated_Type (Typ))
6374 then
6375 IR := Make_Itype_Reference (Sloc (Comp));
6376 Set_Itype (IR, Designated_Type (Typ));
6377 Append (IR, Result);
6378 end if;
6380 Next_Component (Comp);
6381 end loop;
6382 end;
6383 end if;
6384 end if;
6386 -- When a type is frozen, the first subtype of the type is frozen as
6387 -- well (RM 13.14(15)). This has to be done after freezing the type,
6388 -- since obviously the first subtype depends on its own base type.
6390 if Is_Type (E) then
6391 Freeze_And_Append (First_Subtype (E), N, Result);
6393 -- If we just froze a tagged non-class wide record, then freeze the
6394 -- corresponding class-wide type. This must be done after the tagged
6395 -- type itself is frozen, because the class-wide type refers to the
6396 -- tagged type which generates the class.
6398 if Is_Tagged_Type (E)
6399 and then not Is_Class_Wide_Type (E)
6400 and then Present (Class_Wide_Type (E))
6401 then
6402 Freeze_And_Append (Class_Wide_Type (E), N, Result);
6403 end if;
6404 end if;
6406 Check_Debug_Info_Needed (E);
6408 -- Special handling for subprograms
6410 if Is_Subprogram (E) then
6412 -- If subprogram has address clause then reset Is_Public flag, since
6413 -- we do not want the backend to generate external references.
6415 if Present (Address_Clause (E))
6416 and then not Is_Library_Level_Entity (E)
6417 then
6418 Set_Is_Public (E, False);
6419 end if;
6420 end if;
6422 Ghost_Mode := Save_Ghost_Mode;
6423 return Result;
6424 end Freeze_Entity;
6426 -----------------------------
6427 -- Freeze_Enumeration_Type --
6428 -----------------------------
6430 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6431 begin
6432 -- By default, if no size clause is present, an enumeration type with
6433 -- Convention C is assumed to interface to a C enum, and has integer
6434 -- size. This applies to types. For subtypes, verify that its base
6435 -- type has no size clause either. Treat other foreign conventions
6436 -- in the same way, and also make sure alignment is set right.
6438 if Has_Foreign_Convention (Typ)
6439 and then not Has_Size_Clause (Typ)
6440 and then not Has_Size_Clause (Base_Type (Typ))
6441 and then Esize (Typ) < Standard_Integer_Size
6443 -- Don't do this if Short_Enums on target
6445 and then not Target_Short_Enums
6446 then
6447 Init_Esize (Typ, Standard_Integer_Size);
6448 Set_Alignment (Typ, Alignment (Standard_Integer));
6450 -- Normal Ada case or size clause present or not Long_C_Enums on target
6452 else
6453 -- If the enumeration type interfaces to C, and it has a size clause
6454 -- that specifies less than int size, it warrants a warning. The
6455 -- user may intend the C type to be an enum or a char, so this is
6456 -- not by itself an error that the Ada compiler can detect, but it
6457 -- it is a worth a heads-up. For Boolean and Character types we
6458 -- assume that the programmer has the proper C type in mind.
6460 if Convention (Typ) = Convention_C
6461 and then Has_Size_Clause (Typ)
6462 and then Esize (Typ) /= Esize (Standard_Integer)
6463 and then not Is_Boolean_Type (Typ)
6464 and then not Is_Character_Type (Typ)
6466 -- Don't do this if Short_Enums on target
6468 and then not Target_Short_Enums
6469 then
6470 Error_Msg_N
6471 ("C enum types have the size of a C int??", Size_Clause (Typ));
6472 end if;
6474 Adjust_Esize_For_Alignment (Typ);
6475 end if;
6476 end Freeze_Enumeration_Type;
6478 -----------------------
6479 -- Freeze_Expression --
6480 -----------------------
6482 procedure Freeze_Expression (N : Node_Id) is
6483 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6484 Typ : Entity_Id;
6485 Nam : Entity_Id;
6486 Desig_Typ : Entity_Id;
6487 P : Node_Id;
6488 Parent_P : Node_Id;
6490 Freeze_Outside : Boolean := False;
6491 -- This flag is set true if the entity must be frozen outside the
6492 -- current subprogram. This happens in the case of expander generated
6493 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6494 -- not freeze all entities like other bodies, but which nevertheless
6495 -- may reference entities that have to be frozen before the body and
6496 -- obviously cannot be frozen inside the body.
6498 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6499 -- If the expression is an array aggregate, the type of the component
6500 -- expressions is also frozen. If the component type is an access type
6501 -- and the expressions include allocators, the designed type is frozen
6502 -- as well.
6504 function In_Expanded_Body (N : Node_Id) return Boolean;
6505 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6506 -- it is the handled statement sequence of an expander-generated
6507 -- subprogram (init proc, stream subprogram, or renaming as body).
6508 -- If so, this is not a freezing context.
6510 -----------------------------------------
6511 -- Find_Aggregate_Component_Desig_Type --
6512 -----------------------------------------
6514 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6515 Assoc : Node_Id;
6516 Exp : Node_Id;
6518 begin
6519 if Present (Expressions (N)) then
6520 Exp := First (Expressions (N));
6521 while Present (Exp) loop
6522 if Nkind (Exp) = N_Allocator then
6523 return Designated_Type (Component_Type (Etype (N)));
6524 end if;
6526 Next (Exp);
6527 end loop;
6528 end if;
6530 if Present (Component_Associations (N)) then
6531 Assoc := First (Component_Associations (N));
6532 while Present (Assoc) loop
6533 if Nkind (Expression (Assoc)) = N_Allocator then
6534 return Designated_Type (Component_Type (Etype (N)));
6535 end if;
6537 Next (Assoc);
6538 end loop;
6539 end if;
6541 return Empty;
6542 end Find_Aggregate_Component_Desig_Type;
6544 ----------------------
6545 -- In_Expanded_Body --
6546 ----------------------
6548 function In_Expanded_Body (N : Node_Id) return Boolean is
6549 P : Node_Id;
6550 Id : Entity_Id;
6552 begin
6553 if Nkind (N) = N_Subprogram_Body then
6554 P := N;
6555 else
6556 P := Parent (N);
6557 end if;
6559 if Nkind (P) /= N_Subprogram_Body then
6560 return False;
6562 else
6563 Id := Defining_Unit_Name (Specification (P));
6565 -- The following are expander-created bodies, or bodies that
6566 -- are not freeze points.
6568 if Nkind (Id) = N_Defining_Identifier
6569 and then (Is_Init_Proc (Id)
6570 or else Is_TSS (Id, TSS_Stream_Input)
6571 or else Is_TSS (Id, TSS_Stream_Output)
6572 or else Is_TSS (Id, TSS_Stream_Read)
6573 or else Is_TSS (Id, TSS_Stream_Write)
6574 or else Nkind_In (Original_Node (P),
6575 N_Subprogram_Renaming_Declaration,
6576 N_Expression_Function))
6577 then
6578 return True;
6579 else
6580 return False;
6581 end if;
6582 end if;
6583 end In_Expanded_Body;
6585 -- Start of processing for Freeze_Expression
6587 begin
6588 -- Immediate return if freezing is inhibited. This flag is set by the
6589 -- analyzer to stop freezing on generated expressions that would cause
6590 -- freezing if they were in the source program, but which are not
6591 -- supposed to freeze, since they are created.
6593 if Must_Not_Freeze (N) then
6594 return;
6595 end if;
6597 -- If expression is non-static, then it does not freeze in a default
6598 -- expression, see section "Handling of Default Expressions" in the
6599 -- spec of package Sem for further details. Note that we have to make
6600 -- sure that we actually have a real expression (if we have a subtype
6601 -- indication, we can't test Is_OK_Static_Expression). However, we
6602 -- exclude the case of the prefix of an attribute of a static scalar
6603 -- subtype from this early return, because static subtype attributes
6604 -- should always cause freezing, even in default expressions, but
6605 -- the attribute may not have been marked as static yet (because in
6606 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
6607 -- Freeze_Expression on the prefix).
6609 if In_Spec_Exp
6610 and then Nkind (N) in N_Subexpr
6611 and then not Is_OK_Static_Expression (N)
6612 and then (Nkind (Parent (N)) /= N_Attribute_Reference
6613 or else not (Is_Entity_Name (N)
6614 and then Is_Type (Entity (N))
6615 and then Is_OK_Static_Subtype (Entity (N))))
6616 then
6617 return;
6618 end if;
6620 -- Freeze type of expression if not frozen already
6622 Typ := Empty;
6624 if Nkind (N) in N_Has_Etype then
6625 if not Is_Frozen (Etype (N)) then
6626 Typ := Etype (N);
6628 -- Base type may be an derived numeric type that is frozen at
6629 -- the point of declaration, but first_subtype is still unfrozen.
6631 elsif not Is_Frozen (First_Subtype (Etype (N))) then
6632 Typ := First_Subtype (Etype (N));
6633 end if;
6634 end if;
6636 -- For entity name, freeze entity if not frozen already. A special
6637 -- exception occurs for an identifier that did not come from source.
6638 -- We don't let such identifiers freeze a non-internal entity, i.e.
6639 -- an entity that did come from source, since such an identifier was
6640 -- generated by the expander, and cannot have any semantic effect on
6641 -- the freezing semantics. For example, this stops the parameter of
6642 -- an initialization procedure from freezing the variable.
6644 if Is_Entity_Name (N)
6645 and then not Is_Frozen (Entity (N))
6646 and then (Nkind (N) /= N_Identifier
6647 or else Comes_From_Source (N)
6648 or else not Comes_From_Source (Entity (N)))
6649 then
6650 Nam := Entity (N);
6652 if Present (Nam) and then Ekind (Nam) = E_Function then
6653 Check_Expression_Function (N, Nam);
6654 end if;
6656 else
6657 Nam := Empty;
6658 end if;
6660 -- For an allocator freeze designated type if not frozen already
6662 -- For an aggregate whose component type is an access type, freeze the
6663 -- designated type now, so that its freeze does not appear within the
6664 -- loop that might be created in the expansion of the aggregate. If the
6665 -- designated type is a private type without full view, the expression
6666 -- cannot contain an allocator, so the type is not frozen.
6668 -- For a function, we freeze the entity when the subprogram declaration
6669 -- is frozen, but a function call may appear in an initialization proc.
6670 -- before the declaration is frozen. We need to generate the extra
6671 -- formals, if any, to ensure that the expansion of the call includes
6672 -- the proper actuals. This only applies to Ada subprograms, not to
6673 -- imported ones.
6675 Desig_Typ := Empty;
6677 case Nkind (N) is
6678 when N_Allocator =>
6679 Desig_Typ := Designated_Type (Etype (N));
6681 when N_Aggregate =>
6682 if Is_Array_Type (Etype (N))
6683 and then Is_Access_Type (Component_Type (Etype (N)))
6684 then
6686 -- Check whether aggregate includes allocators.
6688 Desig_Typ := Find_Aggregate_Component_Desig_Type;
6689 end if;
6691 when N_Selected_Component |
6692 N_Indexed_Component |
6693 N_Slice =>
6695 if Is_Access_Type (Etype (Prefix (N))) then
6696 Desig_Typ := Designated_Type (Etype (Prefix (N)));
6697 end if;
6699 when N_Identifier =>
6700 if Present (Nam)
6701 and then Ekind (Nam) = E_Function
6702 and then Nkind (Parent (N)) = N_Function_Call
6703 and then Convention (Nam) = Convention_Ada
6704 then
6705 Create_Extra_Formals (Nam);
6706 end if;
6708 when others =>
6709 null;
6710 end case;
6712 if Desig_Typ /= Empty
6713 and then (Is_Frozen (Desig_Typ)
6714 or else (not Is_Fully_Defined (Desig_Typ)))
6715 then
6716 Desig_Typ := Empty;
6717 end if;
6719 -- All done if nothing needs freezing
6721 if No (Typ)
6722 and then No (Nam)
6723 and then No (Desig_Typ)
6724 then
6725 return;
6726 end if;
6728 -- Examine the enclosing context by climbing the parent chain. The
6729 -- traversal serves two purposes - to detect scenarios where freezeing
6730 -- is not needed and to find the proper insertion point for the freeze
6731 -- nodes. Although somewhat similar to Insert_Actions, this traversal
6732 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
6733 -- the tree may result in types being frozen too early.
6735 P := N;
6736 loop
6737 Parent_P := Parent (P);
6739 -- If we don't have a parent, then we are not in a well-formed tree.
6740 -- This is an unusual case, but there are some legitimate situations
6741 -- in which this occurs, notably when the expressions in the range of
6742 -- a type declaration are resolved. We simply ignore the freeze
6743 -- request in this case. Is this right ???
6745 if No (Parent_P) then
6746 return;
6747 end if;
6749 -- See if we have got to an appropriate point in the tree
6751 case Nkind (Parent_P) is
6753 -- A special test for the exception of (RM 13.14(8)) for the case
6754 -- of per-object expressions (RM 3.8(18)) occurring in component
6755 -- definition or a discrete subtype definition. Note that we test
6756 -- for a component declaration which includes both cases we are
6757 -- interested in, and furthermore the tree does not have explicit
6758 -- nodes for either of these two constructs.
6760 when N_Component_Declaration =>
6762 -- The case we want to test for here is an identifier that is
6763 -- a per-object expression, this is either a discriminant that
6764 -- appears in a context other than the component declaration
6765 -- or it is a reference to the type of the enclosing construct.
6767 -- For either of these cases, we skip the freezing
6769 if not In_Spec_Expression
6770 and then Nkind (N) = N_Identifier
6771 and then (Present (Entity (N)))
6772 then
6773 -- We recognize the discriminant case by just looking for
6774 -- a reference to a discriminant. It can only be one for
6775 -- the enclosing construct. Skip freezing in this case.
6777 if Ekind (Entity (N)) = E_Discriminant then
6778 return;
6780 -- For the case of a reference to the enclosing record,
6781 -- (or task or protected type), we look for a type that
6782 -- matches the current scope.
6784 elsif Entity (N) = Current_Scope then
6785 return;
6786 end if;
6787 end if;
6789 -- If we have an enumeration literal that appears as the choice in
6790 -- the aggregate of an enumeration representation clause, then
6791 -- freezing does not occur (RM 13.14(10)).
6793 when N_Enumeration_Representation_Clause =>
6795 -- The case we are looking for is an enumeration literal
6797 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
6798 and then Is_Enumeration_Type (Etype (N))
6799 then
6800 -- If enumeration literal appears directly as the choice,
6801 -- do not freeze (this is the normal non-overloaded case)
6803 if Nkind (Parent (N)) = N_Component_Association
6804 and then First (Choices (Parent (N))) = N
6805 then
6806 return;
6808 -- If enumeration literal appears as the name of function
6809 -- which is the choice, then also do not freeze. This
6810 -- happens in the overloaded literal case, where the
6811 -- enumeration literal is temporarily changed to a function
6812 -- call for overloading analysis purposes.
6814 elsif Nkind (Parent (N)) = N_Function_Call
6815 and then
6816 Nkind (Parent (Parent (N))) = N_Component_Association
6817 and then
6818 First (Choices (Parent (Parent (N)))) = Parent (N)
6819 then
6820 return;
6821 end if;
6822 end if;
6824 -- Normally if the parent is a handled sequence of statements,
6825 -- then the current node must be a statement, and that is an
6826 -- appropriate place to insert a freeze node.
6828 when N_Handled_Sequence_Of_Statements =>
6830 -- An exception occurs when the sequence of statements is for
6831 -- an expander generated body that did not do the usual freeze
6832 -- all operation. In this case we usually want to freeze
6833 -- outside this body, not inside it, and we skip past the
6834 -- subprogram body that we are inside.
6836 if In_Expanded_Body (Parent_P) then
6837 declare
6838 Subp : constant Node_Id := Parent (Parent_P);
6839 Spec : Entity_Id;
6841 begin
6842 -- Freeze the entity only when it is declared inside the
6843 -- body of the expander generated procedure. This case
6844 -- is recognized by the scope of the entity or its type,
6845 -- which is either the spec for some enclosing body, or
6846 -- (in the case of init_procs, for which there are no
6847 -- separate specs) the current scope.
6849 if Nkind (Subp) = N_Subprogram_Body then
6850 Spec := Corresponding_Spec (Subp);
6852 if (Present (Typ) and then Scope (Typ) = Spec)
6853 or else
6854 (Present (Nam) and then Scope (Nam) = Spec)
6855 then
6856 exit;
6858 elsif Present (Typ)
6859 and then Scope (Typ) = Current_Scope
6860 and then Defining_Entity (Subp) = Current_Scope
6861 then
6862 exit;
6863 end if;
6864 end if;
6866 -- An expression function may act as a completion of
6867 -- a function declaration. As such, it can reference
6868 -- entities declared between the two views:
6870 -- Hidden []; -- 1
6871 -- function F return ...;
6872 -- private
6873 -- function Hidden return ...;
6874 -- function F return ... is (Hidden); -- 2
6876 -- Refering to the example above, freezing the expression
6877 -- of F (2) would place Hidden's freeze node (1) in the
6878 -- wrong place. Avoid explicit freezing and let the usual
6879 -- scenarios do the job - for example, reaching the end
6880 -- of the private declarations, or a call to F.
6882 if Nkind (Original_Node (Subp)) =
6883 N_Expression_Function
6884 then
6885 null;
6887 -- Freeze outside the body
6889 else
6890 Parent_P := Parent (Parent_P);
6891 Freeze_Outside := True;
6892 end if;
6893 end;
6895 -- Here if normal case where we are in handled statement
6896 -- sequence and want to do the insertion right there.
6898 else
6899 exit;
6900 end if;
6902 -- If parent is a body or a spec or a block, then the current node
6903 -- is a statement or declaration and we can insert the freeze node
6904 -- before it.
6906 when N_Block_Statement |
6907 N_Entry_Body |
6908 N_Package_Body |
6909 N_Package_Specification |
6910 N_Protected_Body |
6911 N_Subprogram_Body |
6912 N_Task_Body => exit;
6914 -- The expander is allowed to define types in any statements list,
6915 -- so any of the following parent nodes also mark a freezing point
6916 -- if the actual node is in a list of statements or declarations.
6918 when N_Abortable_Part |
6919 N_Accept_Alternative |
6920 N_And_Then |
6921 N_Case_Statement_Alternative |
6922 N_Compilation_Unit_Aux |
6923 N_Conditional_Entry_Call |
6924 N_Delay_Alternative |
6925 N_Elsif_Part |
6926 N_Entry_Call_Alternative |
6927 N_Exception_Handler |
6928 N_Extended_Return_Statement |
6929 N_Freeze_Entity |
6930 N_If_Statement |
6931 N_Or_Else |
6932 N_Selective_Accept |
6933 N_Triggering_Alternative =>
6935 exit when Is_List_Member (P);
6937 -- Freeze nodes produced by an expression coming from the Actions
6938 -- list of a N_Expression_With_Actions node must remain within the
6939 -- Actions list. Inserting the freeze nodes further up the tree
6940 -- may lead to use before declaration issues in the case of array
6941 -- types.
6943 when N_Expression_With_Actions =>
6944 if Is_List_Member (P)
6945 and then List_Containing (P) = Actions (Parent_P)
6946 then
6947 exit;
6948 end if;
6950 -- Note: N_Loop_Statement is a special case. A type that appears
6951 -- in the source can never be frozen in a loop (this occurs only
6952 -- because of a loop expanded by the expander), so we keep on
6953 -- going. Otherwise we terminate the search. Same is true of any
6954 -- entity which comes from source. (if they have predefined type,
6955 -- that type does not appear to come from source, but the entity
6956 -- should not be frozen here).
6958 when N_Loop_Statement =>
6959 exit when not Comes_From_Source (Etype (N))
6960 and then (No (Nam) or else not Comes_From_Source (Nam));
6962 -- For all other cases, keep looking at parents
6964 when others =>
6965 null;
6966 end case;
6968 -- We fall through the case if we did not yet find the proper
6969 -- place in the free for inserting the freeze node, so climb.
6971 P := Parent_P;
6972 end loop;
6974 -- If the expression appears in a record or an initialization procedure,
6975 -- the freeze nodes are collected and attached to the current scope, to
6976 -- be inserted and analyzed on exit from the scope, to insure that
6977 -- generated entities appear in the correct scope. If the expression is
6978 -- a default for a discriminant specification, the scope is still void.
6979 -- The expression can also appear in the discriminant part of a private
6980 -- or concurrent type.
6982 -- If the expression appears in a constrained subcomponent of an
6983 -- enclosing record declaration, the freeze nodes must be attached to
6984 -- the outer record type so they can eventually be placed in the
6985 -- enclosing declaration list.
6987 -- The other case requiring this special handling is if we are in a
6988 -- default expression, since in that case we are about to freeze a
6989 -- static type, and the freeze scope needs to be the outer scope, not
6990 -- the scope of the subprogram with the default parameter.
6992 -- For default expressions and other spec expressions in generic units,
6993 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
6994 -- placing them at the proper place, after the generic unit.
6996 if (In_Spec_Exp and not Inside_A_Generic)
6997 or else Freeze_Outside
6998 or else (Is_Type (Current_Scope)
6999 and then (not Is_Concurrent_Type (Current_Scope)
7000 or else not Has_Completion (Current_Scope)))
7001 or else Ekind (Current_Scope) = E_Void
7002 then
7003 declare
7004 N : constant Node_Id := Current_Scope;
7005 Freeze_Nodes : List_Id := No_List;
7006 Pos : Int := Scope_Stack.Last;
7008 begin
7009 if Present (Desig_Typ) then
7010 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
7011 end if;
7013 if Present (Typ) then
7014 Freeze_And_Append (Typ, N, Freeze_Nodes);
7015 end if;
7017 if Present (Nam) then
7018 Freeze_And_Append (Nam, N, Freeze_Nodes);
7019 end if;
7021 -- The current scope may be that of a constrained component of
7022 -- an enclosing record declaration, or of a loop of an enclosing
7023 -- quantified expression, which is above the current scope in the
7024 -- scope stack. Indeed in the context of a quantified expression,
7025 -- a scope is created and pushed above the current scope in order
7026 -- to emulate the loop-like behavior of the quantified expression.
7027 -- If the expression is within a top-level pragma, as for a pre-
7028 -- condition on a library-level subprogram, nothing to do.
7030 if not Is_Compilation_Unit (Current_Scope)
7031 and then (Is_Record_Type (Scope (Current_Scope))
7032 or else Nkind (Parent (Current_Scope)) =
7033 N_Quantified_Expression)
7034 then
7035 Pos := Pos - 1;
7036 end if;
7038 if Is_Non_Empty_List (Freeze_Nodes) then
7039 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
7040 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
7041 Freeze_Nodes;
7042 else
7043 Append_List (Freeze_Nodes,
7044 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
7045 end if;
7046 end if;
7047 end;
7049 return;
7050 end if;
7052 -- Now we have the right place to do the freezing. First, a special
7053 -- adjustment, if we are in spec-expression analysis mode, these freeze
7054 -- actions must not be thrown away (normally all inserted actions are
7055 -- thrown away in this mode. However, the freeze actions are from static
7056 -- expressions and one of the important reasons we are doing this
7057 -- special analysis is to get these freeze actions. Therefore we turn
7058 -- off the In_Spec_Expression mode to propagate these freeze actions.
7059 -- This also means they get properly analyzed and expanded.
7061 In_Spec_Expression := False;
7063 -- Freeze the designated type of an allocator (RM 13.14(13))
7065 if Present (Desig_Typ) then
7066 Freeze_Before (P, Desig_Typ);
7067 end if;
7069 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
7070 -- the enumeration representation clause exception in the loop above.
7072 if Present (Typ) then
7073 Freeze_Before (P, Typ);
7074 end if;
7076 -- Freeze name if one is present (RM 13.14(11))
7078 if Present (Nam) then
7079 Freeze_Before (P, Nam);
7080 end if;
7082 -- Restore In_Spec_Expression flag
7084 In_Spec_Expression := In_Spec_Exp;
7085 end Freeze_Expression;
7087 -----------------------------
7088 -- Freeze_Fixed_Point_Type --
7089 -----------------------------
7091 -- Certain fixed-point types and subtypes, including implicit base types
7092 -- and declared first subtypes, have not yet set up a range. This is
7093 -- because the range cannot be set until the Small and Size values are
7094 -- known, and these are not known till the type is frozen.
7096 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
7097 -- whose bounds are unanalyzed real literals. This routine will recognize
7098 -- this case, and transform this range node into a properly typed range
7099 -- with properly analyzed and resolved values.
7101 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
7102 Rng : constant Node_Id := Scalar_Range (Typ);
7103 Lo : constant Node_Id := Low_Bound (Rng);
7104 Hi : constant Node_Id := High_Bound (Rng);
7105 Btyp : constant Entity_Id := Base_Type (Typ);
7106 Brng : constant Node_Id := Scalar_Range (Btyp);
7107 BLo : constant Node_Id := Low_Bound (Brng);
7108 BHi : constant Node_Id := High_Bound (Brng);
7109 Small : constant Ureal := Small_Value (Typ);
7110 Loval : Ureal;
7111 Hival : Ureal;
7112 Atype : Entity_Id;
7114 Orig_Lo : Ureal;
7115 Orig_Hi : Ureal;
7116 -- Save original bounds (for shaving tests)
7118 Actual_Size : Nat;
7119 -- Actual size chosen
7121 function Fsize (Lov, Hiv : Ureal) return Nat;
7122 -- Returns size of type with given bounds. Also leaves these
7123 -- bounds set as the current bounds of the Typ.
7125 -----------
7126 -- Fsize --
7127 -----------
7129 function Fsize (Lov, Hiv : Ureal) return Nat is
7130 begin
7131 Set_Realval (Lo, Lov);
7132 Set_Realval (Hi, Hiv);
7133 return Minimum_Size (Typ);
7134 end Fsize;
7136 -- Start of processing for Freeze_Fixed_Point_Type
7138 begin
7139 -- If Esize of a subtype has not previously been set, set it now
7141 if Unknown_Esize (Typ) then
7142 Atype := Ancestor_Subtype (Typ);
7144 if Present (Atype) then
7145 Set_Esize (Typ, Esize (Atype));
7146 else
7147 Set_Esize (Typ, Esize (Base_Type (Typ)));
7148 end if;
7149 end if;
7151 -- Immediate return if the range is already analyzed. This means that
7152 -- the range is already set, and does not need to be computed by this
7153 -- routine.
7155 if Analyzed (Rng) then
7156 return;
7157 end if;
7159 -- Immediate return if either of the bounds raises Constraint_Error
7161 if Raises_Constraint_Error (Lo)
7162 or else Raises_Constraint_Error (Hi)
7163 then
7164 return;
7165 end if;
7167 Loval := Realval (Lo);
7168 Hival := Realval (Hi);
7170 Orig_Lo := Loval;
7171 Orig_Hi := Hival;
7173 -- Ordinary fixed-point case
7175 if Is_Ordinary_Fixed_Point_Type (Typ) then
7177 -- For the ordinary fixed-point case, we are allowed to fudge the
7178 -- end-points up or down by small. Generally we prefer to fudge up,
7179 -- i.e. widen the bounds for non-model numbers so that the end points
7180 -- are included. However there are cases in which this cannot be
7181 -- done, and indeed cases in which we may need to narrow the bounds.
7182 -- The following circuit makes the decision.
7184 -- Note: our terminology here is that Incl_EP means that the bounds
7185 -- are widened by Small if necessary to include the end points, and
7186 -- Excl_EP means that the bounds are narrowed by Small to exclude the
7187 -- end-points if this reduces the size.
7189 -- Note that in the Incl case, all we care about is including the
7190 -- end-points. In the Excl case, we want to narrow the bounds as
7191 -- much as permitted by the RM, to give the smallest possible size.
7193 Fudge : declare
7194 Loval_Incl_EP : Ureal;
7195 Hival_Incl_EP : Ureal;
7197 Loval_Excl_EP : Ureal;
7198 Hival_Excl_EP : Ureal;
7200 Size_Incl_EP : Nat;
7201 Size_Excl_EP : Nat;
7203 Model_Num : Ureal;
7204 First_Subt : Entity_Id;
7205 Actual_Lo : Ureal;
7206 Actual_Hi : Ureal;
7208 begin
7209 -- First step. Base types are required to be symmetrical. Right
7210 -- now, the base type range is a copy of the first subtype range.
7211 -- This will be corrected before we are done, but right away we
7212 -- need to deal with the case where both bounds are non-negative.
7213 -- In this case, we set the low bound to the negative of the high
7214 -- bound, to make sure that the size is computed to include the
7215 -- required sign. Note that we do not need to worry about the
7216 -- case of both bounds negative, because the sign will be dealt
7217 -- with anyway. Furthermore we can't just go making such a bound
7218 -- symmetrical, since in a twos-complement system, there is an
7219 -- extra negative value which could not be accommodated on the
7220 -- positive side.
7222 if Typ = Btyp
7223 and then not UR_Is_Negative (Loval)
7224 and then Hival > Loval
7225 then
7226 Loval := -Hival;
7227 Set_Realval (Lo, Loval);
7228 end if;
7230 -- Compute the fudged bounds. If the number is a model number,
7231 -- then we do nothing to include it, but we are allowed to backoff
7232 -- to the next adjacent model number when we exclude it. If it is
7233 -- not a model number then we straddle the two values with the
7234 -- model numbers on either side.
7236 Model_Num := UR_Trunc (Loval / Small) * Small;
7238 if Loval = Model_Num then
7239 Loval_Incl_EP := Model_Num;
7240 else
7241 Loval_Incl_EP := Model_Num - Small;
7242 end if;
7244 -- The low value excluding the end point is Small greater, but
7245 -- we do not do this exclusion if the low value is positive,
7246 -- since it can't help the size and could actually hurt by
7247 -- crossing the high bound.
7249 if UR_Is_Negative (Loval_Incl_EP) then
7250 Loval_Excl_EP := Loval_Incl_EP + Small;
7252 -- If the value went from negative to zero, then we have the
7253 -- case where Loval_Incl_EP is the model number just below
7254 -- zero, so we want to stick to the negative value for the
7255 -- base type to maintain the condition that the size will
7256 -- include signed values.
7258 if Typ = Btyp
7259 and then UR_Is_Zero (Loval_Excl_EP)
7260 then
7261 Loval_Excl_EP := Loval_Incl_EP;
7262 end if;
7264 else
7265 Loval_Excl_EP := Loval_Incl_EP;
7266 end if;
7268 -- Similar processing for upper bound and high value
7270 Model_Num := UR_Trunc (Hival / Small) * Small;
7272 if Hival = Model_Num then
7273 Hival_Incl_EP := Model_Num;
7274 else
7275 Hival_Incl_EP := Model_Num + Small;
7276 end if;
7278 if UR_Is_Positive (Hival_Incl_EP) then
7279 Hival_Excl_EP := Hival_Incl_EP - Small;
7280 else
7281 Hival_Excl_EP := Hival_Incl_EP;
7282 end if;
7284 -- One further adjustment is needed. In the case of subtypes, we
7285 -- cannot go outside the range of the base type, or we get
7286 -- peculiarities, and the base type range is already set. This
7287 -- only applies to the Incl values, since clearly the Excl values
7288 -- are already as restricted as they are allowed to be.
7290 if Typ /= Btyp then
7291 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
7292 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
7293 end if;
7295 -- Get size including and excluding end points
7297 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
7298 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
7300 -- No need to exclude end-points if it does not reduce size
7302 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
7303 Loval_Excl_EP := Loval_Incl_EP;
7304 end if;
7306 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
7307 Hival_Excl_EP := Hival_Incl_EP;
7308 end if;
7310 -- Now we set the actual size to be used. We want to use the
7311 -- bounds fudged up to include the end-points but only if this
7312 -- can be done without violating a specifically given size
7313 -- size clause or causing an unacceptable increase in size.
7315 -- Case of size clause given
7317 if Has_Size_Clause (Typ) then
7319 -- Use the inclusive size only if it is consistent with
7320 -- the explicitly specified size.
7322 if Size_Incl_EP <= RM_Size (Typ) then
7323 Actual_Lo := Loval_Incl_EP;
7324 Actual_Hi := Hival_Incl_EP;
7325 Actual_Size := Size_Incl_EP;
7327 -- If the inclusive size is too large, we try excluding
7328 -- the end-points (will be caught later if does not work).
7330 else
7331 Actual_Lo := Loval_Excl_EP;
7332 Actual_Hi := Hival_Excl_EP;
7333 Actual_Size := Size_Excl_EP;
7334 end if;
7336 -- Case of size clause not given
7338 else
7339 -- If we have a base type whose corresponding first subtype
7340 -- has an explicit size that is large enough to include our
7341 -- end-points, then do so. There is no point in working hard
7342 -- to get a base type whose size is smaller than the specified
7343 -- size of the first subtype.
7345 First_Subt := First_Subtype (Typ);
7347 if Has_Size_Clause (First_Subt)
7348 and then Size_Incl_EP <= Esize (First_Subt)
7349 then
7350 Actual_Size := Size_Incl_EP;
7351 Actual_Lo := Loval_Incl_EP;
7352 Actual_Hi := Hival_Incl_EP;
7354 -- If excluding the end-points makes the size smaller and
7355 -- results in a size of 8,16,32,64, then we take the smaller
7356 -- size. For the 64 case, this is compulsory. For the other
7357 -- cases, it seems reasonable. We like to include end points
7358 -- if we can, but not at the expense of moving to the next
7359 -- natural boundary of size.
7361 elsif Size_Incl_EP /= Size_Excl_EP
7362 and then Addressable (Size_Excl_EP)
7363 then
7364 Actual_Size := Size_Excl_EP;
7365 Actual_Lo := Loval_Excl_EP;
7366 Actual_Hi := Hival_Excl_EP;
7368 -- Otherwise we can definitely include the end points
7370 else
7371 Actual_Size := Size_Incl_EP;
7372 Actual_Lo := Loval_Incl_EP;
7373 Actual_Hi := Hival_Incl_EP;
7374 end if;
7376 -- One pathological case: normally we never fudge a low bound
7377 -- down, since it would seem to increase the size (if it has
7378 -- any effect), but for ranges containing single value, or no
7379 -- values, the high bound can be small too large. Consider:
7381 -- type t is delta 2.0**(-14)
7382 -- range 131072.0 .. 0;
7384 -- That lower bound is *just* outside the range of 32 bits, and
7385 -- does need fudging down in this case. Note that the bounds
7386 -- will always have crossed here, since the high bound will be
7387 -- fudged down if necessary, as in the case of:
7389 -- type t is delta 2.0**(-14)
7390 -- range 131072.0 .. 131072.0;
7392 -- So we detect the situation by looking for crossed bounds,
7393 -- and if the bounds are crossed, and the low bound is greater
7394 -- than zero, we will always back it off by small, since this
7395 -- is completely harmless.
7397 if Actual_Lo > Actual_Hi then
7398 if UR_Is_Positive (Actual_Lo) then
7399 Actual_Lo := Loval_Incl_EP - Small;
7400 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7402 -- And of course, we need to do exactly the same parallel
7403 -- fudge for flat ranges in the negative region.
7405 elsif UR_Is_Negative (Actual_Hi) then
7406 Actual_Hi := Hival_Incl_EP + Small;
7407 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7408 end if;
7409 end if;
7410 end if;
7412 Set_Realval (Lo, Actual_Lo);
7413 Set_Realval (Hi, Actual_Hi);
7414 end Fudge;
7416 -- For the decimal case, none of this fudging is required, since there
7417 -- are no end-point problems in the decimal case (the end-points are
7418 -- always included).
7420 else
7421 Actual_Size := Fsize (Loval, Hival);
7422 end if;
7424 -- At this stage, the actual size has been calculated and the proper
7425 -- required bounds are stored in the low and high bounds.
7427 if Actual_Size > 64 then
7428 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7429 Error_Msg_N
7430 ("size required (^) for type& too large, maximum allowed is 64",
7431 Typ);
7432 Actual_Size := 64;
7433 end if;
7435 -- Check size against explicit given size
7437 if Has_Size_Clause (Typ) then
7438 if Actual_Size > RM_Size (Typ) then
7439 Error_Msg_Uint_1 := RM_Size (Typ);
7440 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7441 Error_Msg_NE
7442 ("size given (^) for type& too small, minimum allowed is ^",
7443 Size_Clause (Typ), Typ);
7445 else
7446 Actual_Size := UI_To_Int (Esize (Typ));
7447 end if;
7449 -- Increase size to next natural boundary if no size clause given
7451 else
7452 if Actual_Size <= 8 then
7453 Actual_Size := 8;
7454 elsif Actual_Size <= 16 then
7455 Actual_Size := 16;
7456 elsif Actual_Size <= 32 then
7457 Actual_Size := 32;
7458 else
7459 Actual_Size := 64;
7460 end if;
7462 Init_Esize (Typ, Actual_Size);
7463 Adjust_Esize_For_Alignment (Typ);
7464 end if;
7466 -- If we have a base type, then expand the bounds so that they extend to
7467 -- the full width of the allocated size in bits, to avoid junk range
7468 -- checks on intermediate computations.
7470 if Base_Type (Typ) = Typ then
7471 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7472 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7473 end if;
7475 -- Final step is to reanalyze the bounds using the proper type
7476 -- and set the Corresponding_Integer_Value fields of the literals.
7478 Set_Etype (Lo, Empty);
7479 Set_Analyzed (Lo, False);
7480 Analyze (Lo);
7482 -- Resolve with universal fixed if the base type, and the base type if
7483 -- it is a subtype. Note we can't resolve the base type with itself,
7484 -- that would be a reference before definition.
7486 if Typ = Btyp then
7487 Resolve (Lo, Universal_Fixed);
7488 else
7489 Resolve (Lo, Btyp);
7490 end if;
7492 -- Set corresponding integer value for bound
7494 Set_Corresponding_Integer_Value
7495 (Lo, UR_To_Uint (Realval (Lo) / Small));
7497 -- Similar processing for high bound
7499 Set_Etype (Hi, Empty);
7500 Set_Analyzed (Hi, False);
7501 Analyze (Hi);
7503 if Typ = Btyp then
7504 Resolve (Hi, Universal_Fixed);
7505 else
7506 Resolve (Hi, Btyp);
7507 end if;
7509 Set_Corresponding_Integer_Value
7510 (Hi, UR_To_Uint (Realval (Hi) / Small));
7512 -- Set type of range to correspond to bounds
7514 Set_Etype (Rng, Etype (Lo));
7516 -- Set Esize to calculated size if not set already
7518 if Unknown_Esize (Typ) then
7519 Init_Esize (Typ, Actual_Size);
7520 end if;
7522 -- Set RM_Size if not already set. If already set, check value
7524 declare
7525 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7527 begin
7528 if RM_Size (Typ) /= Uint_0 then
7529 if RM_Size (Typ) < Minsiz then
7530 Error_Msg_Uint_1 := RM_Size (Typ);
7531 Error_Msg_Uint_2 := Minsiz;
7532 Error_Msg_NE
7533 ("size given (^) for type& too small, minimum allowed is ^",
7534 Size_Clause (Typ), Typ);
7535 end if;
7537 else
7538 Set_RM_Size (Typ, Minsiz);
7539 end if;
7540 end;
7542 -- Check for shaving
7544 if Comes_From_Source (Typ) then
7545 if Orig_Lo < Expr_Value_R (Lo) then
7546 Error_Msg_N
7547 ("declared low bound of type & is outside type range??", Typ);
7548 Error_Msg_N
7549 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
7550 end if;
7552 if Orig_Hi > Expr_Value_R (Hi) then
7553 Error_Msg_N
7554 ("declared high bound of type & is outside type range??", Typ);
7555 Error_Msg_N
7556 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
7557 end if;
7558 end if;
7559 end Freeze_Fixed_Point_Type;
7561 ------------------
7562 -- Freeze_Itype --
7563 ------------------
7565 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7566 L : List_Id;
7568 begin
7569 Set_Has_Delayed_Freeze (T);
7570 L := Freeze_Entity (T, N);
7572 if Is_Non_Empty_List (L) then
7573 Insert_Actions (N, L);
7574 end if;
7575 end Freeze_Itype;
7577 --------------------------
7578 -- Freeze_Static_Object --
7579 --------------------------
7581 procedure Freeze_Static_Object (E : Entity_Id) is
7583 Cannot_Be_Static : exception;
7584 -- Exception raised if the type of a static object cannot be made
7585 -- static. This happens if the type depends on non-global objects.
7587 procedure Ensure_Expression_Is_SA (N : Node_Id);
7588 -- Called to ensure that an expression used as part of a type definition
7589 -- is statically allocatable, which means that the expression type is
7590 -- statically allocatable, and the expression is either static, or a
7591 -- reference to a library level constant.
7593 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7594 -- Called to mark a type as static, checking that it is possible
7595 -- to set the type as static. If it is not possible, then the
7596 -- exception Cannot_Be_Static is raised.
7598 -----------------------------
7599 -- Ensure_Expression_Is_SA --
7600 -----------------------------
7602 procedure Ensure_Expression_Is_SA (N : Node_Id) is
7603 Ent : Entity_Id;
7605 begin
7606 Ensure_Type_Is_SA (Etype (N));
7608 if Is_OK_Static_Expression (N) then
7609 return;
7611 elsif Nkind (N) = N_Identifier then
7612 Ent := Entity (N);
7614 if Present (Ent)
7615 and then Ekind (Ent) = E_Constant
7616 and then Is_Library_Level_Entity (Ent)
7617 then
7618 return;
7619 end if;
7620 end if;
7622 raise Cannot_Be_Static;
7623 end Ensure_Expression_Is_SA;
7625 -----------------------
7626 -- Ensure_Type_Is_SA --
7627 -----------------------
7629 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7630 N : Node_Id;
7631 C : Entity_Id;
7633 begin
7634 -- If type is library level, we are all set
7636 if Is_Library_Level_Entity (Typ) then
7637 return;
7638 end if;
7640 -- We are also OK if the type already marked as statically allocated,
7641 -- which means we processed it before.
7643 if Is_Statically_Allocated (Typ) then
7644 return;
7645 end if;
7647 -- Mark type as statically allocated
7649 Set_Is_Statically_Allocated (Typ);
7651 -- Check that it is safe to statically allocate this type
7653 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
7654 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
7655 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
7657 elsif Is_Array_Type (Typ) then
7658 N := First_Index (Typ);
7659 while Present (N) loop
7660 Ensure_Type_Is_SA (Etype (N));
7661 Next_Index (N);
7662 end loop;
7664 Ensure_Type_Is_SA (Component_Type (Typ));
7666 elsif Is_Access_Type (Typ) then
7667 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
7669 declare
7670 F : Entity_Id;
7671 T : constant Entity_Id := Etype (Designated_Type (Typ));
7673 begin
7674 if T /= Standard_Void_Type then
7675 Ensure_Type_Is_SA (T);
7676 end if;
7678 F := First_Formal (Designated_Type (Typ));
7679 while Present (F) loop
7680 Ensure_Type_Is_SA (Etype (F));
7681 Next_Formal (F);
7682 end loop;
7683 end;
7685 else
7686 Ensure_Type_Is_SA (Designated_Type (Typ));
7687 end if;
7689 elsif Is_Record_Type (Typ) then
7690 C := First_Entity (Typ);
7691 while Present (C) loop
7692 if Ekind (C) = E_Discriminant
7693 or else Ekind (C) = E_Component
7694 then
7695 Ensure_Type_Is_SA (Etype (C));
7697 elsif Is_Type (C) then
7698 Ensure_Type_Is_SA (C);
7699 end if;
7701 Next_Entity (C);
7702 end loop;
7704 elsif Ekind (Typ) = E_Subprogram_Type then
7705 Ensure_Type_Is_SA (Etype (Typ));
7707 C := First_Formal (Typ);
7708 while Present (C) loop
7709 Ensure_Type_Is_SA (Etype (C));
7710 Next_Formal (C);
7711 end loop;
7713 else
7714 raise Cannot_Be_Static;
7715 end if;
7716 end Ensure_Type_Is_SA;
7718 -- Start of processing for Freeze_Static_Object
7720 begin
7721 Ensure_Type_Is_SA (Etype (E));
7723 exception
7724 when Cannot_Be_Static =>
7726 -- If the object that cannot be static is imported or exported, then
7727 -- issue an error message saying that this object cannot be imported
7728 -- or exported. If it has an address clause it is an overlay in the
7729 -- current partition and the static requirement is not relevant.
7730 -- Do not issue any error message when ignoring rep clauses.
7732 if Ignore_Rep_Clauses then
7733 null;
7735 elsif Is_Imported (E) then
7736 if No (Address_Clause (E)) then
7737 Error_Msg_N
7738 ("& cannot be imported (local type is not constant)", E);
7739 end if;
7741 -- Otherwise must be exported, something is wrong if compiler
7742 -- is marking something as statically allocated which cannot be).
7744 else pragma Assert (Is_Exported (E));
7745 Error_Msg_N
7746 ("& cannot be exported (local type is not constant)", E);
7747 end if;
7748 end Freeze_Static_Object;
7750 -----------------------
7751 -- Freeze_Subprogram --
7752 -----------------------
7754 procedure Freeze_Subprogram (E : Entity_Id) is
7755 Retype : Entity_Id;
7756 F : Entity_Id;
7758 begin
7759 -- Subprogram may not have an address clause unless it is imported
7761 if Present (Address_Clause (E)) then
7762 if not Is_Imported (E) then
7763 Error_Msg_N
7764 ("address clause can only be given " &
7765 "for imported subprogram",
7766 Name (Address_Clause (E)));
7767 end if;
7768 end if;
7770 -- Reset the Pure indication on an imported subprogram unless an
7771 -- explicit Pure_Function pragma was present or the subprogram is an
7772 -- intrinsic. We do this because otherwise it is an insidious error
7773 -- to call a non-pure function from pure unit and have calls
7774 -- mysteriously optimized away. What happens here is that the Import
7775 -- can bypass the normal check to ensure that pure units call only pure
7776 -- subprograms.
7778 -- The reason for the intrinsic exception is that in general, intrinsic
7779 -- functions (such as shifts) are pure anyway. The only exceptions are
7780 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
7781 -- in any case, so no problem arises.
7783 if Is_Imported (E)
7784 and then Is_Pure (E)
7785 and then not Has_Pragma_Pure_Function (E)
7786 and then not Is_Intrinsic_Subprogram (E)
7787 then
7788 Set_Is_Pure (E, False);
7789 end if;
7791 -- We also reset the Pure indication on a subprogram with an Address
7792 -- parameter, because the parameter may be used as a pointer and the
7793 -- referenced data may change even if the address value does not.
7795 -- Note that if the programmer gave an explicit Pure_Function pragma,
7796 -- then we believe the programmer, and leave the subprogram Pure.
7797 -- We also suppress this check on run-time files.
7799 if Is_Pure (E)
7800 and then Is_Subprogram (E)
7801 and then not Has_Pragma_Pure_Function (E)
7802 and then not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
7803 then
7804 Check_Function_With_Address_Parameter (E);
7805 end if;
7807 -- For non-foreign convention subprograms, this is where we create
7808 -- the extra formals (for accessibility level and constrained bit
7809 -- information). We delay this till the freeze point precisely so
7810 -- that we know the convention.
7812 if not Has_Foreign_Convention (E) then
7813 Create_Extra_Formals (E);
7814 Set_Mechanisms (E);
7816 -- If this is convention Ada and a Valued_Procedure, that's odd
7818 if Ekind (E) = E_Procedure
7819 and then Is_Valued_Procedure (E)
7820 and then Convention (E) = Convention_Ada
7821 and then Warn_On_Export_Import
7822 then
7823 Error_Msg_N
7824 ("??Valued_Procedure has no effect for convention Ada", E);
7825 Set_Is_Valued_Procedure (E, False);
7826 end if;
7828 -- Case of foreign convention
7830 else
7831 Set_Mechanisms (E);
7833 -- For foreign conventions, warn about return of unconstrained array
7835 if Ekind (E) = E_Function then
7836 Retype := Underlying_Type (Etype (E));
7838 -- If no return type, probably some other error, e.g. a
7839 -- missing full declaration, so ignore.
7841 if No (Retype) then
7842 null;
7844 -- If the return type is generic, we have emitted a warning
7845 -- earlier on, and there is nothing else to check here. Specific
7846 -- instantiations may lead to erroneous behavior.
7848 elsif Is_Generic_Type (Etype (E)) then
7849 null;
7851 -- Display warning if returning unconstrained array
7853 elsif Is_Array_Type (Retype)
7854 and then not Is_Constrained (Retype)
7856 -- Check appropriate warning is enabled (should we check for
7857 -- Warnings (Off) on specific entities here, probably so???)
7859 and then Warn_On_Export_Import
7860 then
7861 Error_Msg_N
7862 ("?x?foreign convention function& should not return " &
7863 "unconstrained array", E);
7864 return;
7865 end if;
7866 end if;
7868 -- If any of the formals for an exported foreign convention
7869 -- subprogram have defaults, then emit an appropriate warning since
7870 -- this is odd (default cannot be used from non-Ada code)
7872 if Is_Exported (E) then
7873 F := First_Formal (E);
7874 while Present (F) loop
7875 if Warn_On_Export_Import
7876 and then Present (Default_Value (F))
7877 then
7878 Error_Msg_N
7879 ("?x?parameter cannot be defaulted in non-Ada call",
7880 Default_Value (F));
7881 end if;
7883 Next_Formal (F);
7884 end loop;
7885 end if;
7886 end if;
7888 -- Pragma Inline_Always is disallowed for dispatching subprograms
7889 -- because the address of such subprograms is saved in the dispatch
7890 -- table to support dispatching calls, and dispatching calls cannot
7891 -- be inlined. This is consistent with the restriction against using
7892 -- 'Access or 'Address on an Inline_Always subprogram.
7894 if Is_Dispatching_Operation (E)
7895 and then Has_Pragma_Inline_Always (E)
7896 then
7897 Error_Msg_N
7898 ("pragma Inline_Always not allowed for dispatching subprograms", E);
7899 end if;
7901 -- Because of the implicit representation of inherited predefined
7902 -- operators in the front-end, the overriding status of the operation
7903 -- may be affected when a full view of a type is analyzed, and this is
7904 -- not captured by the analysis of the corresponding type declaration.
7905 -- Therefore the correctness of a not-overriding indicator must be
7906 -- rechecked when the subprogram is frozen.
7908 if Nkind (E) = N_Defining_Operator_Symbol
7909 and then not Error_Posted (Parent (E))
7910 then
7911 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
7912 end if;
7914 if Modify_Tree_For_C
7915 and then Nkind (Parent (E)) = N_Function_Specification
7916 and then Is_Array_Type (Etype (E))
7917 and then Is_Constrained (Etype (E))
7918 and then not Is_Unchecked_Conversion_Instance (E)
7919 and then not Rewritten_For_C (E)
7920 then
7921 Build_Procedure_Form (Unit_Declaration_Node (E));
7922 end if;
7923 end Freeze_Subprogram;
7925 ----------------------
7926 -- Is_Fully_Defined --
7927 ----------------------
7929 function Is_Fully_Defined (T : Entity_Id) return Boolean is
7930 begin
7931 if Ekind (T) = E_Class_Wide_Type then
7932 return Is_Fully_Defined (Etype (T));
7934 elsif Is_Array_Type (T) then
7935 return Is_Fully_Defined (Component_Type (T));
7937 elsif Is_Record_Type (T)
7938 and not Is_Private_Type (T)
7939 then
7940 -- Verify that the record type has no components with private types
7941 -- without completion.
7943 declare
7944 Comp : Entity_Id;
7946 begin
7947 Comp := First_Component (T);
7948 while Present (Comp) loop
7949 if not Is_Fully_Defined (Etype (Comp)) then
7950 return False;
7951 end if;
7953 Next_Component (Comp);
7954 end loop;
7955 return True;
7956 end;
7958 -- For the designated type of an access to subprogram, all types in
7959 -- the profile must be fully defined.
7961 elsif Ekind (T) = E_Subprogram_Type then
7962 declare
7963 F : Entity_Id;
7965 begin
7966 F := First_Formal (T);
7967 while Present (F) loop
7968 if not Is_Fully_Defined (Etype (F)) then
7969 return False;
7970 end if;
7972 Next_Formal (F);
7973 end loop;
7975 return Is_Fully_Defined (Etype (T));
7976 end;
7978 else
7979 return not Is_Private_Type (T)
7980 or else Present (Full_View (Base_Type (T)));
7981 end if;
7982 end Is_Fully_Defined;
7984 ---------------------------------
7985 -- Process_Default_Expressions --
7986 ---------------------------------
7988 procedure Process_Default_Expressions
7989 (E : Entity_Id;
7990 After : in out Node_Id)
7992 Loc : constant Source_Ptr := Sloc (E);
7993 Dbody : Node_Id;
7994 Formal : Node_Id;
7995 Dcopy : Node_Id;
7996 Dnam : Entity_Id;
7998 begin
7999 Set_Default_Expressions_Processed (E);
8001 -- A subprogram instance and its associated anonymous subprogram share
8002 -- their signature. The default expression functions are defined in the
8003 -- wrapper packages for the anonymous subprogram, and should not be
8004 -- generated again for the instance.
8006 if Is_Generic_Instance (E)
8007 and then Present (Alias (E))
8008 and then Default_Expressions_Processed (Alias (E))
8009 then
8010 return;
8011 end if;
8013 Formal := First_Formal (E);
8014 while Present (Formal) loop
8015 if Present (Default_Value (Formal)) then
8017 -- We work with a copy of the default expression because we
8018 -- do not want to disturb the original, since this would mess
8019 -- up the conformance checking.
8021 Dcopy := New_Copy_Tree (Default_Value (Formal));
8023 -- The analysis of the expression may generate insert actions,
8024 -- which of course must not be executed. We wrap those actions
8025 -- in a procedure that is not called, and later on eliminated.
8026 -- The following cases have no side-effects, and are analyzed
8027 -- directly.
8029 if Nkind (Dcopy) = N_Identifier
8030 or else Nkind_In (Dcopy, N_Expanded_Name,
8031 N_Integer_Literal,
8032 N_Character_Literal,
8033 N_String_Literal,
8034 N_Real_Literal)
8035 or else (Nkind (Dcopy) = N_Attribute_Reference
8036 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
8037 or else Known_Null (Dcopy)
8038 then
8039 -- If there is no default function, we must still do a full
8040 -- analyze call on the default value, to ensure that all error
8041 -- checks are performed, e.g. those associated with static
8042 -- evaluation. Note: this branch will always be taken if the
8043 -- analyzer is turned off (but we still need the error checks).
8045 -- Note: the setting of parent here is to meet the requirement
8046 -- that we can only analyze the expression while attached to
8047 -- the tree. Really the requirement is that the parent chain
8048 -- be set, we don't actually need to be in the tree.
8050 Set_Parent (Dcopy, Declaration_Node (Formal));
8051 Analyze (Dcopy);
8053 -- Default expressions are resolved with their own type if the
8054 -- context is generic, to avoid anomalies with private types.
8056 if Ekind (Scope (E)) = E_Generic_Package then
8057 Resolve (Dcopy);
8058 else
8059 Resolve (Dcopy, Etype (Formal));
8060 end if;
8062 -- If that resolved expression will raise constraint error,
8063 -- then flag the default value as raising constraint error.
8064 -- This allows a proper error message on the calls.
8066 if Raises_Constraint_Error (Dcopy) then
8067 Set_Raises_Constraint_Error (Default_Value (Formal));
8068 end if;
8070 -- If the default is a parameterless call, we use the name of
8071 -- the called function directly, and there is no body to build.
8073 elsif Nkind (Dcopy) = N_Function_Call
8074 and then No (Parameter_Associations (Dcopy))
8075 then
8076 null;
8078 -- Else construct and analyze the body of a wrapper procedure
8079 -- that contains an object declaration to hold the expression.
8080 -- Given that this is done only to complete the analysis, it
8081 -- simpler to build a procedure than a function which might
8082 -- involve secondary stack expansion.
8084 else
8085 Dnam := Make_Temporary (Loc, 'D');
8087 Dbody :=
8088 Make_Subprogram_Body (Loc,
8089 Specification =>
8090 Make_Procedure_Specification (Loc,
8091 Defining_Unit_Name => Dnam),
8093 Declarations => New_List (
8094 Make_Object_Declaration (Loc,
8095 Defining_Identifier => Make_Temporary (Loc, 'T'),
8096 Object_Definition =>
8097 New_Occurrence_Of (Etype (Formal), Loc),
8098 Expression => New_Copy_Tree (Dcopy))),
8100 Handled_Statement_Sequence =>
8101 Make_Handled_Sequence_Of_Statements (Loc,
8102 Statements => Empty_List));
8104 Set_Scope (Dnam, Scope (E));
8105 Set_Assignment_OK (First (Declarations (Dbody)));
8106 Set_Is_Eliminated (Dnam);
8107 Insert_After (After, Dbody);
8108 Analyze (Dbody);
8109 After := Dbody;
8110 end if;
8111 end if;
8113 Next_Formal (Formal);
8114 end loop;
8115 end Process_Default_Expressions;
8117 ----------------------------------------
8118 -- Set_Component_Alignment_If_Not_Set --
8119 ----------------------------------------
8121 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
8122 begin
8123 -- Ignore if not base type, subtypes don't need anything
8125 if Typ /= Base_Type (Typ) then
8126 return;
8127 end if;
8129 -- Do not override existing representation
8131 if Is_Packed (Typ) then
8132 return;
8134 elsif Has_Specified_Layout (Typ) then
8135 return;
8137 elsif Component_Alignment (Typ) /= Calign_Default then
8138 return;
8140 else
8141 Set_Component_Alignment
8142 (Typ, Scope_Stack.Table
8143 (Scope_Stack.Last).Component_Alignment_Default);
8144 end if;
8145 end Set_Component_Alignment_If_Not_Set;
8147 --------------------------
8148 -- Set_SSO_From_Default --
8149 --------------------------
8151 procedure Set_SSO_From_Default (T : Entity_Id) is
8152 Reversed : Boolean;
8154 begin
8155 -- Set default SSO for an array or record base type, except in case of
8156 -- a type extension (which always inherits the SSO of its parent type).
8158 if Is_Base_Type (T)
8159 and then (Is_Array_Type (T)
8160 or else (Is_Record_Type (T)
8161 and then not (Is_Tagged_Type (T)
8162 and then Is_Derived_Type (T))))
8163 then
8164 Reversed :=
8165 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
8166 or else
8167 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
8169 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
8171 -- For a record type, if bit order is specified explicitly,
8172 -- then do not set SSO from default if not consistent. Note that
8173 -- we do not want to look at a Bit_Order attribute definition
8174 -- for a parent: if we were to inherit Bit_Order, then both
8175 -- SSO_Set_*_By_Default flags would have been cleared already
8176 -- (by Inherit_Aspects_At_Freeze_Point).
8178 and then not
8179 (Is_Record_Type (T)
8180 and then
8181 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
8182 and then Reverse_Bit_Order (T) /= Reversed)
8183 then
8184 -- If flags cause reverse storage order, then set the result. Note
8185 -- that we would have ignored the pragma setting the non default
8186 -- storage order in any case, hence the assertion at this point.
8188 pragma Assert
8189 (not Reversed or else Support_Nondefault_SSO_On_Target);
8191 Set_Reverse_Storage_Order (T, Reversed);
8193 -- For a record type, also set reversed bit order. Note: if a bit
8194 -- order has been specified explicitly, then this is a no-op.
8196 if Is_Record_Type (T) then
8197 Set_Reverse_Bit_Order (T, Reversed);
8198 end if;
8199 end if;
8200 end if;
8201 end Set_SSO_From_Default;
8203 ------------------
8204 -- Undelay_Type --
8205 ------------------
8207 procedure Undelay_Type (T : Entity_Id) is
8208 begin
8209 Set_Has_Delayed_Freeze (T, False);
8210 Set_Freeze_Node (T, Empty);
8212 -- Since we don't want T to have a Freeze_Node, we don't want its
8213 -- Full_View or Corresponding_Record_Type to have one either.
8215 -- ??? Fundamentally, this whole handling is unpleasant. What we really
8216 -- want is to be sure that for an Itype that's part of record R and is a
8217 -- subtype of type T, that it's frozen after the later of the freeze
8218 -- points of R and T. We have no way of doing that directly, so what we
8219 -- do is force most such Itypes to be frozen as part of freezing R via
8220 -- this procedure and only delay the ones that need to be delayed
8221 -- (mostly the designated types of access types that are defined as part
8222 -- of the record).
8224 if Is_Private_Type (T)
8225 and then Present (Full_View (T))
8226 and then Is_Itype (Full_View (T))
8227 and then Is_Record_Type (Scope (Full_View (T)))
8228 then
8229 Undelay_Type (Full_View (T));
8230 end if;
8232 if Is_Concurrent_Type (T)
8233 and then Present (Corresponding_Record_Type (T))
8234 and then Is_Itype (Corresponding_Record_Type (T))
8235 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
8236 then
8237 Undelay_Type (Corresponding_Record_Type (T));
8238 end if;
8239 end Undelay_Type;
8241 ------------------
8242 -- Warn_Overlay --
8243 ------------------
8245 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Entity_Id) is
8246 Ent : constant Entity_Id := Entity (Nam);
8247 -- The object to which the address clause applies
8249 Init : Node_Id;
8250 Old : Entity_Id := Empty;
8251 Decl : Node_Id;
8253 begin
8254 -- No warning if address clause overlay warnings are off
8256 if not Address_Clause_Overlay_Warnings then
8257 return;
8258 end if;
8260 -- No warning if there is an explicit initialization
8262 Init := Original_Node (Expression (Declaration_Node (Ent)));
8264 if Present (Init) and then Comes_From_Source (Init) then
8265 return;
8266 end if;
8268 -- We only give the warning for non-imported entities of a type for
8269 -- which a non-null base init proc is defined, or for objects of access
8270 -- types with implicit null initialization, or when Normalize_Scalars
8271 -- applies and the type is scalar or a string type (the latter being
8272 -- tested for because predefined String types are initialized by inline
8273 -- code rather than by an init_proc). Note that we do not give the
8274 -- warning for Initialize_Scalars, since we suppressed initialization
8275 -- in this case. Also, do not warn if Suppress_Initialization is set.
8277 if Present (Expr)
8278 and then not Is_Imported (Ent)
8279 and then not Initialization_Suppressed (Typ)
8280 and then (Has_Non_Null_Base_Init_Proc (Typ)
8281 or else Is_Access_Type (Typ)
8282 or else (Normalize_Scalars
8283 and then (Is_Scalar_Type (Typ)
8284 or else Is_String_Type (Typ))))
8285 then
8286 if Nkind (Expr) = N_Attribute_Reference
8287 and then Is_Entity_Name (Prefix (Expr))
8288 then
8289 Old := Entity (Prefix (Expr));
8291 elsif Is_Entity_Name (Expr)
8292 and then Ekind (Entity (Expr)) = E_Constant
8293 then
8294 Decl := Declaration_Node (Entity (Expr));
8296 if Nkind (Decl) = N_Object_Declaration
8297 and then Present (Expression (Decl))
8298 and then Nkind (Expression (Decl)) = N_Attribute_Reference
8299 and then Is_Entity_Name (Prefix (Expression (Decl)))
8300 then
8301 Old := Entity (Prefix (Expression (Decl)));
8303 elsif Nkind (Expr) = N_Function_Call then
8304 return;
8305 end if;
8307 -- A function call (most likely to To_Address) is probably not an
8308 -- overlay, so skip warning. Ditto if the function call was inlined
8309 -- and transformed into an entity.
8311 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
8312 return;
8313 end if;
8315 -- If a pragma Import follows, we assume that it is for the current
8316 -- target of the address clause, and skip the warning. There may be
8317 -- a source pragma or an aspect that specifies import and generates
8318 -- the corresponding pragma. These will indicate that the entity is
8319 -- imported and that is checked above so that the spurious warning
8320 -- (generated when the entity is frozen) will be suppressed. The
8321 -- pragma may be attached to the aspect, so it is not yet a list
8322 -- member.
8324 if Is_List_Member (Parent (Expr)) then
8325 Decl := Next (Parent (Expr));
8327 if Present (Decl)
8328 and then Nkind (Decl) = N_Pragma
8329 and then Pragma_Name (Decl) = Name_Import
8330 then
8331 return;
8332 end if;
8333 end if;
8335 -- Otherwise give warning message
8337 if Present (Old) then
8338 Error_Msg_Node_2 := Old;
8339 Error_Msg_N
8340 ("default initialization of & may modify &??",
8341 Nam);
8342 else
8343 Error_Msg_N
8344 ("default initialization of & may modify overlaid storage??",
8345 Nam);
8346 end if;
8348 -- Add friendly warning if initialization comes from a packed array
8349 -- component.
8351 if Is_Record_Type (Typ) then
8352 declare
8353 Comp : Entity_Id;
8355 begin
8356 Comp := First_Component (Typ);
8357 while Present (Comp) loop
8358 if Nkind (Parent (Comp)) = N_Component_Declaration
8359 and then Present (Expression (Parent (Comp)))
8360 then
8361 exit;
8362 elsif Is_Array_Type (Etype (Comp))
8363 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
8364 then
8365 Error_Msg_NE
8366 ("\packed array component& " &
8367 "will be initialized to zero??",
8368 Nam, Comp);
8369 exit;
8370 else
8371 Next_Component (Comp);
8372 end if;
8373 end loop;
8374 end;
8375 end if;
8377 Error_Msg_N
8378 ("\use pragma Import for & to " &
8379 "suppress initialization (RM B.1(24))??",
8380 Nam);
8381 end if;
8382 end Warn_Overlay;
8384 end Freeze;