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