2015-01-06 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / freeze.adb
blobcc5553e09ab9c4b175d5d538cf3ba1d29080ad19
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 non-constant aliased object. We
4600 -- consider that the fact that a non-constant object is aliased
4601 -- may indicate that some funny business is going on, e.g. an
4602 -- aliased object is passed by reference to a procedure which
4603 -- captures the address of the object, which is later used to
4604 -- assign a new value, even though the compiler thinks that
4605 -- it is not modified. Such code is highly dubious, but we
4606 -- choose to make it "work" for non-constant aliased objects.
4607 -- Note that we used to do this for all aliased objects,
4608 -- whether or not constant, but this caused anomalies down
4609 -- the line because we ended up with static objects that
4610 -- were not Is_True_Constant. Not resetting Is_True_Constant
4611 -- for (aliased) constant objects ensures that this anomaly
4612 -- never occurs.
4614 -- However, we don't do that for internal entities. We figure
4615 -- that if we deliberately set Is_True_Constant for an internal
4616 -- entity, e.g. a dispatch table entry, then we mean it.
4618 if Ekind (E) /= E_Constant
4619 and then (Is_Aliased (E) or else Is_Aliased (Etype (E)))
4620 and then not Is_Internal_Name (Chars (E))
4621 then
4622 Set_Is_True_Constant (E, False);
4623 end if;
4625 -- If the object needs any kind of default initialization, an
4626 -- error must be issued if No_Default_Initialization applies.
4627 -- The check doesn't apply to imported objects, which are not
4628 -- ever default initialized, and is why the check is deferred
4629 -- until freezing, at which point we know if Import applies.
4630 -- Deferred constants are also exempted from this test because
4631 -- their completion is explicit, or through an import pragma.
4633 if Ekind (E) = E_Constant
4634 and then Present (Full_View (E))
4635 then
4636 null;
4638 elsif Comes_From_Source (E)
4639 and then not Is_Imported (E)
4640 and then not Has_Init_Expression (Declaration_Node (E))
4641 and then
4642 ((Has_Non_Null_Base_Init_Proc (Etype (E))
4643 and then not No_Initialization (Declaration_Node (E))
4644 and then not Is_Value_Type (Etype (E))
4645 and then not Initialization_Suppressed (Etype (E)))
4646 or else
4647 (Needs_Simple_Initialization (Etype (E))
4648 and then not Is_Internal (E)))
4649 then
4650 Has_Default_Initialization := True;
4651 Check_Restriction
4652 (No_Default_Initialization, Declaration_Node (E));
4653 end if;
4655 -- Check that a Thread_Local_Storage variable does not have
4656 -- default initialization, and any explicit initialization must
4657 -- either be the null constant or a static constant.
4659 if Has_Pragma_Thread_Local_Storage (E) then
4660 declare
4661 Decl : constant Node_Id := Declaration_Node (E);
4662 begin
4663 if Has_Default_Initialization
4664 or else
4665 (Has_Init_Expression (Decl)
4666 and then
4667 (No (Expression (Decl))
4668 or else not
4669 (Is_OK_Static_Expression (Expression (Decl))
4670 or else
4671 Nkind (Expression (Decl)) = N_Null)))
4672 then
4673 Error_Msg_NE
4674 ("Thread_Local_Storage variable& is "
4675 & "improperly initialized", Decl, E);
4676 Error_Msg_NE
4677 ("\only allowed initialization is explicit "
4678 & "NULL or static expression", Decl, E);
4679 end if;
4680 end;
4681 end if;
4683 -- For imported objects, set Is_Public unless there is also an
4684 -- address clause, which means that there is no external symbol
4685 -- needed for the Import (Is_Public may still be set for other
4686 -- unrelated reasons). Note that we delayed this processing
4687 -- till freeze time so that we can be sure not to set the flag
4688 -- if there is an address clause. If there is such a clause,
4689 -- then the only purpose of the Import pragma is to suppress
4690 -- implicit initialization.
4692 if Is_Imported (E) and then No (Address_Clause (E)) then
4693 Set_Is_Public (E);
4694 end if;
4696 -- For source objects that are not Imported and are library
4697 -- level, if no linker section pragma was given inherit the
4698 -- appropriate linker section from the corresponding type.
4700 if Comes_From_Source (E)
4701 and then not Is_Imported (E)
4702 and then Is_Library_Level_Entity (E)
4703 and then No (Linker_Section_Pragma (E))
4704 then
4705 Set_Linker_Section_Pragma
4706 (E, Linker_Section_Pragma (Etype (E)));
4707 end if;
4709 -- For convention C objects of an enumeration type, warn if
4710 -- the size is not integer size and no explicit size given.
4711 -- Skip warning for Boolean, and Character, assume programmer
4712 -- expects 8-bit sizes for these cases.
4714 if (Convention (E) = Convention_C
4715 or else
4716 Convention (E) = Convention_CPP)
4717 and then Is_Enumeration_Type (Etype (E))
4718 and then not Is_Character_Type (Etype (E))
4719 and then not Is_Boolean_Type (Etype (E))
4720 and then Esize (Etype (E)) < Standard_Integer_Size
4721 and then not Has_Size_Clause (E)
4722 then
4723 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
4724 Error_Msg_N
4725 ("??convention C enumeration object has size less than ^",
4727 Error_Msg_N ("\??use explicit size clause to set size", E);
4728 end if;
4729 end if;
4731 -- Check that a constant which has a pragma Volatile[_Components]
4732 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
4734 -- Note: Atomic[_Components] also sets Volatile[_Components]
4736 if Ekind (E) = E_Constant
4737 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
4738 and then not Is_Imported (E)
4739 and then not Has_Boolean_Aspect_Import (E)
4740 then
4741 -- Make sure we actually have a pragma, and have not merely
4742 -- inherited the indication from elsewhere (e.g. an address
4743 -- clause, which is not good enough in RM terms).
4745 if Has_Rep_Pragma (E, Name_Atomic)
4746 or else
4747 Has_Rep_Pragma (E, Name_Atomic_Components)
4748 then
4749 Error_Msg_N
4750 ("stand alone atomic constant must be " &
4751 "imported (RM C.6(13))", E);
4753 elsif Has_Rep_Pragma (E, Name_Volatile)
4754 or else
4755 Has_Rep_Pragma (E, Name_Volatile_Components)
4756 then
4757 Error_Msg_N
4758 ("stand alone volatile constant must be " &
4759 "imported (RM C.6(13))", E);
4760 end if;
4761 end if;
4763 -- Static objects require special handling
4765 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
4766 and then Is_Statically_Allocated (E)
4767 then
4768 Freeze_Static_Object (E);
4769 end if;
4771 -- Remaining step is to layout objects
4773 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
4774 or else Is_Formal (E)
4775 then
4776 Layout_Object (E);
4777 end if;
4779 -- For an object that does not have delayed freezing, and whose
4780 -- initialization actions have been captured in a compound
4781 -- statement, move them back now directly within the enclosing
4782 -- statement sequence.
4784 if Ekind_In (E, E_Constant, E_Variable)
4785 and then not Has_Delayed_Freeze (E)
4786 then
4787 Explode_Initialization_Compound_Statement (E);
4788 end if;
4789 end if;
4791 -- Case of a type or subtype being frozen
4793 else
4794 -- We used to check here that a full type must have preelaborable
4795 -- initialization if it completes a private type specified with
4796 -- pragma Preelaborable_Initialization, but that missed cases where
4797 -- the types occur within a generic package, since the freezing
4798 -- that occurs within a containing scope generally skips traversal
4799 -- of a generic unit's declarations (those will be frozen within
4800 -- instances). This check was moved to Analyze_Package_Specification.
4802 -- The type may be defined in a generic unit. This can occur when
4803 -- freezing a generic function that returns the type (which is
4804 -- defined in a parent unit). It is clearly meaningless to freeze
4805 -- this type. However, if it is a subtype, its size may be determi-
4806 -- nable and used in subsequent checks, so might as well try to
4807 -- compute it.
4809 -- In Ada 2012, Freeze_Entities is also used in the front end to
4810 -- trigger the analysis of aspect expressions, so in this case we
4811 -- want to continue the freezing process.
4813 if Present (Scope (E))
4814 and then Is_Generic_Unit (Scope (E))
4815 and then
4816 (not Has_Predicates (E)
4817 and then not Has_Delayed_Freeze (E))
4818 then
4819 Check_Compile_Time_Size (E);
4820 return No_List;
4821 end if;
4823 -- Check for error of Type_Invariant'Class applied to an untagged
4824 -- type (check delayed to freeze time when full type is available).
4826 declare
4827 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
4828 begin
4829 if Present (Prag)
4830 and then Class_Present (Prag)
4831 and then not Is_Tagged_Type (E)
4832 then
4833 Error_Msg_NE
4834 ("Type_Invariant''Class cannot be specified for &",
4835 Prag, E);
4836 Error_Msg_N
4837 ("\can only be specified for a tagged type", Prag);
4838 end if;
4839 end;
4841 -- A Ghost type cannot be effectively volatile (SPARK RM 6.9(8))
4843 if Is_Ghost_Entity (E)
4844 and then Is_Effectively_Volatile (E)
4845 then
4846 Error_Msg_N ("ghost type & cannot be volatile", E);
4847 end if;
4849 -- Deal with special cases of freezing for subtype
4851 if E /= Base_Type (E) then
4853 -- Before we do anything else, a specialized test for the case of
4854 -- a size given for an array where the array needs to be packed,
4855 -- but was not so the size cannot be honored. This is the case
4856 -- where implicit packing may apply. The reason we do this so
4857 -- early is that if we have implicit packing, the layout of the
4858 -- base type is affected, so we must do this before we freeze
4859 -- the base type.
4861 -- We could do this processing only if implicit packing is enabled
4862 -- since in all other cases, the error would be caught by the back
4863 -- end. However, we choose to do the check even if we do not have
4864 -- implicit packing enabled, since this allows us to give a more
4865 -- useful error message (advising use of pragmas Implicit_Packing
4866 -- or Pack).
4868 if Is_Array_Type (E) then
4869 declare
4870 Ctyp : constant Entity_Id := Component_Type (E);
4871 Rsiz : constant Uint := RM_Size (Ctyp);
4872 SZ : constant Node_Id := Size_Clause (E);
4873 Btyp : constant Entity_Id := Base_Type (E);
4875 Lo : Node_Id;
4876 Hi : Node_Id;
4877 Indx : Node_Id;
4879 Num_Elmts : Uint;
4880 -- Number of elements in array
4882 begin
4883 -- Check enabling conditions. These are straightforward
4884 -- except for the test for a limited composite type. This
4885 -- eliminates the rare case of a array of limited components
4886 -- where there are issues of whether or not we can go ahead
4887 -- and pack the array (since we can't freely pack and unpack
4888 -- arrays if they are limited).
4890 -- Note that we check the root type explicitly because the
4891 -- whole point is we are doing this test before we have had
4892 -- a chance to freeze the base type (and it is that freeze
4893 -- action that causes stuff to be inherited).
4895 if Has_Size_Clause (E)
4896 and then Known_Static_RM_Size (E)
4897 and then not Is_Packed (E)
4898 and then not Has_Pragma_Pack (E)
4899 and then not Has_Component_Size_Clause (E)
4900 and then Known_Static_RM_Size (Ctyp)
4901 and then RM_Size (Ctyp) < 64
4902 and then not Is_Limited_Composite (E)
4903 and then not Is_Packed (Root_Type (E))
4904 and then not Has_Component_Size_Clause (Root_Type (E))
4905 and then not (CodePeer_Mode or GNATprove_Mode)
4906 then
4907 -- Compute number of elements in array
4909 Num_Elmts := Uint_1;
4910 Indx := First_Index (E);
4911 while Present (Indx) loop
4912 Get_Index_Bounds (Indx, Lo, Hi);
4914 if not (Compile_Time_Known_Value (Lo)
4915 and then
4916 Compile_Time_Known_Value (Hi))
4917 then
4918 goto No_Implicit_Packing;
4919 end if;
4921 Num_Elmts :=
4922 Num_Elmts *
4923 UI_Max (Uint_0,
4924 Expr_Value (Hi) - Expr_Value (Lo) + 1);
4925 Next_Index (Indx);
4926 end loop;
4928 -- What we are looking for here is the situation where
4929 -- the RM_Size given would be exactly right if there was
4930 -- a pragma Pack (resulting in the component size being
4931 -- the same as the RM_Size). Furthermore, the component
4932 -- type size must be an odd size (not a multiple of
4933 -- storage unit). If the component RM size is an exact
4934 -- number of storage units that is a power of two, the
4935 -- array is not packed and has a standard representation.
4937 if RM_Size (E) = Num_Elmts * Rsiz
4938 and then Rsiz mod System_Storage_Unit /= 0
4939 then
4940 -- For implicit packing mode, just set the component
4941 -- size silently.
4943 if Implicit_Packing then
4944 Set_Component_Size (Btyp, Rsiz);
4945 Set_Is_Bit_Packed_Array (Btyp);
4946 Set_Is_Packed (Btyp);
4947 Set_Has_Non_Standard_Rep (Btyp);
4949 -- Otherwise give an error message
4951 else
4952 Error_Msg_NE
4953 ("size given for& too small", SZ, E);
4954 Error_Msg_N -- CODEFIX
4955 ("\use explicit pragma Pack "
4956 & "or use pragma Implicit_Packing", SZ);
4957 end if;
4959 elsif RM_Size (E) = Num_Elmts * Rsiz
4960 and then Implicit_Packing
4961 and then
4962 (Rsiz / System_Storage_Unit = 1
4963 or else
4964 Rsiz / System_Storage_Unit = 2
4965 or else
4966 Rsiz / System_Storage_Unit = 4)
4967 then
4968 -- Not a packed array, but indicate the desired
4969 -- component size, for the back-end.
4971 Set_Component_Size (Btyp, Rsiz);
4972 end if;
4973 end if;
4974 end;
4975 end if;
4977 <<No_Implicit_Packing>>
4979 -- If ancestor subtype present, freeze that first. Note that this
4980 -- will also get the base type frozen. Need RM reference ???
4982 Atype := Ancestor_Subtype (E);
4984 if Present (Atype) then
4985 Freeze_And_Append (Atype, N, Result);
4987 -- No ancestor subtype present
4989 else
4990 -- See if we have a nearest ancestor that has a predicate.
4991 -- That catches the case of derived type with a predicate.
4992 -- Need RM reference here ???
4994 Atype := Nearest_Ancestor (E);
4996 if Present (Atype) and then Has_Predicates (Atype) then
4997 Freeze_And_Append (Atype, N, Result);
4998 end if;
5000 -- Freeze base type before freezing the entity (RM 13.14(15))
5002 if E /= Base_Type (E) then
5003 Freeze_And_Append (Base_Type (E), N, Result);
5004 end if;
5005 end if;
5007 -- A subtype inherits all the type-related representation aspects
5008 -- from its parents (RM 13.1(8)).
5010 Inherit_Aspects_At_Freeze_Point (E);
5012 -- For a derived type, freeze its parent type first (RM 13.14(15))
5014 elsif Is_Derived_Type (E) then
5015 Freeze_And_Append (Etype (E), N, Result);
5016 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5018 -- A derived type inherits each type-related representation aspect
5019 -- of its parent type that was directly specified before the
5020 -- declaration of the derived type (RM 13.1(15)).
5022 Inherit_Aspects_At_Freeze_Point (E);
5023 end if;
5025 -- Check for incompatible size and alignment for record type
5027 if Warn_On_Size_Alignment
5028 and then Is_Record_Type (E)
5029 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5031 -- If explicit Object_Size clause given assume that the programmer
5032 -- knows what he is doing, and expects the compiler behavior.
5034 and then not Has_Object_Size_Clause (E)
5036 -- Check for size not a multiple of alignment
5038 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5039 then
5040 declare
5041 SC : constant Node_Id := Size_Clause (E);
5042 AC : constant Node_Id := Alignment_Clause (E);
5043 Loc : Node_Id;
5044 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5046 begin
5047 if Present (SC) and then Present (AC) then
5049 -- Give a warning
5051 if Sloc (SC) > Sloc (AC) then
5052 Loc := SC;
5053 Error_Msg_NE
5054 ("??size is not a multiple of alignment for &", Loc, E);
5055 Error_Msg_Sloc := Sloc (AC);
5056 Error_Msg_Uint_1 := Alignment (E);
5057 Error_Msg_N ("\??alignment of ^ specified #", Loc);
5059 else
5060 Loc := AC;
5061 Error_Msg_NE
5062 ("??size is not a multiple of alignment for &", Loc, E);
5063 Error_Msg_Sloc := Sloc (SC);
5064 Error_Msg_Uint_1 := RM_Size (E);
5065 Error_Msg_N ("\??size of ^ specified #", Loc);
5066 end if;
5068 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5069 Error_Msg_N ("\??Object_Size will be increased to ^", Loc);
5070 end if;
5071 end;
5072 end if;
5074 -- Array type
5076 if Is_Array_Type (E) then
5077 Freeze_Array_Type (E);
5079 -- For a class-wide type, the corresponding specific type is
5080 -- frozen as well (RM 13.14(15))
5082 elsif Is_Class_Wide_Type (E) then
5083 Freeze_And_Append (Root_Type (E), N, Result);
5085 -- If the base type of the class-wide type is still incomplete,
5086 -- the class-wide remains unfrozen as well. This is legal when
5087 -- E is the formal of a primitive operation of some other type
5088 -- which is being frozen.
5090 if not Is_Frozen (Root_Type (E)) then
5091 Set_Is_Frozen (E, False);
5092 return Result;
5093 end if;
5095 -- The equivalent type associated with a class-wide subtype needs
5096 -- to be frozen to ensure that its layout is done.
5098 if Ekind (E) = E_Class_Wide_Subtype
5099 and then Present (Equivalent_Type (E))
5100 then
5101 Freeze_And_Append (Equivalent_Type (E), N, Result);
5102 end if;
5104 -- Generate an itype reference for a library-level class-wide type
5105 -- at the freeze point. Otherwise the first explicit reference to
5106 -- the type may appear in an inner scope which will be rejected by
5107 -- the back-end.
5109 if Is_Itype (E)
5110 and then Is_Compilation_Unit (Scope (E))
5111 then
5112 declare
5113 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5115 begin
5116 Set_Itype (Ref, E);
5118 -- From a gigi point of view, a class-wide subtype derives
5119 -- from its record equivalent type. As a result, the itype
5120 -- reference must appear after the freeze node of the
5121 -- equivalent type or gigi will reject the reference.
5123 if Ekind (E) = E_Class_Wide_Subtype
5124 and then Present (Equivalent_Type (E))
5125 then
5126 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5127 else
5128 Add_To_Result (Ref);
5129 end if;
5130 end;
5131 end if;
5133 -- For a record type or record subtype, freeze all component types
5134 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5135 -- using Is_Record_Type, because we don't want to attempt the freeze
5136 -- for the case of a private type with record extension (we will do
5137 -- that later when the full type is frozen).
5139 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype)
5140 and then not (Present (Scope (E))
5141 and then Is_Generic_Unit (Scope (E)))
5142 then
5143 Freeze_Record_Type (E);
5145 -- For a concurrent type, freeze corresponding record type. This does
5146 -- not correspond to any specific rule in the RM, but the record type
5147 -- is essentially part of the concurrent type. Also freeze all local
5148 -- entities. This includes record types created for entry parameter
5149 -- blocks and whatever local entities may appear in the private part.
5151 elsif Is_Concurrent_Type (E) then
5152 if Present (Corresponding_Record_Type (E)) then
5153 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5154 end if;
5156 Comp := First_Entity (E);
5157 while Present (Comp) loop
5158 if Is_Type (Comp) then
5159 Freeze_And_Append (Comp, N, Result);
5161 elsif (Ekind (Comp)) /= E_Function then
5163 -- The guard on the presence of the Etype seems to be needed
5164 -- for some CodePeer (-gnatcC) cases, but not clear why???
5166 if Present (Etype (Comp)) then
5167 if Is_Itype (Etype (Comp))
5168 and then Underlying_Type (Scope (Etype (Comp))) = E
5169 then
5170 Undelay_Type (Etype (Comp));
5171 end if;
5173 Freeze_And_Append (Etype (Comp), N, Result);
5174 end if;
5175 end if;
5177 Next_Entity (Comp);
5178 end loop;
5180 -- Private types are required to point to the same freeze node as
5181 -- their corresponding full views. The freeze node itself has to
5182 -- point to the partial view of the entity (because from the partial
5183 -- view, we can retrieve the full view, but not the reverse).
5184 -- However, in order to freeze correctly, we need to freeze the full
5185 -- view. If we are freezing at the end of a scope (or within the
5186 -- scope) of the private type, the partial and full views will have
5187 -- been swapped, the full view appears first in the entity chain and
5188 -- the swapping mechanism ensures that the pointers are properly set
5189 -- (on scope exit).
5191 -- If we encounter the partial view before the full view (e.g. when
5192 -- freezing from another scope), we freeze the full view, and then
5193 -- set the pointers appropriately since we cannot rely on swapping to
5194 -- fix things up (subtypes in an outer scope might not get swapped).
5196 -- If the full view is itself private, the above requirements apply
5197 -- to the underlying full view instead of the full view. But there is
5198 -- no swapping mechanism for the underlying full view so we need to
5199 -- set the pointers appropriately in both cases.
5201 elsif Is_Incomplete_Or_Private_Type (E)
5202 and then not Is_Generic_Type (E)
5203 then
5204 -- The construction of the dispatch table associated with library
5205 -- level tagged types forces freezing of all the primitives of the
5206 -- type, which may cause premature freezing of the partial view.
5207 -- For example:
5209 -- package Pkg is
5210 -- type T is tagged private;
5211 -- type DT is new T with private;
5212 -- procedure Prim (X : in out T; Y : in out DT'Class);
5213 -- private
5214 -- type T is tagged null record;
5215 -- Obj : T;
5216 -- type DT is new T with null record;
5217 -- end;
5219 -- In this case the type will be frozen later by the usual
5220 -- mechanism: an object declaration, an instantiation, or the
5221 -- end of a declarative part.
5223 if Is_Library_Level_Tagged_Type (E)
5224 and then not Present (Full_View (E))
5225 then
5226 Set_Is_Frozen (E, False);
5227 return Result;
5229 -- Case of full view present
5231 elsif Present (Full_View (E)) then
5233 -- If full view has already been frozen, then no further
5234 -- processing is required
5236 if Is_Frozen (Full_View (E)) then
5237 Set_Has_Delayed_Freeze (E, False);
5238 Set_Freeze_Node (E, Empty);
5240 -- Otherwise freeze full view and patch the pointers so that
5241 -- the freeze node will elaborate both views in the back end.
5242 -- However, if full view is itself private, freeze underlying
5243 -- full view instead and patch the pointers so that the freeze
5244 -- node will elaborate the three views in the back end.
5246 else
5247 declare
5248 Full : Entity_Id := Full_View (E);
5250 begin
5251 if Is_Private_Type (Full)
5252 and then Present (Underlying_Full_View (Full))
5253 then
5254 Full := Underlying_Full_View (Full);
5255 end if;
5257 Freeze_And_Append (Full, N, Result);
5259 if Full /= Full_View (E)
5260 and then Has_Delayed_Freeze (Full_View (E))
5261 then
5262 F_Node := Freeze_Node (Full);
5264 if Present (F_Node) then
5265 Set_Freeze_Node (Full_View (E), F_Node);
5266 Set_Entity (F_Node, Full_View (E));
5268 else
5269 Set_Has_Delayed_Freeze (Full_View (E), False);
5270 Set_Freeze_Node (Full_View (E), Empty);
5271 end if;
5272 end if;
5274 if Has_Delayed_Freeze (E) then
5275 F_Node := Freeze_Node (Full_View (E));
5277 if Present (F_Node) then
5278 Set_Freeze_Node (E, F_Node);
5279 Set_Entity (F_Node, E);
5281 else
5282 -- {Incomplete,Private}_Subtypes with Full_Views
5283 -- constrained by discriminants.
5285 Set_Has_Delayed_Freeze (E, False);
5286 Set_Freeze_Node (E, Empty);
5287 end if;
5288 end if;
5289 end;
5290 end if;
5292 Check_Debug_Info_Needed (E);
5294 -- AI-117 requires that the convention of a partial view be the
5295 -- same as the convention of the full view. Note that this is a
5296 -- recognized breach of privacy, but it's essential for logical
5297 -- consistency of representation, and the lack of a rule in
5298 -- RM95 was an oversight.
5300 Set_Convention (E, Convention (Full_View (E)));
5302 Set_Size_Known_At_Compile_Time (E,
5303 Size_Known_At_Compile_Time (Full_View (E)));
5305 -- Size information is copied from the full view to the
5306 -- incomplete or private view for consistency.
5308 -- We skip this is the full view is not a type. This is very
5309 -- strange of course, and can only happen as a result of
5310 -- certain illegalities, such as a premature attempt to derive
5311 -- from an incomplete type.
5313 if Is_Type (Full_View (E)) then
5314 Set_Size_Info (E, Full_View (E));
5315 Set_RM_Size (E, RM_Size (Full_View (E)));
5316 end if;
5318 return Result;
5320 -- Case of underlying full view present
5322 elsif Is_Private_Type (E)
5323 and then Present (Underlying_Full_View (E))
5324 then
5325 if not Is_Frozen (Underlying_Full_View (E)) then
5326 Freeze_And_Append (Underlying_Full_View (E), N, Result);
5327 end if;
5329 -- Patch the pointers so that the freeze node will elaborate
5330 -- both views in the back end.
5332 if Has_Delayed_Freeze (E) then
5333 F_Node := Freeze_Node (Underlying_Full_View (E));
5335 if Present (F_Node) then
5336 Set_Freeze_Node (E, F_Node);
5337 Set_Entity (F_Node, E);
5339 else
5340 Set_Has_Delayed_Freeze (E, False);
5341 Set_Freeze_Node (E, Empty);
5342 end if;
5343 end if;
5345 Check_Debug_Info_Needed (E);
5347 return Result;
5349 -- Case of no full view present. If entity is derived or subtype,
5350 -- it is safe to freeze, correctness depends on the frozen status
5351 -- of parent. Otherwise it is either premature usage, or a Taft
5352 -- amendment type, so diagnosis is at the point of use and the
5353 -- type might be frozen later.
5355 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
5356 null;
5358 else
5359 Set_Is_Frozen (E, False);
5360 return No_List;
5361 end if;
5363 -- For access subprogram, freeze types of all formals, the return
5364 -- type was already frozen, since it is the Etype of the function.
5365 -- Formal types can be tagged Taft amendment types, but otherwise
5366 -- they cannot be incomplete.
5368 elsif Ekind (E) = E_Subprogram_Type then
5369 Formal := First_Formal (E);
5370 while Present (Formal) loop
5371 if Ekind (Etype (Formal)) = E_Incomplete_Type
5372 and then No (Full_View (Etype (Formal)))
5373 and then not Is_Value_Type (Etype (Formal))
5374 then
5375 if Is_Tagged_Type (Etype (Formal)) then
5376 null;
5378 -- AI05-151: Incomplete types are allowed in access to
5379 -- subprogram specifications.
5381 elsif Ada_Version < Ada_2012 then
5382 Error_Msg_NE
5383 ("invalid use of incomplete type&", E, Etype (Formal));
5384 end if;
5385 end if;
5387 Freeze_And_Append (Etype (Formal), N, Result);
5388 Next_Formal (Formal);
5389 end loop;
5391 Freeze_Subprogram (E);
5393 -- For access to a protected subprogram, freeze the equivalent type
5394 -- (however this is not set if we are not generating code or if this
5395 -- is an anonymous type used just for resolution).
5397 elsif Is_Access_Protected_Subprogram_Type (E) then
5398 if Present (Equivalent_Type (E)) then
5399 Freeze_And_Append (Equivalent_Type (E), N, Result);
5400 end if;
5401 end if;
5403 -- Generic types are never seen by the back-end, and are also not
5404 -- processed by the expander (since the expander is turned off for
5405 -- generic processing), so we never need freeze nodes for them.
5407 if Is_Generic_Type (E) then
5408 return Result;
5409 end if;
5411 -- Some special processing for non-generic types to complete
5412 -- representation details not known till the freeze point.
5414 if Is_Fixed_Point_Type (E) then
5415 Freeze_Fixed_Point_Type (E);
5417 -- Some error checks required for ordinary fixed-point type. Defer
5418 -- these till the freeze-point since we need the small and range
5419 -- values. We only do these checks for base types
5421 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
5422 if Small_Value (E) < Ureal_2_M_80 then
5423 Error_Msg_Name_1 := Name_Small;
5424 Error_Msg_N
5425 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
5427 elsif Small_Value (E) > Ureal_2_80 then
5428 Error_Msg_Name_1 := Name_Small;
5429 Error_Msg_N
5430 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
5431 end if;
5433 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
5434 Error_Msg_Name_1 := Name_First;
5435 Error_Msg_N
5436 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
5437 end if;
5439 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
5440 Error_Msg_Name_1 := Name_Last;
5441 Error_Msg_N
5442 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
5443 end if;
5444 end if;
5446 elsif Is_Enumeration_Type (E) then
5447 Freeze_Enumeration_Type (E);
5449 elsif Is_Integer_Type (E) then
5450 Adjust_Esize_For_Alignment (E);
5452 if Is_Modular_Integer_Type (E)
5453 and then Warn_On_Suspicious_Modulus_Value
5454 then
5455 Check_Suspicious_Modulus (E);
5456 end if;
5458 -- The pool applies to named and anonymous access types, but not
5459 -- to subprogram and to internal types generated for 'Access
5460 -- references.
5462 elsif Is_Access_Type (E)
5463 and then not Is_Access_Subprogram_Type (E)
5464 and then Ekind (E) /= E_Access_Attribute_Type
5465 then
5466 -- If a pragma Default_Storage_Pool applies, and this type has no
5467 -- Storage_Pool or Storage_Size clause (which must have occurred
5468 -- before the freezing point), then use the default. This applies
5469 -- only to base types.
5471 -- None of this applies to access to subprograms, for which there
5472 -- are clearly no pools.
5474 if Present (Default_Pool)
5475 and then Is_Base_Type (E)
5476 and then not Has_Storage_Size_Clause (E)
5477 and then No (Associated_Storage_Pool (E))
5478 then
5479 -- Case of pragma Default_Storage_Pool (null)
5481 if Nkind (Default_Pool) = N_Null then
5482 Set_No_Pool_Assigned (E);
5484 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
5486 else
5487 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
5488 end if;
5489 end if;
5491 -- Check restriction for standard storage pool
5493 if No (Associated_Storage_Pool (E)) then
5494 Check_Restriction (No_Standard_Storage_Pools, E);
5495 end if;
5497 -- Deal with error message for pure access type. This is not an
5498 -- error in Ada 2005 if there is no pool (see AI-366).
5500 if Is_Pure_Unit_Access_Type (E)
5501 and then (Ada_Version < Ada_2005
5502 or else not No_Pool_Assigned (E))
5503 and then not Is_Generic_Unit (Scope (E))
5504 then
5505 Error_Msg_N ("named access type not allowed in pure unit", E);
5507 if Ada_Version >= Ada_2005 then
5508 Error_Msg_N
5509 ("\would be legal if Storage_Size of 0 given??", E);
5511 elsif No_Pool_Assigned (E) then
5512 Error_Msg_N
5513 ("\would be legal in Ada 2005??", E);
5515 else
5516 Error_Msg_N
5517 ("\would be legal in Ada 2005 if "
5518 & "Storage_Size of 0 given??", E);
5519 end if;
5520 end if;
5521 end if;
5523 -- Case of composite types
5525 if Is_Composite_Type (E) then
5527 -- AI-117 requires that all new primitives of a tagged type must
5528 -- inherit the convention of the full view of the type. Inherited
5529 -- and overriding operations are defined to inherit the convention
5530 -- of their parent or overridden subprogram (also specified in
5531 -- AI-117), which will have occurred earlier (in Derive_Subprogram
5532 -- and New_Overloaded_Entity). Here we set the convention of
5533 -- primitives that are still convention Ada, which will ensure
5534 -- that any new primitives inherit the type's convention. Class-
5535 -- wide types can have a foreign convention inherited from their
5536 -- specific type, but are excluded from this since they don't have
5537 -- any associated primitives.
5539 if Is_Tagged_Type (E)
5540 and then not Is_Class_Wide_Type (E)
5541 and then Convention (E) /= Convention_Ada
5542 then
5543 declare
5544 Prim_List : constant Elist_Id := Primitive_Operations (E);
5545 Prim : Elmt_Id;
5547 begin
5548 Prim := First_Elmt (Prim_List);
5549 while Present (Prim) loop
5550 if Convention (Node (Prim)) = Convention_Ada then
5551 Set_Convention (Node (Prim), Convention (E));
5552 end if;
5554 Next_Elmt (Prim);
5555 end loop;
5556 end;
5557 end if;
5559 -- If the type is a simple storage pool type, then this is where
5560 -- we attempt to locate and validate its Allocate, Deallocate, and
5561 -- Storage_Size operations (the first is required, and the latter
5562 -- two are optional). We also verify that the full type for a
5563 -- private type is allowed to be a simple storage pool type.
5565 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
5566 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
5567 then
5568 -- If the type is marked Has_Private_Declaration, then this is
5569 -- a full type for a private type that was specified with the
5570 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
5571 -- pragma is allowed for the full type (for example, it can't
5572 -- be an array type, or a nonlimited record type).
5574 if Has_Private_Declaration (E) then
5575 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
5576 and then not Is_Private_Type (E)
5577 then
5578 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
5579 Error_Msg_N
5580 ("pragma% can only apply to full type that is an " &
5581 "explicitly limited type", E);
5582 end if;
5583 end if;
5585 Validate_Simple_Pool_Ops : declare
5586 Pool_Type : Entity_Id renames E;
5587 Address_Type : constant Entity_Id := RTE (RE_Address);
5588 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
5590 procedure Validate_Simple_Pool_Op_Formal
5591 (Pool_Op : Entity_Id;
5592 Pool_Op_Formal : in out Entity_Id;
5593 Expected_Mode : Formal_Kind;
5594 Expected_Type : Entity_Id;
5595 Formal_Name : String;
5596 OK_Formal : in out Boolean);
5597 -- Validate one formal Pool_Op_Formal of the candidate pool
5598 -- operation Pool_Op. The formal must be of Expected_Type
5599 -- and have mode Expected_Mode. OK_Formal will be set to
5600 -- False if the formal doesn't match. If OK_Formal is False
5601 -- on entry, then the formal will effectively be ignored
5602 -- (because validation of the pool op has already failed).
5603 -- Upon return, Pool_Op_Formal will be updated to the next
5604 -- formal, if any.
5606 procedure Validate_Simple_Pool_Operation
5607 (Op_Name : Name_Id);
5608 -- Search for and validate a simple pool operation with the
5609 -- name Op_Name. If the name is Allocate, then there must be
5610 -- exactly one such primitive operation for the simple pool
5611 -- type. If the name is Deallocate or Storage_Size, then
5612 -- there can be at most one such primitive operation. The
5613 -- profile of the located primitive must conform to what
5614 -- is expected for each operation.
5616 ------------------------------------
5617 -- Validate_Simple_Pool_Op_Formal --
5618 ------------------------------------
5620 procedure Validate_Simple_Pool_Op_Formal
5621 (Pool_Op : Entity_Id;
5622 Pool_Op_Formal : in out Entity_Id;
5623 Expected_Mode : Formal_Kind;
5624 Expected_Type : Entity_Id;
5625 Formal_Name : String;
5626 OK_Formal : in out Boolean)
5628 begin
5629 -- If OK_Formal is False on entry, then simply ignore
5630 -- the formal, because an earlier formal has already
5631 -- been flagged.
5633 if not OK_Formal then
5634 return;
5636 -- If no formal is passed in, then issue an error for a
5637 -- missing formal.
5639 elsif not Present (Pool_Op_Formal) then
5640 Error_Msg_NE
5641 ("simple storage pool op missing formal " &
5642 Formal_Name & " of type&", Pool_Op, Expected_Type);
5643 OK_Formal := False;
5645 return;
5646 end if;
5648 if Etype (Pool_Op_Formal) /= Expected_Type then
5650 -- If the pool type was expected for this formal, then
5651 -- this will not be considered a candidate operation
5652 -- for the simple pool, so we unset OK_Formal so that
5653 -- the op and any later formals will be ignored.
5655 if Expected_Type = Pool_Type then
5656 OK_Formal := False;
5658 return;
5660 else
5661 Error_Msg_NE
5662 ("wrong type for formal " & Formal_Name &
5663 " of simple storage pool op; expected type&",
5664 Pool_Op_Formal, Expected_Type);
5665 end if;
5666 end if;
5668 -- Issue error if formal's mode is not the expected one
5670 if Ekind (Pool_Op_Formal) /= Expected_Mode then
5671 Error_Msg_N
5672 ("wrong mode for formal of simple storage pool op",
5673 Pool_Op_Formal);
5674 end if;
5676 -- Advance to the next formal
5678 Next_Formal (Pool_Op_Formal);
5679 end Validate_Simple_Pool_Op_Formal;
5681 ------------------------------------
5682 -- Validate_Simple_Pool_Operation --
5683 ------------------------------------
5685 procedure Validate_Simple_Pool_Operation
5686 (Op_Name : Name_Id)
5688 Op : Entity_Id;
5689 Found_Op : Entity_Id := Empty;
5690 Formal : Entity_Id;
5691 Is_OK : Boolean;
5693 begin
5694 pragma Assert
5695 (Nam_In (Op_Name, Name_Allocate,
5696 Name_Deallocate,
5697 Name_Storage_Size));
5699 Error_Msg_Name_1 := Op_Name;
5701 -- For each homonym declared immediately in the scope
5702 -- of the simple storage pool type, determine whether
5703 -- the homonym is an operation of the pool type, and,
5704 -- if so, check that its profile is as expected for
5705 -- a simple pool operation of that name.
5707 Op := Get_Name_Entity_Id (Op_Name);
5708 while Present (Op) loop
5709 if Ekind_In (Op, E_Function, E_Procedure)
5710 and then Scope (Op) = Current_Scope
5711 then
5712 Formal := First_Entity (Op);
5714 Is_OK := True;
5716 -- The first parameter must be of the pool type
5717 -- in order for the operation to qualify.
5719 if Op_Name = Name_Storage_Size then
5720 Validate_Simple_Pool_Op_Formal
5721 (Op, Formal, E_In_Parameter, Pool_Type,
5722 "Pool", Is_OK);
5723 else
5724 Validate_Simple_Pool_Op_Formal
5725 (Op, Formal, E_In_Out_Parameter, Pool_Type,
5726 "Pool", Is_OK);
5727 end if;
5729 -- If another operation with this name has already
5730 -- been located for the type, then flag an error,
5731 -- since we only allow the type to have a single
5732 -- such primitive.
5734 if Present (Found_Op) and then Is_OK then
5735 Error_Msg_NE
5736 ("only one % operation allowed for " &
5737 "simple storage pool type&", Op, Pool_Type);
5738 end if;
5740 -- In the case of Allocate and Deallocate, a formal
5741 -- of type System.Address is required.
5743 if Op_Name = Name_Allocate then
5744 Validate_Simple_Pool_Op_Formal
5745 (Op, Formal, E_Out_Parameter,
5746 Address_Type, "Storage_Address", Is_OK);
5748 elsif Op_Name = Name_Deallocate then
5749 Validate_Simple_Pool_Op_Formal
5750 (Op, Formal, E_In_Parameter,
5751 Address_Type, "Storage_Address", Is_OK);
5752 end if;
5754 -- In the case of Allocate and Deallocate, formals
5755 -- of type Storage_Count are required as the third
5756 -- and fourth parameters.
5758 if Op_Name /= Name_Storage_Size then
5759 Validate_Simple_Pool_Op_Formal
5760 (Op, Formal, E_In_Parameter,
5761 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
5762 Validate_Simple_Pool_Op_Formal
5763 (Op, Formal, E_In_Parameter,
5764 Stg_Cnt_Type, "Alignment", Is_OK);
5765 end if;
5767 -- If no mismatched formals have been found (Is_OK)
5768 -- and no excess formals are present, then this
5769 -- operation has been validated, so record it.
5771 if not Present (Formal) and then Is_OK then
5772 Found_Op := Op;
5773 end if;
5774 end if;
5776 Op := Homonym (Op);
5777 end loop;
5779 -- There must be a valid Allocate operation for the type,
5780 -- so issue an error if none was found.
5782 if Op_Name = Name_Allocate
5783 and then not Present (Found_Op)
5784 then
5785 Error_Msg_N ("missing % operation for simple " &
5786 "storage pool type", Pool_Type);
5788 elsif Present (Found_Op) then
5790 -- Simple pool operations can't be abstract
5792 if Is_Abstract_Subprogram (Found_Op) then
5793 Error_Msg_N
5794 ("simple storage pool operation must not be " &
5795 "abstract", Found_Op);
5796 end if;
5798 -- The Storage_Size operation must be a function with
5799 -- Storage_Count as its result type.
5801 if Op_Name = Name_Storage_Size then
5802 if Ekind (Found_Op) = E_Procedure then
5803 Error_Msg_N
5804 ("% operation must be a function", Found_Op);
5806 elsif Etype (Found_Op) /= Stg_Cnt_Type then
5807 Error_Msg_NE
5808 ("wrong result type for%, expected type&",
5809 Found_Op, Stg_Cnt_Type);
5810 end if;
5812 -- Allocate and Deallocate must be procedures
5814 elsif Ekind (Found_Op) = E_Function then
5815 Error_Msg_N
5816 ("% operation must be a procedure", Found_Op);
5817 end if;
5818 end if;
5819 end Validate_Simple_Pool_Operation;
5821 -- Start of processing for Validate_Simple_Pool_Ops
5823 begin
5824 Validate_Simple_Pool_Operation (Name_Allocate);
5825 Validate_Simple_Pool_Operation (Name_Deallocate);
5826 Validate_Simple_Pool_Operation (Name_Storage_Size);
5827 end Validate_Simple_Pool_Ops;
5828 end if;
5829 end if;
5831 -- Now that all types from which E may depend are frozen, see if the
5832 -- size is known at compile time, if it must be unsigned, or if
5833 -- strict alignment is required
5835 Check_Compile_Time_Size (E);
5836 Check_Unsigned_Type (E);
5838 if Base_Type (E) = E then
5839 Check_Strict_Alignment (E);
5840 end if;
5842 -- Do not allow a size clause for a type which does not have a size
5843 -- that is known at compile time
5845 if Has_Size_Clause (E)
5846 and then not Size_Known_At_Compile_Time (E)
5847 then
5848 -- Suppress this message if errors posted on E, even if we are
5849 -- in all errors mode, since this is often a junk message
5851 if not Error_Posted (E) then
5852 Error_Msg_N
5853 ("size clause not allowed for variable length type",
5854 Size_Clause (E));
5855 end if;
5856 end if;
5858 -- Now we set/verify the representation information, in particular
5859 -- the size and alignment values. This processing is not required for
5860 -- generic types, since generic types do not play any part in code
5861 -- generation, and so the size and alignment values for such types
5862 -- are irrelevant. Ditto for types declared within a generic unit,
5863 -- which may have components that depend on generic parameters, and
5864 -- that will be recreated in an instance.
5866 if Inside_A_Generic then
5867 null;
5869 -- Otherwise we call the layout procedure
5871 else
5872 Layout_Type (E);
5873 end if;
5875 -- If this is an access to subprogram whose designated type is itself
5876 -- a subprogram type, the return type of this anonymous subprogram
5877 -- type must be decorated as well.
5879 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
5880 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
5881 then
5882 Layout_Type (Etype (Designated_Type (E)));
5883 end if;
5885 -- If the type has a Defaut_Value/Default_Component_Value aspect,
5886 -- this is where we analye the expression (after the type is frozen,
5887 -- since in the case of Default_Value, we are analyzing with the
5888 -- type itself, and we treat Default_Component_Value similarly for
5889 -- the sake of uniformity).
5891 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
5892 declare
5893 Nam : Name_Id;
5894 Exp : Node_Id;
5895 Typ : Entity_Id;
5897 begin
5898 if Is_Scalar_Type (E) then
5899 Nam := Name_Default_Value;
5900 Typ := E;
5901 Exp := Default_Aspect_Value (Typ);
5902 else
5903 Nam := Name_Default_Component_Value;
5904 Typ := Component_Type (E);
5905 Exp := Default_Aspect_Component_Value (E);
5906 end if;
5908 Analyze_And_Resolve (Exp, Typ);
5910 if Etype (Exp) /= Any_Type then
5911 if not Is_OK_Static_Expression (Exp) then
5912 Error_Msg_Name_1 := Nam;
5913 Flag_Non_Static_Expr
5914 ("aspect% requires static expression", Exp);
5915 end if;
5916 end if;
5917 end;
5918 end if;
5920 -- End of freeze processing for type entities
5921 end if;
5923 -- Here is where we logically freeze the current entity. If it has a
5924 -- freeze node, then this is the point at which the freeze node is
5925 -- linked into the result list.
5927 if Has_Delayed_Freeze (E) then
5929 -- If a freeze node is already allocated, use it, otherwise allocate
5930 -- a new one. The preallocation happens in the case of anonymous base
5931 -- types, where we preallocate so that we can set First_Subtype_Link.
5932 -- Note that we reset the Sloc to the current freeze location.
5934 if Present (Freeze_Node (E)) then
5935 F_Node := Freeze_Node (E);
5936 Set_Sloc (F_Node, Loc);
5938 else
5939 F_Node := New_Node (N_Freeze_Entity, Loc);
5940 Set_Freeze_Node (E, F_Node);
5941 Set_Access_Types_To_Process (F_Node, No_Elist);
5942 Set_TSS_Elist (F_Node, No_Elist);
5943 Set_Actions (F_Node, No_List);
5944 end if;
5946 Set_Entity (F_Node, E);
5947 Add_To_Result (F_Node);
5949 -- A final pass over record types with discriminants. If the type
5950 -- has an incomplete declaration, there may be constrained access
5951 -- subtypes declared elsewhere, which do not depend on the discrimi-
5952 -- nants of the type, and which are used as component types (i.e.
5953 -- the full view is a recursive type). The designated types of these
5954 -- subtypes can only be elaborated after the type itself, and they
5955 -- need an itype reference.
5957 if Ekind (E) = E_Record_Type
5958 and then Has_Discriminants (E)
5959 then
5960 declare
5961 Comp : Entity_Id;
5962 IR : Node_Id;
5963 Typ : Entity_Id;
5965 begin
5966 Comp := First_Component (E);
5967 while Present (Comp) loop
5968 Typ := Etype (Comp);
5970 if Ekind (Comp) = E_Component
5971 and then Is_Access_Type (Typ)
5972 and then Scope (Typ) /= E
5973 and then Base_Type (Designated_Type (Typ)) = E
5974 and then Is_Itype (Designated_Type (Typ))
5975 then
5976 IR := Make_Itype_Reference (Sloc (Comp));
5977 Set_Itype (IR, Designated_Type (Typ));
5978 Append (IR, Result);
5979 end if;
5981 Next_Component (Comp);
5982 end loop;
5983 end;
5984 end if;
5985 end if;
5987 -- When a type is frozen, the first subtype of the type is frozen as
5988 -- well (RM 13.14(15)). This has to be done after freezing the type,
5989 -- since obviously the first subtype depends on its own base type.
5991 if Is_Type (E) then
5992 Freeze_And_Append (First_Subtype (E), N, Result);
5994 -- If we just froze a tagged non-class wide record, then freeze the
5995 -- corresponding class-wide type. This must be done after the tagged
5996 -- type itself is frozen, because the class-wide type refers to the
5997 -- tagged type which generates the class.
5999 if Is_Tagged_Type (E)
6000 and then not Is_Class_Wide_Type (E)
6001 and then Present (Class_Wide_Type (E))
6002 then
6003 Freeze_And_Append (Class_Wide_Type (E), N, Result);
6004 end if;
6005 end if;
6007 Check_Debug_Info_Needed (E);
6009 -- Special handling for subprograms
6011 if Is_Subprogram (E) then
6013 -- If subprogram has address clause then reset Is_Public flag, since
6014 -- we do not want the backend to generate external references.
6016 if Present (Address_Clause (E))
6017 and then not Is_Library_Level_Entity (E)
6018 then
6019 Set_Is_Public (E, False);
6020 end if;
6021 end if;
6023 return Result;
6024 end Freeze_Entity;
6026 -----------------------------
6027 -- Freeze_Enumeration_Type --
6028 -----------------------------
6030 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6031 begin
6032 -- By default, if no size clause is present, an enumeration type with
6033 -- Convention C is assumed to interface to a C enum, and has integer
6034 -- size. This applies to types. For subtypes, verify that its base
6035 -- type has no size clause either. Treat other foreign conventions
6036 -- in the same way, and also make sure alignment is set right.
6038 if Has_Foreign_Convention (Typ)
6039 and then not Has_Size_Clause (Typ)
6040 and then not Has_Size_Clause (Base_Type (Typ))
6041 and then Esize (Typ) < Standard_Integer_Size
6043 -- Don't do this if Short_Enums on target
6045 and then not Target_Short_Enums
6046 then
6047 Init_Esize (Typ, Standard_Integer_Size);
6048 Set_Alignment (Typ, Alignment (Standard_Integer));
6050 -- Normal Ada case or size clause present or not Long_C_Enums on target
6052 else
6053 -- If the enumeration type interfaces to C, and it has a size clause
6054 -- that specifies less than int size, it warrants a warning. The
6055 -- user may intend the C type to be an enum or a char, so this is
6056 -- not by itself an error that the Ada compiler can detect, but it
6057 -- it is a worth a heads-up. For Boolean and Character types we
6058 -- assume that the programmer has the proper C type in mind.
6060 if Convention (Typ) = Convention_C
6061 and then Has_Size_Clause (Typ)
6062 and then Esize (Typ) /= Esize (Standard_Integer)
6063 and then not Is_Boolean_Type (Typ)
6064 and then not Is_Character_Type (Typ)
6066 -- Don't do this if Short_Enums on target
6068 and then not Target_Short_Enums
6069 then
6070 Error_Msg_N
6071 ("C enum types have the size of a C int??", Size_Clause (Typ));
6072 end if;
6074 Adjust_Esize_For_Alignment (Typ);
6075 end if;
6076 end Freeze_Enumeration_Type;
6078 -----------------------
6079 -- Freeze_Expression --
6080 -----------------------
6082 procedure Freeze_Expression (N : Node_Id) is
6083 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6084 Typ : Entity_Id;
6085 Nam : Entity_Id;
6086 Desig_Typ : Entity_Id;
6087 P : Node_Id;
6088 Parent_P : Node_Id;
6090 Freeze_Outside : Boolean := False;
6091 -- This flag is set true if the entity must be frozen outside the
6092 -- current subprogram. This happens in the case of expander generated
6093 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6094 -- not freeze all entities like other bodies, but which nevertheless
6095 -- may reference entities that have to be frozen before the body and
6096 -- obviously cannot be frozen inside the body.
6098 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6099 -- If the expression is an array aggregate, the type of the component
6100 -- expressions is also frozen. If the component type is an access type
6101 -- and the expressions include allocators, the designed type is frozen
6102 -- as well.
6104 function In_Expanded_Body (N : Node_Id) return Boolean;
6105 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6106 -- it is the handled statement sequence of an expander-generated
6107 -- subprogram (init proc, stream subprogram, or renaming as body).
6108 -- If so, this is not a freezing context.
6110 -----------------------------------------
6111 -- Find_Aggregate_Component_Desig_Type --
6112 -----------------------------------------
6114 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6115 Assoc : Node_Id;
6116 Exp : Node_Id;
6118 begin
6119 if Present (Expressions (N)) then
6120 Exp := First (Expressions (N));
6121 while Present (Exp) loop
6122 if Nkind (Exp) = N_Allocator then
6123 return Designated_Type (Component_Type (Etype (N)));
6124 end if;
6126 Next (Exp);
6127 end loop;
6128 end if;
6130 if Present (Component_Associations (N)) then
6131 Assoc := First (Component_Associations (N));
6132 while Present (Assoc) loop
6133 if Nkind (Expression (Assoc)) = N_Allocator then
6134 return Designated_Type (Component_Type (Etype (N)));
6135 end if;
6137 Next (Assoc);
6138 end loop;
6139 end if;
6141 return Empty;
6142 end Find_Aggregate_Component_Desig_Type;
6144 ----------------------
6145 -- In_Expanded_Body --
6146 ----------------------
6148 function In_Expanded_Body (N : Node_Id) return Boolean is
6149 P : Node_Id;
6150 Id : Entity_Id;
6152 begin
6153 if Nkind (N) = N_Subprogram_Body then
6154 P := N;
6155 else
6156 P := Parent (N);
6157 end if;
6159 if Nkind (P) /= N_Subprogram_Body then
6160 return False;
6162 else
6163 Id := Defining_Unit_Name (Specification (P));
6165 -- The following are expander-created bodies, or bodies that
6166 -- are not freeze points.
6168 if Nkind (Id) = N_Defining_Identifier
6169 and then (Is_Init_Proc (Id)
6170 or else Is_TSS (Id, TSS_Stream_Input)
6171 or else Is_TSS (Id, TSS_Stream_Output)
6172 or else Is_TSS (Id, TSS_Stream_Read)
6173 or else Is_TSS (Id, TSS_Stream_Write)
6174 or else Nkind_In (Original_Node (P),
6175 N_Subprogram_Renaming_Declaration,
6176 N_Expression_Function))
6177 then
6178 return True;
6179 else
6180 return False;
6181 end if;
6182 end if;
6183 end In_Expanded_Body;
6185 -- Start of processing for Freeze_Expression
6187 begin
6188 -- Immediate return if freezing is inhibited. This flag is set by the
6189 -- analyzer to stop freezing on generated expressions that would cause
6190 -- freezing if they were in the source program, but which are not
6191 -- supposed to freeze, since they are created.
6193 if Must_Not_Freeze (N) then
6194 return;
6195 end if;
6197 -- If expression is non-static, then it does not freeze in a default
6198 -- expression, see section "Handling of Default Expressions" in the
6199 -- spec of package Sem for further details. Note that we have to make
6200 -- sure that we actually have a real expression (if we have a subtype
6201 -- indication, we can't test Is_OK_Static_Expression). However, we
6202 -- exclude the case of the prefix of an attribute of a static scalar
6203 -- subtype from this early return, because static subtype attributes
6204 -- should always cause freezing, even in default expressions, but
6205 -- the attribute may not have been marked as static yet (because in
6206 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
6207 -- Freeze_Expression on the prefix).
6209 if In_Spec_Exp
6210 and then Nkind (N) in N_Subexpr
6211 and then not Is_OK_Static_Expression (N)
6212 and then (Nkind (Parent (N)) /= N_Attribute_Reference
6213 or else not (Is_Entity_Name (N)
6214 and then Is_Type (Entity (N))
6215 and then Is_OK_Static_Subtype (Entity (N))))
6216 then
6217 return;
6218 end if;
6220 -- Freeze type of expression if not frozen already
6222 Typ := Empty;
6224 if Nkind (N) in N_Has_Etype then
6225 if not Is_Frozen (Etype (N)) then
6226 Typ := Etype (N);
6228 -- Base type may be an derived numeric type that is frozen at
6229 -- the point of declaration, but first_subtype is still unfrozen.
6231 elsif not Is_Frozen (First_Subtype (Etype (N))) then
6232 Typ := First_Subtype (Etype (N));
6233 end if;
6234 end if;
6236 -- For entity name, freeze entity if not frozen already. A special
6237 -- exception occurs for an identifier that did not come from source.
6238 -- We don't let such identifiers freeze a non-internal entity, i.e.
6239 -- an entity that did come from source, since such an identifier was
6240 -- generated by the expander, and cannot have any semantic effect on
6241 -- the freezing semantics. For example, this stops the parameter of
6242 -- an initialization procedure from freezing the variable.
6244 if Is_Entity_Name (N)
6245 and then not Is_Frozen (Entity (N))
6246 and then (Nkind (N) /= N_Identifier
6247 or else Comes_From_Source (N)
6248 or else not Comes_From_Source (Entity (N)))
6249 then
6250 Nam := Entity (N);
6252 if Present (Nam) and then Ekind (Nam) = E_Function then
6253 Check_Expression_Function (N, Nam);
6254 end if;
6256 else
6257 Nam := Empty;
6258 end if;
6260 -- For an allocator freeze designated type if not frozen already
6262 -- For an aggregate whose component type is an access type, freeze the
6263 -- designated type now, so that its freeze does not appear within the
6264 -- loop that might be created in the expansion of the aggregate. If the
6265 -- designated type is a private type without full view, the expression
6266 -- cannot contain an allocator, so the type is not frozen.
6268 -- For a function, we freeze the entity when the subprogram declaration
6269 -- is frozen, but a function call may appear in an initialization proc.
6270 -- before the declaration is frozen. We need to generate the extra
6271 -- formals, if any, to ensure that the expansion of the call includes
6272 -- the proper actuals. This only applies to Ada subprograms, not to
6273 -- imported ones.
6275 Desig_Typ := Empty;
6277 case Nkind (N) is
6278 when N_Allocator =>
6279 Desig_Typ := Designated_Type (Etype (N));
6281 when N_Aggregate =>
6282 if Is_Array_Type (Etype (N))
6283 and then Is_Access_Type (Component_Type (Etype (N)))
6284 then
6286 -- Check whether aggregate includes allocators.
6288 Desig_Typ := Find_Aggregate_Component_Desig_Type;
6289 end if;
6291 when N_Selected_Component |
6292 N_Indexed_Component |
6293 N_Slice =>
6295 if Is_Access_Type (Etype (Prefix (N))) then
6296 Desig_Typ := Designated_Type (Etype (Prefix (N)));
6297 end if;
6299 when N_Identifier =>
6300 if Present (Nam)
6301 and then Ekind (Nam) = E_Function
6302 and then Nkind (Parent (N)) = N_Function_Call
6303 and then Convention (Nam) = Convention_Ada
6304 then
6305 Create_Extra_Formals (Nam);
6306 end if;
6308 when others =>
6309 null;
6310 end case;
6312 if Desig_Typ /= Empty
6313 and then (Is_Frozen (Desig_Typ)
6314 or else (not Is_Fully_Defined (Desig_Typ)))
6315 then
6316 Desig_Typ := Empty;
6317 end if;
6319 -- All done if nothing needs freezing
6321 if No (Typ)
6322 and then No (Nam)
6323 and then No (Desig_Typ)
6324 then
6325 return;
6326 end if;
6328 -- Examine the enclosing context by climbing the parent chain. The
6329 -- traversal serves two purposes - to detect scenarios where freezeing
6330 -- is not needed and to find the proper insertion point for the freeze
6331 -- nodes. Although somewhat similar to Insert_Actions, this traversal
6332 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
6333 -- the tree may result in types being frozen too early.
6335 P := N;
6336 loop
6337 Parent_P := Parent (P);
6339 -- If we don't have a parent, then we are not in a well-formed tree.
6340 -- This is an unusual case, but there are some legitimate situations
6341 -- in which this occurs, notably when the expressions in the range of
6342 -- a type declaration are resolved. We simply ignore the freeze
6343 -- request in this case. Is this right ???
6345 if No (Parent_P) then
6346 return;
6347 end if;
6349 -- See if we have got to an appropriate point in the tree
6351 case Nkind (Parent_P) is
6353 -- A special test for the exception of (RM 13.14(8)) for the case
6354 -- of per-object expressions (RM 3.8(18)) occurring in component
6355 -- definition or a discrete subtype definition. Note that we test
6356 -- for a component declaration which includes both cases we are
6357 -- interested in, and furthermore the tree does not have explicit
6358 -- nodes for either of these two constructs.
6360 when N_Component_Declaration =>
6362 -- The case we want to test for here is an identifier that is
6363 -- a per-object expression, this is either a discriminant that
6364 -- appears in a context other than the component declaration
6365 -- or it is a reference to the type of the enclosing construct.
6367 -- For either of these cases, we skip the freezing
6369 if not In_Spec_Expression
6370 and then Nkind (N) = N_Identifier
6371 and then (Present (Entity (N)))
6372 then
6373 -- We recognize the discriminant case by just looking for
6374 -- a reference to a discriminant. It can only be one for
6375 -- the enclosing construct. Skip freezing in this case.
6377 if Ekind (Entity (N)) = E_Discriminant then
6378 return;
6380 -- For the case of a reference to the enclosing record,
6381 -- (or task or protected type), we look for a type that
6382 -- matches the current scope.
6384 elsif Entity (N) = Current_Scope then
6385 return;
6386 end if;
6387 end if;
6389 -- If we have an enumeration literal that appears as the choice in
6390 -- the aggregate of an enumeration representation clause, then
6391 -- freezing does not occur (RM 13.14(10)).
6393 when N_Enumeration_Representation_Clause =>
6395 -- The case we are looking for is an enumeration literal
6397 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
6398 and then Is_Enumeration_Type (Etype (N))
6399 then
6400 -- If enumeration literal appears directly as the choice,
6401 -- do not freeze (this is the normal non-overloaded case)
6403 if Nkind (Parent (N)) = N_Component_Association
6404 and then First (Choices (Parent (N))) = N
6405 then
6406 return;
6408 -- If enumeration literal appears as the name of function
6409 -- which is the choice, then also do not freeze. This
6410 -- happens in the overloaded literal case, where the
6411 -- enumeration literal is temporarily changed to a function
6412 -- call for overloading analysis purposes.
6414 elsif Nkind (Parent (N)) = N_Function_Call
6415 and then
6416 Nkind (Parent (Parent (N))) = N_Component_Association
6417 and then
6418 First (Choices (Parent (Parent (N)))) = Parent (N)
6419 then
6420 return;
6421 end if;
6422 end if;
6424 -- Normally if the parent is a handled sequence of statements,
6425 -- then the current node must be a statement, and that is an
6426 -- appropriate place to insert a freeze node.
6428 when N_Handled_Sequence_Of_Statements =>
6430 -- An exception occurs when the sequence of statements is for
6431 -- an expander generated body that did not do the usual freeze
6432 -- all operation. In this case we usually want to freeze
6433 -- outside this body, not inside it, and we skip past the
6434 -- subprogram body that we are inside.
6436 if In_Expanded_Body (Parent_P) then
6437 declare
6438 Subp : constant Node_Id := Parent (Parent_P);
6439 Spec : Entity_Id;
6441 begin
6442 -- Freeze the entity only when it is declared inside the
6443 -- body of the expander generated procedure. This case
6444 -- is recognized by the scope of the entity or its type,
6445 -- which is either the spec for some enclosing body, or
6446 -- (in the case of init_procs, for which there are no
6447 -- separate specs) the current scope.
6449 if Nkind (Subp) = N_Subprogram_Body then
6450 Spec := Corresponding_Spec (Subp);
6452 if (Present (Typ) and then Scope (Typ) = Spec)
6453 or else
6454 (Present (Nam) and then Scope (Nam) = Spec)
6455 then
6456 exit;
6458 elsif Present (Typ)
6459 and then Scope (Typ) = Current_Scope
6460 and then Defining_Entity (Subp) = Current_Scope
6461 then
6462 exit;
6463 end if;
6464 end if;
6466 -- An expression function may act as a completion of
6467 -- a function declaration. As such, it can reference
6468 -- entities declared between the two views:
6470 -- Hidden []; -- 1
6471 -- function F return ...;
6472 -- private
6473 -- function Hidden return ...;
6474 -- function F return ... is (Hidden); -- 2
6476 -- Refering to the example above, freezing the expression
6477 -- of F (2) would place Hidden's freeze node (1) in the
6478 -- wrong place. Avoid explicit freezing and let the usual
6479 -- scenarios do the job - for example, reaching the end
6480 -- of the private declarations, or a call to F.
6482 if Nkind (Original_Node (Subp)) =
6483 N_Expression_Function
6484 then
6485 null;
6487 -- Freeze outside the body
6489 else
6490 Parent_P := Parent (Parent_P);
6491 Freeze_Outside := True;
6492 end if;
6493 end;
6495 -- Here if normal case where we are in handled statement
6496 -- sequence and want to do the insertion right there.
6498 else
6499 exit;
6500 end if;
6502 -- If parent is a body or a spec or a block, then the current node
6503 -- is a statement or declaration and we can insert the freeze node
6504 -- before it.
6506 when N_Block_Statement |
6507 N_Entry_Body |
6508 N_Package_Body |
6509 N_Package_Specification |
6510 N_Protected_Body |
6511 N_Subprogram_Body |
6512 N_Task_Body => exit;
6514 -- The expander is allowed to define types in any statements list,
6515 -- so any of the following parent nodes also mark a freezing point
6516 -- if the actual node is in a list of statements or declarations.
6518 when N_Abortable_Part |
6519 N_Accept_Alternative |
6520 N_And_Then |
6521 N_Case_Statement_Alternative |
6522 N_Compilation_Unit_Aux |
6523 N_Conditional_Entry_Call |
6524 N_Delay_Alternative |
6525 N_Elsif_Part |
6526 N_Entry_Call_Alternative |
6527 N_Exception_Handler |
6528 N_Extended_Return_Statement |
6529 N_Freeze_Entity |
6530 N_If_Statement |
6531 N_Or_Else |
6532 N_Selective_Accept |
6533 N_Triggering_Alternative =>
6535 exit when Is_List_Member (P);
6537 -- Freeze nodes produced by an expression coming from the Actions
6538 -- list of a N_Expression_With_Actions node must remain within the
6539 -- Actions list. Inserting the freeze nodes further up the tree
6540 -- may lead to use before declaration issues in the case of array
6541 -- types.
6543 when N_Expression_With_Actions =>
6544 if Is_List_Member (P)
6545 and then List_Containing (P) = Actions (Parent_P)
6546 then
6547 exit;
6548 end if;
6550 -- Note: N_Loop_Statement is a special case. A type that appears
6551 -- in the source can never be frozen in a loop (this occurs only
6552 -- because of a loop expanded by the expander), so we keep on
6553 -- going. Otherwise we terminate the search. Same is true of any
6554 -- entity which comes from source. (if they have predefined type,
6555 -- that type does not appear to come from source, but the entity
6556 -- should not be frozen here).
6558 when N_Loop_Statement =>
6559 exit when not Comes_From_Source (Etype (N))
6560 and then (No (Nam) or else not Comes_From_Source (Nam));
6562 -- For all other cases, keep looking at parents
6564 when others =>
6565 null;
6566 end case;
6568 -- We fall through the case if we did not yet find the proper
6569 -- place in the free for inserting the freeze node, so climb.
6571 P := Parent_P;
6572 end loop;
6574 -- If the expression appears in a record or an initialization procedure,
6575 -- the freeze nodes are collected and attached to the current scope, to
6576 -- be inserted and analyzed on exit from the scope, to insure that
6577 -- generated entities appear in the correct scope. If the expression is
6578 -- a default for a discriminant specification, the scope is still void.
6579 -- The expression can also appear in the discriminant part of a private
6580 -- or concurrent type.
6582 -- If the expression appears in a constrained subcomponent of an
6583 -- enclosing record declaration, the freeze nodes must be attached to
6584 -- the outer record type so they can eventually be placed in the
6585 -- enclosing declaration list.
6587 -- The other case requiring this special handling is if we are in a
6588 -- default expression, since in that case we are about to freeze a
6589 -- static type, and the freeze scope needs to be the outer scope, not
6590 -- the scope of the subprogram with the default parameter.
6592 -- For default expressions and other spec expressions in generic units,
6593 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
6594 -- placing them at the proper place, after the generic unit.
6596 if (In_Spec_Exp and not Inside_A_Generic)
6597 or else Freeze_Outside
6598 or else (Is_Type (Current_Scope)
6599 and then (not Is_Concurrent_Type (Current_Scope)
6600 or else not Has_Completion (Current_Scope)))
6601 or else Ekind (Current_Scope) = E_Void
6602 then
6603 declare
6604 N : constant Node_Id := Current_Scope;
6605 Freeze_Nodes : List_Id := No_List;
6606 Pos : Int := Scope_Stack.Last;
6608 begin
6609 if Present (Desig_Typ) then
6610 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
6611 end if;
6613 if Present (Typ) then
6614 Freeze_And_Append (Typ, N, Freeze_Nodes);
6615 end if;
6617 if Present (Nam) then
6618 Freeze_And_Append (Nam, N, Freeze_Nodes);
6619 end if;
6621 -- The current scope may be that of a constrained component of
6622 -- an enclosing record declaration, or of a loop of an enclosing
6623 -- quantified expression, which is above the current scope in the
6624 -- scope stack. Indeed in the context of a quantified expression,
6625 -- a scope is created and pushed above the current scope in order
6626 -- to emulate the loop-like behavior of the quantified expression.
6627 -- If the expression is within a top-level pragma, as for a pre-
6628 -- condition on a library-level subprogram, nothing to do.
6630 if not Is_Compilation_Unit (Current_Scope)
6631 and then (Is_Record_Type (Scope (Current_Scope))
6632 or else Nkind (Parent (Current_Scope)) =
6633 N_Quantified_Expression)
6634 then
6635 Pos := Pos - 1;
6636 end if;
6638 if Is_Non_Empty_List (Freeze_Nodes) then
6639 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
6640 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
6641 Freeze_Nodes;
6642 else
6643 Append_List (Freeze_Nodes,
6644 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
6645 end if;
6646 end if;
6647 end;
6649 return;
6650 end if;
6652 -- Now we have the right place to do the freezing. First, a special
6653 -- adjustment, if we are in spec-expression analysis mode, these freeze
6654 -- actions must not be thrown away (normally all inserted actions are
6655 -- thrown away in this mode. However, the freeze actions are from static
6656 -- expressions and one of the important reasons we are doing this
6657 -- special analysis is to get these freeze actions. Therefore we turn
6658 -- off the In_Spec_Expression mode to propagate these freeze actions.
6659 -- This also means they get properly analyzed and expanded.
6661 In_Spec_Expression := False;
6663 -- Freeze the designated type of an allocator (RM 13.14(13))
6665 if Present (Desig_Typ) then
6666 Freeze_Before (P, Desig_Typ);
6667 end if;
6669 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
6670 -- the enumeration representation clause exception in the loop above.
6672 if Present (Typ) then
6673 Freeze_Before (P, Typ);
6674 end if;
6676 -- Freeze name if one is present (RM 13.14(11))
6678 if Present (Nam) then
6679 Freeze_Before (P, Nam);
6680 end if;
6682 -- Restore In_Spec_Expression flag
6684 In_Spec_Expression := In_Spec_Exp;
6685 end Freeze_Expression;
6687 -----------------------------
6688 -- Freeze_Fixed_Point_Type --
6689 -----------------------------
6691 -- Certain fixed-point types and subtypes, including implicit base types
6692 -- and declared first subtypes, have not yet set up a range. This is
6693 -- because the range cannot be set until the Small and Size values are
6694 -- known, and these are not known till the type is frozen.
6696 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
6697 -- whose bounds are unanalyzed real literals. This routine will recognize
6698 -- this case, and transform this range node into a properly typed range
6699 -- with properly analyzed and resolved values.
6701 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
6702 Rng : constant Node_Id := Scalar_Range (Typ);
6703 Lo : constant Node_Id := Low_Bound (Rng);
6704 Hi : constant Node_Id := High_Bound (Rng);
6705 Btyp : constant Entity_Id := Base_Type (Typ);
6706 Brng : constant Node_Id := Scalar_Range (Btyp);
6707 BLo : constant Node_Id := Low_Bound (Brng);
6708 BHi : constant Node_Id := High_Bound (Brng);
6709 Small : constant Ureal := Small_Value (Typ);
6710 Loval : Ureal;
6711 Hival : Ureal;
6712 Atype : Entity_Id;
6714 Orig_Lo : Ureal;
6715 Orig_Hi : Ureal;
6716 -- Save original bounds (for shaving tests)
6718 Actual_Size : Nat;
6719 -- Actual size chosen
6721 function Fsize (Lov, Hiv : Ureal) return Nat;
6722 -- Returns size of type with given bounds. Also leaves these
6723 -- bounds set as the current bounds of the Typ.
6725 -----------
6726 -- Fsize --
6727 -----------
6729 function Fsize (Lov, Hiv : Ureal) return Nat is
6730 begin
6731 Set_Realval (Lo, Lov);
6732 Set_Realval (Hi, Hiv);
6733 return Minimum_Size (Typ);
6734 end Fsize;
6736 -- Start of processing for Freeze_Fixed_Point_Type
6738 begin
6739 -- If Esize of a subtype has not previously been set, set it now
6741 if Unknown_Esize (Typ) then
6742 Atype := Ancestor_Subtype (Typ);
6744 if Present (Atype) then
6745 Set_Esize (Typ, Esize (Atype));
6746 else
6747 Set_Esize (Typ, Esize (Base_Type (Typ)));
6748 end if;
6749 end if;
6751 -- Immediate return if the range is already analyzed. This means that
6752 -- the range is already set, and does not need to be computed by this
6753 -- routine.
6755 if Analyzed (Rng) then
6756 return;
6757 end if;
6759 -- Immediate return if either of the bounds raises Constraint_Error
6761 if Raises_Constraint_Error (Lo)
6762 or else Raises_Constraint_Error (Hi)
6763 then
6764 return;
6765 end if;
6767 Loval := Realval (Lo);
6768 Hival := Realval (Hi);
6770 Orig_Lo := Loval;
6771 Orig_Hi := Hival;
6773 -- Ordinary fixed-point case
6775 if Is_Ordinary_Fixed_Point_Type (Typ) then
6777 -- For the ordinary fixed-point case, we are allowed to fudge the
6778 -- end-points up or down by small. Generally we prefer to fudge up,
6779 -- i.e. widen the bounds for non-model numbers so that the end points
6780 -- are included. However there are cases in which this cannot be
6781 -- done, and indeed cases in which we may need to narrow the bounds.
6782 -- The following circuit makes the decision.
6784 -- Note: our terminology here is that Incl_EP means that the bounds
6785 -- are widened by Small if necessary to include the end points, and
6786 -- Excl_EP means that the bounds are narrowed by Small to exclude the
6787 -- end-points if this reduces the size.
6789 -- Note that in the Incl case, all we care about is including the
6790 -- end-points. In the Excl case, we want to narrow the bounds as
6791 -- much as permitted by the RM, to give the smallest possible size.
6793 Fudge : declare
6794 Loval_Incl_EP : Ureal;
6795 Hival_Incl_EP : Ureal;
6797 Loval_Excl_EP : Ureal;
6798 Hival_Excl_EP : Ureal;
6800 Size_Incl_EP : Nat;
6801 Size_Excl_EP : Nat;
6803 Model_Num : Ureal;
6804 First_Subt : Entity_Id;
6805 Actual_Lo : Ureal;
6806 Actual_Hi : Ureal;
6808 begin
6809 -- First step. Base types are required to be symmetrical. Right
6810 -- now, the base type range is a copy of the first subtype range.
6811 -- This will be corrected before we are done, but right away we
6812 -- need to deal with the case where both bounds are non-negative.
6813 -- In this case, we set the low bound to the negative of the high
6814 -- bound, to make sure that the size is computed to include the
6815 -- required sign. Note that we do not need to worry about the
6816 -- case of both bounds negative, because the sign will be dealt
6817 -- with anyway. Furthermore we can't just go making such a bound
6818 -- symmetrical, since in a twos-complement system, there is an
6819 -- extra negative value which could not be accommodated on the
6820 -- positive side.
6822 if Typ = Btyp
6823 and then not UR_Is_Negative (Loval)
6824 and then Hival > Loval
6825 then
6826 Loval := -Hival;
6827 Set_Realval (Lo, Loval);
6828 end if;
6830 -- Compute the fudged bounds. If the number is a model number,
6831 -- then we do nothing to include it, but we are allowed to backoff
6832 -- to the next adjacent model number when we exclude it. If it is
6833 -- not a model number then we straddle the two values with the
6834 -- model numbers on either side.
6836 Model_Num := UR_Trunc (Loval / Small) * Small;
6838 if Loval = Model_Num then
6839 Loval_Incl_EP := Model_Num;
6840 else
6841 Loval_Incl_EP := Model_Num - Small;
6842 end if;
6844 -- The low value excluding the end point is Small greater, but
6845 -- we do not do this exclusion if the low value is positive,
6846 -- since it can't help the size and could actually hurt by
6847 -- crossing the high bound.
6849 if UR_Is_Negative (Loval_Incl_EP) then
6850 Loval_Excl_EP := Loval_Incl_EP + Small;
6852 -- If the value went from negative to zero, then we have the
6853 -- case where Loval_Incl_EP is the model number just below
6854 -- zero, so we want to stick to the negative value for the
6855 -- base type to maintain the condition that the size will
6856 -- include signed values.
6858 if Typ = Btyp
6859 and then UR_Is_Zero (Loval_Excl_EP)
6860 then
6861 Loval_Excl_EP := Loval_Incl_EP;
6862 end if;
6864 else
6865 Loval_Excl_EP := Loval_Incl_EP;
6866 end if;
6868 -- Similar processing for upper bound and high value
6870 Model_Num := UR_Trunc (Hival / Small) * Small;
6872 if Hival = Model_Num then
6873 Hival_Incl_EP := Model_Num;
6874 else
6875 Hival_Incl_EP := Model_Num + Small;
6876 end if;
6878 if UR_Is_Positive (Hival_Incl_EP) then
6879 Hival_Excl_EP := Hival_Incl_EP - Small;
6880 else
6881 Hival_Excl_EP := Hival_Incl_EP;
6882 end if;
6884 -- One further adjustment is needed. In the case of subtypes, we
6885 -- cannot go outside the range of the base type, or we get
6886 -- peculiarities, and the base type range is already set. This
6887 -- only applies to the Incl values, since clearly the Excl values
6888 -- are already as restricted as they are allowed to be.
6890 if Typ /= Btyp then
6891 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
6892 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
6893 end if;
6895 -- Get size including and excluding end points
6897 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
6898 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
6900 -- No need to exclude end-points if it does not reduce size
6902 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
6903 Loval_Excl_EP := Loval_Incl_EP;
6904 end if;
6906 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
6907 Hival_Excl_EP := Hival_Incl_EP;
6908 end if;
6910 -- Now we set the actual size to be used. We want to use the
6911 -- bounds fudged up to include the end-points but only if this
6912 -- can be done without violating a specifically given size
6913 -- size clause or causing an unacceptable increase in size.
6915 -- Case of size clause given
6917 if Has_Size_Clause (Typ) then
6919 -- Use the inclusive size only if it is consistent with
6920 -- the explicitly specified size.
6922 if Size_Incl_EP <= RM_Size (Typ) then
6923 Actual_Lo := Loval_Incl_EP;
6924 Actual_Hi := Hival_Incl_EP;
6925 Actual_Size := Size_Incl_EP;
6927 -- If the inclusive size is too large, we try excluding
6928 -- the end-points (will be caught later if does not work).
6930 else
6931 Actual_Lo := Loval_Excl_EP;
6932 Actual_Hi := Hival_Excl_EP;
6933 Actual_Size := Size_Excl_EP;
6934 end if;
6936 -- Case of size clause not given
6938 else
6939 -- If we have a base type whose corresponding first subtype
6940 -- has an explicit size that is large enough to include our
6941 -- end-points, then do so. There is no point in working hard
6942 -- to get a base type whose size is smaller than the specified
6943 -- size of the first subtype.
6945 First_Subt := First_Subtype (Typ);
6947 if Has_Size_Clause (First_Subt)
6948 and then Size_Incl_EP <= Esize (First_Subt)
6949 then
6950 Actual_Size := Size_Incl_EP;
6951 Actual_Lo := Loval_Incl_EP;
6952 Actual_Hi := Hival_Incl_EP;
6954 -- If excluding the end-points makes the size smaller and
6955 -- results in a size of 8,16,32,64, then we take the smaller
6956 -- size. For the 64 case, this is compulsory. For the other
6957 -- cases, it seems reasonable. We like to include end points
6958 -- if we can, but not at the expense of moving to the next
6959 -- natural boundary of size.
6961 elsif Size_Incl_EP /= Size_Excl_EP
6962 and then Addressable (Size_Excl_EP)
6963 then
6964 Actual_Size := Size_Excl_EP;
6965 Actual_Lo := Loval_Excl_EP;
6966 Actual_Hi := Hival_Excl_EP;
6968 -- Otherwise we can definitely include the end points
6970 else
6971 Actual_Size := Size_Incl_EP;
6972 Actual_Lo := Loval_Incl_EP;
6973 Actual_Hi := Hival_Incl_EP;
6974 end if;
6976 -- One pathological case: normally we never fudge a low bound
6977 -- down, since it would seem to increase the size (if it has
6978 -- any effect), but for ranges containing single value, or no
6979 -- values, the high bound can be small too large. Consider:
6981 -- type t is delta 2.0**(-14)
6982 -- range 131072.0 .. 0;
6984 -- That lower bound is *just* outside the range of 32 bits, and
6985 -- does need fudging down in this case. Note that the bounds
6986 -- will always have crossed here, since the high bound will be
6987 -- fudged down if necessary, as in the case of:
6989 -- type t is delta 2.0**(-14)
6990 -- range 131072.0 .. 131072.0;
6992 -- So we detect the situation by looking for crossed bounds,
6993 -- and if the bounds are crossed, and the low bound is greater
6994 -- than zero, we will always back it off by small, since this
6995 -- is completely harmless.
6997 if Actual_Lo > Actual_Hi then
6998 if UR_Is_Positive (Actual_Lo) then
6999 Actual_Lo := Loval_Incl_EP - Small;
7000 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7002 -- And of course, we need to do exactly the same parallel
7003 -- fudge for flat ranges in the negative region.
7005 elsif UR_Is_Negative (Actual_Hi) then
7006 Actual_Hi := Hival_Incl_EP + Small;
7007 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7008 end if;
7009 end if;
7010 end if;
7012 Set_Realval (Lo, Actual_Lo);
7013 Set_Realval (Hi, Actual_Hi);
7014 end Fudge;
7016 -- For the decimal case, none of this fudging is required, since there
7017 -- are no end-point problems in the decimal case (the end-points are
7018 -- always included).
7020 else
7021 Actual_Size := Fsize (Loval, Hival);
7022 end if;
7024 -- At this stage, the actual size has been calculated and the proper
7025 -- required bounds are stored in the low and high bounds.
7027 if Actual_Size > 64 then
7028 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7029 Error_Msg_N
7030 ("size required (^) for type& too large, maximum allowed is 64",
7031 Typ);
7032 Actual_Size := 64;
7033 end if;
7035 -- Check size against explicit given size
7037 if Has_Size_Clause (Typ) then
7038 if Actual_Size > RM_Size (Typ) then
7039 Error_Msg_Uint_1 := RM_Size (Typ);
7040 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7041 Error_Msg_NE
7042 ("size given (^) for type& too small, minimum allowed is ^",
7043 Size_Clause (Typ), Typ);
7045 else
7046 Actual_Size := UI_To_Int (Esize (Typ));
7047 end if;
7049 -- Increase size to next natural boundary if no size clause given
7051 else
7052 if Actual_Size <= 8 then
7053 Actual_Size := 8;
7054 elsif Actual_Size <= 16 then
7055 Actual_Size := 16;
7056 elsif Actual_Size <= 32 then
7057 Actual_Size := 32;
7058 else
7059 Actual_Size := 64;
7060 end if;
7062 Init_Esize (Typ, Actual_Size);
7063 Adjust_Esize_For_Alignment (Typ);
7064 end if;
7066 -- If we have a base type, then expand the bounds so that they extend to
7067 -- the full width of the allocated size in bits, to avoid junk range
7068 -- checks on intermediate computations.
7070 if Base_Type (Typ) = Typ then
7071 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7072 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7073 end if;
7075 -- Final step is to reanalyze the bounds using the proper type
7076 -- and set the Corresponding_Integer_Value fields of the literals.
7078 Set_Etype (Lo, Empty);
7079 Set_Analyzed (Lo, False);
7080 Analyze (Lo);
7082 -- Resolve with universal fixed if the base type, and the base type if
7083 -- it is a subtype. Note we can't resolve the base type with itself,
7084 -- that would be a reference before definition.
7086 if Typ = Btyp then
7087 Resolve (Lo, Universal_Fixed);
7088 else
7089 Resolve (Lo, Btyp);
7090 end if;
7092 -- Set corresponding integer value for bound
7094 Set_Corresponding_Integer_Value
7095 (Lo, UR_To_Uint (Realval (Lo) / Small));
7097 -- Similar processing for high bound
7099 Set_Etype (Hi, Empty);
7100 Set_Analyzed (Hi, False);
7101 Analyze (Hi);
7103 if Typ = Btyp then
7104 Resolve (Hi, Universal_Fixed);
7105 else
7106 Resolve (Hi, Btyp);
7107 end if;
7109 Set_Corresponding_Integer_Value
7110 (Hi, UR_To_Uint (Realval (Hi) / Small));
7112 -- Set type of range to correspond to bounds
7114 Set_Etype (Rng, Etype (Lo));
7116 -- Set Esize to calculated size if not set already
7118 if Unknown_Esize (Typ) then
7119 Init_Esize (Typ, Actual_Size);
7120 end if;
7122 -- Set RM_Size if not already set. If already set, check value
7124 declare
7125 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7127 begin
7128 if RM_Size (Typ) /= Uint_0 then
7129 if RM_Size (Typ) < Minsiz then
7130 Error_Msg_Uint_1 := RM_Size (Typ);
7131 Error_Msg_Uint_2 := Minsiz;
7132 Error_Msg_NE
7133 ("size given (^) for type& too small, minimum allowed is ^",
7134 Size_Clause (Typ), Typ);
7135 end if;
7137 else
7138 Set_RM_Size (Typ, Minsiz);
7139 end if;
7140 end;
7142 -- Check for shaving
7144 if Comes_From_Source (Typ) then
7145 if Orig_Lo < Expr_Value_R (Lo) then
7146 Error_Msg_N
7147 ("declared low bound of type & is outside type range??", Typ);
7148 Error_Msg_N
7149 ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
7150 end if;
7152 if Orig_Hi > Expr_Value_R (Hi) then
7153 Error_Msg_N
7154 ("declared high bound of type & is outside type range??", Typ);
7155 Error_Msg_N
7156 ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
7157 end if;
7158 end if;
7159 end Freeze_Fixed_Point_Type;
7161 ------------------
7162 -- Freeze_Itype --
7163 ------------------
7165 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7166 L : List_Id;
7168 begin
7169 Set_Has_Delayed_Freeze (T);
7170 L := Freeze_Entity (T, N);
7172 if Is_Non_Empty_List (L) then
7173 Insert_Actions (N, L);
7174 end if;
7175 end Freeze_Itype;
7177 --------------------------
7178 -- Freeze_Static_Object --
7179 --------------------------
7181 procedure Freeze_Static_Object (E : Entity_Id) is
7183 Cannot_Be_Static : exception;
7184 -- Exception raised if the type of a static object cannot be made
7185 -- static. This happens if the type depends on non-global objects.
7187 procedure Ensure_Expression_Is_SA (N : Node_Id);
7188 -- Called to ensure that an expression used as part of a type definition
7189 -- is statically allocatable, which means that the expression type is
7190 -- statically allocatable, and the expression is either static, or a
7191 -- reference to a library level constant.
7193 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7194 -- Called to mark a type as static, checking that it is possible
7195 -- to set the type as static. If it is not possible, then the
7196 -- exception Cannot_Be_Static is raised.
7198 -----------------------------
7199 -- Ensure_Expression_Is_SA --
7200 -----------------------------
7202 procedure Ensure_Expression_Is_SA (N : Node_Id) is
7203 Ent : Entity_Id;
7205 begin
7206 Ensure_Type_Is_SA (Etype (N));
7208 if Is_OK_Static_Expression (N) then
7209 return;
7211 elsif Nkind (N) = N_Identifier then
7212 Ent := Entity (N);
7214 if Present (Ent)
7215 and then Ekind (Ent) = E_Constant
7216 and then Is_Library_Level_Entity (Ent)
7217 then
7218 return;
7219 end if;
7220 end if;
7222 raise Cannot_Be_Static;
7223 end Ensure_Expression_Is_SA;
7225 -----------------------
7226 -- Ensure_Type_Is_SA --
7227 -----------------------
7229 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7230 N : Node_Id;
7231 C : Entity_Id;
7233 begin
7234 -- If type is library level, we are all set
7236 if Is_Library_Level_Entity (Typ) then
7237 return;
7238 end if;
7240 -- We are also OK if the type already marked as statically allocated,
7241 -- which means we processed it before.
7243 if Is_Statically_Allocated (Typ) then
7244 return;
7245 end if;
7247 -- Mark type as statically allocated
7249 Set_Is_Statically_Allocated (Typ);
7251 -- Check that it is safe to statically allocate this type
7253 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
7254 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
7255 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
7257 elsif Is_Array_Type (Typ) then
7258 N := First_Index (Typ);
7259 while Present (N) loop
7260 Ensure_Type_Is_SA (Etype (N));
7261 Next_Index (N);
7262 end loop;
7264 Ensure_Type_Is_SA (Component_Type (Typ));
7266 elsif Is_Access_Type (Typ) then
7267 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
7269 declare
7270 F : Entity_Id;
7271 T : constant Entity_Id := Etype (Designated_Type (Typ));
7273 begin
7274 if T /= Standard_Void_Type then
7275 Ensure_Type_Is_SA (T);
7276 end if;
7278 F := First_Formal (Designated_Type (Typ));
7279 while Present (F) loop
7280 Ensure_Type_Is_SA (Etype (F));
7281 Next_Formal (F);
7282 end loop;
7283 end;
7285 else
7286 Ensure_Type_Is_SA (Designated_Type (Typ));
7287 end if;
7289 elsif Is_Record_Type (Typ) then
7290 C := First_Entity (Typ);
7291 while Present (C) loop
7292 if Ekind (C) = E_Discriminant
7293 or else Ekind (C) = E_Component
7294 then
7295 Ensure_Type_Is_SA (Etype (C));
7297 elsif Is_Type (C) then
7298 Ensure_Type_Is_SA (C);
7299 end if;
7301 Next_Entity (C);
7302 end loop;
7304 elsif Ekind (Typ) = E_Subprogram_Type then
7305 Ensure_Type_Is_SA (Etype (Typ));
7307 C := First_Formal (Typ);
7308 while Present (C) loop
7309 Ensure_Type_Is_SA (Etype (C));
7310 Next_Formal (C);
7311 end loop;
7313 else
7314 raise Cannot_Be_Static;
7315 end if;
7316 end Ensure_Type_Is_SA;
7318 -- Start of processing for Freeze_Static_Object
7320 begin
7321 Ensure_Type_Is_SA (Etype (E));
7323 exception
7324 when Cannot_Be_Static =>
7326 -- If the object that cannot be static is imported or exported, then
7327 -- issue an error message saying that this object cannot be imported
7328 -- or exported. If it has an address clause it is an overlay in the
7329 -- current partition and the static requirement is not relevant.
7330 -- Do not issue any error message when ignoring rep clauses.
7332 if Ignore_Rep_Clauses then
7333 null;
7335 elsif Is_Imported (E) then
7336 if No (Address_Clause (E)) then
7337 Error_Msg_N
7338 ("& cannot be imported (local type is not constant)", E);
7339 end if;
7341 -- Otherwise must be exported, something is wrong if compiler
7342 -- is marking something as statically allocated which cannot be).
7344 else pragma Assert (Is_Exported (E));
7345 Error_Msg_N
7346 ("& cannot be exported (local type is not constant)", E);
7347 end if;
7348 end Freeze_Static_Object;
7350 -----------------------
7351 -- Freeze_Subprogram --
7352 -----------------------
7354 procedure Freeze_Subprogram (E : Entity_Id) is
7355 Retype : Entity_Id;
7356 F : Entity_Id;
7358 begin
7359 -- Subprogram may not have an address clause unless it is imported
7361 if Present (Address_Clause (E)) then
7362 if not Is_Imported (E) then
7363 Error_Msg_N
7364 ("address clause can only be given " &
7365 "for imported subprogram",
7366 Name (Address_Clause (E)));
7367 end if;
7368 end if;
7370 -- Reset the Pure indication on an imported subprogram unless an
7371 -- explicit Pure_Function pragma was present or the subprogram is an
7372 -- intrinsic. We do this because otherwise it is an insidious error
7373 -- to call a non-pure function from pure unit and have calls
7374 -- mysteriously optimized away. What happens here is that the Import
7375 -- can bypass the normal check to ensure that pure units call only pure
7376 -- subprograms.
7378 -- The reason for the intrinsic exception is that in general, intrinsic
7379 -- functions (such as shifts) are pure anyway. The only exceptions are
7380 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
7381 -- in any case, so no problem arises.
7383 if Is_Imported (E)
7384 and then Is_Pure (E)
7385 and then not Has_Pragma_Pure_Function (E)
7386 and then not Is_Intrinsic_Subprogram (E)
7387 then
7388 Set_Is_Pure (E, False);
7389 end if;
7391 -- For non-foreign convention subprograms, this is where we create
7392 -- the extra formals (for accessibility level and constrained bit
7393 -- information). We delay this till the freeze point precisely so
7394 -- that we know the convention.
7396 if not Has_Foreign_Convention (E) then
7397 Create_Extra_Formals (E);
7398 Set_Mechanisms (E);
7400 -- If this is convention Ada and a Valued_Procedure, that's odd
7402 if Ekind (E) = E_Procedure
7403 and then Is_Valued_Procedure (E)
7404 and then Convention (E) = Convention_Ada
7405 and then Warn_On_Export_Import
7406 then
7407 Error_Msg_N
7408 ("??Valued_Procedure has no effect for convention Ada", E);
7409 Set_Is_Valued_Procedure (E, False);
7410 end if;
7412 -- Case of foreign convention
7414 else
7415 Set_Mechanisms (E);
7417 -- For foreign conventions, warn about return of unconstrained array
7419 if Ekind (E) = E_Function then
7420 Retype := Underlying_Type (Etype (E));
7422 -- If no return type, probably some other error, e.g. a
7423 -- missing full declaration, so ignore.
7425 if No (Retype) then
7426 null;
7428 -- If the return type is generic, we have emitted a warning
7429 -- earlier on, and there is nothing else to check here. Specific
7430 -- instantiations may lead to erroneous behavior.
7432 elsif Is_Generic_Type (Etype (E)) then
7433 null;
7435 -- Display warning if returning unconstrained array
7437 elsif Is_Array_Type (Retype)
7438 and then not Is_Constrained (Retype)
7440 -- Check appropriate warning is enabled (should we check for
7441 -- Warnings (Off) on specific entities here, probably so???)
7443 and then Warn_On_Export_Import
7445 -- Exclude the VM case, since return of unconstrained arrays
7446 -- is properly handled in both the JVM and .NET cases.
7448 and then VM_Target = No_VM
7449 then
7450 Error_Msg_N
7451 ("?x?foreign convention function& should not return " &
7452 "unconstrained array", E);
7453 return;
7454 end if;
7455 end if;
7457 -- If any of the formals for an exported foreign convention
7458 -- subprogram have defaults, then emit an appropriate warning since
7459 -- this is odd (default cannot be used from non-Ada code)
7461 if Is_Exported (E) then
7462 F := First_Formal (E);
7463 while Present (F) loop
7464 if Warn_On_Export_Import
7465 and then Present (Default_Value (F))
7466 then
7467 Error_Msg_N
7468 ("?x?parameter cannot be defaulted in non-Ada call",
7469 Default_Value (F));
7470 end if;
7472 Next_Formal (F);
7473 end loop;
7474 end if;
7475 end if;
7477 -- Pragma Inline_Always is disallowed for dispatching subprograms
7478 -- because the address of such subprograms is saved in the dispatch
7479 -- table to support dispatching calls, and dispatching calls cannot
7480 -- be inlined. This is consistent with the restriction against using
7481 -- 'Access or 'Address on an Inline_Always subprogram.
7483 if Is_Dispatching_Operation (E)
7484 and then Has_Pragma_Inline_Always (E)
7485 then
7486 Error_Msg_N
7487 ("pragma Inline_Always not allowed for dispatching subprograms", E);
7488 end if;
7490 -- Because of the implicit representation of inherited predefined
7491 -- operators in the front-end, the overriding status of the operation
7492 -- may be affected when a full view of a type is analyzed, and this is
7493 -- not captured by the analysis of the corresponding type declaration.
7494 -- Therefore the correctness of a not-overriding indicator must be
7495 -- rechecked when the subprogram is frozen.
7497 if Nkind (E) = N_Defining_Operator_Symbol
7498 and then not Error_Posted (Parent (E))
7499 then
7500 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
7501 end if;
7502 end Freeze_Subprogram;
7504 ----------------------
7505 -- Is_Fully_Defined --
7506 ----------------------
7508 function Is_Fully_Defined (T : Entity_Id) return Boolean is
7509 begin
7510 if Ekind (T) = E_Class_Wide_Type then
7511 return Is_Fully_Defined (Etype (T));
7513 elsif Is_Array_Type (T) then
7514 return Is_Fully_Defined (Component_Type (T));
7516 elsif Is_Record_Type (T)
7517 and not Is_Private_Type (T)
7518 then
7519 -- Verify that the record type has no components with private types
7520 -- without completion.
7522 declare
7523 Comp : Entity_Id;
7525 begin
7526 Comp := First_Component (T);
7527 while Present (Comp) loop
7528 if not Is_Fully_Defined (Etype (Comp)) then
7529 return False;
7530 end if;
7532 Next_Component (Comp);
7533 end loop;
7534 return True;
7535 end;
7537 -- For the designated type of an access to subprogram, all types in
7538 -- the profile must be fully defined.
7540 elsif Ekind (T) = E_Subprogram_Type then
7541 declare
7542 F : Entity_Id;
7544 begin
7545 F := First_Formal (T);
7546 while Present (F) loop
7547 if not Is_Fully_Defined (Etype (F)) then
7548 return False;
7549 end if;
7551 Next_Formal (F);
7552 end loop;
7554 return Is_Fully_Defined (Etype (T));
7555 end;
7557 else
7558 return not Is_Private_Type (T)
7559 or else Present (Full_View (Base_Type (T)));
7560 end if;
7561 end Is_Fully_Defined;
7563 ---------------------------------
7564 -- Process_Default_Expressions --
7565 ---------------------------------
7567 procedure Process_Default_Expressions
7568 (E : Entity_Id;
7569 After : in out Node_Id)
7571 Loc : constant Source_Ptr := Sloc (E);
7572 Dbody : Node_Id;
7573 Formal : Node_Id;
7574 Dcopy : Node_Id;
7575 Dnam : Entity_Id;
7577 begin
7578 Set_Default_Expressions_Processed (E);
7580 -- A subprogram instance and its associated anonymous subprogram share
7581 -- their signature. The default expression functions are defined in the
7582 -- wrapper packages for the anonymous subprogram, and should not be
7583 -- generated again for the instance.
7585 if Is_Generic_Instance (E)
7586 and then Present (Alias (E))
7587 and then Default_Expressions_Processed (Alias (E))
7588 then
7589 return;
7590 end if;
7592 Formal := First_Formal (E);
7593 while Present (Formal) loop
7594 if Present (Default_Value (Formal)) then
7596 -- We work with a copy of the default expression because we
7597 -- do not want to disturb the original, since this would mess
7598 -- up the conformance checking.
7600 Dcopy := New_Copy_Tree (Default_Value (Formal));
7602 -- The analysis of the expression may generate insert actions,
7603 -- which of course must not be executed. We wrap those actions
7604 -- in a procedure that is not called, and later on eliminated.
7605 -- The following cases have no side-effects, and are analyzed
7606 -- directly.
7608 if Nkind (Dcopy) = N_Identifier
7609 or else Nkind_In (Dcopy, N_Expanded_Name,
7610 N_Integer_Literal,
7611 N_Character_Literal,
7612 N_String_Literal,
7613 N_Real_Literal)
7614 or else (Nkind (Dcopy) = N_Attribute_Reference
7615 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
7616 or else Known_Null (Dcopy)
7617 then
7618 -- If there is no default function, we must still do a full
7619 -- analyze call on the default value, to ensure that all error
7620 -- checks are performed, e.g. those associated with static
7621 -- evaluation. Note: this branch will always be taken if the
7622 -- analyzer is turned off (but we still need the error checks).
7624 -- Note: the setting of parent here is to meet the requirement
7625 -- that we can only analyze the expression while attached to
7626 -- the tree. Really the requirement is that the parent chain
7627 -- be set, we don't actually need to be in the tree.
7629 Set_Parent (Dcopy, Declaration_Node (Formal));
7630 Analyze (Dcopy);
7632 -- Default expressions are resolved with their own type if the
7633 -- context is generic, to avoid anomalies with private types.
7635 if Ekind (Scope (E)) = E_Generic_Package then
7636 Resolve (Dcopy);
7637 else
7638 Resolve (Dcopy, Etype (Formal));
7639 end if;
7641 -- If that resolved expression will raise constraint error,
7642 -- then flag the default value as raising constraint error.
7643 -- This allows a proper error message on the calls.
7645 if Raises_Constraint_Error (Dcopy) then
7646 Set_Raises_Constraint_Error (Default_Value (Formal));
7647 end if;
7649 -- If the default is a parameterless call, we use the name of
7650 -- the called function directly, and there is no body to build.
7652 elsif Nkind (Dcopy) = N_Function_Call
7653 and then No (Parameter_Associations (Dcopy))
7654 then
7655 null;
7657 -- Else construct and analyze the body of a wrapper procedure
7658 -- that contains an object declaration to hold the expression.
7659 -- Given that this is done only to complete the analysis, it
7660 -- simpler to build a procedure than a function which might
7661 -- involve secondary stack expansion.
7663 else
7664 Dnam := Make_Temporary (Loc, 'D');
7666 Dbody :=
7667 Make_Subprogram_Body (Loc,
7668 Specification =>
7669 Make_Procedure_Specification (Loc,
7670 Defining_Unit_Name => Dnam),
7672 Declarations => New_List (
7673 Make_Object_Declaration (Loc,
7674 Defining_Identifier => Make_Temporary (Loc, 'T'),
7675 Object_Definition =>
7676 New_Occurrence_Of (Etype (Formal), Loc),
7677 Expression => New_Copy_Tree (Dcopy))),
7679 Handled_Statement_Sequence =>
7680 Make_Handled_Sequence_Of_Statements (Loc,
7681 Statements => Empty_List));
7683 Set_Scope (Dnam, Scope (E));
7684 Set_Assignment_OK (First (Declarations (Dbody)));
7685 Set_Is_Eliminated (Dnam);
7686 Insert_After (After, Dbody);
7687 Analyze (Dbody);
7688 After := Dbody;
7689 end if;
7690 end if;
7692 Next_Formal (Formal);
7693 end loop;
7694 end Process_Default_Expressions;
7696 ----------------------------------------
7697 -- Set_Component_Alignment_If_Not_Set --
7698 ----------------------------------------
7700 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
7701 begin
7702 -- Ignore if not base type, subtypes don't need anything
7704 if Typ /= Base_Type (Typ) then
7705 return;
7706 end if;
7708 -- Do not override existing representation
7710 if Is_Packed (Typ) then
7711 return;
7713 elsif Has_Specified_Layout (Typ) then
7714 return;
7716 elsif Component_Alignment (Typ) /= Calign_Default then
7717 return;
7719 else
7720 Set_Component_Alignment
7721 (Typ, Scope_Stack.Table
7722 (Scope_Stack.Last).Component_Alignment_Default);
7723 end if;
7724 end Set_Component_Alignment_If_Not_Set;
7726 --------------------------
7727 -- Set_SSO_From_Default --
7728 --------------------------
7730 procedure Set_SSO_From_Default (T : Entity_Id) is
7731 begin
7732 -- Set default SSO for an array or record base type, except in case of
7733 -- a type extension (which always inherits the SSO of its parent type).
7735 if Is_Base_Type (T)
7736 and then (Is_Array_Type (T)
7737 or else (Is_Record_Type (T)
7738 and then not (Is_Tagged_Type (T)
7739 and then Is_Derived_Type (T))))
7740 then
7741 if ((Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
7742 or else
7743 ((not Bytes_Big_Endian) and then SSO_Set_High_By_Default (T)))
7745 -- For a record type, if native bit order is specified explicitly,
7746 -- then never set reverse SSO from default.
7748 and then not
7749 (Is_Record_Type (T)
7750 and then Has_Rep_Item (T, Name_Bit_Order)
7751 and then not Reverse_Bit_Order (T))
7752 then
7753 -- If flags cause reverse storage order, then set the result. Note
7754 -- that we would have ignored the pragma setting the non default
7755 -- storage order in any case, hence the assertion at this point.
7757 pragma Assert (Support_Nondefault_SSO_On_Target);
7758 Set_Reverse_Storage_Order (T);
7760 -- For a record type, also set reversed bit order. Note that if
7761 -- a bit order has been specified explicitly, then this is a
7762 -- no-op, as per the guard above.
7764 if Is_Record_Type (T) then
7765 Set_Reverse_Bit_Order (T);
7766 end if;
7767 end if;
7768 end if;
7769 end Set_SSO_From_Default;
7771 ------------------
7772 -- Undelay_Type --
7773 ------------------
7775 procedure Undelay_Type (T : Entity_Id) is
7776 begin
7777 Set_Has_Delayed_Freeze (T, False);
7778 Set_Freeze_Node (T, Empty);
7780 -- Since we don't want T to have a Freeze_Node, we don't want its
7781 -- Full_View or Corresponding_Record_Type to have one either.
7783 -- ??? Fundamentally, this whole handling is unpleasant. What we really
7784 -- want is to be sure that for an Itype that's part of record R and is a
7785 -- subtype of type T, that it's frozen after the later of the freeze
7786 -- points of R and T. We have no way of doing that directly, so what we
7787 -- do is force most such Itypes to be frozen as part of freezing R via
7788 -- this procedure and only delay the ones that need to be delayed
7789 -- (mostly the designated types of access types that are defined as part
7790 -- of the record).
7792 if Is_Private_Type (T)
7793 and then Present (Full_View (T))
7794 and then Is_Itype (Full_View (T))
7795 and then Is_Record_Type (Scope (Full_View (T)))
7796 then
7797 Undelay_Type (Full_View (T));
7798 end if;
7800 if Is_Concurrent_Type (T)
7801 and then Present (Corresponding_Record_Type (T))
7802 and then Is_Itype (Corresponding_Record_Type (T))
7803 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
7804 then
7805 Undelay_Type (Corresponding_Record_Type (T));
7806 end if;
7807 end Undelay_Type;
7809 ------------------
7810 -- Warn_Overlay --
7811 ------------------
7813 procedure Warn_Overlay
7814 (Expr : Node_Id;
7815 Typ : Entity_Id;
7816 Nam : Entity_Id)
7818 Ent : constant Entity_Id := Entity (Nam);
7819 -- The object to which the address clause applies
7821 Init : Node_Id;
7822 Old : Entity_Id := Empty;
7823 Decl : Node_Id;
7825 begin
7826 -- No warning if address clause overlay warnings are off
7828 if not Address_Clause_Overlay_Warnings then
7829 return;
7830 end if;
7832 -- No warning if there is an explicit initialization
7834 Init := Original_Node (Expression (Declaration_Node (Ent)));
7836 if Present (Init) and then Comes_From_Source (Init) then
7837 return;
7838 end if;
7840 -- We only give the warning for non-imported entities of a type for
7841 -- which a non-null base init proc is defined, or for objects of access
7842 -- types with implicit null initialization, or when Normalize_Scalars
7843 -- applies and the type is scalar or a string type (the latter being
7844 -- tested for because predefined String types are initialized by inline
7845 -- code rather than by an init_proc). Note that we do not give the
7846 -- warning for Initialize_Scalars, since we suppressed initialization
7847 -- in this case. Also, do not warn if Suppress_Initialization is set.
7849 if Present (Expr)
7850 and then not Is_Imported (Ent)
7851 and then not Initialization_Suppressed (Typ)
7852 and then (Has_Non_Null_Base_Init_Proc (Typ)
7853 or else Is_Access_Type (Typ)
7854 or else (Normalize_Scalars
7855 and then (Is_Scalar_Type (Typ)
7856 or else Is_String_Type (Typ))))
7857 then
7858 if Nkind (Expr) = N_Attribute_Reference
7859 and then Is_Entity_Name (Prefix (Expr))
7860 then
7861 Old := Entity (Prefix (Expr));
7863 elsif Is_Entity_Name (Expr)
7864 and then Ekind (Entity (Expr)) = E_Constant
7865 then
7866 Decl := Declaration_Node (Entity (Expr));
7868 if Nkind (Decl) = N_Object_Declaration
7869 and then Present (Expression (Decl))
7870 and then Nkind (Expression (Decl)) = N_Attribute_Reference
7871 and then Is_Entity_Name (Prefix (Expression (Decl)))
7872 then
7873 Old := Entity (Prefix (Expression (Decl)));
7875 elsif Nkind (Expr) = N_Function_Call then
7876 return;
7877 end if;
7879 -- A function call (most likely to To_Address) is probably not an
7880 -- overlay, so skip warning. Ditto if the function call was inlined
7881 -- and transformed into an entity.
7883 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
7884 return;
7885 end if;
7887 Decl := Next (Parent (Expr));
7889 -- If a pragma Import follows, we assume that it is for the current
7890 -- target of the address clause, and skip the warning.
7892 if Present (Decl)
7893 and then Nkind (Decl) = N_Pragma
7894 and then Pragma_Name (Decl) = Name_Import
7895 then
7896 return;
7897 end if;
7899 if Present (Old) then
7900 Error_Msg_Node_2 := Old;
7901 Error_Msg_N
7902 ("default initialization of & may modify &??",
7903 Nam);
7904 else
7905 Error_Msg_N
7906 ("default initialization of & may modify overlaid storage??",
7907 Nam);
7908 end if;
7910 -- Add friendly warning if initialization comes from a packed array
7911 -- component.
7913 if Is_Record_Type (Typ) then
7914 declare
7915 Comp : Entity_Id;
7917 begin
7918 Comp := First_Component (Typ);
7919 while Present (Comp) loop
7920 if Nkind (Parent (Comp)) = N_Component_Declaration
7921 and then Present (Expression (Parent (Comp)))
7922 then
7923 exit;
7924 elsif Is_Array_Type (Etype (Comp))
7925 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
7926 then
7927 Error_Msg_NE
7928 ("\packed array component& " &
7929 "will be initialized to zero??",
7930 Nam, Comp);
7931 exit;
7932 else
7933 Next_Component (Comp);
7934 end if;
7935 end loop;
7936 end;
7937 end if;
7939 Error_Msg_N
7940 ("\use pragma Import for & to " &
7941 "suppress initialization (RM B.1(24))??",
7942 Nam);
7943 end if;
7944 end Warn_Overlay;
7946 end Freeze;