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