]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/freeze.adb
[multiple changes]
[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-- --
e1308fa8 9-- Copyright (C) 1992-2012, 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
26with Atree; use Atree;
27with Debug; use Debug;
28with Einfo; use Einfo;
29with Elists; use Elists;
30with Errout; use Errout;
1ce1f005 31with Exp_Ch3; use Exp_Ch3;
70482933 32with Exp_Ch7; use Exp_Ch7;
ce2b6ba5 33with Exp_Disp; use Exp_Disp;
70482933
RK
34with Exp_Pakd; use Exp_Pakd;
35with Exp_Util; use Exp_Util;
fbf5a39b 36with Exp_Tss; use Exp_Tss;
70482933 37with Layout; use Layout;
ca0cb93e 38with Lib; use Lib;
7d8b9c99 39with Namet; use Namet;
70482933
RK
40with Nlists; use Nlists;
41with Nmake; use Nmake;
42with Opt; use Opt;
43with Restrict; use Restrict;
6e937c1c 44with Rident; use Rident;
b7f7dab2 45with Rtsfind; use Rtsfind;
70482933 46with Sem; use Sem;
a4100e55 47with Sem_Aux; use Sem_Aux;
70482933
RK
48with Sem_Cat; use Sem_Cat;
49with Sem_Ch6; use Sem_Ch6;
50with Sem_Ch7; use Sem_Ch7;
51with Sem_Ch8; use Sem_Ch8;
b98e2969 52with Sem_Ch9; use Sem_Ch9;
70482933
RK
53with Sem_Ch13; use Sem_Ch13;
54with Sem_Eval; use Sem_Eval;
55with Sem_Mech; use Sem_Mech;
56with Sem_Prag; use Sem_Prag;
57with Sem_Res; use Sem_Res;
58with Sem_Util; use Sem_Util;
59with Sinfo; use Sinfo;
60with Snames; use Snames;
61with Stand; use Stand;
62with Targparm; use Targparm;
63with Tbuild; use Tbuild;
64with Ttypes; use Ttypes;
65with Uintp; use Uintp;
66with Urealp; use Urealp;
67
68package body Freeze is
69
70 -----------------------
71 -- Local Subprograms --
72 -----------------------
73
74 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
75 -- Typ is a type that is being frozen. If no size clause is given,
76 -- but a default Esize has been computed, then this default Esize is
77 -- adjusted up if necessary to be consistent with a given alignment,
78 -- but never to a value greater than Long_Long_Integer'Size. This
79 -- is used for all discrete types and for fixed-point types.
80
81 procedure Build_And_Analyze_Renamed_Body
82 (Decl : Node_Id;
83 New_S : Entity_Id;
84 After : in out Node_Id);
49e90211 85 -- Build body for a renaming declaration, insert in tree and analyze
70482933 86
fbf5a39b
AC
87 procedure Check_Address_Clause (E : Entity_Id);
88 -- Apply legality checks to address clauses for object declarations,
2c9beb8a 89 -- at the point the object is frozen.
fbf5a39b 90
70482933
RK
91 procedure Check_Strict_Alignment (E : Entity_Id);
92 -- E is a base type. If E is tagged or has a component that is aliased
93 -- or tagged or contains something this is aliased or tagged, set
94 -- Strict_Alignment.
95
96 procedure Check_Unsigned_Type (E : Entity_Id);
97 pragma Inline (Check_Unsigned_Type);
98 -- If E is a fixed-point or discrete type, then all the necessary work
99 -- to freeze it is completed except for possible setting of the flag
100 -- Is_Unsigned_Type, which is done by this procedure. The call has no
101 -- effect if the entity E is not a discrete or fixed-point type.
102
103 procedure Freeze_And_Append
104 (Ent : Entity_Id;
c159409f 105 N : Node_Id;
70482933
RK
106 Result : in out List_Id);
107 -- Freezes Ent using Freeze_Entity, and appends the resulting list of
c159409f
AC
108 -- nodes to Result, modifying Result from No_List if necessary. N has
109 -- the same usage as in Freeze_Entity.
70482933
RK
110
111 procedure Freeze_Enumeration_Type (Typ : Entity_Id);
112 -- Freeze enumeration type. The Esize field is set as processing
113 -- proceeds (i.e. set by default when the type is declared and then
114 -- adjusted by rep clauses. What this procedure does is to make sure
115 -- that if a foreign convention is specified, and no specific size
116 -- is given, then the size must be at least Integer'Size.
117
70482933
RK
118 procedure Freeze_Static_Object (E : Entity_Id);
119 -- If an object is frozen which has Is_Statically_Allocated set, then
120 -- all referenced types must also be marked with this flag. This routine
121 -- is in charge of meeting this requirement for the object entity E.
122
123 procedure Freeze_Subprogram (E : Entity_Id);
124 -- Perform freezing actions for a subprogram (create extra formals,
125 -- and set proper default mechanism values). Note that this routine
126 -- is not called for internal subprograms, for which neither of these
127 -- actions is needed (or desirable, we do not want for example to have
128 -- these extra formals present in initialization procedures, where they
129 -- would serve no purpose). In this call E is either a subprogram or
130 -- a subprogram type (i.e. an access to a subprogram).
131
132 function Is_Fully_Defined (T : Entity_Id) return Boolean;
bde58e32 133 -- True if T is not private and has no private components, or has a full
657a9dd9
AC
134 -- view. Used to determine whether the designated type of an access type
135 -- should be frozen when the access type is frozen. This is done when an
136 -- allocator is frozen, or an expression that may involve attributes of
137 -- the designated type. Otherwise freezing the access type does not freeze
138 -- the designated type.
70482933
RK
139
140 procedure Process_Default_Expressions
141 (E : Entity_Id;
142 After : in out Node_Id);
c159409f
AC
143 -- This procedure is called for each subprogram to complete processing of
144 -- default expressions at the point where all types are known to be frozen.
145 -- The expressions must be analyzed in full, to make sure that all error
146 -- processing is done (they have only been pre-analyzed). If the expression
147 -- is not an entity or literal, its analysis may generate code which must
148 -- not be executed. In that case we build a function body to hold that
149 -- code. This wrapper function serves no other purpose (it used to be
150 -- called to evaluate the default, but now the default is inlined at each
151 -- point of call).
70482933
RK
152
153 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
c159409f
AC
154 -- Typ is a record or array type that is being frozen. This routine sets
155 -- the default component alignment from the scope stack values if the
156 -- alignment is otherwise not specified.
70482933
RK
157
158 procedure Check_Debug_Info_Needed (T : Entity_Id);
159 -- As each entity is frozen, this routine is called to deal with the
160 -- setting of Debug_Info_Needed for the entity. This flag is set if
161 -- the entity comes from source, or if we are in Debug_Generated_Code
162 -- mode or if the -gnatdV debug flag is set. However, it never sets
1b24ada5
RD
163 -- the flag if Debug_Info_Off is set. This procedure also ensures that
164 -- subsidiary entities have the flag set as required.
70482933 165
c6823a20 166 procedure Undelay_Type (T : Entity_Id);
c159409f
AC
167 -- T is a type of a component that we know to be an Itype. We don't want
168 -- this to have a Freeze_Node, so ensure it doesn't. Do the same for any
169 -- Full_View or Corresponding_Record_Type.
c6823a20 170
fbf5a39b
AC
171 procedure Warn_Overlay
172 (Expr : Node_Id;
173 Typ : Entity_Id;
174 Nam : Node_Id);
175 -- Expr is the expression for an address clause for entity Nam whose type
176 -- is Typ. If Typ has a default initialization, and there is no explicit
177 -- initialization in the source declaration, check whether the address
178 -- clause might cause overlaying of an entity, and emit a warning on the
179 -- side effect that the initialization will cause.
180
70482933
RK
181 -------------------------------
182 -- Adjust_Esize_For_Alignment --
183 -------------------------------
184
185 procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
186 Align : Uint;
187
188 begin
189 if Known_Esize (Typ) and then Known_Alignment (Typ) then
190 Align := Alignment_In_Bits (Typ);
191
192 if Align > Esize (Typ)
193 and then Align <= Standard_Long_Long_Integer_Size
194 then
195 Set_Esize (Typ, Align);
196 end if;
197 end if;
198 end Adjust_Esize_For_Alignment;
199
200 ------------------------------------
201 -- Build_And_Analyze_Renamed_Body --
202 ------------------------------------
203
204 procedure Build_And_Analyze_Renamed_Body
205 (Decl : Node_Id;
206 New_S : Entity_Id;
207 After : in out Node_Id)
208 is
ca0cb93e
AC
209 Body_Decl : constant Node_Id := Unit_Declaration_Node (New_S);
210 Ent : constant Entity_Id := Defining_Entity (Decl);
211 Body_Node : Node_Id;
212 Renamed_Subp : Entity_Id;
d4fc0fb4 213
70482933 214 begin
1c612f29
RD
215 -- If the renamed subprogram is intrinsic, there is no need for a
216 -- wrapper body: we set the alias that will be called and expanded which
217 -- completes the declaration. This transformation is only legal if the
218 -- renamed entity has already been elaborated.
ca0cb93e 219
d4fc0fb4
AC
220 -- Note that it is legal for a renaming_as_body to rename an intrinsic
221 -- subprogram, as long as the renaming occurs before the new entity
222 -- is frozen. See RM 8.5.4 (5).
223
224 if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
545cb5be 225 and then Is_Entity_Name (Name (Body_Decl))
d4fc0fb4 226 then
ca0cb93e
AC
227 Renamed_Subp := Entity (Name (Body_Decl));
228 else
229 Renamed_Subp := Empty;
230 end if;
231
232 if Present (Renamed_Subp)
233 and then Is_Intrinsic_Subprogram (Renamed_Subp)
ca0cb93e
AC
234 and then
235 (not In_Same_Source_Unit (Renamed_Subp, Ent)
236 or else Sloc (Renamed_Subp) < Sloc (Ent))
879e23f0 237
308e6f3a 238 -- We can make the renaming entity intrinsic if the renamed function
545cb5be
AC
239 -- has an interface name, or if it is one of the shift/rotate
240 -- operations known to the compiler.
879e23f0 241
545cb5be
AC
242 and then (Present (Interface_Name (Renamed_Subp))
243 or else Chars (Renamed_Subp) = Name_Rotate_Left
244 or else Chars (Renamed_Subp) = Name_Rotate_Right
245 or else Chars (Renamed_Subp) = Name_Shift_Left
246 or else Chars (Renamed_Subp) = Name_Shift_Right
247 or else Chars (Renamed_Subp) = Name_Shift_Right_Arithmetic)
ca0cb93e
AC
248 then
249 Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
545cb5be 250
ca0cb93e
AC
251 if Present (Alias (Renamed_Subp)) then
252 Set_Alias (Ent, Alias (Renamed_Subp));
d4fc0fb4 253 else
ca0cb93e 254 Set_Alias (Ent, Renamed_Subp);
d4fc0fb4
AC
255 end if;
256
257 Set_Is_Intrinsic_Subprogram (Ent);
258 Set_Has_Completion (Ent);
259
260 else
261 Body_Node := Build_Renamed_Body (Decl, New_S);
262 Insert_After (After, Body_Node);
263 Mark_Rewrite_Insertion (Body_Node);
264 Analyze (Body_Node);
265 After := Body_Node;
266 end if;
70482933
RK
267 end Build_And_Analyze_Renamed_Body;
268
269 ------------------------
270 -- Build_Renamed_Body --
271 ------------------------
272
273 function Build_Renamed_Body
274 (Decl : Node_Id;
fbf5a39b 275 New_S : Entity_Id) return Node_Id
70482933
RK
276 is
277 Loc : constant Source_Ptr := Sloc (New_S);
545cb5be
AC
278 -- We use for the source location of the renamed body, the location of
279 -- the spec entity. It might seem more natural to use the location of
280 -- the renaming declaration itself, but that would be wrong, since then
281 -- the body we create would look as though it was created far too late,
282 -- and this could cause problems with elaboration order analysis,
283 -- particularly in connection with instantiations.
70482933
RK
284
285 N : constant Node_Id := Unit_Declaration_Node (New_S);
286 Nam : constant Node_Id := Name (N);
287 Old_S : Entity_Id;
288 Spec : constant Node_Id := New_Copy_Tree (Specification (Decl));
289 Actuals : List_Id := No_List;
290 Call_Node : Node_Id;
291 Call_Name : Node_Id;
292 Body_Node : Node_Id;
293 Formal : Entity_Id;
294 O_Formal : Entity_Id;
295 Param_Spec : Node_Id;
296
def46b54
RD
297 Pref : Node_Id := Empty;
298 -- If the renamed entity is a primitive operation given in prefix form,
299 -- the prefix is the target object and it has to be added as the first
300 -- actual in the generated call.
301
70482933 302 begin
def46b54
RD
303 -- Determine the entity being renamed, which is the target of the call
304 -- statement. If the name is an explicit dereference, this is a renaming
305 -- of a subprogram type rather than a subprogram. The name itself is
306 -- fully analyzed.
70482933
RK
307
308 if Nkind (Nam) = N_Selected_Component then
309 Old_S := Entity (Selector_Name (Nam));
310
311 elsif Nkind (Nam) = N_Explicit_Dereference then
312 Old_S := Etype (Nam);
313
314 elsif Nkind (Nam) = N_Indexed_Component then
70482933
RK
315 if Is_Entity_Name (Prefix (Nam)) then
316 Old_S := Entity (Prefix (Nam));
317 else
318 Old_S := Entity (Selector_Name (Prefix (Nam)));
319 end if;
320
321 elsif Nkind (Nam) = N_Character_Literal then
322 Old_S := Etype (New_S);
323
324 else
325 Old_S := Entity (Nam);
326 end if;
327
328 if Is_Entity_Name (Nam) then
07fc65c4 329
def46b54
RD
330 -- If the renamed entity is a predefined operator, retain full name
331 -- to ensure its visibility.
07fc65c4
GB
332
333 if Ekind (Old_S) = E_Operator
334 and then Nkind (Nam) = N_Expanded_Name
335 then
336 Call_Name := New_Copy (Name (N));
337 else
338 Call_Name := New_Reference_To (Old_S, Loc);
339 end if;
340
70482933 341 else
def46b54
RD
342 if Nkind (Nam) = N_Selected_Component
343 and then Present (First_Formal (Old_S))
344 and then
345 (Is_Controlling_Formal (First_Formal (Old_S))
346 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
347 then
348
349 -- Retrieve the target object, to be added as a first actual
350 -- in the call.
351
352 Call_Name := New_Occurrence_Of (Old_S, Loc);
353 Pref := Prefix (Nam);
354
355 else
356 Call_Name := New_Copy (Name (N));
357 end if;
70482933 358
545cb5be 359 -- Original name may have been overloaded, but is fully resolved now
70482933
RK
360
361 Set_Is_Overloaded (Call_Name, False);
362 end if;
363
def46b54 364 -- For simple renamings, subsequent calls can be expanded directly as
d4fc0fb4 365 -- calls to the renamed entity. The body must be generated in any case
a3068ca6
AC
366 -- for calls that may appear elsewhere. This is not done in the case
367 -- where the subprogram is an instantiation because the actual proper
368 -- body has not been built yet.
70482933 369
545cb5be 370 if Ekind_In (Old_S, E_Function, E_Procedure)
70482933 371 and then Nkind (Decl) = N_Subprogram_Declaration
a3068ca6 372 and then not Is_Generic_Instance (Old_S)
70482933
RK
373 then
374 Set_Body_To_Inline (Decl, Old_S);
375 end if;
376
377 -- The body generated for this renaming is an internal artifact, and
378 -- does not constitute a freeze point for the called entity.
379
380 Set_Must_Not_Freeze (Call_Name);
381
382 Formal := First_Formal (Defining_Entity (Decl));
383
def46b54
RD
384 if Present (Pref) then
385 declare
386 Pref_Type : constant Entity_Id := Etype (Pref);
387 Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
388
389 begin
def46b54 390 -- The controlling formal may be an access parameter, or the
e14c931f 391 -- actual may be an access value, so adjust accordingly.
def46b54
RD
392
393 if Is_Access_Type (Pref_Type)
394 and then not Is_Access_Type (Form_Type)
395 then
396 Actuals := New_List
397 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
398
399 elsif Is_Access_Type (Form_Type)
400 and then not Is_Access_Type (Pref)
401 then
402 Actuals := New_List
403 (Make_Attribute_Reference (Loc,
404 Attribute_Name => Name_Access,
405 Prefix => Relocate_Node (Pref)));
406 else
407 Actuals := New_List (Pref);
408 end if;
409 end;
410
411 elsif Present (Formal) then
70482933
RK
412 Actuals := New_List;
413
def46b54
RD
414 else
415 Actuals := No_List;
416 end if;
417
418 if Present (Formal) then
70482933
RK
419 while Present (Formal) loop
420 Append (New_Reference_To (Formal, Loc), Actuals);
421 Next_Formal (Formal);
422 end loop;
423 end if;
424
def46b54
RD
425 -- If the renamed entity is an entry, inherit its profile. For other
426 -- renamings as bodies, both profiles must be subtype conformant, so it
427 -- is not necessary to replace the profile given in the declaration.
428 -- However, default values that are aggregates are rewritten when
429 -- partially analyzed, so we recover the original aggregate to insure
430 -- that subsequent conformity checking works. Similarly, if the default
431 -- expression was constant-folded, recover the original expression.
70482933
RK
432
433 Formal := First_Formal (Defining_Entity (Decl));
434
435 if Present (Formal) then
436 O_Formal := First_Formal (Old_S);
437 Param_Spec := First (Parameter_Specifications (Spec));
70482933
RK
438 while Present (Formal) loop
439 if Is_Entry (Old_S) then
70482933
RK
440 if Nkind (Parameter_Type (Param_Spec)) /=
441 N_Access_Definition
442 then
443 Set_Etype (Formal, Etype (O_Formal));
444 Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
445 end if;
446
07fc65c4
GB
447 elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
448 or else Nkind (Original_Node (Default_Value (O_Formal))) /=
449 Nkind (Default_Value (O_Formal))
450 then
70482933
RK
451 Set_Expression (Param_Spec,
452 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
453 end if;
454
455 Next_Formal (Formal);
456 Next_Formal (O_Formal);
457 Next (Param_Spec);
458 end loop;
459 end if;
460
461 -- If the renamed entity is a function, the generated body contains a
462 -- return statement. Otherwise, build a procedure call. If the entity is
463 -- an entry, subsequent analysis of the call will transform it into the
464 -- proper entry or protected operation call. If the renamed entity is
465 -- a character literal, return it directly.
466
467 if Ekind (Old_S) = E_Function
468 or else Ekind (Old_S) = E_Operator
469 or else (Ekind (Old_S) = E_Subprogram_Type
470 and then Etype (Old_S) /= Standard_Void_Type)
471 then
472 Call_Node :=
86cde7b1 473 Make_Simple_Return_Statement (Loc,
70482933
RK
474 Expression =>
475 Make_Function_Call (Loc,
476 Name => Call_Name,
477 Parameter_Associations => Actuals));
478
479 elsif Ekind (Old_S) = E_Enumeration_Literal then
480 Call_Node :=
86cde7b1 481 Make_Simple_Return_Statement (Loc,
70482933
RK
482 Expression => New_Occurrence_Of (Old_S, Loc));
483
484 elsif Nkind (Nam) = N_Character_Literal then
485 Call_Node :=
86cde7b1 486 Make_Simple_Return_Statement (Loc,
70482933
RK
487 Expression => Call_Name);
488
489 else
490 Call_Node :=
491 Make_Procedure_Call_Statement (Loc,
492 Name => Call_Name,
493 Parameter_Associations => Actuals);
494 end if;
495
49e90211 496 -- Create entities for subprogram body and formals
70482933
RK
497
498 Set_Defining_Unit_Name (Spec,
499 Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
500
501 Param_Spec := First (Parameter_Specifications (Spec));
70482933
RK
502 while Present (Param_Spec) loop
503 Set_Defining_Identifier (Param_Spec,
504 Make_Defining_Identifier (Loc,
505 Chars => Chars (Defining_Identifier (Param_Spec))));
506 Next (Param_Spec);
507 end loop;
508
509 Body_Node :=
510 Make_Subprogram_Body (Loc,
511 Specification => Spec,
512 Declarations => New_List,
513 Handled_Statement_Sequence =>
514 Make_Handled_Sequence_Of_Statements (Loc,
515 Statements => New_List (Call_Node)));
516
517 if Nkind (Decl) /= N_Subprogram_Declaration then
518 Rewrite (N,
519 Make_Subprogram_Declaration (Loc,
520 Specification => Specification (N)));
521 end if;
522
523 -- Link the body to the entity whose declaration it completes. If
def46b54
RD
524 -- the body is analyzed when the renamed entity is frozen, it may
525 -- be necessary to restore the proper scope (see package Exp_Ch13).
70482933
RK
526
527 if Nkind (N) = N_Subprogram_Renaming_Declaration
528 and then Present (Corresponding_Spec (N))
529 then
530 Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
531 else
532 Set_Corresponding_Spec (Body_Node, New_S);
533 end if;
534
535 return Body_Node;
536 end Build_Renamed_Body;
537
fbf5a39b
AC
538 --------------------------
539 -- Check_Address_Clause --
540 --------------------------
541
542 procedure Check_Address_Clause (E : Entity_Id) is
543 Addr : constant Node_Id := Address_Clause (E);
544 Expr : Node_Id;
545 Decl : constant Node_Id := Declaration_Node (E);
546 Typ : constant Entity_Id := Etype (E);
547
548 begin
549 if Present (Addr) then
550 Expr := Expression (Addr);
551
0d901290 552 if Needs_Constant_Address (Decl, Typ) then
fbf5a39b 553 Check_Constant_Address_Clause (Expr, E);
f3b57ab0
AC
554
555 -- Has_Delayed_Freeze was set on E when the address clause was
556 -- analyzed. Reset the flag now unless freeze actions were
557 -- attached to it in the mean time.
558
559 if No (Freeze_Node (E)) then
560 Set_Has_Delayed_Freeze (E, False);
561 end if;
fbf5a39b
AC
562 end if;
563
1d57c04f
AC
564 -- If Rep_Clauses are to be ignored, remove address clause from
565 -- list attached to entity, because it may be illegal for gigi,
566 -- for example by breaking order of elaboration..
567
568 if Ignore_Rep_Clauses then
569 declare
570 Rep : Node_Id;
571
572 begin
573 Rep := First_Rep_Item (E);
574
575 if Rep = Addr then
576 Set_First_Rep_Item (E, Next_Rep_Item (Addr));
577
578 else
579 while Present (Rep)
580 and then Next_Rep_Item (Rep) /= Addr
581 loop
582 Rep := Next_Rep_Item (Rep);
583 end loop;
584 end if;
585
586 if Present (Rep) then
587 Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
588 end if;
589 end;
590
591 Rewrite (Addr, Make_Null_Statement (Sloc (E)));
592
593 elsif not Error_Posted (Expr)
048e5cef 594 and then not Needs_Finalization (Typ)
fbf5a39b
AC
595 then
596 Warn_Overlay (Expr, Typ, Name (Addr));
597 end if;
598 end if;
599 end Check_Address_Clause;
600
70482933
RK
601 -----------------------------
602 -- Check_Compile_Time_Size --
603 -----------------------------
604
605 procedure Check_Compile_Time_Size (T : Entity_Id) is
606
c6823a20 607 procedure Set_Small_Size (T : Entity_Id; S : Uint);
70482933 608 -- Sets the compile time known size (32 bits or less) in the Esize
c6823a20 609 -- field, of T checking for a size clause that was given which attempts
2593c3e1 610 -- to give a smaller size, and also checking for an alignment clause.
70482933
RK
611
612 function Size_Known (T : Entity_Id) return Boolean;
07fc65c4 613 -- Recursive function that does all the work
70482933
RK
614
615 function Static_Discriminated_Components (T : Entity_Id) return Boolean;
616 -- If T is a constrained subtype, its size is not known if any of its
617 -- discriminant constraints is not static and it is not a null record.
fbf5a39b 618 -- The test is conservative and doesn't check that the components are
70482933
RK
619 -- in fact constrained by non-static discriminant values. Could be made
620 -- more precise ???
621
622 --------------------
623 -- Set_Small_Size --
624 --------------------
625
c6823a20 626 procedure Set_Small_Size (T : Entity_Id; S : Uint) is
70482933
RK
627 begin
628 if S > 32 then
629 return;
630
2593c3e1
AC
631 -- Check for bad size clause given
632
70482933
RK
633 elsif Has_Size_Clause (T) then
634 if RM_Size (T) < S then
635 Error_Msg_Uint_1 := S;
636 Error_Msg_NE
d58b9515 637 ("size for& too small, minimum allowed is ^",
70482933 638 Size_Clause (T), T);
70482933
RK
639 end if;
640
fc893455 641 -- Set size if not set already
70482933 642
fc893455
AC
643 elsif Unknown_RM_Size (T) then
644 Set_RM_Size (T, S);
70482933
RK
645 end if;
646 end Set_Small_Size;
647
648 ----------------
649 -- Size_Known --
650 ----------------
651
652 function Size_Known (T : Entity_Id) return Boolean is
653 Index : Entity_Id;
654 Comp : Entity_Id;
655 Ctyp : Entity_Id;
656 Low : Node_Id;
657 High : Node_Id;
658
659 begin
660 if Size_Known_At_Compile_Time (T) then
661 return True;
662
c6a9797e
RD
663 -- Always True for scalar types. This is true even for generic formal
664 -- scalar types. We used to return False in the latter case, but the
665 -- size is known at compile time, even in the template, we just do
666 -- not know the exact size but that's not the point of this routine.
667
70482933
RK
668 elsif Is_Scalar_Type (T)
669 or else Is_Task_Type (T)
670 then
c6a9797e
RD
671 return True;
672
673 -- Array types
70482933
RK
674
675 elsif Is_Array_Type (T) then
c6a9797e
RD
676
677 -- String literals always have known size, and we can set it
678
70482933 679 if Ekind (T) = E_String_Literal_Subtype then
c6823a20
EB
680 Set_Small_Size (T, Component_Size (T)
681 * String_Literal_Length (T));
70482933
RK
682 return True;
683
c6a9797e
RD
684 -- Unconstrained types never have known at compile time size
685
70482933
RK
686 elsif not Is_Constrained (T) then
687 return False;
688
def46b54
RD
689 -- Don't do any recursion on type with error posted, since we may
690 -- have a malformed type that leads us into a loop.
07fc65c4
GB
691
692 elsif Error_Posted (T) then
693 return False;
694
c6a9797e
RD
695 -- Otherwise if component size unknown, then array size unknown
696
70482933
RK
697 elsif not Size_Known (Component_Type (T)) then
698 return False;
699 end if;
700
def46b54
RD
701 -- Check for all indexes static, and also compute possible size
702 -- (in case it is less than 32 and may be packable).
70482933
RK
703
704 declare
705 Esiz : Uint := Component_Size (T);
706 Dim : Uint;
707
708 begin
709 Index := First_Index (T);
70482933
RK
710 while Present (Index) loop
711 if Nkind (Index) = N_Range then
712 Get_Index_Bounds (Index, Low, High);
713
714 elsif Error_Posted (Scalar_Range (Etype (Index))) then
715 return False;
716
717 else
718 Low := Type_Low_Bound (Etype (Index));
719 High := Type_High_Bound (Etype (Index));
720 end if;
721
722 if not Compile_Time_Known_Value (Low)
723 or else not Compile_Time_Known_Value (High)
724 or else Etype (Index) = Any_Type
725 then
726 return False;
727
728 else
729 Dim := Expr_Value (High) - Expr_Value (Low) + 1;
730
731 if Dim >= 0 then
732 Esiz := Esiz * Dim;
733 else
734 Esiz := Uint_0;
735 end if;
736 end if;
737
738 Next_Index (Index);
739 end loop;
740
c6823a20 741 Set_Small_Size (T, Esiz);
70482933
RK
742 return True;
743 end;
744
c6a9797e
RD
745 -- Access types always have known at compile time sizes
746
70482933
RK
747 elsif Is_Access_Type (T) then
748 return True;
749
c6a9797e
RD
750 -- For non-generic private types, go to underlying type if present
751
70482933
RK
752 elsif Is_Private_Type (T)
753 and then not Is_Generic_Type (T)
754 and then Present (Underlying_Type (T))
755 then
def46b54
RD
756 -- Don't do any recursion on type with error posted, since we may
757 -- have a malformed type that leads us into a loop.
07fc65c4
GB
758
759 if Error_Posted (T) then
760 return False;
761 else
762 return Size_Known (Underlying_Type (T));
763 end if;
70482933 764
c6a9797e
RD
765 -- Record types
766
70482933 767 elsif Is_Record_Type (T) then
fbf5a39b
AC
768
769 -- A class-wide type is never considered to have a known size
770
70482933
RK
771 if Is_Class_Wide_Type (T) then
772 return False;
773
fbf5a39b 774 -- A subtype of a variant record must not have non-static
308e6f3a 775 -- discriminated components.
fbf5a39b
AC
776
777 elsif T /= Base_Type (T)
778 and then not Static_Discriminated_Components (T)
779 then
780 return False;
70482933 781
def46b54
RD
782 -- Don't do any recursion on type with error posted, since we may
783 -- have a malformed type that leads us into a loop.
07fc65c4
GB
784
785 elsif Error_Posted (T) then
786 return False;
fbf5a39b 787 end if;
07fc65c4 788
fbf5a39b 789 -- Now look at the components of the record
70482933 790
fbf5a39b 791 declare
def46b54
RD
792 -- The following two variables are used to keep track of the
793 -- size of packed records if we can tell the size of the packed
794 -- record in the front end. Packed_Size_Known is True if so far
795 -- we can figure out the size. It is initialized to True for a
796 -- packed record, unless the record has discriminants. The
797 -- reason we eliminate the discriminated case is that we don't
798 -- know the way the back end lays out discriminated packed
799 -- records. If Packed_Size_Known is True, then Packed_Size is
800 -- the size in bits so far.
fbf5a39b
AC
801
802 Packed_Size_Known : Boolean :=
803 Is_Packed (T)
804 and then not Has_Discriminants (T);
805
806 Packed_Size : Uint := Uint_0;
807
808 begin
809 -- Test for variant part present
810
811 if Has_Discriminants (T)
812 and then Present (Parent (T))
813 and then Nkind (Parent (T)) = N_Full_Type_Declaration
814 and then Nkind (Type_Definition (Parent (T))) =
545cb5be 815 N_Record_Definition
fbf5a39b
AC
816 and then not Null_Present (Type_Definition (Parent (T)))
817 and then Present (Variant_Part
818 (Component_List (Type_Definition (Parent (T)))))
819 then
820 -- If variant part is present, and type is unconstrained,
821 -- then we must have defaulted discriminants, or a size
822 -- clause must be present for the type, or else the size
823 -- is definitely not known at compile time.
824
825 if not Is_Constrained (T)
826 and then
545cb5be 827 No (Discriminant_Default_Value (First_Discriminant (T)))
fc893455 828 and then Unknown_RM_Size (T)
70482933 829 then
fbf5a39b
AC
830 return False;
831 end if;
832 end if;
70482933 833
fbf5a39b
AC
834 -- Loop through components
835
fea9e956 836 Comp := First_Component_Or_Discriminant (T);
fbf5a39b 837 while Present (Comp) loop
fea9e956 838 Ctyp := Etype (Comp);
fbf5a39b 839
fea9e956
ES
840 -- We do not know the packed size if there is a component
841 -- clause present (we possibly could, but this would only
842 -- help in the case of a record with partial rep clauses.
843 -- That's because in the case of full rep clauses, the
844 -- size gets figured out anyway by a different circuit).
fbf5a39b 845
fea9e956
ES
846 if Present (Component_Clause (Comp)) then
847 Packed_Size_Known := False;
848 end if;
70482933 849
fea9e956
ES
850 -- We need to identify a component that is an array where
851 -- the index type is an enumeration type with non-standard
852 -- representation, and some bound of the type depends on a
853 -- discriminant.
70482933 854
fea9e956 855 -- This is because gigi computes the size by doing a
e14c931f 856 -- substitution of the appropriate discriminant value in
fea9e956
ES
857 -- the size expression for the base type, and gigi is not
858 -- clever enough to evaluate the resulting expression (which
859 -- involves a call to rep_to_pos) at compile time.
fbf5a39b 860
fea9e956
ES
861 -- It would be nice if gigi would either recognize that
862 -- this expression can be computed at compile time, or
863 -- alternatively figured out the size from the subtype
864 -- directly, where all the information is at hand ???
fbf5a39b 865
fea9e956
ES
866 if Is_Array_Type (Etype (Comp))
867 and then Present (Packed_Array_Type (Etype (Comp)))
868 then
869 declare
870 Ocomp : constant Entity_Id :=
871 Original_Record_Component (Comp);
872 OCtyp : constant Entity_Id := Etype (Ocomp);
873 Ind : Node_Id;
874 Indtyp : Entity_Id;
875 Lo, Hi : Node_Id;
70482933 876
fea9e956
ES
877 begin
878 Ind := First_Index (OCtyp);
879 while Present (Ind) loop
880 Indtyp := Etype (Ind);
70482933 881
fea9e956
ES
882 if Is_Enumeration_Type (Indtyp)
883 and then Has_Non_Standard_Rep (Indtyp)
884 then
885 Lo := Type_Low_Bound (Indtyp);
886 Hi := Type_High_Bound (Indtyp);
fbf5a39b 887
fea9e956
ES
888 if Is_Entity_Name (Lo)
889 and then Ekind (Entity (Lo)) = E_Discriminant
890 then
891 return False;
fbf5a39b 892
fea9e956
ES
893 elsif Is_Entity_Name (Hi)
894 and then Ekind (Entity (Hi)) = E_Discriminant
895 then
896 return False;
897 end if;
898 end if;
fbf5a39b 899
fea9e956
ES
900 Next_Index (Ind);
901 end loop;
902 end;
903 end if;
70482933 904
def46b54
RD
905 -- Clearly size of record is not known if the size of one of
906 -- the components is not known.
70482933 907
fea9e956
ES
908 if not Size_Known (Ctyp) then
909 return False;
910 end if;
70482933 911
fea9e956 912 -- Accumulate packed size if possible
70482933 913
fea9e956 914 if Packed_Size_Known then
70482933 915
fea9e956
ES
916 -- We can only deal with elementary types, since for
917 -- non-elementary components, alignment enters into the
918 -- picture, and we don't know enough to handle proper
919 -- alignment in this context. Packed arrays count as
920 -- elementary if the representation is a modular type.
fbf5a39b 921
fea9e956
ES
922 if Is_Elementary_Type (Ctyp)
923 or else (Is_Array_Type (Ctyp)
2593c3e1
AC
924 and then Present (Packed_Array_Type (Ctyp))
925 and then Is_Modular_Integer_Type
926 (Packed_Array_Type (Ctyp)))
fea9e956 927 then
2593c3e1
AC
928 -- If RM_Size is known and static, then we can keep
929 -- accumulating the packed size.
70482933 930
fea9e956 931 if Known_Static_RM_Size (Ctyp) then
70482933 932
fea9e956
ES
933 -- A little glitch, to be removed sometime ???
934 -- gigi does not understand zero sizes yet.
935
936 if RM_Size (Ctyp) = Uint_0 then
70482933 937 Packed_Size_Known := False;
fea9e956
ES
938
939 -- Normal case where we can keep accumulating the
940 -- packed array size.
941
942 else
943 Packed_Size := Packed_Size + RM_Size (Ctyp);
70482933 944 end if;
fbf5a39b 945
fea9e956
ES
946 -- If we have a field whose RM_Size is not known then
947 -- we can't figure out the packed size here.
fbf5a39b
AC
948
949 else
950 Packed_Size_Known := False;
70482933 951 end if;
fea9e956
ES
952
953 -- If we have a non-elementary type we can't figure out
954 -- the packed array size (alignment issues).
955
956 else
957 Packed_Size_Known := False;
70482933 958 end if;
fbf5a39b 959 end if;
70482933 960
fea9e956 961 Next_Component_Or_Discriminant (Comp);
fbf5a39b 962 end loop;
70482933 963
fbf5a39b 964 if Packed_Size_Known then
c6823a20 965 Set_Small_Size (T, Packed_Size);
fbf5a39b 966 end if;
70482933 967
fbf5a39b
AC
968 return True;
969 end;
70482933 970
c6a9797e
RD
971 -- All other cases, size not known at compile time
972
70482933
RK
973 else
974 return False;
975 end if;
976 end Size_Known;
977
978 -------------------------------------
979 -- Static_Discriminated_Components --
980 -------------------------------------
981
982 function Static_Discriminated_Components
0da2c8ac 983 (T : Entity_Id) return Boolean
70482933
RK
984 is
985 Constraint : Elmt_Id;
986
987 begin
988 if Has_Discriminants (T)
989 and then Present (Discriminant_Constraint (T))
990 and then Present (First_Component (T))
991 then
992 Constraint := First_Elmt (Discriminant_Constraint (T));
70482933
RK
993 while Present (Constraint) loop
994 if not Compile_Time_Known_Value (Node (Constraint)) then
995 return False;
996 end if;
997
998 Next_Elmt (Constraint);
999 end loop;
1000 end if;
1001
1002 return True;
1003 end Static_Discriminated_Components;
1004
1005 -- Start of processing for Check_Compile_Time_Size
1006
1007 begin
1008 Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1009 end Check_Compile_Time_Size;
1010
1011 -----------------------------
1012 -- Check_Debug_Info_Needed --
1013 -----------------------------
1014
1015 procedure Check_Debug_Info_Needed (T : Entity_Id) is
1016 begin
1b24ada5 1017 if Debug_Info_Off (T) then
70482933
RK
1018 return;
1019
1020 elsif Comes_From_Source (T)
1021 or else Debug_Generated_Code
1022 or else Debug_Flag_VV
1b24ada5 1023 or else Needs_Debug_Info (T)
70482933
RK
1024 then
1025 Set_Debug_Info_Needed (T);
1026 end if;
1027 end Check_Debug_Info_Needed;
1028
1029 ----------------------------
1030 -- Check_Strict_Alignment --
1031 ----------------------------
1032
1033 procedure Check_Strict_Alignment (E : Entity_Id) is
1034 Comp : Entity_Id;
1035
1036 begin
1037 if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1038 Set_Strict_Alignment (E);
1039
1040 elsif Is_Array_Type (E) then
1041 Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1042
1043 elsif Is_Record_Type (E) then
1044 if Is_Limited_Record (E) then
1045 Set_Strict_Alignment (E);
1046 return;
1047 end if;
1048
1049 Comp := First_Component (E);
70482933
RK
1050 while Present (Comp) loop
1051 if not Is_Type (Comp)
1052 and then (Strict_Alignment (Etype (Comp))
fbf5a39b 1053 or else Is_Aliased (Comp))
70482933
RK
1054 then
1055 Set_Strict_Alignment (E);
1056 return;
1057 end if;
1058
1059 Next_Component (Comp);
1060 end loop;
1061 end if;
1062 end Check_Strict_Alignment;
1063
1064 -------------------------
1065 -- Check_Unsigned_Type --
1066 -------------------------
1067
1068 procedure Check_Unsigned_Type (E : Entity_Id) is
1069 Ancestor : Entity_Id;
1070 Lo_Bound : Node_Id;
1071 Btyp : Entity_Id;
1072
1073 begin
1074 if not Is_Discrete_Or_Fixed_Point_Type (E) then
1075 return;
1076 end if;
1077
1078 -- Do not attempt to analyze case where range was in error
1079
199c6a10
AC
1080 if No (Scalar_Range (E))
1081 or else Error_Posted (Scalar_Range (E))
1082 then
70482933
RK
1083 return;
1084 end if;
1085
1086 -- The situation that is non trivial is something like
1087
1088 -- subtype x1 is integer range -10 .. +10;
1089 -- subtype x2 is x1 range 0 .. V1;
1090 -- subtype x3 is x2 range V2 .. V3;
1091 -- subtype x4 is x3 range V4 .. V5;
1092
1093 -- where Vn are variables. Here the base type is signed, but we still
1094 -- know that x4 is unsigned because of the lower bound of x2.
1095
1096 -- The only way to deal with this is to look up the ancestor chain
1097
1098 Ancestor := E;
1099 loop
1100 if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1101 return;
1102 end if;
1103
1104 Lo_Bound := Type_Low_Bound (Ancestor);
1105
1106 if Compile_Time_Known_Value (Lo_Bound) then
1107
1108 if Expr_Rep_Value (Lo_Bound) >= 0 then
1109 Set_Is_Unsigned_Type (E, True);
1110 end if;
1111
1112 return;
1113
1114 else
1115 Ancestor := Ancestor_Subtype (Ancestor);
1116
1117 -- If no ancestor had a static lower bound, go to base type
1118
1119 if No (Ancestor) then
1120
1121 -- Note: the reason we still check for a compile time known
1122 -- value for the base type is that at least in the case of
1123 -- generic formals, we can have bounds that fail this test,
1124 -- and there may be other cases in error situations.
1125
1126 Btyp := Base_Type (E);
1127
1128 if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1129 return;
1130 end if;
1131
1132 Lo_Bound := Type_Low_Bound (Base_Type (E));
1133
1134 if Compile_Time_Known_Value (Lo_Bound)
1135 and then Expr_Rep_Value (Lo_Bound) >= 0
1136 then
1137 Set_Is_Unsigned_Type (E, True);
1138 end if;
1139
1140 return;
70482933
RK
1141 end if;
1142 end if;
1143 end loop;
1144 end Check_Unsigned_Type;
1145
cfb120b5
AC
1146 -------------------------
1147 -- Is_Atomic_Aggregate --
1148 -------------------------
fbf5a39b 1149
cfb120b5 1150 function Is_Atomic_Aggregate
b0159fbe
AC
1151 (E : Entity_Id;
1152 Typ : Entity_Id) return Boolean
1153 is
fbf5a39b
AC
1154 Loc : constant Source_Ptr := Sloc (E);
1155 New_N : Node_Id;
b0159fbe 1156 Par : Node_Id;
fbf5a39b
AC
1157 Temp : Entity_Id;
1158
1159 begin
b0159fbe
AC
1160 Par := Parent (E);
1161
01957849 1162 -- Array may be qualified, so find outer context
b0159fbe
AC
1163
1164 if Nkind (Par) = N_Qualified_Expression then
1165 Par := Parent (Par);
1166 end if;
1167
fb2e11ee 1168 if Nkind_In (Par, N_Object_Declaration, N_Assignment_Statement)
b0159fbe 1169 and then Comes_From_Source (Par)
fbf5a39b 1170 then
b29def53 1171 Temp := Make_Temporary (Loc, 'T', E);
fbf5a39b
AC
1172 New_N :=
1173 Make_Object_Declaration (Loc,
1174 Defining_Identifier => Temp,
c6a9797e
RD
1175 Object_Definition => New_Occurrence_Of (Typ, Loc),
1176 Expression => Relocate_Node (E));
b0159fbe 1177 Insert_Before (Par, New_N);
fbf5a39b
AC
1178 Analyze (New_N);
1179
b0159fbe
AC
1180 Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1181 return True;
2c1b72d7 1182
b0159fbe
AC
1183 else
1184 return False;
fbf5a39b 1185 end if;
cfb120b5 1186 end Is_Atomic_Aggregate;
fbf5a39b 1187
70482933
RK
1188 ----------------
1189 -- Freeze_All --
1190 ----------------
1191
1192 -- Note: the easy coding for this procedure would be to just build a
1193 -- single list of freeze nodes and then insert them and analyze them
1194 -- all at once. This won't work, because the analysis of earlier freeze
1195 -- nodes may recursively freeze types which would otherwise appear later
1196 -- on in the freeze list. So we must analyze and expand the freeze nodes
1197 -- as they are generated.
1198
1199 procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
70482933
RK
1200 E : Entity_Id;
1201 Decl : Node_Id;
1202
1203 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
def46b54
RD
1204 -- This is the internal recursive routine that does freezing of entities
1205 -- (but NOT the analysis of default expressions, which should not be
1206 -- recursive, we don't want to analyze those till we are sure that ALL
1207 -- the types are frozen).
70482933 1208
fbf5a39b
AC
1209 --------------------
1210 -- Freeze_All_Ent --
1211 --------------------
1212
545cb5be 1213 procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
70482933
RK
1214 E : Entity_Id;
1215 Flist : List_Id;
1216 Lastn : Node_Id;
1217
1218 procedure Process_Flist;
def46b54
RD
1219 -- If freeze nodes are present, insert and analyze, and reset cursor
1220 -- for next insertion.
70482933 1221
fbf5a39b
AC
1222 -------------------
1223 -- Process_Flist --
1224 -------------------
1225
70482933
RK
1226 procedure Process_Flist is
1227 begin
1228 if Is_Non_Empty_List (Flist) then
1229 Lastn := Next (After);
1230 Insert_List_After_And_Analyze (After, Flist);
1231
1232 if Present (Lastn) then
1233 After := Prev (Lastn);
1234 else
1235 After := Last (List_Containing (After));
1236 end if;
1237 end if;
1238 end Process_Flist;
1239
fbf5a39b
AC
1240 -- Start or processing for Freeze_All_Ent
1241
70482933
RK
1242 begin
1243 E := From;
1244 while Present (E) loop
1245
1246 -- If the entity is an inner package which is not a package
def46b54
RD
1247 -- renaming, then its entities must be frozen at this point. Note
1248 -- that such entities do NOT get frozen at the end of the nested
1249 -- package itself (only library packages freeze).
70482933
RK
1250
1251 -- Same is true for task declarations, where anonymous records
1252 -- created for entry parameters must be frozen.
1253
1254 if Ekind (E) = E_Package
1255 and then No (Renamed_Object (E))
1256 and then not Is_Child_Unit (E)
1257 and then not Is_Frozen (E)
1258 then
7d8b9c99 1259 Push_Scope (E);
70482933
RK
1260 Install_Visible_Declarations (E);
1261 Install_Private_Declarations (E);
1262
1263 Freeze_All (First_Entity (E), After);
1264
1265 End_Package_Scope (E);
1266
d3cb4cc0
AC
1267 if Is_Generic_Instance (E)
1268 and then Has_Delayed_Freeze (E)
1269 then
1270 Set_Has_Delayed_Freeze (E, False);
1271 Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1272 end if;
1273
70482933
RK
1274 elsif Ekind (E) in Task_Kind
1275 and then
1276 (Nkind (Parent (E)) = N_Task_Type_Declaration
fbf5a39b 1277 or else
70482933
RK
1278 Nkind (Parent (E)) = N_Single_Task_Declaration)
1279 then
7d8b9c99 1280 Push_Scope (E);
70482933
RK
1281 Freeze_All (First_Entity (E), After);
1282 End_Scope;
1283
1284 -- For a derived tagged type, we must ensure that all the
def46b54
RD
1285 -- primitive operations of the parent have been frozen, so that
1286 -- their addresses will be in the parent's dispatch table at the
1287 -- point it is inherited.
70482933
RK
1288
1289 elsif Ekind (E) = E_Record_Type
1290 and then Is_Tagged_Type (E)
1291 and then Is_Tagged_Type (Etype (E))
1292 and then Is_Derived_Type (E)
1293 then
1294 declare
1295 Prim_List : constant Elist_Id :=
1296 Primitive_Operations (Etype (E));
fbf5a39b
AC
1297
1298 Prim : Elmt_Id;
1299 Subp : Entity_Id;
70482933
RK
1300
1301 begin
df3e68b1 1302 Prim := First_Elmt (Prim_List);
70482933
RK
1303 while Present (Prim) loop
1304 Subp := Node (Prim);
1305
1306 if Comes_From_Source (Subp)
1307 and then not Is_Frozen (Subp)
1308 then
c159409f 1309 Flist := Freeze_Entity (Subp, After);
70482933
RK
1310 Process_Flist;
1311 end if;
1312
1313 Next_Elmt (Prim);
1314 end loop;
1315 end;
1316 end if;
1317
1318 if not Is_Frozen (E) then
c159409f 1319 Flist := Freeze_Entity (E, After);
70482933 1320 Process_Flist;
47e11d08
AC
1321
1322 -- If already frozen, and there are delayed aspects, this is where
1323 -- we do the visibility check for these aspects (see Sem_Ch13 spec
1324 -- for a description of how we handle aspect visibility).
1325
1326 elsif Has_Delayed_Aspects (E) then
02e4edea 1327
b98e2969
AC
1328 -- Retrieve the visibility to the discriminants in order to
1329 -- analyze properly the aspects.
1330
1331 Push_Scope_And_Install_Discriminants (E);
1332
47e11d08
AC
1333 declare
1334 Ritem : Node_Id;
1335
1336 begin
1337 Ritem := First_Rep_Item (E);
1338 while Present (Ritem) loop
1339 if Nkind (Ritem) = N_Aspect_Specification
bd949ee2 1340 and then Entity (Ritem) = E
47e11d08
AC
1341 and then Is_Delayed_Aspect (Ritem)
1342 then
1343 Check_Aspect_At_End_Of_Declarations (Ritem);
1344 end if;
1345
1346 Ritem := Next_Rep_Item (Ritem);
1347 end loop;
1348 end;
b98e2969
AC
1349
1350 Uninstall_Discriminants_And_Pop_Scope (E);
70482933
RK
1351 end if;
1352
def46b54
RD
1353 -- If an incomplete type is still not frozen, this may be a
1354 -- premature freezing because of a body declaration that follows.
ef992452
AC
1355 -- Indicate where the freezing took place. Freezing will happen
1356 -- if the body comes from source, but not if it is internally
1357 -- generated, for example as the body of a type invariant.
fbf5a39b 1358
def46b54
RD
1359 -- If the freezing is caused by the end of the current declarative
1360 -- part, it is a Taft Amendment type, and there is no error.
fbf5a39b
AC
1361
1362 if not Is_Frozen (E)
1363 and then Ekind (E) = E_Incomplete_Type
1364 then
1365 declare
1366 Bod : constant Node_Id := Next (After);
1367
1368 begin
35fae080
RD
1369 -- The presence of a body freezes all entities previously
1370 -- declared in the current list of declarations, but this
1371 -- does not apply if the body does not come from source.
1372 -- A type invariant is transformed into a subprogram body
1373 -- which is placed at the end of the private part of the
1374 -- current package, but this body does not freeze incomplete
1375 -- types that may be declared in this private part.
1376
545cb5be
AC
1377 if (Nkind_In (Bod, N_Subprogram_Body,
1378 N_Entry_Body,
1379 N_Package_Body,
1380 N_Protected_Body,
1381 N_Task_Body)
fbf5a39b 1382 or else Nkind (Bod) in N_Body_Stub)
ef992452 1383 and then
35fae080 1384 List_Containing (After) = List_Containing (Parent (E))
ef992452 1385 and then Comes_From_Source (Bod)
fbf5a39b
AC
1386 then
1387 Error_Msg_Sloc := Sloc (Next (After));
1388 Error_Msg_NE
1389 ("type& is frozen# before its full declaration",
1390 Parent (E), E);
1391 end if;
1392 end;
1393 end if;
1394
70482933
RK
1395 Next_Entity (E);
1396 end loop;
1397 end Freeze_All_Ent;
1398
1399 -- Start of processing for Freeze_All
1400
1401 begin
1402 Freeze_All_Ent (From, After);
1403
1404 -- Now that all types are frozen, we can deal with default expressions
1405 -- that require us to build a default expression functions. This is the
1406 -- point at which such functions are constructed (after all types that
1407 -- might be used in such expressions have been frozen).
fbf5a39b 1408
d4fc0fb4
AC
1409 -- For subprograms that are renaming_as_body, we create the wrapper
1410 -- bodies as needed.
1411
70482933
RK
1412 -- We also add finalization chains to access types whose designated
1413 -- types are controlled. This is normally done when freezing the type,
1414 -- but this misses recursive type definitions where the later members
c6a9797e 1415 -- of the recursion introduce controlled components.
70482933
RK
1416
1417 -- Loop through entities
1418
1419 E := From;
1420 while Present (E) loop
70482933
RK
1421 if Is_Subprogram (E) then
1422
1423 if not Default_Expressions_Processed (E) then
1424 Process_Default_Expressions (E, After);
1425 end if;
1426
1427 if not Has_Completion (E) then
1428 Decl := Unit_Declaration_Node (E);
1429
1430 if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
8417f4b2
AC
1431 if Error_Posted (Decl) then
1432 Set_Has_Completion (E);
8417f4b2
AC
1433 else
1434 Build_And_Analyze_Renamed_Body (Decl, E, After);
1435 end if;
70482933
RK
1436
1437 elsif Nkind (Decl) = N_Subprogram_Declaration
1438 and then Present (Corresponding_Body (Decl))
1439 and then
1440 Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
fbf5a39b 1441 = N_Subprogram_Renaming_Declaration
70482933
RK
1442 then
1443 Build_And_Analyze_Renamed_Body
1444 (Decl, Corresponding_Body (Decl), After);
1445 end if;
1446 end if;
1447
1448 elsif Ekind (E) in Task_Kind
1449 and then
1450 (Nkind (Parent (E)) = N_Task_Type_Declaration
fbf5a39b 1451 or else
70482933
RK
1452 Nkind (Parent (E)) = N_Single_Task_Declaration)
1453 then
1454 declare
1455 Ent : Entity_Id;
545cb5be 1456
70482933
RK
1457 begin
1458 Ent := First_Entity (E);
70482933 1459 while Present (Ent) loop
70482933
RK
1460 if Is_Entry (Ent)
1461 and then not Default_Expressions_Processed (Ent)
1462 then
1463 Process_Default_Expressions (Ent, After);
1464 end if;
1465
1466 Next_Entity (Ent);
1467 end loop;
1468 end;
1469
ca5af305
AC
1470 -- We add finalization masters to access types whose designated types
1471 -- require finalization. This is normally done when freezing the
1472 -- type, but this misses recursive type definitions where the later
1473 -- members of the recursion introduce controlled components (such as
1474 -- can happen when incomplete types are involved), as well cases
1475 -- where a component type is private and the controlled full type
1476 -- occurs after the access type is frozen. Cases that don't need a
1477 -- finalization master are generic formal types (the actual type will
1478 -- have it) and types with Java and CIL conventions, since those are
1479 -- used for API bindings. (Are there any other cases that should be
1480 -- excluded here???)
df3e68b1 1481
70482933
RK
1482 elsif Is_Access_Type (E)
1483 and then Comes_From_Source (E)
df3e68b1 1484 and then not Is_Generic_Type (E)
048e5cef 1485 and then Needs_Finalization (Designated_Type (E))
70482933 1486 then
ca5af305 1487 Build_Finalization_Master (E);
70482933
RK
1488 end if;
1489
1490 Next_Entity (E);
1491 end loop;
70482933
RK
1492 end Freeze_All;
1493
1494 -----------------------
1495 -- Freeze_And_Append --
1496 -----------------------
1497
1498 procedure Freeze_And_Append
1499 (Ent : Entity_Id;
c159409f 1500 N : Node_Id;
70482933
RK
1501 Result : in out List_Id)
1502 is
c159409f 1503 L : constant List_Id := Freeze_Entity (Ent, N);
70482933
RK
1504 begin
1505 if Is_Non_Empty_List (L) then
1506 if Result = No_List then
1507 Result := L;
1508 else
1509 Append_List (L, Result);
1510 end if;
1511 end if;
1512 end Freeze_And_Append;
1513
1514 -------------------
1515 -- Freeze_Before --
1516 -------------------
1517
1518 procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
c159409f 1519 Freeze_Nodes : constant List_Id := Freeze_Entity (T, N);
70482933
RK
1520 begin
1521 if Is_Non_Empty_List (Freeze_Nodes) then
fbf5a39b 1522 Insert_Actions (N, Freeze_Nodes);
70482933
RK
1523 end if;
1524 end Freeze_Before;
1525
1526 -------------------
1527 -- Freeze_Entity --
1528 -------------------
1529
c159409f
AC
1530 function Freeze_Entity (E : Entity_Id; N : Node_Id) return List_Id is
1531 Loc : constant Source_Ptr := Sloc (N);
c6823a20 1532 Test_E : Entity_Id := E;
70482933
RK
1533 Comp : Entity_Id;
1534 F_Node : Node_Id;
70482933
RK
1535 Indx : Node_Id;
1536 Formal : Entity_Id;
1537 Atype : Entity_Id;
1538
90878b12
AC
1539 Result : List_Id := No_List;
1540 -- List of freezing actions, left at No_List if none
1541
4c8a5bb8
AC
1542 Has_Default_Initialization : Boolean := False;
1543 -- This flag gets set to true for a variable with default initialization
1544
90878b12
AC
1545 procedure Add_To_Result (N : Node_Id);
1546 -- N is a freezing action to be appended to the Result
1547
b98e2969
AC
1548 function After_Last_Declaration return Boolean;
1549 -- If Loc is a freeze_entity that appears after the last declaration
1550 -- in the scope, inhibit error messages on late completion.
1551
70482933 1552 procedure Check_Current_Instance (Comp_Decl : Node_Id);
edd63e9b
ES
1553 -- Check that an Access or Unchecked_Access attribute with a prefix
1554 -- which is the current instance type can only be applied when the type
1555 -- is limited.
70482933 1556
67b3acf8
RD
1557 procedure Check_Suspicious_Modulus (Utype : Entity_Id);
1558 -- Give warning for modulus of 8, 16, 32, or 64 given as an explicit
1559 -- integer literal without an explicit corresponding size clause. The
1560 -- caller has checked that Utype is a modular integer type.
1561
70482933 1562 procedure Freeze_Record_Type (Rec : Entity_Id);
edd63e9b
ES
1563 -- Freeze each component, handle some representation clauses, and freeze
1564 -- primitive operations if this is a tagged type.
70482933 1565
90878b12
AC
1566 -------------------
1567 -- Add_To_Result --
1568 -------------------
1569
1570 procedure Add_To_Result (N : Node_Id) is
1571 begin
1572 if No (Result) then
1573 Result := New_List (N);
1574 else
1575 Append (N, Result);
1576 end if;
1577 end Add_To_Result;
1578
70482933
RK
1579 ----------------------------
1580 -- After_Last_Declaration --
1581 ----------------------------
1582
1583 function After_Last_Declaration return Boolean is
fb2e11ee 1584 Spec : constant Node_Id := Parent (Current_Scope);
70482933
RK
1585 begin
1586 if Nkind (Spec) = N_Package_Specification then
1587 if Present (Private_Declarations (Spec)) then
1588 return Loc >= Sloc (Last (Private_Declarations (Spec)));
70482933
RK
1589 elsif Present (Visible_Declarations (Spec)) then
1590 return Loc >= Sloc (Last (Visible_Declarations (Spec)));
1591 else
1592 return False;
1593 end if;
70482933
RK
1594 else
1595 return False;
1596 end if;
1597 end After_Last_Declaration;
1598
1599 ----------------------------
1600 -- Check_Current_Instance --
1601 ----------------------------
1602
1603 procedure Check_Current_Instance (Comp_Decl : Node_Id) is
1604
e1308fa8
AC
1605 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
1606 -- Determine whether Typ is compatible with the rules for aliased
1607 -- views of types as defined in RM 3.10 in the various dialects.
32c760e6 1608
70482933 1609 function Process (N : Node_Id) return Traverse_Result;
49e90211 1610 -- Process routine to apply check to given node
70482933 1611
e1308fa8
AC
1612 -----------------------------
1613 -- Is_Aliased_View_Of_Type --
1614 -----------------------------
1615
1616 function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
1617 Typ_Decl : constant Node_Id := Parent (Typ);
1618
1619 begin
1620 -- Common case
1621
1622 if Nkind (Typ_Decl) = N_Full_Type_Declaration
1623 and then Limited_Present (Type_Definition (Typ_Decl))
1624 then
1625 return True;
1626
1627 -- The following paragraphs describe what a legal aliased view of
1628 -- a type is in the various dialects of Ada.
1629
1630 -- Ada 95
1631
1632 -- The current instance of a limited type, and a formal parameter
1633 -- or generic formal object of a tagged type.
1634
1635 -- Ada 95 limited type
1636 -- * Type with reserved word "limited"
1637 -- * A protected or task type
1638 -- * A composite type with limited component
1639
1640 elsif Ada_Version <= Ada_95 then
1641 return Is_Limited_Type (Typ);
1642
1643 -- Ada 2005
1644
1645 -- The current instance of a limited tagged type, a protected
1646 -- type, a task type, or a type that has the reserved word
1647 -- "limited" in its full definition ... a formal parameter or
1648 -- generic formal object of a tagged type.
1649
1650 -- Ada 2005 limited type
1651 -- * Type with reserved word "limited", "synchronized", "task"
1652 -- or "protected"
1653 -- * A composite type with limited component
1654 -- * A derived type whose parent is a non-interface limited type
1655
1656 elsif Ada_Version = Ada_2005 then
1657 return
1658 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
1659 or else
1660 (Is_Derived_Type (Typ)
1661 and then not Is_Interface (Etype (Typ))
1662 and then Is_Limited_Type (Etype (Typ)));
1663
1664 -- Ada 2012 and beyond
1665
1666 -- The current instance of an immutably limited type ... a formal
1667 -- parameter or generic formal object of a tagged type.
1668
1669 -- Ada 2012 limited type
1670 -- * Type with reserved word "limited", "synchronized", "task"
1671 -- or "protected"
1672 -- * A composite type with limited component
1673 -- * A derived type whose parent is a non-interface limited type
1674 -- * An incomplete view
1675
1676 -- Ada 2012 immutably limited type
1677 -- * Explicitly limited record type
1678 -- * Record extension with "limited" present
1679 -- * Non-formal limited private type that is either tagged
1680 -- or has at least one access discriminant with a default
1681 -- expression
1682 -- * Task type, protected type or synchronized interface
1683 -- * Type derived from immutably limited type
1684
1685 else
1686 return
1687 Is_Immutably_Limited_Type (Typ)
1688 or else Is_Incomplete_Type (Typ);
1689 end if;
1690 end Is_Aliased_View_Of_Type;
1691
fbf5a39b
AC
1692 -------------
1693 -- Process --
1694 -------------
1695
70482933
RK
1696 function Process (N : Node_Id) return Traverse_Result is
1697 begin
1698 case Nkind (N) is
1699 when N_Attribute_Reference =>
def46b54 1700 if (Attribute_Name (N) = Name_Access
70482933
RK
1701 or else
1702 Attribute_Name (N) = Name_Unchecked_Access)
1703 and then Is_Entity_Name (Prefix (N))
1704 and then Is_Type (Entity (Prefix (N)))
1705 and then Entity (Prefix (N)) = E
1706 then
1707 Error_Msg_N
1708 ("current instance must be a limited type", Prefix (N));
1709 return Abandon;
1710 else
1711 return OK;
1712 end if;
1713
1714 when others => return OK;
1715 end case;
1716 end Process;
1717
1718 procedure Traverse is new Traverse_Proc (Process);
1719
e1308fa8 1720 -- Local variables
32c760e6 1721
e1308fa8
AC
1722 Rec_Type : constant Entity_Id :=
1723 Scope (Defining_Identifier (Comp_Decl));
32c760e6 1724
e1308fa8 1725 -- Start of processing for Check_Current_Instance
32c760e6 1726
e1308fa8
AC
1727 begin
1728 if not Is_Aliased_View_Of_Type (Rec_Type) then
32c760e6
ES
1729 Traverse (Comp_Decl);
1730 end if;
70482933
RK
1731 end Check_Current_Instance;
1732
67b3acf8
RD
1733 ------------------------------
1734 -- Check_Suspicious_Modulus --
1735 ------------------------------
1736
1737 procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
1738 Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
1739
1740 begin
1741 if Nkind (Decl) = N_Full_Type_Declaration then
1742 declare
1743 Tdef : constant Node_Id := Type_Definition (Decl);
3e7302c3 1744
67b3acf8
RD
1745 begin
1746 if Nkind (Tdef) = N_Modular_Type_Definition then
1747 declare
1748 Modulus : constant Node_Id :=
1749 Original_Node (Expression (Tdef));
1750 begin
1751 if Nkind (Modulus) = N_Integer_Literal then
1752 declare
1753 Modv : constant Uint := Intval (Modulus);
1754 Sizv : constant Uint := RM_Size (Utype);
1755
1756 begin
1757 -- First case, modulus and size are the same. This
1758 -- happens if you have something like mod 32, with
1759 -- an explicit size of 32, this is for sure a case
1760 -- where the warning is given, since it is seems
1761 -- very unlikely that someone would want e.g. a
1762 -- five bit type stored in 32 bits. It is much
1763 -- more likely they wanted a 32-bit type.
1764
1765 if Modv = Sizv then
1766 null;
1767
1768 -- Second case, the modulus is 32 or 64 and no
1769 -- size clause is present. This is a less clear
1770 -- case for giving the warning, but in the case
1771 -- of 32/64 (5-bit or 6-bit types) these seem rare
1772 -- enough that it is a likely error (and in any
1773 -- case using 2**5 or 2**6 in these cases seems
1774 -- clearer. We don't include 8 or 16 here, simply
1775 -- because in practice 3-bit and 4-bit types are
1776 -- more common and too many false positives if
1777 -- we warn in these cases.
1778
1779 elsif not Has_Size_Clause (Utype)
1780 and then (Modv = Uint_32 or else Modv = Uint_64)
1781 then
1782 null;
1783
1784 -- No warning needed
1785
1786 else
1787 return;
1788 end if;
1789
1790 -- If we fall through, give warning
1791
1792 Error_Msg_Uint_1 := Modv;
1793 Error_Msg_N
1794 ("?2 '*'*^' may have been intended here",
1795 Modulus);
1796 end;
1797 end if;
1798 end;
1799 end if;
1800 end;
1801 end if;
1802 end Check_Suspicious_Modulus;
1803
70482933
RK
1804 ------------------------
1805 -- Freeze_Record_Type --
1806 ------------------------
1807
1808 procedure Freeze_Record_Type (Rec : Entity_Id) is
1809 Comp : Entity_Id;
fbf5a39b 1810 IR : Node_Id;
70482933 1811 ADC : Node_Id;
c6823a20 1812 Prev : Entity_Id;
70482933 1813
67ce0d7e
RD
1814 Junk : Boolean;
1815 pragma Warnings (Off, Junk);
1816
22a83cea
AC
1817 Rec_Pushed : Boolean := False;
1818 -- Set True if the record type scope Rec has been pushed on the scope
1819 -- stack. Needed for the analysis of delayed aspects specified to the
1820 -- components of Rec.
1821
70482933
RK
1822 Unplaced_Component : Boolean := False;
1823 -- Set True if we find at least one component with no component
1824 -- clause (used to warn about useless Pack pragmas).
1825
1826 Placed_Component : Boolean := False;
1827 -- Set True if we find at least one component with a component
8dc10d38
AC
1828 -- clause (used to warn about useless Bit_Order pragmas, and also
1829 -- to detect cases where Implicit_Packing may have an effect).
1830
1831 All_Scalar_Components : Boolean := True;
1832 -- Set False if we encounter a component of a non-scalar type
1833
1834 Scalar_Component_Total_RM_Size : Uint := Uint_0;
1835 Scalar_Component_Total_Esize : Uint := Uint_0;
1836 -- Accumulates total RM_Size values and total Esize values of all
1837 -- scalar components. Used for processing of Implicit_Packing.
70482933 1838
e18d6a15
JM
1839 function Check_Allocator (N : Node_Id) return Node_Id;
1840 -- If N is an allocator, possibly wrapped in one or more level of
1841 -- qualified expression(s), return the inner allocator node, else
1842 -- return Empty.
19590d70 1843
7d8b9c99
RD
1844 procedure Check_Itype (Typ : Entity_Id);
1845 -- If the component subtype is an access to a constrained subtype of
1846 -- an already frozen type, make the subtype frozen as well. It might
1847 -- otherwise be frozen in the wrong scope, and a freeze node on
1848 -- subtype has no effect. Similarly, if the component subtype is a
1849 -- regular (not protected) access to subprogram, set the anonymous
1850 -- subprogram type to frozen as well, to prevent an out-of-scope
1851 -- freeze node at some eventual point of call. Protected operations
1852 -- are handled elsewhere.
6e059adb 1853
19590d70
GD
1854 ---------------------
1855 -- Check_Allocator --
1856 ---------------------
1857
e18d6a15
JM
1858 function Check_Allocator (N : Node_Id) return Node_Id is
1859 Inner : Node_Id;
19590d70 1860 begin
e18d6a15 1861 Inner := N;
e18d6a15
JM
1862 loop
1863 if Nkind (Inner) = N_Allocator then
1864 return Inner;
e18d6a15
JM
1865 elsif Nkind (Inner) = N_Qualified_Expression then
1866 Inner := Expression (Inner);
e18d6a15
JM
1867 else
1868 return Empty;
1869 end if;
1870 end loop;
19590d70
GD
1871 end Check_Allocator;
1872
6871ba5f
AC
1873 -----------------
1874 -- Check_Itype --
1875 -----------------
1876
7d8b9c99
RD
1877 procedure Check_Itype (Typ : Entity_Id) is
1878 Desig : constant Entity_Id := Designated_Type (Typ);
1879
6e059adb
AC
1880 begin
1881 if not Is_Frozen (Desig)
1882 and then Is_Frozen (Base_Type (Desig))
1883 then
1884 Set_Is_Frozen (Desig);
1885
1886 -- In addition, add an Itype_Reference to ensure that the
7d8b9c99
RD
1887 -- access subtype is elaborated early enough. This cannot be
1888 -- done if the subtype may depend on discriminants.
6e059adb
AC
1889
1890 if Ekind (Comp) = E_Component
1891 and then Is_Itype (Etype (Comp))
1892 and then not Has_Discriminants (Rec)
1893 then
1894 IR := Make_Itype_Reference (Sloc (Comp));
1895 Set_Itype (IR, Desig);
90878b12 1896 Add_To_Result (IR);
6e059adb 1897 end if;
7d8b9c99
RD
1898
1899 elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
1900 and then Convention (Desig) /= Convention_Protected
1901 then
1902 Set_Is_Frozen (Desig);
6e059adb
AC
1903 end if;
1904 end Check_Itype;
1905
1906 -- Start of processing for Freeze_Record_Type
1907
70482933 1908 begin
22a83cea
AC
1909 -- Deal with delayed aspect specifications for components. The
1910 -- analysis of the aspect is required to be delayed to the freeze
d27f3ff4
AC
1911 -- point, thus we analyze the pragma or attribute definition
1912 -- clause in the tree at this point. We also analyze the aspect
22a83cea
AC
1913 -- specification node at the freeze point when the aspect doesn't
1914 -- correspond to pragma/attribute definition clause.
70482933
RK
1915
1916 Comp := First_Entity (Rec);
c6823a20 1917 while Present (Comp) loop
b3f532ce 1918 if Ekind (Comp) = E_Component
b7f7dab2 1919 and then Has_Delayed_Aspects (Comp)
b3f532ce 1920 then
22a83cea
AC
1921 if not Rec_Pushed then
1922 Push_Scope (Rec);
1923 Rec_Pushed := True;
b3f532ce 1924
22a83cea
AC
1925 -- The visibility to the discriminants must be restored in
1926 -- order to properly analyze the aspects.
b3f532ce 1927
22a83cea
AC
1928 if Has_Discriminants (Rec) then
1929 Install_Discriminants (Rec);
1930 end if;
b3f532ce
AC
1931 end if;
1932
22a83cea 1933 Analyze_Aspects_At_Freeze_Point (Comp);
b3f532ce
AC
1934 end if;
1935
22a83cea
AC
1936 Next_Entity (Comp);
1937 end loop;
1938
1939 -- Pop the scope if Rec scope has been pushed on the scope stack
1940 -- during the delayed aspect analysis process.
1941
1942 if Rec_Pushed then
1943 if Has_Discriminants (Rec) then
1944 Uninstall_Discriminants (Rec);
1945 end if;
1946
1947 Pop_Scope;
1948 end if;
1949
1950 -- Freeze components and embedded subtypes
1951
1952 Comp := First_Entity (Rec);
1953 Prev := Empty;
1954 while Present (Comp) loop
1955
b3f532ce 1956 -- Handle the component and discriminant case
70482933 1957
d27f3ff4 1958 if Ekind_In (Comp, E_Component, E_Discriminant) then
70482933
RK
1959 declare
1960 CC : constant Node_Id := Component_Clause (Comp);
1961
1962 begin
c6823a20
EB
1963 -- Freezing a record type freezes the type of each of its
1964 -- components. However, if the type of the component is
1965 -- part of this record, we do not want or need a separate
1966 -- Freeze_Node. Note that Is_Itype is wrong because that's
1967 -- also set in private type cases. We also can't check for
1968 -- the Scope being exactly Rec because of private types and
1969 -- record extensions.
1970
1971 if Is_Itype (Etype (Comp))
1972 and then Is_Record_Type (Underlying_Type
1973 (Scope (Etype (Comp))))
1974 then
1975 Undelay_Type (Etype (Comp));
1976 end if;
1977
c159409f 1978 Freeze_And_Append (Etype (Comp), N, Result);
c6823a20 1979
0da2c8ac
AC
1980 -- Check for error of component clause given for variable
1981 -- sized type. We have to delay this test till this point,
1982 -- since the component type has to be frozen for us to know
1983 -- if it is variable length. We omit this test in a generic
1984 -- context, it will be applied at instantiation time.
579fda56 1985
24c34107
AC
1986 -- We also omit this test in CodePeer mode, since we do not
1987 -- have sufficient info on size and representation clauses.
0da2c8ac 1988
70482933
RK
1989 if Present (CC) then
1990 Placed_Component := True;
1991
07fc65c4
GB
1992 if Inside_A_Generic then
1993 null;
1994
24c34107
AC
1995 elsif CodePeer_Mode then
1996 null;
1997
7d8b9c99
RD
1998 elsif not
1999 Size_Known_At_Compile_Time
2000 (Underlying_Type (Etype (Comp)))
70482933
RK
2001 then
2002 Error_Msg_N
2003 ("component clause not allowed for variable " &
2004 "length component", CC);
2005 end if;
2006
2007 else
2008 Unplaced_Component := True;
2009 end if;
70482933 2010
0da2c8ac 2011 -- Case of component requires byte alignment
70482933 2012
0da2c8ac 2013 if Must_Be_On_Byte_Boundary (Etype (Comp)) then
70482933 2014
0da2c8ac 2015 -- Set the enclosing record to also require byte align
70482933 2016
0da2c8ac 2017 Set_Must_Be_On_Byte_Boundary (Rec);
70482933 2018
7d8b9c99
RD
2019 -- Check for component clause that is inconsistent with
2020 -- the required byte boundary alignment.
70482933 2021
0da2c8ac
AC
2022 if Present (CC)
2023 and then Normalized_First_Bit (Comp) mod
2024 System_Storage_Unit /= 0
2025 then
2026 Error_Msg_N
2027 ("component & must be byte aligned",
2028 Component_Name (Component_Clause (Comp)));
2029 end if;
2030 end if;
0da2c8ac 2031 end;
70482933
RK
2032 end if;
2033
8a95f4e8
RD
2034 -- Gather data for possible Implicit_Packing later. Note that at
2035 -- this stage we might be dealing with a real component, or with
2036 -- an implicit subtype declaration.
8dc10d38 2037
426d2717
AC
2038 if not Is_Scalar_Type (Etype (Comp)) then
2039 All_Scalar_Components := False;
2040 else
2041 Scalar_Component_Total_RM_Size :=
2042 Scalar_Component_Total_RM_Size + RM_Size (Etype (Comp));
2043 Scalar_Component_Total_Esize :=
2044 Scalar_Component_Total_Esize + Esize (Etype (Comp));
8dc10d38
AC
2045 end if;
2046
c6823a20
EB
2047 -- If the component is an Itype with Delayed_Freeze and is either
2048 -- a record or array subtype and its base type has not yet been
545cb5be
AC
2049 -- frozen, we must remove this from the entity list of this record
2050 -- and put it on the entity list of the scope of its base type.
2051 -- Note that we know that this is not the type of a component
2052 -- since we cleared Has_Delayed_Freeze for it in the previous
2053 -- loop. Thus this must be the Designated_Type of an access type,
2054 -- which is the type of a component.
c6823a20
EB
2055
2056 if Is_Itype (Comp)
2057 and then Is_Type (Scope (Comp))
2058 and then Is_Composite_Type (Comp)
2059 and then Base_Type (Comp) /= Comp
2060 and then Has_Delayed_Freeze (Comp)
2061 and then not Is_Frozen (Base_Type (Comp))
2062 then
2063 declare
2064 Will_Be_Frozen : Boolean := False;
1b24ada5 2065 S : Entity_Id;
c6823a20
EB
2066
2067 begin
fea9e956
ES
2068 -- We have a pretty bad kludge here. Suppose Rec is subtype
2069 -- being defined in a subprogram that's created as part of
2070 -- the freezing of Rec'Base. In that case, we know that
2071 -- Comp'Base must have already been frozen by the time we
2072 -- get to elaborate this because Gigi doesn't elaborate any
2073 -- bodies until it has elaborated all of the declarative
2074 -- part. But Is_Frozen will not be set at this point because
2075 -- we are processing code in lexical order.
2076
2077 -- We detect this case by going up the Scope chain of Rec
2078 -- and seeing if we have a subprogram scope before reaching
2079 -- the top of the scope chain or that of Comp'Base. If we
2080 -- do, then mark that Comp'Base will actually be frozen. If
2081 -- so, we merely undelay it.
c6823a20 2082
1b24ada5 2083 S := Scope (Rec);
c6823a20
EB
2084 while Present (S) loop
2085 if Is_Subprogram (S) then
2086 Will_Be_Frozen := True;
2087 exit;
2088 elsif S = Scope (Base_Type (Comp)) then
2089 exit;
2090 end if;
2091
2092 S := Scope (S);
2093 end loop;
2094
2095 if Will_Be_Frozen then
2096 Undelay_Type (Comp);
2097 else
2098 if Present (Prev) then
2099 Set_Next_Entity (Prev, Next_Entity (Comp));
2100 else
2101 Set_First_Entity (Rec, Next_Entity (Comp));
2102 end if;
2103
2104 -- Insert in entity list of scope of base type (which
2105 -- must be an enclosing scope, because still unfrozen).
2106
2107 Append_Entity (Comp, Scope (Base_Type (Comp)));
2108 end if;
2109 end;
2110
def46b54
RD
2111 -- If the component is an access type with an allocator as default
2112 -- value, the designated type will be frozen by the corresponding
2113 -- expression in init_proc. In order to place the freeze node for
2114 -- the designated type before that for the current record type,
2115 -- freeze it now.
c6823a20
EB
2116
2117 -- Same process if the component is an array of access types,
2118 -- initialized with an aggregate. If the designated type is
def46b54
RD
2119 -- private, it cannot contain allocators, and it is premature
2120 -- to freeze the type, so we check for this as well.
c6823a20
EB
2121
2122 elsif Is_Access_Type (Etype (Comp))
2123 and then Present (Parent (Comp))
2124 and then Present (Expression (Parent (Comp)))
c6823a20
EB
2125 then
2126 declare
e18d6a15
JM
2127 Alloc : constant Node_Id :=
2128 Check_Allocator (Expression (Parent (Comp)));
c6823a20
EB
2129
2130 begin
e18d6a15 2131 if Present (Alloc) then
19590d70 2132
e18d6a15
JM
2133 -- If component is pointer to a classwide type, freeze
2134 -- the specific type in the expression being allocated.
2135 -- The expression may be a subtype indication, in which
2136 -- case freeze the subtype mark.
c6823a20 2137
e18d6a15
JM
2138 if Is_Class_Wide_Type
2139 (Designated_Type (Etype (Comp)))
0f4cb75c 2140 then
e18d6a15
JM
2141 if Is_Entity_Name (Expression (Alloc)) then
2142 Freeze_And_Append
c159409f 2143 (Entity (Expression (Alloc)), N, Result);
e18d6a15
JM
2144 elsif
2145 Nkind (Expression (Alloc)) = N_Subtype_Indication
2146 then
2147 Freeze_And_Append
2148 (Entity (Subtype_Mark (Expression (Alloc))),
c159409f 2149 N, Result);
e18d6a15 2150 end if;
0f4cb75c 2151
e18d6a15
JM
2152 elsif Is_Itype (Designated_Type (Etype (Comp))) then
2153 Check_Itype (Etype (Comp));
0f4cb75c 2154
e18d6a15
JM
2155 else
2156 Freeze_And_Append
c159409f 2157 (Designated_Type (Etype (Comp)), N, Result);
e18d6a15 2158 end if;
c6823a20
EB
2159 end if;
2160 end;
2161
2162 elsif Is_Access_Type (Etype (Comp))
2163 and then Is_Itype (Designated_Type (Etype (Comp)))
2164 then
7d8b9c99 2165 Check_Itype (Etype (Comp));
c6823a20
EB
2166
2167 elsif Is_Array_Type (Etype (Comp))
2168 and then Is_Access_Type (Component_Type (Etype (Comp)))
2169 and then Present (Parent (Comp))
2170 and then Nkind (Parent (Comp)) = N_Component_Declaration
2171 and then Present (Expression (Parent (Comp)))
2172 and then Nkind (Expression (Parent (Comp))) = N_Aggregate
2173 and then Is_Fully_Defined
2174 (Designated_Type (Component_Type (Etype (Comp))))
2175 then
2176 Freeze_And_Append
2177 (Designated_Type
c159409f 2178 (Component_Type (Etype (Comp))), N, Result);
c6823a20
EB
2179 end if;
2180
2181 Prev := Comp;
70482933
RK
2182 Next_Entity (Comp);
2183 end loop;
2184
f91510fc
AC
2185 ADC := Get_Attribute_Definition_Clause
2186 (Rec, Attribute_Scalar_Storage_Order);
2187
d3b00ce3
AC
2188 if Present (ADC) then
2189
2190 -- Check compatibility of Scalar_Storage_Order with Bit_Order, if
2191 -- the former is specified.
2192
2193 if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
2194
2195 -- Note: report error on Rec, not on ADC, as ADC may apply to
2196 -- an ancestor type.
2197
2198 Error_Msg_Sloc := Sloc (ADC);
2199 Error_Msg_N
2200 ("scalar storage order for& specified# inconsistent with "
2201 & "bit order", Rec);
2202 end if;
2203
2204 -- Warn if there is a Scalar_Storage_Order but no component clause
50cd5b4d 2205
d3b00ce3
AC
2206 if not Placed_Component then
2207 Error_Msg_N
2208 ("?scalar storage order specified but no component clause",
2209 ADC);
2210 end if;
f91510fc
AC
2211 end if;
2212
164e06c6 2213 -- Deal with Bit_Order aspect specifying a non-default bit order
fea9e956 2214
2a290fec 2215 ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
758ad973
AC
2216
2217 if Present (ADC) and then Base_Type (Rec) = Rec then
fea9e956 2218 if not Placed_Component then
d3b00ce3 2219 Error_Msg_N ("?bit order specification has no effect", ADC);
fea9e956
ES
2220 Error_Msg_N
2221 ("\?since no component clauses were specified", ADC);
2222
8a95f4e8 2223 -- Here is where we do the processing for reversed bit order
70482933 2224
758ad973 2225 elsif Reverse_Bit_Order (Rec)
2a290fec 2226 and then not Reverse_Storage_Order (Rec)
758ad973 2227 then
fea9e956 2228 Adjust_Record_For_Reverse_Bit_Order (Rec);
702d139e 2229
758ad973 2230 -- Case where we have both an explicit Bit_Order and the same
702d139e
TQ
2231 -- Scalar_Storage_Order: leave record untouched, the back-end
2232 -- will take care of required layout conversions.
2233
2234 else
2235 null;
2236
fea9e956 2237 end if;
70482933
RK
2238 end if;
2239
8a95f4e8
RD
2240 -- Complete error checking on record representation clause (e.g.
2241 -- overlap of components). This is called after adjusting the
2242 -- record for reverse bit order.
2243
2244 declare
2245 RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
2246 begin
2247 if Present (RRC) then
2248 Check_Record_Representation_Clause (RRC);
2249 end if;
2250 end;
2251
1b24ada5
RD
2252 -- Set OK_To_Reorder_Components depending on debug flags
2253
d347f572 2254 if Is_Base_Type (Rec) and then Convention (Rec) = Convention_Ada then
1b24ada5 2255 if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
d3b00ce3
AC
2256 or else
2257 (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
1b24ada5
RD
2258 then
2259 Set_OK_To_Reorder_Components (Rec);
2260 end if;
2261 end if;
2262
ee094616
RD
2263 -- Check for useless pragma Pack when all components placed. We only
2264 -- do this check for record types, not subtypes, since a subtype may
2265 -- have all its components placed, and it still makes perfectly good
1b24ada5
RD
2266 -- sense to pack other subtypes or the parent type. We do not give
2267 -- this warning if Optimize_Alignment is set to Space, since the
2268 -- pragma Pack does have an effect in this case (it always resets
2269 -- the alignment to one).
70482933 2270
ee094616
RD
2271 if Ekind (Rec) = E_Record_Type
2272 and then Is_Packed (Rec)
70482933 2273 and then not Unplaced_Component
1b24ada5 2274 and then Optimize_Alignment /= 'S'
70482933 2275 then
def46b54
RD
2276 -- Reset packed status. Probably not necessary, but we do it so
2277 -- that there is no chance of the back end doing something strange
2278 -- with this redundant indication of packing.
ee094616 2279
70482933 2280 Set_Is_Packed (Rec, False);
ee094616
RD
2281
2282 -- Give warning if redundant constructs warnings on
2283
2284 if Warn_On_Redundant_Constructs then
ed2233dc 2285 Error_Msg_N -- CODEFIX
ee094616
RD
2286 ("?pragma Pack has no effect, no unplaced components",
2287 Get_Rep_Pragma (Rec, Name_Pack));
2288 end if;
70482933
RK
2289 end if;
2290
ee094616
RD
2291 -- If this is the record corresponding to a remote type, freeze the
2292 -- remote type here since that is what we are semantically freezing.
2293 -- This prevents the freeze node for that type in an inner scope.
70482933
RK
2294
2295 -- Also, Check for controlled components and unchecked unions.
ee094616
RD
2296 -- Finally, enforce the restriction that access attributes with a
2297 -- current instance prefix can only apply to limited types.
70482933 2298
8dc10d38 2299 if Ekind (Rec) = E_Record_Type then
70482933 2300 if Present (Corresponding_Remote_Type (Rec)) then
c159409f 2301 Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
70482933
RK
2302 end if;
2303
2304 Comp := First_Component (Rec);
70482933 2305 while Present (Comp) loop
80fa4617
EB
2306
2307 -- Do not set Has_Controlled_Component on a class-wide
2308 -- equivalent type. See Make_CW_Equivalent_Type.
2309
2310 if not Is_Class_Wide_Equivalent_Type (Rec)
2311 and then (Has_Controlled_Component (Etype (Comp))
2312 or else (Chars (Comp) /= Name_uParent
2313 and then Is_Controlled (Etype (Comp)))
2314 or else (Is_Protected_Type (Etype (Comp))
2315 and then Present
2316 (Corresponding_Record_Type
2317 (Etype (Comp)))
2318 and then Has_Controlled_Component
2319 (Corresponding_Record_Type
2320 (Etype (Comp)))))
70482933
RK
2321 then
2322 Set_Has_Controlled_Component (Rec);
70482933
RK
2323 end if;
2324
2325 if Has_Unchecked_Union (Etype (Comp)) then
2326 Set_Has_Unchecked_Union (Rec);
2327 end if;
2328
e1308fa8
AC
2329 -- Scan component declaration for likely misuses of current
2330 -- instance, either in a constraint or a default expression.
70482933 2331
e1308fa8 2332 if Has_Per_Object_Constraint (Comp) then
70482933
RK
2333 Check_Current_Instance (Parent (Comp));
2334 end if;
2335
2336 Next_Component (Comp);
2337 end loop;
2338 end if;
2339
2340 Set_Component_Alignment_If_Not_Set (Rec);
2341
ee094616
RD
2342 -- For first subtypes, check if there are any fixed-point fields with
2343 -- component clauses, where we must check the size. This is not done
2344 -- till the freeze point, since for fixed-point types, we do not know
2345 -- the size until the type is frozen. Similar processing applies to
2346 -- bit packed arrays.
70482933
RK
2347
2348 if Is_First_Subtype (Rec) then
2349 Comp := First_Component (Rec);
70482933
RK
2350 while Present (Comp) loop
2351 if Present (Component_Clause (Comp))
d05ef0ab
AC
2352 and then (Is_Fixed_Point_Type (Etype (Comp))
2353 or else
2354 Is_Bit_Packed_Array (Etype (Comp)))
70482933
RK
2355 then
2356 Check_Size
d05ef0ab 2357 (Component_Name (Component_Clause (Comp)),
70482933
RK
2358 Etype (Comp),
2359 Esize (Comp),
2360 Junk);
2361 end if;
2362
2363 Next_Component (Comp);
2364 end loop;
2365 end if;
7d8b9c99
RD
2366
2367 -- Generate warning for applying C or C++ convention to a record
2368 -- with discriminants. This is suppressed for the unchecked union
1b24ada5
RD
2369 -- case, since the whole point in this case is interface C. We also
2370 -- do not generate this within instantiations, since we will have
2371 -- generated a message on the template.
7d8b9c99
RD
2372
2373 if Has_Discriminants (E)
2374 and then not Is_Unchecked_Union (E)
7d8b9c99
RD
2375 and then (Convention (E) = Convention_C
2376 or else
2377 Convention (E) = Convention_CPP)
2378 and then Comes_From_Source (E)
1b24ada5
RD
2379 and then not In_Instance
2380 and then not Has_Warnings_Off (E)
2381 and then not Has_Warnings_Off (Base_Type (E))
7d8b9c99
RD
2382 then
2383 declare
2384 Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
2385 A2 : Node_Id;
2386
2387 begin
2388 if Present (Cprag) then
2389 A2 := Next (First (Pragma_Argument_Associations (Cprag)));
2390
2391 if Convention (E) = Convention_C then
2392 Error_Msg_N
2393 ("?variant record has no direct equivalent in C", A2);
2394 else
2395 Error_Msg_N
2396 ("?variant record has no direct equivalent in C++", A2);
2397 end if;
2398
2399 Error_Msg_NE
2400 ("\?use of convention for type& is dubious", A2, E);
2401 end if;
2402 end;
2403 end if;
8dc10d38 2404
ce14c577 2405 -- See if Size is too small as is (and implicit packing might help)
8dc10d38 2406
426d2717 2407 if not Is_Packed (Rec)
ce14c577
AC
2408
2409 -- No implicit packing if even one component is explicitly placed
2410
426d2717 2411 and then not Placed_Component
ce14c577
AC
2412
2413 -- Must have size clause and all scalar components
2414
8dc10d38
AC
2415 and then Has_Size_Clause (Rec)
2416 and then All_Scalar_Components
ce14c577
AC
2417
2418 -- Do not try implicit packing on records with discriminants, too
2419 -- complicated, especially in the variant record case.
2420
8dc10d38 2421 and then not Has_Discriminants (Rec)
ce14c577
AC
2422
2423 -- We can implicitly pack if the specified size of the record is
2424 -- less than the sum of the object sizes (no point in packing if
2425 -- this is not the case).
2426
fc893455 2427 and then RM_Size (Rec) < Scalar_Component_Total_Esize
ce14c577
AC
2428
2429 -- And the total RM size cannot be greater than the specified size
2430 -- since otherwise packing will not get us where we have to be!
2431
fc893455 2432 and then RM_Size (Rec) >= Scalar_Component_Total_RM_Size
ce14c577 2433
56812278 2434 -- Never do implicit packing in CodePeer or Alfa modes since
59e6b23c 2435 -- we don't do any packing in these modes, since this generates
25ebc085
AC
2436 -- over-complex code that confuses static analysis, and in
2437 -- general, neither CodePeer not GNATprove care about the
2438 -- internal representation of objects.
ce14c577 2439
56812278 2440 and then not (CodePeer_Mode or Alfa_Mode)
8dc10d38 2441 then
426d2717
AC
2442 -- If implicit packing enabled, do it
2443
2444 if Implicit_Packing then
2445 Set_Is_Packed (Rec);
2446
2447 -- Otherwise flag the size clause
2448
2449 else
2450 declare
2451 Sz : constant Node_Id := Size_Clause (Rec);
2452 begin
ed2233dc 2453 Error_Msg_NE -- CODEFIX
426d2717 2454 ("size given for& too small", Sz, Rec);
ed2233dc 2455 Error_Msg_N -- CODEFIX
426d2717
AC
2456 ("\use explicit pragma Pack "
2457 & "or use pragma Implicit_Packing", Sz);
2458 end;
2459 end if;
8dc10d38 2460 end if;
70482933
RK
2461 end Freeze_Record_Type;
2462
2463 -- Start of processing for Freeze_Entity
2464
2465 begin
c6823a20
EB
2466 -- We are going to test for various reasons why this entity need not be
2467 -- frozen here, but in the case of an Itype that's defined within a
2468 -- record, that test actually applies to the record.
2469
2470 if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
2471 Test_E := Scope (E);
2472 elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
2473 and then Is_Record_Type (Underlying_Type (Scope (E)))
2474 then
2475 Test_E := Underlying_Type (Scope (E));
2476 end if;
2477
fbf5a39b 2478 -- Do not freeze if already frozen since we only need one freeze node
70482933
RK
2479
2480 if Is_Frozen (E) then
2481 return No_List;
2482
c6823a20
EB
2483 -- It is improper to freeze an external entity within a generic because
2484 -- its freeze node will appear in a non-valid context. The entity will
2485 -- be frozen in the proper scope after the current generic is analyzed.
70482933 2486
c6823a20 2487 elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
70482933
RK
2488 return No_List;
2489
164e06c6
AC
2490 -- AI05-0213: A formal incomplete type does not freeze the actual. In
2491 -- the instance, the same applies to the subtype renaming the actual.
d3cb4cc0
AC
2492
2493 elsif Is_Private_Type (E)
2494 and then Is_Generic_Actual_Type (E)
2495 and then No (Full_View (Base_Type (E)))
2496 and then Ada_Version >= Ada_2012
2497 then
2498 return No_List;
2499
70482933
RK
2500 -- Do not freeze a global entity within an inner scope created during
2501 -- expansion. A call to subprogram E within some internal procedure
2502 -- (a stream attribute for example) might require freezing E, but the
2503 -- freeze node must appear in the same declarative part as E itself.
2504 -- The two-pass elaboration mechanism in gigi guarantees that E will
2505 -- be frozen before the inner call is elaborated. We exclude constants
2506 -- from this test, because deferred constants may be frozen early, and
19590d70
GD
2507 -- must be diagnosed (e.g. in the case of a deferred constant being used
2508 -- in a default expression). If the enclosing subprogram comes from
2509 -- source, or is a generic instance, then the freeze point is the one
2510 -- mandated by the language, and we freeze the entity. A subprogram that
2511 -- is a child unit body that acts as a spec does not have a spec that
2512 -- comes from source, but can only come from source.
70482933 2513
c6823a20
EB
2514 elsif In_Open_Scopes (Scope (Test_E))
2515 and then Scope (Test_E) /= Current_Scope
2516 and then Ekind (Test_E) /= E_Constant
70482933
RK
2517 then
2518 declare
3cae7f14 2519 S : Entity_Id;
70482933
RK
2520
2521 begin
3cae7f14 2522 S := Current_Scope;
70482933
RK
2523 while Present (S) loop
2524 if Is_Overloadable (S) then
2525 if Comes_From_Source (S)
2526 or else Is_Generic_Instance (S)
fea9e956 2527 or else Is_Child_Unit (S)
70482933
RK
2528 then
2529 exit;
2530 else
2531 return No_List;
2532 end if;
2533 end if;
2534
2535 S := Scope (S);
2536 end loop;
2537 end;
555360a5
AC
2538
2539 -- Similarly, an inlined instance body may make reference to global
2540 -- entities, but these references cannot be the proper freezing point
def46b54
RD
2541 -- for them, and in the absence of inlining freezing will take place in
2542 -- their own scope. Normally instance bodies are analyzed after the
2543 -- enclosing compilation, and everything has been frozen at the proper
2544 -- place, but with front-end inlining an instance body is compiled
2545 -- before the end of the enclosing scope, and as a result out-of-order
2546 -- freezing must be prevented.
555360a5
AC
2547
2548 elsif Front_End_Inlining
7d8b9c99 2549 and then In_Instance_Body
c6823a20 2550 and then Present (Scope (Test_E))
555360a5
AC
2551 then
2552 declare
3cae7f14 2553 S : Entity_Id;
c6823a20 2554
555360a5 2555 begin
3cae7f14 2556 S := Scope (Test_E);
555360a5
AC
2557 while Present (S) loop
2558 if Is_Generic_Instance (S) then
2559 exit;
2560 else
2561 S := Scope (S);
2562 end if;
2563 end loop;
2564
2565 if No (S) then
2566 return No_List;
2567 end if;
2568 end;
70482933
RK
2569 end if;
2570
b98e2969 2571 -- Deal with delayed aspect specifications. The analysis of the
8a0320ad
AC
2572 -- aspect is required to be delayed to the freeze point, thus we
2573 -- analyze the pragma or attribute definition clause in the tree at
b98e2969
AC
2574 -- this point. We also analyze the aspect specification node at the
2575 -- freeze point when the aspect doesn't correspond to
2576 -- pragma/attribute definition clause.
c159409f
AC
2577
2578 if Has_Delayed_Aspects (E) then
8a0320ad 2579 Analyze_Aspects_At_Freeze_Point (E);
c159409f
AC
2580 end if;
2581
70482933
RK
2582 -- Here to freeze the entity
2583
70482933
RK
2584 Set_Is_Frozen (E);
2585
2586 -- Case of entity being frozen is other than a type
2587
2588 if not Is_Type (E) then
70482933
RK
2589 -- If entity is exported or imported and does not have an external
2590 -- name, now is the time to provide the appropriate default name.
2591 -- Skip this if the entity is stubbed, since we don't need a name
75a64833
AC
2592 -- for any stubbed routine. For the case on intrinsics, if no
2593 -- external name is specified, then calls will be handled in
545cb5be
AC
2594 -- Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
2595 -- external name is provided, then Expand_Intrinsic_Call leaves
75a64833 2596 -- calls in place for expansion by GIGI.
70482933
RK
2597
2598 if (Is_Imported (E) or else Is_Exported (E))
2599 and then No (Interface_Name (E))
2600 and then Convention (E) /= Convention_Stubbed
75a64833 2601 and then Convention (E) /= Convention_Intrinsic
70482933
RK
2602 then
2603 Set_Encoded_Interface_Name
2604 (E, Get_Default_External_Name (E));
fbf5a39b 2605
bbaba73f
EB
2606 -- If entity is an atomic object appearing in a declaration and
2607 -- the expression is an aggregate, assign it to a temporary to
2608 -- ensure that the actual assignment is done atomically rather
2609 -- than component-wise (the assignment to the temp may be done
2610 -- component-wise, but that is harmless).
fbf5a39b
AC
2611
2612 elsif Is_Atomic (E)
2613 and then Nkind (Parent (E)) = N_Object_Declaration
2614 and then Present (Expression (Parent (E)))
bbaba73f 2615 and then Nkind (Expression (Parent (E))) = N_Aggregate
f96b2d85 2616 and then Is_Atomic_Aggregate (Expression (Parent (E)), Etype (E))
fbf5a39b 2617 then
b0159fbe 2618 null;
70482933
RK
2619 end if;
2620
2621 -- For a subprogram, freeze all parameter types and also the return
fbf5a39b 2622 -- type (RM 13.14(14)). However skip this for internal subprograms.
70482933 2623 -- This is also the point where any extra formal parameters are
fb2e11ee
AC
2624 -- created since we now know whether the subprogram will use a
2625 -- foreign convention.
70482933
RK
2626
2627 if Is_Subprogram (E) then
70482933 2628 if not Is_Internal (E) then
70482933 2629 declare
6d11af89 2630 F_Type : Entity_Id;
def46b54 2631 R_Type : Entity_Id;
6d11af89 2632 Warn_Node : Node_Id;
70482933 2633
70482933
RK
2634 begin
2635 -- Loop through formals
2636
2637 Formal := First_Formal (E);
70482933 2638 while Present (Formal) loop
70482933 2639 F_Type := Etype (Formal);
406935b6
AC
2640
2641 -- AI05-0151 : incomplete types can appear in a profile.
2642 -- By the time the entity is frozen, the full view must
2643 -- be available, unless it is a limited view.
2644
2645 if Is_Incomplete_Type (F_Type)
2646 and then Present (Full_View (F_Type))
2647 then
2648 F_Type := Full_View (F_Type);
2649 Set_Etype (Formal, F_Type);
2650 end if;
2651
c159409f 2652 Freeze_And_Append (F_Type, N, Result);
70482933
RK
2653
2654 if Is_Private_Type (F_Type)
2655 and then Is_Private_Type (Base_Type (F_Type))
2656 and then No (Full_View (Base_Type (F_Type)))
2657 and then not Is_Generic_Type (F_Type)
2658 and then not Is_Derived_Type (F_Type)
2659 then
2660 -- If the type of a formal is incomplete, subprogram
2661 -- is being frozen prematurely. Within an instance
2662 -- (but not within a wrapper package) this is an
fb2e11ee 2663 -- artifact of our need to regard the end of an
70482933
RK
2664 -- instantiation as a freeze point. Otherwise it is
2665 -- a definite error.
fbf5a39b 2666
70482933
RK
2667 if In_Instance then
2668 Set_Is_Frozen (E, False);
2669 return No_List;
2670
86cde7b1
RD
2671 elsif not After_Last_Declaration
2672 and then not Freezing_Library_Level_Tagged_Type
2673 then
70482933
RK
2674 Error_Msg_Node_1 := F_Type;
2675 Error_Msg
2676 ("type& must be fully defined before this point",
2677 Loc);
2678 end if;
2679 end if;
2680
def46b54 2681 -- Check suspicious parameter for C function. These tests
1b24ada5 2682 -- apply only to exported/imported subprograms.
70482933 2683
def46b54 2684 if Warn_On_Export_Import
1b24ada5 2685 and then Comes_From_Source (E)
def46b54
RD
2686 and then (Convention (E) = Convention_C
2687 or else
2688 Convention (E) = Convention_CPP)
def46b54 2689 and then (Is_Imported (E) or else Is_Exported (E))
1b24ada5
RD
2690 and then Convention (E) /= Convention (Formal)
2691 and then not Has_Warnings_Off (E)
2692 and then not Has_Warnings_Off (F_Type)
2693 and then not Has_Warnings_Off (Formal)
fbf5a39b 2694 then
b3afa59b
AC
2695 -- Qualify mention of formals with subprogram name
2696
70482933 2697 Error_Msg_Qual_Level := 1;
def46b54
RD
2698
2699 -- Check suspicious use of fat C pointer
2700
2701 if Is_Access_Type (F_Type)
2702 and then Esize (F_Type) > Ttypes.System_Address_Size
2703 then
2704 Error_Msg_N
b3afa59b
AC
2705 ("?type of & does not correspond to C pointer!",
2706 Formal);
def46b54
RD
2707
2708 -- Check suspicious return of boolean
2709
2710 elsif Root_Type (F_Type) = Standard_Boolean
2711 and then Convention (F_Type) = Convention_Ada
67198556
RD
2712 and then not Has_Warnings_Off (F_Type)
2713 and then not Has_Size_Clause (F_Type)
6a2afd13 2714 and then VM_Target = No_VM
def46b54 2715 then
ed2233dc 2716 Error_Msg_N ("& is an 8-bit Ada Boolean?", Formal);
b3afa59b
AC
2717 Error_Msg_N
2718 ("\use appropriate corresponding type in C "
2719 & "(e.g. char)?", Formal);
def46b54
RD
2720
2721 -- Check suspicious tagged type
2722
2723 elsif (Is_Tagged_Type (F_Type)
2724 or else (Is_Access_Type (F_Type)
2725 and then
2726 Is_Tagged_Type
2727 (Designated_Type (F_Type))))
2728 and then Convention (E) = Convention_C
2729 then
2730 Error_Msg_N
e7d72fb9 2731 ("?& involves a tagged type which does not "
def46b54
RD
2732 & "correspond to any C type!", Formal);
2733
2734 -- Check wrong convention subprogram pointer
2735
2736 elsif Ekind (F_Type) = E_Access_Subprogram_Type
2737 and then not Has_Foreign_Convention (F_Type)
2738 then
2739 Error_Msg_N
2740 ("?subprogram pointer & should "
2741 & "have foreign convention!", Formal);
2742 Error_Msg_Sloc := Sloc (F_Type);
2743 Error_Msg_NE
2744 ("\?add Convention pragma to declaration of &#",
2745 Formal, F_Type);
2746 end if;
2747
b3afa59b
AC
2748 -- Turn off name qualification after message output
2749
70482933
RK
2750 Error_Msg_Qual_Level := 0;
2751 end if;
2752
2753 -- Check for unconstrained array in exported foreign
2754 -- convention case.
2755
def46b54 2756 if Has_Foreign_Convention (E)
70482933
RK
2757 and then not Is_Imported (E)
2758 and then Is_Array_Type (F_Type)
2759 and then not Is_Constrained (F_Type)
fbf5a39b 2760 and then Warn_On_Export_Import
3acdda2d
AC
2761
2762 -- Exclude VM case, since both .NET and JVM can handle
2763 -- unconstrained arrays without a problem.
2764
2765 and then VM_Target = No_VM
70482933
RK
2766 then
2767 Error_Msg_Qual_Level := 1;
6d11af89
AC
2768
2769 -- If this is an inherited operation, place the
2770 -- warning on the derived type declaration, rather
2771 -- than on the original subprogram.
2772
2773 if Nkind (Original_Node (Parent (E))) =
2774 N_Full_Type_Declaration
2775 then
2776 Warn_Node := Parent (E);
2777
2778 if Formal = First_Formal (E) then
2779 Error_Msg_NE
add9f797 2780 ("?in inherited operation&", Warn_Node, E);
6d11af89
AC
2781 end if;
2782 else
2783 Warn_Node := Formal;
2784 end if;
2785
2786 Error_Msg_NE
70482933 2787 ("?type of argument& is unconstrained array",
6d11af89
AC
2788 Warn_Node, Formal);
2789 Error_Msg_NE
70482933 2790 ("?foreign caller must pass bounds explicitly",
6d11af89 2791 Warn_Node, Formal);
70482933
RK
2792 Error_Msg_Qual_Level := 0;
2793 end if;
2794
d8db0bca
JM
2795 if not From_With_Type (F_Type) then
2796 if Is_Access_Type (F_Type) then
2797 F_Type := Designated_Type (F_Type);
2798 end if;
2799
7d8b9c99
RD
2800 -- If the formal is an anonymous_access_to_subprogram
2801 -- freeze the subprogram type as well, to prevent
2802 -- scope anomalies in gigi, because there is no other
2803 -- clear point at which it could be frozen.
2804
93bcda23 2805 if Is_Itype (Etype (Formal))
7d8b9c99
RD
2806 and then Ekind (F_Type) = E_Subprogram_Type
2807 then
c159409f 2808 Freeze_And_Append (F_Type, N, Result);
d8db0bca
JM
2809 end if;
2810 end if;
2811
70482933
RK
2812 Next_Formal (Formal);
2813 end loop;
2814
5e39baa6 2815 -- Case of function: similar checks on return type
70482933
RK
2816
2817 if Ekind (E) = E_Function then
def46b54
RD
2818
2819 -- Freeze return type
2820
2821 R_Type := Etype (E);
406935b6
AC
2822
2823 -- AI05-0151: the return type may have been incomplete
2824 -- at the point of declaration.
2825
2826 if Ekind (R_Type) = E_Incomplete_Type
2827 and then Present (Full_View (R_Type))
2828 then
2829 R_Type := Full_View (R_Type);
2830 Set_Etype (E, R_Type);
2831 end if;
2832
c159409f 2833 Freeze_And_Append (R_Type, N, Result);
def46b54
RD
2834
2835 -- Check suspicious return type for C function
70482933 2836
fbf5a39b 2837 if Warn_On_Export_Import
def46b54
RD
2838 and then (Convention (E) = Convention_C
2839 or else
2840 Convention (E) = Convention_CPP)
def46b54 2841 and then (Is_Imported (E) or else Is_Exported (E))
fbf5a39b 2842 then
def46b54
RD
2843 -- Check suspicious return of fat C pointer
2844
2845 if Is_Access_Type (R_Type)
2846 and then Esize (R_Type) > Ttypes.System_Address_Size
1b24ada5
RD
2847 and then not Has_Warnings_Off (E)
2848 and then not Has_Warnings_Off (R_Type)
def46b54
RD
2849 then
2850 Error_Msg_N
2851 ("?return type of& does not "
2852 & "correspond to C pointer!", E);
2853
2854 -- Check suspicious return of boolean
2855
2856 elsif Root_Type (R_Type) = Standard_Boolean
2857 and then Convention (R_Type) = Convention_Ada
6a2afd13 2858 and then VM_Target = No_VM
1b24ada5
RD
2859 and then not Has_Warnings_Off (E)
2860 and then not Has_Warnings_Off (R_Type)
67198556 2861 and then not Has_Size_Clause (R_Type)
def46b54 2862 then
b3afa59b
AC
2863 declare
2864 N : constant Node_Id :=
2865 Result_Definition (Declaration_Node (E));
2866 begin
2867 Error_Msg_NE
2868 ("return type of & is an 8-bit Ada Boolean?",
2869 N, E);
2870 Error_Msg_NE
2871 ("\use appropriate corresponding type in C "
2872 & "(e.g. char)?", N, E);
2873 end;
70482933 2874
def46b54
RD
2875 -- Check suspicious return tagged type
2876
2877 elsif (Is_Tagged_Type (R_Type)
2878 or else (Is_Access_Type (R_Type)
2879 and then
2880 Is_Tagged_Type
2881 (Designated_Type (R_Type))))
2882 and then Convention (E) = Convention_C
1b24ada5
RD
2883 and then not Has_Warnings_Off (E)
2884 and then not Has_Warnings_Off (R_Type)
def46b54
RD
2885 then
2886 Error_Msg_N
2887 ("?return type of & does not "
2888 & "correspond to C type!", E);
2889
2890 -- Check return of wrong convention subprogram pointer
2891
2892 elsif Ekind (R_Type) = E_Access_Subprogram_Type
2893 and then not Has_Foreign_Convention (R_Type)
1b24ada5
RD
2894 and then not Has_Warnings_Off (E)
2895 and then not Has_Warnings_Off (R_Type)
def46b54
RD
2896 then
2897 Error_Msg_N
2898 ("?& should return a foreign "
2899 & "convention subprogram pointer", E);
2900 Error_Msg_Sloc := Sloc (R_Type);
2901 Error_Msg_NE
2902 ("\?add Convention pragma to declaration of& #",
2903 E, R_Type);
2904 end if;
2905 end if;
2906
308e6f3a 2907 -- Give warning for suspicious return of a result of an
e7d72fb9
AC
2908 -- unconstrained array type in a foreign convention
2909 -- function.
59366db6 2910
e7d72fb9
AC
2911 if Has_Foreign_Convention (E)
2912
2c1b72d7 2913 -- We are looking for a return of unconstrained array
e7d72fb9
AC
2914
2915 and then Is_Array_Type (R_Type)
93bcda23 2916 and then not Is_Constrained (R_Type)
e7d72fb9 2917
2c1b72d7
AC
2918 -- Exclude imported routines, the warning does not
2919 -- belong on the import, but rather on the routine
2920 -- definition.
e7d72fb9 2921
70482933 2922 and then not Is_Imported (E)
e7d72fb9 2923
2c1b72d7
AC
2924 -- Exclude VM case, since both .NET and JVM can handle
2925 -- return of unconstrained arrays without a problem.
e7d72fb9 2926
f3b57ab0 2927 and then VM_Target = No_VM
e7d72fb9 2928
2c1b72d7
AC
2929 -- Check that general warning is enabled, and that it
2930 -- is not suppressed for this particular case.
e7d72fb9 2931
fbf5a39b 2932 and then Warn_On_Export_Import
1b24ada5 2933 and then not Has_Warnings_Off (E)
93bcda23 2934 and then not Has_Warnings_Off (R_Type)
70482933
RK
2935 then
2936 Error_Msg_N
fbf5a39b 2937 ("?foreign convention function& should not " &
1b24ada5 2938 "return unconstrained array!", E);
70482933
RK
2939 end if;
2940 end if;
2941 end;
2942 end if;
2943
2944 -- Must freeze its parent first if it is a derived subprogram
2945
2946 if Present (Alias (E)) then
c159409f 2947 Freeze_And_Append (Alias (E), N, Result);
70482933
RK
2948 end if;
2949
19590d70
GD
2950 -- We don't freeze internal subprograms, because we don't normally
2951 -- want addition of extra formals or mechanism setting to happen
2952 -- for those. However we do pass through predefined dispatching
2953 -- cases, since extra formals may be needed in some cases, such as
2954 -- for the stream 'Input function (build-in-place formals).
2955
2956 if not Is_Internal (E)
2957 or else Is_Predefined_Dispatching_Operation (E)
2958 then
70482933
RK
2959 Freeze_Subprogram (E);
2960 end if;
2961
2962 -- Here for other than a subprogram or type
2963
2964 else
2965 -- If entity has a type, and it is not a generic unit, then
7d8b9c99 2966 -- freeze it first (RM 13.14(10)).
70482933 2967
ac72c9c5 2968 if Present (Etype (E))
70482933
RK
2969 and then Ekind (E) /= E_Generic_Function
2970 then
c159409f 2971 Freeze_And_Append (Etype (E), N, Result);
70482933
RK
2972 end if;
2973
2c9beb8a 2974 -- Special processing for objects created by object declaration
70482933
RK
2975
2976 if Nkind (Declaration_Node (E)) = N_Object_Declaration then
2c9beb8a 2977
6823270c
AC
2978 -- Abstract type allowed only for C++ imported variables or
2979 -- constants.
2980
2981 -- Note: we inhibit this check for objects that do not come
2982 -- from source because there is at least one case (the
3e24afaa 2983 -- expansion of x'Class'Input where x is abstract) where we
6823270c
AC
2984 -- legitimately generate an abstract object.
2985
2986 if Is_Abstract_Type (Etype (E))
2987 and then Comes_From_Source (Parent (E))
2988 and then not (Is_Imported (E)
2989 and then Is_CPP_Class (Etype (E)))
2990 then
2991 Error_Msg_N ("type of object cannot be abstract",
2992 Object_Definition (Parent (E)));
2993
2994 if Is_CPP_Class (Etype (E)) then
ed2233dc
AC
2995 Error_Msg_NE
2996 ("\} may need a cpp_constructor",
6823270c
AC
2997 Object_Definition (Parent (E)), Etype (E));
2998 end if;
2999 end if;
3000
2c9beb8a
RD
3001 -- For object created by object declaration, perform required
3002 -- categorization (preelaborate and pure) checks. Defer these
3003 -- checks to freeze time since pragma Import inhibits default
3004 -- initialization and thus pragma Import affects these checks.
3005
70482933 3006 Validate_Object_Declaration (Declaration_Node (E));
2c9beb8a 3007
1ce1f005 3008 -- If there is an address clause, check that it is valid
2c9beb8a 3009
fbf5a39b 3010 Check_Address_Clause (E);
2c9beb8a 3011
1ce1f005
GD
3012 -- If the object needs any kind of default initialization, an
3013 -- error must be issued if No_Default_Initialization applies.
3014 -- The check doesn't apply to imported objects, which are not
3015 -- ever default initialized, and is why the check is deferred
3016 -- until freezing, at which point we know if Import applies.
4fec4e7a
ES
3017 -- Deferred constants are also exempted from this test because
3018 -- their completion is explicit, or through an import pragma.
1ce1f005 3019
4fec4e7a
ES
3020 if Ekind (E) = E_Constant
3021 and then Present (Full_View (E))
3022 then
3023 null;
3024
3025 elsif Comes_From_Source (E)
b6e209b5 3026 and then not Is_Imported (E)
1ce1f005
GD
3027 and then not Has_Init_Expression (Declaration_Node (E))
3028 and then
3029 ((Has_Non_Null_Base_Init_Proc (Etype (E))
3030 and then not No_Initialization (Declaration_Node (E))
3031 and then not Is_Value_Type (Etype (E))
5b1e6aca 3032 and then not Initialization_Suppressed (Etype (E)))
1ce1f005
GD
3033 or else
3034 (Needs_Simple_Initialization (Etype (E))
3035 and then not Is_Internal (E)))
3036 then
4c8a5bb8 3037 Has_Default_Initialization := True;
1ce1f005
GD
3038 Check_Restriction
3039 (No_Default_Initialization, Declaration_Node (E));
3040 end if;
3041
4c8a5bb8
AC
3042 -- Check that a Thread_Local_Storage variable does not have
3043 -- default initialization, and any explicit initialization must
3044 -- either be the null constant or a static constant.
3045
3046 if Has_Pragma_Thread_Local_Storage (E) then
3047 declare
3048 Decl : constant Node_Id := Declaration_Node (E);
3049 begin
3050 if Has_Default_Initialization
3051 or else
3052 (Has_Init_Expression (Decl)
3053 and then
3054 (No (Expression (Decl))
3055 or else not
3056 (Is_Static_Expression (Expression (Decl))
3057 or else
3058 Nkind (Expression (Decl)) = N_Null)))
3059 then
3060 Error_Msg_NE
3061 ("Thread_Local_Storage variable& is "
3062 & "improperly initialized", Decl, E);
3063 Error_Msg_NE
3064 ("\only allowed initialization is explicit "
3065 & "NULL or static expression", Decl, E);
3066 end if;
3067 end;
3068 end if;
3069
def46b54
RD
3070 -- For imported objects, set Is_Public unless there is also an
3071 -- address clause, which means that there is no external symbol
3072 -- needed for the Import (Is_Public may still be set for other
3073 -- unrelated reasons). Note that we delayed this processing
3074 -- till freeze time so that we can be sure not to set the flag
3075 -- if there is an address clause. If there is such a clause,
3076 -- then the only purpose of the Import pragma is to suppress
3077 -- implicit initialization.
2c9beb8a
RD
3078
3079 if Is_Imported (E)
add9f797 3080 and then No (Address_Clause (E))
2c9beb8a
RD
3081 then
3082 Set_Is_Public (E);
3083 end if;
7d8b9c99
RD
3084
3085 -- For convention C objects of an enumeration type, warn if
3086 -- the size is not integer size and no explicit size given.
3087 -- Skip warning for Boolean, and Character, assume programmer
3088 -- expects 8-bit sizes for these cases.
3089
3090 if (Convention (E) = Convention_C
3091 or else
3092 Convention (E) = Convention_CPP)
3093 and then Is_Enumeration_Type (Etype (E))
3094 and then not Is_Character_Type (Etype (E))
3095 and then not Is_Boolean_Type (Etype (E))
3096 and then Esize (Etype (E)) < Standard_Integer_Size
3097 and then not Has_Size_Clause (E)
3098 then
3099 Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
3100 Error_Msg_N
3101 ("?convention C enumeration object has size less than ^",
3102 E);
3103 Error_Msg_N ("\?use explicit size clause to set size", E);
3104 end if;
70482933
RK
3105 end if;
3106
3107 -- Check that a constant which has a pragma Volatile[_Components]
7d8b9c99 3108 -- or Atomic[_Components] also has a pragma Import (RM C.6(13)).
70482933
RK
3109
3110 -- Note: Atomic[_Components] also sets Volatile[_Components]
3111
3112 if Ekind (E) = E_Constant
3113 and then (Has_Volatile_Components (E) or else Is_Volatile (E))
3114 and then not Is_Imported (E)
3115 then
3116 -- Make sure we actually have a pragma, and have not merely
3117 -- inherited the indication from elsewhere (e.g. an address
3118 -- clause, which is not good enough in RM terms!)
3119
1d571f3b 3120 if Has_Rep_Pragma (E, Name_Atomic)
91b1417d 3121 or else
1d571f3b 3122 Has_Rep_Pragma (E, Name_Atomic_Components)
70482933
RK
3123 then
3124 Error_Msg_N
91b1417d 3125 ("stand alone atomic constant must be " &
def46b54 3126 "imported (RM C.6(13))", E);
91b1417d 3127
1d571f3b 3128 elsif Has_Rep_Pragma (E, Name_Volatile)
91b1417d 3129 or else
1d571f3b 3130 Has_Rep_Pragma (E, Name_Volatile_Components)
91b1417d
AC
3131 then
3132 Error_Msg_N
3133 ("stand alone volatile constant must be " &
86cde7b1 3134 "imported (RM C.6(13))", E);
70482933
RK
3135 end if;
3136 end if;
3137
3138 -- Static objects require special handling
3139
3140 if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
3141 and then Is_Statically_Allocated (E)
3142 then
3143 Freeze_Static_Object (E);
3144 end if;
3145
3146 -- Remaining step is to layout objects
3147
3148 if Ekind (E) = E_Variable
3149 or else
3150 Ekind (E) = E_Constant
3151 or else
3152 Ekind (E) = E_Loop_Parameter
3153 or else
3154 Is_Formal (E)
3155 then
3156 Layout_Object (E);
3157 end if;
3158 end if;
3159
3160 -- Case of a type or subtype being frozen
3161
3162 else
31b5873d
GD
3163 -- We used to check here that a full type must have preelaborable
3164 -- initialization if it completes a private type specified with
308e6f3a 3165 -- pragma Preelaborable_Initialization, but that missed cases where
31b5873d
GD
3166 -- the types occur within a generic package, since the freezing
3167 -- that occurs within a containing scope generally skips traversal
3168 -- of a generic unit's declarations (those will be frozen within
3169 -- instances). This check was moved to Analyze_Package_Specification.
3f1ede06 3170
70482933
RK
3171 -- The type may be defined in a generic unit. This can occur when
3172 -- freezing a generic function that returns the type (which is
3173 -- defined in a parent unit). It is clearly meaningless to freeze
3174 -- this type. However, if it is a subtype, its size may be determi-
3175 -- nable and used in subsequent checks, so might as well try to
3176 -- compute it.
3177
cf7bb903 3178 -- In Ada 2012, Freeze_Entities is also used in the front end to
e876c43a
AC
3179 -- trigger the analysis of aspect expressions, so in this case we
3180 -- want to continue the freezing process.
3181
70482933
RK
3182 if Present (Scope (E))
3183 and then Is_Generic_Unit (Scope (E))
e876c43a 3184 and then not Has_Predicates (E)
70482933
RK
3185 then
3186 Check_Compile_Time_Size (E);
3187 return No_List;
3188 end if;
3189
3190 -- Deal with special cases of freezing for subtype
3191
3192 if E /= Base_Type (E) then
3193
86cde7b1
RD
3194 -- Before we do anything else, a specialized test for the case of
3195 -- a size given for an array where the array needs to be packed,
3196 -- but was not so the size cannot be honored. This would of course
3197 -- be caught by the backend, and indeed we don't catch all cases.
3198 -- The point is that we can give a better error message in those
3199 -- cases that we do catch with the circuitry here. Also if pragma
3200 -- Implicit_Packing is set, this is where the packing occurs.
3201
3202 -- The reason we do this so early is that the processing in the
3203 -- automatic packing case affects the layout of the base type, so
3204 -- it must be done before we freeze the base type.
3205
3206 if Is_Array_Type (E) then
3207 declare
3208 Lo, Hi : Node_Id;
3209 Ctyp : constant Entity_Id := Component_Type (E);
3210
3211 begin
3212 -- Check enabling conditions. These are straightforward
3213 -- except for the test for a limited composite type. This
3214 -- eliminates the rare case of a array of limited components
3215 -- where there are issues of whether or not we can go ahead
3216 -- and pack the array (since we can't freely pack and unpack
3217 -- arrays if they are limited).
3218
3219 -- Note that we check the root type explicitly because the
3220 -- whole point is we are doing this test before we have had
3221 -- a chance to freeze the base type (and it is that freeze
3222 -- action that causes stuff to be inherited).
3223
3224 if Present (Size_Clause (E))
fc893455 3225 and then Known_Static_RM_Size (E)
86cde7b1
RD
3226 and then not Is_Packed (E)
3227 and then not Has_Pragma_Pack (E)
3228 and then Number_Dimensions (E) = 1
3229 and then not Has_Component_Size_Clause (E)
fc893455 3230 and then Known_Static_RM_Size (Ctyp)
86cde7b1
RD
3231 and then not Is_Limited_Composite (E)
3232 and then not Is_Packed (Root_Type (E))
3233 and then not Has_Component_Size_Clause (Root_Type (E))
56812278 3234 and then not (CodePeer_Mode or Alfa_Mode)
86cde7b1
RD
3235 then
3236 Get_Index_Bounds (First_Index (E), Lo, Hi);
3237
3238 if Compile_Time_Known_Value (Lo)
3239 and then Compile_Time_Known_Value (Hi)
3240 and then Known_Static_RM_Size (Ctyp)
3241 and then RM_Size (Ctyp) < 64
3242 then
3243 declare
3244 Lov : constant Uint := Expr_Value (Lo);
3245 Hiv : constant Uint := Expr_Value (Hi);
3246 Len : constant Uint := UI_Max
3247 (Uint_0,
3248 Hiv - Lov + 1);
3249 Rsiz : constant Uint := RM_Size (Ctyp);
3250 SZ : constant Node_Id := Size_Clause (E);
3251 Btyp : constant Entity_Id := Base_Type (E);
3252
3253 -- What we are looking for here is the situation where
3254 -- the RM_Size given would be exactly right if there
3255 -- was a pragma Pack (resulting in the component size
3256 -- being the same as the RM_Size). Furthermore, the
3257 -- component type size must be an odd size (not a
5a989c6b
AC
3258 -- multiple of storage unit). If the component RM size
3259 -- is an exact number of storage units that is a power
3260 -- of two, the array is not packed and has a standard
3261 -- representation.
86cde7b1
RD
3262
3263 begin
3264 if RM_Size (E) = Len * Rsiz
3265 and then Rsiz mod System_Storage_Unit /= 0
3266 then
3267 -- For implicit packing mode, just set the
fd366a46 3268 -- component size silently.
86cde7b1
RD
3269
3270 if Implicit_Packing then
3271 Set_Component_Size (Btyp, Rsiz);
3272 Set_Is_Bit_Packed_Array (Btyp);
3273 Set_Is_Packed (Btyp);
3274 Set_Has_Non_Standard_Rep (Btyp);
3275
3276 -- Otherwise give an error message
3277
3278 else
3279 Error_Msg_NE
3280 ("size given for& too small", SZ, E);
ed2233dc 3281 Error_Msg_N -- CODEFIX
86cde7b1
RD
3282 ("\use explicit pragma Pack "
3283 & "or use pragma Implicit_Packing", SZ);
3284 end if;
5a989c6b
AC
3285
3286 elsif RM_Size (E) = Len * Rsiz
3287 and then Implicit_Packing
3288 and then
3289 (Rsiz / System_Storage_Unit = 1
3290 or else Rsiz / System_Storage_Unit = 2
3291 or else Rsiz / System_Storage_Unit = 4)
3292 then
3293
3294 -- Not a packed array, but indicate the desired
3295 -- component size, for the back-end.
3296
3297 Set_Component_Size (Btyp, Rsiz);
86cde7b1
RD
3298 end if;
3299 end;
3300 end if;
3301 end if;
3302 end;
3303 end if;
3304
def46b54 3305 -- If ancestor subtype present, freeze that first. Note that this
8110ee3b 3306 -- will also get the base type frozen. Need RM reference ???
70482933
RK
3307
3308 Atype := Ancestor_Subtype (E);
3309
3310 if Present (Atype) then
c159409f 3311 Freeze_And_Append (Atype, N, Result);
70482933 3312
8110ee3b 3313 -- No ancestor subtype present
70482933 3314
8110ee3b
RD
3315 else
3316 -- See if we have a nearest ancestor that has a predicate.
3317 -- That catches the case of derived type with a predicate.
3318 -- Need RM reference here ???
3319
3320 Atype := Nearest_Ancestor (E);
3321
3322 if Present (Atype) and then Has_Predicates (Atype) then
3323 Freeze_And_Append (Atype, N, Result);
3324 end if;
3325
3326 -- Freeze base type before freezing the entity (RM 13.14(15))
3327
3328 if E /= Base_Type (E) then
3329 Freeze_And_Append (Base_Type (E), N, Result);
3330 end if;
70482933
RK
3331 end if;
3332
fbf5a39b 3333 -- For a derived type, freeze its parent type first (RM 13.14(15))
70482933
RK
3334
3335 elsif Is_Derived_Type (E) then
c159409f
AC
3336 Freeze_And_Append (Etype (E), N, Result);
3337 Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
70482933
RK
3338 end if;
3339
3340 -- For array type, freeze index types and component type first
fbf5a39b 3341 -- before freezing the array (RM 13.14(15)).
70482933
RK
3342
3343 if Is_Array_Type (E) then
3344 declare
094cefda
AC
3345 FS : constant Entity_Id := First_Subtype (E);
3346 Ctyp : constant Entity_Id := Component_Type (E);
3347 Clause : Entity_Id;
70482933
RK
3348
3349 Non_Standard_Enum : Boolean := False;
7d8b9c99
RD
3350 -- Set true if any of the index types is an enumeration type
3351 -- with a non-standard representation.
70482933
RK
3352
3353 begin
c159409f 3354 Freeze_And_Append (Ctyp, N, Result);
70482933
RK
3355
3356 Indx := First_Index (E);
3357 while Present (Indx) loop
c159409f 3358 Freeze_And_Append (Etype (Indx), N, Result);
70482933
RK
3359
3360 if Is_Enumeration_Type (Etype (Indx))
3361 and then Has_Non_Standard_Rep (Etype (Indx))
3362 then
3363 Non_Standard_Enum := True;
3364 end if;
3365
3366 Next_Index (Indx);
3367 end loop;
3368
07fc65c4 3369 -- Processing that is done only for base types
70482933
RK
3370
3371 if Ekind (E) = E_Array_Type then
07fc65c4
GB
3372
3373 -- Propagate flags for component type
3374
70482933
RK
3375 if Is_Controlled (Component_Type (E))
3376 or else Has_Controlled_Component (Ctyp)
3377 then
3378 Set_Has_Controlled_Component (E);
3379 end if;
3380
3381 if Has_Unchecked_Union (Component_Type (E)) then
3382 Set_Has_Unchecked_Union (E);
3383 end if;
70482933 3384
07fc65c4
GB
3385 -- If packing was requested or if the component size was set
3386 -- explicitly, then see if bit packing is required. This
3387 -- processing is only done for base types, since all the
3388 -- representation aspects involved are type-related. This
3389 -- is not just an optimization, if we start processing the
e14c931f 3390 -- subtypes, they interfere with the settings on the base
07fc65c4
GB
3391 -- type (this is because Is_Packed has a slightly different
3392 -- meaning before and after freezing).
70482933 3393
70482933
RK
3394 declare
3395 Csiz : Uint;
3396 Esiz : Uint;
3397
3398 begin
3399 if (Is_Packed (E) or else Has_Pragma_Pack (E))
70482933 3400 and then Known_Static_RM_Size (Ctyp)
094cefda 3401 and then not Has_Component_Size_Clause (E)
70482933
RK
3402 then
3403 Csiz := UI_Max (RM_Size (Ctyp), 1);
3404
3405 elsif Known_Component_Size (E) then
3406 Csiz := Component_Size (E);
3407
3408 elsif not Known_Static_Esize (Ctyp) then
3409 Csiz := Uint_0;
3410
3411 else
3412 Esiz := Esize (Ctyp);
3413
3414 -- We can set the component size if it is less than
3415 -- 16, rounding it up to the next storage unit size.
3416
3417 if Esiz <= 8 then
3418 Csiz := Uint_8;
3419 elsif Esiz <= 16 then
3420 Csiz := Uint_16;
3421 else
3422 Csiz := Uint_0;
3423 end if;
3424
7d8b9c99
RD
3425 -- Set component size up to match alignment if it
3426 -- would otherwise be less than the alignment. This
3427 -- deals with cases of types whose alignment exceeds
3428 -- their size (padded types).
70482933
RK
3429
3430 if Csiz /= 0 then
3431 declare
3432 A : constant Uint := Alignment_In_Bits (Ctyp);
70482933
RK
3433 begin
3434 if Csiz < A then
3435 Csiz := A;
3436 end if;
3437 end;
3438 end if;
70482933
RK
3439 end if;
3440
86cde7b1
RD
3441 -- Case of component size that may result in packing
3442
70482933 3443 if 1 <= Csiz and then Csiz <= 64 then
86cde7b1
RD
3444 declare
3445 Ent : constant Entity_Id :=
3446 First_Subtype (E);
3447 Pack_Pragma : constant Node_Id :=
3448 Get_Rep_Pragma (Ent, Name_Pack);
3449 Comp_Size_C : constant Node_Id :=
3450 Get_Attribute_Definition_Clause
3451 (Ent, Attribute_Component_Size);
3452 begin
3453 -- Warn if we have pack and component size so that
3454 -- the pack is ignored.
70482933 3455
86cde7b1
RD
3456 -- Note: here we must check for the presence of a
3457 -- component size before checking for a Pack pragma
3458 -- to deal with the case where the array type is a
3459 -- derived type whose parent is currently private.
3460
3461 if Present (Comp_Size_C)
3462 and then Has_Pragma_Pack (Ent)
094cefda 3463 and then Warn_On_Redundant_Constructs
86cde7b1
RD
3464 then
3465 Error_Msg_Sloc := Sloc (Comp_Size_C);
3466 Error_Msg_NE
3467 ("?pragma Pack for& ignored!",
3468 Pack_Pragma, Ent);
3469 Error_Msg_N
3470 ("\?explicit component size given#!",
3471 Pack_Pragma);
094cefda
AC
3472 Set_Is_Packed (Base_Type (Ent), False);
3473 Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
86cde7b1 3474 end if;
70482933 3475
86cde7b1
RD
3476 -- Set component size if not already set by a
3477 -- component size clause.
70482933 3478
86cde7b1
RD
3479 if not Present (Comp_Size_C) then
3480 Set_Component_Size (E, Csiz);
3481 end if;
fbf5a39b 3482
86cde7b1
RD
3483 -- Check for base type of 8, 16, 32 bits, where an
3484 -- unsigned subtype has a length one less than the
3485 -- base type (e.g. Natural subtype of Integer).
fbf5a39b 3486
86cde7b1
RD
3487 -- In such cases, if a component size was not set
3488 -- explicitly, then generate a warning.
fbf5a39b 3489
86cde7b1
RD
3490 if Has_Pragma_Pack (E)
3491 and then not Present (Comp_Size_C)
3492 and then
3493 (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
3494 and then Esize (Base_Type (Ctyp)) = Csiz + 1
3495 then
3496 Error_Msg_Uint_1 := Csiz;
3497
3498 if Present (Pack_Pragma) then
3499 Error_Msg_N
3500 ("?pragma Pack causes component size "
3501 & "to be ^!", Pack_Pragma);
3502 Error_Msg_N
3503 ("\?use Component_Size to set "
3504 & "desired value!", Pack_Pragma);
3505 end if;
fbf5a39b 3506 end if;
fbf5a39b 3507
86cde7b1
RD
3508 -- Actual packing is not needed for 8, 16, 32, 64.
3509 -- Also not needed for 24 if alignment is 1.
70482933 3510
86cde7b1
RD
3511 if Csiz = 8
3512 or else Csiz = 16
3513 or else Csiz = 32
3514 or else Csiz = 64
3515 or else (Csiz = 24 and then Alignment (Ctyp) = 1)
3516 then
3517 -- Here the array was requested to be packed,
3518 -- but the packing request had no effect, so
3519 -- Is_Packed is reset.
70482933 3520
86cde7b1
RD
3521 -- Note: semantically this means that we lose
3522 -- track of the fact that a derived type
3523 -- inherited a pragma Pack that was non-
3524 -- effective, but that seems fine.
70482933 3525
86cde7b1
RD
3526 -- We regard a Pack pragma as a request to set
3527 -- a representation characteristic, and this
3528 -- request may be ignored.
70482933 3529
094cefda
AC
3530 Set_Is_Packed (Base_Type (E), False);
3531 Set_Is_Bit_Packed_Array (Base_Type (E), False);
70482933 3532
094cefda
AC
3533 if Known_Static_Esize (Component_Type (E))
3534 and then Esize (Component_Type (E)) = Csiz
3535 then
3536 Set_Has_Non_Standard_Rep
3537 (Base_Type (E), False);
3538 end if;
3539
3540 -- In all other cases, packing is indeed needed
70482933 3541
86cde7b1 3542 else
094cefda
AC
3543 Set_Has_Non_Standard_Rep (Base_Type (E), True);
3544 Set_Is_Bit_Packed_Array (Base_Type (E), True);
3545 Set_Is_Packed (Base_Type (E), True);
86cde7b1
RD
3546 end if;
3547 end;
70482933
RK
3548 end if;
3549 end;
07fc65c4 3550
094cefda
AC
3551 -- Check for Atomic_Components or Aliased with unsuitable
3552 -- packing or explicit component size clause given.
3553
3554 if (Has_Atomic_Components (E)
3555 or else Has_Aliased_Components (E))
3556 and then (Has_Component_Size_Clause (E)
3557 or else Is_Packed (E))
3558 then
3559 Alias_Atomic_Check : declare
3560
3561 procedure Complain_CS (T : String);
3562 -- Outputs error messages for incorrect CS clause or
3563 -- pragma Pack for aliased or atomic components (T is
3564 -- "aliased" or "atomic");
3565
3566 -----------------
3567 -- Complain_CS --
3568 -----------------
3569
3570 procedure Complain_CS (T : String) is
3571 begin
3572 if Has_Component_Size_Clause (E) then
3573 Clause :=
3574 Get_Attribute_Definition_Clause
3575 (FS, Attribute_Component_Size);
3576
3577 if Known_Static_Esize (Ctyp) then
3578 Error_Msg_N
3579 ("incorrect component size for "
3580 & T & " components", Clause);
3581 Error_Msg_Uint_1 := Esize (Ctyp);
3582 Error_Msg_N
3583 ("\only allowed value is^", Clause);
3584
3585 else
3586 Error_Msg_N
3587 ("component size cannot be given for "
3588 & T & " components", Clause);
3589 end if;
3590
3591 else
3592 Error_Msg_N
3593 ("cannot pack " & T & " components",
3594 Get_Rep_Pragma (FS, Name_Pack));
3595 end if;
3596
3597 return;
3598 end Complain_CS;
3599
3600 -- Start of processing for Alias_Atomic_Check
3601
3602 begin
fc893455
AC
3603
3604 -- If object size of component type isn't known, we
3605 -- cannot be sure so we defer to the back end.
3606
3607 if not Known_Static_Esize (Ctyp) then
3608 null;
3609
84df40f7 3610 -- Case where component size has no effect. First
fc893455
AC
3611 -- check for object size of component type multiple
3612 -- of the storage unit size.
094cefda 3613
fc893455 3614 elsif Esize (Ctyp) mod System_Storage_Unit = 0
84df40f7
AC
3615
3616 -- OK in both packing case and component size case
3617 -- if RM size is known and static and the same as
3618 -- the object size.
3619
3620 and then
3621 ((Known_Static_RM_Size (Ctyp)
3622 and then Esize (Ctyp) = RM_Size (Ctyp))
3623
3624 -- Or if we have an explicit component size
3625 -- clause and the component size and object size
3626 -- are equal.
3627
3628 or else
3629 (Has_Component_Size_Clause (E)
3630 and then Component_Size (E) = Esize (Ctyp)))
094cefda
AC
3631 then
3632 null;
3633
3634 elsif Has_Aliased_Components (E)
3635 or else Is_Aliased (Ctyp)
3636 then
3637 Complain_CS ("aliased");
3638
3639 elsif Has_Atomic_Components (E)
3640 or else Is_Atomic (Ctyp)
3641 then
3642 Complain_CS ("atomic");
3643 end if;
3644 end Alias_Atomic_Check;
3645 end if;
3646
3647 -- Warn for case of atomic type
3648
3649 Clause := Get_Rep_Pragma (FS, Name_Atomic);
3650
3651 if Present (Clause)
3652 and then not Addressable (Component_Size (FS))
3653 then
3654 Error_Msg_NE
3655 ("non-atomic components of type& may not be "
3656 & "accessible by separate tasks?", Clause, E);
3657
3658 if Has_Component_Size_Clause (E) then
3659 Error_Msg_Sloc :=
3660 Sloc
3661 (Get_Attribute_Definition_Clause
3662 (FS, Attribute_Component_Size));
3663 Error_Msg_N
3664 ("\because of component size clause#?",
3665 Clause);
3666
3667 elsif Has_Pragma_Pack (E) then
3668 Error_Msg_Sloc :=
3669 Sloc (Get_Rep_Pragma (FS, Name_Pack));
3670 Error_Msg_N
3671 ("\because of pragma Pack#?", Clause);
3672 end if;
3673 end if;
3674
07fc65c4
GB
3675 -- Processing that is done only for subtypes
3676
3677 else
3678 -- Acquire alignment from base type
3679
3680 if Unknown_Alignment (E) then
3681 Set_Alignment (E, Alignment (Base_Type (E)));
7d8b9c99 3682 Adjust_Esize_Alignment (E);
07fc65c4
GB
3683 end if;
3684 end if;
3685
d05ef0ab
AC
3686 -- For bit-packed arrays, check the size
3687
75a64833 3688 if Is_Bit_Packed_Array (E) and then Known_RM_Size (E) then
d05ef0ab 3689 declare
67ce0d7e
RD
3690 SizC : constant Node_Id := Size_Clause (E);
3691
d05ef0ab 3692 Discard : Boolean;
67ce0d7e 3693 pragma Warnings (Off, Discard);
d05ef0ab
AC
3694
3695 begin
3696 -- It is not clear if it is possible to have no size
7d8b9c99
RD
3697 -- clause at this stage, but it is not worth worrying
3698 -- about. Post error on the entity name in the size
d05ef0ab
AC
3699 -- clause if present, else on the type entity itself.
3700
3701 if Present (SizC) then
7d8b9c99 3702 Check_Size (Name (SizC), E, RM_Size (E), Discard);
d05ef0ab 3703 else
7d8b9c99 3704 Check_Size (E, E, RM_Size (E), Discard);
d05ef0ab
AC
3705 end if;
3706 end;
3707 end if;
3708
2d4e0553
AC
3709 -- If any of the index types was an enumeration type with a
3710 -- non-standard rep clause, then we indicate that the array
3711 -- type is always packed (even if it is not bit packed).
70482933
RK
3712
3713 if Non_Standard_Enum then
3714 Set_Has_Non_Standard_Rep (Base_Type (E));
3715 Set_Is_Packed (Base_Type (E));
3716 end if;
70482933 3717
0da2c8ac 3718 Set_Component_Alignment_If_Not_Set (E);
70482933 3719
0da2c8ac
AC
3720 -- If the array is packed, we must create the packed array
3721 -- type to be used to actually implement the type. This is
3722 -- only needed for real array types (not for string literal
3723 -- types, since they are present only for the front end).
70482933 3724
0da2c8ac
AC
3725 if Is_Packed (E)
3726 and then Ekind (E) /= E_String_Literal_Subtype
3727 then
3728 Create_Packed_Array_Type (E);
c159409f 3729 Freeze_And_Append (Packed_Array_Type (E), N, Result);
70482933 3730
0da2c8ac 3731 -- Size information of packed array type is copied to the
fea9e956 3732 -- array type, since this is really the representation. But
def46b54
RD
3733 -- do not override explicit existing size values. If the
3734 -- ancestor subtype is constrained the packed_array_type
3735 -- will be inherited from it, but the size may have been
3736 -- provided already, and must not be overridden either.
fea9e956 3737
def46b54
RD
3738 if not Has_Size_Clause (E)
3739 and then
3740 (No (Ancestor_Subtype (E))
3741 or else not Has_Size_Clause (Ancestor_Subtype (E)))
3742 then
fea9e956
ES
3743 Set_Esize (E, Esize (Packed_Array_Type (E)));
3744 Set_RM_Size (E, RM_Size (Packed_Array_Type (E)));
3745 end if;
70482933 3746
fea9e956
ES
3747 if not Has_Alignment_Clause (E) then
3748 Set_Alignment (E, Alignment (Packed_Array_Type (E)));
3749 end if;
0da2c8ac
AC
3750 end if;
3751
def46b54
RD
3752 -- For non-packed arrays set the alignment of the array to the
3753 -- alignment of the component type if it is unknown. Skip this
3754 -- in atomic case (atomic arrays may need larger alignments).
0da2c8ac
AC
3755
3756 if not Is_Packed (E)
3757 and then Unknown_Alignment (E)
3758 and then Known_Alignment (Ctyp)
3759 and then Known_Static_Component_Size (E)
3760 and then Known_Static_Esize (Ctyp)
3761 and then Esize (Ctyp) = Component_Size (E)
3762 and then not Is_Atomic (E)
3763 then
3764 Set_Alignment (E, Alignment (Component_Type (E)));
3765 end if;
3766 end;
70482933 3767
fbf5a39b
AC
3768 -- For a class-wide type, the corresponding specific type is
3769 -- frozen as well (RM 13.14(15))
70482933
RK
3770
3771 elsif Is_Class_Wide_Type (E) then
c159409f 3772 Freeze_And_Append (Root_Type (E), N, Result);
70482933 3773
86cde7b1
RD
3774 -- If the base type of the class-wide type is still incomplete,
3775 -- the class-wide remains unfrozen as well. This is legal when
3776 -- E is the formal of a primitive operation of some other type
3777 -- which is being frozen.
3778
3779 if not Is_Frozen (Root_Type (E)) then
3780 Set_Is_Frozen (E, False);
3781 return Result;
3782 end if;
3783
70482933
RK
3784 -- If the Class_Wide_Type is an Itype (when type is the anonymous
3785 -- parent of a derived type) and it is a library-level entity,
3786 -- generate an itype reference for it. Otherwise, its first
3787 -- explicit reference may be in an inner scope, which will be
3788 -- rejected by the back-end.
3789
3790 if Is_Itype (E)
3791 and then Is_Compilation_Unit (Scope (E))
3792 then
70482933 3793 declare
fbf5a39b 3794 Ref : constant Node_Id := Make_Itype_Reference (Loc);
70482933
RK
3795
3796 begin
3797 Set_Itype (Ref, E);
90878b12 3798 Add_To_Result (Ref);
70482933
RK
3799 end;
3800 end if;
3801
def46b54 3802 -- The equivalent type associated with a class-wide subtype needs
cbae498b 3803 -- to be frozen to ensure that its layout is done.
fbf5a39b
AC
3804
3805 if Ekind (E) = E_Class_Wide_Subtype
3806 and then Present (Equivalent_Type (E))
3807 then
c159409f 3808 Freeze_And_Append (Equivalent_Type (E), N, Result);
fbf5a39b
AC
3809 end if;
3810
3811 -- For a record (sub)type, freeze all the component types (RM
def46b54
RD
3812 -- 13.14(15). We test for E_Record_(sub)Type here, rather than using
3813 -- Is_Record_Type, because we don't want to attempt the freeze for
3814 -- the case of a private type with record extension (we will do that
3815 -- later when the full type is frozen).
70482933
RK
3816
3817 elsif Ekind (E) = E_Record_Type
fd366a46 3818 or else Ekind (E) = E_Record_Subtype
70482933
RK
3819 then
3820 Freeze_Record_Type (E);
3821
3822 -- For a concurrent type, freeze corresponding record type. This
e14c931f 3823 -- does not correspond to any specific rule in the RM, but the
70482933
RK
3824 -- record type is essentially part of the concurrent type.
3825 -- Freeze as well all local entities. This includes record types
3826 -- created for entry parameter blocks, and whatever local entities
3827 -- may appear in the private part.
3828
3829 elsif Is_Concurrent_Type (E) then
3830 if Present (Corresponding_Record_Type (E)) then
3831 Freeze_And_Append
c159409f 3832 (Corresponding_Record_Type (E), N, Result);
70482933
RK
3833 end if;
3834
3835 Comp := First_Entity (E);
70482933
RK
3836 while Present (Comp) loop
3837 if Is_Type (Comp) then
c159409f 3838 Freeze_And_Append (Comp, N, Result);
70482933
RK
3839
3840 elsif (Ekind (Comp)) /= E_Function then
c6823a20
EB
3841 if Is_Itype (Etype (Comp))
3842 and then Underlying_Type (Scope (Etype (Comp))) = E
3843 then
3844 Undelay_Type (Etype (Comp));
3845 end if;
3846
c159409f 3847 Freeze_And_Append (Etype (Comp), N, Result);
70482933
RK
3848 end if;
3849
3850 Next_Entity (Comp);
3851 end loop;
3852
ee094616
RD
3853 -- Private types are required to point to the same freeze node as
3854 -- their corresponding full views. The freeze node itself has to
3855 -- point to the partial view of the entity (because from the partial
3856 -- view, we can retrieve the full view, but not the reverse).
3857 -- However, in order to freeze correctly, we need to freeze the full
3858 -- view. If we are freezing at the end of a scope (or within the
3859 -- scope of the private type), the partial and full views will have
3860 -- been swapped, the full view appears first in the entity chain and
3861 -- the swapping mechanism ensures that the pointers are properly set
3862 -- (on scope exit).
3863
3864 -- If we encounter the partial view before the full view (e.g. when
3865 -- freezing from another scope), we freeze the full view, and then
3866 -- set the pointers appropriately since we cannot rely on swapping to
3867 -- fix things up (subtypes in an outer scope might not get swapped).
70482933
RK
3868
3869 elsif Is_Incomplete_Or_Private_Type (E)
3870 and then not Is_Generic_Type (E)
3871 then
86cde7b1
RD
3872 -- The construction of the dispatch table associated with library
3873 -- level tagged types forces freezing of all the primitives of the
3874 -- type, which may cause premature freezing of the partial view.
3875 -- For example:
3876
3877 -- package Pkg is
3878 -- type T is tagged private;
3879 -- type DT is new T with private;
3e24afaa 3880 -- procedure Prim (X : in out T; Y : in out DT'Class);
86cde7b1
RD
3881 -- private
3882 -- type T is tagged null record;
3883 -- Obj : T;
3884 -- type DT is new T with null record;
3885 -- end;
3886
3887 -- In this case the type will be frozen later by the usual
3888 -- mechanism: an object declaration, an instantiation, or the
3889 -- end of a declarative part.
3890
3891 if Is_Library_Level_Tagged_Type (E)
3892 and then not Present (Full_View (E))
3893 then
3894 Set_Is_Frozen (E, False);
3895 return Result;
3896
70482933
RK
3897 -- Case of full view present
3898
86cde7b1 3899 elsif Present (Full_View (E)) then
70482933 3900
ee094616
RD
3901 -- If full view has already been frozen, then no further
3902 -- processing is required
70482933
RK
3903
3904 if Is_Frozen (Full_View (E)) then
70482933
RK
3905 Set_Has_Delayed_Freeze (E, False);
3906 Set_Freeze_Node (E, Empty);
3907 Check_Debug_Info_Needed (E);
3908
ee094616
RD
3909 -- Otherwise freeze full view and patch the pointers so that
3910 -- the freeze node will elaborate both views in the back-end.
70482933
RK
3911
3912 else
fbf5a39b
AC
3913 declare
3914 Full : constant Entity_Id := Full_View (E);
70482933 3915
fbf5a39b
AC
3916 begin
3917 if Is_Private_Type (Full)
3918 and then Present (Underlying_Full_View (Full))
3919 then
3920 Freeze_And_Append
c159409f 3921 (Underlying_Full_View (Full), N, Result);
fbf5a39b 3922 end if;
70482933 3923
c159409f 3924 Freeze_And_Append (Full, N, Result);
70482933 3925
fbf5a39b
AC
3926 if Has_Delayed_Freeze (E) then
3927 F_Node := Freeze_Node (Full);
70482933 3928
fbf5a39b
AC
3929 if Present (F_Node) then
3930 Set_Freeze_Node (E, F_Node);
3931 Set_Entity (F_Node, E);
3932
3933 else
def46b54
RD
3934 -- {Incomplete,Private}_Subtypes with Full_Views
3935 -- constrained by discriminants.
fbf5a39b
AC
3936
3937 Set_Has_Delayed_Freeze (E, False);
3938 Set_Freeze_Node (E, Empty);
3939 end if;
70482933 3940 end if;
fbf5a39b 3941 end;
70482933
RK
3942
3943 Check_Debug_Info_Needed (E);
3944 end if;
3945
ee094616
RD
3946 -- AI-117 requires that the convention of a partial view be the
3947 -- same as the convention of the full view. Note that this is a
3948 -- recognized breach of privacy, but it's essential for logical
3949 -- consistency of representation, and the lack of a rule in
3950 -- RM95 was an oversight.
70482933
RK
3951
3952 Set_Convention (E, Convention (Full_View (E)));
3953
3954 Set_Size_Known_At_Compile_Time (E,
3955 Size_Known_At_Compile_Time (Full_View (E)));
3956
3957 -- Size information is copied from the full view to the
def46b54 3958 -- incomplete or private view for consistency.
70482933 3959
ee094616
RD
3960 -- We skip this is the full view is not a type. This is very
3961 -- strange of course, and can only happen as a result of
3962 -- certain illegalities, such as a premature attempt to derive
3963 -- from an incomplete type.
70482933
RK
3964
3965 if Is_Type (Full_View (E)) then
3966 Set_Size_Info (E, Full_View (E));
3967 Set_RM_Size (E, RM_Size (Full_View (E)));
3968 end if;
3969
3970 return Result;
3971
3972 -- Case of no full view present. If entity is derived or subtype,
3973 -- it is safe to freeze, correctness depends on the frozen status
3974 -- of parent. Otherwise it is either premature usage, or a Taft
3975 -- amendment type, so diagnosis is at the point of use and the
3976 -- type might be frozen later.
3977
3978 elsif E /= Base_Type (E)
3979 or else Is_Derived_Type (E)
3980 then
3981 null;
3982
3983 else
3984 Set_Is_Frozen (E, False);
3985 return No_List;
3986 end if;
3987
3988 -- For access subprogram, freeze types of all formals, the return
3989 -- type was already frozen, since it is the Etype of the function.
8aec446b 3990 -- Formal types can be tagged Taft amendment types, but otherwise
205c14b0 3991 -- they cannot be incomplete.
70482933
RK
3992
3993 elsif Ekind (E) = E_Subprogram_Type then
3994 Formal := First_Formal (E);
3995 while Present (Formal) loop
8aec446b
AC
3996 if Ekind (Etype (Formal)) = E_Incomplete_Type
3997 and then No (Full_View (Etype (Formal)))
3998 and then not Is_Value_Type (Etype (Formal))
3999 then
4000 if Is_Tagged_Type (Etype (Formal)) then
4001 null;
dd386db0 4002
3cae7f14 4003 -- AI05-151: Incomplete types are allowed in access to
dd386db0
AC
4004 -- subprogram specifications.
4005
4006 elsif Ada_Version < Ada_2012 then
8aec446b
AC
4007 Error_Msg_NE
4008 ("invalid use of incomplete type&", E, Etype (Formal));
4009 end if;
4010 end if;
4011
c159409f 4012 Freeze_And_Append (Etype (Formal), N, Result);
70482933
RK
4013 Next_Formal (Formal);
4014 end loop;
4015
70482933
RK
4016 Freeze_Subprogram (E);
4017
ee094616
RD
4018 -- For access to a protected subprogram, freeze the equivalent type
4019 -- (however this is not set if we are not generating code or if this
4020 -- is an anonymous type used just for resolution).
70482933 4021
fea9e956 4022 elsif Is_Access_Protected_Subprogram_Type (E) then
57747aec 4023 if Present (Equivalent_Type (E)) then
c159409f 4024 Freeze_And_Append (Equivalent_Type (E), N, Result);
d8db0bca 4025 end if;
70482933
RK
4026 end if;
4027
4028 -- Generic types are never seen by the back-end, and are also not
4029 -- processed by the expander (since the expander is turned off for
4030 -- generic processing), so we never need freeze nodes for them.
4031
4032 if Is_Generic_Type (E) then
4033 return Result;
4034 end if;
4035
4036 -- Some special processing for non-generic types to complete
4037 -- representation details not known till the freeze point.
4038
4039 if Is_Fixed_Point_Type (E) then
4040 Freeze_Fixed_Point_Type (E);
4041
ee094616
RD
4042 -- Some error checks required for ordinary fixed-point type. Defer
4043 -- these till the freeze-point since we need the small and range
4044 -- values. We only do these checks for base types
fbf5a39b 4045
d347f572 4046 if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
fbf5a39b
AC
4047 if Small_Value (E) < Ureal_2_M_80 then
4048 Error_Msg_Name_1 := Name_Small;
4049 Error_Msg_N
7d8b9c99 4050 ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
fbf5a39b
AC
4051
4052 elsif Small_Value (E) > Ureal_2_80 then
4053 Error_Msg_Name_1 := Name_Small;
4054 Error_Msg_N
7d8b9c99 4055 ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
fbf5a39b
AC
4056 end if;
4057
4058 if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
4059 Error_Msg_Name_1 := Name_First;
4060 Error_Msg_N
7d8b9c99 4061 ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
fbf5a39b
AC
4062 end if;
4063
4064 if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
4065 Error_Msg_Name_1 := Name_Last;
4066 Error_Msg_N
7d8b9c99 4067 ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
fbf5a39b
AC
4068 end if;
4069 end if;
4070
70482933
RK
4071 elsif Is_Enumeration_Type (E) then
4072 Freeze_Enumeration_Type (E);
4073
4074 elsif Is_Integer_Type (E) then
4075 Adjust_Esize_For_Alignment (E);
4076
79afa047
AC
4077 if Is_Modular_Integer_Type (E)
4078 and then Warn_On_Suspicious_Modulus_Value
4079 then
67b3acf8
RD
4080 Check_Suspicious_Modulus (E);
4081 end if;
4082
edd63e9b
ES
4083 elsif Is_Access_Type (E) then
4084
fab2daeb
AC
4085 -- If a pragma Default_Storage_Pool applies, and this type has no
4086 -- Storage_Pool or Storage_Size clause (which must have occurred
4087 -- before the freezing point), then use the default. This applies
4088 -- only to base types.
4089
4090 if Present (Default_Pool)
d347f572 4091 and then Is_Base_Type (E)
fab2daeb
AC
4092 and then not Has_Storage_Size_Clause (E)
4093 and then No (Associated_Storage_Pool (E))
4094 then
4095 -- Case of pragma Default_Storage_Pool (null)
4096
4097 if Nkind (Default_Pool) = N_Null then
4098 Set_No_Pool_Assigned (E);
4099
4100 -- Case of pragma Default_Storage_Pool (storage_pool_NAME)
4101
4102 else
4103 Set_Associated_Storage_Pool (E, Entity (Default_Pool));
4104 end if;
4105 end if;
4106
edd63e9b
ES
4107 -- Check restriction for standard storage pool
4108
4109 if No (Associated_Storage_Pool (E)) then
4110 Check_Restriction (No_Standard_Storage_Pools, E);
4111 end if;
4112
4113 -- Deal with error message for pure access type. This is not an
4114 -- error in Ada 2005 if there is no pool (see AI-366).
4115
4116 if Is_Pure_Unit_Access_Type (E)
0791fbe9 4117 and then (Ada_Version < Ada_2005
2c1b72d7 4118 or else not No_Pool_Assigned (E))
edd63e9b
ES
4119 then
4120 Error_Msg_N ("named access type not allowed in pure unit", E);
c6a9797e 4121
0791fbe9 4122 if Ada_Version >= Ada_2005 then
c6a9797e
RD
4123 Error_Msg_N
4124 ("\would be legal if Storage_Size of 0 given?", E);
4125
4126 elsif No_Pool_Assigned (E) then
4127 Error_Msg_N
4128 ("\would be legal in Ada 2005?", E);
4129
4130 else
4131 Error_Msg_N
4132 ("\would be legal in Ada 2005 if "
4133 & "Storage_Size of 0 given?", E);
4134 end if;
edd63e9b 4135 end if;
70482933
RK
4136 end if;
4137
edd63e9b
ES
4138 -- Case of composite types
4139
70482933
RK
4140 if Is_Composite_Type (E) then
4141
edd63e9b
ES
4142 -- AI-117 requires that all new primitives of a tagged type must
4143 -- inherit the convention of the full view of the type. Inherited
4144 -- and overriding operations are defined to inherit the convention
4145 -- of their parent or overridden subprogram (also specified in
ee094616
RD
4146 -- AI-117), which will have occurred earlier (in Derive_Subprogram
4147 -- and New_Overloaded_Entity). Here we set the convention of
4148 -- primitives that are still convention Ada, which will ensure
def46b54
RD
4149 -- that any new primitives inherit the type's convention. Class-
4150 -- wide types can have a foreign convention inherited from their
4151 -- specific type, but are excluded from this since they don't have
4152 -- any associated primitives.
70482933
RK
4153
4154 if Is_Tagged_Type (E)
4155 and then not Is_Class_Wide_Type (E)
4156 and then Convention (E) /= Convention_Ada
4157 then
4158 declare
4159 Prim_List : constant Elist_Id := Primitive_Operations (E);
07fc65c4 4160 Prim : Elmt_Id;
3cae7f14 4161
70482933 4162 begin
07fc65c4 4163 Prim := First_Elmt (Prim_List);
70482933
RK
4164 while Present (Prim) loop
4165 if Convention (Node (Prim)) = Convention_Ada then
4166 Set_Convention (Node (Prim), Convention (E));
4167 end if;
4168
4169 Next_Elmt (Prim);
4170 end loop;
4171 end;
4172 end if;
a8551b5f
AC
4173
4174 -- If the type is a simple storage pool type, then this is where
4175 -- we attempt to locate and validate its Allocate, Deallocate, and
4176 -- Storage_Size operations (the first is required, and the latter
4177 -- two are optional). We also verify that the full type for a
4178 -- private type is allowed to be a simple storage pool type.
4179
f6205414 4180 if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
a8551b5f
AC
4181 and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
4182 then
a8551b5f
AC
4183 -- If the type is marked Has_Private_Declaration, then this is
4184 -- a full type for a private type that was specified with the
f6205414 4185 -- pragma Simple_Storage_Pool_Type, and here we ensure that the
a8551b5f
AC
4186 -- pragma is allowed for the full type (for example, it can't
4187 -- be an array type, or a nonlimited record type).
4188
4189 if Has_Private_Declaration (E) then
4190 if (not Is_Record_Type (E)
4191 or else not Is_Immutably_Limited_Type (E))
4192 and then not Is_Private_Type (E)
4193 then
f6205414 4194 Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
a8551b5f
AC
4195 Error_Msg_N
4196 ("pragma% can only apply to full type that is an " &
4197 "explicitly limited type", E);
4198 end if;
4199 end if;
4200
4201 Validate_Simple_Pool_Ops : declare
4202 Pool_Type : Entity_Id renames E;
4203 Address_Type : constant Entity_Id := RTE (RE_Address);
4204 Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
4205
4206 procedure Validate_Simple_Pool_Op_Formal
4207 (Pool_Op : Entity_Id;
4208 Pool_Op_Formal : in out Entity_Id;
4209 Expected_Mode : Formal_Kind;
4210 Expected_Type : Entity_Id;
4211 Formal_Name : String;
4212 OK_Formal : in out Boolean);
4213 -- Validate one formal Pool_Op_Formal of the candidate pool
4214 -- operation Pool_Op. The formal must be of Expected_Type
4215 -- and have mode Expected_Mode. OK_Formal will be set to
4216 -- False if the formal doesn't match. If OK_Formal is False
4217 -- on entry, then the formal will effectively be ignored
4218 -- (because validation of the pool op has already failed).
4219 -- Upon return, Pool_Op_Formal will be updated to the next
4220 -- formal, if any.
4221
4222 procedure Validate_Simple_Pool_Operation (Op_Name : Name_Id);
4223 -- Search for and validate a simple pool operation with the
4224 -- name Op_Name. If the name is Allocate, then there must be
4225 -- exactly one such primitive operation for the simple pool
4226 -- type. If the name is Deallocate or Storage_Size, then
4227 -- there can be at most one such primitive operation. The
4228 -- profile of the located primitive must conform to what
4229 -- is expected for each operation.
4230
4231 ------------------------------------
4232 -- Validate_Simple_Pool_Op_Formal --
4233 ------------------------------------
4234
4235 procedure Validate_Simple_Pool_Op_Formal
4236 (Pool_Op : Entity_Id;
4237 Pool_Op_Formal : in out Entity_Id;
4238 Expected_Mode : Formal_Kind;
4239 Expected_Type : Entity_Id;
4240 Formal_Name : String;
4241 OK_Formal : in out Boolean)
4242 is
4243 begin
4244 -- If OK_Formal is False on entry, then simply ignore
4245 -- the formal, because an earlier formal has already
4246 -- been flagged.
4247
4248 if not OK_Formal then
4249 return;
4250
4251 -- If no formal is passed in, then issue an error for a
4252 -- missing formal.
4253
4254 elsif not Present (Pool_Op_Formal) then
4255 Error_Msg_NE
4256 ("simple storage pool op missing formal " &
4257 Formal_Name & " of type&", Pool_Op, Expected_Type);
4258 OK_Formal := False;
4259
4260 return;
4261 end if;
4262
4263 if Etype (Pool_Op_Formal) /= Expected_Type then
260359e3 4264
a8551b5f
AC
4265 -- If the pool type was expected for this formal, then
4266 -- this will not be considered a candidate operation
4267 -- for the simple pool, so we unset OK_Formal so that
4268 -- the op and any later formals will be ignored.
4269
4270 if Expected_Type = Pool_Type then
4271 OK_Formal := False;
4272
4273 return;
4274
4275 else
4276 Error_Msg_NE
4277 ("wrong type for formal " & Formal_Name &
4278 " of simple storage pool op; expected type&",
4279 Pool_Op_Formal, Expected_Type);
4280 end if;
4281 end if;
4282
4283 -- Issue error if formal's mode is not the expected one
4284
4285 if Ekind (Pool_Op_Formal) /= Expected_Mode then
4286 Error_Msg_N
4287 ("wrong mode for formal of simple storage pool op",
4288 Pool_Op_Formal);
4289 end if;
4290
4291 -- Advance to the next formal
4292
4293 Next_Formal (Pool_Op_Formal);
4294 end Validate_Simple_Pool_Op_Formal;
4295
4296 ------------------------------------
4297 -- Validate_Simple_Pool_Operation --
4298 ------------------------------------
4299
4300 procedure Validate_Simple_Pool_Operation
4301 (Op_Name : Name_Id)
4302 is
4303 Op : Entity_Id;
4304 Found_Op : Entity_Id := Empty;
4305 Formal : Entity_Id;
4306 Is_OK : Boolean;
4307
4308 begin
4309 pragma Assert
4310 (Op_Name = Name_Allocate
260359e3
AC
4311 or else Op_Name = Name_Deallocate
4312 or else Op_Name = Name_Storage_Size);
a8551b5f
AC
4313
4314 Error_Msg_Name_1 := Op_Name;
4315
4316 -- For each homonym declared immediately in the scope
4317 -- of the simple storage pool type, determine whether
4318 -- the homonym is an operation of the pool type, and,
4319 -- if so, check that its profile is as expected for
4320 -- a simple pool operation of that name.
4321
4322 Op := Get_Name_Entity_Id (Op_Name);
4323 while Present (Op) loop
4324 if Ekind_In (Op, E_Function, E_Procedure)
4325 and then Scope (Op) = Current_Scope
4326 then
4327 Formal := First_Entity (Op);
4328
4329 Is_OK := True;
4330
4331 -- The first parameter must be of the pool type
4332 -- in order for the operation to qualify.
4333
4334 if Op_Name = Name_Storage_Size then
4335 Validate_Simple_Pool_Op_Formal
4336 (Op, Formal, E_In_Parameter, Pool_Type,
4337 "Pool", Is_OK);
a8551b5f
AC
4338 else
4339 Validate_Simple_Pool_Op_Formal
4340 (Op, Formal, E_In_Out_Parameter, Pool_Type,
4341 "Pool", Is_OK);
4342 end if;
4343
4344 -- If another operation with this name has already
4345 -- been located for the type, then flag an error,
4346 -- since we only allow the type to have a single
4347 -- such primitive.
4348
4349 if Present (Found_Op) and then Is_OK then
4350 Error_Msg_NE
4351 ("only one % operation allowed for " &
4352 "simple storage pool type&", Op, Pool_Type);
4353 end if;
4354
4355 -- In the case of Allocate and Deallocate, a formal
4356 -- of type System.Address is required.
4357
4358 if Op_Name = Name_Allocate then
4359 Validate_Simple_Pool_Op_Formal
4360 (Op, Formal, E_Out_Parameter,
4361 Address_Type, "Storage_Address", Is_OK);
a8551b5f
AC
4362 elsif Op_Name = Name_Deallocate then
4363 Validate_Simple_Pool_Op_Formal
4364 (Op, Formal, E_In_Parameter,
4365 Address_Type, "Storage_Address", Is_OK);
4366 end if;
4367
4368 -- In the case of Allocate and Deallocate, formals
4369 -- of type Storage_Count are required as the third
4370 -- and fourth parameters.
4371
4372 if Op_Name /= Name_Storage_Size then
4373 Validate_Simple_Pool_Op_Formal
4374 (Op, Formal, E_In_Parameter,
4375 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
a8551b5f
AC
4376 Validate_Simple_Pool_Op_Formal
4377 (Op, Formal, E_In_Parameter,
4378 Stg_Cnt_Type, "Alignment", Is_OK);
4379 end if;
4380
4381 -- If no mismatched formals have been found (Is_OK)
4382 -- and no excess formals are present, then this
4383 -- operation has been validated, so record it.
4384
4385 if not Present (Formal) and then Is_OK then
4386 Found_Op := Op;
4387 end if;
4388 end if;
4389
4390 Op := Homonym (Op);
4391 end loop;
4392
4393 -- There must be a valid Allocate operation for the type,
4394 -- so issue an error if none was found.
4395
4396 if Op_Name = Name_Allocate
4397 and then not Present (Found_Op)
4398 then
4399 Error_Msg_N ("missing % operation for simple " &
4400 "storage pool type", Pool_Type);
4401
4402 elsif Present (Found_Op) then
260359e3 4403
a8551b5f
AC
4404 -- Simple pool operations can't be abstract
4405
4406 if Is_Abstract_Subprogram (Found_Op) then
4407 Error_Msg_N
4408 ("simple storage pool operation must not be " &
4409 "abstract", Found_Op);
4410 end if;
4411
4412 -- The Storage_Size operation must be a function with
4413 -- Storage_Count as its result type.
4414
4415 if Op_Name = Name_Storage_Size then
4416 if Ekind (Found_Op) = E_Procedure then
4417 Error_Msg_N
4418 ("% operation must be a function", Found_Op);
4419
4420 elsif Etype (Found_Op) /= Stg_Cnt_Type then
4421 Error_Msg_NE
4422 ("wrong result type for%, expected type&",
4423 Found_Op, Stg_Cnt_Type);
4424 end if;
4425
4426 -- Allocate and Deallocate must be procedures
4427
4428 elsif Ekind (Found_Op) = E_Function then
4429 Error_Msg_N
4430 ("% operation must be a procedure", Found_Op);
4431 end if;
4432 end if;
4433 end Validate_Simple_Pool_Operation;
4434
4435 -- Start of processing for Validate_Simple_Pool_Ops
4436
4437 begin
4438 Validate_Simple_Pool_Operation (Name_Allocate);
a8551b5f 4439 Validate_Simple_Pool_Operation (Name_Deallocate);
a8551b5f
AC
4440 Validate_Simple_Pool_Operation (Name_Storage_Size);
4441 end Validate_Simple_Pool_Ops;
4442 end if;
70482933
RK
4443 end if;
4444
ee094616
RD
4445 -- Now that all types from which E may depend are frozen, see if the
4446 -- size is known at compile time, if it must be unsigned, or if
7d8b9c99 4447 -- strict alignment is required
70482933
RK
4448
4449 Check_Compile_Time_Size (E);
4450 Check_Unsigned_Type (E);
4451
4452 if Base_Type (E) = E then
4453 Check_Strict_Alignment (E);
4454 end if;
4455
4456 -- Do not allow a size clause for a type which does not have a size
4457 -- that is known at compile time
4458
4459 if Has_Size_Clause (E)
4460 and then not Size_Known_At_Compile_Time (E)
4461 then
e14c931f 4462 -- Suppress this message if errors posted on E, even if we are
07fc65c4
GB
4463 -- in all errors mode, since this is often a junk message
4464
4465 if not Error_Posted (E) then
4466 Error_Msg_N
4467 ("size clause not allowed for variable length type",
4468 Size_Clause (E));
4469 end if;
70482933
RK
4470 end if;
4471
a01b9df6
AC
4472 -- Now we set/verify the representation information, in particular
4473 -- the size and alignment values. This processing is not required for
4474 -- generic types, since generic types do not play any part in code
4475 -- generation, and so the size and alignment values for such types
84c4181d
AC
4476 -- are irrelevant. Ditto for types declared within a generic unit,
4477 -- which may have components that depend on generic parameters, and
4478 -- that will be recreated in an instance.
70482933 4479
84c4181d
AC
4480 if Inside_A_Generic then
4481 null;
70482933
RK
4482
4483 -- Otherwise we call the layout procedure
4484
4485 else
4486 Layout_Type (E);
4487 end if;
a01b9df6 4488
cc570be6
AC
4489 -- If this is an access to subprogram whose designated type is itself
4490 -- a subprogram type, the return type of this anonymous subprogram
4491 -- type must be decorated as well.
4492
4493 if Ekind (E) = E_Anonymous_Access_Subprogram_Type
4494 and then Ekind (Designated_Type (E)) = E_Subprogram_Type
4495 then
4496 Layout_Type (Etype (Designated_Type (E)));
4497 end if;
4498
a01b9df6
AC
4499 -- If the type has a Defaut_Value/Default_Component_Value aspect,
4500 -- this is where we analye the expression (after the type is frozen,
4501 -- since in the case of Default_Value, we are analyzing with the
4502 -- type itself, and we treat Default_Component_Value similarly for
4503 -- the sake of uniformity.
4504
4505 if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
4506 declare
37da997b
RD
4507 Nam : Name_Id;
4508 Exp : Node_Id;
4509 Typ : Entity_Id;
a01b9df6
AC
4510
4511 begin
4512 if Is_Scalar_Type (E) then
4513 Nam := Name_Default_Value;
4514 Typ := E;
6d9e03cb 4515 Exp := Default_Aspect_Value (Typ);
a01b9df6
AC
4516 else
4517 Nam := Name_Default_Component_Value;
4518 Typ := Component_Type (E);
37da997b 4519 Exp := Default_Aspect_Component_Value (E);
a01b9df6
AC
4520 end if;
4521
a01b9df6
AC
4522 Analyze_And_Resolve (Exp, Typ);
4523
4524 if Etype (Exp) /= Any_Type then
4525 if not Is_Static_Expression (Exp) then
4526 Error_Msg_Name_1 := Nam;
4527 Flag_Non_Static_Expr
4528 ("aspect% requires static expression", Exp);
4529 end if;
4530 end if;
4531 end;
4532 end if;
70482933
RK
4533
4534 -- End of freeze processing for type entities
4535 end if;
4536
4537 -- Here is where we logically freeze the current entity. If it has a
4538 -- freeze node, then this is the point at which the freeze node is
4539 -- linked into the result list.
4540
4541 if Has_Delayed_Freeze (E) then
4542
4543 -- If a freeze node is already allocated, use it, otherwise allocate
4544 -- a new one. The preallocation happens in the case of anonymous base
4545 -- types, where we preallocate so that we can set First_Subtype_Link.
4546 -- Note that we reset the Sloc to the current freeze location.
4547
4548 if Present (Freeze_Node (E)) then
4549 F_Node := Freeze_Node (E);
4550 Set_Sloc (F_Node, Loc);
4551
4552 else
4553 F_Node := New_Node (N_Freeze_Entity, Loc);
4554 Set_Freeze_Node (E, F_Node);
4555 Set_Access_Types_To_Process (F_Node, No_Elist);
4556 Set_TSS_Elist (F_Node, No_Elist);
4557 Set_Actions (F_Node, No_List);
4558 end if;
4559
4560 Set_Entity (F_Node, E);
90878b12 4561 Add_To_Result (F_Node);
35ae2ed8
AC
4562
4563 -- A final pass over record types with discriminants. If the type
4564 -- has an incomplete declaration, there may be constrained access
4565 -- subtypes declared elsewhere, which do not depend on the discrimi-
4566 -- nants of the type, and which are used as component types (i.e.
4567 -- the full view is a recursive type). The designated types of these
4568 -- subtypes can only be elaborated after the type itself, and they
4569 -- need an itype reference.
4570
4571 if Ekind (E) = E_Record_Type
4572 and then Has_Discriminants (E)
4573 then
4574 declare
4575 Comp : Entity_Id;
4576 IR : Node_Id;
4577 Typ : Entity_Id;
4578
4579 begin
4580 Comp := First_Component (E);
35ae2ed8
AC
4581 while Present (Comp) loop
4582 Typ := Etype (Comp);
4583
4584 if Ekind (Comp) = E_Component
4585 and then Is_Access_Type (Typ)
4586 and then Scope (Typ) /= E
4587 and then Base_Type (Designated_Type (Typ)) = E
4588 and then Is_Itype (Designated_Type (Typ))
4589 then
4590 IR := Make_Itype_Reference (Sloc (Comp));
4591 Set_Itype (IR, Designated_Type (Typ));
4592 Append (IR, Result);
4593 end if;
4594
4595 Next_Component (Comp);
4596 end loop;
4597 end;
4598 end if;
70482933
RK
4599 end if;
4600
4601 -- When a type is frozen, the first subtype of the type is frozen as
4602 -- well (RM 13.14(15)). This has to be done after freezing the type,
4603 -- since obviously the first subtype depends on its own base type.
4604
4605 if Is_Type (E) then
c159409f 4606 Freeze_And_Append (First_Subtype (E), N, Result);
70482933
RK
4607
4608 -- If we just froze a tagged non-class wide record, then freeze the
4609 -- corresponding class-wide type. This must be done after the tagged
4610 -- type itself is frozen, because the class-wide type refers to the
4611 -- tagged type which generates the class.
4612
4613 if Is_Tagged_Type (E)
4614 and then not Is_Class_Wide_Type (E)
4615 and then Present (Class_Wide_Type (E))
4616 then
c159409f 4617 Freeze_And_Append (Class_Wide_Type (E), N, Result);
70482933
RK
4618 end if;
4619 end if;
4620
4621 Check_Debug_Info_Needed (E);
4622
4623 -- Special handling for subprograms
4624
4625 if Is_Subprogram (E) then
4626
4627 -- If subprogram has address clause then reset Is_Public flag, since
4628 -- we do not want the backend to generate external references.
4629
4630 if Present (Address_Clause (E))
4631 and then not Is_Library_Level_Entity (E)
4632 then
4633 Set_Is_Public (E, False);
4634
4635 -- If no address clause and not intrinsic, then for imported
4636 -- subprogram in main unit, generate descriptor if we are in
4637 -- Propagate_Exceptions mode.
4638
90878b12
AC
4639 -- This is very odd code, it makes a null result, why ???
4640
70482933
RK
4641 elsif Propagate_Exceptions
4642 and then Is_Imported (E)
4643 and then not Is_Intrinsic_Subprogram (E)
4644 and then Convention (E) /= Convention_Stubbed
4645 then
4646 if Result = No_List then
4647 Result := Empty_List;
4648 end if;
70482933 4649 end if;
70482933
RK
4650 end if;
4651
4652 return Result;
4653 end Freeze_Entity;
4654
4655 -----------------------------
4656 -- Freeze_Enumeration_Type --
4657 -----------------------------
4658
4659 procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
4660 begin
d677afa9
ES
4661 -- By default, if no size clause is present, an enumeration type with
4662 -- Convention C is assumed to interface to a C enum, and has integer
4663 -- size. This applies to types. For subtypes, verify that its base
be482a8c
AC
4664 -- type has no size clause either. Treat other foreign conventions
4665 -- in the same way, and also make sure alignment is set right.
d677afa9 4666
70482933
RK
4667 if Has_Foreign_Convention (Typ)
4668 and then not Has_Size_Clause (Typ)
d677afa9 4669 and then not Has_Size_Clause (Base_Type (Typ))
70482933
RK
4670 and then Esize (Typ) < Standard_Integer_Size
4671 then
4672 Init_Esize (Typ, Standard_Integer_Size);
be482a8c 4673 Set_Alignment (Typ, Alignment (Standard_Integer));
d677afa9 4674
70482933 4675 else
d677afa9
ES
4676 -- If the enumeration type interfaces to C, and it has a size clause
4677 -- that specifies less than int size, it warrants a warning. The
4678 -- user may intend the C type to be an enum or a char, so this is
4679 -- not by itself an error that the Ada compiler can detect, but it
4680 -- it is a worth a heads-up. For Boolean and Character types we
4681 -- assume that the programmer has the proper C type in mind.
4682
4683 if Convention (Typ) = Convention_C
4684 and then Has_Size_Clause (Typ)
4685 and then Esize (Typ) /= Esize (Standard_Integer)
4686 and then not Is_Boolean_Type (Typ)
4687 and then not Is_Character_Type (Typ)
4688 then
4689 Error_Msg_N
4690 ("C enum types have the size of a C int?", Size_Clause (Typ));
4691 end if;
4692
70482933
RK
4693 Adjust_Esize_For_Alignment (Typ);
4694 end if;
4695 end Freeze_Enumeration_Type;
4696
4697 -----------------------
4698 -- Freeze_Expression --
4699 -----------------------
4700
4701 procedure Freeze_Expression (N : Node_Id) is
c6a9797e
RD
4702 In_Spec_Exp : constant Boolean := In_Spec_Expression;
4703 Typ : Entity_Id;
4704 Nam : Entity_Id;
4705 Desig_Typ : Entity_Id;
4706 P : Node_Id;
4707 Parent_P : Node_Id;
70482933
RK
4708
4709 Freeze_Outside : Boolean := False;
4710 -- This flag is set true if the entity must be frozen outside the
4711 -- current subprogram. This happens in the case of expander generated
4712 -- subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
4713 -- not freeze all entities like other bodies, but which nevertheless
4714 -- may reference entities that have to be frozen before the body and
4715 -- obviously cannot be frozen inside the body.
4716
4717 function In_Exp_Body (N : Node_Id) return Boolean;
4718 -- Given an N_Handled_Sequence_Of_Statements node N, determines whether
c6823a20 4719 -- it is the handled statement sequence of an expander-generated
7d8b9c99
RD
4720 -- subprogram (init proc, stream subprogram, or renaming as body).
4721 -- If so, this is not a freezing context.
70482933 4722
fbf5a39b
AC
4723 -----------------
4724 -- In_Exp_Body --
4725 -----------------
4726
70482933 4727 function In_Exp_Body (N : Node_Id) return Boolean is
7d8b9c99
RD
4728 P : Node_Id;
4729 Id : Entity_Id;
70482933
RK
4730
4731 begin
4732 if Nkind (N) = N_Subprogram_Body then
4733 P := N;
4734 else
4735 P := Parent (N);
4736 end if;
4737
4738 if Nkind (P) /= N_Subprogram_Body then
4739 return False;
4740
4741 else
7d8b9c99
RD
4742 Id := Defining_Unit_Name (Specification (P));
4743
21791d97
AC
4744 -- Following complex conditional could use comments ???
4745
7d8b9c99 4746 if Nkind (Id) = N_Defining_Identifier
21791d97
AC
4747 and then (Is_Init_Proc (Id)
4748 or else Is_TSS (Id, TSS_Stream_Input)
4749 or else Is_TSS (Id, TSS_Stream_Output)
4750 or else Is_TSS (Id, TSS_Stream_Read)
4751 or else Is_TSS (Id, TSS_Stream_Write)
4752 or else Nkind_In (Original_Node (P),
4753 N_Subprogram_Renaming_Declaration,
4754 N_Expression_Function))
70482933
RK
4755 then
4756 return True;
4757 else
4758 return False;
4759 end if;
4760 end if;
70482933
RK
4761 end In_Exp_Body;
4762
4763 -- Start of processing for Freeze_Expression
4764
4765 begin
edd63e9b
ES
4766 -- Immediate return if freezing is inhibited. This flag is set by the
4767 -- analyzer to stop freezing on generated expressions that would cause
4768 -- freezing if they were in the source program, but which are not
4769 -- supposed to freeze, since they are created.
70482933
RK
4770
4771 if Must_Not_Freeze (N) then
4772 return;
4773 end if;
4774
4775 -- If expression is non-static, then it does not freeze in a default
4776 -- expression, see section "Handling of Default Expressions" in the
fe58fea7
AC
4777 -- spec of package Sem for further details. Note that we have to make
4778 -- sure that we actually have a real expression (if we have a subtype
4779 -- indication, we can't test Is_Static_Expression!) However, we exclude
4780 -- the case of the prefix of an attribute of a static scalar subtype
4781 -- from this early return, because static subtype attributes should
4782 -- always cause freezing, even in default expressions, but the attribute
4783 -- may not have been marked as static yet (because in Resolve_Attribute,
4784 -- the call to Eval_Attribute follows the call of Freeze_Expression on
4785 -- the prefix).
70482933 4786
c6a9797e 4787 if In_Spec_Exp
70482933
RK
4788 and then Nkind (N) in N_Subexpr
4789 and then not Is_Static_Expression (N)
fe58fea7
AC
4790 and then (Nkind (Parent (N)) /= N_Attribute_Reference
4791 or else not (Is_Entity_Name (N)
4792 and then Is_Type (Entity (N))
4793 and then Is_Static_Subtype (Entity (N))))
70482933
RK
4794 then
4795 return;
4796 end if;
4797
4798 -- Freeze type of expression if not frozen already
4799
fbf5a39b
AC
4800 Typ := Empty;
4801
4802 if Nkind (N) in N_Has_Etype then
4803 if not Is_Frozen (Etype (N)) then
4804 Typ := Etype (N);
4805
4806 -- Base type may be an derived numeric type that is frozen at
4807 -- the point of declaration, but first_subtype is still unfrozen.
4808
4809 elsif not Is_Frozen (First_Subtype (Etype (N))) then
4810 Typ := First_Subtype (Etype (N));
4811 end if;
70482933
RK
4812 end if;
4813
4814 -- For entity name, freeze entity if not frozen already. A special
4815 -- exception occurs for an identifier that did not come from source.
4816 -- We don't let such identifiers freeze a non-internal entity, i.e.
4817 -- an entity that did come from source, since such an identifier was
4818 -- generated by the expander, and cannot have any semantic effect on
4819 -- the freezing semantics. For example, this stops the parameter of
4820 -- an initialization procedure from freezing the variable.
4821
4822 if Is_Entity_Name (N)
4823 and then not Is_Frozen (Entity (N))
4824 and then (Nkind (N) /= N_Identifier
4825 or else Comes_From_Source (N)
4826 or else not Comes_From_Source (Entity (N)))
4827 then
4828 Nam := Entity (N);
70482933
RK
4829 else
4830 Nam := Empty;
4831 end if;
4832
49e90211 4833 -- For an allocator freeze designated type if not frozen already
70482933 4834
ee094616
RD
4835 -- For an aggregate whose component type is an access type, freeze the
4836 -- designated type now, so that its freeze does not appear within the
4837 -- loop that might be created in the expansion of the aggregate. If the
4838 -- designated type is a private type without full view, the expression
4839 -- cannot contain an allocator, so the type is not frozen.
70482933 4840
7aedb36a
AC
4841 -- For a function, we freeze the entity when the subprogram declaration
4842 -- is frozen, but a function call may appear in an initialization proc.
f6cf5b85 4843 -- before the declaration is frozen. We need to generate the extra
7aedb36a 4844 -- formals, if any, to ensure that the expansion of the call includes
2f4f3f3f
AC
4845 -- the proper actuals. This only applies to Ada subprograms, not to
4846 -- imported ones.
7aedb36a 4847
70482933 4848 Desig_Typ := Empty;
70482933 4849
fbf5a39b 4850 case Nkind (N) is
70482933
RK
4851 when N_Allocator =>
4852 Desig_Typ := Designated_Type (Etype (N));
4853
4854 when N_Aggregate =>
4855 if Is_Array_Type (Etype (N))
4856 and then Is_Access_Type (Component_Type (Etype (N)))
4857 then
4858 Desig_Typ := Designated_Type (Component_Type (Etype (N)));
4859 end if;
4860
4861 when N_Selected_Component |
4862 N_Indexed_Component |
4863 N_Slice =>
4864
4865 if Is_Access_Type (Etype (Prefix (N))) then
4866 Desig_Typ := Designated_Type (Etype (Prefix (N)));
4867 end if;
4868
7aedb36a
AC
4869 when N_Identifier =>
4870 if Present (Nam)
4871 and then Ekind (Nam) = E_Function
4872 and then Nkind (Parent (N)) = N_Function_Call
2f4f3f3f 4873 and then Convention (Nam) = Convention_Ada
7aedb36a
AC
4874 then
4875 Create_Extra_Formals (Nam);
4876 end if;
4877
70482933
RK
4878 when others =>
4879 null;
70482933
RK
4880 end case;
4881
4882 if Desig_Typ /= Empty
4883 and then (Is_Frozen (Desig_Typ)
4884 or else (not Is_Fully_Defined (Desig_Typ)))
4885 then
4886 Desig_Typ := Empty;
4887 end if;
4888
4889 -- All done if nothing needs freezing
4890
4891 if No (Typ)
4892 and then No (Nam)
4893 and then No (Desig_Typ)
4894 then
4895 return;
4896 end if;
4897
f6cf5b85 4898 -- Loop for looking at the right place to insert the freeze nodes,
70482933
RK
4899 -- exiting from the loop when it is appropriate to insert the freeze
4900 -- node before the current node P.
4901
bce79204
AC
4902 -- Also checks some special exceptions to the freezing rules. These
4903 -- cases result in a direct return, bypassing the freeze action.
70482933
RK
4904
4905 P := N;
4906 loop
4907 Parent_P := Parent (P);
4908
ee094616
RD
4909 -- If we don't have a parent, then we are not in a well-formed tree.
4910 -- This is an unusual case, but there are some legitimate situations
4911 -- in which this occurs, notably when the expressions in the range of
4912 -- a type declaration are resolved. We simply ignore the freeze
4913 -- request in this case. Is this right ???
70482933
RK
4914
4915 if No (Parent_P) then
4916 return;
4917 end if;
4918
4919 -- See if we have got to an appropriate point in the tree
4920
4921 case Nkind (Parent_P) is
4922
edd63e9b
ES
4923 -- A special test for the exception of (RM 13.14(8)) for the case
4924 -- of per-object expressions (RM 3.8(18)) occurring in component
4925 -- definition or a discrete subtype definition. Note that we test
4926 -- for a component declaration which includes both cases we are
4927 -- interested in, and furthermore the tree does not have explicit
4928 -- nodes for either of these two constructs.
70482933
RK
4929
4930 when N_Component_Declaration =>
4931
4932 -- The case we want to test for here is an identifier that is
4933 -- a per-object expression, this is either a discriminant that
4934 -- appears in a context other than the component declaration
4935 -- or it is a reference to the type of the enclosing construct.
4936
4937 -- For either of these cases, we skip the freezing
4938
c6a9797e 4939 if not In_Spec_Expression
70482933
RK
4940 and then Nkind (N) = N_Identifier
4941 and then (Present (Entity (N)))
4942 then
4943 -- We recognize the discriminant case by just looking for
4944 -- a reference to a discriminant. It can only be one for
4945 -- the enclosing construct. Skip freezing in this case.
4946
4947 if Ekind (Entity (N)) = E_Discriminant then
4948 return;
4949
4950 -- For the case of a reference to the enclosing record,
4951 -- (or task or protected type), we look for a type that
4952 -- matches the current scope.
4953
4954 elsif Entity (N) = Current_Scope then
4955 return;
4956 end if;
4957 end if;
4958
edd63e9b
ES
4959 -- If we have an enumeration literal that appears as the choice in
4960 -- the aggregate of an enumeration representation clause, then
4961 -- freezing does not occur (RM 13.14(10)).
70482933
RK
4962
4963 when N_Enumeration_Representation_Clause =>
4964
4965 -- The case we are looking for is an enumeration literal
4966
4967 if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
4968 and then Is_Enumeration_Type (Etype (N))
4969 then
4970 -- If enumeration literal appears directly as the choice,
e14c931f 4971 -- do not freeze (this is the normal non-overloaded case)
70482933
RK
4972
4973 if Nkind (Parent (N)) = N_Component_Association
4974 and then First (Choices (Parent (N))) = N
4975 then
4976 return;
4977
ee094616
RD
4978 -- If enumeration literal appears as the name of function
4979 -- which is the choice, then also do not freeze. This
4980 -- happens in the overloaded literal case, where the
70482933
RK
4981 -- enumeration literal is temporarily changed to a function
4982 -- call for overloading analysis purposes.
4983
4984 elsif Nkind (Parent (N)) = N_Function_Call
4985 and then
4986 Nkind (Parent (Parent (N))) = N_Component_Association
4987 and then
4988 First (Choices (Parent (Parent (N)))) = Parent (N)
4989 then
4990 return;
4991 end if;
4992 end if;
4993
4994 -- Normally if the parent is a handled sequence of statements,
4995 -- then the current node must be a statement, and that is an
4996 -- appropriate place to insert a freeze node.
4997
4998 when N_Handled_Sequence_Of_Statements =>
4999
edd63e9b
ES
5000 -- An exception occurs when the sequence of statements is for
5001 -- an expander generated body that did not do the usual freeze
5002 -- all operation. In this case we usually want to freeze
5003 -- outside this body, not inside it, and we skip past the
5004 -- subprogram body that we are inside.
70482933
RK
5005
5006 if In_Exp_Body (Parent_P) then
5007
5008 -- However, we *do* want to freeze at this point if we have
5009 -- an entity to freeze, and that entity is declared *inside*
5010 -- the body of the expander generated procedure. This case
5011 -- is recognized by the scope of the type, which is either
5012 -- the spec for some enclosing body, or (in the case of
5013 -- init_procs, for which there are no separate specs) the
5014 -- current scope.
5015
5016 declare
5017 Subp : constant Node_Id := Parent (Parent_P);
5018 Cspc : Entity_Id;
5019
5020 begin
5021 if Nkind (Subp) = N_Subprogram_Body then
5022 Cspc := Corresponding_Spec (Subp);
5023
5024 if (Present (Typ) and then Scope (Typ) = Cspc)
5025 or else
5026 (Present (Nam) and then Scope (Nam) = Cspc)
5027 then
5028 exit;
5029
5030 elsif Present (Typ)
5031 and then Scope (Typ) = Current_Scope
5032 and then Current_Scope = Defining_Entity (Subp)
5033 then
5034 exit;
5035 end if;
5036 end if;
5037 end;
5038
5039 -- If not that exception to the exception, then this is
5040 -- where we delay the freeze till outside the body.
5041
5042 Parent_P := Parent (Parent_P);
5043 Freeze_Outside := True;
5044
5045 -- Here if normal case where we are in handled statement
5046 -- sequence and want to do the insertion right there.
5047
5048 else
5049 exit;
5050 end if;
5051
ee094616
RD
5052 -- If parent is a body or a spec or a block, then the current node
5053 -- is a statement or declaration and we can insert the freeze node
5054 -- before it.
70482933 5055
8b3c6430
AC
5056 when N_Block_Statement |
5057 N_Entry_Body |
70482933 5058 N_Package_Body |
8b3c6430 5059 N_Package_Specification |
70482933 5060 N_Protected_Body |
8b3c6430
AC
5061 N_Subprogram_Body |
5062 N_Task_Body => exit;
70482933
RK
5063
5064 -- The expander is allowed to define types in any statements list,
5065 -- so any of the following parent nodes also mark a freezing point
5066 -- if the actual node is in a list of statements or declarations.
5067
8b3c6430
AC
5068 when N_Abortable_Part |
5069 N_Accept_Alternative |
5070 N_And_Then |
70482933
RK
5071 N_Case_Statement_Alternative |
5072 N_Compilation_Unit_Aux |
70482933 5073 N_Conditional_Entry_Call |
8b3c6430
AC
5074 N_Delay_Alternative |
5075 N_Elsif_Part |
70482933 5076 N_Entry_Call_Alternative |
8b3c6430
AC
5077 N_Exception_Handler |
5078 N_Extended_Return_Statement |
5079 N_Freeze_Entity |
5080 N_If_Statement |
bce79204 5081 N_Or_Else |
8b3c6430
AC
5082 N_Selective_Accept |
5083 N_Triggering_Alternative =>
70482933
RK
5084
5085 exit when Is_List_Member (P);
5086
5087 -- Note: The N_Loop_Statement is a special case. A type that
5088 -- appears in the source can never be frozen in a loop (this
edd63e9b
ES
5089 -- occurs only because of a loop expanded by the expander), so we
5090 -- keep on going. Otherwise we terminate the search. Same is true
ee094616
RD
5091 -- of any entity which comes from source. (if they have predefined
5092 -- type, that type does not appear to come from source, but the
5093 -- entity should not be frozen here).
70482933
RK
5094
5095 when N_Loop_Statement =>
5096 exit when not Comes_From_Source (Etype (N))
5097 and then (No (Nam) or else not Comes_From_Source (Nam));
5098
5099 -- For all other cases, keep looking at parents
5100
5101 when others =>
5102 null;
5103 end case;
5104
5105 -- We fall through the case if we did not yet find the proper
5106 -- place in the free for inserting the freeze node, so climb!
5107
5108 P := Parent_P;
5109 end loop;
5110
edd63e9b
ES
5111 -- If the expression appears in a record or an initialization procedure,
5112 -- the freeze nodes are collected and attached to the current scope, to
5113 -- be inserted and analyzed on exit from the scope, to insure that
5114 -- generated entities appear in the correct scope. If the expression is
5115 -- a default for a discriminant specification, the scope is still void.
5116 -- The expression can also appear in the discriminant part of a private
5117 -- or concurrent type.
70482933 5118
c6823a20 5119 -- If the expression appears in a constrained subcomponent of an
edd63e9b
ES
5120 -- enclosing record declaration, the freeze nodes must be attached to
5121 -- the outer record type so they can eventually be placed in the
c6823a20
EB
5122 -- enclosing declaration list.
5123
ee094616
RD
5124 -- The other case requiring this special handling is if we are in a
5125 -- default expression, since in that case we are about to freeze a
5126 -- static type, and the freeze scope needs to be the outer scope, not
5127 -- the scope of the subprogram with the default parameter.
70482933 5128
c6a9797e
RD
5129 -- For default expressions and other spec expressions in generic units,
5130 -- the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
5131 -- placing them at the proper place, after the generic unit.
70482933 5132
c6a9797e 5133 if (In_Spec_Exp and not Inside_A_Generic)
70482933
RK
5134 or else Freeze_Outside
5135 or else (Is_Type (Current_Scope)
5136 and then (not Is_Concurrent_Type (Current_Scope)
5137 or else not Has_Completion (Current_Scope)))
5138 or else Ekind (Current_Scope) = E_Void
5139 then
5140 declare
df378148
AC
5141 N : constant Node_Id := Current_Scope;
5142 Freeze_Nodes : List_Id := No_List;
5143 Pos : Int := Scope_Stack.Last;
70482933
RK
5144
5145 begin
5146 if Present (Desig_Typ) then
c159409f 5147 Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
70482933
RK
5148 end if;
5149
5150 if Present (Typ) then
c159409f 5151 Freeze_And_Append (Typ, N, Freeze_Nodes);
70482933
RK
5152 end if;
5153
5154 if Present (Nam) then
c159409f 5155 Freeze_And_Append (Nam, N, Freeze_Nodes);
70482933
RK
5156 end if;
5157
c6823a20 5158 -- The current scope may be that of a constrained component of
df378148
AC
5159 -- an enclosing record declaration, or of a loop of an enclosing
5160 -- quantified expression, which is above the current scope in the
5161 -- scope stack. Indeed in the context of a quantified expression,
5162 -- a scope is created and pushed above the current scope in order
5163 -- to emulate the loop-like behavior of the quantified expression.
6191e212
AC
5164 -- If the expression is within a top-level pragma, as for a pre-
5165 -- condition on a library-level subprogram, nothing to do.
c6823a20 5166
6191e212 5167 if not Is_Compilation_Unit (Current_Scope)
df378148
AC
5168 and then (Is_Record_Type (Scope (Current_Scope))
5169 or else Nkind (Parent (Current_Scope)) =
21791d97 5170 N_Quantified_Expression)
6191e212 5171 then
c6823a20
EB
5172 Pos := Pos - 1;
5173 end if;
5174
70482933 5175 if Is_Non_Empty_List (Freeze_Nodes) then
c6823a20
EB
5176 if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
5177 Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
c159409f 5178 Freeze_Nodes;
70482933 5179 else
cd5a9750
AC
5180 Append_List (Freeze_Nodes,
5181 Scope_Stack.Table (Pos).Pending_Freeze_Actions);
70482933
RK
5182 end if;
5183 end if;
5184 end;
5185
5186 return;
5187 end if;
5188
5189 -- Now we have the right place to do the freezing. First, a special
c6a9797e
RD
5190 -- adjustment, if we are in spec-expression analysis mode, these freeze
5191 -- actions must not be thrown away (normally all inserted actions are
5192 -- thrown away in this mode. However, the freeze actions are from static
5193 -- expressions and one of the important reasons we are doing this
ee094616 5194 -- special analysis is to get these freeze actions. Therefore we turn
c6a9797e 5195 -- off the In_Spec_Expression mode to propagate these freeze actions.
ee094616 5196 -- This also means they get properly analyzed and expanded.
70482933 5197
c6a9797e 5198 In_Spec_Expression := False;
70482933 5199
fbf5a39b 5200 -- Freeze the designated type of an allocator (RM 13.14(13))
70482933
RK
5201
5202 if Present (Desig_Typ) then
5203 Freeze_Before (P, Desig_Typ);
5204 end if;
5205
fbf5a39b 5206 -- Freeze type of expression (RM 13.14(10)). Note that we took care of
70482933
RK
5207 -- the enumeration representation clause exception in the loop above.
5208
5209 if Present (Typ) then
5210 Freeze_Before (P, Typ);
5211 end if;
5212
fbf5a39b 5213 -- Freeze name if one is present (RM 13.14(11))
70482933
RK
5214
5215 if Present (Nam) then
5216 Freeze_Before (P, Nam);
5217 end if;
5218
c6a9797e
RD
5219 -- Restore In_Spec_Expression flag
5220
5221 In_Spec_Expression := In_Spec_Exp;
70482933
RK
5222 end Freeze_Expression;
5223
5224 -----------------------------
5225 -- Freeze_Fixed_Point_Type --
5226 -----------------------------
5227
edd63e9b
ES
5228 -- Certain fixed-point types and subtypes, including implicit base types
5229 -- and declared first subtypes, have not yet set up a range. This is
5230 -- because the range cannot be set until the Small and Size values are
5231 -- known, and these are not known till the type is frozen.
70482933 5232
edd63e9b
ES
5233 -- To signal this case, Scalar_Range contains an unanalyzed syntactic range
5234 -- whose bounds are unanalyzed real literals. This routine will recognize
5235 -- this case, and transform this range node into a properly typed range
5236 -- with properly analyzed and resolved values.
70482933
RK
5237
5238 procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
5239 Rng : constant Node_Id := Scalar_Range (Typ);
5240 Lo : constant Node_Id := Low_Bound (Rng);
5241 Hi : constant Node_Id := High_Bound (Rng);
5242 Btyp : constant Entity_Id := Base_Type (Typ);
5243 Brng : constant Node_Id := Scalar_Range (Btyp);
5244 BLo : constant Node_Id := Low_Bound (Brng);
5245 BHi : constant Node_Id := High_Bound (Brng);
5246 Small : constant Ureal := Small_Value (Typ);
5247 Loval : Ureal;
5248 Hival : Ureal;
5249 Atype : Entity_Id;
5250
5251 Actual_Size : Nat;
5252
5253 function Fsize (Lov, Hiv : Ureal) return Nat;
5254 -- Returns size of type with given bounds. Also leaves these
5255 -- bounds set as the current bounds of the Typ.
5256
0da2c8ac
AC
5257 -----------
5258 -- Fsize --
5259 -----------
5260
70482933
RK
5261 function Fsize (Lov, Hiv : Ureal) return Nat is
5262 begin
5263 Set_Realval (Lo, Lov);
5264 Set_Realval (Hi, Hiv);
5265 return Minimum_Size (Typ);
5266 end Fsize;
5267
0da2c8ac 5268 -- Start of processing for Freeze_Fixed_Point_Type
70482933
RK
5269
5270 begin
5271 -- If Esize of a subtype has not previously been set, set it now
5272
5273 if Unknown_Esize (Typ) then
5274 Atype := Ancestor_Subtype (Typ);
5275
5276 if Present (Atype) then
fbf5a39b 5277 Set_Esize (Typ, Esize (Atype));
70482933 5278 else
fbf5a39b 5279 Set_Esize (Typ, Esize (Base_Type (Typ)));
70482933
RK
5280 end if;
5281 end if;
5282
ee094616
RD
5283 -- Immediate return if the range is already analyzed. This means that
5284 -- the range is already set, and does not need to be computed by this
5285 -- routine.
70482933
RK
5286
5287 if Analyzed (Rng) then
5288 return;
5289 end if;
5290
5291 -- Immediate return if either of the bounds raises Constraint_Error
5292
5293 if Raises_Constraint_Error (Lo)
5294 or else Raises_Constraint_Error (Hi)
5295 then
5296 return;
5297 end if;
5298
5299 Loval := Realval (Lo);
5300 Hival := Realval (Hi);
5301
5302 -- Ordinary fixed-point case
5303
5304 if Is_Ordinary_Fixed_Point_Type (Typ) then
5305
5306 -- For the ordinary fixed-point case, we are allowed to fudge the
ee094616
RD
5307 -- end-points up or down by small. Generally we prefer to fudge up,
5308 -- i.e. widen the bounds for non-model numbers so that the end points
5309 -- are included. However there are cases in which this cannot be
5310 -- done, and indeed cases in which we may need to narrow the bounds.
5311 -- The following circuit makes the decision.
70482933 5312
ee094616
RD
5313 -- Note: our terminology here is that Incl_EP means that the bounds
5314 -- are widened by Small if necessary to include the end points, and
5315 -- Excl_EP means that the bounds are narrowed by Small to exclude the
5316 -- end-points if this reduces the size.
70482933
RK
5317
5318 -- Note that in the Incl case, all we care about is including the
5319 -- end-points. In the Excl case, we want to narrow the bounds as
5320 -- much as permitted by the RM, to give the smallest possible size.
5321
5322 Fudge : declare
5323 Loval_Incl_EP : Ureal;
5324 Hival_Incl_EP : Ureal;
5325
5326 Loval_Excl_EP : Ureal;
5327 Hival_Excl_EP : Ureal;
5328
5329 Size_Incl_EP : Nat;
5330 Size_Excl_EP : Nat;
5331
5332 Model_Num : Ureal;
5333 First_Subt : Entity_Id;
5334 Actual_Lo : Ureal;
5335 Actual_Hi : Ureal;
5336
5337 begin
5338 -- First step. Base types are required to be symmetrical. Right
5339 -- now, the base type range is a copy of the first subtype range.
5340 -- This will be corrected before we are done, but right away we
5341 -- need to deal with the case where both bounds are non-negative.
5342 -- In this case, we set the low bound to the negative of the high
5343 -- bound, to make sure that the size is computed to include the
5344 -- required sign. Note that we do not need to worry about the
5345 -- case of both bounds negative, because the sign will be dealt
5346 -- with anyway. Furthermore we can't just go making such a bound
5347 -- symmetrical, since in a twos-complement system, there is an
e14c931f 5348 -- extra negative value which could not be accommodated on the
70482933
RK
5349 -- positive side.
5350
5351 if Typ = Btyp
5352 and then not UR_Is_Negative (Loval)
5353 and then Hival > Loval
5354 then
5355 Loval := -Hival;
5356 Set_Realval (Lo, Loval);
5357 end if;
5358
5359 -- Compute the fudged bounds. If the number is a model number,
edd63e9b
ES
5360 -- then we do nothing to include it, but we are allowed to backoff
5361 -- to the next adjacent model number when we exclude it. If it is
5362 -- not a model number then we straddle the two values with the
5363 -- model numbers on either side.
70482933
RK
5364
5365 Model_Num := UR_Trunc (Loval / Small) * Small;
5366
5367 if Loval = Model_Num then
5368 Loval_Incl_EP := Model_Num;
5369 else
5370 Loval_Incl_EP := Model_Num - Small;
5371 end if;
5372
5373 -- The low value excluding the end point is Small greater, but
5374 -- we do not do this exclusion if the low value is positive,
5375 -- since it can't help the size and could actually hurt by
5376 -- crossing the high bound.
5377
5378 if UR_Is_Negative (Loval_Incl_EP) then
5379 Loval_Excl_EP := Loval_Incl_EP + Small;
def46b54
RD
5380
5381 -- If the value went from negative to zero, then we have the
5382 -- case where Loval_Incl_EP is the model number just below
5383 -- zero, so we want to stick to the negative value for the
5384 -- base type to maintain the condition that the size will
5385 -- include signed values.
5386
5387 if Typ = Btyp
5388 and then UR_Is_Zero (Loval_Excl_EP)
5389 then
5390 Loval_Excl_EP := Loval_Incl_EP;
5391 end if;
5392
70482933
RK
5393 else
5394 Loval_Excl_EP := Loval_Incl_EP;
5395 end if;
5396
5397 -- Similar processing for upper bound and high value
5398
5399 Model_Num := UR_Trunc (Hival / Small) * Small;
5400
5401 if Hival = Model_Num then
5402 Hival_Incl_EP := Model_Num;
5403 else
5404 Hival_Incl_EP := Model_Num + Small;
5405 end if;
5406
5407 if UR_Is_Positive (Hival_Incl_EP) then
5408 Hival_Excl_EP := Hival_Incl_EP - Small;
5409 else
5410 Hival_Excl_EP := Hival_Incl_EP;
5411 end if;
5412
ee094616
RD
5413 -- One further adjustment is needed. In the case of subtypes, we
5414 -- cannot go outside the range of the base type, or we get
70482933 5415 -- peculiarities, and the base type range is already set. This
ee094616
RD
5416 -- only applies to the Incl values, since clearly the Excl values
5417 -- are already as restricted as they are allowed to be.
70482933
RK
5418
5419 if Typ /= Btyp then
5420 Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
5421 Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
5422 end if;
5423
5424 -- Get size including and excluding end points
5425
5426 Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
5427 Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
5428
5429 -- No need to exclude end-points if it does not reduce size
5430
5431 if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
5432 Loval_Excl_EP := Loval_Incl_EP;
5433 end if;
5434
5435 if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
5436 Hival_Excl_EP := Hival_Incl_EP;
5437 end if;
5438
5439 -- Now we set the actual size to be used. We want to use the
5440 -- bounds fudged up to include the end-points but only if this
5441 -- can be done without violating a specifically given size
5442 -- size clause or causing an unacceptable increase in size.
5443
5444 -- Case of size clause given
5445
5446 if Has_Size_Clause (Typ) then
5447
5448 -- Use the inclusive size only if it is consistent with
5449 -- the explicitly specified size.
5450
5451 if Size_Incl_EP <= RM_Size (Typ) then
5452 Actual_Lo := Loval_Incl_EP;
5453 Actual_Hi := Hival_Incl_EP;
5454 Actual_Size := Size_Incl_EP;
5455
5456 -- If the inclusive size is too large, we try excluding
5457 -- the end-points (will be caught later if does not work).
5458
5459 else
5460 Actual_Lo := Loval_Excl_EP;
5461 Actual_Hi := Hival_Excl_EP;
5462 Actual_Size := Size_Excl_EP;
5463 end if;
5464
5465 -- Case of size clause not given
5466
5467 else
5468 -- If we have a base type whose corresponding first subtype
5469 -- has an explicit size that is large enough to include our
5470 -- end-points, then do so. There is no point in working hard
5471 -- to get a base type whose size is smaller than the specified
5472 -- size of the first subtype.
5473
5474 First_Subt := First_Subtype (Typ);
5475
5476 if Has_Size_Clause (First_Subt)
5477 and then Size_Incl_EP <= Esize (First_Subt)
5478 then
5479 Actual_Size := Size_Incl_EP;
5480 Actual_Lo := Loval_Incl_EP;
5481 Actual_Hi := Hival_Incl_EP;
5482
5483 -- If excluding the end-points makes the size smaller and
5484 -- results in a size of 8,16,32,64, then we take the smaller
5485 -- size. For the 64 case, this is compulsory. For the other
5486 -- cases, it seems reasonable. We like to include end points
5487 -- if we can, but not at the expense of moving to the next
5488 -- natural boundary of size.
5489
5490 elsif Size_Incl_EP /= Size_Excl_EP
094cefda 5491 and then Addressable (Size_Excl_EP)
70482933
RK
5492 then
5493 Actual_Size := Size_Excl_EP;
5494 Actual_Lo := Loval_Excl_EP;
5495 Actual_Hi := Hival_Excl_EP;
5496
5497 -- Otherwise we can definitely include the end points
5498
5499 else
5500 Actual_Size := Size_Incl_EP;
5501 Actual_Lo := Loval_Incl_EP;
5502 Actual_Hi := Hival_Incl_EP;
5503 end if;
5504
edd63e9b
ES
5505 -- One pathological case: normally we never fudge a low bound
5506 -- down, since it would seem to increase the size (if it has
5507 -- any effect), but for ranges containing single value, or no
5508 -- values, the high bound can be small too large. Consider:
70482933
RK
5509
5510 -- type t is delta 2.0**(-14)
5511 -- range 131072.0 .. 0;
5512
edd63e9b
ES
5513 -- That lower bound is *just* outside the range of 32 bits, and
5514 -- does need fudging down in this case. Note that the bounds
5515 -- will always have crossed here, since the high bound will be
5516 -- fudged down if necessary, as in the case of:
70482933
RK
5517
5518 -- type t is delta 2.0**(-14)
5519 -- range 131072.0 .. 131072.0;
5520
edd63e9b
ES
5521 -- So we detect the situation by looking for crossed bounds,
5522 -- and if the bounds are crossed, and the low bound is greater
5523 -- than zero, we will always back it off by small, since this
5524 -- is completely harmless.
70482933
RK
5525
5526 if Actual_Lo > Actual_Hi then
5527 if UR_Is_Positive (Actual_Lo) then
5528 Actual_Lo := Loval_Incl_EP - Small;
5529 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
5530
5531 -- And of course, we need to do exactly the same parallel
5532 -- fudge for flat ranges in the negative region.
5533
5534 elsif UR_Is_Negative (Actual_Hi) then
5535 Actual_Hi := Hival_Incl_EP + Small;
5536 Actual_Size := Fsize (Actual_Lo, Actual_Hi);
5537 end if;
5538 end if;
5539 end if;
5540
5541 Set_Realval (Lo, Actual_Lo);
5542 Set_Realval (Hi, Actual_Hi);
5543 end Fudge;
5544
5545 -- For the decimal case, none of this fudging is required, since there
5546 -- are no end-point problems in the decimal case (the end-points are
5547 -- always included).
5548
5549 else
5550 Actual_Size := Fsize (Loval, Hival);
5551 end if;
5552
5553 -- At this stage, the actual size has been calculated and the proper
5554 -- required bounds are stored in the low and high bounds.
5555
5556 if Actual_Size > 64 then
5557 Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
5558 Error_Msg_N
7d8b9c99
RD
5559 ("size required (^) for type& too large, maximum allowed is 64",
5560 Typ);
70482933
RK
5561 Actual_Size := 64;
5562 end if;
5563
5564 -- Check size against explicit given size
5565
5566 if Has_Size_Clause (Typ) then
5567 if Actual_Size > RM_Size (Typ) then
5568 Error_Msg_Uint_1 := RM_Size (Typ);
5569 Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
5570 Error_Msg_NE
7d8b9c99 5571 ("size given (^) for type& too small, minimum allowed is ^",
70482933
RK
5572 Size_Clause (Typ), Typ);
5573
5574 else
5575 Actual_Size := UI_To_Int (Esize (Typ));
5576 end if;
5577
5578 -- Increase size to next natural boundary if no size clause given
5579
5580 else
5581 if Actual_Size <= 8 then
5582 Actual_Size := 8;
5583 elsif Actual_Size <= 16 then
5584 Actual_Size := 16;
5585 elsif Actual_Size <= 32 then
5586 Actual_Size := 32;
5587 else
5588 Actual_Size := 64;
5589 end if;
5590
5591 Init_Esize (Typ, Actual_Size);
5592 Adjust_Esize_For_Alignment (Typ);
5593 end if;
5594
edd63e9b
ES
5595 -- If we have a base type, then expand the bounds so that they extend to
5596 -- the full width of the allocated size in bits, to avoid junk range
5597 -- checks on intermediate computations.
70482933
RK
5598
5599 if Base_Type (Typ) = Typ then
5600 Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
5601 Set_Realval (Hi, (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
5602 end if;
5603
5604 -- Final step is to reanalyze the bounds using the proper type
5605 -- and set the Corresponding_Integer_Value fields of the literals.
5606
5607 Set_Etype (Lo, Empty);
5608 Set_Analyzed (Lo, False);
5609 Analyze (Lo);
5610
edd63e9b
ES
5611 -- Resolve with universal fixed if the base type, and the base type if
5612 -- it is a subtype. Note we can't resolve the base type with itself,
5613 -- that would be a reference before definition.
70482933
RK
5614
5615 if Typ = Btyp then
5616 Resolve (Lo, Universal_Fixed);
5617 else
5618 Resolve (Lo, Btyp);
5619 end if;
5620
5621 -- Set corresponding integer value for bound
5622
5623 Set_Corresponding_Integer_Value
5624 (Lo, UR_To_Uint (Realval (Lo) / Small));
5625
5626 -- Similar processing for high bound
5627
5628 Set_Etype (Hi, Empty);
5629 Set_Analyzed (Hi, False);
5630 Analyze (Hi);
5631
5632 if Typ = Btyp then
5633 Resolve (Hi, Universal_Fixed);
5634 else
5635 Resolve (Hi, Btyp);
5636 end if;
5637
5638 Set_Corresponding_Integer_Value
5639 (Hi, UR_To_Uint (Realval (Hi) / Small));
5640
5641 -- Set type of range to correspond to bounds
5642
5643 Set_Etype (Rng, Etype (Lo));
5644
fbf5a39b 5645 -- Set Esize to calculated size if not set already
70482933 5646
fbf5a39b
AC
5647 if Unknown_Esize (Typ) then
5648 Init_Esize (Typ, Actual_Size);
5649 end if;
70482933
RK
5650
5651 -- Set RM_Size if not already set. If already set, check value
5652
5653 declare
5654 Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
5655
5656 begin
5657 if RM_Size (Typ) /= Uint_0 then
5658 if RM_Size (Typ) < Minsiz then
5659 Error_Msg_Uint_1 := RM_Size (Typ);
5660 Error_Msg_Uint_2 := Minsiz;
5661 Error_Msg_NE
7d8b9c99 5662 ("size given (^) for type& too small, minimum allowed is ^",
70482933
RK
5663 Size_Clause (Typ), Typ);
5664 end if;
5665
5666 else
5667 Set_RM_Size (Typ, Minsiz);
5668 end if;
5669 end;
70482933
RK
5670 end Freeze_Fixed_Point_Type;
5671
5672 ------------------
5673 -- Freeze_Itype --
5674 ------------------
5675
5676 procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
5677 L : List_Id;
5678
5679 begin
5680 Set_Has_Delayed_Freeze (T);
c159409f 5681 L := Freeze_Entity (T, N);
70482933
RK
5682
5683 if Is_Non_Empty_List (L) then
5684 Insert_Actions (N, L);
5685 end if;
5686 end Freeze_Itype;
5687
5688 --------------------------
5689 -- Freeze_Static_Object --
5690 --------------------------
5691
5692 procedure Freeze_Static_Object (E : Entity_Id) is
5693
5694 Cannot_Be_Static : exception;
5695 -- Exception raised if the type of a static object cannot be made
5696 -- static. This happens if the type depends on non-global objects.
5697
5698 procedure Ensure_Expression_Is_SA (N : Node_Id);
ee094616
RD
5699 -- Called to ensure that an expression used as part of a type definition
5700 -- is statically allocatable, which means that the expression type is
5701 -- statically allocatable, and the expression is either static, or a
5702 -- reference to a library level constant.
70482933
RK
5703
5704 procedure Ensure_Type_Is_SA (Typ : Entity_Id);
5705 -- Called to mark a type as static, checking that it is possible
5706 -- to set the type as static. If it is not possible, then the
5707 -- exception Cannot_Be_Static is raised.
5708
5709 -----------------------------
5710 -- Ensure_Expression_Is_SA --
5711 -----------------------------
5712
5713 procedure Ensure_Expression_Is_SA (N : Node_Id) is
5714 Ent : Entity_Id;
5715
5716 begin
5717 Ensure_Type_Is_SA (Etype (N));
5718
5719 if Is_Static_Expression (N) then
5720 return;
5721
5722 elsif Nkind (N) = N_Identifier then
5723 Ent := Entity (N);
5724
5725 if Present (Ent)
5726 and then Ekind (Ent) = E_Constant
5727 and then Is_Library_Level_Entity (Ent)
5728 then
5729 return;
5730 end if;
5731 end if;
5732
5733 raise Cannot_Be_Static;
5734 end Ensure_Expression_Is_SA;
5735
5736 -----------------------
5737 -- Ensure_Type_Is_SA --
5738 -----------------------
5739
5740 procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
5741 N : Node_Id;
5742 C : Entity_Id;
5743
5744 begin
5745 -- If type is library level, we are all set
5746
5747 if Is_Library_Level_Entity (Typ) then
5748 return;
5749 end if;
5750
ee094616
RD
5751 -- We are also OK if the type already marked as statically allocated,
5752 -- which means we processed it before.
70482933
RK
5753
5754 if Is_Statically_Allocated (Typ) then
5755 return;
5756 end if;
5757
5758 -- Mark type as statically allocated
5759
5760 Set_Is_Statically_Allocated (Typ);
5761
5762 -- Check that it is safe to statically allocate this type
5763
5764 if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
5765 Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
5766 Ensure_Expression_Is_SA (Type_High_Bound (Typ));
5767
5768 elsif Is_Array_Type (Typ) then
5769 N := First_Index (Typ);
5770 while Present (N) loop
5771 Ensure_Type_Is_SA (Etype (N));
5772 Next_Index (N);
5773 end loop;
5774
5775 Ensure_Type_Is_SA (Component_Type (Typ));
5776
5777 elsif Is_Access_Type (Typ) then
5778 if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
5779
5780 declare
5781 F : Entity_Id;
5782 T : constant Entity_Id := Etype (Designated_Type (Typ));
5783
5784 begin
5785 if T /= Standard_Void_Type then
5786 Ensure_Type_Is_SA (T);
5787 end if;
5788
5789 F := First_Formal (Designated_Type (Typ));
70482933
RK
5790 while Present (F) loop
5791 Ensure_Type_Is_SA (Etype (F));
5792 Next_Formal (F);
5793 end loop;
5794 end;
5795
5796 else
5797 Ensure_Type_Is_SA (Designated_Type (Typ));
5798 end if;
5799
5800 elsif Is_Record_Type (Typ) then
5801 C := First_Entity (Typ);
70482933
RK
5802 while Present (C) loop
5803 if Ekind (C) = E_Discriminant
5804 or else Ekind (C) = E_Component
5805 then
5806 Ensure_Type_Is_SA (Etype (C));
5807
5808 elsif Is_Type (C) then
5809 Ensure_Type_Is_SA (C);
5810 end if;
5811
5812 Next_Entity (C);
5813 end loop;
5814
5815 elsif Ekind (Typ) = E_Subprogram_Type then
5816 Ensure_Type_Is_SA (Etype (Typ));
5817
5818 C := First_Formal (Typ);
5819 while Present (C) loop
5820 Ensure_Type_Is_SA (Etype (C));
5821 Next_Formal (C);
5822 end loop;
5823
5824 else
5825 raise Cannot_Be_Static;
5826 end if;
5827 end Ensure_Type_Is_SA;
5828
5829 -- Start of processing for Freeze_Static_Object
5830
5831 begin
5832 Ensure_Type_Is_SA (Etype (E));
5833
5834 exception
5835 when Cannot_Be_Static =>
5836
09494c32
AC
5837 -- If the object that cannot be static is imported or exported, then
5838 -- issue an error message saying that this object cannot be imported
5839 -- or exported. If it has an address clause it is an overlay in the
5840 -- current partition and the static requirement is not relevant.
d606f1df 5841 -- Do not issue any error message when ignoring rep clauses.
09494c32 5842
d606f1df
AC
5843 if Ignore_Rep_Clauses then
5844 null;
5845
5846 elsif Is_Imported (E) then
5847 if No (Address_Clause (E)) then
5848 Error_Msg_N
5849 ("& cannot be imported (local type is not constant)", E);
5850 end if;
70482933
RK
5851
5852 -- Otherwise must be exported, something is wrong if compiler
5853 -- is marking something as statically allocated which cannot be).
5854
5855 else pragma Assert (Is_Exported (E));
5856 Error_Msg_N
5857 ("& cannot be exported (local type is not constant)", E);
5858 end if;
5859 end Freeze_Static_Object;
5860
5861 -----------------------
5862 -- Freeze_Subprogram --
5863 -----------------------
5864
5865 procedure Freeze_Subprogram (E : Entity_Id) is
5866 Retype : Entity_Id;
5867 F : Entity_Id;
5868
5869 begin
5870 -- Subprogram may not have an address clause unless it is imported
5871
5872 if Present (Address_Clause (E)) then
5873 if not Is_Imported (E) then
5874 Error_Msg_N
5875 ("address clause can only be given " &
5876 "for imported subprogram",
5877 Name (Address_Clause (E)));
5878 end if;
5879 end if;
5880
91b1417d
AC
5881 -- Reset the Pure indication on an imported subprogram unless an
5882 -- explicit Pure_Function pragma was present. We do this because
ee094616
RD
5883 -- otherwise it is an insidious error to call a non-pure function from
5884 -- pure unit and have calls mysteriously optimized away. What happens
5885 -- here is that the Import can bypass the normal check to ensure that
5886 -- pure units call only pure subprograms.
91b1417d
AC
5887
5888 if Is_Imported (E)
5889 and then Is_Pure (E)
5890 and then not Has_Pragma_Pure_Function (E)
5891 then
5892 Set_Is_Pure (E, False);
5893 end if;
5894
70482933
RK
5895 -- For non-foreign convention subprograms, this is where we create
5896 -- the extra formals (for accessibility level and constrained bit
5897 -- information). We delay this till the freeze point precisely so
5898 -- that we know the convention!
5899
5900 if not Has_Foreign_Convention (E) then
5901 Create_Extra_Formals (E);
5902 Set_Mechanisms (E);
5903
5904 -- If this is convention Ada and a Valued_Procedure, that's odd
5905
5906 if Ekind (E) = E_Procedure
5907 and then Is_Valued_Procedure (E)
5908 and then Convention (E) = Convention_Ada
fbf5a39b 5909 and then Warn_On_Export_Import
70482933
RK
5910 then
5911 Error_Msg_N
5912 ("?Valued_Procedure has no effect for convention Ada", E);
5913 Set_Is_Valued_Procedure (E, False);
5914 end if;
5915
5916 -- Case of foreign convention
5917
5918 else
5919 Set_Mechanisms (E);
5920
fbf5a39b 5921 -- For foreign conventions, warn about return of an
70482933
RK
5922 -- unconstrained array.
5923
5924 -- Note: we *do* allow a return by descriptor for the VMS case,
5925 -- though here there is probably more to be done ???
5926
5927 if Ekind (E) = E_Function then
5928 Retype := Underlying_Type (Etype (E));
5929
5930 -- If no return type, probably some other error, e.g. a
5931 -- missing full declaration, so ignore.
5932
5933 if No (Retype) then
5934 null;
5935
5936 -- If the return type is generic, we have emitted a warning
edd63e9b
ES
5937 -- earlier on, and there is nothing else to check here. Specific
5938 -- instantiations may lead to erroneous behavior.
70482933
RK
5939
5940 elsif Is_Generic_Type (Etype (E)) then
5941 null;
5942
e7d72fb9 5943 -- Display warning if returning unconstrained array
59366db6 5944
70482933
RK
5945 elsif Is_Array_Type (Retype)
5946 and then not Is_Constrained (Retype)
e7d72fb9 5947
2c1b72d7
AC
5948 -- Exclude cases where descriptor mechanism is set, since the
5949 -- VMS descriptor mechanisms allow such unconstrained returns.
e7d72fb9 5950
70482933 5951 and then Mechanism (E) not in Descriptor_Codes
e7d72fb9 5952
df3e68b1
HK
5953 -- Check appropriate warning is enabled (should we check for
5954 -- Warnings (Off) on specific entities here, probably so???)
e7d72fb9 5955
fbf5a39b 5956 and then Warn_On_Export_Import
e7d72fb9 5957
2c1b72d7
AC
5958 -- Exclude the VM case, since return of unconstrained arrays
5959 -- is properly handled in both the JVM and .NET cases.
e7d72fb9 5960
f3b57ab0 5961 and then VM_Target = No_VM
70482933 5962 then
fbf5a39b
AC
5963 Error_Msg_N
5964 ("?foreign convention function& should not return " &
5965 "unconstrained array", E);
70482933
RK
5966 return;
5967 end if;
5968 end if;
5969
5970 -- If any of the formals for an exported foreign convention
edd63e9b
ES
5971 -- subprogram have defaults, then emit an appropriate warning since
5972 -- this is odd (default cannot be used from non-Ada code)
70482933
RK
5973
5974 if Is_Exported (E) then
5975 F := First_Formal (E);
5976 while Present (F) loop
fbf5a39b
AC
5977 if Warn_On_Export_Import
5978 and then Present (Default_Value (F))
5979 then
70482933
RK
5980 Error_Msg_N
5981 ("?parameter cannot be defaulted in non-Ada call",
5982 Default_Value (F));
5983 end if;
5984
5985 Next_Formal (F);
5986 end loop;
5987 end if;
5988 end if;
5989
e7d72fb9
AC
5990 -- For VMS, descriptor mechanisms for parameters are allowed only for
5991 -- imported/exported subprograms. Moreover, the NCA descriptor is not
5992 -- allowed for parameters of exported subprograms.
70482933
RK
5993
5994 if OpenVMS_On_Target then
7d8b9c99
RD
5995 if Is_Exported (E) then
5996 F := First_Formal (E);
5997 while Present (F) loop
5998 if Mechanism (F) = By_Descriptor_NCA then
5999 Error_Msg_N
6000 ("'N'C'A' descriptor for parameter not permitted", F);
6001 Error_Msg_N
6002 ("\can only be used for imported subprogram", F);
6003 end if;
6004
6005 Next_Formal (F);
6006 end loop;
6007
6008 elsif not Is_Imported (E) then
70482933
RK
6009 F := First_Formal (E);
6010 while Present (F) loop
6011 if Mechanism (F) in Descriptor_Codes then
6012 Error_Msg_N
6013 ("descriptor mechanism for parameter not permitted", F);
6014 Error_Msg_N
7d8b9c99 6015 ("\can only be used for imported/exported subprogram", F);
70482933
RK
6016 end if;
6017
6018 Next_Formal (F);
6019 end loop;
6020 end if;
6021 end if;
edd63e9b
ES
6022
6023 -- Pragma Inline_Always is disallowed for dispatching subprograms
6024 -- because the address of such subprograms is saved in the dispatch
6025 -- table to support dispatching calls, and dispatching calls cannot
6026 -- be inlined. This is consistent with the restriction against using
6027 -- 'Access or 'Address on an Inline_Always subprogram.
6028
def46b54
RD
6029 if Is_Dispatching_Operation (E)
6030 and then Has_Pragma_Inline_Always (E)
6031 then
edd63e9b
ES
6032 Error_Msg_N
6033 ("pragma Inline_Always not allowed for dispatching subprograms", E);
6034 end if;
c6a9797e
RD
6035
6036 -- Because of the implicit representation of inherited predefined
6037 -- operators in the front-end, the overriding status of the operation
6038 -- may be affected when a full view of a type is analyzed, and this is
6039 -- not captured by the analysis of the corresponding type declaration.
6040 -- Therefore the correctness of a not-overriding indicator must be
6041 -- rechecked when the subprogram is frozen.
6042
6043 if Nkind (E) = N_Defining_Operator_Symbol
6044 and then not Error_Posted (Parent (E))
6045 then
6046 Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
6047 end if;
70482933
RK
6048 end Freeze_Subprogram;
6049
15ce9ca2
AC
6050 ----------------------
6051 -- Is_Fully_Defined --
6052 ----------------------
70482933 6053
70482933
RK
6054 function Is_Fully_Defined (T : Entity_Id) return Boolean is
6055 begin
6056 if Ekind (T) = E_Class_Wide_Type then
6057 return Is_Fully_Defined (Etype (T));
657a9dd9
AC
6058
6059 elsif Is_Array_Type (T) then
6060 return Is_Fully_Defined (Component_Type (T));
6061
6062 elsif Is_Record_Type (T)
6063 and not Is_Private_Type (T)
6064 then
ee094616
RD
6065 -- Verify that the record type has no components with private types
6066 -- without completion.
657a9dd9
AC
6067
6068 declare
6069 Comp : Entity_Id;
bde58e32 6070
657a9dd9
AC
6071 begin
6072 Comp := First_Component (T);
657a9dd9
AC
6073 while Present (Comp) loop
6074 if not Is_Fully_Defined (Etype (Comp)) then
6075 return False;
6076 end if;
6077
6078 Next_Component (Comp);
6079 end loop;
6080 return True;
6081 end;
6082
30537990 6083 -- For the designated type of an access to subprogram, all types in
4519314c
AC
6084 -- the profile must be fully defined.
6085
6086 elsif Ekind (T) = E_Subprogram_Type then
6087 declare
6088 F : Entity_Id;
6089
6090 begin
6091 F := First_Formal (T);
6092 while Present (F) loop
6093 if not Is_Fully_Defined (Etype (F)) then
6094 return False;
6095 end if;
6096
6097 Next_Formal (F);
6098 end loop;
6099
6100 return Is_Fully_Defined (Etype (T));
6101 end;
6102
86cde7b1
RD
6103 else
6104 return not Is_Private_Type (T)
6105 or else Present (Full_View (Base_Type (T)));
70482933
RK
6106 end if;
6107 end Is_Fully_Defined;
6108
70d904ca 6109 ---------------------------------
70482933
RK
6110 -- Process_Default_Expressions --
6111 ---------------------------------
6112
6113 procedure Process_Default_Expressions
6114 (E : Entity_Id;
6115 After : in out Node_Id)
6116 is
6117 Loc : constant Source_Ptr := Sloc (E);
6118 Dbody : Node_Id;
6119 Formal : Node_Id;
6120 Dcopy : Node_Id;
6121 Dnam : Entity_Id;
6122
6123 begin
6124 Set_Default_Expressions_Processed (E);
6125
ee094616
RD
6126 -- A subprogram instance and its associated anonymous subprogram share
6127 -- their signature. The default expression functions are defined in the
6128 -- wrapper packages for the anonymous subprogram, and should not be
6129 -- generated again for the instance.
70482933
RK
6130
6131 if Is_Generic_Instance (E)
6132 and then Present (Alias (E))
6133 and then Default_Expressions_Processed (Alias (E))
6134 then
6135 return;
6136 end if;
6137
6138 Formal := First_Formal (E);
70482933
RK
6139 while Present (Formal) loop
6140 if Present (Default_Value (Formal)) then
6141
6142 -- We work with a copy of the default expression because we
6143 -- do not want to disturb the original, since this would mess
6144 -- up the conformance checking.
6145
6146 Dcopy := New_Copy_Tree (Default_Value (Formal));
6147
6148 -- The analysis of the expression may generate insert actions,
6149 -- which of course must not be executed. We wrap those actions
6150 -- in a procedure that is not called, and later on eliminated.
6151 -- The following cases have no side-effects, and are analyzed
6152 -- directly.
6153
6154 if Nkind (Dcopy) = N_Identifier
6155 or else Nkind (Dcopy) = N_Expanded_Name
6156 or else Nkind (Dcopy) = N_Integer_Literal
6157 or else (Nkind (Dcopy) = N_Real_Literal
6158 and then not Vax_Float (Etype (Dcopy)))
6159 or else Nkind (Dcopy) = N_Character_Literal
6160 or else Nkind (Dcopy) = N_String_Literal
86cde7b1 6161 or else Known_Null (Dcopy)
70482933
RK
6162 or else (Nkind (Dcopy) = N_Attribute_Reference
6163 and then
6164 Attribute_Name (Dcopy) = Name_Null_Parameter)
70482933
RK
6165 then
6166
6167 -- If there is no default function, we must still do a full
ee094616
RD
6168 -- analyze call on the default value, to ensure that all error
6169 -- checks are performed, e.g. those associated with static
6170 -- evaluation. Note: this branch will always be taken if the
6171 -- analyzer is turned off (but we still need the error checks).
70482933
RK
6172
6173 -- Note: the setting of parent here is to meet the requirement
6174 -- that we can only analyze the expression while attached to
6175 -- the tree. Really the requirement is that the parent chain
6176 -- be set, we don't actually need to be in the tree.
6177
6178 Set_Parent (Dcopy, Declaration_Node (Formal));
6179 Analyze (Dcopy);
6180
6181 -- Default expressions are resolved with their own type if the
6182 -- context is generic, to avoid anomalies with private types.
6183
6184 if Ekind (Scope (E)) = E_Generic_Package then
fbf5a39b 6185 Resolve (Dcopy);
70482933
RK
6186 else
6187 Resolve (Dcopy, Etype (Formal));
6188 end if;
6189
6190 -- If that resolved expression will raise constraint error,
6191 -- then flag the default value as raising constraint error.
6192 -- This allows a proper error message on the calls.
6193
6194 if Raises_Constraint_Error (Dcopy) then
6195 Set_Raises_Constraint_Error (Default_Value (Formal));
6196 end if;
6197
6198 -- If the default is a parameterless call, we use the name of
6199 -- the called function directly, and there is no body to build.
6200
6201 elsif Nkind (Dcopy) = N_Function_Call
6202 and then No (Parameter_Associations (Dcopy))
6203 then
6204 null;
6205
6206 -- Else construct and analyze the body of a wrapper procedure
6207 -- that contains an object declaration to hold the expression.
6208 -- Given that this is done only to complete the analysis, it
6209 -- simpler to build a procedure than a function which might
6210 -- involve secondary stack expansion.
6211
6212 else
b29def53 6213 Dnam := Make_Temporary (Loc, 'D');
70482933
RK
6214
6215 Dbody :=
6216 Make_Subprogram_Body (Loc,
6217 Specification =>
6218 Make_Procedure_Specification (Loc,
6219 Defining_Unit_Name => Dnam),
6220
6221 Declarations => New_List (
6222 Make_Object_Declaration (Loc,
2c1b72d7
AC
6223 Defining_Identifier => Make_Temporary (Loc, 'T'),
6224 Object_Definition =>
df3e68b1 6225 New_Occurrence_Of (Etype (Formal), Loc),
2c1b72d7 6226 Expression => New_Copy_Tree (Dcopy))),
70482933
RK
6227
6228 Handled_Statement_Sequence =>
6229 Make_Handled_Sequence_Of_Statements (Loc,
2c1b72d7 6230 Statements => Empty_List));
70482933
RK
6231
6232 Set_Scope (Dnam, Scope (E));
6233 Set_Assignment_OK (First (Declarations (Dbody)));
6234 Set_Is_Eliminated (Dnam);
6235 Insert_After (After, Dbody);
6236 Analyze (Dbody);
6237 After := Dbody;
6238 end if;
6239 end if;
6240
6241 Next_Formal (Formal);
6242 end loop;
70482933
RK
6243 end Process_Default_Expressions;
6244
6245 ----------------------------------------
6246 -- Set_Component_Alignment_If_Not_Set --
6247 ----------------------------------------
6248
6249 procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
6250 begin
6251 -- Ignore if not base type, subtypes don't need anything
6252
6253 if Typ /= Base_Type (Typ) then
6254 return;
6255 end if;
6256
6257 -- Do not override existing representation
6258
6259 if Is_Packed (Typ) then
6260 return;
6261
6262 elsif Has_Specified_Layout (Typ) then
6263 return;
6264
6265 elsif Component_Alignment (Typ) /= Calign_Default then
6266 return;
6267
6268 else
6269 Set_Component_Alignment
6270 (Typ, Scope_Stack.Table
6271 (Scope_Stack.Last).Component_Alignment_Default);
6272 end if;
6273 end Set_Component_Alignment_If_Not_Set;
6274
c6823a20
EB
6275 ------------------
6276 -- Undelay_Type --
6277 ------------------
6278
6279 procedure Undelay_Type (T : Entity_Id) is
6280 begin
6281 Set_Has_Delayed_Freeze (T, False);
6282 Set_Freeze_Node (T, Empty);
6283
6284 -- Since we don't want T to have a Freeze_Node, we don't want its
6285 -- Full_View or Corresponding_Record_Type to have one either.
6286
6287 -- ??? Fundamentally, this whole handling is a kludge. What we really
ee094616
RD
6288 -- want is to be sure that for an Itype that's part of record R and is a
6289 -- subtype of type T, that it's frozen after the later of the freeze
c6823a20
EB
6290 -- points of R and T. We have no way of doing that directly, so what we
6291 -- do is force most such Itypes to be frozen as part of freezing R via
6292 -- this procedure and only delay the ones that need to be delayed
ee094616
RD
6293 -- (mostly the designated types of access types that are defined as part
6294 -- of the record).
c6823a20
EB
6295
6296 if Is_Private_Type (T)
6297 and then Present (Full_View (T))
6298 and then Is_Itype (Full_View (T))
6299 and then Is_Record_Type (Scope (Full_View (T)))
6300 then
6301 Undelay_Type (Full_View (T));
6302 end if;
6303
6304 if Is_Concurrent_Type (T)
6305 and then Present (Corresponding_Record_Type (T))
6306 and then Is_Itype (Corresponding_Record_Type (T))
6307 and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
6308 then
6309 Undelay_Type (Corresponding_Record_Type (T));
6310 end if;
6311 end Undelay_Type;
6312
fbf5a39b
AC
6313 ------------------
6314 -- Warn_Overlay --
6315 ------------------
6316
6317 procedure Warn_Overlay
6318 (Expr : Node_Id;
6319 Typ : Entity_Id;
6320 Nam : Entity_Id)
6321 is
6322 Ent : constant Entity_Id := Entity (Nam);
49e90211 6323 -- The object to which the address clause applies
fbf5a39b
AC
6324
6325 Init : Node_Id;
6326 Old : Entity_Id := Empty;
6327 Decl : Node_Id;
6328
6329 begin
6330 -- No warning if address clause overlay warnings are off
6331
6332 if not Address_Clause_Overlay_Warnings then
6333 return;
6334 end if;
6335
6336 -- No warning if there is an explicit initialization
6337
6338 Init := Original_Node (Expression (Declaration_Node (Ent)));
6339
6340 if Present (Init) and then Comes_From_Source (Init) then
6341 return;
6342 end if;
6343
edd63e9b 6344 -- We only give the warning for non-imported entities of a type for
0ac73189 6345 -- which a non-null base init proc is defined, or for objects of access
a5d83d61 6346 -- types with implicit null initialization, or when Normalize_Scalars
0ac73189
AC
6347 -- applies and the type is scalar or a string type (the latter being
6348 -- tested for because predefined String types are initialized by inline
a5d83d61
AC
6349 -- code rather than by an init_proc). Note that we do not give the
6350 -- warning for Initialize_Scalars, since we suppressed initialization
e526d0c7 6351 -- in this case. Also, do not warn if Suppress_Initialization is set.
fbf5a39b
AC
6352
6353 if Present (Expr)
fbf5a39b 6354 and then not Is_Imported (Ent)
e526d0c7 6355 and then not Initialization_Suppressed (Typ)
0ac73189 6356 and then (Has_Non_Null_Base_Init_Proc (Typ)
e526d0c7
AC
6357 or else Is_Access_Type (Typ)
6358 or else (Normalize_Scalars
6359 and then (Is_Scalar_Type (Typ)
6360 or else Is_String_Type (Typ))))
fbf5a39b
AC
6361 then
6362 if Nkind (Expr) = N_Attribute_Reference
6363 and then Is_Entity_Name (Prefix (Expr))
6364 then
6365 Old := Entity (Prefix (Expr));
6366
6367 elsif Is_Entity_Name (Expr)
6368 and then Ekind (Entity (Expr)) = E_Constant
6369 then
6370 Decl := Declaration_Node (Entity (Expr));
6371
6372 if Nkind (Decl) = N_Object_Declaration
6373 and then Present (Expression (Decl))
6374 and then Nkind (Expression (Decl)) = N_Attribute_Reference
6375 and then Is_Entity_Name (Prefix (Expression (Decl)))
6376 then
6377 Old := Entity (Prefix (Expression (Decl)));
6378
6379 elsif Nkind (Expr) = N_Function_Call then
6380 return;
6381 end if;
6382
ee094616
RD
6383 -- A function call (most likely to To_Address) is probably not an
6384 -- overlay, so skip warning. Ditto if the function call was inlined
6385 -- and transformed into an entity.
fbf5a39b
AC
6386
6387 elsif Nkind (Original_Node (Expr)) = N_Function_Call then
6388 return;
6389 end if;
6390
6391 Decl := Next (Parent (Expr));
6392
6393 -- If a pragma Import follows, we assume that it is for the current
6394 -- target of the address clause, and skip the warning.
6395
6396 if Present (Decl)
6397 and then Nkind (Decl) = N_Pragma
1b24ada5 6398 and then Pragma_Name (Decl) = Name_Import
fbf5a39b
AC
6399 then
6400 return;
6401 end if;
6402
6403 if Present (Old) then
6404 Error_Msg_Node_2 := Old;
6405 Error_Msg_N
6406 ("default initialization of & may modify &?",
6407 Nam);
6408 else
6409 Error_Msg_N
6410 ("default initialization of & may modify overlaid storage?",
6411 Nam);
6412 end if;
6413
6414 -- Add friendly warning if initialization comes from a packed array
6415 -- component.
6416
6417 if Is_Record_Type (Typ) then
6418 declare
6419 Comp : Entity_Id;
6420
6421 begin
6422 Comp := First_Component (Typ);
fbf5a39b
AC
6423 while Present (Comp) loop
6424 if Nkind (Parent (Comp)) = N_Component_Declaration
6425 and then Present (Expression (Parent (Comp)))
6426 then
6427 exit;
6428 elsif Is_Array_Type (Etype (Comp))
6429 and then Present (Packed_Array_Type (Etype (Comp)))
6430 then
6431 Error_Msg_NE
3f1ede06
RD
6432 ("\packed array component& " &
6433 "will be initialized to zero?",
6434 Nam, Comp);
fbf5a39b
AC
6435 exit;
6436 else
6437 Next_Component (Comp);
6438 end if;
6439 end loop;
6440 end;
6441 end if;
6442
6443 Error_Msg_N
3f1ede06 6444 ("\use pragma Import for & to " &
86cde7b1 6445 "suppress initialization (RM B.1(24))?",
3f1ede06 6446 Nam);
fbf5a39b
AC
6447 end if;
6448 end Warn_Overlay;
6449
70482933 6450end Freeze;