Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / freeze.adb
blob86622003b97a90e8ba6002a15cbafb6d79dfcee3
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-2023, 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;
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 if Nkind (Decl) /= N_Subprogram_Declaration then
477 Rewrite (N,
478 Make_Subprogram_Declaration (Loc,
479 Specification => Specification (N)));
480 end if;
482 -- For simple renamings, subsequent calls can be expanded directly as
483 -- calls to the renamed entity. The body must be generated in any case
484 -- for calls that may appear elsewhere. This is not done in the case
485 -- where the subprogram is an instantiation because the actual proper
486 -- body has not been built yet.
488 if Ekind (Old_S) in E_Function | E_Procedure
489 and then not Is_Generic_Instance (Old_S)
490 then
491 Set_Body_To_Inline (Decl, Old_S);
492 end if;
494 -- Check whether the return type is a limited view. If the subprogram
495 -- is already frozen the generated body may have a non-limited view
496 -- of the type, that must be used, because it is the one in the spec
497 -- of the renaming declaration.
499 if Ekind (Old_S) = E_Function
500 and then Is_Entity_Name (Result_Definition (Spec))
501 then
502 declare
503 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
504 begin
505 if Has_Non_Limited_View (Ret_Type) then
506 Set_Result_Definition
507 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
508 end if;
509 end;
510 end if;
512 -- The body generated for this renaming is an internal artifact, and
513 -- does not constitute a freeze point for the called entity.
515 Set_Must_Not_Freeze (Call_Name);
517 Formal := First_Formal (Defining_Entity (Decl));
519 if Present (Pref) then
520 declare
521 Pref_Type : constant Entity_Id := Etype (Pref);
522 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
524 begin
525 -- The controlling formal may be an access parameter, or the
526 -- actual may be an access value, so adjust accordingly.
528 if Is_Access_Type (Pref_Type)
529 and then not Is_Access_Type (Form_Type)
530 then
531 Actuals := New_List
532 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
534 elsif Is_Access_Type (Form_Type)
535 and then not Is_Access_Type (Pref)
536 then
537 Actuals :=
538 New_List (
539 Make_Attribute_Reference (Loc,
540 Attribute_Name => Name_Access,
541 Prefix => Relocate_Node (Pref)));
542 else
543 Actuals := New_List (Pref);
544 end if;
545 end;
547 elsif Present (Formal) then
548 Actuals := New_List;
550 else
551 Actuals := No_List;
552 end if;
554 while Present (Formal) loop
555 Append (New_Occurrence_Of (Formal, Loc), Actuals);
556 Next_Formal (Formal);
557 end loop;
559 -- If the renamed entity is an entry, inherit its profile. For other
560 -- renamings as bodies, both profiles must be subtype conformant, so it
561 -- is not necessary to replace the profile given in the declaration.
562 -- However, default values that are aggregates are rewritten when
563 -- partially analyzed, so we recover the original aggregate to insure
564 -- that subsequent conformity checking works. Similarly, if the default
565 -- expression was constant-folded, recover the original expression.
567 Formal := First_Formal (Defining_Entity (Decl));
569 if Present (Formal) then
570 O_Formal := First_Formal (Old_S);
571 Param_Spec := First (Parameter_Specifications (Spec));
572 while Present (Formal) loop
573 if Is_Entry (Old_S) then
574 if Nkind (Parameter_Type (Param_Spec)) /=
575 N_Access_Definition
576 then
577 Set_Etype (Formal, Etype (O_Formal));
578 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
579 end if;
581 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
582 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
583 Nkind (Default_Value (O_Formal))
584 then
585 Set_Expression (Param_Spec,
586 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
587 end if;
589 Next_Formal (Formal);
590 Next_Formal (O_Formal);
591 Next (Param_Spec);
592 end loop;
593 end if;
595 -- If the renamed entity is a function, the generated body contains a
596 -- return statement. Otherwise, build a procedure call. If the entity is
597 -- an entry, subsequent analysis of the call will transform it into the
598 -- proper entry or protected operation call. If the renamed entity is
599 -- a character literal, return it directly.
601 if Ekind (Old_S) = E_Function
602 or else Ekind (Old_S) = E_Operator
603 or else (Ekind (Old_S) = E_Subprogram_Type
604 and then Etype (Old_S) /= Standard_Void_Type)
605 then
606 Call_Node :=
607 Make_Simple_Return_Statement (Loc,
608 Expression =>
609 Make_Function_Call (Loc,
610 Name => Call_Name,
611 Parameter_Associations => Actuals));
613 elsif Ekind (Old_S) = E_Enumeration_Literal then
614 Call_Node :=
615 Make_Simple_Return_Statement (Loc,
616 Expression => New_Occurrence_Of (Old_S, Loc));
618 elsif Nkind (Nam) = N_Character_Literal then
619 Call_Node :=
620 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
622 else
623 Call_Node :=
624 Make_Procedure_Call_Statement (Loc,
625 Name => Call_Name,
626 Parameter_Associations => Actuals);
627 end if;
629 -- Create entities for subprogram body and formals
631 Set_Defining_Unit_Name (Spec,
632 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
634 Param_Spec := First (Parameter_Specifications (Spec));
635 while Present (Param_Spec) loop
636 Set_Defining_Identifier (Param_Spec,
637 Make_Defining_Identifier (Loc,
638 Chars => Chars (Defining_Identifier (Param_Spec))));
639 Next (Param_Spec);
640 end loop;
642 -- In GNATprove, prefer to generate an expression function whenever
643 -- possible, to benefit from the more precise analysis in that case
644 -- (as if an implicit postcondition had been generated).
646 if GNATprove_Mode
647 and then Nkind (Call_Node) = N_Simple_Return_Statement
648 then
649 Body_Node :=
650 Make_Expression_Function (Loc,
651 Specification => Spec,
652 Expression => Expression (Call_Node));
653 else
654 Body_Node :=
655 Make_Subprogram_Body (Loc,
656 Specification => Spec,
657 Declarations => New_List,
658 Handled_Statement_Sequence =>
659 Make_Handled_Sequence_Of_Statements (Loc,
660 Statements => New_List (Call_Node)));
661 end if;
663 -- Link the body to the entity whose declaration it completes. If
664 -- the body is analyzed when the renamed entity is frozen, it may
665 -- be necessary to restore the proper scope (see package Exp_Ch13).
667 if Nkind (N) = N_Subprogram_Renaming_Declaration
668 and then Present (Corresponding_Spec (N))
669 then
670 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
671 else
672 Set_Corresponding_Spec (Body_Node, New_S);
673 end if;
675 return Body_Node;
676 end Build_Renamed_Body;
678 --------------------------
679 -- Check_Address_Clause --
680 --------------------------
682 procedure Check_Address_Clause (E : Entity_Id) is
683 Addr : constant Node_Id := Address_Clause (E);
684 Typ : constant Entity_Id := Etype (E);
685 Decl : Node_Id;
686 Expr : Node_Id;
687 Init : Node_Id;
688 Lhs : Node_Id;
689 Tag_Assign : Node_Id;
691 begin
692 if Present (Addr) then
694 -- For a deferred constant, the initialization value is on full view
696 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
697 Decl := Declaration_Node (Full_View (E));
698 else
699 Decl := Declaration_Node (E);
700 end if;
702 Expr := Expression (Addr);
704 if Needs_Constant_Address (Decl, Typ) then
705 Check_Constant_Address_Clause (Expr, E);
707 -- Has_Delayed_Freeze was set on E when the address clause was
708 -- analyzed, and must remain set because we want the address
709 -- clause to be elaborated only after any entity it references
710 -- has been elaborated.
711 end if;
713 -- If Rep_Clauses are to be ignored, remove address clause from
714 -- list attached to entity, because it may be illegal for gigi,
715 -- for example by breaking order of elaboration.
717 if Ignore_Rep_Clauses then
718 declare
719 Rep : Node_Id;
721 begin
722 Rep := First_Rep_Item (E);
724 if Rep = Addr then
725 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
727 else
728 while Present (Rep)
729 and then Next_Rep_Item (Rep) /= Addr
730 loop
731 Next_Rep_Item (Rep);
732 end loop;
733 end if;
735 if Present (Rep) then
736 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
737 end if;
738 end;
740 -- And now remove the address clause
742 Kill_Rep_Clause (Addr);
744 elsif not Error_Posted (Expr)
745 and then not Needs_Finalization (Typ)
746 then
747 Warn_Overlay (Expr, Typ, Name (Addr));
748 end if;
750 Init := Expression (Decl);
752 -- If a variable, or a non-imported constant, overlays a constant
753 -- object and has an initialization value, then the initialization
754 -- may end up writing into read-only memory. Detect the cases of
755 -- statically identical values and remove the initialization. In
756 -- the other cases, give a warning. We will give other warnings
757 -- later for the variable if it is assigned.
759 if (Ekind (E) = E_Variable
760 or else (Ekind (E) = E_Constant
761 and then not Is_Imported (E)))
762 and then Overlays_Constant (E)
763 and then Present (Init)
764 then
765 declare
766 O_Ent : Entity_Id;
767 Off : Boolean;
769 begin
770 Find_Overlaid_Entity (Addr, O_Ent, Off);
772 if Ekind (O_Ent) = E_Constant
773 and then Etype (O_Ent) = Typ
774 and then Present (Constant_Value (O_Ent))
775 and then Compile_Time_Compare
776 (Init,
777 Constant_Value (O_Ent),
778 Assume_Valid => True) = EQ
779 then
780 Set_No_Initialization (Decl);
781 return;
783 elsif Comes_From_Source (Init)
784 and then Address_Clause_Overlay_Warnings
785 then
786 Error_Msg_Sloc := Sloc (Addr);
787 Error_Msg_NE
788 ("?o?constant& may be modified via address clause#",
789 Decl, O_Ent);
790 end if;
791 end;
792 end if;
794 -- Remove side effects from initial expression, except in the case of
795 -- limited build-in-place calls and aggregates, which have their own
796 -- expansion elsewhere. This exception is necessary to avoid copying
797 -- limited objects.
799 if Present (Init)
800 and then not Is_Limited_View (Typ)
801 then
802 -- Capture initialization value at point of declaration, and make
803 -- explicit assignment legal, because object may be a constant.
805 Remove_Side_Effects (Init);
806 Lhs := New_Occurrence_Of (E, Sloc (Decl));
807 Set_Assignment_OK (Lhs);
809 -- Move initialization to freeze actions, once the object has
810 -- been frozen and the address clause alignment check has been
811 -- performed.
813 Append_Freeze_Action (E,
814 Make_Assignment_Statement (Sloc (Decl),
815 Name => Lhs,
816 Expression => Expression (Decl)));
818 Set_No_Initialization (Decl);
820 -- If the object is tagged, check whether the tag must be
821 -- reassigned explicitly.
823 Tag_Assign := Make_Tag_Assignment (Decl);
824 if Present (Tag_Assign) then
825 Append_Freeze_Action (E, Tag_Assign);
826 end if;
827 end if;
828 end if;
829 end Check_Address_Clause;
831 -----------------------------
832 -- Check_Compile_Time_Size --
833 -----------------------------
835 procedure Check_Compile_Time_Size (T : Entity_Id) is
837 procedure Set_Small_Size (T : Entity_Id; S : Uint);
838 -- Sets the compile time known size in the RM_Size field of T, checking
839 -- for a size clause that was given which attempts to give a small size.
841 function Size_Known (T : Entity_Id) return Boolean;
842 -- Recursive function that does all the work
844 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
845 -- If T is a constrained subtype, its size is not known if any of its
846 -- discriminant constraints is not static and it is not a null record.
847 -- The test is conservative and doesn't check that the components are
848 -- in fact constrained by non-static discriminant values. Could be made
849 -- more precise ???
851 --------------------
852 -- Set_Small_Size --
853 --------------------
855 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
856 begin
857 if S > System_Max_Integer_Size then
858 return;
860 -- Check for bad size clause given
862 elsif Has_Size_Clause (T) then
863 if RM_Size (T) < S then
864 Error_Msg_Uint_1 := S;
865 Error_Msg_NE (Size_Too_Small_Message, Size_Clause (T), T);
866 end if;
868 -- Set size if not set already. Do not set it to Uint_0, because in
869 -- some cases (notably array-of-record), the Component_Size is
870 -- No_Uint, which causes S to be Uint_0. Presumably the RM_Size and
871 -- Component_Size will eventually be set correctly by the back end.
873 elsif not Known_RM_Size (T) and then S /= Uint_0 then
874 Set_RM_Size (T, S);
875 end if;
876 end Set_Small_Size;
878 ----------------
879 -- Size_Known --
880 ----------------
882 function Size_Known (T : Entity_Id) return Boolean is
883 Comp : Entity_Id;
884 Ctyp : Entity_Id;
886 begin
887 if Size_Known_At_Compile_Time (T) then
888 return True;
890 -- Always True for elementary types, even generic formal elementary
891 -- types. We used to return False in the latter case, but the size
892 -- is known at compile time, even in the template, we just do not
893 -- know the exact size but that's not the point of this routine.
895 elsif Is_Elementary_Type (T) or else Is_Task_Type (T) then
896 return True;
898 -- Array types
900 elsif Is_Array_Type (T) then
902 -- String literals always have known size, and we can set it
904 if Ekind (T) = E_String_Literal_Subtype then
905 if Known_Component_Size (T) then
906 Set_Small_Size
907 (T, Component_Size (T) * String_Literal_Length (T));
909 else
910 -- The following is wrong, but does what previous versions
911 -- did. The Component_Size is unknown for the string in a
912 -- pragma Warnings.
913 Set_Small_Size (T, Uint_0);
914 end if;
916 return True;
918 -- Unconstrained types never have known at compile time size
920 elsif not Is_Constrained (T) then
921 return False;
923 -- Don't do any recursion on type with error posted, since we may
924 -- have a malformed type that leads us into a loop.
926 elsif Error_Posted (T) then
927 return False;
929 -- Otherwise if component size unknown, then array size unknown
931 elsif not Size_Known (Component_Type (T)) then
932 return False;
933 end if;
935 -- Check for all indexes static, and also compute possible size
936 -- (in case it is not greater than System_Max_Integer_Size and
937 -- thus may be packable).
939 declare
940 Index : Entity_Id;
941 Low : Node_Id;
942 High : Node_Id;
943 Size : Uint := Component_Size (T);
944 Dim : Uint;
946 begin
947 -- See comment in Set_Small_Size above
949 if No (Size) then
950 Size := Uint_0;
951 end if;
953 Index := First_Index (T);
954 while Present (Index) loop
955 if Nkind (Index) = N_Range then
956 Get_Index_Bounds (Index, Low, High);
958 elsif Error_Posted (Scalar_Range (Etype (Index))) then
959 return False;
961 else
962 Low := Type_Low_Bound (Etype (Index));
963 High := Type_High_Bound (Etype (Index));
964 end if;
966 if not Compile_Time_Known_Value (Low)
967 or else not Compile_Time_Known_Value (High)
968 or else Etype (Index) = Any_Type
969 then
970 return False;
972 else
973 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
975 if Dim > Uint_0 then
976 Size := Size * Dim;
977 else
978 Size := Uint_0;
979 end if;
980 end if;
982 Next_Index (Index);
983 end loop;
985 Set_Small_Size (T, Size);
986 return True;
987 end;
989 -- For non-generic private types, go to underlying type if present
991 elsif Is_Private_Type (T)
992 and then not Is_Generic_Type (T)
993 and then Present (Underlying_Type (T))
994 then
995 -- Don't do any recursion on type with error posted, since we may
996 -- have a malformed type that leads us into a loop.
998 if Error_Posted (T) then
999 return False;
1000 else
1001 return Size_Known (Underlying_Type (T));
1002 end if;
1004 -- Record types
1006 elsif Is_Record_Type (T) then
1008 -- A class-wide type is never considered to have a known size
1010 if Is_Class_Wide_Type (T) then
1011 return False;
1013 -- A subtype of a variant record must not have non-static
1014 -- discriminated components.
1016 elsif T /= Base_Type (T)
1017 and then not Static_Discriminated_Components (T)
1018 then
1019 return False;
1021 -- Don't do any recursion on type with error posted, since we may
1022 -- have a malformed type that leads us into a loop.
1024 elsif Error_Posted (T) then
1025 return False;
1026 end if;
1028 -- Now look at the components of the record
1030 declare
1031 -- The following two variables are used to keep track of the
1032 -- size of packed records if we can tell the size of the packed
1033 -- record in the front end. Packed_Size_Known is True if so far
1034 -- we can figure out the size. It is initialized to True for a
1035 -- packed record, unless the record has either discriminants or
1036 -- independent components, or is a strict-alignment type, since
1037 -- it cannot be fully packed in this case.
1039 -- The reason we eliminate the discriminated case is that
1040 -- we don't know the way the back end lays out discriminated
1041 -- packed records. If Packed_Size_Known is True, then
1042 -- Packed_Size is the size in bits so far.
1044 Packed_Size_Known : Boolean :=
1045 Is_Packed (T)
1046 and then not Has_Discriminants (T)
1047 and then not Has_Independent_Components (T)
1048 and then not Strict_Alignment (T);
1050 Packed_Size : Uint := Uint_0;
1051 -- Size in bits so far
1053 begin
1054 -- Test for variant part present
1056 if Has_Discriminants (T)
1057 and then Present (Parent (T))
1058 and then Nkind (Parent (T)) = N_Full_Type_Declaration
1059 and then Nkind (Type_Definition (Parent (T))) =
1060 N_Record_Definition
1061 and then not Null_Present (Type_Definition (Parent (T)))
1062 and then
1063 Present (Variant_Part
1064 (Component_List (Type_Definition (Parent (T)))))
1065 then
1066 -- If variant part is present, and type is unconstrained,
1067 -- then we must have defaulted discriminants, or a size
1068 -- clause must be present for the type, or else the size
1069 -- is definitely not known at compile time.
1071 if not Is_Constrained (T)
1072 and then
1073 No (Discriminant_Default_Value (First_Discriminant (T)))
1074 and then not Known_RM_Size (T)
1075 then
1076 return False;
1077 end if;
1078 end if;
1080 -- Loop through components
1082 Comp := First_Component_Or_Discriminant (T);
1083 while Present (Comp) loop
1084 Ctyp := Etype (Comp);
1086 -- We do not know the packed size if there is a component
1087 -- clause present (we possibly could, but this would only
1088 -- help in the case of a record with partial rep clauses.
1089 -- That's because in the case of full rep clauses, the
1090 -- size gets figured out anyway by a different circuit).
1092 if Present (Component_Clause (Comp)) then
1093 Packed_Size_Known := False;
1094 end if;
1096 -- We do not know the packed size for an independent
1097 -- component or if it is of a strict-alignment type,
1098 -- since packing does not touch these (RM 13.2(7)).
1100 if Is_Independent (Comp)
1101 or else Is_Independent (Ctyp)
1102 or else Strict_Alignment (Ctyp)
1103 then
1104 Packed_Size_Known := False;
1105 end if;
1107 -- We need to identify a component that is an array where
1108 -- the index type is an enumeration type with non-standard
1109 -- representation, and some bound of the type depends on a
1110 -- discriminant.
1112 -- This is because gigi computes the size by doing a
1113 -- substitution of the appropriate discriminant value in
1114 -- the size expression for the base type, and gigi is not
1115 -- clever enough to evaluate the resulting expression (which
1116 -- involves a call to rep_to_pos) at compile time.
1118 -- It would be nice if gigi would either recognize that
1119 -- this expression can be computed at compile time, or
1120 -- alternatively figured out the size from the subtype
1121 -- directly, where all the information is at hand ???
1123 if Is_Array_Type (Etype (Comp))
1124 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1125 then
1126 declare
1127 Ocomp : constant Entity_Id :=
1128 Original_Record_Component (Comp);
1129 OCtyp : constant Entity_Id := Etype (Ocomp);
1130 Ind : Node_Id;
1131 Indtyp : Entity_Id;
1132 Lo, Hi : Node_Id;
1134 begin
1135 Ind := First_Index (OCtyp);
1136 while Present (Ind) loop
1137 Indtyp := Etype (Ind);
1139 if Is_Enumeration_Type (Indtyp)
1140 and then Has_Non_Standard_Rep (Indtyp)
1141 then
1142 Lo := Type_Low_Bound (Indtyp);
1143 Hi := Type_High_Bound (Indtyp);
1145 if Is_Entity_Name (Lo)
1146 and then Ekind (Entity (Lo)) = E_Discriminant
1147 then
1148 return False;
1150 elsif Is_Entity_Name (Hi)
1151 and then Ekind (Entity (Hi)) = E_Discriminant
1152 then
1153 return False;
1154 end if;
1155 end if;
1157 Next_Index (Ind);
1158 end loop;
1159 end;
1160 end if;
1162 -- Clearly size of record is not known if the size of one of
1163 -- the components is not known.
1165 if not Size_Known (Ctyp) then
1166 return False;
1167 end if;
1169 -- Accumulate packed size if possible
1171 if Packed_Size_Known then
1173 -- We can deal with elementary types, small packed arrays
1174 -- if the representation is a modular type and also small
1175 -- record types as checked by Set_Small_Size.
1177 if Is_Elementary_Type (Ctyp)
1178 or else (Is_Array_Type (Ctyp)
1179 and then Present
1180 (Packed_Array_Impl_Type (Ctyp))
1181 and then Is_Modular_Integer_Type
1182 (Packed_Array_Impl_Type (Ctyp)))
1183 or else Is_Record_Type (Ctyp)
1184 then
1185 -- If RM_Size is known and static, then we can keep
1186 -- accumulating the packed size.
1188 if Known_Static_RM_Size (Ctyp) then
1190 Packed_Size := Packed_Size + RM_Size (Ctyp);
1192 -- If we have a field whose RM_Size is not known then
1193 -- we can't figure out the packed size here.
1195 else
1196 Packed_Size_Known := False;
1197 end if;
1199 -- For other types we can't figure out the packed size
1201 else
1202 Packed_Size_Known := False;
1203 end if;
1204 end if;
1206 Next_Component_Or_Discriminant (Comp);
1207 end loop;
1209 if Packed_Size_Known then
1210 Set_Small_Size (T, Packed_Size);
1211 end if;
1213 return True;
1214 end;
1216 -- All other cases, size not known at compile time
1218 else
1219 return False;
1220 end if;
1221 end Size_Known;
1223 -------------------------------------
1224 -- Static_Discriminated_Components --
1225 -------------------------------------
1227 function Static_Discriminated_Components
1228 (T : Entity_Id) return Boolean
1230 Constraint : Elmt_Id;
1232 begin
1233 if Has_Discriminants (T)
1234 and then Present (Discriminant_Constraint (T))
1235 and then Present (First_Component (T))
1236 then
1237 Constraint := First_Elmt (Discriminant_Constraint (T));
1238 while Present (Constraint) loop
1239 if not Compile_Time_Known_Value (Node (Constraint)) then
1240 return False;
1241 end if;
1243 Next_Elmt (Constraint);
1244 end loop;
1245 end if;
1247 return True;
1248 end Static_Discriminated_Components;
1250 -- Start of processing for Check_Compile_Time_Size
1252 begin
1253 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1254 end Check_Compile_Time_Size;
1256 -----------------------------------
1257 -- Check_Component_Storage_Order --
1258 -----------------------------------
1260 procedure Check_Component_Storage_Order
1261 (Encl_Type : Entity_Id;
1262 Comp : Entity_Id;
1263 ADC : Node_Id;
1264 Comp_ADC_Present : out Boolean)
1266 Comp_Base : Entity_Id;
1267 Comp_ADC : Node_Id;
1268 Encl_Base : Entity_Id;
1269 Err_Node : Node_Id;
1271 Component_Aliased : Boolean;
1273 Comp_Byte_Aligned : Boolean := False;
1274 -- Set for the record case, True if Comp is aligned on byte boundaries
1275 -- (in which case it is allowed to have different storage order).
1277 Comp_SSO_Differs : Boolean;
1278 -- Set True when the component is a nested composite, and it does not
1279 -- have the same scalar storage order as Encl_Type.
1281 begin
1282 -- Record case
1284 if Present (Comp) then
1285 Err_Node := Comp;
1286 Comp_Base := Etype (Comp);
1288 if Is_Tag (Comp) then
1289 Comp_Byte_Aligned := True;
1290 Component_Aliased := False;
1292 else
1293 -- If a component clause is present, check if the component starts
1294 -- and ends on byte boundaries. Otherwise conservatively assume it
1295 -- does so only in the case where the record is not packed.
1297 if Present (Component_Clause (Comp)) then
1298 Comp_Byte_Aligned :=
1299 Known_Normalized_First_Bit (Comp)
1300 and then
1301 Known_Esize (Comp)
1302 and then
1303 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0
1304 and then
1305 Esize (Comp) mod System_Storage_Unit = 0;
1306 else
1307 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1308 end if;
1310 Component_Aliased := Is_Aliased (Comp);
1311 end if;
1313 -- Array case
1315 else
1316 Err_Node := Encl_Type;
1317 Comp_Base := Component_Type (Encl_Type);
1319 Component_Aliased := Has_Aliased_Components (Encl_Type);
1320 end if;
1322 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1323 -- the attribute definition clause is attached to the first subtype.
1324 -- Also, if the base type is incomplete or private, go to full view
1325 -- if known
1327 Encl_Base := Base_Type (Encl_Type);
1328 if Present (Underlying_Type (Encl_Base)) then
1329 Encl_Base := Underlying_Type (Encl_Base);
1330 end if;
1332 Comp_Base := Base_Type (Comp_Base);
1333 if Present (Underlying_Type (Comp_Base)) then
1334 Comp_Base := Underlying_Type (Comp_Base);
1335 end if;
1337 Comp_ADC :=
1338 Get_Attribute_Definition_Clause
1339 (First_Subtype (Comp_Base), Attribute_Scalar_Storage_Order);
1340 Comp_ADC_Present := Present (Comp_ADC);
1342 -- Case of record or array component: check storage order compatibility.
1343 -- But, if the record has Complex_Representation, then it is treated as
1344 -- a scalar in the back end so the storage order is irrelevant.
1346 if (Is_Record_Type (Comp_Base)
1347 and then not Has_Complex_Representation (Comp_Base))
1348 or else Is_Array_Type (Comp_Base)
1349 then
1350 Comp_SSO_Differs :=
1351 Reverse_Storage_Order (Encl_Base) /=
1352 Reverse_Storage_Order (Comp_Base);
1354 -- Parent and extension must have same storage order
1356 if Present (Comp) and then Chars (Comp) = Name_uParent then
1357 if Comp_SSO_Differs then
1358 Error_Msg_N
1359 ("record extension must have same scalar storage order as "
1360 & "parent", Err_Node);
1361 end if;
1363 -- If component and composite SSO differs, check that component
1364 -- falls on byte boundaries and isn't bit packed.
1366 elsif Comp_SSO_Differs then
1368 -- Component SSO differs from enclosing composite:
1370 -- Reject if composite is a bit-packed array, as it is rewritten
1371 -- into an array of scalars.
1373 if Is_Bit_Packed_Array (Encl_Base) then
1374 Error_Msg_N
1375 ("type of packed array must have same scalar storage order "
1376 & "as component", Err_Node);
1378 -- Reject if not byte aligned
1380 elsif Is_Record_Type (Encl_Base)
1381 and then not Comp_Byte_Aligned
1382 then
1383 if Present (Component_Clause (Comp)) then
1384 Error_Msg_N
1385 ("type of non-byte-aligned component must have same scalar"
1386 & " storage order as enclosing record", Err_Node);
1387 else
1388 Error_Msg_N
1389 ("type of packed component must have same scalar"
1390 & " storage order as enclosing record", Err_Node);
1391 end if;
1393 -- Warn if specified only for the outer composite
1395 elsif Present (ADC) and then No (Comp_ADC) then
1396 Error_Msg_NE
1397 ("scalar storage order specified for & does not apply to "
1398 & "component?", Err_Node, Encl_Base);
1399 end if;
1400 end if;
1402 -- Enclosing type has explicit SSO: non-composite component must not
1403 -- be aliased.
1405 elsif Present (ADC) and then Component_Aliased then
1406 Error_Msg_N
1407 ("aliased component not permitted for type with explicit "
1408 & "Scalar_Storage_Order", Err_Node);
1409 end if;
1410 end Check_Component_Storage_Order;
1412 -----------------------------
1413 -- Check_Debug_Info_Needed --
1414 -----------------------------
1416 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1417 begin
1418 if Debug_Info_Off (T) then
1419 return;
1421 elsif Comes_From_Source (T)
1422 or else Debug_Generated_Code
1423 or else Debug_Flag_VV
1424 or else Needs_Debug_Info (T)
1425 then
1426 Set_Debug_Info_Needed (T);
1427 end if;
1428 end Check_Debug_Info_Needed;
1430 -------------------------------
1431 -- Check_Expression_Function --
1432 -------------------------------
1434 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1435 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1436 -- Function to search for deferred constant
1438 -------------------
1439 -- Find_Constant --
1440 -------------------
1442 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1443 begin
1444 -- When a constant is initialized with the result of a dispatching
1445 -- call, the constant declaration is rewritten as a renaming of the
1446 -- displaced function result. This scenario is not a premature use of
1447 -- a constant even though the Has_Completion flag is not set.
1449 if Is_Entity_Name (Nod)
1450 and then Present (Entity (Nod))
1451 and then Ekind (Entity (Nod)) = E_Constant
1452 and then Scope (Entity (Nod)) = Current_Scope
1453 and then Nkind (Declaration_Node (Entity (Nod))) =
1454 N_Object_Declaration
1455 and then not Is_Imported (Entity (Nod))
1456 and then not Has_Completion (Entity (Nod))
1457 and then not (Present (Full_View (Entity (Nod)))
1458 and then Has_Completion (Full_View (Entity (Nod))))
1459 then
1460 Error_Msg_NE
1461 ("premature use of& in call or instance", N, Entity (Nod));
1463 elsif Nkind (Nod) = N_Attribute_Reference then
1464 Analyze (Prefix (Nod));
1466 if Is_Entity_Name (Prefix (Nod))
1467 and then Is_Type (Entity (Prefix (Nod)))
1468 then
1469 if Expander_Active then
1470 Check_Fully_Declared (Entity (Prefix (Nod)), N);
1471 end if;
1473 Freeze_Before (N, Entity (Prefix (Nod)));
1474 end if;
1475 end if;
1477 return OK;
1478 end Find_Constant;
1480 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1482 -- Local variables
1484 Decl : Node_Id;
1486 -- Start of processing for Check_Expression_Function
1488 begin
1489 Decl := Original_Node (Unit_Declaration_Node (Nam));
1491 -- The subprogram body created for the expression function is not
1492 -- itself a freeze point.
1494 if Scope (Nam) = Current_Scope
1495 and then Nkind (Decl) = N_Expression_Function
1496 and then Nkind (N) /= N_Subprogram_Body
1497 then
1498 Check_Deferred (Expression (Decl));
1499 end if;
1500 end Check_Expression_Function;
1502 --------------------------------
1503 -- Check_Inherited_Conditions --
1504 --------------------------------
1506 procedure Check_Inherited_Conditions
1507 (R : Entity_Id;
1508 Late_Overriding : Boolean := False)
1510 Prim_Ops : constant Elist_Id := Primitive_Operations (R);
1511 Decls : List_Id;
1512 Op_Node : Elmt_Id;
1513 Par_Prim : Entity_Id;
1514 Prim : Entity_Id;
1515 Wrapper_Needed : Boolean;
1517 function Build_DTW_Body
1518 (Loc : Source_Ptr;
1519 DTW_Spec : Node_Id;
1520 DTW_Decls : List_Id;
1521 Par_Prim : Entity_Id;
1522 Wrapped_Subp : Entity_Id) return Node_Id;
1523 -- Build the body of the dispatch table wrapper containing the given
1524 -- spec and declarations; the call to the wrapped subprogram includes
1525 -- the proper type conversion.
1527 function Build_DTW_Spec (Par_Prim : Entity_Id) return Node_Id;
1528 -- Build the spec of the dispatch table wrapper
1530 procedure Build_Inherited_Condition_Pragmas
1531 (Subp : Entity_Id;
1532 Wrapper_Needed : out Boolean);
1533 -- Build corresponding pragmas for an operation whose ancestor has
1534 -- class-wide pre/postconditions. If the operation is inherited then
1535 -- Wrapper_Needed is returned True to force the creation of a wrapper
1536 -- for the inherited operation. If the ancestor is being overridden,
1537 -- the pragmas are constructed only to verify their legality, in case
1538 -- they contain calls to other primitives that may have been overridden.
1540 function Needs_Wrapper
1541 (Class_Cond : Node_Id;
1542 Subp : Entity_Id;
1543 Par_Subp : Entity_Id) return Boolean;
1544 -- Checks whether the dispatch-table wrapper (DTW) for Subp must be
1545 -- built to evaluate the given class-wide condition.
1547 --------------------
1548 -- Build_DTW_Body --
1549 --------------------
1551 function Build_DTW_Body
1552 (Loc : Source_Ptr;
1553 DTW_Spec : Node_Id;
1554 DTW_Decls : List_Id;
1555 Par_Prim : Entity_Id;
1556 Wrapped_Subp : Entity_Id) return Node_Id
1558 Par_Typ : constant Entity_Id := Find_Dispatching_Type (Par_Prim);
1559 Actuals : constant List_Id := Empty_List;
1560 Call : Node_Id;
1561 Formal : Entity_Id := First_Formal (Par_Prim);
1562 New_F_Spec : Entity_Id := First (Parameter_Specifications (DTW_Spec));
1563 New_Formal : Entity_Id;
1565 begin
1566 -- Build parameter association for call to wrapped subprogram
1568 while Present (Formal) loop
1569 New_Formal := Defining_Identifier (New_F_Spec);
1571 -- If the controlling argument is inherited, add conversion to
1572 -- parent type for the call.
1574 if Etype (Formal) = Par_Typ
1575 and then Is_Controlling_Formal (Formal)
1576 then
1577 Append_To (Actuals,
1578 Make_Type_Conversion (Loc,
1579 New_Occurrence_Of (Par_Typ, Loc),
1580 New_Occurrence_Of (New_Formal, Loc)));
1581 else
1582 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1583 end if;
1585 Next_Formal (Formal);
1586 Next (New_F_Spec);
1587 end loop;
1589 if Ekind (Wrapped_Subp) = E_Procedure then
1590 Call :=
1591 Make_Procedure_Call_Statement (Loc,
1592 Name => New_Occurrence_Of (Wrapped_Subp, Loc),
1593 Parameter_Associations => Actuals);
1594 else
1595 Call :=
1596 Make_Simple_Return_Statement (Loc,
1597 Expression =>
1598 Make_Function_Call (Loc,
1599 Name => New_Occurrence_Of (Wrapped_Subp, Loc),
1600 Parameter_Associations => Actuals));
1601 end if;
1603 return
1604 Make_Subprogram_Body (Loc,
1605 Specification => Copy_Subprogram_Spec (DTW_Spec),
1606 Declarations => DTW_Decls,
1607 Handled_Statement_Sequence =>
1608 Make_Handled_Sequence_Of_Statements (Loc,
1609 Statements => New_List (Call),
1610 End_Label => Make_Identifier (Loc,
1611 Chars (Defining_Entity (DTW_Spec)))));
1612 end Build_DTW_Body;
1614 --------------------
1615 -- Build_DTW_Spec --
1616 --------------------
1618 function Build_DTW_Spec (Par_Prim : Entity_Id) return Node_Id is
1619 DTW_Id : Entity_Id;
1620 DTW_Spec : Node_Id;
1622 begin
1623 DTW_Spec := Build_Overriding_Spec (Par_Prim, R);
1624 DTW_Id := Defining_Entity (DTW_Spec);
1626 -- Clear the not-overriding indicator since the DTW wrapper overrides
1627 -- its wrapped subprogram; required because if present in the parent
1628 -- primitive, given that Build_Overriding_Spec inherits it, we report
1629 -- spurious errors.
1631 Set_Must_Not_Override (DTW_Spec, False);
1633 -- Add minimal decoration of fields
1635 Mutate_Ekind (DTW_Id, Ekind (Par_Prim));
1636 Set_LSP_Subprogram (DTW_Id, Par_Prim);
1637 Set_Is_Dispatch_Table_Wrapper (DTW_Id);
1638 Set_Is_Wrapper (DTW_Id);
1640 -- The DTW wrapper is never a null procedure
1642 if Nkind (DTW_Spec) = N_Procedure_Specification then
1643 Set_Null_Present (DTW_Spec, False);
1644 end if;
1646 return DTW_Spec;
1647 end Build_DTW_Spec;
1649 ---------------------------------------
1650 -- Build_Inherited_Condition_Pragmas --
1651 ---------------------------------------
1653 procedure Build_Inherited_Condition_Pragmas
1654 (Subp : Entity_Id;
1655 Wrapper_Needed : out Boolean)
1657 Class_Pre : constant Node_Id :=
1658 Class_Preconditions (Ultimate_Alias (Subp));
1659 Class_Post : Node_Id := Class_Postconditions (Par_Prim);
1660 A_Post : Node_Id;
1661 New_Prag : Node_Id;
1663 begin
1664 Wrapper_Needed := False;
1666 if No (Class_Pre) and then No (Class_Post) then
1667 return;
1668 end if;
1670 -- For class-wide preconditions we just evaluate whether the wrapper
1671 -- is needed; there is no need to build the pragma since the check
1672 -- is performed on the caller side.
1674 if Present (Class_Pre)
1675 and then Needs_Wrapper (Class_Pre, Subp, Par_Prim)
1676 then
1677 Wrapper_Needed := True;
1678 end if;
1680 -- For class-wide postconditions we evaluate whether the wrapper is
1681 -- needed and we build the class-wide postcondition pragma to install
1682 -- it in the wrapper.
1684 if Present (Class_Post)
1685 and then Needs_Wrapper (Class_Post, Subp, Par_Prim)
1686 then
1687 Wrapper_Needed := True;
1689 -- Update the class-wide postcondition
1691 Class_Post := New_Copy_Tree (Class_Post);
1692 Build_Class_Wide_Expression
1693 (Pragma_Or_Expr => Class_Post,
1694 Subp => Subp,
1695 Par_Subp => Par_Prim,
1696 Adjust_Sloc => False);
1698 -- Install the updated class-wide postcondition in a copy of the
1699 -- pragma postcondition defined for the nearest ancestor.
1701 A_Post := Get_Class_Wide_Pragma (Par_Prim,
1702 Pragma_Postcondition);
1704 if No (A_Post) then
1705 declare
1706 Subps : constant Subprogram_List :=
1707 Inherited_Subprograms (Subp);
1708 begin
1709 for Index in Subps'Range loop
1710 A_Post := Get_Class_Wide_Pragma (Subps (Index),
1711 Pragma_Postcondition);
1712 exit when Present (A_Post);
1713 end loop;
1714 end;
1715 end if;
1717 -- A_Post can be null here if the postcondition was inlined in the
1718 -- called subprogram.
1720 if Present (A_Post) then
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 if;
1728 end Build_Inherited_Condition_Pragmas;
1730 -------------------
1731 -- Needs_Wrapper --
1732 -------------------
1734 function Needs_Wrapper
1735 (Class_Cond : Node_Id;
1736 Subp : Entity_Id;
1737 Par_Subp : Entity_Id) return Boolean
1739 Result : Boolean := False;
1741 function Check_Entity (N : Node_Id) return Traverse_Result;
1742 -- Check calls to overridden primitives
1744 --------------------
1745 -- Replace_Entity --
1746 --------------------
1748 function Check_Entity (N : Node_Id) return Traverse_Result is
1749 New_E : Entity_Id;
1751 begin
1752 if Nkind (N) = N_Identifier
1753 and then Present (Entity (N))
1754 and then
1755 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1756 and then
1757 (Nkind (Parent (N)) /= N_Attribute_Reference
1758 or else Attribute_Name (Parent (N)) /= Name_Class)
1759 then
1760 -- Determine whether entity has a renaming
1762 New_E := Get_Mapped_Entity (Entity (N));
1764 -- If the entity is an overridden primitive and we are not
1765 -- in GNATprove mode, we must build a wrapper for the current
1766 -- inherited operation. If the reference is the prefix of an
1767 -- attribute such as 'Result (or others ???) there is no need
1768 -- for a wrapper: the condition is just rewritten in terms of
1769 -- the inherited subprogram.
1771 if Present (New_E)
1772 and then Comes_From_Source (New_E)
1773 and then Is_Subprogram (New_E)
1774 and then Nkind (Parent (N)) /= N_Attribute_Reference
1775 and then not GNATprove_Mode
1776 then
1777 Result := True;
1778 return Abandon;
1779 end if;
1780 end if;
1782 return OK;
1783 end Check_Entity;
1785 procedure Check_Condition_Entities is
1786 new Traverse_Proc (Check_Entity);
1788 -- Start of processing for Needs_Wrapper
1790 begin
1791 Update_Primitives_Mapping (Par_Subp, Subp);
1793 Map_Formals (Par_Subp, Subp);
1794 Check_Condition_Entities (Class_Cond);
1796 return Result;
1797 end Needs_Wrapper;
1799 Ifaces_List : Elist_Id := No_Elist;
1800 Ifaces_Listed : Boolean := False;
1801 -- Cache the list of interface operations inherited by R
1803 Wrappers_List : Elist_Id := No_Elist;
1804 -- List containing identifiers of built wrappers. Used to defer building
1805 -- and analyzing their class-wide precondition subprograms.
1807 -- Start of processing for Check_Inherited_Conditions
1809 begin
1810 if Late_Overriding then
1811 Op_Node := First_Elmt (Prim_Ops);
1812 while Present (Op_Node) loop
1813 Prim := Node (Op_Node);
1815 -- Map the overridden primitive to the overriding one
1817 if Present (Overridden_Operation (Prim))
1818 and then Comes_From_Source (Prim)
1819 then
1820 Par_Prim := Overridden_Operation (Prim);
1821 Update_Primitives_Mapping (Par_Prim, Prim);
1823 -- Force discarding previous mappings of its formals
1825 Map_Formals (Par_Prim, Prim, Force_Update => True);
1826 end if;
1828 Next_Elmt (Op_Node);
1829 end loop;
1830 end if;
1832 -- Perform validity checks on the inherited conditions of overriding
1833 -- operations, for conformance with LSP, and apply SPARK-specific
1834 -- restrictions on inherited conditions.
1836 Op_Node := First_Elmt (Prim_Ops);
1837 while Present (Op_Node) loop
1838 Prim := Node (Op_Node);
1840 Par_Prim := Overridden_Operation (Prim);
1841 if Present (Par_Prim)
1842 and then Comes_From_Source (Prim)
1843 then
1844 -- When the primitive is an LSP wrapper we climb to the parent
1845 -- primitive that has the inherited contract.
1847 if Is_Wrapper (Par_Prim)
1848 and then Present (LSP_Subprogram (Par_Prim))
1849 then
1850 Par_Prim := LSP_Subprogram (Par_Prim);
1851 end if;
1853 -- Check that overrider and overridden operations have
1854 -- the same strub mode.
1856 Check_Same_Strub_Mode (Prim, Par_Prim);
1858 -- Analyze the contract items of the overridden operation, before
1859 -- they are rewritten as pragmas.
1861 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1863 -- In GNATprove mode this is where we can collect the inherited
1864 -- conditions, because we do not create the Check pragmas that
1865 -- normally convey the modified class-wide conditions on
1866 -- overriding operations.
1868 if GNATprove_Mode then
1869 Collect_Inherited_Class_Wide_Conditions (Prim);
1870 end if;
1871 end if;
1873 -- Go over operations inherited from interfaces and check
1874 -- them for strub mode compatibility as well.
1876 if Has_Interfaces (R)
1877 and then Is_Dispatching_Operation (Prim)
1878 and then Find_Dispatching_Type (Prim) = R
1879 then
1880 declare
1881 Elmt : Elmt_Id;
1882 Iface_Elmt : Elmt_Id;
1883 Iface : Entity_Id;
1884 Iface_Prim : Entity_Id;
1886 begin
1887 -- Collect the interfaces only once. We haven't
1888 -- finished freezing yet, so we can't use the faster
1889 -- search from Sem_Disp.Covered_Interface_Primitives.
1891 if not Ifaces_Listed then
1892 Collect_Interfaces (R, Ifaces_List);
1893 Ifaces_Listed := True;
1894 end if;
1896 Iface_Elmt := First_Elmt (Ifaces_List);
1897 while Present (Iface_Elmt) loop
1898 Iface := Node (Iface_Elmt);
1900 Elmt := First_Elmt (Primitive_Operations (Iface));
1901 while Present (Elmt) loop
1902 Iface_Prim := Node (Elmt);
1904 if Iface_Prim /= Par_Prim
1905 and then Chars (Iface_Prim) = Chars (Prim)
1906 and then Comes_From_Source (Iface_Prim)
1907 and then (Is_Interface_Conformant
1908 (R, Iface_Prim, Prim))
1909 then
1910 Check_Same_Strub_Mode (Prim, Iface_Prim);
1911 end if;
1913 Next_Elmt (Elmt);
1914 end loop;
1916 Next_Elmt (Iface_Elmt);
1917 end loop;
1918 end;
1919 end if;
1921 Next_Elmt (Op_Node);
1922 end loop;
1924 -- Now examine the inherited operations to check whether they require
1925 -- a wrapper to handle inherited conditions that call other primitives,
1926 -- so that LSP can be verified/enforced.
1928 Op_Node := First_Elmt (Prim_Ops);
1930 while Present (Op_Node) loop
1931 Decls := Empty_List;
1932 Prim := Node (Op_Node);
1933 Wrapper_Needed := False;
1935 -- Skip internal entities built for mapping interface primitives
1937 if not Comes_From_Source (Prim)
1938 and then Present (Alias (Prim))
1939 and then No (Interface_Alias (Prim))
1940 then
1941 Par_Prim := Ultimate_Alias (Prim);
1943 -- When the primitive is an LSP wrapper we climb to the parent
1944 -- primitive that has the inherited contract.
1946 if Is_Wrapper (Par_Prim)
1947 and then Present (LSP_Subprogram (Par_Prim))
1948 then
1949 Par_Prim := LSP_Subprogram (Par_Prim);
1950 end if;
1952 -- Analyze the contract items of the parent operation, and
1953 -- determine whether a wrapper is needed. This is determined
1954 -- when the condition is rewritten in sem_prag, using the
1955 -- mapping between overridden and overriding operations built
1956 -- in the loop above.
1958 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1959 Build_Inherited_Condition_Pragmas (Prim, Wrapper_Needed);
1960 end if;
1962 if Wrapper_Needed
1963 and then not Is_Abstract_Subprogram (Par_Prim)
1964 and then Expander_Active
1965 then
1966 -- Build the dispatch-table wrapper (DTW). The support for
1967 -- AI12-0195 relies on two kind of wrappers: one for indirect
1968 -- calls (also used for AI12-0220), and one for putting in the
1969 -- dispatch table:
1971 -- 1) "indirect-call wrapper" (ICW) is needed anytime there are
1972 -- class-wide preconditions. Prim'Access will point directly
1973 -- at the ICW if any, or at the "pristine" body if Prim has
1974 -- no class-wide preconditions.
1976 -- 2) "dispatch-table wrapper" (DTW) is needed anytime the class
1977 -- wide preconditions *or* the class-wide postconditions are
1978 -- affected by overriding.
1980 -- The DTW holds a single statement that is a single call where
1981 -- the controlling actuals are conversions to the corresponding
1982 -- type in the parent primitive. If the primitive is a function
1983 -- the statement is a return statement with a call.
1985 declare
1986 Alias_Id : constant Entity_Id := Ultimate_Alias (Prim);
1987 Loc : constant Source_Ptr := Sloc (R);
1988 DTW_Body : Node_Id;
1989 DTW_Decl : Node_Id;
1990 DTW_Id : Entity_Id;
1991 DTW_Spec : Node_Id;
1993 Prim_Next_E : constant Entity_Id := Next_Entity (Prim);
1994 Prim_Prev_E : constant Entity_Id := Prev_Entity (Prim);
1996 begin
1997 DTW_Spec := Build_DTW_Spec (Par_Prim);
1998 DTW_Id := Defining_Entity (DTW_Spec);
1999 DTW_Decl := Make_Subprogram_Declaration (Loc,
2000 Specification => DTW_Spec);
2002 -- The spec of the wrapper has been built using the source
2003 -- location of its parent primitive; we must update it now
2004 -- (with the source location of the internal primitive built
2005 -- by Derive_Subprogram that will override this wrapper) to
2006 -- avoid inlining conflicts between internally built helpers
2007 -- for class-wide pre/postconditions of the parent and the
2008 -- helpers built for this wrapper.
2010 Set_Sloc (DTW_Id, Sloc (Prim));
2012 -- For inherited class-wide preconditions the DTW wrapper
2013 -- reuses the ICW of the parent (which checks the parent
2014 -- interpretation of the class-wide preconditions); the
2015 -- interpretation of the class-wide preconditions for the
2016 -- inherited subprogram is checked at the caller side.
2018 -- When the subprogram inherits class-wide postconditions
2019 -- the DTW also checks the interpretation of the class-wide
2020 -- postconditions for the inherited subprogram, and the body
2021 -- of the parent checks its interpretation of the parent for
2022 -- the class-wide postconditions.
2024 -- procedure Prim (F1 : T1; ...) is
2025 -- [ pragma Check (Postcondition, Expr); ]
2026 -- begin
2027 -- Par_Prim_ICW (Par_Type (F1), ...);
2028 -- end;
2030 if Present (Indirect_Call_Wrapper (Par_Prim)) then
2031 DTW_Body :=
2032 Build_DTW_Body (Loc,
2033 DTW_Spec => DTW_Spec,
2034 DTW_Decls => Decls,
2035 Par_Prim => Par_Prim,
2036 Wrapped_Subp => Indirect_Call_Wrapper (Par_Prim));
2038 -- For subprograms that only inherit class-wide postconditions
2039 -- the DTW wrapper calls the parent primitive (which on its
2040 -- body checks the interpretation of the class-wide post-
2041 -- conditions for the parent subprogram), and the DTW checks
2042 -- the interpretation of the class-wide postconditions for the
2043 -- inherited subprogram.
2045 -- procedure Prim (F1 : T1; ...) is
2046 -- pragma Check (Postcondition, Expr);
2047 -- begin
2048 -- Par_Prim (Par_Type (F1), ...);
2049 -- end;
2051 else
2052 DTW_Body :=
2053 Build_DTW_Body (Loc,
2054 DTW_Spec => DTW_Spec,
2055 DTW_Decls => Decls,
2056 Par_Prim => Par_Prim,
2057 Wrapped_Subp => Par_Prim);
2058 end if;
2060 -- Insert the declaration of the wrapper before the freezing
2061 -- node of the record type declaration to ensure that it will
2062 -- override the internal primitive built by Derive_Subprogram.
2064 if Late_Overriding then
2065 Ensure_Freeze_Node (R);
2066 Insert_Before_And_Analyze (Freeze_Node (R), DTW_Decl);
2067 else
2068 Append_Freeze_Action (R, DTW_Decl);
2069 Analyze (DTW_Decl);
2070 end if;
2072 -- The analyis of DTW_Decl has removed Prim from its scope
2073 -- chain and added DTW_Id at the end of the scope chain. Move
2074 -- DTW_Id to its correct place in the scope chain: the analysis
2075 -- of the wrapper declaration has just added DTW_Id at the end
2076 -- of the list of entities of its scope. However, given that
2077 -- this wrapper overrides Prim, we must move DTW_Id to the
2078 -- original place of Prim in its scope chain. This is required
2079 -- for wrappers of private type primitives to ensure their
2080 -- correct visibility since wrappers are built when the full
2081 -- tagged type declaration is frozen (in the private part of
2082 -- the package) but they may override primitives defined in the
2083 -- public part of the package.
2085 declare
2086 DTW_Prev_E : constant Entity_Id := Prev_Entity (DTW_Id);
2088 begin
2089 pragma Assert (Last_Entity (Current_Scope) = DTW_Id);
2090 pragma Assert
2091 (Ekind (Current_Scope) not in E_Package | E_Generic_Package
2092 or else No (First_Private_Entity (Current_Scope))
2093 or else First_Private_Entity (Current_Scope) /= DTW_Id);
2095 -- Remove DTW_Id from the end of the doubly-linked list of
2096 -- entities of this scope; no need to handle removing it
2097 -- from the beginning of the chain since such case can never
2098 -- occur for this entity.
2100 Set_Last_Entity (Current_Scope, DTW_Prev_E);
2101 Set_Next_Entity (DTW_Prev_E, Empty);
2103 -- Place DTW_Id back in the original place of its wrapped
2104 -- primitive in the list of entities of this scope.
2106 Link_Entities (Prim_Prev_E, DTW_Id);
2107 Link_Entities (DTW_Id, Prim_Next_E);
2108 end;
2110 -- Insert the body of the wrapper in the freeze actions of
2111 -- its record type declaration to ensure that it is placed
2112 -- in the scope of its declaration but not too early to cause
2113 -- premature freezing of other entities.
2115 Append_Freeze_Action (R, DTW_Body);
2116 Analyze (DTW_Body);
2118 -- Ensure correct decoration
2120 pragma Assert (Is_Dispatching_Operation (DTW_Id));
2121 pragma Assert (Present (Overridden_Operation (DTW_Id)));
2122 pragma Assert (Overridden_Operation (DTW_Id) = Alias_Id);
2124 -- Inherit dispatch table slot
2126 Set_DTC_Entity_Value (R, DTW_Id);
2127 Set_DT_Position (DTW_Id, DT_Position (Alias_Id));
2129 -- Register the wrapper in the dispatch table
2131 if Late_Overriding
2132 and then not Building_Static_DT (R)
2133 then
2134 Insert_List_After_And_Analyze (Freeze_Node (R),
2135 Register_Primitive (Loc, DTW_Id));
2136 end if;
2138 -- Defer building helpers and ICW for the DTW. Required to
2139 -- ensure uniqueness in their names because when building
2140 -- these wrappers for overlapped subprograms their homonym
2141 -- number is not definite until all these dispatch table
2142 -- wrappers of tagged type R have been analyzed.
2144 if Present (Indirect_Call_Wrapper (Par_Prim)) then
2145 Append_New_Elmt (DTW_Id, Wrappers_List);
2146 end if;
2147 end;
2148 end if;
2150 Next_Elmt (Op_Node);
2151 end loop;
2153 -- Build and analyze deferred class-wide precondition subprograms of
2154 -- built wrappers.
2156 if Present (Wrappers_List) then
2157 declare
2158 Body_N : Node_Id;
2159 CW_Subp : Entity_Id;
2160 Decl_N : Node_Id;
2161 DTW_Id : Entity_Id;
2162 Elmt : Elmt_Id;
2164 begin
2165 Elmt := First_Elmt (Wrappers_List);
2167 while Present (Elmt) loop
2168 DTW_Id := Node (Elmt);
2169 Next_Elmt (Elmt);
2171 Merge_Class_Conditions (DTW_Id);
2172 Make_Class_Precondition_Subps (DTW_Id, Late_Overriding);
2174 CW_Subp := Static_Call_Helper (DTW_Id);
2175 Decl_N := Unit_Declaration_Node (CW_Subp);
2176 Analyze (Decl_N);
2178 -- If the DTW was built for a late-overriding primitive
2179 -- its body must be analyzed now (since the tagged type
2180 -- is already frozen).
2182 if Late_Overriding then
2183 Body_N :=
2184 Unit_Declaration_Node (Corresponding_Body (Decl_N));
2185 Analyze (Body_N);
2186 end if;
2187 end loop;
2188 end;
2189 end if;
2190 end Check_Inherited_Conditions;
2192 ----------------------------
2193 -- Check_Strict_Alignment --
2194 ----------------------------
2196 procedure Check_Strict_Alignment (E : Entity_Id) is
2197 Comp : Entity_Id;
2199 begin
2200 -- Bit-packed array types do not require strict alignment, even if they
2201 -- are by-reference types, because they are accessed in a special way.
2203 if Is_By_Reference_Type (E) and then not Is_Bit_Packed_Array (E) then
2204 Set_Strict_Alignment (E);
2206 elsif Is_Array_Type (E) then
2207 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
2209 -- ??? AI12-001: Any component of a packed type that contains an
2210 -- aliased part must be aligned according to the alignment of its
2211 -- subtype (RM 13.2(7)). This means that the following test:
2213 -- if Has_Aliased_Components (E) then
2214 -- Set_Strict_Alignment (E);
2215 -- end if;
2217 -- should be implemented here. Unfortunately it would break Florist,
2218 -- which has the bad habit of overaligning all the types it declares
2219 -- on 32-bit platforms. Other legacy codebases could also be affected
2220 -- because this check has historically been missing in GNAT.
2222 elsif Is_Record_Type (E) then
2223 Comp := First_Component (E);
2224 while Present (Comp) loop
2225 if not Is_Type (Comp)
2226 and then (Is_Aliased (Comp)
2227 or else Strict_Alignment (Etype (Comp)))
2228 then
2229 Set_Strict_Alignment (E);
2230 return;
2231 end if;
2233 Next_Component (Comp);
2234 end loop;
2235 end if;
2236 end Check_Strict_Alignment;
2238 -------------------------
2239 -- Check_Unsigned_Type --
2240 -------------------------
2242 procedure Check_Unsigned_Type (E : Entity_Id) is
2243 Ancestor : Entity_Id;
2244 Lo_Bound : Node_Id;
2245 Btyp : Entity_Id;
2247 begin
2248 if not Is_Discrete_Or_Fixed_Point_Type (E) then
2249 return;
2250 end if;
2252 -- Do not attempt to analyze case where range was in error
2254 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
2255 return;
2256 end if;
2258 -- The situation that is nontrivial is something like:
2260 -- subtype x1 is integer range -10 .. +10;
2261 -- subtype x2 is x1 range 0 .. V1;
2262 -- subtype x3 is x2 range V2 .. V3;
2263 -- subtype x4 is x3 range V4 .. V5;
2265 -- where Vn are variables. Here the base type is signed, but we still
2266 -- know that x4 is unsigned because of the lower bound of x2.
2268 -- The only way to deal with this is to look up the ancestor chain
2270 Ancestor := E;
2271 loop
2272 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
2273 return;
2274 end if;
2276 Lo_Bound := Type_Low_Bound (Ancestor);
2278 if Compile_Time_Known_Value (Lo_Bound) then
2279 if Expr_Rep_Value (Lo_Bound) >= 0 then
2280 Set_Is_Unsigned_Type (E, True);
2281 end if;
2283 return;
2285 else
2286 Ancestor := Ancestor_Subtype (Ancestor);
2288 -- If no ancestor had a static lower bound, go to base type
2290 if No (Ancestor) then
2292 -- Note: the reason we still check for a compile time known
2293 -- value for the base type is that at least in the case of
2294 -- generic formals, we can have bounds that fail this test,
2295 -- and there may be other cases in error situations.
2297 Btyp := Base_Type (E);
2299 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
2300 return;
2301 end if;
2303 Lo_Bound := Type_Low_Bound (Base_Type (E));
2305 if Compile_Time_Known_Value (Lo_Bound)
2306 and then Expr_Rep_Value (Lo_Bound) >= 0
2307 then
2308 Set_Is_Unsigned_Type (E, True);
2309 end if;
2311 return;
2312 end if;
2313 end if;
2314 end loop;
2315 end Check_Unsigned_Type;
2317 -----------------------------------------------
2318 -- Explode_Initialization_Compound_Statement --
2319 -----------------------------------------------
2321 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
2322 Init_Stmts : constant Node_Id := Initialization_Statements (E);
2324 begin
2325 if Present (Init_Stmts)
2326 and then Nkind (Init_Stmts) = N_Compound_Statement
2327 then
2328 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
2330 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
2331 -- just removing it, because Freeze_All may rely on this particular
2332 -- Node_Id still being present in the enclosing list to know where to
2333 -- stop freezing.
2335 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
2337 Set_Initialization_Statements (E, Empty);
2338 end if;
2339 end Explode_Initialization_Compound_Statement;
2341 ----------------
2342 -- Freeze_All --
2343 ----------------
2345 -- Note: the easy coding for this procedure would be to just build a
2346 -- single list of freeze nodes and then insert them and analyze them
2347 -- all at once. This won't work, because the analysis of earlier freeze
2348 -- nodes may recursively freeze types which would otherwise appear later
2349 -- on in the freeze list. So we must analyze and expand the freeze nodes
2350 -- as they are generated.
2352 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
2353 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
2354 -- This is the internal recursive routine that does freezing of entities
2355 -- (but NOT the analysis of default expressions, which should not be
2356 -- recursive, we don't want to analyze those till we are sure that ALL
2357 -- the types are frozen).
2359 --------------------
2360 -- Freeze_All_Ent --
2361 --------------------
2363 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
2364 E : Entity_Id;
2365 Flist : List_Id;
2367 procedure Process_Flist;
2368 -- If freeze nodes are present, insert and analyze, and reset cursor
2369 -- for next insertion.
2371 -------------------
2372 -- Process_Flist --
2373 -------------------
2375 procedure Process_Flist is
2376 Lastn : Node_Id;
2377 begin
2378 if Is_Non_Empty_List (Flist) then
2379 Lastn := Next (After);
2380 Insert_List_After_And_Analyze (After, Flist);
2382 if Present (Lastn) then
2383 After := Prev (Lastn);
2384 else
2385 After := Last (List_Containing (After));
2386 end if;
2387 end if;
2388 end Process_Flist;
2390 -- Start of processing for Freeze_All_Ent
2392 begin
2393 E := From;
2394 while Present (E) loop
2396 -- If the entity is an inner package which is not a package
2397 -- renaming, then its entities must be frozen at this point. Note
2398 -- that such entities do NOT get frozen at the end of the nested
2399 -- package itself (only library packages freeze).
2401 -- Same is true for task declarations, where anonymous records
2402 -- created for entry parameters must be frozen.
2404 if Ekind (E) = E_Package
2405 and then No (Renamed_Entity (E))
2406 and then not Is_Child_Unit (E)
2407 and then not Is_Frozen (E)
2408 then
2409 Push_Scope (E);
2411 Install_Visible_Declarations (E);
2412 Install_Private_Declarations (E);
2413 Freeze_All (First_Entity (E), After);
2415 End_Package_Scope (E);
2417 if Is_Generic_Instance (E)
2418 and then Has_Delayed_Freeze (E)
2419 then
2420 Set_Has_Delayed_Freeze (E, False);
2421 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
2422 end if;
2424 elsif Ekind (E) in Task_Kind
2425 and then Nkind (Parent (E)) in
2426 N_Single_Task_Declaration | N_Task_Type_Declaration
2427 then
2428 Push_Scope (E);
2429 Freeze_All (First_Entity (E), After);
2430 End_Scope;
2432 -- For a derived tagged type, we must ensure that all the
2433 -- primitive operations of the parent have been frozen, so that
2434 -- their addresses will be in the parent's dispatch table at the
2435 -- point it is inherited.
2437 elsif Ekind (E) = E_Record_Type
2438 and then Is_Tagged_Type (E)
2439 and then Is_Tagged_Type (Etype (E))
2440 and then Is_Derived_Type (E)
2441 then
2442 declare
2443 Prim_List : constant Elist_Id :=
2444 Primitive_Operations (Etype (E));
2446 Prim : Elmt_Id;
2447 Subp : Entity_Id;
2449 begin
2450 Prim := First_Elmt (Prim_List);
2451 while Present (Prim) loop
2452 Subp := Node (Prim);
2454 if Comes_From_Source (Subp)
2455 and then not Is_Frozen (Subp)
2456 then
2457 Flist := Freeze_Entity (Subp, After);
2458 Process_Flist;
2459 end if;
2461 Next_Elmt (Prim);
2462 end loop;
2463 end;
2464 end if;
2466 if not Is_Frozen (E) then
2467 Flist := Freeze_Entity (E, After);
2468 Process_Flist;
2470 -- If already frozen, and there are delayed aspects, this is where
2471 -- we do the visibility check for these aspects (see Sem_Ch13 spec
2472 -- for a description of how we handle aspect visibility).
2474 elsif Has_Delayed_Aspects (E) then
2475 declare
2476 Ritem : Node_Id;
2478 begin
2479 Ritem := First_Rep_Item (E);
2480 while Present (Ritem) loop
2481 if Nkind (Ritem) = N_Aspect_Specification
2482 and then Entity (Ritem) = E
2483 and then Is_Delayed_Aspect (Ritem)
2484 then
2485 Check_Aspect_At_End_Of_Declarations (Ritem);
2486 end if;
2488 Next_Rep_Item (Ritem);
2489 end loop;
2490 end;
2491 end if;
2493 -- If an incomplete type is still not frozen, this may be a
2494 -- premature freezing because of a body declaration that follows.
2495 -- Indicate where the freezing took place. Freezing will happen
2496 -- if the body comes from source, but not if it is internally
2497 -- generated, for example as the body of a type invariant.
2499 -- If the freezing is caused by the end of the current declarative
2500 -- part, it is a Taft Amendment type, and there is no error.
2502 if not Is_Frozen (E)
2503 and then Ekind (E) = E_Incomplete_Type
2504 then
2505 declare
2506 Bod : constant Node_Id := Next (After);
2508 begin
2509 -- The presence of a body freezes all entities previously
2510 -- declared in the current list of declarations, but this
2511 -- does not apply if the body does not come from source.
2512 -- A type invariant is transformed into a subprogram body
2513 -- which is placed at the end of the private part of the
2514 -- current package, but this body does not freeze incomplete
2515 -- types that may be declared in this private part.
2517 if Comes_From_Source (Bod)
2518 and then Nkind (Bod) in N_Entry_Body
2519 | N_Package_Body
2520 | N_Protected_Body
2521 | N_Subprogram_Body
2522 | N_Task_Body
2523 | N_Body_Stub
2524 and then
2525 In_Same_List (After, Parent (E))
2526 then
2527 Error_Msg_Sloc := Sloc (Next (After));
2528 Error_Msg_NE
2529 ("type& is frozen# before its full declaration",
2530 Parent (E), E);
2531 end if;
2532 end;
2533 end if;
2535 Next_Entity (E);
2536 end loop;
2537 end Freeze_All_Ent;
2539 -- Local variables
2541 Decl : Node_Id;
2542 E : Entity_Id;
2543 Item : Entity_Id;
2545 -- Start of processing for Freeze_All
2547 begin
2548 Freeze_All_Ent (From, After);
2550 -- Now that all types are frozen, we can deal with default expressions
2551 -- that require us to build a default expression functions. This is the
2552 -- point at which such functions are constructed (after all types that
2553 -- might be used in such expressions have been frozen).
2555 -- For subprograms that are renaming_as_body, we create the wrapper
2556 -- bodies as needed.
2558 -- We also add finalization chains to access types whose designated
2559 -- types are controlled. This is normally done when freezing the type,
2560 -- but this misses recursive type definitions where the later members
2561 -- of the recursion introduce controlled components.
2563 -- Loop through entities
2565 E := From;
2566 while Present (E) loop
2567 if Is_Subprogram (E) then
2568 if not Default_Expressions_Processed (E) then
2569 Process_Default_Expressions (E, After);
2570 end if;
2572 -- Check subprogram renamings for the same strub-mode.
2573 -- Avoid rechecking dispatching operations, that's taken
2574 -- care of in Check_Inherited_Conditions, that covers
2575 -- inherited interface operations.
2577 Item := Alias (E);
2578 if Present (Item)
2579 and then not Is_Dispatching_Operation (E)
2580 then
2581 Check_Same_Strub_Mode (E, Item);
2582 end if;
2584 if not Has_Completion (E) then
2585 Decl := Unit_Declaration_Node (E);
2587 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
2588 if Error_Posted (Decl) then
2589 Set_Has_Completion (E);
2590 else
2591 Build_And_Analyze_Renamed_Body (Decl, E, After);
2592 end if;
2594 elsif Nkind (Decl) = N_Subprogram_Declaration
2595 and then Present (Corresponding_Body (Decl))
2596 and then
2597 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl))) =
2598 N_Subprogram_Renaming_Declaration
2599 then
2600 Build_And_Analyze_Renamed_Body
2601 (Decl, Corresponding_Body (Decl), After);
2602 end if;
2603 end if;
2605 -- Freeze the default expressions of entries, entry families, and
2606 -- protected subprograms.
2608 elsif Is_Concurrent_Type (E) then
2609 Item := First_Entity (E);
2610 while Present (Item) loop
2611 if Is_Subprogram_Or_Entry (Item)
2612 and then not Default_Expressions_Processed (Item)
2613 then
2614 Process_Default_Expressions (Item, After);
2615 end if;
2617 Next_Entity (Item);
2618 end loop;
2619 end if;
2621 -- Historical note: We used to create a finalization master for an
2622 -- access type whose designated type is not controlled, but contains
2623 -- private controlled compoments. This form of postprocessing is no
2624 -- longer needed because the finalization master is now created when
2625 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
2627 Next_Entity (E);
2628 end loop;
2629 end Freeze_All;
2631 -----------------------
2632 -- Freeze_And_Append --
2633 -----------------------
2635 procedure Freeze_And_Append
2636 (Ent : Entity_Id;
2637 N : Node_Id;
2638 Result : in out List_Id)
2640 -- Freezing an Expression_Function does not freeze its profile:
2641 -- the formals will have been frozen otherwise before the E_F
2642 -- can be called.
2644 L : constant List_Id :=
2645 Freeze_Entity
2646 (Ent, N, Do_Freeze_Profile => not Is_Expression_Function (Ent));
2647 begin
2648 if Is_Non_Empty_List (L) then
2649 if Result = No_List then
2650 Result := L;
2651 else
2652 Append_List (L, Result);
2653 end if;
2654 end if;
2655 end Freeze_And_Append;
2657 -------------------
2658 -- Freeze_Before --
2659 -------------------
2661 procedure Freeze_Before
2662 (N : Node_Id;
2663 T : Entity_Id;
2664 Do_Freeze_Profile : Boolean := True)
2666 -- Freeze T, then insert the generated Freeze nodes before the node N.
2667 -- Flag Freeze_Profile is used when T is an overloadable entity, and
2668 -- indicates whether its profile should be frozen at the same time.
2670 Freeze_Nodes : constant List_Id :=
2671 Freeze_Entity (T, N, Do_Freeze_Profile);
2672 Pack : constant Entity_Id := Scope (T);
2674 begin
2675 if Ekind (T) = E_Function then
2676 Check_Expression_Function (N, T);
2677 end if;
2679 if Is_Non_Empty_List (Freeze_Nodes) then
2681 -- If the entity is a type declared in an inner package, it may be
2682 -- frozen by an outer declaration before the package itself is
2683 -- frozen. Install the package scope to analyze the freeze nodes,
2684 -- which may include generated subprograms such as predicate
2685 -- functions, etc.
2687 if Is_Type (T) and then From_Nested_Package (T) then
2688 Push_Scope (Pack);
2689 Install_Visible_Declarations (Pack);
2690 Install_Private_Declarations (Pack);
2691 Insert_Actions (N, Freeze_Nodes);
2692 End_Package_Scope (Pack);
2694 else
2695 Insert_Actions (N, Freeze_Nodes);
2696 end if;
2697 end if;
2698 end Freeze_Before;
2700 -------------------
2701 -- Freeze_Entity --
2702 -------------------
2704 -- WARNING: This routine manages Ghost regions. Return statements must be
2705 -- replaced by gotos which jump to the end of the routine and restore the
2706 -- Ghost mode.
2708 function Freeze_Entity
2709 (E : Entity_Id;
2710 N : Node_Id;
2711 Do_Freeze_Profile : Boolean := True) return List_Id
2713 Loc : constant Source_Ptr := Sloc (N);
2715 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2716 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2717 -- Save the Ghost-related attributes to restore on exit
2719 Atype : Entity_Id;
2720 Comp : Entity_Id;
2721 F_Node : Node_Id;
2722 Formal : Entity_Id;
2723 Indx : Node_Id;
2725 Result : List_Id := No_List;
2726 -- List of freezing actions, left at No_List if none
2728 Test_E : Entity_Id := E;
2729 -- A local temporary used to test if freezing is necessary for E, since
2730 -- its value can be set to something other than E in certain cases. For
2731 -- example, E cannot be used directly in cases such as when it is an
2732 -- Itype defined within a record - since it is the location of record
2733 -- which matters.
2735 procedure Add_To_Result (Fnod : Node_Id);
2736 -- Add freeze action Fnod to list Result
2738 function After_Last_Declaration return Boolean;
2739 -- If Loc is a freeze_entity that appears after the last declaration
2740 -- in the scope, inhibit error messages on late completion.
2742 procedure Check_Current_Instance (Comp_Decl : Node_Id);
2743 -- Check that an Access or Unchecked_Access attribute with a prefix
2744 -- which is the current instance type can only be applied when the type
2745 -- is limited.
2747 procedure Check_No_Parts_Violations
2748 (Typ : Entity_Id; Aspect_No_Parts : Aspect_Id) with
2749 Pre => Aspect_No_Parts in
2750 Aspect_No_Controlled_Parts | Aspect_No_Task_Parts;
2751 -- Check that Typ does not violate the semantics of the specified
2752 -- Aspect_No_Parts (No_Controlled_Parts or No_Task_Parts) when it is
2753 -- specified on Typ or one of its ancestors.
2755 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id);
2756 -- Give a warning for pragma Convention with language C or C++ applied
2757 -- to a discriminated record type. This is suppressed for the unchecked
2758 -- union case, since the whole point in this case is interface C. We
2759 -- also do not generate this within instantiations, since we will have
2760 -- generated a message on the template.
2762 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
2763 -- Give warning for modulus of 8, 16, 32, 64 or 128 given as an explicit
2764 -- integer literal without an explicit corresponding size clause. The
2765 -- caller has checked that Utype is a modular integer type.
2767 procedure Freeze_Array_Type (Arr : Entity_Id);
2768 -- Freeze array type, including freezing index and component types
2770 procedure Freeze_Object_Declaration (E : Entity_Id);
2771 -- Perform checks and generate freeze node if needed for a constant or
2772 -- variable declared by an object declaration.
2774 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
2775 -- Create Freeze_Generic_Entity nodes for types declared in a generic
2776 -- package. Recurse on inner generic packages.
2778 function Freeze_Profile (E : Entity_Id) return Boolean;
2779 -- Freeze formals and return type of subprogram. If some type in the
2780 -- profile is incomplete and we are in an instance, freezing of the
2781 -- entity will take place elsewhere, and the function returns False.
2783 procedure Freeze_Record_Type (Rec : Entity_Id);
2784 -- Freeze record type, including freezing component types, and freezing
2785 -- primitive operations if this is a tagged type.
2787 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2788 -- Determine whether an arbitrary entity is subject to Boolean aspect
2789 -- Import and its value is specified as True.
2791 procedure Inherit_Freeze_Node
2792 (Fnod : Node_Id;
2793 Typ : Entity_Id);
2794 -- Set type Typ's freeze node to refer to Fnode. This routine ensures
2795 -- that any attributes attached to Typ's original node are preserved.
2797 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2798 -- If E is an entity for an imported subprogram with pre/post-conditions
2799 -- then this procedure will create a wrapper to ensure that proper run-
2800 -- time checking of the pre/postconditions. See body for details.
2802 -------------------
2803 -- Add_To_Result --
2804 -------------------
2806 procedure Add_To_Result (Fnod : Node_Id) is
2807 begin
2808 Append_New_To (Result, Fnod);
2809 end Add_To_Result;
2811 ----------------------------
2812 -- After_Last_Declaration --
2813 ----------------------------
2815 function After_Last_Declaration return Boolean is
2816 Spec : constant Node_Id := Parent (Current_Scope);
2818 begin
2819 if Nkind (Spec) = N_Package_Specification then
2820 if Present (Private_Declarations (Spec)) then
2821 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2822 elsif Present (Visible_Declarations (Spec)) then
2823 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2824 else
2825 return False;
2826 end if;
2828 else
2829 return False;
2830 end if;
2831 end After_Last_Declaration;
2833 ----------------------------
2834 -- Check_Current_Instance --
2835 ----------------------------
2837 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2839 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2840 -- Determine whether Typ is compatible with the rules for aliased
2841 -- views of types as defined in RM 3.10 in the various dialects.
2843 function Process (N : Node_Id) return Traverse_Result;
2844 -- Process routine to apply check to given node
2846 -----------------------------
2847 -- Is_Aliased_View_Of_Type --
2848 -----------------------------
2850 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2851 Typ_Decl : constant Node_Id := Parent (Typ);
2853 begin
2854 -- Common case
2856 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2857 and then Limited_Present (Type_Definition (Typ_Decl))
2858 then
2859 return True;
2861 -- The following paragraphs describe what a legal aliased view of
2862 -- a type is in the various dialects of Ada.
2864 -- Ada 95
2866 -- The current instance of a limited type, and a formal parameter
2867 -- or generic formal object of a tagged type.
2869 -- Ada 95 limited type
2870 -- * Type with reserved word "limited"
2871 -- * A protected or task type
2872 -- * A composite type with limited component
2874 elsif Ada_Version <= Ada_95 then
2875 return Is_Limited_Type (Typ);
2877 -- Ada 2005
2879 -- The current instance of a limited tagged type, a protected
2880 -- type, a task type, or a type that has the reserved word
2881 -- "limited" in its full definition ... a formal parameter or
2882 -- generic formal object of a tagged type.
2884 -- Ada 2005 limited type
2885 -- * Type with reserved word "limited", "synchronized", "task"
2886 -- or "protected"
2887 -- * A composite type with limited component
2888 -- * A derived type whose parent is a non-interface limited type
2890 elsif Ada_Version = Ada_2005 then
2891 return
2892 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2893 or else
2894 (Is_Derived_Type (Typ)
2895 and then not Is_Interface (Etype (Typ))
2896 and then Is_Limited_Type (Etype (Typ)));
2898 -- Ada 2012 and beyond
2900 -- The current instance of an immutably limited type ... a formal
2901 -- parameter or generic formal object of a tagged type.
2903 -- Ada 2012 limited type
2904 -- * Type with reserved word "limited", "synchronized", "task"
2905 -- or "protected"
2906 -- * A composite type with limited component
2907 -- * A derived type whose parent is a non-interface limited type
2908 -- * An incomplete view
2910 -- Ada 2012 immutably limited type
2911 -- * Explicitly limited record type
2912 -- * Record extension with "limited" present
2913 -- * Non-formal limited private type that is either tagged
2914 -- or has at least one access discriminant with a default
2915 -- expression
2916 -- * Task type, protected type or synchronized interface
2917 -- * Type derived from immutably limited type
2919 else
2920 return
2921 Is_Immutably_Limited_Type (Typ)
2922 or else Is_Incomplete_Type (Typ);
2923 end if;
2924 end Is_Aliased_View_Of_Type;
2926 -------------
2927 -- Process --
2928 -------------
2930 function Process (N : Node_Id) return Traverse_Result is
2931 begin
2932 case Nkind (N) is
2933 when N_Attribute_Reference =>
2934 if Attribute_Name (N) in Name_Access | Name_Unchecked_Access
2935 and then Is_Entity_Name (Prefix (N))
2936 and then Is_Type (Entity (Prefix (N)))
2937 and then Entity (Prefix (N)) = E
2938 then
2939 if Ada_Version < Ada_2012 then
2940 Error_Msg_N
2941 ("current instance must be a limited type",
2942 Prefix (N));
2943 else
2944 Error_Msg_N
2945 ("current instance must be an immutably limited "
2946 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2947 end if;
2949 return Abandon;
2951 else
2952 return OK;
2953 end if;
2955 when others =>
2956 return OK;
2957 end case;
2958 end Process;
2960 procedure Traverse is new Traverse_Proc (Process);
2962 -- Local variables
2964 Rec_Type : constant Entity_Id :=
2965 Scope (Defining_Identifier (Comp_Decl));
2967 -- Start of processing for Check_Current_Instance
2969 begin
2970 if not Is_Aliased_View_Of_Type (Rec_Type) then
2971 Traverse (Comp_Decl);
2972 end if;
2973 end Check_Current_Instance;
2975 -------------------------------
2976 -- Check_No_Parts_Violations --
2977 -------------------------------
2979 procedure Check_No_Parts_Violations
2980 (Typ : Entity_Id; Aspect_No_Parts : Aspect_Id)
2983 function Find_Aspect_No_Parts
2984 (Typ : Entity_Id) return Node_Id;
2985 -- Search for Aspect_No_Parts on a given type. When
2986 -- the aspect is not explicity specified Empty is returned.
2988 function Get_Aspect_No_Parts_Value
2989 (Typ : Entity_Id) return Entity_Id;
2990 -- Obtain the value for the Aspect_No_Parts on a given
2991 -- type. When the aspect is not explicitly specified Empty is
2992 -- returned.
2994 function Has_Aspect_No_Parts
2995 (Typ : Entity_Id) return Boolean;
2996 -- Predicate function which identifies whether No_Parts
2997 -- is explicitly specified on a given type.
2999 -------------------------------------
3000 -- Find_Aspect_No_Parts --
3001 -------------------------------------
3003 function Find_Aspect_No_Parts
3004 (Typ : Entity_Id) return Node_Id
3006 Partial_View : constant Entity_Id :=
3007 Incomplete_Or_Partial_View (Typ);
3009 Aspect_Spec : Entity_Id :=
3010 Find_Aspect (Typ, Aspect_No_Parts);
3011 Curr_Aspect_Spec : Entity_Id;
3012 begin
3014 -- Examine Typ's associated node, when present, since aspect
3015 -- specifications do not get transferred when nodes get rewritten.
3017 -- For example, this can happen in the expansion of array types
3019 if No (Aspect_Spec)
3020 and then Present (Associated_Node_For_Itype (Typ))
3021 and then Nkind (Associated_Node_For_Itype (Typ))
3022 = N_Full_Type_Declaration
3023 then
3024 Aspect_Spec :=
3025 Find_Aspect
3026 (Id => Defining_Identifier
3027 (Associated_Node_For_Itype (Typ)),
3028 A => Aspect_No_Parts);
3029 end if;
3031 -- Examine aspects specifications on private type declarations
3033 -- Should Find_Aspect be improved to handle this case ???
3035 if No (Aspect_Spec)
3036 and then Present (Partial_View)
3037 and then Present
3038 (Aspect_Specifications
3039 (Declaration_Node
3040 (Partial_View)))
3041 then
3042 Curr_Aspect_Spec :=
3043 First
3044 (Aspect_Specifications
3045 (Declaration_Node
3046 (Partial_View)));
3048 -- Search through aspects present on the private type
3050 while Present (Curr_Aspect_Spec) loop
3051 if Get_Aspect_Id (Curr_Aspect_Spec)
3052 = Aspect_No_Parts
3053 then
3054 Aspect_Spec := Curr_Aspect_Spec;
3055 exit;
3056 end if;
3058 Next (Curr_Aspect_Spec);
3059 end loop;
3061 end if;
3063 -- When errors are posted on the aspect return Empty
3065 if Error_Posted (Aspect_Spec) then
3066 return Empty;
3067 end if;
3069 return Aspect_Spec;
3070 end Find_Aspect_No_Parts;
3072 ------------------------------------------
3073 -- Get_Aspect_No_Parts_Value --
3074 ------------------------------------------
3076 function Get_Aspect_No_Parts_Value
3077 (Typ : Entity_Id) return Entity_Id
3079 Aspect_Spec : constant Entity_Id :=
3080 Find_Aspect_No_Parts (Typ);
3081 begin
3083 -- Return the value of the aspect when present
3085 if Present (Aspect_Spec) then
3087 -- No expression is the same as True
3089 if No (Expression (Aspect_Spec)) then
3090 return Standard_True;
3091 end if;
3093 -- Assume its expression has already been constant folded into
3094 -- a Boolean value and return its value.
3096 return Entity (Expression (Aspect_Spec));
3097 end if;
3099 -- Otherwise, the aspect is not specified - so return Empty
3101 return Empty;
3102 end Get_Aspect_No_Parts_Value;
3104 ------------------------------------
3105 -- Has_Aspect_No_Parts --
3106 ------------------------------------
3108 function Has_Aspect_No_Parts
3109 (Typ : Entity_Id) return Boolean
3110 is (Present (Find_Aspect_No_Parts (Typ)));
3112 -- Generic instances
3114 -------------------------------------------
3115 -- Get_Generic_Formal_Types_In_Hierarchy --
3116 -------------------------------------------
3118 function Get_Generic_Formal_Types_In_Hierarchy
3119 is new Collect_Types_In_Hierarchy (Predicate => Is_Generic_Formal);
3120 -- Return a list of all types within a given type's hierarchy which
3121 -- are generic formals.
3123 ----------------------------------------
3124 -- Get_Types_With_Aspect_In_Hierarchy --
3125 ----------------------------------------
3127 function Get_Types_With_Aspect_In_Hierarchy
3128 is new Collect_Types_In_Hierarchy
3129 (Predicate => Has_Aspect_No_Parts);
3130 -- Returns a list of all types within a given type's hierarchy which
3131 -- have the Aspect_No_Parts specified.
3133 -- Local declarations
3135 Aspect_Value : Entity_Id;
3136 Curr_Value : Entity_Id;
3137 Curr_Typ_Elmt : Elmt_Id;
3138 Curr_Body_Elmt : Elmt_Id;
3139 Curr_Formal_Elmt : Elmt_Id;
3140 Gen_Bodies : Elist_Id;
3141 Gen_Formals : Elist_Id;
3142 Scop : Entity_Id;
3143 Types_With_Aspect : Elist_Id;
3145 -- Start of processing for Check_No_Parts_Violations
3147 begin
3148 -- Nothing to check if the type is elementary or artificial
3150 if Is_Elementary_Type (Typ) or else not Comes_From_Source (Typ) then
3151 return;
3152 end if;
3154 Types_With_Aspect := Get_Types_With_Aspect_In_Hierarchy (Typ);
3156 -- Nothing to check if there are no types with No_Parts specified
3158 if Is_Empty_Elmt_List (Types_With_Aspect) then
3159 return;
3160 end if;
3162 -- Set name for all errors below
3164 Error_Msg_Name_1 := Aspect_Names (Aspect_No_Parts);
3166 -- Obtain the aspect value for No_Parts for comparison
3168 Aspect_Value :=
3169 Get_Aspect_No_Parts_Value
3170 (Node (First_Elmt (Types_With_Aspect)));
3172 -- When the value is True and there are controlled/task parts or the
3173 -- type itself is controlled/task, trigger the appropriate error.
3175 if Aspect_Value = Standard_True then
3176 if Aspect_No_Parts = Aspect_No_Controlled_Parts then
3177 if Is_Controlled (Typ) or else Has_Controlled_Component (Typ)
3178 then
3179 Error_Msg_N
3180 ("aspect % applied to controlled type &", Typ);
3181 end if;
3183 elsif Aspect_No_Parts = Aspect_No_Task_Parts then
3184 if Has_Task (Typ) then
3185 Error_Msg_N
3186 ("aspect % applied to task type &", Typ);
3187 Error_Msg_N
3188 ("\replace task components with access-to-task-type "
3189 & "components??", Typ);
3190 end if;
3192 else
3193 raise Program_Error;
3194 end if;
3195 end if;
3197 -- Move through Types_With_Aspect - checking that the value specified
3198 -- for their corresponding Aspect_No_Parts do not override each
3199 -- other.
3201 Curr_Typ_Elmt := First_Elmt (Types_With_Aspect);
3202 while Present (Curr_Typ_Elmt) loop
3203 Curr_Value :=
3204 Get_Aspect_No_Parts_Value (Node (Curr_Typ_Elmt));
3206 -- Compare the aspect value against the current type
3208 if Curr_Value /= Aspect_Value then
3209 Error_Msg_NE
3210 ("cannot override aspect % of "
3211 & "ancestor type &", Typ, Node (Curr_Typ_Elmt));
3212 return;
3213 end if;
3215 Next_Elmt (Curr_Typ_Elmt);
3216 end loop;
3218 -- Issue an error if the aspect applies to a type declared inside a
3219 -- generic body and if said type derives from or has a component
3220 -- of ageneric formal type - since those are considered to have
3221 -- controlled/task parts and have Aspect_No_Parts specified as
3222 -- False by default (RM H.4.1(4/5) is about the language-defined
3223 -- No_Controlled_Parts aspect, and we are using the same rules for
3224 -- No_Task_Parts).
3226 -- We do not check tagged types since deriving from a formal type
3227 -- within an enclosing generic unit is already illegal
3228 -- (RM 3.9.1 (4/2)).
3230 if Aspect_Value = Standard_True
3231 and then In_Generic_Body (Typ)
3232 and then not Is_Tagged_Type (Typ)
3233 then
3234 Gen_Bodies := New_Elmt_List;
3235 Gen_Formals :=
3236 Get_Generic_Formal_Types_In_Hierarchy
3237 (Typ => Typ,
3238 Examine_Components => True);
3240 -- Climb scopes collecting generic bodies
3242 Scop := Scope (Typ);
3243 while Present (Scop) and then Scop /= Standard_Standard loop
3245 -- Generic package body
3247 if Ekind (Scop) = E_Generic_Package
3248 and then In_Package_Body (Scop)
3249 then
3250 Append_Elmt (Scop, Gen_Bodies);
3252 -- Generic subprogram body
3254 elsif Is_Generic_Subprogram (Scop) then
3255 Append_Elmt (Scop, Gen_Bodies);
3256 end if;
3258 Scop := Scope (Scop);
3259 end loop;
3261 -- Warn about the improper use of Aspect_No_Parts on a type
3262 -- declaration deriving from or that has a component of a generic
3263 -- formal type within the formal type's corresponding generic
3264 -- body by moving through all formal types in Typ's hierarchy and
3265 -- checking if they are formals in any of the enclosing generic
3266 -- bodies.
3268 -- However, a special exception gets made for formal types which
3269 -- derive from a type which has Aspect_No_Parts True.
3271 -- For example:
3273 -- generic
3274 -- type Form is private;
3275 -- package G is
3276 -- type Type_A is new Form with No_Controlled_Parts; -- OK
3277 -- end;
3279 -- package body G is
3280 -- type Type_B is new Form with No_Controlled_Parts; -- ERROR
3281 -- end;
3283 -- generic
3284 -- type Form is private;
3285 -- package G is
3286 -- type Type_A is record C : Form; end record
3287 -- with No_Controlled_Parts; -- OK
3288 -- end;
3290 -- package body G is
3291 -- type Type_B is record C : Form; end record
3292 -- with No_Controlled_Parts; -- ERROR
3293 -- end;
3295 -- type Root is tagged null record with No_Controlled_Parts;
3297 -- generic
3298 -- type Form is new Root with private;
3299 -- package G is
3300 -- type Type_A is record C : Form; end record
3301 -- with No_Controlled_Parts; -- OK
3302 -- end;
3304 -- package body G is
3305 -- type Type_B is record C : Form; end record
3306 -- with No_Controlled_Parts; -- OK
3307 -- end;
3309 Curr_Formal_Elmt := First_Elmt (Gen_Formals);
3310 while Present (Curr_Formal_Elmt) loop
3312 Curr_Body_Elmt := First_Elmt (Gen_Bodies);
3313 while Present (Curr_Body_Elmt) loop
3315 -- Obtain types in the formal type's hierarchy which have
3316 -- the aspect specified.
3318 Types_With_Aspect :=
3319 Get_Types_With_Aspect_In_Hierarchy
3320 (Node (Curr_Formal_Elmt));
3322 -- We found a type declaration in a generic body where both
3323 -- Aspect_No_Parts is true and one of its ancestors is a
3324 -- generic formal type.
3326 if Scope (Node (Curr_Formal_Elmt)) =
3327 Node (Curr_Body_Elmt)
3329 -- Check that no ancestors of the formal type have
3330 -- Aspect_No_Parts True before issuing the error.
3332 and then (Is_Empty_Elmt_List (Types_With_Aspect)
3333 or else
3334 Get_Aspect_No_Parts_Value
3335 (Node (First_Elmt (Types_With_Aspect)))
3336 = Standard_False)
3337 then
3338 Error_Msg_Node_1 := Typ;
3339 Error_Msg_Node_2 := Node (Curr_Formal_Elmt);
3340 Error_Msg
3341 ("aspect % cannot be applied to "
3342 & "type & which has an ancestor or component of "
3343 & "formal type & within the formal type's "
3344 & "corresponding generic body", Sloc (Typ));
3345 end if;
3347 Next_Elmt (Curr_Body_Elmt);
3348 end loop;
3350 Next_Elmt (Curr_Formal_Elmt);
3351 end loop;
3352 end if;
3353 end Check_No_Parts_Violations;
3355 ---------------------------------
3356 -- Check_Suspicious_Convention --
3357 ---------------------------------
3359 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id) is
3360 begin
3361 if Has_Discriminants (Rec_Type)
3362 and then Is_Base_Type (Rec_Type)
3363 and then not Is_Unchecked_Union (Rec_Type)
3364 and then (Convention (Rec_Type) = Convention_C
3365 or else
3366 Convention (Rec_Type) = Convention_CPP)
3367 and then Comes_From_Source (Rec_Type)
3368 and then not In_Instance
3369 and then not Has_Warnings_Off (Rec_Type)
3370 then
3371 declare
3372 Cprag : constant Node_Id :=
3373 Get_Rep_Pragma (Rec_Type, Name_Convention);
3374 A2 : Node_Id;
3376 begin
3377 if Present (Cprag) then
3378 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
3380 if Convention (Rec_Type) = Convention_C then
3381 Error_Msg_N
3382 ("?x?discriminated record has no direct equivalent in "
3383 & "C", A2);
3384 else
3385 Error_Msg_N
3386 ("?x?discriminated record has no direct equivalent in "
3387 & "C++", A2);
3388 end if;
3390 Error_Msg_NE
3391 ("\?x?use of convention for type& is dubious",
3392 A2, Rec_Type);
3393 end if;
3394 end;
3395 end if;
3396 end Check_Suspicious_Convention;
3398 ------------------------------
3399 -- Check_Suspicious_Modulus --
3400 ------------------------------
3402 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
3403 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
3405 begin
3406 if not Warn_On_Suspicious_Modulus_Value then
3407 return;
3408 end if;
3410 if Nkind (Decl) = N_Full_Type_Declaration then
3411 declare
3412 Tdef : constant Node_Id := Type_Definition (Decl);
3414 begin
3415 if Nkind (Tdef) = N_Modular_Type_Definition then
3416 declare
3417 Modulus : constant Node_Id :=
3418 Original_Node (Expression (Tdef));
3420 begin
3421 if Nkind (Modulus) = N_Integer_Literal then
3422 declare
3423 Modv : constant Uint := Intval (Modulus);
3424 Sizv : constant Uint := RM_Size (Utype);
3426 begin
3427 -- First case, modulus and size are the same. This
3428 -- happens if you have something like mod 32, with
3429 -- an explicit size of 32, this is for sure a case
3430 -- where the warning is given, since it is seems
3431 -- very unlikely that someone would want e.g. a
3432 -- five bit type stored in 32 bits. It is much
3433 -- more likely they wanted a 32-bit type.
3435 if Modv = Sizv then
3436 null;
3438 -- Second case, the modulus is 32 or 64 and no
3439 -- size clause is present. This is a less clear
3440 -- case for giving the warning, but in the case
3441 -- of 32/64 (5-bit or 6-bit types) these seem rare
3442 -- enough that it is a likely error (and in any
3443 -- case using 2**5 or 2**6 in these cases seems
3444 -- clearer. We don't include 8 or 16 here, simply
3445 -- because in practice 3-bit and 4-bit types are
3446 -- more common and too many false positives if
3447 -- we warn in these cases.
3449 elsif not Has_Size_Clause (Utype)
3450 and then (Modv = Uint_32 or else Modv = Uint_64)
3451 then
3452 null;
3454 -- No warning needed
3456 else
3457 return;
3458 end if;
3460 -- If we fall through, give warning
3462 Error_Msg_Uint_1 := Modv;
3463 Error_Msg_N
3464 ("?.m?2 '*'*^' may have been intended here",
3465 Modulus);
3466 end;
3467 end if;
3468 end;
3469 end if;
3470 end;
3471 end if;
3472 end Check_Suspicious_Modulus;
3474 -----------------------
3475 -- Freeze_Array_Type --
3476 -----------------------
3478 procedure Freeze_Array_Type (Arr : Entity_Id) is
3479 FS : constant Entity_Id := First_Subtype (Arr);
3480 Ctyp : constant Entity_Id := Component_Type (Arr);
3481 Clause : Entity_Id;
3483 Non_Standard_Enum : Boolean := False;
3484 -- Set true if any of the index types is an enumeration type with a
3485 -- non-standard representation.
3487 begin
3488 Freeze_And_Append (Ctyp, N, Result);
3490 Indx := First_Index (Arr);
3491 while Present (Indx) loop
3492 Freeze_And_Append (Etype (Indx), N, Result);
3494 if Is_Enumeration_Type (Etype (Indx))
3495 and then Has_Non_Standard_Rep (Etype (Indx))
3496 then
3497 Non_Standard_Enum := True;
3498 end if;
3500 Next_Index (Indx);
3501 end loop;
3503 -- Processing that is done only for base types
3505 if Ekind (Arr) = E_Array_Type then
3507 -- Deal with default setting of reverse storage order
3509 Set_SSO_From_Default (Arr);
3511 -- Propagate flags for component type
3513 if Is_Controlled (Ctyp)
3514 or else Has_Controlled_Component (Ctyp)
3515 then
3516 Set_Has_Controlled_Component (Arr);
3517 end if;
3519 if Has_Unchecked_Union (Ctyp) then
3520 Set_Has_Unchecked_Union (Arr);
3521 end if;
3523 -- The array type requires its own invariant procedure in order to
3524 -- verify the component invariant over all elements. In GNATprove
3525 -- mode, the component invariants are checked by other means. They
3526 -- should not be added to the array type invariant procedure, so
3527 -- that the procedure can be used to check the array type
3528 -- invariants if any.
3530 if Has_Invariants (Ctyp)
3531 and then not GNATprove_Mode
3532 then
3533 Set_Has_Own_Invariants (Arr);
3534 end if;
3536 -- Warn for pragma Pack overriding foreign convention
3538 if Has_Foreign_Convention (Ctyp)
3539 and then Has_Pragma_Pack (Arr)
3540 then
3541 declare
3542 CN : constant Name_Id :=
3543 Get_Convention_Name (Convention (Ctyp));
3544 PP : constant Node_Id :=
3545 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
3546 begin
3547 if Present (PP) then
3548 Error_Msg_Name_1 := CN;
3549 Error_Msg_Sloc := Sloc (Arr);
3550 Error_Msg_N
3551 ("pragma Pack affects convention % components #??", PP);
3552 Error_Msg_Name_1 := CN;
3553 Error_Msg_N
3554 ("\array components may not have % compatible "
3555 & "representation??", PP);
3556 end if;
3557 end;
3558 end if;
3560 -- Check for Aliased or Atomic_Components or Full Access with
3561 -- unsuitable packing or explicit component size clause given.
3563 if (Has_Aliased_Components (Arr)
3564 or else Has_Atomic_Components (Arr)
3565 or else Is_Full_Access (Ctyp))
3566 and then
3567 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
3568 then
3569 Alias_Atomic_Check : declare
3571 procedure Complain_CS (T : String);
3572 -- Outputs error messages for incorrect CS clause or pragma
3573 -- Pack for aliased or full access components (T is either
3574 -- "aliased" or "atomic" or "volatile full access");
3576 -----------------
3577 -- Complain_CS --
3578 -----------------
3580 procedure Complain_CS (T : String) is
3581 begin
3582 if Has_Component_Size_Clause (Arr) then
3583 Clause :=
3584 Get_Attribute_Definition_Clause
3585 (FS, Attribute_Component_Size);
3587 Error_Msg_N
3588 ("incorrect component size for "
3589 & T & " components", Clause);
3590 Error_Msg_Uint_1 := Esize (Ctyp);
3591 Error_Msg_N
3592 ("\only allowed value is^", Clause);
3594 else
3595 Error_Msg_N
3596 ("?cannot pack " & T & " components (RM 13.2(7))",
3597 Get_Rep_Pragma (FS, Name_Pack));
3598 Set_Is_Packed (Arr, False);
3599 end if;
3600 end Complain_CS;
3602 -- Start of processing for Alias_Atomic_Check
3604 begin
3605 -- If object size of component type isn't known, we cannot
3606 -- be sure so we defer to the back end.
3608 if not Known_Static_Esize (Ctyp) then
3609 null;
3611 -- Case where component size has no effect. First check for
3612 -- object size of component type multiple of the storage
3613 -- unit size.
3615 elsif Esize (Ctyp) mod System_Storage_Unit = 0
3617 -- OK in both packing case and component size case if RM
3618 -- size is known and static and same as the object size.
3620 and then
3621 ((Known_Static_RM_Size (Ctyp)
3622 and then Esize (Ctyp) = RM_Size (Ctyp))
3624 -- Or if we have an explicit component size clause and
3625 -- the component size and object size are equal.
3627 or else
3628 (Has_Component_Size_Clause (Arr)
3629 and then Component_Size (Arr) = Esize (Ctyp)))
3630 then
3631 null;
3633 elsif Has_Aliased_Components (Arr) then
3634 Complain_CS ("aliased");
3636 elsif Has_Atomic_Components (Arr)
3637 or else Is_Atomic (Ctyp)
3638 then
3639 Complain_CS ("atomic");
3641 elsif Is_Volatile_Full_Access (Ctyp) then
3642 Complain_CS ("volatile full access");
3643 end if;
3644 end Alias_Atomic_Check;
3645 end if;
3647 -- Check for Independent_Components/Independent with unsuitable
3648 -- packing or explicit component size clause given.
3650 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
3651 and then
3652 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
3653 then
3654 begin
3655 -- If object size of component type isn't known, we cannot
3656 -- be sure so we defer to the back end.
3658 if not Known_Static_Esize (Ctyp) then
3659 null;
3661 -- Case where component size has no effect. First check for
3662 -- object size of component type multiple of the storage
3663 -- unit size.
3665 elsif Esize (Ctyp) mod System_Storage_Unit = 0
3667 -- OK in both packing case and component size case if RM
3668 -- size is known and multiple of the storage unit size.
3670 and then
3671 ((Known_Static_RM_Size (Ctyp)
3672 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
3674 -- Or if we have an explicit component size clause and
3675 -- the component size is larger than the object size.
3677 or else
3678 (Has_Component_Size_Clause (Arr)
3679 and then Component_Size (Arr) >= Esize (Ctyp)))
3680 then
3681 null;
3683 else
3684 if Has_Component_Size_Clause (Arr) then
3685 Clause :=
3686 Get_Attribute_Definition_Clause
3687 (FS, Attribute_Component_Size);
3689 Error_Msg_N
3690 ("incorrect component size for "
3691 & "independent components", Clause);
3692 Error_Msg_Uint_1 := Esize (Ctyp);
3693 Error_Msg_N
3694 ("\minimum allowed is^", Clause);
3696 else
3697 Error_Msg_N
3698 ("?cannot pack independent components (RM 13.2(7))",
3699 Get_Rep_Pragma (FS, Name_Pack));
3700 Set_Is_Packed (Arr, False);
3701 end if;
3702 end if;
3703 end;
3704 end if;
3706 -- If packing was requested or if the component size was
3707 -- set explicitly, then see if bit packing is required. This
3708 -- processing is only done for base types, since all of the
3709 -- representation aspects involved are type-related.
3711 -- This is not just an optimization, if we start processing the
3712 -- subtypes, they interfere with the settings on the base type
3713 -- (this is because Is_Packed has a slightly different meaning
3714 -- before and after freezing).
3716 declare
3717 Csiz : Uint;
3718 Esiz : Uint;
3720 begin
3721 if Is_Packed (Arr)
3722 and then Known_Static_RM_Size (Ctyp)
3723 and then not Has_Component_Size_Clause (Arr)
3724 then
3725 Csiz := UI_Max (RM_Size (Ctyp), 1);
3727 elsif Known_Component_Size (Arr) then
3728 Csiz := Component_Size (Arr);
3730 elsif not Known_Static_Esize (Ctyp) then
3731 Csiz := Uint_0;
3733 else
3734 Esiz := Esize (Ctyp);
3736 -- We can set the component size if it is less than 16,
3737 -- rounding it up to the next storage unit size.
3739 if Esiz <= 8 then
3740 Csiz := Uint_8;
3741 elsif Esiz <= 16 then
3742 Csiz := Uint_16;
3743 else
3744 Csiz := Uint_0;
3745 end if;
3747 -- Set component size up to match alignment if it would
3748 -- otherwise be less than the alignment. This deals with
3749 -- cases of types whose alignment exceeds their size (the
3750 -- padded type cases).
3752 if Csiz /= 0 and then Known_Alignment (Ctyp) then
3753 declare
3754 A : constant Uint := Alignment_In_Bits (Ctyp);
3755 begin
3756 if Csiz < A then
3757 Csiz := A;
3758 end if;
3759 end;
3760 end if;
3761 end if;
3763 -- Case of component size that may result in bit packing
3765 if 1 <= Csiz and then Csiz <= System_Max_Integer_Size then
3766 declare
3767 Ent : constant Entity_Id :=
3768 First_Subtype (Arr);
3769 Pack_Pragma : constant Node_Id :=
3770 Get_Rep_Pragma (Ent, Name_Pack);
3771 Comp_Size_C : constant Node_Id :=
3772 Get_Attribute_Definition_Clause
3773 (Ent, Attribute_Component_Size);
3775 begin
3776 -- Warn if we have pack and component size so that the
3777 -- pack is ignored.
3779 -- Note: here we must check for the presence of a
3780 -- component size before checking for a Pack pragma to
3781 -- deal with the case where the array type is a derived
3782 -- type whose parent is currently private.
3784 if Present (Comp_Size_C)
3785 and then Has_Pragma_Pack (Ent)
3786 and then Warn_On_Redundant_Constructs
3787 then
3788 Error_Msg_Sloc := Sloc (Comp_Size_C);
3789 Error_Msg_NE
3790 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
3791 Error_Msg_N
3792 ("\?r?explicit component size given#!", Pack_Pragma);
3793 Set_Is_Packed (Base_Type (Ent), False);
3794 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
3795 end if;
3797 -- Set component size if not already set by a component
3798 -- size clause.
3800 if No (Comp_Size_C) then
3801 Set_Component_Size (Arr, Csiz);
3802 end if;
3804 -- Check for base type of 8, 16, 32 bits, where an
3805 -- unsigned subtype has a length one less than the
3806 -- base type (e.g. Natural subtype of Integer).
3808 -- In such cases, if a component size was not set
3809 -- explicitly, then generate a warning.
3811 if Has_Pragma_Pack (Arr)
3812 and then No (Comp_Size_C)
3813 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
3814 and then Known_Esize (Base_Type (Ctyp))
3815 and then Esize (Base_Type (Ctyp)) = Csiz + 1
3816 then
3817 Error_Msg_Uint_1 := Csiz;
3819 if Present (Pack_Pragma) then
3820 Error_Msg_N
3821 ("??pragma Pack causes component size to be ^!",
3822 Pack_Pragma);
3823 Error_Msg_N
3824 ("\??use Component_Size to set desired value!",
3825 Pack_Pragma);
3826 end if;
3827 end if;
3829 -- Bit packing is never needed for 8, 16, 32, 64 or 128
3831 if Addressable (Csiz) then
3833 -- If the Esize of the component is known and equal to
3834 -- the component size then even packing is not needed.
3836 if Known_Static_Esize (Ctyp)
3837 and then Esize (Ctyp) = Csiz
3838 then
3839 -- Here the array was requested to be packed, but
3840 -- the packing request had no effect whatsoever,
3841 -- so flag Is_Packed is reset.
3843 -- Note: semantically this means that we lose track
3844 -- of the fact that a derived type inherited pragma
3845 -- Pack that was non-effective, but that is fine.
3847 -- We regard a Pack pragma as a request to set a
3848 -- representation characteristic, and this request
3849 -- may be ignored.
3851 Set_Is_Packed (Base_Type (Arr), False);
3852 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
3853 else
3854 Set_Is_Packed (Base_Type (Arr), True);
3855 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3856 end if;
3858 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3860 -- Bit packing is not needed for multiples of the storage
3861 -- unit if the type is composite because the back end can
3862 -- byte pack composite types efficiently. That's not true
3863 -- for discrete types because every read would generate a
3864 -- lot of instructions, so we keep using the manipulation
3865 -- routines of the runtime for them.
3867 elsif Csiz mod System_Storage_Unit = 0
3868 and then Is_Composite_Type (Ctyp)
3869 then
3870 Set_Is_Packed (Base_Type (Arr), True);
3871 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3872 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3874 -- In all other cases, bit packing is needed
3876 else
3877 Set_Is_Packed (Base_Type (Arr), True);
3878 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3879 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
3880 end if;
3881 end;
3882 end if;
3883 end;
3885 -- Warn for case of atomic type
3887 Clause := Get_Rep_Pragma (FS, Name_Atomic);
3889 if Present (Clause)
3890 and then not Addressable (Component_Size (FS))
3891 then
3892 Error_Msg_NE
3893 ("non-atomic components of type& may not be "
3894 & "accessible by separate tasks??", Clause, Arr);
3896 if Has_Component_Size_Clause (Arr) then
3897 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
3898 (FS, Attribute_Component_Size));
3899 Error_Msg_N ("\because of component size clause#??", Clause);
3901 elsif Has_Pragma_Pack (Arr) then
3902 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
3903 Error_Msg_N ("\because of pragma Pack#??", Clause);
3904 end if;
3905 end if;
3907 -- Check for scalar storage order
3909 declare
3910 Dummy : Boolean;
3911 begin
3912 Check_Component_Storage_Order
3913 (Encl_Type => Arr,
3914 Comp => Empty,
3915 ADC => Get_Attribute_Definition_Clause
3916 (First_Subtype (Arr),
3917 Attribute_Scalar_Storage_Order),
3918 Comp_ADC_Present => Dummy);
3919 end;
3921 -- Processing that is done only for subtypes
3923 else
3924 -- Acquire alignment from base type. Known_Alignment of the base
3925 -- type is False for Wide_String, for example.
3927 if not Known_Alignment (Arr)
3928 and then Known_Alignment (Base_Type (Arr))
3929 then
3930 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
3931 Adjust_Esize_Alignment (Arr);
3932 end if;
3933 end if;
3935 -- Specific checks for bit-packed arrays
3937 if Is_Bit_Packed_Array (Arr) then
3939 -- Check number of elements for bit-packed arrays that come from
3940 -- source and have compile time known ranges. The bit-packed
3941 -- arrays circuitry does not support arrays with more than
3942 -- Integer'Last + 1 elements, and when this restriction is
3943 -- violated, causes incorrect data access.
3945 -- For the case where this is not compile time known, a run-time
3946 -- check should be generated???
3948 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
3949 declare
3950 Elmts : Uint;
3951 Index : Node_Id;
3952 Ilen : Node_Id;
3953 Ityp : Entity_Id;
3955 begin
3956 Elmts := Uint_1;
3957 Index := First_Index (Arr);
3958 while Present (Index) loop
3959 Ityp := Etype (Index);
3961 -- Never generate an error if any index is of a generic
3962 -- type. We will check this in instances.
3964 if Is_Generic_Type (Ityp) then
3965 Elmts := Uint_0;
3966 exit;
3967 end if;
3969 Ilen :=
3970 Make_Attribute_Reference (Loc,
3971 Prefix => New_Occurrence_Of (Ityp, Loc),
3972 Attribute_Name => Name_Range_Length);
3973 Analyze_And_Resolve (Ilen);
3975 -- No attempt is made to check number of elements if not
3976 -- compile time known.
3978 if Nkind (Ilen) /= N_Integer_Literal then
3979 Elmts := Uint_0;
3980 exit;
3981 end if;
3983 Elmts := Elmts * Intval (Ilen);
3984 Next_Index (Index);
3985 end loop;
3987 if Elmts > Intval (High_Bound
3988 (Scalar_Range (Standard_Integer))) + 1
3989 then
3990 Error_Msg_N
3991 ("bit packed array type may not have "
3992 & "more than Integer''Last+1 elements", Arr);
3993 end if;
3994 end;
3995 end if;
3997 -- Check size
3999 if Known_RM_Size (Arr) then
4000 declare
4001 SizC : constant Node_Id := Size_Clause (Arr);
4002 Discard : Boolean;
4004 begin
4005 -- It is not clear if it is possible to have no size clause
4006 -- at this stage, but it is not worth worrying about. Post
4007 -- error on the entity name in the size clause if present,
4008 -- else on the type entity itself.
4010 if Present (SizC) then
4011 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
4012 else
4013 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
4014 end if;
4015 end;
4016 end if;
4017 end if;
4019 -- If any of the index types was an enumeration type with a non-
4020 -- standard rep clause, then we indicate that the array type is
4021 -- always packed (even if it is not bit-packed).
4023 if Non_Standard_Enum then
4024 Set_Has_Non_Standard_Rep (Base_Type (Arr));
4025 Set_Is_Packed (Base_Type (Arr));
4026 end if;
4028 Set_Component_Alignment_If_Not_Set (Arr);
4030 -- If the array is packed and bit-packed or packed to eliminate holes
4031 -- in the non-contiguous enumeration index types, we must create the
4032 -- packed array type to be used to actually implement the type. This
4033 -- is only needed for real array types (not for string literal types,
4034 -- since they are present only for the front end).
4036 if Is_Packed (Arr)
4037 and then (Is_Bit_Packed_Array (Arr) or else Non_Standard_Enum)
4038 and then Ekind (Arr) /= E_String_Literal_Subtype
4039 then
4040 Create_Packed_Array_Impl_Type (Arr);
4041 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
4043 -- Make sure that we have the necessary routines to implement the
4044 -- packing, and complain now if not. Note that we only test this
4045 -- for constrained array types.
4047 if Is_Constrained (Arr)
4048 and then Is_Bit_Packed_Array (Arr)
4049 and then Present (Packed_Array_Impl_Type (Arr))
4050 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
4051 then
4052 declare
4053 CS : constant Uint := Component_Size (Arr);
4054 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
4056 begin
4057 if RE /= RE_Null
4058 and then not RTE_Available (RE)
4059 then
4060 Error_Msg_CRT
4061 ("packing of " & UI_Image (CS) & "-bit components",
4062 First_Subtype (Etype (Arr)));
4064 -- Cancel the packing
4066 Set_Is_Packed (Base_Type (Arr), False);
4067 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
4068 Set_Packed_Array_Impl_Type (Arr, Empty);
4069 goto Skip_Packed;
4070 end if;
4071 end;
4072 end if;
4074 -- Size information of packed array type is copied to the array
4075 -- type, since this is really the representation. But do not
4076 -- override explicit existing size values. If the ancestor subtype
4077 -- is constrained the Packed_Array_Impl_Type will be inherited
4078 -- from it, but the size may have been provided already, and
4079 -- must not be overridden either.
4081 if not Has_Size_Clause (Arr)
4082 and then
4083 (No (Ancestor_Subtype (Arr))
4084 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
4085 then
4086 Copy_Esize (To => Arr, From => Packed_Array_Impl_Type (Arr));
4087 Copy_RM_Size (To => Arr, From => Packed_Array_Impl_Type (Arr));
4088 end if;
4090 if not Has_Alignment_Clause (Arr) then
4091 Copy_Alignment
4092 (To => Arr, From => Packed_Array_Impl_Type (Arr));
4093 end if;
4094 end if;
4096 <<Skip_Packed>>
4098 -- A Ghost type cannot have a component of protected or task type
4099 -- (SPARK RM 6.9(19)).
4101 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
4102 Error_Msg_N
4103 ("ghost array type & cannot have concurrent component type",
4104 Arr);
4105 end if;
4106 end Freeze_Array_Type;
4108 -------------------------------
4109 -- Freeze_Object_Declaration --
4110 -------------------------------
4112 procedure Freeze_Object_Declaration (E : Entity_Id) is
4113 procedure Check_Large_Modular_Array (Typ : Entity_Id);
4114 -- Check that the size of array type Typ can be computed without
4115 -- overflow, and generates a Storage_Error otherwise. This is only
4116 -- relevant for array types whose index has System_Max_Integer_Size
4117 -- bits, where wrap-around arithmetic might yield a meaningless value
4118 -- for the length of the array, or its corresponding attribute.
4120 procedure Check_Pragma_Thread_Local_Storage (Var_Id : Entity_Id);
4121 -- Ensure that the initialization state of variable Var_Id subject
4122 -- to pragma Thread_Local_Storage agrees with the semantics of the
4123 -- pragma.
4125 function Has_Default_Initialization
4126 (Obj_Id : Entity_Id) return Boolean;
4127 -- Determine whether object Obj_Id default initialized
4129 -------------------------------
4130 -- Check_Large_Modular_Array --
4131 -------------------------------
4133 procedure Check_Large_Modular_Array (Typ : Entity_Id) is
4134 Obj_Loc : constant Source_Ptr := Sloc (E);
4135 Idx_Typ : Entity_Id;
4137 begin
4138 -- Nothing to do when expansion is disabled because this routine
4139 -- generates a runtime check.
4141 if not Expander_Active then
4142 return;
4144 -- Nothing to do for String literal subtypes because their index
4145 -- cannot be a modular type.
4147 elsif Ekind (Typ) = E_String_Literal_Subtype then
4148 return;
4150 -- Nothing to do for an imported object because the object will
4151 -- be created on the exporting side.
4153 elsif Is_Imported (E) then
4154 return;
4156 -- Nothing to do for unconstrained array types. This case arises
4157 -- when the object declaration is illegal.
4159 elsif not Is_Constrained (Typ) then
4160 return;
4161 end if;
4163 Idx_Typ := Etype (First_Index (Typ));
4165 -- To prevent arithmetic overflow with large values, we raise
4166 -- Storage_Error under the following guard:
4168 -- (Arr'Last / 2 - Arr'First / 2) > (2 ** 30)
4170 -- This takes care of the boundary case, but it is preferable to
4171 -- use a smaller limit, because even on 64-bit architectures an
4172 -- array of more than 2 ** 30 bytes is likely to raise
4173 -- Storage_Error.
4175 if Is_Modular_Integer_Type (Idx_Typ)
4176 and then RM_Size (Idx_Typ) = RM_Size (Standard_Long_Long_Integer)
4177 then
4178 Insert_Action (Declaration_Node (E),
4179 Make_Raise_Storage_Error (Obj_Loc,
4180 Condition =>
4181 Make_Op_Ge (Obj_Loc,
4182 Left_Opnd =>
4183 Make_Op_Subtract (Obj_Loc,
4184 Left_Opnd =>
4185 Make_Op_Divide (Obj_Loc,
4186 Left_Opnd =>
4187 Make_Attribute_Reference (Obj_Loc,
4188 Prefix =>
4189 New_Occurrence_Of (Typ, Obj_Loc),
4190 Attribute_Name => Name_Last),
4191 Right_Opnd =>
4192 Make_Integer_Literal (Obj_Loc, Uint_2)),
4193 Right_Opnd =>
4194 Make_Op_Divide (Obj_Loc,
4195 Left_Opnd =>
4196 Make_Attribute_Reference (Obj_Loc,
4197 Prefix =>
4198 New_Occurrence_Of (Typ, Obj_Loc),
4199 Attribute_Name => Name_First),
4200 Right_Opnd =>
4201 Make_Integer_Literal (Obj_Loc, Uint_2))),
4202 Right_Opnd =>
4203 Make_Integer_Literal (Obj_Loc, (Uint_2 ** 30))),
4204 Reason => SE_Object_Too_Large));
4205 end if;
4206 end Check_Large_Modular_Array;
4208 ---------------------------------------
4209 -- Check_Pragma_Thread_Local_Storage --
4210 ---------------------------------------
4212 procedure Check_Pragma_Thread_Local_Storage (Var_Id : Entity_Id) is
4213 function Has_Incompatible_Initialization
4214 (Var_Decl : Node_Id) return Boolean;
4215 -- Determine whether variable Var_Id with declaration Var_Decl is
4216 -- initialized with a value that violates the semantics of pragma
4217 -- Thread_Local_Storage.
4219 -------------------------------------
4220 -- Has_Incompatible_Initialization --
4221 -------------------------------------
4223 function Has_Incompatible_Initialization
4224 (Var_Decl : Node_Id) return Boolean
4226 Init_Expr : constant Node_Id := Expression (Var_Decl);
4228 begin
4229 -- The variable is default-initialized. This directly violates
4230 -- the semantics of the pragma.
4232 if Has_Default_Initialization (Var_Id) then
4233 return True;
4235 -- The variable has explicit initialization. In this case only
4236 -- a handful of values satisfy the semantics of the pragma.
4238 elsif Has_Init_Expression (Var_Decl)
4239 and then Present (Init_Expr)
4240 then
4241 -- "null" is a legal form of initialization
4243 if Nkind (Init_Expr) = N_Null then
4244 return False;
4246 -- A static expression is a legal form of initialization
4248 elsif Is_Static_Expression (Init_Expr) then
4249 return False;
4251 -- A static aggregate is a legal form of initialization
4253 elsif Nkind (Init_Expr) = N_Aggregate
4254 and then Compile_Time_Known_Aggregate (Init_Expr)
4255 then
4256 return False;
4258 -- All other initialization expressions violate the semantic
4259 -- of the pragma.
4261 else
4262 return True;
4263 end if;
4265 -- The variable lacks any kind of initialization, which agrees
4266 -- with the semantics of the pragma.
4268 else
4269 return False;
4270 end if;
4271 end Has_Incompatible_Initialization;
4273 -- Local declarations
4275 Var_Decl : constant Node_Id := Declaration_Node (Var_Id);
4277 -- Start of processing for Check_Pragma_Thread_Local_Storage
4279 begin
4280 -- A variable whose initialization is suppressed lacks any kind of
4281 -- initialization.
4283 if Suppress_Initialization (Var_Id) then
4284 null;
4286 -- The variable has default initialization, or is explicitly
4287 -- initialized to a value other than null, static expression,
4288 -- or a static aggregate.
4290 elsif Has_Incompatible_Initialization (Var_Decl) then
4291 Error_Msg_NE
4292 ("Thread_Local_Storage variable& is improperly initialized",
4293 Var_Decl, Var_Id);
4294 Error_Msg_NE
4295 ("\only allowed initialization is explicit NULL, static "
4296 & "expression or static aggregate", Var_Decl, Var_Id);
4297 end if;
4298 end Check_Pragma_Thread_Local_Storage;
4300 --------------------------------
4301 -- Has_Default_Initialization --
4302 --------------------------------
4304 function Has_Default_Initialization
4305 (Obj_Id : Entity_Id) return Boolean
4307 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
4308 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4310 begin
4311 return
4312 Comes_From_Source (Obj_Id)
4313 and then not Is_Imported (Obj_Id)
4314 and then not Has_Init_Expression (Obj_Decl)
4315 and then
4316 ((Has_Non_Null_Base_Init_Proc (Obj_Typ)
4317 and then not No_Initialization (Obj_Decl)
4318 and then not Initialization_Suppressed (Obj_Typ))
4319 or else
4320 (Needs_Simple_Initialization (Obj_Typ)
4321 and then not Is_Internal (Obj_Id)));
4322 end Has_Default_Initialization;
4324 -- Local variables
4326 Typ : constant Entity_Id := Etype (E);
4327 Def : Node_Id;
4329 -- Start of processing for Freeze_Object_Declaration
4331 begin
4332 -- Abstract type allowed only for C++ imported variables or constants
4334 -- Note: we inhibit this check for objects that do not come from
4335 -- source because there is at least one case (the expansion of
4336 -- x'Class'Input where x is abstract) where we legitimately
4337 -- generate an abstract object.
4339 if Is_Abstract_Type (Typ)
4340 and then Comes_From_Source (Parent (E))
4341 and then not (Is_Imported (E) and then Is_CPP_Class (Typ))
4342 then
4343 Def := Object_Definition (Parent (E));
4345 Error_Msg_N ("type of object cannot be abstract", Def);
4347 if Is_CPP_Class (Etype (E)) then
4348 Error_Msg_NE ("\} may need a cpp_constructor", Def, Typ);
4350 elsif Present (Expression (Parent (E))) then
4351 Error_Msg_N -- CODEFIX
4352 ("\maybe a class-wide type was meant", Def);
4353 end if;
4354 end if;
4356 -- For object created by object declaration, perform required
4357 -- categorization (preelaborate and pure) checks. Defer these
4358 -- checks to freeze time since pragma Import inhibits default
4359 -- initialization and thus pragma Import affects these checks.
4361 Validate_Object_Declaration (Declaration_Node (E));
4363 -- If there is an address clause, check that it is valid and if need
4364 -- be move initialization to the freeze node.
4366 Check_Address_Clause (E);
4368 -- Similar processing is needed for aspects that may affect object
4369 -- layout, like Address, if there is an initialization expression.
4370 -- We don't do this if there is a pragma Linker_Section, because it
4371 -- would prevent the back end from statically initializing the
4372 -- object; we don't want elaboration code in that case.
4374 if Has_Delayed_Aspects (E)
4375 and then Expander_Active
4376 and then Is_Array_Type (Typ)
4377 and then Present (Expression (Declaration_Node (E)))
4378 and then No (Linker_Section_Pragma (E))
4379 then
4380 declare
4381 Decl : constant Node_Id := Declaration_Node (E);
4382 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
4384 begin
4385 -- Capture initialization value at point of declaration, and
4386 -- make explicit assignment legal, because object may be a
4387 -- constant.
4389 Remove_Side_Effects (Expression (Decl));
4390 Set_Assignment_OK (Lhs);
4392 -- Move initialization to freeze actions
4394 Append_Freeze_Action (E,
4395 Make_Assignment_Statement (Loc,
4396 Name => Lhs,
4397 Expression => Expression (Decl)));
4399 Set_No_Initialization (Decl);
4400 -- Set_Is_Frozen (E, False);
4401 end;
4402 end if;
4404 -- Reset Is_True_Constant for non-constant aliased object. We
4405 -- consider that the fact that a non-constant object is aliased may
4406 -- indicate that some funny business is going on, e.g. an aliased
4407 -- object is passed by reference to a procedure which captures the
4408 -- address of the object, which is later used to assign a new value,
4409 -- even though the compiler thinks that it is not modified. Such
4410 -- code is highly dubious, but we choose to make it "work" for
4411 -- non-constant aliased objects.
4413 -- Note that we used to do this for all aliased objects, whether or
4414 -- not constant, but this caused anomalies down the line because we
4415 -- ended up with static objects that were not Is_True_Constant. Not
4416 -- resetting Is_True_Constant for (aliased) constant objects ensures
4417 -- that this anomaly never occurs.
4419 -- However, we don't do that for internal entities. We figure that if
4420 -- we deliberately set Is_True_Constant for an internal entity, e.g.
4421 -- a dispatch table entry, then we mean it.
4423 if Ekind (E) /= E_Constant
4424 and then (Is_Aliased (E) or else Is_Aliased (Typ))
4425 and then not Is_Internal_Name (Chars (E))
4426 then
4427 Set_Is_True_Constant (E, False);
4428 end if;
4430 -- If the object needs any kind of default initialization, an error
4431 -- must be issued if No_Default_Initialization applies. The check
4432 -- doesn't apply to imported objects, which are not ever default
4433 -- initialized, and is why the check is deferred until freezing, at
4434 -- which point we know if Import applies. Deferred constants are also
4435 -- exempted from this test because their completion is explicit, or
4436 -- through an import pragma.
4438 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
4439 null;
4441 elsif Has_Default_Initialization (E) then
4442 Check_Restriction
4443 (No_Default_Initialization, Declaration_Node (E));
4444 end if;
4446 -- Ensure that a variable subject to pragma Thread_Local_Storage
4448 -- * Lacks default initialization, or
4450 -- * The initialization expression is either "null", a static
4451 -- constant, or a compile-time known aggregate.
4453 if Has_Pragma_Thread_Local_Storage (E) then
4454 Check_Pragma_Thread_Local_Storage (E);
4455 end if;
4457 -- For imported objects, set Is_Public unless there is also an
4458 -- address clause, which means that there is no external symbol
4459 -- needed for the Import (Is_Public may still be set for other
4460 -- unrelated reasons). Note that we delayed this processing
4461 -- till freeze time so that we can be sure not to set the flag
4462 -- if there is an address clause. If there is such a clause,
4463 -- then the only purpose of the Import pragma is to suppress
4464 -- implicit initialization.
4466 if Is_Imported (E) and then No (Address_Clause (E)) then
4467 Set_Is_Public (E);
4468 end if;
4470 -- For source objects that are not Imported and are library level, if
4471 -- no linker section pragma was given inherit the appropriate linker
4472 -- section from the corresponding type.
4474 if Comes_From_Source (E)
4475 and then not Is_Imported (E)
4476 and then Is_Library_Level_Entity (E)
4477 and then No (Linker_Section_Pragma (E))
4478 then
4479 Set_Linker_Section_Pragma (E, Linker_Section_Pragma (Typ));
4480 end if;
4482 -- For convention C objects of an enumeration type, warn if the size
4483 -- is not integer size and no explicit size given. Skip warning for
4484 -- Boolean and Character, and assume programmer expects 8-bit sizes
4485 -- for these cases.
4487 if (Convention (E) = Convention_C
4488 or else
4489 Convention (E) = Convention_CPP)
4490 and then Is_Enumeration_Type (Typ)
4491 and then not Is_Character_Type (Typ)
4492 and then not Is_Boolean_Type (Typ)
4493 and then Esize (Typ) < Standard_Integer_Size
4494 and then not Has_Size_Clause (E)
4495 then
4496 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
4497 Error_Msg_N
4498 ("??convention C enumeration object has size less than ^", E);
4499 Error_Msg_N ("\??use explicit size clause to set size", E);
4500 end if;
4502 -- Declaring too big an array in disabled ghost code is OK
4504 if Is_Array_Type (Typ) and then not Is_Ignored_Ghost_Entity (E) then
4505 Check_Large_Modular_Array (Typ);
4506 end if;
4507 end Freeze_Object_Declaration;
4509 -----------------------------
4510 -- Freeze_Generic_Entities --
4511 -----------------------------
4513 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
4514 E : Entity_Id;
4515 F : Node_Id;
4516 Flist : List_Id;
4518 begin
4519 Flist := New_List;
4520 E := First_Entity (Pack);
4521 while Present (E) loop
4522 if Is_Type (E) and then not Is_Generic_Type (E) then
4523 F := Make_Freeze_Generic_Entity (Sloc (Pack));
4524 Set_Entity (F, E);
4525 Append_To (Flist, F);
4527 elsif Ekind (E) = E_Generic_Package then
4528 Append_List_To (Flist, Freeze_Generic_Entities (E));
4529 end if;
4531 Next_Entity (E);
4532 end loop;
4534 return Flist;
4535 end Freeze_Generic_Entities;
4537 --------------------
4538 -- Freeze_Profile --
4539 --------------------
4541 function Freeze_Profile (E : Entity_Id) return Boolean is
4542 F_Type : Entity_Id;
4543 R_Type : Entity_Id;
4544 Warn_Node : Node_Id;
4546 begin
4547 -- Loop through formals
4549 Formal := First_Formal (E);
4550 while Present (Formal) loop
4551 F_Type := Etype (Formal);
4553 -- AI05-0151: incomplete types can appear in a profile. By the
4554 -- time the entity is frozen, the full view must be available,
4555 -- unless it is a limited view.
4557 if Is_Incomplete_Type (F_Type)
4558 and then Present (Full_View (F_Type))
4559 and then not From_Limited_With (F_Type)
4560 then
4561 F_Type := Full_View (F_Type);
4562 Set_Etype (Formal, F_Type);
4563 end if;
4565 if not From_Limited_With (F_Type)
4566 and then Should_Freeze_Type (F_Type, E, N)
4567 then
4568 Freeze_And_Append (F_Type, N, Result);
4569 end if;
4571 if Is_Private_Type (F_Type)
4572 and then Is_Private_Type (Base_Type (F_Type))
4573 and then No (Full_View (Base_Type (F_Type)))
4574 and then not Is_Generic_Type (F_Type)
4575 and then not Is_Derived_Type (F_Type)
4576 then
4577 -- If the type of a formal is incomplete, subprogram is being
4578 -- frozen prematurely. Within an instance (but not within a
4579 -- wrapper package) this is an artifact of our need to regard
4580 -- the end of an instantiation as a freeze point. Otherwise it
4581 -- is a definite error.
4583 if In_Instance then
4584 Set_Is_Frozen (E, False);
4585 Result := No_List;
4586 return False;
4588 elsif not After_Last_Declaration then
4589 Error_Msg_NE
4590 ("type & must be fully defined before this point",
4592 F_Type);
4593 end if;
4594 end if;
4596 -- Check suspicious parameter for C function. These tests apply
4597 -- only to exported/imported subprograms.
4599 if Warn_On_Export_Import
4600 and then Comes_From_Source (E)
4601 and then Convention (E) in Convention_C_Family
4602 and then (Is_Imported (E) or else Is_Exported (E))
4603 and then Convention (E) /= Convention (Formal)
4604 and then not Has_Warnings_Off (E)
4605 and then not Has_Warnings_Off (F_Type)
4606 and then not Has_Warnings_Off (Formal)
4607 then
4608 -- Qualify mention of formals with subprogram name
4610 Error_Msg_Qual_Level := 1;
4612 -- Check suspicious use of fat C pointer, but do not emit
4613 -- a warning on an access to subprogram when unnesting is
4614 -- active.
4616 if Is_Access_Type (F_Type)
4617 and then Known_Esize (F_Type)
4618 and then Esize (F_Type) > Ttypes.System_Address_Size
4619 and then (not Unnest_Subprogram_Mode
4620 or else not Is_Access_Subprogram_Type (F_Type))
4621 then
4622 Error_Msg_N
4623 ("?x?type of & does not correspond to C pointer!", Formal);
4625 -- Check suspicious return of boolean
4627 elsif Root_Type (F_Type) = Standard_Boolean
4628 and then Convention (F_Type) = Convention_Ada
4629 and then not Has_Warnings_Off (F_Type)
4630 and then not Has_Size_Clause (F_Type)
4631 then
4632 Error_Msg_N
4633 ("& is an 8-bit Ada Boolean?x?", Formal);
4634 Error_Msg_N
4635 ("\use appropriate corresponding type in C "
4636 & "(e.g. char)?x?", Formal);
4638 -- Check suspicious tagged type
4640 elsif (Is_Tagged_Type (F_Type)
4641 or else
4642 (Is_Access_Type (F_Type)
4643 and then Is_Tagged_Type (Designated_Type (F_Type))))
4644 and then Convention (E) = Convention_C
4645 then
4646 Error_Msg_N
4647 ("?x?& involves a tagged type which does not "
4648 & "correspond to any C type!", Formal);
4650 -- Check wrong convention subprogram pointer
4652 elsif Ekind (F_Type) = E_Access_Subprogram_Type
4653 and then not Has_Foreign_Convention (F_Type)
4654 then
4655 Error_Msg_N
4656 ("?x?subprogram pointer & should "
4657 & "have foreign convention!", Formal);
4658 Error_Msg_Sloc := Sloc (F_Type);
4659 Error_Msg_NE
4660 ("\?x?add Convention pragma to declaration of &#",
4661 Formal, F_Type);
4662 end if;
4664 -- Turn off name qualification after message output
4666 Error_Msg_Qual_Level := 0;
4667 end if;
4669 -- Check for unconstrained array in exported foreign convention
4670 -- case.
4672 if Has_Foreign_Convention (E)
4673 and then not Is_Imported (E)
4674 and then Is_Array_Type (F_Type)
4675 and then not Is_Constrained (F_Type)
4676 and then Warn_On_Export_Import
4677 then
4678 Error_Msg_Qual_Level := 1;
4680 -- If this is an inherited operation, place the warning on
4681 -- the derived type declaration, rather than on the original
4682 -- subprogram.
4684 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
4685 then
4686 Warn_Node := Parent (E);
4688 if Formal = First_Formal (E) then
4689 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
4690 end if;
4691 else
4692 Warn_Node := Formal;
4693 end if;
4695 Error_Msg_NE ("?x?type of argument& is unconstrained array",
4696 Warn_Node, Formal);
4697 Error_Msg_N ("\?x?foreign caller must pass bounds explicitly",
4698 Warn_Node);
4699 Error_Msg_Qual_Level := 0;
4700 end if;
4702 if not From_Limited_With (F_Type) then
4703 if Is_Access_Type (F_Type) then
4704 F_Type := Designated_Type (F_Type);
4705 end if;
4706 end if;
4708 Next_Formal (Formal);
4709 end loop;
4711 -- Case of function: similar checks on return type
4713 if Ekind (E) = E_Function then
4715 -- Freeze return type
4717 R_Type := Etype (E);
4719 -- AI05-0151: the return type may have been incomplete at the
4720 -- point of declaration. Replace it with the full view, unless the
4721 -- current type is a limited view. In that case the full view is
4722 -- in a different unit, and gigi finds the non-limited view after
4723 -- the other unit is elaborated.
4725 if Ekind (R_Type) = E_Incomplete_Type
4726 and then Present (Full_View (R_Type))
4727 and then not From_Limited_With (R_Type)
4728 then
4729 R_Type := Full_View (R_Type);
4730 Set_Etype (E, R_Type);
4731 end if;
4733 if Should_Freeze_Type (R_Type, E, N) then
4734 Freeze_And_Append (R_Type, N, Result);
4735 end if;
4737 -- Check suspicious return type for C function
4739 if Warn_On_Export_Import
4740 and then Comes_From_Source (E)
4741 and then Convention (E) in Convention_C_Family
4742 and then (Is_Imported (E) or else Is_Exported (E))
4743 then
4744 -- Check suspicious return of fat C pointer
4746 if Is_Access_Type (R_Type)
4747 and then Known_Esize (R_Type)
4748 and then Esize (R_Type) > Ttypes.System_Address_Size
4749 and then not Has_Warnings_Off (E)
4750 and then not Has_Warnings_Off (R_Type)
4751 then
4752 Error_Msg_N
4753 ("?x?return type of& does not correspond to C pointer!",
4756 -- Check suspicious return of boolean
4758 elsif Root_Type (R_Type) = Standard_Boolean
4759 and then Convention (R_Type) = Convention_Ada
4760 and then not Has_Warnings_Off (E)
4761 and then not Has_Warnings_Off (R_Type)
4762 and then not Has_Size_Clause (R_Type)
4763 then
4764 declare
4765 N : constant Node_Id :=
4766 Result_Definition (Declaration_Node (E));
4767 begin
4768 Error_Msg_NE
4769 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
4770 Error_Msg_NE
4771 ("\use appropriate corresponding type in C "
4772 & "(e.g. char)?x?", N, E);
4773 end;
4775 -- Check suspicious return tagged type
4777 elsif (Is_Tagged_Type (R_Type)
4778 or else (Is_Access_Type (R_Type)
4779 and then
4780 Is_Tagged_Type
4781 (Designated_Type (R_Type))))
4782 and then Convention (E) = Convention_C
4783 and then not Has_Warnings_Off (E)
4784 and then not Has_Warnings_Off (R_Type)
4785 then
4786 Error_Msg_N ("?x?return type of & does not "
4787 & "correspond to C type!", E);
4789 -- Check return of wrong convention subprogram pointer
4791 elsif Ekind (R_Type) = E_Access_Subprogram_Type
4792 and then not Has_Foreign_Convention (R_Type)
4793 and then not Has_Warnings_Off (E)
4794 and then not Has_Warnings_Off (R_Type)
4795 then
4796 Error_Msg_N ("?x?& should return a foreign "
4797 & "convention subprogram pointer", E);
4798 Error_Msg_Sloc := Sloc (R_Type);
4799 Error_Msg_NE
4800 ("\?x?add Convention pragma to declaration of& #",
4801 E, R_Type);
4802 end if;
4803 end if;
4805 -- Give warning for suspicious return of a result of an
4806 -- unconstrained array type in a foreign convention function.
4808 if Has_Foreign_Convention (E)
4810 -- We are looking for a return of unconstrained array
4812 and then Is_Array_Type (R_Type)
4813 and then not Is_Constrained (R_Type)
4815 -- Exclude imported routines, the warning does not belong on
4816 -- the import, but rather on the routine definition.
4818 and then not Is_Imported (E)
4820 -- Check that general warning is enabled, and that it is not
4821 -- suppressed for this particular case.
4823 and then Warn_On_Export_Import
4824 and then not Has_Warnings_Off (E)
4825 and then not Has_Warnings_Off (R_Type)
4826 then
4827 Error_Msg_N
4828 ("?x?foreign convention function& should not return "
4829 & "unconstrained array!", E);
4830 end if;
4831 end if;
4833 -- Check suspicious use of Import in pure unit (cases where the RM
4834 -- allows calls to be omitted).
4836 if Is_Imported (E)
4838 -- It might be suspicious if the compilation unit has the Pure
4839 -- aspect/pragma.
4841 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
4843 -- The RM allows omission of calls only in the case of
4844 -- library-level subprograms (see RM-10.2.1(18)).
4846 and then Is_Library_Level_Entity (E)
4848 -- Ignore internally generated entity. This happens in some cases
4849 -- of subprograms in specs, where we generate an implied body.
4851 and then Comes_From_Source (Import_Pragma (E))
4853 -- Assume run-time knows what it is doing
4855 and then not GNAT_Mode
4857 -- Assume explicit Pure_Function means import is pure
4859 and then not Has_Pragma_Pure_Function (E)
4861 -- Don't need warning in relaxed semantics mode
4863 and then not Relaxed_RM_Semantics
4865 -- Assume convention Intrinsic is OK, since this is specialized.
4866 -- This deals with the DEC unit current_exception.ads
4868 and then Convention (E) /= Convention_Intrinsic
4870 -- Assume that ASM interface knows what it is doing
4872 and then Convention (E) /= Convention_Assembler
4873 then
4874 Error_Msg_N
4875 ("pragma Import in Pure unit??", Import_Pragma (E));
4876 Error_Msg_NE
4877 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
4878 Import_Pragma (E), E);
4879 end if;
4881 return True;
4882 end Freeze_Profile;
4884 ------------------------
4885 -- Freeze_Record_Type --
4886 ------------------------
4888 procedure Freeze_Record_Type (Rec : Entity_Id) is
4889 ADC : Node_Id;
4890 Comp : Entity_Id;
4891 IR : Node_Id;
4892 Prev : Entity_Id;
4894 Junk : Boolean;
4895 pragma Warnings (Off, Junk);
4897 Aliased_Component : Boolean := False;
4898 -- Set True if we find at least one component which is aliased. This
4899 -- is used to prevent Implicit_Packing of the record, since packing
4900 -- cannot modify the size of alignment of an aliased component.
4902 All_Elem_Components : Boolean := True;
4903 -- True if all components are of a type whose underlying type is
4904 -- elementary.
4906 All_Sized_Components : Boolean := True;
4907 -- True if all components have a known RM_Size
4909 All_Storage_Unit_Components : Boolean := True;
4910 -- True if all components have an RM_Size that is a multiple of the
4911 -- storage unit.
4913 Elem_Component_Total_Esize : Uint := Uint_0;
4914 -- Accumulates total Esize values of all elementary components. Used
4915 -- for processing of Implicit_Packing.
4917 Placed_Component : Boolean := False;
4918 -- Set True if we find at least one component with a component
4919 -- clause (used to warn about useless Bit_Order pragmas, and also
4920 -- to detect cases where Implicit_Packing may have an effect).
4922 Sized_Component_Total_RM_Size : Uint := Uint_0;
4923 -- Accumulates total RM_Size values of all sized components. Used
4924 -- for processing of Implicit_Packing.
4926 Sized_Component_Total_Round_RM_Size : Uint := Uint_0;
4927 -- Accumulates total RM_Size values of all sized components, rounded
4928 -- individually to a multiple of the storage unit.
4930 SSO_ADC : Node_Id;
4931 -- Scalar_Storage_Order attribute definition clause for the record
4933 SSO_ADC_Component : Boolean := False;
4934 -- Set True if we find at least one component whose type has a
4935 -- Scalar_Storage_Order attribute definition clause.
4937 Unplaced_Component : Boolean := False;
4938 -- Set True if we find at least one component with no component
4939 -- clause (used to warn about useless Pack pragmas).
4941 procedure Check_Itype (Typ : Entity_Id);
4942 -- If the component subtype is an access to a constrained subtype of
4943 -- an already frozen type, make the subtype frozen as well. It might
4944 -- otherwise be frozen in the wrong scope, and a freeze node on
4945 -- subtype has no effect. Similarly, if the component subtype is a
4946 -- regular (not protected) access to subprogram, set the anonymous
4947 -- subprogram type to frozen as well, to prevent an out-of-scope
4948 -- freeze node at some eventual point of call. Protected operations
4949 -- are handled elsewhere.
4951 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
4952 -- Make sure that all types mentioned in Discrete_Choices of the
4953 -- variants referenceed by the Variant_Part VP are frozen. This is
4954 -- a recursive routine to deal with nested variants.
4956 -----------------
4957 -- Check_Itype --
4958 -----------------
4960 procedure Check_Itype (Typ : Entity_Id) is
4961 Desig : constant Entity_Id := Designated_Type (Typ);
4963 begin
4964 if not Is_Frozen (Desig)
4965 and then Is_Frozen (Base_Type (Desig))
4966 then
4967 Set_Is_Frozen (Desig);
4969 -- In addition, add an Itype_Reference to ensure that the
4970 -- access subtype is elaborated early enough. This cannot be
4971 -- done if the subtype may depend on discriminants.
4973 if Ekind (Comp) = E_Component
4974 and then Is_Itype (Etype (Comp))
4975 and then not Has_Discriminants (Rec)
4976 then
4977 IR := Make_Itype_Reference (Sloc (Comp));
4978 Set_Itype (IR, Desig);
4979 Add_To_Result (IR);
4980 end if;
4982 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
4983 and then Convention (Desig) /= Convention_Protected
4984 then
4985 Set_Is_Frozen (Desig);
4986 Create_Extra_Formals (Desig);
4987 end if;
4988 end Check_Itype;
4990 ------------------------------------
4991 -- Freeze_Choices_In_Variant_Part --
4992 ------------------------------------
4994 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
4995 pragma Assert (Nkind (VP) = N_Variant_Part);
4997 Variant : Node_Id;
4998 Choice : Node_Id;
4999 CL : Node_Id;
5001 begin
5002 -- Loop through variants
5004 Variant := First_Non_Pragma (Variants (VP));
5005 while Present (Variant) loop
5007 -- Loop through choices, checking that all types are frozen
5009 Choice := First_Non_Pragma (Discrete_Choices (Variant));
5010 while Present (Choice) loop
5011 if Nkind (Choice) in N_Has_Etype
5012 and then Present (Etype (Choice))
5013 then
5014 Freeze_And_Append (Etype (Choice), N, Result);
5015 end if;
5017 Next_Non_Pragma (Choice);
5018 end loop;
5020 -- Check for nested variant part to process
5022 CL := Component_List (Variant);
5024 if not Null_Present (CL) then
5025 if Present (Variant_Part (CL)) then
5026 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
5027 end if;
5028 end if;
5030 Next_Non_Pragma (Variant);
5031 end loop;
5032 end Freeze_Choices_In_Variant_Part;
5034 -- Start of processing for Freeze_Record_Type
5036 begin
5037 -- Freeze components and embedded subtypes
5039 Comp := First_Entity (Rec);
5040 Prev := Empty;
5041 while Present (Comp) loop
5042 if Is_Aliased (Comp) then
5043 Aliased_Component := True;
5044 end if;
5046 -- Handle the component and discriminant case
5048 if Ekind (Comp) in E_Component | E_Discriminant then
5049 declare
5050 CC : constant Node_Id := Component_Clause (Comp);
5052 begin
5053 -- Freezing a record type freezes the type of each of its
5054 -- components. However, if the type of the component is
5055 -- part of this record, we do not want or need a separate
5056 -- Freeze_Node. Note that Is_Itype is wrong because that's
5057 -- also set in private type cases. We also can't check for
5058 -- the Scope being exactly Rec because of private types and
5059 -- record extensions.
5061 if Is_Itype (Etype (Comp))
5062 and then Is_Record_Type (Underlying_Type
5063 (Scope (Etype (Comp))))
5064 then
5065 Undelay_Type (Etype (Comp));
5066 end if;
5068 Freeze_And_Append (Etype (Comp), N, Result);
5070 -- Warn for pragma Pack overriding foreign convention
5072 if Has_Foreign_Convention (Etype (Comp))
5073 and then Has_Pragma_Pack (Rec)
5075 -- Don't warn for aliased components, since override
5076 -- cannot happen in that case.
5078 and then not Is_Aliased (Comp)
5079 then
5080 declare
5081 CN : constant Name_Id :=
5082 Get_Convention_Name (Convention (Etype (Comp)));
5083 PP : constant Node_Id :=
5084 Get_Pragma (Rec, Pragma_Pack);
5085 begin
5086 if Present (PP) then
5087 Error_Msg_Name_1 := CN;
5088 Error_Msg_Sloc := Sloc (Comp);
5089 Error_Msg_N
5090 ("pragma Pack affects convention % component#??",
5091 PP);
5092 Error_Msg_Name_1 := CN;
5093 Error_Msg_NE
5094 ("\component & may not have % compatible "
5095 & "representation??", PP, Comp);
5096 end if;
5097 end;
5098 end if;
5100 -- Check for error of component clause given for variable
5101 -- sized type. We have to delay this test till this point,
5102 -- since the component type has to be frozen for us to know
5103 -- if it is variable length.
5105 if Present (CC) then
5106 Placed_Component := True;
5108 -- We omit this test in a generic context, it will be
5109 -- applied at instantiation time.
5111 if Inside_A_Generic then
5112 null;
5114 -- Also omit this test in CodePeer mode, since we do not
5115 -- have sufficient info on size and rep clauses.
5117 elsif CodePeer_Mode then
5118 null;
5120 -- Do the check
5122 elsif not
5123 Size_Known_At_Compile_Time
5124 (Underlying_Type (Etype (Comp)))
5125 then
5126 Error_Msg_N
5127 ("component clause not allowed for variable " &
5128 "length component", CC);
5129 end if;
5131 else
5132 Unplaced_Component := True;
5133 end if;
5135 -- Case of component requires byte alignment
5137 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
5139 -- Set the enclosing record to also require byte align
5141 Set_Must_Be_On_Byte_Boundary (Rec);
5143 -- Check for component clause that is inconsistent with
5144 -- the required byte boundary alignment.
5146 if Present (CC)
5147 and then Normalized_First_Bit (Comp) mod
5148 System_Storage_Unit /= 0
5149 then
5150 Error_Msg_N
5151 ("component & must be byte aligned",
5152 Component_Name (Component_Clause (Comp)));
5153 end if;
5154 end if;
5155 end;
5156 end if;
5158 -- Gather data for possible Implicit_Packing later. Note that at
5159 -- this stage we might be dealing with a real component, or with
5160 -- an implicit subtype declaration.
5162 if Known_Static_RM_Size (Etype (Comp)) then
5163 declare
5164 Comp_Type : constant Entity_Id := Etype (Comp);
5165 Comp_Size : constant Uint := RM_Size (Comp_Type);
5166 SSU : constant Int := Ttypes.System_Storage_Unit;
5168 begin
5169 Sized_Component_Total_RM_Size :=
5170 Sized_Component_Total_RM_Size + Comp_Size;
5172 Sized_Component_Total_Round_RM_Size :=
5173 Sized_Component_Total_Round_RM_Size +
5174 (Comp_Size + SSU - 1) / SSU * SSU;
5176 if Present (Underlying_Type (Comp_Type))
5177 and then Is_Elementary_Type (Underlying_Type (Comp_Type))
5178 then
5179 Elem_Component_Total_Esize :=
5180 Elem_Component_Total_Esize + Esize (Comp_Type);
5181 else
5182 All_Elem_Components := False;
5184 if Comp_Size mod SSU /= 0 then
5185 All_Storage_Unit_Components := False;
5186 end if;
5187 end if;
5188 end;
5189 else
5190 All_Sized_Components := False;
5191 end if;
5193 -- If the component is an Itype with Delayed_Freeze and is either
5194 -- a record or array subtype and its base type has not yet been
5195 -- frozen, we must remove this from the entity list of this record
5196 -- and put it on the entity list of the scope of its base type.
5197 -- Note that we know that this is not the type of a component
5198 -- since we cleared Has_Delayed_Freeze for it in the previous
5199 -- loop. Thus this must be the Designated_Type of an access type,
5200 -- which is the type of a component.
5202 if Is_Itype (Comp)
5203 and then Is_Type (Scope (Comp))
5204 and then Is_Composite_Type (Comp)
5205 and then Base_Type (Comp) /= Comp
5206 and then Has_Delayed_Freeze (Comp)
5207 and then not Is_Frozen (Base_Type (Comp))
5208 then
5209 declare
5210 Will_Be_Frozen : Boolean := False;
5211 S : Entity_Id;
5213 begin
5214 -- We have a difficult case to handle here. Suppose Rec is
5215 -- subtype being defined in a subprogram that's created as
5216 -- part of the freezing of Rec'Base. In that case, we know
5217 -- that Comp'Base must have already been frozen by the time
5218 -- we get to elaborate this because Gigi doesn't elaborate
5219 -- any bodies until it has elaborated all of the declarative
5220 -- part. But Is_Frozen will not be set at this point because
5221 -- we are processing code in lexical order.
5223 -- We detect this case by going up the Scope chain of Rec
5224 -- and seeing if we have a subprogram scope before reaching
5225 -- the top of the scope chain or that of Comp'Base. If we
5226 -- do, then mark that Comp'Base will actually be frozen. If
5227 -- so, we merely undelay it.
5229 S := Scope (Rec);
5230 while Present (S) loop
5231 if Is_Subprogram (S) then
5232 Will_Be_Frozen := True;
5233 exit;
5234 elsif S = Scope (Base_Type (Comp)) then
5235 exit;
5236 end if;
5238 S := Scope (S);
5239 end loop;
5241 if Will_Be_Frozen then
5242 Undelay_Type (Comp);
5244 else
5245 if Present (Prev) then
5246 Link_Entities (Prev, Next_Entity (Comp));
5247 else
5248 Set_First_Entity (Rec, Next_Entity (Comp));
5249 end if;
5251 -- Insert in entity list of scope of base type (which
5252 -- must be an enclosing scope, because still unfrozen).
5254 Append_Entity (Comp, Scope (Base_Type (Comp)));
5255 end if;
5256 end;
5258 -- If the component is an access type with an allocator as default
5259 -- value, the designated type will be frozen by the corresponding
5260 -- expression in init_proc. In order to place the freeze node for
5261 -- the designated type before that for the current record type,
5262 -- freeze it now.
5264 -- Same process if the component is an array of access types,
5265 -- initialized with an aggregate. If the designated type is
5266 -- private, it cannot contain allocators, and it is premature
5267 -- to freeze the type, so we check for this as well.
5269 elsif Is_Access_Type (Etype (Comp))
5270 and then Present (Parent (Comp))
5271 and then
5272 Nkind (Parent (Comp))
5273 in N_Component_Declaration | N_Discriminant_Specification
5274 and then Present (Expression (Parent (Comp)))
5275 then
5276 declare
5277 Alloc : constant Node_Id :=
5278 Unqualify (Expression (Parent (Comp)));
5280 begin
5281 if Nkind (Alloc) = N_Allocator then
5283 -- If component is pointer to a class-wide type, freeze
5284 -- the specific type in the expression being allocated.
5285 -- The expression may be a subtype indication, in which
5286 -- case freeze the subtype mark.
5288 if Is_Class_Wide_Type (Designated_Type (Etype (Comp)))
5289 then
5290 if Is_Entity_Name (Expression (Alloc)) then
5291 Freeze_And_Append
5292 (Entity (Expression (Alloc)), N, Result);
5294 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
5295 then
5296 Freeze_And_Append
5297 (Entity (Subtype_Mark (Expression (Alloc))),
5298 N, Result);
5299 end if;
5300 elsif Is_Itype (Designated_Type (Etype (Comp))) then
5301 Check_Itype (Etype (Comp));
5302 else
5303 Freeze_And_Append
5304 (Designated_Type (Etype (Comp)), N, Result);
5305 end if;
5306 end if;
5307 end;
5308 elsif Is_Access_Type (Etype (Comp))
5309 and then Is_Itype (Designated_Type (Etype (Comp)))
5310 then
5311 Check_Itype (Etype (Comp));
5313 -- Freeze the designated type when initializing a component with
5314 -- an aggregate in case the aggregate contains allocators.
5316 -- type T is ...;
5317 -- type T_Ptr is access all T;
5318 -- type T_Array is array ... of T_Ptr;
5320 -- type Rec is record
5321 -- Comp : T_Array := (others => ...);
5322 -- end record;
5324 elsif Is_Array_Type (Etype (Comp))
5325 and then Is_Access_Type (Component_Type (Etype (Comp)))
5326 then
5327 declare
5328 Comp_Par : constant Node_Id := Parent (Comp);
5329 Desig_Typ : constant Entity_Id :=
5330 Designated_Type
5331 (Component_Type (Etype (Comp)));
5333 begin
5334 -- The only case when this sort of freezing is not done is
5335 -- when the designated type is class-wide and the root type
5336 -- is the record owning the component. This scenario results
5337 -- in a circularity because the class-wide type requires
5338 -- primitives that have not been created yet as the root
5339 -- type is in the process of being frozen.
5341 -- type Rec is tagged;
5342 -- type Rec_Ptr is access all Rec'Class;
5343 -- type Rec_Array is array ... of Rec_Ptr;
5345 -- type Rec is record
5346 -- Comp : Rec_Array := (others => ...);
5347 -- end record;
5349 if Is_Class_Wide_Type (Desig_Typ)
5350 and then Root_Type (Desig_Typ) = Rec
5351 then
5352 null;
5354 elsif Is_Fully_Defined (Desig_Typ)
5355 and then Present (Comp_Par)
5356 and then Nkind (Comp_Par) = N_Component_Declaration
5357 and then Present (Expression (Comp_Par))
5358 and then Nkind (Expression (Comp_Par)) = N_Aggregate
5359 then
5360 Freeze_And_Append (Desig_Typ, N, Result);
5361 end if;
5362 end;
5363 end if;
5365 Prev := Comp;
5366 Next_Entity (Comp);
5367 end loop;
5369 SSO_ADC :=
5370 Get_Attribute_Definition_Clause
5371 (Rec, Attribute_Scalar_Storage_Order);
5373 -- If the record type has Complex_Representation, then it is treated
5374 -- as a scalar in the back end so the storage order is irrelevant.
5376 if Has_Complex_Representation (Rec) then
5377 if Present (SSO_ADC) then
5378 Error_Msg_N
5379 ("??storage order has no effect with Complex_Representation",
5380 SSO_ADC);
5381 end if;
5383 else
5384 -- Deal with default setting of reverse storage order
5386 Set_SSO_From_Default (Rec);
5388 -- Check consistent attribute setting on component types
5390 declare
5391 Comp_ADC_Present : Boolean;
5392 begin
5393 Comp := First_Component (Rec);
5394 while Present (Comp) loop
5395 Check_Component_Storage_Order
5396 (Encl_Type => Rec,
5397 Comp => Comp,
5398 ADC => SSO_ADC,
5399 Comp_ADC_Present => Comp_ADC_Present);
5400 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
5401 Next_Component (Comp);
5402 end loop;
5403 end;
5405 -- Now deal with reverse storage order/bit order issues
5407 if Present (SSO_ADC) then
5409 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
5410 -- if the former is specified.
5412 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
5414 -- Note: report error on Rec, not on SSO_ADC, as ADC may
5415 -- apply to some ancestor type.
5417 Error_Msg_Sloc := Sloc (SSO_ADC);
5418 Error_Msg_N
5419 ("scalar storage order for& specified# inconsistent with "
5420 & "bit order", Rec);
5421 end if;
5423 -- Warn if there is a Scalar_Storage_Order attribute definition
5424 -- clause but no component clause, no component that itself has
5425 -- such an attribute definition, and no pragma Pack.
5427 if not (Placed_Component
5428 or else
5429 SSO_ADC_Component
5430 or else
5431 Is_Packed (Rec))
5432 then
5433 Error_Msg_N
5434 ("??scalar storage order specified but no component "
5435 & "clause", SSO_ADC);
5436 end if;
5437 end if;
5438 end if;
5440 -- Deal with Bit_Order aspect
5442 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
5444 if Present (ADC) and then Base_Type (Rec) = Rec then
5445 if not (Placed_Component
5446 or else Present (SSO_ADC)
5447 or else Is_Packed (Rec))
5448 then
5449 -- Warn if clause has no effect when no component clause is
5450 -- present, but suppress warning if the Bit_Order is required
5451 -- due to the presence of a Scalar_Storage_Order attribute.
5453 Error_Msg_N
5454 ("??bit order specification has no effect", ADC);
5455 Error_Msg_N
5456 ("\??since no component clauses were specified", ADC);
5458 -- Here is where we do the processing to adjust component clauses
5459 -- for reversed bit order, when not using reverse SSO. If an error
5460 -- has been reported on Rec already (such as SSO incompatible with
5461 -- bit order), don't bother adjusting as this may generate extra
5462 -- noise.
5464 elsif Reverse_Bit_Order (Rec)
5465 and then not Reverse_Storage_Order (Rec)
5466 and then not Error_Posted (Rec)
5467 then
5468 Adjust_Record_For_Reverse_Bit_Order (Rec);
5470 -- Case where we have both an explicit Bit_Order and the same
5471 -- Scalar_Storage_Order: leave record untouched, the back-end
5472 -- will take care of required layout conversions.
5474 else
5475 null;
5477 end if;
5478 end if;
5480 -- Check for useless pragma Pack when all components placed. We only
5481 -- do this check for record types, not subtypes, since a subtype may
5482 -- have all its components placed, and it still makes perfectly good
5483 -- sense to pack other subtypes or the parent type. We do not give
5484 -- this warning if Optimize_Alignment is set to Space, since the
5485 -- pragma Pack does have an effect in this case (it always resets
5486 -- the alignment to one).
5488 if Ekind (Rec) = E_Record_Type
5489 and then Is_Packed (Rec)
5490 and then not Unplaced_Component
5491 and then Optimize_Alignment /= 'S'
5492 then
5493 -- Reset packed status. Probably not necessary, but we do it so
5494 -- that there is no chance of the back end doing something strange
5495 -- with this redundant indication of packing.
5497 Set_Is_Packed (Rec, False);
5499 -- Give warning if redundant constructs warnings on
5501 if Warn_On_Redundant_Constructs then
5502 Error_Msg_N -- CODEFIX
5503 ("??pragma Pack has no effect, no unplaced components",
5504 Get_Rep_Pragma (Rec, Name_Pack));
5505 end if;
5506 end if;
5508 -- If this is the record corresponding to a remote type, freeze the
5509 -- remote type here since that is what we are semantically freezing.
5510 -- This prevents the freeze node for that type in an inner scope.
5512 if Ekind (Rec) = E_Record_Type then
5513 if Present (Corresponding_Remote_Type (Rec)) then
5514 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
5515 end if;
5517 -- Check for controlled components, unchecked unions, and type
5518 -- invariants.
5520 Comp := First_Component (Rec);
5521 while Present (Comp) loop
5523 -- Do not set Has_Controlled_Component on a class-wide
5524 -- equivalent type. See Make_CW_Equivalent_Type.
5526 if not Is_Class_Wide_Equivalent_Type (Rec)
5527 and then
5528 (Has_Controlled_Component (Etype (Comp))
5529 or else
5530 (Chars (Comp) /= Name_uParent
5531 and then Is_Controlled (Etype (Comp)))
5532 or else
5533 (Is_Protected_Type (Etype (Comp))
5534 and then
5535 Present (Corresponding_Record_Type (Etype (Comp)))
5536 and then
5537 Has_Controlled_Component
5538 (Corresponding_Record_Type (Etype (Comp)))))
5539 then
5540 Set_Has_Controlled_Component (Rec);
5541 end if;
5543 if Has_Unchecked_Union (Etype (Comp)) then
5544 Set_Has_Unchecked_Union (Rec);
5545 end if;
5547 -- The record type requires its own invariant procedure in
5548 -- order to verify the invariant of each individual component.
5549 -- Do not consider internal components such as _parent because
5550 -- parent class-wide invariants are always inherited.
5551 -- In GNATprove mode, the component invariants are checked by
5552 -- other means. They should not be added to the record type
5553 -- invariant procedure, so that the procedure can be used to
5554 -- check the recordy type invariants if any.
5556 if Comes_From_Source (Comp)
5557 and then Has_Invariants (Etype (Comp))
5558 and then not GNATprove_Mode
5559 then
5560 Set_Has_Own_Invariants (Rec);
5561 end if;
5563 -- Scan component declaration for likely misuses of current
5564 -- instance, either in a constraint or a default expression.
5566 if Has_Per_Object_Constraint (Comp) then
5567 Check_Current_Instance (Parent (Comp));
5568 end if;
5570 Next_Component (Comp);
5571 end loop;
5572 end if;
5574 -- Enforce the restriction that access attributes with a current
5575 -- instance prefix can only apply to limited types. This comment
5576 -- is floating here, but does not seem to belong here???
5578 -- Set component alignment if not otherwise already set
5580 Set_Component_Alignment_If_Not_Set (Rec);
5582 -- For first subtypes, check if there are any fixed-point fields with
5583 -- component clauses, where we must check the size. This is not done
5584 -- till the freeze point since for fixed-point types, we do not know
5585 -- the size until the type is frozen. Similar processing applies to
5586 -- bit-packed arrays.
5588 if Is_First_Subtype (Rec) then
5589 Comp := First_Component (Rec);
5590 while Present (Comp) loop
5591 if Present (Component_Clause (Comp))
5592 and then (Is_Fixed_Point_Type (Etype (Comp))
5593 or else Is_Bit_Packed_Array (Etype (Comp)))
5594 then
5595 Check_Size
5596 (Component_Name (Component_Clause (Comp)),
5597 Etype (Comp),
5598 Esize (Comp),
5599 Junk);
5600 end if;
5602 Next_Component (Comp);
5603 end loop;
5604 end if;
5606 -- See if Size is too small as is (and implicit packing might help)
5608 if not Is_Packed (Rec)
5610 -- No implicit packing if even one component is explicitly placed
5612 and then not Placed_Component
5614 -- Or even one component is aliased
5616 and then not Aliased_Component
5618 -- Must have size clause and all sized components
5620 and then Has_Size_Clause (Rec)
5621 and then All_Sized_Components
5623 -- Do not try implicit packing on records with discriminants, too
5624 -- complicated, especially in the variant record case.
5626 and then not Has_Discriminants (Rec)
5628 -- We want to implicitly pack if the specified size of the record
5629 -- is less than the sum of the object sizes (no point in packing
5630 -- if this is not the case), if we can compute it, i.e. if we have
5631 -- only elementary components. Otherwise, we have at least one
5632 -- composite component and we want to implicitly pack only if bit
5633 -- packing is required for it, as we are sure in this case that
5634 -- the back end cannot do the expected layout without packing.
5636 and then
5637 ((All_Elem_Components
5638 and then RM_Size (Rec) < Elem_Component_Total_Esize)
5639 or else
5640 (not All_Elem_Components
5641 and then not All_Storage_Unit_Components
5642 and then RM_Size (Rec) < Sized_Component_Total_Round_RM_Size))
5644 -- And the total RM size cannot be greater than the specified size
5645 -- since otherwise packing will not get us where we have to be.
5647 and then Sized_Component_Total_RM_Size <= RM_Size (Rec)
5649 -- Never do implicit packing in CodePeer or SPARK modes since
5650 -- we don't do any packing in these modes, since this generates
5651 -- over-complex code that confuses static analysis, and in
5652 -- general, neither CodePeer not GNATprove care about the
5653 -- internal representation of objects.
5655 and then not (CodePeer_Mode or GNATprove_Mode)
5656 then
5657 -- If implicit packing enabled, do it
5659 if Implicit_Packing then
5660 Set_Is_Packed (Rec);
5662 -- Otherwise flag the size clause
5664 else
5665 declare
5666 Sz : constant Node_Id := Size_Clause (Rec);
5667 begin
5668 Error_Msg_NE -- CODEFIX
5669 ("size given for& too small", Sz, Rec);
5670 Error_Msg_N -- CODEFIX
5671 ("\use explicit pragma Pack "
5672 & "or use pragma Implicit_Packing", Sz);
5673 end;
5674 end if;
5675 end if;
5677 -- The following checks are relevant only when SPARK_Mode is on as
5678 -- they are not standard Ada legality rules.
5680 if SPARK_Mode = On then
5682 -- A discriminated type cannot be effectively volatile
5683 -- (SPARK RM 7.1.3(5)).
5685 if Is_Effectively_Volatile (Rec) then
5686 if Has_Discriminants (Rec) then
5687 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
5688 end if;
5690 -- A non-effectively volatile record type cannot contain
5691 -- effectively volatile components (SPARK RM 7.1.3(6)).
5693 else
5694 Comp := First_Component (Rec);
5695 while Present (Comp) loop
5696 if Comes_From_Source (Comp)
5697 and then Is_Effectively_Volatile (Etype (Comp))
5698 then
5699 Error_Msg_Name_1 := Chars (Rec);
5700 Error_Msg_N
5701 ("component & of non-volatile type % cannot be "
5702 & "volatile", Comp);
5703 end if;
5705 Next_Component (Comp);
5706 end loop;
5707 end if;
5709 -- A type which does not yield a synchronized object cannot have
5710 -- a component that yields a synchronized object (SPARK RM 9.5).
5712 if not Yields_Synchronized_Object (Rec) then
5713 Comp := First_Component (Rec);
5714 while Present (Comp) loop
5715 if Comes_From_Source (Comp)
5716 and then Yields_Synchronized_Object (Etype (Comp))
5717 then
5718 Error_Msg_Name_1 := Chars (Rec);
5719 Error_Msg_N
5720 ("component & of non-synchronized type % cannot be "
5721 & "synchronized", Comp);
5722 end if;
5724 Next_Component (Comp);
5725 end loop;
5726 end if;
5728 -- A Ghost type cannot have a component of protected or task type
5729 -- (SPARK RM 6.9(19)).
5731 if Is_Ghost_Entity (Rec) then
5732 Comp := First_Component (Rec);
5733 while Present (Comp) loop
5734 if Comes_From_Source (Comp)
5735 and then Is_Concurrent_Type (Etype (Comp))
5736 then
5737 Error_Msg_Name_1 := Chars (Rec);
5738 Error_Msg_N
5739 ("component & of ghost type % cannot be concurrent",
5740 Comp);
5741 end if;
5743 Next_Component (Comp);
5744 end loop;
5745 end if;
5746 end if;
5748 -- Make sure that if we have an iterator aspect, then we have
5749 -- either Constant_Indexing or Variable_Indexing.
5751 declare
5752 Iterator_Aspect : Node_Id;
5754 begin
5755 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
5757 if No (Iterator_Aspect) then
5758 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
5759 end if;
5761 if Present (Iterator_Aspect) then
5762 if Has_Aspect (Rec, Aspect_Constant_Indexing)
5763 or else
5764 Has_Aspect (Rec, Aspect_Variable_Indexing)
5765 then
5766 null;
5767 else
5768 Error_Msg_N
5769 ("Iterator_Element requires indexing aspect",
5770 Iterator_Aspect);
5771 end if;
5772 end if;
5773 end;
5775 -- All done if not a full record definition
5777 if Ekind (Rec) /= E_Record_Type then
5778 return;
5779 end if;
5781 -- Finally we need to check the variant part to make sure that
5782 -- all types within choices are properly frozen as part of the
5783 -- freezing of the record type.
5785 Check_Variant_Part : declare
5786 D : constant Node_Id := Declaration_Node (Rec);
5787 T : Node_Id;
5788 C : Node_Id;
5790 begin
5791 -- Find component list
5793 C := Empty;
5795 if Nkind (D) = N_Full_Type_Declaration then
5796 T := Type_Definition (D);
5798 if Nkind (T) = N_Record_Definition then
5799 C := Component_List (T);
5801 elsif Nkind (T) = N_Derived_Type_Definition
5802 and then Present (Record_Extension_Part (T))
5803 then
5804 C := Component_List (Record_Extension_Part (T));
5805 end if;
5806 end if;
5808 -- Case of variant part present
5810 if Present (C) and then Present (Variant_Part (C)) then
5811 Freeze_Choices_In_Variant_Part (Variant_Part (C));
5812 end if;
5814 -- Note: we used to call Check_Choices here, but it is too early,
5815 -- since predicated subtypes are frozen here, but their freezing
5816 -- actions are in Analyze_Freeze_Entity, which has not been called
5817 -- yet for entities frozen within this procedure, so we moved that
5818 -- call to the Analyze_Freeze_Entity for the record type.
5820 end Check_Variant_Part;
5822 -- Check that all the primitives of an interface type are abstract
5823 -- or null procedures.
5825 if Is_Interface (Rec)
5826 and then not Error_Posted (Parent (Rec))
5827 then
5828 declare
5829 Elmt : Elmt_Id;
5830 Subp : Entity_Id;
5832 begin
5833 Elmt := First_Elmt (Primitive_Operations (Rec));
5834 while Present (Elmt) loop
5835 Subp := Node (Elmt);
5837 if not Is_Abstract_Subprogram (Subp)
5839 -- Avoid reporting the error on inherited primitives
5841 and then Comes_From_Source (Subp)
5842 then
5843 Error_Msg_Name_1 := Chars (Subp);
5845 if Ekind (Subp) = E_Procedure then
5846 if not Null_Present (Parent (Subp)) then
5847 Error_Msg_N
5848 ("interface procedure % must be abstract or null",
5849 Parent (Subp));
5850 end if;
5851 else
5852 Error_Msg_N
5853 ("interface function % must be abstract",
5854 Parent (Subp));
5855 end if;
5856 end if;
5858 Next_Elmt (Elmt);
5859 end loop;
5860 end;
5861 end if;
5863 -- For a derived tagged type, check whether inherited primitives
5864 -- might require a wrapper to handle class-wide conditions.
5866 if Is_Tagged_Type (Rec) and then Is_Derived_Type (Rec) then
5867 Check_Inherited_Conditions (Rec);
5868 end if;
5869 end Freeze_Record_Type;
5871 -------------------------------
5872 -- Has_Boolean_Aspect_Import --
5873 -------------------------------
5875 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
5876 Decl : constant Node_Id := Declaration_Node (E);
5877 Asp : Node_Id;
5878 Expr : Node_Id;
5880 begin
5881 if Has_Aspects (Decl) then
5882 Asp := First (Aspect_Specifications (Decl));
5883 while Present (Asp) loop
5884 Expr := Expression (Asp);
5886 -- The value of aspect Import is True when the expression is
5887 -- either missing or it is explicitly set to True.
5889 if Get_Aspect_Id (Asp) = Aspect_Import
5890 and then (No (Expr)
5891 or else (Compile_Time_Known_Value (Expr)
5892 and then Is_True (Expr_Value (Expr))))
5893 then
5894 return True;
5895 end if;
5897 Next (Asp);
5898 end loop;
5899 end if;
5901 return False;
5902 end Has_Boolean_Aspect_Import;
5904 -------------------------
5905 -- Inherit_Freeze_Node --
5906 -------------------------
5908 procedure Inherit_Freeze_Node
5909 (Fnod : Node_Id;
5910 Typ : Entity_Id)
5912 Typ_Fnod : constant Node_Id := Freeze_Node (Typ);
5914 begin
5915 Set_Freeze_Node (Typ, Fnod);
5916 Set_Entity (Fnod, Typ);
5918 -- The input type had an existing node. Propagate relevant attributes
5919 -- from the old freeze node to the inherited freeze node.
5921 -- ??? if both freeze nodes have attributes, would they differ?
5923 if Present (Typ_Fnod) then
5925 -- Attribute Access_Types_To_Process
5927 if Present (Access_Types_To_Process (Typ_Fnod))
5928 and then No (Access_Types_To_Process (Fnod))
5929 then
5930 Set_Access_Types_To_Process (Fnod,
5931 Access_Types_To_Process (Typ_Fnod));
5932 end if;
5934 -- Attribute Actions
5936 if Present (Actions (Typ_Fnod)) and then No (Actions (Fnod)) then
5937 Set_Actions (Fnod, Actions (Typ_Fnod));
5938 end if;
5940 -- Attribute First_Subtype_Link
5942 if Present (First_Subtype_Link (Typ_Fnod))
5943 and then No (First_Subtype_Link (Fnod))
5944 then
5945 Set_First_Subtype_Link (Fnod, First_Subtype_Link (Typ_Fnod));
5946 end if;
5948 -- Attribute TSS_Elist
5950 if Present (TSS_Elist (Typ_Fnod))
5951 and then No (TSS_Elist (Fnod))
5952 then
5953 Set_TSS_Elist (Fnod, TSS_Elist (Typ_Fnod));
5954 end if;
5955 end if;
5956 end Inherit_Freeze_Node;
5958 ------------------------------
5959 -- Wrap_Imported_Subprogram --
5960 ------------------------------
5962 -- The issue here is that our normal approach of checking preconditions
5963 -- and postconditions does not work for imported procedures, since we
5964 -- are not generating code for the body. To get around this we create
5965 -- a wrapper, as shown by the following example:
5967 -- procedure K (A : Integer);
5968 -- pragma Import (C, K);
5970 -- The spec is rewritten by removing the effects of pragma Import, but
5971 -- leaving the convention unchanged, as though the source had said:
5973 -- procedure K (A : Integer);
5974 -- pragma Convention (C, K);
5976 -- and we create a body, added to the entity K freeze actions, which
5977 -- looks like:
5979 -- procedure K (A : Integer) is
5980 -- procedure K (A : Integer);
5981 -- pragma Import (C, K);
5982 -- begin
5983 -- K (A);
5984 -- end K;
5986 -- Now the contract applies in the normal way to the outer procedure,
5987 -- and the inner procedure has no contracts, so there is no problem
5988 -- in just calling it to get the original effect.
5990 -- In the case of a function, we create an appropriate return statement
5991 -- for the subprogram body that calls the inner procedure.
5993 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
5994 function Copy_Import_Pragma return Node_Id;
5995 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
5997 ------------------------
5998 -- Copy_Import_Pragma --
5999 ------------------------
6001 function Copy_Import_Pragma return Node_Id is
6003 -- The subprogram should have an import pragma, otherwise it does
6004 -- need a wrapper.
6006 Prag : constant Node_Id := Import_Pragma (E);
6007 pragma Assert (Present (Prag));
6009 -- Save all semantic fields of the pragma
6011 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
6012 Save_From : constant Boolean := From_Aspect_Specification (Prag);
6013 Save_Prag : constant Node_Id := Next_Pragma (Prag);
6014 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
6016 Result : Node_Id;
6018 begin
6019 -- Reset all semantic fields. This avoids a potential infinite
6020 -- loop when the pragma comes from an aspect as the duplication
6021 -- will copy the aspect, then copy the corresponding pragma and
6022 -- so on.
6024 Set_Corresponding_Aspect (Prag, Empty);
6025 Set_From_Aspect_Specification (Prag, False);
6026 Set_Next_Pragma (Prag, Empty);
6027 Set_Next_Rep_Item (Prag, Empty);
6029 Result := Copy_Separate_Tree (Prag);
6031 -- Restore the original semantic fields
6033 Set_Corresponding_Aspect (Prag, Save_Asp);
6034 Set_From_Aspect_Specification (Prag, Save_From);
6035 Set_Next_Pragma (Prag, Save_Prag);
6036 Set_Next_Rep_Item (Prag, Save_Rep);
6038 return Result;
6039 end Copy_Import_Pragma;
6041 -- Local variables
6043 Loc : constant Source_Ptr := Sloc (E);
6044 CE : constant Name_Id := Chars (E);
6045 Bod : Node_Id;
6046 Forml : Entity_Id;
6047 Parms : List_Id;
6048 Prag : Node_Id;
6049 Spec : Node_Id;
6050 Stmt : Node_Id;
6052 -- Start of processing for Wrap_Imported_Subprogram
6054 begin
6055 -- Nothing to do if not imported
6057 if not Is_Imported (E) then
6058 return;
6060 -- Test enabling conditions for wrapping
6062 elsif Is_Subprogram (E)
6063 and then Present (Contract (E))
6064 and then Present (Pre_Post_Conditions (Contract (E)))
6065 and then not GNATprove_Mode
6066 then
6067 -- Here we do the wrap
6069 -- Note on calls to Copy_Separate_Tree. The trees we are copying
6070 -- here are fully analyzed, but we definitely want fully syntactic
6071 -- unanalyzed trees in the body we construct, so that the analysis
6072 -- generates the right visibility, and that is exactly what the
6073 -- calls to Copy_Separate_Tree give us.
6075 Prag := Copy_Import_Pragma;
6077 -- Fix up spec so it is no longer imported and has convention Ada
6079 Set_Has_Completion (E, False);
6080 Set_Import_Pragma (E, Empty);
6081 Set_Interface_Name (E, Empty);
6082 Set_Is_Imported (E, False);
6083 Set_Convention (E, Convention_Ada);
6085 -- Grab the subprogram declaration and specification
6087 Spec := Declaration_Node (E);
6089 -- Build parameter list that we need
6091 Parms := New_List;
6092 Forml := First_Formal (E);
6093 while Present (Forml) loop
6094 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
6095 Next_Formal (Forml);
6096 end loop;
6098 -- Build the call
6100 -- An imported function whose result type is anonymous access
6101 -- creates a new anonymous access type when it is relocated into
6102 -- the declarations of the body generated below. As a result, the
6103 -- accessibility level of these two anonymous access types may not
6104 -- be compatible even though they are essentially the same type.
6105 -- Use an unchecked type conversion to reconcile this case. Note
6106 -- that the conversion is safe because in the named access type
6107 -- case, both the body and imported function utilize the same
6108 -- type.
6110 if Ekind (E) in E_Function | E_Generic_Function then
6111 Stmt :=
6112 Make_Simple_Return_Statement (Loc,
6113 Expression =>
6114 Unchecked_Convert_To (Etype (E),
6115 Make_Function_Call (Loc,
6116 Name => Make_Identifier (Loc, CE),
6117 Parameter_Associations => Parms)));
6119 else
6120 Stmt :=
6121 Make_Procedure_Call_Statement (Loc,
6122 Name => Make_Identifier (Loc, CE),
6123 Parameter_Associations => Parms);
6124 end if;
6126 -- Now build the body
6128 Bod :=
6129 Make_Subprogram_Body (Loc,
6130 Specification =>
6131 Copy_Separate_Tree (Spec),
6132 Declarations => New_List (
6133 Make_Subprogram_Declaration (Loc,
6134 Specification => Copy_Separate_Tree (Spec)),
6135 Prag),
6136 Handled_Statement_Sequence =>
6137 Make_Handled_Sequence_Of_Statements (Loc,
6138 Statements => New_List (Stmt),
6139 End_Label => Make_Identifier (Loc, CE)));
6141 -- Append the body to freeze result
6143 Add_To_Result (Bod);
6144 return;
6146 -- Case of imported subprogram that does not get wrapped
6148 else
6149 -- Set Is_Public. All imported entities need an external symbol
6150 -- created for them since they are always referenced from another
6151 -- object file. Note this used to be set when we set Is_Imported
6152 -- back in Sem_Prag, but now we delay it to this point, since we
6153 -- don't want to set this flag if we wrap an imported subprogram.
6155 Set_Is_Public (E);
6156 end if;
6157 end Wrap_Imported_Subprogram;
6159 -- Start of processing for Freeze_Entity
6161 begin
6162 -- The entity being frozen may be subject to pragma Ghost. Set the mode
6163 -- now to ensure that any nodes generated during freezing are properly
6164 -- flagged as Ghost.
6166 Set_Ghost_Mode (E);
6168 -- We are going to test for various reasons why this entity need not be
6169 -- frozen here, but in the case of an Itype that's defined within a
6170 -- record, that test actually applies to the record.
6172 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
6173 Test_E := Scope (E);
6175 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
6176 and then Is_Record_Type (Underlying_Type (Scope (E)))
6177 then
6178 Test_E := Underlying_Type (Scope (E));
6179 end if;
6181 -- Do not freeze if already frozen since we only need one freeze node
6183 if Is_Frozen (E) then
6184 Result := No_List;
6185 goto Leave;
6187 -- Do not freeze if we are preanalyzing without freezing
6189 elsif Inside_Preanalysis_Without_Freezing > 0 then
6190 Result := No_List;
6191 goto Leave;
6193 elsif Ekind (E) = E_Generic_Package then
6194 Result := Freeze_Generic_Entities (E);
6195 goto Leave;
6197 -- It is improper to freeze an external entity within a generic because
6198 -- its freeze node will appear in a non-valid context. The entity will
6199 -- be frozen in the proper scope after the current generic is analyzed.
6200 -- However, aspects must be analyzed because they may be queried later
6201 -- within the generic itself, and the corresponding pragma or attribute
6202 -- definition has not been analyzed yet. After this, indicate that the
6203 -- entity has no further delayed aspects, to prevent a later aspect
6204 -- analysis out of the scope of the generic.
6206 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
6207 if Has_Delayed_Aspects (E) then
6208 Analyze_Aspects_At_Freeze_Point (E);
6209 Set_Has_Delayed_Aspects (E, False);
6210 end if;
6212 Result := No_List;
6213 goto Leave;
6215 -- AI05-0213: A formal incomplete type does not freeze the actual. In
6216 -- the instance, the same applies to the subtype renaming the actual.
6218 elsif Is_Private_Type (E)
6219 and then Is_Generic_Actual_Type (E)
6220 and then No (Full_View (Base_Type (E)))
6221 and then Ada_Version >= Ada_2012
6222 then
6223 Result := No_List;
6224 goto Leave;
6226 -- Formal subprograms are never frozen
6228 elsif Is_Formal_Subprogram (E) then
6229 Result := No_List;
6230 goto Leave;
6232 -- Generic types are never frozen as they lack delayed semantic checks
6234 elsif Is_Generic_Type (E) then
6235 Result := No_List;
6236 goto Leave;
6238 -- Do not freeze a global entity within an inner scope created during
6239 -- expansion. A call to subprogram E within some internal procedure
6240 -- (a stream attribute for example) might require freezing E, but the
6241 -- freeze node must appear in the same declarative part as E itself.
6242 -- The two-pass elaboration mechanism in gigi guarantees that E will
6243 -- be frozen before the inner call is elaborated. We exclude constants
6244 -- from this test, because deferred constants may be frozen early, and
6245 -- must be diagnosed (e.g. in the case of a deferred constant being used
6246 -- in a default expression). If the enclosing subprogram comes from
6247 -- source, or is a generic instance, then the freeze point is the one
6248 -- mandated by the language, and we freeze the entity. A subprogram that
6249 -- is a child unit body that acts as a spec does not have a spec that
6250 -- comes from source, but can only come from source.
6252 elsif In_Open_Scopes (Scope (Test_E))
6253 and then Scope (Test_E) /= Current_Scope
6254 and then Ekind (Test_E) /= E_Constant
6255 then
6256 -- Here we deal with the special case of the expansion of
6257 -- postconditions. Previously this was handled by the loop below,
6258 -- since these postcondition checks got isolated to a separate,
6259 -- internally generated, subprogram. Now, however, the postcondition
6260 -- checks get contained within their corresponding subprogram
6261 -- directly.
6263 if not Comes_From_Source (N)
6264 and then Nkind (N) = N_Pragma
6265 and then From_Aspect_Specification (N)
6266 and then Is_Valid_Assertion_Kind (Original_Aspect_Pragma_Name (N))
6268 -- Now, verify the placement of the pragma is within an expanded
6269 -- subprogram which contains postcondition expansion - detected
6270 -- through the presence of the "Wrapped_Statements" field.
6272 and then Present (Enclosing_Subprogram (Current_Scope))
6273 and then Present (Wrapped_Statements
6274 (Enclosing_Subprogram (Current_Scope)))
6275 then
6276 goto Leave;
6277 end if;
6279 -- Otherwise, loop through scopes checking if an enclosing scope
6280 -- comes from source or is a generic. Note that, for the purpose
6281 -- of this test, we need to consider that the internally generated
6282 -- subprogram described above comes from source too if the original
6283 -- subprogram itself does.
6285 declare
6286 S : Entity_Id;
6288 begin
6289 S := Current_Scope;
6290 while Present (S) loop
6291 if Is_Overloadable (S) then
6292 if Comes_From_Source (S)
6293 or else (Chars (S) = Name_uWrapped_Statements
6294 and then Comes_From_Source (Scope (S)))
6295 or else Is_Generic_Instance (S)
6296 or else Is_Child_Unit (S)
6297 then
6298 exit;
6299 else
6300 Result := No_List;
6301 goto Leave;
6302 end if;
6303 end if;
6305 S := Scope (S);
6306 end loop;
6307 end;
6309 -- Similarly, an inlined instance body may make reference to global
6310 -- entities, but these references cannot be the proper freezing point
6311 -- for them, and in the absence of inlining freezing will take place in
6312 -- their own scope. Normally instance bodies are analyzed after the
6313 -- enclosing compilation, and everything has been frozen at the proper
6314 -- place, but with front-end inlining an instance body is compiled
6315 -- before the end of the enclosing scope, and as a result out-of-order
6316 -- freezing must be prevented.
6318 elsif Front_End_Inlining
6319 and then In_Instance_Body
6320 and then Present (Scope (Test_E))
6321 then
6322 declare
6323 S : Entity_Id;
6325 begin
6326 S := Scope (Test_E);
6327 while Present (S) loop
6328 if Is_Generic_Instance (S) then
6329 exit;
6330 else
6331 S := Scope (S);
6332 end if;
6333 end loop;
6335 if No (S) then
6336 Result := No_List;
6337 goto Leave;
6338 end if;
6339 end;
6340 end if;
6342 -- Add checks to detect proper initialization of scalars that may appear
6343 -- as subprogram parameters.
6345 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
6346 Apply_Parameter_Validity_Checks (E);
6347 end if;
6349 -- Deal with delayed aspect specifications. The analysis of the aspect
6350 -- is required to be delayed to the freeze point, thus we analyze the
6351 -- pragma or attribute definition clause in the tree at this point. We
6352 -- also analyze the aspect specification node at the freeze point when
6353 -- the aspect doesn't correspond to pragma/attribute definition clause.
6354 -- In addition, a derived type may have inherited aspects that were
6355 -- delayed in the parent, so these must also be captured now.
6357 -- For a record type, we deal with the delayed aspect specifications on
6358 -- components first, which is consistent with the non-delayed case and
6359 -- makes it possible to have a single processing to detect conflicts.
6361 if Is_Record_Type (E) then
6362 declare
6363 Comp : Entity_Id;
6365 Rec_Pushed : Boolean := False;
6366 -- Set True if the record type E has been pushed on the scope
6367 -- stack. Needed for the analysis of delayed aspects specified
6368 -- to the components of Rec.
6370 begin
6371 Comp := First_Component (E);
6372 while Present (Comp) loop
6373 if Has_Delayed_Aspects (Comp) then
6374 if not Rec_Pushed then
6375 Push_Scope (E);
6376 Rec_Pushed := True;
6378 -- The visibility to the discriminants must be restored
6379 -- in order to properly analyze the aspects.
6381 if Has_Discriminants (E) then
6382 Install_Discriminants (E);
6383 end if;
6384 end if;
6386 Analyze_Aspects_At_Freeze_Point (Comp);
6387 end if;
6389 Next_Component (Comp);
6390 end loop;
6392 -- Pop the scope if Rec scope has been pushed on the scope stack
6393 -- during the delayed aspect analysis process.
6395 if Rec_Pushed then
6396 if Has_Discriminants (E) then
6397 Uninstall_Discriminants (E);
6398 end if;
6400 Pop_Scope;
6401 end if;
6402 end;
6403 end if;
6405 if Has_Delayed_Aspects (E) then
6406 Analyze_Aspects_At_Freeze_Point (E);
6407 end if;
6409 -- Here to freeze the entity
6411 Set_Is_Frozen (E);
6413 -- Case of entity being frozen is other than a type
6415 if not Is_Type (E) then
6417 -- If entity is exported or imported and does not have an external
6418 -- name, now is the time to provide the appropriate default name.
6419 -- Skip this if the entity is stubbed, since we don't need a name
6420 -- for any stubbed routine. For the case on intrinsics, if no
6421 -- external name is specified, then calls will be handled in
6422 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
6423 -- external name is provided, then Expand_Intrinsic_Call leaves
6424 -- calls in place for expansion by GIGI.
6426 if (Is_Imported (E) or else Is_Exported (E))
6427 and then No (Interface_Name (E))
6428 and then Convention (E) /= Convention_Stubbed
6429 and then Convention (E) /= Convention_Intrinsic
6430 then
6431 Set_Encoded_Interface_Name
6432 (E, Get_Default_External_Name (E));
6433 end if;
6435 -- Subprogram case
6437 if Is_Subprogram (E) then
6439 -- Check for needing to wrap imported subprogram
6441 Wrap_Imported_Subprogram (E);
6443 -- Freeze all parameter types and the return type (RM 13.14(14)).
6444 -- However skip this for internal subprograms. This is also where
6445 -- any extra formal parameters are created since we now know
6446 -- whether the subprogram will use a foreign convention.
6448 -- In Ada 2012, freezing a subprogram does not always freeze the
6449 -- corresponding profile (see AI05-019). An attribute reference
6450 -- is not a freezing point of the profile. Similarly, we do not
6451 -- freeze the profile of primitives of a library-level tagged type
6452 -- when we are building its dispatch table. Flag Do_Freeze_Profile
6453 -- indicates whether the profile should be frozen now.
6455 -- This processing doesn't apply to internal entities (see below)
6457 if not Is_Internal (E) and then Do_Freeze_Profile then
6458 if not Freeze_Profile (E) then
6459 goto Leave;
6460 end if;
6461 end if;
6463 -- Must freeze its parent first if it is a derived subprogram
6465 if Present (Alias (E)) then
6466 Freeze_And_Append (Alias (E), N, Result);
6467 end if;
6469 -- We don't freeze internal subprograms, because we don't normally
6470 -- want addition of extra formals or mechanism setting to happen
6471 -- for those. However we do pass through predefined dispatching
6472 -- cases, since extra formals may be needed in some cases, such as
6473 -- for the stream 'Input function (build-in-place formals).
6475 if not Is_Internal (E)
6476 or else Is_Predefined_Dispatching_Operation (E)
6477 then
6478 Freeze_Subprogram (E);
6479 end if;
6481 -- If warning on suspicious contracts then check for the case of
6482 -- a postcondition other than False for a No_Return subprogram.
6484 if No_Return (E)
6485 and then Warn_On_Suspicious_Contract
6486 and then Present (Contract (E))
6487 then
6488 declare
6489 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
6490 Exp : Node_Id;
6492 begin
6493 while Present (Prag) loop
6494 if Pragma_Name_Unmapped (Prag) in Name_Post
6495 | Name_Postcondition
6496 | Name_Refined_Post
6497 then
6498 Exp :=
6499 Expression
6500 (First (Pragma_Argument_Associations (Prag)));
6502 if Nkind (Exp) /= N_Identifier
6503 or else Chars (Exp) /= Name_False
6504 then
6505 Error_Msg_NE
6506 ("useless postcondition, & is marked "
6507 & "No_Return?.t?", Exp, E);
6508 end if;
6509 end if;
6511 Prag := Next_Pragma (Prag);
6512 end loop;
6513 end;
6514 end if;
6516 -- Here for other than a subprogram or type
6518 else
6519 -- If entity has a type declared in the current scope, and it is
6520 -- not a generic unit, then freeze it first.
6522 if Present (Etype (E))
6523 and then Ekind (E) /= E_Generic_Function
6524 and then Within_Scope (Etype (E), Current_Scope)
6525 then
6526 Freeze_And_Append (Etype (E), N, Result);
6528 -- For an object of an anonymous array type, aspects on the
6529 -- object declaration apply to the type itself. This is the
6530 -- case for Atomic_Components, Volatile_Components, and
6531 -- Independent_Components. In these cases analysis of the
6532 -- generated pragma will mark the anonymous types accordingly,
6533 -- and the object itself does not require a freeze node.
6535 if Ekind (E) = E_Variable
6536 and then Is_Itype (Etype (E))
6537 and then Is_Array_Type (Etype (E))
6538 and then Has_Delayed_Aspects (E)
6539 then
6540 Set_Has_Delayed_Aspects (E, False);
6541 Set_Has_Delayed_Freeze (E, False);
6542 Set_Freeze_Node (E, Empty);
6543 end if;
6544 end if;
6546 -- Special processing for objects created by object declaration;
6547 -- we protect the call to Declaration_Node against entities of
6548 -- expressions replaced by the frontend with an N_Raise_CE node.
6550 if Ekind (E) in E_Constant | E_Variable
6551 and then Nkind (Declaration_Node (E)) = N_Object_Declaration
6552 then
6553 Freeze_Object_Declaration (E);
6554 end if;
6556 -- Check that a constant which has a pragma Volatile[_Components]
6557 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
6559 -- Note: Atomic[_Components] also sets Volatile[_Components]
6561 if Ekind (E) = E_Constant
6562 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
6563 and then not Is_Imported (E)
6564 and then not Has_Boolean_Aspect_Import (E)
6565 then
6566 -- Make sure we actually have a pragma, and have not merely
6567 -- inherited the indication from elsewhere (e.g. an address
6568 -- clause, which is not good enough in RM terms).
6570 if Has_Rep_Pragma (E, Name_Atomic)
6571 or else
6572 Has_Rep_Pragma (E, Name_Atomic_Components)
6573 then
6574 Error_Msg_N
6575 ("standalone atomic constant must be " &
6576 "imported (RM C.6(13))", E);
6578 elsif Has_Rep_Pragma (E, Name_Volatile)
6579 or else
6580 Has_Rep_Pragma (E, Name_Volatile_Components)
6581 then
6582 Error_Msg_N
6583 ("standalone volatile constant must be " &
6584 "imported (RM C.6(13))", E);
6585 end if;
6586 end if;
6588 -- Static objects require special handling
6590 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
6591 and then Is_Statically_Allocated (E)
6592 then
6593 Freeze_Static_Object (E);
6594 end if;
6596 -- Remaining step is to layout objects
6598 if Ekind (E) in E_Variable | E_Constant | E_Loop_Parameter
6599 or else Is_Formal (E)
6600 then
6601 Layout_Object (E);
6602 end if;
6604 -- For an object that does not have delayed freezing, and whose
6605 -- initialization actions have been captured in a compound
6606 -- statement, move them back now directly within the enclosing
6607 -- statement sequence.
6609 if Ekind (E) in E_Constant | E_Variable
6610 and then not Has_Delayed_Freeze (E)
6611 then
6612 Explode_Initialization_Compound_Statement (E);
6613 end if;
6615 -- Do not generate a freeze node for a generic unit
6617 if Is_Generic_Unit (E) then
6618 Result := No_List;
6619 goto Leave;
6620 end if;
6621 end if;
6623 -- Case of a type or subtype being frozen
6625 else
6626 -- Verify several SPARK legality rules related to Ghost types now
6627 -- that the type is frozen.
6629 Check_Ghost_Type (E);
6631 -- We used to check here that a full type must have preelaborable
6632 -- initialization if it completes a private type specified with
6633 -- pragma Preelaborable_Initialization, but that missed cases where
6634 -- the types occur within a generic package, since the freezing
6635 -- that occurs within a containing scope generally skips traversal
6636 -- of a generic unit's declarations (those will be frozen within
6637 -- instances). This check was moved to Analyze_Package_Specification.
6639 -- The type may be defined in a generic unit. This can occur when
6640 -- freezing a generic function that returns the type (which is
6641 -- defined in a parent unit). It is clearly meaningless to freeze
6642 -- this type. However, if it is a subtype, its size may be determi-
6643 -- nable and used in subsequent checks, so might as well try to
6644 -- compute it.
6646 -- In Ada 2012, Freeze_Entities is also used in the front end to
6647 -- trigger the analysis of aspect expressions, so in this case we
6648 -- want to continue the freezing process.
6650 -- Is_Generic_Unit (Scope (E)) is dubious here, do we want instead
6651 -- In_Generic_Scope (E)???
6653 if Present (Scope (E))
6654 and then Is_Generic_Unit (Scope (E))
6655 and then
6656 (not Has_Predicates (E)
6657 and then not Has_Delayed_Freeze (E))
6658 then
6659 Check_Compile_Time_Size (E);
6660 Result := No_List;
6661 goto Leave;
6662 end if;
6664 -- Check for error of Type_Invariant'Class applied to an untagged
6665 -- type (check delayed to freeze time when full type is available).
6667 declare
6668 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
6669 begin
6670 if Present (Prag)
6671 and then Class_Present (Prag)
6672 and then not Is_Tagged_Type (E)
6673 then
6674 Error_Msg_NE
6675 ("Type_Invariant''Class cannot be specified for &", Prag, E);
6676 Error_Msg_N
6677 ("\can only be specified for a tagged type", Prag);
6678 end if;
6679 end;
6681 -- Deal with special cases of freezing for subtype
6683 if E /= Base_Type (E) then
6685 -- Before we do anything else, a specific test for the case of a
6686 -- size given for an array where the array would need to be packed
6687 -- in order for the size to be honored, but is not. This is the
6688 -- case where implicit packing may apply. The reason we do this so
6689 -- early is that, if we have implicit packing, the layout of the
6690 -- base type is affected, so we must do this before we freeze the
6691 -- base type.
6693 -- We could do this processing only if implicit packing is enabled
6694 -- since in all other cases, the error would be caught by the back
6695 -- end. However, we choose to do the check even if we do not have
6696 -- implicit packing enabled, since this allows us to give a more
6697 -- useful error message (advising use of pragma Implicit_Packing
6698 -- or pragma Pack).
6700 if Is_Array_Type (E) then
6701 declare
6702 Ctyp : constant Entity_Id := Component_Type (E);
6703 Rsiz : constant Uint :=
6704 (if Known_RM_Size (Ctyp) then RM_Size (Ctyp) else Uint_0);
6705 SZ : constant Node_Id := Size_Clause (E);
6706 Btyp : constant Entity_Id := Base_Type (E);
6708 Lo : Node_Id;
6709 Hi : Node_Id;
6710 Indx : Node_Id;
6712 Dim : Uint;
6713 Num_Elmts : Uint := Uint_1;
6714 -- Number of elements in array
6716 begin
6717 -- Check enabling conditions. These are straightforward
6718 -- except for the test for a limited composite type. This
6719 -- eliminates the rare case of a array of limited components
6720 -- where there are issues of whether or not we can go ahead
6721 -- and pack the array (since we can't freely pack and unpack
6722 -- arrays if they are limited).
6724 -- Note that we check the root type explicitly because the
6725 -- whole point is we are doing this test before we have had
6726 -- a chance to freeze the base type (and it is that freeze
6727 -- action that causes stuff to be inherited).
6729 -- The conditions on the size are identical to those used in
6730 -- Freeze_Array_Type to set the Is_Packed flag.
6732 if Has_Size_Clause (E)
6733 and then Known_Static_RM_Size (E)
6734 and then not Is_Packed (E)
6735 and then not Has_Pragma_Pack (E)
6736 and then not Has_Component_Size_Clause (E)
6737 and then Known_Static_RM_Size (Ctyp)
6738 and then Rsiz <= System_Max_Integer_Size
6739 and then not (Addressable (Rsiz)
6740 and then Known_Static_Esize (Ctyp)
6741 and then Esize (Ctyp) = Rsiz)
6742 and then not (Rsiz mod System_Storage_Unit = 0
6743 and then Is_Composite_Type (Ctyp))
6744 and then not Is_Limited_Composite (E)
6745 and then not Is_Packed (Root_Type (E))
6746 and then not Has_Component_Size_Clause (Root_Type (E))
6747 and then not (CodePeer_Mode or GNATprove_Mode)
6748 then
6749 -- Compute number of elements in array
6751 Indx := First_Index (E);
6752 while Present (Indx) loop
6753 Get_Index_Bounds (Indx, Lo, Hi);
6755 if not (Compile_Time_Known_Value (Lo)
6756 and then
6757 Compile_Time_Known_Value (Hi))
6758 then
6759 goto No_Implicit_Packing;
6760 end if;
6762 Dim := Expr_Value (Hi) - Expr_Value (Lo) + 1;
6764 if Dim > Uint_0 then
6765 Num_Elmts := Num_Elmts * Dim;
6766 else
6767 Num_Elmts := Uint_0;
6768 end if;
6770 Next_Index (Indx);
6771 end loop;
6773 -- What we are looking for here is the situation where
6774 -- the RM_Size given would be exactly right if there was
6775 -- a pragma Pack, resulting in the component size being
6776 -- the RM_Size of the component type.
6778 if RM_Size (E) = Num_Elmts * Rsiz then
6780 -- For implicit packing mode, just set the component
6781 -- size and Freeze_Array_Type will do the rest.
6783 if Implicit_Packing then
6784 Set_Component_Size (Btyp, Rsiz);
6786 -- Otherwise give an error message, except that if the
6787 -- specified Size is zero, there is no need for pragma
6788 -- Pack. Note that size zero is not considered
6789 -- Addressable.
6791 elsif RM_Size (E) /= Uint_0 then
6792 Error_Msg_NE
6793 ("size given for& too small", SZ, E);
6794 Error_Msg_N -- CODEFIX
6795 ("\use explicit pragma Pack or use pragma "
6796 & "Implicit_Packing", SZ);
6797 end if;
6798 end if;
6799 end if;
6800 end;
6801 end if;
6803 <<No_Implicit_Packing>>
6805 -- If ancestor subtype present, freeze that first. Note that this
6806 -- will also get the base type frozen. Need RM reference ???
6808 Atype := Ancestor_Subtype (E);
6810 if Present (Atype) then
6811 Freeze_And_Append (Atype, N, Result);
6813 -- No ancestor subtype present
6815 else
6816 -- See if we have a nearest ancestor that has a predicate.
6817 -- That catches the case of derived type with a predicate.
6818 -- Need RM reference here ???
6820 Atype := Nearest_Ancestor (E);
6822 if Present (Atype) and then Has_Predicates (Atype) then
6823 Freeze_And_Append (Atype, N, Result);
6824 end if;
6826 -- Freeze base type before freezing the entity (RM 13.14(15))
6828 if E /= Base_Type (E) then
6829 Freeze_And_Append (Base_Type (E), N, Result);
6830 end if;
6831 end if;
6833 -- A subtype inherits all the type-related representation aspects
6834 -- from its parents (RM 13.1(8)).
6836 if May_Inherit_Delayed_Rep_Aspects (E) then
6837 Inherit_Delayed_Rep_Aspects (E);
6838 end if;
6840 Inherit_Aspects_At_Freeze_Point (E);
6842 -- For a derived type, freeze its parent type first (RM 13.14(15))
6844 elsif Is_Derived_Type (E) then
6845 Freeze_And_Append (Etype (E), N, Result);
6847 -- A derived type inherits each type-related representation aspect
6848 -- of its parent type that was directly specified before the
6849 -- declaration of the derived type (RM 13.1(15)).
6851 if May_Inherit_Delayed_Rep_Aspects (E) then
6852 Inherit_Delayed_Rep_Aspects (E);
6853 end if;
6855 Inherit_Aspects_At_Freeze_Point (E);
6856 end if;
6858 -- Case of array type
6860 if Is_Array_Type (E) then
6861 Freeze_Array_Type (E);
6862 end if;
6864 -- Check for incompatible size and alignment for array/record type
6866 if Warn_On_Size_Alignment
6867 and then (Is_Array_Type (E) or else Is_Record_Type (E))
6868 and then Has_Size_Clause (E)
6869 and then Has_Alignment_Clause (E)
6871 -- If explicit Object_Size clause given assume that the programmer
6872 -- knows what he is doing, and expects the compiler behavior.
6874 and then not Has_Object_Size_Clause (E)
6876 -- It does not really make sense to warn for the minimum alignment
6877 -- since the programmer could not get rid of the warning.
6879 and then Alignment (E) > 1
6881 -- Check for size not a multiple of alignment
6883 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
6884 then
6885 declare
6886 SC : constant Node_Id := Size_Clause (E);
6887 AC : constant Node_Id := Alignment_Clause (E);
6888 Loc : Node_Id;
6889 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
6891 begin
6892 if Present (SC) and then Present (AC) then
6894 -- Give a warning
6896 if Sloc (SC) > Sloc (AC) then
6897 Loc := SC;
6898 Error_Msg_NE
6899 ("?.z?size is not a multiple of alignment for &",
6900 Loc, E);
6901 Error_Msg_Sloc := Sloc (AC);
6902 Error_Msg_Uint_1 := Alignment (E);
6903 Error_Msg_N ("\?.z?alignment of ^ specified #", Loc);
6905 else
6906 Loc := AC;
6907 Error_Msg_NE
6908 ("?.z?size is not a multiple of alignment for &",
6909 Loc, E);
6910 Error_Msg_Sloc := Sloc (SC);
6911 Error_Msg_Uint_1 := RM_Size (E);
6912 Error_Msg_N ("\?.z?size of ^ specified #", Loc);
6913 end if;
6915 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
6916 Error_Msg_N ("\?.z?Object_Size will be increased to ^", Loc);
6917 end if;
6918 end;
6919 end if;
6921 -- For a class-wide type, the corresponding specific type is
6922 -- frozen as well (RM 13.14(15))
6924 if Is_Class_Wide_Type (E) then
6925 Freeze_And_Append (Root_Type (E), N, Result);
6927 -- If the base type of the class-wide type is still incomplete,
6928 -- the class-wide remains unfrozen as well. This is legal when
6929 -- E is the formal of a primitive operation of some other type
6930 -- which is being frozen.
6932 if not Is_Frozen (Root_Type (E)) then
6933 Set_Is_Frozen (E, False);
6934 goto Leave;
6935 end if;
6937 -- The equivalent type associated with a class-wide subtype needs
6938 -- to be frozen to ensure that its layout is done.
6940 if Ekind (E) = E_Class_Wide_Subtype
6941 and then Present (Equivalent_Type (E))
6942 then
6943 Freeze_And_Append (Equivalent_Type (E), N, Result);
6944 end if;
6946 -- Generate an itype reference for a library-level class-wide type
6947 -- at the freeze point. Otherwise the first explicit reference to
6948 -- the type may appear in an inner scope which will be rejected by
6949 -- the back-end.
6951 if Is_Itype (E)
6952 and then Is_Compilation_Unit (Scope (E))
6953 then
6954 declare
6955 Ref : constant Node_Id := Make_Itype_Reference (Loc);
6957 begin
6958 Set_Itype (Ref, E);
6960 -- From a gigi point of view, a class-wide subtype derives
6961 -- from its record equivalent type. As a result, the itype
6962 -- reference must appear after the freeze node of the
6963 -- equivalent type or gigi will reject the reference.
6965 if Ekind (E) = E_Class_Wide_Subtype
6966 and then Present (Equivalent_Type (E))
6967 then
6968 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
6969 else
6970 Add_To_Result (Ref);
6971 end if;
6972 end;
6973 end if;
6975 -- For a record type or record subtype, freeze all component types
6976 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
6977 -- using Is_Record_Type, because we don't want to attempt the freeze
6978 -- for the case of a private type with record extension (we will do
6979 -- that later when the full type is frozen).
6981 elsif Ekind (E) in E_Record_Type | E_Record_Subtype then
6982 if not In_Generic_Scope (E) then
6983 Freeze_Record_Type (E);
6984 end if;
6986 -- Report a warning if a discriminated record base type has a
6987 -- convention with language C or C++ applied to it. This check is
6988 -- done even within generic scopes (but not in instantiations),
6989 -- which is why we don't do it as part of Freeze_Record_Type.
6991 Check_Suspicious_Convention (E);
6993 -- For a concurrent type, freeze corresponding record type. This does
6994 -- not correspond to any specific rule in the RM, but the record type
6995 -- is essentially part of the concurrent type. Also freeze all local
6996 -- entities. This includes record types created for entry parameter
6997 -- blocks and whatever local entities may appear in the private part.
6999 elsif Is_Concurrent_Type (E) then
7000 if Present (Corresponding_Record_Type (E)) then
7001 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
7002 end if;
7004 Comp := First_Entity (E);
7005 while Present (Comp) loop
7006 if Is_Type (Comp) then
7007 Freeze_And_Append (Comp, N, Result);
7009 elsif (Ekind (Comp)) /= E_Function then
7011 -- The guard on the presence of the Etype seems to be needed
7012 -- for some CodePeer (-gnatcC) cases, but not clear why???
7014 if Present (Etype (Comp)) then
7015 if Is_Itype (Etype (Comp))
7016 and then Underlying_Type (Scope (Etype (Comp))) = E
7017 then
7018 Undelay_Type (Etype (Comp));
7019 end if;
7021 Freeze_And_Append (Etype (Comp), N, Result);
7022 end if;
7023 end if;
7025 Next_Entity (Comp);
7026 end loop;
7028 -- Private types are required to point to the same freeze node as
7029 -- their corresponding full views. The freeze node itself has to
7030 -- point to the partial view of the entity (because from the partial
7031 -- view, we can retrieve the full view, but not the reverse).
7032 -- However, in order to freeze correctly, we need to freeze the full
7033 -- view. If we are freezing at the end of a scope (or within the
7034 -- scope) of the private type, the partial and full views will have
7035 -- been swapped, the full view appears first in the entity chain and
7036 -- the swapping mechanism ensures that the pointers are properly set
7037 -- (on scope exit).
7039 -- If we encounter the partial view before the full view (e.g. when
7040 -- freezing from another scope), we freeze the full view, and then
7041 -- set the pointers appropriately since we cannot rely on swapping to
7042 -- fix things up (subtypes in an outer scope might not get swapped).
7044 -- If the full view is itself private, the above requirements apply
7045 -- to the underlying full view instead of the full view. But there is
7046 -- no swapping mechanism for the underlying full view so we need to
7047 -- set the pointers appropriately in both cases.
7049 elsif Is_Incomplete_Or_Private_Type (E)
7050 and then not Is_Generic_Type (E)
7051 then
7052 -- The construction of the dispatch table associated with library
7053 -- level tagged types forces freezing of all the primitives of the
7054 -- type, which may cause premature freezing of the partial view.
7055 -- For example:
7057 -- package Pkg is
7058 -- type T is tagged private;
7059 -- type DT is new T with private;
7060 -- procedure Prim (X : in out T; Y : in out DT'Class);
7061 -- private
7062 -- type T is tagged null record;
7063 -- Obj : T;
7064 -- type DT is new T with null record;
7065 -- end;
7067 -- In this case the type will be frozen later by the usual
7068 -- mechanism: an object declaration, an instantiation, or the
7069 -- end of a declarative part.
7071 if Is_Library_Level_Tagged_Type (E)
7072 and then No (Full_View (E))
7073 then
7074 Set_Is_Frozen (E, False);
7075 goto Leave;
7077 -- Case of full view present
7079 elsif Present (Full_View (E)) then
7081 -- If full view has already been frozen, then no further
7082 -- processing is required
7084 if Is_Frozen (Full_View (E)) then
7085 Set_Has_Delayed_Freeze (E, False);
7086 Set_Freeze_Node (E, Empty);
7088 -- Otherwise freeze full view and patch the pointers so that
7089 -- the freeze node will elaborate both views in the back end.
7090 -- However, if full view is itself private, freeze underlying
7091 -- full view instead and patch the pointers so that the freeze
7092 -- node will elaborate the three views in the back end.
7094 else
7095 declare
7096 Full : Entity_Id := Full_View (E);
7098 begin
7099 if Is_Private_Type (Full)
7100 and then Present (Underlying_Full_View (Full))
7101 then
7102 Full := Underlying_Full_View (Full);
7103 end if;
7105 Freeze_And_Append (Full, N, Result);
7107 if Full /= Full_View (E)
7108 and then Has_Delayed_Freeze (Full_View (E))
7109 then
7110 F_Node := Freeze_Node (Full);
7112 if Present (F_Node) then
7113 Inherit_Freeze_Node
7114 (Fnod => F_Node, Typ => Full_View (E));
7115 else
7116 Set_Has_Delayed_Freeze (Full_View (E), False);
7117 Set_Freeze_Node (Full_View (E), Empty);
7118 end if;
7119 end if;
7121 if Has_Delayed_Freeze (E) then
7122 F_Node := Freeze_Node (Full_View (E));
7124 if Present (F_Node) then
7125 Inherit_Freeze_Node (Fnod => F_Node, Typ => E);
7126 else
7127 -- {Incomplete,Private}_Subtypes with Full_Views
7128 -- constrained by discriminants.
7130 Set_Has_Delayed_Freeze (E, False);
7131 Set_Freeze_Node (E, Empty);
7132 end if;
7133 end if;
7134 end;
7135 end if;
7137 Check_Debug_Info_Needed (E);
7139 -- AI95-117 requires that the convention of a partial view be
7140 -- the same as the convention of the full view. Note that this
7141 -- is a recognized breach of privacy, but it's essential for
7142 -- logical consistency of representation, and the lack of a
7143 -- rule in RM95 was an oversight.
7145 Set_Convention (E, Convention (Full_View (E)));
7147 Set_Size_Known_At_Compile_Time (E,
7148 Size_Known_At_Compile_Time (Full_View (E)));
7150 -- Size information is copied from the full view to the
7151 -- incomplete or private view for consistency.
7153 -- We skip this is the full view is not a type. This is very
7154 -- strange of course, and can only happen as a result of
7155 -- certain illegalities, such as a premature attempt to derive
7156 -- from an incomplete type.
7158 if Is_Type (Full_View (E)) then
7159 Set_Size_Info (E, Full_View (E));
7160 Copy_RM_Size (To => E, From => Full_View (E));
7161 end if;
7163 goto Leave;
7165 -- Case of underlying full view present
7167 elsif Is_Private_Type (E)
7168 and then Present (Underlying_Full_View (E))
7169 then
7170 if not Is_Frozen (Underlying_Full_View (E)) then
7171 Freeze_And_Append (Underlying_Full_View (E), N, Result);
7172 end if;
7174 -- Patch the pointers so that the freeze node will elaborate
7175 -- both views in the back end.
7177 if Has_Delayed_Freeze (E) then
7178 F_Node := Freeze_Node (Underlying_Full_View (E));
7180 if Present (F_Node) then
7181 Inherit_Freeze_Node
7182 (Fnod => F_Node,
7183 Typ => E);
7184 else
7185 Set_Has_Delayed_Freeze (E, False);
7186 Set_Freeze_Node (E, Empty);
7187 end if;
7188 end if;
7190 Check_Debug_Info_Needed (E);
7192 goto Leave;
7194 -- Case of no full view present. If entity is subtype or derived,
7195 -- it is safe to freeze, correctness depends on the frozen status
7196 -- of parent. Otherwise it is either premature usage, or a Taft
7197 -- amendment type, so diagnosis is at the point of use and the
7198 -- type might be frozen later.
7200 elsif E /= Base_Type (E) then
7201 declare
7202 Btyp : constant Entity_Id := Base_Type (E);
7204 begin
7205 -- However, if the base type is itself private and has no
7206 -- (underlying) full view either, wait until the full type
7207 -- declaration is seen and all the full views are created.
7209 if Is_Private_Type (Btyp)
7210 and then No (Full_View (Btyp))
7211 and then No (Underlying_Full_View (Btyp))
7212 and then Has_Delayed_Freeze (Btyp)
7213 and then No (Freeze_Node (Btyp))
7214 then
7215 Set_Is_Frozen (E, False);
7216 Result := No_List;
7217 goto Leave;
7218 end if;
7219 end;
7221 elsif Is_Derived_Type (E) then
7222 null;
7224 else
7225 Set_Is_Frozen (E, False);
7226 Result := No_List;
7227 goto Leave;
7228 end if;
7230 -- For access subprogram, freeze types of all formals, the return
7231 -- type was already frozen, since it is the Etype of the function.
7232 -- Formal types can be tagged Taft amendment types, but otherwise
7233 -- they cannot be incomplete.
7235 elsif Ekind (E) = E_Subprogram_Type then
7236 Formal := First_Formal (E);
7237 while Present (Formal) loop
7238 if Ekind (Etype (Formal)) = E_Incomplete_Type
7239 and then No (Full_View (Etype (Formal)))
7240 then
7241 if Is_Tagged_Type (Etype (Formal)) then
7242 null;
7244 -- AI05-151: Incomplete types are allowed in access to
7245 -- subprogram specifications.
7247 elsif Ada_Version < Ada_2012 then
7248 Error_Msg_NE
7249 ("invalid use of incomplete type&", E, Etype (Formal));
7250 end if;
7251 end if;
7253 Freeze_And_Append (Etype (Formal), N, Result);
7254 Next_Formal (Formal);
7255 end loop;
7257 Freeze_Subprogram (E);
7259 -- For access to a protected subprogram, freeze the equivalent type
7260 -- (however this is not set if we are not generating code or if this
7261 -- is an anonymous type used just for resolution).
7263 elsif Is_Access_Protected_Subprogram_Type (E) then
7264 if Present (Equivalent_Type (E)) then
7265 Freeze_And_Append (Equivalent_Type (E), N, Result);
7266 end if;
7267 end if;
7269 -- Generic types are never seen by the back-end, and are also not
7270 -- processed by the expander (since the expander is turned off for
7271 -- generic processing), so we never need freeze nodes for them.
7273 if Is_Generic_Type (E) then
7274 goto Leave;
7275 end if;
7277 -- Some special processing for non-generic types to complete
7278 -- representation details not known till the freeze point.
7280 if Is_Fixed_Point_Type (E) then
7281 Freeze_Fixed_Point_Type (E);
7283 elsif Is_Enumeration_Type (E) then
7284 Freeze_Enumeration_Type (E);
7286 elsif Is_Integer_Type (E) then
7287 Adjust_Esize_For_Alignment (E);
7289 if Is_Modular_Integer_Type (E)
7290 and then Warn_On_Suspicious_Modulus_Value
7291 then
7292 Check_Suspicious_Modulus (E);
7293 end if;
7295 -- The pool applies to named and anonymous access types, but not
7296 -- to subprogram and to internal types generated for 'Access
7297 -- references.
7299 elsif Is_Access_Object_Type (E)
7300 and then Ekind (E) /= E_Access_Attribute_Type
7301 then
7302 -- If a pragma Default_Storage_Pool applies, and this type has no
7303 -- Storage_Pool or Storage_Size clause (which must have occurred
7304 -- before the freezing point), then use the default. This applies
7305 -- only to base types.
7307 -- None of this applies to access to subprograms, for which there
7308 -- are clearly no pools.
7310 if Present (Default_Pool)
7311 and then Is_Base_Type (E)
7312 and then not Has_Storage_Size_Clause (E)
7313 and then No (Associated_Storage_Pool (E))
7314 then
7315 -- Case of pragma Default_Storage_Pool (null)
7317 if Nkind (Default_Pool) = N_Null then
7318 Set_No_Pool_Assigned (E);
7320 -- Case of pragma Default_Storage_Pool (Standard)
7322 elsif Entity (Default_Pool) = Standard_Standard then
7323 Set_Associated_Storage_Pool (E, RTE (RE_Global_Pool_Object));
7325 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
7327 else
7328 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
7329 end if;
7330 end if;
7332 -- Check restriction for standard storage pool
7334 if No (Associated_Storage_Pool (E)) then
7335 Check_Restriction (No_Standard_Storage_Pools, E);
7336 end if;
7338 -- Deal with error message for pure access type. This is not an
7339 -- error in Ada 2005 if there is no pool (see AI-366).
7341 if Is_Pure_Unit_Access_Type (E)
7342 and then (Ada_Version < Ada_2005
7343 or else not No_Pool_Assigned (E))
7344 and then not Is_Generic_Unit (Scope (E))
7345 then
7346 Error_Msg_N ("named access type not allowed in pure unit", E);
7348 if Ada_Version >= Ada_2005 then
7349 Error_Msg_N
7350 ("\would be legal if Storage_Size of 0 given??", E);
7352 elsif No_Pool_Assigned (E) then
7353 Error_Msg_N
7354 ("\would be legal in Ada 2005??", E);
7356 else
7357 Error_Msg_N
7358 ("\would be legal in Ada 2005 if "
7359 & "Storage_Size of 0 given??", E);
7360 end if;
7361 end if;
7362 end if;
7364 -- Case of composite types
7366 if Is_Composite_Type (E) then
7368 -- AI95-117 requires that all new primitives of a tagged type must
7369 -- inherit the convention of the full view of the type. Inherited
7370 -- and overriding operations are defined to inherit the convention
7371 -- of their parent or overridden subprogram (also specified in
7372 -- AI-117), which will have occurred earlier (in Derive_Subprogram
7373 -- and New_Overloaded_Entity). Here we set the convention of
7374 -- primitives that are still convention Ada, which will ensure
7375 -- that any new primitives inherit the type's convention. Class-
7376 -- wide types can have a foreign convention inherited from their
7377 -- specific type, but are excluded from this since they don't have
7378 -- any associated primitives.
7380 if Is_Tagged_Type (E)
7381 and then not Is_Class_Wide_Type (E)
7382 and then Convention (E) /= Convention_Ada
7383 then
7384 declare
7385 Prim_List : constant Elist_Id := Primitive_Operations (E);
7386 Prim : Elmt_Id;
7388 begin
7389 Prim := First_Elmt (Prim_List);
7390 while Present (Prim) loop
7391 if Convention (Node (Prim)) = Convention_Ada then
7392 Set_Convention (Node (Prim), Convention (E));
7393 end if;
7395 Next_Elmt (Prim);
7396 end loop;
7397 end;
7398 end if;
7400 -- If the type is a simple storage pool type, then this is where
7401 -- we attempt to locate and validate its Allocate, Deallocate, and
7402 -- Storage_Size operations (the first is required, and the latter
7403 -- two are optional). We also verify that the full type for a
7404 -- private type is allowed to be a simple storage pool type.
7406 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
7407 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
7408 then
7409 -- If the type is marked Has_Private_Declaration, then this is
7410 -- a full type for a private type that was specified with the
7411 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
7412 -- pragma is allowed for the full type (for example, it can't
7413 -- be an array type, or a nonlimited record type).
7415 if Has_Private_Declaration (E) then
7416 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
7417 and then not Is_Private_Type (E)
7418 then
7419 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
7420 Error_Msg_N
7421 ("pragma% can only apply to full type that is an " &
7422 "explicitly limited type", E);
7423 end if;
7424 end if;
7426 Validate_Simple_Pool_Ops : declare
7427 Pool_Type : Entity_Id renames E;
7428 Address_Type : constant Entity_Id := RTE (RE_Address);
7429 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
7431 procedure Validate_Simple_Pool_Op_Formal
7432 (Pool_Op : Entity_Id;
7433 Pool_Op_Formal : in out Entity_Id;
7434 Expected_Mode : Formal_Kind;
7435 Expected_Type : Entity_Id;
7436 Formal_Name : String;
7437 OK_Formal : in out Boolean);
7438 -- Validate one formal Pool_Op_Formal of the candidate pool
7439 -- operation Pool_Op. The formal must be of Expected_Type
7440 -- and have mode Expected_Mode. OK_Formal will be set to
7441 -- False if the formal doesn't match. If OK_Formal is False
7442 -- on entry, then the formal will effectively be ignored
7443 -- (because validation of the pool op has already failed).
7444 -- Upon return, Pool_Op_Formal will be updated to the next
7445 -- formal, if any.
7447 procedure Validate_Simple_Pool_Operation
7448 (Op_Name : Name_Id);
7449 -- Search for and validate a simple pool operation with the
7450 -- name Op_Name. If the name is Allocate, then there must be
7451 -- exactly one such primitive operation for the simple pool
7452 -- type. If the name is Deallocate or Storage_Size, then
7453 -- there can be at most one such primitive operation. The
7454 -- profile of the located primitive must conform to what
7455 -- is expected for each operation.
7457 ------------------------------------
7458 -- Validate_Simple_Pool_Op_Formal --
7459 ------------------------------------
7461 procedure Validate_Simple_Pool_Op_Formal
7462 (Pool_Op : Entity_Id;
7463 Pool_Op_Formal : in out Entity_Id;
7464 Expected_Mode : Formal_Kind;
7465 Expected_Type : Entity_Id;
7466 Formal_Name : String;
7467 OK_Formal : in out Boolean)
7469 begin
7470 -- If OK_Formal is False on entry, then simply ignore
7471 -- the formal, because an earlier formal has already
7472 -- been flagged.
7474 if not OK_Formal then
7475 return;
7477 -- If no formal is passed in, then issue an error for a
7478 -- missing formal.
7480 elsif No (Pool_Op_Formal) then
7481 Error_Msg_NE
7482 ("simple storage pool op missing formal " &
7483 Formal_Name & " of type&", Pool_Op, Expected_Type);
7484 OK_Formal := False;
7486 return;
7487 end if;
7489 if Etype (Pool_Op_Formal) /= Expected_Type then
7491 -- If the pool type was expected for this formal, then
7492 -- this will not be considered a candidate operation
7493 -- for the simple pool, so we unset OK_Formal so that
7494 -- the op and any later formals will be ignored.
7496 if Expected_Type = Pool_Type then
7497 OK_Formal := False;
7499 return;
7501 else
7502 Error_Msg_NE
7503 ("wrong type for formal " & Formal_Name &
7504 " of simple storage pool op; expected type&",
7505 Pool_Op_Formal, Expected_Type);
7506 end if;
7507 end if;
7509 -- Issue error if formal's mode is not the expected one
7511 if Ekind (Pool_Op_Formal) /= Expected_Mode then
7512 Error_Msg_N
7513 ("wrong mode for formal of simple storage pool op",
7514 Pool_Op_Formal);
7515 end if;
7517 -- Advance to the next formal
7519 Next_Formal (Pool_Op_Formal);
7520 end Validate_Simple_Pool_Op_Formal;
7522 ------------------------------------
7523 -- Validate_Simple_Pool_Operation --
7524 ------------------------------------
7526 procedure Validate_Simple_Pool_Operation
7527 (Op_Name : Name_Id)
7529 Op : Entity_Id;
7530 Found_Op : Entity_Id := Empty;
7531 Formal : Entity_Id;
7532 Is_OK : Boolean;
7534 begin
7535 pragma Assert
7536 (Op_Name in Name_Allocate
7537 | Name_Deallocate
7538 | Name_Storage_Size);
7540 Error_Msg_Name_1 := Op_Name;
7542 -- For each homonym declared immediately in the scope
7543 -- of the simple storage pool type, determine whether
7544 -- the homonym is an operation of the pool type, and,
7545 -- if so, check that its profile is as expected for
7546 -- a simple pool operation of that name.
7548 Op := Get_Name_Entity_Id (Op_Name);
7549 while Present (Op) loop
7550 if Ekind (Op) in E_Function | E_Procedure
7551 and then Scope (Op) = Current_Scope
7552 then
7553 Formal := First_Entity (Op);
7555 Is_OK := True;
7557 -- The first parameter must be of the pool type
7558 -- in order for the operation to qualify.
7560 if Op_Name = Name_Storage_Size then
7561 Validate_Simple_Pool_Op_Formal
7562 (Op, Formal, E_In_Parameter, Pool_Type,
7563 "Pool", Is_OK);
7564 else
7565 Validate_Simple_Pool_Op_Formal
7566 (Op, Formal, E_In_Out_Parameter, Pool_Type,
7567 "Pool", Is_OK);
7568 end if;
7570 -- If another operation with this name has already
7571 -- been located for the type, then flag an error,
7572 -- since we only allow the type to have a single
7573 -- such primitive.
7575 if Present (Found_Op) and then Is_OK then
7576 Error_Msg_NE
7577 ("only one % operation allowed for " &
7578 "simple storage pool type&", Op, Pool_Type);
7579 end if;
7581 -- In the case of Allocate and Deallocate, a formal
7582 -- of type System.Address is required.
7584 if Op_Name = Name_Allocate then
7585 Validate_Simple_Pool_Op_Formal
7586 (Op, Formal, E_Out_Parameter,
7587 Address_Type, "Storage_Address", Is_OK);
7589 elsif Op_Name = Name_Deallocate then
7590 Validate_Simple_Pool_Op_Formal
7591 (Op, Formal, E_In_Parameter,
7592 Address_Type, "Storage_Address", Is_OK);
7593 end if;
7595 -- In the case of Allocate and Deallocate, formals
7596 -- of type Storage_Count are required as the third
7597 -- and fourth parameters.
7599 if Op_Name /= Name_Storage_Size then
7600 Validate_Simple_Pool_Op_Formal
7601 (Op, Formal, E_In_Parameter,
7602 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
7603 Validate_Simple_Pool_Op_Formal
7604 (Op, Formal, E_In_Parameter,
7605 Stg_Cnt_Type, "Alignment", Is_OK);
7606 end if;
7608 -- If no mismatched formals have been found (Is_OK)
7609 -- and no excess formals are present, then this
7610 -- operation has been validated, so record it.
7612 if No (Formal) and then Is_OK then
7613 Found_Op := Op;
7614 end if;
7615 end if;
7617 Op := Homonym (Op);
7618 end loop;
7620 -- There must be a valid Allocate operation for the type,
7621 -- so issue an error if none was found.
7623 if Op_Name = Name_Allocate
7624 and then No (Found_Op)
7625 then
7626 Error_Msg_N ("missing % operation for simple " &
7627 "storage pool type", Pool_Type);
7629 elsif Present (Found_Op) then
7631 -- Simple pool operations can't be abstract
7633 if Is_Abstract_Subprogram (Found_Op) then
7634 Error_Msg_N
7635 ("simple storage pool operation must not be " &
7636 "abstract", Found_Op);
7637 end if;
7639 -- The Storage_Size operation must be a function with
7640 -- Storage_Count as its result type.
7642 if Op_Name = Name_Storage_Size then
7643 if Ekind (Found_Op) = E_Procedure then
7644 Error_Msg_N
7645 ("% operation must be a function", Found_Op);
7647 elsif Etype (Found_Op) /= Stg_Cnt_Type then
7648 Error_Msg_NE
7649 ("wrong result type for%, expected type&",
7650 Found_Op, Stg_Cnt_Type);
7651 end if;
7653 -- Allocate and Deallocate must be procedures
7655 elsif Ekind (Found_Op) = E_Function then
7656 Error_Msg_N
7657 ("% operation must be a procedure", Found_Op);
7658 end if;
7659 end if;
7660 end Validate_Simple_Pool_Operation;
7662 -- Start of processing for Validate_Simple_Pool_Ops
7664 begin
7665 Validate_Simple_Pool_Operation (Name_Allocate);
7666 Validate_Simple_Pool_Operation (Name_Deallocate);
7667 Validate_Simple_Pool_Operation (Name_Storage_Size);
7668 end Validate_Simple_Pool_Ops;
7669 end if;
7670 end if;
7672 -- Now that all types from which E may depend are frozen, see if
7673 -- strict alignment is required, a component clause on a record
7674 -- is correct, the size is known at compile time and if it must
7675 -- be unsigned, in that order.
7677 if Base_Type (E) = E then
7678 Check_Strict_Alignment (E);
7679 end if;
7681 if Ekind (E) in E_Record_Type | E_Record_Subtype then
7682 declare
7683 RC : constant Node_Id := Get_Record_Representation_Clause (E);
7684 begin
7685 if Present (RC) then
7686 Check_Record_Representation_Clause (RC);
7687 end if;
7688 end;
7689 end if;
7691 Check_Compile_Time_Size (E);
7693 Check_Unsigned_Type (E);
7695 -- Do not allow a size clause for a type which does not have a size
7696 -- that is known at compile time
7698 if (Has_Size_Clause (E) or else Has_Object_Size_Clause (E))
7699 and then not Size_Known_At_Compile_Time (E)
7700 then
7701 -- Suppress this message if errors posted on E, even if we are
7702 -- in all errors mode, since this is often a junk message
7704 if not Error_Posted (E) then
7705 Error_Msg_N
7706 ("size clause not allowed for variable length type",
7707 Size_Clause (E));
7708 end if;
7709 end if;
7711 -- Now we set/verify the representation information, in particular
7712 -- the size and alignment values. This processing is not required for
7713 -- generic types, since generic types do not play any part in code
7714 -- generation, and so the size and alignment values for such types
7715 -- are irrelevant. Ditto for types declared within a generic unit,
7716 -- which may have components that depend on generic parameters, and
7717 -- that will be recreated in an instance.
7719 if Inside_A_Generic then
7720 null;
7722 -- Otherwise we call the layout procedure
7724 else
7725 Layout_Type (E);
7726 end if;
7728 -- If this is an access to subprogram whose designated type is itself
7729 -- a subprogram type, the return type of this anonymous subprogram
7730 -- type must be decorated as well.
7732 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
7733 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
7734 then
7735 Layout_Type (Etype (Designated_Type (E)));
7736 end if;
7738 -- If the type has a Defaut_Value/Default_Component_Value aspect,
7739 -- this is where we analyze the expression (after the type is frozen,
7740 -- since in the case of Default_Value, we are analyzing with the
7741 -- type itself, and we treat Default_Component_Value similarly for
7742 -- the sake of uniformity).
7744 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
7745 declare
7746 Nam : Name_Id;
7747 Exp : Node_Id;
7748 Typ : Entity_Id;
7750 begin
7751 if Is_Scalar_Type (E) then
7752 Nam := Name_Default_Value;
7753 Typ := E;
7754 Exp := Default_Aspect_Value (Typ);
7755 else
7756 Nam := Name_Default_Component_Value;
7757 Typ := Component_Type (E);
7758 Exp := Default_Aspect_Component_Value (E);
7759 end if;
7761 Analyze_And_Resolve (Exp, Typ);
7763 if Etype (Exp) /= Any_Type then
7764 if not Is_OK_Static_Expression (Exp) then
7765 Error_Msg_Name_1 := Nam;
7766 Flag_Non_Static_Expr
7767 ("aspect% requires static expression", Exp);
7768 end if;
7769 end if;
7770 end;
7771 end if;
7773 -- Verify at this point that No_Controlled_Parts and No_Task_Parts,
7774 -- when specified on the current type or one of its ancestors, has
7775 -- not been overridden and that no violation of the aspect has
7776 -- occurred.
7778 -- It is important that we perform the checks here after the type has
7779 -- been processed because if said type depended on a private type it
7780 -- will not have been marked controlled or having tasks.
7782 Check_No_Parts_Violations (E, Aspect_No_Controlled_Parts);
7783 Check_No_Parts_Violations (E, Aspect_No_Task_Parts);
7785 -- End of freeze processing for type entities
7786 end if;
7788 -- Here is where we logically freeze the current entity. If it has a
7789 -- freeze node, then this is the point at which the freeze node is
7790 -- linked into the result list.
7792 if Has_Delayed_Freeze (E) then
7794 -- If a freeze node is already allocated, use it, otherwise allocate
7795 -- a new one. The preallocation happens in the case of anonymous base
7796 -- types, where we preallocate so that we can set First_Subtype_Link.
7797 -- Note that we reset the Sloc to the current freeze location.
7799 if Present (Freeze_Node (E)) then
7800 F_Node := Freeze_Node (E);
7801 Set_Sloc (F_Node, Loc);
7803 else
7804 F_Node := New_Node (N_Freeze_Entity, Loc);
7805 Set_Freeze_Node (E, F_Node);
7806 Set_Access_Types_To_Process (F_Node, No_Elist);
7807 Set_TSS_Elist (F_Node, No_Elist);
7808 Set_Actions (F_Node, No_List);
7809 end if;
7811 Set_Entity (F_Node, E);
7812 Add_To_Result (F_Node);
7814 -- A final pass over record types with discriminants. If the type
7815 -- has an incomplete declaration, there may be constrained access
7816 -- subtypes declared elsewhere, which do not depend on the discrimi-
7817 -- nants of the type, and which are used as component types (i.e.
7818 -- the full view is a recursive type). The designated types of these
7819 -- subtypes can only be elaborated after the type itself, and they
7820 -- need an itype reference.
7822 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
7823 declare
7824 Comp : Entity_Id;
7825 IR : Node_Id;
7826 Typ : Entity_Id;
7828 begin
7829 Comp := First_Component (E);
7830 while Present (Comp) loop
7831 Typ := Etype (Comp);
7833 if Is_Access_Type (Typ)
7834 and then Scope (Typ) /= E
7835 and then Base_Type (Designated_Type (Typ)) = E
7836 and then Is_Itype (Designated_Type (Typ))
7837 then
7838 IR := Make_Itype_Reference (Sloc (Comp));
7839 Set_Itype (IR, Designated_Type (Typ));
7840 Append (IR, Result);
7841 end if;
7843 Next_Component (Comp);
7844 end loop;
7845 end;
7846 end if;
7847 end if;
7849 -- When a type is frozen, the first subtype of the type is frozen as
7850 -- well (RM 13.14(15)). This has to be done after freezing the type,
7851 -- since obviously the first subtype depends on its own base type.
7853 if Is_Type (E) then
7854 Freeze_And_Append (First_Subtype (E), N, Result);
7856 -- If we just froze a tagged non-class-wide record, then freeze the
7857 -- corresponding class-wide type. This must be done after the tagged
7858 -- type itself is frozen, because the class-wide type refers to the
7859 -- tagged type which generates the class.
7861 -- For a tagged type, freeze explicitly those primitive operations
7862 -- that are expression functions, which otherwise have no clear
7863 -- freeze point: these have to be frozen before the dispatch table
7864 -- for the type is built, and before any explicit call to the
7865 -- primitive, which would otherwise be the freeze point for it.
7867 if Is_Tagged_Type (E)
7868 and then not Is_Class_Wide_Type (E)
7869 and then Present (Class_Wide_Type (E))
7870 then
7871 Freeze_And_Append (Class_Wide_Type (E), N, Result);
7873 declare
7874 Ops : constant Elist_Id := Primitive_Operations (E);
7876 Elmt : Elmt_Id;
7877 Subp : Entity_Id;
7879 begin
7880 if Ops /= No_Elist then
7881 Elmt := First_Elmt (Ops);
7882 while Present (Elmt) loop
7883 Subp := Node (Elmt);
7884 if Is_Expression_Function (Subp) then
7885 Freeze_And_Append (Subp, N, Result);
7886 end if;
7888 Next_Elmt (Elmt);
7889 end loop;
7890 end if;
7891 end;
7892 end if;
7893 end if;
7895 Check_Debug_Info_Needed (E);
7897 -- If subprogram has address clause then reset Is_Public flag, since we
7898 -- do not want the backend to generate external references.
7900 if Is_Subprogram (E)
7901 and then Present (Address_Clause (E))
7902 and then not Is_Library_Level_Entity (E)
7903 then
7904 Set_Is_Public (E, False);
7905 end if;
7907 -- The Ghost mode of the enclosing context is ignored, while the
7908 -- entity being frozen is living. Insert the freezing action prior
7909 -- to the start of the enclosing ignored Ghost region. As a result
7910 -- the freezeing action will be preserved when the ignored Ghost
7911 -- context is eliminated. The insertion must take place even when
7912 -- the context is a spec expression, otherwise "Handling of Default
7913 -- and Per-Object Expressions" will suppress the insertion, and the
7914 -- freeze node will be dropped on the floor.
7916 if Saved_GM = Ignore
7917 and then Ghost_Mode /= Ignore
7918 and then Present (Ignored_Ghost_Region)
7919 then
7920 Insert_Actions
7921 (Assoc_Node => Ignored_Ghost_Region,
7922 Ins_Actions => Result,
7923 Spec_Expr_OK => True);
7925 Result := No_List;
7926 end if;
7928 <<Leave>>
7929 Restore_Ghost_Region (Saved_GM, Saved_IGR);
7931 return Result;
7932 end Freeze_Entity;
7934 -----------------------------
7935 -- Freeze_Enumeration_Type --
7936 -----------------------------
7938 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
7939 begin
7940 -- By default, if no size clause is present, an enumeration type with
7941 -- Convention C is assumed to interface to a C enum and has integer
7942 -- size, except for a boolean type because it is assumed to interface
7943 -- to _Bool introduced in C99. This applies to types. For subtypes,
7944 -- verify that its base type has no size clause either. Treat other
7945 -- foreign conventions in the same way, and also make sure alignment
7946 -- is set right.
7948 if Has_Foreign_Convention (Typ)
7949 and then not Is_Boolean_Type (Typ)
7950 and then not Has_Size_Clause (Typ)
7951 and then not Has_Size_Clause (Base_Type (Typ))
7952 and then Esize (Typ) < Standard_Integer_Size
7954 -- Don't do this if Short_Enums on target
7956 and then not Target_Short_Enums
7957 then
7958 Set_Esize (Typ, UI_From_Int (Standard_Integer_Size));
7959 Set_Alignment (Typ, Alignment (Standard_Integer));
7961 -- Normal Ada case or size clause present or not Long_C_Enums on target
7963 else
7964 -- If the enumeration type interfaces to C, and it has a size clause
7965 -- that is smaller than the size of int, it warrants a warning. The
7966 -- user may intend the C type to be a boolean or a char, so this is
7967 -- not by itself an error that the Ada compiler can detect, but it
7968 -- is worth a heads-up. For Boolean and Character types we
7969 -- assume that the programmer has the proper C type in mind.
7970 -- For explicit sizes larger than int, assume the user knows what
7971 -- he is doing and that the code is intentional.
7973 if Convention (Typ) = Convention_C
7974 and then Has_Size_Clause (Typ)
7975 and then Esize (Typ) < Standard_Integer_Size
7976 and then not Is_Boolean_Type (Typ)
7977 and then not Is_Character_Type (Typ)
7979 -- Don't do this if Short_Enums on target
7981 and then not Target_Short_Enums
7982 then
7983 Error_Msg_N
7984 ("??the size of enums in C is implementation-defined",
7985 Size_Clause (Typ));
7986 Error_Msg_N
7987 ("\??check that the C counterpart has size of " &
7988 UI_Image (Esize (Typ)),
7989 Size_Clause (Typ));
7990 end if;
7992 Adjust_Esize_For_Alignment (Typ);
7993 end if;
7994 end Freeze_Enumeration_Type;
7996 -----------------------
7997 -- Freeze_Expression --
7998 -----------------------
8000 procedure Freeze_Expression (N : Node_Id) is
8002 function Find_Aggregate_Component_Desig_Type return Entity_Id;
8003 -- If the expression is an array aggregate, the type of the component
8004 -- expressions is also frozen. If the component type is an access type
8005 -- and the expressions include allocators, the designed type is frozen
8006 -- as well.
8008 function In_Expanded_Body (N : Node_Id) return Boolean;
8009 -- Given an N_Handled_Sequence_Of_Statements node, determines whether it
8010 -- is the statement sequence of an expander-generated subprogram: body
8011 -- created for an expression function, for a predicate function, an init
8012 -- proc, a stream subprogram, or a renaming as body. If so, this is not
8013 -- a freezing context and the entity will be frozen at a later point.
8015 function Has_Decl_In_List
8016 (E : Entity_Id;
8017 N : Node_Id;
8018 L : List_Id) return Boolean;
8019 -- Determines whether an entity E referenced in node N is declared in
8020 -- the list L.
8022 -----------------------------------------
8023 -- Find_Aggregate_Component_Desig_Type --
8024 -----------------------------------------
8026 function Find_Aggregate_Component_Desig_Type return Entity_Id is
8027 Assoc : Node_Id;
8028 Exp : Node_Id;
8030 begin
8031 if Present (Expressions (N)) then
8032 Exp := First (Expressions (N));
8033 while Present (Exp) loop
8034 if Nkind (Exp) = N_Allocator then
8035 return Designated_Type (Component_Type (Etype (N)));
8036 end if;
8038 Next (Exp);
8039 end loop;
8040 end if;
8042 if Present (Component_Associations (N)) then
8043 Assoc := First (Component_Associations (N));
8044 while Present (Assoc) loop
8045 if Nkind (Expression (Assoc)) = N_Allocator then
8046 return Designated_Type (Component_Type (Etype (N)));
8047 end if;
8049 Next (Assoc);
8050 end loop;
8051 end if;
8053 return Empty;
8054 end Find_Aggregate_Component_Desig_Type;
8056 ----------------------
8057 -- In_Expanded_Body --
8058 ----------------------
8060 function In_Expanded_Body (N : Node_Id) return Boolean is
8061 P : constant Node_Id := Parent (N);
8062 Id : Entity_Id;
8064 begin
8065 if Nkind (P) /= N_Subprogram_Body then
8066 return False;
8068 -- AI12-0157: An expression function that is a completion is a freeze
8069 -- point. If the body is the result of expansion, it is not.
8071 elsif Was_Expression_Function (P) then
8072 return not Comes_From_Source (P);
8074 -- This is the body of a generated predicate function
8076 elsif Present (Corresponding_Spec (P))
8077 and then Is_Predicate_Function (Corresponding_Spec (P))
8078 then
8079 return True;
8081 else
8082 Id := Defining_Unit_Name (Specification (P));
8084 -- The following are expander-created bodies, or bodies that
8085 -- are not freeze points.
8087 if Nkind (Id) = N_Defining_Identifier
8088 and then (Is_Init_Proc (Id)
8089 or else Is_TSS (Id, TSS_Stream_Input)
8090 or else Is_TSS (Id, TSS_Stream_Output)
8091 or else Is_TSS (Id, TSS_Stream_Read)
8092 or else Is_TSS (Id, TSS_Stream_Write)
8093 or else Is_TSS (Id, TSS_Put_Image)
8094 or else Nkind (Original_Node (P)) =
8095 N_Subprogram_Renaming_Declaration)
8096 then
8097 return True;
8098 else
8099 return False;
8100 end if;
8101 end if;
8102 end In_Expanded_Body;
8104 ----------------------
8105 -- Has_Decl_In_List --
8106 ----------------------
8108 function Has_Decl_In_List
8109 (E : Entity_Id;
8110 N : Node_Id;
8111 L : List_Id) return Boolean
8113 Decl_Node : Node_Id;
8115 begin
8116 -- If E is an itype, pretend that it is declared in N
8118 if Is_Itype (E) then
8119 Decl_Node := N;
8120 else
8121 Decl_Node := Declaration_Node (E);
8122 end if;
8124 return Is_List_Member (Decl_Node)
8125 and then List_Containing (Decl_Node) = L;
8126 end Has_Decl_In_List;
8128 -- Local variables
8130 In_Spec_Exp : constant Boolean := In_Spec_Expression;
8132 Desig_Typ : Entity_Id;
8133 Nam : Entity_Id;
8134 P : Node_Id;
8135 Parent_P : Node_Id;
8136 Typ : Entity_Id;
8138 Allocator_Typ : Entity_Id := Empty;
8140 Freeze_Outside : Boolean := False;
8141 -- This flag is set true if the entity must be frozen outside the
8142 -- current subprogram. This happens in the case of expander generated
8143 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
8144 -- not freeze all entities like other bodies, but which nevertheless
8145 -- may reference entities that have to be frozen before the body and
8146 -- obviously cannot be frozen inside the body.
8148 Freeze_Outside_Subp : Entity_Id := Empty;
8149 -- This entity is set if we are inside a subprogram body and the frozen
8150 -- entity is defined in the enclosing scope of this subprogram. In such
8151 -- case we must skip the subprogram body when climbing the parents chain
8152 -- to locate the correct placement for the freezing node.
8154 -- Start of processing for Freeze_Expression
8156 begin
8157 -- Immediate return if freezing is inhibited. This flag is set by the
8158 -- analyzer to stop freezing on generated expressions that would cause
8159 -- freezing if they were in the source program, but which are not
8160 -- supposed to freeze, since they are created.
8162 if Must_Not_Freeze (N) then
8163 return;
8164 end if;
8166 -- If expression is non-static, then it does not freeze in a default
8167 -- expression, see section "Handling of Default Expressions" in the
8168 -- spec of package Sem for further details. Note that we have to make
8169 -- sure that we actually have a real expression (if we have a subtype
8170 -- indication, we can't test Is_OK_Static_Expression). However, we
8171 -- exclude the case of the prefix of an attribute of a static scalar
8172 -- subtype from this early return, because static subtype attributes
8173 -- should always cause freezing, even in default expressions, but
8174 -- the attribute may not have been marked as static yet (because in
8175 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
8176 -- Freeze_Expression on the prefix).
8178 if In_Spec_Exp
8179 and then Nkind (N) in N_Subexpr
8180 and then not Is_OK_Static_Expression (N)
8181 and then (Nkind (Parent (N)) /= N_Attribute_Reference
8182 or else not (Is_Entity_Name (N)
8183 and then Is_Type (Entity (N))
8184 and then Is_OK_Static_Subtype (Entity (N))))
8185 then
8186 return;
8187 end if;
8189 -- Freeze type of expression if not frozen already
8191 Typ := Empty;
8193 if Nkind (N) in N_Has_Etype and then Present (Etype (N)) then
8194 if not Is_Frozen (Etype (N)) then
8195 Typ := Etype (N);
8197 -- Base type may be an derived numeric type that is frozen at the
8198 -- point of declaration, but first_subtype is still unfrozen.
8200 elsif not Is_Frozen (First_Subtype (Etype (N))) then
8201 Typ := First_Subtype (Etype (N));
8202 end if;
8203 end if;
8205 -- For entity name, freeze entity if not frozen already. A special
8206 -- exception occurs for an identifier that did not come from source.
8207 -- We don't let such identifiers freeze a non-internal entity, i.e.
8208 -- an entity that did come from source, since such an identifier was
8209 -- generated by the expander, and cannot have any semantic effect on
8210 -- the freezing semantics. For example, this stops the parameter of
8211 -- an initialization procedure from freezing the variable.
8213 if Is_Entity_Name (N)
8214 and then Present (Entity (N))
8215 and then not Is_Frozen (Entity (N))
8216 and then (Nkind (N) /= N_Identifier
8217 or else Comes_From_Source (N)
8218 or else not Comes_From_Source (Entity (N)))
8219 then
8220 Nam := Entity (N);
8222 if Present (Nam) and then Ekind (Nam) = E_Function then
8223 Check_Expression_Function (N, Nam);
8224 end if;
8226 else
8227 Nam := Empty;
8228 end if;
8230 -- For an allocator freeze designated type if not frozen already
8232 -- For an aggregate whose component type is an access type, freeze the
8233 -- designated type now, so that its freeze does not appear within the
8234 -- loop that might be created in the expansion of the aggregate. If the
8235 -- designated type is a private type without full view, the expression
8236 -- cannot contain an allocator, so the type is not frozen.
8238 -- For a function, we freeze the entity when the subprogram declaration
8239 -- is frozen, but a function call may appear in an initialization proc.
8240 -- before the declaration is frozen. We need to generate the extra
8241 -- formals, if any, to ensure that the expansion of the call includes
8242 -- the proper actuals. This only applies to Ada subprograms, not to
8243 -- imported ones.
8245 Desig_Typ := Empty;
8247 case Nkind (N) is
8248 when N_Allocator =>
8249 Desig_Typ := Designated_Type (Etype (N));
8251 if Nkind (Expression (N)) = N_Qualified_Expression then
8252 Allocator_Typ := Entity (Subtype_Mark (Expression (N)));
8253 end if;
8255 when N_Aggregate =>
8256 if Is_Array_Type (Etype (N))
8257 and then Is_Access_Type (Component_Type (Etype (N)))
8258 then
8259 -- Check whether aggregate includes allocators
8261 Desig_Typ := Find_Aggregate_Component_Desig_Type;
8262 end if;
8264 when N_Indexed_Component
8265 | N_Selected_Component
8266 | N_Slice
8268 if Is_Access_Type (Etype (Prefix (N))) then
8269 Desig_Typ := Designated_Type (Etype (Prefix (N)));
8270 end if;
8272 when N_Identifier =>
8273 if Present (Nam)
8274 and then Ekind (Nam) = E_Function
8275 and then Nkind (Parent (N)) = N_Function_Call
8276 and then not Has_Foreign_Convention (Nam)
8277 then
8278 Create_Extra_Formals (Nam);
8279 end if;
8281 when others =>
8282 null;
8283 end case;
8285 if Desig_Typ /= Empty
8286 and then (Is_Frozen (Desig_Typ)
8287 or else (not Is_Fully_Defined (Desig_Typ)))
8288 then
8289 Desig_Typ := Empty;
8290 end if;
8292 -- All done if nothing needs freezing
8294 if No (Typ)
8295 and then No (Nam)
8296 and then No (Desig_Typ)
8297 and then No (Allocator_Typ)
8298 then
8299 return;
8300 end if;
8302 -- Check if we are inside a subprogram body and the frozen entity is
8303 -- defined in the enclosing scope of this subprogram. In such case we
8304 -- must skip the subprogram when climbing the parents chain to locate
8305 -- the correct placement for the freezing node.
8307 -- This is not needed for default expressions and other spec expressions
8308 -- in generic units since the Move_Freeze_Nodes mechanism (sem_ch12.adb)
8309 -- takes care of placing them at the proper place, after the generic
8310 -- unit.
8312 if Present (Nam)
8313 and then Scope (Nam) /= Current_Scope
8314 and then not (In_Spec_Exp and then Inside_A_Generic)
8315 then
8316 declare
8317 S : Entity_Id := Current_Scope;
8319 begin
8320 while Present (S)
8321 and then In_Same_Source_Unit (Nam, S)
8322 loop
8323 if Scope (S) = Scope (Nam) then
8324 if Is_Subprogram (S) and then Has_Completion (S) then
8325 Freeze_Outside_Subp := S;
8326 end if;
8328 exit;
8329 end if;
8331 S := Scope (S);
8332 end loop;
8333 end;
8334 end if;
8336 -- Examine the enclosing context by climbing the parent chain
8338 -- If we identified that we must freeze the entity outside of a given
8339 -- subprogram then we just climb up to that subprogram checking if some
8340 -- enclosing node is marked as Must_Not_Freeze (since in such case we
8341 -- must not freeze yet this entity).
8343 P := N;
8345 if Present (Freeze_Outside_Subp) then
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 -- If the parent is a subprogram body, the candidate insertion
8367 -- point is just ahead of it.
8369 if Nkind (Parent_P) = N_Subprogram_Body
8370 and then Unique_Defining_Entity (Parent_P) =
8371 Freeze_Outside_Subp
8372 then
8373 P := Parent_P;
8374 exit;
8375 end if;
8377 P := Parent_P;
8378 end loop;
8380 -- Otherwise the traversal serves two purposes - to detect scenarios
8381 -- where freezeing is not needed and to find the proper insertion point
8382 -- for the freeze nodes. Although somewhat similar to Insert_Actions,
8383 -- this traversal is freezing semantics-sensitive. Inserting freeze
8384 -- nodes blindly in the tree may result in types being frozen too early.
8386 else
8387 loop
8388 -- Do not freeze the current expression if another expression in
8389 -- the chain of parents must not be frozen.
8391 if Nkind (P) in N_Subexpr and then Must_Not_Freeze (P) then
8392 return;
8393 end if;
8395 Parent_P := Parent (P);
8397 -- If we don't have a parent, then we are not in a well-formed
8398 -- tree. This is an unusual case, but there are some legitimate
8399 -- situations in which this occurs, notably when the expressions
8400 -- in the range of a type declaration are resolved. We simply
8401 -- ignore the freeze request in this case.
8403 if No (Parent_P) then
8404 return;
8405 end if;
8407 -- See if we have got to an appropriate point in the tree
8409 case Nkind (Parent_P) is
8411 -- A special test for the exception of (RM 13.14(8)) for the
8412 -- case of per-object expressions (RM 3.8(18)) occurring in
8413 -- component definition or a discrete subtype definition. Note
8414 -- that we test for a component declaration which includes both
8415 -- cases we are interested in, and furthermore the tree does
8416 -- not have explicit nodes for either of these two constructs.
8418 when N_Component_Declaration =>
8420 -- The case we want to test for here is an identifier that
8421 -- is a per-object expression, this is either a discriminant
8422 -- that appears in a context other than the component
8423 -- declaration or it is a reference to the type of the
8424 -- enclosing construct.
8426 -- For either of these cases, we skip the freezing
8428 if not In_Spec_Expression
8429 and then Nkind (N) = N_Identifier
8430 and then (Present (Entity (N)))
8431 then
8432 -- We recognize the discriminant case by just looking for
8433 -- a reference to a discriminant. It can only be one for
8434 -- the enclosing construct. Skip freezing in this case.
8436 if Ekind (Entity (N)) = E_Discriminant then
8437 return;
8439 -- For the case of a reference to the enclosing record,
8440 -- (or task or protected type), we look for a type that
8441 -- matches the current scope.
8443 elsif Entity (N) = Current_Scope then
8444 return;
8445 end if;
8446 end if;
8448 -- If we have an enumeration literal that appears as the choice
8449 -- in the aggregate of an enumeration representation clause,
8450 -- then freezing does not occur (RM 13.14(10)).
8452 when N_Enumeration_Representation_Clause =>
8454 -- The case we are looking for is an enumeration literal
8456 if Nkind (N) in N_Identifier | N_Character_Literal
8457 and then Is_Enumeration_Type (Etype (N))
8458 then
8459 -- If enumeration literal appears directly as the choice,
8460 -- do not freeze (this is the normal non-overloaded case)
8462 if Nkind (Parent (N)) = N_Component_Association
8463 and then First (Choices (Parent (N))) = N
8464 then
8465 return;
8467 -- If enumeration literal appears as the name of function
8468 -- which is the choice, then also do not freeze. This
8469 -- happens in the overloaded literal case, where the
8470 -- enumeration literal is temporarily changed to a
8471 -- function call for overloading analysis purposes.
8473 elsif Nkind (Parent (N)) = N_Function_Call
8474 and then Nkind (Parent (Parent (N))) =
8475 N_Component_Association
8476 and then First (Choices (Parent (Parent (N)))) =
8477 Parent (N)
8478 then
8479 return;
8480 end if;
8481 end if;
8483 -- Normally if the parent is a handled sequence of statements,
8484 -- then the current node must be a statement, and that is an
8485 -- appropriate place to insert a freeze node.
8487 when N_Handled_Sequence_Of_Statements =>
8489 -- An exception occurs when the sequence of statements is
8490 -- for an expander generated body that did not do the usual
8491 -- freeze all operation. In this case we usually want to
8492 -- freeze outside this body, not inside it, and we skip
8493 -- past the subprogram body that we are inside.
8495 if In_Expanded_Body (Parent_P) then
8496 declare
8497 Subp_Body : constant Node_Id := Parent (Parent_P);
8498 Spec_Id : Entity_Id;
8500 begin
8501 -- Freeze the entity only when it is declared inside
8502 -- the body of the expander generated procedure. This
8503 -- case is recognized by the subprogram scope of the
8504 -- entity or its type, which is either the spec of an
8505 -- enclosing body, or (in the case of init_procs for
8506 -- which there is no separate spec) the current scope.
8508 if Nkind (Subp_Body) = N_Subprogram_Body then
8509 declare
8510 S : Entity_Id;
8512 begin
8513 Spec_Id := Corresponding_Spec (Subp_Body);
8515 if Present (Typ) then
8516 S := Scope (Typ);
8517 elsif Present (Nam) then
8518 S := Scope (Nam);
8519 else
8520 S := Standard_Standard;
8521 end if;
8523 while S /= Standard_Standard
8524 and then not Is_Subprogram (S)
8525 loop
8526 S := Scope (S);
8527 end loop;
8529 if S = Spec_Id then
8530 exit;
8532 elsif Present (Typ)
8533 and then Scope (Typ) = Current_Scope
8534 and then
8535 Defining_Entity (Subp_Body) = Current_Scope
8536 then
8537 exit;
8538 end if;
8539 end;
8540 end if;
8542 -- If the entity is not frozen by an expression
8543 -- function that is not a completion, continue
8544 -- climbing the tree.
8546 if Nkind (Subp_Body) = N_Subprogram_Body
8547 and then Was_Expression_Function (Subp_Body)
8548 then
8549 null;
8551 -- Freeze outside the body
8553 else
8554 Parent_P := Parent (Parent_P);
8555 Freeze_Outside := True;
8556 end if;
8557 end;
8559 -- Here if normal case where we are in handled statement
8560 -- sequence and want to do the insertion right there.
8562 else
8563 exit;
8564 end if;
8566 -- If parent is a body or a spec or a block, then the current
8567 -- node is a statement or declaration and we can insert the
8568 -- freeze node before it.
8570 when N_Block_Statement
8571 | N_Entry_Body
8572 | N_Package_Body
8573 | N_Package_Specification
8574 | N_Protected_Body
8575 | N_Subprogram_Body
8576 | N_Task_Body
8578 exit;
8580 -- The expander is allowed to define types in any statements
8581 -- list, so any of the following parent nodes also mark a
8582 -- freezing point if the actual node is in a list of
8583 -- statements or declarations.
8585 when N_Abortable_Part
8586 | N_Accept_Alternative
8587 | N_Case_Statement_Alternative
8588 | N_Compilation_Unit_Aux
8589 | N_Conditional_Entry_Call
8590 | N_Delay_Alternative
8591 | N_Elsif_Part
8592 | N_Entry_Call_Alternative
8593 | N_Exception_Handler
8594 | N_Extended_Return_Statement
8595 | N_Freeze_Entity
8596 | N_If_Statement
8597 | N_Selective_Accept
8598 | N_Triggering_Alternative
8600 exit when Is_List_Member (P);
8602 -- The freeze nodes produced by an expression coming from the
8603 -- Actions list of an N_Expression_With_Actions, short-circuit
8604 -- expression or N_Case_Expression_Alternative node must remain
8605 -- within the Actions list if they freeze an entity declared in
8606 -- this list, as inserting the freeze nodes further up the tree
8607 -- may lead to use before declaration issues for the entity.
8609 when N_Case_Expression_Alternative
8610 | N_Expression_With_Actions
8611 | N_Short_Circuit
8613 exit when (Present (Nam)
8614 and then
8615 Has_Decl_In_List (Nam, P, Actions (Parent_P)))
8616 or else (Present (Typ)
8617 and then
8618 Has_Decl_In_List (Typ, P, Actions (Parent_P)));
8620 -- Likewise for an N_If_Expression and its two Actions list
8622 when N_If_Expression =>
8623 declare
8624 L1 : constant List_Id := Then_Actions (Parent_P);
8625 L2 : constant List_Id := Else_Actions (Parent_P);
8627 begin
8628 exit when (Present (Nam)
8629 and then
8630 Has_Decl_In_List (Nam, P, L1))
8631 or else (Present (Typ)
8632 and then
8633 Has_Decl_In_List (Typ, P, L1))
8634 or else (Present (Nam)
8635 and then
8636 Has_Decl_In_List (Nam, P, L2))
8637 or else (Present (Typ)
8638 and then
8639 Has_Decl_In_List (Typ, P, L2));
8640 end;
8642 -- N_Loop_Statement is a special case: a type that appears in
8643 -- the source can never be frozen in a loop (this occurs only
8644 -- because of a loop expanded by the expander), so we keep on
8645 -- going. Otherwise we terminate the search. Same is true of
8646 -- any entity which comes from source (if it has a predefined
8647 -- type, this type does not appear to come from source, but the
8648 -- entity should not be frozen here).
8650 when N_Loop_Statement =>
8651 exit when not Comes_From_Source (Etype (N))
8652 and then (No (Nam) or else not Comes_From_Source (Nam));
8654 -- For all other cases, keep looking at parents
8656 when others =>
8657 null;
8658 end case;
8660 -- We fall through the case if we did not yet find the proper
8661 -- place in the tree for inserting the freeze node, so climb.
8663 P := Parent_P;
8664 end loop;
8665 end if;
8667 -- If the expression appears in a record or an initialization procedure,
8668 -- the freeze nodes are collected and attached to the current scope, to
8669 -- be inserted and analyzed on exit from the scope, to insure that
8670 -- generated entities appear in the correct scope. If the expression is
8671 -- a default for a discriminant specification, the scope is still void.
8672 -- The expression can also appear in the discriminant part of a private
8673 -- or concurrent type.
8675 -- If the expression appears in a constrained subcomponent of an
8676 -- enclosing record declaration, the freeze nodes must be attached to
8677 -- the outer record type so they can eventually be placed in the
8678 -- enclosing declaration list.
8680 -- The other case requiring this special handling is if we are in a
8681 -- default expression, since in that case we are about to freeze a
8682 -- static type, and the freeze scope needs to be the outer scope, not
8683 -- the scope of the subprogram with the default parameter.
8685 -- For default expressions and other spec expressions in generic units,
8686 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
8687 -- placing them at the proper place, after the generic unit.
8689 if (In_Spec_Exp and not Inside_A_Generic)
8690 or else Freeze_Outside
8691 or else (Is_Type (Current_Scope)
8692 and then (not Is_Concurrent_Type (Current_Scope)
8693 or else not Has_Completion (Current_Scope)))
8694 or else Ekind (Current_Scope) = E_Void
8695 then
8696 declare
8697 Freeze_Nodes : List_Id := No_List;
8698 Pos : Int := Scope_Stack.Last;
8700 begin
8701 if Present (Desig_Typ) then
8702 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
8703 end if;
8705 if Present (Typ) then
8706 Freeze_And_Append (Typ, N, Freeze_Nodes);
8707 end if;
8709 if Present (Nam) then
8710 Freeze_And_Append (Nam, N, Freeze_Nodes);
8711 end if;
8713 -- The current scope may be that of a constrained component of
8714 -- an enclosing record declaration, or of a loop of an enclosing
8715 -- quantified expression, which is above the current scope in the
8716 -- scope stack. Indeed in the context of a quantified expression,
8717 -- a scope is created and pushed above the current scope in order
8718 -- to emulate the loop-like behavior of the quantified expression.
8719 -- If the expression is within a top-level pragma, as for a pre-
8720 -- condition on a library-level subprogram, nothing to do.
8722 if not Is_Compilation_Unit (Current_Scope)
8723 and then (Is_Record_Type (Scope (Current_Scope))
8724 or else Nkind (Parent (Current_Scope)) =
8725 N_Quantified_Expression)
8726 then
8727 Pos := Pos - 1;
8728 end if;
8730 if Is_Non_Empty_List (Freeze_Nodes) then
8732 -- When the current scope is transient, insert the freeze nodes
8733 -- prior to the expression that produced them. Transient scopes
8734 -- may create additional declarations when finalizing objects
8735 -- or managing the secondary stack. Inserting the freeze nodes
8736 -- of those constructs prior to the scope would result in a
8737 -- freeze-before-declaration, therefore the freeze node must
8738 -- remain interleaved with their constructs.
8740 if Scope_Is_Transient then
8741 Insert_Actions (N, Freeze_Nodes);
8743 elsif No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
8744 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
8745 Freeze_Nodes;
8746 else
8747 Append_List (Freeze_Nodes,
8748 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
8749 end if;
8750 end if;
8751 end;
8753 return;
8754 end if;
8756 -- Now we have the right place to do the freezing. First, a special
8757 -- adjustment, if we are in spec-expression analysis mode, these freeze
8758 -- actions must not be thrown away (normally all inserted actions are
8759 -- thrown away in this mode. However, the freeze actions are from static
8760 -- expressions and one of the important reasons we are doing this
8761 -- special analysis is to get these freeze actions. Therefore we turn
8762 -- off the In_Spec_Expression mode to propagate these freeze actions.
8763 -- This also means they get properly analyzed and expanded.
8765 In_Spec_Expression := False;
8767 -- Freeze the subtype mark before a qualified expression on an
8768 -- allocator as per AARM 13.14(4.a). This is needed in particular to
8769 -- generate predicate functions.
8771 if Present (Allocator_Typ) then
8772 Freeze_Before (P, Allocator_Typ);
8773 end if;
8775 -- Freeze the designated type of an allocator (RM 13.14(13))
8777 if Present (Desig_Typ) then
8778 Freeze_Before (P, Desig_Typ);
8779 end if;
8781 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
8782 -- the enumeration representation clause exception in the loop above.
8784 if Present (Typ) then
8785 Freeze_Before (P, Typ);
8786 end if;
8788 -- Freeze name if one is present (RM 13.14(11))
8790 if Present (Nam) then
8791 Freeze_Before (P, Nam);
8792 end if;
8794 -- Restore In_Spec_Expression flag
8796 In_Spec_Expression := In_Spec_Exp;
8797 end Freeze_Expression;
8799 -----------------------
8800 -- Freeze_Expr_Types --
8801 -----------------------
8803 procedure Freeze_Expr_Types
8804 (Def_Id : Entity_Id;
8805 Typ : Entity_Id;
8806 Expr : Node_Id;
8807 N : Node_Id)
8809 function Cloned_Expression return Node_Id;
8810 -- Build a duplicate of the expression of the return statement that has
8811 -- no defining entities shared with the original expression.
8813 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result;
8814 -- Freeze all types referenced in the subtree rooted at Node
8816 -----------------------
8817 -- Cloned_Expression --
8818 -----------------------
8820 function Cloned_Expression return Node_Id is
8821 function Clone_Id (Node : Node_Id) return Traverse_Result;
8822 -- Tree traversal routine that clones the defining identifier of
8823 -- iterator and loop parameter specification nodes.
8825 --------------
8826 -- Clone_Id --
8827 --------------
8829 function Clone_Id (Node : Node_Id) return Traverse_Result is
8830 begin
8831 if Nkind (Node) in
8832 N_Iterator_Specification | N_Loop_Parameter_Specification
8833 then
8834 Set_Defining_Identifier
8835 (Node, New_Copy (Defining_Identifier (Node)));
8836 end if;
8838 return OK;
8839 end Clone_Id;
8841 procedure Clone_Def_Ids is new Traverse_Proc (Clone_Id);
8843 -- Local variable
8845 Dup_Expr : constant Node_Id := New_Copy_Tree (Expr);
8847 -- Start of processing for Cloned_Expression
8849 begin
8850 -- We must duplicate the expression with semantic information to
8851 -- inherit the decoration of global entities in generic instances.
8852 -- Set the parent of the new node to be the parent of the original
8853 -- to get the proper context, which is needed for complete error
8854 -- reporting and for semantic analysis.
8856 Set_Parent (Dup_Expr, Parent (Expr));
8858 -- Replace the defining identifier of iterators and loop param
8859 -- specifications by a clone to ensure that the cloned expression
8860 -- and the original expression don't have shared identifiers;
8861 -- otherwise, as part of the preanalysis of the expression, these
8862 -- shared identifiers may be left decorated with itypes which
8863 -- will not be available in the tree passed to the backend.
8865 Clone_Def_Ids (Dup_Expr);
8867 return Dup_Expr;
8868 end Cloned_Expression;
8870 ----------------------
8871 -- Freeze_Type_Refs --
8872 ----------------------
8874 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result is
8875 procedure Check_And_Freeze_Type (Typ : Entity_Id);
8876 -- Check that Typ is fully declared and freeze it if so
8878 ---------------------------
8879 -- Check_And_Freeze_Type --
8880 ---------------------------
8882 procedure Check_And_Freeze_Type (Typ : Entity_Id) is
8883 begin
8884 -- Skip Itypes created by the preanalysis, and itypes whose
8885 -- scope is another type (i.e. component subtypes that depend
8886 -- on a discriminant),
8888 if Is_Itype (Typ)
8889 and then (Scope_Within_Or_Same (Scope (Typ), Def_Id)
8890 or else Is_Type (Scope (Typ)))
8891 then
8892 return;
8893 end if;
8895 -- This provides a better error message than generating primitives
8896 -- whose compilation fails much later. Refine the error message if
8897 -- possible.
8899 Check_Fully_Declared (Typ, Node);
8901 if Error_Posted (Node) then
8902 if Has_Private_Component (Typ)
8903 and then not Is_Private_Type (Typ)
8904 then
8905 Error_Msg_NE ("\type& has private component", Node, Typ);
8906 end if;
8908 else
8909 Freeze_Before (N, Typ);
8910 end if;
8911 end Check_And_Freeze_Type;
8913 -- Start of processing for Freeze_Type_Refs
8915 begin
8916 -- Check that a type referenced by an entity can be frozen
8918 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
8919 -- The entity itself may be a type, as in a membership test
8920 -- or an attribute reference. Freezing its own type would be
8921 -- incomplete if the entity is derived or an extension.
8923 if Is_Type (Entity (Node)) then
8924 Check_And_Freeze_Type (Entity (Node));
8926 else
8927 Check_And_Freeze_Type (Etype (Entity (Node)));
8928 end if;
8930 -- Check that the enclosing record type can be frozen
8932 if Ekind (Entity (Node)) in E_Component | E_Discriminant then
8933 Check_And_Freeze_Type (Scope (Entity (Node)));
8934 end if;
8936 -- Freezing an access type does not freeze the designated type, but
8937 -- freezing conversions between access to interfaces requires that
8938 -- the interface types themselves be frozen, so that dispatch table
8939 -- entities are properly created.
8941 -- Unclear whether a more general rule is needed ???
8943 elsif Nkind (Node) = N_Type_Conversion
8944 and then Is_Access_Type (Etype (Node))
8945 and then Is_Interface (Designated_Type (Etype (Node)))
8946 then
8947 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8948 end if;
8950 -- An implicit dereference freezes the designated type. In the case
8951 -- of a dispatching call whose controlling argument is an access
8952 -- type, the dereference is not made explicit, so we must check for
8953 -- such a call and freeze the designated type.
8955 if Nkind (Node) in N_Has_Etype
8956 and then Present (Etype (Node))
8957 and then Is_Access_Type (Etype (Node))
8958 then
8959 if Nkind (Parent (Node)) = N_Function_Call
8960 and then Node = Controlling_Argument (Parent (Node))
8961 then
8962 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8964 -- An explicit dereference freezes the designated type as well,
8965 -- even though that type is not attached to an entity in the
8966 -- expression.
8968 elsif Nkind (Parent (Node)) = N_Explicit_Dereference then
8969 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8970 end if;
8972 -- An iterator specification freezes the iterator type, even though
8973 -- that type is not attached to an entity in the construct.
8975 elsif Nkind (Node) in N_Has_Etype
8976 and then Nkind (Parent (Node)) = N_Iterator_Specification
8977 and then Node = Name (Parent (Node))
8978 then
8979 declare
8980 Iter : constant Node_Id :=
8981 Find_Value_Of_Aspect (Etype (Node), Aspect_Default_Iterator);
8983 begin
8984 if Present (Iter) then
8985 Check_And_Freeze_Type (Etype (Iter));
8986 end if;
8987 end;
8988 end if;
8990 -- No point in posting several errors on the same expression
8992 if Serious_Errors_Detected > 0 then
8993 return Abandon;
8994 else
8995 return OK;
8996 end if;
8997 end Freeze_Type_Refs;
8999 procedure Freeze_References is new Traverse_Proc (Freeze_Type_Refs);
9001 -- Local variables
9003 Saved_First_Entity : constant Entity_Id := First_Entity (Def_Id);
9004 Saved_Last_Entity : constant Entity_Id := Last_Entity (Def_Id);
9005 Dup_Expr : constant Node_Id := Cloned_Expression;
9007 -- Start of processing for Freeze_Expr_Types
9009 begin
9010 -- Preanalyze a duplicate of the expression to have available the
9011 -- minimum decoration needed to locate referenced unfrozen types
9012 -- without adding any decoration to the function expression.
9014 -- This routine is also applied to expressions in the contract for
9015 -- the subprogram. If that happens when expanding the code for
9016 -- pre/postconditions during expansion of the subprogram body, the
9017 -- subprogram is already installed.
9019 if Def_Id /= Current_Scope then
9020 Push_Scope (Def_Id);
9021 Install_Formals (Def_Id);
9023 Preanalyze_Spec_Expression (Dup_Expr, Typ);
9024 End_Scope;
9025 else
9026 Preanalyze_Spec_Expression (Dup_Expr, Typ);
9027 end if;
9029 -- Restore certain attributes of Def_Id since the preanalysis may
9030 -- have introduced itypes to this scope, thus modifying attributes
9031 -- First_Entity and Last_Entity.
9033 Set_First_Entity (Def_Id, Saved_First_Entity);
9034 Set_Last_Entity (Def_Id, Saved_Last_Entity);
9036 if Present (Last_Entity (Def_Id)) then
9037 Set_Next_Entity (Last_Entity (Def_Id), Empty);
9038 end if;
9040 -- Freeze all types referenced in the expression
9042 Freeze_References (Dup_Expr);
9043 end Freeze_Expr_Types;
9045 -----------------------------
9046 -- Freeze_Fixed_Point_Type --
9047 -----------------------------
9049 -- Certain fixed-point types and subtypes, including implicit base types
9050 -- and declared first subtypes, have not yet set up a range. This is
9051 -- because the range cannot be set until the Small and Size values are
9052 -- known, and these are not known till the type is frozen.
9054 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
9055 -- whose bounds are unanalyzed real literals. This routine will recognize
9056 -- this case, and transform this range node into a properly typed range
9057 -- with properly analyzed and resolved values.
9059 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
9060 Rng : constant Node_Id := Scalar_Range (Typ);
9061 Lo : constant Node_Id := Low_Bound (Rng);
9062 Hi : constant Node_Id := High_Bound (Rng);
9063 Btyp : constant Entity_Id := Base_Type (Typ);
9064 Brng : constant Node_Id := Scalar_Range (Btyp);
9065 BLo : constant Node_Id := Low_Bound (Brng);
9066 BHi : constant Node_Id := High_Bound (Brng);
9067 Ftyp : constant Entity_Id := Underlying_Type (First_Subtype (Typ));
9069 Small : Ureal;
9070 Loval : Ureal;
9071 Hival : Ureal;
9072 Atype : Entity_Id;
9074 Orig_Lo : Ureal;
9075 Orig_Hi : Ureal;
9076 -- Save original bounds (for shaving tests)
9078 Actual_Size : Int;
9079 -- Actual size chosen
9081 function Fsize (Lov, Hiv : Ureal) return Int;
9082 -- Returns size of type with given bounds. Also leaves these
9083 -- bounds set as the current bounds of the Typ.
9085 function Larger (A, B : Ureal) return Boolean;
9086 -- Returns true if A > B with a margin of Typ'Small
9088 function Smaller (A, B : Ureal) return Boolean;
9089 -- Returns true if A < B with a margin of Typ'Small
9091 -----------
9092 -- Fsize --
9093 -----------
9095 function Fsize (Lov, Hiv : Ureal) return Int is
9096 begin
9097 Set_Realval (Lo, Lov);
9098 Set_Realval (Hi, Hiv);
9099 return Minimum_Size (Typ);
9100 end Fsize;
9102 ------------
9103 -- Larger --
9104 ------------
9106 function Larger (A, B : Ureal) return Boolean is
9107 begin
9108 return A > B and then A - Small_Value (Typ) > B;
9109 end Larger;
9111 -------------
9112 -- Smaller --
9113 -------------
9115 function Smaller (A, B : Ureal) return Boolean is
9116 begin
9117 return A < B and then A + Small_Value (Typ) < B;
9118 end Smaller;
9120 -- Start of processing for Freeze_Fixed_Point_Type
9122 begin
9123 -- The type, or its first subtype if we are freezing the anonymous
9124 -- base, may have a delayed Small aspect. It must be analyzed now,
9125 -- so that all characteristics of the type (size, bounds) can be
9126 -- computed and validated in the call to Minimum_Size that follows.
9128 if Has_Delayed_Aspects (Ftyp) then
9129 Analyze_Aspects_At_Freeze_Point (Ftyp);
9130 Set_Has_Delayed_Aspects (Ftyp, False);
9131 end if;
9133 if May_Inherit_Delayed_Rep_Aspects (Ftyp) then
9134 Inherit_Delayed_Rep_Aspects (Ftyp);
9135 Set_May_Inherit_Delayed_Rep_Aspects (Ftyp, False);
9136 end if;
9138 -- Inherit the Small value from the first subtype in any case
9140 if Typ /= Ftyp then
9141 Set_Small_Value (Typ, Small_Value (Ftyp));
9142 end if;
9144 -- If Esize of a subtype has not previously been set, set it now
9146 if not Known_Esize (Typ) then
9147 Atype := Ancestor_Subtype (Typ);
9149 if Present (Atype) then
9150 Set_Esize (Typ, Esize (Atype));
9151 else
9152 Copy_Esize (To => Typ, From => Btyp);
9153 end if;
9154 end if;
9156 -- Immediate return if the range is already analyzed. This means that
9157 -- the range is already set, and does not need to be computed by this
9158 -- routine.
9160 if Analyzed (Rng) then
9161 return;
9162 end if;
9164 -- Immediate return if either of the bounds raises Constraint_Error
9166 if Raises_Constraint_Error (Lo)
9167 or else Raises_Constraint_Error (Hi)
9168 then
9169 return;
9170 end if;
9172 Small := Small_Value (Typ);
9173 Loval := Realval (Lo);
9174 Hival := Realval (Hi);
9176 Orig_Lo := Loval;
9177 Orig_Hi := Hival;
9179 -- Ordinary fixed-point case
9181 if Is_Ordinary_Fixed_Point_Type (Typ) then
9183 -- For the ordinary fixed-point case, we are allowed to fudge the
9184 -- end-points up or down by small. Generally we prefer to fudge up,
9185 -- i.e. widen the bounds for non-model numbers so that the end points
9186 -- are included. However there are cases in which this cannot be
9187 -- done, and indeed cases in which we may need to narrow the bounds.
9188 -- The following circuit makes the decision.
9190 -- Note: our terminology here is that Incl_EP means that the bounds
9191 -- are widened by Small if necessary to include the end points, and
9192 -- Excl_EP means that the bounds are narrowed by Small to exclude the
9193 -- end-points if this reduces the size.
9195 -- Note that in the Incl case, all we care about is including the
9196 -- end-points. In the Excl case, we want to narrow the bounds as
9197 -- much as permitted by the RM, to give the smallest possible size.
9199 Fudge : declare
9200 Loval_Incl_EP : Ureal;
9201 Hival_Incl_EP : Ureal;
9203 Loval_Excl_EP : Ureal;
9204 Hival_Excl_EP : Ureal;
9206 Size_Incl_EP : Int;
9207 Size_Excl_EP : Int;
9209 Model_Num : Ureal;
9210 Actual_Lo : Ureal;
9211 Actual_Hi : Ureal;
9213 begin
9214 -- First step. Base types are required to be symmetrical. Right
9215 -- now, the base type range is a copy of the first subtype range.
9216 -- This will be corrected before we are done, but right away we
9217 -- need to deal with the case where both bounds are non-negative.
9218 -- In this case, we set the low bound to the negative of the high
9219 -- bound, to make sure that the size is computed to include the
9220 -- required sign. Note that we do not need to worry about the
9221 -- case of both bounds negative, because the sign will be dealt
9222 -- with anyway. Furthermore we can't just go making such a bound
9223 -- symmetrical, since in a twos-complement system, there is an
9224 -- extra negative value which could not be accommodated on the
9225 -- positive side.
9227 if Typ = Btyp
9228 and then not UR_Is_Negative (Loval)
9229 and then Hival > Loval
9230 then
9231 Loval := -Hival;
9232 Set_Realval (Lo, Loval);
9233 end if;
9235 -- Compute the fudged bounds. If the bound is a model number, (or
9236 -- greater if given low bound, smaller if high bound) then we do
9237 -- nothing to include it, but we are allowed to backoff to the
9238 -- next adjacent model number when we exclude it. If it is not a
9239 -- model number then we straddle the two values with the model
9240 -- numbers on either side.
9242 Model_Num := UR_Trunc (Loval / Small) * Small;
9244 if UR_Ge (Loval, Model_Num) then
9245 Loval_Incl_EP := Model_Num;
9246 else
9247 Loval_Incl_EP := Model_Num - Small;
9248 end if;
9250 -- The low value excluding the end point is Small greater, but
9251 -- we do not do this exclusion if the low value is positive,
9252 -- since it can't help the size and could actually hurt by
9253 -- crossing the high bound.
9255 if UR_Is_Negative (Loval_Incl_EP) then
9256 Loval_Excl_EP := Loval_Incl_EP + Small;
9258 -- If the value went from negative to zero, then we have the
9259 -- case where Loval_Incl_EP is the model number just below
9260 -- zero, so we want to stick to the negative value for the
9261 -- base type to maintain the condition that the size will
9262 -- include signed values.
9264 if Typ = Btyp
9265 and then UR_Is_Zero (Loval_Excl_EP)
9266 then
9267 Loval_Excl_EP := Loval_Incl_EP;
9268 end if;
9270 else
9271 Loval_Excl_EP := Loval_Incl_EP;
9272 end if;
9274 -- Similar processing for upper bound and high value
9276 Model_Num := UR_Trunc (Hival / Small) * Small;
9278 if UR_Le (Hival, Model_Num) then
9279 Hival_Incl_EP := Model_Num;
9280 else
9281 Hival_Incl_EP := Model_Num + Small;
9282 end if;
9284 if UR_Is_Positive (Hival_Incl_EP) then
9285 Hival_Excl_EP := Hival_Incl_EP - Small;
9286 else
9287 Hival_Excl_EP := Hival_Incl_EP;
9288 end if;
9290 -- One further adjustment is needed. In the case of subtypes, we
9291 -- cannot go outside the range of the base type, or we get
9292 -- peculiarities, and the base type range is already set. This
9293 -- only applies to the Incl values, since clearly the Excl values
9294 -- are already as restricted as they are allowed to be.
9296 if Typ /= Btyp then
9297 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
9298 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
9299 end if;
9301 -- Get size including and excluding end points
9303 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
9304 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
9306 -- No need to exclude end-points if it does not reduce size
9308 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
9309 Loval_Excl_EP := Loval_Incl_EP;
9310 end if;
9312 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
9313 Hival_Excl_EP := Hival_Incl_EP;
9314 end if;
9316 -- Now we set the actual size to be used. We want to use the
9317 -- bounds fudged up to include the end-points but only if this
9318 -- can be done without violating a specifically given size
9319 -- size clause or causing an unacceptable increase in size.
9321 -- Case of size clause given
9323 if Has_Size_Clause (Typ) then
9325 -- Use the inclusive size only if it is consistent with
9326 -- the explicitly specified size.
9328 if Size_Incl_EP <= RM_Size (Typ) then
9329 Actual_Lo := Loval_Incl_EP;
9330 Actual_Hi := Hival_Incl_EP;
9331 Actual_Size := Size_Incl_EP;
9333 -- If the inclusive size is too large, we try excluding
9334 -- the end-points (will be caught later if does not work).
9336 else
9337 Actual_Lo := Loval_Excl_EP;
9338 Actual_Hi := Hival_Excl_EP;
9339 Actual_Size := Size_Excl_EP;
9340 end if;
9342 -- Case of size clause not given
9344 else
9345 -- If we have a base type whose corresponding first subtype
9346 -- has an explicit size that is large enough to include our
9347 -- end-points, then do so. There is no point in working hard
9348 -- to get a base type whose size is smaller than the specified
9349 -- size of the first subtype.
9351 if Has_Size_Clause (Ftyp)
9352 and then Size_Incl_EP <= Esize (Ftyp)
9353 then
9354 Actual_Size := Size_Incl_EP;
9355 Actual_Lo := Loval_Incl_EP;
9356 Actual_Hi := Hival_Incl_EP;
9358 -- If excluding the end-points makes the size smaller and
9359 -- results in a size of 8,16,32,64, then we take the smaller
9360 -- size. For the 64 case, this is compulsory. For the other
9361 -- cases, it seems reasonable. We like to include end points
9362 -- if we can, but not at the expense of moving to the next
9363 -- natural boundary of size.
9365 elsif Size_Incl_EP /= Size_Excl_EP
9366 and then Addressable (Size_Excl_EP)
9367 then
9368 Actual_Size := Size_Excl_EP;
9369 Actual_Lo := Loval_Excl_EP;
9370 Actual_Hi := Hival_Excl_EP;
9372 -- Otherwise we can definitely include the end points
9374 else
9375 Actual_Size := Size_Incl_EP;
9376 Actual_Lo := Loval_Incl_EP;
9377 Actual_Hi := Hival_Incl_EP;
9378 end if;
9380 -- One pathological case: normally we never fudge a low bound
9381 -- down, since it would seem to increase the size (if it has
9382 -- any effect), but for ranges containing single value, or no
9383 -- values, the high bound can be small too large. Consider:
9385 -- type t is delta 2.0**(-14)
9386 -- range 131072.0 .. 0;
9388 -- That lower bound is *just* outside the range of 32 bits, and
9389 -- does need fudging down in this case. Note that the bounds
9390 -- will always have crossed here, since the high bound will be
9391 -- fudged down if necessary, as in the case of:
9393 -- type t is delta 2.0**(-14)
9394 -- range 131072.0 .. 131072.0;
9396 -- So we detect the situation by looking for crossed bounds,
9397 -- and if the bounds are crossed, and the low bound is greater
9398 -- than zero, we will always back it off by small, since this
9399 -- is completely harmless.
9401 if Actual_Lo > Actual_Hi then
9402 if UR_Is_Positive (Actual_Lo) then
9403 Actual_Lo := Loval_Incl_EP - Small;
9404 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
9406 -- And of course, we need to do exactly the same parallel
9407 -- fudge for flat ranges in the negative region.
9409 elsif UR_Is_Negative (Actual_Hi) then
9410 Actual_Hi := Hival_Incl_EP + Small;
9411 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
9412 end if;
9413 end if;
9414 end if;
9416 Set_Realval (Lo, Actual_Lo);
9417 Set_Realval (Hi, Actual_Hi);
9418 end Fudge;
9420 -- Enforce some limitations for ordinary fixed-point types. They come
9421 -- from an exact algorithm used to implement Text_IO.Fixed_IO and the
9422 -- Fore, Image and Value attributes. The requirement on the Small is
9423 -- to lie in the range 2**(-(Siz - 1)) .. 2**(Siz - 1) for a type of
9424 -- Siz bits (Siz=32,64,128) and the requirement on the bounds is to
9425 -- be smaller in magnitude than 10.0**N * 2**(Siz - 1), where N is
9426 -- given by the formula N = floor ((Siz - 1) * log 2 / log 10).
9428 -- If the bounds of a 32-bit type are too large, force 64-bit type
9430 if Actual_Size <= 32
9431 and then Small <= Ureal_2_31
9432 and then (Smaller (Expr_Value_R (Lo), Ureal_M_2_10_18)
9433 or else Larger (Expr_Value_R (Hi), Ureal_2_10_18))
9434 then
9435 Actual_Size := 33;
9436 end if;
9438 -- If the bounds of a 64-bit type are too large, force 128-bit type
9440 if System_Max_Integer_Size = 128
9441 and then Actual_Size <= 64
9442 and then Small <= Ureal_2_63
9443 and then (Smaller (Expr_Value_R (Lo), Ureal_M_9_10_36)
9444 or else Larger (Expr_Value_R (Hi), Ureal_9_10_36))
9445 then
9446 Actual_Size := 65;
9447 end if;
9449 -- Give error messages for first subtypes and not base types, as the
9450 -- bounds of base types are always maximum for their size, see below.
9452 if System_Max_Integer_Size < 128 and then Typ /= Btyp then
9454 -- See the 128-bit case below for the reason why we cannot test
9455 -- against the 2**(-63) .. 2**63 range. This quirk should have
9456 -- been kludged around as in the 128-bit case below, but it was
9457 -- not and we end up with a ludicrous range as a result???
9459 if Small < Ureal_2_M_80 then
9460 Error_Msg_Name_1 := Name_Small;
9461 Error_Msg_N
9462 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", Typ);
9464 elsif Small > Ureal_2_80 then
9465 Error_Msg_Name_1 := Name_Small;
9466 Error_Msg_N
9467 ("`&''%` too large, maximum allowed is 2.0'*'*80", Typ);
9468 end if;
9470 if Smaller (Expr_Value_R (Lo), Ureal_M_9_10_36) then
9471 Error_Msg_Name_1 := Name_First;
9472 Error_Msg_N
9473 ("`&''%` too small, minimum allowed is -9.0E+36", Typ);
9474 end if;
9476 if Larger (Expr_Value_R (Hi), Ureal_9_10_36) then
9477 Error_Msg_Name_1 := Name_Last;
9478 Error_Msg_N
9479 ("`&''%` too large, maximum allowed is 9.0E+36", Typ);
9480 end if;
9482 elsif System_Max_Integer_Size = 128 and then Typ /= Btyp then
9484 -- ACATS c35902d tests a delta equal to 2**(-(Max_Mantissa + 1))
9485 -- but we cannot really support anything smaller than Fine_Delta
9486 -- because of the way we implement I/O for fixed point types???
9488 if Small = Ureal_2_M_128 then
9489 null;
9491 elsif Small < Ureal_2_M_127 then
9492 Error_Msg_Name_1 := Name_Small;
9493 Error_Msg_N
9494 ("`&''%` too small, minimum allowed is 2.0'*'*(-127)", Typ);
9496 elsif Small > Ureal_2_127 then
9497 Error_Msg_Name_1 := Name_Small;
9498 Error_Msg_N
9499 ("`&''%` too large, maximum allowed is 2.0'*'*127", Typ);
9500 end if;
9502 if Actual_Size > 64
9503 and then (Norm_Num (Small) > Uint_2 ** 127
9504 or else Norm_Den (Small) > Uint_2 ** 127)
9505 and then Small /= Ureal_2_M_128
9506 then
9507 Error_Msg_Name_1 := Name_Small;
9508 Error_Msg_N
9509 ("`&''%` not the ratio of two 128-bit integers", Typ);
9510 end if;
9512 if Smaller (Expr_Value_R (Lo), Ureal_M_10_76) then
9513 Error_Msg_Name_1 := Name_First;
9514 Error_Msg_N
9515 ("`&''%` too small, minimum allowed is -1.0E+76", Typ);
9516 end if;
9518 if Larger (Expr_Value_R (Hi), Ureal_10_76) then
9519 Error_Msg_Name_1 := Name_Last;
9520 Error_Msg_N
9521 ("`&''%` too large, maximum allowed is 1.0E+76", Typ);
9522 end if;
9523 end if;
9525 -- For the decimal case, none of this fudging is required, since there
9526 -- are no end-point problems in the decimal case (the end-points are
9527 -- always included).
9529 else
9530 Actual_Size := Fsize (Loval, Hival);
9531 end if;
9533 -- At this stage, the actual size has been calculated and the proper
9534 -- required bounds are stored in the low and high bounds.
9536 if Actual_Size > System_Max_Integer_Size then
9537 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
9538 Error_Msg_Uint_2 := UI_From_Int (System_Max_Integer_Size);
9539 Error_Msg_N
9540 ("size required (^) for type& too large, maximum allowed is ^",
9541 Typ);
9542 Actual_Size := System_Max_Integer_Size;
9543 end if;
9545 -- Check size against explicit given size
9547 if Has_Size_Clause (Typ) then
9548 if Actual_Size > RM_Size (Typ) then
9549 Error_Msg_Uint_1 := RM_Size (Typ);
9550 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
9551 Error_Msg_NE
9552 ("size given (^) for type& too small, minimum allowed is ^",
9553 Size_Clause (Typ), Typ);
9555 else
9556 Actual_Size := UI_To_Int (Esize (Typ));
9557 end if;
9559 -- Increase size to next natural boundary if no size clause given
9561 else
9562 if Actual_Size <= 8 then
9563 Actual_Size := 8;
9564 elsif Actual_Size <= 16 then
9565 Actual_Size := 16;
9566 elsif Actual_Size <= 32 then
9567 Actual_Size := 32;
9568 elsif Actual_Size <= 64 then
9569 Actual_Size := 64;
9570 else
9571 Actual_Size := 128;
9572 end if;
9574 Set_Esize (Typ, UI_From_Int (Actual_Size));
9575 Adjust_Esize_For_Alignment (Typ);
9576 end if;
9578 -- If we have a base type, then expand the bounds so that they extend to
9579 -- the full width of the allocated size in bits, to avoid junk range
9580 -- checks on intermediate computations.
9582 if Typ = Btyp then
9583 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
9584 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
9585 end if;
9587 -- Final step is to reanalyze the bounds using the proper type
9588 -- and set the Corresponding_Integer_Value fields of the literals.
9590 Set_Etype (Lo, Empty);
9591 Set_Analyzed (Lo, False);
9592 Analyze (Lo);
9594 -- Resolve with universal fixed if the base type, and with the base
9595 -- type if we are freezing a subtype. Note we can't resolve the base
9596 -- type with itself, that would be a reference before definition.
9597 -- The resolution of the bounds of a subtype, if they are given by real
9598 -- literals, includes the setting of the Corresponding_Integer_Value,
9599 -- as for other literals of a fixed-point type.
9601 if Typ = Btyp then
9602 Resolve (Lo, Universal_Fixed);
9603 Set_Corresponding_Integer_Value
9604 (Lo, UR_To_Uint (Realval (Lo) / Small));
9605 else
9606 Resolve (Lo, Btyp);
9607 end if;
9609 -- Similar processing for high bound
9611 Set_Etype (Hi, Empty);
9612 Set_Analyzed (Hi, False);
9613 Analyze (Hi);
9615 if Typ = Btyp then
9616 Resolve (Hi, Universal_Fixed);
9617 Set_Corresponding_Integer_Value
9618 (Hi, UR_To_Uint (Realval (Hi) / Small));
9619 else
9620 Resolve (Hi, Btyp);
9621 end if;
9623 -- Set type of range to correspond to bounds
9625 Set_Etype (Rng, Etype (Lo));
9627 -- Set Esize to calculated size if not set already
9629 if not Known_Esize (Typ) then
9630 Set_Esize (Typ, UI_From_Int (Actual_Size));
9631 end if;
9633 -- Set RM_Size if not already set. If already set, check value
9635 declare
9636 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
9638 begin
9639 if Known_RM_Size (Typ) then
9640 if RM_Size (Typ) < Minsiz then
9641 Error_Msg_Uint_1 := RM_Size (Typ);
9642 Error_Msg_Uint_2 := Minsiz;
9643 Error_Msg_NE
9644 ("size given (^) for type& too small, minimum allowed is ^",
9645 Size_Clause (Typ), Typ);
9646 end if;
9648 else
9649 Set_RM_Size (Typ, Minsiz);
9650 end if;
9651 end;
9653 -- Check for shaving
9655 if Comes_From_Source (Typ) then
9657 -- In SPARK mode the given bounds must be strictly representable
9659 if SPARK_Mode = On then
9660 if Orig_Lo < Expr_Value_R (Lo) then
9661 Error_Msg_NE
9662 ("declared low bound of type & is outside type range",
9663 Lo, Typ);
9664 end if;
9666 if Orig_Hi > Expr_Value_R (Hi) then
9667 Error_Msg_NE
9668 ("declared high bound of type & is outside type range",
9669 Hi, Typ);
9670 end if;
9672 else
9673 if Orig_Lo < Expr_Value_R (Lo) then
9674 Error_Msg_N
9675 ("declared low bound of type & is outside type range??", Typ);
9676 Error_Msg_N
9677 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
9678 end if;
9680 if Orig_Hi > Expr_Value_R (Hi) then
9681 Error_Msg_N
9682 ("declared high bound of type & is outside type range??",
9683 Typ);
9684 Error_Msg_N
9685 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
9686 end if;
9687 end if;
9688 end if;
9689 end Freeze_Fixed_Point_Type;
9691 ------------------
9692 -- Freeze_Itype --
9693 ------------------
9695 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
9696 L : List_Id;
9698 begin
9699 Set_Has_Delayed_Freeze (T);
9700 L := Freeze_Entity (T, N);
9702 Insert_Actions (N, L);
9703 end Freeze_Itype;
9705 --------------------------
9706 -- Freeze_Static_Object --
9707 --------------------------
9709 procedure Freeze_Static_Object (E : Entity_Id) is
9711 Cannot_Be_Static : exception;
9712 -- Exception raised if the type of a static object cannot be made
9713 -- static. This happens if the type depends on non-global objects.
9715 procedure Ensure_Expression_Is_SA (N : Node_Id);
9716 -- Called to ensure that an expression used as part of a type definition
9717 -- is statically allocatable, which means that the expression type is
9718 -- statically allocatable, and the expression is either static, or a
9719 -- reference to a library level constant.
9721 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
9722 -- Called to mark a type as static, checking that it is possible
9723 -- to set the type as static. If it is not possible, then the
9724 -- exception Cannot_Be_Static is raised.
9726 -----------------------------
9727 -- Ensure_Expression_Is_SA --
9728 -----------------------------
9730 procedure Ensure_Expression_Is_SA (N : Node_Id) is
9731 Ent : Entity_Id;
9733 begin
9734 Ensure_Type_Is_SA (Etype (N));
9736 if Is_OK_Static_Expression (N) then
9737 return;
9739 elsif Nkind (N) = N_Identifier then
9740 Ent := Entity (N);
9742 if Present (Ent)
9743 and then Ekind (Ent) = E_Constant
9744 and then Is_Library_Level_Entity (Ent)
9745 then
9746 return;
9747 end if;
9748 end if;
9750 raise Cannot_Be_Static;
9751 end Ensure_Expression_Is_SA;
9753 -----------------------
9754 -- Ensure_Type_Is_SA --
9755 -----------------------
9757 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
9758 N : Node_Id;
9759 C : Entity_Id;
9761 begin
9762 -- If type is library level, we are all set
9764 if Is_Library_Level_Entity (Typ) then
9765 return;
9766 end if;
9768 -- We are also OK if the type already marked as statically allocated,
9769 -- which means we processed it before.
9771 if Is_Statically_Allocated (Typ) then
9772 return;
9773 end if;
9775 -- Mark type as statically allocated
9777 Set_Is_Statically_Allocated (Typ);
9779 -- Check that it is safe to statically allocate this type
9781 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
9782 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
9783 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
9785 elsif Is_Array_Type (Typ) then
9786 N := First_Index (Typ);
9787 while Present (N) loop
9788 Ensure_Type_Is_SA (Etype (N));
9789 Next_Index (N);
9790 end loop;
9792 Ensure_Type_Is_SA (Component_Type (Typ));
9794 elsif Is_Access_Type (Typ) then
9795 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
9797 declare
9798 F : Entity_Id;
9799 T : constant Entity_Id := Etype (Designated_Type (Typ));
9801 begin
9802 if T /= Standard_Void_Type then
9803 Ensure_Type_Is_SA (T);
9804 end if;
9806 F := First_Formal (Designated_Type (Typ));
9807 while Present (F) loop
9808 Ensure_Type_Is_SA (Etype (F));
9809 Next_Formal (F);
9810 end loop;
9811 end;
9813 else
9814 Ensure_Type_Is_SA (Designated_Type (Typ));
9815 end if;
9817 elsif Is_Record_Type (Typ) then
9818 C := First_Entity (Typ);
9819 while Present (C) loop
9820 if Ekind (C) = E_Discriminant
9821 or else Ekind (C) = E_Component
9822 then
9823 Ensure_Type_Is_SA (Etype (C));
9825 elsif Is_Type (C) then
9826 Ensure_Type_Is_SA (C);
9827 end if;
9829 Next_Entity (C);
9830 end loop;
9832 elsif Ekind (Typ) = E_Subprogram_Type then
9833 Ensure_Type_Is_SA (Etype (Typ));
9835 C := First_Formal (Typ);
9836 while Present (C) loop
9837 Ensure_Type_Is_SA (Etype (C));
9838 Next_Formal (C);
9839 end loop;
9841 else
9842 raise Cannot_Be_Static;
9843 end if;
9844 end Ensure_Type_Is_SA;
9846 -- Start of processing for Freeze_Static_Object
9848 begin
9849 Ensure_Type_Is_SA (Etype (E));
9851 exception
9852 when Cannot_Be_Static =>
9854 -- If the object that cannot be static is imported or exported, then
9855 -- issue an error message saying that this object cannot be imported
9856 -- or exported. If it has an address clause it is an overlay in the
9857 -- current partition and the static requirement is not relevant.
9858 -- Do not issue any error message when ignoring rep clauses.
9860 if Ignore_Rep_Clauses then
9861 null;
9863 elsif Is_Imported (E) then
9864 if No (Address_Clause (E)) then
9865 Error_Msg_N
9866 ("& cannot be imported (local type is not constant)", E);
9867 end if;
9869 -- Otherwise must be exported, something is wrong if compiler
9870 -- is marking something as statically allocated which cannot be).
9872 else pragma Assert (Is_Exported (E));
9873 Error_Msg_N
9874 ("& cannot be exported (local type is not constant)", E);
9875 end if;
9876 end Freeze_Static_Object;
9878 -----------------------
9879 -- Freeze_Subprogram --
9880 -----------------------
9882 procedure Freeze_Subprogram (E : Entity_Id) is
9884 procedure Set_Profile_Convention (Subp_Id : Entity_Id);
9885 -- Set the conventions of all anonymous access-to-subprogram formals and
9886 -- result subtype of subprogram Subp_Id to the convention of Subp_Id.
9888 ----------------------------
9889 -- Set_Profile_Convention --
9890 ----------------------------
9892 procedure Set_Profile_Convention (Subp_Id : Entity_Id) is
9893 Conv : constant Convention_Id := Convention (Subp_Id);
9895 procedure Set_Type_Convention (Typ : Entity_Id);
9896 -- Set the convention of anonymous access-to-subprogram type Typ and
9897 -- its designated type to Conv.
9899 -------------------------
9900 -- Set_Type_Convention --
9901 -------------------------
9903 procedure Set_Type_Convention (Typ : Entity_Id) is
9904 begin
9905 -- Set the convention on both the anonymous access-to-subprogram
9906 -- type and the subprogram type it points to because both types
9907 -- participate in conformance-related checks.
9909 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
9910 Set_Convention (Typ, Conv);
9911 Set_Convention (Designated_Type (Typ), Conv);
9912 end if;
9913 end Set_Type_Convention;
9915 -- Local variables
9917 Formal : Entity_Id;
9919 -- Start of processing for Set_Profile_Convention
9921 begin
9922 Formal := First_Formal (Subp_Id);
9923 while Present (Formal) loop
9924 Set_Type_Convention (Etype (Formal));
9925 Next_Formal (Formal);
9926 end loop;
9928 if Ekind (Subp_Id) = E_Function then
9929 Set_Type_Convention (Etype (Subp_Id));
9930 end if;
9931 end Set_Profile_Convention;
9933 -- Local variables
9935 F : Entity_Id;
9936 Retype : Entity_Id;
9938 -- Start of processing for Freeze_Subprogram
9940 begin
9941 -- Subprogram may not have an address clause unless it is imported
9943 if Present (Address_Clause (E)) then
9944 if not Is_Imported (E) then
9945 Error_Msg_N
9946 ("address clause can only be given for imported subprogram",
9947 Name (Address_Clause (E)));
9948 end if;
9949 end if;
9951 -- Reset the Pure indication on an imported subprogram unless an
9952 -- explicit Pure_Function pragma was present or the subprogram is an
9953 -- intrinsic. We do this because otherwise it is an insidious error
9954 -- to call a non-pure function from pure unit and have calls
9955 -- mysteriously optimized away. What happens here is that the Import
9956 -- can bypass the normal check to ensure that pure units call only pure
9957 -- subprograms.
9959 -- The reason for the intrinsic exception is that in general, intrinsic
9960 -- functions (such as shifts) are pure anyway. The only exceptions are
9961 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
9962 -- in any case, so no problem arises.
9964 if Is_Imported (E)
9965 and then Is_Pure (E)
9966 and then not Has_Pragma_Pure_Function (E)
9967 and then not Is_Intrinsic_Subprogram (E)
9968 then
9969 Set_Is_Pure (E, False);
9970 end if;
9972 -- For C++ constructors check that their external name has been given
9973 -- (either in pragma CPP_Constructor or in a pragma import).
9975 if Is_Constructor (E)
9976 and then Convention (E) = Convention_CPP
9977 and then
9978 (No (Interface_Name (E))
9979 or else String_Equal
9980 (L => Strval (Interface_Name (E)),
9981 R => Strval (Get_Default_External_Name (E))))
9982 then
9983 Error_Msg_N
9984 ("'C++ constructor must have external name or link name", E);
9985 end if;
9987 -- We also reset the Pure indication on a subprogram with an Address
9988 -- parameter, because the parameter may be used as a pointer and the
9989 -- referenced data may change even if the address value does not.
9991 -- Note that if the programmer gave an explicit Pure_Function pragma,
9992 -- then we believe the programmer, and leave the subprogram Pure. We
9993 -- also suppress this check on run-time files.
9995 if Is_Pure (E)
9996 and then Is_Subprogram (E)
9997 and then not Has_Pragma_Pure_Function (E)
9998 and then not Is_Internal_Unit (Current_Sem_Unit)
9999 then
10000 Check_Function_With_Address_Parameter (E);
10001 end if;
10003 -- Ensure that all anonymous access-to-subprogram types inherit the
10004 -- convention of their related subprogram (RM 6.3.1(13.1/5)). This is
10005 -- not done for a defaulted convention Ada because those types also
10006 -- default to Ada. Convention Protected must not be propagated when
10007 -- the subprogram is an entry because this would be illegal. The only
10008 -- way to force convention Protected on these kinds of types is to
10009 -- include keyword "protected" in the access definition. Conventions
10010 -- Entry and Intrinsic are also not propagated (specified by AI12-0207).
10012 if Convention (E) /= Convention_Ada
10013 and then Convention (E) /= Convention_Protected
10014 and then Convention (E) /= Convention_Entry
10015 and then Convention (E) /= Convention_Intrinsic
10016 then
10017 Set_Profile_Convention (E);
10018 end if;
10020 -- For non-foreign convention subprograms, this is where we create
10021 -- the extra formals (for accessibility level and constrained bit
10022 -- information). We delay this till the freeze point precisely so
10023 -- that we know the convention.
10025 if not Has_Foreign_Convention (E) then
10027 -- Extra formals of dispatching operations are added later by
10028 -- Expand_Freeze_Record_Type, which also adds extra formals to
10029 -- internal entities built to handle interface types.
10031 if not Is_Dispatching_Operation (E) then
10032 Create_Extra_Formals (E);
10034 pragma Assert
10035 ((Ekind (E) = E_Subprogram_Type
10036 and then Extra_Formals_OK (E))
10037 or else
10038 (Is_Subprogram (E)
10039 and then Extra_Formals_OK (E)
10040 and then
10041 (No (Overridden_Operation (E))
10042 or else Extra_Formals_Match_OK (E,
10043 Ultimate_Alias (Overridden_Operation (E))))));
10044 end if;
10046 Set_Mechanisms (E);
10048 -- If this is convention Ada and a Valued_Procedure, that's odd
10050 if Ekind (E) = E_Procedure
10051 and then Is_Valued_Procedure (E)
10052 and then Convention (E) = Convention_Ada
10053 and then Warn_On_Export_Import
10054 then
10055 Error_Msg_N
10056 ("??Valued_Procedure has no effect for convention Ada", E);
10057 Set_Is_Valued_Procedure (E, False);
10058 end if;
10060 -- Case of foreign convention
10062 else
10063 Set_Mechanisms (E);
10065 -- For foreign conventions, warn about return of unconstrained array
10067 if Ekind (E) = E_Function then
10068 Retype := Underlying_Type (Etype (E));
10070 -- If no return type, probably some other error, e.g. a
10071 -- missing full declaration, so ignore.
10073 if No (Retype) then
10074 null;
10076 -- If the return type is generic, we have emitted a warning
10077 -- earlier on, and there is nothing else to check here. Specific
10078 -- instantiations may lead to erroneous behavior.
10080 elsif Is_Generic_Type (Etype (E)) then
10081 null;
10083 -- Display warning if returning unconstrained array
10085 elsif Is_Array_Type (Retype)
10086 and then not Is_Constrained (Retype)
10088 -- Check appropriate warning is enabled (should we check for
10089 -- Warnings (Off) on specific entities here, probably so???)
10091 and then Warn_On_Export_Import
10092 then
10093 Error_Msg_N
10094 ("?x?foreign convention function& should not return " &
10095 "unconstrained array", E);
10096 return;
10097 end if;
10098 end if;
10100 -- If any of the formals for an exported foreign convention
10101 -- subprogram have defaults, then emit an appropriate warning since
10102 -- this is odd (default cannot be used from non-Ada code)
10104 if Is_Exported (E) then
10105 F := First_Formal (E);
10106 while Present (F) loop
10107 if Warn_On_Export_Import
10108 and then Present (Default_Value (F))
10109 then
10110 Error_Msg_N
10111 ("?x?parameter cannot be defaulted in non-Ada call",
10112 Default_Value (F));
10113 end if;
10115 Next_Formal (F);
10116 end loop;
10117 end if;
10118 end if;
10120 -- Pragma Inline_Always is disallowed for dispatching subprograms
10121 -- because the address of such subprograms is saved in the dispatch
10122 -- table to support dispatching calls, and dispatching calls cannot
10123 -- be inlined. This is consistent with the restriction against using
10124 -- 'Access or 'Address on an Inline_Always subprogram.
10126 if Is_Dispatching_Operation (E)
10127 and then Has_Pragma_Inline_Always (E)
10128 then
10129 Error_Msg_N
10130 ("pragma Inline_Always not allowed for dispatching subprograms", E);
10131 end if;
10133 -- Because of the implicit representation of inherited predefined
10134 -- operators in the front-end, the overriding status of the operation
10135 -- may be affected when a full view of a type is analyzed, and this is
10136 -- not captured by the analysis of the corresponding type declaration.
10137 -- Therefore the correctness of a not-overriding indicator must be
10138 -- rechecked when the subprogram is frozen.
10140 if Nkind (E) = N_Defining_Operator_Symbol
10141 and then not Error_Posted (Parent (E))
10142 then
10143 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
10144 end if;
10146 Retype := Get_Fullest_View (Etype (E));
10148 if Transform_Function_Array
10149 and then Nkind (Parent (E)) = N_Function_Specification
10150 and then Is_Array_Type (Retype)
10151 and then Is_Constrained (Retype)
10152 and then not Is_Unchecked_Conversion_Instance (E)
10153 and then not Rewritten_For_C (E)
10154 then
10155 Build_Procedure_Form (Unit_Declaration_Node (E));
10156 end if;
10157 end Freeze_Subprogram;
10159 ----------------------
10160 -- Is_Fully_Defined --
10161 ----------------------
10163 function Is_Fully_Defined (T : Entity_Id) return Boolean is
10164 begin
10165 if Ekind (T) = E_Class_Wide_Type then
10166 return Is_Fully_Defined (Etype (T));
10168 elsif Is_Array_Type (T) then
10169 return Is_Fully_Defined (Component_Type (T));
10171 elsif Is_Record_Type (T)
10172 and not Is_Private_Type (T)
10173 then
10174 -- Verify that the record type has no components with private types
10175 -- without completion.
10177 declare
10178 Comp : Entity_Id;
10180 begin
10181 Comp := First_Component (T);
10182 while Present (Comp) loop
10183 if not Is_Fully_Defined (Etype (Comp)) then
10184 return False;
10185 end if;
10187 Next_Component (Comp);
10188 end loop;
10189 return True;
10190 end;
10192 -- For the designated type of an access to subprogram, all types in
10193 -- the profile must be fully defined.
10195 elsif Ekind (T) = E_Subprogram_Type then
10196 declare
10197 F : Entity_Id;
10199 begin
10200 F := First_Formal (T);
10201 while Present (F) loop
10202 if not Is_Fully_Defined (Etype (F)) then
10203 return False;
10204 end if;
10206 Next_Formal (F);
10207 end loop;
10209 return Is_Fully_Defined (Etype (T));
10210 end;
10212 else
10213 return not Is_Private_Type (T)
10214 or else Present (Full_View (Base_Type (T)));
10215 end if;
10216 end Is_Fully_Defined;
10218 ---------------------------------
10219 -- Process_Default_Expressions --
10220 ---------------------------------
10222 procedure Process_Default_Expressions
10223 (E : Entity_Id;
10224 After : in out Node_Id)
10226 Loc : constant Source_Ptr := Sloc (E);
10227 Dbody : Node_Id;
10228 Formal : Node_Id;
10229 Dcopy : Node_Id;
10230 Dnam : Entity_Id;
10232 begin
10233 Set_Default_Expressions_Processed (E);
10235 -- A subprogram instance and its associated anonymous subprogram share
10236 -- their signature. The default expression functions are defined in the
10237 -- wrapper packages for the anonymous subprogram, and should not be
10238 -- generated again for the instance.
10240 if Is_Generic_Instance (E)
10241 and then Present (Alias (E))
10242 and then Default_Expressions_Processed (Alias (E))
10243 then
10244 return;
10245 end if;
10247 Formal := First_Formal (E);
10248 while Present (Formal) loop
10249 if Present (Default_Value (Formal)) then
10251 -- We work with a copy of the default expression because we
10252 -- do not want to disturb the original, since this would mess
10253 -- up the conformance checking.
10255 Dcopy := New_Copy_Tree (Default_Value (Formal));
10257 -- The analysis of the expression may generate insert actions,
10258 -- which of course must not be executed. We wrap those actions
10259 -- in a procedure that is not called, and later on eliminated.
10260 -- The following cases have no side effects, and are analyzed
10261 -- directly.
10263 if Nkind (Dcopy) = N_Identifier
10264 or else Nkind (Dcopy) in N_Expanded_Name
10265 | N_Integer_Literal
10266 | N_Character_Literal
10267 | N_String_Literal
10268 | N_Real_Literal
10269 or else (Nkind (Dcopy) = N_Attribute_Reference
10270 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
10271 or else Known_Null (Dcopy)
10272 then
10273 -- If there is no default function, we must still do a full
10274 -- analyze call on the default value, to ensure that all error
10275 -- checks are performed, e.g. those associated with static
10276 -- evaluation. Note: this branch will always be taken if the
10277 -- analyzer is turned off (but we still need the error checks).
10279 -- Note: the setting of parent here is to meet the requirement
10280 -- that we can only analyze the expression while attached to
10281 -- the tree. Really the requirement is that the parent chain
10282 -- be set, we don't actually need to be in the tree.
10284 Set_Parent (Dcopy, Declaration_Node (Formal));
10285 Analyze (Dcopy);
10287 -- Default expressions are resolved with their own type if the
10288 -- context is generic, to avoid anomalies with private types.
10290 if Ekind (Scope (E)) = E_Generic_Package then
10291 Resolve (Dcopy);
10292 else
10293 Resolve (Dcopy, Etype (Formal));
10294 end if;
10296 -- If that resolved expression will raise constraint error,
10297 -- then flag the default value as raising constraint error.
10298 -- This allows a proper error message on the calls.
10300 if Raises_Constraint_Error (Dcopy) then
10301 Set_Raises_Constraint_Error (Default_Value (Formal));
10302 end if;
10304 -- If the default is a parameterless call, we use the name of
10305 -- the called function directly, and there is no body to build.
10307 elsif Nkind (Dcopy) = N_Function_Call
10308 and then No (Parameter_Associations (Dcopy))
10309 then
10310 null;
10312 -- Else construct and analyze the body of a wrapper procedure
10313 -- that contains an object declaration to hold the expression.
10314 -- Given that this is done only to complete the analysis, it is
10315 -- simpler to build a procedure than a function which might
10316 -- involve secondary stack expansion.
10318 else
10319 Dnam := Make_Temporary (Loc, 'D');
10321 Dbody :=
10322 Make_Subprogram_Body (Loc,
10323 Specification =>
10324 Make_Procedure_Specification (Loc,
10325 Defining_Unit_Name => Dnam),
10327 Declarations => New_List (
10328 Make_Object_Declaration (Loc,
10329 Defining_Identifier => Make_Temporary (Loc, 'T'),
10330 Object_Definition =>
10331 New_Occurrence_Of (Etype (Formal), Loc),
10332 Expression => New_Copy_Tree (Dcopy))),
10334 Handled_Statement_Sequence =>
10335 Make_Handled_Sequence_Of_Statements (Loc,
10336 Statements => Empty_List));
10338 Set_Scope (Dnam, Scope (E));
10339 Set_Assignment_OK (First (Declarations (Dbody)));
10340 Set_Is_Eliminated (Dnam);
10341 Insert_After (After, Dbody);
10342 Analyze (Dbody);
10343 After := Dbody;
10344 end if;
10345 end if;
10347 Next_Formal (Formal);
10348 end loop;
10349 end Process_Default_Expressions;
10351 ----------------------------------------
10352 -- Set_Component_Alignment_If_Not_Set --
10353 ----------------------------------------
10355 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
10356 begin
10357 -- Ignore if not base type, subtypes don't need anything
10359 if Typ /= Base_Type (Typ) then
10360 return;
10361 end if;
10363 -- Do not override existing representation
10365 if Is_Packed (Typ) then
10366 return;
10368 elsif Has_Specified_Layout (Typ) then
10369 return;
10371 elsif Component_Alignment (Typ) /= Calign_Default then
10372 return;
10374 else
10375 Set_Component_Alignment
10376 (Typ, Scope_Stack.Table
10377 (Scope_Stack.Last).Component_Alignment_Default);
10378 end if;
10379 end Set_Component_Alignment_If_Not_Set;
10381 --------------------------
10382 -- Set_SSO_From_Default --
10383 --------------------------
10385 procedure Set_SSO_From_Default (T : Entity_Id) is
10386 Reversed : Boolean;
10388 begin
10389 -- Set default SSO for an array or record base type, except in case of
10390 -- a type extension (which always inherits the SSO of its parent type).
10392 if Is_Base_Type (T)
10393 and then (Is_Array_Type (T)
10394 or else (Is_Record_Type (T)
10395 and then not (Is_Tagged_Type (T)
10396 and then Is_Derived_Type (T))))
10397 then
10398 Reversed :=
10399 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
10400 or else
10401 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
10403 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
10405 -- For a record type, if bit order is specified explicitly,
10406 -- then do not set SSO from default if not consistent. Note that
10407 -- we do not want to look at a Bit_Order attribute definition
10408 -- for a parent: if we were to inherit Bit_Order, then both
10409 -- SSO_Set_*_By_Default flags would have been cleared already
10410 -- (by Inherit_Aspects_At_Freeze_Point).
10412 and then not
10413 (Is_Record_Type (T)
10414 and then
10415 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
10416 and then Reverse_Bit_Order (T) /= Reversed)
10417 then
10418 -- If flags cause reverse storage order, then set the result. Note
10419 -- that we would have ignored the pragma setting the non default
10420 -- storage order in any case, hence the assertion at this point.
10422 pragma Assert
10423 (not Reversed or else Support_Nondefault_SSO_On_Target);
10425 Set_Reverse_Storage_Order (T, Reversed);
10427 -- For a record type, also set reversed bit order. Note: if a bit
10428 -- order has been specified explicitly, then this is a no-op.
10430 if Is_Record_Type (T) then
10431 Set_Reverse_Bit_Order (T, Reversed);
10432 end if;
10433 end if;
10434 end if;
10435 end Set_SSO_From_Default;
10437 ------------------
10438 -- Undelay_Type --
10439 ------------------
10441 procedure Undelay_Type (T : Entity_Id) is
10442 begin
10443 Set_Has_Delayed_Freeze (T, False);
10444 Set_Freeze_Node (T, Empty);
10446 -- Since we don't want T to have a Freeze_Node, we don't want its
10447 -- Full_View or Corresponding_Record_Type to have one either.
10449 -- ??? Fundamentally, this whole handling is unpleasant. What we really
10450 -- want is to be sure that for an Itype that's part of record R and is a
10451 -- subtype of type T, that it's frozen after the later of the freeze
10452 -- points of R and T. We have no way of doing that directly, so what we
10453 -- do is force most such Itypes to be frozen as part of freezing R via
10454 -- this procedure and only delay the ones that need to be delayed
10455 -- (mostly the designated types of access types that are defined as part
10456 -- of the record).
10458 if Is_Private_Type (T)
10459 and then Present (Full_View (T))
10460 and then Is_Itype (Full_View (T))
10461 and then Is_Record_Type (Scope (Full_View (T)))
10462 then
10463 Undelay_Type (Full_View (T));
10464 end if;
10466 if Is_Concurrent_Type (T)
10467 and then Present (Corresponding_Record_Type (T))
10468 and then Is_Itype (Corresponding_Record_Type (T))
10469 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
10470 then
10471 Undelay_Type (Corresponding_Record_Type (T));
10472 end if;
10473 end Undelay_Type;
10475 ------------------
10476 -- Warn_Overlay --
10477 ------------------
10479 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id) is
10480 Ent : constant Entity_Id := Entity (Nam);
10481 -- The object to which the address clause applies
10483 Init : Node_Id;
10484 Old : Entity_Id := Empty;
10485 Decl : Node_Id;
10487 begin
10488 -- No warning if address clause overlay warnings are off
10490 if not Address_Clause_Overlay_Warnings then
10491 return;
10492 end if;
10494 -- No warning if there is an explicit initialization
10496 Init := Original_Node (Expression (Declaration_Node (Ent)));
10498 if Present (Init) and then Comes_From_Source (Init) then
10499 return;
10500 end if;
10502 -- We only give the warning for non-imported entities of a type for
10503 -- which a non-null base init proc is defined, or for objects of access
10504 -- types with implicit null initialization, or when Normalize_Scalars
10505 -- applies and the type is scalar or a string type (the latter being
10506 -- tested for because predefined String types are initialized by inline
10507 -- code rather than by an init_proc). Note that we do not give the
10508 -- warning for Initialize_Scalars, since we suppressed initialization
10509 -- in this case. Also, do not warn if Suppress_Initialization is set
10510 -- either on the type, or on the object via pragma or aspect.
10512 if Present (Expr)
10513 and then not Is_Imported (Ent)
10514 and then not Initialization_Suppressed (Typ)
10515 and then not (Ekind (Ent) = E_Variable
10516 and then Initialization_Suppressed (Ent))
10517 and then (Has_Non_Null_Base_Init_Proc (Typ)
10518 or else Is_Access_Type (Typ)
10519 or else (Normalize_Scalars
10520 and then (Is_Scalar_Type (Typ)
10521 or else Is_String_Type (Typ))))
10522 then
10523 if Nkind (Expr) = N_Attribute_Reference
10524 and then Is_Entity_Name (Prefix (Expr))
10525 then
10526 Old := Entity (Prefix (Expr));
10528 elsif Is_Entity_Name (Expr)
10529 and then Ekind (Entity (Expr)) = E_Constant
10530 then
10531 Decl := Declaration_Node (Entity (Expr));
10533 if Nkind (Decl) = N_Object_Declaration
10534 and then Present (Expression (Decl))
10535 and then Nkind (Expression (Decl)) = N_Attribute_Reference
10536 and then Is_Entity_Name (Prefix (Expression (Decl)))
10537 then
10538 Old := Entity (Prefix (Expression (Decl)));
10540 elsif Nkind (Expr) = N_Function_Call then
10541 return;
10542 end if;
10544 -- A function call (most likely to To_Address) is probably not an
10545 -- overlay, so skip warning. Ditto if the function call was inlined
10546 -- and transformed into an entity.
10548 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
10549 return;
10550 end if;
10552 -- If a pragma Import follows, we assume that it is for the current
10553 -- target of the address clause, and skip the warning. There may be
10554 -- a source pragma or an aspect that specifies import and generates
10555 -- the corresponding pragma. These will indicate that the entity is
10556 -- imported and that is checked above so that the spurious warning
10557 -- (generated when the entity is frozen) will be suppressed. The
10558 -- pragma may be attached to the aspect, so it is not yet a list
10559 -- member.
10561 if Is_List_Member (Parent (Expr)) then
10562 Decl := Next (Parent (Expr));
10564 if Present (Decl)
10565 and then Nkind (Decl) = N_Pragma
10566 and then Pragma_Name (Decl) = Name_Import
10567 then
10568 return;
10569 end if;
10570 end if;
10572 -- Otherwise give warning message
10574 if Present (Old) then
10575 Error_Msg_Node_2 := Old;
10576 Error_Msg_N
10577 ("default initialization of & may modify &?o?",
10578 Nam);
10579 else
10580 Error_Msg_N
10581 ("default initialization of & may modify overlaid storage?o?",
10582 Nam);
10583 end if;
10585 -- Add friendly warning if initialization comes from a packed array
10586 -- component.
10588 if Is_Record_Type (Typ) then
10589 declare
10590 Comp : Entity_Id;
10592 begin
10593 Comp := First_Component (Typ);
10594 while Present (Comp) loop
10595 if Nkind (Parent (Comp)) = N_Component_Declaration
10596 and then Present (Expression (Parent (Comp)))
10597 then
10598 exit;
10599 elsif Is_Array_Type (Etype (Comp))
10600 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
10601 then
10602 Error_Msg_NE
10603 ("\packed array component& " &
10604 "will be initialized to zero??",
10605 Nam, Comp);
10606 exit;
10607 else
10608 Next_Component (Comp);
10609 end if;
10610 end loop;
10611 end;
10612 end if;
10614 Error_Msg_N
10615 ("\use pragma Import for & to " &
10616 "suppress initialization (RM B.1(24))??",
10617 Nam);
10618 end if;
10619 end Warn_Overlay;
10621 end Freeze;