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