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