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