]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/inline.adb
[Ada] Bindgen: protect reference to System.Parameters with Sec_Stack_Used
[thirdparty/gcc.git] / gcc / ada / inline.adb
CommitLineData
38cbfe40
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- I N L I N E --
6-- --
7-- B o d y --
8-- --
4dfba737 9-- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
38cbfe40
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- --
38cbfe40
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. --
38cbfe40
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. --
38cbfe40
RK
23-- --
24------------------------------------------------------------------------------
25
697b781a 26with Aspects; use Aspects;
38cbfe40 27with Atree; use Atree;
16b10ccc 28with Debug; use Debug;
38cbfe40
RK
29with Einfo; use Einfo;
30with Elists; use Elists;
31with Errout; use Errout;
540d8610
ES
32with Expander; use Expander;
33with Exp_Ch6; use Exp_Ch6;
38cbfe40 34with Exp_Ch7; use Exp_Ch7;
38cbfe40 35with Exp_Tss; use Exp_Tss;
540d8610 36with Exp_Util; use Exp_Util;
38cbfe40
RK
37with Fname; use Fname;
38with Fname.UF; use Fname.UF;
39with Lib; use Lib;
a99ada67 40with Namet; use Namet;
540d8610 41with Nmake; use Nmake;
38cbfe40 42with Nlists; use Nlists;
16b10ccc 43with Output; use Output;
a4100e55 44with Sem_Aux; use Sem_Aux;
38cbfe40
RK
45with Sem_Ch8; use Sem_Ch8;
46with Sem_Ch10; use Sem_Ch10;
47with Sem_Ch12; use Sem_Ch12;
2d180af1 48with Sem_Prag; use Sem_Prag;
38cbfe40
RK
49with Sem_Util; use Sem_Util;
50with Sinfo; use Sinfo;
2d180af1 51with Sinput; use Sinput;
38cbfe40
RK
52with Snames; use Snames;
53with Stand; use Stand;
54with Uname; use Uname;
540d8610 55with Tbuild; use Tbuild;
38cbfe40
RK
56
57package body Inline is
58
16b10ccc
AC
59 Check_Inlining_Restrictions : constant Boolean := True;
60 -- In the following cases the frontend rejects inlining because they
61 -- are not handled well by the backend. This variable facilitates
62 -- disabling these restrictions to evaluate future versions of the
63 -- GCC backend in which some of the restrictions may be supported.
64 --
65 -- - subprograms that have:
66 -- - nested subprograms
67 -- - instantiations
68 -- - package declarations
69 -- - task or protected object declarations
70 -- - some of the following statements:
71 -- - abort
72 -- - asynchronous-select
73 -- - conditional-entry-call
74 -- - delay-relative
75 -- - delay-until
76 -- - selective-accept
77 -- - timed-entry-call
78
79 Inlined_Calls : Elist_Id;
80 -- List of frontend inlined calls
81
82 Backend_Calls : Elist_Id;
83 -- List of inline calls passed to the backend
84
85 Backend_Inlined_Subps : Elist_Id;
86 -- List of subprograms inlined by the backend
87
88 Backend_Not_Inlined_Subps : Elist_Id;
89 -- List of subprograms that cannot be inlined by the backend
90
38cbfe40
RK
91 --------------------
92 -- Inlined Bodies --
93 --------------------
94
95 -- Inlined functions are actually placed in line by the backend if the
96 -- corresponding bodies are available (i.e. compiled). Whenever we find
97 -- a call to an inlined subprogram, we add the name of the enclosing
98 -- compilation unit to a worklist. After all compilation, and after
99 -- expansion of generic bodies, we traverse the list of pending bodies
100 -- and compile them as well.
101
102 package Inlined_Bodies is new Table.Table (
103 Table_Component_Type => Entity_Id,
104 Table_Index_Type => Int,
105 Table_Low_Bound => 0,
106 Table_Initial => Alloc.Inlined_Bodies_Initial,
107 Table_Increment => Alloc.Inlined_Bodies_Increment,
108 Table_Name => "Inlined_Bodies");
109
110 -----------------------
111 -- Inline Processing --
112 -----------------------
113
114 -- For each call to an inlined subprogram, we make entries in a table
8a49a499 115 -- that stores caller and callee, and indicates the call direction from
38cbfe40
RK
116 -- one to the other. We also record the compilation unit that contains
117 -- the callee. After analyzing the bodies of all such compilation units,
8a49a499
AC
118 -- we compute the transitive closure of inlined subprograms called from
119 -- the main compilation unit and make it available to the code generator
120 -- in no particular order, thus allowing cycles in the call graph.
38cbfe40
RK
121
122 Last_Inlined : Entity_Id := Empty;
123
124 -- For each entry in the table we keep a list of successors in topological
125 -- order, i.e. callers of the current subprogram.
126
127 type Subp_Index is new Nat;
128 No_Subp : constant Subp_Index := 0;
129
9de61fcb 130 -- The subprogram entities are hashed into the Inlined table
38cbfe40
RK
131
132 Num_Hash_Headers : constant := 512;
133
134 Hash_Headers : array (Subp_Index range 0 .. Num_Hash_Headers - 1)
135 of Subp_Index;
136
137 type Succ_Index is new Nat;
138 No_Succ : constant Succ_Index := 0;
139
140 type Succ_Info is record
141 Subp : Subp_Index;
142 Next : Succ_Index;
143 end record;
144
3f80a182
AC
145 -- The following table stores list elements for the successor lists. These
146 -- lists cannot be chained directly through entries in the Inlined table,
147 -- because a given subprogram can appear in several such lists.
38cbfe40
RK
148
149 package Successors is new Table.Table (
150 Table_Component_Type => Succ_Info,
151 Table_Index_Type => Succ_Index,
152 Table_Low_Bound => 1,
153 Table_Initial => Alloc.Successors_Initial,
154 Table_Increment => Alloc.Successors_Increment,
155 Table_Name => "Successors");
156
157 type Subp_Info is record
158 Name : Entity_Id := Empty;
8a49a499 159 Next : Subp_Index := No_Subp;
38cbfe40 160 First_Succ : Succ_Index := No_Succ;
38cbfe40 161 Main_Call : Boolean := False;
8a49a499 162 Processed : Boolean := False;
38cbfe40
RK
163 end record;
164
165 package Inlined is new Table.Table (
166 Table_Component_Type => Subp_Info,
167 Table_Index_Type => Subp_Index,
168 Table_Low_Bound => 1,
169 Table_Initial => Alloc.Inlined_Initial,
170 Table_Increment => Alloc.Inlined_Increment,
171 Table_Name => "Inlined");
172
173 -----------------------
174 -- Local Subprograms --
175 -----------------------
176
38cbfe40
RK
177 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty);
178 -- Make two entries in Inlined table, for an inlined subprogram being
179 -- called, and for the inlined subprogram that contains the call. If
180 -- the call is in the main compilation unit, Caller is Empty.
181
4ef36ac7
AC
182 procedure Add_Inlined_Subprogram (E : Entity_Id);
183 -- Add subprogram E to the list of inlined subprogram for the unit
6c26bac2 184
38cbfe40
RK
185 function Add_Subp (E : Entity_Id) return Subp_Index;
186 -- Make entry in Inlined table for subprogram E, or return table index
187 -- that already holds E.
188
6c26bac2
AC
189 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id;
190 pragma Inline (Get_Code_Unit_Entity);
191 -- Return the entity node for the unit containing E. Always return the spec
192 -- for a package.
193
38cbfe40
RK
194 function Has_Initialized_Type (E : Entity_Id) return Boolean;
195 -- If a candidate for inlining contains type declarations for types with
31101470 196 -- nontrivial initialization procedures, they are not worth inlining.
38cbfe40 197
6c26bac2
AC
198 function Has_Single_Return (N : Node_Id) return Boolean;
199 -- In general we cannot inline functions that return unconstrained type.
ea0c8cfb
RD
200 -- However, we can handle such functions if all return statements return a
201 -- local variable that is the only declaration in the body of the function.
202 -- In that case the call can be replaced by that local variable as is done
203 -- for other inlined calls.
6c26bac2
AC
204
205 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean;
206 -- Return True if E is in the main unit or its spec or in a subunit
207
38cbfe40 208 function Is_Nested (E : Entity_Id) return Boolean;
3f80a182
AC
209 -- If the function is nested inside some other function, it will always
210 -- be compiled if that function is, so don't add it to the inline list.
211 -- We cannot compile a nested function outside the scope of the containing
212 -- function anyway. This is also the case if the function is defined in a
213 -- task body or within an entry (for example, an initialization procedure).
38cbfe40 214
697b781a
AC
215 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id);
216 -- Remove all aspects and/or pragmas that have no meaning in inlined body
217 -- Body_Decl. The analysis of these items is performed on the non-inlined
218 -- body. The items currently removed are:
219 -- Contract_Cases
220 -- Global
221 -- Depends
222 -- Postcondition
223 -- Precondition
224 -- Refined_Global
225 -- Refined_Depends
226 -- Refined_Post
227 -- Test_Case
228 -- Unmodified
229 -- Unreferenced
38cbfe40
RK
230
231 ------------------------------
232 -- Deferred Cleanup Actions --
233 ------------------------------
234
235 -- The cleanup actions for scopes that contain instantiations is delayed
3f80a182
AC
236 -- until after expansion of those instantiations, because they may contain
237 -- finalizable objects or tasks that affect the cleanup code. A scope
238 -- that contains instantiations only needs to be finalized once, even
239 -- if it contains more than one instance. We keep a list of scopes
240 -- that must still be finalized, and call cleanup_actions after all
241 -- the instantiations have been completed.
38cbfe40
RK
242
243 To_Clean : Elist_Id;
244
245 procedure Add_Scope_To_Clean (Inst : Entity_Id);
9de61fcb 246 -- Build set of scopes on which cleanup actions must be performed
38cbfe40
RK
247
248 procedure Cleanup_Scopes;
9de61fcb 249 -- Complete cleanup actions on scopes that need it
38cbfe40
RK
250
251 --------------
252 -- Add_Call --
253 --------------
254
255 procedure Add_Call (Called : Entity_Id; Caller : Entity_Id := Empty) is
fbf5a39b 256 P1 : constant Subp_Index := Add_Subp (Called);
38cbfe40
RK
257 P2 : Subp_Index;
258 J : Succ_Index;
259
260 begin
261 if Present (Caller) then
262 P2 := Add_Subp (Caller);
263
8a49a499 264 -- Add P1 to the list of successors of P2, if not already there.
38cbfe40
RK
265 -- Note that P2 may contain more than one call to P1, and only
266 -- one needs to be recorded.
267
8a49a499 268 J := Inlined.Table (P2).First_Succ;
38cbfe40 269 while J /= No_Succ loop
8a49a499 270 if Successors.Table (J).Subp = P1 then
38cbfe40
RK
271 return;
272 end if;
273
274 J := Successors.Table (J).Next;
275 end loop;
276
8a49a499 277 -- On exit, make a successor entry for P1
38cbfe40
RK
278
279 Successors.Increment_Last;
8a49a499 280 Successors.Table (Successors.Last).Subp := P1;
38cbfe40 281 Successors.Table (Successors.Last).Next :=
8a49a499
AC
282 Inlined.Table (P2).First_Succ;
283 Inlined.Table (P2).First_Succ := Successors.Last;
38cbfe40
RK
284 else
285 Inlined.Table (P1).Main_Call := True;
286 end if;
287 end Add_Call;
288
289 ----------------------
290 -- Add_Inlined_Body --
291 ----------------------
292
cf27c5a2 293 procedure Add_Inlined_Body (E : Entity_Id; N : Node_Id) is
38cbfe40 294
4c7be310
AC
295 type Inline_Level_Type is (Dont_Inline, Inline_Call, Inline_Package);
296 -- Level of inlining for the call: Dont_Inline means no inlining,
297 -- Inline_Call means that only the call is considered for inlining,
298 -- Inline_Package means that the call is considered for inlining and
299 -- its package compiled and scanned for more inlining opportunities.
300
c581c520
PMR
301 function Is_Non_Loading_Expression_Function
302 (Id : Entity_Id) return Boolean;
303 -- Determine whether arbitrary entity Id denotes a subprogram which is
304 -- either
305 --
306 -- * An expression function
307 --
308 -- * A function completed by an expression function where both the
309 -- spec and body are in the same context.
310
4c7be310 311 function Must_Inline return Inline_Level_Type;
38cbfe40
RK
312 -- Inlining is only done if the call statement N is in the main unit,
313 -- or within the body of another inlined subprogram.
314
c581c520
PMR
315 ----------------------------------------
316 -- Is_Non_Loading_Expression_Function --
317 ----------------------------------------
318
319 function Is_Non_Loading_Expression_Function
320 (Id : Entity_Id) return Boolean
321 is
322 Body_Decl : Node_Id;
323 Body_Id : Entity_Id;
324 Spec_Decl : Node_Id;
325
326 begin
327 -- A stand-alone expression function is transformed into a spec-body
328 -- pair in-place. Since both the spec and body are in the same list,
329 -- the inlining of such an expression function does not need to load
330 -- anything extra.
331
332 if Is_Expression_Function (Id) then
333 return True;
334
335 -- A function may be completed by an expression function
336
337 elsif Ekind (Id) = E_Function then
338 Spec_Decl := Unit_Declaration_Node (Id);
339
340 if Nkind (Spec_Decl) = N_Subprogram_Declaration then
341 Body_Id := Corresponding_Body (Spec_Decl);
342
343 if Present (Body_Id) then
344 Body_Decl := Unit_Declaration_Node (Body_Id);
345
346 -- The inlining of a completing expression function does
347 -- not need to load anything extra when both the spec and
348 -- body are in the same context.
349
350 return
351 Was_Expression_Function (Body_Decl)
352 and then Parent (Spec_Decl) = Parent (Body_Decl);
353 end if;
354 end if;
355 end if;
356
357 return False;
358 end Is_Non_Loading_Expression_Function;
359
fbf5a39b
AC
360 -----------------
361 -- Must_Inline --
362 -----------------
363
4c7be310 364 function Must_Inline return Inline_Level_Type is
a99ada67 365 Scop : Entity_Id;
38cbfe40
RK
366 Comp : Node_Id;
367
368 begin
fbf5a39b 369 -- Check if call is in main unit
38cbfe40 370
a99ada67
RD
371 Scop := Current_Scope;
372
373 -- Do not try to inline if scope is standard. This could happen, for
374 -- example, for a call to Add_Global_Declaration, and it causes
375 -- trouble to try to inline at this level.
376
377 if Scop = Standard_Standard then
4c7be310 378 return Dont_Inline;
a99ada67
RD
379 end if;
380
381 -- Otherwise lookup scope stack to outer scope
382
38cbfe40
RK
383 while Scope (Scop) /= Standard_Standard
384 and then not Is_Child_Unit (Scop)
385 loop
386 Scop := Scope (Scop);
387 end loop;
388
389 Comp := Parent (Scop);
38cbfe40
RK
390 while Nkind (Comp) /= N_Compilation_Unit loop
391 Comp := Parent (Comp);
392 end loop;
393
4c7be310
AC
394 -- If the call is in the main unit, inline the call and compile the
395 -- package of the subprogram to find more calls to be inlined.
396
fbf5a39b
AC
397 if Comp = Cunit (Main_Unit)
398 or else Comp = Library_Unit (Cunit (Main_Unit))
38cbfe40
RK
399 then
400 Add_Call (E);
4c7be310 401 return Inline_Package;
38cbfe40
RK
402 end if;
403
4ef36ac7
AC
404 -- The call is not in the main unit. See if it is in some subprogram
405 -- that can be inlined outside its unit. If so, inline the call and,
406 -- if the inlining level is set to 1, stop there; otherwise also
407 -- compile the package as above.
38cbfe40
RK
408
409 Scop := Current_Scope;
410 while Scope (Scop) /= Standard_Standard
411 and then not Is_Child_Unit (Scop)
412 loop
4ef36ac7
AC
413 if Is_Overloadable (Scop)
414 and then Is_Inlined (Scop)
415 and then not Is_Nested (Scop)
416 then
38cbfe40 417 Add_Call (E, Scop);
2137e8a6 418
4c7be310
AC
419 if Inline_Level = 1 then
420 return Inline_Call;
421 else
422 return Inline_Package;
423 end if;
38cbfe40
RK
424 end if;
425
426 Scop := Scope (Scop);
427 end loop;
428
4c7be310 429 return Dont_Inline;
38cbfe40
RK
430 end Must_Inline;
431
4c7be310
AC
432 Level : Inline_Level_Type;
433
38cbfe40
RK
434 -- Start of processing for Add_Inlined_Body
435
436 begin
cf27c5a2
EB
437 Append_New_Elmt (N, To => Backend_Calls);
438
4ef36ac7
AC
439 -- Skip subprograms that cannot be inlined outside their unit
440
441 if Is_Abstract_Subprogram (E)
442 or else Convention (E) = Convention_Protected
443 or else Is_Nested (E)
444 then
445 return;
446 end if;
447
2e885a6f
AC
448 -- Find out whether the call must be inlined. Unless the result is
449 -- Dont_Inline, Must_Inline also creates an edge for the call in the
450 -- callgraph; however, it will not be activated until after Is_Called
451 -- is set on the subprogram.
452
453 Level := Must_Inline;
454
455 if Level = Dont_Inline then
456 return;
457 end if;
458
459 -- If the call was generated by the compiler and is to a subprogram in
460 -- a run-time unit, we need to suppress debugging information for it,
461 -- so that the code that is eventually inlined will not affect the
462 -- debugging of the program. We do not do it if the call comes from
463 -- source because, even if the call is inlined, the user may expect it
464 -- to be present in the debugging information.
465
466 if not Comes_From_Source (N)
467 and then In_Extended_Main_Source_Unit (N)
8ab31c0c 468 and then Is_Predefined_Unit (Get_Source_Unit (E))
2e885a6f
AC
469 then
470 Set_Needs_Debug_Info (E, False);
471 end if;
472
c581c520
PMR
473 -- If the subprogram is an expression function, or is completed by one
474 -- where both the spec and body are in the same context, then there is
475 -- no need to load any package body since the body of the function is
476 -- in the spec.
2e885a6f 477
c581c520 478 if Is_Non_Loading_Expression_Function (E) then
2e885a6f
AC
479 Set_Is_Called (E);
480 return;
481 end if;
482
38cbfe40
RK
483 -- Find unit containing E, and add to list of inlined bodies if needed.
484 -- If the body is already present, no need to load any other unit. This
485 -- is the case for an initialization procedure, which appears in the
486 -- package declaration that contains the type. It is also the case if
487 -- the body has already been analyzed. Finally, if the unit enclosing
488 -- E is an instance, the instance body will be analyzed in any case,
489 -- and there is no need to add the enclosing unit (whose body might not
490 -- be available).
491
492 -- Library-level functions must be handled specially, because there is
493 -- no enclosing package to retrieve. In this case, it is the body of
494 -- the function that will have to be loaded.
495
2e885a6f
AC
496 declare
497 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
cf27c5a2 498
2e885a6f
AC
499 begin
500 if Pack = E then
501 Set_Is_Called (E);
502 Inlined_Bodies.Increment_Last;
503 Inlined_Bodies.Table (Inlined_Bodies.Last) := E;
504
505 elsif Ekind (Pack) = E_Package then
506 Set_Is_Called (E);
507
508 if Is_Generic_Instance (Pack) then
509 null;
510
511 -- Do not inline the package if the subprogram is an init proc
512 -- or other internally generated subprogram, because in that
513 -- case the subprogram body appears in the same unit that
514 -- declares the type, and that body is visible to the back end.
515 -- Do not inline it either if it is in the main unit.
516 -- Extend the -gnatn2 processing to -gnatn1 for Inline_Always
517 -- calls if the back-end takes care of inlining the call.
e49de265 518 -- Note that Level in Inline_Package | Inline_Call here.
2e885a6f 519
e49de265
BD
520 elsif ((Level = Inline_Call
521 and then Has_Pragma_Inline_Always (E)
522 and then Back_End_Inlining)
523 or else Level = Inline_Package)
2e885a6f
AC
524 and then not Is_Inlined (Pack)
525 and then not Is_Internal (E)
526 and then not In_Main_Unit_Or_Subunit (Pack)
527 then
528 Set_Is_Inlined (Pack);
38cbfe40 529 Inlined_Bodies.Increment_Last;
2e885a6f 530 Inlined_Bodies.Table (Inlined_Bodies.Last) := Pack;
38cbfe40 531 end if;
2e885a6f 532 end if;
cf27c5a2 533
2e885a6f
AC
534 -- Ensure that Analyze_Inlined_Bodies will be invoked after
535 -- completing the analysis of the current unit.
536
537 Inline_Processing_Required := True;
538 end;
38cbfe40
RK
539 end Add_Inlined_Body;
540
541 ----------------------------
542 -- Add_Inlined_Subprogram --
543 ----------------------------
544
4ef36ac7 545 procedure Add_Inlined_Subprogram (E : Entity_Id) is
d8d7e809 546 Decl : constant Node_Id := Parent (Declaration_Node (E));
feecad68 547 Pack : constant Entity_Id := Get_Code_Unit_Entity (E);
38cbfe40 548
6c26bac2
AC
549 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id);
550 -- Append Subp to the list of subprograms inlined by the backend
551
552 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id);
553 -- Append Subp to the list of subprograms that cannot be inlined by
ea0c8cfb 554 -- the backend.
6c26bac2 555
6c26bac2
AC
556 -----------------------------------------
557 -- Register_Backend_Inlined_Subprogram --
558 -----------------------------------------
559
560 procedure Register_Backend_Inlined_Subprogram (Subp : Entity_Id) is
561 begin
21c51f53 562 Append_New_Elmt (Subp, To => Backend_Inlined_Subps);
6c26bac2
AC
563 end Register_Backend_Inlined_Subprogram;
564
565 ---------------------------------------------
566 -- Register_Backend_Not_Inlined_Subprogram --
567 ---------------------------------------------
568
569 procedure Register_Backend_Not_Inlined_Subprogram (Subp : Entity_Id) is
570 begin
21c51f53 571 Append_New_Elmt (Subp, To => Backend_Not_Inlined_Subps);
6c26bac2
AC
572 end Register_Backend_Not_Inlined_Subprogram;
573
fbf5a39b
AC
574 -- Start of processing for Add_Inlined_Subprogram
575
38cbfe40 576 begin
9466892f
AC
577 -- If the subprogram is to be inlined, and if its unit is known to be
578 -- inlined or is an instance whose body will be analyzed anyway or the
d8d7e809
AC
579 -- subprogram was generated as a body by the compiler (for example an
580 -- initialization procedure) or its declaration was provided along with
581 -- the body (for example an expression function), and if it is declared
9466892f
AC
582 -- at the library level not in the main unit, and if it can be inlined
583 -- by the back-end, then insert it in the list of inlined subprograms.
584
585 if Is_Inlined (E)
586 and then (Is_Inlined (Pack)
3f80a182 587 or else Is_Generic_Instance (Pack)
d8d7e809
AC
588 or else Nkind (Decl) = N_Subprogram_Body
589 or else Present (Corresponding_Body (Decl)))
053cf994 590 and then not In_Main_Unit_Or_Subunit (E)
38cbfe40
RK
591 and then not Is_Nested (E)
592 and then not Has_Initialized_Type (E)
593 then
71ff3d18 594 Register_Backend_Inlined_Subprogram (E);
fbf5a39b 595
71ff3d18
AC
596 if No (Last_Inlined) then
597 Set_First_Inlined_Subprogram (Cunit (Main_Unit), E);
38cbfe40 598 else
71ff3d18 599 Set_Next_Inlined_Subprogram (Last_Inlined, E);
fbf5a39b 600 end if;
71ff3d18
AC
601
602 Last_Inlined := E;
3c756b76 603
6c26bac2
AC
604 else
605 Register_Backend_Not_Inlined_Subprogram (E);
38cbfe40 606 end if;
38cbfe40
RK
607 end Add_Inlined_Subprogram;
608
609 ------------------------
610 -- Add_Scope_To_Clean --
611 ------------------------
612
613 procedure Add_Scope_To_Clean (Inst : Entity_Id) is
fbf5a39b 614 Scop : constant Entity_Id := Enclosing_Dynamic_Scope (Inst);
38cbfe40 615 Elmt : Elmt_Id;
38cbfe40
RK
616
617 begin
618 -- If the instance appears in a library-level package declaration,
619 -- all finalization is global, and nothing needs doing here.
620
621 if Scop = Standard_Standard then
622 return;
623 end if;
624
ddf67a1d
AC
625 -- If the instance is within a generic unit, no finalization code
626 -- can be generated. Note that at this point all bodies have been
627 -- analyzed, and the scope stack itself is not present, and the flag
628 -- Inside_A_Generic is not set.
0fb2ea01
AC
629
630 declare
631 S : Entity_Id;
5132708f 632
0fb2ea01
AC
633 begin
634 S := Scope (Inst);
635 while Present (S) and then S /= Standard_Standard loop
ddf67a1d 636 if Is_Generic_Unit (S) then
0fb2ea01
AC
637 return;
638 end if;
639
640 S := Scope (S);
641 end loop;
642 end;
643
38cbfe40 644 Elmt := First_Elmt (To_Clean);
38cbfe40 645 while Present (Elmt) loop
38cbfe40
RK
646 if Node (Elmt) = Scop then
647 return;
648 end if;
649
650 Elmt := Next_Elmt (Elmt);
651 end loop;
652
653 Append_Elmt (Scop, To_Clean);
654 end Add_Scope_To_Clean;
655
656 --------------
657 -- Add_Subp --
658 --------------
659
660 function Add_Subp (E : Entity_Id) return Subp_Index is
661 Index : Subp_Index := Subp_Index (E) mod Num_Hash_Headers;
662 J : Subp_Index;
663
664 procedure New_Entry;
9de61fcb 665 -- Initialize entry in Inlined table
38cbfe40
RK
666
667 procedure New_Entry is
668 begin
669 Inlined.Increment_Last;
670 Inlined.Table (Inlined.Last).Name := E;
8a49a499 671 Inlined.Table (Inlined.Last).Next := No_Subp;
38cbfe40 672 Inlined.Table (Inlined.Last).First_Succ := No_Succ;
38cbfe40 673 Inlined.Table (Inlined.Last).Main_Call := False;
8a49a499 674 Inlined.Table (Inlined.Last).Processed := False;
38cbfe40
RK
675 end New_Entry;
676
677 -- Start of processing for Add_Subp
678
679 begin
680 if Hash_Headers (Index) = No_Subp then
681 New_Entry;
682 Hash_Headers (Index) := Inlined.Last;
683 return Inlined.Last;
684
685 else
686 J := Hash_Headers (Index);
38cbfe40 687 while J /= No_Subp loop
38cbfe40
RK
688 if Inlined.Table (J).Name = E then
689 return J;
690 else
691 Index := J;
692 J := Inlined.Table (J).Next;
693 end if;
694 end loop;
695
696 -- On exit, subprogram was not found. Enter in table. Index is
697 -- the current last entry on the hash chain.
698
699 New_Entry;
700 Inlined.Table (Index).Next := Inlined.Last;
701 return Inlined.Last;
702 end if;
703 end Add_Subp;
704
705 ----------------------------
706 -- Analyze_Inlined_Bodies --
707 ----------------------------
708
709 procedure Analyze_Inlined_Bodies is
710 Comp_Unit : Node_Id;
711 J : Int;
712 Pack : Entity_Id;
8a49a499 713 Subp : Subp_Index;
38cbfe40
RK
714 S : Succ_Index;
715
8a49a499
AC
716 type Pending_Index is new Nat;
717
718 package Pending_Inlined is new Table.Table (
719 Table_Component_Type => Subp_Index,
720 Table_Index_Type => Pending_Index,
721 Table_Low_Bound => 1,
722 Table_Initial => Alloc.Inlined_Initial,
723 Table_Increment => Alloc.Inlined_Increment,
724 Table_Name => "Pending_Inlined");
725 -- The workpile used to compute the transitive closure
726
84f4072a 727 -- Start of processing for Analyze_Inlined_Bodies
1237d6ef 728
38cbfe40 729 begin
07fc65c4 730 if Serious_Errors_Detected = 0 then
a99ada67 731 Push_Scope (Standard_Standard);
38cbfe40
RK
732
733 J := 0;
734 while J <= Inlined_Bodies.Last
07fc65c4 735 and then Serious_Errors_Detected = 0
38cbfe40
RK
736 loop
737 Pack := Inlined_Bodies.Table (J);
38cbfe40
RK
738 while Present (Pack)
739 and then Scope (Pack) /= Standard_Standard
740 and then not Is_Child_Unit (Pack)
741 loop
742 Pack := Scope (Pack);
743 end loop;
744
745 Comp_Unit := Parent (Pack);
38cbfe40
RK
746 while Present (Comp_Unit)
747 and then Nkind (Comp_Unit) /= N_Compilation_Unit
748 loop
749 Comp_Unit := Parent (Comp_Unit);
750 end loop;
751
b03d3f73
AC
752 -- Load the body if it exists and contains inlineable entities,
753 -- unless it is the main unit, or is an instance whose body has
754 -- already been analyzed.
07fc65c4 755
38cbfe40
RK
756 if Present (Comp_Unit)
757 and then Comp_Unit /= Cunit (Main_Unit)
758 and then Body_Required (Comp_Unit)
2bb988bb
AC
759 and then
760 (Nkind (Unit (Comp_Unit)) /= N_Package_Declaration
761 or else
762 (No (Corresponding_Body (Unit (Comp_Unit)))
763 and then Body_Needed_For_Inlining
764 (Defining_Entity (Unit (Comp_Unit)))))
38cbfe40
RK
765 then
766 declare
767 Bname : constant Unit_Name_Type :=
768 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
769
770 OK : Boolean;
771
772 begin
773 if not Is_Loaded (Bname) then
1237d6ef 774 Style_Check := False;
d3271136 775 Load_Needed_Body (Comp_Unit, OK);
38cbfe40
RK
776
777 if not OK then
46ff89f3
AC
778
779 -- Warn that a body was not available for inlining
780 -- by the back-end.
781
38cbfe40
RK
782 Error_Msg_Unit_1 := Bname;
783 Error_Msg_N
685bc70f 784 ("one or more inlined subprograms accessed in $!??",
38cbfe40 785 Comp_Unit);
a99ada67 786 Error_Msg_File_1 :=
38cbfe40 787 Get_File_Name (Bname, Subunit => False);
685bc70f 788 Error_Msg_N ("\but file{ was not found!??", Comp_Unit);
38cbfe40
RK
789 end if;
790 end if;
791 end;
792 end if;
793
794 J := J + 1;
38cbfe40 795
04e9213d
AC
796 if J > Inlined_Bodies.Last then
797
798 -- The analysis of required bodies may have produced additional
799 -- generic instantiations. To obtain further inlining, we need
800 -- to perform another round of generic body instantiations.
801
802 Instantiate_Bodies;
38cbfe40 803
04e9213d
AC
804 -- Symmetrically, the instantiation of required generic bodies
805 -- may have caused additional bodies to be inlined. To obtain
806 -- further inlining, we keep looping over the inlined bodies.
807 end if;
808 end loop;
38cbfe40 809
1237d6ef
AC
810 -- The list of inlined subprograms is an overestimate, because it
811 -- includes inlined functions called from functions that are compiled
812 -- as part of an inlined package, but are not themselves called. An
813 -- accurate computation of just those subprograms that are needed
814 -- requires that we perform a transitive closure over the call graph,
4ef36ac7 815 -- starting from calls in the main compilation unit.
38cbfe40
RK
816
817 for Index in Inlined.First .. Inlined.Last loop
8a49a499 818 if not Is_Called (Inlined.Table (Index).Name) then
5b5b27ad 819
8a49a499
AC
820 -- This means that Add_Inlined_Body added the subprogram to the
821 -- table but wasn't able to handle its code unit. Do nothing.
822
053cf994 823 Inlined.Table (Index).Processed := True;
5b5b27ad 824
8a49a499
AC
825 elsif Inlined.Table (Index).Main_Call then
826 Pending_Inlined.Increment_Last;
827 Pending_Inlined.Table (Pending_Inlined.Last) := Index;
828 Inlined.Table (Index).Processed := True;
5b5b27ad 829
8a49a499 830 else
38cbfe40 831 Set_Is_Called (Inlined.Table (Index).Name, False);
38cbfe40
RK
832 end if;
833 end loop;
834
8a49a499
AC
835 -- Iterate over the workpile until it is emptied, propagating the
836 -- Is_Called flag to the successors of the processed subprogram.
38cbfe40 837
8a49a499
AC
838 while Pending_Inlined.Last >= Pending_Inlined.First loop
839 Subp := Pending_Inlined.Table (Pending_Inlined.Last);
840 Pending_Inlined.Decrement_Last;
38cbfe40 841
8a49a499
AC
842 S := Inlined.Table (Subp).First_Succ;
843
844 while S /= No_Succ loop
845 Subp := Successors.Table (S).Subp;
8a49a499
AC
846
847 if not Inlined.Table (Subp).Processed then
053cf994 848 Set_Is_Called (Inlined.Table (Subp).Name);
8a49a499
AC
849 Pending_Inlined.Increment_Last;
850 Pending_Inlined.Table (Pending_Inlined.Last) := Subp;
851 Inlined.Table (Subp).Processed := True;
852 end if;
853
854 S := Successors.Table (S).Next;
855 end loop;
38cbfe40
RK
856 end loop;
857
8a49a499
AC
858 -- Finally add the called subprograms to the list of inlined
859 -- subprograms for the unit.
38cbfe40
RK
860
861 for Index in Inlined.First .. Inlined.Last loop
4ef36ac7
AC
862 if Is_Called (Inlined.Table (Index).Name) then
863 Add_Inlined_Subprogram (Inlined.Table (Index).Name);
38cbfe40
RK
864 end if;
865 end loop;
866
867 Pop_Scope;
868 end if;
869 end Analyze_Inlined_Bodies;
870
540d8610
ES
871 --------------------------
872 -- Build_Body_To_Inline --
873 --------------------------
38cbfe40 874
16b10ccc
AC
875 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
876 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
274d2584 877 Analysis_Status : constant Boolean := Full_Analysis;
540d8610
ES
878 Original_Body : Node_Id;
879 Body_To_Analyze : Node_Id;
880 Max_Size : constant := 10;
540d8610
ES
881
882 function Has_Pending_Instantiation return Boolean;
3f80a182
AC
883 -- If some enclosing body contains instantiations that appear before
884 -- the corresponding generic body, the enclosing body has a freeze node
885 -- so that it can be elaborated after the generic itself. This might
540d8610
ES
886 -- conflict with subsequent inlinings, so that it is unsafe to try to
887 -- inline in such a case.
888
7b2888e6
AC
889 function Has_Single_Return_In_GNATprove_Mode return Boolean;
890 -- This function is called only in GNATprove mode, and it returns
16b10ccc 891 -- True if the subprogram has no return statement or a single return
039538bc
AC
892 -- statement as last statement. It returns False for subprogram with
893 -- a single return as last statement inside one or more blocks, as
894 -- inlining would generate gotos in that case as well (although the
895 -- goto is useless in that case).
540d8610
ES
896
897 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
898 -- If the body of the subprogram includes a call that returns an
899 -- unconstrained type, the secondary stack is involved, and it
900 -- is not worth inlining.
901
540d8610
ES
902 -------------------------------
903 -- Has_Pending_Instantiation --
904 -------------------------------
38cbfe40 905
540d8610
ES
906 function Has_Pending_Instantiation return Boolean is
907 S : Entity_Id;
38cbfe40 908
540d8610
ES
909 begin
910 S := Current_Scope;
911 while Present (S) loop
912 if Is_Compilation_Unit (S)
913 or else Is_Child_Unit (S)
914 then
915 return False;
fbf5a39b 916
540d8610
ES
917 elsif Ekind (S) = E_Package
918 and then Has_Forward_Instantiation (S)
919 then
920 return True;
921 end if;
fbf5a39b 922
540d8610
ES
923 S := Scope (S);
924 end loop;
df3e68b1 925
540d8610
ES
926 return False;
927 end Has_Pending_Instantiation;
38cbfe40 928
7b2888e6
AC
929 -----------------------------------------
930 -- Has_Single_Return_In_GNATprove_Mode --
931 -----------------------------------------
932
933 function Has_Single_Return_In_GNATprove_Mode return Boolean is
bfaf8a97 934 Body_To_Inline : constant Node_Id := N;
dafe11cd 935 Last_Statement : Node_Id := Empty;
7b2888e6
AC
936
937 function Check_Return (N : Node_Id) return Traverse_Result;
938 -- Returns OK on node N if this is not a return statement different
939 -- from the last statement in the subprogram.
940
941 ------------------
942 -- Check_Return --
943 ------------------
944
945 function Check_Return (N : Node_Id) return Traverse_Result is
946 begin
bfaf8a97 947 case Nkind (N) is
dafe11cd
HK
948 when N_Extended_Return_Statement
949 | N_Simple_Return_Statement
bfaf8a97
AC
950 =>
951 if N = Last_Statement then
952 return OK;
953 else
954 return Abandon;
955 end if;
7b2888e6 956
bfaf8a97
AC
957 -- Skip locally declared subprogram bodies inside the body to
958 -- inline, as the return statements inside those do not count.
959
960 when N_Subprogram_Body =>
961 if N = Body_To_Inline then
962 return OK;
963 else
964 return Skip;
965 end if;
966
967 when others =>
968 return OK;
969 end case;
7b2888e6
AC
970 end Check_Return;
971
972 function Check_All_Returns is new Traverse_Func (Check_Return);
973
974 -- Start of processing for Has_Single_Return_In_GNATprove_Mode
975
976 begin
039538bc 977 -- Retrieve the last statement
7b2888e6
AC
978
979 Last_Statement := Last (Statements (Handled_Statement_Sequence (N)));
980
7b2888e6
AC
981 -- Check that the last statement is the only possible return
982 -- statement in the subprogram.
983
984 return Check_All_Returns (N) = OK;
985 end Has_Single_Return_In_GNATprove_Mode;
986
540d8610
ES
987 --------------------------
988 -- Uses_Secondary_Stack --
989 --------------------------
990
991 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
992 function Check_Call (N : Node_Id) return Traverse_Result;
993 -- Look for function calls that return an unconstrained type
994
995 ----------------
996 -- Check_Call --
997 ----------------
998
999 function Check_Call (N : Node_Id) return Traverse_Result is
1000 begin
1001 if Nkind (N) = N_Function_Call
1002 and then Is_Entity_Name (Name (N))
1003 and then Is_Composite_Type (Etype (Entity (Name (N))))
1004 and then not Is_Constrained (Etype (Entity (Name (N))))
1005 then
1006 Cannot_Inline
1007 ("cannot inline & (call returns unconstrained type)?",
16b10ccc 1008 N, Spec_Id);
540d8610
ES
1009 return Abandon;
1010 else
1011 return OK;
38cbfe40 1012 end if;
540d8610
ES
1013 end Check_Call;
1014
1015 function Check_Calls is new Traverse_Func (Check_Call);
1016
1017 begin
1018 return Check_Calls (Bod) = Abandon;
1019 end Uses_Secondary_Stack;
1020
1021 -- Start of processing for Build_Body_To_Inline
1022
1023 begin
1024 -- Return immediately if done already
1025
1026 if Nkind (Decl) = N_Subprogram_Declaration
1027 and then Present (Body_To_Inline (Decl))
1028 then
1029 return;
1030
7b2888e6
AC
1031 -- Subprograms that have return statements in the middle of the body are
1032 -- inlined with gotos. GNATprove does not currently support gotos, so
1033 -- we prevent such inlining.
1034
1035 elsif GNATprove_Mode
1036 and then not Has_Single_Return_In_GNATprove_Mode
1037 then
16b10ccc 1038 Cannot_Inline ("cannot inline & (multiple returns)?", N, Spec_Id);
7b2888e6
AC
1039 return;
1040
540d8610
ES
1041 -- Functions that return unconstrained composite types require
1042 -- secondary stack handling, and cannot currently be inlined, unless
1043 -- all return statements return a local variable that is the first
1044 -- local declaration in the body.
1045
16b10ccc
AC
1046 elsif Ekind (Spec_Id) = E_Function
1047 and then not Is_Scalar_Type (Etype (Spec_Id))
1048 and then not Is_Access_Type (Etype (Spec_Id))
1049 and then not Is_Constrained (Etype (Spec_Id))
540d8610 1050 then
6c26bac2 1051 if not Has_Single_Return (N) then
540d8610 1052 Cannot_Inline
16b10ccc 1053 ("cannot inline & (unconstrained return type)?", N, Spec_Id);
540d8610 1054 return;
38cbfe40
RK
1055 end if;
1056
540d8610
ES
1057 -- Ditto for functions that return controlled types, where controlled
1058 -- actions interfere in complex ways with inlining.
38cbfe40 1059
16b10ccc
AC
1060 elsif Ekind (Spec_Id) = E_Function
1061 and then Needs_Finalization (Etype (Spec_Id))
540d8610
ES
1062 then
1063 Cannot_Inline
16b10ccc 1064 ("cannot inline & (controlled return type)?", N, Spec_Id);
540d8610
ES
1065 return;
1066 end if;
1067
1068 if Present (Declarations (N))
16b10ccc 1069 and then Has_Excluded_Declaration (Spec_Id, Declarations (N))
540d8610
ES
1070 then
1071 return;
1072 end if;
1073
1074 if Present (Handled_Statement_Sequence (N)) then
1075 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
1076 Cannot_Inline
1077 ("cannot inline& (exception handler)?",
1078 First (Exception_Handlers (Handled_Statement_Sequence (N))),
16b10ccc 1079 Spec_Id);
540d8610 1080 return;
3f80a182 1081
16b10ccc
AC
1082 elsif Has_Excluded_Statement
1083 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
540d8610
ES
1084 then
1085 return;
1086 end if;
1087 end if;
1088
2d180af1
YM
1089 -- We do not inline a subprogram that is too large, unless it is marked
1090 -- Inline_Always or we are in GNATprove mode. This pragma does not
1091 -- suppress the other checks on inlining (forbidden declarations,
1092 -- handlers, etc).
540d8610 1093
16b10ccc
AC
1094 if not (Has_Pragma_Inline_Always (Spec_Id) or else GNATprove_Mode)
1095 and then List_Length
1096 (Statements (Handled_Statement_Sequence (N))) > Max_Size
540d8610 1097 then
16b10ccc 1098 Cannot_Inline ("cannot inline& (body too large)?", N, Spec_Id);
540d8610
ES
1099 return;
1100 end if;
1101
1102 if Has_Pending_Instantiation then
1103 Cannot_Inline
1104 ("cannot inline& (forward instance within enclosing body)?",
16b10ccc 1105 N, Spec_Id);
540d8610
ES
1106 return;
1107 end if;
1108
1109 -- Within an instance, the body to inline must be treated as a nested
1110 -- generic, so that the proper global references are preserved.
1111
1112 -- Note that we do not do this at the library level, because it is not
66f95f60 1113 -- needed, and furthermore this causes trouble if front-end inlining
540d8610
ES
1114 -- is activated (-gnatN).
1115
1116 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1117 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
5e9cb404 1118 Original_Body := Copy_Generic_Node (N, Empty, Instantiating => True);
540d8610
ES
1119 else
1120 Original_Body := Copy_Separate_Tree (N);
1121 end if;
1122
1123 -- We need to capture references to the formals in order to substitute
1124 -- the actuals at the point of inlining, i.e. instantiation. To treat
3f80a182
AC
1125 -- the formals as globals to the body to inline, we nest it within a
1126 -- dummy parameterless subprogram, declared within the real one. To
1127 -- avoid generating an internal name (which is never public, and which
1128 -- affects serial numbers of other generated names), we use an internal
1129 -- symbol that cannot conflict with user declarations.
38cbfe40 1130
540d8610
ES
1131 Set_Parameter_Specifications (Specification (Original_Body), No_List);
1132 Set_Defining_Unit_Name
1133 (Specification (Original_Body),
697b781a 1134 Make_Defining_Identifier (Sloc (N), Name_uParent));
540d8610
ES
1135 Set_Corresponding_Spec (Original_Body, Empty);
1136
3de3a1be 1137 -- Remove all aspects/pragmas that have no meaning in an inlined body
6d0b56ad 1138
697b781a 1139 Remove_Aspects_And_Pragmas (Original_Body);
6d0b56ad 1140
5e9cb404
AC
1141 Body_To_Analyze :=
1142 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
540d8610
ES
1143
1144 -- Set return type of function, which is also global and does not need
1145 -- to be resolved.
1146
16b10ccc 1147 if Ekind (Spec_Id) = E_Function then
697b781a
AC
1148 Set_Result_Definition
1149 (Specification (Body_To_Analyze),
1150 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
540d8610
ES
1151 end if;
1152
1153 if No (Declarations (N)) then
1154 Set_Declarations (N, New_List (Body_To_Analyze));
1155 else
1156 Append (Body_To_Analyze, Declarations (N));
1157 end if;
1158
812e6118 1159 -- The body to inline is preanalyzed. In GNATprove mode we must disable
697b781a
AC
1160 -- full analysis as well so that light expansion does not take place
1161 -- either, and name resolution is unaffected.
274d2584 1162
540d8610 1163 Expander_Mode_Save_And_Set (False);
274d2584 1164 Full_Analysis := False;
540d8610
ES
1165
1166 Analyze (Body_To_Analyze);
1167 Push_Scope (Defining_Entity (Body_To_Analyze));
1168 Save_Global_References (Original_Body);
1169 End_Scope;
1170 Remove (Body_To_Analyze);
1171
1172 Expander_Mode_Restore;
274d2584 1173 Full_Analysis := Analysis_Status;
540d8610
ES
1174
1175 -- Restore environment if previously saved
1176
1177 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
1178 Restore_Env;
1179 end if;
1180
43478196 1181 -- If secondary stack is used, there is no point in inlining. We have
540d8610
ES
1182 -- already issued the warning in this case, so nothing to do.
1183
1184 if Uses_Secondary_Stack (Body_To_Analyze) then
1185 return;
1186 end if;
1187
1188 Set_Body_To_Inline (Decl, Original_Body);
16b10ccc
AC
1189 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1190 Set_Is_Inlined (Spec_Id);
540d8610
ES
1191 end Build_Body_To_Inline;
1192
3de3a1be
YM
1193 -------------------------------------------
1194 -- Call_Can_Be_Inlined_In_GNATprove_Mode --
1195 -------------------------------------------
1196
1197 function Call_Can_Be_Inlined_In_GNATprove_Mode
1198 (N : Node_Id;
1199 Subp : Entity_Id) return Boolean
1200 is
1201 F : Entity_Id;
1202 A : Node_Id;
1203
1204 begin
1205 F := First_Formal (Subp);
1206 A := First_Actual (N);
1207 while Present (F) loop
1208 if Ekind (F) /= E_Out_Parameter
1209 and then not Same_Type (Etype (F), Etype (A))
1210 and then
1211 (Is_By_Reference_Type (Etype (A))
da9683f4 1212 or else Is_Limited_Type (Etype (A)))
3de3a1be
YM
1213 then
1214 return False;
1215 end if;
1216
1217 Next_Formal (F);
1218 Next_Actual (A);
1219 end loop;
1220
1221 return True;
1222 end Call_Can_Be_Inlined_In_GNATprove_Mode;
1223
2d180af1
YM
1224 --------------------------------------
1225 -- Can_Be_Inlined_In_GNATprove_Mode --
1226 --------------------------------------
1227
1228 function Can_Be_Inlined_In_GNATprove_Mode
1229 (Spec_Id : Entity_Id;
1230 Body_Id : Entity_Id) return Boolean
1231 is
57d08392 1232 function Has_Formal_With_Discriminant_Dependent_Fields
d3ef4bd6 1233 (Id : Entity_Id) return Boolean;
5f6061af 1234 -- Returns true if the subprogram has at least one formal parameter of
57d08392
AC
1235 -- an unconstrained record type with per-object constraints on component
1236 -- types.
d3ef4bd6 1237
2d180af1 1238 function Has_Some_Contract (Id : Entity_Id) return Boolean;
4ac62786
AC
1239 -- Return True if subprogram Id has any contract. The presence of
1240 -- Extensions_Visible or Volatile_Function is also considered as a
1241 -- contract here.
2d180af1 1242
82701811 1243 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean;
4ac62786 1244 -- Return True if subprogram Id defines a compilation unit
2e1295ad 1245 -- Shouldn't this be in Sem_Aux???
82701811 1246
db174c98 1247 function In_Package_Spec (Id : Entity_Id) return Boolean;
4ac62786
AC
1248 -- Return True if subprogram Id is defined in the package specification,
1249 -- either its visible or private part.
2d180af1 1250
57d08392
AC
1251 ---------------------------------------------------
1252 -- Has_Formal_With_Discriminant_Dependent_Fields --
1253 ---------------------------------------------------
d3ef4bd6 1254
57d08392 1255 function Has_Formal_With_Discriminant_Dependent_Fields
4ac62786
AC
1256 (Id : Entity_Id) return Boolean
1257 is
57d08392
AC
1258 function Has_Discriminant_Dependent_Component
1259 (Typ : Entity_Id) return Boolean;
4ac62786
AC
1260 -- Determine whether unconstrained record type Typ has at least one
1261 -- component that depends on a discriminant.
d3ef4bd6 1262
57d08392
AC
1263 ------------------------------------------
1264 -- Has_Discriminant_Dependent_Component --
1265 ------------------------------------------
d3ef4bd6 1266
57d08392
AC
1267 function Has_Discriminant_Dependent_Component
1268 (Typ : Entity_Id) return Boolean
1269 is
1270 Comp : Entity_Id;
d3ef4bd6 1271
57d08392 1272 begin
4ac62786
AC
1273 -- Inspect all components of the record type looking for one that
1274 -- depends on a discriminant.
d3ef4bd6 1275
57d08392
AC
1276 Comp := First_Component (Typ);
1277 while Present (Comp) loop
1278 if Has_Discriminant_Dependent_Constraint (Comp) then
1279 return True;
1280 end if;
d3ef4bd6 1281
57d08392
AC
1282 Next_Component (Comp);
1283 end loop;
1284
1285 return False;
1286 end Has_Discriminant_Dependent_Component;
d3ef4bd6 1287
57d08392 1288 -- Local variables
d3ef4bd6 1289
57d08392
AC
1290 Subp_Id : constant Entity_Id := Ultimate_Alias (Id);
1291 Formal : Entity_Id;
1292 Formal_Typ : Entity_Id;
d3ef4bd6 1293
3de3a1be
YM
1294 -- Start of processing for
1295 -- Has_Formal_With_Discriminant_Dependent_Fields
d3ef4bd6 1296
57d08392
AC
1297 begin
1298 -- Inspect all parameters of the subprogram looking for a formal
1299 -- of an unconstrained record type with at least one discriminant
1300 -- dependent component.
1301
1302 Formal := First_Formal (Subp_Id);
1303 while Present (Formal) loop
1304 Formal_Typ := Etype (Formal);
d3ef4bd6 1305
57d08392
AC
1306 if Is_Record_Type (Formal_Typ)
1307 and then not Is_Constrained (Formal_Typ)
1308 and then Has_Discriminant_Dependent_Component (Formal_Typ)
1309 then
1310 return True;
d3ef4bd6 1311 end if;
57d08392
AC
1312
1313 Next_Formal (Formal);
1314 end loop;
d3ef4bd6
AC
1315
1316 return False;
57d08392 1317 end Has_Formal_With_Discriminant_Dependent_Fields;
d3ef4bd6 1318
2d180af1
YM
1319 -----------------------
1320 -- Has_Some_Contract --
1321 -----------------------
1322
1323 function Has_Some_Contract (Id : Entity_Id) return Boolean is
a98480dd
AC
1324 Items : Node_Id;
1325
2d180af1 1326 begin
a98480dd
AC
1327 -- A call to an expression function may precede the actual body which
1328 -- is inserted at the end of the enclosing declarations. Ensure that
c05ba1f1 1329 -- the related entity is decorated before inspecting the contract.
a98480dd 1330
c05ba1f1 1331 if Is_Subprogram_Or_Generic_Subprogram (Id) then
a98480dd
AC
1332 Items := Contract (Id);
1333
b276ab7a
AC
1334 -- Note that Classifications is not Empty when Extensions_Visible
1335 -- or Volatile_Function is present, which causes such subprograms
1336 -- to be considered to have a contract here. This is fine as we
1337 -- want to avoid inlining these too.
1338
a98480dd
AC
1339 return Present (Items)
1340 and then (Present (Pre_Post_Conditions (Items)) or else
1341 Present (Contract_Test_Cases (Items)) or else
1342 Present (Classifications (Items)));
1343 end if;
1344
1345 return False;
2d180af1
YM
1346 end Has_Some_Contract;
1347
63a5b3dc
AC
1348 ---------------------
1349 -- In_Package_Spec --
1350 ---------------------
2d180af1 1351
db174c98 1352 function In_Package_Spec (Id : Entity_Id) return Boolean is
63a5b3dc
AC
1353 P : constant Node_Id := Parent (Subprogram_Spec (Id));
1354 -- Parent of the subprogram's declaration
fc27e20e 1355
2d180af1 1356 begin
63a5b3dc
AC
1357 return Nkind (Enclosing_Declaration (P)) = N_Package_Declaration;
1358 end In_Package_Spec;
2d180af1 1359
82701811
AC
1360 ------------------------
1361 -- Is_Unit_Subprogram --
1362 ------------------------
1363
1364 function Is_Unit_Subprogram (Id : Entity_Id) return Boolean is
1365 Decl : Node_Id := Parent (Parent (Id));
1366 begin
1367 if Nkind (Parent (Id)) = N_Defining_Program_Unit_Name then
1368 Decl := Parent (Decl);
1369 end if;
1370
1371 return Nkind (Parent (Decl)) = N_Compilation_Unit;
1372 end Is_Unit_Subprogram;
1373
fc27e20e
RD
1374 -- Local declarations
1375
da9683f4
AC
1376 Id : Entity_Id;
1377 -- Procedure or function entity for the subprogram
2d180af1 1378
704228bd 1379 -- Start of processing for Can_Be_Inlined_In_GNATprove_Mode
2d180af1
YM
1380
1381 begin
4bd4bb7f
AC
1382 pragma Assert (Present (Spec_Id) or else Present (Body_Id));
1383
2d180af1
YM
1384 if Present (Spec_Id) then
1385 Id := Spec_Id;
1386 else
1387 Id := Body_Id;
1388 end if;
1389
52c1498c
YM
1390 -- Only local subprograms without contracts are inlined in GNATprove
1391 -- mode, as these are the subprograms which a user is not interested in
1392 -- analyzing in isolation, but rather in the context of their call. This
1393 -- is a convenient convention, that could be changed for an explicit
1394 -- pragma/aspect one day.
1395
1396 -- In a number of special cases, inlining is not desirable or not
1397 -- possible, see below.
1399d355 1398
2d180af1
YM
1399 -- Do not inline unit-level subprograms
1400
82701811 1401 if Is_Unit_Subprogram (Id) then
2d180af1
YM
1402 return False;
1403
63a5b3dc
AC
1404 -- Do not inline subprograms declared in package specs, because they are
1405 -- not local, i.e. can be called either from anywhere (if declared in
1406 -- visible part) or from the child units (if declared in private part).
2d180af1 1407
63a5b3dc 1408 elsif In_Package_Spec (Id) then
2d180af1
YM
1409 return False;
1410
9fb1e654
AC
1411 -- Do not inline subprograms declared in other units. This is important
1412 -- in particular for subprograms defined in the private part of a
1413 -- package spec, when analyzing one of its child packages, as otherwise
1414 -- we issue spurious messages about the impossibility to inline such
1415 -- calls.
1416
1417 elsif not In_Extended_Main_Code_Unit (Id) then
1418 return False;
1419
7188885e
AC
1420 -- Do not inline subprograms marked No_Return, possibly used for
1421 -- signaling errors, which GNATprove handles specially.
1422
1423 elsif No_Return (Id) then
1424 return False;
1425
2d180af1 1426 -- Do not inline subprograms that have a contract on the spec or the
b276ab7a
AC
1427 -- body. Use the contract(s) instead in GNATprove. This also prevents
1428 -- inlining of subprograms with Extensions_Visible or Volatile_Function.
2d180af1
YM
1429
1430 elsif (Present (Spec_Id) and then Has_Some_Contract (Spec_Id))
4bd4bb7f
AC
1431 or else
1432 (Present (Body_Id) and then Has_Some_Contract (Body_Id))
2d180af1
YM
1433 then
1434 return False;
1435
52c1498c
YM
1436 -- Do not inline expression functions, which are directly inlined at the
1437 -- prover level.
2d180af1
YM
1438
1439 elsif (Present (Spec_Id) and then Is_Expression_Function (Spec_Id))
4bd4bb7f
AC
1440 or else
1441 (Present (Body_Id) and then Is_Expression_Function (Body_Id))
2d180af1
YM
1442 then
1443 return False;
1444
52c1498c
YM
1445 -- Do not inline generic subprogram instances. The visibility rules of
1446 -- generic instances plays badly with inlining.
1399d355 1447
ac072cb2
AC
1448 elsif Is_Generic_Instance (Spec_Id) then
1449 return False;
1450
2178830b
AC
1451 -- Only inline subprograms whose spec is marked SPARK_Mode On. For
1452 -- the subprogram body, a similar check is performed after the body
1453 -- is analyzed, as this is where a pragma SPARK_Mode might be inserted.
1454
1455 elsif Present (Spec_Id)
eb1ee757
AC
1456 and then
1457 (No (SPARK_Pragma (Spec_Id))
933aa0ac
AC
1458 or else
1459 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) /= On)
2d180af1
YM
1460 then
1461 return False;
1462
1463 -- Subprograms in generic instances are currently not inlined, to avoid
1464 -- problems with inlining of standard library subprograms.
1465
1466 elsif Instantiation_Location (Sloc (Id)) /= No_Location then
1467 return False;
1468
a9e6f868
YM
1469 -- Do not inline subprograms and entries defined inside protected types,
1470 -- which typically are not helper subprograms, which also avoids getting
1471 -- spurious messages on calls that cannot be inlined.
1472
66f95f60 1473 elsif Within_Protected_Type (Id) then
a9e6f868
YM
1474 return False;
1475
d3ef4bd6 1476 -- Do not inline predicate functions (treated specially by GNATprove)
2178830b
AC
1477
1478 elsif Is_Predicate_Function (Id) then
1479 return False;
1480
d3ef4bd6
AC
1481 -- Do not inline subprograms with a parameter of an unconstrained
1482 -- record type if it has discrimiant dependent fields. Indeed, with
1483 -- such parameters, the frontend cannot always ensure type compliance
1484 -- in record component accesses (in particular with records containing
1485 -- packed arrays).
1486
57d08392 1487 elsif Has_Formal_With_Discriminant_Dependent_Fields (Id) then
d3ef4bd6
AC
1488 return False;
1489
2d180af1
YM
1490 -- Otherwise, this is a subprogram declared inside the private part of a
1491 -- package, or inside a package body, or locally in a subprogram, and it
1492 -- does not have any contract. Inline it.
1493
1494 else
1495 return True;
1496 end if;
1497 end Can_Be_Inlined_In_GNATprove_Mode;
1498
da9683f4
AC
1499 -------------------
1500 -- Cannot_Inline --
1501 -------------------
1502
1503 procedure Cannot_Inline
1504 (Msg : String;
1505 N : Node_Id;
1506 Subp : Entity_Id;
1507 Is_Serious : Boolean := False)
1508 is
1509 begin
1510 -- In GNATprove mode, inlining is the technical means by which the
1511 -- higher-level goal of contextual analysis is reached, so issue
1512 -- messages about failure to apply contextual analysis to a
1513 -- subprogram, rather than failure to inline it.
1514
1515 if GNATprove_Mode
1516 and then Msg (Msg'First .. Msg'First + 12) = "cannot inline"
1517 then
1518 declare
1519 Len1 : constant Positive :=
1520 String (String'("cannot inline"))'Length;
1521 Len2 : constant Positive :=
1522 String (String'("info: no contextual analysis of"))'Length;
1523
1524 New_Msg : String (1 .. Msg'Length + Len2 - Len1);
1525
1526 begin
1527 New_Msg (1 .. Len2) := "info: no contextual analysis of";
1528 New_Msg (Len2 + 1 .. Msg'Length + Len2 - Len1) :=
1529 Msg (Msg'First + Len1 .. Msg'Last);
1530 Cannot_Inline (New_Msg, N, Subp, Is_Serious);
1531 return;
1532 end;
1533 end if;
1534
1535 pragma Assert (Msg (Msg'Last) = '?');
1536
66f95f60 1537 -- Legacy front-end inlining model
da9683f4
AC
1538
1539 if not Back_End_Inlining then
1540
1541 -- Do not emit warning if this is a predefined unit which is not
1542 -- the main unit. With validity checks enabled, some predefined
1543 -- subprograms may contain nested subprograms and become ineligible
1544 -- for inlining.
1545
8ab31c0c 1546 if Is_Predefined_Unit (Get_Source_Unit (Subp))
da9683f4
AC
1547 and then not In_Extended_Main_Source_Unit (Subp)
1548 then
1549 null;
1550
1551 -- In GNATprove mode, issue a warning, and indicate that the
1552 -- subprogram is not always inlined by setting flag Is_Inlined_Always
1553 -- to False.
1554
1555 elsif GNATprove_Mode then
1556 Set_Is_Inlined_Always (Subp, False);
1557 Error_Msg_NE (Msg & "p?", N, Subp);
1558
1559 elsif Has_Pragma_Inline_Always (Subp) then
1560
1561 -- Remove last character (question mark) to make this into an
1562 -- error, because the Inline_Always pragma cannot be obeyed.
1563
1564 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1565
1566 elsif Ineffective_Inline_Warnings then
1567 Error_Msg_NE (Msg & "p?", N, Subp);
1568 end if;
1569
66f95f60 1570 -- New semantics relying on back-end inlining
da9683f4
AC
1571
1572 elsif Is_Serious then
1573
1574 -- Remove last character (question mark) to make this into an error.
1575
1576 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1577
1578 -- In GNATprove mode, issue a warning, and indicate that the subprogram
1579 -- is not always inlined by setting flag Is_Inlined_Always to False.
1580
1581 elsif GNATprove_Mode then
1582 Set_Is_Inlined_Always (Subp, False);
1583 Error_Msg_NE (Msg & "p?", N, Subp);
1584
1585 else
1586
1587 -- Do not emit warning if this is a predefined unit which is not
1588 -- the main unit. This behavior is currently provided for backward
1589 -- compatibility but it will be removed when we enforce the
1590 -- strictness of the new rules.
1591
8ab31c0c 1592 if Is_Predefined_Unit (Get_Source_Unit (Subp))
da9683f4
AC
1593 and then not In_Extended_Main_Source_Unit (Subp)
1594 then
1595 null;
1596
1597 elsif Has_Pragma_Inline_Always (Subp) then
1598
1599 -- Emit a warning if this is a call to a runtime subprogram
1600 -- which is located inside a generic. Previously this call
1601 -- was silently skipped.
1602
1603 if Is_Generic_Instance (Subp) then
1604 declare
1605 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
1606 begin
8ab31c0c 1607 if Is_Predefined_Unit (Get_Source_Unit (Gen_P)) then
da9683f4
AC
1608 Set_Is_Inlined (Subp, False);
1609 Error_Msg_NE (Msg & "p?", N, Subp);
1610 return;
1611 end if;
1612 end;
1613 end if;
1614
1615 -- Remove last character (question mark) to make this into an
1616 -- error, because the Inline_Always pragma cannot be obeyed.
1617
1618 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
1619
1620 else
1621 Set_Is_Inlined (Subp, False);
1622
1623 if Ineffective_Inline_Warnings then
1624 Error_Msg_NE (Msg & "p?", N, Subp);
1625 end if;
1626 end if;
1627 end if;
1628 end Cannot_Inline;
1629
16b10ccc
AC
1630 --------------------------------------------
1631 -- Check_And_Split_Unconstrained_Function --
1632 --------------------------------------------
540d8610 1633
16b10ccc 1634 procedure Check_And_Split_Unconstrained_Function
540d8610
ES
1635 (N : Node_Id;
1636 Spec_Id : Entity_Id;
1637 Body_Id : Entity_Id)
1638 is
1639 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
1640 -- Use generic machinery to build an unexpanded body for the subprogram.
1641 -- This body is subsequently used for inline expansions at call sites.
1642
1643 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
1644 -- Return true if we generate code for the function body N, the function
1645 -- body N has no local declarations and its unique statement is a single
1646 -- extended return statement with a handled statements sequence.
1647
540d8610
ES
1648 procedure Split_Unconstrained_Function
1649 (N : Node_Id;
1650 Spec_Id : Entity_Id);
1651 -- N is an inlined function body that returns an unconstrained type and
1652 -- has a single extended return statement. Split N in two subprograms:
1653 -- a procedure P' and a function F'. The formals of P' duplicate the
7ec25b2b 1654 -- formals of N plus an extra formal which is used to return a value;
540d8610
ES
1655 -- its body is composed by the declarations and list of statements
1656 -- of the extended return statement of N.
1657
1658 --------------------------
1659 -- Build_Body_To_Inline --
1660 --------------------------
1661
1662 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
66f95f60
AC
1663 procedure Generate_Subprogram_Body
1664 (N : Node_Id;
1665 Body_To_Inline : out Node_Id);
1666 -- Generate a parameterless duplicate of subprogram body N. Note that
1667 -- occurrences of pragmas referencing the formals are removed since
1668 -- they have no meaning when the body is inlined and the formals are
1669 -- rewritten (the analysis of the non-inlined body will handle these
1670 -- pragmas). A new internal name is associated with Body_To_Inline.
1671
8016e567
PT
1672 ------------------------------
1673 -- Generate_Subprogram_Body --
1674 ------------------------------
66f95f60
AC
1675
1676 procedure Generate_Subprogram_Body
1677 (N : Node_Id;
1678 Body_To_Inline : out Node_Id)
1679 is
1680 begin
1681 -- Within an instance, the body to inline must be treated as a
1682 -- nested generic so that proper global references are preserved.
1683
1684 -- Note that we do not do this at the library level, because it
1685 -- is not needed, and furthermore this causes trouble if front
1686 -- end inlining is activated (-gnatN).
1687
1688 if In_Instance
1689 and then Scope (Current_Scope) /= Standard_Standard
1690 then
5e9cb404
AC
1691 Body_To_Inline :=
1692 Copy_Generic_Node (N, Empty, Instantiating => True);
66f95f60
AC
1693 else
1694 Body_To_Inline := Copy_Separate_Tree (N);
1695 end if;
1696
1697 -- Remove aspects/pragmas that have no meaning in an inlined body
1698
1699 Remove_Aspects_And_Pragmas (Body_To_Inline);
1700
1701 -- We need to capture references to the formals in order
1702 -- to substitute the actuals at the point of inlining, i.e.
1703 -- instantiation. To treat the formals as globals to the body to
1704 -- inline, we nest it within a dummy parameterless subprogram,
1705 -- declared within the real one.
1706
1707 Set_Parameter_Specifications
1708 (Specification (Body_To_Inline), No_List);
1709
1710 -- A new internal name is associated with Body_To_Inline to avoid
1711 -- conflicts when the non-inlined body N is analyzed.
1712
1713 Set_Defining_Unit_Name (Specification (Body_To_Inline),
1714 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
1715 Set_Corresponding_Spec (Body_To_Inline, Empty);
1716 end Generate_Subprogram_Body;
1717
1718 -- Local variables
1719
540d8610
ES
1720 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
1721 Original_Body : Node_Id;
1722 Body_To_Analyze : Node_Id;
1723
1724 begin
1725 pragma Assert (Current_Scope = Spec_Id);
1726
1727 -- Within an instance, the body to inline must be treated as a nested
1728 -- generic, so that the proper global references are preserved. We
1729 -- do not do this at the library level, because it is not needed, and
66f95f60 1730 -- furthermore this causes trouble if front-end inlining is activated
540d8610
ES
1731 -- (-gnatN).
1732
1733 if In_Instance
1734 and then Scope (Current_Scope) /= Standard_Standard
1735 then
1736 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
1737 end if;
1738
643827e9
SB
1739 -- Capture references to formals in order to substitute the actuals
1740 -- at the point of inlining or instantiation. To treat the formals
1741 -- as globals to the body to inline, nest the body within a dummy
1742 -- parameterless subprogram, declared within the real one.
540d8610 1743
16b10ccc 1744 Generate_Subprogram_Body (N, Original_Body);
5e9cb404
AC
1745 Body_To_Analyze :=
1746 Copy_Generic_Node (Original_Body, Empty, Instantiating => False);
540d8610
ES
1747
1748 -- Set return type of function, which is also global and does not
1749 -- need to be resolved.
1750
1751 if Ekind (Spec_Id) = E_Function then
1752 Set_Result_Definition (Specification (Body_To_Analyze),
1753 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
1754 end if;
1755
1756 if No (Declarations (N)) then
1757 Set_Declarations (N, New_List (Body_To_Analyze));
1758 else
1759 Append_To (Declarations (N), Body_To_Analyze);
1760 end if;
1761
1762 Preanalyze (Body_To_Analyze);
1763
1764 Push_Scope (Defining_Entity (Body_To_Analyze));
1765 Save_Global_References (Original_Body);
1766 End_Scope;
1767 Remove (Body_To_Analyze);
1768
1769 -- Restore environment if previously saved
1770
1771 if In_Instance
1772 and then Scope (Current_Scope) /= Standard_Standard
1773 then
1774 Restore_Env;
1775 end if;
1776
1777 pragma Assert (No (Body_To_Inline (Decl)));
1778 Set_Body_To_Inline (Decl, Original_Body);
1779 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
1780 end Build_Body_To_Inline;
1781
540d8610
ES
1782 --------------------------------------
1783 -- Can_Split_Unconstrained_Function --
1784 --------------------------------------
1785
643827e9 1786 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean is
540d8610
ES
1787 Ret_Node : constant Node_Id :=
1788 First (Statements (Handled_Statement_Sequence (N)));
1789 D : Node_Id;
1790
1791 begin
1792 -- No user defined declarations allowed in the function except inside
1793 -- the unique return statement; implicit labels are the only allowed
1794 -- declarations.
1795
1796 if not Is_Empty_List (Declarations (N)) then
1797 D := First (Declarations (N));
1798 while Present (D) loop
1799 if Nkind (D) /= N_Implicit_Label_Declaration then
1800 return False;
1801 end if;
1802
1803 Next (D);
1804 end loop;
1805 end if;
1806
1807 -- We only split the inlined function when we are generating the code
1808 -- of its body; otherwise we leave duplicated split subprograms in
1809 -- the tree which (if referenced) generate wrong references at link
1810 -- time.
1811
1812 return In_Extended_Main_Code_Unit (N)
1813 and then Present (Ret_Node)
1814 and then Nkind (Ret_Node) = N_Extended_Return_Statement
1815 and then No (Next (Ret_Node))
1816 and then Present (Handled_Statement_Sequence (Ret_Node));
1817 end Can_Split_Unconstrained_Function;
1818
540d8610
ES
1819 ----------------------------------
1820 -- Split_Unconstrained_Function --
1821 ----------------------------------
1822
1823 procedure Split_Unconstrained_Function
1824 (N : Node_Id;
1825 Spec_Id : Entity_Id)
1826 is
1827 Loc : constant Source_Ptr := Sloc (N);
1828 Ret_Node : constant Node_Id :=
1829 First (Statements (Handled_Statement_Sequence (N)));
1830 Ret_Obj : constant Node_Id :=
1831 First (Return_Object_Declarations (Ret_Node));
1832
1833 procedure Build_Procedure
1834 (Proc_Id : out Entity_Id;
1835 Decl_List : out List_Id);
1836 -- Build a procedure containing the statements found in the extended
1837 -- return statement of the unconstrained function body N.
1838
3f80a182
AC
1839 ---------------------
1840 -- Build_Procedure --
1841 ---------------------
1842
540d8610
ES
1843 procedure Build_Procedure
1844 (Proc_Id : out Entity_Id;
1845 Decl_List : out List_Id)
1846 is
3f80a182
AC
1847 Formal : Entity_Id;
1848 Formal_List : constant List_Id := New_List;
1849 Proc_Spec : Node_Id;
1850 Proc_Body : Node_Id;
1851 Subp_Name : constant Name_Id := New_Internal_Name ('F');
540d8610 1852 Body_Decl_List : List_Id := No_List;
3f80a182 1853 Param_Type : Node_Id;
540d8610
ES
1854
1855 begin
1856 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
3f80a182
AC
1857 Param_Type :=
1858 New_Copy (Object_Definition (Ret_Obj));
540d8610
ES
1859 else
1860 Param_Type :=
1861 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
1862 end if;
1863
1864 Append_To (Formal_List,
1865 Make_Parameter_Specification (Loc,
3f80a182 1866 Defining_Identifier =>
540d8610
ES
1867 Make_Defining_Identifier (Loc,
1868 Chars => Chars (Defining_Identifier (Ret_Obj))),
3f80a182
AC
1869 In_Present => False,
1870 Out_Present => True,
540d8610 1871 Null_Exclusion_Present => False,
3f80a182 1872 Parameter_Type => Param_Type));
540d8610
ES
1873
1874 Formal := First_Formal (Spec_Id);
596f7139
AC
1875
1876 -- Note that we copy the parameter type rather than creating
1877 -- a reference to it, because it may be a class-wide entity
1878 -- that will not be retrieved by name.
1879
540d8610
ES
1880 while Present (Formal) loop
1881 Append_To (Formal_List,
1882 Make_Parameter_Specification (Loc,
3f80a182 1883 Defining_Identifier =>
540d8610
ES
1884 Make_Defining_Identifier (Sloc (Formal),
1885 Chars => Chars (Formal)),
3f80a182
AC
1886 In_Present => In_Present (Parent (Formal)),
1887 Out_Present => Out_Present (Parent (Formal)),
540d8610
ES
1888 Null_Exclusion_Present =>
1889 Null_Exclusion_Present (Parent (Formal)),
3f80a182 1890 Parameter_Type =>
596f7139 1891 New_Copy_Tree (Parameter_Type (Parent (Formal))),
3f80a182 1892 Expression =>
540d8610
ES
1893 Copy_Separate_Tree (Expression (Parent (Formal)))));
1894
1895 Next_Formal (Formal);
1896 end loop;
1897
3f80a182 1898 Proc_Id := Make_Defining_Identifier (Loc, Chars => Subp_Name);
540d8610
ES
1899
1900 Proc_Spec :=
1901 Make_Procedure_Specification (Loc,
3f80a182 1902 Defining_Unit_Name => Proc_Id,
540d8610
ES
1903 Parameter_Specifications => Formal_List);
1904
1905 Decl_List := New_List;
1906
1907 Append_To (Decl_List,
1908 Make_Subprogram_Declaration (Loc, Proc_Spec));
1909
1910 -- Can_Convert_Unconstrained_Function checked that the function
1911 -- has no local declarations except implicit label declarations.
1912 -- Copy these declarations to the built procedure.
1913
1914 if Present (Declarations (N)) then
1915 Body_Decl_List := New_List;
1916
1917 declare
1918 D : Node_Id;
1919 New_D : Node_Id;
1920
1921 begin
1922 D := First (Declarations (N));
1923 while Present (D) loop
1924 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
1925
1926 New_D :=
1927 Make_Implicit_Label_Declaration (Loc,
1928 Make_Defining_Identifier (Loc,
1929 Chars => Chars (Defining_Identifier (D))),
1930 Label_Construct => Empty);
1931 Append_To (Body_Decl_List, New_D);
1932
1933 Next (D);
1934 end loop;
1935 end;
1936 end if;
1937
1938 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
1939
1940 Proc_Body :=
1941 Make_Subprogram_Body (Loc,
1942 Specification => Copy_Separate_Tree (Proc_Spec),
1943 Declarations => Body_Decl_List,
1944 Handled_Statement_Sequence =>
1945 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
1946
1947 Set_Defining_Unit_Name (Specification (Proc_Body),
1948 Make_Defining_Identifier (Loc, Subp_Name));
1949
1950 Append_To (Decl_List, Proc_Body);
1951 end Build_Procedure;
1952
1953 -- Local variables
1954
1955 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
1956 Blk_Stmt : Node_Id;
1957 Proc_Id : Entity_Id;
1958 Proc_Call : Node_Id;
1959
1960 -- Start of processing for Split_Unconstrained_Function
1961
1962 begin
1963 -- Build the associated procedure, analyze it and insert it before
3f80a182 1964 -- the function body N.
540d8610
ES
1965
1966 declare
1967 Scope : constant Entity_Id := Current_Scope;
1968 Decl_List : List_Id;
1969 begin
1970 Pop_Scope;
1971 Build_Procedure (Proc_Id, Decl_List);
1972 Insert_Actions (N, Decl_List);
7ec25b2b 1973 Set_Is_Inlined (Proc_Id);
540d8610
ES
1974 Push_Scope (Scope);
1975 end;
1976
1977 -- Build the call to the generated procedure
1978
1979 declare
1980 Actual_List : constant List_Id := New_List;
1981 Formal : Entity_Id;
1982
1983 begin
1984 Append_To (Actual_List,
1985 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
1986
1987 Formal := First_Formal (Spec_Id);
1988 while Present (Formal) loop
1989 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
1990
1991 -- Avoid spurious warning on unreferenced formals
1992
1993 Set_Referenced (Formal);
1994 Next_Formal (Formal);
1995 end loop;
1996
1997 Proc_Call :=
1998 Make_Procedure_Call_Statement (Loc,
3f80a182 1999 Name => New_Occurrence_Of (Proc_Id, Loc),
540d8610
ES
2000 Parameter_Associations => Actual_List);
2001 end;
2002
66f95f60 2003 -- Generate:
540d8610
ES
2004
2005 -- declare
2006 -- New_Obj : ...
2007 -- begin
66f95f60
AC
2008 -- Proc (New_Obj, ...);
2009 -- return New_Obj;
2010 -- end;
540d8610
ES
2011
2012 Blk_Stmt :=
2013 Make_Block_Statement (Loc,
3f80a182 2014 Declarations => New_List (New_Obj),
540d8610
ES
2015 Handled_Statement_Sequence =>
2016 Make_Handled_Sequence_Of_Statements (Loc,
2017 Statements => New_List (
2018
2019 Proc_Call,
2020
2021 Make_Simple_Return_Statement (Loc,
2022 Expression =>
2023 New_Occurrence_Of
2024 (Defining_Identifier (New_Obj), Loc)))));
2025
2026 Rewrite (Ret_Node, Blk_Stmt);
2027 end Split_Unconstrained_Function;
2028
16b10ccc
AC
2029 -- Local variables
2030
2031 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
2032
2033 -- Start of processing for Check_And_Split_Unconstrained_Function
540d8610
ES
2034
2035 begin
16b10ccc
AC
2036 pragma Assert (Back_End_Inlining
2037 and then Ekind (Spec_Id) = E_Function
2038 and then Returns_Unconstrained_Type (Spec_Id)
2039 and then Comes_From_Source (Body_Id)
2040 and then (Has_Pragma_Inline_Always (Spec_Id)
2041 or else Optimization_Level > 0));
2042
2043 -- This routine must not be used in GNATprove mode since GNATprove
2044 -- relies on frontend inlining
2045
2046 pragma Assert (not GNATprove_Mode);
2047
2048 -- No need to split the function if we cannot generate the code
2049
2050 if Serious_Errors_Detected /= 0 then
2051 return;
2052 end if;
2053
16b10ccc
AC
2054 -- No action needed in stubs since the attribute Body_To_Inline
2055 -- is not available
4bd4bb7f 2056
16b10ccc
AC
2057 if Nkind (Decl) = N_Subprogram_Body_Stub then
2058 return;
2059
2060 -- Cannot build the body to inline if the attribute is already set.
2061 -- This attribute may have been set if this is a subprogram renaming
2062 -- declarations (see Freeze.Build_Renamed_Body).
2063
2064 elsif Present (Body_To_Inline (Decl)) then
2065 return;
2066
2067 -- Check excluded declarations
2068
2069 elsif Present (Declarations (N))
2070 and then Has_Excluded_Declaration (Spec_Id, Declarations (N))
2071 then
2072 return;
2073
2074 -- Check excluded statements. There is no need to protect us against
2075 -- exception handlers since they are supported by the GCC backend.
2076
2077 elsif Present (Handled_Statement_Sequence (N))
2078 and then Has_Excluded_Statement
2079 (Spec_Id, Statements (Handled_Statement_Sequence (N)))
2080 then
2081 return;
540d8610
ES
2082 end if;
2083
2084 -- Build the body to inline only if really needed
2085
16b10ccc
AC
2086 if Can_Split_Unconstrained_Function (N) then
2087 Split_Unconstrained_Function (N, Spec_Id);
2088 Build_Body_To_Inline (N, Spec_Id);
2089 Set_Is_Inlined (Spec_Id);
540d8610 2090 end if;
16b10ccc 2091 end Check_And_Split_Unconstrained_Function;
3f80a182 2092
1773d80b
AC
2093 -------------------------------------
2094 -- Check_Package_Body_For_Inlining --
2095 -------------------------------------
540d8610 2096
1773d80b 2097 procedure Check_Package_Body_For_Inlining (N : Node_Id; P : Entity_Id) is
540d8610
ES
2098 Bname : Unit_Name_Type;
2099 E : Entity_Id;
2100 OK : Boolean;
2101
2102 begin
88f7d2d1
AC
2103 -- Legacy implementation (relying on frontend inlining)
2104
2105 if not Back_End_Inlining
039538bc 2106 and then Is_Compilation_Unit (P)
540d8610
ES
2107 and then not Is_Generic_Instance (P)
2108 then
2109 Bname := Get_Body_Name (Get_Unit_Name (Unit (N)));
2110
2111 E := First_Entity (P);
2112 while Present (E) loop
88f7d2d1
AC
2113 if Has_Pragma_Inline_Always (E)
2114 or else (Has_Pragma_Inline (E) and Front_End_Inlining)
2115 then
540d8610
ES
2116 if not Is_Loaded (Bname) then
2117 Load_Needed_Body (N, OK);
2118
2119 if OK then
2120
2121 -- Check we are not trying to inline a parent whose body
2122 -- depends on a child, when we are compiling the body of
2123 -- the child. Otherwise we have a potential elaboration
2124 -- circularity with inlined subprograms and with
2125 -- Taft-Amendment types.
2126
2127 declare
2128 Comp : Node_Id; -- Body just compiled
2129 Child_Spec : Entity_Id; -- Spec of main unit
2130 Ent : Entity_Id; -- For iteration
2131 With_Clause : Node_Id; -- Context of body.
2132
2133 begin
2134 if Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
2135 and then Present (Body_Entity (P))
2136 then
2137 Child_Spec :=
2138 Defining_Entity
2139 ((Unit (Library_Unit (Cunit (Main_Unit)))));
2140
2141 Comp :=
2142 Parent (Unit_Declaration_Node (Body_Entity (P)));
2143
2144 -- Check whether the context of the body just
2145 -- compiled includes a child of itself, and that
2146 -- child is the spec of the main compilation.
2147
2148 With_Clause := First (Context_Items (Comp));
2149 while Present (With_Clause) loop
2150 if Nkind (With_Clause) = N_With_Clause
2151 and then
2152 Scope (Entity (Name (With_Clause))) = P
2153 and then
2154 Entity (Name (With_Clause)) = Child_Spec
2155 then
2156 Error_Msg_Node_2 := Child_Spec;
2157 Error_Msg_NE
2158 ("body of & depends on child unit&??",
2159 With_Clause, P);
2160 Error_Msg_N
2161 ("\subprograms in body cannot be inlined??",
2162 With_Clause);
2163
2164 -- Disable further inlining from this unit,
2165 -- and keep Taft-amendment types incomplete.
2166
2167 Ent := First_Entity (P);
2168 while Present (Ent) loop
2169 if Is_Type (Ent)
3f80a182 2170 and then Has_Completion_In_Body (Ent)
540d8610
ES
2171 then
2172 Set_Full_View (Ent, Empty);
2173
2174 elsif Is_Subprogram (Ent) then
2175 Set_Is_Inlined (Ent, False);
2176 end if;
2177
2178 Next_Entity (Ent);
2179 end loop;
2180
2181 return;
2182 end if;
2183
2184 Next (With_Clause);
2185 end loop;
2186 end if;
2187 end;
2188
2189 elsif Ineffective_Inline_Warnings then
2190 Error_Msg_Unit_1 := Bname;
2191 Error_Msg_N
2192 ("unable to inline subprograms defined in $??", P);
2193 Error_Msg_N ("\body not found??", P);
2194 return;
2195 end if;
2196 end if;
2197
2198 return;
2199 end if;
2200
2201 Next_Entity (E);
2202 end loop;
2203 end if;
1773d80b 2204 end Check_Package_Body_For_Inlining;
540d8610
ES
2205
2206 --------------------
2207 -- Cleanup_Scopes --
2208 --------------------
2209
2210 procedure Cleanup_Scopes is
2211 Elmt : Elmt_Id;
2212 Decl : Node_Id;
2213 Scop : Entity_Id;
2214
2215 begin
2216 Elmt := First_Elmt (To_Clean);
2217 while Present (Elmt) loop
2218 Scop := Node (Elmt);
2219
2220 if Ekind (Scop) = E_Entry then
2221 Scop := Protected_Body_Subprogram (Scop);
2222
2223 elsif Is_Subprogram (Scop)
2224 and then Is_Protected_Type (Scope (Scop))
2225 and then Present (Protected_Body_Subprogram (Scop))
2226 then
3f80a182
AC
2227 -- If a protected operation contains an instance, its cleanup
2228 -- operations have been delayed, and the subprogram has been
2229 -- rewritten in the expansion of the enclosing protected body. It
2230 -- is the corresponding subprogram that may require the cleanup
2231 -- operations, so propagate the information that triggers cleanup
2232 -- activity.
540d8610
ES
2233
2234 Set_Uses_Sec_Stack
2235 (Protected_Body_Subprogram (Scop),
2236 Uses_Sec_Stack (Scop));
2237
2238 Scop := Protected_Body_Subprogram (Scop);
2239 end if;
2240
2241 if Ekind (Scop) = E_Block then
2242 Decl := Parent (Block_Node (Scop));
2243
2244 else
2245 Decl := Unit_Declaration_Node (Scop);
2246
3f80a182
AC
2247 if Nkind_In (Decl, N_Subprogram_Declaration,
2248 N_Task_Type_Declaration,
2249 N_Subprogram_Body_Stub)
540d8610
ES
2250 then
2251 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
2252 end if;
2253 end if;
2254
2255 Push_Scope (Scop);
2256 Expand_Cleanup_Actions (Decl);
2257 End_Scope;
2258
2259 Elmt := Next_Elmt (Elmt);
2260 end loop;
2261 end Cleanup_Scopes;
2262
2263 -------------------------
2264 -- Expand_Inlined_Call --
2265 -------------------------
2266
2267 procedure Expand_Inlined_Call
2268 (N : Node_Id;
2269 Subp : Entity_Id;
2270 Orig_Subp : Entity_Id)
2271 is
2272 Loc : constant Source_Ptr := Sloc (N);
2273 Is_Predef : constant Boolean :=
8ab31c0c 2274 Is_Predefined_Unit (Get_Source_Unit (Subp));
540d8610
ES
2275 Orig_Bod : constant Node_Id :=
2276 Body_To_Inline (Unit_Declaration_Node (Subp));
2277
2278 Blk : Node_Id;
2279 Decl : Node_Id;
2280 Decls : constant List_Id := New_List;
3f80a182 2281 Exit_Lab : Entity_Id := Empty;
540d8610
ES
2282 F : Entity_Id;
2283 A : Node_Id;
dcd5fd67 2284 Lab_Decl : Node_Id := Empty;
540d8610
ES
2285 Lab_Id : Node_Id;
2286 New_A : Node_Id;
dcd5fd67 2287 Num_Ret : Nat := 0;
540d8610
ES
2288 Ret_Type : Entity_Id;
2289
dcd5fd67 2290 Targ : Node_Id := Empty;
540d8610
ES
2291 -- The target of the call. If context is an assignment statement then
2292 -- this is the left-hand side of the assignment, else it is a temporary
2293 -- to which the return value is assigned prior to rewriting the call.
2294
85be939e 2295 Targ1 : Node_Id := Empty;
540d8610
ES
2296 -- A separate target used when the return type is unconstrained
2297
2298 Temp : Entity_Id;
2299 Temp_Typ : Entity_Id;
2300
2301 Return_Object : Entity_Id := Empty;
2302 -- Entity in declaration in an extended_return_statement
2303
2304 Is_Unc : Boolean;
2305 Is_Unc_Decl : Boolean;
2306 -- If the type returned by the function is unconstrained and the call
2307 -- can be inlined, special processing is required.
2308
64f5d139
JM
2309 procedure Declare_Postconditions_Result;
2310 -- When generating C code, declare _Result, which may be used in the
2311 -- inlined _Postconditions procedure to verify the return value.
2312
540d8610
ES
2313 procedure Make_Exit_Label;
2314 -- Build declaration for exit label to be used in Return statements,
2315 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
2316 -- declaration). Does nothing if Exit_Lab already set.
2317
2318 function Process_Formals (N : Node_Id) return Traverse_Result;
2319 -- Replace occurrence of a formal with the corresponding actual, or the
2320 -- thunk generated for it. Replace a return statement with an assignment
2321 -- to the target of the call, with appropriate conversions if needed.
2322
2323 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
2324 -- If the call being expanded is that of an internal subprogram, set the
2325 -- sloc of the generated block to that of the call itself, so that the
52c1498c
YM
2326 -- expansion is skipped by the "next" command in gdb. Same processing
2327 -- for a subprogram in a predefined file, e.g. Ada.Tags. If
2328 -- Debug_Generated_Code is true, suppress this change to simplify our
2329 -- own development. Same in GNATprove mode, to ensure that warnings and
2330 -- diagnostics point to the proper location.
540d8610
ES
2331
2332 procedure Reset_Dispatching_Calls (N : Node_Id);
2333 -- In subtree N search for occurrences of dispatching calls that use the
2334 -- Ada 2005 Object.Operation notation and the object is a formal of the
2335 -- inlined subprogram. Reset the entity associated with Operation in all
2336 -- the found occurrences.
2337
2338 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
2339 -- If the function body is a single expression, replace call with
2340 -- expression, else insert block appropriately.
2341
2342 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
2343 -- If procedure body has no local variables, inline body without
2344 -- creating block, otherwise rewrite call with block.
2345
2346 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2347 -- Determine whether a formal parameter is used only once in Orig_Bod
2348
64f5d139
JM
2349 -----------------------------------
2350 -- Declare_Postconditions_Result --
2351 -----------------------------------
2352
2353 procedure Declare_Postconditions_Result is
2354 Enclosing_Subp : constant Entity_Id := Scope (Subp);
2355
2356 begin
2357 pragma Assert
2358 (Modify_Tree_For_C
2359 and then Is_Subprogram (Enclosing_Subp)
2360 and then Present (Postconditions_Proc (Enclosing_Subp)));
2361
2362 if Ekind (Enclosing_Subp) = E_Function then
fb757f7d
AC
2363 if Nkind (First (Parameter_Associations (N))) in
2364 N_Numeric_Or_String_Literal
64f5d139
JM
2365 then
2366 Append_To (Declarations (Blk),
2367 Make_Object_Declaration (Loc,
2368 Defining_Identifier =>
2369 Make_Defining_Identifier (Loc, Name_uResult),
2370 Constant_Present => True,
2371 Object_Definition =>
2372 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
2373 Expression =>
2374 New_Copy_Tree (First (Parameter_Associations (N)))));
2375 else
2376 Append_To (Declarations (Blk),
2377 Make_Object_Renaming_Declaration (Loc,
2378 Defining_Identifier =>
2379 Make_Defining_Identifier (Loc, Name_uResult),
2380 Subtype_Mark =>
2381 New_Occurrence_Of (Etype (Enclosing_Subp), Loc),
2382 Name =>
2383 New_Copy_Tree (First (Parameter_Associations (N)))));
2384 end if;
2385 end if;
2386 end Declare_Postconditions_Result;
2387
540d8610
ES
2388 ---------------------
2389 -- Make_Exit_Label --
2390 ---------------------
2391
2392 procedure Make_Exit_Label is
2393 Lab_Ent : Entity_Id;
2394 begin
2395 if No (Exit_Lab) then
2396 Lab_Ent := Make_Temporary (Loc, 'L');
2397 Lab_Id := New_Occurrence_Of (Lab_Ent, Loc);
2398 Exit_Lab := Make_Label (Loc, Lab_Id);
2399 Lab_Decl :=
2400 Make_Implicit_Label_Declaration (Loc,
3f80a182
AC
2401 Defining_Identifier => Lab_Ent,
2402 Label_Construct => Exit_Lab);
540d8610
ES
2403 end if;
2404 end Make_Exit_Label;
2405
2406 ---------------------
2407 -- Process_Formals --
2408 ---------------------
2409
2410 function Process_Formals (N : Node_Id) return Traverse_Result is
2411 A : Entity_Id;
2412 E : Entity_Id;
2413 Ret : Node_Id;
2414
2415 begin
2416 if Is_Entity_Name (N) and then Present (Entity (N)) then
2417 E := Entity (N);
2418
2419 if Is_Formal (E) and then Scope (E) = Subp then
2420 A := Renamed_Object (E);
2421
2422 -- Rewrite the occurrence of the formal into an occurrence of
2423 -- the actual. Also establish visibility on the proper view of
2424 -- the actual's subtype for the body's context (if the actual's
2425 -- subtype is private at the call point but its full view is
2426 -- visible to the body, then the inlined tree here must be
2427 -- analyzed with the full view).
2428
2429 if Is_Entity_Name (A) then
1db700c3 2430 Rewrite (N, New_Occurrence_Of (Entity (A), Sloc (N)));
540d8610
ES
2431 Check_Private_View (N);
2432
2433 elsif Nkind (A) = N_Defining_Identifier then
1db700c3 2434 Rewrite (N, New_Occurrence_Of (A, Sloc (N)));
540d8610
ES
2435 Check_Private_View (N);
2436
2437 -- Numeric literal
2438
2439 else
2440 Rewrite (N, New_Copy (A));
2441 end if;
2442 end if;
2443
2444 return Skip;
2445
2446 elsif Is_Entity_Name (N)
2447 and then Present (Return_Object)
2448 and then Chars (N) = Chars (Return_Object)
2449 then
2450 -- Occurrence within an extended return statement. The return
2451 -- object is local to the body been inlined, and thus the generic
2452 -- copy is not analyzed yet, so we match by name, and replace it
2453 -- with target of call.
2454
2455 if Nkind (Targ) = N_Defining_Identifier then
2456 Rewrite (N, New_Occurrence_Of (Targ, Loc));
2457 else
2458 Rewrite (N, New_Copy_Tree (Targ));
2459 end if;
2460
2461 return Skip;
2462
2463 elsif Nkind (N) = N_Simple_Return_Statement then
2464 if No (Expression (N)) then
00f45f30 2465 Num_Ret := Num_Ret + 1;
540d8610
ES
2466 Make_Exit_Label;
2467 Rewrite (N,
2468 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
2469
2470 else
2471 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
2472 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
2473 then
2474 -- Function body is a single expression. No need for
2475 -- exit label.
2476
2477 null;
2478
2479 else
2480 Num_Ret := Num_Ret + 1;
2481 Make_Exit_Label;
2482 end if;
2483
2484 -- Because of the presence of private types, the views of the
031936bc
YM
2485 -- expression and the context may be different, so place
2486 -- a type conversion to the context type to avoid spurious
540d8610
ES
2487 -- errors, e.g. when the expression is a numeric literal and
2488 -- the context is private. If the expression is an aggregate,
2489 -- use a qualified expression, because an aggregate is not a
031936bc
YM
2490 -- legal argument of a conversion. Ditto for numeric, character
2491 -- and string literals, and attributes that yield a universal
2492 -- type, because those must be resolved to a specific type.
2493
2494 if Nkind_In (Expression (N), N_Aggregate,
031936bc 2495 N_Character_Literal,
663afa9f 2496 N_Null,
031936bc 2497 N_String_Literal)
89a53f83 2498 or else Yields_Universal_Type (Expression (N))
540d8610
ES
2499 then
2500 Ret :=
2501 Make_Qualified_Expression (Sloc (N),
2502 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3f80a182 2503 Expression => Relocate_Node (Expression (N)));
031936bc
YM
2504
2505 -- Use an unchecked type conversion between access types, for
2506 -- which a type conversion would not always be valid, as no
2507 -- check may result from the conversion.
2508
2509 elsif Is_Access_Type (Ret_Type) then
540d8610
ES
2510 Ret :=
2511 Unchecked_Convert_To
2512 (Ret_Type, Relocate_Node (Expression (N)));
031936bc
YM
2513
2514 -- Otherwise use a type conversion, which may trigger a check
2515
2516 else
2517 Ret :=
2518 Make_Type_Conversion (Sloc (N),
2519 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2520 Expression => Relocate_Node (Expression (N)));
540d8610
ES
2521 end if;
2522
2523 if Nkind (Targ) = N_Defining_Identifier then
2524 Rewrite (N,
2525 Make_Assignment_Statement (Loc,
2526 Name => New_Occurrence_Of (Targ, Loc),
2527 Expression => Ret));
2528 else
2529 Rewrite (N,
2530 Make_Assignment_Statement (Loc,
2531 Name => New_Copy (Targ),
2532 Expression => Ret));
2533 end if;
2534
2535 Set_Assignment_OK (Name (N));
2536
2537 if Present (Exit_Lab) then
2538 Insert_After (N,
2539 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
2540 end if;
2541 end if;
2542
2543 return OK;
2544
2545 -- An extended return becomes a block whose first statement is the
2546 -- assignment of the initial expression of the return object to the
2547 -- target of the call itself.
2548
2549 elsif Nkind (N) = N_Extended_Return_Statement then
2550 declare
2551 Return_Decl : constant Entity_Id :=
2552 First (Return_Object_Declarations (N));
2553 Assign : Node_Id;
2554
2555 begin
2556 Return_Object := Defining_Identifier (Return_Decl);
2557
2558 if Present (Expression (Return_Decl)) then
2559 if Nkind (Targ) = N_Defining_Identifier then
2560 Assign :=
2561 Make_Assignment_Statement (Loc,
2562 Name => New_Occurrence_Of (Targ, Loc),
2563 Expression => Expression (Return_Decl));
2564 else
2565 Assign :=
2566 Make_Assignment_Statement (Loc,
2567 Name => New_Copy (Targ),
2568 Expression => Expression (Return_Decl));
2569 end if;
2570
2571 Set_Assignment_OK (Name (Assign));
2572
2573 if No (Handled_Statement_Sequence (N)) then
2574 Set_Handled_Statement_Sequence (N,
2575 Make_Handled_Sequence_Of_Statements (Loc,
2576 Statements => New_List));
2577 end if;
2578
2579 Prepend (Assign,
2580 Statements (Handled_Statement_Sequence (N)));
2581 end if;
2582
2583 Rewrite (N,
2584 Make_Block_Statement (Loc,
2585 Handled_Statement_Sequence =>
2586 Handled_Statement_Sequence (N)));
2587
2588 return OK;
2589 end;
2590
2591 -- Remove pragma Unreferenced since it may refer to formals that
2592 -- are not visible in the inlined body, and in any case we will
2593 -- not be posting warnings on the inlined body so it is unneeded.
2594
2595 elsif Nkind (N) = N_Pragma
6e759c2a 2596 and then Pragma_Name (N) = Name_Unreferenced
540d8610
ES
2597 then
2598 Rewrite (N, Make_Null_Statement (Sloc (N)));
2599 return OK;
2600
2601 else
2602 return OK;
2603 end if;
2604 end Process_Formals;
2605
2606 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
2607
2608 ------------------
2609 -- Process_Sloc --
2610 ------------------
2611
2612 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
2613 begin
2614 if not Debug_Generated_Code then
2615 Set_Sloc (Nod, Sloc (N));
2616 Set_Comes_From_Source (Nod, False);
2617 end if;
2618
2619 return OK;
2620 end Process_Sloc;
2621
2622 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
2623
2624 ------------------------------
2625 -- Reset_Dispatching_Calls --
2626 ------------------------------
2627
2628 procedure Reset_Dispatching_Calls (N : Node_Id) is
2629
2630 function Do_Reset (N : Node_Id) return Traverse_Result;
2631 -- Comment required ???
2632
2633 --------------
2634 -- Do_Reset --
2635 --------------
2636
2637 function Do_Reset (N : Node_Id) return Traverse_Result is
2638 begin
2639 if Nkind (N) = N_Procedure_Call_Statement
2640 and then Nkind (Name (N)) = N_Selected_Component
2641 and then Nkind (Prefix (Name (N))) = N_Identifier
2642 and then Is_Formal (Entity (Prefix (Name (N))))
2643 and then Is_Dispatching_Operation
2644 (Entity (Selector_Name (Name (N))))
2645 then
2646 Set_Entity (Selector_Name (Name (N)), Empty);
2647 end if;
2648
2649 return OK;
2650 end Do_Reset;
2651
2652 function Do_Reset_Calls is new Traverse_Func (Do_Reset);
2653
2654 -- Local variables
2655
2656 Dummy : constant Traverse_Result := Do_Reset_Calls (N);
2657 pragma Unreferenced (Dummy);
2658
2659 -- Start of processing for Reset_Dispatching_Calls
2660
2661 begin
2662 null;
2663 end Reset_Dispatching_Calls;
2664
2665 ---------------------------
2666 -- Rewrite_Function_Call --
2667 ---------------------------
2668
2669 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
2670 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2671 Fst : constant Node_Id := First (Statements (HSS));
2672
2673 begin
2674 -- Optimize simple case: function body is a single return statement,
2675 -- which has been expanded into an assignment.
2676
2677 if Is_Empty_List (Declarations (Blk))
2678 and then Nkind (Fst) = N_Assignment_Statement
2679 and then No (Next (Fst))
2680 then
2681 -- The function call may have been rewritten as the temporary
2682 -- that holds the result of the call, in which case remove the
2683 -- now useless declaration.
2684
2685 if Nkind (N) = N_Identifier
2686 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2687 then
2688 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
2689 end if;
2690
2691 Rewrite (N, Expression (Fst));
2692
2693 elsif Nkind (N) = N_Identifier
2694 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2695 then
2696 -- The block assigns the result of the call to the temporary
2697
2698 Insert_After (Parent (Entity (N)), Blk);
2699
2700 -- If the context is an assignment, and the left-hand side is free of
2701 -- side-effects, the replacement is also safe.
2702 -- Can this be generalized further???
2703
2704 elsif Nkind (Parent (N)) = N_Assignment_Statement
2705 and then
2706 (Is_Entity_Name (Name (Parent (N)))
2707 or else
2708 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
2709 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
2710
2711 or else
2712 (Nkind (Name (Parent (N))) = N_Selected_Component
2713 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
2714 then
2715 -- Replace assignment with the block
2716
2717 declare
2718 Original_Assignment : constant Node_Id := Parent (N);
2719
2720 begin
2721 -- Preserve the original assignment node to keep the complete
2722 -- assignment subtree consistent enough for Analyze_Assignment
2723 -- to proceed (specifically, the original Lhs node must still
2724 -- have an assignment statement as its parent).
2725
2726 -- We cannot rely on Original_Node to go back from the block
2727 -- node to the assignment node, because the assignment might
2728 -- already be a rewrite substitution.
2729
2730 Discard_Node (Relocate_Node (Original_Assignment));
2731 Rewrite (Original_Assignment, Blk);
2732 end;
2733
2734 elsif Nkind (Parent (N)) = N_Object_Declaration then
2735
2736 -- A call to a function which returns an unconstrained type
2737 -- found in the expression initializing an object-declaration is
2738 -- expanded into a procedure call which must be added after the
2739 -- object declaration.
2740
ea0c8cfb 2741 if Is_Unc_Decl and Back_End_Inlining then
540d8610
ES
2742 Insert_Action_After (Parent (N), Blk);
2743 else
2744 Set_Expression (Parent (N), Empty);
2745 Insert_After (Parent (N), Blk);
2746 end if;
2747
6c26bac2 2748 elsif Is_Unc and then not Back_End_Inlining then
540d8610
ES
2749 Insert_Before (Parent (N), Blk);
2750 end if;
2751 end Rewrite_Function_Call;
2752
2753 ----------------------------
2754 -- Rewrite_Procedure_Call --
2755 ----------------------------
2756
2757 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
2758 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2759
2760 begin
2761 -- If there is a transient scope for N, this will be the scope of the
2762 -- actions for N, and the statements in Blk need to be within this
2763 -- scope. For example, they need to have visibility on the constant
2764 -- declarations created for the formals.
2765
2766 -- If N needs no transient scope, and if there are no declarations in
2767 -- the inlined body, we can do a little optimization and insert the
2768 -- statements for the body directly after N, and rewrite N to a
2769 -- null statement, instead of rewriting N into a full-blown block
2770 -- statement.
2771
2772 if not Scope_Is_Transient
2773 and then Is_Empty_List (Declarations (Blk))
2774 then
2775 Insert_List_After (N, Statements (HSS));
2776 Rewrite (N, Make_Null_Statement (Loc));
2777 else
2778 Rewrite (N, Blk);
2779 end if;
2780 end Rewrite_Procedure_Call;
2781
2782 -------------------------
2783 -- Formal_Is_Used_Once --
2784 -------------------------
2785
2786 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
2787 Use_Counter : Int := 0;
2788
2789 function Count_Uses (N : Node_Id) return Traverse_Result;
2790 -- Traverse the tree and count the uses of the formal parameter.
2791 -- In this case, for optimization purposes, we do not need to
2792 -- continue the traversal once more than one use is encountered.
2793
2794 ----------------
2795 -- Count_Uses --
2796 ----------------
2797
2798 function Count_Uses (N : Node_Id) return Traverse_Result is
2799 begin
2800 -- The original node is an identifier
2801
2802 if Nkind (N) = N_Identifier
2803 and then Present (Entity (N))
2804
2805 -- Original node's entity points to the one in the copied body
2806
2807 and then Nkind (Entity (N)) = N_Identifier
2808 and then Present (Entity (Entity (N)))
2809
2810 -- The entity of the copied node is the formal parameter
2811
2812 and then Entity (Entity (N)) = Formal
2813 then
2814 Use_Counter := Use_Counter + 1;
2815
2816 if Use_Counter > 1 then
2817
2818 -- Denote more than one use and abandon the traversal
2819
2820 Use_Counter := 2;
2821 return Abandon;
2822
2823 end if;
2824 end if;
2825
2826 return OK;
2827 end Count_Uses;
2828
2829 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
2830
2831 -- Start of processing for Formal_Is_Used_Once
2832
2833 begin
2834 Count_Formal_Uses (Orig_Bod);
2835 return Use_Counter = 1;
2836 end Formal_Is_Used_Once;
2837
2838 -- Start of processing for Expand_Inlined_Call
2839
2840 begin
2841 -- Initializations for old/new semantics
2842
6c26bac2 2843 if not Back_End_Inlining then
540d8610
ES
2844 Is_Unc := Is_Array_Type (Etype (Subp))
2845 and then not Is_Constrained (Etype (Subp));
2846 Is_Unc_Decl := False;
2847 else
2848 Is_Unc := Returns_Unconstrained_Type (Subp)
2849 and then Optimization_Level > 0;
2850 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
2851 and then Is_Unc;
2852 end if;
2853
2854 -- Check for an illegal attempt to inline a recursive procedure. If the
2855 -- subprogram has parameters this is detected when trying to supply a
2856 -- binding for parameters that already have one. For parameterless
2857 -- subprograms this must be done explicitly.
2858
2859 if In_Open_Scopes (Subp) then
db99c46e
AC
2860 Cannot_Inline
2861 ("cannot inline call to recursive subprogram?", N, Subp);
540d8610
ES
2862 Set_Is_Inlined (Subp, False);
2863 return;
2864
2865 -- Skip inlining if this is not a true inlining since the attribute
09edc2c2
AC
2866 -- Body_To_Inline is also set for renamings (see sinfo.ads). For a
2867 -- true inlining, Orig_Bod has code rather than being an entity.
540d8610
ES
2868
2869 elsif Nkind (Orig_Bod) in N_Entity then
09edc2c2 2870 return;
540d8610
ES
2871
2872 -- Skip inlining if the function returns an unconstrained type using
2873 -- an extended return statement since this part of the new inlining
2874 -- model which is not yet supported by the current implementation. ???
2875
2876 elsif Is_Unc
2877 and then
db99c46e
AC
2878 Nkind (First (Statements (Handled_Statement_Sequence (Orig_Bod)))) =
2879 N_Extended_Return_Statement
6c26bac2 2880 and then not Back_End_Inlining
540d8610
ES
2881 then
2882 return;
2883 end if;
2884
2885 if Nkind (Orig_Bod) = N_Defining_Identifier
2886 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
2887 then
2888 -- Subprogram is renaming_as_body. Calls occurring after the renaming
2889 -- can be replaced with calls to the renamed entity directly, because
2890 -- the subprograms are subtype conformant. If the renamed subprogram
2891 -- is an inherited operation, we must redo the expansion because
2892 -- implicit conversions may be needed. Similarly, if the renamed
2893 -- entity is inlined, expand the call for further optimizations.
2894
2895 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
2896
2897 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
2898 Expand_Call (N);
2899 end if;
2900
2901 return;
2902 end if;
2903
2904 -- Register the call in the list of inlined calls
2905
21c51f53 2906 Append_New_Elmt (N, To => Inlined_Calls);
540d8610
ES
2907
2908 -- Use generic machinery to copy body of inlined subprogram, as if it
2909 -- were an instantiation, resetting source locations appropriately, so
2910 -- that nested inlined calls appear in the main unit.
2911
2912 Save_Env (Subp, Empty);
2913 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
2914
2915 -- Old semantics
2916
6c26bac2 2917 if not Back_End_Inlining then
540d8610
ES
2918 declare
2919 Bod : Node_Id;
2920
2921 begin
2922 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2923 Blk :=
2924 Make_Block_Statement (Loc,
3f80a182 2925 Declarations => Declarations (Bod),
540d8610
ES
2926 Handled_Statement_Sequence =>
2927 Handled_Statement_Sequence (Bod));
2928
2929 if No (Declarations (Bod)) then
2930 Set_Declarations (Blk, New_List);
2931 end if;
2932
64f5d139
JM
2933 -- When generating C code, declare _Result, which may be used to
2934 -- verify the return value.
2935
2936 if Modify_Tree_For_C
2937 and then Nkind (N) = N_Procedure_Call_Statement
2938 and then Chars (Name (N)) = Name_uPostconditions
2939 then
2940 Declare_Postconditions_Result;
2941 end if;
2942
540d8610
ES
2943 -- For the unconstrained case, capture the name of the local
2944 -- variable that holds the result. This must be the first
2945 -- declaration in the block, because its bounds cannot depend
2946 -- on local variables. Otherwise there is no way to declare the
2947 -- result outside of the block. Needless to say, in general the
2948 -- bounds will depend on the actuals in the call.
2949
2950 -- If the context is an assignment statement, as is the case
2951 -- for the expansion of an extended return, the left-hand side
2952 -- provides bounds even if the return type is unconstrained.
2953
2954 if Is_Unc then
2955 declare
2956 First_Decl : Node_Id;
2957
2958 begin
2959 First_Decl := First (Declarations (Blk));
2960
2961 if Nkind (First_Decl) /= N_Object_Declaration then
2962 return;
2963 end if;
2964
2965 if Nkind (Parent (N)) /= N_Assignment_Statement then
2966 Targ1 := Defining_Identifier (First_Decl);
2967 else
2968 Targ1 := Name (Parent (N));
2969 end if;
2970 end;
2971 end if;
2972 end;
2973
2974 -- New semantics
2975
2976 else
2977 declare
2978 Bod : Node_Id;
2979
2980 begin
2981 -- General case
2982
2983 if not Is_Unc then
2984 Bod :=
2985 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2986 Blk :=
2987 Make_Block_Statement (Loc,
3f80a182
AC
2988 Declarations => Declarations (Bod),
2989 Handled_Statement_Sequence =>
2990 Handled_Statement_Sequence (Bod));
540d8610
ES
2991
2992 -- Inline a call to a function that returns an unconstrained type.
2993 -- The semantic analyzer checked that frontend-inlined functions
2994 -- returning unconstrained types have no declarations and have
2995 -- a single extended return statement. As part of its processing
643827e9 2996 -- the function was split into two subprograms: a procedure P' and
66f95f60 2997 -- a function F' that has a block with a call to procedure P' (see
540d8610
ES
2998 -- Split_Unconstrained_Function).
2999
3000 else
3001 pragma Assert
3002 (Nkind
3003 (First
3f80a182
AC
3004 (Statements (Handled_Statement_Sequence (Orig_Bod)))) =
3005 N_Block_Statement);
540d8610
ES
3006
3007 declare
3008 Blk_Stmt : constant Node_Id :=
3f80a182 3009 First (Statements (Handled_Statement_Sequence (Orig_Bod)));
540d8610 3010 First_Stmt : constant Node_Id :=
3f80a182 3011 First (Statements (Handled_Statement_Sequence (Blk_Stmt)));
540d8610
ES
3012 Second_Stmt : constant Node_Id := Next (First_Stmt);
3013
3014 begin
3015 pragma Assert
3016 (Nkind (First_Stmt) = N_Procedure_Call_Statement
3017 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
3018 and then No (Next (Second_Stmt)));
3019
3020 Bod :=
3021 Copy_Generic_Node
3022 (First
3023 (Statements (Handled_Statement_Sequence (Orig_Bod))),
3024 Empty, Instantiating => True);
3025 Blk := Bod;
3026
3027 -- Capture the name of the local variable that holds the
3028 -- result. This must be the first declaration in the block,
3029 -- because its bounds cannot depend on local variables.
3030 -- Otherwise there is no way to declare the result outside
3031 -- of the block. Needless to say, in general the bounds will
3032 -- depend on the actuals in the call.
3033
3034 if Nkind (Parent (N)) /= N_Assignment_Statement then
3035 Targ1 := Defining_Identifier (First (Declarations (Blk)));
3036
3037 -- If the context is an assignment statement, as is the case
3038 -- for the expansion of an extended return, the left-hand
3039 -- side provides bounds even if the return type is
3040 -- unconstrained.
3041
3042 else
3043 Targ1 := Name (Parent (N));
3044 end if;
3045 end;
3046 end if;
3047
3048 if No (Declarations (Bod)) then
3049 Set_Declarations (Blk, New_List);
3050 end if;
3051 end;
3052 end if;
3053
3054 -- If this is a derived function, establish the proper return type
3055
3056 if Present (Orig_Subp) and then Orig_Subp /= Subp then
3057 Ret_Type := Etype (Orig_Subp);
3058 else
3059 Ret_Type := Etype (Subp);
3060 end if;
3061
3062 -- Create temporaries for the actuals that are expressions, or that are
3063 -- scalars and require copying to preserve semantics.
3064
3065 F := First_Formal (Subp);
3066 A := First_Actual (N);
3067 while Present (F) loop
3068 if Present (Renamed_Object (F)) then
4e6768ab 3069
662c2ad4 3070 -- If expander is active, it is an error to try to inline a
52c1498c
YM
3071 -- recursive program. In GNATprove mode, just indicate that the
3072 -- inlining will not happen, and mark the subprogram as not always
3073 -- inlined.
4e6768ab 3074
4bd4bb7f 3075 if GNATprove_Mode then
4e6768ab
AC
3076 Cannot_Inline
3077 ("cannot inline call to recursive subprogram?", N, Subp);
4bd4bb7f
AC
3078 Set_Is_Inlined_Always (Subp, False);
3079 else
3080 Error_Msg_N
3081 ("cannot inline call to recursive subprogram", N);
4e6768ab
AC
3082 end if;
3083
540d8610
ES
3084 return;
3085 end if;
3086
3087 -- Reset Last_Assignment for any parameters of mode out or in out, to
3088 -- prevent spurious warnings about overwriting for assignments to the
3089 -- formal in the inlined code.
3090
3091 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
3092 Set_Last_Assignment (Entity (A), Empty);
3093 end if;
3094
3095 -- If the argument may be a controlling argument in a call within
3096 -- the inlined body, we must preserve its classwide nature to insure
3097 -- that dynamic dispatching take place subsequently. If the formal
3098 -- has a constraint it must be preserved to retain the semantics of
3099 -- the body.
3100
3101 if Is_Class_Wide_Type (Etype (F))
3102 or else (Is_Access_Type (Etype (F))
3103 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
3104 then
3105 Temp_Typ := Etype (F);
3106
3107 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3108 and then Etype (F) /= Base_Type (Etype (F))
f4ef7b06 3109 and then Is_Constrained (Etype (F))
540d8610
ES
3110 then
3111 Temp_Typ := Etype (F);
f4ef7b06 3112
540d8610
ES
3113 else
3114 Temp_Typ := Etype (A);
3115 end if;
3116
3117 -- If the actual is a simple name or a literal, no need to
3118 -- create a temporary, object can be used directly.
3119
3120 -- If the actual is a literal and the formal has its address taken,
3121 -- we cannot pass the literal itself as an argument, so its value
3de3a1be
YM
3122 -- must be captured in a temporary. Skip this optimization in
3123 -- GNATprove mode, to make sure any check on a type conversion
3124 -- will be issued.
540d8610
ES
3125
3126 if (Is_Entity_Name (A)
3127 and then
da9683f4
AC
3128 (not Is_Scalar_Type (Etype (A))
3129 or else Ekind (Entity (A)) = E_Enumeration_Literal)
3de3a1be 3130 and then not GNATprove_Mode)
540d8610
ES
3131
3132 -- When the actual is an identifier and the corresponding formal is
3133 -- used only once in the original body, the formal can be substituted
3de3a1be
YM
3134 -- directly with the actual parameter. Skip this optimization in
3135 -- GNATprove mode, to make sure any check on a type conversion
3136 -- will be issued.
540d8610 3137
da9683f4
AC
3138 or else
3139 (Nkind (A) = N_Identifier
3140 and then Formal_Is_Used_Once (F)
3141 and then not GNATprove_Mode)
540d8610
ES
3142
3143 or else
3144 (Nkind_In (A, N_Real_Literal,
3145 N_Integer_Literal,
3146 N_Character_Literal)
3147 and then not Address_Taken (F))
3148 then
3149 if Etype (F) /= Etype (A) then
3150 Set_Renamed_Object
3151 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3152 else
3153 Set_Renamed_Object (F, A);
3154 end if;
3155
3156 else
3157 Temp := Make_Temporary (Loc, 'C');
3158
3159 -- If the actual for an in/in-out parameter is a view conversion,
3160 -- make it into an unchecked conversion, given that an untagged
3161 -- type conversion is not a proper object for a renaming.
3162
3163 -- In-out conversions that involve real conversions have already
3164 -- been transformed in Expand_Actuals.
3165
3166 if Nkind (A) = N_Type_Conversion
3167 and then Ekind (F) /= E_In_Parameter
3168 then
3169 New_A :=
3170 Make_Unchecked_Type_Conversion (Loc,
3171 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3172 Expression => Relocate_Node (Expression (A)));
3173
bfaf8a97
AC
3174 -- In GNATprove mode, keep the most precise type of the actual for
3175 -- the temporary variable, when the formal type is unconstrained.
3176 -- Otherwise, the AST may contain unexpected assignment statements
dafe11cd
HK
3177 -- to a temporary variable of unconstrained type renaming a local
3178 -- variable of constrained type, which is not expected by
3179 -- GNATprove.
f4ef7b06 3180
bfaf8a97
AC
3181 elsif Etype (F) /= Etype (A)
3182 and then (not GNATprove_Mode or else Is_Constrained (Etype (F)))
3183 then
4f324de2 3184 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
540d8610
ES
3185 Temp_Typ := Etype (F);
3186
3187 else
3188 New_A := Relocate_Node (A);
3189 end if;
3190
3191 Set_Sloc (New_A, Sloc (N));
3192
3193 -- If the actual has a by-reference type, it cannot be copied,
3194 -- so its value is captured in a renaming declaration. Otherwise
3195 -- declare a local constant initialized with the actual.
3196
3197 -- We also use a renaming declaration for expressions of an array
3198 -- type that is not bit-packed, both for efficiency reasons and to
3199 -- respect the semantics of the call: in most cases the original
3200 -- call will pass the parameter by reference, and thus the inlined
3201 -- code will have the same semantics.
3202
36428cc4
AC
3203 -- Finally, we need a renaming declaration in the case of limited
3204 -- types for which initialization cannot be by copy either.
3205
540d8610
ES
3206 if Ekind (F) = E_In_Parameter
3207 and then not Is_By_Reference_Type (Etype (A))
36428cc4 3208 and then not Is_Limited_Type (Etype (A))
540d8610
ES
3209 and then
3210 (not Is_Array_Type (Etype (A))
3211 or else not Is_Object_Reference (A)
3212 or else Is_Bit_Packed_Array (Etype (A)))
3213 then
3214 Decl :=
3215 Make_Object_Declaration (Loc,
3216 Defining_Identifier => Temp,
3217 Constant_Present => True,
3218 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3219 Expression => New_A);
3de3a1be 3220
540d8610 3221 else
3de3a1be
YM
3222 -- In GNATprove mode, make an explicit copy of input
3223 -- parameters when formal and actual types differ, to make
3224 -- sure any check on the type conversion will be issued.
3225 -- The legality of the copy is ensured by calling first
3226 -- Call_Can_Be_Inlined_In_GNATprove_Mode.
3227
3228 if GNATprove_Mode
3229 and then Ekind (F) /= E_Out_Parameter
3230 and then not Same_Type (Etype (F), Etype (A))
3231 then
3232 pragma Assert (not (Is_By_Reference_Type (Etype (A))));
3233 pragma Assert (not (Is_Limited_Type (Etype (A))));
72cdccfa 3234
3abbc5c2 3235 Append_To (Decls,
3de3a1be 3236 Make_Object_Declaration (Loc,
3abbc5c2 3237 Defining_Identifier => Make_Temporary (Loc, 'C'),
3de3a1be
YM
3238 Constant_Present => True,
3239 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3abbc5c2 3240 Expression => New_Copy_Tree (New_A)));
3de3a1be
YM
3241 end if;
3242
540d8610
ES
3243 Decl :=
3244 Make_Object_Renaming_Declaration (Loc,
3245 Defining_Identifier => Temp,
3246 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3247 Name => New_A);
3248 end if;
3249
3250 Append (Decl, Decls);
3251 Set_Renamed_Object (F, Temp);
3252 end if;
3253
3254 Next_Formal (F);
3255 Next_Actual (A);
3256 end loop;
3257
3258 -- Establish target of function call. If context is not assignment or
3259 -- declaration, create a temporary as a target. The declaration for the
3260 -- temporary may be subsequently optimized away if the body is a single
3261 -- expression, or if the left-hand side of the assignment is simple
3262 -- enough, i.e. an entity or an explicit dereference of one.
3263
3264 if Ekind (Subp) = E_Function then
3265 if Nkind (Parent (N)) = N_Assignment_Statement
3266 and then Is_Entity_Name (Name (Parent (N)))
3267 then
3268 Targ := Name (Parent (N));
3269
3270 elsif Nkind (Parent (N)) = N_Assignment_Statement
3271 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
3272 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3273 then
3274 Targ := Name (Parent (N));
3275
3276 elsif Nkind (Parent (N)) = N_Assignment_Statement
3277 and then Nkind (Name (Parent (N))) = N_Selected_Component
3278 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3279 then
3280 Targ := New_Copy_Tree (Name (Parent (N)));
3281
3282 elsif Nkind (Parent (N)) = N_Object_Declaration
3283 and then Is_Limited_Type (Etype (Subp))
3284 then
3285 Targ := Defining_Identifier (Parent (N));
3286
3287 -- New semantics: In an object declaration avoid an extra copy
3288 -- of the result of a call to an inlined function that returns
3289 -- an unconstrained type
3290
6c26bac2 3291 elsif Back_End_Inlining
540d8610
ES
3292 and then Nkind (Parent (N)) = N_Object_Declaration
3293 and then Is_Unc
3294 then
3295 Targ := Defining_Identifier (Parent (N));
3296
3297 else
3298 -- Replace call with temporary and create its declaration
3299
3300 Temp := Make_Temporary (Loc, 'C');
3301 Set_Is_Internal (Temp);
3302
3303 -- For the unconstrained case, the generated temporary has the
3304 -- same constrained declaration as the result variable. It may
3305 -- eventually be possible to remove that temporary and use the
3306 -- result variable directly.
3307
3f80a182 3308 if Is_Unc and then Nkind (Parent (N)) /= N_Assignment_Statement
540d8610
ES
3309 then
3310 Decl :=
3311 Make_Object_Declaration (Loc,
3312 Defining_Identifier => Temp,
3313 Object_Definition =>
3314 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3315
3316 Replace_Formals (Decl);
3317
3318 else
3319 Decl :=
3320 Make_Object_Declaration (Loc,
3321 Defining_Identifier => Temp,
3322 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
3323
3324 Set_Etype (Temp, Ret_Type);
3325 end if;
3326
3327 Set_No_Initialization (Decl);
3328 Append (Decl, Decls);
3329 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3330 Targ := Temp;
3331 end if;
3332 end if;
3333
3334 Insert_Actions (N, Decls);
3335
3336 if Is_Unc_Decl then
3337
3338 -- Special management for inlining a call to a function that returns
3339 -- an unconstrained type and initializes an object declaration: we
3340 -- avoid generating undesired extra calls and goto statements.
3341
3342 -- Given:
66f95f60 3343 -- function Func (...) return String is
540d8610
ES
3344 -- begin
3345 -- declare
3346 -- Result : String (1 .. 4);
3347 -- begin
3348 -- Proc (Result, ...);
3349 -- return Result;
3350 -- end;
66f95f60 3351 -- end Func;
540d8610
ES
3352
3353 -- Result : String := Func (...);
3354
3355 -- Replace this object declaration by:
3356
3357 -- Result : String (1 .. 4);
3358 -- Proc (Result, ...);
3359
3360 Remove_Homonym (Targ);
3361
3362 Decl :=
3363 Make_Object_Declaration
3364 (Loc,
3365 Defining_Identifier => Targ,
3366 Object_Definition =>
3367 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3368 Replace_Formals (Decl);
3369 Rewrite (Parent (N), Decl);
3370 Analyze (Parent (N));
3371
3372 -- Avoid spurious warnings since we know that this declaration is
3373 -- referenced by the procedure call.
3374
3375 Set_Never_Set_In_Source (Targ, False);
3376
3377 -- Remove the local declaration of the extended return stmt from the
3378 -- inlined code
3379
3380 Remove (Parent (Targ1));
3381
3382 -- Update the reference to the result (since we have rewriten the
3383 -- object declaration)
3384
3385 declare
3386 Blk_Call_Stmt : Node_Id;
3387
3388 begin
3389 -- Capture the call to the procedure
3390
3391 Blk_Call_Stmt :=
3392 First (Statements (Handled_Statement_Sequence (Blk)));
3393 pragma Assert
3394 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
3395
3396 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
3397 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
3398 New_Occurrence_Of (Targ, Loc));
3399 end;
3400
3401 -- Remove the return statement
3402
3403 pragma Assert
3404 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3405 N_Simple_Return_Statement);
3406
3407 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3408 end if;
3409
3410 -- Traverse the tree and replace formals with actuals or their thunks.
3411 -- Attach block to tree before analysis and rewriting.
3412
3413 Replace_Formals (Blk);
3414 Set_Parent (Blk, N);
3415
e5c4e2bc
AC
3416 if GNATprove_Mode then
3417 null;
3418
3419 elsif not Comes_From_Source (Subp) or else Is_Predef then
540d8610
ES
3420 Reset_Slocs (Blk);
3421 end if;
3422
3423 if Is_Unc_Decl then
3424
3425 -- No action needed since return statement has been already removed
3426
3427 null;
3428
3429 elsif Present (Exit_Lab) then
3430
fae8eb5b
GD
3431 -- If there's a single return statement at the end of the subprogram,
3432 -- the corresponding goto statement and the corresponding label are
3433 -- useless.
540d8610
ES
3434
3435 if Num_Ret = 1
3436 and then
3437 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3438 N_Goto_Statement
3439 then
3440 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3441 else
3442 Append (Lab_Decl, (Declarations (Blk)));
3443 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
3444 end if;
3445 end if;
3446
3447 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
3448 -- on conflicting private views that Gigi would ignore. If this is a
3449 -- predefined unit, analyze with checks off, as is done in the non-
3450 -- inlined run-time units.
3451
3452 declare
3453 I_Flag : constant Boolean := In_Inlined_Body;
3454
3455 begin
3456 In_Inlined_Body := True;
3457
3458 if Is_Predef then
3459 declare
3460 Style : constant Boolean := Style_Check;
3461
3462 begin
3463 Style_Check := False;
3464
3465 -- Search for dispatching calls that use the Object.Operation
3466 -- notation using an Object that is a parameter of the inlined
3467 -- function. We reset the decoration of Operation to force
3468 -- the reanalysis of the inlined dispatching call because
3469 -- the actual object has been inlined.
3470
3471 Reset_Dispatching_Calls (Blk);
3472
3473 Analyze (Blk, Suppress => All_Checks);
3474 Style_Check := Style;
3475 end;
3476
3477 else
3478 Analyze (Blk);
3479 end if;
3480
3481 In_Inlined_Body := I_Flag;
3482 end;
3483
3484 if Ekind (Subp) = E_Procedure then
3485 Rewrite_Procedure_Call (N, Blk);
3486
3487 else
3488 Rewrite_Function_Call (N, Blk);
3489
3490 if Is_Unc_Decl then
3491 null;
3492
3493 -- For the unconstrained case, the replacement of the call has been
3494 -- made prior to the complete analysis of the generated declarations.
3495 -- Propagate the proper type now.
3496
3497 elsif Is_Unc then
3498 if Nkind (N) = N_Identifier then
3499 Set_Etype (N, Etype (Entity (N)));
3500 else
3501 Set_Etype (N, Etype (Targ1));
3502 end if;
3503 end if;
3504 end if;
3505
3506 Restore_Env;
3507
3508 -- Cleanup mapping between formals and actuals for other expansions
3509
3510 F := First_Formal (Subp);
3511 while Present (F) loop
3512 Set_Renamed_Object (F, Empty);
3513 Next_Formal (F);
3514 end loop;
3515 end Expand_Inlined_Call;
3f80a182 3516
70c34e1c
AC
3517 --------------------------
3518 -- Get_Code_Unit_Entity --
3519 --------------------------
3520
3521 function Get_Code_Unit_Entity (E : Entity_Id) return Entity_Id is
8a49a499 3522 Unit : Entity_Id := Cunit_Entity (Get_Code_Unit (E));
5b5b27ad 3523
70c34e1c 3524 begin
8a49a499
AC
3525 if Ekind (Unit) = E_Package_Body then
3526 Unit := Spec_Entity (Unit);
3527 end if;
5b5b27ad 3528
8a49a499 3529 return Unit;
70c34e1c
AC
3530 end Get_Code_Unit_Entity;
3531
6c26bac2
AC
3532 ------------------------------
3533 -- Has_Excluded_Declaration --
3534 ------------------------------
3535
3536 function Has_Excluded_Declaration
3537 (Subp : Entity_Id;
3538 Decls : List_Id) return Boolean
3539 is
3540 D : Node_Id;
3541
3542 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3543 -- Nested subprograms make a given body ineligible for inlining, but
3544 -- we make an exception for instantiations of unchecked conversion.
3545 -- The body has not been analyzed yet, so check the name, and verify
3546 -- that the visible entity with that name is the predefined unit.
3547
3548 -----------------------------
3549 -- Is_Unchecked_Conversion --
3550 -----------------------------
3551
3552 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3553 Id : constant Node_Id := Name (D);
3554 Conv : Entity_Id;
3555
3556 begin
3557 if Nkind (Id) = N_Identifier
3558 and then Chars (Id) = Name_Unchecked_Conversion
3559 then
3560 Conv := Current_Entity (Id);
3561
3562 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3563 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3564 then
3565 Conv := Current_Entity (Selector_Name (Id));
3566 else
3567 return False;
3568 end if;
3569
3570 return Present (Conv)
8ab31c0c 3571 and then Is_Predefined_Unit (Get_Source_Unit (Conv))
6c26bac2
AC
3572 and then Is_Intrinsic_Subprogram (Conv);
3573 end Is_Unchecked_Conversion;
3574
3575 -- Start of processing for Has_Excluded_Declaration
3576
3577 begin
16b10ccc
AC
3578 -- No action needed if the check is not needed
3579
3580 if not Check_Inlining_Restrictions then
3581 return False;
3582 end if;
3583
6c26bac2
AC
3584 D := First (Decls);
3585 while Present (D) loop
3c756b76 3586
6fd52b78
AC
3587 -- First declarations universally excluded
3588
3589 if Nkind (D) = N_Package_Declaration then
6c26bac2 3590 Cannot_Inline
ca7e6c26 3591 ("cannot inline & (nested package declaration)?", D, Subp);
6fd52b78
AC
3592 return True;
3593
3594 elsif Nkind (D) = N_Package_Instantiation then
3595 Cannot_Inline
ca7e6c26 3596 ("cannot inline & (nested package instantiation)?", D, Subp);
6c26bac2 3597 return True;
6fd52b78
AC
3598 end if;
3599
66f95f60 3600 -- Then declarations excluded only for front-end inlining
6fd52b78
AC
3601
3602 if Back_End_Inlining then
3603 null;
6c26bac2
AC
3604
3605 elsif Nkind (D) = N_Task_Type_Declaration
3606 or else Nkind (D) = N_Single_Task_Declaration
3607 then
3608 Cannot_Inline
ca7e6c26 3609 ("cannot inline & (nested task type declaration)?", D, Subp);
6c26bac2
AC
3610 return True;
3611
3612 elsif Nkind (D) = N_Protected_Type_Declaration
3613 or else Nkind (D) = N_Single_Protected_Declaration
3614 then
3615 Cannot_Inline
3616 ("cannot inline & (nested protected type declaration)?",
3617 D, Subp);
3618 return True;
3619
6fd52b78 3620 elsif Nkind (D) = N_Subprogram_Body then
6c26bac2 3621 Cannot_Inline
ca7e6c26 3622 ("cannot inline & (nested subprogram)?", D, Subp);
6c26bac2
AC
3623 return True;
3624
3625 elsif Nkind (D) = N_Function_Instantiation
3626 and then not Is_Unchecked_Conversion (D)
3627 then
3628 Cannot_Inline
ca7e6c26 3629 ("cannot inline & (nested function instantiation)?", D, Subp);
6c26bac2
AC
3630 return True;
3631
3632 elsif Nkind (D) = N_Procedure_Instantiation then
3633 Cannot_Inline
ca7e6c26 3634 ("cannot inline & (nested procedure instantiation)?", D, Subp);
6c26bac2 3635 return True;
f99ff327
AC
3636
3637 -- Subtype declarations with predicates will generate predicate
3638 -- functions, i.e. nested subprogram bodies, so inlining is not
3639 -- possible.
3640
3641 elsif Nkind (D) = N_Subtype_Declaration
3642 and then Present (Aspect_Specifications (D))
3643 then
3644 declare
3645 A : Node_Id;
3646 A_Id : Aspect_Id;
3647
3648 begin
3649 A := First (Aspect_Specifications (D));
3650 while Present (A) loop
3651 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3652
3653 if A_Id = Aspect_Predicate
3654 or else A_Id = Aspect_Static_Predicate
3655 or else A_Id = Aspect_Dynamic_Predicate
3656 then
3657 Cannot_Inline
ca7e6c26
AC
3658 ("cannot inline & (subtype declaration with "
3659 & "predicate)?", D, Subp);
f99ff327
AC
3660 return True;
3661 end if;
3662
3663 Next (A);
3664 end loop;
3665 end;
6c26bac2
AC
3666 end if;
3667
3668 Next (D);
3669 end loop;
3670
3671 return False;
3672 end Has_Excluded_Declaration;
3673
3674 ----------------------------
3675 -- Has_Excluded_Statement --
3676 ----------------------------
3677
3678 function Has_Excluded_Statement
3679 (Subp : Entity_Id;
3680 Stats : List_Id) return Boolean
3681 is
3682 S : Node_Id;
3683 E : Node_Id;
3684
3685 begin
16b10ccc
AC
3686 -- No action needed if the check is not needed
3687
3688 if not Check_Inlining_Restrictions then
3689 return False;
3690 end if;
3691
6c26bac2
AC
3692 S := First (Stats);
3693 while Present (S) loop
3694 if Nkind_In (S, N_Abort_Statement,
3695 N_Asynchronous_Select,
3696 N_Conditional_Entry_Call,
3697 N_Delay_Relative_Statement,
3698 N_Delay_Until_Statement,
3699 N_Selective_Accept,
3700 N_Timed_Entry_Call)
3701 then
3702 Cannot_Inline
3703 ("cannot inline & (non-allowed statement)?", S, Subp);
3704 return True;
3705
3706 elsif Nkind (S) = N_Block_Statement then
3707 if Present (Declarations (S))
3708 and then Has_Excluded_Declaration (Subp, Declarations (S))
3709 then
3710 return True;
3711
3712 elsif Present (Handled_Statement_Sequence (S)) then
16b10ccc
AC
3713 if not Back_End_Inlining
3714 and then
3715 Present
3716 (Exception_Handlers (Handled_Statement_Sequence (S)))
6c26bac2
AC
3717 then
3718 Cannot_Inline
3719 ("cannot inline& (exception handler)?",
3720 First (Exception_Handlers
3721 (Handled_Statement_Sequence (S))),
3722 Subp);
3723 return True;
3724
3725 elsif Has_Excluded_Statement
3726 (Subp, Statements (Handled_Statement_Sequence (S)))
3727 then
3728 return True;
3729 end if;
3730 end if;
3731
3732 elsif Nkind (S) = N_Case_Statement then
3733 E := First (Alternatives (S));
3734 while Present (E) loop
3735 if Has_Excluded_Statement (Subp, Statements (E)) then
3736 return True;
3737 end if;
3738
3739 Next (E);
3740 end loop;
3741
3742 elsif Nkind (S) = N_If_Statement then
3743 if Has_Excluded_Statement (Subp, Then_Statements (S)) then
3744 return True;
3745 end if;
3746
3747 if Present (Elsif_Parts (S)) then
3748 E := First (Elsif_Parts (S));
3749 while Present (E) loop
3750 if Has_Excluded_Statement (Subp, Then_Statements (E)) then
3751 return True;
3752 end if;
3753
3754 Next (E);
3755 end loop;
3756 end if;
3757
3758 if Present (Else_Statements (S))
3759 and then Has_Excluded_Statement (Subp, Else_Statements (S))
3760 then
3761 return True;
3762 end if;
3763
3764 elsif Nkind (S) = N_Loop_Statement
3765 and then Has_Excluded_Statement (Subp, Statements (S))
3766 then
3767 return True;
3768
3769 elsif Nkind (S) = N_Extended_Return_Statement then
3770 if Present (Handled_Statement_Sequence (S))
3771 and then
3772 Has_Excluded_Statement
3773 (Subp, Statements (Handled_Statement_Sequence (S)))
3774 then
3775 return True;
3776
16b10ccc
AC
3777 elsif not Back_End_Inlining
3778 and then Present (Handled_Statement_Sequence (S))
6c26bac2
AC
3779 and then
3780 Present (Exception_Handlers
3781 (Handled_Statement_Sequence (S)))
3782 then
3783 Cannot_Inline
3784 ("cannot inline& (exception handler)?",
3785 First (Exception_Handlers (Handled_Statement_Sequence (S))),
3786 Subp);
3787 return True;
3788 end if;
3789 end if;
3790
3791 Next (S);
3792 end loop;
3793
3794 return False;
3795 end Has_Excluded_Statement;
3796
38cbfe40
RK
3797 --------------------------
3798 -- Has_Initialized_Type --
3799 --------------------------
3800
3801 function Has_Initialized_Type (E : Entity_Id) return Boolean is
90a4b336 3802 E_Body : constant Node_Id := Subprogram_Body (E);
38cbfe40
RK
3803 Decl : Node_Id;
3804
3805 begin
3806 if No (E_Body) then -- imported subprogram
3807 return False;
3808
3809 else
3810 Decl := First (Declarations (E_Body));
38cbfe40 3811 while Present (Decl) loop
38cbfe40
RK
3812 if Nkind (Decl) = N_Full_Type_Declaration
3813 and then Present (Init_Proc (Defining_Identifier (Decl)))
3814 then
3815 return True;
3816 end if;
3817
3818 Next (Decl);
3819 end loop;
3820 end if;
3821
3822 return False;
3823 end Has_Initialized_Type;
3824
ea0c8cfb
RD
3825 -----------------------
3826 -- Has_Single_Return --
3827 -----------------------
6c26bac2
AC
3828
3829 function Has_Single_Return (N : Node_Id) return Boolean is
3830 Return_Statement : Node_Id := Empty;
3831
3832 function Check_Return (N : Node_Id) return Traverse_Result;
3833
3834 ------------------
3835 -- Check_Return --
3836 ------------------
3837
3838 function Check_Return (N : Node_Id) return Traverse_Result is
3839 begin
3840 if Nkind (N) = N_Simple_Return_Statement then
3841 if Present (Expression (N))
3842 and then Is_Entity_Name (Expression (N))
3843 then
3844 if No (Return_Statement) then
3845 Return_Statement := N;
3846 return OK;
3847
3848 elsif Chars (Expression (N)) =
3849 Chars (Expression (Return_Statement))
3850 then
3851 return OK;
3852
3853 else
3854 return Abandon;
3855 end if;
3856
3857 -- A return statement within an extended return is a noop
3858 -- after inlining.
3859
3860 elsif No (Expression (N))
3861 and then
3862 Nkind (Parent (Parent (N))) = N_Extended_Return_Statement
3863 then
3864 return OK;
3865
3866 else
3867 -- Expression has wrong form
3868
3869 return Abandon;
3870 end if;
3871
ea0c8cfb
RD
3872 -- We can only inline a build-in-place function if it has a single
3873 -- extended return.
6c26bac2
AC
3874
3875 elsif Nkind (N) = N_Extended_Return_Statement then
3876 if No (Return_Statement) then
3877 Return_Statement := N;
3878 return OK;
3879
3880 else
3881 return Abandon;
3882 end if;
3883
3884 else
3885 return OK;
3886 end if;
3887 end Check_Return;
3888
3889 function Check_All_Returns is new Traverse_Func (Check_Return);
3890
3891 -- Start of processing for Has_Single_Return
3892
3893 begin
3894 if Check_All_Returns (N) /= OK then
3895 return False;
3896
3897 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3898 return True;
3899
3900 else
3901 return Present (Declarations (N))
3902 and then Present (First (Declarations (N)))
3903 and then Chars (Expression (Return_Statement)) =
3904 Chars (Defining_Identifier (First (Declarations (N))));
3905 end if;
3906 end Has_Single_Return;
3907
5b5b27ad
AC
3908 -----------------------------
3909 -- In_Main_Unit_Or_Subunit --
3910 -----------------------------
3911
3912 function In_Main_Unit_Or_Subunit (E : Entity_Id) return Boolean is
3913 Comp : Node_Id := Cunit (Get_Code_Unit (E));
3914
3915 begin
3916 -- Check whether the subprogram or package to inline is within the main
3917 -- unit or its spec or within a subunit. In either case there are no
3918 -- additional bodies to process. If the subprogram appears in a parent
3919 -- of the current unit, the check on whether inlining is possible is
3920 -- done in Analyze_Inlined_Bodies.
3921
3922 while Nkind (Unit (Comp)) = N_Subunit loop
3923 Comp := Library_Unit (Comp);
3924 end loop;
3925
3926 return Comp = Cunit (Main_Unit)
3927 or else Comp = Library_Unit (Cunit (Main_Unit));
3928 end In_Main_Unit_Or_Subunit;
3929
38cbfe40
RK
3930 ----------------
3931 -- Initialize --
3932 ----------------
3933
3934 procedure Initialize is
3935 begin
38cbfe40
RK
3936 Pending_Descriptor.Init;
3937 Pending_Instantiations.Init;
3938 Inlined_Bodies.Init;
3939 Successors.Init;
3940 Inlined.Init;
3941
3942 for J in Hash_Headers'Range loop
3943 Hash_Headers (J) := No_Subp;
3944 end loop;
16b10ccc
AC
3945
3946 Inlined_Calls := No_Elist;
3947 Backend_Calls := No_Elist;
3948 Backend_Inlined_Subps := No_Elist;
3949 Backend_Not_Inlined_Subps := No_Elist;
38cbfe40
RK
3950 end Initialize;
3951
3952 ------------------------
3953 -- Instantiate_Bodies --
3954 ------------------------
3955
3956 -- Generic bodies contain all the non-local references, so an
3957 -- instantiation does not need any more context than Standard
3958 -- itself, even if the instantiation appears in an inner scope.
3959 -- Generic associations have verified that the contract model is
3960 -- satisfied, so that any error that may occur in the analysis of
3961 -- the body is an internal error.
3962
3963 procedure Instantiate_Bodies is
3bb91f98 3964 J : Nat;
38cbfe40
RK
3965 Info : Pending_Body_Info;
3966
3967 begin
07fc65c4 3968 if Serious_Errors_Detected = 0 then
fbf5a39b 3969 Expander_Active := (Operating_Mode = Opt.Generate_Code);
a99ada67 3970 Push_Scope (Standard_Standard);
38cbfe40
RK
3971 To_Clean := New_Elmt_List;
3972
3973 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
3974 Start_Generic;
3975 end if;
3976
3977 -- A body instantiation may generate additional instantiations, so
3978 -- the following loop must scan to the end of a possibly expanding
3979 -- set (that's why we can't simply use a FOR loop here).
3980
3981 J := 0;
38cbfe40 3982 while J <= Pending_Instantiations.Last
07fc65c4 3983 and then Serious_Errors_Detected = 0
38cbfe40 3984 loop
38cbfe40
RK
3985 Info := Pending_Instantiations.Table (J);
3986
fbf5a39b 3987 -- If the instantiation node is absent, it has been removed
38cbfe40
RK
3988 -- as part of unreachable code.
3989
3990 if No (Info.Inst_Node) then
3991 null;
3992
fbf5a39b 3993 elsif Nkind (Info.Act_Decl) = N_Package_Declaration then
38cbfe40
RK
3994 Instantiate_Package_Body (Info);
3995 Add_Scope_To_Clean (Defining_Entity (Info.Act_Decl));
3996
3997 else
3998 Instantiate_Subprogram_Body (Info);
3999 end if;
4000
4001 J := J + 1;
4002 end loop;
4003
4004 -- Reset the table of instantiations. Additional instantiations
4005 -- may be added through inlining, when additional bodies are
4006 -- analyzed.
4007
4008 Pending_Instantiations.Init;
4009
4010 -- We can now complete the cleanup actions of scopes that contain
4011 -- pending instantiations (skipped for generic units, since we
4012 -- never need any cleanups in generic units).
38cbfe40
RK
4013
4014 if Expander_Active
4015 and then not Is_Generic_Unit (Main_Unit_Entity)
4016 then
4017 Cleanup_Scopes;
38cbfe40
RK
4018 elsif Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4019 End_Generic;
4020 end if;
4021
4022 Pop_Scope;
4023 end if;
4024 end Instantiate_Bodies;
4025
4026 ---------------
4027 -- Is_Nested --
4028 ---------------
4029
4030 function Is_Nested (E : Entity_Id) return Boolean is
5132708f 4031 Scop : Entity_Id;
38cbfe40
RK
4032
4033 begin
5132708f 4034 Scop := Scope (E);
38cbfe40
RK
4035 while Scop /= Standard_Standard loop
4036 if Ekind (Scop) in Subprogram_Kind then
4037 return True;
4038
4039 elsif Ekind (Scop) = E_Task_Type
4040 or else Ekind (Scop) = E_Entry
0b7f0f0e
AC
4041 or else Ekind (Scop) = E_Entry_Family
4042 then
38cbfe40
RK
4043 return True;
4044 end if;
4045
4046 Scop := Scope (Scop);
4047 end loop;
4048
4049 return False;
4050 end Is_Nested;
4051
16b10ccc
AC
4052 ------------------------
4053 -- List_Inlining_Info --
4054 ------------------------
4055
4056 procedure List_Inlining_Info is
4057 Elmt : Elmt_Id;
4058 Nod : Node_Id;
4059 Count : Nat;
4060
4061 begin
4062 if not Debug_Flag_Dot_J then
4063 return;
4064 end if;
4065
4066 -- Generate listing of calls inlined by the frontend
4067
4068 if Present (Inlined_Calls) then
4069 Count := 0;
4070 Elmt := First_Elmt (Inlined_Calls);
4071 while Present (Elmt) loop
4072 Nod := Node (Elmt);
4073
4074 if In_Extended_Main_Code_Unit (Nod) then
4075 Count := Count + 1;
4076
4077 if Count = 1 then
1725676d 4078 Write_Str ("List of calls inlined by the frontend");
16b10ccc
AC
4079 Write_Eol;
4080 end if;
4081
4082 Write_Str (" ");
4083 Write_Int (Count);
4084 Write_Str (":");
4085 Write_Location (Sloc (Nod));
4086 Write_Str (":");
4087 Output.Write_Eol;
4088 end if;
4089
4090 Next_Elmt (Elmt);
4091 end loop;
4092 end if;
4093
4094 -- Generate listing of calls passed to the backend
4095
4096 if Present (Backend_Calls) then
4097 Count := 0;
4098
4099 Elmt := First_Elmt (Backend_Calls);
4100 while Present (Elmt) loop
4101 Nod := Node (Elmt);
4102
4103 if In_Extended_Main_Code_Unit (Nod) then
4104 Count := Count + 1;
4105
4106 if Count = 1 then
1725676d 4107 Write_Str ("List of inlined calls passed to the backend");
16b10ccc
AC
4108 Write_Eol;
4109 end if;
4110
4111 Write_Str (" ");
4112 Write_Int (Count);
4113 Write_Str (":");
4114 Write_Location (Sloc (Nod));
4115 Output.Write_Eol;
4116 end if;
4117
4118 Next_Elmt (Elmt);
4119 end loop;
4120 end if;
4121
4122 -- Generate listing of subprograms passed to the backend
4123
62a64085 4124 if Present (Backend_Inlined_Subps) and then Back_End_Inlining then
16b10ccc
AC
4125 Count := 0;
4126
4127 Elmt := First_Elmt (Backend_Inlined_Subps);
4128 while Present (Elmt) loop
4129 Nod := Node (Elmt);
4130
4131 Count := Count + 1;
4132
4133 if Count = 1 then
4134 Write_Str
1725676d 4135 ("List of inlined subprograms passed to the backend");
16b10ccc
AC
4136 Write_Eol;
4137 end if;
4138
4139 Write_Str (" ");
4140 Write_Int (Count);
4141 Write_Str (":");
4142 Write_Name (Chars (Nod));
4143 Write_Str (" (");
4144 Write_Location (Sloc (Nod));
4145 Write_Str (")");
4146 Output.Write_Eol;
4147
4148 Next_Elmt (Elmt);
4149 end loop;
4150 end if;
4151
1725676d 4152 -- Generate listing of subprograms that cannot be inlined by the backend
16b10ccc 4153
62a64085 4154 if Present (Backend_Not_Inlined_Subps) and then Back_End_Inlining then
16b10ccc
AC
4155 Count := 0;
4156
4157 Elmt := First_Elmt (Backend_Not_Inlined_Subps);
4158 while Present (Elmt) loop
4159 Nod := Node (Elmt);
4160
4161 Count := Count + 1;
4162
4163 if Count = 1 then
4164 Write_Str
1725676d 4165 ("List of subprograms that cannot be inlined by the backend");
16b10ccc
AC
4166 Write_Eol;
4167 end if;
4168
4169 Write_Str (" ");
4170 Write_Int (Count);
4171 Write_Str (":");
4172 Write_Name (Chars (Nod));
4173 Write_Str (" (");
4174 Write_Location (Sloc (Nod));
4175 Write_Str (")");
4176 Output.Write_Eol;
4177
4178 Next_Elmt (Elmt);
4179 end loop;
4180 end if;
4181 end List_Inlining_Info;
4182
38cbfe40
RK
4183 ----------
4184 -- Lock --
4185 ----------
4186
4187 procedure Lock is
4188 begin
38cbfe40 4189 Pending_Instantiations.Release;
de33eb38 4190 Pending_Instantiations.Locked := True;
38cbfe40 4191 Inlined_Bodies.Release;
de33eb38 4192 Inlined_Bodies.Locked := True;
38cbfe40 4193 Successors.Release;
de33eb38 4194 Successors.Locked := True;
38cbfe40 4195 Inlined.Release;
de33eb38 4196 Inlined.Locked := True;
38cbfe40
RK
4197 end Lock;
4198
697b781a
AC
4199 --------------------------------
4200 -- Remove_Aspects_And_Pragmas --
4201 --------------------------------
16b10ccc 4202
697b781a
AC
4203 procedure Remove_Aspects_And_Pragmas (Body_Decl : Node_Id) is
4204 procedure Remove_Items (List : List_Id);
4205 -- Remove all useless aspects/pragmas from a particular list
16b10ccc 4206
697b781a
AC
4207 ------------------
4208 -- Remove_Items --
4209 ------------------
16b10ccc 4210
697b781a
AC
4211 procedure Remove_Items (List : List_Id) is
4212 Item : Node_Id;
4213 Item_Id : Node_Id;
4214 Next_Item : Node_Id;
4215
4216 begin
4217 -- Traverse the list looking for an aspect specification or a pragma
4218
4219 Item := First (List);
4220 while Present (Item) loop
4221 Next_Item := Next (Item);
4222
4223 if Nkind (Item) = N_Aspect_Specification then
4224 Item_Id := Identifier (Item);
4225 elsif Nkind (Item) = N_Pragma then
4226 Item_Id := Pragma_Identifier (Item);
4227 else
4228 Item_Id := Empty;
4229 end if;
4230
4231 if Present (Item_Id)
4232 and then Nam_In (Chars (Item_Id), Name_Contract_Cases,
4233 Name_Global,
4234 Name_Depends,
16b10ccc 4235 Name_Postcondition,
697b781a
AC
4236 Name_Precondition,
4237 Name_Refined_Global,
4238 Name_Refined_Depends,
4239 Name_Refined_Post,
4240 Name_Test_Case,
4241 Name_Unmodified,
da9683f4
AC
4242 Name_Unreferenced,
4243 Name_Unused)
697b781a
AC
4244 then
4245 Remove (Item);
4246 end if;
16b10ccc 4247
697b781a
AC
4248 Item := Next_Item;
4249 end loop;
4250 end Remove_Items;
4251
4252 -- Start of processing for Remove_Aspects_And_Pragmas
4253
4254 begin
4255 Remove_Items (Aspect_Specifications (Body_Decl));
4256 Remove_Items (Declarations (Body_Decl));
da9683f4 4257
fae8eb5b 4258 -- Pragmas Unmodified, Unreferenced, and Unused may additionally appear
da9683f4
AC
4259 -- in the body of the subprogram.
4260
4261 Remove_Items (Statements (Handled_Statement_Sequence (Body_Decl)));
697b781a 4262 end Remove_Aspects_And_Pragmas;
16b10ccc 4263
eefd2467
AC
4264 --------------------------
4265 -- Remove_Dead_Instance --
4266 --------------------------
4267
4268 procedure Remove_Dead_Instance (N : Node_Id) is
4269 J : Int;
4270
4271 begin
4272 J := 0;
4273 while J <= Pending_Instantiations.Last loop
4274 if Pending_Instantiations.Table (J).Inst_Node = N then
4275 Pending_Instantiations.Table (J).Inst_Node := Empty;
4276 return;
4277 end if;
4278
4279 J := J + 1;
4280 end loop;
4281 end Remove_Dead_Instance;
4282
38cbfe40 4283end Inline;