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