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