74e1688c7b6054d063db1be0f7c872c50cc841e5
[gcc.git] / gcc / ada / freeze.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- F R E E Z E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Pakd; use Exp_Pakd;
37 with Exp_Util; use Exp_Util;
38 with Exp_Tss; use Exp_Tss;
39 with Fname; use Fname;
40 with Ghost; use Ghost;
41 with Layout; use Layout;
42 with Lib; use Lib;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Mech; use Sem_Mech;
59 with Sem_Prag; use Sem_Prag;
60 with Sem_Res; use Sem_Res;
61 with Sem_Util; use Sem_Util;
62 with Sinfo; use Sinfo;
63 with Snames; use Snames;
64 with Stand; use Stand;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uintp; use Uintp;
69 with Urealp; use Urealp;
70 with Warnsw; use Warnsw;
71
72 package body Freeze is
73
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
77
78 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
79 -- Typ is a type that is being frozen. If no size clause is given,
80 -- but a default Esize has been computed, then this default Esize is
81 -- adjusted up if necessary to be consistent with a given alignment,
82 -- but never to a value greater than Long_Long_Integer'Size. This
83 -- is used for all discrete types and for fixed-point types.
84
85 procedure Build_And_Analyze_Renamed_Body
86 (Decl : Node_Id;
87 New_S : Entity_Id;
88 After : in out Node_Id);
89 -- Build body for a renaming declaration, insert in tree and analyze
90
91 procedure Check_Address_Clause (E : Entity_Id);
92 -- Apply legality checks to address clauses for object declarations,
93 -- at the point the object is frozen. Also ensure any initialization is
94 -- performed only after the object has been frozen.
95
96 procedure Check_Component_Storage_Order
97 (Encl_Type : Entity_Id;
98 Comp : Entity_Id;
99 ADC : Node_Id;
100 Comp_ADC_Present : out Boolean);
101 -- For an Encl_Type that has a Scalar_Storage_Order attribute definition
102 -- clause, verify that the component type has an explicit and compatible
103 -- attribute/aspect. For arrays, Comp is Empty; for records, it is the
104 -- entity of the component under consideration. For an Encl_Type that
105 -- does not have a Scalar_Storage_Order attribute definition clause,
106 -- verify that the component also does not have such a clause.
107 -- ADC is the attribute definition clause if present (or Empty). On return,
108 -- Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
109 -- attribute definition clause.
110
111 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
112 -- When an expression function is frozen by a use of it, the expression
113 -- itself is frozen. Check that the expression does not include references
114 -- to deferred constants without completion. We report this at the freeze
115 -- point of the function, to provide a better error message.
116 --
117 -- In most cases the expression itself is frozen by the time the function
118 -- itself is frozen, because the formals will be frozen by then. However,
119 -- Attribute references to outer types are freeze points for those types;
120 -- this routine generates the required freeze nodes for them.
121
122 procedure Check_Strict_Alignment (E : Entity_Id);
123 -- E is a base type. If E is tagged or has a component that is aliased
124 -- or tagged or contains something this is aliased or tagged, set
125 -- Strict_Alignment.
126
127 procedure Check_Unsigned_Type (E : Entity_Id);
128 pragma Inline (Check_Unsigned_Type);
129 -- If E is a fixed-point or discrete type, then all the necessary work
130 -- to freeze it is completed except for possible setting of the flag
131 -- Is_Unsigned_Type, which is done by this procedure. The call has no
132 -- effect if the entity E is not a discrete or fixed-point type.
133
134 procedure Freeze_And_Append
135 (Ent : Entity_Id;
136 N : Node_Id;
137 Result : in out List_Id);
138 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
139 -- nodes to Result, modifying Result from No_List if necessary. N has
140 -- the same usage as in Freeze_Entity.
141
142 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
143 -- Freeze enumeration type. The Esize field is set as processing
144 -- proceeds (i.e. set by default when the type is declared and then
145 -- adjusted by rep clauses. What this procedure does is to make sure
146 -- that if a foreign convention is specified, and no specific size
147 -- is given, then the size must be at least Integer'Size.
148
149 procedure Freeze_Static_Object (E : Entity_Id);
150 -- If an object is frozen which has Is_Statically_Allocated set, then
151 -- all referenced types must also be marked with this flag. This routine
152 -- is in charge of meeting this requirement for the object entity E.
153
154 procedure Freeze_Subprogram (E : Entity_Id);
155 -- Perform freezing actions for a subprogram (create extra formals,
156 -- and set proper default mechanism values). Note that this routine
157 -- is not called for internal subprograms, for which neither of these
158 -- actions is needed (or desirable, we do not want for example to have
159 -- these extra formals present in initialization procedures, where they
160 -- would serve no purpose). In this call E is either a subprogram or
161 -- a subprogram type (i.e. an access to a subprogram).
162
163 function Is_Fully_Defined (T : Entity_Id) return Boolean;
164 -- True if T is not private and has no private components, or has a full
165 -- view. Used to determine whether the designated type of an access type
166 -- should be frozen when the access type is frozen. This is done when an
167 -- allocator is frozen, or an expression that may involve attributes of
168 -- the designated type. Otherwise freezing the access type does not freeze
169 -- the designated type.
170
171 procedure Process_Default_Expressions
172 (E : Entity_Id;
173 After : in out Node_Id);
174 -- This procedure is called for each subprogram to complete processing of
175 -- default expressions at the point where all types are known to be frozen.
176 -- The expressions must be analyzed in full, to make sure that all error
177 -- processing is done (they have only been pre-analyzed). If the expression
178 -- is not an entity or literal, its analysis may generate code which must
179 -- not be executed. In that case we build a function body to hold that
180 -- code. This wrapper function serves no other purpose (it used to be
181 -- called to evaluate the default, but now the default is inlined at each
182 -- point of call).
183
184 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
185 -- Typ is a record or array type that is being frozen. This routine sets
186 -- the default component alignment from the scope stack values if the
187 -- alignment is otherwise not specified.
188
189 procedure Check_Debug_Info_Needed (T : Entity_Id);
190 -- As each entity is frozen, this routine is called to deal with the
191 -- setting of Debug_Info_Needed for the entity. This flag is set if
192 -- the entity comes from source, or if we are in Debug_Generated_Code
193 -- mode or if the -gnatdV debug flag is set. However, it never sets
194 -- the flag if Debug_Info_Off is set. This procedure also ensures that
195 -- subsidiary entities have the flag set as required.
196
197 procedure Set_SSO_From_Default (T : Entity_Id);
198 -- T is a record or array type that is being frozen. If it is a base type,
199 -- and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
200 -- will be set appropriately. Note that an explicit occurrence of aspect
201 -- Scalar_Storage_Order or an explicit setting of this aspect with an
202 -- attribute definition clause occurs, then these two flags are reset in
203 -- any case, so call will have no effect.
204
205 procedure Undelay_Type (T : Entity_Id);
206 -- T is a type of a component that we know to be an Itype. We don't want
207 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
208 -- Full_View or Corresponding_Record_Type.
209
210 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
211 -- Expr is the expression for an address clause for entity Nam whose type
212 -- is Typ. If Typ has a default initialization, and there is no explicit
213 -- initialization in the source declaration, check whether the address
214 -- clause might cause overlaying of an entity, and emit a warning on the
215 -- side effect that the initialization will cause.
216
217 -------------------------------
218 -- Adjust_Esize_For_Alignment --
219 -------------------------------
220
221 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
222 Align : Uint;
223
224 begin
225 if Known_Esize (Typ) and then Known_Alignment (Typ) then
226 Align := Alignment_In_Bits (Typ);
227
228 if Align > Esize (Typ)
229 and then Align <= Standard_Long_Long_Integer_Size
230 then
231 Set_Esize (Typ, Align);
232 end if;
233 end if;
234 end Adjust_Esize_For_Alignment;
235
236 ------------------------------------
237 -- Build_And_Analyze_Renamed_Body --
238 ------------------------------------
239
240 procedure Build_And_Analyze_Renamed_Body
241 (Decl : Node_Id;
242 New_S : Entity_Id;
243 After : in out Node_Id)
244 is
245 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
246 Ent : constant Entity_Id := Defining_Entity (Decl);
247 Body_Node : Node_Id;
248 Renamed_Subp : Entity_Id;
249
250 begin
251 -- If the renamed subprogram is intrinsic, there is no need for a
252 -- wrapper body: we set the alias that will be called and expanded which
253 -- completes the declaration. This transformation is only legal if the
254 -- renamed entity has already been elaborated.
255
256 -- Note that it is legal for a renaming_as_body to rename an intrinsic
257 -- subprogram, as long as the renaming occurs before the new entity
258 -- is frozen (RM 8.5.4 (5)).
259
260 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
261 and then Is_Entity_Name (Name (Body_Decl))
262 then
263 Renamed_Subp := Entity (Name (Body_Decl));
264 else
265 Renamed_Subp := Empty;
266 end if;
267
268 if Present (Renamed_Subp)
269 and then Is_Intrinsic_Subprogram (Renamed_Subp)
270 and then
271 (not In_Same_Source_Unit (Renamed_Subp, Ent)
272 or else Sloc (Renamed_Subp) < Sloc (Ent))
273
274 -- We can make the renaming entity intrinsic if the renamed function
275 -- has an interface name, or if it is one of the shift/rotate
276 -- operations known to the compiler.
277
278 and then
279 (Present (Interface_Name (Renamed_Subp))
280 or else Nam_In (Chars (Renamed_Subp), Name_Rotate_Left,
281 Name_Rotate_Right,
282 Name_Shift_Left,
283 Name_Shift_Right,
284 Name_Shift_Right_Arithmetic))
285 then
286 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
287
288 if Present (Alias (Renamed_Subp)) then
289 Set_Alias (Ent, Alias (Renamed_Subp));
290 else
291 Set_Alias (Ent, Renamed_Subp);
292 end if;
293
294 Set_Is_Intrinsic_Subprogram (Ent);
295 Set_Has_Completion (Ent);
296
297 else
298 Body_Node := Build_Renamed_Body (Decl, New_S);
299 Insert_After (After, Body_Node);
300 Mark_Rewrite_Insertion (Body_Node);
301 Analyze (Body_Node);
302 After := Body_Node;
303 end if;
304 end Build_And_Analyze_Renamed_Body;
305
306 ------------------------
307 -- Build_Renamed_Body --
308 ------------------------
309
310 function Build_Renamed_Body
311 (Decl : Node_Id;
312 New_S : Entity_Id) return Node_Id
313 is
314 Loc : constant Source_Ptr := Sloc (New_S);
315 -- We use for the source location of the renamed body, the location of
316 -- the spec entity. It might seem more natural to use the location of
317 -- the renaming declaration itself, but that would be wrong, since then
318 -- the body we create would look as though it was created far too late,
319 -- and this could cause problems with elaboration order analysis,
320 -- particularly in connection with instantiations.
321
322 N : constant Node_Id := Unit_Declaration_Node (New_S);
323 Nam : constant Node_Id := Name (N);
324 Old_S : Entity_Id;
325 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
326 Actuals : List_Id := No_List;
327 Call_Node : Node_Id;
328 Call_Name : Node_Id;
329 Body_Node : Node_Id;
330 Formal : Entity_Id;
331 O_Formal : Entity_Id;
332 Param_Spec : Node_Id;
333
334 Pref : Node_Id := Empty;
335 -- If the renamed entity is a primitive operation given in prefix form,
336 -- the prefix is the target object and it has to be added as the first
337 -- actual in the generated call.
338
339 begin
340 -- Determine the entity being renamed, which is the target of the call
341 -- statement. If the name is an explicit dereference, this is a renaming
342 -- of a subprogram type rather than a subprogram. The name itself is
343 -- fully analyzed.
344
345 if Nkind (Nam) = N_Selected_Component then
346 Old_S := Entity (Selector_Name (Nam));
347
348 elsif Nkind (Nam) = N_Explicit_Dereference then
349 Old_S := Etype (Nam);
350
351 elsif Nkind (Nam) = N_Indexed_Component then
352 if Is_Entity_Name (Prefix (Nam)) then
353 Old_S := Entity (Prefix (Nam));
354 else
355 Old_S := Entity (Selector_Name (Prefix (Nam)));
356 end if;
357
358 elsif Nkind (Nam) = N_Character_Literal then
359 Old_S := Etype (New_S);
360
361 else
362 Old_S := Entity (Nam);
363 end if;
364
365 if Is_Entity_Name (Nam) then
366
367 -- If the renamed entity is a predefined operator, retain full name
368 -- to ensure its visibility.
369
370 if Ekind (Old_S) = E_Operator
371 and then Nkind (Nam) = N_Expanded_Name
372 then
373 Call_Name := New_Copy (Name (N));
374 else
375 Call_Name := New_Occurrence_Of (Old_S, Loc);
376 end if;
377
378 else
379 if Nkind (Nam) = N_Selected_Component
380 and then Present (First_Formal (Old_S))
381 and then
382 (Is_Controlling_Formal (First_Formal (Old_S))
383 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
384 then
385
386 -- Retrieve the target object, to be added as a first actual
387 -- in the call.
388
389 Call_Name := New_Occurrence_Of (Old_S, Loc);
390 Pref := Prefix (Nam);
391
392 else
393 Call_Name := New_Copy (Name (N));
394 end if;
395
396 -- Original name may have been overloaded, but is fully resolved now
397
398 Set_Is_Overloaded (Call_Name, False);
399 end if;
400
401 -- For simple renamings, subsequent calls can be expanded directly as
402 -- calls to the renamed entity. The body must be generated in any case
403 -- for calls that may appear elsewhere. This is not done in the case
404 -- where the subprogram is an instantiation because the actual proper
405 -- body has not been built yet.
406
407 if Ekind_In (Old_S, E_Function, E_Procedure)
408 and then Nkind (Decl) = N_Subprogram_Declaration
409 and then not Is_Generic_Instance (Old_S)
410 then
411 Set_Body_To_Inline (Decl, Old_S);
412 end if;
413
414 -- Check whether the return type is a limited view. If the subprogram
415 -- is already frozen the generated body may have a non-limited view
416 -- of the type, that must be used, because it is the one in the spec
417 -- of the renaming declaration.
418
419 if Ekind (Old_S) = E_Function
420 and then Is_Entity_Name (Result_Definition (Spec))
421 then
422 declare
423 Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
424 begin
425 if Has_Non_Limited_View (Ret_Type) then
426 Set_Result_Definition
427 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
428 end if;
429 end;
430 end if;
431
432 -- The body generated for this renaming is an internal artifact, and
433 -- does not constitute a freeze point for the called entity.
434
435 Set_Must_Not_Freeze (Call_Name);
436
437 Formal := First_Formal (Defining_Entity (Decl));
438
439 if Present (Pref) then
440 declare
441 Pref_Type : constant Entity_Id := Etype (Pref);
442 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
443
444 begin
445 -- The controlling formal may be an access parameter, or the
446 -- actual may be an access value, so adjust accordingly.
447
448 if Is_Access_Type (Pref_Type)
449 and then not Is_Access_Type (Form_Type)
450 then
451 Actuals := New_List
452 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
453
454 elsif Is_Access_Type (Form_Type)
455 and then not Is_Access_Type (Pref)
456 then
457 Actuals :=
458 New_List (
459 Make_Attribute_Reference (Loc,
460 Attribute_Name => Name_Access,
461 Prefix => Relocate_Node (Pref)));
462 else
463 Actuals := New_List (Pref);
464 end if;
465 end;
466
467 elsif Present (Formal) then
468 Actuals := New_List;
469
470 else
471 Actuals := No_List;
472 end if;
473
474 if Present (Formal) then
475 while Present (Formal) loop
476 Append (New_Occurrence_Of (Formal, Loc), Actuals);
477 Next_Formal (Formal);
478 end loop;
479 end if;
480
481 -- If the renamed entity is an entry, inherit its profile. For other
482 -- renamings as bodies, both profiles must be subtype conformant, so it
483 -- is not necessary to replace the profile given in the declaration.
484 -- However, default values that are aggregates are rewritten when
485 -- partially analyzed, so we recover the original aggregate to insure
486 -- that subsequent conformity checking works. Similarly, if the default
487 -- expression was constant-folded, recover the original expression.
488
489 Formal := First_Formal (Defining_Entity (Decl));
490
491 if Present (Formal) then
492 O_Formal := First_Formal (Old_S);
493 Param_Spec := First (Parameter_Specifications (Spec));
494 while Present (Formal) loop
495 if Is_Entry (Old_S) then
496 if Nkind (Parameter_Type (Param_Spec)) /=
497 N_Access_Definition
498 then
499 Set_Etype (Formal, Etype (O_Formal));
500 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
501 end if;
502
503 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
504 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
505 Nkind (Default_Value (O_Formal))
506 then
507 Set_Expression (Param_Spec,
508 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
509 end if;
510
511 Next_Formal (Formal);
512 Next_Formal (O_Formal);
513 Next (Param_Spec);
514 end loop;
515 end if;
516
517 -- If the renamed entity is a function, the generated body contains a
518 -- return statement. Otherwise, build a procedure call. If the entity is
519 -- an entry, subsequent analysis of the call will transform it into the
520 -- proper entry or protected operation call. If the renamed entity is
521 -- a character literal, return it directly.
522
523 if Ekind (Old_S) = E_Function
524 or else Ekind (Old_S) = E_Operator
525 or else (Ekind (Old_S) = E_Subprogram_Type
526 and then Etype (Old_S) /= Standard_Void_Type)
527 then
528 Call_Node :=
529 Make_Simple_Return_Statement (Loc,
530 Expression =>
531 Make_Function_Call (Loc,
532 Name => Call_Name,
533 Parameter_Associations => Actuals));
534
535 elsif Ekind (Old_S) = E_Enumeration_Literal then
536 Call_Node :=
537 Make_Simple_Return_Statement (Loc,
538 Expression => New_Occurrence_Of (Old_S, Loc));
539
540 elsif Nkind (Nam) = N_Character_Literal then
541 Call_Node :=
542 Make_Simple_Return_Statement (Loc, Expression => Call_Name);
543
544 else
545 Call_Node :=
546 Make_Procedure_Call_Statement (Loc,
547 Name => Call_Name,
548 Parameter_Associations => Actuals);
549 end if;
550
551 -- Create entities for subprogram body and formals
552
553 Set_Defining_Unit_Name (Spec,
554 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
555
556 Param_Spec := First (Parameter_Specifications (Spec));
557 while Present (Param_Spec) loop
558 Set_Defining_Identifier (Param_Spec,
559 Make_Defining_Identifier (Loc,
560 Chars => Chars (Defining_Identifier (Param_Spec))));
561 Next (Param_Spec);
562 end loop;
563
564 Body_Node :=
565 Make_Subprogram_Body (Loc,
566 Specification => Spec,
567 Declarations => New_List,
568 Handled_Statement_Sequence =>
569 Make_Handled_Sequence_Of_Statements (Loc,
570 Statements => New_List (Call_Node)));
571
572 if Nkind (Decl) /= N_Subprogram_Declaration then
573 Rewrite (N,
574 Make_Subprogram_Declaration (Loc,
575 Specification => Specification (N)));
576 end if;
577
578 -- Link the body to the entity whose declaration it completes. If
579 -- the body is analyzed when the renamed entity is frozen, it may
580 -- be necessary to restore the proper scope (see package Exp_Ch13).
581
582 if Nkind (N) = N_Subprogram_Renaming_Declaration
583 and then Present (Corresponding_Spec (N))
584 then
585 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
586 else
587 Set_Corresponding_Spec (Body_Node, New_S);
588 end if;
589
590 return Body_Node;
591 end Build_Renamed_Body;
592
593 --------------------------
594 -- Check_Address_Clause --
595 --------------------------
596
597 procedure Check_Address_Clause (E : Entity_Id) is
598 Addr : constant Node_Id := Address_Clause (E);
599 Typ : constant Entity_Id := Etype (E);
600 Decl : Node_Id;
601 Expr : Node_Id;
602 Init : Node_Id;
603 Lhs : Node_Id;
604 Tag_Assign : Node_Id;
605
606 begin
607 if Present (Addr) then
608
609 -- For a deferred constant, the initialization value is on full view
610
611 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
612 Decl := Declaration_Node (Full_View (E));
613 else
614 Decl := Declaration_Node (E);
615 end if;
616
617 Expr := Expression (Addr);
618
619 if Needs_Constant_Address (Decl, Typ) then
620 Check_Constant_Address_Clause (Expr, E);
621
622 -- Has_Delayed_Freeze was set on E when the address clause was
623 -- analyzed, and must remain set because we want the address
624 -- clause to be elaborated only after any entity it references
625 -- has been elaborated.
626 end if;
627
628 -- If Rep_Clauses are to be ignored, remove address clause from
629 -- list attached to entity, because it may be illegal for gigi,
630 -- for example by breaking order of elaboration..
631
632 if Ignore_Rep_Clauses then
633 declare
634 Rep : Node_Id;
635
636 begin
637 Rep := First_Rep_Item (E);
638
639 if Rep = Addr then
640 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
641
642 else
643 while Present (Rep)
644 and then Next_Rep_Item (Rep) /= Addr
645 loop
646 Rep := Next_Rep_Item (Rep);
647 end loop;
648 end if;
649
650 if Present (Rep) then
651 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
652 end if;
653 end;
654
655 -- And now remove the address clause
656
657 Kill_Rep_Clause (Addr);
658
659 elsif not Error_Posted (Expr)
660 and then not Needs_Finalization (Typ)
661 then
662 Warn_Overlay (Expr, Typ, Name (Addr));
663 end if;
664
665 Init := Expression (Decl);
666
667 -- If a variable, or a non-imported constant, overlays a constant
668 -- object and has an initialization value, then the initialization
669 -- may end up writing into read-only memory. Detect the cases of
670 -- statically identical values and remove the initialization. In
671 -- the other cases, give a warning. We will give other warnings
672 -- later for the variable if it is assigned.
673
674 if (Ekind (E) = E_Variable
675 or else (Ekind (E) = E_Constant
676 and then not Is_Imported (E)))
677 and then Overlays_Constant (E)
678 and then Present (Init)
679 then
680 declare
681 O_Ent : Entity_Id;
682 Off : Boolean;
683
684 begin
685 Find_Overlaid_Entity (Addr, O_Ent, Off);
686
687 if Ekind (O_Ent) = E_Constant
688 and then Etype (O_Ent) = Typ
689 and then Present (Constant_Value (O_Ent))
690 and then Compile_Time_Compare
691 (Init,
692 Constant_Value (O_Ent),
693 Assume_Valid => True) = EQ
694 then
695 Set_No_Initialization (Decl);
696 return;
697
698 elsif Comes_From_Source (Init)
699 and then Address_Clause_Overlay_Warnings
700 then
701 Error_Msg_Sloc := Sloc (Addr);
702 Error_Msg_NE
703 ("??constant& may be modified via address clause#",
704 Decl, O_Ent);
705 end if;
706 end;
707 end if;
708
709 if Present (Init) then
710
711 -- Capture initialization value at point of declaration,
712 -- and make explicit assignment legal, because object may
713 -- be a constant.
714
715 Remove_Side_Effects (Init);
716 Lhs := New_Occurrence_Of (E, Sloc (Decl));
717 Set_Assignment_OK (Lhs);
718
719 -- Move initialization to freeze actions, once the object has
720 -- been frozen and the address clause alignment check has been
721 -- performed.
722
723 Append_Freeze_Action (E,
724 Make_Assignment_Statement (Sloc (Decl),
725 Name => Lhs,
726 Expression => Expression (Decl)));
727
728 Set_No_Initialization (Decl);
729
730 -- If the objet is tagged, check whether the tag must be
731 -- reassigned explicitly.
732
733 Tag_Assign := Make_Tag_Assignment (Decl);
734 if Present (Tag_Assign) then
735 Append_Freeze_Action (E, Tag_Assign);
736 end if;
737 end if;
738 end if;
739 end Check_Address_Clause;
740
741 -----------------------------
742 -- Check_Compile_Time_Size --
743 -----------------------------
744
745 procedure Check_Compile_Time_Size (T : Entity_Id) is
746
747 procedure Set_Small_Size (T : Entity_Id; S : Uint);
748 -- Sets the compile time known size (32 bits or less) in the Esize
749 -- field, of T checking for a size clause that was given which attempts
750 -- to give a smaller size, and also checking for an alignment clause.
751
752 function Size_Known (T : Entity_Id) return Boolean;
753 -- Recursive function that does all the work
754
755 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
756 -- If T is a constrained subtype, its size is not known if any of its
757 -- discriminant constraints is not static and it is not a null record.
758 -- The test is conservative and doesn't check that the components are
759 -- in fact constrained by non-static discriminant values. Could be made
760 -- more precise ???
761
762 --------------------
763 -- Set_Small_Size --
764 --------------------
765
766 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
767 begin
768 if S > 32 then
769 return;
770
771 -- Check for bad size clause given
772
773 elsif Has_Size_Clause (T) then
774 if RM_Size (T) < S then
775 Error_Msg_Uint_1 := S;
776 Error_Msg_NE
777 ("size for& too small, minimum allowed is ^",
778 Size_Clause (T), T);
779 end if;
780
781 -- Set size if not set already
782
783 elsif Unknown_RM_Size (T) then
784 Set_RM_Size (T, S);
785 end if;
786 end Set_Small_Size;
787
788 ----------------
789 -- Size_Known --
790 ----------------
791
792 function Size_Known (T : Entity_Id) return Boolean is
793 Index : Entity_Id;
794 Comp : Entity_Id;
795 Ctyp : Entity_Id;
796 Low : Node_Id;
797 High : Node_Id;
798
799 begin
800 if Size_Known_At_Compile_Time (T) then
801 return True;
802
803 -- Always True for scalar types. This is true even for generic formal
804 -- scalar types. We used to return False in the latter case, but the
805 -- size is known at compile time, even in the template, we just do
806 -- not know the exact size but that's not the point of this routine.
807
808 elsif Is_Scalar_Type (T)
809 or else Is_Task_Type (T)
810 then
811 return True;
812
813 -- Array types
814
815 elsif Is_Array_Type (T) then
816
817 -- String literals always have known size, and we can set it
818
819 if Ekind (T) = E_String_Literal_Subtype then
820 Set_Small_Size (T, Component_Size (T)
821 * String_Literal_Length (T));
822 return True;
823
824 -- Unconstrained types never have known at compile time size
825
826 elsif not Is_Constrained (T) then
827 return False;
828
829 -- Don't do any recursion on type with error posted, since we may
830 -- have a malformed type that leads us into a loop.
831
832 elsif Error_Posted (T) then
833 return False;
834
835 -- Otherwise if component size unknown, then array size unknown
836
837 elsif not Size_Known (Component_Type (T)) then
838 return False;
839 end if;
840
841 -- Check for all indexes static, and also compute possible size
842 -- (in case it is less than 32 and may be packable).
843
844 declare
845 Esiz : Uint := Component_Size (T);
846 Dim : Uint;
847
848 begin
849 Index := First_Index (T);
850 while Present (Index) loop
851 if Nkind (Index) = N_Range then
852 Get_Index_Bounds (Index, Low, High);
853
854 elsif Error_Posted (Scalar_Range (Etype (Index))) then
855 return False;
856
857 else
858 Low := Type_Low_Bound (Etype (Index));
859 High := Type_High_Bound (Etype (Index));
860 end if;
861
862 if not Compile_Time_Known_Value (Low)
863 or else not Compile_Time_Known_Value (High)
864 or else Etype (Index) = Any_Type
865 then
866 return False;
867
868 else
869 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
870
871 if Dim >= 0 then
872 Esiz := Esiz * Dim;
873 else
874 Esiz := Uint_0;
875 end if;
876 end if;
877
878 Next_Index (Index);
879 end loop;
880
881 Set_Small_Size (T, Esiz);
882 return True;
883 end;
884
885 -- Access types always have known at compile time sizes
886
887 elsif Is_Access_Type (T) then
888 return True;
889
890 -- For non-generic private types, go to underlying type if present
891
892 elsif Is_Private_Type (T)
893 and then not Is_Generic_Type (T)
894 and then Present (Underlying_Type (T))
895 then
896 -- Don't do any recursion on type with error posted, since we may
897 -- have a malformed type that leads us into a loop.
898
899 if Error_Posted (T) then
900 return False;
901 else
902 return Size_Known (Underlying_Type (T));
903 end if;
904
905 -- Record types
906
907 elsif Is_Record_Type (T) then
908
909 -- A class-wide type is never considered to have a known size
910
911 if Is_Class_Wide_Type (T) then
912 return False;
913
914 -- A subtype of a variant record must not have non-static
915 -- discriminated components.
916
917 elsif T /= Base_Type (T)
918 and then not Static_Discriminated_Components (T)
919 then
920 return False;
921
922 -- Don't do any recursion on type with error posted, since we may
923 -- have a malformed type that leads us into a loop.
924
925 elsif Error_Posted (T) then
926 return False;
927 end if;
928
929 -- Now look at the components of the record
930
931 declare
932 -- The following two variables are used to keep track of the
933 -- size of packed records if we can tell the size of the packed
934 -- record in the front end. Packed_Size_Known is True if so far
935 -- we can figure out the size. It is initialized to True for a
936 -- packed record, unless the record has discriminants or atomic
937 -- components or independent components.
938
939 -- The reason we eliminate the discriminated case is that
940 -- we don't know the way the back end lays out discriminated
941 -- packed records. If Packed_Size_Known is True, then
942 -- Packed_Size is the size in bits so far.
943
944 Packed_Size_Known : Boolean :=
945 Is_Packed (T)
946 and then not Has_Discriminants (T)
947 and then not Has_Atomic_Components (T)
948 and then not Has_Independent_Components (T);
949
950 Packed_Size : Uint := Uint_0;
951 -- Size in bits so far
952
953 begin
954 -- Test for variant part present
955
956 if Has_Discriminants (T)
957 and then Present (Parent (T))
958 and then Nkind (Parent (T)) = N_Full_Type_Declaration
959 and then Nkind (Type_Definition (Parent (T))) =
960 N_Record_Definition
961 and then not Null_Present (Type_Definition (Parent (T)))
962 and then
963 Present (Variant_Part
964 (Component_List (Type_Definition (Parent (T)))))
965 then
966 -- If variant part is present, and type is unconstrained,
967 -- then we must have defaulted discriminants, or a size
968 -- clause must be present for the type, or else the size
969 -- is definitely not known at compile time.
970
971 if not Is_Constrained (T)
972 and then
973 No (Discriminant_Default_Value (First_Discriminant (T)))
974 and then Unknown_RM_Size (T)
975 then
976 return False;
977 end if;
978 end if;
979
980 -- Loop through components
981
982 Comp := First_Component_Or_Discriminant (T);
983 while Present (Comp) loop
984 Ctyp := Etype (Comp);
985
986 -- We do not know the packed size if there is a component
987 -- clause present (we possibly could, but this would only
988 -- help in the case of a record with partial rep clauses.
989 -- That's because in the case of full rep clauses, the
990 -- size gets figured out anyway by a different circuit).
991
992 if Present (Component_Clause (Comp)) then
993 Packed_Size_Known := False;
994 end if;
995
996 -- We do not know the packed size for an atomic/VFA type
997 -- or component, or an independent type or component, or a
998 -- by-reference type or aliased component (because packing
999 -- does not touch these).
1000
1001 if Is_Atomic_Or_VFA (Ctyp)
1002 or else Is_Atomic_Or_VFA (Comp)
1003 or else Is_Independent (Ctyp)
1004 or else Is_Independent (Comp)
1005 or else Is_By_Reference_Type (Ctyp)
1006 or else Is_Aliased (Comp)
1007 then
1008 Packed_Size_Known := False;
1009 end if;
1010
1011 -- We need to identify a component that is an array where
1012 -- the index type is an enumeration type with non-standard
1013 -- representation, and some bound of the type depends on a
1014 -- discriminant.
1015
1016 -- This is because gigi computes the size by doing a
1017 -- substitution of the appropriate discriminant value in
1018 -- the size expression for the base type, and gigi is not
1019 -- clever enough to evaluate the resulting expression (which
1020 -- involves a call to rep_to_pos) at compile time.
1021
1022 -- It would be nice if gigi would either recognize that
1023 -- this expression can be computed at compile time, or
1024 -- alternatively figured out the size from the subtype
1025 -- directly, where all the information is at hand ???
1026
1027 if Is_Array_Type (Etype (Comp))
1028 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1029 then
1030 declare
1031 Ocomp : constant Entity_Id :=
1032 Original_Record_Component (Comp);
1033 OCtyp : constant Entity_Id := Etype (Ocomp);
1034 Ind : Node_Id;
1035 Indtyp : Entity_Id;
1036 Lo, Hi : Node_Id;
1037
1038 begin
1039 Ind := First_Index (OCtyp);
1040 while Present (Ind) loop
1041 Indtyp := Etype (Ind);
1042
1043 if Is_Enumeration_Type (Indtyp)
1044 and then Has_Non_Standard_Rep (Indtyp)
1045 then
1046 Lo := Type_Low_Bound (Indtyp);
1047 Hi := Type_High_Bound (Indtyp);
1048
1049 if Is_Entity_Name (Lo)
1050 and then Ekind (Entity (Lo)) = E_Discriminant
1051 then
1052 return False;
1053
1054 elsif Is_Entity_Name (Hi)
1055 and then Ekind (Entity (Hi)) = E_Discriminant
1056 then
1057 return False;
1058 end if;
1059 end if;
1060
1061 Next_Index (Ind);
1062 end loop;
1063 end;
1064 end if;
1065
1066 -- Clearly size of record is not known if the size of one of
1067 -- the components is not known.
1068
1069 if not Size_Known (Ctyp) then
1070 return False;
1071 end if;
1072
1073 -- Accumulate packed size if possible
1074
1075 if Packed_Size_Known then
1076
1077 -- We can only deal with elementary types, since for
1078 -- non-elementary components, alignment enters into the
1079 -- picture, and we don't know enough to handle proper
1080 -- alignment in this context. Packed arrays count as
1081 -- elementary if the representation is a modular type.
1082
1083 if Is_Elementary_Type (Ctyp)
1084 or else (Is_Array_Type (Ctyp)
1085 and then Present
1086 (Packed_Array_Impl_Type (Ctyp))
1087 and then Is_Modular_Integer_Type
1088 (Packed_Array_Impl_Type (Ctyp)))
1089 then
1090 -- Packed size unknown if we have an atomic/VFA type
1091 -- or a by-reference type, since the back end knows
1092 -- how these are layed out.
1093
1094 if Is_Atomic_Or_VFA (Ctyp)
1095 or else Is_By_Reference_Type (Ctyp)
1096 then
1097 Packed_Size_Known := False;
1098
1099 -- If RM_Size is known and static, then we can keep
1100 -- accumulating the packed size
1101
1102 elsif Known_Static_RM_Size (Ctyp) then
1103
1104 -- A little glitch, to be removed sometime ???
1105 -- gigi does not understand zero sizes yet.
1106
1107 if RM_Size (Ctyp) = Uint_0 then
1108 Packed_Size_Known := False;
1109
1110 -- Normal case where we can keep accumulating the
1111 -- packed array size.
1112
1113 else
1114 Packed_Size := Packed_Size + RM_Size (Ctyp);
1115 end if;
1116
1117 -- If we have a field whose RM_Size is not known then
1118 -- we can't figure out the packed size here.
1119
1120 else
1121 Packed_Size_Known := False;
1122 end if;
1123
1124 -- If we have a non-elementary type we can't figure out
1125 -- the packed array size (alignment issues).
1126
1127 else
1128 Packed_Size_Known := False;
1129 end if;
1130 end if;
1131
1132 Next_Component_Or_Discriminant (Comp);
1133 end loop;
1134
1135 if Packed_Size_Known then
1136 Set_Small_Size (T, Packed_Size);
1137 end if;
1138
1139 return True;
1140 end;
1141
1142 -- All other cases, size not known at compile time
1143
1144 else
1145 return False;
1146 end if;
1147 end Size_Known;
1148
1149 -------------------------------------
1150 -- Static_Discriminated_Components --
1151 -------------------------------------
1152
1153 function Static_Discriminated_Components
1154 (T : Entity_Id) return Boolean
1155 is
1156 Constraint : Elmt_Id;
1157
1158 begin
1159 if Has_Discriminants (T)
1160 and then Present (Discriminant_Constraint (T))
1161 and then Present (First_Component (T))
1162 then
1163 Constraint := First_Elmt (Discriminant_Constraint (T));
1164 while Present (Constraint) loop
1165 if not Compile_Time_Known_Value (Node (Constraint)) then
1166 return False;
1167 end if;
1168
1169 Next_Elmt (Constraint);
1170 end loop;
1171 end if;
1172
1173 return True;
1174 end Static_Discriminated_Components;
1175
1176 -- Start of processing for Check_Compile_Time_Size
1177
1178 begin
1179 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1180 end Check_Compile_Time_Size;
1181
1182 -----------------------------------
1183 -- Check_Component_Storage_Order --
1184 -----------------------------------
1185
1186 procedure Check_Component_Storage_Order
1187 (Encl_Type : Entity_Id;
1188 Comp : Entity_Id;
1189 ADC : Node_Id;
1190 Comp_ADC_Present : out Boolean)
1191 is
1192 Comp_Type : Entity_Id;
1193 Comp_ADC : Node_Id;
1194 Err_Node : Node_Id;
1195
1196 Comp_Byte_Aligned : Boolean;
1197 -- Set for the record case, True if Comp starts on a byte boundary
1198 -- (in which case it is allowed to have different storage order).
1199
1200 Comp_SSO_Differs : Boolean;
1201 -- Set True when the component is a nested composite, and it does not
1202 -- have the same scalar storage order as Encl_Type.
1203
1204 Component_Aliased : Boolean;
1205
1206 begin
1207 -- Record case
1208
1209 if Present (Comp) then
1210 Err_Node := Comp;
1211 Comp_Type := Etype (Comp);
1212
1213 if Is_Tag (Comp) then
1214 Comp_Byte_Aligned := True;
1215 Component_Aliased := False;
1216
1217 else
1218 -- If a component clause is present, check if the component starts
1219 -- on a storage element boundary. Otherwise conservatively assume
1220 -- it does so only in the case where the record is not packed.
1221
1222 if Present (Component_Clause (Comp)) then
1223 Comp_Byte_Aligned :=
1224 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0;
1225 else
1226 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1227 end if;
1228
1229 Component_Aliased := Is_Aliased (Comp);
1230 end if;
1231
1232 -- Array case
1233
1234 else
1235 Err_Node := Encl_Type;
1236 Comp_Type := Component_Type (Encl_Type);
1237
1238 Component_Aliased := Has_Aliased_Components (Encl_Type);
1239 end if;
1240
1241 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1242 -- the attribute definition clause is attached to the first subtype.
1243
1244 Comp_Type := Base_Type (Comp_Type);
1245 Comp_ADC := Get_Attribute_Definition_Clause
1246 (First_Subtype (Comp_Type),
1247 Attribute_Scalar_Storage_Order);
1248 Comp_ADC_Present := Present (Comp_ADC);
1249
1250 -- Case of record or array component: check storage order compatibility.
1251 -- But, if the record has Complex_Representation, then it is treated as
1252 -- a scalar in the back end so the storage order is irrelevant.
1253
1254 if (Is_Record_Type (Comp_Type)
1255 and then not Has_Complex_Representation (Comp_Type))
1256 or else Is_Array_Type (Comp_Type)
1257 then
1258 Comp_SSO_Differs :=
1259 Reverse_Storage_Order (Encl_Type)
1260 /=
1261 Reverse_Storage_Order (Comp_Type);
1262
1263 -- Parent and extension must have same storage order
1264
1265 if Present (Comp) and then Chars (Comp) = Name_uParent then
1266 if Comp_SSO_Differs then
1267 Error_Msg_N
1268 ("record extension must have same scalar storage order as "
1269 & "parent", Err_Node);
1270 end if;
1271
1272 -- If enclosing composite has explicit SSO then nested composite must
1273 -- have explicit SSO as well.
1274
1275 elsif Present (ADC) and then No (Comp_ADC) then
1276 Error_Msg_N ("nested composite must have explicit scalar "
1277 & "storage order", Err_Node);
1278
1279 -- If component and composite SSO differs, check that component
1280 -- falls on byte boundaries and isn't packed.
1281
1282 elsif Comp_SSO_Differs then
1283
1284 -- Component SSO differs from enclosing composite:
1285
1286 -- Reject if component is a packed array, as it may be represented
1287 -- as a scalar internally.
1288
1289 if Is_Packed_Array (Comp_Type) then
1290 Error_Msg_N
1291 ("type of packed component must have same scalar "
1292 & "storage order as enclosing composite", Err_Node);
1293
1294 -- Reject if composite is a packed array, as it may be rewritten
1295 -- into an array of scalars.
1296
1297 elsif Is_Packed_Array (Encl_Type) then
1298 Error_Msg_N ("type of packed array must have same scalar "
1299 & "storage order as component", Err_Node);
1300
1301 -- Reject if not byte aligned
1302
1303 elsif Is_Record_Type (Encl_Type)
1304 and then not Comp_Byte_Aligned
1305 then
1306 Error_Msg_N
1307 ("type of non-byte-aligned component must have same scalar "
1308 & "storage order as enclosing composite", Err_Node);
1309 end if;
1310 end if;
1311
1312 -- Enclosing type has explicit SSO: non-composite component must not
1313 -- be aliased.
1314
1315 elsif Present (ADC) and then Component_Aliased then
1316 Error_Msg_N
1317 ("aliased component not permitted for type with "
1318 & "explicit Scalar_Storage_Order", Err_Node);
1319 end if;
1320 end Check_Component_Storage_Order;
1321
1322 -----------------------------
1323 -- Check_Debug_Info_Needed --
1324 -----------------------------
1325
1326 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1327 begin
1328 if Debug_Info_Off (T) then
1329 return;
1330
1331 elsif Comes_From_Source (T)
1332 or else Debug_Generated_Code
1333 or else Debug_Flag_VV
1334 or else Needs_Debug_Info (T)
1335 then
1336 Set_Debug_Info_Needed (T);
1337 end if;
1338 end Check_Debug_Info_Needed;
1339
1340 -------------------------------
1341 -- Check_Expression_Function --
1342 -------------------------------
1343
1344 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1345 Decl : Node_Id;
1346
1347 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1348 -- Function to search for deferred constant
1349
1350 -------------------
1351 -- Find_Constant --
1352 -------------------
1353
1354 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1355 begin
1356 -- When a constant is initialized with the result of a dispatching
1357 -- call, the constant declaration is rewritten as a renaming of the
1358 -- displaced function result. This scenario is not a premature use of
1359 -- a constant even though the Has_Completion flag is not set.
1360
1361 if Is_Entity_Name (Nod)
1362 and then Present (Entity (Nod))
1363 and then Ekind (Entity (Nod)) = E_Constant
1364 and then Scope (Entity (Nod)) = Current_Scope
1365 and then Nkind (Declaration_Node (Entity (Nod))) =
1366 N_Object_Declaration
1367 and then not Is_Imported (Entity (Nod))
1368 and then not Has_Completion (Entity (Nod))
1369 then
1370 Error_Msg_NE
1371 ("premature use of& in call or instance", N, Entity (Nod));
1372
1373 elsif Nkind (Nod) = N_Attribute_Reference then
1374 Analyze (Prefix (Nod));
1375
1376 if Is_Entity_Name (Prefix (Nod))
1377 and then Is_Type (Entity (Prefix (Nod)))
1378 then
1379 Freeze_Before (N, Entity (Prefix (Nod)));
1380 end if;
1381 end if;
1382
1383 return OK;
1384 end Find_Constant;
1385
1386 procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1387
1388 -- Start of processing for Check_Expression_Function
1389
1390 begin
1391 Decl := Original_Node (Unit_Declaration_Node (Nam));
1392
1393 if Scope (Nam) = Current_Scope
1394 and then Nkind (Decl) = N_Expression_Function
1395 then
1396 Check_Deferred (Expression (Decl));
1397 end if;
1398 end Check_Expression_Function;
1399
1400 ----------------------------
1401 -- Check_Strict_Alignment --
1402 ----------------------------
1403
1404 procedure Check_Strict_Alignment (E : Entity_Id) is
1405 Comp : Entity_Id;
1406
1407 begin
1408 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1409 Set_Strict_Alignment (E);
1410
1411 elsif Is_Array_Type (E) then
1412 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1413
1414 elsif Is_Record_Type (E) then
1415 if Is_Limited_Record (E) then
1416 Set_Strict_Alignment (E);
1417 return;
1418 end if;
1419
1420 Comp := First_Component (E);
1421 while Present (Comp) loop
1422 if not Is_Type (Comp)
1423 and then (Strict_Alignment (Etype (Comp))
1424 or else Is_Aliased (Comp))
1425 then
1426 Set_Strict_Alignment (E);
1427 return;
1428 end if;
1429
1430 Next_Component (Comp);
1431 end loop;
1432 end if;
1433 end Check_Strict_Alignment;
1434
1435 -------------------------
1436 -- Check_Unsigned_Type --
1437 -------------------------
1438
1439 procedure Check_Unsigned_Type (E : Entity_Id) is
1440 Ancestor : Entity_Id;
1441 Lo_Bound : Node_Id;
1442 Btyp : Entity_Id;
1443
1444 begin
1445 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1446 return;
1447 end if;
1448
1449 -- Do not attempt to analyze case where range was in error
1450
1451 if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
1452 return;
1453 end if;
1454
1455 -- The situation that is nontrivial is something like:
1456
1457 -- subtype x1 is integer range -10 .. +10;
1458 -- subtype x2 is x1 range 0 .. V1;
1459 -- subtype x3 is x2 range V2 .. V3;
1460 -- subtype x4 is x3 range V4 .. V5;
1461
1462 -- where Vn are variables. Here the base type is signed, but we still
1463 -- know that x4 is unsigned because of the lower bound of x2.
1464
1465 -- The only way to deal with this is to look up the ancestor chain
1466
1467 Ancestor := E;
1468 loop
1469 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1470 return;
1471 end if;
1472
1473 Lo_Bound := Type_Low_Bound (Ancestor);
1474
1475 if Compile_Time_Known_Value (Lo_Bound) then
1476 if Expr_Rep_Value (Lo_Bound) >= 0 then
1477 Set_Is_Unsigned_Type (E, True);
1478 end if;
1479
1480 return;
1481
1482 else
1483 Ancestor := Ancestor_Subtype (Ancestor);
1484
1485 -- If no ancestor had a static lower bound, go to base type
1486
1487 if No (Ancestor) then
1488
1489 -- Note: the reason we still check for a compile time known
1490 -- value for the base type is that at least in the case of
1491 -- generic formals, we can have bounds that fail this test,
1492 -- and there may be other cases in error situations.
1493
1494 Btyp := Base_Type (E);
1495
1496 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1497 return;
1498 end if;
1499
1500 Lo_Bound := Type_Low_Bound (Base_Type (E));
1501
1502 if Compile_Time_Known_Value (Lo_Bound)
1503 and then Expr_Rep_Value (Lo_Bound) >= 0
1504 then
1505 Set_Is_Unsigned_Type (E, True);
1506 end if;
1507
1508 return;
1509 end if;
1510 end if;
1511 end loop;
1512 end Check_Unsigned_Type;
1513
1514 -----------------------------
1515 -- Is_Atomic_VFA_Aggregate --
1516 -----------------------------
1517
1518 function Is_Atomic_VFA_Aggregate (N : Node_Id) return Boolean is
1519 Loc : constant Source_Ptr := Sloc (N);
1520 New_N : Node_Id;
1521 Par : Node_Id;
1522 Temp : Entity_Id;
1523 Typ : Entity_Id;
1524
1525 begin
1526 Par := Parent (N);
1527
1528 -- Array may be qualified, so find outer context
1529
1530 if Nkind (Par) = N_Qualified_Expression then
1531 Par := Parent (Par);
1532 end if;
1533
1534 if not Comes_From_Source (Par) then
1535 return False;
1536 end if;
1537
1538 case Nkind (Par) is
1539 when N_Assignment_Statement =>
1540 Typ := Etype (Name (Par));
1541
1542 if not Is_Atomic_Or_VFA (Typ)
1543 and then not (Is_Entity_Name (Name (Par))
1544 and then Is_Atomic_Or_VFA (Entity (Name (Par))))
1545 then
1546 return False;
1547 end if;
1548
1549 when N_Object_Declaration =>
1550 Typ := Etype (Defining_Identifier (Par));
1551
1552 if not Is_Atomic_Or_VFA (Typ)
1553 and then not Is_Atomic_Or_VFA (Defining_Identifier (Par))
1554 then
1555 return False;
1556 end if;
1557
1558 when others =>
1559 return False;
1560 end case;
1561
1562 Temp := Make_Temporary (Loc, 'T', N);
1563 New_N :=
1564 Make_Object_Declaration (Loc,
1565 Defining_Identifier => Temp,
1566 Object_Definition => New_Occurrence_Of (Typ, Loc),
1567 Expression => Relocate_Node (N));
1568 Insert_Before (Par, New_N);
1569 Analyze (New_N);
1570
1571 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1572 return True;
1573 end Is_Atomic_VFA_Aggregate;
1574
1575 -----------------------------------------------
1576 -- Explode_Initialization_Compound_Statement --
1577 -----------------------------------------------
1578
1579 procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
1580 Init_Stmts : constant Node_Id := Initialization_Statements (E);
1581
1582 begin
1583 if Present (Init_Stmts)
1584 and then Nkind (Init_Stmts) = N_Compound_Statement
1585 then
1586 Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
1587
1588 -- Note that we rewrite Init_Stmts into a NULL statement, rather than
1589 -- just removing it, because Freeze_All may rely on this particular
1590 -- Node_Id still being present in the enclosing list to know where to
1591 -- stop freezing.
1592
1593 Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
1594
1595 Set_Initialization_Statements (E, Empty);
1596 end if;
1597 end Explode_Initialization_Compound_Statement;
1598
1599 ----------------
1600 -- Freeze_All --
1601 ----------------
1602
1603 -- Note: the easy coding for this procedure would be to just build a
1604 -- single list of freeze nodes and then insert them and analyze them
1605 -- all at once. This won't work, because the analysis of earlier freeze
1606 -- nodes may recursively freeze types which would otherwise appear later
1607 -- on in the freeze list. So we must analyze and expand the freeze nodes
1608 -- as they are generated.
1609
1610 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1611 E : Entity_Id;
1612 Decl : Node_Id;
1613
1614 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1615 -- This is the internal recursive routine that does freezing of entities
1616 -- (but NOT the analysis of default expressions, which should not be
1617 -- recursive, we don't want to analyze those till we are sure that ALL
1618 -- the types are frozen).
1619
1620 --------------------
1621 -- Freeze_All_Ent --
1622 --------------------
1623
1624 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
1625 E : Entity_Id;
1626 Flist : List_Id;
1627 Lastn : Node_Id;
1628
1629 procedure Process_Flist;
1630 -- If freeze nodes are present, insert and analyze, and reset cursor
1631 -- for next insertion.
1632
1633 -------------------
1634 -- Process_Flist --
1635 -------------------
1636
1637 procedure Process_Flist is
1638 begin
1639 if Is_Non_Empty_List (Flist) then
1640 Lastn := Next (After);
1641 Insert_List_After_And_Analyze (After, Flist);
1642
1643 if Present (Lastn) then
1644 After := Prev (Lastn);
1645 else
1646 After := Last (List_Containing (After));
1647 end if;
1648 end if;
1649 end Process_Flist;
1650
1651 -- Start of processing for Freeze_All_Ent
1652
1653 begin
1654 E := From;
1655 while Present (E) loop
1656
1657 -- If the entity is an inner package which is not a package
1658 -- renaming, then its entities must be frozen at this point. Note
1659 -- that such entities do NOT get frozen at the end of the nested
1660 -- package itself (only library packages freeze).
1661
1662 -- Same is true for task declarations, where anonymous records
1663 -- created for entry parameters must be frozen.
1664
1665 if Ekind (E) = E_Package
1666 and then No (Renamed_Object (E))
1667 and then not Is_Child_Unit (E)
1668 and then not Is_Frozen (E)
1669 then
1670 Push_Scope (E);
1671 Install_Visible_Declarations (E);
1672 Install_Private_Declarations (E);
1673
1674 Freeze_All (First_Entity (E), After);
1675
1676 End_Package_Scope (E);
1677
1678 if Is_Generic_Instance (E)
1679 and then Has_Delayed_Freeze (E)
1680 then
1681 Set_Has_Delayed_Freeze (E, False);
1682 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1683 end if;
1684
1685 elsif Ekind (E) in Task_Kind
1686 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1687 N_Single_Task_Declaration)
1688 then
1689 Push_Scope (E);
1690 Freeze_All (First_Entity (E), After);
1691 End_Scope;
1692
1693 -- For a derived tagged type, we must ensure that all the
1694 -- primitive operations of the parent have been frozen, so that
1695 -- their addresses will be in the parent's dispatch table at the
1696 -- point it is inherited.
1697
1698 elsif Ekind (E) = E_Record_Type
1699 and then Is_Tagged_Type (E)
1700 and then Is_Tagged_Type (Etype (E))
1701 and then Is_Derived_Type (E)
1702 then
1703 declare
1704 Prim_List : constant Elist_Id :=
1705 Primitive_Operations (Etype (E));
1706
1707 Prim : Elmt_Id;
1708 Subp : Entity_Id;
1709
1710 begin
1711 Prim := First_Elmt (Prim_List);
1712 while Present (Prim) loop
1713 Subp := Node (Prim);
1714
1715 if Comes_From_Source (Subp)
1716 and then not Is_Frozen (Subp)
1717 then
1718 Flist := Freeze_Entity (Subp, After);
1719 Process_Flist;
1720 end if;
1721
1722 Next_Elmt (Prim);
1723 end loop;
1724 end;
1725 end if;
1726
1727 if not Is_Frozen (E) then
1728 Flist := Freeze_Entity (E, After);
1729 Process_Flist;
1730
1731 -- If already frozen, and there are delayed aspects, this is where
1732 -- we do the visibility check for these aspects (see Sem_Ch13 spec
1733 -- for a description of how we handle aspect visibility).
1734
1735 elsif Has_Delayed_Aspects (E) then
1736
1737 -- Retrieve the visibility to the discriminants in order to
1738 -- analyze properly the aspects.
1739
1740 Push_Scope_And_Install_Discriminants (E);
1741
1742 declare
1743 Ritem : Node_Id;
1744
1745 begin
1746 Ritem := First_Rep_Item (E);
1747 while Present (Ritem) loop
1748 if Nkind (Ritem) = N_Aspect_Specification
1749 and then Entity (Ritem) = E
1750 and then Is_Delayed_Aspect (Ritem)
1751 then
1752 Check_Aspect_At_End_Of_Declarations (Ritem);
1753 end if;
1754
1755 Ritem := Next_Rep_Item (Ritem);
1756 end loop;
1757 end;
1758
1759 Uninstall_Discriminants_And_Pop_Scope (E);
1760 end if;
1761
1762 -- If an incomplete type is still not frozen, this may be a
1763 -- premature freezing because of a body declaration that follows.
1764 -- Indicate where the freezing took place. Freezing will happen
1765 -- if the body comes from source, but not if it is internally
1766 -- generated, for example as the body of a type invariant.
1767
1768 -- If the freezing is caused by the end of the current declarative
1769 -- part, it is a Taft Amendment type, and there is no error.
1770
1771 if not Is_Frozen (E)
1772 and then Ekind (E) = E_Incomplete_Type
1773 then
1774 declare
1775 Bod : constant Node_Id := Next (After);
1776
1777 begin
1778 -- The presence of a body freezes all entities previously
1779 -- declared in the current list of declarations, but this
1780 -- does not apply if the body does not come from source.
1781 -- A type invariant is transformed into a subprogram body
1782 -- which is placed at the end of the private part of the
1783 -- current package, but this body does not freeze incomplete
1784 -- types that may be declared in this private part.
1785
1786 if (Nkind_In (Bod, N_Subprogram_Body,
1787 N_Entry_Body,
1788 N_Package_Body,
1789 N_Protected_Body,
1790 N_Task_Body)
1791 or else Nkind (Bod) in N_Body_Stub)
1792 and then
1793 List_Containing (After) = List_Containing (Parent (E))
1794 and then Comes_From_Source (Bod)
1795 then
1796 Error_Msg_Sloc := Sloc (Next (After));
1797 Error_Msg_NE
1798 ("type& is frozen# before its full declaration",
1799 Parent (E), E);
1800 end if;
1801 end;
1802 end if;
1803
1804 Next_Entity (E);
1805 end loop;
1806 end Freeze_All_Ent;
1807
1808 -- Start of processing for Freeze_All
1809
1810 begin
1811 Freeze_All_Ent (From, After);
1812
1813 -- Now that all types are frozen, we can deal with default expressions
1814 -- that require us to build a default expression functions. This is the
1815 -- point at which such functions are constructed (after all types that
1816 -- might be used in such expressions have been frozen).
1817
1818 -- For subprograms that are renaming_as_body, we create the wrapper
1819 -- bodies as needed.
1820
1821 -- We also add finalization chains to access types whose designated
1822 -- types are controlled. This is normally done when freezing the type,
1823 -- but this misses recursive type definitions where the later members
1824 -- of the recursion introduce controlled components.
1825
1826 -- Loop through entities
1827
1828 E := From;
1829 while Present (E) loop
1830 if Is_Subprogram (E) then
1831 if not Default_Expressions_Processed (E) then
1832 Process_Default_Expressions (E, After);
1833 end if;
1834
1835 if not Has_Completion (E) then
1836 Decl := Unit_Declaration_Node (E);
1837
1838 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1839 if Error_Posted (Decl) then
1840 Set_Has_Completion (E);
1841 else
1842 Build_And_Analyze_Renamed_Body (Decl, E, After);
1843 end if;
1844
1845 elsif Nkind (Decl) = N_Subprogram_Declaration
1846 and then Present (Corresponding_Body (Decl))
1847 and then
1848 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1849 = N_Subprogram_Renaming_Declaration
1850 then
1851 Build_And_Analyze_Renamed_Body
1852 (Decl, Corresponding_Body (Decl), After);
1853 end if;
1854 end if;
1855
1856 elsif Ekind (E) in Task_Kind
1857 and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1858 N_Single_Task_Declaration)
1859 then
1860 declare
1861 Ent : Entity_Id;
1862
1863 begin
1864 Ent := First_Entity (E);
1865 while Present (Ent) loop
1866 if Is_Entry (Ent)
1867 and then not Default_Expressions_Processed (Ent)
1868 then
1869 Process_Default_Expressions (Ent, After);
1870 end if;
1871
1872 Next_Entity (Ent);
1873 end loop;
1874 end;
1875 end if;
1876
1877 -- Historical note: We used to create a finalization master for an
1878 -- access type whose designated type is not controlled, but contains
1879 -- private controlled compoments. This form of postprocessing is no
1880 -- longer needed because the finalization master is now created when
1881 -- the access type is frozen (see Exp_Ch3.Freeze_Type).
1882
1883 Next_Entity (E);
1884 end loop;
1885 end Freeze_All;
1886
1887 -----------------------
1888 -- Freeze_And_Append --
1889 -----------------------
1890
1891 procedure Freeze_And_Append
1892 (Ent : Entity_Id;
1893 N : Node_Id;
1894 Result : in out List_Id)
1895 is
1896 L : constant List_Id := Freeze_Entity (Ent, N);
1897 begin
1898 if Is_Non_Empty_List (L) then
1899 if Result = No_List then
1900 Result := L;
1901 else
1902 Append_List (L, Result);
1903 end if;
1904 end if;
1905 end Freeze_And_Append;
1906
1907 -------------------
1908 -- Freeze_Before --
1909 -------------------
1910
1911 procedure Freeze_Before
1912 (N : Node_Id;
1913 T : Entity_Id;
1914 Do_Freeze_Profile : Boolean := True)
1915 is
1916 -- Freeze T, then insert the generated Freeze nodes before the node N.
1917 -- Flag Freeze_Profile is used when T is an overloadable entity, and
1918 -- indicates whether its profile should be frozen at the same time.
1919
1920 Freeze_Nodes : constant List_Id :=
1921 Freeze_Entity (T, N, Do_Freeze_Profile);
1922
1923 begin
1924 if Ekind (T) = E_Function then
1925 Check_Expression_Function (N, T);
1926 end if;
1927
1928 if Is_Non_Empty_List (Freeze_Nodes) then
1929 Insert_Actions (N, Freeze_Nodes);
1930 end if;
1931 end Freeze_Before;
1932
1933 -------------------
1934 -- Freeze_Entity --
1935 -------------------
1936
1937 function Freeze_Entity
1938 (E : Entity_Id;
1939 N : Node_Id;
1940 Do_Freeze_Profile : Boolean := True) return List_Id
1941 is
1942 Loc : constant Source_Ptr := Sloc (N);
1943 Atype : Entity_Id;
1944 Comp : Entity_Id;
1945 F_Node : Node_Id;
1946 Formal : Entity_Id;
1947 Indx : Node_Id;
1948
1949 Has_Default_Initialization : Boolean := False;
1950 -- This flag gets set to true for a variable with default initialization
1951
1952 Late_Freezing : Boolean := False;
1953 -- Used to detect attempt to freeze function declared in another unit
1954
1955 Result : List_Id := No_List;
1956 -- List of freezing actions, left at No_List if none
1957
1958 Test_E : Entity_Id := E;
1959 -- This could use a comment ???
1960
1961 procedure Add_To_Result (N : Node_Id);
1962 -- N is a freezing action to be appended to the Result
1963
1964 function After_Last_Declaration return Boolean;
1965 -- If Loc is a freeze_entity that appears after the last declaration
1966 -- in the scope, inhibit error messages on late completion.
1967
1968 procedure Check_Current_Instance (Comp_Decl : Node_Id);
1969 -- Check that an Access or Unchecked_Access attribute with a prefix
1970 -- which is the current instance type can only be applied when the type
1971 -- is limited.
1972
1973 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
1974 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
1975 -- integer literal without an explicit corresponding size clause. The
1976 -- caller has checked that Utype is a modular integer type.
1977
1978 procedure Freeze_Array_Type (Arr : Entity_Id);
1979 -- Freeze array type, including freezing index and component types
1980
1981 procedure Freeze_Object_Declaration (E : Entity_Id);
1982 -- Perform checks and generate freeze node if needed for a constant or
1983 -- variable declared by an object declaration.
1984
1985 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
1986 -- Create Freeze_Generic_Entity nodes for types declared in a generic
1987 -- package. Recurse on inner generic packages.
1988
1989 function Freeze_Profile (E : Entity_Id) return Boolean;
1990 -- Freeze formals and return type of subprogram. If some type in the
1991 -- profile is a limited view, freezing of the entity will take place
1992 -- elsewhere, and the function returns False. This routine will be
1993 -- modified if and when we can implement AI05-019 efficiently ???
1994
1995 procedure Freeze_Record_Type (Rec : Entity_Id);
1996 -- Freeze record type, including freezing component types, and freezing
1997 -- primitive operations if this is a tagged type.
1998
1999 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
2000 -- Determine whether an arbitrary entity is subject to Boolean aspect
2001 -- Import and its value is specified as True.
2002
2003 procedure Late_Freeze_Subprogram (E : Entity_Id);
2004 -- Following AI05-151, a function can return a limited view of a type
2005 -- declared elsewhere. In that case the function cannot be frozen at
2006 -- the end of its enclosing package. If its first use is in a different
2007 -- unit, it cannot be frozen there, but if the call is legal the full
2008 -- view of the return type is available and the subprogram can now be
2009 -- frozen. However the freeze node cannot be inserted at the point of
2010 -- call, but rather must go in the package holding the function, so that
2011 -- the backend can process it in the proper context.
2012
2013 function New_Freeze_Node return Node_Id;
2014 -- Create a new freeze node for entity E
2015
2016 procedure Wrap_Imported_Subprogram (E : Entity_Id);
2017 -- If E is an entity for an imported subprogram with pre/post-conditions
2018 -- then this procedure will create a wrapper to ensure that proper run-
2019 -- time checking of the pre/postconditions. See body for details.
2020
2021 -------------------
2022 -- Add_To_Result --
2023 -------------------
2024
2025 procedure Add_To_Result (N : Node_Id) is
2026 begin
2027 if No (Result) then
2028 Result := New_List (N);
2029 else
2030 Append (N, Result);
2031 end if;
2032 end Add_To_Result;
2033
2034 ----------------------------
2035 -- After_Last_Declaration --
2036 ----------------------------
2037
2038 function After_Last_Declaration return Boolean is
2039 Spec : constant Node_Id := Parent (Current_Scope);
2040
2041 begin
2042 if Nkind (Spec) = N_Package_Specification then
2043 if Present (Private_Declarations (Spec)) then
2044 return Loc >= Sloc (Last (Private_Declarations (Spec)));
2045 elsif Present (Visible_Declarations (Spec)) then
2046 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2047 else
2048 return False;
2049 end if;
2050
2051 else
2052 return False;
2053 end if;
2054 end After_Last_Declaration;
2055
2056 ----------------------------
2057 -- Check_Current_Instance --
2058 ----------------------------
2059
2060 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2061
2062 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2063 -- Determine whether Typ is compatible with the rules for aliased
2064 -- views of types as defined in RM 3.10 in the various dialects.
2065
2066 function Process (N : Node_Id) return Traverse_Result;
2067 -- Process routine to apply check to given node
2068
2069 -----------------------------
2070 -- Is_Aliased_View_Of_Type --
2071 -----------------------------
2072
2073 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2074 Typ_Decl : constant Node_Id := Parent (Typ);
2075
2076 begin
2077 -- Common case
2078
2079 if Nkind (Typ_Decl) = N_Full_Type_Declaration
2080 and then Limited_Present (Type_Definition (Typ_Decl))
2081 then
2082 return True;
2083
2084 -- The following paragraphs describe what a legal aliased view of
2085 -- a type is in the various dialects of Ada.
2086
2087 -- Ada 95
2088
2089 -- The current instance of a limited type, and a formal parameter
2090 -- or generic formal object of a tagged type.
2091
2092 -- Ada 95 limited type
2093 -- * Type with reserved word "limited"
2094 -- * A protected or task type
2095 -- * A composite type with limited component
2096
2097 elsif Ada_Version <= Ada_95 then
2098 return Is_Limited_Type (Typ);
2099
2100 -- Ada 2005
2101
2102 -- The current instance of a limited tagged type, a protected
2103 -- type, a task type, or a type that has the reserved word
2104 -- "limited" in its full definition ... a formal parameter or
2105 -- generic formal object of a tagged type.
2106
2107 -- Ada 2005 limited type
2108 -- * Type with reserved word "limited", "synchronized", "task"
2109 -- or "protected"
2110 -- * A composite type with limited component
2111 -- * A derived type whose parent is a non-interface limited type
2112
2113 elsif Ada_Version = Ada_2005 then
2114 return
2115 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2116 or else
2117 (Is_Derived_Type (Typ)
2118 and then not Is_Interface (Etype (Typ))
2119 and then Is_Limited_Type (Etype (Typ)));
2120
2121 -- Ada 2012 and beyond
2122
2123 -- The current instance of an immutably limited type ... a formal
2124 -- parameter or generic formal object of a tagged type.
2125
2126 -- Ada 2012 limited type
2127 -- * Type with reserved word "limited", "synchronized", "task"
2128 -- or "protected"
2129 -- * A composite type with limited component
2130 -- * A derived type whose parent is a non-interface limited type
2131 -- * An incomplete view
2132
2133 -- Ada 2012 immutably limited type
2134 -- * Explicitly limited record type
2135 -- * Record extension with "limited" present
2136 -- * Non-formal limited private type that is either tagged
2137 -- or has at least one access discriminant with a default
2138 -- expression
2139 -- * Task type, protected type or synchronized interface
2140 -- * Type derived from immutably limited type
2141
2142 else
2143 return
2144 Is_Immutably_Limited_Type (Typ)
2145 or else Is_Incomplete_Type (Typ);
2146 end if;
2147 end Is_Aliased_View_Of_Type;
2148
2149 -------------
2150 -- Process --
2151 -------------
2152
2153 function Process (N : Node_Id) return Traverse_Result is
2154 begin
2155 case Nkind (N) is
2156 when N_Attribute_Reference =>
2157 if Nam_In (Attribute_Name (N), Name_Access,
2158 Name_Unchecked_Access)
2159 and then Is_Entity_Name (Prefix (N))
2160 and then Is_Type (Entity (Prefix (N)))
2161 and then Entity (Prefix (N)) = E
2162 then
2163 if Ada_Version < Ada_2012 then
2164 Error_Msg_N
2165 ("current instance must be a limited type",
2166 Prefix (N));
2167 else
2168 Error_Msg_N
2169 ("current instance must be an immutably limited "
2170 & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2171 end if;
2172
2173 return Abandon;
2174
2175 else
2176 return OK;
2177 end if;
2178
2179 when others => return OK;
2180 end case;
2181 end Process;
2182
2183 procedure Traverse is new Traverse_Proc (Process);
2184
2185 -- Local variables
2186
2187 Rec_Type : constant Entity_Id :=
2188 Scope (Defining_Identifier (Comp_Decl));
2189
2190 -- Start of processing for Check_Current_Instance
2191
2192 begin
2193 if not Is_Aliased_View_Of_Type (Rec_Type) then
2194 Traverse (Comp_Decl);
2195 end if;
2196 end Check_Current_Instance;
2197
2198 ------------------------------
2199 -- Check_Suspicious_Modulus --
2200 ------------------------------
2201
2202 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
2203 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
2204
2205 begin
2206 if not Warn_On_Suspicious_Modulus_Value then
2207 return;
2208 end if;
2209
2210 if Nkind (Decl) = N_Full_Type_Declaration then
2211 declare
2212 Tdef : constant Node_Id := Type_Definition (Decl);
2213
2214 begin
2215 if Nkind (Tdef) = N_Modular_Type_Definition then
2216 declare
2217 Modulus : constant Node_Id :=
2218 Original_Node (Expression (Tdef));
2219
2220 begin
2221 if Nkind (Modulus) = N_Integer_Literal then
2222 declare
2223 Modv : constant Uint := Intval (Modulus);
2224 Sizv : constant Uint := RM_Size (Utype);
2225
2226 begin
2227 -- First case, modulus and size are the same. This
2228 -- happens if you have something like mod 32, with
2229 -- an explicit size of 32, this is for sure a case
2230 -- where the warning is given, since it is seems
2231 -- very unlikely that someone would want e.g. a
2232 -- five bit type stored in 32 bits. It is much
2233 -- more likely they wanted a 32-bit type.
2234
2235 if Modv = Sizv then
2236 null;
2237
2238 -- Second case, the modulus is 32 or 64 and no
2239 -- size clause is present. This is a less clear
2240 -- case for giving the warning, but in the case
2241 -- of 32/64 (5-bit or 6-bit types) these seem rare
2242 -- enough that it is a likely error (and in any
2243 -- case using 2**5 or 2**6 in these cases seems
2244 -- clearer. We don't include 8 or 16 here, simply
2245 -- because in practice 3-bit and 4-bit types are
2246 -- more common and too many false positives if
2247 -- we warn in these cases.
2248
2249 elsif not Has_Size_Clause (Utype)
2250 and then (Modv = Uint_32 or else Modv = Uint_64)
2251 then
2252 null;
2253
2254 -- No warning needed
2255
2256 else
2257 return;
2258 end if;
2259
2260 -- If we fall through, give warning
2261
2262 Error_Msg_Uint_1 := Modv;
2263 Error_Msg_N
2264 ("?M?2 '*'*^' may have been intended here",
2265 Modulus);
2266 end;
2267 end if;
2268 end;
2269 end if;
2270 end;
2271 end if;
2272 end Check_Suspicious_Modulus;
2273
2274 -----------------------
2275 -- Freeze_Array_Type --
2276 -----------------------
2277
2278 procedure Freeze_Array_Type (Arr : Entity_Id) is
2279 FS : constant Entity_Id := First_Subtype (Arr);
2280 Ctyp : constant Entity_Id := Component_Type (Arr);
2281 Clause : Entity_Id;
2282
2283 Non_Standard_Enum : Boolean := False;
2284 -- Set true if any of the index types is an enumeration type with a
2285 -- non-standard representation.
2286
2287 begin
2288 Freeze_And_Append (Ctyp, N, Result);
2289
2290 Indx := First_Index (Arr);
2291 while Present (Indx) loop
2292 Freeze_And_Append (Etype (Indx), N, Result);
2293
2294 if Is_Enumeration_Type (Etype (Indx))
2295 and then Has_Non_Standard_Rep (Etype (Indx))
2296 then
2297 Non_Standard_Enum := True;
2298 end if;
2299
2300 Next_Index (Indx);
2301 end loop;
2302
2303 -- Processing that is done only for base types
2304
2305 if Ekind (Arr) = E_Array_Type then
2306
2307 -- Deal with default setting of reverse storage order
2308
2309 Set_SSO_From_Default (Arr);
2310
2311 -- Propagate flags for component type
2312
2313 if Is_Controlled_Active (Component_Type (Arr))
2314 or else Has_Controlled_Component (Ctyp)
2315 then
2316 Set_Has_Controlled_Component (Arr);
2317 end if;
2318
2319 if Has_Unchecked_Union (Component_Type (Arr)) then
2320 Set_Has_Unchecked_Union (Arr);
2321 end if;
2322
2323 -- Warn for pragma Pack overriding foreign convention
2324
2325 if Has_Foreign_Convention (Ctyp)
2326 and then Has_Pragma_Pack (Arr)
2327 then
2328 declare
2329 CN : constant Name_Id :=
2330 Get_Convention_Name (Convention (Ctyp));
2331 PP : constant Node_Id :=
2332 Get_Pragma (First_Subtype (Arr), Pragma_Pack);
2333 begin
2334 if Present (PP) then
2335 Error_Msg_Name_1 := CN;
2336 Error_Msg_Sloc := Sloc (Arr);
2337 Error_Msg_N
2338 ("pragma Pack affects convention % components #??", PP);
2339 Error_Msg_Name_1 := CN;
2340 Error_Msg_N
2341 ("\array components may not have % compatible "
2342 & "representation??", PP);
2343 end if;
2344 end;
2345 end if;
2346
2347 -- If packing was requested or if the component size was
2348 -- set explicitly, then see if bit packing is required. This
2349 -- processing is only done for base types, since all of the
2350 -- representation aspects involved are type-related.
2351
2352 -- This is not just an optimization, if we start processing the
2353 -- subtypes, they interfere with the settings on the base type
2354 -- (this is because Is_Packed has a slightly different meaning
2355 -- before and after freezing).
2356
2357 declare
2358 Csiz : Uint;
2359 Esiz : Uint;
2360
2361 begin
2362 if (Is_Packed (Arr) or else Has_Pragma_Pack (Arr))
2363 and then Known_Static_RM_Size (Ctyp)
2364 and then not Has_Component_Size_Clause (Arr)
2365 then
2366 Csiz := UI_Max (RM_Size (Ctyp), 1);
2367
2368 elsif Known_Component_Size (Arr) then
2369 Csiz := Component_Size (Arr);
2370
2371 elsif not Known_Static_Esize (Ctyp) then
2372 Csiz := Uint_0;
2373
2374 else
2375 Esiz := Esize (Ctyp);
2376
2377 -- We can set the component size if it is less than 16,
2378 -- rounding it up to the next storage unit size.
2379
2380 if Esiz <= 8 then
2381 Csiz := Uint_8;
2382 elsif Esiz <= 16 then
2383 Csiz := Uint_16;
2384 else
2385 Csiz := Uint_0;
2386 end if;
2387
2388 -- Set component size up to match alignment if it would
2389 -- otherwise be less than the alignment. This deals with
2390 -- cases of types whose alignment exceeds their size (the
2391 -- padded type cases).
2392
2393 if Csiz /= 0 then
2394 declare
2395 A : constant Uint := Alignment_In_Bits (Ctyp);
2396 begin
2397 if Csiz < A then
2398 Csiz := A;
2399 end if;
2400 end;
2401 end if;
2402 end if;
2403
2404 -- Case of component size that may result in packing
2405
2406 if 1 <= Csiz and then Csiz <= 64 then
2407 declare
2408 Ent : constant Entity_Id :=
2409 First_Subtype (Arr);
2410 Pack_Pragma : constant Node_Id :=
2411 Get_Rep_Pragma (Ent, Name_Pack);
2412 Comp_Size_C : constant Node_Id :=
2413 Get_Attribute_Definition_Clause
2414 (Ent, Attribute_Component_Size);
2415
2416 begin
2417 -- Warn if we have pack and component size so that the
2418 -- pack is ignored.
2419
2420 -- Note: here we must check for the presence of a
2421 -- component size before checking for a Pack pragma to
2422 -- deal with the case where the array type is a derived
2423 -- type whose parent is currently private.
2424
2425 if Present (Comp_Size_C)
2426 and then Has_Pragma_Pack (Ent)
2427 and then Warn_On_Redundant_Constructs
2428 then
2429 Error_Msg_Sloc := Sloc (Comp_Size_C);
2430 Error_Msg_NE
2431 ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
2432 Error_Msg_N
2433 ("\?r?explicit component size given#!", Pack_Pragma);
2434 Set_Is_Packed (Base_Type (Ent), False);
2435 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
2436 end if;
2437
2438 -- Set component size if not already set by a component
2439 -- size clause.
2440
2441 if not Present (Comp_Size_C) then
2442 Set_Component_Size (Arr, Csiz);
2443 end if;
2444
2445 -- Check for base type of 8, 16, 32 bits, where an
2446 -- unsigned subtype has a length one less than the
2447 -- base type (e.g. Natural subtype of Integer).
2448
2449 -- In such cases, if a component size was not set
2450 -- explicitly, then generate a warning.
2451
2452 if Has_Pragma_Pack (Arr)
2453 and then not Present (Comp_Size_C)
2454 and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2455 and then Esize (Base_Type (Ctyp)) = Csiz + 1
2456 then
2457 Error_Msg_Uint_1 := Csiz;
2458
2459 if Present (Pack_Pragma) then
2460 Error_Msg_N
2461 ("??pragma Pack causes component size to be ^!",
2462 Pack_Pragma);
2463 Error_Msg_N
2464 ("\??use Component_Size to set desired value!",
2465 Pack_Pragma);
2466 end if;
2467 end if;
2468
2469 -- Actual packing is not needed for 8, 16, 32, 64. Also
2470 -- not needed for 24 if alignment is 1.
2471
2472 if Csiz = 8
2473 or else Csiz = 16
2474 or else Csiz = 32
2475 or else Csiz = 64
2476 or else (Csiz = 24 and then Alignment (Ctyp) = 1)
2477 then
2478 -- Here the array was requested to be packed, but
2479 -- the packing request had no effect, so Is_Packed
2480 -- is reset.
2481
2482 -- Note: semantically this means that we lose track
2483 -- of the fact that a derived type inherited a pragma
2484 -- Pack that was non- effective, but that seems fine.
2485
2486 -- We regard a Pack pragma as a request to set a
2487 -- representation characteristic, and this request
2488 -- may be ignored.
2489
2490 Set_Is_Packed (Base_Type (Arr), False);
2491 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2492
2493 if Known_Static_Esize (Component_Type (Arr))
2494 and then Esize (Component_Type (Arr)) = Csiz
2495 then
2496 Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
2497 end if;
2498
2499 -- In all other cases, packing is indeed needed
2500
2501 else
2502 Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2503 Set_Is_Bit_Packed_Array (Base_Type (Arr), True);
2504 Set_Is_Packed (Base_Type (Arr), True);
2505 end if;
2506 end;
2507 end if;
2508 end;
2509
2510 -- Check for Aliased or Atomic_Components/Atomic/VFA with
2511 -- unsuitable packing or explicit component size clause given.
2512
2513 if (Has_Aliased_Components (Arr)
2514 or else Has_Atomic_Components (Arr)
2515 or else Is_Atomic_Or_VFA (Ctyp))
2516 and then
2517 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2518 then
2519 Alias_Atomic_Check : declare
2520
2521 procedure Complain_CS (T : String);
2522 -- Outputs error messages for incorrect CS clause or pragma
2523 -- Pack for aliased or atomic/VFA components (T is "aliased"
2524 -- or "atomic/vfa");
2525
2526 -----------------
2527 -- Complain_CS --
2528 -----------------
2529
2530 procedure Complain_CS (T : String) is
2531 begin
2532 if Has_Component_Size_Clause (Arr) then
2533 Clause :=
2534 Get_Attribute_Definition_Clause
2535 (FS, Attribute_Component_Size);
2536
2537 Error_Msg_N
2538 ("incorrect component size for "
2539 & T & " components", Clause);
2540 Error_Msg_Uint_1 := Esize (Ctyp);
2541 Error_Msg_N
2542 ("\only allowed value is^", Clause);
2543
2544 else
2545 Error_Msg_N
2546 ("cannot pack " & T & " components",
2547 Get_Rep_Pragma (FS, Name_Pack));
2548 end if;
2549 end Complain_CS;
2550
2551 -- Start of processing for Alias_Atomic_Check
2552
2553 begin
2554 -- If object size of component type isn't known, we cannot
2555 -- be sure so we defer to the back end.
2556
2557 if not Known_Static_Esize (Ctyp) then
2558 null;
2559
2560 -- Case where component size has no effect. First check for
2561 -- object size of component type multiple of the storage
2562 -- unit size.
2563
2564 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2565
2566 -- OK in both packing case and component size case if RM
2567 -- size is known and static and same as the object size.
2568
2569 and then
2570 ((Known_Static_RM_Size (Ctyp)
2571 and then Esize (Ctyp) = RM_Size (Ctyp))
2572
2573 -- Or if we have an explicit component size clause and
2574 -- the component size and object size are equal.
2575
2576 or else
2577 (Has_Component_Size_Clause (Arr)
2578 and then Component_Size (Arr) = Esize (Ctyp)))
2579 then
2580 null;
2581
2582 elsif Has_Aliased_Components (Arr) then
2583 Complain_CS ("aliased");
2584
2585 elsif Has_Atomic_Components (Arr)
2586 or else Is_Atomic (Ctyp)
2587 then
2588 Complain_CS ("atomic");
2589
2590 elsif Is_Volatile_Full_Access (Ctyp) then
2591 Complain_CS ("volatile full access");
2592 end if;
2593 end Alias_Atomic_Check;
2594 end if;
2595
2596 -- Check for Independent_Components/Independent with unsuitable
2597 -- packing or explicit component size clause given.
2598
2599 if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
2600 and then
2601 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2602 then
2603 begin
2604 -- If object size of component type isn't known, we cannot
2605 -- be sure so we defer to the back end.
2606
2607 if not Known_Static_Esize (Ctyp) then
2608 null;
2609
2610 -- Case where component size has no effect. First check for
2611 -- object size of component type multiple of the storage
2612 -- unit size.
2613
2614 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2615
2616 -- OK in both packing case and component size case if RM
2617 -- size is known and multiple of the storage unit size.
2618
2619 and then
2620 ((Known_Static_RM_Size (Ctyp)
2621 and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
2622
2623 -- Or if we have an explicit component size clause and
2624 -- the component size is larger than the object size.
2625
2626 or else
2627 (Has_Component_Size_Clause (Arr)
2628 and then Component_Size (Arr) >= Esize (Ctyp)))
2629 then
2630 null;
2631
2632 else
2633 if Has_Component_Size_Clause (Arr) then
2634 Clause :=
2635 Get_Attribute_Definition_Clause
2636 (FS, Attribute_Component_Size);
2637
2638 Error_Msg_N
2639 ("incorrect component size for "
2640 & "independent components", Clause);
2641 Error_Msg_Uint_1 := Esize (Ctyp);
2642 Error_Msg_N
2643 ("\minimum allowed is^", Clause);
2644
2645 else
2646 Error_Msg_N
2647 ("cannot pack independent components",
2648 Get_Rep_Pragma (FS, Name_Pack));
2649 end if;
2650 end if;
2651 end;
2652 end if;
2653
2654 -- Warn for case of atomic type
2655
2656 Clause := Get_Rep_Pragma (FS, Name_Atomic);
2657
2658 if Present (Clause)
2659 and then not Addressable (Component_Size (FS))
2660 then
2661 Error_Msg_NE
2662 ("non-atomic components of type& may not be "
2663 & "accessible by separate tasks??", Clause, Arr);
2664
2665 if Has_Component_Size_Clause (Arr) then
2666 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2667 (FS, Attribute_Component_Size));
2668 Error_Msg_N ("\because of component size clause#??", Clause);
2669
2670 elsif Has_Pragma_Pack (Arr) then
2671 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2672 Error_Msg_N ("\because of pragma Pack#??", Clause);
2673 end if;
2674 end if;
2675
2676 -- Check for scalar storage order
2677
2678 declare
2679 Dummy : Boolean;
2680 begin
2681 Check_Component_Storage_Order
2682 (Encl_Type => Arr,
2683 Comp => Empty,
2684 ADC => Get_Attribute_Definition_Clause
2685 (First_Subtype (Arr),
2686 Attribute_Scalar_Storage_Order),
2687 Comp_ADC_Present => Dummy);
2688 end;
2689
2690 -- Processing that is done only for subtypes
2691
2692 else
2693 -- Acquire alignment from base type
2694
2695 if Unknown_Alignment (Arr) then
2696 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
2697 Adjust_Esize_Alignment (Arr);
2698 end if;
2699 end if;
2700
2701 -- Specific checks for bit-packed arrays
2702
2703 if Is_Bit_Packed_Array (Arr) then
2704
2705 -- Check number of elements for bit packed arrays that come from
2706 -- source and have compile time known ranges. The bit-packed
2707 -- arrays circuitry does not support arrays with more than
2708 -- Integer'Last + 1 elements, and when this restriction is
2709 -- violated, causes incorrect data access.
2710
2711 -- For the case where this is not compile time known, a run-time
2712 -- check should be generated???
2713
2714 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
2715 declare
2716 Elmts : Uint;
2717 Index : Node_Id;
2718 Ilen : Node_Id;
2719 Ityp : Entity_Id;
2720
2721 begin
2722 Elmts := Uint_1;
2723 Index := First_Index (Arr);
2724 while Present (Index) loop
2725 Ityp := Etype (Index);
2726
2727 -- Never generate an error if any index is of a generic
2728 -- type. We will check this in instances.
2729
2730 if Is_Generic_Type (Ityp) then
2731 Elmts := Uint_0;
2732 exit;
2733 end if;
2734
2735 Ilen :=
2736 Make_Attribute_Reference (Loc,
2737 Prefix => New_Occurrence_Of (Ityp, Loc),
2738 Attribute_Name => Name_Range_Length);
2739 Analyze_And_Resolve (Ilen);
2740
2741 -- No attempt is made to check number of elements if not
2742 -- compile time known.
2743
2744 if Nkind (Ilen) /= N_Integer_Literal then
2745 Elmts := Uint_0;
2746 exit;
2747 end if;
2748
2749 Elmts := Elmts * Intval (Ilen);
2750 Next_Index (Index);
2751 end loop;
2752
2753 if Elmts > Intval (High_Bound
2754 (Scalar_Range (Standard_Integer))) + 1
2755 then
2756 Error_Msg_N
2757 ("bit packed array type may not have "
2758 & "more than Integer''Last+1 elements", Arr);
2759 end if;
2760 end;
2761 end if;
2762
2763 -- Check size
2764
2765 if Known_RM_Size (Arr) then
2766 declare
2767 SizC : constant Node_Id := Size_Clause (Arr);
2768 Discard : Boolean;
2769
2770 begin
2771 -- It is not clear if it is possible to have no size clause
2772 -- at this stage, but it is not worth worrying about. Post
2773 -- error on the entity name in the size clause if present,
2774 -- else on the type entity itself.
2775
2776 if Present (SizC) then
2777 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
2778 else
2779 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
2780 end if;
2781 end;
2782 end if;
2783 end if;
2784
2785 -- If any of the index types was an enumeration type with a non-
2786 -- standard rep clause, then we indicate that the array type is
2787 -- always packed (even if it is not bit packed).
2788
2789 if Non_Standard_Enum then
2790 Set_Has_Non_Standard_Rep (Base_Type (Arr));
2791 Set_Is_Packed (Base_Type (Arr));
2792 end if;
2793
2794 Set_Component_Alignment_If_Not_Set (Arr);
2795
2796 -- If the array is packed, we must create the packed array type to be
2797 -- used to actually implement the type. This is only needed for real
2798 -- array types (not for string literal types, since they are present
2799 -- only for the front end).
2800
2801 if Is_Packed (Arr)
2802 and then Ekind (Arr) /= E_String_Literal_Subtype
2803 then
2804 Create_Packed_Array_Impl_Type (Arr);
2805 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
2806
2807 -- Make sure that we have the necessary routines to implement the
2808 -- packing, and complain now if not. Note that we only test this
2809 -- for constrained array types.
2810
2811 if Is_Constrained (Arr)
2812 and then Is_Bit_Packed_Array (Arr)
2813 and then Present (Packed_Array_Impl_Type (Arr))
2814 and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
2815 then
2816 declare
2817 CS : constant Uint := Component_Size (Arr);
2818 RE : constant RE_Id := Get_Id (UI_To_Int (CS));
2819
2820 begin
2821 if RE /= RE_Null
2822 and then not RTE_Available (RE)
2823 then
2824 Error_Msg_CRT
2825 ("packing of " & UI_Image (CS) & "-bit components",
2826 First_Subtype (Etype (Arr)));
2827
2828 -- Cancel the packing
2829
2830 Set_Is_Packed (Base_Type (Arr), False);
2831 Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2832 Set_Packed_Array_Impl_Type (Arr, Empty);
2833 goto Skip_Packed;
2834 end if;
2835 end;
2836 end if;
2837
2838 -- Size information of packed array type is copied to the array
2839 -- type, since this is really the representation. But do not
2840 -- override explicit existing size values. If the ancestor subtype
2841 -- is constrained the Packed_Array_Impl_Type will be inherited
2842 -- from it, but the size may have been provided already, and
2843 -- must not be overridden either.
2844
2845 if not Has_Size_Clause (Arr)
2846 and then
2847 (No (Ancestor_Subtype (Arr))
2848 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
2849 then
2850 Set_Esize (Arr, Esize (Packed_Array_Impl_Type (Arr)));
2851 Set_RM_Size (Arr, RM_Size (Packed_Array_Impl_Type (Arr)));
2852 end if;
2853
2854 if not Has_Alignment_Clause (Arr) then
2855 Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
2856 end if;
2857 end if;
2858
2859 <<Skip_Packed>>
2860
2861 -- For non-packed arrays set the alignment of the array to the
2862 -- alignment of the component type if it is unknown. Skip this
2863 -- in atomic/VFA case (atomic/VFA arrays may need larger alignments).
2864
2865 if not Is_Packed (Arr)
2866 and then Unknown_Alignment (Arr)
2867 and then Known_Alignment (Ctyp)
2868 and then Known_Static_Component_Size (Arr)
2869 and then Known_Static_Esize (Ctyp)
2870 and then Esize (Ctyp) = Component_Size (Arr)
2871 and then not Is_Atomic_Or_VFA (Arr)
2872 then
2873 Set_Alignment (Arr, Alignment (Component_Type (Arr)));
2874 end if;
2875
2876 -- A Ghost type cannot have a component of protected or task type
2877 -- (SPARK RM 6.9(19)).
2878
2879 if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
2880 Error_Msg_N
2881 ("ghost array type & cannot have concurrent component type",
2882 Arr);
2883 end if;
2884 end Freeze_Array_Type;
2885
2886 -------------------------------
2887 -- Freeze_Object_Declaration --
2888 -------------------------------
2889
2890 procedure Freeze_Object_Declaration (E : Entity_Id) is
2891 begin
2892 -- Abstract type allowed only for C++ imported variables or constants
2893
2894 -- Note: we inhibit this check for objects that do not come from
2895 -- source because there is at least one case (the expansion of
2896 -- x'Class'Input where x is abstract) where we legitimately
2897 -- generate an abstract object.
2898
2899 if Is_Abstract_Type (Etype (E))
2900 and then Comes_From_Source (Parent (E))
2901 and then not (Is_Imported (E) and then Is_CPP_Class (Etype (E)))
2902 then
2903 Error_Msg_N ("type of object cannot be abstract",
2904 Object_Definition (Parent (E)));
2905
2906 if Is_CPP_Class (Etype (E)) then
2907 Error_Msg_NE
2908 ("\} may need a cpp_constructor",
2909 Object_Definition (Parent (E)), Etype (E));
2910
2911 elsif Present (Expression (Parent (E))) then
2912 Error_Msg_N -- CODEFIX
2913 ("\maybe a class-wide type was meant",
2914 Object_Definition (Parent (E)));
2915 end if;
2916 end if;
2917
2918 -- For object created by object declaration, perform required
2919 -- categorization (preelaborate and pure) checks. Defer these
2920 -- checks to freeze time since pragma Import inhibits default
2921 -- initialization and thus pragma Import affects these checks.
2922
2923 Validate_Object_Declaration (Declaration_Node (E));
2924
2925 -- If there is an address clause, check that it is valid
2926 -- and if need be move initialization to the freeze node.
2927
2928 Check_Address_Clause (E);
2929
2930 -- Similar processing is needed for aspects that may affect
2931 -- object layout, like Alignment, if there is an initialization
2932 -- expression.
2933
2934 if Has_Delayed_Aspects (E)
2935 and then Expander_Active
2936 and then Is_Array_Type (Etype (E))
2937 and then Present (Expression (Parent (E)))
2938 then
2939 declare
2940 Decl : constant Node_Id := Parent (E);
2941 Lhs : constant Node_Id := New_Occurrence_Of (E, Loc);
2942
2943 begin
2944
2945 -- Capture initialization value at point of declaration, and
2946 -- make explicit assignment legal, because object may be a
2947 -- constant.
2948
2949 Remove_Side_Effects (Expression (Decl));
2950 Set_Assignment_OK (Lhs);
2951
2952 -- Move initialization to freeze actions.
2953
2954 Append_Freeze_Action (E,
2955 Make_Assignment_Statement (Loc,
2956 Name => Lhs,
2957 Expression => Expression (Decl)));
2958
2959 Set_No_Initialization (Decl);
2960 -- Set_Is_Frozen (E, False);
2961 end;
2962 end if;
2963
2964 -- Reset Is_True_Constant for non-constant aliased object. We
2965 -- consider that the fact that a non-constant object is aliased may
2966 -- indicate that some funny business is going on, e.g. an aliased
2967 -- object is passed by reference to a procedure which captures the
2968 -- address of the object, which is later used to assign a new value,
2969 -- even though the compiler thinks that it is not modified. Such
2970 -- code is highly dubious, but we choose to make it "work" for
2971 -- non-constant aliased objects.
2972
2973 -- Note that we used to do this for all aliased objects, whether or
2974 -- not constant, but this caused anomalies down the line because we
2975 -- ended up with static objects that were not Is_True_Constant. Not
2976 -- resetting Is_True_Constant for (aliased) constant objects ensures
2977 -- that this anomaly never occurs.
2978
2979 -- However, we don't do that for internal entities. We figure that if
2980 -- we deliberately set Is_True_Constant for an internal entity, e.g.
2981 -- a dispatch table entry, then we mean it.
2982
2983 if Ekind (E) /= E_Constant
2984 and then (Is_Aliased (E) or else Is_Aliased (Etype (E)))
2985 and then not Is_Internal_Name (Chars (E))
2986 then
2987 Set_Is_True_Constant (E, False);
2988 end if;
2989
2990 -- If the object needs any kind of default initialization, an error
2991 -- must be issued if No_Default_Initialization applies. The check
2992 -- doesn't apply to imported objects, which are not ever default
2993 -- initialized, and is why the check is deferred until freezing, at
2994 -- which point we know if Import applies. Deferred constants are also
2995 -- exempted from this test because their completion is explicit, or
2996 -- through an import pragma.
2997
2998 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
2999 null;
3000
3001 elsif Comes_From_Source (E)
3002 and then not Is_Imported (E)
3003 and then not Has_Init_Expression (Declaration_Node (E))
3004 and then
3005 ((Has_Non_Null_Base_Init_Proc (Etype (E))
3006 and then not No_Initialization (Declaration_Node (E))
3007 and then not Initialization_Suppressed (Etype (E)))
3008 or else
3009 (Needs_Simple_Initialization (Etype (E))
3010 and then not Is_Internal (E)))
3011 then
3012 Has_Default_Initialization := True;
3013 Check_Restriction
3014 (No_Default_Initialization, Declaration_Node (E));
3015 end if;
3016
3017 -- Check that a Thread_Local_Storage variable does not have
3018 -- default initialization, and any explicit initialization must
3019 -- either be the null constant or a static constant.
3020
3021 if Has_Pragma_Thread_Local_Storage (E) then
3022 declare
3023 Decl : constant Node_Id := Declaration_Node (E);
3024 begin
3025 if Has_Default_Initialization
3026 or else
3027 (Has_Init_Expression (Decl)
3028 and then
3029 (No (Expression (Decl))
3030 or else not
3031 (Is_OK_Static_Expression (Expression (Decl))
3032 or else Nkind (Expression (Decl)) = N_Null)))
3033 then
3034 Error_Msg_NE
3035 ("Thread_Local_Storage variable& is "
3036 & "improperly initialized", Decl, E);
3037 Error_Msg_NE
3038 ("\only allowed initialization is explicit "
3039 & "NULL or static expression", Decl, E);
3040 end if;
3041 end;
3042 end if;
3043
3044 -- For imported objects, set Is_Public unless there is also an
3045 -- address clause, which means that there is no external symbol
3046 -- needed for the Import (Is_Public may still be set for other
3047 -- unrelated reasons). Note that we delayed this processing
3048 -- till freeze time so that we can be sure not to set the flag
3049 -- if there is an address clause. If there is such a clause,
3050 -- then the only purpose of the Import pragma is to suppress
3051 -- implicit initialization.
3052
3053 if Is_Imported (E) and then No (Address_Clause (E)) then
3054 Set_Is_Public (E);
3055 end if;
3056
3057 -- For source objects that are not Imported and are library
3058 -- level, if no linker section pragma was given inherit the
3059 -- appropriate linker section from the corresponding type.
3060
3061 if Comes_From_Source (E)
3062 and then not Is_Imported (E)
3063 and then Is_Library_Level_Entity (E)
3064 and then No (Linker_Section_Pragma (E))
3065 then
3066 Set_Linker_Section_Pragma
3067 (E, Linker_Section_Pragma (Etype (E)));
3068 end if;
3069
3070 -- For convention C objects of an enumeration type, warn if the
3071 -- size is not integer size and no explicit size given. Skip
3072 -- warning for Boolean, and Character, assume programmer expects
3073 -- 8-bit sizes for these cases.
3074
3075 if (Convention (E) = Convention_C
3076 or else
3077 Convention (E) = Convention_CPP)
3078 and then Is_Enumeration_Type (Etype (E))
3079 and then not Is_Character_Type (Etype (E))
3080 and then not Is_Boolean_Type (Etype (E))
3081 and then Esize (Etype (E)) < Standard_Integer_Size
3082 and then not Has_Size_Clause (E)
3083 then
3084 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
3085 Error_Msg_N
3086 ("??convention C enumeration object has size less than ^", E);
3087 Error_Msg_N ("\??use explicit size clause to set size", E);
3088 end if;
3089 end Freeze_Object_Declaration;
3090
3091 -----------------------------
3092 -- Freeze_Generic_Entities --
3093 -----------------------------
3094
3095 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
3096 E : Entity_Id;
3097 F : Node_Id;
3098 Flist : List_Id;
3099
3100 begin
3101 Flist := New_List;
3102 E := First_Entity (Pack);
3103 while Present (E) loop
3104 if Is_Type (E) and then not Is_Generic_Type (E) then
3105 F := Make_Freeze_Generic_Entity (Sloc (Pack));
3106 Set_Entity (F, E);
3107 Append_To (Flist, F);
3108
3109 elsif Ekind (E) = E_Generic_Package then
3110 Append_List_To (Flist, Freeze_Generic_Entities (E));
3111 end if;
3112
3113 Next_Entity (E);
3114 end loop;
3115
3116 return Flist;
3117 end Freeze_Generic_Entities;
3118
3119 --------------------
3120 -- Freeze_Profile --
3121 --------------------
3122
3123 function Freeze_Profile (E : Entity_Id) return Boolean is
3124 F_Type : Entity_Id;
3125 R_Type : Entity_Id;
3126 Warn_Node : Node_Id;
3127
3128 begin
3129 -- Loop through formals
3130
3131 Formal := First_Formal (E);
3132 while Present (Formal) loop
3133 F_Type := Etype (Formal);
3134
3135 -- AI05-0151: incomplete types can appear in a profile. By the
3136 -- time the entity is frozen, the full view must be available,
3137 -- unless it is a limited view.
3138
3139 if Is_Incomplete_Type (F_Type)
3140 and then Present (Full_View (F_Type))
3141 and then not From_Limited_With (F_Type)
3142 then
3143 F_Type := Full_View (F_Type);
3144 Set_Etype (Formal, F_Type);
3145 end if;
3146
3147 if not From_Limited_With (F_Type) then
3148 Freeze_And_Append (F_Type, N, Result);
3149 end if;
3150
3151 if Is_Private_Type (F_Type)
3152 and then Is_Private_Type (Base_Type (F_Type))
3153 and then No (Full_View (Base_Type (F_Type)))
3154 and then not Is_Generic_Type (F_Type)
3155 and then not Is_Derived_Type (F_Type)
3156 then
3157 -- If the type of a formal is incomplete, subprogram is being
3158 -- frozen prematurely. Within an instance (but not within a
3159 -- wrapper package) this is an artifact of our need to regard
3160 -- the end of an instantiation as a freeze point. Otherwise it
3161 -- is a definite error.
3162
3163 if In_Instance then
3164 Set_Is_Frozen (E, False);
3165 Result := No_List;
3166 return False;
3167
3168 elsif not After_Last_Declaration
3169 and then not Freezing_Library_Level_Tagged_Type
3170 then
3171 Error_Msg_Node_1 := F_Type;
3172 Error_Msg
3173 ("type & must be fully defined before this point", Loc);
3174 end if;
3175 end if;
3176
3177 -- Check suspicious parameter for C function. These tests apply
3178 -- only to exported/imported subprograms.
3179
3180 if Warn_On_Export_Import
3181 and then Comes_From_Source (E)
3182 and then (Convention (E) = Convention_C
3183 or else
3184 Convention (E) = Convention_CPP)
3185 and then (Is_Imported (E) or else Is_Exported (E))
3186 and then Convention (E) /= Convention (Formal)
3187 and then not Has_Warnings_Off (E)
3188 and then not Has_Warnings_Off (F_Type)
3189 and then not Has_Warnings_Off (Formal)
3190 then
3191 -- Qualify mention of formals with subprogram name
3192
3193 Error_Msg_Qual_Level := 1;
3194
3195 -- Check suspicious use of fat C pointer
3196
3197 if Is_Access_Type (F_Type)
3198 and then Esize (F_Type) > Ttypes.System_Address_Size
3199 then
3200 Error_Msg_N
3201 ("?x?type of & does not correspond to C pointer!", Formal);
3202
3203 -- Check suspicious return of boolean
3204
3205 elsif Root_Type (F_Type) = Standard_Boolean
3206 and then Convention (F_Type) = Convention_Ada
3207 and then not Has_Warnings_Off (F_Type)
3208 and then not Has_Size_Clause (F_Type)
3209 then
3210 Error_Msg_N
3211 ("& is an 8-bit Ada Boolean?x?", Formal);
3212 Error_Msg_N
3213 ("\use appropriate corresponding type in C "
3214 & "(e.g. char)?x?", Formal);
3215
3216 -- Check suspicious tagged type
3217
3218 elsif (Is_Tagged_Type (F_Type)
3219 or else
3220 (Is_Access_Type (F_Type)
3221 and then Is_Tagged_Type (Designated_Type (F_Type))))
3222 and then Convention (E) = Convention_C
3223 then
3224 Error_Msg_N
3225 ("?x?& involves a tagged type which does not "
3226 & "correspond to any C type!", Formal);
3227
3228 -- Check wrong convention subprogram pointer
3229
3230 elsif Ekind (F_Type) = E_Access_Subprogram_Type
3231 and then not Has_Foreign_Convention (F_Type)
3232 then
3233 Error_Msg_N
3234 ("?x?subprogram pointer & should "
3235 & "have foreign convention!", Formal);
3236 Error_Msg_Sloc := Sloc (F_Type);
3237 Error_Msg_NE
3238 ("\?x?add Convention pragma to declaration of &#",
3239 Formal, F_Type);
3240 end if;
3241
3242 -- Turn off name qualification after message output
3243
3244 Error_Msg_Qual_Level := 0;
3245 end if;
3246
3247 -- Check for unconstrained array in exported foreign convention
3248 -- case.
3249
3250 if Has_Foreign_Convention (E)
3251 and then not Is_Imported (E)
3252 and then Is_Array_Type (F_Type)
3253 and then not Is_Constrained (F_Type)
3254 and then Warn_On_Export_Import
3255 then
3256 Error_Msg_Qual_Level := 1;
3257
3258 -- If this is an inherited operation, place the warning on
3259 -- the derived type declaration, rather than on the original
3260 -- subprogram.
3261
3262 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
3263 then
3264 Warn_Node := Parent (E);
3265
3266 if Formal = First_Formal (E) then
3267 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
3268 end if;
3269 else
3270 Warn_Node := Formal;
3271 end if;
3272
3273 Error_Msg_NE ("?x?type of argument& is unconstrained array",
3274 Warn_Node, Formal);
3275 Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
3276 Warn_Node, Formal);
3277 Error_Msg_Qual_Level := 0;
3278 end if;
3279
3280 if not From_Limited_With (F_Type) then
3281 if Is_Access_Type (F_Type) then
3282 F_Type := Designated_Type (F_Type);
3283 end if;
3284
3285 -- If the formal is an anonymous_access_to_subprogram
3286 -- freeze the subprogram type as well, to prevent
3287 -- scope anomalies in gigi, because there is no other
3288 -- clear point at which it could be frozen.
3289
3290 if Is_Itype (Etype (Formal))
3291 and then Ekind (F_Type) = E_Subprogram_Type
3292 then
3293 Freeze_And_Append (F_Type, N, Result);
3294 end if;
3295 end if;
3296
3297 Next_Formal (Formal);
3298 end loop;
3299
3300 -- Case of function: similar checks on return type
3301
3302 if Ekind (E) = E_Function then
3303
3304 -- Check whether function is declared elsewhere. Previous code
3305 -- used Get_Source_Unit on both arguments, but the values are
3306 -- equal in the case of a parent and a child unit.
3307 -- Confusion with subunits in code ????
3308
3309 Late_Freezing :=
3310 not In_Same_Extended_Unit (E, N)
3311 and then Returns_Limited_View (E);
3312
3313 -- Freeze return type
3314
3315 R_Type := Etype (E);
3316
3317 -- AI05-0151: the return type may have been incomplete
3318 -- at the point of declaration. Replace it with the full
3319 -- view, unless the current type is a limited view. In
3320 -- that case the full view is in a different unit, and
3321 -- gigi finds the non-limited view after the other unit
3322 -- is elaborated.
3323
3324 if Ekind (R_Type) = E_Incomplete_Type
3325 and then Present (Full_View (R_Type))
3326 and then not From_Limited_With (R_Type)
3327 then
3328 R_Type := Full_View (R_Type);
3329 Set_Etype (E, R_Type);
3330
3331 -- If the return type is a limited view and the non-limited
3332 -- view is still incomplete, the function has to be frozen at a
3333 -- later time. If the function is abstract there is no place at
3334 -- which the full view will become available, and no code to be
3335 -- generated for it, so mark type as frozen.
3336
3337 elsif Ekind (R_Type) = E_Incomplete_Type
3338 and then From_Limited_With (R_Type)
3339 and then Ekind (Non_Limited_View (R_Type)) = E_Incomplete_Type
3340 then
3341 if Is_Abstract_Subprogram (E) then
3342 null;
3343 else
3344 Set_Is_Frozen (E, False);
3345 Set_Returns_Limited_View (E);
3346 return False;
3347 end if;
3348 end if;
3349
3350 Freeze_And_Append (R_Type, N, Result);
3351
3352 -- Check suspicious return type for C function
3353
3354 if Warn_On_Export_Import
3355 and then (Convention (E) = Convention_C
3356 or else
3357 Convention (E) = Convention_CPP)
3358 and then (Is_Imported (E) or else Is_Exported (E))
3359 then
3360 -- Check suspicious return of fat C pointer
3361
3362 if Is_Access_Type (R_Type)
3363 and then Esize (R_Type) > Ttypes.System_Address_Size
3364 and then not Has_Warnings_Off (E)
3365 and then not Has_Warnings_Off (R_Type)
3366 then
3367 Error_Msg_N ("?x?return type of& does not "
3368 & "correspond to C pointer!", E);
3369
3370 -- Check suspicious return of boolean
3371
3372 elsif Root_Type (R_Type) = Standard_Boolean
3373 and then Convention (R_Type) = Convention_Ada
3374 and then not Has_Warnings_Off (E)
3375 and then not Has_Warnings_Off (R_Type)
3376 and then not Has_Size_Clause (R_Type)
3377 then
3378 declare
3379 N : constant Node_Id :=
3380 Result_Definition (Declaration_Node (E));
3381 begin
3382 Error_Msg_NE
3383 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
3384 Error_Msg_NE
3385 ("\use appropriate corresponding type in C "
3386 & "(e.g. char)?x?", N, E);
3387 end;
3388
3389 -- Check suspicious return tagged type
3390
3391 elsif (Is_Tagged_Type (R_Type)
3392 or else (Is_Access_Type (R_Type)
3393 and then
3394 Is_Tagged_Type
3395 (Designated_Type (R_Type))))
3396 and then Convention (E) = Convention_C
3397 and then not Has_Warnings_Off (E)
3398 and then not Has_Warnings_Off (R_Type)
3399 then
3400 Error_Msg_N ("?x?return type of & does not "
3401 & "correspond to C type!", E);
3402
3403 -- Check return of wrong convention subprogram pointer
3404
3405 elsif Ekind (R_Type) = E_Access_Subprogram_Type
3406 and then not Has_Foreign_Convention (R_Type)
3407 and then not Has_Warnings_Off (E)
3408 and then not Has_Warnings_Off (R_Type)
3409 then
3410 Error_Msg_N ("?x?& should return a foreign "
3411 & "convention subprogram pointer", E);
3412 Error_Msg_Sloc := Sloc (R_Type);
3413 Error_Msg_NE
3414 ("\?x?add Convention pragma to declaration of& #",
3415 E, R_Type);
3416 end if;
3417 end if;
3418
3419 -- Give warning for suspicious return of a result of an
3420 -- unconstrained array type in a foreign convention function.
3421
3422 if Has_Foreign_Convention (E)
3423
3424 -- We are looking for a return of unconstrained array
3425
3426 and then Is_Array_Type (R_Type)
3427 and then not Is_Constrained (R_Type)
3428
3429 -- Exclude imported routines, the warning does not belong on
3430 -- the import, but rather on the routine definition.
3431
3432 and then not Is_Imported (E)
3433
3434 -- Check that general warning is enabled, and that it is not
3435 -- suppressed for this particular case.
3436
3437 and then Warn_On_Export_Import
3438 and then not Has_Warnings_Off (E)
3439 and then not Has_Warnings_Off (R_Type)
3440 then
3441 Error_Msg_N
3442 ("?x?foreign convention function& should not return "
3443 & "unconstrained array!", E);
3444 end if;
3445 end if;
3446
3447 -- Check suspicious use of Import in pure unit (cases where the RM
3448 -- allows calls to be omitted).
3449
3450 if Is_Imported (E)
3451
3452 -- It might be suspicious if the compilation unit has the Pure
3453 -- aspect/pragma.
3454
3455 and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
3456
3457 -- The RM allows omission of calls only in the case of
3458 -- library-level subprograms (see RM-10.2.1(18)).
3459
3460 and then Is_Library_Level_Entity (E)
3461
3462 -- Ignore internally generated entity. This happens in some cases
3463 -- of subprograms in specs, where we generate an implied body.
3464
3465 and then Comes_From_Source (Import_Pragma (E))
3466
3467 -- Assume run-time knows what it is doing
3468
3469 and then not GNAT_Mode
3470
3471 -- Assume explicit Pure_Function means import is pure
3472
3473 and then not Has_Pragma_Pure_Function (E)
3474
3475 -- Don't need warning in relaxed semantics mode
3476
3477 and then not Relaxed_RM_Semantics
3478
3479 -- Assume convention Intrinsic is OK, since this is specialized.
3480 -- This deals with the DEC unit current_exception.ads
3481
3482 and then Convention (E) /= Convention_Intrinsic
3483
3484 -- Assume that ASM interface knows what it is doing. This deals
3485 -- with e.g. unsigned.ads in the AAMP back end.
3486
3487 and then Convention (E) /= Convention_Assembler
3488 then
3489 Error_Msg_N
3490 ("pragma Import in Pure unit??", Import_Pragma (E));
3491 Error_Msg_NE
3492 ("\calls to & may be omitted (RM 10.2.1(18/3))??",
3493 Import_Pragma (E), E);
3494 end if;
3495
3496 return True;
3497 end Freeze_Profile;
3498
3499 ------------------------
3500 -- Freeze_Record_Type --
3501 ------------------------
3502
3503 procedure Freeze_Record_Type (Rec : Entity_Id) is
3504 ADC : Node_Id;
3505 Comp : Entity_Id;
3506 IR : Node_Id;
3507 Prev : Entity_Id;
3508
3509 Junk : Boolean;
3510 pragma Warnings (Off, Junk);
3511
3512 Rec_Pushed : Boolean := False;
3513 -- Set True if the record type scope Rec has been pushed on the scope
3514 -- stack. Needed for the analysis of delayed aspects specified to the
3515 -- components of Rec.
3516
3517 SSO_ADC : Node_Id;
3518 -- Scalar_Storage_Order attribute definition clause for the record
3519
3520 Unplaced_Component : Boolean := False;
3521 -- Set True if we find at least one component with no component
3522 -- clause (used to warn about useless Pack pragmas).
3523
3524 Placed_Component : Boolean := False;
3525 -- Set True if we find at least one component with a component
3526 -- clause (used to warn about useless Bit_Order pragmas, and also
3527 -- to detect cases where Implicit_Packing may have an effect).
3528
3529 Aliased_Component : Boolean := False;
3530 -- Set True if we find at least one component which is aliased. This
3531 -- is used to prevent Implicit_Packing of the record, since packing
3532 -- cannot modify the size of alignment of an aliased component.
3533
3534 SSO_ADC_Component : Boolean := False;
3535 -- Set True if we find at least one component whose type has a
3536 -- Scalar_Storage_Order attribute definition clause.
3537
3538 All_Scalar_Components : Boolean := True;
3539 -- Set False if we encounter a component of a non-scalar type
3540
3541 Scalar_Component_Total_RM_Size : Uint := Uint_0;
3542 Scalar_Component_Total_Esize : Uint := Uint_0;
3543 -- Accumulates total RM_Size values and total Esize values of all
3544 -- scalar components. Used for processing of Implicit_Packing.
3545
3546 function Check_Allocator (N : Node_Id) return Node_Id;
3547 -- If N is an allocator, possibly wrapped in one or more level of
3548 -- qualified expression(s), return the inner allocator node, else
3549 -- return Empty.
3550
3551 procedure Check_Itype (Typ : Entity_Id);
3552 -- If the component subtype is an access to a constrained subtype of
3553 -- an already frozen type, make the subtype frozen as well. It might
3554 -- otherwise be frozen in the wrong scope, and a freeze node on
3555 -- subtype has no effect. Similarly, if the component subtype is a
3556 -- regular (not protected) access to subprogram, set the anonymous
3557 -- subprogram type to frozen as well, to prevent an out-of-scope
3558 -- freeze node at some eventual point of call. Protected operations
3559 -- are handled elsewhere.
3560
3561 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3562 -- Make sure that all types mentioned in Discrete_Choices of the
3563 -- variants referenceed by the Variant_Part VP are frozen. This is
3564 -- a recursive routine to deal with nested variants.
3565
3566 ---------------------
3567 -- Check_Allocator --
3568 ---------------------
3569
3570 function Check_Allocator (N : Node_Id) return Node_Id is
3571 Inner : Node_Id;
3572 begin
3573 Inner := N;
3574 loop
3575 if Nkind (Inner) = N_Allocator then
3576 return Inner;
3577 elsif Nkind (Inner) = N_Qualified_Expression then
3578 Inner := Expression (Inner);
3579 else
3580 return Empty;
3581 end if;
3582 end loop;
3583 end Check_Allocator;
3584
3585 -----------------
3586 -- Check_Itype --
3587 -----------------
3588
3589 procedure Check_Itype (Typ : Entity_Id) is
3590 Desig : constant Entity_Id := Designated_Type (Typ);
3591
3592 begin
3593 if not Is_Frozen (Desig)
3594 and then Is_Frozen (Base_Type (Desig))
3595 then
3596 Set_Is_Frozen (Desig);
3597
3598 -- In addition, add an Itype_Reference to ensure that the
3599 -- access subtype is elaborated early enough. This cannot be
3600 -- done if the subtype may depend on discriminants.
3601
3602 if Ekind (Comp) = E_Component
3603 and then Is_Itype (Etype (Comp))
3604 and then not Has_Discriminants (Rec)
3605 then
3606 IR := Make_Itype_Reference (Sloc (Comp));
3607 Set_Itype (IR, Desig);
3608 Add_To_Result (IR);
3609 end if;
3610
3611 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
3612 and then Convention (Desig) /= Convention_Protected
3613 then
3614 Set_Is_Frozen (Desig);
3615 end if;
3616 end Check_Itype;
3617
3618 ------------------------------------
3619 -- Freeze_Choices_In_Variant_Part --
3620 ------------------------------------
3621
3622 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
3623 pragma Assert (Nkind (VP) = N_Variant_Part);
3624
3625 Variant : Node_Id;
3626 Choice : Node_Id;
3627 CL : Node_Id;
3628
3629 begin
3630 -- Loop through variants
3631
3632 Variant := First_Non_Pragma (Variants (VP));
3633 while Present (Variant) loop
3634
3635 -- Loop through choices, checking that all types are frozen
3636
3637 Choice := First_Non_Pragma (Discrete_Choices (Variant));
3638 while Present (Choice) loop
3639 if Nkind (Choice) in N_Has_Etype
3640 and then Present (Etype (Choice))
3641 then
3642 Freeze_And_Append (Etype (Choice), N, Result);
3643 end if;
3644
3645 Next_Non_Pragma (Choice);
3646 end loop;
3647
3648 -- Check for nested variant part to process
3649
3650 CL := Component_List (Variant);
3651
3652 if not Null_Present (CL) then
3653 if Present (Variant_Part (CL)) then
3654 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
3655 end if;
3656 end if;
3657
3658 Next_Non_Pragma (Variant);
3659 end loop;
3660 end Freeze_Choices_In_Variant_Part;
3661
3662 -- Start of processing for Freeze_Record_Type
3663
3664 begin
3665 -- Deal with delayed aspect specifications for components. The
3666 -- analysis of the aspect is required to be delayed to the freeze
3667 -- point, thus we analyze the pragma or attribute definition
3668 -- clause in the tree at this point. We also analyze the aspect
3669 -- specification node at the freeze point when the aspect doesn't
3670 -- correspond to pragma/attribute definition clause.
3671
3672 Comp := First_Entity (Rec);
3673 while Present (Comp) loop
3674 if Ekind (Comp) = E_Component
3675 and then Has_Delayed_Aspects (Comp)
3676 then
3677 if not Rec_Pushed then
3678 Push_Scope (Rec);
3679 Rec_Pushed := True;
3680
3681 -- The visibility to the discriminants must be restored in
3682 -- order to properly analyze the aspects.
3683
3684 if Has_Discriminants (Rec) then
3685 Install_Discriminants (Rec);
3686 end if;
3687 end if;
3688
3689 Analyze_Aspects_At_Freeze_Point (Comp);
3690 end if;
3691
3692 Next_Entity (Comp);
3693 end loop;
3694
3695 -- Pop the scope if Rec scope has been pushed on the scope stack
3696 -- during the delayed aspect analysis process.
3697
3698 if Rec_Pushed then
3699 if Has_Discriminants (Rec) then
3700 Uninstall_Discriminants (Rec);
3701 end if;
3702
3703 Pop_Scope;
3704 end if;
3705
3706 -- Freeze components and embedded subtypes
3707
3708 Comp := First_Entity (Rec);
3709 Prev := Empty;
3710 while Present (Comp) loop
3711 if Is_Aliased (Comp) then
3712 Aliased_Component := True;
3713 end if;
3714
3715 -- Handle the component and discriminant case
3716
3717 if Ekind_In (Comp, E_Component, E_Discriminant) then
3718 declare
3719 CC : constant Node_Id := Component_Clause (Comp);
3720
3721 begin
3722 -- Freezing a record type freezes the type of each of its
3723 -- components. However, if the type of the component is
3724 -- part of this record, we do not want or need a separate
3725 -- Freeze_Node. Note that Is_Itype is wrong because that's
3726 -- also set in private type cases. We also can't check for
3727 -- the Scope being exactly Rec because of private types and
3728 -- record extensions.
3729
3730 if Is_Itype (Etype (Comp))
3731 and then Is_Record_Type (Underlying_Type
3732 (Scope (Etype (Comp))))
3733 then
3734 Undelay_Type (Etype (Comp));
3735 end if;
3736
3737 Freeze_And_Append (Etype (Comp), N, Result);
3738
3739 -- Warn for pragma Pack overriding foreign convention
3740
3741 if Has_Foreign_Convention (Etype (Comp))
3742 and then Has_Pragma_Pack (Rec)
3743
3744 -- Don't warn for aliased components, since override
3745 -- cannot happen in that case.
3746
3747 and then not Is_Aliased (Comp)
3748 then
3749 declare
3750 CN : constant Name_Id :=
3751 Get_Convention_Name (Convention (Etype (Comp)));
3752 PP : constant Node_Id :=
3753 Get_Pragma (Rec, Pragma_Pack);
3754 begin
3755 if Present (PP) then
3756 Error_Msg_Name_1 := CN;
3757 Error_Msg_Sloc := Sloc (Comp);
3758 Error_Msg_N
3759 ("pragma Pack affects convention % component#??",
3760 PP);
3761 Error_Msg_Name_1 := CN;
3762 Error_Msg_NE
3763 ("\component & may not have % compatible "
3764 & "representation??", PP, Comp);
3765 end if;
3766 end;
3767 end if;
3768
3769 -- Check for error of component clause given for variable
3770 -- sized type. We have to delay this test till this point,
3771 -- since the component type has to be frozen for us to know
3772 -- if it is variable length.
3773
3774 if Present (CC) then
3775 Placed_Component := True;
3776
3777 -- We omit this test in a generic context, it will be
3778 -- applied at instantiation time.
3779
3780 if Inside_A_Generic then
3781 null;
3782
3783 -- Also omit this test in CodePeer mode, since we do not
3784 -- have sufficient info on size and rep clauses.
3785
3786 elsif CodePeer_Mode then
3787 null;
3788
3789 -- Omit check if component has a generic type. This can
3790 -- happen in an instantiation within a generic in ASIS
3791 -- mode, where we force freeze actions without full
3792 -- expansion.
3793
3794 elsif Is_Generic_Type (Etype (Comp)) then
3795 null;
3796
3797 -- Do the check
3798
3799 elsif not
3800 Size_Known_At_Compile_Time
3801 (Underlying_Type (Etype (Comp)))
3802 then
3803 Error_Msg_N
3804 ("component clause not allowed for variable " &
3805 "length component", CC);
3806 end if;
3807
3808 else
3809 Unplaced_Component := True;
3810 end if;
3811
3812 -- Case of component requires byte alignment
3813
3814 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
3815
3816 -- Set the enclosing record to also require byte align
3817
3818 Set_Must_Be_On_Byte_Boundary (Rec);
3819
3820 -- Check for component clause that is inconsistent with
3821 -- the required byte boundary alignment.
3822
3823 if Present (CC)
3824 and then Normalized_First_Bit (Comp) mod
3825 System_Storage_Unit /= 0
3826 then
3827 Error_Msg_N
3828 ("component & must be byte aligned",
3829 Component_Name (Component_Clause (Comp)));
3830 end if;
3831 end if;
3832 end;
3833 end if;
3834
3835 -- Gather data for possible Implicit_Packing later. Note that at
3836 -- this stage we might be dealing with a real component, or with
3837 -- an implicit subtype declaration.
3838
3839 if not Is_Scalar_Type (Etype (Comp)) then
3840 All_Scalar_Components := False;
3841 else
3842 Scalar_Component_Total_RM_Size :=
3843 Scalar_Component_Total_RM_Size + RM_Size (Etype (Comp));
3844 Scalar_Component_Total_Esize :=
3845 Scalar_Component_Total_Esize + Esize (Etype (Comp));
3846 end if;
3847
3848 -- If the component is an Itype with Delayed_Freeze and is either
3849 -- a record or array subtype and its base type has not yet been
3850 -- frozen, we must remove this from the entity list of this record
3851 -- and put it on the entity list of the scope of its base type.
3852 -- Note that we know that this is not the type of a component
3853 -- since we cleared Has_Delayed_Freeze for it in the previous
3854 -- loop. Thus this must be the Designated_Type of an access type,
3855 -- which is the type of a component.
3856
3857 if Is_Itype (Comp)
3858 and then Is_Type (Scope (Comp))
3859 and then Is_Composite_Type (Comp)
3860 and then Base_Type (Comp) /= Comp
3861 and then Has_Delayed_Freeze (Comp)
3862 and then not Is_Frozen (Base_Type (Comp))
3863 then
3864 declare
3865 Will_Be_Frozen : Boolean := False;
3866 S : Entity_Id;
3867
3868 begin
3869 -- We have a difficult case to handle here. Suppose Rec is
3870 -- subtype being defined in a subprogram that's created as
3871 -- part of the freezing of Rec'Base. In that case, we know
3872 -- that Comp'Base must have already been frozen by the time
3873 -- we get to elaborate this because Gigi doesn't elaborate
3874 -- any bodies until it has elaborated all of the declarative
3875 -- part. But Is_Frozen will not be set at this point because
3876 -- we are processing code in lexical order.
3877
3878 -- We detect this case by going up the Scope chain of Rec
3879 -- and seeing if we have a subprogram scope before reaching
3880 -- the top of the scope chain or that of Comp'Base. If we
3881 -- do, then mark that Comp'Base will actually be frozen. If
3882 -- so, we merely undelay it.
3883
3884 S := Scope (Rec);
3885 while Present (S) loop
3886 if Is_Subprogram (S) then
3887 Will_Be_Frozen := True;
3888 exit;
3889 elsif S = Scope (Base_Type (Comp)) then
3890 exit;
3891 end if;
3892
3893 S := Scope (S);
3894 end loop;
3895
3896 if Will_Be_Frozen then
3897 Undelay_Type (Comp);
3898
3899 else
3900 if Present (Prev) then
3901 Set_Next_Entity (Prev, Next_Entity (Comp));
3902 else
3903 Set_First_Entity (Rec, Next_Entity (Comp));
3904 end if;
3905
3906 -- Insert in entity list of scope of base type (which
3907 -- must be an enclosing scope, because still unfrozen).
3908
3909 Append_Entity (Comp, Scope (Base_Type (Comp)));
3910 end if;
3911 end;
3912
3913 -- If the component is an access type with an allocator as default
3914 -- value, the designated type will be frozen by the corresponding
3915 -- expression in init_proc. In order to place the freeze node for
3916 -- the designated type before that for the current record type,
3917 -- freeze it now.
3918
3919 -- Same process if the component is an array of access types,
3920 -- initialized with an aggregate. If the designated type is
3921 -- private, it cannot contain allocators, and it is premature
3922 -- to freeze the type, so we check for this as well.
3923
3924 elsif Is_Access_Type (Etype (Comp))
3925 and then Present (Parent (Comp))
3926 and then Present (Expression (Parent (Comp)))
3927 then
3928 declare
3929 Alloc : constant Node_Id :=
3930 Check_Allocator (Expression (Parent (Comp)));
3931
3932 begin
3933 if Present (Alloc) then
3934
3935 -- If component is pointer to a class-wide type, freeze
3936 -- the specific type in the expression being allocated.
3937 -- The expression may be a subtype indication, in which
3938 -- case freeze the subtype mark.
3939
3940 if Is_Class_Wide_Type
3941 (Designated_Type (Etype (Comp)))
3942 then
3943 if Is_Entity_Name (Expression (Alloc)) then
3944 Freeze_And_Append
3945 (Entity (Expression (Alloc)), N, Result);
3946
3947 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
3948 then
3949 Freeze_And_Append
3950 (Entity (Subtype_Mark (Expression (Alloc))),
3951 N, Result);
3952 end if;
3953
3954 elsif Is_Itype (Designated_Type (Etype (Comp))) then
3955 Check_Itype (Etype (Comp));
3956
3957 else
3958 Freeze_And_Append
3959 (Designated_Type (Etype (Comp)), N, Result);
3960 end if;
3961 end if;
3962 end;
3963
3964 elsif Is_Access_Type (Etype (Comp))
3965 and then Is_Itype (Designated_Type (Etype (Comp)))
3966 then
3967 Check_Itype (Etype (Comp));
3968
3969 -- Freeze the designated type when initializing a component with
3970 -- an aggregate in case the aggregate contains allocators.
3971
3972 -- type T is ...;
3973 -- type T_Ptr is access all T;
3974 -- type T_Array is array ... of T_Ptr;
3975
3976 -- type Rec is record
3977 -- Comp : T_Array := (others => ...);
3978 -- end record;
3979
3980 elsif Is_Array_Type (Etype (Comp))
3981 and then Is_Access_Type (Component_Type (Etype (Comp)))
3982 then
3983 declare
3984 Comp_Par : constant Node_Id := Parent (Comp);
3985 Desig_Typ : constant Entity_Id :=
3986 Designated_Type
3987 (Component_Type (Etype (Comp)));
3988
3989 begin
3990 -- The only case when this sort of freezing is not done is
3991 -- when the designated type is class-wide and the root type
3992 -- is the record owning the component. This scenario results
3993 -- in a circularity because the class-wide type requires
3994 -- primitives that have not been created yet as the root
3995 -- type is in the process of being frozen.
3996
3997 -- type Rec is tagged;
3998 -- type Rec_Ptr is access all Rec'Class;
3999 -- type Rec_Array is array ... of Rec_Ptr;
4000
4001 -- type Rec is record
4002 -- Comp : Rec_Array := (others => ...);
4003 -- end record;
4004
4005 if Is_Class_Wide_Type (Desig_Typ)
4006 and then Root_Type (Desig_Typ) = Rec
4007 then
4008 null;
4009
4010 elsif Is_Fully_Defined (Desig_Typ)
4011 and then Present (Comp_Par)
4012 and then Nkind (Comp_Par) = N_Component_Declaration
4013 and then Present (Expression (Comp_Par))
4014 and then Nkind (Expression (Comp_Par)) = N_Aggregate
4015 then
4016 Freeze_And_Append (Desig_Typ, N, Result);
4017 end if;
4018 end;
4019 end if;
4020
4021 Prev := Comp;
4022 Next_Entity (Comp);
4023 end loop;
4024
4025 SSO_ADC :=
4026 Get_Attribute_Definition_Clause
4027 (Rec, Attribute_Scalar_Storage_Order);
4028
4029 -- If the record type has Complex_Representation, then it is treated
4030 -- as a scalar in the back end so the storage order is irrelevant.
4031
4032 if Has_Complex_Representation (Rec) then
4033 if Present (SSO_ADC) then
4034 Error_Msg_N
4035 ("??storage order has no effect with Complex_Representation",
4036 SSO_ADC);
4037 end if;
4038
4039 else
4040 -- Deal with default setting of reverse storage order
4041
4042 Set_SSO_From_Default (Rec);
4043
4044 -- Check consistent attribute setting on component types
4045
4046 declare
4047 Comp_ADC_Present : Boolean;
4048 begin
4049 Comp := First_Component (Rec);
4050 while Present (Comp) loop
4051 Check_Component_Storage_Order
4052 (Encl_Type => Rec,
4053 Comp => Comp,
4054 ADC => SSO_ADC,
4055 Comp_ADC_Present => Comp_ADC_Present);
4056 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
4057 Next_Component (Comp);
4058 end loop;
4059 end;
4060
4061 -- Now deal with reverse storage order/bit order issues
4062
4063 if Present (SSO_ADC) then
4064
4065 -- Check compatibility of Scalar_Storage_Order with Bit_Order,
4066 -- if the former is specified.
4067
4068 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
4069
4070 -- Note: report error on Rec, not on SSO_ADC, as ADC may
4071 -- apply to some ancestor type.
4072
4073 Error_Msg_Sloc := Sloc (SSO_ADC);
4074 Error_Msg_N
4075 ("scalar storage order for& specified# inconsistent with "
4076 & "bit order", Rec);
4077 end if;
4078
4079 -- Warn if there is a Scalar_Storage_Order attribute definition
4080 -- clause but no component clause, no component that itself has
4081 -- such an attribute definition, and no pragma Pack.
4082
4083 if not (Placed_Component
4084 or else
4085 SSO_ADC_Component
4086 or else
4087 Is_Packed (Rec))
4088 then
4089 Error_Msg_N
4090 ("??scalar storage order specified but no component "
4091 & "clause", SSO_ADC);
4092 end if;
4093 end if;
4094 end if;
4095
4096 -- Deal with Bit_Order aspect
4097
4098 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
4099
4100 if Present (ADC) and then Base_Type (Rec) = Rec then
4101 if not (Placed_Component
4102 or else Present (SSO_ADC)
4103 or else Is_Packed (Rec))
4104 then
4105 -- Warn if clause has no effect when no component clause is
4106 -- present, but suppress warning if the Bit_Order is required
4107 -- due to the presence of a Scalar_Storage_Order attribute.
4108
4109 Error_Msg_N
4110 ("??bit order specification has no effect", ADC);
4111 Error_Msg_N
4112 ("\??since no component clauses were specified", ADC);
4113
4114 -- Here is where we do the processing to adjust component clauses
4115 -- for reversed bit order, when not using reverse SSO.
4116
4117 elsif Reverse_Bit_Order (Rec)
4118 and then not Reverse_Storage_Order (Rec)
4119 then
4120 Adjust_Record_For_Reverse_Bit_Order (Rec);
4121
4122 -- Case where we have both an explicit Bit_Order and the same
4123 -- Scalar_Storage_Order: leave record untouched, the back-end
4124 -- will take care of required layout conversions.
4125
4126 else
4127 null;
4128
4129 end if;
4130 end if;
4131
4132 -- Complete error checking on record representation clause (e.g.
4133 -- overlap of components). This is called after adjusting the
4134 -- record for reverse bit order.
4135
4136 declare
4137 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
4138 begin
4139 if Present (RRC) then
4140 Check_Record_Representation_Clause (RRC);
4141 end if;
4142 end;
4143
4144 -- Set OK_To_Reorder_Components depending on debug flags
4145
4146 if Is_Base_Type (Rec) and then Convention (Rec) = Convention_Ada then
4147 if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
4148 or else
4149 (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
4150 then
4151 Set_OK_To_Reorder_Components (Rec);
4152 end if;
4153 end if;
4154
4155 -- Check for useless pragma Pack when all components placed. We only
4156 -- do this check for record types, not subtypes, since a subtype may
4157 -- have all its components placed, and it still makes perfectly good
4158 -- sense to pack other subtypes or the parent type. We do not give
4159 -- this warning if Optimize_Alignment is set to Space, since the
4160 -- pragma Pack does have an effect in this case (it always resets
4161 -- the alignment to one).
4162
4163 if Ekind (Rec) = E_Record_Type
4164 and then Is_Packed (Rec)
4165 and then not Unplaced_Component
4166 and then Optimize_Alignment /= 'S'
4167 then
4168 -- Reset packed status. Probably not necessary, but we do it so
4169 -- that there is no chance of the back end doing something strange
4170 -- with this redundant indication of packing.
4171
4172 Set_Is_Packed (Rec, False);
4173
4174 -- Give warning if redundant constructs warnings on
4175
4176 if Warn_On_Redundant_Constructs then
4177 Error_Msg_N -- CODEFIX
4178 ("??pragma Pack has no effect, no unplaced components",
4179 Get_Rep_Pragma (Rec, Name_Pack));
4180 end if;
4181 end if;
4182
4183 -- If this is the record corresponding to a remote type, freeze the
4184 -- remote type here since that is what we are semantically freezing.
4185 -- This prevents the freeze node for that type in an inner scope.
4186
4187 if Ekind (Rec) = E_Record_Type then
4188 if Present (Corresponding_Remote_Type (Rec)) then
4189 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
4190 end if;
4191
4192 -- Check for controlled components and unchecked unions.
4193
4194 Comp := First_Component (Rec);
4195 while Present (Comp) loop
4196
4197 -- Do not set Has_Controlled_Component on a class-wide
4198 -- equivalent type. See Make_CW_Equivalent_Type.
4199
4200 if not Is_Class_Wide_Equivalent_Type (Rec)
4201 and then
4202 (Has_Controlled_Component (Etype (Comp))
4203 or else
4204 (Chars (Comp) /= Name_uParent
4205 and then Is_Controlled_Active (Etype (Comp)))
4206 or else
4207 (Is_Protected_Type (Etype (Comp))
4208 and then
4209 Present (Corresponding_Record_Type (Etype (Comp)))
4210 and then
4211 Has_Controlled_Component
4212 (Corresponding_Record_Type (Etype (Comp)))))
4213 then
4214 Set_Has_Controlled_Component (Rec);
4215 end if;
4216
4217 if Has_Unchecked_Union (Etype (Comp)) then
4218 Set_Has_Unchecked_Union (Rec);
4219 end if;
4220
4221 -- Scan component declaration for likely misuses of current
4222 -- instance, either in a constraint or a default expression.
4223
4224 if Has_Per_Object_Constraint (Comp) then
4225 Check_Current_Instance (Parent (Comp));
4226 end if;
4227
4228 Next_Component (Comp);
4229 end loop;
4230 end if;
4231
4232 -- Enforce the restriction that access attributes with a current
4233 -- instance prefix can only apply to limited types. This comment
4234 -- is floating here, but does not seem to belong here???
4235
4236 -- Set component alignment if not otherwise already set
4237
4238 Set_Component_Alignment_If_Not_Set (Rec);
4239
4240 -- For first subtypes, check if there are any fixed-point fields with
4241 -- component clauses, where we must check the size. This is not done
4242 -- till the freeze point since for fixed-point types, we do not know
4243 -- the size until the type is frozen. Similar processing applies to
4244 -- bit packed arrays.
4245
4246 if Is_First_Subtype (Rec) then
4247 Comp := First_Component (Rec);
4248 while Present (Comp) loop
4249 if Present (Component_Clause (Comp))
4250 and then (Is_Fixed_Point_Type (Etype (Comp))
4251 or else Is_Bit_Packed_Array (Etype (Comp)))
4252 then
4253 Check_Size
4254 (Component_Name (Component_Clause (Comp)),
4255 Etype (Comp),
4256 Esize (Comp),
4257 Junk);
4258 end if;
4259
4260 Next_Component (Comp);
4261 end loop;
4262 end if;
4263
4264 -- Generate warning for applying C or C++ convention to a record
4265 -- with discriminants. This is suppressed for the unchecked union
4266 -- case, since the whole point in this case is interface C. We also
4267 -- do not generate this within instantiations, since we will have
4268 -- generated a message on the template.
4269
4270 if Has_Discriminants (E)
4271 and then not Is_Unchecked_Union (E)
4272 and then (Convention (E) = Convention_C
4273 or else
4274 Convention (E) = Convention_CPP)
4275 and then Comes_From_Source (E)
4276 and then not In_Instance
4277 and then not Has_Warnings_Off (E)
4278 and then not Has_Warnings_Off (Base_Type (E))
4279 then
4280 declare
4281 Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
4282 A2 : Node_Id;
4283
4284 begin
4285 if Present (Cprag) then
4286 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
4287
4288 if Convention (E) = Convention_C then
4289 Error_Msg_N
4290 ("?x?variant record has no direct equivalent in C",
4291 A2);
4292 else
4293 Error_Msg_N
4294 ("?x?variant record has no direct equivalent in C++",
4295 A2);
4296 end if;
4297
4298 Error_Msg_NE
4299 ("\?x?use of convention for type& is dubious", A2, E);
4300 end if;
4301 end;
4302 end if;
4303
4304 -- See if Size is too small as is (and implicit packing might help)
4305
4306 if not Is_Packed (Rec)
4307
4308 -- No implicit packing if even one component is explicitly placed
4309
4310 and then not Placed_Component
4311
4312 -- Or even one component is aliased
4313
4314 and then not Aliased_Component
4315
4316 -- Must have size clause and all scalar components
4317
4318 and then Has_Size_Clause (Rec)
4319 and then All_Scalar_Components
4320
4321 -- Do not try implicit packing on records with discriminants, too
4322 -- complicated, especially in the variant record case.
4323
4324 and then not Has_Discriminants (Rec)
4325
4326 -- We can implicitly pack if the specified size of the record is
4327 -- less than the sum of the object sizes (no point in packing if
4328 -- this is not the case).
4329
4330 and then RM_Size (Rec) < Scalar_Component_Total_Esize
4331
4332 -- And the total RM size cannot be greater than the specified size
4333 -- since otherwise packing will not get us where we have to be.
4334
4335 and then RM_Size (Rec) >= Scalar_Component_Total_RM_Size
4336
4337 -- Never do implicit packing in CodePeer or SPARK modes since
4338 -- we don't do any packing in these modes, since this generates
4339 -- over-complex code that confuses static analysis, and in
4340 -- general, neither CodePeer not GNATprove care about the
4341 -- internal representation of objects.
4342
4343 and then not (CodePeer_Mode or GNATprove_Mode)
4344 then
4345 -- If implicit packing enabled, do it
4346
4347 if Implicit_Packing then
4348 Set_Is_Packed (Rec);
4349
4350 -- Otherwise flag the size clause
4351
4352 else
4353 declare
4354 Sz : constant Node_Id := Size_Clause (Rec);
4355 begin
4356 Error_Msg_NE -- CODEFIX
4357 ("size given for& too small", Sz, Rec);
4358 Error_Msg_N -- CODEFIX
4359 ("\use explicit pragma Pack "
4360 & "or use pragma Implicit_Packing", Sz);
4361 end;
4362 end if;
4363 end if;
4364
4365 -- The following checks are relevant only when SPARK_Mode is on as
4366 -- they are not standard Ada legality rules.
4367
4368 if SPARK_Mode = On then
4369 if Is_Effectively_Volatile (Rec) then
4370
4371 -- A discriminated type cannot be effectively volatile
4372 -- (SPARK RM C.6(4)).
4373
4374 if Has_Discriminants (Rec) then
4375 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
4376
4377 -- A tagged type cannot be effectively volatile
4378 -- (SPARK RM C.6(5)).
4379
4380 elsif Is_Tagged_Type (Rec) then
4381 Error_Msg_N ("tagged type & cannot be volatile", Rec);
4382 end if;
4383
4384 -- A non-effectively volatile record type cannot contain
4385 -- effectively volatile components (SPARK RM C.6(2)).
4386
4387 else
4388 Comp := First_Component (Rec);
4389 while Present (Comp) loop
4390 if Comes_From_Source (Comp)
4391 and then Is_Effectively_Volatile (Etype (Comp))
4392 then
4393 Error_Msg_Name_1 := Chars (Rec);
4394 Error_Msg_N
4395 ("component & of non-volatile type % cannot be "
4396 & "volatile", Comp);
4397 end if;
4398
4399 Next_Component (Comp);
4400 end loop;
4401 end if;
4402
4403 -- A type which does not yield a synchronized object cannot have
4404 -- a component that yields a synchronized object (SPARK RM 9.5).
4405
4406 if not Yields_Synchronized_Object (Rec) then
4407 Comp := First_Component (Rec);
4408 while Present (Comp) loop
4409 if Comes_From_Source (Comp)
4410 and then Yields_Synchronized_Object (Etype (Comp))
4411 then
4412 Error_Msg_Name_1 := Chars (Rec);
4413 Error_Msg_N
4414 ("component & of non-synchronized type % cannot be "
4415 & "synchronized", Comp);
4416 end if;
4417
4418 Next_Component (Comp);
4419 end loop;
4420 end if;
4421
4422 -- A Ghost type cannot have a component of protected or task type
4423 -- (SPARK RM 6.9(19)).
4424
4425 if Is_Ghost_Entity (Rec) then
4426 Comp := First_Component (Rec);
4427 while Present (Comp) loop
4428 if Comes_From_Source (Comp)
4429 and then Is_Concurrent_Type (Etype (Comp))
4430 then
4431 Error_Msg_Name_1 := Chars (Rec);
4432 Error_Msg_N
4433 ("component & of ghost type % cannot be concurrent",
4434 Comp);
4435 end if;
4436
4437 Next_Component (Comp);
4438 end loop;
4439 end if;
4440 end if;
4441
4442 -- Make sure that if we have an iterator aspect, then we have
4443 -- either Constant_Indexing or Variable_Indexing.
4444
4445 declare
4446 Iterator_Aspect : Node_Id;
4447
4448 begin
4449 Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
4450
4451 if No (Iterator_Aspect) then
4452 Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
4453 end if;
4454
4455 if Present (Iterator_Aspect) then
4456 if Has_Aspect (Rec, Aspect_Constant_Indexing)
4457 or else
4458 Has_Aspect (Rec, Aspect_Variable_Indexing)
4459 then
4460 null;
4461 else
4462 Error_Msg_N
4463 ("Iterator_Element requires indexing aspect",
4464 Iterator_Aspect);
4465 end if;
4466 end if;
4467 end;
4468
4469 -- All done if not a full record definition
4470
4471 if Ekind (Rec) /= E_Record_Type then
4472 return;
4473 end if;
4474
4475 -- Finally we need to check the variant part to make sure that
4476 -- all types within choices are properly frozen as part of the
4477 -- freezing of the record type.
4478
4479 Check_Variant_Part : declare
4480 D : constant Node_Id := Declaration_Node (Rec);
4481 T : Node_Id;
4482 C : Node_Id;
4483
4484 begin
4485 -- Find component list
4486
4487 C := Empty;
4488
4489 if Nkind (D) = N_Full_Type_Declaration then
4490 T := Type_Definition (D);
4491
4492 if Nkind (T) = N_Record_Definition then
4493 C := Component_List (T);
4494
4495 elsif Nkind (T) = N_Derived_Type_Definition
4496 and then Present (Record_Extension_Part (T))
4497 then
4498 C := Component_List (Record_Extension_Part (T));
4499 end if;
4500 end if;
4501
4502 -- Case of variant part present
4503
4504 if Present (C) and then Present (Variant_Part (C)) then
4505 Freeze_Choices_In_Variant_Part (Variant_Part (C));
4506 end if;
4507
4508 -- Note: we used to call Check_Choices here, but it is too early,
4509 -- since predicated subtypes are frozen here, but their freezing
4510 -- actions are in Analyze_Freeze_Entity, which has not been called
4511 -- yet for entities frozen within this procedure, so we moved that
4512 -- call to the Analyze_Freeze_Entity for the record type.
4513
4514 end Check_Variant_Part;
4515
4516 -- Check that all the primitives of an interface type are abstract
4517 -- or null procedures.
4518
4519 if Is_Interface (Rec)
4520 and then not Error_Posted (Parent (Rec))
4521 then
4522 declare
4523 Elmt : Elmt_Id;
4524 Subp : Entity_Id;
4525
4526 begin
4527 Elmt := First_Elmt (Primitive_Operations (Rec));
4528 while Present (Elmt) loop
4529 Subp := Node (Elmt);
4530
4531 if not Is_Abstract_Subprogram (Subp)
4532
4533 -- Avoid reporting the error on inherited primitives
4534
4535 and then Comes_From_Source (Subp)
4536 then
4537 Error_Msg_Name_1 := Chars (Subp);
4538
4539 if Ekind (Subp) = E_Procedure then
4540 if not Null_Present (Parent (Subp)) then
4541 Error_Msg_N
4542 ("interface procedure % must be abstract or null",
4543 Parent (Subp));
4544 end if;
4545 else
4546 Error_Msg_N
4547 ("interface function % must be abstract",
4548 Parent (Subp));
4549 end if;
4550 end if;
4551
4552 Next_Elmt (Elmt);
4553 end loop;
4554 end;
4555 end if;
4556 end Freeze_Record_Type;
4557
4558 -------------------------------
4559 -- Has_Boolean_Aspect_Import --
4560 -------------------------------
4561
4562 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4563 Decl : constant Node_Id := Declaration_Node (E);
4564 Asp : Node_Id;
4565 Expr : Node_Id;
4566
4567 begin
4568 if Has_Aspects (Decl) then
4569 Asp := First (Aspect_Specifications (Decl));
4570 while Present (Asp) loop
4571 Expr := Expression (Asp);
4572
4573 -- The value of aspect Import is True when the expression is
4574 -- either missing or it is explicitly set to True.
4575
4576 if Get_Aspect_Id (Asp) = Aspect_Import
4577 and then (No (Expr)
4578 or else (Compile_Time_Known_Value (Expr)
4579 and then Is_True (Expr_Value (Expr))))
4580 then
4581 return True;
4582 end if;
4583
4584 Next (Asp);
4585 end loop;
4586 end if;
4587
4588 return False;
4589 end Has_Boolean_Aspect_Import;
4590
4591 ----------------------------
4592 -- Late_Freeze_Subprogram --
4593 ----------------------------
4594
4595 procedure Late_Freeze_Subprogram (E : Entity_Id) is
4596 Spec : constant Node_Id :=
4597 Specification (Unit_Declaration_Node (Scope (E)));
4598 Decls : List_Id;
4599
4600 begin
4601 if Present (Private_Declarations (Spec)) then
4602 Decls := Private_Declarations (Spec);
4603 else
4604 Decls := Visible_Declarations (Spec);
4605 end if;
4606
4607 Append_List (Result, Decls);
4608 end Late_Freeze_Subprogram;
4609
4610 ---------------------
4611 -- New_Freeze_Node --
4612 ---------------------
4613
4614 function New_Freeze_Node return Node_Id is
4615 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4616 Result : Node_Id;
4617
4618 begin
4619 -- Handle the case where an ignored Ghost subprogram freezes the type
4620 -- of one of its formals. The type can either be non-Ghost or checked
4621 -- Ghost. Since the freeze node for the type is generated in the
4622 -- context of the subprogram, the node will be incorrectly flagged as
4623 -- ignored Ghost and erroneously removed from the tree.
4624
4625 -- type Typ is ...;
4626 -- procedure Ignored_Ghost_Proc (Formal : Typ) with Ghost;
4627
4628 -- Reset the Ghost mode to "none". This preserves the freeze node.
4629
4630 if Ghost_Mode = Ignore
4631 and then not Is_Ignored_Ghost_Entity (E)
4632 and then not Is_Ignored_Ghost_Node (E)
4633 then
4634 Ghost_Mode := None;
4635 end if;
4636
4637 Result := New_Node (N_Freeze_Entity, Loc);
4638
4639 Ghost_Mode := Save_Ghost_Mode;
4640 return Result;
4641 end New_Freeze_Node;
4642
4643 ------------------------------
4644 -- Wrap_Imported_Subprogram --
4645 ------------------------------
4646
4647 -- The issue here is that our normal approach of checking preconditions
4648 -- and postconditions does not work for imported procedures, since we
4649 -- are not generating code for the body. To get around this we create
4650 -- a wrapper, as shown by the following example:
4651
4652 -- procedure K (A : Integer);
4653 -- pragma Import (C, K);
4654
4655 -- The spec is rewritten by removing the effects of pragma Import, but
4656 -- leaving the convention unchanged, as though the source had said:
4657
4658 -- procedure K (A : Integer);
4659 -- pragma Convention (C, K);
4660
4661 -- and we create a body, added to the entity K freeze actions, which
4662 -- looks like:
4663
4664 -- procedure K (A : Integer) is
4665 -- procedure K (A : Integer);
4666 -- pragma Import (C, K);
4667 -- begin
4668 -- K (A);
4669 -- end K;
4670
4671 -- Now the contract applies in the normal way to the outer procedure,
4672 -- and the inner procedure has no contracts, so there is no problem
4673 -- in just calling it to get the original effect.
4674
4675 -- In the case of a function, we create an appropriate return statement
4676 -- for the subprogram body that calls the inner procedure.
4677
4678 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
4679 function Copy_Import_Pragma return Node_Id;
4680 -- Obtain a copy of the Import_Pragma which belongs to subprogram E
4681
4682 ------------------------
4683 -- Copy_Import_Pragma --
4684 ------------------------
4685
4686 function Copy_Import_Pragma return Node_Id is
4687
4688 -- The subprogram should have an import pragma, otherwise it does
4689 -- need a wrapper.
4690
4691 Prag : constant Node_Id := Import_Pragma (E);
4692 pragma Assert (Present (Prag));
4693
4694 -- Save all semantic fields of the pragma
4695
4696 Save_Asp : constant Node_Id := Corresponding_Aspect (Prag);
4697 Save_From : constant Boolean := From_Aspect_Specification (Prag);
4698 Save_Prag : constant Node_Id := Next_Pragma (Prag);
4699 Save_Rep : constant Node_Id := Next_Rep_Item (Prag);
4700
4701 Result : Node_Id;
4702
4703 begin
4704 -- Reset all semantic fields. This avoids a potential infinite
4705 -- loop when the pragma comes from an aspect as the duplication
4706 -- will copy the aspect, then copy the corresponding pragma and
4707 -- so on.
4708
4709 Set_Corresponding_Aspect (Prag, Empty);
4710 Set_From_Aspect_Specification (Prag, False);
4711 Set_Next_Pragma (Prag, Empty);
4712 Set_Next_Rep_Item (Prag, Empty);
4713
4714 Result := Copy_Separate_Tree (Prag);
4715
4716 -- Restore the original semantic fields
4717
4718 Set_Corresponding_Aspect (Prag, Save_Asp);
4719 Set_From_Aspect_Specification (Prag, Save_From);
4720 Set_Next_Pragma (Prag, Save_Prag);
4721 Set_Next_Rep_Item (Prag, Save_Rep);
4722
4723 return Result;
4724 end Copy_Import_Pragma;
4725
4726 -- Local variables
4727
4728 Loc : constant Source_Ptr := Sloc (E);
4729 CE : constant Name_Id := Chars (E);
4730 Bod : Node_Id;
4731 Forml : Entity_Id;
4732 Parms : List_Id;
4733 Prag : Node_Id;
4734 Spec : Node_Id;
4735 Stmt : Node_Id;
4736
4737 -- Start of processing for Wrap_Imported_Subprogram
4738
4739 begin
4740 -- Nothing to do if not imported
4741
4742 if not Is_Imported (E) then
4743 return;
4744
4745 -- Test enabling conditions for wrapping
4746
4747 elsif Is_Subprogram (E)
4748 and then Present (Contract (E))
4749 and then Present (Pre_Post_Conditions (Contract (E)))
4750 and then not GNATprove_Mode
4751 then
4752 -- Here we do the wrap
4753
4754 -- Note on calls to Copy_Separate_Tree. The trees we are copying
4755 -- here are fully analyzed, but we definitely want fully syntactic
4756 -- unanalyzed trees in the body we construct, so that the analysis
4757 -- generates the right visibility, and that is exactly what the
4758 -- calls to Copy_Separate_Tree give us.
4759
4760 Prag := Copy_Import_Pragma;
4761
4762 -- Fix up spec to be not imported any more
4763
4764 Set_Has_Completion (E, False);
4765 Set_Import_Pragma (E, Empty);
4766 Set_Interface_Name (E, Empty);
4767 Set_Is_Imported (E, False);
4768
4769 -- Grab the subprogram declaration and specification
4770
4771 Spec := Declaration_Node (E);
4772
4773 -- Build parameter list that we need
4774
4775 Parms := New_List;
4776 Forml := First_Formal (E);
4777 while Present (Forml) loop
4778 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
4779 Next_Formal (Forml);
4780 end loop;
4781
4782 -- Build the call
4783
4784 if Ekind_In (E, E_Function, E_Generic_Function) then
4785 Stmt :=
4786 Make_Simple_Return_Statement (Loc,
4787 Expression =>
4788 Make_Function_Call (Loc,
4789 Name => Make_Identifier (Loc, CE),
4790 Parameter_Associations => Parms));
4791
4792 else
4793 Stmt :=
4794 Make_Procedure_Call_Statement (Loc,
4795 Name => Make_Identifier (Loc, CE),
4796 Parameter_Associations => Parms);
4797 end if;
4798
4799 -- Now build the body
4800
4801 Bod :=
4802 Make_Subprogram_Body (Loc,
4803 Specification =>
4804 Copy_Separate_Tree (Spec),
4805 Declarations => New_List (
4806 Make_Subprogram_Declaration (Loc,
4807 Specification => Copy_Separate_Tree (Spec)),
4808 Prag),
4809 Handled_Statement_Sequence =>
4810 Make_Handled_Sequence_Of_Statements (Loc,
4811 Statements => New_List (Stmt),
4812 End_Label => Make_Identifier (Loc, CE)));
4813
4814 -- Append the body to freeze result
4815
4816 Add_To_Result (Bod);
4817 return;
4818
4819 -- Case of imported subprogram that does not get wrapped
4820
4821 else
4822 -- Set Is_Public. All imported entities need an external symbol
4823 -- created for them since they are always referenced from another
4824 -- object file. Note this used to be set when we set Is_Imported
4825 -- back in Sem_Prag, but now we delay it to this point, since we
4826 -- don't want to set this flag if we wrap an imported subprogram.
4827
4828 Set_Is_Public (E);
4829 end if;
4830 end Wrap_Imported_Subprogram;
4831
4832 -- Local variables
4833
4834 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4835
4836 -- Start of processing for Freeze_Entity
4837
4838 begin
4839 -- The entity being frozen may be subject to pragma Ghost. Set the mode
4840 -- now to ensure that any nodes generated during freezing are properly
4841 -- flagged as Ghost.
4842
4843 Set_Ghost_Mode_From_Entity (E);
4844
4845 -- We are going to test for various reasons why this entity need not be
4846 -- frozen here, but in the case of an Itype that's defined within a
4847 -- record, that test actually applies to the record.
4848
4849 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
4850 Test_E := Scope (E);
4851 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
4852 and then Is_Record_Type (Underlying_Type (Scope (E)))
4853 then
4854 Test_E := Underlying_Type (Scope (E));
4855 end if;
4856
4857 -- Do not freeze if already frozen since we only need one freeze node
4858
4859 if Is_Frozen (E) then
4860 Ghost_Mode := Save_Ghost_Mode;
4861 return No_List;
4862
4863 -- It is improper to freeze an external entity within a generic because
4864 -- its freeze node will appear in a non-valid context. The entity will
4865 -- be frozen in the proper scope after the current generic is analyzed.
4866 -- However, aspects must be analyzed because they may be queried later
4867 -- within the generic itself, and the corresponding pragma or attribute
4868 -- definition has not been analyzed yet.
4869
4870 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
4871 if Has_Delayed_Aspects (E) then
4872 Analyze_Aspects_At_Freeze_Point (E);
4873 end if;
4874
4875 Ghost_Mode := Save_Ghost_Mode;
4876 return No_List;
4877
4878 -- AI05-0213: A formal incomplete type does not freeze the actual. In
4879 -- the instance, the same applies to the subtype renaming the actual.
4880
4881 elsif Is_Private_Type (E)
4882 and then Is_Generic_Actual_Type (E)
4883 and then No (Full_View (Base_Type (E)))
4884 and then Ada_Version >= Ada_2012
4885 then
4886 Ghost_Mode := Save_Ghost_Mode;
4887 return No_List;
4888
4889 -- Formal subprograms are never frozen
4890
4891 elsif Is_Formal_Subprogram (E) then
4892 Ghost_Mode := Save_Ghost_Mode;
4893 return No_List;
4894
4895 -- Generic types are never frozen as they lack delayed semantic checks
4896
4897 elsif Is_Generic_Type (E) then
4898 Ghost_Mode := Save_Ghost_Mode;
4899 return No_List;
4900
4901 -- Do not freeze a global entity within an inner scope created during
4902 -- expansion. A call to subprogram E within some internal procedure
4903 -- (a stream attribute for example) might require freezing E, but the
4904 -- freeze node must appear in the same declarative part as E itself.
4905 -- The two-pass elaboration mechanism in gigi guarantees that E will
4906 -- be frozen before the inner call is elaborated. We exclude constants
4907 -- from this test, because deferred constants may be frozen early, and
4908 -- must be diagnosed (e.g. in the case of a deferred constant being used
4909 -- in a default expression). If the enclosing subprogram comes from
4910 -- source, or is a generic instance, then the freeze point is the one
4911 -- mandated by the language, and we freeze the entity. A subprogram that
4912 -- is a child unit body that acts as a spec does not have a spec that
4913 -- comes from source, but can only come from source.
4914
4915 elsif In_Open_Scopes (Scope (Test_E))
4916 and then Scope (Test_E) /= Current_Scope
4917 and then Ekind (Test_E) /= E_Constant
4918 then
4919 declare
4920 S : Entity_Id;
4921
4922 begin
4923 S := Current_Scope;
4924 while Present (S) loop
4925 if Is_Overloadable (S) then
4926 if Comes_From_Source (S)
4927 or else Is_Generic_Instance (S)
4928 or else Is_Child_Unit (S)
4929 then
4930 exit;
4931 else
4932 Ghost_Mode := Save_Ghost_Mode;
4933 return No_List;
4934 end if;
4935 end if;
4936
4937 S := Scope (S);
4938 end loop;
4939 end;
4940
4941 -- Similarly, an inlined instance body may make reference to global
4942 -- entities, but these references cannot be the proper freezing point
4943 -- for them, and in the absence of inlining freezing will take place in
4944 -- their own scope. Normally instance bodies are analyzed after the
4945 -- enclosing compilation, and everything has been frozen at the proper
4946 -- place, but with front-end inlining an instance body is compiled
4947 -- before the end of the enclosing scope, and as a result out-of-order
4948 -- freezing must be prevented.
4949
4950 elsif Front_End_Inlining
4951 and then In_Instance_Body
4952 and then Present (Scope (Test_E))
4953 then
4954 declare
4955 S : Entity_Id;
4956
4957 begin
4958 S := Scope (Test_E);
4959 while Present (S) loop
4960 if Is_Generic_Instance (S) then
4961 exit;
4962 else
4963 S := Scope (S);
4964 end if;
4965 end loop;
4966
4967 if No (S) then
4968 Ghost_Mode := Save_Ghost_Mode;
4969 return No_List;
4970 end if;
4971 end;
4972
4973 elsif Ekind (E) = E_Generic_Package then
4974 Result := Freeze_Generic_Entities (E);
4975
4976 Ghost_Mode := Save_Ghost_Mode;
4977 return Result;
4978 end if;
4979
4980 -- Add checks to detect proper initialization of scalars that may appear
4981 -- as subprogram parameters.
4982
4983 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
4984 Apply_Parameter_Validity_Checks (E);
4985 end if;
4986
4987 -- Deal with delayed aspect specifications. The analysis of the aspect
4988 -- is required to be delayed to the freeze point, thus we analyze the
4989 -- pragma or attribute definition clause in the tree at this point. We
4990 -- also analyze the aspect specification node at the freeze point when
4991 -- the aspect doesn't correspond to pragma/attribute definition clause.
4992
4993 if Has_Delayed_Aspects (E) then
4994 Analyze_Aspects_At_Freeze_Point (E);
4995 end if;
4996
4997 -- Here to freeze the entity
4998
4999 Set_Is_Frozen (E);
5000
5001 -- Case of entity being frozen is other than a type
5002
5003 if not Is_Type (E) then
5004
5005 -- If entity is exported or imported and does not have an external
5006 -- name, now is the time to provide the appropriate default name.
5007 -- Skip this if the entity is stubbed, since we don't need a name
5008 -- for any stubbed routine. For the case on intrinsics, if no
5009 -- external name is specified, then calls will be handled in
5010 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
5011 -- external name is provided, then Expand_Intrinsic_Call leaves
5012 -- calls in place for expansion by GIGI.
5013
5014 if (Is_Imported (E) or else Is_Exported (E))
5015 and then No (Interface_Name (E))
5016 and then Convention (E) /= Convention_Stubbed
5017 and then Convention (E) /= Convention_Intrinsic
5018 then
5019 Set_Encoded_Interface_Name
5020 (E, Get_Default_External_Name (E));
5021
5022 -- If entity is an atomic object appearing in a declaration and
5023 -- the expression is an aggregate, assign it to a temporary to
5024 -- ensure that the actual assignment is done atomically rather
5025 -- than component-wise (the assignment to the temp may be done
5026 -- component-wise, but that is harmless).
5027
5028 elsif Is_Atomic_Or_VFA (E)
5029 and then Nkind (Parent (E)) = N_Object_Declaration
5030 and then Present (Expression (Parent (E)))
5031 and then Nkind (Expression (Parent (E))) = N_Aggregate
5032 and then Is_Atomic_VFA_Aggregate (Expression (Parent (E)))
5033 then
5034 null;
5035 end if;
5036
5037 -- Subprogram case
5038
5039 if Is_Subprogram (E) then
5040
5041 -- Check for needing to wrap imported subprogram
5042
5043 Wrap_Imported_Subprogram (E);
5044
5045 -- Freeze all parameter types and the return type (RM 13.14(14)).
5046 -- However skip this for internal subprograms. This is also where
5047 -- any extra formal parameters are created since we now know
5048 -- whether the subprogram will use a foreign convention.
5049
5050 -- In Ada 2012, freezing a subprogram does not always freeze the
5051 -- corresponding profile (see AI05-019). An attribute reference
5052 -- is not a freezing point of the profile. Flag Do_Freeze_Profile
5053 -- indicates whether the profile should be frozen now.
5054 -- Other constructs that should not freeze ???
5055
5056 -- This processing doesn't apply to internal entities (see below)
5057
5058 -- Disable this mechanism for now, to fix regressions in ASIS and
5059 -- various ACATS tests. Implementation of AI05-019 remains
5060 -- unsolved ???
5061
5062 if not Is_Internal (E)
5063 and then (Do_Freeze_Profile or else True)
5064 then
5065 if not Freeze_Profile (E) then
5066 Ghost_Mode := Save_Ghost_Mode;
5067 return Result;
5068 end if;
5069 end if;
5070
5071 -- Must freeze its parent first if it is a derived subprogram
5072
5073 if Present (Alias (E)) then
5074 Freeze_And_Append (Alias (E), N, Result);
5075 end if;
5076
5077 -- We don't freeze internal subprograms, because we don't normally
5078 -- want addition of extra formals or mechanism setting to happen
5079 -- for those. However we do pass through predefined dispatching
5080 -- cases, since extra formals may be needed in some cases, such as
5081 -- for the stream 'Input function (build-in-place formals).
5082
5083 if not Is_Internal (E)
5084 or else Is_Predefined_Dispatching_Operation (E)
5085 then
5086 Freeze_Subprogram (E);
5087 end if;
5088
5089 if Late_Freezing then
5090 Late_Freeze_Subprogram (E);
5091 Ghost_Mode := Save_Ghost_Mode;
5092 return No_List;
5093 end if;
5094
5095 -- If warning on suspicious contracts then check for the case of
5096 -- a postcondition other than False for a No_Return subprogram.
5097
5098 if No_Return (E)
5099 and then Warn_On_Suspicious_Contract
5100 and then Present (Contract (E))
5101 then
5102 declare
5103 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
5104 Exp : Node_Id;
5105
5106 begin
5107 while Present (Prag) loop
5108 if Nam_In (Pragma_Name (Prag), Name_Post,
5109 Name_Postcondition,
5110 Name_Refined_Post)
5111 then
5112 Exp :=
5113 Expression
5114 (First (Pragma_Argument_Associations (Prag)));
5115
5116 if Nkind (Exp) /= N_Identifier
5117 or else Chars (Exp) /= Name_False
5118 then
5119 Error_Msg_NE
5120 ("useless postcondition, & is marked "
5121 & "No_Return?T?", Exp, E);
5122 end if;
5123 end if;
5124
5125 Prag := Next_Pragma (Prag);
5126 end loop;
5127 end;
5128 end if;
5129
5130 -- Here for other than a subprogram or type
5131
5132 else
5133 -- If entity has a type, and it is not a generic unit, then
5134 -- freeze it first (RM 13.14(10)).
5135
5136 if Present (Etype (E))
5137 and then Ekind (E) /= E_Generic_Function
5138 then
5139 Freeze_And_Append (Etype (E), N, Result);
5140
5141 -- For an object of an anonymous array type, aspects on the
5142 -- object declaration apply to the type itself. This is the
5143 -- case for Atomic_Components, Volatile_Components, and
5144 -- Independent_Components. In these cases analysis of the
5145 -- generated pragma will mark the anonymous types accordingly,
5146 -- and the object itself does not require a freeze node.
5147
5148 if Ekind (E) = E_Variable
5149 and then Is_Itype (Etype (E))
5150 and then Is_Array_Type (Etype (E))
5151 and then Has_Delayed_Aspects (E)
5152 then
5153 Set_Has_Delayed_Aspects (E, False);
5154 Set_Has_Delayed_Freeze (E, False);
5155 Set_Freeze_Node (E, Empty);
5156 end if;
5157 end if;
5158
5159 -- Special processing for objects created by object declaration
5160
5161 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
5162 Freeze_Object_Declaration (E);
5163 end if;
5164
5165 -- Check that a constant which has a pragma Volatile[_Components]
5166 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
5167
5168 -- Note: Atomic[_Components] also sets Volatile[_Components]
5169
5170 if Ekind (E) = E_Constant
5171 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
5172 and then not Is_Imported (E)
5173 and then not Has_Boolean_Aspect_Import (E)
5174 then
5175 -- Make sure we actually have a pragma, and have not merely
5176 -- inherited the indication from elsewhere (e.g. an address
5177 -- clause, which is not good enough in RM terms).
5178
5179 if Has_Rep_Pragma (E, Name_Atomic)
5180 or else
5181 Has_Rep_Pragma (E, Name_Atomic_Components)
5182 then
5183 Error_Msg_N
5184 ("stand alone atomic constant must be " &
5185 "imported (RM C.6(13))", E);
5186
5187 elsif Has_Rep_Pragma (E, Name_Volatile)
5188 or else
5189 Has_Rep_Pragma (E, Name_Volatile_Components)
5190 then
5191 Error_Msg_N
5192 ("stand alone volatile constant must be " &
5193 "imported (RM C.6(13))", E);
5194 end if;
5195 end if;
5196
5197 -- Static objects require special handling
5198
5199 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
5200 and then Is_Statically_Allocated (E)
5201 then
5202 Freeze_Static_Object (E);
5203 end if;
5204
5205 -- Remaining step is to layout objects
5206
5207 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
5208 or else Is_Formal (E)
5209 then
5210 Layout_Object (E);
5211 end if;
5212
5213 -- For an object that does not have delayed freezing, and whose
5214 -- initialization actions have been captured in a compound
5215 -- statement, move them back now directly within the enclosing
5216 -- statement sequence.
5217
5218 if Ekind_In (E, E_Constant, E_Variable)
5219 and then not Has_Delayed_Freeze (E)
5220 then
5221 Explode_Initialization_Compound_Statement (E);
5222 end if;
5223 end if;
5224
5225 -- Case of a type or subtype being frozen
5226
5227 else
5228 -- We used to check here that a full type must have preelaborable
5229 -- initialization if it completes a private type specified with
5230 -- pragma Preelaborable_Initialization, but that missed cases where
5231 -- the types occur within a generic package, since the freezing
5232 -- that occurs within a containing scope generally skips traversal
5233 -- of a generic unit's declarations (those will be frozen within
5234 -- instances). This check was moved to Analyze_Package_Specification.
5235
5236 -- The type may be defined in a generic unit. This can occur when
5237 -- freezing a generic function that returns the type (which is
5238 -- defined in a parent unit). It is clearly meaningless to freeze
5239 -- this type. However, if it is a subtype, its size may be determi-
5240 -- nable and used in subsequent checks, so might as well try to
5241 -- compute it.
5242
5243 -- In Ada 2012, Freeze_Entities is also used in the front end to
5244 -- trigger the analysis of aspect expressions, so in this case we
5245 -- want to continue the freezing process.
5246
5247 if Present (Scope (E))
5248 and then Is_Generic_Unit (Scope (E))
5249 and then
5250 (not Has_Predicates (E)
5251 and then not Has_Delayed_Freeze (E))
5252 then
5253 Check_Compile_Time_Size (E);
5254 Ghost_Mode := Save_Ghost_Mode;
5255 return No_List;
5256 end if;
5257
5258 -- Check for error of Type_Invariant'Class applied to an untagged
5259 -- type (check delayed to freeze time when full type is available).
5260
5261 declare
5262 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
5263 begin
5264 if Present (Prag)
5265 and then Class_Present (Prag)
5266 and then not Is_Tagged_Type (E)
5267 then
5268 Error_Msg_NE
5269 ("Type_Invariant''Class cannot be specified for &",
5270 Prag, E);
5271 Error_Msg_N
5272 ("\can only be specified for a tagged type", Prag);
5273 end if;
5274 end;
5275
5276 if Is_Ghost_Entity (E) then
5277
5278 -- A Ghost type cannot be concurrent (SPARK RM 6.9(19)). Verify
5279 -- this legality rule first to five a finer-grained diagnostic.
5280
5281 if Is_Concurrent_Type (E) then
5282 Error_Msg_N ("ghost type & cannot be concurrent", E);
5283
5284 -- A Ghost type cannot be effectively volatile (SPARK RM 6.9(7))
5285
5286 elsif Is_Effectively_Volatile (E) then
5287 Error_Msg_N ("ghost type & cannot be volatile", E);
5288 end if;
5289 end if;
5290
5291 -- Deal with special cases of freezing for subtype
5292
5293 if E /= Base_Type (E) then
5294
5295 -- Before we do anything else, a specialized test for the case of
5296 -- a size given for an array where the array needs to be packed,
5297 -- but was not so the size cannot be honored. This is the case
5298 -- where implicit packing may apply. The reason we do this so
5299 -- early is that if we have implicit packing, the layout of the
5300 -- base type is affected, so we must do this before we freeze
5301 -- the base type.
5302
5303 -- We could do this processing only if implicit packing is enabled
5304 -- since in all other cases, the error would be caught by the back
5305 -- end. However, we choose to do the check even if we do not have
5306 -- implicit packing enabled, since this allows us to give a more
5307 -- useful error message (advising use of pragmas Implicit_Packing
5308 -- or Pack).
5309
5310 if Is_Array_Type (E) then
5311 declare
5312 Ctyp : constant Entity_Id := Component_Type (E);
5313 Rsiz : constant Uint := RM_Size (Ctyp);
5314 SZ : constant Node_Id := Size_Clause (E);
5315 Btyp : constant Entity_Id := Base_Type (E);
5316
5317 Lo : Node_Id;
5318 Hi : Node_Id;
5319 Indx : Node_Id;
5320
5321 Num_Elmts : Uint;
5322 -- Number of elements in array
5323
5324 begin
5325 -- Check enabling conditions. These are straightforward
5326 -- except for the test for a limited composite type. This
5327 -- eliminates the rare case of a array of limited components
5328 -- where there are issues of whether or not we can go ahead
5329 -- and pack the array (since we can't freely pack and unpack
5330 -- arrays if they are limited).
5331
5332 -- Note that we check the root type explicitly because the
5333 -- whole point is we are doing this test before we have had
5334 -- a chance to freeze the base type (and it is that freeze
5335 -- action that causes stuff to be inherited).
5336
5337 if Has_Size_Clause (E)
5338 and then Known_Static_RM_Size (E)
5339 and then not Is_Packed (E)
5340 and then not Has_Pragma_Pack (E)
5341 and then not Has_Component_Size_Clause (E)
5342 and then Known_Static_RM_Size (Ctyp)
5343 and then RM_Size (Ctyp) < 64
5344 and then not Is_Limited_Composite (E)
5345 and then not Is_Packed (Root_Type (E))
5346 and then not Has_Component_Size_Clause (Root_Type (E))
5347 and then not (CodePeer_Mode or GNATprove_Mode)
5348 then
5349 -- Compute number of elements in array
5350
5351 Num_Elmts := Uint_1;
5352 Indx := First_Index (E);
5353 while Present (Indx) loop
5354 Get_Index_Bounds (Indx, Lo, Hi);
5355
5356 if not (Compile_Time_Known_Value (Lo)
5357 and then
5358 Compile_Time_Known_Value (Hi))
5359 then
5360 goto No_Implicit_Packing;
5361 end if;
5362
5363 Num_Elmts :=
5364 Num_Elmts *
5365 UI_Max (Uint_0,
5366 Expr_Value (Hi) - Expr_Value (Lo) + 1);
5367 Next_Index (Indx);
5368 end loop;
5369
5370 -- What we are looking for here is the situation where
5371 -- the RM_Size given would be exactly right if there was
5372 -- a pragma Pack (resulting in the component size being
5373 -- the same as the RM_Size). Furthermore, the component
5374 -- type size must be an odd size (not a multiple of
5375 -- storage unit). If the component RM size is an exact
5376 -- number of storage units that is a power of two, the
5377 -- array is not packed and has a standard representation.
5378
5379 if RM_Size (E) = Num_Elmts * Rsiz
5380 and then Rsiz mod System_Storage_Unit /= 0
5381 then
5382 -- For implicit packing mode, just set the component
5383 -- size silently.
5384
5385 if Implicit_Packing then
5386 Set_Component_Size (Btyp, Rsiz);
5387 Set_Is_Bit_Packed_Array (Btyp);
5388 Set_Is_Packed (Btyp);
5389 Set_Has_Non_Standard_Rep (Btyp);
5390
5391 -- Otherwise give an error message
5392
5393 else
5394 Error_Msg_NE
5395 ("size given for& too small", SZ, E);
5396 Error_Msg_N -- CODEFIX
5397 ("\use explicit pragma Pack "
5398 & "or use pragma Implicit_Packing", SZ);
5399 end if;
5400
5401 elsif RM_Size (E) = Num_Elmts * Rsiz
5402 and then Implicit_Packing
5403 and then
5404 (Rsiz / System_Storage_Unit = 1
5405 or else
5406 Rsiz / System_Storage_Unit = 2
5407 or else
5408 Rsiz / System_Storage_Unit = 4)
5409 then
5410 -- Not a packed array, but indicate the desired
5411 -- component size, for the back-end.
5412
5413 Set_Component_Size (Btyp, Rsiz);
5414 end if;
5415 end if;
5416 end;
5417 end if;
5418
5419 <<No_Implicit_Packing>>
5420
5421 -- If ancestor subtype present, freeze that first. Note that this
5422 -- will also get the base type frozen. Need RM reference ???
5423
5424 Atype := Ancestor_Subtype (E);
5425
5426 if Present (Atype) then
5427 Freeze_And_Append (Atype, N, Result);
5428
5429 -- No ancestor subtype present
5430
5431 else
5432 -- See if we have a nearest ancestor that has a predicate.
5433 -- That catches the case of derived type with a predicate.
5434 -- Need RM reference here ???
5435
5436 Atype := Nearest_Ancestor (E);
5437
5438 if Present (Atype) and then Has_Predicates (Atype) then
5439 Freeze_And_Append (Atype, N, Result);
5440 end if;
5441
5442 -- Freeze base type before freezing the entity (RM 13.14(15))
5443
5444 if E /= Base_Type (E) then
5445 Freeze_And_Append (Base_Type (E), N, Result);
5446 end if;
5447 end if;
5448
5449 -- A subtype inherits all the type-related representation aspects
5450 -- from its parents (RM 13.1(8)).
5451
5452 Inherit_Aspects_At_Freeze_Point (E);
5453
5454 -- For a derived type, freeze its parent type first (RM 13.14(15))
5455
5456 elsif Is_Derived_Type (E) then
5457 Freeze_And_Append (Etype (E), N, Result);
5458 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5459
5460 -- A derived type inherits each type-related representation aspect
5461 -- of its parent type that was directly specified before the
5462 -- declaration of the derived type (RM 13.1(15)).
5463
5464 Inherit_Aspects_At_Freeze_Point (E);
5465 end if;
5466
5467 -- Check for incompatible size and alignment for record type
5468
5469 if Warn_On_Size_Alignment
5470 and then Is_Record_Type (E)
5471 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5472
5473 -- If explicit Object_Size clause given assume that the programmer
5474 -- knows what he is doing, and expects the compiler behavior.
5475
5476 and then not Has_Object_Size_Clause (E)
5477
5478 -- Check for size not a multiple of alignment
5479
5480 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5481 then
5482 declare
5483 SC : constant Node_Id := Size_Clause (E);
5484 AC : constant Node_Id := Alignment_Clause (E);
5485 Loc : Node_Id;
5486 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5487
5488 begin
5489 if Present (SC) and then Present (AC) then
5490
5491 -- Give a warning
5492
5493 if Sloc (SC) > Sloc (AC) then
5494 Loc := SC;
5495 Error_Msg_NE
5496 ("?Z?size is not a multiple of alignment for &",
5497 Loc, E);
5498 Error_Msg_Sloc := Sloc (AC);
5499 Error_Msg_Uint_1 := Alignment (E);
5500 Error_Msg_N ("\?Z?alignment of ^ specified #", Loc);
5501
5502 else
5503 Loc := AC;
5504 Error_Msg_NE
5505 ("?Z?size is not a multiple of alignment for &",
5506 Loc, E);
5507 Error_Msg_Sloc := Sloc (SC);
5508 Error_Msg_Uint_1 := RM_Size (E);
5509 Error_Msg_N ("\?Z?size of ^ specified #", Loc);
5510 end if;
5511
5512 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5513 Error_Msg_N ("\?Z?Object_Size will be increased to ^", Loc);
5514 end if;
5515 end;
5516 end if;
5517
5518 -- Array type
5519
5520 if Is_Array_Type (E) then
5521 Freeze_Array_Type (E);
5522
5523 -- For a class-wide type, the corresponding specific type is
5524 -- frozen as well (RM 13.14(15))
5525
5526 elsif Is_Class_Wide_Type (E) then
5527 Freeze_And_Append (Root_Type (E), N, Result);
5528
5529 -- If the base type of the class-wide type is still incomplete,
5530 -- the class-wide remains unfrozen as well. This is legal when
5531 -- E is the formal of a primitive operation of some other type
5532 -- which is being frozen.
5533
5534 if not Is_Frozen (Root_Type (E)) then
5535 Set_Is_Frozen (E, False);
5536 Ghost_Mode := Save_Ghost_Mode;
5537 return Result;
5538 end if;
5539
5540 -- The equivalent type associated with a class-wide subtype needs
5541 -- to be frozen to ensure that its layout is done.
5542
5543 if Ekind (E) = E_Class_Wide_Subtype
5544 and then Present (Equivalent_Type (E))
5545 then
5546 Freeze_And_Append (Equivalent_Type (E), N, Result);
5547 end if;
5548
5549 -- Generate an itype reference for a library-level class-wide type
5550 -- at the freeze point. Otherwise the first explicit reference to
5551 -- the type may appear in an inner scope which will be rejected by
5552 -- the back-end.
5553
5554 if Is_Itype (E)
5555 and then Is_Compilation_Unit (Scope (E))
5556 then
5557 declare
5558 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5559
5560 begin
5561 Set_Itype (Ref, E);
5562
5563 -- From a gigi point of view, a class-wide subtype derives
5564 -- from its record equivalent type. As a result, the itype
5565 -- reference must appear after the freeze node of the
5566 -- equivalent type or gigi will reject the reference.
5567
5568 if Ekind (E) = E_Class_Wide_Subtype
5569 and then Present (Equivalent_Type (E))
5570 then
5571 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5572 else
5573 Add_To_Result (Ref);
5574 end if;
5575 end;
5576 end if;
5577
5578 -- For a record type or record subtype, freeze all component types
5579 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5580 -- using Is_Record_Type, because we don't want to attempt the freeze
5581 -- for the case of a private type with record extension (we will do
5582 -- that later when the full type is frozen).
5583
5584 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype)
5585 and then not (Present (Scope (E))
5586 and then Is_Generic_Unit (Scope (E)))
5587 then
5588 Freeze_Record_Type (E);
5589
5590 -- For a concurrent type, freeze corresponding record type. This does
5591 -- not correspond to any specific rule in the RM, but the record type
5592 -- is essentially part of the concurrent type. Also freeze all local
5593 -- entities. This includes record types created for entry parameter
5594 -- blocks and whatever local entities may appear in the private part.
5595
5596 elsif Is_Concurrent_Type (E) then
5597 if Present (Corresponding_Record_Type (E)) then
5598 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5599 end if;
5600
5601 Comp := First_Entity (E);
5602 while Present (Comp) loop
5603 if Is_Type (Comp) then
5604 Freeze_And_Append (Comp, N, Result);
5605
5606 elsif (Ekind (Comp)) /= E_Function then
5607
5608 -- The guard on the presence of the Etype seems to be needed
5609 -- for some CodePeer (-gnatcC) cases, but not clear why???
5610
5611 if Present (Etype (Comp)) then
5612 if Is_Itype (Etype (Comp))
5613 and then Underlying_Type (Scope (Etype (Comp))) = E
5614 then
5615 Undelay_Type (Etype (Comp));
5616 end if;
5617
5618 Freeze_And_Append (Etype (Comp), N, Result);
5619 end if;
5620 end if;
5621
5622 Next_Entity (Comp);
5623 end loop;
5624
5625 -- Private types are required to point to the same freeze node as
5626 -- their corresponding full views. The freeze node itself has to
5627 -- point to the partial view of the entity (because from the partial
5628 -- view, we can retrieve the full view, but not the reverse).
5629 -- However, in order to freeze correctly, we need to freeze the full
5630 -- view. If we are freezing at the end of a scope (or within the
5631 -- scope) of the private type, the partial and full views will have
5632 -- been swapped, the full view appears first in the entity chain and
5633 -- the swapping mechanism ensures that the pointers are properly set
5634 -- (on scope exit).
5635
5636 -- If we encounter the partial view before the full view (e.g. when
5637 -- freezing from another scope), we freeze the full view, and then
5638 -- set the pointers appropriately since we cannot rely on swapping to
5639 -- fix things up (subtypes in an outer scope might not get swapped).
5640
5641 -- If the full view is itself private, the above requirements apply
5642 -- to the underlying full view instead of the full view. But there is
5643 -- no swapping mechanism for the underlying full view so we need to
5644 -- set the pointers appropriately in both cases.
5645
5646 elsif Is_Incomplete_Or_Private_Type (E)
5647 and then not Is_Generic_Type (E)
5648 then
5649 -- The construction of the dispatch table associated with library
5650 -- level tagged types forces freezing of all the primitives of the
5651 -- type, which may cause premature freezing of the partial view.
5652 -- For example:
5653
5654 -- package Pkg is
5655 -- type T is tagged private;
5656 -- type DT is new T with private;
5657 -- procedure Prim (X : in out T; Y : in out DT'Class);
5658 -- private
5659 -- type T is tagged null record;
5660 -- Obj : T;
5661 -- type DT is new T with null record;
5662 -- end;
5663
5664 -- In this case the type will be frozen later by the usual
5665 -- mechanism: an object declaration, an instantiation, or the
5666 -- end of a declarative part.
5667
5668 if Is_Library_Level_Tagged_Type (E)
5669 and then not Present (Full_View (E))
5670 then
5671 Set_Is_Frozen (E, False);
5672 Ghost_Mode := Save_Ghost_Mode;
5673 return Result;
5674
5675 -- Case of full view present
5676
5677 elsif Present (Full_View (E)) then
5678
5679 -- If full view has already been frozen, then no further
5680 -- processing is required
5681
5682 if Is_Frozen (Full_View (E)) then
5683 Set_Has_Delayed_Freeze (E, False);
5684 Set_Freeze_Node (E, Empty);
5685
5686 -- Otherwise freeze full view and patch the pointers so that
5687 -- the freeze node will elaborate both views in the back end.
5688 -- However, if full view is itself private, freeze underlying
5689 -- full view instead and patch the pointers so that the freeze
5690 -- node will elaborate the three views in the back end.
5691
5692 else
5693 declare
5694 Full : Entity_Id := Full_View (E);
5695
5696 begin
5697 if Is_Private_Type (Full)
5698 and then Present (Underlying_Full_View (Full))
5699 then
5700 Full := Underlying_Full_View (Full);
5701 end if;
5702
5703 Freeze_And_Append (Full, N, Result);
5704
5705 if Full /= Full_View (E)
5706 and then Has_Delayed_Freeze (Full_View (E))
5707 then
5708 F_Node := Freeze_Node (Full);
5709
5710 if Present (F_Node) then
5711 Set_Freeze_Node (Full_View (E), F_Node);
5712 Set_Entity (F_Node, Full_View (E));
5713
5714 else
5715 Set_Has_Delayed_Freeze (Full_View (E), False);
5716 Set_Freeze_Node (Full_View (E), Empty);
5717 end if;
5718 end if;
5719
5720 if Has_Delayed_Freeze (E) then
5721 F_Node := Freeze_Node (Full_View (E));
5722
5723 if Present (F_Node) then
5724 Set_Freeze_Node (E, F_Node);
5725 Set_Entity (F_Node, E);
5726
5727 else
5728 -- {Incomplete,Private}_Subtypes with Full_Views
5729 -- constrained by discriminants.
5730
5731 Set_Has_Delayed_Freeze (E, False);
5732 Set_Freeze_Node (E, Empty);
5733 end if;
5734 end if;
5735 end;
5736 end if;
5737
5738 Check_Debug_Info_Needed (E);
5739
5740 -- AI-117 requires that the convention of a partial view be the
5741 -- same as the convention of the full view. Note that this is a
5742 -- recognized breach of privacy, but it's essential for logical
5743 -- consistency of representation, and the lack of a rule in
5744 -- RM95 was an oversight.
5745
5746 Set_Convention (E, Convention (Full_View (E)));
5747
5748 Set_Size_Known_At_Compile_Time (E,
5749 Size_Known_At_Compile_Time (Full_View (E)));
5750
5751 -- Size information is copied from the full view to the
5752 -- incomplete or private view for consistency.
5753
5754 -- We skip this is the full view is not a type. This is very
5755 -- strange of course, and can only happen as a result of
5756 -- certain illegalities, such as a premature attempt to derive
5757 -- from an incomplete type.
5758
5759 if Is_Type (Full_View (E)) then
5760 Set_Size_Info (E, Full_View (E));
5761 Set_RM_Size (E, RM_Size (Full_View (E)));
5762 end if;
5763
5764 Ghost_Mode := Save_Ghost_Mode;
5765 return Result;
5766
5767 -- Case of underlying full view present
5768
5769 elsif Is_Private_Type (E)
5770 and then Present (Underlying_Full_View (E))
5771 then
5772 if not Is_Frozen (Underlying_Full_View (E)) then
5773 Freeze_And_Append (Underlying_Full_View (E), N, Result);
5774 end if;
5775
5776 -- Patch the pointers so that the freeze node will elaborate
5777 -- both views in the back end.
5778
5779 if Has_Delayed_Freeze (E) then
5780 F_Node := Freeze_Node (Underlying_Full_View (E));
5781
5782 if Present (F_Node) then
5783 Set_Freeze_Node (E, F_Node);
5784 Set_Entity (F_Node, E);
5785
5786 else
5787 Set_Has_Delayed_Freeze (E, False);
5788 Set_Freeze_Node (E, Empty);
5789 end if;
5790 end if;
5791
5792 Check_Debug_Info_Needed (E);
5793
5794 Ghost_Mode := Save_Ghost_Mode;
5795 return Result;
5796
5797 -- Case of no full view present. If entity is derived or subtype,
5798 -- it is safe to freeze, correctness depends on the frozen status
5799 -- of parent. Otherwise it is either premature usage, or a Taft
5800 -- amendment type, so diagnosis is at the point of use and the
5801 -- type might be frozen later.
5802
5803 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
5804 null;
5805
5806 else
5807 Set_Is_Frozen (E, False);
5808 Ghost_Mode := Save_Ghost_Mode;
5809 return No_List;
5810 end if;
5811
5812 -- For access subprogram, freeze types of all formals, the return
5813 -- type was already frozen, since it is the Etype of the function.
5814 -- Formal types can be tagged Taft amendment types, but otherwise
5815 -- they cannot be incomplete.
5816
5817 elsif Ekind (E) = E_Subprogram_Type then
5818 Formal := First_Formal (E);
5819 while Present (Formal) loop
5820 if Ekind (Etype (Formal)) = E_Incomplete_Type
5821 and then No (Full_View (Etype (Formal)))
5822 then
5823 if Is_Tagged_Type (Etype (Formal)) then
5824 null;
5825
5826 -- AI05-151: Incomplete types are allowed in access to
5827 -- subprogram specifications.
5828
5829 elsif Ada_Version < Ada_2012 then
5830 Error_Msg_NE
5831 ("invalid use of incomplete type&", E, Etype (Formal));
5832 end if;
5833 end if;
5834
5835 Freeze_And_Append (Etype (Formal), N, Result);
5836 Next_Formal (Formal);
5837 end loop;
5838
5839 Freeze_Subprogram (E);
5840
5841 -- For access to a protected subprogram, freeze the equivalent type
5842 -- (however this is not set if we are not generating code or if this
5843 -- is an anonymous type used just for resolution).
5844
5845 elsif Is_Access_Protected_Subprogram_Type (E) then
5846 if Present (Equivalent_Type (E)) then
5847 Freeze_And_Append (Equivalent_Type (E), N, Result);
5848 end if;
5849 end if;
5850
5851 -- Generic types are never seen by the back-end, and are also not
5852 -- processed by the expander (since the expander is turned off for
5853 -- generic processing), so we never need freeze nodes for them.
5854
5855 if Is_Generic_Type (E) then
5856 Ghost_Mode := Save_Ghost_Mode;
5857 return Result;
5858 end if;
5859
5860 -- Some special processing for non-generic types to complete
5861 -- representation details not known till the freeze point.
5862
5863 if Is_Fixed_Point_Type (E) then
5864 Freeze_Fixed_Point_Type (E);
5865
5866 -- Some error checks required for ordinary fixed-point type. Defer
5867 -- these till the freeze-point since we need the small and range
5868 -- values. We only do these checks for base types
5869
5870 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
5871 if Small_Value (E) < Ureal_2_M_80 then
5872 Error_Msg_Name_1 := Name_Small;
5873 Error_Msg_N
5874 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
5875
5876 elsif Small_Value (E) > Ureal_2_80 then
5877 Error_Msg_Name_1 := Name_Small;
5878 Error_Msg_N
5879 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
5880 end if;
5881
5882 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
5883 Error_Msg_Name_1 := Name_First;
5884 Error_Msg_N
5885 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
5886 end if;
5887
5888 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
5889 Error_Msg_Name_1 := Name_Last;
5890 Error_Msg_N
5891 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
5892 end if;
5893 end if;
5894
5895 elsif Is_Enumeration_Type (E) then
5896 Freeze_Enumeration_Type (E);
5897
5898 elsif Is_Integer_Type (E) then
5899 Adjust_Esize_For_Alignment (E);
5900
5901 if Is_Modular_Integer_Type (E)
5902 and then Warn_On_Suspicious_Modulus_Value
5903 then
5904 Check_Suspicious_Modulus (E);
5905 end if;
5906
5907 -- The pool applies to named and anonymous access types, but not
5908 -- to subprogram and to internal types generated for 'Access
5909 -- references.
5910
5911 elsif Is_Access_Type (E)
5912 and then not Is_Access_Subprogram_Type (E)
5913 and then Ekind (E) /= E_Access_Attribute_Type
5914 then
5915 -- If a pragma Default_Storage_Pool applies, and this type has no
5916 -- Storage_Pool or Storage_Size clause (which must have occurred
5917 -- before the freezing point), then use the default. This applies
5918 -- only to base types.
5919
5920 -- None of this applies to access to subprograms, for which there
5921 -- are clearly no pools.
5922
5923 if Present (Default_Pool)
5924 and then Is_Base_Type (E)
5925 and then not Has_Storage_Size_Clause (E)
5926 and then No (Associated_Storage_Pool (E))
5927 then
5928 -- Case of pragma Default_Storage_Pool (null)
5929
5930 if Nkind (Default_Pool) = N_Null then
5931 Set_No_Pool_Assigned (E);
5932
5933 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
5934
5935 else
5936 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
5937 end if;
5938 end if;
5939
5940 -- Check restriction for standard storage pool
5941
5942 if No (Associated_Storage_Pool (E)) then
5943 Check_Restriction (No_Standard_Storage_Pools, E);
5944 end if;
5945
5946 -- Deal with error message for pure access type. This is not an
5947 -- error in Ada 2005 if there is no pool (see AI-366).
5948
5949 if Is_Pure_Unit_Access_Type (E)
5950 and then (Ada_Version < Ada_2005
5951 or else not No_Pool_Assigned (E))
5952 and then not Is_Generic_Unit (Scope (E))
5953 then
5954 Error_Msg_N ("named access type not allowed in pure unit", E);
5955
5956 if Ada_Version >= Ada_2005 then
5957 Error_Msg_N
5958 ("\would be legal if Storage_Size of 0 given??", E);
5959
5960 elsif No_Pool_Assigned (E) then
5961 Error_Msg_N
5962 ("\would be legal in Ada 2005??", E);
5963
5964 else
5965 Error_Msg_N
5966 ("\would be legal in Ada 2005 if "
5967 & "Storage_Size of 0 given??", E);
5968 end if;
5969 end if;
5970 end if;
5971
5972 -- Case of composite types
5973
5974 if Is_Composite_Type (E) then
5975
5976 -- AI-117 requires that all new primitives of a tagged type must
5977 -- inherit the convention of the full view of the type. Inherited
5978 -- and overriding operations are defined to inherit the convention
5979 -- of their parent or overridden subprogram (also specified in
5980 -- AI-117), which will have occurred earlier (in Derive_Subprogram
5981 -- and New_Overloaded_Entity). Here we set the convention of
5982 -- primitives that are still convention Ada, which will ensure
5983 -- that any new primitives inherit the type's convention. Class-
5984 -- wide types can have a foreign convention inherited from their
5985 -- specific type, but are excluded from this since they don't have
5986 -- any associated primitives.
5987
5988 if Is_Tagged_Type (E)
5989 and then not Is_Class_Wide_Type (E)
5990 and then Convention (E) /= Convention_Ada
5991 then
5992 declare
5993 Prim_List : constant Elist_Id := Primitive_Operations (E);
5994 Prim : Elmt_Id;
5995
5996 begin
5997 Prim := First_Elmt (Prim_List);
5998 while Present (Prim) loop
5999 if Convention (Node (Prim)) = Convention_Ada then
6000 Set_Convention (Node (Prim), Convention (E));
6001 end if;
6002
6003 Next_Elmt (Prim);
6004 end loop;
6005 end;
6006 end if;
6007
6008 -- If the type is a simple storage pool type, then this is where
6009 -- we attempt to locate and validate its Allocate, Deallocate, and
6010 -- Storage_Size operations (the first is required, and the latter
6011 -- two are optional). We also verify that the full type for a
6012 -- private type is allowed to be a simple storage pool type.
6013
6014 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
6015 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
6016 then
6017 -- If the type is marked Has_Private_Declaration, then this is
6018 -- a full type for a private type that was specified with the
6019 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
6020 -- pragma is allowed for the full type (for example, it can't
6021 -- be an array type, or a nonlimited record type).
6022
6023 if Has_Private_Declaration (E) then
6024 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
6025 and then not Is_Private_Type (E)
6026 then
6027 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
6028 Error_Msg_N
6029 ("pragma% can only apply to full type that is an " &
6030 "explicitly limited type", E);
6031 end if;
6032 end if;
6033
6034 Validate_Simple_Pool_Ops : declare
6035 Pool_Type : Entity_Id renames E;
6036 Address_Type : constant Entity_Id := RTE (RE_Address);
6037 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
6038
6039 procedure Validate_Simple_Pool_Op_Formal
6040 (Pool_Op : Entity_Id;
6041 Pool_Op_Formal : in out Entity_Id;
6042 Expected_Mode : Formal_Kind;
6043 Expected_Type : Entity_Id;
6044 Formal_Name : String;
6045 OK_Formal : in out Boolean);
6046 -- Validate one formal Pool_Op_Formal of the candidate pool
6047 -- operation Pool_Op. The formal must be of Expected_Type
6048 -- and have mode Expected_Mode. OK_Formal will be set to
6049 -- False if the formal doesn't match. If OK_Formal is False
6050 -- on entry, then the formal will effectively be ignored
6051 -- (because validation of the pool op has already failed).
6052 -- Upon return, Pool_Op_Formal will be updated to the next
6053 -- formal, if any.
6054
6055 procedure Validate_Simple_Pool_Operation
6056 (Op_Name : Name_Id);
6057 -- Search for and validate a simple pool operation with the
6058 -- name Op_Name. If the name is Allocate, then there must be
6059 -- exactly one such primitive operation for the simple pool
6060 -- type. If the name is Deallocate or Storage_Size, then
6061 -- there can be at most one such primitive operation. The
6062 -- profile of the located primitive must conform to what
6063 -- is expected for each operation.
6064
6065 ------------------------------------
6066 -- Validate_Simple_Pool_Op_Formal --
6067 ------------------------------------
6068
6069 procedure Validate_Simple_Pool_Op_Formal
6070 (Pool_Op : Entity_Id;
6071 Pool_Op_Formal : in out Entity_Id;
6072 Expected_Mode : Formal_Kind;
6073 Expected_Type : Entity_Id;
6074 Formal_Name : String;
6075 OK_Formal : in out Boolean)
6076 is
6077 begin
6078 -- If OK_Formal is False on entry, then simply ignore
6079 -- the formal, because an earlier formal has already
6080 -- been flagged.
6081
6082 if not OK_Formal then
6083 return;
6084
6085 -- If no formal is passed in, then issue an error for a
6086 -- missing formal.
6087
6088 elsif not Present (Pool_Op_Formal) then
6089 Error_Msg_NE
6090 ("simple storage pool op missing formal " &
6091 Formal_Name & " of type&", Pool_Op, Expected_Type);
6092 OK_Formal := False;
6093
6094 return;
6095 end if;
6096
6097 if Etype (Pool_Op_Formal) /= Expected_Type then
6098
6099 -- If the pool type was expected for this formal, then
6100 -- this will not be considered a candidate operation
6101 -- for the simple pool, so we unset OK_Formal so that
6102 -- the op and any later formals will be ignored.
6103
6104 if Expected_Type = Pool_Type then
6105 OK_Formal := False;
6106
6107 return;
6108
6109 else
6110 Error_Msg_NE
6111 ("wrong type for formal " & Formal_Name &
6112 " of simple storage pool op; expected type&",
6113 Pool_Op_Formal, Expected_Type);
6114 end if;
6115 end if;
6116
6117 -- Issue error if formal's mode is not the expected one
6118
6119 if Ekind (Pool_Op_Formal) /= Expected_Mode then
6120 Error_Msg_N
6121 ("wrong mode for formal of simple storage pool op",
6122 Pool_Op_Formal);
6123 end if;
6124
6125 -- Advance to the next formal
6126
6127 Next_Formal (Pool_Op_Formal);
6128 end Validate_Simple_Pool_Op_Formal;
6129
6130 ------------------------------------
6131 -- Validate_Simple_Pool_Operation --
6132 ------------------------------------
6133
6134 procedure Validate_Simple_Pool_Operation
6135 (Op_Name : Name_Id)
6136 is
6137 Op : Entity_Id;
6138 Found_Op : Entity_Id := Empty;
6139 Formal : Entity_Id;
6140 Is_OK : Boolean;
6141
6142 begin
6143 pragma Assert
6144 (Nam_In (Op_Name, Name_Allocate,
6145 Name_Deallocate,
6146 Name_Storage_Size));
6147
6148 Error_Msg_Name_1 := Op_Name;
6149
6150 -- For each homonym declared immediately in the scope
6151 -- of the simple storage pool type, determine whether
6152 -- the homonym is an operation of the pool type, and,
6153 -- if so, check that its profile is as expected for
6154 -- a simple pool operation of that name.
6155
6156 Op := Get_Name_Entity_Id (Op_Name);
6157 while Present (Op) loop
6158 if Ekind_In (Op, E_Function, E_Procedure)
6159 and then Scope (Op) = Current_Scope
6160 then
6161 Formal := First_Entity (Op);
6162
6163 Is_OK := True;
6164
6165 -- The first parameter must be of the pool type
6166 -- in order for the operation to qualify.
6167
6168 if Op_Name = Name_Storage_Size then
6169 Validate_Simple_Pool_Op_Formal
6170 (Op, Formal, E_In_Parameter, Pool_Type,
6171 "Pool", Is_OK);
6172 else
6173 Validate_Simple_Pool_Op_Formal
6174 (Op, Formal, E_In_Out_Parameter, Pool_Type,
6175 "Pool", Is_OK);
6176 end if;
6177
6178 -- If another operation with this name has already
6179 -- been located for the type, then flag an error,
6180 -- since we only allow the type to have a single
6181 -- such primitive.
6182
6183 if Present (Found_Op) and then Is_OK then
6184 Error_Msg_NE
6185 ("only one % operation allowed for " &
6186 "simple storage pool type&", Op, Pool_Type);
6187 end if;
6188
6189 -- In the case of Allocate and Deallocate, a formal
6190 -- of type System.Address is required.
6191
6192 if Op_Name = Name_Allocate then
6193 Validate_Simple_Pool_Op_Formal
6194 (Op, Formal, E_Out_Parameter,
6195 Address_Type, "Storage_Address", Is_OK);
6196
6197 elsif Op_Name = Name_Deallocate then
6198 Validate_Simple_Pool_Op_Formal
6199 (Op, Formal, E_In_Parameter,
6200 Address_Type, "Storage_Address", Is_OK);
6201 end if;
6202
6203 -- In the case of Allocate and Deallocate, formals
6204 -- of type Storage_Count are required as the third
6205 -- and fourth parameters.
6206
6207 if Op_Name /= Name_Storage_Size then
6208 Validate_Simple_Pool_Op_Formal
6209 (Op, Formal, E_In_Parameter,
6210 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
6211 Validate_Simple_Pool_Op_Formal
6212 (Op, Formal, E_In_Parameter,
6213 Stg_Cnt_Type, "Alignment", Is_OK);
6214 end if;
6215
6216 -- If no mismatched formals have been found (Is_OK)
6217 -- and no excess formals are present, then this
6218 -- operation has been validated, so record it.
6219
6220 if not Present (Formal) and then Is_OK then
6221 Found_Op := Op;
6222 end if;
6223 end if;
6224
6225 Op := Homonym (Op);
6226 end loop;
6227
6228 -- There must be a valid Allocate operation for the type,
6229 -- so issue an error if none was found.
6230
6231 if Op_Name = Name_Allocate
6232 and then not Present (Found_Op)
6233 then
6234 Error_Msg_N ("missing % operation for simple " &
6235 "storage pool type", Pool_Type);
6236
6237 elsif Present (Found_Op) then
6238
6239 -- Simple pool operations can't be abstract
6240
6241 if Is_Abstract_Subprogram (Found_Op) then
6242 Error_Msg_N
6243 ("simple storage pool operation must not be " &
6244 "abstract", Found_Op);
6245 end if;
6246
6247 -- The Storage_Size operation must be a function with
6248 -- Storage_Count as its result type.
6249
6250 if Op_Name = Name_Storage_Size then
6251 if Ekind (Found_Op) = E_Procedure then
6252 Error_Msg_N
6253 ("% operation must be a function", Found_Op);
6254
6255 elsif Etype (Found_Op) /= Stg_Cnt_Type then
6256 Error_Msg_NE
6257 ("wrong result type for%, expected type&",
6258 Found_Op, Stg_Cnt_Type);
6259 end if;
6260
6261 -- Allocate and Deallocate must be procedures
6262
6263 elsif Ekind (Found_Op) = E_Function then
6264 Error_Msg_N
6265 ("% operation must be a procedure", Found_Op);
6266 end if;
6267 end if;
6268 end Validate_Simple_Pool_Operation;
6269
6270 -- Start of processing for Validate_Simple_Pool_Ops
6271
6272 begin
6273 Validate_Simple_Pool_Operation (Name_Allocate);
6274 Validate_Simple_Pool_Operation (Name_Deallocate);
6275 Validate_Simple_Pool_Operation (Name_Storage_Size);
6276 end Validate_Simple_Pool_Ops;
6277 end if;
6278 end if;
6279
6280 -- Now that all types from which E may depend are frozen, see if the
6281 -- size is known at compile time, if it must be unsigned, or if
6282 -- strict alignment is required
6283
6284 Check_Compile_Time_Size (E);
6285 Check_Unsigned_Type (E);
6286
6287 if Base_Type (E) = E then
6288 Check_Strict_Alignment (E);
6289 end if;
6290
6291 -- Do not allow a size clause for a type which does not have a size
6292 -- that is known at compile time
6293
6294 if Has_Size_Clause (E)
6295 and then not Size_Known_At_Compile_Time (E)
6296 then
6297 -- Suppress this message if errors posted on E, even if we are
6298 -- in all errors mode, since this is often a junk message
6299
6300 if not Error_Posted (E) then
6301 Error_Msg_N
6302 ("size clause not allowed for variable length type",
6303 Size_Clause (E));
6304 end if;
6305 end if;
6306
6307 -- Now we set/verify the representation information, in particular
6308 -- the size and alignment values. This processing is not required for
6309 -- generic types, since generic types do not play any part in code
6310 -- generation, and so the size and alignment values for such types
6311 -- are irrelevant. Ditto for types declared within a generic unit,
6312 -- which may have components that depend on generic parameters, and
6313 -- that will be recreated in an instance.
6314
6315 if Inside_A_Generic then
6316 null;
6317
6318 -- Otherwise we call the layout procedure
6319
6320 else
6321 Layout_Type (E);
6322 end if;
6323
6324 -- If this is an access to subprogram whose designated type is itself
6325 -- a subprogram type, the return type of this anonymous subprogram
6326 -- type must be decorated as well.
6327
6328 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
6329 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
6330 then
6331 Layout_Type (Etype (Designated_Type (E)));
6332 end if;
6333
6334 -- If the type has a Defaut_Value/Default_Component_Value aspect,
6335 -- this is where we analye the expression (after the type is frozen,
6336 -- since in the case of Default_Value, we are analyzing with the
6337 -- type itself, and we treat Default_Component_Value similarly for
6338 -- the sake of uniformity).
6339
6340 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
6341 declare
6342 Nam : Name_Id;
6343 Exp : Node_Id;
6344 Typ : Entity_Id;
6345
6346 begin
6347 if Is_Scalar_Type (E) then
6348 Nam := Name_Default_Value;
6349 Typ := E;
6350 Exp := Default_Aspect_Value (Typ);
6351 else
6352 Nam := Name_Default_Component_Value;
6353 Typ := Component_Type (E);
6354 Exp := Default_Aspect_Component_Value (E);
6355 end if;
6356
6357 Analyze_And_Resolve (Exp, Typ);
6358
6359 if Etype (Exp) /= Any_Type then
6360 if not Is_OK_Static_Expression (Exp) then
6361 Error_Msg_Name_1 := Nam;
6362 Flag_Non_Static_Expr
6363 ("aspect% requires static expression", Exp);
6364 end if;
6365 end if;
6366 end;
6367 end if;
6368
6369 -- End of freeze processing for type entities
6370 end if;
6371
6372 -- Here is where we logically freeze the current entity. If it has a
6373 -- freeze node, then this is the point at which the freeze node is
6374 -- linked into the result list.
6375
6376 if Has_Delayed_Freeze (E) then
6377
6378 -- If a freeze node is already allocated, use it, otherwise allocate
6379 -- a new one. The preallocation happens in the case of anonymous base
6380 -- types, where we preallocate so that we can set First_Subtype_Link.
6381 -- Note that we reset the Sloc to the current freeze location.
6382
6383 if Present (Freeze_Node (E)) then
6384 F_Node := Freeze_Node (E);
6385 Set_Sloc (F_Node, Loc);
6386
6387 else
6388 F_Node := New_Freeze_Node;
6389 Set_Freeze_Node (E, F_Node);
6390 Set_Access_Types_To_Process (F_Node, No_Elist);
6391 Set_TSS_Elist (F_Node, No_Elist);
6392 Set_Actions (F_Node, No_List);
6393 end if;
6394
6395 Set_Entity (F_Node, E);
6396 Add_To_Result (F_Node);
6397
6398 -- A final pass over record types with discriminants. If the type
6399 -- has an incomplete declaration, there may be constrained access
6400 -- subtypes declared elsewhere, which do not depend on the discrimi-
6401 -- nants of the type, and which are used as component types (i.e.
6402 -- the full view is a recursive type). The designated types of these
6403 -- subtypes can only be elaborated after the type itself, and they
6404 -- need an itype reference.
6405
6406 if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then
6407 declare
6408 Comp : Entity_Id;
6409 IR : Node_Id;
6410 Typ : Entity_Id;
6411
6412 begin
6413 Comp := First_Component (E);
6414 while Present (Comp) loop
6415 Typ := Etype (Comp);
6416
6417 if Ekind (Comp) = E_Component
6418 and then Is_Access_Type (Typ)
6419 and then Scope (Typ) /= E
6420 and then Base_Type (Designated_Type (Typ)) = E
6421 and then Is_Itype (Designated_Type (Typ))
6422 then
6423 IR := Make_Itype_Reference (Sloc (Comp));
6424 Set_Itype (IR, Designated_Type (Typ));
6425 Append (IR, Result);
6426 end if;
6427
6428 Next_Component (Comp);
6429 end loop;
6430 end;
6431 end if;
6432 end if;
6433
6434 -- When a type is frozen, the first subtype of the type is frozen as
6435 -- well (RM 13.14(15)). This has to be done after freezing the type,
6436 -- since obviously the first subtype depends on its own base type.
6437
6438 if Is_Type (E) then
6439 Freeze_And_Append (First_Subtype (E), N, Result);
6440
6441 -- If we just froze a tagged non-class wide record, then freeze the
6442 -- corresponding class-wide type. This must be done after the tagged
6443 -- type itself is frozen, because the class-wide type refers to the
6444 -- tagged type which generates the class.
6445
6446 if Is_Tagged_Type (E)
6447 and then not Is_Class_Wide_Type (E)
6448 and then Present (Class_Wide_Type (E))
6449 then
6450 Freeze_And_Append (Class_Wide_Type (E), N, Result);
6451 end if;
6452 end if;
6453
6454 Check_Debug_Info_Needed (E);
6455
6456 -- Special handling for subprograms
6457
6458 if Is_Subprogram (E) then
6459
6460 -- If subprogram has address clause then reset Is_Public flag, since
6461 -- we do not want the backend to generate external references.
6462
6463 if Present (Address_Clause (E))
6464 and then not Is_Library_Level_Entity (E)
6465 then
6466 Set_Is_Public (E, False);
6467 end if;
6468 end if;
6469
6470 Ghost_Mode := Save_Ghost_Mode;
6471 return Result;
6472 end Freeze_Entity;
6473
6474 -----------------------------
6475 -- Freeze_Enumeration_Type --
6476 -----------------------------
6477
6478 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6479 begin
6480 -- By default, if no size clause is present, an enumeration type with
6481 -- Convention C is assumed to interface to a C enum, and has integer
6482 -- size. This applies to types. For subtypes, verify that its base
6483 -- type has no size clause either. Treat other foreign conventions
6484 -- in the same way, and also make sure alignment is set right.
6485
6486 if Has_Foreign_Convention (Typ)
6487 and then not Has_Size_Clause (Typ)
6488 and then not Has_Size_Clause (Base_Type (Typ))
6489 and then Esize (Typ) < Standard_Integer_Size
6490
6491 -- Don't do this if Short_Enums on target
6492
6493 and then not Target_Short_Enums
6494 then
6495 Init_Esize (Typ, Standard_Integer_Size);
6496 Set_Alignment (Typ, Alignment (Standard_Integer));
6497
6498 -- Normal Ada case or size clause present or not Long_C_Enums on target
6499
6500 else
6501 -- If the enumeration type interfaces to C, and it has a size clause
6502 -- that specifies less than int size, it warrants a warning. The
6503 -- user may intend the C type to be an enum or a char, so this is
6504 -- not by itself an error that the Ada compiler can detect, but it
6505 -- it is a worth a heads-up. For Boolean and Character types we
6506 -- assume that the programmer has the proper C type in mind.
6507
6508 if Convention (Typ) = Convention_C
6509 and then Has_Size_Clause (Typ)
6510 and then Esize (Typ) /= Esize (Standard_Integer)
6511 and then not Is_Boolean_Type (Typ)
6512 and then not Is_Character_Type (Typ)
6513
6514 -- Don't do this if Short_Enums on target
6515
6516 and then not Target_Short_Enums
6517 then
6518 Error_Msg_N
6519 ("C enum types have the size of a C int??", Size_Clause (Typ));
6520 end if;
6521
6522 Adjust_Esize_For_Alignment (Typ);
6523 end if;
6524 end Freeze_Enumeration_Type;
6525
6526 -----------------------
6527 -- Freeze_Expression --
6528 -----------------------
6529
6530 procedure Freeze_Expression (N : Node_Id) is
6531 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6532 Typ : Entity_Id;
6533 Nam : Entity_Id;
6534 Desig_Typ : Entity_Id;
6535 P : Node_Id;
6536 Parent_P : Node_Id;
6537
6538 Freeze_Outside : Boolean := False;
6539 -- This flag is set true if the entity must be frozen outside the
6540 -- current subprogram. This happens in the case of expander generated
6541 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6542 -- not freeze all entities like other bodies, but which nevertheless
6543 -- may reference entities that have to be frozen before the body and
6544 -- obviously cannot be frozen inside the body.
6545
6546 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6547 -- If the expression is an array aggregate, the type of the component
6548 -- expressions is also frozen. If the component type is an access type
6549 -- and the expressions include allocators, the designed type is frozen
6550 -- as well.
6551
6552 function In_Expanded_Body (N : Node_Id) return Boolean;
6553 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6554 -- it is the handled statement sequence of an expander-generated
6555 -- subprogram (init proc, stream subprogram, or renaming as body).
6556 -- If so, this is not a freezing context.
6557
6558 -----------------------------------------
6559 -- Find_Aggregate_Component_Desig_Type --
6560 -----------------------------------------
6561
6562 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6563 Assoc : Node_Id;
6564 Exp : Node_Id;
6565
6566 begin
6567 if Present (Expressions (N)) then
6568 Exp := First (Expressions (N));
6569 while Present (Exp) loop
6570 if Nkind (Exp) = N_Allocator then
6571 return Designated_Type (Component_Type (Etype (N)));
6572 end if;
6573
6574 Next (Exp);
6575 end loop;
6576 end if;
6577
6578 if Present (Component_Associations (N)) then
6579 Assoc := First (Component_Associations (N));
6580 while Present (Assoc) loop
6581 if Nkind (Expression (Assoc)) = N_Allocator then
6582 return Designated_Type (Component_Type (Etype (N)));
6583 end if;
6584
6585 Next (Assoc);
6586 end loop;
6587 end if;
6588
6589 return Empty;
6590 end Find_Aggregate_Component_Desig_Type;
6591
6592 ----------------------
6593 -- In_Expanded_Body --
6594 ----------------------
6595
6596 function In_Expanded_Body (N : Node_Id) return Boolean is
6597 P : Node_Id;
6598 Id : Entity_Id;
6599
6600 begin
6601 if Nkind (N) = N_Subprogram_Body then
6602 P := N;
6603 else
6604 P := Parent (N);
6605 end if;
6606
6607 if Nkind (P) /= N_Subprogram_Body then
6608 return False;
6609
6610 else
6611 Id := Defining_Unit_Name (Specification (P));
6612
6613 -- The following are expander-created bodies, or bodies that
6614 -- are not freeze points.
6615
6616 if Nkind (Id) = N_Defining_Identifier
6617 and then (Is_Init_Proc (Id)
6618 or else Is_TSS (Id, TSS_Stream_Input)
6619 or else Is_TSS (Id, TSS_Stream_Output)
6620 or else Is_TSS (Id, TSS_Stream_Read)
6621 or else Is_TSS (Id, TSS_Stream_Write)
6622 or else Nkind_In (Original_Node (P),
6623 N_Subprogram_Renaming_Declaration,
6624 N_Expression_Function))
6625 then
6626 return True;
6627 else
6628 return False;
6629 end if;
6630 end if;
6631 end In_Expanded_Body;
6632
6633 -- Start of processing for Freeze_Expression
6634
6635 begin
6636 -- Immediate return if freezing is inhibited. This flag is set by the
6637 -- analyzer to stop freezing on generated expressions that would cause
6638 -- freezing if they were in the source program, but which are not
6639 -- supposed to freeze, since they are created.
6640
6641 if Must_Not_Freeze (N) then
6642 return;
6643 end if;
6644
6645 -- If expression is non-static, then it does not freeze in a default
6646 -- expression, see section "Handling of Default Expressions" in the
6647 -- spec of package Sem for further details. Note that we have to make
6648 -- sure that we actually have a real expression (if we have a subtype
6649 -- indication, we can't test Is_OK_Static_Expression). However, we
6650 -- exclude the case of the prefix of an attribute of a static scalar
6651 -- subtype from this early return, because static subtype attributes
6652 -- should always cause freezing, even in default expressions, but
6653 -- the attribute may not have been marked as static yet (because in
6654 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
6655 -- Freeze_Expression on the prefix).
6656
6657 if In_Spec_Exp
6658 and then Nkind (N) in N_Subexpr
6659 and then not Is_OK_Static_Expression (N)
6660 and then (Nkind (Parent (N)) /= N_Attribute_Reference
6661 or else not (Is_Entity_Name (N)
6662 and then Is_Type (Entity (N))
6663 and then Is_OK_Static_Subtype (Entity (N))))
6664 then
6665 return;
6666 end if;
6667
6668 -- Freeze type of expression if not frozen already
6669
6670 Typ := Empty;
6671
6672 if Nkind (N) in N_Has_Etype then
6673 if not Is_Frozen (Etype (N)) then
6674 Typ := Etype (N);
6675
6676 -- Base type may be an derived numeric type that is frozen at
6677 -- the point of declaration, but first_subtype is still unfrozen.
6678
6679 elsif not Is_Frozen (First_Subtype (Etype (N))) then
6680 Typ := First_Subtype (Etype (N));
6681 end if;
6682 end if;
6683
6684 -- For entity name, freeze entity if not frozen already. A special
6685 -- exception occurs for an identifier that did not come from source.
6686 -- We don't let such identifiers freeze a non-internal entity, i.e.
6687 -- an entity that did come from source, since such an identifier was
6688 -- generated by the expander, and cannot have any semantic effect on
6689 -- the freezing semantics. For example, this stops the parameter of
6690 -- an initialization procedure from freezing the variable.
6691
6692 if Is_Entity_Name (N)
6693 and then not Is_Frozen (Entity (N))
6694 and then (Nkind (N) /= N_Identifier
6695 or else Comes_From_Source (N)
6696 or else not Comes_From_Source (Entity (N)))
6697 then
6698 Nam := Entity (N);
6699
6700 if Present (Nam) and then Ekind (Nam) = E_Function then
6701 Check_Expression_Function (N, Nam);
6702 end if;
6703
6704 else
6705 Nam := Empty;
6706 end if;
6707
6708 -- For an allocator freeze designated type if not frozen already
6709
6710 -- For an aggregate whose component type is an access type, freeze the
6711 -- designated type now, so that its freeze does not appear within the
6712 -- loop that might be created in the expansion of the aggregate. If the
6713 -- designated type is a private type without full view, the expression
6714 -- cannot contain an allocator, so the type is not frozen.
6715
6716 -- For a function, we freeze the entity when the subprogram declaration
6717 -- is frozen, but a function call may appear in an initialization proc.
6718 -- before the declaration is frozen. We need to generate the extra
6719 -- formals, if any, to ensure that the expansion of the call includes
6720 -- the proper actuals. This only applies to Ada subprograms, not to
6721 -- imported ones.
6722
6723 Desig_Typ := Empty;
6724
6725 case Nkind (N) is
6726 when N_Allocator =>
6727 Desig_Typ := Designated_Type (Etype (N));
6728
6729 when N_Aggregate =>
6730 if Is_Array_Type (Etype (N))
6731 and then Is_Access_Type (Component_Type (Etype (N)))
6732 then
6733
6734 -- Check whether aggregate includes allocators.
6735
6736 Desig_Typ := Find_Aggregate_Component_Desig_Type;
6737 end if;
6738
6739 when N_Selected_Component |
6740 N_Indexed_Component |
6741 N_Slice =>
6742
6743 if Is_Access_Type (Etype (Prefix (N))) then
6744 Desig_Typ := Designated_Type (Etype (Prefix (N)));
6745 end if;
6746
6747 when N_Identifier =>
6748 if Present (Nam)
6749 and then Ekind (Nam) = E_Function
6750 and then Nkind (Parent (N)) = N_Function_Call
6751 and then Convention (Nam) = Convention_Ada
6752 then
6753 Create_Extra_Formals (Nam);
6754 end if;
6755
6756 when others =>
6757 null;
6758 end case;
6759
6760 if Desig_Typ /= Empty
6761 and then (Is_Frozen (Desig_Typ)
6762 or else (not Is_Fully_Defined (Desig_Typ)))
6763 then
6764 Desig_Typ := Empty;
6765 end if;
6766
6767 -- All done if nothing needs freezing
6768
6769 if No (Typ)
6770 and then No (Nam)
6771 and then No (Desig_Typ)
6772 then
6773 return;
6774 end if;
6775
6776 -- Examine the enclosing context by climbing the parent chain. The
6777 -- traversal serves two purposes - to detect scenarios where freezeing
6778 -- is not needed and to find the proper insertion point for the freeze
6779 -- nodes. Although somewhat similar to Insert_Actions, this traversal
6780 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
6781 -- the tree may result in types being frozen too early.
6782
6783 P := N;
6784 loop
6785 Parent_P := Parent (P);
6786
6787 -- If we don't have a parent, then we are not in a well-formed tree.
6788 -- This is an unusual case, but there are some legitimate situations
6789 -- in which this occurs, notably when the expressions in the range of
6790 -- a type declaration are resolved. We simply ignore the freeze
6791 -- request in this case. Is this right ???
6792
6793 if No (Parent_P) then
6794 return;
6795 end if;
6796
6797 -- See if we have got to an appropriate point in the tree
6798
6799 case Nkind (Parent_P) is
6800
6801 -- A special test for the exception of (RM 13.14(8)) for the case
6802 -- of per-object expressions (RM 3.8(18)) occurring in component
6803 -- definition or a discrete subtype definition. Note that we test
6804 -- for a component declaration which includes both cases we are
6805 -- interested in, and furthermore the tree does not have explicit
6806 -- nodes for either of these two constructs.
6807
6808 when N_Component_Declaration =>
6809
6810 -- The case we want to test for here is an identifier that is
6811 -- a per-object expression, this is either a discriminant that
6812 -- appears in a context other than the component declaration
6813 -- or it is a reference to the type of the enclosing construct.
6814
6815 -- For either of these cases, we skip the freezing
6816
6817 if not In_Spec_Expression
6818 and then Nkind (N) = N_Identifier
6819 and then (Present (Entity (N)))
6820 then
6821 -- We recognize the discriminant case by just looking for
6822 -- a reference to a discriminant. It can only be one for
6823 -- the enclosing construct. Skip freezing in this case.
6824
6825 if Ekind (Entity (N)) = E_Discriminant then
6826 return;
6827
6828 -- For the case of a reference to the enclosing record,
6829 -- (or task or protected type), we look for a type that
6830 -- matches the current scope.
6831
6832 elsif Entity (N) = Current_Scope then
6833 return;
6834 end if;
6835 end if;
6836
6837 -- If we have an enumeration literal that appears as the choice in
6838 -- the aggregate of an enumeration representation clause, then
6839 -- freezing does not occur (RM 13.14(10)).
6840
6841 when N_Enumeration_Representation_Clause =>
6842
6843 -- The case we are looking for is an enumeration literal
6844
6845 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
6846 and then Is_Enumeration_Type (Etype (N))
6847 then
6848 -- If enumeration literal appears directly as the choice,
6849 -- do not freeze (this is the normal non-overloaded case)
6850
6851 if Nkind (Parent (N)) = N_Component_Association
6852 and then First (Choices (Parent (N))) = N
6853 then
6854 return;
6855
6856 -- If enumeration literal appears as the name of function
6857 -- which is the choice, then also do not freeze. This
6858 -- happens in the overloaded literal case, where the
6859 -- enumeration literal is temporarily changed to a function
6860 -- call for overloading analysis purposes.
6861
6862 elsif Nkind (Parent (N)) = N_Function_Call
6863 and then
6864 Nkind (Parent (Parent (N))) = N_Component_Association
6865 and then
6866 First (Choices (Parent (Parent (N)))) = Parent (N)
6867 then
6868 return;
6869 end if;
6870 end if;
6871
6872 -- Normally if the parent is a handled sequence of statements,
6873 -- then the current node must be a statement, and that is an
6874 -- appropriate place to insert a freeze node.
6875
6876 when N_Handled_Sequence_Of_Statements =>
6877
6878 -- An exception occurs when the sequence of statements is for
6879 -- an expander generated body that did not do the usual freeze
6880 -- all operation. In this case we usually want to freeze
6881 -- outside this body, not inside it, and we skip past the
6882 -- subprogram body that we are inside.
6883
6884 if In_Expanded_Body (Parent_P) then
6885 declare
6886 Subp : constant Node_Id := Parent (Parent_P);
6887 Spec : Entity_Id;
6888
6889 begin
6890 -- Freeze the entity only when it is declared inside the
6891 -- body of the expander generated procedure. This case
6892 -- is recognized by the scope of the entity or its type,
6893 -- which is either the spec for some enclosing body, or
6894 -- (in the case of init_procs, for which there are no
6895 -- separate specs) the current scope.
6896
6897 if Nkind (Subp) = N_Subprogram_Body then
6898 Spec := Corresponding_Spec (Subp);
6899
6900 if (Present (Typ) and then Scope (Typ) = Spec)
6901 or else
6902 (Present (Nam) and then Scope (Nam) = Spec)
6903 then
6904 exit;
6905
6906 elsif Present (Typ)
6907 and then Scope (Typ) = Current_Scope
6908 and then Defining_Entity (Subp) = Current_Scope
6909 then
6910 exit;
6911 end if;
6912 end if;
6913
6914 -- An expression function may act as a completion of
6915 -- a function declaration. As such, it can reference
6916 -- entities declared between the two views:
6917
6918 -- Hidden []; -- 1
6919 -- function F return ...;
6920 -- private
6921 -- function Hidden return ...;
6922 -- function F return ... is (Hidden); -- 2
6923
6924 -- Refering to the example above, freezing the expression
6925 -- of F (2) would place Hidden's freeze node (1) in the
6926 -- wrong place. Avoid explicit freezing and let the usual
6927 -- scenarios do the job - for example, reaching the end
6928 -- of the private declarations, or a call to F.
6929
6930 if Nkind (Original_Node (Subp)) =
6931 N_Expression_Function
6932 then
6933 null;
6934
6935 -- Freeze outside the body
6936
6937 else
6938 Parent_P := Parent (Parent_P);
6939 Freeze_Outside := True;
6940 end if;
6941 end;
6942
6943 -- Here if normal case where we are in handled statement
6944 -- sequence and want to do the insertion right there.
6945
6946 else
6947 exit;
6948 end if;
6949
6950 -- If parent is a body or a spec or a block, then the current node
6951 -- is a statement or declaration and we can insert the freeze node
6952 -- before it.
6953
6954 when N_Block_Statement |
6955 N_Entry_Body |
6956 N_Package_Body |
6957 N_Package_Specification |
6958 N_Protected_Body |
6959 N_Subprogram_Body |
6960 N_Task_Body => exit;
6961
6962 -- The expander is allowed to define types in any statements list,
6963 -- so any of the following parent nodes also mark a freezing point
6964 -- if the actual node is in a list of statements or declarations.
6965
6966 when N_Abortable_Part |
6967 N_Accept_Alternative |
6968 N_And_Then |
6969 N_Case_Statement_Alternative |
6970 N_Compilation_Unit_Aux |
6971 N_Conditional_Entry_Call |
6972 N_Delay_Alternative |
6973 N_Elsif_Part |
6974 N_Entry_Call_Alternative |
6975 N_Exception_Handler |
6976 N_Extended_Return_Statement |
6977 N_Freeze_Entity |
6978 N_If_Statement |
6979 N_Or_Else |
6980 N_Selective_Accept |
6981 N_Triggering_Alternative =>
6982
6983 exit when Is_List_Member (P);
6984
6985 -- Freeze nodes produced by an expression coming from the Actions
6986 -- list of a N_Expression_With_Actions node must remain within the
6987 -- Actions list. Inserting the freeze nodes further up the tree
6988 -- may lead to use before declaration issues in the case of array
6989 -- types.
6990
6991 when N_Expression_With_Actions =>
6992 if Is_List_Member (P)
6993 and then List_Containing (P) = Actions (Parent_P)
6994 then
6995 exit;
6996 end if;
6997
6998 -- Note: N_Loop_Statement is a special case. A type that appears
6999 -- in the source can never be frozen in a loop (this occurs only
7000 -- because of a loop expanded by the expander), so we keep on
7001 -- going. Otherwise we terminate the search. Same is true of any
7002 -- entity which comes from source. (if they have predefined type,
7003 -- that type does not appear to come from source, but the entity
7004 -- should not be frozen here).
7005
7006 when N_Loop_Statement =>
7007 exit when not Comes_From_Source (Etype (N))
7008 and then (No (Nam) or else not Comes_From_Source (Nam));
7009
7010 -- For all other cases, keep looking at parents
7011
7012 when others =>
7013 null;
7014 end case;
7015
7016 -- We fall through the case if we did not yet find the proper
7017 -- place in the free for inserting the freeze node, so climb.
7018
7019 P := Parent_P;
7020 end loop;
7021
7022 -- If the expression appears in a record or an initialization procedure,
7023 -- the freeze nodes are collected and attached to the current scope, to
7024 -- be inserted and analyzed on exit from the scope, to insure that
7025 -- generated entities appear in the correct scope. If the expression is
7026 -- a default for a discriminant specification, the scope is still void.
7027 -- The expression can also appear in the discriminant part of a private
7028 -- or concurrent type.
7029
7030 -- If the expression appears in a constrained subcomponent of an
7031 -- enclosing record declaration, the freeze nodes must be attached to
7032 -- the outer record type so they can eventually be placed in the
7033 -- enclosing declaration list.
7034
7035 -- The other case requiring this special handling is if we are in a
7036 -- default expression, since in that case we are about to freeze a
7037 -- static type, and the freeze scope needs to be the outer scope, not
7038 -- the scope of the subprogram with the default parameter.
7039
7040 -- For default expressions and other spec expressions in generic units,
7041 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
7042 -- placing them at the proper place, after the generic unit.
7043
7044 if (In_Spec_Exp and not Inside_A_Generic)
7045 or else Freeze_Outside
7046 or else (Is_Type (Current_Scope)
7047 and then (not Is_Concurrent_Type (Current_Scope)
7048 or else not Has_Completion (Current_Scope)))
7049 or else Ekind (Current_Scope) = E_Void
7050 then
7051 declare
7052 N : constant Node_Id := Current_Scope;
7053 Freeze_Nodes : List_Id := No_List;
7054 Pos : Int := Scope_Stack.Last;
7055
7056 begin
7057 if Present (Desig_Typ) then
7058 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
7059 end if;
7060
7061 if Present (Typ) then
7062 Freeze_And_Append (Typ, N, Freeze_Nodes);
7063 end if;
7064
7065 if Present (Nam) then
7066 Freeze_And_Append (Nam, N, Freeze_Nodes);
7067 end if;
7068
7069 -- The current scope may be that of a constrained component of
7070 -- an enclosing record declaration, or of a loop of an enclosing
7071 -- quantified expression, which is above the current scope in the
7072 -- scope stack. Indeed in the context of a quantified expression,
7073 -- a scope is created and pushed above the current scope in order
7074 -- to emulate the loop-like behavior of the quantified expression.
7075 -- If the expression is within a top-level pragma, as for a pre-
7076 -- condition on a library-level subprogram, nothing to do.
7077
7078 if not Is_Compilation_Unit (Current_Scope)
7079 and then (Is_Record_Type (Scope (Current_Scope))
7080 or else Nkind (Parent (Current_Scope)) =
7081 N_Quantified_Expression)
7082 then
7083 Pos := Pos - 1;
7084 end if;
7085
7086 if Is_Non_Empty_List (Freeze_Nodes) then
7087 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
7088 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
7089 Freeze_Nodes;
7090 else
7091 Append_List (Freeze_Nodes,
7092 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
7093 end if;
7094 end if;
7095 end;
7096
7097 return;
7098 end if;
7099
7100 -- Now we have the right place to do the freezing. First, a special
7101 -- adjustment, if we are in spec-expression analysis mode, these freeze
7102 -- actions must not be thrown away (normally all inserted actions are
7103 -- thrown away in this mode. However, the freeze actions are from static
7104 -- expressions and one of the important reasons we are doing this
7105 -- special analysis is to get these freeze actions. Therefore we turn
7106 -- off the In_Spec_Expression mode to propagate these freeze actions.
7107 -- This also means they get properly analyzed and expanded.
7108
7109 In_Spec_Expression := False;
7110
7111 -- Freeze the designated type of an allocator (RM 13.14(13))
7112
7113 if Present (Desig_Typ) then
7114 Freeze_Before (P, Desig_Typ);
7115 end if;
7116
7117 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
7118 -- the enumeration representation clause exception in the loop above.
7119
7120 if Present (Typ) then
7121 Freeze_Before (P, Typ);
7122 end if;
7123
7124 -- Freeze name if one is present (RM 13.14(11))
7125
7126 if Present (Nam) then
7127 Freeze_Before (P, Nam);
7128 end if;
7129
7130 -- Restore In_Spec_Expression flag
7131
7132 In_Spec_Expression := In_Spec_Exp;
7133 end Freeze_Expression;
7134
7135 -----------------------------
7136 -- Freeze_Fixed_Point_Type --
7137 -----------------------------
7138
7139 -- Certain fixed-point types and subtypes, including implicit base types
7140 -- and declared first subtypes, have not yet set up a range. This is
7141 -- because the range cannot be set until the Small and Size values are
7142 -- known, and these are not known till the type is frozen.
7143
7144 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
7145 -- whose bounds are unanalyzed real literals. This routine will recognize
7146 -- this case, and transform this range node into a properly typed range
7147 -- with properly analyzed and resolved values.
7148
7149 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
7150 Rng : constant Node_Id := Scalar_Range (Typ);
7151 Lo : constant Node_Id := Low_Bound (Rng);
7152 Hi : constant Node_Id := High_Bound (Rng);
7153 Btyp : constant Entity_Id := Base_Type (Typ);
7154 Brng : constant Node_Id := Scalar_Range (Btyp);
7155 BLo : constant Node_Id := Low_Bound (Brng);
7156 BHi : constant Node_Id := High_Bound (Brng);
7157 Small : constant Ureal := Small_Value (Typ);
7158 Loval : Ureal;
7159 Hival : Ureal;
7160 Atype : Entity_Id;
7161
7162 Orig_Lo : Ureal;
7163 Orig_Hi : Ureal;
7164 -- Save original bounds (for shaving tests)
7165
7166 Actual_Size : Nat;
7167 -- Actual size chosen
7168
7169 function Fsize (Lov, Hiv : Ureal) return Nat;
7170 -- Returns size of type with given bounds. Also leaves these
7171 -- bounds set as the current bounds of the Typ.
7172
7173 -----------
7174 -- Fsize --
7175 -----------
7176
7177 function Fsize (Lov, Hiv : Ureal) return Nat is
7178 begin
7179 Set_Realval (Lo, Lov);
7180 Set_Realval (Hi, Hiv);
7181 return Minimum_Size (Typ);
7182 end Fsize;
7183
7184 -- Start of processing for Freeze_Fixed_Point_Type
7185
7186 begin
7187 -- If Esize of a subtype has not previously been set, set it now
7188
7189 if Unknown_Esize (Typ) then
7190 Atype := Ancestor_Subtype (Typ);
7191
7192 if Present (Atype) then
7193 Set_Esize (Typ, Esize (Atype));
7194 else
7195 Set_Esize (Typ, Esize (Base_Type (Typ)));
7196 end if;
7197 end if;
7198
7199 -- Immediate return if the range is already analyzed. This means that
7200 -- the range is already set, and does not need to be computed by this
7201 -- routine.
7202
7203 if Analyzed (Rng) then
7204 return;
7205 end if;
7206
7207 -- Immediate return if either of the bounds raises Constraint_Error
7208
7209 if Raises_Constraint_Error (Lo)
7210 or else Raises_Constraint_Error (Hi)
7211 then
7212 return;
7213 end if;
7214
7215 Loval := Realval (Lo);
7216 Hival := Realval (Hi);
7217
7218 Orig_Lo := Loval;
7219 Orig_Hi := Hival;
7220
7221 -- Ordinary fixed-point case
7222
7223 if Is_Ordinary_Fixed_Point_Type (Typ) then
7224
7225 -- For the ordinary fixed-point case, we are allowed to fudge the
7226 -- end-points up or down by small. Generally we prefer to fudge up,
7227 -- i.e. widen the bounds for non-model numbers so that the end points
7228 -- are included. However there are cases in which this cannot be
7229 -- done, and indeed cases in which we may need to narrow the bounds.
7230 -- The following circuit makes the decision.
7231
7232 -- Note: our terminology here is that Incl_EP means that the bounds
7233 -- are widened by Small if necessary to include the end points, and
7234 -- Excl_EP means that the bounds are narrowed by Small to exclude the
7235 -- end-points if this reduces the size.
7236
7237 -- Note that in the Incl case, all we care about is including the
7238 -- end-points. In the Excl case, we want to narrow the bounds as
7239 -- much as permitted by the RM, to give the smallest possible size.
7240
7241 Fudge : declare
7242 Loval_Incl_EP : Ureal;
7243 Hival_Incl_EP : Ureal;
7244
7245 Loval_Excl_EP : Ureal;
7246 Hival_Excl_EP : Ureal;
7247
7248 Size_Incl_EP : Nat;
7249 Size_Excl_EP : Nat;
7250
7251 Model_Num : Ureal;
7252 First_Subt : Entity_Id;
7253 Actual_Lo : Ureal;
7254 Actual_Hi : Ureal;
7255
7256 begin
7257 -- First step. Base types are required to be symmetrical. Right
7258 -- now, the base type range is a copy of the first subtype range.
7259 -- This will be corrected before we are done, but right away we
7260 -- need to deal with the case where both bounds are non-negative.
7261 -- In this case, we set the low bound to the negative of the high
7262 -- bound, to make sure that the size is computed to include the
7263 -- required sign. Note that we do not need to worry about the
7264 -- case of both bounds negative, because the sign will be dealt
7265 -- with anyway. Furthermore we can't just go making such a bound
7266 -- symmetrical, since in a twos-complement system, there is an
7267 -- extra negative value which could not be accommodated on the
7268 -- positive side.
7269
7270 if Typ = Btyp
7271 and then not UR_Is_Negative (Loval)
7272 and then Hival > Loval
7273 then
7274 Loval := -Hival;
7275 Set_Realval (Lo, Loval);
7276 end if;
7277
7278 -- Compute the fudged bounds. If the number is a model number,
7279 -- then we do nothing to include it, but we are allowed to backoff
7280 -- to the next adjacent model number when we exclude it. If it is
7281 -- not a model number then we straddle the two values with the
7282 -- model numbers on either side.
7283
7284 Model_Num := UR_Trunc (Loval / Small) * Small;
7285
7286 if Loval = Model_Num then
7287 Loval_Incl_EP := Model_Num;
7288 else
7289 Loval_Incl_EP := Model_Num - Small;
7290 end if;
7291
7292 -- The low value excluding the end point is Small greater, but
7293 -- we do not do this exclusion if the low value is positive,
7294 -- since it can't help the size and could actually hurt by
7295 -- crossing the high bound.
7296
7297 if UR_Is_Negative (Loval_Incl_EP) then
7298 Loval_Excl_EP := Loval_Incl_EP + Small;
7299
7300 -- If the value went from negative to zero, then we have the
7301 -- case where Loval_Incl_EP is the model number just below
7302 -- zero, so we want to stick to the negative value for the
7303 -- base type to maintain the condition that the size will
7304 -- include signed values.
7305
7306 if Typ = Btyp
7307 and then UR_Is_Zero (Loval_Excl_EP)
7308 then
7309 Loval_Excl_EP := Loval_Incl_EP;
7310 end if;
7311
7312 else
7313 Loval_Excl_EP := Loval_Incl_EP;
7314 end if;
7315
7316 -- Similar processing for upper bound and high value
7317
7318 Model_Num := UR_Trunc (Hival / Small) * Small;
7319
7320 if Hival = Model_Num then
7321 Hival_Incl_EP := Model_Num;
7322 else
7323 Hival_Incl_EP := Model_Num + Small;
7324 end if;
7325
7326 if UR_Is_Positive (Hival_Incl_EP) then
7327 Hival_Excl_EP := Hival_Incl_EP - Small;
7328 else
7329 Hival_Excl_EP := Hival_Incl_EP;
7330 end if;
7331
7332 -- One further adjustment is needed. In the case of subtypes, we
7333 -- cannot go outside the range of the base type, or we get
7334 -- peculiarities, and the base type range is already set. This
7335 -- only applies to the Incl values, since clearly the Excl values
7336 -- are already as restricted as they are allowed to be.
7337
7338 if Typ /= Btyp then
7339 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
7340 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
7341 end if;
7342
7343 -- Get size including and excluding end points
7344
7345 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
7346 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
7347
7348 -- No need to exclude end-points if it does not reduce size
7349
7350 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
7351 Loval_Excl_EP := Loval_Incl_EP;
7352 end if;
7353
7354 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
7355 Hival_Excl_EP := Hival_Incl_EP;
7356 end if;
7357
7358 -- Now we set the actual size to be used. We want to use the
7359 -- bounds fudged up to include the end-points but only if this
7360 -- can be done without violating a specifically given size
7361 -- size clause or causing an unacceptable increase in size.
7362
7363 -- Case of size clause given
7364
7365 if Has_Size_Clause (Typ) then
7366
7367 -- Use the inclusive size only if it is consistent with
7368 -- the explicitly specified size.
7369
7370 if Size_Incl_EP <= RM_Size (Typ) then
7371 Actual_Lo := Loval_Incl_EP;
7372 Actual_Hi := Hival_Incl_EP;
7373 Actual_Size := Size_Incl_EP;
7374
7375 -- If the inclusive size is too large, we try excluding
7376 -- the end-points (will be caught later if does not work).
7377
7378 else
7379 Actual_Lo := Loval_Excl_EP;
7380 Actual_Hi := Hival_Excl_EP;
7381 Actual_Size := Size_Excl_EP;
7382 end if;
7383
7384 -- Case of size clause not given
7385
7386 else
7387 -- If we have a base type whose corresponding first subtype
7388 -- has an explicit size that is large enough to include our
7389 -- end-points, then do so. There is no point in working hard
7390 -- to get a base type whose size is smaller than the specified
7391 -- size of the first subtype.
7392
7393 First_Subt := First_Subtype (Typ);
7394
7395 if Has_Size_Clause (First_Subt)
7396 and then Size_Incl_EP <= Esize (First_Subt)
7397 then
7398 Actual_Size := Size_Incl_EP;
7399 Actual_Lo := Loval_Incl_EP;
7400 Actual_Hi := Hival_Incl_EP;
7401
7402 -- If excluding the end-points makes the size smaller and
7403 -- results in a size of 8,16,32,64, then we take the smaller
7404 -- size. For the 64 case, this is compulsory. For the other
7405 -- cases, it seems reasonable. We like to include end points
7406 -- if we can, but not at the expense of moving to the next
7407 -- natural boundary of size.
7408
7409 elsif Size_Incl_EP /= Size_Excl_EP
7410 and then Addressable (Size_Excl_EP)
7411 then
7412 Actual_Size := Size_Excl_EP;
7413 Actual_Lo := Loval_Excl_EP;
7414 Actual_Hi := Hival_Excl_EP;
7415
7416 -- Otherwise we can definitely include the end points
7417
7418 else
7419 Actual_Size := Size_Incl_EP;
7420 Actual_Lo := Loval_Incl_EP;
7421 Actual_Hi := Hival_Incl_EP;
7422 end if;
7423
7424 -- One pathological case: normally we never fudge a low bound
7425 -- down, since it would seem to increase the size (if it has
7426 -- any effect), but for ranges containing single value, or no
7427 -- values, the high bound can be small too large. Consider:
7428
7429 -- type t is delta 2.0**(-14)
7430 -- range 131072.0 .. 0;
7431
7432 -- That lower bound is *just* outside the range of 32 bits, and
7433 -- does need fudging down in this case. Note that the bounds
7434 -- will always have crossed here, since the high bound will be
7435 -- fudged down if necessary, as in the case of:
7436
7437 -- type t is delta 2.0**(-14)
7438 -- range 131072.0 .. 131072.0;
7439
7440 -- So we detect the situation by looking for crossed bounds,
7441 -- and if the bounds are crossed, and the low bound is greater
7442 -- than zero, we will always back it off by small, since this
7443 -- is completely harmless.
7444
7445 if Actual_Lo > Actual_Hi then
7446 if UR_Is_Positive (Actual_Lo) then
7447 Actual_Lo := Loval_Incl_EP - Small;
7448 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7449
7450 -- And of course, we need to do exactly the same parallel
7451 -- fudge for flat ranges in the negative region.
7452
7453 elsif UR_Is_Negative (Actual_Hi) then
7454 Actual_Hi := Hival_Incl_EP + Small;
7455 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7456 end if;
7457 end if;
7458 end if;
7459
7460 Set_Realval (Lo, Actual_Lo);
7461 Set_Realval (Hi, Actual_Hi);
7462 end Fudge;
7463
7464 -- For the decimal case, none of this fudging is required, since there
7465 -- are no end-point problems in the decimal case (the end-points are
7466 -- always included).
7467
7468 else
7469 Actual_Size := Fsize (Loval, Hival);
7470 end if;
7471
7472 -- At this stage, the actual size has been calculated and the proper
7473 -- required bounds are stored in the low and high bounds.
7474
7475 if Actual_Size > 64 then
7476 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7477 Error_Msg_N
7478 ("size required (^) for type& too large, maximum allowed is 64",
7479 Typ);
7480 Actual_Size := 64;
7481 end if;
7482
7483 -- Check size against explicit given size
7484
7485 if Has_Size_Clause (Typ) then
7486 if Actual_Size > RM_Size (Typ) then
7487 Error_Msg_Uint_1 := RM_Size (Typ);
7488 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7489 Error_Msg_NE
7490 ("size given (^) for type& too small, minimum allowed is ^",
7491 Size_Clause (Typ), Typ);
7492
7493 else
7494 Actual_Size := UI_To_Int (Esize (Typ));
7495 end if;
7496
7497 -- Increase size to next natural boundary if no size clause given
7498
7499 else
7500 if Actual_Size <= 8 then
7501 Actual_Size := 8;
7502 elsif Actual_Size <= 16 then
7503 Actual_Size := 16;
7504 elsif Actual_Size <= 32 then
7505 Actual_Size := 32;
7506 else
7507 Actual_Size := 64;
7508 end if;
7509
7510 Init_Esize (Typ, Actual_Size);
7511 Adjust_Esize_For_Alignment (Typ);
7512 end if;
7513
7514 -- If we have a base type, then expand the bounds so that they extend to
7515 -- the full width of the allocated size in bits, to avoid junk range
7516 -- checks on intermediate computations.
7517
7518 if Base_Type (Typ) = Typ then
7519 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7520 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7521 end if;
7522
7523 -- Final step is to reanalyze the bounds using the proper type
7524 -- and set the Corresponding_Integer_Value fields of the literals.
7525
7526 Set_Etype (Lo, Empty);
7527 Set_Analyzed (Lo, False);
7528 Analyze (Lo);
7529
7530 -- Resolve with universal fixed if the base type, and the base type if
7531 -- it is a subtype. Note we can't resolve the base type with itself,
7532 -- that would be a reference before definition.
7533
7534 if Typ = Btyp then
7535 Resolve (Lo, Universal_Fixed);
7536 else
7537 Resolve (Lo, Btyp);
7538 end if;
7539
7540 -- Set corresponding integer value for bound
7541
7542 Set_Corresponding_Integer_Value
7543 (Lo, UR_To_Uint (Realval (Lo) / Small));
7544
7545 -- Similar processing for high bound
7546
7547 Set_Etype (Hi, Empty);
7548 Set_Analyzed (Hi, False);
7549 Analyze (Hi);
7550
7551 if Typ = Btyp then
7552 Resolve (Hi, Universal_Fixed);
7553 else
7554 Resolve (Hi, Btyp);
7555 end if;
7556
7557 Set_Corresponding_Integer_Value
7558 (Hi, UR_To_Uint (Realval (Hi) / Small));
7559
7560 -- Set type of range to correspond to bounds
7561
7562 Set_Etype (Rng, Etype (Lo));
7563
7564 -- Set Esize to calculated size if not set already
7565
7566 if Unknown_Esize (Typ) then
7567 Init_Esize (Typ, Actual_Size);
7568 end if;
7569
7570 -- Set RM_Size if not already set. If already set, check value
7571
7572 declare
7573 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7574
7575 begin
7576 if RM_Size (Typ) /= Uint_0 then
7577 if RM_Size (Typ) < Minsiz then
7578 Error_Msg_Uint_1 := RM_Size (Typ);
7579 Error_Msg_Uint_2 := Minsiz;
7580 Error_Msg_NE
7581 ("size given (^) for type& too small, minimum allowed is ^",
7582 Size_Clause (Typ), Typ);
7583 end if;
7584
7585 else
7586 Set_RM_Size (Typ, Minsiz);
7587 end if;
7588 end;
7589
7590 -- Check for shaving
7591
7592 if Comes_From_Source (Typ) then
7593 if Orig_Lo < Expr_Value_R (Lo) then
7594 Error_Msg_N
7595 ("declared low bound of type & is outside type range??", Typ);
7596 Error_Msg_N
7597 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
7598 end if;
7599
7600 if Orig_Hi > Expr_Value_R (Hi) then
7601 Error_Msg_N
7602 ("declared high bound of type & is outside type range??", Typ);
7603 Error_Msg_N
7604 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
7605 end if;
7606 end if;
7607 end Freeze_Fixed_Point_Type;
7608
7609 ------------------
7610 -- Freeze_Itype --
7611 ------------------
7612
7613 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7614 L : List_Id;
7615
7616 begin
7617 Set_Has_Delayed_Freeze (T);
7618 L := Freeze_Entity (T, N);
7619
7620 if Is_Non_Empty_List (L) then
7621 Insert_Actions (N, L);
7622 end if;
7623 end Freeze_Itype;
7624
7625 --------------------------
7626 -- Freeze_Static_Object --
7627 --------------------------
7628
7629 procedure Freeze_Static_Object (E : Entity_Id) is
7630
7631 Cannot_Be_Static : exception;
7632 -- Exception raised if the type of a static object cannot be made
7633 -- static. This happens if the type depends on non-global objects.
7634
7635 procedure Ensure_Expression_Is_SA (N : Node_Id);
7636 -- Called to ensure that an expression used as part of a type definition
7637 -- is statically allocatable, which means that the expression type is
7638 -- statically allocatable, and the expression is either static, or a
7639 -- reference to a library level constant.
7640
7641 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7642 -- Called to mark a type as static, checking that it is possible
7643 -- to set the type as static. If it is not possible, then the
7644 -- exception Cannot_Be_Static is raised.
7645
7646 -----------------------------
7647 -- Ensure_Expression_Is_SA --
7648 -----------------------------
7649
7650 procedure Ensure_Expression_Is_SA (N : Node_Id) is
7651 Ent : Entity_Id;
7652
7653 begin
7654 Ensure_Type_Is_SA (Etype (N));
7655
7656 if Is_OK_Static_Expression (N) then
7657 return;
7658
7659 elsif Nkind (N) = N_Identifier then
7660 Ent := Entity (N);
7661
7662 if Present (Ent)
7663 and then Ekind (Ent) = E_Constant
7664 and then Is_Library_Level_Entity (Ent)
7665 then
7666 return;
7667 end if;
7668 end if;
7669
7670 raise Cannot_Be_Static;
7671 end Ensure_Expression_Is_SA;
7672
7673 -----------------------
7674 -- Ensure_Type_Is_SA --
7675 -----------------------
7676
7677 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7678 N : Node_Id;
7679 C : Entity_Id;
7680
7681 begin
7682 -- If type is library level, we are all set
7683
7684 if Is_Library_Level_Entity (Typ) then
7685 return;
7686 end if;
7687
7688 -- We are also OK if the type already marked as statically allocated,
7689 -- which means we processed it before.
7690
7691 if Is_Statically_Allocated (Typ) then
7692 return;
7693 end if;
7694
7695 -- Mark type as statically allocated
7696
7697 Set_Is_Statically_Allocated (Typ);
7698
7699 -- Check that it is safe to statically allocate this type
7700
7701 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
7702 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
7703 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
7704
7705 elsif Is_Array_Type (Typ) then
7706 N := First_Index (Typ);
7707 while Present (N) loop
7708 Ensure_Type_Is_SA (Etype (N));
7709 Next_Index (N);
7710 end loop;
7711
7712 Ensure_Type_Is_SA (Component_Type (Typ));
7713
7714 elsif Is_Access_Type (Typ) then
7715 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
7716
7717 declare
7718 F : Entity_Id;
7719 T : constant Entity_Id := Etype (Designated_Type (Typ));
7720
7721 begin
7722 if T /= Standard_Void_Type then
7723 Ensure_Type_Is_SA (T);
7724 end if;
7725
7726 F := First_Formal (Designated_Type (Typ));
7727 while Present (F) loop
7728 Ensure_Type_Is_SA (Etype (F));
7729 Next_Formal (F);
7730 end loop;
7731 end;
7732
7733 else
7734 Ensure_Type_Is_SA (Designated_Type (Typ));
7735 end if;
7736
7737 elsif Is_Record_Type (Typ) then
7738 C := First_Entity (Typ);
7739 while Present (C) loop
7740 if Ekind (C) = E_Discriminant
7741 or else Ekind (C) = E_Component
7742 then
7743 Ensure_Type_Is_SA (Etype (C));
7744
7745 elsif Is_Type (C) then
7746 Ensure_Type_Is_SA (C);
7747 end if;
7748
7749 Next_Entity (C);
7750 end loop;
7751
7752 elsif Ekind (Typ) = E_Subprogram_Type then
7753 Ensure_Type_Is_SA (Etype (Typ));
7754
7755 C := First_Formal (Typ);
7756 while Present (C) loop
7757 Ensure_Type_Is_SA (Etype (C));
7758 Next_Formal (C);
7759 end loop;
7760
7761 else
7762 raise Cannot_Be_Static;
7763 end if;
7764 end Ensure_Type_Is_SA;
7765
7766 -- Start of processing for Freeze_Static_Object
7767
7768 begin
7769 Ensure_Type_Is_SA (Etype (E));
7770
7771 exception
7772 when Cannot_Be_Static =>
7773
7774 -- If the object that cannot be static is imported or exported, then
7775 -- issue an error message saying that this object cannot be imported
7776 -- or exported. If it has an address clause it is an overlay in the
7777 -- current partition and the static requirement is not relevant.
7778 -- Do not issue any error message when ignoring rep clauses.
7779
7780 if Ignore_Rep_Clauses then
7781 null;
7782
7783 elsif Is_Imported (E) then
7784 if No (Address_Clause (E)) then
7785 Error_Msg_N
7786 ("& cannot be imported (local type is not constant)", E);
7787 end if;
7788
7789 -- Otherwise must be exported, something is wrong if compiler
7790 -- is marking something as statically allocated which cannot be).
7791
7792 else pragma Assert (Is_Exported (E));
7793 Error_Msg_N
7794 ("& cannot be exported (local type is not constant)", E);
7795 end if;
7796 end Freeze_Static_Object;
7797
7798 -----------------------
7799 -- Freeze_Subprogram --
7800 -----------------------
7801
7802 procedure Freeze_Subprogram (E : Entity_Id) is
7803 Retype : Entity_Id;
7804 F : Entity_Id;
7805
7806 begin
7807 -- Subprogram may not have an address clause unless it is imported
7808
7809 if Present (Address_Clause (E)) then
7810 if not Is_Imported (E) then
7811 Error_Msg_N
7812 ("address clause can only be given " &
7813 "for imported subprogram",
7814 Name (Address_Clause (E)));
7815 end if;
7816 end if;
7817
7818 -- Reset the Pure indication on an imported subprogram unless an
7819 -- explicit Pure_Function pragma was present or the subprogram is an
7820 -- intrinsic. We do this because otherwise it is an insidious error
7821 -- to call a non-pure function from pure unit and have calls
7822 -- mysteriously optimized away. What happens here is that the Import
7823 -- can bypass the normal check to ensure that pure units call only pure
7824 -- subprograms.
7825
7826 -- The reason for the intrinsic exception is that in general, intrinsic
7827 -- functions (such as shifts) are pure anyway. The only exceptions are
7828 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
7829 -- in any case, so no problem arises.
7830
7831 if Is_Imported (E)
7832 and then Is_Pure (E)
7833 and then not Has_Pragma_Pure_Function (E)
7834 and then not Is_Intrinsic_Subprogram (E)
7835 then
7836 Set_Is_Pure (E, False);
7837 end if;
7838
7839 -- We also reset the Pure indication on a subprogram with an Address
7840 -- parameter, because the parameter may be used as a pointer and the
7841 -- referenced data may change even if the address value does not.
7842
7843 -- Note that if the programmer gave an explicit Pure_Function pragma,
7844 -- then we believe the programmer, and leave the subprogram Pure.
7845 -- We also suppress this check on run-time files.
7846
7847 if Is_Pure (E)
7848 and then Is_Subprogram (E)
7849 and then not Has_Pragma_Pure_Function (E)
7850 and then not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
7851 then
7852 Check_Function_With_Address_Parameter (E);
7853 end if;
7854
7855 -- For non-foreign convention subprograms, this is where we create
7856 -- the extra formals (for accessibility level and constrained bit
7857 -- information). We delay this till the freeze point precisely so
7858 -- that we know the convention.
7859
7860 if not Has_Foreign_Convention (E) then
7861 Create_Extra_Formals (E);
7862 Set_Mechanisms (E);
7863
7864 -- If this is convention Ada and a Valued_Procedure, that's odd
7865
7866 if Ekind (E) = E_Procedure
7867 and then Is_Valued_Procedure (E)
7868 and then Convention (E) = Convention_Ada
7869 and then Warn_On_Export_Import
7870 then
7871 Error_Msg_N
7872 ("??Valued_Procedure has no effect for convention Ada", E);
7873 Set_Is_Valued_Procedure (E, False);
7874 end if;
7875
7876 -- Case of foreign convention
7877
7878 else
7879 Set_Mechanisms (E);
7880
7881 -- For foreign conventions, warn about return of unconstrained array
7882
7883 if Ekind (E) = E_Function then
7884 Retype := Underlying_Type (Etype (E));
7885
7886 -- If no return type, probably some other error, e.g. a
7887 -- missing full declaration, so ignore.
7888
7889 if No (Retype) then
7890 null;
7891
7892 -- If the return type is generic, we have emitted a warning
7893 -- earlier on, and there is nothing else to check here. Specific
7894 -- instantiations may lead to erroneous behavior.
7895
7896 elsif Is_Generic_Type (Etype (E)) then
7897 null;
7898
7899 -- Display warning if returning unconstrained array
7900
7901 elsif Is_Array_Type (Retype)
7902 and then not Is_Constrained (Retype)
7903
7904 -- Check appropriate warning is enabled (should we check for
7905 -- Warnings (Off) on specific entities here, probably so???)
7906
7907 and then Warn_On_Export_Import
7908 then
7909 Error_Msg_N
7910 ("?x?foreign convention function& should not return " &
7911 "unconstrained array", E);
7912 return;
7913 end if;
7914 end if;
7915
7916 -- If any of the formals for an exported foreign convention
7917 -- subprogram have defaults, then emit an appropriate warning since
7918 -- this is odd (default cannot be used from non-Ada code)
7919
7920 if Is_Exported (E) then
7921 F := First_Formal (E);
7922 while Present (F) loop
7923 if Warn_On_Export_Import
7924 and then Present (Default_Value (F))
7925 then
7926 Error_Msg_N
7927 ("?x?parameter cannot be defaulted in non-Ada call",
7928 Default_Value (F));
7929 end if;
7930
7931 Next_Formal (F);
7932 end loop;
7933 end if;
7934 end if;
7935
7936 -- Pragma Inline_Always is disallowed for dispatching subprograms
7937 -- because the address of such subprograms is saved in the dispatch
7938 -- table to support dispatching calls, and dispatching calls cannot
7939 -- be inlined. This is consistent with the restriction against using
7940 -- 'Access or 'Address on an Inline_Always subprogram.
7941
7942 if Is_Dispatching_Operation (E)
7943 and then Has_Pragma_Inline_Always (E)
7944 then
7945 Error_Msg_N
7946 ("pragma Inline_Always not allowed for dispatching subprograms", E);
7947 end if;
7948
7949 -- Because of the implicit representation of inherited predefined
7950 -- operators in the front-end, the overriding status of the operation
7951 -- may be affected when a full view of a type is analyzed, and this is
7952 -- not captured by the analysis of the corresponding type declaration.
7953 -- Therefore the correctness of a not-overriding indicator must be
7954 -- rechecked when the subprogram is frozen.
7955
7956 if Nkind (E) = N_Defining_Operator_Symbol
7957 and then not Error_Posted (Parent (E))
7958 then
7959 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
7960 end if;
7961
7962 if Modify_Tree_For_C
7963 and then Nkind (Parent (E)) = N_Function_Specification
7964 and then Is_Array_Type (Etype (E))
7965 and then Is_Constrained (Etype (E))
7966 and then not Is_Unchecked_Conversion_Instance (E)
7967 and then not Rewritten_For_C (E)
7968 then
7969 Build_Procedure_Form (Unit_Declaration_Node (E));
7970 end if;
7971 end Freeze_Subprogram;
7972
7973 ----------------------
7974 -- Is_Fully_Defined --
7975 ----------------------
7976
7977 function Is_Fully_Defined (T : Entity_Id) return Boolean is
7978 begin
7979 if Ekind (T) = E_Class_Wide_Type then
7980 return Is_Fully_Defined (Etype (T));
7981
7982 elsif Is_Array_Type (T) then
7983 return Is_Fully_Defined (Component_Type (T));
7984
7985 elsif Is_Record_Type (T)
7986 and not Is_Private_Type (T)
7987 then
7988 -- Verify that the record type has no components with private types
7989 -- without completion.
7990
7991 declare
7992 Comp : Entity_Id;
7993
7994 begin
7995 Comp := First_Component (T);
7996 while Present (Comp) loop
7997 if not Is_Fully_Defined (Etype (Comp)) then
7998 return False;
7999 end if;
8000
8001 Next_Component (Comp);
8002 end loop;
8003 return True;
8004 end;
8005
8006 -- For the designated type of an access to subprogram, all types in
8007 -- the profile must be fully defined.
8008
8009 elsif Ekind (T) = E_Subprogram_Type then
8010 declare
8011 F : Entity_Id;
8012
8013 begin
8014 F := First_Formal (T);
8015 while Present (F) loop
8016 if not Is_Fully_Defined (Etype (F)) then
8017 return False;
8018 end if;
8019
8020 Next_Formal (F);
8021 end loop;
8022
8023 return Is_Fully_Defined (Etype (T));
8024 end;
8025
8026 else
8027 return not Is_Private_Type (T)
8028 or else Present (Full_View (Base_Type (T)));
8029 end if;
8030 end Is_Fully_Defined;
8031
8032 ---------------------------------
8033 -- Process_Default_Expressions --
8034 ---------------------------------
8035
8036 procedure Process_Default_Expressions
8037 (E : Entity_Id;
8038 After : in out Node_Id)
8039 is
8040 Loc : constant Source_Ptr := Sloc (E);
8041 Dbody : Node_Id;
8042 Formal : Node_Id;
8043 Dcopy : Node_Id;
8044 Dnam : Entity_Id;
8045
8046 begin
8047 Set_Default_Expressions_Processed (E);
8048
8049 -- A subprogram instance and its associated anonymous subprogram share
8050 -- their signature. The default expression functions are defined in the
8051 -- wrapper packages for the anonymous subprogram, and should not be
8052 -- generated again for the instance.
8053
8054 if Is_Generic_Instance (E)
8055 and then Present (Alias (E))
8056 and then Default_Expressions_Processed (Alias (E))
8057 then
8058 return;
8059 end if;
8060
8061 Formal := First_Formal (E);
8062 while Present (Formal) loop
8063 if Present (Default_Value (Formal)) then
8064
8065 -- We work with a copy of the default expression because we
8066 -- do not want to disturb the original, since this would mess
8067 -- up the conformance checking.
8068
8069 Dcopy := New_Copy_Tree (Default_Value (Formal));
8070
8071 -- The analysis of the expression may generate insert actions,
8072 -- which of course must not be executed. We wrap those actions
8073 -- in a procedure that is not called, and later on eliminated.
8074 -- The following cases have no side-effects, and are analyzed
8075 -- directly.
8076
8077 if Nkind (Dcopy) = N_Identifier
8078 or else Nkind_In (Dcopy, N_Expanded_Name,
8079 N_Integer_Literal,
8080 N_Character_Literal,
8081 N_String_Literal,
8082 N_Real_Literal)
8083 or else (Nkind (Dcopy) = N_Attribute_Reference
8084 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
8085 or else Known_Null (Dcopy)
8086 then
8087 -- If there is no default function, we must still do a full
8088 -- analyze call on the default value, to ensure that all error
8089 -- checks are performed, e.g. those associated with static
8090 -- evaluation. Note: this branch will always be taken if the
8091 -- analyzer is turned off (but we still need the error checks).
8092
8093 -- Note: the setting of parent here is to meet the requirement
8094 -- that we can only analyze the expression while attached to
8095 -- the tree. Really the requirement is that the parent chain
8096 -- be set, we don't actually need to be in the tree.
8097
8098 Set_Parent (Dcopy, Declaration_Node (Formal));
8099 Analyze (Dcopy);
8100
8101 -- Default expressions are resolved with their own type if the
8102 -- context is generic, to avoid anomalies with private types.
8103
8104 if Ekind (Scope (E)) = E_Generic_Package then
8105 Resolve (Dcopy);
8106 else
8107 Resolve (Dcopy, Etype (Formal));
8108 end if;
8109
8110 -- If that resolved expression will raise constraint error,
8111 -- then flag the default value as raising constraint error.
8112 -- This allows a proper error message on the calls.
8113
8114 if Raises_Constraint_Error (Dcopy) then
8115 Set_Raises_Constraint_Error (Default_Value (Formal));
8116 end if;
8117
8118 -- If the default is a parameterless call, we use the name of
8119 -- the called function directly, and there is no body to build.
8120
8121 elsif Nkind (Dcopy) = N_Function_Call
8122 and then No (Parameter_Associations (Dcopy))
8123 then
8124 null;
8125
8126 -- Else construct and analyze the body of a wrapper procedure
8127 -- that contains an object declaration to hold the expression.
8128 -- Given that this is done only to complete the analysis, it
8129 -- simpler to build a procedure than a function which might
8130 -- involve secondary stack expansion.
8131
8132 else
8133 Dnam := Make_Temporary (Loc, 'D');
8134
8135 Dbody :=
8136 Make_Subprogram_Body (Loc,
8137 Specification =>
8138 Make_Procedure_Specification (Loc,
8139 Defining_Unit_Name => Dnam),
8140
8141 Declarations => New_List (
8142 Make_Object_Declaration (Loc,
8143 Defining_Identifier => Make_Temporary (Loc, 'T'),
8144 Object_Definition =>
8145 New_Occurrence_Of (Etype (Formal), Loc),
8146 Expression => New_Copy_Tree (Dcopy))),
8147
8148 Handled_Statement_Sequence =>
8149 Make_Handled_Sequence_Of_Statements (Loc,
8150 Statements => Empty_List));
8151
8152 Set_Scope (Dnam, Scope (E));
8153 Set_Assignment_OK (First (Declarations (Dbody)));
8154 Set_Is_Eliminated (Dnam);
8155 Insert_After (After, Dbody);
8156 Analyze (Dbody);
8157 After := Dbody;
8158 end if;
8159 end if;
8160
8161 Next_Formal (Formal);
8162 end loop;
8163 end Process_Default_Expressions;
8164
8165 ----------------------------------------
8166 -- Set_Component_Alignment_If_Not_Set --
8167 ----------------------------------------
8168
8169 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
8170 begin
8171 -- Ignore if not base type, subtypes don't need anything
8172
8173 if Typ /= Base_Type (Typ) then
8174 return;
8175 end if;
8176
8177 -- Do not override existing representation
8178
8179 if Is_Packed (Typ) then
8180 return;
8181
8182 elsif Has_Specified_Layout (Typ) then
8183 return;
8184
8185 elsif Component_Alignment (Typ) /= Calign_Default then
8186 return;
8187
8188 else
8189 Set_Component_Alignment
8190 (Typ, Scope_Stack.Table
8191 (Scope_Stack.Last).Component_Alignment_Default);
8192 end if;
8193 end Set_Component_Alignment_If_Not_Set;
8194
8195 --------------------------
8196 -- Set_SSO_From_Default --
8197 --------------------------
8198
8199 procedure Set_SSO_From_Default (T : Entity_Id) is
8200 Reversed : Boolean;
8201
8202 begin
8203 -- Set default SSO for an array or record base type, except in case of
8204 -- a type extension (which always inherits the SSO of its parent type).
8205
8206 if Is_Base_Type (T)
8207 and then (Is_Array_Type (T)
8208 or else (Is_Record_Type (T)
8209 and then not (Is_Tagged_Type (T)
8210 and then Is_Derived_Type (T))))
8211 then
8212 Reversed :=
8213 (Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
8214 or else
8215 (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
8216
8217 if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
8218
8219 -- For a record type, if bit order is specified explicitly,
8220 -- then do not set SSO from default if not consistent. Note that
8221 -- we do not want to look at a Bit_Order attribute definition
8222 -- for a parent: if we were to inherit Bit_Order, then both
8223 -- SSO_Set_*_By_Default flags would have been cleared already
8224 -- (by Inherit_Aspects_At_Freeze_Point).
8225
8226 and then not
8227 (Is_Record_Type (T)
8228 and then
8229 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
8230 and then Reverse_Bit_Order (T) /= Reversed)
8231 then
8232 -- If flags cause reverse storage order, then set the result. Note
8233 -- that we would have ignored the pragma setting the non default
8234 -- storage order in any case, hence the assertion at this point.
8235
8236 pragma Assert
8237 (not Reversed or else Support_Nondefault_SSO_On_Target);
8238
8239 Set_Reverse_Storage_Order (T, Reversed);
8240
8241 -- For a record type, also set reversed bit order. Note: if a bit
8242 -- order has been specified explicitly, then this is a no-op.
8243
8244 if Is_Record_Type (T) then
8245 Set_Reverse_Bit_Order (T, Reversed);
8246 end if;
8247 end if;
8248 end if;
8249 end Set_SSO_From_Default;
8250
8251 ------------------
8252 -- Undelay_Type --
8253 ------------------
8254
8255 procedure Undelay_Type (T : Entity_Id) is
8256 begin
8257 Set_Has_Delayed_Freeze (T, False);
8258 Set_Freeze_Node (T, Empty);
8259
8260 -- Since we don't want T to have a Freeze_Node, we don't want its
8261 -- Full_View or Corresponding_Record_Type to have one either.
8262
8263 -- ??? Fundamentally, this whole handling is unpleasant. What we really
8264 -- want is to be sure that for an Itype that's part of record R and is a
8265 -- subtype of type T, that it's frozen after the later of the freeze
8266 -- points of R and T. We have no way of doing that directly, so what we
8267 -- do is force most such Itypes to be frozen as part of freezing R via
8268 -- this procedure and only delay the ones that need to be delayed
8269 -- (mostly the designated types of access types that are defined as part
8270 -- of the record).
8271
8272 if Is_Private_Type (T)
8273 and then Present (Full_View (T))
8274 and then Is_Itype (Full_View (T))
8275 and then Is_Record_Type (Scope (Full_View (T)))
8276 then
8277 Undelay_Type (Full_View (T));
8278 end if;
8279
8280 if Is_Concurrent_Type (T)
8281 and then Present (Corresponding_Record_Type (T))
8282 and then Is_Itype (Corresponding_Record_Type (T))
8283 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
8284 then
8285 Undelay_Type (Corresponding_Record_Type (T));
8286 end if;
8287 end Undelay_Type;
8288
8289 ------------------
8290 -- Warn_Overlay --
8291 ------------------
8292
8293 procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Entity_Id) is
8294 Ent : constant Entity_Id := Entity (Nam);
8295 -- The object to which the address clause applies
8296
8297 Init : Node_Id;
8298 Old : Entity_Id := Empty;
8299 Decl : Node_Id;
8300
8301 begin
8302 -- No warning if address clause overlay warnings are off
8303
8304 if not Address_Clause_Overlay_Warnings then
8305 return;
8306 end if;
8307
8308 -- No warning if there is an explicit initialization
8309
8310 Init := Original_Node (Expression (Declaration_Node (Ent)));
8311
8312 if Present (Init) and then Comes_From_Source (Init) then
8313 return;
8314 end if;
8315
8316 -- We only give the warning for non-imported entities of a type for
8317 -- which a non-null base init proc is defined, or for objects of access
8318 -- types with implicit null initialization, or when Normalize_Scalars
8319 -- applies and the type is scalar or a string type (the latter being
8320 -- tested for because predefined String types are initialized by inline
8321 -- code rather than by an init_proc). Note that we do not give the
8322 -- warning for Initialize_Scalars, since we suppressed initialization
8323 -- in this case. Also, do not warn if Suppress_Initialization is set.
8324
8325 if Present (Expr)
8326 and then not Is_Imported (Ent)
8327 and then not Initialization_Suppressed (Typ)
8328 and then (Has_Non_Null_Base_Init_Proc (Typ)
8329 or else Is_Access_Type (Typ)
8330 or else (Normalize_Scalars
8331 and then (Is_Scalar_Type (Typ)
8332 or else Is_String_Type (Typ))))
8333 then
8334 if Nkind (Expr) = N_Attribute_Reference
8335 and then Is_Entity_Name (Prefix (Expr))
8336 then
8337 Old := Entity (Prefix (Expr));
8338
8339 elsif Is_Entity_Name (Expr)
8340 and then Ekind (Entity (Expr)) = E_Constant
8341 then
8342 Decl := Declaration_Node (Entity (Expr));
8343
8344 if Nkind (Decl) = N_Object_Declaration
8345 and then Present (Expression (Decl))
8346 and then Nkind (Expression (Decl)) = N_Attribute_Reference
8347 and then Is_Entity_Name (Prefix (Expression (Decl)))
8348 then
8349 Old := Entity (Prefix (Expression (Decl)));
8350
8351 elsif Nkind (Expr) = N_Function_Call then
8352 return;
8353 end if;
8354
8355 -- A function call (most likely to To_Address) is probably not an
8356 -- overlay, so skip warning. Ditto if the function call was inlined
8357 -- and transformed into an entity.
8358
8359 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
8360 return;
8361 end if;
8362
8363 -- If a pragma Import follows, we assume that it is for the current
8364 -- target of the address clause, and skip the warning. There may be
8365 -- a source pragma or an aspect that specifies import and generates
8366 -- the corresponding pragma. These will indicate that the entity is
8367 -- imported and that is checked above so that the spurious warning
8368 -- (generated when the entity is frozen) will be suppressed. The
8369 -- pragma may be attached to the aspect, so it is not yet a list
8370 -- member.
8371
8372 if Is_List_Member (Parent (Expr)) then
8373 Decl := Next (Parent (Expr));
8374
8375 if Present (Decl)
8376 and then Nkind (Decl) = N_Pragma
8377 and then Pragma_Name (Decl) = Name_Import
8378 then
8379 return;
8380 end if;
8381 end if;
8382
8383 -- Otherwise give warning message
8384
8385 if Present (Old) then
8386 Error_Msg_Node_2 := Old;
8387 Error_Msg_N
8388 ("default initialization of & may modify &??",
8389 Nam);
8390 else
8391 Error_Msg_N
8392 ("default initialization of & may modify overlaid storage??",
8393 Nam);
8394 end if;
8395
8396 -- Add friendly warning if initialization comes from a packed array
8397 -- component.
8398
8399 if Is_Record_Type (Typ) then
8400 declare
8401 Comp : Entity_Id;
8402
8403 begin
8404 Comp := First_Component (Typ);
8405 while Present (Comp) loop
8406 if Nkind (Parent (Comp)) = N_Component_Declaration
8407 and then Present (Expression (Parent (Comp)))
8408 then
8409 exit;
8410 elsif Is_Array_Type (Etype (Comp))
8411 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
8412 then
8413 Error_Msg_NE
8414 ("\packed array component& " &
8415 "will be initialized to zero??",
8416 Nam, Comp);
8417 exit;
8418 else
8419 Next_Component (Comp);
8420 end if;
8421 end loop;
8422 end;
8423 end if;
8424
8425 Error_Msg_N
8426 ("\use pragma Import for & to " &
8427 "suppress initialization (RM B.1(24))??",
8428 Nam);
8429 end if;
8430 end Warn_Overlay;
8431
8432 end Freeze;