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