]>
Commit | Line | Data |
---|---|---|
1 | ------------------------------------------------------------------------------ | |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- E X P _ C H 6 -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
9 | -- Copyright (C) 1992-2016, Free Software Foundation, Inc. -- | |
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- -- | |
13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- | |
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 -- | |
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. -- | |
20 | -- -- | |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- | |
23 | -- -- | |
24 | ------------------------------------------------------------------------------ | |
25 | ||
26 | with Atree; use Atree; | |
27 | with Checks; use Checks; | |
28 | with Contracts; use Contracts; | |
29 | with Debug; use Debug; | |
30 | with Einfo; use Einfo; | |
31 | with Errout; use Errout; | |
32 | with Elists; use Elists; | |
33 | with Exp_Aggr; use Exp_Aggr; | |
34 | with Exp_Atag; use Exp_Atag; | |
35 | with Exp_Ch2; use Exp_Ch2; | |
36 | with Exp_Ch3; use Exp_Ch3; | |
37 | with Exp_Ch7; use Exp_Ch7; | |
38 | with Exp_Ch9; use Exp_Ch9; | |
39 | with Exp_Dbug; use Exp_Dbug; | |
40 | with Exp_Disp; use Exp_Disp; | |
41 | with Exp_Dist; use Exp_Dist; | |
42 | with Exp_Intr; use Exp_Intr; | |
43 | with Exp_Pakd; use Exp_Pakd; | |
44 | with Exp_Tss; use Exp_Tss; | |
45 | with Exp_Util; use Exp_Util; | |
46 | with Freeze; use Freeze; | |
47 | with Inline; use Inline; | |
48 | with Lib; use Lib; | |
49 | with Namet; use Namet; | |
50 | with Nlists; use Nlists; | |
51 | with Nmake; use Nmake; | |
52 | with Opt; use Opt; | |
53 | with Restrict; use Restrict; | |
54 | with Rident; use Rident; | |
55 | with Rtsfind; use Rtsfind; | |
56 | with Sem; use Sem; | |
57 | with Sem_Aux; use Sem_Aux; | |
58 | with Sem_Ch6; use Sem_Ch6; | |
59 | with Sem_Ch8; use Sem_Ch8; | |
60 | with Sem_Ch12; use Sem_Ch12; | |
61 | with Sem_Ch13; use Sem_Ch13; | |
62 | with Sem_Dim; use Sem_Dim; | |
63 | with Sem_Disp; use Sem_Disp; | |
64 | with Sem_Dist; use Sem_Dist; | |
65 | with Sem_Eval; use Sem_Eval; | |
66 | with Sem_Mech; use Sem_Mech; | |
67 | with Sem_Res; use Sem_Res; | |
68 | with Sem_SCIL; use Sem_SCIL; | |
69 | with Sem_Util; use Sem_Util; | |
70 | with Sinfo; use Sinfo; | |
71 | with Snames; use Snames; | |
72 | with Stand; use Stand; | |
73 | with Targparm; use Targparm; | |
74 | with Tbuild; use Tbuild; | |
75 | with Uintp; use Uintp; | |
76 | with Validsw; use Validsw; | |
77 | ||
78 | package body Exp_Ch6 is | |
79 | ||
80 | ----------------------- | |
81 | -- Local Subprograms -- | |
82 | ----------------------- | |
83 | ||
84 | procedure Add_Access_Actual_To_Build_In_Place_Call | |
85 | (Function_Call : Node_Id; | |
86 | Function_Id : Entity_Id; | |
87 | Return_Object : Node_Id; | |
88 | Is_Access : Boolean := False); | |
89 | -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the | |
90 | -- object name given by Return_Object and add the attribute to the end of | |
91 | -- the actual parameter list associated with the build-in-place function | |
92 | -- call denoted by Function_Call. However, if Is_Access is True, then | |
93 | -- Return_Object is already an access expression, in which case it's passed | |
94 | -- along directly to the build-in-place function. Finally, if Return_Object | |
95 | -- is empty, then pass a null literal as the actual. | |
96 | ||
97 | procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
98 | (Function_Call : Node_Id; | |
99 | Function_Id : Entity_Id; | |
100 | Alloc_Form : BIP_Allocation_Form := Unspecified; | |
101 | Alloc_Form_Exp : Node_Id := Empty; | |
102 | Pool_Actual : Node_Id := Make_Null (No_Location)); | |
103 | -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place | |
104 | -- function call that returns a caller-unknown-size result (BIP_Alloc_Form | |
105 | -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it, | |
106 | -- otherwise pass a literal corresponding to the Alloc_Form parameter | |
107 | -- (which must not be Unspecified in that case). Pool_Actual is the | |
108 | -- parameter to pass to BIP_Storage_Pool. | |
109 | ||
110 | procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call | |
111 | (Func_Call : Node_Id; | |
112 | Func_Id : Entity_Id; | |
113 | Ptr_Typ : Entity_Id := Empty; | |
114 | Master_Exp : Node_Id := Empty); | |
115 | -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs | |
116 | -- finalization actions, add an actual parameter which is a pointer to the | |
117 | -- finalization master of the caller. If Master_Exp is not Empty, then that | |
118 | -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this | |
119 | -- will result in an automatic "null" value for the actual. | |
120 | ||
121 | procedure Add_Task_Actuals_To_Build_In_Place_Call | |
122 | (Function_Call : Node_Id; | |
123 | Function_Id : Entity_Id; | |
124 | Master_Actual : Node_Id; | |
125 | Chain : Node_Id := Empty); | |
126 | -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type | |
127 | -- contains tasks, add two actual parameters: the master, and a pointer to | |
128 | -- the caller's activation chain. Master_Actual is the actual parameter | |
129 | -- expression to pass for the master. In most cases, this is the current | |
130 | -- master (_master). The two exceptions are: If the function call is the | |
131 | -- initialization expression for an allocator, we pass the master of the | |
132 | -- access type. If the function call is the initialization expression for a | |
133 | -- return object, we pass along the master passed in by the caller. In most | |
134 | -- contexts, the activation chain to pass is the local one, which is | |
135 | -- indicated by No (Chain). However, in an allocator, the caller passes in | |
136 | -- the activation Chain. Note: Master_Actual can be Empty, but only if | |
137 | -- there are no tasks. | |
138 | ||
139 | procedure Check_Overriding_Operation (Subp : Entity_Id); | |
140 | -- Subp is a dispatching operation. Check whether it may override an | |
141 | -- inherited private operation, in which case its DT entry is that of | |
142 | -- the hidden operation, not the one it may have received earlier. | |
143 | -- This must be done before emitting the code to set the corresponding | |
144 | -- DT to the address of the subprogram. The actual placement of Subp in | |
145 | -- the proper place in the list of primitive operations is done in | |
146 | -- Declare_Inherited_Private_Subprograms, which also has to deal with | |
147 | -- implicit operations. This duplication is unavoidable for now??? | |
148 | ||
149 | procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id); | |
150 | -- This procedure is called only if the subprogram body N, whose spec | |
151 | -- has the given entity Spec, contains a parameterless recursive call. | |
152 | -- It attempts to generate runtime code to detect if this a case of | |
153 | -- infinite recursion. | |
154 | -- | |
155 | -- The body is scanned to determine dependencies. If the only external | |
156 | -- dependencies are on a small set of scalar variables, then the values | |
157 | -- of these variables are captured on entry to the subprogram, and if | |
158 | -- the values are not changed for the call, we know immediately that | |
159 | -- we have an infinite recursion. | |
160 | ||
161 | procedure Expand_Actuals | |
162 | (N : Node_Id; | |
163 | Subp : Entity_Id; | |
164 | Post_Call : out List_Id); | |
165 | -- Return a list of actions to take place after the call in Post_Call. The | |
166 | -- call will later be rewritten as an Expression_With_Actions, with the | |
167 | -- Post_Call actions inserted, and the call inside. | |
168 | -- | |
169 | -- For each actual of an in-out or out parameter which is a numeric (view) | |
170 | -- conversion of the form T (A), where A denotes a variable, we insert the | |
171 | -- declaration: | |
172 | -- | |
173 | -- Temp : T[ := T (A)]; | |
174 | -- | |
175 | -- prior to the call. Then we replace the actual with a reference to Temp, | |
176 | -- and append the assignment: | |
177 | -- | |
178 | -- A := TypeA (Temp); | |
179 | -- | |
180 | -- after the call. Here TypeA is the actual type of variable A. For out | |
181 | -- parameters, the initial declaration has no expression. If A is not an | |
182 | -- entity name, we generate instead: | |
183 | -- | |
184 | -- Var : TypeA renames A; | |
185 | -- Temp : T := Var; -- omitting expression for out parameter. | |
186 | -- ... | |
187 | -- Var := TypeA (Temp); | |
188 | -- | |
189 | -- For other in-out parameters, we emit the required constraint checks | |
190 | -- before and/or after the call. | |
191 | -- | |
192 | -- For all parameter modes, actuals that denote components and slices of | |
193 | -- packed arrays are expanded into suitable temporaries. | |
194 | -- | |
195 | -- For non-scalar objects that are possibly unaligned, add call by copy | |
196 | -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT). | |
197 | -- | |
198 | -- For OUT and IN OUT parameters, add predicate checks after the call | |
199 | -- based on the predicates of the actual type. | |
200 | ||
201 | procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id); | |
202 | -- Does the main work of Expand_Call. Post_Call is as for Expand_Actuals. | |
203 | ||
204 | procedure Expand_Ctrl_Function_Call (N : Node_Id); | |
205 | -- N is a function call which returns a controlled object. Transform the | |
206 | -- call into a temporary which retrieves the returned object from the | |
207 | -- secondary stack using 'reference. | |
208 | ||
209 | procedure Expand_Non_Function_Return (N : Node_Id); | |
210 | -- Expand a simple return statement found in a procedure body, entry body, | |
211 | -- accept statement, or an extended return statement. Note that all non- | |
212 | -- function returns are simple return statements. | |
213 | ||
214 | function Expand_Protected_Object_Reference | |
215 | (N : Node_Id; | |
216 | Scop : Entity_Id) return Node_Id; | |
217 | ||
218 | procedure Expand_Protected_Subprogram_Call | |
219 | (N : Node_Id; | |
220 | Subp : Entity_Id; | |
221 | Scop : Entity_Id); | |
222 | -- A call to a protected subprogram within the protected object may appear | |
223 | -- as a regular call. The list of actuals must be expanded to contain a | |
224 | -- reference to the object itself, and the call becomes a call to the | |
225 | -- corresponding protected subprogram. | |
226 | ||
227 | procedure Expand_Simple_Function_Return (N : Node_Id); | |
228 | -- Expand simple return from function. In the case where we are returning | |
229 | -- from a function body this is called by Expand_N_Simple_Return_Statement. | |
230 | ||
231 | function Has_Unconstrained_Access_Discriminants | |
232 | (Subtyp : Entity_Id) return Boolean; | |
233 | -- Returns True if the given subtype is unconstrained and has one or more | |
234 | -- access discriminants. | |
235 | ||
236 | procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id); | |
237 | -- Insert the Post_Call list previously produced by routine Expand_Actuals | |
238 | -- or Expand_Call_Helper into the tree. | |
239 | ||
240 | procedure Rewrite_Function_Call_For_C (N : Node_Id); | |
241 | -- When generating C code, replace a call to a function that returns an | |
242 | -- array into the generated procedure with an additional out parameter. | |
243 | ||
244 | procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id); | |
245 | -- N is a return statement for a function that returns its result on the | |
246 | -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the | |
247 | -- function and all blocks and loops that the return statement is jumping | |
248 | -- out of. This ensures that the secondary stack is not released; otherwise | |
249 | -- the function result would be reclaimed before returning to the caller. | |
250 | ||
251 | ---------------------------------------------- | |
252 | -- Add_Access_Actual_To_Build_In_Place_Call -- | |
253 | ---------------------------------------------- | |
254 | ||
255 | procedure Add_Access_Actual_To_Build_In_Place_Call | |
256 | (Function_Call : Node_Id; | |
257 | Function_Id : Entity_Id; | |
258 | Return_Object : Node_Id; | |
259 | Is_Access : Boolean := False) | |
260 | is | |
261 | Loc : constant Source_Ptr := Sloc (Function_Call); | |
262 | Obj_Address : Node_Id; | |
263 | Obj_Acc_Formal : Entity_Id; | |
264 | ||
265 | begin | |
266 | -- Locate the implicit access parameter in the called function | |
267 | ||
268 | Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access); | |
269 | ||
270 | -- If no return object is provided, then pass null | |
271 | ||
272 | if not Present (Return_Object) then | |
273 | Obj_Address := Make_Null (Loc); | |
274 | Set_Parent (Obj_Address, Function_Call); | |
275 | ||
276 | -- If Return_Object is already an expression of an access type, then use | |
277 | -- it directly, since it must be an access value denoting the return | |
278 | -- object, and couldn't possibly be the return object itself. | |
279 | ||
280 | elsif Is_Access then | |
281 | Obj_Address := Return_Object; | |
282 | Set_Parent (Obj_Address, Function_Call); | |
283 | ||
284 | -- Apply Unrestricted_Access to caller's return object | |
285 | ||
286 | else | |
287 | Obj_Address := | |
288 | Make_Attribute_Reference (Loc, | |
289 | Prefix => Return_Object, | |
290 | Attribute_Name => Name_Unrestricted_Access); | |
291 | ||
292 | Set_Parent (Return_Object, Obj_Address); | |
293 | Set_Parent (Obj_Address, Function_Call); | |
294 | end if; | |
295 | ||
296 | Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal)); | |
297 | ||
298 | -- Build the parameter association for the new actual and add it to the | |
299 | -- end of the function's actuals. | |
300 | ||
301 | Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address); | |
302 | end Add_Access_Actual_To_Build_In_Place_Call; | |
303 | ||
304 | ------------------------------------------------------ | |
305 | -- Add_Unconstrained_Actuals_To_Build_In_Place_Call -- | |
306 | ------------------------------------------------------ | |
307 | ||
308 | procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
309 | (Function_Call : Node_Id; | |
310 | Function_Id : Entity_Id; | |
311 | Alloc_Form : BIP_Allocation_Form := Unspecified; | |
312 | Alloc_Form_Exp : Node_Id := Empty; | |
313 | Pool_Actual : Node_Id := Make_Null (No_Location)) | |
314 | is | |
315 | Loc : constant Source_Ptr := Sloc (Function_Call); | |
316 | Alloc_Form_Actual : Node_Id; | |
317 | Alloc_Form_Formal : Node_Id; | |
318 | Pool_Formal : Node_Id; | |
319 | ||
320 | begin | |
321 | -- The allocation form generally doesn't need to be passed in the case | |
322 | -- of a constrained result subtype, since normally the caller performs | |
323 | -- the allocation in that case. However this formal is still needed in | |
324 | -- the case where the function has a tagged result, because generally | |
325 | -- such functions can be called in a dispatching context and such calls | |
326 | -- must be handled like calls to class-wide functions. | |
327 | ||
328 | if Is_Constrained (Underlying_Type (Etype (Function_Id))) | |
329 | and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id))) | |
330 | then | |
331 | return; | |
332 | end if; | |
333 | ||
334 | -- Locate the implicit allocation form parameter in the called function. | |
335 | -- Maybe it would be better for each implicit formal of a build-in-place | |
336 | -- function to have a flag or a Uint attribute to identify it. ??? | |
337 | ||
338 | Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form); | |
339 | ||
340 | if Present (Alloc_Form_Exp) then | |
341 | pragma Assert (Alloc_Form = Unspecified); | |
342 | ||
343 | Alloc_Form_Actual := Alloc_Form_Exp; | |
344 | ||
345 | else | |
346 | pragma Assert (Alloc_Form /= Unspecified); | |
347 | ||
348 | Alloc_Form_Actual := | |
349 | Make_Integer_Literal (Loc, | |
350 | Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form))); | |
351 | end if; | |
352 | ||
353 | Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal)); | |
354 | ||
355 | -- Build the parameter association for the new actual and add it to the | |
356 | -- end of the function's actuals. | |
357 | ||
358 | Add_Extra_Actual_To_Call | |
359 | (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual); | |
360 | ||
361 | -- Pass the Storage_Pool parameter. This parameter is omitted on | |
362 | -- ZFP as those targets do not support pools. | |
363 | ||
364 | if RTE_Available (RE_Root_Storage_Pool_Ptr) then | |
365 | Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool); | |
366 | Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal)); | |
367 | Add_Extra_Actual_To_Call | |
368 | (Function_Call, Pool_Formal, Pool_Actual); | |
369 | end if; | |
370 | end Add_Unconstrained_Actuals_To_Build_In_Place_Call; | |
371 | ||
372 | ----------------------------------------------------------- | |
373 | -- Add_Finalization_Master_Actual_To_Build_In_Place_Call -- | |
374 | ----------------------------------------------------------- | |
375 | ||
376 | procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call | |
377 | (Func_Call : Node_Id; | |
378 | Func_Id : Entity_Id; | |
379 | Ptr_Typ : Entity_Id := Empty; | |
380 | Master_Exp : Node_Id := Empty) | |
381 | is | |
382 | begin | |
383 | if not Needs_BIP_Finalization_Master (Func_Id) then | |
384 | return; | |
385 | end if; | |
386 | ||
387 | declare | |
388 | Formal : constant Entity_Id := | |
389 | Build_In_Place_Formal (Func_Id, BIP_Finalization_Master); | |
390 | Loc : constant Source_Ptr := Sloc (Func_Call); | |
391 | ||
392 | Actual : Node_Id; | |
393 | Desig_Typ : Entity_Id; | |
394 | ||
395 | begin | |
396 | -- If there is a finalization master actual, such as the implicit | |
397 | -- finalization master of an enclosing build-in-place function, | |
398 | -- then this must be added as an extra actual of the call. | |
399 | ||
400 | if Present (Master_Exp) then | |
401 | Actual := Master_Exp; | |
402 | ||
403 | -- Case where the context does not require an actual master | |
404 | ||
405 | elsif No (Ptr_Typ) then | |
406 | Actual := Make_Null (Loc); | |
407 | ||
408 | else | |
409 | Desig_Typ := Directly_Designated_Type (Ptr_Typ); | |
410 | ||
411 | -- Check for a library-level access type whose designated type has | |
412 | -- supressed finalization. Such an access types lack a master. | |
413 | -- Pass a null actual to the callee in order to signal a missing | |
414 | -- master. | |
415 | ||
416 | if Is_Library_Level_Entity (Ptr_Typ) | |
417 | and then Finalize_Storage_Only (Desig_Typ) | |
418 | then | |
419 | Actual := Make_Null (Loc); | |
420 | ||
421 | -- Types in need of finalization actions | |
422 | ||
423 | elsif Needs_Finalization (Desig_Typ) then | |
424 | ||
425 | -- The general mechanism of creating finalization masters for | |
426 | -- anonymous access types is disabled by default, otherwise | |
427 | -- finalization masters will pop all over the place. Such types | |
428 | -- use context-specific masters. | |
429 | ||
430 | if Ekind (Ptr_Typ) = E_Anonymous_Access_Type | |
431 | and then No (Finalization_Master (Ptr_Typ)) | |
432 | then | |
433 | Build_Anonymous_Master (Ptr_Typ); | |
434 | end if; | |
435 | ||
436 | -- Access-to-controlled types should always have a master | |
437 | ||
438 | pragma Assert (Present (Finalization_Master (Ptr_Typ))); | |
439 | ||
440 | Actual := | |
441 | Make_Attribute_Reference (Loc, | |
442 | Prefix => | |
443 | New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc), | |
444 | Attribute_Name => Name_Unrestricted_Access); | |
445 | ||
446 | -- Tagged types | |
447 | ||
448 | else | |
449 | Actual := Make_Null (Loc); | |
450 | end if; | |
451 | end if; | |
452 | ||
453 | Analyze_And_Resolve (Actual, Etype (Formal)); | |
454 | ||
455 | -- Build the parameter association for the new actual and add it to | |
456 | -- the end of the function's actuals. | |
457 | ||
458 | Add_Extra_Actual_To_Call (Func_Call, Formal, Actual); | |
459 | end; | |
460 | end Add_Finalization_Master_Actual_To_Build_In_Place_Call; | |
461 | ||
462 | ------------------------------ | |
463 | -- Add_Extra_Actual_To_Call -- | |
464 | ------------------------------ | |
465 | ||
466 | procedure Add_Extra_Actual_To_Call | |
467 | (Subprogram_Call : Node_Id; | |
468 | Extra_Formal : Entity_Id; | |
469 | Extra_Actual : Node_Id) | |
470 | is | |
471 | Loc : constant Source_Ptr := Sloc (Subprogram_Call); | |
472 | Param_Assoc : Node_Id; | |
473 | ||
474 | begin | |
475 | Param_Assoc := | |
476 | Make_Parameter_Association (Loc, | |
477 | Selector_Name => New_Occurrence_Of (Extra_Formal, Loc), | |
478 | Explicit_Actual_Parameter => Extra_Actual); | |
479 | ||
480 | Set_Parent (Param_Assoc, Subprogram_Call); | |
481 | Set_Parent (Extra_Actual, Param_Assoc); | |
482 | ||
483 | if Present (Parameter_Associations (Subprogram_Call)) then | |
484 | if Nkind (Last (Parameter_Associations (Subprogram_Call))) = | |
485 | N_Parameter_Association | |
486 | then | |
487 | ||
488 | -- Find last named actual, and append | |
489 | ||
490 | declare | |
491 | L : Node_Id; | |
492 | begin | |
493 | L := First_Actual (Subprogram_Call); | |
494 | while Present (L) loop | |
495 | if No (Next_Actual (L)) then | |
496 | Set_Next_Named_Actual (Parent (L), Extra_Actual); | |
497 | exit; | |
498 | end if; | |
499 | Next_Actual (L); | |
500 | end loop; | |
501 | end; | |
502 | ||
503 | else | |
504 | Set_First_Named_Actual (Subprogram_Call, Extra_Actual); | |
505 | end if; | |
506 | ||
507 | Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call)); | |
508 | ||
509 | else | |
510 | Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc)); | |
511 | Set_First_Named_Actual (Subprogram_Call, Extra_Actual); | |
512 | end if; | |
513 | end Add_Extra_Actual_To_Call; | |
514 | ||
515 | --------------------------------------------- | |
516 | -- Add_Task_Actuals_To_Build_In_Place_Call -- | |
517 | --------------------------------------------- | |
518 | ||
519 | procedure Add_Task_Actuals_To_Build_In_Place_Call | |
520 | (Function_Call : Node_Id; | |
521 | Function_Id : Entity_Id; | |
522 | Master_Actual : Node_Id; | |
523 | Chain : Node_Id := Empty) | |
524 | is | |
525 | Loc : constant Source_Ptr := Sloc (Function_Call); | |
526 | Result_Subt : constant Entity_Id := | |
527 | Available_View (Etype (Function_Id)); | |
528 | Actual : Node_Id; | |
529 | Chain_Actual : Node_Id; | |
530 | Chain_Formal : Node_Id; | |
531 | Master_Formal : Node_Id; | |
532 | ||
533 | begin | |
534 | -- No such extra parameters are needed if there are no tasks | |
535 | ||
536 | if not Has_Task (Result_Subt) then | |
537 | return; | |
538 | end if; | |
539 | ||
540 | Actual := Master_Actual; | |
541 | ||
542 | -- Use a dummy _master actual in case of No_Task_Hierarchy | |
543 | ||
544 | if Restriction_Active (No_Task_Hierarchy) then | |
545 | Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc); | |
546 | ||
547 | -- In the case where we use the master associated with an access type, | |
548 | -- the actual is an entity and requires an explicit reference. | |
549 | ||
550 | elsif Nkind (Actual) = N_Defining_Identifier then | |
551 | Actual := New_Occurrence_Of (Actual, Loc); | |
552 | end if; | |
553 | ||
554 | -- Locate the implicit master parameter in the called function | |
555 | ||
556 | Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master); | |
557 | Analyze_And_Resolve (Actual, Etype (Master_Formal)); | |
558 | ||
559 | -- Build the parameter association for the new actual and add it to the | |
560 | -- end of the function's actuals. | |
561 | ||
562 | Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual); | |
563 | ||
564 | -- Locate the implicit activation chain parameter in the called function | |
565 | ||
566 | Chain_Formal := | |
567 | Build_In_Place_Formal (Function_Id, BIP_Activation_Chain); | |
568 | ||
569 | -- Create the actual which is a pointer to the current activation chain | |
570 | ||
571 | if No (Chain) then | |
572 | Chain_Actual := | |
573 | Make_Attribute_Reference (Loc, | |
574 | Prefix => Make_Identifier (Loc, Name_uChain), | |
575 | Attribute_Name => Name_Unrestricted_Access); | |
576 | ||
577 | -- Allocator case; make a reference to the Chain passed in by the caller | |
578 | ||
579 | else | |
580 | Chain_Actual := | |
581 | Make_Attribute_Reference (Loc, | |
582 | Prefix => New_Occurrence_Of (Chain, Loc), | |
583 | Attribute_Name => Name_Unrestricted_Access); | |
584 | end if; | |
585 | ||
586 | Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal)); | |
587 | ||
588 | -- Build the parameter association for the new actual and add it to the | |
589 | -- end of the function's actuals. | |
590 | ||
591 | Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual); | |
592 | end Add_Task_Actuals_To_Build_In_Place_Call; | |
593 | ||
594 | ----------------------- | |
595 | -- BIP_Formal_Suffix -- | |
596 | ----------------------- | |
597 | ||
598 | function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is | |
599 | begin | |
600 | case Kind is | |
601 | when BIP_Alloc_Form => | |
602 | return "BIPalloc"; | |
603 | ||
604 | when BIP_Storage_Pool => | |
605 | return "BIPstoragepool"; | |
606 | ||
607 | when BIP_Finalization_Master => | |
608 | return "BIPfinalizationmaster"; | |
609 | ||
610 | when BIP_Task_Master => | |
611 | return "BIPtaskmaster"; | |
612 | ||
613 | when BIP_Activation_Chain => | |
614 | return "BIPactivationchain"; | |
615 | ||
616 | when BIP_Object_Access => | |
617 | return "BIPaccess"; | |
618 | end case; | |
619 | end BIP_Formal_Suffix; | |
620 | ||
621 | --------------------------- | |
622 | -- Build_In_Place_Formal -- | |
623 | --------------------------- | |
624 | ||
625 | function Build_In_Place_Formal | |
626 | (Func : Entity_Id; | |
627 | Kind : BIP_Formal_Kind) return Entity_Id | |
628 | is | |
629 | Formal_Name : constant Name_Id := | |
630 | New_External_Name | |
631 | (Chars (Func), BIP_Formal_Suffix (Kind)); | |
632 | Extra_Formal : Entity_Id := Extra_Formals (Func); | |
633 | ||
634 | begin | |
635 | -- Maybe it would be better for each implicit formal of a build-in-place | |
636 | -- function to have a flag or a Uint attribute to identify it. ??? | |
637 | ||
638 | -- The return type in the function declaration may have been a limited | |
639 | -- view, and the extra formals for the function were not generated at | |
640 | -- that point. At the point of call the full view must be available and | |
641 | -- the extra formals can be created. | |
642 | ||
643 | if No (Extra_Formal) then | |
644 | Create_Extra_Formals (Func); | |
645 | Extra_Formal := Extra_Formals (Func); | |
646 | end if; | |
647 | ||
648 | loop | |
649 | pragma Assert (Present (Extra_Formal)); | |
650 | exit when Chars (Extra_Formal) = Formal_Name; | |
651 | ||
652 | Next_Formal_With_Extras (Extra_Formal); | |
653 | end loop; | |
654 | ||
655 | return Extra_Formal; | |
656 | end Build_In_Place_Formal; | |
657 | ||
658 | ------------------------------- | |
659 | -- Build_Procedure_Body_Form -- | |
660 | ------------------------------- | |
661 | ||
662 | function Build_Procedure_Body_Form | |
663 | (Func_Id : Entity_Id; | |
664 | Func_Body : Node_Id) return Node_Id | |
665 | is | |
666 | Loc : constant Source_Ptr := Sloc (Func_Body); | |
667 | ||
668 | Proc_Decl : constant Node_Id := | |
669 | Next (Unit_Declaration_Node (Func_Id)); | |
670 | -- It is assumed that the next node following the declaration of the | |
671 | -- corresponding subprogram spec is the declaration of the procedure | |
672 | -- form. | |
673 | ||
674 | Proc_Id : constant Entity_Id := Defining_Entity (Proc_Decl); | |
675 | ||
676 | procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id); | |
677 | -- Replace each return statement found in the list Stmts with an | |
678 | -- assignment of the return expression to parameter Param_Id. | |
679 | ||
680 | --------------------- | |
681 | -- Replace_Returns -- | |
682 | --------------------- | |
683 | ||
684 | procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id) is | |
685 | Stmt : Node_Id; | |
686 | ||
687 | begin | |
688 | Stmt := First (Stmts); | |
689 | while Present (Stmt) loop | |
690 | if Nkind (Stmt) = N_Block_Statement then | |
691 | Replace_Returns (Param_Id, Statements (Stmt)); | |
692 | ||
693 | elsif Nkind (Stmt) = N_Case_Statement then | |
694 | declare | |
695 | Alt : Node_Id; | |
696 | begin | |
697 | Alt := First (Alternatives (Stmt)); | |
698 | while Present (Alt) loop | |
699 | Replace_Returns (Param_Id, Statements (Alt)); | |
700 | Next (Alt); | |
701 | end loop; | |
702 | end; | |
703 | ||
704 | elsif Nkind (Stmt) = N_Extended_Return_Statement then | |
705 | declare | |
706 | Ret_Obj : constant Entity_Id := | |
707 | Defining_Entity | |
708 | (First (Return_Object_Declarations (Stmt))); | |
709 | Assign : constant Node_Id := | |
710 | Make_Assignment_Statement (Sloc (Stmt), | |
711 | Name => | |
712 | New_Occurrence_Of (Param_Id, Loc), | |
713 | Expression => | |
714 | New_Occurrence_Of (Ret_Obj, Sloc (Stmt))); | |
715 | Stmts : List_Id; | |
716 | ||
717 | begin | |
718 | -- The extended return may just contain the declaration | |
719 | ||
720 | if Present (Handled_Statement_Sequence (Stmt)) then | |
721 | Stmts := Statements (Handled_Statement_Sequence (Stmt)); | |
722 | else | |
723 | Stmts := New_List; | |
724 | end if; | |
725 | ||
726 | Set_Assignment_OK (Name (Assign)); | |
727 | ||
728 | Rewrite (Stmt, | |
729 | Make_Block_Statement (Sloc (Stmt), | |
730 | Declarations => | |
731 | Return_Object_Declarations (Stmt), | |
732 | Handled_Statement_Sequence => | |
733 | Make_Handled_Sequence_Of_Statements (Loc, | |
734 | Statements => Stmts))); | |
735 | ||
736 | Replace_Returns (Param_Id, Stmts); | |
737 | ||
738 | Append_To (Stmts, Assign); | |
739 | Append_To (Stmts, Make_Simple_Return_Statement (Loc)); | |
740 | end; | |
741 | ||
742 | elsif Nkind (Stmt) = N_If_Statement then | |
743 | Replace_Returns (Param_Id, Then_Statements (Stmt)); | |
744 | Replace_Returns (Param_Id, Else_Statements (Stmt)); | |
745 | ||
746 | declare | |
747 | Part : Node_Id; | |
748 | begin | |
749 | Part := First (Elsif_Parts (Stmt)); | |
750 | while Present (Part) loop | |
751 | Replace_Returns (Param_Id, Then_Statements (Part)); | |
752 | Next (Part); | |
753 | end loop; | |
754 | end; | |
755 | ||
756 | elsif Nkind (Stmt) = N_Loop_Statement then | |
757 | Replace_Returns (Param_Id, Statements (Stmt)); | |
758 | ||
759 | elsif Nkind (Stmt) = N_Simple_Return_Statement then | |
760 | ||
761 | -- Generate: | |
762 | -- Param := Expr; | |
763 | -- return; | |
764 | ||
765 | Rewrite (Stmt, | |
766 | Make_Assignment_Statement (Sloc (Stmt), | |
767 | Name => New_Occurrence_Of (Param_Id, Loc), | |
768 | Expression => Relocate_Node (Expression (Stmt)))); | |
769 | ||
770 | Insert_After (Stmt, Make_Simple_Return_Statement (Loc)); | |
771 | ||
772 | -- Skip the added return | |
773 | ||
774 | Next (Stmt); | |
775 | end if; | |
776 | ||
777 | Next (Stmt); | |
778 | end loop; | |
779 | end Replace_Returns; | |
780 | ||
781 | -- Local variables | |
782 | ||
783 | Stmts : List_Id; | |
784 | New_Body : Node_Id; | |
785 | ||
786 | -- Start of processing for Build_Procedure_Body_Form | |
787 | ||
788 | begin | |
789 | -- This routine replaces the original function body: | |
790 | ||
791 | -- function F (...) return Array_Typ is | |
792 | -- begin | |
793 | -- ... | |
794 | -- return Something; | |
795 | -- end F; | |
796 | ||
797 | -- with the following: | |
798 | ||
799 | -- procedure P (..., Result : out Array_Typ) is | |
800 | -- begin | |
801 | -- ... | |
802 | -- Result := Something; | |
803 | -- end P; | |
804 | ||
805 | Stmts := | |
806 | Statements (Handled_Statement_Sequence (Func_Body)); | |
807 | Replace_Returns (Last_Entity (Proc_Id), Stmts); | |
808 | ||
809 | New_Body := | |
810 | Make_Subprogram_Body (Loc, | |
811 | Specification => | |
812 | Copy_Subprogram_Spec (Specification (Proc_Decl)), | |
813 | Declarations => Declarations (Func_Body), | |
814 | Handled_Statement_Sequence => | |
815 | Make_Handled_Sequence_Of_Statements (Loc, | |
816 | Statements => Stmts)); | |
817 | ||
818 | -- If the function is a generic instance, so is the new procedure. | |
819 | -- Set flag accordingly so that the proper renaming declarations are | |
820 | -- generated. | |
821 | ||
822 | Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id)); | |
823 | return New_Body; | |
824 | end Build_Procedure_Body_Form; | |
825 | ||
826 | -------------------------------- | |
827 | -- Check_Overriding_Operation -- | |
828 | -------------------------------- | |
829 | ||
830 | procedure Check_Overriding_Operation (Subp : Entity_Id) is | |
831 | Typ : constant Entity_Id := Find_Dispatching_Type (Subp); | |
832 | Op_List : constant Elist_Id := Primitive_Operations (Typ); | |
833 | Op_Elmt : Elmt_Id; | |
834 | Prim_Op : Entity_Id; | |
835 | Par_Op : Entity_Id; | |
836 | ||
837 | begin | |
838 | if Is_Derived_Type (Typ) | |
839 | and then not Is_Private_Type (Typ) | |
840 | and then In_Open_Scopes (Scope (Etype (Typ))) | |
841 | and then Is_Base_Type (Typ) | |
842 | then | |
843 | -- Subp overrides an inherited private operation if there is an | |
844 | -- inherited operation with a different name than Subp (see | |
845 | -- Derive_Subprogram) whose Alias is a hidden subprogram with the | |
846 | -- same name as Subp. | |
847 | ||
848 | Op_Elmt := First_Elmt (Op_List); | |
849 | while Present (Op_Elmt) loop | |
850 | Prim_Op := Node (Op_Elmt); | |
851 | Par_Op := Alias (Prim_Op); | |
852 | ||
853 | if Present (Par_Op) | |
854 | and then not Comes_From_Source (Prim_Op) | |
855 | and then Chars (Prim_Op) /= Chars (Par_Op) | |
856 | and then Chars (Par_Op) = Chars (Subp) | |
857 | and then Is_Hidden (Par_Op) | |
858 | and then Type_Conformant (Prim_Op, Subp) | |
859 | then | |
860 | Set_DT_Position_Value (Subp, DT_Position (Prim_Op)); | |
861 | end if; | |
862 | ||
863 | Next_Elmt (Op_Elmt); | |
864 | end loop; | |
865 | end if; | |
866 | end Check_Overriding_Operation; | |
867 | ||
868 | ------------------------------- | |
869 | -- Detect_Infinite_Recursion -- | |
870 | ------------------------------- | |
871 | ||
872 | procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is | |
873 | Loc : constant Source_Ptr := Sloc (N); | |
874 | ||
875 | Var_List : constant Elist_Id := New_Elmt_List; | |
876 | -- List of globals referenced by body of procedure | |
877 | ||
878 | Call_List : constant Elist_Id := New_Elmt_List; | |
879 | -- List of recursive calls in body of procedure | |
880 | ||
881 | Shad_List : constant Elist_Id := New_Elmt_List; | |
882 | -- List of entity id's for entities created to capture the value of | |
883 | -- referenced globals on entry to the procedure. | |
884 | ||
885 | Scop : constant Uint := Scope_Depth (Spec); | |
886 | -- This is used to record the scope depth of the current procedure, so | |
887 | -- that we can identify global references. | |
888 | ||
889 | Max_Vars : constant := 4; | |
890 | -- Do not test more than four global variables | |
891 | ||
892 | Count_Vars : Natural := 0; | |
893 | -- Count variables found so far | |
894 | ||
895 | Var : Entity_Id; | |
896 | Elm : Elmt_Id; | |
897 | Ent : Entity_Id; | |
898 | Call : Elmt_Id; | |
899 | Decl : Node_Id; | |
900 | Test : Node_Id; | |
901 | Elm1 : Elmt_Id; | |
902 | Elm2 : Elmt_Id; | |
903 | Last : Node_Id; | |
904 | ||
905 | function Process (Nod : Node_Id) return Traverse_Result; | |
906 | -- Function to traverse the subprogram body (using Traverse_Func) | |
907 | ||
908 | ------------- | |
909 | -- Process -- | |
910 | ------------- | |
911 | ||
912 | function Process (Nod : Node_Id) return Traverse_Result is | |
913 | begin | |
914 | -- Procedure call | |
915 | ||
916 | if Nkind (Nod) = N_Procedure_Call_Statement then | |
917 | ||
918 | -- Case of one of the detected recursive calls | |
919 | ||
920 | if Is_Entity_Name (Name (Nod)) | |
921 | and then Has_Recursive_Call (Entity (Name (Nod))) | |
922 | and then Entity (Name (Nod)) = Spec | |
923 | then | |
924 | Append_Elmt (Nod, Call_List); | |
925 | return Skip; | |
926 | ||
927 | -- Any other procedure call may have side effects | |
928 | ||
929 | else | |
930 | return Abandon; | |
931 | end if; | |
932 | ||
933 | -- A call to a pure function can always be ignored | |
934 | ||
935 | elsif Nkind (Nod) = N_Function_Call | |
936 | and then Is_Entity_Name (Name (Nod)) | |
937 | and then Is_Pure (Entity (Name (Nod))) | |
938 | then | |
939 | return Skip; | |
940 | ||
941 | -- Case of an identifier reference | |
942 | ||
943 | elsif Nkind (Nod) = N_Identifier then | |
944 | Ent := Entity (Nod); | |
945 | ||
946 | -- If no entity, then ignore the reference | |
947 | ||
948 | -- Not clear why this can happen. To investigate, remove this | |
949 | -- test and look at the crash that occurs here in 3401-004 ??? | |
950 | ||
951 | if No (Ent) then | |
952 | return Skip; | |
953 | ||
954 | -- Ignore entities with no Scope, again not clear how this | |
955 | -- can happen, to investigate, look at 4108-008 ??? | |
956 | ||
957 | elsif No (Scope (Ent)) then | |
958 | return Skip; | |
959 | ||
960 | -- Ignore the reference if not to a more global object | |
961 | ||
962 | elsif Scope_Depth (Scope (Ent)) >= Scop then | |
963 | return Skip; | |
964 | ||
965 | -- References to types, exceptions and constants are always OK | |
966 | ||
967 | elsif Is_Type (Ent) | |
968 | or else Ekind (Ent) = E_Exception | |
969 | or else Ekind (Ent) = E_Constant | |
970 | then | |
971 | return Skip; | |
972 | ||
973 | -- If other than a non-volatile scalar variable, we have some | |
974 | -- kind of global reference (e.g. to a function) that we cannot | |
975 | -- deal with so we forget the attempt. | |
976 | ||
977 | elsif Ekind (Ent) /= E_Variable | |
978 | or else not Is_Scalar_Type (Etype (Ent)) | |
979 | or else Treat_As_Volatile (Ent) | |
980 | then | |
981 | return Abandon; | |
982 | ||
983 | -- Otherwise we have a reference to a global scalar | |
984 | ||
985 | else | |
986 | -- Loop through global entities already detected | |
987 | ||
988 | Elm := First_Elmt (Var_List); | |
989 | loop | |
990 | -- If not detected before, record this new global reference | |
991 | ||
992 | if No (Elm) then | |
993 | Count_Vars := Count_Vars + 1; | |
994 | ||
995 | if Count_Vars <= Max_Vars then | |
996 | Append_Elmt (Entity (Nod), Var_List); | |
997 | else | |
998 | return Abandon; | |
999 | end if; | |
1000 | ||
1001 | exit; | |
1002 | ||
1003 | -- If recorded before, ignore | |
1004 | ||
1005 | elsif Node (Elm) = Entity (Nod) then | |
1006 | return Skip; | |
1007 | ||
1008 | -- Otherwise keep looking | |
1009 | ||
1010 | else | |
1011 | Next_Elmt (Elm); | |
1012 | end if; | |
1013 | end loop; | |
1014 | ||
1015 | return Skip; | |
1016 | end if; | |
1017 | ||
1018 | -- For all other node kinds, recursively visit syntactic children | |
1019 | ||
1020 | else | |
1021 | return OK; | |
1022 | end if; | |
1023 | end Process; | |
1024 | ||
1025 | function Traverse_Body is new Traverse_Func (Process); | |
1026 | ||
1027 | -- Start of processing for Detect_Infinite_Recursion | |
1028 | ||
1029 | begin | |
1030 | -- Do not attempt detection in No_Implicit_Conditional mode, since we | |
1031 | -- won't be able to generate the code to handle the recursion in any | |
1032 | -- case. | |
1033 | ||
1034 | if Restriction_Active (No_Implicit_Conditionals) then | |
1035 | return; | |
1036 | end if; | |
1037 | ||
1038 | -- Otherwise do traversal and quit if we get abandon signal | |
1039 | ||
1040 | if Traverse_Body (N) = Abandon then | |
1041 | return; | |
1042 | ||
1043 | -- We must have a call, since Has_Recursive_Call was set. If not just | |
1044 | -- ignore (this is only an error check, so if we have a funny situation, | |
1045 | -- due to bugs or errors, we do not want to bomb). | |
1046 | ||
1047 | elsif Is_Empty_Elmt_List (Call_List) then | |
1048 | return; | |
1049 | end if; | |
1050 | ||
1051 | -- Here is the case where we detect recursion at compile time | |
1052 | ||
1053 | -- Push our current scope for analyzing the declarations and code that | |
1054 | -- we will insert for the checking. | |
1055 | ||
1056 | Push_Scope (Spec); | |
1057 | ||
1058 | -- This loop builds temporary variables for each of the referenced | |
1059 | -- globals, so that at the end of the loop the list Shad_List contains | |
1060 | -- these temporaries in one-to-one correspondence with the elements in | |
1061 | -- Var_List. | |
1062 | ||
1063 | Last := Empty; | |
1064 | Elm := First_Elmt (Var_List); | |
1065 | while Present (Elm) loop | |
1066 | Var := Node (Elm); | |
1067 | Ent := Make_Temporary (Loc, 'S'); | |
1068 | Append_Elmt (Ent, Shad_List); | |
1069 | ||
1070 | -- Insert a declaration for this temporary at the start of the | |
1071 | -- declarations for the procedure. The temporaries are declared as | |
1072 | -- constant objects initialized to the current values of the | |
1073 | -- corresponding temporaries. | |
1074 | ||
1075 | Decl := | |
1076 | Make_Object_Declaration (Loc, | |
1077 | Defining_Identifier => Ent, | |
1078 | Object_Definition => New_Occurrence_Of (Etype (Var), Loc), | |
1079 | Constant_Present => True, | |
1080 | Expression => New_Occurrence_Of (Var, Loc)); | |
1081 | ||
1082 | if No (Last) then | |
1083 | Prepend (Decl, Declarations (N)); | |
1084 | else | |
1085 | Insert_After (Last, Decl); | |
1086 | end if; | |
1087 | ||
1088 | Last := Decl; | |
1089 | Analyze (Decl); | |
1090 | Next_Elmt (Elm); | |
1091 | end loop; | |
1092 | ||
1093 | -- Loop through calls | |
1094 | ||
1095 | Call := First_Elmt (Call_List); | |
1096 | while Present (Call) loop | |
1097 | ||
1098 | -- Build a predicate expression of the form | |
1099 | ||
1100 | -- True | |
1101 | -- and then global1 = temp1 | |
1102 | -- and then global2 = temp2 | |
1103 | -- ... | |
1104 | ||
1105 | -- This predicate determines if any of the global values | |
1106 | -- referenced by the procedure have changed since the | |
1107 | -- current call, if not an infinite recursion is assured. | |
1108 | ||
1109 | Test := New_Occurrence_Of (Standard_True, Loc); | |
1110 | ||
1111 | Elm1 := First_Elmt (Var_List); | |
1112 | Elm2 := First_Elmt (Shad_List); | |
1113 | while Present (Elm1) loop | |
1114 | Test := | |
1115 | Make_And_Then (Loc, | |
1116 | Left_Opnd => Test, | |
1117 | Right_Opnd => | |
1118 | Make_Op_Eq (Loc, | |
1119 | Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc), | |
1120 | Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc))); | |
1121 | ||
1122 | Next_Elmt (Elm1); | |
1123 | Next_Elmt (Elm2); | |
1124 | end loop; | |
1125 | ||
1126 | -- Now we replace the call with the sequence | |
1127 | ||
1128 | -- if no-changes (see above) then | |
1129 | -- raise Storage_Error; | |
1130 | -- else | |
1131 | -- original-call | |
1132 | -- end if; | |
1133 | ||
1134 | Rewrite (Node (Call), | |
1135 | Make_If_Statement (Loc, | |
1136 | Condition => Test, | |
1137 | Then_Statements => New_List ( | |
1138 | Make_Raise_Storage_Error (Loc, | |
1139 | Reason => SE_Infinite_Recursion)), | |
1140 | ||
1141 | Else_Statements => New_List ( | |
1142 | Relocate_Node (Node (Call))))); | |
1143 | ||
1144 | Analyze (Node (Call)); | |
1145 | ||
1146 | Next_Elmt (Call); | |
1147 | end loop; | |
1148 | ||
1149 | -- Remove temporary scope stack entry used for analysis | |
1150 | ||
1151 | Pop_Scope; | |
1152 | end Detect_Infinite_Recursion; | |
1153 | ||
1154 | -------------------- | |
1155 | -- Expand_Actuals -- | |
1156 | -------------------- | |
1157 | ||
1158 | procedure Expand_Actuals | |
1159 | (N : Node_Id; | |
1160 | Subp : Entity_Id; | |
1161 | Post_Call : out List_Id) | |
1162 | is | |
1163 | Loc : constant Source_Ptr := Sloc (N); | |
1164 | Actual : Node_Id; | |
1165 | Formal : Entity_Id; | |
1166 | N_Node : Node_Id; | |
1167 | E_Actual : Entity_Id; | |
1168 | E_Formal : Entity_Id; | |
1169 | ||
1170 | procedure Add_Call_By_Copy_Code; | |
1171 | -- For cases where the parameter must be passed by copy, this routine | |
1172 | -- generates a temporary variable into which the actual is copied and | |
1173 | -- then passes this as the parameter. For an OUT or IN OUT parameter, | |
1174 | -- an assignment is also generated to copy the result back. The call | |
1175 | -- also takes care of any constraint checks required for the type | |
1176 | -- conversion case (on both the way in and the way out). | |
1177 | ||
1178 | procedure Add_Simple_Call_By_Copy_Code; | |
1179 | -- This is similar to the above, but is used in cases where we know | |
1180 | -- that all that is needed is to simply create a temporary and copy | |
1181 | -- the value in and out of the temporary. | |
1182 | ||
1183 | procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id); | |
1184 | -- Perform copy-back for actual parameter Act which denotes a validation | |
1185 | -- variable. | |
1186 | ||
1187 | procedure Check_Fortran_Logical; | |
1188 | -- A value of type Logical that is passed through a formal parameter | |
1189 | -- must be normalized because .TRUE. usually does not have the same | |
1190 | -- representation as True. We assume that .FALSE. = False = 0. | |
1191 | -- What about functions that return a logical type ??? | |
1192 | ||
1193 | function Is_Legal_Copy return Boolean; | |
1194 | -- Check that an actual can be copied before generating the temporary | |
1195 | -- to be used in the call. If the actual is of a by_reference type then | |
1196 | -- the program is illegal (this can only happen in the presence of | |
1197 | -- rep. clauses that force an incorrect alignment). If the formal is | |
1198 | -- a by_reference parameter imposed by a DEC pragma, emit a warning to | |
1199 | -- the effect that this might lead to unaligned arguments. | |
1200 | ||
1201 | function Make_Var (Actual : Node_Id) return Entity_Id; | |
1202 | -- Returns an entity that refers to the given actual parameter, Actual | |
1203 | -- (not including any type conversion). If Actual is an entity name, | |
1204 | -- then this entity is returned unchanged, otherwise a renaming is | |
1205 | -- created to provide an entity for the actual. | |
1206 | ||
1207 | procedure Reset_Packed_Prefix; | |
1208 | -- The expansion of a packed array component reference is delayed in | |
1209 | -- the context of a call. Now we need to complete the expansion, so we | |
1210 | -- unmark the analyzed bits in all prefixes. | |
1211 | ||
1212 | --------------------------- | |
1213 | -- Add_Call_By_Copy_Code -- | |
1214 | --------------------------- | |
1215 | ||
1216 | procedure Add_Call_By_Copy_Code is | |
1217 | Crep : Boolean; | |
1218 | Expr : Node_Id; | |
1219 | F_Typ : Entity_Id := Etype (Formal); | |
1220 | Indic : Node_Id; | |
1221 | Init : Node_Id; | |
1222 | Temp : Entity_Id; | |
1223 | V_Typ : Entity_Id; | |
1224 | Var : Entity_Id; | |
1225 | ||
1226 | begin | |
1227 | if not Is_Legal_Copy then | |
1228 | return; | |
1229 | end if; | |
1230 | ||
1231 | Temp := Make_Temporary (Loc, 'T', Actual); | |
1232 | ||
1233 | -- Handle formals whose type comes from the limited view | |
1234 | ||
1235 | if From_Limited_With (F_Typ) | |
1236 | and then Has_Non_Limited_View (F_Typ) | |
1237 | then | |
1238 | F_Typ := Non_Limited_View (F_Typ); | |
1239 | end if; | |
1240 | ||
1241 | -- Use formal type for temp, unless formal type is an unconstrained | |
1242 | -- array, in which case we don't have to worry about bounds checks, | |
1243 | -- and we use the actual type, since that has appropriate bounds. | |
1244 | ||
1245 | if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then | |
1246 | Indic := New_Occurrence_Of (Etype (Actual), Loc); | |
1247 | else | |
1248 | Indic := New_Occurrence_Of (F_Typ, Loc); | |
1249 | end if; | |
1250 | ||
1251 | if Nkind (Actual) = N_Type_Conversion then | |
1252 | V_Typ := Etype (Expression (Actual)); | |
1253 | ||
1254 | -- If the formal is an (in-)out parameter, capture the name | |
1255 | -- of the variable in order to build the post-call assignment. | |
1256 | ||
1257 | Var := Make_Var (Expression (Actual)); | |
1258 | ||
1259 | Crep := not Same_Representation | |
1260 | (F_Typ, Etype (Expression (Actual))); | |
1261 | ||
1262 | else | |
1263 | V_Typ := Etype (Actual); | |
1264 | Var := Make_Var (Actual); | |
1265 | Crep := False; | |
1266 | end if; | |
1267 | ||
1268 | -- Setup initialization for case of in out parameter, or an out | |
1269 | -- parameter where the formal is an unconstrained array (in the | |
1270 | -- latter case, we have to pass in an object with bounds). | |
1271 | ||
1272 | -- If this is an out parameter, the initial copy is wasteful, so as | |
1273 | -- an optimization for the one-dimensional case we extract the | |
1274 | -- bounds of the actual and build an uninitialized temporary of the | |
1275 | -- right size. | |
1276 | ||
1277 | if Ekind (Formal) = E_In_Out_Parameter | |
1278 | or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ)) | |
1279 | then | |
1280 | if Nkind (Actual) = N_Type_Conversion then | |
1281 | if Conversion_OK (Actual) then | |
1282 | Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); | |
1283 | else | |
1284 | Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); | |
1285 | end if; | |
1286 | ||
1287 | elsif Ekind (Formal) = E_Out_Parameter | |
1288 | and then Is_Array_Type (F_Typ) | |
1289 | and then Number_Dimensions (F_Typ) = 1 | |
1290 | and then not Has_Non_Null_Base_Init_Proc (F_Typ) | |
1291 | then | |
1292 | -- Actual is a one-dimensional array or slice, and the type | |
1293 | -- requires no initialization. Create a temporary of the | |
1294 | -- right size, but do not copy actual into it (optimization). | |
1295 | ||
1296 | Init := Empty; | |
1297 | Indic := | |
1298 | Make_Subtype_Indication (Loc, | |
1299 | Subtype_Mark => New_Occurrence_Of (F_Typ, Loc), | |
1300 | Constraint => | |
1301 | Make_Index_Or_Discriminant_Constraint (Loc, | |
1302 | Constraints => New_List ( | |
1303 | Make_Range (Loc, | |
1304 | Low_Bound => | |
1305 | Make_Attribute_Reference (Loc, | |
1306 | Prefix => New_Occurrence_Of (Var, Loc), | |
1307 | Attribute_Name => Name_First), | |
1308 | High_Bound => | |
1309 | Make_Attribute_Reference (Loc, | |
1310 | Prefix => New_Occurrence_Of (Var, Loc), | |
1311 | Attribute_Name => Name_Last))))); | |
1312 | ||
1313 | else | |
1314 | Init := New_Occurrence_Of (Var, Loc); | |
1315 | end if; | |
1316 | ||
1317 | -- An initialization is created for packed conversions as | |
1318 | -- actuals for out parameters to enable Make_Object_Declaration | |
1319 | -- to determine the proper subtype for N_Node. Note that this | |
1320 | -- is wasteful because the extra copying on the call side is | |
1321 | -- not required for such out parameters. ??? | |
1322 | ||
1323 | elsif Ekind (Formal) = E_Out_Parameter | |
1324 | and then Nkind (Actual) = N_Type_Conversion | |
1325 | and then (Is_Bit_Packed_Array (F_Typ) | |
1326 | or else | |
1327 | Is_Bit_Packed_Array (Etype (Expression (Actual)))) | |
1328 | then | |
1329 | if Conversion_OK (Actual) then | |
1330 | Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); | |
1331 | else | |
1332 | Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); | |
1333 | end if; | |
1334 | ||
1335 | elsif Ekind (Formal) = E_In_Parameter then | |
1336 | ||
1337 | -- Handle the case in which the actual is a type conversion | |
1338 | ||
1339 | if Nkind (Actual) = N_Type_Conversion then | |
1340 | if Conversion_OK (Actual) then | |
1341 | Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); | |
1342 | else | |
1343 | Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); | |
1344 | end if; | |
1345 | else | |
1346 | Init := New_Occurrence_Of (Var, Loc); | |
1347 | end if; | |
1348 | ||
1349 | else | |
1350 | Init := Empty; | |
1351 | end if; | |
1352 | ||
1353 | N_Node := | |
1354 | Make_Object_Declaration (Loc, | |
1355 | Defining_Identifier => Temp, | |
1356 | Object_Definition => Indic, | |
1357 | Expression => Init); | |
1358 | Set_Assignment_OK (N_Node); | |
1359 | Insert_Action (N, N_Node); | |
1360 | ||
1361 | -- Now, normally the deal here is that we use the defining | |
1362 | -- identifier created by that object declaration. There is | |
1363 | -- one exception to this. In the change of representation case | |
1364 | -- the above declaration will end up looking like: | |
1365 | ||
1366 | -- temp : type := identifier; | |
1367 | ||
1368 | -- And in this case we might as well use the identifier directly | |
1369 | -- and eliminate the temporary. Note that the analysis of the | |
1370 | -- declaration was not a waste of time in that case, since it is | |
1371 | -- what generated the necessary change of representation code. If | |
1372 | -- the change of representation introduced additional code, as in | |
1373 | -- a fixed-integer conversion, the expression is not an identifier | |
1374 | -- and must be kept. | |
1375 | ||
1376 | if Crep | |
1377 | and then Present (Expression (N_Node)) | |
1378 | and then Is_Entity_Name (Expression (N_Node)) | |
1379 | then | |
1380 | Temp := Entity (Expression (N_Node)); | |
1381 | Rewrite (N_Node, Make_Null_Statement (Loc)); | |
1382 | end if; | |
1383 | ||
1384 | -- For IN parameter, all we do is to replace the actual | |
1385 | ||
1386 | if Ekind (Formal) = E_In_Parameter then | |
1387 | Rewrite (Actual, New_Occurrence_Of (Temp, Loc)); | |
1388 | Analyze (Actual); | |
1389 | ||
1390 | -- Processing for OUT or IN OUT parameter | |
1391 | ||
1392 | else | |
1393 | -- Kill current value indications for the temporary variable we | |
1394 | -- created, since we just passed it as an OUT parameter. | |
1395 | ||
1396 | Kill_Current_Values (Temp); | |
1397 | Set_Is_Known_Valid (Temp, False); | |
1398 | ||
1399 | -- If type conversion, use reverse conversion on exit | |
1400 | ||
1401 | if Nkind (Actual) = N_Type_Conversion then | |
1402 | if Conversion_OK (Actual) then | |
1403 | Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc)); | |
1404 | else | |
1405 | Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc)); | |
1406 | end if; | |
1407 | else | |
1408 | Expr := New_Occurrence_Of (Temp, Loc); | |
1409 | end if; | |
1410 | ||
1411 | Rewrite (Actual, New_Occurrence_Of (Temp, Loc)); | |
1412 | Analyze (Actual); | |
1413 | ||
1414 | -- If the actual is a conversion of a packed reference, it may | |
1415 | -- already have been expanded by Remove_Side_Effects, and the | |
1416 | -- resulting variable is a temporary which does not designate | |
1417 | -- the proper out-parameter, which may not be addressable. In | |
1418 | -- that case, generate an assignment to the original expression | |
1419 | -- (before expansion of the packed reference) so that the proper | |
1420 | -- expansion of assignment to a packed component can take place. | |
1421 | ||
1422 | declare | |
1423 | Obj : Node_Id; | |
1424 | Lhs : Node_Id; | |
1425 | ||
1426 | begin | |
1427 | if Is_Renaming_Of_Object (Var) | |
1428 | and then Nkind (Renamed_Object (Var)) = N_Selected_Component | |
1429 | and then Nkind (Original_Node (Prefix (Renamed_Object (Var)))) | |
1430 | = N_Indexed_Component | |
1431 | and then | |
1432 | Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var)))) | |
1433 | then | |
1434 | Obj := Renamed_Object (Var); | |
1435 | Lhs := | |
1436 | Make_Selected_Component (Loc, | |
1437 | Prefix => | |
1438 | New_Copy_Tree (Original_Node (Prefix (Obj))), | |
1439 | Selector_Name => New_Copy (Selector_Name (Obj))); | |
1440 | Reset_Analyzed_Flags (Lhs); | |
1441 | ||
1442 | else | |
1443 | Lhs := New_Occurrence_Of (Var, Loc); | |
1444 | end if; | |
1445 | ||
1446 | Set_Assignment_OK (Lhs); | |
1447 | ||
1448 | if Is_Access_Type (E_Formal) | |
1449 | and then Is_Entity_Name (Lhs) | |
1450 | and then | |
1451 | Present (Effective_Extra_Accessibility (Entity (Lhs))) | |
1452 | then | |
1453 | -- Copyback target is an Ada 2012 stand-alone object of an | |
1454 | -- anonymous access type. | |
1455 | ||
1456 | pragma Assert (Ada_Version >= Ada_2012); | |
1457 | ||
1458 | if Type_Access_Level (E_Formal) > | |
1459 | Object_Access_Level (Lhs) | |
1460 | then | |
1461 | Append_To (Post_Call, | |
1462 | Make_Raise_Program_Error (Loc, | |
1463 | Reason => PE_Accessibility_Check_Failed)); | |
1464 | end if; | |
1465 | ||
1466 | Append_To (Post_Call, | |
1467 | Make_Assignment_Statement (Loc, | |
1468 | Name => Lhs, | |
1469 | Expression => Expr)); | |
1470 | ||
1471 | -- We would like to somehow suppress generation of the | |
1472 | -- extra_accessibility assignment generated by the expansion | |
1473 | -- of the above assignment statement. It's not a correctness | |
1474 | -- issue because the following assignment renders it dead, | |
1475 | -- but generating back-to-back assignments to the same | |
1476 | -- target is undesirable. ??? | |
1477 | ||
1478 | Append_To (Post_Call, | |
1479 | Make_Assignment_Statement (Loc, | |
1480 | Name => New_Occurrence_Of ( | |
1481 | Effective_Extra_Accessibility (Entity (Lhs)), Loc), | |
1482 | Expression => Make_Integer_Literal (Loc, | |
1483 | Type_Access_Level (E_Formal)))); | |
1484 | ||
1485 | else | |
1486 | Append_To (Post_Call, | |
1487 | Make_Assignment_Statement (Loc, | |
1488 | Name => Lhs, | |
1489 | Expression => Expr)); | |
1490 | end if; | |
1491 | end; | |
1492 | end if; | |
1493 | end Add_Call_By_Copy_Code; | |
1494 | ||
1495 | ---------------------------------- | |
1496 | -- Add_Simple_Call_By_Copy_Code -- | |
1497 | ---------------------------------- | |
1498 | ||
1499 | procedure Add_Simple_Call_By_Copy_Code is | |
1500 | Decl : Node_Id; | |
1501 | F_Typ : Entity_Id := Etype (Formal); | |
1502 | Incod : Node_Id; | |
1503 | Indic : Node_Id; | |
1504 | Lhs : Node_Id; | |
1505 | Outcod : Node_Id; | |
1506 | Rhs : Node_Id; | |
1507 | Temp : Entity_Id; | |
1508 | ||
1509 | begin | |
1510 | if not Is_Legal_Copy then | |
1511 | return; | |
1512 | end if; | |
1513 | ||
1514 | -- Handle formals whose type comes from the limited view | |
1515 | ||
1516 | if From_Limited_With (F_Typ) | |
1517 | and then Has_Non_Limited_View (F_Typ) | |
1518 | then | |
1519 | F_Typ := Non_Limited_View (F_Typ); | |
1520 | end if; | |
1521 | ||
1522 | -- Use formal type for temp, unless formal type is an unconstrained | |
1523 | -- array, in which case we don't have to worry about bounds checks, | |
1524 | -- and we use the actual type, since that has appropriate bounds. | |
1525 | ||
1526 | if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then | |
1527 | Indic := New_Occurrence_Of (Etype (Actual), Loc); | |
1528 | else | |
1529 | Indic := New_Occurrence_Of (F_Typ, Loc); | |
1530 | end if; | |
1531 | ||
1532 | -- Prepare to generate code | |
1533 | ||
1534 | Reset_Packed_Prefix; | |
1535 | ||
1536 | Temp := Make_Temporary (Loc, 'T', Actual); | |
1537 | Incod := Relocate_Node (Actual); | |
1538 | Outcod := New_Copy_Tree (Incod); | |
1539 | ||
1540 | -- Generate declaration of temporary variable, initializing it | |
1541 | -- with the input parameter unless we have an OUT formal or | |
1542 | -- this is an initialization call. | |
1543 | ||
1544 | -- If the formal is an out parameter with discriminants, the | |
1545 | -- discriminants must be captured even if the rest of the object | |
1546 | -- is in principle uninitialized, because the discriminants may | |
1547 | -- be read by the called subprogram. | |
1548 | ||
1549 | if Ekind (Formal) = E_Out_Parameter then | |
1550 | Incod := Empty; | |
1551 | ||
1552 | if Has_Discriminants (F_Typ) then | |
1553 | Indic := New_Occurrence_Of (Etype (Actual), Loc); | |
1554 | end if; | |
1555 | ||
1556 | elsif Inside_Init_Proc then | |
1557 | ||
1558 | -- Could use a comment here to match comment below ??? | |
1559 | ||
1560 | if Nkind (Actual) /= N_Selected_Component | |
1561 | or else | |
1562 | not Has_Discriminant_Dependent_Constraint | |
1563 | (Entity (Selector_Name (Actual))) | |
1564 | then | |
1565 | Incod := Empty; | |
1566 | ||
1567 | -- Otherwise, keep the component in order to generate the proper | |
1568 | -- actual subtype, that depends on enclosing discriminants. | |
1569 | ||
1570 | else | |
1571 | null; | |
1572 | end if; | |
1573 | end if; | |
1574 | ||
1575 | Decl := | |
1576 | Make_Object_Declaration (Loc, | |
1577 | Defining_Identifier => Temp, | |
1578 | Object_Definition => Indic, | |
1579 | Expression => Incod); | |
1580 | ||
1581 | if Inside_Init_Proc | |
1582 | and then No (Incod) | |
1583 | then | |
1584 | -- If the call is to initialize a component of a composite type, | |
1585 | -- and the component does not depend on discriminants, use the | |
1586 | -- actual type of the component. This is required in case the | |
1587 | -- component is constrained, because in general the formal of the | |
1588 | -- initialization procedure will be unconstrained. Note that if | |
1589 | -- the component being initialized is constrained by an enclosing | |
1590 | -- discriminant, the presence of the initialization in the | |
1591 | -- declaration will generate an expression for the actual subtype. | |
1592 | ||
1593 | Set_No_Initialization (Decl); | |
1594 | Set_Object_Definition (Decl, | |
1595 | New_Occurrence_Of (Etype (Actual), Loc)); | |
1596 | end if; | |
1597 | ||
1598 | Insert_Action (N, Decl); | |
1599 | ||
1600 | -- The actual is simply a reference to the temporary | |
1601 | ||
1602 | Rewrite (Actual, New_Occurrence_Of (Temp, Loc)); | |
1603 | ||
1604 | -- Generate copy out if OUT or IN OUT parameter | |
1605 | ||
1606 | if Ekind (Formal) /= E_In_Parameter then | |
1607 | Lhs := Outcod; | |
1608 | Rhs := New_Occurrence_Of (Temp, Loc); | |
1609 | ||
1610 | -- Deal with conversion | |
1611 | ||
1612 | if Nkind (Lhs) = N_Type_Conversion then | |
1613 | Lhs := Expression (Lhs); | |
1614 | Rhs := Convert_To (Etype (Actual), Rhs); | |
1615 | end if; | |
1616 | ||
1617 | Append_To (Post_Call, | |
1618 | Make_Assignment_Statement (Loc, | |
1619 | Name => Lhs, | |
1620 | Expression => Rhs)); | |
1621 | Set_Assignment_OK (Name (Last (Post_Call))); | |
1622 | end if; | |
1623 | end Add_Simple_Call_By_Copy_Code; | |
1624 | ||
1625 | -------------------------------------- | |
1626 | -- Add_Validation_Call_By_Copy_Code -- | |
1627 | -------------------------------------- | |
1628 | ||
1629 | procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id) is | |
1630 | Expr : Node_Id; | |
1631 | Obj : Node_Id; | |
1632 | Obj_Typ : Entity_Id; | |
1633 | Var : Node_Id; | |
1634 | Var_Id : Entity_Id; | |
1635 | ||
1636 | begin | |
1637 | Var := Act; | |
1638 | ||
1639 | -- Use the expression when the context qualifies a reference in some | |
1640 | -- fashion. | |
1641 | ||
1642 | while Nkind_In (Var, N_Qualified_Expression, | |
1643 | N_Type_Conversion, | |
1644 | N_Unchecked_Type_Conversion) | |
1645 | loop | |
1646 | Var := Expression (Var); | |
1647 | end loop; | |
1648 | ||
1649 | -- Copy the value of the validation variable back into the object | |
1650 | -- being validated. | |
1651 | ||
1652 | if Is_Entity_Name (Var) then | |
1653 | Var_Id := Entity (Var); | |
1654 | Obj := Validated_Object (Var_Id); | |
1655 | Obj_Typ := Etype (Obj); | |
1656 | ||
1657 | Expr := New_Occurrence_Of (Var_Id, Loc); | |
1658 | ||
1659 | -- A type conversion is needed when the validation variable and | |
1660 | -- the validated object carry different types. This case occurs | |
1661 | -- when the actual is qualified in some fashion. | |
1662 | ||
1663 | -- Common: | |
1664 | -- subtype Int is Integer range ...; | |
1665 | -- procedure Call (Val : in out Integer); | |
1666 | ||
1667 | -- Original: | |
1668 | -- Object : Int; | |
1669 | -- Call (Integer (Object)); | |
1670 | ||
1671 | -- Expanded: | |
1672 | -- Object : Int; | |
1673 | -- Var : Integer := Object; -- conversion to base type | |
1674 | -- if not Var'Valid then -- validity check | |
1675 | -- Call (Var); -- modify Var | |
1676 | -- Object := Int (Var); -- conversion to subtype | |
1677 | ||
1678 | if Etype (Var_Id) /= Obj_Typ then | |
1679 | Expr := | |
1680 | Make_Type_Conversion (Loc, | |
1681 | Subtype_Mark => New_Occurrence_Of (Obj_Typ, Loc), | |
1682 | Expression => Expr); | |
1683 | end if; | |
1684 | ||
1685 | -- Generate: | |
1686 | -- Object := Var; | |
1687 | -- <or> | |
1688 | -- Object := Object_Type (Var); | |
1689 | ||
1690 | Append_To (Post_Call, | |
1691 | Make_Assignment_Statement (Loc, | |
1692 | Name => Obj, | |
1693 | Expression => Expr)); | |
1694 | ||
1695 | -- If the flow reaches this point, then this routine was invoked with | |
1696 | -- an actual which does not denote a validation variable. | |
1697 | ||
1698 | else | |
1699 | pragma Assert (False); | |
1700 | null; | |
1701 | end if; | |
1702 | end Add_Validation_Call_By_Copy_Code; | |
1703 | ||
1704 | --------------------------- | |
1705 | -- Check_Fortran_Logical -- | |
1706 | --------------------------- | |
1707 | ||
1708 | procedure Check_Fortran_Logical is | |
1709 | Logical : constant Entity_Id := Etype (Formal); | |
1710 | Var : Entity_Id; | |
1711 | ||
1712 | -- Note: this is very incomplete, e.g. it does not handle arrays | |
1713 | -- of logical values. This is really not the right approach at all???) | |
1714 | ||
1715 | begin | |
1716 | if Convention (Subp) = Convention_Fortran | |
1717 | and then Root_Type (Etype (Formal)) = Standard_Boolean | |
1718 | and then Ekind (Formal) /= E_In_Parameter | |
1719 | then | |
1720 | Var := Make_Var (Actual); | |
1721 | Append_To (Post_Call, | |
1722 | Make_Assignment_Statement (Loc, | |
1723 | Name => New_Occurrence_Of (Var, Loc), | |
1724 | Expression => | |
1725 | Unchecked_Convert_To ( | |
1726 | Logical, | |
1727 | Make_Op_Ne (Loc, | |
1728 | Left_Opnd => New_Occurrence_Of (Var, Loc), | |
1729 | Right_Opnd => | |
1730 | Unchecked_Convert_To ( | |
1731 | Logical, | |
1732 | New_Occurrence_Of (Standard_False, Loc)))))); | |
1733 | end if; | |
1734 | end Check_Fortran_Logical; | |
1735 | ||
1736 | ------------------- | |
1737 | -- Is_Legal_Copy -- | |
1738 | ------------------- | |
1739 | ||
1740 | function Is_Legal_Copy return Boolean is | |
1741 | begin | |
1742 | -- An attempt to copy a value of such a type can only occur if | |
1743 | -- representation clauses give the actual a misaligned address. | |
1744 | ||
1745 | if Is_By_Reference_Type (Etype (Formal)) then | |
1746 | ||
1747 | -- If the front-end does not perform full type layout, the actual | |
1748 | -- may in fact be properly aligned but there is not enough front- | |
1749 | -- end information to determine this. In that case gigi will emit | |
1750 | -- an error if a copy is not legal, or generate the proper code. | |
1751 | -- For other backends we report the error now. | |
1752 | ||
1753 | -- Seems wrong to be issuing an error in the expander, since it | |
1754 | -- will be missed in -gnatc mode ??? | |
1755 | ||
1756 | if Frontend_Layout_On_Target then | |
1757 | Error_Msg_N | |
1758 | ("misaligned actual cannot be passed by reference", Actual); | |
1759 | end if; | |
1760 | ||
1761 | return False; | |
1762 | ||
1763 | -- For users of Starlet, we assume that the specification of by- | |
1764 | -- reference mechanism is mandatory. This may lead to unaligned | |
1765 | -- objects but at least for DEC legacy code it is known to work. | |
1766 | -- The warning will alert users of this code that a problem may | |
1767 | -- be lurking. | |
1768 | ||
1769 | elsif Mechanism (Formal) = By_Reference | |
1770 | and then Is_Valued_Procedure (Scope (Formal)) | |
1771 | then | |
1772 | Error_Msg_N | |
1773 | ("by_reference actual may be misaligned??", Actual); | |
1774 | return False; | |
1775 | ||
1776 | else | |
1777 | return True; | |
1778 | end if; | |
1779 | end Is_Legal_Copy; | |
1780 | ||
1781 | -------------- | |
1782 | -- Make_Var -- | |
1783 | -------------- | |
1784 | ||
1785 | function Make_Var (Actual : Node_Id) return Entity_Id is | |
1786 | Var : Entity_Id; | |
1787 | ||
1788 | begin | |
1789 | if Is_Entity_Name (Actual) then | |
1790 | return Entity (Actual); | |
1791 | ||
1792 | else | |
1793 | Var := Make_Temporary (Loc, 'T', Actual); | |
1794 | ||
1795 | N_Node := | |
1796 | Make_Object_Renaming_Declaration (Loc, | |
1797 | Defining_Identifier => Var, | |
1798 | Subtype_Mark => | |
1799 | New_Occurrence_Of (Etype (Actual), Loc), | |
1800 | Name => Relocate_Node (Actual)); | |
1801 | ||
1802 | Insert_Action (N, N_Node); | |
1803 | return Var; | |
1804 | end if; | |
1805 | end Make_Var; | |
1806 | ||
1807 | ------------------------- | |
1808 | -- Reset_Packed_Prefix -- | |
1809 | ------------------------- | |
1810 | ||
1811 | procedure Reset_Packed_Prefix is | |
1812 | Pfx : Node_Id := Actual; | |
1813 | begin | |
1814 | loop | |
1815 | Set_Analyzed (Pfx, False); | |
1816 | exit when | |
1817 | not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component); | |
1818 | Pfx := Prefix (Pfx); | |
1819 | end loop; | |
1820 | end Reset_Packed_Prefix; | |
1821 | ||
1822 | -- Start of processing for Expand_Actuals | |
1823 | ||
1824 | begin | |
1825 | Post_Call := New_List; | |
1826 | ||
1827 | Formal := First_Formal (Subp); | |
1828 | Actual := First_Actual (N); | |
1829 | while Present (Formal) loop | |
1830 | E_Formal := Etype (Formal); | |
1831 | E_Actual := Etype (Actual); | |
1832 | ||
1833 | -- Handle formals whose type comes from the limited view | |
1834 | ||
1835 | if From_Limited_With (E_Formal) | |
1836 | and then Has_Non_Limited_View (E_Formal) | |
1837 | then | |
1838 | E_Formal := Non_Limited_View (E_Formal); | |
1839 | end if; | |
1840 | ||
1841 | if Is_Scalar_Type (E_Formal) | |
1842 | or else Nkind (Actual) = N_Slice | |
1843 | then | |
1844 | Check_Fortran_Logical; | |
1845 | ||
1846 | -- RM 6.4.1 (11) | |
1847 | ||
1848 | elsif Ekind (Formal) /= E_Out_Parameter then | |
1849 | ||
1850 | -- The unusual case of the current instance of a protected type | |
1851 | -- requires special handling. This can only occur in the context | |
1852 | -- of a call within the body of a protected operation. | |
1853 | ||
1854 | if Is_Entity_Name (Actual) | |
1855 | and then Ekind (Entity (Actual)) = E_Protected_Type | |
1856 | and then In_Open_Scopes (Entity (Actual)) | |
1857 | then | |
1858 | if Scope (Subp) /= Entity (Actual) then | |
1859 | Error_Msg_N | |
1860 | ("operation outside protected type may not " | |
1861 | & "call back its protected operations??", Actual); | |
1862 | end if; | |
1863 | ||
1864 | Rewrite (Actual, | |
1865 | Expand_Protected_Object_Reference (N, Entity (Actual))); | |
1866 | end if; | |
1867 | ||
1868 | -- Ada 2005 (AI-318-02): If the actual parameter is a call to a | |
1869 | -- build-in-place function, then a temporary return object needs | |
1870 | -- to be created and access to it must be passed to the function. | |
1871 | -- Currently we limit such functions to those with inherently | |
1872 | -- limited result subtypes, but eventually we plan to expand the | |
1873 | -- functions that are treated as build-in-place to include other | |
1874 | -- composite result types. | |
1875 | ||
1876 | if Is_Build_In_Place_Function_Call (Actual) then | |
1877 | Make_Build_In_Place_Call_In_Anonymous_Context (Actual); | |
1878 | end if; | |
1879 | ||
1880 | Apply_Constraint_Check (Actual, E_Formal); | |
1881 | ||
1882 | -- Out parameter case. No constraint checks on access type | |
1883 | -- RM 6.4.1 (13) | |
1884 | ||
1885 | elsif Is_Access_Type (E_Formal) then | |
1886 | null; | |
1887 | ||
1888 | -- RM 6.4.1 (14) | |
1889 | ||
1890 | elsif Has_Discriminants (Base_Type (E_Formal)) | |
1891 | or else Has_Non_Null_Base_Init_Proc (E_Formal) | |
1892 | then | |
1893 | Apply_Constraint_Check (Actual, E_Formal); | |
1894 | ||
1895 | -- RM 6.4.1 (15) | |
1896 | ||
1897 | else | |
1898 | Apply_Constraint_Check (Actual, Base_Type (E_Formal)); | |
1899 | end if; | |
1900 | ||
1901 | -- Processing for IN-OUT and OUT parameters | |
1902 | ||
1903 | if Ekind (Formal) /= E_In_Parameter then | |
1904 | ||
1905 | -- For type conversions of arrays, apply length/range checks | |
1906 | ||
1907 | if Is_Array_Type (E_Formal) | |
1908 | and then Nkind (Actual) = N_Type_Conversion | |
1909 | then | |
1910 | if Is_Constrained (E_Formal) then | |
1911 | Apply_Length_Check (Expression (Actual), E_Formal); | |
1912 | else | |
1913 | Apply_Range_Check (Expression (Actual), E_Formal); | |
1914 | end if; | |
1915 | end if; | |
1916 | ||
1917 | -- The actual denotes a variable which captures the value of an | |
1918 | -- object for validation purposes. Add a copy-back to reflect any | |
1919 | -- potential changes in value back into the original object. | |
1920 | ||
1921 | -- Var : ... := Object; | |
1922 | -- if not Var'Valid then -- validity check | |
1923 | -- Call (Var); -- modify var | |
1924 | -- Object := Var; -- update Object | |
1925 | ||
1926 | -- This case is given higher priority because the subsequent check | |
1927 | -- for type conversion may add an extra copy of the variable and | |
1928 | -- prevent proper value propagation back in the original object. | |
1929 | ||
1930 | if Is_Validation_Variable_Reference (Actual) then | |
1931 | Add_Validation_Call_By_Copy_Code (Actual); | |
1932 | ||
1933 | -- If argument is a type conversion for a type that is passed by | |
1934 | -- copy, then we must pass the parameter by copy. | |
1935 | ||
1936 | elsif Nkind (Actual) = N_Type_Conversion | |
1937 | and then | |
1938 | (Is_Numeric_Type (E_Formal) | |
1939 | or else Is_Access_Type (E_Formal) | |
1940 | or else Is_Enumeration_Type (E_Formal) | |
1941 | or else Is_Bit_Packed_Array (Etype (Formal)) | |
1942 | or else Is_Bit_Packed_Array (Etype (Expression (Actual))) | |
1943 | ||
1944 | -- Also pass by copy if change of representation | |
1945 | ||
1946 | or else not Same_Representation | |
1947 | (Etype (Formal), | |
1948 | Etype (Expression (Actual)))) | |
1949 | then | |
1950 | Add_Call_By_Copy_Code; | |
1951 | ||
1952 | -- References to components of bit-packed arrays are expanded | |
1953 | -- at this point, rather than at the point of analysis of the | |
1954 | -- actuals, to handle the expansion of the assignment to | |
1955 | -- [in] out parameters. | |
1956 | ||
1957 | elsif Is_Ref_To_Bit_Packed_Array (Actual) then | |
1958 | Add_Simple_Call_By_Copy_Code; | |
1959 | ||
1960 | -- If a non-scalar actual is possibly bit-aligned, we need a copy | |
1961 | -- because the back-end cannot cope with such objects. In other | |
1962 | -- cases where alignment forces a copy, the back-end generates | |
1963 | -- it properly. It should not be generated unconditionally in the | |
1964 | -- front-end because it does not know precisely the alignment | |
1965 | -- requirements of the target, and makes too conservative an | |
1966 | -- estimate, leading to superfluous copies or spurious errors | |
1967 | -- on by-reference parameters. | |
1968 | ||
1969 | elsif Nkind (Actual) = N_Selected_Component | |
1970 | and then | |
1971 | Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual))) | |
1972 | and then not Represented_As_Scalar (Etype (Formal)) | |
1973 | then | |
1974 | Add_Simple_Call_By_Copy_Code; | |
1975 | ||
1976 | -- References to slices of bit-packed arrays are expanded | |
1977 | ||
1978 | elsif Is_Ref_To_Bit_Packed_Slice (Actual) then | |
1979 | Add_Call_By_Copy_Code; | |
1980 | ||
1981 | -- References to possibly unaligned slices of arrays are expanded | |
1982 | ||
1983 | elsif Is_Possibly_Unaligned_Slice (Actual) then | |
1984 | Add_Call_By_Copy_Code; | |
1985 | ||
1986 | -- Deal with access types where the actual subtype and the | |
1987 | -- formal subtype are not the same, requiring a check. | |
1988 | ||
1989 | -- It is necessary to exclude tagged types because of "downward | |
1990 | -- conversion" errors. | |
1991 | ||
1992 | elsif Is_Access_Type (E_Formal) | |
1993 | and then not Same_Type (E_Formal, E_Actual) | |
1994 | and then not Is_Tagged_Type (Designated_Type (E_Formal)) | |
1995 | then | |
1996 | Add_Call_By_Copy_Code; | |
1997 | ||
1998 | -- If the actual is not a scalar and is marked for volatile | |
1999 | -- treatment, whereas the formal is not volatile, then pass | |
2000 | -- by copy unless it is a by-reference type. | |
2001 | ||
2002 | -- Note: we use Is_Volatile here rather than Treat_As_Volatile, | |
2003 | -- because this is the enforcement of a language rule that applies | |
2004 | -- only to "real" volatile variables, not e.g. to the address | |
2005 | -- clause overlay case. | |
2006 | ||
2007 | elsif Is_Entity_Name (Actual) | |
2008 | and then Is_Volatile (Entity (Actual)) | |
2009 | and then not Is_By_Reference_Type (E_Actual) | |
2010 | and then not Is_Scalar_Type (Etype (Entity (Actual))) | |
2011 | and then not Is_Volatile (E_Formal) | |
2012 | then | |
2013 | Add_Call_By_Copy_Code; | |
2014 | ||
2015 | elsif Nkind (Actual) = N_Indexed_Component | |
2016 | and then Is_Entity_Name (Prefix (Actual)) | |
2017 | and then Has_Volatile_Components (Entity (Prefix (Actual))) | |
2018 | then | |
2019 | Add_Call_By_Copy_Code; | |
2020 | ||
2021 | -- Add call-by-copy code for the case of scalar out parameters | |
2022 | -- when it is not known at compile time that the subtype of the | |
2023 | -- formal is a subrange of the subtype of the actual (or vice | |
2024 | -- versa for in out parameters), in order to get range checks | |
2025 | -- on such actuals. (Maybe this case should be handled earlier | |
2026 | -- in the if statement???) | |
2027 | ||
2028 | elsif Is_Scalar_Type (E_Formal) | |
2029 | and then | |
2030 | (not In_Subrange_Of (E_Formal, E_Actual) | |
2031 | or else | |
2032 | (Ekind (Formal) = E_In_Out_Parameter | |
2033 | and then not In_Subrange_Of (E_Actual, E_Formal))) | |
2034 | then | |
2035 | -- Perhaps the setting back to False should be done within | |
2036 | -- Add_Call_By_Copy_Code, since it could get set on other | |
2037 | -- cases occurring above??? | |
2038 | ||
2039 | if Do_Range_Check (Actual) then | |
2040 | Set_Do_Range_Check (Actual, False); | |
2041 | end if; | |
2042 | ||
2043 | Add_Call_By_Copy_Code; | |
2044 | end if; | |
2045 | ||
2046 | -- RM 3.2.4 (23/3): A predicate is checked on in-out and out | |
2047 | -- by-reference parameters on exit from the call. If the actual | |
2048 | -- is a derived type and the operation is inherited, the body | |
2049 | -- of the operation will not contain a call to the predicate | |
2050 | -- function, so it must be done explicitly after the call. Ditto | |
2051 | -- if the actual is an entity of a predicated subtype. | |
2052 | ||
2053 | -- The rule refers to by-reference types, but a check is needed | |
2054 | -- for by-copy types as well. That check is subsumed by the rule | |
2055 | -- for subtype conversion on assignment, but we can generate the | |
2056 | -- required check now. | |
2057 | ||
2058 | -- Note also that Subp may be either a subprogram entity for | |
2059 | -- direct calls, or a type entity for indirect calls, which must | |
2060 | -- be handled separately because the name does not denote an | |
2061 | -- overloadable entity. | |
2062 | ||
2063 | By_Ref_Predicate_Check : declare | |
2064 | Aund : constant Entity_Id := Underlying_Type (E_Actual); | |
2065 | Atyp : Entity_Id; | |
2066 | ||
2067 | function Is_Public_Subp return Boolean; | |
2068 | -- Check whether the subprogram being called is a visible | |
2069 | -- operation of the type of the actual. Used to determine | |
2070 | -- whether an invariant check must be generated on the | |
2071 | -- caller side. | |
2072 | ||
2073 | --------------------- | |
2074 | -- Is_Public_Subp -- | |
2075 | --------------------- | |
2076 | ||
2077 | function Is_Public_Subp return Boolean is | |
2078 | Pack : constant Entity_Id := Scope (Subp); | |
2079 | Subp_Decl : Node_Id; | |
2080 | ||
2081 | begin | |
2082 | if not Is_Subprogram (Subp) then | |
2083 | return False; | |
2084 | ||
2085 | -- The operation may be inherited, or a primitive of the | |
2086 | -- root type. | |
2087 | ||
2088 | elsif | |
2089 | Nkind_In (Parent (Subp), N_Private_Extension_Declaration, | |
2090 | N_Full_Type_Declaration) | |
2091 | then | |
2092 | Subp_Decl := Parent (Subp); | |
2093 | ||
2094 | else | |
2095 | Subp_Decl := Unit_Declaration_Node (Subp); | |
2096 | end if; | |
2097 | ||
2098 | return Ekind (Pack) = E_Package | |
2099 | and then | |
2100 | List_Containing (Subp_Decl) = | |
2101 | Visible_Declarations | |
2102 | (Specification (Unit_Declaration_Node (Pack))); | |
2103 | end Is_Public_Subp; | |
2104 | ||
2105 | -- Start of processing for By_Ref_Predicate_Check | |
2106 | ||
2107 | begin | |
2108 | if No (Aund) then | |
2109 | Atyp := E_Actual; | |
2110 | else | |
2111 | Atyp := Aund; | |
2112 | end if; | |
2113 | ||
2114 | if Has_Predicates (Atyp) | |
2115 | and then Present (Predicate_Function (Atyp)) | |
2116 | ||
2117 | -- Skip predicate checks for special cases | |
2118 | ||
2119 | and then Predicate_Tests_On_Arguments (Subp) | |
2120 | then | |
2121 | Append_To (Post_Call, | |
2122 | Make_Predicate_Check (Atyp, Actual)); | |
2123 | end if; | |
2124 | ||
2125 | -- We generated caller-side invariant checks in two cases: | |
2126 | ||
2127 | -- a) when calling an inherited operation, where there is an | |
2128 | -- implicit view conversion of the actual to the parent type. | |
2129 | ||
2130 | -- b) When the conversion is explicit | |
2131 | ||
2132 | -- We treat these cases separately because the required | |
2133 | -- conversion for a) is added later when expanding the call. | |
2134 | ||
2135 | if Has_Invariants (Etype (Actual)) | |
2136 | and then | |
2137 | Nkind (Parent (Subp)) = N_Private_Extension_Declaration | |
2138 | then | |
2139 | if Comes_From_Source (N) and then Is_Public_Subp then | |
2140 | Append_To (Post_Call, Make_Invariant_Call (Actual)); | |
2141 | end if; | |
2142 | ||
2143 | elsif Nkind (Actual) = N_Type_Conversion | |
2144 | and then Has_Invariants (Etype (Expression (Actual))) | |
2145 | then | |
2146 | if Comes_From_Source (N) and then Is_Public_Subp then | |
2147 | Append_To (Post_Call, | |
2148 | Make_Invariant_Call (Expression (Actual))); | |
2149 | end if; | |
2150 | end if; | |
2151 | end By_Ref_Predicate_Check; | |
2152 | ||
2153 | -- Processing for IN parameters | |
2154 | ||
2155 | else | |
2156 | -- For IN parameters in the bit-packed array case, we expand an | |
2157 | -- indexed component (the circuit in Exp_Ch4 deliberately left | |
2158 | -- indexed components appearing as actuals untouched, so that | |
2159 | -- the special processing above for the OUT and IN OUT cases | |
2160 | -- could be performed. We could make the test in Exp_Ch4 more | |
2161 | -- complex and have it detect the parameter mode, but it is | |
2162 | -- easier simply to handle all cases here.) | |
2163 | ||
2164 | if Nkind (Actual) = N_Indexed_Component | |
2165 | and then Is_Bit_Packed_Array (Etype (Prefix (Actual))) | |
2166 | then | |
2167 | Reset_Packed_Prefix; | |
2168 | Expand_Packed_Element_Reference (Actual); | |
2169 | ||
2170 | -- If we have a reference to a bit-packed array, we copy it, since | |
2171 | -- the actual must be byte aligned. | |
2172 | ||
2173 | -- Is this really necessary in all cases??? | |
2174 | ||
2175 | elsif Is_Ref_To_Bit_Packed_Array (Actual) then | |
2176 | Add_Simple_Call_By_Copy_Code; | |
2177 | ||
2178 | -- If a non-scalar actual is possibly unaligned, we need a copy | |
2179 | ||
2180 | elsif Is_Possibly_Unaligned_Object (Actual) | |
2181 | and then not Represented_As_Scalar (Etype (Formal)) | |
2182 | then | |
2183 | Add_Simple_Call_By_Copy_Code; | |
2184 | ||
2185 | -- Similarly, we have to expand slices of packed arrays here | |
2186 | -- because the result must be byte aligned. | |
2187 | ||
2188 | elsif Is_Ref_To_Bit_Packed_Slice (Actual) then | |
2189 | Add_Call_By_Copy_Code; | |
2190 | ||
2191 | -- Only processing remaining is to pass by copy if this is a | |
2192 | -- reference to a possibly unaligned slice, since the caller | |
2193 | -- expects an appropriately aligned argument. | |
2194 | ||
2195 | elsif Is_Possibly_Unaligned_Slice (Actual) then | |
2196 | Add_Call_By_Copy_Code; | |
2197 | ||
2198 | -- An unusual case: a current instance of an enclosing task can be | |
2199 | -- an actual, and must be replaced by a reference to self. | |
2200 | ||
2201 | elsif Is_Entity_Name (Actual) | |
2202 | and then Is_Task_Type (Entity (Actual)) | |
2203 | then | |
2204 | if In_Open_Scopes (Entity (Actual)) then | |
2205 | Rewrite (Actual, | |
2206 | (Make_Function_Call (Loc, | |
2207 | Name => New_Occurrence_Of (RTE (RE_Self), Loc)))); | |
2208 | Analyze (Actual); | |
2209 | ||
2210 | -- A task type cannot otherwise appear as an actual | |
2211 | ||
2212 | else | |
2213 | raise Program_Error; | |
2214 | end if; | |
2215 | end if; | |
2216 | end if; | |
2217 | ||
2218 | Next_Formal (Formal); | |
2219 | Next_Actual (Actual); | |
2220 | end loop; | |
2221 | end Expand_Actuals; | |
2222 | ||
2223 | ----------------- | |
2224 | -- Expand_Call -- | |
2225 | ----------------- | |
2226 | ||
2227 | procedure Expand_Call (N : Node_Id) is | |
2228 | Post_Call : List_Id; | |
2229 | begin | |
2230 | Expand_Call_Helper (N, Post_Call); | |
2231 | Insert_Post_Call_Actions (N, Post_Call); | |
2232 | end Expand_Call; | |
2233 | ||
2234 | ------------------------ | |
2235 | -- Expand_Call_Helper -- | |
2236 | ------------------------ | |
2237 | ||
2238 | -- This procedure handles expansion of function calls and procedure call | |
2239 | -- statements (i.e. it serves as the body for Expand_N_Function_Call and | |
2240 | -- Expand_N_Procedure_Call_Statement). Processing for calls includes: | |
2241 | ||
2242 | -- Replace call to Raise_Exception by Raise_Exception_Always if possible | |
2243 | -- Provide values of actuals for all formals in Extra_Formals list | |
2244 | -- Replace "call" to enumeration literal function by literal itself | |
2245 | -- Rewrite call to predefined operator as operator | |
2246 | -- Replace actuals to in-out parameters that are numeric conversions, | |
2247 | -- with explicit assignment to temporaries before and after the call. | |
2248 | ||
2249 | -- Note that the list of actuals has been filled with default expressions | |
2250 | -- during semantic analysis of the call. Only the extra actuals required | |
2251 | -- for the 'Constrained attribute and for accessibility checks are added | |
2252 | -- at this point. | |
2253 | ||
2254 | procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id) is | |
2255 | Loc : constant Source_Ptr := Sloc (N); | |
2256 | Call_Node : Node_Id := N; | |
2257 | Extra_Actuals : List_Id := No_List; | |
2258 | Prev : Node_Id := Empty; | |
2259 | ||
2260 | procedure Add_Actual_Parameter (Insert_Param : Node_Id); | |
2261 | -- Adds one entry to the end of the actual parameter list. Used for | |
2262 | -- default parameters and for extra actuals (for Extra_Formals). The | |
2263 | -- argument is an N_Parameter_Association node. | |
2264 | ||
2265 | procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id); | |
2266 | -- Adds an extra actual to the list of extra actuals. Expr is the | |
2267 | -- expression for the value of the actual, EF is the entity for the | |
2268 | -- extra formal. | |
2269 | ||
2270 | procedure Add_View_Conversion_Invariants | |
2271 | (Formal : Entity_Id; | |
2272 | Actual : Node_Id); | |
2273 | -- Adds invariant checks for every intermediate type between the range | |
2274 | -- of a view converted argument to its ancestor (from parent to child). | |
2275 | ||
2276 | function Inherited_From_Formal (S : Entity_Id) return Entity_Id; | |
2277 | -- Within an instance, a type derived from an untagged formal derived | |
2278 | -- type inherits from the original parent, not from the actual. The | |
2279 | -- current derivation mechanism has the derived type inherit from the | |
2280 | -- actual, which is only correct outside of the instance. If the | |
2281 | -- subprogram is inherited, we test for this particular case through a | |
2282 | -- convoluted tree traversal before setting the proper subprogram to be | |
2283 | -- called. | |
2284 | ||
2285 | function In_Unfrozen_Instance (E : Entity_Id) return Boolean; | |
2286 | -- Return true if E comes from an instance that is not yet frozen | |
2287 | ||
2288 | function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean; | |
2289 | -- Determine if Subp denotes a non-dispatching call to a Deep routine | |
2290 | ||
2291 | function New_Value (From : Node_Id) return Node_Id; | |
2292 | -- From is the original Expression. New_Value is equivalent to a call | |
2293 | -- to Duplicate_Subexpr with an explicit dereference when From is an | |
2294 | -- access parameter. | |
2295 | ||
2296 | -------------------------- | |
2297 | -- Add_Actual_Parameter -- | |
2298 | -------------------------- | |
2299 | ||
2300 | procedure Add_Actual_Parameter (Insert_Param : Node_Id) is | |
2301 | Actual_Expr : constant Node_Id := | |
2302 | Explicit_Actual_Parameter (Insert_Param); | |
2303 | ||
2304 | begin | |
2305 | -- Case of insertion is first named actual | |
2306 | ||
2307 | if No (Prev) or else | |
2308 | Nkind (Parent (Prev)) /= N_Parameter_Association | |
2309 | then | |
2310 | Set_Next_Named_Actual | |
2311 | (Insert_Param, First_Named_Actual (Call_Node)); | |
2312 | Set_First_Named_Actual (Call_Node, Actual_Expr); | |
2313 | ||
2314 | if No (Prev) then | |
2315 | if No (Parameter_Associations (Call_Node)) then | |
2316 | Set_Parameter_Associations (Call_Node, New_List); | |
2317 | end if; | |
2318 | ||
2319 | Append (Insert_Param, Parameter_Associations (Call_Node)); | |
2320 | ||
2321 | else | |
2322 | Insert_After (Prev, Insert_Param); | |
2323 | end if; | |
2324 | ||
2325 | -- Case of insertion is not first named actual | |
2326 | ||
2327 | else | |
2328 | Set_Next_Named_Actual | |
2329 | (Insert_Param, Next_Named_Actual (Parent (Prev))); | |
2330 | Set_Next_Named_Actual (Parent (Prev), Actual_Expr); | |
2331 | Append (Insert_Param, Parameter_Associations (Call_Node)); | |
2332 | end if; | |
2333 | ||
2334 | Prev := Actual_Expr; | |
2335 | end Add_Actual_Parameter; | |
2336 | ||
2337 | ---------------------- | |
2338 | -- Add_Extra_Actual -- | |
2339 | ---------------------- | |
2340 | ||
2341 | procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is | |
2342 | Loc : constant Source_Ptr := Sloc (Expr); | |
2343 | ||
2344 | begin | |
2345 | if Extra_Actuals = No_List then | |
2346 | Extra_Actuals := New_List; | |
2347 | Set_Parent (Extra_Actuals, Call_Node); | |
2348 | end if; | |
2349 | ||
2350 | Append_To (Extra_Actuals, | |
2351 | Make_Parameter_Association (Loc, | |
2352 | Selector_Name => New_Occurrence_Of (EF, Loc), | |
2353 | Explicit_Actual_Parameter => Expr)); | |
2354 | ||
2355 | Analyze_And_Resolve (Expr, Etype (EF)); | |
2356 | ||
2357 | if Nkind (Call_Node) = N_Function_Call then | |
2358 | Set_Is_Accessibility_Actual (Parent (Expr)); | |
2359 | end if; | |
2360 | end Add_Extra_Actual; | |
2361 | ||
2362 | ------------------------------------ | |
2363 | -- Add_View_Conversion_Invariants -- | |
2364 | ------------------------------------ | |
2365 | ||
2366 | procedure Add_View_Conversion_Invariants | |
2367 | (Formal : Entity_Id; | |
2368 | Actual : Node_Id) | |
2369 | is | |
2370 | Arg : Entity_Id; | |
2371 | Curr_Typ : Entity_Id; | |
2372 | Inv_Checks : List_Id; | |
2373 | Par_Typ : Entity_Id; | |
2374 | ||
2375 | begin | |
2376 | Inv_Checks := No_List; | |
2377 | ||
2378 | -- Extract the argument from a potentially nested set of view | |
2379 | -- conversions. | |
2380 | ||
2381 | Arg := Actual; | |
2382 | while Nkind (Arg) = N_Type_Conversion loop | |
2383 | Arg := Expression (Arg); | |
2384 | end loop; | |
2385 | ||
2386 | -- Move up the derivation chain starting with the type of the formal | |
2387 | -- parameter down to the type of the actual object. | |
2388 | ||
2389 | Curr_Typ := Empty; | |
2390 | Par_Typ := Etype (Arg); | |
2391 | while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop | |
2392 | Curr_Typ := Par_Typ; | |
2393 | ||
2394 | if Has_Invariants (Curr_Typ) | |
2395 | and then Present (Invariant_Procedure (Curr_Typ)) | |
2396 | then | |
2397 | -- Verify the invariate of the current type. Generate: | |
2398 | ||
2399 | -- <Curr_Typ>Invariant (Curr_Typ (Arg)); | |
2400 | ||
2401 | Prepend_New_To (Inv_Checks, | |
2402 | Make_Procedure_Call_Statement (Loc, | |
2403 | Name => | |
2404 | New_Occurrence_Of | |
2405 | (Invariant_Procedure (Curr_Typ), Loc), | |
2406 | Parameter_Associations => New_List ( | |
2407 | Make_Type_Conversion (Loc, | |
2408 | Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc), | |
2409 | Expression => New_Copy_Tree (Arg))))); | |
2410 | end if; | |
2411 | ||
2412 | Par_Typ := Base_Type (Etype (Curr_Typ)); | |
2413 | end loop; | |
2414 | ||
2415 | if not Is_Empty_List (Inv_Checks) then | |
2416 | Insert_Actions_After (N, Inv_Checks); | |
2417 | end if; | |
2418 | end Add_View_Conversion_Invariants; | |
2419 | ||
2420 | --------------------------- | |
2421 | -- Inherited_From_Formal -- | |
2422 | --------------------------- | |
2423 | ||
2424 | function Inherited_From_Formal (S : Entity_Id) return Entity_Id is | |
2425 | Par : Entity_Id; | |
2426 | Gen_Par : Entity_Id; | |
2427 | Gen_Prim : Elist_Id; | |
2428 | Elmt : Elmt_Id; | |
2429 | Indic : Node_Id; | |
2430 | ||
2431 | begin | |
2432 | -- If the operation is inherited, it is attached to the corresponding | |
2433 | -- type derivation. If the parent in the derivation is a generic | |
2434 | -- actual, it is a subtype of the actual, and we have to recover the | |
2435 | -- original derived type declaration to find the proper parent. | |
2436 | ||
2437 | if Nkind (Parent (S)) /= N_Full_Type_Declaration | |
2438 | or else not Is_Derived_Type (Defining_Identifier (Parent (S))) | |
2439 | or else Nkind (Type_Definition (Original_Node (Parent (S)))) /= | |
2440 | N_Derived_Type_Definition | |
2441 | or else not In_Instance | |
2442 | then | |
2443 | return Empty; | |
2444 | ||
2445 | else | |
2446 | Indic := | |
2447 | Subtype_Indication | |
2448 | (Type_Definition (Original_Node (Parent (S)))); | |
2449 | ||
2450 | if Nkind (Indic) = N_Subtype_Indication then | |
2451 | Par := Entity (Subtype_Mark (Indic)); | |
2452 | else | |
2453 | Par := Entity (Indic); | |
2454 | end if; | |
2455 | end if; | |
2456 | ||
2457 | if not Is_Generic_Actual_Type (Par) | |
2458 | or else Is_Tagged_Type (Par) | |
2459 | or else Nkind (Parent (Par)) /= N_Subtype_Declaration | |
2460 | or else not In_Open_Scopes (Scope (Par)) | |
2461 | then | |
2462 | return Empty; | |
2463 | else | |
2464 | Gen_Par := Generic_Parent_Type (Parent (Par)); | |
2465 | end if; | |
2466 | ||
2467 | -- If the actual has no generic parent type, the formal is not | |
2468 | -- a formal derived type, so nothing to inherit. | |
2469 | ||
2470 | if No (Gen_Par) then | |
2471 | return Empty; | |
2472 | end if; | |
2473 | ||
2474 | -- If the generic parent type is still the generic type, this is a | |
2475 | -- private formal, not a derived formal, and there are no operations | |
2476 | -- inherited from the formal. | |
2477 | ||
2478 | if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then | |
2479 | return Empty; | |
2480 | end if; | |
2481 | ||
2482 | Gen_Prim := Collect_Primitive_Operations (Gen_Par); | |
2483 | ||
2484 | Elmt := First_Elmt (Gen_Prim); | |
2485 | while Present (Elmt) loop | |
2486 | if Chars (Node (Elmt)) = Chars (S) then | |
2487 | declare | |
2488 | F1 : Entity_Id; | |
2489 | F2 : Entity_Id; | |
2490 | ||
2491 | begin | |
2492 | F1 := First_Formal (S); | |
2493 | F2 := First_Formal (Node (Elmt)); | |
2494 | while Present (F1) | |
2495 | and then Present (F2) | |
2496 | loop | |
2497 | if Etype (F1) = Etype (F2) | |
2498 | or else Etype (F2) = Gen_Par | |
2499 | then | |
2500 | Next_Formal (F1); | |
2501 | Next_Formal (F2); | |
2502 | else | |
2503 | Next_Elmt (Elmt); | |
2504 | exit; -- not the right subprogram | |
2505 | end if; | |
2506 | ||
2507 | return Node (Elmt); | |
2508 | end loop; | |
2509 | end; | |
2510 | ||
2511 | else | |
2512 | Next_Elmt (Elmt); | |
2513 | end if; | |
2514 | end loop; | |
2515 | ||
2516 | raise Program_Error; | |
2517 | end Inherited_From_Formal; | |
2518 | ||
2519 | -------------------------- | |
2520 | -- In_Unfrozen_Instance -- | |
2521 | -------------------------- | |
2522 | ||
2523 | function In_Unfrozen_Instance (E : Entity_Id) return Boolean is | |
2524 | S : Entity_Id; | |
2525 | ||
2526 | begin | |
2527 | S := E; | |
2528 | while Present (S) and then S /= Standard_Standard loop | |
2529 | if Is_Generic_Instance (S) | |
2530 | and then Present (Freeze_Node (S)) | |
2531 | and then not Analyzed (Freeze_Node (S)) | |
2532 | then | |
2533 | return True; | |
2534 | end if; | |
2535 | ||
2536 | S := Scope (S); | |
2537 | end loop; | |
2538 | ||
2539 | return False; | |
2540 | end In_Unfrozen_Instance; | |
2541 | ||
2542 | ------------------------- | |
2543 | -- Is_Direct_Deep_Call -- | |
2544 | ------------------------- | |
2545 | ||
2546 | function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is | |
2547 | begin | |
2548 | if Is_TSS (Subp, TSS_Deep_Adjust) | |
2549 | or else Is_TSS (Subp, TSS_Deep_Finalize) | |
2550 | or else Is_TSS (Subp, TSS_Deep_Initialize) | |
2551 | then | |
2552 | declare | |
2553 | Actual : Node_Id; | |
2554 | Formal : Node_Id; | |
2555 | ||
2556 | begin | |
2557 | Actual := First (Parameter_Associations (N)); | |
2558 | Formal := First_Formal (Subp); | |
2559 | while Present (Actual) | |
2560 | and then Present (Formal) | |
2561 | loop | |
2562 | if Nkind (Actual) = N_Identifier | |
2563 | and then Is_Controlling_Actual (Actual) | |
2564 | and then Etype (Actual) = Etype (Formal) | |
2565 | then | |
2566 | return True; | |
2567 | end if; | |
2568 | ||
2569 | Next (Actual); | |
2570 | Next_Formal (Formal); | |
2571 | end loop; | |
2572 | end; | |
2573 | end if; | |
2574 | ||
2575 | return False; | |
2576 | end Is_Direct_Deep_Call; | |
2577 | ||
2578 | --------------- | |
2579 | -- New_Value -- | |
2580 | --------------- | |
2581 | ||
2582 | function New_Value (From : Node_Id) return Node_Id is | |
2583 | Res : constant Node_Id := Duplicate_Subexpr (From); | |
2584 | begin | |
2585 | if Is_Access_Type (Etype (From)) then | |
2586 | return Make_Explicit_Dereference (Sloc (From), Prefix => Res); | |
2587 | else | |
2588 | return Res; | |
2589 | end if; | |
2590 | end New_Value; | |
2591 | ||
2592 | -- Local variables | |
2593 | ||
2594 | Remote : constant Boolean := Is_Remote_Call (Call_Node); | |
2595 | Actual : Node_Id; | |
2596 | Formal : Entity_Id; | |
2597 | Orig_Subp : Entity_Id := Empty; | |
2598 | Param_Count : Natural := 0; | |
2599 | Parent_Formal : Entity_Id; | |
2600 | Parent_Subp : Entity_Id; | |
2601 | Scop : Entity_Id; | |
2602 | Subp : Entity_Id; | |
2603 | ||
2604 | Prev_Orig : Node_Id; | |
2605 | -- Original node for an actual, which may have been rewritten. If the | |
2606 | -- actual is a function call that has been transformed from a selected | |
2607 | -- component, the original node is unanalyzed. Otherwise, it carries | |
2608 | -- semantic information used to generate additional actuals. | |
2609 | ||
2610 | CW_Interface_Formals_Present : Boolean := False; | |
2611 | ||
2612 | -- Start of processing for Expand_Call_Helper | |
2613 | ||
2614 | begin | |
2615 | Post_Call := New_List; | |
2616 | ||
2617 | -- Expand the function or procedure call if the first actual has a | |
2618 | -- declared dimension aspect, and the subprogram is declared in one | |
2619 | -- of the dimension I/O packages. | |
2620 | ||
2621 | if Ada_Version >= Ada_2012 | |
2622 | and then | |
2623 | Nkind_In (Call_Node, N_Procedure_Call_Statement, N_Function_Call) | |
2624 | and then Present (Parameter_Associations (Call_Node)) | |
2625 | then | |
2626 | Expand_Put_Call_With_Symbol (Call_Node); | |
2627 | end if; | |
2628 | ||
2629 | -- Ignore if previous error | |
2630 | ||
2631 | if Nkind (Call_Node) in N_Has_Etype | |
2632 | and then Etype (Call_Node) = Any_Type | |
2633 | then | |
2634 | return; | |
2635 | end if; | |
2636 | ||
2637 | -- Call using access to subprogram with explicit dereference | |
2638 | ||
2639 | if Nkind (Name (Call_Node)) = N_Explicit_Dereference then | |
2640 | Subp := Etype (Name (Call_Node)); | |
2641 | Parent_Subp := Empty; | |
2642 | ||
2643 | -- Case of call to simple entry, where the Name is a selected component | |
2644 | -- whose prefix is the task, and whose selector name is the entry name | |
2645 | ||
2646 | elsif Nkind (Name (Call_Node)) = N_Selected_Component then | |
2647 | Subp := Entity (Selector_Name (Name (Call_Node))); | |
2648 | Parent_Subp := Empty; | |
2649 | ||
2650 | -- Case of call to member of entry family, where Name is an indexed | |
2651 | -- component, with the prefix being a selected component giving the | |
2652 | -- task and entry family name, and the index being the entry index. | |
2653 | ||
2654 | elsif Nkind (Name (Call_Node)) = N_Indexed_Component then | |
2655 | Subp := Entity (Selector_Name (Prefix (Name (Call_Node)))); | |
2656 | Parent_Subp := Empty; | |
2657 | ||
2658 | -- Normal case | |
2659 | ||
2660 | else | |
2661 | Subp := Entity (Name (Call_Node)); | |
2662 | Parent_Subp := Alias (Subp); | |
2663 | ||
2664 | -- Replace call to Raise_Exception by call to Raise_Exception_Always | |
2665 | -- if we can tell that the first parameter cannot possibly be null. | |
2666 | -- This improves efficiency by avoiding a run-time test. | |
2667 | ||
2668 | -- We do not do this if Raise_Exception_Always does not exist, which | |
2669 | -- can happen in configurable run time profiles which provide only a | |
2670 | -- Raise_Exception. | |
2671 | ||
2672 | if Is_RTE (Subp, RE_Raise_Exception) | |
2673 | and then RTE_Available (RE_Raise_Exception_Always) | |
2674 | then | |
2675 | declare | |
2676 | FA : constant Node_Id := | |
2677 | Original_Node (First_Actual (Call_Node)); | |
2678 | ||
2679 | begin | |
2680 | -- The case we catch is where the first argument is obtained | |
2681 | -- using the Identity attribute (which must always be | |
2682 | -- non-null). | |
2683 | ||
2684 | if Nkind (FA) = N_Attribute_Reference | |
2685 | and then Attribute_Name (FA) = Name_Identity | |
2686 | then | |
2687 | Subp := RTE (RE_Raise_Exception_Always); | |
2688 | Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc)); | |
2689 | end if; | |
2690 | end; | |
2691 | end if; | |
2692 | ||
2693 | if Ekind (Subp) = E_Entry then | |
2694 | Parent_Subp := Empty; | |
2695 | end if; | |
2696 | end if; | |
2697 | ||
2698 | -- Ada 2005 (AI-345): We have a procedure call as a triggering | |
2699 | -- alternative in an asynchronous select or as an entry call in | |
2700 | -- a conditional or timed select. Check whether the procedure call | |
2701 | -- is a renaming of an entry and rewrite it as an entry call. | |
2702 | ||
2703 | if Ada_Version >= Ada_2005 | |
2704 | and then Nkind (Call_Node) = N_Procedure_Call_Statement | |
2705 | and then | |
2706 | ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative | |
2707 | and then Triggering_Statement (Parent (Call_Node)) = Call_Node) | |
2708 | or else | |
2709 | (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative | |
2710 | and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node)) | |
2711 | then | |
2712 | declare | |
2713 | Ren_Decl : Node_Id; | |
2714 | Ren_Root : Entity_Id := Subp; | |
2715 | ||
2716 | begin | |
2717 | -- This may be a chain of renamings, find the root | |
2718 | ||
2719 | if Present (Alias (Ren_Root)) then | |
2720 | Ren_Root := Alias (Ren_Root); | |
2721 | end if; | |
2722 | ||
2723 | if Present (Original_Node (Parent (Parent (Ren_Root)))) then | |
2724 | Ren_Decl := Original_Node (Parent (Parent (Ren_Root))); | |
2725 | ||
2726 | if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then | |
2727 | Rewrite (Call_Node, | |
2728 | Make_Entry_Call_Statement (Loc, | |
2729 | Name => | |
2730 | New_Copy_Tree (Name (Ren_Decl)), | |
2731 | Parameter_Associations => | |
2732 | New_Copy_List_Tree | |
2733 | (Parameter_Associations (Call_Node)))); | |
2734 | ||
2735 | return; | |
2736 | end if; | |
2737 | end if; | |
2738 | end; | |
2739 | end if; | |
2740 | ||
2741 | -- When generating C code, transform a function call that returns a | |
2742 | -- constrained array type into procedure form. | |
2743 | ||
2744 | if Modify_Tree_For_C | |
2745 | and then Nkind (Call_Node) = N_Function_Call | |
2746 | and then Is_Entity_Name (Name (Call_Node)) | |
2747 | and then Rewritten_For_C (Ultimate_Alias (Entity (Name (Call_Node)))) | |
2748 | then | |
2749 | -- For internally generated calls ensure that they reference the | |
2750 | -- entity of the spec of the called function (needed since the | |
2751 | -- expander may generate calls using the entity of their body). | |
2752 | -- See for example Expand_Boolean_Operator(). | |
2753 | ||
2754 | if not (Comes_From_Source (Call_Node)) | |
2755 | and then Nkind (Unit_Declaration_Node | |
2756 | (Ultimate_Alias (Entity (Name (Call_Node))))) = | |
2757 | N_Subprogram_Body | |
2758 | then | |
2759 | Set_Entity (Name (Call_Node), | |
2760 | Corresponding_Function | |
2761 | (Corresponding_Procedure | |
2762 | (Ultimate_Alias (Entity (Name (Call_Node)))))); | |
2763 | end if; | |
2764 | ||
2765 | Rewrite_Function_Call_For_C (Call_Node); | |
2766 | return; | |
2767 | end if; | |
2768 | ||
2769 | -- First step, compute extra actuals, corresponding to any Extra_Formals | |
2770 | -- present. Note that we do not access Extra_Formals directly, instead | |
2771 | -- we simply note the presence of the extra formals as we process the | |
2772 | -- regular formals collecting corresponding actuals in Extra_Actuals. | |
2773 | ||
2774 | -- We also generate any required range checks for actuals for in formals | |
2775 | -- as we go through the loop, since this is a convenient place to do it. | |
2776 | -- (Though it seems that this would be better done in Expand_Actuals???) | |
2777 | ||
2778 | -- Special case: Thunks must not compute the extra actuals; they must | |
2779 | -- just propagate to the target primitive their extra actuals. | |
2780 | ||
2781 | if Is_Thunk (Current_Scope) | |
2782 | and then Thunk_Entity (Current_Scope) = Subp | |
2783 | and then Present (Extra_Formals (Subp)) | |
2784 | then | |
2785 | pragma Assert (Present (Extra_Formals (Current_Scope))); | |
2786 | ||
2787 | declare | |
2788 | Target_Formal : Entity_Id; | |
2789 | Thunk_Formal : Entity_Id; | |
2790 | ||
2791 | begin | |
2792 | Target_Formal := Extra_Formals (Subp); | |
2793 | Thunk_Formal := Extra_Formals (Current_Scope); | |
2794 | while Present (Target_Formal) loop | |
2795 | Add_Extra_Actual | |
2796 | (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal); | |
2797 | ||
2798 | Target_Formal := Extra_Formal (Target_Formal); | |
2799 | Thunk_Formal := Extra_Formal (Thunk_Formal); | |
2800 | end loop; | |
2801 | ||
2802 | while Is_Non_Empty_List (Extra_Actuals) loop | |
2803 | Add_Actual_Parameter (Remove_Head (Extra_Actuals)); | |
2804 | end loop; | |
2805 | ||
2806 | Expand_Actuals (Call_Node, Subp, Post_Call); | |
2807 | pragma Assert (Is_Empty_List (Post_Call)); | |
2808 | return; | |
2809 | end; | |
2810 | end if; | |
2811 | ||
2812 | Formal := First_Formal (Subp); | |
2813 | Actual := First_Actual (Call_Node); | |
2814 | Param_Count := 1; | |
2815 | while Present (Formal) loop | |
2816 | ||
2817 | -- Generate range check if required | |
2818 | ||
2819 | if Do_Range_Check (Actual) | |
2820 | and then Ekind (Formal) = E_In_Parameter | |
2821 | then | |
2822 | Generate_Range_Check | |
2823 | (Actual, Etype (Formal), CE_Range_Check_Failed); | |
2824 | end if; | |
2825 | ||
2826 | -- Prepare to examine current entry | |
2827 | ||
2828 | Prev := Actual; | |
2829 | Prev_Orig := Original_Node (Prev); | |
2830 | ||
2831 | -- Ada 2005 (AI-251): Check if any formal is a class-wide interface | |
2832 | -- to expand it in a further round. | |
2833 | ||
2834 | CW_Interface_Formals_Present := | |
2835 | CW_Interface_Formals_Present | |
2836 | or else | |
2837 | (Is_Class_Wide_Type (Etype (Formal)) | |
2838 | and then Is_Interface (Etype (Etype (Formal)))) | |
2839 | or else | |
2840 | (Ekind (Etype (Formal)) = E_Anonymous_Access_Type | |
2841 | and then Is_Class_Wide_Type (Directly_Designated_Type | |
2842 | (Etype (Etype (Formal)))) | |
2843 | and then Is_Interface (Directly_Designated_Type | |
2844 | (Etype (Etype (Formal))))); | |
2845 | ||
2846 | -- Create possible extra actual for constrained case. Usually, the | |
2847 | -- extra actual is of the form actual'constrained, but since this | |
2848 | -- attribute is only available for unconstrained records, TRUE is | |
2849 | -- expanded if the type of the formal happens to be constrained (for | |
2850 | -- instance when this procedure is inherited from an unconstrained | |
2851 | -- record to a constrained one) or if the actual has no discriminant | |
2852 | -- (its type is constrained). An exception to this is the case of a | |
2853 | -- private type without discriminants. In this case we pass FALSE | |
2854 | -- because the object has underlying discriminants with defaults. | |
2855 | ||
2856 | if Present (Extra_Constrained (Formal)) then | |
2857 | if Ekind (Etype (Prev)) in Private_Kind | |
2858 | and then not Has_Discriminants (Base_Type (Etype (Prev))) | |
2859 | then | |
2860 | Add_Extra_Actual | |
2861 | (New_Occurrence_Of (Standard_False, Loc), | |
2862 | Extra_Constrained (Formal)); | |
2863 | ||
2864 | elsif Is_Constrained (Etype (Formal)) | |
2865 | or else not Has_Discriminants (Etype (Prev)) | |
2866 | then | |
2867 | Add_Extra_Actual | |
2868 | (New_Occurrence_Of (Standard_True, Loc), | |
2869 | Extra_Constrained (Formal)); | |
2870 | ||
2871 | -- Do not produce extra actuals for Unchecked_Union parameters. | |
2872 | -- Jump directly to the end of the loop. | |
2873 | ||
2874 | elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then | |
2875 | goto Skip_Extra_Actual_Generation; | |
2876 | ||
2877 | else | |
2878 | -- If the actual is a type conversion, then the constrained | |
2879 | -- test applies to the actual, not the target type. | |
2880 | ||
2881 | declare | |
2882 | Act_Prev : Node_Id; | |
2883 | ||
2884 | begin | |
2885 | -- Test for unchecked conversions as well, which can occur | |
2886 | -- as out parameter actuals on calls to stream procedures. | |
2887 | ||
2888 | Act_Prev := Prev; | |
2889 | while Nkind_In (Act_Prev, N_Type_Conversion, | |
2890 | N_Unchecked_Type_Conversion) | |
2891 | loop | |
2892 | Act_Prev := Expression (Act_Prev); | |
2893 | end loop; | |
2894 | ||
2895 | -- If the expression is a conversion of a dereference, this | |
2896 | -- is internally generated code that manipulates addresses, | |
2897 | -- e.g. when building interface tables. No check should | |
2898 | -- occur in this case, and the discriminated object is not | |
2899 | -- directly a hand. | |
2900 | ||
2901 | if not Comes_From_Source (Actual) | |
2902 | and then Nkind (Actual) = N_Unchecked_Type_Conversion | |
2903 | and then Nkind (Act_Prev) = N_Explicit_Dereference | |
2904 | then | |
2905 | Add_Extra_Actual | |
2906 | (New_Occurrence_Of (Standard_False, Loc), | |
2907 | Extra_Constrained (Formal)); | |
2908 | ||
2909 | else | |
2910 | Add_Extra_Actual | |
2911 | (Make_Attribute_Reference (Sloc (Prev), | |
2912 | Prefix => | |
2913 | Duplicate_Subexpr_No_Checks | |
2914 | (Act_Prev, Name_Req => True), | |
2915 | Attribute_Name => Name_Constrained), | |
2916 | Extra_Constrained (Formal)); | |
2917 | end if; | |
2918 | end; | |
2919 | end if; | |
2920 | end if; | |
2921 | ||
2922 | -- Create possible extra actual for accessibility level | |
2923 | ||
2924 | if Present (Extra_Accessibility (Formal)) then | |
2925 | ||
2926 | -- Ada 2005 (AI-252): If the actual was rewritten as an Access | |
2927 | -- attribute, then the original actual may be an aliased object | |
2928 | -- occurring as the prefix in a call using "Object.Operation" | |
2929 | -- notation. In that case we must pass the level of the object, | |
2930 | -- so Prev_Orig is reset to Prev and the attribute will be | |
2931 | -- processed by the code for Access attributes further below. | |
2932 | ||
2933 | if Prev_Orig /= Prev | |
2934 | and then Nkind (Prev) = N_Attribute_Reference | |
2935 | and then | |
2936 | Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access | |
2937 | and then Is_Aliased_View (Prev_Orig) | |
2938 | then | |
2939 | Prev_Orig := Prev; | |
2940 | end if; | |
2941 | ||
2942 | -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of | |
2943 | -- accessibility levels. | |
2944 | ||
2945 | if Is_Thunk (Current_Scope) then | |
2946 | declare | |
2947 | Parm_Ent : Entity_Id; | |
2948 | ||
2949 | begin | |
2950 | if Is_Controlling_Actual (Actual) then | |
2951 | ||
2952 | -- Find the corresponding actual of the thunk | |
2953 | ||
2954 | Parm_Ent := First_Entity (Current_Scope); | |
2955 | for J in 2 .. Param_Count loop | |
2956 | Next_Entity (Parm_Ent); | |
2957 | end loop; | |
2958 | ||
2959 | -- Handle unchecked conversion of access types generated | |
2960 | -- in thunks (cf. Expand_Interface_Thunk). | |
2961 | ||
2962 | elsif Is_Access_Type (Etype (Actual)) | |
2963 | and then Nkind (Actual) = N_Unchecked_Type_Conversion | |
2964 | then | |
2965 | Parm_Ent := Entity (Expression (Actual)); | |
2966 | ||
2967 | else pragma Assert (Is_Entity_Name (Actual)); | |
2968 | Parm_Ent := Entity (Actual); | |
2969 | end if; | |
2970 | ||
2971 | Add_Extra_Actual | |
2972 | (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc), | |
2973 | Extra_Accessibility (Formal)); | |
2974 | end; | |
2975 | ||
2976 | elsif Is_Entity_Name (Prev_Orig) then | |
2977 | ||
2978 | -- When passing an access parameter, or a renaming of an access | |
2979 | -- parameter, as the actual to another access parameter we need | |
2980 | -- to pass along the actual's own access level parameter. This | |
2981 | -- is done if we are within the scope of the formal access | |
2982 | -- parameter (if this is an inlined body the extra formal is | |
2983 | -- irrelevant). | |
2984 | ||
2985 | if (Is_Formal (Entity (Prev_Orig)) | |
2986 | or else | |
2987 | (Present (Renamed_Object (Entity (Prev_Orig))) | |
2988 | and then | |
2989 | Is_Entity_Name (Renamed_Object (Entity (Prev_Orig))) | |
2990 | and then | |
2991 | Is_Formal | |
2992 | (Entity (Renamed_Object (Entity (Prev_Orig)))))) | |
2993 | and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type | |
2994 | and then In_Open_Scopes (Scope (Entity (Prev_Orig))) | |
2995 | then | |
2996 | declare | |
2997 | Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig); | |
2998 | ||
2999 | begin | |
3000 | pragma Assert (Present (Parm_Ent)); | |
3001 | ||
3002 | if Present (Extra_Accessibility (Parm_Ent)) then | |
3003 | Add_Extra_Actual | |
3004 | (New_Occurrence_Of | |
3005 | (Extra_Accessibility (Parm_Ent), Loc), | |
3006 | Extra_Accessibility (Formal)); | |
3007 | ||
3008 | -- If the actual access parameter does not have an | |
3009 | -- associated extra formal providing its scope level, | |
3010 | -- then treat the actual as having library-level | |
3011 | -- accessibility. | |
3012 | ||
3013 | else | |
3014 | Add_Extra_Actual | |
3015 | (Make_Integer_Literal (Loc, | |
3016 | Intval => Scope_Depth (Standard_Standard)), | |
3017 | Extra_Accessibility (Formal)); | |
3018 | end if; | |
3019 | end; | |
3020 | ||
3021 | -- The actual is a normal access value, so just pass the level | |
3022 | -- of the actual's access type. | |
3023 | ||
3024 | else | |
3025 | Add_Extra_Actual | |
3026 | (Dynamic_Accessibility_Level (Prev_Orig), | |
3027 | Extra_Accessibility (Formal)); | |
3028 | end if; | |
3029 | ||
3030 | -- If the actual is an access discriminant, then pass the level | |
3031 | -- of the enclosing object (RM05-3.10.2(12.4/2)). | |
3032 | ||
3033 | elsif Nkind (Prev_Orig) = N_Selected_Component | |
3034 | and then Ekind (Entity (Selector_Name (Prev_Orig))) = | |
3035 | E_Discriminant | |
3036 | and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) = | |
3037 | E_Anonymous_Access_Type | |
3038 | then | |
3039 | Add_Extra_Actual | |
3040 | (Make_Integer_Literal (Loc, | |
3041 | Intval => Object_Access_Level (Prefix (Prev_Orig))), | |
3042 | Extra_Accessibility (Formal)); | |
3043 | ||
3044 | -- All other cases | |
3045 | ||
3046 | else | |
3047 | case Nkind (Prev_Orig) is | |
3048 | when N_Attribute_Reference => | |
3049 | case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is | |
3050 | ||
3051 | -- For X'Access, pass on the level of the prefix X | |
3052 | ||
3053 | when Attribute_Access => | |
3054 | ||
3055 | -- If this is an Access attribute applied to the | |
3056 | -- the current instance object passed to a type | |
3057 | -- initialization procedure, then use the level | |
3058 | -- of the type itself. This is not really correct, | |
3059 | -- as there should be an extra level parameter | |
3060 | -- passed in with _init formals (only in the case | |
3061 | -- where the type is immutably limited), but we | |
3062 | -- don't have an easy way currently to create such | |
3063 | -- an extra formal (init procs aren't ever frozen). | |
3064 | -- For now we just use the level of the type, | |
3065 | -- which may be too shallow, but that works better | |
3066 | -- than passing Object_Access_Level of the type, | |
3067 | -- which can be one level too deep in some cases. | |
3068 | -- ??? | |
3069 | ||
3070 | if Is_Entity_Name (Prefix (Prev_Orig)) | |
3071 | and then Is_Type (Entity (Prefix (Prev_Orig))) | |
3072 | then | |
3073 | Add_Extra_Actual | |
3074 | (Make_Integer_Literal (Loc, | |
3075 | Intval => | |
3076 | Type_Access_Level | |
3077 | (Entity (Prefix (Prev_Orig)))), | |
3078 | Extra_Accessibility (Formal)); | |
3079 | ||
3080 | else | |
3081 | Add_Extra_Actual | |
3082 | (Make_Integer_Literal (Loc, | |
3083 | Intval => | |
3084 | Object_Access_Level | |
3085 | (Prefix (Prev_Orig))), | |
3086 | Extra_Accessibility (Formal)); | |
3087 | end if; | |
3088 | ||
3089 | -- Treat the unchecked attributes as library-level | |
3090 | ||
3091 | when Attribute_Unchecked_Access | |
3092 | | Attribute_Unrestricted_Access | |
3093 | => | |
3094 | Add_Extra_Actual | |
3095 | (Make_Integer_Literal (Loc, | |
3096 | Intval => Scope_Depth (Standard_Standard)), | |
3097 | Extra_Accessibility (Formal)); | |
3098 | ||
3099 | -- No other cases of attributes returning access | |
3100 | -- values that can be passed to access parameters. | |
3101 | ||
3102 | when others => | |
3103 | raise Program_Error; | |
3104 | ||
3105 | end case; | |
3106 | ||
3107 | -- For allocators we pass the level of the execution of the | |
3108 | -- called subprogram, which is one greater than the current | |
3109 | -- scope level. | |
3110 | ||
3111 | when N_Allocator => | |
3112 | Add_Extra_Actual | |
3113 | (Make_Integer_Literal (Loc, | |
3114 | Intval => Scope_Depth (Current_Scope) + 1), | |
3115 | Extra_Accessibility (Formal)); | |
3116 | ||
3117 | -- For most other cases we simply pass the level of the | |
3118 | -- actual's access type. The type is retrieved from | |
3119 | -- Prev rather than Prev_Orig, because in some cases | |
3120 | -- Prev_Orig denotes an original expression that has | |
3121 | -- not been analyzed. | |
3122 | ||
3123 | when others => | |
3124 | Add_Extra_Actual | |
3125 | (Dynamic_Accessibility_Level (Prev), | |
3126 | Extra_Accessibility (Formal)); | |
3127 | end case; | |
3128 | end if; | |
3129 | end if; | |
3130 | ||
3131 | -- Perform the check of 4.6(49) that prevents a null value from being | |
3132 | -- passed as an actual to an access parameter. Note that the check | |
3133 | -- is elided in the common cases of passing an access attribute or | |
3134 | -- access parameter as an actual. Also, we currently don't enforce | |
3135 | -- this check for expander-generated actuals and when -gnatdj is set. | |
3136 | ||
3137 | if Ada_Version >= Ada_2005 then | |
3138 | ||
3139 | -- Ada 2005 (AI-231): Check null-excluding access types. Note that | |
3140 | -- the intent of 6.4.1(13) is that null-exclusion checks should | |
3141 | -- not be done for 'out' parameters, even though it refers only | |
3142 | -- to constraint checks, and a null_exclusion is not a constraint. | |
3143 | -- Note that AI05-0196-1 corrects this mistake in the RM. | |
3144 | ||
3145 | if Is_Access_Type (Etype (Formal)) | |
3146 | and then Can_Never_Be_Null (Etype (Formal)) | |
3147 | and then Ekind (Formal) /= E_Out_Parameter | |
3148 | and then Nkind (Prev) /= N_Raise_Constraint_Error | |
3149 | and then (Known_Null (Prev) | |
3150 | or else not Can_Never_Be_Null (Etype (Prev))) | |
3151 | then | |
3152 | Install_Null_Excluding_Check (Prev); | |
3153 | end if; | |
3154 | ||
3155 | -- Ada_Version < Ada_2005 | |
3156 | ||
3157 | else | |
3158 | if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type | |
3159 | or else Access_Checks_Suppressed (Subp) | |
3160 | then | |
3161 | null; | |
3162 | ||
3163 | elsif Debug_Flag_J then | |
3164 | null; | |
3165 | ||
3166 | elsif not Comes_From_Source (Prev) then | |
3167 | null; | |
3168 | ||
3169 | elsif Is_Entity_Name (Prev) | |
3170 | and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type | |
3171 | then | |
3172 | null; | |
3173 | ||
3174 | elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then | |
3175 | null; | |
3176 | ||
3177 | else | |
3178 | Install_Null_Excluding_Check (Prev); | |
3179 | end if; | |
3180 | end if; | |
3181 | ||
3182 | -- Perform appropriate validity checks on parameters that | |
3183 | -- are entities. | |
3184 | ||
3185 | if Validity_Checks_On then | |
3186 | if (Ekind (Formal) = E_In_Parameter | |
3187 | and then Validity_Check_In_Params) | |
3188 | or else | |
3189 | (Ekind (Formal) = E_In_Out_Parameter | |
3190 | and then Validity_Check_In_Out_Params) | |
3191 | then | |
3192 | -- If the actual is an indexed component of a packed type (or | |
3193 | -- is an indexed or selected component whose prefix recursively | |
3194 | -- meets this condition), it has not been expanded yet. It will | |
3195 | -- be copied in the validity code that follows, and has to be | |
3196 | -- expanded appropriately, so reanalyze it. | |
3197 | ||
3198 | -- What we do is just to unset analyzed bits on prefixes till | |
3199 | -- we reach something that does not have a prefix. | |
3200 | ||
3201 | declare | |
3202 | Nod : Node_Id; | |
3203 | ||
3204 | begin | |
3205 | Nod := Actual; | |
3206 | while Nkind_In (Nod, N_Indexed_Component, | |
3207 | N_Selected_Component) | |
3208 | loop | |
3209 | Set_Analyzed (Nod, False); | |
3210 | Nod := Prefix (Nod); | |
3211 | end loop; | |
3212 | end; | |
3213 | ||
3214 | Ensure_Valid (Actual); | |
3215 | end if; | |
3216 | end if; | |
3217 | ||
3218 | -- For IN OUT and OUT parameters, ensure that subscripts are valid | |
3219 | -- since this is a left side reference. We only do this for calls | |
3220 | -- from the source program since we assume that compiler generated | |
3221 | -- calls explicitly generate any required checks. We also need it | |
3222 | -- only if we are doing standard validity checks, since clearly it is | |
3223 | -- not needed if validity checks are off, and in subscript validity | |
3224 | -- checking mode, all indexed components are checked with a call | |
3225 | -- directly from Expand_N_Indexed_Component. | |
3226 | ||
3227 | if Comes_From_Source (Call_Node) | |
3228 | and then Ekind (Formal) /= E_In_Parameter | |
3229 | and then Validity_Checks_On | |
3230 | and then Validity_Check_Default | |
3231 | and then not Validity_Check_Subscripts | |
3232 | then | |
3233 | Check_Valid_Lvalue_Subscripts (Actual); | |
3234 | end if; | |
3235 | ||
3236 | -- Mark any scalar OUT parameter that is a simple variable as no | |
3237 | -- longer known to be valid (unless the type is always valid). This | |
3238 | -- reflects the fact that if an OUT parameter is never set in a | |
3239 | -- procedure, then it can become invalid on the procedure return. | |
3240 | ||
3241 | if Ekind (Formal) = E_Out_Parameter | |
3242 | and then Is_Entity_Name (Actual) | |
3243 | and then Ekind (Entity (Actual)) = E_Variable | |
3244 | and then not Is_Known_Valid (Etype (Actual)) | |
3245 | then | |
3246 | Set_Is_Known_Valid (Entity (Actual), False); | |
3247 | end if; | |
3248 | ||
3249 | -- For an OUT or IN OUT parameter, if the actual is an entity, then | |
3250 | -- clear current values, since they can be clobbered. We are probably | |
3251 | -- doing this in more places than we need to, but better safe than | |
3252 | -- sorry when it comes to retaining bad current values. | |
3253 | ||
3254 | if Ekind (Formal) /= E_In_Parameter | |
3255 | and then Is_Entity_Name (Actual) | |
3256 | and then Present (Entity (Actual)) | |
3257 | then | |
3258 | declare | |
3259 | Ent : constant Entity_Id := Entity (Actual); | |
3260 | Sav : Node_Id; | |
3261 | ||
3262 | begin | |
3263 | -- For an OUT or IN OUT parameter that is an assignable entity, | |
3264 | -- we do not want to clobber the Last_Assignment field, since | |
3265 | -- if it is set, it was precisely because it is indeed an OUT | |
3266 | -- or IN OUT parameter. We do reset the Is_Known_Valid flag | |
3267 | -- since the subprogram could have returned in invalid value. | |
3268 | ||
3269 | if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter) | |
3270 | and then Is_Assignable (Ent) | |
3271 | then | |
3272 | Sav := Last_Assignment (Ent); | |
3273 | Kill_Current_Values (Ent); | |
3274 | Set_Last_Assignment (Ent, Sav); | |
3275 | Set_Is_Known_Valid (Ent, False); | |
3276 | ||
3277 | -- For all other cases, just kill the current values | |
3278 | ||
3279 | else | |
3280 | Kill_Current_Values (Ent); | |
3281 | end if; | |
3282 | end; | |
3283 | end if; | |
3284 | ||
3285 | -- If the formal is class wide and the actual is an aggregate, force | |
3286 | -- evaluation so that the back end who does not know about class-wide | |
3287 | -- type, does not generate a temporary of the wrong size. | |
3288 | ||
3289 | if not Is_Class_Wide_Type (Etype (Formal)) then | |
3290 | null; | |
3291 | ||
3292 | elsif Nkind (Actual) = N_Aggregate | |
3293 | or else (Nkind (Actual) = N_Qualified_Expression | |
3294 | and then Nkind (Expression (Actual)) = N_Aggregate) | |
3295 | then | |
3296 | Force_Evaluation (Actual); | |
3297 | end if; | |
3298 | ||
3299 | -- In a remote call, if the formal is of a class-wide type, check | |
3300 | -- that the actual meets the requirements described in E.4(18). | |
3301 | ||
3302 | if Remote and then Is_Class_Wide_Type (Etype (Formal)) then | |
3303 | Insert_Action (Actual, | |
3304 | Make_Transportable_Check (Loc, | |
3305 | Duplicate_Subexpr_Move_Checks (Actual))); | |
3306 | end if; | |
3307 | ||
3308 | -- Perform invariant checks for all intermediate types in a view | |
3309 | -- conversion after successful return from a call that passes the | |
3310 | -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3, | |
3311 | -- 13/3, 14/3)). Consider only source conversion in order to avoid | |
3312 | -- generating spurious checks on complex expansion such as object | |
3313 | -- initialization through an extension aggregate. | |
3314 | ||
3315 | if Comes_From_Source (N) | |
3316 | and then Ekind (Formal) /= E_In_Parameter | |
3317 | and then Nkind (Actual) = N_Type_Conversion | |
3318 | then | |
3319 | Add_View_Conversion_Invariants (Formal, Actual); | |
3320 | end if; | |
3321 | ||
3322 | -- Generating C the initialization of an allocator is performed by | |
3323 | -- means of individual statements, and hence it must be done before | |
3324 | -- the call. | |
3325 | ||
3326 | if Modify_Tree_For_C | |
3327 | and then Nkind (Actual) = N_Allocator | |
3328 | and then Nkind (Expression (Actual)) = N_Qualified_Expression | |
3329 | then | |
3330 | Remove_Side_Effects (Actual); | |
3331 | end if; | |
3332 | ||
3333 | -- This label is required when skipping extra actual generation for | |
3334 | -- Unchecked_Union parameters. | |
3335 | ||
3336 | <<Skip_Extra_Actual_Generation>> | |
3337 | ||
3338 | Param_Count := Param_Count + 1; | |
3339 | Next_Actual (Actual); | |
3340 | Next_Formal (Formal); | |
3341 | end loop; | |
3342 | ||
3343 | -- If we are calling an Ada 2012 function which needs to have the | |
3344 | -- "accessibility level determined by the point of call" (AI05-0234) | |
3345 | -- passed in to it, then pass it in. | |
3346 | ||
3347 | if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type) | |
3348 | and then | |
3349 | Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp))) | |
3350 | then | |
3351 | declare | |
3352 | Ancestor : Node_Id := Parent (Call_Node); | |
3353 | Level : Node_Id := Empty; | |
3354 | Defer : Boolean := False; | |
3355 | ||
3356 | begin | |
3357 | -- Unimplemented: if Subp returns an anonymous access type, then | |
3358 | ||
3359 | -- a) if the call is the operand of an explict conversion, then | |
3360 | -- the target type of the conversion (a named access type) | |
3361 | -- determines the accessibility level pass in; | |
3362 | ||
3363 | -- b) if the call defines an access discriminant of an object | |
3364 | -- (e.g., the discriminant of an object being created by an | |
3365 | -- allocator, or the discriminant of a function result), | |
3366 | -- then the accessibility level to pass in is that of the | |
3367 | -- discriminated object being initialized). | |
3368 | ||
3369 | -- ??? | |
3370 | ||
3371 | while Nkind (Ancestor) = N_Qualified_Expression | |
3372 | loop | |
3373 | Ancestor := Parent (Ancestor); | |
3374 | end loop; | |
3375 | ||
3376 | case Nkind (Ancestor) is | |
3377 | when N_Allocator => | |
3378 | ||
3379 | -- At this point, we'd like to assign | |
3380 | ||
3381 | -- Level := Dynamic_Accessibility_Level (Ancestor); | |
3382 | ||
3383 | -- but Etype of Ancestor may not have been set yet, | |
3384 | -- so that doesn't work. | |
3385 | ||
3386 | -- Handle this later in Expand_Allocator_Expression. | |
3387 | ||
3388 | Defer := True; | |
3389 | ||
3390 | when N_Object_Declaration | |
3391 | | N_Object_Renaming_Declaration | |
3392 | => | |
3393 | declare | |
3394 | Def_Id : constant Entity_Id := | |
3395 | Defining_Identifier (Ancestor); | |
3396 | ||
3397 | begin | |
3398 | if Is_Return_Object (Def_Id) then | |
3399 | if Present (Extra_Accessibility_Of_Result | |
3400 | (Return_Applies_To (Scope (Def_Id)))) | |
3401 | then | |
3402 | -- Pass along value that was passed in if the | |
3403 | -- routine we are returning from also has an | |
3404 | -- Accessibility_Of_Result formal. | |
3405 | ||
3406 | Level := | |
3407 | New_Occurrence_Of | |
3408 | (Extra_Accessibility_Of_Result | |
3409 | (Return_Applies_To (Scope (Def_Id))), Loc); | |
3410 | end if; | |
3411 | else | |
3412 | Level := | |
3413 | Make_Integer_Literal (Loc, | |
3414 | Intval => Object_Access_Level (Def_Id)); | |
3415 | end if; | |
3416 | end; | |
3417 | ||
3418 | when N_Simple_Return_Statement => | |
3419 | if Present (Extra_Accessibility_Of_Result | |
3420 | (Return_Applies_To | |
3421 | (Return_Statement_Entity (Ancestor)))) | |
3422 | then | |
3423 | -- Pass along value that was passed in if the returned | |
3424 | -- routine also has an Accessibility_Of_Result formal. | |
3425 | ||
3426 | Level := | |
3427 | New_Occurrence_Of | |
3428 | (Extra_Accessibility_Of_Result | |
3429 | (Return_Applies_To | |
3430 | (Return_Statement_Entity (Ancestor))), Loc); | |
3431 | end if; | |
3432 | ||
3433 | when others => | |
3434 | null; | |
3435 | end case; | |
3436 | ||
3437 | if not Defer then | |
3438 | if not Present (Level) then | |
3439 | ||
3440 | -- The "innermost master that evaluates the function call". | |
3441 | ||
3442 | -- ??? - Should we use Integer'Last here instead in order | |
3443 | -- to deal with (some of) the problems associated with | |
3444 | -- calls to subps whose enclosing scope is unknown (e.g., | |
3445 | -- Anon_Access_To_Subp_Param.all)? | |
3446 | ||
3447 | Level := | |
3448 | Make_Integer_Literal (Loc, | |
3449 | Intval => Scope_Depth (Current_Scope) + 1); | |
3450 | end if; | |
3451 | ||
3452 | Add_Extra_Actual | |
3453 | (Level, | |
3454 | Extra_Accessibility_Of_Result (Ultimate_Alias (Subp))); | |
3455 | end if; | |
3456 | end; | |
3457 | end if; | |
3458 | ||
3459 | -- If we are expanding the RHS of an assignment we need to check if tag | |
3460 | -- propagation is needed. You might expect this processing to be in | |
3461 | -- Analyze_Assignment but has to be done earlier (bottom-up) because the | |
3462 | -- assignment might be transformed to a declaration for an unconstrained | |
3463 | -- value if the expression is classwide. | |
3464 | ||
3465 | if Nkind (Call_Node) = N_Function_Call | |
3466 | and then Is_Tag_Indeterminate (Call_Node) | |
3467 | and then Is_Entity_Name (Name (Call_Node)) | |
3468 | then | |
3469 | declare | |
3470 | Ass : Node_Id := Empty; | |
3471 | ||
3472 | begin | |
3473 | if Nkind (Parent (Call_Node)) = N_Assignment_Statement then | |
3474 | Ass := Parent (Call_Node); | |
3475 | ||
3476 | elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression | |
3477 | and then Nkind (Parent (Parent (Call_Node))) = | |
3478 | N_Assignment_Statement | |
3479 | then | |
3480 | Ass := Parent (Parent (Call_Node)); | |
3481 | ||
3482 | elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference | |
3483 | and then Nkind (Parent (Parent (Call_Node))) = | |
3484 | N_Assignment_Statement | |
3485 | then | |
3486 | Ass := Parent (Parent (Call_Node)); | |
3487 | end if; | |
3488 | ||
3489 | if Present (Ass) | |
3490 | and then Is_Class_Wide_Type (Etype (Name (Ass))) | |
3491 | then | |
3492 | if Is_Access_Type (Etype (Call_Node)) then | |
3493 | if Designated_Type (Etype (Call_Node)) /= | |
3494 | Root_Type (Etype (Name (Ass))) | |
3495 | then | |
3496 | Error_Msg_NE | |
3497 | ("tag-indeterminate expression " | |
3498 | & " must have designated type& (RM 5.2 (6))", | |
3499 | Call_Node, Root_Type (Etype (Name (Ass)))); | |
3500 | else | |
3501 | Propagate_Tag (Name (Ass), Call_Node); | |
3502 | end if; | |
3503 | ||
3504 | elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then | |
3505 | Error_Msg_NE | |
3506 | ("tag-indeterminate expression must have type&" | |
3507 | & "(RM 5.2 (6))", | |
3508 | Call_Node, Root_Type (Etype (Name (Ass)))); | |
3509 | ||
3510 | else | |
3511 | Propagate_Tag (Name (Ass), Call_Node); | |
3512 | end if; | |
3513 | ||
3514 | -- The call will be rewritten as a dispatching call, and | |
3515 | -- expanded as such. | |
3516 | ||
3517 | return; | |
3518 | end if; | |
3519 | end; | |
3520 | end if; | |
3521 | ||
3522 | -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand | |
3523 | -- it to point to the correct secondary virtual table | |
3524 | ||
3525 | if Nkind (Call_Node) in N_Subprogram_Call | |
3526 | and then CW_Interface_Formals_Present | |
3527 | then | |
3528 | Expand_Interface_Actuals (Call_Node); | |
3529 | end if; | |
3530 | ||
3531 | -- Deals with Dispatch_Call if we still have a call, before expanding | |
3532 | -- extra actuals since this will be done on the re-analysis of the | |
3533 | -- dispatching call. Note that we do not try to shorten the actual list | |
3534 | -- for a dispatching call, it would not make sense to do so. Expansion | |
3535 | -- of dispatching calls is suppressed for VM targets, because the VM | |
3536 | -- back-ends directly handle the generation of dispatching calls and | |
3537 | -- would have to undo any expansion to an indirect call. | |
3538 | ||
3539 | if Nkind (Call_Node) in N_Subprogram_Call | |
3540 | and then Present (Controlling_Argument (Call_Node)) | |
3541 | then | |
3542 | declare | |
3543 | Call_Typ : constant Entity_Id := Etype (Call_Node); | |
3544 | Typ : constant Entity_Id := Find_Dispatching_Type (Subp); | |
3545 | Eq_Prim_Op : Entity_Id := Empty; | |
3546 | New_Call : Node_Id; | |
3547 | Param : Node_Id; | |
3548 | Prev_Call : Node_Id; | |
3549 | ||
3550 | begin | |
3551 | if not Is_Limited_Type (Typ) then | |
3552 | Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq); | |
3553 | end if; | |
3554 | ||
3555 | if Tagged_Type_Expansion then | |
3556 | Expand_Dispatching_Call (Call_Node); | |
3557 | ||
3558 | -- The following return is worrisome. Is it really OK to skip | |
3559 | -- all remaining processing in this procedure ??? | |
3560 | ||
3561 | return; | |
3562 | ||
3563 | -- VM targets | |
3564 | ||
3565 | else | |
3566 | Apply_Tag_Checks (Call_Node); | |
3567 | ||
3568 | -- If this is a dispatching "=", we must first compare the | |
3569 | -- tags so we generate: x.tag = y.tag and then x = y | |
3570 | ||
3571 | if Subp = Eq_Prim_Op then | |
3572 | ||
3573 | -- Mark the node as analyzed to avoid reanalyzing this | |
3574 | -- dispatching call (which would cause a never-ending loop) | |
3575 | ||
3576 | Prev_Call := Relocate_Node (Call_Node); | |
3577 | Set_Analyzed (Prev_Call); | |
3578 | ||
3579 | Param := First_Actual (Call_Node); | |
3580 | New_Call := | |
3581 | Make_And_Then (Loc, | |
3582 | Left_Opnd => | |
3583 | Make_Op_Eq (Loc, | |
3584 | Left_Opnd => | |
3585 | Make_Selected_Component (Loc, | |
3586 | Prefix => New_Value (Param), | |
3587 | Selector_Name => | |
3588 | New_Occurrence_Of | |
3589 | (First_Tag_Component (Typ), Loc)), | |
3590 | ||
3591 | Right_Opnd => | |
3592 | Make_Selected_Component (Loc, | |
3593 | Prefix => | |
3594 | Unchecked_Convert_To (Typ, | |
3595 | New_Value (Next_Actual (Param))), | |
3596 | Selector_Name => | |
3597 | New_Occurrence_Of | |
3598 | (First_Tag_Component (Typ), Loc))), | |
3599 | Right_Opnd => Prev_Call); | |
3600 | ||
3601 | Rewrite (Call_Node, New_Call); | |
3602 | ||
3603 | Analyze_And_Resolve | |
3604 | (Call_Node, Call_Typ, Suppress => All_Checks); | |
3605 | end if; | |
3606 | ||
3607 | -- Expansion of a dispatching call results in an indirect call, | |
3608 | -- which in turn causes current values to be killed (see | |
3609 | -- Resolve_Call), so on VM targets we do the call here to | |
3610 | -- ensure consistent warnings between VM and non-VM targets. | |
3611 | ||
3612 | Kill_Current_Values; | |
3613 | end if; | |
3614 | ||
3615 | -- If this is a dispatching "=" then we must update the reference | |
3616 | -- to the call node because we generated: | |
3617 | -- x.tag = y.tag and then x = y | |
3618 | ||
3619 | if Subp = Eq_Prim_Op then | |
3620 | Call_Node := Right_Opnd (Call_Node); | |
3621 | end if; | |
3622 | end; | |
3623 | end if; | |
3624 | ||
3625 | -- Similarly, expand calls to RCI subprograms on which pragma | |
3626 | -- All_Calls_Remote applies. The rewriting will be reanalyzed | |
3627 | -- later. Do this only when the call comes from source since we | |
3628 | -- do not want such a rewriting to occur in expanded code. | |
3629 | ||
3630 | if Is_All_Remote_Call (Call_Node) then | |
3631 | Expand_All_Calls_Remote_Subprogram_Call (Call_Node); | |
3632 | ||
3633 | -- Similarly, do not add extra actuals for an entry call whose entity | |
3634 | -- is a protected procedure, or for an internal protected subprogram | |
3635 | -- call, because it will be rewritten as a protected subprogram call | |
3636 | -- and reanalyzed (see Expand_Protected_Subprogram_Call). | |
3637 | ||
3638 | elsif Is_Protected_Type (Scope (Subp)) | |
3639 | and then (Ekind (Subp) = E_Procedure | |
3640 | or else Ekind (Subp) = E_Function) | |
3641 | then | |
3642 | null; | |
3643 | ||
3644 | -- During that loop we gathered the extra actuals (the ones that | |
3645 | -- correspond to Extra_Formals), so now they can be appended. | |
3646 | ||
3647 | else | |
3648 | while Is_Non_Empty_List (Extra_Actuals) loop | |
3649 | Add_Actual_Parameter (Remove_Head (Extra_Actuals)); | |
3650 | end loop; | |
3651 | end if; | |
3652 | ||
3653 | -- At this point we have all the actuals, so this is the point at which | |
3654 | -- the various expansion activities for actuals is carried out. | |
3655 | ||
3656 | Expand_Actuals (Call_Node, Subp, Post_Call); | |
3657 | ||
3658 | -- Verify that the actuals do not share storage. This check must be done | |
3659 | -- on the caller side rather that inside the subprogram to avoid issues | |
3660 | -- of parameter passing. | |
3661 | ||
3662 | if Check_Aliasing_Of_Parameters then | |
3663 | Apply_Parameter_Aliasing_Checks (Call_Node, Subp); | |
3664 | end if; | |
3665 | ||
3666 | -- If the subprogram is a renaming, or if it is inherited, replace it in | |
3667 | -- the call with the name of the actual subprogram being called. If this | |
3668 | -- is a dispatching call, the run-time decides what to call. The Alias | |
3669 | -- attribute does not apply to entries. | |
3670 | ||
3671 | if Nkind (Call_Node) /= N_Entry_Call_Statement | |
3672 | and then No (Controlling_Argument (Call_Node)) | |
3673 | and then Present (Parent_Subp) | |
3674 | and then not Is_Direct_Deep_Call (Subp) | |
3675 | then | |
3676 | if Present (Inherited_From_Formal (Subp)) then | |
3677 | Parent_Subp := Inherited_From_Formal (Subp); | |
3678 | else | |
3679 | Parent_Subp := Ultimate_Alias (Parent_Subp); | |
3680 | end if; | |
3681 | ||
3682 | -- The below setting of Entity is suspect, see F109-018 discussion??? | |
3683 | ||
3684 | Set_Entity (Name (Call_Node), Parent_Subp); | |
3685 | ||
3686 | if Is_Abstract_Subprogram (Parent_Subp) | |
3687 | and then not In_Instance | |
3688 | then | |
3689 | Error_Msg_NE | |
3690 | ("cannot call abstract subprogram &!", | |
3691 | Name (Call_Node), Parent_Subp); | |
3692 | end if; | |
3693 | ||
3694 | -- Inspect all formals of derived subprogram Subp. Compare parameter | |
3695 | -- types with the parent subprogram and check whether an actual may | |
3696 | -- need a type conversion to the corresponding formal of the parent | |
3697 | -- subprogram. | |
3698 | ||
3699 | -- Not clear whether intrinsic subprograms need such conversions. ??? | |
3700 | ||
3701 | if not Is_Intrinsic_Subprogram (Parent_Subp) | |
3702 | or else Is_Generic_Instance (Parent_Subp) | |
3703 | then | |
3704 | declare | |
3705 | procedure Convert (Act : Node_Id; Typ : Entity_Id); | |
3706 | -- Rewrite node Act as a type conversion of Act to Typ. Analyze | |
3707 | -- and resolve the newly generated construct. | |
3708 | ||
3709 | ------------- | |
3710 | -- Convert -- | |
3711 | ------------- | |
3712 | ||
3713 | procedure Convert (Act : Node_Id; Typ : Entity_Id) is | |
3714 | begin | |
3715 | Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act))); | |
3716 | Analyze (Act); | |
3717 | Resolve (Act, Typ); | |
3718 | end Convert; | |
3719 | ||
3720 | -- Local variables | |
3721 | ||
3722 | Actual_Typ : Entity_Id; | |
3723 | Formal_Typ : Entity_Id; | |
3724 | Parent_Typ : Entity_Id; | |
3725 | ||
3726 | begin | |
3727 | Actual := First_Actual (Call_Node); | |
3728 | Formal := First_Formal (Subp); | |
3729 | Parent_Formal := First_Formal (Parent_Subp); | |
3730 | while Present (Formal) loop | |
3731 | Actual_Typ := Etype (Actual); | |
3732 | Formal_Typ := Etype (Formal); | |
3733 | Parent_Typ := Etype (Parent_Formal); | |
3734 | ||
3735 | -- For an IN parameter of a scalar type, the parent formal | |
3736 | -- type and derived formal type differ or the parent formal | |
3737 | -- type and actual type do not match statically. | |
3738 | ||
3739 | if Is_Scalar_Type (Formal_Typ) | |
3740 | and then Ekind (Formal) = E_In_Parameter | |
3741 | and then Formal_Typ /= Parent_Typ | |
3742 | and then | |
3743 | not Subtypes_Statically_Match (Parent_Typ, Actual_Typ) | |
3744 | and then not Raises_Constraint_Error (Actual) | |
3745 | then | |
3746 | Convert (Actual, Parent_Typ); | |
3747 | Enable_Range_Check (Actual); | |
3748 | ||
3749 | -- If the actual has been marked as requiring a range | |
3750 | -- check, then generate it here. | |
3751 | ||
3752 | if Do_Range_Check (Actual) then | |
3753 | Generate_Range_Check | |
3754 | (Actual, Etype (Formal), CE_Range_Check_Failed); | |
3755 | end if; | |
3756 | ||
3757 | -- For access types, the parent formal type and actual type | |
3758 | -- differ. | |
3759 | ||
3760 | elsif Is_Access_Type (Formal_Typ) | |
3761 | and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ) | |
3762 | then | |
3763 | if Ekind (Formal) /= E_In_Parameter then | |
3764 | Convert (Actual, Parent_Typ); | |
3765 | ||
3766 | elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type | |
3767 | and then Designated_Type (Parent_Typ) /= | |
3768 | Designated_Type (Actual_Typ) | |
3769 | and then not Is_Controlling_Formal (Formal) | |
3770 | then | |
3771 | -- This unchecked conversion is not necessary unless | |
3772 | -- inlining is enabled, because in that case the type | |
3773 | -- mismatch may become visible in the body about to be | |
3774 | -- inlined. | |
3775 | ||
3776 | Rewrite (Actual, | |
3777 | Unchecked_Convert_To (Parent_Typ, | |
3778 | Relocate_Node (Actual))); | |
3779 | Analyze (Actual); | |
3780 | Resolve (Actual, Parent_Typ); | |
3781 | end if; | |
3782 | ||
3783 | -- If there is a change of representation, then generate a | |
3784 | -- warning, and do the change of representation. | |
3785 | ||
3786 | elsif not Same_Representation (Formal_Typ, Parent_Typ) then | |
3787 | Error_Msg_N | |
3788 | ("??change of representation required", Actual); | |
3789 | Convert (Actual, Parent_Typ); | |
3790 | ||
3791 | -- For array and record types, the parent formal type and | |
3792 | -- derived formal type have different sizes or pragma Pack | |
3793 | -- status. | |
3794 | ||
3795 | elsif ((Is_Array_Type (Formal_Typ) | |
3796 | and then Is_Array_Type (Parent_Typ)) | |
3797 | or else | |
3798 | (Is_Record_Type (Formal_Typ) | |
3799 | and then Is_Record_Type (Parent_Typ))) | |
3800 | and then | |
3801 | (Esize (Formal_Typ) /= Esize (Parent_Typ) | |
3802 | or else Has_Pragma_Pack (Formal_Typ) /= | |
3803 | Has_Pragma_Pack (Parent_Typ)) | |
3804 | then | |
3805 | Convert (Actual, Parent_Typ); | |
3806 | end if; | |
3807 | ||
3808 | Next_Actual (Actual); | |
3809 | Next_Formal (Formal); | |
3810 | Next_Formal (Parent_Formal); | |
3811 | end loop; | |
3812 | end; | |
3813 | end if; | |
3814 | ||
3815 | Orig_Subp := Subp; | |
3816 | Subp := Parent_Subp; | |
3817 | end if; | |
3818 | ||
3819 | -- Deal with case where call is an explicit dereference | |
3820 | ||
3821 | if Nkind (Name (Call_Node)) = N_Explicit_Dereference then | |
3822 | ||
3823 | -- Handle case of access to protected subprogram type | |
3824 | ||
3825 | if Is_Access_Protected_Subprogram_Type | |
3826 | (Base_Type (Etype (Prefix (Name (Call_Node))))) | |
3827 | then | |
3828 | -- If this is a call through an access to protected operation, the | |
3829 | -- prefix has the form (object'address, operation'access). Rewrite | |
3830 | -- as a for other protected calls: the object is the 1st parameter | |
3831 | -- of the list of actuals. | |
3832 | ||
3833 | declare | |
3834 | Call : Node_Id; | |
3835 | Parm : List_Id; | |
3836 | Nam : Node_Id; | |
3837 | Obj : Node_Id; | |
3838 | Ptr : constant Node_Id := Prefix (Name (Call_Node)); | |
3839 | ||
3840 | T : constant Entity_Id := | |
3841 | Equivalent_Type (Base_Type (Etype (Ptr))); | |
3842 | ||
3843 | D_T : constant Entity_Id := | |
3844 | Designated_Type (Base_Type (Etype (Ptr))); | |
3845 | ||
3846 | begin | |
3847 | Obj := | |
3848 | Make_Selected_Component (Loc, | |
3849 | Prefix => Unchecked_Convert_To (T, Ptr), | |
3850 | Selector_Name => | |
3851 | New_Occurrence_Of (First_Entity (T), Loc)); | |
3852 | ||
3853 | Nam := | |
3854 | Make_Selected_Component (Loc, | |
3855 | Prefix => Unchecked_Convert_To (T, Ptr), | |
3856 | Selector_Name => | |
3857 | New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc)); | |
3858 | ||
3859 | Nam := | |
3860 | Make_Explicit_Dereference (Loc, | |
3861 | Prefix => Nam); | |
3862 | ||
3863 | if Present (Parameter_Associations (Call_Node)) then | |
3864 | Parm := Parameter_Associations (Call_Node); | |
3865 | else | |
3866 | Parm := New_List; | |
3867 | end if; | |
3868 | ||
3869 | Prepend (Obj, Parm); | |
3870 | ||
3871 | if Etype (D_T) = Standard_Void_Type then | |
3872 | Call := | |
3873 | Make_Procedure_Call_Statement (Loc, | |
3874 | Name => Nam, | |
3875 | Parameter_Associations => Parm); | |
3876 | else | |
3877 | Call := | |
3878 | Make_Function_Call (Loc, | |
3879 | Name => Nam, | |
3880 | Parameter_Associations => Parm); | |
3881 | end if; | |
3882 | ||
3883 | Set_First_Named_Actual (Call, First_Named_Actual (Call_Node)); | |
3884 | Set_Etype (Call, Etype (D_T)); | |
3885 | ||
3886 | -- We do not re-analyze the call to avoid infinite recursion. | |
3887 | -- We analyze separately the prefix and the object, and set | |
3888 | -- the checks on the prefix that would otherwise be emitted | |
3889 | -- when resolving a call. | |
3890 | ||
3891 | Rewrite (Call_Node, Call); | |
3892 | Analyze (Nam); | |
3893 | Apply_Access_Check (Nam); | |
3894 | Analyze (Obj); | |
3895 | return; | |
3896 | end; | |
3897 | end if; | |
3898 | end if; | |
3899 | ||
3900 | -- If this is a call to an intrinsic subprogram, then perform the | |
3901 | -- appropriate expansion to the corresponding tree node and we | |
3902 | -- are all done (since after that the call is gone). | |
3903 | ||
3904 | -- In the case where the intrinsic is to be processed by the back end, | |
3905 | -- the call to Expand_Intrinsic_Call will do nothing, which is fine, | |
3906 | -- since the idea in this case is to pass the call unchanged. If the | |
3907 | -- intrinsic is an inherited unchecked conversion, and the derived type | |
3908 | -- is the target type of the conversion, we must retain it as the return | |
3909 | -- type of the expression. Otherwise the expansion below, which uses the | |
3910 | -- parent operation, will yield the wrong type. | |
3911 | ||
3912 | if Is_Intrinsic_Subprogram (Subp) then | |
3913 | Expand_Intrinsic_Call (Call_Node, Subp); | |
3914 | ||
3915 | if Nkind (Call_Node) = N_Unchecked_Type_Conversion | |
3916 | and then Parent_Subp /= Orig_Subp | |
3917 | and then Etype (Parent_Subp) /= Etype (Orig_Subp) | |
3918 | then | |
3919 | Set_Etype (Call_Node, Etype (Orig_Subp)); | |
3920 | end if; | |
3921 | ||
3922 | return; | |
3923 | end if; | |
3924 | ||
3925 | if Ekind_In (Subp, E_Function, E_Procedure) then | |
3926 | ||
3927 | -- We perform a simple optimization on calls for To_Address by | |
3928 | -- replacing them with an unchecked conversion. Not only is this | |
3929 | -- efficient, but it also avoids order of elaboration problems when | |
3930 | -- address clauses are inlined (address expression elaborated at the | |
3931 | -- wrong point). | |
3932 | ||
3933 | -- We perform this optimization regardless of whether we are in the | |
3934 | -- main unit or in a unit in the context of the main unit, to ensure | |
3935 | -- that the generated tree is the same in both cases, for CodePeer | |
3936 | -- use. | |
3937 | ||
3938 | if Is_RTE (Subp, RE_To_Address) then | |
3939 | Rewrite (Call_Node, | |
3940 | Unchecked_Convert_To | |
3941 | (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node)))); | |
3942 | return; | |
3943 | end if; | |
3944 | ||
3945 | -- Handle inlining. No action needed if the subprogram is not inlined | |
3946 | ||
3947 | if not Is_Inlined (Subp) then | |
3948 | null; | |
3949 | ||
3950 | -- Frontend inlining of expression functions (performed also when | |
3951 | -- backend inlining is enabled). | |
3952 | ||
3953 | elsif Is_Inlinable_Expression_Function (Subp) then | |
3954 | Rewrite (N, New_Copy (Expression_Of_Expression_Function (Subp))); | |
3955 | Analyze (N); | |
3956 | return; | |
3957 | ||
3958 | -- Handle frontend inlining | |
3959 | ||
3960 | elsif not Back_End_Inlining then | |
3961 | Inlined_Subprogram : declare | |
3962 | Bod : Node_Id; | |
3963 | Must_Inline : Boolean := False; | |
3964 | Spec : constant Node_Id := Unit_Declaration_Node (Subp); | |
3965 | ||
3966 | begin | |
3967 | -- Verify that the body to inline has already been seen, and | |
3968 | -- that if the body is in the current unit the inlining does | |
3969 | -- not occur earlier. This avoids order-of-elaboration problems | |
3970 | -- in the back end. | |
3971 | ||
3972 | -- This should be documented in sinfo/einfo ??? | |
3973 | ||
3974 | if No (Spec) | |
3975 | or else Nkind (Spec) /= N_Subprogram_Declaration | |
3976 | or else No (Body_To_Inline (Spec)) | |
3977 | then | |
3978 | Must_Inline := False; | |
3979 | ||
3980 | -- If this an inherited function that returns a private type, | |
3981 | -- do not inline if the full view is an unconstrained array, | |
3982 | -- because such calls cannot be inlined. | |
3983 | ||
3984 | elsif Present (Orig_Subp) | |
3985 | and then Is_Array_Type (Etype (Orig_Subp)) | |
3986 | and then not Is_Constrained (Etype (Orig_Subp)) | |
3987 | then | |
3988 | Must_Inline := False; | |
3989 | ||
3990 | elsif In_Unfrozen_Instance (Scope (Subp)) then | |
3991 | Must_Inline := False; | |
3992 | ||
3993 | else | |
3994 | Bod := Body_To_Inline (Spec); | |
3995 | ||
3996 | if (In_Extended_Main_Code_Unit (Call_Node) | |
3997 | or else In_Extended_Main_Code_Unit (Parent (Call_Node)) | |
3998 | or else Has_Pragma_Inline_Always (Subp)) | |
3999 | and then (not In_Same_Extended_Unit (Sloc (Bod), Loc) | |
4000 | or else | |
4001 | Earlier_In_Extended_Unit (Sloc (Bod), Loc)) | |
4002 | then | |
4003 | Must_Inline := True; | |
4004 | ||
4005 | -- If we are compiling a package body that is not the main | |
4006 | -- unit, it must be for inlining/instantiation purposes, | |
4007 | -- in which case we inline the call to insure that the same | |
4008 | -- temporaries are generated when compiling the body by | |
4009 | -- itself. Otherwise link errors can occur. | |
4010 | ||
4011 | -- If the function being called is itself in the main unit, | |
4012 | -- we cannot inline, because there is a risk of double | |
4013 | -- elaboration and/or circularity: the inlining can make | |
4014 | -- visible a private entity in the body of the main unit, | |
4015 | -- that gigi will see before its sees its proper definition. | |
4016 | ||
4017 | elsif not (In_Extended_Main_Code_Unit (Call_Node)) | |
4018 | and then In_Package_Body | |
4019 | then | |
4020 | Must_Inline := not In_Extended_Main_Source_Unit (Subp); | |
4021 | ||
4022 | -- Inline calls to _postconditions when generating C code | |
4023 | ||
4024 | elsif Modify_Tree_For_C | |
4025 | and then In_Same_Extended_Unit (Sloc (Bod), Loc) | |
4026 | and then Chars (Name (N)) = Name_uPostconditions | |
4027 | then | |
4028 | Must_Inline := True; | |
4029 | end if; | |
4030 | end if; | |
4031 | ||
4032 | if Must_Inline then | |
4033 | Expand_Inlined_Call (Call_Node, Subp, Orig_Subp); | |
4034 | ||
4035 | else | |
4036 | -- Let the back end handle it | |
4037 | ||
4038 | Add_Inlined_Body (Subp, Call_Node); | |
4039 | ||
4040 | if Front_End_Inlining | |
4041 | and then Nkind (Spec) = N_Subprogram_Declaration | |
4042 | and then (In_Extended_Main_Code_Unit (Call_Node)) | |
4043 | and then No (Body_To_Inline (Spec)) | |
4044 | and then not Has_Completion (Subp) | |
4045 | and then In_Same_Extended_Unit (Sloc (Spec), Loc) | |
4046 | then | |
4047 | Cannot_Inline | |
4048 | ("cannot inline& (body not seen yet)?", | |
4049 | Call_Node, Subp); | |
4050 | end if; | |
4051 | end if; | |
4052 | end Inlined_Subprogram; | |
4053 | ||
4054 | -- Back end inlining: let the back end handle it | |
4055 | ||
4056 | elsif No (Unit_Declaration_Node (Subp)) | |
4057 | or else Nkind (Unit_Declaration_Node (Subp)) /= | |
4058 | N_Subprogram_Declaration | |
4059 | or else No (Body_To_Inline (Unit_Declaration_Node (Subp))) | |
4060 | or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in | |
4061 | N_Entity | |
4062 | then | |
4063 | Add_Inlined_Body (Subp, Call_Node); | |
4064 | ||
4065 | -- If the inlined call appears within an instantiation and some | |
4066 | -- level of optimization is required, ensure that the enclosing | |
4067 | -- instance body is available so that the back-end can actually | |
4068 | -- perform the inlining. | |
4069 | ||
4070 | if In_Instance | |
4071 | and then Comes_From_Source (Subp) | |
4072 | and then Optimization_Level > 0 | |
4073 | then | |
4074 | declare | |
4075 | Decl : Node_Id; | |
4076 | Inst : Entity_Id; | |
4077 | Inst_Node : Node_Id; | |
4078 | ||
4079 | begin | |
4080 | Inst := Scope (Subp); | |
4081 | ||
4082 | -- Find enclosing instance | |
4083 | ||
4084 | while Present (Inst) and then Inst /= Standard_Standard loop | |
4085 | exit when Is_Generic_Instance (Inst); | |
4086 | Inst := Scope (Inst); | |
4087 | end loop; | |
4088 | ||
4089 | if Present (Inst) | |
4090 | and then Is_Generic_Instance (Inst) | |
4091 | and then not Is_Inlined (Inst) | |
4092 | then | |
4093 | Set_Is_Inlined (Inst); | |
4094 | Decl := Unit_Declaration_Node (Inst); | |
4095 | ||
4096 | -- Do not add a pending instantiation if the body exits | |
4097 | -- already, or if the instance is a compilation unit, or | |
4098 | -- the instance node is missing. | |
4099 | ||
4100 | if Present (Corresponding_Body (Decl)) | |
4101 | or else Nkind (Parent (Decl)) = N_Compilation_Unit | |
4102 | or else No (Next (Decl)) | |
4103 | then | |
4104 | null; | |
4105 | ||
4106 | else | |
4107 | -- The instantiation node usually follows the package | |
4108 | -- declaration for the instance. If the generic unit | |
4109 | -- has aspect specifications, they are transformed | |
4110 | -- into pragmas in the instance, and the instance node | |
4111 | -- appears after them. | |
4112 | ||
4113 | Inst_Node := Next (Decl); | |
4114 | ||
4115 | while Nkind (Inst_Node) /= N_Package_Instantiation loop | |
4116 | Inst_Node := Next (Inst_Node); | |
4117 | end loop; | |
4118 | ||
4119 | Add_Pending_Instantiation (Inst_Node, Decl); | |
4120 | end if; | |
4121 | end if; | |
4122 | end; | |
4123 | end if; | |
4124 | ||
4125 | -- Front end expansion of simple functions returning unconstrained | |
4126 | -- types (see Check_And_Split_Unconstrained_Function). Note that the | |
4127 | -- case of a simple renaming (Body_To_Inline in N_Entity above, see | |
4128 | -- also Build_Renamed_Body) cannot be expanded here because this may | |
4129 | -- give rise to order-of-elaboration issues for the types of the | |
4130 | -- parameters of the subprogram, if any. | |
4131 | ||
4132 | else | |
4133 | Expand_Inlined_Call (Call_Node, Subp, Orig_Subp); | |
4134 | end if; | |
4135 | end if; | |
4136 | ||
4137 | -- Check for protected subprogram. This is either an intra-object call, | |
4138 | -- or a protected function call. Protected procedure calls are rewritten | |
4139 | -- as entry calls and handled accordingly. | |
4140 | ||
4141 | -- In Ada 2005, this may be an indirect call to an access parameter that | |
4142 | -- is an access_to_subprogram. In that case the anonymous type has a | |
4143 | -- scope that is a protected operation, but the call is a regular one. | |
4144 | -- In either case do not expand call if subprogram is eliminated. | |
4145 | ||
4146 | Scop := Scope (Subp); | |
4147 | ||
4148 | if Nkind (Call_Node) /= N_Entry_Call_Statement | |
4149 | and then Is_Protected_Type (Scop) | |
4150 | and then Ekind (Subp) /= E_Subprogram_Type | |
4151 | and then not Is_Eliminated (Subp) | |
4152 | then | |
4153 | -- If the call is an internal one, it is rewritten as a call to the | |
4154 | -- corresponding unprotected subprogram. | |
4155 | ||
4156 | Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop); | |
4157 | end if; | |
4158 | ||
4159 | -- Functions returning controlled objects need special attention. If | |
4160 | -- the return type is limited, then the context is initialization and | |
4161 | -- different processing applies. If the call is to a protected function, | |
4162 | -- the expansion above will call Expand_Call recursively. Otherwise the | |
4163 | -- function call is transformed into a temporary which obtains the | |
4164 | -- result from the secondary stack. | |
4165 | ||
4166 | if Needs_Finalization (Etype (Subp)) then | |
4167 | if not Is_Limited_View (Etype (Subp)) | |
4168 | and then | |
4169 | (No (First_Formal (Subp)) | |
4170 | or else | |
4171 | not Is_Concurrent_Record_Type (Etype (First_Formal (Subp)))) | |
4172 | then | |
4173 | Expand_Ctrl_Function_Call (Call_Node); | |
4174 | ||
4175 | -- Build-in-place function calls which appear in anonymous contexts | |
4176 | -- need a transient scope to ensure the proper finalization of the | |
4177 | -- intermediate result after its use. | |
4178 | ||
4179 | elsif Is_Build_In_Place_Function_Call (Call_Node) | |
4180 | and then | |
4181 | Nkind_In (Parent (Call_Node), N_Attribute_Reference, | |
4182 | N_Function_Call, | |
4183 | N_Indexed_Component, | |
4184 | N_Object_Renaming_Declaration, | |
4185 | N_Procedure_Call_Statement, | |
4186 | N_Selected_Component, | |
4187 | N_Slice) | |
4188 | then | |
4189 | Establish_Transient_Scope (Call_Node, Sec_Stack => True); | |
4190 | end if; | |
4191 | end if; | |
4192 | end Expand_Call_Helper; | |
4193 | ||
4194 | ------------------------------- | |
4195 | -- Expand_Ctrl_Function_Call -- | |
4196 | ------------------------------- | |
4197 | ||
4198 | procedure Expand_Ctrl_Function_Call (N : Node_Id) is | |
4199 | function Is_Element_Reference (N : Node_Id) return Boolean; | |
4200 | -- Determine whether node N denotes a reference to an Ada 2012 container | |
4201 | -- element. | |
4202 | ||
4203 | -------------------------- | |
4204 | -- Is_Element_Reference -- | |
4205 | -------------------------- | |
4206 | ||
4207 | function Is_Element_Reference (N : Node_Id) return Boolean is | |
4208 | Ref : constant Node_Id := Original_Node (N); | |
4209 | ||
4210 | begin | |
4211 | -- Analysis marks an element reference by setting the generalized | |
4212 | -- indexing attribute of an indexed component before the component | |
4213 | -- is rewritten into a function call. | |
4214 | ||
4215 | return | |
4216 | Nkind (Ref) = N_Indexed_Component | |
4217 | and then Present (Generalized_Indexing (Ref)); | |
4218 | end Is_Element_Reference; | |
4219 | ||
4220 | -- Start of processing for Expand_Ctrl_Function_Call | |
4221 | ||
4222 | begin | |
4223 | -- Optimization, if the returned value (which is on the sec-stack) is | |
4224 | -- returned again, no need to copy/readjust/finalize, we can just pass | |
4225 | -- the value thru (see Expand_N_Simple_Return_Statement), and thus no | |
4226 | -- attachment is needed | |
4227 | ||
4228 | if Nkind (Parent (N)) = N_Simple_Return_Statement then | |
4229 | return; | |
4230 | end if; | |
4231 | ||
4232 | -- Resolution is now finished, make sure we don't start analysis again | |
4233 | -- because of the duplication. | |
4234 | ||
4235 | Set_Analyzed (N); | |
4236 | ||
4237 | -- A function which returns a controlled object uses the secondary | |
4238 | -- stack. Rewrite the call into a temporary which obtains the result of | |
4239 | -- the function using 'reference. | |
4240 | ||
4241 | Remove_Side_Effects (N); | |
4242 | ||
4243 | -- The side effect removal of the function call produced a temporary. | |
4244 | -- When the context is a case expression, if expression, or expression | |
4245 | -- with actions, the lifetime of the temporary must be extended to match | |
4246 | -- that of the context. Otherwise the function result will be finalized | |
4247 | -- too early and affect the result of the expression. To prevent this | |
4248 | -- unwanted effect, the temporary should not be considered for clean up | |
4249 | -- actions by the general finalization machinery. | |
4250 | ||
4251 | -- Exception to this rule are references to Ada 2012 container elements. | |
4252 | -- Such references must be finalized at the end of each iteration of the | |
4253 | -- related quantified expression, otherwise the container will remain | |
4254 | -- busy. | |
4255 | ||
4256 | if Nkind (N) = N_Explicit_Dereference | |
4257 | and then Within_Case_Or_If_Expression (N) | |
4258 | and then not Is_Element_Reference (N) | |
4259 | then | |
4260 | Set_Is_Ignored_Transient (Entity (Prefix (N))); | |
4261 | end if; | |
4262 | end Expand_Ctrl_Function_Call; | |
4263 | ||
4264 | ---------------------------------------- | |
4265 | -- Expand_N_Extended_Return_Statement -- | |
4266 | ---------------------------------------- | |
4267 | ||
4268 | -- If there is a Handled_Statement_Sequence, we rewrite this: | |
4269 | ||
4270 | -- return Result : T := <expression> do | |
4271 | -- <handled_seq_of_stms> | |
4272 | -- end return; | |
4273 | ||
4274 | -- to be: | |
4275 | ||
4276 | -- declare | |
4277 | -- Result : T := <expression>; | |
4278 | -- begin | |
4279 | -- <handled_seq_of_stms> | |
4280 | -- return Result; | |
4281 | -- end; | |
4282 | ||
4283 | -- Otherwise (no Handled_Statement_Sequence), we rewrite this: | |
4284 | ||
4285 | -- return Result : T := <expression>; | |
4286 | ||
4287 | -- to be: | |
4288 | ||
4289 | -- return <expression>; | |
4290 | ||
4291 | -- unless it's build-in-place or there's no <expression>, in which case | |
4292 | -- we generate: | |
4293 | ||
4294 | -- declare | |
4295 | -- Result : T := <expression>; | |
4296 | -- begin | |
4297 | -- return Result; | |
4298 | -- end; | |
4299 | ||
4300 | -- Note that this case could have been written by the user as an extended | |
4301 | -- return statement, or could have been transformed to this from a simple | |
4302 | -- return statement. | |
4303 | ||
4304 | -- That is, we need to have a reified return object if there are statements | |
4305 | -- (which might refer to it) or if we're doing build-in-place (so we can | |
4306 | -- set its address to the final resting place or if there is no expression | |
4307 | -- (in which case default initial values might need to be set). | |
4308 | ||
4309 | procedure Expand_N_Extended_Return_Statement (N : Node_Id) is | |
4310 | Loc : constant Source_Ptr := Sloc (N); | |
4311 | ||
4312 | function Build_Heap_Allocator | |
4313 | (Temp_Id : Entity_Id; | |
4314 | Temp_Typ : Entity_Id; | |
4315 | Func_Id : Entity_Id; | |
4316 | Ret_Typ : Entity_Id; | |
4317 | Alloc_Expr : Node_Id) return Node_Id; | |
4318 | -- Create the statements necessary to allocate a return object on the | |
4319 | -- caller's master. The master is available through implicit parameter | |
4320 | -- BIPfinalizationmaster. | |
4321 | -- | |
4322 | -- if BIPfinalizationmaster /= null then | |
4323 | -- declare | |
4324 | -- type Ptr_Typ is access Ret_Typ; | |
4325 | -- for Ptr_Typ'Storage_Pool use | |
4326 | -- Base_Pool (BIPfinalizationmaster.all).all; | |
4327 | -- Local : Ptr_Typ; | |
4328 | -- | |
4329 | -- begin | |
4330 | -- procedure Allocate (...) is | |
4331 | -- begin | |
4332 | -- System.Storage_Pools.Subpools.Allocate_Any (...); | |
4333 | -- end Allocate; | |
4334 | -- | |
4335 | -- Local := <Alloc_Expr>; | |
4336 | -- Temp_Id := Temp_Typ (Local); | |
4337 | -- end; | |
4338 | -- end if; | |
4339 | -- | |
4340 | -- Temp_Id is the temporary which is used to reference the internally | |
4341 | -- created object in all allocation forms. Temp_Typ is the type of the | |
4342 | -- temporary. Func_Id is the enclosing function. Ret_Typ is the return | |
4343 | -- type of Func_Id. Alloc_Expr is the actual allocator. | |
4344 | ||
4345 | function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id; | |
4346 | -- Construct a call to System.Tasking.Stages.Move_Activation_Chain | |
4347 | -- with parameters: | |
4348 | -- From current activation chain | |
4349 | -- To activation chain passed in by the caller | |
4350 | -- New_Master master passed in by the caller | |
4351 | -- | |
4352 | -- Func_Id is the entity of the function where the extended return | |
4353 | -- statement appears. | |
4354 | ||
4355 | -------------------------- | |
4356 | -- Build_Heap_Allocator -- | |
4357 | -------------------------- | |
4358 | ||
4359 | function Build_Heap_Allocator | |
4360 | (Temp_Id : Entity_Id; | |
4361 | Temp_Typ : Entity_Id; | |
4362 | Func_Id : Entity_Id; | |
4363 | Ret_Typ : Entity_Id; | |
4364 | Alloc_Expr : Node_Id) return Node_Id | |
4365 | is | |
4366 | begin | |
4367 | pragma Assert (Is_Build_In_Place_Function (Func_Id)); | |
4368 | ||
4369 | -- Processing for build-in-place object allocation. | |
4370 | ||
4371 | if Needs_Finalization (Ret_Typ) then | |
4372 | declare | |
4373 | Decls : constant List_Id := New_List; | |
4374 | Fin_Mas_Id : constant Entity_Id := | |
4375 | Build_In_Place_Formal | |
4376 | (Func_Id, BIP_Finalization_Master); | |
4377 | Stmts : constant List_Id := New_List; | |
4378 | Desig_Typ : Entity_Id; | |
4379 | Local_Id : Entity_Id; | |
4380 | Pool_Id : Entity_Id; | |
4381 | Ptr_Typ : Entity_Id; | |
4382 | ||
4383 | begin | |
4384 | -- Generate: | |
4385 | -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all; | |
4386 | ||
4387 | Pool_Id := Make_Temporary (Loc, 'P'); | |
4388 | ||
4389 | Append_To (Decls, | |
4390 | Make_Object_Renaming_Declaration (Loc, | |
4391 | Defining_Identifier => Pool_Id, | |
4392 | Subtype_Mark => | |
4393 | New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc), | |
4394 | Name => | |
4395 | Make_Explicit_Dereference (Loc, | |
4396 | Prefix => | |
4397 | Make_Function_Call (Loc, | |
4398 | Name => | |
4399 | New_Occurrence_Of (RTE (RE_Base_Pool), Loc), | |
4400 | Parameter_Associations => New_List ( | |
4401 | Make_Explicit_Dereference (Loc, | |
4402 | Prefix => | |
4403 | New_Occurrence_Of (Fin_Mas_Id, Loc))))))); | |
4404 | ||
4405 | -- Create an access type which uses the storage pool of the | |
4406 | -- caller's master. This additional type is necessary because | |
4407 | -- the finalization master cannot be associated with the type | |
4408 | -- of the temporary. Otherwise the secondary stack allocation | |
4409 | -- will fail. | |
4410 | ||
4411 | Desig_Typ := Ret_Typ; | |
4412 | ||
4413 | -- Ensure that the build-in-place machinery uses a fat pointer | |
4414 | -- when allocating an unconstrained array on the heap. In this | |
4415 | -- case the result object type is a constrained array type even | |
4416 | -- though the function type is unconstrained. | |
4417 | ||
4418 | if Ekind (Desig_Typ) = E_Array_Subtype then | |
4419 | Desig_Typ := Base_Type (Desig_Typ); | |
4420 | end if; | |
4421 | ||
4422 | -- Generate: | |
4423 | -- type Ptr_Typ is access Desig_Typ; | |
4424 | ||
4425 | Ptr_Typ := Make_Temporary (Loc, 'P'); | |
4426 | ||
4427 | Append_To (Decls, | |
4428 | Make_Full_Type_Declaration (Loc, | |
4429 | Defining_Identifier => Ptr_Typ, | |
4430 | Type_Definition => | |
4431 | Make_Access_To_Object_Definition (Loc, | |
4432 | Subtype_Indication => | |
4433 | New_Occurrence_Of (Desig_Typ, Loc)))); | |
4434 | ||
4435 | -- Perform minor decoration in order to set the master and the | |
4436 | -- storage pool attributes. | |
4437 | ||
4438 | Set_Ekind (Ptr_Typ, E_Access_Type); | |
4439 | Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id); | |
4440 | Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id); | |
4441 | ||
4442 | -- Create the temporary, generate: | |
4443 | -- Local_Id : Ptr_Typ; | |
4444 | ||
4445 | Local_Id := Make_Temporary (Loc, 'T'); | |
4446 | ||
4447 | Append_To (Decls, | |
4448 | Make_Object_Declaration (Loc, | |
4449 | Defining_Identifier => Local_Id, | |
4450 | Object_Definition => | |
4451 | New_Occurrence_Of (Ptr_Typ, Loc))); | |
4452 | ||
4453 | -- Allocate the object, generate: | |
4454 | -- Local_Id := <Alloc_Expr>; | |
4455 | ||
4456 | Append_To (Stmts, | |
4457 | Make_Assignment_Statement (Loc, | |
4458 | Name => New_Occurrence_Of (Local_Id, Loc), | |
4459 | Expression => Alloc_Expr)); | |
4460 | ||
4461 | -- Generate: | |
4462 | -- Temp_Id := Temp_Typ (Local_Id); | |
4463 | ||
4464 | Append_To (Stmts, | |
4465 | Make_Assignment_Statement (Loc, | |
4466 | Name => New_Occurrence_Of (Temp_Id, Loc), | |
4467 | Expression => | |
4468 | Unchecked_Convert_To (Temp_Typ, | |
4469 | New_Occurrence_Of (Local_Id, Loc)))); | |
4470 | ||
4471 | -- Wrap the allocation in a block. This is further conditioned | |
4472 | -- by checking the caller finalization master at runtime. A | |
4473 | -- null value indicates a non-existent master, most likely due | |
4474 | -- to a Finalize_Storage_Only allocation. | |
4475 | ||
4476 | -- Generate: | |
4477 | -- if BIPfinalizationmaster /= null then | |
4478 | -- declare | |
4479 | -- <Decls> | |
4480 | -- begin | |
4481 | -- <Stmts> | |
4482 | -- end; | |
4483 | -- end if; | |
4484 | ||
4485 | return | |
4486 | Make_If_Statement (Loc, | |
4487 | Condition => | |
4488 | Make_Op_Ne (Loc, | |
4489 | Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc), | |
4490 | Right_Opnd => Make_Null (Loc)), | |
4491 | ||
4492 | Then_Statements => New_List ( | |
4493 | Make_Block_Statement (Loc, | |
4494 | Declarations => Decls, | |
4495 | Handled_Statement_Sequence => | |
4496 | Make_Handled_Sequence_Of_Statements (Loc, | |
4497 | Statements => Stmts)))); | |
4498 | end; | |
4499 | ||
4500 | -- For all other cases, generate: | |
4501 | -- Temp_Id := <Alloc_Expr>; | |
4502 | ||
4503 | else | |
4504 | return | |
4505 | Make_Assignment_Statement (Loc, | |
4506 | Name => New_Occurrence_Of (Temp_Id, Loc), | |
4507 | Expression => Alloc_Expr); | |
4508 | end if; | |
4509 | end Build_Heap_Allocator; | |
4510 | ||
4511 | --------------------------- | |
4512 | -- Move_Activation_Chain -- | |
4513 | --------------------------- | |
4514 | ||
4515 | function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is | |
4516 | begin | |
4517 | return | |
4518 | Make_Procedure_Call_Statement (Loc, | |
4519 | Name => | |
4520 | New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc), | |
4521 | ||
4522 | Parameter_Associations => New_List ( | |
4523 | ||
4524 | -- Source chain | |
4525 | ||
4526 | Make_Attribute_Reference (Loc, | |
4527 | Prefix => Make_Identifier (Loc, Name_uChain), | |
4528 | Attribute_Name => Name_Unrestricted_Access), | |
4529 | ||
4530 | -- Destination chain | |
4531 | ||
4532 | New_Occurrence_Of | |
4533 | (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc), | |
4534 | ||
4535 | -- New master | |
4536 | ||
4537 | New_Occurrence_Of | |
4538 | (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc))); | |
4539 | end Move_Activation_Chain; | |
4540 | ||
4541 | -- Local variables | |
4542 | ||
4543 | Func_Id : constant Entity_Id := | |
4544 | Return_Applies_To (Return_Statement_Entity (N)); | |
4545 | Is_BIP_Func : constant Boolean := | |
4546 | Is_Build_In_Place_Function (Func_Id); | |
4547 | Ret_Obj_Id : constant Entity_Id := | |
4548 | First_Entity (Return_Statement_Entity (N)); | |
4549 | Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id); | |
4550 | Ret_Typ : constant Entity_Id := Etype (Func_Id); | |
4551 | ||
4552 | Exp : Node_Id; | |
4553 | HSS : Node_Id; | |
4554 | Result : Node_Id; | |
4555 | Return_Stmt : Node_Id; | |
4556 | Stmts : List_Id; | |
4557 | ||
4558 | -- Start of processing for Expand_N_Extended_Return_Statement | |
4559 | ||
4560 | begin | |
4561 | -- Given that functionality of interface thunks is simple (just displace | |
4562 | -- the pointer to the object) they are always handled by means of | |
4563 | -- simple return statements. | |
4564 | ||
4565 | pragma Assert (not Is_Thunk (Current_Scope)); | |
4566 | ||
4567 | if Nkind (Ret_Obj_Decl) = N_Object_Declaration then | |
4568 | Exp := Expression (Ret_Obj_Decl); | |
4569 | else | |
4570 | Exp := Empty; | |
4571 | end if; | |
4572 | ||
4573 | HSS := Handled_Statement_Sequence (N); | |
4574 | ||
4575 | -- If the returned object needs finalization actions, the function must | |
4576 | -- perform the appropriate cleanup should it fail to return. The state | |
4577 | -- of the function itself is tracked through a flag which is coupled | |
4578 | -- with the scope finalizer. There is one flag per each return object | |
4579 | -- in case of multiple returns. | |
4580 | ||
4581 | if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then | |
4582 | declare | |
4583 | Flag_Decl : Node_Id; | |
4584 | Flag_Id : Entity_Id; | |
4585 | Func_Bod : Node_Id; | |
4586 | ||
4587 | begin | |
4588 | -- Recover the function body | |
4589 | ||
4590 | Func_Bod := Unit_Declaration_Node (Func_Id); | |
4591 | ||
4592 | if Nkind (Func_Bod) = N_Subprogram_Declaration then | |
4593 | Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod))); | |
4594 | end if; | |
4595 | ||
4596 | -- Create a flag to track the function state | |
4597 | ||
4598 | Flag_Id := Make_Temporary (Loc, 'F'); | |
4599 | Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id); | |
4600 | ||
4601 | -- Insert the flag at the beginning of the function declarations, | |
4602 | -- generate: | |
4603 | -- Fnn : Boolean := False; | |
4604 | ||
4605 | Flag_Decl := | |
4606 | Make_Object_Declaration (Loc, | |
4607 | Defining_Identifier => Flag_Id, | |
4608 | Object_Definition => | |
4609 | New_Occurrence_Of (Standard_Boolean, Loc), | |
4610 | Expression => | |
4611 | New_Occurrence_Of (Standard_False, Loc)); | |
4612 | ||
4613 | Prepend_To (Declarations (Func_Bod), Flag_Decl); | |
4614 | Analyze (Flag_Decl); | |
4615 | end; | |
4616 | end if; | |
4617 | ||
4618 | -- Build a simple_return_statement that returns the return object when | |
4619 | -- there is a statement sequence, or no expression, or the result will | |
4620 | -- be built in place. Note however that we currently do this for all | |
4621 | -- composite cases, even though nonlimited composite results are not yet | |
4622 | -- built in place (though we plan to do so eventually). | |
4623 | ||
4624 | if Present (HSS) | |
4625 | or else Is_Composite_Type (Ret_Typ) | |
4626 | or else No (Exp) | |
4627 | then | |
4628 | if No (HSS) then | |
4629 | Stmts := New_List; | |
4630 | ||
4631 | -- If the extended return has a handled statement sequence, then wrap | |
4632 | -- it in a block and use the block as the first statement. | |
4633 | ||
4634 | else | |
4635 | Stmts := New_List ( | |
4636 | Make_Block_Statement (Loc, | |
4637 | Declarations => New_List, | |
4638 | Handled_Statement_Sequence => HSS)); | |
4639 | end if; | |
4640 | ||
4641 | -- If the result type contains tasks, we call Move_Activation_Chain. | |
4642 | -- Later, the cleanup code will call Complete_Master, which will | |
4643 | -- terminate any unactivated tasks belonging to the return statement | |
4644 | -- master. But Move_Activation_Chain updates their master to be that | |
4645 | -- of the caller, so they will not be terminated unless the return | |
4646 | -- statement completes unsuccessfully due to exception, abort, goto, | |
4647 | -- or exit. As a formality, we test whether the function requires the | |
4648 | -- result to be built in place, though that's necessarily true for | |
4649 | -- the case of result types with task parts. | |
4650 | ||
4651 | if Is_BIP_Func and then Has_Task (Ret_Typ) then | |
4652 | ||
4653 | -- The return expression is an aggregate for a complex type which | |
4654 | -- contains tasks. This particular case is left unexpanded since | |
4655 | -- the regular expansion would insert all temporaries and | |
4656 | -- initialization code in the wrong block. | |
4657 | ||
4658 | if Nkind (Exp) = N_Aggregate then | |
4659 | Expand_N_Aggregate (Exp); | |
4660 | end if; | |
4661 | ||
4662 | -- Do not move the activation chain if the return object does not | |
4663 | -- contain tasks. | |
4664 | ||
4665 | if Has_Task (Etype (Ret_Obj_Id)) then | |
4666 | Append_To (Stmts, Move_Activation_Chain (Func_Id)); | |
4667 | end if; | |
4668 | end if; | |
4669 | ||
4670 | -- Update the state of the function right before the object is | |
4671 | -- returned. | |
4672 | ||
4673 | if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then | |
4674 | declare | |
4675 | Flag_Id : constant Entity_Id := | |
4676 | Status_Flag_Or_Transient_Decl (Ret_Obj_Id); | |
4677 | ||
4678 | begin | |
4679 | -- Generate: | |
4680 | -- Fnn := True; | |
4681 | ||
4682 | Append_To (Stmts, | |
4683 | Make_Assignment_Statement (Loc, | |
4684 | Name => New_Occurrence_Of (Flag_Id, Loc), | |
4685 | Expression => New_Occurrence_Of (Standard_True, Loc))); | |
4686 | end; | |
4687 | end if; | |
4688 | ||
4689 | -- Build a simple_return_statement that returns the return object | |
4690 | ||
4691 | Return_Stmt := | |
4692 | Make_Simple_Return_Statement (Loc, | |
4693 | Expression => New_Occurrence_Of (Ret_Obj_Id, Loc)); | |
4694 | Append_To (Stmts, Return_Stmt); | |
4695 | ||
4696 | HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts); | |
4697 | end if; | |
4698 | ||
4699 | -- Case where we build a return statement block | |
4700 | ||
4701 | if Present (HSS) then | |
4702 | Result := | |
4703 | Make_Block_Statement (Loc, | |
4704 | Declarations => Return_Object_Declarations (N), | |
4705 | Handled_Statement_Sequence => HSS); | |
4706 | ||
4707 | -- We set the entity of the new block statement to be that of the | |
4708 | -- return statement. This is necessary so that various fields, such | |
4709 | -- as Finalization_Chain_Entity carry over from the return statement | |
4710 | -- to the block. Note that this block is unusual, in that its entity | |
4711 | -- is an E_Return_Statement rather than an E_Block. | |
4712 | ||
4713 | Set_Identifier | |
4714 | (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc)); | |
4715 | ||
4716 | -- If the object decl was already rewritten as a renaming, then we | |
4717 | -- don't want to do the object allocation and transformation of | |
4718 | -- the return object declaration to a renaming. This case occurs | |
4719 | -- when the return object is initialized by a call to another | |
4720 | -- build-in-place function, and that function is responsible for | |
4721 | -- the allocation of the return object. | |
4722 | ||
4723 | if Is_BIP_Func | |
4724 | and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration | |
4725 | then | |
4726 | pragma Assert | |
4727 | (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration | |
4728 | and then Is_Build_In_Place_Function_Call | |
4729 | (Expression (Original_Node (Ret_Obj_Decl)))); | |
4730 | ||
4731 | -- Return the build-in-place result by reference | |
4732 | ||
4733 | Set_By_Ref (Return_Stmt); | |
4734 | ||
4735 | elsif Is_BIP_Func then | |
4736 | ||
4737 | -- Locate the implicit access parameter associated with the | |
4738 | -- caller-supplied return object and convert the return | |
4739 | -- statement's return object declaration to a renaming of a | |
4740 | -- dereference of the access parameter. If the return object's | |
4741 | -- declaration includes an expression that has not already been | |
4742 | -- expanded as separate assignments, then add an assignment | |
4743 | -- statement to ensure the return object gets initialized. | |
4744 | ||
4745 | -- declare | |
4746 | -- Result : T [:= <expression>]; | |
4747 | -- begin | |
4748 | -- ... | |
4749 | ||
4750 | -- is converted to | |
4751 | ||
4752 | -- declare | |
4753 | -- Result : T renames FuncRA.all; | |
4754 | -- [Result := <expression;] | |
4755 | -- begin | |
4756 | -- ... | |
4757 | ||
4758 | declare | |
4759 | Ret_Obj_Expr : constant Node_Id := Expression (Ret_Obj_Decl); | |
4760 | Ret_Obj_Typ : constant Entity_Id := Etype (Ret_Obj_Id); | |
4761 | ||
4762 | Init_Assignment : Node_Id := Empty; | |
4763 | Obj_Acc_Formal : Entity_Id; | |
4764 | Obj_Acc_Deref : Node_Id; | |
4765 | Obj_Alloc_Formal : Entity_Id; | |
4766 | ||
4767 | begin | |
4768 | -- Build-in-place results must be returned by reference | |
4769 | ||
4770 | Set_By_Ref (Return_Stmt); | |
4771 | ||
4772 | -- Retrieve the implicit access parameter passed by the caller | |
4773 | ||
4774 | Obj_Acc_Formal := | |
4775 | Build_In_Place_Formal (Func_Id, BIP_Object_Access); | |
4776 | ||
4777 | -- If the return object's declaration includes an expression | |
4778 | -- and the declaration isn't marked as No_Initialization, then | |
4779 | -- we need to generate an assignment to the object and insert | |
4780 | -- it after the declaration before rewriting it as a renaming | |
4781 | -- (otherwise we'll lose the initialization). The case where | |
4782 | -- the result type is an interface (or class-wide interface) | |
4783 | -- is also excluded because the context of the function call | |
4784 | -- must be unconstrained, so the initialization will always | |
4785 | -- be done as part of an allocator evaluation (storage pool | |
4786 | -- or secondary stack), never to a constrained target object | |
4787 | -- passed in by the caller. Besides the assignment being | |
4788 | -- unneeded in this case, it avoids problems with trying to | |
4789 | -- generate a dispatching assignment when the return expression | |
4790 | -- is a nonlimited descendant of a limited interface (the | |
4791 | -- interface has no assignment operation). | |
4792 | ||
4793 | if Present (Ret_Obj_Expr) | |
4794 | and then not No_Initialization (Ret_Obj_Decl) | |
4795 | and then not Is_Interface (Ret_Obj_Typ) | |
4796 | then | |
4797 | Init_Assignment := | |
4798 | Make_Assignment_Statement (Loc, | |
4799 | Name => New_Occurrence_Of (Ret_Obj_Id, Loc), | |
4800 | Expression => Relocate_Node (Ret_Obj_Expr)); | |
4801 | ||
4802 | Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id)); | |
4803 | Set_Assignment_OK (Name (Init_Assignment)); | |
4804 | Set_No_Ctrl_Actions (Init_Assignment); | |
4805 | ||
4806 | Set_Parent (Name (Init_Assignment), Init_Assignment); | |
4807 | Set_Parent (Expression (Init_Assignment), Init_Assignment); | |
4808 | ||
4809 | Set_Expression (Ret_Obj_Decl, Empty); | |
4810 | ||
4811 | if Is_Class_Wide_Type (Etype (Ret_Obj_Id)) | |
4812 | and then not Is_Class_Wide_Type | |
4813 | (Etype (Expression (Init_Assignment))) | |
4814 | then | |
4815 | Rewrite (Expression (Init_Assignment), | |
4816 | Make_Type_Conversion (Loc, | |
4817 | Subtype_Mark => | |
4818 | New_Occurrence_Of (Etype (Ret_Obj_Id), Loc), | |
4819 | Expression => | |
4820 | Relocate_Node (Expression (Init_Assignment)))); | |
4821 | end if; | |
4822 | ||
4823 | -- In the case of functions where the calling context can | |
4824 | -- determine the form of allocation needed, initialization | |
4825 | -- is done with each part of the if statement that handles | |
4826 | -- the different forms of allocation (this is true for | |
4827 | -- unconstrained and tagged result subtypes). | |
4828 | ||
4829 | if Is_Constrained (Ret_Typ) | |
4830 | and then not Is_Tagged_Type (Underlying_Type (Ret_Typ)) | |
4831 | then | |
4832 | Insert_After (Ret_Obj_Decl, Init_Assignment); | |
4833 | end if; | |
4834 | end if; | |
4835 | ||
4836 | -- When the function's subtype is unconstrained, a run-time | |
4837 | -- test is needed to determine the form of allocation to use | |
4838 | -- for the return object. The function has an implicit formal | |
4839 | -- parameter indicating this. If the BIP_Alloc_Form formal has | |
4840 | -- the value one, then the caller has passed access to an | |
4841 | -- existing object for use as the return object. If the value | |
4842 | -- is two, then the return object must be allocated on the | |
4843 | -- secondary stack. Otherwise, the object must be allocated in | |
4844 | -- a storage pool (currently only supported for the global | |
4845 | -- heap, user-defined storage pools TBD ???). We generate an | |
4846 | -- if statement to test the implicit allocation formal and | |
4847 | -- initialize a local access value appropriately, creating | |
4848 | -- allocators in the secondary stack and global heap cases. | |
4849 | -- The special formal also exists and must be tested when the | |
4850 | -- function has a tagged result, even when the result subtype | |
4851 | -- is constrained, because in general such functions can be | |
4852 | -- called in dispatching contexts and must be handled similarly | |
4853 | -- to functions with a class-wide result. | |
4854 | ||
4855 | if not Is_Constrained (Ret_Typ) | |
4856 | or else Is_Tagged_Type (Underlying_Type (Ret_Typ)) | |
4857 | then | |
4858 | Obj_Alloc_Formal := | |
4859 | Build_In_Place_Formal (Func_Id, BIP_Alloc_Form); | |
4860 | ||
4861 | declare | |
4862 | Pool_Id : constant Entity_Id := | |
4863 | Make_Temporary (Loc, 'P'); | |
4864 | Alloc_Obj_Id : Entity_Id; | |
4865 | Alloc_Obj_Decl : Node_Id; | |
4866 | Alloc_If_Stmt : Node_Id; | |
4867 | Heap_Allocator : Node_Id; | |
4868 | Pool_Decl : Node_Id; | |
4869 | Pool_Allocator : Node_Id; | |
4870 | Ptr_Type_Decl : Node_Id; | |
4871 | Ref_Type : Entity_Id; | |
4872 | SS_Allocator : Node_Id; | |
4873 | ||
4874 | begin | |
4875 | -- Reuse the itype created for the function's implicit | |
4876 | -- access formal. This avoids the need to create a new | |
4877 | -- access type here, plus it allows assigning the access | |
4878 | -- formal directly without applying a conversion. | |
4879 | ||
4880 | -- Ref_Type := Etype (Object_Access); | |
4881 | ||
4882 | -- Create an access type designating the function's | |
4883 | -- result subtype. | |
4884 | ||
4885 | Ref_Type := Make_Temporary (Loc, 'A'); | |
4886 | ||
4887 | Ptr_Type_Decl := | |
4888 | Make_Full_Type_Declaration (Loc, | |
4889 | Defining_Identifier => Ref_Type, | |
4890 | Type_Definition => | |
4891 | Make_Access_To_Object_Definition (Loc, | |
4892 | All_Present => True, | |
4893 | Subtype_Indication => | |
4894 | New_Occurrence_Of (Ret_Obj_Typ, Loc))); | |
4895 | ||
4896 | Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl); | |
4897 | ||
4898 | -- Create an access object that will be initialized to an | |
4899 | -- access value denoting the return object, either coming | |
4900 | -- from an implicit access value passed in by the caller | |
4901 | -- or from the result of an allocator. | |
4902 | ||
4903 | Alloc_Obj_Id := Make_Temporary (Loc, 'R'); | |
4904 | Set_Etype (Alloc_Obj_Id, Ref_Type); | |
4905 | ||
4906 | Alloc_Obj_Decl := | |
4907 | Make_Object_Declaration (Loc, | |
4908 | Defining_Identifier => Alloc_Obj_Id, | |
4909 | Object_Definition => | |
4910 | New_Occurrence_Of (Ref_Type, Loc)); | |
4911 | ||
4912 | Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl); | |
4913 | ||
4914 | -- Create allocators for both the secondary stack and | |
4915 | -- global heap. If there's an initialization expression, | |
4916 | -- then create these as initialized allocators. | |
4917 | ||
4918 | if Present (Ret_Obj_Expr) | |
4919 | and then not No_Initialization (Ret_Obj_Decl) | |
4920 | then | |
4921 | -- Always use the type of the expression for the | |
4922 | -- qualified expression, rather than the result type. | |
4923 | -- In general we cannot always use the result type | |
4924 | -- for the allocator, because the expression might be | |
4925 | -- of a specific type, such as in the case of an | |
4926 | -- aggregate or even a nonlimited object when the | |
4927 | -- result type is a limited class-wide interface type. | |
4928 | ||
4929 | Heap_Allocator := | |
4930 | Make_Allocator (Loc, | |
4931 | Expression => | |
4932 | Make_Qualified_Expression (Loc, | |
4933 | Subtype_Mark => | |
4934 | New_Occurrence_Of | |
4935 | (Etype (Ret_Obj_Expr), Loc), | |
4936 | Expression => New_Copy_Tree (Ret_Obj_Expr))); | |
4937 | ||
4938 | else | |
4939 | -- If the function returns a class-wide type we cannot | |
4940 | -- use the return type for the allocator. Instead we | |
4941 | -- use the type of the expression, which must be an | |
4942 | -- aggregate of a definite type. | |
4943 | ||
4944 | if Is_Class_Wide_Type (Ret_Obj_Typ) then | |
4945 | Heap_Allocator := | |
4946 | Make_Allocator (Loc, | |
4947 | Expression => | |
4948 | New_Occurrence_Of | |
4949 | (Etype (Ret_Obj_Expr), Loc)); | |
4950 | else | |
4951 | Heap_Allocator := | |
4952 | Make_Allocator (Loc, | |
4953 | Expression => | |
4954 | New_Occurrence_Of (Ret_Obj_Typ, Loc)); | |
4955 | end if; | |
4956 | ||
4957 | -- If the object requires default initialization then | |
4958 | -- that will happen later following the elaboration of | |
4959 | -- the object renaming. If we don't turn it off here | |
4960 | -- then the object will be default initialized twice. | |
4961 | ||
4962 | Set_No_Initialization (Heap_Allocator); | |
4963 | end if; | |
4964 | ||
4965 | -- The Pool_Allocator is just like the Heap_Allocator, | |
4966 | -- except we set Storage_Pool and Procedure_To_Call so | |
4967 | -- it will use the user-defined storage pool. | |
4968 | ||
4969 | Pool_Allocator := New_Copy_Tree (Heap_Allocator); | |
4970 | ||
4971 | -- Do not generate the renaming of the build-in-place | |
4972 | -- pool parameter on ZFP because the parameter is not | |
4973 | -- created in the first place. | |
4974 | ||
4975 | if RTE_Available (RE_Root_Storage_Pool_Ptr) then | |
4976 | Pool_Decl := | |
4977 | Make_Object_Renaming_Declaration (Loc, | |
4978 | Defining_Identifier => Pool_Id, | |
4979 | Subtype_Mark => | |
4980 | New_Occurrence_Of | |
4981 | (RTE (RE_Root_Storage_Pool), Loc), | |
4982 | Name => | |
4983 | Make_Explicit_Dereference (Loc, | |
4984 | New_Occurrence_Of | |
4985 | (Build_In_Place_Formal | |
4986 | (Func_Id, BIP_Storage_Pool), Loc))); | |
4987 | Set_Storage_Pool (Pool_Allocator, Pool_Id); | |
4988 | Set_Procedure_To_Call | |
4989 | (Pool_Allocator, RTE (RE_Allocate_Any)); | |
4990 | else | |
4991 | Pool_Decl := Make_Null_Statement (Loc); | |
4992 | end if; | |
4993 | ||
4994 | -- If the No_Allocators restriction is active, then only | |
4995 | -- an allocator for secondary stack allocation is needed. | |
4996 | -- It's OK for such allocators to have Comes_From_Source | |
4997 | -- set to False, because gigi knows not to flag them as | |
4998 | -- being a violation of No_Implicit_Heap_Allocations. | |
4999 | ||
5000 | if Restriction_Active (No_Allocators) then | |
5001 | SS_Allocator := Heap_Allocator; | |
5002 | Heap_Allocator := Make_Null (Loc); | |
5003 | Pool_Allocator := Make_Null (Loc); | |
5004 | ||
5005 | -- Otherwise the heap and pool allocators may be needed, | |
5006 | -- so we make another allocator for secondary stack | |
5007 | -- allocation. | |
5008 | ||
5009 | else | |
5010 | SS_Allocator := New_Copy_Tree (Heap_Allocator); | |
5011 | ||
5012 | -- The heap and pool allocators are marked as | |
5013 | -- Comes_From_Source since they correspond to an | |
5014 | -- explicit user-written allocator (that is, it will | |
5015 | -- only be executed on behalf of callers that call the | |
5016 | -- function as initialization for such an allocator). | |
5017 | -- Prevents errors when No_Implicit_Heap_Allocations | |
5018 | -- is in force. | |
5019 | ||
5020 | Set_Comes_From_Source (Heap_Allocator, True); | |
5021 | Set_Comes_From_Source (Pool_Allocator, True); | |
5022 | end if; | |
5023 | ||
5024 | -- The allocator is returned on the secondary stack. | |
5025 | ||
5026 | Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool)); | |
5027 | Set_Procedure_To_Call | |
5028 | (SS_Allocator, RTE (RE_SS_Allocate)); | |
5029 | ||
5030 | -- The allocator is returned on the secondary stack, | |
5031 | -- so indicate that the function return, as well as | |
5032 | -- all blocks that encloses the allocator, must not | |
5033 | -- release it. The flags must be set now because | |
5034 | -- the decision to use the secondary stack is done | |
5035 | -- very late in the course of expanding the return | |
5036 | -- statement, past the point where these flags are | |
5037 | -- normally set. | |
5038 | ||
5039 | Set_Uses_Sec_Stack (Func_Id); | |
5040 | Set_Uses_Sec_Stack (Return_Statement_Entity (N)); | |
5041 | Set_Sec_Stack_Needed_For_Return | |
5042 | (Return_Statement_Entity (N)); | |
5043 | Set_Enclosing_Sec_Stack_Return (N); | |
5044 | ||
5045 | -- Create an if statement to test the BIP_Alloc_Form | |
5046 | -- formal and initialize the access object to either the | |
5047 | -- BIP_Object_Access formal (BIP_Alloc_Form = | |
5048 | -- Caller_Allocation), the result of allocating the | |
5049 | -- object in the secondary stack (BIP_Alloc_Form = | |
5050 | -- Secondary_Stack), or else an allocator to create the | |
5051 | -- return object in the heap or user-defined pool | |
5052 | -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool). | |
5053 | ||
5054 | -- ??? An unchecked type conversion must be made in the | |
5055 | -- case of assigning the access object formal to the | |
5056 | -- local access object, because a normal conversion would | |
5057 | -- be illegal in some cases (such as converting access- | |
5058 | -- to-unconstrained to access-to-constrained), but the | |
5059 | -- the unchecked conversion will presumably fail to work | |
5060 | -- right in just such cases. It's not clear at all how to | |
5061 | -- handle this. ??? | |
5062 | ||
5063 | Alloc_If_Stmt := | |
5064 | Make_If_Statement (Loc, | |
5065 | Condition => | |
5066 | Make_Op_Eq (Loc, | |
5067 | Left_Opnd => | |
5068 | New_Occurrence_Of (Obj_Alloc_Formal, Loc), | |
5069 | Right_Opnd => | |
5070 | Make_Integer_Literal (Loc, | |
5071 | UI_From_Int (BIP_Allocation_Form'Pos | |
5072 | (Caller_Allocation)))), | |
5073 | ||
5074 | Then_Statements => New_List ( | |
5075 | Make_Assignment_Statement (Loc, | |
5076 | Name => | |
5077 | New_Occurrence_Of (Alloc_Obj_Id, Loc), | |
5078 | Expression => | |
5079 | Make_Unchecked_Type_Conversion (Loc, | |
5080 | Subtype_Mark => | |
5081 | New_Occurrence_Of (Ref_Type, Loc), | |
5082 | Expression => | |
5083 | New_Occurrence_Of (Obj_Acc_Formal, Loc)))), | |
5084 | ||
5085 | Elsif_Parts => New_List ( | |
5086 | Make_Elsif_Part (Loc, | |
5087 | Condition => | |
5088 | Make_Op_Eq (Loc, | |
5089 | Left_Opnd => | |
5090 | New_Occurrence_Of (Obj_Alloc_Formal, Loc), | |
5091 | Right_Opnd => | |
5092 | Make_Integer_Literal (Loc, | |
5093 | UI_From_Int (BIP_Allocation_Form'Pos | |
5094 | (Secondary_Stack)))), | |
5095 | ||
5096 | Then_Statements => New_List ( | |
5097 | Make_Assignment_Statement (Loc, | |
5098 | Name => | |
5099 | New_Occurrence_Of (Alloc_Obj_Id, Loc), | |
5100 | Expression => SS_Allocator))), | |
5101 | ||
5102 | Make_Elsif_Part (Loc, | |
5103 | Condition => | |
5104 | Make_Op_Eq (Loc, | |
5105 | Left_Opnd => | |
5106 | New_Occurrence_Of (Obj_Alloc_Formal, Loc), | |
5107 | Right_Opnd => | |
5108 | Make_Integer_Literal (Loc, | |
5109 | UI_From_Int (BIP_Allocation_Form'Pos | |
5110 | (Global_Heap)))), | |
5111 | ||
5112 | Then_Statements => New_List ( | |
5113 | Build_Heap_Allocator | |
5114 | (Temp_Id => Alloc_Obj_Id, | |
5115 | Temp_Typ => Ref_Type, | |
5116 | Func_Id => Func_Id, | |
5117 | Ret_Typ => Ret_Obj_Typ, | |
5118 | Alloc_Expr => Heap_Allocator)))), | |
5119 | ||
5120 | Else_Statements => New_List ( | |
5121 | Pool_Decl, | |
5122 | Build_Heap_Allocator | |
5123 | (Temp_Id => Alloc_Obj_Id, | |
5124 | Temp_Typ => Ref_Type, | |
5125 | Func_Id => Func_Id, | |
5126 | Ret_Typ => Ret_Obj_Typ, | |
5127 | Alloc_Expr => Pool_Allocator))); | |
5128 | ||
5129 | -- If a separate initialization assignment was created | |
5130 | -- earlier, append that following the assignment of the | |
5131 | -- implicit access formal to the access object, to ensure | |
5132 | -- that the return object is initialized in that case. In | |
5133 | -- this situation, the target of the assignment must be | |
5134 | -- rewritten to denote a dereference of the access to the | |
5135 | -- return object passed in by the caller. | |
5136 | ||
5137 | if Present (Init_Assignment) then | |
5138 | Rewrite (Name (Init_Assignment), | |
5139 | Make_Explicit_Dereference (Loc, | |
5140 | Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc))); | |
5141 | ||
5142 | Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id)); | |
5143 | ||
5144 | Append_To | |
5145 | (Then_Statements (Alloc_If_Stmt), Init_Assignment); | |
5146 | end if; | |
5147 | ||
5148 | Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt); | |
5149 | ||
5150 | -- Remember the local access object for use in the | |
5151 | -- dereference of the renaming created below. | |
5152 | ||
5153 | Obj_Acc_Formal := Alloc_Obj_Id; | |
5154 | end; | |
5155 | end if; | |
5156 | ||
5157 | -- Replace the return object declaration with a renaming of a | |
5158 | -- dereference of the access value designating the return | |
5159 | -- object. | |
5160 | ||
5161 | Obj_Acc_Deref := | |
5162 | Make_Explicit_Dereference (Loc, | |
5163 | Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc)); | |
5164 | ||
5165 | Rewrite (Ret_Obj_Decl, | |
5166 | Make_Object_Renaming_Declaration (Loc, | |
5167 | Defining_Identifier => Ret_Obj_Id, | |
5168 | Access_Definition => Empty, | |
5169 | Subtype_Mark => New_Occurrence_Of (Ret_Obj_Typ, Loc), | |
5170 | Name => Obj_Acc_Deref)); | |
5171 | ||
5172 | Set_Renamed_Object (Ret_Obj_Id, Obj_Acc_Deref); | |
5173 | end; | |
5174 | end if; | |
5175 | ||
5176 | -- Case where we do not build a block | |
5177 | ||
5178 | else | |
5179 | -- We're about to drop Return_Object_Declarations on the floor, so | |
5180 | -- we need to insert it, in case it got expanded into useful code. | |
5181 | -- Remove side effects from expression, which may be duplicated in | |
5182 | -- subsequent checks (see Expand_Simple_Function_Return). | |
5183 | ||
5184 | Insert_List_Before (N, Return_Object_Declarations (N)); | |
5185 | Remove_Side_Effects (Exp); | |
5186 | ||
5187 | -- Build simple_return_statement that returns the expression directly | |
5188 | ||
5189 | Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp); | |
5190 | Result := Return_Stmt; | |
5191 | end if; | |
5192 | ||
5193 | -- Set the flag to prevent infinite recursion | |
5194 | ||
5195 | Set_Comes_From_Extended_Return_Statement (Return_Stmt); | |
5196 | ||
5197 | Rewrite (N, Result); | |
5198 | Analyze (N); | |
5199 | end Expand_N_Extended_Return_Statement; | |
5200 | ||
5201 | ---------------------------- | |
5202 | -- Expand_N_Function_Call -- | |
5203 | ---------------------------- | |
5204 | ||
5205 | procedure Expand_N_Function_Call (N : Node_Id) is | |
5206 | begin | |
5207 | Expand_Call (N); | |
5208 | end Expand_N_Function_Call; | |
5209 | ||
5210 | --------------------------------------- | |
5211 | -- Expand_N_Procedure_Call_Statement -- | |
5212 | --------------------------------------- | |
5213 | ||
5214 | procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is | |
5215 | begin | |
5216 | Expand_Call (N); | |
5217 | end Expand_N_Procedure_Call_Statement; | |
5218 | ||
5219 | -------------------------------------- | |
5220 | -- Expand_N_Simple_Return_Statement -- | |
5221 | -------------------------------------- | |
5222 | ||
5223 | procedure Expand_N_Simple_Return_Statement (N : Node_Id) is | |
5224 | begin | |
5225 | -- Defend against previous errors (i.e. the return statement calls a | |
5226 | -- function that is not available in configurable runtime). | |
5227 | ||
5228 | if Present (Expression (N)) | |
5229 | and then Nkind (Expression (N)) = N_Empty | |
5230 | then | |
5231 | Check_Error_Detected; | |
5232 | return; | |
5233 | end if; | |
5234 | ||
5235 | -- Distinguish the function and non-function cases: | |
5236 | ||
5237 | case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is | |
5238 | when E_Function | |
5239 | | E_Generic_Function | |
5240 | => | |
5241 | Expand_Simple_Function_Return (N); | |
5242 | ||
5243 | when E_Entry | |
5244 | | E_Entry_Family | |
5245 | | E_Generic_Procedure | |
5246 | | E_Procedure | |
5247 | | E_Return_Statement | |
5248 | => | |
5249 | Expand_Non_Function_Return (N); | |
5250 | ||
5251 | when others => | |
5252 | raise Program_Error; | |
5253 | end case; | |
5254 | ||
5255 | exception | |
5256 | when RE_Not_Available => | |
5257 | return; | |
5258 | end Expand_N_Simple_Return_Statement; | |
5259 | ||
5260 | ------------------------------ | |
5261 | -- Expand_N_Subprogram_Body -- | |
5262 | ------------------------------ | |
5263 | ||
5264 | -- Add poll call if ATC polling is enabled, unless the body will be inlined | |
5265 | -- by the back-end. | |
5266 | ||
5267 | -- Add dummy push/pop label nodes at start and end to clear any local | |
5268 | -- exception indications if local-exception-to-goto optimization is active. | |
5269 | ||
5270 | -- Add return statement if last statement in body is not a return statement | |
5271 | -- (this makes things easier on Gigi which does not want to have to handle | |
5272 | -- a missing return). | |
5273 | ||
5274 | -- Add call to Activate_Tasks if body is a task activator | |
5275 | ||
5276 | -- Deal with possible detection of infinite recursion | |
5277 | ||
5278 | -- Eliminate body completely if convention stubbed | |
5279 | ||
5280 | -- Encode entity names within body, since we will not need to reference | |
5281 | -- these entities any longer in the front end. | |
5282 | ||
5283 | -- Initialize scalar out parameters if Initialize/Normalize_Scalars | |
5284 | ||
5285 | -- Reset Pure indication if any parameter has root type System.Address | |
5286 | -- or has any parameters of limited types, where limited means that the | |
5287 | -- run-time view is limited (i.e. the full type is limited). | |
5288 | ||
5289 | -- Wrap thread body | |
5290 | ||
5291 | procedure Expand_N_Subprogram_Body (N : Node_Id) is | |
5292 | Body_Id : constant Entity_Id := Defining_Entity (N); | |
5293 | HSS : constant Node_Id := Handled_Statement_Sequence (N); | |
5294 | Loc : constant Source_Ptr := Sloc (N); | |
5295 | ||
5296 | procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id); | |
5297 | -- Append a return statement to the statement sequence Stmts if the last | |
5298 | -- statement is not already a return or a goto statement. Note that the | |
5299 | -- latter test is not critical, it does not matter if we add a few extra | |
5300 | -- returns, since they get eliminated anyway later on. Spec_Id denotes | |
5301 | -- the corresponding spec of the subprogram body. | |
5302 | ||
5303 | ---------------- | |
5304 | -- Add_Return -- | |
5305 | ---------------- | |
5306 | ||
5307 | procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is | |
5308 | Last_Stmt : Node_Id; | |
5309 | Loc : Source_Ptr; | |
5310 | Stmt : Node_Id; | |
5311 | ||
5312 | begin | |
5313 | -- Get last statement, ignoring any Pop_xxx_Label nodes, which are | |
5314 | -- not relevant in this context since they are not executable. | |
5315 | ||
5316 | Last_Stmt := Last (Stmts); | |
5317 | while Nkind (Last_Stmt) in N_Pop_xxx_Label loop | |
5318 | Prev (Last_Stmt); | |
5319 | end loop; | |
5320 | ||
5321 | -- Now insert return unless last statement is a transfer | |
5322 | ||
5323 | if not Is_Transfer (Last_Stmt) then | |
5324 | ||
5325 | -- The source location for the return is the end label of the | |
5326 | -- procedure if present. Otherwise use the sloc of the last | |
5327 | -- statement in the list. If the list comes from a generated | |
5328 | -- exception handler and we are not debugging generated code, | |
5329 | -- all the statements within the handler are made invisible | |
5330 | -- to the debugger. | |
5331 | ||
5332 | if Nkind (Parent (Stmts)) = N_Exception_Handler | |
5333 | and then not Comes_From_Source (Parent (Stmts)) | |
5334 | then | |
5335 | Loc := Sloc (Last_Stmt); | |
5336 | elsif Present (End_Label (HSS)) then | |
5337 | Loc := Sloc (End_Label (HSS)); | |
5338 | else | |
5339 | Loc := Sloc (Last_Stmt); | |
5340 | end if; | |
5341 | ||
5342 | -- Append return statement, and set analyzed manually. We can't | |
5343 | -- call Analyze on this return since the scope is wrong. | |
5344 | ||
5345 | -- Note: it almost works to push the scope and then do the Analyze | |
5346 | -- call, but something goes wrong in some weird cases and it is | |
5347 | -- not worth worrying about ??? | |
5348 | ||
5349 | Stmt := Make_Simple_Return_Statement (Loc); | |
5350 | ||
5351 | -- The return statement is handled properly, and the call to the | |
5352 | -- postcondition, inserted below, does not require information | |
5353 | -- from the body either. However, that call is analyzed in the | |
5354 | -- enclosing scope, and an elaboration check might improperly be | |
5355 | -- added to it. A guard in Sem_Elab is needed to prevent that | |
5356 | -- spurious check, see Check_Elab_Call. | |
5357 | ||
5358 | Append_To (Stmts, Stmt); | |
5359 | Set_Analyzed (Stmt); | |
5360 | ||
5361 | -- Call the _Postconditions procedure if the related subprogram | |
5362 | -- has contract assertions that need to be verified on exit. | |
5363 | ||
5364 | if Ekind (Spec_Id) = E_Procedure | |
5365 | and then Present (Postconditions_Proc (Spec_Id)) | |
5366 | then | |
5367 | Insert_Action (Stmt, | |
5368 | Make_Procedure_Call_Statement (Loc, | |
5369 | Name => | |
5370 | New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc))); | |
5371 | end if; | |
5372 | end if; | |
5373 | end Add_Return; | |
5374 | ||
5375 | -- Local variables | |
5376 | ||
5377 | Except_H : Node_Id; | |
5378 | L : List_Id; | |
5379 | Spec_Id : Entity_Id; | |
5380 | ||
5381 | -- Start of processing for Expand_N_Subprogram_Body | |
5382 | ||
5383 | begin | |
5384 | if Present (Corresponding_Spec (N)) then | |
5385 | Spec_Id := Corresponding_Spec (N); | |
5386 | else | |
5387 | Spec_Id := Body_Id; | |
5388 | end if; | |
5389 | ||
5390 | -- If this is a Pure function which has any parameters whose root type | |
5391 | -- is System.Address, reset the Pure indication. | |
5392 | -- This check is also performed when the subprogram is frozen, but we | |
5393 | -- repeat it on the body so that the indication is consistent, and so | |
5394 | -- it applies as well to bodies without separate specifications. | |
5395 | ||
5396 | if Is_Pure (Spec_Id) | |
5397 | and then Is_Subprogram (Spec_Id) | |
5398 | and then not Has_Pragma_Pure_Function (Spec_Id) | |
5399 | then | |
5400 | Check_Function_With_Address_Parameter (Spec_Id); | |
5401 | ||
5402 | if Spec_Id /= Body_Id then | |
5403 | Set_Is_Pure (Body_Id, Is_Pure (Spec_Id)); | |
5404 | end if; | |
5405 | end if; | |
5406 | ||
5407 | -- Set L to either the list of declarations if present, or to the list | |
5408 | -- of statements if no declarations are present. This is used to insert | |
5409 | -- new stuff at the start. | |
5410 | ||
5411 | if Is_Non_Empty_List (Declarations (N)) then | |
5412 | L := Declarations (N); | |
5413 | else | |
5414 | L := Statements (HSS); | |
5415 | end if; | |
5416 | ||
5417 | -- If local-exception-to-goto optimization active, insert dummy push | |
5418 | -- statements at start, and dummy pop statements at end, but inhibit | |
5419 | -- this if we have No_Exception_Handlers, since they are useless and | |
5420 | -- intefere with analysis, e.g. by codepeer. | |
5421 | ||
5422 | if (Debug_Flag_Dot_G | |
5423 | or else Restriction_Active (No_Exception_Propagation)) | |
5424 | and then not Restriction_Active (No_Exception_Handlers) | |
5425 | and then not CodePeer_Mode | |
5426 | and then Is_Non_Empty_List (L) | |
5427 | then | |
5428 | declare | |
5429 | FS : constant Node_Id := First (L); | |
5430 | FL : constant Source_Ptr := Sloc (FS); | |
5431 | LS : Node_Id; | |
5432 | LL : Source_Ptr; | |
5433 | ||
5434 | begin | |
5435 | -- LS points to either last statement, if statements are present | |
5436 | -- or to the last declaration if there are no statements present. | |
5437 | -- It is the node after which the pop's are generated. | |
5438 | ||
5439 | if Is_Non_Empty_List (Statements (HSS)) then | |
5440 | LS := Last (Statements (HSS)); | |
5441 | else | |
5442 | LS := Last (L); | |
5443 | end if; | |
5444 | ||
5445 | LL := Sloc (LS); | |
5446 | ||
5447 | Insert_List_Before_And_Analyze (FS, New_List ( | |
5448 | Make_Push_Constraint_Error_Label (FL), | |
5449 | Make_Push_Program_Error_Label (FL), | |
5450 | Make_Push_Storage_Error_Label (FL))); | |
5451 | ||
5452 | Insert_List_After_And_Analyze (LS, New_List ( | |
5453 | Make_Pop_Constraint_Error_Label (LL), | |
5454 | Make_Pop_Program_Error_Label (LL), | |
5455 | Make_Pop_Storage_Error_Label (LL))); | |
5456 | end; | |
5457 | end if; | |
5458 | ||
5459 | -- Need poll on entry to subprogram if polling enabled. We only do this | |
5460 | -- for non-empty subprograms, since it does not seem necessary to poll | |
5461 | -- for a dummy null subprogram. | |
5462 | ||
5463 | if Is_Non_Empty_List (L) then | |
5464 | ||
5465 | -- Do not add a polling call if the subprogram is to be inlined by | |
5466 | -- the back-end, to avoid repeated calls with multiple inlinings. | |
5467 | ||
5468 | if Is_Inlined (Spec_Id) | |
5469 | and then Front_End_Inlining | |
5470 | and then Optimization_Level > 1 | |
5471 | then | |
5472 | null; | |
5473 | else | |
5474 | Generate_Poll_Call (First (L)); | |
5475 | end if; | |
5476 | end if; | |
5477 | ||
5478 | -- Initialize any scalar OUT args if Initialize/Normalize_Scalars | |
5479 | ||
5480 | if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then | |
5481 | declare | |
5482 | F : Entity_Id; | |
5483 | A : Node_Id; | |
5484 | ||
5485 | begin | |
5486 | -- Loop through formals | |
5487 | ||
5488 | F := First_Formal (Spec_Id); | |
5489 | while Present (F) loop | |
5490 | if Is_Scalar_Type (Etype (F)) | |
5491 | and then Ekind (F) = E_Out_Parameter | |
5492 | then | |
5493 | Check_Restriction (No_Default_Initialization, F); | |
5494 | ||
5495 | -- Insert the initialization. We turn off validity checks | |
5496 | -- for this assignment, since we do not want any check on | |
5497 | -- the initial value itself (which may well be invalid). | |
5498 | -- Predicate checks are disabled as well (RM 6.4.1 (13/3)) | |
5499 | ||
5500 | A := | |
5501 | Make_Assignment_Statement (Loc, | |
5502 | Name => New_Occurrence_Of (F, Loc), | |
5503 | Expression => Get_Simple_Init_Val (Etype (F), N)); | |
5504 | Set_Suppress_Assignment_Checks (A); | |
5505 | ||
5506 | Insert_Before_And_Analyze (First (L), | |
5507 | A, Suppress => Validity_Check); | |
5508 | end if; | |
5509 | ||
5510 | Next_Formal (F); | |
5511 | end loop; | |
5512 | end; | |
5513 | end if; | |
5514 | ||
5515 | -- Clear out statement list for stubbed procedure | |
5516 | ||
5517 | if Present (Corresponding_Spec (N)) then | |
5518 | Set_Elaboration_Flag (N, Spec_Id); | |
5519 | ||
5520 | if Convention (Spec_Id) = Convention_Stubbed | |
5521 | or else Is_Eliminated (Spec_Id) | |
5522 | then | |
5523 | Set_Declarations (N, Empty_List); | |
5524 | Set_Handled_Statement_Sequence (N, | |
5525 | Make_Handled_Sequence_Of_Statements (Loc, | |
5526 | Statements => New_List (Make_Null_Statement (Loc)))); | |
5527 | ||
5528 | return; | |
5529 | end if; | |
5530 | end if; | |
5531 | ||
5532 | -- Create a set of discriminals for the next protected subprogram body | |
5533 | ||
5534 | if Is_List_Member (N) | |
5535 | and then Present (Parent (List_Containing (N))) | |
5536 | and then Nkind (Parent (List_Containing (N))) = N_Protected_Body | |
5537 | and then Present (Next_Protected_Operation (N)) | |
5538 | then | |
5539 | Set_Discriminals (Parent (Base_Type (Scope (Spec_Id)))); | |
5540 | end if; | |
5541 | ||
5542 | -- Returns_By_Ref flag is normally set when the subprogram is frozen but | |
5543 | -- subprograms with no specs are not frozen. | |
5544 | ||
5545 | declare | |
5546 | Typ : constant Entity_Id := Etype (Spec_Id); | |
5547 | Utyp : constant Entity_Id := Underlying_Type (Typ); | |
5548 | ||
5549 | begin | |
5550 | if Is_Limited_View (Typ) then | |
5551 | Set_Returns_By_Ref (Spec_Id); | |
5552 | ||
5553 | elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then | |
5554 | Set_Returns_By_Ref (Spec_Id); | |
5555 | end if; | |
5556 | end; | |
5557 | ||
5558 | -- For a procedure, we add a return for all possible syntactic ends of | |
5559 | -- the subprogram. | |
5560 | ||
5561 | if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then | |
5562 | Add_Return (Spec_Id, Statements (HSS)); | |
5563 | ||
5564 | if Present (Exception_Handlers (HSS)) then | |
5565 | Except_H := First_Non_Pragma (Exception_Handlers (HSS)); | |
5566 | while Present (Except_H) loop | |
5567 | Add_Return (Spec_Id, Statements (Except_H)); | |
5568 | Next_Non_Pragma (Except_H); | |
5569 | end loop; | |
5570 | end if; | |
5571 | ||
5572 | -- For a function, we must deal with the case where there is at least | |
5573 | -- one missing return. What we do is to wrap the entire body of the | |
5574 | -- function in a block: | |
5575 | ||
5576 | -- begin | |
5577 | -- ... | |
5578 | -- end; | |
5579 | ||
5580 | -- becomes | |
5581 | ||
5582 | -- begin | |
5583 | -- begin | |
5584 | -- ... | |
5585 | -- end; | |
5586 | ||
5587 | -- raise Program_Error; | |
5588 | -- end; | |
5589 | ||
5590 | -- This approach is necessary because the raise must be signalled to the | |
5591 | -- caller, not handled by any local handler (RM 6.4(11)). | |
5592 | ||
5593 | -- Note: we do not need to analyze the constructed sequence here, since | |
5594 | -- it has no handler, and an attempt to analyze the handled statement | |
5595 | -- sequence twice is risky in various ways (e.g. the issue of expanding | |
5596 | -- cleanup actions twice). | |
5597 | ||
5598 | elsif Has_Missing_Return (Spec_Id) then | |
5599 | declare | |
5600 | Hloc : constant Source_Ptr := Sloc (HSS); | |
5601 | Blok : constant Node_Id := | |
5602 | Make_Block_Statement (Hloc, | |
5603 | Handled_Statement_Sequence => HSS); | |
5604 | Rais : constant Node_Id := | |
5605 | Make_Raise_Program_Error (Hloc, | |
5606 | Reason => PE_Missing_Return); | |
5607 | ||
5608 | begin | |
5609 | Set_Handled_Statement_Sequence (N, | |
5610 | Make_Handled_Sequence_Of_Statements (Hloc, | |
5611 | Statements => New_List (Blok, Rais))); | |
5612 | ||
5613 | Push_Scope (Spec_Id); | |
5614 | Analyze (Blok); | |
5615 | Analyze (Rais); | |
5616 | Pop_Scope; | |
5617 | end; | |
5618 | end if; | |
5619 | ||
5620 | -- If subprogram contains a parameterless recursive call, then we may | |
5621 | -- have an infinite recursion, so see if we can generate code to check | |
5622 | -- for this possibility if storage checks are not suppressed. | |
5623 | ||
5624 | if Ekind (Spec_Id) = E_Procedure | |
5625 | and then Has_Recursive_Call (Spec_Id) | |
5626 | and then not Storage_Checks_Suppressed (Spec_Id) | |
5627 | then | |
5628 | Detect_Infinite_Recursion (N, Spec_Id); | |
5629 | end if; | |
5630 | ||
5631 | -- Set to encode entity names in package body before gigi is called | |
5632 | ||
5633 | Qualify_Entity_Names (N); | |
5634 | end Expand_N_Subprogram_Body; | |
5635 | ||
5636 | ----------------------------------- | |
5637 | -- Expand_N_Subprogram_Body_Stub -- | |
5638 | ----------------------------------- | |
5639 | ||
5640 | procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is | |
5641 | Bod : Node_Id; | |
5642 | ||
5643 | begin | |
5644 | if Present (Corresponding_Body (N)) then | |
5645 | Bod := Unit_Declaration_Node (Corresponding_Body (N)); | |
5646 | ||
5647 | -- The body may have been expanded already when it is analyzed | |
5648 | -- through the subunit node. Do no expand again: it interferes | |
5649 | -- with the construction of unnesting tables when generating C. | |
5650 | ||
5651 | if not Analyzed (Bod) then | |
5652 | Expand_N_Subprogram_Body (Bod); | |
5653 | end if; | |
5654 | ||
5655 | -- Add full qualification to entities that may be created late | |
5656 | -- during unnesting. | |
5657 | ||
5658 | Qualify_Entity_Names (N); | |
5659 | end if; | |
5660 | end Expand_N_Subprogram_Body_Stub; | |
5661 | ||
5662 | ------------------------------------- | |
5663 | -- Expand_N_Subprogram_Declaration -- | |
5664 | ------------------------------------- | |
5665 | ||
5666 | -- If the declaration appears within a protected body, it is a private | |
5667 | -- operation of the protected type. We must create the corresponding | |
5668 | -- protected subprogram an associated formals. For a normal protected | |
5669 | -- operation, this is done when expanding the protected type declaration. | |
5670 | ||
5671 | -- If the declaration is for a null procedure, emit null body | |
5672 | ||
5673 | procedure Expand_N_Subprogram_Declaration (N : Node_Id) is | |
5674 | Loc : constant Source_Ptr := Sloc (N); | |
5675 | Subp : constant Entity_Id := Defining_Entity (N); | |
5676 | ||
5677 | -- Local variables | |
5678 | ||
5679 | Scop : constant Entity_Id := Scope (Subp); | |
5680 | Prot_Bod : Node_Id; | |
5681 | Prot_Decl : Node_Id; | |
5682 | Prot_Id : Entity_Id; | |
5683 | ||
5684 | -- Start of processing for Expand_N_Subprogram_Declaration | |
5685 | ||
5686 | begin | |
5687 | -- In SPARK, subprogram declarations are only allowed in package | |
5688 | -- specifications. | |
5689 | ||
5690 | if Nkind (Parent (N)) /= N_Package_Specification then | |
5691 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
5692 | Check_SPARK_05_Restriction | |
5693 | ("subprogram declaration is not a library item", N); | |
5694 | ||
5695 | elsif Present (Next (N)) | |
5696 | and then Nkind (Next (N)) = N_Pragma | |
5697 | and then Get_Pragma_Id (Next (N)) = Pragma_Import | |
5698 | then | |
5699 | -- In SPARK, subprogram declarations are also permitted in | |
5700 | -- declarative parts when immediately followed by a corresponding | |
5701 | -- pragma Import. We only check here that there is some pragma | |
5702 | -- Import. | |
5703 | ||
5704 | null; | |
5705 | else | |
5706 | Check_SPARK_05_Restriction | |
5707 | ("subprogram declaration is not allowed here", N); | |
5708 | end if; | |
5709 | end if; | |
5710 | ||
5711 | -- Deal with case of protected subprogram. Do not generate protected | |
5712 | -- operation if operation is flagged as eliminated. | |
5713 | ||
5714 | if Is_List_Member (N) | |
5715 | and then Present (Parent (List_Containing (N))) | |
5716 | and then Nkind (Parent (List_Containing (N))) = N_Protected_Body | |
5717 | and then Is_Protected_Type (Scop) | |
5718 | then | |
5719 | if No (Protected_Body_Subprogram (Subp)) | |
5720 | and then not Is_Eliminated (Subp) | |
5721 | then | |
5722 | Prot_Decl := | |
5723 | Make_Subprogram_Declaration (Loc, | |
5724 | Specification => | |
5725 | Build_Protected_Sub_Specification | |
5726 | (N, Scop, Unprotected_Mode)); | |
5727 | ||
5728 | -- The protected subprogram is declared outside of the protected | |
5729 | -- body. Given that the body has frozen all entities so far, we | |
5730 | -- analyze the subprogram and perform freezing actions explicitly. | |
5731 | -- including the generation of an explicit freeze node, to ensure | |
5732 | -- that gigi has the proper order of elaboration. | |
5733 | -- If the body is a subunit, the insertion point is before the | |
5734 | -- stub in the parent. | |
5735 | ||
5736 | Prot_Bod := Parent (List_Containing (N)); | |
5737 | ||
5738 | if Nkind (Parent (Prot_Bod)) = N_Subunit then | |
5739 | Prot_Bod := Corresponding_Stub (Parent (Prot_Bod)); | |
5740 | end if; | |
5741 | ||
5742 | Insert_Before (Prot_Bod, Prot_Decl); | |
5743 | Prot_Id := Defining_Unit_Name (Specification (Prot_Decl)); | |
5744 | Set_Has_Delayed_Freeze (Prot_Id); | |
5745 | ||
5746 | Push_Scope (Scope (Scop)); | |
5747 | Analyze (Prot_Decl); | |
5748 | Freeze_Before (N, Prot_Id); | |
5749 | Set_Protected_Body_Subprogram (Subp, Prot_Id); | |
5750 | ||
5751 | -- Create protected operation as well. Even though the operation | |
5752 | -- is only accessible within the body, it is possible to make it | |
5753 | -- available outside of the protected object by using 'Access to | |
5754 | -- provide a callback, so build protected version in all cases. | |
5755 | ||
5756 | Prot_Decl := | |
5757 | Make_Subprogram_Declaration (Loc, | |
5758 | Specification => | |
5759 | Build_Protected_Sub_Specification (N, Scop, Protected_Mode)); | |
5760 | Insert_Before (Prot_Bod, Prot_Decl); | |
5761 | Analyze (Prot_Decl); | |
5762 | ||
5763 | Pop_Scope; | |
5764 | end if; | |
5765 | ||
5766 | -- Ada 2005 (AI-348): Generate body for a null procedure. In most | |
5767 | -- cases this is superfluous because calls to it will be automatically | |
5768 | -- inlined, but we definitely need the body if preconditions for the | |
5769 | -- procedure are present, or if performing coverage analysis. | |
5770 | ||
5771 | elsif Nkind (Specification (N)) = N_Procedure_Specification | |
5772 | and then Null_Present (Specification (N)) | |
5773 | then | |
5774 | declare | |
5775 | Bod : constant Node_Id := Body_To_Inline (N); | |
5776 | ||
5777 | begin | |
5778 | Set_Has_Completion (Subp, False); | |
5779 | Append_Freeze_Action (Subp, Bod); | |
5780 | ||
5781 | -- The body now contains raise statements, so calls to it will | |
5782 | -- not be inlined. | |
5783 | ||
5784 | Set_Is_Inlined (Subp, False); | |
5785 | end; | |
5786 | end if; | |
5787 | ||
5788 | -- When generating C code, transform a function that returns a | |
5789 | -- constrained array type into a procedure with an out parameter | |
5790 | -- that carries the return value. | |
5791 | ||
5792 | -- We skip this transformation for unchecked conversions, since they | |
5793 | -- are not needed by the C generator (and this also produces cleaner | |
5794 | -- output). | |
5795 | ||
5796 | if Modify_Tree_For_C | |
5797 | and then Nkind (Specification (N)) = N_Function_Specification | |
5798 | and then Is_Array_Type (Etype (Subp)) | |
5799 | and then Is_Constrained (Etype (Subp)) | |
5800 | and then not Is_Unchecked_Conversion_Instance (Subp) | |
5801 | then | |
5802 | Build_Procedure_Form (N); | |
5803 | end if; | |
5804 | end Expand_N_Subprogram_Declaration; | |
5805 | ||
5806 | -------------------------------- | |
5807 | -- Expand_Non_Function_Return -- | |
5808 | -------------------------------- | |
5809 | ||
5810 | procedure Expand_Non_Function_Return (N : Node_Id) is | |
5811 | pragma Assert (No (Expression (N))); | |
5812 | ||
5813 | Loc : constant Source_Ptr := Sloc (N); | |
5814 | Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N)); | |
5815 | Kind : constant Entity_Kind := Ekind (Scope_Id); | |
5816 | Call : Node_Id; | |
5817 | Acc_Stat : Node_Id; | |
5818 | Goto_Stat : Node_Id; | |
5819 | Lab_Node : Node_Id; | |
5820 | ||
5821 | begin | |
5822 | -- Call the _Postconditions procedure if the related subprogram has | |
5823 | -- contract assertions that need to be verified on exit. | |
5824 | ||
5825 | if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure) | |
5826 | and then Present (Postconditions_Proc (Scope_Id)) | |
5827 | then | |
5828 | Insert_Action (N, | |
5829 | Make_Procedure_Call_Statement (Loc, | |
5830 | Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc))); | |
5831 | end if; | |
5832 | ||
5833 | -- If it is a return from a procedure do no extra steps | |
5834 | ||
5835 | if Kind = E_Procedure or else Kind = E_Generic_Procedure then | |
5836 | return; | |
5837 | ||
5838 | -- If it is a nested return within an extended one, replace it with a | |
5839 | -- return of the previously declared return object. | |
5840 | ||
5841 | elsif Kind = E_Return_Statement then | |
5842 | Rewrite (N, | |
5843 | Make_Simple_Return_Statement (Loc, | |
5844 | Expression => | |
5845 | New_Occurrence_Of (First_Entity (Scope_Id), Loc))); | |
5846 | Set_Comes_From_Extended_Return_Statement (N); | |
5847 | Set_Return_Statement_Entity (N, Scope_Id); | |
5848 | Expand_Simple_Function_Return (N); | |
5849 | return; | |
5850 | end if; | |
5851 | ||
5852 | pragma Assert (Is_Entry (Scope_Id)); | |
5853 | ||
5854 | -- Look at the enclosing block to see whether the return is from an | |
5855 | -- accept statement or an entry body. | |
5856 | ||
5857 | for J in reverse 0 .. Scope_Stack.Last loop | |
5858 | Scope_Id := Scope_Stack.Table (J).Entity; | |
5859 | exit when Is_Concurrent_Type (Scope_Id); | |
5860 | end loop; | |
5861 | ||
5862 | -- If it is a return from accept statement it is expanded as call to | |
5863 | -- RTS Complete_Rendezvous and a goto to the end of the accept body. | |
5864 | ||
5865 | -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept, | |
5866 | -- Expand_N_Accept_Alternative in exp_ch9.adb) | |
5867 | ||
5868 | if Is_Task_Type (Scope_Id) then | |
5869 | ||
5870 | Call := | |
5871 | Make_Procedure_Call_Statement (Loc, | |
5872 | Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc)); | |
5873 | Insert_Before (N, Call); | |
5874 | -- why not insert actions here??? | |
5875 | Analyze (Call); | |
5876 | ||
5877 | Acc_Stat := Parent (N); | |
5878 | while Nkind (Acc_Stat) /= N_Accept_Statement loop | |
5879 | Acc_Stat := Parent (Acc_Stat); | |
5880 | end loop; | |
5881 | ||
5882 | Lab_Node := Last (Statements | |
5883 | (Handled_Statement_Sequence (Acc_Stat))); | |
5884 | ||
5885 | Goto_Stat := Make_Goto_Statement (Loc, | |
5886 | Name => New_Occurrence_Of | |
5887 | (Entity (Identifier (Lab_Node)), Loc)); | |
5888 | ||
5889 | Set_Analyzed (Goto_Stat); | |
5890 | ||
5891 | Rewrite (N, Goto_Stat); | |
5892 | Analyze (N); | |
5893 | ||
5894 | -- If it is a return from an entry body, put a Complete_Entry_Body call | |
5895 | -- in front of the return. | |
5896 | ||
5897 | elsif Is_Protected_Type (Scope_Id) then | |
5898 | Call := | |
5899 | Make_Procedure_Call_Statement (Loc, | |
5900 | Name => | |
5901 | New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc), | |
5902 | Parameter_Associations => New_List ( | |
5903 | Make_Attribute_Reference (Loc, | |
5904 | Prefix => | |
5905 | New_Occurrence_Of | |
5906 | (Find_Protection_Object (Current_Scope), Loc), | |
5907 | Attribute_Name => Name_Unchecked_Access))); | |
5908 | ||
5909 | Insert_Before (N, Call); | |
5910 | Analyze (Call); | |
5911 | end if; | |
5912 | end Expand_Non_Function_Return; | |
5913 | ||
5914 | --------------------------------------- | |
5915 | -- Expand_Protected_Object_Reference -- | |
5916 | --------------------------------------- | |
5917 | ||
5918 | function Expand_Protected_Object_Reference | |
5919 | (N : Node_Id; | |
5920 | Scop : Entity_Id) return Node_Id | |
5921 | is | |
5922 | Loc : constant Source_Ptr := Sloc (N); | |
5923 | Corr : Entity_Id; | |
5924 | Rec : Node_Id; | |
5925 | Param : Entity_Id; | |
5926 | Proc : Entity_Id; | |
5927 | ||
5928 | begin | |
5929 | Rec := Make_Identifier (Loc, Name_uObject); | |
5930 | Set_Etype (Rec, Corresponding_Record_Type (Scop)); | |
5931 | ||
5932 | -- Find enclosing protected operation, and retrieve its first parameter, | |
5933 | -- which denotes the enclosing protected object. If the enclosing | |
5934 | -- operation is an entry, we are immediately within the protected body, | |
5935 | -- and we can retrieve the object from the service entries procedure. A | |
5936 | -- barrier function has the same signature as an entry. A barrier | |
5937 | -- function is compiled within the protected object, but unlike | |
5938 | -- protected operations its never needs locks, so that its protected | |
5939 | -- body subprogram points to itself. | |
5940 | ||
5941 | Proc := Current_Scope; | |
5942 | while Present (Proc) | |
5943 | and then Scope (Proc) /= Scop | |
5944 | loop | |
5945 | Proc := Scope (Proc); | |
5946 | end loop; | |
5947 | ||
5948 | Corr := Protected_Body_Subprogram (Proc); | |
5949 | ||
5950 | if No (Corr) then | |
5951 | ||
5952 | -- Previous error left expansion incomplete. | |
5953 | -- Nothing to do on this call. | |
5954 | ||
5955 | return Empty; | |
5956 | end if; | |
5957 | ||
5958 | Param := | |
5959 | Defining_Identifier | |
5960 | (First (Parameter_Specifications (Parent (Corr)))); | |
5961 | ||
5962 | if Is_Subprogram (Proc) and then Proc /= Corr then | |
5963 | ||
5964 | -- Protected function or procedure | |
5965 | ||
5966 | Set_Entity (Rec, Param); | |
5967 | ||
5968 | -- Rec is a reference to an entity which will not be in scope when | |
5969 | -- the call is reanalyzed, and needs no further analysis. | |
5970 | ||
5971 | Set_Analyzed (Rec); | |
5972 | ||
5973 | else | |
5974 | -- Entry or barrier function for entry body. The first parameter of | |
5975 | -- the entry body procedure is pointer to the object. We create a | |
5976 | -- local variable of the proper type, duplicating what is done to | |
5977 | -- define _object later on. | |
5978 | ||
5979 | declare | |
5980 | Decls : List_Id; | |
5981 | Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T'); | |
5982 | ||
5983 | begin | |
5984 | Decls := New_List ( | |
5985 | Make_Full_Type_Declaration (Loc, | |
5986 | Defining_Identifier => Obj_Ptr, | |
5987 | Type_Definition => | |
5988 | Make_Access_To_Object_Definition (Loc, | |
5989 | Subtype_Indication => | |
5990 | New_Occurrence_Of | |
5991 | (Corresponding_Record_Type (Scop), Loc)))); | |
5992 | ||
5993 | Insert_Actions (N, Decls); | |
5994 | Freeze_Before (N, Obj_Ptr); | |
5995 | ||
5996 | Rec := | |
5997 | Make_Explicit_Dereference (Loc, | |
5998 | Prefix => | |
5999 | Unchecked_Convert_To (Obj_Ptr, | |
6000 | New_Occurrence_Of (Param, Loc))); | |
6001 | ||
6002 | -- Analyze new actual. Other actuals in calls are already analyzed | |
6003 | -- and the list of actuals is not reanalyzed after rewriting. | |
6004 | ||
6005 | Set_Parent (Rec, N); | |
6006 | Analyze (Rec); | |
6007 | end; | |
6008 | end if; | |
6009 | ||
6010 | return Rec; | |
6011 | end Expand_Protected_Object_Reference; | |
6012 | ||
6013 | -------------------------------------- | |
6014 | -- Expand_Protected_Subprogram_Call -- | |
6015 | -------------------------------------- | |
6016 | ||
6017 | procedure Expand_Protected_Subprogram_Call | |
6018 | (N : Node_Id; | |
6019 | Subp : Entity_Id; | |
6020 | Scop : Entity_Id) | |
6021 | is | |
6022 | Rec : Node_Id; | |
6023 | ||
6024 | procedure Expand_Internal_Init_Call; | |
6025 | -- A call to an operation of the type may occur in the initialization | |
6026 | -- of a private component. In that case the prefix of the call is an | |
6027 | -- entity name and the call is treated as internal even though it | |
6028 | -- appears in code outside of the protected type. | |
6029 | ||
6030 | procedure Freeze_Called_Function; | |
6031 | -- If it is a function call it can appear in elaboration code and | |
6032 | -- the called entity must be frozen before the call. This must be | |
6033 | -- done before the call is expanded, as the expansion may rewrite it | |
6034 | -- to something other than a call (e.g. a temporary initialized in a | |
6035 | -- transient block). | |
6036 | ||
6037 | ------------------------------- | |
6038 | -- Expand_Internal_Init_Call -- | |
6039 | ------------------------------- | |
6040 | ||
6041 | procedure Expand_Internal_Init_Call is | |
6042 | begin | |
6043 | -- If the context is a protected object (rather than a protected | |
6044 | -- type) the call itself is bound to raise program_error because | |
6045 | -- the protected body will not have been elaborated yet. This is | |
6046 | -- diagnosed subsequently in Sem_Elab. | |
6047 | ||
6048 | Freeze_Called_Function; | |
6049 | ||
6050 | -- The target of the internal call is the first formal of the | |
6051 | -- enclosing initialization procedure. | |
6052 | ||
6053 | Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N)); | |
6054 | Build_Protected_Subprogram_Call (N, | |
6055 | Name => Name (N), | |
6056 | Rec => Rec, | |
6057 | External => False); | |
6058 | Analyze (N); | |
6059 | Resolve (N, Etype (Subp)); | |
6060 | end Expand_Internal_Init_Call; | |
6061 | ||
6062 | ---------------------------- | |
6063 | -- Freeze_Called_Function -- | |
6064 | ---------------------------- | |
6065 | ||
6066 | procedure Freeze_Called_Function is | |
6067 | begin | |
6068 | if Ekind (Subp) = E_Function then | |
6069 | Freeze_Expression (Name (N)); | |
6070 | end if; | |
6071 | end Freeze_Called_Function; | |
6072 | ||
6073 | -- Start of processing for Expand_Protected_Subprogram_Call | |
6074 | ||
6075 | begin | |
6076 | -- If the protected object is not an enclosing scope, this is an inter- | |
6077 | -- object function call. Inter-object procedure calls are expanded by | |
6078 | -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the | |
6079 | -- subprogram being called is in the protected body being compiled, and | |
6080 | -- if the protected object in the call is statically the enclosing type. | |
6081 | -- The object may be a component of some other data structure, in which | |
6082 | -- case this must be handled as an inter-object call. | |
6083 | ||
6084 | if not In_Open_Scopes (Scop) | |
6085 | or else Is_Entry_Wrapper (Current_Scope) | |
6086 | or else not Is_Entity_Name (Name (N)) | |
6087 | then | |
6088 | if Nkind (Name (N)) = N_Selected_Component then | |
6089 | Rec := Prefix (Name (N)); | |
6090 | ||
6091 | elsif Nkind (Name (N)) = N_Indexed_Component then | |
6092 | Rec := Prefix (Prefix (Name (N))); | |
6093 | ||
6094 | -- If this is a call within an entry wrapper, it appears within a | |
6095 | -- precondition that calls another primitive of the synchronized | |
6096 | -- type. The target object of the call is the first actual on the | |
6097 | -- wrapper. Note that this is an external call, because the wrapper | |
6098 | -- is called outside of the synchronized object. This means that | |
6099 | -- an entry call to an entry with preconditions involves two | |
6100 | -- synchronized operations. | |
6101 | ||
6102 | elsif Ekind (Current_Scope) = E_Procedure | |
6103 | and then Is_Entry_Wrapper (Current_Scope) | |
6104 | then | |
6105 | Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N)); | |
6106 | ||
6107 | else | |
6108 | -- If the context is the initialization procedure for a protected | |
6109 | -- type, the call is legal because the called entity must be a | |
6110 | -- function of that enclosing type, and this is treated as an | |
6111 | -- internal call. | |
6112 | ||
6113 | pragma Assert | |
6114 | (Is_Entity_Name (Name (N)) and then Inside_Init_Proc); | |
6115 | ||
6116 | Expand_Internal_Init_Call; | |
6117 | return; | |
6118 | end if; | |
6119 | ||
6120 | Freeze_Called_Function; | |
6121 | Build_Protected_Subprogram_Call (N, | |
6122 | Name => New_Occurrence_Of (Subp, Sloc (N)), | |
6123 | Rec => Convert_Concurrent (Rec, Etype (Rec)), | |
6124 | External => True); | |
6125 | ||
6126 | else | |
6127 | Rec := Expand_Protected_Object_Reference (N, Scop); | |
6128 | ||
6129 | if No (Rec) then | |
6130 | return; | |
6131 | end if; | |
6132 | ||
6133 | Freeze_Called_Function; | |
6134 | Build_Protected_Subprogram_Call (N, | |
6135 | Name => Name (N), | |
6136 | Rec => Rec, | |
6137 | External => False); | |
6138 | end if; | |
6139 | ||
6140 | -- Analyze and resolve the new call. The actuals have already been | |
6141 | -- resolved, but expansion of a function call will add extra actuals | |
6142 | -- if needed. Analysis of a procedure call already includes resolution. | |
6143 | ||
6144 | Analyze (N); | |
6145 | ||
6146 | if Ekind (Subp) = E_Function then | |
6147 | Resolve (N, Etype (Subp)); | |
6148 | end if; | |
6149 | end Expand_Protected_Subprogram_Call; | |
6150 | ||
6151 | ----------------------------------- | |
6152 | -- Expand_Simple_Function_Return -- | |
6153 | ----------------------------------- | |
6154 | ||
6155 | -- The "simple" comes from the syntax rule simple_return_statement. The | |
6156 | -- semantics are not at all simple. | |
6157 | ||
6158 | procedure Expand_Simple_Function_Return (N : Node_Id) is | |
6159 | Loc : constant Source_Ptr := Sloc (N); | |
6160 | ||
6161 | Scope_Id : constant Entity_Id := | |
6162 | Return_Applies_To (Return_Statement_Entity (N)); | |
6163 | -- The function we are returning from | |
6164 | ||
6165 | R_Type : constant Entity_Id := Etype (Scope_Id); | |
6166 | -- The result type of the function | |
6167 | ||
6168 | Utyp : constant Entity_Id := Underlying_Type (R_Type); | |
6169 | ||
6170 | Exp : Node_Id := Expression (N); | |
6171 | pragma Assert (Present (Exp)); | |
6172 | ||
6173 | Exptyp : constant Entity_Id := Etype (Exp); | |
6174 | -- The type of the expression (not necessarily the same as R_Type) | |
6175 | ||
6176 | Subtype_Ind : Node_Id; | |
6177 | -- If the result type of the function is class-wide and the expression | |
6178 | -- has a specific type, then we use the expression's type as the type of | |
6179 | -- the return object. In cases where the expression is an aggregate that | |
6180 | -- is built in place, this avoids the need for an expensive conversion | |
6181 | -- of the return object to the specific type on assignments to the | |
6182 | -- individual components. | |
6183 | ||
6184 | begin | |
6185 | if Is_Class_Wide_Type (R_Type) | |
6186 | and then not Is_Class_Wide_Type (Exptyp) | |
6187 | and then Nkind (Exp) /= N_Type_Conversion | |
6188 | then | |
6189 | Subtype_Ind := New_Occurrence_Of (Exptyp, Loc); | |
6190 | else | |
6191 | Subtype_Ind := New_Occurrence_Of (R_Type, Loc); | |
6192 | ||
6193 | -- If the result type is class-wide and the expression is a view | |
6194 | -- conversion, the conversion plays no role in the expansion because | |
6195 | -- it does not modify the tag of the object. Remove the conversion | |
6196 | -- altogether to prevent tag overwriting. | |
6197 | ||
6198 | if Is_Class_Wide_Type (R_Type) | |
6199 | and then not Is_Class_Wide_Type (Exptyp) | |
6200 | and then Nkind (Exp) = N_Type_Conversion | |
6201 | then | |
6202 | Exp := Expression (Exp); | |
6203 | end if; | |
6204 | end if; | |
6205 | ||
6206 | -- For the case of a simple return that does not come from an extended | |
6207 | -- return, in the case of Ada 2005 where we are returning a limited | |
6208 | -- type, we rewrite "return <expression>;" to be: | |
6209 | ||
6210 | -- return _anon_ : <return_subtype> := <expression> | |
6211 | ||
6212 | -- The expansion produced by Expand_N_Extended_Return_Statement will | |
6213 | -- contain simple return statements (for example, a block containing | |
6214 | -- simple return of the return object), which brings us back here with | |
6215 | -- Comes_From_Extended_Return_Statement set. The reason for the barrier | |
6216 | -- checking for a simple return that does not come from an extended | |
6217 | -- return is to avoid this infinite recursion. | |
6218 | ||
6219 | -- The reason for this design is that for Ada 2005 limited returns, we | |
6220 | -- need to reify the return object, so we can build it "in place", and | |
6221 | -- we need a block statement to hang finalization and tasking stuff. | |
6222 | ||
6223 | -- ??? In order to avoid disruption, we avoid translating to extended | |
6224 | -- return except in the cases where we really need to (Ada 2005 for | |
6225 | -- inherently limited). We might prefer to do this translation in all | |
6226 | -- cases (except perhaps for the case of Ada 95 inherently limited), | |
6227 | -- in order to fully exercise the Expand_N_Extended_Return_Statement | |
6228 | -- code. This would also allow us to do the build-in-place optimization | |
6229 | -- for efficiency even in cases where it is semantically not required. | |
6230 | ||
6231 | -- As before, we check the type of the return expression rather than the | |
6232 | -- return type of the function, because the latter may be a limited | |
6233 | -- class-wide interface type, which is not a limited type, even though | |
6234 | -- the type of the expression may be. | |
6235 | ||
6236 | if not Comes_From_Extended_Return_Statement (N) | |
6237 | and then Is_Limited_View (Etype (Expression (N))) | |
6238 | and then Ada_Version >= Ada_2005 | |
6239 | and then not Debug_Flag_Dot_L | |
6240 | ||
6241 | -- The functionality of interface thunks is simple and it is always | |
6242 | -- handled by means of simple return statements. This leaves their | |
6243 | -- expansion simple and clean. | |
6244 | ||
6245 | and then not Is_Thunk (Current_Scope) | |
6246 | then | |
6247 | declare | |
6248 | Return_Object_Entity : constant Entity_Id := | |
6249 | Make_Temporary (Loc, 'R', Exp); | |
6250 | ||
6251 | Obj_Decl : constant Node_Id := | |
6252 | Make_Object_Declaration (Loc, | |
6253 | Defining_Identifier => Return_Object_Entity, | |
6254 | Object_Definition => Subtype_Ind, | |
6255 | Expression => Exp); | |
6256 | ||
6257 | Ext : constant Node_Id := | |
6258 | Make_Extended_Return_Statement (Loc, | |
6259 | Return_Object_Declarations => New_List (Obj_Decl)); | |
6260 | -- Do not perform this high-level optimization if the result type | |
6261 | -- is an interface because the "this" pointer must be displaced. | |
6262 | ||
6263 | begin | |
6264 | Rewrite (N, Ext); | |
6265 | Analyze (N); | |
6266 | return; | |
6267 | end; | |
6268 | end if; | |
6269 | ||
6270 | -- Here we have a simple return statement that is part of the expansion | |
6271 | -- of an extended return statement (either written by the user, or | |
6272 | -- generated by the above code). | |
6273 | ||
6274 | -- Always normalize C/Fortran boolean result. This is not always needed, | |
6275 | -- but it seems a good idea to minimize the passing around of non- | |
6276 | -- normalized values, and in any case this handles the processing of | |
6277 | -- barrier functions for protected types, which turn the condition into | |
6278 | -- a return statement. | |
6279 | ||
6280 | if Is_Boolean_Type (Exptyp) | |
6281 | and then Nonzero_Is_True (Exptyp) | |
6282 | then | |
6283 | Adjust_Condition (Exp); | |
6284 | Adjust_Result_Type (Exp, Exptyp); | |
6285 | end if; | |
6286 | ||
6287 | -- Do validity check if enabled for returns | |
6288 | ||
6289 | if Validity_Checks_On | |
6290 | and then Validity_Check_Returns | |
6291 | then | |
6292 | Ensure_Valid (Exp); | |
6293 | end if; | |
6294 | ||
6295 | -- Check the result expression of a scalar function against the subtype | |
6296 | -- of the function by inserting a conversion. This conversion must | |
6297 | -- eventually be performed for other classes of types, but for now it's | |
6298 | -- only done for scalars. | |
6299 | -- ??? | |
6300 | ||
6301 | if Is_Scalar_Type (Exptyp) then | |
6302 | Rewrite (Exp, Convert_To (R_Type, Exp)); | |
6303 | ||
6304 | -- The expression is resolved to ensure that the conversion gets | |
6305 | -- expanded to generate a possible constraint check. | |
6306 | ||
6307 | Analyze_And_Resolve (Exp, R_Type); | |
6308 | end if; | |
6309 | ||
6310 | -- Deal with returning variable length objects and controlled types | |
6311 | ||
6312 | -- Nothing to do if we are returning by reference, or this is not a | |
6313 | -- type that requires special processing (indicated by the fact that | |
6314 | -- it requires a cleanup scope for the secondary stack case). | |
6315 | ||
6316 | if Is_Limited_View (Exptyp) | |
6317 | or else Is_Limited_Interface (Exptyp) | |
6318 | then | |
6319 | null; | |
6320 | ||
6321 | -- No copy needed for thunks returning interface type objects since | |
6322 | -- the object is returned by reference and the maximum functionality | |
6323 | -- required is just to displace the pointer. | |
6324 | ||
6325 | elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then | |
6326 | null; | |
6327 | ||
6328 | -- If the call is within a thunk and the type is a limited view, the | |
6329 | -- backend will eventually see the non-limited view of the type. | |
6330 | ||
6331 | elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then | |
6332 | return; | |
6333 | ||
6334 | elsif not Requires_Transient_Scope (R_Type) then | |
6335 | ||
6336 | -- Mutable records with variable-length components are not returned | |
6337 | -- on the sec-stack, so we need to make sure that the back end will | |
6338 | -- only copy back the size of the actual value, and not the maximum | |
6339 | -- size. We create an actual subtype for this purpose. However we | |
6340 | -- need not do it if the expression is a function call since this | |
6341 | -- will be done in the called function and doing it here too would | |
6342 | -- cause a temporary with maximum size to be created. | |
6343 | ||
6344 | declare | |
6345 | Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp)); | |
6346 | Decl : Node_Id; | |
6347 | Ent : Entity_Id; | |
6348 | begin | |
6349 | if Nkind (Exp) /= N_Function_Call | |
6350 | and then Has_Discriminants (Ubt) | |
6351 | and then not Is_Constrained (Ubt) | |
6352 | and then not Has_Unchecked_Union (Ubt) | |
6353 | then | |
6354 | Decl := Build_Actual_Subtype (Ubt, Exp); | |
6355 | Ent := Defining_Identifier (Decl); | |
6356 | Insert_Action (Exp, Decl); | |
6357 | Rewrite (Exp, Unchecked_Convert_To (Ent, Exp)); | |
6358 | Analyze_And_Resolve (Exp); | |
6359 | end if; | |
6360 | end; | |
6361 | ||
6362 | -- Here if secondary stack is used | |
6363 | ||
6364 | else | |
6365 | -- Prevent the reclamation of the secondary stack by all enclosing | |
6366 | -- blocks and loops as well as the related function; otherwise the | |
6367 | -- result would be reclaimed too early. | |
6368 | ||
6369 | Set_Enclosing_Sec_Stack_Return (N); | |
6370 | ||
6371 | -- Optimize the case where the result is a function call. In this | |
6372 | -- case either the result is already on the secondary stack, or is | |
6373 | -- already being returned with the stack pointer depressed and no | |
6374 | -- further processing is required except to set the By_Ref flag | |
6375 | -- to ensure that gigi does not attempt an extra unnecessary copy. | |
6376 | -- (actually not just unnecessary but harmfully wrong in the case | |
6377 | -- of a controlled type, where gigi does not know how to do a copy). | |
6378 | -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy | |
6379 | -- for array types if the constrained status of the target type is | |
6380 | -- different from that of the expression. | |
6381 | ||
6382 | if Requires_Transient_Scope (Exptyp) | |
6383 | and then | |
6384 | (not Is_Array_Type (Exptyp) | |
6385 | or else Is_Constrained (Exptyp) = Is_Constrained (R_Type) | |
6386 | or else CW_Or_Has_Controlled_Part (Utyp)) | |
6387 | and then Nkind (Exp) = N_Function_Call | |
6388 | then | |
6389 | Set_By_Ref (N); | |
6390 | ||
6391 | -- Remove side effects from the expression now so that other parts | |
6392 | -- of the expander do not have to reanalyze this node without this | |
6393 | -- optimization | |
6394 | ||
6395 | Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp)); | |
6396 | ||
6397 | -- For controlled types, do the allocation on the secondary stack | |
6398 | -- manually in order to call adjust at the right time: | |
6399 | ||
6400 | -- type Anon1 is access R_Type; | |
6401 | -- for Anon1'Storage_pool use ss_pool; | |
6402 | -- Anon2 : anon1 := new R_Type'(expr); | |
6403 | -- return Anon2.all; | |
6404 | ||
6405 | -- We do the same for classwide types that are not potentially | |
6406 | -- controlled (by the virtue of restriction No_Finalization) because | |
6407 | -- gigi is not able to properly allocate class-wide types. | |
6408 | ||
6409 | elsif CW_Or_Has_Controlled_Part (Utyp) then | |
6410 | declare | |
6411 | Loc : constant Source_Ptr := Sloc (N); | |
6412 | Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A'); | |
6413 | Alloc_Node : Node_Id; | |
6414 | Temp : Entity_Id; | |
6415 | ||
6416 | begin | |
6417 | Set_Ekind (Acc_Typ, E_Access_Type); | |
6418 | ||
6419 | Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool)); | |
6420 | ||
6421 | -- This is an allocator for the secondary stack, and it's fine | |
6422 | -- to have Comes_From_Source set False on it, as gigi knows not | |
6423 | -- to flag it as a violation of No_Implicit_Heap_Allocations. | |
6424 | ||
6425 | Alloc_Node := | |
6426 | Make_Allocator (Loc, | |
6427 | Expression => | |
6428 | Make_Qualified_Expression (Loc, | |
6429 | Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc), | |
6430 | Expression => Relocate_Node (Exp))); | |
6431 | ||
6432 | -- We do not want discriminant checks on the declaration, | |
6433 | -- given that it gets its value from the allocator. | |
6434 | ||
6435 | Set_No_Initialization (Alloc_Node); | |
6436 | ||
6437 | Temp := Make_Temporary (Loc, 'R', Alloc_Node); | |
6438 | ||
6439 | Insert_List_Before_And_Analyze (N, New_List ( | |
6440 | Make_Full_Type_Declaration (Loc, | |
6441 | Defining_Identifier => Acc_Typ, | |
6442 | Type_Definition => | |
6443 | Make_Access_To_Object_Definition (Loc, | |
6444 | Subtype_Indication => Subtype_Ind)), | |
6445 | ||
6446 | Make_Object_Declaration (Loc, | |
6447 | Defining_Identifier => Temp, | |
6448 | Object_Definition => New_Occurrence_Of (Acc_Typ, Loc), | |
6449 | Expression => Alloc_Node))); | |
6450 | ||
6451 | Rewrite (Exp, | |
6452 | Make_Explicit_Dereference (Loc, | |
6453 | Prefix => New_Occurrence_Of (Temp, Loc))); | |
6454 | ||
6455 | -- Ada 2005 (AI-251): If the type of the returned object is | |
6456 | -- an interface then add an implicit type conversion to force | |
6457 | -- displacement of the "this" pointer. | |
6458 | ||
6459 | if Is_Interface (R_Type) then | |
6460 | Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp))); | |
6461 | end if; | |
6462 | ||
6463 | Analyze_And_Resolve (Exp, R_Type); | |
6464 | end; | |
6465 | ||
6466 | -- Otherwise use the gigi mechanism to allocate result on the | |
6467 | -- secondary stack. | |
6468 | ||
6469 | else | |
6470 | Check_Restriction (No_Secondary_Stack, N); | |
6471 | Set_Storage_Pool (N, RTE (RE_SS_Pool)); | |
6472 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); | |
6473 | end if; | |
6474 | end if; | |
6475 | ||
6476 | -- Implement the rules of 6.5(8-10), which require a tag check in | |
6477 | -- the case of a limited tagged return type, and tag reassignment for | |
6478 | -- nonlimited tagged results. These actions are needed when the return | |
6479 | -- type is a specific tagged type and the result expression is a | |
6480 | -- conversion or a formal parameter, because in that case the tag of | |
6481 | -- the expression might differ from the tag of the specific result type. | |
6482 | ||
6483 | if Is_Tagged_Type (Utyp) | |
6484 | and then not Is_Class_Wide_Type (Utyp) | |
6485 | and then (Nkind_In (Exp, N_Type_Conversion, | |
6486 | N_Unchecked_Type_Conversion) | |
6487 | or else (Is_Entity_Name (Exp) | |
6488 | and then Ekind (Entity (Exp)) in Formal_Kind)) | |
6489 | then | |
6490 | -- When the return type is limited, perform a check that the tag of | |
6491 | -- the result is the same as the tag of the return type. | |
6492 | ||
6493 | if Is_Limited_Type (R_Type) then | |
6494 | Insert_Action (Exp, | |
6495 | Make_Raise_Constraint_Error (Loc, | |
6496 | Condition => | |
6497 | Make_Op_Ne (Loc, | |
6498 | Left_Opnd => | |
6499 | Make_Selected_Component (Loc, | |
6500 | Prefix => Duplicate_Subexpr (Exp), | |
6501 | Selector_Name => Make_Identifier (Loc, Name_uTag)), | |
6502 | Right_Opnd => | |
6503 | Make_Attribute_Reference (Loc, | |
6504 | Prefix => | |
6505 | New_Occurrence_Of (Base_Type (Utyp), Loc), | |
6506 | Attribute_Name => Name_Tag)), | |
6507 | Reason => CE_Tag_Check_Failed)); | |
6508 | ||
6509 | -- If the result type is a specific nonlimited tagged type, then we | |
6510 | -- have to ensure that the tag of the result is that of the result | |
6511 | -- type. This is handled by making a copy of the expression in | |
6512 | -- the case where it might have a different tag, namely when the | |
6513 | -- expression is a conversion or a formal parameter. We create a new | |
6514 | -- object of the result type and initialize it from the expression, | |
6515 | -- which will implicitly force the tag to be set appropriately. | |
6516 | ||
6517 | else | |
6518 | declare | |
6519 | ExpR : constant Node_Id := Relocate_Node (Exp); | |
6520 | Result_Id : constant Entity_Id := | |
6521 | Make_Temporary (Loc, 'R', ExpR); | |
6522 | Result_Exp : constant Node_Id := | |
6523 | New_Occurrence_Of (Result_Id, Loc); | |
6524 | Result_Obj : constant Node_Id := | |
6525 | Make_Object_Declaration (Loc, | |
6526 | Defining_Identifier => Result_Id, | |
6527 | Object_Definition => | |
6528 | New_Occurrence_Of (R_Type, Loc), | |
6529 | Constant_Present => True, | |
6530 | Expression => ExpR); | |
6531 | ||
6532 | begin | |
6533 | Set_Assignment_OK (Result_Obj); | |
6534 | Insert_Action (Exp, Result_Obj); | |
6535 | ||
6536 | Rewrite (Exp, Result_Exp); | |
6537 | Analyze_And_Resolve (Exp, R_Type); | |
6538 | end; | |
6539 | end if; | |
6540 | ||
6541 | -- Ada 2005 (AI-344): If the result type is class-wide, then insert | |
6542 | -- a check that the level of the return expression's underlying type | |
6543 | -- is not deeper than the level of the master enclosing the function. | |
6544 | -- Always generate the check when the type of the return expression | |
6545 | -- is class-wide, when it's a type conversion, or when it's a formal | |
6546 | -- parameter. Otherwise, suppress the check in the case where the | |
6547 | -- return expression has a specific type whose level is known not to | |
6548 | -- be statically deeper than the function's result type. | |
6549 | ||
6550 | -- No runtime check needed in interface thunks since it is performed | |
6551 | -- by the target primitive associated with the thunk. | |
6552 | ||
6553 | -- Note: accessibility check is skipped in the VM case, since there | |
6554 | -- does not seem to be any practical way to implement this check. | |
6555 | ||
6556 | elsif Ada_Version >= Ada_2005 | |
6557 | and then Tagged_Type_Expansion | |
6558 | and then Is_Class_Wide_Type (R_Type) | |
6559 | and then not Is_Thunk (Current_Scope) | |
6560 | and then not Scope_Suppress.Suppress (Accessibility_Check) | |
6561 | and then | |
6562 | (Is_Class_Wide_Type (Etype (Exp)) | |
6563 | or else Nkind_In (Exp, N_Type_Conversion, | |
6564 | N_Unchecked_Type_Conversion) | |
6565 | or else (Is_Entity_Name (Exp) | |
6566 | and then Ekind (Entity (Exp)) in Formal_Kind) | |
6567 | or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) > | |
6568 | Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id))) | |
6569 | then | |
6570 | declare | |
6571 | Tag_Node : Node_Id; | |
6572 | ||
6573 | begin | |
6574 | -- Ada 2005 (AI-251): In class-wide interface objects we displace | |
6575 | -- "this" to reference the base of the object. This is required to | |
6576 | -- get access to the TSD of the object. | |
6577 | ||
6578 | if Is_Class_Wide_Type (Etype (Exp)) | |
6579 | and then Is_Interface (Etype (Exp)) | |
6580 | then | |
6581 | -- If the expression is an explicit dereference then we can | |
6582 | -- directly displace the pointer to reference the base of | |
6583 | -- the object. | |
6584 | ||
6585 | if Nkind (Exp) = N_Explicit_Dereference then | |
6586 | Tag_Node := | |
6587 | Make_Explicit_Dereference (Loc, | |
6588 | Prefix => | |
6589 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
6590 | Make_Function_Call (Loc, | |
6591 | Name => | |
6592 | New_Occurrence_Of (RTE (RE_Base_Address), Loc), | |
6593 | Parameter_Associations => New_List ( | |
6594 | Unchecked_Convert_To (RTE (RE_Address), | |
6595 | Duplicate_Subexpr (Prefix (Exp))))))); | |
6596 | ||
6597 | -- Similar case to the previous one but the expression is a | |
6598 | -- renaming of an explicit dereference. | |
6599 | ||
6600 | elsif Nkind (Exp) = N_Identifier | |
6601 | and then Present (Renamed_Object (Entity (Exp))) | |
6602 | and then Nkind (Renamed_Object (Entity (Exp))) | |
6603 | = N_Explicit_Dereference | |
6604 | then | |
6605 | Tag_Node := | |
6606 | Make_Explicit_Dereference (Loc, | |
6607 | Prefix => | |
6608 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
6609 | Make_Function_Call (Loc, | |
6610 | Name => | |
6611 | New_Occurrence_Of (RTE (RE_Base_Address), Loc), | |
6612 | Parameter_Associations => New_List ( | |
6613 | Unchecked_Convert_To (RTE (RE_Address), | |
6614 | Duplicate_Subexpr | |
6615 | (Prefix | |
6616 | (Renamed_Object (Entity (Exp))))))))); | |
6617 | ||
6618 | -- Common case: obtain the address of the actual object and | |
6619 | -- displace the pointer to reference the base of the object. | |
6620 | ||
6621 | else | |
6622 | Tag_Node := | |
6623 | Make_Explicit_Dereference (Loc, | |
6624 | Prefix => | |
6625 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
6626 | Make_Function_Call (Loc, | |
6627 | Name => | |
6628 | New_Occurrence_Of (RTE (RE_Base_Address), Loc), | |
6629 | Parameter_Associations => New_List ( | |
6630 | Make_Attribute_Reference (Loc, | |
6631 | Prefix => Duplicate_Subexpr (Exp), | |
6632 | Attribute_Name => Name_Address))))); | |
6633 | end if; | |
6634 | else | |
6635 | Tag_Node := | |
6636 | Make_Attribute_Reference (Loc, | |
6637 | Prefix => Duplicate_Subexpr (Exp), | |
6638 | Attribute_Name => Name_Tag); | |
6639 | end if; | |
6640 | ||
6641 | -- CodePeer does not do anything useful with | |
6642 | -- Ada.Tags.Type_Specific_Data components. | |
6643 | ||
6644 | if not CodePeer_Mode then | |
6645 | Insert_Action (Exp, | |
6646 | Make_Raise_Program_Error (Loc, | |
6647 | Condition => | |
6648 | Make_Op_Gt (Loc, | |
6649 | Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node), | |
6650 | Right_Opnd => | |
6651 | Make_Integer_Literal (Loc, | |
6652 | Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))), | |
6653 | Reason => PE_Accessibility_Check_Failed)); | |
6654 | end if; | |
6655 | end; | |
6656 | ||
6657 | -- AI05-0073: If function has a controlling access result, check that | |
6658 | -- the tag of the return value, if it is not null, matches designated | |
6659 | -- type of return type. | |
6660 | ||
6661 | -- The return expression is referenced twice in the code below, so it | |
6662 | -- must be made free of side effects. Given that different compilers | |
6663 | -- may evaluate these parameters in different order, both occurrences | |
6664 | -- perform a copy. | |
6665 | ||
6666 | elsif Ekind (R_Type) = E_Anonymous_Access_Type | |
6667 | and then Has_Controlling_Result (Scope_Id) | |
6668 | then | |
6669 | Insert_Action (N, | |
6670 | Make_Raise_Constraint_Error (Loc, | |
6671 | Condition => | |
6672 | Make_And_Then (Loc, | |
6673 | Left_Opnd => | |
6674 | Make_Op_Ne (Loc, | |
6675 | Left_Opnd => Duplicate_Subexpr (Exp), | |
6676 | Right_Opnd => Make_Null (Loc)), | |
6677 | ||
6678 | Right_Opnd => Make_Op_Ne (Loc, | |
6679 | Left_Opnd => | |
6680 | Make_Selected_Component (Loc, | |
6681 | Prefix => Duplicate_Subexpr (Exp), | |
6682 | Selector_Name => Make_Identifier (Loc, Name_uTag)), | |
6683 | ||
6684 | Right_Opnd => | |
6685 | Make_Attribute_Reference (Loc, | |
6686 | Prefix => | |
6687 | New_Occurrence_Of (Designated_Type (R_Type), Loc), | |
6688 | Attribute_Name => Name_Tag))), | |
6689 | ||
6690 | Reason => CE_Tag_Check_Failed), | |
6691 | Suppress => All_Checks); | |
6692 | end if; | |
6693 | ||
6694 | -- AI05-0234: RM 6.5(21/3). Check access discriminants to | |
6695 | -- ensure that the function result does not outlive an | |
6696 | -- object designated by one of it discriminants. | |
6697 | ||
6698 | if Present (Extra_Accessibility_Of_Result (Scope_Id)) | |
6699 | and then Has_Unconstrained_Access_Discriminants (R_Type) | |
6700 | then | |
6701 | declare | |
6702 | Discrim_Source : Node_Id; | |
6703 | ||
6704 | procedure Check_Against_Result_Level (Level : Node_Id); | |
6705 | -- Check the given accessibility level against the level | |
6706 | -- determined by the point of call. (AI05-0234). | |
6707 | ||
6708 | -------------------------------- | |
6709 | -- Check_Against_Result_Level -- | |
6710 | -------------------------------- | |
6711 | ||
6712 | procedure Check_Against_Result_Level (Level : Node_Id) is | |
6713 | begin | |
6714 | Insert_Action (N, | |
6715 | Make_Raise_Program_Error (Loc, | |
6716 | Condition => | |
6717 | Make_Op_Gt (Loc, | |
6718 | Left_Opnd => Level, | |
6719 | Right_Opnd => | |
6720 | New_Occurrence_Of | |
6721 | (Extra_Accessibility_Of_Result (Scope_Id), Loc)), | |
6722 | Reason => PE_Accessibility_Check_Failed)); | |
6723 | end Check_Against_Result_Level; | |
6724 | ||
6725 | begin | |
6726 | Discrim_Source := Exp; | |
6727 | while Nkind (Discrim_Source) = N_Qualified_Expression loop | |
6728 | Discrim_Source := Expression (Discrim_Source); | |
6729 | end loop; | |
6730 | ||
6731 | if Nkind (Discrim_Source) = N_Identifier | |
6732 | and then Is_Return_Object (Entity (Discrim_Source)) | |
6733 | then | |
6734 | Discrim_Source := Entity (Discrim_Source); | |
6735 | ||
6736 | if Is_Constrained (Etype (Discrim_Source)) then | |
6737 | Discrim_Source := Etype (Discrim_Source); | |
6738 | else | |
6739 | Discrim_Source := Expression (Parent (Discrim_Source)); | |
6740 | end if; | |
6741 | ||
6742 | elsif Nkind (Discrim_Source) = N_Identifier | |
6743 | and then Nkind_In (Original_Node (Discrim_Source), | |
6744 | N_Aggregate, N_Extension_Aggregate) | |
6745 | then | |
6746 | Discrim_Source := Original_Node (Discrim_Source); | |
6747 | ||
6748 | elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then | |
6749 | Nkind (Original_Node (Discrim_Source)) = N_Function_Call | |
6750 | then | |
6751 | Discrim_Source := Original_Node (Discrim_Source); | |
6752 | end if; | |
6753 | ||
6754 | while Nkind_In (Discrim_Source, N_Qualified_Expression, | |
6755 | N_Type_Conversion, | |
6756 | N_Unchecked_Type_Conversion) | |
6757 | loop | |
6758 | Discrim_Source := Expression (Discrim_Source); | |
6759 | end loop; | |
6760 | ||
6761 | case Nkind (Discrim_Source) is | |
6762 | when N_Defining_Identifier => | |
6763 | pragma Assert (Is_Composite_Type (Discrim_Source) | |
6764 | and then Has_Discriminants (Discrim_Source) | |
6765 | and then Is_Constrained (Discrim_Source)); | |
6766 | ||
6767 | declare | |
6768 | Discrim : Entity_Id := | |
6769 | First_Discriminant (Base_Type (R_Type)); | |
6770 | Disc_Elmt : Elmt_Id := | |
6771 | First_Elmt (Discriminant_Constraint | |
6772 | (Discrim_Source)); | |
6773 | begin | |
6774 | loop | |
6775 | if Ekind (Etype (Discrim)) = | |
6776 | E_Anonymous_Access_Type | |
6777 | then | |
6778 | Check_Against_Result_Level | |
6779 | (Dynamic_Accessibility_Level (Node (Disc_Elmt))); | |
6780 | end if; | |
6781 | ||
6782 | Next_Elmt (Disc_Elmt); | |
6783 | Next_Discriminant (Discrim); | |
6784 | exit when not Present (Discrim); | |
6785 | end loop; | |
6786 | end; | |
6787 | ||
6788 | when N_Aggregate | |
6789 | | N_Extension_Aggregate | |
6790 | => | |
6791 | -- Unimplemented: extension aggregate case where discrims | |
6792 | -- come from ancestor part, not extension part. | |
6793 | ||
6794 | declare | |
6795 | Discrim : Entity_Id := | |
6796 | First_Discriminant (Base_Type (R_Type)); | |
6797 | ||
6798 | Disc_Exp : Node_Id := Empty; | |
6799 | ||
6800 | Positionals_Exhausted | |
6801 | : Boolean := not Present (Expressions | |
6802 | (Discrim_Source)); | |
6803 | ||
6804 | function Associated_Expr | |
6805 | (Comp_Id : Entity_Id; | |
6806 | Associations : List_Id) return Node_Id; | |
6807 | ||
6808 | -- Given a component and a component associations list, | |
6809 | -- locate the expression for that component; returns | |
6810 | -- Empty if no such expression is found. | |
6811 | ||
6812 | --------------------- | |
6813 | -- Associated_Expr -- | |
6814 | --------------------- | |
6815 | ||
6816 | function Associated_Expr | |
6817 | (Comp_Id : Entity_Id; | |
6818 | Associations : List_Id) return Node_Id | |
6819 | is | |
6820 | Assoc : Node_Id; | |
6821 | Choice : Node_Id; | |
6822 | ||
6823 | begin | |
6824 | -- Simple linear search seems ok here | |
6825 | ||
6826 | Assoc := First (Associations); | |
6827 | while Present (Assoc) loop | |
6828 | Choice := First (Choices (Assoc)); | |
6829 | while Present (Choice) loop | |
6830 | if (Nkind (Choice) = N_Identifier | |
6831 | and then Chars (Choice) = Chars (Comp_Id)) | |
6832 | or else (Nkind (Choice) = N_Others_Choice) | |
6833 | then | |
6834 | return Expression (Assoc); | |
6835 | end if; | |
6836 | ||
6837 | Next (Choice); | |
6838 | end loop; | |
6839 | ||
6840 | Next (Assoc); | |
6841 | end loop; | |
6842 | ||
6843 | return Empty; | |
6844 | end Associated_Expr; | |
6845 | ||
6846 | -- Start of processing for Expand_Simple_Function_Return | |
6847 | ||
6848 | begin | |
6849 | if not Positionals_Exhausted then | |
6850 | Disc_Exp := First (Expressions (Discrim_Source)); | |
6851 | end if; | |
6852 | ||
6853 | loop | |
6854 | if Positionals_Exhausted then | |
6855 | Disc_Exp := | |
6856 | Associated_Expr | |
6857 | (Discrim, | |
6858 | Component_Associations (Discrim_Source)); | |
6859 | end if; | |
6860 | ||
6861 | if Ekind (Etype (Discrim)) = | |
6862 | E_Anonymous_Access_Type | |
6863 | then | |
6864 | Check_Against_Result_Level | |
6865 | (Dynamic_Accessibility_Level (Disc_Exp)); | |
6866 | end if; | |
6867 | ||
6868 | Next_Discriminant (Discrim); | |
6869 | exit when not Present (Discrim); | |
6870 | ||
6871 | if not Positionals_Exhausted then | |
6872 | Next (Disc_Exp); | |
6873 | Positionals_Exhausted := not Present (Disc_Exp); | |
6874 | end if; | |
6875 | end loop; | |
6876 | end; | |
6877 | ||
6878 | when N_Function_Call => | |
6879 | ||
6880 | -- No check needed (check performed by callee) | |
6881 | ||
6882 | null; | |
6883 | ||
6884 | when others => | |
6885 | declare | |
6886 | Level : constant Node_Id := | |
6887 | Make_Integer_Literal (Loc, | |
6888 | Object_Access_Level (Discrim_Source)); | |
6889 | ||
6890 | begin | |
6891 | -- Unimplemented: check for name prefix that includes | |
6892 | -- a dereference of an access value with a dynamic | |
6893 | -- accessibility level (e.g., an access param or a | |
6894 | -- saooaaat) and use dynamic level in that case. For | |
6895 | -- example: | |
6896 | -- return Access_Param.all(Some_Index).Some_Component; | |
6897 | -- ??? | |
6898 | ||
6899 | Set_Etype (Level, Standard_Natural); | |
6900 | Check_Against_Result_Level (Level); | |
6901 | end; | |
6902 | end case; | |
6903 | end; | |
6904 | end if; | |
6905 | ||
6906 | -- If we are returning an object that may not be bit-aligned, then copy | |
6907 | -- the value into a temporary first. This copy may need to expand to a | |
6908 | -- loop of component operations. | |
6909 | ||
6910 | if Is_Possibly_Unaligned_Slice (Exp) | |
6911 | or else Is_Possibly_Unaligned_Object (Exp) | |
6912 | then | |
6913 | declare | |
6914 | ExpR : constant Node_Id := Relocate_Node (Exp); | |
6915 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR); | |
6916 | begin | |
6917 | Insert_Action (Exp, | |
6918 | Make_Object_Declaration (Loc, | |
6919 | Defining_Identifier => Tnn, | |
6920 | Constant_Present => True, | |
6921 | Object_Definition => New_Occurrence_Of (R_Type, Loc), | |
6922 | Expression => ExpR), | |
6923 | Suppress => All_Checks); | |
6924 | Rewrite (Exp, New_Occurrence_Of (Tnn, Loc)); | |
6925 | end; | |
6926 | end if; | |
6927 | ||
6928 | -- Call the _Postconditions procedure if the related function has | |
6929 | -- contract assertions that need to be verified on exit. | |
6930 | ||
6931 | if Ekind (Scope_Id) = E_Function | |
6932 | and then Present (Postconditions_Proc (Scope_Id)) | |
6933 | then | |
6934 | -- In the case of discriminated objects, we have created a | |
6935 | -- constrained subtype above, and used the underlying type. This | |
6936 | -- transformation is post-analysis and harmless, except that now the | |
6937 | -- call to the post-condition will be analyzed and the type kinds | |
6938 | -- have to match. | |
6939 | ||
6940 | if Nkind (Exp) = N_Unchecked_Type_Conversion | |
6941 | and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp)) | |
6942 | then | |
6943 | Rewrite (Exp, Expression (Relocate_Node (Exp))); | |
6944 | end if; | |
6945 | ||
6946 | -- We are going to reference the returned value twice in this case, | |
6947 | -- once in the call to _Postconditions, and once in the actual return | |
6948 | -- statement, but we can't have side effects happening twice. | |
6949 | ||
6950 | Force_Evaluation (Exp, Mode => Strict); | |
6951 | ||
6952 | -- Generate call to _Postconditions | |
6953 | ||
6954 | Insert_Action (Exp, | |
6955 | Make_Procedure_Call_Statement (Loc, | |
6956 | Name => | |
6957 | New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc), | |
6958 | Parameter_Associations => New_List (New_Copy_Tree (Exp)))); | |
6959 | end if; | |
6960 | ||
6961 | -- Ada 2005 (AI-251): If this return statement corresponds with an | |
6962 | -- simple return statement associated with an extended return statement | |
6963 | -- and the type of the returned object is an interface then generate an | |
6964 | -- implicit conversion to force displacement of the "this" pointer. | |
6965 | ||
6966 | if Ada_Version >= Ada_2005 | |
6967 | and then Comes_From_Extended_Return_Statement (N) | |
6968 | and then Nkind (Expression (N)) = N_Identifier | |
6969 | and then Is_Interface (Utyp) | |
6970 | and then Utyp /= Underlying_Type (Exptyp) | |
6971 | then | |
6972 | Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp))); | |
6973 | Analyze_And_Resolve (Exp); | |
6974 | end if; | |
6975 | end Expand_Simple_Function_Return; | |
6976 | ||
6977 | -------------------------------------------- | |
6978 | -- Has_Unconstrained_Access_Discriminants -- | |
6979 | -------------------------------------------- | |
6980 | ||
6981 | function Has_Unconstrained_Access_Discriminants | |
6982 | (Subtyp : Entity_Id) return Boolean | |
6983 | is | |
6984 | Discr : Entity_Id; | |
6985 | ||
6986 | begin | |
6987 | if Has_Discriminants (Subtyp) | |
6988 | and then not Is_Constrained (Subtyp) | |
6989 | then | |
6990 | Discr := First_Discriminant (Subtyp); | |
6991 | while Present (Discr) loop | |
6992 | if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then | |
6993 | return True; | |
6994 | end if; | |
6995 | ||
6996 | Next_Discriminant (Discr); | |
6997 | end loop; | |
6998 | end if; | |
6999 | ||
7000 | return False; | |
7001 | end Has_Unconstrained_Access_Discriminants; | |
7002 | ||
7003 | -------------------------------- | |
7004 | -- Is_Build_In_Place_Function -- | |
7005 | -------------------------------- | |
7006 | ||
7007 | function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is | |
7008 | begin | |
7009 | -- This function is called from Expand_Subtype_From_Expr during | |
7010 | -- semantic analysis, even when expansion is off. In those cases | |
7011 | -- the build_in_place expansion will not take place. | |
7012 | ||
7013 | if not Expander_Active then | |
7014 | return False; | |
7015 | end if; | |
7016 | ||
7017 | -- For now we test whether E denotes a function or access-to-function | |
7018 | -- type whose result subtype is inherently limited. Later this test | |
7019 | -- may be revised to allow composite nonlimited types. Functions with | |
7020 | -- a foreign convention or whose result type has a foreign convention | |
7021 | -- never qualify. | |
7022 | ||
7023 | if Ekind_In (E, E_Function, E_Generic_Function) | |
7024 | or else (Ekind (E) = E_Subprogram_Type | |
7025 | and then Etype (E) /= Standard_Void_Type) | |
7026 | then | |
7027 | -- Note: If the function has a foreign convention, it cannot build | |
7028 | -- its result in place, so you're on your own. On the other hand, | |
7029 | -- if only the return type has a foreign convention, its layout is | |
7030 | -- intended to be compatible with the other language, but the build- | |
7031 | -- in place machinery can ensure that the object is not copied. | |
7032 | ||
7033 | if Has_Foreign_Convention (E) then | |
7034 | return False; | |
7035 | ||
7036 | -- In Ada 2005 all functions with an inherently limited return type | |
7037 | -- must be handled using a build-in-place profile, including the case | |
7038 | -- of a function with a limited interface result, where the function | |
7039 | -- may return objects of nonlimited descendants. | |
7040 | ||
7041 | else | |
7042 | return Is_Limited_View (Etype (E)) | |
7043 | and then Ada_Version >= Ada_2005 | |
7044 | and then not Debug_Flag_Dot_L; | |
7045 | end if; | |
7046 | ||
7047 | else | |
7048 | return False; | |
7049 | end if; | |
7050 | end Is_Build_In_Place_Function; | |
7051 | ||
7052 | ------------------------------------- | |
7053 | -- Is_Build_In_Place_Function_Call -- | |
7054 | ------------------------------------- | |
7055 | ||
7056 | function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is | |
7057 | Exp_Node : Node_Id := N; | |
7058 | Function_Id : Entity_Id; | |
7059 | ||
7060 | begin | |
7061 | -- Return False if the expander is currently inactive, since awareness | |
7062 | -- of build-in-place treatment is only relevant during expansion. Note | |
7063 | -- that Is_Build_In_Place_Function, which is called as part of this | |
7064 | -- function, is also conditioned this way, but we need to check here as | |
7065 | -- well to avoid blowing up on processing protected calls when expansion | |
7066 | -- is disabled (such as with -gnatc) since those would trip over the | |
7067 | -- raise of Program_Error below. | |
7068 | ||
7069 | -- In SPARK mode, build-in-place calls are not expanded, so that we | |
7070 | -- may end up with a call that is neither resolved to an entity, nor | |
7071 | -- an indirect call. | |
7072 | ||
7073 | if not Expander_Active then | |
7074 | return False; | |
7075 | end if; | |
7076 | ||
7077 | -- Step past qualification, type conversion (which can occur in actual | |
7078 | -- parameter contexts), and unchecked conversion (which can occur in | |
7079 | -- cases of calls to 'Input). | |
7080 | ||
7081 | if Nkind_In (Exp_Node, N_Qualified_Expression, | |
7082 | N_Type_Conversion, | |
7083 | N_Unchecked_Type_Conversion) | |
7084 | then | |
7085 | Exp_Node := Expression (N); | |
7086 | end if; | |
7087 | ||
7088 | if Nkind (Exp_Node) /= N_Function_Call then | |
7089 | return False; | |
7090 | ||
7091 | else | |
7092 | if Is_Entity_Name (Name (Exp_Node)) then | |
7093 | Function_Id := Entity (Name (Exp_Node)); | |
7094 | ||
7095 | -- In the case of an explicitly dereferenced call, use the subprogram | |
7096 | -- type generated for the dereference. | |
7097 | ||
7098 | elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then | |
7099 | Function_Id := Etype (Name (Exp_Node)); | |
7100 | ||
7101 | -- This may be a call to a protected function. | |
7102 | ||
7103 | elsif Nkind (Name (Exp_Node)) = N_Selected_Component then | |
7104 | Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node)))); | |
7105 | ||
7106 | else | |
7107 | raise Program_Error; | |
7108 | end if; | |
7109 | ||
7110 | return Is_Build_In_Place_Function (Function_Id); | |
7111 | end if; | |
7112 | end Is_Build_In_Place_Function_Call; | |
7113 | ||
7114 | ----------------------- | |
7115 | -- Freeze_Subprogram -- | |
7116 | ----------------------- | |
7117 | ||
7118 | procedure Freeze_Subprogram (N : Node_Id) is | |
7119 | Loc : constant Source_Ptr := Sloc (N); | |
7120 | ||
7121 | procedure Register_Predefined_DT_Entry (Prim : Entity_Id); | |
7122 | -- (Ada 2005): Register a predefined primitive in all the secondary | |
7123 | -- dispatch tables of its primitive type. | |
7124 | ||
7125 | ---------------------------------- | |
7126 | -- Register_Predefined_DT_Entry -- | |
7127 | ---------------------------------- | |
7128 | ||
7129 | procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is | |
7130 | Iface_DT_Ptr : Elmt_Id; | |
7131 | Tagged_Typ : Entity_Id; | |
7132 | Thunk_Id : Entity_Id; | |
7133 | Thunk_Code : Node_Id; | |
7134 | ||
7135 | begin | |
7136 | Tagged_Typ := Find_Dispatching_Type (Prim); | |
7137 | ||
7138 | if No (Access_Disp_Table (Tagged_Typ)) | |
7139 | or else not Has_Interfaces (Tagged_Typ) | |
7140 | or else not RTE_Available (RE_Interface_Tag) | |
7141 | or else Restriction_Active (No_Dispatching_Calls) | |
7142 | then | |
7143 | return; | |
7144 | end if; | |
7145 | ||
7146 | -- Skip the first two access-to-dispatch-table pointers since they | |
7147 | -- leads to the primary dispatch table (predefined DT and user | |
7148 | -- defined DT). We are only concerned with the secondary dispatch | |
7149 | -- table pointers. Note that the access-to- dispatch-table pointer | |
7150 | -- corresponds to the first implemented interface retrieved below. | |
7151 | ||
7152 | Iface_DT_Ptr := | |
7153 | Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ)))); | |
7154 | ||
7155 | while Present (Iface_DT_Ptr) | |
7156 | and then Ekind (Node (Iface_DT_Ptr)) = E_Constant | |
7157 | loop | |
7158 | pragma Assert (Has_Thunks (Node (Iface_DT_Ptr))); | |
7159 | Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code); | |
7160 | ||
7161 | if Present (Thunk_Code) then | |
7162 | Insert_Actions_After (N, New_List ( | |
7163 | Thunk_Code, | |
7164 | ||
7165 | Build_Set_Predefined_Prim_Op_Address (Loc, | |
7166 | Tag_Node => | |
7167 | New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc), | |
7168 | Position => DT_Position (Prim), | |
7169 | Address_Node => | |
7170 | Unchecked_Convert_To (RTE (RE_Prim_Ptr), | |
7171 | Make_Attribute_Reference (Loc, | |
7172 | Prefix => New_Occurrence_Of (Thunk_Id, Loc), | |
7173 | Attribute_Name => Name_Unrestricted_Access))), | |
7174 | ||
7175 | Build_Set_Predefined_Prim_Op_Address (Loc, | |
7176 | Tag_Node => | |
7177 | New_Occurrence_Of | |
7178 | (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))), | |
7179 | Loc), | |
7180 | Position => DT_Position (Prim), | |
7181 | Address_Node => | |
7182 | Unchecked_Convert_To (RTE (RE_Prim_Ptr), | |
7183 | Make_Attribute_Reference (Loc, | |
7184 | Prefix => New_Occurrence_Of (Prim, Loc), | |
7185 | Attribute_Name => Name_Unrestricted_Access))))); | |
7186 | end if; | |
7187 | ||
7188 | -- Skip the tag of the predefined primitives dispatch table | |
7189 | ||
7190 | Next_Elmt (Iface_DT_Ptr); | |
7191 | pragma Assert (Has_Thunks (Node (Iface_DT_Ptr))); | |
7192 | ||
7193 | -- Skip tag of the no-thunks dispatch table | |
7194 | ||
7195 | Next_Elmt (Iface_DT_Ptr); | |
7196 | pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr))); | |
7197 | ||
7198 | -- Skip tag of predefined primitives no-thunks dispatch table | |
7199 | ||
7200 | Next_Elmt (Iface_DT_Ptr); | |
7201 | pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr))); | |
7202 | ||
7203 | Next_Elmt (Iface_DT_Ptr); | |
7204 | end loop; | |
7205 | end Register_Predefined_DT_Entry; | |
7206 | ||
7207 | -- Local variables | |
7208 | ||
7209 | Subp : constant Entity_Id := Entity (N); | |
7210 | ||
7211 | -- Start of processing for Freeze_Subprogram | |
7212 | ||
7213 | begin | |
7214 | -- We suppress the initialization of the dispatch table entry when | |
7215 | -- not Tagged_Type_Expansion because the dispatching mechanism is | |
7216 | -- handled internally by the target. | |
7217 | ||
7218 | if Is_Dispatching_Operation (Subp) | |
7219 | and then not Is_Abstract_Subprogram (Subp) | |
7220 | and then Present (DTC_Entity (Subp)) | |
7221 | and then Present (Scope (DTC_Entity (Subp))) | |
7222 | and then Tagged_Type_Expansion | |
7223 | and then not Restriction_Active (No_Dispatching_Calls) | |
7224 | and then RTE_Available (RE_Tag) | |
7225 | then | |
7226 | declare | |
7227 | Typ : constant Entity_Id := Scope (DTC_Entity (Subp)); | |
7228 | ||
7229 | begin | |
7230 | -- Handle private overridden primitives | |
7231 | ||
7232 | if not Is_CPP_Class (Typ) then | |
7233 | Check_Overriding_Operation (Subp); | |
7234 | end if; | |
7235 | ||
7236 | -- We assume that imported CPP primitives correspond with objects | |
7237 | -- whose constructor is in the CPP side; therefore we don't need | |
7238 | -- to generate code to register them in the dispatch table. | |
7239 | ||
7240 | if Is_CPP_Class (Typ) then | |
7241 | null; | |
7242 | ||
7243 | -- Handle CPP primitives found in derivations of CPP_Class types. | |
7244 | -- These primitives must have been inherited from some parent, and | |
7245 | -- there is no need to register them in the dispatch table because | |
7246 | -- Build_Inherit_Prims takes care of initializing these slots. | |
7247 | ||
7248 | elsif Is_Imported (Subp) | |
7249 | and then (Convention (Subp) = Convention_CPP | |
7250 | or else Convention (Subp) = Convention_C) | |
7251 | then | |
7252 | null; | |
7253 | ||
7254 | -- Generate code to register the primitive in non statically | |
7255 | -- allocated dispatch tables | |
7256 | ||
7257 | elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then | |
7258 | ||
7259 | -- When a primitive is frozen, enter its name in its dispatch | |
7260 | -- table slot. | |
7261 | ||
7262 | if not Is_Interface (Typ) | |
7263 | or else Present (Interface_Alias (Subp)) | |
7264 | then | |
7265 | if Is_Predefined_Dispatching_Operation (Subp) then | |
7266 | Register_Predefined_DT_Entry (Subp); | |
7267 | end if; | |
7268 | ||
7269 | Insert_Actions_After (N, | |
7270 | Register_Primitive (Loc, Prim => Subp)); | |
7271 | end if; | |
7272 | end if; | |
7273 | end; | |
7274 | end if; | |
7275 | ||
7276 | -- Mark functions that return by reference. Note that it cannot be part | |
7277 | -- of the normal semantic analysis of the spec since the underlying | |
7278 | -- returned type may not be known yet (for private types). | |
7279 | ||
7280 | declare | |
7281 | Typ : constant Entity_Id := Etype (Subp); | |
7282 | Utyp : constant Entity_Id := Underlying_Type (Typ); | |
7283 | ||
7284 | begin | |
7285 | if Is_Limited_View (Typ) then | |
7286 | Set_Returns_By_Ref (Subp); | |
7287 | ||
7288 | elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then | |
7289 | Set_Returns_By_Ref (Subp); | |
7290 | end if; | |
7291 | end; | |
7292 | ||
7293 | -- Wnen freezing a null procedure, analyze its delayed aspects now | |
7294 | -- because we may not have reached the end of the declarative list when | |
7295 | -- delayed aspects are normally analyzed. This ensures that dispatching | |
7296 | -- calls are properly rewritten when the generated _Postcondition | |
7297 | -- procedure is analyzed in the null procedure body. | |
7298 | ||
7299 | if Nkind (Parent (Subp)) = N_Procedure_Specification | |
7300 | and then Null_Present (Parent (Subp)) | |
7301 | then | |
7302 | Analyze_Entry_Or_Subprogram_Contract (Subp); | |
7303 | end if; | |
7304 | end Freeze_Subprogram; | |
7305 | ||
7306 | ------------------------------ | |
7307 | -- Insert_Post_Call_Actions -- | |
7308 | ------------------------------ | |
7309 | ||
7310 | procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id) is | |
7311 | begin | |
7312 | if Is_Empty_List (Post_Call) then | |
7313 | return; | |
7314 | end if; | |
7315 | ||
7316 | -- Cases where the call is not a member of a statement list. This | |
7317 | -- includes the case where the call is an actual in another function | |
7318 | -- call or indexing, i.e. an expression context as well. | |
7319 | ||
7320 | if not Is_List_Member (N) | |
7321 | or else Nkind_In (Parent (N), N_Function_Call, N_Indexed_Component) | |
7322 | then | |
7323 | -- In Ada 2012 the call may be a function call in an expression | |
7324 | -- (since OUT and IN OUT parameters are now allowed for such calls). | |
7325 | -- The write-back of (in)-out parameters is handled by the back-end, | |
7326 | -- but the constraint checks generated when subtypes of formal and | |
7327 | -- actual don't match must be inserted in the form of assignments. | |
7328 | ||
7329 | if Nkind (Original_Node (N)) = N_Function_Call then | |
7330 | pragma Assert (Ada_Version >= Ada_2012); | |
7331 | -- Functions with '[in] out' parameters are only allowed in Ada | |
7332 | -- 2012. | |
7333 | ||
7334 | -- We used to handle this by climbing up parents to a | |
7335 | -- non-statement/declaration and then simply making a call to | |
7336 | -- Insert_Actions_After (P, Post_Call), but that doesn't work | |
7337 | -- for Ada 2012. If we are in the middle of an expression, e.g. | |
7338 | -- the condition of an IF, this call would insert after the IF | |
7339 | -- statement, which is much too late to be doing the write back. | |
7340 | -- For example: | |
7341 | ||
7342 | -- if Clobber (X) then | |
7343 | -- Put_Line (X'Img); | |
7344 | -- else | |
7345 | -- goto Junk | |
7346 | -- end if; | |
7347 | ||
7348 | -- Now assume Clobber changes X, if we put the write back after | |
7349 | -- the IF, the Put_Line gets the wrong value and the goto causes | |
7350 | -- the write back to be skipped completely. | |
7351 | ||
7352 | -- To deal with this, we replace the call by | |
7353 | ||
7354 | -- do | |
7355 | -- Tnnn : constant function-result-type := function-call; | |
7356 | -- Post_Call actions | |
7357 | -- in | |
7358 | -- Tnnn; | |
7359 | -- end; | |
7360 | ||
7361 | declare | |
7362 | Loc : constant Source_Ptr := Sloc (N); | |
7363 | Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T'); | |
7364 | FRTyp : constant Entity_Id := Etype (N); | |
7365 | Name : constant Node_Id := Relocate_Node (N); | |
7366 | ||
7367 | begin | |
7368 | Prepend_To (Post_Call, | |
7369 | Make_Object_Declaration (Loc, | |
7370 | Defining_Identifier => Tnnn, | |
7371 | Object_Definition => New_Occurrence_Of (FRTyp, Loc), | |
7372 | Constant_Present => True, | |
7373 | Expression => Name)); | |
7374 | ||
7375 | Rewrite (N, | |
7376 | Make_Expression_With_Actions (Loc, | |
7377 | Actions => Post_Call, | |
7378 | Expression => New_Occurrence_Of (Tnnn, Loc))); | |
7379 | ||
7380 | -- We don't want to just blindly call Analyze_And_Resolve | |
7381 | -- because that would cause unwanted recursion on the call. | |
7382 | -- So for a moment set the call as analyzed to prevent that | |
7383 | -- recursion, and get the rest analyzed properly, then reset | |
7384 | -- the analyzed flag, so our caller can continue. | |
7385 | ||
7386 | Set_Analyzed (Name, True); | |
7387 | Analyze_And_Resolve (N, FRTyp); | |
7388 | Set_Analyzed (Name, False); | |
7389 | end; | |
7390 | ||
7391 | -- If not the special Ada 2012 case of a function call, then we must | |
7392 | -- have the triggering statement of a triggering alternative or an | |
7393 | -- entry call alternative, and we can add the post call stuff to the | |
7394 | -- corresponding statement list. | |
7395 | ||
7396 | else | |
7397 | declare | |
7398 | P : Node_Id; | |
7399 | ||
7400 | begin | |
7401 | P := Parent (N); | |
7402 | pragma Assert (Nkind_In (P, N_Entry_Call_Alternative, | |
7403 | N_Triggering_Alternative)); | |
7404 | ||
7405 | if Is_Non_Empty_List (Statements (P)) then | |
7406 | Insert_List_Before_And_Analyze | |
7407 | (First (Statements (P)), Post_Call); | |
7408 | else | |
7409 | Set_Statements (P, Post_Call); | |
7410 | end if; | |
7411 | end; | |
7412 | end if; | |
7413 | ||
7414 | -- Otherwise, normal case where N is in a statement sequence, just put | |
7415 | -- the post-call stuff after the call statement. | |
7416 | ||
7417 | else | |
7418 | Insert_Actions_After (N, Post_Call); | |
7419 | end if; | |
7420 | end Insert_Post_Call_Actions; | |
7421 | ||
7422 | ----------------------- | |
7423 | -- Is_Null_Procedure -- | |
7424 | ----------------------- | |
7425 | ||
7426 | function Is_Null_Procedure (Subp : Entity_Id) return Boolean is | |
7427 | Decl : constant Node_Id := Unit_Declaration_Node (Subp); | |
7428 | ||
7429 | begin | |
7430 | if Ekind (Subp) /= E_Procedure then | |
7431 | return False; | |
7432 | ||
7433 | -- Check if this is a declared null procedure | |
7434 | ||
7435 | elsif Nkind (Decl) = N_Subprogram_Declaration then | |
7436 | if not Null_Present (Specification (Decl)) then | |
7437 | return False; | |
7438 | ||
7439 | elsif No (Body_To_Inline (Decl)) then | |
7440 | return False; | |
7441 | ||
7442 | -- Check if the body contains only a null statement, followed by | |
7443 | -- the return statement added during expansion. | |
7444 | ||
7445 | else | |
7446 | declare | |
7447 | Orig_Bod : constant Node_Id := Body_To_Inline (Decl); | |
7448 | ||
7449 | Stat : Node_Id; | |
7450 | Stat2 : Node_Id; | |
7451 | ||
7452 | begin | |
7453 | if Nkind (Orig_Bod) /= N_Subprogram_Body then | |
7454 | return False; | |
7455 | else | |
7456 | -- We must skip SCIL nodes because they are currently | |
7457 | -- implemented as special N_Null_Statement nodes. | |
7458 | ||
7459 | Stat := | |
7460 | First_Non_SCIL_Node | |
7461 | (Statements (Handled_Statement_Sequence (Orig_Bod))); | |
7462 | Stat2 := Next_Non_SCIL_Node (Stat); | |
7463 | ||
7464 | return | |
7465 | Is_Empty_List (Declarations (Orig_Bod)) | |
7466 | and then Nkind (Stat) = N_Null_Statement | |
7467 | and then | |
7468 | (No (Stat2) | |
7469 | or else | |
7470 | (Nkind (Stat2) = N_Simple_Return_Statement | |
7471 | and then No (Next (Stat2)))); | |
7472 | end if; | |
7473 | end; | |
7474 | end if; | |
7475 | ||
7476 | else | |
7477 | return False; | |
7478 | end if; | |
7479 | end Is_Null_Procedure; | |
7480 | ||
7481 | ------------------------------------------- | |
7482 | -- Make_Build_In_Place_Call_In_Allocator -- | |
7483 | ------------------------------------------- | |
7484 | ||
7485 | procedure Make_Build_In_Place_Call_In_Allocator | |
7486 | (Allocator : Node_Id; | |
7487 | Function_Call : Node_Id) | |
7488 | is | |
7489 | Acc_Type : constant Entity_Id := Etype (Allocator); | |
7490 | Loc : Source_Ptr; | |
7491 | Func_Call : Node_Id := Function_Call; | |
7492 | Ref_Func_Call : Node_Id; | |
7493 | Function_Id : Entity_Id; | |
7494 | Result_Subt : Entity_Id; | |
7495 | New_Allocator : Node_Id; | |
7496 | Return_Obj_Access : Entity_Id; -- temp for function result | |
7497 | Temp_Init : Node_Id; -- initial value of Return_Obj_Access | |
7498 | Alloc_Form : BIP_Allocation_Form; | |
7499 | Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool | |
7500 | Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case | |
7501 | Chain : Entity_Id; -- activation chain, in case of tasks | |
7502 | ||
7503 | begin | |
7504 | -- Step past qualification or unchecked conversion (the latter can occur | |
7505 | -- in cases of calls to 'Input). | |
7506 | ||
7507 | if Nkind_In (Func_Call, | |
7508 | N_Qualified_Expression, | |
7509 | N_Type_Conversion, | |
7510 | N_Unchecked_Type_Conversion) | |
7511 | then | |
7512 | Func_Call := Expression (Func_Call); | |
7513 | end if; | |
7514 | ||
7515 | -- If the call has already been processed to add build-in-place actuals | |
7516 | -- then return. This should not normally occur in an allocator context, | |
7517 | -- but we add the protection as a defensive measure. | |
7518 | ||
7519 | if Is_Expanded_Build_In_Place_Call (Func_Call) then | |
7520 | return; | |
7521 | end if; | |
7522 | ||
7523 | -- Mark the call as processed as a build-in-place call | |
7524 | ||
7525 | Set_Is_Expanded_Build_In_Place_Call (Func_Call); | |
7526 | ||
7527 | Loc := Sloc (Function_Call); | |
7528 | ||
7529 | if Is_Entity_Name (Name (Func_Call)) then | |
7530 | Function_Id := Entity (Name (Func_Call)); | |
7531 | ||
7532 | elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then | |
7533 | Function_Id := Etype (Name (Func_Call)); | |
7534 | ||
7535 | else | |
7536 | raise Program_Error; | |
7537 | end if; | |
7538 | ||
7539 | Result_Subt := Available_View (Etype (Function_Id)); | |
7540 | ||
7541 | -- Create a temp for the function result. In the caller-allocates case, | |
7542 | -- this will be initialized to the result of a new uninitialized | |
7543 | -- allocator. Note: we do not use Allocator as the Related_Node of | |
7544 | -- Return_Obj_Access in call to Make_Temporary below as this would | |
7545 | -- create a sort of infinite "recursion". | |
7546 | ||
7547 | Return_Obj_Access := Make_Temporary (Loc, 'R'); | |
7548 | Set_Etype (Return_Obj_Access, Acc_Type); | |
7549 | ||
7550 | -- When the result subtype is constrained, the return object is | |
7551 | -- allocated on the caller side, and access to it is passed to the | |
7552 | -- function. | |
7553 | ||
7554 | -- Here and in related routines, we must examine the full view of the | |
7555 | -- type, because the view at the point of call may differ from that | |
7556 | -- that in the function body, and the expansion mechanism depends on | |
7557 | -- the characteristics of the full view. | |
7558 | ||
7559 | if Is_Constrained (Underlying_Type (Result_Subt)) then | |
7560 | ||
7561 | -- Replace the initialized allocator of form "new T'(Func (...))" | |
7562 | -- with an uninitialized allocator of form "new T", where T is the | |
7563 | -- result subtype of the called function. The call to the function | |
7564 | -- is handled separately further below. | |
7565 | ||
7566 | New_Allocator := | |
7567 | Make_Allocator (Loc, | |
7568 | Expression => New_Occurrence_Of (Result_Subt, Loc)); | |
7569 | Set_No_Initialization (New_Allocator); | |
7570 | ||
7571 | -- Copy attributes to new allocator. Note that the new allocator | |
7572 | -- logically comes from source if the original one did, so copy the | |
7573 | -- relevant flag. This ensures proper treatment of the restriction | |
7574 | -- No_Implicit_Heap_Allocations in this case. | |
7575 | ||
7576 | Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator)); | |
7577 | Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator)); | |
7578 | Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator)); | |
7579 | ||
7580 | Rewrite (Allocator, New_Allocator); | |
7581 | ||
7582 | -- Initial value of the temp is the result of the uninitialized | |
7583 | -- allocator | |
7584 | ||
7585 | Temp_Init := Relocate_Node (Allocator); | |
7586 | ||
7587 | -- Indicate that caller allocates, and pass in the return object | |
7588 | ||
7589 | Alloc_Form := Caller_Allocation; | |
7590 | Pool := Make_Null (No_Location); | |
7591 | Return_Obj_Actual := | |
7592 | Make_Unchecked_Type_Conversion (Loc, | |
7593 | Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc), | |
7594 | Expression => | |
7595 | Make_Explicit_Dereference (Loc, | |
7596 | Prefix => New_Occurrence_Of (Return_Obj_Access, Loc))); | |
7597 | ||
7598 | -- When the result subtype is unconstrained, the function itself must | |
7599 | -- perform the allocation of the return object, so we pass parameters | |
7600 | -- indicating that. | |
7601 | ||
7602 | else | |
7603 | Temp_Init := Empty; | |
7604 | ||
7605 | -- Case of a user-defined storage pool. Pass an allocation parameter | |
7606 | -- indicating that the function should allocate its result in the | |
7607 | -- pool, and pass the pool. Use 'Unrestricted_Access because the | |
7608 | -- pool may not be aliased. | |
7609 | ||
7610 | if Present (Associated_Storage_Pool (Acc_Type)) then | |
7611 | Alloc_Form := User_Storage_Pool; | |
7612 | Pool := | |
7613 | Make_Attribute_Reference (Loc, | |
7614 | Prefix => | |
7615 | New_Occurrence_Of | |
7616 | (Associated_Storage_Pool (Acc_Type), Loc), | |
7617 | Attribute_Name => Name_Unrestricted_Access); | |
7618 | ||
7619 | -- No user-defined pool; pass an allocation parameter indicating that | |
7620 | -- the function should allocate its result on the heap. | |
7621 | ||
7622 | else | |
7623 | Alloc_Form := Global_Heap; | |
7624 | Pool := Make_Null (No_Location); | |
7625 | end if; | |
7626 | ||
7627 | -- The caller does not provide the return object in this case, so we | |
7628 | -- have to pass null for the object access actual. | |
7629 | ||
7630 | Return_Obj_Actual := Empty; | |
7631 | end if; | |
7632 | ||
7633 | -- Declare the temp object | |
7634 | ||
7635 | Insert_Action (Allocator, | |
7636 | Make_Object_Declaration (Loc, | |
7637 | Defining_Identifier => Return_Obj_Access, | |
7638 | Object_Definition => New_Occurrence_Of (Acc_Type, Loc), | |
7639 | Expression => Temp_Init)); | |
7640 | ||
7641 | Ref_Func_Call := Make_Reference (Loc, Func_Call); | |
7642 | ||
7643 | -- Ada 2005 (AI-251): If the type of the allocator is an interface | |
7644 | -- then generate an implicit conversion to force displacement of the | |
7645 | -- "this" pointer. | |
7646 | ||
7647 | if Is_Interface (Designated_Type (Acc_Type)) then | |
7648 | Rewrite | |
7649 | (Ref_Func_Call, | |
7650 | OK_Convert_To (Acc_Type, Ref_Func_Call)); | |
7651 | end if; | |
7652 | ||
7653 | declare | |
7654 | Assign : constant Node_Id := | |
7655 | Make_Assignment_Statement (Loc, | |
7656 | Name => New_Occurrence_Of (Return_Obj_Access, Loc), | |
7657 | Expression => Ref_Func_Call); | |
7658 | -- Assign the result of the function call into the temp. In the | |
7659 | -- caller-allocates case, this is overwriting the temp with its | |
7660 | -- initial value, which has no effect. In the callee-allocates case, | |
7661 | -- this is setting the temp to point to the object allocated by the | |
7662 | -- callee. | |
7663 | ||
7664 | Actions : List_Id; | |
7665 | -- Actions to be inserted. If there are no tasks, this is just the | |
7666 | -- assignment statement. If the allocated object has tasks, we need | |
7667 | -- to wrap the assignment in a block that activates them. The | |
7668 | -- activation chain of that block must be passed to the function, | |
7669 | -- rather than some outer chain. | |
7670 | begin | |
7671 | if Has_Task (Result_Subt) then | |
7672 | Actions := New_List; | |
7673 | Build_Task_Allocate_Block_With_Init_Stmts | |
7674 | (Actions, Allocator, Init_Stmts => New_List (Assign)); | |
7675 | Chain := Activation_Chain_Entity (Last (Actions)); | |
7676 | else | |
7677 | Actions := New_List (Assign); | |
7678 | Chain := Empty; | |
7679 | end if; | |
7680 | ||
7681 | Insert_Actions (Allocator, Actions); | |
7682 | end; | |
7683 | ||
7684 | -- When the function has a controlling result, an allocation-form | |
7685 | -- parameter must be passed indicating that the caller is allocating | |
7686 | -- the result object. This is needed because such a function can be | |
7687 | -- called as a dispatching operation and must be treated similarly | |
7688 | -- to functions with unconstrained result subtypes. | |
7689 | ||
7690 | Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
7691 | (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool); | |
7692 | ||
7693 | Add_Finalization_Master_Actual_To_Build_In_Place_Call | |
7694 | (Func_Call, Function_Id, Acc_Type); | |
7695 | ||
7696 | Add_Task_Actuals_To_Build_In_Place_Call | |
7697 | (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type), | |
7698 | Chain => Chain); | |
7699 | ||
7700 | -- Add an implicit actual to the function call that provides access | |
7701 | -- to the allocated object. An unchecked conversion to the (specific) | |
7702 | -- result subtype of the function is inserted to handle cases where | |
7703 | -- the access type of the allocator has a class-wide designated type. | |
7704 | ||
7705 | Add_Access_Actual_To_Build_In_Place_Call | |
7706 | (Func_Call, Function_Id, Return_Obj_Actual); | |
7707 | ||
7708 | -- Finally, replace the allocator node with a reference to the temp | |
7709 | ||
7710 | Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc)); | |
7711 | ||
7712 | Analyze_And_Resolve (Allocator, Acc_Type); | |
7713 | end Make_Build_In_Place_Call_In_Allocator; | |
7714 | ||
7715 | --------------------------------------------------- | |
7716 | -- Make_Build_In_Place_Call_In_Anonymous_Context -- | |
7717 | --------------------------------------------------- | |
7718 | ||
7719 | procedure Make_Build_In_Place_Call_In_Anonymous_Context | |
7720 | (Function_Call : Node_Id) | |
7721 | is | |
7722 | Loc : Source_Ptr; | |
7723 | Func_Call : Node_Id := Function_Call; | |
7724 | Function_Id : Entity_Id; | |
7725 | Result_Subt : Entity_Id; | |
7726 | Return_Obj_Id : Entity_Id; | |
7727 | Return_Obj_Decl : Entity_Id; | |
7728 | ||
7729 | Definite : Boolean; | |
7730 | -- True if result subtype is definite, or has a size that does not | |
7731 | -- require secondary stack usage (i.e. no variant part or components | |
7732 | -- whose type depends on discriminants). In particular, untagged types | |
7733 | -- with only access discriminants do not require secondary stack use. | |
7734 | -- Note that if the return type is tagged we must always use the sec. | |
7735 | -- stack because the call may dispatch on result. | |
7736 | ||
7737 | begin | |
7738 | -- Step past qualification, type conversion (which can occur in actual | |
7739 | -- parameter contexts), and unchecked conversion (which can occur in | |
7740 | -- cases of calls to 'Input). | |
7741 | ||
7742 | if Nkind_In (Func_Call, N_Qualified_Expression, | |
7743 | N_Type_Conversion, | |
7744 | N_Unchecked_Type_Conversion) | |
7745 | then | |
7746 | Func_Call := Expression (Func_Call); | |
7747 | end if; | |
7748 | ||
7749 | -- If the call has already been processed to add build-in-place actuals | |
7750 | -- then return. One place this can occur is for calls to build-in-place | |
7751 | -- functions that occur within a call to a protected operation, where | |
7752 | -- due to rewriting and expansion of the protected call there can be | |
7753 | -- more than one call to Expand_Actuals for the same set of actuals. | |
7754 | ||
7755 | if Is_Expanded_Build_In_Place_Call (Func_Call) then | |
7756 | return; | |
7757 | end if; | |
7758 | ||
7759 | -- Mark the call as processed as a build-in-place call | |
7760 | ||
7761 | Set_Is_Expanded_Build_In_Place_Call (Func_Call); | |
7762 | ||
7763 | Loc := Sloc (Function_Call); | |
7764 | ||
7765 | if Is_Entity_Name (Name (Func_Call)) then | |
7766 | Function_Id := Entity (Name (Func_Call)); | |
7767 | ||
7768 | elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then | |
7769 | Function_Id := Etype (Name (Func_Call)); | |
7770 | ||
7771 | else | |
7772 | raise Program_Error; | |
7773 | end if; | |
7774 | ||
7775 | Result_Subt := Etype (Function_Id); | |
7776 | Definite := | |
7777 | (Is_Definite_Subtype (Underlying_Type (Result_Subt)) | |
7778 | and then not Is_Tagged_Type (Result_Subt)) | |
7779 | or else not Requires_Transient_Scope (Underlying_Type (Result_Subt)); | |
7780 | ||
7781 | -- If the build-in-place function returns a controlled object, then the | |
7782 | -- object needs to be finalized immediately after the context. Since | |
7783 | -- this case produces a transient scope, the servicing finalizer needs | |
7784 | -- to name the returned object. Create a temporary which is initialized | |
7785 | -- with the function call: | |
7786 | -- | |
7787 | -- Temp_Id : Func_Type := BIP_Func_Call; | |
7788 | -- | |
7789 | -- The initialization expression of the temporary will be rewritten by | |
7790 | -- the expander using the appropriate mechanism in Make_Build_In_Place_ | |
7791 | -- Call_In_Object_Declaration. | |
7792 | ||
7793 | if Needs_Finalization (Result_Subt) then | |
7794 | declare | |
7795 | Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R'); | |
7796 | Temp_Decl : Node_Id; | |
7797 | ||
7798 | begin | |
7799 | -- Reset the guard on the function call since the following does | |
7800 | -- not perform actual call expansion. | |
7801 | ||
7802 | Set_Is_Expanded_Build_In_Place_Call (Func_Call, False); | |
7803 | ||
7804 | Temp_Decl := | |
7805 | Make_Object_Declaration (Loc, | |
7806 | Defining_Identifier => Temp_Id, | |
7807 | Object_Definition => | |
7808 | New_Occurrence_Of (Result_Subt, Loc), | |
7809 | Expression => | |
7810 | New_Copy_Tree (Function_Call)); | |
7811 | ||
7812 | Insert_Action (Function_Call, Temp_Decl); | |
7813 | ||
7814 | Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc)); | |
7815 | Analyze (Function_Call); | |
7816 | end; | |
7817 | ||
7818 | -- When the result subtype is definite, an object of the subtype is | |
7819 | -- declared and an access value designating it is passed as an actual. | |
7820 | ||
7821 | elsif Definite then | |
7822 | ||
7823 | -- Create a temporary object to hold the function result | |
7824 | ||
7825 | Return_Obj_Id := Make_Temporary (Loc, 'R'); | |
7826 | Set_Etype (Return_Obj_Id, Result_Subt); | |
7827 | ||
7828 | Return_Obj_Decl := | |
7829 | Make_Object_Declaration (Loc, | |
7830 | Defining_Identifier => Return_Obj_Id, | |
7831 | Aliased_Present => True, | |
7832 | Object_Definition => New_Occurrence_Of (Result_Subt, Loc)); | |
7833 | ||
7834 | Set_No_Initialization (Return_Obj_Decl); | |
7835 | ||
7836 | Insert_Action (Func_Call, Return_Obj_Decl); | |
7837 | ||
7838 | -- When the function has a controlling result, an allocation-form | |
7839 | -- parameter must be passed indicating that the caller is allocating | |
7840 | -- the result object. This is needed because such a function can be | |
7841 | -- called as a dispatching operation and must be treated similarly | |
7842 | -- to functions with unconstrained result subtypes. | |
7843 | ||
7844 | Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
7845 | (Func_Call, Function_Id, Alloc_Form => Caller_Allocation); | |
7846 | ||
7847 | Add_Finalization_Master_Actual_To_Build_In_Place_Call | |
7848 | (Func_Call, Function_Id); | |
7849 | ||
7850 | Add_Task_Actuals_To_Build_In_Place_Call | |
7851 | (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster)); | |
7852 | ||
7853 | -- Add an implicit actual to the function call that provides access | |
7854 | -- to the caller's return object. | |
7855 | ||
7856 | Add_Access_Actual_To_Build_In_Place_Call | |
7857 | (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc)); | |
7858 | ||
7859 | -- When the result subtype is unconstrained, the function must allocate | |
7860 | -- the return object in the secondary stack, so appropriate implicit | |
7861 | -- parameters are added to the call to indicate that. A transient | |
7862 | -- scope is established to ensure eventual cleanup of the result. | |
7863 | ||
7864 | else | |
7865 | -- Pass an allocation parameter indicating that the function should | |
7866 | -- allocate its result on the secondary stack. | |
7867 | ||
7868 | Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
7869 | (Func_Call, Function_Id, Alloc_Form => Secondary_Stack); | |
7870 | ||
7871 | Add_Finalization_Master_Actual_To_Build_In_Place_Call | |
7872 | (Func_Call, Function_Id); | |
7873 | ||
7874 | Add_Task_Actuals_To_Build_In_Place_Call | |
7875 | (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster)); | |
7876 | ||
7877 | -- Pass a null value to the function since no return object is | |
7878 | -- available on the caller side. | |
7879 | ||
7880 | Add_Access_Actual_To_Build_In_Place_Call | |
7881 | (Func_Call, Function_Id, Empty); | |
7882 | end if; | |
7883 | end Make_Build_In_Place_Call_In_Anonymous_Context; | |
7884 | ||
7885 | -------------------------------------------- | |
7886 | -- Make_Build_In_Place_Call_In_Assignment -- | |
7887 | -------------------------------------------- | |
7888 | ||
7889 | procedure Make_Build_In_Place_Call_In_Assignment | |
7890 | (Assign : Node_Id; | |
7891 | Function_Call : Node_Id) | |
7892 | is | |
7893 | Lhs : constant Node_Id := Name (Assign); | |
7894 | Func_Call : Node_Id := Function_Call; | |
7895 | Func_Id : Entity_Id; | |
7896 | Loc : Source_Ptr; | |
7897 | Obj_Decl : Node_Id; | |
7898 | Obj_Id : Entity_Id; | |
7899 | Ptr_Typ : Entity_Id; | |
7900 | Ptr_Typ_Decl : Node_Id; | |
7901 | New_Expr : Node_Id; | |
7902 | Result_Subt : Entity_Id; | |
7903 | Target : Node_Id; | |
7904 | ||
7905 | begin | |
7906 | -- Step past qualification or unchecked conversion (the latter can occur | |
7907 | -- in cases of calls to 'Input). | |
7908 | ||
7909 | if Nkind_In (Func_Call, N_Qualified_Expression, | |
7910 | N_Unchecked_Type_Conversion) | |
7911 | then | |
7912 | Func_Call := Expression (Func_Call); | |
7913 | end if; | |
7914 | ||
7915 | -- If the call has already been processed to add build-in-place actuals | |
7916 | -- then return. This should not normally occur in an assignment context, | |
7917 | -- but we add the protection as a defensive measure. | |
7918 | ||
7919 | if Is_Expanded_Build_In_Place_Call (Func_Call) then | |
7920 | return; | |
7921 | end if; | |
7922 | ||
7923 | -- Mark the call as processed as a build-in-place call | |
7924 | ||
7925 | Set_Is_Expanded_Build_In_Place_Call (Func_Call); | |
7926 | ||
7927 | Loc := Sloc (Function_Call); | |
7928 | ||
7929 | if Is_Entity_Name (Name (Func_Call)) then | |
7930 | Func_Id := Entity (Name (Func_Call)); | |
7931 | ||
7932 | elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then | |
7933 | Func_Id := Etype (Name (Func_Call)); | |
7934 | ||
7935 | else | |
7936 | raise Program_Error; | |
7937 | end if; | |
7938 | ||
7939 | Result_Subt := Etype (Func_Id); | |
7940 | ||
7941 | -- When the result subtype is unconstrained, an additional actual must | |
7942 | -- be passed to indicate that the caller is providing the return object. | |
7943 | -- This parameter must also be passed when the called function has a | |
7944 | -- controlling result, because dispatching calls to the function needs | |
7945 | -- to be treated effectively the same as calls to class-wide functions. | |
7946 | ||
7947 | Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
7948 | (Func_Call, Func_Id, Alloc_Form => Caller_Allocation); | |
7949 | ||
7950 | Add_Finalization_Master_Actual_To_Build_In_Place_Call | |
7951 | (Func_Call, Func_Id); | |
7952 | ||
7953 | Add_Task_Actuals_To_Build_In_Place_Call | |
7954 | (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster)); | |
7955 | ||
7956 | -- Add an implicit actual to the function call that provides access to | |
7957 | -- the caller's return object. | |
7958 | ||
7959 | Add_Access_Actual_To_Build_In_Place_Call | |
7960 | (Func_Call, | |
7961 | Func_Id, | |
7962 | Make_Unchecked_Type_Conversion (Loc, | |
7963 | Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc), | |
7964 | Expression => Relocate_Node (Lhs))); | |
7965 | ||
7966 | -- Create an access type designating the function's result subtype | |
7967 | ||
7968 | Ptr_Typ := Make_Temporary (Loc, 'A'); | |
7969 | ||
7970 | Ptr_Typ_Decl := | |
7971 | Make_Full_Type_Declaration (Loc, | |
7972 | Defining_Identifier => Ptr_Typ, | |
7973 | Type_Definition => | |
7974 | Make_Access_To_Object_Definition (Loc, | |
7975 | All_Present => True, | |
7976 | Subtype_Indication => | |
7977 | New_Occurrence_Of (Result_Subt, Loc))); | |
7978 | Insert_After_And_Analyze (Assign, Ptr_Typ_Decl); | |
7979 | ||
7980 | -- Finally, create an access object initialized to a reference to the | |
7981 | -- function call. We know this access value is non-null, so mark the | |
7982 | -- entity accordingly to suppress junk access checks. | |
7983 | ||
7984 | New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call)); | |
7985 | ||
7986 | Obj_Id := Make_Temporary (Loc, 'R', New_Expr); | |
7987 | Set_Etype (Obj_Id, Ptr_Typ); | |
7988 | Set_Is_Known_Non_Null (Obj_Id); | |
7989 | ||
7990 | Obj_Decl := | |
7991 | Make_Object_Declaration (Loc, | |
7992 | Defining_Identifier => Obj_Id, | |
7993 | Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc), | |
7994 | Expression => New_Expr); | |
7995 | Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl); | |
7996 | ||
7997 | Rewrite (Assign, Make_Null_Statement (Loc)); | |
7998 | ||
7999 | -- Retrieve the target of the assignment | |
8000 | ||
8001 | if Nkind (Lhs) = N_Selected_Component then | |
8002 | Target := Selector_Name (Lhs); | |
8003 | elsif Nkind (Lhs) = N_Type_Conversion then | |
8004 | Target := Expression (Lhs); | |
8005 | else | |
8006 | Target := Lhs; | |
8007 | end if; | |
8008 | ||
8009 | -- If we are assigning to a return object or this is an expression of | |
8010 | -- an extension aggregate, the target should either be an identifier | |
8011 | -- or a simple expression. All other cases imply a different scenario. | |
8012 | ||
8013 | if Nkind (Target) in N_Has_Entity then | |
8014 | Target := Entity (Target); | |
8015 | else | |
8016 | return; | |
8017 | end if; | |
8018 | end Make_Build_In_Place_Call_In_Assignment; | |
8019 | ||
8020 | ---------------------------------------------------- | |
8021 | -- Make_Build_In_Place_Call_In_Object_Declaration -- | |
8022 | ---------------------------------------------------- | |
8023 | ||
8024 | procedure Make_Build_In_Place_Call_In_Object_Declaration | |
8025 | (Obj_Decl : Node_Id; | |
8026 | Function_Call : Node_Id) | |
8027 | is | |
8028 | Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl); | |
8029 | Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id); | |
8030 | Loc : constant Source_Ptr := Sloc (Function_Call); | |
8031 | Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl); | |
8032 | ||
8033 | Call_Deref : Node_Id; | |
8034 | Caller_Object : Node_Id; | |
8035 | Def_Id : Entity_Id; | |
8036 | Fmaster_Actual : Node_Id := Empty; | |
8037 | Func_Call : Node_Id := Function_Call; | |
8038 | Function_Id : Entity_Id; | |
8039 | Pool_Actual : Node_Id; | |
8040 | Ptr_Typ : Entity_Id; | |
8041 | Ptr_Typ_Decl : Node_Id; | |
8042 | Pass_Caller_Acc : Boolean := False; | |
8043 | Res_Decl : Node_Id; | |
8044 | Result_Subt : Entity_Id; | |
8045 | ||
8046 | Definite : Boolean; | |
8047 | -- True if result subtype is definite, or has a size that does not | |
8048 | -- require secondary stack usage (i.e. no variant part or components | |
8049 | -- whose type depends on discriminants). In particular, untagged types | |
8050 | -- with only access discriminants do not require secondary stack use. | |
8051 | -- Note that if the return type is tagged we must always use the sec. | |
8052 | -- stack because the call may dispatch on result. | |
8053 | ||
8054 | begin | |
8055 | -- Step past qualification or unchecked conversion (the latter can occur | |
8056 | -- in cases of calls to 'Input). | |
8057 | ||
8058 | if Nkind_In (Func_Call, N_Qualified_Expression, | |
8059 | N_Unchecked_Type_Conversion) | |
8060 | then | |
8061 | Func_Call := Expression (Func_Call); | |
8062 | end if; | |
8063 | ||
8064 | -- If the call has already been processed to add build-in-place actuals | |
8065 | -- then return. This should not normally occur in an object declaration, | |
8066 | -- but we add the protection as a defensive measure. | |
8067 | ||
8068 | if Is_Expanded_Build_In_Place_Call (Func_Call) then | |
8069 | return; | |
8070 | end if; | |
8071 | ||
8072 | -- Mark the call as processed as a build-in-place call | |
8073 | ||
8074 | Set_Is_Expanded_Build_In_Place_Call (Func_Call); | |
8075 | ||
8076 | if Is_Entity_Name (Name (Func_Call)) then | |
8077 | Function_Id := Entity (Name (Func_Call)); | |
8078 | ||
8079 | elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then | |
8080 | Function_Id := Etype (Name (Func_Call)); | |
8081 | ||
8082 | else | |
8083 | raise Program_Error; | |
8084 | end if; | |
8085 | ||
8086 | Result_Subt := Etype (Function_Id); | |
8087 | Definite := | |
8088 | (Is_Definite_Subtype (Underlying_Type (Result_Subt)) | |
8089 | and then not Is_Tagged_Type (Result_Subt)) | |
8090 | or else not Requires_Transient_Scope (Underlying_Type (Result_Subt)); | |
8091 | ||
8092 | -- Create an access type designating the function's result subtype. We | |
8093 | -- use the type of the original call because it may be a call to an | |
8094 | -- inherited operation, which the expansion has replaced with the parent | |
8095 | -- operation that yields the parent type. Note that this access type | |
8096 | -- must be declared before we establish a transient scope, so that it | |
8097 | -- receives the proper accessibility level. | |
8098 | ||
8099 | Ptr_Typ := Make_Temporary (Loc, 'A'); | |
8100 | Ptr_Typ_Decl := | |
8101 | Make_Full_Type_Declaration (Loc, | |
8102 | Defining_Identifier => Ptr_Typ, | |
8103 | Type_Definition => | |
8104 | Make_Access_To_Object_Definition (Loc, | |
8105 | All_Present => True, | |
8106 | Subtype_Indication => | |
8107 | New_Occurrence_Of (Etype (Function_Call), Loc))); | |
8108 | ||
8109 | -- The access type and its accompanying object must be inserted after | |
8110 | -- the object declaration in the constrained case, so that the function | |
8111 | -- call can be passed access to the object. In the indefinite case, | |
8112 | -- or if the object declaration is for a return object, the access type | |
8113 | -- and object must be inserted before the object, since the object | |
8114 | -- declaration is rewritten to be a renaming of a dereference of the | |
8115 | -- access object. Note: we need to freeze Ptr_Typ explicitly, because | |
8116 | -- the result object is in a different (transient) scope, so won't | |
8117 | -- cause freezing. | |
8118 | ||
8119 | if Definite | |
8120 | and then not Is_Return_Object (Defining_Identifier (Obj_Decl)) | |
8121 | then | |
8122 | Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl); | |
8123 | else | |
8124 | Insert_Action (Obj_Decl, Ptr_Typ_Decl); | |
8125 | end if; | |
8126 | ||
8127 | -- Force immediate freezing of Ptr_Typ because Res_Decl will be | |
8128 | -- elaborated in an inner (transient) scope and thus won't cause | |
8129 | -- freezing by itself. | |
8130 | ||
8131 | declare | |
8132 | Ptr_Typ_Freeze_Ref : constant Node_Id := | |
8133 | New_Occurrence_Of (Ptr_Typ, Loc); | |
8134 | begin | |
8135 | Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl); | |
8136 | Freeze_Expression (Ptr_Typ_Freeze_Ref); | |
8137 | end; | |
8138 | ||
8139 | -- If the object is a return object of an enclosing build-in-place | |
8140 | -- function, then the implicit build-in-place parameters of the | |
8141 | -- enclosing function are simply passed along to the called function. | |
8142 | -- (Unfortunately, this won't cover the case of extension aggregates | |
8143 | -- where the ancestor part is a build-in-place indefinite function | |
8144 | -- call that should be passed along the caller's parameters. Currently | |
8145 | -- those get mishandled by reassigning the result of the call to the | |
8146 | -- aggregate return object, when the call result should really be | |
8147 | -- directly built in place in the aggregate and not in a temporary. ???) | |
8148 | ||
8149 | if Is_Return_Object (Defining_Identifier (Obj_Decl)) then | |
8150 | Pass_Caller_Acc := True; | |
8151 | ||
8152 | -- When the enclosing function has a BIP_Alloc_Form formal then we | |
8153 | -- pass it along to the callee (such as when the enclosing function | |
8154 | -- has an unconstrained or tagged result type). | |
8155 | ||
8156 | if Needs_BIP_Alloc_Form (Encl_Func) then | |
8157 | if RTE_Available (RE_Root_Storage_Pool_Ptr) then | |
8158 | Pool_Actual := | |
8159 | New_Occurrence_Of | |
8160 | (Build_In_Place_Formal (Encl_Func, BIP_Storage_Pool), Loc); | |
8161 | ||
8162 | -- The build-in-place pool formal is not built on e.g. ZFP | |
8163 | ||
8164 | else | |
8165 | Pool_Actual := Empty; | |
8166 | end if; | |
8167 | ||
8168 | Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
8169 | (Function_Call => Func_Call, | |
8170 | Function_Id => Function_Id, | |
8171 | Alloc_Form_Exp => | |
8172 | New_Occurrence_Of | |
8173 | (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc), | |
8174 | Pool_Actual => Pool_Actual); | |
8175 | ||
8176 | -- Otherwise, if enclosing function has a definite result subtype, | |
8177 | -- then caller allocation will be used. | |
8178 | ||
8179 | else | |
8180 | Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
8181 | (Func_Call, Function_Id, Alloc_Form => Caller_Allocation); | |
8182 | end if; | |
8183 | ||
8184 | if Needs_BIP_Finalization_Master (Encl_Func) then | |
8185 | Fmaster_Actual := | |
8186 | New_Occurrence_Of | |
8187 | (Build_In_Place_Formal | |
8188 | (Encl_Func, BIP_Finalization_Master), Loc); | |
8189 | end if; | |
8190 | ||
8191 | -- Retrieve the BIPacc formal from the enclosing function and convert | |
8192 | -- it to the access type of the callee's BIP_Object_Access formal. | |
8193 | ||
8194 | Caller_Object := | |
8195 | Make_Unchecked_Type_Conversion (Loc, | |
8196 | Subtype_Mark => | |
8197 | New_Occurrence_Of | |
8198 | (Etype | |
8199 | (Build_In_Place_Formal (Function_Id, BIP_Object_Access)), | |
8200 | Loc), | |
8201 | Expression => | |
8202 | New_Occurrence_Of | |
8203 | (Build_In_Place_Formal (Encl_Func, BIP_Object_Access), | |
8204 | Loc)); | |
8205 | ||
8206 | -- In the definite case, add an implicit actual to the function call | |
8207 | -- that provides access to the declared object. An unchecked conversion | |
8208 | -- to the (specific) result type of the function is inserted to handle | |
8209 | -- the case where the object is declared with a class-wide type. | |
8210 | ||
8211 | elsif Definite then | |
8212 | Caller_Object := | |
8213 | Make_Unchecked_Type_Conversion (Loc, | |
8214 | Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc), | |
8215 | Expression => New_Occurrence_Of (Obj_Def_Id, Loc)); | |
8216 | ||
8217 | -- When the function has a controlling result, an allocation-form | |
8218 | -- parameter must be passed indicating that the caller is allocating | |
8219 | -- the result object. This is needed because such a function can be | |
8220 | -- called as a dispatching operation and must be treated similarly | |
8221 | -- to functions with indefinite result subtypes. | |
8222 | ||
8223 | Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
8224 | (Func_Call, Function_Id, Alloc_Form => Caller_Allocation); | |
8225 | ||
8226 | -- The allocation for indefinite library-level objects occurs on the | |
8227 | -- heap as opposed to the secondary stack. This accommodates DLLs where | |
8228 | -- the secondary stack is destroyed after each library unload. This is | |
8229 | -- a hybrid mechanism where a stack-allocated object lives on the heap. | |
8230 | ||
8231 | elsif Is_Library_Level_Entity (Defining_Identifier (Obj_Decl)) | |
8232 | and then not Restriction_Active (No_Implicit_Heap_Allocations) | |
8233 | then | |
8234 | Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
8235 | (Func_Call, Function_Id, Alloc_Form => Global_Heap); | |
8236 | Caller_Object := Empty; | |
8237 | ||
8238 | -- Create a finalization master for the access result type to ensure | |
8239 | -- that the heap allocation can properly chain the object and later | |
8240 | -- finalize it when the library unit goes out of scope. | |
8241 | ||
8242 | if Needs_Finalization (Etype (Func_Call)) then | |
8243 | Build_Finalization_Master | |
8244 | (Typ => Ptr_Typ, | |
8245 | For_Lib_Level => True, | |
8246 | Insertion_Node => Ptr_Typ_Decl); | |
8247 | ||
8248 | Fmaster_Actual := | |
8249 | Make_Attribute_Reference (Loc, | |
8250 | Prefix => | |
8251 | New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc), | |
8252 | Attribute_Name => Name_Unrestricted_Access); | |
8253 | end if; | |
8254 | ||
8255 | -- In other indefinite cases, pass an indication to do the allocation | |
8256 | -- on the secondary stack and set Caller_Object to Empty so that a null | |
8257 | -- value will be passed for the caller's object address. A transient | |
8258 | -- scope is established to ensure eventual cleanup of the result. | |
8259 | ||
8260 | else | |
8261 | Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
8262 | (Func_Call, Function_Id, Alloc_Form => Secondary_Stack); | |
8263 | Caller_Object := Empty; | |
8264 | ||
8265 | Establish_Transient_Scope (Obj_Decl, Sec_Stack => True); | |
8266 | end if; | |
8267 | ||
8268 | -- Pass along any finalization master actual, which is needed in the | |
8269 | -- case where the called function initializes a return object of an | |
8270 | -- enclosing build-in-place function. | |
8271 | ||
8272 | Add_Finalization_Master_Actual_To_Build_In_Place_Call | |
8273 | (Func_Call => Func_Call, | |
8274 | Func_Id => Function_Id, | |
8275 | Master_Exp => Fmaster_Actual); | |
8276 | ||
8277 | if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement | |
8278 | and then Has_Task (Result_Subt) | |
8279 | then | |
8280 | -- Here we're passing along the master that was passed in to this | |
8281 | -- function. | |
8282 | ||
8283 | Add_Task_Actuals_To_Build_In_Place_Call | |
8284 | (Func_Call, Function_Id, | |
8285 | Master_Actual => | |
8286 | New_Occurrence_Of | |
8287 | (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc)); | |
8288 | ||
8289 | else | |
8290 | Add_Task_Actuals_To_Build_In_Place_Call | |
8291 | (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster)); | |
8292 | end if; | |
8293 | ||
8294 | Add_Access_Actual_To_Build_In_Place_Call | |
8295 | (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc); | |
8296 | ||
8297 | -- Finally, create an access object initialized to a reference to the | |
8298 | -- function call. We know this access value cannot be null, so mark the | |
8299 | -- entity accordingly to suppress the access check. | |
8300 | ||
8301 | Def_Id := Make_Temporary (Loc, 'R', Func_Call); | |
8302 | Set_Etype (Def_Id, Ptr_Typ); | |
8303 | Set_Is_Known_Non_Null (Def_Id); | |
8304 | ||
8305 | Res_Decl := | |
8306 | Make_Object_Declaration (Loc, | |
8307 | Defining_Identifier => Def_Id, | |
8308 | Constant_Present => True, | |
8309 | Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc), | |
8310 | Expression => | |
8311 | Make_Reference (Loc, Relocate_Node (Func_Call))); | |
8312 | ||
8313 | Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl); | |
8314 | ||
8315 | -- If the result subtype of the called function is definite and is not | |
8316 | -- itself the return expression of an enclosing BIP function, then mark | |
8317 | -- the object as having no initialization. | |
8318 | ||
8319 | if Definite | |
8320 | and then not Is_Return_Object (Defining_Identifier (Obj_Decl)) | |
8321 | then | |
8322 | -- The related object declaration is encased in a transient block | |
8323 | -- because the build-in-place function call contains at least one | |
8324 | -- nested function call that produces a controlled transient | |
8325 | -- temporary: | |
8326 | ||
8327 | -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call); | |
8328 | ||
8329 | -- Since the build-in-place expansion decouples the call from the | |
8330 | -- object declaration, the finalization machinery lacks the context | |
8331 | -- which prompted the generation of the transient block. To resolve | |
8332 | -- this scenario, store the build-in-place call. | |
8333 | ||
8334 | if Scope_Is_Transient and then Node_To_Be_Wrapped = Obj_Decl then | |
8335 | Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl); | |
8336 | end if; | |
8337 | ||
8338 | Set_Expression (Obj_Decl, Empty); | |
8339 | Set_No_Initialization (Obj_Decl); | |
8340 | ||
8341 | -- In case of an indefinite result subtype, or if the call is the | |
8342 | -- return expression of an enclosing BIP function, rewrite the object | |
8343 | -- declaration as an object renaming where the renamed object is a | |
8344 | -- dereference of <function_Call>'reference: | |
8345 | -- | |
8346 | -- Obj : Subt renames <function_call>'Ref.all; | |
8347 | ||
8348 | else | |
8349 | Call_Deref := | |
8350 | Make_Explicit_Dereference (Obj_Loc, | |
8351 | Prefix => New_Occurrence_Of (Def_Id, Obj_Loc)); | |
8352 | ||
8353 | Rewrite (Obj_Decl, | |
8354 | Make_Object_Renaming_Declaration (Obj_Loc, | |
8355 | Defining_Identifier => Make_Temporary (Obj_Loc, 'D'), | |
8356 | Subtype_Mark => New_Occurrence_Of (Result_Subt, Obj_Loc), | |
8357 | Name => Call_Deref)); | |
8358 | ||
8359 | Set_Renamed_Object (Defining_Identifier (Obj_Decl), Call_Deref); | |
8360 | ||
8361 | -- If the original entity comes from source, then mark the new | |
8362 | -- entity as needing debug information, even though it's defined | |
8363 | -- by a generated renaming that does not come from source, so that | |
8364 | -- the Materialize_Entity flag will be set on the entity when | |
8365 | -- Debug_Renaming_Declaration is called during analysis. | |
8366 | ||
8367 | if Comes_From_Source (Obj_Def_Id) then | |
8368 | Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl)); | |
8369 | end if; | |
8370 | ||
8371 | Analyze (Obj_Decl); | |
8372 | ||
8373 | -- Replace the internal identifier of the renaming declaration's | |
8374 | -- entity with identifier of the original object entity. We also have | |
8375 | -- to exchange the entities containing their defining identifiers to | |
8376 | -- ensure the correct replacement of the object declaration by the | |
8377 | -- object renaming declaration to avoid homograph conflicts (since | |
8378 | -- the object declaration's defining identifier was already entered | |
8379 | -- in current scope). The Next_Entity links of the two entities also | |
8380 | -- have to be swapped since the entities are part of the return | |
8381 | -- scope's entity list and the list structure would otherwise be | |
8382 | -- corrupted. Finally, the homonym chain must be preserved as well. | |
8383 | ||
8384 | declare | |
8385 | Ren_Id : constant Entity_Id := Defining_Entity (Obj_Decl); | |
8386 | Next_Id : constant Entity_Id := Next_Entity (Ren_Id); | |
8387 | ||
8388 | begin | |
8389 | Set_Chars (Ren_Id, Chars (Obj_Def_Id)); | |
8390 | ||
8391 | -- Swap next entity links in preparation for exchanging entities | |
8392 | ||
8393 | Set_Next_Entity (Ren_Id, Next_Entity (Obj_Def_Id)); | |
8394 | Set_Next_Entity (Obj_Def_Id, Next_Id); | |
8395 | Set_Homonym (Ren_Id, Homonym (Obj_Def_Id)); | |
8396 | ||
8397 | Exchange_Entities (Ren_Id, Obj_Def_Id); | |
8398 | ||
8399 | -- Preserve source indication of original declaration, so that | |
8400 | -- xref information is properly generated for the right entity. | |
8401 | ||
8402 | Preserve_Comes_From_Source (Obj_Decl, Original_Node (Obj_Decl)); | |
8403 | Preserve_Comes_From_Source (Obj_Def_Id, Original_Node (Obj_Decl)); | |
8404 | ||
8405 | Set_Comes_From_Source (Ren_Id, False); | |
8406 | end; | |
8407 | end if; | |
8408 | ||
8409 | -- If the object entity has a class-wide Etype, then we need to change | |
8410 | -- it to the result subtype of the function call, because otherwise the | |
8411 | -- object will be class-wide without an explicit initialization and | |
8412 | -- won't be allocated properly by the back end. It seems unclean to make | |
8413 | -- such a revision to the type at this point, and we should try to | |
8414 | -- improve this treatment when build-in-place functions with class-wide | |
8415 | -- results are implemented. ??? | |
8416 | ||
8417 | if Is_Class_Wide_Type (Etype (Defining_Identifier (Obj_Decl))) then | |
8418 | Set_Etype (Defining_Identifier (Obj_Decl), Result_Subt); | |
8419 | end if; | |
8420 | end Make_Build_In_Place_Call_In_Object_Declaration; | |
8421 | ||
8422 | -------------------------------------------- | |
8423 | -- Make_CPP_Constructor_Call_In_Allocator -- | |
8424 | -------------------------------------------- | |
8425 | ||
8426 | procedure Make_CPP_Constructor_Call_In_Allocator | |
8427 | (Allocator : Node_Id; | |
8428 | Function_Call : Node_Id) | |
8429 | is | |
8430 | Loc : constant Source_Ptr := Sloc (Function_Call); | |
8431 | Acc_Type : constant Entity_Id := Etype (Allocator); | |
8432 | Function_Id : constant Entity_Id := Entity (Name (Function_Call)); | |
8433 | Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id)); | |
8434 | ||
8435 | New_Allocator : Node_Id; | |
8436 | Return_Obj_Access : Entity_Id; | |
8437 | Tmp_Obj : Node_Id; | |
8438 | ||
8439 | begin | |
8440 | pragma Assert (Nkind (Allocator) = N_Allocator | |
8441 | and then Nkind (Function_Call) = N_Function_Call); | |
8442 | pragma Assert (Convention (Function_Id) = Convention_CPP | |
8443 | and then Is_Constructor (Function_Id)); | |
8444 | pragma Assert (Is_Constrained (Underlying_Type (Result_Subt))); | |
8445 | ||
8446 | -- Replace the initialized allocator of form "new T'(Func (...))" with | |
8447 | -- an uninitialized allocator of form "new T", where T is the result | |
8448 | -- subtype of the called function. The call to the function is handled | |
8449 | -- separately further below. | |
8450 | ||
8451 | New_Allocator := | |
8452 | Make_Allocator (Loc, | |
8453 | Expression => New_Occurrence_Of (Result_Subt, Loc)); | |
8454 | Set_No_Initialization (New_Allocator); | |
8455 | ||
8456 | -- Copy attributes to new allocator. Note that the new allocator | |
8457 | -- logically comes from source if the original one did, so copy the | |
8458 | -- relevant flag. This ensures proper treatment of the restriction | |
8459 | -- No_Implicit_Heap_Allocations in this case. | |
8460 | ||
8461 | Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator)); | |
8462 | Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator)); | |
8463 | Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator)); | |
8464 | ||
8465 | Rewrite (Allocator, New_Allocator); | |
8466 | ||
8467 | -- Create a new access object and initialize it to the result of the | |
8468 | -- new uninitialized allocator. Note: we do not use Allocator as the | |
8469 | -- Related_Node of Return_Obj_Access in call to Make_Temporary below | |
8470 | -- as this would create a sort of infinite "recursion". | |
8471 | ||
8472 | Return_Obj_Access := Make_Temporary (Loc, 'R'); | |
8473 | Set_Etype (Return_Obj_Access, Acc_Type); | |
8474 | ||
8475 | -- Generate: | |
8476 | -- Rnnn : constant ptr_T := new (T); | |
8477 | -- Init (Rnn.all,...); | |
8478 | ||
8479 | Tmp_Obj := | |
8480 | Make_Object_Declaration (Loc, | |
8481 | Defining_Identifier => Return_Obj_Access, | |
8482 | Constant_Present => True, | |
8483 | Object_Definition => New_Occurrence_Of (Acc_Type, Loc), | |
8484 | Expression => Relocate_Node (Allocator)); | |
8485 | Insert_Action (Allocator, Tmp_Obj); | |
8486 | ||
8487 | Insert_List_After_And_Analyze (Tmp_Obj, | |
8488 | Build_Initialization_Call (Loc, | |
8489 | Id_Ref => | |
8490 | Make_Explicit_Dereference (Loc, | |
8491 | Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)), | |
8492 | Typ => Etype (Function_Id), | |
8493 | Constructor_Ref => Function_Call)); | |
8494 | ||
8495 | -- Finally, replace the allocator node with a reference to the result of | |
8496 | -- the function call itself (which will effectively be an access to the | |
8497 | -- object created by the allocator). | |
8498 | ||
8499 | Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc)); | |
8500 | ||
8501 | -- Ada 2005 (AI-251): If the type of the allocator is an interface then | |
8502 | -- generate an implicit conversion to force displacement of the "this" | |
8503 | -- pointer. | |
8504 | ||
8505 | if Is_Interface (Designated_Type (Acc_Type)) then | |
8506 | Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator))); | |
8507 | end if; | |
8508 | ||
8509 | Analyze_And_Resolve (Allocator, Acc_Type); | |
8510 | end Make_CPP_Constructor_Call_In_Allocator; | |
8511 | ||
8512 | ----------------------------------- | |
8513 | -- Needs_BIP_Finalization_Master -- | |
8514 | ----------------------------------- | |
8515 | ||
8516 | function Needs_BIP_Finalization_Master | |
8517 | (Func_Id : Entity_Id) return Boolean | |
8518 | is | |
8519 | pragma Assert (Is_Build_In_Place_Function (Func_Id)); | |
8520 | Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id)); | |
8521 | begin | |
8522 | -- A formal giving the finalization master is needed for build-in-place | |
8523 | -- functions whose result type needs finalization or is a tagged type. | |
8524 | -- Tagged primitive build-in-place functions need such a formal because | |
8525 | -- they can be called by a dispatching call, and extensions may require | |
8526 | -- finalization even if the root type doesn't. This means they're also | |
8527 | -- needed for tagged nonprimitive build-in-place functions with tagged | |
8528 | -- results, since such functions can be called via access-to-function | |
8529 | -- types, and those can be used to call primitives, so masters have to | |
8530 | -- be passed to all such build-in-place functions, primitive or not. | |
8531 | ||
8532 | return | |
8533 | not Restriction_Active (No_Finalization) | |
8534 | and then (Needs_Finalization (Func_Typ) | |
8535 | or else Is_Tagged_Type (Func_Typ)); | |
8536 | end Needs_BIP_Finalization_Master; | |
8537 | ||
8538 | -------------------------- | |
8539 | -- Needs_BIP_Alloc_Form -- | |
8540 | -------------------------- | |
8541 | ||
8542 | function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is | |
8543 | pragma Assert (Is_Build_In_Place_Function (Func_Id)); | |
8544 | Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id)); | |
8545 | begin | |
8546 | return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ); | |
8547 | end Needs_BIP_Alloc_Form; | |
8548 | ||
8549 | -------------------------------------- | |
8550 | -- Needs_Result_Accessibility_Level -- | |
8551 | -------------------------------------- | |
8552 | ||
8553 | function Needs_Result_Accessibility_Level | |
8554 | (Func_Id : Entity_Id) return Boolean | |
8555 | is | |
8556 | Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id)); | |
8557 | ||
8558 | function Has_Unconstrained_Access_Discriminant_Component | |
8559 | (Comp_Typ : Entity_Id) return Boolean; | |
8560 | -- Returns True if any component of the type has an unconstrained access | |
8561 | -- discriminant. | |
8562 | ||
8563 | ----------------------------------------------------- | |
8564 | -- Has_Unconstrained_Access_Discriminant_Component -- | |
8565 | ----------------------------------------------------- | |
8566 | ||
8567 | function Has_Unconstrained_Access_Discriminant_Component | |
8568 | (Comp_Typ : Entity_Id) return Boolean | |
8569 | is | |
8570 | begin | |
8571 | if not Is_Limited_Type (Comp_Typ) then | |
8572 | return False; | |
8573 | ||
8574 | -- Only limited types can have access discriminants with | |
8575 | -- defaults. | |
8576 | ||
8577 | elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then | |
8578 | return True; | |
8579 | ||
8580 | elsif Is_Array_Type (Comp_Typ) then | |
8581 | return Has_Unconstrained_Access_Discriminant_Component | |
8582 | (Underlying_Type (Component_Type (Comp_Typ))); | |
8583 | ||
8584 | elsif Is_Record_Type (Comp_Typ) then | |
8585 | declare | |
8586 | Comp : Entity_Id; | |
8587 | ||
8588 | begin | |
8589 | Comp := First_Component (Comp_Typ); | |
8590 | while Present (Comp) loop | |
8591 | if Has_Unconstrained_Access_Discriminant_Component | |
8592 | (Underlying_Type (Etype (Comp))) | |
8593 | then | |
8594 | return True; | |
8595 | end if; | |
8596 | ||
8597 | Next_Component (Comp); | |
8598 | end loop; | |
8599 | end; | |
8600 | end if; | |
8601 | ||
8602 | return False; | |
8603 | end Has_Unconstrained_Access_Discriminant_Component; | |
8604 | ||
8605 | Feature_Disabled : constant Boolean := True; | |
8606 | -- Temporary | |
8607 | ||
8608 | -- Start of processing for Needs_Result_Accessibility_Level | |
8609 | ||
8610 | begin | |
8611 | -- False if completion unavailable (how does this happen???) | |
8612 | ||
8613 | if not Present (Func_Typ) then | |
8614 | return False; | |
8615 | ||
8616 | elsif Feature_Disabled then | |
8617 | return False; | |
8618 | ||
8619 | -- False if not a function, also handle enum-lit renames case | |
8620 | ||
8621 | elsif Func_Typ = Standard_Void_Type | |
8622 | or else Is_Scalar_Type (Func_Typ) | |
8623 | then | |
8624 | return False; | |
8625 | ||
8626 | -- Handle a corner case, a cross-dialect subp renaming. For example, | |
8627 | -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when | |
8628 | -- an Ada 2005 (or earlier) unit references predefined run-time units. | |
8629 | ||
8630 | elsif Present (Alias (Func_Id)) then | |
8631 | ||
8632 | -- Unimplemented: a cross-dialect subp renaming which does not set | |
8633 | -- the Alias attribute (e.g., a rename of a dereference of an access | |
8634 | -- to subprogram value). ??? | |
8635 | ||
8636 | return Present (Extra_Accessibility_Of_Result (Alias (Func_Id))); | |
8637 | ||
8638 | -- Remaining cases require Ada 2012 mode | |
8639 | ||
8640 | elsif Ada_Version < Ada_2012 then | |
8641 | return False; | |
8642 | ||
8643 | elsif Ekind (Func_Typ) = E_Anonymous_Access_Type | |
8644 | or else Is_Tagged_Type (Func_Typ) | |
8645 | then | |
8646 | -- In the case of, say, a null tagged record result type, the need | |
8647 | -- for this extra parameter might not be obvious. This function | |
8648 | -- returns True for all tagged types for compatibility reasons. | |
8649 | -- A function with, say, a tagged null controlling result type might | |
8650 | -- be overridden by a primitive of an extension having an access | |
8651 | -- discriminant and the overrider and overridden must have compatible | |
8652 | -- calling conventions (including implicitly declared parameters). | |
8653 | -- Similarly, values of one access-to-subprogram type might designate | |
8654 | -- both a primitive subprogram of a given type and a function | |
8655 | -- which is, for example, not a primitive subprogram of any type. | |
8656 | -- Again, this requires calling convention compatibility. | |
8657 | -- It might be possible to solve these issues by introducing | |
8658 | -- wrappers, but that is not the approach that was chosen. | |
8659 | ||
8660 | return True; | |
8661 | ||
8662 | elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then | |
8663 | return True; | |
8664 | ||
8665 | elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then | |
8666 | return True; | |
8667 | ||
8668 | -- False for all other cases | |
8669 | ||
8670 | else | |
8671 | return False; | |
8672 | end if; | |
8673 | end Needs_Result_Accessibility_Level; | |
8674 | ||
8675 | --------------------------------- | |
8676 | -- Rewrite_Function_Call_For_C -- | |
8677 | --------------------------------- | |
8678 | ||
8679 | procedure Rewrite_Function_Call_For_C (N : Node_Id) is | |
8680 | Orig_Func : constant Entity_Id := Entity (Name (N)); | |
8681 | Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func); | |
8682 | Par : constant Node_Id := Parent (N); | |
8683 | Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id); | |
8684 | Loc : constant Source_Ptr := Sloc (Par); | |
8685 | Actuals : List_Id; | |
8686 | Last_Actual : Node_Id; | |
8687 | Last_Formal : Entity_Id; | |
8688 | ||
8689 | -- Start of processing for Rewrite_Function_Call_For_C | |
8690 | ||
8691 | begin | |
8692 | -- The actuals may be given by named associations, so the added actual | |
8693 | -- that is the target of the return value of the call must be a named | |
8694 | -- association as well, so we retrieve the name of the generated | |
8695 | -- out_formal. | |
8696 | ||
8697 | Last_Formal := First_Formal (Proc_Id); | |
8698 | while Present (Next_Formal (Last_Formal)) loop | |
8699 | Last_Formal := Next_Formal (Last_Formal); | |
8700 | end loop; | |
8701 | ||
8702 | Actuals := Parameter_Associations (N); | |
8703 | ||
8704 | -- The original function may lack parameters | |
8705 | ||
8706 | if No (Actuals) then | |
8707 | Actuals := New_List; | |
8708 | end if; | |
8709 | ||
8710 | -- If the function call is the expression of an assignment statement, | |
8711 | -- transform the assignment into a procedure call. Generate: | |
8712 | ||
8713 | -- LHS := Func_Call (...); | |
8714 | ||
8715 | -- Proc_Call (..., LHS); | |
8716 | ||
8717 | -- If function is inherited, a conversion may be necessary. | |
8718 | ||
8719 | if Nkind (Par) = N_Assignment_Statement then | |
8720 | Last_Actual := Name (Par); | |
8721 | ||
8722 | if not Comes_From_Source (Orig_Func) | |
8723 | and then Etype (Orig_Func) /= Etype (Func_Id) | |
8724 | then | |
8725 | Last_Actual := | |
8726 | Make_Type_Conversion (Loc, | |
8727 | New_Occurrence_Of (Etype (Func_Id), Loc), | |
8728 | Last_Actual); | |
8729 | end if; | |
8730 | ||
8731 | Append_To (Actuals, | |
8732 | Make_Parameter_Association (Loc, | |
8733 | Selector_Name => | |
8734 | Make_Identifier (Loc, Chars (Last_Formal)), | |
8735 | Explicit_Actual_Parameter => Last_Actual)); | |
8736 | ||
8737 | Rewrite (Par, | |
8738 | Make_Procedure_Call_Statement (Loc, | |
8739 | Name => New_Occurrence_Of (Proc_Id, Loc), | |
8740 | Parameter_Associations => Actuals)); | |
8741 | Analyze (Par); | |
8742 | ||
8743 | -- Otherwise the context is an expression. Generate a temporary and a | |
8744 | -- procedure call to obtain the function result. Generate: | |
8745 | ||
8746 | -- ... Func_Call (...) ... | |
8747 | ||
8748 | -- Temp : ...; | |
8749 | -- Proc_Call (..., Temp); | |
8750 | -- ... Temp ... | |
8751 | ||
8752 | else | |
8753 | declare | |
8754 | Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T'); | |
8755 | Call : Node_Id; | |
8756 | Decl : Node_Id; | |
8757 | ||
8758 | begin | |
8759 | -- Generate: | |
8760 | -- Temp : ...; | |
8761 | ||
8762 | Decl := | |
8763 | Make_Object_Declaration (Loc, | |
8764 | Defining_Identifier => Temp_Id, | |
8765 | Object_Definition => | |
8766 | New_Occurrence_Of (Etype (Func_Id), Loc)); | |
8767 | ||
8768 | -- Generate: | |
8769 | -- Proc_Call (..., Temp); | |
8770 | ||
8771 | Append_To (Actuals, | |
8772 | Make_Parameter_Association (Loc, | |
8773 | Selector_Name => | |
8774 | Make_Identifier (Loc, Chars (Last_Formal)), | |
8775 | Explicit_Actual_Parameter => | |
8776 | New_Occurrence_Of (Temp_Id, Loc))); | |
8777 | ||
8778 | Call := | |
8779 | Make_Procedure_Call_Statement (Loc, | |
8780 | Name => New_Occurrence_Of (Proc_Id, Loc), | |
8781 | Parameter_Associations => Actuals); | |
8782 | ||
8783 | Insert_Actions (Par, New_List (Decl, Call)); | |
8784 | Rewrite (N, New_Occurrence_Of (Temp_Id, Loc)); | |
8785 | end; | |
8786 | end if; | |
8787 | end Rewrite_Function_Call_For_C; | |
8788 | ||
8789 | ------------------------------------ | |
8790 | -- Set_Enclosing_Sec_Stack_Return -- | |
8791 | ------------------------------------ | |
8792 | ||
8793 | procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is | |
8794 | P : Node_Id := N; | |
8795 | ||
8796 | begin | |
8797 | -- Due to a possible mix of internally generated blocks, source blocks | |
8798 | -- and loops, the scope stack may not be contiguous as all labels are | |
8799 | -- inserted at the top level within the related function. Instead, | |
8800 | -- perform a parent-based traversal and mark all appropriate constructs. | |
8801 | ||
8802 | while Present (P) loop | |
8803 | ||
8804 | -- Mark the label of a source or internally generated block or | |
8805 | -- loop. | |
8806 | ||
8807 | if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then | |
8808 | Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P))); | |
8809 | ||
8810 | -- Mark the enclosing function | |
8811 | ||
8812 | elsif Nkind (P) = N_Subprogram_Body then | |
8813 | if Present (Corresponding_Spec (P)) then | |
8814 | Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P)); | |
8815 | else | |
8816 | Set_Sec_Stack_Needed_For_Return (Defining_Entity (P)); | |
8817 | end if; | |
8818 | ||
8819 | -- Do not go beyond the enclosing function | |
8820 | ||
8821 | exit; | |
8822 | end if; | |
8823 | ||
8824 | P := Parent (P); | |
8825 | end loop; | |
8826 | end Set_Enclosing_Sec_Stack_Return; | |
8827 | ||
8828 | end Exp_Ch6; |