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