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