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