Disable tests for strdup/strndup on __hpux__
[official-gcc.git] / gcc / ada / freeze.adb
blob468d6ee6dbee4a8f14657e5e803986cb732faa8b
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 Local_Restrict;
46 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Util; use Sem_Util;
67 with Sinfo; use Sinfo;
68 with Sinfo.Nodes; use Sinfo.Nodes;
69 with Sinfo.Utils; use Sinfo.Utils;
70 with Snames; use Snames;
71 with Stand; use Stand;
72 with Stringt; use Stringt;
73 with Strub; use Strub;
74 with Targparm; use Targparm;
75 with Tbuild; use Tbuild;
76 with Ttypes; use Ttypes;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
81 package body Freeze is
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
87 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
88 -- Typ is a type that is being frozen. If no size clause is given,
89 -- but a default Esize has been computed, then this default Esize is
90 -- adjusted up if necessary to be consistent with a given alignment,
91 -- but never to a value greater than System_Max_Integer_Size. This is
92 -- used for all discrete types and for fixed-point types.
94 procedure Build_And_Analyze_Renamed_Body
95 (Decl : Node_Id;
96 New_S : Entity_Id;
97 After : in out Node_Id);
98 -- Build body for a renaming declaration, insert in tree and analyze
100 procedure Check_Address_Clause (E : Entity_Id);
101 -- Apply legality checks to address clauses for object declarations,
102 -- at the point the object is frozen. Also ensure any initialization is
103 -- performed only after the object has been frozen.
105 procedure Check_Component_Storage_Order
106 (Encl_Type : Entity_Id;
107 Comp : Entity_Id;
108 ADC : Node_Id;
109 Comp_ADC_Present : out Boolean);
110 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
111 -- clause, verify that the component type has an explicit and compatible
112 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
113 -- entity of the component under consideration. For an Encl_Type that
114 -- does not have a Scalar_Storage_Order attribute definition clause,
115 -- verify that the component also does not have such a clause.
116 -- ADC is the attribute definition clause if present (or Empty). On return,
117 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
118 -- attribute definition clause.
120 procedure Check_Debug_Info_Needed (T : Entity_Id);
121 -- As each entity is frozen, this routine is called to deal with the
122 -- setting of Debug_Info_Needed for the entity. This flag is set if
123 -- the entity comes from source, or if we are in Debug_Generated_Code
124 -- mode or if the -gnatdV debug flag is set. However, it never sets
125 -- the flag if Debug_Info_Off is set. This procedure also ensures that
126 -- subsidiary entities have the flag set as required.
128 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
129 -- When an expression function is frozen by a use of it, the expression
130 -- itself is frozen. Check that the expression does not include references
131 -- to deferred constants without completion. We report this at the freeze
132 -- point of the function, to provide a better error message.
134 -- In most cases the expression itself is frozen by the time the function
135 -- itself is frozen, because the formals will be frozen by then. However,
136 -- Attribute references to outer types are freeze points for those types;
137 -- this routine generates the required freeze nodes for them.
139 procedure Check_Strict_Alignment (E : Entity_Id);
140 -- E is a base type. If E is tagged or has a component that is aliased
141 -- or tagged or contains something this is aliased or tagged, set
142 -- Strict_Alignment.
144 procedure Check_Unsigned_Type (E : Entity_Id);
145 pragma Inline (Check_Unsigned_Type);
146 -- If E is a fixed-point or discrete type, then all the necessary work
147 -- to freeze it is completed except for possible setting of the flag
148 -- Is_Unsigned_Type, which is done by this procedure. The call has no
149 -- effect if the entity E is not a discrete or fixed-point type.
151 procedure Freeze_And_Append
152 (Ent : Entity_Id;
153 N : Node_Id;
154 Result : in out List_Id);
155 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
156 -- nodes to Result, modifying Result from No_List if necessary. N has
157 -- the same usage as in Freeze_Entity.
159 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
160 -- Freeze enumeration type. The Esize field is set as processing
161 -- proceeds (i.e. set by default when the type is declared and then
162 -- adjusted by rep clauses). What this procedure does is to make sure
163 -- that if a foreign convention is specified, and no specific size
164 -- is given, then the size must be at least Integer'Size.
166 procedure Freeze_Static_Object (E : Entity_Id);
167 -- If an object is frozen which has Is_Statically_Allocated set, then
168 -- all referenced types must also be marked with this flag. This routine
169 -- is in charge of meeting this requirement for the object entity E.
171 procedure Freeze_Subprogram (E : Entity_Id);
172 -- Perform freezing actions for a subprogram (create extra formals,
173 -- and set proper default mechanism values). Note that this routine
174 -- is not called for internal subprograms, for which neither of these
175 -- actions is needed (or desirable, we do not want for example to have
176 -- these extra formals present in initialization procedures, where they
177 -- would serve no purpose). In this call E is either a subprogram or
178 -- a subprogram type (i.e. an access to a subprogram).
180 function Is_Fully_Defined (T : Entity_Id) return Boolean;
181 -- True if T is not private and has no private components, or has a full
182 -- view. Used to determine whether the designated type of an access type
183 -- should be frozen when the access type is frozen. This is done when an
184 -- allocator is frozen, or an expression that may involve attributes of
185 -- the designated type. Otherwise freezing the access type does not freeze
186 -- the designated type.
188 function Should_Freeze_Type
189 (Typ : Entity_Id; E : Entity_Id; N : Node_Id) return Boolean;
190 -- If Typ is in the current scope, then return True.
191 -- N is a node whose source location corresponds to the freeze point.
192 -- ??? Expression functions (represented by E) shouldn't freeze types in
193 -- general, but our current expansion and freezing model requires an early
194 -- freezing when the dispatch table is needed or when building an aggregate
195 -- with a subtype of Typ, so return True also in this case.
196 -- Note that expression function completions do freeze and are
197 -- handled in Sem_Ch6.Analyze_Expression_Function.
199 ------------------------
200 -- Should_Freeze_Type --
201 ------------------------
203 function Should_Freeze_Type
204 (Typ : Entity_Id; E : Entity_Id; N : Node_Id) return Boolean
206 function Is_Dispatching_Call_Or_Aggregate
207 (N : Node_Id) return Traverse_Result;
208 -- Return Abandon if N is a dispatching call to a subprogram
209 -- declared in the same scope as Typ or an aggregate whose type
210 -- is Typ.
212 --------------------------------------
213 -- Is_Dispatching_Call_Or_Aggregate --
214 --------------------------------------
216 function Is_Dispatching_Call_Or_Aggregate
217 (N : Node_Id) return Traverse_Result is
218 begin
219 if Nkind (N) = N_Function_Call
220 and then Present (Controlling_Argument (N))
221 and then Scope (Entity (Original_Node (Name (N))))
222 = Scope (Typ)
223 then
224 return Abandon;
225 elsif Nkind (N) = N_Aggregate
226 and then Base_Type (Etype (N)) = Base_Type (Typ)
227 then
228 return Abandon;
229 else
230 return OK;
231 end if;
232 end Is_Dispatching_Call_Or_Aggregate;
234 -------------------------
235 -- Need_Dispatch_Table --
236 -------------------------
238 function Need_Dispatch_Table is new
239 Traverse_Func (Is_Dispatching_Call_Or_Aggregate);
240 -- Return Abandon if the input expression requires access to
241 -- Typ's dispatch table.
243 Decl : constant Node_Id :=
244 (if No (E) then E else Original_Node (Unit_Declaration_Node (E)));
246 -- Start of processing for Should_Freeze_Type
248 begin
249 return Within_Scope (Typ, Current_Scope)
250 or else (Nkind (N) = N_Subprogram_Renaming_Declaration
251 and then Present (Corresponding_Formal_Spec (N)))
252 or else (Present (Decl)
253 and then Nkind (Decl) = N_Expression_Function
254 and then Need_Dispatch_Table (Expression (Decl)) = Abandon);
255 end Should_Freeze_Type;
257 procedure Process_Default_Expressions
258 (E : Entity_Id;
259 After : in out Node_Id);
260 -- This procedure is called for each subprogram to complete processing of
261 -- default expressions at the point where all types are known to be frozen.
262 -- The expressions must be analyzed in full, to make sure that all error
263 -- processing is done (they have only been preanalyzed). If the expression
264 -- is not an entity or literal, its analysis may generate code which must
265 -- not be executed. In that case we build a function body to hold that
266 -- code. This wrapper function serves no other purpose (it used to be
267 -- called to evaluate the default, but now the default is inlined at each
268 -- point of call).
270 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
271 -- Typ is a record or array type that is being frozen. This routine sets
272 -- the default component alignment from the scope stack values if the
273 -- alignment is otherwise not specified.
275 procedure Set_SSO_From_Default (T : Entity_Id);
276 -- T is a record or array type that is being frozen. If it is a base type,
277 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
278 -- will be set appropriately. Note that an explicit occurrence of aspect
279 -- Scalar_Storage_Order or an explicit setting of this aspect with an
280 -- attribute definition clause occurs, then these two flags are reset in
281 -- any case, so call will have no effect.
283 procedure Undelay_Type (T : Entity_Id);
284 -- T is a type of a component that we know to be an Itype. We don't want
285 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
286 -- Full_View or Corresponding_Record_Type.
288 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
289 -- Expr is the expression for an address clause for the entity denoted by
290 -- Nam whose type is Typ. If Typ has a default initialization, and there is
291 -- no explicit initialization in the source declaration, check whether the
292 -- address clause might cause overlaying of an entity, and emit a warning
293 -- on the side effect that the initialization will cause.
295 -------------------------------
296 -- Adjust_Esize_For_Alignment --
297 -------------------------------
299 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
300 Align : Uint;
302 begin
303 if Known_Esize (Typ) and then Known_Alignment (Typ) then
304 Align := Alignment_In_Bits (Typ);
306 if Align > Esize (Typ) and then Align <= System_Max_Integer_Size then
307 Set_Esize (Typ, Align);
308 end if;
309 end if;
310 end Adjust_Esize_For_Alignment;
312 ------------------------------------
313 -- Build_And_Analyze_Renamed_Body --
314 ------------------------------------
316 procedure Build_And_Analyze_Renamed_Body
317 (Decl : Node_Id;
318 New_S : Entity_Id;
319 After : in out Node_Id)
321 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
322 Ent : constant Entity_Id := Defining_Entity (Decl);
323 Body_Node : Node_Id;
324 Renamed_Subp : Entity_Id;
326 begin
327 -- If the renamed subprogram is intrinsic, there is no need for a
328 -- wrapper body: we set the alias that will be called and expanded which
329 -- completes the declaration. This transformation is only legal if the
330 -- renamed entity has already been elaborated.
332 -- Note that it is legal for a renaming_as_body to rename an intrinsic
333 -- subprogram, as long as the renaming occurs before the new entity
334 -- is frozen (RM 8.5.4 (5)).
336 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
337 and then Is_Entity_Name (Name (Body_Decl))
338 then
339 Renamed_Subp := Entity (Name (Body_Decl));
340 else
341 Renamed_Subp := Empty;
342 end if;
344 if Present (Renamed_Subp)
345 and then Is_Intrinsic_Subprogram (Renamed_Subp)
346 and then
347 (not In_Same_Source_Unit (Renamed_Subp, Ent)
348 or else Sloc (Renamed_Subp) < Sloc (Ent))
350 -- We can make the renaming entity intrinsic if the renamed function
351 -- has an interface name, or if it is one of the shift/rotate
352 -- operations known to the compiler.
354 and then
355 (Present (Interface_Name (Renamed_Subp))
356 or else Chars (Renamed_Subp) in Name_Rotate_Left
357 | Name_Rotate_Right
358 | Name_Shift_Left
359 | Name_Shift_Right
360 | Name_Shift_Right_Arithmetic)
361 then
362 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
364 if Present (Alias (Renamed_Subp)) then
365 Set_Alias (Ent, Alias (Renamed_Subp));
366 else
367 Set_Alias (Ent, Renamed_Subp);
368 end if;
370 Set_Is_Intrinsic_Subprogram (Ent);
371 Set_Has_Completion (Ent);
373 else
374 Body_Node := Build_Renamed_Body (Decl, New_S);
375 Insert_After (After, Body_Node);
376 Mark_Rewrite_Insertion (Body_Node);
377 Analyze (Body_Node);
378 After := Body_Node;
379 end if;
380 end Build_And_Analyze_Renamed_Body;
382 ------------------------
383 -- Build_Renamed_Body --
384 ------------------------
386 function Build_Renamed_Body
387 (Decl : Node_Id;
388 New_S : Entity_Id) return Node_Id
390 Loc : constant Source_Ptr := Sloc (New_S);
391 -- We use for the source location of the renamed body, the location of
392 -- the spec entity. It might seem more natural to use the location of
393 -- the renaming declaration itself, but that would be wrong, since then
394 -- the body we create would look as though it was created far too late,
395 -- and this could cause problems with elaboration order analysis,
396 -- particularly in connection with instantiations.
398 N : constant Node_Id := Unit_Declaration_Node (New_S);
399 Nam : constant Node_Id := Name (N);
400 Old_S : Entity_Id;
401 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
402 Actuals : List_Id;
403 Call_Node : Node_Id;
404 Call_Name : Node_Id;
405 Body_Node : Node_Id;
406 Formal : Entity_Id;
407 O_Formal : Entity_Id;
408 Param_Spec : Node_Id;
410 Pref : Node_Id := Empty;
411 -- If the renamed entity is a primitive operation given in prefix form,
412 -- the prefix is the target object and it has to be added as the first
413 -- actual in the generated call.
415 begin
416 -- Determine the entity being renamed, which is the target of the call
417 -- statement. If the name is an explicit dereference, this is a renaming
418 -- of a subprogram type rather than a subprogram. The name itself is
419 -- fully analyzed.
421 if Nkind (Nam) = N_Selected_Component then
422 Old_S := Entity (Selector_Name (Nam));
424 elsif Nkind (Nam) = N_Explicit_Dereference then
425 Old_S := Etype (Nam);
427 elsif Nkind (Nam) = N_Indexed_Component then
428 if Is_Entity_Name (Prefix (Nam)) then
429 Old_S := Entity (Prefix (Nam));
430 else
431 Old_S := Entity (Selector_Name (Prefix (Nam)));
432 end if;
434 elsif Nkind (Nam) = N_Character_Literal then
435 Old_S := Etype (New_S);
437 else
438 Old_S := Entity (Nam);
439 end if;
441 if Is_Entity_Name (Nam) then
443 -- If the renamed entity is a predefined operator, retain full name
444 -- to ensure its visibility.
446 if Ekind (Old_S) = E_Operator
447 and then Nkind (Nam) = N_Expanded_Name
448 then
449 Call_Name := New_Copy (Name (N));
450 else
451 Call_Name := New_Occurrence_Of (Old_S, Loc);
452 end if;
454 else
455 if Nkind (Nam) = N_Selected_Component
456 and then Present (First_Formal (Old_S))
457 and then
458 (Is_Controlling_Formal (First_Formal (Old_S))
459 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
460 then
462 -- Retrieve the target object, to be added as a first actual
463 -- in the call.
465 Call_Name := New_Occurrence_Of (Old_S, Loc);
466 Pref := Prefix (Nam);
468 else
469 Call_Name := New_Copy (Name (N));
470 end if;
472 -- Original name may have been overloaded, but is fully resolved now
474 Set_Is_Overloaded (Call_Name, False);
475 end if;
477 if Nkind (Decl) /= N_Subprogram_Declaration then
478 Rewrite (N,
479 Make_Subprogram_Declaration (Loc,
480 Specification => Specification (N)));
481 end if;
483 -- For simple renamings, subsequent calls can be expanded directly as
484 -- calls to the renamed entity. The body must be generated in any case
485 -- for calls that may appear elsewhere. This is not done in the case
486 -- where the subprogram is an instantiation because the actual proper
487 -- body has not been built yet.
489 if Ekind (Old_S) in E_Function | E_Procedure
490 and then not Is_Generic_Instance (Old_S)
491 then
492 Set_Body_To_Inline (Decl, Old_S);
493 end if;
495 -- Check whether the return type is a limited view. If the subprogram
496 -- is already frozen the generated body may have a non-limited view
497 -- of the type, that must be used, because it is the one in the spec
498 -- of the renaming declaration.
500 if Ekind (Old_S) = E_Function
501 and then Is_Entity_Name (Result_Definition (Spec))
502 then
503 declare
504 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
505 begin
506 if Has_Non_Limited_View (Ret_Type) then
507 Set_Result_Definition
508 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
509 end if;
510 end;
511 end if;
513 -- The body generated for this renaming is an internal artifact, and
514 -- does not constitute a freeze point for the called entity.
516 Set_Must_Not_Freeze (Call_Name);
518 Formal := First_Formal (Defining_Entity (Decl));
520 if Present (Pref) then
521 declare
522 Pref_Type : constant Entity_Id := Etype (Pref);
523 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
525 begin
526 -- The controlling formal may be an access parameter, or the
527 -- actual may be an access value, so adjust accordingly.
529 if Is_Access_Type (Pref_Type)
530 and then not Is_Access_Type (Form_Type)
531 then
532 Actuals := New_List
533 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
535 elsif Is_Access_Type (Form_Type)
536 and then not Is_Access_Type (Pref)
537 then
538 Actuals :=
539 New_List (
540 Make_Attribute_Reference (Loc,
541 Attribute_Name => Name_Access,
542 Prefix => Relocate_Node (Pref)));
543 else
544 Actuals := New_List (Pref);
545 end if;
546 end;
548 elsif Present (Formal) then
549 Actuals := New_List;
551 else
552 Actuals := No_List;
553 end if;
555 while Present (Formal) loop
556 Append (New_Occurrence_Of (Formal, Loc), Actuals);
557 Next_Formal (Formal);
558 end loop;
560 -- If the renamed entity is an entry, inherit its profile. For other
561 -- renamings as bodies, both profiles must be subtype conformant, so it
562 -- is not necessary to replace the profile given in the declaration.
563 -- However, default values that are aggregates are rewritten when
564 -- partially analyzed, so we recover the original aggregate to insure
565 -- that subsequent conformity checking works. Similarly, if the default
566 -- expression was constant-folded, recover the original expression.
568 Formal := First_Formal (Defining_Entity (Decl));
570 if Present (Formal) then
571 O_Formal := First_Formal (Old_S);
572 Param_Spec := First (Parameter_Specifications (Spec));
573 while Present (Formal) loop
574 if Is_Entry (Old_S) then
575 if Nkind (Parameter_Type (Param_Spec)) /=
576 N_Access_Definition
577 then
578 Set_Etype (Formal, Etype (O_Formal));
579 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
580 end if;
582 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
583 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
584 Nkind (Default_Value (O_Formal))
585 then
586 Set_Expression (Param_Spec,
587 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
588 end if;
590 Next_Formal (Formal);
591 Next_Formal (O_Formal);
592 Next (Param_Spec);
593 end loop;
594 end if;
596 -- If the renamed entity is a function, the generated body contains a
597 -- return statement. Otherwise, build a procedure call. If the entity is
598 -- an entry, subsequent analysis of the call will transform it into the
599 -- proper entry or protected operation call. If the renamed entity is
600 -- a character literal, return it directly.
602 if Ekind (Old_S) = E_Function
603 or else Ekind (Old_S) = E_Operator
604 or else (Ekind (Old_S) = E_Subprogram_Type
605 and then Etype (Old_S) /= Standard_Void_Type)
606 then
607 Call_Node :=
608 Make_Simple_Return_Statement (Loc,
609 Expression =>
610 Make_Function_Call (Loc,
611 Name => Call_Name,
612 Parameter_Associations => Actuals));
614 elsif Ekind (Old_S) = E_Enumeration_Literal then
615 Call_Node :=
616 Make_Simple_Return_Statement (Loc,
617 Expression => New_Occurrence_Of (Old_S, Loc));
619 elsif Nkind (Nam) = N_Character_Literal then
620 Call_Node :=
621 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
623 else
624 Call_Node :=
625 Make_Procedure_Call_Statement (Loc,
626 Name => Call_Name,
627 Parameter_Associations => Actuals);
628 end if;
630 -- Create entities for subprogram body and formals
632 Set_Defining_Unit_Name (Spec,
633 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
635 Param_Spec := First (Parameter_Specifications (Spec));
636 while Present (Param_Spec) loop
637 Set_Defining_Identifier (Param_Spec,
638 Make_Defining_Identifier (Loc,
639 Chars => Chars (Defining_Identifier (Param_Spec))));
640 Next (Param_Spec);
641 end loop;
643 -- In GNATprove, prefer to generate an expression function whenever
644 -- possible, to benefit from the more precise analysis in that case
645 -- (as if an implicit postcondition had been generated).
647 if GNATprove_Mode
648 and then Nkind (Call_Node) = N_Simple_Return_Statement
649 then
650 Body_Node :=
651 Make_Expression_Function (Loc,
652 Specification => Spec,
653 Expression => Expression (Call_Node));
654 else
655 Body_Node :=
656 Make_Subprogram_Body (Loc,
657 Specification => Spec,
658 Declarations => New_List,
659 Handled_Statement_Sequence =>
660 Make_Handled_Sequence_Of_Statements (Loc,
661 Statements => New_List (Call_Node)));
662 end if;
664 -- Link the body to the entity whose declaration it completes. If
665 -- the body is analyzed when the renamed entity is frozen, it may
666 -- be necessary to restore the proper scope (see package Exp_Ch13).
668 if Nkind (N) = N_Subprogram_Renaming_Declaration
669 and then Present (Corresponding_Spec (N))
670 then
671 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
672 else
673 Set_Corresponding_Spec (Body_Node, New_S);
674 end if;
676 return Body_Node;
677 end Build_Renamed_Body;
679 --------------------------
680 -- Check_Address_Clause --
681 --------------------------
683 procedure Check_Address_Clause (E : Entity_Id) is
684 Addr : constant Node_Id := Address_Clause (E);
685 Typ : constant Entity_Id := Etype (E);
686 Decl : Node_Id;
687 Expr : Node_Id;
688 Init : Node_Id;
689 Lhs : Node_Id;
690 Tag_Assign : Node_Id;
692 begin
693 if Present (Addr) then
695 -- For a deferred constant, the initialization value is on full view
697 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
698 Decl := Declaration_Node (Full_View (E));
699 else
700 Decl := Declaration_Node (E);
701 end if;
703 Expr := Expression (Addr);
705 if Needs_Constant_Address (Decl, Typ) then
706 Check_Constant_Address_Clause (Expr, E);
708 -- Has_Delayed_Freeze was set on E when the address clause was
709 -- analyzed, and must remain set because we want the address
710 -- clause to be elaborated only after any entity it references
711 -- has been elaborated.
712 end if;
714 -- If Rep_Clauses are to be ignored, remove address clause from
715 -- list attached to entity, because it may be illegal for gigi,
716 -- for example by breaking order of elaboration.
718 if Ignore_Rep_Clauses then
719 declare
720 Rep : Node_Id;
722 begin
723 Rep := First_Rep_Item (E);
725 if Rep = Addr then
726 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
728 else
729 while Present (Rep)
730 and then Next_Rep_Item (Rep) /= Addr
731 loop
732 Next_Rep_Item (Rep);
733 end loop;
734 end if;
736 if Present (Rep) then
737 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
738 end if;
739 end;
741 -- And now remove the address clause
743 Kill_Rep_Clause (Addr);
745 elsif not Error_Posted (Expr)
746 and then not Needs_Finalization (Typ)
747 then
748 Warn_Overlay (Expr, Typ, Name (Addr));
749 end if;
751 Init := Expression (Decl);
753 -- If a variable, or a non-imported constant, overlays a constant
754 -- object and has an initialization value, then the initialization
755 -- may end up writing into read-only memory. Detect the cases of
756 -- statically identical values and remove the initialization. In
757 -- the other cases, give a warning. We will give other warnings
758 -- later for the variable if it is assigned.
760 if (Ekind (E) = E_Variable
761 or else (Ekind (E) = E_Constant
762 and then not Is_Imported (E)))
763 and then Overlays_Constant (E)
764 and then Present (Init)
765 then
766 declare
767 O_Ent : Entity_Id;
768 Off : Boolean;
770 begin
771 Find_Overlaid_Entity (Addr, O_Ent, Off);
773 if Ekind (O_Ent) = E_Constant
774 and then Etype (O_Ent) = Typ
775 and then Present (Constant_Value (O_Ent))
776 and then Compile_Time_Compare
777 (Init,
778 Constant_Value (O_Ent),
779 Assume_Valid => True) = EQ
780 then
781 Set_No_Initialization (Decl);
782 return;
784 elsif Comes_From_Source (Init)
785 and then Address_Clause_Overlay_Warnings
786 then
787 Error_Msg_Sloc := Sloc (Addr);
788 Error_Msg_NE
789 ("?o?constant& may be modified via address clause#",
790 Decl, O_Ent);
791 end if;
792 end;
793 end if;
795 -- Remove side effects from initial expression, except in the case of
796 -- limited build-in-place calls and aggregates, which have their own
797 -- expansion elsewhere. This exception is necessary to avoid copying
798 -- limited objects.
800 if Present (Init)
801 and then not Is_Inherently_Limited_Type (Typ)
802 then
803 -- Capture initialization value at point of declaration, and make
804 -- explicit assignment legal, because object may be a constant.
806 Remove_Side_Effects (Init);
807 Lhs := New_Occurrence_Of (E, Sloc (Decl));
808 Set_Assignment_OK (Lhs);
810 -- Move initialization to freeze actions, once the object has
811 -- been frozen and the address clause alignment check has been
812 -- performed.
814 Append_Freeze_Action (E,
815 Make_Assignment_Statement (Sloc (Decl),
816 Name => Lhs,
817 Expression => Expression (Decl)));
819 Set_No_Initialization (Decl);
821 -- If the object is tagged, check whether the tag must be
822 -- reassigned explicitly.
824 Tag_Assign := Make_Tag_Assignment (Decl);
825 if Present (Tag_Assign) then
826 Append_Freeze_Action (E, Tag_Assign);
827 end if;
828 end if;
829 end if;
830 end Check_Address_Clause;
832 -----------------------------
833 -- Check_Compile_Time_Size --
834 -----------------------------
836 procedure Check_Compile_Time_Size (T : Entity_Id) is
838 procedure Set_Small_Size (T : Entity_Id; S : Uint);
839 -- Sets the compile time known size in the RM_Size field of T, checking
840 -- for a size clause that was given which attempts to give a small size.
842 function Size_Known (T : Entity_Id) return Boolean;
843 -- Recursive function that does all the work
845 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
846 -- If T is a constrained subtype, its size is not known if any of its
847 -- discriminant constraints is not static and it is not a null record.
848 -- The test is conservative and doesn't check that the components are
849 -- in fact constrained by non-static discriminant values. Could be made
850 -- more precise ???
852 --------------------
853 -- Set_Small_Size --
854 --------------------
856 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
857 begin
858 if S > System_Max_Integer_Size then
859 return;
861 -- Check for bad size clause given
863 elsif Has_Size_Clause (T) then
864 if RM_Size (T) < S then
865 Error_Msg_Uint_1 := S;
866 Error_Msg_NE (Size_Too_Small_Message, Size_Clause (T), T);
867 end if;
869 -- Set size if not set already. Do not set it to Uint_0, because in
870 -- some cases (notably array-of-record), the Component_Size is
871 -- No_Uint, which causes S to be Uint_0. Presumably the RM_Size and
872 -- Component_Size will eventually be set correctly by the back end.
874 elsif not Known_RM_Size (T) and then S /= Uint_0 then
875 Set_RM_Size (T, S);
876 end if;
877 end Set_Small_Size;
879 ----------------
880 -- Size_Known --
881 ----------------
883 function Size_Known (T : Entity_Id) return Boolean is
884 Comp : Entity_Id;
885 Ctyp : Entity_Id;
887 begin
888 if Size_Known_At_Compile_Time (T) then
889 return True;
891 -- Always True for elementary types, even generic formal elementary
892 -- types. We used to return False in the latter case, but the size
893 -- is known at compile time, even in the template, we just do not
894 -- know the exact size but that's not the point of this routine.
896 elsif Is_Elementary_Type (T) or else Is_Task_Type (T) then
897 return True;
899 -- Array types
901 elsif Is_Array_Type (T) then
903 -- String literals always have known size, and we can set it
905 if Ekind (T) = E_String_Literal_Subtype then
906 if Known_Component_Size (T) then
907 Set_Small_Size
908 (T, Component_Size (T) * String_Literal_Length (T));
910 else
911 -- The following is wrong, but does what previous versions
912 -- did. The Component_Size is unknown for the string in a
913 -- pragma Warnings.
914 Set_Small_Size (T, Uint_0);
915 end if;
917 return True;
919 -- Unconstrained types never have known at compile time size
921 elsif not Is_Constrained (T) then
922 return False;
924 -- Don't do any recursion on type with error posted, since we may
925 -- have a malformed type that leads us into a loop.
927 elsif Error_Posted (T) then
928 return False;
930 -- Otherwise if component size unknown, then array size unknown
932 elsif not Size_Known (Component_Type (T)) then
933 return False;
934 end if;
936 -- Check for all indexes static, and also compute possible size
937 -- (in case it is not greater than System_Max_Integer_Size and
938 -- thus may be packable).
940 declare
941 Index : Entity_Id;
942 Low : Node_Id;
943 High : Node_Id;
944 Size : Uint := Component_Size (T);
945 Dim : Uint;
947 begin
948 -- See comment in Set_Small_Size above
950 if No (Size) then
951 Size := Uint_0;
952 end if;
954 Index := First_Index (T);
955 while Present (Index) loop
956 if Nkind (Index) = N_Range then
957 Get_Index_Bounds (Index, Low, High);
959 elsif Error_Posted (Scalar_Range (Etype (Index))) then
960 return False;
962 else
963 Low := Type_Low_Bound (Etype (Index));
964 High := Type_High_Bound (Etype (Index));
965 end if;
967 if not Compile_Time_Known_Value (Low)
968 or else not Compile_Time_Known_Value (High)
969 or else Etype (Index) = Any_Type
970 then
971 return False;
973 else
974 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
976 if Dim > Uint_0 then
977 Size := Size * Dim;
978 else
979 Size := Uint_0;
980 end if;
981 end if;
983 Next_Index (Index);
984 end loop;
986 Set_Small_Size (T, Size);
987 return True;
988 end;
990 -- For non-generic private types, go to underlying type if present
992 elsif Is_Private_Type (T)
993 and then not Is_Generic_Type (T)
994 and then Present (Underlying_Type (T))
995 then
996 -- Don't do any recursion on type with error posted, since we may
997 -- have a malformed type that leads us into a loop.
999 if Error_Posted (T) then
1000 return False;
1001 else
1002 return Size_Known (Underlying_Type (T));
1003 end if;
1005 -- Record types
1007 elsif Is_Record_Type (T) then
1009 -- A class-wide type is never considered to have a known size
1011 if Is_Class_Wide_Type (T) then
1012 return False;
1014 -- A subtype of a variant record must not have non-static
1015 -- discriminated components.
1017 elsif T /= Base_Type (T)
1018 and then not Static_Discriminated_Components (T)
1019 then
1020 return False;
1022 -- Don't do any recursion on type with error posted, since we may
1023 -- have a malformed type that leads us into a loop.
1025 elsif Error_Posted (T) then
1026 return False;
1027 end if;
1029 -- Now look at the components of the record
1031 declare
1032 -- The following two variables are used to keep track of the
1033 -- size of packed records if we can tell the size of the packed
1034 -- record in the front end. Packed_Size_Known is True if so far
1035 -- we can figure out the size. It is initialized to True for a
1036 -- packed record, unless the record has either discriminants or
1037 -- independent components, or is a strict-alignment type, since
1038 -- it cannot be fully packed in this case.
1040 -- The reason we eliminate the discriminated case is that
1041 -- we don't know the way the back end lays out discriminated
1042 -- packed records. If Packed_Size_Known is True, then
1043 -- Packed_Size is the size in bits so far.
1045 Packed_Size_Known : Boolean :=
1046 Is_Packed (T)
1047 and then not Has_Discriminants (T)
1048 and then not Has_Independent_Components (T)
1049 and then not Strict_Alignment (T);
1051 Packed_Size : Uint := Uint_0;
1052 -- Size in bits so far
1054 begin
1055 -- Test for variant part present
1057 if Has_Discriminants (T)
1058 and then Present (Parent (T))
1059 and then Nkind (Parent (T)) = N_Full_Type_Declaration
1060 and then Nkind (Type_Definition (Parent (T))) =
1061 N_Record_Definition
1062 and then not Null_Present (Type_Definition (Parent (T)))
1063 and then
1064 Present (Variant_Part
1065 (Component_List (Type_Definition (Parent (T)))))
1066 then
1067 -- If variant part is present, and type is unconstrained,
1068 -- then we must have defaulted discriminants, or a size
1069 -- clause must be present for the type, or else the size
1070 -- is definitely not known at compile time.
1072 if not Is_Constrained (T)
1073 and then
1074 No (Discriminant_Default_Value (First_Discriminant (T)))
1075 and then not Known_RM_Size (T)
1076 then
1077 return False;
1078 end if;
1079 end if;
1081 -- Loop through components
1083 Comp := First_Component_Or_Discriminant (T);
1084 while Present (Comp) loop
1085 Ctyp := Etype (Comp);
1087 -- We do not know the packed size if there is a component
1088 -- clause present (we possibly could, but this would only
1089 -- help in the case of a record with partial rep clauses.
1090 -- That's because in the case of full rep clauses, the
1091 -- size gets figured out anyway by a different circuit).
1093 if Present (Component_Clause (Comp)) then
1094 Packed_Size_Known := False;
1095 end if;
1097 -- We do not know the packed size for an independent
1098 -- component or if it is of a strict-alignment type,
1099 -- since packing does not touch these (RM 13.2(7)).
1101 if Is_Independent (Comp)
1102 or else Is_Independent (Ctyp)
1103 or else Strict_Alignment (Ctyp)
1104 then
1105 Packed_Size_Known := False;
1106 end if;
1108 -- We need to identify a component that is an array where
1109 -- the index type is an enumeration type with non-standard
1110 -- representation, and some bound of the type depends on a
1111 -- discriminant.
1113 -- This is because gigi computes the size by doing a
1114 -- substitution of the appropriate discriminant value in
1115 -- the size expression for the base type, and gigi is not
1116 -- clever enough to evaluate the resulting expression (which
1117 -- involves a call to rep_to_pos) at compile time.
1119 -- It would be nice if gigi would either recognize that
1120 -- this expression can be computed at compile time, or
1121 -- alternatively figured out the size from the subtype
1122 -- directly, where all the information is at hand ???
1124 if Is_Array_Type (Etype (Comp))
1125 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1126 then
1127 declare
1128 Ocomp : constant Entity_Id :=
1129 Original_Record_Component (Comp);
1130 OCtyp : constant Entity_Id := Etype (Ocomp);
1131 Ind : Node_Id;
1132 Indtyp : Entity_Id;
1133 Lo, Hi : Node_Id;
1135 begin
1136 Ind := First_Index (OCtyp);
1137 while Present (Ind) loop
1138 Indtyp := Etype (Ind);
1140 if Is_Enumeration_Type (Indtyp)
1141 and then Has_Non_Standard_Rep (Indtyp)
1142 then
1143 Lo := Type_Low_Bound (Indtyp);
1144 Hi := Type_High_Bound (Indtyp);
1146 if Is_Entity_Name (Lo)
1147 and then Ekind (Entity (Lo)) = E_Discriminant
1148 then
1149 return False;
1151 elsif Is_Entity_Name (Hi)
1152 and then Ekind (Entity (Hi)) = E_Discriminant
1153 then
1154 return False;
1155 end if;
1156 end if;
1158 Next_Index (Ind);
1159 end loop;
1160 end;
1161 end if;
1163 -- Clearly size of record is not known if the size of one of
1164 -- the components is not known.
1166 if not Size_Known (Ctyp) then
1167 return False;
1168 end if;
1170 -- Accumulate packed size if possible
1172 if Packed_Size_Known then
1174 -- We can deal with elementary types, small packed arrays
1175 -- if the representation is a modular type and also small
1176 -- record types as checked by Set_Small_Size.
1178 if Is_Elementary_Type (Ctyp)
1179 or else (Is_Array_Type (Ctyp)
1180 and then Present
1181 (Packed_Array_Impl_Type (Ctyp))
1182 and then Is_Modular_Integer_Type
1183 (Packed_Array_Impl_Type (Ctyp)))
1184 or else Is_Record_Type (Ctyp)
1185 then
1186 -- If RM_Size is known and static, then we can keep
1187 -- accumulating the packed size.
1189 if Known_Static_RM_Size (Ctyp) then
1191 Packed_Size := Packed_Size + RM_Size (Ctyp);
1193 -- If we have a field whose RM_Size is not known then
1194 -- we can't figure out the packed size here.
1196 else
1197 Packed_Size_Known := False;
1198 end if;
1200 -- For other types we can't figure out the packed size
1202 else
1203 Packed_Size_Known := False;
1204 end if;
1205 end if;
1207 Next_Component_Or_Discriminant (Comp);
1208 end loop;
1210 if Packed_Size_Known then
1211 Set_Small_Size (T, Packed_Size);
1212 end if;
1214 return True;
1215 end;
1217 -- All other cases, size not known at compile time
1219 else
1220 return False;
1221 end if;
1222 end Size_Known;
1224 -------------------------------------
1225 -- Static_Discriminated_Components --
1226 -------------------------------------
1228 function Static_Discriminated_Components
1229 (T : Entity_Id) return Boolean
1231 Constraint : Elmt_Id;
1233 begin
1234 if Has_Discriminants (T)
1235 and then Present (Discriminant_Constraint (T))
1236 and then Present (First_Component (T))
1237 then
1238 Constraint := First_Elmt (Discriminant_Constraint (T));
1239 while Present (Constraint) loop
1240 if not Compile_Time_Known_Value (Node (Constraint)) then
1241 return False;
1242 end if;
1244 Next_Elmt (Constraint);
1245 end loop;
1246 end if;
1248 return True;
1249 end Static_Discriminated_Components;
1251 -- Start of processing for Check_Compile_Time_Size
1253 begin
1254 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1255 end Check_Compile_Time_Size;
1257 -----------------------------------
1258 -- Check_Component_Storage_Order --
1259 -----------------------------------
1261 procedure Check_Component_Storage_Order
1262 (Encl_Type : Entity_Id;
1263 Comp : Entity_Id;
1264 ADC : Node_Id;
1265 Comp_ADC_Present : out Boolean)
1267 Comp_Base : Entity_Id;
1268 Comp_ADC : Node_Id;
1269 Encl_Base : Entity_Id;
1270 Err_Node : Node_Id;
1272 Component_Aliased : Boolean;
1274 Comp_Byte_Aligned : Boolean := False;
1275 -- Set for the record case, True if Comp is aligned on byte boundaries
1276 -- (in which case it is allowed to have different storage order).
1278 Comp_SSO_Differs : Boolean;
1279 -- Set True when the component is a nested composite, and it does not
1280 -- have the same scalar storage order as Encl_Type.
1282 begin
1283 -- Record case
1285 if Present (Comp) then
1286 Err_Node := Comp;
1287 Comp_Base := Etype (Comp);
1289 if Is_Tag (Comp) then
1290 Comp_Byte_Aligned := True;
1291 Component_Aliased := False;
1293 else
1294 -- If a component clause is present, check if the component starts
1295 -- and ends on byte boundaries. Otherwise conservatively assume it
1296 -- does so only in the case where the record is not packed.
1298 if Present (Component_Clause (Comp)) then
1299 Comp_Byte_Aligned :=
1300 Known_Normalized_First_Bit (Comp)
1301 and then
1302 Known_Esize (Comp)
1303 and then
1304 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0
1305 and then
1306 Esize (Comp) mod System_Storage_Unit = 0;
1307 else
1308 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1309 end if;
1311 Component_Aliased := Is_Aliased (Comp);
1312 end if;
1314 -- Array case
1316 else
1317 Err_Node := Encl_Type;
1318 Comp_Base := Component_Type (Encl_Type);
1320 Component_Aliased := Has_Aliased_Components (Encl_Type);
1321 end if;
1323 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1324 -- the attribute definition clause is attached to the first subtype.
1325 -- Also, if the base type is incomplete or private, go to full view
1326 -- if known
1328 Encl_Base := Base_Type (Encl_Type);
1329 if Present (Underlying_Type (Encl_Base)) then
1330 Encl_Base := Underlying_Type (Encl_Base);
1331 end if;
1333 Comp_Base := Base_Type (Comp_Base);
1334 if Present (Underlying_Type (Comp_Base)) then
1335 Comp_Base := Underlying_Type (Comp_Base);
1336 end if;
1338 Comp_ADC :=
1339 Get_Attribute_Definition_Clause
1340 (First_Subtype (Comp_Base), Attribute_Scalar_Storage_Order);
1341 Comp_ADC_Present := Present (Comp_ADC);
1343 -- Case of record or array component: check storage order compatibility.
1344 -- But, if the record has Complex_Representation, then it is treated as
1345 -- a scalar in the back end so the storage order is irrelevant.
1347 if (Is_Record_Type (Comp_Base)
1348 and then not Has_Complex_Representation (Comp_Base))
1349 or else Is_Array_Type (Comp_Base)
1350 then
1351 Comp_SSO_Differs :=
1352 Reverse_Storage_Order (Encl_Base) /=
1353 Reverse_Storage_Order (Comp_Base);
1355 -- Parent and extension must have same storage order
1357 if Present (Comp) and then Chars (Comp) = Name_uParent then
1358 if Comp_SSO_Differs then
1359 Error_Msg_N
1360 ("record extension must have same scalar storage order as "
1361 & "parent", Err_Node);
1362 end if;
1364 -- If component and composite SSO differs, check that component
1365 -- falls on byte boundaries and isn't bit packed.
1367 elsif Comp_SSO_Differs then
1369 -- Component SSO differs from enclosing composite:
1371 -- Reject if composite is a bit-packed array, as it is rewritten
1372 -- into an array of scalars.
1374 if Is_Bit_Packed_Array (Encl_Base) then
1375 Error_Msg_N
1376 ("type of packed array must have same scalar storage order "
1377 & "as component", Err_Node);
1379 -- Reject if not byte aligned
1381 elsif Is_Record_Type (Encl_Base)
1382 and then not Comp_Byte_Aligned
1383 then
1384 if Present (Component_Clause (Comp)) then
1385 Error_Msg_N
1386 ("type of non-byte-aligned component must have same scalar"
1387 & " storage order as enclosing record", Err_Node);
1388 else
1389 Error_Msg_N
1390 ("type of packed component must have same scalar"
1391 & " storage order as enclosing record", Err_Node);
1392 end if;
1394 -- Warn if specified only for the outer composite
1396 elsif Present (ADC) and then No (Comp_ADC) then
1397 Error_Msg_NE
1398 ("scalar storage order specified for & does not apply to "
1399 & "component?", Err_Node, Encl_Base);
1400 end if;
1401 end if;
1403 -- Enclosing type has explicit SSO: non-composite component must not
1404 -- be aliased.
1406 elsif Present (ADC) and then Component_Aliased then
1407 Error_Msg_N
1408 ("aliased component not permitted for type with explicit "
1409 & "Scalar_Storage_Order", Err_Node);
1410 end if;
1411 end Check_Component_Storage_Order;
1413 -----------------------------
1414 -- Check_Debug_Info_Needed --
1415 -----------------------------
1417 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1418 begin
1419 if Debug_Info_Off (T) then
1420 return;
1422 elsif Comes_From_Source (T)
1423 or else Debug_Generated_Code
1424 or else Debug_Flag_VV
1425 or else Needs_Debug_Info (T)
1426 then
1427 Set_Debug_Info_Needed (T);
1428 end if;
1429 end Check_Debug_Info_Needed;
1431 -------------------------------
1432 -- Check_Expression_Function --
1433 -------------------------------
1435 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1436 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1437 -- Function to search for deferred constant
1439 -------------------
1440 -- Find_Constant --
1441 -------------------
1443 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1444 begin
1445 -- When a constant is initialized with the result of a dispatching
1446 -- call, the constant declaration is rewritten as a renaming of the
1447 -- displaced function result. This scenario is not a premature use of
1448 -- a constant even though the Has_Completion flag is not set.
1450 if Is_Entity_Name (Nod)
1451 and then Present (Entity (Nod))
1452 and then Ekind (Entity (Nod)) = E_Constant
1453 and then Scope (Entity (Nod)) = Current_Scope
1454 and then Nkind (Declaration_Node (Entity (Nod))) =
1455 N_Object_Declaration
1456 and then not Is_Imported (Entity (Nod))
1457 and then not Has_Completion (Entity (Nod))
1458 and then not (Present (Full_View (Entity (Nod)))
1459 and then Has_Completion (Full_View (Entity (Nod))))
1460 then
1461 Error_Msg_NE
1462 ("premature use of& in call or instance", N, Entity (Nod));
1464 elsif Nkind (Nod) = N_Attribute_Reference then
1465 Analyze (Prefix (Nod));
1467 if Is_Entity_Name (Prefix (Nod))
1468 and then Is_Type (Entity (Prefix (Nod)))
1469 then
1470 if Expander_Active then
1471 Check_Fully_Declared (Entity (Prefix (Nod)), N);
1472 end if;
1474 Freeze_Before (N, Entity (Prefix (Nod)));
1475 end if;
1476 end if;
1478 return OK;
1479 end Find_Constant;
1481 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1483 -- Local variables
1485 Decl : Node_Id;
1487 -- Start of processing for Check_Expression_Function
1489 begin
1490 Decl := Original_Node (Unit_Declaration_Node (Nam));
1492 -- The subprogram body created for the expression function is not
1493 -- itself a freeze point.
1495 if Scope (Nam) = Current_Scope
1496 and then Nkind (Decl) = N_Expression_Function
1497 and then Nkind (N) /= N_Subprogram_Body
1498 then
1499 Check_Deferred (Expression (Decl));
1500 end if;
1501 end Check_Expression_Function;
1503 --------------------------------
1504 -- Check_Inherited_Conditions --
1505 --------------------------------
1507 procedure Check_Inherited_Conditions
1508 (R : Entity_Id;
1509 Late_Overriding : Boolean := False)
1511 Prim_Ops : constant Elist_Id := Primitive_Operations (R);
1512 Decls : List_Id;
1513 Op_Node : Elmt_Id;
1514 Par_Prim : Entity_Id;
1515 Prim : Entity_Id;
1516 Wrapper_Needed : Boolean;
1518 function Build_DTW_Body
1519 (Loc : Source_Ptr;
1520 DTW_Spec : Node_Id;
1521 DTW_Decls : List_Id;
1522 Par_Prim : Entity_Id;
1523 Wrapped_Subp : Entity_Id) return Node_Id;
1524 -- Build the body of the dispatch table wrapper containing the given
1525 -- spec and declarations; the call to the wrapped subprogram includes
1526 -- the proper type conversion.
1528 function Build_DTW_Spec (Par_Prim : Entity_Id) return Node_Id;
1529 -- Build the spec of the dispatch table wrapper
1531 procedure Build_Inherited_Condition_Pragmas
1532 (Subp : Entity_Id;
1533 Wrapper_Needed : out Boolean);
1534 -- Build corresponding pragmas for an operation whose ancestor has
1535 -- class-wide pre/postconditions. If the operation is inherited then
1536 -- Wrapper_Needed is returned True to force the creation of a wrapper
1537 -- for the inherited operation. If the ancestor is being overridden,
1538 -- the pragmas are constructed only to verify their legality, in case
1539 -- they contain calls to other primitives that may have been overridden.
1541 function Needs_Wrapper
1542 (Class_Cond : Node_Id;
1543 Subp : Entity_Id;
1544 Par_Subp : Entity_Id) return Boolean;
1545 -- Checks whether the dispatch-table wrapper (DTW) for Subp must be
1546 -- built to evaluate the given class-wide condition.
1548 --------------------
1549 -- Build_DTW_Body --
1550 --------------------
1552 function Build_DTW_Body
1553 (Loc : Source_Ptr;
1554 DTW_Spec : Node_Id;
1555 DTW_Decls : List_Id;
1556 Par_Prim : Entity_Id;
1557 Wrapped_Subp : Entity_Id) return Node_Id
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 Is_Controlling_Formal (Formal) then
1575 Append_To (Actuals,
1576 Make_Type_Conversion (Loc,
1577 New_Occurrence_Of (Etype (Formal), Loc),
1578 New_Occurrence_Of (New_Formal, Loc)));
1579 else
1580 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1581 end if;
1583 Next_Formal (Formal);
1584 Next (New_F_Spec);
1585 end loop;
1587 if Ekind (Wrapped_Subp) = E_Procedure then
1588 Call :=
1589 Make_Procedure_Call_Statement (Loc,
1590 Name => New_Occurrence_Of (Wrapped_Subp, Loc),
1591 Parameter_Associations => Actuals);
1592 else
1593 Call :=
1594 Make_Simple_Return_Statement (Loc,
1595 Expression =>
1596 Make_Function_Call (Loc,
1597 Name => New_Occurrence_Of (Wrapped_Subp, Loc),
1598 Parameter_Associations => Actuals));
1599 end if;
1601 return
1602 Make_Subprogram_Body (Loc,
1603 Specification => Copy_Subprogram_Spec (DTW_Spec),
1604 Declarations => DTW_Decls,
1605 Handled_Statement_Sequence =>
1606 Make_Handled_Sequence_Of_Statements (Loc,
1607 Statements => New_List (Call),
1608 End_Label => Make_Identifier (Loc,
1609 Chars (Defining_Entity (DTW_Spec)))));
1610 end Build_DTW_Body;
1612 --------------------
1613 -- Build_DTW_Spec --
1614 --------------------
1616 function Build_DTW_Spec (Par_Prim : Entity_Id) return Node_Id is
1617 DTW_Id : Entity_Id;
1618 DTW_Spec : Node_Id;
1620 begin
1621 DTW_Spec := Build_Overriding_Spec (Par_Prim, R);
1622 DTW_Id := Defining_Entity (DTW_Spec);
1624 -- Clear the not-overriding indicator since the DTW wrapper overrides
1625 -- its wrapped subprogram; required because if present in the parent
1626 -- primitive, given that Build_Overriding_Spec inherits it, we report
1627 -- spurious errors.
1629 Set_Must_Not_Override (DTW_Spec, False);
1631 -- Add minimal decoration of fields
1633 Mutate_Ekind (DTW_Id, Ekind (Par_Prim));
1634 Set_LSP_Subprogram (DTW_Id, Par_Prim);
1635 Set_Is_Dispatch_Table_Wrapper (DTW_Id);
1636 Set_Is_Wrapper (DTW_Id);
1638 -- The DTW wrapper is never a null procedure
1640 if Nkind (DTW_Spec) = N_Procedure_Specification then
1641 Set_Null_Present (DTW_Spec, False);
1642 end if;
1644 return DTW_Spec;
1645 end Build_DTW_Spec;
1647 ---------------------------------------
1648 -- Build_Inherited_Condition_Pragmas --
1649 ---------------------------------------
1651 procedure Build_Inherited_Condition_Pragmas
1652 (Subp : Entity_Id;
1653 Wrapper_Needed : out Boolean)
1655 Class_Pre : constant Node_Id :=
1656 Class_Preconditions (Ultimate_Alias (Subp));
1657 Class_Post : Node_Id := Class_Postconditions (Par_Prim);
1658 A_Post : Node_Id;
1659 New_Prag : Node_Id;
1661 begin
1662 Wrapper_Needed := False;
1664 if No (Class_Pre) and then No (Class_Post) then
1665 return;
1666 end if;
1668 -- For class-wide preconditions we just evaluate whether the wrapper
1669 -- is needed; there is no need to build the pragma since the check
1670 -- is performed on the caller side.
1672 if Present (Class_Pre)
1673 and then Needs_Wrapper (Class_Pre, Subp, Par_Prim)
1674 then
1675 Wrapper_Needed := True;
1676 end if;
1678 -- For class-wide postconditions we evaluate whether the wrapper is
1679 -- needed and we build the class-wide postcondition pragma to install
1680 -- it in the wrapper.
1682 if Present (Class_Post)
1683 and then Needs_Wrapper (Class_Post, Subp, Par_Prim)
1684 then
1685 Wrapper_Needed := True;
1687 -- Update the class-wide postcondition
1689 Class_Post := New_Copy_Tree (Class_Post);
1690 Build_Class_Wide_Expression
1691 (Pragma_Or_Expr => Class_Post,
1692 Subp => Subp,
1693 Par_Subp => Par_Prim,
1694 Adjust_Sloc => False);
1696 -- Install the updated class-wide postcondition in a copy of the
1697 -- pragma postcondition defined for the nearest ancestor.
1699 A_Post := Get_Class_Wide_Pragma (Par_Prim,
1700 Pragma_Postcondition);
1702 if No (A_Post) then
1703 declare
1704 Subps : constant Subprogram_List :=
1705 Inherited_Subprograms (Subp);
1706 begin
1707 for Index in Subps'Range loop
1708 A_Post := Get_Class_Wide_Pragma (Subps (Index),
1709 Pragma_Postcondition);
1710 exit when Present (A_Post);
1711 end loop;
1712 end;
1713 end if;
1715 -- A_Post can be null here if the postcondition was inlined in the
1716 -- called subprogram.
1718 if Present (A_Post) then
1719 New_Prag := New_Copy_Tree (A_Post);
1720 Rewrite
1721 (Expression (First (Pragma_Argument_Associations (New_Prag))),
1722 Class_Post);
1723 Append (New_Prag, Decls);
1724 end if;
1725 end if;
1726 end Build_Inherited_Condition_Pragmas;
1728 -------------------
1729 -- Needs_Wrapper --
1730 -------------------
1732 function Needs_Wrapper
1733 (Class_Cond : Node_Id;
1734 Subp : Entity_Id;
1735 Par_Subp : Entity_Id) return Boolean
1737 Result : Boolean := False;
1739 function Check_Entity (N : Node_Id) return Traverse_Result;
1740 -- Check calls to overridden primitives
1742 --------------------
1743 -- Replace_Entity --
1744 --------------------
1746 function Check_Entity (N : Node_Id) return Traverse_Result is
1747 New_E : Entity_Id;
1749 begin
1750 if Nkind (N) = N_Identifier
1751 and then Present (Entity (N))
1752 and then
1753 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1754 and then
1755 (Nkind (Parent (N)) /= N_Attribute_Reference
1756 or else Attribute_Name (Parent (N)) /= Name_Class)
1757 then
1758 -- Determine whether entity has a renaming
1760 New_E := Get_Mapped_Entity (Entity (N));
1762 -- If the entity is an overridden primitive and we are not
1763 -- in GNATprove mode, we must build a wrapper for the current
1764 -- inherited operation. If the reference is the prefix of an
1765 -- attribute such as 'Result (or others ???) there is no need
1766 -- for a wrapper: the condition is just rewritten in terms of
1767 -- the inherited subprogram.
1769 if Present (New_E)
1770 and then Comes_From_Source (New_E)
1771 and then Is_Subprogram (New_E)
1772 and then Nkind (Parent (N)) /= N_Attribute_Reference
1773 and then not GNATprove_Mode
1774 then
1775 Result := True;
1776 return Abandon;
1777 end if;
1778 end if;
1780 return OK;
1781 end Check_Entity;
1783 procedure Check_Condition_Entities is
1784 new Traverse_Proc (Check_Entity);
1786 -- Start of processing for Needs_Wrapper
1788 begin
1789 Update_Primitives_Mapping (Par_Subp, Subp);
1791 Map_Formals (Par_Subp, Subp);
1792 Check_Condition_Entities (Class_Cond);
1794 return Result;
1795 end Needs_Wrapper;
1797 Ifaces_List : Elist_Id := No_Elist;
1798 Ifaces_Listed : Boolean := False;
1799 -- Cache the list of interface operations inherited by R
1801 Wrappers_List : Elist_Id := No_Elist;
1802 -- List containing identifiers of built wrappers. Used to defer building
1803 -- and analyzing their class-wide precondition subprograms.
1805 -- Start of processing for Check_Inherited_Conditions
1807 begin
1808 if Late_Overriding then
1809 Op_Node := First_Elmt (Prim_Ops);
1810 while Present (Op_Node) loop
1811 Prim := Node (Op_Node);
1813 -- Map the overridden primitive to the overriding one
1815 if Present (Overridden_Operation (Prim))
1816 and then Comes_From_Source (Prim)
1817 then
1818 Par_Prim := Overridden_Operation (Prim);
1819 Update_Primitives_Mapping (Par_Prim, Prim);
1821 -- Force discarding previous mappings of its formals
1823 Map_Formals (Par_Prim, Prim, Force_Update => True);
1824 end if;
1826 Next_Elmt (Op_Node);
1827 end loop;
1828 end if;
1830 -- Perform validity checks on the inherited conditions of overriding
1831 -- operations, for conformance with LSP, and apply SPARK-specific
1832 -- restrictions on inherited conditions.
1834 Op_Node := First_Elmt (Prim_Ops);
1835 while Present (Op_Node) loop
1836 Prim := Node (Op_Node);
1838 Par_Prim := Overridden_Operation (Prim);
1839 if Present (Par_Prim)
1840 and then Comes_From_Source (Prim)
1841 then
1842 -- When the primitive is an LSP wrapper we climb to the parent
1843 -- primitive that has the inherited contract.
1845 if Is_Wrapper (Par_Prim)
1846 and then Present (LSP_Subprogram (Par_Prim))
1847 then
1848 Par_Prim := LSP_Subprogram (Par_Prim);
1849 end if;
1851 -- Check that overrider and overridden operations have
1852 -- the same strub mode.
1854 Check_Same_Strub_Mode (Prim, Par_Prim);
1856 -- Analyze the contract items of the overridden operation, before
1857 -- they are rewritten as pragmas.
1859 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1861 -- In GNATprove mode this is where we can collect the inherited
1862 -- conditions, because we do not create the Check pragmas that
1863 -- normally convey the modified class-wide conditions on
1864 -- overriding operations.
1866 if GNATprove_Mode then
1867 Collect_Inherited_Class_Wide_Conditions (Prim);
1868 end if;
1869 end if;
1871 -- Go over operations inherited from interfaces and check
1872 -- them for strub mode compatibility as well.
1874 if Has_Interfaces (R)
1875 and then Is_Dispatching_Operation (Prim)
1876 and then Find_Dispatching_Type (Prim) = R
1877 then
1878 declare
1879 Elmt : Elmt_Id;
1880 Iface_Elmt : Elmt_Id;
1881 Iface : Entity_Id;
1882 Iface_Prim : Entity_Id;
1884 begin
1885 -- Collect the interfaces only once. We haven't
1886 -- finished freezing yet, so we can't use the faster
1887 -- search from Sem_Disp.Covered_Interface_Primitives.
1889 if not Ifaces_Listed then
1890 Collect_Interfaces (R, Ifaces_List);
1891 Ifaces_Listed := True;
1892 end if;
1894 Iface_Elmt := First_Elmt (Ifaces_List);
1895 while Present (Iface_Elmt) loop
1896 Iface := Node (Iface_Elmt);
1898 Elmt := First_Elmt (Primitive_Operations (Iface));
1899 while Present (Elmt) loop
1900 Iface_Prim := Node (Elmt);
1902 if Iface_Prim /= Par_Prim
1903 and then Chars (Iface_Prim) = Chars (Prim)
1904 and then Comes_From_Source (Iface_Prim)
1905 and then Is_Interface_Conformant
1906 (R, Iface_Prim, Prim)
1907 then
1908 Check_Same_Strub_Mode (Prim, Iface_Prim);
1909 end if;
1911 Next_Elmt (Elmt);
1912 end loop;
1914 Next_Elmt (Iface_Elmt);
1915 end loop;
1916 end;
1917 end if;
1919 Next_Elmt (Op_Node);
1920 end loop;
1922 -- Now examine the inherited operations to check whether they require
1923 -- a wrapper to handle inherited conditions that call other primitives,
1924 -- so that LSP can be verified/enforced.
1926 Op_Node := First_Elmt (Prim_Ops);
1928 while Present (Op_Node) loop
1929 Decls := Empty_List;
1930 Prim := Node (Op_Node);
1931 Wrapper_Needed := False;
1933 -- Skip internal entities built for mapping interface primitives
1935 if not Comes_From_Source (Prim)
1936 and then Present (Alias (Prim))
1937 and then No (Interface_Alias (Prim))
1938 then
1939 Par_Prim := Ultimate_Alias (Prim);
1941 -- When the primitive is an LSP wrapper we climb to the parent
1942 -- primitive that has the inherited contract.
1944 if Is_Wrapper (Par_Prim)
1945 and then Present (LSP_Subprogram (Par_Prim))
1946 then
1947 Par_Prim := LSP_Subprogram (Par_Prim);
1948 end if;
1950 -- Analyze the contract items of the parent operation, and
1951 -- determine whether a wrapper is needed. This is determined
1952 -- when the condition is rewritten in sem_prag, using the
1953 -- mapping between overridden and overriding operations built
1954 -- in the loop above.
1956 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1957 Build_Inherited_Condition_Pragmas (Prim, Wrapper_Needed);
1958 end if;
1960 if Wrapper_Needed
1961 and then not Is_Abstract_Subprogram (Par_Prim)
1962 and then Expander_Active
1963 then
1964 -- Build the dispatch-table wrapper (DTW). The support for
1965 -- AI12-0195 relies on two kind of wrappers: one for indirect
1966 -- calls (also used for AI12-0220), and one for putting in the
1967 -- dispatch table:
1969 -- 1) "indirect-call wrapper" (ICW) is needed anytime there are
1970 -- class-wide preconditions. Prim'Access will point directly
1971 -- at the ICW if any, or at the "pristine" body if Prim has
1972 -- no class-wide preconditions.
1974 -- 2) "dispatch-table wrapper" (DTW) is needed anytime the class
1975 -- wide preconditions *or* the class-wide postconditions are
1976 -- affected by overriding.
1978 -- The DTW holds a single statement that is a single call where
1979 -- the controlling actuals are conversions to the corresponding
1980 -- type in the parent primitive. If the primitive is a function
1981 -- the statement is a return statement with a call.
1983 declare
1984 Alias_Id : constant Entity_Id := Ultimate_Alias (Prim);
1985 Loc : constant Source_Ptr := Sloc (R);
1986 DTW_Body : Node_Id;
1987 DTW_Decl : Node_Id;
1988 DTW_Id : Entity_Id;
1989 DTW_Spec : Node_Id;
1991 Prim_Next_E : constant Entity_Id := Next_Entity (Prim);
1992 Prim_Prev_E : constant Entity_Id := Prev_Entity (Prim);
1994 begin
1995 DTW_Spec := Build_DTW_Spec (Par_Prim);
1996 DTW_Id := Defining_Entity (DTW_Spec);
1997 DTW_Decl := Make_Subprogram_Declaration (Loc,
1998 Specification => DTW_Spec);
2000 -- The spec of the wrapper has been built using the source
2001 -- location of its parent primitive; we must update it now
2002 -- (with the source location of the internal primitive built
2003 -- by Derive_Subprogram that will override this wrapper) to
2004 -- avoid inlining conflicts between internally built helpers
2005 -- for class-wide pre/postconditions of the parent and the
2006 -- helpers built for this wrapper.
2008 Set_Sloc (DTW_Id, Sloc (Prim));
2010 -- For inherited class-wide preconditions the DTW wrapper
2011 -- reuses the ICW of the parent (which checks the parent
2012 -- interpretation of the class-wide preconditions); the
2013 -- interpretation of the class-wide preconditions for the
2014 -- inherited subprogram is checked at the caller side.
2016 -- When the subprogram inherits class-wide postconditions
2017 -- the DTW also checks the interpretation of the class-wide
2018 -- postconditions for the inherited subprogram, and the body
2019 -- of the parent checks its interpretation of the parent for
2020 -- the class-wide postconditions.
2022 -- procedure Prim (F1 : T1; ...) is
2023 -- [ pragma Check (Postcondition, Expr); ]
2024 -- begin
2025 -- Par_Prim_ICW (Par_Type (F1), ...);
2026 -- end;
2028 if Present (Indirect_Call_Wrapper (Par_Prim)) then
2029 DTW_Body :=
2030 Build_DTW_Body (Loc,
2031 DTW_Spec => DTW_Spec,
2032 DTW_Decls => Decls,
2033 Par_Prim => Par_Prim,
2034 Wrapped_Subp => Indirect_Call_Wrapper (Par_Prim));
2036 -- For subprograms that only inherit class-wide postconditions
2037 -- the DTW wrapper calls the parent primitive (which on its
2038 -- body checks the interpretation of the class-wide post-
2039 -- conditions for the parent subprogram), and the DTW checks
2040 -- the interpretation of the class-wide postconditions for the
2041 -- inherited subprogram.
2043 -- procedure Prim (F1 : T1; ...) is
2044 -- pragma Check (Postcondition, Expr);
2045 -- begin
2046 -- Par_Prim (Par_Type (F1), ...);
2047 -- end;
2049 else
2050 DTW_Body :=
2051 Build_DTW_Body (Loc,
2052 DTW_Spec => DTW_Spec,
2053 DTW_Decls => Decls,
2054 Par_Prim => Par_Prim,
2055 Wrapped_Subp => Par_Prim);
2056 end if;
2058 -- Insert the declaration of the wrapper before the freezing
2059 -- node of the record type declaration to ensure that it will
2060 -- override the internal primitive built by Derive_Subprogram.
2062 if Late_Overriding then
2063 Ensure_Freeze_Node (R);
2064 Insert_Before_And_Analyze (Freeze_Node (R), DTW_Decl);
2065 else
2066 Append_Freeze_Action (R, DTW_Decl);
2067 Analyze (DTW_Decl);
2068 end if;
2070 -- The analyis of DTW_Decl has removed Prim from its scope
2071 -- chain and added DTW_Id at the end of the scope chain. Move
2072 -- DTW_Id to its correct place in the scope chain: the analysis
2073 -- of the wrapper declaration has just added DTW_Id at the end
2074 -- of the list of entities of its scope. However, given that
2075 -- this wrapper overrides Prim, we must move DTW_Id to the
2076 -- original place of Prim in its scope chain. This is required
2077 -- for wrappers of private type primitives to ensure their
2078 -- correct visibility since wrappers are built when the full
2079 -- tagged type declaration is frozen (in the private part of
2080 -- the package) but they may override primitives defined in the
2081 -- public part of the package.
2083 declare
2084 DTW_Prev_E : constant Entity_Id := Prev_Entity (DTW_Id);
2086 begin
2087 pragma Assert (Last_Entity (Current_Scope) = DTW_Id);
2088 pragma Assert
2089 (Ekind (Current_Scope) not in E_Package | E_Generic_Package
2090 or else No (First_Private_Entity (Current_Scope))
2091 or else First_Private_Entity (Current_Scope) /= DTW_Id);
2093 -- Remove DTW_Id from the end of the doubly-linked list of
2094 -- entities of this scope; no need to handle removing it
2095 -- from the beginning of the chain since such case can never
2096 -- occur for this entity.
2098 Set_Last_Entity (Current_Scope, DTW_Prev_E);
2099 Set_Next_Entity (DTW_Prev_E, Empty);
2101 -- Place DTW_Id back in the original place of its wrapped
2102 -- primitive in the list of entities of this scope.
2104 Link_Entities (Prim_Prev_E, DTW_Id);
2105 Link_Entities (DTW_Id, Prim_Next_E);
2106 end;
2108 -- Insert the body of the wrapper in the freeze actions of
2109 -- its record type declaration to ensure that it is placed
2110 -- in the scope of its declaration but not too early to cause
2111 -- premature freezing of other entities.
2113 Append_Freeze_Action (R, DTW_Body);
2114 Analyze (DTW_Body);
2116 -- Ensure correct decoration
2118 pragma Assert (Is_Dispatching_Operation (DTW_Id));
2119 pragma Assert (Present (Overridden_Operation (DTW_Id)));
2120 pragma Assert (Overridden_Operation (DTW_Id) = Alias_Id);
2122 -- Inherit dispatch table slot
2124 Set_DTC_Entity_Value (R, DTW_Id);
2125 Set_DT_Position (DTW_Id, DT_Position (Alias_Id));
2127 -- Register the wrapper in the dispatch table
2129 if Late_Overriding
2130 and then not Building_Static_DT (R)
2131 then
2132 Insert_List_After_And_Analyze (Freeze_Node (R),
2133 Register_Primitive (Loc, DTW_Id));
2134 end if;
2136 -- Defer building helpers and ICW for the DTW. Required to
2137 -- ensure uniqueness in their names because when building
2138 -- these wrappers for overlapped subprograms their homonym
2139 -- number is not definite until all these dispatch table
2140 -- wrappers of tagged type R have been analyzed.
2142 if Present (Indirect_Call_Wrapper (Par_Prim)) then
2143 Append_New_Elmt (DTW_Id, Wrappers_List);
2144 end if;
2145 end;
2146 end if;
2148 Next_Elmt (Op_Node);
2149 end loop;
2151 -- Build and analyze deferred class-wide precondition subprograms of
2152 -- built wrappers.
2154 if Present (Wrappers_List) then
2155 declare
2156 Body_N : Node_Id;
2157 CW_Subp : Entity_Id;
2158 Decl_N : Node_Id;
2159 DTW_Id : Entity_Id;
2160 Elmt : Elmt_Id;
2162 begin
2163 Elmt := First_Elmt (Wrappers_List);
2165 while Present (Elmt) loop
2166 DTW_Id := Node (Elmt);
2167 Next_Elmt (Elmt);
2169 Merge_Class_Conditions (DTW_Id);
2170 Make_Class_Precondition_Subps (DTW_Id, Late_Overriding);
2172 CW_Subp := Static_Call_Helper (DTW_Id);
2173 Decl_N := Unit_Declaration_Node (CW_Subp);
2174 Analyze (Decl_N);
2176 -- If the DTW was built for a late-overriding primitive
2177 -- its body must be analyzed now (since the tagged type
2178 -- is already frozen).
2180 if Late_Overriding then
2181 Body_N :=
2182 Unit_Declaration_Node (Corresponding_Body (Decl_N));
2183 Analyze (Body_N);
2184 end if;
2185 end loop;
2186 end;
2187 end if;
2188 end Check_Inherited_Conditions;
2190 ----------------------------
2191 -- Check_Strict_Alignment --
2192 ----------------------------
2194 procedure Check_Strict_Alignment (E : Entity_Id) is
2195 Comp : Entity_Id;
2197 begin
2198 -- Bit-packed array types do not require strict alignment, even if they
2199 -- are by-reference types, because they are accessed in a special way.
2201 if Is_By_Reference_Type (E) and then not Is_Bit_Packed_Array (E) then
2202 Set_Strict_Alignment (E);
2204 elsif Is_Array_Type (E) then
2205 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
2207 -- ??? AI12-001: Any component of a packed type that contains an
2208 -- aliased part must be aligned according to the alignment of its
2209 -- subtype (RM 13.2(7)). This means that the following test:
2211 -- if Has_Aliased_Components (E) then
2212 -- Set_Strict_Alignment (E);
2213 -- end if;
2215 -- should be implemented here. Unfortunately it would break Florist,
2216 -- which has the bad habit of overaligning all the types it declares
2217 -- on 32-bit platforms. Other legacy codebases could also be affected
2218 -- because this check has historically been missing in GNAT.
2220 elsif Is_Record_Type (E) then
2221 Comp := First_Component (E);
2222 while Present (Comp) loop
2223 if not Is_Type (Comp)
2224 and then (Is_Aliased (Comp)
2225 or else Strict_Alignment (Etype (Comp)))
2226 then
2227 Set_Strict_Alignment (E);
2228 return;
2229 end if;
2231 Next_Component (Comp);
2232 end loop;
2233 end if;
2234 end Check_Strict_Alignment;
2236 -------------------------
2237 -- Check_Unsigned_Type --
2238 -------------------------
2240 procedure Check_Unsigned_Type (E : Entity_Id) is
2241 Ancestor : Entity_Id;
2242 Lo_Bound : Node_Id;
2243 Btyp : Entity_Id;
2245 begin
2246 if not Is_Discrete_Or_Fixed_Point_Type (E) then
2247 return;
2248 end if;
2250 -- Do not attempt to analyze case where range was in error
2252 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
2253 return;
2254 end if;
2256 -- The situation that is nontrivial is something like:
2258 -- subtype x1 is integer range -10 .. +10;
2259 -- subtype x2 is x1 range 0 .. V1;
2260 -- subtype x3 is x2 range V2 .. V3;
2261 -- subtype x4 is x3 range V4 .. V5;
2263 -- where Vn are variables. Here the base type is signed, but we still
2264 -- know that x4 is unsigned because of the lower bound of x2.
2266 -- The only way to deal with this is to look up the ancestor chain
2268 Ancestor := E;
2269 loop
2270 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
2271 return;
2272 end if;
2274 Lo_Bound := Type_Low_Bound (Ancestor);
2276 if Compile_Time_Known_Value (Lo_Bound) then
2277 if Expr_Rep_Value (Lo_Bound) >= 0 then
2278 Set_Is_Unsigned_Type (E, True);
2279 end if;
2281 return;
2283 else
2284 Ancestor := Ancestor_Subtype (Ancestor);
2286 -- If no ancestor had a static lower bound, go to base type
2288 if No (Ancestor) then
2290 -- Note: the reason we still check for a compile time known
2291 -- value for the base type is that at least in the case of
2292 -- generic formals, we can have bounds that fail this test,
2293 -- and there may be other cases in error situations.
2295 Btyp := Base_Type (E);
2297 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
2298 return;
2299 end if;
2301 Lo_Bound := Type_Low_Bound (Base_Type (E));
2303 if Compile_Time_Known_Value (Lo_Bound)
2304 and then Expr_Rep_Value (Lo_Bound) >= 0
2305 then
2306 Set_Is_Unsigned_Type (E, True);
2307 end if;
2309 return;
2310 end if;
2311 end if;
2312 end loop;
2313 end Check_Unsigned_Type;
2315 -----------------------------------------------
2316 -- Explode_Initialization_Compound_Statement --
2317 -----------------------------------------------
2319 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
2320 Init_Stmts : constant Node_Id := Initialization_Statements (E);
2322 begin
2323 if Present (Init_Stmts)
2324 and then Nkind (Init_Stmts) = N_Compound_Statement
2325 then
2326 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
2328 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
2329 -- just removing it, because Freeze_All may rely on this particular
2330 -- Node_Id still being present in the enclosing list to know where to
2331 -- stop freezing.
2333 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
2335 Set_Initialization_Statements (E, Empty);
2336 end if;
2337 end Explode_Initialization_Compound_Statement;
2339 ----------------
2340 -- Freeze_All --
2341 ----------------
2343 -- Note: the easy coding for this procedure would be to just build a
2344 -- single list of freeze nodes and then insert them and analyze them
2345 -- all at once. This won't work, because the analysis of earlier freeze
2346 -- nodes may recursively freeze types which would otherwise appear later
2347 -- on in the freeze list. So we must analyze and expand the freeze nodes
2348 -- as they are generated.
2350 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
2351 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
2352 -- This is the internal recursive routine that does freezing of entities
2353 -- (but NOT the analysis of default expressions, which should not be
2354 -- recursive, we don't want to analyze those till we are sure that ALL
2355 -- the types are frozen).
2357 --------------------
2358 -- Freeze_All_Ent --
2359 --------------------
2361 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
2362 E : Entity_Id;
2363 Flist : List_Id;
2365 procedure Process_Flist;
2366 -- If freeze nodes are present, insert and analyze, and reset cursor
2367 -- for next insertion.
2369 -------------------
2370 -- Process_Flist --
2371 -------------------
2373 procedure Process_Flist is
2374 Lastn : Node_Id;
2375 begin
2376 if Is_Non_Empty_List (Flist) then
2377 Lastn := Next (After);
2378 Insert_List_After_And_Analyze (After, Flist);
2380 if Present (Lastn) then
2381 After := Prev (Lastn);
2382 else
2383 After := Last (List_Containing (After));
2384 end if;
2385 end if;
2386 end Process_Flist;
2388 -- Start of processing for Freeze_All_Ent
2390 begin
2391 E := From;
2392 while Present (E) loop
2394 -- If the entity is an inner package which is not a package
2395 -- renaming, then its entities must be frozen at this point. Note
2396 -- that such entities do NOT get frozen at the end of the nested
2397 -- package itself (only library packages freeze).
2399 -- Same is true for task declarations, where anonymous records
2400 -- created for entry parameters must be frozen.
2402 if Ekind (E) = E_Package
2403 and then No (Renamed_Entity (E))
2404 and then not Is_Child_Unit (E)
2405 and then not Is_Frozen (E)
2406 then
2407 Push_Scope (E);
2409 Install_Visible_Declarations (E);
2410 Install_Private_Declarations (E);
2411 Freeze_All (First_Entity (E), After);
2413 End_Package_Scope (E);
2415 if Is_Generic_Instance (E)
2416 and then Has_Delayed_Freeze (E)
2417 then
2418 Set_Has_Delayed_Freeze (E, False);
2419 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
2420 end if;
2422 elsif Ekind (E) in Task_Kind
2423 and then Nkind (Parent (E)) in
2424 N_Single_Task_Declaration | N_Task_Type_Declaration
2425 then
2426 Push_Scope (E);
2427 Freeze_All (First_Entity (E), After);
2428 End_Scope;
2430 -- For a derived tagged type, we must ensure that all the
2431 -- primitive operations of the parent have been frozen, so that
2432 -- their addresses will be in the parent's dispatch table at the
2433 -- point it is inherited.
2435 elsif Ekind (E) = E_Record_Type
2436 and then Is_Tagged_Type (E)
2437 and then Is_Tagged_Type (Etype (E))
2438 and then Is_Derived_Type (E)
2439 then
2440 declare
2441 Prim_List : constant Elist_Id :=
2442 Primitive_Operations (Etype (E));
2444 Prim : Elmt_Id;
2445 Subp : Entity_Id;
2447 begin
2448 Prim := First_Elmt (Prim_List);
2449 while Present (Prim) loop
2450 Subp := Node (Prim);
2452 if Comes_From_Source (Subp)
2453 and then not Is_Frozen (Subp)
2454 then
2455 Flist := Freeze_Entity (Subp, After);
2456 Process_Flist;
2457 end if;
2459 Next_Elmt (Prim);
2460 end loop;
2461 end;
2462 end if;
2464 if not Is_Frozen (E) then
2465 Flist := Freeze_Entity (E, After);
2466 Process_Flist;
2468 -- If already frozen, and there are delayed aspects, this is where
2469 -- we do the visibility check for these aspects (see Sem_Ch13 spec
2470 -- for a description of how we handle aspect visibility).
2472 elsif Has_Delayed_Aspects (E) then
2473 declare
2474 Ritem : Node_Id;
2476 begin
2477 Ritem := First_Rep_Item (E);
2478 while Present (Ritem) loop
2479 if Nkind (Ritem) = N_Aspect_Specification
2480 and then Entity (Ritem) = E
2481 and then Is_Delayed_Aspect (Ritem)
2482 then
2483 Check_Aspect_At_End_Of_Declarations (Ritem);
2484 end if;
2486 Next_Rep_Item (Ritem);
2487 end loop;
2488 end;
2489 end if;
2491 -- If an incomplete type is still not frozen, this may be a
2492 -- premature freezing because of a body declaration that follows.
2493 -- Indicate where the freezing took place. Freezing will happen
2494 -- if the body comes from source, but not if it is internally
2495 -- generated, for example as the body of a type invariant.
2497 -- If the freezing is caused by the end of the current declarative
2498 -- part, it is a Taft Amendment type, and there is no error.
2500 if not Is_Frozen (E)
2501 and then Ekind (E) = E_Incomplete_Type
2502 then
2503 declare
2504 Bod : constant Node_Id := Next (After);
2506 begin
2507 -- The presence of a body freezes all entities previously
2508 -- declared in the current list of declarations, but this
2509 -- does not apply if the body does not come from source.
2510 -- A type invariant is transformed into a subprogram body
2511 -- which is placed at the end of the private part of the
2512 -- current package, but this body does not freeze incomplete
2513 -- types that may be declared in this private part.
2515 if Comes_From_Source (Bod)
2516 and then Nkind (Bod) in N_Entry_Body
2517 | N_Package_Body
2518 | N_Protected_Body
2519 | N_Subprogram_Body
2520 | N_Task_Body
2521 | N_Body_Stub
2522 and then
2523 In_Same_List (After, Parent (E))
2524 then
2525 Error_Msg_Sloc := Sloc (Next (After));
2526 Error_Msg_NE
2527 ("type& is frozen# before its full declaration",
2528 Parent (E), E);
2529 end if;
2530 end;
2531 end if;
2533 Next_Entity (E);
2534 end loop;
2535 end Freeze_All_Ent;
2537 -- Local variables
2539 Decl : Node_Id;
2540 E : Entity_Id;
2541 Item : Entity_Id;
2543 -- Start of processing for Freeze_All
2545 begin
2546 Freeze_All_Ent (From, After);
2548 -- Now that all types are frozen, we can deal with default expressions
2549 -- that require us to build a default expression functions. This is the
2550 -- point at which such functions are constructed (after all types that
2551 -- might be used in such expressions have been frozen).
2553 -- For subprograms that are renaming_as_body, we create the wrapper
2554 -- bodies as needed.
2556 -- We also add finalization chains to access types whose designated
2557 -- types are controlled. This is normally done when freezing the type,
2558 -- but this misses recursive type definitions where the later members
2559 -- of the recursion introduce controlled components.
2561 -- Loop through entities
2563 E := From;
2564 while Present (E) loop
2565 if Is_Subprogram (E) then
2566 if not Default_Expressions_Processed (E) then
2567 Process_Default_Expressions (E, After);
2568 end if;
2570 -- Check subprogram renamings for the same strub-mode.
2571 -- Avoid rechecking dispatching operations, that's taken
2572 -- care of in Check_Inherited_Conditions, that covers
2573 -- inherited interface operations.
2575 Item := Alias (E);
2576 if Present (Item)
2577 and then not Is_Dispatching_Operation (E)
2578 then
2579 Check_Same_Strub_Mode (E, Item);
2580 end if;
2582 if not Has_Completion (E) then
2583 Decl := Unit_Declaration_Node (E);
2585 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
2586 if Error_Posted (Decl) then
2587 Set_Has_Completion (E);
2588 else
2589 Build_And_Analyze_Renamed_Body (Decl, E, After);
2590 end if;
2592 elsif Nkind (Decl) = N_Subprogram_Declaration
2593 and then Present (Corresponding_Body (Decl))
2594 and then
2595 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl))) =
2596 N_Subprogram_Renaming_Declaration
2597 then
2598 Build_And_Analyze_Renamed_Body
2599 (Decl, Corresponding_Body (Decl), After);
2600 end if;
2601 end if;
2603 -- Freeze the default expressions of entries, entry families, and
2604 -- protected subprograms.
2606 elsif Is_Concurrent_Type (E) then
2607 Item := First_Entity (E);
2608 while Present (Item) loop
2609 if Is_Subprogram_Or_Entry (Item)
2610 and then not Default_Expressions_Processed (Item)
2611 then
2612 Process_Default_Expressions (Item, After);
2613 end if;
2615 Next_Entity (Item);
2616 end loop;
2617 end if;
2619 -- Historical note: We used to create a finalization master for an
2620 -- access type whose designated type is not controlled, but contains
2621 -- private controlled compoments. This form of postprocessing is no
2622 -- longer needed because the finalization master is now created when
2623 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
2625 Next_Entity (E);
2626 end loop;
2627 end Freeze_All;
2629 -----------------------
2630 -- Freeze_And_Append --
2631 -----------------------
2633 procedure Freeze_And_Append
2634 (Ent : Entity_Id;
2635 N : Node_Id;
2636 Result : in out List_Id)
2638 -- Freezing an Expression_Function does not freeze its profile:
2639 -- the formals will have been frozen otherwise before the E_F
2640 -- can be called.
2642 L : constant List_Id :=
2643 Freeze_Entity
2644 (Ent, N, Do_Freeze_Profile => not Is_Expression_Function (Ent));
2645 begin
2646 if Is_Non_Empty_List (L) then
2647 if Result = No_List then
2648 Result := L;
2649 else
2650 Append_List (L, Result);
2651 end if;
2652 end if;
2653 end Freeze_And_Append;
2655 -------------------
2656 -- Freeze_Before --
2657 -------------------
2659 procedure Freeze_Before
2660 (N : Node_Id;
2661 T : Entity_Id;
2662 Do_Freeze_Profile : Boolean := True)
2664 -- Freeze T, then insert the generated Freeze nodes before the node N.
2665 -- Flag Freeze_Profile is used when T is an overloadable entity, and
2666 -- indicates whether its profile should be frozen at the same time.
2668 Freeze_Nodes : constant List_Id :=
2669 Freeze_Entity (T, N, Do_Freeze_Profile);
2670 Pack : constant Entity_Id := Scope (T);
2672 begin
2673 if Ekind (T) = E_Function then
2674 Check_Expression_Function (N, T);
2675 end if;
2677 if Is_Non_Empty_List (Freeze_Nodes) then
2679 -- If the entity is a type declared in an inner package, it may be
2680 -- frozen by an outer declaration before the package itself is
2681 -- frozen. Install the package scope to analyze the freeze nodes,
2682 -- which may include generated subprograms such as predicate
2683 -- functions, etc.
2685 if Is_Type (T) and then From_Nested_Package (T) then
2686 Push_Scope (Pack);
2687 Install_Visible_Declarations (Pack);
2688 Install_Private_Declarations (Pack);
2689 Insert_Actions (N, Freeze_Nodes);
2690 End_Package_Scope (Pack);
2692 else
2693 Insert_Actions (N, Freeze_Nodes);
2694 end if;
2695 end if;
2696 end Freeze_Before;
2698 -------------------
2699 -- Freeze_Entity --
2700 -------------------
2702 -- WARNING: This routine manages Ghost regions. Return statements must be
2703 -- replaced by gotos which jump to the end of the routine and restore the
2704 -- Ghost mode.
2706 function Freeze_Entity
2707 (E : Entity_Id;
2708 N : Node_Id;
2709 Do_Freeze_Profile : Boolean := True) return List_Id
2711 Loc : constant Source_Ptr := Sloc (N);
2713 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2714 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2715 -- Save the Ghost-related attributes to restore on exit
2717 Atype : Entity_Id;
2718 Comp : Entity_Id;
2719 F_Node : Node_Id;
2720 Formal : Entity_Id;
2721 Indx : Node_Id;
2723 Result : List_Id := No_List;
2724 -- List of freezing actions, left at No_List if none
2726 Test_E : Entity_Id := E;
2727 -- A local temporary used to test if freezing is necessary for E, since
2728 -- its value can be set to something other than E in certain cases. For
2729 -- example, E cannot be used directly in cases such as when it is an
2730 -- Itype defined within a record - since it is the location of record
2731 -- which matters.
2733 procedure Add_To_Result (Fnod : Node_Id);
2734 -- Add freeze action Fnod to list Result
2736 function After_Last_Declaration return Boolean;
2737 -- If Loc is a freeze_entity that appears after the last declaration
2738 -- in the scope, inhibit error messages on late completion.
2740 procedure Check_Current_Instance (Comp_Decl : Node_Id);
2741 -- Check that an Access or Unchecked_Access attribute with a prefix
2742 -- which is the current instance type can only be applied when the type
2743 -- is limited.
2745 procedure Check_No_Parts_Violations
2746 (Typ : Entity_Id; Aspect_No_Parts : Aspect_Id) with
2747 Pre => Aspect_No_Parts in
2748 Aspect_No_Controlled_Parts | Aspect_No_Task_Parts;
2749 -- Check that Typ does not violate the semantics of the specified
2750 -- Aspect_No_Parts (No_Controlled_Parts or No_Task_Parts) when it is
2751 -- specified on Typ or one of its ancestors.
2753 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id);
2754 -- Give a warning for pragma Convention with language C or C++ applied
2755 -- to a discriminated record type. This is suppressed for the unchecked
2756 -- union case, since the whole point in this case is interface C. We
2757 -- also do not generate this within instantiations, since we will have
2758 -- generated a message on the template.
2760 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
2761 -- Give warning for modulus of 8, 16, 32, 64 or 128 given as an explicit
2762 -- integer literal without an explicit corresponding size clause. The
2763 -- caller has checked that Utype is a modular integer type.
2765 procedure Freeze_Array_Type (Arr : Entity_Id);
2766 -- Freeze array type, including freezing index and component types
2768 procedure Freeze_Object_Declaration (E : Entity_Id);
2769 -- Perform checks and generate freeze node if needed for a constant or
2770 -- variable declared by an object declaration.
2772 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
2773 -- Create Freeze_Generic_Entity nodes for types declared in a generic
2774 -- package. Recurse on inner generic packages.
2776 function Freeze_Profile (E : Entity_Id) return Boolean;
2777 -- Freeze formals and return type of subprogram. If some type in the
2778 -- profile is incomplete and we are in an instance, freezing of the
2779 -- entity will take place elsewhere, and the function returns False.
2781 procedure Freeze_Record_Type (Rec : Entity_Id);
2782 -- Freeze record type, including freezing component types, and freezing
2783 -- primitive operations if this is a tagged type.
2785 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2786 -- Determine whether an arbitrary entity is subject to Boolean aspect
2787 -- Import and its value is specified as True.
2789 procedure Inherit_Freeze_Node
2790 (Fnod : Node_Id;
2791 Typ : Entity_Id);
2792 -- Set type Typ's freeze node to refer to Fnode. This routine ensures
2793 -- that any attributes attached to Typ's original node are preserved.
2795 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2796 -- If E is an entity for an imported subprogram with pre/post-conditions
2797 -- then this procedure will create a wrapper to ensure that proper run-
2798 -- time checking of the pre/postconditions. See body for details.
2800 -------------------
2801 -- Add_To_Result --
2802 -------------------
2804 procedure Add_To_Result (Fnod : Node_Id) is
2805 begin
2806 Append_New_To (Result, Fnod);
2807 end Add_To_Result;
2809 ----------------------------
2810 -- After_Last_Declaration --
2811 ----------------------------
2813 function After_Last_Declaration return Boolean is
2814 Spec : constant Node_Id := Parent (Current_Scope);
2816 begin
2817 if Nkind (Spec) = N_Package_Specification then
2818 if Present (Private_Declarations (Spec)) then
2819 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2820 elsif Present (Visible_Declarations (Spec)) then
2821 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2822 else
2823 return False;
2824 end if;
2826 else
2827 return False;
2828 end if;
2829 end After_Last_Declaration;
2831 ----------------------------
2832 -- Check_Current_Instance --
2833 ----------------------------
2835 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2837 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2838 -- Determine whether Typ is compatible with the rules for aliased
2839 -- views of types as defined in RM 3.10 in the various dialects.
2841 function Process (N : Node_Id) return Traverse_Result;
2842 -- Process routine to apply check to given node
2844 -----------------------------
2845 -- Is_Aliased_View_Of_Type --
2846 -----------------------------
2848 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2849 Typ_Decl : constant Node_Id := Parent (Typ);
2851 begin
2852 -- Common case
2854 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2855 and then Limited_Present (Type_Definition (Typ_Decl))
2856 then
2857 return True;
2859 -- The following paragraphs describe what a legal aliased view of
2860 -- a type is in the various dialects of Ada.
2862 -- Ada 95
2864 -- The current instance of a limited type, and a formal parameter
2865 -- or generic formal object of a tagged type.
2867 -- Ada 95 limited type
2868 -- * Type with reserved word "limited"
2869 -- * A protected or task type
2870 -- * A composite type with limited component
2872 elsif Ada_Version <= Ada_95 then
2873 return Is_Limited_Type (Typ);
2875 -- Ada 2005
2877 -- The current instance of a limited tagged type, a protected
2878 -- type, a task type, or a type that has the reserved word
2879 -- "limited" in its full definition ... a formal parameter or
2880 -- generic formal object of a tagged type.
2882 -- Ada 2005 limited type
2883 -- * Type with reserved word "limited", "synchronized", "task"
2884 -- or "protected"
2885 -- * A composite type with limited component
2886 -- * A derived type whose parent is a non-interface limited type
2888 elsif Ada_Version = Ada_2005 then
2889 return
2890 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2891 or else
2892 (Is_Derived_Type (Typ)
2893 and then not Is_Interface (Etype (Typ))
2894 and then Is_Limited_Type (Etype (Typ)));
2896 -- Ada 2012 and beyond
2898 -- The current instance of an immutably limited type ... a formal
2899 -- parameter or generic formal object of a tagged type.
2901 -- Ada 2012 limited type
2902 -- * Type with reserved word "limited", "synchronized", "task"
2903 -- or "protected"
2904 -- * A composite type with limited component
2905 -- * A derived type whose parent is a non-interface limited type
2906 -- * An incomplete view
2908 -- Ada 2012 immutably limited type
2909 -- * Explicitly limited record type
2910 -- * Record extension with "limited" present
2911 -- * Non-formal limited private type that is either tagged
2912 -- or has at least one access discriminant with a default
2913 -- expression
2914 -- * Task type, protected type or synchronized interface
2915 -- * Type derived from immutably limited type
2917 else
2918 return
2919 Is_Immutably_Limited_Type (Typ)
2920 or else Is_Incomplete_Type (Typ);
2921 end if;
2922 end Is_Aliased_View_Of_Type;
2924 -------------
2925 -- Process --
2926 -------------
2928 function Process (N : Node_Id) return Traverse_Result is
2929 begin
2930 case Nkind (N) is
2931 when N_Attribute_Reference =>
2932 if Attribute_Name (N) in Name_Access | Name_Unchecked_Access
2933 and then Is_Entity_Name (Prefix (N))
2934 and then Is_Type (Entity (Prefix (N)))
2935 and then Entity (Prefix (N)) = E
2936 then
2937 if Ada_Version < Ada_2012 then
2938 Error_Msg_N
2939 ("current instance must be a limited type",
2940 Prefix (N));
2941 else
2942 Error_Msg_N
2943 ("current instance must be an immutably limited "
2944 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2945 end if;
2947 return Abandon;
2949 else
2950 return OK;
2951 end if;
2953 when others =>
2954 return OK;
2955 end case;
2956 end Process;
2958 procedure Traverse is new Traverse_Proc (Process);
2960 -- Local variables
2962 Rec_Type : constant Entity_Id :=
2963 Scope (Defining_Identifier (Comp_Decl));
2965 -- Start of processing for Check_Current_Instance
2967 begin
2968 if not Is_Aliased_View_Of_Type (Rec_Type) then
2969 Traverse (Comp_Decl);
2970 end if;
2971 end Check_Current_Instance;
2973 -------------------------------
2974 -- Check_No_Parts_Violations --
2975 -------------------------------
2977 procedure Check_No_Parts_Violations
2978 (Typ : Entity_Id; Aspect_No_Parts : Aspect_Id)
2981 function Find_Aspect_No_Parts
2982 (Typ : Entity_Id) return Node_Id;
2983 -- Search for Aspect_No_Parts on a given type. When
2984 -- the aspect is not explicity specified Empty is returned.
2986 function Get_Aspect_No_Parts_Value
2987 (Typ : Entity_Id) return Entity_Id;
2988 -- Obtain the value for the Aspect_No_Parts on a given
2989 -- type. When the aspect is not explicitly specified Empty is
2990 -- returned.
2992 function Has_Aspect_No_Parts
2993 (Typ : Entity_Id) return Boolean;
2994 -- Predicate function which identifies whether No_Parts
2995 -- is explicitly specified on a given type.
2997 -------------------------------------
2998 -- Find_Aspect_No_Parts --
2999 -------------------------------------
3001 function Find_Aspect_No_Parts
3002 (Typ : Entity_Id) return Node_Id
3004 Partial_View : constant Entity_Id :=
3005 Incomplete_Or_Partial_View (Typ);
3007 Aspect_Spec : Entity_Id :=
3008 Find_Aspect (Typ, Aspect_No_Parts);
3009 Curr_Aspect_Spec : Entity_Id;
3010 begin
3012 -- Examine Typ's associated node, when present, since aspect
3013 -- specifications do not get transferred when nodes get rewritten.
3015 -- For example, this can happen in the expansion of array types
3017 if No (Aspect_Spec)
3018 and then Present (Associated_Node_For_Itype (Typ))
3019 and then Nkind (Associated_Node_For_Itype (Typ))
3020 = N_Full_Type_Declaration
3021 then
3022 Aspect_Spec :=
3023 Find_Aspect
3024 (Id => Defining_Identifier
3025 (Associated_Node_For_Itype (Typ)),
3026 A => Aspect_No_Parts);
3027 end if;
3029 -- Examine aspects specifications on private type declarations
3031 -- Should Find_Aspect be improved to handle this case ???
3033 if No (Aspect_Spec)
3034 and then Present (Partial_View)
3035 and then Present
3036 (Aspect_Specifications
3037 (Declaration_Node
3038 (Partial_View)))
3039 then
3040 Curr_Aspect_Spec :=
3041 First
3042 (Aspect_Specifications
3043 (Declaration_Node
3044 (Partial_View)));
3046 -- Search through aspects present on the private type
3048 while Present (Curr_Aspect_Spec) loop
3049 if Get_Aspect_Id (Curr_Aspect_Spec)
3050 = Aspect_No_Parts
3051 then
3052 Aspect_Spec := Curr_Aspect_Spec;
3053 exit;
3054 end if;
3056 Next (Curr_Aspect_Spec);
3057 end loop;
3059 end if;
3061 -- When errors are posted on the aspect return Empty
3063 if Error_Posted (Aspect_Spec) then
3064 return Empty;
3065 end if;
3067 return Aspect_Spec;
3068 end Find_Aspect_No_Parts;
3070 ------------------------------------------
3071 -- Get_Aspect_No_Parts_Value --
3072 ------------------------------------------
3074 function Get_Aspect_No_Parts_Value
3075 (Typ : Entity_Id) return Entity_Id
3077 Aspect_Spec : constant Entity_Id :=
3078 Find_Aspect_No_Parts (Typ);
3079 begin
3081 -- Return the value of the aspect when present
3083 if Present (Aspect_Spec) then
3085 -- No expression is the same as True
3087 if No (Expression (Aspect_Spec)) then
3088 return Standard_True;
3089 end if;
3091 -- Assume its expression has already been constant folded into
3092 -- a Boolean value and return its value.
3094 return Entity (Expression (Aspect_Spec));
3095 end if;
3097 -- Otherwise, the aspect is not specified - so return Empty
3099 return Empty;
3100 end Get_Aspect_No_Parts_Value;
3102 ------------------------------------
3103 -- Has_Aspect_No_Parts --
3104 ------------------------------------
3106 function Has_Aspect_No_Parts
3107 (Typ : Entity_Id) return Boolean
3108 is (Present (Find_Aspect_No_Parts (Typ)));
3110 -- Generic instances
3112 -------------------------------------------
3113 -- Get_Generic_Formal_Types_In_Hierarchy --
3114 -------------------------------------------
3116 function Get_Generic_Formal_Types_In_Hierarchy
3117 is new Collect_Types_In_Hierarchy (Predicate => Is_Generic_Formal);
3118 -- Return a list of all types within a given type's hierarchy which
3119 -- are generic formals.
3121 ----------------------------------------
3122 -- Get_Types_With_Aspect_In_Hierarchy --
3123 ----------------------------------------
3125 function Get_Types_With_Aspect_In_Hierarchy
3126 is new Collect_Types_In_Hierarchy
3127 (Predicate => Has_Aspect_No_Parts);
3128 -- Returns a list of all types within a given type's hierarchy which
3129 -- have the Aspect_No_Parts specified.
3131 -- Local declarations
3133 Aspect_Value : Entity_Id;
3134 Curr_Value : Entity_Id;
3135 Curr_Typ_Elmt : Elmt_Id;
3136 Curr_Body_Elmt : Elmt_Id;
3137 Curr_Formal_Elmt : Elmt_Id;
3138 Gen_Bodies : Elist_Id;
3139 Gen_Formals : Elist_Id;
3140 Scop : Entity_Id;
3141 Types_With_Aspect : Elist_Id;
3143 -- Start of processing for Check_No_Parts_Violations
3145 begin
3146 -- Nothing to check if the type is elementary or artificial
3148 if Is_Elementary_Type (Typ) or else not Comes_From_Source (Typ) then
3149 return;
3150 end if;
3152 Types_With_Aspect := Get_Types_With_Aspect_In_Hierarchy (Typ);
3154 -- Nothing to check if there are no types with No_Parts specified
3156 if Is_Empty_Elmt_List (Types_With_Aspect) then
3157 return;
3158 end if;
3160 -- Set name for all errors below
3162 Error_Msg_Name_1 := Aspect_Names (Aspect_No_Parts);
3164 -- Obtain the aspect value for No_Parts for comparison
3166 Aspect_Value :=
3167 Get_Aspect_No_Parts_Value
3168 (Node (First_Elmt (Types_With_Aspect)));
3170 -- When the value is True and there are controlled/task parts or the
3171 -- type itself is controlled/task, trigger the appropriate error.
3173 if Aspect_Value = Standard_True then
3174 if Aspect_No_Parts = Aspect_No_Controlled_Parts then
3175 if Is_Controlled (Typ) or else Has_Controlled_Component (Typ)
3176 then
3177 Error_Msg_N
3178 ("aspect % applied to controlled type &", Typ);
3179 end if;
3181 elsif Aspect_No_Parts = Aspect_No_Task_Parts then
3182 if Has_Task (Typ) then
3183 Error_Msg_N
3184 ("aspect % applied to task type &", Typ);
3185 Error_Msg_N
3186 ("\replace task components with access-to-task-type "
3187 & "components??", Typ);
3188 end if;
3190 else
3191 raise Program_Error;
3192 end if;
3193 end if;
3195 -- Move through Types_With_Aspect - checking that the value specified
3196 -- for their corresponding Aspect_No_Parts do not override each
3197 -- other.
3199 Curr_Typ_Elmt := First_Elmt (Types_With_Aspect);
3200 while Present (Curr_Typ_Elmt) loop
3201 Curr_Value :=
3202 Get_Aspect_No_Parts_Value (Node (Curr_Typ_Elmt));
3204 -- Compare the aspect value against the current type
3206 if Curr_Value /= Aspect_Value then
3207 Error_Msg_NE
3208 ("cannot override aspect % of "
3209 & "ancestor type &", Typ, Node (Curr_Typ_Elmt));
3210 return;
3211 end if;
3213 Next_Elmt (Curr_Typ_Elmt);
3214 end loop;
3216 -- Issue an error if the aspect applies to a type declared inside a
3217 -- generic body and if said type derives from or has a component
3218 -- of ageneric formal type - since those are considered to have
3219 -- controlled/task parts and have Aspect_No_Parts specified as
3220 -- False by default (RM H.4.1(4/5) is about the language-defined
3221 -- No_Controlled_Parts aspect, and we are using the same rules for
3222 -- No_Task_Parts).
3224 -- We do not check tagged types since deriving from a formal type
3225 -- within an enclosing generic unit is already illegal
3226 -- (RM 3.9.1 (4/2)).
3228 if Aspect_Value = Standard_True
3229 and then In_Generic_Body (Typ)
3230 and then not Is_Tagged_Type (Typ)
3231 then
3232 Gen_Bodies := New_Elmt_List;
3233 Gen_Formals :=
3234 Get_Generic_Formal_Types_In_Hierarchy
3235 (Typ => Typ,
3236 Examine_Components => True);
3238 -- Climb scopes collecting generic bodies
3240 Scop := Scope (Typ);
3241 while Present (Scop) and then Scop /= Standard_Standard loop
3243 -- Generic package body
3245 if Ekind (Scop) = E_Generic_Package
3246 and then In_Package_Body (Scop)
3247 then
3248 Append_Elmt (Scop, Gen_Bodies);
3250 -- Generic subprogram body
3252 elsif Is_Generic_Subprogram (Scop) then
3253 Append_Elmt (Scop, Gen_Bodies);
3254 end if;
3256 Scop := Scope (Scop);
3257 end loop;
3259 -- Warn about the improper use of Aspect_No_Parts on a type
3260 -- declaration deriving from or that has a component of a generic
3261 -- formal type within the formal type's corresponding generic
3262 -- body by moving through all formal types in Typ's hierarchy and
3263 -- checking if they are formals in any of the enclosing generic
3264 -- bodies.
3266 -- However, a special exception gets made for formal types which
3267 -- derive from a type which has Aspect_No_Parts True.
3269 -- For example:
3271 -- generic
3272 -- type Form is private;
3273 -- package G is
3274 -- type Type_A is new Form with No_Controlled_Parts; -- OK
3275 -- end;
3277 -- package body G is
3278 -- type Type_B is new Form with No_Controlled_Parts; -- ERROR
3279 -- end;
3281 -- generic
3282 -- type Form is private;
3283 -- package G is
3284 -- type Type_A is record C : Form; end record
3285 -- with No_Controlled_Parts; -- OK
3286 -- end;
3288 -- package body G is
3289 -- type Type_B is record C : Form; end record
3290 -- with No_Controlled_Parts; -- ERROR
3291 -- end;
3293 -- type Root is tagged null record with No_Controlled_Parts;
3295 -- generic
3296 -- type Form is new Root with private;
3297 -- package G is
3298 -- type Type_A is record C : Form; end record
3299 -- with No_Controlled_Parts; -- OK
3300 -- end;
3302 -- package body G is
3303 -- type Type_B is record C : Form; end record
3304 -- with No_Controlled_Parts; -- OK
3305 -- end;
3307 Curr_Formal_Elmt := First_Elmt (Gen_Formals);
3308 while Present (Curr_Formal_Elmt) loop
3310 Curr_Body_Elmt := First_Elmt (Gen_Bodies);
3311 while Present (Curr_Body_Elmt) loop
3313 -- Obtain types in the formal type's hierarchy which have
3314 -- the aspect specified.
3316 Types_With_Aspect :=
3317 Get_Types_With_Aspect_In_Hierarchy
3318 (Node (Curr_Formal_Elmt));
3320 -- We found a type declaration in a generic body where both
3321 -- Aspect_No_Parts is true and one of its ancestors is a
3322 -- generic formal type.
3324 if Scope (Node (Curr_Formal_Elmt)) =
3325 Node (Curr_Body_Elmt)
3327 -- Check that no ancestors of the formal type have
3328 -- Aspect_No_Parts True before issuing the error.
3330 and then (Is_Empty_Elmt_List (Types_With_Aspect)
3331 or else
3332 Get_Aspect_No_Parts_Value
3333 (Node (First_Elmt (Types_With_Aspect)))
3334 = Standard_False)
3335 then
3336 Error_Msg_Node_1 := Typ;
3337 Error_Msg_Node_2 := Node (Curr_Formal_Elmt);
3338 Error_Msg
3339 ("aspect % cannot be applied to "
3340 & "type & which has an ancestor or component of "
3341 & "formal type & within the formal type's "
3342 & "corresponding generic body", Sloc (Typ));
3343 end if;
3345 Next_Elmt (Curr_Body_Elmt);
3346 end loop;
3348 Next_Elmt (Curr_Formal_Elmt);
3349 end loop;
3350 end if;
3351 end Check_No_Parts_Violations;
3353 ---------------------------------
3354 -- Check_Suspicious_Convention --
3355 ---------------------------------
3357 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id) is
3358 begin
3359 if Has_Discriminants (Rec_Type)
3360 and then Is_Base_Type (Rec_Type)
3361 and then not Is_Unchecked_Union (Rec_Type)
3362 and then (Convention (Rec_Type) = Convention_C
3363 or else
3364 Convention (Rec_Type) = Convention_CPP)
3365 and then Comes_From_Source (Rec_Type)
3366 and then not In_Instance
3367 and then not Has_Warnings_Off (Rec_Type)
3368 then
3369 declare
3370 Cprag : constant Node_Id :=
3371 Get_Rep_Pragma (Rec_Type, Name_Convention);
3372 A2 : Node_Id;
3374 begin
3375 if Present (Cprag) then
3376 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
3378 if Convention (Rec_Type) = Convention_C then
3379 Error_Msg_N
3380 ("?x?discriminated record has no direct equivalent in "
3381 & "C", A2);
3382 else
3383 Error_Msg_N
3384 ("?x?discriminated record has no direct equivalent in "
3385 & "C++", A2);
3386 end if;
3388 Error_Msg_NE
3389 ("\?x?use of convention for type& is dubious",
3390 A2, Rec_Type);
3391 end if;
3392 end;
3393 end if;
3394 end Check_Suspicious_Convention;
3396 ------------------------------
3397 -- Check_Suspicious_Modulus --
3398 ------------------------------
3400 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
3401 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
3403 begin
3404 if not Warn_On_Suspicious_Modulus_Value then
3405 return;
3406 end if;
3408 if Nkind (Decl) = N_Full_Type_Declaration then
3409 declare
3410 Tdef : constant Node_Id := Type_Definition (Decl);
3412 begin
3413 if Nkind (Tdef) = N_Modular_Type_Definition then
3414 declare
3415 Modulus : constant Node_Id :=
3416 Original_Node (Expression (Tdef));
3418 begin
3419 if Nkind (Modulus) = N_Integer_Literal then
3420 declare
3421 Modv : constant Uint := Intval (Modulus);
3422 Sizv : constant Uint := RM_Size (Utype);
3424 begin
3425 -- First case, modulus and size are the same. This
3426 -- happens if you have something like mod 32, with
3427 -- an explicit size of 32, this is for sure a case
3428 -- where the warning is given, since it is seems
3429 -- very unlikely that someone would want e.g. a
3430 -- five bit type stored in 32 bits. It is much
3431 -- more likely they wanted a 32-bit type.
3433 if Modv = Sizv then
3434 null;
3436 -- Second case, the modulus is 32 or 64 and no
3437 -- size clause is present. This is a less clear
3438 -- case for giving the warning, but in the case
3439 -- of 32/64 (5-bit or 6-bit types) these seem rare
3440 -- enough that it is a likely error (and in any
3441 -- case using 2**5 or 2**6 in these cases seems
3442 -- clearer. We don't include 8 or 16 here, simply
3443 -- because in practice 3-bit and 4-bit types are
3444 -- more common and too many false positives if
3445 -- we warn in these cases.
3447 elsif not Has_Size_Clause (Utype)
3448 and then (Modv = Uint_32 or else Modv = Uint_64)
3449 then
3450 null;
3452 -- No warning needed
3454 else
3455 return;
3456 end if;
3458 -- If we fall through, give warning
3460 Error_Msg_Uint_1 := Modv;
3461 Error_Msg_N
3462 ("?.m?2 '*'*^' may have been intended here",
3463 Modulus);
3464 end;
3465 end if;
3466 end;
3467 end if;
3468 end;
3469 end if;
3470 end Check_Suspicious_Modulus;
3472 -----------------------
3473 -- Freeze_Array_Type --
3474 -----------------------
3476 procedure Freeze_Array_Type (Arr : Entity_Id) is
3477 FS : constant Entity_Id := First_Subtype (Arr);
3478 Ctyp : constant Entity_Id := Component_Type (Arr);
3479 Clause : Entity_Id;
3481 Non_Standard_Enum : Boolean := False;
3482 -- Set true if any of the index types is an enumeration type with a
3483 -- non-standard representation.
3485 begin
3486 Freeze_And_Append (Ctyp, N, Result);
3488 Indx := First_Index (Arr);
3489 while Present (Indx) loop
3490 Freeze_And_Append (Etype (Indx), N, Result);
3492 if Is_Enumeration_Type (Etype (Indx))
3493 and then Has_Non_Standard_Rep (Etype (Indx))
3494 then
3495 Non_Standard_Enum := True;
3496 end if;
3498 Next_Index (Indx);
3499 end loop;
3501 -- Processing that is done only for base types
3503 if Ekind (Arr) = E_Array_Type then
3505 -- Deal with default setting of reverse storage order
3507 Set_SSO_From_Default (Arr);
3509 -- Propagate flags for component type
3511 if Is_Controlled (Ctyp)
3512 or else Has_Controlled_Component (Ctyp)
3513 then
3514 Set_Has_Controlled_Component (Arr);
3515 end if;
3517 if Has_Unchecked_Union (Ctyp) then
3518 Set_Has_Unchecked_Union (Arr);
3519 end if;
3521 -- The array type requires its own invariant procedure in order to
3522 -- verify the component invariant over all elements. In GNATprove
3523 -- mode, the component invariants are checked by other means. They
3524 -- should not be added to the array type invariant procedure, so
3525 -- that the procedure can be used to check the array type
3526 -- invariants if any.
3528 if Has_Invariants (Ctyp)
3529 and then not GNATprove_Mode
3530 then
3531 Set_Has_Own_Invariants (Arr);
3532 end if;
3534 -- Warn for pragma Pack overriding foreign convention
3536 if Has_Foreign_Convention (Ctyp)
3537 and then Has_Pragma_Pack (Arr)
3538 then
3539 declare
3540 CN : constant Name_Id :=
3541 Get_Convention_Name (Convention (Ctyp));
3542 PP : constant Node_Id :=
3543 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
3544 begin
3545 if Present (PP) then
3546 Error_Msg_Name_1 := CN;
3547 Error_Msg_Sloc := Sloc (Arr);
3548 Error_Msg_N
3549 ("pragma Pack affects convention % components #??", PP);
3550 Error_Msg_Name_1 := CN;
3551 Error_Msg_N
3552 ("\array components may not have % compatible "
3553 & "representation??", PP);
3554 end if;
3555 end;
3556 end if;
3558 -- Check for Aliased or Atomic_Components or Full Access with
3559 -- unsuitable packing or explicit component size clause given.
3561 if (Has_Aliased_Components (Arr)
3562 or else Has_Atomic_Components (Arr)
3563 or else Is_Full_Access (Ctyp))
3564 and then
3565 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
3566 then
3567 Alias_Atomic_Check : declare
3569 procedure Complain_CS (T : String);
3570 -- Outputs error messages for incorrect CS clause or pragma
3571 -- Pack for aliased or full access components (T is either
3572 -- "aliased" or "atomic" or "volatile full access");
3574 -----------------
3575 -- Complain_CS --
3576 -----------------
3578 procedure Complain_CS (T : String) is
3579 begin
3580 if Has_Component_Size_Clause (Arr) then
3581 Clause :=
3582 Get_Attribute_Definition_Clause
3583 (FS, Attribute_Component_Size);
3585 Error_Msg_N
3586 ("incorrect component size for "
3587 & T & " components", Clause);
3588 Error_Msg_Uint_1 := Esize (Ctyp);
3589 Error_Msg_N
3590 ("\only allowed value is^", Clause);
3592 else
3593 Error_Msg_N
3594 ("?cannot pack " & T & " components (RM 13.2(7))",
3595 Get_Rep_Pragma (FS, Name_Pack));
3596 Set_Is_Packed (Arr, False);
3597 end if;
3598 end Complain_CS;
3600 -- Start of processing for Alias_Atomic_Check
3602 begin
3603 -- If object size of component type isn't known, we cannot
3604 -- be sure so we defer to the back end.
3606 if not Known_Static_Esize (Ctyp) then
3607 null;
3609 -- Case where component size has no effect. First check for
3610 -- object size of component type multiple of the storage
3611 -- unit size.
3613 elsif Esize (Ctyp) mod System_Storage_Unit = 0
3615 -- OK in both packing case and component size case if RM
3616 -- size is known and static and same as the object size.
3618 and then
3619 ((Known_Static_RM_Size (Ctyp)
3620 and then Esize (Ctyp) = RM_Size (Ctyp))
3622 -- Or if we have an explicit component size clause and
3623 -- the component size and object size are equal.
3625 or else
3626 (Has_Component_Size_Clause (Arr)
3627 and then Component_Size (Arr) = Esize (Ctyp)))
3628 then
3629 null;
3631 elsif Has_Aliased_Components (Arr) then
3632 Complain_CS ("aliased");
3634 elsif Has_Atomic_Components (Arr)
3635 or else Is_Atomic (Ctyp)
3636 then
3637 Complain_CS ("atomic");
3639 elsif Is_Volatile_Full_Access (Ctyp) then
3640 Complain_CS ("volatile full access");
3641 end if;
3642 end Alias_Atomic_Check;
3643 end if;
3645 -- Check for Independent_Components/Independent with unsuitable
3646 -- packing or explicit component size clause given.
3648 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
3649 and then
3650 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
3651 then
3652 begin
3653 -- If object size of component type isn't known, we cannot
3654 -- be sure so we defer to the back end.
3656 if not Known_Static_Esize (Ctyp) then
3657 null;
3659 -- Case where component size has no effect. First check for
3660 -- object size of component type multiple of the storage
3661 -- unit size.
3663 elsif Esize (Ctyp) mod System_Storage_Unit = 0
3665 -- OK in both packing case and component size case if RM
3666 -- size is known and multiple of the storage unit size.
3668 and then
3669 ((Known_Static_RM_Size (Ctyp)
3670 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
3672 -- Or if we have an explicit component size clause and
3673 -- the component size is larger than the object size.
3675 or else
3676 (Has_Component_Size_Clause (Arr)
3677 and then Component_Size (Arr) >= Esize (Ctyp)))
3678 then
3679 null;
3681 else
3682 if Has_Component_Size_Clause (Arr) then
3683 Clause :=
3684 Get_Attribute_Definition_Clause
3685 (FS, Attribute_Component_Size);
3687 Error_Msg_N
3688 ("incorrect component size for "
3689 & "independent components", Clause);
3690 Error_Msg_Uint_1 := Esize (Ctyp);
3691 Error_Msg_N
3692 ("\minimum allowed is^", Clause);
3694 else
3695 Error_Msg_N
3696 ("?cannot pack independent components (RM 13.2(7))",
3697 Get_Rep_Pragma (FS, Name_Pack));
3698 Set_Is_Packed (Arr, False);
3699 end if;
3700 end if;
3701 end;
3702 end if;
3704 -- If packing was requested or if the component size was
3705 -- set explicitly, then see if bit packing is required. This
3706 -- processing is only done for base types, since all of the
3707 -- representation aspects involved are type-related.
3709 -- This is not just an optimization, if we start processing the
3710 -- subtypes, they interfere with the settings on the base type
3711 -- (this is because Is_Packed has a slightly different meaning
3712 -- before and after freezing).
3714 declare
3715 Csiz : Uint;
3716 Esiz : Uint;
3718 begin
3719 if Is_Packed (Arr)
3720 and then Known_Static_RM_Size (Ctyp)
3721 and then not Has_Component_Size_Clause (Arr)
3722 then
3723 Csiz := UI_Max (RM_Size (Ctyp), 1);
3725 elsif Known_Component_Size (Arr) then
3726 Csiz := Component_Size (Arr);
3728 elsif not Known_Static_Esize (Ctyp) then
3729 Csiz := Uint_0;
3731 else
3732 Esiz := Esize (Ctyp);
3734 -- We can set the component size if it is less than 16,
3735 -- rounding it up to the next storage unit size.
3737 if Esiz <= 8 then
3738 Csiz := Uint_8;
3739 elsif Esiz <= 16 then
3740 Csiz := Uint_16;
3741 else
3742 Csiz := Uint_0;
3743 end if;
3745 -- Set component size up to match alignment if it would
3746 -- otherwise be less than the alignment. This deals with
3747 -- cases of types whose alignment exceeds their size (the
3748 -- padded type cases).
3750 if Csiz /= 0 and then Known_Alignment (Ctyp) then
3751 declare
3752 A : constant Uint := Alignment_In_Bits (Ctyp);
3753 begin
3754 if Csiz < A then
3755 Csiz := A;
3756 end if;
3757 end;
3758 end if;
3759 end if;
3761 -- Case of component size that may result in bit packing
3763 if 1 <= Csiz and then Csiz <= System_Max_Integer_Size then
3764 declare
3765 Ent : constant Entity_Id :=
3766 First_Subtype (Arr);
3767 Pack_Pragma : constant Node_Id :=
3768 Get_Rep_Pragma (Ent, Name_Pack);
3769 Comp_Size_C : constant Node_Id :=
3770 Get_Attribute_Definition_Clause
3771 (Ent, Attribute_Component_Size);
3773 begin
3774 -- Warn if we have pack and component size so that the
3775 -- pack is ignored.
3777 -- Note: here we must check for the presence of a
3778 -- component size before checking for a Pack pragma to
3779 -- deal with the case where the array type is a derived
3780 -- type whose parent is currently private.
3782 if Present (Comp_Size_C)
3783 and then Has_Pragma_Pack (Ent)
3784 and then Warn_On_Redundant_Constructs
3785 then
3786 Error_Msg_Sloc := Sloc (Comp_Size_C);
3787 Error_Msg_NE
3788 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
3789 Error_Msg_N
3790 ("\?r?explicit component size given#!", Pack_Pragma);
3791 Set_Is_Packed (Base_Type (Ent), False);
3792 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
3793 end if;
3795 -- Set component size if not already set by a component
3796 -- size clause.
3798 if No (Comp_Size_C) then
3799 Set_Component_Size (Arr, Csiz);
3800 end if;
3802 -- Check for base type of 8, 16, 32 bits, where an
3803 -- unsigned subtype has a length one less than the
3804 -- base type (e.g. Natural subtype of Integer).
3806 -- In such cases, if a component size was not set
3807 -- explicitly, then generate a warning.
3809 if Has_Pragma_Pack (Arr)
3810 and then No (Comp_Size_C)
3811 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
3812 and then Known_Esize (Base_Type (Ctyp))
3813 and then Esize (Base_Type (Ctyp)) = Csiz + 1
3814 then
3815 Error_Msg_Uint_1 := Csiz;
3817 if Present (Pack_Pragma) then
3818 Error_Msg_N
3819 ("??pragma Pack causes component size to be ^!",
3820 Pack_Pragma);
3821 Error_Msg_N
3822 ("\??use Component_Size to set desired value!",
3823 Pack_Pragma);
3824 end if;
3825 end if;
3827 -- Bit packing is never needed for 8, 16, 32, 64 or 128
3829 if Addressable (Csiz) then
3831 -- If the Esize of the component is known and equal to
3832 -- the component size then even packing is not needed.
3834 if Known_Static_Esize (Ctyp)
3835 and then Esize (Ctyp) = Csiz
3836 then
3837 -- Here the array was requested to be packed, but
3838 -- the packing request had no effect whatsoever,
3839 -- so flag Is_Packed is reset.
3841 -- Note: semantically this means that we lose track
3842 -- of the fact that a derived type inherited pragma
3843 -- Pack that was non-effective, but that is fine.
3845 -- We regard a Pack pragma as a request to set a
3846 -- representation characteristic, and this request
3847 -- may be ignored.
3849 Set_Is_Packed (Base_Type (Arr), False);
3850 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
3851 else
3852 Set_Is_Packed (Base_Type (Arr), True);
3853 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3854 end if;
3856 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3858 -- Bit packing is not needed for multiples of the storage
3859 -- unit if the type is composite because the back end can
3860 -- byte pack composite types efficiently. That's not true
3861 -- for discrete types because every read would generate a
3862 -- lot of instructions, so we keep using the manipulation
3863 -- routines of the runtime for them.
3865 elsif Csiz mod System_Storage_Unit = 0
3866 and then Is_Composite_Type (Ctyp)
3867 then
3868 Set_Is_Packed (Base_Type (Arr), True);
3869 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3870 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3872 -- In all other cases, bit packing is needed
3874 else
3875 Set_Is_Packed (Base_Type (Arr), True);
3876 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3877 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
3878 end if;
3879 end;
3880 end if;
3881 end;
3883 -- Warn for case of atomic type
3885 Clause := Get_Rep_Pragma (FS, Name_Atomic);
3887 if Present (Clause)
3888 and then not Addressable (Component_Size (FS))
3889 then
3890 Error_Msg_NE
3891 ("non-atomic components of type& may not be "
3892 & "accessible by separate tasks??", Clause, Arr);
3894 if Has_Component_Size_Clause (Arr) then
3895 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
3896 (FS, Attribute_Component_Size));
3897 Error_Msg_N ("\because of component size clause#??", Clause);
3899 elsif Has_Pragma_Pack (Arr) then
3900 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
3901 Error_Msg_N ("\because of pragma Pack#??", Clause);
3902 end if;
3903 end if;
3905 -- Check for scalar storage order
3907 declare
3908 Dummy : Boolean;
3909 begin
3910 Check_Component_Storage_Order
3911 (Encl_Type => Arr,
3912 Comp => Empty,
3913 ADC => Get_Attribute_Definition_Clause
3914 (First_Subtype (Arr),
3915 Attribute_Scalar_Storage_Order),
3916 Comp_ADC_Present => Dummy);
3917 end;
3919 -- Processing that is done only for subtypes
3921 else
3922 -- Acquire alignment from base type. Known_Alignment of the base
3923 -- type is False for Wide_String, for example.
3925 if not Known_Alignment (Arr)
3926 and then Known_Alignment (Base_Type (Arr))
3927 then
3928 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
3929 Adjust_Esize_Alignment (Arr);
3930 end if;
3931 end if;
3933 -- Specific checks for bit-packed arrays
3935 if Is_Bit_Packed_Array (Arr) then
3937 -- Check number of elements for bit-packed arrays that come from
3938 -- source and have compile time known ranges. The bit-packed
3939 -- arrays circuitry does not support arrays with more than
3940 -- Integer'Last + 1 elements, and when this restriction is
3941 -- violated, causes incorrect data access.
3943 -- For the case where this is not compile time known, a run-time
3944 -- check should be generated???
3946 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
3947 declare
3948 Elmts : Uint;
3949 Index : Node_Id;
3950 Ilen : Node_Id;
3951 Ityp : Entity_Id;
3953 begin
3954 Elmts := Uint_1;
3955 Index := First_Index (Arr);
3956 while Present (Index) loop
3957 Ityp := Etype (Index);
3959 -- Never generate an error if any index is of a generic
3960 -- type. We will check this in instances.
3962 if Is_Generic_Type (Ityp) then
3963 Elmts := Uint_0;
3964 exit;
3965 end if;
3967 Ilen :=
3968 Make_Attribute_Reference (Loc,
3969 Prefix => New_Occurrence_Of (Ityp, Loc),
3970 Attribute_Name => Name_Range_Length);
3971 Analyze_And_Resolve (Ilen);
3973 -- No attempt is made to check number of elements if not
3974 -- compile time known.
3976 if Nkind (Ilen) /= N_Integer_Literal then
3977 Elmts := Uint_0;
3978 exit;
3979 end if;
3981 Elmts := Elmts * Intval (Ilen);
3982 Next_Index (Index);
3983 end loop;
3985 if Elmts > Intval (High_Bound
3986 (Scalar_Range (Standard_Integer))) + 1
3987 then
3988 Error_Msg_N
3989 ("bit packed array type may not have "
3990 & "more than Integer''Last+1 elements", Arr);
3991 end if;
3992 end;
3993 end if;
3995 -- Check size
3997 if Known_RM_Size (Arr) then
3998 declare
3999 SizC : constant Node_Id := Size_Clause (Arr);
4000 Discard : Boolean;
4002 begin
4003 -- It is not clear if it is possible to have no size clause
4004 -- at this stage, but it is not worth worrying about. Post
4005 -- error on the entity name in the size clause if present,
4006 -- else on the type entity itself.
4008 if Present (SizC) then
4009 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
4010 else
4011 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
4012 end if;
4013 end;
4014 end if;
4015 end if;
4017 -- If any of the index types was an enumeration type with a non-
4018 -- standard rep clause, then we indicate that the array type is
4019 -- always packed (even if it is not bit-packed).
4021 if Non_Standard_Enum then
4022 Set_Has_Non_Standard_Rep (Base_Type (Arr));
4023 Set_Is_Packed (Base_Type (Arr));
4024 end if;
4026 Set_Component_Alignment_If_Not_Set (Arr);
4028 -- If the array is packed and bit-packed or packed to eliminate holes
4029 -- in the non-contiguous enumeration index types, we must create the
4030 -- packed array type to be used to actually implement the type. This
4031 -- is only needed for real array types (not for string literal types,
4032 -- since they are present only for the front end).
4034 if Is_Packed (Arr)
4035 and then (Is_Bit_Packed_Array (Arr) or else Non_Standard_Enum)
4036 and then Ekind (Arr) /= E_String_Literal_Subtype
4037 then
4038 Create_Packed_Array_Impl_Type (Arr);
4039 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
4041 -- Make sure that we have the necessary routines to implement the
4042 -- packing, and complain now if not. Note that we only test this
4043 -- for constrained array types.
4045 if Is_Constrained (Arr)
4046 and then Is_Bit_Packed_Array (Arr)
4047 and then Present (Packed_Array_Impl_Type (Arr))
4048 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
4049 then
4050 declare
4051 CS : constant Uint := Component_Size (Arr);
4052 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
4054 begin
4055 if RE /= RE_Null
4056 and then not RTE_Available (RE)
4057 then
4058 Error_Msg_CRT
4059 ("packing of " & UI_Image (CS) & "-bit components",
4060 First_Subtype (Etype (Arr)));
4062 -- Cancel the packing
4064 Set_Is_Packed (Base_Type (Arr), False);
4065 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
4066 Set_Packed_Array_Impl_Type (Arr, Empty);
4067 goto Skip_Packed;
4068 end if;
4069 end;
4070 end if;
4072 -- Size information of packed array type is copied to the array
4073 -- type, since this is really the representation. But do not
4074 -- override explicit existing size values. If the ancestor subtype
4075 -- is constrained the Packed_Array_Impl_Type will be inherited
4076 -- from it, but the size may have been provided already, and
4077 -- must not be overridden either.
4079 if not Has_Size_Clause (Arr)
4080 and then
4081 (No (Ancestor_Subtype (Arr))
4082 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
4083 then
4084 Copy_Esize (To => Arr, From => Packed_Array_Impl_Type (Arr));
4085 Copy_RM_Size (To => Arr, From => Packed_Array_Impl_Type (Arr));
4086 end if;
4088 if not Has_Alignment_Clause (Arr) then
4089 Copy_Alignment
4090 (To => Arr, From => Packed_Array_Impl_Type (Arr));
4091 end if;
4092 end if;
4094 <<Skip_Packed>>
4096 -- A Ghost type cannot have a component of protected or task type
4097 -- (SPARK RM 6.9(19)).
4099 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
4100 Error_Msg_N
4101 ("ghost array type & cannot have concurrent component type",
4102 Arr);
4103 end if;
4104 end Freeze_Array_Type;
4106 -------------------------------
4107 -- Freeze_Object_Declaration --
4108 -------------------------------
4110 procedure Freeze_Object_Declaration (E : Entity_Id) is
4111 procedure Check_Large_Modular_Array (Typ : Entity_Id);
4112 -- Check that the size of array type Typ can be computed without
4113 -- overflow, and generates a Storage_Error otherwise. This is only
4114 -- relevant for array types whose index is a modular type with
4115 -- Standard_Long_Long_Integer_Size bits: wrap-around arithmetic
4116 -- might yield a meaningless value for the length of the array,
4117 -- or its corresponding attribute.
4119 procedure Check_Pragma_Thread_Local_Storage (Var_Id : Entity_Id);
4120 -- Ensure that the initialization state of variable Var_Id subject
4121 -- to pragma Thread_Local_Storage agrees with the semantics of the
4122 -- pragma.
4124 function Has_Default_Initialization
4125 (Obj_Id : Entity_Id) return Boolean;
4126 -- Determine whether object Obj_Id default initialized
4128 -------------------------------
4129 -- Check_Large_Modular_Array --
4130 -------------------------------
4132 procedure Check_Large_Modular_Array (Typ : Entity_Id) is
4133 Obj_Loc : constant Source_Ptr := Sloc (E);
4134 Idx_Typ : Entity_Id;
4136 begin
4137 -- Nothing to do when expansion is disabled because this routine
4138 -- generates a runtime check.
4140 if not Expander_Active then
4141 return;
4143 -- Nothing to do for String literal subtypes because their index
4144 -- cannot be a modular type.
4146 elsif Ekind (Typ) = E_String_Literal_Subtype then
4147 return;
4149 -- Nothing to do for an imported object because the object will
4150 -- be created on the exporting side.
4152 elsif Is_Imported (E) then
4153 return;
4155 -- Nothing to do for unconstrained array types. This case arises
4156 -- when the object declaration is illegal.
4158 elsif not Is_Constrained (Typ) then
4159 return;
4160 end if;
4162 Idx_Typ := Etype (First_Index (Typ));
4164 -- To prevent arithmetic overflow with large values, we raise
4165 -- Storage_Error under the following guard:
4167 -- (Arr'Last / 2 - Arr'First / 2) > (2 ** 30)
4169 -- This takes care of the boundary case, but it is preferable to
4170 -- use a smaller limit, because even on 64-bit architectures an
4171 -- array of more than 2 ** 30 bytes is likely to raise
4172 -- Storage_Error.
4174 if Is_Modular_Integer_Type (Idx_Typ)
4175 and then RM_Size (Idx_Typ) = Standard_Long_Long_Integer_Size
4176 then
4177 -- Ensure that the type of the object is elaborated before
4178 -- the check itself is emitted to avoid elaboration issues
4179 -- in the code generator at the library level.
4181 if Is_Itype (Etype (E))
4182 and then In_Open_Scopes (Scope (Etype (E)))
4183 then
4184 declare
4185 Ref_Node : constant Node_Id :=
4186 Make_Itype_Reference (Obj_Loc);
4187 begin
4188 Set_Itype (Ref_Node, Etype (E));
4189 Insert_Action (Declaration_Node (E), Ref_Node);
4190 end;
4191 end if;
4193 Insert_Action (Declaration_Node (E),
4194 Make_Raise_Storage_Error (Obj_Loc,
4195 Condition =>
4196 Make_Op_Ge (Obj_Loc,
4197 Left_Opnd =>
4198 Make_Op_Subtract (Obj_Loc,
4199 Left_Opnd =>
4200 Make_Op_Divide (Obj_Loc,
4201 Left_Opnd =>
4202 Make_Attribute_Reference (Obj_Loc,
4203 Prefix =>
4204 New_Occurrence_Of (Typ, Obj_Loc),
4205 Attribute_Name => Name_Last),
4206 Right_Opnd =>
4207 Make_Integer_Literal (Obj_Loc, Uint_2)),
4208 Right_Opnd =>
4209 Make_Op_Divide (Obj_Loc,
4210 Left_Opnd =>
4211 Make_Attribute_Reference (Obj_Loc,
4212 Prefix =>
4213 New_Occurrence_Of (Typ, Obj_Loc),
4214 Attribute_Name => Name_First),
4215 Right_Opnd =>
4216 Make_Integer_Literal (Obj_Loc, Uint_2))),
4217 Right_Opnd =>
4218 Make_Integer_Literal (Obj_Loc, (Uint_2 ** 30))),
4219 Reason => SE_Object_Too_Large));
4220 end if;
4221 end Check_Large_Modular_Array;
4223 ---------------------------------------
4224 -- Check_Pragma_Thread_Local_Storage --
4225 ---------------------------------------
4227 procedure Check_Pragma_Thread_Local_Storage (Var_Id : Entity_Id) is
4228 function Has_Incompatible_Initialization
4229 (Var_Decl : Node_Id) return Boolean;
4230 -- Determine whether variable Var_Id with declaration Var_Decl is
4231 -- initialized with a value that violates the semantics of pragma
4232 -- Thread_Local_Storage.
4234 -------------------------------------
4235 -- Has_Incompatible_Initialization --
4236 -------------------------------------
4238 function Has_Incompatible_Initialization
4239 (Var_Decl : Node_Id) return Boolean
4241 Init_Expr : constant Node_Id := Expression (Var_Decl);
4243 begin
4244 -- The variable is default-initialized. This directly violates
4245 -- the semantics of the pragma.
4247 if Has_Default_Initialization (Var_Id) then
4248 return True;
4250 -- The variable has explicit initialization. In this case only
4251 -- a handful of values satisfy the semantics of the pragma.
4253 elsif Has_Init_Expression (Var_Decl)
4254 and then Present (Init_Expr)
4255 then
4256 -- "null" is a legal form of initialization
4258 if Nkind (Init_Expr) = N_Null then
4259 return False;
4261 -- A static expression is a legal form of initialization
4263 elsif Is_Static_Expression (Init_Expr) then
4264 return False;
4266 -- A static aggregate is a legal form of initialization
4268 elsif Nkind (Init_Expr) = N_Aggregate
4269 and then Compile_Time_Known_Aggregate (Init_Expr)
4270 then
4271 return False;
4273 -- All other initialization expressions violate the semantic
4274 -- of the pragma.
4276 else
4277 return True;
4278 end if;
4280 -- The variable lacks any kind of initialization, which agrees
4281 -- with the semantics of the pragma.
4283 else
4284 return False;
4285 end if;
4286 end Has_Incompatible_Initialization;
4288 -- Local declarations
4290 Var_Decl : constant Node_Id := Declaration_Node (Var_Id);
4292 -- Start of processing for Check_Pragma_Thread_Local_Storage
4294 begin
4295 -- A variable whose initialization is suppressed lacks any kind of
4296 -- initialization.
4298 if Suppress_Initialization (Var_Id) then
4299 null;
4301 -- The variable has default initialization, or is explicitly
4302 -- initialized to a value other than null, static expression,
4303 -- or a static aggregate.
4305 elsif Has_Incompatible_Initialization (Var_Decl) then
4306 Error_Msg_NE
4307 ("Thread_Local_Storage variable& is improperly initialized",
4308 Var_Decl, Var_Id);
4309 Error_Msg_NE
4310 ("\only allowed initialization is explicit NULL, static "
4311 & "expression or static aggregate", Var_Decl, Var_Id);
4312 end if;
4313 end Check_Pragma_Thread_Local_Storage;
4315 --------------------------------
4316 -- Has_Default_Initialization --
4317 --------------------------------
4319 function Has_Default_Initialization
4320 (Obj_Id : Entity_Id) return Boolean
4322 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
4323 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4325 begin
4326 return
4327 Comes_From_Source (Obj_Id)
4328 and then not Is_Imported (Obj_Id)
4329 and then not Has_Init_Expression (Obj_Decl)
4330 and then
4331 ((Has_Non_Null_Base_Init_Proc (Obj_Typ)
4332 and then not No_Initialization (Obj_Decl)
4333 and then not Initialization_Suppressed (Obj_Typ))
4334 or else
4335 (Needs_Simple_Initialization (Obj_Typ)
4336 and then not Is_Internal (Obj_Id)));
4337 end Has_Default_Initialization;
4339 -- Local variables
4341 Typ : constant Entity_Id := Etype (E);
4342 Def : Node_Id;
4344 -- Start of processing for Freeze_Object_Declaration
4346 begin
4347 -- Abstract type allowed only for C++ imported variables or constants
4349 -- Note: we inhibit this check for objects that do not come from
4350 -- source because there is at least one case (the expansion of
4351 -- x'Class'Input where x is abstract) where we legitimately
4352 -- generate an abstract object.
4354 if Is_Abstract_Type (Typ)
4355 and then Comes_From_Source (Parent (E))
4356 and then not (Is_Imported (E) and then Is_CPP_Class (Typ))
4357 then
4358 Def := Object_Definition (Parent (E));
4360 Error_Msg_N ("type of object cannot be abstract", Def);
4362 if Is_CPP_Class (Etype (E)) then
4363 Error_Msg_NE ("\} may need a cpp_constructor", Def, Typ);
4365 elsif Present (Expression (Parent (E))) then
4366 Error_Msg_N -- CODEFIX
4367 ("\maybe a class-wide type was meant", Def);
4368 end if;
4369 end if;
4371 -- For object created by object declaration, perform required
4372 -- categorization (preelaborate and pure) checks. Defer these
4373 -- checks to freeze time since pragma Import inhibits default
4374 -- initialization and thus pragma Import affects these checks.
4376 Validate_Object_Declaration (Declaration_Node (E));
4378 -- If there is an address clause, check that it is valid and if need
4379 -- be move initialization to the freeze node.
4381 Check_Address_Clause (E);
4383 -- Similar processing is needed for aspects that may affect object
4384 -- layout, like Address, if there is an initialization expression.
4385 -- We don't do this if there is a pragma Linker_Section, because it
4386 -- would prevent the back end from statically initializing the
4387 -- object; we don't want elaboration code in that case.
4389 if Has_Delayed_Aspects (E)
4390 and then Expander_Active
4391 and then Is_Array_Type (Typ)
4392 and then Present (Expression (Declaration_Node (E)))
4393 and then No (Linker_Section_Pragma (E))
4394 then
4395 declare
4396 Decl : constant Node_Id := Declaration_Node (E);
4397 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
4399 begin
4400 -- Capture initialization value at point of declaration, and
4401 -- make explicit assignment legal, because object may be a
4402 -- constant.
4404 Remove_Side_Effects (Expression (Decl));
4405 Set_Assignment_OK (Lhs);
4407 -- Move initialization to freeze actions
4409 Append_Freeze_Action (E,
4410 Make_Assignment_Statement (Loc,
4411 Name => Lhs,
4412 Expression => Expression (Decl)));
4414 Set_No_Initialization (Decl);
4415 -- Set_Is_Frozen (E, False);
4416 end;
4417 end if;
4419 -- Reset Is_True_Constant for non-constant aliased object. We
4420 -- consider that the fact that a non-constant object is aliased may
4421 -- indicate that some funny business is going on, e.g. an aliased
4422 -- object is passed by reference to a procedure which captures the
4423 -- address of the object, which is later used to assign a new value,
4424 -- even though the compiler thinks that it is not modified. Such
4425 -- code is highly dubious, but we choose to make it "work" for
4426 -- non-constant aliased objects.
4428 -- Note that we used to do this for all aliased objects, whether or
4429 -- not constant, but this caused anomalies down the line because we
4430 -- ended up with static objects that were not Is_True_Constant. Not
4431 -- resetting Is_True_Constant for (aliased) constant objects ensures
4432 -- that this anomaly never occurs.
4434 -- However, we don't do that for internal entities. We figure that if
4435 -- we deliberately set Is_True_Constant for an internal entity, e.g.
4436 -- a dispatch table entry, then we mean it.
4438 if Ekind (E) /= E_Constant
4439 and then (Is_Aliased (E) or else Is_Aliased (Typ))
4440 and then not Is_Internal_Name (Chars (E))
4441 then
4442 Set_Is_True_Constant (E, False);
4443 end if;
4445 -- If the object needs any kind of default initialization, an error
4446 -- must be issued if No_Default_Initialization applies. The check
4447 -- doesn't apply to imported objects, which are not ever default
4448 -- initialized, and is why the check is deferred until freezing, at
4449 -- which point we know if Import applies. Deferred constants are also
4450 -- exempted from this test because their completion is explicit, or
4451 -- through an import pragma.
4453 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
4454 null;
4456 elsif Has_Default_Initialization (E) then
4457 Check_Restriction
4458 (No_Default_Initialization, Declaration_Node (E));
4459 end if;
4461 -- Ensure that a variable subject to pragma Thread_Local_Storage
4463 -- * Lacks default initialization, or
4465 -- * The initialization expression is either "null", a static
4466 -- constant, or a compile-time known aggregate.
4468 if Has_Pragma_Thread_Local_Storage (E) then
4469 Check_Pragma_Thread_Local_Storage (E);
4470 end if;
4472 -- For imported objects, set Is_Public unless there is also an
4473 -- address clause, which means that there is no external symbol
4474 -- needed for the Import (Is_Public may still be set for other
4475 -- unrelated reasons). Note that we delayed this processing
4476 -- till freeze time so that we can be sure not to set the flag
4477 -- if there is an address clause. If there is such a clause,
4478 -- then the only purpose of the Import pragma is to suppress
4479 -- implicit initialization.
4481 if Is_Imported (E) and then No (Address_Clause (E)) then
4482 Set_Is_Public (E);
4483 end if;
4485 -- For source objects that are not Imported and are library level, if
4486 -- no linker section pragma was given inherit the appropriate linker
4487 -- section from the corresponding type.
4489 if Comes_From_Source (E)
4490 and then not Is_Imported (E)
4491 and then Is_Library_Level_Entity (E)
4492 and then No (Linker_Section_Pragma (E))
4493 then
4494 Set_Linker_Section_Pragma (E, Linker_Section_Pragma (Typ));
4495 end if;
4497 -- For convention C objects of an enumeration type, warn if the size
4498 -- is not integer size and no explicit size given. Skip warning for
4499 -- Boolean and Character, and assume programmer expects 8-bit sizes
4500 -- for these cases.
4502 if (Convention (E) = Convention_C
4503 or else
4504 Convention (E) = Convention_CPP)
4505 and then Is_Enumeration_Type (Typ)
4506 and then not Is_Character_Type (Typ)
4507 and then not Is_Boolean_Type (Typ)
4508 and then Esize (Typ) < Standard_Integer_Size
4509 and then not Has_Size_Clause (E)
4510 then
4511 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
4512 Error_Msg_N
4513 ("??convention C enumeration object has size less than ^", E);
4514 Error_Msg_N ("\??use explicit size clause to set size", E);
4515 end if;
4517 -- Declaring too big an array in disabled ghost code is OK
4519 if Is_Array_Type (Typ) and then not Is_Ignored_Ghost_Entity (E) then
4520 Check_Large_Modular_Array (Typ);
4521 end if;
4522 end Freeze_Object_Declaration;
4524 -----------------------------
4525 -- Freeze_Generic_Entities --
4526 -----------------------------
4528 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
4529 E : Entity_Id;
4530 F : Node_Id;
4531 Flist : List_Id;
4533 begin
4534 Flist := New_List;
4535 E := First_Entity (Pack);
4536 while Present (E) loop
4537 if Is_Type (E) and then not Is_Generic_Type (E) then
4538 F := Make_Freeze_Generic_Entity (Sloc (Pack));
4539 Set_Entity (F, E);
4540 Append_To (Flist, F);
4542 elsif Ekind (E) = E_Generic_Package then
4543 Append_List_To (Flist, Freeze_Generic_Entities (E));
4544 end if;
4546 Next_Entity (E);
4547 end loop;
4549 return Flist;
4550 end Freeze_Generic_Entities;
4552 --------------------
4553 -- Freeze_Profile --
4554 --------------------
4556 function Freeze_Profile (E : Entity_Id) return Boolean is
4557 F_Type : Entity_Id;
4558 R_Type : Entity_Id;
4559 Warn_Node : Node_Id;
4561 begin
4562 -- Loop through formals
4564 Formal := First_Formal (E);
4565 while Present (Formal) loop
4566 F_Type := Etype (Formal);
4568 -- AI05-0151: incomplete types can appear in a profile. By the
4569 -- time the entity is frozen, the full view must be available,
4570 -- unless it is a limited view.
4572 if Is_Incomplete_Type (F_Type)
4573 and then Present (Full_View (F_Type))
4574 and then not From_Limited_With (F_Type)
4575 then
4576 F_Type := Full_View (F_Type);
4577 Set_Etype (Formal, F_Type);
4578 end if;
4580 if not From_Limited_With (F_Type)
4581 and then Should_Freeze_Type (F_Type, E, N)
4582 then
4583 Freeze_And_Append (F_Type, N, Result);
4584 end if;
4586 if Is_Private_Type (F_Type)
4587 and then Is_Private_Type (Base_Type (F_Type))
4588 and then No (Full_View (Base_Type (F_Type)))
4589 and then not Is_Generic_Type (F_Type)
4590 and then not Is_Derived_Type (F_Type)
4591 then
4592 -- If the type of a formal is incomplete, subprogram is being
4593 -- frozen prematurely. Within an instance (but not within a
4594 -- wrapper package) this is an artifact of our need to regard
4595 -- the end of an instantiation as a freeze point. Otherwise it
4596 -- is a definite error.
4598 if In_Instance then
4599 Set_Is_Frozen (E, False);
4600 Result := No_List;
4601 return False;
4603 elsif not After_Last_Declaration then
4604 Error_Msg_NE
4605 ("type & must be fully defined before this point",
4607 F_Type);
4608 end if;
4609 end if;
4611 -- Check suspicious parameter for C function. These tests apply
4612 -- only to exported/imported subprograms.
4614 if Warn_On_Export_Import
4615 and then Comes_From_Source (E)
4616 and then Convention (E) in Convention_C_Family
4617 and then (Is_Imported (E) or else Is_Exported (E))
4618 and then Convention (E) /= Convention (Formal)
4619 and then not Has_Warnings_Off (E)
4620 and then not Has_Warnings_Off (F_Type)
4621 and then not Has_Warnings_Off (Formal)
4622 then
4623 -- Qualify mention of formals with subprogram name
4625 Error_Msg_Qual_Level := 1;
4627 -- Check suspicious use of fat C pointer, but do not emit
4628 -- a warning on an access to subprogram when unnesting is
4629 -- active.
4631 if Is_Access_Type (F_Type)
4632 and then Known_Esize (F_Type)
4633 and then Esize (F_Type) > Ttypes.System_Address_Size
4634 and then (not Unnest_Subprogram_Mode
4635 or else not Is_Access_Subprogram_Type (F_Type))
4636 then
4637 Error_Msg_N
4638 ("?x?type of & does not correspond to C pointer!", Formal);
4640 -- Check suspicious return of boolean
4642 elsif Root_Type (F_Type) = Standard_Boolean
4643 and then Convention (F_Type) = Convention_Ada
4644 and then not Has_Warnings_Off (F_Type)
4645 and then not Has_Size_Clause (F_Type)
4646 then
4647 Error_Msg_N
4648 ("& is an 8-bit Ada Boolean?x?", Formal);
4649 Error_Msg_N
4650 ("\use appropriate corresponding type in C "
4651 & "(e.g. char)?x?", Formal);
4653 -- Check suspicious tagged type
4655 elsif (Is_Tagged_Type (F_Type)
4656 or else
4657 (Is_Access_Type (F_Type)
4658 and then Is_Tagged_Type (Designated_Type (F_Type))))
4659 and then Convention (E) = Convention_C
4660 then
4661 Error_Msg_N
4662 ("?x?& involves a tagged type which does not "
4663 & "correspond to any C type!", Formal);
4665 -- Check wrong convention subprogram pointer
4667 elsif Ekind (F_Type) = E_Access_Subprogram_Type
4668 and then not Has_Foreign_Convention (F_Type)
4669 then
4670 Error_Msg_N
4671 ("?x?subprogram pointer & should "
4672 & "have foreign convention!", Formal);
4673 Error_Msg_Sloc := Sloc (F_Type);
4674 Error_Msg_NE
4675 ("\?x?add Convention pragma to declaration of &#",
4676 Formal, F_Type);
4677 end if;
4679 -- Turn off name qualification after message output
4681 Error_Msg_Qual_Level := 0;
4682 end if;
4684 -- Check for unconstrained array in exported foreign convention
4685 -- case.
4687 if Has_Foreign_Convention (E)
4688 and then not Is_Imported (E)
4689 and then Is_Array_Type (F_Type)
4690 and then not Is_Constrained (F_Type)
4691 and then Warn_On_Export_Import
4692 then
4693 Error_Msg_Qual_Level := 1;
4695 -- If this is an inherited operation, place the warning on
4696 -- the derived type declaration, rather than on the original
4697 -- subprogram.
4699 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
4700 then
4701 Warn_Node := Parent (E);
4703 if Formal = First_Formal (E) then
4704 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
4705 end if;
4706 else
4707 Warn_Node := Formal;
4708 end if;
4710 Error_Msg_NE ("?x?type of argument& is unconstrained array",
4711 Warn_Node, Formal);
4712 Error_Msg_N ("\?x?foreign caller must pass bounds explicitly",
4713 Warn_Node);
4714 Error_Msg_Qual_Level := 0;
4715 end if;
4717 if not From_Limited_With (F_Type) then
4718 if Is_Access_Type (F_Type) then
4719 F_Type := Designated_Type (F_Type);
4720 end if;
4721 end if;
4723 Next_Formal (Formal);
4724 end loop;
4726 -- Case of function: similar checks on return type
4728 if Ekind (E) = E_Function then
4730 -- Freeze return type
4732 R_Type := Etype (E);
4734 -- AI05-0151: the return type may have been incomplete at the
4735 -- point of declaration. Replace it with the full view, unless the
4736 -- current type is a limited view. In that case the full view is
4737 -- in a different unit, and gigi finds the non-limited view after
4738 -- the other unit is elaborated.
4740 if Ekind (R_Type) = E_Incomplete_Type
4741 and then Present (Full_View (R_Type))
4742 and then not From_Limited_With (R_Type)
4743 then
4744 R_Type := Full_View (R_Type);
4745 Set_Etype (E, R_Type);
4746 end if;
4748 if Should_Freeze_Type (R_Type, E, N) then
4749 Freeze_And_Append (R_Type, N, Result);
4750 end if;
4752 -- Check suspicious return type for C function
4754 if Warn_On_Export_Import
4755 and then Comes_From_Source (E)
4756 and then Convention (E) in Convention_C_Family
4757 and then (Is_Imported (E) or else Is_Exported (E))
4758 then
4759 -- Check suspicious return of fat C pointer
4761 if Is_Access_Type (R_Type)
4762 and then Known_Esize (R_Type)
4763 and then Esize (R_Type) > Ttypes.System_Address_Size
4764 and then not Has_Warnings_Off (E)
4765 and then not Has_Warnings_Off (R_Type)
4766 then
4767 Error_Msg_N
4768 ("?x?return type of& does not correspond to C pointer!",
4771 -- Check suspicious return of boolean
4773 elsif Root_Type (R_Type) = Standard_Boolean
4774 and then Convention (R_Type) = Convention_Ada
4775 and then not Has_Warnings_Off (E)
4776 and then not Has_Warnings_Off (R_Type)
4777 and then not Has_Size_Clause (R_Type)
4778 then
4779 declare
4780 N : constant Node_Id :=
4781 Result_Definition (Declaration_Node (E));
4782 begin
4783 Error_Msg_NE
4784 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
4785 Error_Msg_NE
4786 ("\use appropriate corresponding type in C "
4787 & "(e.g. char)?x?", N, E);
4788 end;
4790 -- Check suspicious return tagged type
4792 elsif (Is_Tagged_Type (R_Type)
4793 or else (Is_Access_Type (R_Type)
4794 and then
4795 Is_Tagged_Type
4796 (Designated_Type (R_Type))))
4797 and then Convention (E) = Convention_C
4798 and then not Has_Warnings_Off (E)
4799 and then not Has_Warnings_Off (R_Type)
4800 then
4801 Error_Msg_N ("?x?return type of & does not "
4802 & "correspond to C type!", E);
4804 -- Check return of wrong convention subprogram pointer
4806 elsif Ekind (R_Type) = E_Access_Subprogram_Type
4807 and then not Has_Foreign_Convention (R_Type)
4808 and then not Has_Warnings_Off (E)
4809 and then not Has_Warnings_Off (R_Type)
4810 then
4811 Error_Msg_N ("?x?& should return a foreign "
4812 & "convention subprogram pointer", E);
4813 Error_Msg_Sloc := Sloc (R_Type);
4814 Error_Msg_NE
4815 ("\?x?add Convention pragma to declaration of& #",
4816 E, R_Type);
4817 end if;
4818 end if;
4820 -- Give warning for suspicious return of a result of an
4821 -- unconstrained array type in a foreign convention function.
4823 if Has_Foreign_Convention (E)
4825 -- We are looking for a return of unconstrained array
4827 and then Is_Array_Type (R_Type)
4828 and then not Is_Constrained (R_Type)
4830 -- Exclude imported routines, the warning does not belong on
4831 -- the import, but rather on the routine definition.
4833 and then not Is_Imported (E)
4835 -- Check that general warning is enabled, and that it is not
4836 -- suppressed for this particular case.
4838 and then Warn_On_Export_Import
4839 and then not Has_Warnings_Off (E)
4840 and then not Has_Warnings_Off (R_Type)
4841 then
4842 Error_Msg_N
4843 ("?x?foreign convention function& should not return "
4844 & "unconstrained array!", E);
4845 end if;
4846 end if;
4848 -- Check suspicious use of Import in pure unit (cases where the RM
4849 -- allows calls to be omitted).
4851 if Is_Imported (E)
4853 -- It might be suspicious if the compilation unit has the Pure
4854 -- aspect/pragma.
4856 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
4858 -- The RM allows omission of calls only in the case of
4859 -- library-level subprograms (see RM-10.2.1(18)).
4861 and then Is_Library_Level_Entity (E)
4863 -- Ignore internally generated entity. This happens in some cases
4864 -- of subprograms in specs, where we generate an implied body.
4866 and then Comes_From_Source (Import_Pragma (E))
4868 -- Assume run-time knows what it is doing
4870 and then not GNAT_Mode
4872 -- Assume explicit Pure_Function means import is pure
4874 and then not Has_Pragma_Pure_Function (E)
4876 -- Don't need warning in relaxed semantics mode
4878 and then not Relaxed_RM_Semantics
4880 -- Assume convention Intrinsic is OK, since this is specialized.
4881 -- This deals with the DEC unit current_exception.ads
4883 and then Convention (E) /= Convention_Intrinsic
4885 -- Assume that ASM interface knows what it is doing
4887 and then Convention (E) /= Convention_Assembler
4888 then
4889 Error_Msg_N
4890 ("pragma Import in Pure unit??", Import_Pragma (E));
4891 Error_Msg_NE
4892 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
4893 Import_Pragma (E), E);
4894 end if;
4896 return True;
4897 end Freeze_Profile;
4899 ------------------------
4900 -- Freeze_Record_Type --
4901 ------------------------
4903 procedure Freeze_Record_Type (Rec : Entity_Id) is
4904 ADC : Node_Id;
4905 Comp : Entity_Id;
4906 IR : Node_Id;
4907 Prev : Entity_Id;
4909 Junk : Boolean;
4910 pragma Warnings (Off, Junk);
4912 Aliased_Component : Boolean := False;
4913 -- Set True if we find at least one component which is aliased. This
4914 -- is used to prevent Implicit_Packing of the record, since packing
4915 -- cannot modify the size of alignment of an aliased component.
4917 All_Elem_Components : Boolean := True;
4918 -- True if all components are of a type whose underlying type is
4919 -- elementary.
4921 All_Sized_Components : Boolean := True;
4922 -- True if all components have a known RM_Size
4924 All_Storage_Unit_Components : Boolean := True;
4925 -- True if all components have an RM_Size that is a multiple of the
4926 -- storage unit.
4928 Elem_Component_Total_Esize : Uint := Uint_0;
4929 -- Accumulates total Esize values of all elementary components. Used
4930 -- for processing of Implicit_Packing.
4932 Placed_Component : Boolean := False;
4933 -- Set True if we find at least one component with a component
4934 -- clause (used to warn about useless Bit_Order pragmas, and also
4935 -- to detect cases where Implicit_Packing may have an effect).
4937 Sized_Component_Total_RM_Size : Uint := Uint_0;
4938 -- Accumulates total RM_Size values of all sized components. Used
4939 -- for processing of Implicit_Packing.
4941 Sized_Component_Total_Round_RM_Size : Uint := Uint_0;
4942 -- Accumulates total RM_Size values of all sized components, rounded
4943 -- individually to a multiple of the storage unit.
4945 SSO_ADC : Node_Id;
4946 -- Scalar_Storage_Order attribute definition clause for the record
4948 SSO_ADC_Component : Boolean := False;
4949 -- Set True if we find at least one component whose type has a
4950 -- Scalar_Storage_Order attribute definition clause.
4952 Unplaced_Component : Boolean := False;
4953 -- Set True if we find at least one component with no component
4954 -- clause (used to warn about useless Pack pragmas).
4956 procedure Check_Itype (Typ : Entity_Id);
4957 -- If the component subtype is an access to a constrained subtype of
4958 -- an already frozen type, make the subtype frozen as well. It might
4959 -- otherwise be frozen in the wrong scope, and a freeze node on
4960 -- subtype has no effect. Similarly, if the component subtype is a
4961 -- regular (not protected) access to subprogram, set the anonymous
4962 -- subprogram type to frozen as well, to prevent an out-of-scope
4963 -- freeze node at some eventual point of call. Protected operations
4964 -- are handled elsewhere.
4966 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
4967 -- Make sure that all types mentioned in Discrete_Choices of the
4968 -- variants referenceed by the Variant_Part VP are frozen. This is
4969 -- a recursive routine to deal with nested variants.
4971 -----------------
4972 -- Check_Itype --
4973 -----------------
4975 procedure Check_Itype (Typ : Entity_Id) is
4976 Desig : constant Entity_Id := Designated_Type (Typ);
4978 begin
4979 if not Is_Frozen (Desig)
4980 and then Is_Frozen (Base_Type (Desig))
4981 then
4982 Set_Is_Frozen (Desig);
4984 -- In addition, add an Itype_Reference to ensure that the
4985 -- access subtype is elaborated early enough. This cannot be
4986 -- done if the subtype may depend on discriminants.
4988 if Ekind (Comp) = E_Component
4989 and then Is_Itype (Etype (Comp))
4990 and then not Has_Discriminants (Rec)
4991 then
4992 IR := Make_Itype_Reference (Sloc (Comp));
4993 Set_Itype (IR, Desig);
4994 Add_To_Result (IR);
4995 end if;
4997 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
4998 and then Convention (Desig) /= Convention_Protected
4999 then
5000 Set_Is_Frozen (Desig);
5001 Create_Extra_Formals (Desig);
5002 end if;
5003 end Check_Itype;
5005 ------------------------------------
5006 -- Freeze_Choices_In_Variant_Part --
5007 ------------------------------------
5009 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
5010 pragma Assert (Nkind (VP) = N_Variant_Part);
5012 Variant : Node_Id;
5013 Choice : Node_Id;
5014 CL : Node_Id;
5016 begin
5017 -- Loop through variants
5019 Variant := First_Non_Pragma (Variants (VP));
5020 while Present (Variant) loop
5022 -- Loop through choices, checking that all types are frozen
5024 Choice := First_Non_Pragma (Discrete_Choices (Variant));
5025 while Present (Choice) loop
5026 if Nkind (Choice) in N_Has_Etype
5027 and then Present (Etype (Choice))
5028 then
5029 Freeze_And_Append (Etype (Choice), N, Result);
5030 end if;
5032 Next_Non_Pragma (Choice);
5033 end loop;
5035 -- Check for nested variant part to process
5037 CL := Component_List (Variant);
5039 if not Null_Present (CL) then
5040 if Present (Variant_Part (CL)) then
5041 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
5042 end if;
5043 end if;
5045 Next_Non_Pragma (Variant);
5046 end loop;
5047 end Freeze_Choices_In_Variant_Part;
5049 -- Start of processing for Freeze_Record_Type
5051 begin
5052 -- Freeze components and embedded subtypes
5054 Comp := First_Entity (Rec);
5055 Prev := Empty;
5056 while Present (Comp) loop
5057 if Is_Aliased (Comp) then
5058 Aliased_Component := True;
5059 end if;
5061 -- Handle the component and discriminant case
5063 if Ekind (Comp) in E_Component | E_Discriminant then
5064 declare
5065 CC : constant Node_Id := Component_Clause (Comp);
5067 begin
5068 -- Freezing a record type freezes the type of each of its
5069 -- components. However, if the type of the component is
5070 -- part of this record, we do not want or need a separate
5071 -- Freeze_Node. Note that Is_Itype is wrong because that's
5072 -- also set in private type cases. We also can't check for
5073 -- the Scope being exactly Rec because of private types and
5074 -- record extensions.
5076 if Is_Itype (Etype (Comp))
5077 and then Is_Record_Type (Underlying_Type
5078 (Scope (Etype (Comp))))
5079 then
5080 Undelay_Type (Etype (Comp));
5081 end if;
5083 Freeze_And_Append (Etype (Comp), N, Result);
5085 -- Warn for pragma Pack overriding foreign convention
5087 if Has_Foreign_Convention (Etype (Comp))
5088 and then Has_Pragma_Pack (Rec)
5090 -- Don't warn for aliased components, since override
5091 -- cannot happen in that case.
5093 and then not Is_Aliased (Comp)
5094 then
5095 declare
5096 CN : constant Name_Id :=
5097 Get_Convention_Name (Convention (Etype (Comp)));
5098 PP : constant Node_Id :=
5099 Get_Pragma (Rec, Pragma_Pack);
5100 begin
5101 if Present (PP) then
5102 Error_Msg_Name_1 := CN;
5103 Error_Msg_Sloc := Sloc (Comp);
5104 Error_Msg_N
5105 ("pragma Pack affects convention % component#??",
5106 PP);
5107 Error_Msg_Name_1 := CN;
5108 Error_Msg_NE
5109 ("\component & may not have % compatible "
5110 & "representation??", PP, Comp);
5111 end if;
5112 end;
5113 end if;
5115 -- Check for error of component clause given for variable
5116 -- sized type. We have to delay this test till this point,
5117 -- since the component type has to be frozen for us to know
5118 -- if it is variable length.
5120 if Present (CC) then
5121 Placed_Component := True;
5123 -- We omit this test in a generic context, it will be
5124 -- applied at instantiation time.
5126 if Inside_A_Generic then
5127 null;
5129 -- Also omit this test in CodePeer mode, since we do not
5130 -- have sufficient info on size and rep clauses.
5132 elsif CodePeer_Mode then
5133 null;
5135 -- Do the check
5137 elsif not
5138 Size_Known_At_Compile_Time
5139 (Underlying_Type (Etype (Comp)))
5140 then
5141 Error_Msg_N
5142 ("component clause not allowed for variable " &
5143 "length component", CC);
5144 end if;
5146 else
5147 Unplaced_Component := True;
5148 end if;
5150 -- Case of component requires byte alignment
5152 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
5154 -- Set the enclosing record to also require byte align
5156 Set_Must_Be_On_Byte_Boundary (Rec);
5158 -- Check for component clause that is inconsistent with
5159 -- the required byte boundary alignment.
5161 if Present (CC)
5162 and then Normalized_First_Bit (Comp) mod
5163 System_Storage_Unit /= 0
5164 then
5165 Error_Msg_N
5166 ("component & must be byte aligned",
5167 Component_Name (Component_Clause (Comp)));
5168 end if;
5169 end if;
5170 end;
5171 end if;
5173 -- Gather data for possible Implicit_Packing later. Note that at
5174 -- this stage we might be dealing with a real component, or with
5175 -- an implicit subtype declaration.
5177 if Known_Static_RM_Size (Etype (Comp)) then
5178 declare
5179 Comp_Type : constant Entity_Id := Etype (Comp);
5180 Comp_Size : constant Uint := RM_Size (Comp_Type);
5181 SSU : constant Int := Ttypes.System_Storage_Unit;
5183 begin
5184 Sized_Component_Total_RM_Size :=
5185 Sized_Component_Total_RM_Size + Comp_Size;
5187 Sized_Component_Total_Round_RM_Size :=
5188 Sized_Component_Total_Round_RM_Size +
5189 (Comp_Size + SSU - 1) / SSU * SSU;
5191 if Present (Underlying_Type (Comp_Type))
5192 and then Is_Elementary_Type (Underlying_Type (Comp_Type))
5193 then
5194 Elem_Component_Total_Esize :=
5195 Elem_Component_Total_Esize + Esize (Comp_Type);
5196 else
5197 All_Elem_Components := False;
5199 if Comp_Size mod SSU /= 0 then
5200 All_Storage_Unit_Components := False;
5201 end if;
5202 end if;
5203 end;
5204 else
5205 All_Sized_Components := False;
5206 end if;
5208 -- If the component is an Itype with Delayed_Freeze and is either
5209 -- a record or array subtype and its base type has not yet been
5210 -- frozen, we must remove this from the entity list of this record
5211 -- and put it on the entity list of the scope of its base type.
5212 -- Note that we know that this is not the type of a component
5213 -- since we cleared Has_Delayed_Freeze for it in the previous
5214 -- loop. Thus this must be the Designated_Type of an access type,
5215 -- which is the type of a component.
5217 if Is_Itype (Comp)
5218 and then Is_Type (Scope (Comp))
5219 and then Is_Composite_Type (Comp)
5220 and then Base_Type (Comp) /= Comp
5221 and then Has_Delayed_Freeze (Comp)
5222 and then not Is_Frozen (Base_Type (Comp))
5223 then
5224 declare
5225 Will_Be_Frozen : Boolean := False;
5226 S : Entity_Id;
5228 begin
5229 -- We have a difficult case to handle here. Suppose Rec is
5230 -- subtype being defined in a subprogram that's created as
5231 -- part of the freezing of Rec'Base. In that case, we know
5232 -- that Comp'Base must have already been frozen by the time
5233 -- we get to elaborate this because Gigi doesn't elaborate
5234 -- any bodies until it has elaborated all of the declarative
5235 -- part. But Is_Frozen will not be set at this point because
5236 -- we are processing code in lexical order.
5238 -- We detect this case by going up the Scope chain of Rec
5239 -- and seeing if we have a subprogram scope before reaching
5240 -- the top of the scope chain or that of Comp'Base. If we
5241 -- do, then mark that Comp'Base will actually be frozen. If
5242 -- so, we merely undelay it.
5244 S := Scope (Rec);
5245 while Present (S) loop
5246 if Is_Subprogram (S) then
5247 Will_Be_Frozen := True;
5248 exit;
5249 elsif S = Scope (Base_Type (Comp)) then
5250 exit;
5251 end if;
5253 S := Scope (S);
5254 end loop;
5256 if Will_Be_Frozen then
5257 Undelay_Type (Comp);
5259 else
5260 if Present (Prev) then
5261 Link_Entities (Prev, Next_Entity (Comp));
5262 else
5263 Set_First_Entity (Rec, Next_Entity (Comp));
5264 end if;
5266 -- Insert in entity list of scope of base type (which
5267 -- must be an enclosing scope, because still unfrozen).
5269 Append_Entity (Comp, Scope (Base_Type (Comp)));
5270 end if;
5271 end;
5273 -- If the component is an access type with an allocator as default
5274 -- value, the designated type will be frozen by the corresponding
5275 -- expression in init_proc. In order to place the freeze node for
5276 -- the designated type before that for the current record type,
5277 -- freeze it now.
5279 -- Same process if the component is an array of access types,
5280 -- initialized with an aggregate. If the designated type is
5281 -- private, it cannot contain allocators, and it is premature
5282 -- to freeze the type, so we check for this as well.
5284 elsif Is_Access_Type (Etype (Comp))
5285 and then Present (Parent (Comp))
5286 and then
5287 Nkind (Parent (Comp))
5288 in N_Component_Declaration | N_Discriminant_Specification
5289 and then Present (Expression (Parent (Comp)))
5290 then
5291 declare
5292 Alloc : constant Node_Id :=
5293 Unqualify (Expression (Parent (Comp)));
5295 begin
5296 if Nkind (Alloc) = N_Allocator then
5298 -- If component is pointer to a class-wide type, freeze
5299 -- the specific type in the expression being allocated.
5300 -- The expression may be a subtype indication, in which
5301 -- case freeze the subtype mark.
5303 if Is_Class_Wide_Type (Designated_Type (Etype (Comp)))
5304 then
5305 if Is_Entity_Name (Expression (Alloc)) then
5306 Freeze_And_Append
5307 (Entity (Expression (Alloc)), N, Result);
5309 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
5310 then
5311 Freeze_And_Append
5312 (Entity (Subtype_Mark (Expression (Alloc))),
5313 N, Result);
5314 end if;
5315 elsif Is_Itype (Designated_Type (Etype (Comp))) then
5316 Check_Itype (Etype (Comp));
5317 else
5318 Freeze_And_Append
5319 (Designated_Type (Etype (Comp)), N, Result);
5320 end if;
5321 end if;
5322 end;
5323 elsif Is_Access_Type (Etype (Comp))
5324 and then Is_Itype (Designated_Type (Etype (Comp)))
5325 then
5326 Check_Itype (Etype (Comp));
5328 -- Freeze the designated type when initializing a component with
5329 -- an aggregate in case the aggregate contains allocators.
5331 -- type T is ...;
5332 -- type T_Ptr is access all T;
5333 -- type T_Array is array ... of T_Ptr;
5335 -- type Rec is record
5336 -- Comp : T_Array := (others => ...);
5337 -- end record;
5339 elsif Is_Array_Type (Etype (Comp))
5340 and then Is_Access_Type (Component_Type (Etype (Comp)))
5341 then
5342 declare
5343 Comp_Par : constant Node_Id := Parent (Comp);
5344 Desig_Typ : constant Entity_Id :=
5345 Designated_Type
5346 (Component_Type (Etype (Comp)));
5348 begin
5349 -- The only case when this sort of freezing is not done is
5350 -- when the designated type is class-wide and the root type
5351 -- is the record owning the component. This scenario results
5352 -- in a circularity because the class-wide type requires
5353 -- primitives that have not been created yet as the root
5354 -- type is in the process of being frozen.
5356 -- type Rec is tagged;
5357 -- type Rec_Ptr is access all Rec'Class;
5358 -- type Rec_Array is array ... of Rec_Ptr;
5360 -- type Rec is record
5361 -- Comp : Rec_Array := (others => ...);
5362 -- end record;
5364 if Is_Class_Wide_Type (Desig_Typ)
5365 and then Root_Type (Desig_Typ) = Rec
5366 then
5367 null;
5369 elsif Is_Fully_Defined (Desig_Typ)
5370 and then Present (Comp_Par)
5371 and then Nkind (Comp_Par) = N_Component_Declaration
5372 and then Present (Expression (Comp_Par))
5373 and then Nkind (Expression (Comp_Par)) = N_Aggregate
5374 then
5375 Freeze_And_Append (Desig_Typ, N, Result);
5376 end if;
5377 end;
5378 end if;
5380 Prev := Comp;
5381 Next_Entity (Comp);
5382 end loop;
5384 SSO_ADC :=
5385 Get_Attribute_Definition_Clause
5386 (Rec, Attribute_Scalar_Storage_Order);
5388 -- If the record type has Complex_Representation, then it is treated
5389 -- as a scalar in the back end so the storage order is irrelevant.
5391 if Has_Complex_Representation (Rec) then
5392 if Present (SSO_ADC) then
5393 Error_Msg_N
5394 ("??storage order has no effect with Complex_Representation",
5395 SSO_ADC);
5396 end if;
5398 else
5399 -- Deal with default setting of reverse storage order
5401 Set_SSO_From_Default (Rec);
5403 -- Check consistent attribute setting on component types
5405 declare
5406 Comp_ADC_Present : Boolean;
5407 begin
5408 Comp := First_Component (Rec);
5409 while Present (Comp) loop
5410 Check_Component_Storage_Order
5411 (Encl_Type => Rec,
5412 Comp => Comp,
5413 ADC => SSO_ADC,
5414 Comp_ADC_Present => Comp_ADC_Present);
5415 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
5416 Next_Component (Comp);
5417 end loop;
5418 end;
5420 -- Now deal with reverse storage order/bit order issues
5422 if Present (SSO_ADC) then
5424 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
5425 -- if the former is specified.
5427 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
5429 -- Note: report error on Rec, not on SSO_ADC, as ADC may
5430 -- apply to some ancestor type.
5432 Error_Msg_Sloc := Sloc (SSO_ADC);
5433 Error_Msg_N
5434 ("scalar storage order for& specified# inconsistent with "
5435 & "bit order", Rec);
5436 end if;
5438 -- Warn if there is a Scalar_Storage_Order attribute definition
5439 -- clause but no component clause, no component that itself has
5440 -- such an attribute definition, and no pragma Pack.
5442 if not (Placed_Component
5443 or else
5444 SSO_ADC_Component
5445 or else
5446 Is_Packed (Rec))
5447 then
5448 Error_Msg_N
5449 ("??scalar storage order specified but no component "
5450 & "clause", SSO_ADC);
5451 end if;
5452 end if;
5453 end if;
5455 -- Deal with Bit_Order aspect
5457 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
5459 if Present (ADC) and then Base_Type (Rec) = Rec then
5460 if not (Placed_Component
5461 or else Present (SSO_ADC)
5462 or else Is_Packed (Rec))
5463 then
5464 -- Warn if clause has no effect when no component clause is
5465 -- present, but suppress warning if the Bit_Order is required
5466 -- due to the presence of a Scalar_Storage_Order attribute.
5468 Error_Msg_N
5469 ("??bit order specification has no effect", ADC);
5470 Error_Msg_N
5471 ("\??since no component clauses were specified", ADC);
5473 -- Here is where we do the processing to adjust component clauses
5474 -- for reversed bit order, when not using reverse SSO. If an error
5475 -- has been reported on Rec already (such as SSO incompatible with
5476 -- bit order), don't bother adjusting as this may generate extra
5477 -- noise.
5479 elsif Reverse_Bit_Order (Rec)
5480 and then not Reverse_Storage_Order (Rec)
5481 and then not Error_Posted (Rec)
5482 then
5483 Adjust_Record_For_Reverse_Bit_Order (Rec);
5485 -- Case where we have both an explicit Bit_Order and the same
5486 -- Scalar_Storage_Order: leave record untouched, the back-end
5487 -- will take care of required layout conversions.
5489 else
5490 null;
5492 end if;
5493 end if;
5495 -- Check for useless pragma Pack when all components placed. We only
5496 -- do this check for record types, not subtypes, since a subtype may
5497 -- have all its components placed, and it still makes perfectly good
5498 -- sense to pack other subtypes or the parent type. We do not give
5499 -- this warning if Optimize_Alignment is set to Space, since the
5500 -- pragma Pack does have an effect in this case (it always resets
5501 -- the alignment to one).
5503 if Ekind (Rec) = E_Record_Type
5504 and then Is_Packed (Rec)
5505 and then not Unplaced_Component
5506 and then Optimize_Alignment /= 'S'
5507 then
5508 -- Reset packed status. Probably not necessary, but we do it so
5509 -- that there is no chance of the back end doing something strange
5510 -- with this redundant indication of packing.
5512 Set_Is_Packed (Rec, False);
5514 -- Give warning if redundant constructs warnings on
5516 if Warn_On_Redundant_Constructs then
5517 Error_Msg_N -- CODEFIX
5518 ("?r?pragma Pack has no effect, no unplaced components",
5519 Get_Rep_Pragma (Rec, Name_Pack));
5520 end if;
5521 end if;
5523 -- If this is the record corresponding to a remote type, freeze the
5524 -- remote type here since that is what we are semantically freezing.
5525 -- This prevents the freeze node for that type in an inner scope.
5527 if Ekind (Rec) = E_Record_Type then
5528 if Present (Corresponding_Remote_Type (Rec)) then
5529 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
5530 end if;
5532 -- Check for controlled components, unchecked unions, and type
5533 -- invariants.
5535 Comp := First_Component (Rec);
5536 while Present (Comp) loop
5538 -- Do not set Has_Controlled_Component on a class-wide
5539 -- equivalent type. See Make_CW_Equivalent_Type.
5541 if not Is_Class_Wide_Equivalent_Type (Rec)
5542 and then
5543 (Has_Controlled_Component (Etype (Comp))
5544 or else
5545 (Chars (Comp) /= Name_uParent
5546 and then Is_Controlled (Etype (Comp)))
5547 or else
5548 (Is_Protected_Type (Etype (Comp))
5549 and then
5550 Present (Corresponding_Record_Type (Etype (Comp)))
5551 and then
5552 Has_Controlled_Component
5553 (Corresponding_Record_Type (Etype (Comp)))))
5554 then
5555 Set_Has_Controlled_Component (Rec);
5556 end if;
5558 if Has_Unchecked_Union (Etype (Comp)) then
5559 Set_Has_Unchecked_Union (Rec);
5560 end if;
5562 -- The record type requires its own invariant procedure in
5563 -- order to verify the invariant of each individual component.
5564 -- Do not consider internal components such as _parent because
5565 -- parent class-wide invariants are always inherited.
5566 -- In GNATprove mode, the component invariants are checked by
5567 -- other means. They should not be added to the record type
5568 -- invariant procedure, so that the procedure can be used to
5569 -- check the recordy type invariants if any.
5571 if Comes_From_Source (Comp)
5572 and then Has_Invariants (Etype (Comp))
5573 and then not GNATprove_Mode
5574 then
5575 Set_Has_Own_Invariants (Rec);
5576 end if;
5578 -- Scan component declaration for likely misuses of current
5579 -- instance, either in a constraint or a default expression.
5581 if Has_Per_Object_Constraint (Comp) then
5582 Check_Current_Instance (Parent (Comp));
5583 end if;
5585 Next_Component (Comp);
5586 end loop;
5587 end if;
5589 -- Enforce the restriction that access attributes with a current
5590 -- instance prefix can only apply to limited types. This comment
5591 -- is floating here, but does not seem to belong here???
5593 -- Set component alignment if not otherwise already set
5595 Set_Component_Alignment_If_Not_Set (Rec);
5597 -- For first subtypes, check if there are any fixed-point fields with
5598 -- component clauses, where we must check the size. This is not done
5599 -- till the freeze point since for fixed-point types, we do not know
5600 -- the size until the type is frozen. Similar processing applies to
5601 -- bit-packed arrays.
5603 if Is_First_Subtype (Rec) then
5604 Comp := First_Component (Rec);
5605 while Present (Comp) loop
5606 if Present (Component_Clause (Comp))
5607 and then (Is_Fixed_Point_Type (Etype (Comp))
5608 or else Is_Bit_Packed_Array (Etype (Comp)))
5609 then
5610 Check_Size
5611 (Component_Name (Component_Clause (Comp)),
5612 Etype (Comp),
5613 Esize (Comp),
5614 Junk);
5615 end if;
5617 Next_Component (Comp);
5618 end loop;
5619 end if;
5621 -- See if Size is too small as is (and implicit packing might help)
5623 if not Is_Packed (Rec)
5625 -- No implicit packing if even one component is explicitly placed
5627 and then not Placed_Component
5629 -- Or even one component is aliased
5631 and then not Aliased_Component
5633 -- Must have size clause and all sized components
5635 and then Has_Size_Clause (Rec)
5636 and then All_Sized_Components
5638 -- Do not try implicit packing on records with discriminants, too
5639 -- complicated, especially in the variant record case.
5641 and then not Has_Discriminants (Rec)
5643 -- We want to implicitly pack if the specified size of the record
5644 -- is less than the sum of the object sizes (no point in packing
5645 -- if this is not the case), if we can compute it, i.e. if we have
5646 -- only elementary components. Otherwise, we have at least one
5647 -- composite component and we want to implicitly pack only if bit
5648 -- packing is required for it, as we are sure in this case that
5649 -- the back end cannot do the expected layout without packing.
5651 and then
5652 ((All_Elem_Components
5653 and then RM_Size (Rec) < Elem_Component_Total_Esize)
5654 or else
5655 (not All_Elem_Components
5656 and then not All_Storage_Unit_Components
5657 and then RM_Size (Rec) < Sized_Component_Total_Round_RM_Size))
5659 -- And the total RM size cannot be greater than the specified size
5660 -- since otherwise packing will not get us where we have to be.
5662 and then Sized_Component_Total_RM_Size <= RM_Size (Rec)
5664 -- Never do implicit packing in CodePeer or SPARK modes since
5665 -- we don't do any packing in these modes, since this generates
5666 -- over-complex code that confuses static analysis, and in
5667 -- general, neither CodePeer not GNATprove care about the
5668 -- internal representation of objects.
5670 and then not (CodePeer_Mode or GNATprove_Mode)
5671 then
5672 -- If implicit packing enabled, do it
5674 if Implicit_Packing then
5675 Set_Is_Packed (Rec);
5677 -- Otherwise flag the size clause
5679 else
5680 declare
5681 Sz : constant Node_Id := Size_Clause (Rec);
5682 begin
5683 Error_Msg_NE -- CODEFIX
5684 ("size given for& too small", Sz, Rec);
5685 Error_Msg_N -- CODEFIX
5686 ("\use explicit pragma Pack "
5687 & "or use pragma Implicit_Packing", Sz);
5688 end;
5689 end if;
5690 end if;
5692 -- Make sure that if we have an iterator aspect, then we have
5693 -- either Constant_Indexing or Variable_Indexing.
5695 declare
5696 Iterator_Aspect : Node_Id;
5698 begin
5699 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
5701 if No (Iterator_Aspect) then
5702 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
5703 end if;
5705 if Present (Iterator_Aspect) then
5706 if Has_Aspect (Rec, Aspect_Constant_Indexing)
5707 or else
5708 Has_Aspect (Rec, Aspect_Variable_Indexing)
5709 then
5710 null;
5711 else
5712 Error_Msg_N
5713 ("Iterator_Element requires indexing aspect",
5714 Iterator_Aspect);
5715 end if;
5716 end if;
5717 end;
5719 -- All done if not a full record definition
5721 if Ekind (Rec) /= E_Record_Type then
5722 return;
5723 end if;
5725 -- Finally we need to check the variant part to make sure that
5726 -- all types within choices are properly frozen as part of the
5727 -- freezing of the record type.
5729 Check_Variant_Part : declare
5730 D : constant Node_Id := Declaration_Node (Rec);
5731 T : Node_Id;
5732 C : Node_Id;
5734 begin
5735 -- Find component list
5737 C := Empty;
5739 if Nkind (D) = N_Full_Type_Declaration then
5740 T := Type_Definition (D);
5742 if Nkind (T) = N_Record_Definition then
5743 C := Component_List (T);
5745 elsif Nkind (T) = N_Derived_Type_Definition
5746 and then Present (Record_Extension_Part (T))
5747 then
5748 C := Component_List (Record_Extension_Part (T));
5749 end if;
5750 end if;
5752 -- Case of variant part present
5754 if Present (C) and then Present (Variant_Part (C)) then
5755 Freeze_Choices_In_Variant_Part (Variant_Part (C));
5756 end if;
5758 -- Note: we used to call Check_Choices here, but it is too early,
5759 -- since predicated subtypes are frozen here, but their freezing
5760 -- actions are in Analyze_Freeze_Entity, which has not been called
5761 -- yet for entities frozen within this procedure, so we moved that
5762 -- call to the Analyze_Freeze_Entity for the record type.
5764 end Check_Variant_Part;
5766 -- Check that all the primitives of an interface type are abstract
5767 -- or null procedures.
5769 if Is_Interface (Rec)
5770 and then not Error_Posted (Parent (Rec))
5771 then
5772 declare
5773 Elmt : Elmt_Id;
5774 Subp : Entity_Id;
5776 begin
5777 Elmt := First_Elmt (Primitive_Operations (Rec));
5778 while Present (Elmt) loop
5779 Subp := Node (Elmt);
5781 if not Is_Abstract_Subprogram (Subp)
5783 -- Avoid reporting the error on inherited primitives
5785 and then Comes_From_Source (Subp)
5786 then
5787 Error_Msg_Name_1 := Chars (Subp);
5789 if Ekind (Subp) = E_Procedure then
5790 if not Null_Present (Parent (Subp)) then
5791 Error_Msg_N
5792 ("interface procedure % must be abstract or null",
5793 Parent (Subp));
5794 end if;
5795 else
5796 Error_Msg_N
5797 ("interface function % must be abstract",
5798 Parent (Subp));
5799 end if;
5800 end if;
5802 Next_Elmt (Elmt);
5803 end loop;
5804 end;
5805 end if;
5807 -- For a derived tagged type, check whether inherited primitives
5808 -- might require a wrapper to handle class-wide conditions.
5810 if Is_Tagged_Type (Rec) and then Is_Derived_Type (Rec) then
5811 Check_Inherited_Conditions (Rec);
5812 end if;
5813 end Freeze_Record_Type;
5815 -------------------------------
5816 -- Has_Boolean_Aspect_Import --
5817 -------------------------------
5819 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
5820 Decl : constant Node_Id := Declaration_Node (E);
5821 Asp : Node_Id;
5822 Expr : Node_Id;
5824 begin
5825 if Has_Aspects (Decl) then
5826 Asp := First (Aspect_Specifications (Decl));
5827 while Present (Asp) loop
5828 Expr := Expression (Asp);
5830 -- The value of aspect Import is True when the expression is
5831 -- either missing or it is explicitly set to True.
5833 if Get_Aspect_Id (Asp) = Aspect_Import
5834 and then (No (Expr)
5835 or else (Compile_Time_Known_Value (Expr)
5836 and then Is_True (Expr_Value (Expr))))
5837 then
5838 return True;
5839 end if;
5841 Next (Asp);
5842 end loop;
5843 end if;
5845 return False;
5846 end Has_Boolean_Aspect_Import;
5848 -------------------------
5849 -- Inherit_Freeze_Node --
5850 -------------------------
5852 procedure Inherit_Freeze_Node
5853 (Fnod : Node_Id;
5854 Typ : Entity_Id)
5856 Typ_Fnod : constant Node_Id := Freeze_Node (Typ);
5858 begin
5859 Set_Freeze_Node (Typ, Fnod);
5860 Set_Entity (Fnod, Typ);
5862 -- The input type had an existing node. Propagate relevant attributes
5863 -- from the old freeze node to the inherited freeze node.
5865 -- ??? if both freeze nodes have attributes, would they differ?
5867 if Present (Typ_Fnod) then
5869 -- Attribute Access_Types_To_Process
5871 if Present (Access_Types_To_Process (Typ_Fnod))
5872 and then No (Access_Types_To_Process (Fnod))
5873 then
5874 Set_Access_Types_To_Process (Fnod,
5875 Access_Types_To_Process (Typ_Fnod));
5876 end if;
5878 -- Attribute Actions
5880 if Present (Actions (Typ_Fnod)) and then No (Actions (Fnod)) then
5881 Set_Actions (Fnod, Actions (Typ_Fnod));
5882 end if;
5884 -- Attribute First_Subtype_Link
5886 if Present (First_Subtype_Link (Typ_Fnod))
5887 and then No (First_Subtype_Link (Fnod))
5888 then
5889 Set_First_Subtype_Link (Fnod, First_Subtype_Link (Typ_Fnod));
5890 end if;
5892 -- Attribute TSS_Elist
5894 if Present (TSS_Elist (Typ_Fnod))
5895 and then No (TSS_Elist (Fnod))
5896 then
5897 Set_TSS_Elist (Fnod, TSS_Elist (Typ_Fnod));
5898 end if;
5899 end if;
5900 end Inherit_Freeze_Node;
5902 ------------------------------
5903 -- Wrap_Imported_Subprogram --
5904 ------------------------------
5906 -- The issue here is that our normal approach of checking preconditions
5907 -- and postconditions does not work for imported procedures, since we
5908 -- are not generating code for the body. To get around this we create
5909 -- a wrapper, as shown by the following example:
5911 -- procedure K (A : Integer);
5912 -- pragma Import (C, K);
5914 -- The spec is rewritten by removing the effects of pragma Import, but
5915 -- leaving the convention unchanged, as though the source had said:
5917 -- procedure K (A : Integer);
5918 -- pragma Convention (C, K);
5920 -- and we create a body, added to the entity K freeze actions, which
5921 -- looks like:
5923 -- procedure K (A : Integer) is
5924 -- procedure K (A : Integer);
5925 -- pragma Import (C, K);
5926 -- begin
5927 -- K (A);
5928 -- end K;
5930 -- Now the contract applies in the normal way to the outer procedure,
5931 -- and the inner procedure has no contracts, so there is no problem
5932 -- in just calling it to get the original effect.
5934 -- In the case of a function, we create an appropriate return statement
5935 -- for the subprogram body that calls the inner procedure.
5937 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
5938 function Copy_Import_Pragma return Node_Id;
5939 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
5941 ------------------------
5942 -- Copy_Import_Pragma --
5943 ------------------------
5945 function Copy_Import_Pragma return Node_Id is
5947 -- The subprogram should have an import pragma, otherwise it does
5948 -- need a wrapper.
5950 Prag : constant Node_Id := Import_Pragma (E);
5951 pragma Assert (Present (Prag));
5953 -- Save all semantic fields of the pragma
5955 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
5956 Save_From : constant Boolean := From_Aspect_Specification (Prag);
5957 Save_Prag : constant Node_Id := Next_Pragma (Prag);
5958 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
5960 Result : Node_Id;
5962 begin
5963 -- Reset all semantic fields. This avoids a potential infinite
5964 -- loop when the pragma comes from an aspect as the duplication
5965 -- will copy the aspect, then copy the corresponding pragma and
5966 -- so on.
5968 Set_Corresponding_Aspect (Prag, Empty);
5969 Set_From_Aspect_Specification (Prag, False);
5970 Set_Next_Pragma (Prag, Empty);
5971 Set_Next_Rep_Item (Prag, Empty);
5973 Result := Copy_Separate_Tree (Prag);
5975 -- Restore the original semantic fields
5977 Set_Corresponding_Aspect (Prag, Save_Asp);
5978 Set_From_Aspect_Specification (Prag, Save_From);
5979 Set_Next_Pragma (Prag, Save_Prag);
5980 Set_Next_Rep_Item (Prag, Save_Rep);
5982 return Result;
5983 end Copy_Import_Pragma;
5985 -- Local variables
5987 Loc : constant Source_Ptr := Sloc (E);
5988 CE : constant Name_Id := Chars (E);
5989 Bod : Node_Id;
5990 Forml : Entity_Id;
5991 Parms : List_Id;
5992 Prag : Node_Id;
5993 Spec : Node_Id;
5994 Stmt : Node_Id;
5996 -- Start of processing for Wrap_Imported_Subprogram
5998 begin
5999 -- Nothing to do if not imported
6001 if not Is_Imported (E) then
6002 return;
6004 -- Test enabling conditions for wrapping
6006 elsif Is_Subprogram (E)
6007 and then Present (Contract (E))
6008 and then Present (Pre_Post_Conditions (Contract (E)))
6009 and then not GNATprove_Mode
6010 then
6011 -- Here we do the wrap
6013 Prag := Copy_Import_Pragma;
6015 -- Fix up spec so it is no longer imported and has convention Ada
6017 Set_Has_Completion (E, False);
6018 Set_Import_Pragma (E, Empty);
6019 Set_Interface_Name (E, Empty);
6020 Set_Is_Imported (E, False);
6021 Set_Convention (E, Convention_Ada);
6023 -- Grab the subprogram declaration and specification
6025 Spec := Declaration_Node (E);
6027 -- Build parameter list that we need
6029 Parms := New_List;
6030 Forml := First_Formal (E);
6031 while Present (Forml) loop
6032 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
6033 Next_Formal (Forml);
6034 end loop;
6036 -- Build the call
6038 -- An imported function whose result type is anonymous access
6039 -- creates a new anonymous access type when it is relocated into
6040 -- the declarations of the body generated below. As a result, the
6041 -- accessibility level of these two anonymous access types may not
6042 -- be compatible even though they are essentially the same type.
6043 -- Use an unchecked type conversion to reconcile this case. Note
6044 -- that the conversion is safe because in the named access type
6045 -- case, both the body and imported function utilize the same
6046 -- type.
6048 if Ekind (E) in E_Function | E_Generic_Function then
6049 Stmt :=
6050 Make_Simple_Return_Statement (Loc,
6051 Expression =>
6052 Unchecked_Convert_To (Etype (E),
6053 Make_Function_Call (Loc,
6054 Name => Make_Identifier (Loc, CE),
6055 Parameter_Associations => Parms)));
6057 else
6058 Stmt :=
6059 Make_Procedure_Call_Statement (Loc,
6060 Name => Make_Identifier (Loc, CE),
6061 Parameter_Associations => Parms);
6062 end if;
6064 -- Now build the body
6066 Bod :=
6067 Make_Subprogram_Body (Loc,
6068 Specification => Copy_Subprogram_Spec (Spec),
6069 Declarations => New_List (
6070 Make_Subprogram_Declaration (Loc,
6071 Specification => Copy_Subprogram_Spec (Spec)),
6072 Prag),
6073 Handled_Statement_Sequence =>
6074 Make_Handled_Sequence_Of_Statements (Loc,
6075 Statements => New_List (Stmt),
6076 End_Label => Make_Identifier (Loc, CE)));
6078 -- Append the body to freeze result
6080 Add_To_Result (Bod);
6081 return;
6083 -- Case of imported subprogram that does not get wrapped
6085 else
6086 -- Set Is_Public. All imported entities need an external symbol
6087 -- created for them since they are always referenced from another
6088 -- object file. Note this used to be set when we set Is_Imported
6089 -- back in Sem_Prag, but now we delay it to this point, since we
6090 -- don't want to set this flag if we wrap an imported subprogram.
6092 Set_Is_Public (E);
6093 end if;
6094 end Wrap_Imported_Subprogram;
6096 -- Start of processing for Freeze_Entity
6098 begin
6099 -- The entity being frozen may be subject to pragma Ghost. Set the mode
6100 -- now to ensure that any nodes generated during freezing are properly
6101 -- flagged as Ghost.
6103 Set_Ghost_Mode (E);
6105 -- We are going to test for various reasons why this entity need not be
6106 -- frozen here, but in the case of an Itype that's defined within a
6107 -- record, that test actually applies to the record.
6109 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
6110 Test_E := Scope (E);
6112 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
6113 and then Is_Record_Type (Underlying_Type (Scope (E)))
6114 then
6115 Test_E := Underlying_Type (Scope (E));
6116 end if;
6118 -- Do not freeze if already frozen since we only need one freeze node
6120 if Is_Frozen (E) then
6122 if Is_Itype (E)
6123 and then not Is_Base_Type (E)
6124 and then not Is_Frozen (Etype (E))
6125 then
6126 -- If a frozen subtype of an unfrozen type seems impossible
6127 -- then see Analyze_Protected_Definition.Undelay_Itypes.
6129 Result := Freeze_Entity
6130 (Etype (E), N, Do_Freeze_Profile => Do_Freeze_Profile);
6131 else
6132 Result := No_List;
6133 end if;
6135 goto Leave;
6137 -- Do not freeze if we are preanalyzing without freezing
6139 elsif Inside_Preanalysis_Without_Freezing > 0 then
6140 Result := No_List;
6141 goto Leave;
6143 elsif Ekind (E) = E_Generic_Package then
6144 Result := Freeze_Generic_Entities (E);
6145 goto Leave;
6147 -- It is improper to freeze an external entity within a generic because
6148 -- its freeze node will appear in a non-valid context. The entity will
6149 -- be frozen in the proper scope after the current generic is analyzed.
6150 -- However, aspects must be analyzed because they may be queried later
6151 -- within the generic itself, and the corresponding pragma or attribute
6152 -- definition has not been analyzed yet. After this, indicate that the
6153 -- entity has no further delayed aspects, to prevent a later aspect
6154 -- analysis out of the scope of the generic.
6156 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
6157 if Has_Delayed_Aspects (E) then
6158 Analyze_Aspects_At_Freeze_Point (E);
6159 Set_Has_Delayed_Aspects (E, False);
6160 end if;
6162 Result := No_List;
6163 goto Leave;
6165 -- AI05-0213: A formal incomplete type does not freeze the actual. In
6166 -- the instance, the same applies to the subtype renaming the actual.
6168 elsif Is_Private_Type (E)
6169 and then Is_Generic_Actual_Type (E)
6170 and then No (Full_View (Base_Type (E)))
6171 and then Ada_Version >= Ada_2012
6172 then
6173 Result := No_List;
6174 goto Leave;
6176 -- Formal subprograms are never frozen
6178 elsif Is_Formal_Subprogram (E) then
6179 Result := No_List;
6180 goto Leave;
6182 -- Generic types are never frozen as they lack delayed semantic checks
6184 elsif Is_Generic_Type (E) then
6185 Result := No_List;
6186 goto Leave;
6188 -- Do not freeze a global entity within an inner scope created during
6189 -- expansion. A call to subprogram E within some internal procedure
6190 -- (a stream attribute for example) might require freezing E, but the
6191 -- freeze node must appear in the same declarative part as E itself.
6192 -- The two-pass elaboration mechanism in gigi guarantees that E will
6193 -- be frozen before the inner call is elaborated. We exclude constants
6194 -- from this test, because deferred constants may be frozen early, and
6195 -- must be diagnosed (e.g. in the case of a deferred constant being used
6196 -- in a default expression). If the enclosing subprogram comes from
6197 -- source, or is a generic instance, then the freeze point is the one
6198 -- mandated by the language, and we freeze the entity. A subprogram that
6199 -- is a child unit body that acts as a spec does not have a spec that
6200 -- comes from source, but can only come from source.
6202 elsif In_Open_Scopes (Scope (Test_E))
6203 and then Scope (Test_E) /= Current_Scope
6204 and then Ekind (Test_E) /= E_Constant
6205 then
6206 -- Here we deal with the special case of the expansion of
6207 -- postconditions. Previously this was handled by the loop below,
6208 -- since these postcondition checks got isolated to a separate,
6209 -- internally generated, subprogram. Now, however, the postcondition
6210 -- checks get contained within their corresponding subprogram
6211 -- directly.
6213 if not Comes_From_Source (N)
6214 and then Nkind (N) = N_Pragma
6215 and then From_Aspect_Specification (N)
6216 and then Is_Valid_Assertion_Kind (Original_Aspect_Pragma_Name (N))
6218 -- Now, verify the placement of the pragma is within an expanded
6219 -- subprogram which contains postcondition expansion - detected
6220 -- through the presence of the "Wrapped_Statements" field.
6222 and then Present (Enclosing_Subprogram (Current_Scope))
6223 and then Present (Wrapped_Statements
6224 (Enclosing_Subprogram (Current_Scope)))
6225 then
6226 goto Leave;
6227 end if;
6229 -- Otherwise, loop through scopes checking if an enclosing scope
6230 -- comes from source or is a generic. Note that, for the purpose
6231 -- of this test, we need to consider that the internally generated
6232 -- subprogram described above comes from source too if the original
6233 -- subprogram itself does.
6235 declare
6236 S : Entity_Id;
6238 begin
6239 S := Current_Scope;
6240 while Present (S) loop
6241 if Is_Overloadable (S) then
6242 if Comes_From_Source (S)
6243 or else (Chars (S) = Name_uWrapped_Statements
6244 and then Comes_From_Source (Scope (S)))
6245 or else Is_Generic_Instance (S)
6246 or else Is_Child_Unit (S)
6247 then
6248 exit;
6249 else
6250 Result := No_List;
6251 goto Leave;
6252 end if;
6253 end if;
6255 S := Scope (S);
6256 end loop;
6257 end;
6259 -- Similarly, an inlined instance body may make reference to global
6260 -- entities, but these references cannot be the proper freezing point
6261 -- for them, and in the absence of inlining freezing will take place in
6262 -- their own scope. Normally instance bodies are analyzed after the
6263 -- enclosing compilation, and everything has been frozen at the proper
6264 -- place, but with front-end inlining an instance body is compiled
6265 -- before the end of the enclosing scope, and as a result out-of-order
6266 -- freezing must be prevented.
6268 elsif Front_End_Inlining
6269 and then In_Instance_Body
6270 and then Present (Scope (Test_E))
6271 then
6272 declare
6273 S : Entity_Id;
6275 begin
6276 S := Scope (Test_E);
6277 while Present (S) loop
6278 if Is_Generic_Instance (S) then
6279 exit;
6280 else
6281 S := Scope (S);
6282 end if;
6283 end loop;
6285 if No (S) then
6286 Result := No_List;
6287 goto Leave;
6288 end if;
6289 end;
6290 end if;
6292 -- Add checks to detect proper initialization of scalars that may appear
6293 -- as subprogram parameters.
6295 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
6296 Apply_Parameter_Validity_Checks (E);
6297 end if;
6299 -- Deal with delayed aspect specifications. The analysis of the aspect
6300 -- is required to be delayed to the freeze point, thus we analyze the
6301 -- pragma or attribute definition clause in the tree at this point. We
6302 -- also analyze the aspect specification node at the freeze point when
6303 -- the aspect doesn't correspond to pragma/attribute definition clause.
6304 -- In addition, a derived type may have inherited aspects that were
6305 -- delayed in the parent, so these must also be captured now.
6307 -- For a record type, we deal with the delayed aspect specifications on
6308 -- components first, which is consistent with the non-delayed case and
6309 -- makes it possible to have a single processing to detect conflicts.
6311 if Is_Record_Type (E) then
6312 declare
6313 Comp : Entity_Id;
6315 Rec_Pushed : Boolean := False;
6316 -- Set True if the record type E has been pushed on the scope
6317 -- stack. Needed for the analysis of delayed aspects specified
6318 -- to the components of Rec.
6320 begin
6321 Comp := First_Component (E);
6322 while Present (Comp) loop
6323 if Has_Delayed_Aspects (Comp) then
6324 if not Rec_Pushed then
6325 Push_Scope (E);
6326 Rec_Pushed := True;
6328 -- The visibility to the discriminants must be restored
6329 -- in order to properly analyze the aspects.
6331 if Has_Discriminants (E) then
6332 Install_Discriminants (E);
6333 end if;
6334 end if;
6336 Analyze_Aspects_At_Freeze_Point (Comp);
6337 end if;
6339 Next_Component (Comp);
6340 end loop;
6342 -- Pop the scope if Rec scope has been pushed on the scope stack
6343 -- during the delayed aspect analysis process.
6345 if Rec_Pushed then
6346 if Has_Discriminants (E) then
6347 Uninstall_Discriminants (E);
6348 end if;
6350 Pop_Scope;
6351 end if;
6352 end;
6353 end if;
6355 if Has_Delayed_Aspects (E) then
6356 Analyze_Aspects_At_Freeze_Point (E);
6357 end if;
6359 -- Here to freeze the entity
6361 Set_Is_Frozen (E);
6363 -- Case of entity being frozen is other than a type
6365 if not Is_Type (E) then
6367 -- If entity is exported or imported and does not have an external
6368 -- name, now is the time to provide the appropriate default name.
6369 -- Skip this if the entity is stubbed, since we don't need a name
6370 -- for any stubbed routine. For the case on intrinsics, if no
6371 -- external name is specified, then calls will be handled in
6372 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
6373 -- external name is provided, then Expand_Intrinsic_Call leaves
6374 -- calls in place for expansion by GIGI.
6376 if (Is_Imported (E) or else Is_Exported (E))
6377 and then No (Interface_Name (E))
6378 and then Convention (E) /= Convention_Stubbed
6379 and then Convention (E) /= Convention_Intrinsic
6380 then
6381 Set_Encoded_Interface_Name
6382 (E, Get_Default_External_Name (E));
6383 end if;
6385 -- Subprogram case
6387 if Is_Subprogram (E) then
6389 -- Check for needing to wrap imported subprogram
6391 if not Inside_A_Generic then
6392 Wrap_Imported_Subprogram (E);
6393 end if;
6395 -- Freeze all parameter types and the return type (RM 13.14(14)).
6396 -- However skip this for internal subprograms. This is also where
6397 -- any extra formal parameters are created since we now know
6398 -- whether the subprogram will use a foreign convention.
6400 -- In Ada 2012, freezing a subprogram does not always freeze the
6401 -- corresponding profile (see AI05-019). An attribute reference
6402 -- is not a freezing point of the profile. Similarly, we do not
6403 -- freeze the profile of primitives of a library-level tagged type
6404 -- when we are building its dispatch table. Flag Do_Freeze_Profile
6405 -- indicates whether the profile should be frozen now.
6407 -- This processing doesn't apply to internal entities (see below)
6409 if not Is_Internal (E) and then Do_Freeze_Profile then
6410 if not Freeze_Profile (E) then
6411 goto Leave;
6412 end if;
6413 end if;
6415 -- Must freeze its parent first if it is a derived subprogram
6417 if Present (Alias (E)) then
6418 Freeze_And_Append (Alias (E), N, Result);
6419 end if;
6421 -- We don't freeze internal subprograms, because we don't normally
6422 -- want addition of extra formals or mechanism setting to happen
6423 -- for those. However we do pass through predefined dispatching
6424 -- cases, since extra formals may be needed in some cases, such as
6425 -- for the stream 'Input function (build-in-place formals).
6427 if not Is_Internal (E)
6428 or else Is_Predefined_Dispatching_Operation (E)
6429 then
6430 Freeze_Subprogram (E);
6431 end if;
6433 -- If warning on suspicious contracts then check for the case of
6434 -- a postcondition other than False for a No_Return subprogram.
6436 if No_Return (E)
6437 and then Warn_On_Suspicious_Contract
6438 and then Present (Contract (E))
6439 then
6440 declare
6441 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
6442 Exp : Node_Id;
6444 begin
6445 while Present (Prag) loop
6446 if Pragma_Name_Unmapped (Prag) in Name_Post
6447 | Name_Postcondition
6448 | Name_Refined_Post
6449 then
6450 Exp :=
6451 Expression
6452 (First (Pragma_Argument_Associations (Prag)));
6454 if Nkind (Exp) /= N_Identifier
6455 or else Chars (Exp) /= Name_False
6456 then
6457 Error_Msg_NE
6458 ("useless postcondition, & is marked "
6459 & "No_Return?.t?", Exp, E);
6460 end if;
6461 end if;
6463 Prag := Next_Pragma (Prag);
6464 end loop;
6465 end;
6466 end if;
6468 -- Here for other than a subprogram or type
6470 else
6471 -- If entity has a type declared in the current scope, and it is
6472 -- not a generic unit, then freeze it first.
6474 if Present (Etype (E))
6475 and then Ekind (E) /= E_Generic_Function
6476 and then Within_Scope (Etype (E), Current_Scope)
6477 then
6478 Freeze_And_Append (Etype (E), N, Result);
6480 -- For an object of an anonymous array type, aspects on the
6481 -- object declaration apply to the type itself. This is the
6482 -- case for Atomic_Components, Volatile_Components, and
6483 -- Independent_Components. In these cases analysis of the
6484 -- generated pragma will mark the anonymous types accordingly,
6485 -- and the object itself does not require a freeze node.
6487 if Ekind (E) = E_Variable
6488 and then Is_Itype (Etype (E))
6489 and then Is_Array_Type (Etype (E))
6490 and then Has_Delayed_Aspects (E)
6491 then
6492 Set_Has_Delayed_Aspects (E, False);
6493 Set_Has_Delayed_Freeze (E, False);
6494 Set_Freeze_Node (E, Empty);
6495 end if;
6496 end if;
6498 -- Special processing for objects created by object declaration;
6499 -- we protect the call to Declaration_Node against entities of
6500 -- expressions replaced by the frontend with an N_Raise_CE node.
6502 if Ekind (E) in E_Constant | E_Variable
6503 and then Nkind (Declaration_Node (E)) = N_Object_Declaration
6504 then
6505 Freeze_Object_Declaration (E);
6506 end if;
6508 -- Check that a constant which has a pragma Volatile[_Components]
6509 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
6511 -- Note: Atomic[_Components] also sets Volatile[_Components]
6513 if Ekind (E) = E_Constant
6514 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
6515 and then not Is_Imported (E)
6516 and then not Has_Boolean_Aspect_Import (E)
6517 then
6518 -- Make sure we actually have a pragma, and have not merely
6519 -- inherited the indication from elsewhere (e.g. an address
6520 -- clause, which is not good enough in RM terms).
6522 if Has_Rep_Pragma (E, Name_Atomic)
6523 or else
6524 Has_Rep_Pragma (E, Name_Atomic_Components)
6525 then
6526 Error_Msg_N
6527 ("standalone atomic constant must be " &
6528 "imported (RM C.6(13))", E);
6530 elsif Has_Rep_Pragma (E, Name_Volatile)
6531 or else
6532 Has_Rep_Pragma (E, Name_Volatile_Components)
6533 then
6534 Error_Msg_N
6535 ("standalone volatile constant must be " &
6536 "imported (RM C.6(13))", E);
6537 end if;
6538 end if;
6540 -- Static objects require special handling
6542 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
6543 and then Is_Statically_Allocated (E)
6544 then
6545 Freeze_Static_Object (E);
6546 end if;
6548 -- Remaining step is to layout objects
6550 if Ekind (E) in E_Variable | E_Constant | E_Loop_Parameter
6551 or else Is_Formal (E)
6552 then
6553 Layout_Object (E);
6554 end if;
6556 -- For an object that does not have delayed freezing, and whose
6557 -- initialization actions have been captured in a compound
6558 -- statement, move them back now directly within the enclosing
6559 -- statement sequence.
6561 if Ekind (E) in E_Constant | E_Variable
6562 and then not Has_Delayed_Freeze (E)
6563 then
6564 Explode_Initialization_Compound_Statement (E);
6565 end if;
6567 -- Do not generate a freeze node for a generic unit
6569 if Is_Generic_Unit (E) then
6570 Result := No_List;
6571 goto Leave;
6572 end if;
6573 end if;
6575 -- Case of a type or subtype being frozen
6577 else
6578 -- Verify several SPARK legality rules related to Ghost types now
6579 -- that the type is frozen.
6581 Check_Ghost_Type (E);
6583 -- We used to check here that a full type must have preelaborable
6584 -- initialization if it completes a private type specified with
6585 -- pragma Preelaborable_Initialization, but that missed cases where
6586 -- the types occur within a generic package, since the freezing
6587 -- that occurs within a containing scope generally skips traversal
6588 -- of a generic unit's declarations (those will be frozen within
6589 -- instances). This check was moved to Analyze_Package_Specification.
6591 -- The type may be defined in a generic unit. This can occur when
6592 -- freezing a generic function that returns the type (which is
6593 -- defined in a parent unit). It is clearly meaningless to freeze
6594 -- this type. However, if it is a subtype, its size may be determi-
6595 -- nable and used in subsequent checks, so might as well try to
6596 -- compute it.
6598 -- In Ada 2012, Freeze_Entities is also used in the front end to
6599 -- trigger the analysis of aspect expressions, so in this case we
6600 -- want to continue the freezing process.
6602 -- Is_Generic_Unit (Scope (E)) is dubious here, do we want instead
6603 -- In_Generic_Scope (E)???
6605 if Present (Scope (E))
6606 and then Is_Generic_Unit (Scope (E))
6607 and then
6608 (not Has_Predicates (E)
6609 and then not Has_Delayed_Freeze (E))
6610 then
6611 Check_Compile_Time_Size (E);
6612 Result := No_List;
6613 goto Leave;
6614 end if;
6616 -- Check for error of Type_Invariant'Class applied to an untagged
6617 -- type (check delayed to freeze time when full type is available).
6619 declare
6620 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
6621 begin
6622 if Present (Prag)
6623 and then Class_Present (Prag)
6624 and then not Is_Tagged_Type (E)
6625 then
6626 Error_Msg_NE
6627 ("Type_Invariant''Class cannot be specified for &", Prag, E);
6628 Error_Msg_N
6629 ("\can only be specified for a tagged type", Prag);
6630 end if;
6631 end;
6633 -- Deal with special cases of freezing for subtype
6635 if E /= Base_Type (E) then
6637 -- Before we do anything else, a specific test for the case of a
6638 -- size given for an array where the array would need to be packed
6639 -- in order for the size to be honored, but is not. This is the
6640 -- case where implicit packing may apply. The reason we do this so
6641 -- early is that, if we have implicit packing, the layout of the
6642 -- base type is affected, so we must do this before we freeze the
6643 -- base type.
6645 -- We could do this processing only if implicit packing is enabled
6646 -- since in all other cases, the error would be caught by the back
6647 -- end. However, we choose to do the check even if we do not have
6648 -- implicit packing enabled, since this allows us to give a more
6649 -- useful error message (advising use of pragma Implicit_Packing
6650 -- or pragma Pack).
6652 if Is_Array_Type (E) then
6653 declare
6654 Ctyp : constant Entity_Id := Component_Type (E);
6655 Rsiz : constant Uint :=
6656 (if Known_RM_Size (Ctyp) then RM_Size (Ctyp) else Uint_0);
6657 SZ : constant Node_Id := Size_Clause (E);
6658 Btyp : constant Entity_Id := Base_Type (E);
6660 Lo : Node_Id;
6661 Hi : Node_Id;
6662 Indx : Node_Id;
6664 Dim : Uint;
6665 Num_Elmts : Uint := Uint_1;
6666 -- Number of elements in array
6668 begin
6669 -- Check enabling conditions. These are straightforward
6670 -- except for the test for a limited composite type. This
6671 -- eliminates the rare case of a array of limited components
6672 -- where there are issues of whether or not we can go ahead
6673 -- and pack the array (since we can't freely pack and unpack
6674 -- arrays if they are limited).
6676 -- Note that we check the root type explicitly because the
6677 -- whole point is we are doing this test before we have had
6678 -- a chance to freeze the base type (and it is that freeze
6679 -- action that causes stuff to be inherited).
6681 -- The conditions on the size are identical to those used in
6682 -- Freeze_Array_Type to set the Is_Packed flag.
6684 if Has_Size_Clause (E)
6685 and then Known_Static_RM_Size (E)
6686 and then not Is_Packed (E)
6687 and then not Has_Pragma_Pack (E)
6688 and then not Has_Component_Size_Clause (E)
6689 and then Known_Static_RM_Size (Ctyp)
6690 and then Rsiz <= System_Max_Integer_Size
6691 and then not (Addressable (Rsiz)
6692 and then Known_Static_Esize (Ctyp)
6693 and then Esize (Ctyp) = Rsiz)
6694 and then not (Rsiz mod System_Storage_Unit = 0
6695 and then Is_Composite_Type (Ctyp))
6696 and then not Is_Limited_Composite (E)
6697 and then not Is_Packed (Root_Type (E))
6698 and then not Has_Component_Size_Clause (Root_Type (E))
6699 and then not (CodePeer_Mode or GNATprove_Mode)
6700 then
6701 -- Compute number of elements in array
6703 Indx := First_Index (E);
6704 while Present (Indx) loop
6705 Get_Index_Bounds (Indx, Lo, Hi);
6707 if not (Compile_Time_Known_Value (Lo)
6708 and then
6709 Compile_Time_Known_Value (Hi))
6710 then
6711 goto No_Implicit_Packing;
6712 end if;
6714 Dim := Expr_Value (Hi) - Expr_Value (Lo) + 1;
6716 if Dim > Uint_0 then
6717 Num_Elmts := Num_Elmts * Dim;
6718 else
6719 Num_Elmts := Uint_0;
6720 end if;
6722 Next_Index (Indx);
6723 end loop;
6725 -- What we are looking for here is the situation where
6726 -- the RM_Size given would be exactly right if there was
6727 -- a pragma Pack, resulting in the component size being
6728 -- the RM_Size of the component type.
6730 if RM_Size (E) = Num_Elmts * Rsiz then
6732 -- For implicit packing mode, just set the component
6733 -- size and Freeze_Array_Type will do the rest.
6735 if Implicit_Packing then
6736 Set_Component_Size (Btyp, Rsiz);
6738 -- Otherwise give an error message, except that if the
6739 -- specified Size is zero, there is no need for pragma
6740 -- Pack. Note that size zero is not considered
6741 -- Addressable.
6743 elsif RM_Size (E) /= Uint_0 then
6744 Error_Msg_NE
6745 ("size given for& too small", SZ, E);
6746 Error_Msg_N -- CODEFIX
6747 ("\use explicit pragma Pack or use pragma "
6748 & "Implicit_Packing", SZ);
6749 end if;
6750 end if;
6751 end if;
6752 end;
6753 end if;
6755 <<No_Implicit_Packing>>
6757 -- If ancestor subtype present, freeze that first. Note that this
6758 -- will also get the base type frozen. Need RM reference ???
6760 Atype := Ancestor_Subtype (E);
6762 if Present (Atype) then
6763 Freeze_And_Append (Atype, N, Result);
6765 -- No ancestor subtype present
6767 else
6768 -- See if we have a nearest ancestor that has a predicate.
6769 -- That catches the case of derived type with a predicate.
6770 -- Need RM reference here ???
6772 Atype := Nearest_Ancestor (E);
6774 if Present (Atype) and then Has_Predicates (Atype) then
6775 Freeze_And_Append (Atype, N, Result);
6776 end if;
6778 -- Freeze base type before freezing the entity (RM 13.14(15))
6780 if E /= Base_Type (E) then
6781 Freeze_And_Append (Base_Type (E), N, Result);
6782 end if;
6783 end if;
6785 -- A subtype inherits all the type-related representation aspects
6786 -- from its parents (RM 13.1(8)).
6788 if May_Inherit_Delayed_Rep_Aspects (E) then
6789 Inherit_Delayed_Rep_Aspects (E);
6790 end if;
6792 Inherit_Aspects_At_Freeze_Point (E);
6794 -- For a derived type, freeze its parent type first (RM 13.14(15))
6796 elsif Is_Derived_Type (E) then
6797 Freeze_And_Append (Etype (E), N, Result);
6799 -- A derived type inherits each type-related representation aspect
6800 -- of its parent type that was directly specified before the
6801 -- declaration of the derived type (RM 13.1(15)).
6803 if May_Inherit_Delayed_Rep_Aspects (E) then
6804 Inherit_Delayed_Rep_Aspects (E);
6805 end if;
6807 Inherit_Aspects_At_Freeze_Point (E);
6808 end if;
6810 -- Case of array type
6812 if Is_Array_Type (E) then
6813 Freeze_Array_Type (E);
6814 end if;
6816 -- Check for incompatible size and alignment for array/record type
6818 if Warn_On_Size_Alignment
6819 and then (Is_Array_Type (E) or else Is_Record_Type (E))
6820 and then Has_Size_Clause (E)
6821 and then Has_Alignment_Clause (E)
6823 -- If explicit Object_Size clause given assume that the programmer
6824 -- knows what he is doing, and expects the compiler behavior.
6826 and then not Has_Object_Size_Clause (E)
6828 -- It does not really make sense to warn for the minimum alignment
6829 -- since the programmer could not get rid of the warning.
6831 and then Alignment (E) > 1
6833 -- Check for size not a multiple of alignment
6835 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
6836 then
6837 declare
6838 SC : constant Node_Id := Size_Clause (E);
6839 AC : constant Node_Id := Alignment_Clause (E);
6840 Loc : Node_Id;
6841 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
6843 begin
6844 if Present (SC) and then Present (AC) then
6846 -- Give a warning
6848 if Sloc (SC) > Sloc (AC) then
6849 Loc := SC;
6850 Error_Msg_NE
6851 ("?.z?size is not a multiple of alignment for &",
6852 Loc, E);
6853 Error_Msg_Sloc := Sloc (AC);
6854 Error_Msg_Uint_1 := Alignment (E);
6855 Error_Msg_N ("\?.z?alignment of ^ specified #", Loc);
6857 else
6858 Loc := AC;
6859 Error_Msg_NE
6860 ("?.z?size is not a multiple of alignment for &",
6861 Loc, E);
6862 Error_Msg_Sloc := Sloc (SC);
6863 Error_Msg_Uint_1 := RM_Size (E);
6864 Error_Msg_N ("\?.z?size of ^ specified #", Loc);
6865 end if;
6867 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
6868 Error_Msg_N ("\?.z?Object_Size will be increased to ^", Loc);
6869 end if;
6870 end;
6871 end if;
6873 -- For a class-wide type, the corresponding specific type is
6874 -- frozen as well (RM 13.14(15))
6876 if Is_Class_Wide_Type (E) then
6877 Freeze_And_Append (Root_Type (E), N, Result);
6879 -- If the base type of the class-wide type is still incomplete,
6880 -- the class-wide remains unfrozen as well. This is legal when
6881 -- E is the formal of a primitive operation of some other type
6882 -- which is being frozen.
6884 if not Is_Frozen (Root_Type (E)) then
6885 Set_Is_Frozen (E, False);
6886 goto Leave;
6887 end if;
6889 -- The equivalent type associated with a class-wide subtype needs
6890 -- to be frozen to ensure that its layout is done.
6892 if Ekind (E) = E_Class_Wide_Subtype
6893 and then Present (Equivalent_Type (E))
6894 then
6895 Freeze_And_Append (Equivalent_Type (E), N, Result);
6896 end if;
6898 -- Generate an itype reference for a library-level class-wide type
6899 -- at the freeze point. Otherwise the first explicit reference to
6900 -- the type may appear in an inner scope which will be rejected by
6901 -- the back-end.
6903 if Is_Itype (E)
6904 and then Is_Compilation_Unit (Scope (E))
6905 then
6906 declare
6907 Ref : constant Node_Id := Make_Itype_Reference (Loc);
6909 begin
6910 Set_Itype (Ref, E);
6912 -- From a gigi point of view, a class-wide subtype derives
6913 -- from its record equivalent type. As a result, the itype
6914 -- reference must appear after the freeze node of the
6915 -- equivalent type or gigi will reject the reference.
6917 if Ekind (E) = E_Class_Wide_Subtype
6918 and then Present (Equivalent_Type (E))
6919 then
6920 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
6921 else
6922 Add_To_Result (Ref);
6923 end if;
6924 end;
6925 end if;
6927 -- For a record type or record subtype, freeze all component types
6928 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
6929 -- using Is_Record_Type, because we don't want to attempt the freeze
6930 -- for the case of a private type with record extension (we will do
6931 -- that later when the full type is frozen).
6933 elsif Ekind (E) in E_Record_Type | E_Record_Subtype then
6934 if not In_Generic_Scope (E) then
6935 Freeze_Record_Type (E);
6936 end if;
6938 -- Report a warning if a discriminated record base type has a
6939 -- convention with language C or C++ applied to it. This check is
6940 -- done even within generic scopes (but not in instantiations),
6941 -- which is why we don't do it as part of Freeze_Record_Type.
6943 Check_Suspicious_Convention (E);
6945 -- For a concurrent type, freeze corresponding record type. This does
6946 -- not correspond to any specific rule in the RM, but the record type
6947 -- is essentially part of the concurrent type. Also freeze all local
6948 -- entities. This includes record types created for entry parameter
6949 -- blocks and whatever local entities may appear in the private part.
6951 elsif Is_Concurrent_Type (E) then
6952 if Present (Corresponding_Record_Type (E)) then
6953 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
6954 end if;
6956 Comp := First_Entity (E);
6957 while Present (Comp) loop
6958 if Is_Type (Comp) then
6959 Freeze_And_Append (Comp, N, Result);
6961 elsif (Ekind (Comp)) /= E_Function then
6963 -- The guard on the presence of the Etype seems to be needed
6964 -- for some CodePeer (-gnatcC) cases, but not clear why???
6966 if Present (Etype (Comp)) then
6967 if Is_Itype (Etype (Comp))
6968 and then Underlying_Type (Scope (Etype (Comp))) = E
6969 then
6970 Undelay_Type (Etype (Comp));
6971 end if;
6973 Freeze_And_Append (Etype (Comp), N, Result);
6974 end if;
6975 end if;
6977 Next_Entity (Comp);
6978 end loop;
6980 -- Private types are required to point to the same freeze node as
6981 -- their corresponding full views. The freeze node itself has to
6982 -- point to the partial view of the entity (because from the partial
6983 -- view, we can retrieve the full view, but not the reverse).
6984 -- However, in order to freeze correctly, we need to freeze the full
6985 -- view. If we are freezing at the end of a scope (or within the
6986 -- scope) of the private type, the partial and full views will have
6987 -- been swapped, the full view appears first in the entity chain and
6988 -- the swapping mechanism ensures that the pointers are properly set
6989 -- (on scope exit).
6991 -- If we encounter the partial view before the full view (e.g. when
6992 -- freezing from another scope), we freeze the full view, and then
6993 -- set the pointers appropriately since we cannot rely on swapping to
6994 -- fix things up (subtypes in an outer scope might not get swapped).
6996 -- If the full view is itself private, the above requirements apply
6997 -- to the underlying full view instead of the full view. But there is
6998 -- no swapping mechanism for the underlying full view so we need to
6999 -- set the pointers appropriately in both cases.
7001 elsif Is_Incomplete_Or_Private_Type (E)
7002 and then not Is_Generic_Type (E)
7003 then
7004 -- The construction of the dispatch table associated with library
7005 -- level tagged types forces freezing of all the primitives of the
7006 -- type, which may cause premature freezing of the partial view.
7007 -- For example:
7009 -- package Pkg is
7010 -- type T is tagged private;
7011 -- type DT is new T with private;
7012 -- procedure Prim (X : in out T; Y : in out DT'Class);
7013 -- private
7014 -- type T is tagged null record;
7015 -- Obj : T;
7016 -- type DT is new T with null record;
7017 -- end;
7019 -- In this case the type will be frozen later by the usual
7020 -- mechanism: an object declaration, an instantiation, or the
7021 -- end of a declarative part.
7023 if Is_Library_Level_Tagged_Type (E)
7024 and then No (Full_View (E))
7025 then
7026 Set_Is_Frozen (E, False);
7027 goto Leave;
7029 -- Case of full view present
7031 elsif Present (Full_View (E)) then
7033 -- If full view has already been frozen, then no further
7034 -- processing is required
7036 if Is_Frozen (Full_View (E)) then
7037 Set_Has_Delayed_Freeze (E, False);
7038 Set_Freeze_Node (E, Empty);
7040 -- Otherwise freeze full view and patch the pointers so that
7041 -- the freeze node will elaborate both views in the back end.
7042 -- However, if full view is itself private, freeze underlying
7043 -- full view instead and patch the pointers so that the freeze
7044 -- node will elaborate the three views in the back end.
7046 else
7047 declare
7048 Full : Entity_Id := Full_View (E);
7050 begin
7051 if Is_Private_Type (Full)
7052 and then Present (Underlying_Full_View (Full))
7053 then
7054 Full := Underlying_Full_View (Full);
7055 end if;
7057 Freeze_And_Append (Full, N, Result);
7059 if Full /= Full_View (E)
7060 and then Has_Delayed_Freeze (Full_View (E))
7061 then
7062 F_Node := Freeze_Node (Full);
7064 if Present (F_Node) then
7065 Inherit_Freeze_Node
7066 (Fnod => F_Node, Typ => Full_View (E));
7067 else
7068 Set_Has_Delayed_Freeze (Full_View (E), False);
7069 Set_Freeze_Node (Full_View (E), Empty);
7070 end if;
7071 end if;
7073 if Has_Delayed_Freeze (E) then
7074 F_Node := Freeze_Node (Full_View (E));
7076 if Present (F_Node) then
7077 Inherit_Freeze_Node (Fnod => F_Node, Typ => E);
7078 else
7079 -- {Incomplete,Private}_Subtypes with Full_Views
7080 -- constrained by discriminants.
7082 Set_Has_Delayed_Freeze (E, False);
7083 Set_Freeze_Node (E, Empty);
7084 end if;
7085 end if;
7086 end;
7087 end if;
7089 Check_Debug_Info_Needed (E);
7091 -- AI95-117 requires that the convention of a partial view be
7092 -- the same as the convention of the full view. Note that this
7093 -- is a recognized breach of privacy, but it's essential for
7094 -- logical consistency of representation, and the lack of a
7095 -- rule in RM95 was an oversight.
7097 Set_Convention (E, Convention (Full_View (E)));
7099 Set_Size_Known_At_Compile_Time (E,
7100 Size_Known_At_Compile_Time (Full_View (E)));
7102 -- Size information is copied from the full view to the
7103 -- incomplete or private view for consistency.
7105 -- We skip this is the full view is not a type. This is very
7106 -- strange of course, and can only happen as a result of
7107 -- certain illegalities, such as a premature attempt to derive
7108 -- from an incomplete type.
7110 if Is_Type (Full_View (E)) then
7111 Set_Size_Info (E, Full_View (E));
7112 Copy_RM_Size (To => E, From => Full_View (E));
7113 end if;
7115 goto Leave;
7117 -- Case of underlying full view present
7119 elsif Is_Private_Type (E)
7120 and then Present (Underlying_Full_View (E))
7121 then
7122 if not Is_Frozen (Underlying_Full_View (E)) then
7123 Freeze_And_Append (Underlying_Full_View (E), N, Result);
7124 end if;
7126 -- Patch the pointers so that the freeze node will elaborate
7127 -- both views in the back end.
7129 if Has_Delayed_Freeze (E) then
7130 F_Node := Freeze_Node (Underlying_Full_View (E));
7132 if Present (F_Node) then
7133 Inherit_Freeze_Node
7134 (Fnod => F_Node,
7135 Typ => E);
7136 else
7137 Set_Has_Delayed_Freeze (E, False);
7138 Set_Freeze_Node (E, Empty);
7139 end if;
7140 end if;
7142 Check_Debug_Info_Needed (E);
7144 goto Leave;
7146 -- Case of no full view present. If entity is subtype or derived,
7147 -- it is safe to freeze, correctness depends on the frozen status
7148 -- of parent. Otherwise it is either premature usage, or a Taft
7149 -- amendment type, so diagnosis is at the point of use and the
7150 -- type might be frozen later.
7152 elsif E /= Base_Type (E) then
7153 declare
7154 Btyp : constant Entity_Id := Base_Type (E);
7156 begin
7157 -- However, if the base type is itself private and has no
7158 -- (underlying) full view either, wait until the full type
7159 -- declaration is seen and all the full views are created.
7161 if Is_Private_Type (Btyp)
7162 and then No (Full_View (Btyp))
7163 and then No (Underlying_Full_View (Btyp))
7164 and then Has_Delayed_Freeze (Btyp)
7165 and then No (Freeze_Node (Btyp))
7166 then
7167 Set_Is_Frozen (E, False);
7168 Result := No_List;
7169 goto Leave;
7170 end if;
7171 end;
7173 elsif Is_Derived_Type (E) then
7174 null;
7176 else
7177 Set_Is_Frozen (E, False);
7178 Result := No_List;
7179 goto Leave;
7180 end if;
7182 -- For access subprogram, freeze types of all formals, the return
7183 -- type was already frozen, since it is the Etype of the function.
7184 -- Formal types can be tagged Taft amendment types, but otherwise
7185 -- they cannot be incomplete.
7187 elsif Ekind (E) = E_Subprogram_Type then
7188 Formal := First_Formal (E);
7189 while Present (Formal) loop
7190 if Ekind (Etype (Formal)) = E_Incomplete_Type
7191 and then No (Full_View (Etype (Formal)))
7192 then
7193 if Is_Tagged_Type (Etype (Formal)) then
7194 null;
7196 -- AI05-151: Incomplete types are allowed in access to
7197 -- subprogram specifications.
7199 elsif Ada_Version < Ada_2012 then
7200 Error_Msg_NE
7201 ("invalid use of incomplete type&", E, Etype (Formal));
7202 end if;
7203 end if;
7205 Freeze_And_Append (Etype (Formal), N, Result);
7206 Next_Formal (Formal);
7207 end loop;
7209 Freeze_Subprogram (E);
7211 -- For access to a protected subprogram, freeze the equivalent type
7212 -- (however this is not set if we are not generating code or if this
7213 -- is an anonymous type used just for resolution).
7215 elsif Is_Access_Protected_Subprogram_Type (E) then
7216 if Present (Equivalent_Type (E)) then
7217 Freeze_And_Append (Equivalent_Type (E), N, Result);
7218 end if;
7219 end if;
7221 -- Generic types are never seen by the back-end, and are also not
7222 -- processed by the expander (since the expander is turned off for
7223 -- generic processing), so we never need freeze nodes for them.
7225 if Is_Generic_Type (E) then
7226 goto Leave;
7227 end if;
7229 -- Some special processing for non-generic types to complete
7230 -- representation details not known till the freeze point.
7232 if Is_Fixed_Point_Type (E) then
7233 Freeze_Fixed_Point_Type (E);
7235 elsif Is_Enumeration_Type (E) then
7236 Freeze_Enumeration_Type (E);
7238 elsif Is_Integer_Type (E) then
7239 Adjust_Esize_For_Alignment (E);
7241 if Is_Modular_Integer_Type (E) then
7242 -- Standard_Address has been built with the assumption that its
7243 -- modulus was System_Address_Size, but this is not a universal
7244 -- property and may need to be corrected.
7246 if Is_RTE (E, RE_Address) then
7247 Set_Modulus (Standard_Address, Modulus (E));
7248 Set_Intval
7249 (High_Bound (Scalar_Range (Standard_Address)),
7250 Modulus (E) - 1);
7252 elsif Warn_On_Suspicious_Modulus_Value then
7253 Check_Suspicious_Modulus (E);
7254 end if;
7255 end if;
7257 -- The pool applies to named and anonymous access types, but not
7258 -- to subprogram and to internal types generated for 'Access
7259 -- references.
7261 elsif Is_Access_Object_Type (E)
7262 and then Ekind (E) /= E_Access_Attribute_Type
7263 then
7264 -- If a pragma Default_Storage_Pool applies, and this type has no
7265 -- Storage_Pool or Storage_Size clause (which must have occurred
7266 -- before the freezing point), then use the default. This applies
7267 -- only to base types.
7269 -- None of this applies to access to subprograms, for which there
7270 -- are clearly no pools.
7272 if Present (Default_Pool)
7273 and then Is_Base_Type (E)
7274 and then not Has_Storage_Size_Clause (E)
7275 and then No (Associated_Storage_Pool (E))
7276 then
7277 -- Case of pragma Default_Storage_Pool (null)
7279 if Nkind (Default_Pool) = N_Null then
7280 Set_No_Pool_Assigned (E);
7282 -- Case of pragma Default_Storage_Pool (Standard)
7284 elsif Entity (Default_Pool) = Standard_Standard then
7285 Set_Associated_Storage_Pool (E, RTE (RE_Global_Pool_Object));
7287 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
7289 else
7290 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
7291 end if;
7292 end if;
7294 -- Check restriction for standard storage pool
7296 if No (Associated_Storage_Pool (E)) then
7297 Check_Restriction (No_Standard_Storage_Pools, E);
7298 end if;
7300 -- Deal with error message for pure access type. This is not an
7301 -- error in Ada 2005 if there is no pool (see AI-366).
7303 if Is_Pure_Unit_Access_Type (E)
7304 and then (Ada_Version < Ada_2005
7305 or else not No_Pool_Assigned (E))
7306 and then not Is_Generic_Unit (Scope (E))
7307 then
7308 Error_Msg_N ("named access type not allowed in pure unit", E);
7310 if Ada_Version >= Ada_2005 then
7311 Error_Msg_N
7312 ("\would be legal if Storage_Size of 0 given??", E);
7314 elsif No_Pool_Assigned (E) then
7315 Error_Msg_N
7316 ("\would be legal in Ada 2005??", E);
7318 else
7319 Error_Msg_N
7320 ("\would be legal in Ada 2005 if "
7321 & "Storage_Size of 0 given??", E);
7322 end if;
7323 end if;
7324 end if;
7326 -- Case of composite types
7328 if Is_Composite_Type (E) then
7330 -- AI95-117 requires that all new primitives of a tagged type must
7331 -- inherit the convention of the full view of the type. Inherited
7332 -- and overriding operations are defined to inherit the convention
7333 -- of their parent or overridden subprogram (also specified in
7334 -- AI-117), which will have occurred earlier (in Derive_Subprogram
7335 -- and New_Overloaded_Entity). Here we set the convention of
7336 -- primitives that are still convention Ada, which will ensure
7337 -- that any new primitives inherit the type's convention. Class-
7338 -- wide types can have a foreign convention inherited from their
7339 -- specific type, but are excluded from this since they don't have
7340 -- any associated primitives.
7342 if Is_Tagged_Type (E)
7343 and then not Is_Class_Wide_Type (E)
7344 and then Convention (E) /= Convention_Ada
7345 then
7346 declare
7347 Prim_List : constant Elist_Id := Primitive_Operations (E);
7348 Prim : Elmt_Id;
7350 begin
7351 Prim := First_Elmt (Prim_List);
7352 while Present (Prim) loop
7353 if Convention (Node (Prim)) = Convention_Ada then
7354 Set_Convention (Node (Prim), Convention (E));
7355 end if;
7357 Next_Elmt (Prim);
7358 end loop;
7359 end;
7360 end if;
7362 -- If the type is a simple storage pool type, then this is where
7363 -- we attempt to locate and validate its Allocate, Deallocate, and
7364 -- Storage_Size operations (the first is required, and the latter
7365 -- two are optional). We also verify that the full type for a
7366 -- private type is allowed to be a simple storage pool type.
7368 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
7369 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
7370 then
7371 -- If the type is marked Has_Private_Declaration, then this is
7372 -- a full type for a private type that was specified with the
7373 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
7374 -- pragma is allowed for the full type (for example, it can't
7375 -- be an array type, or a nonlimited record type).
7377 if Has_Private_Declaration (E) then
7378 if (not Is_Record_Type (E)
7379 or else not Is_Inherently_Limited_Type (E))
7380 and then not Is_Private_Type (E)
7381 then
7382 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
7383 Error_Msg_N
7384 ("pragma% can only apply to full type that is an " &
7385 "explicitly limited type", E);
7386 end if;
7387 end if;
7389 Validate_Simple_Pool_Ops : declare
7390 Pool_Type : Entity_Id renames E;
7391 Address_Type : constant Entity_Id := RTE (RE_Address);
7392 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
7394 procedure Validate_Simple_Pool_Op_Formal
7395 (Pool_Op : Entity_Id;
7396 Pool_Op_Formal : in out Entity_Id;
7397 Expected_Mode : Formal_Kind;
7398 Expected_Type : Entity_Id;
7399 Formal_Name : String;
7400 OK_Formal : in out Boolean);
7401 -- Validate one formal Pool_Op_Formal of the candidate pool
7402 -- operation Pool_Op. The formal must be of Expected_Type
7403 -- and have mode Expected_Mode. OK_Formal will be set to
7404 -- False if the formal doesn't match. If OK_Formal is False
7405 -- on entry, then the formal will effectively be ignored
7406 -- (because validation of the pool op has already failed).
7407 -- Upon return, Pool_Op_Formal will be updated to the next
7408 -- formal, if any.
7410 procedure Validate_Simple_Pool_Operation
7411 (Op_Name : Name_Id);
7412 -- Search for and validate a simple pool operation with the
7413 -- name Op_Name. If the name is Allocate, then there must be
7414 -- exactly one such primitive operation for the simple pool
7415 -- type. If the name is Deallocate or Storage_Size, then
7416 -- there can be at most one such primitive operation. The
7417 -- profile of the located primitive must conform to what
7418 -- is expected for each operation.
7420 ------------------------------------
7421 -- Validate_Simple_Pool_Op_Formal --
7422 ------------------------------------
7424 procedure Validate_Simple_Pool_Op_Formal
7425 (Pool_Op : Entity_Id;
7426 Pool_Op_Formal : in out Entity_Id;
7427 Expected_Mode : Formal_Kind;
7428 Expected_Type : Entity_Id;
7429 Formal_Name : String;
7430 OK_Formal : in out Boolean)
7432 begin
7433 -- If OK_Formal is False on entry, then simply ignore
7434 -- the formal, because an earlier formal has already
7435 -- been flagged.
7437 if not OK_Formal then
7438 return;
7440 -- If no formal is passed in, then issue an error for a
7441 -- missing formal.
7443 elsif No (Pool_Op_Formal) then
7444 Error_Msg_NE
7445 ("simple storage pool op missing formal " &
7446 Formal_Name & " of type&", Pool_Op, Expected_Type);
7447 OK_Formal := False;
7449 return;
7450 end if;
7452 if Etype (Pool_Op_Formal) /= Expected_Type then
7454 -- If the pool type was expected for this formal, then
7455 -- this will not be considered a candidate operation
7456 -- for the simple pool, so we unset OK_Formal so that
7457 -- the op and any later formals will be ignored.
7459 if Expected_Type = Pool_Type then
7460 OK_Formal := False;
7462 return;
7464 else
7465 Error_Msg_NE
7466 ("wrong type for formal " & Formal_Name &
7467 " of simple storage pool op; expected type&",
7468 Pool_Op_Formal, Expected_Type);
7469 end if;
7470 end if;
7472 -- Issue error if formal's mode is not the expected one
7474 if Ekind (Pool_Op_Formal) /= Expected_Mode then
7475 Error_Msg_N
7476 ("wrong mode for formal of simple storage pool op",
7477 Pool_Op_Formal);
7478 end if;
7480 -- Advance to the next formal
7482 Next_Formal (Pool_Op_Formal);
7483 end Validate_Simple_Pool_Op_Formal;
7485 ------------------------------------
7486 -- Validate_Simple_Pool_Operation --
7487 ------------------------------------
7489 procedure Validate_Simple_Pool_Operation
7490 (Op_Name : Name_Id)
7492 Op : Entity_Id;
7493 Found_Op : Entity_Id := Empty;
7494 Formal : Entity_Id;
7495 Is_OK : Boolean;
7497 begin
7498 pragma Assert
7499 (Op_Name in Name_Allocate
7500 | Name_Deallocate
7501 | Name_Storage_Size);
7503 Error_Msg_Name_1 := Op_Name;
7505 -- For each homonym declared immediately in the scope
7506 -- of the simple storage pool type, determine whether
7507 -- the homonym is an operation of the pool type, and,
7508 -- if so, check that its profile is as expected for
7509 -- a simple pool operation of that name.
7511 Op := Get_Name_Entity_Id (Op_Name);
7512 while Present (Op) loop
7513 if Ekind (Op) in E_Function | E_Procedure
7514 and then Scope (Op) = Current_Scope
7515 then
7516 Formal := First_Entity (Op);
7518 Is_OK := True;
7520 -- The first parameter must be of the pool type
7521 -- in order for the operation to qualify.
7523 if Op_Name = Name_Storage_Size then
7524 Validate_Simple_Pool_Op_Formal
7525 (Op, Formal, E_In_Parameter, Pool_Type,
7526 "Pool", Is_OK);
7527 else
7528 Validate_Simple_Pool_Op_Formal
7529 (Op, Formal, E_In_Out_Parameter, Pool_Type,
7530 "Pool", Is_OK);
7531 end if;
7533 -- If another operation with this name has already
7534 -- been located for the type, then flag an error,
7535 -- since we only allow the type to have a single
7536 -- such primitive.
7538 if Present (Found_Op) and then Is_OK then
7539 Error_Msg_NE
7540 ("only one % operation allowed for " &
7541 "simple storage pool type&", Op, Pool_Type);
7542 end if;
7544 -- In the case of Allocate and Deallocate, a formal
7545 -- of type System.Address is required.
7547 if Op_Name = Name_Allocate then
7548 Validate_Simple_Pool_Op_Formal
7549 (Op, Formal, E_Out_Parameter,
7550 Address_Type, "Storage_Address", Is_OK);
7552 elsif Op_Name = Name_Deallocate then
7553 Validate_Simple_Pool_Op_Formal
7554 (Op, Formal, E_In_Parameter,
7555 Address_Type, "Storage_Address", Is_OK);
7556 end if;
7558 -- In the case of Allocate and Deallocate, formals
7559 -- of type Storage_Count are required as the third
7560 -- and fourth parameters.
7562 if Op_Name /= Name_Storage_Size then
7563 Validate_Simple_Pool_Op_Formal
7564 (Op, Formal, E_In_Parameter,
7565 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
7566 Validate_Simple_Pool_Op_Formal
7567 (Op, Formal, E_In_Parameter,
7568 Stg_Cnt_Type, "Alignment", Is_OK);
7569 end if;
7571 -- If no mismatched formals have been found (Is_OK)
7572 -- and no excess formals are present, then this
7573 -- operation has been validated, so record it.
7575 if No (Formal) and then Is_OK then
7576 Found_Op := Op;
7577 end if;
7578 end if;
7580 Op := Homonym (Op);
7581 end loop;
7583 -- There must be a valid Allocate operation for the type,
7584 -- so issue an error if none was found.
7586 if Op_Name = Name_Allocate
7587 and then No (Found_Op)
7588 then
7589 Error_Msg_N ("missing % operation for simple " &
7590 "storage pool type", Pool_Type);
7592 elsif Present (Found_Op) then
7594 -- Simple pool operations can't be abstract
7596 if Is_Abstract_Subprogram (Found_Op) then
7597 Error_Msg_N
7598 ("simple storage pool operation must not be " &
7599 "abstract", Found_Op);
7600 end if;
7602 -- The Storage_Size operation must be a function with
7603 -- Storage_Count as its result type.
7605 if Op_Name = Name_Storage_Size then
7606 if Ekind (Found_Op) = E_Procedure then
7607 Error_Msg_N
7608 ("% operation must be a function", Found_Op);
7610 elsif Etype (Found_Op) /= Stg_Cnt_Type then
7611 Error_Msg_NE
7612 ("wrong result type for%, expected type&",
7613 Found_Op, Stg_Cnt_Type);
7614 end if;
7616 -- Allocate and Deallocate must be procedures
7618 elsif Ekind (Found_Op) = E_Function then
7619 Error_Msg_N
7620 ("% operation must be a procedure", Found_Op);
7621 end if;
7622 end if;
7623 end Validate_Simple_Pool_Operation;
7625 -- Start of processing for Validate_Simple_Pool_Ops
7627 begin
7628 Validate_Simple_Pool_Operation (Name_Allocate);
7629 Validate_Simple_Pool_Operation (Name_Deallocate);
7630 Validate_Simple_Pool_Operation (Name_Storage_Size);
7631 end Validate_Simple_Pool_Ops;
7632 end if;
7633 end if;
7635 -- Now that all types from which E may depend are frozen, see if
7636 -- strict alignment is required, a component clause on a record
7637 -- is correct, the size is known at compile time and if it must
7638 -- be unsigned, in that order.
7640 if Base_Type (E) = E then
7641 Check_Strict_Alignment (E);
7642 end if;
7644 if Ekind (E) in E_Record_Type | E_Record_Subtype then
7645 declare
7646 RC : constant Node_Id := Get_Record_Representation_Clause (E);
7647 begin
7648 if Present (RC) then
7649 Check_Record_Representation_Clause (RC);
7650 end if;
7651 end;
7652 end if;
7654 Check_Compile_Time_Size (E);
7656 Check_Unsigned_Type (E);
7658 -- Do not allow a size clause for a type which does not have a size
7659 -- that is known at compile time
7661 if (Has_Size_Clause (E) or else Has_Object_Size_Clause (E))
7662 and then not Size_Known_At_Compile_Time (E)
7663 then
7664 -- Suppress this message if errors posted on E, even if we are
7665 -- in all errors mode, since this is often a junk message
7667 if not Error_Posted (E) then
7668 Error_Msg_N
7669 ("size clause not allowed for variable length type",
7670 Size_Clause (E));
7671 end if;
7672 end if;
7674 -- Now we set/verify the representation information, in particular
7675 -- the size and alignment values. This processing is not required for
7676 -- generic types, since generic types do not play any part in code
7677 -- generation, and so the size and alignment values for such types
7678 -- are irrelevant. Ditto for types declared within a generic unit,
7679 -- which may have components that depend on generic parameters, and
7680 -- that will be recreated in an instance.
7682 if Inside_A_Generic then
7683 null;
7685 -- Otherwise we call the layout procedure
7687 else
7688 Layout_Type (E);
7689 end if;
7691 -- If this is an access to subprogram whose designated type is itself
7692 -- a subprogram type, the return type of this anonymous subprogram
7693 -- type must be decorated as well.
7695 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
7696 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
7697 then
7698 Layout_Type (Etype (Designated_Type (E)));
7699 end if;
7701 -- If the type has a Defaut_Value/Default_Component_Value aspect,
7702 -- this is where we analyze the expression (after the type is frozen,
7703 -- since in the case of Default_Value, we are analyzing with the
7704 -- type itself, and we treat Default_Component_Value similarly for
7705 -- the sake of uniformity).
7707 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
7708 declare
7709 Nam : Name_Id;
7710 Exp : Node_Id;
7711 Typ : Entity_Id;
7713 begin
7714 if Is_Scalar_Type (E) then
7715 Nam := Name_Default_Value;
7716 Typ := E;
7717 Exp := Default_Aspect_Value (Typ);
7718 else
7719 Nam := Name_Default_Component_Value;
7720 Typ := Component_Type (E);
7721 Exp := Default_Aspect_Component_Value (E);
7722 end if;
7724 Analyze_And_Resolve (Exp, Typ);
7726 if Etype (Exp) /= Any_Type then
7727 if not Is_OK_Static_Expression (Exp) then
7728 Error_Msg_Name_1 := Nam;
7729 Flag_Non_Static_Expr
7730 ("aspect% requires static expression", Exp);
7731 end if;
7732 end if;
7733 end;
7734 end if;
7736 -- Verify at this point that No_Controlled_Parts and No_Task_Parts,
7737 -- when specified on the current type or one of its ancestors, has
7738 -- not been overridden and that no violation of the aspect has
7739 -- occurred.
7741 -- It is important that we perform the checks here after the type has
7742 -- been processed because if said type depended on a private type it
7743 -- will not have been marked controlled or having tasks.
7745 Check_No_Parts_Violations (E, Aspect_No_Controlled_Parts);
7746 Check_No_Parts_Violations (E, Aspect_No_Task_Parts);
7748 -- End of freeze processing for type entities
7749 end if;
7751 -- Here is where we logically freeze the current entity. If it has a
7752 -- freeze node, then this is the point at which the freeze node is
7753 -- linked into the result list.
7755 if Has_Delayed_Freeze (E) then
7757 -- If a freeze node is already allocated, use it, otherwise allocate
7758 -- a new one. The preallocation happens in the case of anonymous base
7759 -- types, where we preallocate so that we can set First_Subtype_Link.
7760 -- Note that we reset the Sloc to the current freeze location.
7762 if Present (Freeze_Node (E)) then
7763 F_Node := Freeze_Node (E);
7764 Set_Sloc (F_Node, Loc);
7766 else
7767 F_Node := New_Node (N_Freeze_Entity, Loc);
7768 Set_Freeze_Node (E, F_Node);
7769 Set_Access_Types_To_Process (F_Node, No_Elist);
7770 Set_TSS_Elist (F_Node, No_Elist);
7771 Set_Actions (F_Node, No_List);
7772 end if;
7774 Set_Entity (F_Node, E);
7775 Add_To_Result (F_Node);
7777 -- A final pass over record types with discriminants. If the type
7778 -- has an incomplete declaration, there may be constrained access
7779 -- subtypes declared elsewhere, which do not depend on the discrimi-
7780 -- nants of the type, and which are used as component types (i.e.
7781 -- the full view is a recursive type). The designated types of these
7782 -- subtypes can only be elaborated after the type itself, and they
7783 -- need an itype reference.
7785 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
7786 declare
7787 Comp : Entity_Id;
7788 IR : Node_Id;
7789 Typ : Entity_Id;
7791 begin
7792 Comp := First_Component (E);
7793 while Present (Comp) loop
7794 Typ := Etype (Comp);
7796 if Is_Access_Type (Typ)
7797 and then Scope (Typ) /= E
7798 and then Base_Type (Designated_Type (Typ)) = E
7799 and then Is_Itype (Designated_Type (Typ))
7800 then
7801 IR := Make_Itype_Reference (Sloc (Comp));
7802 Set_Itype (IR, Designated_Type (Typ));
7803 Append (IR, Result);
7804 end if;
7806 Next_Component (Comp);
7807 end loop;
7808 end;
7809 end if;
7810 end if;
7812 -- When a type is frozen, the first subtype of the type is frozen as
7813 -- well (RM 13.14(15)). This has to be done after freezing the type,
7814 -- since obviously the first subtype depends on its own base type.
7816 if Is_Type (E) then
7817 Freeze_And_Append (First_Subtype (E), N, Result);
7819 -- If we just froze a tagged non-class-wide record, then freeze the
7820 -- corresponding class-wide type. This must be done after the tagged
7821 -- type itself is frozen, because the class-wide type refers to the
7822 -- tagged type which generates the class.
7824 -- For a tagged type, freeze explicitly those primitive operations
7825 -- that are expression functions, which otherwise have no clear
7826 -- freeze point: these have to be frozen before the dispatch table
7827 -- for the type is built, and before any explicit call to the
7828 -- primitive, which would otherwise be the freeze point for it.
7830 if Is_Tagged_Type (E)
7831 and then not Is_Class_Wide_Type (E)
7832 and then Present (Class_Wide_Type (E))
7833 then
7834 Freeze_And_Append (Class_Wide_Type (E), N, Result);
7836 declare
7837 Ops : constant Elist_Id := Primitive_Operations (E);
7839 Elmt : Elmt_Id;
7840 Subp : Entity_Id;
7842 begin
7843 if Ops /= No_Elist then
7844 Elmt := First_Elmt (Ops);
7845 while Present (Elmt) loop
7846 Subp := Node (Elmt);
7847 if Is_Expression_Function (Subp) then
7848 Freeze_And_Append (Subp, N, Result);
7849 end if;
7851 Next_Elmt (Elmt);
7852 end loop;
7853 end if;
7854 end;
7855 end if;
7856 end if;
7858 Check_Debug_Info_Needed (E);
7860 -- If subprogram has address clause then reset Is_Public flag, since we
7861 -- do not want the backend to generate external references.
7863 if Is_Subprogram (E)
7864 and then Present (Address_Clause (E))
7865 and then not Is_Library_Level_Entity (E)
7866 then
7867 Set_Is_Public (E, False);
7868 end if;
7870 -- The Ghost mode of the enclosing context is ignored, while the
7871 -- entity being frozen is living. Insert the freezing action prior
7872 -- to the start of the enclosing ignored Ghost region. As a result
7873 -- the freezeing action will be preserved when the ignored Ghost
7874 -- context is eliminated. The insertion must take place even when
7875 -- the context is a spec expression, otherwise "Handling of Default
7876 -- and Per-Object Expressions" will suppress the insertion, and the
7877 -- freeze node will be dropped on the floor.
7879 if Saved_GM = Ignore
7880 and then Ghost_Mode /= Ignore
7881 and then Present (Ignored_Ghost_Region)
7882 then
7883 Insert_Actions
7884 (Assoc_Node => Ignored_Ghost_Region,
7885 Ins_Actions => Result,
7886 Spec_Expr_OK => True);
7888 Result := No_List;
7889 end if;
7891 <<Leave>>
7892 Restore_Ghost_Region (Saved_GM, Saved_IGR);
7894 return Result;
7895 end Freeze_Entity;
7897 -----------------------------
7898 -- Freeze_Enumeration_Type --
7899 -----------------------------
7901 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
7902 begin
7903 -- By default, if no size clause is present, an enumeration type with
7904 -- Convention C is assumed to interface to a C enum and has integer
7905 -- size, except for a boolean type because it is assumed to interface
7906 -- to _Bool introduced in C99. This applies to types. For subtypes,
7907 -- verify that its base type has no size clause either. Treat other
7908 -- foreign conventions in the same way, and also make sure alignment
7909 -- is set right.
7911 if Has_Foreign_Convention (Typ)
7912 and then not Is_Boolean_Type (Typ)
7913 and then not Has_Size_Clause (Typ)
7914 and then not Has_Size_Clause (Base_Type (Typ))
7915 and then Esize (Typ) < Standard_Integer_Size
7917 -- Don't do this if Short_Enums on target
7919 and then not Target_Short_Enums
7920 then
7921 Set_Esize (Typ, UI_From_Int (Standard_Integer_Size));
7922 Set_Alignment (Typ, Alignment (Standard_Integer));
7924 -- Normal Ada case or size clause present or not Long_C_Enums on target
7926 else
7927 -- If the enumeration type interfaces to C, and it has a size clause
7928 -- that is smaller than the size of int, it warrants a warning. The
7929 -- user may intend the C type to be a boolean or a char, so this is
7930 -- not by itself an error that the Ada compiler can detect, but it
7931 -- is worth a heads-up. For Boolean and Character types we
7932 -- assume that the programmer has the proper C type in mind.
7933 -- For explicit sizes larger than int, assume the user knows what
7934 -- he is doing and that the code is intentional.
7936 if Convention (Typ) = Convention_C
7937 and then Has_Size_Clause (Typ)
7938 and then Esize (Typ) < Standard_Integer_Size
7939 and then not Is_Boolean_Type (Typ)
7940 and then not Is_Character_Type (Typ)
7942 -- Don't do this if Short_Enums on target
7944 and then not Target_Short_Enums
7945 then
7946 Error_Msg_N
7947 ("??the size of enums in C is implementation-defined",
7948 Size_Clause (Typ));
7949 Error_Msg_N
7950 ("\??check that the C counterpart has size of " &
7951 UI_Image (Esize (Typ)),
7952 Size_Clause (Typ));
7953 end if;
7955 Adjust_Esize_For_Alignment (Typ);
7956 end if;
7958 -- Reject a very large size on a type with a non-standard representation
7959 -- because Expand_Freeze_Enumeration_Type cannot deal with it.
7961 if Has_Non_Standard_Rep (Typ)
7962 and then Known_Esize (Typ)
7963 and then Esize (Typ) > System_Max_Integer_Size
7964 then
7965 Error_Msg_N
7966 ("enumeration type with representation clause too large", Typ);
7967 Error_Msg_Uint_1 := UI_From_Int (System_Max_Integer_Size);
7968 Error_Msg_N
7969 ("\the size of such a type cannot exceed ^ bits", Typ);
7970 end if;
7971 end Freeze_Enumeration_Type;
7973 -----------------------
7974 -- Freeze_Expression --
7975 -----------------------
7977 procedure Freeze_Expression (N : Node_Id) is
7979 function Declared_In_Expanded_Body
7980 (N : Node_Id;
7981 Typ : Entity_Id;
7982 Nam : Entity_Id) return Boolean;
7983 -- Given the N_Handled_Sequence_Of_Statements node of an expander
7984 -- generated subprogram body, determines if the frozen entity is
7985 -- declared inside this body. This is recognized locating the
7986 -- enclosing subprogram of the entity Name or its Type and
7987 -- checking if it is this subprogram body.
7989 function Find_Aggregate_Component_Desig_Type return Entity_Id;
7990 -- If the expression is an array aggregate, the type of the component
7991 -- expressions is also frozen. If the component type is an access type
7992 -- and the expressions include allocators, the designed type is frozen
7993 -- as well.
7995 function In_Expanded_Body (N : Node_Id) return Boolean;
7996 -- Given an N_Handled_Sequence_Of_Statements node, determines whether it
7997 -- is the statement sequence of an expander-generated subprogram: body
7998 -- created for an expression function, for a predicate function, an init
7999 -- proc, a stream subprogram, or a renaming as body. If so, this is not
8000 -- a freezing context and the entity will be frozen at a later point.
8002 function Has_Decl_In_List
8003 (E : Entity_Id;
8004 N : Node_Id;
8005 L : List_Id) return Boolean;
8006 -- Determines whether an entity E referenced in node N is declared in
8007 -- the list L.
8009 -------------------------------
8010 -- Declared_In_Expanded_Body --
8011 -------------------------------
8013 function Declared_In_Expanded_Body
8014 (N : Node_Id;
8015 Typ : Entity_Id;
8016 Nam : Entity_Id) return Boolean
8018 pragma Assert (In_Expanded_Body (N));
8020 Subp_Body : constant Node_Id := Parent (N);
8021 Subp_Id : Entity_Id;
8022 Scop : Entity_Id;
8024 begin
8025 if Acts_As_Spec (Subp_Body) then
8026 Subp_Id := Unique_Defining_Entity (Specification (Subp_Body));
8027 else
8028 Subp_Id := Corresponding_Spec (Subp_Body);
8029 end if;
8031 if Present (Typ) then
8032 Scop := Scope (Typ);
8033 elsif Present (Nam) then
8034 Scop := Scope (Nam);
8035 else
8036 Scop := Standard_Standard;
8037 end if;
8039 while Scop /= Standard_Standard
8040 and then not Is_Subprogram (Scop)
8041 loop
8042 Scop := Scope (Scop);
8043 end loop;
8045 return Scop = Subp_Id;
8046 end Declared_In_Expanded_Body;
8048 -----------------------------------------
8049 -- Find_Aggregate_Component_Desig_Type --
8050 -----------------------------------------
8052 function Find_Aggregate_Component_Desig_Type return Entity_Id is
8053 Assoc : Node_Id;
8054 Exp : Node_Id;
8056 begin
8057 if Present (Expressions (N)) then
8058 Exp := First (Expressions (N));
8059 while Present (Exp) loop
8060 if Nkind (Exp) = N_Allocator then
8061 return Designated_Type (Component_Type (Etype (N)));
8062 end if;
8064 Next (Exp);
8065 end loop;
8066 end if;
8068 if Present (Component_Associations (N)) then
8069 Assoc := First (Component_Associations (N));
8070 while Present (Assoc) loop
8071 if Nkind (Expression (Assoc)) = N_Allocator then
8072 return Designated_Type (Component_Type (Etype (N)));
8073 end if;
8075 Next (Assoc);
8076 end loop;
8077 end if;
8079 return Empty;
8080 end Find_Aggregate_Component_Desig_Type;
8082 ----------------------
8083 -- In_Expanded_Body --
8084 ----------------------
8086 function In_Expanded_Body (N : Node_Id) return Boolean is
8087 P : constant Node_Id := Parent (N);
8088 Id : Entity_Id;
8090 begin
8091 if Nkind (P) /= N_Subprogram_Body then
8092 return False;
8094 -- Treat the generated body of an expression function like other
8095 -- bodies generated during expansion (e.g. stream subprograms) so
8096 -- that those bodies are not treated as freezing points.
8098 elsif Was_Expression_Function (P) then
8099 pragma Assert (not Comes_From_Source (P));
8100 return True;
8102 -- This is the body of a generated predicate function
8104 elsif Present (Corresponding_Spec (P))
8105 and then Is_Predicate_Function (Corresponding_Spec (P))
8106 then
8107 return True;
8109 else
8110 Id := Defining_Unit_Name (Specification (P));
8112 -- The following are expander-created bodies, or bodies that
8113 -- are not freeze points.
8115 if Nkind (Id) = N_Defining_Identifier
8116 and then (Is_Init_Proc (Id)
8117 or else Is_TSS (Id, TSS_Stream_Input)
8118 or else Is_TSS (Id, TSS_Stream_Output)
8119 or else Is_TSS (Id, TSS_Stream_Read)
8120 or else Is_TSS (Id, TSS_Stream_Write)
8121 or else Is_TSS (Id, TSS_Put_Image)
8122 or else Nkind (Original_Node (P)) =
8123 N_Subprogram_Renaming_Declaration)
8124 then
8125 return True;
8126 else
8127 return False;
8128 end if;
8129 end if;
8130 end In_Expanded_Body;
8132 ----------------------
8133 -- Has_Decl_In_List --
8134 ----------------------
8136 function Has_Decl_In_List
8137 (E : Entity_Id;
8138 N : Node_Id;
8139 L : List_Id) return Boolean
8141 Decl_Node : Node_Id;
8143 begin
8144 -- If E is an itype, pretend that it is declared in N except for a
8145 -- class-wide subtype with an equivalent type, because this latter
8146 -- type comes with a bona-fide declaration node.
8148 if Is_Itype (E) then
8149 if Ekind (E) = E_Class_Wide_Subtype
8150 and then Present (Equivalent_Type (E))
8151 then
8152 Decl_Node := Declaration_Node (Equivalent_Type (E));
8153 else
8154 Decl_Node := N;
8155 end if;
8157 else
8158 Decl_Node := Declaration_Node (E);
8159 end if;
8161 return Is_List_Member (Decl_Node)
8162 and then List_Containing (Decl_Node) = L;
8163 end Has_Decl_In_List;
8165 -- Local variables
8167 In_Spec_Exp : constant Boolean := In_Spec_Expression;
8169 Desig_Typ : Entity_Id;
8170 Nam : Entity_Id;
8171 P : Node_Id;
8172 Parent_P : Node_Id;
8173 Typ : Entity_Id;
8175 Allocator_Typ : Entity_Id := Empty;
8177 Freeze_Outside_Subp : Entity_Id := Empty;
8178 -- This entity is set if we are inside a subprogram body and the frozen
8179 -- entity is defined in the enclosing scope of this subprogram. In such
8180 -- case we must skip the subprogram body when climbing the parents chain
8181 -- to locate the correct placement for the freezing node.
8183 -- Start of processing for Freeze_Expression
8185 begin
8186 -- Immediate return if freezing is inhibited. This flag is set by the
8187 -- analyzer to stop freezing on generated expressions that would cause
8188 -- freezing if they were in the source program, but which are not
8189 -- supposed to freeze, since they are created.
8191 if Must_Not_Freeze (N) then
8192 return;
8193 end if;
8195 -- If expression is non-static, then it does not freeze in a default
8196 -- expression, see section "Handling of Default Expressions" in the
8197 -- spec of package Sem for further details. Note that we have to make
8198 -- sure that we actually have a real expression (if we have a subtype
8199 -- indication, we can't test Is_OK_Static_Expression). However, we
8200 -- exclude the case of the prefix of an attribute of a static scalar
8201 -- subtype from this early return, because static subtype attributes
8202 -- should always cause freezing, even in default expressions, but
8203 -- the attribute may not have been marked as static yet (because in
8204 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
8205 -- Freeze_Expression on the prefix).
8207 if In_Spec_Exp
8208 and then Nkind (N) in N_Subexpr
8209 and then not Is_OK_Static_Expression (N)
8210 and then (Nkind (Parent (N)) /= N_Attribute_Reference
8211 or else not (Is_Entity_Name (N)
8212 and then Is_Type (Entity (N))
8213 and then Is_OK_Static_Subtype (Entity (N))))
8214 then
8215 return;
8216 end if;
8218 -- Freeze type of expression if not frozen already
8220 Typ := Empty;
8222 if Nkind (N) in N_Has_Etype and then Present (Etype (N)) then
8223 if not Is_Frozen (Etype (N)) then
8224 Typ := Etype (N);
8226 -- Base type may be an derived numeric type that is frozen at the
8227 -- point of declaration, but first_subtype is still unfrozen.
8229 elsif not Is_Frozen (First_Subtype (Etype (N))) then
8230 Typ := First_Subtype (Etype (N));
8231 end if;
8232 end if;
8234 -- For entity name, freeze entity if not frozen already. A special
8235 -- exception occurs for an identifier that did not come from source.
8236 -- We don't let such identifiers freeze a non-internal entity, i.e.
8237 -- an entity that did come from source, since such an identifier was
8238 -- generated by the expander, and cannot have any semantic effect on
8239 -- the freezing semantics. For example, this stops the parameter of
8240 -- an initialization procedure from freezing the variable.
8242 if Is_Entity_Name (N)
8243 and then Present (Entity (N))
8244 and then not Is_Frozen (Entity (N))
8245 and then (Nkind (N) /= N_Identifier
8246 or else Comes_From_Source (N)
8247 or else not Comes_From_Source (Entity (N)))
8248 then
8249 Nam := Entity (N);
8251 if Present (Nam) and then Ekind (Nam) = E_Function then
8252 Check_Expression_Function (N, Nam);
8253 end if;
8255 else
8256 Nam := Empty;
8257 end if;
8259 -- For an allocator freeze designated type if not frozen already
8261 -- For an aggregate whose component type is an access type, freeze the
8262 -- designated type now, so that its freeze does not appear within the
8263 -- loop that might be created in the expansion of the aggregate. If the
8264 -- designated type is a private type without full view, the expression
8265 -- cannot contain an allocator, so the type is not frozen.
8267 -- For a function, we freeze the entity when the subprogram declaration
8268 -- is frozen, but a function call may appear in an initialization proc.
8269 -- before the declaration is frozen. We need to generate the extra
8270 -- formals, if any, to ensure that the expansion of the call includes
8271 -- the proper actuals. This only applies to Ada subprograms, not to
8272 -- imported ones.
8274 Desig_Typ := Empty;
8276 case Nkind (N) is
8277 when N_Allocator =>
8278 Desig_Typ := Designated_Type (Etype (N));
8280 if Nkind (Expression (N)) = N_Qualified_Expression then
8281 Allocator_Typ := Entity (Subtype_Mark (Expression (N)));
8282 end if;
8284 when N_Aggregate =>
8285 if Is_Array_Type (Etype (N))
8286 and then Is_Access_Type (Component_Type (Etype (N)))
8287 then
8288 -- Check whether aggregate includes allocators
8290 Desig_Typ := Find_Aggregate_Component_Desig_Type;
8291 end if;
8293 when N_Indexed_Component
8294 | N_Selected_Component
8295 | N_Slice
8297 if Is_Access_Type (Etype (Prefix (N))) then
8298 Desig_Typ := Designated_Type (Etype (Prefix (N)));
8299 end if;
8301 when N_Identifier =>
8302 if Present (Nam)
8303 and then Ekind (Nam) = E_Function
8304 and then Nkind (Parent (N)) = N_Function_Call
8305 and then not Has_Foreign_Convention (Nam)
8306 then
8307 Create_Extra_Formals (Nam);
8308 end if;
8310 when others =>
8311 null;
8312 end case;
8314 if Desig_Typ /= Empty
8315 and then (Is_Frozen (Desig_Typ)
8316 or else not Is_Fully_Defined (Desig_Typ))
8317 then
8318 Desig_Typ := Empty;
8319 end if;
8321 -- All done if nothing needs freezing
8323 if No (Typ)
8324 and then No (Nam)
8325 and then No (Desig_Typ)
8326 and then No (Allocator_Typ)
8327 then
8328 return;
8329 end if;
8331 -- Check if we are inside a subprogram body and the frozen entity is
8332 -- defined in the enclosing scope of this subprogram. In such case we
8333 -- must skip the subprogram when climbing the parents chain to locate
8334 -- the correct placement for the freezing node.
8336 -- This is not needed for default expressions and other spec expressions
8337 -- in generic units since the Move_Freeze_Nodes mechanism (sem_ch12.adb)
8338 -- takes care of placing them at the proper place, after the generic
8339 -- unit.
8341 if Present (Nam)
8342 and then Scope (Nam) /= Current_Scope
8343 and then not (In_Spec_Exp and then Inside_A_Generic)
8344 then
8345 declare
8346 S : Entity_Id := Current_Scope;
8348 begin
8349 while Present (S)
8350 and then In_Same_Source_Unit (Nam, S)
8351 loop
8352 if Scope (S) = Scope (Nam) then
8353 if Is_Subprogram (S) and then Has_Completion (S) then
8354 Freeze_Outside_Subp := S;
8355 end if;
8357 exit;
8358 end if;
8360 S := Scope (S);
8361 end loop;
8362 end;
8363 end if;
8365 -- Examine the enclosing context by climbing the parent chain
8367 -- If we identified that we must freeze the entity outside of a given
8368 -- subprogram then we just climb up to that subprogram checking if some
8369 -- enclosing node is marked as Must_Not_Freeze (since in such case we
8370 -- must not freeze yet this entity).
8372 P := N;
8374 if Present (Freeze_Outside_Subp) then
8375 loop
8376 -- Do not freeze the current expression if another expression in
8377 -- the chain of parents must not be frozen.
8379 if Nkind (P) in N_Subexpr and then Must_Not_Freeze (P) then
8380 return;
8381 end if;
8383 Parent_P := Parent (P);
8385 -- If we don't have a parent, then we are not in a well-formed
8386 -- tree. This is an unusual case, but there are some legitimate
8387 -- situations in which this occurs, notably when the expressions
8388 -- in the range of a type declaration are resolved. We simply
8389 -- ignore the freeze request in this case.
8391 if No (Parent_P) then
8392 return;
8393 end if;
8395 -- If the parent is a subprogram body, the candidate insertion
8396 -- point is just ahead of it.
8398 if Nkind (Parent_P) = N_Subprogram_Body
8399 and then Unique_Defining_Entity (Parent_P) =
8400 Freeze_Outside_Subp
8401 then
8402 P := Parent_P;
8403 exit;
8404 end if;
8406 P := Parent_P;
8407 end loop;
8409 -- Otherwise the traversal serves two purposes - to detect scenarios
8410 -- where freezeing is not needed and to find the proper insertion point
8411 -- for the freeze nodes. Although somewhat similar to Insert_Actions,
8412 -- this traversal is freezing semantics-sensitive. Inserting freeze
8413 -- nodes blindly in the tree may result in types being frozen too early.
8415 else
8416 loop
8417 -- Do not freeze the current expression if another expression in
8418 -- the chain of parents must not be frozen.
8420 if Nkind (P) in N_Subexpr and then Must_Not_Freeze (P) then
8421 return;
8422 end if;
8424 Parent_P := Parent (P);
8426 -- If we don't have a parent, then we are not in a well-formed
8427 -- tree. This is an unusual case, but there are some legitimate
8428 -- situations in which this occurs, notably when the expressions
8429 -- in the range of a type declaration are resolved. We simply
8430 -- ignore the freeze request in this case.
8432 if No (Parent_P) then
8433 return;
8434 end if;
8436 -- See if we have got to an appropriate point in the tree
8438 case Nkind (Parent_P) is
8440 -- A special test for the exception of (RM 13.14(8)) for the
8441 -- case of per-object expressions (RM 3.8(18)) occurring in
8442 -- component definition or a discrete subtype definition. Note
8443 -- that we test for a component declaration which includes both
8444 -- cases we are interested in, and furthermore the tree does
8445 -- not have explicit nodes for either of these two constructs.
8447 when N_Component_Declaration =>
8449 -- The case we want to test for here is an identifier that
8450 -- is a per-object expression, this is either a discriminant
8451 -- that appears in a context other than the component
8452 -- declaration or it is a reference to the type of the
8453 -- enclosing construct.
8455 -- For either of these cases, we skip the freezing
8457 if not In_Spec_Expression
8458 and then Nkind (N) = N_Identifier
8459 and then Present (Entity (N))
8460 then
8461 -- We recognize the discriminant case by just looking for
8462 -- a reference to a discriminant. It can only be one for
8463 -- the enclosing construct. Skip freezing in this case.
8465 if Ekind (Entity (N)) = E_Discriminant then
8466 return;
8468 -- For the case of a reference to the enclosing record,
8469 -- (or task or protected type), we look for a type that
8470 -- matches the current scope.
8472 elsif Entity (N) = Current_Scope then
8473 return;
8474 end if;
8475 end if;
8477 -- If we have an enumeration literal that appears as the choice
8478 -- in the aggregate of an enumeration representation clause,
8479 -- then freezing does not occur (RM 13.14(10)).
8481 when N_Enumeration_Representation_Clause =>
8483 -- The case we are looking for is an enumeration literal
8485 if Nkind (N) in N_Identifier | N_Character_Literal
8486 and then Is_Enumeration_Type (Etype (N))
8487 then
8488 -- If enumeration literal appears directly as the choice,
8489 -- do not freeze (this is the normal non-overloaded case)
8491 if Nkind (Parent (N)) = N_Component_Association
8492 and then First (Choices (Parent (N))) = N
8493 then
8494 return;
8496 -- If enumeration literal appears as the name of function
8497 -- which is the choice, then also do not freeze. This
8498 -- happens in the overloaded literal case, where the
8499 -- enumeration literal is temporarily changed to a
8500 -- function call for overloading analysis purposes.
8502 elsif Nkind (Parent (N)) = N_Function_Call
8503 and then Nkind (Parent (Parent (N))) =
8504 N_Component_Association
8505 and then First (Choices (Parent (Parent (N)))) =
8506 Parent (N)
8507 then
8508 return;
8509 end if;
8510 end if;
8512 -- Normally if the parent is a handled sequence of statements,
8513 -- then the current node must be a statement, and that is an
8514 -- appropriate place to insert a freeze node.
8516 when N_Handled_Sequence_Of_Statements =>
8518 -- An exception occurs when the sequence of statements is
8519 -- for an expander generated body that did not do the usual
8520 -- freeze all operation. In this case we usually want to
8521 -- freeze outside this body, not inside it, unless the
8522 -- entity is declared inside this expander generated body.
8524 exit when not In_Expanded_Body (Parent_P)
8525 or else Declared_In_Expanded_Body (Parent_P, Typ, Nam);
8527 -- If parent is a body or a spec or a block, then the current
8528 -- node is a statement or declaration and we can insert the
8529 -- freeze node before it.
8531 when N_Block_Statement
8532 | N_Entry_Body
8533 | N_Package_Body
8534 | N_Package_Specification
8535 | N_Protected_Body
8536 | N_Subprogram_Body
8537 | N_Task_Body
8539 exit;
8541 -- The expander is allowed to define types in any statements
8542 -- list, so any of the following parent nodes also mark a
8543 -- freezing point if the actual node is in a list of
8544 -- statements or declarations.
8546 when N_Abortable_Part
8547 | N_Accept_Alternative
8548 | N_Case_Statement_Alternative
8549 | N_Compilation_Unit_Aux
8550 | N_Conditional_Entry_Call
8551 | N_Delay_Alternative
8552 | N_Elsif_Part
8553 | N_Entry_Call_Alternative
8554 | N_Exception_Handler
8555 | N_Extended_Return_Statement
8556 | N_Freeze_Entity
8557 | N_If_Statement
8558 | N_Selective_Accept
8559 | N_Triggering_Alternative
8561 if No (Current_Subprogram) then
8562 exit when Is_List_Member (P);
8564 -- Check exceptional case documented above for an enclosing
8565 -- handled sequence of statements.
8567 else
8568 declare
8569 Par : Node_Id := Parent (Parent_P);
8571 begin
8572 while Present (Par)
8573 and then
8574 Nkind (Par) /= N_Handled_Sequence_Of_Statements
8575 and then Nkind (Parent (Par)) /= N_Subprogram_Body
8576 loop
8577 Par := Parent (Par);
8578 end loop;
8580 -- If we don't have a parent, then we are not in a
8581 -- well-formed tree and we ignore the freeze request.
8582 -- See previous comment in the enclosing loop.
8584 if No (Par) then
8585 return;
8586 end if;
8588 exit when not In_Expanded_Body (Par)
8589 or else Declared_In_Expanded_Body (Par, Typ, Nam);
8590 end;
8591 end if;
8593 -- The freeze nodes produced by an expression coming from the
8594 -- Actions list of an N_Expression_With_Actions, short-circuit
8595 -- expression or N_Case_Expression_Alternative node must remain
8596 -- within the Actions list if they freeze an entity declared in
8597 -- this list, as inserting the freeze nodes further up the tree
8598 -- may lead to use before declaration issues for the entity.
8600 when N_Case_Expression_Alternative
8601 | N_Expression_With_Actions
8602 | N_Short_Circuit
8604 exit when (Present (Nam)
8605 and then
8606 Has_Decl_In_List (Nam, P, Actions (Parent_P)))
8607 or else (Present (Typ)
8608 and then
8609 Has_Decl_In_List (Typ, P, Actions (Parent_P)));
8611 -- Likewise for an N_If_Expression and its two Actions list
8613 when N_If_Expression =>
8614 declare
8615 L1 : constant List_Id := Then_Actions (Parent_P);
8616 L2 : constant List_Id := Else_Actions (Parent_P);
8618 begin
8619 exit when (Present (Nam)
8620 and then
8621 Has_Decl_In_List (Nam, P, L1))
8622 or else (Present (Typ)
8623 and then
8624 Has_Decl_In_List (Typ, P, L1))
8625 or else (Present (Nam)
8626 and then
8627 Has_Decl_In_List (Nam, P, L2))
8628 or else (Present (Typ)
8629 and then
8630 Has_Decl_In_List (Typ, P, L2));
8631 end;
8633 -- N_Loop_Statement is a special case: a type that appears in
8634 -- the source can never be frozen in a loop (this occurs only
8635 -- because of a loop expanded by the expander), so we keep on
8636 -- going. Otherwise we terminate the search. Same is true of
8637 -- any entity which comes from source (if it has a predefined
8638 -- type, this type does not appear to come from source, but the
8639 -- entity should not be frozen here).
8641 when N_Loop_Statement =>
8642 exit when not Comes_From_Source (Etype (N))
8643 and then (No (Nam) or else not Comes_From_Source (Nam));
8645 -- For all other cases, keep looking at parents
8647 when others =>
8648 null;
8649 end case;
8651 -- We fall through the case if we did not yet find the proper
8652 -- place in the tree for inserting the freeze node, so climb.
8654 P := Parent_P;
8655 end loop;
8656 end if;
8658 -- If the expression appears in a record or an initialization procedure,
8659 -- the freeze nodes are collected and attached to the current scope, to
8660 -- be inserted and analyzed on exit from the scope, to insure that
8661 -- generated entities appear in the correct scope. If the expression is
8662 -- a default for a discriminant specification, the scope is still void.
8663 -- The expression can also appear in the discriminant part of a private
8664 -- or concurrent type.
8666 -- If the expression appears in a constrained subcomponent of an
8667 -- enclosing record declaration, the freeze nodes must be attached to
8668 -- the outer record type so they can eventually be placed in the
8669 -- enclosing declaration list.
8671 -- The other case requiring this special handling is if we are in a
8672 -- default expression, since in that case we are about to freeze a
8673 -- static type, and the freeze scope needs to be the outer scope, not
8674 -- the scope of the subprogram with the default parameter.
8676 -- For default expressions and other spec expressions in generic units,
8677 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
8678 -- placing them at the proper place, after the generic unit.
8680 if (In_Spec_Exp and not Inside_A_Generic)
8681 or else (Is_Type (Current_Scope)
8682 and then (not Is_Concurrent_Type (Current_Scope)
8683 or else not Has_Completion (Current_Scope)))
8684 or else Ekind (Current_Scope) = E_Void
8685 then
8686 declare
8687 Freeze_Nodes : List_Id := No_List;
8688 Pos : Int := Scope_Stack.Last;
8690 begin
8691 if Present (Desig_Typ) then
8692 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
8693 end if;
8695 if Present (Typ) then
8696 Freeze_And_Append (Typ, N, Freeze_Nodes);
8697 end if;
8699 if Present (Nam) then
8700 Freeze_And_Append (Nam, N, Freeze_Nodes);
8701 end if;
8703 -- The current scope may be that of a constrained component of
8704 -- an enclosing record declaration, or of a loop of an enclosing
8705 -- quantified expression or aggregate with an iterated component
8706 -- in Ada 2022, which is above the current scope in the scope
8707 -- stack. Indeed in the context of a quantified expression or
8708 -- an aggregate with an iterated component, an internal scope is
8709 -- created and pushed above the current scope in order to emulate
8710 -- the loop-like behavior of the construct.
8711 -- If the expression is within a top-level pragma, as for a pre-
8712 -- condition on a library-level subprogram, nothing to do.
8714 if not Is_Compilation_Unit (Current_Scope)
8715 and then (Is_Record_Type (Scope (Current_Scope))
8716 or else (Ekind (Current_Scope) = E_Loop
8717 and then Is_Internal (Current_Scope)))
8718 then
8719 Pos := Pos - 1;
8720 end if;
8722 if Is_Non_Empty_List (Freeze_Nodes) then
8724 -- When the current scope is transient, insert the freeze nodes
8725 -- prior to the expression that produced them. Transient scopes
8726 -- may create additional declarations when finalizing objects
8727 -- or managing the secondary stack. Inserting the freeze nodes
8728 -- of those constructs prior to the scope would result in a
8729 -- freeze-before-declaration, therefore the freeze node must
8730 -- remain interleaved with their constructs.
8732 if Scope_Is_Transient then
8733 Insert_Actions (N, Freeze_Nodes);
8735 elsif No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
8736 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
8737 Freeze_Nodes;
8738 else
8739 Append_List (Freeze_Nodes,
8740 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
8741 end if;
8742 end if;
8743 end;
8745 return;
8746 end if;
8748 -- Now we have the right place to do the freezing. First, a special
8749 -- adjustment, if we are in spec-expression analysis mode, these freeze
8750 -- actions must not be thrown away (normally all inserted actions are
8751 -- thrown away in this mode. However, the freeze actions are from static
8752 -- expressions and one of the important reasons we are doing this
8753 -- special analysis is to get these freeze actions. Therefore we turn
8754 -- off the In_Spec_Expression mode to propagate these freeze actions.
8755 -- This also means they get properly analyzed and expanded.
8757 In_Spec_Expression := False;
8759 -- Freeze the subtype mark before a qualified expression on an
8760 -- allocator as per AARM 13.14(4.a). This is needed in particular to
8761 -- generate predicate functions.
8763 if Present (Allocator_Typ) then
8764 Freeze_Before (P, Allocator_Typ);
8765 end if;
8767 -- Freeze the designated type of an allocator (RM 13.14(13))
8769 if Present (Desig_Typ) then
8770 Freeze_Before (P, Desig_Typ);
8771 end if;
8773 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
8774 -- the enumeration representation clause exception in the loop above.
8776 if Present (Typ) then
8777 Freeze_Before (P, Typ);
8778 end if;
8780 -- Freeze name if one is present (RM 13.14(11))
8782 if Present (Nam) then
8783 Freeze_Before (P, Nam);
8784 end if;
8786 -- Restore In_Spec_Expression flag
8788 In_Spec_Expression := In_Spec_Exp;
8789 end Freeze_Expression;
8791 -----------------------
8792 -- Freeze_Expr_Types --
8793 -----------------------
8795 procedure Freeze_Expr_Types
8796 (Def_Id : Entity_Id;
8797 Typ : Entity_Id;
8798 Expr : Node_Id;
8799 N : Node_Id)
8801 function Cloned_Expression return Node_Id;
8802 -- Build a duplicate of the expression of the return statement that has
8803 -- no defining entities shared with the original expression.
8805 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result;
8806 -- Freeze all types referenced in the subtree rooted at Node
8808 -----------------------
8809 -- Cloned_Expression --
8810 -----------------------
8812 function Cloned_Expression return Node_Id is
8813 function Clone_Id (Node : Node_Id) return Traverse_Result;
8814 -- Tree traversal routine that clones the defining identifier of
8815 -- iterator and loop parameter specification nodes.
8817 --------------
8818 -- Clone_Id --
8819 --------------
8821 function Clone_Id (Node : Node_Id) return Traverse_Result is
8822 begin
8823 if Nkind (Node) in
8824 N_Iterator_Specification | N_Loop_Parameter_Specification
8825 then
8826 Set_Defining_Identifier
8827 (Node, New_Copy (Defining_Identifier (Node)));
8828 end if;
8830 return OK;
8831 end Clone_Id;
8833 procedure Clone_Def_Ids is new Traverse_Proc (Clone_Id);
8835 -- Local variable
8837 Dup_Expr : constant Node_Id := New_Copy_Tree (Expr);
8839 -- Start of processing for Cloned_Expression
8841 begin
8842 -- We must duplicate the expression with semantic information to
8843 -- inherit the decoration of global entities in generic instances.
8844 -- Set the parent of the new node to be the parent of the original
8845 -- to get the proper context, which is needed for complete error
8846 -- reporting and for semantic analysis.
8848 Set_Parent (Dup_Expr, Parent (Expr));
8850 -- Replace the defining identifier of iterators and loop param
8851 -- specifications by a clone to ensure that the cloned expression
8852 -- and the original expression don't have shared identifiers;
8853 -- otherwise, as part of the preanalysis of the expression, these
8854 -- shared identifiers may be left decorated with itypes which
8855 -- will not be available in the tree passed to the backend.
8857 Clone_Def_Ids (Dup_Expr);
8859 return Dup_Expr;
8860 end Cloned_Expression;
8862 ----------------------
8863 -- Freeze_Type_Refs --
8864 ----------------------
8866 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result is
8867 procedure Check_And_Freeze_Type (Typ : Entity_Id);
8868 -- Check that Typ is fully declared and freeze it if so
8870 ---------------------------
8871 -- Check_And_Freeze_Type --
8872 ---------------------------
8874 procedure Check_And_Freeze_Type (Typ : Entity_Id) is
8875 begin
8876 -- Skip Itypes created by the preanalysis, and itypes whose
8877 -- scope is another type (i.e. component subtypes that depend
8878 -- on a discriminant),
8880 if Is_Itype (Typ)
8881 and then (Scope_Within_Or_Same (Scope (Typ), Def_Id)
8882 or else Is_Type (Scope (Typ)))
8883 then
8884 return;
8885 end if;
8887 -- This provides a better error message than generating primitives
8888 -- whose compilation fails much later. Refine the error message if
8889 -- possible.
8891 Check_Fully_Declared (Typ, Node);
8893 if Error_Posted (Node) then
8894 if Has_Private_Component (Typ)
8895 and then not Is_Private_Type (Typ)
8896 then
8897 Error_Msg_NE ("\type& has private component", Node, Typ);
8898 end if;
8900 else
8901 Freeze_Before (N, Typ);
8902 end if;
8903 end Check_And_Freeze_Type;
8905 -- Start of processing for Freeze_Type_Refs
8907 begin
8908 -- Check that a type referenced by an entity can be frozen
8910 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
8911 -- The entity itself may be a type, as in a membership test
8912 -- or an attribute reference. Freezing its own type would be
8913 -- incomplete if the entity is derived or an extension.
8915 if Is_Type (Entity (Node)) then
8916 Check_And_Freeze_Type (Entity (Node));
8918 else
8919 Check_And_Freeze_Type (Etype (Entity (Node)));
8920 end if;
8922 -- Check that the enclosing record type can be frozen
8924 if Ekind (Entity (Node)) in E_Component | E_Discriminant then
8925 Check_And_Freeze_Type (Scope (Entity (Node)));
8926 end if;
8928 -- Freezing an access type does not freeze the designated type, but
8929 -- freezing conversions between access to interfaces requires that
8930 -- the interface types themselves be frozen, so that dispatch table
8931 -- entities are properly created.
8933 -- Unclear whether a more general rule is needed ???
8935 elsif Nkind (Node) = N_Type_Conversion
8936 and then Is_Access_Type (Etype (Node))
8937 and then Is_Interface (Designated_Type (Etype (Node)))
8938 then
8939 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8940 end if;
8942 -- An implicit dereference freezes the designated type. In the case
8943 -- of a dispatching call whose controlling argument is an access
8944 -- type, the dereference is not made explicit, so we must check for
8945 -- such a call and freeze the designated type.
8947 if Nkind (Node) in N_Has_Etype
8948 and then Present (Etype (Node))
8949 and then Is_Access_Type (Etype (Node))
8950 then
8951 if Nkind (Parent (Node)) = N_Function_Call
8952 and then Node = Controlling_Argument (Parent (Node))
8953 then
8954 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8956 -- An explicit dereference freezes the designated type as well,
8957 -- even though that type is not attached to an entity in the
8958 -- expression.
8960 elsif Nkind (Parent (Node)) = N_Explicit_Dereference then
8961 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8962 end if;
8964 -- An iterator specification freezes the iterator type, even though
8965 -- that type is not attached to an entity in the construct.
8967 elsif Nkind (Node) in N_Has_Etype
8968 and then Nkind (Parent (Node)) = N_Iterator_Specification
8969 and then Node = Name (Parent (Node))
8970 then
8971 declare
8972 Iter : constant Node_Id :=
8973 Find_Value_Of_Aspect (Etype (Node), Aspect_Default_Iterator);
8975 begin
8976 if Present (Iter) then
8977 Check_And_Freeze_Type (Etype (Iter));
8978 end if;
8979 end;
8980 end if;
8982 -- No point in posting several errors on the same expression
8984 if Serious_Errors_Detected > 0 then
8985 return Abandon;
8986 else
8987 return OK;
8988 end if;
8989 end Freeze_Type_Refs;
8991 procedure Freeze_References is new Traverse_Proc (Freeze_Type_Refs);
8993 -- Local variables
8995 Saved_First_Entity : constant Entity_Id := First_Entity (Def_Id);
8996 Saved_Last_Entity : constant Entity_Id := Last_Entity (Def_Id);
8997 Dup_Expr : constant Node_Id := Cloned_Expression;
8999 -- Start of processing for Freeze_Expr_Types
9001 begin
9002 -- Preanalyze a duplicate of the expression to have available the
9003 -- minimum decoration needed to locate referenced unfrozen types
9004 -- without adding any decoration to the function expression.
9006 -- This routine is also applied to expressions in the contract for
9007 -- the subprogram. If that happens when expanding the code for
9008 -- pre/postconditions during expansion of the subprogram body, the
9009 -- subprogram is already installed.
9011 if Def_Id /= Current_Scope then
9012 Push_Scope (Def_Id);
9013 Install_Formals (Def_Id);
9015 Preanalyze_Spec_Expression (Dup_Expr, Typ);
9016 End_Scope;
9017 else
9018 Preanalyze_Spec_Expression (Dup_Expr, Typ);
9019 end if;
9021 -- Restore certain attributes of Def_Id since the preanalysis may
9022 -- have introduced itypes to this scope, thus modifying attributes
9023 -- First_Entity and Last_Entity.
9025 Set_First_Entity (Def_Id, Saved_First_Entity);
9026 Set_Last_Entity (Def_Id, Saved_Last_Entity);
9028 if Present (Last_Entity (Def_Id)) then
9029 Set_Next_Entity (Last_Entity (Def_Id), Empty);
9030 end if;
9032 -- Freeze all types referenced in the expression
9034 Freeze_References (Dup_Expr);
9035 end Freeze_Expr_Types;
9037 -----------------------------
9038 -- Freeze_Fixed_Point_Type --
9039 -----------------------------
9041 -- Certain fixed-point types and subtypes, including implicit base types
9042 -- and declared first subtypes, have not yet set up a range. This is
9043 -- because the range cannot be set until the Small and Size values are
9044 -- known, and these are not known till the type is frozen.
9046 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
9047 -- whose bounds are unanalyzed real literals. This routine will recognize
9048 -- this case, and transform this range node into a properly typed range
9049 -- with properly analyzed and resolved values.
9051 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
9052 Rng : constant Node_Id := Scalar_Range (Typ);
9053 Lo : constant Node_Id := Low_Bound (Rng);
9054 Hi : constant Node_Id := High_Bound (Rng);
9055 Btyp : constant Entity_Id := Base_Type (Typ);
9056 Brng : constant Node_Id := Scalar_Range (Btyp);
9057 BLo : constant Node_Id := Low_Bound (Brng);
9058 BHi : constant Node_Id := High_Bound (Brng);
9059 Ftyp : constant Entity_Id := Underlying_Type (First_Subtype (Typ));
9061 Small : Ureal;
9062 Loval : Ureal;
9063 Hival : Ureal;
9064 Atype : Entity_Id;
9066 Orig_Lo : Ureal;
9067 Orig_Hi : Ureal;
9068 -- Save original bounds (for shaving tests)
9070 Actual_Size : Int;
9071 -- Actual size chosen
9073 function Fsize (Lov, Hiv : Ureal) return Int;
9074 -- Returns size of type with given bounds. Also leaves these
9075 -- bounds set as the current bounds of the Typ.
9077 function Larger (A, B : Ureal) return Boolean;
9078 -- Returns true if A > B with a margin of Typ'Small
9080 function Smaller (A, B : Ureal) return Boolean;
9081 -- Returns true if A < B with a margin of Typ'Small
9083 -----------
9084 -- Fsize --
9085 -----------
9087 function Fsize (Lov, Hiv : Ureal) return Int is
9088 begin
9089 Set_Realval (Lo, Lov);
9090 Set_Realval (Hi, Hiv);
9091 return Minimum_Size (Typ);
9092 end Fsize;
9094 ------------
9095 -- Larger --
9096 ------------
9098 function Larger (A, B : Ureal) return Boolean is
9099 begin
9100 return A > B and then A - Small_Value (Typ) > B;
9101 end Larger;
9103 -------------
9104 -- Smaller --
9105 -------------
9107 function Smaller (A, B : Ureal) return Boolean is
9108 begin
9109 return A < B and then A + Small_Value (Typ) < B;
9110 end Smaller;
9112 -- Start of processing for Freeze_Fixed_Point_Type
9114 begin
9115 -- The type, or its first subtype if we are freezing the anonymous
9116 -- base, may have a delayed Small aspect. It must be analyzed now,
9117 -- so that all characteristics of the type (size, bounds) can be
9118 -- computed and validated in the call to Minimum_Size that follows.
9120 if Has_Delayed_Aspects (Ftyp) then
9121 Analyze_Aspects_At_Freeze_Point (Ftyp);
9122 Set_Has_Delayed_Aspects (Ftyp, False);
9123 end if;
9125 if May_Inherit_Delayed_Rep_Aspects (Ftyp) then
9126 Inherit_Delayed_Rep_Aspects (Ftyp);
9127 Set_May_Inherit_Delayed_Rep_Aspects (Ftyp, False);
9128 end if;
9130 -- Inherit the Small value from the first subtype in any case
9132 if Typ /= Ftyp then
9133 Set_Small_Value (Typ, Small_Value (Ftyp));
9134 end if;
9136 -- If Esize of a subtype has not previously been set, set it now
9138 if not Known_Esize (Typ) then
9139 Atype := Ancestor_Subtype (Typ);
9141 if Present (Atype) then
9142 Set_Esize (Typ, Esize (Atype));
9143 else
9144 Copy_Esize (To => Typ, From => Btyp);
9145 end if;
9146 end if;
9148 -- Immediate return if the range is already analyzed. This means that
9149 -- the range is already set, and does not need to be computed by this
9150 -- routine.
9152 if Analyzed (Rng) then
9153 return;
9154 end if;
9156 -- Immediate return if either of the bounds raises Constraint_Error
9158 if Raises_Constraint_Error (Lo)
9159 or else Raises_Constraint_Error (Hi)
9160 then
9161 return;
9162 end if;
9164 Small := Small_Value (Typ);
9165 Loval := Realval (Lo);
9166 Hival := Realval (Hi);
9168 Orig_Lo := Loval;
9169 Orig_Hi := Hival;
9171 -- Ordinary fixed-point case
9173 if Is_Ordinary_Fixed_Point_Type (Typ) then
9175 -- For the ordinary fixed-point case, we are allowed to fudge the
9176 -- end-points up or down by small. Generally we prefer to fudge up,
9177 -- i.e. widen the bounds for non-model numbers so that the end points
9178 -- are included. However there are cases in which this cannot be
9179 -- done, and indeed cases in which we may need to narrow the bounds.
9180 -- The following circuit makes the decision.
9182 -- Note: our terminology here is that Incl_EP means that the bounds
9183 -- are widened by Small if necessary to include the end points, and
9184 -- Excl_EP means that the bounds are narrowed by Small to exclude the
9185 -- end-points if this reduces the size.
9187 -- Note that in the Incl case, all we care about is including the
9188 -- end-points. In the Excl case, we want to narrow the bounds as
9189 -- much as permitted by the RM, to give the smallest possible size.
9191 Fudge : declare
9192 Loval_Incl_EP : Ureal;
9193 Hival_Incl_EP : Ureal;
9195 Loval_Excl_EP : Ureal;
9196 Hival_Excl_EP : Ureal;
9198 Size_Incl_EP : Int;
9199 Size_Excl_EP : Int;
9201 Model_Num : Ureal;
9202 Actual_Lo : Ureal;
9203 Actual_Hi : Ureal;
9205 begin
9206 -- First step. Base types are required to be symmetrical. Right
9207 -- now, the base type range is a copy of the first subtype range.
9208 -- This will be corrected before we are done, but right away we
9209 -- need to deal with the case where both bounds are non-negative.
9210 -- In this case, we set the low bound to the negative of the high
9211 -- bound, to make sure that the size is computed to include the
9212 -- required sign. Note that we do not need to worry about the
9213 -- case of both bounds negative, because the sign will be dealt
9214 -- with anyway. Furthermore we can't just go making such a bound
9215 -- symmetrical, since in a twos-complement system, there is an
9216 -- extra negative value which could not be accommodated on the
9217 -- positive side.
9219 if Typ = Btyp
9220 and then not UR_Is_Negative (Loval)
9221 and then Hival > Loval
9222 then
9223 Loval := -Hival;
9224 Set_Realval (Lo, Loval);
9225 end if;
9227 -- Compute the fudged bounds. If the bound is a model number, (or
9228 -- greater if given low bound, smaller if high bound) then we do
9229 -- nothing to include it, but we are allowed to backoff to the
9230 -- next adjacent model number when we exclude it. If it is not a
9231 -- model number then we straddle the two values with the model
9232 -- numbers on either side.
9234 Model_Num := UR_Trunc (Loval / Small) * Small;
9236 if UR_Ge (Loval, Model_Num) then
9237 Loval_Incl_EP := Model_Num;
9238 else
9239 Loval_Incl_EP := Model_Num - Small;
9240 end if;
9242 -- The low value excluding the end point is Small greater, but
9243 -- we do not do this exclusion if the low value is positive,
9244 -- since it can't help the size and could actually hurt by
9245 -- crossing the high bound.
9247 if UR_Is_Negative (Loval_Incl_EP) then
9248 Loval_Excl_EP := Loval_Incl_EP + Small;
9250 -- If the value went from negative to zero, then we have the
9251 -- case where Loval_Incl_EP is the model number just below
9252 -- zero, so we want to stick to the negative value for the
9253 -- base type to maintain the condition that the size will
9254 -- include signed values.
9256 if Typ = Btyp
9257 and then UR_Is_Zero (Loval_Excl_EP)
9258 then
9259 Loval_Excl_EP := Loval_Incl_EP;
9260 end if;
9262 else
9263 Loval_Excl_EP := Loval_Incl_EP;
9264 end if;
9266 -- Similar processing for upper bound and high value
9268 Model_Num := UR_Trunc (Hival / Small) * Small;
9270 if UR_Le (Hival, Model_Num) then
9271 Hival_Incl_EP := Model_Num;
9272 else
9273 Hival_Incl_EP := Model_Num + Small;
9274 end if;
9276 if UR_Is_Positive (Hival_Incl_EP) then
9277 Hival_Excl_EP := Hival_Incl_EP - Small;
9278 else
9279 Hival_Excl_EP := Hival_Incl_EP;
9280 end if;
9282 -- One further adjustment is needed. In the case of subtypes, we
9283 -- cannot go outside the range of the base type, or we get
9284 -- peculiarities, and the base type range is already set. This
9285 -- only applies to the Incl values, since clearly the Excl values
9286 -- are already as restricted as they are allowed to be.
9288 if Typ /= Btyp then
9289 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
9290 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
9291 end if;
9293 -- Get size including and excluding end points
9295 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
9296 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
9298 -- No need to exclude end-points if it does not reduce size
9300 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
9301 Loval_Excl_EP := Loval_Incl_EP;
9302 end if;
9304 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
9305 Hival_Excl_EP := Hival_Incl_EP;
9306 end if;
9308 -- Now we set the actual size to be used. We want to use the
9309 -- bounds fudged up to include the end-points but only if this
9310 -- can be done without violating a specifically given size
9311 -- size clause or causing an unacceptable increase in size.
9313 -- Case of size clause given
9315 if Has_Size_Clause (Typ) then
9317 -- Use the inclusive size only if it is consistent with
9318 -- the explicitly specified size.
9320 if Size_Incl_EP <= RM_Size (Typ) then
9321 Actual_Lo := Loval_Incl_EP;
9322 Actual_Hi := Hival_Incl_EP;
9323 Actual_Size := Size_Incl_EP;
9325 -- If the inclusive size is too large, we try excluding
9326 -- the end-points (will be caught later if does not work).
9328 else
9329 Actual_Lo := Loval_Excl_EP;
9330 Actual_Hi := Hival_Excl_EP;
9331 Actual_Size := Size_Excl_EP;
9332 end if;
9334 -- Case of size clause not given
9336 else
9337 -- If we have a base type whose corresponding first subtype
9338 -- has an explicit size that is large enough to include our
9339 -- end-points, then do so. There is no point in working hard
9340 -- to get a base type whose size is smaller than the specified
9341 -- size of the first subtype.
9343 if Has_Size_Clause (Ftyp)
9344 and then Size_Incl_EP <= Esize (Ftyp)
9345 then
9346 Actual_Size := Size_Incl_EP;
9347 Actual_Lo := Loval_Incl_EP;
9348 Actual_Hi := Hival_Incl_EP;
9350 -- If excluding the end-points makes the size smaller and
9351 -- results in a size of 8,16,32,64, then we take the smaller
9352 -- size. For the 64 case, this is compulsory. For the other
9353 -- cases, it seems reasonable. We like to include end points
9354 -- if we can, but not at the expense of moving to the next
9355 -- natural boundary of size.
9357 elsif Size_Incl_EP /= Size_Excl_EP
9358 and then Addressable (Size_Excl_EP)
9359 then
9360 Actual_Size := Size_Excl_EP;
9361 Actual_Lo := Loval_Excl_EP;
9362 Actual_Hi := Hival_Excl_EP;
9364 -- Otherwise we can definitely include the end points
9366 else
9367 Actual_Size := Size_Incl_EP;
9368 Actual_Lo := Loval_Incl_EP;
9369 Actual_Hi := Hival_Incl_EP;
9370 end if;
9372 -- One pathological case: normally we never fudge a low bound
9373 -- down, since it would seem to increase the size (if it has
9374 -- any effect), but for ranges containing single value, or no
9375 -- values, the high bound can be small too large. Consider:
9377 -- type t is delta 2.0**(-14)
9378 -- range 131072.0 .. 0;
9380 -- That lower bound is *just* outside the range of 32 bits, and
9381 -- does need fudging down in this case. Note that the bounds
9382 -- will always have crossed here, since the high bound will be
9383 -- fudged down if necessary, as in the case of:
9385 -- type t is delta 2.0**(-14)
9386 -- range 131072.0 .. 131072.0;
9388 -- So we detect the situation by looking for crossed bounds,
9389 -- and if the bounds are crossed, and the low bound is greater
9390 -- than zero, we will always back it off by small, since this
9391 -- is completely harmless.
9393 if Actual_Lo > Actual_Hi then
9394 if UR_Is_Positive (Actual_Lo) then
9395 Actual_Lo := Loval_Incl_EP - Small;
9396 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
9398 -- And of course, we need to do exactly the same parallel
9399 -- fudge for flat ranges in the negative region.
9401 elsif UR_Is_Negative (Actual_Hi) then
9402 Actual_Hi := Hival_Incl_EP + Small;
9403 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
9404 end if;
9405 end if;
9406 end if;
9408 Set_Realval (Lo, Actual_Lo);
9409 Set_Realval (Hi, Actual_Hi);
9410 end Fudge;
9412 -- Enforce some limitations for ordinary fixed-point types. They come
9413 -- from an exact algorithm used to implement Text_IO.Fixed_IO and the
9414 -- Fore, Image and Value attributes. The requirement on the Small is
9415 -- to lie in the range 2**(-(Siz - 1)) .. 2**(Siz - 1) for a type of
9416 -- Siz bits (Siz=32,64,128) and the requirement on the bounds is to
9417 -- be smaller in magnitude than 10.0**N * 2**(Siz - 1), where N is
9418 -- given by the formula N = floor ((Siz - 1) * log 2 / log 10).
9420 -- If the bounds of a 32-bit type are too large, force 64-bit type
9422 if Actual_Size <= 32
9423 and then Small <= Ureal_2_31
9424 and then (Smaller (Expr_Value_R (Lo), Ureal_M_2_10_18)
9425 or else Larger (Expr_Value_R (Hi), Ureal_2_10_18))
9426 then
9427 Actual_Size := 33;
9428 end if;
9430 -- If the bounds of a 64-bit type are too large, force 128-bit type
9432 if System_Max_Integer_Size = 128
9433 and then Actual_Size <= 64
9434 and then Small <= Ureal_2_63
9435 and then (Smaller (Expr_Value_R (Lo), Ureal_M_9_10_36)
9436 or else Larger (Expr_Value_R (Hi), Ureal_9_10_36))
9437 then
9438 Actual_Size := 65;
9439 end if;
9441 -- Give error messages for first subtypes and not base types, as the
9442 -- bounds of base types are always maximum for their size, see below.
9444 if System_Max_Integer_Size < 128 and then Typ /= Btyp then
9446 -- See the 128-bit case below for the reason why we cannot test
9447 -- against the 2**(-63) .. 2**63 range. This quirk should have
9448 -- been kludged around as in the 128-bit case below, but it was
9449 -- not and we end up with a ludicrous range as a result???
9451 if Small < Ureal_2_M_80 then
9452 Error_Msg_Name_1 := Name_Small;
9453 Error_Msg_N
9454 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", Typ);
9456 elsif Small > Ureal_2_80 then
9457 Error_Msg_Name_1 := Name_Small;
9458 Error_Msg_N
9459 ("`&''%` too large, maximum allowed is 2.0'*'*80", Typ);
9460 end if;
9462 if Smaller (Expr_Value_R (Lo), Ureal_M_9_10_36) then
9463 Error_Msg_Name_1 := Name_First;
9464 Error_Msg_N
9465 ("`&''%` too small, minimum allowed is -9.0E+36", Typ);
9466 end if;
9468 if Larger (Expr_Value_R (Hi), Ureal_9_10_36) then
9469 Error_Msg_Name_1 := Name_Last;
9470 Error_Msg_N
9471 ("`&''%` too large, maximum allowed is 9.0E+36", Typ);
9472 end if;
9474 elsif System_Max_Integer_Size = 128 and then Typ /= Btyp then
9476 -- ACATS c35902d tests a delta equal to 2**(-(Max_Mantissa + 1))
9477 -- but we cannot really support anything smaller than Fine_Delta
9478 -- because of the way we implement I/O for fixed point types???
9480 if Small = Ureal_2_M_128 then
9481 null;
9483 elsif Small < Ureal_2_M_127 then
9484 Error_Msg_Name_1 := Name_Small;
9485 Error_Msg_N
9486 ("`&''%` too small, minimum allowed is 2.0'*'*(-127)", Typ);
9488 elsif Small > Ureal_2_127 then
9489 Error_Msg_Name_1 := Name_Small;
9490 Error_Msg_N
9491 ("`&''%` too large, maximum allowed is 2.0'*'*127", Typ);
9492 end if;
9494 if Actual_Size > 64
9495 and then (Norm_Num (Small) > Uint_2 ** 127
9496 or else Norm_Den (Small) > Uint_2 ** 127)
9497 and then Small /= Ureal_2_M_128
9498 then
9499 Error_Msg_Name_1 := Name_Small;
9500 Error_Msg_N
9501 ("`&''%` not the ratio of two 128-bit integers", Typ);
9502 end if;
9504 if Smaller (Expr_Value_R (Lo), Ureal_M_10_76) then
9505 Error_Msg_Name_1 := Name_First;
9506 Error_Msg_N
9507 ("`&''%` too small, minimum allowed is -1.0E+76", Typ);
9508 end if;
9510 if Larger (Expr_Value_R (Hi), Ureal_10_76) then
9511 Error_Msg_Name_1 := Name_Last;
9512 Error_Msg_N
9513 ("`&''%` too large, maximum allowed is 1.0E+76", Typ);
9514 end if;
9515 end if;
9517 -- For the decimal case, none of this fudging is required, since there
9518 -- are no end-point problems in the decimal case (the end-points are
9519 -- always included).
9521 else
9522 Actual_Size := Fsize (Loval, Hival);
9523 end if;
9525 -- At this stage, the actual size has been calculated and the proper
9526 -- required bounds are stored in the low and high bounds.
9528 if Actual_Size > System_Max_Integer_Size then
9529 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
9530 Error_Msg_Uint_2 := UI_From_Int (System_Max_Integer_Size);
9531 Error_Msg_N
9532 ("size required (^) for type& too large, maximum allowed is ^",
9533 Typ);
9534 Actual_Size := System_Max_Integer_Size;
9535 end if;
9537 -- Check size against explicit given size
9539 if Has_Size_Clause (Typ) then
9540 if Actual_Size > RM_Size (Typ) then
9541 Error_Msg_Uint_1 := RM_Size (Typ);
9542 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
9543 Error_Msg_NE
9544 ("size given (^) for type& too small, minimum allowed is ^",
9545 Size_Clause (Typ), Typ);
9547 else
9548 Actual_Size := UI_To_Int (Esize (Typ));
9549 end if;
9551 -- Increase size to next natural boundary if no size clause given
9553 else
9554 if Actual_Size <= 8 then
9555 Actual_Size := 8;
9556 elsif Actual_Size <= 16 then
9557 Actual_Size := 16;
9558 elsif Actual_Size <= 32 then
9559 Actual_Size := 32;
9560 elsif Actual_Size <= 64 then
9561 Actual_Size := 64;
9562 else
9563 Actual_Size := 128;
9564 end if;
9566 Set_Esize (Typ, UI_From_Int (Actual_Size));
9567 Adjust_Esize_For_Alignment (Typ);
9568 end if;
9570 -- If we have a base type, then expand the bounds so that they extend to
9571 -- the full width of the allocated size in bits, to avoid junk range
9572 -- checks on intermediate computations.
9574 if Typ = Btyp then
9575 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
9576 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
9577 end if;
9579 -- Final step is to reanalyze the bounds using the proper type
9580 -- and set the Corresponding_Integer_Value fields of the literals.
9582 Set_Etype (Lo, Empty);
9583 Set_Analyzed (Lo, False);
9584 Analyze (Lo);
9586 -- Resolve with universal fixed if the base type, and with the base
9587 -- type if we are freezing a subtype. Note we can't resolve the base
9588 -- type with itself, that would be a reference before definition.
9589 -- The resolution of the bounds of a subtype, if they are given by real
9590 -- literals, includes the setting of the Corresponding_Integer_Value,
9591 -- as for other literals of a fixed-point type.
9593 if Typ = Btyp then
9594 Resolve (Lo, Universal_Fixed);
9595 Set_Corresponding_Integer_Value
9596 (Lo, UR_To_Uint (Realval (Lo) / Small));
9597 else
9598 Resolve (Lo, Btyp);
9599 end if;
9601 -- Similar processing for high bound
9603 Set_Etype (Hi, Empty);
9604 Set_Analyzed (Hi, False);
9605 Analyze (Hi);
9607 if Typ = Btyp then
9608 Resolve (Hi, Universal_Fixed);
9609 Set_Corresponding_Integer_Value
9610 (Hi, UR_To_Uint (Realval (Hi) / Small));
9611 else
9612 Resolve (Hi, Btyp);
9613 end if;
9615 -- Set type of range to correspond to bounds
9617 Set_Etype (Rng, Etype (Lo));
9619 -- Set Esize to calculated size if not set already
9621 if not Known_Esize (Typ) then
9622 Set_Esize (Typ, UI_From_Int (Actual_Size));
9623 end if;
9625 -- Set RM_Size if not already set. If already set, check value
9627 declare
9628 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
9630 begin
9631 if Known_RM_Size (Typ) then
9632 if RM_Size (Typ) < Minsiz then
9633 Error_Msg_Uint_1 := RM_Size (Typ);
9634 Error_Msg_Uint_2 := Minsiz;
9635 Error_Msg_NE
9636 ("size given (^) for type& too small, minimum allowed is ^",
9637 Size_Clause (Typ), Typ);
9638 end if;
9640 else
9641 Set_RM_Size (Typ, Minsiz);
9642 end if;
9643 end;
9645 -- Check for shaving
9647 if Comes_From_Source (Typ) then
9649 -- In SPARK mode the given bounds must be strictly representable
9651 if SPARK_Mode = On then
9652 if Orig_Lo < Expr_Value_R (Lo) then
9653 Error_Msg_NE
9654 ("declared low bound of type & is outside type range",
9655 Lo, Typ);
9656 end if;
9658 if Orig_Hi > Expr_Value_R (Hi) then
9659 Error_Msg_NE
9660 ("declared high bound of type & is outside type range",
9661 Hi, Typ);
9662 end if;
9664 else
9665 if Orig_Lo < Expr_Value_R (Lo) then
9666 Error_Msg_N
9667 ("declared low bound of type & is outside type range??", Typ);
9668 Error_Msg_N
9669 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
9670 end if;
9672 if Orig_Hi > Expr_Value_R (Hi) then
9673 Error_Msg_N
9674 ("declared high bound of type & is outside type range??",
9675 Typ);
9676 Error_Msg_N
9677 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
9678 end if;
9679 end if;
9680 end if;
9681 end Freeze_Fixed_Point_Type;
9683 ------------------
9684 -- Freeze_Itype --
9685 ------------------
9687 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
9688 L : List_Id;
9690 begin
9691 Set_Has_Delayed_Freeze (T);
9692 L := Freeze_Entity (T, N);
9694 Insert_Actions (N, L);
9695 end Freeze_Itype;
9697 --------------------------
9698 -- Freeze_Static_Object --
9699 --------------------------
9701 procedure Freeze_Static_Object (E : Entity_Id) is
9703 Cannot_Be_Static : exception;
9704 -- Exception raised if the type of a static object cannot be made
9705 -- static. This happens if the type depends on non-global objects.
9707 procedure Ensure_Expression_Is_SA (N : Node_Id);
9708 -- Called to ensure that an expression used as part of a type definition
9709 -- is statically allocatable, which means that the expression type is
9710 -- statically allocatable, and the expression is either static, or a
9711 -- reference to a library level constant.
9713 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
9714 -- Called to mark a type as static, checking that it is possible
9715 -- to set the type as static. If it is not possible, then the
9716 -- exception Cannot_Be_Static is raised.
9718 -----------------------------
9719 -- Ensure_Expression_Is_SA --
9720 -----------------------------
9722 procedure Ensure_Expression_Is_SA (N : Node_Id) is
9723 Ent : Entity_Id;
9725 begin
9726 Ensure_Type_Is_SA (Etype (N));
9728 if Is_OK_Static_Expression (N) then
9729 return;
9731 elsif Nkind (N) = N_Identifier then
9732 Ent := Entity (N);
9734 if Present (Ent)
9735 and then Ekind (Ent) = E_Constant
9736 and then Is_Library_Level_Entity (Ent)
9737 then
9738 return;
9739 end if;
9740 end if;
9742 raise Cannot_Be_Static;
9743 end Ensure_Expression_Is_SA;
9745 -----------------------
9746 -- Ensure_Type_Is_SA --
9747 -----------------------
9749 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
9750 N : Node_Id;
9751 C : Entity_Id;
9753 begin
9754 -- If type is library level, we are all set
9756 if Is_Library_Level_Entity (Typ) then
9757 return;
9758 end if;
9760 -- We are also OK if the type already marked as statically allocated,
9761 -- which means we processed it before.
9763 if Is_Statically_Allocated (Typ) then
9764 return;
9765 end if;
9767 -- Mark type as statically allocated
9769 Set_Is_Statically_Allocated (Typ);
9771 -- Check that it is safe to statically allocate this type
9773 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
9774 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
9775 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
9777 elsif Is_Array_Type (Typ) then
9778 N := First_Index (Typ);
9779 while Present (N) loop
9780 Ensure_Type_Is_SA (Etype (N));
9781 Next_Index (N);
9782 end loop;
9784 Ensure_Type_Is_SA (Component_Type (Typ));
9786 elsif Is_Access_Type (Typ) then
9787 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
9789 declare
9790 F : Entity_Id;
9791 T : constant Entity_Id := Etype (Designated_Type (Typ));
9793 begin
9794 if T /= Standard_Void_Type then
9795 Ensure_Type_Is_SA (T);
9796 end if;
9798 F := First_Formal (Designated_Type (Typ));
9799 while Present (F) loop
9800 Ensure_Type_Is_SA (Etype (F));
9801 Next_Formal (F);
9802 end loop;
9803 end;
9805 else
9806 Ensure_Type_Is_SA (Designated_Type (Typ));
9807 end if;
9809 elsif Is_Record_Type (Typ) then
9810 C := First_Entity (Typ);
9811 while Present (C) loop
9812 if Ekind (C) = E_Discriminant
9813 or else Ekind (C) = E_Component
9814 then
9815 Ensure_Type_Is_SA (Etype (C));
9817 elsif Is_Type (C) then
9818 Ensure_Type_Is_SA (C);
9819 end if;
9821 Next_Entity (C);
9822 end loop;
9824 elsif Ekind (Typ) = E_Subprogram_Type then
9825 Ensure_Type_Is_SA (Etype (Typ));
9827 C := First_Formal (Typ);
9828 while Present (C) loop
9829 Ensure_Type_Is_SA (Etype (C));
9830 Next_Formal (C);
9831 end loop;
9833 else
9834 raise Cannot_Be_Static;
9835 end if;
9836 end Ensure_Type_Is_SA;
9838 -- Start of processing for Freeze_Static_Object
9840 begin
9841 Ensure_Type_Is_SA (Etype (E));
9843 exception
9844 when Cannot_Be_Static =>
9846 -- If the object that cannot be static is imported or exported, then
9847 -- issue an error message saying that this object cannot be imported
9848 -- or exported. If it has an address clause it is an overlay in the
9849 -- current partition and the static requirement is not relevant.
9850 -- Do not issue any error message when ignoring rep clauses.
9852 if Ignore_Rep_Clauses then
9853 null;
9855 elsif Is_Imported (E) then
9856 if No (Address_Clause (E)) then
9857 Error_Msg_N
9858 ("& cannot be imported (local type is not constant)", E);
9859 end if;
9861 -- Otherwise must be exported, something is wrong if compiler
9862 -- is marking something as statically allocated which cannot be).
9864 else pragma Assert (Is_Exported (E));
9865 Error_Msg_N
9866 ("& cannot be exported (local type is not constant)", E);
9867 end if;
9868 end Freeze_Static_Object;
9870 -----------------------
9871 -- Freeze_Subprogram --
9872 -----------------------
9874 procedure Freeze_Subprogram (E : Entity_Id) is
9876 procedure Set_Profile_Convention (Subp_Id : Entity_Id);
9877 -- Set the conventions of all anonymous access-to-subprogram formals and
9878 -- result subtype of subprogram Subp_Id to the convention of Subp_Id.
9880 ----------------------------
9881 -- Set_Profile_Convention --
9882 ----------------------------
9884 procedure Set_Profile_Convention (Subp_Id : Entity_Id) is
9885 Conv : constant Convention_Id := Convention (Subp_Id);
9887 procedure Set_Type_Convention (Typ : Entity_Id);
9888 -- Set the convention of anonymous access-to-subprogram type Typ and
9889 -- its designated type to Conv.
9891 -------------------------
9892 -- Set_Type_Convention --
9893 -------------------------
9895 procedure Set_Type_Convention (Typ : Entity_Id) is
9896 begin
9897 -- Set the convention on both the anonymous access-to-subprogram
9898 -- type and the subprogram type it points to because both types
9899 -- participate in conformance-related checks.
9901 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
9902 Set_Convention (Typ, Conv);
9903 Set_Convention (Designated_Type (Typ), Conv);
9904 end if;
9905 end Set_Type_Convention;
9907 -- Local variables
9909 Formal : Entity_Id;
9911 -- Start of processing for Set_Profile_Convention
9913 begin
9914 Formal := First_Formal (Subp_Id);
9915 while Present (Formal) loop
9916 Set_Type_Convention (Etype (Formal));
9917 Next_Formal (Formal);
9918 end loop;
9920 if Ekind (Subp_Id) = E_Function then
9921 Set_Type_Convention (Etype (Subp_Id));
9922 end if;
9923 end Set_Profile_Convention;
9925 -- Local variables
9927 F : Entity_Id;
9928 Retype : Entity_Id;
9930 -- Start of processing for Freeze_Subprogram
9932 begin
9933 -- Subprogram may not have an address clause unless it is imported
9935 if Present (Address_Clause (E)) then
9936 if not Is_Imported (E) then
9937 Error_Msg_N
9938 ("address clause can only be given for imported subprogram",
9939 Name (Address_Clause (E)));
9940 end if;
9941 end if;
9943 -- Reset the Pure indication on an imported subprogram unless an
9944 -- explicit Pure_Function pragma was present or the subprogram is an
9945 -- intrinsic. We do this because otherwise it is an insidious error
9946 -- to call a non-pure function from pure unit and have calls
9947 -- mysteriously optimized away. What happens here is that the Import
9948 -- can bypass the normal check to ensure that pure units call only pure
9949 -- subprograms.
9951 -- The reason for the intrinsic exception is that in general, intrinsic
9952 -- functions (such as shifts) are pure anyway. The only exceptions are
9953 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
9954 -- in any case, so no problem arises.
9956 if Is_Imported (E)
9957 and then Is_Pure (E)
9958 and then not Has_Pragma_Pure_Function (E)
9959 and then not Is_Intrinsic_Subprogram (E)
9960 then
9961 Set_Is_Pure (E, False);
9962 end if;
9964 -- For C++ constructors check that their external name has been given
9965 -- (either in pragma CPP_Constructor or in a pragma import).
9967 if Is_Constructor (E)
9968 and then Convention (E) = Convention_CPP
9969 and then
9970 (No (Interface_Name (E))
9971 or else String_Equal
9972 (L => Strval (Interface_Name (E)),
9973 R => Strval (Get_Default_External_Name (E))))
9974 then
9975 Error_Msg_N
9976 ("'C++ constructor must have external name or link name", E);
9977 end if;
9979 -- We also reset the Pure indication on a subprogram with an Address
9980 -- parameter, because the parameter may be used as a pointer and the
9981 -- referenced data may change even if the address value does not.
9983 -- Note that if the programmer gave an explicit Pure_Function pragma,
9984 -- then we believe the programmer, and leave the subprogram Pure. We
9985 -- also suppress this check on run-time files.
9987 if Is_Pure (E)
9988 and then Is_Subprogram (E)
9989 and then not Has_Pragma_Pure_Function (E)
9990 and then not Is_Internal_Unit (Current_Sem_Unit)
9991 then
9992 Check_Function_With_Address_Parameter (E);
9993 end if;
9995 -- Ensure that all anonymous access-to-subprogram types inherit the
9996 -- convention of their related subprogram (RM 6.3.1(13.1/5)). This is
9997 -- not done for a defaulted convention Ada because those types also
9998 -- default to Ada. Convention Protected must not be propagated when
9999 -- the subprogram is an entry because this would be illegal. The only
10000 -- way to force convention Protected on these kinds of types is to
10001 -- include keyword "protected" in the access definition. Conventions
10002 -- Entry and Intrinsic are also not propagated (specified by AI12-0207).
10004 if Convention (E) /= Convention_Ada
10005 and then Convention (E) /= Convention_Protected
10006 and then Convention (E) /= Convention_Entry
10007 and then Convention (E) /= Convention_Intrinsic
10008 then
10009 Set_Profile_Convention (E);
10010 end if;
10012 -- For non-foreign convention subprograms, this is where we create
10013 -- the extra formals (for accessibility level and constrained bit
10014 -- information). We delay this till the freeze point precisely so
10015 -- that we know the convention.
10017 if not Has_Foreign_Convention (E) then
10019 -- Extra formals of dispatching operations are added later by
10020 -- Expand_Freeze_Record_Type, which also adds extra formals to
10021 -- internal entities built to handle interface types.
10023 if not Is_Dispatching_Operation (E) then
10024 Create_Extra_Formals (E);
10026 pragma Assert
10027 ((Ekind (E) = E_Subprogram_Type
10028 and then Extra_Formals_OK (E))
10029 or else
10030 (Is_Subprogram (E)
10031 and then Extra_Formals_OK (E)
10032 and then
10033 (No (Overridden_Operation (E))
10034 or else Extra_Formals_Match_OK (E,
10035 Ultimate_Alias (Overridden_Operation (E))))));
10036 end if;
10038 Set_Mechanisms (E);
10040 -- If this is convention Ada and a Valued_Procedure, that's odd
10042 if Ekind (E) = E_Procedure
10043 and then Is_Valued_Procedure (E)
10044 and then Convention (E) = Convention_Ada
10045 and then Warn_On_Export_Import
10046 then
10047 Error_Msg_N
10048 ("??Valued_Procedure has no effect for convention Ada", E);
10049 Set_Is_Valued_Procedure (E, False);
10050 end if;
10052 -- Case of foreign convention
10054 else
10055 Set_Mechanisms (E);
10057 -- For foreign conventions, warn about return of unconstrained array
10059 if Ekind (E) = E_Function then
10060 Retype := Underlying_Type (Etype (E));
10062 -- If no return type, probably some other error, e.g. a
10063 -- missing full declaration, so ignore.
10065 if No (Retype) then
10066 null;
10068 -- If the return type is generic, we have emitted a warning
10069 -- earlier on, and there is nothing else to check here. Specific
10070 -- instantiations may lead to erroneous behavior.
10072 elsif Is_Generic_Type (Etype (E)) then
10073 null;
10075 -- Display warning if returning unconstrained array
10077 elsif Is_Array_Type (Retype)
10078 and then not Is_Constrained (Retype)
10080 -- Check appropriate warning is enabled (should we check for
10081 -- Warnings (Off) on specific entities here, probably so???)
10083 and then Warn_On_Export_Import
10084 then
10085 Error_Msg_N
10086 ("?x?foreign convention function& should not return " &
10087 "unconstrained array", E);
10088 return;
10089 end if;
10090 end if;
10092 -- If any of the formals for an exported foreign convention
10093 -- subprogram have defaults, then emit an appropriate warning since
10094 -- this is odd (default cannot be used from non-Ada code)
10096 if Is_Exported (E) then
10097 F := First_Formal (E);
10098 while Present (F) loop
10099 if Warn_On_Export_Import
10100 and then Present (Default_Value (F))
10101 then
10102 Error_Msg_N
10103 ("?x?parameter cannot be defaulted in non-Ada call",
10104 Default_Value (F));
10105 end if;
10107 Next_Formal (F);
10108 end loop;
10109 end if;
10110 end if;
10112 -- Pragma Inline_Always is disallowed for dispatching subprograms
10113 -- because the address of such subprograms is saved in the dispatch
10114 -- table to support dispatching calls, and dispatching calls cannot
10115 -- be inlined. This is consistent with the restriction against using
10116 -- 'Access or 'Address on an Inline_Always subprogram.
10118 if Is_Dispatching_Operation (E)
10119 and then Has_Pragma_Inline_Always (E)
10120 then
10121 Error_Msg_N
10122 ("pragma Inline_Always not allowed for dispatching subprograms", E);
10123 end if;
10125 if Is_Dispatching_Operation (E)
10126 and then Present (Overridden_Operation (E))
10127 then
10128 Local_Restrict.Check_Overriding
10129 (Overrider_Op => E, Overridden_Op => Overridden_Operation (E));
10130 end if;
10132 -- Because of the implicit representation of inherited predefined
10133 -- operators in the front-end, the overriding status of the operation
10134 -- may be affected when a full view of a type is analyzed, and this is
10135 -- not captured by the analysis of the corresponding type declaration.
10136 -- Therefore the correctness of a not-overriding indicator must be
10137 -- rechecked when the subprogram is frozen.
10139 if Nkind (E) = N_Defining_Operator_Symbol
10140 and then not Error_Posted (Parent (E))
10141 then
10142 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
10143 end if;
10145 Retype := Get_Fullest_View (Etype (E));
10147 if Transform_Function_Array
10148 and then Nkind (Parent (E)) = N_Function_Specification
10149 and then Is_Array_Type (Retype)
10150 and then Is_Constrained (Retype)
10151 and then not Is_Unchecked_Conversion_Instance (E)
10152 and then not Rewritten_For_C (E)
10153 then
10154 Build_Procedure_Form (Unit_Declaration_Node (E));
10155 end if;
10156 end Freeze_Subprogram;
10158 ----------------------
10159 -- Is_Fully_Defined --
10160 ----------------------
10162 function Is_Fully_Defined (T : Entity_Id) return Boolean is
10163 begin
10164 if Ekind (T) = E_Class_Wide_Type then
10165 return Is_Fully_Defined (Etype (T));
10167 elsif Is_Array_Type (T) then
10168 return Is_Fully_Defined (Component_Type (T));
10170 elsif Is_Record_Type (T)
10171 and not Is_Private_Type (T)
10172 then
10173 -- Verify that the record type has no components with private types
10174 -- without completion.
10176 declare
10177 Comp : Entity_Id;
10179 begin
10180 Comp := First_Component (T);
10181 while Present (Comp) loop
10182 if not Is_Fully_Defined (Etype (Comp)) then
10183 return False;
10184 end if;
10186 Next_Component (Comp);
10187 end loop;
10188 return True;
10189 end;
10191 -- For the designated type of an access to subprogram, all types in
10192 -- the profile must be fully defined.
10194 elsif Ekind (T) = E_Subprogram_Type then
10195 declare
10196 F : Entity_Id;
10198 begin
10199 F := First_Formal (T);
10200 while Present (F) loop
10201 if not Is_Fully_Defined (Etype (F)) then
10202 return False;
10203 end if;
10205 Next_Formal (F);
10206 end loop;
10208 return Is_Fully_Defined (Etype (T));
10209 end;
10211 else
10212 return not Is_Private_Type (T)
10213 or else Present (Full_View (Base_Type (T)));
10214 end if;
10215 end Is_Fully_Defined;
10217 ---------------------------------
10218 -- Process_Default_Expressions --
10219 ---------------------------------
10221 procedure Process_Default_Expressions
10222 (E : Entity_Id;
10223 After : in out Node_Id)
10225 Loc : constant Source_Ptr := Sloc (E);
10226 Dbody : Node_Id;
10227 Formal : Node_Id;
10228 Dcopy : Node_Id;
10229 Dnam : Entity_Id;
10231 begin
10232 Set_Default_Expressions_Processed (E);
10234 -- A subprogram instance and its associated anonymous subprogram share
10235 -- their signature. The default expression functions are defined in the
10236 -- wrapper packages for the anonymous subprogram, and should not be
10237 -- generated again for the instance.
10239 if Is_Generic_Instance (E)
10240 and then Present (Alias (E))
10241 and then Default_Expressions_Processed (Alias (E))
10242 then
10243 return;
10244 end if;
10246 Formal := First_Formal (E);
10247 while Present (Formal) loop
10248 if Present (Default_Value (Formal)) then
10250 -- We work with a copy of the default expression because we
10251 -- do not want to disturb the original, since this would mess
10252 -- up the conformance checking.
10254 Dcopy := New_Copy_Tree (Default_Value (Formal));
10256 -- The analysis of the expression may generate insert actions,
10257 -- which of course must not be executed. We wrap those actions
10258 -- in a procedure that is not called, and later on eliminated.
10259 -- The following cases have no side effects, and are analyzed
10260 -- directly.
10262 if Nkind (Dcopy) = N_Identifier
10263 or else Nkind (Dcopy) in N_Expanded_Name
10264 | N_Integer_Literal
10265 | N_Character_Literal
10266 | N_String_Literal
10267 | N_Real_Literal
10268 or else (Nkind (Dcopy) = N_Attribute_Reference
10269 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
10270 or else Known_Null (Dcopy)
10271 then
10272 -- If there is no default function, we must still do a full
10273 -- analyze call on the default value, to ensure that all error
10274 -- checks are performed, e.g. those associated with static
10275 -- evaluation. Note: this branch will always be taken if the
10276 -- analyzer is turned off (but we still need the error checks).
10278 -- Note: the setting of parent here is to meet the requirement
10279 -- that we can only analyze the expression while attached to
10280 -- the tree. Really the requirement is that the parent chain
10281 -- be set, we don't actually need to be in the tree.
10283 Set_Parent (Dcopy, Declaration_Node (Formal));
10284 Analyze (Dcopy);
10286 -- Default expressions are resolved with their own type if the
10287 -- context is generic, to avoid anomalies with private types.
10289 if Ekind (Scope (E)) = E_Generic_Package then
10290 Resolve (Dcopy);
10291 else
10292 Resolve (Dcopy, Etype (Formal));
10293 end if;
10295 -- If that resolved expression will raise constraint error,
10296 -- then flag the default value as raising constraint error.
10297 -- This allows a proper error message on the calls.
10299 if Raises_Constraint_Error (Dcopy) then
10300 Set_Raises_Constraint_Error (Default_Value (Formal));
10301 end if;
10303 -- If the default is a parameterless call, we use the name of
10304 -- the called function directly, and there is no body to build.
10306 elsif Nkind (Dcopy) = N_Function_Call
10307 and then No (Parameter_Associations (Dcopy))
10308 then
10309 null;
10311 -- Else construct and analyze the body of a wrapper procedure
10312 -- that contains an object declaration to hold the expression.
10313 -- Given that this is done only to complete the analysis, it is
10314 -- simpler to build a procedure than a function which might
10315 -- involve secondary stack expansion.
10317 else
10318 Dnam := Make_Temporary (Loc, 'D');
10320 Dbody :=
10321 Make_Subprogram_Body (Loc,
10322 Specification =>
10323 Make_Procedure_Specification (Loc,
10324 Defining_Unit_Name => Dnam),
10326 Declarations => New_List (
10327 Make_Object_Declaration (Loc,
10328 Defining_Identifier => Make_Temporary (Loc, 'T'),
10329 Object_Definition =>
10330 New_Occurrence_Of (Etype (Formal), Loc),
10331 Expression => New_Copy_Tree (Dcopy))),
10333 Handled_Statement_Sequence =>
10334 Make_Handled_Sequence_Of_Statements (Loc,
10335 Statements => Empty_List));
10337 Set_Scope (Dnam, Scope (E));
10338 Set_Assignment_OK (First (Declarations (Dbody)));
10339 Set_Is_Eliminated (Dnam);
10340 Insert_After (After, Dbody);
10341 Analyze (Dbody);
10342 After := Dbody;
10343 end if;
10344 end if;
10346 Next_Formal (Formal);
10347 end loop;
10348 end Process_Default_Expressions;
10350 ----------------------------------------
10351 -- Set_Component_Alignment_If_Not_Set --
10352 ----------------------------------------
10354 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
10355 begin
10356 -- Ignore if not base type, subtypes don't need anything
10358 if Typ /= Base_Type (Typ) then
10359 return;
10360 end if;
10362 -- Do not override existing representation
10364 if Is_Packed (Typ) then
10365 return;
10367 elsif Has_Specified_Layout (Typ) then
10368 return;
10370 elsif Component_Alignment (Typ) /= Calign_Default then
10371 return;
10373 else
10374 Set_Component_Alignment
10375 (Typ, Scope_Stack.Table
10376 (Scope_Stack.Last).Component_Alignment_Default);
10377 end if;
10378 end Set_Component_Alignment_If_Not_Set;
10380 --------------------------
10381 -- Set_SSO_From_Default --
10382 --------------------------
10384 procedure Set_SSO_From_Default (T : Entity_Id) is
10385 Reversed : Boolean;
10387 begin
10388 -- Set default SSO for an array or record base type, except in case of
10389 -- a type extension (which always inherits the SSO of its parent type).
10391 if Is_Base_Type (T)
10392 and then (Is_Array_Type (T)
10393 or else (Is_Record_Type (T)
10394 and then not (Is_Tagged_Type (T)
10395 and then Is_Derived_Type (T))))
10396 then
10397 Reversed :=
10398 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
10399 or else
10400 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
10402 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
10404 -- For a record type, if bit order is specified explicitly,
10405 -- then do not set SSO from default if not consistent. Note that
10406 -- we do not want to look at a Bit_Order attribute definition
10407 -- for a parent: if we were to inherit Bit_Order, then both
10408 -- SSO_Set_*_By_Default flags would have been cleared already
10409 -- (by Inherit_Aspects_At_Freeze_Point).
10411 and then not
10412 (Is_Record_Type (T)
10413 and then
10414 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
10415 and then Reverse_Bit_Order (T) /= Reversed)
10416 then
10417 -- If flags cause reverse storage order, then set the result. Note
10418 -- that we would have ignored the pragma setting the non default
10419 -- storage order in any case, hence the assertion at this point.
10421 pragma Assert
10422 (not Reversed or else Support_Nondefault_SSO_On_Target);
10424 Set_Reverse_Storage_Order (T, Reversed);
10426 -- For a record type, also set reversed bit order. Note: if a bit
10427 -- order has been specified explicitly, then this is a no-op.
10429 if Is_Record_Type (T) then
10430 Set_Reverse_Bit_Order (T, Reversed);
10431 end if;
10432 end if;
10433 end if;
10434 end Set_SSO_From_Default;
10436 ------------------
10437 -- Undelay_Type --
10438 ------------------
10440 procedure Undelay_Type (T : Entity_Id) is
10441 begin
10442 Set_Has_Delayed_Freeze (T, False);
10443 Set_Freeze_Node (T, Empty);
10445 -- Since we don't want T to have a Freeze_Node, we don't want its
10446 -- Full_View or Corresponding_Record_Type to have one either.
10448 -- ??? Fundamentally, this whole handling is unpleasant. What we really
10449 -- want is to be sure that for an Itype that's part of record R and is a
10450 -- subtype of type T, that it's frozen after the later of the freeze
10451 -- points of R and T. We have no way of doing that directly, so what we
10452 -- do is force most such Itypes to be frozen as part of freezing R via
10453 -- this procedure and only delay the ones that need to be delayed
10454 -- (mostly the designated types of access types that are defined as part
10455 -- of the record).
10457 if Is_Private_Type (T)
10458 and then Present (Full_View (T))
10459 and then Is_Itype (Full_View (T))
10460 and then Is_Record_Type (Scope (Full_View (T)))
10461 then
10462 Undelay_Type (Full_View (T));
10463 end if;
10465 if Is_Concurrent_Type (T)
10466 and then Present (Corresponding_Record_Type (T))
10467 and then Is_Itype (Corresponding_Record_Type (T))
10468 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
10469 then
10470 Undelay_Type (Corresponding_Record_Type (T));
10471 end if;
10472 end Undelay_Type;
10474 ------------------
10475 -- Warn_Overlay --
10476 ------------------
10478 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id) is
10479 Ent : constant Entity_Id := Entity (Nam);
10480 -- The object to which the address clause applies
10482 Init : Node_Id;
10483 Old : Entity_Id := Empty;
10484 Decl : Node_Id;
10486 begin
10487 -- No warning if address clause overlay warnings are off
10489 if not Address_Clause_Overlay_Warnings then
10490 return;
10491 end if;
10493 -- No warning if there is an explicit initialization
10495 Init := Original_Node (Expression (Declaration_Node (Ent)));
10497 if Present (Init) and then Comes_From_Source (Init) then
10498 return;
10499 end if;
10501 -- We only give the warning for non-imported entities of a type for
10502 -- which a non-null base init proc is defined, or for objects of access
10503 -- types with implicit null initialization, or when Normalize_Scalars
10504 -- applies and the type is scalar or a string type (the latter being
10505 -- tested for because predefined String types are initialized by inline
10506 -- code rather than by an init_proc). Note that we do not give the
10507 -- warning for Initialize_Scalars, since we suppressed initialization
10508 -- in this case. Also, do not warn if Suppress_Initialization is set
10509 -- either on the type, or on the object via pragma or aspect.
10511 if Present (Expr)
10512 and then not Is_Imported (Ent)
10513 and then not Initialization_Suppressed (Typ)
10514 and then not (Ekind (Ent) = E_Variable
10515 and then Initialization_Suppressed (Ent))
10516 and then (Has_Non_Null_Base_Init_Proc (Typ)
10517 or else Is_Access_Type (Typ)
10518 or else (Normalize_Scalars
10519 and then (Is_Scalar_Type (Typ)
10520 or else Is_String_Type (Typ))))
10521 then
10522 if Nkind (Expr) = N_Attribute_Reference
10523 and then Is_Entity_Name (Prefix (Expr))
10524 then
10525 Old := Entity (Prefix (Expr));
10527 elsif Is_Entity_Name (Expr)
10528 and then Ekind (Entity (Expr)) = E_Constant
10529 then
10530 Decl := Declaration_Node (Entity (Expr));
10532 if Nkind (Decl) = N_Object_Declaration
10533 and then Present (Expression (Decl))
10534 and then Nkind (Expression (Decl)) = N_Attribute_Reference
10535 and then Is_Entity_Name (Prefix (Expression (Decl)))
10536 then
10537 Old := Entity (Prefix (Expression (Decl)));
10539 elsif Nkind (Expr) = N_Function_Call then
10540 return;
10541 end if;
10543 -- A function call (most likely to To_Address) is probably not an
10544 -- overlay, so skip warning. Ditto if the function call was inlined
10545 -- and transformed into an entity.
10547 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
10548 return;
10549 end if;
10551 -- If a pragma Import follows, we assume that it is for the current
10552 -- target of the address clause, and skip the warning. There may be
10553 -- a source pragma or an aspect that specifies import and generates
10554 -- the corresponding pragma. These will indicate that the entity is
10555 -- imported and that is checked above so that the spurious warning
10556 -- (generated when the entity is frozen) will be suppressed. The
10557 -- pragma may be attached to the aspect, so it is not yet a list
10558 -- member.
10560 if Is_List_Member (Parent (Expr)) then
10561 Decl := Next (Parent (Expr));
10563 if Present (Decl)
10564 and then Nkind (Decl) = N_Pragma
10565 and then Pragma_Name (Decl) = Name_Import
10566 then
10567 return;
10568 end if;
10569 end if;
10571 -- Otherwise give warning message
10573 if Present (Old) then
10574 Error_Msg_Node_2 := Old;
10575 Error_Msg_N
10576 ("default initialization of & may modify &?o?",
10577 Nam);
10578 else
10579 Error_Msg_N
10580 ("default initialization of & may modify overlaid storage?o?",
10581 Nam);
10582 end if;
10584 -- Add friendly warning if initialization comes from a packed array
10585 -- component.
10587 if Is_Record_Type (Typ) then
10588 declare
10589 Comp : Entity_Id;
10591 begin
10592 Comp := First_Component (Typ);
10593 while Present (Comp) loop
10594 if Nkind (Parent (Comp)) = N_Component_Declaration
10595 and then Present (Expression (Parent (Comp)))
10596 then
10597 exit;
10598 elsif Is_Array_Type (Etype (Comp))
10599 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
10600 then
10601 Error_Msg_NE
10602 ("\packed array component& " &
10603 "will be initialized to zero??",
10604 Nam, Comp);
10605 exit;
10606 else
10607 Next_Component (Comp);
10608 end if;
10609 end loop;
10610 end;
10611 end if;
10613 Error_Msg_N
10614 ("\use pragma Import for & to " &
10615 "suppress initialization (RM B.1(24))??",
10616 Nam);
10617 end if;
10618 end Warn_Overlay;
10620 end Freeze;