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