]>
Commit | Line | Data |
---|---|---|
01aef5ad | 1 | ------------------------------------------------------------------------------ |
70482933 RK |
2 | -- -- |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- E X P _ C H 6 -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
e7ba564f | 9 | -- Copyright (C) 1992-2014, Free Software Foundation, Inc. -- |
70482933 RK |
10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
b5c84c3c | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
70482933 RK |
14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
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 -- | |
b5c84c3c RD |
18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
70482933 RK |
20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
71ff80dc | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
70482933 RK |
23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
26 | with Atree; use Atree; | |
27 | with Checks; use Checks; | |
28 | with Debug; use Debug; | |
29 | with Einfo; use Einfo; | |
30 | with Errout; use Errout; | |
31 | with Elists; use Elists; | |
4a1bfefb | 32 | with Exp_Aggr; use Exp_Aggr; |
f937473f | 33 | with Exp_Atag; use Exp_Atag; |
70482933 RK |
34 | with Exp_Ch2; use Exp_Ch2; |
35 | with Exp_Ch3; use Exp_Ch3; | |
36 | with Exp_Ch7; use Exp_Ch7; | |
37 | with Exp_Ch9; use Exp_Ch9; | |
70482933 RK |
38 | with Exp_Dbug; use Exp_Dbug; |
39 | with Exp_Disp; use Exp_Disp; | |
40 | with Exp_Dist; use Exp_Dist; | |
41 | with Exp_Intr; use Exp_Intr; | |
42 | with Exp_Pakd; use Exp_Pakd; | |
a767d69b | 43 | with Exp_Prag; use Exp_Prag; |
70482933 RK |
44 | with Exp_Tss; use Exp_Tss; |
45 | with Exp_Util; use Exp_Util; | |
fbf5a39b | 46 | with Fname; use Fname; |
70482933 | 47 | with Freeze; use Freeze; |
70482933 RK |
48 | with Inline; use Inline; |
49 | with Lib; use Lib; | |
7888a6ae | 50 | with Namet; use Namet; |
70482933 RK |
51 | with Nlists; use Nlists; |
52 | with Nmake; use Nmake; | |
53 | with Opt; use Opt; | |
54 | with Restrict; use Restrict; | |
6e937c1c | 55 | with Rident; use Rident; |
70482933 RK |
56 | with Rtsfind; use Rtsfind; |
57 | with Sem; use Sem; | |
a4100e55 | 58 | with Sem_Aux; use Sem_Aux; |
70482933 RK |
59 | with Sem_Ch6; use Sem_Ch6; |
60 | with Sem_Ch8; use Sem_Ch8; | |
70482933 | 61 | with Sem_Ch13; use Sem_Ch13; |
dec6faf1 | 62 | with Sem_Dim; use Sem_Dim; |
70482933 RK |
63 | with Sem_Disp; use Sem_Disp; |
64 | with Sem_Dist; use Sem_Dist; | |
dec6faf1 | 65 | with Sem_Eval; use Sem_Eval; |
758c442c | 66 | with Sem_Mech; use Sem_Mech; |
70482933 | 67 | with Sem_Res; use Sem_Res; |
d06b3b1d | 68 | with Sem_SCIL; use Sem_SCIL; |
70482933 RK |
69 | with Sem_Util; use Sem_Util; |
70 | with Sinfo; use Sinfo; | |
71 | with Snames; use Snames; | |
72 | with Stand; use Stand; | |
8b404dac | 73 | with Stringt; use Stringt; |
2b3d67a5 | 74 | with Targparm; use Targparm; |
70482933 RK |
75 | with Tbuild; use Tbuild; |
76 | with Uintp; use Uintp; | |
77 | with Validsw; use Validsw; | |
78 | ||
79 | package body Exp_Ch6 is | |
80 | ||
81 | ----------------------- | |
82 | -- Local Subprograms -- | |
83 | ----------------------- | |
84 | ||
02822a92 RD |
85 | procedure Add_Access_Actual_To_Build_In_Place_Call |
86 | (Function_Call : Node_Id; | |
87 | Function_Id : Entity_Id; | |
f937473f RD |
88 | Return_Object : Node_Id; |
89 | Is_Access : Boolean := False); | |
02822a92 RD |
90 | -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the |
91 | -- object name given by Return_Object and add the attribute to the end of | |
92 | -- the actual parameter list associated with the build-in-place function | |
f937473f RD |
93 | -- call denoted by Function_Call. However, if Is_Access is True, then |
94 | -- Return_Object is already an access expression, in which case it's passed | |
95 | -- along directly to the build-in-place function. Finally, if Return_Object | |
96 | -- is empty, then pass a null literal as the actual. | |
97 | ||
200b7162 | 98 | procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call |
f937473f RD |
99 | (Function_Call : Node_Id; |
100 | Function_Id : Entity_Id; | |
101 | Alloc_Form : BIP_Allocation_Form := Unspecified; | |
200b7162 BD |
102 | Alloc_Form_Exp : Node_Id := Empty; |
103 | Pool_Actual : Node_Id := Make_Null (No_Location)); | |
104 | -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place | |
105 | -- function call that returns a caller-unknown-size result (BIP_Alloc_Form | |
106 | -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it, | |
107 | -- otherwise pass a literal corresponding to the Alloc_Form parameter | |
108 | -- (which must not be Unspecified in that case). Pool_Actual is the | |
109 | -- parameter to pass to BIP_Storage_Pool. | |
f937473f | 110 | |
d3f70b35 | 111 | procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call |
2c17ca0a AC |
112 | (Func_Call : Node_Id; |
113 | Func_Id : Entity_Id; | |
114 | Ptr_Typ : Entity_Id := Empty; | |
115 | Master_Exp : Node_Id := Empty); | |
df3e68b1 HK |
116 | -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs |
117 | -- finalization actions, add an actual parameter which is a pointer to the | |
2c17ca0a AC |
118 | -- finalization master of the caller. If Master_Exp is not Empty, then that |
119 | -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this | |
120 | -- will result in an automatic "null" value for the actual. | |
f937473f RD |
121 | |
122 | procedure Add_Task_Actuals_To_Build_In_Place_Call | |
123 | (Function_Call : Node_Id; | |
124 | Function_Id : Entity_Id; | |
1399d355 AC |
125 | Master_Actual : Node_Id; |
126 | Chain : Node_Id := Empty); | |
f937473f RD |
127 | -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type |
128 | -- contains tasks, add two actual parameters: the master, and a pointer to | |
129 | -- the caller's activation chain. Master_Actual is the actual parameter | |
130 | -- expression to pass for the master. In most cases, this is the current | |
131 | -- master (_master). The two exceptions are: If the function call is the | |
132 | -- initialization expression for an allocator, we pass the master of the | |
6dfc5592 | 133 | -- access type. If the function call is the initialization expression for a |
1399d355 AC |
134 | -- return object, we pass along the master passed in by the caller. In most |
135 | -- contexts, the activation chain to pass is the local one, which is | |
136 | -- indicated by No (Chain). However, in an allocator, the caller passes in | |
137 | -- the activation Chain. Note: Master_Actual can be Empty, but only if | |
138 | -- there are no tasks. | |
02822a92 | 139 | |
70482933 RK |
140 | procedure Check_Overriding_Operation (Subp : Entity_Id); |
141 | -- Subp is a dispatching operation. Check whether it may override an | |
142 | -- inherited private operation, in which case its DT entry is that of | |
143 | -- the hidden operation, not the one it may have received earlier. | |
144 | -- This must be done before emitting the code to set the corresponding | |
145 | -- DT to the address of the subprogram. The actual placement of Subp in | |
146 | -- the proper place in the list of primitive operations is done in | |
147 | -- Declare_Inherited_Private_Subprograms, which also has to deal with | |
148 | -- implicit operations. This duplication is unavoidable for now??? | |
149 | ||
150 | procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id); | |
151 | -- This procedure is called only if the subprogram body N, whose spec | |
152 | -- has the given entity Spec, contains a parameterless recursive call. | |
153 | -- It attempts to generate runtime code to detect if this a case of | |
154 | -- infinite recursion. | |
155 | -- | |
156 | -- The body is scanned to determine dependencies. If the only external | |
157 | -- dependencies are on a small set of scalar variables, then the values | |
158 | -- of these variables are captured on entry to the subprogram, and if | |
159 | -- the values are not changed for the call, we know immediately that | |
160 | -- we have an infinite recursion. | |
161 | ||
da574a86 AC |
162 | procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id); |
163 | -- For each actual of an in-out or out parameter which is a numeric | |
164 | -- (view) conversion of the form T (A), where A denotes a variable, | |
165 | -- we insert the declaration: | |
166 | -- | |
167 | -- Temp : T[ := T (A)]; | |
168 | -- | |
169 | -- prior to the call. Then we replace the actual with a reference to Temp, | |
170 | -- and append the assignment: | |
171 | -- | |
172 | -- A := TypeA (Temp); | |
173 | -- | |
174 | -- after the call. Here TypeA is the actual type of variable A. For out | |
175 | -- parameters, the initial declaration has no expression. If A is not an | |
176 | -- entity name, we generate instead: | |
177 | -- | |
178 | -- Var : TypeA renames A; | |
179 | -- Temp : T := Var; -- omitting expression for out parameter. | |
180 | -- ... | |
181 | -- Var := TypeA (Temp); | |
182 | -- | |
183 | -- For other in-out parameters, we emit the required constraint checks | |
184 | -- before and/or after the call. | |
185 | -- | |
186 | -- For all parameter modes, actuals that denote components and slices of | |
187 | -- packed arrays are expanded into suitable temporaries. | |
188 | -- | |
189 | -- For non-scalar objects that are possibly unaligned, add call by copy | |
190 | -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT). | |
191 | -- | |
5f6fb720 AC |
192 | -- For OUT and IN OUT parameters, add predicate checks after the call |
193 | -- based on the predicates of the actual type. | |
194 | -- | |
da574a86 AC |
195 | -- The parameter N is IN OUT because in some cases, the expansion code |
196 | -- rewrites the call as an expression actions with the call inside. In | |
197 | -- this case N is reset to point to the inside call so that the caller | |
198 | -- can continue processing of this call. | |
199 | ||
df3e68b1 HK |
200 | procedure Expand_Ctrl_Function_Call (N : Node_Id); |
201 | -- N is a function call which returns a controlled object. Transform the | |
202 | -- call into a temporary which retrieves the returned object from the | |
203 | -- secondary stack using 'reference. | |
204 | ||
2b3d67a5 AC |
205 | procedure Expand_Non_Function_Return (N : Node_Id); |
206 | -- Called by Expand_N_Simple_Return_Statement in case we're returning from | |
207 | -- a procedure body, entry body, accept statement, or extended return | |
aeae67ed | 208 | -- statement. Note that all non-function returns are simple return |
2b3d67a5 AC |
209 | -- statements. |
210 | ||
70482933 RK |
211 | function Expand_Protected_Object_Reference |
212 | (N : Node_Id; | |
02822a92 | 213 | Scop : Entity_Id) return Node_Id; |
70482933 RK |
214 | |
215 | procedure Expand_Protected_Subprogram_Call | |
216 | (N : Node_Id; | |
217 | Subp : Entity_Id; | |
218 | Scop : Entity_Id); | |
219 | -- A call to a protected subprogram within the protected object may appear | |
220 | -- as a regular call. The list of actuals must be expanded to contain a | |
221 | -- reference to the object itself, and the call becomes a call to the | |
222 | -- corresponding protected subprogram. | |
223 | ||
63585f75 SB |
224 | function Has_Unconstrained_Access_Discriminants |
225 | (Subtyp : Entity_Id) return Boolean; | |
226 | -- Returns True if the given subtype is unconstrained and has one | |
227 | -- or more access discriminants. | |
228 | ||
2b3d67a5 AC |
229 | procedure Expand_Simple_Function_Return (N : Node_Id); |
230 | -- Expand simple return from function. In the case where we are returning | |
231 | -- from a function body this is called by Expand_N_Simple_Return_Statement. | |
232 | ||
02822a92 RD |
233 | ---------------------------------------------- |
234 | -- Add_Access_Actual_To_Build_In_Place_Call -- | |
235 | ---------------------------------------------- | |
236 | ||
237 | procedure Add_Access_Actual_To_Build_In_Place_Call | |
238 | (Function_Call : Node_Id; | |
239 | Function_Id : Entity_Id; | |
f937473f RD |
240 | Return_Object : Node_Id; |
241 | Is_Access : Boolean := False) | |
02822a92 RD |
242 | is |
243 | Loc : constant Source_Ptr := Sloc (Function_Call); | |
244 | Obj_Address : Node_Id; | |
f937473f | 245 | Obj_Acc_Formal : Entity_Id; |
02822a92 RD |
246 | |
247 | begin | |
f937473f | 248 | -- Locate the implicit access parameter in the called function |
02822a92 | 249 | |
f937473f | 250 | Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access); |
02822a92 | 251 | |
f937473f RD |
252 | -- If no return object is provided, then pass null |
253 | ||
254 | if not Present (Return_Object) then | |
255 | Obj_Address := Make_Null (Loc); | |
7888a6ae | 256 | Set_Parent (Obj_Address, Function_Call); |
02822a92 | 257 | |
f937473f RD |
258 | -- If Return_Object is already an expression of an access type, then use |
259 | -- it directly, since it must be an access value denoting the return | |
260 | -- object, and couldn't possibly be the return object itself. | |
261 | ||
262 | elsif Is_Access then | |
263 | Obj_Address := Return_Object; | |
7888a6ae | 264 | Set_Parent (Obj_Address, Function_Call); |
02822a92 RD |
265 | |
266 | -- Apply Unrestricted_Access to caller's return object | |
267 | ||
f937473f RD |
268 | else |
269 | Obj_Address := | |
270 | Make_Attribute_Reference (Loc, | |
271 | Prefix => Return_Object, | |
272 | Attribute_Name => Name_Unrestricted_Access); | |
7888a6ae GD |
273 | |
274 | Set_Parent (Return_Object, Obj_Address); | |
275 | Set_Parent (Obj_Address, Function_Call); | |
f937473f | 276 | end if; |
02822a92 RD |
277 | |
278 | Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal)); | |
279 | ||
280 | -- Build the parameter association for the new actual and add it to the | |
281 | -- end of the function's actuals. | |
282 | ||
f937473f RD |
283 | Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address); |
284 | end Add_Access_Actual_To_Build_In_Place_Call; | |
285 | ||
3e7302c3 | 286 | ------------------------------------------------------ |
200b7162 | 287 | -- Add_Unconstrained_Actuals_To_Build_In_Place_Call -- |
3e7302c3 | 288 | ------------------------------------------------------ |
f937473f | 289 | |
200b7162 | 290 | procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call |
f937473f RD |
291 | (Function_Call : Node_Id; |
292 | Function_Id : Entity_Id; | |
293 | Alloc_Form : BIP_Allocation_Form := Unspecified; | |
200b7162 BD |
294 | Alloc_Form_Exp : Node_Id := Empty; |
295 | Pool_Actual : Node_Id := Make_Null (No_Location)) | |
f937473f RD |
296 | is |
297 | Loc : constant Source_Ptr := Sloc (Function_Call); | |
298 | Alloc_Form_Actual : Node_Id; | |
299 | Alloc_Form_Formal : Node_Id; | |
200b7162 | 300 | Pool_Formal : Node_Id; |
f937473f RD |
301 | |
302 | begin | |
7888a6ae GD |
303 | -- The allocation form generally doesn't need to be passed in the case |
304 | -- of a constrained result subtype, since normally the caller performs | |
305 | -- the allocation in that case. However this formal is still needed in | |
306 | -- the case where the function has a tagged result, because generally | |
307 | -- such functions can be called in a dispatching context and such calls | |
308 | -- must be handled like calls to class-wide functions. | |
309 | ||
310 | if Is_Constrained (Underlying_Type (Etype (Function_Id))) | |
311 | and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id))) | |
312 | then | |
313 | return; | |
314 | end if; | |
315 | ||
f937473f RD |
316 | -- Locate the implicit allocation form parameter in the called function. |
317 | -- Maybe it would be better for each implicit formal of a build-in-place | |
318 | -- function to have a flag or a Uint attribute to identify it. ??? | |
319 | ||
320 | Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form); | |
321 | ||
322 | if Present (Alloc_Form_Exp) then | |
323 | pragma Assert (Alloc_Form = Unspecified); | |
324 | ||
325 | Alloc_Form_Actual := Alloc_Form_Exp; | |
326 | ||
327 | else | |
328 | pragma Assert (Alloc_Form /= Unspecified); | |
329 | ||
330 | Alloc_Form_Actual := | |
331 | Make_Integer_Literal (Loc, | |
332 | Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form))); | |
333 | end if; | |
334 | ||
335 | Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal)); | |
336 | ||
337 | -- Build the parameter association for the new actual and add it to the | |
338 | -- end of the function's actuals. | |
339 | ||
340 | Add_Extra_Actual_To_Call | |
341 | (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual); | |
200b7162 | 342 | |
ea10ca9c | 343 | -- Pass the Storage_Pool parameter. This parameter is omitted on |
3e452820 | 344 | -- .NET/JVM/ZFP as those targets do not support pools. |
200b7162 | 345 | |
ea10ca9c AC |
346 | if VM_Target = No_VM |
347 | and then RTE_Available (RE_Root_Storage_Pool_Ptr) | |
3e452820 | 348 | then |
8417f4b2 AC |
349 | Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool); |
350 | Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal)); | |
351 | Add_Extra_Actual_To_Call | |
352 | (Function_Call, Pool_Formal, Pool_Actual); | |
353 | end if; | |
200b7162 | 354 | end Add_Unconstrained_Actuals_To_Build_In_Place_Call; |
f937473f | 355 | |
d3f70b35 AC |
356 | ----------------------------------------------------------- |
357 | -- Add_Finalization_Master_Actual_To_Build_In_Place_Call -- | |
358 | ----------------------------------------------------------- | |
df3e68b1 | 359 | |
d3f70b35 | 360 | procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call |
2c17ca0a AC |
361 | (Func_Call : Node_Id; |
362 | Func_Id : Entity_Id; | |
363 | Ptr_Typ : Entity_Id := Empty; | |
364 | Master_Exp : Node_Id := Empty) | |
df3e68b1 HK |
365 | is |
366 | begin | |
d3f70b35 | 367 | if not Needs_BIP_Finalization_Master (Func_Id) then |
df3e68b1 HK |
368 | return; |
369 | end if; | |
370 | ||
371 | declare | |
372 | Formal : constant Entity_Id := | |
d3f70b35 | 373 | Build_In_Place_Formal (Func_Id, BIP_Finalization_Master); |
df3e68b1 HK |
374 | Loc : constant Source_Ptr := Sloc (Func_Call); |
375 | ||
376 | Actual : Node_Id; | |
377 | Desig_Typ : Entity_Id; | |
378 | ||
379 | begin | |
2c17ca0a AC |
380 | -- If there is a finalization master actual, such as the implicit |
381 | -- finalization master of an enclosing build-in-place function, | |
382 | -- then this must be added as an extra actual of the call. | |
383 | ||
384 | if Present (Master_Exp) then | |
385 | Actual := Master_Exp; | |
386 | ||
d3f70b35 | 387 | -- Case where the context does not require an actual master |
df3e68b1 | 388 | |
2c17ca0a | 389 | elsif No (Ptr_Typ) then |
df3e68b1 HK |
390 | Actual := Make_Null (Loc); |
391 | ||
392 | else | |
393 | Desig_Typ := Directly_Designated_Type (Ptr_Typ); | |
394 | ||
395 | -- Check for a library-level access type whose designated type has | |
d3f70b35 | 396 | -- supressed finalization. Such an access types lack a master. |
df3e68b1 | 397 | -- Pass a null actual to the callee in order to signal a missing |
d3f70b35 | 398 | -- master. |
df3e68b1 HK |
399 | |
400 | if Is_Library_Level_Entity (Ptr_Typ) | |
401 | and then Finalize_Storage_Only (Desig_Typ) | |
402 | then | |
403 | Actual := Make_Null (Loc); | |
404 | ||
405 | -- Types in need of finalization actions | |
406 | ||
407 | elsif Needs_Finalization (Desig_Typ) then | |
408 | ||
d3f70b35 AC |
409 | -- The general mechanism of creating finalization masters for |
410 | -- anonymous access types is disabled by default, otherwise | |
411 | -- finalization masters will pop all over the place. Such types | |
412 | -- use context-specific masters. | |
df3e68b1 HK |
413 | |
414 | if Ekind (Ptr_Typ) = E_Anonymous_Access_Type | |
d3f70b35 | 415 | and then No (Finalization_Master (Ptr_Typ)) |
df3e68b1 | 416 | then |
d3f70b35 | 417 | Build_Finalization_Master |
df3e68b1 HK |
418 | (Typ => Ptr_Typ, |
419 | Ins_Node => Associated_Node_For_Itype (Ptr_Typ), | |
420 | Encl_Scope => Scope (Ptr_Typ)); | |
421 | end if; | |
422 | ||
d3f70b35 | 423 | -- Access-to-controlled types should always have a master |
df3e68b1 | 424 | |
d3f70b35 | 425 | pragma Assert (Present (Finalization_Master (Ptr_Typ))); |
df3e68b1 HK |
426 | |
427 | Actual := | |
428 | Make_Attribute_Reference (Loc, | |
429 | Prefix => | |
e4494292 | 430 | New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc), |
df3e68b1 HK |
431 | Attribute_Name => Name_Unrestricted_Access); |
432 | ||
433 | -- Tagged types | |
434 | ||
435 | else | |
436 | Actual := Make_Null (Loc); | |
437 | end if; | |
438 | end if; | |
439 | ||
440 | Analyze_And_Resolve (Actual, Etype (Formal)); | |
441 | ||
442 | -- Build the parameter association for the new actual and add it to | |
443 | -- the end of the function's actuals. | |
444 | ||
445 | Add_Extra_Actual_To_Call (Func_Call, Formal, Actual); | |
446 | end; | |
d3f70b35 | 447 | end Add_Finalization_Master_Actual_To_Build_In_Place_Call; |
df3e68b1 | 448 | |
f937473f RD |
449 | ------------------------------ |
450 | -- Add_Extra_Actual_To_Call -- | |
451 | ------------------------------ | |
452 | ||
453 | procedure Add_Extra_Actual_To_Call | |
454 | (Subprogram_Call : Node_Id; | |
455 | Extra_Formal : Entity_Id; | |
456 | Extra_Actual : Node_Id) | |
457 | is | |
458 | Loc : constant Source_Ptr := Sloc (Subprogram_Call); | |
459 | Param_Assoc : Node_Id; | |
460 | ||
461 | begin | |
02822a92 RD |
462 | Param_Assoc := |
463 | Make_Parameter_Association (Loc, | |
f937473f RD |
464 | Selector_Name => New_Occurrence_Of (Extra_Formal, Loc), |
465 | Explicit_Actual_Parameter => Extra_Actual); | |
02822a92 | 466 | |
f937473f RD |
467 | Set_Parent (Param_Assoc, Subprogram_Call); |
468 | Set_Parent (Extra_Actual, Param_Assoc); | |
02822a92 | 469 | |
f937473f RD |
470 | if Present (Parameter_Associations (Subprogram_Call)) then |
471 | if Nkind (Last (Parameter_Associations (Subprogram_Call))) = | |
02822a92 RD |
472 | N_Parameter_Association |
473 | then | |
f937473f RD |
474 | |
475 | -- Find last named actual, and append | |
476 | ||
477 | declare | |
478 | L : Node_Id; | |
479 | begin | |
480 | L := First_Actual (Subprogram_Call); | |
481 | while Present (L) loop | |
482 | if No (Next_Actual (L)) then | |
483 | Set_Next_Named_Actual (Parent (L), Extra_Actual); | |
484 | exit; | |
485 | end if; | |
486 | Next_Actual (L); | |
487 | end loop; | |
488 | end; | |
489 | ||
02822a92 | 490 | else |
f937473f | 491 | Set_First_Named_Actual (Subprogram_Call, Extra_Actual); |
02822a92 RD |
492 | end if; |
493 | ||
f937473f | 494 | Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call)); |
02822a92 RD |
495 | |
496 | else | |
f937473f RD |
497 | Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc)); |
498 | Set_First_Named_Actual (Subprogram_Call, Extra_Actual); | |
02822a92 | 499 | end if; |
f937473f RD |
500 | end Add_Extra_Actual_To_Call; |
501 | ||
f937473f RD |
502 | --------------------------------------------- |
503 | -- Add_Task_Actuals_To_Build_In_Place_Call -- | |
504 | --------------------------------------------- | |
505 | ||
506 | procedure Add_Task_Actuals_To_Build_In_Place_Call | |
507 | (Function_Call : Node_Id; | |
508 | Function_Id : Entity_Id; | |
1399d355 AC |
509 | Master_Actual : Node_Id; |
510 | Chain : Node_Id := Empty) | |
f937473f | 511 | is |
af89615f AC |
512 | Loc : constant Source_Ptr := Sloc (Function_Call); |
513 | Result_Subt : constant Entity_Id := | |
514 | Available_View (Etype (Function_Id)); | |
515 | Actual : Node_Id; | |
516 | Chain_Actual : Node_Id; | |
517 | Chain_Formal : Node_Id; | |
518 | Master_Formal : Node_Id; | |
6dfc5592 | 519 | |
f937473f RD |
520 | begin |
521 | -- No such extra parameters are needed if there are no tasks | |
522 | ||
1a36a0cd | 523 | if not Has_Task (Result_Subt) then |
f937473f RD |
524 | return; |
525 | end if; | |
526 | ||
af89615f AC |
527 | Actual := Master_Actual; |
528 | ||
44bf8eb0 AC |
529 | -- Use a dummy _master actual in case of No_Task_Hierarchy |
530 | ||
531 | if Restriction_Active (No_Task_Hierarchy) then | |
532 | Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc); | |
94bbf008 AC |
533 | |
534 | -- In the case where we use the master associated with an access type, | |
535 | -- the actual is an entity and requires an explicit reference. | |
536 | ||
537 | elsif Nkind (Actual) = N_Defining_Identifier then | |
e4494292 | 538 | Actual := New_Occurrence_Of (Actual, Loc); |
44bf8eb0 AC |
539 | end if; |
540 | ||
af89615f | 541 | -- Locate the implicit master parameter in the called function |
f937473f | 542 | |
af89615f AC |
543 | Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master); |
544 | Analyze_And_Resolve (Actual, Etype (Master_Formal)); | |
f937473f | 545 | |
af89615f AC |
546 | -- Build the parameter association for the new actual and add it to the |
547 | -- end of the function's actuals. | |
f937473f | 548 | |
af89615f | 549 | Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual); |
75a64833 | 550 | |
af89615f | 551 | -- Locate the implicit activation chain parameter in the called function |
f937473f | 552 | |
af89615f AC |
553 | Chain_Formal := |
554 | Build_In_Place_Formal (Function_Id, BIP_Activation_Chain); | |
f937473f | 555 | |
af89615f | 556 | -- Create the actual which is a pointer to the current activation chain |
f937473f | 557 | |
1399d355 AC |
558 | if No (Chain) then |
559 | Chain_Actual := | |
560 | Make_Attribute_Reference (Loc, | |
561 | Prefix => Make_Identifier (Loc, Name_uChain), | |
562 | Attribute_Name => Name_Unrestricted_Access); | |
563 | ||
564 | -- Allocator case; make a reference to the Chain passed in by the caller | |
565 | ||
566 | else | |
567 | Chain_Actual := | |
568 | Make_Attribute_Reference (Loc, | |
569 | Prefix => New_Occurrence_Of (Chain, Loc), | |
570 | Attribute_Name => Name_Unrestricted_Access); | |
571 | end if; | |
f937473f | 572 | |
af89615f | 573 | Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal)); |
f937473f | 574 | |
af89615f AC |
575 | -- Build the parameter association for the new actual and add it to the |
576 | -- end of the function's actuals. | |
f937473f | 577 | |
af89615f | 578 | Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual); |
f937473f RD |
579 | end Add_Task_Actuals_To_Build_In_Place_Call; |
580 | ||
581 | ----------------------- | |
582 | -- BIP_Formal_Suffix -- | |
583 | ----------------------- | |
584 | ||
585 | function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is | |
586 | begin | |
587 | case Kind is | |
d3f70b35 | 588 | when BIP_Alloc_Form => |
f937473f | 589 | return "BIPalloc"; |
af89615f | 590 | when BIP_Storage_Pool => |
200b7162 | 591 | return "BIPstoragepool"; |
d3f70b35 AC |
592 | when BIP_Finalization_Master => |
593 | return "BIPfinalizationmaster"; | |
af89615f AC |
594 | when BIP_Task_Master => |
595 | return "BIPtaskmaster"; | |
d3f70b35 | 596 | when BIP_Activation_Chain => |
f937473f | 597 | return "BIPactivationchain"; |
d3f70b35 | 598 | when BIP_Object_Access => |
f937473f RD |
599 | return "BIPaccess"; |
600 | end case; | |
601 | end BIP_Formal_Suffix; | |
602 | ||
603 | --------------------------- | |
604 | -- Build_In_Place_Formal -- | |
605 | --------------------------- | |
606 | ||
607 | function Build_In_Place_Formal | |
608 | (Func : Entity_Id; | |
609 | Kind : BIP_Formal_Kind) return Entity_Id | |
610 | is | |
af89615f AC |
611 | Formal_Name : constant Name_Id := |
612 | New_External_Name | |
613 | (Chars (Func), BIP_Formal_Suffix (Kind)); | |
f937473f RD |
614 | Extra_Formal : Entity_Id := Extra_Formals (Func); |
615 | ||
616 | begin | |
617 | -- Maybe it would be better for each implicit formal of a build-in-place | |
618 | -- function to have a flag or a Uint attribute to identify it. ??? | |
619 | ||
0d566e01 ES |
620 | -- The return type in the function declaration may have been a limited |
621 | -- view, and the extra formals for the function were not generated at | |
aeae67ed | 622 | -- that point. At the point of call the full view must be available and |
0d566e01 ES |
623 | -- the extra formals can be created. |
624 | ||
625 | if No (Extra_Formal) then | |
626 | Create_Extra_Formals (Func); | |
627 | Extra_Formal := Extra_Formals (Func); | |
628 | end if; | |
629 | ||
f937473f | 630 | loop |
19590d70 | 631 | pragma Assert (Present (Extra_Formal)); |
af89615f AC |
632 | exit when Chars (Extra_Formal) = Formal_Name; |
633 | ||
f937473f RD |
634 | Next_Formal_With_Extras (Extra_Formal); |
635 | end loop; | |
636 | ||
f937473f RD |
637 | return Extra_Formal; |
638 | end Build_In_Place_Formal; | |
02822a92 | 639 | |
c9a4817d RD |
640 | -------------------------------- |
641 | -- Check_Overriding_Operation -- | |
642 | -------------------------------- | |
70482933 RK |
643 | |
644 | procedure Check_Overriding_Operation (Subp : Entity_Id) is | |
645 | Typ : constant Entity_Id := Find_Dispatching_Type (Subp); | |
646 | Op_List : constant Elist_Id := Primitive_Operations (Typ); | |
647 | Op_Elmt : Elmt_Id; | |
648 | Prim_Op : Entity_Id; | |
649 | Par_Op : Entity_Id; | |
650 | ||
651 | begin | |
652 | if Is_Derived_Type (Typ) | |
653 | and then not Is_Private_Type (Typ) | |
654 | and then In_Open_Scopes (Scope (Etype (Typ))) | |
d347f572 | 655 | and then Is_Base_Type (Typ) |
70482933 | 656 | then |
2f1b20a9 ES |
657 | -- Subp overrides an inherited private operation if there is an |
658 | -- inherited operation with a different name than Subp (see | |
659 | -- Derive_Subprogram) whose Alias is a hidden subprogram with the | |
660 | -- same name as Subp. | |
70482933 RK |
661 | |
662 | Op_Elmt := First_Elmt (Op_List); | |
663 | while Present (Op_Elmt) loop | |
664 | Prim_Op := Node (Op_Elmt); | |
665 | Par_Op := Alias (Prim_Op); | |
666 | ||
667 | if Present (Par_Op) | |
668 | and then not Comes_From_Source (Prim_Op) | |
669 | and then Chars (Prim_Op) /= Chars (Par_Op) | |
670 | and then Chars (Par_Op) = Chars (Subp) | |
671 | and then Is_Hidden (Par_Op) | |
672 | and then Type_Conformant (Prim_Op, Subp) | |
673 | then | |
674 | Set_DT_Position (Subp, DT_Position (Prim_Op)); | |
675 | end if; | |
676 | ||
677 | Next_Elmt (Op_Elmt); | |
678 | end loop; | |
679 | end if; | |
680 | end Check_Overriding_Operation; | |
681 | ||
682 | ------------------------------- | |
683 | -- Detect_Infinite_Recursion -- | |
684 | ------------------------------- | |
685 | ||
686 | procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is | |
687 | Loc : constant Source_Ptr := Sloc (N); | |
688 | ||
fbf5a39b | 689 | Var_List : constant Elist_Id := New_Elmt_List; |
70482933 RK |
690 | -- List of globals referenced by body of procedure |
691 | ||
fbf5a39b | 692 | Call_List : constant Elist_Id := New_Elmt_List; |
70482933 RK |
693 | -- List of recursive calls in body of procedure |
694 | ||
fbf5a39b | 695 | Shad_List : constant Elist_Id := New_Elmt_List; |
2f1b20a9 ES |
696 | -- List of entity id's for entities created to capture the value of |
697 | -- referenced globals on entry to the procedure. | |
70482933 RK |
698 | |
699 | Scop : constant Uint := Scope_Depth (Spec); | |
2f1b20a9 ES |
700 | -- This is used to record the scope depth of the current procedure, so |
701 | -- that we can identify global references. | |
70482933 RK |
702 | |
703 | Max_Vars : constant := 4; | |
704 | -- Do not test more than four global variables | |
705 | ||
706 | Count_Vars : Natural := 0; | |
707 | -- Count variables found so far | |
708 | ||
709 | Var : Entity_Id; | |
710 | Elm : Elmt_Id; | |
711 | Ent : Entity_Id; | |
712 | Call : Elmt_Id; | |
713 | Decl : Node_Id; | |
714 | Test : Node_Id; | |
715 | Elm1 : Elmt_Id; | |
716 | Elm2 : Elmt_Id; | |
717 | Last : Node_Id; | |
718 | ||
719 | function Process (Nod : Node_Id) return Traverse_Result; | |
720 | -- Function to traverse the subprogram body (using Traverse_Func) | |
721 | ||
722 | ------------- | |
723 | -- Process -- | |
724 | ------------- | |
725 | ||
726 | function Process (Nod : Node_Id) return Traverse_Result is | |
727 | begin | |
728 | -- Procedure call | |
729 | ||
730 | if Nkind (Nod) = N_Procedure_Call_Statement then | |
731 | ||
732 | -- Case of one of the detected recursive calls | |
733 | ||
734 | if Is_Entity_Name (Name (Nod)) | |
735 | and then Has_Recursive_Call (Entity (Name (Nod))) | |
736 | and then Entity (Name (Nod)) = Spec | |
737 | then | |
738 | Append_Elmt (Nod, Call_List); | |
739 | return Skip; | |
740 | ||
741 | -- Any other procedure call may have side effects | |
742 | ||
743 | else | |
744 | return Abandon; | |
745 | end if; | |
746 | ||
747 | -- A call to a pure function can always be ignored | |
748 | ||
749 | elsif Nkind (Nod) = N_Function_Call | |
750 | and then Is_Entity_Name (Name (Nod)) | |
751 | and then Is_Pure (Entity (Name (Nod))) | |
752 | then | |
753 | return Skip; | |
754 | ||
755 | -- Case of an identifier reference | |
756 | ||
757 | elsif Nkind (Nod) = N_Identifier then | |
758 | Ent := Entity (Nod); | |
759 | ||
760 | -- If no entity, then ignore the reference | |
761 | ||
762 | -- Not clear why this can happen. To investigate, remove this | |
763 | -- test and look at the crash that occurs here in 3401-004 ??? | |
764 | ||
765 | if No (Ent) then | |
766 | return Skip; | |
767 | ||
768 | -- Ignore entities with no Scope, again not clear how this | |
769 | -- can happen, to investigate, look at 4108-008 ??? | |
770 | ||
771 | elsif No (Scope (Ent)) then | |
772 | return Skip; | |
773 | ||
774 | -- Ignore the reference if not to a more global object | |
775 | ||
776 | elsif Scope_Depth (Scope (Ent)) >= Scop then | |
777 | return Skip; | |
778 | ||
779 | -- References to types, exceptions and constants are always OK | |
780 | ||
781 | elsif Is_Type (Ent) | |
782 | or else Ekind (Ent) = E_Exception | |
783 | or else Ekind (Ent) = E_Constant | |
784 | then | |
785 | return Skip; | |
786 | ||
787 | -- If other than a non-volatile scalar variable, we have some | |
788 | -- kind of global reference (e.g. to a function) that we cannot | |
789 | -- deal with so we forget the attempt. | |
790 | ||
791 | elsif Ekind (Ent) /= E_Variable | |
792 | or else not Is_Scalar_Type (Etype (Ent)) | |
fbf5a39b | 793 | or else Treat_As_Volatile (Ent) |
70482933 RK |
794 | then |
795 | return Abandon; | |
796 | ||
797 | -- Otherwise we have a reference to a global scalar | |
798 | ||
799 | else | |
800 | -- Loop through global entities already detected | |
801 | ||
802 | Elm := First_Elmt (Var_List); | |
803 | loop | |
804 | -- If not detected before, record this new global reference | |
805 | ||
806 | if No (Elm) then | |
807 | Count_Vars := Count_Vars + 1; | |
808 | ||
809 | if Count_Vars <= Max_Vars then | |
810 | Append_Elmt (Entity (Nod), Var_List); | |
811 | else | |
812 | return Abandon; | |
813 | end if; | |
814 | ||
815 | exit; | |
816 | ||
817 | -- If recorded before, ignore | |
818 | ||
819 | elsif Node (Elm) = Entity (Nod) then | |
820 | return Skip; | |
821 | ||
822 | -- Otherwise keep looking | |
823 | ||
824 | else | |
825 | Next_Elmt (Elm); | |
826 | end if; | |
827 | end loop; | |
828 | ||
829 | return Skip; | |
830 | end if; | |
831 | ||
832 | -- For all other node kinds, recursively visit syntactic children | |
833 | ||
834 | else | |
835 | return OK; | |
836 | end if; | |
837 | end Process; | |
838 | ||
02822a92 | 839 | function Traverse_Body is new Traverse_Func (Process); |
70482933 RK |
840 | |
841 | -- Start of processing for Detect_Infinite_Recursion | |
842 | ||
843 | begin | |
2f1b20a9 ES |
844 | -- Do not attempt detection in No_Implicit_Conditional mode, since we |
845 | -- won't be able to generate the code to handle the recursion in any | |
846 | -- case. | |
70482933 | 847 | |
6e937c1c | 848 | if Restriction_Active (No_Implicit_Conditionals) then |
70482933 RK |
849 | return; |
850 | end if; | |
851 | ||
852 | -- Otherwise do traversal and quit if we get abandon signal | |
853 | ||
854 | if Traverse_Body (N) = Abandon then | |
855 | return; | |
856 | ||
2f1b20a9 ES |
857 | -- We must have a call, since Has_Recursive_Call was set. If not just |
858 | -- ignore (this is only an error check, so if we have a funny situation, | |
a90bd866 | 859 | -- due to bugs or errors, we do not want to bomb). |
70482933 RK |
860 | |
861 | elsif Is_Empty_Elmt_List (Call_List) then | |
862 | return; | |
863 | end if; | |
864 | ||
865 | -- Here is the case where we detect recursion at compile time | |
866 | ||
2f1b20a9 ES |
867 | -- Push our current scope for analyzing the declarations and code that |
868 | -- we will insert for the checking. | |
70482933 | 869 | |
7888a6ae | 870 | Push_Scope (Spec); |
70482933 | 871 | |
2f1b20a9 ES |
872 | -- This loop builds temporary variables for each of the referenced |
873 | -- globals, so that at the end of the loop the list Shad_List contains | |
874 | -- these temporaries in one-to-one correspondence with the elements in | |
875 | -- Var_List. | |
70482933 RK |
876 | |
877 | Last := Empty; | |
878 | Elm := First_Elmt (Var_List); | |
879 | while Present (Elm) loop | |
880 | Var := Node (Elm); | |
c12beea0 | 881 | Ent := Make_Temporary (Loc, 'S'); |
70482933 RK |
882 | Append_Elmt (Ent, Shad_List); |
883 | ||
2f1b20a9 ES |
884 | -- Insert a declaration for this temporary at the start of the |
885 | -- declarations for the procedure. The temporaries are declared as | |
886 | -- constant objects initialized to the current values of the | |
887 | -- corresponding temporaries. | |
70482933 RK |
888 | |
889 | Decl := | |
890 | Make_Object_Declaration (Loc, | |
891 | Defining_Identifier => Ent, | |
892 | Object_Definition => New_Occurrence_Of (Etype (Var), Loc), | |
893 | Constant_Present => True, | |
894 | Expression => New_Occurrence_Of (Var, Loc)); | |
895 | ||
896 | if No (Last) then | |
897 | Prepend (Decl, Declarations (N)); | |
898 | else | |
899 | Insert_After (Last, Decl); | |
900 | end if; | |
901 | ||
902 | Last := Decl; | |
903 | Analyze (Decl); | |
904 | Next_Elmt (Elm); | |
905 | end loop; | |
906 | ||
907 | -- Loop through calls | |
908 | ||
909 | Call := First_Elmt (Call_List); | |
910 | while Present (Call) loop | |
911 | ||
912 | -- Build a predicate expression of the form | |
913 | ||
914 | -- True | |
915 | -- and then global1 = temp1 | |
916 | -- and then global2 = temp2 | |
917 | -- ... | |
918 | ||
919 | -- This predicate determines if any of the global values | |
920 | -- referenced by the procedure have changed since the | |
921 | -- current call, if not an infinite recursion is assured. | |
922 | ||
923 | Test := New_Occurrence_Of (Standard_True, Loc); | |
924 | ||
925 | Elm1 := First_Elmt (Var_List); | |
926 | Elm2 := First_Elmt (Shad_List); | |
927 | while Present (Elm1) loop | |
928 | Test := | |
929 | Make_And_Then (Loc, | |
930 | Left_Opnd => Test, | |
931 | Right_Opnd => | |
932 | Make_Op_Eq (Loc, | |
933 | Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc), | |
934 | Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc))); | |
935 | ||
936 | Next_Elmt (Elm1); | |
937 | Next_Elmt (Elm2); | |
938 | end loop; | |
939 | ||
940 | -- Now we replace the call with the sequence | |
941 | ||
942 | -- if no-changes (see above) then | |
943 | -- raise Storage_Error; | |
944 | -- else | |
945 | -- original-call | |
946 | -- end if; | |
947 | ||
948 | Rewrite (Node (Call), | |
949 | Make_If_Statement (Loc, | |
950 | Condition => Test, | |
951 | Then_Statements => New_List ( | |
07fc65c4 GB |
952 | Make_Raise_Storage_Error (Loc, |
953 | Reason => SE_Infinite_Recursion)), | |
70482933 RK |
954 | |
955 | Else_Statements => New_List ( | |
956 | Relocate_Node (Node (Call))))); | |
957 | ||
958 | Analyze (Node (Call)); | |
959 | ||
960 | Next_Elmt (Call); | |
961 | end loop; | |
962 | ||
963 | -- Remove temporary scope stack entry used for analysis | |
964 | ||
965 | Pop_Scope; | |
966 | end Detect_Infinite_Recursion; | |
967 | ||
968 | -------------------- | |
969 | -- Expand_Actuals -- | |
970 | -------------------- | |
971 | ||
da574a86 | 972 | procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id) is |
70482933 RK |
973 | Loc : constant Source_Ptr := Sloc (N); |
974 | Actual : Node_Id; | |
975 | Formal : Entity_Id; | |
976 | N_Node : Node_Id; | |
977 | Post_Call : List_Id; | |
f6820c2d | 978 | E_Actual : Entity_Id; |
70482933 RK |
979 | E_Formal : Entity_Id; |
980 | ||
981 | procedure Add_Call_By_Copy_Code; | |
fbf5a39b AC |
982 | -- For cases where the parameter must be passed by copy, this routine |
983 | -- generates a temporary variable into which the actual is copied and | |
984 | -- then passes this as the parameter. For an OUT or IN OUT parameter, | |
985 | -- an assignment is also generated to copy the result back. The call | |
986 | -- also takes care of any constraint checks required for the type | |
987 | -- conversion case (on both the way in and the way out). | |
70482933 | 988 | |
f44fe430 RD |
989 | procedure Add_Simple_Call_By_Copy_Code; |
990 | -- This is similar to the above, but is used in cases where we know | |
991 | -- that all that is needed is to simply create a temporary and copy | |
992 | -- the value in and out of the temporary. | |
70482933 RK |
993 | |
994 | procedure Check_Fortran_Logical; | |
995 | -- A value of type Logical that is passed through a formal parameter | |
996 | -- must be normalized because .TRUE. usually does not have the same | |
997 | -- representation as True. We assume that .FALSE. = False = 0. | |
998 | -- What about functions that return a logical type ??? | |
999 | ||
758c442c GD |
1000 | function Is_Legal_Copy return Boolean; |
1001 | -- Check that an actual can be copied before generating the temporary | |
1002 | -- to be used in the call. If the actual is of a by_reference type then | |
1003 | -- the program is illegal (this can only happen in the presence of | |
1004 | -- rep. clauses that force an incorrect alignment). If the formal is | |
1005 | -- a by_reference parameter imposed by a DEC pragma, emit a warning to | |
1006 | -- the effect that this might lead to unaligned arguments. | |
1007 | ||
70482933 | 1008 | function Make_Var (Actual : Node_Id) return Entity_Id; |
da574a86 AC |
1009 | -- Returns an entity that refers to the given actual parameter, Actual |
1010 | -- (not including any type conversion). If Actual is an entity name, | |
1011 | -- then this entity is returned unchanged, otherwise a renaming is | |
1012 | -- created to provide an entity for the actual. | |
70482933 RK |
1013 | |
1014 | procedure Reset_Packed_Prefix; | |
1015 | -- The expansion of a packed array component reference is delayed in | |
1016 | -- the context of a call. Now we need to complete the expansion, so we | |
1017 | -- unmark the analyzed bits in all prefixes. | |
1018 | ||
1019 | --------------------------- | |
1020 | -- Add_Call_By_Copy_Code -- | |
1021 | --------------------------- | |
1022 | ||
1023 | procedure Add_Call_By_Copy_Code is | |
cc335f43 AC |
1024 | Expr : Node_Id; |
1025 | Init : Node_Id; | |
1026 | Temp : Entity_Id; | |
f44fe430 | 1027 | Indic : Node_Id; |
cc335f43 | 1028 | Var : Entity_Id; |
0da2c8ac | 1029 | F_Typ : constant Entity_Id := Etype (Formal); |
cc335f43 AC |
1030 | V_Typ : Entity_Id; |
1031 | Crep : Boolean; | |
70482933 RK |
1032 | |
1033 | begin | |
758c442c GD |
1034 | if not Is_Legal_Copy then |
1035 | return; | |
1036 | end if; | |
1037 | ||
b086849e | 1038 | Temp := Make_Temporary (Loc, 'T', Actual); |
70482933 | 1039 | |
f44fe430 RD |
1040 | -- Use formal type for temp, unless formal type is an unconstrained |
1041 | -- array, in which case we don't have to worry about bounds checks, | |
758c442c | 1042 | -- and we use the actual type, since that has appropriate bounds. |
f44fe430 RD |
1043 | |
1044 | if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then | |
1045 | Indic := New_Occurrence_Of (Etype (Actual), Loc); | |
1046 | else | |
1047 | Indic := New_Occurrence_Of (Etype (Formal), Loc); | |
1048 | end if; | |
1049 | ||
70482933 RK |
1050 | if Nkind (Actual) = N_Type_Conversion then |
1051 | V_Typ := Etype (Expression (Actual)); | |
19f0526a AC |
1052 | |
1053 | -- If the formal is an (in-)out parameter, capture the name | |
1054 | -- of the variable in order to build the post-call assignment. | |
81a5b587 AC |
1055 | |
1056 | Var := Make_Var (Expression (Actual)); | |
19f0526a | 1057 | |
08aa9a4a | 1058 | Crep := not Same_Representation |
0da2c8ac | 1059 | (F_Typ, Etype (Expression (Actual))); |
08aa9a4a | 1060 | |
70482933 RK |
1061 | else |
1062 | V_Typ := Etype (Actual); | |
1063 | Var := Make_Var (Actual); | |
1064 | Crep := False; | |
1065 | end if; | |
1066 | ||
1067 | -- Setup initialization for case of in out parameter, or an out | |
1068 | -- parameter where the formal is an unconstrained array (in the | |
1069 | -- latter case, we have to pass in an object with bounds). | |
1070 | ||
cc335f43 AC |
1071 | -- If this is an out parameter, the initial copy is wasteful, so as |
1072 | -- an optimization for the one-dimensional case we extract the | |
1073 | -- bounds of the actual and build an uninitialized temporary of the | |
1074 | -- right size. | |
1075 | ||
70482933 | 1076 | if Ekind (Formal) = E_In_Out_Parameter |
0da2c8ac | 1077 | or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ)) |
70482933 RK |
1078 | then |
1079 | if Nkind (Actual) = N_Type_Conversion then | |
1080 | if Conversion_OK (Actual) then | |
0da2c8ac | 1081 | Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); |
70482933 | 1082 | else |
0da2c8ac | 1083 | Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); |
70482933 | 1084 | end if; |
cc335f43 AC |
1085 | |
1086 | elsif Ekind (Formal) = E_Out_Parameter | |
0da2c8ac AC |
1087 | and then Is_Array_Type (F_Typ) |
1088 | and then Number_Dimensions (F_Typ) = 1 | |
1089 | and then not Has_Non_Null_Base_Init_Proc (F_Typ) | |
cc335f43 AC |
1090 | then |
1091 | -- Actual is a one-dimensional array or slice, and the type | |
1092 | -- requires no initialization. Create a temporary of the | |
f44fe430 | 1093 | -- right size, but do not copy actual into it (optimization). |
cc335f43 AC |
1094 | |
1095 | Init := Empty; | |
1096 | Indic := | |
1097 | Make_Subtype_Indication (Loc, | |
5f6fb720 | 1098 | Subtype_Mark => New_Occurrence_Of (F_Typ, Loc), |
cc335f43 AC |
1099 | Constraint => |
1100 | Make_Index_Or_Discriminant_Constraint (Loc, | |
1101 | Constraints => New_List ( | |
1102 | Make_Range (Loc, | |
1103 | Low_Bound => | |
1104 | Make_Attribute_Reference (Loc, | |
5f6fb720 | 1105 | Prefix => New_Occurrence_Of (Var, Loc), |
70f91180 | 1106 | Attribute_Name => Name_First), |
cc335f43 AC |
1107 | High_Bound => |
1108 | Make_Attribute_Reference (Loc, | |
5f6fb720 | 1109 | Prefix => New_Occurrence_Of (Var, Loc), |
cc335f43 AC |
1110 | Attribute_Name => Name_Last))))); |
1111 | ||
70482933 RK |
1112 | else |
1113 | Init := New_Occurrence_Of (Var, Loc); | |
1114 | end if; | |
1115 | ||
1116 | -- An initialization is created for packed conversions as | |
1117 | -- actuals for out parameters to enable Make_Object_Declaration | |
1118 | -- to determine the proper subtype for N_Node. Note that this | |
1119 | -- is wasteful because the extra copying on the call side is | |
1120 | -- not required for such out parameters. ??? | |
1121 | ||
1122 | elsif Ekind (Formal) = E_Out_Parameter | |
1123 | and then Nkind (Actual) = N_Type_Conversion | |
0da2c8ac | 1124 | and then (Is_Bit_Packed_Array (F_Typ) |
70482933 RK |
1125 | or else |
1126 | Is_Bit_Packed_Array (Etype (Expression (Actual)))) | |
1127 | then | |
1128 | if Conversion_OK (Actual) then | |
f44fe430 | 1129 | Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); |
70482933 | 1130 | else |
f44fe430 | 1131 | Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); |
70482933 | 1132 | end if; |
2e071734 AC |
1133 | |
1134 | elsif Ekind (Formal) = E_In_Parameter then | |
02822a92 RD |
1135 | |
1136 | -- Handle the case in which the actual is a type conversion | |
1137 | ||
1138 | if Nkind (Actual) = N_Type_Conversion then | |
1139 | if Conversion_OK (Actual) then | |
1140 | Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); | |
1141 | else | |
1142 | Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc)); | |
1143 | end if; | |
1144 | else | |
1145 | Init := New_Occurrence_Of (Var, Loc); | |
1146 | end if; | |
2e071734 | 1147 | |
70482933 RK |
1148 | else |
1149 | Init := Empty; | |
1150 | end if; | |
1151 | ||
1152 | N_Node := | |
1153 | Make_Object_Declaration (Loc, | |
1154 | Defining_Identifier => Temp, | |
cc335f43 | 1155 | Object_Definition => Indic, |
f44fe430 | 1156 | Expression => Init); |
70482933 RK |
1157 | Set_Assignment_OK (N_Node); |
1158 | Insert_Action (N, N_Node); | |
1159 | ||
1160 | -- Now, normally the deal here is that we use the defining | |
1161 | -- identifier created by that object declaration. There is | |
1162 | -- one exception to this. In the change of representation case | |
1163 | -- the above declaration will end up looking like: | |
1164 | ||
1165 | -- temp : type := identifier; | |
1166 | ||
1167 | -- And in this case we might as well use the identifier directly | |
1168 | -- and eliminate the temporary. Note that the analysis of the | |
1169 | -- declaration was not a waste of time in that case, since it is | |
1170 | -- what generated the necessary change of representation code. If | |
1171 | -- the change of representation introduced additional code, as in | |
1172 | -- a fixed-integer conversion, the expression is not an identifier | |
1173 | -- and must be kept. | |
1174 | ||
1175 | if Crep | |
1176 | and then Present (Expression (N_Node)) | |
1177 | and then Is_Entity_Name (Expression (N_Node)) | |
1178 | then | |
1179 | Temp := Entity (Expression (N_Node)); | |
1180 | Rewrite (N_Node, Make_Null_Statement (Loc)); | |
1181 | end if; | |
1182 | ||
fbf5a39b | 1183 | -- For IN parameter, all we do is to replace the actual |
70482933 | 1184 | |
fbf5a39b | 1185 | if Ekind (Formal) = E_In_Parameter then |
e4494292 | 1186 | Rewrite (Actual, New_Occurrence_Of (Temp, Loc)); |
fbf5a39b AC |
1187 | Analyze (Actual); |
1188 | ||
1189 | -- Processing for OUT or IN OUT parameter | |
1190 | ||
1191 | else | |
c8ef728f ES |
1192 | -- Kill current value indications for the temporary variable we |
1193 | -- created, since we just passed it as an OUT parameter. | |
1194 | ||
1195 | Kill_Current_Values (Temp); | |
75ba322d | 1196 | Set_Is_Known_Valid (Temp, False); |
c8ef728f | 1197 | |
fbf5a39b AC |
1198 | -- If type conversion, use reverse conversion on exit |
1199 | ||
1200 | if Nkind (Actual) = N_Type_Conversion then | |
1201 | if Conversion_OK (Actual) then | |
1202 | Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc)); | |
1203 | else | |
1204 | Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc)); | |
1205 | end if; | |
70482933 | 1206 | else |
fbf5a39b | 1207 | Expr := New_Occurrence_Of (Temp, Loc); |
70482933 | 1208 | end if; |
70482933 | 1209 | |
e4494292 | 1210 | Rewrite (Actual, New_Occurrence_Of (Temp, Loc)); |
fbf5a39b | 1211 | Analyze (Actual); |
70482933 | 1212 | |
d766cee3 RD |
1213 | -- If the actual is a conversion of a packed reference, it may |
1214 | -- already have been expanded by Remove_Side_Effects, and the | |
1215 | -- resulting variable is a temporary which does not designate | |
1216 | -- the proper out-parameter, which may not be addressable. In | |
1217 | -- that case, generate an assignment to the original expression | |
b0159fbe | 1218 | -- (before expansion of the packed reference) so that the proper |
d766cee3 | 1219 | -- expansion of assignment to a packed component can take place. |
70482933 | 1220 | |
d766cee3 RD |
1221 | declare |
1222 | Obj : Node_Id; | |
1223 | Lhs : Node_Id; | |
1224 | ||
1225 | begin | |
1226 | if Is_Renaming_Of_Object (Var) | |
1227 | and then Nkind (Renamed_Object (Var)) = N_Selected_Component | |
1228 | and then Is_Entity_Name (Prefix (Renamed_Object (Var))) | |
1229 | and then Nkind (Original_Node (Prefix (Renamed_Object (Var)))) | |
1230 | = N_Indexed_Component | |
1231 | and then | |
1232 | Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var)))) | |
1233 | then | |
1234 | Obj := Renamed_Object (Var); | |
1235 | Lhs := | |
1236 | Make_Selected_Component (Loc, | |
1237 | Prefix => | |
1238 | New_Copy_Tree (Original_Node (Prefix (Obj))), | |
1239 | Selector_Name => New_Copy (Selector_Name (Obj))); | |
1240 | Reset_Analyzed_Flags (Lhs); | |
1241 | ||
1242 | else | |
1243 | Lhs := New_Occurrence_Of (Var, Loc); | |
1244 | end if; | |
1245 | ||
1246 | Set_Assignment_OK (Lhs); | |
1247 | ||
d15f9422 AC |
1248 | if Is_Access_Type (E_Formal) |
1249 | and then Is_Entity_Name (Lhs) | |
996c8821 RD |
1250 | and then |
1251 | Present (Effective_Extra_Accessibility (Entity (Lhs))) | |
d15f9422 | 1252 | then |
4bb43ffb AC |
1253 | -- Copyback target is an Ada 2012 stand-alone object of an |
1254 | -- anonymous access type. | |
d15f9422 AC |
1255 | |
1256 | pragma Assert (Ada_Version >= Ada_2012); | |
1257 | ||
1258 | if Type_Access_Level (E_Formal) > | |
996c8821 RD |
1259 | Object_Access_Level (Lhs) |
1260 | then | |
1261 | Append_To (Post_Call, | |
1262 | Make_Raise_Program_Error (Loc, | |
1263 | Reason => PE_Accessibility_Check_Failed)); | |
d15f9422 AC |
1264 | end if; |
1265 | ||
1266 | Append_To (Post_Call, | |
1267 | Make_Assignment_Statement (Loc, | |
1268 | Name => Lhs, | |
1269 | Expression => Expr)); | |
1270 | ||
996c8821 RD |
1271 | -- We would like to somehow suppress generation of the |
1272 | -- extra_accessibility assignment generated by the expansion | |
1273 | -- of the above assignment statement. It's not a correctness | |
1274 | -- issue because the following assignment renders it dead, | |
1275 | -- but generating back-to-back assignments to the same | |
1276 | -- target is undesirable. ??? | |
d15f9422 AC |
1277 | |
1278 | Append_To (Post_Call, | |
1279 | Make_Assignment_Statement (Loc, | |
1280 | Name => New_Occurrence_Of ( | |
1281 | Effective_Extra_Accessibility (Entity (Lhs)), Loc), | |
1282 | Expression => Make_Integer_Literal (Loc, | |
1283 | Type_Access_Level (E_Formal)))); | |
996c8821 | 1284 | |
d15f9422 AC |
1285 | else |
1286 | Append_To (Post_Call, | |
1287 | Make_Assignment_Statement (Loc, | |
1288 | Name => Lhs, | |
1289 | Expression => Expr)); | |
1290 | end if; | |
d766cee3 | 1291 | end; |
fbf5a39b | 1292 | end if; |
70482933 RK |
1293 | end Add_Call_By_Copy_Code; |
1294 | ||
1295 | ---------------------------------- | |
f44fe430 | 1296 | -- Add_Simple_Call_By_Copy_Code -- |
70482933 RK |
1297 | ---------------------------------- |
1298 | ||
f44fe430 | 1299 | procedure Add_Simple_Call_By_Copy_Code is |
70482933 | 1300 | Temp : Entity_Id; |
758c442c | 1301 | Decl : Node_Id; |
70482933 RK |
1302 | Incod : Node_Id; |
1303 | Outcod : Node_Id; | |
1304 | Lhs : Node_Id; | |
1305 | Rhs : Node_Id; | |
f44fe430 RD |
1306 | Indic : Node_Id; |
1307 | F_Typ : constant Entity_Id := Etype (Formal); | |
70482933 RK |
1308 | |
1309 | begin | |
758c442c GD |
1310 | if not Is_Legal_Copy then |
1311 | return; | |
1312 | end if; | |
1313 | ||
f44fe430 RD |
1314 | -- Use formal type for temp, unless formal type is an unconstrained |
1315 | -- array, in which case we don't have to worry about bounds checks, | |
758c442c | 1316 | -- and we use the actual type, since that has appropriate bounds. |
f44fe430 RD |
1317 | |
1318 | if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then | |
1319 | Indic := New_Occurrence_Of (Etype (Actual), Loc); | |
1320 | else | |
1321 | Indic := New_Occurrence_Of (Etype (Formal), Loc); | |
1322 | end if; | |
70482933 RK |
1323 | |
1324 | -- Prepare to generate code | |
1325 | ||
f44fe430 RD |
1326 | Reset_Packed_Prefix; |
1327 | ||
b086849e | 1328 | Temp := Make_Temporary (Loc, 'T', Actual); |
70482933 RK |
1329 | Incod := Relocate_Node (Actual); |
1330 | Outcod := New_Copy_Tree (Incod); | |
1331 | ||
1332 | -- Generate declaration of temporary variable, initializing it | |
c73ae90f | 1333 | -- with the input parameter unless we have an OUT formal or |
758c442c | 1334 | -- this is an initialization call. |
70482933 | 1335 | |
c73ae90f GD |
1336 | -- If the formal is an out parameter with discriminants, the |
1337 | -- discriminants must be captured even if the rest of the object | |
1338 | -- is in principle uninitialized, because the discriminants may | |
1339 | -- be read by the called subprogram. | |
1340 | ||
70482933 RK |
1341 | if Ekind (Formal) = E_Out_Parameter then |
1342 | Incod := Empty; | |
758c442c | 1343 | |
c73ae90f GD |
1344 | if Has_Discriminants (Etype (Formal)) then |
1345 | Indic := New_Occurrence_Of (Etype (Actual), Loc); | |
1346 | end if; | |
1347 | ||
758c442c | 1348 | elsif Inside_Init_Proc then |
c73ae90f GD |
1349 | |
1350 | -- Could use a comment here to match comment below ??? | |
1351 | ||
758c442c GD |
1352 | if Nkind (Actual) /= N_Selected_Component |
1353 | or else | |
1354 | not Has_Discriminant_Dependent_Constraint | |
1355 | (Entity (Selector_Name (Actual))) | |
1356 | then | |
1357 | Incod := Empty; | |
1358 | ||
c73ae90f GD |
1359 | -- Otherwise, keep the component in order to generate the proper |
1360 | -- actual subtype, that depends on enclosing discriminants. | |
758c442c | 1361 | |
c73ae90f | 1362 | else |
758c442c GD |
1363 | null; |
1364 | end if; | |
70482933 RK |
1365 | end if; |
1366 | ||
758c442c | 1367 | Decl := |
70482933 RK |
1368 | Make_Object_Declaration (Loc, |
1369 | Defining_Identifier => Temp, | |
f44fe430 | 1370 | Object_Definition => Indic, |
758c442c GD |
1371 | Expression => Incod); |
1372 | ||
1373 | if Inside_Init_Proc | |
1374 | and then No (Incod) | |
1375 | then | |
1376 | -- If the call is to initialize a component of a composite type, | |
1377 | -- and the component does not depend on discriminants, use the | |
1378 | -- actual type of the component. This is required in case the | |
1379 | -- component is constrained, because in general the formal of the | |
1380 | -- initialization procedure will be unconstrained. Note that if | |
1381 | -- the component being initialized is constrained by an enclosing | |
1382 | -- discriminant, the presence of the initialization in the | |
1383 | -- declaration will generate an expression for the actual subtype. | |
1384 | ||
1385 | Set_No_Initialization (Decl); | |
1386 | Set_Object_Definition (Decl, | |
1387 | New_Occurrence_Of (Etype (Actual), Loc)); | |
1388 | end if; | |
1389 | ||
1390 | Insert_Action (N, Decl); | |
70482933 RK |
1391 | |
1392 | -- The actual is simply a reference to the temporary | |
1393 | ||
1394 | Rewrite (Actual, New_Occurrence_Of (Temp, Loc)); | |
1395 | ||
1396 | -- Generate copy out if OUT or IN OUT parameter | |
1397 | ||
1398 | if Ekind (Formal) /= E_In_Parameter then | |
1399 | Lhs := Outcod; | |
1400 | Rhs := New_Occurrence_Of (Temp, Loc); | |
1401 | ||
1402 | -- Deal with conversion | |
1403 | ||
1404 | if Nkind (Lhs) = N_Type_Conversion then | |
1405 | Lhs := Expression (Lhs); | |
1406 | Rhs := Convert_To (Etype (Actual), Rhs); | |
1407 | end if; | |
1408 | ||
1409 | Append_To (Post_Call, | |
1410 | Make_Assignment_Statement (Loc, | |
1411 | Name => Lhs, | |
1412 | Expression => Rhs)); | |
f44fe430 | 1413 | Set_Assignment_OK (Name (Last (Post_Call))); |
70482933 | 1414 | end if; |
f44fe430 | 1415 | end Add_Simple_Call_By_Copy_Code; |
70482933 RK |
1416 | |
1417 | --------------------------- | |
1418 | -- Check_Fortran_Logical -- | |
1419 | --------------------------- | |
1420 | ||
1421 | procedure Check_Fortran_Logical is | |
fbf5a39b | 1422 | Logical : constant Entity_Id := Etype (Formal); |
70482933 RK |
1423 | Var : Entity_Id; |
1424 | ||
1425 | -- Note: this is very incomplete, e.g. it does not handle arrays | |
1426 | -- of logical values. This is really not the right approach at all???) | |
1427 | ||
1428 | begin | |
1429 | if Convention (Subp) = Convention_Fortran | |
1430 | and then Root_Type (Etype (Formal)) = Standard_Boolean | |
1431 | and then Ekind (Formal) /= E_In_Parameter | |
1432 | then | |
1433 | Var := Make_Var (Actual); | |
1434 | Append_To (Post_Call, | |
1435 | Make_Assignment_Statement (Loc, | |
1436 | Name => New_Occurrence_Of (Var, Loc), | |
1437 | Expression => | |
1438 | Unchecked_Convert_To ( | |
1439 | Logical, | |
1440 | Make_Op_Ne (Loc, | |
1441 | Left_Opnd => New_Occurrence_Of (Var, Loc), | |
1442 | Right_Opnd => | |
1443 | Unchecked_Convert_To ( | |
1444 | Logical, | |
1445 | New_Occurrence_Of (Standard_False, Loc)))))); | |
1446 | end if; | |
1447 | end Check_Fortran_Logical; | |
1448 | ||
758c442c GD |
1449 | ------------------- |
1450 | -- Is_Legal_Copy -- | |
1451 | ------------------- | |
1452 | ||
1453 | function Is_Legal_Copy return Boolean is | |
1454 | begin | |
1455 | -- An attempt to copy a value of such a type can only occur if | |
1456 | -- representation clauses give the actual a misaligned address. | |
1457 | ||
1458 | if Is_By_Reference_Type (Etype (Formal)) then | |
f45ccc7c | 1459 | |
aaf1cd90 RD |
1460 | -- If the front-end does not perform full type layout, the actual |
1461 | -- may in fact be properly aligned but there is not enough front- | |
1462 | -- end information to determine this. In that case gigi will emit | |
1463 | -- an error if a copy is not legal, or generate the proper code. | |
1464 | -- For other backends we report the error now. | |
1465 | ||
1466 | -- Seems wrong to be issuing an error in the expander, since it | |
1467 | -- will be missed in -gnatc mode ??? | |
f45ccc7c AC |
1468 | |
1469 | if Frontend_Layout_On_Target then | |
1470 | Error_Msg_N | |
1471 | ("misaligned actual cannot be passed by reference", Actual); | |
1472 | end if; | |
1473 | ||
758c442c GD |
1474 | return False; |
1475 | ||
1476 | -- For users of Starlet, we assume that the specification of by- | |
7888a6ae | 1477 | -- reference mechanism is mandatory. This may lead to unaligned |
758c442c GD |
1478 | -- objects but at least for DEC legacy code it is known to work. |
1479 | -- The warning will alert users of this code that a problem may | |
1480 | -- be lurking. | |
1481 | ||
1482 | elsif Mechanism (Formal) = By_Reference | |
1483 | and then Is_Valued_Procedure (Scope (Formal)) | |
1484 | then | |
1485 | Error_Msg_N | |
685bc70f | 1486 | ("by_reference actual may be misaligned??", Actual); |
758c442c GD |
1487 | return False; |
1488 | ||
1489 | else | |
1490 | return True; | |
1491 | end if; | |
1492 | end Is_Legal_Copy; | |
1493 | ||
70482933 RK |
1494 | -------------- |
1495 | -- Make_Var -- | |
1496 | -------------- | |
1497 | ||
1498 | function Make_Var (Actual : Node_Id) return Entity_Id is | |
1499 | Var : Entity_Id; | |
1500 | ||
1501 | begin | |
1502 | if Is_Entity_Name (Actual) then | |
1503 | return Entity (Actual); | |
1504 | ||
1505 | else | |
b086849e | 1506 | Var := Make_Temporary (Loc, 'T', Actual); |
70482933 RK |
1507 | |
1508 | N_Node := | |
1509 | Make_Object_Renaming_Declaration (Loc, | |
1510 | Defining_Identifier => Var, | |
1511 | Subtype_Mark => | |
1512 | New_Occurrence_Of (Etype (Actual), Loc), | |
1513 | Name => Relocate_Node (Actual)); | |
1514 | ||
1515 | Insert_Action (N, N_Node); | |
1516 | return Var; | |
1517 | end if; | |
1518 | end Make_Var; | |
1519 | ||
1520 | ------------------------- | |
1521 | -- Reset_Packed_Prefix -- | |
1522 | ------------------------- | |
1523 | ||
1524 | procedure Reset_Packed_Prefix is | |
1525 | Pfx : Node_Id := Actual; | |
70482933 RK |
1526 | begin |
1527 | loop | |
1528 | Set_Analyzed (Pfx, False); | |
ac4d6407 RD |
1529 | exit when |
1530 | not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component); | |
70482933 RK |
1531 | Pfx := Prefix (Pfx); |
1532 | end loop; | |
1533 | end Reset_Packed_Prefix; | |
1534 | ||
1535 | -- Start of processing for Expand_Actuals | |
1536 | ||
1537 | begin | |
70482933 RK |
1538 | Post_Call := New_List; |
1539 | ||
2f1b20a9 ES |
1540 | Formal := First_Formal (Subp); |
1541 | Actual := First_Actual (N); | |
70482933 RK |
1542 | while Present (Formal) loop |
1543 | E_Formal := Etype (Formal); | |
f6820c2d | 1544 | E_Actual := Etype (Actual); |
70482933 RK |
1545 | |
1546 | if Is_Scalar_Type (E_Formal) | |
1547 | or else Nkind (Actual) = N_Slice | |
1548 | then | |
1549 | Check_Fortran_Logical; | |
1550 | ||
1551 | -- RM 6.4.1 (11) | |
1552 | ||
1553 | elsif Ekind (Formal) /= E_Out_Parameter then | |
1554 | ||
1555 | -- The unusual case of the current instance of a protected type | |
1556 | -- requires special handling. This can only occur in the context | |
1557 | -- of a call within the body of a protected operation. | |
1558 | ||
1559 | if Is_Entity_Name (Actual) | |
1560 | and then Ekind (Entity (Actual)) = E_Protected_Type | |
1561 | and then In_Open_Scopes (Entity (Actual)) | |
1562 | then | |
1563 | if Scope (Subp) /= Entity (Actual) then | |
685bc70f AC |
1564 | Error_Msg_N |
1565 | ("operation outside protected type may not " | |
1566 | & "call back its protected operations??", Actual); | |
70482933 RK |
1567 | end if; |
1568 | ||
1569 | Rewrite (Actual, | |
1570 | Expand_Protected_Object_Reference (N, Entity (Actual))); | |
1571 | end if; | |
1572 | ||
02822a92 RD |
1573 | -- Ada 2005 (AI-318-02): If the actual parameter is a call to a |
1574 | -- build-in-place function, then a temporary return object needs | |
1575 | -- to be created and access to it must be passed to the function. | |
f937473f RD |
1576 | -- Currently we limit such functions to those with inherently |
1577 | -- limited result subtypes, but eventually we plan to expand the | |
1578 | -- functions that are treated as build-in-place to include other | |
1579 | -- composite result types. | |
02822a92 | 1580 | |
95eb8b69 | 1581 | if Is_Build_In_Place_Function_Call (Actual) then |
02822a92 RD |
1582 | Make_Build_In_Place_Call_In_Anonymous_Context (Actual); |
1583 | end if; | |
1584 | ||
70482933 RK |
1585 | Apply_Constraint_Check (Actual, E_Formal); |
1586 | ||
1587 | -- Out parameter case. No constraint checks on access type | |
1588 | -- RM 6.4.1 (13) | |
1589 | ||
1590 | elsif Is_Access_Type (E_Formal) then | |
1591 | null; | |
1592 | ||
1593 | -- RM 6.4.1 (14) | |
1594 | ||
1595 | elsif Has_Discriminants (Base_Type (E_Formal)) | |
1596 | or else Has_Non_Null_Base_Init_Proc (E_Formal) | |
1597 | then | |
1598 | Apply_Constraint_Check (Actual, E_Formal); | |
1599 | ||
1600 | -- RM 6.4.1 (15) | |
1601 | ||
1602 | else | |
1603 | Apply_Constraint_Check (Actual, Base_Type (E_Formal)); | |
1604 | end if; | |
1605 | ||
1606 | -- Processing for IN-OUT and OUT parameters | |
1607 | ||
1608 | if Ekind (Formal) /= E_In_Parameter then | |
1609 | ||
1610 | -- For type conversions of arrays, apply length/range checks | |
1611 | ||
1612 | if Is_Array_Type (E_Formal) | |
1613 | and then Nkind (Actual) = N_Type_Conversion | |
1614 | then | |
1615 | if Is_Constrained (E_Formal) then | |
1616 | Apply_Length_Check (Expression (Actual), E_Formal); | |
1617 | else | |
1618 | Apply_Range_Check (Expression (Actual), E_Formal); | |
1619 | end if; | |
1620 | end if; | |
1621 | ||
1622 | -- If argument is a type conversion for a type that is passed | |
1623 | -- by copy, then we must pass the parameter by copy. | |
1624 | ||
1625 | if Nkind (Actual) = N_Type_Conversion | |
1626 | and then | |
1627 | (Is_Numeric_Type (E_Formal) | |
1628 | or else Is_Access_Type (E_Formal) | |
1629 | or else Is_Enumeration_Type (E_Formal) | |
1630 | or else Is_Bit_Packed_Array (Etype (Formal)) | |
1631 | or else Is_Bit_Packed_Array (Etype (Expression (Actual))) | |
1632 | ||
1633 | -- Also pass by copy if change of representation | |
1634 | ||
1635 | or else not Same_Representation | |
da574a86 AC |
1636 | (Etype (Formal), |
1637 | Etype (Expression (Actual)))) | |
70482933 RK |
1638 | then |
1639 | Add_Call_By_Copy_Code; | |
1640 | ||
1641 | -- References to components of bit packed arrays are expanded | |
1642 | -- at this point, rather than at the point of analysis of the | |
1643 | -- actuals, to handle the expansion of the assignment to | |
1644 | -- [in] out parameters. | |
1645 | ||
1646 | elsif Is_Ref_To_Bit_Packed_Array (Actual) then | |
f44fe430 RD |
1647 | Add_Simple_Call_By_Copy_Code; |
1648 | ||
02822a92 RD |
1649 | -- If a non-scalar actual is possibly bit-aligned, we need a copy |
1650 | -- because the back-end cannot cope with such objects. In other | |
1651 | -- cases where alignment forces a copy, the back-end generates | |
1652 | -- it properly. It should not be generated unconditionally in the | |
1653 | -- front-end because it does not know precisely the alignment | |
1654 | -- requirements of the target, and makes too conservative an | |
1655 | -- estimate, leading to superfluous copies or spurious errors | |
1656 | -- on by-reference parameters. | |
f44fe430 | 1657 | |
02822a92 RD |
1658 | elsif Nkind (Actual) = N_Selected_Component |
1659 | and then | |
1660 | Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual))) | |
f44fe430 RD |
1661 | and then not Represented_As_Scalar (Etype (Formal)) |
1662 | then | |
1663 | Add_Simple_Call_By_Copy_Code; | |
70482933 RK |
1664 | |
1665 | -- References to slices of bit packed arrays are expanded | |
1666 | ||
1667 | elsif Is_Ref_To_Bit_Packed_Slice (Actual) then | |
1668 | Add_Call_By_Copy_Code; | |
1669 | ||
fbf5a39b AC |
1670 | -- References to possibly unaligned slices of arrays are expanded |
1671 | ||
1672 | elsif Is_Possibly_Unaligned_Slice (Actual) then | |
1673 | Add_Call_By_Copy_Code; | |
1674 | ||
7888a6ae | 1675 | -- Deal with access types where the actual subtype and the |
70482933 RK |
1676 | -- formal subtype are not the same, requiring a check. |
1677 | ||
638e383e | 1678 | -- It is necessary to exclude tagged types because of "downward |
70f91180 | 1679 | -- conversion" errors. |
70482933 RK |
1680 | |
1681 | elsif Is_Access_Type (E_Formal) | |
f6820c2d | 1682 | and then not Same_Type (E_Formal, E_Actual) |
70482933 RK |
1683 | and then not Is_Tagged_Type (Designated_Type (E_Formal)) |
1684 | then | |
1685 | Add_Call_By_Copy_Code; | |
1686 | ||
faf3cf91 ES |
1687 | -- If the actual is not a scalar and is marked for volatile |
1688 | -- treatment, whereas the formal is not volatile, then pass | |
1689 | -- by copy unless it is a by-reference type. | |
1690 | ||
0386aad1 AC |
1691 | -- Note: we use Is_Volatile here rather than Treat_As_Volatile, |
1692 | -- because this is the enforcement of a language rule that applies | |
1693 | -- only to "real" volatile variables, not e.g. to the address | |
1694 | -- clause overlay case. | |
1695 | ||
70482933 | 1696 | elsif Is_Entity_Name (Actual) |
0386aad1 | 1697 | and then Is_Volatile (Entity (Actual)) |
f6820c2d | 1698 | and then not Is_By_Reference_Type (E_Actual) |
70482933 | 1699 | and then not Is_Scalar_Type (Etype (Entity (Actual))) |
0386aad1 | 1700 | and then not Is_Volatile (E_Formal) |
70482933 RK |
1701 | then |
1702 | Add_Call_By_Copy_Code; | |
1703 | ||
1704 | elsif Nkind (Actual) = N_Indexed_Component | |
1705 | and then Is_Entity_Name (Prefix (Actual)) | |
1706 | and then Has_Volatile_Components (Entity (Prefix (Actual))) | |
1707 | then | |
1708 | Add_Call_By_Copy_Code; | |
d79e621a GD |
1709 | |
1710 | -- Add call-by-copy code for the case of scalar out parameters | |
1711 | -- when it is not known at compile time that the subtype of the | |
c2369146 AC |
1712 | -- formal is a subrange of the subtype of the actual (or vice |
1713 | -- versa for in out parameters), in order to get range checks | |
1714 | -- on such actuals. (Maybe this case should be handled earlier | |
1715 | -- in the if statement???) | |
d79e621a GD |
1716 | |
1717 | elsif Is_Scalar_Type (E_Formal) | |
c2369146 | 1718 | and then |
f6820c2d | 1719 | (not In_Subrange_Of (E_Formal, E_Actual) |
c2369146 AC |
1720 | or else |
1721 | (Ekind (Formal) = E_In_Out_Parameter | |
f6820c2d | 1722 | and then not In_Subrange_Of (E_Actual, E_Formal))) |
d79e621a GD |
1723 | then |
1724 | -- Perhaps the setting back to False should be done within | |
1725 | -- Add_Call_By_Copy_Code, since it could get set on other | |
1726 | -- cases occurring above??? | |
1727 | ||
1728 | if Do_Range_Check (Actual) then | |
1729 | Set_Do_Range_Check (Actual, False); | |
1730 | end if; | |
1731 | ||
1732 | Add_Call_By_Copy_Code; | |
70482933 RK |
1733 | end if; |
1734 | ||
5f6fb720 | 1735 | -- RM 3.2.4 (23/3): A predicate is checked on in-out and out |
f6820c2d AC |
1736 | -- by-reference parameters on exit from the call. If the actual |
1737 | -- is a derived type and the operation is inherited, the body | |
1738 | -- of the operation will not contain a call to the predicate | |
1739 | -- function, so it must be done explicitly after the call. Ditto | |
1740 | -- if the actual is an entity of a predicated subtype. | |
1741 | ||
cae64f11 AC |
1742 | -- The rule refers to by-reference types, but a check is needed |
1743 | -- for by-copy types as well. That check is subsumed by the rule | |
1744 | -- for subtype conversion on assignment, but we can generate the | |
1745 | -- required check now. | |
1746 | ||
dd4e47ab | 1747 | -- Note also that Subp may be either a subprogram entity for |
e93f4e12 AC |
1748 | -- direct calls, or a type entity for indirect calls, which must |
1749 | -- be handled separately because the name does not denote an | |
1750 | -- overloadable entity. | |
dd4e47ab | 1751 | |
5f6fb720 AC |
1752 | declare |
1753 | Aund : constant Entity_Id := Underlying_Type (E_Actual); | |
1754 | Atyp : Entity_Id; | |
1755 | ||
1756 | begin | |
1757 | if No (Aund) then | |
1758 | Atyp := E_Actual; | |
1759 | else | |
1760 | Atyp := Aund; | |
1761 | end if; | |
1762 | ||
1763 | if Has_Predicates (Atyp) | |
1764 | and then Present (Predicate_Function (Atyp)) | |
1765 | ||
1766 | -- Skip predicate checks for special cases | |
1767 | ||
b8e6830b | 1768 | and then Predicate_Tests_On_Arguments (Subp) |
5f6fb720 AC |
1769 | then |
1770 | Append_To (Post_Call, | |
1771 | Make_Predicate_Check (Atyp, Actual)); | |
1772 | end if; | |
1773 | end; | |
f6820c2d | 1774 | |
fbf5a39b | 1775 | -- Processing for IN parameters |
70482933 RK |
1776 | |
1777 | else | |
fbf5a39b AC |
1778 | -- For IN parameters is in the packed array case, we expand an |
1779 | -- indexed component (the circuit in Exp_Ch4 deliberately left | |
1780 | -- indexed components appearing as actuals untouched, so that | |
1781 | -- the special processing above for the OUT and IN OUT cases | |
1782 | -- could be performed. We could make the test in Exp_Ch4 more | |
1783 | -- complex and have it detect the parameter mode, but it is | |
f44fe430 | 1784 | -- easier simply to handle all cases here.) |
fbf5a39b | 1785 | |
70482933 RK |
1786 | if Nkind (Actual) = N_Indexed_Component |
1787 | and then Is_Packed (Etype (Prefix (Actual))) | |
1788 | then | |
1789 | Reset_Packed_Prefix; | |
1790 | Expand_Packed_Element_Reference (Actual); | |
1791 | ||
0386aad1 AC |
1792 | -- If we have a reference to a bit packed array, we copy it, since |
1793 | -- the actual must be byte aligned. | |
70482933 | 1794 | |
fbf5a39b | 1795 | -- Is this really necessary in all cases??? |
70482933 | 1796 | |
fbf5a39b | 1797 | elsif Is_Ref_To_Bit_Packed_Array (Actual) then |
f44fe430 RD |
1798 | Add_Simple_Call_By_Copy_Code; |
1799 | ||
1800 | -- If a non-scalar actual is possibly unaligned, we need a copy | |
1801 | ||
1802 | elsif Is_Possibly_Unaligned_Object (Actual) | |
1803 | and then not Represented_As_Scalar (Etype (Formal)) | |
1804 | then | |
1805 | Add_Simple_Call_By_Copy_Code; | |
70482933 | 1806 | |
fbf5a39b AC |
1807 | -- Similarly, we have to expand slices of packed arrays here |
1808 | -- because the result must be byte aligned. | |
70482933 | 1809 | |
fbf5a39b AC |
1810 | elsif Is_Ref_To_Bit_Packed_Slice (Actual) then |
1811 | Add_Call_By_Copy_Code; | |
70482933 | 1812 | |
fbf5a39b AC |
1813 | -- Only processing remaining is to pass by copy if this is a |
1814 | -- reference to a possibly unaligned slice, since the caller | |
1815 | -- expects an appropriately aligned argument. | |
70482933 | 1816 | |
fbf5a39b AC |
1817 | elsif Is_Possibly_Unaligned_Slice (Actual) then |
1818 | Add_Call_By_Copy_Code; | |
fb468a94 AC |
1819 | |
1820 | -- An unusual case: a current instance of an enclosing task can be | |
1821 | -- an actual, and must be replaced by a reference to self. | |
1822 | ||
1823 | elsif Is_Entity_Name (Actual) | |
1824 | and then Is_Task_Type (Entity (Actual)) | |
1825 | then | |
1826 | if In_Open_Scopes (Entity (Actual)) then | |
1827 | Rewrite (Actual, | |
1828 | (Make_Function_Call (Loc, | |
da574a86 | 1829 | Name => New_Occurrence_Of (RTE (RE_Self), Loc)))); |
fb468a94 AC |
1830 | Analyze (Actual); |
1831 | ||
1832 | -- A task type cannot otherwise appear as an actual | |
1833 | ||
1834 | else | |
1835 | raise Program_Error; | |
1836 | end if; | |
70482933 RK |
1837 | end if; |
1838 | end if; | |
1839 | ||
1840 | Next_Formal (Formal); | |
1841 | Next_Actual (Actual); | |
1842 | end loop; | |
1843 | ||
1844 | -- Find right place to put post call stuff if it is present | |
1845 | ||
1846 | if not Is_Empty_List (Post_Call) then | |
1847 | ||
bdf69d33 | 1848 | -- Cases where the call is not a member of a statement list |
70482933 RK |
1849 | |
1850 | if not Is_List_Member (N) then | |
70482933 | 1851 | |
da574a86 AC |
1852 | -- In Ada 2012 the call may be a function call in an expression |
1853 | -- (since OUT and IN OUT parameters are now allowed for such | |
1854 | -- calls). The write-back of (in)-out parameters is handled | |
1855 | -- by the back-end, but the constraint checks generated when | |
1856 | -- subtypes of formal and actual don't match must be inserted | |
1857 | -- in the form of assignments. | |
bdf69d33 | 1858 | |
da574a86 AC |
1859 | if Ada_Version >= Ada_2012 |
1860 | and then Nkind (N) = N_Function_Call | |
1861 | then | |
1862 | -- We used to just do handle this by climbing up parents to | |
1863 | -- a non-statement/declaration and then simply making a call | |
1864 | -- to Insert_Actions_After (P, Post_Call), but that doesn't | |
1865 | -- work. If we are in the middle of an expression, e.g. the | |
1866 | -- condition of an IF, this call would insert after the IF | |
1867 | -- statement, which is much too late to be doing the write | |
1868 | -- back. For example: | |
1869 | ||
1870 | -- if Clobber (X) then | |
1871 | -- Put_Line (X'Img); | |
1872 | -- else | |
1873 | -- goto Junk | |
1874 | -- end if; | |
1875 | ||
1876 | -- Now assume Clobber changes X, if we put the write back | |
1877 | -- after the IF, the Put_Line gets the wrong value and the | |
1878 | -- goto causes the write back to be skipped completely. | |
1879 | ||
1880 | -- To deal with this, we replace the call by | |
1881 | ||
1882 | -- do | |
1883 | -- Tnnn : function-result-type renames function-call; | |
1884 | -- Post_Call actions | |
1885 | -- in | |
1886 | -- Tnnn; | |
1887 | -- end; | |
1888 | ||
1889 | -- Note: this won't do in Modify_Tree_For_C mode, but we | |
1890 | -- will deal with that later (it will require creating a | |
1891 | -- declaration for Temp, using Insert_Declaration) ??? | |
1892 | ||
1893 | declare | |
1894 | Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T'); | |
1895 | FRTyp : constant Entity_Id := Etype (N); | |
1896 | Name : constant Node_Id := Relocate_Node (N); | |
1897 | ||
1898 | begin | |
1899 | Prepend_To (Post_Call, | |
1900 | Make_Object_Renaming_Declaration (Loc, | |
1901 | Defining_Identifier => Tnnn, | |
1902 | Subtype_Mark => New_Occurrence_Of (FRTyp, Loc), | |
1903 | Name => Name)); | |
1904 | ||
1905 | Rewrite (N, | |
1906 | Make_Expression_With_Actions (Loc, | |
1907 | Actions => Post_Call, | |
1908 | Expression => New_Occurrence_Of (Tnnn, Loc))); | |
1909 | ||
1910 | -- We don't want to just blindly call Analyze_And_Resolve | |
1911 | -- because that would cause unwanted recursion on the call. | |
1912 | -- So for a moment set the call as analyzed to prevent that | |
1913 | -- recursion, and get the rest analyzed properly, then reset | |
1914 | -- the analyzed flag, so our caller can continue. | |
1915 | ||
1916 | Set_Analyzed (Name, True); | |
1917 | Analyze_And_Resolve (N, FRTyp); | |
1918 | Set_Analyzed (Name, False); | |
1919 | ||
1920 | -- Reset calling argument to point to function call inside | |
1921 | -- the expression with actions so the caller can continue | |
1922 | -- to process the call. | |
1923 | ||
1924 | N := Name; | |
1925 | end; | |
1926 | ||
1927 | -- If not the special Ada 2012 case of a function call, then | |
1928 | -- we must have the triggering statement of a triggering | |
1929 | -- alternative or an entry call alternative, and we can add | |
1930 | -- the post call stuff to the corresponding statement list. | |
bdf69d33 | 1931 | |
da574a86 AC |
1932 | else |
1933 | declare | |
1934 | P : Node_Id; | |
70482933 | 1935 | |
da574a86 AC |
1936 | begin |
1937 | P := Parent (N); | |
bdf69d33 AC |
1938 | pragma Assert (Nkind_In (P, N_Triggering_Alternative, |
1939 | N_Entry_Call_Alternative)); | |
1940 | ||
1941 | if Is_Non_Empty_List (Statements (P)) then | |
1942 | Insert_List_Before_And_Analyze | |
1943 | (First (Statements (P)), Post_Call); | |
1944 | else | |
1945 | Set_Statements (P, Post_Call); | |
1946 | end if; | |
bdf69d33 | 1947 | |
da574a86 AC |
1948 | return; |
1949 | end; | |
1950 | end if; | |
70482933 RK |
1951 | |
1952 | -- Otherwise, normal case where N is in a statement sequence, | |
1953 | -- just put the post-call stuff after the call statement. | |
1954 | ||
1955 | else | |
1956 | Insert_Actions_After (N, Post_Call); | |
da574a86 | 1957 | return; |
70482933 RK |
1958 | end if; |
1959 | end if; | |
1960 | ||
98f01d53 | 1961 | -- The call node itself is re-analyzed in Expand_Call |
70482933 RK |
1962 | |
1963 | end Expand_Actuals; | |
1964 | ||
1965 | ----------------- | |
1966 | -- Expand_Call -- | |
1967 | ----------------- | |
1968 | ||
1969 | -- This procedure handles expansion of function calls and procedure call | |
1970 | -- statements (i.e. it serves as the body for Expand_N_Function_Call and | |
70f91180 | 1971 | -- Expand_N_Procedure_Call_Statement). Processing for calls includes: |
70482933 | 1972 | |
70f91180 | 1973 | -- Replace call to Raise_Exception by Raise_Exception_Always if possible |
70482933 RK |
1974 | -- Provide values of actuals for all formals in Extra_Formals list |
1975 | -- Replace "call" to enumeration literal function by literal itself | |
1976 | -- Rewrite call to predefined operator as operator | |
1977 | -- Replace actuals to in-out parameters that are numeric conversions, | |
1978 | -- with explicit assignment to temporaries before and after the call. | |
70482933 RK |
1979 | |
1980 | -- Note that the list of actuals has been filled with default expressions | |
1981 | -- during semantic analysis of the call. Only the extra actuals required | |
1982 | -- for the 'Constrained attribute and for accessibility checks are added | |
1983 | -- at this point. | |
1984 | ||
1985 | procedure Expand_Call (N : Node_Id) is | |
1986 | Loc : constant Source_Ptr := Sloc (N); | |
6dfc5592 | 1987 | Call_Node : Node_Id := N; |
70482933 | 1988 | Extra_Actuals : List_Id := No_List; |
fdce4bb7 | 1989 | Prev : Node_Id := Empty; |
758c442c | 1990 | |
70482933 RK |
1991 | procedure Add_Actual_Parameter (Insert_Param : Node_Id); |
1992 | -- Adds one entry to the end of the actual parameter list. Used for | |
2f1b20a9 ES |
1993 | -- default parameters and for extra actuals (for Extra_Formals). The |
1994 | -- argument is an N_Parameter_Association node. | |
70482933 RK |
1995 | |
1996 | procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id); | |
2f1b20a9 ES |
1997 | -- Adds an extra actual to the list of extra actuals. Expr is the |
1998 | -- expression for the value of the actual, EF is the entity for the | |
1999 | -- extra formal. | |
70482933 | 2000 | |
84f4072a JM |
2001 | procedure Do_Inline (Subp : Entity_Id; Orig_Subp : Entity_Id); |
2002 | -- Check and inline the body of Subp. Invoked when compiling with | |
2003 | -- optimizations enabled and Subp has pragma inline or inline always. | |
2004 | -- If the subprogram is a renaming, or if it is inherited, then Subp | |
2005 | -- references the renamed entity and Orig_Subp is the entity of the | |
2006 | -- call node N. | |
2007 | ||
2008 | procedure Do_Inline_Always (Subp : Entity_Id; Orig_Subp : Entity_Id); | |
2009 | -- Check and inline the body of Subp. Invoked when compiling without | |
2010 | -- optimizations and Subp has pragma inline always. If the subprogram is | |
2011 | -- a renaming, or if it is inherited, then Subp references the renamed | |
2012 | -- entity and Orig_Subp is the entity of the call node N. | |
2013 | ||
70482933 | 2014 | function Inherited_From_Formal (S : Entity_Id) return Entity_Id; |
1fb63e89 | 2015 | -- Within an instance, a type derived from an untagged formal derived |
70f91180 RD |
2016 | -- type inherits from the original parent, not from the actual. The |
2017 | -- current derivation mechanism has the derived type inherit from the | |
2018 | -- actual, which is only correct outside of the instance. If the | |
2019 | -- subprogram is inherited, we test for this particular case through a | |
2020 | -- convoluted tree traversal before setting the proper subprogram to be | |
2021 | -- called. | |
70482933 | 2022 | |
84f4072a JM |
2023 | function In_Unfrozen_Instance (E : Entity_Id) return Boolean; |
2024 | -- Return true if E comes from an instance that is not yet frozen | |
2025 | ||
df3e68b1 | 2026 | function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean; |
2c1b72d7 | 2027 | -- Determine if Subp denotes a non-dispatching call to a Deep routine |
df3e68b1 | 2028 | |
dd386db0 AC |
2029 | function New_Value (From : Node_Id) return Node_Id; |
2030 | -- From is the original Expression. New_Value is equivalent to a call | |
2031 | -- to Duplicate_Subexpr with an explicit dereference when From is an | |
2032 | -- access parameter. | |
2033 | ||
70482933 RK |
2034 | -------------------------- |
2035 | -- Add_Actual_Parameter -- | |
2036 | -------------------------- | |
2037 | ||
2038 | procedure Add_Actual_Parameter (Insert_Param : Node_Id) is | |
2039 | Actual_Expr : constant Node_Id := | |
2040 | Explicit_Actual_Parameter (Insert_Param); | |
2041 | ||
2042 | begin | |
2043 | -- Case of insertion is first named actual | |
2044 | ||
2045 | if No (Prev) or else | |
2046 | Nkind (Parent (Prev)) /= N_Parameter_Association | |
2047 | then | |
6dfc5592 RD |
2048 | Set_Next_Named_Actual |
2049 | (Insert_Param, First_Named_Actual (Call_Node)); | |
2050 | Set_First_Named_Actual (Call_Node, Actual_Expr); | |
70482933 RK |
2051 | |
2052 | if No (Prev) then | |
6dfc5592 RD |
2053 | if No (Parameter_Associations (Call_Node)) then |
2054 | Set_Parameter_Associations (Call_Node, New_List); | |
70482933 | 2055 | end if; |
57a3fca9 AC |
2056 | |
2057 | Append (Insert_Param, Parameter_Associations (Call_Node)); | |
2058 | ||
70482933 RK |
2059 | else |
2060 | Insert_After (Prev, Insert_Param); | |
2061 | end if; | |
2062 | ||
2063 | -- Case of insertion is not first named actual | |
2064 | ||
2065 | else | |
2066 | Set_Next_Named_Actual | |
2067 | (Insert_Param, Next_Named_Actual (Parent (Prev))); | |
2068 | Set_Next_Named_Actual (Parent (Prev), Actual_Expr); | |
6dfc5592 | 2069 | Append (Insert_Param, Parameter_Associations (Call_Node)); |
70482933 RK |
2070 | end if; |
2071 | ||
2072 | Prev := Actual_Expr; | |
2073 | end Add_Actual_Parameter; | |
2074 | ||
2075 | ---------------------- | |
2076 | -- Add_Extra_Actual -- | |
2077 | ---------------------- | |
2078 | ||
2079 | procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is | |
2080 | Loc : constant Source_Ptr := Sloc (Expr); | |
2081 | ||
2082 | begin | |
2083 | if Extra_Actuals = No_List then | |
2084 | Extra_Actuals := New_List; | |
6dfc5592 | 2085 | Set_Parent (Extra_Actuals, Call_Node); |
70482933 RK |
2086 | end if; |
2087 | ||
2088 | Append_To (Extra_Actuals, | |
2089 | Make_Parameter_Association (Loc, | |
7a2c2277 | 2090 | Selector_Name => New_Occurrence_Of (EF, Loc), |
9d983bbf | 2091 | Explicit_Actual_Parameter => Expr)); |
70482933 RK |
2092 | |
2093 | Analyze_And_Resolve (Expr, Etype (EF)); | |
75a64833 | 2094 | |
6dfc5592 | 2095 | if Nkind (Call_Node) = N_Function_Call then |
75a64833 AC |
2096 | Set_Is_Accessibility_Actual (Parent (Expr)); |
2097 | end if; | |
70482933 RK |
2098 | end Add_Extra_Actual; |
2099 | ||
84f4072a JM |
2100 | ---------------- |
2101 | -- Do_Inline -- | |
2102 | ---------------- | |
2103 | ||
2104 | procedure Do_Inline (Subp : Entity_Id; Orig_Subp : Entity_Id) is | |
2105 | Spec : constant Node_Id := Unit_Declaration_Node (Subp); | |
2106 | ||
2107 | procedure Do_Backend_Inline; | |
2108 | -- Check that the call can be safely passed to the backend. If true | |
2109 | -- then register the enclosing unit of Subp to Inlined_Bodies so that | |
2110 | -- the body of Subp can be retrieved and analyzed by the backend. | |
2111 | ||
84f4072a JM |
2112 | ----------------------- |
2113 | -- Do_Backend_Inline -- | |
2114 | ----------------------- | |
2115 | ||
2116 | procedure Do_Backend_Inline is | |
2117 | begin | |
2118 | -- No extra test needed for init subprograms since we know they | |
a90bd866 | 2119 | -- are available to the backend. |
84f4072a JM |
2120 | |
2121 | if Is_Init_Proc (Subp) then | |
2122 | Add_Inlined_Body (Subp); | |
2123 | Register_Backend_Call (Call_Node); | |
2124 | ||
2125 | -- Verify that if the body to inline is located in the current | |
2126 | -- unit the inlining does not occur earlier. This avoids | |
2127 | -- order-of-elaboration problems in the back end. | |
2128 | ||
2129 | elsif In_Same_Extended_Unit (Call_Node, Subp) | |
2130 | and then Nkind (Spec) = N_Subprogram_Declaration | |
2131 | and then Earlier_In_Extended_Unit | |
2132 | (Loc, Sloc (Body_To_Inline (Spec))) | |
2133 | then | |
2134 | Error_Msg_NE | |
685bc70f | 2135 | ("cannot inline& (body not seen yet)??", Call_Node, Subp); |
84f4072a JM |
2136 | |
2137 | else | |
2138 | declare | |
2139 | Backend_Inline : Boolean := True; | |
2140 | ||
2141 | begin | |
2142 | -- If we are compiling a package body that is not the | |
2143 | -- main unit, it must be for inlining/instantiation | |
2144 | -- purposes, in which case we inline the call to insure | |
2145 | -- that the same temporaries are generated when compiling | |
2146 | -- the body by itself. Otherwise link errors can occur. | |
2147 | ||
2148 | -- If the function being called is itself in the main | |
2149 | -- unit, we cannot inline, because there is a risk of | |
2150 | -- double elaboration and/or circularity: the inlining | |
2151 | -- can make visible a private entity in the body of the | |
2152 | -- main unit, that gigi will see before its sees its | |
2153 | -- proper definition. | |
2154 | ||
2155 | if not (In_Extended_Main_Code_Unit (Call_Node)) | |
2156 | and then In_Package_Body | |
2157 | then | |
2158 | Backend_Inline := | |
2159 | not In_Extended_Main_Source_Unit (Subp); | |
2160 | end if; | |
2161 | ||
2162 | if Backend_Inline then | |
2163 | Add_Inlined_Body (Subp); | |
2164 | Register_Backend_Call (Call_Node); | |
2165 | end if; | |
2166 | end; | |
2167 | end if; | |
2168 | end Do_Backend_Inline; | |
2169 | ||
84f4072a JM |
2170 | -- Start of processing for Do_Inline |
2171 | ||
2172 | begin | |
2173 | -- Verify that the body to inline has already been seen | |
2174 | ||
2175 | if No (Spec) | |
2176 | or else Nkind (Spec) /= N_Subprogram_Declaration | |
2177 | or else No (Body_To_Inline (Spec)) | |
2178 | then | |
2179 | if Comes_From_Source (Subp) | |
2180 | and then Must_Inline (Subp) | |
2181 | then | |
2182 | Cannot_Inline | |
2183 | ("cannot inline& (body not seen yet)?", Call_Node, Subp); | |
2184 | ||
2185 | -- Let the back end handle it | |
2186 | ||
2187 | else | |
2188 | Do_Backend_Inline; | |
2189 | return; | |
2190 | end if; | |
2191 | ||
2192 | -- If this an inherited function that returns a private type, do not | |
2193 | -- inline if the full view is an unconstrained array, because such | |
2194 | -- calls cannot be inlined. | |
2195 | ||
2196 | elsif Present (Orig_Subp) | |
2197 | and then Is_Array_Type (Etype (Orig_Subp)) | |
2198 | and then not Is_Constrained (Etype (Orig_Subp)) | |
2199 | then | |
2200 | Cannot_Inline | |
2201 | ("cannot inline& (unconstrained array)?", Call_Node, Subp); | |
2202 | ||
2203 | else | |
2204 | Expand_Inlined_Call (Call_Node, Subp, Orig_Subp); | |
2205 | end if; | |
2206 | end Do_Inline; | |
2207 | ||
2208 | ---------------------- | |
2209 | -- Do_Inline_Always -- | |
2210 | ---------------------- | |
2211 | ||
2212 | procedure Do_Inline_Always (Subp : Entity_Id; Orig_Subp : Entity_Id) is | |
2213 | Spec : constant Node_Id := Unit_Declaration_Node (Subp); | |
2214 | Body_Id : Entity_Id; | |
2215 | ||
2216 | begin | |
2217 | if No (Spec) | |
2218 | or else Nkind (Spec) /= N_Subprogram_Declaration | |
2219 | or else No (Body_To_Inline (Spec)) | |
2220 | or else Serious_Errors_Detected /= 0 | |
2221 | then | |
2222 | return; | |
2223 | end if; | |
2224 | ||
2225 | Body_Id := Corresponding_Body (Spec); | |
2226 | ||
2227 | -- Verify that the body to inline has already been seen | |
2228 | ||
2229 | if No (Body_Id) | |
2230 | or else not Analyzed (Body_Id) | |
2231 | then | |
2232 | Set_Is_Inlined (Subp, False); | |
2233 | ||
2234 | if Comes_From_Source (Subp) then | |
2235 | ||
2236 | -- Report a warning only if the call is located in the unit of | |
2237 | -- the called subprogram; otherwise it is an error. | |
2238 | ||
2239 | if not In_Same_Extended_Unit (Call_Node, Subp) then | |
2240 | Cannot_Inline | |
685bc70f | 2241 | ("cannot inline& (body not seen yet)?", Call_Node, Subp, |
84f4072a JM |
2242 | Is_Serious => True); |
2243 | ||
2244 | elsif In_Open_Scopes (Subp) then | |
2245 | ||
2246 | -- For backward compatibility we generate the same error | |
2247 | -- or warning of the previous implementation. This will | |
2248 | -- be changed when we definitely incorporate the new | |
2249 | -- support ??? | |
2250 | ||
2251 | if Front_End_Inlining | |
2252 | and then Optimization_Level = 0 | |
2253 | then | |
2254 | Error_Msg_N | |
685bc70f | 2255 | ("call to recursive subprogram cannot be inlined?p?", |
84f4072a JM |
2256 | N); |
2257 | ||
2258 | -- Do not emit error compiling runtime packages | |
2259 | ||
2260 | elsif Is_Predefined_File_Name | |
2261 | (Unit_File_Name (Get_Source_Unit (Subp))) | |
2262 | then | |
2263 | Error_Msg_N | |
685bc70f | 2264 | ("call to recursive subprogram cannot be inlined??", |
84f4072a JM |
2265 | N); |
2266 | ||
2267 | else | |
2268 | Error_Msg_N | |
2269 | ("call to recursive subprogram cannot be inlined", | |
2270 | N); | |
2271 | end if; | |
2272 | ||
2273 | else | |
2274 | Cannot_Inline | |
2275 | ("cannot inline& (body not seen yet)?", Call_Node, Subp); | |
2276 | end if; | |
2277 | end if; | |
2278 | ||
2279 | return; | |
2280 | ||
2281 | -- If this an inherited function that returns a private type, do not | |
2282 | -- inline if the full view is an unconstrained array, because such | |
2283 | -- calls cannot be inlined. | |
2284 | ||
2285 | elsif Present (Orig_Subp) | |
2286 | and then Is_Array_Type (Etype (Orig_Subp)) | |
2287 | and then not Is_Constrained (Etype (Orig_Subp)) | |
2288 | then | |
2289 | Cannot_Inline | |
2290 | ("cannot inline& (unconstrained array)?", Call_Node, Subp); | |
2291 | ||
2292 | -- If the called subprogram comes from an instance in the same | |
2293 | -- unit, and the instance is not yet frozen, inlining might | |
2294 | -- trigger order-of-elaboration problems. | |
2295 | ||
2296 | elsif In_Unfrozen_Instance (Scope (Subp)) then | |
2297 | Cannot_Inline | |
2298 | ("cannot inline& (unfrozen instance)?", Call_Node, Subp); | |
2299 | ||
2300 | else | |
2301 | Expand_Inlined_Call (Call_Node, Subp, Orig_Subp); | |
2302 | end if; | |
2303 | end Do_Inline_Always; | |
2304 | ||
70482933 RK |
2305 | --------------------------- |
2306 | -- Inherited_From_Formal -- | |
2307 | --------------------------- | |
2308 | ||
2309 | function Inherited_From_Formal (S : Entity_Id) return Entity_Id is | |
2310 | Par : Entity_Id; | |
2311 | Gen_Par : Entity_Id; | |
2312 | Gen_Prim : Elist_Id; | |
2313 | Elmt : Elmt_Id; | |
2314 | Indic : Node_Id; | |
2315 | ||
2316 | begin | |
2317 | -- If the operation is inherited, it is attached to the corresponding | |
2318 | -- type derivation. If the parent in the derivation is a generic | |
2319 | -- actual, it is a subtype of the actual, and we have to recover the | |
2320 | -- original derived type declaration to find the proper parent. | |
2321 | ||
2322 | if Nkind (Parent (S)) /= N_Full_Type_Declaration | |
fbf5a39b | 2323 | or else not Is_Derived_Type (Defining_Identifier (Parent (S))) |
2f1b20a9 ES |
2324 | or else Nkind (Type_Definition (Original_Node (Parent (S)))) /= |
2325 | N_Derived_Type_Definition | |
fbf5a39b | 2326 | or else not In_Instance |
70482933 RK |
2327 | then |
2328 | return Empty; | |
2329 | ||
2330 | else | |
2331 | Indic := | |
e27b834b AC |
2332 | Subtype_Indication |
2333 | (Type_Definition (Original_Node (Parent (S)))); | |
70482933 RK |
2334 | |
2335 | if Nkind (Indic) = N_Subtype_Indication then | |
2336 | Par := Entity (Subtype_Mark (Indic)); | |
2337 | else | |
2338 | Par := Entity (Indic); | |
2339 | end if; | |
2340 | end if; | |
2341 | ||
2342 | if not Is_Generic_Actual_Type (Par) | |
2343 | or else Is_Tagged_Type (Par) | |
2344 | or else Nkind (Parent (Par)) /= N_Subtype_Declaration | |
2345 | or else not In_Open_Scopes (Scope (Par)) | |
70482933 RK |
2346 | then |
2347 | return Empty; | |
70482933 RK |
2348 | else |
2349 | Gen_Par := Generic_Parent_Type (Parent (Par)); | |
2350 | end if; | |
2351 | ||
7888a6ae GD |
2352 | -- If the actual has no generic parent type, the formal is not |
2353 | -- a formal derived type, so nothing to inherit. | |
2354 | ||
2355 | if No (Gen_Par) then | |
2356 | return Empty; | |
2357 | end if; | |
2358 | ||
2f1b20a9 ES |
2359 | -- If the generic parent type is still the generic type, this is a |
2360 | -- private formal, not a derived formal, and there are no operations | |
2361 | -- inherited from the formal. | |
fbf5a39b AC |
2362 | |
2363 | if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then | |
2364 | return Empty; | |
2365 | end if; | |
2366 | ||
70482933 | 2367 | Gen_Prim := Collect_Primitive_Operations (Gen_Par); |
70482933 | 2368 | |
2f1b20a9 | 2369 | Elmt := First_Elmt (Gen_Prim); |
70482933 RK |
2370 | while Present (Elmt) loop |
2371 | if Chars (Node (Elmt)) = Chars (S) then | |
2372 | declare | |
2373 | F1 : Entity_Id; | |
2374 | F2 : Entity_Id; | |
70482933 | 2375 | |
2f1b20a9 | 2376 | begin |
70482933 RK |
2377 | F1 := First_Formal (S); |
2378 | F2 := First_Formal (Node (Elmt)); | |
70482933 RK |
2379 | while Present (F1) |
2380 | and then Present (F2) | |
2381 | loop | |
70482933 RK |
2382 | if Etype (F1) = Etype (F2) |
2383 | or else Etype (F2) = Gen_Par | |
2384 | then | |
2385 | Next_Formal (F1); | |
2386 | Next_Formal (F2); | |
2387 | else | |
2388 | Next_Elmt (Elmt); | |
2389 | exit; -- not the right subprogram | |
2390 | end if; | |
2391 | ||
2392 | return Node (Elmt); | |
2393 | end loop; | |
2394 | end; | |
2395 | ||
2396 | else | |
2397 | Next_Elmt (Elmt); | |
2398 | end if; | |
2399 | end loop; | |
2400 | ||
2401 | raise Program_Error; | |
2402 | end Inherited_From_Formal; | |
2403 | ||
84f4072a JM |
2404 | -------------------------- |
2405 | -- In_Unfrozen_Instance -- | |
2406 | -------------------------- | |
2407 | ||
2408 | function In_Unfrozen_Instance (E : Entity_Id) return Boolean is | |
bde73c6b | 2409 | S : Entity_Id; |
84f4072a JM |
2410 | |
2411 | begin | |
bde73c6b AC |
2412 | S := E; |
2413 | while Present (S) and then S /= Standard_Standard loop | |
84f4072a JM |
2414 | if Is_Generic_Instance (S) |
2415 | and then Present (Freeze_Node (S)) | |
2416 | and then not Analyzed (Freeze_Node (S)) | |
2417 | then | |
2418 | return True; | |
2419 | end if; | |
2420 | ||
2421 | S := Scope (S); | |
2422 | end loop; | |
2423 | ||
2424 | return False; | |
2425 | end In_Unfrozen_Instance; | |
2426 | ||
df3e68b1 HK |
2427 | ------------------------- |
2428 | -- Is_Direct_Deep_Call -- | |
2429 | ------------------------- | |
2430 | ||
2431 | function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is | |
2432 | begin | |
2433 | if Is_TSS (Subp, TSS_Deep_Adjust) | |
2434 | or else Is_TSS (Subp, TSS_Deep_Finalize) | |
2435 | or else Is_TSS (Subp, TSS_Deep_Initialize) | |
2436 | then | |
2437 | declare | |
2438 | Actual : Node_Id; | |
2439 | Formal : Node_Id; | |
2440 | ||
2441 | begin | |
2442 | Actual := First (Parameter_Associations (N)); | |
2443 | Formal := First_Formal (Subp); | |
2444 | while Present (Actual) | |
2445 | and then Present (Formal) | |
2446 | loop | |
2447 | if Nkind (Actual) = N_Identifier | |
2448 | and then Is_Controlling_Actual (Actual) | |
2449 | and then Etype (Actual) = Etype (Formal) | |
2450 | then | |
2451 | return True; | |
2452 | end if; | |
2453 | ||
2454 | Next (Actual); | |
2455 | Next_Formal (Formal); | |
2456 | end loop; | |
2457 | end; | |
2458 | end if; | |
2459 | ||
2460 | return False; | |
2461 | end Is_Direct_Deep_Call; | |
2462 | ||
dd386db0 AC |
2463 | --------------- |
2464 | -- New_Value -- | |
2465 | --------------- | |
2466 | ||
2467 | function New_Value (From : Node_Id) return Node_Id is | |
2468 | Res : constant Node_Id := Duplicate_Subexpr (From); | |
2469 | begin | |
2470 | if Is_Access_Type (Etype (From)) then | |
bde73c6b | 2471 | return Make_Explicit_Dereference (Sloc (From), Prefix => Res); |
dd386db0 AC |
2472 | else |
2473 | return Res; | |
2474 | end if; | |
2475 | end New_Value; | |
2476 | ||
fdce4bb7 JM |
2477 | -- Local variables |
2478 | ||
deb8dacc HK |
2479 | Curr_S : constant Entity_Id := Current_Scope; |
2480 | Remote : constant Boolean := Is_Remote_Call (Call_Node); | |
fdce4bb7 JM |
2481 | Actual : Node_Id; |
2482 | Formal : Entity_Id; | |
2483 | Orig_Subp : Entity_Id := Empty; | |
2484 | Param_Count : Natural := 0; | |
2485 | Parent_Formal : Entity_Id; | |
2486 | Parent_Subp : Entity_Id; | |
2487 | Scop : Entity_Id; | |
2488 | Subp : Entity_Id; | |
2489 | ||
e27b834b | 2490 | Prev_Orig : Node_Id; |
fdce4bb7 JM |
2491 | -- Original node for an actual, which may have been rewritten. If the |
2492 | -- actual is a function call that has been transformed from a selected | |
2493 | -- component, the original node is unanalyzed. Otherwise, it carries | |
2494 | -- semantic information used to generate additional actuals. | |
2495 | ||
2496 | CW_Interface_Formals_Present : Boolean := False; | |
2497 | ||
70482933 RK |
2498 | -- Start of processing for Expand_Call |
2499 | ||
2500 | begin | |
dec6faf1 AC |
2501 | -- Expand the procedure call if the first actual has a dimension and if |
2502 | -- the procedure is Put (Ada 2012). | |
2503 | ||
2504 | if Ada_Version >= Ada_2012 | |
2505 | and then Nkind (Call_Node) = N_Procedure_Call_Statement | |
2506 | and then Present (Parameter_Associations (Call_Node)) | |
2507 | then | |
df378148 | 2508 | Expand_Put_Call_With_Symbol (Call_Node); |
dec6faf1 AC |
2509 | end if; |
2510 | ||
07fc65c4 GB |
2511 | -- Ignore if previous error |
2512 | ||
6dfc5592 RD |
2513 | if Nkind (Call_Node) in N_Has_Etype |
2514 | and then Etype (Call_Node) = Any_Type | |
2515 | then | |
07fc65c4 GB |
2516 | return; |
2517 | end if; | |
2518 | ||
70482933 RK |
2519 | -- Call using access to subprogram with explicit dereference |
2520 | ||
6dfc5592 RD |
2521 | if Nkind (Name (Call_Node)) = N_Explicit_Dereference then |
2522 | Subp := Etype (Name (Call_Node)); | |
70482933 RK |
2523 | Parent_Subp := Empty; |
2524 | ||
2525 | -- Case of call to simple entry, where the Name is a selected component | |
2526 | -- whose prefix is the task, and whose selector name is the entry name | |
2527 | ||
6dfc5592 RD |
2528 | elsif Nkind (Name (Call_Node)) = N_Selected_Component then |
2529 | Subp := Entity (Selector_Name (Name (Call_Node))); | |
70482933 RK |
2530 | Parent_Subp := Empty; |
2531 | ||
2532 | -- Case of call to member of entry family, where Name is an indexed | |
2533 | -- component, with the prefix being a selected component giving the | |
2534 | -- task and entry family name, and the index being the entry index. | |
2535 | ||
6dfc5592 RD |
2536 | elsif Nkind (Name (Call_Node)) = N_Indexed_Component then |
2537 | Subp := Entity (Selector_Name (Prefix (Name (Call_Node)))); | |
70482933 RK |
2538 | Parent_Subp := Empty; |
2539 | ||
2540 | -- Normal case | |
2541 | ||
2542 | else | |
6dfc5592 | 2543 | Subp := Entity (Name (Call_Node)); |
70482933 RK |
2544 | Parent_Subp := Alias (Subp); |
2545 | ||
2546 | -- Replace call to Raise_Exception by call to Raise_Exception_Always | |
2547 | -- if we can tell that the first parameter cannot possibly be null. | |
70f91180 | 2548 | -- This improves efficiency by avoiding a run-time test. |
70482933 | 2549 | |
7888a6ae GD |
2550 | -- We do not do this if Raise_Exception_Always does not exist, which |
2551 | -- can happen in configurable run time profiles which provide only a | |
70f91180 | 2552 | -- Raise_Exception. |
7888a6ae GD |
2553 | |
2554 | if Is_RTE (Subp, RE_Raise_Exception) | |
2555 | and then RTE_Available (RE_Raise_Exception_Always) | |
70482933 RK |
2556 | then |
2557 | declare | |
3cae7f14 RD |
2558 | FA : constant Node_Id := |
2559 | Original_Node (First_Actual (Call_Node)); | |
2560 | ||
70482933 RK |
2561 | begin |
2562 | -- The case we catch is where the first argument is obtained | |
2f1b20a9 ES |
2563 | -- using the Identity attribute (which must always be |
2564 | -- non-null). | |
70482933 RK |
2565 | |
2566 | if Nkind (FA) = N_Attribute_Reference | |
2567 | and then Attribute_Name (FA) = Name_Identity | |
2568 | then | |
2569 | Subp := RTE (RE_Raise_Exception_Always); | |
6dfc5592 | 2570 | Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc)); |
70482933 RK |
2571 | end if; |
2572 | end; | |
2573 | end if; | |
2574 | ||
2575 | if Ekind (Subp) = E_Entry then | |
2576 | Parent_Subp := Empty; | |
2577 | end if; | |
2578 | end if; | |
2579 | ||
d3f70b35 AC |
2580 | -- Detect the following code in System.Finalization_Masters only on |
2581 | -- .NET/JVM targets: | |
e80f0cb0 | 2582 | |
d3f70b35 | 2583 | -- procedure Finalize (Master : in out Finalization_Master) is |
deb8dacc HK |
2584 | -- begin |
2585 | -- . . . | |
2586 | -- begin | |
2587 | -- Finalize (Curr_Ptr.all); | |
e80f0cb0 | 2588 | |
deb8dacc | 2589 | -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize |
e80f0cb0 RD |
2590 | -- cannot be named in library or user code, the compiler has to deal |
2591 | -- with this by transforming the call to Finalize into Deep_Finalize. | |
deb8dacc HK |
2592 | |
2593 | if VM_Target /= No_VM | |
2594 | and then Chars (Subp) = Name_Finalize | |
2595 | and then Ekind (Curr_S) = E_Block | |
2596 | and then Ekind (Scope (Curr_S)) = E_Procedure | |
2597 | and then Chars (Scope (Curr_S)) = Name_Finalize | |
2598 | and then Etype (First_Formal (Scope (Curr_S))) = | |
d3f70b35 | 2599 | RTE (RE_Finalization_Master) |
deb8dacc HK |
2600 | then |
2601 | declare | |
2602 | Deep_Fin : constant Entity_Id := | |
2603 | Find_Prim_Op (RTE (RE_Root_Controlled), | |
2604 | TSS_Deep_Finalize); | |
2605 | begin | |
2606 | -- Since Root_Controlled is a tagged type, the compiler should | |
2607 | -- always generate Deep_Finalize for it. | |
2608 | ||
2609 | pragma Assert (Present (Deep_Fin)); | |
2610 | ||
2611 | -- Generate: | |
2612 | -- Deep_Finalize (Curr_Ptr.all); | |
2613 | ||
2614 | Rewrite (N, | |
2615 | Make_Procedure_Call_Statement (Loc, | |
2616 | Name => | |
e4494292 | 2617 | New_Occurrence_Of (Deep_Fin, Loc), |
deb8dacc HK |
2618 | Parameter_Associations => |
2619 | New_Copy_List_Tree (Parameter_Associations (N)))); | |
2620 | ||
2621 | Analyze (N); | |
2622 | return; | |
2623 | end; | |
2624 | end if; | |
2625 | ||
f4d379b8 HK |
2626 | -- Ada 2005 (AI-345): We have a procedure call as a triggering |
2627 | -- alternative in an asynchronous select or as an entry call in | |
2628 | -- a conditional or timed select. Check whether the procedure call | |
2629 | -- is a renaming of an entry and rewrite it as an entry call. | |
2630 | ||
0791fbe9 | 2631 | if Ada_Version >= Ada_2005 |
6dfc5592 | 2632 | and then Nkind (Call_Node) = N_Procedure_Call_Statement |
f4d379b8 | 2633 | and then |
6dfc5592 | 2634 | ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative |
3cae7f14 | 2635 | and then Triggering_Statement (Parent (Call_Node)) = Call_Node) |
f4d379b8 | 2636 | or else |
6dfc5592 | 2637 | (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative |
3cae7f14 | 2638 | and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node)) |
f4d379b8 HK |
2639 | then |
2640 | declare | |
2641 | Ren_Decl : Node_Id; | |
2642 | Ren_Root : Entity_Id := Subp; | |
2643 | ||
2644 | begin | |
2645 | -- This may be a chain of renamings, find the root | |
2646 | ||
2647 | if Present (Alias (Ren_Root)) then | |
2648 | Ren_Root := Alias (Ren_Root); | |
2649 | end if; | |
2650 | ||
2651 | if Present (Original_Node (Parent (Parent (Ren_Root)))) then | |
2652 | Ren_Decl := Original_Node (Parent (Parent (Ren_Root))); | |
2653 | ||
2654 | if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then | |
6dfc5592 | 2655 | Rewrite (Call_Node, |
f4d379b8 HK |
2656 | Make_Entry_Call_Statement (Loc, |
2657 | Name => | |
2658 | New_Copy_Tree (Name (Ren_Decl)), | |
2659 | Parameter_Associations => | |
6dfc5592 RD |
2660 | New_Copy_List_Tree |
2661 | (Parameter_Associations (Call_Node)))); | |
f4d379b8 HK |
2662 | |
2663 | return; | |
2664 | end if; | |
2665 | end if; | |
2666 | end; | |
2667 | end if; | |
2668 | ||
e27b834b AC |
2669 | -- First step, compute extra actuals, corresponding to any Extra_Formals |
2670 | -- present. Note that we do not access Extra_Formals directly, instead | |
2671 | -- we simply note the presence of the extra formals as we process the | |
2672 | -- regular formals collecting corresponding actuals in Extra_Actuals. | |
70482933 | 2673 | |
c2369146 AC |
2674 | -- We also generate any required range checks for actuals for in formals |
2675 | -- as we go through the loop, since this is a convenient place to do it. | |
2676 | -- (Though it seems that this would be better done in Expand_Actuals???) | |
fbf5a39b | 2677 | |
e2441021 AC |
2678 | -- Special case: Thunks must not compute the extra actuals; they must |
2679 | -- just propagate to the target primitive their extra actuals. | |
2680 | ||
2681 | if Is_Thunk (Current_Scope) | |
2682 | and then Thunk_Entity (Current_Scope) = Subp | |
2683 | and then Present (Extra_Formals (Subp)) | |
2684 | then | |
2685 | pragma Assert (Present (Extra_Formals (Current_Scope))); | |
2686 | ||
2687 | declare | |
2688 | Target_Formal : Entity_Id; | |
2689 | Thunk_Formal : Entity_Id; | |
2690 | ||
2691 | begin | |
2692 | Target_Formal := Extra_Formals (Subp); | |
2693 | Thunk_Formal := Extra_Formals (Current_Scope); | |
2694 | while Present (Target_Formal) loop | |
2695 | Add_Extra_Actual | |
2696 | (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal); | |
2697 | ||
2698 | Target_Formal := Extra_Formal (Target_Formal); | |
2699 | Thunk_Formal := Extra_Formal (Thunk_Formal); | |
2700 | end loop; | |
2701 | ||
2702 | while Is_Non_Empty_List (Extra_Actuals) loop | |
2703 | Add_Actual_Parameter (Remove_Head (Extra_Actuals)); | |
2704 | end loop; | |
2705 | ||
2706 | Expand_Actuals (Call_Node, Subp); | |
2707 | return; | |
2708 | end; | |
2709 | end if; | |
2710 | ||
8c5b03a0 AC |
2711 | Formal := First_Formal (Subp); |
2712 | Actual := First_Actual (Call_Node); | |
fdce4bb7 | 2713 | Param_Count := 1; |
70482933 | 2714 | while Present (Formal) loop |
fbf5a39b | 2715 | |
d79e621a | 2716 | -- Generate range check if required |
fbf5a39b | 2717 | |
d79e621a | 2718 | if Do_Range_Check (Actual) |
c2369146 | 2719 | and then Ekind (Formal) = E_In_Parameter |
d79e621a | 2720 | then |
d79e621a GD |
2721 | Generate_Range_Check |
2722 | (Actual, Etype (Formal), CE_Range_Check_Failed); | |
2723 | end if; | |
fbf5a39b AC |
2724 | |
2725 | -- Prepare to examine current entry | |
2726 | ||
70482933 RK |
2727 | Prev := Actual; |
2728 | Prev_Orig := Original_Node (Prev); | |
2729 | ||
758c442c | 2730 | -- Ada 2005 (AI-251): Check if any formal is a class-wide interface |
2f1b20a9 | 2731 | -- to expand it in a further round. |
758c442c GD |
2732 | |
2733 | CW_Interface_Formals_Present := | |
2734 | CW_Interface_Formals_Present | |
2735 | or else | |
2736 | (Ekind (Etype (Formal)) = E_Class_Wide_Type | |
8c5b03a0 | 2737 | and then Is_Interface (Etype (Etype (Formal)))) |
758c442c GD |
2738 | or else |
2739 | (Ekind (Etype (Formal)) = E_Anonymous_Access_Type | |
2740 | and then Is_Interface (Directly_Designated_Type | |
2741 | (Etype (Etype (Formal))))); | |
2742 | ||
2743 | -- Create possible extra actual for constrained case. Usually, the | |
2744 | -- extra actual is of the form actual'constrained, but since this | |
2745 | -- attribute is only available for unconstrained records, TRUE is | |
2746 | -- expanded if the type of the formal happens to be constrained (for | |
2747 | -- instance when this procedure is inherited from an unconstrained | |
2748 | -- record to a constrained one) or if the actual has no discriminant | |
2749 | -- (its type is constrained). An exception to this is the case of a | |
2750 | -- private type without discriminants. In this case we pass FALSE | |
2751 | -- because the object has underlying discriminants with defaults. | |
70482933 RK |
2752 | |
2753 | if Present (Extra_Constrained (Formal)) then | |
2754 | if Ekind (Etype (Prev)) in Private_Kind | |
2755 | and then not Has_Discriminants (Base_Type (Etype (Prev))) | |
2756 | then | |
01aef5ad GD |
2757 | Add_Extra_Actual |
2758 | (New_Occurrence_Of (Standard_False, Loc), | |
2759 | Extra_Constrained (Formal)); | |
70482933 RK |
2760 | |
2761 | elsif Is_Constrained (Etype (Formal)) | |
2762 | or else not Has_Discriminants (Etype (Prev)) | |
2763 | then | |
01aef5ad GD |
2764 | Add_Extra_Actual |
2765 | (New_Occurrence_Of (Standard_True, Loc), | |
2766 | Extra_Constrained (Formal)); | |
70482933 | 2767 | |
5d09245e AC |
2768 | -- Do not produce extra actuals for Unchecked_Union parameters. |
2769 | -- Jump directly to the end of the loop. | |
2770 | ||
2771 | elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then | |
2772 | goto Skip_Extra_Actual_Generation; | |
2773 | ||
70482933 RK |
2774 | else |
2775 | -- If the actual is a type conversion, then the constrained | |
2776 | -- test applies to the actual, not the target type. | |
2777 | ||
2778 | declare | |
2f1b20a9 | 2779 | Act_Prev : Node_Id; |
70482933 RK |
2780 | |
2781 | begin | |
2f1b20a9 ES |
2782 | -- Test for unchecked conversions as well, which can occur |
2783 | -- as out parameter actuals on calls to stream procedures. | |
70482933 | 2784 | |
2f1b20a9 | 2785 | Act_Prev := Prev; |
ac4d6407 RD |
2786 | while Nkind_In (Act_Prev, N_Type_Conversion, |
2787 | N_Unchecked_Type_Conversion) | |
fbf5a39b | 2788 | loop |
70482933 | 2789 | Act_Prev := Expression (Act_Prev); |
fbf5a39b | 2790 | end loop; |
70482933 | 2791 | |
3563739b AC |
2792 | -- If the expression is a conversion of a dereference, this |
2793 | -- is internally generated code that manipulates addresses, | |
2794 | -- e.g. when building interface tables. No check should | |
2795 | -- occur in this case, and the discriminated object is not | |
2796 | -- directly a hand. | |
f4d379b8 HK |
2797 | |
2798 | if not Comes_From_Source (Actual) | |
2799 | and then Nkind (Actual) = N_Unchecked_Type_Conversion | |
2800 | and then Nkind (Act_Prev) = N_Explicit_Dereference | |
2801 | then | |
2802 | Add_Extra_Actual | |
2803 | (New_Occurrence_Of (Standard_False, Loc), | |
2804 | Extra_Constrained (Formal)); | |
2805 | ||
2806 | else | |
2807 | Add_Extra_Actual | |
2808 | (Make_Attribute_Reference (Sloc (Prev), | |
2809 | Prefix => | |
2810 | Duplicate_Subexpr_No_Checks | |
2811 | (Act_Prev, Name_Req => True), | |
2812 | Attribute_Name => Name_Constrained), | |
2813 | Extra_Constrained (Formal)); | |
2814 | end if; | |
70482933 RK |
2815 | end; |
2816 | end if; | |
2817 | end if; | |
2818 | ||
2819 | -- Create possible extra actual for accessibility level | |
2820 | ||
2821 | if Present (Extra_Accessibility (Formal)) then | |
7888a6ae GD |
2822 | |
2823 | -- Ada 2005 (AI-252): If the actual was rewritten as an Access | |
2824 | -- attribute, then the original actual may be an aliased object | |
2825 | -- occurring as the prefix in a call using "Object.Operation" | |
2826 | -- notation. In that case we must pass the level of the object, | |
2827 | -- so Prev_Orig is reset to Prev and the attribute will be | |
2828 | -- processed by the code for Access attributes further below. | |
2829 | ||
2830 | if Prev_Orig /= Prev | |
2831 | and then Nkind (Prev) = N_Attribute_Reference | |
2832 | and then | |
2833 | Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access | |
2834 | and then Is_Aliased_View (Prev_Orig) | |
2835 | then | |
2836 | Prev_Orig := Prev; | |
2837 | end if; | |
2838 | ||
9d983bbf AC |
2839 | -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of |
2840 | -- accessibility levels. | |
fdce4bb7 | 2841 | |
da1c23dd | 2842 | if Is_Thunk (Current_Scope) then |
fdce4bb7 JM |
2843 | declare |
2844 | Parm_Ent : Entity_Id; | |
2845 | ||
2846 | begin | |
2847 | if Is_Controlling_Actual (Actual) then | |
2848 | ||
2849 | -- Find the corresponding actual of the thunk | |
2850 | ||
2851 | Parm_Ent := First_Entity (Current_Scope); | |
2852 | for J in 2 .. Param_Count loop | |
2853 | Next_Entity (Parm_Ent); | |
2854 | end loop; | |
2855 | ||
8a49a499 | 2856 | -- Handle unchecked conversion of access types generated |
5b5b27ad | 2857 | -- in thunks (cf. Expand_Interface_Thunk). |
8a49a499 AC |
2858 | |
2859 | elsif Is_Access_Type (Etype (Actual)) | |
2860 | and then Nkind (Actual) = N_Unchecked_Type_Conversion | |
2861 | then | |
2862 | Parm_Ent := Entity (Expression (Actual)); | |
2863 | ||
fdce4bb7 JM |
2864 | else pragma Assert (Is_Entity_Name (Actual)); |
2865 | Parm_Ent := Entity (Actual); | |
2866 | end if; | |
2867 | ||
2868 | Add_Extra_Actual | |
2869 | (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc), | |
2870 | Extra_Accessibility (Formal)); | |
2871 | end; | |
2872 | ||
2873 | elsif Is_Entity_Name (Prev_Orig) then | |
70482933 | 2874 | |
d766cee3 RD |
2875 | -- When passing an access parameter, or a renaming of an access |
2876 | -- parameter, as the actual to another access parameter we need | |
2877 | -- to pass along the actual's own access level parameter. This | |
2878 | -- is done if we are within the scope of the formal access | |
2879 | -- parameter (if this is an inlined body the extra formal is | |
2880 | -- irrelevant). | |
2881 | ||
2882 | if (Is_Formal (Entity (Prev_Orig)) | |
2883 | or else | |
2884 | (Present (Renamed_Object (Entity (Prev_Orig))) | |
2885 | and then | |
2886 | Is_Entity_Name (Renamed_Object (Entity (Prev_Orig))) | |
2887 | and then | |
2888 | Is_Formal | |
2889 | (Entity (Renamed_Object (Entity (Prev_Orig)))))) | |
70482933 RK |
2890 | and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type |
2891 | and then In_Open_Scopes (Scope (Entity (Prev_Orig))) | |
2892 | then | |
2893 | declare | |
2894 | Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig); | |
2895 | ||
2896 | begin | |
2897 | pragma Assert (Present (Parm_Ent)); | |
2898 | ||
2899 | if Present (Extra_Accessibility (Parm_Ent)) then | |
f4d379b8 HK |
2900 | Add_Extra_Actual |
2901 | (New_Occurrence_Of | |
2902 | (Extra_Accessibility (Parm_Ent), Loc), | |
2903 | Extra_Accessibility (Formal)); | |
70482933 RK |
2904 | |
2905 | -- If the actual access parameter does not have an | |
2906 | -- associated extra formal providing its scope level, | |
2907 | -- then treat the actual as having library-level | |
2908 | -- accessibility. | |
2909 | ||
2910 | else | |
f4d379b8 HK |
2911 | Add_Extra_Actual |
2912 | (Make_Integer_Literal (Loc, | |
01aef5ad | 2913 | Intval => Scope_Depth (Standard_Standard)), |
f4d379b8 | 2914 | Extra_Accessibility (Formal)); |
70482933 RK |
2915 | end if; |
2916 | end; | |
2917 | ||
7888a6ae GD |
2918 | -- The actual is a normal access value, so just pass the level |
2919 | -- of the actual's access type. | |
70482933 RK |
2920 | |
2921 | else | |
f4d379b8 | 2922 | Add_Extra_Actual |
d15f9422 | 2923 | (Dynamic_Accessibility_Level (Prev_Orig), |
f4d379b8 | 2924 | Extra_Accessibility (Formal)); |
70482933 RK |
2925 | end if; |
2926 | ||
01aef5ad GD |
2927 | -- If the actual is an access discriminant, then pass the level |
2928 | -- of the enclosing object (RM05-3.10.2(12.4/2)). | |
2929 | ||
2930 | elsif Nkind (Prev_Orig) = N_Selected_Component | |
2931 | and then Ekind (Entity (Selector_Name (Prev_Orig))) = | |
2932 | E_Discriminant | |
2933 | and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) = | |
2934 | E_Anonymous_Access_Type | |
2935 | then | |
2936 | Add_Extra_Actual | |
2937 | (Make_Integer_Literal (Loc, | |
2938 | Intval => Object_Access_Level (Prefix (Prev_Orig))), | |
2939 | Extra_Accessibility (Formal)); | |
2940 | ||
2941 | -- All other cases | |
fdce4bb7 | 2942 | |
70482933 RK |
2943 | else |
2944 | case Nkind (Prev_Orig) is | |
2945 | ||
2946 | when N_Attribute_Reference => | |
70482933 RK |
2947 | case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is |
2948 | ||
75a64833 | 2949 | -- For X'Access, pass on the level of the prefix X |
70482933 RK |
2950 | |
2951 | when Attribute_Access => | |
996c8821 | 2952 | |
6cce2156 GD |
2953 | -- If this is an Access attribute applied to the |
2954 | -- the current instance object passed to a type | |
2955 | -- initialization procedure, then use the level | |
2956 | -- of the type itself. This is not really correct, | |
2957 | -- as there should be an extra level parameter | |
2958 | -- passed in with _init formals (only in the case | |
2959 | -- where the type is immutably limited), but we | |
2960 | -- don't have an easy way currently to create such | |
2961 | -- an extra formal (init procs aren't ever frozen). | |
2962 | -- For now we just use the level of the type, | |
2963 | -- which may be too shallow, but that works better | |
2964 | -- than passing Object_Access_Level of the type, | |
2965 | -- which can be one level too deep in some cases. | |
2966 | -- ??? | |
2967 | ||
2968 | if Is_Entity_Name (Prefix (Prev_Orig)) | |
2969 | and then Is_Type (Entity (Prefix (Prev_Orig))) | |
2970 | then | |
2971 | Add_Extra_Actual | |
2972 | (Make_Integer_Literal (Loc, | |
2973 | Intval => | |
2974 | Type_Access_Level | |
2975 | (Entity (Prefix (Prev_Orig)))), | |
2976 | Extra_Accessibility (Formal)); | |
2977 | ||
2978 | else | |
2979 | Add_Extra_Actual | |
2980 | (Make_Integer_Literal (Loc, | |
2981 | Intval => | |
2982 | Object_Access_Level | |
2983 | (Prefix (Prev_Orig))), | |
2984 | Extra_Accessibility (Formal)); | |
2985 | end if; | |
70482933 RK |
2986 | |
2987 | -- Treat the unchecked attributes as library-level | |
2988 | ||
2989 | when Attribute_Unchecked_Access | | |
2990 | Attribute_Unrestricted_Access => | |
01aef5ad GD |
2991 | Add_Extra_Actual |
2992 | (Make_Integer_Literal (Loc, | |
2993 | Intval => Scope_Depth (Standard_Standard)), | |
2994 | Extra_Accessibility (Formal)); | |
70482933 RK |
2995 | |
2996 | -- No other cases of attributes returning access | |
9d983bbf | 2997 | -- values that can be passed to access parameters. |
70482933 RK |
2998 | |
2999 | when others => | |
3000 | raise Program_Error; | |
3001 | ||
3002 | end case; | |
3003 | ||
92a745f3 TQ |
3004 | -- For allocators we pass the level of the execution of the |
3005 | -- called subprogram, which is one greater than the current | |
3006 | -- scope level. | |
70482933 RK |
3007 | |
3008 | when N_Allocator => | |
01aef5ad GD |
3009 | Add_Extra_Actual |
3010 | (Make_Integer_Literal (Loc, | |
3011 | Intval => Scope_Depth (Current_Scope) + 1), | |
3012 | Extra_Accessibility (Formal)); | |
70482933 | 3013 | |
d15f9422 AC |
3014 | -- For most other cases we simply pass the level of the |
3015 | -- actual's access type. The type is retrieved from | |
3016 | -- Prev rather than Prev_Orig, because in some cases | |
3017 | -- Prev_Orig denotes an original expression that has | |
3018 | -- not been analyzed. | |
70482933 RK |
3019 | |
3020 | when others => | |
01aef5ad | 3021 | Add_Extra_Actual |
d15f9422 | 3022 | (Dynamic_Accessibility_Level (Prev), |
01aef5ad | 3023 | Extra_Accessibility (Formal)); |
70482933 RK |
3024 | end case; |
3025 | end if; | |
3026 | end if; | |
3027 | ||
2f1b20a9 | 3028 | -- Perform the check of 4.6(49) that prevents a null value from being |
b3f48fd4 AC |
3029 | -- passed as an actual to an access parameter. Note that the check |
3030 | -- is elided in the common cases of passing an access attribute or | |
2f1b20a9 ES |
3031 | -- access parameter as an actual. Also, we currently don't enforce |
3032 | -- this check for expander-generated actuals and when -gnatdj is set. | |
70482933 | 3033 | |
0791fbe9 | 3034 | if Ada_Version >= Ada_2005 then |
70482933 | 3035 | |
b3f48fd4 AC |
3036 | -- Ada 2005 (AI-231): Check null-excluding access types. Note that |
3037 | -- the intent of 6.4.1(13) is that null-exclusion checks should | |
3038 | -- not be done for 'out' parameters, even though it refers only | |
308e6f3a | 3039 | -- to constraint checks, and a null_exclusion is not a constraint. |
b3f48fd4 | 3040 | -- Note that AI05-0196-1 corrects this mistake in the RM. |
70482933 | 3041 | |
2f1b20a9 ES |
3042 | if Is_Access_Type (Etype (Formal)) |
3043 | and then Can_Never_Be_Null (Etype (Formal)) | |
b3f48fd4 | 3044 | and then Ekind (Formal) /= E_Out_Parameter |
2f1b20a9 | 3045 | and then Nkind (Prev) /= N_Raise_Constraint_Error |
d766cee3 | 3046 | and then (Known_Null (Prev) |
996c8821 | 3047 | or else not Can_Never_Be_Null (Etype (Prev))) |
2f1b20a9 ES |
3048 | then |
3049 | Install_Null_Excluding_Check (Prev); | |
3050 | end if; | |
70482933 | 3051 | |
0791fbe9 | 3052 | -- Ada_Version < Ada_2005 |
70482933 | 3053 | |
2f1b20a9 ES |
3054 | else |
3055 | if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type | |
3056 | or else Access_Checks_Suppressed (Subp) | |
3057 | then | |
3058 | null; | |
70482933 | 3059 | |
2f1b20a9 ES |
3060 | elsif Debug_Flag_J then |
3061 | null; | |
70482933 | 3062 | |
2f1b20a9 ES |
3063 | elsif not Comes_From_Source (Prev) then |
3064 | null; | |
70482933 | 3065 | |
2f1b20a9 ES |
3066 | elsif Is_Entity_Name (Prev) |
3067 | and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type | |
3068 | then | |
3069 | null; | |
2820d220 | 3070 | |
ac4d6407 | 3071 | elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then |
2f1b20a9 ES |
3072 | null; |
3073 | ||
3074 | -- Suppress null checks when passing to access parameters of Java | |
7888a6ae GD |
3075 | -- and CIL subprograms. (Should this be done for other foreign |
3076 | -- conventions as well ???) | |
2f1b20a9 | 3077 | |
7888a6ae GD |
3078 | elsif Convention (Subp) = Convention_Java |
3079 | or else Convention (Subp) = Convention_CIL | |
3080 | then | |
2f1b20a9 ES |
3081 | null; |
3082 | ||
3083 | else | |
3084 | Install_Null_Excluding_Check (Prev); | |
3085 | end if; | |
70482933 RK |
3086 | end if; |
3087 | ||
fbf5a39b AC |
3088 | -- Perform appropriate validity checks on parameters that |
3089 | -- are entities. | |
70482933 RK |
3090 | |
3091 | if Validity_Checks_On then | |
6cdb2c6e | 3092 | if (Ekind (Formal) = E_In_Parameter |
996c8821 | 3093 | and then Validity_Check_In_Params) |
6cdb2c6e AC |
3094 | or else |
3095 | (Ekind (Formal) = E_In_Out_Parameter | |
996c8821 | 3096 | and then Validity_Check_In_Out_Params) |
70482933 | 3097 | then |
7888a6ae GD |
3098 | -- If the actual is an indexed component of a packed type (or |
3099 | -- is an indexed or selected component whose prefix recursively | |
3100 | -- meets this condition), it has not been expanded yet. It will | |
3101 | -- be copied in the validity code that follows, and has to be | |
3102 | -- expanded appropriately, so reanalyze it. | |
08aa9a4a | 3103 | |
7888a6ae GD |
3104 | -- What we do is just to unset analyzed bits on prefixes till |
3105 | -- we reach something that does not have a prefix. | |
3106 | ||
3107 | declare | |
3108 | Nod : Node_Id; | |
3109 | ||
3110 | begin | |
3111 | Nod := Actual; | |
ac4d6407 RD |
3112 | while Nkind_In (Nod, N_Indexed_Component, |
3113 | N_Selected_Component) | |
7888a6ae GD |
3114 | loop |
3115 | Set_Analyzed (Nod, False); | |
3116 | Nod := Prefix (Nod); | |
3117 | end loop; | |
3118 | end; | |
08aa9a4a | 3119 | |
70482933 | 3120 | Ensure_Valid (Actual); |
70482933 RK |
3121 | end if; |
3122 | end if; | |
3123 | ||
3124 | -- For IN OUT and OUT parameters, ensure that subscripts are valid | |
3125 | -- since this is a left side reference. We only do this for calls | |
3126 | -- from the source program since we assume that compiler generated | |
3127 | -- calls explicitly generate any required checks. We also need it | |
b3f48fd4 AC |
3128 | -- only if we are doing standard validity checks, since clearly it is |
3129 | -- not needed if validity checks are off, and in subscript validity | |
3130 | -- checking mode, all indexed components are checked with a call | |
3131 | -- directly from Expand_N_Indexed_Component. | |
70482933 | 3132 | |
6dfc5592 | 3133 | if Comes_From_Source (Call_Node) |
70482933 RK |
3134 | and then Ekind (Formal) /= E_In_Parameter |
3135 | and then Validity_Checks_On | |
3136 | and then Validity_Check_Default | |
3137 | and then not Validity_Check_Subscripts | |
3138 | then | |
3139 | Check_Valid_Lvalue_Subscripts (Actual); | |
3140 | end if; | |
3141 | ||
c8ef728f ES |
3142 | -- Mark any scalar OUT parameter that is a simple variable as no |
3143 | -- longer known to be valid (unless the type is always valid). This | |
3144 | -- reflects the fact that if an OUT parameter is never set in a | |
3145 | -- procedure, then it can become invalid on the procedure return. | |
fbf5a39b AC |
3146 | |
3147 | if Ekind (Formal) = E_Out_Parameter | |
3148 | and then Is_Entity_Name (Actual) | |
3149 | and then Ekind (Entity (Actual)) = E_Variable | |
3150 | and then not Is_Known_Valid (Etype (Actual)) | |
3151 | then | |
3152 | Set_Is_Known_Valid (Entity (Actual), False); | |
3153 | end if; | |
3154 | ||
c8ef728f ES |
3155 | -- For an OUT or IN OUT parameter, if the actual is an entity, then |
3156 | -- clear current values, since they can be clobbered. We are probably | |
3157 | -- doing this in more places than we need to, but better safe than | |
a90bd866 | 3158 | -- sorry when it comes to retaining bad current values. |
fbf5a39b AC |
3159 | |
3160 | if Ekind (Formal) /= E_In_Parameter | |
3161 | and then Is_Entity_Name (Actual) | |
67ce0d7e | 3162 | and then Present (Entity (Actual)) |
fbf5a39b | 3163 | then |
67ce0d7e RD |
3164 | declare |
3165 | Ent : constant Entity_Id := Entity (Actual); | |
3166 | Sav : Node_Id; | |
3167 | ||
3168 | begin | |
ac4d6407 RD |
3169 | -- For an OUT or IN OUT parameter that is an assignable entity, |
3170 | -- we do not want to clobber the Last_Assignment field, since | |
3171 | -- if it is set, it was precisely because it is indeed an OUT | |
a90bd866 | 3172 | -- or IN OUT parameter. We do reset the Is_Known_Valid flag |
75ba322d | 3173 | -- since the subprogram could have returned in invalid value. |
ac4d6407 | 3174 | |
8c5b03a0 | 3175 | if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter) |
67ce0d7e RD |
3176 | and then Is_Assignable (Ent) |
3177 | then | |
3178 | Sav := Last_Assignment (Ent); | |
3179 | Kill_Current_Values (Ent); | |
3180 | Set_Last_Assignment (Ent, Sav); | |
75ba322d | 3181 | Set_Is_Known_Valid (Ent, False); |
67ce0d7e | 3182 | |
4bb43ffb | 3183 | -- For all other cases, just kill the current values |
67ce0d7e RD |
3184 | |
3185 | else | |
3186 | Kill_Current_Values (Ent); | |
3187 | end if; | |
3188 | end; | |
fbf5a39b AC |
3189 | end if; |
3190 | ||
70482933 RK |
3191 | -- If the formal is class wide and the actual is an aggregate, force |
3192 | -- evaluation so that the back end who does not know about class-wide | |
3193 | -- type, does not generate a temporary of the wrong size. | |
3194 | ||
3195 | if not Is_Class_Wide_Type (Etype (Formal)) then | |
3196 | null; | |
3197 | ||
3198 | elsif Nkind (Actual) = N_Aggregate | |
3199 | or else (Nkind (Actual) = N_Qualified_Expression | |
3200 | and then Nkind (Expression (Actual)) = N_Aggregate) | |
3201 | then | |
3202 | Force_Evaluation (Actual); | |
3203 | end if; | |
3204 | ||
3205 | -- In a remote call, if the formal is of a class-wide type, check | |
3206 | -- that the actual meets the requirements described in E.4(18). | |
3207 | ||
7888a6ae | 3208 | if Remote and then Is_Class_Wide_Type (Etype (Formal)) then |
70482933 | 3209 | Insert_Action (Actual, |
7888a6ae GD |
3210 | Make_Transportable_Check (Loc, |
3211 | Duplicate_Subexpr_Move_Checks (Actual))); | |
70482933 RK |
3212 | end if; |
3213 | ||
5d09245e AC |
3214 | -- This label is required when skipping extra actual generation for |
3215 | -- Unchecked_Union parameters. | |
3216 | ||
3217 | <<Skip_Extra_Actual_Generation>> | |
3218 | ||
fdce4bb7 | 3219 | Param_Count := Param_Count + 1; |
70482933 RK |
3220 | Next_Actual (Actual); |
3221 | Next_Formal (Formal); | |
3222 | end loop; | |
3223 | ||
bdf69d33 | 3224 | -- If we are calling an Ada 2012 function which needs to have the |
63585f75 SB |
3225 | -- "accessibility level determined by the point of call" (AI05-0234) |
3226 | -- passed in to it, then pass it in. | |
3227 | ||
b8a93198 | 3228 | if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type) |
57a3fca9 AC |
3229 | and then |
3230 | Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp))) | |
63585f75 SB |
3231 | then |
3232 | declare | |
3233 | Ancestor : Node_Id := Parent (Call_Node); | |
3234 | Level : Node_Id := Empty; | |
3235 | Defer : Boolean := False; | |
3236 | ||
3237 | begin | |
3238 | -- Unimplemented: if Subp returns an anonymous access type, then | |
57a3fca9 | 3239 | |
63585f75 SB |
3240 | -- a) if the call is the operand of an explict conversion, then |
3241 | -- the target type of the conversion (a named access type) | |
3242 | -- determines the accessibility level pass in; | |
57a3fca9 | 3243 | |
63585f75 SB |
3244 | -- b) if the call defines an access discriminant of an object |
3245 | -- (e.g., the discriminant of an object being created by an | |
3246 | -- allocator, or the discriminant of a function result), | |
3247 | -- then the accessibility level to pass in is that of the | |
3248 | -- discriminated object being initialized). | |
3249 | ||
57a3fca9 AC |
3250 | -- ??? |
3251 | ||
63585f75 SB |
3252 | while Nkind (Ancestor) = N_Qualified_Expression |
3253 | loop | |
3254 | Ancestor := Parent (Ancestor); | |
3255 | end loop; | |
3256 | ||
3257 | case Nkind (Ancestor) is | |
3258 | when N_Allocator => | |
ebf494ec | 3259 | |
63585f75 | 3260 | -- At this point, we'd like to assign |
ebf494ec | 3261 | |
63585f75 | 3262 | -- Level := Dynamic_Accessibility_Level (Ancestor); |
ebf494ec | 3263 | |
63585f75 SB |
3264 | -- but Etype of Ancestor may not have been set yet, |
3265 | -- so that doesn't work. | |
ebf494ec | 3266 | |
63585f75 SB |
3267 | -- Handle this later in Expand_Allocator_Expression. |
3268 | ||
3269 | Defer := True; | |
3270 | ||
3271 | when N_Object_Declaration | N_Object_Renaming_Declaration => | |
3272 | declare | |
3273 | Def_Id : constant Entity_Id := | |
3274 | Defining_Identifier (Ancestor); | |
ebf494ec | 3275 | |
63585f75 SB |
3276 | begin |
3277 | if Is_Return_Object (Def_Id) then | |
3278 | if Present (Extra_Accessibility_Of_Result | |
3279 | (Return_Applies_To (Scope (Def_Id)))) | |
3280 | then | |
3281 | -- Pass along value that was passed in if the | |
3282 | -- routine we are returning from also has an | |
3283 | -- Accessibility_Of_Result formal. | |
3284 | ||
3285 | Level := | |
3286 | New_Occurrence_Of | |
3287 | (Extra_Accessibility_Of_Result | |
ebf494ec | 3288 | (Return_Applies_To (Scope (Def_Id))), Loc); |
63585f75 SB |
3289 | end if; |
3290 | else | |
ebf494ec RD |
3291 | Level := |
3292 | Make_Integer_Literal (Loc, | |
3293 | Intval => Object_Access_Level (Def_Id)); | |
63585f75 SB |
3294 | end if; |
3295 | end; | |
3296 | ||
3297 | when N_Simple_Return_Statement => | |
3298 | if Present (Extra_Accessibility_Of_Result | |
ebf494ec RD |
3299 | (Return_Applies_To |
3300 | (Return_Statement_Entity (Ancestor)))) | |
63585f75 | 3301 | then |
fb12497d AC |
3302 | -- Pass along value that was passed in if the returned |
3303 | -- routine also has an Accessibility_Of_Result formal. | |
63585f75 SB |
3304 | |
3305 | Level := | |
3306 | New_Occurrence_Of | |
3307 | (Extra_Accessibility_Of_Result | |
3308 | (Return_Applies_To | |
3309 | (Return_Statement_Entity (Ancestor))), Loc); | |
3310 | end if; | |
3311 | ||
3312 | when others => | |
3313 | null; | |
3314 | end case; | |
3315 | ||
3316 | if not Defer then | |
3317 | if not Present (Level) then | |
ebf494ec | 3318 | |
63585f75 | 3319 | -- The "innermost master that evaluates the function call". |
ebf494ec | 3320 | |
886b5a18 AC |
3321 | -- ??? - Should we use Integer'Last here instead in order |
3322 | -- to deal with (some of) the problems associated with | |
3323 | -- calls to subps whose enclosing scope is unknown (e.g., | |
3324 | -- Anon_Access_To_Subp_Param.all)? | |
63585f75 SB |
3325 | |
3326 | Level := Make_Integer_Literal (Loc, | |
3327 | Scope_Depth (Current_Scope) + 1); | |
3328 | end if; | |
3329 | ||
57a3fca9 AC |
3330 | Add_Extra_Actual |
3331 | (Level, | |
3332 | Extra_Accessibility_Of_Result (Ultimate_Alias (Subp))); | |
63585f75 SB |
3333 | end if; |
3334 | end; | |
3335 | end if; | |
3336 | ||
4bb43ffb | 3337 | -- If we are expanding the RHS of an assignment we need to check if tag |
c8ef728f ES |
3338 | -- propagation is needed. You might expect this processing to be in |
3339 | -- Analyze_Assignment but has to be done earlier (bottom-up) because the | |
3340 | -- assignment might be transformed to a declaration for an unconstrained | |
3341 | -- value if the expression is classwide. | |
70482933 | 3342 | |
6dfc5592 RD |
3343 | if Nkind (Call_Node) = N_Function_Call |
3344 | and then Is_Tag_Indeterminate (Call_Node) | |
3345 | and then Is_Entity_Name (Name (Call_Node)) | |
70482933 RK |
3346 | then |
3347 | declare | |
3348 | Ass : Node_Id := Empty; | |
3349 | ||
3350 | begin | |
6dfc5592 RD |
3351 | if Nkind (Parent (Call_Node)) = N_Assignment_Statement then |
3352 | Ass := Parent (Call_Node); | |
70482933 | 3353 | |
6dfc5592 | 3354 | elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression |
3cae7f14 RD |
3355 | and then Nkind (Parent (Parent (Call_Node))) = |
3356 | N_Assignment_Statement | |
70482933 | 3357 | then |
6dfc5592 | 3358 | Ass := Parent (Parent (Call_Node)); |
02822a92 | 3359 | |
6dfc5592 | 3360 | elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference |
3cae7f14 RD |
3361 | and then Nkind (Parent (Parent (Call_Node))) = |
3362 | N_Assignment_Statement | |
02822a92 | 3363 | then |
6dfc5592 | 3364 | Ass := Parent (Parent (Call_Node)); |
70482933 RK |
3365 | end if; |
3366 | ||
3367 | if Present (Ass) | |
3368 | and then Is_Class_Wide_Type (Etype (Name (Ass))) | |
3369 | then | |
6dfc5592 RD |
3370 | if Is_Access_Type (Etype (Call_Node)) then |
3371 | if Designated_Type (Etype (Call_Node)) /= | |
02822a92 RD |
3372 | Root_Type (Etype (Name (Ass))) |
3373 | then | |
3374 | Error_Msg_NE | |
3375 | ("tag-indeterminate expression " | |
d766cee3 | 3376 | & " must have designated type& (RM 5.2 (6))", |
3cae7f14 | 3377 | Call_Node, Root_Type (Etype (Name (Ass)))); |
02822a92 | 3378 | else |
6dfc5592 | 3379 | Propagate_Tag (Name (Ass), Call_Node); |
02822a92 RD |
3380 | end if; |
3381 | ||
6dfc5592 | 3382 | elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then |
fbf5a39b AC |
3383 | Error_Msg_NE |
3384 | ("tag-indeterminate expression must have type&" | |
6dfc5592 RD |
3385 | & "(RM 5.2 (6))", |
3386 | Call_Node, Root_Type (Etype (Name (Ass)))); | |
02822a92 | 3387 | |
fbf5a39b | 3388 | else |
6dfc5592 | 3389 | Propagate_Tag (Name (Ass), Call_Node); |
fbf5a39b AC |
3390 | end if; |
3391 | ||
3392 | -- The call will be rewritten as a dispatching call, and | |
3393 | -- expanded as such. | |
3394 | ||
70482933 RK |
3395 | return; |
3396 | end if; | |
3397 | end; | |
3398 | end if; | |
3399 | ||
758c442c GD |
3400 | -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand |
3401 | -- it to point to the correct secondary virtual table | |
3402 | ||
d3b00ce3 | 3403 | if Nkind (Call_Node) in N_Subprogram_Call |
758c442c GD |
3404 | and then CW_Interface_Formals_Present |
3405 | then | |
6dfc5592 | 3406 | Expand_Interface_Actuals (Call_Node); |
758c442c GD |
3407 | end if; |
3408 | ||
70482933 RK |
3409 | -- Deals with Dispatch_Call if we still have a call, before expanding |
3410 | -- extra actuals since this will be done on the re-analysis of the | |
b3f48fd4 AC |
3411 | -- dispatching call. Note that we do not try to shorten the actual list |
3412 | -- for a dispatching call, it would not make sense to do so. Expansion | |
3413 | -- of dispatching calls is suppressed when VM_Target, because the VM | |
3414 | -- back-ends directly handle the generation of dispatching calls and | |
3415 | -- would have to undo any expansion to an indirect call. | |
70482933 | 3416 | |
d3b00ce3 | 3417 | if Nkind (Call_Node) in N_Subprogram_Call |
6dfc5592 | 3418 | and then Present (Controlling_Argument (Call_Node)) |
70482933 | 3419 | then |
6dfc5592 | 3420 | declare |
dd386db0 | 3421 | Call_Typ : constant Entity_Id := Etype (Call_Node); |
6dfc5592 RD |
3422 | Typ : constant Entity_Id := Find_Dispatching_Type (Subp); |
3423 | Eq_Prim_Op : Entity_Id := Empty; | |
dd386db0 AC |
3424 | New_Call : Node_Id; |
3425 | Param : Node_Id; | |
3426 | Prev_Call : Node_Id; | |
fbf5a39b | 3427 | |
6dfc5592 RD |
3428 | begin |
3429 | if not Is_Limited_Type (Typ) then | |
3430 | Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq); | |
3431 | end if; | |
fbf5a39b | 3432 | |
6dfc5592 RD |
3433 | if Tagged_Type_Expansion then |
3434 | Expand_Dispatching_Call (Call_Node); | |
70f91180 | 3435 | |
6dfc5592 RD |
3436 | -- The following return is worrisome. Is it really OK to skip |
3437 | -- all remaining processing in this procedure ??? | |
5a1ccfb1 | 3438 | |
6dfc5592 | 3439 | return; |
5a1ccfb1 | 3440 | |
6dfc5592 RD |
3441 | -- VM targets |
3442 | ||
3443 | else | |
3444 | Apply_Tag_Checks (Call_Node); | |
3445 | ||
dd386db0 AC |
3446 | -- If this is a dispatching "=", we must first compare the |
3447 | -- tags so we generate: x.tag = y.tag and then x = y | |
3448 | ||
3449 | if Subp = Eq_Prim_Op then | |
3450 | ||
3451 | -- Mark the node as analyzed to avoid reanalizing this | |
3452 | -- dispatching call (which would cause a never-ending loop) | |
3453 | ||
3454 | Prev_Call := Relocate_Node (Call_Node); | |
3455 | Set_Analyzed (Prev_Call); | |
3456 | ||
3457 | Param := First_Actual (Call_Node); | |
3458 | New_Call := | |
3459 | Make_And_Then (Loc, | |
3460 | Left_Opnd => | |
3461 | Make_Op_Eq (Loc, | |
3462 | Left_Opnd => | |
3463 | Make_Selected_Component (Loc, | |
3464 | Prefix => New_Value (Param), | |
3465 | Selector_Name => | |
e4494292 RD |
3466 | New_Occurrence_Of |
3467 | (First_Tag_Component (Typ), Loc)), | |
dd386db0 AC |
3468 | |
3469 | Right_Opnd => | |
3470 | Make_Selected_Component (Loc, | |
3471 | Prefix => | |
3472 | Unchecked_Convert_To (Typ, | |
3473 | New_Value (Next_Actual (Param))), | |
3474 | Selector_Name => | |
e4494292 | 3475 | New_Occurrence_Of |
dd386db0 AC |
3476 | (First_Tag_Component (Typ), Loc))), |
3477 | Right_Opnd => Prev_Call); | |
3478 | ||
3479 | Rewrite (Call_Node, New_Call); | |
3480 | ||
3481 | Analyze_And_Resolve | |
3482 | (Call_Node, Call_Typ, Suppress => All_Checks); | |
3483 | end if; | |
3484 | ||
6dfc5592 RD |
3485 | -- Expansion of a dispatching call results in an indirect call, |
3486 | -- which in turn causes current values to be killed (see | |
3487 | -- Resolve_Call), so on VM targets we do the call here to | |
3488 | -- ensure consistent warnings between VM and non-VM targets. | |
3489 | ||
3490 | Kill_Current_Values; | |
3491 | end if; | |
3492 | ||
3493 | -- If this is a dispatching "=" then we must update the reference | |
3494 | -- to the call node because we generated: | |
3495 | -- x.tag = y.tag and then x = y | |
3496 | ||
dd386db0 | 3497 | if Subp = Eq_Prim_Op then |
6dfc5592 RD |
3498 | Call_Node := Right_Opnd (Call_Node); |
3499 | end if; | |
3500 | end; | |
70f91180 | 3501 | end if; |
70482933 RK |
3502 | |
3503 | -- Similarly, expand calls to RCI subprograms on which pragma | |
3504 | -- All_Calls_Remote applies. The rewriting will be reanalyzed | |
b3f48fd4 AC |
3505 | -- later. Do this only when the call comes from source since we |
3506 | -- do not want such a rewriting to occur in expanded code. | |
70482933 | 3507 | |
6dfc5592 RD |
3508 | if Is_All_Remote_Call (Call_Node) then |
3509 | Expand_All_Calls_Remote_Subprogram_Call (Call_Node); | |
70482933 RK |
3510 | |
3511 | -- Similarly, do not add extra actuals for an entry call whose entity | |
3512 | -- is a protected procedure, or for an internal protected subprogram | |
3513 | -- call, because it will be rewritten as a protected subprogram call | |
3514 | -- and reanalyzed (see Expand_Protected_Subprogram_Call). | |
3515 | ||
3516 | elsif Is_Protected_Type (Scope (Subp)) | |
3517 | and then (Ekind (Subp) = E_Procedure | |
3518 | or else Ekind (Subp) = E_Function) | |
3519 | then | |
3520 | null; | |
3521 | ||
3522 | -- During that loop we gathered the extra actuals (the ones that | |
3523 | -- correspond to Extra_Formals), so now they can be appended. | |
3524 | ||
3525 | else | |
3526 | while Is_Non_Empty_List (Extra_Actuals) loop | |
3527 | Add_Actual_Parameter (Remove_Head (Extra_Actuals)); | |
3528 | end loop; | |
3529 | end if; | |
3530 | ||
b3f48fd4 AC |
3531 | -- At this point we have all the actuals, so this is the point at which |
3532 | -- the various expansion activities for actuals is carried out. | |
f44fe430 | 3533 | |
6dfc5592 | 3534 | Expand_Actuals (Call_Node, Subp); |
70482933 | 3535 | |
5f49133f AC |
3536 | -- Verify that the actuals do not share storage. This check must be done |
3537 | -- on the caller side rather that inside the subprogram to avoid issues | |
3538 | -- of parameter passing. | |
3539 | ||
3540 | if Check_Aliasing_Of_Parameters then | |
3541 | Apply_Parameter_Aliasing_Checks (Call_Node, Subp); | |
3542 | end if; | |
3543 | ||
b3f48fd4 AC |
3544 | -- If the subprogram is a renaming, or if it is inherited, replace it in |
3545 | -- the call with the name of the actual subprogram being called. If this | |
3546 | -- is a dispatching call, the run-time decides what to call. The Alias | |
3547 | -- attribute does not apply to entries. | |
70482933 | 3548 | |
6dfc5592 RD |
3549 | if Nkind (Call_Node) /= N_Entry_Call_Statement |
3550 | and then No (Controlling_Argument (Call_Node)) | |
70482933 | 3551 | and then Present (Parent_Subp) |
df3e68b1 | 3552 | and then not Is_Direct_Deep_Call (Subp) |
70482933 RK |
3553 | then |
3554 | if Present (Inherited_From_Formal (Subp)) then | |
3555 | Parent_Subp := Inherited_From_Formal (Subp); | |
3556 | else | |
b81a5940 | 3557 | Parent_Subp := Ultimate_Alias (Parent_Subp); |
70482933 RK |
3558 | end if; |
3559 | ||
c8ef728f ES |
3560 | -- The below setting of Entity is suspect, see F109-018 discussion??? |
3561 | ||
6dfc5592 | 3562 | Set_Entity (Name (Call_Node), Parent_Subp); |
70482933 | 3563 | |
f937473f | 3564 | if Is_Abstract_Subprogram (Parent_Subp) |
70482933 RK |
3565 | and then not In_Instance |
3566 | then | |
3567 | Error_Msg_NE | |
6dfc5592 RD |
3568 | ("cannot call abstract subprogram &!", |
3569 | Name (Call_Node), Parent_Subp); | |
70482933 RK |
3570 | end if; |
3571 | ||
d4817e3f HK |
3572 | -- Inspect all formals of derived subprogram Subp. Compare parameter |
3573 | -- types with the parent subprogram and check whether an actual may | |
3574 | -- need a type conversion to the corresponding formal of the parent | |
3575 | -- subprogram. | |
70482933 | 3576 | |
d4817e3f | 3577 | -- Not clear whether intrinsic subprograms need such conversions. ??? |
70482933 RK |
3578 | |
3579 | if not Is_Intrinsic_Subprogram (Parent_Subp) | |
3580 | or else Is_Generic_Instance (Parent_Subp) | |
3581 | then | |
d4817e3f HK |
3582 | declare |
3583 | procedure Convert (Act : Node_Id; Typ : Entity_Id); | |
3584 | -- Rewrite node Act as a type conversion of Act to Typ. Analyze | |
3585 | -- and resolve the newly generated construct. | |
70482933 | 3586 | |
d4817e3f HK |
3587 | ------------- |
3588 | -- Convert -- | |
3589 | ------------- | |
70482933 | 3590 | |
d4817e3f HK |
3591 | procedure Convert (Act : Node_Id; Typ : Entity_Id) is |
3592 | begin | |
3593 | Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act))); | |
3594 | Analyze (Act); | |
3595 | Resolve (Act, Typ); | |
3596 | end Convert; | |
3597 | ||
3598 | -- Local variables | |
3599 | ||
3600 | Actual_Typ : Entity_Id; | |
3601 | Formal_Typ : Entity_Id; | |
3602 | Parent_Typ : Entity_Id; | |
3603 | ||
3604 | begin | |
6dfc5592 | 3605 | Actual := First_Actual (Call_Node); |
d4817e3f HK |
3606 | Formal := First_Formal (Subp); |
3607 | Parent_Formal := First_Formal (Parent_Subp); | |
3608 | while Present (Formal) loop | |
3609 | Actual_Typ := Etype (Actual); | |
3610 | Formal_Typ := Etype (Formal); | |
3611 | Parent_Typ := Etype (Parent_Formal); | |
3612 | ||
3613 | -- For an IN parameter of a scalar type, the parent formal | |
3614 | -- type and derived formal type differ or the parent formal | |
3615 | -- type and actual type do not match statically. | |
3616 | ||
3617 | if Is_Scalar_Type (Formal_Typ) | |
3618 | and then Ekind (Formal) = E_In_Parameter | |
3619 | and then Formal_Typ /= Parent_Typ | |
3620 | and then | |
3621 | not Subtypes_Statically_Match (Parent_Typ, Actual_Typ) | |
3622 | and then not Raises_Constraint_Error (Actual) | |
3623 | then | |
3624 | Convert (Actual, Parent_Typ); | |
3625 | Enable_Range_Check (Actual); | |
3626 | ||
d79e621a GD |
3627 | -- If the actual has been marked as requiring a range |
3628 | -- check, then generate it here. | |
3629 | ||
3630 | if Do_Range_Check (Actual) then | |
d79e621a GD |
3631 | Generate_Range_Check |
3632 | (Actual, Etype (Formal), CE_Range_Check_Failed); | |
3633 | end if; | |
3634 | ||
d4817e3f HK |
3635 | -- For access types, the parent formal type and actual type |
3636 | -- differ. | |
3637 | ||
3638 | elsif Is_Access_Type (Formal_Typ) | |
3639 | and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ) | |
70482933 | 3640 | then |
d4817e3f HK |
3641 | if Ekind (Formal) /= E_In_Parameter then |
3642 | Convert (Actual, Parent_Typ); | |
3643 | ||
3644 | elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type | |
3645 | and then Designated_Type (Parent_Typ) /= | |
3646 | Designated_Type (Actual_Typ) | |
3647 | and then not Is_Controlling_Formal (Formal) | |
3648 | then | |
3649 | -- This unchecked conversion is not necessary unless | |
3650 | -- inlining is enabled, because in that case the type | |
3651 | -- mismatch may become visible in the body about to be | |
3652 | -- inlined. | |
3653 | ||
3654 | Rewrite (Actual, | |
3655 | Unchecked_Convert_To (Parent_Typ, | |
3656 | Relocate_Node (Actual))); | |
d4817e3f HK |
3657 | Analyze (Actual); |
3658 | Resolve (Actual, Parent_Typ); | |
3659 | end if; | |
70482933 | 3660 | |
ab01e614 AC |
3661 | -- If there is a change of representation, then generate a |
3662 | -- warning, and do the change of representation. | |
3663 | ||
3664 | elsif not Same_Representation (Formal_Typ, Parent_Typ) then | |
3665 | Error_Msg_N | |
3666 | ("??change of representation required", Actual); | |
3667 | Convert (Actual, Parent_Typ); | |
3668 | ||
d4817e3f HK |
3669 | -- For array and record types, the parent formal type and |
3670 | -- derived formal type have different sizes or pragma Pack | |
3671 | -- status. | |
70482933 | 3672 | |
d4817e3f | 3673 | elsif ((Is_Array_Type (Formal_Typ) |
ab01e614 | 3674 | and then Is_Array_Type (Parent_Typ)) |
d4817e3f HK |
3675 | or else |
3676 | (Is_Record_Type (Formal_Typ) | |
ab01e614 | 3677 | and then Is_Record_Type (Parent_Typ))) |
d4817e3f HK |
3678 | and then |
3679 | (Esize (Formal_Typ) /= Esize (Parent_Typ) | |
ab01e614 AC |
3680 | or else Has_Pragma_Pack (Formal_Typ) /= |
3681 | Has_Pragma_Pack (Parent_Typ)) | |
d4817e3f HK |
3682 | then |
3683 | Convert (Actual, Parent_Typ); | |
70482933 | 3684 | end if; |
70482933 | 3685 | |
d4817e3f HK |
3686 | Next_Actual (Actual); |
3687 | Next_Formal (Formal); | |
3688 | Next_Formal (Parent_Formal); | |
3689 | end loop; | |
3690 | end; | |
70482933 RK |
3691 | end if; |
3692 | ||
3693 | Orig_Subp := Subp; | |
3694 | Subp := Parent_Subp; | |
3695 | end if; | |
3696 | ||
8a36a0cc AC |
3697 | -- Deal with case where call is an explicit dereference |
3698 | ||
6dfc5592 | 3699 | if Nkind (Name (Call_Node)) = N_Explicit_Dereference then |
70482933 RK |
3700 | |
3701 | -- Handle case of access to protected subprogram type | |
3702 | ||
f937473f | 3703 | if Is_Access_Protected_Subprogram_Type |
6dfc5592 | 3704 | (Base_Type (Etype (Prefix (Name (Call_Node))))) |
70482933 | 3705 | then |
b3f48fd4 AC |
3706 | -- If this is a call through an access to protected operation, the |
3707 | -- prefix has the form (object'address, operation'access). Rewrite | |
3708 | -- as a for other protected calls: the object is the 1st parameter | |
3709 | -- of the list of actuals. | |
70482933 RK |
3710 | |
3711 | declare | |
3712 | Call : Node_Id; | |
3713 | Parm : List_Id; | |
3714 | Nam : Node_Id; | |
3715 | Obj : Node_Id; | |
6dfc5592 | 3716 | Ptr : constant Node_Id := Prefix (Name (Call_Node)); |
fbf5a39b AC |
3717 | |
3718 | T : constant Entity_Id := | |
3719 | Equivalent_Type (Base_Type (Etype (Ptr))); | |
3720 | ||
3721 | D_T : constant Entity_Id := | |
3722 | Designated_Type (Base_Type (Etype (Ptr))); | |
70482933 RK |
3723 | |
3724 | begin | |
f44fe430 RD |
3725 | Obj := |
3726 | Make_Selected_Component (Loc, | |
3727 | Prefix => Unchecked_Convert_To (T, Ptr), | |
3728 | Selector_Name => | |
3729 | New_Occurrence_Of (First_Entity (T), Loc)); | |
3730 | ||
3731 | Nam := | |
3732 | Make_Selected_Component (Loc, | |
3733 | Prefix => Unchecked_Convert_To (T, Ptr), | |
3734 | Selector_Name => | |
3735 | New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc)); | |
70482933 | 3736 | |
02822a92 RD |
3737 | Nam := |
3738 | Make_Explicit_Dereference (Loc, | |
3739 | Prefix => Nam); | |
70482933 | 3740 | |
6dfc5592 RD |
3741 | if Present (Parameter_Associations (Call_Node)) then |
3742 | Parm := Parameter_Associations (Call_Node); | |
70482933 RK |
3743 | else |
3744 | Parm := New_List; | |
3745 | end if; | |
3746 | ||
3747 | Prepend (Obj, Parm); | |
3748 | ||
3749 | if Etype (D_T) = Standard_Void_Type then | |
02822a92 RD |
3750 | Call := |
3751 | Make_Procedure_Call_Statement (Loc, | |
3752 | Name => Nam, | |
3753 | Parameter_Associations => Parm); | |
70482933 | 3754 | else |
02822a92 RD |
3755 | Call := |
3756 | Make_Function_Call (Loc, | |
3757 | Name => Nam, | |
3758 | Parameter_Associations => Parm); | |
70482933 RK |
3759 | end if; |
3760 | ||
6dfc5592 | 3761 | Set_First_Named_Actual (Call, First_Named_Actual (Call_Node)); |
70482933 RK |
3762 | Set_Etype (Call, Etype (D_T)); |
3763 | ||
3764 | -- We do not re-analyze the call to avoid infinite recursion. | |
3765 | -- We analyze separately the prefix and the object, and set | |
3766 | -- the checks on the prefix that would otherwise be emitted | |
3767 | -- when resolving a call. | |
3768 | ||
6dfc5592 | 3769 | Rewrite (Call_Node, Call); |
70482933 RK |
3770 | Analyze (Nam); |
3771 | Apply_Access_Check (Nam); | |
3772 | Analyze (Obj); | |
3773 | return; | |
3774 | end; | |
3775 | end if; | |
3776 | end if; | |
3777 | ||
3778 | -- If this is a call to an intrinsic subprogram, then perform the | |
3779 | -- appropriate expansion to the corresponding tree node and we | |
a90bd866 | 3780 | -- are all done (since after that the call is gone). |
70482933 | 3781 | |
98f01d53 AC |
3782 | -- In the case where the intrinsic is to be processed by the back end, |
3783 | -- the call to Expand_Intrinsic_Call will do nothing, which is fine, | |
b3f48fd4 AC |
3784 | -- since the idea in this case is to pass the call unchanged. If the |
3785 | -- intrinsic is an inherited unchecked conversion, and the derived type | |
3786 | -- is the target type of the conversion, we must retain it as the return | |
3787 | -- type of the expression. Otherwise the expansion below, which uses the | |
3788 | -- parent operation, will yield the wrong type. | |
98f01d53 | 3789 | |
70482933 | 3790 | if Is_Intrinsic_Subprogram (Subp) then |
6dfc5592 | 3791 | Expand_Intrinsic_Call (Call_Node, Subp); |
d766cee3 | 3792 | |
6dfc5592 | 3793 | if Nkind (Call_Node) = N_Unchecked_Type_Conversion |
d766cee3 RD |
3794 | and then Parent_Subp /= Orig_Subp |
3795 | and then Etype (Parent_Subp) /= Etype (Orig_Subp) | |
3796 | then | |
6dfc5592 | 3797 | Set_Etype (Call_Node, Etype (Orig_Subp)); |
d766cee3 RD |
3798 | end if; |
3799 | ||
70482933 RK |
3800 | return; |
3801 | end if; | |
3802 | ||
b29def53 AC |
3803 | if Ekind_In (Subp, E_Function, E_Procedure) then |
3804 | ||
26a43556 | 3805 | -- We perform two simple optimization on calls: |
8dbf3473 | 3806 | |
3563739b | 3807 | -- a) replace calls to null procedures unconditionally; |
26a43556 | 3808 | |
3563739b | 3809 | -- b) for To_Address, just do an unchecked conversion. Not only is |
26a43556 AC |
3810 | -- this efficient, but it also avoids order of elaboration problems |
3811 | -- when address clauses are inlined (address expression elaborated | |
3812 | -- at the wrong point). | |
3813 | ||
3814 | -- We perform these optimization regardless of whether we are in the | |
3815 | -- main unit or in a unit in the context of the main unit, to ensure | |
2cbac6c6 | 3816 | -- that tree generated is the same in both cases, for CodePeer use. |
26a43556 AC |
3817 | |
3818 | if Is_RTE (Subp, RE_To_Address) then | |
6dfc5592 | 3819 | Rewrite (Call_Node, |
26a43556 | 3820 | Unchecked_Convert_To |
6dfc5592 | 3821 | (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node)))); |
26a43556 AC |
3822 | return; |
3823 | ||
3824 | elsif Is_Null_Procedure (Subp) then | |
6dfc5592 | 3825 | Rewrite (Call_Node, Make_Null_Statement (Loc)); |
8dbf3473 AC |
3826 | return; |
3827 | end if; | |
3828 | ||
6c26bac2 | 3829 | -- Handle inlining. No action needed if the subprogram is not inlined |
f087ea44 | 3830 | |
6c26bac2 AC |
3831 | if not Is_Inlined (Subp) then |
3832 | null; | |
f087ea44 | 3833 | |
6c26bac2 | 3834 | -- Handle frontend inlining |
84f4072a | 3835 | |
6c26bac2 | 3836 | elsif not Back_End_Inlining then |
a41ea816 | 3837 | Inlined_Subprogram : declare |
fbf5a39b AC |
3838 | Bod : Node_Id; |
3839 | Must_Inline : Boolean := False; | |
3840 | Spec : constant Node_Id := Unit_Declaration_Node (Subp); | |
a41ea816 | 3841 | |
70482933 | 3842 | begin |
2f1b20a9 ES |
3843 | -- Verify that the body to inline has already been seen, and |
3844 | -- that if the body is in the current unit the inlining does | |
3845 | -- not occur earlier. This avoids order-of-elaboration problems | |
3846 | -- in the back end. | |
3847 | ||
3848 | -- This should be documented in sinfo/einfo ??? | |
70482933 | 3849 | |
fbf5a39b AC |
3850 | if No (Spec) |
3851 | or else Nkind (Spec) /= N_Subprogram_Declaration | |
3852 | or else No (Body_To_Inline (Spec)) | |
70482933 | 3853 | then |
fbf5a39b AC |
3854 | Must_Inline := False; |
3855 | ||
26a43556 AC |
3856 | -- If this an inherited function that returns a private type, |
3857 | -- do not inline if the full view is an unconstrained array, | |
3858 | -- because such calls cannot be inlined. | |
5b4994bc AC |
3859 | |
3860 | elsif Present (Orig_Subp) | |
3861 | and then Is_Array_Type (Etype (Orig_Subp)) | |
3862 | and then not Is_Constrained (Etype (Orig_Subp)) | |
3863 | then | |
3864 | Must_Inline := False; | |
3865 | ||
84f4072a | 3866 | elsif In_Unfrozen_Instance (Scope (Subp)) then |
5b4994bc AC |
3867 | Must_Inline := False; |
3868 | ||
fbf5a39b AC |
3869 | else |
3870 | Bod := Body_To_Inline (Spec); | |
3871 | ||
6dfc5592 RD |
3872 | if (In_Extended_Main_Code_Unit (Call_Node) |
3873 | or else In_Extended_Main_Code_Unit (Parent (Call_Node)) | |
ac4d6407 | 3874 | or else Has_Pragma_Inline_Always (Subp)) |
fbf5a39b AC |
3875 | and then (not In_Same_Extended_Unit (Sloc (Bod), Loc) |
3876 | or else | |
3877 | Earlier_In_Extended_Unit (Sloc (Bod), Loc)) | |
3878 | then | |
3879 | Must_Inline := True; | |
3880 | ||
3881 | -- If we are compiling a package body that is not the main | |
3882 | -- unit, it must be for inlining/instantiation purposes, | |
3883 | -- in which case we inline the call to insure that the same | |
3884 | -- temporaries are generated when compiling the body by | |
3885 | -- itself. Otherwise link errors can occur. | |
3886 | ||
2820d220 AC |
3887 | -- If the function being called is itself in the main unit, |
3888 | -- we cannot inline, because there is a risk of double | |
3889 | -- elaboration and/or circularity: the inlining can make | |
3890 | -- visible a private entity in the body of the main unit, | |
3891 | -- that gigi will see before its sees its proper definition. | |
3892 | ||
6dfc5592 | 3893 | elsif not (In_Extended_Main_Code_Unit (Call_Node)) |
fbf5a39b AC |
3894 | and then In_Package_Body |
3895 | then | |
2820d220 | 3896 | Must_Inline := not In_Extended_Main_Source_Unit (Subp); |
fbf5a39b AC |
3897 | end if; |
3898 | end if; | |
3899 | ||
3900 | if Must_Inline then | |
6dfc5592 | 3901 | Expand_Inlined_Call (Call_Node, Subp, Orig_Subp); |
70482933 RK |
3902 | |
3903 | else | |
fbf5a39b | 3904 | -- Let the back end handle it |
70482933 RK |
3905 | |
3906 | Add_Inlined_Body (Subp); | |
3907 | ||
3908 | if Front_End_Inlining | |
3909 | and then Nkind (Spec) = N_Subprogram_Declaration | |
6dfc5592 | 3910 | and then (In_Extended_Main_Code_Unit (Call_Node)) |
70482933 RK |
3911 | and then No (Body_To_Inline (Spec)) |
3912 | and then not Has_Completion (Subp) | |
3913 | and then In_Same_Extended_Unit (Sloc (Spec), Loc) | |
70482933 | 3914 | then |
fbf5a39b | 3915 | Cannot_Inline |
685bc70f AC |
3916 | ("cannot inline& (body not seen yet)?", |
3917 | Call_Node, Subp); | |
70482933 RK |
3918 | end if; |
3919 | end if; | |
a41ea816 | 3920 | end Inlined_Subprogram; |
84f4072a | 3921 | |
6c26bac2 AC |
3922 | -- Back end inlining: let the back end handle it |
3923 | ||
3924 | elsif No (Unit_Declaration_Node (Subp)) | |
ea0c8cfb RD |
3925 | or else Nkind (Unit_Declaration_Node (Subp)) /= |
3926 | N_Subprogram_Declaration | |
3927 | or else No (Body_To_Inline (Unit_Declaration_Node (Subp))) | |
6c26bac2 AC |
3928 | then |
3929 | Add_Inlined_Body (Subp); | |
3930 | Register_Backend_Call (Call_Node); | |
3931 | ||
ea0c8cfb RD |
3932 | -- Frontend expands supported functions returning unconstrained types |
3933 | ||
3934 | else | |
3935 | pragma Assert (Ekind (Subp) = E_Function | |
3936 | and then Returns_Unconstrained_Type (Subp)); | |
84f4072a | 3937 | |
84f4072a JM |
3938 | declare |
3939 | Spec : constant Node_Id := Unit_Declaration_Node (Subp); | |
3940 | ||
3941 | begin | |
ea3a4ad0 | 3942 | if Must_Inline (Subp) then |
84f4072a JM |
3943 | if In_Extended_Main_Code_Unit (Call_Node) |
3944 | and then In_Same_Extended_Unit (Sloc (Spec), Loc) | |
3945 | and then not Has_Completion (Subp) | |
3946 | then | |
3947 | Cannot_Inline | |
3948 | ("cannot inline& (body not seen yet)?", | |
3949 | Call_Node, Subp); | |
3950 | ||
3951 | else | |
3952 | Do_Inline_Always (Subp, Orig_Subp); | |
3953 | end if; | |
ea3a4ad0 JM |
3954 | |
3955 | elsif Optimization_Level > 0 then | |
3956 | Do_Inline (Subp, Orig_Subp); | |
84f4072a JM |
3957 | end if; |
3958 | ||
3959 | -- The call may have been inlined or may have been passed to | |
3960 | -- the backend. No further action needed if it was inlined. | |
3961 | ||
3962 | if Nkind (N) /= N_Function_Call then | |
3963 | return; | |
3964 | end if; | |
3965 | end; | |
70482933 RK |
3966 | end if; |
3967 | end if; | |
3968 | ||
26a43556 AC |
3969 | -- Check for protected subprogram. This is either an intra-object call, |
3970 | -- or a protected function call. Protected procedure calls are rewritten | |
3971 | -- as entry calls and handled accordingly. | |
70482933 | 3972 | |
26a43556 AC |
3973 | -- In Ada 2005, this may be an indirect call to an access parameter that |
3974 | -- is an access_to_subprogram. In that case the anonymous type has a | |
3975 | -- scope that is a protected operation, but the call is a regular one. | |
6f76a257 | 3976 | -- In either case do not expand call if subprogram is eliminated. |
c8ef728f | 3977 | |
70482933 RK |
3978 | Scop := Scope (Subp); |
3979 | ||
6dfc5592 | 3980 | if Nkind (Call_Node) /= N_Entry_Call_Statement |
70482933 | 3981 | and then Is_Protected_Type (Scop) |
c8ef728f | 3982 | and then Ekind (Subp) /= E_Subprogram_Type |
6f76a257 | 3983 | and then not Is_Eliminated (Subp) |
70482933 | 3984 | then |
26a43556 AC |
3985 | -- If the call is an internal one, it is rewritten as a call to the |
3986 | -- corresponding unprotected subprogram. | |
70482933 | 3987 | |
6dfc5592 | 3988 | Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop); |
70482933 RK |
3989 | end if; |
3990 | ||
df3e68b1 HK |
3991 | -- Functions returning controlled objects need special attention. If |
3992 | -- the return type is limited, then the context is initialization and | |
3993 | -- different processing applies. If the call is to a protected function, | |
3994 | -- the expansion above will call Expand_Call recursively. Otherwise the | |
3995 | -- function call is transformed into a temporary which obtains the | |
3996 | -- result from the secondary stack. | |
70482933 | 3997 | |
c768e988 | 3998 | if Needs_Finalization (Etype (Subp)) then |
51245e2d | 3999 | if not Is_Limited_View (Etype (Subp)) |
c768e988 AC |
4000 | and then |
4001 | (No (First_Formal (Subp)) | |
4002 | or else | |
4003 | not Is_Concurrent_Record_Type (Etype (First_Formal (Subp)))) | |
4004 | then | |
6dfc5592 | 4005 | Expand_Ctrl_Function_Call (Call_Node); |
c768e988 AC |
4006 | |
4007 | -- Build-in-place function calls which appear in anonymous contexts | |
4008 | -- need a transient scope to ensure the proper finalization of the | |
4009 | -- intermediate result after its use. | |
4010 | ||
6dfc5592 | 4011 | elsif Is_Build_In_Place_Function_Call (Call_Node) |
d3b00ce3 AC |
4012 | and then |
4013 | Nkind_In (Parent (Call_Node), N_Attribute_Reference, | |
4014 | N_Function_Call, | |
4015 | N_Indexed_Component, | |
4016 | N_Object_Renaming_Declaration, | |
4017 | N_Procedure_Call_Statement, | |
4018 | N_Selected_Component, | |
4019 | N_Slice) | |
c768e988 | 4020 | then |
6dfc5592 | 4021 | Establish_Transient_Scope (Call_Node, Sec_Stack => True); |
c768e988 | 4022 | end if; |
70482933 | 4023 | end if; |
70482933 RK |
4024 | end Expand_Call; |
4025 | ||
df3e68b1 HK |
4026 | ------------------------------- |
4027 | -- Expand_Ctrl_Function_Call -- | |
4028 | ------------------------------- | |
4029 | ||
4030 | procedure Expand_Ctrl_Function_Call (N : Node_Id) is | |
bf561f2b AC |
4031 | function Is_Element_Reference (N : Node_Id) return Boolean; |
4032 | -- Determine whether node N denotes a reference to an Ada 2012 container | |
4033 | -- element. | |
4034 | ||
4035 | -------------------------- | |
4036 | -- Is_Element_Reference -- | |
4037 | -------------------------- | |
4038 | ||
4039 | function Is_Element_Reference (N : Node_Id) return Boolean is | |
4040 | Ref : constant Node_Id := Original_Node (N); | |
4041 | ||
4042 | begin | |
4043 | -- Analysis marks an element reference by setting the generalized | |
4044 | -- indexing attribute of an indexed component before the component | |
4045 | -- is rewritten into a function call. | |
4046 | ||
4047 | return | |
4048 | Nkind (Ref) = N_Indexed_Component | |
4049 | and then Present (Generalized_Indexing (Ref)); | |
4050 | end Is_Element_Reference; | |
4051 | ||
4052 | -- Local variables | |
4053 | ||
4054 | Is_Elem_Ref : constant Boolean := Is_Element_Reference (N); | |
4055 | ||
4056 | -- Start of processing for Expand_Ctrl_Function_Call | |
4057 | ||
df3e68b1 HK |
4058 | begin |
4059 | -- Optimization, if the returned value (which is on the sec-stack) is | |
4060 | -- returned again, no need to copy/readjust/finalize, we can just pass | |
4061 | -- the value thru (see Expand_N_Simple_Return_Statement), and thus no | |
4062 | -- attachment is needed | |
4063 | ||
4064 | if Nkind (Parent (N)) = N_Simple_Return_Statement then | |
4065 | return; | |
4066 | end if; | |
4067 | ||
4068 | -- Resolution is now finished, make sure we don't start analysis again | |
4069 | -- because of the duplication. | |
4070 | ||
4071 | Set_Analyzed (N); | |
4072 | ||
4073 | -- A function which returns a controlled object uses the secondary | |
4074 | -- stack. Rewrite the call into a temporary which obtains the result of | |
4075 | -- the function using 'reference. | |
4076 | ||
4077 | Remove_Side_Effects (N); | |
3cebd1c0 | 4078 | |
bf561f2b AC |
4079 | -- When the temporary function result appears inside a case expression |
4080 | -- or an if expression, its lifetime must be extended to match that of | |
4081 | -- the context. If not, the function result will be finalized too early | |
4082 | -- and the evaluation of the expression could yield incorrect result. An | |
4083 | -- exception to this rule are references to Ada 2012 container elements. | |
4084 | -- Such references must be finalized at the end of each iteration of the | |
4085 | -- related quantified expression, otherwise the container will remain | |
4086 | -- busy. | |
4087 | ||
4088 | if not Is_Elem_Ref | |
4089 | and then Within_Case_Or_If_Expression (N) | |
3cebd1c0 AC |
4090 | and then Nkind (N) = N_Explicit_Dereference |
4091 | then | |
4092 | Set_Is_Processed_Transient (Entity (Prefix (N))); | |
4093 | end if; | |
df3e68b1 HK |
4094 | end Expand_Ctrl_Function_Call; |
4095 | ||
2b3d67a5 AC |
4096 | ---------------------------------------- |
4097 | -- Expand_N_Extended_Return_Statement -- | |
4098 | ---------------------------------------- | |
4099 | ||
4100 | -- If there is a Handled_Statement_Sequence, we rewrite this: | |
4101 | ||
4102 | -- return Result : T := <expression> do | |
4103 | -- <handled_seq_of_stms> | |
4104 | -- end return; | |
4105 | ||
4106 | -- to be: | |
4107 | ||
4108 | -- declare | |
4109 | -- Result : T := <expression>; | |
4110 | -- begin | |
4111 | -- <handled_seq_of_stms> | |
4112 | -- return Result; | |
4113 | -- end; | |
4114 | ||
4115 | -- Otherwise (no Handled_Statement_Sequence), we rewrite this: | |
4116 | ||
4117 | -- return Result : T := <expression>; | |
4118 | ||
4119 | -- to be: | |
4120 | ||
4121 | -- return <expression>; | |
4122 | ||
4123 | -- unless it's build-in-place or there's no <expression>, in which case | |
4124 | -- we generate: | |
4125 | ||
4126 | -- declare | |
4127 | -- Result : T := <expression>; | |
4128 | -- begin | |
4129 | -- return Result; | |
4130 | -- end; | |
4131 | ||
4132 | -- Note that this case could have been written by the user as an extended | |
4133 | -- return statement, or could have been transformed to this from a simple | |
4134 | -- return statement. | |
4135 | ||
4136 | -- That is, we need to have a reified return object if there are statements | |
4137 | -- (which might refer to it) or if we're doing build-in-place (so we can | |
4138 | -- set its address to the final resting place or if there is no expression | |
4139 | -- (in which case default initial values might need to be set). | |
4140 | ||
4141 | procedure Expand_N_Extended_Return_Statement (N : Node_Id) is | |
4142 | Loc : constant Source_Ptr := Sloc (N); | |
4143 | ||
df3e68b1 HK |
4144 | Par_Func : constant Entity_Id := |
4145 | Return_Applies_To (Return_Statement_Entity (N)); | |
1a36a0cd | 4146 | Result_Subt : constant Entity_Id := Etype (Par_Func); |
df3e68b1 HK |
4147 | Ret_Obj_Id : constant Entity_Id := |
4148 | First_Entity (Return_Statement_Entity (N)); | |
4149 | Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id); | |
4150 | ||
4151 | Is_Build_In_Place : constant Boolean := | |
4152 | Is_Build_In_Place_Function (Par_Func); | |
4153 | ||
4154 | Exp : Node_Id; | |
4155 | HSS : Node_Id; | |
4156 | Result : Node_Id; | |
4157 | Return_Stmt : Node_Id; | |
4158 | Stmts : List_Id; | |
4159 | ||
4160 | function Build_Heap_Allocator | |
4161 | (Temp_Id : Entity_Id; | |
4162 | Temp_Typ : Entity_Id; | |
4163 | Func_Id : Entity_Id; | |
4164 | Ret_Typ : Entity_Id; | |
4165 | Alloc_Expr : Node_Id) return Node_Id; | |
4166 | -- Create the statements necessary to allocate a return object on the | |
d3f70b35 AC |
4167 | -- caller's master. The master is available through implicit parameter |
4168 | -- BIPfinalizationmaster. | |
df3e68b1 | 4169 | -- |
d3f70b35 | 4170 | -- if BIPfinalizationmaster /= null then |
df3e68b1 HK |
4171 | -- declare |
4172 | -- type Ptr_Typ is access Ret_Typ; | |
4173 | -- for Ptr_Typ'Storage_Pool use | |
d3f70b35 | 4174 | -- Base_Pool (BIPfinalizationmaster.all).all; |
df3e68b1 HK |
4175 | -- Local : Ptr_Typ; |
4176 | -- | |
4177 | -- begin | |
4178 | -- procedure Allocate (...) is | |
4179 | -- begin | |
d3f70b35 | 4180 | -- System.Storage_Pools.Subpools.Allocate_Any (...); |
df3e68b1 HK |
4181 | -- end Allocate; |
4182 | -- | |
4183 | -- Local := <Alloc_Expr>; | |
4184 | -- Temp_Id := Temp_Typ (Local); | |
4185 | -- end; | |
4186 | -- end if; | |
4187 | -- | |
4188 | -- Temp_Id is the temporary which is used to reference the internally | |
4189 | -- created object in all allocation forms. Temp_Typ is the type of the | |
4190 | -- temporary. Func_Id is the enclosing function. Ret_Typ is the return | |
4191 | -- type of Func_Id. Alloc_Expr is the actual allocator. | |
2b3d67a5 | 4192 | |
2b3d67a5 AC |
4193 | function Move_Activation_Chain return Node_Id; |
4194 | -- Construct a call to System.Tasking.Stages.Move_Activation_Chain | |
4195 | -- with parameters: | |
4196 | -- From current activation chain | |
4197 | -- To activation chain passed in by the caller | |
4198 | -- New_Master master passed in by the caller | |
4199 | ||
df3e68b1 HK |
4200 | -------------------------- |
4201 | -- Build_Heap_Allocator -- | |
4202 | -------------------------- | |
4203 | ||
4204 | function Build_Heap_Allocator | |
4205 | (Temp_Id : Entity_Id; | |
4206 | Temp_Typ : Entity_Id; | |
4207 | Func_Id : Entity_Id; | |
4208 | Ret_Typ : Entity_Id; | |
4209 | Alloc_Expr : Node_Id) return Node_Id | |
4210 | is | |
4211 | begin | |
200b7162 BD |
4212 | pragma Assert (Is_Build_In_Place_Function (Func_Id)); |
4213 | ||
df3e68b1 | 4214 | -- Processing for build-in-place object allocation. This is disabled |
d3f70b35 | 4215 | -- on .NET/JVM because the targets do not support pools. |
df3e68b1 HK |
4216 | |
4217 | if VM_Target = No_VM | |
df3e68b1 HK |
4218 | and then Needs_Finalization (Ret_Typ) |
4219 | then | |
4220 | declare | |
d3f70b35 AC |
4221 | Decls : constant List_Id := New_List; |
4222 | Fin_Mas_Id : constant Entity_Id := | |
4223 | Build_In_Place_Formal | |
4224 | (Func_Id, BIP_Finalization_Master); | |
4225 | Stmts : constant List_Id := New_List; | |
ba759acd AC |
4226 | Desig_Typ : Entity_Id; |
4227 | Local_Id : Entity_Id; | |
4228 | Pool_Id : Entity_Id; | |
4229 | Ptr_Typ : Entity_Id; | |
df3e68b1 HK |
4230 | |
4231 | begin | |
4232 | -- Generate: | |
d3f70b35 | 4233 | -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all; |
df3e68b1 HK |
4234 | |
4235 | Pool_Id := Make_Temporary (Loc, 'P'); | |
4236 | ||
4237 | Append_To (Decls, | |
4238 | Make_Object_Renaming_Declaration (Loc, | |
4239 | Defining_Identifier => Pool_Id, | |
2c1b72d7 | 4240 | Subtype_Mark => |
e4494292 | 4241 | New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc), |
2c1b72d7 | 4242 | Name => |
df3e68b1 HK |
4243 | Make_Explicit_Dereference (Loc, |
4244 | Prefix => | |
4245 | Make_Function_Call (Loc, | |
2c1b72d7 | 4246 | Name => |
e4494292 | 4247 | New_Occurrence_Of (RTE (RE_Base_Pool), Loc), |
df3e68b1 HK |
4248 | Parameter_Associations => New_List ( |
4249 | Make_Explicit_Dereference (Loc, | |
d3f70b35 | 4250 | Prefix => |
e4494292 | 4251 | New_Occurrence_Of (Fin_Mas_Id, Loc))))))); |
df3e68b1 HK |
4252 | |
4253 | -- Create an access type which uses the storage pool of the | |
d3f70b35 AC |
4254 | -- caller's master. This additional type is necessary because |
4255 | -- the finalization master cannot be associated with the type | |
df3e68b1 HK |
4256 | -- of the temporary. Otherwise the secondary stack allocation |
4257 | -- will fail. | |
4258 | ||
ba759acd AC |
4259 | Desig_Typ := Ret_Typ; |
4260 | ||
4261 | -- Ensure that the build-in-place machinery uses a fat pointer | |
4262 | -- when allocating an unconstrained array on the heap. In this | |
4263 | -- case the result object type is a constrained array type even | |
4264 | -- though the function type is unconstrained. | |
4265 | ||
4266 | if Ekind (Desig_Typ) = E_Array_Subtype then | |
4267 | Desig_Typ := Base_Type (Desig_Typ); | |
4268 | end if; | |
4269 | ||
df3e68b1 | 4270 | -- Generate: |
ba759acd | 4271 | -- type Ptr_Typ is access Desig_Typ; |
df3e68b1 HK |
4272 | |
4273 | Ptr_Typ := Make_Temporary (Loc, 'P'); | |
4274 | ||
4275 | Append_To (Decls, | |
4276 | Make_Full_Type_Declaration (Loc, | |
4277 | Defining_Identifier => Ptr_Typ, | |
2c1b72d7 | 4278 | Type_Definition => |
df3e68b1 HK |
4279 | Make_Access_To_Object_Definition (Loc, |
4280 | Subtype_Indication => | |
e4494292 | 4281 | New_Occurrence_Of (Desig_Typ, Loc)))); |
df3e68b1 | 4282 | |
d3f70b35 AC |
4283 | -- Perform minor decoration in order to set the master and the |
4284 | -- storage pool attributes. | |
df3e68b1 HK |
4285 | |
4286 | Set_Ekind (Ptr_Typ, E_Access_Type); | |
d3f70b35 | 4287 | Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id); |
df3e68b1 HK |
4288 | Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id); |
4289 | ||
4290 | -- Create the temporary, generate: | |
df3e68b1 HK |
4291 | -- Local_Id : Ptr_Typ; |
4292 | ||
4293 | Local_Id := Make_Temporary (Loc, 'T'); | |
4294 | ||
4295 | Append_To (Decls, | |
4296 | Make_Object_Declaration (Loc, | |
4297 | Defining_Identifier => Local_Id, | |
2c1b72d7 | 4298 | Object_Definition => |
e4494292 | 4299 | New_Occurrence_Of (Ptr_Typ, Loc))); |
df3e68b1 HK |
4300 | |
4301 | -- Allocate the object, generate: | |
df3e68b1 HK |
4302 | -- Local_Id := <Alloc_Expr>; |
4303 | ||
4304 | Append_To (Stmts, | |
4305 | Make_Assignment_Statement (Loc, | |
e4494292 | 4306 | Name => New_Occurrence_Of (Local_Id, Loc), |
df3e68b1 HK |
4307 | Expression => Alloc_Expr)); |
4308 | ||
4309 | -- Generate: | |
4310 | -- Temp_Id := Temp_Typ (Local_Id); | |
4311 | ||
4312 | Append_To (Stmts, | |
4313 | Make_Assignment_Statement (Loc, | |
e4494292 | 4314 | Name => New_Occurrence_Of (Temp_Id, Loc), |
df3e68b1 HK |
4315 | Expression => |
4316 | Unchecked_Convert_To (Temp_Typ, | |
e4494292 | 4317 | New_Occurrence_Of (Local_Id, Loc)))); |
df3e68b1 HK |
4318 | |
4319 | -- Wrap the allocation in a block. This is further conditioned | |
d3f70b35 AC |
4320 | -- by checking the caller finalization master at runtime. A |
4321 | -- null value indicates a non-existent master, most likely due | |
4322 | -- to a Finalize_Storage_Only allocation. | |
df3e68b1 HK |
4323 | |
4324 | -- Generate: | |
d3f70b35 | 4325 | -- if BIPfinalizationmaster /= null then |
df3e68b1 HK |
4326 | -- declare |
4327 | -- <Decls> | |
4328 | -- begin | |
4329 | -- <Stmts> | |
4330 | -- end; | |
4331 | -- end if; | |
4332 | ||
4333 | return | |
4334 | Make_If_Statement (Loc, | |
2c1b72d7 | 4335 | Condition => |
df3e68b1 | 4336 | Make_Op_Ne (Loc, |
e4494292 | 4337 | Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc), |
2c1b72d7 | 4338 | Right_Opnd => Make_Null (Loc)), |
df3e68b1 HK |
4339 | |
4340 | Then_Statements => New_List ( | |
4341 | Make_Block_Statement (Loc, | |
2c1b72d7 | 4342 | Declarations => Decls, |
df3e68b1 HK |
4343 | Handled_Statement_Sequence => |
4344 | Make_Handled_Sequence_Of_Statements (Loc, | |
4345 | Statements => Stmts)))); | |
4346 | end; | |
4347 | ||
4348 | -- For all other cases, generate: | |
df3e68b1 HK |
4349 | -- Temp_Id := <Alloc_Expr>; |
4350 | ||
4351 | else | |
4352 | return | |
4353 | Make_Assignment_Statement (Loc, | |
e4494292 | 4354 | Name => New_Occurrence_Of (Temp_Id, Loc), |
df3e68b1 HK |
4355 | Expression => Alloc_Expr); |
4356 | end if; | |
4357 | end Build_Heap_Allocator; | |
2b3d67a5 | 4358 | |
2b3d67a5 AC |
4359 | --------------------------- |
4360 | -- Move_Activation_Chain -- | |
4361 | --------------------------- | |
4362 | ||
4363 | function Move_Activation_Chain return Node_Id is | |
2b3d67a5 | 4364 | begin |
2b3d67a5 AC |
4365 | return |
4366 | Make_Procedure_Call_Statement (Loc, | |
2c1b72d7 | 4367 | Name => |
e4494292 | 4368 | New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc), |
0613fb33 AC |
4369 | |
4370 | Parameter_Associations => New_List ( | |
4371 | ||
4372 | -- Source chain | |
4373 | ||
4374 | Make_Attribute_Reference (Loc, | |
4375 | Prefix => Make_Identifier (Loc, Name_uChain), | |
4376 | Attribute_Name => Name_Unrestricted_Access), | |
4377 | ||
4378 | -- Destination chain | |
4379 | ||
e4494292 | 4380 | New_Occurrence_Of |
0613fb33 AC |
4381 | (Build_In_Place_Formal (Par_Func, BIP_Activation_Chain), Loc), |
4382 | ||
4383 | -- New master | |
4384 | ||
e4494292 | 4385 | New_Occurrence_Of |
af89615f | 4386 | (Build_In_Place_Formal (Par_Func, BIP_Task_Master), Loc))); |
2b3d67a5 AC |
4387 | end Move_Activation_Chain; |
4388 | ||
df3e68b1 | 4389 | -- Start of processing for Expand_N_Extended_Return_Statement |
2b3d67a5 | 4390 | |
df3e68b1 | 4391 | begin |
f6f4d8d4 JM |
4392 | -- Given that functionality of interface thunks is simple (just displace |
4393 | -- the pointer to the object) they are always handled by means of | |
4394 | -- simple return statements. | |
4395 | ||
da1c23dd | 4396 | pragma Assert (not Is_Thunk (Current_Scope)); |
f6f4d8d4 | 4397 | |
df3e68b1 HK |
4398 | if Nkind (Ret_Obj_Decl) = N_Object_Declaration then |
4399 | Exp := Expression (Ret_Obj_Decl); | |
4400 | else | |
4401 | Exp := Empty; | |
4402 | end if; | |
2b3d67a5 | 4403 | |
df3e68b1 | 4404 | HSS := Handled_Statement_Sequence (N); |
2b3d67a5 | 4405 | |
df3e68b1 HK |
4406 | -- If the returned object needs finalization actions, the function must |
4407 | -- perform the appropriate cleanup should it fail to return. The state | |
4408 | -- of the function itself is tracked through a flag which is coupled | |
4409 | -- with the scope finalizer. There is one flag per each return object | |
4410 | -- in case of multiple returns. | |
2b3d67a5 | 4411 | |
df3e68b1 HK |
4412 | if Is_Build_In_Place |
4413 | and then Needs_Finalization (Etype (Ret_Obj_Id)) | |
4414 | then | |
4415 | declare | |
4416 | Flag_Decl : Node_Id; | |
4417 | Flag_Id : Entity_Id; | |
4418 | Func_Bod : Node_Id; | |
2b3d67a5 | 4419 | |
df3e68b1 HK |
4420 | begin |
4421 | -- Recover the function body | |
2b3d67a5 | 4422 | |
df3e68b1 | 4423 | Func_Bod := Unit_Declaration_Node (Par_Func); |
0613fb33 | 4424 | |
df3e68b1 HK |
4425 | if Nkind (Func_Bod) = N_Subprogram_Declaration then |
4426 | Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod))); | |
4427 | end if; | |
2b3d67a5 | 4428 | |
df3e68b1 | 4429 | -- Create a flag to track the function state |
2b3d67a5 | 4430 | |
df3e68b1 | 4431 | Flag_Id := Make_Temporary (Loc, 'F'); |
3cebd1c0 | 4432 | Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id); |
2b3d67a5 | 4433 | |
df3e68b1 HK |
4434 | -- Insert the flag at the beginning of the function declarations, |
4435 | -- generate: | |
4436 | -- Fnn : Boolean := False; | |
2b3d67a5 | 4437 | |
df3e68b1 HK |
4438 | Flag_Decl := |
4439 | Make_Object_Declaration (Loc, | |
4440 | Defining_Identifier => Flag_Id, | |
2c1b72d7 | 4441 | Object_Definition => |
e4494292 RD |
4442 | New_Occurrence_Of (Standard_Boolean, Loc), |
4443 | Expression => | |
4444 | New_Occurrence_Of (Standard_False, Loc)); | |
2b3d67a5 | 4445 | |
df3e68b1 HK |
4446 | Prepend_To (Declarations (Func_Bod), Flag_Decl); |
4447 | Analyze (Flag_Decl); | |
4448 | end; | |
4449 | end if; | |
2b3d67a5 AC |
4450 | |
4451 | -- Build a simple_return_statement that returns the return object when | |
4452 | -- there is a statement sequence, or no expression, or the result will | |
4453 | -- be built in place. Note however that we currently do this for all | |
4454 | -- composite cases, even though nonlimited composite results are not yet | |
4455 | -- built in place (though we plan to do so eventually). | |
4456 | ||
df3e68b1 | 4457 | if Present (HSS) |
1a36a0cd | 4458 | or else Is_Composite_Type (Result_Subt) |
2b3d67a5 AC |
4459 | or else No (Exp) |
4460 | then | |
df3e68b1 HK |
4461 | if No (HSS) then |
4462 | Stmts := New_List; | |
2b3d67a5 AC |
4463 | |
4464 | -- If the extended return has a handled statement sequence, then wrap | |
4465 | -- it in a block and use the block as the first statement. | |
4466 | ||
4467 | else | |
df3e68b1 HK |
4468 | Stmts := New_List ( |
4469 | Make_Block_Statement (Loc, | |
2c1b72d7 | 4470 | Declarations => New_List, |
df3e68b1 | 4471 | Handled_Statement_Sequence => HSS)); |
2b3d67a5 AC |
4472 | end if; |
4473 | ||
df3e68b1 HK |
4474 | -- If the result type contains tasks, we call Move_Activation_Chain. |
4475 | -- Later, the cleanup code will call Complete_Master, which will | |
4476 | -- terminate any unactivated tasks belonging to the return statement | |
4477 | -- master. But Move_Activation_Chain updates their master to be that | |
4478 | -- of the caller, so they will not be terminated unless the return | |
4479 | -- statement completes unsuccessfully due to exception, abort, goto, | |
4480 | -- or exit. As a formality, we test whether the function requires the | |
4481 | -- result to be built in place, though that's necessarily true for | |
4482 | -- the case of result types with task parts. | |
2b3d67a5 AC |
4483 | |
4484 | if Is_Build_In_Place | |
1a36a0cd | 4485 | and then Has_Task (Result_Subt) |
2b3d67a5 | 4486 | then |
4a1bfefb AC |
4487 | -- The return expression is an aggregate for a complex type which |
4488 | -- contains tasks. This particular case is left unexpanded since | |
4489 | -- the regular expansion would insert all temporaries and | |
4490 | -- initialization code in the wrong block. | |
4491 | ||
4492 | if Nkind (Exp) = N_Aggregate then | |
4493 | Expand_N_Aggregate (Exp); | |
4494 | end if; | |
4495 | ||
1a36a0cd AC |
4496 | -- Do not move the activation chain if the return object does not |
4497 | -- contain tasks. | |
4498 | ||
4499 | if Has_Task (Etype (Ret_Obj_Id)) then | |
4500 | Append_To (Stmts, Move_Activation_Chain); | |
4501 | end if; | |
2b3d67a5 AC |
4502 | end if; |
4503 | ||
df3e68b1 HK |
4504 | -- Update the state of the function right before the object is |
4505 | -- returned. | |
4506 | ||
4507 | if Is_Build_In_Place | |
4508 | and then Needs_Finalization (Etype (Ret_Obj_Id)) | |
4509 | then | |
4510 | declare | |
35a1c212 | 4511 | Flag_Id : constant Entity_Id := |
3cebd1c0 | 4512 | Status_Flag_Or_Transient_Decl (Ret_Obj_Id); |
4fdebd93 | 4513 | |
df3e68b1 HK |
4514 | begin |
4515 | -- Generate: | |
4516 | -- Fnn := True; | |
4517 | ||
4518 | Append_To (Stmts, | |
4519 | Make_Assignment_Statement (Loc, | |
e4494292 RD |
4520 | Name => New_Occurrence_Of (Flag_Id, Loc), |
4521 | Expression => New_Occurrence_Of (Standard_True, Loc))); | |
df3e68b1 | 4522 | end; |
2b3d67a5 AC |
4523 | end if; |
4524 | ||
4525 | -- Build a simple_return_statement that returns the return object | |
4526 | ||
df3e68b1 | 4527 | Return_Stmt := |
2b3d67a5 | 4528 | Make_Simple_Return_Statement (Loc, |
2c1b72d7 | 4529 | Expression => New_Occurrence_Of (Ret_Obj_Id, Loc)); |
df3e68b1 | 4530 | Append_To (Stmts, Return_Stmt); |
2b3d67a5 | 4531 | |
df3e68b1 | 4532 | HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts); |
2b3d67a5 AC |
4533 | end if; |
4534 | ||
df3e68b1 | 4535 | -- Case where we build a return statement block |
2b3d67a5 | 4536 | |
df3e68b1 | 4537 | if Present (HSS) then |
2b3d67a5 AC |
4538 | Result := |
4539 | Make_Block_Statement (Loc, | |
2c1b72d7 | 4540 | Declarations => Return_Object_Declarations (N), |
df3e68b1 | 4541 | Handled_Statement_Sequence => HSS); |
2b3d67a5 AC |
4542 | |
4543 | -- We set the entity of the new block statement to be that of the | |
4544 | -- return statement. This is necessary so that various fields, such | |
4545 | -- as Finalization_Chain_Entity carry over from the return statement | |
4546 | -- to the block. Note that this block is unusual, in that its entity | |
4547 | -- is an E_Return_Statement rather than an E_Block. | |
4548 | ||
4549 | Set_Identifier | |
4550 | (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc)); | |
4551 | ||
54bf19e4 AC |
4552 | -- If the object decl was already rewritten as a renaming, then we |
4553 | -- don't want to do the object allocation and transformation of of | |
4554 | -- the return object declaration to a renaming. This case occurs | |
2b3d67a5 | 4555 | -- when the return object is initialized by a call to another |
54bf19e4 AC |
4556 | -- build-in-place function, and that function is responsible for |
4557 | -- the allocation of the return object. | |
2b3d67a5 AC |
4558 | |
4559 | if Is_Build_In_Place | |
df3e68b1 | 4560 | and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration |
2b3d67a5 | 4561 | then |
df3e68b1 HK |
4562 | pragma Assert |
4563 | (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration | |
2c1b72d7 AC |
4564 | and then Is_Build_In_Place_Function_Call |
4565 | (Expression (Original_Node (Ret_Obj_Decl)))); | |
df3e68b1 HK |
4566 | |
4567 | -- Return the build-in-place result by reference | |
2b3d67a5 | 4568 | |
df3e68b1 | 4569 | Set_By_Ref (Return_Stmt); |
2b3d67a5 AC |
4570 | |
4571 | elsif Is_Build_In_Place then | |
4572 | ||
4573 | -- Locate the implicit access parameter associated with the | |
4574 | -- caller-supplied return object and convert the return | |
4575 | -- statement's return object declaration to a renaming of a | |
4576 | -- dereference of the access parameter. If the return object's | |
4577 | -- declaration includes an expression that has not already been | |
4578 | -- expanded as separate assignments, then add an assignment | |
4579 | -- statement to ensure the return object gets initialized. | |
4580 | ||
df3e68b1 HK |
4581 | -- declare |
4582 | -- Result : T [:= <expression>]; | |
4583 | -- begin | |
4584 | -- ... | |
2b3d67a5 AC |
4585 | |
4586 | -- is converted to | |
4587 | ||
df3e68b1 HK |
4588 | -- declare |
4589 | -- Result : T renames FuncRA.all; | |
4590 | -- [Result := <expression;] | |
4591 | -- begin | |
4592 | -- ... | |
2b3d67a5 AC |
4593 | |
4594 | declare | |
4595 | Return_Obj_Id : constant Entity_Id := | |
df3e68b1 | 4596 | Defining_Identifier (Ret_Obj_Decl); |
2b3d67a5 AC |
4597 | Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id); |
4598 | Return_Obj_Expr : constant Node_Id := | |
df3e68b1 | 4599 | Expression (Ret_Obj_Decl); |
2b3d67a5 AC |
4600 | Constr_Result : constant Boolean := |
4601 | Is_Constrained (Result_Subt); | |
4602 | Obj_Alloc_Formal : Entity_Id; | |
4603 | Object_Access : Entity_Id; | |
4604 | Obj_Acc_Deref : Node_Id; | |
4605 | Init_Assignment : Node_Id := Empty; | |
4606 | ||
4607 | begin | |
4608 | -- Build-in-place results must be returned by reference | |
4609 | ||
df3e68b1 | 4610 | Set_By_Ref (Return_Stmt); |
2b3d67a5 AC |
4611 | |
4612 | -- Retrieve the implicit access parameter passed by the caller | |
4613 | ||
4614 | Object_Access := | |
df3e68b1 | 4615 | Build_In_Place_Formal (Par_Func, BIP_Object_Access); |
2b3d67a5 AC |
4616 | |
4617 | -- If the return object's declaration includes an expression | |
4618 | -- and the declaration isn't marked as No_Initialization, then | |
4619 | -- we need to generate an assignment to the object and insert | |
4620 | -- it after the declaration before rewriting it as a renaming | |
4621 | -- (otherwise we'll lose the initialization). The case where | |
4622 | -- the result type is an interface (or class-wide interface) | |
4623 | -- is also excluded because the context of the function call | |
4624 | -- must be unconstrained, so the initialization will always | |
4625 | -- be done as part of an allocator evaluation (storage pool | |
4626 | -- or secondary stack), never to a constrained target object | |
4627 | -- passed in by the caller. Besides the assignment being | |
4628 | -- unneeded in this case, it avoids problems with trying to | |
4629 | -- generate a dispatching assignment when the return expression | |
4630 | -- is a nonlimited descendant of a limited interface (the | |
4631 | -- interface has no assignment operation). | |
4632 | ||
4633 | if Present (Return_Obj_Expr) | |
df3e68b1 | 4634 | and then not No_Initialization (Ret_Obj_Decl) |
2b3d67a5 AC |
4635 | and then not Is_Interface (Return_Obj_Typ) |
4636 | then | |
4637 | Init_Assignment := | |
4638 | Make_Assignment_Statement (Loc, | |
e4494292 | 4639 | Name => New_Occurrence_Of (Return_Obj_Id, Loc), |
2c1b72d7 | 4640 | Expression => Relocate_Node (Return_Obj_Expr)); |
df3e68b1 | 4641 | |
2b3d67a5 AC |
4642 | Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id)); |
4643 | Set_Assignment_OK (Name (Init_Assignment)); | |
4644 | Set_No_Ctrl_Actions (Init_Assignment); | |
4645 | ||
4646 | Set_Parent (Name (Init_Assignment), Init_Assignment); | |
4647 | Set_Parent (Expression (Init_Assignment), Init_Assignment); | |
4648 | ||
df3e68b1 | 4649 | Set_Expression (Ret_Obj_Decl, Empty); |
2b3d67a5 AC |
4650 | |
4651 | if Is_Class_Wide_Type (Etype (Return_Obj_Id)) | |
4652 | and then not Is_Class_Wide_Type | |
4653 | (Etype (Expression (Init_Assignment))) | |
4654 | then | |
4655 | Rewrite (Expression (Init_Assignment), | |
4656 | Make_Type_Conversion (Loc, | |
4657 | Subtype_Mark => | |
df3e68b1 | 4658 | New_Occurrence_Of (Etype (Return_Obj_Id), Loc), |
2c1b72d7 | 4659 | Expression => |
2b3d67a5 AC |
4660 | Relocate_Node (Expression (Init_Assignment)))); |
4661 | end if; | |
4662 | ||
4663 | -- In the case of functions where the calling context can | |
4664 | -- determine the form of allocation needed, initialization | |
4665 | -- is done with each part of the if statement that handles | |
4666 | -- the different forms of allocation (this is true for | |
4667 | -- unconstrained and tagged result subtypes). | |
4668 | ||
4669 | if Constr_Result | |
4670 | and then not Is_Tagged_Type (Underlying_Type (Result_Subt)) | |
4671 | then | |
df3e68b1 | 4672 | Insert_After (Ret_Obj_Decl, Init_Assignment); |
2b3d67a5 AC |
4673 | end if; |
4674 | end if; | |
4675 | ||
4676 | -- When the function's subtype is unconstrained, a run-time | |
4677 | -- test is needed to determine the form of allocation to use | |
4678 | -- for the return object. The function has an implicit formal | |
4679 | -- parameter indicating this. If the BIP_Alloc_Form formal has | |
4680 | -- the value one, then the caller has passed access to an | |
4681 | -- existing object for use as the return object. If the value | |
4682 | -- is two, then the return object must be allocated on the | |
4683 | -- secondary stack. Otherwise, the object must be allocated in | |
4684 | -- a storage pool (currently only supported for the global | |
4685 | -- heap, user-defined storage pools TBD ???). We generate an | |
4686 | -- if statement to test the implicit allocation formal and | |
4687 | -- initialize a local access value appropriately, creating | |
4688 | -- allocators in the secondary stack and global heap cases. | |
4689 | -- The special formal also exists and must be tested when the | |
4690 | -- function has a tagged result, even when the result subtype | |
4691 | -- is constrained, because in general such functions can be | |
4692 | -- called in dispatching contexts and must be handled similarly | |
4693 | -- to functions with a class-wide result. | |
4694 | ||
4695 | if not Constr_Result | |
4696 | or else Is_Tagged_Type (Underlying_Type (Result_Subt)) | |
4697 | then | |
4698 | Obj_Alloc_Formal := | |
df3e68b1 | 4699 | Build_In_Place_Formal (Par_Func, BIP_Alloc_Form); |
2b3d67a5 AC |
4700 | |
4701 | declare | |
8417f4b2 AC |
4702 | Pool_Id : constant Entity_Id := |
4703 | Make_Temporary (Loc, 'P'); | |
2b3d67a5 AC |
4704 | Alloc_Obj_Id : Entity_Id; |
4705 | Alloc_Obj_Decl : Node_Id; | |
4706 | Alloc_If_Stmt : Node_Id; | |
200b7162 | 4707 | Heap_Allocator : Node_Id; |
200b7162 BD |
4708 | Pool_Decl : Node_Id; |
4709 | Pool_Allocator : Node_Id; | |
8417f4b2 AC |
4710 | Ptr_Type_Decl : Node_Id; |
4711 | Ref_Type : Entity_Id; | |
4712 | SS_Allocator : Node_Id; | |
2b3d67a5 AC |
4713 | |
4714 | begin | |
4715 | -- Reuse the itype created for the function's implicit | |
4716 | -- access formal. This avoids the need to create a new | |
4717 | -- access type here, plus it allows assigning the access | |
4718 | -- formal directly without applying a conversion. | |
4719 | ||
df3e68b1 | 4720 | -- Ref_Type := Etype (Object_Access); |
2b3d67a5 AC |
4721 | |
4722 | -- Create an access type designating the function's | |
4723 | -- result subtype. | |
4724 | ||
4725 | Ref_Type := Make_Temporary (Loc, 'A'); | |
4726 | ||
4727 | Ptr_Type_Decl := | |
4728 | Make_Full_Type_Declaration (Loc, | |
4729 | Defining_Identifier => Ref_Type, | |
2c1b72d7 | 4730 | Type_Definition => |
2b3d67a5 | 4731 | Make_Access_To_Object_Definition (Loc, |
2c1b72d7 | 4732 | All_Present => True, |
2b3d67a5 | 4733 | Subtype_Indication => |
e4494292 | 4734 | New_Occurrence_Of (Return_Obj_Typ, Loc))); |
2b3d67a5 | 4735 | |
df3e68b1 | 4736 | Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl); |
2b3d67a5 AC |
4737 | |
4738 | -- Create an access object that will be initialized to an | |
4739 | -- access value denoting the return object, either coming | |
4740 | -- from an implicit access value passed in by the caller | |
4741 | -- or from the result of an allocator. | |
4742 | ||
4743 | Alloc_Obj_Id := Make_Temporary (Loc, 'R'); | |
4744 | Set_Etype (Alloc_Obj_Id, Ref_Type); | |
4745 | ||
4746 | Alloc_Obj_Decl := | |
4747 | Make_Object_Declaration (Loc, | |
4748 | Defining_Identifier => Alloc_Obj_Id, | |
2c1b72d7 | 4749 | Object_Definition => |
e4494292 | 4750 | New_Occurrence_Of (Ref_Type, Loc)); |
2b3d67a5 | 4751 | |
df3e68b1 | 4752 | Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl); |
2b3d67a5 AC |
4753 | |
4754 | -- Create allocators for both the secondary stack and | |
4755 | -- global heap. If there's an initialization expression, | |
4756 | -- then create these as initialized allocators. | |
4757 | ||
4758 | if Present (Return_Obj_Expr) | |
df3e68b1 | 4759 | and then not No_Initialization (Ret_Obj_Decl) |
2b3d67a5 AC |
4760 | then |
4761 | -- Always use the type of the expression for the | |
4762 | -- qualified expression, rather than the result type. | |
4763 | -- In general we cannot always use the result type | |
4764 | -- for the allocator, because the expression might be | |
4765 | -- of a specific type, such as in the case of an | |
4766 | -- aggregate or even a nonlimited object when the | |
4767 | -- result type is a limited class-wide interface type. | |
4768 | ||
4769 | Heap_Allocator := | |
4770 | Make_Allocator (Loc, | |
4771 | Expression => | |
4772 | Make_Qualified_Expression (Loc, | |
4773 | Subtype_Mark => | |
e4494292 | 4774 | New_Occurrence_Of |
2b3d67a5 | 4775 | (Etype (Return_Obj_Expr), Loc), |
2c1b72d7 | 4776 | Expression => |
2b3d67a5 AC |
4777 | New_Copy_Tree (Return_Obj_Expr))); |
4778 | ||
4779 | else | |
4780 | -- If the function returns a class-wide type we cannot | |
4781 | -- use the return type for the allocator. Instead we | |
4782 | -- use the type of the expression, which must be an | |
4783 | -- aggregate of a definite type. | |
4784 | ||
4785 | if Is_Class_Wide_Type (Return_Obj_Typ) then | |
4786 | Heap_Allocator := | |
4787 | Make_Allocator (Loc, | |
4788 | Expression => | |
e4494292 | 4789 | New_Occurrence_Of |
2b3d67a5 AC |
4790 | (Etype (Return_Obj_Expr), Loc)); |
4791 | else | |
4792 | Heap_Allocator := | |
4793 | Make_Allocator (Loc, | |
4794 | Expression => | |
e4494292 | 4795 | New_Occurrence_Of (Return_Obj_Typ, Loc)); |
2b3d67a5 AC |
4796 | end if; |
4797 | ||
4798 | -- If the object requires default initialization then | |
4799 | -- that will happen later following the elaboration of | |
4800 | -- the object renaming. If we don't turn it off here | |
4801 | -- then the object will be default initialized twice. | |
4802 | ||
4803 | Set_No_Initialization (Heap_Allocator); | |
4804 | end if; | |
4805 | ||
200b7162 | 4806 | -- The Pool_Allocator is just like the Heap_Allocator, |
8417f4b2 AC |
4807 | -- except we set Storage_Pool and Procedure_To_Call so |
4808 | -- it will use the user-defined storage pool. | |
200b7162 BD |
4809 | |
4810 | Pool_Allocator := New_Copy_Tree (Heap_Allocator); | |
8417f4b2 AC |
4811 | |
4812 | -- Do not generate the renaming of the build-in-place | |
3e452820 AC |
4813 | -- pool parameter on .NET/JVM/ZFP because the parameter |
4814 | -- is not created in the first place. | |
8417f4b2 | 4815 | |
ea10ca9c AC |
4816 | if VM_Target = No_VM |
4817 | and then RTE_Available (RE_Root_Storage_Pool_Ptr) | |
3e452820 | 4818 | then |
8417f4b2 AC |
4819 | Pool_Decl := |
4820 | Make_Object_Renaming_Declaration (Loc, | |
4821 | Defining_Identifier => Pool_Id, | |
4822 | Subtype_Mark => | |
e4494292 | 4823 | New_Occurrence_Of |
8417f4b2 AC |
4824 | (RTE (RE_Root_Storage_Pool), Loc), |
4825 | Name => | |
4826 | Make_Explicit_Dereference (Loc, | |
e4494292 | 4827 | New_Occurrence_Of |
8417f4b2 AC |
4828 | (Build_In_Place_Formal |
4829 | (Par_Func, BIP_Storage_Pool), Loc))); | |
4830 | Set_Storage_Pool (Pool_Allocator, Pool_Id); | |
4831 | Set_Procedure_To_Call | |
4832 | (Pool_Allocator, RTE (RE_Allocate_Any)); | |
4833 | else | |
4834 | Pool_Decl := Make_Null_Statement (Loc); | |
4835 | end if; | |
200b7162 | 4836 | |
2b3d67a5 AC |
4837 | -- If the No_Allocators restriction is active, then only |
4838 | -- an allocator for secondary stack allocation is needed. | |
4839 | -- It's OK for such allocators to have Comes_From_Source | |
4840 | -- set to False, because gigi knows not to flag them as | |
4841 | -- being a violation of No_Implicit_Heap_Allocations. | |
4842 | ||
4843 | if Restriction_Active (No_Allocators) then | |
4844 | SS_Allocator := Heap_Allocator; | |
4845 | Heap_Allocator := Make_Null (Loc); | |
200b7162 | 4846 | Pool_Allocator := Make_Null (Loc); |
2b3d67a5 | 4847 | |
200b7162 BD |
4848 | -- Otherwise the heap and pool allocators may be needed, |
4849 | -- so we make another allocator for secondary stack | |
4850 | -- allocation. | |
2b3d67a5 AC |
4851 | |
4852 | else | |
4853 | SS_Allocator := New_Copy_Tree (Heap_Allocator); | |
4854 | ||
3e7302c3 | 4855 | -- The heap and pool allocators are marked as |
200b7162 BD |
4856 | -- Comes_From_Source since they correspond to an |
4857 | -- explicit user-written allocator (that is, it will | |
4858 | -- only be executed on behalf of callers that call the | |
3e7302c3 AC |
4859 | -- function as initialization for such an allocator). |
4860 | -- Prevents errors when No_Implicit_Heap_Allocations | |
4861 | -- is in force. | |
2b3d67a5 AC |
4862 | |
4863 | Set_Comes_From_Source (Heap_Allocator, True); | |
200b7162 | 4864 | Set_Comes_From_Source (Pool_Allocator, True); |
2b3d67a5 AC |
4865 | end if; |
4866 | ||
4867 | -- The allocator is returned on the secondary stack. We | |
4868 | -- don't do this on VM targets, since the SS is not used. | |
4869 | ||
4870 | if VM_Target = No_VM then | |
4871 | Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool)); | |
4872 | Set_Procedure_To_Call | |
4873 | (SS_Allocator, RTE (RE_SS_Allocate)); | |
4874 | ||
4875 | -- The allocator is returned on the secondary stack, | |
4876 | -- so indicate that the function return, as well as | |
4877 | -- the block that encloses the allocator, must not | |
54bf19e4 AC |
4878 | -- release it. The flags must be set now because |
4879 | -- the decision to use the secondary stack is done | |
4880 | -- very late in the course of expanding the return | |
2b3d67a5 AC |
4881 | -- statement, past the point where these flags are |
4882 | -- normally set. | |
4883 | ||
df3e68b1 | 4884 | Set_Sec_Stack_Needed_For_Return (Par_Func); |
2b3d67a5 AC |
4885 | Set_Sec_Stack_Needed_For_Return |
4886 | (Return_Statement_Entity (N)); | |
df3e68b1 | 4887 | Set_Uses_Sec_Stack (Par_Func); |
2b3d67a5 AC |
4888 | Set_Uses_Sec_Stack (Return_Statement_Entity (N)); |
4889 | end if; | |
4890 | ||
4891 | -- Create an if statement to test the BIP_Alloc_Form | |
4892 | -- formal and initialize the access object to either the | |
200b7162 BD |
4893 | -- BIP_Object_Access formal (BIP_Alloc_Form = |
4894 | -- Caller_Allocation), the result of allocating the | |
4895 | -- object in the secondary stack (BIP_Alloc_Form = | |
4896 | -- Secondary_Stack), or else an allocator to create the | |
4897 | -- return object in the heap or user-defined pool | |
4898 | -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool). | |
2b3d67a5 AC |
4899 | |
4900 | -- ??? An unchecked type conversion must be made in the | |
4901 | -- case of assigning the access object formal to the | |
4902 | -- local access object, because a normal conversion would | |
4903 | -- be illegal in some cases (such as converting access- | |
4904 | -- to-unconstrained to access-to-constrained), but the | |
4905 | -- the unchecked conversion will presumably fail to work | |
4906 | -- right in just such cases. It's not clear at all how to | |
4907 | -- handle this. ??? | |
4908 | ||
4909 | Alloc_If_Stmt := | |
4910 | Make_If_Statement (Loc, | |
df3e68b1 | 4911 | Condition => |
2b3d67a5 | 4912 | Make_Op_Eq (Loc, |
2c1b72d7 | 4913 | Left_Opnd => |
e4494292 | 4914 | New_Occurrence_Of (Obj_Alloc_Formal, Loc), |
2b3d67a5 AC |
4915 | Right_Opnd => |
4916 | Make_Integer_Literal (Loc, | |
4917 | UI_From_Int (BIP_Allocation_Form'Pos | |
4918 | (Caller_Allocation)))), | |
df3e68b1 HK |
4919 | |
4920 | Then_Statements => New_List ( | |
4921 | Make_Assignment_Statement (Loc, | |
2c1b72d7 | 4922 | Name => |
e4494292 | 4923 | New_Occurrence_Of (Alloc_Obj_Id, Loc), |
df3e68b1 HK |
4924 | Expression => |
4925 | Make_Unchecked_Type_Conversion (Loc, | |
4926 | Subtype_Mark => | |
e4494292 | 4927 | New_Occurrence_Of (Ref_Type, Loc), |
2c1b72d7 | 4928 | Expression => |
e4494292 | 4929 | New_Occurrence_Of (Object_Access, Loc)))), |
df3e68b1 HK |
4930 | |
4931 | Elsif_Parts => New_List ( | |
4932 | Make_Elsif_Part (Loc, | |
4933 | Condition => | |
4934 | Make_Op_Eq (Loc, | |
2c1b72d7 | 4935 | Left_Opnd => |
e4494292 | 4936 | New_Occurrence_Of (Obj_Alloc_Formal, Loc), |
df3e68b1 HK |
4937 | Right_Opnd => |
4938 | Make_Integer_Literal (Loc, | |
4939 | UI_From_Int (BIP_Allocation_Form'Pos | |
2b3d67a5 | 4940 | (Secondary_Stack)))), |
df3e68b1 HK |
4941 | |
4942 | Then_Statements => New_List ( | |
4943 | Make_Assignment_Statement (Loc, | |
2c1b72d7 | 4944 | Name => |
e4494292 | 4945 | New_Occurrence_Of (Alloc_Obj_Id, Loc), |
200b7162 BD |
4946 | Expression => SS_Allocator))), |
4947 | ||
4948 | Make_Elsif_Part (Loc, | |
4949 | Condition => | |
4950 | Make_Op_Eq (Loc, | |
4951 | Left_Opnd => | |
e4494292 | 4952 | New_Occurrence_Of (Obj_Alloc_Formal, Loc), |
200b7162 BD |
4953 | Right_Opnd => |
4954 | Make_Integer_Literal (Loc, | |
4955 | UI_From_Int (BIP_Allocation_Form'Pos | |
4956 | (Global_Heap)))), | |
4957 | ||
4958 | Then_Statements => New_List ( | |
4959 | Build_Heap_Allocator | |
4960 | (Temp_Id => Alloc_Obj_Id, | |
4961 | Temp_Typ => Ref_Type, | |
4962 | Func_Id => Par_Func, | |
4963 | Ret_Typ => Return_Obj_Typ, | |
4964 | Alloc_Expr => Heap_Allocator)))), | |
df3e68b1 HK |
4965 | |
4966 | Else_Statements => New_List ( | |
200b7162 | 4967 | Pool_Decl, |
df3e68b1 HK |
4968 | Build_Heap_Allocator |
4969 | (Temp_Id => Alloc_Obj_Id, | |
4970 | Temp_Typ => Ref_Type, | |
4971 | Func_Id => Par_Func, | |
4972 | Ret_Typ => Return_Obj_Typ, | |
200b7162 | 4973 | Alloc_Expr => Pool_Allocator))); |
2b3d67a5 AC |
4974 | |
4975 | -- If a separate initialization assignment was created | |
4976 | -- earlier, append that following the assignment of the | |
4977 | -- implicit access formal to the access object, to ensure | |
54bf19e4 AC |
4978 | -- that the return object is initialized in that case. In |
4979 | -- this situation, the target of the assignment must be | |
4980 | -- rewritten to denote a dereference of the access to the | |
4981 | -- return object passed in by the caller. | |
2b3d67a5 AC |
4982 | |
4983 | if Present (Init_Assignment) then | |
4984 | Rewrite (Name (Init_Assignment), | |
4985 | Make_Explicit_Dereference (Loc, | |
e4494292 | 4986 | Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc))); |
df3e68b1 | 4987 | |
2b3d67a5 AC |
4988 | Set_Etype |
4989 | (Name (Init_Assignment), Etype (Return_Obj_Id)); | |
4990 | ||
4991 | Append_To | |
2c1b72d7 | 4992 | (Then_Statements (Alloc_If_Stmt), Init_Assignment); |
2b3d67a5 AC |
4993 | end if; |
4994 | ||
df3e68b1 | 4995 | Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt); |
2b3d67a5 AC |
4996 | |
4997 | -- Remember the local access object for use in the | |
4998 | -- dereference of the renaming created below. | |
4999 | ||
5000 | Object_Access := Alloc_Obj_Id; | |
5001 | end; | |
5002 | end if; | |
5003 | ||
5004 | -- Replace the return object declaration with a renaming of a | |
5005 | -- dereference of the access value designating the return | |
5006 | -- object. | |
5007 | ||
5008 | Obj_Acc_Deref := | |
5009 | Make_Explicit_Dereference (Loc, | |
e4494292 | 5010 | Prefix => New_Occurrence_Of (Object_Access, Loc)); |
2b3d67a5 | 5011 | |
df3e68b1 | 5012 | Rewrite (Ret_Obj_Decl, |
2b3d67a5 AC |
5013 | Make_Object_Renaming_Declaration (Loc, |
5014 | Defining_Identifier => Return_Obj_Id, | |
2c1b72d7 AC |
5015 | Access_Definition => Empty, |
5016 | Subtype_Mark => | |
df3e68b1 | 5017 | New_Occurrence_Of (Return_Obj_Typ, Loc), |
2c1b72d7 | 5018 | Name => Obj_Acc_Deref)); |
2b3d67a5 AC |
5019 | |
5020 | Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref); | |
5021 | end; | |
5022 | end if; | |
5023 | ||
5024 | -- Case where we do not build a block | |
5025 | ||
5026 | else | |
df3e68b1 HK |
5027 | -- We're about to drop Return_Object_Declarations on the floor, so |
5028 | -- we need to insert it, in case it got expanded into useful code. | |
2b3d67a5 AC |
5029 | -- Remove side effects from expression, which may be duplicated in |
5030 | -- subsequent checks (see Expand_Simple_Function_Return). | |
5031 | ||
df3e68b1 | 5032 | Insert_List_Before (N, Return_Object_Declarations (N)); |
2b3d67a5 AC |
5033 | Remove_Side_Effects (Exp); |
5034 | ||
5035 | -- Build simple_return_statement that returns the expression directly | |
5036 | ||
df3e68b1 HK |
5037 | Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp); |
5038 | Result := Return_Stmt; | |
2b3d67a5 AC |
5039 | end if; |
5040 | ||
5041 | -- Set the flag to prevent infinite recursion | |
5042 | ||
df3e68b1 | 5043 | Set_Comes_From_Extended_Return_Statement (Return_Stmt); |
2b3d67a5 AC |
5044 | |
5045 | Rewrite (N, Result); | |
5046 | Analyze (N); | |
5047 | end Expand_N_Extended_Return_Statement; | |
5048 | ||
70482933 RK |
5049 | ---------------------------- |
5050 | -- Expand_N_Function_Call -- | |
5051 | ---------------------------- | |
5052 | ||
5053 | procedure Expand_N_Function_Call (N : Node_Id) is | |
70482933 | 5054 | begin |
ac4d6407 | 5055 | Expand_Call (N); |
70482933 RK |
5056 | end Expand_N_Function_Call; |
5057 | ||
5058 | --------------------------------------- | |
5059 | -- Expand_N_Procedure_Call_Statement -- | |
5060 | --------------------------------------- | |
5061 | ||
5062 | procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is | |
5063 | begin | |
5064 | Expand_Call (N); | |
5065 | end Expand_N_Procedure_Call_Statement; | |
5066 | ||
2b3d67a5 AC |
5067 | -------------------------------------- |
5068 | -- Expand_N_Simple_Return_Statement -- | |
5069 | -------------------------------------- | |
5070 | ||
5071 | procedure Expand_N_Simple_Return_Statement (N : Node_Id) is | |
5072 | begin | |
5073 | -- Defend against previous errors (i.e. the return statement calls a | |
5074 | -- function that is not available in configurable runtime). | |
5075 | ||
5076 | if Present (Expression (N)) | |
5077 | and then Nkind (Expression (N)) = N_Empty | |
5078 | then | |
ee2ba856 | 5079 | Check_Error_Detected; |
2b3d67a5 AC |
5080 | return; |
5081 | end if; | |
5082 | ||
5083 | -- Distinguish the function and non-function cases: | |
5084 | ||
5085 | case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is | |
5086 | ||
5087 | when E_Function | | |
5088 | E_Generic_Function => | |
5089 | Expand_Simple_Function_Return (N); | |
5090 | ||
5091 | when E_Procedure | | |
5092 | E_Generic_Procedure | | |
5093 | E_Entry | | |
5094 | E_Entry_Family | | |
5095 | E_Return_Statement => | |
5096 | Expand_Non_Function_Return (N); | |
5097 | ||
5098 | when others => | |
5099 | raise Program_Error; | |
5100 | end case; | |
5101 | ||
5102 | exception | |
5103 | when RE_Not_Available => | |
5104 | return; | |
5105 | end Expand_N_Simple_Return_Statement; | |
5106 | ||
70482933 RK |
5107 | ------------------------------ |
5108 | -- Expand_N_Subprogram_Body -- | |
5109 | ------------------------------ | |
5110 | ||
4a3b249c RD |
5111 | -- Add poll call if ATC polling is enabled, unless the body will be inlined |
5112 | -- by the back-end. | |
70482933 | 5113 | |
7888a6ae | 5114 | -- Add dummy push/pop label nodes at start and end to clear any local |
4a3b249c | 5115 | -- exception indications if local-exception-to-goto optimization is active. |
7888a6ae | 5116 | |
f44fe430 RD |
5117 | -- Add return statement if last statement in body is not a return statement |
5118 | -- (this makes things easier on Gigi which does not want to have to handle | |
5119 | -- a missing return). | |
70482933 RK |
5120 | |
5121 | -- Add call to Activate_Tasks if body is a task activator | |
5122 | ||
5123 | -- Deal with possible detection of infinite recursion | |
5124 | ||
5125 | -- Eliminate body completely if convention stubbed | |
5126 | ||
5127 | -- Encode entity names within body, since we will not need to reference | |
5128 | -- these entities any longer in the front end. | |
5129 | ||
5130 | -- Initialize scalar out parameters if Initialize/Normalize_Scalars | |
5131 | ||
c9a4817d | 5132 | -- Reset Pure indication if any parameter has root type System.Address |
199c6a10 AC |
5133 | -- or has any parameters of limited types, where limited means that the |
5134 | -- run-time view is limited (i.e. the full type is limited). | |
c9a4817d | 5135 | |
12e0c41c AC |
5136 | -- Wrap thread body |
5137 | ||
70482933 RK |
5138 | procedure Expand_N_Subprogram_Body (N : Node_Id) is |
5139 | Loc : constant Source_Ptr := Sloc (N); | |
5140 | H : constant Node_Id := Handled_Statement_Sequence (N); | |
c9a4817d | 5141 | Body_Id : Entity_Id; |
70482933 | 5142 | Except_H : Node_Id; |
70482933 | 5143 | L : List_Id; |
70f91180 | 5144 | Spec_Id : Entity_Id; |
70482933 RK |
5145 | |
5146 | procedure Add_Return (S : List_Id); | |
5147 | -- Append a return statement to the statement sequence S if the last | |
5148 | -- statement is not already a return or a goto statement. Note that | |
4a3b249c RD |
5149 | -- the latter test is not critical, it does not matter if we add a few |
5150 | -- extra returns, since they get eliminated anyway later on. | |
70482933 RK |
5151 | |
5152 | ---------------- | |
5153 | -- Add_Return -- | |
5154 | ---------------- | |
5155 | ||
5156 | procedure Add_Return (S : List_Id) is | |
7888a6ae GD |
5157 | Last_Stm : Node_Id; |
5158 | Loc : Source_Ptr; | |
12e0c41c AC |
5159 | |
5160 | begin | |
7888a6ae GD |
5161 | -- Get last statement, ignoring any Pop_xxx_Label nodes, which are |
5162 | -- not relevant in this context since they are not executable. | |
12e0c41c | 5163 | |
7888a6ae GD |
5164 | Last_Stm := Last (S); |
5165 | while Nkind (Last_Stm) in N_Pop_xxx_Label loop | |
5166 | Prev (Last_Stm); | |
5167 | end loop; | |
12e0c41c | 5168 | |
7888a6ae | 5169 | -- Now insert return unless last statement is a transfer |
12e0c41c | 5170 | |
7888a6ae | 5171 | if not Is_Transfer (Last_Stm) then |
12e0c41c | 5172 | |
7888a6ae GD |
5173 | -- The source location for the return is the end label of the |
5174 | -- procedure if present. Otherwise use the sloc of the last | |
5175 | -- statement in the list. If the list comes from a generated | |
5176 | -- exception handler and we are not debugging generated code, | |
5177 | -- all the statements within the handler are made invisible | |
5178 | -- to the debugger. | |
12e0c41c | 5179 | |
7888a6ae GD |
5180 | if Nkind (Parent (S)) = N_Exception_Handler |
5181 | and then not Comes_From_Source (Parent (S)) | |
5182 | then | |
5183 | Loc := Sloc (Last_Stm); | |
7888a6ae GD |
5184 | elsif Present (End_Label (H)) then |
5185 | Loc := Sloc (End_Label (H)); | |
7888a6ae GD |
5186 | else |
5187 | Loc := Sloc (Last_Stm); | |
5188 | end if; | |
12e0c41c | 5189 | |
5334d18f BD |
5190 | declare |
5191 | Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc); | |
5192 | ||
5193 | begin | |
4a3b249c RD |
5194 | -- Append return statement, and set analyzed manually. We can't |
5195 | -- call Analyze on this return since the scope is wrong. | |
5334d18f BD |
5196 | |
5197 | -- Note: it almost works to push the scope and then do the | |
4a3b249c | 5198 | -- Analyze call, but something goes wrong in some weird cases |
5334d18f BD |
5199 | -- and it is not worth worrying about ??? |
5200 | ||
5201 | Append_To (S, Rtn); | |
5202 | Set_Analyzed (Rtn); | |
5203 | ||
5204 | -- Call _Postconditions procedure if appropriate. We need to | |
5205 | -- do this explicitly because we did not analyze the generated | |
5206 | -- return statement above, so the call did not get inserted. | |
5207 | ||
5208 | if Ekind (Spec_Id) = E_Procedure | |
5209 | and then Has_Postconditions (Spec_Id) | |
5210 | then | |
5211 | pragma Assert (Present (Postcondition_Proc (Spec_Id))); | |
5212 | Insert_Action (Rtn, | |
5213 | Make_Procedure_Call_Statement (Loc, | |
5214 | Name => | |
e4494292 RD |
5215 | New_Occurrence_Of |
5216 | (Postcondition_Proc (Spec_Id), Loc))); | |
5334d18f BD |
5217 | end if; |
5218 | end; | |
12e0c41c | 5219 | end if; |
7888a6ae | 5220 | end Add_Return; |
12e0c41c | 5221 | |
70482933 RK |
5222 | -- Start of processing for Expand_N_Subprogram_Body |
5223 | ||
5224 | begin | |
4a3b249c RD |
5225 | -- Set L to either the list of declarations if present, or to the list |
5226 | -- of statements if no declarations are present. This is used to insert | |
5227 | -- new stuff at the start. | |
70482933 RK |
5228 | |
5229 | if Is_Non_Empty_List (Declarations (N)) then | |
5230 | L := Declarations (N); | |
5231 | else | |
7888a6ae GD |
5232 | L := Statements (H); |
5233 | end if; | |
5234 | ||
5235 | -- If local-exception-to-goto optimization active, insert dummy push | |
1adaea16 AC |
5236 | -- statements at start, and dummy pop statements at end, but inhibit |
5237 | -- this if we have No_Exception_Handlers, since they are useless and | |
5238 | -- intefere with analysis, e.g. by codepeer. | |
7888a6ae GD |
5239 | |
5240 | if (Debug_Flag_Dot_G | |
5241 | or else Restriction_Active (No_Exception_Propagation)) | |
1adaea16 AC |
5242 | and then not Restriction_Active (No_Exception_Handlers) |
5243 | and then not CodePeer_Mode | |
7888a6ae GD |
5244 | and then Is_Non_Empty_List (L) |
5245 | then | |
5246 | declare | |
5247 | FS : constant Node_Id := First (L); | |
5248 | FL : constant Source_Ptr := Sloc (FS); | |
5249 | LS : Node_Id; | |
5250 | LL : Source_Ptr; | |
5251 | ||
5252 | begin | |
5253 | -- LS points to either last statement, if statements are present | |
5254 | -- or to the last declaration if there are no statements present. | |
5255 | -- It is the node after which the pop's are generated. | |
5256 | ||
5257 | if Is_Non_Empty_List (Statements (H)) then | |
5258 | LS := Last (Statements (H)); | |
5259 | else | |
5260 | LS := Last (L); | |
5261 | end if; | |
5262 | ||
5263 | LL := Sloc (LS); | |
5264 | ||
5265 | Insert_List_Before_And_Analyze (FS, New_List ( | |
5266 | Make_Push_Constraint_Error_Label (FL), | |
5267 | Make_Push_Program_Error_Label (FL), | |
5268 | Make_Push_Storage_Error_Label (FL))); | |
5269 | ||
5270 | Insert_List_After_And_Analyze (LS, New_List ( | |
5271 | Make_Pop_Constraint_Error_Label (LL), | |
5272 | Make_Pop_Program_Error_Label (LL), | |
5273 | Make_Pop_Storage_Error_Label (LL))); | |
5274 | end; | |
70482933 RK |
5275 | end if; |
5276 | ||
70482933 RK |
5277 | -- Find entity for subprogram |
5278 | ||
c9a4817d RD |
5279 | Body_Id := Defining_Entity (N); |
5280 | ||
70482933 RK |
5281 | if Present (Corresponding_Spec (N)) then |
5282 | Spec_Id := Corresponding_Spec (N); | |
5283 | else | |
c9a4817d RD |
5284 | Spec_Id := Body_Id; |
5285 | end if; | |
5286 | ||
7888a6ae GD |
5287 | -- Need poll on entry to subprogram if polling enabled. We only do this |
5288 | -- for non-empty subprograms, since it does not seem necessary to poll | |
4a3b249c | 5289 | -- for a dummy null subprogram. |
c885d7a1 AC |
5290 | |
5291 | if Is_Non_Empty_List (L) then | |
4a3b249c RD |
5292 | |
5293 | -- Do not add a polling call if the subprogram is to be inlined by | |
5294 | -- the back-end, to avoid repeated calls with multiple inlinings. | |
5295 | ||
c885d7a1 AC |
5296 | if Is_Inlined (Spec_Id) |
5297 | and then Front_End_Inlining | |
5298 | and then Optimization_Level > 1 | |
5299 | then | |
5300 | null; | |
5301 | else | |
5302 | Generate_Poll_Call (First (L)); | |
5303 | end if; | |
5304 | end if; | |
5305 | ||
4a3b249c RD |
5306 | -- If this is a Pure function which has any parameters whose root type |
5307 | -- is System.Address, reset the Pure indication, since it will likely | |
5308 | -- cause incorrect code to be generated as the parameter is probably | |
5309 | -- a pointer, and the fact that the same pointer is passed does not mean | |
5310 | -- that the same value is being referenced. | |
91b1417d AC |
5311 | |
5312 | -- Note that if the programmer gave an explicit Pure_Function pragma, | |
5313 | -- then we believe the programmer, and leave the subprogram Pure. | |
5314 | ||
4a3b249c RD |
5315 | -- This code should probably be at the freeze point, so that it happens |
5316 | -- even on a -gnatc (or more importantly -gnatt) compile, so that the | |
5317 | -- semantic tree has Is_Pure set properly ??? | |
c9a4817d RD |
5318 | |
5319 | if Is_Pure (Spec_Id) | |
5320 | and then Is_Subprogram (Spec_Id) | |
5321 | and then not Has_Pragma_Pure_Function (Spec_Id) | |
5322 | then | |
5323 | declare | |
2f1b20a9 | 5324 | F : Entity_Id; |
c9a4817d RD |
5325 | |
5326 | begin | |
2f1b20a9 | 5327 | F := First_Formal (Spec_Id); |
c9a4817d | 5328 | while Present (F) loop |
e5dc610e | 5329 | if Is_Descendent_Of_Address (Etype (F)) |
199c6a10 AC |
5330 | |
5331 | -- Note that this test is being made in the body of the | |
5332 | -- subprogram, not the spec, so we are testing the full | |
5333 | -- type for being limited here, as required. | |
5334 | ||
e5dc610e AC |
5335 | or else Is_Limited_Type (Etype (F)) |
5336 | then | |
c9a4817d RD |
5337 | Set_Is_Pure (Spec_Id, False); |
5338 | ||
5339 | if Spec_Id /= Body_Id then | |
5340 | Set_Is_Pure (Body_Id, False); | |
5341 | end if; | |
5342 | ||
5343 | exit; | |
5344 | end if; | |
5345 | ||
5346 | Next_Formal (F); | |
5347 | end loop; | |
5348 | end; | |
70482933 RK |
5349 | end if; |
5350 | ||
5351 | -- Initialize any scalar OUT args if Initialize/Normalize_Scalars | |
5352 | ||
5353 | if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then | |
5354 | declare | |
2f1b20a9 | 5355 | F : Entity_Id; |
05c064c1 | 5356 | A : Node_Id; |
70482933 RK |
5357 | |
5358 | begin | |
70482933 RK |
5359 | -- Loop through formals |
5360 | ||
2f1b20a9 | 5361 | F := First_Formal (Spec_Id); |
70482933 RK |
5362 | while Present (F) loop |
5363 | if Is_Scalar_Type (Etype (F)) | |
5364 | and then Ekind (F) = E_Out_Parameter | |
5365 | then | |
70f91180 RD |
5366 | Check_Restriction (No_Default_Initialization, F); |
5367 | ||
02822a92 RD |
5368 | -- Insert the initialization. We turn off validity checks |
5369 | -- for this assignment, since we do not want any check on | |
5370 | -- the initial value itself (which may well be invalid). | |
05c064c1 | 5371 | -- Predicate checks are disabled as well (RM 6.4.1 (13/3)) |
02822a92 | 5372 | |
05c064c1 | 5373 | A := Make_Assignment_Statement (Loc, |
02822a92 | 5374 | Name => New_Occurrence_Of (F, Loc), |
05c064c1 AC |
5375 | Expression => Get_Simple_Init_Val (Etype (F), N)); |
5376 | Set_Suppress_Assignment_Checks (A); | |
5377 | ||
5378 | Insert_Before_And_Analyze (First (L), | |
5379 | A, Suppress => Validity_Check); | |
70482933 RK |
5380 | end if; |
5381 | ||
5382 | Next_Formal (F); | |
5383 | end loop; | |
70482933 RK |
5384 | end; |
5385 | end if; | |
5386 | ||
5387 | -- Clear out statement list for stubbed procedure | |
5388 | ||
5389 | if Present (Corresponding_Spec (N)) then | |
5390 | Set_Elaboration_Flag (N, Spec_Id); | |
5391 | ||
5392 | if Convention (Spec_Id) = Convention_Stubbed | |
5393 | or else Is_Eliminated (Spec_Id) | |
5394 | then | |
5395 | Set_Declarations (N, Empty_List); | |
5396 | Set_Handled_Statement_Sequence (N, | |
5397 | Make_Handled_Sequence_Of_Statements (Loc, | |
2c1b72d7 | 5398 | Statements => New_List (Make_Null_Statement (Loc)))); |
70482933 RK |
5399 | return; |
5400 | end if; | |
5401 | end if; | |
5402 | ||
70f91180 RD |
5403 | -- Create a set of discriminals for the next protected subprogram body |
5404 | ||
5405 | if Is_List_Member (N) | |
5406 | and then Present (Parent (List_Containing (N))) | |
5407 | and then Nkind (Parent (List_Containing (N))) = N_Protected_Body | |
5408 | and then Present (Next_Protected_Operation (N)) | |
5409 | then | |
5410 | Set_Discriminals (Parent (Base_Type (Scope (Spec_Id)))); | |
5411 | end if; | |
5412 | ||
4a3b249c RD |
5413 | -- Returns_By_Ref flag is normally set when the subprogram is frozen but |
5414 | -- subprograms with no specs are not frozen. | |
70482933 RK |
5415 | |
5416 | declare | |
5417 | Typ : constant Entity_Id := Etype (Spec_Id); | |
5418 | Utyp : constant Entity_Id := Underlying_Type (Typ); | |
5419 | ||
5420 | begin | |
5421 | if not Acts_As_Spec (N) | |
5422 | and then Nkind (Parent (Parent (Spec_Id))) /= | |
5423 | N_Subprogram_Body_Stub | |
5424 | then | |
5425 | null; | |
5426 | ||
51245e2d | 5427 | elsif Is_Limited_View (Typ) then |
70482933 RK |
5428 | Set_Returns_By_Ref (Spec_Id); |
5429 | ||
048e5cef | 5430 | elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then |
70482933 RK |
5431 | Set_Returns_By_Ref (Spec_Id); |
5432 | end if; | |
5433 | end; | |
5434 | ||
4a3b249c RD |
5435 | -- For a procedure, we add a return for all possible syntactic ends of |
5436 | -- the subprogram. | |
70482933 | 5437 | |
b29def53 | 5438 | if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then |
70482933 RK |
5439 | Add_Return (Statements (H)); |
5440 | ||
5441 | if Present (Exception_Handlers (H)) then | |
5442 | Except_H := First_Non_Pragma (Exception_Handlers (H)); | |
70482933 RK |
5443 | while Present (Except_H) loop |
5444 | Add_Return (Statements (Except_H)); | |
5445 | Next_Non_Pragma (Except_H); | |
5446 | end loop; | |
5447 | end if; | |
5448 | ||
98f01d53 AC |
5449 | -- For a function, we must deal with the case where there is at least |
5450 | -- one missing return. What we do is to wrap the entire body of the | |
5451 | -- function in a block: | |
70482933 RK |
5452 | |
5453 | -- begin | |
5454 | -- ... | |
5455 | -- end; | |
5456 | ||
5457 | -- becomes | |
5458 | ||
5459 | -- begin | |
5460 | -- begin | |
5461 | -- ... | |
5462 | -- end; | |
5463 | ||
5464 | -- raise Program_Error; | |
5465 | -- end; | |
5466 | ||
4a3b249c RD |
5467 | -- This approach is necessary because the raise must be signalled to the |
5468 | -- caller, not handled by any local handler (RM 6.4(11)). | |
70482933 | 5469 | |
4a3b249c RD |
5470 | -- Note: we do not need to analyze the constructed sequence here, since |
5471 | -- it has no handler, and an attempt to analyze the handled statement | |
5472 | -- sequence twice is risky in various ways (e.g. the issue of expanding | |
5473 | -- cleanup actions twice). | |
70482933 RK |
5474 | |
5475 | elsif Has_Missing_Return (Spec_Id) then | |
5476 | declare | |
5477 | Hloc : constant Source_Ptr := Sloc (H); | |
5478 | Blok : constant Node_Id := | |
5479 | Make_Block_Statement (Hloc, | |
5480 | Handled_Statement_Sequence => H); | |
5481 | Rais : constant Node_Id := | |
07fc65c4 GB |
5482 | Make_Raise_Program_Error (Hloc, |
5483 | Reason => PE_Missing_Return); | |
70482933 RK |
5484 | |
5485 | begin | |
5486 | Set_Handled_Statement_Sequence (N, | |
5487 | Make_Handled_Sequence_Of_Statements (Hloc, | |
5488 | Statements => New_List (Blok, Rais))); | |
5489 | ||
7888a6ae | 5490 | Push_Scope (Spec_Id); |
70482933 RK |
5491 | Analyze (Blok); |
5492 | Analyze (Rais); | |
5493 | Pop_Scope; | |
5494 | end; | |
5495 | end if; | |
5496 | ||
70482933 RK |
5497 | -- If subprogram contains a parameterless recursive call, then we may |
5498 | -- have an infinite recursion, so see if we can generate code to check | |
5499 | -- for this possibility if storage checks are not suppressed. | |
5500 | ||
5501 | if Ekind (Spec_Id) = E_Procedure | |
5502 | and then Has_Recursive_Call (Spec_Id) | |
5503 | and then not Storage_Checks_Suppressed (Spec_Id) | |
5504 | then | |
5505 | Detect_Infinite_Recursion (N, Spec_Id); | |
5506 | end if; | |
5507 | ||
70482933 RK |
5508 | -- Set to encode entity names in package body before gigi is called |
5509 | ||
5510 | Qualify_Entity_Names (N); | |
5511 | end Expand_N_Subprogram_Body; | |
5512 | ||
5513 | ----------------------------------- | |
5514 | -- Expand_N_Subprogram_Body_Stub -- | |
5515 | ----------------------------------- | |
5516 | ||
5517 | procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is | |
5518 | begin | |
5519 | if Present (Corresponding_Body (N)) then | |
5520 | Expand_N_Subprogram_Body ( | |
5521 | Unit_Declaration_Node (Corresponding_Body (N))); | |
5522 | end if; | |
70482933 RK |
5523 | end Expand_N_Subprogram_Body_Stub; |
5524 | ||
5525 | ------------------------------------- | |
5526 | -- Expand_N_Subprogram_Declaration -- | |
5527 | ------------------------------------- | |
5528 | ||
70482933 RK |
5529 | -- If the declaration appears within a protected body, it is a private |
5530 | -- operation of the protected type. We must create the corresponding | |
5531 | -- protected subprogram an associated formals. For a normal protected | |
5532 | -- operation, this is done when expanding the protected type declaration. | |
5533 | ||
758c442c GD |
5534 | -- If the declaration is for a null procedure, emit null body |
5535 | ||
70482933 | 5536 | procedure Expand_N_Subprogram_Declaration (N : Node_Id) is |
fbf5a39b AC |
5537 | Loc : constant Source_Ptr := Sloc (N); |
5538 | Subp : constant Entity_Id := Defining_Entity (N); | |
5539 | Scop : constant Entity_Id := Scope (Subp); | |
5540 | Prot_Decl : Node_Id; | |
5541 | Prot_Bod : Node_Id; | |
5542 | Prot_Id : Entity_Id; | |
70482933 RK |
5543 | |
5544 | begin | |
2ba431e5 YM |
5545 | -- In SPARK, subprogram declarations are only allowed in package |
5546 | -- specifications. | |
7ff2d234 | 5547 | |
fe5d3068 YM |
5548 | if Nkind (Parent (N)) /= N_Package_Specification then |
5549 | if Nkind (Parent (N)) = N_Compilation_Unit then | |
2ba431e5 | 5550 | Check_SPARK_Restriction |
fe5d3068 YM |
5551 | ("subprogram declaration is not a library item", N); |
5552 | ||
5553 | elsif Present (Next (N)) | |
7ff2d234 AC |
5554 | and then Nkind (Next (N)) = N_Pragma |
5555 | and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import | |
5556 | then | |
2ba431e5 | 5557 | -- In SPARK, subprogram declarations are also permitted in |
7ff2d234 AC |
5558 | -- declarative parts when immediately followed by a corresponding |
5559 | -- pragma Import. We only check here that there is some pragma | |
5560 | -- Import. | |
5561 | ||
5562 | null; | |
5563 | else | |
2ba431e5 | 5564 | Check_SPARK_Restriction |
fe5d3068 | 5565 | ("subprogram declaration is not allowed here", N); |
7ff2d234 AC |
5566 | end if; |
5567 | end if; | |
5568 | ||
2f1b20a9 ES |
5569 | -- Deal with case of protected subprogram. Do not generate protected |
5570 | -- operation if operation is flagged as eliminated. | |
70482933 RK |
5571 | |
5572 | if Is_List_Member (N) | |
5573 | and then Present (Parent (List_Containing (N))) | |
5574 | and then Nkind (Parent (List_Containing (N))) = N_Protected_Body | |
5575 | and then Is_Protected_Type (Scop) | |
5576 | then | |
6871ba5f AC |
5577 | if No (Protected_Body_Subprogram (Subp)) |
5578 | and then not Is_Eliminated (Subp) | |
5579 | then | |
fbf5a39b | 5580 | Prot_Decl := |
70482933 RK |
5581 | Make_Subprogram_Declaration (Loc, |
5582 | Specification => | |
5583 | Build_Protected_Sub_Specification | |
2f1b20a9 | 5584 | (N, Scop, Unprotected_Mode)); |
70482933 RK |
5585 | |
5586 | -- The protected subprogram is declared outside of the protected | |
5587 | -- body. Given that the body has frozen all entities so far, we | |
fbf5a39b | 5588 | -- analyze the subprogram and perform freezing actions explicitly. |
19590d70 GD |
5589 | -- including the generation of an explicit freeze node, to ensure |
5590 | -- that gigi has the proper order of elaboration. | |
fbf5a39b AC |
5591 | -- If the body is a subunit, the insertion point is before the |
5592 | -- stub in the parent. | |
70482933 RK |
5593 | |
5594 | Prot_Bod := Parent (List_Containing (N)); | |
5595 | ||
5596 | if Nkind (Parent (Prot_Bod)) = N_Subunit then | |
5597 | Prot_Bod := Corresponding_Stub (Parent (Prot_Bod)); | |
5598 | end if; | |
5599 | ||
fbf5a39b AC |
5600 | Insert_Before (Prot_Bod, Prot_Decl); |
5601 | Prot_Id := Defining_Unit_Name (Specification (Prot_Decl)); | |
19590d70 | 5602 | Set_Has_Delayed_Freeze (Prot_Id); |
70482933 | 5603 | |
7888a6ae | 5604 | Push_Scope (Scope (Scop)); |
fbf5a39b | 5605 | Analyze (Prot_Decl); |
6b958cec | 5606 | Freeze_Before (N, Prot_Id); |
fbf5a39b | 5607 | Set_Protected_Body_Subprogram (Subp, Prot_Id); |
47bfea3a AC |
5608 | |
5609 | -- Create protected operation as well. Even though the operation | |
5610 | -- is only accessible within the body, it is possible to make it | |
5611 | -- available outside of the protected object by using 'Access to | |
3d923671 | 5612 | -- provide a callback, so build protected version in all cases. |
47bfea3a AC |
5613 | |
5614 | Prot_Decl := | |
3d923671 AC |
5615 | Make_Subprogram_Declaration (Loc, |
5616 | Specification => | |
5617 | Build_Protected_Sub_Specification (N, Scop, Protected_Mode)); | |
47bfea3a AC |
5618 | Insert_Before (Prot_Bod, Prot_Decl); |
5619 | Analyze (Prot_Decl); | |
5620 | ||
70482933 RK |
5621 | Pop_Scope; |
5622 | end if; | |
758c442c | 5623 | |
54bf19e4 AC |
5624 | -- Ada 2005 (AI-348): Generate body for a null procedure. In most |
5625 | -- cases this is superfluous because calls to it will be automatically | |
5626 | -- inlined, but we definitely need the body if preconditions for the | |
5627 | -- procedure are present. | |
02822a92 | 5628 | |
758c442c GD |
5629 | elsif Nkind (Specification (N)) = N_Procedure_Specification |
5630 | and then Null_Present (Specification (N)) | |
5631 | then | |
5632 | declare | |
e1f3cb58 | 5633 | Bod : constant Node_Id := Body_To_Inline (N); |
d6533e74 | 5634 | |
758c442c | 5635 | begin |
e1f3cb58 AC |
5636 | Set_Has_Completion (Subp, False); |
5637 | Append_Freeze_Action (Subp, Bod); | |
c73ae90f | 5638 | |
e1f3cb58 AC |
5639 | -- The body now contains raise statements, so calls to it will |
5640 | -- not be inlined. | |
c73ae90f | 5641 | |
e1f3cb58 | 5642 | Set_Is_Inlined (Subp, False); |
758c442c | 5643 | end; |
70482933 RK |
5644 | end if; |
5645 | end Expand_N_Subprogram_Declaration; | |
5646 | ||
2b3d67a5 AC |
5647 | -------------------------------- |
5648 | -- Expand_Non_Function_Return -- | |
5649 | -------------------------------- | |
5650 | ||
5651 | procedure Expand_Non_Function_Return (N : Node_Id) is | |
5652 | pragma Assert (No (Expression (N))); | |
5653 | ||
5654 | Loc : constant Source_Ptr := Sloc (N); | |
5655 | Scope_Id : Entity_Id := | |
5656 | Return_Applies_To (Return_Statement_Entity (N)); | |
5657 | Kind : constant Entity_Kind := Ekind (Scope_Id); | |
5658 | Call : Node_Id; | |
5659 | Acc_Stat : Node_Id; | |
5660 | Goto_Stat : Node_Id; | |
5661 | Lab_Node : Node_Id; | |
5662 | ||
5663 | begin | |
5664 | -- Call _Postconditions procedure if procedure with active | |
54bf19e4 AC |
5665 | -- postconditions. Here, we use the Postcondition_Proc attribute, |
5666 | -- which is needed for implicitly-generated returns. Functions | |
5667 | -- never have implicitly-generated returns, and there's no | |
5668 | -- room for Postcondition_Proc in E_Function, so we look up the | |
5669 | -- identifier Name_uPostconditions for function returns (see | |
2b3d67a5 AC |
5670 | -- Expand_Simple_Function_Return). |
5671 | ||
5672 | if Ekind (Scope_Id) = E_Procedure | |
5673 | and then Has_Postconditions (Scope_Id) | |
5674 | then | |
5675 | pragma Assert (Present (Postcondition_Proc (Scope_Id))); | |
5676 | Insert_Action (N, | |
5677 | Make_Procedure_Call_Statement (Loc, | |
e4494292 | 5678 | Name => New_Occurrence_Of (Postcondition_Proc (Scope_Id), Loc))); |
2b3d67a5 AC |
5679 | end if; |
5680 | ||
5681 | -- If it is a return from a procedure do no extra steps | |
5682 | ||
5683 | if Kind = E_Procedure or else Kind = E_Generic_Procedure then | |
5684 | return; | |
5685 | ||
5686 | -- If it is a nested return within an extended one, replace it with a | |
5687 | -- return of the previously declared return object. | |
5688 | ||
5689 | elsif Kind = E_Return_Statement then | |
5690 | Rewrite (N, | |
5691 | Make_Simple_Return_Statement (Loc, | |
5692 | Expression => | |
5693 | New_Occurrence_Of (First_Entity (Scope_Id), Loc))); | |
5694 | Set_Comes_From_Extended_Return_Statement (N); | |
5695 | Set_Return_Statement_Entity (N, Scope_Id); | |
5696 | Expand_Simple_Function_Return (N); | |
5697 | return; | |
5698 | end if; | |
5699 | ||
5700 | pragma Assert (Is_Entry (Scope_Id)); | |
5701 | ||
5702 | -- Look at the enclosing block to see whether the return is from an | |
5703 | -- accept statement or an entry body. | |
5704 | ||
5705 | for J in reverse 0 .. Scope_Stack.Last loop | |
5706 | Scope_Id := Scope_Stack.Table (J).Entity; | |
5707 | exit when Is_Concurrent_Type (Scope_Id); | |
5708 | end loop; | |
5709 | ||
5710 | -- If it is a return from accept statement it is expanded as call to | |
5711 | -- RTS Complete_Rendezvous and a goto to the end of the accept body. | |
5712 | ||
5713 | -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept, | |
5714 | -- Expand_N_Accept_Alternative in exp_ch9.adb) | |
5715 | ||
5716 | if Is_Task_Type (Scope_Id) then | |
5717 | ||
5718 | Call := | |
5719 | Make_Procedure_Call_Statement (Loc, | |
e4494292 | 5720 | Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc)); |
2b3d67a5 AC |
5721 | Insert_Before (N, Call); |
5722 | -- why not insert actions here??? | |
5723 | Analyze (Call); | |
5724 | ||
5725 | Acc_Stat := Parent (N); | |
5726 | while Nkind (Acc_Stat) /= N_Accept_Statement loop | |
5727 | Acc_Stat := Parent (Acc_Stat); | |
5728 | end loop; | |
5729 | ||
5730 | Lab_Node := Last (Statements | |
5731 | (Handled_Statement_Sequence (Acc_Stat))); | |
5732 | ||
5733 | Goto_Stat := Make_Goto_Statement (Loc, | |
5734 | Name => New_Occurrence_Of | |
5735 | (Entity (Identifier (Lab_Node)), Loc)); | |
5736 | ||
5737 | Set_Analyzed (Goto_Stat); | |
5738 | ||
5739 | Rewrite (N, Goto_Stat); | |
5740 | Analyze (N); | |
5741 | ||
5742 | -- If it is a return from an entry body, put a Complete_Entry_Body call | |
5743 | -- in front of the return. | |
5744 | ||
5745 | elsif Is_Protected_Type (Scope_Id) then | |
5746 | Call := | |
5747 | Make_Procedure_Call_Statement (Loc, | |
5748 | Name => | |
e4494292 | 5749 | New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc), |
2b3d67a5 AC |
5750 | Parameter_Associations => New_List ( |
5751 | Make_Attribute_Reference (Loc, | |
2c1b72d7 | 5752 | Prefix => |
e4494292 | 5753 | New_Occurrence_Of |
2b3d67a5 | 5754 | (Find_Protection_Object (Current_Scope), Loc), |
2c1b72d7 | 5755 | Attribute_Name => Name_Unchecked_Access))); |
2b3d67a5 AC |
5756 | |
5757 | Insert_Before (N, Call); | |
5758 | Analyze (Call); | |
5759 | end if; | |
5760 | end Expand_Non_Function_Return; | |
5761 | ||
70482933 RK |
5762 | --------------------------------------- |
5763 | -- Expand_Protected_Object_Reference -- | |
5764 | --------------------------------------- | |
5765 | ||
5766 | function Expand_Protected_Object_Reference | |
5767 | (N : Node_Id; | |
02822a92 | 5768 | Scop : Entity_Id) return Node_Id |
70482933 RK |
5769 | is |
5770 | Loc : constant Source_Ptr := Sloc (N); | |
5771 | Corr : Entity_Id; | |
5772 | Rec : Node_Id; | |
5773 | Param : Entity_Id; | |
5774 | Proc : Entity_Id; | |
5775 | ||
5776 | begin | |
7675ad4f | 5777 | Rec := Make_Identifier (Loc, Name_uObject); |
70482933 RK |
5778 | Set_Etype (Rec, Corresponding_Record_Type (Scop)); |
5779 | ||
2f1b20a9 ES |
5780 | -- Find enclosing protected operation, and retrieve its first parameter, |
5781 | -- which denotes the enclosing protected object. If the enclosing | |
5782 | -- operation is an entry, we are immediately within the protected body, | |
5783 | -- and we can retrieve the object from the service entries procedure. A | |
16b05213 | 5784 | -- barrier function has the same signature as an entry. A barrier |
2f1b20a9 ES |
5785 | -- function is compiled within the protected object, but unlike |
5786 | -- protected operations its never needs locks, so that its protected | |
5787 | -- body subprogram points to itself. | |
70482933 RK |
5788 | |
5789 | Proc := Current_Scope; | |
70482933 RK |
5790 | while Present (Proc) |
5791 | and then Scope (Proc) /= Scop | |
5792 | loop | |
5793 | Proc := Scope (Proc); | |
5794 | end loop; | |
5795 | ||
5796 | Corr := Protected_Body_Subprogram (Proc); | |
5797 | ||
5798 | if No (Corr) then | |
5799 | ||
5800 | -- Previous error left expansion incomplete. | |
5801 | -- Nothing to do on this call. | |
5802 | ||
5803 | return Empty; | |
5804 | end if; | |
5805 | ||
5806 | Param := | |
5807 | Defining_Identifier | |
5808 | (First (Parameter_Specifications (Parent (Corr)))); | |
5809 | ||
5810 | if Is_Subprogram (Proc) | |
5811 | and then Proc /= Corr | |
5812 | then | |
98f01d53 | 5813 | -- Protected function or procedure |
70482933 RK |
5814 | |
5815 | Set_Entity (Rec, Param); | |
5816 | ||
2f1b20a9 ES |
5817 | -- Rec is a reference to an entity which will not be in scope when |
5818 | -- the call is reanalyzed, and needs no further analysis. | |
70482933 RK |
5819 | |
5820 | Set_Analyzed (Rec); | |
5821 | ||
5822 | else | |
2f1b20a9 ES |
5823 | -- Entry or barrier function for entry body. The first parameter of |
5824 | -- the entry body procedure is pointer to the object. We create a | |
5825 | -- local variable of the proper type, duplicating what is done to | |
5826 | -- define _object later on. | |
70482933 RK |
5827 | |
5828 | declare | |
c12beea0 RD |
5829 | Decls : List_Id; |
5830 | Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T'); | |
fbf5a39b | 5831 | |
70482933 RK |
5832 | begin |
5833 | Decls := New_List ( | |
5834 | Make_Full_Type_Declaration (Loc, | |
5835 | Defining_Identifier => Obj_Ptr, | |
2c1b72d7 | 5836 | Type_Definition => |
70482933 RK |
5837 | Make_Access_To_Object_Definition (Loc, |
5838 | Subtype_Indication => | |
e4494292 | 5839 | New_Occurrence_Of |
c12beea0 | 5840 | (Corresponding_Record_Type (Scop), Loc)))); |
70482933 RK |
5841 | |
5842 | Insert_Actions (N, Decls); | |
6b958cec | 5843 | Freeze_Before (N, Obj_Ptr); |
70482933 RK |
5844 | |
5845 | Rec := | |
5846 | Make_Explicit_Dereference (Loc, | |
2c1b72d7 AC |
5847 | Prefix => |
5848 | Unchecked_Convert_To (Obj_Ptr, | |
5849 | New_Occurrence_Of (Param, Loc))); | |
70482933 | 5850 | |
2f1b20a9 | 5851 | -- Analyze new actual. Other actuals in calls are already analyzed |
7888a6ae | 5852 | -- and the list of actuals is not reanalyzed after rewriting. |
70482933 RK |
5853 | |
5854 | Set_Parent (Rec, N); | |
5855 | Analyze (Rec); | |
5856 | end; | |
5857 | end if; | |
5858 | ||
5859 | return Rec; | |
5860 | end Expand_Protected_Object_Reference; | |
5861 | ||
5862 | -------------------------------------- | |
5863 | -- Expand_Protected_Subprogram_Call -- | |
5864 | -------------------------------------- | |
5865 | ||
5866 | procedure Expand_Protected_Subprogram_Call | |
5867 | (N : Node_Id; | |
5868 | Subp : Entity_Id; | |
5869 | Scop : Entity_Id) | |
5870 | is | |
5871 | Rec : Node_Id; | |
5872 | ||
36295779 AC |
5873 | procedure Freeze_Called_Function; |
5874 | -- If it is a function call it can appear in elaboration code and | |
5875 | -- the called entity must be frozen before the call. This must be | |
5876 | -- done before the call is expanded, as the expansion may rewrite it | |
5877 | -- to something other than a call (e.g. a temporary initialized in a | |
5878 | -- transient block). | |
5879 | ||
5880 | ---------------------------- | |
5881 | -- Freeze_Called_Function -- | |
5882 | ---------------------------- | |
5883 | ||
5884 | procedure Freeze_Called_Function is | |
5885 | begin | |
5886 | if Ekind (Subp) = E_Function then | |
5887 | Freeze_Expression (Name (N)); | |
5888 | end if; | |
5889 | end Freeze_Called_Function; | |
5890 | ||
5891 | -- Start of processing for Expand_Protected_Subprogram_Call | |
5892 | ||
70482933 | 5893 | begin |
54bf19e4 AC |
5894 | -- If the protected object is not an enclosing scope, this is an inter- |
5895 | -- object function call. Inter-object procedure calls are expanded by | |
5896 | -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the | |
5897 | -- subprogram being called is in the protected body being compiled, and | |
5898 | -- if the protected object in the call is statically the enclosing type. | |
5899 | -- The object may be an component of some other data structure, in which | |
5900 | -- case this must be handled as an inter-object call. | |
70482933 RK |
5901 | |
5902 | if not In_Open_Scopes (Scop) | |
5903 | or else not Is_Entity_Name (Name (N)) | |
5904 | then | |
5905 | if Nkind (Name (N)) = N_Selected_Component then | |
5906 | Rec := Prefix (Name (N)); | |
5907 | ||
5908 | else | |
5909 | pragma Assert (Nkind (Name (N)) = N_Indexed_Component); | |
5910 | Rec := Prefix (Prefix (Name (N))); | |
5911 | end if; | |
5912 | ||
36295779 | 5913 | Freeze_Called_Function; |
70482933 | 5914 | Build_Protected_Subprogram_Call (N, |
2c1b72d7 | 5915 | Name => New_Occurrence_Of (Subp, Sloc (N)), |
2ba1a7c7 | 5916 | Rec => Convert_Concurrent (Rec, Etype (Rec)), |
70482933 RK |
5917 | External => True); |
5918 | ||
5919 | else | |
5920 | Rec := Expand_Protected_Object_Reference (N, Scop); | |
5921 | ||
5922 | if No (Rec) then | |
5923 | return; | |
5924 | end if; | |
5925 | ||
36295779 | 5926 | Freeze_Called_Function; |
70482933 RK |
5927 | Build_Protected_Subprogram_Call (N, |
5928 | Name => Name (N), | |
5929 | Rec => Rec, | |
5930 | External => False); | |
5931 | ||
5932 | end if; | |
5933 | ||
811c6a85 | 5934 | -- Analyze and resolve the new call. The actuals have already been |
b0159fbe | 5935 | -- resolved, but expansion of a function call will add extra actuals |
811c6a85 AC |
5936 | -- if needed. Analysis of a procedure call already includes resolution. |
5937 | ||
5938 | Analyze (N); | |
5939 | ||
5940 | if Ekind (Subp) = E_Function then | |
5941 | Resolve (N, Etype (Subp)); | |
5942 | end if; | |
70482933 RK |
5943 | end Expand_Protected_Subprogram_Call; |
5944 | ||
63585f75 SB |
5945 | -------------------------------------------- |
5946 | -- Has_Unconstrained_Access_Discriminants -- | |
5947 | -------------------------------------------- | |
5948 | ||
5949 | function Has_Unconstrained_Access_Discriminants | |
5950 | (Subtyp : Entity_Id) return Boolean | |
5951 | is | |
5952 | Discr : Entity_Id; | |
5953 | ||
5954 | begin | |
5955 | if Has_Discriminants (Subtyp) | |
5956 | and then not Is_Constrained (Subtyp) | |
5957 | then | |
5958 | Discr := First_Discriminant (Subtyp); | |
5959 | while Present (Discr) loop | |
5960 | if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then | |
5961 | return True; | |
5962 | end if; | |
5963 | ||
5964 | Next_Discriminant (Discr); | |
5965 | end loop; | |
5966 | end if; | |
ebf494ec | 5967 | |
63585f75 SB |
5968 | return False; |
5969 | end Has_Unconstrained_Access_Discriminants; | |
5970 | ||
2b3d67a5 AC |
5971 | ----------------------------------- |
5972 | -- Expand_Simple_Function_Return -- | |
5973 | ----------------------------------- | |
5974 | ||
54bf19e4 | 5975 | -- The "simple" comes from the syntax rule simple_return_statement. The |
a90bd866 | 5976 | -- semantics are not at all simple. |
2b3d67a5 AC |
5977 | |
5978 | procedure Expand_Simple_Function_Return (N : Node_Id) is | |
5979 | Loc : constant Source_Ptr := Sloc (N); | |
5980 | ||
5981 | Scope_Id : constant Entity_Id := | |
5982 | Return_Applies_To (Return_Statement_Entity (N)); | |
5983 | -- The function we are returning from | |
5984 | ||
5985 | R_Type : constant Entity_Id := Etype (Scope_Id); | |
5986 | -- The result type of the function | |
5987 | ||
5988 | Utyp : constant Entity_Id := Underlying_Type (R_Type); | |
5989 | ||
5990 | Exp : constant Node_Id := Expression (N); | |
5991 | pragma Assert (Present (Exp)); | |
5992 | ||
5993 | Exptyp : constant Entity_Id := Etype (Exp); | |
5994 | -- The type of the expression (not necessarily the same as R_Type) | |
5995 | ||
5996 | Subtype_Ind : Node_Id; | |
54bf19e4 AC |
5997 | -- If the result type of the function is class-wide and the expression |
5998 | -- has a specific type, then we use the expression's type as the type of | |
5999 | -- the return object. In cases where the expression is an aggregate that | |
6000 | -- is built in place, this avoids the need for an expensive conversion | |
6001 | -- of the return object to the specific type on assignments to the | |
6002 | -- individual components. | |
2b3d67a5 AC |
6003 | |
6004 | begin | |
6005 | if Is_Class_Wide_Type (R_Type) | |
6006 | and then not Is_Class_Wide_Type (Etype (Exp)) | |
6007 | then | |
6008 | Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc); | |
6009 | else | |
6010 | Subtype_Ind := New_Occurrence_Of (R_Type, Loc); | |
6011 | end if; | |
6012 | ||
6013 | -- For the case of a simple return that does not come from an extended | |
6014 | -- return, in the case of Ada 2005 where we are returning a limited | |
6015 | -- type, we rewrite "return <expression>;" to be: | |
6016 | ||
6017 | -- return _anon_ : <return_subtype> := <expression> | |
6018 | ||
6019 | -- The expansion produced by Expand_N_Extended_Return_Statement will | |
6020 | -- contain simple return statements (for example, a block containing | |
6021 | -- simple return of the return object), which brings us back here with | |
6022 | -- Comes_From_Extended_Return_Statement set. The reason for the barrier | |
6023 | -- checking for a simple return that does not come from an extended | |
6024 | -- return is to avoid this infinite recursion. | |
6025 | ||
6026 | -- The reason for this design is that for Ada 2005 limited returns, we | |
6027 | -- need to reify the return object, so we can build it "in place", and | |
6028 | -- we need a block statement to hang finalization and tasking stuff. | |
6029 | ||
6030 | -- ??? In order to avoid disruption, we avoid translating to extended | |
6031 | -- return except in the cases where we really need to (Ada 2005 for | |
6032 | -- inherently limited). We might prefer to do this translation in all | |
6033 | -- cases (except perhaps for the case of Ada 95 inherently limited), | |
6034 | -- in order to fully exercise the Expand_N_Extended_Return_Statement | |
6035 | -- code. This would also allow us to do the build-in-place optimization | |
6036 | -- for efficiency even in cases where it is semantically not required. | |
6037 | ||
6038 | -- As before, we check the type of the return expression rather than the | |
6039 | -- return type of the function, because the latter may be a limited | |
6040 | -- class-wide interface type, which is not a limited type, even though | |
6041 | -- the type of the expression may be. | |
6042 | ||
6043 | if not Comes_From_Extended_Return_Statement (N) | |
51245e2d | 6044 | and then Is_Limited_View (Etype (Expression (N))) |
0791fbe9 | 6045 | and then Ada_Version >= Ada_2005 |
2b3d67a5 | 6046 | and then not Debug_Flag_Dot_L |
f6f4d8d4 JM |
6047 | |
6048 | -- The functionality of interface thunks is simple and it is always | |
6049 | -- handled by means of simple return statements. This leaves their | |
6050 | -- expansion simple and clean. | |
6051 | ||
da1c23dd | 6052 | and then not Is_Thunk (Current_Scope) |
2b3d67a5 AC |
6053 | then |
6054 | declare | |
6055 | Return_Object_Entity : constant Entity_Id := | |
6056 | Make_Temporary (Loc, 'R', Exp); | |
f6f4d8d4 | 6057 | |
2b3d67a5 AC |
6058 | Obj_Decl : constant Node_Id := |
6059 | Make_Object_Declaration (Loc, | |
6060 | Defining_Identifier => Return_Object_Entity, | |
6061 | Object_Definition => Subtype_Ind, | |
6062 | Expression => Exp); | |
6063 | ||
f6f4d8d4 JM |
6064 | Ext : constant Node_Id := |
6065 | Make_Extended_Return_Statement (Loc, | |
6066 | Return_Object_Declarations => New_List (Obj_Decl)); | |
2b3d67a5 AC |
6067 | -- Do not perform this high-level optimization if the result type |
6068 | -- is an interface because the "this" pointer must be displaced. | |
6069 | ||
6070 | begin | |
6071 | Rewrite (N, Ext); | |
6072 | Analyze (N); | |
6073 | return; | |
6074 | end; | |
6075 | end if; | |
6076 | ||
6077 | -- Here we have a simple return statement that is part of the expansion | |
6078 | -- of an extended return statement (either written by the user, or | |
6079 | -- generated by the above code). | |
6080 | ||
6081 | -- Always normalize C/Fortran boolean result. This is not always needed, | |
6082 | -- but it seems a good idea to minimize the passing around of non- | |
6083 | -- normalized values, and in any case this handles the processing of | |
6084 | -- barrier functions for protected types, which turn the condition into | |
6085 | -- a return statement. | |
6086 | ||
6087 | if Is_Boolean_Type (Exptyp) | |
6088 | and then Nonzero_Is_True (Exptyp) | |
6089 | then | |
6090 | Adjust_Condition (Exp); | |
6091 | Adjust_Result_Type (Exp, Exptyp); | |
6092 | end if; | |
6093 | ||
6094 | -- Do validity check if enabled for returns | |
6095 | ||
6096 | if Validity_Checks_On | |
6097 | and then Validity_Check_Returns | |
6098 | then | |
6099 | Ensure_Valid (Exp); | |
6100 | end if; | |
6101 | ||
6102 | -- Check the result expression of a scalar function against the subtype | |
6103 | -- of the function by inserting a conversion. This conversion must | |
6104 | -- eventually be performed for other classes of types, but for now it's | |
6105 | -- only done for scalars. | |
6106 | -- ??? | |
6107 | ||
6108 | if Is_Scalar_Type (Exptyp) then | |
6109 | Rewrite (Exp, Convert_To (R_Type, Exp)); | |
6110 | ||
6111 | -- The expression is resolved to ensure that the conversion gets | |
6112 | -- expanded to generate a possible constraint check. | |
6113 | ||
6114 | Analyze_And_Resolve (Exp, R_Type); | |
6115 | end if; | |
6116 | ||
6117 | -- Deal with returning variable length objects and controlled types | |
6118 | ||
6119 | -- Nothing to do if we are returning by reference, or this is not a | |
6120 | -- type that requires special processing (indicated by the fact that | |
6121 | -- it requires a cleanup scope for the secondary stack case). | |
6122 | ||
51245e2d | 6123 | if Is_Limited_View (Exptyp) |
2b3d67a5 AC |
6124 | or else Is_Limited_Interface (Exptyp) |
6125 | then | |
6126 | null; | |
6127 | ||
f6f4d8d4 JM |
6128 | -- No copy needed for thunks returning interface type objects since |
6129 | -- the object is returned by reference and the maximum functionality | |
6130 | -- required is just to displace the pointer. | |
6131 | ||
4b342b91 | 6132 | elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then |
f6f4d8d4 JM |
6133 | null; |
6134 | ||
2b3d67a5 AC |
6135 | elsif not Requires_Transient_Scope (R_Type) then |
6136 | ||
6137 | -- Mutable records with no variable length components are not | |
6138 | -- returned on the sec-stack, so we need to make sure that the | |
6139 | -- backend will only copy back the size of the actual value, and not | |
6140 | -- the maximum size. We create an actual subtype for this purpose. | |
6141 | ||
6142 | declare | |
6143 | Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp)); | |
6144 | Decl : Node_Id; | |
6145 | Ent : Entity_Id; | |
6146 | begin | |
6147 | if Has_Discriminants (Ubt) | |
6148 | and then not Is_Constrained (Ubt) | |
6149 | and then not Has_Unchecked_Union (Ubt) | |
6150 | then | |
6151 | Decl := Build_Actual_Subtype (Ubt, Exp); | |
6152 | Ent := Defining_Identifier (Decl); | |
6153 | Insert_Action (Exp, Decl); | |
6154 | Rewrite (Exp, Unchecked_Convert_To (Ent, Exp)); | |
6155 | Analyze_And_Resolve (Exp); | |
6156 | end if; | |
6157 | end; | |
6158 | ||
6159 | -- Here if secondary stack is used | |
6160 | ||
6161 | else | |
c624298a AC |
6162 | -- Prevent the reclamation of the secondary stack by all enclosing |
6163 | -- blocks and loops as well as the related function, otherwise the | |
6164 | -- result will be reclaimed too early or even clobbered. Due to a | |
6165 | -- possible mix of internally generated blocks, source blocks and | |
6166 | -- loops, the scope stack may not be contiguous as all labels are | |
6167 | -- inserted at the top level within the related function. Instead, | |
6168 | -- perform a parent-based traversal and mark all appropriate | |
6169 | -- constructs. | |
2b3d67a5 AC |
6170 | |
6171 | declare | |
c624298a AC |
6172 | P : Node_Id; |
6173 | ||
2b3d67a5 | 6174 | begin |
c624298a AC |
6175 | P := N; |
6176 | while Present (P) loop | |
adb252d8 | 6177 | |
c624298a AC |
6178 | -- Mark the label of a source or internally generated block or |
6179 | -- loop. | |
adb252d8 | 6180 | |
c624298a AC |
6181 | if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then |
6182 | Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P))); | |
6183 | ||
6184 | -- Mark the enclosing function | |
6185 | ||
6186 | elsif Nkind (P) = N_Subprogram_Body then | |
6187 | if Present (Corresponding_Spec (P)) then | |
6188 | Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P)); | |
6189 | else | |
6190 | Set_Sec_Stack_Needed_For_Return (Defining_Entity (P)); | |
6191 | end if; | |
6192 | ||
6193 | -- Do not go beyond the enclosing function | |
6194 | ||
6195 | exit; | |
6196 | end if; | |
6197 | ||
6198 | P := Parent (P); | |
6199 | end loop; | |
2b3d67a5 AC |
6200 | end; |
6201 | ||
6202 | -- Optimize the case where the result is a function call. In this | |
6203 | -- case either the result is already on the secondary stack, or is | |
6204 | -- already being returned with the stack pointer depressed and no | |
54bf19e4 AC |
6205 | -- further processing is required except to set the By_Ref flag |
6206 | -- to ensure that gigi does not attempt an extra unnecessary copy. | |
2b3d67a5 AC |
6207 | -- (actually not just unnecessary but harmfully wrong in the case |
6208 | -- of a controlled type, where gigi does not know how to do a copy). | |
54bf19e4 AC |
6209 | -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy |
6210 | -- for array types if the constrained status of the target type is | |
6211 | -- different from that of the expression. | |
2b3d67a5 AC |
6212 | |
6213 | if Requires_Transient_Scope (Exptyp) | |
6214 | and then | |
6215 | (not Is_Array_Type (Exptyp) | |
6216 | or else Is_Constrained (Exptyp) = Is_Constrained (R_Type) | |
6217 | or else CW_Or_Has_Controlled_Part (Utyp)) | |
6218 | and then Nkind (Exp) = N_Function_Call | |
6219 | then | |
6220 | Set_By_Ref (N); | |
6221 | ||
6222 | -- Remove side effects from the expression now so that other parts | |
6223 | -- of the expander do not have to reanalyze this node without this | |
6224 | -- optimization | |
6225 | ||
6226 | Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp)); | |
6227 | ||
6228 | -- For controlled types, do the allocation on the secondary stack | |
6229 | -- manually in order to call adjust at the right time: | |
6230 | ||
6231 | -- type Anon1 is access R_Type; | |
6232 | -- for Anon1'Storage_pool use ss_pool; | |
6233 | -- Anon2 : anon1 := new R_Type'(expr); | |
6234 | -- return Anon2.all; | |
6235 | ||
6236 | -- We do the same for classwide types that are not potentially | |
6237 | -- controlled (by the virtue of restriction No_Finalization) because | |
6238 | -- gigi is not able to properly allocate class-wide types. | |
6239 | ||
6240 | elsif CW_Or_Has_Controlled_Part (Utyp) then | |
6241 | declare | |
6242 | Loc : constant Source_Ptr := Sloc (N); | |
6243 | Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A'); | |
6244 | Alloc_Node : Node_Id; | |
6245 | Temp : Entity_Id; | |
6246 | ||
6247 | begin | |
6248 | Set_Ekind (Acc_Typ, E_Access_Type); | |
6249 | ||
6250 | Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool)); | |
6251 | ||
6252 | -- This is an allocator for the secondary stack, and it's fine | |
6253 | -- to have Comes_From_Source set False on it, as gigi knows not | |
6254 | -- to flag it as a violation of No_Implicit_Heap_Allocations. | |
6255 | ||
6256 | Alloc_Node := | |
6257 | Make_Allocator (Loc, | |
6258 | Expression => | |
6259 | Make_Qualified_Expression (Loc, | |
e4494292 | 6260 | Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc), |
2b3d67a5 AC |
6261 | Expression => Relocate_Node (Exp))); |
6262 | ||
6263 | -- We do not want discriminant checks on the declaration, | |
6264 | -- given that it gets its value from the allocator. | |
6265 | ||
6266 | Set_No_Initialization (Alloc_Node); | |
6267 | ||
6268 | Temp := Make_Temporary (Loc, 'R', Alloc_Node); | |
6269 | ||
6270 | Insert_List_Before_And_Analyze (N, New_List ( | |
6271 | Make_Full_Type_Declaration (Loc, | |
6272 | Defining_Identifier => Acc_Typ, | |
6273 | Type_Definition => | |
6274 | Make_Access_To_Object_Definition (Loc, | |
6275 | Subtype_Indication => Subtype_Ind)), | |
6276 | ||
6277 | Make_Object_Declaration (Loc, | |
6278 | Defining_Identifier => Temp, | |
e4494292 | 6279 | Object_Definition => New_Occurrence_Of (Acc_Typ, Loc), |
2b3d67a5 AC |
6280 | Expression => Alloc_Node))); |
6281 | ||
6282 | Rewrite (Exp, | |
6283 | Make_Explicit_Dereference (Loc, | |
e4494292 | 6284 | Prefix => New_Occurrence_Of (Temp, Loc))); |
2b3d67a5 | 6285 | |
a1092b48 AC |
6286 | -- Ada 2005 (AI-251): If the type of the returned object is |
6287 | -- an interface then add an implicit type conversion to force | |
6288 | -- displacement of the "this" pointer. | |
6289 | ||
6290 | if Is_Interface (R_Type) then | |
6291 | Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp))); | |
6292 | end if; | |
6293 | ||
2b3d67a5 AC |
6294 | Analyze_And_Resolve (Exp, R_Type); |
6295 | end; | |
6296 | ||
6297 | -- Otherwise use the gigi mechanism to allocate result on the | |
6298 | -- secondary stack. | |
6299 | ||
6300 | else | |
6301 | Check_Restriction (No_Secondary_Stack, N); | |
6302 | Set_Storage_Pool (N, RTE (RE_SS_Pool)); | |
6303 | ||
6304 | -- If we are generating code for the VM do not use | |
6305 | -- SS_Allocate since everything is heap-allocated anyway. | |
6306 | ||
6307 | if VM_Target = No_VM then | |
6308 | Set_Procedure_To_Call (N, RTE (RE_SS_Allocate)); | |
6309 | end if; | |
6310 | end if; | |
6311 | end if; | |
6312 | ||
54bf19e4 AC |
6313 | -- Implement the rules of 6.5(8-10), which require a tag check in |
6314 | -- the case of a limited tagged return type, and tag reassignment for | |
2b3d67a5 AC |
6315 | -- nonlimited tagged results. These actions are needed when the return |
6316 | -- type is a specific tagged type and the result expression is a | |
54bf19e4 AC |
6317 | -- conversion or a formal parameter, because in that case the tag of |
6318 | -- the expression might differ from the tag of the specific result type. | |
2b3d67a5 AC |
6319 | |
6320 | if Is_Tagged_Type (Utyp) | |
6321 | and then not Is_Class_Wide_Type (Utyp) | |
6322 | and then (Nkind_In (Exp, N_Type_Conversion, | |
6323 | N_Unchecked_Type_Conversion) | |
6324 | or else (Is_Entity_Name (Exp) | |
6325 | and then Ekind (Entity (Exp)) in Formal_Kind)) | |
6326 | then | |
54bf19e4 AC |
6327 | -- When the return type is limited, perform a check that the tag of |
6328 | -- the result is the same as the tag of the return type. | |
2b3d67a5 AC |
6329 | |
6330 | if Is_Limited_Type (R_Type) then | |
6331 | Insert_Action (Exp, | |
6332 | Make_Raise_Constraint_Error (Loc, | |
6333 | Condition => | |
6334 | Make_Op_Ne (Loc, | |
2c1b72d7 | 6335 | Left_Opnd => |
2b3d67a5 | 6336 | Make_Selected_Component (Loc, |
7675ad4f AC |
6337 | Prefix => Duplicate_Subexpr (Exp), |
6338 | Selector_Name => Make_Identifier (Loc, Name_uTag)), | |
2b3d67a5 AC |
6339 | Right_Opnd => |
6340 | Make_Attribute_Reference (Loc, | |
2c1b72d7 AC |
6341 | Prefix => |
6342 | New_Occurrence_Of (Base_Type (Utyp), Loc), | |
2b3d67a5 | 6343 | Attribute_Name => Name_Tag)), |
2c1b72d7 | 6344 | Reason => CE_Tag_Check_Failed)); |
2b3d67a5 AC |
6345 | |
6346 | -- If the result type is a specific nonlimited tagged type, then we | |
6347 | -- have to ensure that the tag of the result is that of the result | |
54bf19e4 AC |
6348 | -- type. This is handled by making a copy of the expression in |
6349 | -- the case where it might have a different tag, namely when the | |
2b3d67a5 AC |
6350 | -- expression is a conversion or a formal parameter. We create a new |
6351 | -- object of the result type and initialize it from the expression, | |
6352 | -- which will implicitly force the tag to be set appropriately. | |
6353 | ||
6354 | else | |
6355 | declare | |
6356 | ExpR : constant Node_Id := Relocate_Node (Exp); | |
6357 | Result_Id : constant Entity_Id := | |
6358 | Make_Temporary (Loc, 'R', ExpR); | |
6359 | Result_Exp : constant Node_Id := | |
e4494292 | 6360 | New_Occurrence_Of (Result_Id, Loc); |
2b3d67a5 AC |
6361 | Result_Obj : constant Node_Id := |
6362 | Make_Object_Declaration (Loc, | |
6363 | Defining_Identifier => Result_Id, | |
6364 | Object_Definition => | |
e4494292 | 6365 | New_Occurrence_Of (R_Type, Loc), |
2b3d67a5 AC |
6366 | Constant_Present => True, |
6367 | Expression => ExpR); | |
6368 | ||
6369 | begin | |
6370 | Set_Assignment_OK (Result_Obj); | |
6371 | Insert_Action (Exp, Result_Obj); | |
6372 | ||
6373 | Rewrite (Exp, Result_Exp); | |
6374 | Analyze_And_Resolve (Exp, R_Type); | |
6375 | end; | |
6376 | end if; | |
6377 | ||
6378 | -- Ada 2005 (AI-344): If the result type is class-wide, then insert | |
6379 | -- a check that the level of the return expression's underlying type | |
6380 | -- is not deeper than the level of the master enclosing the function. | |
6381 | -- Always generate the check when the type of the return expression | |
6382 | -- is class-wide, when it's a type conversion, or when it's a formal | |
6383 | -- parameter. Otherwise, suppress the check in the case where the | |
6384 | -- return expression has a specific type whose level is known not to | |
6385 | -- be statically deeper than the function's result type. | |
6386 | ||
0a376301 JM |
6387 | -- No runtime check needed in interface thunks since it is performed |
6388 | -- by the target primitive associated with the thunk. | |
6389 | ||
2b3d67a5 AC |
6390 | -- Note: accessibility check is skipped in the VM case, since there |
6391 | -- does not seem to be any practical way to implement this check. | |
6392 | ||
0791fbe9 | 6393 | elsif Ada_Version >= Ada_2005 |
2b3d67a5 AC |
6394 | and then Tagged_Type_Expansion |
6395 | and then Is_Class_Wide_Type (R_Type) | |
0a376301 | 6396 | and then not Is_Thunk (Current_Scope) |
3217f71e | 6397 | and then not Scope_Suppress.Suppress (Accessibility_Check) |
2b3d67a5 AC |
6398 | and then |
6399 | (Is_Class_Wide_Type (Etype (Exp)) | |
6400 | or else Nkind_In (Exp, N_Type_Conversion, | |
6401 | N_Unchecked_Type_Conversion) | |
6402 | or else (Is_Entity_Name (Exp) | |
2c1b72d7 | 6403 | and then Ekind (Entity (Exp)) in Formal_Kind) |
2b3d67a5 AC |
6404 | or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) > |
6405 | Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id))) | |
6406 | then | |
6407 | declare | |
6408 | Tag_Node : Node_Id; | |
6409 | ||
6410 | begin | |
6411 | -- Ada 2005 (AI-251): In class-wide interface objects we displace | |
c5f5123f AC |
6412 | -- "this" to reference the base of the object. This is required to |
6413 | -- get access to the TSD of the object. | |
2b3d67a5 AC |
6414 | |
6415 | if Is_Class_Wide_Type (Etype (Exp)) | |
6416 | and then Is_Interface (Etype (Exp)) | |
6417 | and then Nkind (Exp) = N_Explicit_Dereference | |
6418 | then | |
6419 | Tag_Node := | |
6420 | Make_Explicit_Dereference (Loc, | |
2c1b72d7 AC |
6421 | Prefix => |
6422 | Unchecked_Convert_To (RTE (RE_Tag_Ptr), | |
6423 | Make_Function_Call (Loc, | |
6424 | Name => | |
e4494292 | 6425 | New_Occurrence_Of (RTE (RE_Base_Address), Loc), |
2c1b72d7 AC |
6426 | Parameter_Associations => New_List ( |
6427 | Unchecked_Convert_To (RTE (RE_Address), | |
6428 | Duplicate_Subexpr (Prefix (Exp))))))); | |
2b3d67a5 AC |
6429 | else |
6430 | Tag_Node := | |
6431 | Make_Attribute_Reference (Loc, | |
2c1b72d7 | 6432 | Prefix => Duplicate_Subexpr (Exp), |
2b3d67a5 AC |
6433 | Attribute_Name => Name_Tag); |
6434 | end if; | |
6435 | ||
6436 | Insert_Action (Exp, | |
6437 | Make_Raise_Program_Error (Loc, | |
6438 | Condition => | |
6439 | Make_Op_Gt (Loc, | |
2c1b72d7 | 6440 | Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node), |
2b3d67a5 AC |
6441 | Right_Opnd => |
6442 | Make_Integer_Literal (Loc, | |
6443 | Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))), | |
6444 | Reason => PE_Accessibility_Check_Failed)); | |
6445 | end; | |
6446 | ||
6447 | -- AI05-0073: If function has a controlling access result, check that | |
6448 | -- the tag of the return value, if it is not null, matches designated | |
6449 | -- type of return type. | |
f7ea2603 RD |
6450 | |
6451 | -- The return expression is referenced twice in the code below, so it | |
6452 | -- must be made free of side effects. Given that different compilers | |
2b3d67a5 AC |
6453 | -- may evaluate these parameters in different order, both occurrences |
6454 | -- perform a copy. | |
6455 | ||
6456 | elsif Ekind (R_Type) = E_Anonymous_Access_Type | |
6457 | and then Has_Controlling_Result (Scope_Id) | |
6458 | then | |
6459 | Insert_Action (N, | |
6460 | Make_Raise_Constraint_Error (Loc, | |
6461 | Condition => | |
6462 | Make_And_Then (Loc, | |
6463 | Left_Opnd => | |
6464 | Make_Op_Ne (Loc, | |
6465 | Left_Opnd => Duplicate_Subexpr (Exp), | |
6466 | Right_Opnd => Make_Null (Loc)), | |
ebf494ec | 6467 | |
2b3d67a5 AC |
6468 | Right_Opnd => Make_Op_Ne (Loc, |
6469 | Left_Opnd => | |
6470 | Make_Selected_Component (Loc, | |
6471 | Prefix => Duplicate_Subexpr (Exp), | |
7675ad4f | 6472 | Selector_Name => Make_Identifier (Loc, Name_uTag)), |
ebf494ec | 6473 | |
2b3d67a5 AC |
6474 | Right_Opnd => |
6475 | Make_Attribute_Reference (Loc, | |
6476 | Prefix => | |
6477 | New_Occurrence_Of (Designated_Type (R_Type), Loc), | |
6478 | Attribute_Name => Name_Tag))), | |
ebf494ec | 6479 | |
2b3d67a5 AC |
6480 | Reason => CE_Tag_Check_Failed), |
6481 | Suppress => All_Checks); | |
6482 | end if; | |
6483 | ||
63585f75 SB |
6484 | -- AI05-0234: RM 6.5(21/3). Check access discriminants to |
6485 | -- ensure that the function result does not outlive an | |
6486 | -- object designated by one of it discriminants. | |
6487 | ||
57a3fca9 | 6488 | if Present (Extra_Accessibility_Of_Result (Scope_Id)) |
63585f75 SB |
6489 | and then Has_Unconstrained_Access_Discriminants (R_Type) |
6490 | then | |
6491 | declare | |
ebf494ec | 6492 | Discrim_Source : Node_Id; |
63585f75 SB |
6493 | |
6494 | procedure Check_Against_Result_Level (Level : Node_Id); | |
ebf494ec RD |
6495 | -- Check the given accessibility level against the level |
6496 | -- determined by the point of call. (AI05-0234). | |
63585f75 SB |
6497 | |
6498 | -------------------------------- | |
6499 | -- Check_Against_Result_Level -- | |
6500 | -------------------------------- | |
6501 | ||
6502 | procedure Check_Against_Result_Level (Level : Node_Id) is | |
6503 | begin | |
6504 | Insert_Action (N, | |
6505 | Make_Raise_Program_Error (Loc, | |
6506 | Condition => | |
6507 | Make_Op_Gt (Loc, | |
6508 | Left_Opnd => Level, | |
6509 | Right_Opnd => | |
6510 | New_Occurrence_Of | |
6511 | (Extra_Accessibility_Of_Result (Scope_Id), Loc)), | |
6512 | Reason => PE_Accessibility_Check_Failed)); | |
6513 | end Check_Against_Result_Level; | |
ebf494ec | 6514 | |
63585f75 | 6515 | begin |
ebf494ec | 6516 | Discrim_Source := Exp; |
63585f75 SB |
6517 | while Nkind (Discrim_Source) = N_Qualified_Expression loop |
6518 | Discrim_Source := Expression (Discrim_Source); | |
6519 | end loop; | |
6520 | ||
6521 | if Nkind (Discrim_Source) = N_Identifier | |
6522 | and then Is_Return_Object (Entity (Discrim_Source)) | |
6523 | then | |
63585f75 SB |
6524 | Discrim_Source := Entity (Discrim_Source); |
6525 | ||
6526 | if Is_Constrained (Etype (Discrim_Source)) then | |
6527 | Discrim_Source := Etype (Discrim_Source); | |
6528 | else | |
6529 | Discrim_Source := Expression (Parent (Discrim_Source)); | |
6530 | end if; | |
6531 | ||
6532 | elsif Nkind (Discrim_Source) = N_Identifier | |
6533 | and then Nkind_In (Original_Node (Discrim_Source), | |
6534 | N_Aggregate, N_Extension_Aggregate) | |
6535 | then | |
63585f75 SB |
6536 | Discrim_Source := Original_Node (Discrim_Source); |
6537 | ||
6538 | elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then | |
6539 | Nkind (Original_Node (Discrim_Source)) = N_Function_Call | |
6540 | then | |
63585f75 | 6541 | Discrim_Source := Original_Node (Discrim_Source); |
63585f75 SB |
6542 | end if; |
6543 | ||
6544 | while Nkind_In (Discrim_Source, N_Qualified_Expression, | |
6545 | N_Type_Conversion, | |
6546 | N_Unchecked_Type_Conversion) | |
6547 | loop | |
63585f75 SB |
6548 | Discrim_Source := Expression (Discrim_Source); |
6549 | end loop; | |
6550 | ||
6551 | case Nkind (Discrim_Source) is | |
6552 | when N_Defining_Identifier => | |
6553 | ||
54bf19e4 AC |
6554 | pragma Assert (Is_Composite_Type (Discrim_Source) |
6555 | and then Has_Discriminants (Discrim_Source) | |
6556 | and then Is_Constrained (Discrim_Source)); | |
63585f75 SB |
6557 | |
6558 | declare | |
6559 | Discrim : Entity_Id := | |
6560 | First_Discriminant (Base_Type (R_Type)); | |
6561 | Disc_Elmt : Elmt_Id := | |
6562 | First_Elmt (Discriminant_Constraint | |
6563 | (Discrim_Source)); | |
6564 | begin | |
6565 | loop | |
6566 | if Ekind (Etype (Discrim)) = | |
54bf19e4 AC |
6567 | E_Anonymous_Access_Type |
6568 | then | |
63585f75 SB |
6569 | Check_Against_Result_Level |
6570 | (Dynamic_Accessibility_Level (Node (Disc_Elmt))); | |
6571 | end if; | |
6572 | ||
6573 | Next_Elmt (Disc_Elmt); | |
6574 | Next_Discriminant (Discrim); | |
6575 | exit when not Present (Discrim); | |
6576 | end loop; | |
6577 | end; | |
6578 | ||
6579 | when N_Aggregate | N_Extension_Aggregate => | |
6580 | ||
54bf19e4 AC |
6581 | -- Unimplemented: extension aggregate case where discrims |
6582 | -- come from ancestor part, not extension part. | |
63585f75 SB |
6583 | |
6584 | declare | |
6585 | Discrim : Entity_Id := | |
6586 | First_Discriminant (Base_Type (R_Type)); | |
6587 | ||
6588 | Disc_Exp : Node_Id := Empty; | |
6589 | ||
6590 | Positionals_Exhausted | |
6591 | : Boolean := not Present (Expressions | |
6592 | (Discrim_Source)); | |
6593 | ||
6594 | function Associated_Expr | |
6595 | (Comp_Id : Entity_Id; | |
6596 | Associations : List_Id) return Node_Id; | |
6597 | ||
6598 | -- Given a component and a component associations list, | |
6599 | -- locate the expression for that component; returns | |
6600 | -- Empty if no such expression is found. | |
6601 | ||
6602 | --------------------- | |
6603 | -- Associated_Expr -- | |
6604 | --------------------- | |
6605 | ||
6606 | function Associated_Expr | |
6607 | (Comp_Id : Entity_Id; | |
6608 | Associations : List_Id) return Node_Id | |
6609 | is | |
54bf19e4 | 6610 | Assoc : Node_Id; |
63585f75 | 6611 | Choice : Node_Id; |
54bf19e4 | 6612 | |
63585f75 SB |
6613 | begin |
6614 | -- Simple linear search seems ok here | |
6615 | ||
54bf19e4 | 6616 | Assoc := First (Associations); |
63585f75 SB |
6617 | while Present (Assoc) loop |
6618 | Choice := First (Choices (Assoc)); | |
63585f75 SB |
6619 | while Present (Choice) loop |
6620 | if (Nkind (Choice) = N_Identifier | |
54bf19e4 AC |
6621 | and then Chars (Choice) = Chars (Comp_Id)) |
6622 | or else (Nkind (Choice) = N_Others_Choice) | |
63585f75 SB |
6623 | then |
6624 | return Expression (Assoc); | |
6625 | end if; | |
6626 | ||
6627 | Next (Choice); | |
6628 | end loop; | |
6629 | ||
6630 | Next (Assoc); | |
6631 | end loop; | |
6632 | ||
6633 | return Empty; | |
6634 | end Associated_Expr; | |
6635 | ||
6636 | -- Start of processing for Expand_Simple_Function_Return | |
6637 | ||
6638 | begin | |
6639 | if not Positionals_Exhausted then | |
6640 | Disc_Exp := First (Expressions (Discrim_Source)); | |
6641 | end if; | |
6642 | ||
6643 | loop | |
6644 | if Positionals_Exhausted then | |
54bf19e4 AC |
6645 | Disc_Exp := |
6646 | Associated_Expr | |
6647 | (Discrim, | |
6648 | Component_Associations (Discrim_Source)); | |
63585f75 SB |
6649 | end if; |
6650 | ||
6651 | if Ekind (Etype (Discrim)) = | |
54bf19e4 AC |
6652 | E_Anonymous_Access_Type |
6653 | then | |
63585f75 SB |
6654 | Check_Against_Result_Level |
6655 | (Dynamic_Accessibility_Level (Disc_Exp)); | |
6656 | end if; | |
6657 | ||
6658 | Next_Discriminant (Discrim); | |
6659 | exit when not Present (Discrim); | |
6660 | ||
6661 | if not Positionals_Exhausted then | |
6662 | Next (Disc_Exp); | |
6663 | Positionals_Exhausted := not Present (Disc_Exp); | |
6664 | end if; | |
6665 | end loop; | |
6666 | end; | |
6667 | ||
6668 | when N_Function_Call => | |
54bf19e4 AC |
6669 | |
6670 | -- No check needed (check performed by callee) | |
6671 | ||
63585f75 SB |
6672 | null; |
6673 | ||
6674 | when others => | |
6675 | ||
6676 | declare | |
6677 | Level : constant Node_Id := | |
54bf19e4 AC |
6678 | Make_Integer_Literal (Loc, |
6679 | Object_Access_Level (Discrim_Source)); | |
6680 | ||
63585f75 SB |
6681 | begin |
6682 | -- Unimplemented: check for name prefix that includes | |
6683 | -- a dereference of an access value with a dynamic | |
6684 | -- accessibility level (e.g., an access param or a | |
6685 | -- saooaaat) and use dynamic level in that case. For | |
6686 | -- example: | |
6687 | -- return Access_Param.all(Some_Index).Some_Component; | |
54bf19e4 | 6688 | -- ??? |
63585f75 SB |
6689 | |
6690 | Set_Etype (Level, Standard_Natural); | |
6691 | Check_Against_Result_Level (Level); | |
6692 | end; | |
6693 | ||
6694 | end case; | |
6695 | end; | |
6696 | end if; | |
6697 | ||
2b3d67a5 AC |
6698 | -- If we are returning an object that may not be bit-aligned, then copy |
6699 | -- the value into a temporary first. This copy may need to expand to a | |
6700 | -- loop of component operations. | |
6701 | ||
6702 | if Is_Possibly_Unaligned_Slice (Exp) | |
6703 | or else Is_Possibly_Unaligned_Object (Exp) | |
6704 | then | |
6705 | declare | |
6706 | ExpR : constant Node_Id := Relocate_Node (Exp); | |
6707 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR); | |
6708 | begin | |
6709 | Insert_Action (Exp, | |
6710 | Make_Object_Declaration (Loc, | |
6711 | Defining_Identifier => Tnn, | |
6712 | Constant_Present => True, | |
6713 | Object_Definition => New_Occurrence_Of (R_Type, Loc), | |
6714 | Expression => ExpR), | |
2c1b72d7 | 6715 | Suppress => All_Checks); |
2b3d67a5 AC |
6716 | Rewrite (Exp, New_Occurrence_Of (Tnn, Loc)); |
6717 | end; | |
6718 | end if; | |
6719 | ||
6720 | -- Generate call to postcondition checks if they are present | |
6721 | ||
6722 | if Ekind (Scope_Id) = E_Function | |
6723 | and then Has_Postconditions (Scope_Id) | |
6724 | then | |
6725 | -- We are going to reference the returned value twice in this case, | |
6726 | -- once in the call to _Postconditions, and once in the actual return | |
6727 | -- statement, but we can't have side effects happening twice, and in | |
6728 | -- any case for efficiency we don't want to do the computation twice. | |
6729 | ||
6730 | -- If the returned expression is an entity name, we don't need to | |
6731 | -- worry since it is efficient and safe to reference it twice, that's | |
6732 | -- also true for literals other than string literals, and for the | |
6733 | -- case of X.all where X is an entity name. | |
6734 | ||
6735 | if Is_Entity_Name (Exp) | |
6736 | or else Nkind_In (Exp, N_Character_Literal, | |
6737 | N_Integer_Literal, | |
6738 | N_Real_Literal) | |
6739 | or else (Nkind (Exp) = N_Explicit_Dereference | |
2c1b72d7 | 6740 | and then Is_Entity_Name (Prefix (Exp))) |
2b3d67a5 AC |
6741 | then |
6742 | null; | |
6743 | ||
6744 | -- Otherwise we are going to need a temporary to capture the value | |
6745 | ||
6746 | else | |
6747 | declare | |
ca3e17b0 | 6748 | ExpR : Node_Id := Relocate_Node (Exp); |
2b3d67a5 AC |
6749 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR); |
6750 | ||
6751 | begin | |
ca3e17b0 AC |
6752 | -- In the case of discriminated objects, we have created a |
6753 | -- constrained subtype above, and used the underlying type. | |
6754 | -- This transformation is post-analysis and harmless, except | |
6755 | -- that now the call to the post-condition will be analyzed and | |
6756 | -- type kinds have to match. | |
6757 | ||
6758 | if Nkind (ExpR) = N_Unchecked_Type_Conversion | |
6759 | and then | |
6760 | Is_Private_Type (R_Type) /= Is_Private_Type (Etype (ExpR)) | |
6761 | then | |
6762 | ExpR := Expression (ExpR); | |
6763 | end if; | |
6764 | ||
2b3d67a5 AC |
6765 | -- For a complex expression of an elementary type, capture |
6766 | -- value in the temporary and use it as the reference. | |
6767 | ||
6768 | if Is_Elementary_Type (R_Type) then | |
6769 | Insert_Action (Exp, | |
6770 | Make_Object_Declaration (Loc, | |
6771 | Defining_Identifier => Tnn, | |
6772 | Constant_Present => True, | |
6773 | Object_Definition => New_Occurrence_Of (R_Type, Loc), | |
6774 | Expression => ExpR), | |
6775 | Suppress => All_Checks); | |
6776 | ||
6777 | Rewrite (Exp, New_Occurrence_Of (Tnn, Loc)); | |
6778 | ||
6779 | -- If we have something we can rename, generate a renaming of | |
6780 | -- the object and replace the expression with a reference | |
6781 | ||
6782 | elsif Is_Object_Reference (Exp) then | |
6783 | Insert_Action (Exp, | |
6784 | Make_Object_Renaming_Declaration (Loc, | |
6785 | Defining_Identifier => Tnn, | |
6786 | Subtype_Mark => New_Occurrence_Of (R_Type, Loc), | |
6787 | Name => ExpR), | |
6788 | Suppress => All_Checks); | |
6789 | ||
6790 | Rewrite (Exp, New_Occurrence_Of (Tnn, Loc)); | |
6791 | ||
6792 | -- Otherwise we have something like a string literal or an | |
6793 | -- aggregate. We could copy the value, but that would be | |
6794 | -- inefficient. Instead we make a reference to the value and | |
6795 | -- capture this reference with a renaming, the expression is | |
6796 | -- then replaced by a dereference of this renaming. | |
6797 | ||
6798 | else | |
6799 | -- For now, copy the value, since the code below does not | |
6800 | -- seem to work correctly ??? | |
6801 | ||
6802 | Insert_Action (Exp, | |
6803 | Make_Object_Declaration (Loc, | |
6804 | Defining_Identifier => Tnn, | |
6805 | Constant_Present => True, | |
6806 | Object_Definition => New_Occurrence_Of (R_Type, Loc), | |
6807 | Expression => Relocate_Node (Exp)), | |
6808 | Suppress => All_Checks); | |
6809 | ||
6810 | Rewrite (Exp, New_Occurrence_Of (Tnn, Loc)); | |
6811 | ||
6812 | -- Insert_Action (Exp, | |
6813 | -- Make_Object_Renaming_Declaration (Loc, | |
6814 | -- Defining_Identifier => Tnn, | |
6815 | -- Access_Definition => | |
6816 | -- Make_Access_Definition (Loc, | |
6817 | -- All_Present => True, | |
6818 | -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)), | |
6819 | -- Name => | |
6820 | -- Make_Reference (Loc, | |
6821 | -- Prefix => Relocate_Node (Exp))), | |
6822 | -- Suppress => All_Checks); | |
6823 | ||
6824 | -- Rewrite (Exp, | |
6825 | -- Make_Explicit_Dereference (Loc, | |
6826 | -- Prefix => New_Occurrence_Of (Tnn, Loc))); | |
6827 | end if; | |
6828 | end; | |
6829 | end if; | |
6830 | ||
6831 | -- Generate call to _postconditions | |
6832 | ||
6833 | Insert_Action (Exp, | |
6834 | Make_Procedure_Call_Statement (Loc, | |
6835 | Name => Make_Identifier (Loc, Name_uPostconditions), | |
6836 | Parameter_Associations => New_List (Duplicate_Subexpr (Exp)))); | |
6837 | end if; | |
6838 | ||
6839 | -- Ada 2005 (AI-251): If this return statement corresponds with an | |
6840 | -- simple return statement associated with an extended return statement | |
6841 | -- and the type of the returned object is an interface then generate an | |
6842 | -- implicit conversion to force displacement of the "this" pointer. | |
6843 | ||
0791fbe9 | 6844 | if Ada_Version >= Ada_2005 |
2b3d67a5 AC |
6845 | and then Comes_From_Extended_Return_Statement (N) |
6846 | and then Nkind (Expression (N)) = N_Identifier | |
6847 | and then Is_Interface (Utyp) | |
6848 | and then Utyp /= Underlying_Type (Exptyp) | |
6849 | then | |
6850 | Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp))); | |
6851 | Analyze_And_Resolve (Exp); | |
6852 | end if; | |
6853 | end Expand_Simple_Function_Return; | |
6854 | ||
ea3c0651 AC |
6855 | -------------------------------- |
6856 | -- Expand_Subprogram_Contract -- | |
6857 | -------------------------------- | |
6858 | ||
6859 | procedure Expand_Subprogram_Contract | |
6860 | (N : Node_Id; | |
6861 | Spec_Id : Entity_Id; | |
6862 | Body_Id : Entity_Id) | |
6863 | is | |
6864 | procedure Add_Invariant_And_Predicate_Checks | |
6865 | (Subp_Id : Entity_Id; | |
6866 | Stmts : in out List_Id; | |
6867 | Result : out Node_Id); | |
6868 | -- Process the result of function Subp_Id (if applicable) and all its | |
6869 | -- formals. Add invariant and predicate checks where applicable. The | |
6870 | -- routine appends all the checks to list Stmts. If Subp_Id denotes a | |
6871 | -- function, Result contains the entity of parameter _Result, to be | |
6872 | -- used in the creation of procedure _Postconditions. | |
6873 | ||
6874 | procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id); | |
6875 | -- Append a node to a list. If there is no list, create a new one. When | |
6876 | -- the item denotes a pragma, it is added to the list only when it is | |
6877 | -- enabled. | |
6878 | ||
6879 | procedure Build_Postconditions_Procedure | |
6880 | (Subp_Id : Entity_Id; | |
6881 | Stmts : List_Id; | |
6882 | Result : Entity_Id); | |
6883 | -- Create the body of procedure _Postconditions which handles various | |
6884 | -- assertion actions on exit from subprogram Subp_Id. Stmts is the list | |
6885 | -- of statements to be checked on exit. Parameter Result is the entity | |
6886 | -- of parameter _Result when Subp_Id denotes a function. | |
6887 | ||
6888 | function Build_Pragma_Check_Equivalent | |
6889 | (Prag : Node_Id; | |
6890 | Subp_Id : Entity_Id := Empty; | |
6891 | Inher_Id : Entity_Id := Empty) return Node_Id; | |
6892 | -- Transform a [refined] pre- or postcondition denoted by Prag into an | |
6893 | -- equivalent pragma Check. When the pre- or postcondition is inherited, | |
6894 | -- the routine corrects the references of all formals of Inher_Id to | |
6895 | -- point to the formals of Subp_Id. | |
6896 | ||
6897 | procedure Collect_Body_Postconditions (Stmts : in out List_Id); | |
6898 | -- Process all postconditions found in the declarations of the body. The | |
6899 | -- routine appends the pragma Check equivalents to list Stmts. | |
6900 | ||
6901 | procedure Collect_Spec_Postconditions | |
6902 | (Subp_Id : Entity_Id; | |
6903 | Stmts : in out List_Id); | |
6904 | -- Process all [inherited] postconditions of subprogram spec Subp_Id. | |
6905 | -- The routine appends the pragma Check equivalents to list Stmts. | |
6906 | ||
6907 | procedure Collect_Spec_Preconditions (Subp_Id : Entity_Id); | |
6908 | -- Process all [inherited] preconditions of subprogram spec Subp_Id. The | |
6909 | -- routine prepends the pragma Check equivalents to the declarations of | |
6910 | -- the body. | |
6911 | ||
6912 | procedure Prepend_To_Declarations (Item : Node_Id); | |
6913 | -- Prepend a single item to the declarations of the subprogram body | |
6914 | ||
6915 | procedure Process_Contract_Cases | |
6916 | (Subp_Id : Entity_Id; | |
6917 | Stmts : in out List_Id); | |
6918 | -- Process pragma Contract_Cases of subprogram spec Subp_Id. The routine | |
6919 | -- appends the expanded code to list Stmts. | |
6920 | ||
6921 | ---------------------------------------- | |
6922 | -- Add_Invariant_And_Predicate_Checks -- | |
6923 | ---------------------------------------- | |
6924 | ||
6925 | procedure Add_Invariant_And_Predicate_Checks | |
6926 | (Subp_Id : Entity_Id; | |
6927 | Stmts : in out List_Id; | |
6928 | Result : out Node_Id) | |
6929 | is | |
6930 | procedure Add_Invariant_Access_Checks (Id : Entity_Id); | |
6931 | -- Id denotes the return value of a function or a formal parameter. | |
6932 | -- Add an invariant check if the type of Id is access to a type with | |
6933 | -- invariants. The routine appends the generated code to Stmts. | |
6934 | ||
6935 | function Invariant_Checks_OK (Typ : Entity_Id) return Boolean; | |
6936 | -- Determine whether type Typ can benefit from invariant checks. To | |
6937 | -- qualify, the type must have a non-null invariant procedure and | |
6938 | -- subprogram Subp_Id must appear visible from the point of view of | |
6939 | -- the type. | |
6940 | ||
ea3c0651 AC |
6941 | --------------------------------- |
6942 | -- Add_Invariant_Access_Checks -- | |
6943 | --------------------------------- | |
6944 | ||
6945 | procedure Add_Invariant_Access_Checks (Id : Entity_Id) is | |
6946 | Loc : constant Source_Ptr := Sloc (N); | |
6947 | Ref : Node_Id; | |
6948 | Typ : Entity_Id; | |
6949 | ||
6950 | begin | |
6951 | Typ := Etype (Id); | |
6952 | ||
6953 | if Is_Access_Type (Typ) and then not Is_Access_Constant (Typ) then | |
6954 | Typ := Designated_Type (Typ); | |
6955 | ||
6956 | if Invariant_Checks_OK (Typ) then | |
6957 | Ref := | |
6958 | Make_Explicit_Dereference (Loc, | |
6959 | Prefix => New_Occurrence_Of (Id, Loc)); | |
6960 | Set_Etype (Ref, Typ); | |
6961 | ||
6962 | -- Generate: | |
6963 | -- if <Id> /= null then | |
6964 | -- <invariant_call (<Ref>)> | |
6965 | -- end if; | |
6966 | ||
6967 | Append_Enabled_Item | |
6968 | (Item => | |
6969 | Make_If_Statement (Loc, | |
6970 | Condition => | |
6971 | Make_Op_Ne (Loc, | |
6972 | Left_Opnd => New_Occurrence_Of (Id, Loc), | |
6973 | Right_Opnd => Make_Null (Loc)), | |
6974 | Then_Statements => New_List ( | |
6975 | Make_Invariant_Call (Ref))), | |
6976 | List => Stmts); | |
6977 | end if; | |
6978 | end if; | |
6979 | end Add_Invariant_Access_Checks; | |
6980 | ||
6981 | ------------------------- | |
6982 | -- Invariant_Checks_OK -- | |
6983 | ------------------------- | |
6984 | ||
6985 | function Invariant_Checks_OK (Typ : Entity_Id) return Boolean is | |
6986 | function Has_Null_Body (Proc_Id : Entity_Id) return Boolean; | |
6987 | -- Determine whether the body of procedure Proc_Id contains a sole | |
6988 | -- null statement, possibly followed by an optional return. | |
6989 | ||
6990 | function Has_Public_Visibility_Of_Subprogram return Boolean; | |
6991 | -- Determine whether type Typ has public visibility of subprogram | |
6992 | -- Subp_Id. | |
6993 | ||
6994 | ------------------- | |
6995 | -- Has_Null_Body -- | |
6996 | ------------------- | |
6997 | ||
6998 | function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is | |
6999 | Body_Id : Entity_Id; | |
7000 | Decl : Node_Id; | |
7001 | Spec : Node_Id; | |
7002 | Stmt1 : Node_Id; | |
7003 | Stmt2 : Node_Id; | |
7004 | ||
7005 | begin | |
7006 | Spec := Parent (Proc_Id); | |
7007 | Decl := Parent (Spec); | |
7008 | ||
7009 | -- Retrieve the entity of the invariant procedure body | |
7010 | ||
7011 | if Nkind (Spec) = N_Procedure_Specification | |
7012 | and then Nkind (Decl) = N_Subprogram_Declaration | |
7013 | then | |
7014 | Body_Id := Corresponding_Body (Decl); | |
7015 | ||
7016 | -- The body acts as a spec | |
7017 | ||
7018 | else | |
7019 | Body_Id := Proc_Id; | |
7020 | end if; | |
7021 | ||
7022 | -- The body will be generated later | |
7023 | ||
7024 | if No (Body_Id) then | |
7025 | return False; | |
7026 | end if; | |
7027 | ||
7028 | Spec := Parent (Body_Id); | |
7029 | Decl := Parent (Spec); | |
7030 | ||
7031 | pragma Assert | |
7032 | (Nkind (Spec) = N_Procedure_Specification | |
7033 | and then Nkind (Decl) = N_Subprogram_Body); | |
7034 | ||
7035 | Stmt1 := First (Statements (Handled_Statement_Sequence (Decl))); | |
7036 | ||
7037 | -- Look for a null statement followed by an optional return | |
7038 | -- statement. | |
7039 | ||
7040 | if Nkind (Stmt1) = N_Null_Statement then | |
7041 | Stmt2 := Next (Stmt1); | |
7042 | ||
7043 | if Present (Stmt2) then | |
7044 | return Nkind (Stmt2) = N_Simple_Return_Statement; | |
7045 | else | |
7046 | return True; | |
7047 | end if; | |
7048 | end if; | |
7049 | ||
7050 | return False; | |
7051 | end Has_Null_Body; | |
7052 | ||
7053 | ----------------------------------------- | |
7054 | -- Has_Public_Visibility_Of_Subprogram -- | |
7055 | ----------------------------------------- | |
7056 | ||
7057 | function Has_Public_Visibility_Of_Subprogram return Boolean is | |
7058 | Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id); | |
d950f051 | 7059 | |
ea3c0651 AC |
7060 | begin |
7061 | -- An Initialization procedure must be considered visible even | |
7062 | -- though it is internally generated. | |
7063 | ||
7064 | if Is_Init_Proc (Defining_Entity (Subp_Decl)) then | |
7065 | return True; | |
7066 | ||
d950f051 AC |
7067 | elsif Ekind (Scope (Typ)) /= E_Package then |
7068 | return False; | |
7069 | ||
ea3c0651 AC |
7070 | -- Internally generated code is never publicly visible except |
7071 | -- for a subprogram that is the implementation of an expression | |
7072 | -- function. In that case the visibility is determined by the | |
7073 | -- last check. | |
7074 | ||
7075 | elsif not Comes_From_Source (Subp_Decl) | |
7076 | and then | |
7077 | (Nkind (Original_Node (Subp_Decl)) /= N_Expression_Function | |
7078 | or else not | |
7079 | Comes_From_Source (Defining_Entity (Subp_Decl))) | |
7080 | then | |
7081 | return False; | |
7082 | ||
7083 | -- Determine whether the subprogram is declared in the visible | |
7084 | -- declarations of the package containing the type. | |
7085 | ||
7086 | else | |
d950f051 AC |
7087 | return List_Containing (Subp_Decl) = |
7088 | Visible_Declarations | |
7089 | (Specification (Unit_Declaration_Node (Scope (Typ)))); | |
ea3c0651 AC |
7090 | end if; |
7091 | end Has_Public_Visibility_Of_Subprogram; | |
7092 | ||
7093 | -- Start of processing for Invariant_Checks_OK | |
7094 | ||
7095 | begin | |
7096 | return | |
7097 | Has_Invariants (Typ) | |
7098 | and then Present (Invariant_Procedure (Typ)) | |
7099 | and then not Has_Null_Body (Invariant_Procedure (Typ)) | |
7100 | and then Has_Public_Visibility_Of_Subprogram; | |
7101 | end Invariant_Checks_OK; | |
7102 | ||
ea3c0651 AC |
7103 | -- Local variables |
7104 | ||
ff1f1705 AC |
7105 | Loc : constant Source_Ptr := Sloc (N); |
7106 | -- Source location of subprogram contract | |
7107 | ||
ea3c0651 AC |
7108 | Formal : Entity_Id; |
7109 | Typ : Entity_Id; | |
7110 | ||
7111 | -- Start of processing for Add_Invariant_And_Predicate_Checks | |
7112 | ||
7113 | begin | |
7114 | Result := Empty; | |
7115 | ||
7116 | -- Do not generate any checks if no code is being generated | |
7117 | ||
7118 | if not Expander_Active then | |
7119 | return; | |
7120 | end if; | |
7121 | ||
7122 | -- Process the result of a function | |
7123 | ||
7124 | if Ekind_In (Subp_Id, E_Function, E_Generic_Function) then | |
7125 | Typ := Etype (Subp_Id); | |
7126 | ||
7127 | -- Generate _Result which is used in procedure _Postconditions to | |
7128 | -- verify the return value. | |
7129 | ||
7130 | Result := Make_Defining_Identifier (Loc, Name_uResult); | |
7131 | Set_Etype (Result, Typ); | |
7132 | ||
7133 | -- Add an invariant check when the return type has invariants and | |
7134 | -- the related function is visible to the outside. | |
7135 | ||
7136 | if Invariant_Checks_OK (Typ) then | |
7137 | Append_Enabled_Item | |
7138 | (Item => | |
7139 | Make_Invariant_Call (New_Occurrence_Of (Result, Loc)), | |
7140 | List => Stmts); | |
7141 | end if; | |
7142 | ||
7143 | -- Add an invariant check when the return type is an access to a | |
7144 | -- type with invariants. | |
7145 | ||
7146 | Add_Invariant_Access_Checks (Result); | |
7147 | end if; | |
7148 | ||
7149 | -- Add invariant and predicates for all formals that qualify | |
7150 | ||
7151 | Formal := First_Formal (Subp_Id); | |
7152 | while Present (Formal) loop | |
7153 | Typ := Etype (Formal); | |
7154 | ||
7155 | if Ekind (Formal) /= E_In_Parameter | |
7156 | or else Is_Access_Type (Typ) | |
7157 | then | |
7158 | if Invariant_Checks_OK (Typ) then | |
7159 | Append_Enabled_Item | |
7160 | (Item => | |
7161 | Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)), | |
7162 | List => Stmts); | |
7163 | end if; | |
7164 | ||
7165 | Add_Invariant_Access_Checks (Formal); | |
7166 | ||
28e18b4f AC |
7167 | -- Note: we used to add predicate checks for OUT and IN OUT |
7168 | -- formals here, but that was misguided, since such checks are | |
7169 | -- performed on the caller side, based on the predicate of the | |
7170 | -- actual, rather than the predicate of the formal. | |
7171 | ||
ea3c0651 AC |
7172 | end if; |
7173 | ||
7174 | Next_Formal (Formal); | |
7175 | end loop; | |
7176 | end Add_Invariant_And_Predicate_Checks; | |
7177 | ||
7178 | ------------------------- | |
7179 | -- Append_Enabled_Item -- | |
7180 | ------------------------- | |
7181 | ||
7182 | procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id) is | |
7183 | begin | |
7184 | -- Do not chain ignored or disabled pragmas | |
7185 | ||
7186 | if Nkind (Item) = N_Pragma | |
7187 | and then (Is_Ignored (Item) or else Is_Disabled (Item)) | |
7188 | then | |
7189 | null; | |
7190 | ||
5b6f12c7 | 7191 | -- Otherwise, add the item |
ea3c0651 AC |
7192 | |
7193 | else | |
7194 | if No (List) then | |
7195 | List := New_List; | |
7196 | end if; | |
7197 | ||
cbee4f74 AC |
7198 | -- If the pragma is a conjunct in a composite postcondition, it |
7199 | -- has been processed in reverse order. In the postcondition body | |
7200 | -- if must appear before the others. | |
7201 | ||
7202 | if Nkind (Item) = N_Pragma | |
7203 | and then From_Aspect_Specification (Item) | |
7204 | and then Split_PPC (Item) | |
7205 | then | |
7206 | Prepend (Item, List); | |
7207 | else | |
7208 | Append (Item, List); | |
7209 | end if; | |
ea3c0651 AC |
7210 | end if; |
7211 | end Append_Enabled_Item; | |
7212 | ||
7213 | ------------------------------------ | |
7214 | -- Build_Postconditions_Procedure -- | |
7215 | ------------------------------------ | |
7216 | ||
7217 | procedure Build_Postconditions_Procedure | |
7218 | (Subp_Id : Entity_Id; | |
7219 | Stmts : List_Id; | |
7220 | Result : Entity_Id) | |
7221 | is | |
8e1e62e3 AC |
7222 | procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id); |
7223 | -- Insert node Stmt before the first source declaration of the | |
7224 | -- related subprogram's body. If no such declaration exists, Stmt | |
7225 | -- becomes the last declaration. | |
ea3c0651 | 7226 | |
8e1e62e3 AC |
7227 | -------------------------------------------- |
7228 | -- Insert_Before_First_Source_Declaration -- | |
7229 | -------------------------------------------- | |
ea3c0651 | 7230 | |
8e1e62e3 AC |
7231 | procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id) is |
7232 | Decls : constant List_Id := Declarations (N); | |
7233 | Decl : Node_Id; | |
ea3c0651 AC |
7234 | |
7235 | begin | |
8e1e62e3 AC |
7236 | -- Inspect the declarations of the related subprogram body looking |
7237 | -- for the first source declaration. | |
7238 | ||
7239 | if Present (Decls) then | |
7240 | Decl := First (Decls); | |
7241 | while Present (Decl) loop | |
7242 | if Comes_From_Source (Decl) then | |
7243 | Insert_Before (Decl, Stmt); | |
7244 | return; | |
7245 | end if; | |
7246 | ||
7247 | Next (Decl); | |
7248 | end loop; | |
7249 | ||
7250 | -- If we get there, then the subprogram body lacks any source | |
7251 | -- declarations. The body of _Postconditions now acts as the | |
7252 | -- last declaration. | |
7253 | ||
7254 | Append (Stmt, Decls); | |
7255 | ||
ea3c0651 AC |
7256 | -- Ensure that the body has a declaration list |
7257 | ||
8e1e62e3 AC |
7258 | else |
7259 | Set_Declarations (N, New_List (Stmt)); | |
ea3c0651 | 7260 | end if; |
8e1e62e3 | 7261 | end Insert_Before_First_Source_Declaration; |
ea3c0651 AC |
7262 | |
7263 | -- Local variables | |
7264 | ||
7265 | Loc : constant Source_Ptr := Sloc (N); | |
7266 | Params : List_Id := No_List; | |
7267 | Proc_Id : Entity_Id; | |
7268 | ||
7269 | -- Start of processing for Build_Postconditions_Procedure | |
7270 | ||
7271 | begin | |
7272 | -- Do not create the routine if no code is being generated | |
7273 | ||
7274 | if not Expander_Active then | |
7275 | return; | |
7276 | ||
7277 | -- Nothing to do if there are no actions to check on exit | |
7278 | ||
7279 | elsif No (Stmts) then | |
7280 | return; | |
7281 | end if; | |
7282 | ||
7283 | Proc_Id := Make_Defining_Identifier (Loc, Name_uPostconditions); | |
7284 | ||
7285 | -- The related subprogram is a function, create the specification of | |
7286 | -- parameter _Result. | |
7287 | ||
7288 | if Present (Result) then | |
7289 | Params := New_List ( | |
7290 | Make_Parameter_Specification (Loc, | |
7291 | Defining_Identifier => Result, | |
7292 | Parameter_Type => | |
e4494292 | 7293 | New_Occurrence_Of (Etype (Result), Loc))); |
ea3c0651 AC |
7294 | end if; |
7295 | ||
8e1e62e3 AC |
7296 | -- Insert _Postconditions before the first source declaration of the |
7297 | -- body. This ensures that the body will not cause any premature | |
7298 | -- freezing as it may mention types: | |
ea3c0651 AC |
7299 | |
7300 | -- procedure Proc (Obj : Array_Typ) is | |
7301 | -- procedure _postconditions is | |
7302 | -- begin | |
7303 | -- ... Obj ... | |
7304 | -- end _postconditions; | |
7305 | ||
7306 | -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1)); | |
7307 | -- begin | |
7308 | ||
7309 | -- In the example above, Obj is of type T but the incorrect placement | |
7310 | -- of _Postconditions will cause a crash in gigi due to an out of | |
7311 | -- order reference. The body of _Postconditions must be placed after | |
7312 | -- the declaration of Temp to preserve correct visibility. | |
7313 | ||
ff1f1705 AC |
7314 | -- Note that we set an explicit End_Label in order to override the |
7315 | -- sloc of the implicit RETURN statement, and prevent it from | |
7316 | -- inheriting the sloc of one of the postconditions: this would cause | |
7317 | -- confusing debug info to be produced, interfering with coverage | |
7318 | -- analysis tools. | |
7319 | ||
8e1e62e3 | 7320 | Insert_Before_First_Source_Declaration ( |
ea3c0651 AC |
7321 | Make_Subprogram_Body (Loc, |
7322 | Specification => | |
7323 | Make_Procedure_Specification (Loc, | |
7324 | Defining_Unit_Name => Proc_Id, | |
7325 | Parameter_Specifications => Params), | |
7326 | ||
7327 | Declarations => Empty_List, | |
7328 | Handled_Statement_Sequence => | |
ff1f1705 AC |
7329 | Make_Handled_Sequence_Of_Statements (Loc, |
7330 | Statements => Stmts, | |
7331 | End_Label => Make_Identifier (Loc, Chars (Proc_Id))))); | |
ea3c0651 AC |
7332 | |
7333 | -- Set the attributes of the related subprogram to capture the | |
7334 | -- generated procedure. | |
7335 | ||
7336 | if Ekind_In (Subp_Id, E_Generic_Procedure, E_Procedure) then | |
7337 | Set_Postcondition_Proc (Subp_Id, Proc_Id); | |
7338 | end if; | |
7339 | ||
7340 | Set_Has_Postconditions (Subp_Id); | |
7341 | end Build_Postconditions_Procedure; | |
7342 | ||
7343 | ----------------------------------- | |
7344 | -- Build_Pragma_Check_Equivalent -- | |
7345 | ----------------------------------- | |
7346 | ||
7347 | function Build_Pragma_Check_Equivalent | |
7348 | (Prag : Node_Id; | |
7349 | Subp_Id : Entity_Id := Empty; | |
7350 | Inher_Id : Entity_Id := Empty) return Node_Id | |
7351 | is | |
7352 | Loc : constant Source_Ptr := Sloc (Prag); | |
7353 | Prag_Nam : constant Name_Id := Pragma_Name (Prag); | |
7354 | Check_Prag : Node_Id; | |
7355 | Formals_Map : Elist_Id; | |
7356 | Inher_Formal : Entity_Id; | |
7357 | Msg_Arg : Node_Id; | |
7358 | Nam : Name_Id; | |
7359 | Subp_Formal : Entity_Id; | |
7360 | ||
7361 | begin | |
7362 | Formals_Map := No_Elist; | |
7363 | ||
7364 | -- When the pre- or postcondition is inherited, map the formals of | |
7365 | -- the inherited subprogram to those of the current subprogram. | |
7366 | ||
7367 | if Present (Inher_Id) then | |
7368 | pragma Assert (Present (Subp_Id)); | |
7369 | ||
7370 | Formals_Map := New_Elmt_List; | |
7371 | ||
7372 | -- Create a relation <inherited formal> => <subprogram formal> | |
7373 | ||
7374 | Inher_Formal := First_Formal (Inher_Id); | |
7375 | Subp_Formal := First_Formal (Subp_Id); | |
7376 | while Present (Inher_Formal) and then Present (Subp_Formal) loop | |
7377 | Append_Elmt (Inher_Formal, Formals_Map); | |
7378 | Append_Elmt (Subp_Formal, Formals_Map); | |
7379 | ||
7380 | Next_Formal (Inher_Formal); | |
7381 | Next_Formal (Subp_Formal); | |
7382 | end loop; | |
7383 | end if; | |
7384 | ||
7385 | -- Copy the original pragma while performing substitutions (if | |
7386 | -- applicable). | |
7387 | ||
7388 | Check_Prag := | |
7389 | New_Copy_Tree | |
7390 | (Source => Prag, | |
7391 | Map => Formals_Map, | |
7392 | New_Scope => Current_Scope); | |
7393 | ||
7394 | -- Mark the pragma as being internally generated and reset the | |
7395 | -- Analyzed flag. | |
7396 | ||
7397 | Set_Comes_From_Source (Check_Prag, False); | |
7398 | Set_Analyzed (Check_Prag, False); | |
7399 | ||
7400 | -- For a postcondition pragma within a generic, preserve the pragma | |
7401 | -- for later expansion. This is also used when an error was detected, | |
7402 | -- thus setting Expander_Active to False. | |
7403 | ||
7404 | if Prag_Nam = Name_Postcondition and then not Expander_Active then | |
7405 | return Check_Prag; | |
7406 | end if; | |
7407 | ||
7408 | if Present (Corresponding_Aspect (Prag)) then | |
7409 | Nam := Chars (Identifier (Corresponding_Aspect (Prag))); | |
7410 | else | |
7411 | Nam := Prag_Nam; | |
7412 | end if; | |
7413 | ||
7414 | -- Convert the copy into pragma Check by correcting the name and | |
7415 | -- adding a check_kind argument. | |
7416 | ||
7417 | Set_Pragma_Identifier | |
7418 | (Check_Prag, Make_Identifier (Loc, Name_Check)); | |
7419 | ||
7420 | Prepend_To (Pragma_Argument_Associations (Check_Prag), | |
7421 | Make_Pragma_Argument_Association (Loc, | |
7422 | Expression => Make_Identifier (Loc, Nam))); | |
7423 | ||
7424 | -- Update the error message when the pragma is inherited | |
7425 | ||
7426 | if Present (Inher_Id) then | |
7427 | Msg_Arg := Last (Pragma_Argument_Associations (Check_Prag)); | |
7428 | ||
7429 | if Chars (Msg_Arg) = Name_Message then | |
7430 | String_To_Name_Buffer (Strval (Expression (Msg_Arg))); | |
7431 | ||
7432 | -- Insert "inherited" to improve the error message | |
7433 | ||
7434 | if Name_Buffer (1 .. 8) = "failed p" then | |
7435 | Insert_Str_In_Name_Buffer ("inherited ", 8); | |
7436 | Set_Strval (Expression (Msg_Arg), String_From_Name_Buffer); | |
7437 | end if; | |
7438 | end if; | |
7439 | end if; | |
7440 | ||
7441 | return Check_Prag; | |
7442 | end Build_Pragma_Check_Equivalent; | |
7443 | ||
7444 | --------------------------------- | |
7445 | -- Collect_Body_Postconditions -- | |
7446 | --------------------------------- | |
7447 | ||
7448 | procedure Collect_Body_Postconditions (Stmts : in out List_Id) is | |
7449 | procedure Collect_Body_Postconditions_Of_Kind (Post_Nam : Name_Id); | |
e917e3b8 | 7450 | -- Process all postconditions of the kind denoted by Post_Nam |
ea3c0651 AC |
7451 | |
7452 | ----------------------------------------- | |
7453 | -- Collect_Body_Postconditions_Of_Kind -- | |
7454 | ----------------------------------------- | |
7455 | ||
7456 | procedure Collect_Body_Postconditions_Of_Kind (Post_Nam : Name_Id) is | |
5f24a82a HK |
7457 | procedure Collect_Body_Postconditions_In_Decls |
7458 | (First_Decl : Node_Id); | |
7459 | -- Process all postconditions found in a declarative list starting | |
7460 | -- with declaration First_Decl. | |
ea3c0651 | 7461 | |
5f24a82a HK |
7462 | ------------------------------------------ |
7463 | -- Collect_Body_Postconditions_In_Decls -- | |
7464 | ------------------------------------------ | |
ea3c0651 | 7465 | |
5f24a82a HK |
7466 | procedure Collect_Body_Postconditions_In_Decls |
7467 | (First_Decl : Node_Id) | |
7468 | is | |
7469 | Check_Prag : Node_Id; | |
7470 | Decl : Node_Id; | |
ea3c0651 | 7471 | |
5f24a82a HK |
7472 | begin |
7473 | -- Inspect the declarative list looking for a pragma that | |
7474 | -- matches the desired name. | |
ea3c0651 | 7475 | |
5f24a82a HK |
7476 | Decl := First_Decl; |
7477 | while Present (Decl) loop | |
ea3c0651 | 7478 | |
5f24a82a | 7479 | -- Note that non-matching pragmas are skipped |
ea3c0651 | 7480 | |
5f24a82a HK |
7481 | if Nkind (Decl) = N_Pragma then |
7482 | if Pragma_Name (Decl) = Post_Nam then | |
7483 | if not Analyzed (Decl) then | |
7484 | Analyze (Decl); | |
7485 | end if; | |
7486 | ||
7487 | Check_Prag := Build_Pragma_Check_Equivalent (Decl); | |
7488 | ||
7489 | if Expander_Active then | |
7490 | Append_Enabled_Item | |
7491 | (Item => Check_Prag, | |
7492 | List => Stmts); | |
7493 | ||
e917e3b8 | 7494 | -- If analyzing a generic unit, save pragma for later |
5f24a82a HK |
7495 | |
7496 | else | |
7497 | Prepend_To_Declarations (Check_Prag); | |
7498 | end if; | |
ea3c0651 | 7499 | end if; |
5f24a82a HK |
7500 | |
7501 | -- Skip internally generated code | |
7502 | ||
7503 | elsif not Comes_From_Source (Decl) then | |
7504 | null; | |
7505 | ||
7506 | -- Postcondition pragmas are usually grouped together. There | |
7507 | -- is no need to inspect the whole declarative list. | |
7508 | ||
7509 | else | |
7510 | exit; | |
ea3c0651 AC |
7511 | end if; |
7512 | ||
5f24a82a HK |
7513 | Next (Decl); |
7514 | end loop; | |
7515 | end Collect_Body_Postconditions_In_Decls; | |
ea3c0651 | 7516 | |
5f24a82a | 7517 | -- Local variables |
ea3c0651 | 7518 | |
5f24a82a | 7519 | Unit_Decl : constant Node_Id := Parent (N); |
ea3c0651 | 7520 | |
5f24a82a | 7521 | -- Start of processing for Collect_Body_Postconditions_Of_Kind |
ea3c0651 | 7522 | |
5f24a82a HK |
7523 | begin |
7524 | pragma Assert (Nam_In (Post_Nam, Name_Postcondition, | |
7525 | Name_Refined_Post)); | |
7526 | ||
7527 | -- Inspect the declarations of the subprogram body looking for a | |
7528 | -- pragma that matches the desired name. | |
7529 | ||
7530 | Collect_Body_Postconditions_In_Decls | |
7531 | (First_Decl => First (Declarations (N))); | |
7532 | ||
7533 | -- The subprogram body being processed is actually the proper body | |
7534 | -- of a stub with a corresponding spec. The subprogram stub may | |
7535 | -- carry a postcondition pragma in which case it must be taken | |
7536 | -- into account. The pragma appears after the stub. | |
7537 | ||
7538 | if Present (Spec_Id) and then Nkind (Unit_Decl) = N_Subunit then | |
7539 | Collect_Body_Postconditions_In_Decls | |
7540 | (First_Decl => Next (Corresponding_Stub (Unit_Decl))); | |
7541 | end if; | |
ea3c0651 AC |
7542 | end Collect_Body_Postconditions_Of_Kind; |
7543 | ||
7544 | -- Start of processing for Collect_Body_Postconditions | |
7545 | ||
7546 | begin | |
7547 | Collect_Body_Postconditions_Of_Kind (Name_Refined_Post); | |
7548 | Collect_Body_Postconditions_Of_Kind (Name_Postcondition); | |
7549 | end Collect_Body_Postconditions; | |
7550 | ||
7551 | --------------------------------- | |
7552 | -- Collect_Spec_Postconditions -- | |
7553 | --------------------------------- | |
7554 | ||
7555 | procedure Collect_Spec_Postconditions | |
7556 | (Subp_Id : Entity_Id; | |
7557 | Stmts : in out List_Id) | |
7558 | is | |
7559 | Inher_Subps : constant Subprogram_List := | |
7560 | Inherited_Subprograms (Subp_Id); | |
7561 | Check_Prag : Node_Id; | |
7562 | Prag : Node_Id; | |
7563 | Inher_Subp_Id : Entity_Id; | |
7564 | ||
7565 | begin | |
7566 | -- Process the contract of the spec | |
7567 | ||
7568 | Prag := Pre_Post_Conditions (Contract (Subp_Id)); | |
7569 | while Present (Prag) loop | |
7570 | if Pragma_Name (Prag) = Name_Postcondition then | |
7571 | Check_Prag := Build_Pragma_Check_Equivalent (Prag); | |
7572 | ||
7573 | if Expander_Active then | |
7574 | Append_Enabled_Item | |
7575 | (Item => Check_Prag, | |
7576 | List => Stmts); | |
7577 | ||
7578 | -- When analyzing a generic unit, save the pragma for later | |
7579 | ||
7580 | else | |
7581 | Prepend_To_Declarations (Check_Prag); | |
7582 | end if; | |
7583 | end if; | |
7584 | ||
7585 | Prag := Next_Pragma (Prag); | |
7586 | end loop; | |
7587 | ||
7588 | -- Process the contracts of all inherited subprograms, looking for | |
7589 | -- class-wide postconditions. | |
7590 | ||
7591 | for Index in Inher_Subps'Range loop | |
7592 | Inher_Subp_Id := Inher_Subps (Index); | |
7593 | ||
7594 | Prag := Pre_Post_Conditions (Contract (Inher_Subp_Id)); | |
7595 | while Present (Prag) loop | |
7596 | if Pragma_Name (Prag) = Name_Postcondition | |
7597 | and then Class_Present (Prag) | |
7598 | then | |
7599 | Check_Prag := | |
7600 | Build_Pragma_Check_Equivalent | |
7601 | (Prag => Prag, | |
7602 | Subp_Id => Subp_Id, | |
7603 | Inher_Id => Inher_Subp_Id); | |
7604 | ||
7605 | if Expander_Active then | |
7606 | Append_Enabled_Item | |
7607 | (Item => Check_Prag, | |
7608 | List => Stmts); | |
7609 | ||
7610 | -- When analyzing a generic unit, save the pragma for later | |
7611 | ||
7612 | else | |
7613 | Prepend_To_Declarations (Check_Prag); | |
7614 | end if; | |
7615 | end if; | |
7616 | ||
7617 | Prag := Next_Pragma (Prag); | |
7618 | end loop; | |
7619 | end loop; | |
7620 | end Collect_Spec_Postconditions; | |
7621 | ||
7622 | -------------------------------- | |
7623 | -- Collect_Spec_Preconditions -- | |
7624 | -------------------------------- | |
7625 | ||
7626 | procedure Collect_Spec_Preconditions (Subp_Id : Entity_Id) is | |
5f24a82a HK |
7627 | Class_Pre : Node_Id := Empty; |
7628 | -- The sole class-wide precondition pragma that applies to the | |
7629 | -- subprogram. | |
7630 | ||
7631 | procedure Add_Or_Save_Precondition (Prag : Node_Id); | |
7632 | -- Save a class-wide precondition or add a regulat precondition to | |
7633 | -- the declarative list of the body. | |
7634 | ||
ea3c0651 AC |
7635 | procedure Merge_Preconditions (From : Node_Id; Into : Node_Id); |
7636 | -- Merge two class-wide preconditions by "or else"-ing them. The | |
7637 | -- changes are accumulated in parameter Into. Update the error | |
7638 | -- message of Into. | |
7639 | ||
5f24a82a HK |
7640 | ------------------------------ |
7641 | -- Add_Or_Save_Precondition -- | |
7642 | ------------------------------ | |
7643 | ||
7644 | procedure Add_Or_Save_Precondition (Prag : Node_Id) is | |
7645 | Check_Prag : Node_Id; | |
7646 | ||
7647 | begin | |
7648 | Check_Prag := Build_Pragma_Check_Equivalent (Prag); | |
7649 | ||
7650 | -- Save the sole class-wide precondition (if any) for the next | |
7651 | -- step where it will be merged with inherited preconditions. | |
7652 | ||
7653 | if Class_Present (Prag) then | |
7654 | pragma Assert (No (Class_Pre)); | |
7655 | Class_Pre := Check_Prag; | |
7656 | ||
7657 | -- Accumulate the corresponding Check pragmas to the top of the | |
7658 | -- declarations. Prepending the items ensures that they will be | |
7659 | -- evaluated in their original order. | |
7660 | ||
7661 | else | |
7662 | Prepend_To_Declarations (Check_Prag); | |
7663 | end if; | |
7664 | end Add_Or_Save_Precondition; | |
7665 | ||
ea3c0651 AC |
7666 | ------------------------- |
7667 | -- Merge_Preconditions -- | |
7668 | ------------------------- | |
7669 | ||
7670 | procedure Merge_Preconditions (From : Node_Id; Into : Node_Id) is | |
7671 | function Expression_Arg (Prag : Node_Id) return Node_Id; | |
7672 | -- Return the boolean expression argument of a precondition while | |
7673 | -- updating its parenteses count for the subsequent merge. | |
7674 | ||
7675 | function Message_Arg (Prag : Node_Id) return Node_Id; | |
7676 | -- Return the message argument of a precondition | |
7677 | ||
7678 | -------------------- | |
7679 | -- Expression_Arg -- | |
7680 | -------------------- | |
7681 | ||
7682 | function Expression_Arg (Prag : Node_Id) return Node_Id is | |
7683 | Args : constant List_Id := Pragma_Argument_Associations (Prag); | |
7684 | Arg : constant Node_Id := Get_Pragma_Arg (Next (First (Args))); | |
7685 | ||
7686 | begin | |
7687 | if Paren_Count (Arg) = 0 then | |
7688 | Set_Paren_Count (Arg, 1); | |
7689 | end if; | |
7690 | ||
7691 | return Arg; | |
7692 | end Expression_Arg; | |
7693 | ||
7694 | ----------------- | |
7695 | -- Message_Arg -- | |
7696 | ----------------- | |
7697 | ||
7698 | function Message_Arg (Prag : Node_Id) return Node_Id is | |
7699 | Args : constant List_Id := Pragma_Argument_Associations (Prag); | |
7700 | begin | |
7701 | return Get_Pragma_Arg (Last (Args)); | |
7702 | end Message_Arg; | |
7703 | ||
7704 | -- Local variables | |
7705 | ||
7706 | From_Expr : constant Node_Id := Expression_Arg (From); | |
7707 | From_Msg : constant Node_Id := Message_Arg (From); | |
7708 | Into_Expr : constant Node_Id := Expression_Arg (Into); | |
7709 | Into_Msg : constant Node_Id := Message_Arg (Into); | |
7710 | Loc : constant Source_Ptr := Sloc (Into); | |
7711 | ||
7712 | -- Start of processing for Merge_Preconditions | |
7713 | ||
7714 | begin | |
7715 | -- Merge the two preconditions by "or else"-ing them | |
7716 | ||
7717 | Rewrite (Into_Expr, | |
7718 | Make_Or_Else (Loc, | |
7719 | Right_Opnd => Relocate_Node (Into_Expr), | |
7720 | Left_Opnd => From_Expr)); | |
7721 | ||
7722 | -- Merge the two error messages to produce a single message of the | |
7723 | -- form: | |
7724 | ||
7725 | -- failed precondition from ... | |
7726 | -- also failed inherited precondition from ... | |
7727 | ||
7728 | if not Exception_Locations_Suppressed then | |
7729 | Start_String (Strval (Into_Msg)); | |
7730 | Store_String_Char (ASCII.LF); | |
7731 | Store_String_Chars (" also "); | |
7732 | Store_String_Chars (Strval (From_Msg)); | |
7733 | ||
7734 | Set_Strval (Into_Msg, End_String); | |
7735 | end if; | |
7736 | end Merge_Preconditions; | |
7737 | ||
7738 | -- Local variables | |
7739 | ||
7740 | Inher_Subps : constant Subprogram_List := | |
7741 | Inherited_Subprograms (Subp_Id); | |
5f24a82a | 7742 | Subp_Decl : constant Node_Id := Parent (Parent (Subp_Id)); |
ea3c0651 | 7743 | Check_Prag : Node_Id; |
5f24a82a | 7744 | Decl : Node_Id; |
ea3c0651 AC |
7745 | Inher_Subp_Id : Entity_Id; |
7746 | Prag : Node_Id; | |
7747 | ||
7748 | -- Start of processing for Collect_Spec_Preconditions | |
7749 | ||
7750 | begin | |
7751 | -- Process the contract of the spec | |
7752 | ||
7753 | Prag := Pre_Post_Conditions (Contract (Subp_Id)); | |
7754 | while Present (Prag) loop | |
7755 | if Pragma_Name (Prag) = Name_Precondition then | |
5f24a82a HK |
7756 | Add_Or_Save_Precondition (Prag); |
7757 | end if; | |
ea3c0651 | 7758 | |
5f24a82a HK |
7759 | Prag := Next_Pragma (Prag); |
7760 | end loop; | |
ea3c0651 | 7761 | |
5f24a82a HK |
7762 | -- The subprogram declaration being processed is actually a body |
7763 | -- stub. The stub may carry a precondition pragma in which case it | |
7764 | -- must be taken into account. The pragma appears after the stub. | |
ea3c0651 | 7765 | |
5f24a82a HK |
7766 | if Nkind (Subp_Decl) = N_Subprogram_Body_Stub then |
7767 | ||
7768 | -- Inspect the declarations following the body stub | |
7769 | ||
7770 | Decl := Next (Subp_Decl); | |
7771 | while Present (Decl) loop | |
7772 | ||
7773 | -- Note that non-matching pragmas are skipped | |
7774 | ||
7775 | if Nkind (Decl) = N_Pragma then | |
7776 | if Pragma_Name (Decl) = Name_Precondition then | |
7777 | if not Analyzed (Decl) then | |
7778 | Analyze (Decl); | |
7779 | end if; | |
7780 | ||
7781 | Add_Or_Save_Precondition (Decl); | |
7782 | end if; | |
7783 | ||
7784 | -- Skip internally generated code | |
7785 | ||
7786 | elsif not Comes_From_Source (Decl) then | |
7787 | null; | |
7788 | ||
7789 | -- Preconditions are usually grouped together. There is no need | |
7790 | -- to inspect the whole declarative list. | |
ea3c0651 AC |
7791 | |
7792 | else | |
5f24a82a | 7793 | exit; |
ea3c0651 | 7794 | end if; |
ea3c0651 | 7795 | |
5f24a82a HK |
7796 | Next (Decl); |
7797 | end loop; | |
7798 | end if; | |
ea3c0651 AC |
7799 | |
7800 | -- Process the contracts of all inherited subprograms, looking for | |
7801 | -- class-wide preconditions. | |
7802 | ||
7803 | for Index in Inher_Subps'Range loop | |
7804 | Inher_Subp_Id := Inher_Subps (Index); | |
7805 | ||
7806 | Prag := Pre_Post_Conditions (Contract (Inher_Subp_Id)); | |
7807 | while Present (Prag) loop | |
7808 | if Pragma_Name (Prag) = Name_Precondition | |
7809 | and then Class_Present (Prag) | |
7810 | then | |
7811 | Check_Prag := | |
7812 | Build_Pragma_Check_Equivalent | |
7813 | (Prag => Prag, | |
7814 | Subp_Id => Subp_Id, | |
7815 | Inher_Id => Inher_Subp_Id); | |
7816 | ||
7817 | -- The spec or an inherited subprogram already yielded a | |
7818 | -- class-wide precondition. Merge the existing precondition | |
7819 | -- with the current one using "or else". | |
7820 | ||
7821 | if Present (Class_Pre) then | |
7822 | Merge_Preconditions (Check_Prag, Class_Pre); | |
7823 | else | |
7824 | Class_Pre := Check_Prag; | |
7825 | end if; | |
7826 | end if; | |
7827 | ||
7828 | Prag := Next_Pragma (Prag); | |
7829 | end loop; | |
7830 | end loop; | |
7831 | ||
7832 | -- Add the merged class-wide preconditions (if any) | |
7833 | ||
7834 | if Present (Class_Pre) then | |
7835 | Prepend_To_Declarations (Class_Pre); | |
7836 | end if; | |
7837 | end Collect_Spec_Preconditions; | |
7838 | ||
7839 | ----------------------------- | |
7840 | -- Prepend_To_Declarations -- | |
7841 | ----------------------------- | |
7842 | ||
7843 | procedure Prepend_To_Declarations (Item : Node_Id) is | |
7844 | Decls : List_Id := Declarations (N); | |
7845 | ||
7846 | begin | |
7847 | -- Ensure that the body has a declarative list | |
7848 | ||
7849 | if No (Decls) then | |
7850 | Decls := New_List; | |
7851 | Set_Declarations (N, Decls); | |
7852 | end if; | |
7853 | ||
7854 | Prepend_To (Decls, Item); | |
7855 | end Prepend_To_Declarations; | |
7856 | ||
7857 | ---------------------------- | |
7858 | -- Process_Contract_Cases -- | |
7859 | ---------------------------- | |
7860 | ||
7861 | procedure Process_Contract_Cases | |
7862 | (Subp_Id : Entity_Id; | |
7863 | Stmts : in out List_Id) | |
7864 | is | |
7865 | Prag : Node_Id; | |
7866 | ||
7867 | begin | |
7868 | -- Do not build the Contract_Cases circuitry if no code is being | |
7869 | -- generated. | |
7870 | ||
7871 | if not Expander_Active then | |
3cd4a210 | 7872 | return; |
ea3c0651 AC |
7873 | end if; |
7874 | ||
7875 | Prag := Contract_Test_Cases (Contract (Subp_Id)); | |
7876 | while Present (Prag) loop | |
7877 | if Pragma_Name (Prag) = Name_Contract_Cases then | |
7878 | Expand_Contract_Cases | |
7879 | (CCs => Prag, | |
7880 | Subp_Id => Subp_Id, | |
7881 | Decls => Declarations (N), | |
7882 | Stmts => Stmts); | |
7883 | end if; | |
7884 | ||
7885 | Prag := Next_Pragma (Prag); | |
7886 | end loop; | |
7887 | end Process_Contract_Cases; | |
7888 | ||
7889 | -- Local variables | |
7890 | ||
7891 | Post_Stmts : List_Id := No_List; | |
7892 | Result : Entity_Id; | |
7893 | Subp_Id : Entity_Id; | |
7894 | ||
7895 | -- Start of processing for Expand_Subprogram_Contract | |
7896 | ||
7897 | begin | |
7898 | if Present (Spec_Id) then | |
7899 | Subp_Id := Spec_Id; | |
7900 | else | |
7901 | Subp_Id := Body_Id; | |
7902 | end if; | |
7903 | ||
7904 | -- Do not process a predicate function as its body will end up with a | |
7905 | -- recursive call to itself and blow up the stack. | |
7906 | ||
7907 | if Ekind (Subp_Id) = E_Function | |
7908 | and then Is_Predicate_Function (Subp_Id) | |
7909 | then | |
7910 | return; | |
7911 | ||
7912 | -- Do not process TSS subprograms | |
7913 | ||
7914 | elsif Get_TSS_Name (Subp_Id) /= TSS_Null then | |
7915 | return; | |
7916 | end if; | |
7917 | ||
7918 | -- The expansion of a subprogram contract involves the relocation of | |
7919 | -- various contract assertions to the declarations of the body in a | |
7920 | -- particular order. The order is as follows: | |
7921 | ||
7922 | -- function Example (...) return ... is | |
7923 | -- procedure _Postconditions (...) is | |
7924 | -- begin | |
7925 | -- <refined postconditions from body> | |
7926 | -- <postconditions from body> | |
7927 | -- <postconditions from spec> | |
7928 | -- <inherited postconditions> | |
f1bd0415 | 7929 | -- <contract case consequences> |
ea3c0651 AC |
7930 | -- <invariant check of function result (if applicable)> |
7931 | -- <invariant and predicate checks of parameters> | |
7932 | -- end _Postconditions; | |
7933 | ||
7934 | -- <inherited preconditions> | |
7935 | -- <preconditions from spec> | |
7936 | -- <preconditions from body> | |
7937 | -- <refined preconditions from body> | |
f1bd0415 | 7938 | -- <contract case conditions> |
ea3c0651 AC |
7939 | |
7940 | -- <source declarations> | |
7941 | -- begin | |
7942 | -- <source statements> | |
7943 | ||
7944 | -- _Preconditions (Result); | |
7945 | -- return Result; | |
7946 | -- end Example; | |
7947 | ||
7948 | -- Routine _Postconditions holds all contract assertions that must be | |
7949 | -- verified on exit from the related routine. | |
7950 | ||
7951 | -- Collect all [inherited] preconditions from the spec, transform them | |
7952 | -- into Check pragmas and add them to the declarations of the body in | |
7953 | -- the order outlined above. | |
7954 | ||
7955 | if Present (Spec_Id) then | |
7956 | Collect_Spec_Preconditions (Spec_Id); | |
7957 | end if; | |
7958 | ||
7959 | -- Transform all [refined] postconditions of the body into Check | |
7960 | -- pragmas. The resulting pragmas are accumulated in list Post_Stmts. | |
7961 | ||
7962 | Collect_Body_Postconditions (Post_Stmts); | |
7963 | ||
7964 | -- Transform all [inherited] postconditions from the spec into Check | |
7965 | -- pragmas. The resulting pragmas are accumulated in list Post_Stmts. | |
7966 | ||
7967 | if Present (Spec_Id) then | |
7968 | Collect_Spec_Postconditions (Spec_Id, Post_Stmts); | |
7969 | ||
7970 | -- Transform pragma Contract_Cases from the spec into its circuitry | |
7971 | ||
7972 | Process_Contract_Cases (Spec_Id, Post_Stmts); | |
7973 | end if; | |
7974 | ||
7975 | -- Apply invariant and predicate checks on the result of a function (if | |
7976 | -- applicable) and all formals. The resulting checks are accumulated in | |
7977 | -- list Post_Stmts. | |
7978 | ||
7979 | Add_Invariant_And_Predicate_Checks (Subp_Id, Post_Stmts, Result); | |
7980 | ||
7981 | -- Construct procedure _Postconditions | |
7982 | ||
7983 | Build_Postconditions_Procedure (Subp_Id, Post_Stmts, Result); | |
7984 | end Expand_Subprogram_Contract; | |
7985 | ||
02822a92 RD |
7986 | -------------------------------- |
7987 | -- Is_Build_In_Place_Function -- | |
7988 | -------------------------------- | |
70482933 | 7989 | |
02822a92 RD |
7990 | function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is |
7991 | begin | |
5087048c AC |
7992 | -- This function is called from Expand_Subtype_From_Expr during |
7993 | -- semantic analysis, even when expansion is off. In those cases | |
7994 | -- the build_in_place expansion will not take place. | |
b0256cb6 AC |
7995 | |
7996 | if not Expander_Active then | |
7997 | return False; | |
7998 | end if; | |
7999 | ||
02822a92 | 8000 | -- For now we test whether E denotes a function or access-to-function |
5b6f12c7 AC |
8001 | -- type whose result subtype is inherently limited. Later this test |
8002 | -- may be revised to allow composite nonlimited types. Functions with | |
8003 | -- a foreign convention or whose result type has a foreign convention | |
02822a92 RD |
8004 | -- never qualify. |
8005 | ||
b29def53 | 8006 | if Ekind_In (E, E_Function, E_Generic_Function) |
02822a92 RD |
8007 | or else (Ekind (E) = E_Subprogram_Type |
8008 | and then Etype (E) /= Standard_Void_Type) | |
8009 | then | |
4446a13f AC |
8010 | -- Note: If the function has a foreign convention, it cannot build |
8011 | -- its result in place, so you're on your own. On the other hand, | |
8012 | -- if only the return type has a foreign convention, its layout is | |
8013 | -- intended to be compatible with the other language, but the build- | |
8014 | -- in place machinery can ensure that the object is not copied. | |
f937473f | 8015 | |
4446a13f | 8016 | if Has_Foreign_Convention (E) then |
02822a92 | 8017 | return False; |
c8ef728f | 8018 | |
2a31c32b AC |
8019 | -- In Ada 2005 all functions with an inherently limited return type |
8020 | -- must be handled using a build-in-place profile, including the case | |
8021 | -- of a function with a limited interface result, where the function | |
8022 | -- may return objects of nonlimited descendants. | |
7888a6ae | 8023 | |
02822a92 | 8024 | else |
51245e2d | 8025 | return Is_Limited_View (Etype (E)) |
0791fbe9 | 8026 | and then Ada_Version >= Ada_2005 |
f937473f | 8027 | and then not Debug_Flag_Dot_L; |
c8ef728f ES |
8028 | end if; |
8029 | ||
02822a92 RD |
8030 | else |
8031 | return False; | |
8032 | end if; | |
8033 | end Is_Build_In_Place_Function; | |
f4d379b8 | 8034 | |
02822a92 RD |
8035 | ------------------------------------- |
8036 | -- Is_Build_In_Place_Function_Call -- | |
8037 | ------------------------------------- | |
f4d379b8 | 8038 | |
02822a92 RD |
8039 | function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is |
8040 | Exp_Node : Node_Id := N; | |
8041 | Function_Id : Entity_Id; | |
f4d379b8 | 8042 | |
02822a92 | 8043 | begin |
5b6f12c7 AC |
8044 | -- Return False if the expander is currently inactive, since awareness |
8045 | -- of build-in-place treatment is only relevant during expansion. Note | |
8046 | -- that Is_Build_In_Place_Function, which is called as part of this | |
8047 | -- function, is also conditioned this way, but we need to check here as | |
8048 | -- well to avoid blowing up on processing protected calls when expansion | |
8049 | -- is disabled (such as with -gnatc) since those would trip over the | |
8050 | -- raise of Program_Error below. | |
c6d5d1ac | 8051 | |
5114f3ff AC |
8052 | -- In SPARK mode, build-in-place calls are not expanded, so that we |
8053 | -- may end up with a call that is neither resolved to an entity, nor | |
8054 | -- an indirect call. | |
8055 | ||
4460a9bc | 8056 | if not Expander_Active then |
c6d5d1ac AC |
8057 | return False; |
8058 | end if; | |
8059 | ||
19590d70 GD |
8060 | -- Step past qualification or unchecked conversion (the latter can occur |
8061 | -- in cases of calls to 'Input). | |
8062 | ||
94bbf008 AC |
8063 | if Nkind_In (Exp_Node, N_Qualified_Expression, |
8064 | N_Unchecked_Type_Conversion) | |
19590d70 | 8065 | then |
02822a92 RD |
8066 | Exp_Node := Expression (N); |
8067 | end if; | |
758c442c | 8068 | |
02822a92 RD |
8069 | if Nkind (Exp_Node) /= N_Function_Call then |
8070 | return False; | |
3ca505dc | 8071 | |
02822a92 | 8072 | else |
5114f3ff | 8073 | if Is_Entity_Name (Name (Exp_Node)) then |
02822a92 | 8074 | Function_Id := Entity (Name (Exp_Node)); |
758c442c | 8075 | |
94bbf008 AC |
8076 | -- In the case of an explicitly dereferenced call, use the subprogram |
8077 | -- type generated for the dereference. | |
8078 | ||
02822a92 RD |
8079 | elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then |
8080 | Function_Id := Etype (Name (Exp_Node)); | |
2ba1a7c7 | 8081 | |
0812b84e AC |
8082 | -- This may be a call to a protected function. |
8083 | ||
8084 | elsif Nkind (Name (Exp_Node)) = N_Selected_Component then | |
8085 | Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node)))); | |
8086 | ||
2ba1a7c7 AC |
8087 | else |
8088 | raise Program_Error; | |
02822a92 | 8089 | end if; |
758c442c | 8090 | |
02822a92 RD |
8091 | return Is_Build_In_Place_Function (Function_Id); |
8092 | end if; | |
8093 | end Is_Build_In_Place_Function_Call; | |
758c442c | 8094 | |
02822a92 RD |
8095 | ----------------------- |
8096 | -- Freeze_Subprogram -- | |
8097 | ----------------------- | |
758c442c | 8098 | |
02822a92 RD |
8099 | procedure Freeze_Subprogram (N : Node_Id) is |
8100 | Loc : constant Source_Ptr := Sloc (N); | |
3ca505dc | 8101 | |
02822a92 RD |
8102 | procedure Register_Predefined_DT_Entry (Prim : Entity_Id); |
8103 | -- (Ada 2005): Register a predefined primitive in all the secondary | |
8104 | -- dispatch tables of its primitive type. | |
3ca505dc | 8105 | |
f4d379b8 HK |
8106 | ---------------------------------- |
8107 | -- Register_Predefined_DT_Entry -- | |
8108 | ---------------------------------- | |
8109 | ||
8110 | procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is | |
8111 | Iface_DT_Ptr : Elmt_Id; | |
02822a92 | 8112 | Tagged_Typ : Entity_Id; |
f4d379b8 | 8113 | Thunk_Id : Entity_Id; |
7888a6ae | 8114 | Thunk_Code : Node_Id; |
f4d379b8 HK |
8115 | |
8116 | begin | |
02822a92 | 8117 | Tagged_Typ := Find_Dispatching_Type (Prim); |
f4d379b8 | 8118 | |
02822a92 | 8119 | if No (Access_Disp_Table (Tagged_Typ)) |
ce2b6ba5 | 8120 | or else not Has_Interfaces (Tagged_Typ) |
c8ef728f | 8121 | or else not RTE_Available (RE_Interface_Tag) |
f937473f | 8122 | or else Restriction_Active (No_Dispatching_Calls) |
f4d379b8 HK |
8123 | then |
8124 | return; | |
8125 | end if; | |
8126 | ||
1923d2d6 JM |
8127 | -- Skip the first two access-to-dispatch-table pointers since they |
8128 | -- leads to the primary dispatch table (predefined DT and user | |
8129 | -- defined DT). We are only concerned with the secondary dispatch | |
8130 | -- table pointers. Note that the access-to- dispatch-table pointer | |
8131 | -- corresponds to the first implemented interface retrieved below. | |
f4d379b8 | 8132 | |
02822a92 | 8133 | Iface_DT_Ptr := |
1923d2d6 | 8134 | Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ)))); |
f937473f | 8135 | |
7888a6ae | 8136 | while Present (Iface_DT_Ptr) |
df3e68b1 | 8137 | and then Ekind (Node (Iface_DT_Ptr)) = E_Constant |
7888a6ae | 8138 | loop |
ac4d6407 | 8139 | pragma Assert (Has_Thunks (Node (Iface_DT_Ptr))); |
d766cee3 | 8140 | Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code); |
7888a6ae GD |
8141 | |
8142 | if Present (Thunk_Code) then | |
ac4d6407 | 8143 | Insert_Actions_After (N, New_List ( |
7888a6ae GD |
8144 | Thunk_Code, |
8145 | ||
8146 | Build_Set_Predefined_Prim_Op_Address (Loc, | |
54bf19e4 | 8147 | Tag_Node => |
e4494292 | 8148 | New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc), |
54bf19e4 | 8149 | Position => DT_Position (Prim), |
7888a6ae | 8150 | Address_Node => |
70f91180 | 8151 | Unchecked_Convert_To (RTE (RE_Prim_Ptr), |
1923d2d6 | 8152 | Make_Attribute_Reference (Loc, |
e4494292 | 8153 | Prefix => New_Occurrence_Of (Thunk_Id, Loc), |
1923d2d6 | 8154 | Attribute_Name => Name_Unrestricted_Access))), |
ac4d6407 RD |
8155 | |
8156 | Build_Set_Predefined_Prim_Op_Address (Loc, | |
54bf19e4 | 8157 | Tag_Node => |
e4494292 | 8158 | New_Occurrence_Of |
1923d2d6 JM |
8159 | (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))), |
8160 | Loc), | |
54bf19e4 | 8161 | Position => DT_Position (Prim), |
ac4d6407 | 8162 | Address_Node => |
70f91180 | 8163 | Unchecked_Convert_To (RTE (RE_Prim_Ptr), |
1923d2d6 | 8164 | Make_Attribute_Reference (Loc, |
e4494292 | 8165 | Prefix => New_Occurrence_Of (Prim, Loc), |
1923d2d6 | 8166 | Attribute_Name => Name_Unrestricted_Access))))); |
7888a6ae | 8167 | end if; |
f4d379b8 | 8168 | |
1923d2d6 JM |
8169 | -- Skip the tag of the predefined primitives dispatch table |
8170 | ||
8171 | Next_Elmt (Iface_DT_Ptr); | |
8172 | pragma Assert (Has_Thunks (Node (Iface_DT_Ptr))); | |
8173 | ||
54bf19e4 | 8174 | -- Skip tag of the no-thunks dispatch table |
1923d2d6 JM |
8175 | |
8176 | Next_Elmt (Iface_DT_Ptr); | |
8177 | pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr))); | |
8178 | ||
54bf19e4 | 8179 | -- Skip tag of predefined primitives no-thunks dispatch table |
1923d2d6 | 8180 | |
ac4d6407 RD |
8181 | Next_Elmt (Iface_DT_Ptr); |
8182 | pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr))); | |
8183 | ||
f4d379b8 | 8184 | Next_Elmt (Iface_DT_Ptr); |
f4d379b8 HK |
8185 | end loop; |
8186 | end Register_Predefined_DT_Entry; | |
8187 | ||
7888a6ae | 8188 | -- Local variables |
3ca505dc | 8189 | |
df3e68b1 | 8190 | Subp : constant Entity_Id := Entity (N); |
3ca505dc | 8191 | |
ac4d6407 RD |
8192 | -- Start of processing for Freeze_Subprogram |
8193 | ||
7888a6ae | 8194 | begin |
d766cee3 RD |
8195 | -- We suppress the initialization of the dispatch table entry when |
8196 | -- VM_Target because the dispatching mechanism is handled internally | |
8197 | -- by the VM. | |
8198 | ||
8199 | if Is_Dispatching_Operation (Subp) | |
8200 | and then not Is_Abstract_Subprogram (Subp) | |
8201 | and then Present (DTC_Entity (Subp)) | |
8202 | and then Present (Scope (DTC_Entity (Subp))) | |
1f110335 | 8203 | and then Tagged_Type_Expansion |
d766cee3 RD |
8204 | and then not Restriction_Active (No_Dispatching_Calls) |
8205 | and then RTE_Available (RE_Tag) | |
8206 | then | |
7888a6ae | 8207 | declare |
d766cee3 | 8208 | Typ : constant Entity_Id := Scope (DTC_Entity (Subp)); |
c8ef728f | 8209 | |
7888a6ae | 8210 | begin |
8fc789c8 | 8211 | -- Handle private overridden primitives |
c8ef728f | 8212 | |
d766cee3 RD |
8213 | if not Is_CPP_Class (Typ) then |
8214 | Check_Overriding_Operation (Subp); | |
7888a6ae | 8215 | end if; |
c8ef728f | 8216 | |
d766cee3 RD |
8217 | -- We assume that imported CPP primitives correspond with objects |
8218 | -- whose constructor is in the CPP side; therefore we don't need | |
8219 | -- to generate code to register them in the dispatch table. | |
c8ef728f | 8220 | |
d766cee3 RD |
8221 | if Is_CPP_Class (Typ) then |
8222 | null; | |
3ca505dc | 8223 | |
d766cee3 RD |
8224 | -- Handle CPP primitives found in derivations of CPP_Class types. |
8225 | -- These primitives must have been inherited from some parent, and | |
8226 | -- there is no need to register them in the dispatch table because | |
5b6f12c7 | 8227 | -- Build_Inherit_Prims takes care of initializing these slots. |
3ca505dc | 8228 | |
d766cee3 | 8229 | elsif Is_Imported (Subp) |
54bf19e4 AC |
8230 | and then (Convention (Subp) = Convention_CPP |
8231 | or else Convention (Subp) = Convention_C) | |
d766cee3 RD |
8232 | then |
8233 | null; | |
8234 | ||
8235 | -- Generate code to register the primitive in non statically | |
8236 | -- allocated dispatch tables | |
8237 | ||
bfae1846 AC |
8238 | elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then |
8239 | ||
d766cee3 RD |
8240 | -- When a primitive is frozen, enter its name in its dispatch |
8241 | -- table slot. | |
f4d379b8 | 8242 | |
d766cee3 | 8243 | if not Is_Interface (Typ) |
ce2b6ba5 | 8244 | or else Present (Interface_Alias (Subp)) |
d766cee3 RD |
8245 | then |
8246 | if Is_Predefined_Dispatching_Operation (Subp) then | |
8247 | Register_Predefined_DT_Entry (Subp); | |
7888a6ae | 8248 | end if; |
d766cee3 | 8249 | |
991395ab AC |
8250 | Insert_Actions_After (N, |
8251 | Register_Primitive (Loc, Prim => Subp)); | |
7888a6ae GD |
8252 | end if; |
8253 | end if; | |
8254 | end; | |
70482933 RK |
8255 | end if; |
8256 | ||
7888a6ae GD |
8257 | -- Mark functions that return by reference. Note that it cannot be part |
8258 | -- of the normal semantic analysis of the spec since the underlying | |
8259 | -- returned type may not be known yet (for private types). | |
70482933 | 8260 | |
d766cee3 RD |
8261 | declare |
8262 | Typ : constant Entity_Id := Etype (Subp); | |
8263 | Utyp : constant Entity_Id := Underlying_Type (Typ); | |
8264 | begin | |
51245e2d | 8265 | if Is_Limited_View (Typ) then |
d766cee3 | 8266 | Set_Returns_By_Ref (Subp); |
048e5cef | 8267 | elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then |
d766cee3 RD |
8268 | Set_Returns_By_Ref (Subp); |
8269 | end if; | |
8270 | end; | |
b546e2a7 AC |
8271 | |
8272 | -- Wnen freezing a null procedure, analyze its delayed aspects now | |
8273 | -- because we may not have reached the end of the declarative list when | |
8274 | -- delayed aspects are normally analyzed. This ensures that dispatching | |
8275 | -- calls are properly rewritten when the generated _Postcondition | |
8276 | -- procedure is analyzed in the null procedure body. | |
8277 | ||
8278 | if Nkind (Parent (Subp)) = N_Procedure_Specification | |
8279 | and then Null_Present (Parent (Subp)) | |
8280 | then | |
5afe5d2d | 8281 | Analyze_Subprogram_Contract (Subp); |
b546e2a7 | 8282 | end if; |
70482933 RK |
8283 | end Freeze_Subprogram; |
8284 | ||
8dbf3473 AC |
8285 | ----------------------- |
8286 | -- Is_Null_Procedure -- | |
8287 | ----------------------- | |
8288 | ||
8289 | function Is_Null_Procedure (Subp : Entity_Id) return Boolean is | |
8290 | Decl : constant Node_Id := Unit_Declaration_Node (Subp); | |
8291 | ||
8292 | begin | |
8293 | if Ekind (Subp) /= E_Procedure then | |
8294 | return False; | |
8295 | ||
8296 | -- Check if this is a declared null procedure | |
8297 | ||
8298 | elsif Nkind (Decl) = N_Subprogram_Declaration then | |
e1f3cb58 AC |
8299 | if not Null_Present (Specification (Decl)) then |
8300 | return False; | |
8dbf3473 AC |
8301 | |
8302 | elsif No (Body_To_Inline (Decl)) then | |
8303 | return False; | |
8304 | ||
8305 | -- Check if the body contains only a null statement, followed by | |
8306 | -- the return statement added during expansion. | |
8307 | ||
8308 | else | |
8309 | declare | |
8310 | Orig_Bod : constant Node_Id := Body_To_Inline (Decl); | |
8311 | ||
8312 | Stat : Node_Id; | |
8313 | Stat2 : Node_Id; | |
8314 | ||
8315 | begin | |
8316 | if Nkind (Orig_Bod) /= N_Subprogram_Body then | |
8317 | return False; | |
8318 | else | |
327503f1 JM |
8319 | -- We must skip SCIL nodes because they are currently |
8320 | -- implemented as special N_Null_Statement nodes. | |
8321 | ||
8dbf3473 | 8322 | Stat := |
327503f1 | 8323 | First_Non_SCIL_Node |
8dbf3473 | 8324 | (Statements (Handled_Statement_Sequence (Orig_Bod))); |
327503f1 | 8325 | Stat2 := Next_Non_SCIL_Node (Stat); |
8dbf3473 AC |
8326 | |
8327 | return | |
e1f3cb58 AC |
8328 | Is_Empty_List (Declarations (Orig_Bod)) |
8329 | and then Nkind (Stat) = N_Null_Statement | |
8330 | and then | |
8dbf3473 AC |
8331 | (No (Stat2) |
8332 | or else | |
8333 | (Nkind (Stat2) = N_Simple_Return_Statement | |
8334 | and then No (Next (Stat2)))); | |
8335 | end if; | |
8336 | end; | |
8337 | end if; | |
8338 | ||
8339 | else | |
8340 | return False; | |
8341 | end if; | |
8342 | end Is_Null_Procedure; | |
8343 | ||
02822a92 RD |
8344 | ------------------------------------------- |
8345 | -- Make_Build_In_Place_Call_In_Allocator -- | |
8346 | ------------------------------------------- | |
8347 | ||
8348 | procedure Make_Build_In_Place_Call_In_Allocator | |
8349 | (Allocator : Node_Id; | |
8350 | Function_Call : Node_Id) | |
8351 | is | |
94bbf008 | 8352 | Acc_Type : constant Entity_Id := Etype (Allocator); |
02822a92 RD |
8353 | Loc : Source_Ptr; |
8354 | Func_Call : Node_Id := Function_Call; | |
1399d355 | 8355 | Ref_Func_Call : Node_Id; |
02822a92 RD |
8356 | Function_Id : Entity_Id; |
8357 | Result_Subt : Entity_Id; | |
02822a92 | 8358 | New_Allocator : Node_Id; |
1399d355 AC |
8359 | Return_Obj_Access : Entity_Id; -- temp for function result |
8360 | Temp_Init : Node_Id; -- initial value of Return_Obj_Access | |
8361 | Alloc_Form : BIP_Allocation_Form; | |
8362 | Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool | |
8363 | Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case | |
8364 | Chain : Entity_Id; -- activation chain, in case of tasks | |
02822a92 RD |
8365 | |
8366 | begin | |
19590d70 GD |
8367 | -- Step past qualification or unchecked conversion (the latter can occur |
8368 | -- in cases of calls to 'Input). | |
8369 | ||
ac4d6407 RD |
8370 | if Nkind_In (Func_Call, |
8371 | N_Qualified_Expression, | |
8372 | N_Unchecked_Type_Conversion) | |
19590d70 | 8373 | then |
02822a92 RD |
8374 | Func_Call := Expression (Func_Call); |
8375 | end if; | |
8376 | ||
fdce4bb7 JM |
8377 | -- If the call has already been processed to add build-in-place actuals |
8378 | -- then return. This should not normally occur in an allocator context, | |
8379 | -- but we add the protection as a defensive measure. | |
8380 | ||
8381 | if Is_Expanded_Build_In_Place_Call (Func_Call) then | |
8382 | return; | |
8383 | end if; | |
8384 | ||
8385 | -- Mark the call as processed as a build-in-place call | |
8386 | ||
8387 | Set_Is_Expanded_Build_In_Place_Call (Func_Call); | |
8388 | ||
02822a92 RD |
8389 | Loc := Sloc (Function_Call); |
8390 | ||
8391 | if Is_Entity_Name (Name (Func_Call)) then | |
8392 | Function_Id := Entity (Name (Func_Call)); | |
8393 | ||
8394 | elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then | |
8395 | Function_Id := Etype (Name (Func_Call)); | |
8396 | ||
8397 | else | |
8398 | raise Program_Error; | |
8399 | end if; | |
8400 | ||
94bbf008 | 8401 | Result_Subt := Available_View (Etype (Function_Id)); |
02822a92 | 8402 | |
1399d355 AC |
8403 | -- Create a temp for the function result. In the caller-allocates case, |
8404 | -- this will be initialized to the result of a new uninitialized | |
8405 | -- allocator. Note: we do not use Allocator as the Related_Node of | |
8406 | -- Return_Obj_Access in call to Make_Temporary below as this would | |
8407 | -- create a sort of infinite "recursion". | |
0d566e01 | 8408 | |
1399d355 AC |
8409 | Return_Obj_Access := Make_Temporary (Loc, 'R'); |
8410 | Set_Etype (Return_Obj_Access, Acc_Type); | |
0d566e01 | 8411 | |
1399d355 | 8412 | -- When the result subtype is constrained, the return object is |
f937473f RD |
8413 | -- allocated on the caller side, and access to it is passed to the |
8414 | -- function. | |
02822a92 | 8415 | |
7888a6ae GD |
8416 | -- Here and in related routines, we must examine the full view of the |
8417 | -- type, because the view at the point of call may differ from that | |
8418 | -- that in the function body, and the expansion mechanism depends on | |
8419 | -- the characteristics of the full view. | |
8420 | ||
8421 | if Is_Constrained (Underlying_Type (Result_Subt)) then | |
02822a92 | 8422 | |
f937473f RD |
8423 | -- Replace the initialized allocator of form "new T'(Func (...))" |
8424 | -- with an uninitialized allocator of form "new T", where T is the | |
8425 | -- result subtype of the called function. The call to the function | |
8426 | -- is handled separately further below. | |
02822a92 | 8427 | |
f937473f | 8428 | New_Allocator := |
fad0600d | 8429 | Make_Allocator (Loc, |
e4494292 | 8430 | Expression => New_Occurrence_Of (Result_Subt, Loc)); |
fad0600d AC |
8431 | Set_No_Initialization (New_Allocator); |
8432 | ||
8433 | -- Copy attributes to new allocator. Note that the new allocator | |
8434 | -- logically comes from source if the original one did, so copy the | |
8435 | -- relevant flag. This ensures proper treatment of the restriction | |
8436 | -- No_Implicit_Heap_Allocations in this case. | |
02822a92 | 8437 | |
fad0600d | 8438 | Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator)); |
f937473f | 8439 | Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator)); |
fad0600d | 8440 | Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator)); |
02822a92 | 8441 | |
f937473f | 8442 | Rewrite (Allocator, New_Allocator); |
02822a92 | 8443 | |
1399d355 AC |
8444 | -- Initial value of the temp is the result of the uninitialized |
8445 | -- allocator | |
02822a92 | 8446 | |
1399d355 | 8447 | Temp_Init := Relocate_Node (Allocator); |
f937473f | 8448 | |
1399d355 AC |
8449 | -- Indicate that caller allocates, and pass in the return object |
8450 | ||
8451 | Alloc_Form := Caller_Allocation; | |
8452 | Pool := Make_Null (No_Location); | |
8453 | Return_Obj_Actual := | |
8454 | Make_Unchecked_Type_Conversion (Loc, | |
8455 | Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc), | |
8456 | Expression => | |
8457 | Make_Explicit_Dereference (Loc, | |
8458 | Prefix => New_Occurrence_Of (Return_Obj_Access, Loc))); | |
f937473f RD |
8459 | |
8460 | -- When the result subtype is unconstrained, the function itself must | |
8461 | -- perform the allocation of the return object, so we pass parameters | |
1399d355 | 8462 | -- indicating that. |
f937473f RD |
8463 | |
8464 | else | |
1399d355 AC |
8465 | Temp_Init := Empty; |
8466 | ||
8417f4b2 AC |
8467 | -- Case of a user-defined storage pool. Pass an allocation parameter |
8468 | -- indicating that the function should allocate its result in the | |
8469 | -- pool, and pass the pool. Use 'Unrestricted_Access because the | |
8470 | -- pool may not be aliased. | |
200b7162 | 8471 | |
8417f4b2 AC |
8472 | if VM_Target = No_VM |
8473 | and then Present (Associated_Storage_Pool (Acc_Type)) | |
8474 | then | |
1399d355 AC |
8475 | Alloc_Form := User_Storage_Pool; |
8476 | Pool := | |
8477 | Make_Attribute_Reference (Loc, | |
8478 | Prefix => | |
8479 | New_Occurrence_Of | |
8480 | (Associated_Storage_Pool (Acc_Type), Loc), | |
8481 | Attribute_Name => Name_Unrestricted_Access); | |
8417f4b2 AC |
8482 | |
8483 | -- No user-defined pool; pass an allocation parameter indicating that | |
8484 | -- the function should allocate its result on the heap. | |
8485 | ||
8486 | else | |
1399d355 AC |
8487 | Alloc_Form := Global_Heap; |
8488 | Pool := Make_Null (No_Location); | |
200b7162 | 8489 | end if; |
f937473f | 8490 | |
7888a6ae GD |
8491 | -- The caller does not provide the return object in this case, so we |
8492 | -- have to pass null for the object access actual. | |
8493 | ||
1399d355 AC |
8494 | Return_Obj_Actual := Empty; |
8495 | end if; | |
8496 | ||
8497 | -- Declare the temp object | |
8498 | ||
8499 | Insert_Action (Allocator, | |
8500 | Make_Object_Declaration (Loc, | |
8501 | Defining_Identifier => Return_Obj_Access, | |
8502 | Object_Definition => New_Occurrence_Of (Acc_Type, Loc), | |
8503 | Expression => Temp_Init)); | |
8504 | ||
8505 | Ref_Func_Call := Make_Reference (Loc, Func_Call); | |
8506 | ||
8507 | -- Ada 2005 (AI-251): If the type of the allocator is an interface | |
8508 | -- then generate an implicit conversion to force displacement of the | |
8509 | -- "this" pointer. | |
8510 | ||
8511 | if Is_Interface (Designated_Type (Acc_Type)) then | |
8512 | Rewrite | |
8513 | (Ref_Func_Call, | |
8514 | OK_Convert_To (Acc_Type, Ref_Func_Call)); | |
f937473f | 8515 | end if; |
02822a92 | 8516 | |
1399d355 AC |
8517 | declare |
8518 | Assign : constant Node_Id := | |
8519 | Make_Assignment_Statement (Loc, | |
8520 | Name => New_Occurrence_Of (Return_Obj_Access, Loc), | |
8521 | Expression => Ref_Func_Call); | |
8522 | -- Assign the result of the function call into the temp. In the | |
8523 | -- caller-allocates case, this is overwriting the temp with its | |
8524 | -- initial value, which has no effect. In the callee-allocates case, | |
8525 | -- this is setting the temp to point to the object allocated by the | |
8526 | -- callee. | |
8527 | ||
8528 | Actions : List_Id; | |
8529 | -- Actions to be inserted. If there are no tasks, this is just the | |
8530 | -- assignment statement. If the allocated object has tasks, we need | |
8531 | -- to wrap the assignment in a block that activates them. The | |
8532 | -- activation chain of that block must be passed to the function, | |
8533 | -- rather than some outer chain. | |
8534 | begin | |
8535 | if Has_Task (Result_Subt) then | |
8536 | Actions := New_List; | |
8537 | Build_Task_Allocate_Block_With_Init_Stmts | |
8538 | (Actions, Allocator, Init_Stmts => New_List (Assign)); | |
8539 | Chain := Activation_Chain_Entity (Last (Actions)); | |
8540 | else | |
8541 | Actions := New_List (Assign); | |
8542 | Chain := Empty; | |
8543 | end if; | |
8544 | ||
8545 | Insert_Actions (Allocator, Actions); | |
8546 | end; | |
8547 | ||
8548 | -- When the function has a controlling result, an allocation-form | |
8549 | -- parameter must be passed indicating that the caller is allocating | |
8550 | -- the result object. This is needed because such a function can be | |
8551 | -- called as a dispatching operation and must be treated similarly | |
8552 | -- to functions with unconstrained result subtypes. | |
8553 | ||
8554 | Add_Unconstrained_Actuals_To_Build_In_Place_Call | |
8555 | (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool); | |
8556 | ||
8557 | Add_Finalization_Master_Actual_To_Build_In_Place_Call | |
8558 | (Func_Call, Function_Id, Acc_Type); | |
8559 | ||
8560 | Add_Task_Actuals_To_Build_In_Place_Call | |
8561 | (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type), | |
8562 | Chain => Chain); | |
8563 | ||
8564 | -- Add an implicit actual to the function call that provides access | |
8565 | -- to the allocated object. An unchecked conversion to the (specific) | |
8566 | -- result subtype of the function is inserted to handle cases where | |
8567 | -- the access type of the allocator has a class-wide designated type. | |
8568 | ||
8569 | Add_Access_Actual_To_Build_In_Place_Call | |
8570 | (Func_Call, Function_Id, Return_Obj_Actual); | |
8571 | ||
b254da66 AC |
8572 | -- If the build-in-place function call returns a controlled object, |
8573 | -- the finalization master will require a reference to routine | |
8574 | -- Finalize_Address of the designated type. Setting this attribute | |
8575 | -- is done in the same manner to expansion of allocators. | |
8576 | ||
8577 | if Needs_Finalization (Result_Subt) then | |
8578 | ||
8579 | -- Controlled types with supressed finalization do not need to | |
8580 | -- associate the address of their Finalize_Address primitives with | |
8581 | -- a master since they do not need a master to begin with. | |
8582 | ||
8583 | if Is_Library_Level_Entity (Acc_Type) | |
8584 | and then Finalize_Storage_Only (Result_Subt) | |
8585 | then | |
8586 | null; | |
8587 | ||
5114f3ff AC |
8588 | -- Do not generate the call to Set_Finalize_Address in CodePeer mode |
8589 | -- because Finalize_Address is never built. | |
b254da66 | 8590 | |
5114f3ff | 8591 | elsif not CodePeer_Mode then |
b254da66 AC |
8592 | Insert_Action (Allocator, |
8593 | Make_Set_Finalize_Address_Call (Loc, | |
8594 | Typ => Etype (Function_Id), | |
8595 | Ptr_Typ => Acc_Type)); | |
8596 | end if; | |
8597 | end if; | |
8598 | ||
1399d355 | 8599 | -- Finally, replace the allocator node with a reference to the temp |
02822a92 | 8600 | |
1399d355 | 8601 | Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc)); |
d2d4b355 | 8602 | |
02822a92 RD |
8603 | Analyze_And_Resolve (Allocator, Acc_Type); |
8604 | end Make_Build_In_Place_Call_In_Allocator; | |
8605 | ||
8606 | --------------------------------------------------- | |
8607 | -- Make_Build_In_Place_Call_In_Anonymous_Context -- | |
8608 | --------------------------------------------------- | |
8609 | ||
8610 | procedure Make_Build_In_Place_Call_In_Anonymous_Context | |
8611 | (Function_Call : Node_Id) | |
8612 | is | |
8613 | Loc : Source_Ptr; | |
8614 | Func_Call : Node_Id := Function_Call; | |
8615 | Function_Id : Entity_Id; | |
8616 | Result_Subt : Entity_Id; | |
8617 | Return_Obj_Id : Entity_Id; | |
8618 | Return_Obj_Decl : Entity_Id; | |
8619 | ||
8620 | begin | |
19590d70 GD |
8621 | -- Step past qualification or unchecked conversion (the latter can occur |
8622 | -- in cases of calls to 'Input). | |
8623 | ||
ac4d6407 RD |
8624 | if Nkind_In (Func_Call, N_Qualified_Expression, |
8625 | N_Unchecked_Type_Conversion) | |
19590d70 | 8626 | then |
02822a92 RD |
8627 | Func_Call := Expression (Func_Call); |
8628 | end if; | |
8629 | ||
fdce4bb7 JM |
8630 | -- If the call has already been processed to add build-in-place actuals |
8631 | -- then return. One place this can occur is for calls to build-in-place | |
8632 | -- functions that occur within a call to a protected operation, where | |
8633 | -- due to rewriting and expansion of the protected call there can be | |
8634 | -- more than one call to Expand_Actuals for the same set of actuals. | |
8635 | ||
8636 | if Is_Expanded_Build_In_Place_Call (Func_Call) then | |
8637 | return; | |
8638 | end if; | |
8639 | ||
8640 | -- Mark the call as processed as a build-in-place call | |
8641 | ||
8642 | Set_Is_Expanded_Build_In_Place_Call (Func_Call); | |
8643 | ||
02822a92 RD |
8644 | Loc := Sloc (Function_Call); |
8645 | ||
8646 | if Is_Entity_Name (Name (Func_Call)) then | |
8647 | Function_Id := Entity (Name (Func_Call)); | |
8648 | ||
8649 | elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then | |
8650 | Function_Id := Etype (Name (Func_Call)); | |
8651 | ||
8652 | else | |
8653 | raise Program_Error; | |
8654 | end if; | |
8655 | ||
8656 | Result_Subt := Etype (Function_Id); | |
8657 | ||
df3e68b1 HK |
8658 | -- If the build-in-place function returns a controlled object, then the |
8659 | -- object needs to be finalized immediately after the context. Since | |
8660 | -- this case produces a transient scope, the servicing finalizer needs | |
8661 | -- to name the returned object. Create a temporary which is initialized | |
8662 | -- with the function call: | |
8663 | -- | |
8664 | -- Temp_Id : Func_Type := BIP_Func_Call; | |
8665 | -- | |
8666 | -- The initialization expression of the temporary will be rewritten by | |
8667 | -- the expander using the appropriate mechanism in Make_Build_In_Place_ | |
8668 | -- Call_In_Object_Declaration. | |
8669 | ||
8670 | if Needs_Finalization (Result_Subt) then | |
8671 | declare | |
8672 | Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R'); | |
8673 | Temp_Decl : Node_Id; | |
8674 | ||
8675 | begin | |
8676 | -- Reset the guard on the function call since the following does | |
8677 | -- not perform actual call expansion. | |
8678 | ||
8679 | Set_Is_Expanded_Build_In_Place_Call (Func_Call, False); | |
8680 | ||
8681 | Temp_Decl := | |
8682 | Make_Object_Declaration (Loc, | |
8683 | Defining_Identifier => Temp_Id, | |
8684 | Object_Definition => | |
e4494292 | 8685 | New_Occurrence_Of (Result_Subt, Loc), |
df3e68b1 HK |
8686 | Expression => |
8687 | New_Copy_Tree (Function_Call)); | |
8688 | ||
8689 | Insert_Action (Function_Call, Temp_Decl); | |
8690 | ||
e4494292 | 8691 | Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc)); |
df3e68b1 HK |
8692 | Analyze (Function_Call); |
8693 | end; | |
8694 | ||
f937473f RD |
8695 | -- When the result subtype is constrained, an object of the subtype is |
8696 | -- declared and an access value designating it is passed as an actual. | |
02822a92 | 8697 | |
df3e68b1 | 8698 | elsif Is_Constrained (Underlying_Type (Result_Subt)) then |
02822a92 | 8699 | |
f937473f RD |
8700 | -- Create a temporary object to hold the function result |
8701 | ||
c12beea0 | 8702 | Return_Obj_Id := Make_Temporary (Loc, 'R'); |
f937473f | 8703 | Set_Etype (Return_Obj_Id, Result_Subt); |
02822a92 | 8704 | |
f937473f RD |
8705 | Return_Obj_Decl := |
8706 | Make_Object_Declaration (Loc, | |
8707 | Defining_Identifier => Return_Obj_Id, | |
8708 | Aliased_Present => True, | |
e4494292 | 8709 | Object_Definition => New_Occurrence_Of (Result_Subt, Loc)); |
02822a92 | 8710 | |
f937473f | 8711 | Set_No_Initialization (Return_Obj_Decl); |
02822a92 | 8712 | |
f937473f | 8713 | Insert_Action (Func_Call, Return_Obj_Decl); |
02822a92 | 8714 | |
7888a6ae GD |
8715 | -- When the function has a controlling result, an allocation-form |
8716 | -- parameter must be passed indicating that the caller is allocating | |
8717 | -- the result object. This is needed because such a function can be | |
8718 | -- called as a dispatching operation and must be treated similarly | |
8719 | -- to functions with unconstrained result subtypes. | |
8720 | ||
200b7162 | 8721 | Add_Unconstrained_Actuals_To_Build_In_Place_Call |
7888a6ae GD |
8722 | (Func_Call, Function_Id, Alloc_Form => Caller_Allocation); |
8723 | ||
d3f70b35 | 8724 | Add_Finalization_Master_Actual_To_Build_In_Place_Call |
df3e68b1 | 8725 | (Func_Call, Function_Id); |
f937473f | 8726 | |
f937473f RD |
8727 | Add_Task_Actuals_To_Build_In_Place_Call |
8728 | (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster)); | |
7888a6ae GD |
8729 | |
8730 | -- Add an implicit actual to the function call that provides access | |
8731 | -- to the caller's return object. | |
8732 | ||
f937473f | 8733 | Add_Access_Actual_To_Build_In_Place_Call |
e4494292 | 8734 | (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc)); |
f937473f RD |
8735 | |
8736 | -- When the result subtype is unconstrained, the function must allocate | |
8737 | -- the return object in the secondary stack, so appropriate implicit | |
8738 | -- parameters are added to the call to indicate that. A transient | |
8739 | -- scope is established to ensure eventual cleanup of the result. | |
8740 | ||
8741 | else | |
8742 | -- Pass an allocation parameter indicating that the function should | |
8743 | -- allocate its result on the secondary stack. | |
8744 | ||
200b7162 | 8745 | Add_Unconstrained_Actuals_To_Build_In_Place_Call |
f937473f RD |
8746 | (Func_Call, Function_Id, Alloc_Form => Secondary_Stack); |
8747 | ||
d3f70b35 | 8748 | Add_Finalization_Master_Actual_To_Build_In_Place_Call |
df3e68b1 | 8749 | (Func_Call, Function_Id); |
f937473f | 8750 | |
f937473f RD |
8751 | Add_Task_Actuals_To_Build_In_Place_Call |
8752 | (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster)); | |
7888a6ae GD |
8753 | |
8754 | -- Pass a null value to the function since no return object is | |
8755 | -- available on the caller side. | |
8756 | ||
f937473f RD |
8757 | Add_Access_Actual_To_Build_In_Place_Call |
8758 | (Func_Call, Function_Id, Empty); | |
f937473f | 8759 | end if; |
02822a92 RD |
8760 | end Make_Build_In_Place_Call_In_Anonymous_Context; |
8761 | ||
ce2798e8 | 8762 | -------------------------------------------- |
02822a92 | 8763 | -- Make_Build_In_Place_Call_In_Assignment -- |
ce2798e8 | 8764 | -------------------------------------------- |
02822a92 RD |
8765 | |
8766 | procedure Make_Build_In_Place_Call_In_Assignment | |
8767 | (Assign : Node_Id; | |
8768 | Function_Call : Node_Id) | |
8769 | is | |
3a69b5ff AC |
8770 | Lhs : constant Node_Id := Name (Assign); |
8771 | Func_Call : Node_Id := Function_Call; | |
8772 | Func_Id : Entity_Id; | |
8773 | Loc : Source_Ptr; | |
8774 | Obj_Decl : Node_Id; | |
8775 | Obj_Id : Entity_Id; | |
8776 | Ptr_Typ : Entity_Id; | |
8777 | Ptr_Typ_Decl : Node_Id; | |
74cab21a | 8778 | New_Expr : Node_Id; |
3a69b5ff AC |
8779 | Result_Subt : Entity_Id; |
8780 | Target : Node_Id; | |
02822a92 RD |
8781 | |
8782 | begin | |
19590d70 GD |
8783 | -- Step past qualification or unchecked conversion (the latter can occur |
8784 | -- in cases of calls to 'Input). | |
8785 | ||
ac4d6407 RD |
8786 | if Nkind_In (Func_Call, N_Qualified_Expression, |
8787 | N_Unchecked_Type_Conversion) | |
19590d70 | 8788 | then |
02822a92 RD |
8789 | Func_Call := Expression (Func_Call); |
8790 | end if; | |
8791 | ||
fdce4bb7 JM |
8792 | -- If the call has already been processed to add build-in-place actuals |
8793 | -- then return. This should not normally occur in an assignment context, | |
8794 | -- but we add the protection as a defensive measure. | |
8795 | ||
8796 | if Is_Expanded_Build_In_Place_Call (Func_Call) then | |
8797 | return; | |
8798 | end if; | |
8799 | ||
8800 | -- Mark the call as processed as a build-in-place call | |
8801 | ||
8802 | Set_Is_Expanded_Build_In_Place_Call (Func_Call); | |
8803 | ||
02822a92 RD |
8804 | Loc := Sloc (Function_Call); |
8805 | ||
8806 | if Is_Entity_Name (Name (Func_Call)) then | |
3a69b5ff | 8807 | Func_Id := Entity (Name (Func_Call)); |
02822a92 RD |
8808 | |
8809 | elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then | |
3a69b5ff | 8810 | Func_Id := Etype (Name (Func_Call)); |
02822a92 RD |
8811 | |
8812 | else | |
8813 | raise Program_Error; | |
8814 | end if; | |
8815 | ||
3a69b5ff | 8816 | Result_Subt := Etype (Func_Id); |
02822a92 | 8817 | |
f937473f RD |
8818 | -- When the result subtype is unconstrained, an additional actual must |
8819 | -- be passed to indicate that the caller is providing the return object. | |
7888a6ae GD |
8820 | -- This parameter must also be passed when the called function has a |
8821 | -- controlling result, because dispatching calls to the function needs | |
8822 | -- to be treated effectively the same as calls to class-wide functions. | |
f937473f | 8823 | |
200b7162 | 8824 | Add_Unconstrained_Actuals_To_Build_In_Place_Call |
3a69b5ff | 8825 | (Func_Call, Func_Id, Alloc_Form => Caller_Allocation); |
f937473f | 8826 | |
d3f70b35 | 8827 | Add_Finalization_Master_Actual_To_Build_In_Place_Call |
df3e68b1 | 8828 | (Func_Call, Func_Id); |
02822a92 | 8829 | |
f937473f | 8830 | Add_Task_Actuals_To_Build_In_Place_Call |
3a69b5ff | 8831 | (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster)); |
7888a6ae GD |
8832 | |
8833 | -- Add an implicit actual to the function call that provides access to | |
8834 | -- the caller's return object. | |
8835 | ||
02822a92 RD |
8836 | Add_Access_Actual_To_Build_In_Place_Call |
8837 | (Func_Call, | |
3a69b5ff | 8838 | Func_Id, |
02822a92 | 8839 | Make_Unchecked_Type_Conversion (Loc, |
e4494292 | 8840 | Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc), |
02822a92 RD |
8841 | Expression => Relocate_Node (Lhs))); |
8842 | ||
8843 | -- Create an access type designating the function's result subtype | |
8844 | ||
c12beea0 | 8845 | Ptr_Typ := Make_Temporary (Loc, 'A'); |
02822a92 RD |
8846 | |
8847 | Ptr_Typ_Decl := | |
8848 | Make_Full_Type_Declaration (Loc, | |
3a69b5ff | 8849 | Defining_Identifier => Ptr_Typ, |
2c1b72d7 | 8850 | Type_Definition => |
02822a92 | 8851 | Make_Access_To_Object_Definition (Loc, |
2c1b72d7 | 8852 | All_Present => True, |
02822a92 | 8853 | Subtype_Indication => |
e4494292 | 8854 | New_Occurrence_Of (Result_Subt, Loc))); |
02822a92 RD |
8855 | Insert_After_And_Analyze (Assign, Ptr_Typ_Decl); |
8856 | ||
8857 | -- Finally, create an access object initialized to a reference to the | |
03e1048e AC |
8858 | -- function call. We know this access value is non-null, so mark the |
8859 | -- entity accordingly to suppress junk access checks. | |
02822a92 | 8860 | |
74cab21a EB |
8861 | New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call)); |
8862 | ||
8863 | Obj_Id := Make_Temporary (Loc, 'R', New_Expr); | |
3a69b5ff | 8864 | Set_Etype (Obj_Id, Ptr_Typ); |
74cab21a | 8865 | Set_Is_Known_Non_Null (Obj_Id); |
02822a92 | 8866 | |
3a69b5ff | 8867 | Obj_Decl := |
02822a92 | 8868 | Make_Object_Declaration (Loc, |
3a69b5ff | 8869 | Defining_Identifier => Obj_Id, |
e4494292 | 8870 | Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc), |
74cab21a | 8871 | Expression => New_Expr); |
3a69b5ff | 8872 | Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl); |
02822a92 RD |
8873 | |
8874 | Rewrite (Assign, Make_Null_Statement (Loc)); | |
3a69b5ff AC |
8875 | |
8876 | -- Retrieve the target of the assignment | |
8877 | ||
8878 | if Nkind (Lhs) = N_Selected_Component then | |
8879 | Target := Selector_Name (Lhs); | |
8880 | elsif Nkind (Lhs) = N_Type_Conversion then | |
8881 | Target := Expression (Lhs); | |
8882 | else | |
8883 | Target := Lhs; | |
8884 | end if; | |
8885 | ||
8886 | -- If we are assigning to a return object or this is an expression of | |
8887 | -- an extension aggregate, the target should either be an identifier | |
8888 | -- or a simple expression. All other cases imply a different scenario. | |
8889 | ||
8890 | if Nkind (Target) in N_Has_Entity then | |
8891 | Target := Entity (Target); | |
8892 | else | |
8893 | return; | |
8894 | end if; | |
02822a92 RD |
8895 | end Make_Build_In_Place_Call_In_Assignment; |
8896 | ||
8897 | ---------------------------------------------------- | |
8898 | -- Make_Build_In_Place_Call_In_Object_Declaration -- | |
8899 | ---------------------------------------------------- | |
8900 | ||
8901 | procedure Make_Build_In_Place_Call_In_Object_Declaration | |
8902 | (Object_Decl : Node_Id; | |
8903 | Function_Call : Node_Id) | |
8904 | is | |
f937473f RD |
8905 | Loc : Source_Ptr; |
8906 | Obj_Def_Id : constant Entity_Id := | |
8907 | Defining_Identifier (Object_Decl); | |
2c17ca0a AC |
8908 | Enclosing_Func : constant Entity_Id := |
8909 | Enclosing_Subprogram (Obj_Def_Id); | |
8417f4b2 AC |
8910 | Call_Deref : Node_Id; |
8911 | Caller_Object : Node_Id; | |
8912 | Def_Id : Entity_Id; | |
2c17ca0a | 8913 | Fmaster_Actual : Node_Id := Empty; |
8417f4b2 AC |
8914 | Func_Call : Node_Id := Function_Call; |
8915 | Function_Id : Entity_Id; | |
8916 | Pool_Actual : Node_Id; | |
f65c67d3 | 8917 | Ptr_Typ : Entity_Id; |
8417f4b2 | 8918 | Ptr_Typ_Decl : Node_Id; |
f937473f | 8919 | Pass_Caller_Acc : Boolean := False; |
8c7ff9a0 | 8920 | Res_Decl : Node_Id; |
8417f4b2 | 8921 | Result_Subt : Entity_Id; |
02822a92 RD |
8922 | |
8923 | begin | |
19590d70 GD |
8924 | -- Step past qualification or unchecked conversion (the latter can occur |
8925 | -- in cases of calls to 'Input). | |
8926 | ||
ac4d6407 RD |
8927 | if Nkind_In (Func_Call, N_Qualified_Expression, |
8928 | N_Unchecked_Type_Conversion) | |
19590d70 | 8929 | then |
02822a92 RD |
8930 | Func_Call := Expression (Func_Call); |
8931 | end if; | |
8932 | ||
fdce4bb7 JM |
8933 | -- If the call has already been processed to add build-in-place actuals |
8934 | -- then return. This should not normally occur in an object declaration, | |
8935 | -- but we add the protection as a defensive measure. | |
8936 | ||
8937 | if Is_Expanded_Build_In_Place_Call (Func_Call) then | |
8938 | return; | |
8939 | end if; | |
8940 | ||
8941 | -- Mark the call as processed as a build-in-place call | |
8942 | ||
8943 | Set_Is_Expanded_Build_In_Place_Call (Func_Call); | |
8944 | ||
02822a92 RD |
8945 | Loc := Sloc (Function_Call); |
8946 | ||
8947 | if Is_Entity_Name (Name (Func_Call)) then | |
8948 | Function_Id := Entity (Name (Func_Call)); | |
8949 | ||
8950 | elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then | |
8951 | Function_Id := Etype (Name (Func_Call)); | |
8952 | ||
8953 | else | |
8954 | raise Program_Error; | |
8955 | end if; | |
8956 | ||
8957 | Result_Subt := Etype (Function_Id); | |
8958 | ||
f65c67d3 TQ |
8959 | -- Create an access type designating the function's result subtype. We |
8960 | -- use the type of the original call because it may be a call to an | |
8961 | -- inherited operation, which the expansion has replaced with the parent | |
8962 | -- operation that yields the parent type. Note that this access type | |
8963 | -- must be declared before we establish a transient scope, so that it | |
8964 | -- receives the proper accessibility level. | |
8965 | ||
8966 | Ptr_Typ := Make_Temporary (Loc, 'A'); | |
8967 | Ptr_Typ_Decl := | |
8968 | Make_Full_Type_Declaration (Loc, | |
8969 | Defining_Identifier => Ptr_Typ, | |
8970 | Type_Definition => | |
8971 | Make_Access_To_Object_Definition (Loc, | |
8972 | All_Present => True, | |
8973 | Subtype_Indication => | |
8974 | New_Occurrence_Of (Etype (Function_Call), Loc))); | |
8975 | ||
8976 | -- The access type and its accompanying object must be inserted after | |
8977 | -- the object declaration in the constrained case, so that the function | |
8978 | -- call can be passed access to the object. In the unconstrained case, | |
8979 | -- or if the object declaration is for a return object, the access type | |
8980 | -- and object must be inserted before the object, since the object | |
8981 | -- declaration is rewritten to be a renaming of a dereference of the | |
8982 | -- access object. Note: we need to freeze Ptr_Typ explicitly, because | |
8983 | -- the result object is in a different (transient) scope, so won't | |
8984 | -- cause freezing. | |
8985 | ||
8986 | if Is_Constrained (Underlying_Type (Result_Subt)) | |
8987 | and then not Is_Return_Object (Defining_Identifier (Object_Decl)) | |
8988 | then | |
8989 | Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl); | |
8990 | else | |
8991 | Insert_Action (Object_Decl, Ptr_Typ_Decl); | |
8992 | end if; | |
8993 | ||
8994 | -- Force immediate freezing of Ptr_Typ because Res_Decl will be | |
8995 | -- elaborated in an inner (transient) scope and thus won't cause | |
8996 | -- freezing by itself. | |
8997 | ||
8998 | declare | |
8999 | Ptr_Typ_Freeze_Ref : constant Node_Id := | |
9000 | New_Occurrence_Of (Ptr_Typ, Loc); | |
9001 | begin | |
9002 | Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl); | |
9003 | Freeze_Expression (Ptr_Typ_Freeze_Ref); | |
9004 | end; | |
9005 | ||
1bb6e262 AC |
9006 | -- If the the object is a return object of an enclosing build-in-place |
9007 | -- function, then the implicit build-in-place parameters of the | |
9008 | -- enclosing function are simply passed along to the called function. | |
9009 | -- (Unfortunately, this won't cover the case of extension aggregates | |
9010 | -- where the ancestor part is a build-in-place unconstrained function | |
9011 | -- call that should be passed along the caller's parameters. Currently | |
9012 | -- those get mishandled by reassigning the result of the call to the | |
9013 | -- aggregate return object, when the call result should really be | |
9014 | -- directly built in place in the aggregate and not in a temporary. ???) | |
9015 | ||
9016 | if Is_Return_Object (Defining_Identifier (Object_Decl)) then | |
f937473f RD |
9017 | Pass_Caller_Acc := True; |
9018 | ||
1bb6e262 AC |
9019 | -- When the enclosing function has a BIP_Alloc_Form formal then we |
9020 | -- pass it along to the callee (such as when the enclosing function | |
9021 | -- has an unconstrained or tagged result type). | |
f937473f | 9022 | |
1bb6e262 | 9023 | if Needs_BIP_Alloc_Form (Enclosing_Func) then |
3e452820 AC |
9024 | if VM_Target = No_VM and then |
9025 | RTE_Available (RE_Root_Storage_Pool_Ptr) | |
9026 | then | |
8417f4b2 | 9027 | Pool_Actual := |
e4494292 | 9028 | New_Occurrence_Of (Build_In_Place_Formal |
8417f4b2 AC |
9029 | (Enclosing_Func, BIP_Storage_Pool), Loc); |
9030 | ||
9031 | -- The build-in-place pool formal is not built on .NET/JVM | |
9032 | ||
9033 | else | |
9034 | Pool_Actual := Empty; | |
9035 | end if; | |
9036 | ||
200b7162 | 9037 | Add_Unconstrained_Actuals_To_Build_In_Place_Call |
f937473f RD |
9038 | (Func_Call, |
9039 | Function_Id, | |
9040 | Alloc_Form_Exp => | |
e4494292 | 9041 | New_Occurrence_Of |
f937473f | 9042 | (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form), |
200b7162 | 9043 | Loc), |
8417f4b2 | 9044 | Pool_Actual => Pool_Actual); |
1bb6e262 AC |
9045 | |
9046 | -- Otherwise, if enclosing function has a constrained result subtype, | |
9047 | -- then caller allocation will be used. | |
9048 | ||
9049 | else | |
200b7162 | 9050 | Add_Unconstrained_Actuals_To_Build_In_Place_Call |
1bb6e262 | 9051 | (Func_Call, Function_Id, Alloc_Form => Caller_Allocation); |
f937473f RD |
9052 | end if; |
9053 | ||
2c17ca0a AC |
9054 | if Needs_BIP_Finalization_Master (Enclosing_Func) then |
9055 | Fmaster_Actual := | |
e4494292 | 9056 | New_Occurrence_Of |
2c17ca0a AC |
9057 | (Build_In_Place_Formal |
9058 | (Enclosing_Func, BIP_Finalization_Master), Loc); | |
9059 | end if; | |
9060 | ||
f937473f RD |
9061 | -- Retrieve the BIPacc formal from the enclosing function and convert |
9062 | -- it to the access type of the callee's BIP_Object_Access formal. | |
9063 | ||
9064 | Caller_Object := | |
9065 | Make_Unchecked_Type_Conversion (Loc, | |
9066 | Subtype_Mark => | |
e4494292 | 9067 | New_Occurrence_Of |
f937473f RD |
9068 | (Etype |
9069 | (Build_In_Place_Formal (Function_Id, BIP_Object_Access)), | |
9070 | Loc), | |
9071 | Expression => | |
e4494292 | 9072 | New_Occurrence_Of |
f937473f RD |
9073 | (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access), |
9074 | Loc)); | |
9075 | ||
1bb6e262 AC |
9076 | -- In the constrained case, add an implicit actual to the function call |
9077 | -- that provides access to the declared object. An unchecked conversion | |
9078 | -- to the (specific) result type of the function is inserted to handle | |
9079 | -- the case where the object is declared with a class-wide type. | |
9080 | ||
9081 | elsif Is_Constrained (Underlying_Type (Result_Subt)) then | |
9082 | Caller_Object := | |
9083 | Make_Unchecked_Type_Conversion (Loc, | |
e4494292 RD |
9084 | Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc), |
9085 | Expression => New_Occurrence_Of (Obj_Def_Id, Loc)); | |
1bb6e262 AC |
9086 | |
9087 | -- When the function has a controlling result, an allocation-form | |
9088 | -- parameter must be passed indicating that the caller is allocating | |
9089 | -- the result object. This is needed because such a function can be | |
9090 | -- called as a dispatching operation and must be treated similarly | |
9091 | -- to functions with unconstrained result subtypes. | |
9092 | ||
200b7162 | 9093 | Add_Unconstrained_Actuals_To_Build_In_Place_Call |
1bb6e262 AC |
9094 | (Func_Call, Function_Id, Alloc_Form => Caller_Allocation); |
9095 | ||
f937473f RD |
9096 | -- In other unconstrained cases, pass an indication to do the allocation |
9097 | -- on the secondary stack and set Caller_Object to Empty so that a null | |
9098 | -- value will be passed for the caller's object address. A transient | |
9099 | -- scope is established to ensure eventual cleanup of the result. | |
9100 | ||
9101 | else | |
200b7162 | 9102 | Add_Unconstrained_Actuals_To_Build_In_Place_Call |
3e7302c3 | 9103 | (Func_Call, Function_Id, Alloc_Form => Secondary_Stack); |
f937473f RD |
9104 | Caller_Object := Empty; |
9105 | ||
9106 | Establish_Transient_Scope (Object_Decl, Sec_Stack => True); | |
9107 | end if; | |
9108 | ||
2c17ca0a AC |
9109 | -- Pass along any finalization master actual, which is needed in the |
9110 | -- case where the called function initializes a return object of an | |
9111 | -- enclosing build-in-place function. | |
9112 | ||
d3f70b35 | 9113 | Add_Finalization_Master_Actual_To_Build_In_Place_Call |
2c17ca0a AC |
9114 | (Func_Call => Func_Call, |
9115 | Func_Id => Function_Id, | |
9116 | Master_Exp => Fmaster_Actual); | |
7888a6ae | 9117 | |
f937473f RD |
9118 | if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement |
9119 | and then Has_Task (Result_Subt) | |
9120 | then | |
7888a6ae GD |
9121 | -- Here we're passing along the master that was passed in to this |
9122 | -- function. | |
9123 | ||
f937473f RD |
9124 | Add_Task_Actuals_To_Build_In_Place_Call |
9125 | (Func_Call, Function_Id, | |
9126 | Master_Actual => | |
e4494292 | 9127 | New_Occurrence_Of (Build_In_Place_Formal |
af89615f | 9128 | (Enclosing_Func, BIP_Task_Master), Loc)); |
7888a6ae | 9129 | |
f937473f RD |
9130 | else |
9131 | Add_Task_Actuals_To_Build_In_Place_Call | |
9132 | (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster)); | |
9133 | end if; | |
7888a6ae | 9134 | |
02822a92 | 9135 | Add_Access_Actual_To_Build_In_Place_Call |
f937473f | 9136 | (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc); |
02822a92 | 9137 | |
02822a92 | 9138 | -- Finally, create an access object initialized to a reference to the |
03e1048e AC |
9139 | -- function call. We know this access value cannot be null, so mark the |
9140 | -- entity accordingly to suppress the access check. | |
02822a92 | 9141 | |
f65c67d3 TQ |
9142 | Def_Id := Make_Temporary (Loc, 'R', Func_Call); |
9143 | Set_Etype (Def_Id, Ptr_Typ); | |
74cab21a | 9144 | Set_Is_Known_Non_Null (Def_Id); |
c12beea0 | 9145 | |
8c7ff9a0 | 9146 | Res_Decl := |
02822a92 RD |
9147 | Make_Object_Declaration (Loc, |
9148 | Defining_Identifier => Def_Id, | |
f65c67d3 TQ |
9149 | Constant_Present => True, |
9150 | Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc), | |
9151 | Expression => | |
9152 | Make_Reference (Loc, Relocate_Node (Func_Call))); | |
9153 | ||
8c7ff9a0 | 9154 | Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl); |
02822a92 | 9155 | |
1bb6e262 AC |
9156 | -- If the result subtype of the called function is constrained and |
9157 | -- is not itself the return expression of an enclosing BIP function, | |
9158 | -- then mark the object as having no initialization. | |
9159 | ||
9160 | if Is_Constrained (Underlying_Type (Result_Subt)) | |
9161 | and then not Is_Return_Object (Defining_Identifier (Object_Decl)) | |
9162 | then | |
8c7ff9a0 AC |
9163 | -- The related object declaration is encased in a transient block |
9164 | -- because the build-in-place function call contains at least one | |
9165 | -- nested function call that produces a controlled transient | |
9166 | -- temporary: | |
9167 | ||
9168 | -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call); | |
9169 | ||
9170 | -- Since the build-in-place expansion decouples the call from the | |
9171 | -- object declaration, the finalization machinery lacks the context | |
9172 | -- which prompted the generation of the transient block. To resolve | |
9173 | -- this scenario, store the build-in-place call. | |
9174 | ||
9175 | if Scope_Is_Transient | |
9176 | and then Node_To_Be_Wrapped = Object_Decl | |
9177 | then | |
9178 | Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl); | |
9179 | end if; | |
9180 | ||
f937473f RD |
9181 | Set_Expression (Object_Decl, Empty); |
9182 | Set_No_Initialization (Object_Decl); | |
9183 | ||
1bb6e262 AC |
9184 | -- In case of an unconstrained result subtype, or if the call is the |
9185 | -- return expression of an enclosing BIP function, rewrite the object | |
f937473f RD |
9186 | -- declaration as an object renaming where the renamed object is a |
9187 | -- dereference of <function_Call>'reference: | |
9188 | -- | |
9189 | -- Obj : Subt renames <function_call>'Ref.all; | |
9190 | ||
9191 | else | |
9192 | Call_Deref := | |
9193 | Make_Explicit_Dereference (Loc, | |
e4494292 | 9194 | Prefix => New_Occurrence_Of (Def_Id, Loc)); |
f937473f | 9195 | |
f00c5f52 | 9196 | Loc := Sloc (Object_Decl); |
f937473f RD |
9197 | Rewrite (Object_Decl, |
9198 | Make_Object_Renaming_Declaration (Loc, | |
c12beea0 | 9199 | Defining_Identifier => Make_Temporary (Loc, 'D'), |
f937473f RD |
9200 | Access_Definition => Empty, |
9201 | Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc), | |
9202 | Name => Call_Deref)); | |
9203 | ||
9204 | Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref); | |
9205 | ||
9206 | Analyze (Object_Decl); | |
9207 | ||
9208 | -- Replace the internal identifier of the renaming declaration's | |
9209 | -- entity with identifier of the original object entity. We also have | |
9210 | -- to exchange the entities containing their defining identifiers to | |
9211 | -- ensure the correct replacement of the object declaration by the | |
9212 | -- object renaming declaration to avoid homograph conflicts (since | |
9213 | -- the object declaration's defining identifier was already entered | |
67ce0d7e RD |
9214 | -- in current scope). The Next_Entity links of the two entities also |
9215 | -- have to be swapped since the entities are part of the return | |
9216 | -- scope's entity list and the list structure would otherwise be | |
7e8ed0a6 | 9217 | -- corrupted. Finally, the homonym chain must be preserved as well. |
67ce0d7e RD |
9218 | |
9219 | declare | |
9220 | Renaming_Def_Id : constant Entity_Id := | |
9221 | Defining_Identifier (Object_Decl); | |
9222 | Next_Entity_Temp : constant Entity_Id := | |
9223 | Next_Entity (Renaming_Def_Id); | |
9224 | begin | |
9225 | Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id)); | |
9226 | ||
9227 | -- Swap next entity links in preparation for exchanging entities | |
f937473f | 9228 | |
67ce0d7e RD |
9229 | Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id)); |
9230 | Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp); | |
7e8ed0a6 | 9231 | Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id)); |
67ce0d7e RD |
9232 | |
9233 | Exchange_Entities (Renaming_Def_Id, Obj_Def_Id); | |
f00c5f52 AC |
9234 | |
9235 | -- Preserve source indication of original declaration, so that | |
9236 | -- xref information is properly generated for the right entity. | |
9237 | ||
9238 | Preserve_Comes_From_Source | |
9239 | (Object_Decl, Original_Node (Object_Decl)); | |
e4982b64 AC |
9240 | |
9241 | Preserve_Comes_From_Source | |
9242 | (Obj_Def_Id, Original_Node (Object_Decl)); | |
9243 | ||
f00c5f52 | 9244 | Set_Comes_From_Source (Renaming_Def_Id, False); |
67ce0d7e | 9245 | end; |
f937473f | 9246 | end if; |
02822a92 RD |
9247 | |
9248 | -- If the object entity has a class-wide Etype, then we need to change | |
9249 | -- it to the result subtype of the function call, because otherwise the | |
53b308f6 AC |
9250 | -- object will be class-wide without an explicit initialization and |
9251 | -- won't be allocated properly by the back end. It seems unclean to make | |
9252 | -- such a revision to the type at this point, and we should try to | |
9253 | -- improve this treatment when build-in-place functions with class-wide | |
9254 | -- results are implemented. ??? | |
02822a92 RD |
9255 | |
9256 | if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then | |
9257 | Set_Etype (Defining_Identifier (Object_Decl), Result_Subt); | |
9258 | end if; | |
9259 | end Make_Build_In_Place_Call_In_Object_Declaration; | |
9260 | ||
3bfb3c03 JM |
9261 | -------------------------------------------- |
9262 | -- Make_CPP_Constructor_Call_In_Allocator -- | |
9263 | -------------------------------------------- | |
9264 | ||
9265 | procedure Make_CPP_Constructor_Call_In_Allocator | |
9266 | (Allocator : Node_Id; | |
9267 | Function_Call : Node_Id) | |
9268 | is | |
9269 | Loc : constant Source_Ptr := Sloc (Function_Call); | |
9270 | Acc_Type : constant Entity_Id := Etype (Allocator); | |
9271 | Function_Id : constant Entity_Id := Entity (Name (Function_Call)); | |
9272 | Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id)); | |
9273 | ||
9274 | New_Allocator : Node_Id; | |
9275 | Return_Obj_Access : Entity_Id; | |
9276 | Tmp_Obj : Node_Id; | |
9277 | ||
9278 | begin | |
9279 | pragma Assert (Nkind (Allocator) = N_Allocator | |
8c7ff9a0 | 9280 | and then Nkind (Function_Call) = N_Function_Call); |
3bfb3c03 | 9281 | pragma Assert (Convention (Function_Id) = Convention_CPP |
8c7ff9a0 | 9282 | and then Is_Constructor (Function_Id)); |
3bfb3c03 JM |
9283 | pragma Assert (Is_Constrained (Underlying_Type (Result_Subt))); |
9284 | ||
9285 | -- Replace the initialized allocator of form "new T'(Func (...))" with | |
9286 | -- an uninitialized allocator of form "new T", where T is the result | |
9287 | -- subtype of the called function. The call to the function is handled | |
9288 | -- separately further below. | |
9289 | ||
9290 | New_Allocator := | |
9291 | Make_Allocator (Loc, | |
e4494292 | 9292 | Expression => New_Occurrence_Of (Result_Subt, Loc)); |
3bfb3c03 JM |
9293 | Set_No_Initialization (New_Allocator); |
9294 | ||
9295 | -- Copy attributes to new allocator. Note that the new allocator | |
9296 | -- logically comes from source if the original one did, so copy the | |
9297 | -- relevant flag. This ensures proper treatment of the restriction | |
9298 | -- No_Implicit_Heap_Allocations in this case. | |
9299 | ||
9300 | Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator)); | |
9301 | Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator)); | |
9302 | Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator)); | |
9303 | ||
9304 | Rewrite (Allocator, New_Allocator); | |
9305 | ||
9306 | -- Create a new access object and initialize it to the result of the | |
9307 | -- new uninitialized allocator. Note: we do not use Allocator as the | |
9308 | -- Related_Node of Return_Obj_Access in call to Make_Temporary below | |
9309 | -- as this would create a sort of infinite "recursion". | |
9310 | ||
9311 | Return_Obj_Access := Make_Temporary (Loc, 'R'); | |
9312 | Set_Etype (Return_Obj_Access, Acc_Type); | |
9313 | ||
9314 | -- Generate: | |
9315 | -- Rnnn : constant ptr_T := new (T); | |
9316 | -- Init (Rnn.all,...); | |
9317 | ||
9318 | Tmp_Obj := | |
9319 | Make_Object_Declaration (Loc, | |
9320 | Defining_Identifier => Return_Obj_Access, | |
9321 | Constant_Present => True, | |
e4494292 | 9322 | Object_Definition => New_Occurrence_Of (Acc_Type, Loc), |
3bfb3c03 JM |
9323 | Expression => Relocate_Node (Allocator)); |
9324 | Insert_Action (Allocator, Tmp_Obj); | |
9325 | ||
9326 | Insert_List_After_And_Analyze (Tmp_Obj, | |
9327 | Build_Initialization_Call (Loc, | |
9328 | Id_Ref => | |
9329 | Make_Explicit_Dereference (Loc, | |
e4494292 | 9330 | Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)), |
3bfb3c03 JM |
9331 | Typ => Etype (Function_Id), |
9332 | Constructor_Ref => Function_Call)); | |
9333 | ||
9334 | -- Finally, replace the allocator node with a reference to the result of | |
9335 | -- the function call itself (which will effectively be an access to the | |
9336 | -- object created by the allocator). | |
9337 | ||
e4494292 | 9338 | Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc)); |
3bfb3c03 JM |
9339 | |
9340 | -- Ada 2005 (AI-251): If the type of the allocator is an interface then | |
9341 | -- generate an implicit conversion to force displacement of the "this" | |
9342 | -- pointer. | |
9343 | ||
9344 | if Is_Interface (Designated_Type (Acc_Type)) then | |
9345 | Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator))); | |
9346 | end if; | |
9347 | ||
9348 | Analyze_And_Resolve (Allocator, Acc_Type); | |
9349 | end Make_CPP_Constructor_Call_In_Allocator; | |
9350 | ||
d3f70b35 AC |
9351 | ----------------------------------- |
9352 | -- Needs_BIP_Finalization_Master -- | |
9353 | ----------------------------------- | |
8fb68c56 | 9354 | |
d3f70b35 AC |
9355 | function Needs_BIP_Finalization_Master |
9356 | (Func_Id : Entity_Id) return Boolean | |
9357 | is | |
df3e68b1 HK |
9358 | pragma Assert (Is_Build_In_Place_Function (Func_Id)); |
9359 | Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id)); | |
048e5cef | 9360 | begin |
df3e68b1 HK |
9361 | return |
9362 | not Restriction_Active (No_Finalization) | |
9363 | and then Needs_Finalization (Func_Typ); | |
d3f70b35 | 9364 | end Needs_BIP_Finalization_Master; |
048e5cef | 9365 | |
1bb6e262 AC |
9366 | -------------------------- |
9367 | -- Needs_BIP_Alloc_Form -- | |
9368 | -------------------------- | |
9369 | ||
9370 | function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is | |
9371 | pragma Assert (Is_Build_In_Place_Function (Func_Id)); | |
9372 | Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id)); | |
1bb6e262 AC |
9373 | begin |
9374 | return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ); | |
9375 | end Needs_BIP_Alloc_Form; | |
9376 | ||
63585f75 SB |
9377 | -------------------------------------- |
9378 | -- Needs_Result_Accessibility_Level -- | |
9379 | -------------------------------------- | |
9380 | ||
9381 | function Needs_Result_Accessibility_Level | |
9382 | (Func_Id : Entity_Id) return Boolean | |
9383 | is | |
9384 | Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id)); | |
9385 | ||
9386 | function Has_Unconstrained_Access_Discriminant_Component | |
ebf494ec RD |
9387 | (Comp_Typ : Entity_Id) return Boolean; |
9388 | -- Returns True if any component of the type has an unconstrained access | |
9389 | -- discriminant. | |
63585f75 SB |
9390 | |
9391 | ----------------------------------------------------- | |
9392 | -- Has_Unconstrained_Access_Discriminant_Component -- | |
9393 | ----------------------------------------------------- | |
9394 | ||
9395 | function Has_Unconstrained_Access_Discriminant_Component | |
9396 | (Comp_Typ : Entity_Id) return Boolean | |
9397 | is | |
9398 | begin | |
9399 | if not Is_Limited_Type (Comp_Typ) then | |
9400 | return False; | |
ebf494ec | 9401 | |
63585f75 SB |
9402 | -- Only limited types can have access discriminants with |
9403 | -- defaults. | |
9404 | ||
9405 | elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then | |
9406 | return True; | |
9407 | ||
9408 | elsif Is_Array_Type (Comp_Typ) then | |
9409 | return Has_Unconstrained_Access_Discriminant_Component | |
9410 | (Underlying_Type (Component_Type (Comp_Typ))); | |
9411 | ||
9412 | elsif Is_Record_Type (Comp_Typ) then | |
9413 | declare | |
ebf494ec RD |
9414 | Comp : Entity_Id; |
9415 | ||
63585f75 | 9416 | begin |
ebf494ec | 9417 | Comp := First_Component (Comp_Typ); |
63585f75 SB |
9418 | while Present (Comp) loop |
9419 | if Has_Unconstrained_Access_Discriminant_Component | |
9420 | (Underlying_Type (Etype (Comp))) | |
9421 | then | |
9422 | return True; | |
9423 | end if; | |
9424 | ||
9425 | Next_Component (Comp); | |
9426 | end loop; | |
9427 | end; | |
9428 | end if; | |
9429 | ||
9430 | return False; | |
9431 | end Has_Unconstrained_Access_Discriminant_Component; | |
9432 | ||
57a3fca9 AC |
9433 | Feature_Disabled : constant Boolean := True; |
9434 | -- Temporary | |
9435 | ||
63585f75 SB |
9436 | -- Start of processing for Needs_Result_Accessibility_Level |
9437 | ||
9438 | begin | |
ebf494ec RD |
9439 | -- False if completion unavailable (how does this happen???) |
9440 | ||
9441 | if not Present (Func_Typ) then | |
9442 | return False; | |
63585f75 | 9443 | |
57a3fca9 AC |
9444 | elsif Feature_Disabled then |
9445 | return False; | |
9446 | ||
ebf494ec | 9447 | -- False if not a function, also handle enum-lit renames case |
63585f75 | 9448 | |
ebf494ec RD |
9449 | elsif Func_Typ = Standard_Void_Type |
9450 | or else Is_Scalar_Type (Func_Typ) | |
63585f75 SB |
9451 | then |
9452 | return False; | |
63585f75 | 9453 | |
ebf494ec | 9454 | -- Handle a corner case, a cross-dialect subp renaming. For example, |
30168043 AC |
9455 | -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when |
9456 | -- an Ada 2005 (or earlier) unit references predefined run-time units. | |
ebf494ec RD |
9457 | |
9458 | elsif Present (Alias (Func_Id)) then | |
9459 | ||
63585f75 SB |
9460 | -- Unimplemented: a cross-dialect subp renaming which does not set |
9461 | -- the Alias attribute (e.g., a rename of a dereference of an access | |
54bf19e4 | 9462 | -- to subprogram value). ??? |
63585f75 SB |
9463 | |
9464 | return Present (Extra_Accessibility_Of_Result (Alias (Func_Id))); | |
63585f75 | 9465 | |
ebf494ec RD |
9466 | -- Remaining cases require Ada 2012 mode |
9467 | ||
9468 | elsif Ada_Version < Ada_2012 then | |
63585f75 | 9469 | return False; |
63585f75 | 9470 | |
ebf494ec | 9471 | elsif Ekind (Func_Typ) = E_Anonymous_Access_Type |
63585f75 SB |
9472 | or else Is_Tagged_Type (Func_Typ) |
9473 | then | |
9474 | -- In the case of, say, a null tagged record result type, the need | |
9475 | -- for this extra parameter might not be obvious. This function | |
9476 | -- returns True for all tagged types for compatibility reasons. | |
9477 | -- A function with, say, a tagged null controlling result type might | |
9478 | -- be overridden by a primitive of an extension having an access | |
9479 | -- discriminant and the overrider and overridden must have compatible | |
9480 | -- calling conventions (including implicitly declared parameters). | |
9481 | -- Similarly, values of one access-to-subprogram type might designate | |
9482 | -- both a primitive subprogram of a given type and a function | |
9483 | -- which is, for example, not a primitive subprogram of any type. | |
9484 | -- Again, this requires calling convention compatibility. | |
9485 | -- It might be possible to solve these issues by introducing | |
9486 | -- wrappers, but that is not the approach that was chosen. | |
9487 | ||
9488 | return True; | |
63585f75 | 9489 | |
ebf494ec | 9490 | elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then |
63585f75 | 9491 | return True; |
63585f75 | 9492 | |
ebf494ec | 9493 | elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then |
63585f75 | 9494 | return True; |
63585f75 | 9495 | |
ebf494ec RD |
9496 | -- False for all other cases |
9497 | ||
9498 | else | |
9499 | return False; | |
9500 | end if; | |
63585f75 SB |
9501 | end Needs_Result_Accessibility_Level; |
9502 | ||
70482933 | 9503 | end Exp_Ch6; |