2014-10-31 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / freeze.adb
blobbccec208e45a9701233ac2f325befa52fff36c48
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;
668 end if;
669 end if;
670 end Check_Address_Clause;
672 -----------------------------
673 -- Check_Compile_Time_Size --
674 -----------------------------
676 procedure Check_Compile_Time_Size (T : Entity_Id) is
678 procedure Set_Small_Size (T : Entity_Id; S : Uint);
679 -- Sets the compile time known size (32 bits or less) in the Esize
680 -- field, of T checking for a size clause that was given which attempts
681 -- to give a smaller size, and also checking for an alignment clause.
683 function Size_Known (T : Entity_Id) return Boolean;
684 -- Recursive function that does all the work
686 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
687 -- If T is a constrained subtype, its size is not known if any of its
688 -- discriminant constraints is not static and it is not a null record.
689 -- The test is conservative and doesn't check that the components are
690 -- in fact constrained by non-static discriminant values. Could be made
691 -- more precise ???
693 --------------------
694 -- Set_Small_Size --
695 --------------------
697 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
698 begin
699 if S > 32 then
700 return;
702 -- Check for bad size clause given
704 elsif Has_Size_Clause (T) then
705 if RM_Size (T) < S then
706 Error_Msg_Uint_1 := S;
707 Error_Msg_NE
708 ("size for& too small, minimum allowed is ^",
709 Size_Clause (T), T);
710 end if;
712 -- Set size if not set already
714 elsif Unknown_RM_Size (T) then
715 Set_RM_Size (T, S);
716 end if;
717 end Set_Small_Size;
719 ----------------
720 -- Size_Known --
721 ----------------
723 function Size_Known (T : Entity_Id) return Boolean is
724 Index : Entity_Id;
725 Comp : Entity_Id;
726 Ctyp : Entity_Id;
727 Low : Node_Id;
728 High : Node_Id;
730 begin
731 if Size_Known_At_Compile_Time (T) then
732 return True;
734 -- Always True for scalar types. This is true even for generic formal
735 -- scalar types. We used to return False in the latter case, but the
736 -- size is known at compile time, even in the template, we just do
737 -- not know the exact size but that's not the point of this routine.
739 elsif Is_Scalar_Type (T)
740 or else Is_Task_Type (T)
741 then
742 return True;
744 -- Array types
746 elsif Is_Array_Type (T) then
748 -- String literals always have known size, and we can set it
750 if Ekind (T) = E_String_Literal_Subtype then
751 Set_Small_Size (T, Component_Size (T)
752 * String_Literal_Length (T));
753 return True;
755 -- Unconstrained types never have known at compile time size
757 elsif not Is_Constrained (T) then
758 return False;
760 -- Don't do any recursion on type with error posted, since we may
761 -- have a malformed type that leads us into a loop.
763 elsif Error_Posted (T) then
764 return False;
766 -- Otherwise if component size unknown, then array size unknown
768 elsif not Size_Known (Component_Type (T)) then
769 return False;
770 end if;
772 -- Check for all indexes static, and also compute possible size
773 -- (in case it is less than 32 and may be packable).
775 declare
776 Esiz : Uint := Component_Size (T);
777 Dim : Uint;
779 begin
780 Index := First_Index (T);
781 while Present (Index) loop
782 if Nkind (Index) = N_Range then
783 Get_Index_Bounds (Index, Low, High);
785 elsif Error_Posted (Scalar_Range (Etype (Index))) then
786 return False;
788 else
789 Low := Type_Low_Bound (Etype (Index));
790 High := Type_High_Bound (Etype (Index));
791 end if;
793 if not Compile_Time_Known_Value (Low)
794 or else not Compile_Time_Known_Value (High)
795 or else Etype (Index) = Any_Type
796 then
797 return False;
799 else
800 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
802 if Dim >= 0 then
803 Esiz := Esiz * Dim;
804 else
805 Esiz := Uint_0;
806 end if;
807 end if;
809 Next_Index (Index);
810 end loop;
812 Set_Small_Size (T, Esiz);
813 return True;
814 end;
816 -- Access types always have known at compile time sizes
818 elsif Is_Access_Type (T) then
819 return True;
821 -- For non-generic private types, go to underlying type if present
823 elsif Is_Private_Type (T)
824 and then not Is_Generic_Type (T)
825 and then Present (Underlying_Type (T))
826 then
827 -- Don't do any recursion on type with error posted, since we may
828 -- have a malformed type that leads us into a loop.
830 if Error_Posted (T) then
831 return False;
832 else
833 return Size_Known (Underlying_Type (T));
834 end if;
836 -- Record types
838 elsif Is_Record_Type (T) then
840 -- A class-wide type is never considered to have a known size
842 if Is_Class_Wide_Type (T) then
843 return False;
845 -- A subtype of a variant record must not have non-static
846 -- discriminated components.
848 elsif T /= Base_Type (T)
849 and then not Static_Discriminated_Components (T)
850 then
851 return False;
853 -- Don't do any recursion on type with error posted, since we may
854 -- have a malformed type that leads us into a loop.
856 elsif Error_Posted (T) then
857 return False;
858 end if;
860 -- Now look at the components of the record
862 declare
863 -- The following two variables are used to keep track of the
864 -- size of packed records if we can tell the size of the packed
865 -- record in the front end. Packed_Size_Known is True if so far
866 -- we can figure out the size. It is initialized to True for a
867 -- packed record, unless the record has discriminants or atomic
868 -- components or independent components.
870 -- The reason we eliminate the discriminated case is that
871 -- we don't know the way the back end lays out discriminated
872 -- packed records. If Packed_Size_Known is True, then
873 -- Packed_Size is the size in bits so far.
875 Packed_Size_Known : Boolean :=
876 Is_Packed (T)
877 and then not Has_Discriminants (T)
878 and then not Has_Atomic_Components (T)
879 and then not Has_Independent_Components (T);
881 Packed_Size : Uint := Uint_0;
882 -- Size in bits so far
884 begin
885 -- Test for variant part present
887 if Has_Discriminants (T)
888 and then Present (Parent (T))
889 and then Nkind (Parent (T)) = N_Full_Type_Declaration
890 and then Nkind (Type_Definition (Parent (T))) =
891 N_Record_Definition
892 and then not Null_Present (Type_Definition (Parent (T)))
893 and then
894 Present (Variant_Part
895 (Component_List (Type_Definition (Parent (T)))))
896 then
897 -- If variant part is present, and type is unconstrained,
898 -- then we must have defaulted discriminants, or a size
899 -- clause must be present for the type, or else the size
900 -- is definitely not known at compile time.
902 if not Is_Constrained (T)
903 and then
904 No (Discriminant_Default_Value (First_Discriminant (T)))
905 and then Unknown_RM_Size (T)
906 then
907 return False;
908 end if;
909 end if;
911 -- Loop through components
913 Comp := First_Component_Or_Discriminant (T);
914 while Present (Comp) loop
915 Ctyp := Etype (Comp);
917 -- We do not know the packed size if there is a component
918 -- clause present (we possibly could, but this would only
919 -- help in the case of a record with partial rep clauses.
920 -- That's because in the case of full rep clauses, the
921 -- size gets figured out anyway by a different circuit).
923 if Present (Component_Clause (Comp)) then
924 Packed_Size_Known := False;
925 end if;
927 -- We do not know the packed size if we have a by reference
928 -- type, or an atomic type or an atomic component, or an
929 -- aliased component (because packing does not touch these).
931 if Is_Atomic (Ctyp)
932 or else Is_Atomic (Comp)
933 or else Is_By_Reference_Type (Ctyp)
934 or else Is_Aliased (Comp)
935 then
936 Packed_Size_Known := False;
937 end if;
939 -- We need to identify a component that is an array where
940 -- the index type is an enumeration type with non-standard
941 -- representation, and some bound of the type depends on a
942 -- discriminant.
944 -- This is because gigi computes the size by doing a
945 -- substitution of the appropriate discriminant value in
946 -- the size expression for the base type, and gigi is not
947 -- clever enough to evaluate the resulting expression (which
948 -- involves a call to rep_to_pos) at compile time.
950 -- It would be nice if gigi would either recognize that
951 -- this expression can be computed at compile time, or
952 -- alternatively figured out the size from the subtype
953 -- directly, where all the information is at hand ???
955 if Is_Array_Type (Etype (Comp))
956 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
957 then
958 declare
959 Ocomp : constant Entity_Id :=
960 Original_Record_Component (Comp);
961 OCtyp : constant Entity_Id := Etype (Ocomp);
962 Ind : Node_Id;
963 Indtyp : Entity_Id;
964 Lo, Hi : Node_Id;
966 begin
967 Ind := First_Index (OCtyp);
968 while Present (Ind) loop
969 Indtyp := Etype (Ind);
971 if Is_Enumeration_Type (Indtyp)
972 and then Has_Non_Standard_Rep (Indtyp)
973 then
974 Lo := Type_Low_Bound (Indtyp);
975 Hi := Type_High_Bound (Indtyp);
977 if Is_Entity_Name (Lo)
978 and then Ekind (Entity (Lo)) = E_Discriminant
979 then
980 return False;
982 elsif Is_Entity_Name (Hi)
983 and then Ekind (Entity (Hi)) = E_Discriminant
984 then
985 return False;
986 end if;
987 end if;
989 Next_Index (Ind);
990 end loop;
991 end;
992 end if;
994 -- Clearly size of record is not known if the size of one of
995 -- the components is not known.
997 if not Size_Known (Ctyp) then
998 return False;
999 end if;
1001 -- Accumulate packed size if possible
1003 if Packed_Size_Known then
1005 -- We can only deal with elementary types, since for
1006 -- non-elementary components, alignment enters into the
1007 -- picture, and we don't know enough to handle proper
1008 -- alignment in this context. Packed arrays count as
1009 -- elementary if the representation is a modular type.
1011 if Is_Elementary_Type (Ctyp)
1012 or else (Is_Array_Type (Ctyp)
1013 and then Present
1014 (Packed_Array_Impl_Type (Ctyp))
1015 and then Is_Modular_Integer_Type
1016 (Packed_Array_Impl_Type (Ctyp)))
1017 then
1018 -- Packed size unknown if we have an atomic type
1019 -- or a by reference type, since the back end
1020 -- knows how these are layed out.
1022 if Is_Atomic (Ctyp)
1023 or else Is_By_Reference_Type (Ctyp)
1024 then
1025 Packed_Size_Known := False;
1027 -- If RM_Size is known and static, then we can keep
1028 -- accumulating the packed size
1030 elsif Known_Static_RM_Size (Ctyp) then
1032 -- A little glitch, to be removed sometime ???
1033 -- gigi does not understand zero sizes yet.
1035 if RM_Size (Ctyp) = Uint_0 then
1036 Packed_Size_Known := False;
1038 -- Normal case where we can keep accumulating the
1039 -- packed array size.
1041 else
1042 Packed_Size := Packed_Size + RM_Size (Ctyp);
1043 end if;
1045 -- If we have a field whose RM_Size is not known then
1046 -- we can't figure out the packed size here.
1048 else
1049 Packed_Size_Known := False;
1050 end if;
1052 -- If we have a non-elementary type we can't figure out
1053 -- the packed array size (alignment issues).
1055 else
1056 Packed_Size_Known := False;
1057 end if;
1058 end if;
1060 Next_Component_Or_Discriminant (Comp);
1061 end loop;
1063 if Packed_Size_Known then
1064 Set_Small_Size (T, Packed_Size);
1065 end if;
1067 return True;
1068 end;
1070 -- All other cases, size not known at compile time
1072 else
1073 return False;
1074 end if;
1075 end Size_Known;
1077 -------------------------------------
1078 -- Static_Discriminated_Components --
1079 -------------------------------------
1081 function Static_Discriminated_Components
1082 (T : Entity_Id) return Boolean
1084 Constraint : Elmt_Id;
1086 begin
1087 if Has_Discriminants (T)
1088 and then Present (Discriminant_Constraint (T))
1089 and then Present (First_Component (T))
1090 then
1091 Constraint := First_Elmt (Discriminant_Constraint (T));
1092 while Present (Constraint) loop
1093 if not Compile_Time_Known_Value (Node (Constraint)) then
1094 return False;
1095 end if;
1097 Next_Elmt (Constraint);
1098 end loop;
1099 end if;
1101 return True;
1102 end Static_Discriminated_Components;
1104 -- Start of processing for Check_Compile_Time_Size
1106 begin
1107 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1108 end Check_Compile_Time_Size;
1110 -----------------------------------
1111 -- Check_Component_Storage_Order --
1112 -----------------------------------
1114 procedure Check_Component_Storage_Order
1115 (Encl_Type : Entity_Id;
1116 Comp : Entity_Id;
1117 ADC : Node_Id;
1118 Comp_ADC_Present : out Boolean)
1120 Comp_Type : Entity_Id;
1121 Comp_ADC : Node_Id;
1122 Err_Node : Node_Id;
1124 Comp_Byte_Aligned : Boolean;
1125 -- Set for the record case, True if Comp starts on a byte boundary
1126 -- (in which case it is allowed to have different storage order).
1128 Comp_SSO_Differs : Boolean;
1129 -- Set True when the component is a nested composite, and it does not
1130 -- have the same scalar storage order as Encl_Type.
1132 Component_Aliased : Boolean;
1134 begin
1135 -- Record case
1137 if Present (Comp) then
1138 Err_Node := Comp;
1139 Comp_Type := Etype (Comp);
1141 if Is_Tag (Comp) then
1142 Comp_Byte_Aligned := True;
1143 Component_Aliased := False;
1145 else
1146 -- If a component clause is present, check if the component starts
1147 -- on a storage element boundary. Otherwise conservatively assume
1148 -- it does so only in the case where the record is not packed.
1150 if Present (Component_Clause (Comp)) then
1151 Comp_Byte_Aligned :=
1152 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0;
1153 else
1154 Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1155 end if;
1157 Component_Aliased := Is_Aliased (Comp);
1158 end if;
1160 -- Array case
1162 else
1163 Err_Node := Encl_Type;
1164 Comp_Type := Component_Type (Encl_Type);
1166 Component_Aliased := Has_Aliased_Components (Encl_Type);
1167 end if;
1169 -- Note: the Reverse_Storage_Order flag is set on the base type, but
1170 -- the attribute definition clause is attached to the first subtype.
1172 Comp_Type := Base_Type (Comp_Type);
1173 Comp_ADC := Get_Attribute_Definition_Clause
1174 (First_Subtype (Comp_Type),
1175 Attribute_Scalar_Storage_Order);
1176 Comp_ADC_Present := Present (Comp_ADC);
1178 -- Case of record or array component: check storage order compatibility
1180 if Is_Record_Type (Comp_Type) or else Is_Array_Type (Comp_Type) then
1181 Comp_SSO_Differs :=
1182 Reverse_Storage_Order (Encl_Type)
1184 Reverse_Storage_Order (Comp_Type);
1186 -- Parent and extension must have same storage order
1188 if Present (Comp) and then Chars (Comp) = Name_uParent then
1189 if Comp_SSO_Differs then
1190 Error_Msg_N
1191 ("record extension must have same scalar storage order as "
1192 & "parent", Err_Node);
1193 end if;
1195 -- If enclosing composite has explicit SSO then nested composite must
1196 -- have explicit SSO as well.
1198 elsif Present (ADC) and then No (Comp_ADC) then
1199 Error_Msg_N ("nested composite must have explicit scalar "
1200 & "storage order", Err_Node);
1202 -- If component and composite SSO differs, check that component
1203 -- falls on byte boundaries and isn't packed.
1205 elsif Comp_SSO_Differs then
1207 -- Component SSO differs from enclosing composite:
1209 -- Reject if component is a packed array, as it may be represented
1210 -- as a scalar internally.
1212 if Is_Packed_Array (Comp_Type) then
1213 Error_Msg_N
1214 ("type of packed component must have same scalar "
1215 & "storage order as enclosing composite", Err_Node);
1217 -- Reject if composite is a packed array, as it may be rewritten
1218 -- into an array of scalars.
1220 elsif Is_Packed_Array (Encl_Type) then
1221 Error_Msg_N ("type of packed array must have same scalar "
1222 & "storage order as component", Err_Node);
1224 -- Reject if not byte aligned
1226 elsif Is_Record_Type (Encl_Type)
1227 and then not Comp_Byte_Aligned
1228 then
1229 Error_Msg_N
1230 ("type of non-byte-aligned component must have same scalar "
1231 & "storage order as enclosing composite", Err_Node);
1232 end if;
1233 end if;
1235 -- Enclosing type has explicit SSO: non-composite component must not
1236 -- be aliased.
1238 elsif Present (ADC) and then Component_Aliased then
1239 Error_Msg_N
1240 ("aliased component not permitted for type with "
1241 & "explicit Scalar_Storage_Order", Err_Node);
1242 end if;
1243 end Check_Component_Storage_Order;
1245 -----------------------------
1246 -- Check_Debug_Info_Needed --
1247 -----------------------------
1249 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1250 begin
1251 if Debug_Info_Off (T) then
1252 return;
1254 elsif Comes_From_Source (T)
1255 or else Debug_Generated_Code
1256 or else Debug_Flag_VV
1257 or else Needs_Debug_Info (T)
1258 then
1259 Set_Debug_Info_Needed (T);
1260 end if;
1261 end Check_Debug_Info_Needed;
1263 -------------------------------
1264 -- Check_Expression_Function --
1265 -------------------------------
1267 procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1268 Decl : Node_Id;
1270 function Find_Constant (Nod : Node_Id) return Traverse_Result;
1271 -- Function to search for deferred constant
1273 -------------------
1274 -- Find_Constant --
1275 -------------------
1277 function Find_Constant (Nod : Node_Id) return Traverse_Result is
1278 begin
1279 -- When a constant is initialized with the result of a dispatching
1280 -- call, the constant declaration is rewritten as a renaming of the
1281 -- displaced function result. This scenario is not a premature use of
1282 -- a constant even though the Has_Completion flag is not set.
1284 if Is_Entity_Name (Nod)
1285 and then Present (Entity (Nod))
1286 and then Ekind (Entity (Nod)) = E_Constant
1287 and then Scope (Entity (Nod)) = Current_Scope
1288 and then Nkind (Declaration_Node (Entity (Nod))) =
1289 N_Object_Declaration
1290 and then not Is_Imported (Entity (Nod))
1291 and then not Has_Completion (Entity (Nod))
1292 then
1293 Error_Msg_NE
1294 ("premature use of& in call or instance", N, Entity (Nod));
1296 elsif Nkind (Nod) = N_Attribute_Reference then
1297 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);
2402 -- Make sure that we have the necessary routines to
2403 -- implement the packing, and complain now if not.
2405 declare
2406 CS : constant Int := UI_To_Int (Csiz);
2407 RE : constant RE_Id := Get_Id (CS);
2409 begin
2410 if RE /= RE_Null
2411 and then not RTE_Available (RE)
2412 then
2413 Error_Msg_CRT
2414 ("packing of " & UI_Image (Csiz)
2415 & "-bit components",
2416 First_Subtype (Etype (Arr)));
2417 end if;
2418 end;
2419 end if;
2420 end;
2421 end if;
2422 end;
2424 -- Check for Atomic_Components or Aliased with unsuitable packing
2425 -- or explicit component size clause given.
2427 if (Has_Atomic_Components (Arr)
2428 or else
2429 Has_Aliased_Components (Arr))
2430 and then
2431 (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2432 then
2433 Alias_Atomic_Check : declare
2435 procedure Complain_CS (T : String);
2436 -- Outputs error messages for incorrect CS clause or pragma
2437 -- Pack for aliased or atomic components (T is "aliased" or
2438 -- "atomic");
2440 -----------------
2441 -- Complain_CS --
2442 -----------------
2444 procedure Complain_CS (T : String) is
2445 begin
2446 if Has_Component_Size_Clause (Arr) then
2447 Clause :=
2448 Get_Attribute_Definition_Clause
2449 (FS, Attribute_Component_Size);
2451 if Known_Static_Esize (Ctyp) then
2452 Error_Msg_N
2453 ("incorrect component size for "
2454 & T & " components", Clause);
2455 Error_Msg_Uint_1 := Esize (Ctyp);
2456 Error_Msg_N
2457 ("\only allowed value is^", Clause);
2459 else
2460 Error_Msg_N
2461 ("component size cannot be given for "
2462 & T & " components", Clause);
2463 end if;
2465 else
2466 Error_Msg_N
2467 ("cannot pack " & T & " components",
2468 Get_Rep_Pragma (FS, Name_Pack));
2469 end if;
2471 return;
2472 end Complain_CS;
2474 -- Start of processing for Alias_Atomic_Check
2476 begin
2477 -- If object size of component type isn't known, we cannot
2478 -- be sure so we defer to the back end.
2480 if not Known_Static_Esize (Ctyp) then
2481 null;
2483 -- Case where component size has no effect. First check for
2484 -- object size of component type multiple of the storage
2485 -- unit size.
2487 elsif Esize (Ctyp) mod System_Storage_Unit = 0
2489 -- OK in both packing case and component size case if RM
2490 -- size is known and static and same as the object size.
2492 and then
2493 ((Known_Static_RM_Size (Ctyp)
2494 and then Esize (Ctyp) = RM_Size (Ctyp))
2496 -- Or if we have an explicit component size clause and
2497 -- the component size and object size are equal.
2499 or else
2500 (Has_Component_Size_Clause (Arr)
2501 and then Component_Size (Arr) = Esize (Ctyp)))
2502 then
2503 null;
2505 elsif Has_Aliased_Components (Arr)
2506 or else Is_Aliased (Ctyp)
2507 then
2508 Complain_CS ("aliased");
2510 elsif Has_Atomic_Components (Arr)
2511 or else Is_Atomic (Ctyp)
2512 then
2513 Complain_CS ("atomic");
2514 end if;
2515 end Alias_Atomic_Check;
2516 end if;
2518 -- Warn for case of atomic type
2520 Clause := Get_Rep_Pragma (FS, Name_Atomic);
2522 if Present (Clause)
2523 and then not Addressable (Component_Size (FS))
2524 then
2525 Error_Msg_NE
2526 ("non-atomic components of type& may not be "
2527 & "accessible by separate tasks??", Clause, Arr);
2529 if Has_Component_Size_Clause (Arr) then
2530 Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2531 (FS, Attribute_Component_Size));
2532 Error_Msg_N ("\because of component size clause#??", Clause);
2534 elsif Has_Pragma_Pack (Arr) then
2535 Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2536 Error_Msg_N ("\because of pragma Pack#??", Clause);
2537 end if;
2538 end if;
2540 -- Check for scalar storage order
2542 declare
2543 Dummy : Boolean;
2544 begin
2545 Check_Component_Storage_Order
2546 (Encl_Type => Arr,
2547 Comp => Empty,
2548 ADC => Get_Attribute_Definition_Clause
2549 (First_Subtype (Arr),
2550 Attribute_Scalar_Storage_Order),
2551 Comp_ADC_Present => Dummy);
2552 end;
2554 -- Processing that is done only for subtypes
2556 else
2557 -- Acquire alignment from base type
2559 if Unknown_Alignment (Arr) then
2560 Set_Alignment (Arr, Alignment (Base_Type (Arr)));
2561 Adjust_Esize_Alignment (Arr);
2562 end if;
2563 end if;
2565 -- Specific checks for bit-packed arrays
2567 if Is_Bit_Packed_Array (Arr) then
2569 -- Check number of elements for bit packed arrays that come from
2570 -- source and have compile time known ranges. The bit-packed
2571 -- arrays circuitry does not support arrays with more than
2572 -- Integer'Last + 1 elements, and when this restriction is
2573 -- violated, causes incorrect data access.
2575 -- For the case where this is not compile time known, a run-time
2576 -- check should be generated???
2578 if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
2579 declare
2580 Elmts : Uint;
2581 Index : Node_Id;
2582 Ilen : Node_Id;
2583 Ityp : Entity_Id;
2585 begin
2586 Elmts := Uint_1;
2587 Index := First_Index (Arr);
2588 while Present (Index) loop
2589 Ityp := Etype (Index);
2591 -- Never generate an error if any index is of a generic
2592 -- type. We will check this in instances.
2594 if Is_Generic_Type (Ityp) then
2595 Elmts := Uint_0;
2596 exit;
2597 end if;
2599 Ilen :=
2600 Make_Attribute_Reference (Loc,
2601 Prefix => New_Occurrence_Of (Ityp, Loc),
2602 Attribute_Name => Name_Range_Length);
2603 Analyze_And_Resolve (Ilen);
2605 -- No attempt is made to check number of elements if not
2606 -- compile time known.
2608 if Nkind (Ilen) /= N_Integer_Literal then
2609 Elmts := Uint_0;
2610 exit;
2611 end if;
2613 Elmts := Elmts * Intval (Ilen);
2614 Next_Index (Index);
2615 end loop;
2617 if Elmts > Intval (High_Bound
2618 (Scalar_Range (Standard_Integer))) + 1
2619 then
2620 Error_Msg_N
2621 ("bit packed array type may not have "
2622 & "more than Integer''Last+1 elements", Arr);
2623 end if;
2624 end;
2625 end if;
2627 -- Check size
2629 if Known_RM_Size (Arr) then
2630 declare
2631 SizC : constant Node_Id := Size_Clause (Arr);
2632 Discard : Boolean;
2634 begin
2635 -- It is not clear if it is possible to have no size clause
2636 -- at this stage, but it is not worth worrying about. Post
2637 -- error on the entity name in the size clause if present,
2638 -- else on the type entity itself.
2640 if Present (SizC) then
2641 Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
2642 else
2643 Check_Size (Arr, Arr, RM_Size (Arr), Discard);
2644 end if;
2645 end;
2646 end if;
2647 end if;
2649 -- If any of the index types was an enumeration type with a non-
2650 -- standard rep clause, then we indicate that the array type is
2651 -- always packed (even if it is not bit packed).
2653 if Non_Standard_Enum then
2654 Set_Has_Non_Standard_Rep (Base_Type (Arr));
2655 Set_Is_Packed (Base_Type (Arr));
2656 end if;
2658 Set_Component_Alignment_If_Not_Set (Arr);
2660 -- If the array is packed, we must create the packed array type to be
2661 -- used to actually implement the type. This is only needed for real
2662 -- array types (not for string literal types, since they are present
2663 -- only for the front end).
2665 if Is_Packed (Arr)
2666 and then Ekind (Arr) /= E_String_Literal_Subtype
2667 then
2668 Create_Packed_Array_Impl_Type (Arr);
2669 Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
2671 -- Size information of packed array type is copied to the array
2672 -- type, since this is really the representation. But do not
2673 -- override explicit existing size values. If the ancestor subtype
2674 -- is constrained the Packed_Array_Impl_Type will be inherited
2675 -- from it, but the size may have been provided already, and
2676 -- must not be overridden either.
2678 if not Has_Size_Clause (Arr)
2679 and then
2680 (No (Ancestor_Subtype (Arr))
2681 or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
2682 then
2683 Set_Esize (Arr, Esize (Packed_Array_Impl_Type (Arr)));
2684 Set_RM_Size (Arr, RM_Size (Packed_Array_Impl_Type (Arr)));
2685 end if;
2687 if not Has_Alignment_Clause (Arr) then
2688 Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
2689 end if;
2690 end if;
2692 -- For non-packed arrays set the alignment of the array to the
2693 -- alignment of the component type if it is unknown. Skip this
2694 -- in atomic case (atomic arrays may need larger alignments).
2696 if not Is_Packed (Arr)
2697 and then Unknown_Alignment (Arr)
2698 and then Known_Alignment (Ctyp)
2699 and then Known_Static_Component_Size (Arr)
2700 and then Known_Static_Esize (Ctyp)
2701 and then Esize (Ctyp) = Component_Size (Arr)
2702 and then not Is_Atomic (Arr)
2703 then
2704 Set_Alignment (Arr, Alignment (Component_Type (Arr)));
2705 end if;
2706 end Freeze_Array_Type;
2708 -----------------------------
2709 -- Freeze_Generic_Entities --
2710 -----------------------------
2712 function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
2713 E : Entity_Id;
2714 F : Node_Id;
2715 Flist : List_Id;
2717 begin
2718 Flist := New_List;
2719 E := First_Entity (Pack);
2720 while Present (E) loop
2721 if Is_Type (E) and then not Is_Generic_Type (E) then
2722 F := Make_Freeze_Generic_Entity (Sloc (Pack));
2723 Set_Entity (F, E);
2724 Append_To (Flist, F);
2726 elsif Ekind (E) = E_Generic_Package then
2727 Append_List_To (Flist, Freeze_Generic_Entities (E));
2728 end if;
2730 Next_Entity (E);
2731 end loop;
2733 return Flist;
2734 end Freeze_Generic_Entities;
2736 --------------------
2737 -- Freeze_Profile --
2738 --------------------
2740 function Freeze_Profile (E : Entity_Id) return Boolean is
2741 F_Type : Entity_Id;
2742 R_Type : Entity_Id;
2743 Warn_Node : Node_Id;
2745 begin
2746 -- Loop through formals
2748 Formal := First_Formal (E);
2749 while Present (Formal) loop
2750 F_Type := Etype (Formal);
2752 -- AI05-0151: incomplete types can appear in a profile. By the
2753 -- time the entity is frozen, the full view must be available,
2754 -- unless it is a limited view.
2756 if Is_Incomplete_Type (F_Type)
2757 and then Present (Full_View (F_Type))
2758 and then not From_Limited_With (F_Type)
2759 then
2760 F_Type := Full_View (F_Type);
2761 Set_Etype (Formal, F_Type);
2762 end if;
2764 Freeze_And_Append (F_Type, N, Result);
2766 if Is_Private_Type (F_Type)
2767 and then Is_Private_Type (Base_Type (F_Type))
2768 and then No (Full_View (Base_Type (F_Type)))
2769 and then not Is_Generic_Type (F_Type)
2770 and then not Is_Derived_Type (F_Type)
2771 then
2772 -- If the type of a formal is incomplete, subprogram is being
2773 -- frozen prematurely. Within an instance (but not within a
2774 -- wrapper package) this is an artifact of our need to regard
2775 -- the end of an instantiation as a freeze point. Otherwise it
2776 -- is a definite error.
2778 if In_Instance then
2779 Set_Is_Frozen (E, False);
2780 Result := No_List;
2781 return False;
2783 elsif not After_Last_Declaration
2784 and then not Freezing_Library_Level_Tagged_Type
2785 then
2786 Error_Msg_Node_1 := F_Type;
2787 Error_Msg
2788 ("type & must be fully defined before this point", Loc);
2789 end if;
2790 end if;
2792 -- Check suspicious parameter for C function. These tests apply
2793 -- only to exported/imported subprograms.
2795 if Warn_On_Export_Import
2796 and then Comes_From_Source (E)
2797 and then (Convention (E) = Convention_C
2798 or else
2799 Convention (E) = Convention_CPP)
2800 and then (Is_Imported (E) or else Is_Exported (E))
2801 and then Convention (E) /= Convention (Formal)
2802 and then not Has_Warnings_Off (E)
2803 and then not Has_Warnings_Off (F_Type)
2804 and then not Has_Warnings_Off (Formal)
2805 then
2806 -- Qualify mention of formals with subprogram name
2808 Error_Msg_Qual_Level := 1;
2810 -- Check suspicious use of fat C pointer
2812 if Is_Access_Type (F_Type)
2813 and then Esize (F_Type) > Ttypes.System_Address_Size
2814 then
2815 Error_Msg_N
2816 ("?x?type of & does not correspond to C pointer!", Formal);
2818 -- Check suspicious return of boolean
2820 elsif Root_Type (F_Type) = Standard_Boolean
2821 and then Convention (F_Type) = Convention_Ada
2822 and then not Has_Warnings_Off (F_Type)
2823 and then not Has_Size_Clause (F_Type)
2824 and then VM_Target = No_VM
2825 then
2826 Error_Msg_N
2827 ("& is an 8-bit Ada Boolean?x?", Formal);
2828 Error_Msg_N
2829 ("\use appropriate corresponding type in C "
2830 & "(e.g. char)?x?", Formal);
2832 -- Check suspicious tagged type
2834 elsif (Is_Tagged_Type (F_Type)
2835 or else
2836 (Is_Access_Type (F_Type)
2837 and then Is_Tagged_Type (Designated_Type (F_Type))))
2838 and then Convention (E) = Convention_C
2839 then
2840 Error_Msg_N
2841 ("?x?& involves a tagged type which does not "
2842 & "correspond to any C type!", Formal);
2844 -- Check wrong convention subprogram pointer
2846 elsif Ekind (F_Type) = E_Access_Subprogram_Type
2847 and then not Has_Foreign_Convention (F_Type)
2848 then
2849 Error_Msg_N
2850 ("?x?subprogram pointer & should "
2851 & "have foreign convention!", Formal);
2852 Error_Msg_Sloc := Sloc (F_Type);
2853 Error_Msg_NE
2854 ("\?x?add Convention pragma to declaration of &#",
2855 Formal, F_Type);
2856 end if;
2858 -- Turn off name qualification after message output
2860 Error_Msg_Qual_Level := 0;
2861 end if;
2863 -- Check for unconstrained array in exported foreign convention
2864 -- case.
2866 if Has_Foreign_Convention (E)
2867 and then not Is_Imported (E)
2868 and then Is_Array_Type (F_Type)
2869 and then not Is_Constrained (F_Type)
2870 and then Warn_On_Export_Import
2872 -- Exclude VM case, since both .NET and JVM can handle
2873 -- unconstrained arrays without a problem.
2875 and then VM_Target = No_VM
2876 then
2877 Error_Msg_Qual_Level := 1;
2879 -- If this is an inherited operation, place the warning on
2880 -- the derived type declaration, rather than on the original
2881 -- subprogram.
2883 if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
2884 then
2885 Warn_Node := Parent (E);
2887 if Formal = First_Formal (E) then
2888 Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
2889 end if;
2890 else
2891 Warn_Node := Formal;
2892 end if;
2894 Error_Msg_NE ("?x?type of argument& is unconstrained array",
2895 Warn_Node, Formal);
2896 Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
2897 Warn_Node, Formal);
2898 Error_Msg_Qual_Level := 0;
2899 end if;
2901 if not From_Limited_With (F_Type) then
2902 if Is_Access_Type (F_Type) then
2903 F_Type := Designated_Type (F_Type);
2904 end if;
2906 -- If the formal is an anonymous_access_to_subprogram
2907 -- freeze the subprogram type as well, to prevent
2908 -- scope anomalies in gigi, because there is no other
2909 -- clear point at which it could be frozen.
2911 if Is_Itype (Etype (Formal))
2912 and then Ekind (F_Type) = E_Subprogram_Type
2913 then
2914 Freeze_And_Append (F_Type, N, Result);
2915 end if;
2916 end if;
2918 Next_Formal (Formal);
2919 end loop;
2921 -- Case of function: similar checks on return type
2923 if Ekind (E) = E_Function then
2925 -- Check whether function is declared elsewhere.
2927 Late_Freezing :=
2928 Get_Source_Unit (E) /= Get_Source_Unit (N)
2929 and then Returns_Limited_View (E)
2930 and then not In_Open_Scopes (Scope (E));
2932 -- Freeze return type
2934 R_Type := Etype (E);
2936 -- AI05-0151: the return type may have been incomplete
2937 -- at the point of declaration. Replace it with the full
2938 -- view, unless the current type is a limited view. In
2939 -- that case the full view is in a different unit, and
2940 -- gigi finds the non-limited view after the other unit
2941 -- is elaborated.
2943 if Ekind (R_Type) = E_Incomplete_Type
2944 and then Present (Full_View (R_Type))
2945 and then not From_Limited_With (R_Type)
2946 then
2947 R_Type := Full_View (R_Type);
2948 Set_Etype (E, R_Type);
2950 -- If the return type is a limited view and the non-
2951 -- limited view is still incomplete, the function has
2952 -- to be frozen at a later time.
2954 elsif Ekind (R_Type) = E_Incomplete_Type
2955 and then From_Limited_With (R_Type)
2956 and then
2957 Ekind (Non_Limited_View (R_Type)) = E_Incomplete_Type
2958 then
2959 Set_Is_Frozen (E, False);
2960 Set_Returns_Limited_View (E);
2961 return False;
2962 end if;
2964 Freeze_And_Append (R_Type, N, Result);
2966 -- Check suspicious return type for C function
2968 if Warn_On_Export_Import
2969 and then (Convention (E) = Convention_C
2970 or else
2971 Convention (E) = Convention_CPP)
2972 and then (Is_Imported (E) or else Is_Exported (E))
2973 then
2974 -- Check suspicious return of fat C pointer
2976 if Is_Access_Type (R_Type)
2977 and then Esize (R_Type) > Ttypes.System_Address_Size
2978 and then not Has_Warnings_Off (E)
2979 and then not Has_Warnings_Off (R_Type)
2980 then
2981 Error_Msg_N ("?x?return type of& does not "
2982 & "correspond to C pointer!", E);
2984 -- Check suspicious return of boolean
2986 elsif Root_Type (R_Type) = Standard_Boolean
2987 and then Convention (R_Type) = Convention_Ada
2988 and then VM_Target = No_VM
2989 and then not Has_Warnings_Off (E)
2990 and then not Has_Warnings_Off (R_Type)
2991 and then not Has_Size_Clause (R_Type)
2992 then
2993 declare
2994 N : constant Node_Id :=
2995 Result_Definition (Declaration_Node (E));
2996 begin
2997 Error_Msg_NE
2998 ("return type of & is an 8-bit Ada Boolean?x?", N, E);
2999 Error_Msg_NE
3000 ("\use appropriate corresponding type in C "
3001 & "(e.g. char)?x?", N, E);
3002 end;
3004 -- Check suspicious return tagged type
3006 elsif (Is_Tagged_Type (R_Type)
3007 or else (Is_Access_Type (R_Type)
3008 and then
3009 Is_Tagged_Type
3010 (Designated_Type (R_Type))))
3011 and then Convention (E) = Convention_C
3012 and then not Has_Warnings_Off (E)
3013 and then not Has_Warnings_Off (R_Type)
3014 then
3015 Error_Msg_N ("?x?return type of & does not "
3016 & "correspond to C type!", E);
3018 -- Check return of wrong convention subprogram pointer
3020 elsif Ekind (R_Type) = E_Access_Subprogram_Type
3021 and then not Has_Foreign_Convention (R_Type)
3022 and then not Has_Warnings_Off (E)
3023 and then not Has_Warnings_Off (R_Type)
3024 then
3025 Error_Msg_N ("?x?& should return a foreign "
3026 & "convention subprogram pointer", E);
3027 Error_Msg_Sloc := Sloc (R_Type);
3028 Error_Msg_NE
3029 ("\?x?add Convention pragma to declaration of& #",
3030 E, R_Type);
3031 end if;
3032 end if;
3034 -- Give warning for suspicious return of a result of an
3035 -- unconstrained array type in a foreign convention function.
3037 if Has_Foreign_Convention (E)
3039 -- We are looking for a return of unconstrained array
3041 and then Is_Array_Type (R_Type)
3042 and then not Is_Constrained (R_Type)
3044 -- Exclude imported routines, the warning does not belong on
3045 -- the import, but rather on the routine definition.
3047 and then not Is_Imported (E)
3049 -- Exclude VM case, since both .NET and JVM can handle return
3050 -- of unconstrained arrays without a problem.
3052 and then VM_Target = No_VM
3054 -- Check that general warning is enabled, and that it is not
3055 -- suppressed for this particular case.
3057 and then Warn_On_Export_Import
3058 and then not Has_Warnings_Off (E)
3059 and then not Has_Warnings_Off (R_Type)
3060 then
3061 Error_Msg_N ("?x?foreign convention function& should not " &
3062 "return unconstrained array!", E);
3063 end if;
3064 end if;
3066 return True;
3067 end Freeze_Profile;
3069 ------------------------
3070 -- Freeze_Record_Type --
3071 ------------------------
3073 procedure Freeze_Record_Type (Rec : Entity_Id) is
3074 ADC : Node_Id;
3075 Comp : Entity_Id;
3076 IR : Node_Id;
3077 Prev : Entity_Id;
3079 Junk : Boolean;
3080 pragma Warnings (Off, Junk);
3082 Rec_Pushed : Boolean := False;
3083 -- Set True if the record type scope Rec has been pushed on the scope
3084 -- stack. Needed for the analysis of delayed aspects specified to the
3085 -- components of Rec.
3087 SSO_ADC : Node_Id;
3088 -- Scalar_Storage_Order attribute definition clause for the record
3090 Unplaced_Component : Boolean := False;
3091 -- Set True if we find at least one component with no component
3092 -- clause (used to warn about useless Pack pragmas).
3094 Placed_Component : Boolean := False;
3095 -- Set True if we find at least one component with a component
3096 -- clause (used to warn about useless Bit_Order pragmas, and also
3097 -- to detect cases where Implicit_Packing may have an effect).
3099 Aliased_Component : Boolean := False;
3100 -- Set True if we find at least one component which is aliased. This
3101 -- is used to prevent Implicit_Packing of the record, since packing
3102 -- cannot modify the size of alignment of an aliased component.
3104 SSO_ADC_Component : Boolean := False;
3105 -- Set True if we find at least one component whose type has a
3106 -- Scalar_Storage_Order attribute definition clause.
3108 All_Scalar_Components : Boolean := True;
3109 -- Set False if we encounter a component of a non-scalar type
3111 Scalar_Component_Total_RM_Size : Uint := Uint_0;
3112 Scalar_Component_Total_Esize : Uint := Uint_0;
3113 -- Accumulates total RM_Size values and total Esize values of all
3114 -- scalar components. Used for processing of Implicit_Packing.
3116 function Check_Allocator (N : Node_Id) return Node_Id;
3117 -- If N is an allocator, possibly wrapped in one or more level of
3118 -- qualified expression(s), return the inner allocator node, else
3119 -- return Empty.
3121 procedure Check_Itype (Typ : Entity_Id);
3122 -- If the component subtype is an access to a constrained subtype of
3123 -- an already frozen type, make the subtype frozen as well. It might
3124 -- otherwise be frozen in the wrong scope, and a freeze node on
3125 -- subtype has no effect. Similarly, if the component subtype is a
3126 -- regular (not protected) access to subprogram, set the anonymous
3127 -- subprogram type to frozen as well, to prevent an out-of-scope
3128 -- freeze node at some eventual point of call. Protected operations
3129 -- are handled elsewhere.
3131 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3132 -- Make sure that all types mentioned in Discrete_Choices of the
3133 -- variants referenceed by the Variant_Part VP are frozen. This is
3134 -- a recursive routine to deal with nested variants.
3136 ---------------------
3137 -- Check_Allocator --
3138 ---------------------
3140 function Check_Allocator (N : Node_Id) return Node_Id is
3141 Inner : Node_Id;
3142 begin
3143 Inner := N;
3144 loop
3145 if Nkind (Inner) = N_Allocator then
3146 return Inner;
3147 elsif Nkind (Inner) = N_Qualified_Expression then
3148 Inner := Expression (Inner);
3149 else
3150 return Empty;
3151 end if;
3152 end loop;
3153 end Check_Allocator;
3155 -----------------
3156 -- Check_Itype --
3157 -----------------
3159 procedure Check_Itype (Typ : Entity_Id) is
3160 Desig : constant Entity_Id := Designated_Type (Typ);
3162 begin
3163 if not Is_Frozen (Desig)
3164 and then Is_Frozen (Base_Type (Desig))
3165 then
3166 Set_Is_Frozen (Desig);
3168 -- In addition, add an Itype_Reference to ensure that the
3169 -- access subtype is elaborated early enough. This cannot be
3170 -- done if the subtype may depend on discriminants.
3172 if Ekind (Comp) = E_Component
3173 and then Is_Itype (Etype (Comp))
3174 and then not Has_Discriminants (Rec)
3175 then
3176 IR := Make_Itype_Reference (Sloc (Comp));
3177 Set_Itype (IR, Desig);
3178 Add_To_Result (IR);
3179 end if;
3181 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
3182 and then Convention (Desig) /= Convention_Protected
3183 then
3184 Set_Is_Frozen (Desig);
3185 end if;
3186 end Check_Itype;
3188 ------------------------------------
3189 -- Freeze_Choices_In_Variant_Part --
3190 ------------------------------------
3192 procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
3193 pragma Assert (Nkind (VP) = N_Variant_Part);
3195 Variant : Node_Id;
3196 Choice : Node_Id;
3197 CL : Node_Id;
3199 begin
3200 -- Loop through variants
3202 Variant := First_Non_Pragma (Variants (VP));
3203 while Present (Variant) loop
3205 -- Loop through choices, checking that all types are frozen
3207 Choice := First_Non_Pragma (Discrete_Choices (Variant));
3208 while Present (Choice) loop
3209 if Nkind (Choice) in N_Has_Etype
3210 and then Present (Etype (Choice))
3211 then
3212 Freeze_And_Append (Etype (Choice), N, Result);
3213 end if;
3215 Next_Non_Pragma (Choice);
3216 end loop;
3218 -- Check for nested variant part to process
3220 CL := Component_List (Variant);
3222 if not Null_Present (CL) then
3223 if Present (Variant_Part (CL)) then
3224 Freeze_Choices_In_Variant_Part (Variant_Part (CL));
3225 end if;
3226 end if;
3228 Next_Non_Pragma (Variant);
3229 end loop;
3230 end Freeze_Choices_In_Variant_Part;
3232 -- Start of processing for Freeze_Record_Type
3234 begin
3235 -- Deal with delayed aspect specifications for components. The
3236 -- analysis of the aspect is required to be delayed to the freeze
3237 -- point, thus we analyze the pragma or attribute definition
3238 -- clause in the tree at this point. We also analyze the aspect
3239 -- specification node at the freeze point when the aspect doesn't
3240 -- correspond to pragma/attribute definition clause.
3242 Comp := First_Entity (Rec);
3243 while Present (Comp) loop
3244 if Ekind (Comp) = E_Component
3245 and then Has_Delayed_Aspects (Comp)
3246 then
3247 if not Rec_Pushed then
3248 Push_Scope (Rec);
3249 Rec_Pushed := True;
3251 -- The visibility to the discriminants must be restored in
3252 -- order to properly analyze the aspects.
3254 if Has_Discriminants (Rec) then
3255 Install_Discriminants (Rec);
3256 end if;
3257 end if;
3259 Analyze_Aspects_At_Freeze_Point (Comp);
3260 end if;
3262 Next_Entity (Comp);
3263 end loop;
3265 -- Pop the scope if Rec scope has been pushed on the scope stack
3266 -- during the delayed aspect analysis process.
3268 if Rec_Pushed then
3269 if Has_Discriminants (Rec) then
3270 Uninstall_Discriminants (Rec);
3271 end if;
3273 Pop_Scope;
3274 end if;
3276 -- Freeze components and embedded subtypes
3278 Comp := First_Entity (Rec);
3279 Prev := Empty;
3280 while Present (Comp) loop
3281 if Is_Aliased (Comp) then
3282 Aliased_Component := True;
3283 end if;
3285 -- Handle the component and discriminant case
3287 if Ekind_In (Comp, E_Component, E_Discriminant) then
3288 declare
3289 CC : constant Node_Id := Component_Clause (Comp);
3291 begin
3292 -- Freezing a record type freezes the type of each of its
3293 -- components. However, if the type of the component is
3294 -- part of this record, we do not want or need a separate
3295 -- Freeze_Node. Note that Is_Itype is wrong because that's
3296 -- also set in private type cases. We also can't check for
3297 -- the Scope being exactly Rec because of private types and
3298 -- record extensions.
3300 if Is_Itype (Etype (Comp))
3301 and then Is_Record_Type (Underlying_Type
3302 (Scope (Etype (Comp))))
3303 then
3304 Undelay_Type (Etype (Comp));
3305 end if;
3307 Freeze_And_Append (Etype (Comp), N, Result);
3309 -- Warn for pragma Pack overriding foreign convention
3311 if Has_Foreign_Convention (Etype (Comp))
3312 and then Has_Pragma_Pack (Rec)
3314 -- Don't warn for aliased components, since override
3315 -- cannot happen in that case.
3317 and then not Is_Aliased (Comp)
3318 then
3319 declare
3320 CN : constant Name_Id :=
3321 Get_Convention_Name (Convention (Etype (Comp)));
3322 PP : constant Node_Id :=
3323 Get_Pragma (Rec, Pragma_Pack);
3324 begin
3325 if Present (PP) then
3326 Error_Msg_Name_1 := CN;
3327 Error_Msg_Sloc := Sloc (Comp);
3328 Error_Msg_N
3329 ("pragma Pack affects convention % component#??",
3330 PP);
3331 Error_Msg_Name_1 := CN;
3332 Error_Msg_NE
3333 ("\component & may not have % compatible "
3334 & "representation??", PP, Comp);
3335 end if;
3336 end;
3337 end if;
3339 -- Check for error of component clause given for variable
3340 -- sized type. We have to delay this test till this point,
3341 -- since the component type has to be frozen for us to know
3342 -- if it is variable length.
3344 if Present (CC) then
3345 Placed_Component := True;
3347 -- We omit this test in a generic context, it will be
3348 -- applied at instantiation time.
3350 if Inside_A_Generic then
3351 null;
3353 -- Also omit this test in CodePeer mode, since we do not
3354 -- have sufficient info on size and rep clauses.
3356 elsif CodePeer_Mode then
3357 null;
3359 -- Omit check if component has a generic type. This can
3360 -- happen in an instantiation within a generic in ASIS
3361 -- mode, where we force freeze actions without full
3362 -- expansion.
3364 elsif Is_Generic_Type (Etype (Comp)) then
3365 null;
3367 -- Do the check
3369 elsif not
3370 Size_Known_At_Compile_Time
3371 (Underlying_Type (Etype (Comp)))
3372 then
3373 Error_Msg_N
3374 ("component clause not allowed for variable " &
3375 "length component", CC);
3376 end if;
3378 else
3379 Unplaced_Component := True;
3380 end if;
3382 -- Case of component requires byte alignment
3384 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
3386 -- Set the enclosing record to also require byte align
3388 Set_Must_Be_On_Byte_Boundary (Rec);
3390 -- Check for component clause that is inconsistent with
3391 -- the required byte boundary alignment.
3393 if Present (CC)
3394 and then Normalized_First_Bit (Comp) mod
3395 System_Storage_Unit /= 0
3396 then
3397 Error_Msg_N
3398 ("component & must be byte aligned",
3399 Component_Name (Component_Clause (Comp)));
3400 end if;
3401 end if;
3402 end;
3403 end if;
3405 -- Gather data for possible Implicit_Packing later. Note that at
3406 -- this stage we might be dealing with a real component, or with
3407 -- an implicit subtype declaration.
3409 if not Is_Scalar_Type (Etype (Comp)) then
3410 All_Scalar_Components := False;
3411 else
3412 Scalar_Component_Total_RM_Size :=
3413 Scalar_Component_Total_RM_Size + RM_Size (Etype (Comp));
3414 Scalar_Component_Total_Esize :=
3415 Scalar_Component_Total_Esize + Esize (Etype (Comp));
3416 end if;
3418 -- If the component is an Itype with Delayed_Freeze and is either
3419 -- a record or array subtype and its base type has not yet been
3420 -- frozen, we must remove this from the entity list of this record
3421 -- and put it on the entity list of the scope of its base type.
3422 -- Note that we know that this is not the type of a component
3423 -- since we cleared Has_Delayed_Freeze for it in the previous
3424 -- loop. Thus this must be the Designated_Type of an access type,
3425 -- which is the type of a component.
3427 if Is_Itype (Comp)
3428 and then Is_Type (Scope (Comp))
3429 and then Is_Composite_Type (Comp)
3430 and then Base_Type (Comp) /= Comp
3431 and then Has_Delayed_Freeze (Comp)
3432 and then not Is_Frozen (Base_Type (Comp))
3433 then
3434 declare
3435 Will_Be_Frozen : Boolean := False;
3436 S : Entity_Id;
3438 begin
3439 -- We have a difficult case to handle here. Suppose Rec is
3440 -- subtype being defined in a subprogram that's created as
3441 -- part of the freezing of Rec'Base. In that case, we know
3442 -- that Comp'Base must have already been frozen by the time
3443 -- we get to elaborate this because Gigi doesn't elaborate
3444 -- any bodies until it has elaborated all of the declarative
3445 -- part. But Is_Frozen will not be set at this point because
3446 -- we are processing code in lexical order.
3448 -- We detect this case by going up the Scope chain of Rec
3449 -- and seeing if we have a subprogram scope before reaching
3450 -- the top of the scope chain or that of Comp'Base. If we
3451 -- do, then mark that Comp'Base will actually be frozen. If
3452 -- so, we merely undelay it.
3454 S := Scope (Rec);
3455 while Present (S) loop
3456 if Is_Subprogram (S) then
3457 Will_Be_Frozen := True;
3458 exit;
3459 elsif S = Scope (Base_Type (Comp)) then
3460 exit;
3461 end if;
3463 S := Scope (S);
3464 end loop;
3466 if Will_Be_Frozen then
3467 Undelay_Type (Comp);
3469 else
3470 if Present (Prev) then
3471 Set_Next_Entity (Prev, Next_Entity (Comp));
3472 else
3473 Set_First_Entity (Rec, Next_Entity (Comp));
3474 end if;
3476 -- Insert in entity list of scope of base type (which
3477 -- must be an enclosing scope, because still unfrozen).
3479 Append_Entity (Comp, Scope (Base_Type (Comp)));
3480 end if;
3481 end;
3483 -- If the component is an access type with an allocator as default
3484 -- value, the designated type will be frozen by the corresponding
3485 -- expression in init_proc. In order to place the freeze node for
3486 -- the designated type before that for the current record type,
3487 -- freeze it now.
3489 -- Same process if the component is an array of access types,
3490 -- initialized with an aggregate. If the designated type is
3491 -- private, it cannot contain allocators, and it is premature
3492 -- to freeze the type, so we check for this as well.
3494 elsif Is_Access_Type (Etype (Comp))
3495 and then Present (Parent (Comp))
3496 and then Present (Expression (Parent (Comp)))
3497 then
3498 declare
3499 Alloc : constant Node_Id :=
3500 Check_Allocator (Expression (Parent (Comp)));
3502 begin
3503 if Present (Alloc) then
3505 -- If component is pointer to a class-wide type, freeze
3506 -- the specific type in the expression being allocated.
3507 -- The expression may be a subtype indication, in which
3508 -- case freeze the subtype mark.
3510 if Is_Class_Wide_Type
3511 (Designated_Type (Etype (Comp)))
3512 then
3513 if Is_Entity_Name (Expression (Alloc)) then
3514 Freeze_And_Append
3515 (Entity (Expression (Alloc)), N, Result);
3517 elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
3518 then
3519 Freeze_And_Append
3520 (Entity (Subtype_Mark (Expression (Alloc))),
3521 N, Result);
3522 end if;
3524 elsif Is_Itype (Designated_Type (Etype (Comp))) then
3525 Check_Itype (Etype (Comp));
3527 else
3528 Freeze_And_Append
3529 (Designated_Type (Etype (Comp)), N, Result);
3530 end if;
3531 end if;
3532 end;
3534 elsif Is_Access_Type (Etype (Comp))
3535 and then Is_Itype (Designated_Type (Etype (Comp)))
3536 then
3537 Check_Itype (Etype (Comp));
3539 -- Freeze the designated type when initializing a component with
3540 -- an aggregate in case the aggregate contains allocators.
3542 -- type T is ...;
3543 -- type T_Ptr is access all T;
3544 -- type T_Array is array ... of T_Ptr;
3546 -- type Rec is record
3547 -- Comp : T_Array := (others => ...);
3548 -- end record;
3550 elsif Is_Array_Type (Etype (Comp))
3551 and then Is_Access_Type (Component_Type (Etype (Comp)))
3552 then
3553 declare
3554 Comp_Par : constant Node_Id := Parent (Comp);
3555 Desig_Typ : constant Entity_Id :=
3556 Designated_Type
3557 (Component_Type (Etype (Comp)));
3559 begin
3560 -- The only case when this sort of freezing is not done is
3561 -- when the designated type is class-wide and the root type
3562 -- is the record owning the component. This scenario results
3563 -- in a circularity because the class-wide type requires
3564 -- primitives that have not been created yet as the root
3565 -- type is in the process of being frozen.
3567 -- type Rec is tagged;
3568 -- type Rec_Ptr is access all Rec'Class;
3569 -- type Rec_Array is array ... of Rec_Ptr;
3571 -- type Rec is record
3572 -- Comp : Rec_Array := (others => ...);
3573 -- end record;
3575 if Is_Class_Wide_Type (Desig_Typ)
3576 and then Root_Type (Desig_Typ) = Rec
3577 then
3578 null;
3580 elsif Is_Fully_Defined (Desig_Typ)
3581 and then Present (Comp_Par)
3582 and then Nkind (Comp_Par) = N_Component_Declaration
3583 and then Present (Expression (Comp_Par))
3584 and then Nkind (Expression (Comp_Par)) = N_Aggregate
3585 then
3586 Freeze_And_Append (Desig_Typ, N, Result);
3587 end if;
3588 end;
3589 end if;
3591 Prev := Comp;
3592 Next_Entity (Comp);
3593 end loop;
3595 -- Deal with default setting of reverse storage order
3597 Set_SSO_From_Default (Rec);
3599 -- Check consistent attribute setting on component types
3601 SSO_ADC := Get_Attribute_Definition_Clause
3602 (Rec, Attribute_Scalar_Storage_Order);
3604 declare
3605 Comp_ADC_Present : Boolean;
3606 begin
3607 Comp := First_Component (Rec);
3608 while Present (Comp) loop
3609 Check_Component_Storage_Order
3610 (Encl_Type => Rec,
3611 Comp => Comp,
3612 ADC => SSO_ADC,
3613 Comp_ADC_Present => Comp_ADC_Present);
3614 SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
3615 Next_Component (Comp);
3616 end loop;
3617 end;
3619 -- Now deal with reverse storage order/bit order issues
3621 if Present (SSO_ADC) then
3623 -- Check compatibility of Scalar_Storage_Order with Bit_Order, if
3624 -- the former is specified.
3626 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
3628 -- Note: report error on Rec, not on SSO_ADC, as ADC may apply
3629 -- to some ancestor type.
3631 Error_Msg_Sloc := Sloc (SSO_ADC);
3632 Error_Msg_N
3633 ("scalar storage order for& specified# inconsistent with "
3634 & "bit order", Rec);
3635 end if;
3637 -- Warn if there is an Scalar_Storage_Order attribute definition
3638 -- clause but no component clause, no component that itself has
3639 -- such an attribute definition, and no pragma Pack.
3641 if not (Placed_Component
3642 or else
3643 SSO_ADC_Component
3644 or else
3645 Is_Packed (Rec))
3646 then
3647 Error_Msg_N
3648 ("??scalar storage order specified but no component clause",
3649 SSO_ADC);
3650 end if;
3651 end if;
3653 -- Deal with Bit_Order aspect
3655 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
3657 if Present (ADC) and then Base_Type (Rec) = Rec then
3658 if not (Placed_Component
3659 or else Present (SSO_ADC)
3660 or else Is_Packed (Rec))
3661 then
3662 -- Warn if clause has no effect when no component clause is
3663 -- present, but suppress warning if the Bit_Order is required
3664 -- due to the presence of a Scalar_Storage_Order attribute.
3666 Error_Msg_N
3667 ("??bit order specification has no effect", ADC);
3668 Error_Msg_N
3669 ("\??since no component clauses were specified", ADC);
3671 -- Here is where we do the processing to adjust component clauses
3672 -- for reversed bit order, when not using reverse SSO.
3674 elsif Reverse_Bit_Order (Rec)
3675 and then not Reverse_Storage_Order (Rec)
3676 then
3677 Adjust_Record_For_Reverse_Bit_Order (Rec);
3679 -- Case where we have both an explicit Bit_Order and the same
3680 -- Scalar_Storage_Order: leave record untouched, the back-end
3681 -- will take care of required layout conversions.
3683 else
3684 null;
3686 end if;
3687 end if;
3689 -- Complete error checking on record representation clause (e.g.
3690 -- overlap of components). This is called after adjusting the
3691 -- record for reverse bit order.
3693 declare
3694 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
3695 begin
3696 if Present (RRC) then
3697 Check_Record_Representation_Clause (RRC);
3698 end if;
3699 end;
3701 -- Set OK_To_Reorder_Components depending on debug flags
3703 if Is_Base_Type (Rec) and then Convention (Rec) = Convention_Ada then
3704 if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
3705 or else
3706 (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
3707 then
3708 Set_OK_To_Reorder_Components (Rec);
3709 end if;
3710 end if;
3712 -- Check for useless pragma Pack when all components placed. We only
3713 -- do this check for record types, not subtypes, since a subtype may
3714 -- have all its components placed, and it still makes perfectly good
3715 -- sense to pack other subtypes or the parent type. We do not give
3716 -- this warning if Optimize_Alignment is set to Space, since the
3717 -- pragma Pack does have an effect in this case (it always resets
3718 -- the alignment to one).
3720 if Ekind (Rec) = E_Record_Type
3721 and then Is_Packed (Rec)
3722 and then not Unplaced_Component
3723 and then Optimize_Alignment /= 'S'
3724 then
3725 -- Reset packed status. Probably not necessary, but we do it so
3726 -- that there is no chance of the back end doing something strange
3727 -- with this redundant indication of packing.
3729 Set_Is_Packed (Rec, False);
3731 -- Give warning if redundant constructs warnings on
3733 if Warn_On_Redundant_Constructs then
3734 Error_Msg_N -- CODEFIX
3735 ("??pragma Pack has no effect, no unplaced components",
3736 Get_Rep_Pragma (Rec, Name_Pack));
3737 end if;
3738 end if;
3740 -- If this is the record corresponding to a remote type, freeze the
3741 -- remote type here since that is what we are semantically freezing.
3742 -- This prevents the freeze node for that type in an inner scope.
3744 if Ekind (Rec) = E_Record_Type then
3745 if Present (Corresponding_Remote_Type (Rec)) then
3746 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
3747 end if;
3749 -- Check for controlled components and unchecked unions.
3751 Comp := First_Component (Rec);
3752 while Present (Comp) loop
3754 -- Do not set Has_Controlled_Component on a class-wide
3755 -- equivalent type. See Make_CW_Equivalent_Type.
3757 if not Is_Class_Wide_Equivalent_Type (Rec)
3758 and then
3759 (Has_Controlled_Component (Etype (Comp))
3760 or else
3761 (Chars (Comp) /= Name_uParent
3762 and then Is_Controlled (Etype (Comp)))
3763 or else
3764 (Is_Protected_Type (Etype (Comp))
3765 and then
3766 Present (Corresponding_Record_Type (Etype (Comp)))
3767 and then
3768 Has_Controlled_Component
3769 (Corresponding_Record_Type (Etype (Comp)))))
3770 then
3771 Set_Has_Controlled_Component (Rec);
3772 end if;
3774 if Has_Unchecked_Union (Etype (Comp)) then
3775 Set_Has_Unchecked_Union (Rec);
3776 end if;
3778 -- Scan component declaration for likely misuses of current
3779 -- instance, either in a constraint or a default expression.
3781 if Has_Per_Object_Constraint (Comp) then
3782 Check_Current_Instance (Parent (Comp));
3783 end if;
3785 Next_Component (Comp);
3786 end loop;
3787 end if;
3789 -- Enforce the restriction that access attributes with a current
3790 -- instance prefix can only apply to limited types. This comment
3791 -- is floating here, but does not seem to belong here???
3793 -- Set component alignment if not otherwise already set
3795 Set_Component_Alignment_If_Not_Set (Rec);
3797 -- For first subtypes, check if there are any fixed-point fields with
3798 -- component clauses, where we must check the size. This is not done
3799 -- till the freeze point since for fixed-point types, we do not know
3800 -- the size until the type is frozen. Similar processing applies to
3801 -- bit packed arrays.
3803 if Is_First_Subtype (Rec) then
3804 Comp := First_Component (Rec);
3805 while Present (Comp) loop
3806 if Present (Component_Clause (Comp))
3807 and then (Is_Fixed_Point_Type (Etype (Comp))
3808 or else Is_Bit_Packed_Array (Etype (Comp)))
3809 then
3810 Check_Size
3811 (Component_Name (Component_Clause (Comp)),
3812 Etype (Comp),
3813 Esize (Comp),
3814 Junk);
3815 end if;
3817 Next_Component (Comp);
3818 end loop;
3819 end if;
3821 -- Generate warning for applying C or C++ convention to a record
3822 -- with discriminants. This is suppressed for the unchecked union
3823 -- case, since the whole point in this case is interface C. We also
3824 -- do not generate this within instantiations, since we will have
3825 -- generated a message on the template.
3827 if Has_Discriminants (E)
3828 and then not Is_Unchecked_Union (E)
3829 and then (Convention (E) = Convention_C
3830 or else
3831 Convention (E) = Convention_CPP)
3832 and then Comes_From_Source (E)
3833 and then not In_Instance
3834 and then not Has_Warnings_Off (E)
3835 and then not Has_Warnings_Off (Base_Type (E))
3836 then
3837 declare
3838 Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
3839 A2 : Node_Id;
3841 begin
3842 if Present (Cprag) then
3843 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
3845 if Convention (E) = Convention_C then
3846 Error_Msg_N
3847 ("?x?variant record has no direct equivalent in C",
3848 A2);
3849 else
3850 Error_Msg_N
3851 ("?x?variant record has no direct equivalent in C++",
3852 A2);
3853 end if;
3855 Error_Msg_NE
3856 ("\?x?use of convention for type& is dubious", A2, E);
3857 end if;
3858 end;
3859 end if;
3861 -- See if Size is too small as is (and implicit packing might help)
3863 if not Is_Packed (Rec)
3865 -- No implicit packing if even one component is explicitly placed
3867 and then not Placed_Component
3869 -- Or even one component is aliased
3871 and then not Aliased_Component
3873 -- Must have size clause and all scalar components
3875 and then Has_Size_Clause (Rec)
3876 and then All_Scalar_Components
3878 -- Do not try implicit packing on records with discriminants, too
3879 -- complicated, especially in the variant record case.
3881 and then not Has_Discriminants (Rec)
3883 -- We can implicitly pack if the specified size of the record is
3884 -- less than the sum of the object sizes (no point in packing if
3885 -- this is not the case).
3887 and then RM_Size (Rec) < Scalar_Component_Total_Esize
3889 -- And the total RM size cannot be greater than the specified size
3890 -- since otherwise packing will not get us where we have to be.
3892 and then RM_Size (Rec) >= Scalar_Component_Total_RM_Size
3894 -- Never do implicit packing in CodePeer or SPARK modes since
3895 -- we don't do any packing in these modes, since this generates
3896 -- over-complex code that confuses static analysis, and in
3897 -- general, neither CodePeer not GNATprove care about the
3898 -- internal representation of objects.
3900 and then not (CodePeer_Mode or GNATprove_Mode)
3901 then
3902 -- If implicit packing enabled, do it
3904 if Implicit_Packing then
3905 Set_Is_Packed (Rec);
3907 -- Otherwise flag the size clause
3909 else
3910 declare
3911 Sz : constant Node_Id := Size_Clause (Rec);
3912 begin
3913 Error_Msg_NE -- CODEFIX
3914 ("size given for& too small", Sz, Rec);
3915 Error_Msg_N -- CODEFIX
3916 ("\use explicit pragma Pack "
3917 & "or use pragma Implicit_Packing", Sz);
3918 end;
3919 end if;
3920 end if;
3922 -- The following checks are only relevant when SPARK_Mode is on as
3923 -- they are not standard Ada legality rules.
3925 if SPARK_Mode = On then
3926 if Is_Effectively_Volatile (Rec) then
3928 -- A discriminated type cannot be effectively volatile
3929 -- (SPARK RM C.6(4)).
3931 if Has_Discriminants (Rec) then
3932 Error_Msg_N ("discriminated type & cannot be volatile", Rec);
3934 -- A tagged type cannot be effectively volatile
3935 -- (SPARK RM C.6(5)).
3937 elsif Is_Tagged_Type (Rec) then
3938 Error_Msg_N ("tagged type & cannot be volatile", Rec);
3939 end if;
3941 -- A non-effectively volatile record type cannot contain
3942 -- effectively volatile components (SPARK RM C.6(2)).
3944 else
3945 Comp := First_Component (Rec);
3946 while Present (Comp) loop
3947 if Comes_From_Source (Comp)
3948 and then Is_Effectively_Volatile (Etype (Comp))
3949 then
3950 Error_Msg_Name_1 := Chars (Rec);
3951 Error_Msg_N
3952 ("component & of non-volatile type % cannot be "
3953 & "volatile", Comp);
3954 end if;
3956 Next_Component (Comp);
3957 end loop;
3958 end if;
3959 end if;
3961 -- All done if not a full record definition
3963 if Ekind (Rec) /= E_Record_Type then
3964 return;
3965 end if;
3967 -- Finally we need to check the variant part to make sure that
3968 -- all types within choices are properly frozen as part of the
3969 -- freezing of the record type.
3971 Check_Variant_Part : declare
3972 D : constant Node_Id := Declaration_Node (Rec);
3973 T : Node_Id;
3974 C : Node_Id;
3976 begin
3977 -- Find component list
3979 C := Empty;
3981 if Nkind (D) = N_Full_Type_Declaration then
3982 T := Type_Definition (D);
3984 if Nkind (T) = N_Record_Definition then
3985 C := Component_List (T);
3987 elsif Nkind (T) = N_Derived_Type_Definition
3988 and then Present (Record_Extension_Part (T))
3989 then
3990 C := Component_List (Record_Extension_Part (T));
3991 end if;
3992 end if;
3994 -- Case of variant part present
3996 if Present (C) and then Present (Variant_Part (C)) then
3997 Freeze_Choices_In_Variant_Part (Variant_Part (C));
3998 end if;
4000 -- Note: we used to call Check_Choices here, but it is too early,
4001 -- since predicated subtypes are frozen here, but their freezing
4002 -- actions are in Analyze_Freeze_Entity, which has not been called
4003 -- yet for entities frozen within this procedure, so we moved that
4004 -- call to the Analyze_Freeze_Entity for the record type.
4006 end Check_Variant_Part;
4007 end Freeze_Record_Type;
4009 -------------------------------
4010 -- Has_Boolean_Aspect_Import --
4011 -------------------------------
4013 function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4014 Decl : constant Node_Id := Declaration_Node (E);
4015 Asp : Node_Id;
4016 Expr : Node_Id;
4018 begin
4019 if Has_Aspects (Decl) then
4020 Asp := First (Aspect_Specifications (Decl));
4021 while Present (Asp) loop
4022 Expr := Expression (Asp);
4024 -- The value of aspect Import is True when the expression is
4025 -- either missing or it is explicitly set to True.
4027 if Get_Aspect_Id (Asp) = Aspect_Import
4028 and then (No (Expr)
4029 or else (Compile_Time_Known_Value (Expr)
4030 and then Is_True (Expr_Value (Expr))))
4031 then
4032 return True;
4033 end if;
4035 Next (Asp);
4036 end loop;
4037 end if;
4039 return False;
4040 end Has_Boolean_Aspect_Import;
4042 ----------------------------
4043 -- Late_Freeze_Subprogram --
4044 ----------------------------
4046 procedure Late_Freeze_Subprogram (E : Entity_Id) is
4047 Spec : constant Node_Id :=
4048 Specification (Unit_Declaration_Node (Scope (E)));
4049 Decls : List_Id;
4051 begin
4052 if Present (Private_Declarations (Spec)) then
4053 Decls := Private_Declarations (Spec);
4054 else
4055 Decls := Visible_Declarations (Spec);
4056 end if;
4058 Append_List (Result, Decls);
4059 end Late_Freeze_Subprogram;
4061 ------------------------------
4062 -- Wrap_Imported_Subprogram --
4063 ------------------------------
4065 -- The issue here is that our normal approach of checking preconditions
4066 -- and postconditions does not work for imported procedures, since we
4067 -- are not generating code for the body. To get around this we create
4068 -- a wrapper, as shown by the following example:
4070 -- procedure K (A : Integer);
4071 -- pragma Import (C, K);
4073 -- The spec is rewritten by removing the effects of pragma Import, but
4074 -- leaving the convention unchanged, as though the source had said:
4076 -- procedure K (A : Integer);
4077 -- pragma Convention (C, K);
4079 -- and we create a body, added to the entity K freeze actions, which
4080 -- looks like:
4082 -- procedure K (A : Integer) is
4083 -- procedure K (A : Integer);
4084 -- pragma Import (C, K);
4085 -- begin
4086 -- K (A);
4087 -- end K;
4089 -- Now the contract applies in the normal way to the outer procedure,
4090 -- and the inner procedure has no contracts, so there is no problem
4091 -- in just calling it to get the original effect.
4093 -- In the case of a function, we create an appropriate return statement
4094 -- for the subprogram body that calls the inner procedure.
4096 procedure Wrap_Imported_Subprogram (E : Entity_Id) is
4097 Loc : constant Source_Ptr := Sloc (E);
4098 CE : constant Name_Id := Chars (E);
4099 Spec : Node_Id;
4100 Parms : List_Id;
4101 Stmt : Node_Id;
4102 Iprag : Node_Id;
4103 Bod : Node_Id;
4104 Forml : Entity_Id;
4106 begin
4107 -- Nothing to do if not imported
4109 if not Is_Imported (E) then
4110 return;
4112 -- Test enabling conditions for wrapping
4114 elsif Is_Subprogram (E)
4115 and then Present (Contract (E))
4116 and then Present (Pre_Post_Conditions (Contract (E)))
4117 and then not GNATprove_Mode
4118 then
4119 -- Here we do the wrap
4121 -- Note on calls to Copy_Separate_Tree. The trees we are copying
4122 -- here are fully analyzed, but we definitely want fully syntactic
4123 -- unanalyzed trees in the body we construct, so that the analysis
4124 -- generates the right visibility, and that is exactly what the
4125 -- calls to Copy_Separate_Tree give us.
4127 -- Acquire copy of Inline pragma
4129 Iprag := Copy_Separate_Tree (Import_Pragma (E));
4131 -- Fix up spec to be not imported any more
4133 Set_Is_Imported (E, False);
4134 Set_Interface_Name (E, Empty);
4135 Set_Has_Completion (E, False);
4136 Set_Import_Pragma (E, Empty);
4138 -- Grab the subprogram declaration and specification
4140 Spec := Declaration_Node (E);
4142 -- Build parameter list that we need
4144 Parms := New_List;
4145 Forml := First_Formal (E);
4146 while Present (Forml) loop
4147 Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
4148 Next_Formal (Forml);
4149 end loop;
4151 -- Build the call
4153 if Ekind_In (E, E_Function, E_Generic_Function) then
4154 Stmt :=
4155 Make_Simple_Return_Statement (Loc,
4156 Expression =>
4157 Make_Function_Call (Loc,
4158 Name => Make_Identifier (Loc, CE),
4159 Parameter_Associations => Parms));
4161 else
4162 Stmt :=
4163 Make_Procedure_Call_Statement (Loc,
4164 Name => Make_Identifier (Loc, CE),
4165 Parameter_Associations => Parms);
4166 end if;
4168 -- Now build the body
4170 Bod :=
4171 Make_Subprogram_Body (Loc,
4172 Specification =>
4173 Copy_Separate_Tree (Spec),
4174 Declarations => New_List (
4175 Make_Subprogram_Declaration (Loc,
4176 Specification =>
4177 Copy_Separate_Tree (Spec)),
4178 Iprag),
4179 Handled_Statement_Sequence =>
4180 Make_Handled_Sequence_Of_Statements (Loc,
4181 Statements => New_List (Stmt),
4182 End_Label => Make_Identifier (Loc, CE)));
4184 -- Append the body to freeze result
4186 Add_To_Result (Bod);
4187 return;
4189 -- Case of imported subprogram that does not get wrapped
4191 else
4192 -- Set Is_Public. All imported entities need an external symbol
4193 -- created for them since they are always referenced from another
4194 -- object file. Note this used to be set when we set Is_Imported
4195 -- back in Sem_Prag, but now we delay it to this point, since we
4196 -- don't want to set this flag if we wrap an imported subprogram.
4198 Set_Is_Public (E);
4199 end if;
4200 end Wrap_Imported_Subprogram;
4202 -- Start of processing for Freeze_Entity
4204 begin
4205 -- We are going to test for various reasons why this entity need not be
4206 -- frozen here, but in the case of an Itype that's defined within a
4207 -- record, that test actually applies to the record.
4209 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
4210 Test_E := Scope (E);
4211 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
4212 and then Is_Record_Type (Underlying_Type (Scope (E)))
4213 then
4214 Test_E := Underlying_Type (Scope (E));
4215 end if;
4217 -- Do not freeze if already frozen since we only need one freeze node
4219 if Is_Frozen (E) then
4220 return No_List;
4222 -- It is improper to freeze an external entity within a generic because
4223 -- its freeze node will appear in a non-valid context. The entity will
4224 -- be frozen in the proper scope after the current generic is analyzed.
4225 -- However, aspects must be analyzed because they may be queried later
4226 -- within the generic itself, and the corresponding pragma or attribute
4227 -- definition has not been analyzed yet.
4229 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
4230 if Has_Delayed_Aspects (E) then
4231 Analyze_Aspects_At_Freeze_Point (E);
4232 end if;
4234 return No_List;
4236 -- AI05-0213: A formal incomplete type does not freeze the actual. In
4237 -- the instance, the same applies to the subtype renaming the actual.
4239 elsif Is_Private_Type (E)
4240 and then Is_Generic_Actual_Type (E)
4241 and then No (Full_View (Base_Type (E)))
4242 and then Ada_Version >= Ada_2012
4243 then
4244 return No_List;
4246 -- Formal subprograms are never frozen
4248 elsif Is_Formal_Subprogram (E) then
4249 return No_List;
4251 -- Generic types are never frozen as they lack delayed semantic checks
4253 elsif Is_Generic_Type (E) then
4254 return No_List;
4256 -- Do not freeze a global entity within an inner scope created during
4257 -- expansion. A call to subprogram E within some internal procedure
4258 -- (a stream attribute for example) might require freezing E, but the
4259 -- freeze node must appear in the same declarative part as E itself.
4260 -- The two-pass elaboration mechanism in gigi guarantees that E will
4261 -- be frozen before the inner call is elaborated. We exclude constants
4262 -- from this test, because deferred constants may be frozen early, and
4263 -- must be diagnosed (e.g. in the case of a deferred constant being used
4264 -- in a default expression). If the enclosing subprogram comes from
4265 -- source, or is a generic instance, then the freeze point is the one
4266 -- mandated by the language, and we freeze the entity. A subprogram that
4267 -- is a child unit body that acts as a spec does not have a spec that
4268 -- comes from source, but can only come from source.
4270 elsif In_Open_Scopes (Scope (Test_E))
4271 and then Scope (Test_E) /= Current_Scope
4272 and then Ekind (Test_E) /= E_Constant
4273 then
4274 declare
4275 S : Entity_Id;
4277 begin
4278 S := Current_Scope;
4279 while Present (S) loop
4280 if Is_Overloadable (S) then
4281 if Comes_From_Source (S)
4282 or else Is_Generic_Instance (S)
4283 or else Is_Child_Unit (S)
4284 then
4285 exit;
4286 else
4287 return No_List;
4288 end if;
4289 end if;
4291 S := Scope (S);
4292 end loop;
4293 end;
4295 -- Similarly, an inlined instance body may make reference to global
4296 -- entities, but these references cannot be the proper freezing point
4297 -- for them, and in the absence of inlining freezing will take place in
4298 -- their own scope. Normally instance bodies are analyzed after the
4299 -- enclosing compilation, and everything has been frozen at the proper
4300 -- place, but with front-end inlining an instance body is compiled
4301 -- before the end of the enclosing scope, and as a result out-of-order
4302 -- freezing must be prevented.
4304 elsif Front_End_Inlining
4305 and then In_Instance_Body
4306 and then Present (Scope (Test_E))
4307 then
4308 declare
4309 S : Entity_Id;
4311 begin
4312 S := Scope (Test_E);
4313 while Present (S) loop
4314 if Is_Generic_Instance (S) then
4315 exit;
4316 else
4317 S := Scope (S);
4318 end if;
4319 end loop;
4321 if No (S) then
4322 return No_List;
4323 end if;
4324 end;
4326 elsif Ekind (E) = E_Generic_Package then
4327 return Freeze_Generic_Entities (E);
4328 end if;
4330 -- Add checks to detect proper initialization of scalars that may appear
4331 -- as subprogram parameters.
4333 if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
4334 Apply_Parameter_Validity_Checks (E);
4335 end if;
4337 -- Deal with delayed aspect specifications. The analysis of the aspect
4338 -- is required to be delayed to the freeze point, thus we analyze the
4339 -- pragma or attribute definition clause in the tree at this point. We
4340 -- also analyze the aspect specification node at the freeze point when
4341 -- the aspect doesn't correspond to pragma/attribute definition clause.
4343 if Has_Delayed_Aspects (E) then
4344 Analyze_Aspects_At_Freeze_Point (E);
4345 end if;
4347 -- Here to freeze the entity
4349 Set_Is_Frozen (E);
4351 -- Case of entity being frozen is other than a type
4353 if not Is_Type (E) then
4355 -- If entity is exported or imported and does not have an external
4356 -- name, now is the time to provide the appropriate default name.
4357 -- Skip this if the entity is stubbed, since we don't need a name
4358 -- for any stubbed routine. For the case on intrinsics, if no
4359 -- external name is specified, then calls will be handled in
4360 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
4361 -- external name is provided, then Expand_Intrinsic_Call leaves
4362 -- calls in place for expansion by GIGI.
4364 if (Is_Imported (E) or else Is_Exported (E))
4365 and then No (Interface_Name (E))
4366 and then Convention (E) /= Convention_Stubbed
4367 and then Convention (E) /= Convention_Intrinsic
4368 then
4369 Set_Encoded_Interface_Name
4370 (E, Get_Default_External_Name (E));
4372 -- If entity is an atomic object appearing in a declaration and
4373 -- the expression is an aggregate, assign it to a temporary to
4374 -- ensure that the actual assignment is done atomically rather
4375 -- than component-wise (the assignment to the temp may be done
4376 -- component-wise, but that is harmless).
4378 elsif Is_Atomic (E)
4379 and then Nkind (Parent (E)) = N_Object_Declaration
4380 and then Present (Expression (Parent (E)))
4381 and then Nkind (Expression (Parent (E))) = N_Aggregate
4382 and then Is_Atomic_Aggregate (Expression (Parent (E)), Etype (E))
4383 then
4384 null;
4385 end if;
4387 -- Subprogram case
4389 if Is_Subprogram (E) then
4391 -- Check for needing to wrap imported subprogram
4393 Wrap_Imported_Subprogram (E);
4395 -- Freeze all parameter types and the return type (RM 13.14(14)).
4396 -- However skip this for internal subprograms. This is also where
4397 -- any extra formal parameters are created since we now know
4398 -- whether the subprogram will use a foreign convention.
4400 -- In Ada 2012, freezing a subprogram does not always freeze
4401 -- the corresponding profile (see AI05-019). An attribute
4402 -- reference is not a freezing point of the profile.
4403 -- Other constructs that should not freeze ???
4405 -- This processing doesn't apply to internal entities (see below)
4407 if not Is_Internal (E) then
4408 if not Freeze_Profile (E) then
4409 return Result;
4410 end if;
4411 end if;
4413 -- Must freeze its parent first if it is a derived subprogram
4415 if Present (Alias (E)) then
4416 Freeze_And_Append (Alias (E), N, Result);
4417 end if;
4419 -- We don't freeze internal subprograms, because we don't normally
4420 -- want addition of extra formals or mechanism setting to happen
4421 -- for those. However we do pass through predefined dispatching
4422 -- cases, since extra formals may be needed in some cases, such as
4423 -- for the stream 'Input function (build-in-place formals).
4425 if not Is_Internal (E)
4426 or else Is_Predefined_Dispatching_Operation (E)
4427 then
4428 Freeze_Subprogram (E);
4429 end if;
4431 if Late_Freezing then
4432 Late_Freeze_Subprogram (E);
4433 return No_List;
4434 end if;
4436 -- If warning on suspicious contracts then check for the case of
4437 -- a postcondition other than False for a No_Return subprogram.
4439 if No_Return (E)
4440 and then Warn_On_Suspicious_Contract
4441 and then Present (Contract (E))
4442 then
4443 declare
4444 Prag : Node_Id := Pre_Post_Conditions (Contract (E));
4445 Exp : Node_Id;
4447 begin
4448 while Present (Prag) loop
4449 if Nam_In (Pragma_Name (Prag), Name_Post,
4450 Name_Postcondition,
4451 Name_Refined_Post)
4452 then
4453 Exp :=
4454 Expression
4455 (First (Pragma_Argument_Associations (Prag)));
4457 if Nkind (Exp) /= N_Identifier
4458 or else Chars (Exp) /= Name_False
4459 then
4460 Error_Msg_NE
4461 ("useless postcondition, & is marked "
4462 & "No_Return?T?", Exp, E);
4463 end if;
4464 end if;
4466 Prag := Next_Pragma (Prag);
4467 end loop;
4468 end;
4469 end if;
4471 -- Here for other than a subprogram or type
4473 else
4474 -- If entity has a type, and it is not a generic unit, then
4475 -- freeze it first (RM 13.14(10)).
4477 if Present (Etype (E))
4478 and then Ekind (E) /= E_Generic_Function
4479 then
4480 Freeze_And_Append (Etype (E), N, Result);
4482 -- For an object of an anonymous array type, aspects on the
4483 -- object declaration apply to the type itself. This is the
4484 -- case for Atomic_Components, Volatile_Components, and
4485 -- Independent_Components. In these cases analysis of the
4486 -- generated pragma will mark the anonymous types accordingly,
4487 -- and the object itself does not require a freeze node.
4489 if Ekind (E) = E_Variable
4490 and then Is_Itype (Etype (E))
4491 and then Is_Array_Type (Etype (E))
4492 and then Has_Delayed_Aspects (E)
4493 then
4494 Set_Has_Delayed_Aspects (E, False);
4495 Set_Has_Delayed_Freeze (E, False);
4496 Set_Freeze_Node (E, Empty);
4497 end if;
4498 end if;
4500 -- Special processing for objects created by object declaration
4502 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
4504 -- Abstract type allowed only for C++ imported variables or
4505 -- constants.
4507 -- Note: we inhibit this check for objects that do not come
4508 -- from source because there is at least one case (the
4509 -- expansion of x'Class'Input where x is abstract) where we
4510 -- legitimately generate an abstract object.
4512 if Is_Abstract_Type (Etype (E))
4513 and then Comes_From_Source (Parent (E))
4514 and then not (Is_Imported (E)
4515 and then Is_CPP_Class (Etype (E)))
4516 then
4517 Error_Msg_N ("type of object cannot be abstract",
4518 Object_Definition (Parent (E)));
4520 if Is_CPP_Class (Etype (E)) then
4521 Error_Msg_NE
4522 ("\} may need a cpp_constructor",
4523 Object_Definition (Parent (E)), Etype (E));
4525 elsif Present (Expression (Parent (E))) then
4526 Error_Msg_N -- CODEFIX
4527 ("\maybe a class-wide type was meant",
4528 Object_Definition (Parent (E)));
4529 end if;
4530 end if;
4532 -- For object created by object declaration, perform required
4533 -- categorization (preelaborate and pure) checks. Defer these
4534 -- checks to freeze time since pragma Import inhibits default
4535 -- initialization and thus pragma Import affects these checks.
4537 Validate_Object_Declaration (Declaration_Node (E));
4539 -- If there is an address clause, check that it is valid
4541 Check_Address_Clause (E);
4543 -- Reset Is_True_Constant for aliased object. We consider that
4544 -- the fact that something is aliased may indicate that some
4545 -- funny business is going on, e.g. an aliased object is passed
4546 -- by reference to a procedure which captures the address of
4547 -- the object, which is later used to assign a new value. Such
4548 -- code is highly dubious, but we choose to make it "work" for
4549 -- aliased objects.
4551 -- However, we don't do that for internal entities. We figure
4552 -- that if we deliberately set Is_True_Constant for an internal
4553 -- entity, e.g. a dispatch table entry, then we mean it.
4555 if (Is_Aliased (E) or else Is_Aliased (Etype (E)))
4556 and then not Is_Internal_Name (Chars (E))
4557 then
4558 Set_Is_True_Constant (E, False);
4559 end if;
4561 -- If the object needs any kind of default initialization, an
4562 -- error must be issued if No_Default_Initialization applies.
4563 -- The check doesn't apply to imported objects, which are not
4564 -- ever default initialized, and is why the check is deferred
4565 -- until freezing, at which point we know if Import applies.
4566 -- Deferred constants are also exempted from this test because
4567 -- their completion is explicit, or through an import pragma.
4569 if Ekind (E) = E_Constant
4570 and then Present (Full_View (E))
4571 then
4572 null;
4574 elsif Comes_From_Source (E)
4575 and then not Is_Imported (E)
4576 and then not Has_Init_Expression (Declaration_Node (E))
4577 and then
4578 ((Has_Non_Null_Base_Init_Proc (Etype (E))
4579 and then not No_Initialization (Declaration_Node (E))
4580 and then not Is_Value_Type (Etype (E))
4581 and then not Initialization_Suppressed (Etype (E)))
4582 or else
4583 (Needs_Simple_Initialization (Etype (E))
4584 and then not Is_Internal (E)))
4585 then
4586 Has_Default_Initialization := True;
4587 Check_Restriction
4588 (No_Default_Initialization, Declaration_Node (E));
4589 end if;
4591 -- Check that a Thread_Local_Storage variable does not have
4592 -- default initialization, and any explicit initialization must
4593 -- either be the null constant or a static constant.
4595 if Has_Pragma_Thread_Local_Storage (E) then
4596 declare
4597 Decl : constant Node_Id := Declaration_Node (E);
4598 begin
4599 if Has_Default_Initialization
4600 or else
4601 (Has_Init_Expression (Decl)
4602 and then
4603 (No (Expression (Decl))
4604 or else not
4605 (Is_OK_Static_Expression (Expression (Decl))
4606 or else
4607 Nkind (Expression (Decl)) = N_Null)))
4608 then
4609 Error_Msg_NE
4610 ("Thread_Local_Storage variable& is "
4611 & "improperly initialized", Decl, E);
4612 Error_Msg_NE
4613 ("\only allowed initialization is explicit "
4614 & "NULL or static expression", Decl, E);
4615 end if;
4616 end;
4617 end if;
4619 -- For imported objects, set Is_Public unless there is also an
4620 -- address clause, which means that there is no external symbol
4621 -- needed for the Import (Is_Public may still be set for other
4622 -- unrelated reasons). Note that we delayed this processing
4623 -- till freeze time so that we can be sure not to set the flag
4624 -- if there is an address clause. If there is such a clause,
4625 -- then the only purpose of the Import pragma is to suppress
4626 -- implicit initialization.
4628 if Is_Imported (E) and then No (Address_Clause (E)) then
4629 Set_Is_Public (E);
4630 end if;
4632 -- For source objects that are not Imported and are library
4633 -- level, if no linker section pragma was given inherit the
4634 -- appropriate linker section from the corresponding type.
4636 if Comes_From_Source (E)
4637 and then not Is_Imported (E)
4638 and then Is_Library_Level_Entity (E)
4639 and then No (Linker_Section_Pragma (E))
4640 then
4641 Set_Linker_Section_Pragma
4642 (E, Linker_Section_Pragma (Etype (E)));
4643 end if;
4645 -- For convention C objects of an enumeration type, warn if
4646 -- the size is not integer size and no explicit size given.
4647 -- Skip warning for Boolean, and Character, assume programmer
4648 -- expects 8-bit sizes for these cases.
4650 if (Convention (E) = Convention_C
4651 or else
4652 Convention (E) = Convention_CPP)
4653 and then Is_Enumeration_Type (Etype (E))
4654 and then not Is_Character_Type (Etype (E))
4655 and then not Is_Boolean_Type (Etype (E))
4656 and then Esize (Etype (E)) < Standard_Integer_Size
4657 and then not Has_Size_Clause (E)
4658 then
4659 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
4660 Error_Msg_N
4661 ("??convention C enumeration object has size less than ^",
4663 Error_Msg_N ("\??use explicit size clause to set size", E);
4664 end if;
4665 end if;
4667 -- Check that a constant which has a pragma Volatile[_Components]
4668 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
4670 -- Note: Atomic[_Components] also sets Volatile[_Components]
4672 if Ekind (E) = E_Constant
4673 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
4674 and then not Is_Imported (E)
4675 and then not Has_Boolean_Aspect_Import (E)
4676 then
4677 -- Make sure we actually have a pragma, and have not merely
4678 -- inherited the indication from elsewhere (e.g. an address
4679 -- clause, which is not good enough in RM terms).
4681 if Has_Rep_Pragma (E, Name_Atomic)
4682 or else
4683 Has_Rep_Pragma (E, Name_Atomic_Components)
4684 then
4685 Error_Msg_N
4686 ("stand alone atomic constant must be " &
4687 "imported (RM C.6(13))", E);
4689 elsif Has_Rep_Pragma (E, Name_Volatile)
4690 or else
4691 Has_Rep_Pragma (E, Name_Volatile_Components)
4692 then
4693 Error_Msg_N
4694 ("stand alone volatile constant must be " &
4695 "imported (RM C.6(13))", E);
4696 end if;
4697 end if;
4699 -- Static objects require special handling
4701 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
4702 and then Is_Statically_Allocated (E)
4703 then
4704 Freeze_Static_Object (E);
4705 end if;
4707 -- Remaining step is to layout objects
4709 if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
4710 or else Is_Formal (E)
4711 then
4712 Layout_Object (E);
4713 end if;
4715 -- For an object that does not have delayed freezing, and whose
4716 -- initialization actions have been captured in a compound
4717 -- statement, move them back now directly within the enclosing
4718 -- statement sequence.
4720 if Ekind_In (E, E_Constant, E_Variable)
4721 and then not Has_Delayed_Freeze (E)
4722 then
4723 Explode_Initialization_Compound_Statement (E);
4724 end if;
4725 end if;
4727 -- Case of a type or subtype being frozen
4729 else
4730 -- We used to check here that a full type must have preelaborable
4731 -- initialization if it completes a private type specified with
4732 -- pragma Preelaborable_Initialization, but that missed cases where
4733 -- the types occur within a generic package, since the freezing
4734 -- that occurs within a containing scope generally skips traversal
4735 -- of a generic unit's declarations (those will be frozen within
4736 -- instances). This check was moved to Analyze_Package_Specification.
4738 -- The type may be defined in a generic unit. This can occur when
4739 -- freezing a generic function that returns the type (which is
4740 -- defined in a parent unit). It is clearly meaningless to freeze
4741 -- this type. However, if it is a subtype, its size may be determi-
4742 -- nable and used in subsequent checks, so might as well try to
4743 -- compute it.
4745 -- In Ada 2012, Freeze_Entities is also used in the front end to
4746 -- trigger the analysis of aspect expressions, so in this case we
4747 -- want to continue the freezing process.
4749 if Present (Scope (E))
4750 and then Is_Generic_Unit (Scope (E))
4751 and then
4752 (not Has_Predicates (E)
4753 and then not Has_Delayed_Freeze (E))
4754 then
4755 Check_Compile_Time_Size (E);
4756 return No_List;
4757 end if;
4759 -- Check for error of Type_Invariant'Class applied to an untagged
4760 -- type (check delayed to freeze time when full type is available).
4762 declare
4763 Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
4764 begin
4765 if Present (Prag)
4766 and then Class_Present (Prag)
4767 and then not Is_Tagged_Type (E)
4768 then
4769 Error_Msg_NE
4770 ("Type_Invariant''Class cannot be specified for &",
4771 Prag, E);
4772 Error_Msg_N
4773 ("\can only be specified for a tagged type", Prag);
4774 end if;
4775 end;
4777 -- Deal with special cases of freezing for subtype
4779 if E /= Base_Type (E) then
4781 -- Before we do anything else, a specialized test for the case of
4782 -- a size given for an array where the array needs to be packed,
4783 -- but was not so the size cannot be honored. This is the case
4784 -- where implicit packing may apply. The reason we do this so
4785 -- early is that if we have implicit packing, the layout of the
4786 -- base type is affected, so we must do this before we freeze
4787 -- the base type.
4789 -- We could do this processing only if implicit packing is enabled
4790 -- since in all other cases, the error would be caught by the back
4791 -- end. However, we choose to do the check even if we do not have
4792 -- implicit packing enabled, since this allows us to give a more
4793 -- useful error message (advising use of pragmas Implicit_Packing
4794 -- or Pack).
4796 if Is_Array_Type (E) then
4797 declare
4798 Ctyp : constant Entity_Id := Component_Type (E);
4799 Rsiz : constant Uint := RM_Size (Ctyp);
4800 SZ : constant Node_Id := Size_Clause (E);
4801 Btyp : constant Entity_Id := Base_Type (E);
4803 Lo : Node_Id;
4804 Hi : Node_Id;
4805 Indx : Node_Id;
4807 Num_Elmts : Uint;
4808 -- Number of elements in array
4810 begin
4811 -- Check enabling conditions. These are straightforward
4812 -- except for the test for a limited composite type. This
4813 -- eliminates the rare case of a array of limited components
4814 -- where there are issues of whether or not we can go ahead
4815 -- and pack the array (since we can't freely pack and unpack
4816 -- arrays if they are limited).
4818 -- Note that we check the root type explicitly because the
4819 -- whole point is we are doing this test before we have had
4820 -- a chance to freeze the base type (and it is that freeze
4821 -- action that causes stuff to be inherited).
4823 if Has_Size_Clause (E)
4824 and then Known_Static_RM_Size (E)
4825 and then not Is_Packed (E)
4826 and then not Has_Pragma_Pack (E)
4827 and then not Has_Component_Size_Clause (E)
4828 and then Known_Static_RM_Size (Ctyp)
4829 and then RM_Size (Ctyp) < 64
4830 and then not Is_Limited_Composite (E)
4831 and then not Is_Packed (Root_Type (E))
4832 and then not Has_Component_Size_Clause (Root_Type (E))
4833 and then not (CodePeer_Mode or GNATprove_Mode)
4834 then
4835 -- Compute number of elements in array
4837 Num_Elmts := Uint_1;
4838 Indx := First_Index (E);
4839 while Present (Indx) loop
4840 Get_Index_Bounds (Indx, Lo, Hi);
4842 if not (Compile_Time_Known_Value (Lo)
4843 and then
4844 Compile_Time_Known_Value (Hi))
4845 then
4846 goto No_Implicit_Packing;
4847 end if;
4849 Num_Elmts :=
4850 Num_Elmts *
4851 UI_Max (Uint_0,
4852 Expr_Value (Hi) - Expr_Value (Lo) + 1);
4853 Next_Index (Indx);
4854 end loop;
4856 -- What we are looking for here is the situation where
4857 -- the RM_Size given would be exactly right if there was
4858 -- a pragma Pack (resulting in the component size being
4859 -- the same as the RM_Size). Furthermore, the component
4860 -- type size must be an odd size (not a multiple of
4861 -- storage unit). If the component RM size is an exact
4862 -- number of storage units that is a power of two, the
4863 -- array is not packed and has a standard representation.
4865 if RM_Size (E) = Num_Elmts * Rsiz
4866 and then Rsiz mod System_Storage_Unit /= 0
4867 then
4868 -- For implicit packing mode, just set the component
4869 -- size silently.
4871 if Implicit_Packing then
4872 Set_Component_Size (Btyp, Rsiz);
4873 Set_Is_Bit_Packed_Array (Btyp);
4874 Set_Is_Packed (Btyp);
4875 Set_Has_Non_Standard_Rep (Btyp);
4877 -- Otherwise give an error message
4879 else
4880 Error_Msg_NE
4881 ("size given for& too small", SZ, E);
4882 Error_Msg_N -- CODEFIX
4883 ("\use explicit pragma Pack "
4884 & "or use pragma Implicit_Packing", SZ);
4885 end if;
4887 elsif RM_Size (E) = Num_Elmts * Rsiz
4888 and then Implicit_Packing
4889 and then
4890 (Rsiz / System_Storage_Unit = 1
4891 or else
4892 Rsiz / System_Storage_Unit = 2
4893 or else
4894 Rsiz / System_Storage_Unit = 4)
4895 then
4896 -- Not a packed array, but indicate the desired
4897 -- component size, for the back-end.
4899 Set_Component_Size (Btyp, Rsiz);
4900 end if;
4901 end if;
4902 end;
4903 end if;
4905 <<No_Implicit_Packing>>
4907 -- If ancestor subtype present, freeze that first. Note that this
4908 -- will also get the base type frozen. Need RM reference ???
4910 Atype := Ancestor_Subtype (E);
4912 if Present (Atype) then
4913 Freeze_And_Append (Atype, N, Result);
4915 -- No ancestor subtype present
4917 else
4918 -- See if we have a nearest ancestor that has a predicate.
4919 -- That catches the case of derived type with a predicate.
4920 -- Need RM reference here ???
4922 Atype := Nearest_Ancestor (E);
4924 if Present (Atype) and then Has_Predicates (Atype) then
4925 Freeze_And_Append (Atype, N, Result);
4926 end if;
4928 -- Freeze base type before freezing the entity (RM 13.14(15))
4930 if E /= Base_Type (E) then
4931 Freeze_And_Append (Base_Type (E), N, Result);
4932 end if;
4933 end if;
4935 -- A subtype inherits all the type-related representation aspects
4936 -- from its parents (RM 13.1(8)).
4938 Inherit_Aspects_At_Freeze_Point (E);
4940 -- For a derived type, freeze its parent type first (RM 13.14(15))
4942 elsif Is_Derived_Type (E) then
4943 Freeze_And_Append (Etype (E), N, Result);
4944 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
4946 -- A derived type inherits each type-related representation aspect
4947 -- of its parent type that was directly specified before the
4948 -- declaration of the derived type (RM 13.1(15)).
4950 Inherit_Aspects_At_Freeze_Point (E);
4951 end if;
4953 -- Check for incompatible size and alignment for record type
4955 if Warn_On_Size_Alignment
4956 and then Is_Record_Type (E)
4957 and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
4959 -- If explicit Object_Size clause given assume that the programmer
4960 -- knows what he is doing, and expects the compiler behavior.
4962 and then not Has_Object_Size_Clause (E)
4964 -- Check for size not a multiple of alignment
4966 and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
4967 then
4968 declare
4969 SC : constant Node_Id := Size_Clause (E);
4970 AC : constant Node_Id := Alignment_Clause (E);
4971 Loc : Node_Id;
4972 Abits : constant Uint := Alignment (E) * System_Storage_Unit;
4974 begin
4975 if Present (SC) and then Present (AC) then
4977 -- Give a warning
4979 if Sloc (SC) > Sloc (AC) then
4980 Loc := SC;
4981 Error_Msg_NE
4982 ("??size is not a multiple of alignment for &", Loc, E);
4983 Error_Msg_Sloc := Sloc (AC);
4984 Error_Msg_Uint_1 := Alignment (E);
4985 Error_Msg_N ("\??alignment of ^ specified #", Loc);
4987 else
4988 Loc := AC;
4989 Error_Msg_NE
4990 ("??size is not a multiple of alignment for &", Loc, E);
4991 Error_Msg_Sloc := Sloc (SC);
4992 Error_Msg_Uint_1 := RM_Size (E);
4993 Error_Msg_N ("\??size of ^ specified #", Loc);
4994 end if;
4996 Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
4997 Error_Msg_N ("\??Object_Size will be increased to ^", Loc);
4998 end if;
4999 end;
5000 end if;
5002 -- Array type
5004 if Is_Array_Type (E) then
5005 Freeze_Array_Type (E);
5007 -- For a class-wide type, the corresponding specific type is
5008 -- frozen as well (RM 13.14(15))
5010 elsif Is_Class_Wide_Type (E) then
5011 Freeze_And_Append (Root_Type (E), N, Result);
5013 -- If the base type of the class-wide type is still incomplete,
5014 -- the class-wide remains unfrozen as well. This is legal when
5015 -- E is the formal of a primitive operation of some other type
5016 -- which is being frozen.
5018 if not Is_Frozen (Root_Type (E)) then
5019 Set_Is_Frozen (E, False);
5020 return Result;
5021 end if;
5023 -- The equivalent type associated with a class-wide subtype needs
5024 -- to be frozen to ensure that its layout is done.
5026 if Ekind (E) = E_Class_Wide_Subtype
5027 and then Present (Equivalent_Type (E))
5028 then
5029 Freeze_And_Append (Equivalent_Type (E), N, Result);
5030 end if;
5032 -- Generate an itype reference for a library-level class-wide type
5033 -- at the freeze point. Otherwise the first explicit reference to
5034 -- the type may appear in an inner scope which will be rejected by
5035 -- the back-end.
5037 if Is_Itype (E)
5038 and then Is_Compilation_Unit (Scope (E))
5039 then
5040 declare
5041 Ref : constant Node_Id := Make_Itype_Reference (Loc);
5043 begin
5044 Set_Itype (Ref, E);
5046 -- From a gigi point of view, a class-wide subtype derives
5047 -- from its record equivalent type. As a result, the itype
5048 -- reference must appear after the freeze node of the
5049 -- equivalent type or gigi will reject the reference.
5051 if Ekind (E) = E_Class_Wide_Subtype
5052 and then Present (Equivalent_Type (E))
5053 then
5054 Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5055 else
5056 Add_To_Result (Ref);
5057 end if;
5058 end;
5059 end if;
5061 -- For a record type or record subtype, freeze all component types
5062 -- (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5063 -- using Is_Record_Type, because we don't want to attempt the freeze
5064 -- for the case of a private type with record extension (we will do
5065 -- that later when the full type is frozen).
5067 elsif Ekind_In (E, E_Record_Type, E_Record_Subtype)
5068 and then not (Present (Scope (E))
5069 and then Is_Generic_Unit (Scope (E)))
5070 then
5071 Freeze_Record_Type (E);
5073 -- For a concurrent type, freeze corresponding record type. This does
5074 -- not correspond to any specific rule in the RM, but the record type
5075 -- is essentially part of the concurrent type. Also freeze all local
5076 -- entities. This includes record types created for entry parameter
5077 -- blocks and whatever local entities may appear in the private part.
5079 elsif Is_Concurrent_Type (E) then
5080 if Present (Corresponding_Record_Type (E)) then
5081 Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5082 end if;
5084 Comp := First_Entity (E);
5085 while Present (Comp) loop
5086 if Is_Type (Comp) then
5087 Freeze_And_Append (Comp, N, Result);
5089 elsif (Ekind (Comp)) /= E_Function then
5091 -- The guard on the presence of the Etype seems to be needed
5092 -- for some CodePeer (-gnatcC) cases, but not clear why???
5094 if Present (Etype (Comp)) then
5095 if Is_Itype (Etype (Comp))
5096 and then Underlying_Type (Scope (Etype (Comp))) = E
5097 then
5098 Undelay_Type (Etype (Comp));
5099 end if;
5101 Freeze_And_Append (Etype (Comp), N, Result);
5102 end if;
5103 end if;
5105 Next_Entity (Comp);
5106 end loop;
5108 -- Private types are required to point to the same freeze node as
5109 -- their corresponding full views. The freeze node itself has to
5110 -- point to the partial view of the entity (because from the partial
5111 -- view, we can retrieve the full view, but not the reverse).
5112 -- However, in order to freeze correctly, we need to freeze the full
5113 -- view. If we are freezing at the end of a scope (or within the
5114 -- scope) of the private type, the partial and full views will have
5115 -- been swapped, the full view appears first in the entity chain and
5116 -- the swapping mechanism ensures that the pointers are properly set
5117 -- (on scope exit).
5119 -- If we encounter the partial view before the full view (e.g. when
5120 -- freezing from another scope), we freeze the full view, and then
5121 -- set the pointers appropriately since we cannot rely on swapping to
5122 -- fix things up (subtypes in an outer scope might not get swapped).
5124 -- If the full view is itself private, the above requirements apply
5125 -- to the underlying full view instead of the full view. But there is
5126 -- no swapping mechanism for the underlying full view so we need to
5127 -- set the pointers appropriately in both cases.
5129 elsif Is_Incomplete_Or_Private_Type (E)
5130 and then not Is_Generic_Type (E)
5131 then
5132 -- The construction of the dispatch table associated with library
5133 -- level tagged types forces freezing of all the primitives of the
5134 -- type, which may cause premature freezing of the partial view.
5135 -- For example:
5137 -- package Pkg is
5138 -- type T is tagged private;
5139 -- type DT is new T with private;
5140 -- procedure Prim (X : in out T; Y : in out DT'Class);
5141 -- private
5142 -- type T is tagged null record;
5143 -- Obj : T;
5144 -- type DT is new T with null record;
5145 -- end;
5147 -- In this case the type will be frozen later by the usual
5148 -- mechanism: an object declaration, an instantiation, or the
5149 -- end of a declarative part.
5151 if Is_Library_Level_Tagged_Type (E)
5152 and then not Present (Full_View (E))
5153 then
5154 Set_Is_Frozen (E, False);
5155 return Result;
5157 -- Case of full view present
5159 elsif Present (Full_View (E)) then
5161 -- If full view has already been frozen, then no further
5162 -- processing is required
5164 if Is_Frozen (Full_View (E)) then
5165 Set_Has_Delayed_Freeze (E, False);
5166 Set_Freeze_Node (E, Empty);
5168 -- Otherwise freeze full view and patch the pointers so that
5169 -- the freeze node will elaborate both views in the back end.
5170 -- However, if full view is itself private, freeze underlying
5171 -- full view instead and patch the pointers so that the freeze
5172 -- node will elaborate the three views in the back end.
5174 else
5175 declare
5176 Full : Entity_Id := Full_View (E);
5178 begin
5179 if Is_Private_Type (Full)
5180 and then Present (Underlying_Full_View (Full))
5181 then
5182 Full := Underlying_Full_View (Full);
5183 end if;
5185 Freeze_And_Append (Full, N, Result);
5187 if Full /= Full_View (E)
5188 and then Has_Delayed_Freeze (Full_View (E))
5189 then
5190 F_Node := Freeze_Node (Full);
5192 if Present (F_Node) then
5193 Set_Freeze_Node (Full_View (E), F_Node);
5194 Set_Entity (F_Node, Full_View (E));
5196 else
5197 Set_Has_Delayed_Freeze (Full_View (E), False);
5198 Set_Freeze_Node (Full_View (E), Empty);
5199 end if;
5200 end if;
5202 if Has_Delayed_Freeze (E) then
5203 F_Node := Freeze_Node (Full_View (E));
5205 if Present (F_Node) then
5206 Set_Freeze_Node (E, F_Node);
5207 Set_Entity (F_Node, E);
5209 else
5210 -- {Incomplete,Private}_Subtypes with Full_Views
5211 -- constrained by discriminants.
5213 Set_Has_Delayed_Freeze (E, False);
5214 Set_Freeze_Node (E, Empty);
5215 end if;
5216 end if;
5217 end;
5218 end if;
5220 Check_Debug_Info_Needed (E);
5222 -- AI-117 requires that the convention of a partial view be the
5223 -- same as the convention of the full view. Note that this is a
5224 -- recognized breach of privacy, but it's essential for logical
5225 -- consistency of representation, and the lack of a rule in
5226 -- RM95 was an oversight.
5228 Set_Convention (E, Convention (Full_View (E)));
5230 Set_Size_Known_At_Compile_Time (E,
5231 Size_Known_At_Compile_Time (Full_View (E)));
5233 -- Size information is copied from the full view to the
5234 -- incomplete or private view for consistency.
5236 -- We skip this is the full view is not a type. This is very
5237 -- strange of course, and can only happen as a result of
5238 -- certain illegalities, such as a premature attempt to derive
5239 -- from an incomplete type.
5241 if Is_Type (Full_View (E)) then
5242 Set_Size_Info (E, Full_View (E));
5243 Set_RM_Size (E, RM_Size (Full_View (E)));
5244 end if;
5246 return Result;
5248 -- Case of underlying full view present
5250 elsif Is_Private_Type (E)
5251 and then Present (Underlying_Full_View (E))
5252 then
5253 if not Is_Frozen (Underlying_Full_View (E)) then
5254 Freeze_And_Append (Underlying_Full_View (E), N, Result);
5255 end if;
5257 -- Patch the pointers so that the freeze node will elaborate
5258 -- both views in the back end.
5260 if Has_Delayed_Freeze (E) then
5261 F_Node := Freeze_Node (Underlying_Full_View (E));
5263 if Present (F_Node) then
5264 Set_Freeze_Node (E, F_Node);
5265 Set_Entity (F_Node, E);
5267 else
5268 Set_Has_Delayed_Freeze (E, False);
5269 Set_Freeze_Node (E, Empty);
5270 end if;
5271 end if;
5273 Check_Debug_Info_Needed (E);
5275 return Result;
5277 -- Case of no full view present. If entity is derived or subtype,
5278 -- it is safe to freeze, correctness depends on the frozen status
5279 -- of parent. Otherwise it is either premature usage, or a Taft
5280 -- amendment type, so diagnosis is at the point of use and the
5281 -- type might be frozen later.
5283 elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
5284 null;
5286 else
5287 Set_Is_Frozen (E, False);
5288 return No_List;
5289 end if;
5291 -- For access subprogram, freeze types of all formals, the return
5292 -- type was already frozen, since it is the Etype of the function.
5293 -- Formal types can be tagged Taft amendment types, but otherwise
5294 -- they cannot be incomplete.
5296 elsif Ekind (E) = E_Subprogram_Type then
5297 Formal := First_Formal (E);
5298 while Present (Formal) loop
5299 if Ekind (Etype (Formal)) = E_Incomplete_Type
5300 and then No (Full_View (Etype (Formal)))
5301 and then not Is_Value_Type (Etype (Formal))
5302 then
5303 if Is_Tagged_Type (Etype (Formal)) then
5304 null;
5306 -- AI05-151: Incomplete types are allowed in access to
5307 -- subprogram specifications.
5309 elsif Ada_Version < Ada_2012 then
5310 Error_Msg_NE
5311 ("invalid use of incomplete type&", E, Etype (Formal));
5312 end if;
5313 end if;
5315 Freeze_And_Append (Etype (Formal), N, Result);
5316 Next_Formal (Formal);
5317 end loop;
5319 Freeze_Subprogram (E);
5321 -- For access to a protected subprogram, freeze the equivalent type
5322 -- (however this is not set if we are not generating code or if this
5323 -- is an anonymous type used just for resolution).
5325 elsif Is_Access_Protected_Subprogram_Type (E) then
5326 if Present (Equivalent_Type (E)) then
5327 Freeze_And_Append (Equivalent_Type (E), N, Result);
5328 end if;
5329 end if;
5331 -- Generic types are never seen by the back-end, and are also not
5332 -- processed by the expander (since the expander is turned off for
5333 -- generic processing), so we never need freeze nodes for them.
5335 if Is_Generic_Type (E) then
5336 return Result;
5337 end if;
5339 -- Some special processing for non-generic types to complete
5340 -- representation details not known till the freeze point.
5342 if Is_Fixed_Point_Type (E) then
5343 Freeze_Fixed_Point_Type (E);
5345 -- Some error checks required for ordinary fixed-point type. Defer
5346 -- these till the freeze-point since we need the small and range
5347 -- values. We only do these checks for base types
5349 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
5350 if Small_Value (E) < Ureal_2_M_80 then
5351 Error_Msg_Name_1 := Name_Small;
5352 Error_Msg_N
5353 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
5355 elsif Small_Value (E) > Ureal_2_80 then
5356 Error_Msg_Name_1 := Name_Small;
5357 Error_Msg_N
5358 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
5359 end if;
5361 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
5362 Error_Msg_Name_1 := Name_First;
5363 Error_Msg_N
5364 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
5365 end if;
5367 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
5368 Error_Msg_Name_1 := Name_Last;
5369 Error_Msg_N
5370 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
5371 end if;
5372 end if;
5374 elsif Is_Enumeration_Type (E) then
5375 Freeze_Enumeration_Type (E);
5377 elsif Is_Integer_Type (E) then
5378 Adjust_Esize_For_Alignment (E);
5380 if Is_Modular_Integer_Type (E)
5381 and then Warn_On_Suspicious_Modulus_Value
5382 then
5383 Check_Suspicious_Modulus (E);
5384 end if;
5386 elsif Is_Access_Type (E)
5387 and then not Is_Access_Subprogram_Type (E)
5388 then
5389 -- If a pragma Default_Storage_Pool applies, and this type has no
5390 -- Storage_Pool or Storage_Size clause (which must have occurred
5391 -- before the freezing point), then use the default. This applies
5392 -- only to base types.
5394 -- None of this applies to access to subprograms, for which there
5395 -- are clearly no pools.
5397 if Present (Default_Pool)
5398 and then Is_Base_Type (E)
5399 and then not Has_Storage_Size_Clause (E)
5400 and then No (Associated_Storage_Pool (E))
5401 then
5402 -- Case of pragma Default_Storage_Pool (null)
5404 if Nkind (Default_Pool) = N_Null then
5405 Set_No_Pool_Assigned (E);
5407 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
5409 else
5410 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
5411 end if;
5412 end if;
5414 -- Check restriction for standard storage pool
5416 if No (Associated_Storage_Pool (E)) then
5417 Check_Restriction (No_Standard_Storage_Pools, E);
5418 end if;
5420 -- Deal with error message for pure access type. This is not an
5421 -- error in Ada 2005 if there is no pool (see AI-366).
5423 if Is_Pure_Unit_Access_Type (E)
5424 and then (Ada_Version < Ada_2005
5425 or else not No_Pool_Assigned (E))
5426 and then not Is_Generic_Unit (Scope (E))
5427 then
5428 Error_Msg_N ("named access type not allowed in pure unit", E);
5430 if Ada_Version >= Ada_2005 then
5431 Error_Msg_N
5432 ("\would be legal if Storage_Size of 0 given??", E);
5434 elsif No_Pool_Assigned (E) then
5435 Error_Msg_N
5436 ("\would be legal in Ada 2005??", E);
5438 else
5439 Error_Msg_N
5440 ("\would be legal in Ada 2005 if "
5441 & "Storage_Size of 0 given??", E);
5442 end if;
5443 end if;
5444 end if;
5446 -- Case of composite types
5448 if Is_Composite_Type (E) then
5450 -- AI-117 requires that all new primitives of a tagged type must
5451 -- inherit the convention of the full view of the type. Inherited
5452 -- and overriding operations are defined to inherit the convention
5453 -- of their parent or overridden subprogram (also specified in
5454 -- AI-117), which will have occurred earlier (in Derive_Subprogram
5455 -- and New_Overloaded_Entity). Here we set the convention of
5456 -- primitives that are still convention Ada, which will ensure
5457 -- that any new primitives inherit the type's convention. Class-
5458 -- wide types can have a foreign convention inherited from their
5459 -- specific type, but are excluded from this since they don't have
5460 -- any associated primitives.
5462 if Is_Tagged_Type (E)
5463 and then not Is_Class_Wide_Type (E)
5464 and then Convention (E) /= Convention_Ada
5465 then
5466 declare
5467 Prim_List : constant Elist_Id := Primitive_Operations (E);
5468 Prim : Elmt_Id;
5470 begin
5471 Prim := First_Elmt (Prim_List);
5472 while Present (Prim) loop
5473 if Convention (Node (Prim)) = Convention_Ada then
5474 Set_Convention (Node (Prim), Convention (E));
5475 end if;
5477 Next_Elmt (Prim);
5478 end loop;
5479 end;
5480 end if;
5482 -- If the type is a simple storage pool type, then this is where
5483 -- we attempt to locate and validate its Allocate, Deallocate, and
5484 -- Storage_Size operations (the first is required, and the latter
5485 -- two are optional). We also verify that the full type for a
5486 -- private type is allowed to be a simple storage pool type.
5488 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
5489 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
5490 then
5491 -- If the type is marked Has_Private_Declaration, then this is
5492 -- a full type for a private type that was specified with the
5493 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
5494 -- pragma is allowed for the full type (for example, it can't
5495 -- be an array type, or a nonlimited record type).
5497 if Has_Private_Declaration (E) then
5498 if (not Is_Record_Type (E) or else not Is_Limited_View (E))
5499 and then not Is_Private_Type (E)
5500 then
5501 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
5502 Error_Msg_N
5503 ("pragma% can only apply to full type that is an " &
5504 "explicitly limited type", E);
5505 end if;
5506 end if;
5508 Validate_Simple_Pool_Ops : declare
5509 Pool_Type : Entity_Id renames E;
5510 Address_Type : constant Entity_Id := RTE (RE_Address);
5511 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
5513 procedure Validate_Simple_Pool_Op_Formal
5514 (Pool_Op : Entity_Id;
5515 Pool_Op_Formal : in out Entity_Id;
5516 Expected_Mode : Formal_Kind;
5517 Expected_Type : Entity_Id;
5518 Formal_Name : String;
5519 OK_Formal : in out Boolean);
5520 -- Validate one formal Pool_Op_Formal of the candidate pool
5521 -- operation Pool_Op. The formal must be of Expected_Type
5522 -- and have mode Expected_Mode. OK_Formal will be set to
5523 -- False if the formal doesn't match. If OK_Formal is False
5524 -- on entry, then the formal will effectively be ignored
5525 -- (because validation of the pool op has already failed).
5526 -- Upon return, Pool_Op_Formal will be updated to the next
5527 -- formal, if any.
5529 procedure Validate_Simple_Pool_Operation
5530 (Op_Name : Name_Id);
5531 -- Search for and validate a simple pool operation with the
5532 -- name Op_Name. If the name is Allocate, then there must be
5533 -- exactly one such primitive operation for the simple pool
5534 -- type. If the name is Deallocate or Storage_Size, then
5535 -- there can be at most one such primitive operation. The
5536 -- profile of the located primitive must conform to what
5537 -- is expected for each operation.
5539 ------------------------------------
5540 -- Validate_Simple_Pool_Op_Formal --
5541 ------------------------------------
5543 procedure Validate_Simple_Pool_Op_Formal
5544 (Pool_Op : Entity_Id;
5545 Pool_Op_Formal : in out Entity_Id;
5546 Expected_Mode : Formal_Kind;
5547 Expected_Type : Entity_Id;
5548 Formal_Name : String;
5549 OK_Formal : in out Boolean)
5551 begin
5552 -- If OK_Formal is False on entry, then simply ignore
5553 -- the formal, because an earlier formal has already
5554 -- been flagged.
5556 if not OK_Formal then
5557 return;
5559 -- If no formal is passed in, then issue an error for a
5560 -- missing formal.
5562 elsif not Present (Pool_Op_Formal) then
5563 Error_Msg_NE
5564 ("simple storage pool op missing formal " &
5565 Formal_Name & " of type&", Pool_Op, Expected_Type);
5566 OK_Formal := False;
5568 return;
5569 end if;
5571 if Etype (Pool_Op_Formal) /= Expected_Type then
5573 -- If the pool type was expected for this formal, then
5574 -- this will not be considered a candidate operation
5575 -- for the simple pool, so we unset OK_Formal so that
5576 -- the op and any later formals will be ignored.
5578 if Expected_Type = Pool_Type then
5579 OK_Formal := False;
5581 return;
5583 else
5584 Error_Msg_NE
5585 ("wrong type for formal " & Formal_Name &
5586 " of simple storage pool op; expected type&",
5587 Pool_Op_Formal, Expected_Type);
5588 end if;
5589 end if;
5591 -- Issue error if formal's mode is not the expected one
5593 if Ekind (Pool_Op_Formal) /= Expected_Mode then
5594 Error_Msg_N
5595 ("wrong mode for formal of simple storage pool op",
5596 Pool_Op_Formal);
5597 end if;
5599 -- Advance to the next formal
5601 Next_Formal (Pool_Op_Formal);
5602 end Validate_Simple_Pool_Op_Formal;
5604 ------------------------------------
5605 -- Validate_Simple_Pool_Operation --
5606 ------------------------------------
5608 procedure Validate_Simple_Pool_Operation
5609 (Op_Name : Name_Id)
5611 Op : Entity_Id;
5612 Found_Op : Entity_Id := Empty;
5613 Formal : Entity_Id;
5614 Is_OK : Boolean;
5616 begin
5617 pragma Assert
5618 (Nam_In (Op_Name, Name_Allocate,
5619 Name_Deallocate,
5620 Name_Storage_Size));
5622 Error_Msg_Name_1 := Op_Name;
5624 -- For each homonym declared immediately in the scope
5625 -- of the simple storage pool type, determine whether
5626 -- the homonym is an operation of the pool type, and,
5627 -- if so, check that its profile is as expected for
5628 -- a simple pool operation of that name.
5630 Op := Get_Name_Entity_Id (Op_Name);
5631 while Present (Op) loop
5632 if Ekind_In (Op, E_Function, E_Procedure)
5633 and then Scope (Op) = Current_Scope
5634 then
5635 Formal := First_Entity (Op);
5637 Is_OK := True;
5639 -- The first parameter must be of the pool type
5640 -- in order for the operation to qualify.
5642 if Op_Name = Name_Storage_Size then
5643 Validate_Simple_Pool_Op_Formal
5644 (Op, Formal, E_In_Parameter, Pool_Type,
5645 "Pool", Is_OK);
5646 else
5647 Validate_Simple_Pool_Op_Formal
5648 (Op, Formal, E_In_Out_Parameter, Pool_Type,
5649 "Pool", Is_OK);
5650 end if;
5652 -- If another operation with this name has already
5653 -- been located for the type, then flag an error,
5654 -- since we only allow the type to have a single
5655 -- such primitive.
5657 if Present (Found_Op) and then Is_OK then
5658 Error_Msg_NE
5659 ("only one % operation allowed for " &
5660 "simple storage pool type&", Op, Pool_Type);
5661 end if;
5663 -- In the case of Allocate and Deallocate, a formal
5664 -- of type System.Address is required.
5666 if Op_Name = Name_Allocate then
5667 Validate_Simple_Pool_Op_Formal
5668 (Op, Formal, E_Out_Parameter,
5669 Address_Type, "Storage_Address", Is_OK);
5671 elsif Op_Name = Name_Deallocate then
5672 Validate_Simple_Pool_Op_Formal
5673 (Op, Formal, E_In_Parameter,
5674 Address_Type, "Storage_Address", Is_OK);
5675 end if;
5677 -- In the case of Allocate and Deallocate, formals
5678 -- of type Storage_Count are required as the third
5679 -- and fourth parameters.
5681 if Op_Name /= Name_Storage_Size then
5682 Validate_Simple_Pool_Op_Formal
5683 (Op, Formal, E_In_Parameter,
5684 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
5685 Validate_Simple_Pool_Op_Formal
5686 (Op, Formal, E_In_Parameter,
5687 Stg_Cnt_Type, "Alignment", Is_OK);
5688 end if;
5690 -- If no mismatched formals have been found (Is_OK)
5691 -- and no excess formals are present, then this
5692 -- operation has been validated, so record it.
5694 if not Present (Formal) and then Is_OK then
5695 Found_Op := Op;
5696 end if;
5697 end if;
5699 Op := Homonym (Op);
5700 end loop;
5702 -- There must be a valid Allocate operation for the type,
5703 -- so issue an error if none was found.
5705 if Op_Name = Name_Allocate
5706 and then not Present (Found_Op)
5707 then
5708 Error_Msg_N ("missing % operation for simple " &
5709 "storage pool type", Pool_Type);
5711 elsif Present (Found_Op) then
5713 -- Simple pool operations can't be abstract
5715 if Is_Abstract_Subprogram (Found_Op) then
5716 Error_Msg_N
5717 ("simple storage pool operation must not be " &
5718 "abstract", Found_Op);
5719 end if;
5721 -- The Storage_Size operation must be a function with
5722 -- Storage_Count as its result type.
5724 if Op_Name = Name_Storage_Size then
5725 if Ekind (Found_Op) = E_Procedure then
5726 Error_Msg_N
5727 ("% operation must be a function", Found_Op);
5729 elsif Etype (Found_Op) /= Stg_Cnt_Type then
5730 Error_Msg_NE
5731 ("wrong result type for%, expected type&",
5732 Found_Op, Stg_Cnt_Type);
5733 end if;
5735 -- Allocate and Deallocate must be procedures
5737 elsif Ekind (Found_Op) = E_Function then
5738 Error_Msg_N
5739 ("% operation must be a procedure", Found_Op);
5740 end if;
5741 end if;
5742 end Validate_Simple_Pool_Operation;
5744 -- Start of processing for Validate_Simple_Pool_Ops
5746 begin
5747 Validate_Simple_Pool_Operation (Name_Allocate);
5748 Validate_Simple_Pool_Operation (Name_Deallocate);
5749 Validate_Simple_Pool_Operation (Name_Storage_Size);
5750 end Validate_Simple_Pool_Ops;
5751 end if;
5752 end if;
5754 -- Now that all types from which E may depend are frozen, see if the
5755 -- size is known at compile time, if it must be unsigned, or if
5756 -- strict alignment is required
5758 Check_Compile_Time_Size (E);
5759 Check_Unsigned_Type (E);
5761 if Base_Type (E) = E then
5762 Check_Strict_Alignment (E);
5763 end if;
5765 -- Do not allow a size clause for a type which does not have a size
5766 -- that is known at compile time
5768 if Has_Size_Clause (E)
5769 and then not Size_Known_At_Compile_Time (E)
5770 then
5771 -- Suppress this message if errors posted on E, even if we are
5772 -- in all errors mode, since this is often a junk message
5774 if not Error_Posted (E) then
5775 Error_Msg_N
5776 ("size clause not allowed for variable length type",
5777 Size_Clause (E));
5778 end if;
5779 end if;
5781 -- Now we set/verify the representation information, in particular
5782 -- the size and alignment values. This processing is not required for
5783 -- generic types, since generic types do not play any part in code
5784 -- generation, and so the size and alignment values for such types
5785 -- are irrelevant. Ditto for types declared within a generic unit,
5786 -- which may have components that depend on generic parameters, and
5787 -- that will be recreated in an instance.
5789 if Inside_A_Generic then
5790 null;
5792 -- Otherwise we call the layout procedure
5794 else
5795 Layout_Type (E);
5796 end if;
5798 -- If this is an access to subprogram whose designated type is itself
5799 -- a subprogram type, the return type of this anonymous subprogram
5800 -- type must be decorated as well.
5802 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
5803 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
5804 then
5805 Layout_Type (Etype (Designated_Type (E)));
5806 end if;
5808 -- If the type has a Defaut_Value/Default_Component_Value aspect,
5809 -- this is where we analye the expression (after the type is frozen,
5810 -- since in the case of Default_Value, we are analyzing with the
5811 -- type itself, and we treat Default_Component_Value similarly for
5812 -- the sake of uniformity).
5814 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
5815 declare
5816 Nam : Name_Id;
5817 Exp : Node_Id;
5818 Typ : Entity_Id;
5820 begin
5821 if Is_Scalar_Type (E) then
5822 Nam := Name_Default_Value;
5823 Typ := E;
5824 Exp := Default_Aspect_Value (Typ);
5825 else
5826 Nam := Name_Default_Component_Value;
5827 Typ := Component_Type (E);
5828 Exp := Default_Aspect_Component_Value (E);
5829 end if;
5831 Analyze_And_Resolve (Exp, Typ);
5833 if Etype (Exp) /= Any_Type then
5834 if not Is_OK_Static_Expression (Exp) then
5835 Error_Msg_Name_1 := Nam;
5836 Flag_Non_Static_Expr
5837 ("aspect% requires static expression", Exp);
5838 end if;
5839 end if;
5840 end;
5841 end if;
5843 -- End of freeze processing for type entities
5844 end if;
5846 -- Here is where we logically freeze the current entity. If it has a
5847 -- freeze node, then this is the point at which the freeze node is
5848 -- linked into the result list.
5850 if Has_Delayed_Freeze (E) then
5852 -- If a freeze node is already allocated, use it, otherwise allocate
5853 -- a new one. The preallocation happens in the case of anonymous base
5854 -- types, where we preallocate so that we can set First_Subtype_Link.
5855 -- Note that we reset the Sloc to the current freeze location.
5857 if Present (Freeze_Node (E)) then
5858 F_Node := Freeze_Node (E);
5859 Set_Sloc (F_Node, Loc);
5861 else
5862 F_Node := New_Node (N_Freeze_Entity, Loc);
5863 Set_Freeze_Node (E, F_Node);
5864 Set_Access_Types_To_Process (F_Node, No_Elist);
5865 Set_TSS_Elist (F_Node, No_Elist);
5866 Set_Actions (F_Node, No_List);
5867 end if;
5869 Set_Entity (F_Node, E);
5870 Add_To_Result (F_Node);
5872 -- A final pass over record types with discriminants. If the type
5873 -- has an incomplete declaration, there may be constrained access
5874 -- subtypes declared elsewhere, which do not depend on the discrimi-
5875 -- nants of the type, and which are used as component types (i.e.
5876 -- the full view is a recursive type). The designated types of these
5877 -- subtypes can only be elaborated after the type itself, and they
5878 -- need an itype reference.
5880 if Ekind (E) = E_Record_Type
5881 and then Has_Discriminants (E)
5882 then
5883 declare
5884 Comp : Entity_Id;
5885 IR : Node_Id;
5886 Typ : Entity_Id;
5888 begin
5889 Comp := First_Component (E);
5890 while Present (Comp) loop
5891 Typ := Etype (Comp);
5893 if Ekind (Comp) = E_Component
5894 and then Is_Access_Type (Typ)
5895 and then Scope (Typ) /= E
5896 and then Base_Type (Designated_Type (Typ)) = E
5897 and then Is_Itype (Designated_Type (Typ))
5898 then
5899 IR := Make_Itype_Reference (Sloc (Comp));
5900 Set_Itype (IR, Designated_Type (Typ));
5901 Append (IR, Result);
5902 end if;
5904 Next_Component (Comp);
5905 end loop;
5906 end;
5907 end if;
5908 end if;
5910 -- When a type is frozen, the first subtype of the type is frozen as
5911 -- well (RM 13.14(15)). This has to be done after freezing the type,
5912 -- since obviously the first subtype depends on its own base type.
5914 if Is_Type (E) then
5915 Freeze_And_Append (First_Subtype (E), N, Result);
5917 -- If we just froze a tagged non-class wide record, then freeze the
5918 -- corresponding class-wide type. This must be done after the tagged
5919 -- type itself is frozen, because the class-wide type refers to the
5920 -- tagged type which generates the class.
5922 if Is_Tagged_Type (E)
5923 and then not Is_Class_Wide_Type (E)
5924 and then Present (Class_Wide_Type (E))
5925 then
5926 Freeze_And_Append (Class_Wide_Type (E), N, Result);
5927 end if;
5928 end if;
5930 Check_Debug_Info_Needed (E);
5932 -- Special handling for subprograms
5934 if Is_Subprogram (E) then
5936 -- If subprogram has address clause then reset Is_Public flag, since
5937 -- we do not want the backend to generate external references.
5939 if Present (Address_Clause (E))
5940 and then not Is_Library_Level_Entity (E)
5941 then
5942 Set_Is_Public (E, False);
5943 end if;
5944 end if;
5946 return Result;
5947 end Freeze_Entity;
5949 -----------------------------
5950 -- Freeze_Enumeration_Type --
5951 -----------------------------
5953 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
5954 begin
5955 -- By default, if no size clause is present, an enumeration type with
5956 -- Convention C is assumed to interface to a C enum, and has integer
5957 -- size. This applies to types. For subtypes, verify that its base
5958 -- type has no size clause either. Treat other foreign conventions
5959 -- in the same way, and also make sure alignment is set right.
5961 if Has_Foreign_Convention (Typ)
5962 and then not Has_Size_Clause (Typ)
5963 and then not Has_Size_Clause (Base_Type (Typ))
5964 and then Esize (Typ) < Standard_Integer_Size
5966 -- Don't do this if Short_Enums on target
5968 and then not Target_Short_Enums
5969 then
5970 Init_Esize (Typ, Standard_Integer_Size);
5971 Set_Alignment (Typ, Alignment (Standard_Integer));
5973 -- Normal Ada case or size clause present or not Long_C_Enums on target
5975 else
5976 -- If the enumeration type interfaces to C, and it has a size clause
5977 -- that specifies less than int size, it warrants a warning. The
5978 -- user may intend the C type to be an enum or a char, so this is
5979 -- not by itself an error that the Ada compiler can detect, but it
5980 -- it is a worth a heads-up. For Boolean and Character types we
5981 -- assume that the programmer has the proper C type in mind.
5983 if Convention (Typ) = Convention_C
5984 and then Has_Size_Clause (Typ)
5985 and then Esize (Typ) /= Esize (Standard_Integer)
5986 and then not Is_Boolean_Type (Typ)
5987 and then not Is_Character_Type (Typ)
5989 -- Don't do this if Short_Enums on target
5991 and then not Target_Short_Enums
5992 then
5993 Error_Msg_N
5994 ("C enum types have the size of a C int??", Size_Clause (Typ));
5995 end if;
5997 Adjust_Esize_For_Alignment (Typ);
5998 end if;
5999 end Freeze_Enumeration_Type;
6001 -----------------------
6002 -- Freeze_Expression --
6003 -----------------------
6005 procedure Freeze_Expression (N : Node_Id) is
6006 In_Spec_Exp : constant Boolean := In_Spec_Expression;
6007 Typ : Entity_Id;
6008 Nam : Entity_Id;
6009 Desig_Typ : Entity_Id;
6010 P : Node_Id;
6011 Parent_P : Node_Id;
6013 Freeze_Outside : Boolean := False;
6014 -- This flag is set true if the entity must be frozen outside the
6015 -- current subprogram. This happens in the case of expander generated
6016 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6017 -- not freeze all entities like other bodies, but which nevertheless
6018 -- may reference entities that have to be frozen before the body and
6019 -- obviously cannot be frozen inside the body.
6021 function Find_Aggregate_Component_Desig_Type return Entity_Id;
6022 -- If the expression is an array aggregate, the type of the component
6023 -- expressions is also frozen. If the component type is an access type
6024 -- and the expressions include allocators, the designed type is frozen
6025 -- as well.
6027 function In_Expanded_Body (N : Node_Id) return Boolean;
6028 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
6029 -- it is the handled statement sequence of an expander-generated
6030 -- subprogram (init proc, stream subprogram, or renaming as body).
6031 -- If so, this is not a freezing context.
6033 -----------------------------------------
6034 -- Find_Aggregate_Component_Desig_Type --
6035 -----------------------------------------
6037 function Find_Aggregate_Component_Desig_Type return Entity_Id is
6038 Assoc : Node_Id;
6039 Exp : Node_Id;
6041 begin
6042 if Present (Expressions (N)) then
6043 Exp := First (Expressions (N));
6044 while Present (Exp) loop
6045 if Nkind (Exp) = N_Allocator then
6046 return Designated_Type (Component_Type (Etype (N)));
6047 end if;
6049 Next (Exp);
6050 end loop;
6051 end if;
6053 if Present (Component_Associations (N)) then
6054 Assoc := First (Component_Associations (N));
6055 while Present (Assoc) loop
6056 if Nkind (Expression (Assoc)) = N_Allocator then
6057 return Designated_Type (Component_Type (Etype (N)));
6058 end if;
6060 Next (Assoc);
6061 end loop;
6062 end if;
6064 return Empty;
6065 end Find_Aggregate_Component_Desig_Type;
6067 ----------------------
6068 -- In_Expanded_Body --
6069 ----------------------
6071 function In_Expanded_Body (N : Node_Id) return Boolean is
6072 P : Node_Id;
6073 Id : Entity_Id;
6075 begin
6076 if Nkind (N) = N_Subprogram_Body then
6077 P := N;
6078 else
6079 P := Parent (N);
6080 end if;
6082 if Nkind (P) /= N_Subprogram_Body then
6083 return False;
6085 else
6086 Id := Defining_Unit_Name (Specification (P));
6088 -- The following are expander-created bodies, or bodies that
6089 -- are not freeze points.
6091 if Nkind (Id) = N_Defining_Identifier
6092 and then (Is_Init_Proc (Id)
6093 or else Is_TSS (Id, TSS_Stream_Input)
6094 or else Is_TSS (Id, TSS_Stream_Output)
6095 or else Is_TSS (Id, TSS_Stream_Read)
6096 or else Is_TSS (Id, TSS_Stream_Write)
6097 or else Nkind_In (Original_Node (P),
6098 N_Subprogram_Renaming_Declaration,
6099 N_Expression_Function))
6100 then
6101 return True;
6102 else
6103 return False;
6104 end if;
6105 end if;
6106 end In_Expanded_Body;
6108 -- Start of processing for Freeze_Expression
6110 begin
6111 -- Immediate return if freezing is inhibited. This flag is set by the
6112 -- analyzer to stop freezing on generated expressions that would cause
6113 -- freezing if they were in the source program, but which are not
6114 -- supposed to freeze, since they are created.
6116 if Must_Not_Freeze (N) then
6117 return;
6118 end if;
6120 -- If expression is non-static, then it does not freeze in a default
6121 -- expression, see section "Handling of Default Expressions" in the
6122 -- spec of package Sem for further details. Note that we have to make
6123 -- sure that we actually have a real expression (if we have a subtype
6124 -- indication, we can't test Is_OK_Static_Expression). However, we
6125 -- exclude the case of the prefix of an attribute of a static scalar
6126 -- subtype from this early return, because static subtype attributes
6127 -- should always cause freezing, even in default expressions, but
6128 -- the attribute may not have been marked as static yet (because in
6129 -- Resolve_Attribute, the call to Eval_Attribute follows the call of
6130 -- Freeze_Expression on the prefix).
6132 if In_Spec_Exp
6133 and then Nkind (N) in N_Subexpr
6134 and then not Is_OK_Static_Expression (N)
6135 and then (Nkind (Parent (N)) /= N_Attribute_Reference
6136 or else not (Is_Entity_Name (N)
6137 and then Is_Type (Entity (N))
6138 and then Is_OK_Static_Subtype (Entity (N))))
6139 then
6140 return;
6141 end if;
6143 -- Freeze type of expression if not frozen already
6145 Typ := Empty;
6147 if Nkind (N) in N_Has_Etype then
6148 if not Is_Frozen (Etype (N)) then
6149 Typ := Etype (N);
6151 -- Base type may be an derived numeric type that is frozen at
6152 -- the point of declaration, but first_subtype is still unfrozen.
6154 elsif not Is_Frozen (First_Subtype (Etype (N))) then
6155 Typ := First_Subtype (Etype (N));
6156 end if;
6157 end if;
6159 -- For entity name, freeze entity if not frozen already. A special
6160 -- exception occurs for an identifier that did not come from source.
6161 -- We don't let such identifiers freeze a non-internal entity, i.e.
6162 -- an entity that did come from source, since such an identifier was
6163 -- generated by the expander, and cannot have any semantic effect on
6164 -- the freezing semantics. For example, this stops the parameter of
6165 -- an initialization procedure from freezing the variable.
6167 if Is_Entity_Name (N)
6168 and then not Is_Frozen (Entity (N))
6169 and then (Nkind (N) /= N_Identifier
6170 or else Comes_From_Source (N)
6171 or else not Comes_From_Source (Entity (N)))
6172 then
6173 Nam := Entity (N);
6175 if Present (Nam) and then Ekind (Nam) = E_Function then
6176 Check_Expression_Function (N, Nam);
6177 end if;
6179 else
6180 Nam := Empty;
6181 end if;
6183 -- For an allocator freeze designated type if not frozen already
6185 -- For an aggregate whose component type is an access type, freeze the
6186 -- designated type now, so that its freeze does not appear within the
6187 -- loop that might be created in the expansion of the aggregate. If the
6188 -- designated type is a private type without full view, the expression
6189 -- cannot contain an allocator, so the type is not frozen.
6191 -- For a function, we freeze the entity when the subprogram declaration
6192 -- is frozen, but a function call may appear in an initialization proc.
6193 -- before the declaration is frozen. We need to generate the extra
6194 -- formals, if any, to ensure that the expansion of the call includes
6195 -- the proper actuals. This only applies to Ada subprograms, not to
6196 -- imported ones.
6198 Desig_Typ := Empty;
6200 case Nkind (N) is
6201 when N_Allocator =>
6202 Desig_Typ := Designated_Type (Etype (N));
6204 when N_Aggregate =>
6205 if Is_Array_Type (Etype (N))
6206 and then Is_Access_Type (Component_Type (Etype (N)))
6207 then
6209 -- Check whether aggregate includes allocators.
6211 Desig_Typ := Find_Aggregate_Component_Desig_Type;
6212 end if;
6214 when N_Selected_Component |
6215 N_Indexed_Component |
6216 N_Slice =>
6218 if Is_Access_Type (Etype (Prefix (N))) then
6219 Desig_Typ := Designated_Type (Etype (Prefix (N)));
6220 end if;
6222 when N_Identifier =>
6223 if Present (Nam)
6224 and then Ekind (Nam) = E_Function
6225 and then Nkind (Parent (N)) = N_Function_Call
6226 and then Convention (Nam) = Convention_Ada
6227 then
6228 Create_Extra_Formals (Nam);
6229 end if;
6231 when others =>
6232 null;
6233 end case;
6235 if Desig_Typ /= Empty
6236 and then (Is_Frozen (Desig_Typ)
6237 or else (not Is_Fully_Defined (Desig_Typ)))
6238 then
6239 Desig_Typ := Empty;
6240 end if;
6242 -- All done if nothing needs freezing
6244 if No (Typ)
6245 and then No (Nam)
6246 and then No (Desig_Typ)
6247 then
6248 return;
6249 end if;
6251 -- Examine the enclosing context by climbing the parent chain. The
6252 -- traversal serves two purposes - to detect scenarios where freezeing
6253 -- is not needed and to find the proper insertion point for the freeze
6254 -- nodes. Although somewhat similar to Insert_Actions, this traversal
6255 -- is freezing semantics-sensitive. Inserting freeze nodes blindly in
6256 -- the tree may result in types being frozen too early.
6258 P := N;
6259 loop
6260 Parent_P := Parent (P);
6262 -- If we don't have a parent, then we are not in a well-formed tree.
6263 -- This is an unusual case, but there are some legitimate situations
6264 -- in which this occurs, notably when the expressions in the range of
6265 -- a type declaration are resolved. We simply ignore the freeze
6266 -- request in this case. Is this right ???
6268 if No (Parent_P) then
6269 return;
6270 end if;
6272 -- See if we have got to an appropriate point in the tree
6274 case Nkind (Parent_P) is
6276 -- A special test for the exception of (RM 13.14(8)) for the case
6277 -- of per-object expressions (RM 3.8(18)) occurring in component
6278 -- definition or a discrete subtype definition. Note that we test
6279 -- for a component declaration which includes both cases we are
6280 -- interested in, and furthermore the tree does not have explicit
6281 -- nodes for either of these two constructs.
6283 when N_Component_Declaration =>
6285 -- The case we want to test for here is an identifier that is
6286 -- a per-object expression, this is either a discriminant that
6287 -- appears in a context other than the component declaration
6288 -- or it is a reference to the type of the enclosing construct.
6290 -- For either of these cases, we skip the freezing
6292 if not In_Spec_Expression
6293 and then Nkind (N) = N_Identifier
6294 and then (Present (Entity (N)))
6295 then
6296 -- We recognize the discriminant case by just looking for
6297 -- a reference to a discriminant. It can only be one for
6298 -- the enclosing construct. Skip freezing in this case.
6300 if Ekind (Entity (N)) = E_Discriminant then
6301 return;
6303 -- For the case of a reference to the enclosing record,
6304 -- (or task or protected type), we look for a type that
6305 -- matches the current scope.
6307 elsif Entity (N) = Current_Scope then
6308 return;
6309 end if;
6310 end if;
6312 -- If we have an enumeration literal that appears as the choice in
6313 -- the aggregate of an enumeration representation clause, then
6314 -- freezing does not occur (RM 13.14(10)).
6316 when N_Enumeration_Representation_Clause =>
6318 -- The case we are looking for is an enumeration literal
6320 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
6321 and then Is_Enumeration_Type (Etype (N))
6322 then
6323 -- If enumeration literal appears directly as the choice,
6324 -- do not freeze (this is the normal non-overloaded case)
6326 if Nkind (Parent (N)) = N_Component_Association
6327 and then First (Choices (Parent (N))) = N
6328 then
6329 return;
6331 -- If enumeration literal appears as the name of function
6332 -- which is the choice, then also do not freeze. This
6333 -- happens in the overloaded literal case, where the
6334 -- enumeration literal is temporarily changed to a function
6335 -- call for overloading analysis purposes.
6337 elsif Nkind (Parent (N)) = N_Function_Call
6338 and then
6339 Nkind (Parent (Parent (N))) = N_Component_Association
6340 and then
6341 First (Choices (Parent (Parent (N)))) = Parent (N)
6342 then
6343 return;
6344 end if;
6345 end if;
6347 -- Normally if the parent is a handled sequence of statements,
6348 -- then the current node must be a statement, and that is an
6349 -- appropriate place to insert a freeze node.
6351 when N_Handled_Sequence_Of_Statements =>
6353 -- An exception occurs when the sequence of statements is for
6354 -- an expander generated body that did not do the usual freeze
6355 -- all operation. In this case we usually want to freeze
6356 -- outside this body, not inside it, and we skip past the
6357 -- subprogram body that we are inside.
6359 if In_Expanded_Body (Parent_P) then
6360 declare
6361 Subp : constant Node_Id := Parent (Parent_P);
6362 Spec : Entity_Id;
6364 begin
6365 -- Freeze the entity only when it is declared inside the
6366 -- body of the expander generated procedure. This case
6367 -- is recognized by the scope of the entity or its type,
6368 -- which is either the spec for some enclosing body, or
6369 -- (in the case of init_procs, for which there are no
6370 -- separate specs) the current scope.
6372 if Nkind (Subp) = N_Subprogram_Body then
6373 Spec := Corresponding_Spec (Subp);
6375 if (Present (Typ) and then Scope (Typ) = Spec)
6376 or else
6377 (Present (Nam) and then Scope (Nam) = Spec)
6378 then
6379 exit;
6381 elsif Present (Typ)
6382 and then Scope (Typ) = Current_Scope
6383 and then Defining_Entity (Subp) = Current_Scope
6384 then
6385 exit;
6386 end if;
6387 end if;
6389 -- An expression function may act as a completion of
6390 -- a function declaration. As such, it can reference
6391 -- entities declared between the two views:
6393 -- Hidden []; -- 1
6394 -- function F return ...;
6395 -- private
6396 -- function Hidden return ...;
6397 -- function F return ... is (Hidden); -- 2
6399 -- Refering to the example above, freezing the expression
6400 -- of F (2) would place Hidden's freeze node (1) in the
6401 -- wrong place. Avoid explicit freezing and let the usual
6402 -- scenarios do the job - for example, reaching the end
6403 -- of the private declarations, or a call to F.
6405 if Nkind (Original_Node (Subp)) =
6406 N_Expression_Function
6407 then
6408 null;
6410 -- Freeze outside the body
6412 else
6413 Parent_P := Parent (Parent_P);
6414 Freeze_Outside := True;
6415 end if;
6416 end;
6418 -- Here if normal case where we are in handled statement
6419 -- sequence and want to do the insertion right there.
6421 else
6422 exit;
6423 end if;
6425 -- If parent is a body or a spec or a block, then the current node
6426 -- is a statement or declaration and we can insert the freeze node
6427 -- before it.
6429 when N_Block_Statement |
6430 N_Entry_Body |
6431 N_Package_Body |
6432 N_Package_Specification |
6433 N_Protected_Body |
6434 N_Subprogram_Body |
6435 N_Task_Body => exit;
6437 -- The expander is allowed to define types in any statements list,
6438 -- so any of the following parent nodes also mark a freezing point
6439 -- if the actual node is in a list of statements or declarations.
6441 when N_Abortable_Part |
6442 N_Accept_Alternative |
6443 N_And_Then |
6444 N_Case_Statement_Alternative |
6445 N_Compilation_Unit_Aux |
6446 N_Conditional_Entry_Call |
6447 N_Delay_Alternative |
6448 N_Elsif_Part |
6449 N_Entry_Call_Alternative |
6450 N_Exception_Handler |
6451 N_Extended_Return_Statement |
6452 N_Freeze_Entity |
6453 N_If_Statement |
6454 N_Or_Else |
6455 N_Selective_Accept |
6456 N_Triggering_Alternative =>
6458 exit when Is_List_Member (P);
6460 -- Freeze nodes produced by an expression coming from the Actions
6461 -- list of a N_Expression_With_Actions node must remain within the
6462 -- Actions list. Inserting the freeze nodes further up the tree
6463 -- may lead to use before declaration issues in the case of array
6464 -- types.
6466 when N_Expression_With_Actions =>
6467 if Is_List_Member (P)
6468 and then List_Containing (P) = Actions (Parent_P)
6469 then
6470 exit;
6471 end if;
6473 -- Note: N_Loop_Statement is a special case. A type that appears
6474 -- in the source can never be frozen in a loop (this occurs only
6475 -- because of a loop expanded by the expander), so we keep on
6476 -- going. Otherwise we terminate the search. Same is true of any
6477 -- entity which comes from source. (if they have predefined type,
6478 -- that type does not appear to come from source, but the entity
6479 -- should not be frozen here).
6481 when N_Loop_Statement =>
6482 exit when not Comes_From_Source (Etype (N))
6483 and then (No (Nam) or else not Comes_From_Source (Nam));
6485 -- For all other cases, keep looking at parents
6487 when others =>
6488 null;
6489 end case;
6491 -- We fall through the case if we did not yet find the proper
6492 -- place in the free for inserting the freeze node, so climb.
6494 P := Parent_P;
6495 end loop;
6497 -- If the expression appears in a record or an initialization procedure,
6498 -- the freeze nodes are collected and attached to the current scope, to
6499 -- be inserted and analyzed on exit from the scope, to insure that
6500 -- generated entities appear in the correct scope. If the expression is
6501 -- a default for a discriminant specification, the scope is still void.
6502 -- The expression can also appear in the discriminant part of a private
6503 -- or concurrent type.
6505 -- If the expression appears in a constrained subcomponent of an
6506 -- enclosing record declaration, the freeze nodes must be attached to
6507 -- the outer record type so they can eventually be placed in the
6508 -- enclosing declaration list.
6510 -- The other case requiring this special handling is if we are in a
6511 -- default expression, since in that case we are about to freeze a
6512 -- static type, and the freeze scope needs to be the outer scope, not
6513 -- the scope of the subprogram with the default parameter.
6515 -- For default expressions and other spec expressions in generic units,
6516 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
6517 -- placing them at the proper place, after the generic unit.
6519 if (In_Spec_Exp and not Inside_A_Generic)
6520 or else Freeze_Outside
6521 or else (Is_Type (Current_Scope)
6522 and then (not Is_Concurrent_Type (Current_Scope)
6523 or else not Has_Completion (Current_Scope)))
6524 or else Ekind (Current_Scope) = E_Void
6525 then
6526 declare
6527 N : constant Node_Id := Current_Scope;
6528 Freeze_Nodes : List_Id := No_List;
6529 Pos : Int := Scope_Stack.Last;
6531 begin
6532 if Present (Desig_Typ) then
6533 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
6534 end if;
6536 if Present (Typ) then
6537 Freeze_And_Append (Typ, N, Freeze_Nodes);
6538 end if;
6540 if Present (Nam) then
6541 Freeze_And_Append (Nam, N, Freeze_Nodes);
6542 end if;
6544 -- The current scope may be that of a constrained component of
6545 -- an enclosing record declaration, or of a loop of an enclosing
6546 -- quantified expression, which is above the current scope in the
6547 -- scope stack. Indeed in the context of a quantified expression,
6548 -- a scope is created and pushed above the current scope in order
6549 -- to emulate the loop-like behavior of the quantified expression.
6550 -- If the expression is within a top-level pragma, as for a pre-
6551 -- condition on a library-level subprogram, nothing to do.
6553 if not Is_Compilation_Unit (Current_Scope)
6554 and then (Is_Record_Type (Scope (Current_Scope))
6555 or else Nkind (Parent (Current_Scope)) =
6556 N_Quantified_Expression)
6557 then
6558 Pos := Pos - 1;
6559 end if;
6561 if Is_Non_Empty_List (Freeze_Nodes) then
6562 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
6563 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
6564 Freeze_Nodes;
6565 else
6566 Append_List (Freeze_Nodes,
6567 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
6568 end if;
6569 end if;
6570 end;
6572 return;
6573 end if;
6575 -- Now we have the right place to do the freezing. First, a special
6576 -- adjustment, if we are in spec-expression analysis mode, these freeze
6577 -- actions must not be thrown away (normally all inserted actions are
6578 -- thrown away in this mode. However, the freeze actions are from static
6579 -- expressions and one of the important reasons we are doing this
6580 -- special analysis is to get these freeze actions. Therefore we turn
6581 -- off the In_Spec_Expression mode to propagate these freeze actions.
6582 -- This also means they get properly analyzed and expanded.
6584 In_Spec_Expression := False;
6586 -- Freeze the designated type of an allocator (RM 13.14(13))
6588 if Present (Desig_Typ) then
6589 Freeze_Before (P, Desig_Typ);
6590 end if;
6592 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
6593 -- the enumeration representation clause exception in the loop above.
6595 if Present (Typ) then
6596 Freeze_Before (P, Typ);
6597 end if;
6599 -- Freeze name if one is present (RM 13.14(11))
6601 if Present (Nam) then
6602 Freeze_Before (P, Nam);
6603 end if;
6605 -- Restore In_Spec_Expression flag
6607 In_Spec_Expression := In_Spec_Exp;
6608 end Freeze_Expression;
6610 -----------------------------
6611 -- Freeze_Fixed_Point_Type --
6612 -----------------------------
6614 -- Certain fixed-point types and subtypes, including implicit base types
6615 -- and declared first subtypes, have not yet set up a range. This is
6616 -- because the range cannot be set until the Small and Size values are
6617 -- known, and these are not known till the type is frozen.
6619 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
6620 -- whose bounds are unanalyzed real literals. This routine will recognize
6621 -- this case, and transform this range node into a properly typed range
6622 -- with properly analyzed and resolved values.
6624 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
6625 Rng : constant Node_Id := Scalar_Range (Typ);
6626 Lo : constant Node_Id := Low_Bound (Rng);
6627 Hi : constant Node_Id := High_Bound (Rng);
6628 Btyp : constant Entity_Id := Base_Type (Typ);
6629 Brng : constant Node_Id := Scalar_Range (Btyp);
6630 BLo : constant Node_Id := Low_Bound (Brng);
6631 BHi : constant Node_Id := High_Bound (Brng);
6632 Small : constant Ureal := Small_Value (Typ);
6633 Loval : Ureal;
6634 Hival : Ureal;
6635 Atype : Entity_Id;
6637 Actual_Size : Nat;
6639 function Fsize (Lov, Hiv : Ureal) return Nat;
6640 -- Returns size of type with given bounds. Also leaves these
6641 -- bounds set as the current bounds of the Typ.
6643 -----------
6644 -- Fsize --
6645 -----------
6647 function Fsize (Lov, Hiv : Ureal) return Nat is
6648 begin
6649 Set_Realval (Lo, Lov);
6650 Set_Realval (Hi, Hiv);
6651 return Minimum_Size (Typ);
6652 end Fsize;
6654 -- Start of processing for Freeze_Fixed_Point_Type
6656 begin
6657 -- If Esize of a subtype has not previously been set, set it now
6659 if Unknown_Esize (Typ) then
6660 Atype := Ancestor_Subtype (Typ);
6662 if Present (Atype) then
6663 Set_Esize (Typ, Esize (Atype));
6664 else
6665 Set_Esize (Typ, Esize (Base_Type (Typ)));
6666 end if;
6667 end if;
6669 -- Immediate return if the range is already analyzed. This means that
6670 -- the range is already set, and does not need to be computed by this
6671 -- routine.
6673 if Analyzed (Rng) then
6674 return;
6675 end if;
6677 -- Immediate return if either of the bounds raises Constraint_Error
6679 if Raises_Constraint_Error (Lo)
6680 or else Raises_Constraint_Error (Hi)
6681 then
6682 return;
6683 end if;
6685 Loval := Realval (Lo);
6686 Hival := Realval (Hi);
6688 -- Ordinary fixed-point case
6690 if Is_Ordinary_Fixed_Point_Type (Typ) then
6692 -- For the ordinary fixed-point case, we are allowed to fudge the
6693 -- end-points up or down by small. Generally we prefer to fudge up,
6694 -- i.e. widen the bounds for non-model numbers so that the end points
6695 -- are included. However there are cases in which this cannot be
6696 -- done, and indeed cases in which we may need to narrow the bounds.
6697 -- The following circuit makes the decision.
6699 -- Note: our terminology here is that Incl_EP means that the bounds
6700 -- are widened by Small if necessary to include the end points, and
6701 -- Excl_EP means that the bounds are narrowed by Small to exclude the
6702 -- end-points if this reduces the size.
6704 -- Note that in the Incl case, all we care about is including the
6705 -- end-points. In the Excl case, we want to narrow the bounds as
6706 -- much as permitted by the RM, to give the smallest possible size.
6708 Fudge : declare
6709 Loval_Incl_EP : Ureal;
6710 Hival_Incl_EP : Ureal;
6712 Loval_Excl_EP : Ureal;
6713 Hival_Excl_EP : Ureal;
6715 Size_Incl_EP : Nat;
6716 Size_Excl_EP : Nat;
6718 Model_Num : Ureal;
6719 First_Subt : Entity_Id;
6720 Actual_Lo : Ureal;
6721 Actual_Hi : Ureal;
6723 begin
6724 -- First step. Base types are required to be symmetrical. Right
6725 -- now, the base type range is a copy of the first subtype range.
6726 -- This will be corrected before we are done, but right away we
6727 -- need to deal with the case where both bounds are non-negative.
6728 -- In this case, we set the low bound to the negative of the high
6729 -- bound, to make sure that the size is computed to include the
6730 -- required sign. Note that we do not need to worry about the
6731 -- case of both bounds negative, because the sign will be dealt
6732 -- with anyway. Furthermore we can't just go making such a bound
6733 -- symmetrical, since in a twos-complement system, there is an
6734 -- extra negative value which could not be accommodated on the
6735 -- positive side.
6737 if Typ = Btyp
6738 and then not UR_Is_Negative (Loval)
6739 and then Hival > Loval
6740 then
6741 Loval := -Hival;
6742 Set_Realval (Lo, Loval);
6743 end if;
6745 -- Compute the fudged bounds. If the number is a model number,
6746 -- then we do nothing to include it, but we are allowed to backoff
6747 -- to the next adjacent model number when we exclude it. If it is
6748 -- not a model number then we straddle the two values with the
6749 -- model numbers on either side.
6751 Model_Num := UR_Trunc (Loval / Small) * Small;
6753 if Loval = Model_Num then
6754 Loval_Incl_EP := Model_Num;
6755 else
6756 Loval_Incl_EP := Model_Num - Small;
6757 end if;
6759 -- The low value excluding the end point is Small greater, but
6760 -- we do not do this exclusion if the low value is positive,
6761 -- since it can't help the size and could actually hurt by
6762 -- crossing the high bound.
6764 if UR_Is_Negative (Loval_Incl_EP) then
6765 Loval_Excl_EP := Loval_Incl_EP + Small;
6767 -- If the value went from negative to zero, then we have the
6768 -- case where Loval_Incl_EP is the model number just below
6769 -- zero, so we want to stick to the negative value for the
6770 -- base type to maintain the condition that the size will
6771 -- include signed values.
6773 if Typ = Btyp
6774 and then UR_Is_Zero (Loval_Excl_EP)
6775 then
6776 Loval_Excl_EP := Loval_Incl_EP;
6777 end if;
6779 else
6780 Loval_Excl_EP := Loval_Incl_EP;
6781 end if;
6783 -- Similar processing for upper bound and high value
6785 Model_Num := UR_Trunc (Hival / Small) * Small;
6787 if Hival = Model_Num then
6788 Hival_Incl_EP := Model_Num;
6789 else
6790 Hival_Incl_EP := Model_Num + Small;
6791 end if;
6793 if UR_Is_Positive (Hival_Incl_EP) then
6794 Hival_Excl_EP := Hival_Incl_EP - Small;
6795 else
6796 Hival_Excl_EP := Hival_Incl_EP;
6797 end if;
6799 -- One further adjustment is needed. In the case of subtypes, we
6800 -- cannot go outside the range of the base type, or we get
6801 -- peculiarities, and the base type range is already set. This
6802 -- only applies to the Incl values, since clearly the Excl values
6803 -- are already as restricted as they are allowed to be.
6805 if Typ /= Btyp then
6806 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
6807 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
6808 end if;
6810 -- Get size including and excluding end points
6812 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
6813 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
6815 -- No need to exclude end-points if it does not reduce size
6817 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
6818 Loval_Excl_EP := Loval_Incl_EP;
6819 end if;
6821 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
6822 Hival_Excl_EP := Hival_Incl_EP;
6823 end if;
6825 -- Now we set the actual size to be used. We want to use the
6826 -- bounds fudged up to include the end-points but only if this
6827 -- can be done without violating a specifically given size
6828 -- size clause or causing an unacceptable increase in size.
6830 -- Case of size clause given
6832 if Has_Size_Clause (Typ) then
6834 -- Use the inclusive size only if it is consistent with
6835 -- the explicitly specified size.
6837 if Size_Incl_EP <= RM_Size (Typ) then
6838 Actual_Lo := Loval_Incl_EP;
6839 Actual_Hi := Hival_Incl_EP;
6840 Actual_Size := Size_Incl_EP;
6842 -- If the inclusive size is too large, we try excluding
6843 -- the end-points (will be caught later if does not work).
6845 else
6846 Actual_Lo := Loval_Excl_EP;
6847 Actual_Hi := Hival_Excl_EP;
6848 Actual_Size := Size_Excl_EP;
6849 end if;
6851 -- Case of size clause not given
6853 else
6854 -- If we have a base type whose corresponding first subtype
6855 -- has an explicit size that is large enough to include our
6856 -- end-points, then do so. There is no point in working hard
6857 -- to get a base type whose size is smaller than the specified
6858 -- size of the first subtype.
6860 First_Subt := First_Subtype (Typ);
6862 if Has_Size_Clause (First_Subt)
6863 and then Size_Incl_EP <= Esize (First_Subt)
6864 then
6865 Actual_Size := Size_Incl_EP;
6866 Actual_Lo := Loval_Incl_EP;
6867 Actual_Hi := Hival_Incl_EP;
6869 -- If excluding the end-points makes the size smaller and
6870 -- results in a size of 8,16,32,64, then we take the smaller
6871 -- size. For the 64 case, this is compulsory. For the other
6872 -- cases, it seems reasonable. We like to include end points
6873 -- if we can, but not at the expense of moving to the next
6874 -- natural boundary of size.
6876 elsif Size_Incl_EP /= Size_Excl_EP
6877 and then Addressable (Size_Excl_EP)
6878 then
6879 Actual_Size := Size_Excl_EP;
6880 Actual_Lo := Loval_Excl_EP;
6881 Actual_Hi := Hival_Excl_EP;
6883 -- Otherwise we can definitely include the end points
6885 else
6886 Actual_Size := Size_Incl_EP;
6887 Actual_Lo := Loval_Incl_EP;
6888 Actual_Hi := Hival_Incl_EP;
6889 end if;
6891 -- One pathological case: normally we never fudge a low bound
6892 -- down, since it would seem to increase the size (if it has
6893 -- any effect), but for ranges containing single value, or no
6894 -- values, the high bound can be small too large. Consider:
6896 -- type t is delta 2.0**(-14)
6897 -- range 131072.0 .. 0;
6899 -- That lower bound is *just* outside the range of 32 bits, and
6900 -- does need fudging down in this case. Note that the bounds
6901 -- will always have crossed here, since the high bound will be
6902 -- fudged down if necessary, as in the case of:
6904 -- type t is delta 2.0**(-14)
6905 -- range 131072.0 .. 131072.0;
6907 -- So we detect the situation by looking for crossed bounds,
6908 -- and if the bounds are crossed, and the low bound is greater
6909 -- than zero, we will always back it off by small, since this
6910 -- is completely harmless.
6912 if Actual_Lo > Actual_Hi then
6913 if UR_Is_Positive (Actual_Lo) then
6914 Actual_Lo := Loval_Incl_EP - Small;
6915 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
6917 -- And of course, we need to do exactly the same parallel
6918 -- fudge for flat ranges in the negative region.
6920 elsif UR_Is_Negative (Actual_Hi) then
6921 Actual_Hi := Hival_Incl_EP + Small;
6922 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
6923 end if;
6924 end if;
6925 end if;
6927 Set_Realval (Lo, Actual_Lo);
6928 Set_Realval (Hi, Actual_Hi);
6929 end Fudge;
6931 -- For the decimal case, none of this fudging is required, since there
6932 -- are no end-point problems in the decimal case (the end-points are
6933 -- always included).
6935 else
6936 Actual_Size := Fsize (Loval, Hival);
6937 end if;
6939 -- At this stage, the actual size has been calculated and the proper
6940 -- required bounds are stored in the low and high bounds.
6942 if Actual_Size > 64 then
6943 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
6944 Error_Msg_N
6945 ("size required (^) for type& too large, maximum allowed is 64",
6946 Typ);
6947 Actual_Size := 64;
6948 end if;
6950 -- Check size against explicit given size
6952 if Has_Size_Clause (Typ) then
6953 if Actual_Size > RM_Size (Typ) then
6954 Error_Msg_Uint_1 := RM_Size (Typ);
6955 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
6956 Error_Msg_NE
6957 ("size given (^) for type& too small, minimum allowed is ^",
6958 Size_Clause (Typ), Typ);
6960 else
6961 Actual_Size := UI_To_Int (Esize (Typ));
6962 end if;
6964 -- Increase size to next natural boundary if no size clause given
6966 else
6967 if Actual_Size <= 8 then
6968 Actual_Size := 8;
6969 elsif Actual_Size <= 16 then
6970 Actual_Size := 16;
6971 elsif Actual_Size <= 32 then
6972 Actual_Size := 32;
6973 else
6974 Actual_Size := 64;
6975 end if;
6977 Init_Esize (Typ, Actual_Size);
6978 Adjust_Esize_For_Alignment (Typ);
6979 end if;
6981 -- If we have a base type, then expand the bounds so that they extend to
6982 -- the full width of the allocated size in bits, to avoid junk range
6983 -- checks on intermediate computations.
6985 if Base_Type (Typ) = Typ then
6986 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
6987 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
6988 end if;
6990 -- Final step is to reanalyze the bounds using the proper type
6991 -- and set the Corresponding_Integer_Value fields of the literals.
6993 Set_Etype (Lo, Empty);
6994 Set_Analyzed (Lo, False);
6995 Analyze (Lo);
6997 -- Resolve with universal fixed if the base type, and the base type if
6998 -- it is a subtype. Note we can't resolve the base type with itself,
6999 -- that would be a reference before definition.
7001 if Typ = Btyp then
7002 Resolve (Lo, Universal_Fixed);
7003 else
7004 Resolve (Lo, Btyp);
7005 end if;
7007 -- Set corresponding integer value for bound
7009 Set_Corresponding_Integer_Value
7010 (Lo, UR_To_Uint (Realval (Lo) / Small));
7012 -- Similar processing for high bound
7014 Set_Etype (Hi, Empty);
7015 Set_Analyzed (Hi, False);
7016 Analyze (Hi);
7018 if Typ = Btyp then
7019 Resolve (Hi, Universal_Fixed);
7020 else
7021 Resolve (Hi, Btyp);
7022 end if;
7024 Set_Corresponding_Integer_Value
7025 (Hi, UR_To_Uint (Realval (Hi) / Small));
7027 -- Set type of range to correspond to bounds
7029 Set_Etype (Rng, Etype (Lo));
7031 -- Set Esize to calculated size if not set already
7033 if Unknown_Esize (Typ) then
7034 Init_Esize (Typ, Actual_Size);
7035 end if;
7037 -- Set RM_Size if not already set. If already set, check value
7039 declare
7040 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7042 begin
7043 if RM_Size (Typ) /= Uint_0 then
7044 if RM_Size (Typ) < Minsiz then
7045 Error_Msg_Uint_1 := RM_Size (Typ);
7046 Error_Msg_Uint_2 := Minsiz;
7047 Error_Msg_NE
7048 ("size given (^) for type& too small, minimum allowed is ^",
7049 Size_Clause (Typ), Typ);
7050 end if;
7052 else
7053 Set_RM_Size (Typ, Minsiz);
7054 end if;
7055 end;
7056 end Freeze_Fixed_Point_Type;
7058 ------------------
7059 -- Freeze_Itype --
7060 ------------------
7062 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7063 L : List_Id;
7065 begin
7066 Set_Has_Delayed_Freeze (T);
7067 L := Freeze_Entity (T, N);
7069 if Is_Non_Empty_List (L) then
7070 Insert_Actions (N, L);
7071 end if;
7072 end Freeze_Itype;
7074 --------------------------
7075 -- Freeze_Static_Object --
7076 --------------------------
7078 procedure Freeze_Static_Object (E : Entity_Id) is
7080 Cannot_Be_Static : exception;
7081 -- Exception raised if the type of a static object cannot be made
7082 -- static. This happens if the type depends on non-global objects.
7084 procedure Ensure_Expression_Is_SA (N : Node_Id);
7085 -- Called to ensure that an expression used as part of a type definition
7086 -- is statically allocatable, which means that the expression type is
7087 -- statically allocatable, and the expression is either static, or a
7088 -- reference to a library level constant.
7090 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7091 -- Called to mark a type as static, checking that it is possible
7092 -- to set the type as static. If it is not possible, then the
7093 -- exception Cannot_Be_Static is raised.
7095 -----------------------------
7096 -- Ensure_Expression_Is_SA --
7097 -----------------------------
7099 procedure Ensure_Expression_Is_SA (N : Node_Id) is
7100 Ent : Entity_Id;
7102 begin
7103 Ensure_Type_Is_SA (Etype (N));
7105 if Is_OK_Static_Expression (N) then
7106 return;
7108 elsif Nkind (N) = N_Identifier then
7109 Ent := Entity (N);
7111 if Present (Ent)
7112 and then Ekind (Ent) = E_Constant
7113 and then Is_Library_Level_Entity (Ent)
7114 then
7115 return;
7116 end if;
7117 end if;
7119 raise Cannot_Be_Static;
7120 end Ensure_Expression_Is_SA;
7122 -----------------------
7123 -- Ensure_Type_Is_SA --
7124 -----------------------
7126 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7127 N : Node_Id;
7128 C : Entity_Id;
7130 begin
7131 -- If type is library level, we are all set
7133 if Is_Library_Level_Entity (Typ) then
7134 return;
7135 end if;
7137 -- We are also OK if the type already marked as statically allocated,
7138 -- which means we processed it before.
7140 if Is_Statically_Allocated (Typ) then
7141 return;
7142 end if;
7144 -- Mark type as statically allocated
7146 Set_Is_Statically_Allocated (Typ);
7148 -- Check that it is safe to statically allocate this type
7150 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
7151 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
7152 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
7154 elsif Is_Array_Type (Typ) then
7155 N := First_Index (Typ);
7156 while Present (N) loop
7157 Ensure_Type_Is_SA (Etype (N));
7158 Next_Index (N);
7159 end loop;
7161 Ensure_Type_Is_SA (Component_Type (Typ));
7163 elsif Is_Access_Type (Typ) then
7164 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
7166 declare
7167 F : Entity_Id;
7168 T : constant Entity_Id := Etype (Designated_Type (Typ));
7170 begin
7171 if T /= Standard_Void_Type then
7172 Ensure_Type_Is_SA (T);
7173 end if;
7175 F := First_Formal (Designated_Type (Typ));
7176 while Present (F) loop
7177 Ensure_Type_Is_SA (Etype (F));
7178 Next_Formal (F);
7179 end loop;
7180 end;
7182 else
7183 Ensure_Type_Is_SA (Designated_Type (Typ));
7184 end if;
7186 elsif Is_Record_Type (Typ) then
7187 C := First_Entity (Typ);
7188 while Present (C) loop
7189 if Ekind (C) = E_Discriminant
7190 or else Ekind (C) = E_Component
7191 then
7192 Ensure_Type_Is_SA (Etype (C));
7194 elsif Is_Type (C) then
7195 Ensure_Type_Is_SA (C);
7196 end if;
7198 Next_Entity (C);
7199 end loop;
7201 elsif Ekind (Typ) = E_Subprogram_Type then
7202 Ensure_Type_Is_SA (Etype (Typ));
7204 C := First_Formal (Typ);
7205 while Present (C) loop
7206 Ensure_Type_Is_SA (Etype (C));
7207 Next_Formal (C);
7208 end loop;
7210 else
7211 raise Cannot_Be_Static;
7212 end if;
7213 end Ensure_Type_Is_SA;
7215 -- Start of processing for Freeze_Static_Object
7217 begin
7218 Ensure_Type_Is_SA (Etype (E));
7220 exception
7221 when Cannot_Be_Static =>
7223 -- If the object that cannot be static is imported or exported, then
7224 -- issue an error message saying that this object cannot be imported
7225 -- or exported. If it has an address clause it is an overlay in the
7226 -- current partition and the static requirement is not relevant.
7227 -- Do not issue any error message when ignoring rep clauses.
7229 if Ignore_Rep_Clauses then
7230 null;
7232 elsif Is_Imported (E) then
7233 if No (Address_Clause (E)) then
7234 Error_Msg_N
7235 ("& cannot be imported (local type is not constant)", E);
7236 end if;
7238 -- Otherwise must be exported, something is wrong if compiler
7239 -- is marking something as statically allocated which cannot be).
7241 else pragma Assert (Is_Exported (E));
7242 Error_Msg_N
7243 ("& cannot be exported (local type is not constant)", E);
7244 end if;
7245 end Freeze_Static_Object;
7247 -----------------------
7248 -- Freeze_Subprogram --
7249 -----------------------
7251 procedure Freeze_Subprogram (E : Entity_Id) is
7252 Retype : Entity_Id;
7253 F : Entity_Id;
7255 begin
7256 -- Subprogram may not have an address clause unless it is imported
7258 if Present (Address_Clause (E)) then
7259 if not Is_Imported (E) then
7260 Error_Msg_N
7261 ("address clause can only be given " &
7262 "for imported subprogram",
7263 Name (Address_Clause (E)));
7264 end if;
7265 end if;
7267 -- Reset the Pure indication on an imported subprogram unless an
7268 -- explicit Pure_Function pragma was present or the subprogram is an
7269 -- intrinsic. We do this because otherwise it is an insidious error
7270 -- to call a non-pure function from pure unit and have calls
7271 -- mysteriously optimized away. What happens here is that the Import
7272 -- can bypass the normal check to ensure that pure units call only pure
7273 -- subprograms.
7275 -- The reason for the intrinsic exception is that in general, intrinsic
7276 -- functions (such as shifts) are pure anyway. The only exceptions are
7277 -- the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
7278 -- in any case, so no problem arises.
7280 if Is_Imported (E)
7281 and then Is_Pure (E)
7282 and then not Has_Pragma_Pure_Function (E)
7283 and then not Is_Intrinsic_Subprogram (E)
7284 then
7285 Set_Is_Pure (E, False);
7286 end if;
7288 -- For non-foreign convention subprograms, this is where we create
7289 -- the extra formals (for accessibility level and constrained bit
7290 -- information). We delay this till the freeze point precisely so
7291 -- that we know the convention.
7293 if not Has_Foreign_Convention (E) then
7294 Create_Extra_Formals (E);
7295 Set_Mechanisms (E);
7297 -- If this is convention Ada and a Valued_Procedure, that's odd
7299 if Ekind (E) = E_Procedure
7300 and then Is_Valued_Procedure (E)
7301 and then Convention (E) = Convention_Ada
7302 and then Warn_On_Export_Import
7303 then
7304 Error_Msg_N
7305 ("??Valued_Procedure has no effect for convention Ada", E);
7306 Set_Is_Valued_Procedure (E, False);
7307 end if;
7309 -- Case of foreign convention
7311 else
7312 Set_Mechanisms (E);
7314 -- For foreign conventions, warn about return of unconstrained array
7316 if Ekind (E) = E_Function then
7317 Retype := Underlying_Type (Etype (E));
7319 -- If no return type, probably some other error, e.g. a
7320 -- missing full declaration, so ignore.
7322 if No (Retype) then
7323 null;
7325 -- If the return type is generic, we have emitted a warning
7326 -- earlier on, and there is nothing else to check here. Specific
7327 -- instantiations may lead to erroneous behavior.
7329 elsif Is_Generic_Type (Etype (E)) then
7330 null;
7332 -- Display warning if returning unconstrained array
7334 elsif Is_Array_Type (Retype)
7335 and then not Is_Constrained (Retype)
7337 -- Check appropriate warning is enabled (should we check for
7338 -- Warnings (Off) on specific entities here, probably so???)
7340 and then Warn_On_Export_Import
7342 -- Exclude the VM case, since return of unconstrained arrays
7343 -- is properly handled in both the JVM and .NET cases.
7345 and then VM_Target = No_VM
7346 then
7347 Error_Msg_N
7348 ("?x?foreign convention function& should not return " &
7349 "unconstrained array", E);
7350 return;
7351 end if;
7352 end if;
7354 -- If any of the formals for an exported foreign convention
7355 -- subprogram have defaults, then emit an appropriate warning since
7356 -- this is odd (default cannot be used from non-Ada code)
7358 if Is_Exported (E) then
7359 F := First_Formal (E);
7360 while Present (F) loop
7361 if Warn_On_Export_Import
7362 and then Present (Default_Value (F))
7363 then
7364 Error_Msg_N
7365 ("?x?parameter cannot be defaulted in non-Ada call",
7366 Default_Value (F));
7367 end if;
7369 Next_Formal (F);
7370 end loop;
7371 end if;
7372 end if;
7374 -- Pragma Inline_Always is disallowed for dispatching subprograms
7375 -- because the address of such subprograms is saved in the dispatch
7376 -- table to support dispatching calls, and dispatching calls cannot
7377 -- be inlined. This is consistent with the restriction against using
7378 -- 'Access or 'Address on an Inline_Always subprogram.
7380 if Is_Dispatching_Operation (E)
7381 and then Has_Pragma_Inline_Always (E)
7382 then
7383 Error_Msg_N
7384 ("pragma Inline_Always not allowed for dispatching subprograms", E);
7385 end if;
7387 -- Because of the implicit representation of inherited predefined
7388 -- operators in the front-end, the overriding status of the operation
7389 -- may be affected when a full view of a type is analyzed, and this is
7390 -- not captured by the analysis of the corresponding type declaration.
7391 -- Therefore the correctness of a not-overriding indicator must be
7392 -- rechecked when the subprogram is frozen.
7394 if Nkind (E) = N_Defining_Operator_Symbol
7395 and then not Error_Posted (Parent (E))
7396 then
7397 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
7398 end if;
7399 end Freeze_Subprogram;
7401 ----------------------
7402 -- Is_Fully_Defined --
7403 ----------------------
7405 function Is_Fully_Defined (T : Entity_Id) return Boolean is
7406 begin
7407 if Ekind (T) = E_Class_Wide_Type then
7408 return Is_Fully_Defined (Etype (T));
7410 elsif Is_Array_Type (T) then
7411 return Is_Fully_Defined (Component_Type (T));
7413 elsif Is_Record_Type (T)
7414 and not Is_Private_Type (T)
7415 then
7416 -- Verify that the record type has no components with private types
7417 -- without completion.
7419 declare
7420 Comp : Entity_Id;
7422 begin
7423 Comp := First_Component (T);
7424 while Present (Comp) loop
7425 if not Is_Fully_Defined (Etype (Comp)) then
7426 return False;
7427 end if;
7429 Next_Component (Comp);
7430 end loop;
7431 return True;
7432 end;
7434 -- For the designated type of an access to subprogram, all types in
7435 -- the profile must be fully defined.
7437 elsif Ekind (T) = E_Subprogram_Type then
7438 declare
7439 F : Entity_Id;
7441 begin
7442 F := First_Formal (T);
7443 while Present (F) loop
7444 if not Is_Fully_Defined (Etype (F)) then
7445 return False;
7446 end if;
7448 Next_Formal (F);
7449 end loop;
7451 return Is_Fully_Defined (Etype (T));
7452 end;
7454 else
7455 return not Is_Private_Type (T)
7456 or else Present (Full_View (Base_Type (T)));
7457 end if;
7458 end Is_Fully_Defined;
7460 ---------------------------------
7461 -- Process_Default_Expressions --
7462 ---------------------------------
7464 procedure Process_Default_Expressions
7465 (E : Entity_Id;
7466 After : in out Node_Id)
7468 Loc : constant Source_Ptr := Sloc (E);
7469 Dbody : Node_Id;
7470 Formal : Node_Id;
7471 Dcopy : Node_Id;
7472 Dnam : Entity_Id;
7474 begin
7475 Set_Default_Expressions_Processed (E);
7477 -- A subprogram instance and its associated anonymous subprogram share
7478 -- their signature. The default expression functions are defined in the
7479 -- wrapper packages for the anonymous subprogram, and should not be
7480 -- generated again for the instance.
7482 if Is_Generic_Instance (E)
7483 and then Present (Alias (E))
7484 and then Default_Expressions_Processed (Alias (E))
7485 then
7486 return;
7487 end if;
7489 Formal := First_Formal (E);
7490 while Present (Formal) loop
7491 if Present (Default_Value (Formal)) then
7493 -- We work with a copy of the default expression because we
7494 -- do not want to disturb the original, since this would mess
7495 -- up the conformance checking.
7497 Dcopy := New_Copy_Tree (Default_Value (Formal));
7499 -- The analysis of the expression may generate insert actions,
7500 -- which of course must not be executed. We wrap those actions
7501 -- in a procedure that is not called, and later on eliminated.
7502 -- The following cases have no side-effects, and are analyzed
7503 -- directly.
7505 if Nkind (Dcopy) = N_Identifier
7506 or else Nkind_In (Dcopy, N_Expanded_Name,
7507 N_Integer_Literal,
7508 N_Character_Literal,
7509 N_String_Literal,
7510 N_Real_Literal)
7511 or else (Nkind (Dcopy) = N_Attribute_Reference
7512 and then Attribute_Name (Dcopy) = Name_Null_Parameter)
7513 or else Known_Null (Dcopy)
7514 then
7515 -- If there is no default function, we must still do a full
7516 -- analyze call on the default value, to ensure that all error
7517 -- checks are performed, e.g. those associated with static
7518 -- evaluation. Note: this branch will always be taken if the
7519 -- analyzer is turned off (but we still need the error checks).
7521 -- Note: the setting of parent here is to meet the requirement
7522 -- that we can only analyze the expression while attached to
7523 -- the tree. Really the requirement is that the parent chain
7524 -- be set, we don't actually need to be in the tree.
7526 Set_Parent (Dcopy, Declaration_Node (Formal));
7527 Analyze (Dcopy);
7529 -- Default expressions are resolved with their own type if the
7530 -- context is generic, to avoid anomalies with private types.
7532 if Ekind (Scope (E)) = E_Generic_Package then
7533 Resolve (Dcopy);
7534 else
7535 Resolve (Dcopy, Etype (Formal));
7536 end if;
7538 -- If that resolved expression will raise constraint error,
7539 -- then flag the default value as raising constraint error.
7540 -- This allows a proper error message on the calls.
7542 if Raises_Constraint_Error (Dcopy) then
7543 Set_Raises_Constraint_Error (Default_Value (Formal));
7544 end if;
7546 -- If the default is a parameterless call, we use the name of
7547 -- the called function directly, and there is no body to build.
7549 elsif Nkind (Dcopy) = N_Function_Call
7550 and then No (Parameter_Associations (Dcopy))
7551 then
7552 null;
7554 -- Else construct and analyze the body of a wrapper procedure
7555 -- that contains an object declaration to hold the expression.
7556 -- Given that this is done only to complete the analysis, it
7557 -- simpler to build a procedure than a function which might
7558 -- involve secondary stack expansion.
7560 else
7561 Dnam := Make_Temporary (Loc, 'D');
7563 Dbody :=
7564 Make_Subprogram_Body (Loc,
7565 Specification =>
7566 Make_Procedure_Specification (Loc,
7567 Defining_Unit_Name => Dnam),
7569 Declarations => New_List (
7570 Make_Object_Declaration (Loc,
7571 Defining_Identifier => Make_Temporary (Loc, 'T'),
7572 Object_Definition =>
7573 New_Occurrence_Of (Etype (Formal), Loc),
7574 Expression => New_Copy_Tree (Dcopy))),
7576 Handled_Statement_Sequence =>
7577 Make_Handled_Sequence_Of_Statements (Loc,
7578 Statements => Empty_List));
7580 Set_Scope (Dnam, Scope (E));
7581 Set_Assignment_OK (First (Declarations (Dbody)));
7582 Set_Is_Eliminated (Dnam);
7583 Insert_After (After, Dbody);
7584 Analyze (Dbody);
7585 After := Dbody;
7586 end if;
7587 end if;
7589 Next_Formal (Formal);
7590 end loop;
7591 end Process_Default_Expressions;
7593 ----------------------------------------
7594 -- Set_Component_Alignment_If_Not_Set --
7595 ----------------------------------------
7597 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
7598 begin
7599 -- Ignore if not base type, subtypes don't need anything
7601 if Typ /= Base_Type (Typ) then
7602 return;
7603 end if;
7605 -- Do not override existing representation
7607 if Is_Packed (Typ) then
7608 return;
7610 elsif Has_Specified_Layout (Typ) then
7611 return;
7613 elsif Component_Alignment (Typ) /= Calign_Default then
7614 return;
7616 else
7617 Set_Component_Alignment
7618 (Typ, Scope_Stack.Table
7619 (Scope_Stack.Last).Component_Alignment_Default);
7620 end if;
7621 end Set_Component_Alignment_If_Not_Set;
7623 --------------------------
7624 -- Set_SSO_From_Default --
7625 --------------------------
7627 procedure Set_SSO_From_Default (T : Entity_Id) is
7628 begin
7629 if (Is_Record_Type (T) or else Is_Array_Type (T))
7630 and then Is_Base_Type (T)
7631 then
7632 if ((Bytes_Big_Endian and then SSO_Set_Low_By_Default (T))
7633 or else
7634 ((not Bytes_Big_Endian) and then SSO_Set_High_By_Default (T)))
7636 -- For a record type, if native bit order is specified explicitly,
7637 -- then never set reverse SSO from default.
7639 and then not
7640 (Is_Record_Type (T)
7641 and then Has_Rep_Item (T, Name_Bit_Order)
7642 and then not Reverse_Bit_Order (T))
7643 then
7644 -- If flags cause reverse storage order, then set the result. Note
7645 -- that we would have ignored the pragma setting the non default
7646 -- storage order in any case, hence the assertion at this point.
7648 pragma Assert (Support_Nondefault_SSO_On_Target);
7649 Set_Reverse_Storage_Order (T);
7651 -- For a record type, also set reversed bit order. Note that if
7652 -- a bit order has been specified explicitly, then this is a
7653 -- no-op, as per the guard above.
7655 if Is_Record_Type (T) then
7656 Set_Reverse_Bit_Order (T);
7657 end if;
7658 end if;
7659 end if;
7660 end Set_SSO_From_Default;
7662 ------------------
7663 -- Undelay_Type --
7664 ------------------
7666 procedure Undelay_Type (T : Entity_Id) is
7667 begin
7668 Set_Has_Delayed_Freeze (T, False);
7669 Set_Freeze_Node (T, Empty);
7671 -- Since we don't want T to have a Freeze_Node, we don't want its
7672 -- Full_View or Corresponding_Record_Type to have one either.
7674 -- ??? Fundamentally, this whole handling is unpleasant. What we really
7675 -- want is to be sure that for an Itype that's part of record R and is a
7676 -- subtype of type T, that it's frozen after the later of the freeze
7677 -- points of R and T. We have no way of doing that directly, so what we
7678 -- do is force most such Itypes to be frozen as part of freezing R via
7679 -- this procedure and only delay the ones that need to be delayed
7680 -- (mostly the designated types of access types that are defined as part
7681 -- of the record).
7683 if Is_Private_Type (T)
7684 and then Present (Full_View (T))
7685 and then Is_Itype (Full_View (T))
7686 and then Is_Record_Type (Scope (Full_View (T)))
7687 then
7688 Undelay_Type (Full_View (T));
7689 end if;
7691 if Is_Concurrent_Type (T)
7692 and then Present (Corresponding_Record_Type (T))
7693 and then Is_Itype (Corresponding_Record_Type (T))
7694 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
7695 then
7696 Undelay_Type (Corresponding_Record_Type (T));
7697 end if;
7698 end Undelay_Type;
7700 ------------------
7701 -- Warn_Overlay --
7702 ------------------
7704 procedure Warn_Overlay
7705 (Expr : Node_Id;
7706 Typ : Entity_Id;
7707 Nam : Entity_Id)
7709 Ent : constant Entity_Id := Entity (Nam);
7710 -- The object to which the address clause applies
7712 Init : Node_Id;
7713 Old : Entity_Id := Empty;
7714 Decl : Node_Id;
7716 begin
7717 -- No warning if address clause overlay warnings are off
7719 if not Address_Clause_Overlay_Warnings then
7720 return;
7721 end if;
7723 -- No warning if there is an explicit initialization
7725 Init := Original_Node (Expression (Declaration_Node (Ent)));
7727 if Present (Init) and then Comes_From_Source (Init) then
7728 return;
7729 end if;
7731 -- We only give the warning for non-imported entities of a type for
7732 -- which a non-null base init proc is defined, or for objects of access
7733 -- types with implicit null initialization, or when Normalize_Scalars
7734 -- applies and the type is scalar or a string type (the latter being
7735 -- tested for because predefined String types are initialized by inline
7736 -- code rather than by an init_proc). Note that we do not give the
7737 -- warning for Initialize_Scalars, since we suppressed initialization
7738 -- in this case. Also, do not warn if Suppress_Initialization is set.
7740 if Present (Expr)
7741 and then not Is_Imported (Ent)
7742 and then not Initialization_Suppressed (Typ)
7743 and then (Has_Non_Null_Base_Init_Proc (Typ)
7744 or else Is_Access_Type (Typ)
7745 or else (Normalize_Scalars
7746 and then (Is_Scalar_Type (Typ)
7747 or else Is_String_Type (Typ))))
7748 then
7749 if Nkind (Expr) = N_Attribute_Reference
7750 and then Is_Entity_Name (Prefix (Expr))
7751 then
7752 Old := Entity (Prefix (Expr));
7754 elsif Is_Entity_Name (Expr)
7755 and then Ekind (Entity (Expr)) = E_Constant
7756 then
7757 Decl := Declaration_Node (Entity (Expr));
7759 if Nkind (Decl) = N_Object_Declaration
7760 and then Present (Expression (Decl))
7761 and then Nkind (Expression (Decl)) = N_Attribute_Reference
7762 and then Is_Entity_Name (Prefix (Expression (Decl)))
7763 then
7764 Old := Entity (Prefix (Expression (Decl)));
7766 elsif Nkind (Expr) = N_Function_Call then
7767 return;
7768 end if;
7770 -- A function call (most likely to To_Address) is probably not an
7771 -- overlay, so skip warning. Ditto if the function call was inlined
7772 -- and transformed into an entity.
7774 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
7775 return;
7776 end if;
7778 Decl := Next (Parent (Expr));
7780 -- If a pragma Import follows, we assume that it is for the current
7781 -- target of the address clause, and skip the warning.
7783 if Present (Decl)
7784 and then Nkind (Decl) = N_Pragma
7785 and then Pragma_Name (Decl) = Name_Import
7786 then
7787 return;
7788 end if;
7790 if Present (Old) then
7791 Error_Msg_Node_2 := Old;
7792 Error_Msg_N
7793 ("default initialization of & may modify &??",
7794 Nam);
7795 else
7796 Error_Msg_N
7797 ("default initialization of & may modify overlaid storage??",
7798 Nam);
7799 end if;
7801 -- Add friendly warning if initialization comes from a packed array
7802 -- component.
7804 if Is_Record_Type (Typ) then
7805 declare
7806 Comp : Entity_Id;
7808 begin
7809 Comp := First_Component (Typ);
7810 while Present (Comp) loop
7811 if Nkind (Parent (Comp)) = N_Component_Declaration
7812 and then Present (Expression (Parent (Comp)))
7813 then
7814 exit;
7815 elsif Is_Array_Type (Etype (Comp))
7816 and then Present (Packed_Array_Impl_Type (Etype (Comp)))
7817 then
7818 Error_Msg_NE
7819 ("\packed array component& " &
7820 "will be initialized to zero??",
7821 Nam, Comp);
7822 exit;
7823 else
7824 Next_Component (Comp);
7825 end if;
7826 end loop;
7827 end;
7828 end if;
7830 Error_Msg_N
7831 ("\use pragma Import for & to " &
7832 "suppress initialization (RM B.1(24))??",
7833 Nam);
7834 end if;
7835 end Warn_Overlay;
7837 end Freeze;