c++: remove some xfails
[official-gcc.git] / gcc / ada / freeze.adb
blob382e5b4b06be02cff8aec1270e1b170e6b897299
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-2022, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Ch3; use Exp_Ch3;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Pakd; use Exp_Pakd;
40 with Exp_Util; use Exp_Util;
41 with Exp_Tss; use Exp_Tss;
42 with Ghost; use Ghost;
43 with Layout; use Layout;
44 with Lib; use Lib;
45 with Namet; use Namet;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Mech; use Sem_Mech;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Res; use Sem_Res;
65 with Sem_Util; use Sem_Util;
66 with Sinfo; use Sinfo;
67 with Sinfo.Nodes; use Sinfo.Nodes;
68 with Sinfo.Utils; use Sinfo.Utils;
69 with Snames; use Snames;
70 with Stand; use Stand;
71 with Stringt; use Stringt;
72 with Strub; use Strub;
73 with Targparm; use Targparm;
74 with Tbuild; use Tbuild;
75 with Ttypes; use Ttypes;
76 with Uintp; use Uintp;
77 with Urealp; use Urealp;
78 with Warnsw; use Warnsw;
80 package body Freeze is
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
86 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
87 -- Typ is a type that is being frozen. If no size clause is given,
88 -- but a default Esize has been computed, then this default Esize is
89 -- adjusted up if necessary to be consistent with a given alignment,
90 -- but never to a value greater than System_Max_Integer_Size. This is
91 -- used for all discrete types and for fixed-point types.
93 procedure Build_And_Analyze_Renamed_Body
94 (Decl : Node_Id;
95 New_S : Entity_Id;
96 After : in out Node_Id);
97 -- Build body for a renaming declaration, insert in tree and analyze
99 procedure Check_Address_Clause (E : Entity_Id);
100 -- Apply legality checks to address clauses for object declarations,
101 -- at the point the object is frozen. Also ensure any initialization is
102 -- performed only after the object has been frozen.
104 procedure Check_Component_Storage_Order
105 (Encl_Type : Entity_Id;
106 Comp : Entity_Id;
107 ADC : Node_Id;
108 Comp_ADC_Present : out Boolean);
109 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
110 -- clause, verify that the component type has an explicit and compatible
111 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
112 -- entity of the component under consideration. For an Encl_Type that
113 -- does not have a Scalar_Storage_Order attribute definition clause,
114 -- verify that the component also does not have such a clause.
115 -- ADC is the attribute definition clause if present (or Empty). On return,
116 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
117 -- attribute definition clause.
119 procedure Check_Debug_Info_Needed (T : Entity_Id);
120 -- As each entity is frozen, this routine is called to deal with the
121 -- setting of Debug_Info_Needed for the entity. This flag is set if
122 -- the entity comes from source, or if we are in Debug_Generated_Code
123 -- mode or if the -gnatdV debug flag is set. However, it never sets
124 -- the flag if Debug_Info_Off is set. This procedure also ensures that
125 -- subsidiary entities have the flag set as required.
127 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
128 -- When an expression function is frozen by a use of it, the expression
129 -- itself is frozen. Check that the expression does not include references
130 -- to deferred constants without completion. We report this at the freeze
131 -- point of the function, to provide a better error message.
133 -- In most cases the expression itself is frozen by the time the function
134 -- itself is frozen, because the formals will be frozen by then. However,
135 -- Attribute references to outer types are freeze points for those types;
136 -- this routine generates the required freeze nodes for them.
138 procedure Check_Strict_Alignment (E : Entity_Id);
139 -- E is a base type. If E is tagged or has a component that is aliased
140 -- or tagged or contains something this is aliased or tagged, set
141 -- Strict_Alignment.
143 procedure Check_Unsigned_Type (E : Entity_Id);
144 pragma Inline (Check_Unsigned_Type);
145 -- If E is a fixed-point or discrete type, then all the necessary work
146 -- to freeze it is completed except for possible setting of the flag
147 -- Is_Unsigned_Type, which is done by this procedure. The call has no
148 -- effect if the entity E is not a discrete or fixed-point type.
150 procedure Freeze_And_Append
151 (Ent : Entity_Id;
152 N : Node_Id;
153 Result : in out List_Id);
154 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
155 -- nodes to Result, modifying Result from No_List if necessary. N has
156 -- the same usage as in Freeze_Entity.
158 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
159 -- Freeze enumeration type. The Esize field is set as processing
160 -- proceeds (i.e. set by default when the type is declared and then
161 -- adjusted by rep clauses). What this procedure does is to make sure
162 -- that if a foreign convention is specified, and no specific size
163 -- is given, then the size must be at least Integer'Size.
165 procedure Freeze_Static_Object (E : Entity_Id);
166 -- If an object is frozen which has Is_Statically_Allocated set, then
167 -- all referenced types must also be marked with this flag. This routine
168 -- is in charge of meeting this requirement for the object entity E.
170 procedure Freeze_Subprogram (E : Entity_Id);
171 -- Perform freezing actions for a subprogram (create extra formals,
172 -- and set proper default mechanism values). Note that this routine
173 -- is not called for internal subprograms, for which neither of these
174 -- actions is needed (or desirable, we do not want for example to have
175 -- these extra formals present in initialization procedures, where they
176 -- would serve no purpose). In this call E is either a subprogram or
177 -- a subprogram type (i.e. an access to a subprogram).
179 function Is_Fully_Defined (T : Entity_Id) return Boolean;
180 -- True if T is not private and has no private components, or has a full
181 -- view. Used to determine whether the designated type of an access type
182 -- should be frozen when the access type is frozen. This is done when an
183 -- allocator is frozen, or an expression that may involve attributes of
184 -- the designated type. Otherwise freezing the access type does not freeze
185 -- the designated type.
187 function Should_Freeze_Type
188 (Typ : Entity_Id; E : Entity_Id; N : Node_Id) return Boolean;
189 -- If Typ is in the current scope, then return True.
190 -- N is a node whose source location corresponds to the freeze point.
191 -- ??? Expression functions (represented by E) shouldn't freeze types in
192 -- general, but our current expansion and freezing model requires an early
193 -- freezing when the dispatch table is needed or when building an aggregate
194 -- with a subtype of Typ, so return True also in this case.
195 -- Note that expression function completions do freeze and are
196 -- handled in Sem_Ch6.Analyze_Expression_Function.
198 ------------------------
199 -- Should_Freeze_Type --
200 ------------------------
202 function Should_Freeze_Type
203 (Typ : Entity_Id; E : Entity_Id; N : Node_Id) return Boolean
205 function Is_Dispatching_Call_Or_Aggregate
206 (N : Node_Id) return Traverse_Result;
207 -- Return Abandon if N is a dispatching call to a subprogram
208 -- declared in the same scope as Typ or an aggregate whose type
209 -- is Typ.
211 --------------------------------------
212 -- Is_Dispatching_Call_Or_Aggregate --
213 --------------------------------------
215 function Is_Dispatching_Call_Or_Aggregate
216 (N : Node_Id) return Traverse_Result is
217 begin
218 if Nkind (N) = N_Function_Call
219 and then Present (Controlling_Argument (N))
220 and then Scope (Entity (Original_Node (Name (N))))
221 = Scope (Typ)
222 then
223 return Abandon;
224 elsif Nkind (N) = N_Aggregate
225 and then Base_Type (Etype (N)) = Base_Type (Typ)
226 then
227 return Abandon;
228 else
229 return OK;
230 end if;
231 end Is_Dispatching_Call_Or_Aggregate;
233 -------------------------
234 -- Need_Dispatch_Table --
235 -------------------------
237 function Need_Dispatch_Table is new
238 Traverse_Func (Is_Dispatching_Call_Or_Aggregate);
239 -- Return Abandon if the input expression requires access to
240 -- Typ's dispatch table.
242 Decl : constant Node_Id :=
243 (if No (E) then E else Original_Node (Unit_Declaration_Node (E)));
245 -- Start of processing for Should_Freeze_Type
247 begin
248 return Within_Scope (Typ, Current_Scope)
249 or else (Nkind (N) = N_Subprogram_Renaming_Declaration
250 and then Present (Corresponding_Formal_Spec (N)))
251 or else (Present (Decl)
252 and then Nkind (Decl) = N_Expression_Function
253 and then Need_Dispatch_Table (Expression (Decl)) = Abandon);
254 end Should_Freeze_Type;
256 procedure Process_Default_Expressions
257 (E : Entity_Id;
258 After : in out Node_Id);
259 -- This procedure is called for each subprogram to complete processing of
260 -- default expressions at the point where all types are known to be frozen.
261 -- The expressions must be analyzed in full, to make sure that all error
262 -- processing is done (they have only been preanalyzed). If the expression
263 -- is not an entity or literal, its analysis may generate code which must
264 -- not be executed. In that case we build a function body to hold that
265 -- code. This wrapper function serves no other purpose (it used to be
266 -- called to evaluate the default, but now the default is inlined at each
267 -- point of call).
269 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
270 -- Typ is a record or array type that is being frozen. This routine sets
271 -- the default component alignment from the scope stack values if the
272 -- alignment is otherwise not specified.
274 procedure Set_SSO_From_Default (T : Entity_Id);
275 -- T is a record or array type that is being frozen. If it is a base type,
276 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
277 -- will be set appropriately. Note that an explicit occurrence of aspect
278 -- Scalar_Storage_Order or an explicit setting of this aspect with an
279 -- attribute definition clause occurs, then these two flags are reset in
280 -- any case, so call will have no effect.
282 procedure Undelay_Type (T : Entity_Id);
283 -- T is a type of a component that we know to be an Itype. We don't want
284 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
285 -- Full_View or Corresponding_Record_Type.
287 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
288 -- Expr is the expression for an address clause for the entity denoted by
289 -- Nam whose type is Typ. If Typ has a default initialization, and there is
290 -- no explicit initialization in the source declaration, check whether the
291 -- address clause might cause overlaying of an entity, and emit a warning
292 -- on the side effect that the initialization will cause.
294 -------------------------------
295 -- Adjust_Esize_For_Alignment --
296 -------------------------------
298 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
299 Align : Uint;
301 begin
302 if Known_Esize (Typ) and then Known_Alignment (Typ) then
303 Align := Alignment_In_Bits (Typ);
305 if Align > Esize (Typ) and then Align <= System_Max_Integer_Size then
306 Set_Esize (Typ, Align);
307 end if;
308 end if;
309 end Adjust_Esize_For_Alignment;
311 ------------------------------------
312 -- Build_And_Analyze_Renamed_Body --
313 ------------------------------------
315 procedure Build_And_Analyze_Renamed_Body
316 (Decl : Node_Id;
317 New_S : Entity_Id;
318 After : in out Node_Id)
320 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
321 Ent : constant Entity_Id := Defining_Entity (Decl);
322 Body_Node : Node_Id;
323 Renamed_Subp : Entity_Id;
325 begin
326 -- If the renamed subprogram is intrinsic, there is no need for a
327 -- wrapper body: we set the alias that will be called and expanded which
328 -- completes the declaration. This transformation is only legal if the
329 -- renamed entity has already been elaborated.
331 -- Note that it is legal for a renaming_as_body to rename an intrinsic
332 -- subprogram, as long as the renaming occurs before the new entity
333 -- is frozen (RM 8.5.4 (5)).
335 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
336 and then Is_Entity_Name (Name (Body_Decl))
337 then
338 Renamed_Subp := Entity (Name (Body_Decl));
339 else
340 Renamed_Subp := Empty;
341 end if;
343 if Present (Renamed_Subp)
344 and then Is_Intrinsic_Subprogram (Renamed_Subp)
345 and then
346 (not In_Same_Source_Unit (Renamed_Subp, Ent)
347 or else Sloc (Renamed_Subp) < Sloc (Ent))
349 -- We can make the renaming entity intrinsic if the renamed function
350 -- has an interface name, or if it is one of the shift/rotate
351 -- operations known to the compiler.
353 and then
354 (Present (Interface_Name (Renamed_Subp))
355 or else Chars (Renamed_Subp) in Name_Rotate_Left
356 | Name_Rotate_Right
357 | Name_Shift_Left
358 | Name_Shift_Right
359 | Name_Shift_Right_Arithmetic)
360 then
361 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
363 if Present (Alias (Renamed_Subp)) then
364 Set_Alias (Ent, Alias (Renamed_Subp));
365 else
366 Set_Alias (Ent, Renamed_Subp);
367 end if;
369 Set_Is_Intrinsic_Subprogram (Ent);
370 Set_Has_Completion (Ent);
372 else
373 Body_Node := Build_Renamed_Body (Decl, New_S);
374 Insert_After (After, Body_Node);
375 Mark_Rewrite_Insertion (Body_Node);
376 Analyze (Body_Node);
377 After := Body_Node;
378 end if;
379 end Build_And_Analyze_Renamed_Body;
381 ------------------------
382 -- Build_Renamed_Body --
383 ------------------------
385 function Build_Renamed_Body
386 (Decl : Node_Id;
387 New_S : Entity_Id) return Node_Id
389 Loc : constant Source_Ptr := Sloc (New_S);
390 -- We use for the source location of the renamed body, the location of
391 -- the spec entity. It might seem more natural to use the location of
392 -- the renaming declaration itself, but that would be wrong, since then
393 -- the body we create would look as though it was created far too late,
394 -- and this could cause problems with elaboration order analysis,
395 -- particularly in connection with instantiations.
397 N : constant Node_Id := Unit_Declaration_Node (New_S);
398 Nam : constant Node_Id := Name (N);
399 Old_S : Entity_Id;
400 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
401 Actuals : List_Id := No_List;
402 Call_Node : Node_Id;
403 Call_Name : Node_Id;
404 Body_Node : Node_Id;
405 Formal : Entity_Id;
406 O_Formal : Entity_Id;
407 Param_Spec : Node_Id;
409 Pref : Node_Id := Empty;
410 -- If the renamed entity is a primitive operation given in prefix form,
411 -- the prefix is the target object and it has to be added as the first
412 -- actual in the generated call.
414 begin
415 -- Determine the entity being renamed, which is the target of the call
416 -- statement. If the name is an explicit dereference, this is a renaming
417 -- of a subprogram type rather than a subprogram. The name itself is
418 -- fully analyzed.
420 if Nkind (Nam) = N_Selected_Component then
421 Old_S := Entity (Selector_Name (Nam));
423 elsif Nkind (Nam) = N_Explicit_Dereference then
424 Old_S := Etype (Nam);
426 elsif Nkind (Nam) = N_Indexed_Component then
427 if Is_Entity_Name (Prefix (Nam)) then
428 Old_S := Entity (Prefix (Nam));
429 else
430 Old_S := Entity (Selector_Name (Prefix (Nam)));
431 end if;
433 elsif Nkind (Nam) = N_Character_Literal then
434 Old_S := Etype (New_S);
436 else
437 Old_S := Entity (Nam);
438 end if;
440 if Is_Entity_Name (Nam) then
442 -- If the renamed entity is a predefined operator, retain full name
443 -- to ensure its visibility.
445 if Ekind (Old_S) = E_Operator
446 and then Nkind (Nam) = N_Expanded_Name
447 then
448 Call_Name := New_Copy (Name (N));
449 else
450 Call_Name := New_Occurrence_Of (Old_S, Loc);
451 end if;
453 else
454 if Nkind (Nam) = N_Selected_Component
455 and then Present (First_Formal (Old_S))
456 and then
457 (Is_Controlling_Formal (First_Formal (Old_S))
458 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
459 then
461 -- Retrieve the target object, to be added as a first actual
462 -- in the call.
464 Call_Name := New_Occurrence_Of (Old_S, Loc);
465 Pref := Prefix (Nam);
467 else
468 Call_Name := New_Copy (Name (N));
469 end if;
471 -- Original name may have been overloaded, but is fully resolved now
473 Set_Is_Overloaded (Call_Name, False);
474 end if;
476 -- For simple renamings, subsequent calls can be expanded directly as
477 -- calls to the renamed entity. The body must be generated in any case
478 -- for calls that may appear elsewhere. This is not done in the case
479 -- where the subprogram is an instantiation because the actual proper
480 -- body has not been built yet. This is also not done in GNATprove mode
481 -- as we need to check other conditions for creating a body to inline
482 -- in that case, which are controlled in Analyze_Subprogram_Body_Helper.
484 if Ekind (Old_S) in E_Function | E_Procedure
485 and then Nkind (Decl) = N_Subprogram_Declaration
486 and then not Is_Generic_Instance (Old_S)
487 and then not GNATprove_Mode
488 then
489 Set_Body_To_Inline (Decl, Old_S);
490 end if;
492 -- Check whether the return type is a limited view. If the subprogram
493 -- is already frozen the generated body may have a non-limited view
494 -- of the type, that must be used, because it is the one in the spec
495 -- of the renaming declaration.
497 if Ekind (Old_S) = E_Function
498 and then Is_Entity_Name (Result_Definition (Spec))
499 then
500 declare
501 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
502 begin
503 if Has_Non_Limited_View (Ret_Type) then
504 Set_Result_Definition
505 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
506 end if;
507 end;
508 end if;
510 -- The body generated for this renaming is an internal artifact, and
511 -- does not constitute a freeze point for the called entity.
513 Set_Must_Not_Freeze (Call_Name);
515 Formal := First_Formal (Defining_Entity (Decl));
517 if Present (Pref) then
518 declare
519 Pref_Type : constant Entity_Id := Etype (Pref);
520 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
522 begin
523 -- The controlling formal may be an access parameter, or the
524 -- actual may be an access value, so adjust accordingly.
526 if Is_Access_Type (Pref_Type)
527 and then not Is_Access_Type (Form_Type)
528 then
529 Actuals := New_List
530 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
532 elsif Is_Access_Type (Form_Type)
533 and then not Is_Access_Type (Pref)
534 then
535 Actuals :=
536 New_List (
537 Make_Attribute_Reference (Loc,
538 Attribute_Name => Name_Access,
539 Prefix => Relocate_Node (Pref)));
540 else
541 Actuals := New_List (Pref);
542 end if;
543 end;
545 elsif Present (Formal) then
546 Actuals := New_List;
548 else
549 Actuals := No_List;
550 end if;
552 while Present (Formal) loop
553 Append (New_Occurrence_Of (Formal, Loc), Actuals);
554 Next_Formal (Formal);
555 end loop;
557 -- If the renamed entity is an entry, inherit its profile. For other
558 -- renamings as bodies, both profiles must be subtype conformant, so it
559 -- is not necessary to replace the profile given in the declaration.
560 -- However, default values that are aggregates are rewritten when
561 -- partially analyzed, so we recover the original aggregate to insure
562 -- that subsequent conformity checking works. Similarly, if the default
563 -- expression was constant-folded, recover the original expression.
565 Formal := First_Formal (Defining_Entity (Decl));
567 if Present (Formal) then
568 O_Formal := First_Formal (Old_S);
569 Param_Spec := First (Parameter_Specifications (Spec));
570 while Present (Formal) loop
571 if Is_Entry (Old_S) then
572 if Nkind (Parameter_Type (Param_Spec)) /=
573 N_Access_Definition
574 then
575 Set_Etype (Formal, Etype (O_Formal));
576 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
577 end if;
579 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
580 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
581 Nkind (Default_Value (O_Formal))
582 then
583 Set_Expression (Param_Spec,
584 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
585 end if;
587 Next_Formal (Formal);
588 Next_Formal (O_Formal);
589 Next (Param_Spec);
590 end loop;
591 end if;
593 -- If the renamed entity is a function, the generated body contains a
594 -- return statement. Otherwise, build a procedure call. If the entity is
595 -- an entry, subsequent analysis of the call will transform it into the
596 -- proper entry or protected operation call. If the renamed entity is
597 -- a character literal, return it directly.
599 if Ekind (Old_S) = E_Function
600 or else Ekind (Old_S) = E_Operator
601 or else (Ekind (Old_S) = E_Subprogram_Type
602 and then Etype (Old_S) /= Standard_Void_Type)
603 then
604 Call_Node :=
605 Make_Simple_Return_Statement (Loc,
606 Expression =>
607 Make_Function_Call (Loc,
608 Name => Call_Name,
609 Parameter_Associations => Actuals));
611 elsif Ekind (Old_S) = E_Enumeration_Literal then
612 Call_Node :=
613 Make_Simple_Return_Statement (Loc,
614 Expression => New_Occurrence_Of (Old_S, Loc));
616 elsif Nkind (Nam) = N_Character_Literal then
617 Call_Node :=
618 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
620 else
621 Call_Node :=
622 Make_Procedure_Call_Statement (Loc,
623 Name => Call_Name,
624 Parameter_Associations => Actuals);
625 end if;
627 -- Create entities for subprogram body and formals
629 Set_Defining_Unit_Name (Spec,
630 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
632 Param_Spec := First (Parameter_Specifications (Spec));
633 while Present (Param_Spec) loop
634 Set_Defining_Identifier (Param_Spec,
635 Make_Defining_Identifier (Loc,
636 Chars => Chars (Defining_Identifier (Param_Spec))));
637 Next (Param_Spec);
638 end loop;
640 -- In GNATprove, prefer to generate an expression function whenever
641 -- possible, to benefit from the more precise analysis in that case
642 -- (as if an implicit postcondition had been generated).
644 if GNATprove_Mode
645 and then Nkind (Call_Node) = N_Simple_Return_Statement
646 then
647 Body_Node :=
648 Make_Expression_Function (Loc,
649 Specification => Spec,
650 Expression => Expression (Call_Node));
651 else
652 Body_Node :=
653 Make_Subprogram_Body (Loc,
654 Specification => Spec,
655 Declarations => New_List,
656 Handled_Statement_Sequence =>
657 Make_Handled_Sequence_Of_Statements (Loc,
658 Statements => New_List (Call_Node)));
659 end if;
661 if Nkind (Decl) /= N_Subprogram_Declaration then
662 Rewrite (N,
663 Make_Subprogram_Declaration (Loc,
664 Specification => Specification (N)));
665 end if;
667 -- Link the body to the entity whose declaration it completes. If
668 -- the body is analyzed when the renamed entity is frozen, it may
669 -- be necessary to restore the proper scope (see package Exp_Ch13).
671 if Nkind (N) = N_Subprogram_Renaming_Declaration
672 and then Present (Corresponding_Spec (N))
673 then
674 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
675 else
676 Set_Corresponding_Spec (Body_Node, New_S);
677 end if;
679 return Body_Node;
680 end Build_Renamed_Body;
682 --------------------------
683 -- Check_Address_Clause --
684 --------------------------
686 procedure Check_Address_Clause (E : Entity_Id) is
687 Addr : constant Node_Id := Address_Clause (E);
688 Typ : constant Entity_Id := Etype (E);
689 Decl : Node_Id;
690 Expr : Node_Id;
691 Init : Node_Id;
692 Lhs : Node_Id;
693 Tag_Assign : Node_Id;
695 begin
696 if Present (Addr) then
698 -- For a deferred constant, the initialization value is on full view
700 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
701 Decl := Declaration_Node (Full_View (E));
702 else
703 Decl := Declaration_Node (E);
704 end if;
706 Expr := Expression (Addr);
708 if Needs_Constant_Address (Decl, Typ) then
709 Check_Constant_Address_Clause (Expr, E);
711 -- Has_Delayed_Freeze was set on E when the address clause was
712 -- analyzed, and must remain set because we want the address
713 -- clause to be elaborated only after any entity it references
714 -- has been elaborated.
715 end if;
717 -- If Rep_Clauses are to be ignored, remove address clause from
718 -- list attached to entity, because it may be illegal for gigi,
719 -- for example by breaking order of elaboration.
721 if Ignore_Rep_Clauses then
722 declare
723 Rep : Node_Id;
725 begin
726 Rep := First_Rep_Item (E);
728 if Rep = Addr then
729 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
731 else
732 while Present (Rep)
733 and then Next_Rep_Item (Rep) /= Addr
734 loop
735 Next_Rep_Item (Rep);
736 end loop;
737 end if;
739 if Present (Rep) then
740 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
741 end if;
742 end;
744 -- And now remove the address clause
746 Kill_Rep_Clause (Addr);
748 elsif not Error_Posted (Expr)
749 and then not Needs_Finalization (Typ)
750 then
751 Warn_Overlay (Expr, Typ, Name (Addr));
752 end if;
754 Init := Expression (Decl);
756 -- If a variable, or a non-imported constant, overlays a constant
757 -- object and has an initialization value, then the initialization
758 -- may end up writing into read-only memory. Detect the cases of
759 -- statically identical values and remove the initialization. In
760 -- the other cases, give a warning. We will give other warnings
761 -- later for the variable if it is assigned.
763 if (Ekind (E) = E_Variable
764 or else (Ekind (E) = E_Constant
765 and then not Is_Imported (E)))
766 and then Overlays_Constant (E)
767 and then Present (Init)
768 then
769 declare
770 O_Ent : Entity_Id;
771 Off : Boolean;
773 begin
774 Find_Overlaid_Entity (Addr, O_Ent, Off);
776 if Ekind (O_Ent) = E_Constant
777 and then Etype (O_Ent) = Typ
778 and then Present (Constant_Value (O_Ent))
779 and then Compile_Time_Compare
780 (Init,
781 Constant_Value (O_Ent),
782 Assume_Valid => True) = EQ
783 then
784 Set_No_Initialization (Decl);
785 return;
787 elsif Comes_From_Source (Init)
788 and then Address_Clause_Overlay_Warnings
789 then
790 Error_Msg_Sloc := Sloc (Addr);
791 Error_Msg_NE
792 ("?o?constant& may be modified via address clause#",
793 Decl, O_Ent);
794 end if;
795 end;
796 end if;
798 -- Remove side effects from initial expression, except in the case of
799 -- limited build-in-place calls and aggregates, which have their own
800 -- expansion elsewhere. This exception is necessary to avoid copying
801 -- limited objects.
803 if Present (Init)
804 and then not Is_Limited_View (Typ)
805 then
806 -- Capture initialization value at point of declaration, and make
807 -- explicit assignment legal, because object may be a constant.
809 Remove_Side_Effects (Init);
810 Lhs := New_Occurrence_Of (E, Sloc (Decl));
811 Set_Assignment_OK (Lhs);
813 -- Move initialization to freeze actions, once the object has
814 -- been frozen and the address clause alignment check has been
815 -- performed.
817 Append_Freeze_Action (E,
818 Make_Assignment_Statement (Sloc (Decl),
819 Name => Lhs,
820 Expression => Expression (Decl)));
822 Set_No_Initialization (Decl);
824 -- If the object is tagged, check whether the tag must be
825 -- reassigned explicitly.
827 Tag_Assign := Make_Tag_Assignment (Decl);
828 if Present (Tag_Assign) then
829 Append_Freeze_Action (E, Tag_Assign);
830 end if;
831 end if;
832 end if;
833 end Check_Address_Clause;
835 -----------------------------
836 -- Check_Compile_Time_Size --
837 -----------------------------
839 procedure Check_Compile_Time_Size (T : Entity_Id) is
841 procedure Set_Small_Size (T : Entity_Id; S : Uint);
842 -- Sets the compile time known size in the RM_Size field of T, checking
843 -- for a size clause that was given which attempts to give a small size.
845 function Size_Known (T : Entity_Id) return Boolean;
846 -- Recursive function that does all the work
848 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
849 -- If T is a constrained subtype, its size is not known if any of its
850 -- discriminant constraints is not static and it is not a null record.
851 -- The test is conservative and doesn't check that the components are
852 -- in fact constrained by non-static discriminant values. Could be made
853 -- more precise ???
855 --------------------
856 -- Set_Small_Size --
857 --------------------
859 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
860 begin
861 if S > System_Max_Integer_Size then
862 return;
864 -- Check for bad size clause given
866 elsif Has_Size_Clause (T) then
867 if RM_Size (T) < S then
868 Error_Msg_Uint_1 := S;
869 Error_Msg_NE (Size_Too_Small_Message, Size_Clause (T), T);
870 end if;
872 -- Set size if not set already. Do not set it to Uint_0, because in
873 -- some cases (notably array-of-record), the Component_Size is
874 -- No_Uint, which causes S to be Uint_0. Presumably the RM_Size and
875 -- Component_Size will eventually be set correctly by the back end.
877 elsif not Known_RM_Size (T) and then S /= Uint_0 then
878 Set_RM_Size (T, S);
879 end if;
880 end Set_Small_Size;
882 ----------------
883 -- Size_Known --
884 ----------------
886 function Size_Known (T : Entity_Id) return Boolean is
887 Comp : Entity_Id;
888 Ctyp : Entity_Id;
890 begin
891 if Size_Known_At_Compile_Time (T) then
892 return True;
894 -- Always True for elementary types, even generic formal elementary
895 -- types. We used to return False in the latter case, but the size
896 -- is known at compile time, even in the template, we just do not
897 -- know the exact size but that's not the point of this routine.
899 elsif Is_Elementary_Type (T) or else Is_Task_Type (T) then
900 return True;
902 -- Array types
904 elsif Is_Array_Type (T) then
906 -- String literals always have known size, and we can set it
908 if Ekind (T) = E_String_Literal_Subtype then
909 if Known_Component_Size (T) then
910 Set_Small_Size
911 (T, Component_Size (T) * String_Literal_Length (T));
913 else
914 -- The following is wrong, but does what previous versions
915 -- did. The Component_Size is unknown for the string in a
916 -- pragma Warnings.
917 Set_Small_Size (T, Uint_0);
918 end if;
920 return True;
922 -- Unconstrained types never have known at compile time size
924 elsif not Is_Constrained (T) then
925 return False;
927 -- Don't do any recursion on type with error posted, since we may
928 -- have a malformed type that leads us into a loop.
930 elsif Error_Posted (T) then
931 return False;
933 -- Otherwise if component size unknown, then array size unknown
935 elsif not Size_Known (Component_Type (T)) then
936 return False;
937 end if;
939 -- Check for all indexes static, and also compute possible size
940 -- (in case it is not greater than System_Max_Integer_Size and
941 -- thus may be packable).
943 declare
944 Index : Entity_Id;
945 Low : Node_Id;
946 High : Node_Id;
947 Size : Uint := Component_Size (T);
948 Dim : Uint;
950 begin
951 -- See comment in Set_Small_Size above
953 if No (Size) then
954 Size := Uint_0;
955 end if;
957 Index := First_Index (T);
958 while Present (Index) loop
959 if Nkind (Index) = N_Range then
960 Get_Index_Bounds (Index, Low, High);
962 elsif Error_Posted (Scalar_Range (Etype (Index))) then
963 return False;
965 else
966 Low := Type_Low_Bound (Etype (Index));
967 High := Type_High_Bound (Etype (Index));
968 end if;
970 if not Compile_Time_Known_Value (Low)
971 or else not Compile_Time_Known_Value (High)
972 or else Etype (Index) = Any_Type
973 then
974 return False;
976 else
977 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
979 if Dim > Uint_0 then
980 Size := Size * Dim;
981 else
982 Size := Uint_0;
983 end if;
984 end if;
986 Next_Index (Index);
987 end loop;
989 Set_Small_Size (T, Size);
990 return True;
991 end;
993 -- For non-generic private types, go to underlying type if present
995 elsif Is_Private_Type (T)
996 and then not Is_Generic_Type (T)
997 and then Present (Underlying_Type (T))
998 then
999 -- Don't do any recursion on type with error posted, since we may
1000 -- have a malformed type that leads us into a loop.
1002 if Error_Posted (T) then
1003 return False;
1004 else
1005 return Size_Known (Underlying_Type (T));
1006 end if;
1008 -- Record types
1010 elsif Is_Record_Type (T) then
1012 -- A class-wide type is never considered to have a known size
1014 if Is_Class_Wide_Type (T) then
1015 return False;
1017 -- A subtype of a variant record must not have non-static
1018 -- discriminated components.
1020 elsif T /= Base_Type (T)
1021 and then not Static_Discriminated_Components (T)
1022 then
1023 return False;
1025 -- Don't do any recursion on type with error posted, since we may
1026 -- have a malformed type that leads us into a loop.
1028 elsif Error_Posted (T) then
1029 return False;
1030 end if;
1032 -- Now look at the components of the record
1034 declare
1035 -- The following two variables are used to keep track of the
1036 -- size of packed records if we can tell the size of the packed
1037 -- record in the front end. Packed_Size_Known is True if so far
1038 -- we can figure out the size. It is initialized to True for a
1039 -- packed record, unless the record has either discriminants or
1040 -- independent components, or is a strict-alignment type, since
1041 -- it cannot be fully packed in this case.
1043 -- The reason we eliminate the discriminated case is that
1044 -- we don't know the way the back end lays out discriminated
1045 -- packed records. If Packed_Size_Known is True, then
1046 -- Packed_Size is the size in bits so far.
1048 Packed_Size_Known : Boolean :=
1049 Is_Packed (T)
1050 and then not Has_Discriminants (T)
1051 and then not Has_Independent_Components (T)
1052 and then not Strict_Alignment (T);
1054 Packed_Size : Uint := Uint_0;
1055 -- Size in bits so far
1057 begin
1058 -- Test for variant part present
1060 if Has_Discriminants (T)
1061 and then Present (Parent (T))
1062 and then Nkind (Parent (T)) = N_Full_Type_Declaration
1063 and then Nkind (Type_Definition (Parent (T))) =
1064 N_Record_Definition
1065 and then not Null_Present (Type_Definition (Parent (T)))
1066 and then
1067 Present (Variant_Part
1068 (Component_List (Type_Definition (Parent (T)))))
1069 then
1070 -- If variant part is present, and type is unconstrained,
1071 -- then we must have defaulted discriminants, or a size
1072 -- clause must be present for the type, or else the size
1073 -- is definitely not known at compile time.
1075 if not Is_Constrained (T)
1076 and then
1077 No (Discriminant_Default_Value (First_Discriminant (T)))
1078 and then not Known_RM_Size (T)
1079 then
1080 return False;
1081 end if;
1082 end if;
1084 -- Loop through components
1086 Comp := First_Component_Or_Discriminant (T);
1087 while Present (Comp) loop
1088 Ctyp := Etype (Comp);
1090 -- We do not know the packed size if there is a component
1091 -- clause present (we possibly could, but this would only
1092 -- help in the case of a record with partial rep clauses.
1093 -- That's because in the case of full rep clauses, the
1094 -- size gets figured out anyway by a different circuit).
1096 if Present (Component_Clause (Comp)) then
1097 Packed_Size_Known := False;
1098 end if;
1100 -- We do not know the packed size for an independent
1101 -- component or if it is of a strict-alignment type,
1102 -- since packing does not touch these (RM 13.2(7)).
1104 if Is_Independent (Comp)
1105 or else Is_Independent (Ctyp)
1106 or else Strict_Alignment (Ctyp)
1107 then
1108 Packed_Size_Known := False;
1109 end if;
1111 -- We need to identify a component that is an array where
1112 -- the index type is an enumeration type with non-standard
1113 -- representation, and some bound of the type depends on a
1114 -- discriminant.
1116 -- This is because gigi computes the size by doing a
1117 -- substitution of the appropriate discriminant value in
1118 -- the size expression for the base type, and gigi is not
1119 -- clever enough to evaluate the resulting expression (which
1120 -- involves a call to rep_to_pos) at compile time.
1122 -- It would be nice if gigi would either recognize that
1123 -- this expression can be computed at compile time, or
1124 -- alternatively figured out the size from the subtype
1125 -- directly, where all the information is at hand ???
1127 if Is_Array_Type (Etype (Comp))
1128 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1129 then
1130 declare
1131 Ocomp : constant Entity_Id :=
1132 Original_Record_Component (Comp);
1133 OCtyp : constant Entity_Id := Etype (Ocomp);
1134 Ind : Node_Id;
1135 Indtyp : Entity_Id;
1136 Lo, Hi : Node_Id;
1138 begin
1139 Ind := First_Index (OCtyp);
1140 while Present (Ind) loop
1141 Indtyp := Etype (Ind);
1143 if Is_Enumeration_Type (Indtyp)
1144 and then Has_Non_Standard_Rep (Indtyp)
1145 then
1146 Lo := Type_Low_Bound (Indtyp);
1147 Hi := Type_High_Bound (Indtyp);
1149 if Is_Entity_Name (Lo)
1150 and then Ekind (Entity (Lo)) = E_Discriminant
1151 then
1152 return False;
1154 elsif Is_Entity_Name (Hi)
1155 and then Ekind (Entity (Hi)) = E_Discriminant
1156 then
1157 return False;
1158 end if;
1159 end if;
1161 Next_Index (Ind);
1162 end loop;
1163 end;
1164 end if;
1166 -- Clearly size of record is not known if the size of one of
1167 -- the components is not known.
1169 if not Size_Known (Ctyp) then
1170 return False;
1171 end if;
1173 -- Accumulate packed size if possible
1175 if Packed_Size_Known then
1177 -- We can deal with elementary types, small packed arrays
1178 -- if the representation is a modular type and also small
1179 -- record types as checked by Set_Small_Size.
1181 if Is_Elementary_Type (Ctyp)
1182 or else (Is_Array_Type (Ctyp)
1183 and then Present
1184 (Packed_Array_Impl_Type (Ctyp))
1185 and then Is_Modular_Integer_Type
1186 (Packed_Array_Impl_Type (Ctyp)))
1187 or else Is_Record_Type (Ctyp)
1188 then
1189 -- If RM_Size is known and static, then we can keep
1190 -- accumulating the packed size.
1192 if Known_Static_RM_Size (Ctyp) then
1194 Packed_Size := Packed_Size + RM_Size (Ctyp);
1196 -- If we have a field whose RM_Size is not known then
1197 -- we can't figure out the packed size here.
1199 else
1200 Packed_Size_Known := False;
1201 end if;
1203 -- For other types we can't figure out the packed size
1205 else
1206 Packed_Size_Known := False;
1207 end if;
1208 end if;
1210 Next_Component_Or_Discriminant (Comp);
1211 end loop;
1213 if Packed_Size_Known then
1214 Set_Small_Size (T, Packed_Size);
1215 end if;
1217 return True;
1218 end;
1220 -- All other cases, size not known at compile time
1222 else
1223 return False;
1224 end if;
1225 end Size_Known;
1227 -------------------------------------
1228 -- Static_Discriminated_Components --
1229 -------------------------------------
1231 function Static_Discriminated_Components
1232 (T : Entity_Id) return Boolean
1234 Constraint : Elmt_Id;
1236 begin
1237 if Has_Discriminants (T)
1238 and then Present (Discriminant_Constraint (T))
1239 and then Present (First_Component (T))
1240 then
1241 Constraint := First_Elmt (Discriminant_Constraint (T));
1242 while Present (Constraint) loop
1243 if not Compile_Time_Known_Value (Node (Constraint)) then
1244 return False;
1245 end if;
1247 Next_Elmt (Constraint);
1248 end loop;
1249 end if;
1251 return True;
1252 end Static_Discriminated_Components;
1254 -- Start of processing for Check_Compile_Time_Size
1256 begin
1257 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1258 end Check_Compile_Time_Size;
1260 -----------------------------------
1261 -- Check_Component_Storage_Order --
1262 -----------------------------------
1264 procedure Check_Component_Storage_Order
1265 (Encl_Type : Entity_Id;
1266 Comp : Entity_Id;
1267 ADC : Node_Id;
1268 Comp_ADC_Present : out Boolean)
1270 Comp_Base : Entity_Id;
1271 Comp_ADC : Node_Id;
1272 Encl_Base : Entity_Id;
1273 Err_Node : Node_Id;
1275 Component_Aliased : Boolean;
1277 Comp_Byte_Aligned : Boolean := False;
1278 -- Set for the record case, True if Comp is aligned on byte boundaries
1279 -- (in which case it is allowed to have different storage order).
1281 Comp_SSO_Differs : Boolean;
1282 -- Set True when the component is a nested composite, and it does not
1283 -- have the same scalar storage order as Encl_Type.
1285 begin
1286 -- Record case
1288 if Present (Comp) then
1289 Err_Node := Comp;
1290 Comp_Base := Etype (Comp);
1292 if Is_Tag (Comp) then
1293 Comp_Byte_Aligned := True;
1294 Component_Aliased := False;
1296 else
1297 -- If a component clause is present, check if the component starts
1298 -- and ends on byte boundaries. Otherwise conservatively assume it
1299 -- does so only in the case where the record is not packed.
1301 if Present (Component_Clause (Comp)) then
1302 Comp_Byte_Aligned :=
1303 Known_Normalized_First_Bit (Comp)
1304 and then
1305 Known_Esize (Comp)
1306 and then
1307 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0
1308 and then
1309 Esize (Comp) mod System_Storage_Unit = 0;
1310 else
1311 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1312 end if;
1314 Component_Aliased := Is_Aliased (Comp);
1315 end if;
1317 -- Array case
1319 else
1320 Err_Node := Encl_Type;
1321 Comp_Base := Component_Type (Encl_Type);
1323 Component_Aliased := Has_Aliased_Components (Encl_Type);
1324 end if;
1326 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1327 -- the attribute definition clause is attached to the first subtype.
1328 -- Also, if the base type is incomplete or private, go to full view
1329 -- if known
1331 Encl_Base := Base_Type (Encl_Type);
1332 if Present (Underlying_Type (Encl_Base)) then
1333 Encl_Base := Underlying_Type (Encl_Base);
1334 end if;
1336 Comp_Base := Base_Type (Comp_Base);
1337 if Present (Underlying_Type (Comp_Base)) then
1338 Comp_Base := Underlying_Type (Comp_Base);
1339 end if;
1341 Comp_ADC :=
1342 Get_Attribute_Definition_Clause
1343 (First_Subtype (Comp_Base), Attribute_Scalar_Storage_Order);
1344 Comp_ADC_Present := Present (Comp_ADC);
1346 -- Case of record or array component: check storage order compatibility.
1347 -- But, if the record has Complex_Representation, then it is treated as
1348 -- a scalar in the back end so the storage order is irrelevant.
1350 if (Is_Record_Type (Comp_Base)
1351 and then not Has_Complex_Representation (Comp_Base))
1352 or else Is_Array_Type (Comp_Base)
1353 then
1354 Comp_SSO_Differs :=
1355 Reverse_Storage_Order (Encl_Base) /=
1356 Reverse_Storage_Order (Comp_Base);
1358 -- Parent and extension must have same storage order
1360 if Present (Comp) and then Chars (Comp) = Name_uParent then
1361 if Comp_SSO_Differs then
1362 Error_Msg_N
1363 ("record extension must have same scalar storage order as "
1364 & "parent", Err_Node);
1365 end if;
1367 -- If component and composite SSO differs, check that component
1368 -- falls on byte boundaries and isn't bit packed.
1370 elsif Comp_SSO_Differs then
1372 -- Component SSO differs from enclosing composite:
1374 -- Reject if composite is a bit-packed array, as it is rewritten
1375 -- into an array of scalars.
1377 if Is_Bit_Packed_Array (Encl_Base) then
1378 Error_Msg_N
1379 ("type of packed array must have same scalar storage order "
1380 & "as component", Err_Node);
1382 -- Reject if not byte aligned
1384 elsif Is_Record_Type (Encl_Base)
1385 and then not Comp_Byte_Aligned
1386 then
1387 if Present (Component_Clause (Comp)) then
1388 Error_Msg_N
1389 ("type of non-byte-aligned component must have same scalar"
1390 & " storage order as enclosing record", Err_Node);
1391 else
1392 Error_Msg_N
1393 ("type of packed component must have same scalar"
1394 & " storage order as enclosing record", Err_Node);
1395 end if;
1397 -- Warn if specified only for the outer composite
1399 elsif Present (ADC) and then No (Comp_ADC) then
1400 Error_Msg_NE
1401 ("scalar storage order specified for & does not apply to "
1402 & "component?", Err_Node, Encl_Base);
1403 end if;
1404 end if;
1406 -- Enclosing type has explicit SSO: non-composite component must not
1407 -- be aliased.
1409 elsif Present (ADC) and then Component_Aliased then
1410 Error_Msg_N
1411 ("aliased component not permitted for type with explicit "
1412 & "Scalar_Storage_Order", Err_Node);
1413 end if;
1414 end Check_Component_Storage_Order;
1416 -----------------------------
1417 -- Check_Debug_Info_Needed --
1418 -----------------------------
1420 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1421 begin
1422 if Debug_Info_Off (T) then
1423 return;
1425 elsif Comes_From_Source (T)
1426 or else Debug_Generated_Code
1427 or else Debug_Flag_VV
1428 or else Needs_Debug_Info (T)
1429 then
1430 Set_Debug_Info_Needed (T);
1431 end if;
1432 end Check_Debug_Info_Needed;
1434 -------------------------------
1435 -- Check_Expression_Function --
1436 -------------------------------
1438 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1439 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1440 -- Function to search for deferred constant
1442 -------------------
1443 -- Find_Constant --
1444 -------------------
1446 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1447 begin
1448 -- When a constant is initialized with the result of a dispatching
1449 -- call, the constant declaration is rewritten as a renaming of the
1450 -- displaced function result. This scenario is not a premature use of
1451 -- a constant even though the Has_Completion flag is not set.
1453 if Is_Entity_Name (Nod)
1454 and then Present (Entity (Nod))
1455 and then Ekind (Entity (Nod)) = E_Constant
1456 and then Scope (Entity (Nod)) = Current_Scope
1457 and then Nkind (Declaration_Node (Entity (Nod))) =
1458 N_Object_Declaration
1459 and then not Is_Imported (Entity (Nod))
1460 and then not Has_Completion (Entity (Nod))
1461 and then not (Present (Full_View (Entity (Nod)))
1462 and then Has_Completion (Full_View (Entity (Nod))))
1463 then
1464 Error_Msg_NE
1465 ("premature use of& in call or instance", N, Entity (Nod));
1467 elsif Nkind (Nod) = N_Attribute_Reference then
1468 Analyze (Prefix (Nod));
1470 if Is_Entity_Name (Prefix (Nod))
1471 and then Is_Type (Entity (Prefix (Nod)))
1472 then
1473 if Expander_Active then
1474 Check_Fully_Declared (Entity (Prefix (Nod)), N);
1475 end if;
1477 Freeze_Before (N, Entity (Prefix (Nod)));
1478 end if;
1479 end if;
1481 return OK;
1482 end Find_Constant;
1484 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1486 -- Local variables
1488 Decl : Node_Id;
1490 -- Start of processing for Check_Expression_Function
1492 begin
1493 Decl := Original_Node (Unit_Declaration_Node (Nam));
1495 -- The subprogram body created for the expression function is not
1496 -- itself a freeze point.
1498 if Scope (Nam) = Current_Scope
1499 and then Nkind (Decl) = N_Expression_Function
1500 and then Nkind (N) /= N_Subprogram_Body
1501 then
1502 Check_Deferred (Expression (Decl));
1503 end if;
1504 end Check_Expression_Function;
1506 --------------------------------
1507 -- Check_Inherited_Conditions --
1508 --------------------------------
1510 procedure Check_Inherited_Conditions
1511 (R : Entity_Id;
1512 Late_Overriding : Boolean := False)
1514 Prim_Ops : constant Elist_Id := Primitive_Operations (R);
1515 Decls : List_Id;
1516 Op_Node : Elmt_Id;
1517 Par_Prim : Entity_Id;
1518 Prim : Entity_Id;
1519 Wrapper_Needed : Boolean;
1521 function Build_DTW_Body
1522 (Loc : Source_Ptr;
1523 DTW_Spec : Node_Id;
1524 DTW_Decls : List_Id;
1525 Par_Prim : Entity_Id;
1526 Wrapped_Subp : Entity_Id) return Node_Id;
1527 -- Build the body of the dispatch table wrapper containing the given
1528 -- spec and declarations; the call to the wrapped subprogram includes
1529 -- the proper type conversion.
1531 function Build_DTW_Spec (Par_Prim : Entity_Id) return Node_Id;
1532 -- Build the spec of the dispatch table wrapper
1534 procedure Build_Inherited_Condition_Pragmas
1535 (Subp : Entity_Id;
1536 Wrapper_Needed : out Boolean);
1537 -- Build corresponding pragmas for an operation whose ancestor has
1538 -- class-wide pre/postconditions. If the operation is inherited then
1539 -- Wrapper_Needed is returned True to force the creation of a wrapper
1540 -- for the inherited operation. If the ancestor is being overridden,
1541 -- the pragmas are constructed only to verify their legality, in case
1542 -- they contain calls to other primitives that may have been overridden.
1544 function Needs_Wrapper
1545 (Class_Cond : Node_Id;
1546 Subp : Entity_Id;
1547 Par_Subp : Entity_Id) return Boolean;
1548 -- Checks whether the dispatch-table wrapper (DTW) for Subp must be
1549 -- built to evaluate the given class-wide condition.
1551 --------------------
1552 -- Build_DTW_Body --
1553 --------------------
1555 function Build_DTW_Body
1556 (Loc : Source_Ptr;
1557 DTW_Spec : Node_Id;
1558 DTW_Decls : List_Id;
1559 Par_Prim : Entity_Id;
1560 Wrapped_Subp : Entity_Id) return Node_Id
1562 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Par_Prim);
1563 Actuals : constant List_Id := Empty_List;
1564 Call : Node_Id;
1565 Formal : Entity_Id := First_Formal (Par_Prim);
1566 New_F_Spec : Entity_Id := First (Parameter_Specifications (DTW_Spec));
1567 New_Formal : Entity_Id;
1569 begin
1570 -- Build parameter association for call to wrapped subprogram
1572 while Present (Formal) loop
1573 New_Formal := Defining_Identifier (New_F_Spec);
1575 -- If the controlling argument is inherited, add conversion to
1576 -- parent type for the call.
1578 if Etype (Formal) = Par_Typ
1579 and then Is_Controlling_Formal (Formal)
1580 then
1581 Append_To (Actuals,
1582 Make_Type_Conversion (Loc,
1583 New_Occurrence_Of (Par_Typ, Loc),
1584 New_Occurrence_Of (New_Formal, Loc)));
1585 else
1586 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1587 end if;
1589 Next_Formal (Formal);
1590 Next (New_F_Spec);
1591 end loop;
1593 if Ekind (Wrapped_Subp) = E_Procedure then
1594 Call :=
1595 Make_Procedure_Call_Statement (Loc,
1596 Name => New_Occurrence_Of (Wrapped_Subp, Loc),
1597 Parameter_Associations => Actuals);
1598 else
1599 Call :=
1600 Make_Simple_Return_Statement (Loc,
1601 Expression =>
1602 Make_Function_Call (Loc,
1603 Name => New_Occurrence_Of (Wrapped_Subp, Loc),
1604 Parameter_Associations => Actuals));
1605 end if;
1607 return
1608 Make_Subprogram_Body (Loc,
1609 Specification => Copy_Subprogram_Spec (DTW_Spec),
1610 Declarations => DTW_Decls,
1611 Handled_Statement_Sequence =>
1612 Make_Handled_Sequence_Of_Statements (Loc,
1613 Statements => New_List (Call),
1614 End_Label => Make_Identifier (Loc,
1615 Chars (Defining_Entity (DTW_Spec)))));
1616 end Build_DTW_Body;
1618 --------------------
1619 -- Build_DTW_Spec --
1620 --------------------
1622 function Build_DTW_Spec (Par_Prim : Entity_Id) return Node_Id is
1623 DTW_Id : Entity_Id;
1624 DTW_Spec : Node_Id;
1626 begin
1627 DTW_Spec := Build_Overriding_Spec (Par_Prim, R);
1628 DTW_Id := Defining_Entity (DTW_Spec);
1630 -- Clear the not-overriding indicator since the DTW wrapper overrides
1631 -- its wrapped subprogram; required because if present in the parent
1632 -- primitive, given that Build_Overriding_Spec inherits it, we report
1633 -- spurious errors.
1635 Set_Must_Not_Override (DTW_Spec, False);
1637 -- Add minimal decoration of fields
1639 Mutate_Ekind (DTW_Id, Ekind (Par_Prim));
1640 Set_LSP_Subprogram (DTW_Id, Par_Prim);
1641 Set_Is_Dispatch_Table_Wrapper (DTW_Id);
1642 Set_Is_Wrapper (DTW_Id);
1644 -- The DTW wrapper is never a null procedure
1646 if Nkind (DTW_Spec) = N_Procedure_Specification then
1647 Set_Null_Present (DTW_Spec, False);
1648 end if;
1650 return DTW_Spec;
1651 end Build_DTW_Spec;
1653 ---------------------------------------
1654 -- Build_Inherited_Condition_Pragmas --
1655 ---------------------------------------
1657 procedure Build_Inherited_Condition_Pragmas
1658 (Subp : Entity_Id;
1659 Wrapper_Needed : out Boolean)
1661 Class_Pre : constant Node_Id :=
1662 Class_Preconditions (Ultimate_Alias (Subp));
1663 Class_Post : Node_Id := Class_Postconditions (Par_Prim);
1664 A_Post : Node_Id;
1665 New_Prag : Node_Id;
1667 begin
1668 Wrapper_Needed := False;
1670 if No (Class_Pre) and then No (Class_Post) then
1671 return;
1672 end if;
1674 -- For class-wide preconditions we just evaluate whether the wrapper
1675 -- is needed; there is no need to build the pragma since the check
1676 -- is performed on the caller side.
1678 if Present (Class_Pre)
1679 and then Needs_Wrapper (Class_Pre, Subp, Par_Prim)
1680 then
1681 Wrapper_Needed := True;
1682 end if;
1684 -- For class-wide postconditions we evaluate whether the wrapper is
1685 -- needed and we build the class-wide postcondition pragma to install
1686 -- it in the wrapper.
1688 if Present (Class_Post)
1689 and then Needs_Wrapper (Class_Post, Subp, Par_Prim)
1690 then
1691 Wrapper_Needed := True;
1693 -- Update the class-wide postcondition
1695 Class_Post := New_Copy_Tree (Class_Post);
1696 Build_Class_Wide_Expression
1697 (Pragma_Or_Expr => Class_Post,
1698 Subp => Subp,
1699 Par_Subp => Par_Prim,
1700 Adjust_Sloc => False);
1702 -- Install the updated class-wide postcondition in a copy of the
1703 -- pragma postcondition defined for the nearest ancestor.
1705 A_Post := Get_Class_Wide_Pragma (Par_Prim,
1706 Pragma_Postcondition);
1708 if No (A_Post) then
1709 declare
1710 Subps : constant Subprogram_List :=
1711 Inherited_Subprograms (Subp);
1712 begin
1713 for Index in Subps'Range loop
1714 A_Post := Get_Class_Wide_Pragma (Subps (Index),
1715 Pragma_Postcondition);
1716 exit when Present (A_Post);
1717 end loop;
1718 end;
1719 end if;
1721 New_Prag := New_Copy_Tree (A_Post);
1722 Rewrite
1723 (Expression (First (Pragma_Argument_Associations (New_Prag))),
1724 Class_Post);
1725 Append (New_Prag, Decls);
1726 end if;
1727 end Build_Inherited_Condition_Pragmas;
1729 -------------------
1730 -- Needs_Wrapper --
1731 -------------------
1733 function Needs_Wrapper
1734 (Class_Cond : Node_Id;
1735 Subp : Entity_Id;
1736 Par_Subp : Entity_Id) return Boolean
1738 Result : Boolean := False;
1740 function Check_Entity (N : Node_Id) return Traverse_Result;
1741 -- Check calls to overridden primitives
1743 --------------------
1744 -- Replace_Entity --
1745 --------------------
1747 function Check_Entity (N : Node_Id) return Traverse_Result is
1748 New_E : Entity_Id;
1750 begin
1751 if Nkind (N) = N_Identifier
1752 and then Present (Entity (N))
1753 and then
1754 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1755 and then
1756 (Nkind (Parent (N)) /= N_Attribute_Reference
1757 or else Attribute_Name (Parent (N)) /= Name_Class)
1758 then
1759 -- Determine whether entity has a renaming
1761 New_E := Get_Mapped_Entity (Entity (N));
1763 -- If the entity is an overridden primitive and we are not
1764 -- in GNATprove mode, we must build a wrapper for the current
1765 -- inherited operation. If the reference is the prefix of an
1766 -- attribute such as 'Result (or others ???) there is no need
1767 -- for a wrapper: the condition is just rewritten in terms of
1768 -- the inherited subprogram.
1770 if Present (New_E)
1771 and then Comes_From_Source (New_E)
1772 and then Is_Subprogram (New_E)
1773 and then Nkind (Parent (N)) /= N_Attribute_Reference
1774 and then not GNATprove_Mode
1775 then
1776 Result := True;
1777 return Abandon;
1778 end if;
1779 end if;
1781 return OK;
1782 end Check_Entity;
1784 procedure Check_Condition_Entities is
1785 new Traverse_Proc (Check_Entity);
1787 -- Start of processing for Needs_Wrapper
1789 begin
1790 Update_Primitives_Mapping (Par_Subp, Subp);
1792 Map_Formals (Par_Subp, Subp);
1793 Check_Condition_Entities (Class_Cond);
1795 return Result;
1796 end Needs_Wrapper;
1798 Ifaces_List : Elist_Id := No_Elist;
1799 Ifaces_Listed : Boolean := False;
1800 -- Cache the list of interface operations inherited by R
1802 Wrappers_List : Elist_Id := No_Elist;
1803 -- List containing identifiers of built wrappers. Used to defer building
1804 -- and analyzing their class-wide precondition subprograms.
1806 -- Start of processing for Check_Inherited_Conditions
1808 begin
1809 if Late_Overriding then
1810 Op_Node := First_Elmt (Prim_Ops);
1811 while Present (Op_Node) loop
1812 Prim := Node (Op_Node);
1814 -- Map the overridden primitive to the overriding one
1816 if Present (Overridden_Operation (Prim))
1817 and then Comes_From_Source (Prim)
1818 then
1819 Par_Prim := Overridden_Operation (Prim);
1820 Update_Primitives_Mapping (Par_Prim, Prim);
1822 -- Force discarding previous mappings of its formals
1824 Map_Formals (Par_Prim, Prim, Force_Update => True);
1825 end if;
1827 Next_Elmt (Op_Node);
1828 end loop;
1829 end if;
1831 -- Perform validity checks on the inherited conditions of overriding
1832 -- operations, for conformance with LSP, and apply SPARK-specific
1833 -- restrictions on inherited conditions.
1835 Op_Node := First_Elmt (Prim_Ops);
1836 while Present (Op_Node) loop
1837 Prim := Node (Op_Node);
1839 Par_Prim := Overridden_Operation (Prim);
1840 if Present (Par_Prim)
1841 and then Comes_From_Source (Prim)
1842 then
1843 -- When the primitive is an LSP wrapper we climb to the parent
1844 -- primitive that has the inherited contract.
1846 if Is_Wrapper (Par_Prim)
1847 and then Present (LSP_Subprogram (Par_Prim))
1848 then
1849 Par_Prim := LSP_Subprogram (Par_Prim);
1850 end if;
1852 -- Check that overrider and overridden operations have
1853 -- the same strub mode.
1855 Check_Same_Strub_Mode (Prim, Par_Prim);
1857 -- Analyze the contract items of the overridden operation, before
1858 -- they are rewritten as pragmas.
1860 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1862 -- In GNATprove mode this is where we can collect the inherited
1863 -- conditions, because we do not create the Check pragmas that
1864 -- normally convey the modified class-wide conditions on
1865 -- overriding operations.
1867 if GNATprove_Mode then
1868 Collect_Inherited_Class_Wide_Conditions (Prim);
1869 end if;
1870 end if;
1872 -- Go over operations inherited from interfaces and check
1873 -- them for strub mode compatibility as well.
1875 if Has_Interfaces (R)
1876 and then Is_Dispatching_Operation (Prim)
1877 and then Find_Dispatching_Type (Prim) = R
1878 then
1879 declare
1880 Elmt : Elmt_Id;
1881 Iface_Elmt : Elmt_Id;
1882 Iface : Entity_Id;
1883 Iface_Prim : Entity_Id;
1885 begin
1886 -- Collect the interfaces only once. We haven't
1887 -- finished freezing yet, so we can't use the faster
1888 -- search from Sem_Disp.Covered_Interface_Primitives.
1890 if not Ifaces_Listed then
1891 Collect_Interfaces (R, Ifaces_List);
1892 Ifaces_Listed := True;
1893 end if;
1895 Iface_Elmt := First_Elmt (Ifaces_List);
1896 while Present (Iface_Elmt) loop
1897 Iface := Node (Iface_Elmt);
1899 Elmt := First_Elmt (Primitive_Operations (Iface));
1900 while Present (Elmt) loop
1901 Iface_Prim := Node (Elmt);
1903 if Iface_Prim /= Par_Prim
1904 and then Chars (Iface_Prim) = Chars (Prim)
1905 and then Comes_From_Source (Iface_Prim)
1906 and then (Is_Interface_Conformant
1907 (R, Iface_Prim, Prim))
1908 then
1909 Check_Same_Strub_Mode (Prim, Iface_Prim);
1910 end if;
1912 Next_Elmt (Elmt);
1913 end loop;
1915 Next_Elmt (Iface_Elmt);
1916 end loop;
1917 end;
1918 end if;
1920 Next_Elmt (Op_Node);
1921 end loop;
1923 -- Now examine the inherited operations to check whether they require
1924 -- a wrapper to handle inherited conditions that call other primitives,
1925 -- so that LSP can be verified/enforced.
1927 Op_Node := First_Elmt (Prim_Ops);
1929 while Present (Op_Node) loop
1930 Decls := Empty_List;
1931 Prim := Node (Op_Node);
1932 Wrapper_Needed := False;
1934 -- Skip internal entities built for mapping interface primitives
1936 if not Comes_From_Source (Prim)
1937 and then Present (Alias (Prim))
1938 and then No (Interface_Alias (Prim))
1939 then
1940 Par_Prim := Ultimate_Alias (Prim);
1942 -- When the primitive is an LSP wrapper we climb to the parent
1943 -- primitive that has the inherited contract.
1945 if Is_Wrapper (Par_Prim)
1946 and then Present (LSP_Subprogram (Par_Prim))
1947 then
1948 Par_Prim := LSP_Subprogram (Par_Prim);
1949 end if;
1951 -- Analyze the contract items of the parent operation, and
1952 -- determine whether a wrapper is needed. This is determined
1953 -- when the condition is rewritten in sem_prag, using the
1954 -- mapping between overridden and overriding operations built
1955 -- in the loop above.
1957 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1958 Build_Inherited_Condition_Pragmas (Prim, Wrapper_Needed);
1959 end if;
1961 if Wrapper_Needed
1962 and then not Is_Abstract_Subprogram (Par_Prim)
1963 and then Expander_Active
1964 then
1965 -- Build the dispatch-table wrapper (DTW). The support for
1966 -- AI12-0195 relies on two kind of wrappers: one for indirect
1967 -- calls (also used for AI12-0220), and one for putting in the
1968 -- dispatch table:
1970 -- 1) "indirect-call wrapper" (ICW) is needed anytime there are
1971 -- class-wide preconditions. Prim'Access will point directly
1972 -- at the ICW if any, or at the "pristine" body if Prim has
1973 -- no class-wide preconditions.
1975 -- 2) "dispatch-table wrapper" (DTW) is needed anytime the class
1976 -- wide preconditions *or* the class-wide postconditions are
1977 -- affected by overriding.
1979 -- The DTW holds a single statement that is a single call where
1980 -- the controlling actuals are conversions to the corresponding
1981 -- type in the parent primitive. If the primitive is a function
1982 -- the statement is a return statement with a call.
1984 declare
1985 Alias_Id : constant Entity_Id := Ultimate_Alias (Prim);
1986 Loc : constant Source_Ptr := Sloc (R);
1987 DTW_Body : Node_Id;
1988 DTW_Decl : Node_Id;
1989 DTW_Id : Entity_Id;
1990 DTW_Spec : Node_Id;
1992 Prim_Next_E : constant Entity_Id := Next_Entity (Prim);
1993 Prim_Prev_E : constant Entity_Id := Prev_Entity (Prim);
1995 begin
1996 DTW_Spec := Build_DTW_Spec (Par_Prim);
1997 DTW_Id := Defining_Entity (DTW_Spec);
1998 DTW_Decl := Make_Subprogram_Declaration (Loc,
1999 Specification => DTW_Spec);
2001 -- The spec of the wrapper has been built using the source
2002 -- location of its parent primitive; we must update it now
2003 -- (with the source location of the internal primitive built
2004 -- by Derive_Subprogram that will override this wrapper) to
2005 -- avoid inlining conflicts between internally built helpers
2006 -- for class-wide pre/postconditions of the parent and the
2007 -- helpers built for this wrapper.
2009 Set_Sloc (DTW_Id, Sloc (Prim));
2011 -- For inherited class-wide preconditions the DTW wrapper
2012 -- reuses the ICW of the parent (which checks the parent
2013 -- interpretation of the class-wide preconditions); the
2014 -- interpretation of the class-wide preconditions for the
2015 -- inherited subprogram is checked at the caller side.
2017 -- When the subprogram inherits class-wide postconditions
2018 -- the DTW also checks the interpretation of the class-wide
2019 -- postconditions for the inherited subprogram, and the body
2020 -- of the parent checks its interpretation of the parent for
2021 -- the class-wide postconditions.
2023 -- procedure Prim (F1 : T1; ...) is
2024 -- [ pragma Check (Postcondition, Expr); ]
2025 -- begin
2026 -- Par_Prim_ICW (Par_Type (F1), ...);
2027 -- end;
2029 if Present (Indirect_Call_Wrapper (Par_Prim)) then
2030 DTW_Body :=
2031 Build_DTW_Body (Loc,
2032 DTW_Spec => DTW_Spec,
2033 DTW_Decls => Decls,
2034 Par_Prim => Par_Prim,
2035 Wrapped_Subp => Indirect_Call_Wrapper (Par_Prim));
2037 -- For subprograms that only inherit class-wide postconditions
2038 -- the DTW wrapper calls the parent primitive (which on its
2039 -- body checks the interpretation of the class-wide post-
2040 -- conditions for the parent subprogram), and the DTW checks
2041 -- the interpretation of the class-wide postconditions for the
2042 -- inherited subprogram.
2044 -- procedure Prim (F1 : T1; ...) is
2045 -- pragma Check (Postcondition, Expr);
2046 -- begin
2047 -- Par_Prim (Par_Type (F1), ...);
2048 -- end;
2050 else
2051 DTW_Body :=
2052 Build_DTW_Body (Loc,
2053 DTW_Spec => DTW_Spec,
2054 DTW_Decls => Decls,
2055 Par_Prim => Par_Prim,
2056 Wrapped_Subp => Par_Prim);
2057 end if;
2059 -- Insert the declaration of the wrapper before the freezing
2060 -- node of the record type declaration to ensure that it will
2061 -- override the internal primitive built by Derive_Subprogram.
2063 if Late_Overriding then
2064 Ensure_Freeze_Node (R);
2065 Insert_Before_And_Analyze (Freeze_Node (R), DTW_Decl);
2066 else
2067 Append_Freeze_Action (R, DTW_Decl);
2068 Analyze (DTW_Decl);
2069 end if;
2071 -- The analyis of DTW_Decl has removed Prim from its scope
2072 -- chain and added DTW_Id at the end of the scope chain. Move
2073 -- DTW_Id to its correct place in the scope chain: the analysis
2074 -- of the wrapper declaration has just added DTW_Id at the end
2075 -- of the list of entities of its scope. However, given that
2076 -- this wrapper overrides Prim, we must move DTW_Id to the
2077 -- original place of Prim in its scope chain. This is required
2078 -- for wrappers of private type primitives to ensure their
2079 -- correct visibility since wrappers are built when the full
2080 -- tagged type declaration is frozen (in the private part of
2081 -- the package) but they may override primitives defined in the
2082 -- public part of the package.
2084 declare
2085 DTW_Prev_E : constant Entity_Id := Prev_Entity (DTW_Id);
2087 begin
2088 pragma Assert (Last_Entity (Current_Scope) = DTW_Id);
2089 pragma Assert
2090 (Ekind (Current_Scope) not in E_Package | E_Generic_Package
2091 or else No (First_Private_Entity (Current_Scope))
2092 or else First_Private_Entity (Current_Scope) /= DTW_Id);
2094 -- Remove DTW_Id from the end of the doubly-linked list of
2095 -- entities of this scope; no need to handle removing it
2096 -- from the beginning of the chain since such case can never
2097 -- occur for this entity.
2099 Set_Last_Entity (Current_Scope, DTW_Prev_E);
2100 Set_Next_Entity (DTW_Prev_E, Empty);
2102 -- Place DTW_Id back in the original place of its wrapped
2103 -- primitive in the list of entities of this scope.
2105 Link_Entities (Prim_Prev_E, DTW_Id);
2106 Link_Entities (DTW_Id, Prim_Next_E);
2107 end;
2109 -- Insert the body of the wrapper in the freeze actions of
2110 -- its record type declaration to ensure that it is placed
2111 -- in the scope of its declaration but not too early to cause
2112 -- premature freezing of other entities.
2114 Append_Freeze_Action (R, DTW_Body);
2115 Analyze (DTW_Body);
2117 -- Ensure correct decoration
2119 pragma Assert (Is_Dispatching_Operation (DTW_Id));
2120 pragma Assert (Present (Overridden_Operation (DTW_Id)));
2121 pragma Assert (Overridden_Operation (DTW_Id) = Alias_Id);
2123 -- Inherit dispatch table slot
2125 Set_DTC_Entity_Value (R, DTW_Id);
2126 Set_DT_Position (DTW_Id, DT_Position (Alias_Id));
2128 -- Register the wrapper in the dispatch table
2130 if Late_Overriding
2131 and then not Building_Static_DT (R)
2132 then
2133 Insert_List_After_And_Analyze (Freeze_Node (R),
2134 Register_Primitive (Loc, DTW_Id));
2135 end if;
2137 -- Defer building helpers and ICW for the DTW. Required to
2138 -- ensure uniqueness in their names because when building
2139 -- these wrappers for overlapped subprograms their homonym
2140 -- number is not definite until all these dispatch table
2141 -- wrappers of tagged type R have been analyzed.
2143 if Present (Indirect_Call_Wrapper (Par_Prim)) then
2144 Append_New_Elmt (DTW_Id, Wrappers_List);
2145 end if;
2146 end;
2147 end if;
2149 Next_Elmt (Op_Node);
2150 end loop;
2152 -- Build and analyze deferred class-wide precondition subprograms of
2153 -- built wrappers.
2155 if Present (Wrappers_List) then
2156 declare
2157 Body_N : Node_Id;
2158 CW_Subp : Entity_Id;
2159 Decl_N : Node_Id;
2160 DTW_Id : Entity_Id;
2161 Elmt : Elmt_Id;
2163 begin
2164 Elmt := First_Elmt (Wrappers_List);
2166 while Present (Elmt) loop
2167 DTW_Id := Node (Elmt);
2168 Next_Elmt (Elmt);
2170 Merge_Class_Conditions (DTW_Id);
2171 Make_Class_Precondition_Subps (DTW_Id, Late_Overriding);
2173 CW_Subp := Static_Call_Helper (DTW_Id);
2174 Decl_N := Unit_Declaration_Node (CW_Subp);
2175 Analyze (Decl_N);
2177 -- If the DTW was built for a late-overriding primitive
2178 -- its body must be analyzed now (since the tagged type
2179 -- is already frozen).
2181 if Late_Overriding then
2182 Body_N :=
2183 Unit_Declaration_Node (Corresponding_Body (Decl_N));
2184 Analyze (Body_N);
2185 end if;
2186 end loop;
2187 end;
2188 end if;
2189 end Check_Inherited_Conditions;
2191 ----------------------------
2192 -- Check_Strict_Alignment --
2193 ----------------------------
2195 procedure Check_Strict_Alignment (E : Entity_Id) is
2196 Comp : Entity_Id;
2198 begin
2199 -- Bit-packed array types do not require strict alignment, even if they
2200 -- are by-reference types, because they are accessed in a special way.
2202 if Is_By_Reference_Type (E) and then not Is_Bit_Packed_Array (E) then
2203 Set_Strict_Alignment (E);
2205 elsif Is_Array_Type (E) then
2206 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
2208 -- ??? AI12-001: Any component of a packed type that contains an
2209 -- aliased part must be aligned according to the alignment of its
2210 -- subtype (RM 13.2(7)). This means that the following test:
2212 -- if Has_Aliased_Components (E) then
2213 -- Set_Strict_Alignment (E);
2214 -- end if;
2216 -- should be implemented here. Unfortunately it would break Florist,
2217 -- which has the bad habit of overaligning all the types it declares
2218 -- on 32-bit platforms. Other legacy codebases could also be affected
2219 -- because this check has historically been missing in GNAT.
2221 elsif Is_Record_Type (E) then
2222 Comp := First_Component (E);
2223 while Present (Comp) loop
2224 if not Is_Type (Comp)
2225 and then (Is_Aliased (Comp)
2226 or else Strict_Alignment (Etype (Comp)))
2227 then
2228 Set_Strict_Alignment (E);
2229 return;
2230 end if;
2232 Next_Component (Comp);
2233 end loop;
2234 end if;
2235 end Check_Strict_Alignment;
2237 -------------------------
2238 -- Check_Unsigned_Type --
2239 -------------------------
2241 procedure Check_Unsigned_Type (E : Entity_Id) is
2242 Ancestor : Entity_Id;
2243 Lo_Bound : Node_Id;
2244 Btyp : Entity_Id;
2246 begin
2247 if not Is_Discrete_Or_Fixed_Point_Type (E) then
2248 return;
2249 end if;
2251 -- Do not attempt to analyze case where range was in error
2253 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
2254 return;
2255 end if;
2257 -- The situation that is nontrivial is something like:
2259 -- subtype x1 is integer range -10 .. +10;
2260 -- subtype x2 is x1 range 0 .. V1;
2261 -- subtype x3 is x2 range V2 .. V3;
2262 -- subtype x4 is x3 range V4 .. V5;
2264 -- where Vn are variables. Here the base type is signed, but we still
2265 -- know that x4 is unsigned because of the lower bound of x2.
2267 -- The only way to deal with this is to look up the ancestor chain
2269 Ancestor := E;
2270 loop
2271 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
2272 return;
2273 end if;
2275 Lo_Bound := Type_Low_Bound (Ancestor);
2277 if Compile_Time_Known_Value (Lo_Bound) then
2278 if Expr_Rep_Value (Lo_Bound) >= 0 then
2279 Set_Is_Unsigned_Type (E, True);
2280 end if;
2282 return;
2284 else
2285 Ancestor := Ancestor_Subtype (Ancestor);
2287 -- If no ancestor had a static lower bound, go to base type
2289 if No (Ancestor) then
2291 -- Note: the reason we still check for a compile time known
2292 -- value for the base type is that at least in the case of
2293 -- generic formals, we can have bounds that fail this test,
2294 -- and there may be other cases in error situations.
2296 Btyp := Base_Type (E);
2298 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
2299 return;
2300 end if;
2302 Lo_Bound := Type_Low_Bound (Base_Type (E));
2304 if Compile_Time_Known_Value (Lo_Bound)
2305 and then Expr_Rep_Value (Lo_Bound) >= 0
2306 then
2307 Set_Is_Unsigned_Type (E, True);
2308 end if;
2310 return;
2311 end if;
2312 end if;
2313 end loop;
2314 end Check_Unsigned_Type;
2316 -----------------------------------------------
2317 -- Explode_Initialization_Compound_Statement --
2318 -----------------------------------------------
2320 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
2321 Init_Stmts : constant Node_Id := Initialization_Statements (E);
2323 begin
2324 if Present (Init_Stmts)
2325 and then Nkind (Init_Stmts) = N_Compound_Statement
2326 then
2327 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
2329 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
2330 -- just removing it, because Freeze_All may rely on this particular
2331 -- Node_Id still being present in the enclosing list to know where to
2332 -- stop freezing.
2334 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
2336 Set_Initialization_Statements (E, Empty);
2337 end if;
2338 end Explode_Initialization_Compound_Statement;
2340 ----------------
2341 -- Freeze_All --
2342 ----------------
2344 -- Note: the easy coding for this procedure would be to just build a
2345 -- single list of freeze nodes and then insert them and analyze them
2346 -- all at once. This won't work, because the analysis of earlier freeze
2347 -- nodes may recursively freeze types which would otherwise appear later
2348 -- on in the freeze list. So we must analyze and expand the freeze nodes
2349 -- as they are generated.
2351 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
2352 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
2353 -- This is the internal recursive routine that does freezing of entities
2354 -- (but NOT the analysis of default expressions, which should not be
2355 -- recursive, we don't want to analyze those till we are sure that ALL
2356 -- the types are frozen).
2358 --------------------
2359 -- Freeze_All_Ent --
2360 --------------------
2362 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
2363 E : Entity_Id;
2364 Flist : List_Id;
2366 procedure Process_Flist;
2367 -- If freeze nodes are present, insert and analyze, and reset cursor
2368 -- for next insertion.
2370 -------------------
2371 -- Process_Flist --
2372 -------------------
2374 procedure Process_Flist is
2375 Lastn : Node_Id;
2376 begin
2377 if Is_Non_Empty_List (Flist) then
2378 Lastn := Next (After);
2379 Insert_List_After_And_Analyze (After, Flist);
2381 if Present (Lastn) then
2382 After := Prev (Lastn);
2383 else
2384 After := Last (List_Containing (After));
2385 end if;
2386 end if;
2387 end Process_Flist;
2389 -- Start of processing for Freeze_All_Ent
2391 begin
2392 E := From;
2393 while Present (E) loop
2395 -- If the entity is an inner package which is not a package
2396 -- renaming, then its entities must be frozen at this point. Note
2397 -- that such entities do NOT get frozen at the end of the nested
2398 -- package itself (only library packages freeze).
2400 -- Same is true for task declarations, where anonymous records
2401 -- created for entry parameters must be frozen.
2403 if Ekind (E) = E_Package
2404 and then No (Renamed_Entity (E))
2405 and then not Is_Child_Unit (E)
2406 and then not Is_Frozen (E)
2407 then
2408 Push_Scope (E);
2410 Install_Visible_Declarations (E);
2411 Install_Private_Declarations (E);
2412 Freeze_All (First_Entity (E), After);
2414 End_Package_Scope (E);
2416 if Is_Generic_Instance (E)
2417 and then Has_Delayed_Freeze (E)
2418 then
2419 Set_Has_Delayed_Freeze (E, False);
2420 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
2421 end if;
2423 elsif Ekind (E) in Task_Kind
2424 and then Nkind (Parent (E)) in
2425 N_Single_Task_Declaration | N_Task_Type_Declaration
2426 then
2427 Push_Scope (E);
2428 Freeze_All (First_Entity (E), After);
2429 End_Scope;
2431 -- For a derived tagged type, we must ensure that all the
2432 -- primitive operations of the parent have been frozen, so that
2433 -- their addresses will be in the parent's dispatch table at the
2434 -- point it is inherited.
2436 elsif Ekind (E) = E_Record_Type
2437 and then Is_Tagged_Type (E)
2438 and then Is_Tagged_Type (Etype (E))
2439 and then Is_Derived_Type (E)
2440 then
2441 declare
2442 Prim_List : constant Elist_Id :=
2443 Primitive_Operations (Etype (E));
2445 Prim : Elmt_Id;
2446 Subp : Entity_Id;
2448 begin
2449 Prim := First_Elmt (Prim_List);
2450 while Present (Prim) loop
2451 Subp := Node (Prim);
2453 if Comes_From_Source (Subp)
2454 and then not Is_Frozen (Subp)
2455 then
2456 Flist := Freeze_Entity (Subp, After);
2457 Process_Flist;
2458 end if;
2460 Next_Elmt (Prim);
2461 end loop;
2462 end;
2463 end if;
2465 if not Is_Frozen (E) then
2466 Flist := Freeze_Entity (E, After);
2467 Process_Flist;
2469 -- If already frozen, and there are delayed aspects, this is where
2470 -- we do the visibility check for these aspects (see Sem_Ch13 spec
2471 -- for a description of how we handle aspect visibility).
2473 elsif Has_Delayed_Aspects (E) then
2474 declare
2475 Ritem : Node_Id;
2477 begin
2478 Ritem := First_Rep_Item (E);
2479 while Present (Ritem) loop
2480 if Nkind (Ritem) = N_Aspect_Specification
2481 and then Entity (Ritem) = E
2482 and then Is_Delayed_Aspect (Ritem)
2483 then
2484 Check_Aspect_At_End_Of_Declarations (Ritem);
2485 end if;
2487 Next_Rep_Item (Ritem);
2488 end loop;
2489 end;
2490 end if;
2492 -- If an incomplete type is still not frozen, this may be a
2493 -- premature freezing because of a body declaration that follows.
2494 -- Indicate where the freezing took place. Freezing will happen
2495 -- if the body comes from source, but not if it is internally
2496 -- generated, for example as the body of a type invariant.
2498 -- If the freezing is caused by the end of the current declarative
2499 -- part, it is a Taft Amendment type, and there is no error.
2501 if not Is_Frozen (E)
2502 and then Ekind (E) = E_Incomplete_Type
2503 then
2504 declare
2505 Bod : constant Node_Id := Next (After);
2507 begin
2508 -- The presence of a body freezes all entities previously
2509 -- declared in the current list of declarations, but this
2510 -- does not apply if the body does not come from source.
2511 -- A type invariant is transformed into a subprogram body
2512 -- which is placed at the end of the private part of the
2513 -- current package, but this body does not freeze incomplete
2514 -- types that may be declared in this private part.
2516 if Comes_From_Source (Bod)
2517 and then Nkind (Bod) in N_Entry_Body
2518 | N_Package_Body
2519 | N_Protected_Body
2520 | N_Subprogram_Body
2521 | N_Task_Body
2522 | N_Body_Stub
2523 and then
2524 In_Same_List (After, Parent (E))
2525 then
2526 Error_Msg_Sloc := Sloc (Next (After));
2527 Error_Msg_NE
2528 ("type& is frozen# before its full declaration",
2529 Parent (E), E);
2530 end if;
2531 end;
2532 end if;
2534 Next_Entity (E);
2535 end loop;
2536 end Freeze_All_Ent;
2538 -- Local variables
2540 Decl : Node_Id;
2541 E : Entity_Id;
2542 Item : Entity_Id;
2544 -- Start of processing for Freeze_All
2546 begin
2547 Freeze_All_Ent (From, After);
2549 -- Now that all types are frozen, we can deal with default expressions
2550 -- that require us to build a default expression functions. This is the
2551 -- point at which such functions are constructed (after all types that
2552 -- might be used in such expressions have been frozen).
2554 -- For subprograms that are renaming_as_body, we create the wrapper
2555 -- bodies as needed.
2557 -- We also add finalization chains to access types whose designated
2558 -- types are controlled. This is normally done when freezing the type,
2559 -- but this misses recursive type definitions where the later members
2560 -- of the recursion introduce controlled components.
2562 -- Loop through entities
2564 E := From;
2565 while Present (E) loop
2566 if Is_Subprogram (E) then
2567 if not Default_Expressions_Processed (E) then
2568 Process_Default_Expressions (E, After);
2569 end if;
2571 -- Check subprogram renamings for the same strub-mode.
2572 -- Avoid rechecking dispatching operations, that's taken
2573 -- care of in Check_Inherited_Conditions, that covers
2574 -- inherited interface operations.
2576 Item := Alias (E);
2577 if Present (Item)
2578 and then not Is_Dispatching_Operation (E)
2579 then
2580 Check_Same_Strub_Mode (E, Item);
2581 end if;
2583 if not Has_Completion (E) then
2584 Decl := Unit_Declaration_Node (E);
2586 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
2587 if Error_Posted (Decl) then
2588 Set_Has_Completion (E);
2589 else
2590 Build_And_Analyze_Renamed_Body (Decl, E, After);
2591 end if;
2593 elsif Nkind (Decl) = N_Subprogram_Declaration
2594 and then Present (Corresponding_Body (Decl))
2595 and then
2596 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl))) =
2597 N_Subprogram_Renaming_Declaration
2598 then
2599 Build_And_Analyze_Renamed_Body
2600 (Decl, Corresponding_Body (Decl), After);
2601 end if;
2602 end if;
2604 -- Freeze the default expressions of entries, entry families, and
2605 -- protected subprograms.
2607 elsif Is_Concurrent_Type (E) then
2608 Item := First_Entity (E);
2609 while Present (Item) loop
2610 if Is_Subprogram_Or_Entry (Item)
2611 and then not Default_Expressions_Processed (Item)
2612 then
2613 Process_Default_Expressions (Item, After);
2614 end if;
2616 Next_Entity (Item);
2617 end loop;
2618 end if;
2620 -- Historical note: We used to create a finalization master for an
2621 -- access type whose designated type is not controlled, but contains
2622 -- private controlled compoments. This form of postprocessing is no
2623 -- longer needed because the finalization master is now created when
2624 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
2626 Next_Entity (E);
2627 end loop;
2628 end Freeze_All;
2630 -----------------------
2631 -- Freeze_And_Append --
2632 -----------------------
2634 procedure Freeze_And_Append
2635 (Ent : Entity_Id;
2636 N : Node_Id;
2637 Result : in out List_Id)
2639 -- Freezing an Expression_Function does not freeze its profile:
2640 -- the formals will have been frozen otherwise before the E_F
2641 -- can be called.
2643 L : constant List_Id :=
2644 Freeze_Entity
2645 (Ent, N, Do_Freeze_Profile => not Is_Expression_Function (Ent));
2646 begin
2647 if Is_Non_Empty_List (L) then
2648 if Result = No_List then
2649 Result := L;
2650 else
2651 Append_List (L, Result);
2652 end if;
2653 end if;
2654 end Freeze_And_Append;
2656 -------------------
2657 -- Freeze_Before --
2658 -------------------
2660 procedure Freeze_Before
2661 (N : Node_Id;
2662 T : Entity_Id;
2663 Do_Freeze_Profile : Boolean := True)
2665 -- Freeze T, then insert the generated Freeze nodes before the node N.
2666 -- Flag Freeze_Profile is used when T is an overloadable entity, and
2667 -- indicates whether its profile should be frozen at the same time.
2669 Freeze_Nodes : constant List_Id :=
2670 Freeze_Entity (T, N, Do_Freeze_Profile);
2671 Pack : constant Entity_Id := Scope (T);
2673 begin
2674 if Ekind (T) = E_Function then
2675 Check_Expression_Function (N, T);
2676 end if;
2678 if Is_Non_Empty_List (Freeze_Nodes) then
2680 -- If the entity is a type declared in an inner package, it may be
2681 -- frozen by an outer declaration before the package itself is
2682 -- frozen. Install the package scope to analyze the freeze nodes,
2683 -- which may include generated subprograms such as predicate
2684 -- functions, etc.
2686 if Is_Type (T) and then From_Nested_Package (T) then
2687 Push_Scope (Pack);
2688 Install_Visible_Declarations (Pack);
2689 Install_Private_Declarations (Pack);
2690 Insert_Actions (N, Freeze_Nodes);
2691 End_Package_Scope (Pack);
2693 else
2694 Insert_Actions (N, Freeze_Nodes);
2695 end if;
2696 end if;
2697 end Freeze_Before;
2699 -------------------
2700 -- Freeze_Entity --
2701 -------------------
2703 -- WARNING: This routine manages Ghost regions. Return statements must be
2704 -- replaced by gotos which jump to the end of the routine and restore the
2705 -- Ghost mode.
2707 function Freeze_Entity
2708 (E : Entity_Id;
2709 N : Node_Id;
2710 Do_Freeze_Profile : Boolean := True) return List_Id
2712 Loc : constant Source_Ptr := Sloc (N);
2714 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2715 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2716 -- Save the Ghost-related attributes to restore on exit
2718 Atype : Entity_Id;
2719 Comp : Entity_Id;
2720 F_Node : Node_Id;
2721 Formal : Entity_Id;
2722 Indx : Node_Id;
2724 Result : List_Id := No_List;
2725 -- List of freezing actions, left at No_List if none
2727 Test_E : Entity_Id := E;
2728 -- A local temporary used to test if freezing is necessary for E, since
2729 -- its value can be set to something other than E in certain cases. For
2730 -- example, E cannot be used directly in cases such as when it is an
2731 -- Itype defined within a record - since it is the location of record
2732 -- which matters.
2734 procedure Add_To_Result (Fnod : Node_Id);
2735 -- Add freeze action Fnod to list Result
2737 function After_Last_Declaration return Boolean;
2738 -- If Loc is a freeze_entity that appears after the last declaration
2739 -- in the scope, inhibit error messages on late completion.
2741 procedure Check_Current_Instance (Comp_Decl : Node_Id);
2742 -- Check that an Access or Unchecked_Access attribute with a prefix
2743 -- which is the current instance type can only be applied when the type
2744 -- is limited.
2746 procedure Check_No_Parts_Violations
2747 (Typ : Entity_Id; Aspect_No_Parts : Aspect_Id) with
2748 Pre => Aspect_No_Parts in
2749 Aspect_No_Controlled_Parts | Aspect_No_Task_Parts;
2750 -- Check that Typ does not violate the semantics of the specified
2751 -- Aspect_No_Parts (No_Controlled_Parts or No_Task_Parts) when it is
2752 -- specified on Typ or one of its ancestors.
2754 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id);
2755 -- Give a warning for pragma Convention with language C or C++ applied
2756 -- to a discriminated record type. This is suppressed for the unchecked
2757 -- union case, since the whole point in this case is interface C. We
2758 -- also do not generate this within instantiations, since we will have
2759 -- generated a message on the template.
2761 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
2762 -- Give warning for modulus of 8, 16, 32, 64 or 128 given as an explicit
2763 -- integer literal without an explicit corresponding size clause. The
2764 -- caller has checked that Utype is a modular integer type.
2766 procedure Freeze_Array_Type (Arr : Entity_Id);
2767 -- Freeze array type, including freezing index and component types
2769 procedure Freeze_Object_Declaration (E : Entity_Id);
2770 -- Perform checks and generate freeze node if needed for a constant or
2771 -- variable declared by an object declaration.
2773 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
2774 -- Create Freeze_Generic_Entity nodes for types declared in a generic
2775 -- package. Recurse on inner generic packages.
2777 function Freeze_Profile (E : Entity_Id) return Boolean;
2778 -- Freeze formals and return type of subprogram. If some type in the
2779 -- profile is incomplete and we are in an instance, freezing of the
2780 -- entity will take place elsewhere, and the function returns False.
2782 procedure Freeze_Record_Type (Rec : Entity_Id);
2783 -- Freeze record type, including freezing component types, and freezing
2784 -- primitive operations if this is a tagged type.
2786 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2787 -- Determine whether an arbitrary entity is subject to Boolean aspect
2788 -- Import and its value is specified as True.
2790 procedure Inherit_Freeze_Node
2791 (Fnod : Node_Id;
2792 Typ : Entity_Id);
2793 -- Set type Typ's freeze node to refer to Fnode. This routine ensures
2794 -- that any attributes attached to Typ's original node are preserved.
2796 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2797 -- If E is an entity for an imported subprogram with pre/post-conditions
2798 -- then this procedure will create a wrapper to ensure that proper run-
2799 -- time checking of the pre/postconditions. See body for details.
2801 -------------------
2802 -- Add_To_Result --
2803 -------------------
2805 procedure Add_To_Result (Fnod : Node_Id) is
2806 begin
2807 Append_New_To (Result, Fnod);
2808 end Add_To_Result;
2810 ----------------------------
2811 -- After_Last_Declaration --
2812 ----------------------------
2814 function After_Last_Declaration return Boolean is
2815 Spec : constant Node_Id := Parent (Current_Scope);
2817 begin
2818 if Nkind (Spec) = N_Package_Specification then
2819 if Present (Private_Declarations (Spec)) then
2820 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2821 elsif Present (Visible_Declarations (Spec)) then
2822 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2823 else
2824 return False;
2825 end if;
2827 else
2828 return False;
2829 end if;
2830 end After_Last_Declaration;
2832 ----------------------------
2833 -- Check_Current_Instance --
2834 ----------------------------
2836 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2838 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2839 -- Determine whether Typ is compatible with the rules for aliased
2840 -- views of types as defined in RM 3.10 in the various dialects.
2842 function Process (N : Node_Id) return Traverse_Result;
2843 -- Process routine to apply check to given node
2845 -----------------------------
2846 -- Is_Aliased_View_Of_Type --
2847 -----------------------------
2849 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2850 Typ_Decl : constant Node_Id := Parent (Typ);
2852 begin
2853 -- Common case
2855 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2856 and then Limited_Present (Type_Definition (Typ_Decl))
2857 then
2858 return True;
2860 -- The following paragraphs describe what a legal aliased view of
2861 -- a type is in the various dialects of Ada.
2863 -- Ada 95
2865 -- The current instance of a limited type, and a formal parameter
2866 -- or generic formal object of a tagged type.
2868 -- Ada 95 limited type
2869 -- * Type with reserved word "limited"
2870 -- * A protected or task type
2871 -- * A composite type with limited component
2873 elsif Ada_Version <= Ada_95 then
2874 return Is_Limited_Type (Typ);
2876 -- Ada 2005
2878 -- The current instance of a limited tagged type, a protected
2879 -- type, a task type, or a type that has the reserved word
2880 -- "limited" in its full definition ... a formal parameter or
2881 -- generic formal object of a tagged type.
2883 -- Ada 2005 limited type
2884 -- * Type with reserved word "limited", "synchronized", "task"
2885 -- or "protected"
2886 -- * A composite type with limited component
2887 -- * A derived type whose parent is a non-interface limited type
2889 elsif Ada_Version = Ada_2005 then
2890 return
2891 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2892 or else
2893 (Is_Derived_Type (Typ)
2894 and then not Is_Interface (Etype (Typ))
2895 and then Is_Limited_Type (Etype (Typ)));
2897 -- Ada 2012 and beyond
2899 -- The current instance of an immutably limited type ... a formal
2900 -- parameter or generic formal object of a tagged type.
2902 -- Ada 2012 limited type
2903 -- * Type with reserved word "limited", "synchronized", "task"
2904 -- or "protected"
2905 -- * A composite type with limited component
2906 -- * A derived type whose parent is a non-interface limited type
2907 -- * An incomplete view
2909 -- Ada 2012 immutably limited type
2910 -- * Explicitly limited record type
2911 -- * Record extension with "limited" present
2912 -- * Non-formal limited private type that is either tagged
2913 -- or has at least one access discriminant with a default
2914 -- expression
2915 -- * Task type, protected type or synchronized interface
2916 -- * Type derived from immutably limited type
2918 else
2919 return
2920 Is_Immutably_Limited_Type (Typ)
2921 or else Is_Incomplete_Type (Typ);
2922 end if;
2923 end Is_Aliased_View_Of_Type;
2925 -------------
2926 -- Process --
2927 -------------
2929 function Process (N : Node_Id) return Traverse_Result is
2930 begin
2931 case Nkind (N) is
2932 when N_Attribute_Reference =>
2933 if Attribute_Name (N) in Name_Access | Name_Unchecked_Access
2934 and then Is_Entity_Name (Prefix (N))
2935 and then Is_Type (Entity (Prefix (N)))
2936 and then Entity (Prefix (N)) = E
2937 then
2938 if Ada_Version < Ada_2012 then
2939 Error_Msg_N
2940 ("current instance must be a limited type",
2941 Prefix (N));
2942 else
2943 Error_Msg_N
2944 ("current instance must be an immutably limited "
2945 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2946 end if;
2948 return Abandon;
2950 else
2951 return OK;
2952 end if;
2954 when others =>
2955 return OK;
2956 end case;
2957 end Process;
2959 procedure Traverse is new Traverse_Proc (Process);
2961 -- Local variables
2963 Rec_Type : constant Entity_Id :=
2964 Scope (Defining_Identifier (Comp_Decl));
2966 -- Start of processing for Check_Current_Instance
2968 begin
2969 if not Is_Aliased_View_Of_Type (Rec_Type) then
2970 Traverse (Comp_Decl);
2971 end if;
2972 end Check_Current_Instance;
2974 -------------------------------
2975 -- Check_No_Parts_Violations --
2976 -------------------------------
2978 procedure Check_No_Parts_Violations
2979 (Typ : Entity_Id; Aspect_No_Parts : Aspect_Id)
2982 function Find_Aspect_No_Parts
2983 (Typ : Entity_Id) return Node_Id;
2984 -- Search for Aspect_No_Parts on a given type. When
2985 -- the aspect is not explicity specified Empty is returned.
2987 function Get_Aspect_No_Parts_Value
2988 (Typ : Entity_Id) return Entity_Id;
2989 -- Obtain the value for the Aspect_No_Parts on a given
2990 -- type. When the aspect is not explicitly specified Empty is
2991 -- returned.
2993 function Has_Aspect_No_Parts
2994 (Typ : Entity_Id) return Boolean;
2995 -- Predicate function which identifies whether No_Parts
2996 -- is explicitly specified on a given type.
2998 -------------------------------------
2999 -- Find_Aspect_No_Parts --
3000 -------------------------------------
3002 function Find_Aspect_No_Parts
3003 (Typ : Entity_Id) return Node_Id
3005 Partial_View : constant Entity_Id :=
3006 Incomplete_Or_Partial_View (Typ);
3008 Aspect_Spec : Entity_Id :=
3009 Find_Aspect (Typ, Aspect_No_Parts);
3010 Curr_Aspect_Spec : Entity_Id;
3011 begin
3013 -- Examine Typ's associated node, when present, since aspect
3014 -- specifications do not get transferred when nodes get rewritten.
3016 -- For example, this can happen in the expansion of array types
3018 if No (Aspect_Spec)
3019 and then Present (Associated_Node_For_Itype (Typ))
3020 and then Nkind (Associated_Node_For_Itype (Typ))
3021 = N_Full_Type_Declaration
3022 then
3023 Aspect_Spec :=
3024 Find_Aspect
3025 (Id => Defining_Identifier
3026 (Associated_Node_For_Itype (Typ)),
3027 A => Aspect_No_Parts);
3028 end if;
3030 -- Examine aspects specifications on private type declarations
3032 -- Should Find_Aspect be improved to handle this case ???
3034 if No (Aspect_Spec)
3035 and then Present (Partial_View)
3036 and then Present
3037 (Aspect_Specifications
3038 (Declaration_Node
3039 (Partial_View)))
3040 then
3041 Curr_Aspect_Spec :=
3042 First
3043 (Aspect_Specifications
3044 (Declaration_Node
3045 (Partial_View)));
3047 -- Search through aspects present on the private type
3049 while Present (Curr_Aspect_Spec) loop
3050 if Get_Aspect_Id (Curr_Aspect_Spec)
3051 = Aspect_No_Parts
3052 then
3053 Aspect_Spec := Curr_Aspect_Spec;
3054 exit;
3055 end if;
3057 Next (Curr_Aspect_Spec);
3058 end loop;
3060 end if;
3062 -- When errors are posted on the aspect return Empty
3064 if Error_Posted (Aspect_Spec) then
3065 return Empty;
3066 end if;
3068 return Aspect_Spec;
3069 end Find_Aspect_No_Parts;
3071 ------------------------------------------
3072 -- Get_Aspect_No_Parts_Value --
3073 ------------------------------------------
3075 function Get_Aspect_No_Parts_Value
3076 (Typ : Entity_Id) return Entity_Id
3078 Aspect_Spec : constant Entity_Id :=
3079 Find_Aspect_No_Parts (Typ);
3080 begin
3082 -- Return the value of the aspect when present
3084 if Present (Aspect_Spec) then
3086 -- No expression is the same as True
3088 if No (Expression (Aspect_Spec)) then
3089 return Standard_True;
3090 end if;
3092 -- Assume its expression has already been constant folded into
3093 -- a Boolean value and return its value.
3095 return Entity (Expression (Aspect_Spec));
3096 end if;
3098 -- Otherwise, the aspect is not specified - so return Empty
3100 return Empty;
3101 end Get_Aspect_No_Parts_Value;
3103 ------------------------------------
3104 -- Has_Aspect_No_Parts --
3105 ------------------------------------
3107 function Has_Aspect_No_Parts
3108 (Typ : Entity_Id) return Boolean
3109 is (Present (Find_Aspect_No_Parts (Typ)));
3111 -- Generic instances
3113 -------------------------------------------
3114 -- Get_Generic_Formal_Types_In_Hierarchy --
3115 -------------------------------------------
3117 function Get_Generic_Formal_Types_In_Hierarchy
3118 is new Collect_Types_In_Hierarchy (Predicate => Is_Generic_Formal);
3119 -- Return a list of all types within a given type's hierarchy which
3120 -- are generic formals.
3122 ----------------------------------------
3123 -- Get_Types_With_Aspect_In_Hierarchy --
3124 ----------------------------------------
3126 function Get_Types_With_Aspect_In_Hierarchy
3127 is new Collect_Types_In_Hierarchy
3128 (Predicate => Has_Aspect_No_Parts);
3129 -- Returns a list of all types within a given type's hierarchy which
3130 -- have the Aspect_No_Parts specified.
3132 -- Local declarations
3134 Aspect_Value : Entity_Id;
3135 Curr_Value : Entity_Id;
3136 Curr_Typ_Elmt : Elmt_Id;
3137 Curr_Body_Elmt : Elmt_Id;
3138 Curr_Formal_Elmt : Elmt_Id;
3139 Gen_Bodies : Elist_Id;
3140 Gen_Formals : Elist_Id;
3141 Scop : Entity_Id;
3142 Types_With_Aspect : Elist_Id;
3144 -- Start of processing for Check_No_Parts_Violations
3146 begin
3147 -- Nothing to check if the type is elementary or artificial
3149 if Is_Elementary_Type (Typ) or else not Comes_From_Source (Typ) then
3150 return;
3151 end if;
3153 Types_With_Aspect := Get_Types_With_Aspect_In_Hierarchy (Typ);
3155 -- Nothing to check if there are no types with No_Parts specified
3157 if Is_Empty_Elmt_List (Types_With_Aspect) then
3158 return;
3159 end if;
3161 -- Set name for all errors below
3163 Error_Msg_Name_1 := Aspect_Names (Aspect_No_Parts);
3165 -- Obtain the aspect value for No_Parts for comparison
3167 Aspect_Value :=
3168 Get_Aspect_No_Parts_Value
3169 (Node (First_Elmt (Types_With_Aspect)));
3171 -- When the value is True and there are controlled/task parts or the
3172 -- type itself is controlled/task, trigger the appropriate error.
3174 if Aspect_Value = Standard_True then
3175 if Aspect_No_Parts = Aspect_No_Controlled_Parts then
3176 if Is_Controlled (Typ) or else Has_Controlled_Component (Typ)
3177 then
3178 Error_Msg_N
3179 ("aspect % applied to controlled type &", Typ);
3180 end if;
3182 elsif Aspect_No_Parts = Aspect_No_Task_Parts then
3183 if Has_Task (Typ) then
3184 Error_Msg_N
3185 ("aspect % applied to task type &", Typ);
3186 end if;
3188 else
3189 raise Program_Error;
3190 end if;
3191 end if;
3193 -- Move through Types_With_Aspect - checking that the value specified
3194 -- for their corresponding Aspect_No_Parts do not override each
3195 -- other.
3197 Curr_Typ_Elmt := First_Elmt (Types_With_Aspect);
3198 while Present (Curr_Typ_Elmt) loop
3199 Curr_Value :=
3200 Get_Aspect_No_Parts_Value (Node (Curr_Typ_Elmt));
3202 -- Compare the aspect value against the current type
3204 if Curr_Value /= Aspect_Value then
3205 Error_Msg_NE
3206 ("cannot override aspect % of "
3207 & "ancestor type &", Typ, Node (Curr_Typ_Elmt));
3208 return;
3209 end if;
3211 Next_Elmt (Curr_Typ_Elmt);
3212 end loop;
3214 -- Issue an error if the aspect applies to a type declared inside a
3215 -- generic body and if said type derives from or has a component
3216 -- of ageneric formal type - since those are considered to have
3217 -- controlled/task parts and have Aspect_No_Parts specified as
3218 -- False by default (RM H.4.1(4/5) is about the language-defined
3219 -- No_Controlled_Parts aspect, and we are using the same rules for
3220 -- No_Task_Parts).
3222 -- We do not check tagged types since deriving from a formal type
3223 -- within an enclosing generic unit is already illegal
3224 -- (RM 3.9.1 (4/2)).
3226 if Aspect_Value = Standard_True
3227 and then In_Generic_Body (Typ)
3228 and then not Is_Tagged_Type (Typ)
3229 then
3230 Gen_Bodies := New_Elmt_List;
3231 Gen_Formals :=
3232 Get_Generic_Formal_Types_In_Hierarchy
3233 (Typ => Typ,
3234 Examine_Components => True);
3236 -- Climb scopes collecting generic bodies
3238 Scop := Scope (Typ);
3239 while Present (Scop) and then Scop /= Standard_Standard loop
3241 -- Generic package body
3243 if Ekind (Scop) = E_Generic_Package
3244 and then In_Package_Body (Scop)
3245 then
3246 Append_Elmt (Scop, Gen_Bodies);
3248 -- Generic subprogram body
3250 elsif Is_Generic_Subprogram (Scop) then
3251 Append_Elmt (Scop, Gen_Bodies);
3252 end if;
3254 Scop := Scope (Scop);
3255 end loop;
3257 -- Warn about the improper use of Aspect_No_Parts on a type
3258 -- declaration deriving from or that has a component of a generic
3259 -- formal type within the formal type's corresponding generic
3260 -- body by moving through all formal types in Typ's hierarchy and
3261 -- checking if they are formals in any of the enclosing generic
3262 -- bodies.
3264 -- However, a special exception gets made for formal types which
3265 -- derive from a type which has Aspect_No_Parts True.
3267 -- For example:
3269 -- generic
3270 -- type Form is private;
3271 -- package G is
3272 -- type Type_A is new Form with No_Controlled_Parts; -- OK
3273 -- end;
3275 -- package body G is
3276 -- type Type_B is new Form with No_Controlled_Parts; -- ERROR
3277 -- end;
3279 -- generic
3280 -- type Form is private;
3281 -- package G is
3282 -- type Type_A is record C : Form; end record
3283 -- with No_Controlled_Parts; -- OK
3284 -- end;
3286 -- package body G is
3287 -- type Type_B is record C : Form; end record
3288 -- with No_Controlled_Parts; -- ERROR
3289 -- end;
3291 -- type Root is tagged null record with No_Controlled_Parts;
3293 -- generic
3294 -- type Form is new Root with private;
3295 -- package G is
3296 -- type Type_A is record C : Form; end record
3297 -- with No_Controlled_Parts; -- OK
3298 -- end;
3300 -- package body G is
3301 -- type Type_B is record C : Form; end record
3302 -- with No_Controlled_Parts; -- OK
3303 -- end;
3305 Curr_Formal_Elmt := First_Elmt (Gen_Formals);
3306 while Present (Curr_Formal_Elmt) loop
3308 Curr_Body_Elmt := First_Elmt (Gen_Bodies);
3309 while Present (Curr_Body_Elmt) loop
3311 -- Obtain types in the formal type's hierarchy which have
3312 -- the aspect specified.
3314 Types_With_Aspect :=
3315 Get_Types_With_Aspect_In_Hierarchy
3316 (Node (Curr_Formal_Elmt));
3318 -- We found a type declaration in a generic body where both
3319 -- Aspect_No_Parts is true and one of its ancestors is a
3320 -- generic formal type.
3322 if Scope (Node (Curr_Formal_Elmt)) =
3323 Node (Curr_Body_Elmt)
3325 -- Check that no ancestors of the formal type have
3326 -- Aspect_No_Parts True before issuing the error.
3328 and then (Is_Empty_Elmt_List (Types_With_Aspect)
3329 or else
3330 Get_Aspect_No_Parts_Value
3331 (Node (First_Elmt (Types_With_Aspect)))
3332 = Standard_False)
3333 then
3334 Error_Msg_Node_1 := Typ;
3335 Error_Msg_Node_2 := Node (Curr_Formal_Elmt);
3336 Error_Msg
3337 ("aspect % cannot be applied to "
3338 & "type & which has an ancestor or component of "
3339 & "formal type & within the formal type's "
3340 & "corresponding generic body", Sloc (Typ));
3341 end if;
3343 Next_Elmt (Curr_Body_Elmt);
3344 end loop;
3346 Next_Elmt (Curr_Formal_Elmt);
3347 end loop;
3348 end if;
3349 end Check_No_Parts_Violations;
3351 ---------------------------------
3352 -- Check_Suspicious_Convention --
3353 ---------------------------------
3355 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id) is
3356 begin
3357 if Has_Discriminants (Rec_Type)
3358 and then Is_Base_Type (Rec_Type)
3359 and then not Is_Unchecked_Union (Rec_Type)
3360 and then (Convention (Rec_Type) = Convention_C
3361 or else
3362 Convention (Rec_Type) = Convention_CPP)
3363 and then Comes_From_Source (Rec_Type)
3364 and then not In_Instance
3365 and then not Has_Warnings_Off (Rec_Type)
3366 then
3367 declare
3368 Cprag : constant Node_Id :=
3369 Get_Rep_Pragma (Rec_Type, Name_Convention);
3370 A2 : Node_Id;
3372 begin
3373 if Present (Cprag) then
3374 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
3376 if Convention (Rec_Type) = Convention_C then
3377 Error_Msg_N
3378 ("?x?discriminated record has no direct equivalent in "
3379 & "C", A2);
3380 else
3381 Error_Msg_N
3382 ("?x?discriminated record has no direct equivalent in "
3383 & "C++", A2);
3384 end if;
3386 Error_Msg_NE
3387 ("\?x?use of convention for type& is dubious",
3388 A2, Rec_Type);
3389 end if;
3390 end;
3391 end if;
3392 end Check_Suspicious_Convention;
3394 ------------------------------
3395 -- Check_Suspicious_Modulus --
3396 ------------------------------
3398 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
3399 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
3401 begin
3402 if not Warn_On_Suspicious_Modulus_Value then
3403 return;
3404 end if;
3406 if Nkind (Decl) = N_Full_Type_Declaration then
3407 declare
3408 Tdef : constant Node_Id := Type_Definition (Decl);
3410 begin
3411 if Nkind (Tdef) = N_Modular_Type_Definition then
3412 declare
3413 Modulus : constant Node_Id :=
3414 Original_Node (Expression (Tdef));
3416 begin
3417 if Nkind (Modulus) = N_Integer_Literal then
3418 declare
3419 Modv : constant Uint := Intval (Modulus);
3420 Sizv : constant Uint := RM_Size (Utype);
3422 begin
3423 -- First case, modulus and size are the same. This
3424 -- happens if you have something like mod 32, with
3425 -- an explicit size of 32, this is for sure a case
3426 -- where the warning is given, since it is seems
3427 -- very unlikely that someone would want e.g. a
3428 -- five bit type stored in 32 bits. It is much
3429 -- more likely they wanted a 32-bit type.
3431 if Modv = Sizv then
3432 null;
3434 -- Second case, the modulus is 32 or 64 and no
3435 -- size clause is present. This is a less clear
3436 -- case for giving the warning, but in the case
3437 -- of 32/64 (5-bit or 6-bit types) these seem rare
3438 -- enough that it is a likely error (and in any
3439 -- case using 2**5 or 2**6 in these cases seems
3440 -- clearer. We don't include 8 or 16 here, simply
3441 -- because in practice 3-bit and 4-bit types are
3442 -- more common and too many false positives if
3443 -- we warn in these cases.
3445 elsif not Has_Size_Clause (Utype)
3446 and then (Modv = Uint_32 or else Modv = Uint_64)
3447 then
3448 null;
3450 -- No warning needed
3452 else
3453 return;
3454 end if;
3456 -- If we fall through, give warning
3458 Error_Msg_Uint_1 := Modv;
3459 Error_Msg_N
3460 ("?.m?2 '*'*^' may have been intended here",
3461 Modulus);
3462 end;
3463 end if;
3464 end;
3465 end if;
3466 end;
3467 end if;
3468 end Check_Suspicious_Modulus;
3470 -----------------------
3471 -- Freeze_Array_Type --
3472 -----------------------
3474 procedure Freeze_Array_Type (Arr : Entity_Id) is
3475 FS : constant Entity_Id := First_Subtype (Arr);
3476 Ctyp : constant Entity_Id := Component_Type (Arr);
3477 Clause : Entity_Id;
3479 Non_Standard_Enum : Boolean := False;
3480 -- Set true if any of the index types is an enumeration type with a
3481 -- non-standard representation.
3483 begin
3484 Freeze_And_Append (Ctyp, N, Result);
3486 Indx := First_Index (Arr);
3487 while Present (Indx) loop
3488 Freeze_And_Append (Etype (Indx), N, Result);
3490 if Is_Enumeration_Type (Etype (Indx))
3491 and then Has_Non_Standard_Rep (Etype (Indx))
3492 then
3493 Non_Standard_Enum := True;
3494 end if;
3496 Next_Index (Indx);
3497 end loop;
3499 -- Processing that is done only for base types
3501 if Ekind (Arr) = E_Array_Type then
3503 -- Deal with default setting of reverse storage order
3505 Set_SSO_From_Default (Arr);
3507 -- Propagate flags for component type
3509 if Is_Controlled (Ctyp)
3510 or else Has_Controlled_Component (Ctyp)
3511 then
3512 Set_Has_Controlled_Component (Arr);
3513 end if;
3515 if Has_Unchecked_Union (Ctyp) then
3516 Set_Has_Unchecked_Union (Arr);
3517 end if;
3519 -- The array type requires its own invariant procedure in order to
3520 -- verify the component invariant over all elements. In GNATprove
3521 -- mode, the component invariants are checked by other means. They
3522 -- should not be added to the array type invariant procedure, so
3523 -- that the procedure can be used to check the array type
3524 -- invariants if any.
3526 if Has_Invariants (Ctyp)
3527 and then not GNATprove_Mode
3528 then
3529 Set_Has_Own_Invariants (Arr);
3530 end if;
3532 -- Warn for pragma Pack overriding foreign convention
3534 if Has_Foreign_Convention (Ctyp)
3535 and then Has_Pragma_Pack (Arr)
3536 then
3537 declare
3538 CN : constant Name_Id :=
3539 Get_Convention_Name (Convention (Ctyp));
3540 PP : constant Node_Id :=
3541 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
3542 begin
3543 if Present (PP) then
3544 Error_Msg_Name_1 := CN;
3545 Error_Msg_Sloc := Sloc (Arr);
3546 Error_Msg_N
3547 ("pragma Pack affects convention % components #??", PP);
3548 Error_Msg_Name_1 := CN;
3549 Error_Msg_N
3550 ("\array components may not have % compatible "
3551 & "representation??", PP);
3552 end if;
3553 end;
3554 end if;
3556 -- Check for Aliased or Atomic_Components or Full Access with
3557 -- unsuitable packing or explicit component size clause given.
3559 if (Has_Aliased_Components (Arr)
3560 or else Has_Atomic_Components (Arr)
3561 or else Is_Full_Access (Ctyp))
3562 and then
3563 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
3564 then
3565 Alias_Atomic_Check : declare
3567 procedure Complain_CS (T : String);
3568 -- Outputs error messages for incorrect CS clause or pragma
3569 -- Pack for aliased or full access components (T is either
3570 -- "aliased" or "atomic" or "volatile full access");
3572 -----------------
3573 -- Complain_CS --
3574 -----------------
3576 procedure Complain_CS (T : String) is
3577 begin
3578 if Has_Component_Size_Clause (Arr) then
3579 Clause :=
3580 Get_Attribute_Definition_Clause
3581 (FS, Attribute_Component_Size);
3583 Error_Msg_N
3584 ("incorrect component size for "
3585 & T & " components", Clause);
3586 Error_Msg_Uint_1 := Esize (Ctyp);
3587 Error_Msg_N
3588 ("\only allowed value is^", Clause);
3590 else
3591 Error_Msg_N
3592 ("?cannot pack " & T & " components (RM 13.2(7))",
3593 Get_Rep_Pragma (FS, Name_Pack));
3594 Set_Is_Packed (Arr, False);
3595 end if;
3596 end Complain_CS;
3598 -- Start of processing for Alias_Atomic_Check
3600 begin
3601 -- If object size of component type isn't known, we cannot
3602 -- be sure so we defer to the back end.
3604 if not Known_Static_Esize (Ctyp) then
3605 null;
3607 -- Case where component size has no effect. First check for
3608 -- object size of component type multiple of the storage
3609 -- unit size.
3611 elsif Esize (Ctyp) mod System_Storage_Unit = 0
3613 -- OK in both packing case and component size case if RM
3614 -- size is known and static and same as the object size.
3616 and then
3617 ((Known_Static_RM_Size (Ctyp)
3618 and then Esize (Ctyp) = RM_Size (Ctyp))
3620 -- Or if we have an explicit component size clause and
3621 -- the component size and object size are equal.
3623 or else
3624 (Has_Component_Size_Clause (Arr)
3625 and then Component_Size (Arr) = Esize (Ctyp)))
3626 then
3627 null;
3629 elsif Has_Aliased_Components (Arr) then
3630 Complain_CS ("aliased");
3632 elsif Has_Atomic_Components (Arr)
3633 or else Is_Atomic (Ctyp)
3634 then
3635 Complain_CS ("atomic");
3637 elsif Is_Volatile_Full_Access (Ctyp) then
3638 Complain_CS ("volatile full access");
3639 end if;
3640 end Alias_Atomic_Check;
3641 end if;
3643 -- Check for Independent_Components/Independent with unsuitable
3644 -- packing or explicit component size clause given.
3646 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
3647 and then
3648 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
3649 then
3650 begin
3651 -- If object size of component type isn't known, we cannot
3652 -- be sure so we defer to the back end.
3654 if not Known_Static_Esize (Ctyp) then
3655 null;
3657 -- Case where component size has no effect. First check for
3658 -- object size of component type multiple of the storage
3659 -- unit size.
3661 elsif Esize (Ctyp) mod System_Storage_Unit = 0
3663 -- OK in both packing case and component size case if RM
3664 -- size is known and multiple of the storage unit size.
3666 and then
3667 ((Known_Static_RM_Size (Ctyp)
3668 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
3670 -- Or if we have an explicit component size clause and
3671 -- the component size is larger than the object size.
3673 or else
3674 (Has_Component_Size_Clause (Arr)
3675 and then Component_Size (Arr) >= Esize (Ctyp)))
3676 then
3677 null;
3679 else
3680 if Has_Component_Size_Clause (Arr) then
3681 Clause :=
3682 Get_Attribute_Definition_Clause
3683 (FS, Attribute_Component_Size);
3685 Error_Msg_N
3686 ("incorrect component size for "
3687 & "independent components", Clause);
3688 Error_Msg_Uint_1 := Esize (Ctyp);
3689 Error_Msg_N
3690 ("\minimum allowed is^", Clause);
3692 else
3693 Error_Msg_N
3694 ("?cannot pack independent components (RM 13.2(7))",
3695 Get_Rep_Pragma (FS, Name_Pack));
3696 Set_Is_Packed (Arr, False);
3697 end if;
3698 end if;
3699 end;
3700 end if;
3702 -- If packing was requested or if the component size was
3703 -- set explicitly, then see if bit packing is required. This
3704 -- processing is only done for base types, since all of the
3705 -- representation aspects involved are type-related.
3707 -- This is not just an optimization, if we start processing the
3708 -- subtypes, they interfere with the settings on the base type
3709 -- (this is because Is_Packed has a slightly different meaning
3710 -- before and after freezing).
3712 declare
3713 Csiz : Uint;
3714 Esiz : Uint;
3716 begin
3717 if Is_Packed (Arr)
3718 and then Known_Static_RM_Size (Ctyp)
3719 and then not Has_Component_Size_Clause (Arr)
3720 then
3721 Csiz := UI_Max (RM_Size (Ctyp), 1);
3723 elsif Known_Component_Size (Arr) then
3724 Csiz := Component_Size (Arr);
3726 elsif not Known_Static_Esize (Ctyp) then
3727 Csiz := Uint_0;
3729 else
3730 Esiz := Esize (Ctyp);
3732 -- We can set the component size if it is less than 16,
3733 -- rounding it up to the next storage unit size.
3735 if Esiz <= 8 then
3736 Csiz := Uint_8;
3737 elsif Esiz <= 16 then
3738 Csiz := Uint_16;
3739 else
3740 Csiz := Uint_0;
3741 end if;
3743 -- Set component size up to match alignment if it would
3744 -- otherwise be less than the alignment. This deals with
3745 -- cases of types whose alignment exceeds their size (the
3746 -- padded type cases).
3748 if Csiz /= 0 and then Known_Alignment (Ctyp) then
3749 declare
3750 A : constant Uint := Alignment_In_Bits (Ctyp);
3751 begin
3752 if Csiz < A then
3753 Csiz := A;
3754 end if;
3755 end;
3756 end if;
3757 end if;
3759 -- Case of component size that may result in bit packing
3761 if 1 <= Csiz and then Csiz <= System_Max_Integer_Size then
3762 declare
3763 Ent : constant Entity_Id :=
3764 First_Subtype (Arr);
3765 Pack_Pragma : constant Node_Id :=
3766 Get_Rep_Pragma (Ent, Name_Pack);
3767 Comp_Size_C : constant Node_Id :=
3768 Get_Attribute_Definition_Clause
3769 (Ent, Attribute_Component_Size);
3771 begin
3772 -- Warn if we have pack and component size so that the
3773 -- pack is ignored.
3775 -- Note: here we must check for the presence of a
3776 -- component size before checking for a Pack pragma to
3777 -- deal with the case where the array type is a derived
3778 -- type whose parent is currently private.
3780 if Present (Comp_Size_C)
3781 and then Has_Pragma_Pack (Ent)
3782 and then Warn_On_Redundant_Constructs
3783 then
3784 Error_Msg_Sloc := Sloc (Comp_Size_C);
3785 Error_Msg_NE
3786 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
3787 Error_Msg_N
3788 ("\?r?explicit component size given#!", Pack_Pragma);
3789 Set_Is_Packed (Base_Type (Ent), False);
3790 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
3791 end if;
3793 -- Set component size if not already set by a component
3794 -- size clause.
3796 if not Present (Comp_Size_C) then
3797 Set_Component_Size (Arr, Csiz);
3798 end if;
3800 -- Check for base type of 8, 16, 32 bits, where an
3801 -- unsigned subtype has a length one less than the
3802 -- base type (e.g. Natural subtype of Integer).
3804 -- In such cases, if a component size was not set
3805 -- explicitly, then generate a warning.
3807 if Has_Pragma_Pack (Arr)
3808 and then not Present (Comp_Size_C)
3809 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
3810 and then Known_Esize (Base_Type (Ctyp))
3811 and then Esize (Base_Type (Ctyp)) = Csiz + 1
3812 then
3813 Error_Msg_Uint_1 := Csiz;
3815 if Present (Pack_Pragma) then
3816 Error_Msg_N
3817 ("??pragma Pack causes component size to be ^!",
3818 Pack_Pragma);
3819 Error_Msg_N
3820 ("\??use Component_Size to set desired value!",
3821 Pack_Pragma);
3822 end if;
3823 end if;
3825 -- Bit packing is never needed for 8, 16, 32, 64 or 128
3827 if Addressable (Csiz) then
3829 -- If the Esize of the component is known and equal to
3830 -- the component size then even packing is not needed.
3832 if Known_Static_Esize (Ctyp)
3833 and then Esize (Ctyp) = Csiz
3834 then
3835 -- Here the array was requested to be packed, but
3836 -- the packing request had no effect whatsoever,
3837 -- so flag Is_Packed is reset.
3839 -- Note: semantically this means that we lose track
3840 -- of the fact that a derived type inherited pragma
3841 -- Pack that was non-effective, but that is fine.
3843 -- We regard a Pack pragma as a request to set a
3844 -- representation characteristic, and this request
3845 -- may be ignored.
3847 Set_Is_Packed (Base_Type (Arr), False);
3848 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
3849 else
3850 Set_Is_Packed (Base_Type (Arr), True);
3851 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3852 end if;
3854 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3856 -- Bit packing is not needed for multiples of the storage
3857 -- unit if the type is composite because the back end can
3858 -- byte pack composite types efficiently. That's not true
3859 -- for discrete types because every read would generate a
3860 -- lot of instructions, so we keep using the manipulation
3861 -- routines of the runtime for them.
3863 elsif Csiz mod System_Storage_Unit = 0
3864 and then Is_Composite_Type (Ctyp)
3865 then
3866 Set_Is_Packed (Base_Type (Arr), True);
3867 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3868 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3870 -- In all other cases, bit packing is needed
3872 else
3873 Set_Is_Packed (Base_Type (Arr), True);
3874 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3875 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
3876 end if;
3877 end;
3878 end if;
3879 end;
3881 -- Warn for case of atomic type
3883 Clause := Get_Rep_Pragma (FS, Name_Atomic);
3885 if Present (Clause)
3886 and then not Addressable (Component_Size (FS))
3887 then
3888 Error_Msg_NE
3889 ("non-atomic components of type& may not be "
3890 & "accessible by separate tasks??", Clause, Arr);
3892 if Has_Component_Size_Clause (Arr) then
3893 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
3894 (FS, Attribute_Component_Size));
3895 Error_Msg_N ("\because of component size clause#??", Clause);
3897 elsif Has_Pragma_Pack (Arr) then
3898 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
3899 Error_Msg_N ("\because of pragma Pack#??", Clause);
3900 end if;
3901 end if;
3903 -- Check for scalar storage order
3905 declare
3906 Dummy : Boolean;
3907 begin
3908 Check_Component_Storage_Order
3909 (Encl_Type => Arr,
3910 Comp => Empty,
3911 ADC => Get_Attribute_Definition_Clause
3912 (First_Subtype (Arr),
3913 Attribute_Scalar_Storage_Order),
3914 Comp_ADC_Present => Dummy);
3915 end;
3917 -- Processing that is done only for subtypes
3919 else
3920 -- Acquire alignment from base type. Known_Alignment of the base
3921 -- type is False for Wide_String, for example.
3923 if not Known_Alignment (Arr)
3924 and then Known_Alignment (Base_Type (Arr))
3925 then
3926 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
3927 Adjust_Esize_Alignment (Arr);
3928 end if;
3929 end if;
3931 -- Specific checks for bit-packed arrays
3933 if Is_Bit_Packed_Array (Arr) then
3935 -- Check number of elements for bit-packed arrays that come from
3936 -- source and have compile time known ranges. The bit-packed
3937 -- arrays circuitry does not support arrays with more than
3938 -- Integer'Last + 1 elements, and when this restriction is
3939 -- violated, causes incorrect data access.
3941 -- For the case where this is not compile time known, a run-time
3942 -- check should be generated???
3944 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
3945 declare
3946 Elmts : Uint;
3947 Index : Node_Id;
3948 Ilen : Node_Id;
3949 Ityp : Entity_Id;
3951 begin
3952 Elmts := Uint_1;
3953 Index := First_Index (Arr);
3954 while Present (Index) loop
3955 Ityp := Etype (Index);
3957 -- Never generate an error if any index is of a generic
3958 -- type. We will check this in instances.
3960 if Is_Generic_Type (Ityp) then
3961 Elmts := Uint_0;
3962 exit;
3963 end if;
3965 Ilen :=
3966 Make_Attribute_Reference (Loc,
3967 Prefix => New_Occurrence_Of (Ityp, Loc),
3968 Attribute_Name => Name_Range_Length);
3969 Analyze_And_Resolve (Ilen);
3971 -- No attempt is made to check number of elements if not
3972 -- compile time known.
3974 if Nkind (Ilen) /= N_Integer_Literal then
3975 Elmts := Uint_0;
3976 exit;
3977 end if;
3979 Elmts := Elmts * Intval (Ilen);
3980 Next_Index (Index);
3981 end loop;
3983 if Elmts > Intval (High_Bound
3984 (Scalar_Range (Standard_Integer))) + 1
3985 then
3986 Error_Msg_N
3987 ("bit packed array type may not have "
3988 & "more than Integer''Last+1 elements", Arr);
3989 end if;
3990 end;
3991 end if;
3993 -- Check size
3995 if Known_RM_Size (Arr) then
3996 declare
3997 SizC : constant Node_Id := Size_Clause (Arr);
3998 Discard : Boolean;
4000 begin
4001 -- It is not clear if it is possible to have no size clause
4002 -- at this stage, but it is not worth worrying about. Post
4003 -- error on the entity name in the size clause if present,
4004 -- else on the type entity itself.
4006 if Present (SizC) then
4007 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
4008 else
4009 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
4010 end if;
4011 end;
4012 end if;
4013 end if;
4015 -- If any of the index types was an enumeration type with a non-
4016 -- standard rep clause, then we indicate that the array type is
4017 -- always packed (even if it is not bit-packed).
4019 if Non_Standard_Enum then
4020 Set_Has_Non_Standard_Rep (Base_Type (Arr));
4021 Set_Is_Packed (Base_Type (Arr));
4022 end if;
4024 Set_Component_Alignment_If_Not_Set (Arr);
4026 -- If the array is packed and bit-packed or packed to eliminate holes
4027 -- in the non-contiguous enumeration index types, we must create the
4028 -- packed array type to be used to actually implement the type. This
4029 -- is only needed for real array types (not for string literal types,
4030 -- since they are present only for the front end).
4032 if Is_Packed (Arr)
4033 and then (Is_Bit_Packed_Array (Arr) or else Non_Standard_Enum)
4034 and then Ekind (Arr) /= E_String_Literal_Subtype
4035 then
4036 Create_Packed_Array_Impl_Type (Arr);
4037 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
4039 -- Make sure that we have the necessary routines to implement the
4040 -- packing, and complain now if not. Note that we only test this
4041 -- for constrained array types.
4043 if Is_Constrained (Arr)
4044 and then Is_Bit_Packed_Array (Arr)
4045 and then Present (Packed_Array_Impl_Type (Arr))
4046 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
4047 then
4048 declare
4049 CS : constant Uint := Component_Size (Arr);
4050 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
4052 begin
4053 if RE /= RE_Null
4054 and then not RTE_Available (RE)
4055 then
4056 Error_Msg_CRT
4057 ("packing of " & UI_Image (CS) & "-bit components",
4058 First_Subtype (Etype (Arr)));
4060 -- Cancel the packing
4062 Set_Is_Packed (Base_Type (Arr), False);
4063 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
4064 Set_Packed_Array_Impl_Type (Arr, Empty);
4065 goto Skip_Packed;
4066 end if;
4067 end;
4068 end if;
4070 -- Size information of packed array type is copied to the array
4071 -- type, since this is really the representation. But do not
4072 -- override explicit existing size values. If the ancestor subtype
4073 -- is constrained the Packed_Array_Impl_Type will be inherited
4074 -- from it, but the size may have been provided already, and
4075 -- must not be overridden either.
4077 if not Has_Size_Clause (Arr)
4078 and then
4079 (No (Ancestor_Subtype (Arr))
4080 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
4081 then
4082 Copy_Esize (To => Arr, From => Packed_Array_Impl_Type (Arr));
4083 Copy_RM_Size (To => Arr, From => Packed_Array_Impl_Type (Arr));
4084 end if;
4086 if not Has_Alignment_Clause (Arr) then
4087 Copy_Alignment
4088 (To => Arr, From => Packed_Array_Impl_Type (Arr));
4089 end if;
4090 end if;
4092 <<Skip_Packed>>
4094 -- A Ghost type cannot have a component of protected or task type
4095 -- (SPARK RM 6.9(19)).
4097 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
4098 Error_Msg_N
4099 ("ghost array type & cannot have concurrent component type",
4100 Arr);
4101 end if;
4102 end Freeze_Array_Type;
4104 -------------------------------
4105 -- Freeze_Object_Declaration --
4106 -------------------------------
4108 procedure Freeze_Object_Declaration (E : Entity_Id) is
4109 procedure Check_Large_Modular_Array (Typ : Entity_Id);
4110 -- Check that the size of array type Typ can be computed without
4111 -- overflow, and generates a Storage_Error otherwise. This is only
4112 -- relevant for array types whose index has System_Max_Integer_Size
4113 -- bits, where wrap-around arithmetic might yield a meaningless value
4114 -- for the length of the array, or its corresponding attribute.
4116 procedure Check_Pragma_Thread_Local_Storage (Var_Id : Entity_Id);
4117 -- Ensure that the initialization state of variable Var_Id subject
4118 -- to pragma Thread_Local_Storage agrees with the semantics of the
4119 -- pragma.
4121 function Has_Default_Initialization
4122 (Obj_Id : Entity_Id) return Boolean;
4123 -- Determine whether object Obj_Id default initialized
4125 -------------------------------
4126 -- Check_Large_Modular_Array --
4127 -------------------------------
4129 procedure Check_Large_Modular_Array (Typ : Entity_Id) is
4130 Obj_Loc : constant Source_Ptr := Sloc (E);
4131 Idx_Typ : Entity_Id;
4133 begin
4134 -- Nothing to do when expansion is disabled because this routine
4135 -- generates a runtime check.
4137 if not Expander_Active then
4138 return;
4140 -- Nothing to do for String literal subtypes because their index
4141 -- cannot be a modular type.
4143 elsif Ekind (Typ) = E_String_Literal_Subtype then
4144 return;
4146 -- Nothing to do for an imported object because the object will
4147 -- be created on the exporting side.
4149 elsif Is_Imported (E) then
4150 return;
4152 -- Nothing to do for unconstrained array types. This case arises
4153 -- when the object declaration is illegal.
4155 elsif not Is_Constrained (Typ) then
4156 return;
4157 end if;
4159 Idx_Typ := Etype (First_Index (Typ));
4161 -- To prevent arithmetic overflow with large values, we raise
4162 -- Storage_Error under the following guard:
4164 -- (Arr'Last / 2 - Arr'First / 2) > (2 ** 30)
4166 -- This takes care of the boundary case, but it is preferable to
4167 -- use a smaller limit, because even on 64-bit architectures an
4168 -- array of more than 2 ** 30 bytes is likely to raise
4169 -- Storage_Error.
4171 if Is_Modular_Integer_Type (Idx_Typ)
4172 and then RM_Size (Idx_Typ) = RM_Size (Standard_Long_Long_Integer)
4173 then
4174 Insert_Action (Declaration_Node (E),
4175 Make_Raise_Storage_Error (Obj_Loc,
4176 Condition =>
4177 Make_Op_Ge (Obj_Loc,
4178 Left_Opnd =>
4179 Make_Op_Subtract (Obj_Loc,
4180 Left_Opnd =>
4181 Make_Op_Divide (Obj_Loc,
4182 Left_Opnd =>
4183 Make_Attribute_Reference (Obj_Loc,
4184 Prefix =>
4185 New_Occurrence_Of (Typ, Obj_Loc),
4186 Attribute_Name => Name_Last),
4187 Right_Opnd =>
4188 Make_Integer_Literal (Obj_Loc, Uint_2)),
4189 Right_Opnd =>
4190 Make_Op_Divide (Obj_Loc,
4191 Left_Opnd =>
4192 Make_Attribute_Reference (Obj_Loc,
4193 Prefix =>
4194 New_Occurrence_Of (Typ, Obj_Loc),
4195 Attribute_Name => Name_First),
4196 Right_Opnd =>
4197 Make_Integer_Literal (Obj_Loc, Uint_2))),
4198 Right_Opnd =>
4199 Make_Integer_Literal (Obj_Loc, (Uint_2 ** 30))),
4200 Reason => SE_Object_Too_Large));
4201 end if;
4202 end Check_Large_Modular_Array;
4204 ---------------------------------------
4205 -- Check_Pragma_Thread_Local_Storage --
4206 ---------------------------------------
4208 procedure Check_Pragma_Thread_Local_Storage (Var_Id : Entity_Id) is
4209 function Has_Incompatible_Initialization
4210 (Var_Decl : Node_Id) return Boolean;
4211 -- Determine whether variable Var_Id with declaration Var_Decl is
4212 -- initialized with a value that violates the semantics of pragma
4213 -- Thread_Local_Storage.
4215 -------------------------------------
4216 -- Has_Incompatible_Initialization --
4217 -------------------------------------
4219 function Has_Incompatible_Initialization
4220 (Var_Decl : Node_Id) return Boolean
4222 Init_Expr : constant Node_Id := Expression (Var_Decl);
4224 begin
4225 -- The variable is default-initialized. This directly violates
4226 -- the semantics of the pragma.
4228 if Has_Default_Initialization (Var_Id) then
4229 return True;
4231 -- The variable has explicit initialization. In this case only
4232 -- a handful of values satisfy the semantics of the pragma.
4234 elsif Has_Init_Expression (Var_Decl)
4235 and then Present (Init_Expr)
4236 then
4237 -- "null" is a legal form of initialization
4239 if Nkind (Init_Expr) = N_Null then
4240 return False;
4242 -- A static expression is a legal form of initialization
4244 elsif Is_Static_Expression (Init_Expr) then
4245 return False;
4247 -- A static aggregate is a legal form of initialization
4249 elsif Nkind (Init_Expr) = N_Aggregate
4250 and then Compile_Time_Known_Aggregate (Init_Expr)
4251 then
4252 return False;
4254 -- All other initialization expressions violate the semantic
4255 -- of the pragma.
4257 else
4258 return True;
4259 end if;
4261 -- The variable lacks any kind of initialization, which agrees
4262 -- with the semantics of the pragma.
4264 else
4265 return False;
4266 end if;
4267 end Has_Incompatible_Initialization;
4269 -- Local declarations
4271 Var_Decl : constant Node_Id := Declaration_Node (Var_Id);
4273 -- Start of processing for Check_Pragma_Thread_Local_Storage
4275 begin
4276 -- A variable whose initialization is suppressed lacks any kind of
4277 -- initialization.
4279 if Suppress_Initialization (Var_Id) then
4280 null;
4282 -- The variable has default initialization, or is explicitly
4283 -- initialized to a value other than null, static expression,
4284 -- or a static aggregate.
4286 elsif Has_Incompatible_Initialization (Var_Decl) then
4287 Error_Msg_NE
4288 ("Thread_Local_Storage variable& is improperly initialized",
4289 Var_Decl, Var_Id);
4290 Error_Msg_NE
4291 ("\only allowed initialization is explicit NULL, static "
4292 & "expression or static aggregate", Var_Decl, Var_Id);
4293 end if;
4294 end Check_Pragma_Thread_Local_Storage;
4296 --------------------------------
4297 -- Has_Default_Initialization --
4298 --------------------------------
4300 function Has_Default_Initialization
4301 (Obj_Id : Entity_Id) return Boolean
4303 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
4304 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4306 begin
4307 return
4308 Comes_From_Source (Obj_Id)
4309 and then not Is_Imported (Obj_Id)
4310 and then not Has_Init_Expression (Obj_Decl)
4311 and then
4312 ((Has_Non_Null_Base_Init_Proc (Obj_Typ)
4313 and then not No_Initialization (Obj_Decl)
4314 and then not Initialization_Suppressed (Obj_Typ))
4315 or else
4316 (Needs_Simple_Initialization (Obj_Typ)
4317 and then not Is_Internal (Obj_Id)));
4318 end Has_Default_Initialization;
4320 -- Local variables
4322 Typ : constant Entity_Id := Etype (E);
4323 Def : Node_Id;
4325 -- Start of processing for Freeze_Object_Declaration
4327 begin
4328 -- Abstract type allowed only for C++ imported variables or constants
4330 -- Note: we inhibit this check for objects that do not come from
4331 -- source because there is at least one case (the expansion of
4332 -- x'Class'Input where x is abstract) where we legitimately
4333 -- generate an abstract object.
4335 if Is_Abstract_Type (Typ)
4336 and then Comes_From_Source (Parent (E))
4337 and then not (Is_Imported (E) and then Is_CPP_Class (Typ))
4338 then
4339 Def := Object_Definition (Parent (E));
4341 Error_Msg_N ("type of object cannot be abstract", Def);
4343 if Is_CPP_Class (Etype (E)) then
4344 Error_Msg_NE ("\} may need a cpp_constructor", Def, Typ);
4346 elsif Present (Expression (Parent (E))) then
4347 Error_Msg_N -- CODEFIX
4348 ("\maybe a class-wide type was meant", Def);
4349 end if;
4350 end if;
4352 -- For object created by object declaration, perform required
4353 -- categorization (preelaborate and pure) checks. Defer these
4354 -- checks to freeze time since pragma Import inhibits default
4355 -- initialization and thus pragma Import affects these checks.
4357 Validate_Object_Declaration (Declaration_Node (E));
4359 -- If there is an address clause, check that it is valid and if need
4360 -- be move initialization to the freeze node.
4362 Check_Address_Clause (E);
4364 -- Similar processing is needed for aspects that may affect object
4365 -- layout, like Address, if there is an initialization expression.
4366 -- We don't do this if there is a pragma Linker_Section, because it
4367 -- would prevent the back end from statically initializing the
4368 -- object; we don't want elaboration code in that case.
4370 if Has_Delayed_Aspects (E)
4371 and then Expander_Active
4372 and then Is_Array_Type (Typ)
4373 and then Present (Expression (Declaration_Node (E)))
4374 and then No (Linker_Section_Pragma (E))
4375 then
4376 declare
4377 Decl : constant Node_Id := Declaration_Node (E);
4378 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
4380 begin
4381 -- Capture initialization value at point of declaration, and
4382 -- make explicit assignment legal, because object may be a
4383 -- constant.
4385 Remove_Side_Effects (Expression (Decl));
4386 Set_Assignment_OK (Lhs);
4388 -- Move initialization to freeze actions
4390 Append_Freeze_Action (E,
4391 Make_Assignment_Statement (Loc,
4392 Name => Lhs,
4393 Expression => Expression (Decl)));
4395 Set_No_Initialization (Decl);
4396 -- Set_Is_Frozen (E, False);
4397 end;
4398 end if;
4400 -- Reset Is_True_Constant for non-constant aliased object. We
4401 -- consider that the fact that a non-constant object is aliased may
4402 -- indicate that some funny business is going on, e.g. an aliased
4403 -- object is passed by reference to a procedure which captures the
4404 -- address of the object, which is later used to assign a new value,
4405 -- even though the compiler thinks that it is not modified. Such
4406 -- code is highly dubious, but we choose to make it "work" for
4407 -- non-constant aliased objects.
4409 -- Note that we used to do this for all aliased objects, whether or
4410 -- not constant, but this caused anomalies down the line because we
4411 -- ended up with static objects that were not Is_True_Constant. Not
4412 -- resetting Is_True_Constant for (aliased) constant objects ensures
4413 -- that this anomaly never occurs.
4415 -- However, we don't do that for internal entities. We figure that if
4416 -- we deliberately set Is_True_Constant for an internal entity, e.g.
4417 -- a dispatch table entry, then we mean it.
4419 if Ekind (E) /= E_Constant
4420 and then (Is_Aliased (E) or else Is_Aliased (Typ))
4421 and then not Is_Internal_Name (Chars (E))
4422 then
4423 Set_Is_True_Constant (E, False);
4424 end if;
4426 -- If the object needs any kind of default initialization, an error
4427 -- must be issued if No_Default_Initialization applies. The check
4428 -- doesn't apply to imported objects, which are not ever default
4429 -- initialized, and is why the check is deferred until freezing, at
4430 -- which point we know if Import applies. Deferred constants are also
4431 -- exempted from this test because their completion is explicit, or
4432 -- through an import pragma.
4434 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
4435 null;
4437 elsif Has_Default_Initialization (E) then
4438 Check_Restriction
4439 (No_Default_Initialization, Declaration_Node (E));
4440 end if;
4442 -- Ensure that a variable subject to pragma Thread_Local_Storage
4444 -- * Lacks default initialization, or
4446 -- * The initialization expression is either "null", a static
4447 -- constant, or a compile-time known aggregate.
4449 if Has_Pragma_Thread_Local_Storage (E) then
4450 Check_Pragma_Thread_Local_Storage (E);
4451 end if;
4453 -- For imported objects, set Is_Public unless there is also an
4454 -- address clause, which means that there is no external symbol
4455 -- needed for the Import (Is_Public may still be set for other
4456 -- unrelated reasons). Note that we delayed this processing
4457 -- till freeze time so that we can be sure not to set the flag
4458 -- if there is an address clause. If there is such a clause,
4459 -- then the only purpose of the Import pragma is to suppress
4460 -- implicit initialization.
4462 if Is_Imported (E) and then No (Address_Clause (E)) then
4463 Set_Is_Public (E);
4464 end if;
4466 -- For source objects that are not Imported and are library level, if
4467 -- no linker section pragma was given inherit the appropriate linker
4468 -- section from the corresponding type.
4470 if Comes_From_Source (E)
4471 and then not Is_Imported (E)
4472 and then Is_Library_Level_Entity (E)
4473 and then No (Linker_Section_Pragma (E))
4474 then
4475 Set_Linker_Section_Pragma (E, Linker_Section_Pragma (Typ));
4476 end if;
4478 -- For convention C objects of an enumeration type, warn if the size
4479 -- is not integer size and no explicit size given. Skip warning for
4480 -- Boolean and Character, and assume programmer expects 8-bit sizes
4481 -- for these cases.
4483 if (Convention (E) = Convention_C
4484 or else
4485 Convention (E) = Convention_CPP)
4486 and then Is_Enumeration_Type (Typ)
4487 and then not Is_Character_Type (Typ)
4488 and then not Is_Boolean_Type (Typ)
4489 and then Esize (Typ) < Standard_Integer_Size
4490 and then not Has_Size_Clause (E)
4491 then
4492 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
4493 Error_Msg_N
4494 ("??convention C enumeration object has size less than ^", E);
4495 Error_Msg_N ("\??use explicit size clause to set size", E);
4496 end if;
4498 -- Declaring too big an array in disabled ghost code is OK
4500 if Is_Array_Type (Typ) and then not Is_Ignored_Ghost_Entity (E) then
4501 Check_Large_Modular_Array (Typ);
4502 end if;
4503 end Freeze_Object_Declaration;
4505 -----------------------------
4506 -- Freeze_Generic_Entities --
4507 -----------------------------
4509 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
4510 E : Entity_Id;
4511 F : Node_Id;
4512 Flist : List_Id;
4514 begin
4515 Flist := New_List;
4516 E := First_Entity (Pack);
4517 while Present (E) loop
4518 if Is_Type (E) and then not Is_Generic_Type (E) then
4519 F := Make_Freeze_Generic_Entity (Sloc (Pack));
4520 Set_Entity (F, E);
4521 Append_To (Flist, F);
4523 elsif Ekind (E) = E_Generic_Package then
4524 Append_List_To (Flist, Freeze_Generic_Entities (E));
4525 end if;
4527 Next_Entity (E);
4528 end loop;
4530 return Flist;
4531 end Freeze_Generic_Entities;
4533 --------------------
4534 -- Freeze_Profile --
4535 --------------------
4537 function Freeze_Profile (E : Entity_Id) return Boolean is
4538 F_Type : Entity_Id;
4539 R_Type : Entity_Id;
4540 Warn_Node : Node_Id;
4542 begin
4543 -- Loop through formals
4545 Formal := First_Formal (E);
4546 while Present (Formal) loop
4547 F_Type := Etype (Formal);
4549 -- AI05-0151: incomplete types can appear in a profile. By the
4550 -- time the entity is frozen, the full view must be available,
4551 -- unless it is a limited view.
4553 if Is_Incomplete_Type (F_Type)
4554 and then Present (Full_View (F_Type))
4555 and then not From_Limited_With (F_Type)
4556 then
4557 F_Type := Full_View (F_Type);
4558 Set_Etype (Formal, F_Type);
4559 end if;
4561 if not From_Limited_With (F_Type)
4562 and then Should_Freeze_Type (F_Type, E, N)
4563 then
4564 Freeze_And_Append (F_Type, N, Result);
4565 end if;
4567 if Is_Private_Type (F_Type)
4568 and then Is_Private_Type (Base_Type (F_Type))
4569 and then No (Full_View (Base_Type (F_Type)))
4570 and then not Is_Generic_Type (F_Type)
4571 and then not Is_Derived_Type (F_Type)
4572 then
4573 -- If the type of a formal is incomplete, subprogram is being
4574 -- frozen prematurely. Within an instance (but not within a
4575 -- wrapper package) this is an artifact of our need to regard
4576 -- the end of an instantiation as a freeze point. Otherwise it
4577 -- is a definite error.
4579 if In_Instance then
4580 Set_Is_Frozen (E, False);
4581 Result := No_List;
4582 return False;
4584 elsif not After_Last_Declaration then
4585 Error_Msg_NE
4586 ("type & must be fully defined before this point",
4588 F_Type);
4589 end if;
4590 end if;
4592 -- Check suspicious parameter for C function. These tests apply
4593 -- only to exported/imported subprograms.
4595 if Warn_On_Export_Import
4596 and then Comes_From_Source (E)
4597 and then Convention (E) in Convention_C_Family
4598 and then (Is_Imported (E) or else Is_Exported (E))
4599 and then Convention (E) /= Convention (Formal)
4600 and then not Has_Warnings_Off (E)
4601 and then not Has_Warnings_Off (F_Type)
4602 and then not Has_Warnings_Off (Formal)
4603 then
4604 -- Qualify mention of formals with subprogram name
4606 Error_Msg_Qual_Level := 1;
4608 -- Check suspicious use of fat C pointer, but do not emit
4609 -- a warning on an access to subprogram when unnesting is
4610 -- active.
4612 if Is_Access_Type (F_Type)
4613 and then Known_Esize (F_Type)
4614 and then Esize (F_Type) > Ttypes.System_Address_Size
4615 and then (not Unnest_Subprogram_Mode
4616 or else not Is_Access_Subprogram_Type (F_Type))
4617 then
4618 Error_Msg_N
4619 ("?x?type of & does not correspond to C pointer!", Formal);
4621 -- Check suspicious return of boolean
4623 elsif Root_Type (F_Type) = Standard_Boolean
4624 and then Convention (F_Type) = Convention_Ada
4625 and then not Has_Warnings_Off (F_Type)
4626 and then not Has_Size_Clause (F_Type)
4627 then
4628 Error_Msg_N
4629 ("& is an 8-bit Ada Boolean?x?", Formal);
4630 Error_Msg_N
4631 ("\use appropriate corresponding type in C "
4632 & "(e.g. char)?x?", Formal);
4634 -- Check suspicious tagged type
4636 elsif (Is_Tagged_Type (F_Type)
4637 or else
4638 (Is_Access_Type (F_Type)
4639 and then Is_Tagged_Type (Designated_Type (F_Type))))
4640 and then Convention (E) = Convention_C
4641 then
4642 Error_Msg_N
4643 ("?x?& involves a tagged type which does not "
4644 & "correspond to any C type!", Formal);
4646 -- Check wrong convention subprogram pointer
4648 elsif Ekind (F_Type) = E_Access_Subprogram_Type
4649 and then not Has_Foreign_Convention (F_Type)
4650 then
4651 Error_Msg_N
4652 ("?x?subprogram pointer & should "
4653 & "have foreign convention!", Formal);
4654 Error_Msg_Sloc := Sloc (F_Type);
4655 Error_Msg_NE
4656 ("\?x?add Convention pragma to declaration of &#",
4657 Formal, F_Type);
4658 end if;
4660 -- Turn off name qualification after message output
4662 Error_Msg_Qual_Level := 0;
4663 end if;
4665 -- Check for unconstrained array in exported foreign convention
4666 -- case.
4668 if Has_Foreign_Convention (E)
4669 and then not Is_Imported (E)
4670 and then Is_Array_Type (F_Type)
4671 and then not Is_Constrained (F_Type)
4672 and then Warn_On_Export_Import
4673 then
4674 Error_Msg_Qual_Level := 1;
4676 -- If this is an inherited operation, place the warning on
4677 -- the derived type declaration, rather than on the original
4678 -- subprogram.
4680 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
4681 then
4682 Warn_Node := Parent (E);
4684 if Formal = First_Formal (E) then
4685 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
4686 end if;
4687 else
4688 Warn_Node := Formal;
4689 end if;
4691 Error_Msg_NE ("?x?type of argument& is unconstrained array",
4692 Warn_Node, Formal);
4693 Error_Msg_N ("\?x?foreign caller must pass bounds explicitly",
4694 Warn_Node);
4695 Error_Msg_Qual_Level := 0;
4696 end if;
4698 if not From_Limited_With (F_Type) then
4699 if Is_Access_Type (F_Type) then
4700 F_Type := Designated_Type (F_Type);
4701 end if;
4702 end if;
4704 Next_Formal (Formal);
4705 end loop;
4707 -- Case of function: similar checks on return type
4709 if Ekind (E) = E_Function then
4711 -- Freeze return type
4713 R_Type := Etype (E);
4715 -- AI05-0151: the return type may have been incomplete at the
4716 -- point of declaration. Replace it with the full view, unless the
4717 -- current type is a limited view. In that case the full view is
4718 -- in a different unit, and gigi finds the non-limited view after
4719 -- the other unit is elaborated.
4721 if Ekind (R_Type) = E_Incomplete_Type
4722 and then Present (Full_View (R_Type))
4723 and then not From_Limited_With (R_Type)
4724 then
4725 R_Type := Full_View (R_Type);
4726 Set_Etype (E, R_Type);
4727 end if;
4729 if Should_Freeze_Type (R_Type, E, N) then
4730 Freeze_And_Append (R_Type, N, Result);
4731 end if;
4733 -- Check suspicious return type for C function
4735 if Warn_On_Export_Import
4736 and then Comes_From_Source (E)
4737 and then Convention (E) in Convention_C_Family
4738 and then (Is_Imported (E) or else Is_Exported (E))
4739 then
4740 -- Check suspicious return of fat C pointer
4742 if Is_Access_Type (R_Type)
4743 and then Known_Esize (R_Type)
4744 and then Esize (R_Type) > Ttypes.System_Address_Size
4745 and then not Has_Warnings_Off (E)
4746 and then not Has_Warnings_Off (R_Type)
4747 then
4748 Error_Msg_N
4749 ("?x?return type of& does not correspond to C pointer!",
4752 -- Check suspicious return of boolean
4754 elsif Root_Type (R_Type) = Standard_Boolean
4755 and then Convention (R_Type) = Convention_Ada
4756 and then not Has_Warnings_Off (E)
4757 and then not Has_Warnings_Off (R_Type)
4758 and then not Has_Size_Clause (R_Type)
4759 then
4760 declare
4761 N : constant Node_Id :=
4762 Result_Definition (Declaration_Node (E));
4763 begin
4764 Error_Msg_NE
4765 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
4766 Error_Msg_NE
4767 ("\use appropriate corresponding type in C "
4768 & "(e.g. char)?x?", N, E);
4769 end;
4771 -- Check suspicious return tagged type
4773 elsif (Is_Tagged_Type (R_Type)
4774 or else (Is_Access_Type (R_Type)
4775 and then
4776 Is_Tagged_Type
4777 (Designated_Type (R_Type))))
4778 and then Convention (E) = Convention_C
4779 and then not Has_Warnings_Off (E)
4780 and then not Has_Warnings_Off (R_Type)
4781 then
4782 Error_Msg_N ("?x?return type of & does not "
4783 & "correspond to C type!", E);
4785 -- Check return of wrong convention subprogram pointer
4787 elsif Ekind (R_Type) = E_Access_Subprogram_Type
4788 and then not Has_Foreign_Convention (R_Type)
4789 and then not Has_Warnings_Off (E)
4790 and then not Has_Warnings_Off (R_Type)
4791 then
4792 Error_Msg_N ("?x?& should return a foreign "
4793 & "convention subprogram pointer", E);
4794 Error_Msg_Sloc := Sloc (R_Type);
4795 Error_Msg_NE
4796 ("\?x?add Convention pragma to declaration of& #",
4797 E, R_Type);
4798 end if;
4799 end if;
4801 -- Give warning for suspicious return of a result of an
4802 -- unconstrained array type in a foreign convention function.
4804 if Has_Foreign_Convention (E)
4806 -- We are looking for a return of unconstrained array
4808 and then Is_Array_Type (R_Type)
4809 and then not Is_Constrained (R_Type)
4811 -- Exclude imported routines, the warning does not belong on
4812 -- the import, but rather on the routine definition.
4814 and then not Is_Imported (E)
4816 -- Check that general warning is enabled, and that it is not
4817 -- suppressed for this particular case.
4819 and then Warn_On_Export_Import
4820 and then not Has_Warnings_Off (E)
4821 and then not Has_Warnings_Off (R_Type)
4822 then
4823 Error_Msg_N
4824 ("?x?foreign convention function& should not return "
4825 & "unconstrained array!", E);
4826 end if;
4827 end if;
4829 -- Check suspicious use of Import in pure unit (cases where the RM
4830 -- allows calls to be omitted).
4832 if Is_Imported (E)
4834 -- It might be suspicious if the compilation unit has the Pure
4835 -- aspect/pragma.
4837 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
4839 -- The RM allows omission of calls only in the case of
4840 -- library-level subprograms (see RM-10.2.1(18)).
4842 and then Is_Library_Level_Entity (E)
4844 -- Ignore internally generated entity. This happens in some cases
4845 -- of subprograms in specs, where we generate an implied body.
4847 and then Comes_From_Source (Import_Pragma (E))
4849 -- Assume run-time knows what it is doing
4851 and then not GNAT_Mode
4853 -- Assume explicit Pure_Function means import is pure
4855 and then not Has_Pragma_Pure_Function (E)
4857 -- Don't need warning in relaxed semantics mode
4859 and then not Relaxed_RM_Semantics
4861 -- Assume convention Intrinsic is OK, since this is specialized.
4862 -- This deals with the DEC unit current_exception.ads
4864 and then Convention (E) /= Convention_Intrinsic
4866 -- Assume that ASM interface knows what it is doing
4868 and then Convention (E) /= Convention_Assembler
4869 then
4870 Error_Msg_N
4871 ("pragma Import in Pure unit??", Import_Pragma (E));
4872 Error_Msg_NE
4873 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
4874 Import_Pragma (E), E);
4875 end if;
4877 return True;
4878 end Freeze_Profile;
4880 ------------------------
4881 -- Freeze_Record_Type --
4882 ------------------------
4884 procedure Freeze_Record_Type (Rec : Entity_Id) is
4885 ADC : Node_Id;
4886 Comp : Entity_Id;
4887 IR : Node_Id;
4888 Prev : Entity_Id;
4890 Junk : Boolean;
4891 pragma Warnings (Off, Junk);
4893 Aliased_Component : Boolean := False;
4894 -- Set True if we find at least one component which is aliased. This
4895 -- is used to prevent Implicit_Packing of the record, since packing
4896 -- cannot modify the size of alignment of an aliased component.
4898 All_Elem_Components : Boolean := True;
4899 -- True if all components are of a type whose underlying type is
4900 -- elementary.
4902 All_Sized_Components : Boolean := True;
4903 -- True if all components have a known RM_Size
4905 All_Storage_Unit_Components : Boolean := True;
4906 -- True if all components have an RM_Size that is a multiple of the
4907 -- storage unit.
4909 Elem_Component_Total_Esize : Uint := Uint_0;
4910 -- Accumulates total Esize values of all elementary components. Used
4911 -- for processing of Implicit_Packing.
4913 Placed_Component : Boolean := False;
4914 -- Set True if we find at least one component with a component
4915 -- clause (used to warn about useless Bit_Order pragmas, and also
4916 -- to detect cases where Implicit_Packing may have an effect).
4918 Sized_Component_Total_RM_Size : Uint := Uint_0;
4919 -- Accumulates total RM_Size values of all sized components. Used
4920 -- for processing of Implicit_Packing.
4922 Sized_Component_Total_Round_RM_Size : Uint := Uint_0;
4923 -- Accumulates total RM_Size values of all sized components, rounded
4924 -- individually to a multiple of the storage unit.
4926 SSO_ADC : Node_Id;
4927 -- Scalar_Storage_Order attribute definition clause for the record
4929 SSO_ADC_Component : Boolean := False;
4930 -- Set True if we find at least one component whose type has a
4931 -- Scalar_Storage_Order attribute definition clause.
4933 Unplaced_Component : Boolean := False;
4934 -- Set True if we find at least one component with no component
4935 -- clause (used to warn about useless Pack pragmas).
4937 procedure Check_Itype (Typ : Entity_Id);
4938 -- If the component subtype is an access to a constrained subtype of
4939 -- an already frozen type, make the subtype frozen as well. It might
4940 -- otherwise be frozen in the wrong scope, and a freeze node on
4941 -- subtype has no effect. Similarly, if the component subtype is a
4942 -- regular (not protected) access to subprogram, set the anonymous
4943 -- subprogram type to frozen as well, to prevent an out-of-scope
4944 -- freeze node at some eventual point of call. Protected operations
4945 -- are handled elsewhere.
4947 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
4948 -- Make sure that all types mentioned in Discrete_Choices of the
4949 -- variants referenceed by the Variant_Part VP are frozen. This is
4950 -- a recursive routine to deal with nested variants.
4952 -----------------
4953 -- Check_Itype --
4954 -----------------
4956 procedure Check_Itype (Typ : Entity_Id) is
4957 Desig : constant Entity_Id := Designated_Type (Typ);
4959 begin
4960 if not Is_Frozen (Desig)
4961 and then Is_Frozen (Base_Type (Desig))
4962 then
4963 Set_Is_Frozen (Desig);
4965 -- In addition, add an Itype_Reference to ensure that the
4966 -- access subtype is elaborated early enough. This cannot be
4967 -- done if the subtype may depend on discriminants.
4969 if Ekind (Comp) = E_Component
4970 and then Is_Itype (Etype (Comp))
4971 and then not Has_Discriminants (Rec)
4972 then
4973 IR := Make_Itype_Reference (Sloc (Comp));
4974 Set_Itype (IR, Desig);
4975 Add_To_Result (IR);
4976 end if;
4978 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
4979 and then Convention (Desig) /= Convention_Protected
4980 then
4981 Set_Is_Frozen (Desig);
4982 end if;
4983 end Check_Itype;
4985 ------------------------------------
4986 -- Freeze_Choices_In_Variant_Part --
4987 ------------------------------------
4989 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
4990 pragma Assert (Nkind (VP) = N_Variant_Part);
4992 Variant : Node_Id;
4993 Choice : Node_Id;
4994 CL : Node_Id;
4996 begin
4997 -- Loop through variants
4999 Variant := First_Non_Pragma (Variants (VP));
5000 while Present (Variant) loop
5002 -- Loop through choices, checking that all types are frozen
5004 Choice := First_Non_Pragma (Discrete_Choices (Variant));
5005 while Present (Choice) loop
5006 if Nkind (Choice) in N_Has_Etype
5007 and then Present (Etype (Choice))
5008 then
5009 Freeze_And_Append (Etype (Choice), N, Result);
5010 end if;
5012 Next_Non_Pragma (Choice);
5013 end loop;
5015 -- Check for nested variant part to process
5017 CL := Component_List (Variant);
5019 if not Null_Present (CL) then
5020 if Present (Variant_Part (CL)) then
5021 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
5022 end if;
5023 end if;
5025 Next_Non_Pragma (Variant);
5026 end loop;
5027 end Freeze_Choices_In_Variant_Part;
5029 -- Start of processing for Freeze_Record_Type
5031 begin
5032 -- Freeze components and embedded subtypes
5034 Comp := First_Entity (Rec);
5035 Prev := Empty;
5036 while Present (Comp) loop
5037 if Is_Aliased (Comp) then
5038 Aliased_Component := True;
5039 end if;
5041 -- Handle the component and discriminant case
5043 if Ekind (Comp) in E_Component | E_Discriminant then
5044 declare
5045 CC : constant Node_Id := Component_Clause (Comp);
5047 begin
5048 -- Freezing a record type freezes the type of each of its
5049 -- components. However, if the type of the component is
5050 -- part of this record, we do not want or need a separate
5051 -- Freeze_Node. Note that Is_Itype is wrong because that's
5052 -- also set in private type cases. We also can't check for
5053 -- the Scope being exactly Rec because of private types and
5054 -- record extensions.
5056 if Is_Itype (Etype (Comp))
5057 and then Is_Record_Type (Underlying_Type
5058 (Scope (Etype (Comp))))
5059 then
5060 Undelay_Type (Etype (Comp));
5061 end if;
5063 Freeze_And_Append (Etype (Comp), N, Result);
5065 -- Warn for pragma Pack overriding foreign convention
5067 if Has_Foreign_Convention (Etype (Comp))
5068 and then Has_Pragma_Pack (Rec)
5070 -- Don't warn for aliased components, since override
5071 -- cannot happen in that case.
5073 and then not Is_Aliased (Comp)
5074 then
5075 declare
5076 CN : constant Name_Id :=
5077 Get_Convention_Name (Convention (Etype (Comp)));
5078 PP : constant Node_Id :=
5079 Get_Pragma (Rec, Pragma_Pack);
5080 begin
5081 if Present (PP) then
5082 Error_Msg_Name_1 := CN;
5083 Error_Msg_Sloc := Sloc (Comp);
5084 Error_Msg_N
5085 ("pragma Pack affects convention % component#??",
5086 PP);
5087 Error_Msg_Name_1 := CN;
5088 Error_Msg_NE
5089 ("\component & may not have % compatible "
5090 & "representation??", PP, Comp);
5091 end if;
5092 end;
5093 end if;
5095 -- Check for error of component clause given for variable
5096 -- sized type. We have to delay this test till this point,
5097 -- since the component type has to be frozen for us to know
5098 -- if it is variable length.
5100 if Present (CC) then
5101 Placed_Component := True;
5103 -- We omit this test in a generic context, it will be
5104 -- applied at instantiation time.
5106 if Inside_A_Generic then
5107 null;
5109 -- Also omit this test in CodePeer mode, since we do not
5110 -- have sufficient info on size and rep clauses.
5112 elsif CodePeer_Mode then
5113 null;
5115 -- Do the check
5117 elsif not
5118 Size_Known_At_Compile_Time
5119 (Underlying_Type (Etype (Comp)))
5120 then
5121 Error_Msg_N
5122 ("component clause not allowed for variable " &
5123 "length component", CC);
5124 end if;
5126 else
5127 Unplaced_Component := True;
5128 end if;
5130 -- Case of component requires byte alignment
5132 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
5134 -- Set the enclosing record to also require byte align
5136 Set_Must_Be_On_Byte_Boundary (Rec);
5138 -- Check for component clause that is inconsistent with
5139 -- the required byte boundary alignment.
5141 if Present (CC)
5142 and then Normalized_First_Bit (Comp) mod
5143 System_Storage_Unit /= 0
5144 then
5145 Error_Msg_N
5146 ("component & must be byte aligned",
5147 Component_Name (Component_Clause (Comp)));
5148 end if;
5149 end if;
5150 end;
5151 end if;
5153 -- Gather data for possible Implicit_Packing later. Note that at
5154 -- this stage we might be dealing with a real component, or with
5155 -- an implicit subtype declaration.
5157 if Known_Static_RM_Size (Etype (Comp)) then
5158 declare
5159 Comp_Type : constant Entity_Id := Etype (Comp);
5160 Comp_Size : constant Uint := RM_Size (Comp_Type);
5161 SSU : constant Int := Ttypes.System_Storage_Unit;
5163 begin
5164 Sized_Component_Total_RM_Size :=
5165 Sized_Component_Total_RM_Size + Comp_Size;
5167 Sized_Component_Total_Round_RM_Size :=
5168 Sized_Component_Total_Round_RM_Size +
5169 (Comp_Size + SSU - 1) / SSU * SSU;
5171 if Present (Underlying_Type (Comp_Type))
5172 and then Is_Elementary_Type (Underlying_Type (Comp_Type))
5173 then
5174 Elem_Component_Total_Esize :=
5175 Elem_Component_Total_Esize + Esize (Comp_Type);
5176 else
5177 All_Elem_Components := False;
5179 if Comp_Size mod SSU /= 0 then
5180 All_Storage_Unit_Components := False;
5181 end if;
5182 end if;
5183 end;
5184 else
5185 All_Sized_Components := False;
5186 end if;
5188 -- If the component is an Itype with Delayed_Freeze and is either
5189 -- a record or array subtype and its base type has not yet been
5190 -- frozen, we must remove this from the entity list of this record
5191 -- and put it on the entity list of the scope of its base type.
5192 -- Note that we know that this is not the type of a component
5193 -- since we cleared Has_Delayed_Freeze for it in the previous
5194 -- loop. Thus this must be the Designated_Type of an access type,
5195 -- which is the type of a component.
5197 if Is_Itype (Comp)
5198 and then Is_Type (Scope (Comp))
5199 and then Is_Composite_Type (Comp)
5200 and then Base_Type (Comp) /= Comp
5201 and then Has_Delayed_Freeze (Comp)
5202 and then not Is_Frozen (Base_Type (Comp))
5203 then
5204 declare
5205 Will_Be_Frozen : Boolean := False;
5206 S : Entity_Id;
5208 begin
5209 -- We have a difficult case to handle here. Suppose Rec is
5210 -- subtype being defined in a subprogram that's created as
5211 -- part of the freezing of Rec'Base. In that case, we know
5212 -- that Comp'Base must have already been frozen by the time
5213 -- we get to elaborate this because Gigi doesn't elaborate
5214 -- any bodies until it has elaborated all of the declarative
5215 -- part. But Is_Frozen will not be set at this point because
5216 -- we are processing code in lexical order.
5218 -- We detect this case by going up the Scope chain of Rec
5219 -- and seeing if we have a subprogram scope before reaching
5220 -- the top of the scope chain or that of Comp'Base. If we
5221 -- do, then mark that Comp'Base will actually be frozen. If
5222 -- so, we merely undelay it.
5224 S := Scope (Rec);
5225 while Present (S) loop
5226 if Is_Subprogram (S) then
5227 Will_Be_Frozen := True;
5228 exit;
5229 elsif S = Scope (Base_Type (Comp)) then
5230 exit;
5231 end if;
5233 S := Scope (S);
5234 end loop;
5236 if Will_Be_Frozen then
5237 Undelay_Type (Comp);
5239 else
5240 if Present (Prev) then
5241 Link_Entities (Prev, Next_Entity (Comp));
5242 else
5243 Set_First_Entity (Rec, Next_Entity (Comp));
5244 end if;
5246 -- Insert in entity list of scope of base type (which
5247 -- must be an enclosing scope, because still unfrozen).
5249 Append_Entity (Comp, Scope (Base_Type (Comp)));
5250 end if;
5251 end;
5253 -- If the component is an access type with an allocator as default
5254 -- value, the designated type will be frozen by the corresponding
5255 -- expression in init_proc. In order to place the freeze node for
5256 -- the designated type before that for the current record type,
5257 -- freeze it now.
5259 -- Same process if the component is an array of access types,
5260 -- initialized with an aggregate. If the designated type is
5261 -- private, it cannot contain allocators, and it is premature
5262 -- to freeze the type, so we check for this as well.
5264 elsif Is_Access_Type (Etype (Comp))
5265 and then Present (Parent (Comp))
5266 and then
5267 Nkind (Parent (Comp))
5268 in N_Component_Declaration | N_Discriminant_Specification
5269 and then Present (Expression (Parent (Comp)))
5270 then
5271 declare
5272 Alloc : constant Node_Id :=
5273 Unqualify (Expression (Parent (Comp)));
5275 begin
5276 if Nkind (Alloc) = N_Allocator then
5278 -- If component is pointer to a class-wide type, freeze
5279 -- the specific type in the expression being allocated.
5280 -- The expression may be a subtype indication, in which
5281 -- case freeze the subtype mark.
5283 if Is_Class_Wide_Type (Designated_Type (Etype (Comp)))
5284 then
5285 if Is_Entity_Name (Expression (Alloc)) then
5286 Freeze_And_Append
5287 (Entity (Expression (Alloc)), N, Result);
5289 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
5290 then
5291 Freeze_And_Append
5292 (Entity (Subtype_Mark (Expression (Alloc))),
5293 N, Result);
5294 end if;
5295 elsif Is_Itype (Designated_Type (Etype (Comp))) then
5296 Check_Itype (Etype (Comp));
5297 else
5298 Freeze_And_Append
5299 (Designated_Type (Etype (Comp)), N, Result);
5300 end if;
5301 end if;
5302 end;
5303 elsif Is_Access_Type (Etype (Comp))
5304 and then Is_Itype (Designated_Type (Etype (Comp)))
5305 then
5306 Check_Itype (Etype (Comp));
5308 -- Freeze the designated type when initializing a component with
5309 -- an aggregate in case the aggregate contains allocators.
5311 -- type T is ...;
5312 -- type T_Ptr is access all T;
5313 -- type T_Array is array ... of T_Ptr;
5315 -- type Rec is record
5316 -- Comp : T_Array := (others => ...);
5317 -- end record;
5319 elsif Is_Array_Type (Etype (Comp))
5320 and then Is_Access_Type (Component_Type (Etype (Comp)))
5321 then
5322 declare
5323 Comp_Par : constant Node_Id := Parent (Comp);
5324 Desig_Typ : constant Entity_Id :=
5325 Designated_Type
5326 (Component_Type (Etype (Comp)));
5328 begin
5329 -- The only case when this sort of freezing is not done is
5330 -- when the designated type is class-wide and the root type
5331 -- is the record owning the component. This scenario results
5332 -- in a circularity because the class-wide type requires
5333 -- primitives that have not been created yet as the root
5334 -- type is in the process of being frozen.
5336 -- type Rec is tagged;
5337 -- type Rec_Ptr is access all Rec'Class;
5338 -- type Rec_Array is array ... of Rec_Ptr;
5340 -- type Rec is record
5341 -- Comp : Rec_Array := (others => ...);
5342 -- end record;
5344 if Is_Class_Wide_Type (Desig_Typ)
5345 and then Root_Type (Desig_Typ) = Rec
5346 then
5347 null;
5349 elsif Is_Fully_Defined (Desig_Typ)
5350 and then Present (Comp_Par)
5351 and then Nkind (Comp_Par) = N_Component_Declaration
5352 and then Present (Expression (Comp_Par))
5353 and then Nkind (Expression (Comp_Par)) = N_Aggregate
5354 then
5355 Freeze_And_Append (Desig_Typ, N, Result);
5356 end if;
5357 end;
5358 end if;
5360 Prev := Comp;
5361 Next_Entity (Comp);
5362 end loop;
5364 SSO_ADC :=
5365 Get_Attribute_Definition_Clause
5366 (Rec, Attribute_Scalar_Storage_Order);
5368 -- If the record type has Complex_Representation, then it is treated
5369 -- as a scalar in the back end so the storage order is irrelevant.
5371 if Has_Complex_Representation (Rec) then
5372 if Present (SSO_ADC) then
5373 Error_Msg_N
5374 ("??storage order has no effect with Complex_Representation",
5375 SSO_ADC);
5376 end if;
5378 else
5379 -- Deal with default setting of reverse storage order
5381 Set_SSO_From_Default (Rec);
5383 -- Check consistent attribute setting on component types
5385 declare
5386 Comp_ADC_Present : Boolean;
5387 begin
5388 Comp := First_Component (Rec);
5389 while Present (Comp) loop
5390 Check_Component_Storage_Order
5391 (Encl_Type => Rec,
5392 Comp => Comp,
5393 ADC => SSO_ADC,
5394 Comp_ADC_Present => Comp_ADC_Present);
5395 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
5396 Next_Component (Comp);
5397 end loop;
5398 end;
5400 -- Now deal with reverse storage order/bit order issues
5402 if Present (SSO_ADC) then
5404 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
5405 -- if the former is specified.
5407 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
5409 -- Note: report error on Rec, not on SSO_ADC, as ADC may
5410 -- apply to some ancestor type.
5412 Error_Msg_Sloc := Sloc (SSO_ADC);
5413 Error_Msg_N
5414 ("scalar storage order for& specified# inconsistent with "
5415 & "bit order", Rec);
5416 end if;
5418 -- Warn if there is a Scalar_Storage_Order attribute definition
5419 -- clause but no component clause, no component that itself has
5420 -- such an attribute definition, and no pragma Pack.
5422 if not (Placed_Component
5423 or else
5424 SSO_ADC_Component
5425 or else
5426 Is_Packed (Rec))
5427 then
5428 Error_Msg_N
5429 ("??scalar storage order specified but no component "
5430 & "clause", SSO_ADC);
5431 end if;
5432 end if;
5433 end if;
5435 -- Deal with Bit_Order aspect
5437 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
5439 if Present (ADC) and then Base_Type (Rec) = Rec then
5440 if not (Placed_Component
5441 or else Present (SSO_ADC)
5442 or else Is_Packed (Rec))
5443 then
5444 -- Warn if clause has no effect when no component clause is
5445 -- present, but suppress warning if the Bit_Order is required
5446 -- due to the presence of a Scalar_Storage_Order attribute.
5448 Error_Msg_N
5449 ("??bit order specification has no effect", ADC);
5450 Error_Msg_N
5451 ("\??since no component clauses were specified", ADC);
5453 -- Here is where we do the processing to adjust component clauses
5454 -- for reversed bit order, when not using reverse SSO. If an error
5455 -- has been reported on Rec already (such as SSO incompatible with
5456 -- bit order), don't bother adjusting as this may generate extra
5457 -- noise.
5459 elsif Reverse_Bit_Order (Rec)
5460 and then not Reverse_Storage_Order (Rec)
5461 and then not Error_Posted (Rec)
5462 then
5463 Adjust_Record_For_Reverse_Bit_Order (Rec);
5465 -- Case where we have both an explicit Bit_Order and the same
5466 -- Scalar_Storage_Order: leave record untouched, the back-end
5467 -- will take care of required layout conversions.
5469 else
5470 null;
5472 end if;
5473 end if;
5475 -- Check for useless pragma Pack when all components placed. We only
5476 -- do this check for record types, not subtypes, since a subtype may
5477 -- have all its components placed, and it still makes perfectly good
5478 -- sense to pack other subtypes or the parent type. We do not give
5479 -- this warning if Optimize_Alignment is set to Space, since the
5480 -- pragma Pack does have an effect in this case (it always resets
5481 -- the alignment to one).
5483 if Ekind (Rec) = E_Record_Type
5484 and then Is_Packed (Rec)
5485 and then not Unplaced_Component
5486 and then Optimize_Alignment /= 'S'
5487 then
5488 -- Reset packed status. Probably not necessary, but we do it so
5489 -- that there is no chance of the back end doing something strange
5490 -- with this redundant indication of packing.
5492 Set_Is_Packed (Rec, False);
5494 -- Give warning if redundant constructs warnings on
5496 if Warn_On_Redundant_Constructs then
5497 Error_Msg_N -- CODEFIX
5498 ("??pragma Pack has no effect, no unplaced components",
5499 Get_Rep_Pragma (Rec, Name_Pack));
5500 end if;
5501 end if;
5503 -- If this is the record corresponding to a remote type, freeze the
5504 -- remote type here since that is what we are semantically freezing.
5505 -- This prevents the freeze node for that type in an inner scope.
5507 if Ekind (Rec) = E_Record_Type then
5508 if Present (Corresponding_Remote_Type (Rec)) then
5509 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
5510 end if;
5512 -- Check for controlled components, unchecked unions, and type
5513 -- invariants.
5515 Comp := First_Component (Rec);
5516 while Present (Comp) loop
5518 -- Do not set Has_Controlled_Component on a class-wide
5519 -- equivalent type. See Make_CW_Equivalent_Type.
5521 if not Is_Class_Wide_Equivalent_Type (Rec)
5522 and then
5523 (Has_Controlled_Component (Etype (Comp))
5524 or else
5525 (Chars (Comp) /= Name_uParent
5526 and then Is_Controlled (Etype (Comp)))
5527 or else
5528 (Is_Protected_Type (Etype (Comp))
5529 and then
5530 Present (Corresponding_Record_Type (Etype (Comp)))
5531 and then
5532 Has_Controlled_Component
5533 (Corresponding_Record_Type (Etype (Comp)))))
5534 then
5535 Set_Has_Controlled_Component (Rec);
5536 end if;
5538 if Has_Unchecked_Union (Etype (Comp)) then
5539 Set_Has_Unchecked_Union (Rec);
5540 end if;
5542 -- The record type requires its own invariant procedure in
5543 -- order to verify the invariant of each individual component.
5544 -- Do not consider internal components such as _parent because
5545 -- parent class-wide invariants are always inherited.
5546 -- In GNATprove mode, the component invariants are checked by
5547 -- other means. They should not be added to the record type
5548 -- invariant procedure, so that the procedure can be used to
5549 -- check the recordy type invariants if any.
5551 if Comes_From_Source (Comp)
5552 and then Has_Invariants (Etype (Comp))
5553 and then not GNATprove_Mode
5554 then
5555 Set_Has_Own_Invariants (Rec);
5556 end if;
5558 -- Scan component declaration for likely misuses of current
5559 -- instance, either in a constraint or a default expression.
5561 if Has_Per_Object_Constraint (Comp) then
5562 Check_Current_Instance (Parent (Comp));
5563 end if;
5565 Next_Component (Comp);
5566 end loop;
5567 end if;
5569 -- Enforce the restriction that access attributes with a current
5570 -- instance prefix can only apply to limited types. This comment
5571 -- is floating here, but does not seem to belong here???
5573 -- Set component alignment if not otherwise already set
5575 Set_Component_Alignment_If_Not_Set (Rec);
5577 -- For first subtypes, check if there are any fixed-point fields with
5578 -- component clauses, where we must check the size. This is not done
5579 -- till the freeze point since for fixed-point types, we do not know
5580 -- the size until the type is frozen. Similar processing applies to
5581 -- bit-packed arrays.
5583 if Is_First_Subtype (Rec) then
5584 Comp := First_Component (Rec);
5585 while Present (Comp) loop
5586 if Present (Component_Clause (Comp))
5587 and then (Is_Fixed_Point_Type (Etype (Comp))
5588 or else Is_Bit_Packed_Array (Etype (Comp)))
5589 then
5590 Check_Size
5591 (Component_Name (Component_Clause (Comp)),
5592 Etype (Comp),
5593 Esize (Comp),
5594 Junk);
5595 end if;
5597 Next_Component (Comp);
5598 end loop;
5599 end if;
5601 -- See if Size is too small as is (and implicit packing might help)
5603 if not Is_Packed (Rec)
5605 -- No implicit packing if even one component is explicitly placed
5607 and then not Placed_Component
5609 -- Or even one component is aliased
5611 and then not Aliased_Component
5613 -- Must have size clause and all sized components
5615 and then Has_Size_Clause (Rec)
5616 and then All_Sized_Components
5618 -- Do not try implicit packing on records with discriminants, too
5619 -- complicated, especially in the variant record case.
5621 and then not Has_Discriminants (Rec)
5623 -- We want to implicitly pack if the specified size of the record
5624 -- is less than the sum of the object sizes (no point in packing
5625 -- if this is not the case), if we can compute it, i.e. if we have
5626 -- only elementary components. Otherwise, we have at least one
5627 -- composite component and we want to implicitly pack only if bit
5628 -- packing is required for it, as we are sure in this case that
5629 -- the back end cannot do the expected layout without packing.
5631 and then
5632 ((All_Elem_Components
5633 and then RM_Size (Rec) < Elem_Component_Total_Esize)
5634 or else
5635 (not All_Elem_Components
5636 and then not All_Storage_Unit_Components
5637 and then RM_Size (Rec) < Sized_Component_Total_Round_RM_Size))
5639 -- And the total RM size cannot be greater than the specified size
5640 -- since otherwise packing will not get us where we have to be.
5642 and then Sized_Component_Total_RM_Size <= RM_Size (Rec)
5644 -- Never do implicit packing in CodePeer or SPARK modes since
5645 -- we don't do any packing in these modes, since this generates
5646 -- over-complex code that confuses static analysis, and in
5647 -- general, neither CodePeer not GNATprove care about the
5648 -- internal representation of objects.
5650 and then not (CodePeer_Mode or GNATprove_Mode)
5651 then
5652 -- If implicit packing enabled, do it
5654 if Implicit_Packing then
5655 Set_Is_Packed (Rec);
5657 -- Otherwise flag the size clause
5659 else
5660 declare
5661 Sz : constant Node_Id := Size_Clause (Rec);
5662 begin
5663 Error_Msg_NE -- CODEFIX
5664 ("size given for& too small", Sz, Rec);
5665 Error_Msg_N -- CODEFIX
5666 ("\use explicit pragma Pack "
5667 & "or use pragma Implicit_Packing", Sz);
5668 end;
5669 end if;
5670 end if;
5672 -- The following checks are relevant only when SPARK_Mode is on as
5673 -- they are not standard Ada legality rules.
5675 if SPARK_Mode = On then
5677 -- A discriminated type cannot be effectively volatile
5678 -- (SPARK RM 7.1.3(5)).
5680 if Is_Effectively_Volatile (Rec) then
5681 if Has_Discriminants (Rec) then
5682 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
5683 end if;
5685 -- A non-effectively volatile record type cannot contain
5686 -- effectively volatile components (SPARK RM 7.1.3(6)).
5688 else
5689 Comp := First_Component (Rec);
5690 while Present (Comp) loop
5691 if Comes_From_Source (Comp)
5692 and then Is_Effectively_Volatile (Etype (Comp))
5693 then
5694 Error_Msg_Name_1 := Chars (Rec);
5695 Error_Msg_N
5696 ("component & of non-volatile type % cannot be "
5697 & "volatile", Comp);
5698 end if;
5700 Next_Component (Comp);
5701 end loop;
5702 end if;
5704 -- A type which does not yield a synchronized object cannot have
5705 -- a component that yields a synchronized object (SPARK RM 9.5).
5707 if not Yields_Synchronized_Object (Rec) then
5708 Comp := First_Component (Rec);
5709 while Present (Comp) loop
5710 if Comes_From_Source (Comp)
5711 and then Yields_Synchronized_Object (Etype (Comp))
5712 then
5713 Error_Msg_Name_1 := Chars (Rec);
5714 Error_Msg_N
5715 ("component & of non-synchronized type % cannot be "
5716 & "synchronized", Comp);
5717 end if;
5719 Next_Component (Comp);
5720 end loop;
5721 end if;
5723 -- A Ghost type cannot have a component of protected or task type
5724 -- (SPARK RM 6.9(19)).
5726 if Is_Ghost_Entity (Rec) then
5727 Comp := First_Component (Rec);
5728 while Present (Comp) loop
5729 if Comes_From_Source (Comp)
5730 and then Is_Concurrent_Type (Etype (Comp))
5731 then
5732 Error_Msg_Name_1 := Chars (Rec);
5733 Error_Msg_N
5734 ("component & of ghost type % cannot be concurrent",
5735 Comp);
5736 end if;
5738 Next_Component (Comp);
5739 end loop;
5740 end if;
5741 end if;
5743 -- Make sure that if we have an iterator aspect, then we have
5744 -- either Constant_Indexing or Variable_Indexing.
5746 declare
5747 Iterator_Aspect : Node_Id;
5749 begin
5750 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
5752 if No (Iterator_Aspect) then
5753 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
5754 end if;
5756 if Present (Iterator_Aspect) then
5757 if Has_Aspect (Rec, Aspect_Constant_Indexing)
5758 or else
5759 Has_Aspect (Rec, Aspect_Variable_Indexing)
5760 then
5761 null;
5762 else
5763 Error_Msg_N
5764 ("Iterator_Element requires indexing aspect",
5765 Iterator_Aspect);
5766 end if;
5767 end if;
5768 end;
5770 -- All done if not a full record definition
5772 if Ekind (Rec) /= E_Record_Type then
5773 return;
5774 end if;
5776 -- Finally we need to check the variant part to make sure that
5777 -- all types within choices are properly frozen as part of the
5778 -- freezing of the record type.
5780 Check_Variant_Part : declare
5781 D : constant Node_Id := Declaration_Node (Rec);
5782 T : Node_Id;
5783 C : Node_Id;
5785 begin
5786 -- Find component list
5788 C := Empty;
5790 if Nkind (D) = N_Full_Type_Declaration then
5791 T := Type_Definition (D);
5793 if Nkind (T) = N_Record_Definition then
5794 C := Component_List (T);
5796 elsif Nkind (T) = N_Derived_Type_Definition
5797 and then Present (Record_Extension_Part (T))
5798 then
5799 C := Component_List (Record_Extension_Part (T));
5800 end if;
5801 end if;
5803 -- Case of variant part present
5805 if Present (C) and then Present (Variant_Part (C)) then
5806 Freeze_Choices_In_Variant_Part (Variant_Part (C));
5807 end if;
5809 -- Note: we used to call Check_Choices here, but it is too early,
5810 -- since predicated subtypes are frozen here, but their freezing
5811 -- actions are in Analyze_Freeze_Entity, which has not been called
5812 -- yet for entities frozen within this procedure, so we moved that
5813 -- call to the Analyze_Freeze_Entity for the record type.
5815 end Check_Variant_Part;
5817 -- Check that all the primitives of an interface type are abstract
5818 -- or null procedures.
5820 if Is_Interface (Rec)
5821 and then not Error_Posted (Parent (Rec))
5822 then
5823 declare
5824 Elmt : Elmt_Id;
5825 Subp : Entity_Id;
5827 begin
5828 Elmt := First_Elmt (Primitive_Operations (Rec));
5829 while Present (Elmt) loop
5830 Subp := Node (Elmt);
5832 if not Is_Abstract_Subprogram (Subp)
5834 -- Avoid reporting the error on inherited primitives
5836 and then Comes_From_Source (Subp)
5837 then
5838 Error_Msg_Name_1 := Chars (Subp);
5840 if Ekind (Subp) = E_Procedure then
5841 if not Null_Present (Parent (Subp)) then
5842 Error_Msg_N
5843 ("interface procedure % must be abstract or null",
5844 Parent (Subp));
5845 end if;
5846 else
5847 Error_Msg_N
5848 ("interface function % must be abstract",
5849 Parent (Subp));
5850 end if;
5851 end if;
5853 Next_Elmt (Elmt);
5854 end loop;
5855 end;
5856 end if;
5858 -- For a derived tagged type, check whether inherited primitives
5859 -- might require a wrapper to handle class-wide conditions.
5861 if Is_Tagged_Type (Rec) and then Is_Derived_Type (Rec) then
5862 Check_Inherited_Conditions (Rec);
5863 end if;
5864 end Freeze_Record_Type;
5866 -------------------------------
5867 -- Has_Boolean_Aspect_Import --
5868 -------------------------------
5870 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
5871 Decl : constant Node_Id := Declaration_Node (E);
5872 Asp : Node_Id;
5873 Expr : Node_Id;
5875 begin
5876 if Has_Aspects (Decl) then
5877 Asp := First (Aspect_Specifications (Decl));
5878 while Present (Asp) loop
5879 Expr := Expression (Asp);
5881 -- The value of aspect Import is True when the expression is
5882 -- either missing or it is explicitly set to True.
5884 if Get_Aspect_Id (Asp) = Aspect_Import
5885 and then (No (Expr)
5886 or else (Compile_Time_Known_Value (Expr)
5887 and then Is_True (Expr_Value (Expr))))
5888 then
5889 return True;
5890 end if;
5892 Next (Asp);
5893 end loop;
5894 end if;
5896 return False;
5897 end Has_Boolean_Aspect_Import;
5899 -------------------------
5900 -- Inherit_Freeze_Node --
5901 -------------------------
5903 procedure Inherit_Freeze_Node
5904 (Fnod : Node_Id;
5905 Typ : Entity_Id)
5907 Typ_Fnod : constant Node_Id := Freeze_Node (Typ);
5909 begin
5910 Set_Freeze_Node (Typ, Fnod);
5911 Set_Entity (Fnod, Typ);
5913 -- The input type had an existing node. Propagate relevant attributes
5914 -- from the old freeze node to the inherited freeze node.
5916 -- ??? if both freeze nodes have attributes, would they differ?
5918 if Present (Typ_Fnod) then
5920 -- Attribute Access_Types_To_Process
5922 if Present (Access_Types_To_Process (Typ_Fnod))
5923 and then No (Access_Types_To_Process (Fnod))
5924 then
5925 Set_Access_Types_To_Process (Fnod,
5926 Access_Types_To_Process (Typ_Fnod));
5927 end if;
5929 -- Attribute Actions
5931 if Present (Actions (Typ_Fnod)) and then No (Actions (Fnod)) then
5932 Set_Actions (Fnod, Actions (Typ_Fnod));
5933 end if;
5935 -- Attribute First_Subtype_Link
5937 if Present (First_Subtype_Link (Typ_Fnod))
5938 and then No (First_Subtype_Link (Fnod))
5939 then
5940 Set_First_Subtype_Link (Fnod, First_Subtype_Link (Typ_Fnod));
5941 end if;
5943 -- Attribute TSS_Elist
5945 if Present (TSS_Elist (Typ_Fnod))
5946 and then No (TSS_Elist (Fnod))
5947 then
5948 Set_TSS_Elist (Fnod, TSS_Elist (Typ_Fnod));
5949 end if;
5950 end if;
5951 end Inherit_Freeze_Node;
5953 ------------------------------
5954 -- Wrap_Imported_Subprogram --
5955 ------------------------------
5957 -- The issue here is that our normal approach of checking preconditions
5958 -- and postconditions does not work for imported procedures, since we
5959 -- are not generating code for the body. To get around this we create
5960 -- a wrapper, as shown by the following example:
5962 -- procedure K (A : Integer);
5963 -- pragma Import (C, K);
5965 -- The spec is rewritten by removing the effects of pragma Import, but
5966 -- leaving the convention unchanged, as though the source had said:
5968 -- procedure K (A : Integer);
5969 -- pragma Convention (C, K);
5971 -- and we create a body, added to the entity K freeze actions, which
5972 -- looks like:
5974 -- procedure K (A : Integer) is
5975 -- procedure K (A : Integer);
5976 -- pragma Import (C, K);
5977 -- begin
5978 -- K (A);
5979 -- end K;
5981 -- Now the contract applies in the normal way to the outer procedure,
5982 -- and the inner procedure has no contracts, so there is no problem
5983 -- in just calling it to get the original effect.
5985 -- In the case of a function, we create an appropriate return statement
5986 -- for the subprogram body that calls the inner procedure.
5988 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
5989 function Copy_Import_Pragma return Node_Id;
5990 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
5992 ------------------------
5993 -- Copy_Import_Pragma --
5994 ------------------------
5996 function Copy_Import_Pragma return Node_Id is
5998 -- The subprogram should have an import pragma, otherwise it does
5999 -- need a wrapper.
6001 Prag : constant Node_Id := Import_Pragma (E);
6002 pragma Assert (Present (Prag));
6004 -- Save all semantic fields of the pragma
6006 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
6007 Save_From : constant Boolean := From_Aspect_Specification (Prag);
6008 Save_Prag : constant Node_Id := Next_Pragma (Prag);
6009 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
6011 Result : Node_Id;
6013 begin
6014 -- Reset all semantic fields. This avoids a potential infinite
6015 -- loop when the pragma comes from an aspect as the duplication
6016 -- will copy the aspect, then copy the corresponding pragma and
6017 -- so on.
6019 Set_Corresponding_Aspect (Prag, Empty);
6020 Set_From_Aspect_Specification (Prag, False);
6021 Set_Next_Pragma (Prag, Empty);
6022 Set_Next_Rep_Item (Prag, Empty);
6024 Result := Copy_Separate_Tree (Prag);
6026 -- Restore the original semantic fields
6028 Set_Corresponding_Aspect (Prag, Save_Asp);
6029 Set_From_Aspect_Specification (Prag, Save_From);
6030 Set_Next_Pragma (Prag, Save_Prag);
6031 Set_Next_Rep_Item (Prag, Save_Rep);
6033 return Result;
6034 end Copy_Import_Pragma;
6036 -- Local variables
6038 Loc : constant Source_Ptr := Sloc (E);
6039 CE : constant Name_Id := Chars (E);
6040 Bod : Node_Id;
6041 Forml : Entity_Id;
6042 Parms : List_Id;
6043 Prag : Node_Id;
6044 Spec : Node_Id;
6045 Stmt : Node_Id;
6047 -- Start of processing for Wrap_Imported_Subprogram
6049 begin
6050 -- Nothing to do if not imported
6052 if not Is_Imported (E) then
6053 return;
6055 -- Test enabling conditions for wrapping
6057 elsif Is_Subprogram (E)
6058 and then Present (Contract (E))
6059 and then Present (Pre_Post_Conditions (Contract (E)))
6060 and then not GNATprove_Mode
6061 then
6062 -- Here we do the wrap
6064 -- Note on calls to Copy_Separate_Tree. The trees we are copying
6065 -- here are fully analyzed, but we definitely want fully syntactic
6066 -- unanalyzed trees in the body we construct, so that the analysis
6067 -- generates the right visibility, and that is exactly what the
6068 -- calls to Copy_Separate_Tree give us.
6070 Prag := Copy_Import_Pragma;
6072 -- Fix up spec so it is no longer imported and has convention Ada
6074 Set_Has_Completion (E, False);
6075 Set_Import_Pragma (E, Empty);
6076 Set_Interface_Name (E, Empty);
6077 Set_Is_Imported (E, False);
6078 Set_Convention (E, Convention_Ada);
6080 -- Grab the subprogram declaration and specification
6082 Spec := Declaration_Node (E);
6084 -- Build parameter list that we need
6086 Parms := New_List;
6087 Forml := First_Formal (E);
6088 while Present (Forml) loop
6089 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
6090 Next_Formal (Forml);
6091 end loop;
6093 -- Build the call
6095 -- An imported function whose result type is anonymous access
6096 -- creates a new anonymous access type when it is relocated into
6097 -- the declarations of the body generated below. As a result, the
6098 -- accessibility level of these two anonymous access types may not
6099 -- be compatible even though they are essentially the same type.
6100 -- Use an unchecked type conversion to reconcile this case. Note
6101 -- that the conversion is safe because in the named access type
6102 -- case, both the body and imported function utilize the same
6103 -- type.
6105 if Ekind (E) in E_Function | E_Generic_Function then
6106 Stmt :=
6107 Make_Simple_Return_Statement (Loc,
6108 Expression =>
6109 Unchecked_Convert_To (Etype (E),
6110 Make_Function_Call (Loc,
6111 Name => Make_Identifier (Loc, CE),
6112 Parameter_Associations => Parms)));
6114 else
6115 Stmt :=
6116 Make_Procedure_Call_Statement (Loc,
6117 Name => Make_Identifier (Loc, CE),
6118 Parameter_Associations => Parms);
6119 end if;
6121 -- Now build the body
6123 Bod :=
6124 Make_Subprogram_Body (Loc,
6125 Specification =>
6126 Copy_Separate_Tree (Spec),
6127 Declarations => New_List (
6128 Make_Subprogram_Declaration (Loc,
6129 Specification => Copy_Separate_Tree (Spec)),
6130 Prag),
6131 Handled_Statement_Sequence =>
6132 Make_Handled_Sequence_Of_Statements (Loc,
6133 Statements => New_List (Stmt),
6134 End_Label => Make_Identifier (Loc, CE)));
6136 -- Append the body to freeze result
6138 Add_To_Result (Bod);
6139 return;
6141 -- Case of imported subprogram that does not get wrapped
6143 else
6144 -- Set Is_Public. All imported entities need an external symbol
6145 -- created for them since they are always referenced from another
6146 -- object file. Note this used to be set when we set Is_Imported
6147 -- back in Sem_Prag, but now we delay it to this point, since we
6148 -- don't want to set this flag if we wrap an imported subprogram.
6150 Set_Is_Public (E);
6151 end if;
6152 end Wrap_Imported_Subprogram;
6154 -- Start of processing for Freeze_Entity
6156 begin
6157 -- The entity being frozen may be subject to pragma Ghost. Set the mode
6158 -- now to ensure that any nodes generated during freezing are properly
6159 -- flagged as Ghost.
6161 Set_Ghost_Mode (E);
6163 -- We are going to test for various reasons why this entity need not be
6164 -- frozen here, but in the case of an Itype that's defined within a
6165 -- record, that test actually applies to the record.
6167 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
6168 Test_E := Scope (E);
6170 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
6171 and then Is_Record_Type (Underlying_Type (Scope (E)))
6172 then
6173 Test_E := Underlying_Type (Scope (E));
6174 end if;
6176 -- Do not freeze if already frozen since we only need one freeze node
6178 if Is_Frozen (E) then
6179 Result := No_List;
6180 goto Leave;
6182 -- Do not freeze if we are preanalyzing without freezing
6184 elsif Inside_Preanalysis_Without_Freezing > 0 then
6185 Result := No_List;
6186 goto Leave;
6188 elsif Ekind (E) = E_Generic_Package then
6189 Result := Freeze_Generic_Entities (E);
6190 goto Leave;
6192 -- It is improper to freeze an external entity within a generic because
6193 -- its freeze node will appear in a non-valid context. The entity will
6194 -- be frozen in the proper scope after the current generic is analyzed.
6195 -- However, aspects must be analyzed because they may be queried later
6196 -- within the generic itself, and the corresponding pragma or attribute
6197 -- definition has not been analyzed yet. After this, indicate that the
6198 -- entity has no further delayed aspects, to prevent a later aspect
6199 -- analysis out of the scope of the generic.
6201 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
6202 if Has_Delayed_Aspects (E) then
6203 Analyze_Aspects_At_Freeze_Point (E);
6204 Set_Has_Delayed_Aspects (E, False);
6205 end if;
6207 Result := No_List;
6208 goto Leave;
6210 -- AI05-0213: A formal incomplete type does not freeze the actual. In
6211 -- the instance, the same applies to the subtype renaming the actual.
6213 elsif Is_Private_Type (E)
6214 and then Is_Generic_Actual_Type (E)
6215 and then No (Full_View (Base_Type (E)))
6216 and then Ada_Version >= Ada_2012
6217 then
6218 Result := No_List;
6219 goto Leave;
6221 -- Formal subprograms are never frozen
6223 elsif Is_Formal_Subprogram (E) then
6224 Result := No_List;
6225 goto Leave;
6227 -- Generic types are never frozen as they lack delayed semantic checks
6229 elsif Is_Generic_Type (E) then
6230 Result := No_List;
6231 goto Leave;
6233 -- Do not freeze a global entity within an inner scope created during
6234 -- expansion. A call to subprogram E within some internal procedure
6235 -- (a stream attribute for example) might require freezing E, but the
6236 -- freeze node must appear in the same declarative part as E itself.
6237 -- The two-pass elaboration mechanism in gigi guarantees that E will
6238 -- be frozen before the inner call is elaborated. We exclude constants
6239 -- from this test, because deferred constants may be frozen early, and
6240 -- must be diagnosed (e.g. in the case of a deferred constant being used
6241 -- in a default expression). If the enclosing subprogram comes from
6242 -- source, or is a generic instance, then the freeze point is the one
6243 -- mandated by the language, and we freeze the entity. A subprogram that
6244 -- is a child unit body that acts as a spec does not have a spec that
6245 -- comes from source, but can only come from source.
6247 elsif In_Open_Scopes (Scope (Test_E))
6248 and then Scope (Test_E) /= Current_Scope
6249 and then Ekind (Test_E) /= E_Constant
6250 then
6251 declare
6252 S : Entity_Id;
6254 begin
6255 S := Current_Scope;
6256 while Present (S) loop
6257 if Is_Overloadable (S) then
6258 if Comes_From_Source (S)
6259 or else Is_Generic_Instance (S)
6260 or else Is_Child_Unit (S)
6261 then
6262 exit;
6263 else
6264 Result := No_List;
6265 goto Leave;
6266 end if;
6267 end if;
6269 S := Scope (S);
6270 end loop;
6271 end;
6273 -- Similarly, an inlined instance body may make reference to global
6274 -- entities, but these references cannot be the proper freezing point
6275 -- for them, and in the absence of inlining freezing will take place in
6276 -- their own scope. Normally instance bodies are analyzed after the
6277 -- enclosing compilation, and everything has been frozen at the proper
6278 -- place, but with front-end inlining an instance body is compiled
6279 -- before the end of the enclosing scope, and as a result out-of-order
6280 -- freezing must be prevented.
6282 elsif Front_End_Inlining
6283 and then In_Instance_Body
6284 and then Present (Scope (Test_E))
6285 then
6286 declare
6287 S : Entity_Id;
6289 begin
6290 S := Scope (Test_E);
6291 while Present (S) loop
6292 if Is_Generic_Instance (S) then
6293 exit;
6294 else
6295 S := Scope (S);
6296 end if;
6297 end loop;
6299 if No (S) then
6300 Result := No_List;
6301 goto Leave;
6302 end if;
6303 end;
6304 end if;
6306 -- Add checks to detect proper initialization of scalars that may appear
6307 -- as subprogram parameters.
6309 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
6310 Apply_Parameter_Validity_Checks (E);
6311 end if;
6313 -- Deal with delayed aspect specifications. The analysis of the aspect
6314 -- is required to be delayed to the freeze point, thus we analyze the
6315 -- pragma or attribute definition clause in the tree at this point. We
6316 -- also analyze the aspect specification node at the freeze point when
6317 -- the aspect doesn't correspond to pragma/attribute definition clause.
6318 -- In addition, a derived type may have inherited aspects that were
6319 -- delayed in the parent, so these must also be captured now.
6321 -- For a record type, we deal with the delayed aspect specifications on
6322 -- components first, which is consistent with the non-delayed case and
6323 -- makes it possible to have a single processing to detect conflicts.
6325 if Is_Record_Type (E) then
6326 declare
6327 Comp : Entity_Id;
6329 Rec_Pushed : Boolean := False;
6330 -- Set True if the record type E has been pushed on the scope
6331 -- stack. Needed for the analysis of delayed aspects specified
6332 -- to the components of Rec.
6334 begin
6335 Comp := First_Component (E);
6336 while Present (Comp) loop
6337 if Has_Delayed_Aspects (Comp) then
6338 if not Rec_Pushed then
6339 Push_Scope (E);
6340 Rec_Pushed := True;
6342 -- The visibility to the discriminants must be restored
6343 -- in order to properly analyze the aspects.
6345 if Has_Discriminants (E) then
6346 Install_Discriminants (E);
6347 end if;
6348 end if;
6350 Analyze_Aspects_At_Freeze_Point (Comp);
6351 end if;
6353 Next_Component (Comp);
6354 end loop;
6356 -- Pop the scope if Rec scope has been pushed on the scope stack
6357 -- during the delayed aspect analysis process.
6359 if Rec_Pushed then
6360 if Has_Discriminants (E) then
6361 Uninstall_Discriminants (E);
6362 end if;
6364 Pop_Scope;
6365 end if;
6366 end;
6367 end if;
6369 if Has_Delayed_Aspects (E)
6370 or else May_Inherit_Delayed_Rep_Aspects (E)
6371 then
6372 Analyze_Aspects_At_Freeze_Point (E);
6373 end if;
6375 -- Here to freeze the entity
6377 Set_Is_Frozen (E);
6379 -- Case of entity being frozen is other than a type
6381 if not Is_Type (E) then
6383 -- If entity is exported or imported and does not have an external
6384 -- name, now is the time to provide the appropriate default name.
6385 -- Skip this if the entity is stubbed, since we don't need a name
6386 -- for any stubbed routine. For the case on intrinsics, if no
6387 -- external name is specified, then calls will be handled in
6388 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
6389 -- external name is provided, then Expand_Intrinsic_Call leaves
6390 -- calls in place for expansion by GIGI.
6392 if (Is_Imported (E) or else Is_Exported (E))
6393 and then No (Interface_Name (E))
6394 and then Convention (E) /= Convention_Stubbed
6395 and then Convention (E) /= Convention_Intrinsic
6396 then
6397 Set_Encoded_Interface_Name
6398 (E, Get_Default_External_Name (E));
6399 end if;
6401 -- Subprogram case
6403 if Is_Subprogram (E) then
6405 -- Check for needing to wrap imported subprogram
6407 Wrap_Imported_Subprogram (E);
6409 -- Freeze all parameter types and the return type (RM 13.14(14)).
6410 -- However skip this for internal subprograms. This is also where
6411 -- any extra formal parameters are created since we now know
6412 -- whether the subprogram will use a foreign convention.
6414 -- In Ada 2012, freezing a subprogram does not always freeze the
6415 -- corresponding profile (see AI05-019). An attribute reference
6416 -- is not a freezing point of the profile. Similarly, we do not
6417 -- freeze the profile of primitives of a library-level tagged type
6418 -- when we are building its dispatch table. Flag Do_Freeze_Profile
6419 -- indicates whether the profile should be frozen now.
6421 -- This processing doesn't apply to internal entities (see below)
6423 if not Is_Internal (E) and then Do_Freeze_Profile then
6424 if not Freeze_Profile (E) then
6425 goto Leave;
6426 end if;
6427 end if;
6429 -- Must freeze its parent first if it is a derived subprogram
6431 if Present (Alias (E)) then
6432 Freeze_And_Append (Alias (E), N, Result);
6433 end if;
6435 -- We don't freeze internal subprograms, because we don't normally
6436 -- want addition of extra formals or mechanism setting to happen
6437 -- for those. However we do pass through predefined dispatching
6438 -- cases, since extra formals may be needed in some cases, such as
6439 -- for the stream 'Input function (build-in-place formals).
6441 if not Is_Internal (E)
6442 or else Is_Predefined_Dispatching_Operation (E)
6443 then
6444 Freeze_Subprogram (E);
6445 end if;
6447 -- If warning on suspicious contracts then check for the case of
6448 -- a postcondition other than False for a No_Return subprogram.
6450 if No_Return (E)
6451 and then Warn_On_Suspicious_Contract
6452 and then Present (Contract (E))
6453 then
6454 declare
6455 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
6456 Exp : Node_Id;
6458 begin
6459 while Present (Prag) loop
6460 if Pragma_Name_Unmapped (Prag) in Name_Post
6461 | Name_Postcondition
6462 | Name_Refined_Post
6463 then
6464 Exp :=
6465 Expression
6466 (First (Pragma_Argument_Associations (Prag)));
6468 if Nkind (Exp) /= N_Identifier
6469 or else Chars (Exp) /= Name_False
6470 then
6471 Error_Msg_NE
6472 ("useless postcondition, & is marked "
6473 & "No_Return?.t?", Exp, E);
6474 end if;
6475 end if;
6477 Prag := Next_Pragma (Prag);
6478 end loop;
6479 end;
6480 end if;
6482 -- Here for other than a subprogram or type
6484 else
6485 -- If entity has a type declared in the current scope, and it is
6486 -- not a generic unit, then freeze it first.
6488 if Present (Etype (E))
6489 and then Ekind (E) /= E_Generic_Function
6490 and then Within_Scope (Etype (E), Current_Scope)
6491 then
6492 Freeze_And_Append (Etype (E), N, Result);
6494 -- For an object of an anonymous array type, aspects on the
6495 -- object declaration apply to the type itself. This is the
6496 -- case for Atomic_Components, Volatile_Components, and
6497 -- Independent_Components. In these cases analysis of the
6498 -- generated pragma will mark the anonymous types accordingly,
6499 -- and the object itself does not require a freeze node.
6501 if Ekind (E) = E_Variable
6502 and then Is_Itype (Etype (E))
6503 and then Is_Array_Type (Etype (E))
6504 and then Has_Delayed_Aspects (E)
6505 then
6506 Set_Has_Delayed_Aspects (E, False);
6507 Set_Has_Delayed_Freeze (E, False);
6508 Set_Freeze_Node (E, Empty);
6509 end if;
6510 end if;
6512 -- Special processing for objects created by object declaration;
6513 -- we protect the call to Declaration_Node against entities of
6514 -- expressions replaced by the frontend with an N_Raise_CE node.
6516 if Ekind (E) in E_Constant | E_Variable
6517 and then Nkind (Declaration_Node (E)) = N_Object_Declaration
6518 then
6519 Freeze_Object_Declaration (E);
6520 end if;
6522 -- Check that a constant which has a pragma Volatile[_Components]
6523 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
6525 -- Note: Atomic[_Components] also sets Volatile[_Components]
6527 if Ekind (E) = E_Constant
6528 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
6529 and then not Is_Imported (E)
6530 and then not Has_Boolean_Aspect_Import (E)
6531 then
6532 -- Make sure we actually have a pragma, and have not merely
6533 -- inherited the indication from elsewhere (e.g. an address
6534 -- clause, which is not good enough in RM terms).
6536 if Has_Rep_Pragma (E, Name_Atomic)
6537 or else
6538 Has_Rep_Pragma (E, Name_Atomic_Components)
6539 then
6540 Error_Msg_N
6541 ("standalone atomic constant must be " &
6542 "imported (RM C.6(13))", E);
6544 elsif Has_Rep_Pragma (E, Name_Volatile)
6545 or else
6546 Has_Rep_Pragma (E, Name_Volatile_Components)
6547 then
6548 Error_Msg_N
6549 ("standalone volatile constant must be " &
6550 "imported (RM C.6(13))", E);
6551 end if;
6552 end if;
6554 -- Static objects require special handling
6556 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
6557 and then Is_Statically_Allocated (E)
6558 then
6559 Freeze_Static_Object (E);
6560 end if;
6562 -- Remaining step is to layout objects
6564 if Ekind (E) in E_Variable | E_Constant | E_Loop_Parameter
6565 or else Is_Formal (E)
6566 then
6567 Layout_Object (E);
6568 end if;
6570 -- For an object that does not have delayed freezing, and whose
6571 -- initialization actions have been captured in a compound
6572 -- statement, move them back now directly within the enclosing
6573 -- statement sequence.
6575 if Ekind (E) in E_Constant | E_Variable
6576 and then not Has_Delayed_Freeze (E)
6577 then
6578 Explode_Initialization_Compound_Statement (E);
6579 end if;
6581 -- Do not generate a freeze node for a generic unit
6583 if Is_Generic_Unit (E) then
6584 Result := No_List;
6585 goto Leave;
6586 end if;
6587 end if;
6589 -- Case of a type or subtype being frozen
6591 else
6592 -- Verify several SPARK legality rules related to Ghost types now
6593 -- that the type is frozen.
6595 Check_Ghost_Type (E);
6597 -- We used to check here that a full type must have preelaborable
6598 -- initialization if it completes a private type specified with
6599 -- pragma Preelaborable_Initialization, but that missed cases where
6600 -- the types occur within a generic package, since the freezing
6601 -- that occurs within a containing scope generally skips traversal
6602 -- of a generic unit's declarations (those will be frozen within
6603 -- instances). This check was moved to Analyze_Package_Specification.
6605 -- The type may be defined in a generic unit. This can occur when
6606 -- freezing a generic function that returns the type (which is
6607 -- defined in a parent unit). It is clearly meaningless to freeze
6608 -- this type. However, if it is a subtype, its size may be determi-
6609 -- nable and used in subsequent checks, so might as well try to
6610 -- compute it.
6612 -- In Ada 2012, Freeze_Entities is also used in the front end to
6613 -- trigger the analysis of aspect expressions, so in this case we
6614 -- want to continue the freezing process.
6616 -- Is_Generic_Unit (Scope (E)) is dubious here, do we want instead
6617 -- In_Generic_Scope (E)???
6619 if Present (Scope (E))
6620 and then Is_Generic_Unit (Scope (E))
6621 and then
6622 (not Has_Predicates (E)
6623 and then not Has_Delayed_Freeze (E))
6624 then
6625 Check_Compile_Time_Size (E);
6626 Result := No_List;
6627 goto Leave;
6628 end if;
6630 -- Check for error of Type_Invariant'Class applied to an untagged
6631 -- type (check delayed to freeze time when full type is available).
6633 declare
6634 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
6635 begin
6636 if Present (Prag)
6637 and then Class_Present (Prag)
6638 and then not Is_Tagged_Type (E)
6639 then
6640 Error_Msg_NE
6641 ("Type_Invariant''Class cannot be specified for &", Prag, E);
6642 Error_Msg_N
6643 ("\can only be specified for a tagged type", Prag);
6644 end if;
6645 end;
6647 -- Deal with special cases of freezing for subtype
6649 if E /= Base_Type (E) then
6651 -- Before we do anything else, a specific test for the case of a
6652 -- size given for an array where the array would need to be packed
6653 -- in order for the size to be honored, but is not. This is the
6654 -- case where implicit packing may apply. The reason we do this so
6655 -- early is that, if we have implicit packing, the layout of the
6656 -- base type is affected, so we must do this before we freeze the
6657 -- base type.
6659 -- We could do this processing only if implicit packing is enabled
6660 -- since in all other cases, the error would be caught by the back
6661 -- end. However, we choose to do the check even if we do not have
6662 -- implicit packing enabled, since this allows us to give a more
6663 -- useful error message (advising use of pragma Implicit_Packing
6664 -- or pragma Pack).
6666 if Is_Array_Type (E) then
6667 declare
6668 Ctyp : constant Entity_Id := Component_Type (E);
6669 Rsiz : constant Uint :=
6670 (if Known_RM_Size (Ctyp) then RM_Size (Ctyp) else Uint_0);
6671 SZ : constant Node_Id := Size_Clause (E);
6672 Btyp : constant Entity_Id := Base_Type (E);
6674 Lo : Node_Id;
6675 Hi : Node_Id;
6676 Indx : Node_Id;
6678 Dim : Uint;
6679 Num_Elmts : Uint := Uint_1;
6680 -- Number of elements in array
6682 begin
6683 -- Check enabling conditions. These are straightforward
6684 -- except for the test for a limited composite type. This
6685 -- eliminates the rare case of a array of limited components
6686 -- where there are issues of whether or not we can go ahead
6687 -- and pack the array (since we can't freely pack and unpack
6688 -- arrays if they are limited).
6690 -- Note that we check the root type explicitly because the
6691 -- whole point is we are doing this test before we have had
6692 -- a chance to freeze the base type (and it is that freeze
6693 -- action that causes stuff to be inherited).
6695 -- The conditions on the size are identical to those used in
6696 -- Freeze_Array_Type to set the Is_Packed flag.
6698 if Has_Size_Clause (E)
6699 and then Known_Static_RM_Size (E)
6700 and then not Is_Packed (E)
6701 and then not Has_Pragma_Pack (E)
6702 and then not Has_Component_Size_Clause (E)
6703 and then Known_Static_RM_Size (Ctyp)
6704 and then Rsiz <= System_Max_Integer_Size
6705 and then not (Addressable (Rsiz)
6706 and then Known_Static_Esize (Ctyp)
6707 and then Esize (Ctyp) = Rsiz)
6708 and then not (Rsiz mod System_Storage_Unit = 0
6709 and then Is_Composite_Type (Ctyp))
6710 and then not Is_Limited_Composite (E)
6711 and then not Is_Packed (Root_Type (E))
6712 and then not Has_Component_Size_Clause (Root_Type (E))
6713 and then not (CodePeer_Mode or GNATprove_Mode)
6714 then
6715 -- Compute number of elements in array
6717 Indx := First_Index (E);
6718 while Present (Indx) loop
6719 Get_Index_Bounds (Indx, Lo, Hi);
6721 if not (Compile_Time_Known_Value (Lo)
6722 and then
6723 Compile_Time_Known_Value (Hi))
6724 then
6725 goto No_Implicit_Packing;
6726 end if;
6728 Dim := Expr_Value (Hi) - Expr_Value (Lo) + 1;
6730 if Dim > Uint_0 then
6731 Num_Elmts := Num_Elmts * Dim;
6732 else
6733 Num_Elmts := Uint_0;
6734 end if;
6736 Next_Index (Indx);
6737 end loop;
6739 -- What we are looking for here is the situation where
6740 -- the RM_Size given would be exactly right if there was
6741 -- a pragma Pack, resulting in the component size being
6742 -- the RM_Size of the component type.
6744 if RM_Size (E) = Num_Elmts * Rsiz then
6746 -- For implicit packing mode, just set the component
6747 -- size and Freeze_Array_Type will do the rest.
6749 if Implicit_Packing then
6750 Set_Component_Size (Btyp, Rsiz);
6752 -- Otherwise give an error message, except that if the
6753 -- specified Size is zero, there is no need for pragma
6754 -- Pack. Note that size zero is not considered
6755 -- Addressable.
6757 elsif RM_Size (E) /= Uint_0 then
6758 Error_Msg_NE
6759 ("size given for& too small", SZ, E);
6760 Error_Msg_N -- CODEFIX
6761 ("\use explicit pragma Pack or use pragma "
6762 & "Implicit_Packing", SZ);
6763 end if;
6764 end if;
6765 end if;
6766 end;
6767 end if;
6769 <<No_Implicit_Packing>>
6771 -- If ancestor subtype present, freeze that first. Note that this
6772 -- will also get the base type frozen. Need RM reference ???
6774 Atype := Ancestor_Subtype (E);
6776 if Present (Atype) then
6777 Freeze_And_Append (Atype, N, Result);
6779 -- No ancestor subtype present
6781 else
6782 -- See if we have a nearest ancestor that has a predicate.
6783 -- That catches the case of derived type with a predicate.
6784 -- Need RM reference here ???
6786 Atype := Nearest_Ancestor (E);
6788 if Present (Atype) and then Has_Predicates (Atype) then
6789 Freeze_And_Append (Atype, N, Result);
6790 end if;
6792 -- Freeze base type before freezing the entity (RM 13.14(15))
6794 if E /= Base_Type (E) then
6795 Freeze_And_Append (Base_Type (E), N, Result);
6796 end if;
6797 end if;
6799 -- A subtype inherits all the type-related representation aspects
6800 -- from its parents (RM 13.1(8)).
6802 Inherit_Aspects_At_Freeze_Point (E);
6804 -- For a derived type, freeze its parent type first (RM 13.14(15))
6806 elsif Is_Derived_Type (E) then
6807 Freeze_And_Append (Etype (E), N, Result);
6808 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
6810 -- A derived type inherits each type-related representation aspect
6811 -- of its parent type that was directly specified before the
6812 -- declaration of the derived type (RM 13.1(15)).
6814 Inherit_Aspects_At_Freeze_Point (E);
6815 end if;
6817 -- Case of array type
6819 if Is_Array_Type (E) then
6820 Freeze_Array_Type (E);
6821 end if;
6823 -- Check for incompatible size and alignment for array/record type
6825 if Warn_On_Size_Alignment
6826 and then (Is_Array_Type (E) or else Is_Record_Type (E))
6827 and then Has_Size_Clause (E)
6828 and then Has_Alignment_Clause (E)
6830 -- If explicit Object_Size clause given assume that the programmer
6831 -- knows what he is doing, and expects the compiler behavior.
6833 and then not Has_Object_Size_Clause (E)
6835 -- It does not really make sense to warn for the minimum alignment
6836 -- since the programmer could not get rid of the warning.
6838 and then Alignment (E) > 1
6840 -- Check for size not a multiple of alignment
6842 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
6843 then
6844 declare
6845 SC : constant Node_Id := Size_Clause (E);
6846 AC : constant Node_Id := Alignment_Clause (E);
6847 Loc : Node_Id;
6848 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
6850 begin
6851 if Present (SC) and then Present (AC) then
6853 -- Give a warning
6855 if Sloc (SC) > Sloc (AC) then
6856 Loc := SC;
6857 Error_Msg_NE
6858 ("?.z?size is not a multiple of alignment for &",
6859 Loc, E);
6860 Error_Msg_Sloc := Sloc (AC);
6861 Error_Msg_Uint_1 := Alignment (E);
6862 Error_Msg_N ("\?.z?alignment of ^ specified #", Loc);
6864 else
6865 Loc := AC;
6866 Error_Msg_NE
6867 ("?.z?size is not a multiple of alignment for &",
6868 Loc, E);
6869 Error_Msg_Sloc := Sloc (SC);
6870 Error_Msg_Uint_1 := RM_Size (E);
6871 Error_Msg_N ("\?.z?size of ^ specified #", Loc);
6872 end if;
6874 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
6875 Error_Msg_N ("\?.z?Object_Size will be increased to ^", Loc);
6876 end if;
6877 end;
6878 end if;
6880 -- For a class-wide type, the corresponding specific type is
6881 -- frozen as well (RM 13.14(15))
6883 if Is_Class_Wide_Type (E) then
6884 Freeze_And_Append (Root_Type (E), N, Result);
6886 -- If the base type of the class-wide type is still incomplete,
6887 -- the class-wide remains unfrozen as well. This is legal when
6888 -- E is the formal of a primitive operation of some other type
6889 -- which is being frozen.
6891 if not Is_Frozen (Root_Type (E)) then
6892 Set_Is_Frozen (E, False);
6893 goto Leave;
6894 end if;
6896 -- The equivalent type associated with a class-wide subtype needs
6897 -- to be frozen to ensure that its layout is done.
6899 if Ekind (E) = E_Class_Wide_Subtype
6900 and then Present (Equivalent_Type (E))
6901 then
6902 Freeze_And_Append (Equivalent_Type (E), N, Result);
6903 end if;
6905 -- Generate an itype reference for a library-level class-wide type
6906 -- at the freeze point. Otherwise the first explicit reference to
6907 -- the type may appear in an inner scope which will be rejected by
6908 -- the back-end.
6910 if Is_Itype (E)
6911 and then Is_Compilation_Unit (Scope (E))
6912 then
6913 declare
6914 Ref : constant Node_Id := Make_Itype_Reference (Loc);
6916 begin
6917 Set_Itype (Ref, E);
6919 -- From a gigi point of view, a class-wide subtype derives
6920 -- from its record equivalent type. As a result, the itype
6921 -- reference must appear after the freeze node of the
6922 -- equivalent type or gigi will reject the reference.
6924 if Ekind (E) = E_Class_Wide_Subtype
6925 and then Present (Equivalent_Type (E))
6926 then
6927 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
6928 else
6929 Add_To_Result (Ref);
6930 end if;
6931 end;
6932 end if;
6934 -- For a record type or record subtype, freeze all component types
6935 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
6936 -- using Is_Record_Type, because we don't want to attempt the freeze
6937 -- for the case of a private type with record extension (we will do
6938 -- that later when the full type is frozen).
6940 elsif Ekind (E) in E_Record_Type | E_Record_Subtype then
6941 if not In_Generic_Scope (E) then
6942 Freeze_Record_Type (E);
6943 end if;
6945 -- Report a warning if a discriminated record base type has a
6946 -- convention with language C or C++ applied to it. This check is
6947 -- done even within generic scopes (but not in instantiations),
6948 -- which is why we don't do it as part of Freeze_Record_Type.
6950 Check_Suspicious_Convention (E);
6952 -- For a concurrent type, freeze corresponding record type. This does
6953 -- not correspond to any specific rule in the RM, but the record type
6954 -- is essentially part of the concurrent type. Also freeze all local
6955 -- entities. This includes record types created for entry parameter
6956 -- blocks and whatever local entities may appear in the private part.
6958 elsif Is_Concurrent_Type (E) then
6959 if Present (Corresponding_Record_Type (E)) then
6960 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
6961 end if;
6963 Comp := First_Entity (E);
6964 while Present (Comp) loop
6965 if Is_Type (Comp) then
6966 Freeze_And_Append (Comp, N, Result);
6968 elsif (Ekind (Comp)) /= E_Function then
6970 -- The guard on the presence of the Etype seems to be needed
6971 -- for some CodePeer (-gnatcC) cases, but not clear why???
6973 if Present (Etype (Comp)) then
6974 if Is_Itype (Etype (Comp))
6975 and then Underlying_Type (Scope (Etype (Comp))) = E
6976 then
6977 Undelay_Type (Etype (Comp));
6978 end if;
6980 Freeze_And_Append (Etype (Comp), N, Result);
6981 end if;
6982 end if;
6984 Next_Entity (Comp);
6985 end loop;
6987 -- Private types are required to point to the same freeze node as
6988 -- their corresponding full views. The freeze node itself has to
6989 -- point to the partial view of the entity (because from the partial
6990 -- view, we can retrieve the full view, but not the reverse).
6991 -- However, in order to freeze correctly, we need to freeze the full
6992 -- view. If we are freezing at the end of a scope (or within the
6993 -- scope) of the private type, the partial and full views will have
6994 -- been swapped, the full view appears first in the entity chain and
6995 -- the swapping mechanism ensures that the pointers are properly set
6996 -- (on scope exit).
6998 -- If we encounter the partial view before the full view (e.g. when
6999 -- freezing from another scope), we freeze the full view, and then
7000 -- set the pointers appropriately since we cannot rely on swapping to
7001 -- fix things up (subtypes in an outer scope might not get swapped).
7003 -- If the full view is itself private, the above requirements apply
7004 -- to the underlying full view instead of the full view. But there is
7005 -- no swapping mechanism for the underlying full view so we need to
7006 -- set the pointers appropriately in both cases.
7008 elsif Is_Incomplete_Or_Private_Type (E)
7009 and then not Is_Generic_Type (E)
7010 then
7011 -- The construction of the dispatch table associated with library
7012 -- level tagged types forces freezing of all the primitives of the
7013 -- type, which may cause premature freezing of the partial view.
7014 -- For example:
7016 -- package Pkg is
7017 -- type T is tagged private;
7018 -- type DT is new T with private;
7019 -- procedure Prim (X : in out T; Y : in out DT'Class);
7020 -- private
7021 -- type T is tagged null record;
7022 -- Obj : T;
7023 -- type DT is new T with null record;
7024 -- end;
7026 -- In this case the type will be frozen later by the usual
7027 -- mechanism: an object declaration, an instantiation, or the
7028 -- end of a declarative part.
7030 if Is_Library_Level_Tagged_Type (E)
7031 and then not Present (Full_View (E))
7032 then
7033 Set_Is_Frozen (E, False);
7034 goto Leave;
7036 -- Case of full view present
7038 elsif Present (Full_View (E)) then
7040 -- If full view has already been frozen, then no further
7041 -- processing is required
7043 if Is_Frozen (Full_View (E)) then
7044 Set_Has_Delayed_Freeze (E, False);
7045 Set_Freeze_Node (E, Empty);
7047 -- Otherwise freeze full view and patch the pointers so that
7048 -- the freeze node will elaborate both views in the back end.
7049 -- However, if full view is itself private, freeze underlying
7050 -- full view instead and patch the pointers so that the freeze
7051 -- node will elaborate the three views in the back end.
7053 else
7054 declare
7055 Full : Entity_Id := Full_View (E);
7057 begin
7058 if Is_Private_Type (Full)
7059 and then Present (Underlying_Full_View (Full))
7060 then
7061 Full := Underlying_Full_View (Full);
7062 end if;
7064 Freeze_And_Append (Full, N, Result);
7066 if Full /= Full_View (E)
7067 and then Has_Delayed_Freeze (Full_View (E))
7068 then
7069 F_Node := Freeze_Node (Full);
7071 if Present (F_Node) then
7072 Inherit_Freeze_Node
7073 (Fnod => F_Node, Typ => Full_View (E));
7074 else
7075 Set_Has_Delayed_Freeze (Full_View (E), False);
7076 Set_Freeze_Node (Full_View (E), Empty);
7077 end if;
7078 end if;
7080 if Has_Delayed_Freeze (E) then
7081 F_Node := Freeze_Node (Full_View (E));
7083 if Present (F_Node) then
7084 Inherit_Freeze_Node (Fnod => F_Node, Typ => E);
7085 else
7086 -- {Incomplete,Private}_Subtypes with Full_Views
7087 -- constrained by discriminants.
7089 Set_Has_Delayed_Freeze (E, False);
7090 Set_Freeze_Node (E, Empty);
7091 end if;
7092 end if;
7093 end;
7094 end if;
7096 Check_Debug_Info_Needed (E);
7098 -- AI-117 requires that the convention of a partial view be the
7099 -- same as the convention of the full view. Note that this is a
7100 -- recognized breach of privacy, but it's essential for logical
7101 -- consistency of representation, and the lack of a rule in
7102 -- RM95 was an oversight.
7104 Set_Convention (E, Convention (Full_View (E)));
7106 Set_Size_Known_At_Compile_Time (E,
7107 Size_Known_At_Compile_Time (Full_View (E)));
7109 -- Size information is copied from the full view to the
7110 -- incomplete or private view for consistency.
7112 -- We skip this is the full view is not a type. This is very
7113 -- strange of course, and can only happen as a result of
7114 -- certain illegalities, such as a premature attempt to derive
7115 -- from an incomplete type.
7117 if Is_Type (Full_View (E)) then
7118 Set_Size_Info (E, Full_View (E));
7119 Copy_RM_Size (To => E, From => Full_View (E));
7120 end if;
7122 goto Leave;
7124 -- Case of underlying full view present
7126 elsif Is_Private_Type (E)
7127 and then Present (Underlying_Full_View (E))
7128 then
7129 if not Is_Frozen (Underlying_Full_View (E)) then
7130 Freeze_And_Append (Underlying_Full_View (E), N, Result);
7131 end if;
7133 -- Patch the pointers so that the freeze node will elaborate
7134 -- both views in the back end.
7136 if Has_Delayed_Freeze (E) then
7137 F_Node := Freeze_Node (Underlying_Full_View (E));
7139 if Present (F_Node) then
7140 Inherit_Freeze_Node
7141 (Fnod => F_Node,
7142 Typ => E);
7143 else
7144 Set_Has_Delayed_Freeze (E, False);
7145 Set_Freeze_Node (E, Empty);
7146 end if;
7147 end if;
7149 Check_Debug_Info_Needed (E);
7151 goto Leave;
7153 -- Case of no full view present. If entity is subtype or derived,
7154 -- it is safe to freeze, correctness depends on the frozen status
7155 -- of parent. Otherwise it is either premature usage, or a Taft
7156 -- amendment type, so diagnosis is at the point of use and the
7157 -- type might be frozen later.
7159 elsif E /= Base_Type (E) then
7160 declare
7161 Btyp : constant Entity_Id := Base_Type (E);
7163 begin
7164 -- However, if the base type is itself private and has no
7165 -- (underlying) full view either, wait until the full type
7166 -- declaration is seen and all the full views are created.
7168 if Is_Private_Type (Btyp)
7169 and then No (Full_View (Btyp))
7170 and then No (Underlying_Full_View (Btyp))
7171 and then Has_Delayed_Freeze (Btyp)
7172 and then No (Freeze_Node (Btyp))
7173 then
7174 Set_Is_Frozen (E, False);
7175 Result := No_List;
7176 goto Leave;
7177 end if;
7178 end;
7180 elsif Is_Derived_Type (E) then
7181 null;
7183 else
7184 Set_Is_Frozen (E, False);
7185 Result := No_List;
7186 goto Leave;
7187 end if;
7189 -- For access subprogram, freeze types of all formals, the return
7190 -- type was already frozen, since it is the Etype of the function.
7191 -- Formal types can be tagged Taft amendment types, but otherwise
7192 -- they cannot be incomplete.
7194 elsif Ekind (E) = E_Subprogram_Type then
7195 Formal := First_Formal (E);
7196 while Present (Formal) loop
7197 if Ekind (Etype (Formal)) = E_Incomplete_Type
7198 and then No (Full_View (Etype (Formal)))
7199 then
7200 if Is_Tagged_Type (Etype (Formal)) then
7201 null;
7203 -- AI05-151: Incomplete types are allowed in access to
7204 -- subprogram specifications.
7206 elsif Ada_Version < Ada_2012 then
7207 Error_Msg_NE
7208 ("invalid use of incomplete type&", E, Etype (Formal));
7209 end if;
7210 end if;
7212 Freeze_And_Append (Etype (Formal), N, Result);
7213 Next_Formal (Formal);
7214 end loop;
7216 Freeze_Subprogram (E);
7218 -- For access to a protected subprogram, freeze the equivalent type
7219 -- (however this is not set if we are not generating code or if this
7220 -- is an anonymous type used just for resolution).
7222 elsif Is_Access_Protected_Subprogram_Type (E) then
7223 if Present (Equivalent_Type (E)) then
7224 Freeze_And_Append (Equivalent_Type (E), N, Result);
7225 end if;
7226 end if;
7228 -- Generic types are never seen by the back-end, and are also not
7229 -- processed by the expander (since the expander is turned off for
7230 -- generic processing), so we never need freeze nodes for them.
7232 if Is_Generic_Type (E) then
7233 goto Leave;
7234 end if;
7236 -- Some special processing for non-generic types to complete
7237 -- representation details not known till the freeze point.
7239 if Is_Fixed_Point_Type (E) then
7240 Freeze_Fixed_Point_Type (E);
7242 elsif Is_Enumeration_Type (E) then
7243 Freeze_Enumeration_Type (E);
7245 elsif Is_Integer_Type (E) then
7246 Adjust_Esize_For_Alignment (E);
7248 if Is_Modular_Integer_Type (E)
7249 and then Warn_On_Suspicious_Modulus_Value
7250 then
7251 Check_Suspicious_Modulus (E);
7252 end if;
7254 -- The pool applies to named and anonymous access types, but not
7255 -- to subprogram and to internal types generated for 'Access
7256 -- references.
7258 elsif Is_Access_Object_Type (E)
7259 and then Ekind (E) /= E_Access_Attribute_Type
7260 then
7261 -- If a pragma Default_Storage_Pool applies, and this type has no
7262 -- Storage_Pool or Storage_Size clause (which must have occurred
7263 -- before the freezing point), then use the default. This applies
7264 -- only to base types.
7266 -- None of this applies to access to subprograms, for which there
7267 -- are clearly no pools.
7269 if Present (Default_Pool)
7270 and then Is_Base_Type (E)
7271 and then not Has_Storage_Size_Clause (E)
7272 and then No (Associated_Storage_Pool (E))
7273 then
7274 -- Case of pragma Default_Storage_Pool (null)
7276 if Nkind (Default_Pool) = N_Null then
7277 Set_No_Pool_Assigned (E);
7279 -- Case of pragma Default_Storage_Pool (Standard)
7281 elsif Entity (Default_Pool) = Standard_Standard then
7282 Set_Associated_Storage_Pool (E, RTE (RE_Global_Pool_Object));
7284 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
7286 else
7287 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
7288 end if;
7289 end if;
7291 -- Check restriction for standard storage pool
7293 if No (Associated_Storage_Pool (E)) then
7294 Check_Restriction (No_Standard_Storage_Pools, E);
7295 end if;
7297 -- Deal with error message for pure access type. This is not an
7298 -- error in Ada 2005 if there is no pool (see AI-366).
7300 if Is_Pure_Unit_Access_Type (E)
7301 and then (Ada_Version < Ada_2005
7302 or else not No_Pool_Assigned (E))
7303 and then not Is_Generic_Unit (Scope (E))
7304 then
7305 Error_Msg_N ("named access type not allowed in pure unit", E);
7307 if Ada_Version >= Ada_2005 then
7308 Error_Msg_N
7309 ("\would be legal if Storage_Size of 0 given??", E);
7311 elsif No_Pool_Assigned (E) then
7312 Error_Msg_N
7313 ("\would be legal in Ada 2005??", E);
7315 else
7316 Error_Msg_N
7317 ("\would be legal in Ada 2005 if "
7318 & "Storage_Size of 0 given??", E);
7319 end if;
7320 end if;
7321 end if;
7323 -- Case of composite types
7325 if Is_Composite_Type (E) then
7327 -- AI-117 requires that all new primitives of a tagged type must
7328 -- inherit the convention of the full view of the type. Inherited
7329 -- and overriding operations are defined to inherit the convention
7330 -- of their parent or overridden subprogram (also specified in
7331 -- AI-117), which will have occurred earlier (in Derive_Subprogram
7332 -- and New_Overloaded_Entity). Here we set the convention of
7333 -- primitives that are still convention Ada, which will ensure
7334 -- that any new primitives inherit the type's convention. Class-
7335 -- wide types can have a foreign convention inherited from their
7336 -- specific type, but are excluded from this since they don't have
7337 -- any associated primitives.
7339 if Is_Tagged_Type (E)
7340 and then not Is_Class_Wide_Type (E)
7341 and then Convention (E) /= Convention_Ada
7342 then
7343 declare
7344 Prim_List : constant Elist_Id := Primitive_Operations (E);
7345 Prim : Elmt_Id;
7347 begin
7348 Prim := First_Elmt (Prim_List);
7349 while Present (Prim) loop
7350 if Convention (Node (Prim)) = Convention_Ada then
7351 Set_Convention (Node (Prim), Convention (E));
7352 end if;
7354 Next_Elmt (Prim);
7355 end loop;
7356 end;
7357 end if;
7359 -- If the type is a simple storage pool type, then this is where
7360 -- we attempt to locate and validate its Allocate, Deallocate, and
7361 -- Storage_Size operations (the first is required, and the latter
7362 -- two are optional). We also verify that the full type for a
7363 -- private type is allowed to be a simple storage pool type.
7365 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
7366 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
7367 then
7368 -- If the type is marked Has_Private_Declaration, then this is
7369 -- a full type for a private type that was specified with the
7370 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
7371 -- pragma is allowed for the full type (for example, it can't
7372 -- be an array type, or a nonlimited record type).
7374 if Has_Private_Declaration (E) then
7375 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
7376 and then not Is_Private_Type (E)
7377 then
7378 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
7379 Error_Msg_N
7380 ("pragma% can only apply to full type that is an " &
7381 "explicitly limited type", E);
7382 end if;
7383 end if;
7385 Validate_Simple_Pool_Ops : declare
7386 Pool_Type : Entity_Id renames E;
7387 Address_Type : constant Entity_Id := RTE (RE_Address);
7388 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
7390 procedure Validate_Simple_Pool_Op_Formal
7391 (Pool_Op : Entity_Id;
7392 Pool_Op_Formal : in out Entity_Id;
7393 Expected_Mode : Formal_Kind;
7394 Expected_Type : Entity_Id;
7395 Formal_Name : String;
7396 OK_Formal : in out Boolean);
7397 -- Validate one formal Pool_Op_Formal of the candidate pool
7398 -- operation Pool_Op. The formal must be of Expected_Type
7399 -- and have mode Expected_Mode. OK_Formal will be set to
7400 -- False if the formal doesn't match. If OK_Formal is False
7401 -- on entry, then the formal will effectively be ignored
7402 -- (because validation of the pool op has already failed).
7403 -- Upon return, Pool_Op_Formal will be updated to the next
7404 -- formal, if any.
7406 procedure Validate_Simple_Pool_Operation
7407 (Op_Name : Name_Id);
7408 -- Search for and validate a simple pool operation with the
7409 -- name Op_Name. If the name is Allocate, then there must be
7410 -- exactly one such primitive operation for the simple pool
7411 -- type. If the name is Deallocate or Storage_Size, then
7412 -- there can be at most one such primitive operation. The
7413 -- profile of the located primitive must conform to what
7414 -- is expected for each operation.
7416 ------------------------------------
7417 -- Validate_Simple_Pool_Op_Formal --
7418 ------------------------------------
7420 procedure Validate_Simple_Pool_Op_Formal
7421 (Pool_Op : Entity_Id;
7422 Pool_Op_Formal : in out Entity_Id;
7423 Expected_Mode : Formal_Kind;
7424 Expected_Type : Entity_Id;
7425 Formal_Name : String;
7426 OK_Formal : in out Boolean)
7428 begin
7429 -- If OK_Formal is False on entry, then simply ignore
7430 -- the formal, because an earlier formal has already
7431 -- been flagged.
7433 if not OK_Formal then
7434 return;
7436 -- If no formal is passed in, then issue an error for a
7437 -- missing formal.
7439 elsif not Present (Pool_Op_Formal) then
7440 Error_Msg_NE
7441 ("simple storage pool op missing formal " &
7442 Formal_Name & " of type&", Pool_Op, Expected_Type);
7443 OK_Formal := False;
7445 return;
7446 end if;
7448 if Etype (Pool_Op_Formal) /= Expected_Type then
7450 -- If the pool type was expected for this formal, then
7451 -- this will not be considered a candidate operation
7452 -- for the simple pool, so we unset OK_Formal so that
7453 -- the op and any later formals will be ignored.
7455 if Expected_Type = Pool_Type then
7456 OK_Formal := False;
7458 return;
7460 else
7461 Error_Msg_NE
7462 ("wrong type for formal " & Formal_Name &
7463 " of simple storage pool op; expected type&",
7464 Pool_Op_Formal, Expected_Type);
7465 end if;
7466 end if;
7468 -- Issue error if formal's mode is not the expected one
7470 if Ekind (Pool_Op_Formal) /= Expected_Mode then
7471 Error_Msg_N
7472 ("wrong mode for formal of simple storage pool op",
7473 Pool_Op_Formal);
7474 end if;
7476 -- Advance to the next formal
7478 Next_Formal (Pool_Op_Formal);
7479 end Validate_Simple_Pool_Op_Formal;
7481 ------------------------------------
7482 -- Validate_Simple_Pool_Operation --
7483 ------------------------------------
7485 procedure Validate_Simple_Pool_Operation
7486 (Op_Name : Name_Id)
7488 Op : Entity_Id;
7489 Found_Op : Entity_Id := Empty;
7490 Formal : Entity_Id;
7491 Is_OK : Boolean;
7493 begin
7494 pragma Assert
7495 (Op_Name in Name_Allocate
7496 | Name_Deallocate
7497 | Name_Storage_Size);
7499 Error_Msg_Name_1 := Op_Name;
7501 -- For each homonym declared immediately in the scope
7502 -- of the simple storage pool type, determine whether
7503 -- the homonym is an operation of the pool type, and,
7504 -- if so, check that its profile is as expected for
7505 -- a simple pool operation of that name.
7507 Op := Get_Name_Entity_Id (Op_Name);
7508 while Present (Op) loop
7509 if Ekind (Op) in E_Function | E_Procedure
7510 and then Scope (Op) = Current_Scope
7511 then
7512 Formal := First_Entity (Op);
7514 Is_OK := True;
7516 -- The first parameter must be of the pool type
7517 -- in order for the operation to qualify.
7519 if Op_Name = Name_Storage_Size then
7520 Validate_Simple_Pool_Op_Formal
7521 (Op, Formal, E_In_Parameter, Pool_Type,
7522 "Pool", Is_OK);
7523 else
7524 Validate_Simple_Pool_Op_Formal
7525 (Op, Formal, E_In_Out_Parameter, Pool_Type,
7526 "Pool", Is_OK);
7527 end if;
7529 -- If another operation with this name has already
7530 -- been located for the type, then flag an error,
7531 -- since we only allow the type to have a single
7532 -- such primitive.
7534 if Present (Found_Op) and then Is_OK then
7535 Error_Msg_NE
7536 ("only one % operation allowed for " &
7537 "simple storage pool type&", Op, Pool_Type);
7538 end if;
7540 -- In the case of Allocate and Deallocate, a formal
7541 -- of type System.Address is required.
7543 if Op_Name = Name_Allocate then
7544 Validate_Simple_Pool_Op_Formal
7545 (Op, Formal, E_Out_Parameter,
7546 Address_Type, "Storage_Address", Is_OK);
7548 elsif Op_Name = Name_Deallocate then
7549 Validate_Simple_Pool_Op_Formal
7550 (Op, Formal, E_In_Parameter,
7551 Address_Type, "Storage_Address", Is_OK);
7552 end if;
7554 -- In the case of Allocate and Deallocate, formals
7555 -- of type Storage_Count are required as the third
7556 -- and fourth parameters.
7558 if Op_Name /= Name_Storage_Size then
7559 Validate_Simple_Pool_Op_Formal
7560 (Op, Formal, E_In_Parameter,
7561 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
7562 Validate_Simple_Pool_Op_Formal
7563 (Op, Formal, E_In_Parameter,
7564 Stg_Cnt_Type, "Alignment", Is_OK);
7565 end if;
7567 -- If no mismatched formals have been found (Is_OK)
7568 -- and no excess formals are present, then this
7569 -- operation has been validated, so record it.
7571 if not Present (Formal) and then Is_OK then
7572 Found_Op := Op;
7573 end if;
7574 end if;
7576 Op := Homonym (Op);
7577 end loop;
7579 -- There must be a valid Allocate operation for the type,
7580 -- so issue an error if none was found.
7582 if Op_Name = Name_Allocate
7583 and then not Present (Found_Op)
7584 then
7585 Error_Msg_N ("missing % operation for simple " &
7586 "storage pool type", Pool_Type);
7588 elsif Present (Found_Op) then
7590 -- Simple pool operations can't be abstract
7592 if Is_Abstract_Subprogram (Found_Op) then
7593 Error_Msg_N
7594 ("simple storage pool operation must not be " &
7595 "abstract", Found_Op);
7596 end if;
7598 -- The Storage_Size operation must be a function with
7599 -- Storage_Count as its result type.
7601 if Op_Name = Name_Storage_Size then
7602 if Ekind (Found_Op) = E_Procedure then
7603 Error_Msg_N
7604 ("% operation must be a function", Found_Op);
7606 elsif Etype (Found_Op) /= Stg_Cnt_Type then
7607 Error_Msg_NE
7608 ("wrong result type for%, expected type&",
7609 Found_Op, Stg_Cnt_Type);
7610 end if;
7612 -- Allocate and Deallocate must be procedures
7614 elsif Ekind (Found_Op) = E_Function then
7615 Error_Msg_N
7616 ("% operation must be a procedure", Found_Op);
7617 end if;
7618 end if;
7619 end Validate_Simple_Pool_Operation;
7621 -- Start of processing for Validate_Simple_Pool_Ops
7623 begin
7624 Validate_Simple_Pool_Operation (Name_Allocate);
7625 Validate_Simple_Pool_Operation (Name_Deallocate);
7626 Validate_Simple_Pool_Operation (Name_Storage_Size);
7627 end Validate_Simple_Pool_Ops;
7628 end if;
7629 end if;
7631 -- Now that all types from which E may depend are frozen, see if
7632 -- strict alignment is required, a component clause on a record
7633 -- is correct, the size is known at compile time and if it must
7634 -- be unsigned, in that order.
7636 if Base_Type (E) = E then
7637 Check_Strict_Alignment (E);
7638 end if;
7640 if Ekind (E) in E_Record_Type | E_Record_Subtype then
7641 declare
7642 RC : constant Node_Id := Get_Record_Representation_Clause (E);
7643 begin
7644 if Present (RC) then
7645 Check_Record_Representation_Clause (RC);
7646 end if;
7647 end;
7648 end if;
7650 Check_Compile_Time_Size (E);
7652 Check_Unsigned_Type (E);
7654 -- Do not allow a size clause for a type which does not have a size
7655 -- that is known at compile time
7657 if (Has_Size_Clause (E) or else Has_Object_Size_Clause (E))
7658 and then not Size_Known_At_Compile_Time (E)
7659 then
7660 -- Suppress this message if errors posted on E, even if we are
7661 -- in all errors mode, since this is often a junk message
7663 if not Error_Posted (E) then
7664 Error_Msg_N
7665 ("size clause not allowed for variable length type",
7666 Size_Clause (E));
7667 end if;
7668 end if;
7670 -- Now we set/verify the representation information, in particular
7671 -- the size and alignment values. This processing is not required for
7672 -- generic types, since generic types do not play any part in code
7673 -- generation, and so the size and alignment values for such types
7674 -- are irrelevant. Ditto for types declared within a generic unit,
7675 -- which may have components that depend on generic parameters, and
7676 -- that will be recreated in an instance.
7678 if Inside_A_Generic then
7679 null;
7681 -- Otherwise we call the layout procedure
7683 else
7684 Layout_Type (E);
7685 end if;
7687 -- If this is an access to subprogram whose designated type is itself
7688 -- a subprogram type, the return type of this anonymous subprogram
7689 -- type must be decorated as well.
7691 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
7692 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
7693 then
7694 Layout_Type (Etype (Designated_Type (E)));
7695 end if;
7697 -- If the type has a Defaut_Value/Default_Component_Value aspect,
7698 -- this is where we analyze the expression (after the type is frozen,
7699 -- since in the case of Default_Value, we are analyzing with the
7700 -- type itself, and we treat Default_Component_Value similarly for
7701 -- the sake of uniformity).
7703 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
7704 declare
7705 Nam : Name_Id;
7706 Exp : Node_Id;
7707 Typ : Entity_Id;
7709 begin
7710 if Is_Scalar_Type (E) then
7711 Nam := Name_Default_Value;
7712 Typ := E;
7713 Exp := Default_Aspect_Value (Typ);
7714 else
7715 Nam := Name_Default_Component_Value;
7716 Typ := Component_Type (E);
7717 Exp := Default_Aspect_Component_Value (E);
7718 end if;
7720 Analyze_And_Resolve (Exp, Typ);
7722 if Etype (Exp) /= Any_Type then
7723 if not Is_OK_Static_Expression (Exp) then
7724 Error_Msg_Name_1 := Nam;
7725 Flag_Non_Static_Expr
7726 ("aspect% requires static expression", Exp);
7727 end if;
7728 end if;
7729 end;
7730 end if;
7732 -- Verify at this point that No_Controlled_Parts and No_Task_Parts,
7733 -- when specified on the current type or one of its ancestors, has
7734 -- not been overridden and that no violation of the aspect has
7735 -- occurred.
7737 -- It is important that we perform the checks here after the type has
7738 -- been processed because if said type depended on a private type it
7739 -- will not have been marked controlled or having tasks.
7741 Check_No_Parts_Violations (E, Aspect_No_Controlled_Parts);
7742 Check_No_Parts_Violations (E, Aspect_No_Task_Parts);
7744 -- End of freeze processing for type entities
7745 end if;
7747 -- Here is where we logically freeze the current entity. If it has a
7748 -- freeze node, then this is the point at which the freeze node is
7749 -- linked into the result list.
7751 if Has_Delayed_Freeze (E) then
7753 -- If a freeze node is already allocated, use it, otherwise allocate
7754 -- a new one. The preallocation happens in the case of anonymous base
7755 -- types, where we preallocate so that we can set First_Subtype_Link.
7756 -- Note that we reset the Sloc to the current freeze location.
7758 if Present (Freeze_Node (E)) then
7759 F_Node := Freeze_Node (E);
7760 Set_Sloc (F_Node, Loc);
7762 else
7763 F_Node := New_Node (N_Freeze_Entity, Loc);
7764 Set_Freeze_Node (E, F_Node);
7765 Set_Access_Types_To_Process (F_Node, No_Elist);
7766 Set_TSS_Elist (F_Node, No_Elist);
7767 Set_Actions (F_Node, No_List);
7768 end if;
7770 Set_Entity (F_Node, E);
7771 Add_To_Result (F_Node);
7773 -- A final pass over record types with discriminants. If the type
7774 -- has an incomplete declaration, there may be constrained access
7775 -- subtypes declared elsewhere, which do not depend on the discrimi-
7776 -- nants of the type, and which are used as component types (i.e.
7777 -- the full view is a recursive type). The designated types of these
7778 -- subtypes can only be elaborated after the type itself, and they
7779 -- need an itype reference.
7781 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
7782 declare
7783 Comp : Entity_Id;
7784 IR : Node_Id;
7785 Typ : Entity_Id;
7787 begin
7788 Comp := First_Component (E);
7789 while Present (Comp) loop
7790 Typ := Etype (Comp);
7792 if Is_Access_Type (Typ)
7793 and then Scope (Typ) /= E
7794 and then Base_Type (Designated_Type (Typ)) = E
7795 and then Is_Itype (Designated_Type (Typ))
7796 then
7797 IR := Make_Itype_Reference (Sloc (Comp));
7798 Set_Itype (IR, Designated_Type (Typ));
7799 Append (IR, Result);
7800 end if;
7802 Next_Component (Comp);
7803 end loop;
7804 end;
7805 end if;
7806 end if;
7808 -- When a type is frozen, the first subtype of the type is frozen as
7809 -- well (RM 13.14(15)). This has to be done after freezing the type,
7810 -- since obviously the first subtype depends on its own base type.
7812 if Is_Type (E) then
7813 Freeze_And_Append (First_Subtype (E), N, Result);
7815 -- If we just froze a tagged non-class-wide record, then freeze the
7816 -- corresponding class-wide type. This must be done after the tagged
7817 -- type itself is frozen, because the class-wide type refers to the
7818 -- tagged type which generates the class.
7820 -- For a tagged type, freeze explicitly those primitive operations
7821 -- that are expression functions, which otherwise have no clear
7822 -- freeze point: these have to be frozen before the dispatch table
7823 -- for the type is built, and before any explicit call to the
7824 -- primitive, which would otherwise be the freeze point for it.
7826 if Is_Tagged_Type (E)
7827 and then not Is_Class_Wide_Type (E)
7828 and then Present (Class_Wide_Type (E))
7829 then
7830 Freeze_And_Append (Class_Wide_Type (E), N, Result);
7832 declare
7833 Ops : constant Elist_Id := Primitive_Operations (E);
7835 Elmt : Elmt_Id;
7836 Subp : Entity_Id;
7838 begin
7839 if Ops /= No_Elist then
7840 Elmt := First_Elmt (Ops);
7841 while Present (Elmt) loop
7842 Subp := Node (Elmt);
7843 if Is_Expression_Function (Subp) then
7844 Freeze_And_Append (Subp, N, Result);
7845 end if;
7847 Next_Elmt (Elmt);
7848 end loop;
7849 end if;
7850 end;
7851 end if;
7852 end if;
7854 Check_Debug_Info_Needed (E);
7856 -- If subprogram has address clause then reset Is_Public flag, since we
7857 -- do not want the backend to generate external references.
7859 if Is_Subprogram (E)
7860 and then Present (Address_Clause (E))
7861 and then not Is_Library_Level_Entity (E)
7862 then
7863 Set_Is_Public (E, False);
7864 end if;
7866 -- The Ghost mode of the enclosing context is ignored, while the
7867 -- entity being frozen is living. Insert the freezing action prior
7868 -- to the start of the enclosing ignored Ghost region. As a result
7869 -- the freezeing action will be preserved when the ignored Ghost
7870 -- context is eliminated. The insertion must take place even when
7871 -- the context is a spec expression, otherwise "Handling of Default
7872 -- and Per-Object Expressions" will suppress the insertion, and the
7873 -- freeze node will be dropped on the floor.
7875 if Saved_GM = Ignore
7876 and then Ghost_Mode /= Ignore
7877 and then Present (Ignored_Ghost_Region)
7878 then
7879 Insert_Actions
7880 (Assoc_Node => Ignored_Ghost_Region,
7881 Ins_Actions => Result,
7882 Spec_Expr_OK => True);
7884 Result := No_List;
7885 end if;
7887 <<Leave>>
7888 Restore_Ghost_Region (Saved_GM, Saved_IGR);
7890 return Result;
7891 end Freeze_Entity;
7893 -----------------------------
7894 -- Freeze_Enumeration_Type --
7895 -----------------------------
7897 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
7898 begin
7899 -- By default, if no size clause is present, an enumeration type with
7900 -- Convention C is assumed to interface to a C enum and has integer
7901 -- size, except for a boolean type because it is assumed to interface
7902 -- to _Bool introduced in C99. This applies to types. For subtypes,
7903 -- verify that its base type has no size clause either. Treat other
7904 -- foreign conventions in the same way, and also make sure alignment
7905 -- is set right.
7907 if Has_Foreign_Convention (Typ)
7908 and then not Is_Boolean_Type (Typ)
7909 and then not Has_Size_Clause (Typ)
7910 and then not Has_Size_Clause (Base_Type (Typ))
7911 and then Esize (Typ) < Standard_Integer_Size
7913 -- Don't do this if Short_Enums on target
7915 and then not Target_Short_Enums
7916 then
7917 Set_Esize (Typ, UI_From_Int (Standard_Integer_Size));
7918 Set_Alignment (Typ, Alignment (Standard_Integer));
7920 -- Normal Ada case or size clause present or not Long_C_Enums on target
7922 else
7923 -- If the enumeration type interfaces to C, and it has a size clause
7924 -- that is smaller than the size of int, it warrants a warning. The
7925 -- user may intend the C type to be a boolean or a char, so this is
7926 -- not by itself an error that the Ada compiler can detect, but it
7927 -- is worth a heads-up. For Boolean and Character types we
7928 -- assume that the programmer has the proper C type in mind.
7929 -- For explicit sizes larger than int, assume the user knows what
7930 -- he is doing and that the code is intentional.
7932 if Convention (Typ) = Convention_C
7933 and then Has_Size_Clause (Typ)
7934 and then Esize (Typ) < Standard_Integer_Size
7935 and then not Is_Boolean_Type (Typ)
7936 and then not Is_Character_Type (Typ)
7938 -- Don't do this if Short_Enums on target
7940 and then not Target_Short_Enums
7941 then
7942 Error_Msg_N
7943 ("??the size of enums in C is implementation-defined",
7944 Size_Clause (Typ));
7945 Error_Msg_N
7946 ("\??check that the C counterpart has size of " &
7947 UI_Image (Esize (Typ)),
7948 Size_Clause (Typ));
7949 end if;
7951 Adjust_Esize_For_Alignment (Typ);
7952 end if;
7953 end Freeze_Enumeration_Type;
7955 -----------------------
7956 -- Freeze_Expression --
7957 -----------------------
7959 procedure Freeze_Expression (N : Node_Id) is
7961 function Find_Aggregate_Component_Desig_Type return Entity_Id;
7962 -- If the expression is an array aggregate, the type of the component
7963 -- expressions is also frozen. If the component type is an access type
7964 -- and the expressions include allocators, the designed type is frozen
7965 -- as well.
7967 function In_Expanded_Body (N : Node_Id) return Boolean;
7968 -- Given an N_Handled_Sequence_Of_Statements node, determines whether it
7969 -- is the statement sequence of an expander-generated subprogram: body
7970 -- created for an expression function, for a predicate function, an init
7971 -- proc, a stream subprogram, or a renaming as body. If so, this is not
7972 -- a freezing context and the entity will be frozen at a later point.
7974 function Has_Decl_In_List
7975 (E : Entity_Id;
7976 N : Node_Id;
7977 L : List_Id) return Boolean;
7978 -- Determines whether an entity E referenced in node N is declared in
7979 -- the list L.
7981 -----------------------------------------
7982 -- Find_Aggregate_Component_Desig_Type --
7983 -----------------------------------------
7985 function Find_Aggregate_Component_Desig_Type return Entity_Id is
7986 Assoc : Node_Id;
7987 Exp : Node_Id;
7989 begin
7990 if Present (Expressions (N)) then
7991 Exp := First (Expressions (N));
7992 while Present (Exp) loop
7993 if Nkind (Exp) = N_Allocator then
7994 return Designated_Type (Component_Type (Etype (N)));
7995 end if;
7997 Next (Exp);
7998 end loop;
7999 end if;
8001 if Present (Component_Associations (N)) then
8002 Assoc := First (Component_Associations (N));
8003 while Present (Assoc) loop
8004 if Nkind (Expression (Assoc)) = N_Allocator then
8005 return Designated_Type (Component_Type (Etype (N)));
8006 end if;
8008 Next (Assoc);
8009 end loop;
8010 end if;
8012 return Empty;
8013 end Find_Aggregate_Component_Desig_Type;
8015 ----------------------
8016 -- In_Expanded_Body --
8017 ----------------------
8019 function In_Expanded_Body (N : Node_Id) return Boolean is
8020 P : constant Node_Id := Parent (N);
8021 Id : Entity_Id;
8023 begin
8024 if Nkind (P) /= N_Subprogram_Body then
8025 return False;
8027 -- AI12-0157: An expression function that is a completion is a freeze
8028 -- point. If the body is the result of expansion, it is not.
8030 elsif Was_Expression_Function (P) then
8031 return not Comes_From_Source (P);
8033 -- This is the body of a generated predicate function
8035 elsif Present (Corresponding_Spec (P))
8036 and then Is_Predicate_Function (Corresponding_Spec (P))
8037 then
8038 return True;
8040 else
8041 Id := Defining_Unit_Name (Specification (P));
8043 -- The following are expander-created bodies, or bodies that
8044 -- are not freeze points.
8046 if Nkind (Id) = N_Defining_Identifier
8047 and then (Is_Init_Proc (Id)
8048 or else Is_TSS (Id, TSS_Stream_Input)
8049 or else Is_TSS (Id, TSS_Stream_Output)
8050 or else Is_TSS (Id, TSS_Stream_Read)
8051 or else Is_TSS (Id, TSS_Stream_Write)
8052 or else Is_TSS (Id, TSS_Put_Image)
8053 or else Nkind (Original_Node (P)) =
8054 N_Subprogram_Renaming_Declaration)
8055 then
8056 return True;
8057 else
8058 return False;
8059 end if;
8060 end if;
8061 end In_Expanded_Body;
8063 ----------------------
8064 -- Has_Decl_In_List --
8065 ----------------------
8067 function Has_Decl_In_List
8068 (E : Entity_Id;
8069 N : Node_Id;
8070 L : List_Id) return Boolean
8072 Decl_Node : Node_Id;
8074 begin
8075 -- If E is an itype, pretend that it is declared in N
8077 if Is_Itype (E) then
8078 Decl_Node := N;
8079 else
8080 Decl_Node := Declaration_Node (E);
8081 end if;
8083 return Is_List_Member (Decl_Node)
8084 and then List_Containing (Decl_Node) = L;
8085 end Has_Decl_In_List;
8087 -- Local variables
8089 In_Spec_Exp : constant Boolean := In_Spec_Expression;
8091 Desig_Typ : Entity_Id;
8092 Nam : Entity_Id;
8093 P : Node_Id;
8094 Parent_P : Node_Id;
8095 Typ : Entity_Id;
8097 Allocator_Typ : Entity_Id := Empty;
8099 Freeze_Outside : Boolean := False;
8100 -- This flag is set true if the entity must be frozen outside the
8101 -- current subprogram. This happens in the case of expander generated
8102 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
8103 -- not freeze all entities like other bodies, but which nevertheless
8104 -- may reference entities that have to be frozen before the body and
8105 -- obviously cannot be frozen inside the body.
8107 Freeze_Outside_Subp : Entity_Id := Empty;
8108 -- This entity is set if we are inside a subprogram body and the frozen
8109 -- entity is defined in the enclosing scope of this subprogram. In such
8110 -- case we must skip the subprogram body when climbing the parents chain
8111 -- to locate the correct placement for the freezing node.
8113 -- Start of processing for Freeze_Expression
8115 begin
8116 -- Immediate return if freezing is inhibited. This flag is set by the
8117 -- analyzer to stop freezing on generated expressions that would cause
8118 -- freezing if they were in the source program, but which are not
8119 -- supposed to freeze, since they are created.
8121 if Must_Not_Freeze (N) then
8122 return;
8123 end if;
8125 -- If expression is non-static, then it does not freeze in a default
8126 -- expression, see section "Handling of Default Expressions" in the
8127 -- spec of package Sem for further details. Note that we have to make
8128 -- sure that we actually have a real expression (if we have a subtype
8129 -- indication, we can't test Is_OK_Static_Expression). However, we
8130 -- exclude the case of the prefix of an attribute of a static scalar
8131 -- subtype from this early return, because static subtype attributes
8132 -- should always cause freezing, even in default expressions, but
8133 -- the attribute may not have been marked as static yet (because in
8134 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
8135 -- Freeze_Expression on the prefix).
8137 if In_Spec_Exp
8138 and then Nkind (N) in N_Subexpr
8139 and then not Is_OK_Static_Expression (N)
8140 and then (Nkind (Parent (N)) /= N_Attribute_Reference
8141 or else not (Is_Entity_Name (N)
8142 and then Is_Type (Entity (N))
8143 and then Is_OK_Static_Subtype (Entity (N))))
8144 then
8145 return;
8146 end if;
8148 -- Freeze type of expression if not frozen already
8150 Typ := Empty;
8152 if Nkind (N) in N_Has_Etype and then Present (Etype (N)) then
8153 if not Is_Frozen (Etype (N)) then
8154 Typ := Etype (N);
8156 -- Base type may be an derived numeric type that is frozen at the
8157 -- point of declaration, but first_subtype is still unfrozen.
8159 elsif not Is_Frozen (First_Subtype (Etype (N))) then
8160 Typ := First_Subtype (Etype (N));
8161 end if;
8162 end if;
8164 -- For entity name, freeze entity if not frozen already. A special
8165 -- exception occurs for an identifier that did not come from source.
8166 -- We don't let such identifiers freeze a non-internal entity, i.e.
8167 -- an entity that did come from source, since such an identifier was
8168 -- generated by the expander, and cannot have any semantic effect on
8169 -- the freezing semantics. For example, this stops the parameter of
8170 -- an initialization procedure from freezing the variable.
8172 if Is_Entity_Name (N)
8173 and then Present (Entity (N))
8174 and then not Is_Frozen (Entity (N))
8175 and then (Nkind (N) /= N_Identifier
8176 or else Comes_From_Source (N)
8177 or else not Comes_From_Source (Entity (N)))
8178 then
8179 Nam := Entity (N);
8181 if Present (Nam) and then Ekind (Nam) = E_Function then
8182 Check_Expression_Function (N, Nam);
8183 end if;
8185 else
8186 Nam := Empty;
8187 end if;
8189 -- For an allocator freeze designated type if not frozen already
8191 -- For an aggregate whose component type is an access type, freeze the
8192 -- designated type now, so that its freeze does not appear within the
8193 -- loop that might be created in the expansion of the aggregate. If the
8194 -- designated type is a private type without full view, the expression
8195 -- cannot contain an allocator, so the type is not frozen.
8197 -- For a function, we freeze the entity when the subprogram declaration
8198 -- is frozen, but a function call may appear in an initialization proc.
8199 -- before the declaration is frozen. We need to generate the extra
8200 -- formals, if any, to ensure that the expansion of the call includes
8201 -- the proper actuals. This only applies to Ada subprograms, not to
8202 -- imported ones.
8204 Desig_Typ := Empty;
8206 case Nkind (N) is
8207 when N_Allocator =>
8208 Desig_Typ := Designated_Type (Etype (N));
8210 if Nkind (Expression (N)) = N_Qualified_Expression then
8211 Allocator_Typ := Entity (Subtype_Mark (Expression (N)));
8212 end if;
8214 when N_Aggregate =>
8215 if Is_Array_Type (Etype (N))
8216 and then Is_Access_Type (Component_Type (Etype (N)))
8217 then
8218 -- Check whether aggregate includes allocators
8220 Desig_Typ := Find_Aggregate_Component_Desig_Type;
8221 end if;
8223 when N_Indexed_Component
8224 | N_Selected_Component
8225 | N_Slice
8227 if Is_Access_Type (Etype (Prefix (N))) then
8228 Desig_Typ := Designated_Type (Etype (Prefix (N)));
8229 end if;
8231 when N_Identifier =>
8232 if Present (Nam)
8233 and then Ekind (Nam) = E_Function
8234 and then Nkind (Parent (N)) = N_Function_Call
8235 and then Convention (Nam) = Convention_Ada
8236 then
8237 Create_Extra_Formals (Nam);
8238 end if;
8240 when others =>
8241 null;
8242 end case;
8244 if Desig_Typ /= Empty
8245 and then (Is_Frozen (Desig_Typ)
8246 or else (not Is_Fully_Defined (Desig_Typ)))
8247 then
8248 Desig_Typ := Empty;
8249 end if;
8251 -- All done if nothing needs freezing
8253 if No (Typ)
8254 and then No (Nam)
8255 and then No (Desig_Typ)
8256 and then No (Allocator_Typ)
8257 then
8258 return;
8259 end if;
8261 -- Check if we are inside a subprogram body and the frozen entity is
8262 -- defined in the enclosing scope of this subprogram. In such case we
8263 -- must skip the subprogram when climbing the parents chain to locate
8264 -- the correct placement for the freezing node.
8266 -- This is not needed for default expressions and other spec expressions
8267 -- in generic units since the Move_Freeze_Nodes mechanism (sem_ch12.adb)
8268 -- takes care of placing them at the proper place, after the generic
8269 -- unit.
8271 if Present (Nam)
8272 and then Scope (Nam) /= Current_Scope
8273 and then not (In_Spec_Exp and then Inside_A_Generic)
8274 then
8275 declare
8276 S : Entity_Id := Current_Scope;
8278 begin
8279 while Present (S)
8280 and then In_Same_Source_Unit (Nam, S)
8281 loop
8282 if Scope (S) = Scope (Nam) then
8283 if Is_Subprogram (S) and then Has_Completion (S) then
8284 Freeze_Outside_Subp := S;
8285 end if;
8287 exit;
8288 end if;
8290 S := Scope (S);
8291 end loop;
8292 end;
8293 end if;
8295 -- Examine the enclosing context by climbing the parent chain
8297 -- If we identified that we must freeze the entity outside of a given
8298 -- subprogram then we just climb up to that subprogram checking if some
8299 -- enclosing node is marked as Must_Not_Freeze (since in such case we
8300 -- must not freeze yet this entity).
8302 P := N;
8304 if Present (Freeze_Outside_Subp) then
8305 loop
8306 -- Do not freeze the current expression if another expression in
8307 -- the chain of parents must not be frozen.
8309 if Nkind (P) in N_Subexpr and then Must_Not_Freeze (P) then
8310 return;
8311 end if;
8313 Parent_P := Parent (P);
8315 -- If we don't have a parent, then we are not in a well-formed
8316 -- tree. This is an unusual case, but there are some legitimate
8317 -- situations in which this occurs, notably when the expressions
8318 -- in the range of a type declaration are resolved. We simply
8319 -- ignore the freeze request in this case.
8321 if No (Parent_P) then
8322 return;
8323 end if;
8325 -- If the parent is a subprogram body, the candidate insertion
8326 -- point is just ahead of it.
8328 if Nkind (Parent_P) = N_Subprogram_Body
8329 and then Unique_Defining_Entity (Parent_P) =
8330 Freeze_Outside_Subp
8331 then
8332 P := Parent_P;
8333 exit;
8334 end if;
8336 P := Parent_P;
8337 end loop;
8339 -- Otherwise the traversal serves two purposes - to detect scenarios
8340 -- where freezeing is not needed and to find the proper insertion point
8341 -- for the freeze nodes. Although somewhat similar to Insert_Actions,
8342 -- this traversal is freezing semantics-sensitive. Inserting freeze
8343 -- nodes blindly in the tree may result in types being frozen too early.
8345 else
8346 loop
8347 -- Do not freeze the current expression if another expression in
8348 -- the chain of parents must not be frozen.
8350 if Nkind (P) in N_Subexpr and then Must_Not_Freeze (P) then
8351 return;
8352 end if;
8354 Parent_P := Parent (P);
8356 -- If we don't have a parent, then we are not in a well-formed
8357 -- tree. This is an unusual case, but there are some legitimate
8358 -- situations in which this occurs, notably when the expressions
8359 -- in the range of a type declaration are resolved. We simply
8360 -- ignore the freeze request in this case.
8362 if No (Parent_P) then
8363 return;
8364 end if;
8366 -- See if we have got to an appropriate point in the tree
8368 case Nkind (Parent_P) is
8370 -- A special test for the exception of (RM 13.14(8)) for the
8371 -- case of per-object expressions (RM 3.8(18)) occurring in
8372 -- component definition or a discrete subtype definition. Note
8373 -- that we test for a component declaration which includes both
8374 -- cases we are interested in, and furthermore the tree does
8375 -- not have explicit nodes for either of these two constructs.
8377 when N_Component_Declaration =>
8379 -- The case we want to test for here is an identifier that
8380 -- is a per-object expression, this is either a discriminant
8381 -- that appears in a context other than the component
8382 -- declaration or it is a reference to the type of the
8383 -- enclosing construct.
8385 -- For either of these cases, we skip the freezing
8387 if not In_Spec_Expression
8388 and then Nkind (N) = N_Identifier
8389 and then (Present (Entity (N)))
8390 then
8391 -- We recognize the discriminant case by just looking for
8392 -- a reference to a discriminant. It can only be one for
8393 -- the enclosing construct. Skip freezing in this case.
8395 if Ekind (Entity (N)) = E_Discriminant then
8396 return;
8398 -- For the case of a reference to the enclosing record,
8399 -- (or task or protected type), we look for a type that
8400 -- matches the current scope.
8402 elsif Entity (N) = Current_Scope then
8403 return;
8404 end if;
8405 end if;
8407 -- If we have an enumeration literal that appears as the choice
8408 -- in the aggregate of an enumeration representation clause,
8409 -- then freezing does not occur (RM 13.14(10)).
8411 when N_Enumeration_Representation_Clause =>
8413 -- The case we are looking for is an enumeration literal
8415 if Nkind (N) in N_Identifier | N_Character_Literal
8416 and then Is_Enumeration_Type (Etype (N))
8417 then
8418 -- If enumeration literal appears directly as the choice,
8419 -- do not freeze (this is the normal non-overloaded case)
8421 if Nkind (Parent (N)) = N_Component_Association
8422 and then First (Choices (Parent (N))) = N
8423 then
8424 return;
8426 -- If enumeration literal appears as the name of function
8427 -- which is the choice, then also do not freeze. This
8428 -- happens in the overloaded literal case, where the
8429 -- enumeration literal is temporarily changed to a
8430 -- function call for overloading analysis purposes.
8432 elsif Nkind (Parent (N)) = N_Function_Call
8433 and then Nkind (Parent (Parent (N))) =
8434 N_Component_Association
8435 and then First (Choices (Parent (Parent (N)))) =
8436 Parent (N)
8437 then
8438 return;
8439 end if;
8440 end if;
8442 -- Normally if the parent is a handled sequence of statements,
8443 -- then the current node must be a statement, and that is an
8444 -- appropriate place to insert a freeze node.
8446 when N_Handled_Sequence_Of_Statements =>
8448 -- An exception occurs when the sequence of statements is
8449 -- for an expander generated body that did not do the usual
8450 -- freeze all operation. In this case we usually want to
8451 -- freeze outside this body, not inside it, and we skip
8452 -- past the subprogram body that we are inside.
8454 if In_Expanded_Body (Parent_P) then
8455 declare
8456 Subp_Body : constant Node_Id := Parent (Parent_P);
8457 Spec_Id : Entity_Id;
8459 begin
8460 -- Freeze the entity only when it is declared inside
8461 -- the body of the expander generated procedure. This
8462 -- case is recognized by the subprogram scope of the
8463 -- entity or its type, which is either the spec of an
8464 -- enclosing body, or (in the case of init_procs for
8465 -- which there is no separate spec) the current scope.
8467 if Nkind (Subp_Body) = N_Subprogram_Body then
8468 declare
8469 S : Entity_Id;
8471 begin
8472 Spec_Id := Corresponding_Spec (Subp_Body);
8474 if Present (Typ) then
8475 S := Scope (Typ);
8476 elsif Present (Nam) then
8477 S := Scope (Nam);
8478 else
8479 S := Standard_Standard;
8480 end if;
8482 while S /= Standard_Standard
8483 and then not Is_Subprogram (S)
8484 loop
8485 S := Scope (S);
8486 end loop;
8488 if S = Spec_Id then
8489 exit;
8491 elsif Present (Typ)
8492 and then Scope (Typ) = Current_Scope
8493 and then
8494 Defining_Entity (Subp_Body) = Current_Scope
8495 then
8496 exit;
8497 end if;
8498 end;
8499 end if;
8501 -- If the entity is not frozen by an expression
8502 -- function that is not a completion, continue
8503 -- climbing the tree.
8505 if Nkind (Subp_Body) = N_Subprogram_Body
8506 and then Was_Expression_Function (Subp_Body)
8507 then
8508 null;
8510 -- Freeze outside the body
8512 else
8513 Parent_P := Parent (Parent_P);
8514 Freeze_Outside := True;
8515 end if;
8516 end;
8518 -- Here if normal case where we are in handled statement
8519 -- sequence and want to do the insertion right there.
8521 else
8522 exit;
8523 end if;
8525 -- If parent is a body or a spec or a block, then the current
8526 -- node is a statement or declaration and we can insert the
8527 -- freeze node before it.
8529 when N_Block_Statement
8530 | N_Entry_Body
8531 | N_Package_Body
8532 | N_Package_Specification
8533 | N_Protected_Body
8534 | N_Subprogram_Body
8535 | N_Task_Body
8537 exit;
8539 -- The expander is allowed to define types in any statements
8540 -- list, so any of the following parent nodes also mark a
8541 -- freezing point if the actual node is in a list of
8542 -- statements or declarations.
8544 when N_Abortable_Part
8545 | N_Accept_Alternative
8546 | N_Case_Statement_Alternative
8547 | N_Compilation_Unit_Aux
8548 | N_Conditional_Entry_Call
8549 | N_Delay_Alternative
8550 | N_Elsif_Part
8551 | N_Entry_Call_Alternative
8552 | N_Exception_Handler
8553 | N_Extended_Return_Statement
8554 | N_Freeze_Entity
8555 | N_If_Statement
8556 | N_Selective_Accept
8557 | N_Triggering_Alternative
8559 exit when Is_List_Member (P);
8561 -- The freeze nodes produced by an expression coming from the
8562 -- Actions list of an N_Expression_With_Actions, short-circuit
8563 -- expression or N_Case_Expression_Alternative node must remain
8564 -- within the Actions list if they freeze an entity declared in
8565 -- this list, as inserting the freeze nodes further up the tree
8566 -- may lead to use before declaration issues for the entity.
8568 when N_Case_Expression_Alternative
8569 | N_Expression_With_Actions
8570 | N_Short_Circuit
8572 exit when (Present (Nam)
8573 and then
8574 Has_Decl_In_List (Nam, P, Actions (Parent_P)))
8575 or else (Present (Typ)
8576 and then
8577 Has_Decl_In_List (Typ, P, Actions (Parent_P)));
8579 -- Likewise for an N_If_Expression and its two Actions list
8581 when N_If_Expression =>
8582 declare
8583 L1 : constant List_Id := Then_Actions (Parent_P);
8584 L2 : constant List_Id := Else_Actions (Parent_P);
8586 begin
8587 exit when (Present (Nam)
8588 and then
8589 Has_Decl_In_List (Nam, P, L1))
8590 or else (Present (Typ)
8591 and then
8592 Has_Decl_In_List (Typ, P, L1))
8593 or else (Present (Nam)
8594 and then
8595 Has_Decl_In_List (Nam, P, L2))
8596 or else (Present (Typ)
8597 and then
8598 Has_Decl_In_List (Typ, P, L2));
8599 end;
8601 -- N_Loop_Statement is a special case: a type that appears in
8602 -- the source can never be frozen in a loop (this occurs only
8603 -- because of a loop expanded by the expander), so we keep on
8604 -- going. Otherwise we terminate the search. Same is true of
8605 -- any entity which comes from source (if it has a predefined
8606 -- type, this type does not appear to come from source, but the
8607 -- entity should not be frozen here).
8609 when N_Loop_Statement =>
8610 exit when not Comes_From_Source (Etype (N))
8611 and then (No (Nam) or else not Comes_From_Source (Nam));
8613 -- For all other cases, keep looking at parents
8615 when others =>
8616 null;
8617 end case;
8619 -- We fall through the case if we did not yet find the proper
8620 -- place in the tree for inserting the freeze node, so climb.
8622 P := Parent_P;
8623 end loop;
8624 end if;
8626 -- If the expression appears in a record or an initialization procedure,
8627 -- the freeze nodes are collected and attached to the current scope, to
8628 -- be inserted and analyzed on exit from the scope, to insure that
8629 -- generated entities appear in the correct scope. If the expression is
8630 -- a default for a discriminant specification, the scope is still void.
8631 -- The expression can also appear in the discriminant part of a private
8632 -- or concurrent type.
8634 -- If the expression appears in a constrained subcomponent of an
8635 -- enclosing record declaration, the freeze nodes must be attached to
8636 -- the outer record type so they can eventually be placed in the
8637 -- enclosing declaration list.
8639 -- The other case requiring this special handling is if we are in a
8640 -- default expression, since in that case we are about to freeze a
8641 -- static type, and the freeze scope needs to be the outer scope, not
8642 -- the scope of the subprogram with the default parameter.
8644 -- For default expressions and other spec expressions in generic units,
8645 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
8646 -- placing them at the proper place, after the generic unit.
8648 if (In_Spec_Exp and not Inside_A_Generic)
8649 or else Freeze_Outside
8650 or else (Is_Type (Current_Scope)
8651 and then (not Is_Concurrent_Type (Current_Scope)
8652 or else not Has_Completion (Current_Scope)))
8653 or else Ekind (Current_Scope) = E_Void
8654 then
8655 declare
8656 Freeze_Nodes : List_Id := No_List;
8657 Pos : Int := Scope_Stack.Last;
8659 begin
8660 if Present (Desig_Typ) then
8661 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
8662 end if;
8664 if Present (Typ) then
8665 Freeze_And_Append (Typ, N, Freeze_Nodes);
8666 end if;
8668 if Present (Nam) then
8669 Freeze_And_Append (Nam, N, Freeze_Nodes);
8670 end if;
8672 -- The current scope may be that of a constrained component of
8673 -- an enclosing record declaration, or of a loop of an enclosing
8674 -- quantified expression, which is above the current scope in the
8675 -- scope stack. Indeed in the context of a quantified expression,
8676 -- a scope is created and pushed above the current scope in order
8677 -- to emulate the loop-like behavior of the quantified expression.
8678 -- If the expression is within a top-level pragma, as for a pre-
8679 -- condition on a library-level subprogram, nothing to do.
8681 if not Is_Compilation_Unit (Current_Scope)
8682 and then (Is_Record_Type (Scope (Current_Scope))
8683 or else Nkind (Parent (Current_Scope)) =
8684 N_Quantified_Expression)
8685 then
8686 Pos := Pos - 1;
8687 end if;
8689 if Is_Non_Empty_List (Freeze_Nodes) then
8691 -- When the current scope is transient, insert the freeze nodes
8692 -- prior to the expression that produced them. Transient scopes
8693 -- may create additional declarations when finalizing objects
8694 -- or managing the secondary stack. Inserting the freeze nodes
8695 -- of those constructs prior to the scope would result in a
8696 -- freeze-before-declaration, therefore the freeze node must
8697 -- remain interleaved with their constructs.
8699 if Scope_Is_Transient then
8700 Insert_Actions (N, Freeze_Nodes);
8702 elsif No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
8703 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
8704 Freeze_Nodes;
8705 else
8706 Append_List (Freeze_Nodes,
8707 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
8708 end if;
8709 end if;
8710 end;
8712 return;
8713 end if;
8715 -- Now we have the right place to do the freezing. First, a special
8716 -- adjustment, if we are in spec-expression analysis mode, these freeze
8717 -- actions must not be thrown away (normally all inserted actions are
8718 -- thrown away in this mode. However, the freeze actions are from static
8719 -- expressions and one of the important reasons we are doing this
8720 -- special analysis is to get these freeze actions. Therefore we turn
8721 -- off the In_Spec_Expression mode to propagate these freeze actions.
8722 -- This also means they get properly analyzed and expanded.
8724 In_Spec_Expression := False;
8726 -- Freeze the subtype mark before a qualified expression on an
8727 -- allocator as per AARM 13.14(4.a). This is needed in particular to
8728 -- generate predicate functions.
8730 if Present (Allocator_Typ) then
8731 Freeze_Before (P, Allocator_Typ);
8732 end if;
8734 -- Freeze the designated type of an allocator (RM 13.14(13))
8736 if Present (Desig_Typ) then
8737 Freeze_Before (P, Desig_Typ);
8738 end if;
8740 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
8741 -- the enumeration representation clause exception in the loop above.
8743 if Present (Typ) then
8744 Freeze_Before (P, Typ);
8745 end if;
8747 -- Freeze name if one is present (RM 13.14(11))
8749 if Present (Nam) then
8750 Freeze_Before (P, Nam);
8751 end if;
8753 -- Restore In_Spec_Expression flag
8755 In_Spec_Expression := In_Spec_Exp;
8756 end Freeze_Expression;
8758 -----------------------
8759 -- Freeze_Expr_Types --
8760 -----------------------
8762 procedure Freeze_Expr_Types
8763 (Def_Id : Entity_Id;
8764 Typ : Entity_Id;
8765 Expr : Node_Id;
8766 N : Node_Id)
8768 function Cloned_Expression return Node_Id;
8769 -- Build a duplicate of the expression of the return statement that has
8770 -- no defining entities shared with the original expression.
8772 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result;
8773 -- Freeze all types referenced in the subtree rooted at Node
8775 -----------------------
8776 -- Cloned_Expression --
8777 -----------------------
8779 function Cloned_Expression return Node_Id is
8780 function Clone_Id (Node : Node_Id) return Traverse_Result;
8781 -- Tree traversal routine that clones the defining identifier of
8782 -- iterator and loop parameter specification nodes.
8784 --------------
8785 -- Clone_Id --
8786 --------------
8788 function Clone_Id (Node : Node_Id) return Traverse_Result is
8789 begin
8790 if Nkind (Node) in
8791 N_Iterator_Specification | N_Loop_Parameter_Specification
8792 then
8793 Set_Defining_Identifier
8794 (Node, New_Copy (Defining_Identifier (Node)));
8795 end if;
8797 return OK;
8798 end Clone_Id;
8800 procedure Clone_Def_Ids is new Traverse_Proc (Clone_Id);
8802 -- Local variable
8804 Dup_Expr : constant Node_Id := New_Copy_Tree (Expr);
8806 -- Start of processing for Cloned_Expression
8808 begin
8809 -- We must duplicate the expression with semantic information to
8810 -- inherit the decoration of global entities in generic instances.
8811 -- Set the parent of the new node to be the parent of the original
8812 -- to get the proper context, which is needed for complete error
8813 -- reporting and for semantic analysis.
8815 Set_Parent (Dup_Expr, Parent (Expr));
8817 -- Replace the defining identifier of iterators and loop param
8818 -- specifications by a clone to ensure that the cloned expression
8819 -- and the original expression don't have shared identifiers;
8820 -- otherwise, as part of the preanalysis of the expression, these
8821 -- shared identifiers may be left decorated with itypes which
8822 -- will not be available in the tree passed to the backend.
8824 Clone_Def_Ids (Dup_Expr);
8826 return Dup_Expr;
8827 end Cloned_Expression;
8829 ----------------------
8830 -- Freeze_Type_Refs --
8831 ----------------------
8833 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result is
8834 procedure Check_And_Freeze_Type (Typ : Entity_Id);
8835 -- Check that Typ is fully declared and freeze it if so
8837 ---------------------------
8838 -- Check_And_Freeze_Type --
8839 ---------------------------
8841 procedure Check_And_Freeze_Type (Typ : Entity_Id) is
8842 begin
8843 -- Skip Itypes created by the preanalysis, and itypes whose
8844 -- scope is another type (i.e. component subtypes that depend
8845 -- on a discriminant),
8847 if Is_Itype (Typ)
8848 and then (Scope_Within_Or_Same (Scope (Typ), Def_Id)
8849 or else Is_Type (Scope (Typ)))
8850 then
8851 return;
8852 end if;
8854 -- This provides a better error message than generating primitives
8855 -- whose compilation fails much later. Refine the error message if
8856 -- possible.
8858 Check_Fully_Declared (Typ, Node);
8860 if Error_Posted (Node) then
8861 if Has_Private_Component (Typ)
8862 and then not Is_Private_Type (Typ)
8863 then
8864 Error_Msg_NE ("\type& has private component", Node, Typ);
8865 end if;
8867 else
8868 Freeze_Before (N, Typ);
8869 end if;
8870 end Check_And_Freeze_Type;
8872 -- Start of processing for Freeze_Type_Refs
8874 begin
8875 -- Check that a type referenced by an entity can be frozen
8877 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
8878 -- The entity itself may be a type, as in a membership test
8879 -- or an attribute reference. Freezing its own type would be
8880 -- incomplete if the entity is derived or an extension.
8882 if Is_Type (Entity (Node)) then
8883 Check_And_Freeze_Type (Entity (Node));
8885 else
8886 Check_And_Freeze_Type (Etype (Entity (Node)));
8887 end if;
8889 -- Check that the enclosing record type can be frozen
8891 if Ekind (Entity (Node)) in E_Component | E_Discriminant then
8892 Check_And_Freeze_Type (Scope (Entity (Node)));
8893 end if;
8895 -- Freezing an access type does not freeze the designated type, but
8896 -- freezing conversions between access to interfaces requires that
8897 -- the interface types themselves be frozen, so that dispatch table
8898 -- entities are properly created.
8900 -- Unclear whether a more general rule is needed ???
8902 elsif Nkind (Node) = N_Type_Conversion
8903 and then Is_Access_Type (Etype (Node))
8904 and then Is_Interface (Designated_Type (Etype (Node)))
8905 then
8906 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8907 end if;
8909 -- An implicit dereference freezes the designated type. In the case
8910 -- of a dispatching call whose controlling argument is an access
8911 -- type, the dereference is not made explicit, so we must check for
8912 -- such a call and freeze the designated type.
8914 if Nkind (Node) in N_Has_Etype
8915 and then Present (Etype (Node))
8916 and then Is_Access_Type (Etype (Node))
8917 then
8918 if Nkind (Parent (Node)) = N_Function_Call
8919 and then Node = Controlling_Argument (Parent (Node))
8920 then
8921 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8923 -- An explicit dereference freezes the designated type as well,
8924 -- even though that type is not attached to an entity in the
8925 -- expression.
8927 elsif Nkind (Parent (Node)) = N_Explicit_Dereference then
8928 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8929 end if;
8931 -- An iterator specification freezes the iterator type, even though
8932 -- that type is not attached to an entity in the construct.
8934 elsif Nkind (Node) in N_Has_Etype
8935 and then Nkind (Parent (Node)) = N_Iterator_Specification
8936 and then Node = Name (Parent (Node))
8937 then
8938 declare
8939 Iter : constant Node_Id :=
8940 Find_Value_Of_Aspect (Etype (Node), Aspect_Default_Iterator);
8942 begin
8943 if Present (Iter) then
8944 Check_And_Freeze_Type (Etype (Iter));
8945 end if;
8946 end;
8947 end if;
8949 -- No point in posting several errors on the same expression
8951 if Serious_Errors_Detected > 0 then
8952 return Abandon;
8953 else
8954 return OK;
8955 end if;
8956 end Freeze_Type_Refs;
8958 procedure Freeze_References is new Traverse_Proc (Freeze_Type_Refs);
8960 -- Local variables
8962 Saved_First_Entity : constant Entity_Id := First_Entity (Def_Id);
8963 Saved_Last_Entity : constant Entity_Id := Last_Entity (Def_Id);
8964 Dup_Expr : constant Node_Id := Cloned_Expression;
8966 -- Start of processing for Freeze_Expr_Types
8968 begin
8969 -- Preanalyze a duplicate of the expression to have available the
8970 -- minimum decoration needed to locate referenced unfrozen types
8971 -- without adding any decoration to the function expression.
8973 -- This routine is also applied to expressions in the contract for
8974 -- the subprogram. If that happens when expanding the code for
8975 -- pre/postconditions during expansion of the subprogram body, the
8976 -- subprogram is already installed.
8978 if Def_Id /= Current_Scope then
8979 Push_Scope (Def_Id);
8980 Install_Formals (Def_Id);
8982 Preanalyze_Spec_Expression (Dup_Expr, Typ);
8983 End_Scope;
8984 else
8985 Preanalyze_Spec_Expression (Dup_Expr, Typ);
8986 end if;
8988 -- Restore certain attributes of Def_Id since the preanalysis may
8989 -- have introduced itypes to this scope, thus modifying attributes
8990 -- First_Entity and Last_Entity.
8992 Set_First_Entity (Def_Id, Saved_First_Entity);
8993 Set_Last_Entity (Def_Id, Saved_Last_Entity);
8995 if Present (Last_Entity (Def_Id)) then
8996 Set_Next_Entity (Last_Entity (Def_Id), Empty);
8997 end if;
8999 -- Freeze all types referenced in the expression
9001 Freeze_References (Dup_Expr);
9002 end Freeze_Expr_Types;
9004 -----------------------------
9005 -- Freeze_Fixed_Point_Type --
9006 -----------------------------
9008 -- Certain fixed-point types and subtypes, including implicit base types
9009 -- and declared first subtypes, have not yet set up a range. This is
9010 -- because the range cannot be set until the Small and Size values are
9011 -- known, and these are not known till the type is frozen.
9013 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
9014 -- whose bounds are unanalyzed real literals. This routine will recognize
9015 -- this case, and transform this range node into a properly typed range
9016 -- with properly analyzed and resolved values.
9018 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
9019 Rng : constant Node_Id := Scalar_Range (Typ);
9020 Lo : constant Node_Id := Low_Bound (Rng);
9021 Hi : constant Node_Id := High_Bound (Rng);
9022 Btyp : constant Entity_Id := Base_Type (Typ);
9023 Brng : constant Node_Id := Scalar_Range (Btyp);
9024 BLo : constant Node_Id := Low_Bound (Brng);
9025 BHi : constant Node_Id := High_Bound (Brng);
9026 Ftyp : constant Entity_Id := Underlying_Type (First_Subtype (Typ));
9028 Small : Ureal;
9029 Loval : Ureal;
9030 Hival : Ureal;
9031 Atype : Entity_Id;
9033 Orig_Lo : Ureal;
9034 Orig_Hi : Ureal;
9035 -- Save original bounds (for shaving tests)
9037 Actual_Size : Int;
9038 -- Actual size chosen
9040 function Fsize (Lov, Hiv : Ureal) return Int;
9041 -- Returns size of type with given bounds. Also leaves these
9042 -- bounds set as the current bounds of the Typ.
9044 function Larger (A, B : Ureal) return Boolean;
9045 -- Returns true if A > B with a margin of Typ'Small
9047 function Smaller (A, B : Ureal) return Boolean;
9048 -- Returns true if A < B with a margin of Typ'Small
9050 -----------
9051 -- Fsize --
9052 -----------
9054 function Fsize (Lov, Hiv : Ureal) return Int is
9055 begin
9056 Set_Realval (Lo, Lov);
9057 Set_Realval (Hi, Hiv);
9058 return Minimum_Size (Typ);
9059 end Fsize;
9061 ------------
9062 -- Larger --
9063 ------------
9065 function Larger (A, B : Ureal) return Boolean is
9066 begin
9067 return A > B and then A - Small_Value (Typ) > B;
9068 end Larger;
9070 -------------
9071 -- Smaller --
9072 -------------
9074 function Smaller (A, B : Ureal) return Boolean is
9075 begin
9076 return A < B and then A + Small_Value (Typ) < B;
9077 end Smaller;
9079 -- Start of processing for Freeze_Fixed_Point_Type
9081 begin
9082 -- The type, or its first subtype if we are freezing the anonymous
9083 -- base, may have a delayed Small aspect. It must be analyzed now,
9084 -- so that all characteristics of the type (size, bounds) can be
9085 -- computed and validated in the call to Minimum_Size that follows.
9087 if Has_Delayed_Aspects (Ftyp) then
9088 Analyze_Aspects_At_Freeze_Point (Ftyp);
9089 Set_Has_Delayed_Aspects (Ftyp, False);
9090 end if;
9092 -- Inherit the Small value from the first subtype in any case
9094 if Typ /= Ftyp then
9095 Set_Small_Value (Typ, Small_Value (Ftyp));
9096 end if;
9098 -- If Esize of a subtype has not previously been set, set it now
9100 if not Known_Esize (Typ) then
9101 Atype := Ancestor_Subtype (Typ);
9103 if Present (Atype) then
9104 Set_Esize (Typ, Esize (Atype));
9105 else
9106 Copy_Esize (To => Typ, From => Btyp);
9107 end if;
9108 end if;
9110 -- Immediate return if the range is already analyzed. This means that
9111 -- the range is already set, and does not need to be computed by this
9112 -- routine.
9114 if Analyzed (Rng) then
9115 return;
9116 end if;
9118 -- Immediate return if either of the bounds raises Constraint_Error
9120 if Raises_Constraint_Error (Lo)
9121 or else Raises_Constraint_Error (Hi)
9122 then
9123 return;
9124 end if;
9126 Small := Small_Value (Typ);
9127 Loval := Realval (Lo);
9128 Hival := Realval (Hi);
9130 Orig_Lo := Loval;
9131 Orig_Hi := Hival;
9133 -- Ordinary fixed-point case
9135 if Is_Ordinary_Fixed_Point_Type (Typ) then
9137 -- For the ordinary fixed-point case, we are allowed to fudge the
9138 -- end-points up or down by small. Generally we prefer to fudge up,
9139 -- i.e. widen the bounds for non-model numbers so that the end points
9140 -- are included. However there are cases in which this cannot be
9141 -- done, and indeed cases in which we may need to narrow the bounds.
9142 -- The following circuit makes the decision.
9144 -- Note: our terminology here is that Incl_EP means that the bounds
9145 -- are widened by Small if necessary to include the end points, and
9146 -- Excl_EP means that the bounds are narrowed by Small to exclude the
9147 -- end-points if this reduces the size.
9149 -- Note that in the Incl case, all we care about is including the
9150 -- end-points. In the Excl case, we want to narrow the bounds as
9151 -- much as permitted by the RM, to give the smallest possible size.
9153 Fudge : declare
9154 Loval_Incl_EP : Ureal;
9155 Hival_Incl_EP : Ureal;
9157 Loval_Excl_EP : Ureal;
9158 Hival_Excl_EP : Ureal;
9160 Size_Incl_EP : Int;
9161 Size_Excl_EP : Int;
9163 Model_Num : Ureal;
9164 Actual_Lo : Ureal;
9165 Actual_Hi : Ureal;
9167 begin
9168 -- First step. Base types are required to be symmetrical. Right
9169 -- now, the base type range is a copy of the first subtype range.
9170 -- This will be corrected before we are done, but right away we
9171 -- need to deal with the case where both bounds are non-negative.
9172 -- In this case, we set the low bound to the negative of the high
9173 -- bound, to make sure that the size is computed to include the
9174 -- required sign. Note that we do not need to worry about the
9175 -- case of both bounds negative, because the sign will be dealt
9176 -- with anyway. Furthermore we can't just go making such a bound
9177 -- symmetrical, since in a twos-complement system, there is an
9178 -- extra negative value which could not be accommodated on the
9179 -- positive side.
9181 if Typ = Btyp
9182 and then not UR_Is_Negative (Loval)
9183 and then Hival > Loval
9184 then
9185 Loval := -Hival;
9186 Set_Realval (Lo, Loval);
9187 end if;
9189 -- Compute the fudged bounds. If the bound is a model number, (or
9190 -- greater if given low bound, smaller if high bound) then we do
9191 -- nothing to include it, but we are allowed to backoff to the
9192 -- next adjacent model number when we exclude it. If it is not a
9193 -- model number then we straddle the two values with the model
9194 -- numbers on either side.
9196 Model_Num := UR_Trunc (Loval / Small) * Small;
9198 if UR_Ge (Loval, Model_Num) then
9199 Loval_Incl_EP := Model_Num;
9200 else
9201 Loval_Incl_EP := Model_Num - Small;
9202 end if;
9204 -- The low value excluding the end point is Small greater, but
9205 -- we do not do this exclusion if the low value is positive,
9206 -- since it can't help the size and could actually hurt by
9207 -- crossing the high bound.
9209 if UR_Is_Negative (Loval_Incl_EP) then
9210 Loval_Excl_EP := Loval_Incl_EP + Small;
9212 -- If the value went from negative to zero, then we have the
9213 -- case where Loval_Incl_EP is the model number just below
9214 -- zero, so we want to stick to the negative value for the
9215 -- base type to maintain the condition that the size will
9216 -- include signed values.
9218 if Typ = Btyp
9219 and then UR_Is_Zero (Loval_Excl_EP)
9220 then
9221 Loval_Excl_EP := Loval_Incl_EP;
9222 end if;
9224 else
9225 Loval_Excl_EP := Loval_Incl_EP;
9226 end if;
9228 -- Similar processing for upper bound and high value
9230 Model_Num := UR_Trunc (Hival / Small) * Small;
9232 if UR_Le (Hival, Model_Num) then
9233 Hival_Incl_EP := Model_Num;
9234 else
9235 Hival_Incl_EP := Model_Num + Small;
9236 end if;
9238 if UR_Is_Positive (Hival_Incl_EP) then
9239 Hival_Excl_EP := Hival_Incl_EP - Small;
9240 else
9241 Hival_Excl_EP := Hival_Incl_EP;
9242 end if;
9244 -- One further adjustment is needed. In the case of subtypes, we
9245 -- cannot go outside the range of the base type, or we get
9246 -- peculiarities, and the base type range is already set. This
9247 -- only applies to the Incl values, since clearly the Excl values
9248 -- are already as restricted as they are allowed to be.
9250 if Typ /= Btyp then
9251 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
9252 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
9253 end if;
9255 -- Get size including and excluding end points
9257 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
9258 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
9260 -- No need to exclude end-points if it does not reduce size
9262 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
9263 Loval_Excl_EP := Loval_Incl_EP;
9264 end if;
9266 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
9267 Hival_Excl_EP := Hival_Incl_EP;
9268 end if;
9270 -- Now we set the actual size to be used. We want to use the
9271 -- bounds fudged up to include the end-points but only if this
9272 -- can be done without violating a specifically given size
9273 -- size clause or causing an unacceptable increase in size.
9275 -- Case of size clause given
9277 if Has_Size_Clause (Typ) then
9279 -- Use the inclusive size only if it is consistent with
9280 -- the explicitly specified size.
9282 if Size_Incl_EP <= RM_Size (Typ) then
9283 Actual_Lo := Loval_Incl_EP;
9284 Actual_Hi := Hival_Incl_EP;
9285 Actual_Size := Size_Incl_EP;
9287 -- If the inclusive size is too large, we try excluding
9288 -- the end-points (will be caught later if does not work).
9290 else
9291 Actual_Lo := Loval_Excl_EP;
9292 Actual_Hi := Hival_Excl_EP;
9293 Actual_Size := Size_Excl_EP;
9294 end if;
9296 -- Case of size clause not given
9298 else
9299 -- If we have a base type whose corresponding first subtype
9300 -- has an explicit size that is large enough to include our
9301 -- end-points, then do so. There is no point in working hard
9302 -- to get a base type whose size is smaller than the specified
9303 -- size of the first subtype.
9305 if Has_Size_Clause (Ftyp)
9306 and then Size_Incl_EP <= Esize (Ftyp)
9307 then
9308 Actual_Size := Size_Incl_EP;
9309 Actual_Lo := Loval_Incl_EP;
9310 Actual_Hi := Hival_Incl_EP;
9312 -- If excluding the end-points makes the size smaller and
9313 -- results in a size of 8,16,32,64, then we take the smaller
9314 -- size. For the 64 case, this is compulsory. For the other
9315 -- cases, it seems reasonable. We like to include end points
9316 -- if we can, but not at the expense of moving to the next
9317 -- natural boundary of size.
9319 elsif Size_Incl_EP /= Size_Excl_EP
9320 and then Addressable (Size_Excl_EP)
9321 then
9322 Actual_Size := Size_Excl_EP;
9323 Actual_Lo := Loval_Excl_EP;
9324 Actual_Hi := Hival_Excl_EP;
9326 -- Otherwise we can definitely include the end points
9328 else
9329 Actual_Size := Size_Incl_EP;
9330 Actual_Lo := Loval_Incl_EP;
9331 Actual_Hi := Hival_Incl_EP;
9332 end if;
9334 -- One pathological case: normally we never fudge a low bound
9335 -- down, since it would seem to increase the size (if it has
9336 -- any effect), but for ranges containing single value, or no
9337 -- values, the high bound can be small too large. Consider:
9339 -- type t is delta 2.0**(-14)
9340 -- range 131072.0 .. 0;
9342 -- That lower bound is *just* outside the range of 32 bits, and
9343 -- does need fudging down in this case. Note that the bounds
9344 -- will always have crossed here, since the high bound will be
9345 -- fudged down if necessary, as in the case of:
9347 -- type t is delta 2.0**(-14)
9348 -- range 131072.0 .. 131072.0;
9350 -- So we detect the situation by looking for crossed bounds,
9351 -- and if the bounds are crossed, and the low bound is greater
9352 -- than zero, we will always back it off by small, since this
9353 -- is completely harmless.
9355 if Actual_Lo > Actual_Hi then
9356 if UR_Is_Positive (Actual_Lo) then
9357 Actual_Lo := Loval_Incl_EP - Small;
9358 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
9360 -- And of course, we need to do exactly the same parallel
9361 -- fudge for flat ranges in the negative region.
9363 elsif UR_Is_Negative (Actual_Hi) then
9364 Actual_Hi := Hival_Incl_EP + Small;
9365 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
9366 end if;
9367 end if;
9368 end if;
9370 Set_Realval (Lo, Actual_Lo);
9371 Set_Realval (Hi, Actual_Hi);
9372 end Fudge;
9374 -- Enforce some limitations for ordinary fixed-point types. They come
9375 -- from an exact algorithm used to implement Text_IO.Fixed_IO and the
9376 -- Fore, Image and Value attributes. The requirement on the Small is
9377 -- to lie in the range 2**(-(Siz - 1)) .. 2**(Siz - 1) for a type of
9378 -- Siz bits (Siz=32,64,128) and the requirement on the bounds is to
9379 -- be smaller in magnitude than 10.0**N * 2**(Siz - 1), where N is
9380 -- given by the formula N = floor ((Siz - 1) * log 2 / log 10).
9382 -- If the bounds of a 32-bit type are too large, force 64-bit type
9384 if Actual_Size <= 32
9385 and then Small <= Ureal_2_31
9386 and then (Smaller (Expr_Value_R (Lo), Ureal_M_2_10_18)
9387 or else Larger (Expr_Value_R (Hi), Ureal_2_10_18))
9388 then
9389 Actual_Size := 33;
9390 end if;
9392 -- If the bounds of a 64-bit type are too large, force 128-bit type
9394 if System_Max_Integer_Size = 128
9395 and then Actual_Size <= 64
9396 and then Small <= Ureal_2_63
9397 and then (Smaller (Expr_Value_R (Lo), Ureal_M_9_10_36)
9398 or else Larger (Expr_Value_R (Hi), Ureal_9_10_36))
9399 then
9400 Actual_Size := 65;
9401 end if;
9403 -- Give error messages for first subtypes and not base types, as the
9404 -- bounds of base types are always maximum for their size, see below.
9406 if System_Max_Integer_Size < 128 and then Typ /= Btyp then
9408 -- See the 128-bit case below for the reason why we cannot test
9409 -- against the 2**(-63) .. 2**63 range. This quirk should have
9410 -- been kludged around as in the 128-bit case below, but it was
9411 -- not and we end up with a ludicrous range as a result???
9413 if Small < Ureal_2_M_80 then
9414 Error_Msg_Name_1 := Name_Small;
9415 Error_Msg_N
9416 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", Typ);
9418 elsif Small > Ureal_2_80 then
9419 Error_Msg_Name_1 := Name_Small;
9420 Error_Msg_N
9421 ("`&''%` too large, maximum allowed is 2.0'*'*80", Typ);
9422 end if;
9424 if Smaller (Expr_Value_R (Lo), Ureal_M_9_10_36) then
9425 Error_Msg_Name_1 := Name_First;
9426 Error_Msg_N
9427 ("`&''%` too small, minimum allowed is -9.0E+36", Typ);
9428 end if;
9430 if Larger (Expr_Value_R (Hi), Ureal_9_10_36) then
9431 Error_Msg_Name_1 := Name_Last;
9432 Error_Msg_N
9433 ("`&''%` too large, maximum allowed is 9.0E+36", Typ);
9434 end if;
9436 elsif System_Max_Integer_Size = 128 and then Typ /= Btyp then
9438 -- ACATS c35902d tests a delta equal to 2**(-(Max_Mantissa + 1))
9439 -- but we cannot really support anything smaller than Fine_Delta
9440 -- because of the way we implement I/O for fixed point types???
9442 if Small = Ureal_2_M_128 then
9443 null;
9445 elsif Small < Ureal_2_M_127 then
9446 Error_Msg_Name_1 := Name_Small;
9447 Error_Msg_N
9448 ("`&''%` too small, minimum allowed is 2.0'*'*(-127)", Typ);
9450 elsif Small > Ureal_2_127 then
9451 Error_Msg_Name_1 := Name_Small;
9452 Error_Msg_N
9453 ("`&''%` too large, maximum allowed is 2.0'*'*127", Typ);
9454 end if;
9456 if Actual_Size > 64
9457 and then (Norm_Num (Small) > Uint_2 ** 127
9458 or else Norm_Den (Small) > Uint_2 ** 127)
9459 and then Small /= Ureal_2_M_128
9460 then
9461 Error_Msg_Name_1 := Name_Small;
9462 Error_Msg_N
9463 ("`&''%` not the ratio of two 128-bit integers", Typ);
9464 end if;
9466 if Smaller (Expr_Value_R (Lo), Ureal_M_10_76) then
9467 Error_Msg_Name_1 := Name_First;
9468 Error_Msg_N
9469 ("`&''%` too small, minimum allowed is -1.0E+76", Typ);
9470 end if;
9472 if Larger (Expr_Value_R (Hi), Ureal_10_76) then
9473 Error_Msg_Name_1 := Name_Last;
9474 Error_Msg_N
9475 ("`&''%` too large, maximum allowed is 1.0E+76", Typ);
9476 end if;
9477 end if;
9479 -- For the decimal case, none of this fudging is required, since there
9480 -- are no end-point problems in the decimal case (the end-points are
9481 -- always included).
9483 else
9484 Actual_Size := Fsize (Loval, Hival);
9485 end if;
9487 -- At this stage, the actual size has been calculated and the proper
9488 -- required bounds are stored in the low and high bounds.
9490 if Actual_Size > System_Max_Integer_Size then
9491 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
9492 Error_Msg_Uint_2 := UI_From_Int (System_Max_Integer_Size);
9493 Error_Msg_N
9494 ("size required (^) for type& too large, maximum allowed is ^",
9495 Typ);
9496 Actual_Size := System_Max_Integer_Size;
9497 end if;
9499 -- Check size against explicit given size
9501 if Has_Size_Clause (Typ) then
9502 if Actual_Size > RM_Size (Typ) then
9503 Error_Msg_Uint_1 := RM_Size (Typ);
9504 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
9505 Error_Msg_NE
9506 ("size given (^) for type& too small, minimum allowed is ^",
9507 Size_Clause (Typ), Typ);
9509 else
9510 Actual_Size := UI_To_Int (Esize (Typ));
9511 end if;
9513 -- Increase size to next natural boundary if no size clause given
9515 else
9516 if Actual_Size <= 8 then
9517 Actual_Size := 8;
9518 elsif Actual_Size <= 16 then
9519 Actual_Size := 16;
9520 elsif Actual_Size <= 32 then
9521 Actual_Size := 32;
9522 elsif Actual_Size <= 64 then
9523 Actual_Size := 64;
9524 else
9525 Actual_Size := 128;
9526 end if;
9528 Set_Esize (Typ, UI_From_Int (Actual_Size));
9529 Adjust_Esize_For_Alignment (Typ);
9530 end if;
9532 -- If we have a base type, then expand the bounds so that they extend to
9533 -- the full width of the allocated size in bits, to avoid junk range
9534 -- checks on intermediate computations.
9536 if Typ = Btyp then
9537 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
9538 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
9539 end if;
9541 -- Final step is to reanalyze the bounds using the proper type
9542 -- and set the Corresponding_Integer_Value fields of the literals.
9544 Set_Etype (Lo, Empty);
9545 Set_Analyzed (Lo, False);
9546 Analyze (Lo);
9548 -- Resolve with universal fixed if the base type, and with the base
9549 -- type if we are freezing a subtype. Note we can't resolve the base
9550 -- type with itself, that would be a reference before definition.
9551 -- The resolution of the bounds of a subtype, if they are given by real
9552 -- literals, includes the setting of the Corresponding_Integer_Value,
9553 -- as for other literals of a fixed-point type.
9555 if Typ = Btyp then
9556 Resolve (Lo, Universal_Fixed);
9557 Set_Corresponding_Integer_Value
9558 (Lo, UR_To_Uint (Realval (Lo) / Small));
9559 else
9560 Resolve (Lo, Btyp);
9561 end if;
9563 -- Similar processing for high bound
9565 Set_Etype (Hi, Empty);
9566 Set_Analyzed (Hi, False);
9567 Analyze (Hi);
9569 if Typ = Btyp then
9570 Resolve (Hi, Universal_Fixed);
9571 Set_Corresponding_Integer_Value
9572 (Hi, UR_To_Uint (Realval (Hi) / Small));
9573 else
9574 Resolve (Hi, Btyp);
9575 end if;
9577 -- Set type of range to correspond to bounds
9579 Set_Etype (Rng, Etype (Lo));
9581 -- Set Esize to calculated size if not set already
9583 if not Known_Esize (Typ) then
9584 Set_Esize (Typ, UI_From_Int (Actual_Size));
9585 end if;
9587 -- Set RM_Size if not already set. If already set, check value
9589 declare
9590 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
9592 begin
9593 if Known_RM_Size (Typ) then
9594 if RM_Size (Typ) < Minsiz then
9595 Error_Msg_Uint_1 := RM_Size (Typ);
9596 Error_Msg_Uint_2 := Minsiz;
9597 Error_Msg_NE
9598 ("size given (^) for type& too small, minimum allowed is ^",
9599 Size_Clause (Typ), Typ);
9600 end if;
9602 else
9603 Set_RM_Size (Typ, Minsiz);
9604 end if;
9605 end;
9607 -- Check for shaving
9609 if Comes_From_Source (Typ) then
9611 -- In SPARK mode the given bounds must be strictly representable
9613 if SPARK_Mode = On then
9614 if Orig_Lo < Expr_Value_R (Lo) then
9615 Error_Msg_NE
9616 ("declared low bound of type & is outside type range",
9617 Lo, Typ);
9618 end if;
9620 if Orig_Hi > Expr_Value_R (Hi) then
9621 Error_Msg_NE
9622 ("declared high bound of type & is outside type range",
9623 Hi, Typ);
9624 end if;
9626 else
9627 if Orig_Lo < Expr_Value_R (Lo) then
9628 Error_Msg_N
9629 ("declared low bound of type & is outside type range??", Typ);
9630 Error_Msg_N
9631 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
9632 end if;
9634 if Orig_Hi > Expr_Value_R (Hi) then
9635 Error_Msg_N
9636 ("declared high bound of type & is outside type range??",
9637 Typ);
9638 Error_Msg_N
9639 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
9640 end if;
9641 end if;
9642 end if;
9643 end Freeze_Fixed_Point_Type;
9645 ------------------
9646 -- Freeze_Itype --
9647 ------------------
9649 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
9650 L : List_Id;
9652 begin
9653 Set_Has_Delayed_Freeze (T);
9654 L := Freeze_Entity (T, N);
9656 if Is_Non_Empty_List (L) then
9657 Insert_Actions (N, L);
9658 end if;
9659 end Freeze_Itype;
9661 --------------------------
9662 -- Freeze_Static_Object --
9663 --------------------------
9665 procedure Freeze_Static_Object (E : Entity_Id) is
9667 Cannot_Be_Static : exception;
9668 -- Exception raised if the type of a static object cannot be made
9669 -- static. This happens if the type depends on non-global objects.
9671 procedure Ensure_Expression_Is_SA (N : Node_Id);
9672 -- Called to ensure that an expression used as part of a type definition
9673 -- is statically allocatable, which means that the expression type is
9674 -- statically allocatable, and the expression is either static, or a
9675 -- reference to a library level constant.
9677 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
9678 -- Called to mark a type as static, checking that it is possible
9679 -- to set the type as static. If it is not possible, then the
9680 -- exception Cannot_Be_Static is raised.
9682 -----------------------------
9683 -- Ensure_Expression_Is_SA --
9684 -----------------------------
9686 procedure Ensure_Expression_Is_SA (N : Node_Id) is
9687 Ent : Entity_Id;
9689 begin
9690 Ensure_Type_Is_SA (Etype (N));
9692 if Is_OK_Static_Expression (N) then
9693 return;
9695 elsif Nkind (N) = N_Identifier then
9696 Ent := Entity (N);
9698 if Present (Ent)
9699 and then Ekind (Ent) = E_Constant
9700 and then Is_Library_Level_Entity (Ent)
9701 then
9702 return;
9703 end if;
9704 end if;
9706 raise Cannot_Be_Static;
9707 end Ensure_Expression_Is_SA;
9709 -----------------------
9710 -- Ensure_Type_Is_SA --
9711 -----------------------
9713 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
9714 N : Node_Id;
9715 C : Entity_Id;
9717 begin
9718 -- If type is library level, we are all set
9720 if Is_Library_Level_Entity (Typ) then
9721 return;
9722 end if;
9724 -- We are also OK if the type already marked as statically allocated,
9725 -- which means we processed it before.
9727 if Is_Statically_Allocated (Typ) then
9728 return;
9729 end if;
9731 -- Mark type as statically allocated
9733 Set_Is_Statically_Allocated (Typ);
9735 -- Check that it is safe to statically allocate this type
9737 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
9738 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
9739 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
9741 elsif Is_Array_Type (Typ) then
9742 N := First_Index (Typ);
9743 while Present (N) loop
9744 Ensure_Type_Is_SA (Etype (N));
9745 Next_Index (N);
9746 end loop;
9748 Ensure_Type_Is_SA (Component_Type (Typ));
9750 elsif Is_Access_Type (Typ) then
9751 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
9753 declare
9754 F : Entity_Id;
9755 T : constant Entity_Id := Etype (Designated_Type (Typ));
9757 begin
9758 if T /= Standard_Void_Type then
9759 Ensure_Type_Is_SA (T);
9760 end if;
9762 F := First_Formal (Designated_Type (Typ));
9763 while Present (F) loop
9764 Ensure_Type_Is_SA (Etype (F));
9765 Next_Formal (F);
9766 end loop;
9767 end;
9769 else
9770 Ensure_Type_Is_SA (Designated_Type (Typ));
9771 end if;
9773 elsif Is_Record_Type (Typ) then
9774 C := First_Entity (Typ);
9775 while Present (C) loop
9776 if Ekind (C) = E_Discriminant
9777 or else Ekind (C) = E_Component
9778 then
9779 Ensure_Type_Is_SA (Etype (C));
9781 elsif Is_Type (C) then
9782 Ensure_Type_Is_SA (C);
9783 end if;
9785 Next_Entity (C);
9786 end loop;
9788 elsif Ekind (Typ) = E_Subprogram_Type then
9789 Ensure_Type_Is_SA (Etype (Typ));
9791 C := First_Formal (Typ);
9792 while Present (C) loop
9793 Ensure_Type_Is_SA (Etype (C));
9794 Next_Formal (C);
9795 end loop;
9797 else
9798 raise Cannot_Be_Static;
9799 end if;
9800 end Ensure_Type_Is_SA;
9802 -- Start of processing for Freeze_Static_Object
9804 begin
9805 Ensure_Type_Is_SA (Etype (E));
9807 exception
9808 when Cannot_Be_Static =>
9810 -- If the object that cannot be static is imported or exported, then
9811 -- issue an error message saying that this object cannot be imported
9812 -- or exported. If it has an address clause it is an overlay in the
9813 -- current partition and the static requirement is not relevant.
9814 -- Do not issue any error message when ignoring rep clauses.
9816 if Ignore_Rep_Clauses then
9817 null;
9819 elsif Is_Imported (E) then
9820 if No (Address_Clause (E)) then
9821 Error_Msg_N
9822 ("& cannot be imported (local type is not constant)", E);
9823 end if;
9825 -- Otherwise must be exported, something is wrong if compiler
9826 -- is marking something as statically allocated which cannot be).
9828 else pragma Assert (Is_Exported (E));
9829 Error_Msg_N
9830 ("& cannot be exported (local type is not constant)", E);
9831 end if;
9832 end Freeze_Static_Object;
9834 -----------------------
9835 -- Freeze_Subprogram --
9836 -----------------------
9838 procedure Freeze_Subprogram (E : Entity_Id) is
9839 function Check_Extra_Formals (E : Entity_Id) return Boolean;
9840 -- Return True if the decoration of the attributes associated with extra
9841 -- formals are properly set.
9843 procedure Set_Profile_Convention (Subp_Id : Entity_Id);
9844 -- Set the conventions of all anonymous access-to-subprogram formals and
9845 -- result subtype of subprogram Subp_Id to the convention of Subp_Id.
9847 -------------------------
9848 -- Check_Extra_Formals --
9849 -------------------------
9851 function Check_Extra_Formals (E : Entity_Id) return Boolean is
9852 Last_Formal : Entity_Id := Empty;
9853 Formal : Entity_Id;
9854 Has_Extra_Formals : Boolean := False;
9856 begin
9857 -- No check required if expansion is disabled because extra
9858 -- formals are only generated when we are generating code.
9859 -- See Create_Extra_Formals.
9861 if not Expander_Active then
9862 return True;
9863 end if;
9865 -- Check attribute Extra_Formal: If available, it must be set only
9866 -- on the last formal of E.
9868 Formal := First_Formal (E);
9869 while Present (Formal) loop
9870 if Present (Extra_Formal (Formal)) then
9871 if Has_Extra_Formals then
9872 return False;
9873 end if;
9875 Has_Extra_Formals := True;
9876 end if;
9878 Last_Formal := Formal;
9879 Next_Formal (Formal);
9880 end loop;
9882 -- Check attribute Extra_Accessibility_Of_Result
9884 if Ekind (E) in E_Function | E_Subprogram_Type
9885 and then Needs_Result_Accessibility_Level (E)
9886 and then No (Extra_Accessibility_Of_Result (E))
9887 then
9888 return False;
9889 end if;
9891 -- Check attribute Extra_Formals: If E has extra formals, then this
9892 -- attribute must point to the first extra formal of E.
9894 if Has_Extra_Formals then
9895 return Present (Extra_Formals (E))
9896 and then Present (Extra_Formal (Last_Formal))
9897 and then Extra_Formal (Last_Formal) = Extra_Formals (E);
9899 -- When E has no formals, the first extra formal is available through
9900 -- the Extra_Formals attribute.
9902 elsif Present (Extra_Formals (E)) then
9903 return No (First_Formal (E));
9905 else
9906 return True;
9907 end if;
9908 end Check_Extra_Formals;
9910 ----------------------------
9911 -- Set_Profile_Convention --
9912 ----------------------------
9914 procedure Set_Profile_Convention (Subp_Id : Entity_Id) is
9915 Conv : constant Convention_Id := Convention (Subp_Id);
9917 procedure Set_Type_Convention (Typ : Entity_Id);
9918 -- Set the convention of anonymous access-to-subprogram type Typ and
9919 -- its designated type to Conv.
9921 -------------------------
9922 -- Set_Type_Convention --
9923 -------------------------
9925 procedure Set_Type_Convention (Typ : Entity_Id) is
9926 begin
9927 -- Set the convention on both the anonymous access-to-subprogram
9928 -- type and the subprogram type it points to because both types
9929 -- participate in conformance-related checks.
9931 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
9932 Set_Convention (Typ, Conv);
9933 Set_Convention (Designated_Type (Typ), Conv);
9934 end if;
9935 end Set_Type_Convention;
9937 -- Local variables
9939 Formal : Entity_Id;
9941 -- Start of processing for Set_Profile_Convention
9943 begin
9944 Formal := First_Formal (Subp_Id);
9945 while Present (Formal) loop
9946 Set_Type_Convention (Etype (Formal));
9947 Next_Formal (Formal);
9948 end loop;
9950 if Ekind (Subp_Id) = E_Function then
9951 Set_Type_Convention (Etype (Subp_Id));
9952 end if;
9953 end Set_Profile_Convention;
9955 -- Local variables
9957 F : Entity_Id;
9958 Retype : Entity_Id;
9960 -- Start of processing for Freeze_Subprogram
9962 begin
9963 -- Subprogram may not have an address clause unless it is imported
9965 if Present (Address_Clause (E)) then
9966 if not Is_Imported (E) then
9967 Error_Msg_N
9968 ("address clause can only be given for imported subprogram",
9969 Name (Address_Clause (E)));
9970 end if;
9971 end if;
9973 -- Reset the Pure indication on an imported subprogram unless an
9974 -- explicit Pure_Function pragma was present or the subprogram is an
9975 -- intrinsic. We do this because otherwise it is an insidious error
9976 -- to call a non-pure function from pure unit and have calls
9977 -- mysteriously optimized away. What happens here is that the Import
9978 -- can bypass the normal check to ensure that pure units call only pure
9979 -- subprograms.
9981 -- The reason for the intrinsic exception is that in general, intrinsic
9982 -- functions (such as shifts) are pure anyway. The only exceptions are
9983 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
9984 -- in any case, so no problem arises.
9986 if Is_Imported (E)
9987 and then Is_Pure (E)
9988 and then not Has_Pragma_Pure_Function (E)
9989 and then not Is_Intrinsic_Subprogram (E)
9990 then
9991 Set_Is_Pure (E, False);
9992 end if;
9994 -- For C++ constructors check that their external name has been given
9995 -- (either in pragma CPP_Constructor or in a pragma import).
9997 if Is_Constructor (E)
9998 and then Convention (E) = Convention_CPP
9999 and then
10000 (No (Interface_Name (E))
10001 or else String_Equal
10002 (L => Strval (Interface_Name (E)),
10003 R => Strval (Get_Default_External_Name (E))))
10004 then
10005 Error_Msg_N
10006 ("'C++ constructor must have external name or link name", E);
10007 end if;
10009 -- We also reset the Pure indication on a subprogram with an Address
10010 -- parameter, because the parameter may be used as a pointer and the
10011 -- referenced data may change even if the address value does not.
10013 -- Note that if the programmer gave an explicit Pure_Function pragma,
10014 -- then we believe the programmer, and leave the subprogram Pure. We
10015 -- also suppress this check on run-time files.
10017 if Is_Pure (E)
10018 and then Is_Subprogram (E)
10019 and then not Has_Pragma_Pure_Function (E)
10020 and then not Is_Internal_Unit (Current_Sem_Unit)
10021 then
10022 Check_Function_With_Address_Parameter (E);
10023 end if;
10025 -- Ensure that all anonymous access-to-subprogram types inherit the
10026 -- convention of their related subprogram (RM 6.3.1(13.1/5)). This is
10027 -- not done for a defaulted convention Ada because those types also
10028 -- default to Ada. Convention Protected must not be propagated when
10029 -- the subprogram is an entry because this would be illegal. The only
10030 -- way to force convention Protected on these kinds of types is to
10031 -- include keyword "protected" in the access definition. Conventions
10032 -- Entry and Intrinsic are also not propagated (specified by AI12-0207).
10034 if Convention (E) /= Convention_Ada
10035 and then Convention (E) /= Convention_Protected
10036 and then Convention (E) /= Convention_Entry
10037 and then Convention (E) /= Convention_Intrinsic
10038 then
10039 Set_Profile_Convention (E);
10040 end if;
10042 -- For non-foreign convention subprograms, this is where we create
10043 -- the extra formals (for accessibility level and constrained bit
10044 -- information). We delay this till the freeze point precisely so
10045 -- that we know the convention.
10047 if not Has_Foreign_Convention (E) then
10048 if No (Extra_Formals (E)) then
10050 -- Extra formals are shared by derived subprograms; therefore, if
10051 -- the ultimate alias of E has been frozen before E then the extra
10052 -- formals have been added, but the attribute Extra_Formals is
10053 -- still unset (and must be set now).
10055 if Present (Alias (E))
10056 and then Is_Frozen (Ultimate_Alias (E))
10057 and then Present (Extra_Formals (Ultimate_Alias (E)))
10058 and then Last_Formal (Ultimate_Alias (E)) = Last_Formal (E)
10059 then
10060 Set_Extra_Formals (E, Extra_Formals (Ultimate_Alias (E)));
10062 if Ekind (E) = E_Function then
10063 Set_Extra_Accessibility_Of_Result (E,
10064 Extra_Accessibility_Of_Result (Ultimate_Alias (E)));
10065 end if;
10066 else
10067 Create_Extra_Formals (E);
10068 end if;
10069 end if;
10071 pragma Assert (Check_Extra_Formals (E));
10072 Set_Mechanisms (E);
10074 -- If this is convention Ada and a Valued_Procedure, that's odd
10076 if Ekind (E) = E_Procedure
10077 and then Is_Valued_Procedure (E)
10078 and then Convention (E) = Convention_Ada
10079 and then Warn_On_Export_Import
10080 then
10081 Error_Msg_N
10082 ("??Valued_Procedure has no effect for convention Ada", E);
10083 Set_Is_Valued_Procedure (E, False);
10084 end if;
10086 -- Case of foreign convention
10088 else
10089 Set_Mechanisms (E);
10091 -- For foreign conventions, warn about return of unconstrained array
10093 if Ekind (E) = E_Function then
10094 Retype := Underlying_Type (Etype (E));
10096 -- If no return type, probably some other error, e.g. a
10097 -- missing full declaration, so ignore.
10099 if No (Retype) then
10100 null;
10102 -- If the return type is generic, we have emitted a warning
10103 -- earlier on, and there is nothing else to check here. Specific
10104 -- instantiations may lead to erroneous behavior.
10106 elsif Is_Generic_Type (Etype (E)) then
10107 null;
10109 -- Display warning if returning unconstrained array
10111 elsif Is_Array_Type (Retype)
10112 and then not Is_Constrained (Retype)
10114 -- Check appropriate warning is enabled (should we check for
10115 -- Warnings (Off) on specific entities here, probably so???)
10117 and then Warn_On_Export_Import
10118 then
10119 Error_Msg_N
10120 ("?x?foreign convention function& should not return " &
10121 "unconstrained array", E);
10122 return;
10123 end if;
10124 end if;
10126 -- If any of the formals for an exported foreign convention
10127 -- subprogram have defaults, then emit an appropriate warning since
10128 -- this is odd (default cannot be used from non-Ada code)
10130 if Is_Exported (E) then
10131 F := First_Formal (E);
10132 while Present (F) loop
10133 if Warn_On_Export_Import
10134 and then Present (Default_Value (F))
10135 then
10136 Error_Msg_N
10137 ("?x?parameter cannot be defaulted in non-Ada call",
10138 Default_Value (F));
10139 end if;
10141 Next_Formal (F);
10142 end loop;
10143 end if;
10144 end if;
10146 -- Pragma Inline_Always is disallowed for dispatching subprograms
10147 -- because the address of such subprograms is saved in the dispatch
10148 -- table to support dispatching calls, and dispatching calls cannot
10149 -- be inlined. This is consistent with the restriction against using
10150 -- 'Access or 'Address on an Inline_Always subprogram.
10152 if Is_Dispatching_Operation (E)
10153 and then Has_Pragma_Inline_Always (E)
10154 then
10155 Error_Msg_N
10156 ("pragma Inline_Always not allowed for dispatching subprograms", E);
10157 end if;
10159 -- Because of the implicit representation of inherited predefined
10160 -- operators in the front-end, the overriding status of the operation
10161 -- may be affected when a full view of a type is analyzed, and this is
10162 -- not captured by the analysis of the corresponding type declaration.
10163 -- Therefore the correctness of a not-overriding indicator must be
10164 -- rechecked when the subprogram is frozen.
10166 if Nkind (E) = N_Defining_Operator_Symbol
10167 and then not Error_Posted (Parent (E))
10168 then
10169 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
10170 end if;
10172 Retype := Get_Fullest_View (Etype (E));
10174 if Transform_Function_Array
10175 and then Nkind (Parent (E)) = N_Function_Specification
10176 and then Is_Array_Type (Retype)
10177 and then Is_Constrained (Retype)
10178 and then not Is_Unchecked_Conversion_Instance (E)
10179 and then not Rewritten_For_C (E)
10180 then
10181 Build_Procedure_Form (Unit_Declaration_Node (E));
10182 end if;
10183 end Freeze_Subprogram;
10185 ----------------------
10186 -- Is_Fully_Defined --
10187 ----------------------
10189 function Is_Fully_Defined (T : Entity_Id) return Boolean is
10190 begin
10191 if Ekind (T) = E_Class_Wide_Type then
10192 return Is_Fully_Defined (Etype (T));
10194 elsif Is_Array_Type (T) then
10195 return Is_Fully_Defined (Component_Type (T));
10197 elsif Is_Record_Type (T)
10198 and not Is_Private_Type (T)
10199 then
10200 -- Verify that the record type has no components with private types
10201 -- without completion.
10203 declare
10204 Comp : Entity_Id;
10206 begin
10207 Comp := First_Component (T);
10208 while Present (Comp) loop
10209 if not Is_Fully_Defined (Etype (Comp)) then
10210 return False;
10211 end if;
10213 Next_Component (Comp);
10214 end loop;
10215 return True;
10216 end;
10218 -- For the designated type of an access to subprogram, all types in
10219 -- the profile must be fully defined.
10221 elsif Ekind (T) = E_Subprogram_Type then
10222 declare
10223 F : Entity_Id;
10225 begin
10226 F := First_Formal (T);
10227 while Present (F) loop
10228 if not Is_Fully_Defined (Etype (F)) then
10229 return False;
10230 end if;
10232 Next_Formal (F);
10233 end loop;
10235 return Is_Fully_Defined (Etype (T));
10236 end;
10238 else
10239 return not Is_Private_Type (T)
10240 or else Present (Full_View (Base_Type (T)));
10241 end if;
10242 end Is_Fully_Defined;
10244 ---------------------------------
10245 -- Process_Default_Expressions --
10246 ---------------------------------
10248 procedure Process_Default_Expressions
10249 (E : Entity_Id;
10250 After : in out Node_Id)
10252 Loc : constant Source_Ptr := Sloc (E);
10253 Dbody : Node_Id;
10254 Formal : Node_Id;
10255 Dcopy : Node_Id;
10256 Dnam : Entity_Id;
10258 begin
10259 Set_Default_Expressions_Processed (E);
10261 -- A subprogram instance and its associated anonymous subprogram share
10262 -- their signature. The default expression functions are defined in the
10263 -- wrapper packages for the anonymous subprogram, and should not be
10264 -- generated again for the instance.
10266 if Is_Generic_Instance (E)
10267 and then Present (Alias (E))
10268 and then Default_Expressions_Processed (Alias (E))
10269 then
10270 return;
10271 end if;
10273 Formal := First_Formal (E);
10274 while Present (Formal) loop
10275 if Present (Default_Value (Formal)) then
10277 -- We work with a copy of the default expression because we
10278 -- do not want to disturb the original, since this would mess
10279 -- up the conformance checking.
10281 Dcopy := New_Copy_Tree (Default_Value (Formal));
10283 -- The analysis of the expression may generate insert actions,
10284 -- which of course must not be executed. We wrap those actions
10285 -- in a procedure that is not called, and later on eliminated.
10286 -- The following cases have no side effects, and are analyzed
10287 -- directly.
10289 if Nkind (Dcopy) = N_Identifier
10290 or else Nkind (Dcopy) in N_Expanded_Name
10291 | N_Integer_Literal
10292 | N_Character_Literal
10293 | N_String_Literal
10294 | N_Real_Literal
10295 or else (Nkind (Dcopy) = N_Attribute_Reference
10296 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
10297 or else Known_Null (Dcopy)
10298 then
10299 -- If there is no default function, we must still do a full
10300 -- analyze call on the default value, to ensure that all error
10301 -- checks are performed, e.g. those associated with static
10302 -- evaluation. Note: this branch will always be taken if the
10303 -- analyzer is turned off (but we still need the error checks).
10305 -- Note: the setting of parent here is to meet the requirement
10306 -- that we can only analyze the expression while attached to
10307 -- the tree. Really the requirement is that the parent chain
10308 -- be set, we don't actually need to be in the tree.
10310 Set_Parent (Dcopy, Declaration_Node (Formal));
10311 Analyze (Dcopy);
10313 -- Default expressions are resolved with their own type if the
10314 -- context is generic, to avoid anomalies with private types.
10316 if Ekind (Scope (E)) = E_Generic_Package then
10317 Resolve (Dcopy);
10318 else
10319 Resolve (Dcopy, Etype (Formal));
10320 end if;
10322 -- If that resolved expression will raise constraint error,
10323 -- then flag the default value as raising constraint error.
10324 -- This allows a proper error message on the calls.
10326 if Raises_Constraint_Error (Dcopy) then
10327 Set_Raises_Constraint_Error (Default_Value (Formal));
10328 end if;
10330 -- If the default is a parameterless call, we use the name of
10331 -- the called function directly, and there is no body to build.
10333 elsif Nkind (Dcopy) = N_Function_Call
10334 and then No (Parameter_Associations (Dcopy))
10335 then
10336 null;
10338 -- Else construct and analyze the body of a wrapper procedure
10339 -- that contains an object declaration to hold the expression.
10340 -- Given that this is done only to complete the analysis, it is
10341 -- simpler to build a procedure than a function which might
10342 -- involve secondary stack expansion.
10344 else
10345 Dnam := Make_Temporary (Loc, 'D');
10347 Dbody :=
10348 Make_Subprogram_Body (Loc,
10349 Specification =>
10350 Make_Procedure_Specification (Loc,
10351 Defining_Unit_Name => Dnam),
10353 Declarations => New_List (
10354 Make_Object_Declaration (Loc,
10355 Defining_Identifier => Make_Temporary (Loc, 'T'),
10356 Object_Definition =>
10357 New_Occurrence_Of (Etype (Formal), Loc),
10358 Expression => New_Copy_Tree (Dcopy))),
10360 Handled_Statement_Sequence =>
10361 Make_Handled_Sequence_Of_Statements (Loc,
10362 Statements => Empty_List));
10364 Set_Scope (Dnam, Scope (E));
10365 Set_Assignment_OK (First (Declarations (Dbody)));
10366 Set_Is_Eliminated (Dnam);
10367 Insert_After (After, Dbody);
10368 Analyze (Dbody);
10369 After := Dbody;
10370 end if;
10371 end if;
10373 Next_Formal (Formal);
10374 end loop;
10375 end Process_Default_Expressions;
10377 ----------------------------------------
10378 -- Set_Component_Alignment_If_Not_Set --
10379 ----------------------------------------
10381 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
10382 begin
10383 -- Ignore if not base type, subtypes don't need anything
10385 if Typ /= Base_Type (Typ) then
10386 return;
10387 end if;
10389 -- Do not override existing representation
10391 if Is_Packed (Typ) then
10392 return;
10394 elsif Has_Specified_Layout (Typ) then
10395 return;
10397 elsif Component_Alignment (Typ) /= Calign_Default then
10398 return;
10400 else
10401 Set_Component_Alignment
10402 (Typ, Scope_Stack.Table
10403 (Scope_Stack.Last).Component_Alignment_Default);
10404 end if;
10405 end Set_Component_Alignment_If_Not_Set;
10407 --------------------------
10408 -- Set_SSO_From_Default --
10409 --------------------------
10411 procedure Set_SSO_From_Default (T : Entity_Id) is
10412 Reversed : Boolean;
10414 begin
10415 -- Set default SSO for an array or record base type, except in case of
10416 -- a type extension (which always inherits the SSO of its parent type).
10418 if Is_Base_Type (T)
10419 and then (Is_Array_Type (T)
10420 or else (Is_Record_Type (T)
10421 and then not (Is_Tagged_Type (T)
10422 and then Is_Derived_Type (T))))
10423 then
10424 Reversed :=
10425 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
10426 or else
10427 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
10429 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
10431 -- For a record type, if bit order is specified explicitly,
10432 -- then do not set SSO from default if not consistent. Note that
10433 -- we do not want to look at a Bit_Order attribute definition
10434 -- for a parent: if we were to inherit Bit_Order, then both
10435 -- SSO_Set_*_By_Default flags would have been cleared already
10436 -- (by Inherit_Aspects_At_Freeze_Point).
10438 and then not
10439 (Is_Record_Type (T)
10440 and then
10441 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
10442 and then Reverse_Bit_Order (T) /= Reversed)
10443 then
10444 -- If flags cause reverse storage order, then set the result. Note
10445 -- that we would have ignored the pragma setting the non default
10446 -- storage order in any case, hence the assertion at this point.
10448 pragma Assert
10449 (not Reversed or else Support_Nondefault_SSO_On_Target);
10451 Set_Reverse_Storage_Order (T, Reversed);
10453 -- For a record type, also set reversed bit order. Note: if a bit
10454 -- order has been specified explicitly, then this is a no-op.
10456 if Is_Record_Type (T) then
10457 Set_Reverse_Bit_Order (T, Reversed);
10458 end if;
10459 end if;
10460 end if;
10461 end Set_SSO_From_Default;
10463 ------------------
10464 -- Undelay_Type --
10465 ------------------
10467 procedure Undelay_Type (T : Entity_Id) is
10468 begin
10469 Set_Has_Delayed_Freeze (T, False);
10470 Set_Freeze_Node (T, Empty);
10472 -- Since we don't want T to have a Freeze_Node, we don't want its
10473 -- Full_View or Corresponding_Record_Type to have one either.
10475 -- ??? Fundamentally, this whole handling is unpleasant. What we really
10476 -- want is to be sure that for an Itype that's part of record R and is a
10477 -- subtype of type T, that it's frozen after the later of the freeze
10478 -- points of R and T. We have no way of doing that directly, so what we
10479 -- do is force most such Itypes to be frozen as part of freezing R via
10480 -- this procedure and only delay the ones that need to be delayed
10481 -- (mostly the designated types of access types that are defined as part
10482 -- of the record).
10484 if Is_Private_Type (T)
10485 and then Present (Full_View (T))
10486 and then Is_Itype (Full_View (T))
10487 and then Is_Record_Type (Scope (Full_View (T)))
10488 then
10489 Undelay_Type (Full_View (T));
10490 end if;
10492 if Is_Concurrent_Type (T)
10493 and then Present (Corresponding_Record_Type (T))
10494 and then Is_Itype (Corresponding_Record_Type (T))
10495 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
10496 then
10497 Undelay_Type (Corresponding_Record_Type (T));
10498 end if;
10499 end Undelay_Type;
10501 ------------------
10502 -- Warn_Overlay --
10503 ------------------
10505 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id) is
10506 Ent : constant Entity_Id := Entity (Nam);
10507 -- The object to which the address clause applies
10509 Init : Node_Id;
10510 Old : Entity_Id := Empty;
10511 Decl : Node_Id;
10513 begin
10514 -- No warning if address clause overlay warnings are off
10516 if not Address_Clause_Overlay_Warnings then
10517 return;
10518 end if;
10520 -- No warning if there is an explicit initialization
10522 Init := Original_Node (Expression (Declaration_Node (Ent)));
10524 if Present (Init) and then Comes_From_Source (Init) then
10525 return;
10526 end if;
10528 -- We only give the warning for non-imported entities of a type for
10529 -- which a non-null base init proc is defined, or for objects of access
10530 -- types with implicit null initialization, or when Normalize_Scalars
10531 -- applies and the type is scalar or a string type (the latter being
10532 -- tested for because predefined String types are initialized by inline
10533 -- code rather than by an init_proc). Note that we do not give the
10534 -- warning for Initialize_Scalars, since we suppressed initialization
10535 -- in this case. Also, do not warn if Suppress_Initialization is set
10536 -- either on the type, or on the object via pragma or aspect.
10538 if Present (Expr)
10539 and then not Is_Imported (Ent)
10540 and then not Initialization_Suppressed (Typ)
10541 and then not (Ekind (Ent) = E_Variable
10542 and then Initialization_Suppressed (Ent))
10543 and then (Has_Non_Null_Base_Init_Proc (Typ)
10544 or else Is_Access_Type (Typ)
10545 or else (Normalize_Scalars
10546 and then (Is_Scalar_Type (Typ)
10547 or else Is_String_Type (Typ))))
10548 then
10549 if Nkind (Expr) = N_Attribute_Reference
10550 and then Is_Entity_Name (Prefix (Expr))
10551 then
10552 Old := Entity (Prefix (Expr));
10554 elsif Is_Entity_Name (Expr)
10555 and then Ekind (Entity (Expr)) = E_Constant
10556 then
10557 Decl := Declaration_Node (Entity (Expr));
10559 if Nkind (Decl) = N_Object_Declaration
10560 and then Present (Expression (Decl))
10561 and then Nkind (Expression (Decl)) = N_Attribute_Reference
10562 and then Is_Entity_Name (Prefix (Expression (Decl)))
10563 then
10564 Old := Entity (Prefix (Expression (Decl)));
10566 elsif Nkind (Expr) = N_Function_Call then
10567 return;
10568 end if;
10570 -- A function call (most likely to To_Address) is probably not an
10571 -- overlay, so skip warning. Ditto if the function call was inlined
10572 -- and transformed into an entity.
10574 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
10575 return;
10576 end if;
10578 -- If a pragma Import follows, we assume that it is for the current
10579 -- target of the address clause, and skip the warning. There may be
10580 -- a source pragma or an aspect that specifies import and generates
10581 -- the corresponding pragma. These will indicate that the entity is
10582 -- imported and that is checked above so that the spurious warning
10583 -- (generated when the entity is frozen) will be suppressed. The
10584 -- pragma may be attached to the aspect, so it is not yet a list
10585 -- member.
10587 if Is_List_Member (Parent (Expr)) then
10588 Decl := Next (Parent (Expr));
10590 if Present (Decl)
10591 and then Nkind (Decl) = N_Pragma
10592 and then Pragma_Name (Decl) = Name_Import
10593 then
10594 return;
10595 end if;
10596 end if;
10598 -- Otherwise give warning message
10600 if Present (Old) then
10601 Error_Msg_Node_2 := Old;
10602 Error_Msg_N
10603 ("default initialization of & may modify &?o?",
10604 Nam);
10605 else
10606 Error_Msg_N
10607 ("default initialization of & may modify overlaid storage?o?",
10608 Nam);
10609 end if;
10611 -- Add friendly warning if initialization comes from a packed array
10612 -- component.
10614 if Is_Record_Type (Typ) then
10615 declare
10616 Comp : Entity_Id;
10618 begin
10619 Comp := First_Component (Typ);
10620 while Present (Comp) loop
10621 if Nkind (Parent (Comp)) = N_Component_Declaration
10622 and then Present (Expression (Parent (Comp)))
10623 then
10624 exit;
10625 elsif Is_Array_Type (Etype (Comp))
10626 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
10627 then
10628 Error_Msg_NE
10629 ("\packed array component& " &
10630 "will be initialized to zero??",
10631 Nam, Comp);
10632 exit;
10633 else
10634 Next_Component (Comp);
10635 end if;
10636 end loop;
10637 end;
10638 end if;
10640 Error_Msg_N
10641 ("\use pragma Import for & to " &
10642 "suppress initialization (RM B.1(24))??",
10643 Nam);
10644 end if;
10645 end Warn_Overlay;
10647 end Freeze;