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