PR27116, Spelling errors found by Debian style checker
[official-gcc.git] / gcc / ada / freeze.adb
blob38aeb2456ff4eebdf5fd49b9222aab0d5c761470
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- F R E E Z E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Ch3; use Exp_Ch3;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Pakd; use Exp_Pakd;
40 with Exp_Util; use Exp_Util;
41 with Exp_Tss; use Exp_Tss;
42 with Ghost; use Ghost;
43 with Layout; use Layout;
44 with Lib; use Lib;
45 with Namet; use Namet;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch7; use Sem_Ch7;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Mech; use Sem_Mech;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Res; use Sem_Res;
65 with Sem_Util; use Sem_Util;
66 with Sinfo; use Sinfo;
67 with Sinfo.Nodes; use Sinfo.Nodes;
68 with Sinfo.Utils; use Sinfo.Utils;
69 with Snames; use Snames;
70 with Stand; use Stand;
71 with Stringt; use Stringt;
72 with Strub; use Strub;
73 with Targparm; use Targparm;
74 with Tbuild; use Tbuild;
75 with Ttypes; use Ttypes;
76 with Uintp; use Uintp;
77 with Urealp; use Urealp;
78 with Warnsw; use Warnsw;
80 package body Freeze is
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
86 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
87 -- Typ is a type that is being frozen. If no size clause is given,
88 -- but a default Esize has been computed, then this default Esize is
89 -- adjusted up if necessary to be consistent with a given alignment,
90 -- but never to a value greater than System_Max_Integer_Size. This is
91 -- used for all discrete types and for fixed-point types.
93 procedure Build_And_Analyze_Renamed_Body
94 (Decl : Node_Id;
95 New_S : Entity_Id;
96 After : in out Node_Id);
97 -- Build body for a renaming declaration, insert in tree and analyze
99 procedure Check_Address_Clause (E : Entity_Id);
100 -- Apply legality checks to address clauses for object declarations,
101 -- at the point the object is frozen. Also ensure any initialization is
102 -- performed only after the object has been frozen.
104 procedure Check_Component_Storage_Order
105 (Encl_Type : Entity_Id;
106 Comp : Entity_Id;
107 ADC : Node_Id;
108 Comp_ADC_Present : out Boolean);
109 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
110 -- clause, verify that the component type has an explicit and compatible
111 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
112 -- entity of the component under consideration. For an Encl_Type that
113 -- does not have a Scalar_Storage_Order attribute definition clause,
114 -- verify that the component also does not have such a clause.
115 -- ADC is the attribute definition clause if present (or Empty). On return,
116 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
117 -- attribute definition clause.
119 procedure Check_Debug_Info_Needed (T : Entity_Id);
120 -- As each entity is frozen, this routine is called to deal with the
121 -- setting of Debug_Info_Needed for the entity. This flag is set if
122 -- the entity comes from source, or if we are in Debug_Generated_Code
123 -- mode or if the -gnatdV debug flag is set. However, it never sets
124 -- the flag if Debug_Info_Off is set. This procedure also ensures that
125 -- subsidiary entities have the flag set as required.
127 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
128 -- When an expression function is frozen by a use of it, the expression
129 -- itself is frozen. Check that the expression does not include references
130 -- to deferred constants without completion. We report this at the freeze
131 -- point of the function, to provide a better error message.
133 -- In most cases the expression itself is frozen by the time the function
134 -- itself is frozen, because the formals will be frozen by then. However,
135 -- Attribute references to outer types are freeze points for those types;
136 -- this routine generates the required freeze nodes for them.
138 procedure Check_Strict_Alignment (E : Entity_Id);
139 -- E is a base type. If E is tagged or has a component that is aliased
140 -- or tagged or contains something this is aliased or tagged, set
141 -- Strict_Alignment.
143 procedure Check_Unsigned_Type (E : Entity_Id);
144 pragma Inline (Check_Unsigned_Type);
145 -- If E is a fixed-point or discrete type, then all the necessary work
146 -- to freeze it is completed except for possible setting of the flag
147 -- Is_Unsigned_Type, which is done by this procedure. The call has no
148 -- effect if the entity E is not a discrete or fixed-point type.
150 procedure Freeze_And_Append
151 (Ent : Entity_Id;
152 N : Node_Id;
153 Result : in out List_Id);
154 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
155 -- nodes to Result, modifying Result from No_List if necessary. N has
156 -- the same usage as in Freeze_Entity.
158 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
159 -- Freeze enumeration type. The Esize field is set as processing
160 -- proceeds (i.e. set by default when the type is declared and then
161 -- adjusted by rep clauses). What this procedure does is to make sure
162 -- that if a foreign convention is specified, and no specific size
163 -- is given, then the size must be at least Integer'Size.
165 procedure Freeze_Static_Object (E : Entity_Id);
166 -- If an object is frozen which has Is_Statically_Allocated set, then
167 -- all referenced types must also be marked with this flag. This routine
168 -- is in charge of meeting this requirement for the object entity E.
170 procedure Freeze_Subprogram (E : Entity_Id);
171 -- Perform freezing actions for a subprogram (create extra formals,
172 -- and set proper default mechanism values). Note that this routine
173 -- is not called for internal subprograms, for which neither of these
174 -- actions is needed (or desirable, we do not want for example to have
175 -- these extra formals present in initialization procedures, where they
176 -- would serve no purpose). In this call E is either a subprogram or
177 -- a subprogram type (i.e. an access to a subprogram).
179 function Is_Fully_Defined (T : Entity_Id) return Boolean;
180 -- True if T is not private and has no private components, or has a full
181 -- view. Used to determine whether the designated type of an access type
182 -- should be frozen when the access type is frozen. This is done when an
183 -- allocator is frozen, or an expression that may involve attributes of
184 -- the designated type. Otherwise freezing the access type does not freeze
185 -- the designated type.
187 function Should_Freeze_Type
188 (Typ : Entity_Id; E : Entity_Id; N : Node_Id) return Boolean;
189 -- If Typ is in the current scope, then return True.
190 -- N is a node whose source location corresponds to the freeze point.
191 -- ??? Expression functions (represented by E) shouldn't freeze types in
192 -- general, but our current expansion and freezing model requires an early
193 -- freezing when the dispatch table is needed or when building an aggregate
194 -- with a subtype of Typ, so return True also in this case.
195 -- Note that expression function completions do freeze and are
196 -- handled in Sem_Ch6.Analyze_Expression_Function.
198 ------------------------
199 -- Should_Freeze_Type --
200 ------------------------
202 function Should_Freeze_Type
203 (Typ : Entity_Id; E : Entity_Id; N : Node_Id) return Boolean
205 function Is_Dispatching_Call_Or_Aggregate
206 (N : Node_Id) return Traverse_Result;
207 -- Return Abandon if N is a dispatching call to a subprogram
208 -- declared in the same scope as Typ or an aggregate whose type
209 -- is Typ.
211 --------------------------------------
212 -- Is_Dispatching_Call_Or_Aggregate --
213 --------------------------------------
215 function Is_Dispatching_Call_Or_Aggregate
216 (N : Node_Id) return Traverse_Result is
217 begin
218 if Nkind (N) = N_Function_Call
219 and then Present (Controlling_Argument (N))
220 and then Scope (Entity (Original_Node (Name (N))))
221 = Scope (Typ)
222 then
223 return Abandon;
224 elsif Nkind (N) = N_Aggregate
225 and then Base_Type (Etype (N)) = Base_Type (Typ)
226 then
227 return Abandon;
228 else
229 return OK;
230 end if;
231 end Is_Dispatching_Call_Or_Aggregate;
233 -------------------------
234 -- Need_Dispatch_Table --
235 -------------------------
237 function Need_Dispatch_Table is new
238 Traverse_Func (Is_Dispatching_Call_Or_Aggregate);
239 -- Return Abandon if the input expression requires access to
240 -- Typ's dispatch table.
242 Decl : constant Node_Id :=
243 (if No (E) then E else Original_Node (Unit_Declaration_Node (E)));
245 -- Start of processing for Should_Freeze_Type
247 begin
248 return Within_Scope (Typ, Current_Scope)
249 or else (Nkind (N) = N_Subprogram_Renaming_Declaration
250 and then Present (Corresponding_Formal_Spec (N)))
251 or else (Present (Decl)
252 and then Nkind (Decl) = N_Expression_Function
253 and then Need_Dispatch_Table (Expression (Decl)) = Abandon);
254 end Should_Freeze_Type;
256 procedure Process_Default_Expressions
257 (E : Entity_Id;
258 After : in out Node_Id);
259 -- This procedure is called for each subprogram to complete processing of
260 -- default expressions at the point where all types are known to be frozen.
261 -- The expressions must be analyzed in full, to make sure that all error
262 -- processing is done (they have only been preanalyzed). If the expression
263 -- is not an entity or literal, its analysis may generate code which must
264 -- not be executed. In that case we build a function body to hold that
265 -- code. This wrapper function serves no other purpose (it used to be
266 -- called to evaluate the default, but now the default is inlined at each
267 -- point of call).
269 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
270 -- Typ is a record or array type that is being frozen. This routine sets
271 -- the default component alignment from the scope stack values if the
272 -- alignment is otherwise not specified.
274 procedure Set_SSO_From_Default (T : Entity_Id);
275 -- T is a record or array type that is being frozen. If it is a base type,
276 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
277 -- will be set appropriately. Note that an explicit occurrence of aspect
278 -- Scalar_Storage_Order or an explicit setting of this aspect with an
279 -- attribute definition clause occurs, then these two flags are reset in
280 -- any case, so call will have no effect.
282 procedure Undelay_Type (T : Entity_Id);
283 -- T is a type of a component that we know to be an Itype. We don't want
284 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
285 -- Full_View or Corresponding_Record_Type.
287 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
288 -- Expr is the expression for an address clause for the entity denoted by
289 -- Nam whose type is Typ. If Typ has a default initialization, and there is
290 -- no explicit initialization in the source declaration, check whether the
291 -- address clause might cause overlaying of an entity, and emit a warning
292 -- on the side effect that the initialization will cause.
294 -------------------------------
295 -- Adjust_Esize_For_Alignment --
296 -------------------------------
298 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
299 Align : Uint;
301 begin
302 if Known_Esize (Typ) and then Known_Alignment (Typ) then
303 Align := Alignment_In_Bits (Typ);
305 if Align > Esize (Typ) and then Align <= System_Max_Integer_Size then
306 Set_Esize (Typ, Align);
307 end if;
308 end if;
309 end Adjust_Esize_For_Alignment;
311 ------------------------------------
312 -- Build_And_Analyze_Renamed_Body --
313 ------------------------------------
315 procedure Build_And_Analyze_Renamed_Body
316 (Decl : Node_Id;
317 New_S : Entity_Id;
318 After : in out Node_Id)
320 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
321 Ent : constant Entity_Id := Defining_Entity (Decl);
322 Body_Node : Node_Id;
323 Renamed_Subp : Entity_Id;
325 begin
326 -- If the renamed subprogram is intrinsic, there is no need for a
327 -- wrapper body: we set the alias that will be called and expanded which
328 -- completes the declaration. This transformation is only legal if the
329 -- renamed entity has already been elaborated.
331 -- Note that it is legal for a renaming_as_body to rename an intrinsic
332 -- subprogram, as long as the renaming occurs before the new entity
333 -- is frozen (RM 8.5.4 (5)).
335 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
336 and then Is_Entity_Name (Name (Body_Decl))
337 then
338 Renamed_Subp := Entity (Name (Body_Decl));
339 else
340 Renamed_Subp := Empty;
341 end if;
343 if Present (Renamed_Subp)
344 and then Is_Intrinsic_Subprogram (Renamed_Subp)
345 and then
346 (not In_Same_Source_Unit (Renamed_Subp, Ent)
347 or else Sloc (Renamed_Subp) < Sloc (Ent))
349 -- We can make the renaming entity intrinsic if the renamed function
350 -- has an interface name, or if it is one of the shift/rotate
351 -- operations known to the compiler.
353 and then
354 (Present (Interface_Name (Renamed_Subp))
355 or else Chars (Renamed_Subp) in Name_Rotate_Left
356 | Name_Rotate_Right
357 | Name_Shift_Left
358 | Name_Shift_Right
359 | Name_Shift_Right_Arithmetic)
360 then
361 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
363 if Present (Alias (Renamed_Subp)) then
364 Set_Alias (Ent, Alias (Renamed_Subp));
365 else
366 Set_Alias (Ent, Renamed_Subp);
367 end if;
369 Set_Is_Intrinsic_Subprogram (Ent);
370 Set_Has_Completion (Ent);
372 else
373 Body_Node := Build_Renamed_Body (Decl, New_S);
374 Insert_After (After, Body_Node);
375 Mark_Rewrite_Insertion (Body_Node);
376 Analyze (Body_Node);
377 After := Body_Node;
378 end if;
379 end Build_And_Analyze_Renamed_Body;
381 ------------------------
382 -- Build_Renamed_Body --
383 ------------------------
385 function Build_Renamed_Body
386 (Decl : Node_Id;
387 New_S : Entity_Id) return Node_Id
389 Loc : constant Source_Ptr := Sloc (New_S);
390 -- We use for the source location of the renamed body, the location of
391 -- the spec entity. It might seem more natural to use the location of
392 -- the renaming declaration itself, but that would be wrong, since then
393 -- the body we create would look as though it was created far too late,
394 -- and this could cause problems with elaboration order analysis,
395 -- particularly in connection with instantiations.
397 N : constant Node_Id := Unit_Declaration_Node (New_S);
398 Nam : constant Node_Id := Name (N);
399 Old_S : Entity_Id;
400 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
401 Actuals : List_Id;
402 Call_Node : Node_Id;
403 Call_Name : Node_Id;
404 Body_Node : Node_Id;
405 Formal : Entity_Id;
406 O_Formal : Entity_Id;
407 Param_Spec : Node_Id;
409 Pref : Node_Id := Empty;
410 -- If the renamed entity is a primitive operation given in prefix form,
411 -- the prefix is the target object and it has to be added as the first
412 -- actual in the generated call.
414 begin
415 -- Determine the entity being renamed, which is the target of the call
416 -- statement. If the name is an explicit dereference, this is a renaming
417 -- of a subprogram type rather than a subprogram. The name itself is
418 -- fully analyzed.
420 if Nkind (Nam) = N_Selected_Component then
421 Old_S := Entity (Selector_Name (Nam));
423 elsif Nkind (Nam) = N_Explicit_Dereference then
424 Old_S := Etype (Nam);
426 elsif Nkind (Nam) = N_Indexed_Component then
427 if Is_Entity_Name (Prefix (Nam)) then
428 Old_S := Entity (Prefix (Nam));
429 else
430 Old_S := Entity (Selector_Name (Prefix (Nam)));
431 end if;
433 elsif Nkind (Nam) = N_Character_Literal then
434 Old_S := Etype (New_S);
436 else
437 Old_S := Entity (Nam);
438 end if;
440 if Is_Entity_Name (Nam) then
442 -- If the renamed entity is a predefined operator, retain full name
443 -- to ensure its visibility.
445 if Ekind (Old_S) = E_Operator
446 and then Nkind (Nam) = N_Expanded_Name
447 then
448 Call_Name := New_Copy (Name (N));
449 else
450 Call_Name := New_Occurrence_Of (Old_S, Loc);
451 end if;
453 else
454 if Nkind (Nam) = N_Selected_Component
455 and then Present (First_Formal (Old_S))
456 and then
457 (Is_Controlling_Formal (First_Formal (Old_S))
458 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
459 then
461 -- Retrieve the target object, to be added as a first actual
462 -- in the call.
464 Call_Name := New_Occurrence_Of (Old_S, Loc);
465 Pref := Prefix (Nam);
467 else
468 Call_Name := New_Copy (Name (N));
469 end if;
471 -- Original name may have been overloaded, but is fully resolved now
473 Set_Is_Overloaded (Call_Name, False);
474 end if;
476 if Nkind (Decl) /= N_Subprogram_Declaration then
477 Rewrite (N,
478 Make_Subprogram_Declaration (Loc,
479 Specification => Specification (N)));
480 end if;
482 -- For simple renamings, subsequent calls can be expanded directly as
483 -- calls to the renamed entity. The body must be generated in any case
484 -- for calls that may appear elsewhere. This is not done in the case
485 -- where the subprogram is an instantiation because the actual proper
486 -- body has not been built yet.
488 if Ekind (Old_S) in E_Function | E_Procedure
489 and then not Is_Generic_Instance (Old_S)
490 then
491 Set_Body_To_Inline (Decl, Old_S);
492 end if;
494 -- Check whether the return type is a limited view. If the subprogram
495 -- is already frozen the generated body may have a non-limited view
496 -- of the type, that must be used, because it is the one in the spec
497 -- of the renaming declaration.
499 if Ekind (Old_S) = E_Function
500 and then Is_Entity_Name (Result_Definition (Spec))
501 then
502 declare
503 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
504 begin
505 if Has_Non_Limited_View (Ret_Type) then
506 Set_Result_Definition
507 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
508 end if;
509 end;
510 end if;
512 -- The body generated for this renaming is an internal artifact, and
513 -- does not constitute a freeze point for the called entity.
515 Set_Must_Not_Freeze (Call_Name);
517 Formal := First_Formal (Defining_Entity (Decl));
519 if Present (Pref) then
520 declare
521 Pref_Type : constant Entity_Id := Etype (Pref);
522 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
524 begin
525 -- The controlling formal may be an access parameter, or the
526 -- actual may be an access value, so adjust accordingly.
528 if Is_Access_Type (Pref_Type)
529 and then not Is_Access_Type (Form_Type)
530 then
531 Actuals := New_List
532 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
534 elsif Is_Access_Type (Form_Type)
535 and then not Is_Access_Type (Pref)
536 then
537 Actuals :=
538 New_List (
539 Make_Attribute_Reference (Loc,
540 Attribute_Name => Name_Access,
541 Prefix => Relocate_Node (Pref)));
542 else
543 Actuals := New_List (Pref);
544 end if;
545 end;
547 elsif Present (Formal) then
548 Actuals := New_List;
550 else
551 Actuals := No_List;
552 end if;
554 while Present (Formal) loop
555 Append (New_Occurrence_Of (Formal, Loc), Actuals);
556 Next_Formal (Formal);
557 end loop;
559 -- If the renamed entity is an entry, inherit its profile. For other
560 -- renamings as bodies, both profiles must be subtype conformant, so it
561 -- is not necessary to replace the profile given in the declaration.
562 -- However, default values that are aggregates are rewritten when
563 -- partially analyzed, so we recover the original aggregate to insure
564 -- that subsequent conformity checking works. Similarly, if the default
565 -- expression was constant-folded, recover the original expression.
567 Formal := First_Formal (Defining_Entity (Decl));
569 if Present (Formal) then
570 O_Formal := First_Formal (Old_S);
571 Param_Spec := First (Parameter_Specifications (Spec));
572 while Present (Formal) loop
573 if Is_Entry (Old_S) then
574 if Nkind (Parameter_Type (Param_Spec)) /=
575 N_Access_Definition
576 then
577 Set_Etype (Formal, Etype (O_Formal));
578 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
579 end if;
581 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
582 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
583 Nkind (Default_Value (O_Formal))
584 then
585 Set_Expression (Param_Spec,
586 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
587 end if;
589 Next_Formal (Formal);
590 Next_Formal (O_Formal);
591 Next (Param_Spec);
592 end loop;
593 end if;
595 -- If the renamed entity is a function, the generated body contains a
596 -- return statement. Otherwise, build a procedure call. If the entity is
597 -- an entry, subsequent analysis of the call will transform it into the
598 -- proper entry or protected operation call. If the renamed entity is
599 -- a character literal, return it directly.
601 if Ekind (Old_S) = E_Function
602 or else Ekind (Old_S) = E_Operator
603 or else (Ekind (Old_S) = E_Subprogram_Type
604 and then Etype (Old_S) /= Standard_Void_Type)
605 then
606 Call_Node :=
607 Make_Simple_Return_Statement (Loc,
608 Expression =>
609 Make_Function_Call (Loc,
610 Name => Call_Name,
611 Parameter_Associations => Actuals));
613 elsif Ekind (Old_S) = E_Enumeration_Literal then
614 Call_Node :=
615 Make_Simple_Return_Statement (Loc,
616 Expression => New_Occurrence_Of (Old_S, Loc));
618 elsif Nkind (Nam) = N_Character_Literal then
619 Call_Node :=
620 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
622 else
623 Call_Node :=
624 Make_Procedure_Call_Statement (Loc,
625 Name => Call_Name,
626 Parameter_Associations => Actuals);
627 end if;
629 -- Create entities for subprogram body and formals
631 Set_Defining_Unit_Name (Spec,
632 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
634 Param_Spec := First (Parameter_Specifications (Spec));
635 while Present (Param_Spec) loop
636 Set_Defining_Identifier (Param_Spec,
637 Make_Defining_Identifier (Loc,
638 Chars => Chars (Defining_Identifier (Param_Spec))));
639 Next (Param_Spec);
640 end loop;
642 -- In GNATprove, prefer to generate an expression function whenever
643 -- possible, to benefit from the more precise analysis in that case
644 -- (as if an implicit postcondition had been generated).
646 if GNATprove_Mode
647 and then Nkind (Call_Node) = N_Simple_Return_Statement
648 then
649 Body_Node :=
650 Make_Expression_Function (Loc,
651 Specification => Spec,
652 Expression => Expression (Call_Node));
653 else
654 Body_Node :=
655 Make_Subprogram_Body (Loc,
656 Specification => Spec,
657 Declarations => New_List,
658 Handled_Statement_Sequence =>
659 Make_Handled_Sequence_Of_Statements (Loc,
660 Statements => New_List (Call_Node)));
661 end if;
663 -- Link the body to the entity whose declaration it completes. If
664 -- the body is analyzed when the renamed entity is frozen, it may
665 -- be necessary to restore the proper scope (see package Exp_Ch13).
667 if Nkind (N) = N_Subprogram_Renaming_Declaration
668 and then Present (Corresponding_Spec (N))
669 then
670 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
671 else
672 Set_Corresponding_Spec (Body_Node, New_S);
673 end if;
675 return Body_Node;
676 end Build_Renamed_Body;
678 --------------------------
679 -- Check_Address_Clause --
680 --------------------------
682 procedure Check_Address_Clause (E : Entity_Id) is
683 Addr : constant Node_Id := Address_Clause (E);
684 Typ : constant Entity_Id := Etype (E);
685 Decl : Node_Id;
686 Expr : Node_Id;
687 Init : Node_Id;
688 Lhs : Node_Id;
689 Tag_Assign : Node_Id;
691 begin
692 if Present (Addr) then
694 -- For a deferred constant, the initialization value is on full view
696 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
697 Decl := Declaration_Node (Full_View (E));
698 else
699 Decl := Declaration_Node (E);
700 end if;
702 Expr := Expression (Addr);
704 if Needs_Constant_Address (Decl, Typ) then
705 Check_Constant_Address_Clause (Expr, E);
707 -- Has_Delayed_Freeze was set on E when the address clause was
708 -- analyzed, and must remain set because we want the address
709 -- clause to be elaborated only after any entity it references
710 -- has been elaborated.
711 end if;
713 -- If Rep_Clauses are to be ignored, remove address clause from
714 -- list attached to entity, because it may be illegal for gigi,
715 -- for example by breaking order of elaboration.
717 if Ignore_Rep_Clauses then
718 declare
719 Rep : Node_Id;
721 begin
722 Rep := First_Rep_Item (E);
724 if Rep = Addr then
725 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
727 else
728 while Present (Rep)
729 and then Next_Rep_Item (Rep) /= Addr
730 loop
731 Next_Rep_Item (Rep);
732 end loop;
733 end if;
735 if Present (Rep) then
736 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
737 end if;
738 end;
740 -- And now remove the address clause
742 Kill_Rep_Clause (Addr);
744 elsif not Error_Posted (Expr)
745 and then not Needs_Finalization (Typ)
746 then
747 Warn_Overlay (Expr, Typ, Name (Addr));
748 end if;
750 Init := Expression (Decl);
752 -- If a variable, or a non-imported constant, overlays a constant
753 -- object and has an initialization value, then the initialization
754 -- may end up writing into read-only memory. Detect the cases of
755 -- statically identical values and remove the initialization. In
756 -- the other cases, give a warning. We will give other warnings
757 -- later for the variable if it is assigned.
759 if (Ekind (E) = E_Variable
760 or else (Ekind (E) = E_Constant
761 and then not Is_Imported (E)))
762 and then Overlays_Constant (E)
763 and then Present (Init)
764 then
765 declare
766 O_Ent : Entity_Id;
767 Off : Boolean;
769 begin
770 Find_Overlaid_Entity (Addr, O_Ent, Off);
772 if Ekind (O_Ent) = E_Constant
773 and then Etype (O_Ent) = Typ
774 and then Present (Constant_Value (O_Ent))
775 and then Compile_Time_Compare
776 (Init,
777 Constant_Value (O_Ent),
778 Assume_Valid => True) = EQ
779 then
780 Set_No_Initialization (Decl);
781 return;
783 elsif Comes_From_Source (Init)
784 and then Address_Clause_Overlay_Warnings
785 then
786 Error_Msg_Sloc := Sloc (Addr);
787 Error_Msg_NE
788 ("?o?constant& may be modified via address clause#",
789 Decl, O_Ent);
790 end if;
791 end;
792 end if;
794 -- Remove side effects from initial expression, except in the case of
795 -- limited build-in-place calls and aggregates, which have their own
796 -- expansion elsewhere. This exception is necessary to avoid copying
797 -- limited objects.
799 if Present (Init)
800 and then not Is_Limited_View (Typ)
801 then
802 -- Capture initialization value at point of declaration, and make
803 -- explicit assignment legal, because object may be a constant.
805 Remove_Side_Effects (Init);
806 Lhs := New_Occurrence_Of (E, Sloc (Decl));
807 Set_Assignment_OK (Lhs);
809 -- Move initialization to freeze actions, once the object has
810 -- been frozen and the address clause alignment check has been
811 -- performed.
813 Append_Freeze_Action (E,
814 Make_Assignment_Statement (Sloc (Decl),
815 Name => Lhs,
816 Expression => Expression (Decl)));
818 Set_No_Initialization (Decl);
820 -- If the object is tagged, check whether the tag must be
821 -- reassigned explicitly.
823 Tag_Assign := Make_Tag_Assignment (Decl);
824 if Present (Tag_Assign) then
825 Append_Freeze_Action (E, Tag_Assign);
826 end if;
827 end if;
828 end if;
829 end Check_Address_Clause;
831 -----------------------------
832 -- Check_Compile_Time_Size --
833 -----------------------------
835 procedure Check_Compile_Time_Size (T : Entity_Id) is
837 procedure Set_Small_Size (T : Entity_Id; S : Uint);
838 -- Sets the compile time known size in the RM_Size field of T, checking
839 -- for a size clause that was given which attempts to give a small size.
841 function Size_Known (T : Entity_Id) return Boolean;
842 -- Recursive function that does all the work
844 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
845 -- If T is a constrained subtype, its size is not known if any of its
846 -- discriminant constraints is not static and it is not a null record.
847 -- The test is conservative and doesn't check that the components are
848 -- in fact constrained by non-static discriminant values. Could be made
849 -- more precise ???
851 --------------------
852 -- Set_Small_Size --
853 --------------------
855 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
856 begin
857 if S > System_Max_Integer_Size then
858 return;
860 -- Check for bad size clause given
862 elsif Has_Size_Clause (T) then
863 if RM_Size (T) < S then
864 Error_Msg_Uint_1 := S;
865 Error_Msg_NE (Size_Too_Small_Message, Size_Clause (T), T);
866 end if;
868 -- Set size if not set already. Do not set it to Uint_0, because in
869 -- some cases (notably array-of-record), the Component_Size is
870 -- No_Uint, which causes S to be Uint_0. Presumably the RM_Size and
871 -- Component_Size will eventually be set correctly by the back end.
873 elsif not Known_RM_Size (T) and then S /= Uint_0 then
874 Set_RM_Size (T, S);
875 end if;
876 end Set_Small_Size;
878 ----------------
879 -- Size_Known --
880 ----------------
882 function Size_Known (T : Entity_Id) return Boolean is
883 Comp : Entity_Id;
884 Ctyp : Entity_Id;
886 begin
887 if Size_Known_At_Compile_Time (T) then
888 return True;
890 -- Always True for elementary types, even generic formal elementary
891 -- types. We used to return False in the latter case, but the size
892 -- is known at compile time, even in the template, we just do not
893 -- know the exact size but that's not the point of this routine.
895 elsif Is_Elementary_Type (T) or else Is_Task_Type (T) then
896 return True;
898 -- Array types
900 elsif Is_Array_Type (T) then
902 -- String literals always have known size, and we can set it
904 if Ekind (T) = E_String_Literal_Subtype then
905 if Known_Component_Size (T) then
906 Set_Small_Size
907 (T, Component_Size (T) * String_Literal_Length (T));
909 else
910 -- The following is wrong, but does what previous versions
911 -- did. The Component_Size is unknown for the string in a
912 -- pragma Warnings.
913 Set_Small_Size (T, Uint_0);
914 end if;
916 return True;
918 -- Unconstrained types never have known at compile time size
920 elsif not Is_Constrained (T) then
921 return False;
923 -- Don't do any recursion on type with error posted, since we may
924 -- have a malformed type that leads us into a loop.
926 elsif Error_Posted (T) then
927 return False;
929 -- Otherwise if component size unknown, then array size unknown
931 elsif not Size_Known (Component_Type (T)) then
932 return False;
933 end if;
935 -- Check for all indexes static, and also compute possible size
936 -- (in case it is not greater than System_Max_Integer_Size and
937 -- thus may be packable).
939 declare
940 Index : Entity_Id;
941 Low : Node_Id;
942 High : Node_Id;
943 Size : Uint := Component_Size (T);
944 Dim : Uint;
946 begin
947 -- See comment in Set_Small_Size above
949 if No (Size) then
950 Size := Uint_0;
951 end if;
953 Index := First_Index (T);
954 while Present (Index) loop
955 if Nkind (Index) = N_Range then
956 Get_Index_Bounds (Index, Low, High);
958 elsif Error_Posted (Scalar_Range (Etype (Index))) then
959 return False;
961 else
962 Low := Type_Low_Bound (Etype (Index));
963 High := Type_High_Bound (Etype (Index));
964 end if;
966 if not Compile_Time_Known_Value (Low)
967 or else not Compile_Time_Known_Value (High)
968 or else Etype (Index) = Any_Type
969 then
970 return False;
972 else
973 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
975 if Dim > Uint_0 then
976 Size := Size * Dim;
977 else
978 Size := Uint_0;
979 end if;
980 end if;
982 Next_Index (Index);
983 end loop;
985 Set_Small_Size (T, Size);
986 return True;
987 end;
989 -- For non-generic private types, go to underlying type if present
991 elsif Is_Private_Type (T)
992 and then not Is_Generic_Type (T)
993 and then Present (Underlying_Type (T))
994 then
995 -- Don't do any recursion on type with error posted, since we may
996 -- have a malformed type that leads us into a loop.
998 if Error_Posted (T) then
999 return False;
1000 else
1001 return Size_Known (Underlying_Type (T));
1002 end if;
1004 -- Record types
1006 elsif Is_Record_Type (T) then
1008 -- A class-wide type is never considered to have a known size
1010 if Is_Class_Wide_Type (T) then
1011 return False;
1013 -- A subtype of a variant record must not have non-static
1014 -- discriminated components.
1016 elsif T /= Base_Type (T)
1017 and then not Static_Discriminated_Components (T)
1018 then
1019 return False;
1021 -- Don't do any recursion on type with error posted, since we may
1022 -- have a malformed type that leads us into a loop.
1024 elsif Error_Posted (T) then
1025 return False;
1026 end if;
1028 -- Now look at the components of the record
1030 declare
1031 -- The following two variables are used to keep track of the
1032 -- size of packed records if we can tell the size of the packed
1033 -- record in the front end. Packed_Size_Known is True if so far
1034 -- we can figure out the size. It is initialized to True for a
1035 -- packed record, unless the record has either discriminants or
1036 -- independent components, or is a strict-alignment type, since
1037 -- it cannot be fully packed in this case.
1039 -- The reason we eliminate the discriminated case is that
1040 -- we don't know the way the back end lays out discriminated
1041 -- packed records. If Packed_Size_Known is True, then
1042 -- Packed_Size is the size in bits so far.
1044 Packed_Size_Known : Boolean :=
1045 Is_Packed (T)
1046 and then not Has_Discriminants (T)
1047 and then not Has_Independent_Components (T)
1048 and then not Strict_Alignment (T);
1050 Packed_Size : Uint := Uint_0;
1051 -- Size in bits so far
1053 begin
1054 -- Test for variant part present
1056 if Has_Discriminants (T)
1057 and then Present (Parent (T))
1058 and then Nkind (Parent (T)) = N_Full_Type_Declaration
1059 and then Nkind (Type_Definition (Parent (T))) =
1060 N_Record_Definition
1061 and then not Null_Present (Type_Definition (Parent (T)))
1062 and then
1063 Present (Variant_Part
1064 (Component_List (Type_Definition (Parent (T)))))
1065 then
1066 -- If variant part is present, and type is unconstrained,
1067 -- then we must have defaulted discriminants, or a size
1068 -- clause must be present for the type, or else the size
1069 -- is definitely not known at compile time.
1071 if not Is_Constrained (T)
1072 and then
1073 No (Discriminant_Default_Value (First_Discriminant (T)))
1074 and then not Known_RM_Size (T)
1075 then
1076 return False;
1077 end if;
1078 end if;
1080 -- Loop through components
1082 Comp := First_Component_Or_Discriminant (T);
1083 while Present (Comp) loop
1084 Ctyp := Etype (Comp);
1086 -- We do not know the packed size if there is a component
1087 -- clause present (we possibly could, but this would only
1088 -- help in the case of a record with partial rep clauses.
1089 -- That's because in the case of full rep clauses, the
1090 -- size gets figured out anyway by a different circuit).
1092 if Present (Component_Clause (Comp)) then
1093 Packed_Size_Known := False;
1094 end if;
1096 -- We do not know the packed size for an independent
1097 -- component or if it is of a strict-alignment type,
1098 -- since packing does not touch these (RM 13.2(7)).
1100 if Is_Independent (Comp)
1101 or else Is_Independent (Ctyp)
1102 or else Strict_Alignment (Ctyp)
1103 then
1104 Packed_Size_Known := False;
1105 end if;
1107 -- We need to identify a component that is an array where
1108 -- the index type is an enumeration type with non-standard
1109 -- representation, and some bound of the type depends on a
1110 -- discriminant.
1112 -- This is because gigi computes the size by doing a
1113 -- substitution of the appropriate discriminant value in
1114 -- the size expression for the base type, and gigi is not
1115 -- clever enough to evaluate the resulting expression (which
1116 -- involves a call to rep_to_pos) at compile time.
1118 -- It would be nice if gigi would either recognize that
1119 -- this expression can be computed at compile time, or
1120 -- alternatively figured out the size from the subtype
1121 -- directly, where all the information is at hand ???
1123 if Is_Array_Type (Etype (Comp))
1124 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1125 then
1126 declare
1127 Ocomp : constant Entity_Id :=
1128 Original_Record_Component (Comp);
1129 OCtyp : constant Entity_Id := Etype (Ocomp);
1130 Ind : Node_Id;
1131 Indtyp : Entity_Id;
1132 Lo, Hi : Node_Id;
1134 begin
1135 Ind := First_Index (OCtyp);
1136 while Present (Ind) loop
1137 Indtyp := Etype (Ind);
1139 if Is_Enumeration_Type (Indtyp)
1140 and then Has_Non_Standard_Rep (Indtyp)
1141 then
1142 Lo := Type_Low_Bound (Indtyp);
1143 Hi := Type_High_Bound (Indtyp);
1145 if Is_Entity_Name (Lo)
1146 and then Ekind (Entity (Lo)) = E_Discriminant
1147 then
1148 return False;
1150 elsif Is_Entity_Name (Hi)
1151 and then Ekind (Entity (Hi)) = E_Discriminant
1152 then
1153 return False;
1154 end if;
1155 end if;
1157 Next_Index (Ind);
1158 end loop;
1159 end;
1160 end if;
1162 -- Clearly size of record is not known if the size of one of
1163 -- the components is not known.
1165 if not Size_Known (Ctyp) then
1166 return False;
1167 end if;
1169 -- Accumulate packed size if possible
1171 if Packed_Size_Known then
1173 -- We can deal with elementary types, small packed arrays
1174 -- if the representation is a modular type and also small
1175 -- record types as checked by Set_Small_Size.
1177 if Is_Elementary_Type (Ctyp)
1178 or else (Is_Array_Type (Ctyp)
1179 and then Present
1180 (Packed_Array_Impl_Type (Ctyp))
1181 and then Is_Modular_Integer_Type
1182 (Packed_Array_Impl_Type (Ctyp)))
1183 or else Is_Record_Type (Ctyp)
1184 then
1185 -- If RM_Size is known and static, then we can keep
1186 -- accumulating the packed size.
1188 if Known_Static_RM_Size (Ctyp) then
1190 Packed_Size := Packed_Size + RM_Size (Ctyp);
1192 -- If we have a field whose RM_Size is not known then
1193 -- we can't figure out the packed size here.
1195 else
1196 Packed_Size_Known := False;
1197 end if;
1199 -- For other types we can't figure out the packed size
1201 else
1202 Packed_Size_Known := False;
1203 end if;
1204 end if;
1206 Next_Component_Or_Discriminant (Comp);
1207 end loop;
1209 if Packed_Size_Known then
1210 Set_Small_Size (T, Packed_Size);
1211 end if;
1213 return True;
1214 end;
1216 -- All other cases, size not known at compile time
1218 else
1219 return False;
1220 end if;
1221 end Size_Known;
1223 -------------------------------------
1224 -- Static_Discriminated_Components --
1225 -------------------------------------
1227 function Static_Discriminated_Components
1228 (T : Entity_Id) return Boolean
1230 Constraint : Elmt_Id;
1232 begin
1233 if Has_Discriminants (T)
1234 and then Present (Discriminant_Constraint (T))
1235 and then Present (First_Component (T))
1236 then
1237 Constraint := First_Elmt (Discriminant_Constraint (T));
1238 while Present (Constraint) loop
1239 if not Compile_Time_Known_Value (Node (Constraint)) then
1240 return False;
1241 end if;
1243 Next_Elmt (Constraint);
1244 end loop;
1245 end if;
1247 return True;
1248 end Static_Discriminated_Components;
1250 -- Start of processing for Check_Compile_Time_Size
1252 begin
1253 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1254 end Check_Compile_Time_Size;
1256 -----------------------------------
1257 -- Check_Component_Storage_Order --
1258 -----------------------------------
1260 procedure Check_Component_Storage_Order
1261 (Encl_Type : Entity_Id;
1262 Comp : Entity_Id;
1263 ADC : Node_Id;
1264 Comp_ADC_Present : out Boolean)
1266 Comp_Base : Entity_Id;
1267 Comp_ADC : Node_Id;
1268 Encl_Base : Entity_Id;
1269 Err_Node : Node_Id;
1271 Component_Aliased : Boolean;
1273 Comp_Byte_Aligned : Boolean := False;
1274 -- Set for the record case, True if Comp is aligned on byte boundaries
1275 -- (in which case it is allowed to have different storage order).
1277 Comp_SSO_Differs : Boolean;
1278 -- Set True when the component is a nested composite, and it does not
1279 -- have the same scalar storage order as Encl_Type.
1281 begin
1282 -- Record case
1284 if Present (Comp) then
1285 Err_Node := Comp;
1286 Comp_Base := Etype (Comp);
1288 if Is_Tag (Comp) then
1289 Comp_Byte_Aligned := True;
1290 Component_Aliased := False;
1292 else
1293 -- If a component clause is present, check if the component starts
1294 -- and ends on byte boundaries. Otherwise conservatively assume it
1295 -- does so only in the case where the record is not packed.
1297 if Present (Component_Clause (Comp)) then
1298 Comp_Byte_Aligned :=
1299 Known_Normalized_First_Bit (Comp)
1300 and then
1301 Known_Esize (Comp)
1302 and then
1303 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0
1304 and then
1305 Esize (Comp) mod System_Storage_Unit = 0;
1306 else
1307 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1308 end if;
1310 Component_Aliased := Is_Aliased (Comp);
1311 end if;
1313 -- Array case
1315 else
1316 Err_Node := Encl_Type;
1317 Comp_Base := Component_Type (Encl_Type);
1319 Component_Aliased := Has_Aliased_Components (Encl_Type);
1320 end if;
1322 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1323 -- the attribute definition clause is attached to the first subtype.
1324 -- Also, if the base type is incomplete or private, go to full view
1325 -- if known
1327 Encl_Base := Base_Type (Encl_Type);
1328 if Present (Underlying_Type (Encl_Base)) then
1329 Encl_Base := Underlying_Type (Encl_Base);
1330 end if;
1332 Comp_Base := Base_Type (Comp_Base);
1333 if Present (Underlying_Type (Comp_Base)) then
1334 Comp_Base := Underlying_Type (Comp_Base);
1335 end if;
1337 Comp_ADC :=
1338 Get_Attribute_Definition_Clause
1339 (First_Subtype (Comp_Base), Attribute_Scalar_Storage_Order);
1340 Comp_ADC_Present := Present (Comp_ADC);
1342 -- Case of record or array component: check storage order compatibility.
1343 -- But, if the record has Complex_Representation, then it is treated as
1344 -- a scalar in the back end so the storage order is irrelevant.
1346 if (Is_Record_Type (Comp_Base)
1347 and then not Has_Complex_Representation (Comp_Base))
1348 or else Is_Array_Type (Comp_Base)
1349 then
1350 Comp_SSO_Differs :=
1351 Reverse_Storage_Order (Encl_Base) /=
1352 Reverse_Storage_Order (Comp_Base);
1354 -- Parent and extension must have same storage order
1356 if Present (Comp) and then Chars (Comp) = Name_uParent then
1357 if Comp_SSO_Differs then
1358 Error_Msg_N
1359 ("record extension must have same scalar storage order as "
1360 & "parent", Err_Node);
1361 end if;
1363 -- If component and composite SSO differs, check that component
1364 -- falls on byte boundaries and isn't bit packed.
1366 elsif Comp_SSO_Differs then
1368 -- Component SSO differs from enclosing composite:
1370 -- Reject if composite is a bit-packed array, as it is rewritten
1371 -- into an array of scalars.
1373 if Is_Bit_Packed_Array (Encl_Base) then
1374 Error_Msg_N
1375 ("type of packed array must have same scalar storage order "
1376 & "as component", Err_Node);
1378 -- Reject if not byte aligned
1380 elsif Is_Record_Type (Encl_Base)
1381 and then not Comp_Byte_Aligned
1382 then
1383 if Present (Component_Clause (Comp)) then
1384 Error_Msg_N
1385 ("type of non-byte-aligned component must have same scalar"
1386 & " storage order as enclosing record", Err_Node);
1387 else
1388 Error_Msg_N
1389 ("type of packed component must have same scalar"
1390 & " storage order as enclosing record", Err_Node);
1391 end if;
1393 -- Warn if specified only for the outer composite
1395 elsif Present (ADC) and then No (Comp_ADC) then
1396 Error_Msg_NE
1397 ("scalar storage order specified for & does not apply to "
1398 & "component?", Err_Node, Encl_Base);
1399 end if;
1400 end if;
1402 -- Enclosing type has explicit SSO: non-composite component must not
1403 -- be aliased.
1405 elsif Present (ADC) and then Component_Aliased then
1406 Error_Msg_N
1407 ("aliased component not permitted for type with explicit "
1408 & "Scalar_Storage_Order", Err_Node);
1409 end if;
1410 end Check_Component_Storage_Order;
1412 -----------------------------
1413 -- Check_Debug_Info_Needed --
1414 -----------------------------
1416 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1417 begin
1418 if Debug_Info_Off (T) then
1419 return;
1421 elsif Comes_From_Source (T)
1422 or else Debug_Generated_Code
1423 or else Debug_Flag_VV
1424 or else Needs_Debug_Info (T)
1425 then
1426 Set_Debug_Info_Needed (T);
1427 end if;
1428 end Check_Debug_Info_Needed;
1430 -------------------------------
1431 -- Check_Expression_Function --
1432 -------------------------------
1434 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1435 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1436 -- Function to search for deferred constant
1438 -------------------
1439 -- Find_Constant --
1440 -------------------
1442 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1443 begin
1444 -- When a constant is initialized with the result of a dispatching
1445 -- call, the constant declaration is rewritten as a renaming of the
1446 -- displaced function result. This scenario is not a premature use of
1447 -- a constant even though the Has_Completion flag is not set.
1449 if Is_Entity_Name (Nod)
1450 and then Present (Entity (Nod))
1451 and then Ekind (Entity (Nod)) = E_Constant
1452 and then Scope (Entity (Nod)) = Current_Scope
1453 and then Nkind (Declaration_Node (Entity (Nod))) =
1454 N_Object_Declaration
1455 and then not Is_Imported (Entity (Nod))
1456 and then not Has_Completion (Entity (Nod))
1457 and then not (Present (Full_View (Entity (Nod)))
1458 and then Has_Completion (Full_View (Entity (Nod))))
1459 then
1460 Error_Msg_NE
1461 ("premature use of& in call or instance", N, Entity (Nod));
1463 elsif Nkind (Nod) = N_Attribute_Reference then
1464 Analyze (Prefix (Nod));
1466 if Is_Entity_Name (Prefix (Nod))
1467 and then Is_Type (Entity (Prefix (Nod)))
1468 then
1469 if Expander_Active then
1470 Check_Fully_Declared (Entity (Prefix (Nod)), N);
1471 end if;
1473 Freeze_Before (N, Entity (Prefix (Nod)));
1474 end if;
1475 end if;
1477 return OK;
1478 end Find_Constant;
1480 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1482 -- Local variables
1484 Decl : Node_Id;
1486 -- Start of processing for Check_Expression_Function
1488 begin
1489 Decl := Original_Node (Unit_Declaration_Node (Nam));
1491 -- The subprogram body created for the expression function is not
1492 -- itself a freeze point.
1494 if Scope (Nam) = Current_Scope
1495 and then Nkind (Decl) = N_Expression_Function
1496 and then Nkind (N) /= N_Subprogram_Body
1497 then
1498 Check_Deferred (Expression (Decl));
1499 end if;
1500 end Check_Expression_Function;
1502 --------------------------------
1503 -- Check_Inherited_Conditions --
1504 --------------------------------
1506 procedure Check_Inherited_Conditions
1507 (R : Entity_Id;
1508 Late_Overriding : Boolean := False)
1510 Prim_Ops : constant Elist_Id := Primitive_Operations (R);
1511 Decls : List_Id;
1512 Op_Node : Elmt_Id;
1513 Par_Prim : Entity_Id;
1514 Prim : Entity_Id;
1515 Wrapper_Needed : Boolean;
1517 function Build_DTW_Body
1518 (Loc : Source_Ptr;
1519 DTW_Spec : Node_Id;
1520 DTW_Decls : List_Id;
1521 Par_Prim : Entity_Id;
1522 Wrapped_Subp : Entity_Id) return Node_Id;
1523 -- Build the body of the dispatch table wrapper containing the given
1524 -- spec and declarations; the call to the wrapped subprogram includes
1525 -- the proper type conversion.
1527 function Build_DTW_Spec (Par_Prim : Entity_Id) return Node_Id;
1528 -- Build the spec of the dispatch table wrapper
1530 procedure Build_Inherited_Condition_Pragmas
1531 (Subp : Entity_Id;
1532 Wrapper_Needed : out Boolean);
1533 -- Build corresponding pragmas for an operation whose ancestor has
1534 -- class-wide pre/postconditions. If the operation is inherited then
1535 -- Wrapper_Needed is returned True to force the creation of a wrapper
1536 -- for the inherited operation. If the ancestor is being overridden,
1537 -- the pragmas are constructed only to verify their legality, in case
1538 -- they contain calls to other primitives that may have been overridden.
1540 function Needs_Wrapper
1541 (Class_Cond : Node_Id;
1542 Subp : Entity_Id;
1543 Par_Subp : Entity_Id) return Boolean;
1544 -- Checks whether the dispatch-table wrapper (DTW) for Subp must be
1545 -- built to evaluate the given class-wide condition.
1547 --------------------
1548 -- Build_DTW_Body --
1549 --------------------
1551 function Build_DTW_Body
1552 (Loc : Source_Ptr;
1553 DTW_Spec : Node_Id;
1554 DTW_Decls : List_Id;
1555 Par_Prim : Entity_Id;
1556 Wrapped_Subp : Entity_Id) return Node_Id
1558 Actuals : constant List_Id := Empty_List;
1559 Call : Node_Id;
1560 Formal : Entity_Id := First_Formal (Par_Prim);
1561 New_F_Spec : Entity_Id := First (Parameter_Specifications (DTW_Spec));
1562 New_Formal : Entity_Id;
1564 begin
1565 -- Build parameter association for call to wrapped subprogram
1567 while Present (Formal) loop
1568 New_Formal := Defining_Identifier (New_F_Spec);
1570 -- If the controlling argument is inherited, add conversion to
1571 -- parent type for the call.
1573 if Is_Controlling_Formal (Formal) then
1574 Append_To (Actuals,
1575 Make_Type_Conversion (Loc,
1576 New_Occurrence_Of (Etype (Formal), Loc),
1577 New_Occurrence_Of (New_Formal, Loc)));
1578 else
1579 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1580 end if;
1582 Next_Formal (Formal);
1583 Next (New_F_Spec);
1584 end loop;
1586 if Ekind (Wrapped_Subp) = E_Procedure then
1587 Call :=
1588 Make_Procedure_Call_Statement (Loc,
1589 Name => New_Occurrence_Of (Wrapped_Subp, Loc),
1590 Parameter_Associations => Actuals);
1591 else
1592 Call :=
1593 Make_Simple_Return_Statement (Loc,
1594 Expression =>
1595 Make_Function_Call (Loc,
1596 Name => New_Occurrence_Of (Wrapped_Subp, Loc),
1597 Parameter_Associations => Actuals));
1598 end if;
1600 return
1601 Make_Subprogram_Body (Loc,
1602 Specification => Copy_Subprogram_Spec (DTW_Spec),
1603 Declarations => DTW_Decls,
1604 Handled_Statement_Sequence =>
1605 Make_Handled_Sequence_Of_Statements (Loc,
1606 Statements => New_List (Call),
1607 End_Label => Make_Identifier (Loc,
1608 Chars (Defining_Entity (DTW_Spec)))));
1609 end Build_DTW_Body;
1611 --------------------
1612 -- Build_DTW_Spec --
1613 --------------------
1615 function Build_DTW_Spec (Par_Prim : Entity_Id) return Node_Id is
1616 DTW_Id : Entity_Id;
1617 DTW_Spec : Node_Id;
1619 begin
1620 DTW_Spec := Build_Overriding_Spec (Par_Prim, R);
1621 DTW_Id := Defining_Entity (DTW_Spec);
1623 -- Clear the not-overriding indicator since the DTW wrapper overrides
1624 -- its wrapped subprogram; required because if present in the parent
1625 -- primitive, given that Build_Overriding_Spec inherits it, we report
1626 -- spurious errors.
1628 Set_Must_Not_Override (DTW_Spec, False);
1630 -- Add minimal decoration of fields
1632 Mutate_Ekind (DTW_Id, Ekind (Par_Prim));
1633 Set_LSP_Subprogram (DTW_Id, Par_Prim);
1634 Set_Is_Dispatch_Table_Wrapper (DTW_Id);
1635 Set_Is_Wrapper (DTW_Id);
1637 -- The DTW wrapper is never a null procedure
1639 if Nkind (DTW_Spec) = N_Procedure_Specification then
1640 Set_Null_Present (DTW_Spec, False);
1641 end if;
1643 return DTW_Spec;
1644 end Build_DTW_Spec;
1646 ---------------------------------------
1647 -- Build_Inherited_Condition_Pragmas --
1648 ---------------------------------------
1650 procedure Build_Inherited_Condition_Pragmas
1651 (Subp : Entity_Id;
1652 Wrapper_Needed : out Boolean)
1654 Class_Pre : constant Node_Id :=
1655 Class_Preconditions (Ultimate_Alias (Subp));
1656 Class_Post : Node_Id := Class_Postconditions (Par_Prim);
1657 A_Post : Node_Id;
1658 New_Prag : Node_Id;
1660 begin
1661 Wrapper_Needed := False;
1663 if No (Class_Pre) and then No (Class_Post) then
1664 return;
1665 end if;
1667 -- For class-wide preconditions we just evaluate whether the wrapper
1668 -- is needed; there is no need to build the pragma since the check
1669 -- is performed on the caller side.
1671 if Present (Class_Pre)
1672 and then Needs_Wrapper (Class_Pre, Subp, Par_Prim)
1673 then
1674 Wrapper_Needed := True;
1675 end if;
1677 -- For class-wide postconditions we evaluate whether the wrapper is
1678 -- needed and we build the class-wide postcondition pragma to install
1679 -- it in the wrapper.
1681 if Present (Class_Post)
1682 and then Needs_Wrapper (Class_Post, Subp, Par_Prim)
1683 then
1684 Wrapper_Needed := True;
1686 -- Update the class-wide postcondition
1688 Class_Post := New_Copy_Tree (Class_Post);
1689 Build_Class_Wide_Expression
1690 (Pragma_Or_Expr => Class_Post,
1691 Subp => Subp,
1692 Par_Subp => Par_Prim,
1693 Adjust_Sloc => False);
1695 -- Install the updated class-wide postcondition in a copy of the
1696 -- pragma postcondition defined for the nearest ancestor.
1698 A_Post := Get_Class_Wide_Pragma (Par_Prim,
1699 Pragma_Postcondition);
1701 if No (A_Post) then
1702 declare
1703 Subps : constant Subprogram_List :=
1704 Inherited_Subprograms (Subp);
1705 begin
1706 for Index in Subps'Range loop
1707 A_Post := Get_Class_Wide_Pragma (Subps (Index),
1708 Pragma_Postcondition);
1709 exit when Present (A_Post);
1710 end loop;
1711 end;
1712 end if;
1714 -- A_Post can be null here if the postcondition was inlined in the
1715 -- called subprogram.
1717 if Present (A_Post) then
1718 New_Prag := New_Copy_Tree (A_Post);
1719 Rewrite
1720 (Expression (First (Pragma_Argument_Associations (New_Prag))),
1721 Class_Post);
1722 Append (New_Prag, Decls);
1723 end if;
1724 end if;
1725 end Build_Inherited_Condition_Pragmas;
1727 -------------------
1728 -- Needs_Wrapper --
1729 -------------------
1731 function Needs_Wrapper
1732 (Class_Cond : Node_Id;
1733 Subp : Entity_Id;
1734 Par_Subp : Entity_Id) return Boolean
1736 Result : Boolean := False;
1738 function Check_Entity (N : Node_Id) return Traverse_Result;
1739 -- Check calls to overridden primitives
1741 --------------------
1742 -- Replace_Entity --
1743 --------------------
1745 function Check_Entity (N : Node_Id) return Traverse_Result is
1746 New_E : Entity_Id;
1748 begin
1749 if Nkind (N) = N_Identifier
1750 and then Present (Entity (N))
1751 and then
1752 (Is_Formal (Entity (N)) or else Is_Subprogram (Entity (N)))
1753 and then
1754 (Nkind (Parent (N)) /= N_Attribute_Reference
1755 or else Attribute_Name (Parent (N)) /= Name_Class)
1756 then
1757 -- Determine whether entity has a renaming
1759 New_E := Get_Mapped_Entity (Entity (N));
1761 -- If the entity is an overridden primitive and we are not
1762 -- in GNATprove mode, we must build a wrapper for the current
1763 -- inherited operation. If the reference is the prefix of an
1764 -- attribute such as 'Result (or others ???) there is no need
1765 -- for a wrapper: the condition is just rewritten in terms of
1766 -- the inherited subprogram.
1768 if Present (New_E)
1769 and then Comes_From_Source (New_E)
1770 and then Is_Subprogram (New_E)
1771 and then Nkind (Parent (N)) /= N_Attribute_Reference
1772 and then not GNATprove_Mode
1773 then
1774 Result := True;
1775 return Abandon;
1776 end if;
1777 end if;
1779 return OK;
1780 end Check_Entity;
1782 procedure Check_Condition_Entities is
1783 new Traverse_Proc (Check_Entity);
1785 -- Start of processing for Needs_Wrapper
1787 begin
1788 Update_Primitives_Mapping (Par_Subp, Subp);
1790 Map_Formals (Par_Subp, Subp);
1791 Check_Condition_Entities (Class_Cond);
1793 return Result;
1794 end Needs_Wrapper;
1796 Ifaces_List : Elist_Id := No_Elist;
1797 Ifaces_Listed : Boolean := False;
1798 -- Cache the list of interface operations inherited by R
1800 Wrappers_List : Elist_Id := No_Elist;
1801 -- List containing identifiers of built wrappers. Used to defer building
1802 -- and analyzing their class-wide precondition subprograms.
1804 -- Start of processing for Check_Inherited_Conditions
1806 begin
1807 if Late_Overriding then
1808 Op_Node := First_Elmt (Prim_Ops);
1809 while Present (Op_Node) loop
1810 Prim := Node (Op_Node);
1812 -- Map the overridden primitive to the overriding one
1814 if Present (Overridden_Operation (Prim))
1815 and then Comes_From_Source (Prim)
1816 then
1817 Par_Prim := Overridden_Operation (Prim);
1818 Update_Primitives_Mapping (Par_Prim, Prim);
1820 -- Force discarding previous mappings of its formals
1822 Map_Formals (Par_Prim, Prim, Force_Update => True);
1823 end if;
1825 Next_Elmt (Op_Node);
1826 end loop;
1827 end if;
1829 -- Perform validity checks on the inherited conditions of overriding
1830 -- operations, for conformance with LSP, and apply SPARK-specific
1831 -- restrictions on inherited conditions.
1833 Op_Node := First_Elmt (Prim_Ops);
1834 while Present (Op_Node) loop
1835 Prim := Node (Op_Node);
1837 Par_Prim := Overridden_Operation (Prim);
1838 if Present (Par_Prim)
1839 and then Comes_From_Source (Prim)
1840 then
1841 -- When the primitive is an LSP wrapper we climb to the parent
1842 -- primitive that has the inherited contract.
1844 if Is_Wrapper (Par_Prim)
1845 and then Present (LSP_Subprogram (Par_Prim))
1846 then
1847 Par_Prim := LSP_Subprogram (Par_Prim);
1848 end if;
1850 -- Check that overrider and overridden operations have
1851 -- the same strub mode.
1853 Check_Same_Strub_Mode (Prim, Par_Prim);
1855 -- Analyze the contract items of the overridden operation, before
1856 -- they are rewritten as pragmas.
1858 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1860 -- In GNATprove mode this is where we can collect the inherited
1861 -- conditions, because we do not create the Check pragmas that
1862 -- normally convey the modified class-wide conditions on
1863 -- overriding operations.
1865 if GNATprove_Mode then
1866 Collect_Inherited_Class_Wide_Conditions (Prim);
1867 end if;
1868 end if;
1870 -- Go over operations inherited from interfaces and check
1871 -- them for strub mode compatibility as well.
1873 if Has_Interfaces (R)
1874 and then Is_Dispatching_Operation (Prim)
1875 and then Find_Dispatching_Type (Prim) = R
1876 then
1877 declare
1878 Elmt : Elmt_Id;
1879 Iface_Elmt : Elmt_Id;
1880 Iface : Entity_Id;
1881 Iface_Prim : Entity_Id;
1883 begin
1884 -- Collect the interfaces only once. We haven't
1885 -- finished freezing yet, so we can't use the faster
1886 -- search from Sem_Disp.Covered_Interface_Primitives.
1888 if not Ifaces_Listed then
1889 Collect_Interfaces (R, Ifaces_List);
1890 Ifaces_Listed := True;
1891 end if;
1893 Iface_Elmt := First_Elmt (Ifaces_List);
1894 while Present (Iface_Elmt) loop
1895 Iface := Node (Iface_Elmt);
1897 Elmt := First_Elmt (Primitive_Operations (Iface));
1898 while Present (Elmt) loop
1899 Iface_Prim := Node (Elmt);
1901 if Iface_Prim /= Par_Prim
1902 and then Chars (Iface_Prim) = Chars (Prim)
1903 and then Comes_From_Source (Iface_Prim)
1904 and then Is_Interface_Conformant
1905 (R, Iface_Prim, Prim)
1906 then
1907 Check_Same_Strub_Mode (Prim, Iface_Prim);
1908 end if;
1910 Next_Elmt (Elmt);
1911 end loop;
1913 Next_Elmt (Iface_Elmt);
1914 end loop;
1915 end;
1916 end if;
1918 Next_Elmt (Op_Node);
1919 end loop;
1921 -- Now examine the inherited operations to check whether they require
1922 -- a wrapper to handle inherited conditions that call other primitives,
1923 -- so that LSP can be verified/enforced.
1925 Op_Node := First_Elmt (Prim_Ops);
1927 while Present (Op_Node) loop
1928 Decls := Empty_List;
1929 Prim := Node (Op_Node);
1930 Wrapper_Needed := False;
1932 -- Skip internal entities built for mapping interface primitives
1934 if not Comes_From_Source (Prim)
1935 and then Present (Alias (Prim))
1936 and then No (Interface_Alias (Prim))
1937 then
1938 Par_Prim := Ultimate_Alias (Prim);
1940 -- When the primitive is an LSP wrapper we climb to the parent
1941 -- primitive that has the inherited contract.
1943 if Is_Wrapper (Par_Prim)
1944 and then Present (LSP_Subprogram (Par_Prim))
1945 then
1946 Par_Prim := LSP_Subprogram (Par_Prim);
1947 end if;
1949 -- Analyze the contract items of the parent operation, and
1950 -- determine whether a wrapper is needed. This is determined
1951 -- when the condition is rewritten in sem_prag, using the
1952 -- mapping between overridden and overriding operations built
1953 -- in the loop above.
1955 Analyze_Entry_Or_Subprogram_Contract (Par_Prim);
1956 Build_Inherited_Condition_Pragmas (Prim, Wrapper_Needed);
1957 end if;
1959 if Wrapper_Needed
1960 and then not Is_Abstract_Subprogram (Par_Prim)
1961 and then Expander_Active
1962 then
1963 -- Build the dispatch-table wrapper (DTW). The support for
1964 -- AI12-0195 relies on two kind of wrappers: one for indirect
1965 -- calls (also used for AI12-0220), and one for putting in the
1966 -- dispatch table:
1968 -- 1) "indirect-call wrapper" (ICW) is needed anytime there are
1969 -- class-wide preconditions. Prim'Access will point directly
1970 -- at the ICW if any, or at the "pristine" body if Prim has
1971 -- no class-wide preconditions.
1973 -- 2) "dispatch-table wrapper" (DTW) is needed anytime the class
1974 -- wide preconditions *or* the class-wide postconditions are
1975 -- affected by overriding.
1977 -- The DTW holds a single statement that is a single call where
1978 -- the controlling actuals are conversions to the corresponding
1979 -- type in the parent primitive. If the primitive is a function
1980 -- the statement is a return statement with a call.
1982 declare
1983 Alias_Id : constant Entity_Id := Ultimate_Alias (Prim);
1984 Loc : constant Source_Ptr := Sloc (R);
1985 DTW_Body : Node_Id;
1986 DTW_Decl : Node_Id;
1987 DTW_Id : Entity_Id;
1988 DTW_Spec : Node_Id;
1990 Prim_Next_E : constant Entity_Id := Next_Entity (Prim);
1991 Prim_Prev_E : constant Entity_Id := Prev_Entity (Prim);
1993 begin
1994 DTW_Spec := Build_DTW_Spec (Par_Prim);
1995 DTW_Id := Defining_Entity (DTW_Spec);
1996 DTW_Decl := Make_Subprogram_Declaration (Loc,
1997 Specification => DTW_Spec);
1999 -- The spec of the wrapper has been built using the source
2000 -- location of its parent primitive; we must update it now
2001 -- (with the source location of the internal primitive built
2002 -- by Derive_Subprogram that will override this wrapper) to
2003 -- avoid inlining conflicts between internally built helpers
2004 -- for class-wide pre/postconditions of the parent and the
2005 -- helpers built for this wrapper.
2007 Set_Sloc (DTW_Id, Sloc (Prim));
2009 -- For inherited class-wide preconditions the DTW wrapper
2010 -- reuses the ICW of the parent (which checks the parent
2011 -- interpretation of the class-wide preconditions); the
2012 -- interpretation of the class-wide preconditions for the
2013 -- inherited subprogram is checked at the caller side.
2015 -- When the subprogram inherits class-wide postconditions
2016 -- the DTW also checks the interpretation of the class-wide
2017 -- postconditions for the inherited subprogram, and the body
2018 -- of the parent checks its interpretation of the parent for
2019 -- the class-wide postconditions.
2021 -- procedure Prim (F1 : T1; ...) is
2022 -- [ pragma Check (Postcondition, Expr); ]
2023 -- begin
2024 -- Par_Prim_ICW (Par_Type (F1), ...);
2025 -- end;
2027 if Present (Indirect_Call_Wrapper (Par_Prim)) then
2028 DTW_Body :=
2029 Build_DTW_Body (Loc,
2030 DTW_Spec => DTW_Spec,
2031 DTW_Decls => Decls,
2032 Par_Prim => Par_Prim,
2033 Wrapped_Subp => Indirect_Call_Wrapper (Par_Prim));
2035 -- For subprograms that only inherit class-wide postconditions
2036 -- the DTW wrapper calls the parent primitive (which on its
2037 -- body checks the interpretation of the class-wide post-
2038 -- conditions for the parent subprogram), and the DTW checks
2039 -- the interpretation of the class-wide postconditions for the
2040 -- inherited subprogram.
2042 -- procedure Prim (F1 : T1; ...) is
2043 -- pragma Check (Postcondition, Expr);
2044 -- begin
2045 -- Par_Prim (Par_Type (F1), ...);
2046 -- end;
2048 else
2049 DTW_Body :=
2050 Build_DTW_Body (Loc,
2051 DTW_Spec => DTW_Spec,
2052 DTW_Decls => Decls,
2053 Par_Prim => Par_Prim,
2054 Wrapped_Subp => Par_Prim);
2055 end if;
2057 -- Insert the declaration of the wrapper before the freezing
2058 -- node of the record type declaration to ensure that it will
2059 -- override the internal primitive built by Derive_Subprogram.
2061 if Late_Overriding then
2062 Ensure_Freeze_Node (R);
2063 Insert_Before_And_Analyze (Freeze_Node (R), DTW_Decl);
2064 else
2065 Append_Freeze_Action (R, DTW_Decl);
2066 Analyze (DTW_Decl);
2067 end if;
2069 -- The analyis of DTW_Decl has removed Prim from its scope
2070 -- chain and added DTW_Id at the end of the scope chain. Move
2071 -- DTW_Id to its correct place in the scope chain: the analysis
2072 -- of the wrapper declaration has just added DTW_Id at the end
2073 -- of the list of entities of its scope. However, given that
2074 -- this wrapper overrides Prim, we must move DTW_Id to the
2075 -- original place of Prim in its scope chain. This is required
2076 -- for wrappers of private type primitives to ensure their
2077 -- correct visibility since wrappers are built when the full
2078 -- tagged type declaration is frozen (in the private part of
2079 -- the package) but they may override primitives defined in the
2080 -- public part of the package.
2082 declare
2083 DTW_Prev_E : constant Entity_Id := Prev_Entity (DTW_Id);
2085 begin
2086 pragma Assert (Last_Entity (Current_Scope) = DTW_Id);
2087 pragma Assert
2088 (Ekind (Current_Scope) not in E_Package | E_Generic_Package
2089 or else No (First_Private_Entity (Current_Scope))
2090 or else First_Private_Entity (Current_Scope) /= DTW_Id);
2092 -- Remove DTW_Id from the end of the doubly-linked list of
2093 -- entities of this scope; no need to handle removing it
2094 -- from the beginning of the chain since such case can never
2095 -- occur for this entity.
2097 Set_Last_Entity (Current_Scope, DTW_Prev_E);
2098 Set_Next_Entity (DTW_Prev_E, Empty);
2100 -- Place DTW_Id back in the original place of its wrapped
2101 -- primitive in the list of entities of this scope.
2103 Link_Entities (Prim_Prev_E, DTW_Id);
2104 Link_Entities (DTW_Id, Prim_Next_E);
2105 end;
2107 -- Insert the body of the wrapper in the freeze actions of
2108 -- its record type declaration to ensure that it is placed
2109 -- in the scope of its declaration but not too early to cause
2110 -- premature freezing of other entities.
2112 Append_Freeze_Action (R, DTW_Body);
2113 Analyze (DTW_Body);
2115 -- Ensure correct decoration
2117 pragma Assert (Is_Dispatching_Operation (DTW_Id));
2118 pragma Assert (Present (Overridden_Operation (DTW_Id)));
2119 pragma Assert (Overridden_Operation (DTW_Id) = Alias_Id);
2121 -- Inherit dispatch table slot
2123 Set_DTC_Entity_Value (R, DTW_Id);
2124 Set_DT_Position (DTW_Id, DT_Position (Alias_Id));
2126 -- Register the wrapper in the dispatch table
2128 if Late_Overriding
2129 and then not Building_Static_DT (R)
2130 then
2131 Insert_List_After_And_Analyze (Freeze_Node (R),
2132 Register_Primitive (Loc, DTW_Id));
2133 end if;
2135 -- Defer building helpers and ICW for the DTW. Required to
2136 -- ensure uniqueness in their names because when building
2137 -- these wrappers for overlapped subprograms their homonym
2138 -- number is not definite until all these dispatch table
2139 -- wrappers of tagged type R have been analyzed.
2141 if Present (Indirect_Call_Wrapper (Par_Prim)) then
2142 Append_New_Elmt (DTW_Id, Wrappers_List);
2143 end if;
2144 end;
2145 end if;
2147 Next_Elmt (Op_Node);
2148 end loop;
2150 -- Build and analyze deferred class-wide precondition subprograms of
2151 -- built wrappers.
2153 if Present (Wrappers_List) then
2154 declare
2155 Body_N : Node_Id;
2156 CW_Subp : Entity_Id;
2157 Decl_N : Node_Id;
2158 DTW_Id : Entity_Id;
2159 Elmt : Elmt_Id;
2161 begin
2162 Elmt := First_Elmt (Wrappers_List);
2164 while Present (Elmt) loop
2165 DTW_Id := Node (Elmt);
2166 Next_Elmt (Elmt);
2168 Merge_Class_Conditions (DTW_Id);
2169 Make_Class_Precondition_Subps (DTW_Id, Late_Overriding);
2171 CW_Subp := Static_Call_Helper (DTW_Id);
2172 Decl_N := Unit_Declaration_Node (CW_Subp);
2173 Analyze (Decl_N);
2175 -- If the DTW was built for a late-overriding primitive
2176 -- its body must be analyzed now (since the tagged type
2177 -- is already frozen).
2179 if Late_Overriding then
2180 Body_N :=
2181 Unit_Declaration_Node (Corresponding_Body (Decl_N));
2182 Analyze (Body_N);
2183 end if;
2184 end loop;
2185 end;
2186 end if;
2187 end Check_Inherited_Conditions;
2189 ----------------------------
2190 -- Check_Strict_Alignment --
2191 ----------------------------
2193 procedure Check_Strict_Alignment (E : Entity_Id) is
2194 Comp : Entity_Id;
2196 begin
2197 -- Bit-packed array types do not require strict alignment, even if they
2198 -- are by-reference types, because they are accessed in a special way.
2200 if Is_By_Reference_Type (E) and then not Is_Bit_Packed_Array (E) then
2201 Set_Strict_Alignment (E);
2203 elsif Is_Array_Type (E) then
2204 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
2206 -- ??? AI12-001: Any component of a packed type that contains an
2207 -- aliased part must be aligned according to the alignment of its
2208 -- subtype (RM 13.2(7)). This means that the following test:
2210 -- if Has_Aliased_Components (E) then
2211 -- Set_Strict_Alignment (E);
2212 -- end if;
2214 -- should be implemented here. Unfortunately it would break Florist,
2215 -- which has the bad habit of overaligning all the types it declares
2216 -- on 32-bit platforms. Other legacy codebases could also be affected
2217 -- because this check has historically been missing in GNAT.
2219 elsif Is_Record_Type (E) then
2220 Comp := First_Component (E);
2221 while Present (Comp) loop
2222 if not Is_Type (Comp)
2223 and then (Is_Aliased (Comp)
2224 or else Strict_Alignment (Etype (Comp)))
2225 then
2226 Set_Strict_Alignment (E);
2227 return;
2228 end if;
2230 Next_Component (Comp);
2231 end loop;
2232 end if;
2233 end Check_Strict_Alignment;
2235 -------------------------
2236 -- Check_Unsigned_Type --
2237 -------------------------
2239 procedure Check_Unsigned_Type (E : Entity_Id) is
2240 Ancestor : Entity_Id;
2241 Lo_Bound : Node_Id;
2242 Btyp : Entity_Id;
2244 begin
2245 if not Is_Discrete_Or_Fixed_Point_Type (E) then
2246 return;
2247 end if;
2249 -- Do not attempt to analyze case where range was in error
2251 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
2252 return;
2253 end if;
2255 -- The situation that is nontrivial is something like:
2257 -- subtype x1 is integer range -10 .. +10;
2258 -- subtype x2 is x1 range 0 .. V1;
2259 -- subtype x3 is x2 range V2 .. V3;
2260 -- subtype x4 is x3 range V4 .. V5;
2262 -- where Vn are variables. Here the base type is signed, but we still
2263 -- know that x4 is unsigned because of the lower bound of x2.
2265 -- The only way to deal with this is to look up the ancestor chain
2267 Ancestor := E;
2268 loop
2269 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
2270 return;
2271 end if;
2273 Lo_Bound := Type_Low_Bound (Ancestor);
2275 if Compile_Time_Known_Value (Lo_Bound) then
2276 if Expr_Rep_Value (Lo_Bound) >= 0 then
2277 Set_Is_Unsigned_Type (E, True);
2278 end if;
2280 return;
2282 else
2283 Ancestor := Ancestor_Subtype (Ancestor);
2285 -- If no ancestor had a static lower bound, go to base type
2287 if No (Ancestor) then
2289 -- Note: the reason we still check for a compile time known
2290 -- value for the base type is that at least in the case of
2291 -- generic formals, we can have bounds that fail this test,
2292 -- and there may be other cases in error situations.
2294 Btyp := Base_Type (E);
2296 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
2297 return;
2298 end if;
2300 Lo_Bound := Type_Low_Bound (Base_Type (E));
2302 if Compile_Time_Known_Value (Lo_Bound)
2303 and then Expr_Rep_Value (Lo_Bound) >= 0
2304 then
2305 Set_Is_Unsigned_Type (E, True);
2306 end if;
2308 return;
2309 end if;
2310 end if;
2311 end loop;
2312 end Check_Unsigned_Type;
2314 -----------------------------------------------
2315 -- Explode_Initialization_Compound_Statement --
2316 -----------------------------------------------
2318 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
2319 Init_Stmts : constant Node_Id := Initialization_Statements (E);
2321 begin
2322 if Present (Init_Stmts)
2323 and then Nkind (Init_Stmts) = N_Compound_Statement
2324 then
2325 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
2327 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
2328 -- just removing it, because Freeze_All may rely on this particular
2329 -- Node_Id still being present in the enclosing list to know where to
2330 -- stop freezing.
2332 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
2334 Set_Initialization_Statements (E, Empty);
2335 end if;
2336 end Explode_Initialization_Compound_Statement;
2338 ----------------
2339 -- Freeze_All --
2340 ----------------
2342 -- Note: the easy coding for this procedure would be to just build a
2343 -- single list of freeze nodes and then insert them and analyze them
2344 -- all at once. This won't work, because the analysis of earlier freeze
2345 -- nodes may recursively freeze types which would otherwise appear later
2346 -- on in the freeze list. So we must analyze and expand the freeze nodes
2347 -- as they are generated.
2349 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
2350 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
2351 -- This is the internal recursive routine that does freezing of entities
2352 -- (but NOT the analysis of default expressions, which should not be
2353 -- recursive, we don't want to analyze those till we are sure that ALL
2354 -- the types are frozen).
2356 --------------------
2357 -- Freeze_All_Ent --
2358 --------------------
2360 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
2361 E : Entity_Id;
2362 Flist : List_Id;
2364 procedure Process_Flist;
2365 -- If freeze nodes are present, insert and analyze, and reset cursor
2366 -- for next insertion.
2368 -------------------
2369 -- Process_Flist --
2370 -------------------
2372 procedure Process_Flist is
2373 Lastn : Node_Id;
2374 begin
2375 if Is_Non_Empty_List (Flist) then
2376 Lastn := Next (After);
2377 Insert_List_After_And_Analyze (After, Flist);
2379 if Present (Lastn) then
2380 After := Prev (Lastn);
2381 else
2382 After := Last (List_Containing (After));
2383 end if;
2384 end if;
2385 end Process_Flist;
2387 -- Start of processing for Freeze_All_Ent
2389 begin
2390 E := From;
2391 while Present (E) loop
2393 -- If the entity is an inner package which is not a package
2394 -- renaming, then its entities must be frozen at this point. Note
2395 -- that such entities do NOT get frozen at the end of the nested
2396 -- package itself (only library packages freeze).
2398 -- Same is true for task declarations, where anonymous records
2399 -- created for entry parameters must be frozen.
2401 if Ekind (E) = E_Package
2402 and then No (Renamed_Entity (E))
2403 and then not Is_Child_Unit (E)
2404 and then not Is_Frozen (E)
2405 then
2406 Push_Scope (E);
2408 Install_Visible_Declarations (E);
2409 Install_Private_Declarations (E);
2410 Freeze_All (First_Entity (E), After);
2412 End_Package_Scope (E);
2414 if Is_Generic_Instance (E)
2415 and then Has_Delayed_Freeze (E)
2416 then
2417 Set_Has_Delayed_Freeze (E, False);
2418 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
2419 end if;
2421 elsif Ekind (E) in Task_Kind
2422 and then Nkind (Parent (E)) in
2423 N_Single_Task_Declaration | N_Task_Type_Declaration
2424 then
2425 Push_Scope (E);
2426 Freeze_All (First_Entity (E), After);
2427 End_Scope;
2429 -- For a derived tagged type, we must ensure that all the
2430 -- primitive operations of the parent have been frozen, so that
2431 -- their addresses will be in the parent's dispatch table at the
2432 -- point it is inherited.
2434 elsif Ekind (E) = E_Record_Type
2435 and then Is_Tagged_Type (E)
2436 and then Is_Tagged_Type (Etype (E))
2437 and then Is_Derived_Type (E)
2438 then
2439 declare
2440 Prim_List : constant Elist_Id :=
2441 Primitive_Operations (Etype (E));
2443 Prim : Elmt_Id;
2444 Subp : Entity_Id;
2446 begin
2447 Prim := First_Elmt (Prim_List);
2448 while Present (Prim) loop
2449 Subp := Node (Prim);
2451 if Comes_From_Source (Subp)
2452 and then not Is_Frozen (Subp)
2453 then
2454 Flist := Freeze_Entity (Subp, After);
2455 Process_Flist;
2456 end if;
2458 Next_Elmt (Prim);
2459 end loop;
2460 end;
2461 end if;
2463 if not Is_Frozen (E) then
2464 Flist := Freeze_Entity (E, After);
2465 Process_Flist;
2467 -- If already frozen, and there are delayed aspects, this is where
2468 -- we do the visibility check for these aspects (see Sem_Ch13 spec
2469 -- for a description of how we handle aspect visibility).
2471 elsif Has_Delayed_Aspects (E) then
2472 declare
2473 Ritem : Node_Id;
2475 begin
2476 Ritem := First_Rep_Item (E);
2477 while Present (Ritem) loop
2478 if Nkind (Ritem) = N_Aspect_Specification
2479 and then Entity (Ritem) = E
2480 and then Is_Delayed_Aspect (Ritem)
2481 then
2482 Check_Aspect_At_End_Of_Declarations (Ritem);
2483 end if;
2485 Next_Rep_Item (Ritem);
2486 end loop;
2487 end;
2488 end if;
2490 -- If an incomplete type is still not frozen, this may be a
2491 -- premature freezing because of a body declaration that follows.
2492 -- Indicate where the freezing took place. Freezing will happen
2493 -- if the body comes from source, but not if it is internally
2494 -- generated, for example as the body of a type invariant.
2496 -- If the freezing is caused by the end of the current declarative
2497 -- part, it is a Taft Amendment type, and there is no error.
2499 if not Is_Frozen (E)
2500 and then Ekind (E) = E_Incomplete_Type
2501 then
2502 declare
2503 Bod : constant Node_Id := Next (After);
2505 begin
2506 -- The presence of a body freezes all entities previously
2507 -- declared in the current list of declarations, but this
2508 -- does not apply if the body does not come from source.
2509 -- A type invariant is transformed into a subprogram body
2510 -- which is placed at the end of the private part of the
2511 -- current package, but this body does not freeze incomplete
2512 -- types that may be declared in this private part.
2514 if Comes_From_Source (Bod)
2515 and then Nkind (Bod) in N_Entry_Body
2516 | N_Package_Body
2517 | N_Protected_Body
2518 | N_Subprogram_Body
2519 | N_Task_Body
2520 | N_Body_Stub
2521 and then
2522 In_Same_List (After, Parent (E))
2523 then
2524 Error_Msg_Sloc := Sloc (Next (After));
2525 Error_Msg_NE
2526 ("type& is frozen# before its full declaration",
2527 Parent (E), E);
2528 end if;
2529 end;
2530 end if;
2532 Next_Entity (E);
2533 end loop;
2534 end Freeze_All_Ent;
2536 -- Local variables
2538 Decl : Node_Id;
2539 E : Entity_Id;
2540 Item : Entity_Id;
2542 -- Start of processing for Freeze_All
2544 begin
2545 Freeze_All_Ent (From, After);
2547 -- Now that all types are frozen, we can deal with default expressions
2548 -- that require us to build a default expression functions. This is the
2549 -- point at which such functions are constructed (after all types that
2550 -- might be used in such expressions have been frozen).
2552 -- For subprograms that are renaming_as_body, we create the wrapper
2553 -- bodies as needed.
2555 -- We also add finalization chains to access types whose designated
2556 -- types are controlled. This is normally done when freezing the type,
2557 -- but this misses recursive type definitions where the later members
2558 -- of the recursion introduce controlled components.
2560 -- Loop through entities
2562 E := From;
2563 while Present (E) loop
2564 if Is_Subprogram (E) then
2565 if not Default_Expressions_Processed (E) then
2566 Process_Default_Expressions (E, After);
2567 end if;
2569 -- Check subprogram renamings for the same strub-mode.
2570 -- Avoid rechecking dispatching operations, that's taken
2571 -- care of in Check_Inherited_Conditions, that covers
2572 -- inherited interface operations.
2574 Item := Alias (E);
2575 if Present (Item)
2576 and then not Is_Dispatching_Operation (E)
2577 then
2578 Check_Same_Strub_Mode (E, Item);
2579 end if;
2581 if not Has_Completion (E) then
2582 Decl := Unit_Declaration_Node (E);
2584 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
2585 if Error_Posted (Decl) then
2586 Set_Has_Completion (E);
2587 else
2588 Build_And_Analyze_Renamed_Body (Decl, E, After);
2589 end if;
2591 elsif Nkind (Decl) = N_Subprogram_Declaration
2592 and then Present (Corresponding_Body (Decl))
2593 and then
2594 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl))) =
2595 N_Subprogram_Renaming_Declaration
2596 then
2597 Build_And_Analyze_Renamed_Body
2598 (Decl, Corresponding_Body (Decl), After);
2599 end if;
2600 end if;
2602 -- Freeze the default expressions of entries, entry families, and
2603 -- protected subprograms.
2605 elsif Is_Concurrent_Type (E) then
2606 Item := First_Entity (E);
2607 while Present (Item) loop
2608 if Is_Subprogram_Or_Entry (Item)
2609 and then not Default_Expressions_Processed (Item)
2610 then
2611 Process_Default_Expressions (Item, After);
2612 end if;
2614 Next_Entity (Item);
2615 end loop;
2616 end if;
2618 -- Historical note: We used to create a finalization master for an
2619 -- access type whose designated type is not controlled, but contains
2620 -- private controlled compoments. This form of postprocessing is no
2621 -- longer needed because the finalization master is now created when
2622 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
2624 Next_Entity (E);
2625 end loop;
2626 end Freeze_All;
2628 -----------------------
2629 -- Freeze_And_Append --
2630 -----------------------
2632 procedure Freeze_And_Append
2633 (Ent : Entity_Id;
2634 N : Node_Id;
2635 Result : in out List_Id)
2637 -- Freezing an Expression_Function does not freeze its profile:
2638 -- the formals will have been frozen otherwise before the E_F
2639 -- can be called.
2641 L : constant List_Id :=
2642 Freeze_Entity
2643 (Ent, N, Do_Freeze_Profile => not Is_Expression_Function (Ent));
2644 begin
2645 if Is_Non_Empty_List (L) then
2646 if Result = No_List then
2647 Result := L;
2648 else
2649 Append_List (L, Result);
2650 end if;
2651 end if;
2652 end Freeze_And_Append;
2654 -------------------
2655 -- Freeze_Before --
2656 -------------------
2658 procedure Freeze_Before
2659 (N : Node_Id;
2660 T : Entity_Id;
2661 Do_Freeze_Profile : Boolean := True)
2663 -- Freeze T, then insert the generated Freeze nodes before the node N.
2664 -- Flag Freeze_Profile is used when T is an overloadable entity, and
2665 -- indicates whether its profile should be frozen at the same time.
2667 Freeze_Nodes : constant List_Id :=
2668 Freeze_Entity (T, N, Do_Freeze_Profile);
2669 Pack : constant Entity_Id := Scope (T);
2671 begin
2672 if Ekind (T) = E_Function then
2673 Check_Expression_Function (N, T);
2674 end if;
2676 if Is_Non_Empty_List (Freeze_Nodes) then
2678 -- If the entity is a type declared in an inner package, it may be
2679 -- frozen by an outer declaration before the package itself is
2680 -- frozen. Install the package scope to analyze the freeze nodes,
2681 -- which may include generated subprograms such as predicate
2682 -- functions, etc.
2684 if Is_Type (T) and then From_Nested_Package (T) then
2685 Push_Scope (Pack);
2686 Install_Visible_Declarations (Pack);
2687 Install_Private_Declarations (Pack);
2688 Insert_Actions (N, Freeze_Nodes);
2689 End_Package_Scope (Pack);
2691 else
2692 Insert_Actions (N, Freeze_Nodes);
2693 end if;
2694 end if;
2695 end Freeze_Before;
2697 -------------------
2698 -- Freeze_Entity --
2699 -------------------
2701 -- WARNING: This routine manages Ghost regions. Return statements must be
2702 -- replaced by gotos which jump to the end of the routine and restore the
2703 -- Ghost mode.
2705 function Freeze_Entity
2706 (E : Entity_Id;
2707 N : Node_Id;
2708 Do_Freeze_Profile : Boolean := True) return List_Id
2710 Loc : constant Source_Ptr := Sloc (N);
2712 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2713 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2714 -- Save the Ghost-related attributes to restore on exit
2716 Atype : Entity_Id;
2717 Comp : Entity_Id;
2718 F_Node : Node_Id;
2719 Formal : Entity_Id;
2720 Indx : Node_Id;
2722 Result : List_Id := No_List;
2723 -- List of freezing actions, left at No_List if none
2725 Test_E : Entity_Id := E;
2726 -- A local temporary used to test if freezing is necessary for E, since
2727 -- its value can be set to something other than E in certain cases. For
2728 -- example, E cannot be used directly in cases such as when it is an
2729 -- Itype defined within a record - since it is the location of record
2730 -- which matters.
2732 procedure Add_To_Result (Fnod : Node_Id);
2733 -- Add freeze action Fnod to list Result
2735 function After_Last_Declaration return Boolean;
2736 -- If Loc is a freeze_entity that appears after the last declaration
2737 -- in the scope, inhibit error messages on late completion.
2739 procedure Check_Current_Instance (Comp_Decl : Node_Id);
2740 -- Check that an Access or Unchecked_Access attribute with a prefix
2741 -- which is the current instance type can only be applied when the type
2742 -- is limited.
2744 procedure Check_No_Parts_Violations
2745 (Typ : Entity_Id; Aspect_No_Parts : Aspect_Id) with
2746 Pre => Aspect_No_Parts in
2747 Aspect_No_Controlled_Parts | Aspect_No_Task_Parts;
2748 -- Check that Typ does not violate the semantics of the specified
2749 -- Aspect_No_Parts (No_Controlled_Parts or No_Task_Parts) when it is
2750 -- specified on Typ or one of its ancestors.
2752 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id);
2753 -- Give a warning for pragma Convention with language C or C++ applied
2754 -- to a discriminated record type. This is suppressed for the unchecked
2755 -- union case, since the whole point in this case is interface C. We
2756 -- also do not generate this within instantiations, since we will have
2757 -- generated a message on the template.
2759 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
2760 -- Give warning for modulus of 8, 16, 32, 64 or 128 given as an explicit
2761 -- integer literal without an explicit corresponding size clause. The
2762 -- caller has checked that Utype is a modular integer type.
2764 procedure Freeze_Array_Type (Arr : Entity_Id);
2765 -- Freeze array type, including freezing index and component types
2767 procedure Freeze_Object_Declaration (E : Entity_Id);
2768 -- Perform checks and generate freeze node if needed for a constant or
2769 -- variable declared by an object declaration.
2771 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
2772 -- Create Freeze_Generic_Entity nodes for types declared in a generic
2773 -- package. Recurse on inner generic packages.
2775 function Freeze_Profile (E : Entity_Id) return Boolean;
2776 -- Freeze formals and return type of subprogram. If some type in the
2777 -- profile is incomplete and we are in an instance, freezing of the
2778 -- entity will take place elsewhere, and the function returns False.
2780 procedure Freeze_Record_Type (Rec : Entity_Id);
2781 -- Freeze record type, including freezing component types, and freezing
2782 -- primitive operations if this is a tagged type.
2784 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2785 -- Determine whether an arbitrary entity is subject to Boolean aspect
2786 -- Import and its value is specified as True.
2788 procedure Inherit_Freeze_Node
2789 (Fnod : Node_Id;
2790 Typ : Entity_Id);
2791 -- Set type Typ's freeze node to refer to Fnode. This routine ensures
2792 -- that any attributes attached to Typ's original node are preserved.
2794 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2795 -- If E is an entity for an imported subprogram with pre/post-conditions
2796 -- then this procedure will create a wrapper to ensure that proper run-
2797 -- time checking of the pre/postconditions. See body for details.
2799 -------------------
2800 -- Add_To_Result --
2801 -------------------
2803 procedure Add_To_Result (Fnod : Node_Id) is
2804 begin
2805 Append_New_To (Result, Fnod);
2806 end Add_To_Result;
2808 ----------------------------
2809 -- After_Last_Declaration --
2810 ----------------------------
2812 function After_Last_Declaration return Boolean is
2813 Spec : constant Node_Id := Parent (Current_Scope);
2815 begin
2816 if Nkind (Spec) = N_Package_Specification then
2817 if Present (Private_Declarations (Spec)) then
2818 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2819 elsif Present (Visible_Declarations (Spec)) then
2820 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2821 else
2822 return False;
2823 end if;
2825 else
2826 return False;
2827 end if;
2828 end After_Last_Declaration;
2830 ----------------------------
2831 -- Check_Current_Instance --
2832 ----------------------------
2834 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2836 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2837 -- Determine whether Typ is compatible with the rules for aliased
2838 -- views of types as defined in RM 3.10 in the various dialects.
2840 function Process (N : Node_Id) return Traverse_Result;
2841 -- Process routine to apply check to given node
2843 -----------------------------
2844 -- Is_Aliased_View_Of_Type --
2845 -----------------------------
2847 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2848 Typ_Decl : constant Node_Id := Parent (Typ);
2850 begin
2851 -- Common case
2853 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2854 and then Limited_Present (Type_Definition (Typ_Decl))
2855 then
2856 return True;
2858 -- The following paragraphs describe what a legal aliased view of
2859 -- a type is in the various dialects of Ada.
2861 -- Ada 95
2863 -- The current instance of a limited type, and a formal parameter
2864 -- or generic formal object of a tagged type.
2866 -- Ada 95 limited type
2867 -- * Type with reserved word "limited"
2868 -- * A protected or task type
2869 -- * A composite type with limited component
2871 elsif Ada_Version <= Ada_95 then
2872 return Is_Limited_Type (Typ);
2874 -- Ada 2005
2876 -- The current instance of a limited tagged type, a protected
2877 -- type, a task type, or a type that has the reserved word
2878 -- "limited" in its full definition ... a formal parameter or
2879 -- generic formal object of a tagged type.
2881 -- Ada 2005 limited type
2882 -- * Type with reserved word "limited", "synchronized", "task"
2883 -- or "protected"
2884 -- * A composite type with limited component
2885 -- * A derived type whose parent is a non-interface limited type
2887 elsif Ada_Version = Ada_2005 then
2888 return
2889 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2890 or else
2891 (Is_Derived_Type (Typ)
2892 and then not Is_Interface (Etype (Typ))
2893 and then Is_Limited_Type (Etype (Typ)));
2895 -- Ada 2012 and beyond
2897 -- The current instance of an immutably limited type ... a formal
2898 -- parameter or generic formal object of a tagged type.
2900 -- Ada 2012 limited type
2901 -- * Type with reserved word "limited", "synchronized", "task"
2902 -- or "protected"
2903 -- * A composite type with limited component
2904 -- * A derived type whose parent is a non-interface limited type
2905 -- * An incomplete view
2907 -- Ada 2012 immutably limited type
2908 -- * Explicitly limited record type
2909 -- * Record extension with "limited" present
2910 -- * Non-formal limited private type that is either tagged
2911 -- or has at least one access discriminant with a default
2912 -- expression
2913 -- * Task type, protected type or synchronized interface
2914 -- * Type derived from immutably limited type
2916 else
2917 return
2918 Is_Immutably_Limited_Type (Typ)
2919 or else Is_Incomplete_Type (Typ);
2920 end if;
2921 end Is_Aliased_View_Of_Type;
2923 -------------
2924 -- Process --
2925 -------------
2927 function Process (N : Node_Id) return Traverse_Result is
2928 begin
2929 case Nkind (N) is
2930 when N_Attribute_Reference =>
2931 if Attribute_Name (N) in Name_Access | Name_Unchecked_Access
2932 and then Is_Entity_Name (Prefix (N))
2933 and then Is_Type (Entity (Prefix (N)))
2934 and then Entity (Prefix (N)) = E
2935 then
2936 if Ada_Version < Ada_2012 then
2937 Error_Msg_N
2938 ("current instance must be a limited type",
2939 Prefix (N));
2940 else
2941 Error_Msg_N
2942 ("current instance must be an immutably limited "
2943 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2944 end if;
2946 return Abandon;
2948 else
2949 return OK;
2950 end if;
2952 when others =>
2953 return OK;
2954 end case;
2955 end Process;
2957 procedure Traverse is new Traverse_Proc (Process);
2959 -- Local variables
2961 Rec_Type : constant Entity_Id :=
2962 Scope (Defining_Identifier (Comp_Decl));
2964 -- Start of processing for Check_Current_Instance
2966 begin
2967 if not Is_Aliased_View_Of_Type (Rec_Type) then
2968 Traverse (Comp_Decl);
2969 end if;
2970 end Check_Current_Instance;
2972 -------------------------------
2973 -- Check_No_Parts_Violations --
2974 -------------------------------
2976 procedure Check_No_Parts_Violations
2977 (Typ : Entity_Id; Aspect_No_Parts : Aspect_Id)
2980 function Find_Aspect_No_Parts
2981 (Typ : Entity_Id) return Node_Id;
2982 -- Search for Aspect_No_Parts on a given type. When
2983 -- the aspect is not explicity specified Empty is returned.
2985 function Get_Aspect_No_Parts_Value
2986 (Typ : Entity_Id) return Entity_Id;
2987 -- Obtain the value for the Aspect_No_Parts on a given
2988 -- type. When the aspect is not explicitly specified Empty is
2989 -- returned.
2991 function Has_Aspect_No_Parts
2992 (Typ : Entity_Id) return Boolean;
2993 -- Predicate function which identifies whether No_Parts
2994 -- is explicitly specified on a given type.
2996 -------------------------------------
2997 -- Find_Aspect_No_Parts --
2998 -------------------------------------
3000 function Find_Aspect_No_Parts
3001 (Typ : Entity_Id) return Node_Id
3003 Partial_View : constant Entity_Id :=
3004 Incomplete_Or_Partial_View (Typ);
3006 Aspect_Spec : Entity_Id :=
3007 Find_Aspect (Typ, Aspect_No_Parts);
3008 Curr_Aspect_Spec : Entity_Id;
3009 begin
3011 -- Examine Typ's associated node, when present, since aspect
3012 -- specifications do not get transferred when nodes get rewritten.
3014 -- For example, this can happen in the expansion of array types
3016 if No (Aspect_Spec)
3017 and then Present (Associated_Node_For_Itype (Typ))
3018 and then Nkind (Associated_Node_For_Itype (Typ))
3019 = N_Full_Type_Declaration
3020 then
3021 Aspect_Spec :=
3022 Find_Aspect
3023 (Id => Defining_Identifier
3024 (Associated_Node_For_Itype (Typ)),
3025 A => Aspect_No_Parts);
3026 end if;
3028 -- Examine aspects specifications on private type declarations
3030 -- Should Find_Aspect be improved to handle this case ???
3032 if No (Aspect_Spec)
3033 and then Present (Partial_View)
3034 and then Present
3035 (Aspect_Specifications
3036 (Declaration_Node
3037 (Partial_View)))
3038 then
3039 Curr_Aspect_Spec :=
3040 First
3041 (Aspect_Specifications
3042 (Declaration_Node
3043 (Partial_View)));
3045 -- Search through aspects present on the private type
3047 while Present (Curr_Aspect_Spec) loop
3048 if Get_Aspect_Id (Curr_Aspect_Spec)
3049 = Aspect_No_Parts
3050 then
3051 Aspect_Spec := Curr_Aspect_Spec;
3052 exit;
3053 end if;
3055 Next (Curr_Aspect_Spec);
3056 end loop;
3058 end if;
3060 -- When errors are posted on the aspect return Empty
3062 if Error_Posted (Aspect_Spec) then
3063 return Empty;
3064 end if;
3066 return Aspect_Spec;
3067 end Find_Aspect_No_Parts;
3069 ------------------------------------------
3070 -- Get_Aspect_No_Parts_Value --
3071 ------------------------------------------
3073 function Get_Aspect_No_Parts_Value
3074 (Typ : Entity_Id) return Entity_Id
3076 Aspect_Spec : constant Entity_Id :=
3077 Find_Aspect_No_Parts (Typ);
3078 begin
3080 -- Return the value of the aspect when present
3082 if Present (Aspect_Spec) then
3084 -- No expression is the same as True
3086 if No (Expression (Aspect_Spec)) then
3087 return Standard_True;
3088 end if;
3090 -- Assume its expression has already been constant folded into
3091 -- a Boolean value and return its value.
3093 return Entity (Expression (Aspect_Spec));
3094 end if;
3096 -- Otherwise, the aspect is not specified - so return Empty
3098 return Empty;
3099 end Get_Aspect_No_Parts_Value;
3101 ------------------------------------
3102 -- Has_Aspect_No_Parts --
3103 ------------------------------------
3105 function Has_Aspect_No_Parts
3106 (Typ : Entity_Id) return Boolean
3107 is (Present (Find_Aspect_No_Parts (Typ)));
3109 -- Generic instances
3111 -------------------------------------------
3112 -- Get_Generic_Formal_Types_In_Hierarchy --
3113 -------------------------------------------
3115 function Get_Generic_Formal_Types_In_Hierarchy
3116 is new Collect_Types_In_Hierarchy (Predicate => Is_Generic_Formal);
3117 -- Return a list of all types within a given type's hierarchy which
3118 -- are generic formals.
3120 ----------------------------------------
3121 -- Get_Types_With_Aspect_In_Hierarchy --
3122 ----------------------------------------
3124 function Get_Types_With_Aspect_In_Hierarchy
3125 is new Collect_Types_In_Hierarchy
3126 (Predicate => Has_Aspect_No_Parts);
3127 -- Returns a list of all types within a given type's hierarchy which
3128 -- have the Aspect_No_Parts specified.
3130 -- Local declarations
3132 Aspect_Value : Entity_Id;
3133 Curr_Value : Entity_Id;
3134 Curr_Typ_Elmt : Elmt_Id;
3135 Curr_Body_Elmt : Elmt_Id;
3136 Curr_Formal_Elmt : Elmt_Id;
3137 Gen_Bodies : Elist_Id;
3138 Gen_Formals : Elist_Id;
3139 Scop : Entity_Id;
3140 Types_With_Aspect : Elist_Id;
3142 -- Start of processing for Check_No_Parts_Violations
3144 begin
3145 -- Nothing to check if the type is elementary or artificial
3147 if Is_Elementary_Type (Typ) or else not Comes_From_Source (Typ) then
3148 return;
3149 end if;
3151 Types_With_Aspect := Get_Types_With_Aspect_In_Hierarchy (Typ);
3153 -- Nothing to check if there are no types with No_Parts specified
3155 if Is_Empty_Elmt_List (Types_With_Aspect) then
3156 return;
3157 end if;
3159 -- Set name for all errors below
3161 Error_Msg_Name_1 := Aspect_Names (Aspect_No_Parts);
3163 -- Obtain the aspect value for No_Parts for comparison
3165 Aspect_Value :=
3166 Get_Aspect_No_Parts_Value
3167 (Node (First_Elmt (Types_With_Aspect)));
3169 -- When the value is True and there are controlled/task parts or the
3170 -- type itself is controlled/task, trigger the appropriate error.
3172 if Aspect_Value = Standard_True then
3173 if Aspect_No_Parts = Aspect_No_Controlled_Parts then
3174 if Is_Controlled (Typ) or else Has_Controlled_Component (Typ)
3175 then
3176 Error_Msg_N
3177 ("aspect % applied to controlled type &", Typ);
3178 end if;
3180 elsif Aspect_No_Parts = Aspect_No_Task_Parts then
3181 if Has_Task (Typ) then
3182 Error_Msg_N
3183 ("aspect % applied to task type &", Typ);
3184 Error_Msg_N
3185 ("\replace task components with access-to-task-type "
3186 & "components??", Typ);
3187 end if;
3189 else
3190 raise Program_Error;
3191 end if;
3192 end if;
3194 -- Move through Types_With_Aspect - checking that the value specified
3195 -- for their corresponding Aspect_No_Parts do not override each
3196 -- other.
3198 Curr_Typ_Elmt := First_Elmt (Types_With_Aspect);
3199 while Present (Curr_Typ_Elmt) loop
3200 Curr_Value :=
3201 Get_Aspect_No_Parts_Value (Node (Curr_Typ_Elmt));
3203 -- Compare the aspect value against the current type
3205 if Curr_Value /= Aspect_Value then
3206 Error_Msg_NE
3207 ("cannot override aspect % of "
3208 & "ancestor type &", Typ, Node (Curr_Typ_Elmt));
3209 return;
3210 end if;
3212 Next_Elmt (Curr_Typ_Elmt);
3213 end loop;
3215 -- Issue an error if the aspect applies to a type declared inside a
3216 -- generic body and if said type derives from or has a component
3217 -- of ageneric formal type - since those are considered to have
3218 -- controlled/task parts and have Aspect_No_Parts specified as
3219 -- False by default (RM H.4.1(4/5) is about the language-defined
3220 -- No_Controlled_Parts aspect, and we are using the same rules for
3221 -- No_Task_Parts).
3223 -- We do not check tagged types since deriving from a formal type
3224 -- within an enclosing generic unit is already illegal
3225 -- (RM 3.9.1 (4/2)).
3227 if Aspect_Value = Standard_True
3228 and then In_Generic_Body (Typ)
3229 and then not Is_Tagged_Type (Typ)
3230 then
3231 Gen_Bodies := New_Elmt_List;
3232 Gen_Formals :=
3233 Get_Generic_Formal_Types_In_Hierarchy
3234 (Typ => Typ,
3235 Examine_Components => True);
3237 -- Climb scopes collecting generic bodies
3239 Scop := Scope (Typ);
3240 while Present (Scop) and then Scop /= Standard_Standard loop
3242 -- Generic package body
3244 if Ekind (Scop) = E_Generic_Package
3245 and then In_Package_Body (Scop)
3246 then
3247 Append_Elmt (Scop, Gen_Bodies);
3249 -- Generic subprogram body
3251 elsif Is_Generic_Subprogram (Scop) then
3252 Append_Elmt (Scop, Gen_Bodies);
3253 end if;
3255 Scop := Scope (Scop);
3256 end loop;
3258 -- Warn about the improper use of Aspect_No_Parts on a type
3259 -- declaration deriving from or that has a component of a generic
3260 -- formal type within the formal type's corresponding generic
3261 -- body by moving through all formal types in Typ's hierarchy and
3262 -- checking if they are formals in any of the enclosing generic
3263 -- bodies.
3265 -- However, a special exception gets made for formal types which
3266 -- derive from a type which has Aspect_No_Parts True.
3268 -- For example:
3270 -- generic
3271 -- type Form is private;
3272 -- package G is
3273 -- type Type_A is new Form with No_Controlled_Parts; -- OK
3274 -- end;
3276 -- package body G is
3277 -- type Type_B is new Form with No_Controlled_Parts; -- ERROR
3278 -- end;
3280 -- generic
3281 -- type Form is private;
3282 -- package G is
3283 -- type Type_A is record C : Form; end record
3284 -- with No_Controlled_Parts; -- OK
3285 -- end;
3287 -- package body G is
3288 -- type Type_B is record C : Form; end record
3289 -- with No_Controlled_Parts; -- ERROR
3290 -- end;
3292 -- type Root is tagged null record with No_Controlled_Parts;
3294 -- generic
3295 -- type Form is new Root with private;
3296 -- package G is
3297 -- type Type_A is record C : Form; end record
3298 -- with No_Controlled_Parts; -- OK
3299 -- end;
3301 -- package body G is
3302 -- type Type_B is record C : Form; end record
3303 -- with No_Controlled_Parts; -- OK
3304 -- end;
3306 Curr_Formal_Elmt := First_Elmt (Gen_Formals);
3307 while Present (Curr_Formal_Elmt) loop
3309 Curr_Body_Elmt := First_Elmt (Gen_Bodies);
3310 while Present (Curr_Body_Elmt) loop
3312 -- Obtain types in the formal type's hierarchy which have
3313 -- the aspect specified.
3315 Types_With_Aspect :=
3316 Get_Types_With_Aspect_In_Hierarchy
3317 (Node (Curr_Formal_Elmt));
3319 -- We found a type declaration in a generic body where both
3320 -- Aspect_No_Parts is true and one of its ancestors is a
3321 -- generic formal type.
3323 if Scope (Node (Curr_Formal_Elmt)) =
3324 Node (Curr_Body_Elmt)
3326 -- Check that no ancestors of the formal type have
3327 -- Aspect_No_Parts True before issuing the error.
3329 and then (Is_Empty_Elmt_List (Types_With_Aspect)
3330 or else
3331 Get_Aspect_No_Parts_Value
3332 (Node (First_Elmt (Types_With_Aspect)))
3333 = Standard_False)
3334 then
3335 Error_Msg_Node_1 := Typ;
3336 Error_Msg_Node_2 := Node (Curr_Formal_Elmt);
3337 Error_Msg
3338 ("aspect % cannot be applied to "
3339 & "type & which has an ancestor or component of "
3340 & "formal type & within the formal type's "
3341 & "corresponding generic body", Sloc (Typ));
3342 end if;
3344 Next_Elmt (Curr_Body_Elmt);
3345 end loop;
3347 Next_Elmt (Curr_Formal_Elmt);
3348 end loop;
3349 end if;
3350 end Check_No_Parts_Violations;
3352 ---------------------------------
3353 -- Check_Suspicious_Convention --
3354 ---------------------------------
3356 procedure Check_Suspicious_Convention (Rec_Type : Entity_Id) is
3357 begin
3358 if Has_Discriminants (Rec_Type)
3359 and then Is_Base_Type (Rec_Type)
3360 and then not Is_Unchecked_Union (Rec_Type)
3361 and then (Convention (Rec_Type) = Convention_C
3362 or else
3363 Convention (Rec_Type) = Convention_CPP)
3364 and then Comes_From_Source (Rec_Type)
3365 and then not In_Instance
3366 and then not Has_Warnings_Off (Rec_Type)
3367 then
3368 declare
3369 Cprag : constant Node_Id :=
3370 Get_Rep_Pragma (Rec_Type, Name_Convention);
3371 A2 : Node_Id;
3373 begin
3374 if Present (Cprag) then
3375 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
3377 if Convention (Rec_Type) = Convention_C then
3378 Error_Msg_N
3379 ("?x?discriminated record has no direct equivalent in "
3380 & "C", A2);
3381 else
3382 Error_Msg_N
3383 ("?x?discriminated record has no direct equivalent in "
3384 & "C++", A2);
3385 end if;
3387 Error_Msg_NE
3388 ("\?x?use of convention for type& is dubious",
3389 A2, Rec_Type);
3390 end if;
3391 end;
3392 end if;
3393 end Check_Suspicious_Convention;
3395 ------------------------------
3396 -- Check_Suspicious_Modulus --
3397 ------------------------------
3399 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
3400 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
3402 begin
3403 if not Warn_On_Suspicious_Modulus_Value then
3404 return;
3405 end if;
3407 if Nkind (Decl) = N_Full_Type_Declaration then
3408 declare
3409 Tdef : constant Node_Id := Type_Definition (Decl);
3411 begin
3412 if Nkind (Tdef) = N_Modular_Type_Definition then
3413 declare
3414 Modulus : constant Node_Id :=
3415 Original_Node (Expression (Tdef));
3417 begin
3418 if Nkind (Modulus) = N_Integer_Literal then
3419 declare
3420 Modv : constant Uint := Intval (Modulus);
3421 Sizv : constant Uint := RM_Size (Utype);
3423 begin
3424 -- First case, modulus and size are the same. This
3425 -- happens if you have something like mod 32, with
3426 -- an explicit size of 32, this is for sure a case
3427 -- where the warning is given, since it is seems
3428 -- very unlikely that someone would want e.g. a
3429 -- five bit type stored in 32 bits. It is much
3430 -- more likely they wanted a 32-bit type.
3432 if Modv = Sizv then
3433 null;
3435 -- Second case, the modulus is 32 or 64 and no
3436 -- size clause is present. This is a less clear
3437 -- case for giving the warning, but in the case
3438 -- of 32/64 (5-bit or 6-bit types) these seem rare
3439 -- enough that it is a likely error (and in any
3440 -- case using 2**5 or 2**6 in these cases seems
3441 -- clearer. We don't include 8 or 16 here, simply
3442 -- because in practice 3-bit and 4-bit types are
3443 -- more common and too many false positives if
3444 -- we warn in these cases.
3446 elsif not Has_Size_Clause (Utype)
3447 and then (Modv = Uint_32 or else Modv = Uint_64)
3448 then
3449 null;
3451 -- No warning needed
3453 else
3454 return;
3455 end if;
3457 -- If we fall through, give warning
3459 Error_Msg_Uint_1 := Modv;
3460 Error_Msg_N
3461 ("?.m?2 '*'*^' may have been intended here",
3462 Modulus);
3463 end;
3464 end if;
3465 end;
3466 end if;
3467 end;
3468 end if;
3469 end Check_Suspicious_Modulus;
3471 -----------------------
3472 -- Freeze_Array_Type --
3473 -----------------------
3475 procedure Freeze_Array_Type (Arr : Entity_Id) is
3476 FS : constant Entity_Id := First_Subtype (Arr);
3477 Ctyp : constant Entity_Id := Component_Type (Arr);
3478 Clause : Entity_Id;
3480 Non_Standard_Enum : Boolean := False;
3481 -- Set true if any of the index types is an enumeration type with a
3482 -- non-standard representation.
3484 begin
3485 Freeze_And_Append (Ctyp, N, Result);
3487 Indx := First_Index (Arr);
3488 while Present (Indx) loop
3489 Freeze_And_Append (Etype (Indx), N, Result);
3491 if Is_Enumeration_Type (Etype (Indx))
3492 and then Has_Non_Standard_Rep (Etype (Indx))
3493 then
3494 Non_Standard_Enum := True;
3495 end if;
3497 Next_Index (Indx);
3498 end loop;
3500 -- Processing that is done only for base types
3502 if Ekind (Arr) = E_Array_Type then
3504 -- Deal with default setting of reverse storage order
3506 Set_SSO_From_Default (Arr);
3508 -- Propagate flags for component type
3510 if Is_Controlled (Ctyp)
3511 or else Has_Controlled_Component (Ctyp)
3512 then
3513 Set_Has_Controlled_Component (Arr);
3514 end if;
3516 if Has_Unchecked_Union (Ctyp) then
3517 Set_Has_Unchecked_Union (Arr);
3518 end if;
3520 -- The array type requires its own invariant procedure in order to
3521 -- verify the component invariant over all elements. In GNATprove
3522 -- mode, the component invariants are checked by other means. They
3523 -- should not be added to the array type invariant procedure, so
3524 -- that the procedure can be used to check the array type
3525 -- invariants if any.
3527 if Has_Invariants (Ctyp)
3528 and then not GNATprove_Mode
3529 then
3530 Set_Has_Own_Invariants (Arr);
3531 end if;
3533 -- Warn for pragma Pack overriding foreign convention
3535 if Has_Foreign_Convention (Ctyp)
3536 and then Has_Pragma_Pack (Arr)
3537 then
3538 declare
3539 CN : constant Name_Id :=
3540 Get_Convention_Name (Convention (Ctyp));
3541 PP : constant Node_Id :=
3542 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
3543 begin
3544 if Present (PP) then
3545 Error_Msg_Name_1 := CN;
3546 Error_Msg_Sloc := Sloc (Arr);
3547 Error_Msg_N
3548 ("pragma Pack affects convention % components #??", PP);
3549 Error_Msg_Name_1 := CN;
3550 Error_Msg_N
3551 ("\array components may not have % compatible "
3552 & "representation??", PP);
3553 end if;
3554 end;
3555 end if;
3557 -- Check for Aliased or Atomic_Components or Full Access with
3558 -- unsuitable packing or explicit component size clause given.
3560 if (Has_Aliased_Components (Arr)
3561 or else Has_Atomic_Components (Arr)
3562 or else Is_Full_Access (Ctyp))
3563 and then
3564 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
3565 then
3566 Alias_Atomic_Check : declare
3568 procedure Complain_CS (T : String);
3569 -- Outputs error messages for incorrect CS clause or pragma
3570 -- Pack for aliased or full access components (T is either
3571 -- "aliased" or "atomic" or "volatile full access");
3573 -----------------
3574 -- Complain_CS --
3575 -----------------
3577 procedure Complain_CS (T : String) is
3578 begin
3579 if Has_Component_Size_Clause (Arr) then
3580 Clause :=
3581 Get_Attribute_Definition_Clause
3582 (FS, Attribute_Component_Size);
3584 Error_Msg_N
3585 ("incorrect component size for "
3586 & T & " components", Clause);
3587 Error_Msg_Uint_1 := Esize (Ctyp);
3588 Error_Msg_N
3589 ("\only allowed value is^", Clause);
3591 else
3592 Error_Msg_N
3593 ("?cannot pack " & T & " components (RM 13.2(7))",
3594 Get_Rep_Pragma (FS, Name_Pack));
3595 Set_Is_Packed (Arr, False);
3596 end if;
3597 end Complain_CS;
3599 -- Start of processing for Alias_Atomic_Check
3601 begin
3602 -- If object size of component type isn't known, we cannot
3603 -- be sure so we defer to the back end.
3605 if not Known_Static_Esize (Ctyp) then
3606 null;
3608 -- Case where component size has no effect. First check for
3609 -- object size of component type multiple of the storage
3610 -- unit size.
3612 elsif Esize (Ctyp) mod System_Storage_Unit = 0
3614 -- OK in both packing case and component size case if RM
3615 -- size is known and static and same as the object size.
3617 and then
3618 ((Known_Static_RM_Size (Ctyp)
3619 and then Esize (Ctyp) = RM_Size (Ctyp))
3621 -- Or if we have an explicit component size clause and
3622 -- the component size and object size are equal.
3624 or else
3625 (Has_Component_Size_Clause (Arr)
3626 and then Component_Size (Arr) = Esize (Ctyp)))
3627 then
3628 null;
3630 elsif Has_Aliased_Components (Arr) then
3631 Complain_CS ("aliased");
3633 elsif Has_Atomic_Components (Arr)
3634 or else Is_Atomic (Ctyp)
3635 then
3636 Complain_CS ("atomic");
3638 elsif Is_Volatile_Full_Access (Ctyp) then
3639 Complain_CS ("volatile full access");
3640 end if;
3641 end Alias_Atomic_Check;
3642 end if;
3644 -- Check for Independent_Components/Independent with unsuitable
3645 -- packing or explicit component size clause given.
3647 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
3648 and then
3649 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
3650 then
3651 begin
3652 -- If object size of component type isn't known, we cannot
3653 -- be sure so we defer to the back end.
3655 if not Known_Static_Esize (Ctyp) then
3656 null;
3658 -- Case where component size has no effect. First check for
3659 -- object size of component type multiple of the storage
3660 -- unit size.
3662 elsif Esize (Ctyp) mod System_Storage_Unit = 0
3664 -- OK in both packing case and component size case if RM
3665 -- size is known and multiple of the storage unit size.
3667 and then
3668 ((Known_Static_RM_Size (Ctyp)
3669 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
3671 -- Or if we have an explicit component size clause and
3672 -- the component size is larger than the object size.
3674 or else
3675 (Has_Component_Size_Clause (Arr)
3676 and then Component_Size (Arr) >= Esize (Ctyp)))
3677 then
3678 null;
3680 else
3681 if Has_Component_Size_Clause (Arr) then
3682 Clause :=
3683 Get_Attribute_Definition_Clause
3684 (FS, Attribute_Component_Size);
3686 Error_Msg_N
3687 ("incorrect component size for "
3688 & "independent components", Clause);
3689 Error_Msg_Uint_1 := Esize (Ctyp);
3690 Error_Msg_N
3691 ("\minimum allowed is^", Clause);
3693 else
3694 Error_Msg_N
3695 ("?cannot pack independent components (RM 13.2(7))",
3696 Get_Rep_Pragma (FS, Name_Pack));
3697 Set_Is_Packed (Arr, False);
3698 end if;
3699 end if;
3700 end;
3701 end if;
3703 -- If packing was requested or if the component size was
3704 -- set explicitly, then see if bit packing is required. This
3705 -- processing is only done for base types, since all of the
3706 -- representation aspects involved are type-related.
3708 -- This is not just an optimization, if we start processing the
3709 -- subtypes, they interfere with the settings on the base type
3710 -- (this is because Is_Packed has a slightly different meaning
3711 -- before and after freezing).
3713 declare
3714 Csiz : Uint;
3715 Esiz : Uint;
3717 begin
3718 if Is_Packed (Arr)
3719 and then Known_Static_RM_Size (Ctyp)
3720 and then not Has_Component_Size_Clause (Arr)
3721 then
3722 Csiz := UI_Max (RM_Size (Ctyp), 1);
3724 elsif Known_Component_Size (Arr) then
3725 Csiz := Component_Size (Arr);
3727 elsif not Known_Static_Esize (Ctyp) then
3728 Csiz := Uint_0;
3730 else
3731 Esiz := Esize (Ctyp);
3733 -- We can set the component size if it is less than 16,
3734 -- rounding it up to the next storage unit size.
3736 if Esiz <= 8 then
3737 Csiz := Uint_8;
3738 elsif Esiz <= 16 then
3739 Csiz := Uint_16;
3740 else
3741 Csiz := Uint_0;
3742 end if;
3744 -- Set component size up to match alignment if it would
3745 -- otherwise be less than the alignment. This deals with
3746 -- cases of types whose alignment exceeds their size (the
3747 -- padded type cases).
3749 if Csiz /= 0 and then Known_Alignment (Ctyp) then
3750 declare
3751 A : constant Uint := Alignment_In_Bits (Ctyp);
3752 begin
3753 if Csiz < A then
3754 Csiz := A;
3755 end if;
3756 end;
3757 end if;
3758 end if;
3760 -- Case of component size that may result in bit packing
3762 if 1 <= Csiz and then Csiz <= System_Max_Integer_Size then
3763 declare
3764 Ent : constant Entity_Id :=
3765 First_Subtype (Arr);
3766 Pack_Pragma : constant Node_Id :=
3767 Get_Rep_Pragma (Ent, Name_Pack);
3768 Comp_Size_C : constant Node_Id :=
3769 Get_Attribute_Definition_Clause
3770 (Ent, Attribute_Component_Size);
3772 begin
3773 -- Warn if we have pack and component size so that the
3774 -- pack is ignored.
3776 -- Note: here we must check for the presence of a
3777 -- component size before checking for a Pack pragma to
3778 -- deal with the case where the array type is a derived
3779 -- type whose parent is currently private.
3781 if Present (Comp_Size_C)
3782 and then Has_Pragma_Pack (Ent)
3783 and then Warn_On_Redundant_Constructs
3784 then
3785 Error_Msg_Sloc := Sloc (Comp_Size_C);
3786 Error_Msg_NE
3787 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
3788 Error_Msg_N
3789 ("\?r?explicit component size given#!", Pack_Pragma);
3790 Set_Is_Packed (Base_Type (Ent), False);
3791 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
3792 end if;
3794 -- Set component size if not already set by a component
3795 -- size clause.
3797 if No (Comp_Size_C) then
3798 Set_Component_Size (Arr, Csiz);
3799 end if;
3801 -- Check for base type of 8, 16, 32 bits, where an
3802 -- unsigned subtype has a length one less than the
3803 -- base type (e.g. Natural subtype of Integer).
3805 -- In such cases, if a component size was not set
3806 -- explicitly, then generate a warning.
3808 if Has_Pragma_Pack (Arr)
3809 and then No (Comp_Size_C)
3810 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
3811 and then Known_Esize (Base_Type (Ctyp))
3812 and then Esize (Base_Type (Ctyp)) = Csiz + 1
3813 then
3814 Error_Msg_Uint_1 := Csiz;
3816 if Present (Pack_Pragma) then
3817 Error_Msg_N
3818 ("??pragma Pack causes component size to be ^!",
3819 Pack_Pragma);
3820 Error_Msg_N
3821 ("\??use Component_Size to set desired value!",
3822 Pack_Pragma);
3823 end if;
3824 end if;
3826 -- Bit packing is never needed for 8, 16, 32, 64 or 128
3828 if Addressable (Csiz) then
3830 -- If the Esize of the component is known and equal to
3831 -- the component size then even packing is not needed.
3833 if Known_Static_Esize (Ctyp)
3834 and then Esize (Ctyp) = Csiz
3835 then
3836 -- Here the array was requested to be packed, but
3837 -- the packing request had no effect whatsoever,
3838 -- so flag Is_Packed is reset.
3840 -- Note: semantically this means that we lose track
3841 -- of the fact that a derived type inherited pragma
3842 -- Pack that was non-effective, but that is fine.
3844 -- We regard a Pack pragma as a request to set a
3845 -- representation characteristic, and this request
3846 -- may be ignored.
3848 Set_Is_Packed (Base_Type (Arr), False);
3849 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
3850 else
3851 Set_Is_Packed (Base_Type (Arr), True);
3852 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3853 end if;
3855 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3857 -- Bit packing is not needed for multiples of the storage
3858 -- unit if the type is composite because the back end can
3859 -- byte pack composite types efficiently. That's not true
3860 -- for discrete types because every read would generate a
3861 -- lot of instructions, so we keep using the manipulation
3862 -- routines of the runtime for them.
3864 elsif Csiz mod System_Storage_Unit = 0
3865 and then Is_Composite_Type (Ctyp)
3866 then
3867 Set_Is_Packed (Base_Type (Arr), True);
3868 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3869 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
3871 -- In all other cases, bit packing is needed
3873 else
3874 Set_Is_Packed (Base_Type (Arr), True);
3875 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
3876 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
3877 end if;
3878 end;
3879 end if;
3880 end;
3882 -- Warn for case of atomic type
3884 Clause := Get_Rep_Pragma (FS, Name_Atomic);
3886 if Present (Clause)
3887 and then not Addressable (Component_Size (FS))
3888 then
3889 Error_Msg_NE
3890 ("non-atomic components of type& may not be "
3891 & "accessible by separate tasks??", Clause, Arr);
3893 if Has_Component_Size_Clause (Arr) then
3894 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
3895 (FS, Attribute_Component_Size));
3896 Error_Msg_N ("\because of component size clause#??", Clause);
3898 elsif Has_Pragma_Pack (Arr) then
3899 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
3900 Error_Msg_N ("\because of pragma Pack#??", Clause);
3901 end if;
3902 end if;
3904 -- Check for scalar storage order
3906 declare
3907 Dummy : Boolean;
3908 begin
3909 Check_Component_Storage_Order
3910 (Encl_Type => Arr,
3911 Comp => Empty,
3912 ADC => Get_Attribute_Definition_Clause
3913 (First_Subtype (Arr),
3914 Attribute_Scalar_Storage_Order),
3915 Comp_ADC_Present => Dummy);
3916 end;
3918 -- Processing that is done only for subtypes
3920 else
3921 -- Acquire alignment from base type. Known_Alignment of the base
3922 -- type is False for Wide_String, for example.
3924 if not Known_Alignment (Arr)
3925 and then Known_Alignment (Base_Type (Arr))
3926 then
3927 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
3928 Adjust_Esize_Alignment (Arr);
3929 end if;
3930 end if;
3932 -- Specific checks for bit-packed arrays
3934 if Is_Bit_Packed_Array (Arr) then
3936 -- Check number of elements for bit-packed arrays that come from
3937 -- source and have compile time known ranges. The bit-packed
3938 -- arrays circuitry does not support arrays with more than
3939 -- Integer'Last + 1 elements, and when this restriction is
3940 -- violated, causes incorrect data access.
3942 -- For the case where this is not compile time known, a run-time
3943 -- check should be generated???
3945 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
3946 declare
3947 Elmts : Uint;
3948 Index : Node_Id;
3949 Ilen : Node_Id;
3950 Ityp : Entity_Id;
3952 begin
3953 Elmts := Uint_1;
3954 Index := First_Index (Arr);
3955 while Present (Index) loop
3956 Ityp := Etype (Index);
3958 -- Never generate an error if any index is of a generic
3959 -- type. We will check this in instances.
3961 if Is_Generic_Type (Ityp) then
3962 Elmts := Uint_0;
3963 exit;
3964 end if;
3966 Ilen :=
3967 Make_Attribute_Reference (Loc,
3968 Prefix => New_Occurrence_Of (Ityp, Loc),
3969 Attribute_Name => Name_Range_Length);
3970 Analyze_And_Resolve (Ilen);
3972 -- No attempt is made to check number of elements if not
3973 -- compile time known.
3975 if Nkind (Ilen) /= N_Integer_Literal then
3976 Elmts := Uint_0;
3977 exit;
3978 end if;
3980 Elmts := Elmts * Intval (Ilen);
3981 Next_Index (Index);
3982 end loop;
3984 if Elmts > Intval (High_Bound
3985 (Scalar_Range (Standard_Integer))) + 1
3986 then
3987 Error_Msg_N
3988 ("bit packed array type may not have "
3989 & "more than Integer''Last+1 elements", Arr);
3990 end if;
3991 end;
3992 end if;
3994 -- Check size
3996 if Known_RM_Size (Arr) then
3997 declare
3998 SizC : constant Node_Id := Size_Clause (Arr);
3999 Discard : Boolean;
4001 begin
4002 -- It is not clear if it is possible to have no size clause
4003 -- at this stage, but it is not worth worrying about. Post
4004 -- error on the entity name in the size clause if present,
4005 -- else on the type entity itself.
4007 if Present (SizC) then
4008 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
4009 else
4010 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
4011 end if;
4012 end;
4013 end if;
4014 end if;
4016 -- If any of the index types was an enumeration type with a non-
4017 -- standard rep clause, then we indicate that the array type is
4018 -- always packed (even if it is not bit-packed).
4020 if Non_Standard_Enum then
4021 Set_Has_Non_Standard_Rep (Base_Type (Arr));
4022 Set_Is_Packed (Base_Type (Arr));
4023 end if;
4025 Set_Component_Alignment_If_Not_Set (Arr);
4027 -- If the array is packed and bit-packed or packed to eliminate holes
4028 -- in the non-contiguous enumeration index types, we must create the
4029 -- packed array type to be used to actually implement the type. This
4030 -- is only needed for real array types (not for string literal types,
4031 -- since they are present only for the front end).
4033 if Is_Packed (Arr)
4034 and then (Is_Bit_Packed_Array (Arr) or else Non_Standard_Enum)
4035 and then Ekind (Arr) /= E_String_Literal_Subtype
4036 then
4037 Create_Packed_Array_Impl_Type (Arr);
4038 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
4040 -- Make sure that we have the necessary routines to implement the
4041 -- packing, and complain now if not. Note that we only test this
4042 -- for constrained array types.
4044 if Is_Constrained (Arr)
4045 and then Is_Bit_Packed_Array (Arr)
4046 and then Present (Packed_Array_Impl_Type (Arr))
4047 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
4048 then
4049 declare
4050 CS : constant Uint := Component_Size (Arr);
4051 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
4053 begin
4054 if RE /= RE_Null
4055 and then not RTE_Available (RE)
4056 then
4057 Error_Msg_CRT
4058 ("packing of " & UI_Image (CS) & "-bit components",
4059 First_Subtype (Etype (Arr)));
4061 -- Cancel the packing
4063 Set_Is_Packed (Base_Type (Arr), False);
4064 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
4065 Set_Packed_Array_Impl_Type (Arr, Empty);
4066 goto Skip_Packed;
4067 end if;
4068 end;
4069 end if;
4071 -- Size information of packed array type is copied to the array
4072 -- type, since this is really the representation. But do not
4073 -- override explicit existing size values. If the ancestor subtype
4074 -- is constrained the Packed_Array_Impl_Type will be inherited
4075 -- from it, but the size may have been provided already, and
4076 -- must not be overridden either.
4078 if not Has_Size_Clause (Arr)
4079 and then
4080 (No (Ancestor_Subtype (Arr))
4081 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
4082 then
4083 Copy_Esize (To => Arr, From => Packed_Array_Impl_Type (Arr));
4084 Copy_RM_Size (To => Arr, From => Packed_Array_Impl_Type (Arr));
4085 end if;
4087 if not Has_Alignment_Clause (Arr) then
4088 Copy_Alignment
4089 (To => Arr, From => Packed_Array_Impl_Type (Arr));
4090 end if;
4091 end if;
4093 <<Skip_Packed>>
4095 -- A Ghost type cannot have a component of protected or task type
4096 -- (SPARK RM 6.9(19)).
4098 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
4099 Error_Msg_N
4100 ("ghost array type & cannot have concurrent component type",
4101 Arr);
4102 end if;
4103 end Freeze_Array_Type;
4105 -------------------------------
4106 -- Freeze_Object_Declaration --
4107 -------------------------------
4109 procedure Freeze_Object_Declaration (E : Entity_Id) is
4110 procedure Check_Large_Modular_Array (Typ : Entity_Id);
4111 -- Check that the size of array type Typ can be computed without
4112 -- overflow, and generates a Storage_Error otherwise. This is only
4113 -- relevant for array types whose index is a modular type with
4114 -- Standard_Long_Long_Integer_Size bits: wrap-around arithmetic
4115 -- might yield a meaningless value for the length of the array,
4116 -- or its corresponding attribute.
4118 procedure Check_Pragma_Thread_Local_Storage (Var_Id : Entity_Id);
4119 -- Ensure that the initialization state of variable Var_Id subject
4120 -- to pragma Thread_Local_Storage agrees with the semantics of the
4121 -- pragma.
4123 function Has_Default_Initialization
4124 (Obj_Id : Entity_Id) return Boolean;
4125 -- Determine whether object Obj_Id default initialized
4127 -------------------------------
4128 -- Check_Large_Modular_Array --
4129 -------------------------------
4131 procedure Check_Large_Modular_Array (Typ : Entity_Id) is
4132 Obj_Loc : constant Source_Ptr := Sloc (E);
4133 Idx_Typ : Entity_Id;
4135 begin
4136 -- Nothing to do when expansion is disabled because this routine
4137 -- generates a runtime check.
4139 if not Expander_Active then
4140 return;
4142 -- Nothing to do for String literal subtypes because their index
4143 -- cannot be a modular type.
4145 elsif Ekind (Typ) = E_String_Literal_Subtype then
4146 return;
4148 -- Nothing to do for an imported object because the object will
4149 -- be created on the exporting side.
4151 elsif Is_Imported (E) then
4152 return;
4154 -- Nothing to do for unconstrained array types. This case arises
4155 -- when the object declaration is illegal.
4157 elsif not Is_Constrained (Typ) then
4158 return;
4159 end if;
4161 Idx_Typ := Etype (First_Index (Typ));
4163 -- To prevent arithmetic overflow with large values, we raise
4164 -- Storage_Error under the following guard:
4166 -- (Arr'Last / 2 - Arr'First / 2) > (2 ** 30)
4168 -- This takes care of the boundary case, but it is preferable to
4169 -- use a smaller limit, because even on 64-bit architectures an
4170 -- array of more than 2 ** 30 bytes is likely to raise
4171 -- Storage_Error.
4173 if Is_Modular_Integer_Type (Idx_Typ)
4174 and then RM_Size (Idx_Typ) = Standard_Long_Long_Integer_Size
4175 then
4176 -- Ensure that the type of the object is elaborated before
4177 -- the check itself is emitted to avoid elaboration issues
4178 -- in the code generator at the library level.
4180 if Is_Itype (Etype (E))
4181 and then In_Open_Scopes (Scope (Etype (E)))
4182 then
4183 declare
4184 Ref_Node : constant Node_Id :=
4185 Make_Itype_Reference (Obj_Loc);
4186 begin
4187 Set_Itype (Ref_Node, Etype (E));
4188 Insert_Action (Declaration_Node (E), Ref_Node);
4189 end;
4190 end if;
4192 Insert_Action (Declaration_Node (E),
4193 Make_Raise_Storage_Error (Obj_Loc,
4194 Condition =>
4195 Make_Op_Ge (Obj_Loc,
4196 Left_Opnd =>
4197 Make_Op_Subtract (Obj_Loc,
4198 Left_Opnd =>
4199 Make_Op_Divide (Obj_Loc,
4200 Left_Opnd =>
4201 Make_Attribute_Reference (Obj_Loc,
4202 Prefix =>
4203 New_Occurrence_Of (Typ, Obj_Loc),
4204 Attribute_Name => Name_Last),
4205 Right_Opnd =>
4206 Make_Integer_Literal (Obj_Loc, Uint_2)),
4207 Right_Opnd =>
4208 Make_Op_Divide (Obj_Loc,
4209 Left_Opnd =>
4210 Make_Attribute_Reference (Obj_Loc,
4211 Prefix =>
4212 New_Occurrence_Of (Typ, Obj_Loc),
4213 Attribute_Name => Name_First),
4214 Right_Opnd =>
4215 Make_Integer_Literal (Obj_Loc, Uint_2))),
4216 Right_Opnd =>
4217 Make_Integer_Literal (Obj_Loc, (Uint_2 ** 30))),
4218 Reason => SE_Object_Too_Large));
4219 end if;
4220 end Check_Large_Modular_Array;
4222 ---------------------------------------
4223 -- Check_Pragma_Thread_Local_Storage --
4224 ---------------------------------------
4226 procedure Check_Pragma_Thread_Local_Storage (Var_Id : Entity_Id) is
4227 function Has_Incompatible_Initialization
4228 (Var_Decl : Node_Id) return Boolean;
4229 -- Determine whether variable Var_Id with declaration Var_Decl is
4230 -- initialized with a value that violates the semantics of pragma
4231 -- Thread_Local_Storage.
4233 -------------------------------------
4234 -- Has_Incompatible_Initialization --
4235 -------------------------------------
4237 function Has_Incompatible_Initialization
4238 (Var_Decl : Node_Id) return Boolean
4240 Init_Expr : constant Node_Id := Expression (Var_Decl);
4242 begin
4243 -- The variable is default-initialized. This directly violates
4244 -- the semantics of the pragma.
4246 if Has_Default_Initialization (Var_Id) then
4247 return True;
4249 -- The variable has explicit initialization. In this case only
4250 -- a handful of values satisfy the semantics of the pragma.
4252 elsif Has_Init_Expression (Var_Decl)
4253 and then Present (Init_Expr)
4254 then
4255 -- "null" is a legal form of initialization
4257 if Nkind (Init_Expr) = N_Null then
4258 return False;
4260 -- A static expression is a legal form of initialization
4262 elsif Is_Static_Expression (Init_Expr) then
4263 return False;
4265 -- A static aggregate is a legal form of initialization
4267 elsif Nkind (Init_Expr) = N_Aggregate
4268 and then Compile_Time_Known_Aggregate (Init_Expr)
4269 then
4270 return False;
4272 -- All other initialization expressions violate the semantic
4273 -- of the pragma.
4275 else
4276 return True;
4277 end if;
4279 -- The variable lacks any kind of initialization, which agrees
4280 -- with the semantics of the pragma.
4282 else
4283 return False;
4284 end if;
4285 end Has_Incompatible_Initialization;
4287 -- Local declarations
4289 Var_Decl : constant Node_Id := Declaration_Node (Var_Id);
4291 -- Start of processing for Check_Pragma_Thread_Local_Storage
4293 begin
4294 -- A variable whose initialization is suppressed lacks any kind of
4295 -- initialization.
4297 if Suppress_Initialization (Var_Id) then
4298 null;
4300 -- The variable has default initialization, or is explicitly
4301 -- initialized to a value other than null, static expression,
4302 -- or a static aggregate.
4304 elsif Has_Incompatible_Initialization (Var_Decl) then
4305 Error_Msg_NE
4306 ("Thread_Local_Storage variable& is improperly initialized",
4307 Var_Decl, Var_Id);
4308 Error_Msg_NE
4309 ("\only allowed initialization is explicit NULL, static "
4310 & "expression or static aggregate", Var_Decl, Var_Id);
4311 end if;
4312 end Check_Pragma_Thread_Local_Storage;
4314 --------------------------------
4315 -- Has_Default_Initialization --
4316 --------------------------------
4318 function Has_Default_Initialization
4319 (Obj_Id : Entity_Id) return Boolean
4321 Obj_Decl : constant Node_Id := Declaration_Node (Obj_Id);
4322 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4324 begin
4325 return
4326 Comes_From_Source (Obj_Id)
4327 and then not Is_Imported (Obj_Id)
4328 and then not Has_Init_Expression (Obj_Decl)
4329 and then
4330 ((Has_Non_Null_Base_Init_Proc (Obj_Typ)
4331 and then not No_Initialization (Obj_Decl)
4332 and then not Initialization_Suppressed (Obj_Typ))
4333 or else
4334 (Needs_Simple_Initialization (Obj_Typ)
4335 and then not Is_Internal (Obj_Id)));
4336 end Has_Default_Initialization;
4338 -- Local variables
4340 Typ : constant Entity_Id := Etype (E);
4341 Def : Node_Id;
4343 -- Start of processing for Freeze_Object_Declaration
4345 begin
4346 -- Abstract type allowed only for C++ imported variables or constants
4348 -- Note: we inhibit this check for objects that do not come from
4349 -- source because there is at least one case (the expansion of
4350 -- x'Class'Input where x is abstract) where we legitimately
4351 -- generate an abstract object.
4353 if Is_Abstract_Type (Typ)
4354 and then Comes_From_Source (Parent (E))
4355 and then not (Is_Imported (E) and then Is_CPP_Class (Typ))
4356 then
4357 Def := Object_Definition (Parent (E));
4359 Error_Msg_N ("type of object cannot be abstract", Def);
4361 if Is_CPP_Class (Etype (E)) then
4362 Error_Msg_NE ("\} may need a cpp_constructor", Def, Typ);
4364 elsif Present (Expression (Parent (E))) then
4365 Error_Msg_N -- CODEFIX
4366 ("\maybe a class-wide type was meant", Def);
4367 end if;
4368 end if;
4370 -- For object created by object declaration, perform required
4371 -- categorization (preelaborate and pure) checks. Defer these
4372 -- checks to freeze time since pragma Import inhibits default
4373 -- initialization and thus pragma Import affects these checks.
4375 Validate_Object_Declaration (Declaration_Node (E));
4377 -- If there is an address clause, check that it is valid and if need
4378 -- be move initialization to the freeze node.
4380 Check_Address_Clause (E);
4382 -- Similar processing is needed for aspects that may affect object
4383 -- layout, like Address, if there is an initialization expression.
4384 -- We don't do this if there is a pragma Linker_Section, because it
4385 -- would prevent the back end from statically initializing the
4386 -- object; we don't want elaboration code in that case.
4388 if Has_Delayed_Aspects (E)
4389 and then Expander_Active
4390 and then Is_Array_Type (Typ)
4391 and then Present (Expression (Declaration_Node (E)))
4392 and then No (Linker_Section_Pragma (E))
4393 then
4394 declare
4395 Decl : constant Node_Id := Declaration_Node (E);
4396 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
4398 begin
4399 -- Capture initialization value at point of declaration, and
4400 -- make explicit assignment legal, because object may be a
4401 -- constant.
4403 Remove_Side_Effects (Expression (Decl));
4404 Set_Assignment_OK (Lhs);
4406 -- Move initialization to freeze actions
4408 Append_Freeze_Action (E,
4409 Make_Assignment_Statement (Loc,
4410 Name => Lhs,
4411 Expression => Expression (Decl)));
4413 Set_No_Initialization (Decl);
4414 -- Set_Is_Frozen (E, False);
4415 end;
4416 end if;
4418 -- Reset Is_True_Constant for non-constant aliased object. We
4419 -- consider that the fact that a non-constant object is aliased may
4420 -- indicate that some funny business is going on, e.g. an aliased
4421 -- object is passed by reference to a procedure which captures the
4422 -- address of the object, which is later used to assign a new value,
4423 -- even though the compiler thinks that it is not modified. Such
4424 -- code is highly dubious, but we choose to make it "work" for
4425 -- non-constant aliased objects.
4427 -- Note that we used to do this for all aliased objects, whether or
4428 -- not constant, but this caused anomalies down the line because we
4429 -- ended up with static objects that were not Is_True_Constant. Not
4430 -- resetting Is_True_Constant for (aliased) constant objects ensures
4431 -- that this anomaly never occurs.
4433 -- However, we don't do that for internal entities. We figure that if
4434 -- we deliberately set Is_True_Constant for an internal entity, e.g.
4435 -- a dispatch table entry, then we mean it.
4437 if Ekind (E) /= E_Constant
4438 and then (Is_Aliased (E) or else Is_Aliased (Typ))
4439 and then not Is_Internal_Name (Chars (E))
4440 then
4441 Set_Is_True_Constant (E, False);
4442 end if;
4444 -- If the object needs any kind of default initialization, an error
4445 -- must be issued if No_Default_Initialization applies. The check
4446 -- doesn't apply to imported objects, which are not ever default
4447 -- initialized, and is why the check is deferred until freezing, at
4448 -- which point we know if Import applies. Deferred constants are also
4449 -- exempted from this test because their completion is explicit, or
4450 -- through an import pragma.
4452 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
4453 null;
4455 elsif Has_Default_Initialization (E) then
4456 Check_Restriction
4457 (No_Default_Initialization, Declaration_Node (E));
4458 end if;
4460 -- Ensure that a variable subject to pragma Thread_Local_Storage
4462 -- * Lacks default initialization, or
4464 -- * The initialization expression is either "null", a static
4465 -- constant, or a compile-time known aggregate.
4467 if Has_Pragma_Thread_Local_Storage (E) then
4468 Check_Pragma_Thread_Local_Storage (E);
4469 end if;
4471 -- For imported objects, set Is_Public unless there is also an
4472 -- address clause, which means that there is no external symbol
4473 -- needed for the Import (Is_Public may still be set for other
4474 -- unrelated reasons). Note that we delayed this processing
4475 -- till freeze time so that we can be sure not to set the flag
4476 -- if there is an address clause. If there is such a clause,
4477 -- then the only purpose of the Import pragma is to suppress
4478 -- implicit initialization.
4480 if Is_Imported (E) and then No (Address_Clause (E)) then
4481 Set_Is_Public (E);
4482 end if;
4484 -- For source objects that are not Imported and are library level, if
4485 -- no linker section pragma was given inherit the appropriate linker
4486 -- section from the corresponding type.
4488 if Comes_From_Source (E)
4489 and then not Is_Imported (E)
4490 and then Is_Library_Level_Entity (E)
4491 and then No (Linker_Section_Pragma (E))
4492 then
4493 Set_Linker_Section_Pragma (E, Linker_Section_Pragma (Typ));
4494 end if;
4496 -- For convention C objects of an enumeration type, warn if the size
4497 -- is not integer size and no explicit size given. Skip warning for
4498 -- Boolean and Character, and assume programmer expects 8-bit sizes
4499 -- for these cases.
4501 if (Convention (E) = Convention_C
4502 or else
4503 Convention (E) = Convention_CPP)
4504 and then Is_Enumeration_Type (Typ)
4505 and then not Is_Character_Type (Typ)
4506 and then not Is_Boolean_Type (Typ)
4507 and then Esize (Typ) < Standard_Integer_Size
4508 and then not Has_Size_Clause (E)
4509 then
4510 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
4511 Error_Msg_N
4512 ("??convention C enumeration object has size less than ^", E);
4513 Error_Msg_N ("\??use explicit size clause to set size", E);
4514 end if;
4516 -- Declaring too big an array in disabled ghost code is OK
4518 if Is_Array_Type (Typ) and then not Is_Ignored_Ghost_Entity (E) then
4519 Check_Large_Modular_Array (Typ);
4520 end if;
4521 end Freeze_Object_Declaration;
4523 -----------------------------
4524 -- Freeze_Generic_Entities --
4525 -----------------------------
4527 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
4528 E : Entity_Id;
4529 F : Node_Id;
4530 Flist : List_Id;
4532 begin
4533 Flist := New_List;
4534 E := First_Entity (Pack);
4535 while Present (E) loop
4536 if Is_Type (E) and then not Is_Generic_Type (E) then
4537 F := Make_Freeze_Generic_Entity (Sloc (Pack));
4538 Set_Entity (F, E);
4539 Append_To (Flist, F);
4541 elsif Ekind (E) = E_Generic_Package then
4542 Append_List_To (Flist, Freeze_Generic_Entities (E));
4543 end if;
4545 Next_Entity (E);
4546 end loop;
4548 return Flist;
4549 end Freeze_Generic_Entities;
4551 --------------------
4552 -- Freeze_Profile --
4553 --------------------
4555 function Freeze_Profile (E : Entity_Id) return Boolean is
4556 F_Type : Entity_Id;
4557 R_Type : Entity_Id;
4558 Warn_Node : Node_Id;
4560 begin
4561 -- Loop through formals
4563 Formal := First_Formal (E);
4564 while Present (Formal) loop
4565 F_Type := Etype (Formal);
4567 -- AI05-0151: incomplete types can appear in a profile. By the
4568 -- time the entity is frozen, the full view must be available,
4569 -- unless it is a limited view.
4571 if Is_Incomplete_Type (F_Type)
4572 and then Present (Full_View (F_Type))
4573 and then not From_Limited_With (F_Type)
4574 then
4575 F_Type := Full_View (F_Type);
4576 Set_Etype (Formal, F_Type);
4577 end if;
4579 if not From_Limited_With (F_Type)
4580 and then Should_Freeze_Type (F_Type, E, N)
4581 then
4582 Freeze_And_Append (F_Type, N, Result);
4583 end if;
4585 if Is_Private_Type (F_Type)
4586 and then Is_Private_Type (Base_Type (F_Type))
4587 and then No (Full_View (Base_Type (F_Type)))
4588 and then not Is_Generic_Type (F_Type)
4589 and then not Is_Derived_Type (F_Type)
4590 then
4591 -- If the type of a formal is incomplete, subprogram is being
4592 -- frozen prematurely. Within an instance (but not within a
4593 -- wrapper package) this is an artifact of our need to regard
4594 -- the end of an instantiation as a freeze point. Otherwise it
4595 -- is a definite error.
4597 if In_Instance then
4598 Set_Is_Frozen (E, False);
4599 Result := No_List;
4600 return False;
4602 elsif not After_Last_Declaration then
4603 Error_Msg_NE
4604 ("type & must be fully defined before this point",
4606 F_Type);
4607 end if;
4608 end if;
4610 -- Check suspicious parameter for C function. These tests apply
4611 -- only to exported/imported subprograms.
4613 if Warn_On_Export_Import
4614 and then Comes_From_Source (E)
4615 and then Convention (E) in Convention_C_Family
4616 and then (Is_Imported (E) or else Is_Exported (E))
4617 and then Convention (E) /= Convention (Formal)
4618 and then not Has_Warnings_Off (E)
4619 and then not Has_Warnings_Off (F_Type)
4620 and then not Has_Warnings_Off (Formal)
4621 then
4622 -- Qualify mention of formals with subprogram name
4624 Error_Msg_Qual_Level := 1;
4626 -- Check suspicious use of fat C pointer, but do not emit
4627 -- a warning on an access to subprogram when unnesting is
4628 -- active.
4630 if Is_Access_Type (F_Type)
4631 and then Known_Esize (F_Type)
4632 and then Esize (F_Type) > Ttypes.System_Address_Size
4633 and then (not Unnest_Subprogram_Mode
4634 or else not Is_Access_Subprogram_Type (F_Type))
4635 then
4636 Error_Msg_N
4637 ("?x?type of & does not correspond to C pointer!", Formal);
4639 -- Check suspicious return of boolean
4641 elsif Root_Type (F_Type) = Standard_Boolean
4642 and then Convention (F_Type) = Convention_Ada
4643 and then not Has_Warnings_Off (F_Type)
4644 and then not Has_Size_Clause (F_Type)
4645 then
4646 Error_Msg_N
4647 ("& is an 8-bit Ada Boolean?x?", Formal);
4648 Error_Msg_N
4649 ("\use appropriate corresponding type in C "
4650 & "(e.g. char)?x?", Formal);
4652 -- Check suspicious tagged type
4654 elsif (Is_Tagged_Type (F_Type)
4655 or else
4656 (Is_Access_Type (F_Type)
4657 and then Is_Tagged_Type (Designated_Type (F_Type))))
4658 and then Convention (E) = Convention_C
4659 then
4660 Error_Msg_N
4661 ("?x?& involves a tagged type which does not "
4662 & "correspond to any C type!", Formal);
4664 -- Check wrong convention subprogram pointer
4666 elsif Ekind (F_Type) = E_Access_Subprogram_Type
4667 and then not Has_Foreign_Convention (F_Type)
4668 then
4669 Error_Msg_N
4670 ("?x?subprogram pointer & should "
4671 & "have foreign convention!", Formal);
4672 Error_Msg_Sloc := Sloc (F_Type);
4673 Error_Msg_NE
4674 ("\?x?add Convention pragma to declaration of &#",
4675 Formal, F_Type);
4676 end if;
4678 -- Turn off name qualification after message output
4680 Error_Msg_Qual_Level := 0;
4681 end if;
4683 -- Check for unconstrained array in exported foreign convention
4684 -- case.
4686 if Has_Foreign_Convention (E)
4687 and then not Is_Imported (E)
4688 and then Is_Array_Type (F_Type)
4689 and then not Is_Constrained (F_Type)
4690 and then Warn_On_Export_Import
4691 then
4692 Error_Msg_Qual_Level := 1;
4694 -- If this is an inherited operation, place the warning on
4695 -- the derived type declaration, rather than on the original
4696 -- subprogram.
4698 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
4699 then
4700 Warn_Node := Parent (E);
4702 if Formal = First_Formal (E) then
4703 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
4704 end if;
4705 else
4706 Warn_Node := Formal;
4707 end if;
4709 Error_Msg_NE ("?x?type of argument& is unconstrained array",
4710 Warn_Node, Formal);
4711 Error_Msg_N ("\?x?foreign caller must pass bounds explicitly",
4712 Warn_Node);
4713 Error_Msg_Qual_Level := 0;
4714 end if;
4716 if not From_Limited_With (F_Type) then
4717 if Is_Access_Type (F_Type) then
4718 F_Type := Designated_Type (F_Type);
4719 end if;
4720 end if;
4722 Next_Formal (Formal);
4723 end loop;
4725 -- Case of function: similar checks on return type
4727 if Ekind (E) = E_Function then
4729 -- Freeze return type
4731 R_Type := Etype (E);
4733 -- AI05-0151: the return type may have been incomplete at the
4734 -- point of declaration. Replace it with the full view, unless the
4735 -- current type is a limited view. In that case the full view is
4736 -- in a different unit, and gigi finds the non-limited view after
4737 -- the other unit is elaborated.
4739 if Ekind (R_Type) = E_Incomplete_Type
4740 and then Present (Full_View (R_Type))
4741 and then not From_Limited_With (R_Type)
4742 then
4743 R_Type := Full_View (R_Type);
4744 Set_Etype (E, R_Type);
4745 end if;
4747 if Should_Freeze_Type (R_Type, E, N) then
4748 Freeze_And_Append (R_Type, N, Result);
4749 end if;
4751 -- Check suspicious return type for C function
4753 if Warn_On_Export_Import
4754 and then Comes_From_Source (E)
4755 and then Convention (E) in Convention_C_Family
4756 and then (Is_Imported (E) or else Is_Exported (E))
4757 then
4758 -- Check suspicious return of fat C pointer
4760 if Is_Access_Type (R_Type)
4761 and then Known_Esize (R_Type)
4762 and then Esize (R_Type) > Ttypes.System_Address_Size
4763 and then not Has_Warnings_Off (E)
4764 and then not Has_Warnings_Off (R_Type)
4765 then
4766 Error_Msg_N
4767 ("?x?return type of& does not correspond to C pointer!",
4770 -- Check suspicious return of boolean
4772 elsif Root_Type (R_Type) = Standard_Boolean
4773 and then Convention (R_Type) = Convention_Ada
4774 and then not Has_Warnings_Off (E)
4775 and then not Has_Warnings_Off (R_Type)
4776 and then not Has_Size_Clause (R_Type)
4777 then
4778 declare
4779 N : constant Node_Id :=
4780 Result_Definition (Declaration_Node (E));
4781 begin
4782 Error_Msg_NE
4783 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
4784 Error_Msg_NE
4785 ("\use appropriate corresponding type in C "
4786 & "(e.g. char)?x?", N, E);
4787 end;
4789 -- Check suspicious return tagged type
4791 elsif (Is_Tagged_Type (R_Type)
4792 or else (Is_Access_Type (R_Type)
4793 and then
4794 Is_Tagged_Type
4795 (Designated_Type (R_Type))))
4796 and then Convention (E) = Convention_C
4797 and then not Has_Warnings_Off (E)
4798 and then not Has_Warnings_Off (R_Type)
4799 then
4800 Error_Msg_N ("?x?return type of & does not "
4801 & "correspond to C type!", E);
4803 -- Check return of wrong convention subprogram pointer
4805 elsif Ekind (R_Type) = E_Access_Subprogram_Type
4806 and then not Has_Foreign_Convention (R_Type)
4807 and then not Has_Warnings_Off (E)
4808 and then not Has_Warnings_Off (R_Type)
4809 then
4810 Error_Msg_N ("?x?& should return a foreign "
4811 & "convention subprogram pointer", E);
4812 Error_Msg_Sloc := Sloc (R_Type);
4813 Error_Msg_NE
4814 ("\?x?add Convention pragma to declaration of& #",
4815 E, R_Type);
4816 end if;
4817 end if;
4819 -- Give warning for suspicious return of a result of an
4820 -- unconstrained array type in a foreign convention function.
4822 if Has_Foreign_Convention (E)
4824 -- We are looking for a return of unconstrained array
4826 and then Is_Array_Type (R_Type)
4827 and then not Is_Constrained (R_Type)
4829 -- Exclude imported routines, the warning does not belong on
4830 -- the import, but rather on the routine definition.
4832 and then not Is_Imported (E)
4834 -- Check that general warning is enabled, and that it is not
4835 -- suppressed for this particular case.
4837 and then Warn_On_Export_Import
4838 and then not Has_Warnings_Off (E)
4839 and then not Has_Warnings_Off (R_Type)
4840 then
4841 Error_Msg_N
4842 ("?x?foreign convention function& should not return "
4843 & "unconstrained array!", E);
4844 end if;
4845 end if;
4847 -- Check suspicious use of Import in pure unit (cases where the RM
4848 -- allows calls to be omitted).
4850 if Is_Imported (E)
4852 -- It might be suspicious if the compilation unit has the Pure
4853 -- aspect/pragma.
4855 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
4857 -- The RM allows omission of calls only in the case of
4858 -- library-level subprograms (see RM-10.2.1(18)).
4860 and then Is_Library_Level_Entity (E)
4862 -- Ignore internally generated entity. This happens in some cases
4863 -- of subprograms in specs, where we generate an implied body.
4865 and then Comes_From_Source (Import_Pragma (E))
4867 -- Assume run-time knows what it is doing
4869 and then not GNAT_Mode
4871 -- Assume explicit Pure_Function means import is pure
4873 and then not Has_Pragma_Pure_Function (E)
4875 -- Don't need warning in relaxed semantics mode
4877 and then not Relaxed_RM_Semantics
4879 -- Assume convention Intrinsic is OK, since this is specialized.
4880 -- This deals with the DEC unit current_exception.ads
4882 and then Convention (E) /= Convention_Intrinsic
4884 -- Assume that ASM interface knows what it is doing
4886 and then Convention (E) /= Convention_Assembler
4887 then
4888 Error_Msg_N
4889 ("pragma Import in Pure unit??", Import_Pragma (E));
4890 Error_Msg_NE
4891 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
4892 Import_Pragma (E), E);
4893 end if;
4895 return True;
4896 end Freeze_Profile;
4898 ------------------------
4899 -- Freeze_Record_Type --
4900 ------------------------
4902 procedure Freeze_Record_Type (Rec : Entity_Id) is
4903 ADC : Node_Id;
4904 Comp : Entity_Id;
4905 IR : Node_Id;
4906 Prev : Entity_Id;
4908 Junk : Boolean;
4909 pragma Warnings (Off, Junk);
4911 Aliased_Component : Boolean := False;
4912 -- Set True if we find at least one component which is aliased. This
4913 -- is used to prevent Implicit_Packing of the record, since packing
4914 -- cannot modify the size of alignment of an aliased component.
4916 All_Elem_Components : Boolean := True;
4917 -- True if all components are of a type whose underlying type is
4918 -- elementary.
4920 All_Sized_Components : Boolean := True;
4921 -- True if all components have a known RM_Size
4923 All_Storage_Unit_Components : Boolean := True;
4924 -- True if all components have an RM_Size that is a multiple of the
4925 -- storage unit.
4927 Elem_Component_Total_Esize : Uint := Uint_0;
4928 -- Accumulates total Esize values of all elementary components. Used
4929 -- for processing of Implicit_Packing.
4931 Placed_Component : Boolean := False;
4932 -- Set True if we find at least one component with a component
4933 -- clause (used to warn about useless Bit_Order pragmas, and also
4934 -- to detect cases where Implicit_Packing may have an effect).
4936 Sized_Component_Total_RM_Size : Uint := Uint_0;
4937 -- Accumulates total RM_Size values of all sized components. Used
4938 -- for processing of Implicit_Packing.
4940 Sized_Component_Total_Round_RM_Size : Uint := Uint_0;
4941 -- Accumulates total RM_Size values of all sized components, rounded
4942 -- individually to a multiple of the storage unit.
4944 SSO_ADC : Node_Id;
4945 -- Scalar_Storage_Order attribute definition clause for the record
4947 SSO_ADC_Component : Boolean := False;
4948 -- Set True if we find at least one component whose type has a
4949 -- Scalar_Storage_Order attribute definition clause.
4951 Unplaced_Component : Boolean := False;
4952 -- Set True if we find at least one component with no component
4953 -- clause (used to warn about useless Pack pragmas).
4955 procedure Check_Itype (Typ : Entity_Id);
4956 -- If the component subtype is an access to a constrained subtype of
4957 -- an already frozen type, make the subtype frozen as well. It might
4958 -- otherwise be frozen in the wrong scope, and a freeze node on
4959 -- subtype has no effect. Similarly, if the component subtype is a
4960 -- regular (not protected) access to subprogram, set the anonymous
4961 -- subprogram type to frozen as well, to prevent an out-of-scope
4962 -- freeze node at some eventual point of call. Protected operations
4963 -- are handled elsewhere.
4965 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
4966 -- Make sure that all types mentioned in Discrete_Choices of the
4967 -- variants referenceed by the Variant_Part VP are frozen. This is
4968 -- a recursive routine to deal with nested variants.
4970 -----------------
4971 -- Check_Itype --
4972 -----------------
4974 procedure Check_Itype (Typ : Entity_Id) is
4975 Desig : constant Entity_Id := Designated_Type (Typ);
4977 begin
4978 if not Is_Frozen (Desig)
4979 and then Is_Frozen (Base_Type (Desig))
4980 then
4981 Set_Is_Frozen (Desig);
4983 -- In addition, add an Itype_Reference to ensure that the
4984 -- access subtype is elaborated early enough. This cannot be
4985 -- done if the subtype may depend on discriminants.
4987 if Ekind (Comp) = E_Component
4988 and then Is_Itype (Etype (Comp))
4989 and then not Has_Discriminants (Rec)
4990 then
4991 IR := Make_Itype_Reference (Sloc (Comp));
4992 Set_Itype (IR, Desig);
4993 Add_To_Result (IR);
4994 end if;
4996 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
4997 and then Convention (Desig) /= Convention_Protected
4998 then
4999 Set_Is_Frozen (Desig);
5000 Create_Extra_Formals (Desig);
5001 end if;
5002 end Check_Itype;
5004 ------------------------------------
5005 -- Freeze_Choices_In_Variant_Part --
5006 ------------------------------------
5008 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
5009 pragma Assert (Nkind (VP) = N_Variant_Part);
5011 Variant : Node_Id;
5012 Choice : Node_Id;
5013 CL : Node_Id;
5015 begin
5016 -- Loop through variants
5018 Variant := First_Non_Pragma (Variants (VP));
5019 while Present (Variant) loop
5021 -- Loop through choices, checking that all types are frozen
5023 Choice := First_Non_Pragma (Discrete_Choices (Variant));
5024 while Present (Choice) loop
5025 if Nkind (Choice) in N_Has_Etype
5026 and then Present (Etype (Choice))
5027 then
5028 Freeze_And_Append (Etype (Choice), N, Result);
5029 end if;
5031 Next_Non_Pragma (Choice);
5032 end loop;
5034 -- Check for nested variant part to process
5036 CL := Component_List (Variant);
5038 if not Null_Present (CL) then
5039 if Present (Variant_Part (CL)) then
5040 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
5041 end if;
5042 end if;
5044 Next_Non_Pragma (Variant);
5045 end loop;
5046 end Freeze_Choices_In_Variant_Part;
5048 -- Start of processing for Freeze_Record_Type
5050 begin
5051 -- Freeze components and embedded subtypes
5053 Comp := First_Entity (Rec);
5054 Prev := Empty;
5055 while Present (Comp) loop
5056 if Is_Aliased (Comp) then
5057 Aliased_Component := True;
5058 end if;
5060 -- Handle the component and discriminant case
5062 if Ekind (Comp) in E_Component | E_Discriminant then
5063 declare
5064 CC : constant Node_Id := Component_Clause (Comp);
5066 begin
5067 -- Freezing a record type freezes the type of each of its
5068 -- components. However, if the type of the component is
5069 -- part of this record, we do not want or need a separate
5070 -- Freeze_Node. Note that Is_Itype is wrong because that's
5071 -- also set in private type cases. We also can't check for
5072 -- the Scope being exactly Rec because of private types and
5073 -- record extensions.
5075 if Is_Itype (Etype (Comp))
5076 and then Is_Record_Type (Underlying_Type
5077 (Scope (Etype (Comp))))
5078 then
5079 Undelay_Type (Etype (Comp));
5080 end if;
5082 Freeze_And_Append (Etype (Comp), N, Result);
5084 -- Warn for pragma Pack overriding foreign convention
5086 if Has_Foreign_Convention (Etype (Comp))
5087 and then Has_Pragma_Pack (Rec)
5089 -- Don't warn for aliased components, since override
5090 -- cannot happen in that case.
5092 and then not Is_Aliased (Comp)
5093 then
5094 declare
5095 CN : constant Name_Id :=
5096 Get_Convention_Name (Convention (Etype (Comp)));
5097 PP : constant Node_Id :=
5098 Get_Pragma (Rec, Pragma_Pack);
5099 begin
5100 if Present (PP) then
5101 Error_Msg_Name_1 := CN;
5102 Error_Msg_Sloc := Sloc (Comp);
5103 Error_Msg_N
5104 ("pragma Pack affects convention % component#??",
5105 PP);
5106 Error_Msg_Name_1 := CN;
5107 Error_Msg_NE
5108 ("\component & may not have % compatible "
5109 & "representation??", PP, Comp);
5110 end if;
5111 end;
5112 end if;
5114 -- Check for error of component clause given for variable
5115 -- sized type. We have to delay this test till this point,
5116 -- since the component type has to be frozen for us to know
5117 -- if it is variable length.
5119 if Present (CC) then
5120 Placed_Component := True;
5122 -- We omit this test in a generic context, it will be
5123 -- applied at instantiation time.
5125 if Inside_A_Generic then
5126 null;
5128 -- Also omit this test in CodePeer mode, since we do not
5129 -- have sufficient info on size and rep clauses.
5131 elsif CodePeer_Mode then
5132 null;
5134 -- Do the check
5136 elsif not
5137 Size_Known_At_Compile_Time
5138 (Underlying_Type (Etype (Comp)))
5139 then
5140 Error_Msg_N
5141 ("component clause not allowed for variable " &
5142 "length component", CC);
5143 end if;
5145 else
5146 Unplaced_Component := True;
5147 end if;
5149 -- Case of component requires byte alignment
5151 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
5153 -- Set the enclosing record to also require byte align
5155 Set_Must_Be_On_Byte_Boundary (Rec);
5157 -- Check for component clause that is inconsistent with
5158 -- the required byte boundary alignment.
5160 if Present (CC)
5161 and then Normalized_First_Bit (Comp) mod
5162 System_Storage_Unit /= 0
5163 then
5164 Error_Msg_N
5165 ("component & must be byte aligned",
5166 Component_Name (Component_Clause (Comp)));
5167 end if;
5168 end if;
5169 end;
5170 end if;
5172 -- Gather data for possible Implicit_Packing later. Note that at
5173 -- this stage we might be dealing with a real component, or with
5174 -- an implicit subtype declaration.
5176 if Known_Static_RM_Size (Etype (Comp)) then
5177 declare
5178 Comp_Type : constant Entity_Id := Etype (Comp);
5179 Comp_Size : constant Uint := RM_Size (Comp_Type);
5180 SSU : constant Int := Ttypes.System_Storage_Unit;
5182 begin
5183 Sized_Component_Total_RM_Size :=
5184 Sized_Component_Total_RM_Size + Comp_Size;
5186 Sized_Component_Total_Round_RM_Size :=
5187 Sized_Component_Total_Round_RM_Size +
5188 (Comp_Size + SSU - 1) / SSU * SSU;
5190 if Present (Underlying_Type (Comp_Type))
5191 and then Is_Elementary_Type (Underlying_Type (Comp_Type))
5192 then
5193 Elem_Component_Total_Esize :=
5194 Elem_Component_Total_Esize + Esize (Comp_Type);
5195 else
5196 All_Elem_Components := False;
5198 if Comp_Size mod SSU /= 0 then
5199 All_Storage_Unit_Components := False;
5200 end if;
5201 end if;
5202 end;
5203 else
5204 All_Sized_Components := False;
5205 end if;
5207 -- If the component is an Itype with Delayed_Freeze and is either
5208 -- a record or array subtype and its base type has not yet been
5209 -- frozen, we must remove this from the entity list of this record
5210 -- and put it on the entity list of the scope of its base type.
5211 -- Note that we know that this is not the type of a component
5212 -- since we cleared Has_Delayed_Freeze for it in the previous
5213 -- loop. Thus this must be the Designated_Type of an access type,
5214 -- which is the type of a component.
5216 if Is_Itype (Comp)
5217 and then Is_Type (Scope (Comp))
5218 and then Is_Composite_Type (Comp)
5219 and then Base_Type (Comp) /= Comp
5220 and then Has_Delayed_Freeze (Comp)
5221 and then not Is_Frozen (Base_Type (Comp))
5222 then
5223 declare
5224 Will_Be_Frozen : Boolean := False;
5225 S : Entity_Id;
5227 begin
5228 -- We have a difficult case to handle here. Suppose Rec is
5229 -- subtype being defined in a subprogram that's created as
5230 -- part of the freezing of Rec'Base. In that case, we know
5231 -- that Comp'Base must have already been frozen by the time
5232 -- we get to elaborate this because Gigi doesn't elaborate
5233 -- any bodies until it has elaborated all of the declarative
5234 -- part. But Is_Frozen will not be set at this point because
5235 -- we are processing code in lexical order.
5237 -- We detect this case by going up the Scope chain of Rec
5238 -- and seeing if we have a subprogram scope before reaching
5239 -- the top of the scope chain or that of Comp'Base. If we
5240 -- do, then mark that Comp'Base will actually be frozen. If
5241 -- so, we merely undelay it.
5243 S := Scope (Rec);
5244 while Present (S) loop
5245 if Is_Subprogram (S) then
5246 Will_Be_Frozen := True;
5247 exit;
5248 elsif S = Scope (Base_Type (Comp)) then
5249 exit;
5250 end if;
5252 S := Scope (S);
5253 end loop;
5255 if Will_Be_Frozen then
5256 Undelay_Type (Comp);
5258 else
5259 if Present (Prev) then
5260 Link_Entities (Prev, Next_Entity (Comp));
5261 else
5262 Set_First_Entity (Rec, Next_Entity (Comp));
5263 end if;
5265 -- Insert in entity list of scope of base type (which
5266 -- must be an enclosing scope, because still unfrozen).
5268 Append_Entity (Comp, Scope (Base_Type (Comp)));
5269 end if;
5270 end;
5272 -- If the component is an access type with an allocator as default
5273 -- value, the designated type will be frozen by the corresponding
5274 -- expression in init_proc. In order to place the freeze node for
5275 -- the designated type before that for the current record type,
5276 -- freeze it now.
5278 -- Same process if the component is an array of access types,
5279 -- initialized with an aggregate. If the designated type is
5280 -- private, it cannot contain allocators, and it is premature
5281 -- to freeze the type, so we check for this as well.
5283 elsif Is_Access_Type (Etype (Comp))
5284 and then Present (Parent (Comp))
5285 and then
5286 Nkind (Parent (Comp))
5287 in N_Component_Declaration | N_Discriminant_Specification
5288 and then Present (Expression (Parent (Comp)))
5289 then
5290 declare
5291 Alloc : constant Node_Id :=
5292 Unqualify (Expression (Parent (Comp)));
5294 begin
5295 if Nkind (Alloc) = N_Allocator then
5297 -- If component is pointer to a class-wide type, freeze
5298 -- the specific type in the expression being allocated.
5299 -- The expression may be a subtype indication, in which
5300 -- case freeze the subtype mark.
5302 if Is_Class_Wide_Type (Designated_Type (Etype (Comp)))
5303 then
5304 if Is_Entity_Name (Expression (Alloc)) then
5305 Freeze_And_Append
5306 (Entity (Expression (Alloc)), N, Result);
5308 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
5309 then
5310 Freeze_And_Append
5311 (Entity (Subtype_Mark (Expression (Alloc))),
5312 N, Result);
5313 end if;
5314 elsif Is_Itype (Designated_Type (Etype (Comp))) then
5315 Check_Itype (Etype (Comp));
5316 else
5317 Freeze_And_Append
5318 (Designated_Type (Etype (Comp)), N, Result);
5319 end if;
5320 end if;
5321 end;
5322 elsif Is_Access_Type (Etype (Comp))
5323 and then Is_Itype (Designated_Type (Etype (Comp)))
5324 then
5325 Check_Itype (Etype (Comp));
5327 -- Freeze the designated type when initializing a component with
5328 -- an aggregate in case the aggregate contains allocators.
5330 -- type T is ...;
5331 -- type T_Ptr is access all T;
5332 -- type T_Array is array ... of T_Ptr;
5334 -- type Rec is record
5335 -- Comp : T_Array := (others => ...);
5336 -- end record;
5338 elsif Is_Array_Type (Etype (Comp))
5339 and then Is_Access_Type (Component_Type (Etype (Comp)))
5340 then
5341 declare
5342 Comp_Par : constant Node_Id := Parent (Comp);
5343 Desig_Typ : constant Entity_Id :=
5344 Designated_Type
5345 (Component_Type (Etype (Comp)));
5347 begin
5348 -- The only case when this sort of freezing is not done is
5349 -- when the designated type is class-wide and the root type
5350 -- is the record owning the component. This scenario results
5351 -- in a circularity because the class-wide type requires
5352 -- primitives that have not been created yet as the root
5353 -- type is in the process of being frozen.
5355 -- type Rec is tagged;
5356 -- type Rec_Ptr is access all Rec'Class;
5357 -- type Rec_Array is array ... of Rec_Ptr;
5359 -- type Rec is record
5360 -- Comp : Rec_Array := (others => ...);
5361 -- end record;
5363 if Is_Class_Wide_Type (Desig_Typ)
5364 and then Root_Type (Desig_Typ) = Rec
5365 then
5366 null;
5368 elsif Is_Fully_Defined (Desig_Typ)
5369 and then Present (Comp_Par)
5370 and then Nkind (Comp_Par) = N_Component_Declaration
5371 and then Present (Expression (Comp_Par))
5372 and then Nkind (Expression (Comp_Par)) = N_Aggregate
5373 then
5374 Freeze_And_Append (Desig_Typ, N, Result);
5375 end if;
5376 end;
5377 end if;
5379 Prev := Comp;
5380 Next_Entity (Comp);
5381 end loop;
5383 SSO_ADC :=
5384 Get_Attribute_Definition_Clause
5385 (Rec, Attribute_Scalar_Storage_Order);
5387 -- If the record type has Complex_Representation, then it is treated
5388 -- as a scalar in the back end so the storage order is irrelevant.
5390 if Has_Complex_Representation (Rec) then
5391 if Present (SSO_ADC) then
5392 Error_Msg_N
5393 ("??storage order has no effect with Complex_Representation",
5394 SSO_ADC);
5395 end if;
5397 else
5398 -- Deal with default setting of reverse storage order
5400 Set_SSO_From_Default (Rec);
5402 -- Check consistent attribute setting on component types
5404 declare
5405 Comp_ADC_Present : Boolean;
5406 begin
5407 Comp := First_Component (Rec);
5408 while Present (Comp) loop
5409 Check_Component_Storage_Order
5410 (Encl_Type => Rec,
5411 Comp => Comp,
5412 ADC => SSO_ADC,
5413 Comp_ADC_Present => Comp_ADC_Present);
5414 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
5415 Next_Component (Comp);
5416 end loop;
5417 end;
5419 -- Now deal with reverse storage order/bit order issues
5421 if Present (SSO_ADC) then
5423 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
5424 -- if the former is specified.
5426 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
5428 -- Note: report error on Rec, not on SSO_ADC, as ADC may
5429 -- apply to some ancestor type.
5431 Error_Msg_Sloc := Sloc (SSO_ADC);
5432 Error_Msg_N
5433 ("scalar storage order for& specified# inconsistent with "
5434 & "bit order", Rec);
5435 end if;
5437 -- Warn if there is a Scalar_Storage_Order attribute definition
5438 -- clause but no component clause, no component that itself has
5439 -- such an attribute definition, and no pragma Pack.
5441 if not (Placed_Component
5442 or else
5443 SSO_ADC_Component
5444 or else
5445 Is_Packed (Rec))
5446 then
5447 Error_Msg_N
5448 ("??scalar storage order specified but no component "
5449 & "clause", SSO_ADC);
5450 end if;
5451 end if;
5452 end if;
5454 -- Deal with Bit_Order aspect
5456 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
5458 if Present (ADC) and then Base_Type (Rec) = Rec then
5459 if not (Placed_Component
5460 or else Present (SSO_ADC)
5461 or else Is_Packed (Rec))
5462 then
5463 -- Warn if clause has no effect when no component clause is
5464 -- present, but suppress warning if the Bit_Order is required
5465 -- due to the presence of a Scalar_Storage_Order attribute.
5467 Error_Msg_N
5468 ("??bit order specification has no effect", ADC);
5469 Error_Msg_N
5470 ("\??since no component clauses were specified", ADC);
5472 -- Here is where we do the processing to adjust component clauses
5473 -- for reversed bit order, when not using reverse SSO. If an error
5474 -- has been reported on Rec already (such as SSO incompatible with
5475 -- bit order), don't bother adjusting as this may generate extra
5476 -- noise.
5478 elsif Reverse_Bit_Order (Rec)
5479 and then not Reverse_Storage_Order (Rec)
5480 and then not Error_Posted (Rec)
5481 then
5482 Adjust_Record_For_Reverse_Bit_Order (Rec);
5484 -- Case where we have both an explicit Bit_Order and the same
5485 -- Scalar_Storage_Order: leave record untouched, the back-end
5486 -- will take care of required layout conversions.
5488 else
5489 null;
5491 end if;
5492 end if;
5494 -- Check for useless pragma Pack when all components placed. We only
5495 -- do this check for record types, not subtypes, since a subtype may
5496 -- have all its components placed, and it still makes perfectly good
5497 -- sense to pack other subtypes or the parent type. We do not give
5498 -- this warning if Optimize_Alignment is set to Space, since the
5499 -- pragma Pack does have an effect in this case (it always resets
5500 -- the alignment to one).
5502 if Ekind (Rec) = E_Record_Type
5503 and then Is_Packed (Rec)
5504 and then not Unplaced_Component
5505 and then Optimize_Alignment /= 'S'
5506 then
5507 -- Reset packed status. Probably not necessary, but we do it so
5508 -- that there is no chance of the back end doing something strange
5509 -- with this redundant indication of packing.
5511 Set_Is_Packed (Rec, False);
5513 -- Give warning if redundant constructs warnings on
5515 if Warn_On_Redundant_Constructs then
5516 Error_Msg_N -- CODEFIX
5517 ("?r?pragma Pack has no effect, no unplaced components",
5518 Get_Rep_Pragma (Rec, Name_Pack));
5519 end if;
5520 end if;
5522 -- If this is the record corresponding to a remote type, freeze the
5523 -- remote type here since that is what we are semantically freezing.
5524 -- This prevents the freeze node for that type in an inner scope.
5526 if Ekind (Rec) = E_Record_Type then
5527 if Present (Corresponding_Remote_Type (Rec)) then
5528 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
5529 end if;
5531 -- Check for controlled components, unchecked unions, and type
5532 -- invariants.
5534 Comp := First_Component (Rec);
5535 while Present (Comp) loop
5537 -- Do not set Has_Controlled_Component on a class-wide
5538 -- equivalent type. See Make_CW_Equivalent_Type.
5540 if not Is_Class_Wide_Equivalent_Type (Rec)
5541 and then
5542 (Has_Controlled_Component (Etype (Comp))
5543 or else
5544 (Chars (Comp) /= Name_uParent
5545 and then Is_Controlled (Etype (Comp)))
5546 or else
5547 (Is_Protected_Type (Etype (Comp))
5548 and then
5549 Present (Corresponding_Record_Type (Etype (Comp)))
5550 and then
5551 Has_Controlled_Component
5552 (Corresponding_Record_Type (Etype (Comp)))))
5553 then
5554 Set_Has_Controlled_Component (Rec);
5555 end if;
5557 if Has_Unchecked_Union (Etype (Comp)) then
5558 Set_Has_Unchecked_Union (Rec);
5559 end if;
5561 -- The record type requires its own invariant procedure in
5562 -- order to verify the invariant of each individual component.
5563 -- Do not consider internal components such as _parent because
5564 -- parent class-wide invariants are always inherited.
5565 -- In GNATprove mode, the component invariants are checked by
5566 -- other means. They should not be added to the record type
5567 -- invariant procedure, so that the procedure can be used to
5568 -- check the recordy type invariants if any.
5570 if Comes_From_Source (Comp)
5571 and then Has_Invariants (Etype (Comp))
5572 and then not GNATprove_Mode
5573 then
5574 Set_Has_Own_Invariants (Rec);
5575 end if;
5577 -- Scan component declaration for likely misuses of current
5578 -- instance, either in a constraint or a default expression.
5580 if Has_Per_Object_Constraint (Comp) then
5581 Check_Current_Instance (Parent (Comp));
5582 end if;
5584 Next_Component (Comp);
5585 end loop;
5586 end if;
5588 -- Enforce the restriction that access attributes with a current
5589 -- instance prefix can only apply to limited types. This comment
5590 -- is floating here, but does not seem to belong here???
5592 -- Set component alignment if not otherwise already set
5594 Set_Component_Alignment_If_Not_Set (Rec);
5596 -- For first subtypes, check if there are any fixed-point fields with
5597 -- component clauses, where we must check the size. This is not done
5598 -- till the freeze point since for fixed-point types, we do not know
5599 -- the size until the type is frozen. Similar processing applies to
5600 -- bit-packed arrays.
5602 if Is_First_Subtype (Rec) then
5603 Comp := First_Component (Rec);
5604 while Present (Comp) loop
5605 if Present (Component_Clause (Comp))
5606 and then (Is_Fixed_Point_Type (Etype (Comp))
5607 or else Is_Bit_Packed_Array (Etype (Comp)))
5608 then
5609 Check_Size
5610 (Component_Name (Component_Clause (Comp)),
5611 Etype (Comp),
5612 Esize (Comp),
5613 Junk);
5614 end if;
5616 Next_Component (Comp);
5617 end loop;
5618 end if;
5620 -- See if Size is too small as is (and implicit packing might help)
5622 if not Is_Packed (Rec)
5624 -- No implicit packing if even one component is explicitly placed
5626 and then not Placed_Component
5628 -- Or even one component is aliased
5630 and then not Aliased_Component
5632 -- Must have size clause and all sized components
5634 and then Has_Size_Clause (Rec)
5635 and then All_Sized_Components
5637 -- Do not try implicit packing on records with discriminants, too
5638 -- complicated, especially in the variant record case.
5640 and then not Has_Discriminants (Rec)
5642 -- We want to implicitly pack if the specified size of the record
5643 -- is less than the sum of the object sizes (no point in packing
5644 -- if this is not the case), if we can compute it, i.e. if we have
5645 -- only elementary components. Otherwise, we have at least one
5646 -- composite component and we want to implicitly pack only if bit
5647 -- packing is required for it, as we are sure in this case that
5648 -- the back end cannot do the expected layout without packing.
5650 and then
5651 ((All_Elem_Components
5652 and then RM_Size (Rec) < Elem_Component_Total_Esize)
5653 or else
5654 (not All_Elem_Components
5655 and then not All_Storage_Unit_Components
5656 and then RM_Size (Rec) < Sized_Component_Total_Round_RM_Size))
5658 -- And the total RM size cannot be greater than the specified size
5659 -- since otherwise packing will not get us where we have to be.
5661 and then Sized_Component_Total_RM_Size <= RM_Size (Rec)
5663 -- Never do implicit packing in CodePeer or SPARK modes since
5664 -- we don't do any packing in these modes, since this generates
5665 -- over-complex code that confuses static analysis, and in
5666 -- general, neither CodePeer not GNATprove care about the
5667 -- internal representation of objects.
5669 and then not (CodePeer_Mode or GNATprove_Mode)
5670 then
5671 -- If implicit packing enabled, do it
5673 if Implicit_Packing then
5674 Set_Is_Packed (Rec);
5676 -- Otherwise flag the size clause
5678 else
5679 declare
5680 Sz : constant Node_Id := Size_Clause (Rec);
5681 begin
5682 Error_Msg_NE -- CODEFIX
5683 ("size given for& too small", Sz, Rec);
5684 Error_Msg_N -- CODEFIX
5685 ("\use explicit pragma Pack "
5686 & "or use pragma Implicit_Packing", Sz);
5687 end;
5688 end if;
5689 end if;
5691 -- The following checks are relevant only when SPARK_Mode is on as
5692 -- they are not standard Ada legality rules.
5694 if SPARK_Mode = On then
5696 -- A discriminated type cannot be effectively volatile
5697 -- (SPARK RM 7.1.3(5)).
5699 if Is_Effectively_Volatile (Rec) then
5700 if Has_Discriminants (Rec) then
5701 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
5702 end if;
5704 -- A non-effectively volatile record type cannot contain
5705 -- effectively volatile components (SPARK RM 7.1.3(6)).
5707 else
5708 Comp := First_Component (Rec);
5709 while Present (Comp) loop
5710 if Comes_From_Source (Comp)
5711 and then Is_Effectively_Volatile (Etype (Comp))
5712 then
5713 Error_Msg_Name_1 := Chars (Rec);
5714 Error_Msg_N
5715 ("component & of non-volatile type % cannot be "
5716 & "volatile", Comp);
5717 end if;
5719 Next_Component (Comp);
5720 end loop;
5721 end if;
5723 -- A type which does not yield a synchronized object cannot have
5724 -- a component that yields a synchronized object (SPARK RM 9.5).
5726 if not Yields_Synchronized_Object (Rec) then
5727 Comp := First_Component (Rec);
5728 while Present (Comp) loop
5729 if Comes_From_Source (Comp)
5730 and then Yields_Synchronized_Object (Etype (Comp))
5731 then
5732 Error_Msg_Name_1 := Chars (Rec);
5733 Error_Msg_N
5734 ("component & of non-synchronized type % cannot be "
5735 & "synchronized", Comp);
5736 end if;
5738 Next_Component (Comp);
5739 end loop;
5740 end if;
5742 -- A Ghost type cannot have a component of protected or task type
5743 -- (SPARK RM 6.9(19)).
5745 if Is_Ghost_Entity (Rec) then
5746 Comp := First_Component (Rec);
5747 while Present (Comp) loop
5748 if Comes_From_Source (Comp)
5749 and then Is_Concurrent_Type (Etype (Comp))
5750 then
5751 Error_Msg_Name_1 := Chars (Rec);
5752 Error_Msg_N
5753 ("component & of ghost type % cannot be concurrent",
5754 Comp);
5755 end if;
5757 Next_Component (Comp);
5758 end loop;
5759 end if;
5760 end if;
5762 -- Make sure that if we have an iterator aspect, then we have
5763 -- either Constant_Indexing or Variable_Indexing.
5765 declare
5766 Iterator_Aspect : Node_Id;
5768 begin
5769 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
5771 if No (Iterator_Aspect) then
5772 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
5773 end if;
5775 if Present (Iterator_Aspect) then
5776 if Has_Aspect (Rec, Aspect_Constant_Indexing)
5777 or else
5778 Has_Aspect (Rec, Aspect_Variable_Indexing)
5779 then
5780 null;
5781 else
5782 Error_Msg_N
5783 ("Iterator_Element requires indexing aspect",
5784 Iterator_Aspect);
5785 end if;
5786 end if;
5787 end;
5789 -- All done if not a full record definition
5791 if Ekind (Rec) /= E_Record_Type then
5792 return;
5793 end if;
5795 -- Finally we need to check the variant part to make sure that
5796 -- all types within choices are properly frozen as part of the
5797 -- freezing of the record type.
5799 Check_Variant_Part : declare
5800 D : constant Node_Id := Declaration_Node (Rec);
5801 T : Node_Id;
5802 C : Node_Id;
5804 begin
5805 -- Find component list
5807 C := Empty;
5809 if Nkind (D) = N_Full_Type_Declaration then
5810 T := Type_Definition (D);
5812 if Nkind (T) = N_Record_Definition then
5813 C := Component_List (T);
5815 elsif Nkind (T) = N_Derived_Type_Definition
5816 and then Present (Record_Extension_Part (T))
5817 then
5818 C := Component_List (Record_Extension_Part (T));
5819 end if;
5820 end if;
5822 -- Case of variant part present
5824 if Present (C) and then Present (Variant_Part (C)) then
5825 Freeze_Choices_In_Variant_Part (Variant_Part (C));
5826 end if;
5828 -- Note: we used to call Check_Choices here, but it is too early,
5829 -- since predicated subtypes are frozen here, but their freezing
5830 -- actions are in Analyze_Freeze_Entity, which has not been called
5831 -- yet for entities frozen within this procedure, so we moved that
5832 -- call to the Analyze_Freeze_Entity for the record type.
5834 end Check_Variant_Part;
5836 -- Check that all the primitives of an interface type are abstract
5837 -- or null procedures.
5839 if Is_Interface (Rec)
5840 and then not Error_Posted (Parent (Rec))
5841 then
5842 declare
5843 Elmt : Elmt_Id;
5844 Subp : Entity_Id;
5846 begin
5847 Elmt := First_Elmt (Primitive_Operations (Rec));
5848 while Present (Elmt) loop
5849 Subp := Node (Elmt);
5851 if not Is_Abstract_Subprogram (Subp)
5853 -- Avoid reporting the error on inherited primitives
5855 and then Comes_From_Source (Subp)
5856 then
5857 Error_Msg_Name_1 := Chars (Subp);
5859 if Ekind (Subp) = E_Procedure then
5860 if not Null_Present (Parent (Subp)) then
5861 Error_Msg_N
5862 ("interface procedure % must be abstract or null",
5863 Parent (Subp));
5864 end if;
5865 else
5866 Error_Msg_N
5867 ("interface function % must be abstract",
5868 Parent (Subp));
5869 end if;
5870 end if;
5872 Next_Elmt (Elmt);
5873 end loop;
5874 end;
5875 end if;
5877 -- For a derived tagged type, check whether inherited primitives
5878 -- might require a wrapper to handle class-wide conditions.
5880 if Is_Tagged_Type (Rec) and then Is_Derived_Type (Rec) then
5881 Check_Inherited_Conditions (Rec);
5882 end if;
5883 end Freeze_Record_Type;
5885 -------------------------------
5886 -- Has_Boolean_Aspect_Import --
5887 -------------------------------
5889 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
5890 Decl : constant Node_Id := Declaration_Node (E);
5891 Asp : Node_Id;
5892 Expr : Node_Id;
5894 begin
5895 if Has_Aspects (Decl) then
5896 Asp := First (Aspect_Specifications (Decl));
5897 while Present (Asp) loop
5898 Expr := Expression (Asp);
5900 -- The value of aspect Import is True when the expression is
5901 -- either missing or it is explicitly set to True.
5903 if Get_Aspect_Id (Asp) = Aspect_Import
5904 and then (No (Expr)
5905 or else (Compile_Time_Known_Value (Expr)
5906 and then Is_True (Expr_Value (Expr))))
5907 then
5908 return True;
5909 end if;
5911 Next (Asp);
5912 end loop;
5913 end if;
5915 return False;
5916 end Has_Boolean_Aspect_Import;
5918 -------------------------
5919 -- Inherit_Freeze_Node --
5920 -------------------------
5922 procedure Inherit_Freeze_Node
5923 (Fnod : Node_Id;
5924 Typ : Entity_Id)
5926 Typ_Fnod : constant Node_Id := Freeze_Node (Typ);
5928 begin
5929 Set_Freeze_Node (Typ, Fnod);
5930 Set_Entity (Fnod, Typ);
5932 -- The input type had an existing node. Propagate relevant attributes
5933 -- from the old freeze node to the inherited freeze node.
5935 -- ??? if both freeze nodes have attributes, would they differ?
5937 if Present (Typ_Fnod) then
5939 -- Attribute Access_Types_To_Process
5941 if Present (Access_Types_To_Process (Typ_Fnod))
5942 and then No (Access_Types_To_Process (Fnod))
5943 then
5944 Set_Access_Types_To_Process (Fnod,
5945 Access_Types_To_Process (Typ_Fnod));
5946 end if;
5948 -- Attribute Actions
5950 if Present (Actions (Typ_Fnod)) and then No (Actions (Fnod)) then
5951 Set_Actions (Fnod, Actions (Typ_Fnod));
5952 end if;
5954 -- Attribute First_Subtype_Link
5956 if Present (First_Subtype_Link (Typ_Fnod))
5957 and then No (First_Subtype_Link (Fnod))
5958 then
5959 Set_First_Subtype_Link (Fnod, First_Subtype_Link (Typ_Fnod));
5960 end if;
5962 -- Attribute TSS_Elist
5964 if Present (TSS_Elist (Typ_Fnod))
5965 and then No (TSS_Elist (Fnod))
5966 then
5967 Set_TSS_Elist (Fnod, TSS_Elist (Typ_Fnod));
5968 end if;
5969 end if;
5970 end Inherit_Freeze_Node;
5972 ------------------------------
5973 -- Wrap_Imported_Subprogram --
5974 ------------------------------
5976 -- The issue here is that our normal approach of checking preconditions
5977 -- and postconditions does not work for imported procedures, since we
5978 -- are not generating code for the body. To get around this we create
5979 -- a wrapper, as shown by the following example:
5981 -- procedure K (A : Integer);
5982 -- pragma Import (C, K);
5984 -- The spec is rewritten by removing the effects of pragma Import, but
5985 -- leaving the convention unchanged, as though the source had said:
5987 -- procedure K (A : Integer);
5988 -- pragma Convention (C, K);
5990 -- and we create a body, added to the entity K freeze actions, which
5991 -- looks like:
5993 -- procedure K (A : Integer) is
5994 -- procedure K (A : Integer);
5995 -- pragma Import (C, K);
5996 -- begin
5997 -- K (A);
5998 -- end K;
6000 -- Now the contract applies in the normal way to the outer procedure,
6001 -- and the inner procedure has no contracts, so there is no problem
6002 -- in just calling it to get the original effect.
6004 -- In the case of a function, we create an appropriate return statement
6005 -- for the subprogram body that calls the inner procedure.
6007 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
6008 function Copy_Import_Pragma return Node_Id;
6009 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
6011 ------------------------
6012 -- Copy_Import_Pragma --
6013 ------------------------
6015 function Copy_Import_Pragma return Node_Id is
6017 -- The subprogram should have an import pragma, otherwise it does
6018 -- need a wrapper.
6020 Prag : constant Node_Id := Import_Pragma (E);
6021 pragma Assert (Present (Prag));
6023 -- Save all semantic fields of the pragma
6025 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
6026 Save_From : constant Boolean := From_Aspect_Specification (Prag);
6027 Save_Prag : constant Node_Id := Next_Pragma (Prag);
6028 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
6030 Result : Node_Id;
6032 begin
6033 -- Reset all semantic fields. This avoids a potential infinite
6034 -- loop when the pragma comes from an aspect as the duplication
6035 -- will copy the aspect, then copy the corresponding pragma and
6036 -- so on.
6038 Set_Corresponding_Aspect (Prag, Empty);
6039 Set_From_Aspect_Specification (Prag, False);
6040 Set_Next_Pragma (Prag, Empty);
6041 Set_Next_Rep_Item (Prag, Empty);
6043 Result := Copy_Separate_Tree (Prag);
6045 -- Restore the original semantic fields
6047 Set_Corresponding_Aspect (Prag, Save_Asp);
6048 Set_From_Aspect_Specification (Prag, Save_From);
6049 Set_Next_Pragma (Prag, Save_Prag);
6050 Set_Next_Rep_Item (Prag, Save_Rep);
6052 return Result;
6053 end Copy_Import_Pragma;
6055 -- Local variables
6057 Loc : constant Source_Ptr := Sloc (E);
6058 CE : constant Name_Id := Chars (E);
6059 Bod : Node_Id;
6060 Forml : Entity_Id;
6061 Parms : List_Id;
6062 Prag : Node_Id;
6063 Spec : Node_Id;
6064 Stmt : Node_Id;
6066 -- Start of processing for Wrap_Imported_Subprogram
6068 begin
6069 -- Nothing to do if not imported
6071 if not Is_Imported (E) then
6072 return;
6074 -- Test enabling conditions for wrapping
6076 elsif Is_Subprogram (E)
6077 and then Present (Contract (E))
6078 and then Present (Pre_Post_Conditions (Contract (E)))
6079 and then not GNATprove_Mode
6080 then
6081 -- Here we do the wrap
6083 Prag := Copy_Import_Pragma;
6085 -- Fix up spec so it is no longer imported and has convention Ada
6087 Set_Has_Completion (E, False);
6088 Set_Import_Pragma (E, Empty);
6089 Set_Interface_Name (E, Empty);
6090 Set_Is_Imported (E, False);
6091 Set_Convention (E, Convention_Ada);
6093 -- Grab the subprogram declaration and specification
6095 Spec := Declaration_Node (E);
6097 -- Build parameter list that we need
6099 Parms := New_List;
6100 Forml := First_Formal (E);
6101 while Present (Forml) loop
6102 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
6103 Next_Formal (Forml);
6104 end loop;
6106 -- Build the call
6108 -- An imported function whose result type is anonymous access
6109 -- creates a new anonymous access type when it is relocated into
6110 -- the declarations of the body generated below. As a result, the
6111 -- accessibility level of these two anonymous access types may not
6112 -- be compatible even though they are essentially the same type.
6113 -- Use an unchecked type conversion to reconcile this case. Note
6114 -- that the conversion is safe because in the named access type
6115 -- case, both the body and imported function utilize the same
6116 -- type.
6118 if Ekind (E) in E_Function | E_Generic_Function then
6119 Stmt :=
6120 Make_Simple_Return_Statement (Loc,
6121 Expression =>
6122 Unchecked_Convert_To (Etype (E),
6123 Make_Function_Call (Loc,
6124 Name => Make_Identifier (Loc, CE),
6125 Parameter_Associations => Parms)));
6127 else
6128 Stmt :=
6129 Make_Procedure_Call_Statement (Loc,
6130 Name => Make_Identifier (Loc, CE),
6131 Parameter_Associations => Parms);
6132 end if;
6134 -- Now build the body
6136 Bod :=
6137 Make_Subprogram_Body (Loc,
6138 Specification => Copy_Subprogram_Spec (Spec),
6139 Declarations => New_List (
6140 Make_Subprogram_Declaration (Loc,
6141 Specification => Copy_Subprogram_Spec (Spec)),
6142 Prag),
6143 Handled_Statement_Sequence =>
6144 Make_Handled_Sequence_Of_Statements (Loc,
6145 Statements => New_List (Stmt),
6146 End_Label => Make_Identifier (Loc, CE)));
6148 -- Append the body to freeze result
6150 Add_To_Result (Bod);
6151 return;
6153 -- Case of imported subprogram that does not get wrapped
6155 else
6156 -- Set Is_Public. All imported entities need an external symbol
6157 -- created for them since they are always referenced from another
6158 -- object file. Note this used to be set when we set Is_Imported
6159 -- back in Sem_Prag, but now we delay it to this point, since we
6160 -- don't want to set this flag if we wrap an imported subprogram.
6162 Set_Is_Public (E);
6163 end if;
6164 end Wrap_Imported_Subprogram;
6166 -- Start of processing for Freeze_Entity
6168 begin
6169 -- The entity being frozen may be subject to pragma Ghost. Set the mode
6170 -- now to ensure that any nodes generated during freezing are properly
6171 -- flagged as Ghost.
6173 Set_Ghost_Mode (E);
6175 -- We are going to test for various reasons why this entity need not be
6176 -- frozen here, but in the case of an Itype that's defined within a
6177 -- record, that test actually applies to the record.
6179 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
6180 Test_E := Scope (E);
6182 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
6183 and then Is_Record_Type (Underlying_Type (Scope (E)))
6184 then
6185 Test_E := Underlying_Type (Scope (E));
6186 end if;
6188 -- Do not freeze if already frozen since we only need one freeze node
6190 if Is_Frozen (E) then
6192 if Is_Itype (E)
6193 and then not Is_Base_Type (E)
6194 and then not Is_Frozen (Etype (E))
6195 then
6196 -- If a frozen subtype of an unfrozen type seems impossible
6197 -- then see Analyze_Protected_Definition.Undelay_Itypes.
6199 Result := Freeze_Entity
6200 (Etype (E), N, Do_Freeze_Profile => Do_Freeze_Profile);
6201 else
6202 Result := No_List;
6203 end if;
6205 goto Leave;
6207 -- Do not freeze if we are preanalyzing without freezing
6209 elsif Inside_Preanalysis_Without_Freezing > 0 then
6210 Result := No_List;
6211 goto Leave;
6213 elsif Ekind (E) = E_Generic_Package then
6214 Result := Freeze_Generic_Entities (E);
6215 goto Leave;
6217 -- It is improper to freeze an external entity within a generic because
6218 -- its freeze node will appear in a non-valid context. The entity will
6219 -- be frozen in the proper scope after the current generic is analyzed.
6220 -- However, aspects must be analyzed because they may be queried later
6221 -- within the generic itself, and the corresponding pragma or attribute
6222 -- definition has not been analyzed yet. After this, indicate that the
6223 -- entity has no further delayed aspects, to prevent a later aspect
6224 -- analysis out of the scope of the generic.
6226 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
6227 if Has_Delayed_Aspects (E) then
6228 Analyze_Aspects_At_Freeze_Point (E);
6229 Set_Has_Delayed_Aspects (E, False);
6230 end if;
6232 Result := No_List;
6233 goto Leave;
6235 -- AI05-0213: A formal incomplete type does not freeze the actual. In
6236 -- the instance, the same applies to the subtype renaming the actual.
6238 elsif Is_Private_Type (E)
6239 and then Is_Generic_Actual_Type (E)
6240 and then No (Full_View (Base_Type (E)))
6241 and then Ada_Version >= Ada_2012
6242 then
6243 Result := No_List;
6244 goto Leave;
6246 -- Formal subprograms are never frozen
6248 elsif Is_Formal_Subprogram (E) then
6249 Result := No_List;
6250 goto Leave;
6252 -- Generic types are never frozen as they lack delayed semantic checks
6254 elsif Is_Generic_Type (E) then
6255 Result := No_List;
6256 goto Leave;
6258 -- Do not freeze a global entity within an inner scope created during
6259 -- expansion. A call to subprogram E within some internal procedure
6260 -- (a stream attribute for example) might require freezing E, but the
6261 -- freeze node must appear in the same declarative part as E itself.
6262 -- The two-pass elaboration mechanism in gigi guarantees that E will
6263 -- be frozen before the inner call is elaborated. We exclude constants
6264 -- from this test, because deferred constants may be frozen early, and
6265 -- must be diagnosed (e.g. in the case of a deferred constant being used
6266 -- in a default expression). If the enclosing subprogram comes from
6267 -- source, or is a generic instance, then the freeze point is the one
6268 -- mandated by the language, and we freeze the entity. A subprogram that
6269 -- is a child unit body that acts as a spec does not have a spec that
6270 -- comes from source, but can only come from source.
6272 elsif In_Open_Scopes (Scope (Test_E))
6273 and then Scope (Test_E) /= Current_Scope
6274 and then Ekind (Test_E) /= E_Constant
6275 then
6276 -- Here we deal with the special case of the expansion of
6277 -- postconditions. Previously this was handled by the loop below,
6278 -- since these postcondition checks got isolated to a separate,
6279 -- internally generated, subprogram. Now, however, the postcondition
6280 -- checks get contained within their corresponding subprogram
6281 -- directly.
6283 if not Comes_From_Source (N)
6284 and then Nkind (N) = N_Pragma
6285 and then From_Aspect_Specification (N)
6286 and then Is_Valid_Assertion_Kind (Original_Aspect_Pragma_Name (N))
6288 -- Now, verify the placement of the pragma is within an expanded
6289 -- subprogram which contains postcondition expansion - detected
6290 -- through the presence of the "Wrapped_Statements" field.
6292 and then Present (Enclosing_Subprogram (Current_Scope))
6293 and then Present (Wrapped_Statements
6294 (Enclosing_Subprogram (Current_Scope)))
6295 then
6296 goto Leave;
6297 end if;
6299 -- Otherwise, loop through scopes checking if an enclosing scope
6300 -- comes from source or is a generic. Note that, for the purpose
6301 -- of this test, we need to consider that the internally generated
6302 -- subprogram described above comes from source too if the original
6303 -- subprogram itself does.
6305 declare
6306 S : Entity_Id;
6308 begin
6309 S := Current_Scope;
6310 while Present (S) loop
6311 if Is_Overloadable (S) then
6312 if Comes_From_Source (S)
6313 or else (Chars (S) = Name_uWrapped_Statements
6314 and then Comes_From_Source (Scope (S)))
6315 or else Is_Generic_Instance (S)
6316 or else Is_Child_Unit (S)
6317 then
6318 exit;
6319 else
6320 Result := No_List;
6321 goto Leave;
6322 end if;
6323 end if;
6325 S := Scope (S);
6326 end loop;
6327 end;
6329 -- Similarly, an inlined instance body may make reference to global
6330 -- entities, but these references cannot be the proper freezing point
6331 -- for them, and in the absence of inlining freezing will take place in
6332 -- their own scope. Normally instance bodies are analyzed after the
6333 -- enclosing compilation, and everything has been frozen at the proper
6334 -- place, but with front-end inlining an instance body is compiled
6335 -- before the end of the enclosing scope, and as a result out-of-order
6336 -- freezing must be prevented.
6338 elsif Front_End_Inlining
6339 and then In_Instance_Body
6340 and then Present (Scope (Test_E))
6341 then
6342 declare
6343 S : Entity_Id;
6345 begin
6346 S := Scope (Test_E);
6347 while Present (S) loop
6348 if Is_Generic_Instance (S) then
6349 exit;
6350 else
6351 S := Scope (S);
6352 end if;
6353 end loop;
6355 if No (S) then
6356 Result := No_List;
6357 goto Leave;
6358 end if;
6359 end;
6360 end if;
6362 -- Add checks to detect proper initialization of scalars that may appear
6363 -- as subprogram parameters.
6365 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
6366 Apply_Parameter_Validity_Checks (E);
6367 end if;
6369 -- Deal with delayed aspect specifications. The analysis of the aspect
6370 -- is required to be delayed to the freeze point, thus we analyze the
6371 -- pragma or attribute definition clause in the tree at this point. We
6372 -- also analyze the aspect specification node at the freeze point when
6373 -- the aspect doesn't correspond to pragma/attribute definition clause.
6374 -- In addition, a derived type may have inherited aspects that were
6375 -- delayed in the parent, so these must also be captured now.
6377 -- For a record type, we deal with the delayed aspect specifications on
6378 -- components first, which is consistent with the non-delayed case and
6379 -- makes it possible to have a single processing to detect conflicts.
6381 if Is_Record_Type (E) then
6382 declare
6383 Comp : Entity_Id;
6385 Rec_Pushed : Boolean := False;
6386 -- Set True if the record type E has been pushed on the scope
6387 -- stack. Needed for the analysis of delayed aspects specified
6388 -- to the components of Rec.
6390 begin
6391 Comp := First_Component (E);
6392 while Present (Comp) loop
6393 if Has_Delayed_Aspects (Comp) then
6394 if not Rec_Pushed then
6395 Push_Scope (E);
6396 Rec_Pushed := True;
6398 -- The visibility to the discriminants must be restored
6399 -- in order to properly analyze the aspects.
6401 if Has_Discriminants (E) then
6402 Install_Discriminants (E);
6403 end if;
6404 end if;
6406 Analyze_Aspects_At_Freeze_Point (Comp);
6407 end if;
6409 Next_Component (Comp);
6410 end loop;
6412 -- Pop the scope if Rec scope has been pushed on the scope stack
6413 -- during the delayed aspect analysis process.
6415 if Rec_Pushed then
6416 if Has_Discriminants (E) then
6417 Uninstall_Discriminants (E);
6418 end if;
6420 Pop_Scope;
6421 end if;
6422 end;
6423 end if;
6425 if Has_Delayed_Aspects (E) then
6426 Analyze_Aspects_At_Freeze_Point (E);
6427 end if;
6429 -- Here to freeze the entity
6431 Set_Is_Frozen (E);
6433 -- Case of entity being frozen is other than a type
6435 if not Is_Type (E) then
6437 -- If entity is exported or imported and does not have an external
6438 -- name, now is the time to provide the appropriate default name.
6439 -- Skip this if the entity is stubbed, since we don't need a name
6440 -- for any stubbed routine. For the case on intrinsics, if no
6441 -- external name is specified, then calls will be handled in
6442 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
6443 -- external name is provided, then Expand_Intrinsic_Call leaves
6444 -- calls in place for expansion by GIGI.
6446 if (Is_Imported (E) or else Is_Exported (E))
6447 and then No (Interface_Name (E))
6448 and then Convention (E) /= Convention_Stubbed
6449 and then Convention (E) /= Convention_Intrinsic
6450 then
6451 Set_Encoded_Interface_Name
6452 (E, Get_Default_External_Name (E));
6453 end if;
6455 -- Subprogram case
6457 if Is_Subprogram (E) then
6459 -- Check for needing to wrap imported subprogram
6461 if not Inside_A_Generic then
6462 Wrap_Imported_Subprogram (E);
6463 end if;
6465 -- Freeze all parameter types and the return type (RM 13.14(14)).
6466 -- However skip this for internal subprograms. This is also where
6467 -- any extra formal parameters are created since we now know
6468 -- whether the subprogram will use a foreign convention.
6470 -- In Ada 2012, freezing a subprogram does not always freeze the
6471 -- corresponding profile (see AI05-019). An attribute reference
6472 -- is not a freezing point of the profile. Similarly, we do not
6473 -- freeze the profile of primitives of a library-level tagged type
6474 -- when we are building its dispatch table. Flag Do_Freeze_Profile
6475 -- indicates whether the profile should be frozen now.
6477 -- This processing doesn't apply to internal entities (see below)
6479 if not Is_Internal (E) and then Do_Freeze_Profile then
6480 if not Freeze_Profile (E) then
6481 goto Leave;
6482 end if;
6483 end if;
6485 -- Must freeze its parent first if it is a derived subprogram
6487 if Present (Alias (E)) then
6488 Freeze_And_Append (Alias (E), N, Result);
6489 end if;
6491 -- We don't freeze internal subprograms, because we don't normally
6492 -- want addition of extra formals or mechanism setting to happen
6493 -- for those. However we do pass through predefined dispatching
6494 -- cases, since extra formals may be needed in some cases, such as
6495 -- for the stream 'Input function (build-in-place formals).
6497 if not Is_Internal (E)
6498 or else Is_Predefined_Dispatching_Operation (E)
6499 then
6500 Freeze_Subprogram (E);
6501 end if;
6503 -- If warning on suspicious contracts then check for the case of
6504 -- a postcondition other than False for a No_Return subprogram.
6506 if No_Return (E)
6507 and then Warn_On_Suspicious_Contract
6508 and then Present (Contract (E))
6509 then
6510 declare
6511 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
6512 Exp : Node_Id;
6514 begin
6515 while Present (Prag) loop
6516 if Pragma_Name_Unmapped (Prag) in Name_Post
6517 | Name_Postcondition
6518 | Name_Refined_Post
6519 then
6520 Exp :=
6521 Expression
6522 (First (Pragma_Argument_Associations (Prag)));
6524 if Nkind (Exp) /= N_Identifier
6525 or else Chars (Exp) /= Name_False
6526 then
6527 Error_Msg_NE
6528 ("useless postcondition, & is marked "
6529 & "No_Return?.t?", Exp, E);
6530 end if;
6531 end if;
6533 Prag := Next_Pragma (Prag);
6534 end loop;
6535 end;
6536 end if;
6538 -- Here for other than a subprogram or type
6540 else
6541 -- If entity has a type declared in the current scope, and it is
6542 -- not a generic unit, then freeze it first.
6544 if Present (Etype (E))
6545 and then Ekind (E) /= E_Generic_Function
6546 and then Within_Scope (Etype (E), Current_Scope)
6547 then
6548 Freeze_And_Append (Etype (E), N, Result);
6550 -- For an object of an anonymous array type, aspects on the
6551 -- object declaration apply to the type itself. This is the
6552 -- case for Atomic_Components, Volatile_Components, and
6553 -- Independent_Components. In these cases analysis of the
6554 -- generated pragma will mark the anonymous types accordingly,
6555 -- and the object itself does not require a freeze node.
6557 if Ekind (E) = E_Variable
6558 and then Is_Itype (Etype (E))
6559 and then Is_Array_Type (Etype (E))
6560 and then Has_Delayed_Aspects (E)
6561 then
6562 Set_Has_Delayed_Aspects (E, False);
6563 Set_Has_Delayed_Freeze (E, False);
6564 Set_Freeze_Node (E, Empty);
6565 end if;
6566 end if;
6568 -- Special processing for objects created by object declaration;
6569 -- we protect the call to Declaration_Node against entities of
6570 -- expressions replaced by the frontend with an N_Raise_CE node.
6572 if Ekind (E) in E_Constant | E_Variable
6573 and then Nkind (Declaration_Node (E)) = N_Object_Declaration
6574 then
6575 Freeze_Object_Declaration (E);
6576 end if;
6578 -- Check that a constant which has a pragma Volatile[_Components]
6579 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
6581 -- Note: Atomic[_Components] also sets Volatile[_Components]
6583 if Ekind (E) = E_Constant
6584 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
6585 and then not Is_Imported (E)
6586 and then not Has_Boolean_Aspect_Import (E)
6587 then
6588 -- Make sure we actually have a pragma, and have not merely
6589 -- inherited the indication from elsewhere (e.g. an address
6590 -- clause, which is not good enough in RM terms).
6592 if Has_Rep_Pragma (E, Name_Atomic)
6593 or else
6594 Has_Rep_Pragma (E, Name_Atomic_Components)
6595 then
6596 Error_Msg_N
6597 ("standalone atomic constant must be " &
6598 "imported (RM C.6(13))", E);
6600 elsif Has_Rep_Pragma (E, Name_Volatile)
6601 or else
6602 Has_Rep_Pragma (E, Name_Volatile_Components)
6603 then
6604 Error_Msg_N
6605 ("standalone volatile constant must be " &
6606 "imported (RM C.6(13))", E);
6607 end if;
6608 end if;
6610 -- Static objects require special handling
6612 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
6613 and then Is_Statically_Allocated (E)
6614 then
6615 Freeze_Static_Object (E);
6616 end if;
6618 -- Remaining step is to layout objects
6620 if Ekind (E) in E_Variable | E_Constant | E_Loop_Parameter
6621 or else Is_Formal (E)
6622 then
6623 Layout_Object (E);
6624 end if;
6626 -- For an object that does not have delayed freezing, and whose
6627 -- initialization actions have been captured in a compound
6628 -- statement, move them back now directly within the enclosing
6629 -- statement sequence.
6631 if Ekind (E) in E_Constant | E_Variable
6632 and then not Has_Delayed_Freeze (E)
6633 then
6634 Explode_Initialization_Compound_Statement (E);
6635 end if;
6637 -- Do not generate a freeze node for a generic unit
6639 if Is_Generic_Unit (E) then
6640 Result := No_List;
6641 goto Leave;
6642 end if;
6643 end if;
6645 -- Case of a type or subtype being frozen
6647 else
6648 -- Verify several SPARK legality rules related to Ghost types now
6649 -- that the type is frozen.
6651 Check_Ghost_Type (E);
6653 -- We used to check here that a full type must have preelaborable
6654 -- initialization if it completes a private type specified with
6655 -- pragma Preelaborable_Initialization, but that missed cases where
6656 -- the types occur within a generic package, since the freezing
6657 -- that occurs within a containing scope generally skips traversal
6658 -- of a generic unit's declarations (those will be frozen within
6659 -- instances). This check was moved to Analyze_Package_Specification.
6661 -- The type may be defined in a generic unit. This can occur when
6662 -- freezing a generic function that returns the type (which is
6663 -- defined in a parent unit). It is clearly meaningless to freeze
6664 -- this type. However, if it is a subtype, its size may be determi-
6665 -- nable and used in subsequent checks, so might as well try to
6666 -- compute it.
6668 -- In Ada 2012, Freeze_Entities is also used in the front end to
6669 -- trigger the analysis of aspect expressions, so in this case we
6670 -- want to continue the freezing process.
6672 -- Is_Generic_Unit (Scope (E)) is dubious here, do we want instead
6673 -- In_Generic_Scope (E)???
6675 if Present (Scope (E))
6676 and then Is_Generic_Unit (Scope (E))
6677 and then
6678 (not Has_Predicates (E)
6679 and then not Has_Delayed_Freeze (E))
6680 then
6681 Check_Compile_Time_Size (E);
6682 Result := No_List;
6683 goto Leave;
6684 end if;
6686 -- Check for error of Type_Invariant'Class applied to an untagged
6687 -- type (check delayed to freeze time when full type is available).
6689 declare
6690 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
6691 begin
6692 if Present (Prag)
6693 and then Class_Present (Prag)
6694 and then not Is_Tagged_Type (E)
6695 then
6696 Error_Msg_NE
6697 ("Type_Invariant''Class cannot be specified for &", Prag, E);
6698 Error_Msg_N
6699 ("\can only be specified for a tagged type", Prag);
6700 end if;
6701 end;
6703 -- Deal with special cases of freezing for subtype
6705 if E /= Base_Type (E) then
6707 -- Before we do anything else, a specific test for the case of a
6708 -- size given for an array where the array would need to be packed
6709 -- in order for the size to be honored, but is not. This is the
6710 -- case where implicit packing may apply. The reason we do this so
6711 -- early is that, if we have implicit packing, the layout of the
6712 -- base type is affected, so we must do this before we freeze the
6713 -- base type.
6715 -- We could do this processing only if implicit packing is enabled
6716 -- since in all other cases, the error would be caught by the back
6717 -- end. However, we choose to do the check even if we do not have
6718 -- implicit packing enabled, since this allows us to give a more
6719 -- useful error message (advising use of pragma Implicit_Packing
6720 -- or pragma Pack).
6722 if Is_Array_Type (E) then
6723 declare
6724 Ctyp : constant Entity_Id := Component_Type (E);
6725 Rsiz : constant Uint :=
6726 (if Known_RM_Size (Ctyp) then RM_Size (Ctyp) else Uint_0);
6727 SZ : constant Node_Id := Size_Clause (E);
6728 Btyp : constant Entity_Id := Base_Type (E);
6730 Lo : Node_Id;
6731 Hi : Node_Id;
6732 Indx : Node_Id;
6734 Dim : Uint;
6735 Num_Elmts : Uint := Uint_1;
6736 -- Number of elements in array
6738 begin
6739 -- Check enabling conditions. These are straightforward
6740 -- except for the test for a limited composite type. This
6741 -- eliminates the rare case of a array of limited components
6742 -- where there are issues of whether or not we can go ahead
6743 -- and pack the array (since we can't freely pack and unpack
6744 -- arrays if they are limited).
6746 -- Note that we check the root type explicitly because the
6747 -- whole point is we are doing this test before we have had
6748 -- a chance to freeze the base type (and it is that freeze
6749 -- action that causes stuff to be inherited).
6751 -- The conditions on the size are identical to those used in
6752 -- Freeze_Array_Type to set the Is_Packed flag.
6754 if Has_Size_Clause (E)
6755 and then Known_Static_RM_Size (E)
6756 and then not Is_Packed (E)
6757 and then not Has_Pragma_Pack (E)
6758 and then not Has_Component_Size_Clause (E)
6759 and then Known_Static_RM_Size (Ctyp)
6760 and then Rsiz <= System_Max_Integer_Size
6761 and then not (Addressable (Rsiz)
6762 and then Known_Static_Esize (Ctyp)
6763 and then Esize (Ctyp) = Rsiz)
6764 and then not (Rsiz mod System_Storage_Unit = 0
6765 and then Is_Composite_Type (Ctyp))
6766 and then not Is_Limited_Composite (E)
6767 and then not Is_Packed (Root_Type (E))
6768 and then not Has_Component_Size_Clause (Root_Type (E))
6769 and then not (CodePeer_Mode or GNATprove_Mode)
6770 then
6771 -- Compute number of elements in array
6773 Indx := First_Index (E);
6774 while Present (Indx) loop
6775 Get_Index_Bounds (Indx, Lo, Hi);
6777 if not (Compile_Time_Known_Value (Lo)
6778 and then
6779 Compile_Time_Known_Value (Hi))
6780 then
6781 goto No_Implicit_Packing;
6782 end if;
6784 Dim := Expr_Value (Hi) - Expr_Value (Lo) + 1;
6786 if Dim > Uint_0 then
6787 Num_Elmts := Num_Elmts * Dim;
6788 else
6789 Num_Elmts := Uint_0;
6790 end if;
6792 Next_Index (Indx);
6793 end loop;
6795 -- What we are looking for here is the situation where
6796 -- the RM_Size given would be exactly right if there was
6797 -- a pragma Pack, resulting in the component size being
6798 -- the RM_Size of the component type.
6800 if RM_Size (E) = Num_Elmts * Rsiz then
6802 -- For implicit packing mode, just set the component
6803 -- size and Freeze_Array_Type will do the rest.
6805 if Implicit_Packing then
6806 Set_Component_Size (Btyp, Rsiz);
6808 -- Otherwise give an error message, except that if the
6809 -- specified Size is zero, there is no need for pragma
6810 -- Pack. Note that size zero is not considered
6811 -- Addressable.
6813 elsif RM_Size (E) /= Uint_0 then
6814 Error_Msg_NE
6815 ("size given for& too small", SZ, E);
6816 Error_Msg_N -- CODEFIX
6817 ("\use explicit pragma Pack or use pragma "
6818 & "Implicit_Packing", SZ);
6819 end if;
6820 end if;
6821 end if;
6822 end;
6823 end if;
6825 <<No_Implicit_Packing>>
6827 -- If ancestor subtype present, freeze that first. Note that this
6828 -- will also get the base type frozen. Need RM reference ???
6830 Atype := Ancestor_Subtype (E);
6832 if Present (Atype) then
6833 Freeze_And_Append (Atype, N, Result);
6835 -- No ancestor subtype present
6837 else
6838 -- See if we have a nearest ancestor that has a predicate.
6839 -- That catches the case of derived type with a predicate.
6840 -- Need RM reference here ???
6842 Atype := Nearest_Ancestor (E);
6844 if Present (Atype) and then Has_Predicates (Atype) then
6845 Freeze_And_Append (Atype, N, Result);
6846 end if;
6848 -- Freeze base type before freezing the entity (RM 13.14(15))
6850 if E /= Base_Type (E) then
6851 Freeze_And_Append (Base_Type (E), N, Result);
6852 end if;
6853 end if;
6855 -- A subtype inherits all the type-related representation aspects
6856 -- from its parents (RM 13.1(8)).
6858 if May_Inherit_Delayed_Rep_Aspects (E) then
6859 Inherit_Delayed_Rep_Aspects (E);
6860 end if;
6862 Inherit_Aspects_At_Freeze_Point (E);
6864 -- For a derived type, freeze its parent type first (RM 13.14(15))
6866 elsif Is_Derived_Type (E) then
6867 Freeze_And_Append (Etype (E), N, Result);
6869 -- A derived type inherits each type-related representation aspect
6870 -- of its parent type that was directly specified before the
6871 -- declaration of the derived type (RM 13.1(15)).
6873 if May_Inherit_Delayed_Rep_Aspects (E) then
6874 Inherit_Delayed_Rep_Aspects (E);
6875 end if;
6877 Inherit_Aspects_At_Freeze_Point (E);
6878 end if;
6880 -- Case of array type
6882 if Is_Array_Type (E) then
6883 Freeze_Array_Type (E);
6884 end if;
6886 -- Check for incompatible size and alignment for array/record type
6888 if Warn_On_Size_Alignment
6889 and then (Is_Array_Type (E) or else Is_Record_Type (E))
6890 and then Has_Size_Clause (E)
6891 and then Has_Alignment_Clause (E)
6893 -- If explicit Object_Size clause given assume that the programmer
6894 -- knows what he is doing, and expects the compiler behavior.
6896 and then not Has_Object_Size_Clause (E)
6898 -- It does not really make sense to warn for the minimum alignment
6899 -- since the programmer could not get rid of the warning.
6901 and then Alignment (E) > 1
6903 -- Check for size not a multiple of alignment
6905 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
6906 then
6907 declare
6908 SC : constant Node_Id := Size_Clause (E);
6909 AC : constant Node_Id := Alignment_Clause (E);
6910 Loc : Node_Id;
6911 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
6913 begin
6914 if Present (SC) and then Present (AC) then
6916 -- Give a warning
6918 if Sloc (SC) > Sloc (AC) then
6919 Loc := SC;
6920 Error_Msg_NE
6921 ("?.z?size is not a multiple of alignment for &",
6922 Loc, E);
6923 Error_Msg_Sloc := Sloc (AC);
6924 Error_Msg_Uint_1 := Alignment (E);
6925 Error_Msg_N ("\?.z?alignment of ^ specified #", Loc);
6927 else
6928 Loc := AC;
6929 Error_Msg_NE
6930 ("?.z?size is not a multiple of alignment for &",
6931 Loc, E);
6932 Error_Msg_Sloc := Sloc (SC);
6933 Error_Msg_Uint_1 := RM_Size (E);
6934 Error_Msg_N ("\?.z?size of ^ specified #", Loc);
6935 end if;
6937 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
6938 Error_Msg_N ("\?.z?Object_Size will be increased to ^", Loc);
6939 end if;
6940 end;
6941 end if;
6943 -- For a class-wide type, the corresponding specific type is
6944 -- frozen as well (RM 13.14(15))
6946 if Is_Class_Wide_Type (E) then
6947 Freeze_And_Append (Root_Type (E), N, Result);
6949 -- If the base type of the class-wide type is still incomplete,
6950 -- the class-wide remains unfrozen as well. This is legal when
6951 -- E is the formal of a primitive operation of some other type
6952 -- which is being frozen.
6954 if not Is_Frozen (Root_Type (E)) then
6955 Set_Is_Frozen (E, False);
6956 goto Leave;
6957 end if;
6959 -- The equivalent type associated with a class-wide subtype needs
6960 -- to be frozen to ensure that its layout is done.
6962 if Ekind (E) = E_Class_Wide_Subtype
6963 and then Present (Equivalent_Type (E))
6964 then
6965 Freeze_And_Append (Equivalent_Type (E), N, Result);
6966 end if;
6968 -- Generate an itype reference for a library-level class-wide type
6969 -- at the freeze point. Otherwise the first explicit reference to
6970 -- the type may appear in an inner scope which will be rejected by
6971 -- the back-end.
6973 if Is_Itype (E)
6974 and then Is_Compilation_Unit (Scope (E))
6975 then
6976 declare
6977 Ref : constant Node_Id := Make_Itype_Reference (Loc);
6979 begin
6980 Set_Itype (Ref, E);
6982 -- From a gigi point of view, a class-wide subtype derives
6983 -- from its record equivalent type. As a result, the itype
6984 -- reference must appear after the freeze node of the
6985 -- equivalent type or gigi will reject the reference.
6987 if Ekind (E) = E_Class_Wide_Subtype
6988 and then Present (Equivalent_Type (E))
6989 then
6990 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
6991 else
6992 Add_To_Result (Ref);
6993 end if;
6994 end;
6995 end if;
6997 -- For a record type or record subtype, freeze all component types
6998 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
6999 -- using Is_Record_Type, because we don't want to attempt the freeze
7000 -- for the case of a private type with record extension (we will do
7001 -- that later when the full type is frozen).
7003 elsif Ekind (E) in E_Record_Type | E_Record_Subtype then
7004 if not In_Generic_Scope (E) then
7005 Freeze_Record_Type (E);
7006 end if;
7008 -- Report a warning if a discriminated record base type has a
7009 -- convention with language C or C++ applied to it. This check is
7010 -- done even within generic scopes (but not in instantiations),
7011 -- which is why we don't do it as part of Freeze_Record_Type.
7013 Check_Suspicious_Convention (E);
7015 -- For a concurrent type, freeze corresponding record type. This does
7016 -- not correspond to any specific rule in the RM, but the record type
7017 -- is essentially part of the concurrent type. Also freeze all local
7018 -- entities. This includes record types created for entry parameter
7019 -- blocks and whatever local entities may appear in the private part.
7021 elsif Is_Concurrent_Type (E) then
7022 if Present (Corresponding_Record_Type (E)) then
7023 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
7024 end if;
7026 Comp := First_Entity (E);
7027 while Present (Comp) loop
7028 if Is_Type (Comp) then
7029 Freeze_And_Append (Comp, N, Result);
7031 elsif (Ekind (Comp)) /= E_Function then
7033 -- The guard on the presence of the Etype seems to be needed
7034 -- for some CodePeer (-gnatcC) cases, but not clear why???
7036 if Present (Etype (Comp)) then
7037 if Is_Itype (Etype (Comp))
7038 and then Underlying_Type (Scope (Etype (Comp))) = E
7039 then
7040 Undelay_Type (Etype (Comp));
7041 end if;
7043 Freeze_And_Append (Etype (Comp), N, Result);
7044 end if;
7045 end if;
7047 Next_Entity (Comp);
7048 end loop;
7050 -- Private types are required to point to the same freeze node as
7051 -- their corresponding full views. The freeze node itself has to
7052 -- point to the partial view of the entity (because from the partial
7053 -- view, we can retrieve the full view, but not the reverse).
7054 -- However, in order to freeze correctly, we need to freeze the full
7055 -- view. If we are freezing at the end of a scope (or within the
7056 -- scope) of the private type, the partial and full views will have
7057 -- been swapped, the full view appears first in the entity chain and
7058 -- the swapping mechanism ensures that the pointers are properly set
7059 -- (on scope exit).
7061 -- If we encounter the partial view before the full view (e.g. when
7062 -- freezing from another scope), we freeze the full view, and then
7063 -- set the pointers appropriately since we cannot rely on swapping to
7064 -- fix things up (subtypes in an outer scope might not get swapped).
7066 -- If the full view is itself private, the above requirements apply
7067 -- to the underlying full view instead of the full view. But there is
7068 -- no swapping mechanism for the underlying full view so we need to
7069 -- set the pointers appropriately in both cases.
7071 elsif Is_Incomplete_Or_Private_Type (E)
7072 and then not Is_Generic_Type (E)
7073 then
7074 -- The construction of the dispatch table associated with library
7075 -- level tagged types forces freezing of all the primitives of the
7076 -- type, which may cause premature freezing of the partial view.
7077 -- For example:
7079 -- package Pkg is
7080 -- type T is tagged private;
7081 -- type DT is new T with private;
7082 -- procedure Prim (X : in out T; Y : in out DT'Class);
7083 -- private
7084 -- type T is tagged null record;
7085 -- Obj : T;
7086 -- type DT is new T with null record;
7087 -- end;
7089 -- In this case the type will be frozen later by the usual
7090 -- mechanism: an object declaration, an instantiation, or the
7091 -- end of a declarative part.
7093 if Is_Library_Level_Tagged_Type (E)
7094 and then No (Full_View (E))
7095 then
7096 Set_Is_Frozen (E, False);
7097 goto Leave;
7099 -- Case of full view present
7101 elsif Present (Full_View (E)) then
7103 -- If full view has already been frozen, then no further
7104 -- processing is required
7106 if Is_Frozen (Full_View (E)) then
7107 Set_Has_Delayed_Freeze (E, False);
7108 Set_Freeze_Node (E, Empty);
7110 -- Otherwise freeze full view and patch the pointers so that
7111 -- the freeze node will elaborate both views in the back end.
7112 -- However, if full view is itself private, freeze underlying
7113 -- full view instead and patch the pointers so that the freeze
7114 -- node will elaborate the three views in the back end.
7116 else
7117 declare
7118 Full : Entity_Id := Full_View (E);
7120 begin
7121 if Is_Private_Type (Full)
7122 and then Present (Underlying_Full_View (Full))
7123 then
7124 Full := Underlying_Full_View (Full);
7125 end if;
7127 Freeze_And_Append (Full, N, Result);
7129 if Full /= Full_View (E)
7130 and then Has_Delayed_Freeze (Full_View (E))
7131 then
7132 F_Node := Freeze_Node (Full);
7134 if Present (F_Node) then
7135 Inherit_Freeze_Node
7136 (Fnod => F_Node, Typ => Full_View (E));
7137 else
7138 Set_Has_Delayed_Freeze (Full_View (E), False);
7139 Set_Freeze_Node (Full_View (E), Empty);
7140 end if;
7141 end if;
7143 if Has_Delayed_Freeze (E) then
7144 F_Node := Freeze_Node (Full_View (E));
7146 if Present (F_Node) then
7147 Inherit_Freeze_Node (Fnod => F_Node, Typ => E);
7148 else
7149 -- {Incomplete,Private}_Subtypes with Full_Views
7150 -- constrained by discriminants.
7152 Set_Has_Delayed_Freeze (E, False);
7153 Set_Freeze_Node (E, Empty);
7154 end if;
7155 end if;
7156 end;
7157 end if;
7159 Check_Debug_Info_Needed (E);
7161 -- AI95-117 requires that the convention of a partial view be
7162 -- the same as the convention of the full view. Note that this
7163 -- is a recognized breach of privacy, but it's essential for
7164 -- logical consistency of representation, and the lack of a
7165 -- rule in RM95 was an oversight.
7167 Set_Convention (E, Convention (Full_View (E)));
7169 Set_Size_Known_At_Compile_Time (E,
7170 Size_Known_At_Compile_Time (Full_View (E)));
7172 -- Size information is copied from the full view to the
7173 -- incomplete or private view for consistency.
7175 -- We skip this is the full view is not a type. This is very
7176 -- strange of course, and can only happen as a result of
7177 -- certain illegalities, such as a premature attempt to derive
7178 -- from an incomplete type.
7180 if Is_Type (Full_View (E)) then
7181 Set_Size_Info (E, Full_View (E));
7182 Copy_RM_Size (To => E, From => Full_View (E));
7183 end if;
7185 goto Leave;
7187 -- Case of underlying full view present
7189 elsif Is_Private_Type (E)
7190 and then Present (Underlying_Full_View (E))
7191 then
7192 if not Is_Frozen (Underlying_Full_View (E)) then
7193 Freeze_And_Append (Underlying_Full_View (E), N, Result);
7194 end if;
7196 -- Patch the pointers so that the freeze node will elaborate
7197 -- both views in the back end.
7199 if Has_Delayed_Freeze (E) then
7200 F_Node := Freeze_Node (Underlying_Full_View (E));
7202 if Present (F_Node) then
7203 Inherit_Freeze_Node
7204 (Fnod => F_Node,
7205 Typ => E);
7206 else
7207 Set_Has_Delayed_Freeze (E, False);
7208 Set_Freeze_Node (E, Empty);
7209 end if;
7210 end if;
7212 Check_Debug_Info_Needed (E);
7214 goto Leave;
7216 -- Case of no full view present. If entity is subtype or derived,
7217 -- it is safe to freeze, correctness depends on the frozen status
7218 -- of parent. Otherwise it is either premature usage, or a Taft
7219 -- amendment type, so diagnosis is at the point of use and the
7220 -- type might be frozen later.
7222 elsif E /= Base_Type (E) then
7223 declare
7224 Btyp : constant Entity_Id := Base_Type (E);
7226 begin
7227 -- However, if the base type is itself private and has no
7228 -- (underlying) full view either, wait until the full type
7229 -- declaration is seen and all the full views are created.
7231 if Is_Private_Type (Btyp)
7232 and then No (Full_View (Btyp))
7233 and then No (Underlying_Full_View (Btyp))
7234 and then Has_Delayed_Freeze (Btyp)
7235 and then No (Freeze_Node (Btyp))
7236 then
7237 Set_Is_Frozen (E, False);
7238 Result := No_List;
7239 goto Leave;
7240 end if;
7241 end;
7243 elsif Is_Derived_Type (E) then
7244 null;
7246 else
7247 Set_Is_Frozen (E, False);
7248 Result := No_List;
7249 goto Leave;
7250 end if;
7252 -- For access subprogram, freeze types of all formals, the return
7253 -- type was already frozen, since it is the Etype of the function.
7254 -- Formal types can be tagged Taft amendment types, but otherwise
7255 -- they cannot be incomplete.
7257 elsif Ekind (E) = E_Subprogram_Type then
7258 Formal := First_Formal (E);
7259 while Present (Formal) loop
7260 if Ekind (Etype (Formal)) = E_Incomplete_Type
7261 and then No (Full_View (Etype (Formal)))
7262 then
7263 if Is_Tagged_Type (Etype (Formal)) then
7264 null;
7266 -- AI05-151: Incomplete types are allowed in access to
7267 -- subprogram specifications.
7269 elsif Ada_Version < Ada_2012 then
7270 Error_Msg_NE
7271 ("invalid use of incomplete type&", E, Etype (Formal));
7272 end if;
7273 end if;
7275 Freeze_And_Append (Etype (Formal), N, Result);
7276 Next_Formal (Formal);
7277 end loop;
7279 Freeze_Subprogram (E);
7281 -- For access to a protected subprogram, freeze the equivalent type
7282 -- (however this is not set if we are not generating code or if this
7283 -- is an anonymous type used just for resolution).
7285 elsif Is_Access_Protected_Subprogram_Type (E) then
7286 if Present (Equivalent_Type (E)) then
7287 Freeze_And_Append (Equivalent_Type (E), N, Result);
7288 end if;
7289 end if;
7291 -- Generic types are never seen by the back-end, and are also not
7292 -- processed by the expander (since the expander is turned off for
7293 -- generic processing), so we never need freeze nodes for them.
7295 if Is_Generic_Type (E) then
7296 goto Leave;
7297 end if;
7299 -- Some special processing for non-generic types to complete
7300 -- representation details not known till the freeze point.
7302 if Is_Fixed_Point_Type (E) then
7303 Freeze_Fixed_Point_Type (E);
7305 elsif Is_Enumeration_Type (E) then
7306 Freeze_Enumeration_Type (E);
7308 elsif Is_Integer_Type (E) then
7309 Adjust_Esize_For_Alignment (E);
7311 if Is_Modular_Integer_Type (E) then
7312 -- Standard_Address has been built with the assumption that its
7313 -- modulus was System_Address_Size, but this is not a universal
7314 -- property and may need to be corrected.
7316 if Is_RTE (E, RE_Address) then
7317 Set_Modulus (Standard_Address, Modulus (E));
7318 Set_Intval
7319 (High_Bound (Scalar_Range (Standard_Address)),
7320 Modulus (E) - 1);
7322 elsif Warn_On_Suspicious_Modulus_Value then
7323 Check_Suspicious_Modulus (E);
7324 end if;
7325 end if;
7327 -- The pool applies to named and anonymous access types, but not
7328 -- to subprogram and to internal types generated for 'Access
7329 -- references.
7331 elsif Is_Access_Object_Type (E)
7332 and then Ekind (E) /= E_Access_Attribute_Type
7333 then
7334 -- If a pragma Default_Storage_Pool applies, and this type has no
7335 -- Storage_Pool or Storage_Size clause (which must have occurred
7336 -- before the freezing point), then use the default. This applies
7337 -- only to base types.
7339 -- None of this applies to access to subprograms, for which there
7340 -- are clearly no pools.
7342 if Present (Default_Pool)
7343 and then Is_Base_Type (E)
7344 and then not Has_Storage_Size_Clause (E)
7345 and then No (Associated_Storage_Pool (E))
7346 then
7347 -- Case of pragma Default_Storage_Pool (null)
7349 if Nkind (Default_Pool) = N_Null then
7350 Set_No_Pool_Assigned (E);
7352 -- Case of pragma Default_Storage_Pool (Standard)
7354 elsif Entity (Default_Pool) = Standard_Standard then
7355 Set_Associated_Storage_Pool (E, RTE (RE_Global_Pool_Object));
7357 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
7359 else
7360 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
7361 end if;
7362 end if;
7364 -- Check restriction for standard storage pool
7366 if No (Associated_Storage_Pool (E)) then
7367 Check_Restriction (No_Standard_Storage_Pools, E);
7368 end if;
7370 -- Deal with error message for pure access type. This is not an
7371 -- error in Ada 2005 if there is no pool (see AI-366).
7373 if Is_Pure_Unit_Access_Type (E)
7374 and then (Ada_Version < Ada_2005
7375 or else not No_Pool_Assigned (E))
7376 and then not Is_Generic_Unit (Scope (E))
7377 then
7378 Error_Msg_N ("named access type not allowed in pure unit", E);
7380 if Ada_Version >= Ada_2005 then
7381 Error_Msg_N
7382 ("\would be legal if Storage_Size of 0 given??", E);
7384 elsif No_Pool_Assigned (E) then
7385 Error_Msg_N
7386 ("\would be legal in Ada 2005??", E);
7388 else
7389 Error_Msg_N
7390 ("\would be legal in Ada 2005 if "
7391 & "Storage_Size of 0 given??", E);
7392 end if;
7393 end if;
7394 end if;
7396 -- Case of composite types
7398 if Is_Composite_Type (E) then
7400 -- AI95-117 requires that all new primitives of a tagged type must
7401 -- inherit the convention of the full view of the type. Inherited
7402 -- and overriding operations are defined to inherit the convention
7403 -- of their parent or overridden subprogram (also specified in
7404 -- AI-117), which will have occurred earlier (in Derive_Subprogram
7405 -- and New_Overloaded_Entity). Here we set the convention of
7406 -- primitives that are still convention Ada, which will ensure
7407 -- that any new primitives inherit the type's convention. Class-
7408 -- wide types can have a foreign convention inherited from their
7409 -- specific type, but are excluded from this since they don't have
7410 -- any associated primitives.
7412 if Is_Tagged_Type (E)
7413 and then not Is_Class_Wide_Type (E)
7414 and then Convention (E) /= Convention_Ada
7415 then
7416 declare
7417 Prim_List : constant Elist_Id := Primitive_Operations (E);
7418 Prim : Elmt_Id;
7420 begin
7421 Prim := First_Elmt (Prim_List);
7422 while Present (Prim) loop
7423 if Convention (Node (Prim)) = Convention_Ada then
7424 Set_Convention (Node (Prim), Convention (E));
7425 end if;
7427 Next_Elmt (Prim);
7428 end loop;
7429 end;
7430 end if;
7432 -- If the type is a simple storage pool type, then this is where
7433 -- we attempt to locate and validate its Allocate, Deallocate, and
7434 -- Storage_Size operations (the first is required, and the latter
7435 -- two are optional). We also verify that the full type for a
7436 -- private type is allowed to be a simple storage pool type.
7438 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
7439 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
7440 then
7441 -- If the type is marked Has_Private_Declaration, then this is
7442 -- a full type for a private type that was specified with the
7443 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
7444 -- pragma is allowed for the full type (for example, it can't
7445 -- be an array type, or a nonlimited record type).
7447 if Has_Private_Declaration (E) then
7448 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
7449 and then not Is_Private_Type (E)
7450 then
7451 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
7452 Error_Msg_N
7453 ("pragma% can only apply to full type that is an " &
7454 "explicitly limited type", E);
7455 end if;
7456 end if;
7458 Validate_Simple_Pool_Ops : declare
7459 Pool_Type : Entity_Id renames E;
7460 Address_Type : constant Entity_Id := RTE (RE_Address);
7461 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
7463 procedure Validate_Simple_Pool_Op_Formal
7464 (Pool_Op : Entity_Id;
7465 Pool_Op_Formal : in out Entity_Id;
7466 Expected_Mode : Formal_Kind;
7467 Expected_Type : Entity_Id;
7468 Formal_Name : String;
7469 OK_Formal : in out Boolean);
7470 -- Validate one formal Pool_Op_Formal of the candidate pool
7471 -- operation Pool_Op. The formal must be of Expected_Type
7472 -- and have mode Expected_Mode. OK_Formal will be set to
7473 -- False if the formal doesn't match. If OK_Formal is False
7474 -- on entry, then the formal will effectively be ignored
7475 -- (because validation of the pool op has already failed).
7476 -- Upon return, Pool_Op_Formal will be updated to the next
7477 -- formal, if any.
7479 procedure Validate_Simple_Pool_Operation
7480 (Op_Name : Name_Id);
7481 -- Search for and validate a simple pool operation with the
7482 -- name Op_Name. If the name is Allocate, then there must be
7483 -- exactly one such primitive operation for the simple pool
7484 -- type. If the name is Deallocate or Storage_Size, then
7485 -- there can be at most one such primitive operation. The
7486 -- profile of the located primitive must conform to what
7487 -- is expected for each operation.
7489 ------------------------------------
7490 -- Validate_Simple_Pool_Op_Formal --
7491 ------------------------------------
7493 procedure Validate_Simple_Pool_Op_Formal
7494 (Pool_Op : Entity_Id;
7495 Pool_Op_Formal : in out Entity_Id;
7496 Expected_Mode : Formal_Kind;
7497 Expected_Type : Entity_Id;
7498 Formal_Name : String;
7499 OK_Formal : in out Boolean)
7501 begin
7502 -- If OK_Formal is False on entry, then simply ignore
7503 -- the formal, because an earlier formal has already
7504 -- been flagged.
7506 if not OK_Formal then
7507 return;
7509 -- If no formal is passed in, then issue an error for a
7510 -- missing formal.
7512 elsif No (Pool_Op_Formal) then
7513 Error_Msg_NE
7514 ("simple storage pool op missing formal " &
7515 Formal_Name & " of type&", Pool_Op, Expected_Type);
7516 OK_Formal := False;
7518 return;
7519 end if;
7521 if Etype (Pool_Op_Formal) /= Expected_Type then
7523 -- If the pool type was expected for this formal, then
7524 -- this will not be considered a candidate operation
7525 -- for the simple pool, so we unset OK_Formal so that
7526 -- the op and any later formals will be ignored.
7528 if Expected_Type = Pool_Type then
7529 OK_Formal := False;
7531 return;
7533 else
7534 Error_Msg_NE
7535 ("wrong type for formal " & Formal_Name &
7536 " of simple storage pool op; expected type&",
7537 Pool_Op_Formal, Expected_Type);
7538 end if;
7539 end if;
7541 -- Issue error if formal's mode is not the expected one
7543 if Ekind (Pool_Op_Formal) /= Expected_Mode then
7544 Error_Msg_N
7545 ("wrong mode for formal of simple storage pool op",
7546 Pool_Op_Formal);
7547 end if;
7549 -- Advance to the next formal
7551 Next_Formal (Pool_Op_Formal);
7552 end Validate_Simple_Pool_Op_Formal;
7554 ------------------------------------
7555 -- Validate_Simple_Pool_Operation --
7556 ------------------------------------
7558 procedure Validate_Simple_Pool_Operation
7559 (Op_Name : Name_Id)
7561 Op : Entity_Id;
7562 Found_Op : Entity_Id := Empty;
7563 Formal : Entity_Id;
7564 Is_OK : Boolean;
7566 begin
7567 pragma Assert
7568 (Op_Name in Name_Allocate
7569 | Name_Deallocate
7570 | Name_Storage_Size);
7572 Error_Msg_Name_1 := Op_Name;
7574 -- For each homonym declared immediately in the scope
7575 -- of the simple storage pool type, determine whether
7576 -- the homonym is an operation of the pool type, and,
7577 -- if so, check that its profile is as expected for
7578 -- a simple pool operation of that name.
7580 Op := Get_Name_Entity_Id (Op_Name);
7581 while Present (Op) loop
7582 if Ekind (Op) in E_Function | E_Procedure
7583 and then Scope (Op) = Current_Scope
7584 then
7585 Formal := First_Entity (Op);
7587 Is_OK := True;
7589 -- The first parameter must be of the pool type
7590 -- in order for the operation to qualify.
7592 if Op_Name = Name_Storage_Size then
7593 Validate_Simple_Pool_Op_Formal
7594 (Op, Formal, E_In_Parameter, Pool_Type,
7595 "Pool", Is_OK);
7596 else
7597 Validate_Simple_Pool_Op_Formal
7598 (Op, Formal, E_In_Out_Parameter, Pool_Type,
7599 "Pool", Is_OK);
7600 end if;
7602 -- If another operation with this name has already
7603 -- been located for the type, then flag an error,
7604 -- since we only allow the type to have a single
7605 -- such primitive.
7607 if Present (Found_Op) and then Is_OK then
7608 Error_Msg_NE
7609 ("only one % operation allowed for " &
7610 "simple storage pool type&", Op, Pool_Type);
7611 end if;
7613 -- In the case of Allocate and Deallocate, a formal
7614 -- of type System.Address is required.
7616 if Op_Name = Name_Allocate then
7617 Validate_Simple_Pool_Op_Formal
7618 (Op, Formal, E_Out_Parameter,
7619 Address_Type, "Storage_Address", Is_OK);
7621 elsif Op_Name = Name_Deallocate then
7622 Validate_Simple_Pool_Op_Formal
7623 (Op, Formal, E_In_Parameter,
7624 Address_Type, "Storage_Address", Is_OK);
7625 end if;
7627 -- In the case of Allocate and Deallocate, formals
7628 -- of type Storage_Count are required as the third
7629 -- and fourth parameters.
7631 if Op_Name /= Name_Storage_Size then
7632 Validate_Simple_Pool_Op_Formal
7633 (Op, Formal, E_In_Parameter,
7634 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
7635 Validate_Simple_Pool_Op_Formal
7636 (Op, Formal, E_In_Parameter,
7637 Stg_Cnt_Type, "Alignment", Is_OK);
7638 end if;
7640 -- If no mismatched formals have been found (Is_OK)
7641 -- and no excess formals are present, then this
7642 -- operation has been validated, so record it.
7644 if No (Formal) and then Is_OK then
7645 Found_Op := Op;
7646 end if;
7647 end if;
7649 Op := Homonym (Op);
7650 end loop;
7652 -- There must be a valid Allocate operation for the type,
7653 -- so issue an error if none was found.
7655 if Op_Name = Name_Allocate
7656 and then No (Found_Op)
7657 then
7658 Error_Msg_N ("missing % operation for simple " &
7659 "storage pool type", Pool_Type);
7661 elsif Present (Found_Op) then
7663 -- Simple pool operations can't be abstract
7665 if Is_Abstract_Subprogram (Found_Op) then
7666 Error_Msg_N
7667 ("simple storage pool operation must not be " &
7668 "abstract", Found_Op);
7669 end if;
7671 -- The Storage_Size operation must be a function with
7672 -- Storage_Count as its result type.
7674 if Op_Name = Name_Storage_Size then
7675 if Ekind (Found_Op) = E_Procedure then
7676 Error_Msg_N
7677 ("% operation must be a function", Found_Op);
7679 elsif Etype (Found_Op) /= Stg_Cnt_Type then
7680 Error_Msg_NE
7681 ("wrong result type for%, expected type&",
7682 Found_Op, Stg_Cnt_Type);
7683 end if;
7685 -- Allocate and Deallocate must be procedures
7687 elsif Ekind (Found_Op) = E_Function then
7688 Error_Msg_N
7689 ("% operation must be a procedure", Found_Op);
7690 end if;
7691 end if;
7692 end Validate_Simple_Pool_Operation;
7694 -- Start of processing for Validate_Simple_Pool_Ops
7696 begin
7697 Validate_Simple_Pool_Operation (Name_Allocate);
7698 Validate_Simple_Pool_Operation (Name_Deallocate);
7699 Validate_Simple_Pool_Operation (Name_Storage_Size);
7700 end Validate_Simple_Pool_Ops;
7701 end if;
7702 end if;
7704 -- Now that all types from which E may depend are frozen, see if
7705 -- strict alignment is required, a component clause on a record
7706 -- is correct, the size is known at compile time and if it must
7707 -- be unsigned, in that order.
7709 if Base_Type (E) = E then
7710 Check_Strict_Alignment (E);
7711 end if;
7713 if Ekind (E) in E_Record_Type | E_Record_Subtype then
7714 declare
7715 RC : constant Node_Id := Get_Record_Representation_Clause (E);
7716 begin
7717 if Present (RC) then
7718 Check_Record_Representation_Clause (RC);
7719 end if;
7720 end;
7721 end if;
7723 Check_Compile_Time_Size (E);
7725 Check_Unsigned_Type (E);
7727 -- Do not allow a size clause for a type which does not have a size
7728 -- that is known at compile time
7730 if (Has_Size_Clause (E) or else Has_Object_Size_Clause (E))
7731 and then not Size_Known_At_Compile_Time (E)
7732 then
7733 -- Suppress this message if errors posted on E, even if we are
7734 -- in all errors mode, since this is often a junk message
7736 if not Error_Posted (E) then
7737 Error_Msg_N
7738 ("size clause not allowed for variable length type",
7739 Size_Clause (E));
7740 end if;
7741 end if;
7743 -- Now we set/verify the representation information, in particular
7744 -- the size and alignment values. This processing is not required for
7745 -- generic types, since generic types do not play any part in code
7746 -- generation, and so the size and alignment values for such types
7747 -- are irrelevant. Ditto for types declared within a generic unit,
7748 -- which may have components that depend on generic parameters, and
7749 -- that will be recreated in an instance.
7751 if Inside_A_Generic then
7752 null;
7754 -- Otherwise we call the layout procedure
7756 else
7757 Layout_Type (E);
7758 end if;
7760 -- If this is an access to subprogram whose designated type is itself
7761 -- a subprogram type, the return type of this anonymous subprogram
7762 -- type must be decorated as well.
7764 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
7765 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
7766 then
7767 Layout_Type (Etype (Designated_Type (E)));
7768 end if;
7770 -- If the type has a Defaut_Value/Default_Component_Value aspect,
7771 -- this is where we analyze the expression (after the type is frozen,
7772 -- since in the case of Default_Value, we are analyzing with the
7773 -- type itself, and we treat Default_Component_Value similarly for
7774 -- the sake of uniformity).
7776 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
7777 declare
7778 Nam : Name_Id;
7779 Exp : Node_Id;
7780 Typ : Entity_Id;
7782 begin
7783 if Is_Scalar_Type (E) then
7784 Nam := Name_Default_Value;
7785 Typ := E;
7786 Exp := Default_Aspect_Value (Typ);
7787 else
7788 Nam := Name_Default_Component_Value;
7789 Typ := Component_Type (E);
7790 Exp := Default_Aspect_Component_Value (E);
7791 end if;
7793 Analyze_And_Resolve (Exp, Typ);
7795 if Etype (Exp) /= Any_Type then
7796 if not Is_OK_Static_Expression (Exp) then
7797 Error_Msg_Name_1 := Nam;
7798 Flag_Non_Static_Expr
7799 ("aspect% requires static expression", Exp);
7800 end if;
7801 end if;
7802 end;
7803 end if;
7805 -- Verify at this point that No_Controlled_Parts and No_Task_Parts,
7806 -- when specified on the current type or one of its ancestors, has
7807 -- not been overridden and that no violation of the aspect has
7808 -- occurred.
7810 -- It is important that we perform the checks here after the type has
7811 -- been processed because if said type depended on a private type it
7812 -- will not have been marked controlled or having tasks.
7814 Check_No_Parts_Violations (E, Aspect_No_Controlled_Parts);
7815 Check_No_Parts_Violations (E, Aspect_No_Task_Parts);
7817 -- End of freeze processing for type entities
7818 end if;
7820 -- Here is where we logically freeze the current entity. If it has a
7821 -- freeze node, then this is the point at which the freeze node is
7822 -- linked into the result list.
7824 if Has_Delayed_Freeze (E) then
7826 -- If a freeze node is already allocated, use it, otherwise allocate
7827 -- a new one. The preallocation happens in the case of anonymous base
7828 -- types, where we preallocate so that we can set First_Subtype_Link.
7829 -- Note that we reset the Sloc to the current freeze location.
7831 if Present (Freeze_Node (E)) then
7832 F_Node := Freeze_Node (E);
7833 Set_Sloc (F_Node, Loc);
7835 else
7836 F_Node := New_Node (N_Freeze_Entity, Loc);
7837 Set_Freeze_Node (E, F_Node);
7838 Set_Access_Types_To_Process (F_Node, No_Elist);
7839 Set_TSS_Elist (F_Node, No_Elist);
7840 Set_Actions (F_Node, No_List);
7841 end if;
7843 Set_Entity (F_Node, E);
7844 Add_To_Result (F_Node);
7846 -- A final pass over record types with discriminants. If the type
7847 -- has an incomplete declaration, there may be constrained access
7848 -- subtypes declared elsewhere, which do not depend on the discrimi-
7849 -- nants of the type, and which are used as component types (i.e.
7850 -- the full view is a recursive type). The designated types of these
7851 -- subtypes can only be elaborated after the type itself, and they
7852 -- need an itype reference.
7854 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
7855 declare
7856 Comp : Entity_Id;
7857 IR : Node_Id;
7858 Typ : Entity_Id;
7860 begin
7861 Comp := First_Component (E);
7862 while Present (Comp) loop
7863 Typ := Etype (Comp);
7865 if Is_Access_Type (Typ)
7866 and then Scope (Typ) /= E
7867 and then Base_Type (Designated_Type (Typ)) = E
7868 and then Is_Itype (Designated_Type (Typ))
7869 then
7870 IR := Make_Itype_Reference (Sloc (Comp));
7871 Set_Itype (IR, Designated_Type (Typ));
7872 Append (IR, Result);
7873 end if;
7875 Next_Component (Comp);
7876 end loop;
7877 end;
7878 end if;
7879 end if;
7881 -- When a type is frozen, the first subtype of the type is frozen as
7882 -- well (RM 13.14(15)). This has to be done after freezing the type,
7883 -- since obviously the first subtype depends on its own base type.
7885 if Is_Type (E) then
7886 Freeze_And_Append (First_Subtype (E), N, Result);
7888 -- If we just froze a tagged non-class-wide record, then freeze the
7889 -- corresponding class-wide type. This must be done after the tagged
7890 -- type itself is frozen, because the class-wide type refers to the
7891 -- tagged type which generates the class.
7893 -- For a tagged type, freeze explicitly those primitive operations
7894 -- that are expression functions, which otherwise have no clear
7895 -- freeze point: these have to be frozen before the dispatch table
7896 -- for the type is built, and before any explicit call to the
7897 -- primitive, which would otherwise be the freeze point for it.
7899 if Is_Tagged_Type (E)
7900 and then not Is_Class_Wide_Type (E)
7901 and then Present (Class_Wide_Type (E))
7902 then
7903 Freeze_And_Append (Class_Wide_Type (E), N, Result);
7905 declare
7906 Ops : constant Elist_Id := Primitive_Operations (E);
7908 Elmt : Elmt_Id;
7909 Subp : Entity_Id;
7911 begin
7912 if Ops /= No_Elist then
7913 Elmt := First_Elmt (Ops);
7914 while Present (Elmt) loop
7915 Subp := Node (Elmt);
7916 if Is_Expression_Function (Subp) then
7917 Freeze_And_Append (Subp, N, Result);
7918 end if;
7920 Next_Elmt (Elmt);
7921 end loop;
7922 end if;
7923 end;
7924 end if;
7925 end if;
7927 Check_Debug_Info_Needed (E);
7929 -- If subprogram has address clause then reset Is_Public flag, since we
7930 -- do not want the backend to generate external references.
7932 if Is_Subprogram (E)
7933 and then Present (Address_Clause (E))
7934 and then not Is_Library_Level_Entity (E)
7935 then
7936 Set_Is_Public (E, False);
7937 end if;
7939 -- The Ghost mode of the enclosing context is ignored, while the
7940 -- entity being frozen is living. Insert the freezing action prior
7941 -- to the start of the enclosing ignored Ghost region. As a result
7942 -- the freezeing action will be preserved when the ignored Ghost
7943 -- context is eliminated. The insertion must take place even when
7944 -- the context is a spec expression, otherwise "Handling of Default
7945 -- and Per-Object Expressions" will suppress the insertion, and the
7946 -- freeze node will be dropped on the floor.
7948 if Saved_GM = Ignore
7949 and then Ghost_Mode /= Ignore
7950 and then Present (Ignored_Ghost_Region)
7951 then
7952 Insert_Actions
7953 (Assoc_Node => Ignored_Ghost_Region,
7954 Ins_Actions => Result,
7955 Spec_Expr_OK => True);
7957 Result := No_List;
7958 end if;
7960 <<Leave>>
7961 Restore_Ghost_Region (Saved_GM, Saved_IGR);
7963 return Result;
7964 end Freeze_Entity;
7966 -----------------------------
7967 -- Freeze_Enumeration_Type --
7968 -----------------------------
7970 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
7971 begin
7972 -- By default, if no size clause is present, an enumeration type with
7973 -- Convention C is assumed to interface to a C enum and has integer
7974 -- size, except for a boolean type because it is assumed to interface
7975 -- to _Bool introduced in C99. This applies to types. For subtypes,
7976 -- verify that its base type has no size clause either. Treat other
7977 -- foreign conventions in the same way, and also make sure alignment
7978 -- is set right.
7980 if Has_Foreign_Convention (Typ)
7981 and then not Is_Boolean_Type (Typ)
7982 and then not Has_Size_Clause (Typ)
7983 and then not Has_Size_Clause (Base_Type (Typ))
7984 and then Esize (Typ) < Standard_Integer_Size
7986 -- Don't do this if Short_Enums on target
7988 and then not Target_Short_Enums
7989 then
7990 Set_Esize (Typ, UI_From_Int (Standard_Integer_Size));
7991 Set_Alignment (Typ, Alignment (Standard_Integer));
7993 -- Normal Ada case or size clause present or not Long_C_Enums on target
7995 else
7996 -- If the enumeration type interfaces to C, and it has a size clause
7997 -- that is smaller than the size of int, it warrants a warning. The
7998 -- user may intend the C type to be a boolean or a char, so this is
7999 -- not by itself an error that the Ada compiler can detect, but it
8000 -- is worth a heads-up. For Boolean and Character types we
8001 -- assume that the programmer has the proper C type in mind.
8002 -- For explicit sizes larger than int, assume the user knows what
8003 -- he is doing and that the code is intentional.
8005 if Convention (Typ) = Convention_C
8006 and then Has_Size_Clause (Typ)
8007 and then Esize (Typ) < Standard_Integer_Size
8008 and then not Is_Boolean_Type (Typ)
8009 and then not Is_Character_Type (Typ)
8011 -- Don't do this if Short_Enums on target
8013 and then not Target_Short_Enums
8014 then
8015 Error_Msg_N
8016 ("??the size of enums in C is implementation-defined",
8017 Size_Clause (Typ));
8018 Error_Msg_N
8019 ("\??check that the C counterpart has size of " &
8020 UI_Image (Esize (Typ)),
8021 Size_Clause (Typ));
8022 end if;
8024 Adjust_Esize_For_Alignment (Typ);
8025 end if;
8026 end Freeze_Enumeration_Type;
8028 -----------------------
8029 -- Freeze_Expression --
8030 -----------------------
8032 procedure Freeze_Expression (N : Node_Id) is
8034 function Find_Aggregate_Component_Desig_Type return Entity_Id;
8035 -- If the expression is an array aggregate, the type of the component
8036 -- expressions is also frozen. If the component type is an access type
8037 -- and the expressions include allocators, the designed type is frozen
8038 -- as well.
8040 function In_Expanded_Body (N : Node_Id) return Boolean;
8041 -- Given an N_Handled_Sequence_Of_Statements node, determines whether it
8042 -- is the statement sequence of an expander-generated subprogram: body
8043 -- created for an expression function, for a predicate function, an init
8044 -- proc, a stream subprogram, or a renaming as body. If so, this is not
8045 -- a freezing context and the entity will be frozen at a later point.
8047 function Has_Decl_In_List
8048 (E : Entity_Id;
8049 N : Node_Id;
8050 L : List_Id) return Boolean;
8051 -- Determines whether an entity E referenced in node N is declared in
8052 -- the list L.
8054 -----------------------------------------
8055 -- Find_Aggregate_Component_Desig_Type --
8056 -----------------------------------------
8058 function Find_Aggregate_Component_Desig_Type return Entity_Id is
8059 Assoc : Node_Id;
8060 Exp : Node_Id;
8062 begin
8063 if Present (Expressions (N)) then
8064 Exp := First (Expressions (N));
8065 while Present (Exp) loop
8066 if Nkind (Exp) = N_Allocator then
8067 return Designated_Type (Component_Type (Etype (N)));
8068 end if;
8070 Next (Exp);
8071 end loop;
8072 end if;
8074 if Present (Component_Associations (N)) then
8075 Assoc := First (Component_Associations (N));
8076 while Present (Assoc) loop
8077 if Nkind (Expression (Assoc)) = N_Allocator then
8078 return Designated_Type (Component_Type (Etype (N)));
8079 end if;
8081 Next (Assoc);
8082 end loop;
8083 end if;
8085 return Empty;
8086 end Find_Aggregate_Component_Desig_Type;
8088 ----------------------
8089 -- In_Expanded_Body --
8090 ----------------------
8092 function In_Expanded_Body (N : Node_Id) return Boolean is
8093 P : constant Node_Id := Parent (N);
8094 Id : Entity_Id;
8096 begin
8097 if Nkind (P) /= N_Subprogram_Body then
8098 return False;
8100 -- AI12-0157: An expression function that is a completion is a freeze
8101 -- point. If the body is the result of expansion, it is not.
8103 elsif Was_Expression_Function (P) then
8104 return not Comes_From_Source (P);
8106 -- This is the body of a generated predicate function
8108 elsif Present (Corresponding_Spec (P))
8109 and then Is_Predicate_Function (Corresponding_Spec (P))
8110 then
8111 return True;
8113 else
8114 Id := Defining_Unit_Name (Specification (P));
8116 -- The following are expander-created bodies, or bodies that
8117 -- are not freeze points.
8119 if Nkind (Id) = N_Defining_Identifier
8120 and then (Is_Init_Proc (Id)
8121 or else Is_TSS (Id, TSS_Stream_Input)
8122 or else Is_TSS (Id, TSS_Stream_Output)
8123 or else Is_TSS (Id, TSS_Stream_Read)
8124 or else Is_TSS (Id, TSS_Stream_Write)
8125 or else Is_TSS (Id, TSS_Put_Image)
8126 or else Nkind (Original_Node (P)) =
8127 N_Subprogram_Renaming_Declaration)
8128 then
8129 return True;
8130 else
8131 return False;
8132 end if;
8133 end if;
8134 end In_Expanded_Body;
8136 ----------------------
8137 -- Has_Decl_In_List --
8138 ----------------------
8140 function Has_Decl_In_List
8141 (E : Entity_Id;
8142 N : Node_Id;
8143 L : List_Id) return Boolean
8145 Decl_Node : Node_Id;
8147 begin
8148 -- If E is an itype, pretend that it is declared in N
8150 if Is_Itype (E) then
8151 Decl_Node := N;
8152 else
8153 Decl_Node := Declaration_Node (E);
8154 end if;
8156 return Is_List_Member (Decl_Node)
8157 and then List_Containing (Decl_Node) = L;
8158 end Has_Decl_In_List;
8160 -- Local variables
8162 In_Spec_Exp : constant Boolean := In_Spec_Expression;
8164 Desig_Typ : Entity_Id;
8165 Nam : Entity_Id;
8166 P : Node_Id;
8167 Parent_P : Node_Id;
8168 Typ : Entity_Id;
8170 Allocator_Typ : Entity_Id := Empty;
8172 Freeze_Outside : Boolean := False;
8173 -- This flag is set true if the entity must be frozen outside the
8174 -- current subprogram. This happens in the case of expander generated
8175 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
8176 -- not freeze all entities like other bodies, but which nevertheless
8177 -- may reference entities that have to be frozen before the body and
8178 -- obviously cannot be frozen inside the body.
8180 Freeze_Outside_Subp : Entity_Id := Empty;
8181 -- This entity is set if we are inside a subprogram body and the frozen
8182 -- entity is defined in the enclosing scope of this subprogram. In such
8183 -- case we must skip the subprogram body when climbing the parents chain
8184 -- to locate the correct placement for the freezing node.
8186 -- Start of processing for Freeze_Expression
8188 begin
8189 -- Immediate return if freezing is inhibited. This flag is set by the
8190 -- analyzer to stop freezing on generated expressions that would cause
8191 -- freezing if they were in the source program, but which are not
8192 -- supposed to freeze, since they are created.
8194 if Must_Not_Freeze (N) then
8195 return;
8196 end if;
8198 -- If expression is non-static, then it does not freeze in a default
8199 -- expression, see section "Handling of Default Expressions" in the
8200 -- spec of package Sem for further details. Note that we have to make
8201 -- sure that we actually have a real expression (if we have a subtype
8202 -- indication, we can't test Is_OK_Static_Expression). However, we
8203 -- exclude the case of the prefix of an attribute of a static scalar
8204 -- subtype from this early return, because static subtype attributes
8205 -- should always cause freezing, even in default expressions, but
8206 -- the attribute may not have been marked as static yet (because in
8207 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
8208 -- Freeze_Expression on the prefix).
8210 if In_Spec_Exp
8211 and then Nkind (N) in N_Subexpr
8212 and then not Is_OK_Static_Expression (N)
8213 and then (Nkind (Parent (N)) /= N_Attribute_Reference
8214 or else not (Is_Entity_Name (N)
8215 and then Is_Type (Entity (N))
8216 and then Is_OK_Static_Subtype (Entity (N))))
8217 then
8218 return;
8219 end if;
8221 -- Freeze type of expression if not frozen already
8223 Typ := Empty;
8225 if Nkind (N) in N_Has_Etype and then Present (Etype (N)) then
8226 if not Is_Frozen (Etype (N)) then
8227 Typ := Etype (N);
8229 -- Base type may be an derived numeric type that is frozen at the
8230 -- point of declaration, but first_subtype is still unfrozen.
8232 elsif not Is_Frozen (First_Subtype (Etype (N))) then
8233 Typ := First_Subtype (Etype (N));
8234 end if;
8235 end if;
8237 -- For entity name, freeze entity if not frozen already. A special
8238 -- exception occurs for an identifier that did not come from source.
8239 -- We don't let such identifiers freeze a non-internal entity, i.e.
8240 -- an entity that did come from source, since such an identifier was
8241 -- generated by the expander, and cannot have any semantic effect on
8242 -- the freezing semantics. For example, this stops the parameter of
8243 -- an initialization procedure from freezing the variable.
8245 if Is_Entity_Name (N)
8246 and then Present (Entity (N))
8247 and then not Is_Frozen (Entity (N))
8248 and then (Nkind (N) /= N_Identifier
8249 or else Comes_From_Source (N)
8250 or else not Comes_From_Source (Entity (N)))
8251 then
8252 Nam := Entity (N);
8254 if Present (Nam) and then Ekind (Nam) = E_Function then
8255 Check_Expression_Function (N, Nam);
8256 end if;
8258 else
8259 Nam := Empty;
8260 end if;
8262 -- For an allocator freeze designated type if not frozen already
8264 -- For an aggregate whose component type is an access type, freeze the
8265 -- designated type now, so that its freeze does not appear within the
8266 -- loop that might be created in the expansion of the aggregate. If the
8267 -- designated type is a private type without full view, the expression
8268 -- cannot contain an allocator, so the type is not frozen.
8270 -- For a function, we freeze the entity when the subprogram declaration
8271 -- is frozen, but a function call may appear in an initialization proc.
8272 -- before the declaration is frozen. We need to generate the extra
8273 -- formals, if any, to ensure that the expansion of the call includes
8274 -- the proper actuals. This only applies to Ada subprograms, not to
8275 -- imported ones.
8277 Desig_Typ := Empty;
8279 case Nkind (N) is
8280 when N_Allocator =>
8281 Desig_Typ := Designated_Type (Etype (N));
8283 if Nkind (Expression (N)) = N_Qualified_Expression then
8284 Allocator_Typ := Entity (Subtype_Mark (Expression (N)));
8285 end if;
8287 when N_Aggregate =>
8288 if Is_Array_Type (Etype (N))
8289 and then Is_Access_Type (Component_Type (Etype (N)))
8290 then
8291 -- Check whether aggregate includes allocators
8293 Desig_Typ := Find_Aggregate_Component_Desig_Type;
8294 end if;
8296 when N_Indexed_Component
8297 | N_Selected_Component
8298 | N_Slice
8300 if Is_Access_Type (Etype (Prefix (N))) then
8301 Desig_Typ := Designated_Type (Etype (Prefix (N)));
8302 end if;
8304 when N_Identifier =>
8305 if Present (Nam)
8306 and then Ekind (Nam) = E_Function
8307 and then Nkind (Parent (N)) = N_Function_Call
8308 and then not Has_Foreign_Convention (Nam)
8309 then
8310 Create_Extra_Formals (Nam);
8311 end if;
8313 when others =>
8314 null;
8315 end case;
8317 if Desig_Typ /= Empty
8318 and then (Is_Frozen (Desig_Typ)
8319 or else not Is_Fully_Defined (Desig_Typ))
8320 then
8321 Desig_Typ := Empty;
8322 end if;
8324 -- All done if nothing needs freezing
8326 if No (Typ)
8327 and then No (Nam)
8328 and then No (Desig_Typ)
8329 and then No (Allocator_Typ)
8330 then
8331 return;
8332 end if;
8334 -- Check if we are inside a subprogram body and the frozen entity is
8335 -- defined in the enclosing scope of this subprogram. In such case we
8336 -- must skip the subprogram when climbing the parents chain to locate
8337 -- the correct placement for the freezing node.
8339 -- This is not needed for default expressions and other spec expressions
8340 -- in generic units since the Move_Freeze_Nodes mechanism (sem_ch12.adb)
8341 -- takes care of placing them at the proper place, after the generic
8342 -- unit.
8344 if Present (Nam)
8345 and then Scope (Nam) /= Current_Scope
8346 and then not (In_Spec_Exp and then Inside_A_Generic)
8347 then
8348 declare
8349 S : Entity_Id := Current_Scope;
8351 begin
8352 while Present (S)
8353 and then In_Same_Source_Unit (Nam, S)
8354 loop
8355 if Scope (S) = Scope (Nam) then
8356 if Is_Subprogram (S) and then Has_Completion (S) then
8357 Freeze_Outside_Subp := S;
8358 end if;
8360 exit;
8361 end if;
8363 S := Scope (S);
8364 end loop;
8365 end;
8366 end if;
8368 -- Examine the enclosing context by climbing the parent chain
8370 -- If we identified that we must freeze the entity outside of a given
8371 -- subprogram then we just climb up to that subprogram checking if some
8372 -- enclosing node is marked as Must_Not_Freeze (since in such case we
8373 -- must not freeze yet this entity).
8375 P := N;
8377 if Present (Freeze_Outside_Subp) then
8378 loop
8379 -- Do not freeze the current expression if another expression in
8380 -- the chain of parents must not be frozen.
8382 if Nkind (P) in N_Subexpr and then Must_Not_Freeze (P) then
8383 return;
8384 end if;
8386 Parent_P := Parent (P);
8388 -- If we don't have a parent, then we are not in a well-formed
8389 -- tree. This is an unusual case, but there are some legitimate
8390 -- situations in which this occurs, notably when the expressions
8391 -- in the range of a type declaration are resolved. We simply
8392 -- ignore the freeze request in this case.
8394 if No (Parent_P) then
8395 return;
8396 end if;
8398 -- If the parent is a subprogram body, the candidate insertion
8399 -- point is just ahead of it.
8401 if Nkind (Parent_P) = N_Subprogram_Body
8402 and then Unique_Defining_Entity (Parent_P) =
8403 Freeze_Outside_Subp
8404 then
8405 P := Parent_P;
8406 exit;
8407 end if;
8409 P := Parent_P;
8410 end loop;
8412 -- Otherwise the traversal serves two purposes - to detect scenarios
8413 -- where freezeing is not needed and to find the proper insertion point
8414 -- for the freeze nodes. Although somewhat similar to Insert_Actions,
8415 -- this traversal is freezing semantics-sensitive. Inserting freeze
8416 -- nodes blindly in the tree may result in types being frozen too early.
8418 else
8419 loop
8420 -- Do not freeze the current expression if another expression in
8421 -- the chain of parents must not be frozen.
8423 if Nkind (P) in N_Subexpr and then Must_Not_Freeze (P) then
8424 return;
8425 end if;
8427 Parent_P := Parent (P);
8429 -- If we don't have a parent, then we are not in a well-formed
8430 -- tree. This is an unusual case, but there are some legitimate
8431 -- situations in which this occurs, notably when the expressions
8432 -- in the range of a type declaration are resolved. We simply
8433 -- ignore the freeze request in this case.
8435 if No (Parent_P) then
8436 return;
8437 end if;
8439 -- See if we have got to an appropriate point in the tree
8441 case Nkind (Parent_P) is
8443 -- A special test for the exception of (RM 13.14(8)) for the
8444 -- case of per-object expressions (RM 3.8(18)) occurring in
8445 -- component definition or a discrete subtype definition. Note
8446 -- that we test for a component declaration which includes both
8447 -- cases we are interested in, and furthermore the tree does
8448 -- not have explicit nodes for either of these two constructs.
8450 when N_Component_Declaration =>
8452 -- The case we want to test for here is an identifier that
8453 -- is a per-object expression, this is either a discriminant
8454 -- that appears in a context other than the component
8455 -- declaration or it is a reference to the type of the
8456 -- enclosing construct.
8458 -- For either of these cases, we skip the freezing
8460 if not In_Spec_Expression
8461 and then Nkind (N) = N_Identifier
8462 and then Present (Entity (N))
8463 then
8464 -- We recognize the discriminant case by just looking for
8465 -- a reference to a discriminant. It can only be one for
8466 -- the enclosing construct. Skip freezing in this case.
8468 if Ekind (Entity (N)) = E_Discriminant then
8469 return;
8471 -- For the case of a reference to the enclosing record,
8472 -- (or task or protected type), we look for a type that
8473 -- matches the current scope.
8475 elsif Entity (N) = Current_Scope then
8476 return;
8477 end if;
8478 end if;
8480 -- If we have an enumeration literal that appears as the choice
8481 -- in the aggregate of an enumeration representation clause,
8482 -- then freezing does not occur (RM 13.14(10)).
8484 when N_Enumeration_Representation_Clause =>
8486 -- The case we are looking for is an enumeration literal
8488 if Nkind (N) in N_Identifier | N_Character_Literal
8489 and then Is_Enumeration_Type (Etype (N))
8490 then
8491 -- If enumeration literal appears directly as the choice,
8492 -- do not freeze (this is the normal non-overloaded case)
8494 if Nkind (Parent (N)) = N_Component_Association
8495 and then First (Choices (Parent (N))) = N
8496 then
8497 return;
8499 -- If enumeration literal appears as the name of function
8500 -- which is the choice, then also do not freeze. This
8501 -- happens in the overloaded literal case, where the
8502 -- enumeration literal is temporarily changed to a
8503 -- function call for overloading analysis purposes.
8505 elsif Nkind (Parent (N)) = N_Function_Call
8506 and then Nkind (Parent (Parent (N))) =
8507 N_Component_Association
8508 and then First (Choices (Parent (Parent (N)))) =
8509 Parent (N)
8510 then
8511 return;
8512 end if;
8513 end if;
8515 -- Normally if the parent is a handled sequence of statements,
8516 -- then the current node must be a statement, and that is an
8517 -- appropriate place to insert a freeze node.
8519 when N_Handled_Sequence_Of_Statements =>
8521 -- An exception occurs when the sequence of statements is
8522 -- for an expander generated body that did not do the usual
8523 -- freeze all operation. In this case we usually want to
8524 -- freeze outside this body, not inside it, and we skip
8525 -- past the subprogram body that we are inside.
8527 if In_Expanded_Body (Parent_P) then
8528 declare
8529 Subp_Body : constant Node_Id := Parent (Parent_P);
8530 Spec_Id : Entity_Id;
8532 begin
8533 -- Freeze the entity only when it is declared inside
8534 -- the body of the expander generated procedure. This
8535 -- case is recognized by the subprogram scope of the
8536 -- entity or its type, which is either the spec of an
8537 -- enclosing body, or (in the case of init_procs for
8538 -- which there is no separate spec) the current scope.
8540 if Nkind (Subp_Body) = N_Subprogram_Body then
8541 declare
8542 S : Entity_Id;
8544 begin
8545 Spec_Id := Corresponding_Spec (Subp_Body);
8547 if Present (Typ) then
8548 S := Scope (Typ);
8549 elsif Present (Nam) then
8550 S := Scope (Nam);
8551 else
8552 S := Standard_Standard;
8553 end if;
8555 while S /= Standard_Standard
8556 and then not Is_Subprogram (S)
8557 loop
8558 S := Scope (S);
8559 end loop;
8561 if S = Spec_Id then
8562 exit;
8564 elsif Present (Typ)
8565 and then Scope (Typ) = Current_Scope
8566 and then
8567 Defining_Entity (Subp_Body) = Current_Scope
8568 then
8569 exit;
8570 end if;
8571 end;
8572 end if;
8574 -- If the entity is not frozen by an expression
8575 -- function that is not a completion, continue
8576 -- climbing the tree.
8578 if Nkind (Subp_Body) = N_Subprogram_Body
8579 and then Was_Expression_Function (Subp_Body)
8580 then
8581 null;
8583 -- Freeze outside the body
8585 else
8586 Parent_P := Parent (Parent_P);
8587 Freeze_Outside := True;
8588 end if;
8589 end;
8591 -- Here if normal case where we are in handled statement
8592 -- sequence and want to do the insertion right there.
8594 else
8595 exit;
8596 end if;
8598 -- If parent is a body or a spec or a block, then the current
8599 -- node is a statement or declaration and we can insert the
8600 -- freeze node before it.
8602 when N_Block_Statement
8603 | N_Entry_Body
8604 | N_Package_Body
8605 | N_Package_Specification
8606 | N_Protected_Body
8607 | N_Subprogram_Body
8608 | N_Task_Body
8610 exit;
8612 -- The expander is allowed to define types in any statements
8613 -- list, so any of the following parent nodes also mark a
8614 -- freezing point if the actual node is in a list of
8615 -- statements or declarations.
8617 when N_Abortable_Part
8618 | N_Accept_Alternative
8619 | N_Case_Statement_Alternative
8620 | N_Compilation_Unit_Aux
8621 | N_Conditional_Entry_Call
8622 | N_Delay_Alternative
8623 | N_Elsif_Part
8624 | N_Entry_Call_Alternative
8625 | N_Exception_Handler
8626 | N_Extended_Return_Statement
8627 | N_Freeze_Entity
8628 | N_If_Statement
8629 | N_Selective_Accept
8630 | N_Triggering_Alternative
8632 exit when Is_List_Member (P);
8634 -- The freeze nodes produced by an expression coming from the
8635 -- Actions list of an N_Expression_With_Actions, short-circuit
8636 -- expression or N_Case_Expression_Alternative node must remain
8637 -- within the Actions list if they freeze an entity declared in
8638 -- this list, as inserting the freeze nodes further up the tree
8639 -- may lead to use before declaration issues for the entity.
8641 when N_Case_Expression_Alternative
8642 | N_Expression_With_Actions
8643 | N_Short_Circuit
8645 exit when (Present (Nam)
8646 and then
8647 Has_Decl_In_List (Nam, P, Actions (Parent_P)))
8648 or else (Present (Typ)
8649 and then
8650 Has_Decl_In_List (Typ, P, Actions (Parent_P)));
8652 -- Likewise for an N_If_Expression and its two Actions list
8654 when N_If_Expression =>
8655 declare
8656 L1 : constant List_Id := Then_Actions (Parent_P);
8657 L2 : constant List_Id := Else_Actions (Parent_P);
8659 begin
8660 exit when (Present (Nam)
8661 and then
8662 Has_Decl_In_List (Nam, P, L1))
8663 or else (Present (Typ)
8664 and then
8665 Has_Decl_In_List (Typ, P, L1))
8666 or else (Present (Nam)
8667 and then
8668 Has_Decl_In_List (Nam, P, L2))
8669 or else (Present (Typ)
8670 and then
8671 Has_Decl_In_List (Typ, P, L2));
8672 end;
8674 -- N_Loop_Statement is a special case: a type that appears in
8675 -- the source can never be frozen in a loop (this occurs only
8676 -- because of a loop expanded by the expander), so we keep on
8677 -- going. Otherwise we terminate the search. Same is true of
8678 -- any entity which comes from source (if it has a predefined
8679 -- type, this type does not appear to come from source, but the
8680 -- entity should not be frozen here).
8682 when N_Loop_Statement =>
8683 exit when not Comes_From_Source (Etype (N))
8684 and then (No (Nam) or else not Comes_From_Source (Nam));
8686 -- For all other cases, keep looking at parents
8688 when others =>
8689 null;
8690 end case;
8692 -- We fall through the case if we did not yet find the proper
8693 -- place in the tree for inserting the freeze node, so climb.
8695 P := Parent_P;
8696 end loop;
8697 end if;
8699 -- If the expression appears in a record or an initialization procedure,
8700 -- the freeze nodes are collected and attached to the current scope, to
8701 -- be inserted and analyzed on exit from the scope, to insure that
8702 -- generated entities appear in the correct scope. If the expression is
8703 -- a default for a discriminant specification, the scope is still void.
8704 -- The expression can also appear in the discriminant part of a private
8705 -- or concurrent type.
8707 -- If the expression appears in a constrained subcomponent of an
8708 -- enclosing record declaration, the freeze nodes must be attached to
8709 -- the outer record type so they can eventually be placed in the
8710 -- enclosing declaration list.
8712 -- The other case requiring this special handling is if we are in a
8713 -- default expression, since in that case we are about to freeze a
8714 -- static type, and the freeze scope needs to be the outer scope, not
8715 -- the scope of the subprogram with the default parameter.
8717 -- For default expressions and other spec expressions in generic units,
8718 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
8719 -- placing them at the proper place, after the generic unit.
8721 if (In_Spec_Exp and not Inside_A_Generic)
8722 or else Freeze_Outside
8723 or else (Is_Type (Current_Scope)
8724 and then (not Is_Concurrent_Type (Current_Scope)
8725 or else not Has_Completion (Current_Scope)))
8726 or else Ekind (Current_Scope) = E_Void
8727 then
8728 declare
8729 Freeze_Nodes : List_Id := No_List;
8730 Pos : Int := Scope_Stack.Last;
8732 begin
8733 if Present (Desig_Typ) then
8734 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
8735 end if;
8737 if Present (Typ) then
8738 Freeze_And_Append (Typ, N, Freeze_Nodes);
8739 end if;
8741 if Present (Nam) then
8742 Freeze_And_Append (Nam, N, Freeze_Nodes);
8743 end if;
8745 -- The current scope may be that of a constrained component of
8746 -- an enclosing record declaration, or of a loop of an enclosing
8747 -- quantified expression or aggregate with an iterated component
8748 -- in Ada 2022, which is above the current scope in the scope
8749 -- stack. Indeed in the context of a quantified expression or
8750 -- an aggregate with an iterated component, an internal scope is
8751 -- created and pushed above the current scope in order to emulate
8752 -- the loop-like behavior of the construct.
8753 -- If the expression is within a top-level pragma, as for a pre-
8754 -- condition on a library-level subprogram, nothing to do.
8756 if not Is_Compilation_Unit (Current_Scope)
8757 and then (Is_Record_Type (Scope (Current_Scope))
8758 or else (Ekind (Current_Scope) = E_Loop
8759 and then Is_Internal (Current_Scope)))
8760 then
8761 Pos := Pos - 1;
8762 end if;
8764 if Is_Non_Empty_List (Freeze_Nodes) then
8766 -- When the current scope is transient, insert the freeze nodes
8767 -- prior to the expression that produced them. Transient scopes
8768 -- may create additional declarations when finalizing objects
8769 -- or managing the secondary stack. Inserting the freeze nodes
8770 -- of those constructs prior to the scope would result in a
8771 -- freeze-before-declaration, therefore the freeze node must
8772 -- remain interleaved with their constructs.
8774 if Scope_Is_Transient then
8775 Insert_Actions (N, Freeze_Nodes);
8777 elsif No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
8778 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
8779 Freeze_Nodes;
8780 else
8781 Append_List (Freeze_Nodes,
8782 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
8783 end if;
8784 end if;
8785 end;
8787 return;
8788 end if;
8790 -- Now we have the right place to do the freezing. First, a special
8791 -- adjustment, if we are in spec-expression analysis mode, these freeze
8792 -- actions must not be thrown away (normally all inserted actions are
8793 -- thrown away in this mode. However, the freeze actions are from static
8794 -- expressions and one of the important reasons we are doing this
8795 -- special analysis is to get these freeze actions. Therefore we turn
8796 -- off the In_Spec_Expression mode to propagate these freeze actions.
8797 -- This also means they get properly analyzed and expanded.
8799 In_Spec_Expression := False;
8801 -- Freeze the subtype mark before a qualified expression on an
8802 -- allocator as per AARM 13.14(4.a). This is needed in particular to
8803 -- generate predicate functions.
8805 if Present (Allocator_Typ) then
8806 Freeze_Before (P, Allocator_Typ);
8807 end if;
8809 -- Freeze the designated type of an allocator (RM 13.14(13))
8811 if Present (Desig_Typ) then
8812 Freeze_Before (P, Desig_Typ);
8813 end if;
8815 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
8816 -- the enumeration representation clause exception in the loop above.
8818 if Present (Typ) then
8819 Freeze_Before (P, Typ);
8820 end if;
8822 -- Freeze name if one is present (RM 13.14(11))
8824 if Present (Nam) then
8825 Freeze_Before (P, Nam);
8826 end if;
8828 -- Restore In_Spec_Expression flag
8830 In_Spec_Expression := In_Spec_Exp;
8831 end Freeze_Expression;
8833 -----------------------
8834 -- Freeze_Expr_Types --
8835 -----------------------
8837 procedure Freeze_Expr_Types
8838 (Def_Id : Entity_Id;
8839 Typ : Entity_Id;
8840 Expr : Node_Id;
8841 N : Node_Id)
8843 function Cloned_Expression return Node_Id;
8844 -- Build a duplicate of the expression of the return statement that has
8845 -- no defining entities shared with the original expression.
8847 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result;
8848 -- Freeze all types referenced in the subtree rooted at Node
8850 -----------------------
8851 -- Cloned_Expression --
8852 -----------------------
8854 function Cloned_Expression return Node_Id is
8855 function Clone_Id (Node : Node_Id) return Traverse_Result;
8856 -- Tree traversal routine that clones the defining identifier of
8857 -- iterator and loop parameter specification nodes.
8859 --------------
8860 -- Clone_Id --
8861 --------------
8863 function Clone_Id (Node : Node_Id) return Traverse_Result is
8864 begin
8865 if Nkind (Node) in
8866 N_Iterator_Specification | N_Loop_Parameter_Specification
8867 then
8868 Set_Defining_Identifier
8869 (Node, New_Copy (Defining_Identifier (Node)));
8870 end if;
8872 return OK;
8873 end Clone_Id;
8875 procedure Clone_Def_Ids is new Traverse_Proc (Clone_Id);
8877 -- Local variable
8879 Dup_Expr : constant Node_Id := New_Copy_Tree (Expr);
8881 -- Start of processing for Cloned_Expression
8883 begin
8884 -- We must duplicate the expression with semantic information to
8885 -- inherit the decoration of global entities in generic instances.
8886 -- Set the parent of the new node to be the parent of the original
8887 -- to get the proper context, which is needed for complete error
8888 -- reporting and for semantic analysis.
8890 Set_Parent (Dup_Expr, Parent (Expr));
8892 -- Replace the defining identifier of iterators and loop param
8893 -- specifications by a clone to ensure that the cloned expression
8894 -- and the original expression don't have shared identifiers;
8895 -- otherwise, as part of the preanalysis of the expression, these
8896 -- shared identifiers may be left decorated with itypes which
8897 -- will not be available in the tree passed to the backend.
8899 Clone_Def_Ids (Dup_Expr);
8901 return Dup_Expr;
8902 end Cloned_Expression;
8904 ----------------------
8905 -- Freeze_Type_Refs --
8906 ----------------------
8908 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result is
8909 procedure Check_And_Freeze_Type (Typ : Entity_Id);
8910 -- Check that Typ is fully declared and freeze it if so
8912 ---------------------------
8913 -- Check_And_Freeze_Type --
8914 ---------------------------
8916 procedure Check_And_Freeze_Type (Typ : Entity_Id) is
8917 begin
8918 -- Skip Itypes created by the preanalysis, and itypes whose
8919 -- scope is another type (i.e. component subtypes that depend
8920 -- on a discriminant),
8922 if Is_Itype (Typ)
8923 and then (Scope_Within_Or_Same (Scope (Typ), Def_Id)
8924 or else Is_Type (Scope (Typ)))
8925 then
8926 return;
8927 end if;
8929 -- This provides a better error message than generating primitives
8930 -- whose compilation fails much later. Refine the error message if
8931 -- possible.
8933 Check_Fully_Declared (Typ, Node);
8935 if Error_Posted (Node) then
8936 if Has_Private_Component (Typ)
8937 and then not Is_Private_Type (Typ)
8938 then
8939 Error_Msg_NE ("\type& has private component", Node, Typ);
8940 end if;
8942 else
8943 Freeze_Before (N, Typ);
8944 end if;
8945 end Check_And_Freeze_Type;
8947 -- Start of processing for Freeze_Type_Refs
8949 begin
8950 -- Check that a type referenced by an entity can be frozen
8952 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
8953 -- The entity itself may be a type, as in a membership test
8954 -- or an attribute reference. Freezing its own type would be
8955 -- incomplete if the entity is derived or an extension.
8957 if Is_Type (Entity (Node)) then
8958 Check_And_Freeze_Type (Entity (Node));
8960 else
8961 Check_And_Freeze_Type (Etype (Entity (Node)));
8962 end if;
8964 -- Check that the enclosing record type can be frozen
8966 if Ekind (Entity (Node)) in E_Component | E_Discriminant then
8967 Check_And_Freeze_Type (Scope (Entity (Node)));
8968 end if;
8970 -- Freezing an access type does not freeze the designated type, but
8971 -- freezing conversions between access to interfaces requires that
8972 -- the interface types themselves be frozen, so that dispatch table
8973 -- entities are properly created.
8975 -- Unclear whether a more general rule is needed ???
8977 elsif Nkind (Node) = N_Type_Conversion
8978 and then Is_Access_Type (Etype (Node))
8979 and then Is_Interface (Designated_Type (Etype (Node)))
8980 then
8981 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8982 end if;
8984 -- An implicit dereference freezes the designated type. In the case
8985 -- of a dispatching call whose controlling argument is an access
8986 -- type, the dereference is not made explicit, so we must check for
8987 -- such a call and freeze the designated type.
8989 if Nkind (Node) in N_Has_Etype
8990 and then Present (Etype (Node))
8991 and then Is_Access_Type (Etype (Node))
8992 then
8993 if Nkind (Parent (Node)) = N_Function_Call
8994 and then Node = Controlling_Argument (Parent (Node))
8995 then
8996 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
8998 -- An explicit dereference freezes the designated type as well,
8999 -- even though that type is not attached to an entity in the
9000 -- expression.
9002 elsif Nkind (Parent (Node)) = N_Explicit_Dereference then
9003 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
9004 end if;
9006 -- An iterator specification freezes the iterator type, even though
9007 -- that type is not attached to an entity in the construct.
9009 elsif Nkind (Node) in N_Has_Etype
9010 and then Nkind (Parent (Node)) = N_Iterator_Specification
9011 and then Node = Name (Parent (Node))
9012 then
9013 declare
9014 Iter : constant Node_Id :=
9015 Find_Value_Of_Aspect (Etype (Node), Aspect_Default_Iterator);
9017 begin
9018 if Present (Iter) then
9019 Check_And_Freeze_Type (Etype (Iter));
9020 end if;
9021 end;
9022 end if;
9024 -- No point in posting several errors on the same expression
9026 if Serious_Errors_Detected > 0 then
9027 return Abandon;
9028 else
9029 return OK;
9030 end if;
9031 end Freeze_Type_Refs;
9033 procedure Freeze_References is new Traverse_Proc (Freeze_Type_Refs);
9035 -- Local variables
9037 Saved_First_Entity : constant Entity_Id := First_Entity (Def_Id);
9038 Saved_Last_Entity : constant Entity_Id := Last_Entity (Def_Id);
9039 Dup_Expr : constant Node_Id := Cloned_Expression;
9041 -- Start of processing for Freeze_Expr_Types
9043 begin
9044 -- Preanalyze a duplicate of the expression to have available the
9045 -- minimum decoration needed to locate referenced unfrozen types
9046 -- without adding any decoration to the function expression.
9048 -- This routine is also applied to expressions in the contract for
9049 -- the subprogram. If that happens when expanding the code for
9050 -- pre/postconditions during expansion of the subprogram body, the
9051 -- subprogram is already installed.
9053 if Def_Id /= Current_Scope then
9054 Push_Scope (Def_Id);
9055 Install_Formals (Def_Id);
9057 Preanalyze_Spec_Expression (Dup_Expr, Typ);
9058 End_Scope;
9059 else
9060 Preanalyze_Spec_Expression (Dup_Expr, Typ);
9061 end if;
9063 -- Restore certain attributes of Def_Id since the preanalysis may
9064 -- have introduced itypes to this scope, thus modifying attributes
9065 -- First_Entity and Last_Entity.
9067 Set_First_Entity (Def_Id, Saved_First_Entity);
9068 Set_Last_Entity (Def_Id, Saved_Last_Entity);
9070 if Present (Last_Entity (Def_Id)) then
9071 Set_Next_Entity (Last_Entity (Def_Id), Empty);
9072 end if;
9074 -- Freeze all types referenced in the expression
9076 Freeze_References (Dup_Expr);
9077 end Freeze_Expr_Types;
9079 -----------------------------
9080 -- Freeze_Fixed_Point_Type --
9081 -----------------------------
9083 -- Certain fixed-point types and subtypes, including implicit base types
9084 -- and declared first subtypes, have not yet set up a range. This is
9085 -- because the range cannot be set until the Small and Size values are
9086 -- known, and these are not known till the type is frozen.
9088 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
9089 -- whose bounds are unanalyzed real literals. This routine will recognize
9090 -- this case, and transform this range node into a properly typed range
9091 -- with properly analyzed and resolved values.
9093 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
9094 Rng : constant Node_Id := Scalar_Range (Typ);
9095 Lo : constant Node_Id := Low_Bound (Rng);
9096 Hi : constant Node_Id := High_Bound (Rng);
9097 Btyp : constant Entity_Id := Base_Type (Typ);
9098 Brng : constant Node_Id := Scalar_Range (Btyp);
9099 BLo : constant Node_Id := Low_Bound (Brng);
9100 BHi : constant Node_Id := High_Bound (Brng);
9101 Ftyp : constant Entity_Id := Underlying_Type (First_Subtype (Typ));
9103 Small : Ureal;
9104 Loval : Ureal;
9105 Hival : Ureal;
9106 Atype : Entity_Id;
9108 Orig_Lo : Ureal;
9109 Orig_Hi : Ureal;
9110 -- Save original bounds (for shaving tests)
9112 Actual_Size : Int;
9113 -- Actual size chosen
9115 function Fsize (Lov, Hiv : Ureal) return Int;
9116 -- Returns size of type with given bounds. Also leaves these
9117 -- bounds set as the current bounds of the Typ.
9119 function Larger (A, B : Ureal) return Boolean;
9120 -- Returns true if A > B with a margin of Typ'Small
9122 function Smaller (A, B : Ureal) return Boolean;
9123 -- Returns true if A < B with a margin of Typ'Small
9125 -----------
9126 -- Fsize --
9127 -----------
9129 function Fsize (Lov, Hiv : Ureal) return Int is
9130 begin
9131 Set_Realval (Lo, Lov);
9132 Set_Realval (Hi, Hiv);
9133 return Minimum_Size (Typ);
9134 end Fsize;
9136 ------------
9137 -- Larger --
9138 ------------
9140 function Larger (A, B : Ureal) return Boolean is
9141 begin
9142 return A > B and then A - Small_Value (Typ) > B;
9143 end Larger;
9145 -------------
9146 -- Smaller --
9147 -------------
9149 function Smaller (A, B : Ureal) return Boolean is
9150 begin
9151 return A < B and then A + Small_Value (Typ) < B;
9152 end Smaller;
9154 -- Start of processing for Freeze_Fixed_Point_Type
9156 begin
9157 -- The type, or its first subtype if we are freezing the anonymous
9158 -- base, may have a delayed Small aspect. It must be analyzed now,
9159 -- so that all characteristics of the type (size, bounds) can be
9160 -- computed and validated in the call to Minimum_Size that follows.
9162 if Has_Delayed_Aspects (Ftyp) then
9163 Analyze_Aspects_At_Freeze_Point (Ftyp);
9164 Set_Has_Delayed_Aspects (Ftyp, False);
9165 end if;
9167 if May_Inherit_Delayed_Rep_Aspects (Ftyp) then
9168 Inherit_Delayed_Rep_Aspects (Ftyp);
9169 Set_May_Inherit_Delayed_Rep_Aspects (Ftyp, False);
9170 end if;
9172 -- Inherit the Small value from the first subtype in any case
9174 if Typ /= Ftyp then
9175 Set_Small_Value (Typ, Small_Value (Ftyp));
9176 end if;
9178 -- If Esize of a subtype has not previously been set, set it now
9180 if not Known_Esize (Typ) then
9181 Atype := Ancestor_Subtype (Typ);
9183 if Present (Atype) then
9184 Set_Esize (Typ, Esize (Atype));
9185 else
9186 Copy_Esize (To => Typ, From => Btyp);
9187 end if;
9188 end if;
9190 -- Immediate return if the range is already analyzed. This means that
9191 -- the range is already set, and does not need to be computed by this
9192 -- routine.
9194 if Analyzed (Rng) then
9195 return;
9196 end if;
9198 -- Immediate return if either of the bounds raises Constraint_Error
9200 if Raises_Constraint_Error (Lo)
9201 or else Raises_Constraint_Error (Hi)
9202 then
9203 return;
9204 end if;
9206 Small := Small_Value (Typ);
9207 Loval := Realval (Lo);
9208 Hival := Realval (Hi);
9210 Orig_Lo := Loval;
9211 Orig_Hi := Hival;
9213 -- Ordinary fixed-point case
9215 if Is_Ordinary_Fixed_Point_Type (Typ) then
9217 -- For the ordinary fixed-point case, we are allowed to fudge the
9218 -- end-points up or down by small. Generally we prefer to fudge up,
9219 -- i.e. widen the bounds for non-model numbers so that the end points
9220 -- are included. However there are cases in which this cannot be
9221 -- done, and indeed cases in which we may need to narrow the bounds.
9222 -- The following circuit makes the decision.
9224 -- Note: our terminology here is that Incl_EP means that the bounds
9225 -- are widened by Small if necessary to include the end points, and
9226 -- Excl_EP means that the bounds are narrowed by Small to exclude the
9227 -- end-points if this reduces the size.
9229 -- Note that in the Incl case, all we care about is including the
9230 -- end-points. In the Excl case, we want to narrow the bounds as
9231 -- much as permitted by the RM, to give the smallest possible size.
9233 Fudge : declare
9234 Loval_Incl_EP : Ureal;
9235 Hival_Incl_EP : Ureal;
9237 Loval_Excl_EP : Ureal;
9238 Hival_Excl_EP : Ureal;
9240 Size_Incl_EP : Int;
9241 Size_Excl_EP : Int;
9243 Model_Num : Ureal;
9244 Actual_Lo : Ureal;
9245 Actual_Hi : Ureal;
9247 begin
9248 -- First step. Base types are required to be symmetrical. Right
9249 -- now, the base type range is a copy of the first subtype range.
9250 -- This will be corrected before we are done, but right away we
9251 -- need to deal with the case where both bounds are non-negative.
9252 -- In this case, we set the low bound to the negative of the high
9253 -- bound, to make sure that the size is computed to include the
9254 -- required sign. Note that we do not need to worry about the
9255 -- case of both bounds negative, because the sign will be dealt
9256 -- with anyway. Furthermore we can't just go making such a bound
9257 -- symmetrical, since in a twos-complement system, there is an
9258 -- extra negative value which could not be accommodated on the
9259 -- positive side.
9261 if Typ = Btyp
9262 and then not UR_Is_Negative (Loval)
9263 and then Hival > Loval
9264 then
9265 Loval := -Hival;
9266 Set_Realval (Lo, Loval);
9267 end if;
9269 -- Compute the fudged bounds. If the bound is a model number, (or
9270 -- greater if given low bound, smaller if high bound) then we do
9271 -- nothing to include it, but we are allowed to backoff to the
9272 -- next adjacent model number when we exclude it. If it is not a
9273 -- model number then we straddle the two values with the model
9274 -- numbers on either side.
9276 Model_Num := UR_Trunc (Loval / Small) * Small;
9278 if UR_Ge (Loval, Model_Num) then
9279 Loval_Incl_EP := Model_Num;
9280 else
9281 Loval_Incl_EP := Model_Num - Small;
9282 end if;
9284 -- The low value excluding the end point is Small greater, but
9285 -- we do not do this exclusion if the low value is positive,
9286 -- since it can't help the size and could actually hurt by
9287 -- crossing the high bound.
9289 if UR_Is_Negative (Loval_Incl_EP) then
9290 Loval_Excl_EP := Loval_Incl_EP + Small;
9292 -- If the value went from negative to zero, then we have the
9293 -- case where Loval_Incl_EP is the model number just below
9294 -- zero, so we want to stick to the negative value for the
9295 -- base type to maintain the condition that the size will
9296 -- include signed values.
9298 if Typ = Btyp
9299 and then UR_Is_Zero (Loval_Excl_EP)
9300 then
9301 Loval_Excl_EP := Loval_Incl_EP;
9302 end if;
9304 else
9305 Loval_Excl_EP := Loval_Incl_EP;
9306 end if;
9308 -- Similar processing for upper bound and high value
9310 Model_Num := UR_Trunc (Hival / Small) * Small;
9312 if UR_Le (Hival, Model_Num) then
9313 Hival_Incl_EP := Model_Num;
9314 else
9315 Hival_Incl_EP := Model_Num + Small;
9316 end if;
9318 if UR_Is_Positive (Hival_Incl_EP) then
9319 Hival_Excl_EP := Hival_Incl_EP - Small;
9320 else
9321 Hival_Excl_EP := Hival_Incl_EP;
9322 end if;
9324 -- One further adjustment is needed. In the case of subtypes, we
9325 -- cannot go outside the range of the base type, or we get
9326 -- peculiarities, and the base type range is already set. This
9327 -- only applies to the Incl values, since clearly the Excl values
9328 -- are already as restricted as they are allowed to be.
9330 if Typ /= Btyp then
9331 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
9332 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
9333 end if;
9335 -- Get size including and excluding end points
9337 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
9338 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
9340 -- No need to exclude end-points if it does not reduce size
9342 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
9343 Loval_Excl_EP := Loval_Incl_EP;
9344 end if;
9346 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
9347 Hival_Excl_EP := Hival_Incl_EP;
9348 end if;
9350 -- Now we set the actual size to be used. We want to use the
9351 -- bounds fudged up to include the end-points but only if this
9352 -- can be done without violating a specifically given size
9353 -- size clause or causing an unacceptable increase in size.
9355 -- Case of size clause given
9357 if Has_Size_Clause (Typ) then
9359 -- Use the inclusive size only if it is consistent with
9360 -- the explicitly specified size.
9362 if Size_Incl_EP <= RM_Size (Typ) then
9363 Actual_Lo := Loval_Incl_EP;
9364 Actual_Hi := Hival_Incl_EP;
9365 Actual_Size := Size_Incl_EP;
9367 -- If the inclusive size is too large, we try excluding
9368 -- the end-points (will be caught later if does not work).
9370 else
9371 Actual_Lo := Loval_Excl_EP;
9372 Actual_Hi := Hival_Excl_EP;
9373 Actual_Size := Size_Excl_EP;
9374 end if;
9376 -- Case of size clause not given
9378 else
9379 -- If we have a base type whose corresponding first subtype
9380 -- has an explicit size that is large enough to include our
9381 -- end-points, then do so. There is no point in working hard
9382 -- to get a base type whose size is smaller than the specified
9383 -- size of the first subtype.
9385 if Has_Size_Clause (Ftyp)
9386 and then Size_Incl_EP <= Esize (Ftyp)
9387 then
9388 Actual_Size := Size_Incl_EP;
9389 Actual_Lo := Loval_Incl_EP;
9390 Actual_Hi := Hival_Incl_EP;
9392 -- If excluding the end-points makes the size smaller and
9393 -- results in a size of 8,16,32,64, then we take the smaller
9394 -- size. For the 64 case, this is compulsory. For the other
9395 -- cases, it seems reasonable. We like to include end points
9396 -- if we can, but not at the expense of moving to the next
9397 -- natural boundary of size.
9399 elsif Size_Incl_EP /= Size_Excl_EP
9400 and then Addressable (Size_Excl_EP)
9401 then
9402 Actual_Size := Size_Excl_EP;
9403 Actual_Lo := Loval_Excl_EP;
9404 Actual_Hi := Hival_Excl_EP;
9406 -- Otherwise we can definitely include the end points
9408 else
9409 Actual_Size := Size_Incl_EP;
9410 Actual_Lo := Loval_Incl_EP;
9411 Actual_Hi := Hival_Incl_EP;
9412 end if;
9414 -- One pathological case: normally we never fudge a low bound
9415 -- down, since it would seem to increase the size (if it has
9416 -- any effect), but for ranges containing single value, or no
9417 -- values, the high bound can be small too large. Consider:
9419 -- type t is delta 2.0**(-14)
9420 -- range 131072.0 .. 0;
9422 -- That lower bound is *just* outside the range of 32 bits, and
9423 -- does need fudging down in this case. Note that the bounds
9424 -- will always have crossed here, since the high bound will be
9425 -- fudged down if necessary, as in the case of:
9427 -- type t is delta 2.0**(-14)
9428 -- range 131072.0 .. 131072.0;
9430 -- So we detect the situation by looking for crossed bounds,
9431 -- and if the bounds are crossed, and the low bound is greater
9432 -- than zero, we will always back it off by small, since this
9433 -- is completely harmless.
9435 if Actual_Lo > Actual_Hi then
9436 if UR_Is_Positive (Actual_Lo) then
9437 Actual_Lo := Loval_Incl_EP - Small;
9438 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
9440 -- And of course, we need to do exactly the same parallel
9441 -- fudge for flat ranges in the negative region.
9443 elsif UR_Is_Negative (Actual_Hi) then
9444 Actual_Hi := Hival_Incl_EP + Small;
9445 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
9446 end if;
9447 end if;
9448 end if;
9450 Set_Realval (Lo, Actual_Lo);
9451 Set_Realval (Hi, Actual_Hi);
9452 end Fudge;
9454 -- Enforce some limitations for ordinary fixed-point types. They come
9455 -- from an exact algorithm used to implement Text_IO.Fixed_IO and the
9456 -- Fore, Image and Value attributes. The requirement on the Small is
9457 -- to lie in the range 2**(-(Siz - 1)) .. 2**(Siz - 1) for a type of
9458 -- Siz bits (Siz=32,64,128) and the requirement on the bounds is to
9459 -- be smaller in magnitude than 10.0**N * 2**(Siz - 1), where N is
9460 -- given by the formula N = floor ((Siz - 1) * log 2 / log 10).
9462 -- If the bounds of a 32-bit type are too large, force 64-bit type
9464 if Actual_Size <= 32
9465 and then Small <= Ureal_2_31
9466 and then (Smaller (Expr_Value_R (Lo), Ureal_M_2_10_18)
9467 or else Larger (Expr_Value_R (Hi), Ureal_2_10_18))
9468 then
9469 Actual_Size := 33;
9470 end if;
9472 -- If the bounds of a 64-bit type are too large, force 128-bit type
9474 if System_Max_Integer_Size = 128
9475 and then Actual_Size <= 64
9476 and then Small <= Ureal_2_63
9477 and then (Smaller (Expr_Value_R (Lo), Ureal_M_9_10_36)
9478 or else Larger (Expr_Value_R (Hi), Ureal_9_10_36))
9479 then
9480 Actual_Size := 65;
9481 end if;
9483 -- Give error messages for first subtypes and not base types, as the
9484 -- bounds of base types are always maximum for their size, see below.
9486 if System_Max_Integer_Size < 128 and then Typ /= Btyp then
9488 -- See the 128-bit case below for the reason why we cannot test
9489 -- against the 2**(-63) .. 2**63 range. This quirk should have
9490 -- been kludged around as in the 128-bit case below, but it was
9491 -- not and we end up with a ludicrous range as a result???
9493 if Small < Ureal_2_M_80 then
9494 Error_Msg_Name_1 := Name_Small;
9495 Error_Msg_N
9496 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", Typ);
9498 elsif Small > Ureal_2_80 then
9499 Error_Msg_Name_1 := Name_Small;
9500 Error_Msg_N
9501 ("`&''%` too large, maximum allowed is 2.0'*'*80", Typ);
9502 end if;
9504 if Smaller (Expr_Value_R (Lo), Ureal_M_9_10_36) then
9505 Error_Msg_Name_1 := Name_First;
9506 Error_Msg_N
9507 ("`&''%` too small, minimum allowed is -9.0E+36", Typ);
9508 end if;
9510 if Larger (Expr_Value_R (Hi), Ureal_9_10_36) then
9511 Error_Msg_Name_1 := Name_Last;
9512 Error_Msg_N
9513 ("`&''%` too large, maximum allowed is 9.0E+36", Typ);
9514 end if;
9516 elsif System_Max_Integer_Size = 128 and then Typ /= Btyp then
9518 -- ACATS c35902d tests a delta equal to 2**(-(Max_Mantissa + 1))
9519 -- but we cannot really support anything smaller than Fine_Delta
9520 -- because of the way we implement I/O for fixed point types???
9522 if Small = Ureal_2_M_128 then
9523 null;
9525 elsif Small < Ureal_2_M_127 then
9526 Error_Msg_Name_1 := Name_Small;
9527 Error_Msg_N
9528 ("`&''%` too small, minimum allowed is 2.0'*'*(-127)", Typ);
9530 elsif Small > Ureal_2_127 then
9531 Error_Msg_Name_1 := Name_Small;
9532 Error_Msg_N
9533 ("`&''%` too large, maximum allowed is 2.0'*'*127", Typ);
9534 end if;
9536 if Actual_Size > 64
9537 and then (Norm_Num (Small) > Uint_2 ** 127
9538 or else Norm_Den (Small) > Uint_2 ** 127)
9539 and then Small /= Ureal_2_M_128
9540 then
9541 Error_Msg_Name_1 := Name_Small;
9542 Error_Msg_N
9543 ("`&''%` not the ratio of two 128-bit integers", Typ);
9544 end if;
9546 if Smaller (Expr_Value_R (Lo), Ureal_M_10_76) then
9547 Error_Msg_Name_1 := Name_First;
9548 Error_Msg_N
9549 ("`&''%` too small, minimum allowed is -1.0E+76", Typ);
9550 end if;
9552 if Larger (Expr_Value_R (Hi), Ureal_10_76) then
9553 Error_Msg_Name_1 := Name_Last;
9554 Error_Msg_N
9555 ("`&''%` too large, maximum allowed is 1.0E+76", Typ);
9556 end if;
9557 end if;
9559 -- For the decimal case, none of this fudging is required, since there
9560 -- are no end-point problems in the decimal case (the end-points are
9561 -- always included).
9563 else
9564 Actual_Size := Fsize (Loval, Hival);
9565 end if;
9567 -- At this stage, the actual size has been calculated and the proper
9568 -- required bounds are stored in the low and high bounds.
9570 if Actual_Size > System_Max_Integer_Size then
9571 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
9572 Error_Msg_Uint_2 := UI_From_Int (System_Max_Integer_Size);
9573 Error_Msg_N
9574 ("size required (^) for type& too large, maximum allowed is ^",
9575 Typ);
9576 Actual_Size := System_Max_Integer_Size;
9577 end if;
9579 -- Check size against explicit given size
9581 if Has_Size_Clause (Typ) then
9582 if Actual_Size > RM_Size (Typ) then
9583 Error_Msg_Uint_1 := RM_Size (Typ);
9584 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
9585 Error_Msg_NE
9586 ("size given (^) for type& too small, minimum allowed is ^",
9587 Size_Clause (Typ), Typ);
9589 else
9590 Actual_Size := UI_To_Int (Esize (Typ));
9591 end if;
9593 -- Increase size to next natural boundary if no size clause given
9595 else
9596 if Actual_Size <= 8 then
9597 Actual_Size := 8;
9598 elsif Actual_Size <= 16 then
9599 Actual_Size := 16;
9600 elsif Actual_Size <= 32 then
9601 Actual_Size := 32;
9602 elsif Actual_Size <= 64 then
9603 Actual_Size := 64;
9604 else
9605 Actual_Size := 128;
9606 end if;
9608 Set_Esize (Typ, UI_From_Int (Actual_Size));
9609 Adjust_Esize_For_Alignment (Typ);
9610 end if;
9612 -- If we have a base type, then expand the bounds so that they extend to
9613 -- the full width of the allocated size in bits, to avoid junk range
9614 -- checks on intermediate computations.
9616 if Typ = Btyp then
9617 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
9618 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
9619 end if;
9621 -- Final step is to reanalyze the bounds using the proper type
9622 -- and set the Corresponding_Integer_Value fields of the literals.
9624 Set_Etype (Lo, Empty);
9625 Set_Analyzed (Lo, False);
9626 Analyze (Lo);
9628 -- Resolve with universal fixed if the base type, and with the base
9629 -- type if we are freezing a subtype. Note we can't resolve the base
9630 -- type with itself, that would be a reference before definition.
9631 -- The resolution of the bounds of a subtype, if they are given by real
9632 -- literals, includes the setting of the Corresponding_Integer_Value,
9633 -- as for other literals of a fixed-point type.
9635 if Typ = Btyp then
9636 Resolve (Lo, Universal_Fixed);
9637 Set_Corresponding_Integer_Value
9638 (Lo, UR_To_Uint (Realval (Lo) / Small));
9639 else
9640 Resolve (Lo, Btyp);
9641 end if;
9643 -- Similar processing for high bound
9645 Set_Etype (Hi, Empty);
9646 Set_Analyzed (Hi, False);
9647 Analyze (Hi);
9649 if Typ = Btyp then
9650 Resolve (Hi, Universal_Fixed);
9651 Set_Corresponding_Integer_Value
9652 (Hi, UR_To_Uint (Realval (Hi) / Small));
9653 else
9654 Resolve (Hi, Btyp);
9655 end if;
9657 -- Set type of range to correspond to bounds
9659 Set_Etype (Rng, Etype (Lo));
9661 -- Set Esize to calculated size if not set already
9663 if not Known_Esize (Typ) then
9664 Set_Esize (Typ, UI_From_Int (Actual_Size));
9665 end if;
9667 -- Set RM_Size if not already set. If already set, check value
9669 declare
9670 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
9672 begin
9673 if Known_RM_Size (Typ) then
9674 if RM_Size (Typ) < Minsiz then
9675 Error_Msg_Uint_1 := RM_Size (Typ);
9676 Error_Msg_Uint_2 := Minsiz;
9677 Error_Msg_NE
9678 ("size given (^) for type& too small, minimum allowed is ^",
9679 Size_Clause (Typ), Typ);
9680 end if;
9682 else
9683 Set_RM_Size (Typ, Minsiz);
9684 end if;
9685 end;
9687 -- Check for shaving
9689 if Comes_From_Source (Typ) then
9691 -- In SPARK mode the given bounds must be strictly representable
9693 if SPARK_Mode = On then
9694 if Orig_Lo < Expr_Value_R (Lo) then
9695 Error_Msg_NE
9696 ("declared low bound of type & is outside type range",
9697 Lo, Typ);
9698 end if;
9700 if Orig_Hi > Expr_Value_R (Hi) then
9701 Error_Msg_NE
9702 ("declared high bound of type & is outside type range",
9703 Hi, Typ);
9704 end if;
9706 else
9707 if Orig_Lo < Expr_Value_R (Lo) then
9708 Error_Msg_N
9709 ("declared low bound of type & is outside type range??", Typ);
9710 Error_Msg_N
9711 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
9712 end if;
9714 if Orig_Hi > Expr_Value_R (Hi) then
9715 Error_Msg_N
9716 ("declared high bound of type & is outside type range??",
9717 Typ);
9718 Error_Msg_N
9719 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
9720 end if;
9721 end if;
9722 end if;
9723 end Freeze_Fixed_Point_Type;
9725 ------------------
9726 -- Freeze_Itype --
9727 ------------------
9729 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
9730 L : List_Id;
9732 begin
9733 Set_Has_Delayed_Freeze (T);
9734 L := Freeze_Entity (T, N);
9736 Insert_Actions (N, L);
9737 end Freeze_Itype;
9739 --------------------------
9740 -- Freeze_Static_Object --
9741 --------------------------
9743 procedure Freeze_Static_Object (E : Entity_Id) is
9745 Cannot_Be_Static : exception;
9746 -- Exception raised if the type of a static object cannot be made
9747 -- static. This happens if the type depends on non-global objects.
9749 procedure Ensure_Expression_Is_SA (N : Node_Id);
9750 -- Called to ensure that an expression used as part of a type definition
9751 -- is statically allocatable, which means that the expression type is
9752 -- statically allocatable, and the expression is either static, or a
9753 -- reference to a library level constant.
9755 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
9756 -- Called to mark a type as static, checking that it is possible
9757 -- to set the type as static. If it is not possible, then the
9758 -- exception Cannot_Be_Static is raised.
9760 -----------------------------
9761 -- Ensure_Expression_Is_SA --
9762 -----------------------------
9764 procedure Ensure_Expression_Is_SA (N : Node_Id) is
9765 Ent : Entity_Id;
9767 begin
9768 Ensure_Type_Is_SA (Etype (N));
9770 if Is_OK_Static_Expression (N) then
9771 return;
9773 elsif Nkind (N) = N_Identifier then
9774 Ent := Entity (N);
9776 if Present (Ent)
9777 and then Ekind (Ent) = E_Constant
9778 and then Is_Library_Level_Entity (Ent)
9779 then
9780 return;
9781 end if;
9782 end if;
9784 raise Cannot_Be_Static;
9785 end Ensure_Expression_Is_SA;
9787 -----------------------
9788 -- Ensure_Type_Is_SA --
9789 -----------------------
9791 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
9792 N : Node_Id;
9793 C : Entity_Id;
9795 begin
9796 -- If type is library level, we are all set
9798 if Is_Library_Level_Entity (Typ) then
9799 return;
9800 end if;
9802 -- We are also OK if the type already marked as statically allocated,
9803 -- which means we processed it before.
9805 if Is_Statically_Allocated (Typ) then
9806 return;
9807 end if;
9809 -- Mark type as statically allocated
9811 Set_Is_Statically_Allocated (Typ);
9813 -- Check that it is safe to statically allocate this type
9815 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
9816 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
9817 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
9819 elsif Is_Array_Type (Typ) then
9820 N := First_Index (Typ);
9821 while Present (N) loop
9822 Ensure_Type_Is_SA (Etype (N));
9823 Next_Index (N);
9824 end loop;
9826 Ensure_Type_Is_SA (Component_Type (Typ));
9828 elsif Is_Access_Type (Typ) then
9829 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
9831 declare
9832 F : Entity_Id;
9833 T : constant Entity_Id := Etype (Designated_Type (Typ));
9835 begin
9836 if T /= Standard_Void_Type then
9837 Ensure_Type_Is_SA (T);
9838 end if;
9840 F := First_Formal (Designated_Type (Typ));
9841 while Present (F) loop
9842 Ensure_Type_Is_SA (Etype (F));
9843 Next_Formal (F);
9844 end loop;
9845 end;
9847 else
9848 Ensure_Type_Is_SA (Designated_Type (Typ));
9849 end if;
9851 elsif Is_Record_Type (Typ) then
9852 C := First_Entity (Typ);
9853 while Present (C) loop
9854 if Ekind (C) = E_Discriminant
9855 or else Ekind (C) = E_Component
9856 then
9857 Ensure_Type_Is_SA (Etype (C));
9859 elsif Is_Type (C) then
9860 Ensure_Type_Is_SA (C);
9861 end if;
9863 Next_Entity (C);
9864 end loop;
9866 elsif Ekind (Typ) = E_Subprogram_Type then
9867 Ensure_Type_Is_SA (Etype (Typ));
9869 C := First_Formal (Typ);
9870 while Present (C) loop
9871 Ensure_Type_Is_SA (Etype (C));
9872 Next_Formal (C);
9873 end loop;
9875 else
9876 raise Cannot_Be_Static;
9877 end if;
9878 end Ensure_Type_Is_SA;
9880 -- Start of processing for Freeze_Static_Object
9882 begin
9883 Ensure_Type_Is_SA (Etype (E));
9885 exception
9886 when Cannot_Be_Static =>
9888 -- If the object that cannot be static is imported or exported, then
9889 -- issue an error message saying that this object cannot be imported
9890 -- or exported. If it has an address clause it is an overlay in the
9891 -- current partition and the static requirement is not relevant.
9892 -- Do not issue any error message when ignoring rep clauses.
9894 if Ignore_Rep_Clauses then
9895 null;
9897 elsif Is_Imported (E) then
9898 if No (Address_Clause (E)) then
9899 Error_Msg_N
9900 ("& cannot be imported (local type is not constant)", E);
9901 end if;
9903 -- Otherwise must be exported, something is wrong if compiler
9904 -- is marking something as statically allocated which cannot be).
9906 else pragma Assert (Is_Exported (E));
9907 Error_Msg_N
9908 ("& cannot be exported (local type is not constant)", E);
9909 end if;
9910 end Freeze_Static_Object;
9912 -----------------------
9913 -- Freeze_Subprogram --
9914 -----------------------
9916 procedure Freeze_Subprogram (E : Entity_Id) is
9918 procedure Set_Profile_Convention (Subp_Id : Entity_Id);
9919 -- Set the conventions of all anonymous access-to-subprogram formals and
9920 -- result subtype of subprogram Subp_Id to the convention of Subp_Id.
9922 ----------------------------
9923 -- Set_Profile_Convention --
9924 ----------------------------
9926 procedure Set_Profile_Convention (Subp_Id : Entity_Id) is
9927 Conv : constant Convention_Id := Convention (Subp_Id);
9929 procedure Set_Type_Convention (Typ : Entity_Id);
9930 -- Set the convention of anonymous access-to-subprogram type Typ and
9931 -- its designated type to Conv.
9933 -------------------------
9934 -- Set_Type_Convention --
9935 -------------------------
9937 procedure Set_Type_Convention (Typ : Entity_Id) is
9938 begin
9939 -- Set the convention on both the anonymous access-to-subprogram
9940 -- type and the subprogram type it points to because both types
9941 -- participate in conformance-related checks.
9943 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
9944 Set_Convention (Typ, Conv);
9945 Set_Convention (Designated_Type (Typ), Conv);
9946 end if;
9947 end Set_Type_Convention;
9949 -- Local variables
9951 Formal : Entity_Id;
9953 -- Start of processing for Set_Profile_Convention
9955 begin
9956 Formal := First_Formal (Subp_Id);
9957 while Present (Formal) loop
9958 Set_Type_Convention (Etype (Formal));
9959 Next_Formal (Formal);
9960 end loop;
9962 if Ekind (Subp_Id) = E_Function then
9963 Set_Type_Convention (Etype (Subp_Id));
9964 end if;
9965 end Set_Profile_Convention;
9967 -- Local variables
9969 F : Entity_Id;
9970 Retype : Entity_Id;
9972 -- Start of processing for Freeze_Subprogram
9974 begin
9975 -- Subprogram may not have an address clause unless it is imported
9977 if Present (Address_Clause (E)) then
9978 if not Is_Imported (E) then
9979 Error_Msg_N
9980 ("address clause can only be given for imported subprogram",
9981 Name (Address_Clause (E)));
9982 end if;
9983 end if;
9985 -- Reset the Pure indication on an imported subprogram unless an
9986 -- explicit Pure_Function pragma was present or the subprogram is an
9987 -- intrinsic. We do this because otherwise it is an insidious error
9988 -- to call a non-pure function from pure unit and have calls
9989 -- mysteriously optimized away. What happens here is that the Import
9990 -- can bypass the normal check to ensure that pure units call only pure
9991 -- subprograms.
9993 -- The reason for the intrinsic exception is that in general, intrinsic
9994 -- functions (such as shifts) are pure anyway. The only exceptions are
9995 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
9996 -- in any case, so no problem arises.
9998 if Is_Imported (E)
9999 and then Is_Pure (E)
10000 and then not Has_Pragma_Pure_Function (E)
10001 and then not Is_Intrinsic_Subprogram (E)
10002 then
10003 Set_Is_Pure (E, False);
10004 end if;
10006 -- For C++ constructors check that their external name has been given
10007 -- (either in pragma CPP_Constructor or in a pragma import).
10009 if Is_Constructor (E)
10010 and then Convention (E) = Convention_CPP
10011 and then
10012 (No (Interface_Name (E))
10013 or else String_Equal
10014 (L => Strval (Interface_Name (E)),
10015 R => Strval (Get_Default_External_Name (E))))
10016 then
10017 Error_Msg_N
10018 ("'C++ constructor must have external name or link name", E);
10019 end if;
10021 -- We also reset the Pure indication on a subprogram with an Address
10022 -- parameter, because the parameter may be used as a pointer and the
10023 -- referenced data may change even if the address value does not.
10025 -- Note that if the programmer gave an explicit Pure_Function pragma,
10026 -- then we believe the programmer, and leave the subprogram Pure. We
10027 -- also suppress this check on run-time files.
10029 if Is_Pure (E)
10030 and then Is_Subprogram (E)
10031 and then not Has_Pragma_Pure_Function (E)
10032 and then not Is_Internal_Unit (Current_Sem_Unit)
10033 then
10034 Check_Function_With_Address_Parameter (E);
10035 end if;
10037 -- Ensure that all anonymous access-to-subprogram types inherit the
10038 -- convention of their related subprogram (RM 6.3.1(13.1/5)). This is
10039 -- not done for a defaulted convention Ada because those types also
10040 -- default to Ada. Convention Protected must not be propagated when
10041 -- the subprogram is an entry because this would be illegal. The only
10042 -- way to force convention Protected on these kinds of types is to
10043 -- include keyword "protected" in the access definition. Conventions
10044 -- Entry and Intrinsic are also not propagated (specified by AI12-0207).
10046 if Convention (E) /= Convention_Ada
10047 and then Convention (E) /= Convention_Protected
10048 and then Convention (E) /= Convention_Entry
10049 and then Convention (E) /= Convention_Intrinsic
10050 then
10051 Set_Profile_Convention (E);
10052 end if;
10054 -- For non-foreign convention subprograms, this is where we create
10055 -- the extra formals (for accessibility level and constrained bit
10056 -- information). We delay this till the freeze point precisely so
10057 -- that we know the convention.
10059 if not Has_Foreign_Convention (E) then
10061 -- Extra formals of dispatching operations are added later by
10062 -- Expand_Freeze_Record_Type, which also adds extra formals to
10063 -- internal entities built to handle interface types.
10065 if not Is_Dispatching_Operation (E) then
10066 Create_Extra_Formals (E);
10068 pragma Assert
10069 ((Ekind (E) = E_Subprogram_Type
10070 and then Extra_Formals_OK (E))
10071 or else
10072 (Is_Subprogram (E)
10073 and then Extra_Formals_OK (E)
10074 and then
10075 (No (Overridden_Operation (E))
10076 or else Extra_Formals_Match_OK (E,
10077 Ultimate_Alias (Overridden_Operation (E))))));
10078 end if;
10080 Set_Mechanisms (E);
10082 -- If this is convention Ada and a Valued_Procedure, that's odd
10084 if Ekind (E) = E_Procedure
10085 and then Is_Valued_Procedure (E)
10086 and then Convention (E) = Convention_Ada
10087 and then Warn_On_Export_Import
10088 then
10089 Error_Msg_N
10090 ("??Valued_Procedure has no effect for convention Ada", E);
10091 Set_Is_Valued_Procedure (E, False);
10092 end if;
10094 -- Case of foreign convention
10096 else
10097 Set_Mechanisms (E);
10099 -- For foreign conventions, warn about return of unconstrained array
10101 if Ekind (E) = E_Function then
10102 Retype := Underlying_Type (Etype (E));
10104 -- If no return type, probably some other error, e.g. a
10105 -- missing full declaration, so ignore.
10107 if No (Retype) then
10108 null;
10110 -- If the return type is generic, we have emitted a warning
10111 -- earlier on, and there is nothing else to check here. Specific
10112 -- instantiations may lead to erroneous behavior.
10114 elsif Is_Generic_Type (Etype (E)) then
10115 null;
10117 -- Display warning if returning unconstrained array
10119 elsif Is_Array_Type (Retype)
10120 and then not Is_Constrained (Retype)
10122 -- Check appropriate warning is enabled (should we check for
10123 -- Warnings (Off) on specific entities here, probably so???)
10125 and then Warn_On_Export_Import
10126 then
10127 Error_Msg_N
10128 ("?x?foreign convention function& should not return " &
10129 "unconstrained array", E);
10130 return;
10131 end if;
10132 end if;
10134 -- If any of the formals for an exported foreign convention
10135 -- subprogram have defaults, then emit an appropriate warning since
10136 -- this is odd (default cannot be used from non-Ada code)
10138 if Is_Exported (E) then
10139 F := First_Formal (E);
10140 while Present (F) loop
10141 if Warn_On_Export_Import
10142 and then Present (Default_Value (F))
10143 then
10144 Error_Msg_N
10145 ("?x?parameter cannot be defaulted in non-Ada call",
10146 Default_Value (F));
10147 end if;
10149 Next_Formal (F);
10150 end loop;
10151 end if;
10152 end if;
10154 -- Pragma Inline_Always is disallowed for dispatching subprograms
10155 -- because the address of such subprograms is saved in the dispatch
10156 -- table to support dispatching calls, and dispatching calls cannot
10157 -- be inlined. This is consistent with the restriction against using
10158 -- 'Access or 'Address on an Inline_Always subprogram.
10160 if Is_Dispatching_Operation (E)
10161 and then Has_Pragma_Inline_Always (E)
10162 then
10163 Error_Msg_N
10164 ("pragma Inline_Always not allowed for dispatching subprograms", E);
10165 end if;
10167 -- Because of the implicit representation of inherited predefined
10168 -- operators in the front-end, the overriding status of the operation
10169 -- may be affected when a full view of a type is analyzed, and this is
10170 -- not captured by the analysis of the corresponding type declaration.
10171 -- Therefore the correctness of a not-overriding indicator must be
10172 -- rechecked when the subprogram is frozen.
10174 if Nkind (E) = N_Defining_Operator_Symbol
10175 and then not Error_Posted (Parent (E))
10176 then
10177 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
10178 end if;
10180 Retype := Get_Fullest_View (Etype (E));
10182 if Transform_Function_Array
10183 and then Nkind (Parent (E)) = N_Function_Specification
10184 and then Is_Array_Type (Retype)
10185 and then Is_Constrained (Retype)
10186 and then not Is_Unchecked_Conversion_Instance (E)
10187 and then not Rewritten_For_C (E)
10188 then
10189 Build_Procedure_Form (Unit_Declaration_Node (E));
10190 end if;
10191 end Freeze_Subprogram;
10193 ----------------------
10194 -- Is_Fully_Defined --
10195 ----------------------
10197 function Is_Fully_Defined (T : Entity_Id) return Boolean is
10198 begin
10199 if Ekind (T) = E_Class_Wide_Type then
10200 return Is_Fully_Defined (Etype (T));
10202 elsif Is_Array_Type (T) then
10203 return Is_Fully_Defined (Component_Type (T));
10205 elsif Is_Record_Type (T)
10206 and not Is_Private_Type (T)
10207 then
10208 -- Verify that the record type has no components with private types
10209 -- without completion.
10211 declare
10212 Comp : Entity_Id;
10214 begin
10215 Comp := First_Component (T);
10216 while Present (Comp) loop
10217 if not Is_Fully_Defined (Etype (Comp)) then
10218 return False;
10219 end if;
10221 Next_Component (Comp);
10222 end loop;
10223 return True;
10224 end;
10226 -- For the designated type of an access to subprogram, all types in
10227 -- the profile must be fully defined.
10229 elsif Ekind (T) = E_Subprogram_Type then
10230 declare
10231 F : Entity_Id;
10233 begin
10234 F := First_Formal (T);
10235 while Present (F) loop
10236 if not Is_Fully_Defined (Etype (F)) then
10237 return False;
10238 end if;
10240 Next_Formal (F);
10241 end loop;
10243 return Is_Fully_Defined (Etype (T));
10244 end;
10246 else
10247 return not Is_Private_Type (T)
10248 or else Present (Full_View (Base_Type (T)));
10249 end if;
10250 end Is_Fully_Defined;
10252 ---------------------------------
10253 -- Process_Default_Expressions --
10254 ---------------------------------
10256 procedure Process_Default_Expressions
10257 (E : Entity_Id;
10258 After : in out Node_Id)
10260 Loc : constant Source_Ptr := Sloc (E);
10261 Dbody : Node_Id;
10262 Formal : Node_Id;
10263 Dcopy : Node_Id;
10264 Dnam : Entity_Id;
10266 begin
10267 Set_Default_Expressions_Processed (E);
10269 -- A subprogram instance and its associated anonymous subprogram share
10270 -- their signature. The default expression functions are defined in the
10271 -- wrapper packages for the anonymous subprogram, and should not be
10272 -- generated again for the instance.
10274 if Is_Generic_Instance (E)
10275 and then Present (Alias (E))
10276 and then Default_Expressions_Processed (Alias (E))
10277 then
10278 return;
10279 end if;
10281 Formal := First_Formal (E);
10282 while Present (Formal) loop
10283 if Present (Default_Value (Formal)) then
10285 -- We work with a copy of the default expression because we
10286 -- do not want to disturb the original, since this would mess
10287 -- up the conformance checking.
10289 Dcopy := New_Copy_Tree (Default_Value (Formal));
10291 -- The analysis of the expression may generate insert actions,
10292 -- which of course must not be executed. We wrap those actions
10293 -- in a procedure that is not called, and later on eliminated.
10294 -- The following cases have no side effects, and are analyzed
10295 -- directly.
10297 if Nkind (Dcopy) = N_Identifier
10298 or else Nkind (Dcopy) in N_Expanded_Name
10299 | N_Integer_Literal
10300 | N_Character_Literal
10301 | N_String_Literal
10302 | N_Real_Literal
10303 or else (Nkind (Dcopy) = N_Attribute_Reference
10304 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
10305 or else Known_Null (Dcopy)
10306 then
10307 -- If there is no default function, we must still do a full
10308 -- analyze call on the default value, to ensure that all error
10309 -- checks are performed, e.g. those associated with static
10310 -- evaluation. Note: this branch will always be taken if the
10311 -- analyzer is turned off (but we still need the error checks).
10313 -- Note: the setting of parent here is to meet the requirement
10314 -- that we can only analyze the expression while attached to
10315 -- the tree. Really the requirement is that the parent chain
10316 -- be set, we don't actually need to be in the tree.
10318 Set_Parent (Dcopy, Declaration_Node (Formal));
10319 Analyze (Dcopy);
10321 -- Default expressions are resolved with their own type if the
10322 -- context is generic, to avoid anomalies with private types.
10324 if Ekind (Scope (E)) = E_Generic_Package then
10325 Resolve (Dcopy);
10326 else
10327 Resolve (Dcopy, Etype (Formal));
10328 end if;
10330 -- If that resolved expression will raise constraint error,
10331 -- then flag the default value as raising constraint error.
10332 -- This allows a proper error message on the calls.
10334 if Raises_Constraint_Error (Dcopy) then
10335 Set_Raises_Constraint_Error (Default_Value (Formal));
10336 end if;
10338 -- If the default is a parameterless call, we use the name of
10339 -- the called function directly, and there is no body to build.
10341 elsif Nkind (Dcopy) = N_Function_Call
10342 and then No (Parameter_Associations (Dcopy))
10343 then
10344 null;
10346 -- Else construct and analyze the body of a wrapper procedure
10347 -- that contains an object declaration to hold the expression.
10348 -- Given that this is done only to complete the analysis, it is
10349 -- simpler to build a procedure than a function which might
10350 -- involve secondary stack expansion.
10352 else
10353 Dnam := Make_Temporary (Loc, 'D');
10355 Dbody :=
10356 Make_Subprogram_Body (Loc,
10357 Specification =>
10358 Make_Procedure_Specification (Loc,
10359 Defining_Unit_Name => Dnam),
10361 Declarations => New_List (
10362 Make_Object_Declaration (Loc,
10363 Defining_Identifier => Make_Temporary (Loc, 'T'),
10364 Object_Definition =>
10365 New_Occurrence_Of (Etype (Formal), Loc),
10366 Expression => New_Copy_Tree (Dcopy))),
10368 Handled_Statement_Sequence =>
10369 Make_Handled_Sequence_Of_Statements (Loc,
10370 Statements => Empty_List));
10372 Set_Scope (Dnam, Scope (E));
10373 Set_Assignment_OK (First (Declarations (Dbody)));
10374 Set_Is_Eliminated (Dnam);
10375 Insert_After (After, Dbody);
10376 Analyze (Dbody);
10377 After := Dbody;
10378 end if;
10379 end if;
10381 Next_Formal (Formal);
10382 end loop;
10383 end Process_Default_Expressions;
10385 ----------------------------------------
10386 -- Set_Component_Alignment_If_Not_Set --
10387 ----------------------------------------
10389 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
10390 begin
10391 -- Ignore if not base type, subtypes don't need anything
10393 if Typ /= Base_Type (Typ) then
10394 return;
10395 end if;
10397 -- Do not override existing representation
10399 if Is_Packed (Typ) then
10400 return;
10402 elsif Has_Specified_Layout (Typ) then
10403 return;
10405 elsif Component_Alignment (Typ) /= Calign_Default then
10406 return;
10408 else
10409 Set_Component_Alignment
10410 (Typ, Scope_Stack.Table
10411 (Scope_Stack.Last).Component_Alignment_Default);
10412 end if;
10413 end Set_Component_Alignment_If_Not_Set;
10415 --------------------------
10416 -- Set_SSO_From_Default --
10417 --------------------------
10419 procedure Set_SSO_From_Default (T : Entity_Id) is
10420 Reversed : Boolean;
10422 begin
10423 -- Set default SSO for an array or record base type, except in case of
10424 -- a type extension (which always inherits the SSO of its parent type).
10426 if Is_Base_Type (T)
10427 and then (Is_Array_Type (T)
10428 or else (Is_Record_Type (T)
10429 and then not (Is_Tagged_Type (T)
10430 and then Is_Derived_Type (T))))
10431 then
10432 Reversed :=
10433 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
10434 or else
10435 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
10437 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
10439 -- For a record type, if bit order is specified explicitly,
10440 -- then do not set SSO from default if not consistent. Note that
10441 -- we do not want to look at a Bit_Order attribute definition
10442 -- for a parent: if we were to inherit Bit_Order, then both
10443 -- SSO_Set_*_By_Default flags would have been cleared already
10444 -- (by Inherit_Aspects_At_Freeze_Point).
10446 and then not
10447 (Is_Record_Type (T)
10448 and then
10449 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
10450 and then Reverse_Bit_Order (T) /= Reversed)
10451 then
10452 -- If flags cause reverse storage order, then set the result. Note
10453 -- that we would have ignored the pragma setting the non default
10454 -- storage order in any case, hence the assertion at this point.
10456 pragma Assert
10457 (not Reversed or else Support_Nondefault_SSO_On_Target);
10459 Set_Reverse_Storage_Order (T, Reversed);
10461 -- For a record type, also set reversed bit order. Note: if a bit
10462 -- order has been specified explicitly, then this is a no-op.
10464 if Is_Record_Type (T) then
10465 Set_Reverse_Bit_Order (T, Reversed);
10466 end if;
10467 end if;
10468 end if;
10469 end Set_SSO_From_Default;
10471 ------------------
10472 -- Undelay_Type --
10473 ------------------
10475 procedure Undelay_Type (T : Entity_Id) is
10476 begin
10477 Set_Has_Delayed_Freeze (T, False);
10478 Set_Freeze_Node (T, Empty);
10480 -- Since we don't want T to have a Freeze_Node, we don't want its
10481 -- Full_View or Corresponding_Record_Type to have one either.
10483 -- ??? Fundamentally, this whole handling is unpleasant. What we really
10484 -- want is to be sure that for an Itype that's part of record R and is a
10485 -- subtype of type T, that it's frozen after the later of the freeze
10486 -- points of R and T. We have no way of doing that directly, so what we
10487 -- do is force most such Itypes to be frozen as part of freezing R via
10488 -- this procedure and only delay the ones that need to be delayed
10489 -- (mostly the designated types of access types that are defined as part
10490 -- of the record).
10492 if Is_Private_Type (T)
10493 and then Present (Full_View (T))
10494 and then Is_Itype (Full_View (T))
10495 and then Is_Record_Type (Scope (Full_View (T)))
10496 then
10497 Undelay_Type (Full_View (T));
10498 end if;
10500 if Is_Concurrent_Type (T)
10501 and then Present (Corresponding_Record_Type (T))
10502 and then Is_Itype (Corresponding_Record_Type (T))
10503 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
10504 then
10505 Undelay_Type (Corresponding_Record_Type (T));
10506 end if;
10507 end Undelay_Type;
10509 ------------------
10510 -- Warn_Overlay --
10511 ------------------
10513 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id) is
10514 Ent : constant Entity_Id := Entity (Nam);
10515 -- The object to which the address clause applies
10517 Init : Node_Id;
10518 Old : Entity_Id := Empty;
10519 Decl : Node_Id;
10521 begin
10522 -- No warning if address clause overlay warnings are off
10524 if not Address_Clause_Overlay_Warnings then
10525 return;
10526 end if;
10528 -- No warning if there is an explicit initialization
10530 Init := Original_Node (Expression (Declaration_Node (Ent)));
10532 if Present (Init) and then Comes_From_Source (Init) then
10533 return;
10534 end if;
10536 -- We only give the warning for non-imported entities of a type for
10537 -- which a non-null base init proc is defined, or for objects of access
10538 -- types with implicit null initialization, or when Normalize_Scalars
10539 -- applies and the type is scalar or a string type (the latter being
10540 -- tested for because predefined String types are initialized by inline
10541 -- code rather than by an init_proc). Note that we do not give the
10542 -- warning for Initialize_Scalars, since we suppressed initialization
10543 -- in this case. Also, do not warn if Suppress_Initialization is set
10544 -- either on the type, or on the object via pragma or aspect.
10546 if Present (Expr)
10547 and then not Is_Imported (Ent)
10548 and then not Initialization_Suppressed (Typ)
10549 and then not (Ekind (Ent) = E_Variable
10550 and then Initialization_Suppressed (Ent))
10551 and then (Has_Non_Null_Base_Init_Proc (Typ)
10552 or else Is_Access_Type (Typ)
10553 or else (Normalize_Scalars
10554 and then (Is_Scalar_Type (Typ)
10555 or else Is_String_Type (Typ))))
10556 then
10557 if Nkind (Expr) = N_Attribute_Reference
10558 and then Is_Entity_Name (Prefix (Expr))
10559 then
10560 Old := Entity (Prefix (Expr));
10562 elsif Is_Entity_Name (Expr)
10563 and then Ekind (Entity (Expr)) = E_Constant
10564 then
10565 Decl := Declaration_Node (Entity (Expr));
10567 if Nkind (Decl) = N_Object_Declaration
10568 and then Present (Expression (Decl))
10569 and then Nkind (Expression (Decl)) = N_Attribute_Reference
10570 and then Is_Entity_Name (Prefix (Expression (Decl)))
10571 then
10572 Old := Entity (Prefix (Expression (Decl)));
10574 elsif Nkind (Expr) = N_Function_Call then
10575 return;
10576 end if;
10578 -- A function call (most likely to To_Address) is probably not an
10579 -- overlay, so skip warning. Ditto if the function call was inlined
10580 -- and transformed into an entity.
10582 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
10583 return;
10584 end if;
10586 -- If a pragma Import follows, we assume that it is for the current
10587 -- target of the address clause, and skip the warning. There may be
10588 -- a source pragma or an aspect that specifies import and generates
10589 -- the corresponding pragma. These will indicate that the entity is
10590 -- imported and that is checked above so that the spurious warning
10591 -- (generated when the entity is frozen) will be suppressed. The
10592 -- pragma may be attached to the aspect, so it is not yet a list
10593 -- member.
10595 if Is_List_Member (Parent (Expr)) then
10596 Decl := Next (Parent (Expr));
10598 if Present (Decl)
10599 and then Nkind (Decl) = N_Pragma
10600 and then Pragma_Name (Decl) = Name_Import
10601 then
10602 return;
10603 end if;
10604 end if;
10606 -- Otherwise give warning message
10608 if Present (Old) then
10609 Error_Msg_Node_2 := Old;
10610 Error_Msg_N
10611 ("default initialization of & may modify &?o?",
10612 Nam);
10613 else
10614 Error_Msg_N
10615 ("default initialization of & may modify overlaid storage?o?",
10616 Nam);
10617 end if;
10619 -- Add friendly warning if initialization comes from a packed array
10620 -- component.
10622 if Is_Record_Type (Typ) then
10623 declare
10624 Comp : Entity_Id;
10626 begin
10627 Comp := First_Component (Typ);
10628 while Present (Comp) loop
10629 if Nkind (Parent (Comp)) = N_Component_Declaration
10630 and then Present (Expression (Parent (Comp)))
10631 then
10632 exit;
10633 elsif Is_Array_Type (Etype (Comp))
10634 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
10635 then
10636 Error_Msg_NE
10637 ("\packed array component& " &
10638 "will be initialized to zero??",
10639 Nam, Comp);
10640 exit;
10641 else
10642 Next_Component (Comp);
10643 end if;
10644 end loop;
10645 end;
10646 end if;
10648 Error_Msg_N
10649 ("\use pragma Import for & to " &
10650 "suppress initialization (RM B.1(24))??",
10651 Nam);
10652 end if;
10653 end Warn_Overlay;
10655 end Freeze;