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