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