[Ada] Unnesting: handle conditional expressions
[gcc.git] / gcc / ada / exp_unst.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ U N S T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2014-2019, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Lib; use Lib;
31 with Namet; use Namet;
32 with Nlists; use Nlists;
33 with Nmake; use Nmake;
34 with Opt;
35 with Output; use Output;
36 with Rtsfind; use Rtsfind;
37 with Sem; use Sem;
38 with Sem_Aux; use Sem_Aux;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Mech; use Sem_Mech;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Sinput; use Sinput;
45 with Snames; use Snames;
46 with Stand; use Stand;
47 with Tbuild; use Tbuild;
48 with Uintp; use Uintp;
49
50 package body Exp_Unst is
51
52 -----------------------
53 -- Local Subprograms --
54 -----------------------
55
56 procedure Unnest_Subprogram (Subp : Entity_Id; Subp_Body : Node_Id);
57 -- Subp is a library-level subprogram which has nested subprograms, and
58 -- Subp_Body is the corresponding N_Subprogram_Body node. This procedure
59 -- declares the AREC types and objects, adds assignments to the AREC record
60 -- as required, defines the xxxPTR types for uplevel referenced objects,
61 -- adds the ARECP parameter to all nested subprograms which need it, and
62 -- modifies all uplevel references appropriately.
63
64 -----------
65 -- Calls --
66 -----------
67
68 -- Table to record calls within the nest being analyzed. These are the
69 -- calls which may need to have an AREC actual added. This table is built
70 -- new for each subprogram nest and cleared at the end of processing each
71 -- subprogram nest.
72
73 type Call_Entry is record
74 N : Node_Id;
75 -- The actual call
76
77 Caller : Entity_Id;
78 -- Entity of the subprogram containing the call (can be at any level)
79
80 Callee : Entity_Id;
81 -- Entity of the subprogram called (always at level 2 or higher). Note
82 -- that in accordance with the basic rules of nesting, the level of To
83 -- is either less than or equal to the level of From, or one greater.
84 end record;
85
86 package Calls is new Table.Table (
87 Table_Component_Type => Call_Entry,
88 Table_Index_Type => Nat,
89 Table_Low_Bound => 1,
90 Table_Initial => 100,
91 Table_Increment => 200,
92 Table_Name => "Unnest_Calls");
93 -- Records each call within the outer subprogram and all nested subprograms
94 -- that are to other subprograms nested within the outer subprogram. These
95 -- are the calls that may need an additional parameter.
96
97 procedure Append_Unique_Call (Call : Call_Entry);
98 -- Append a call entry to the Calls table. A check is made to see if the
99 -- table already contains this entry and if so it has no effect.
100
101 ----------------------------------
102 -- Subprograms For Fat Pointers --
103 ----------------------------------
104
105 function Build_Access_Type_Decl
106 (E : Entity_Id;
107 Scop : Entity_Id) return Node_Id;
108 -- For an uplevel reference that involves an unconstrained array type,
109 -- build an access type declaration for the corresponding activation
110 -- record component. The relevant attributes of the access type are
111 -- set here to avoid a full analysis that would require a scope stack.
112
113 function Needs_Fat_Pointer (E : Entity_Id) return Boolean;
114 -- A formal parameter of an unconstrained array type that appears in an
115 -- uplevel reference requires the construction of an access type, to be
116 -- used in the corresponding component declaration.
117
118 -----------
119 -- Urefs --
120 -----------
121
122 -- Table to record explicit uplevel references to objects (variables,
123 -- constants, formal parameters). These are the references that will
124 -- need rewriting to use the activation table (AREC) pointers. Also
125 -- included are implicit and explicit uplevel references to types, but
126 -- these do not get rewritten by the front end. This table is built new
127 -- for each subprogram nest and cleared at the end of processing each
128 -- subprogram nest.
129
130 type Uref_Entry is record
131 Ref : Node_Id;
132 -- The reference itself. For objects this is always an entity reference
133 -- and the referenced entity will have its Is_Uplevel_Referenced_Entity
134 -- flag set and will appear in the Uplevel_Referenced_Entities list of
135 -- the subprogram declaring this entity.
136
137 Ent : Entity_Id;
138 -- The Entity_Id of the uplevel referenced object or type
139
140 Caller : Entity_Id;
141 -- The entity for the subprogram immediately containing this entity
142
143 Callee : Entity_Id;
144 -- The entity for the subprogram containing the referenced entity. Note
145 -- that the level of Callee must be less than the level of Caller, since
146 -- this is an uplevel reference.
147 end record;
148
149 package Urefs is new Table.Table (
150 Table_Component_Type => Uref_Entry,
151 Table_Index_Type => Nat,
152 Table_Low_Bound => 1,
153 Table_Initial => 100,
154 Table_Increment => 200,
155 Table_Name => "Unnest_Urefs");
156
157 ------------------------
158 -- Append_Unique_Call --
159 ------------------------
160
161 procedure Append_Unique_Call (Call : Call_Entry) is
162 begin
163 for J in Calls.First .. Calls.Last loop
164 if Calls.Table (J) = Call then
165 return;
166 end if;
167 end loop;
168
169 Calls.Append (Call);
170 end Append_Unique_Call;
171
172 -----------------------------
173 -- Build_Access_Type_Decl --
174 -----------------------------
175
176 function Build_Access_Type_Decl
177 (E : Entity_Id;
178 Scop : Entity_Id) return Node_Id
179 is
180 Loc : constant Source_Ptr := Sloc (E);
181 Typ : Entity_Id;
182
183 begin
184 Typ := Make_Temporary (Loc, 'S');
185 Set_Ekind (Typ, E_General_Access_Type);
186 Set_Etype (Typ, Typ);
187 Set_Scope (Typ, Scop);
188 Set_Directly_Designated_Type (Typ, Etype (E));
189
190 return
191 Make_Full_Type_Declaration (Loc,
192 Defining_Identifier => Typ,
193 Type_Definition =>
194 Make_Access_To_Object_Definition (Loc,
195 Subtype_Indication => New_Occurrence_Of (Etype (E), Loc)));
196 end Build_Access_Type_Decl;
197
198 ---------------
199 -- Get_Level --
200 ---------------
201
202 function Get_Level (Subp : Entity_Id; Sub : Entity_Id) return Nat is
203 Lev : Nat;
204 S : Entity_Id;
205
206 begin
207 Lev := 1;
208 S := Sub;
209 loop
210 if S = Subp then
211 return Lev;
212 else
213 Lev := Lev + 1;
214 S := Enclosing_Subprogram (S);
215 end if;
216 end loop;
217 end Get_Level;
218
219 --------------------------
220 -- In_Synchronized_Unit --
221 --------------------------
222
223 function In_Synchronized_Unit (Subp : Entity_Id) return Boolean is
224 S : Entity_Id := Scope (Subp);
225
226 begin
227 while Present (S) and then S /= Standard_Standard loop
228 if Is_Concurrent_Type (S) then
229 return True;
230
231 elsif Is_Private_Type (S)
232 and then Present (Full_View (S))
233 and then Is_Concurrent_Type (Full_View (S))
234 then
235 return True;
236 end if;
237
238 S := Scope (S);
239 end loop;
240
241 return False;
242 end In_Synchronized_Unit;
243
244 -----------------------
245 -- Needs_Fat_Pointer --
246 -----------------------
247
248 function Needs_Fat_Pointer (E : Entity_Id) return Boolean is
249 Typ : Entity_Id;
250 begin
251 if Is_Formal (E) then
252 Typ := Etype (E);
253 if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
254 Typ := Full_View (Typ);
255 end if;
256
257 return Is_Array_Type (Typ) and then not Is_Constrained (Typ);
258 else
259 return False;
260 end if;
261 end Needs_Fat_Pointer;
262
263 ----------------
264 -- Subp_Index --
265 ----------------
266
267 function Subp_Index (Sub : Entity_Id) return SI_Type is
268 E : Entity_Id := Sub;
269
270 begin
271 pragma Assert (Is_Subprogram (E));
272
273 if Subps_Index (E) = Uint_0 then
274 E := Ultimate_Alias (E);
275
276 -- The body of a protected operation has a different name and
277 -- has been scanned at this point, and thus has an entry in the
278 -- subprogram table.
279
280 if E = Sub and then Convention (E) = Convention_Protected then
281 E := Protected_Body_Subprogram (E);
282 end if;
283
284 if Ekind (E) = E_Function
285 and then Rewritten_For_C (E)
286 and then Present (Corresponding_Procedure (E))
287 then
288 E := Corresponding_Procedure (E);
289 end if;
290 end if;
291
292 pragma Assert (Subps_Index (E) /= Uint_0);
293 return SI_Type (UI_To_Int (Subps_Index (E)));
294 end Subp_Index;
295
296 -----------------------
297 -- Unnest_Subprogram --
298 -----------------------
299
300 procedure Unnest_Subprogram (Subp : Entity_Id; Subp_Body : Node_Id) is
301 function AREC_Name (J : Pos; S : String) return Name_Id;
302 -- Returns name for string ARECjS, where j is the decimal value of j
303
304 function Enclosing_Subp (Subp : SI_Type) return SI_Type;
305 -- Subp is the index of a subprogram which has a Lev greater than 1.
306 -- This function returns the index of the enclosing subprogram which
307 -- will have a Lev value one less than this.
308
309 function Img_Pos (N : Pos) return String;
310 -- Return image of N without leading blank
311
312 function Upref_Name
313 (Ent : Entity_Id;
314 Index : Pos;
315 Clist : List_Id) return Name_Id;
316 -- This function returns the name to be used in the activation record to
317 -- reference the variable uplevel. Clist is the list of components that
318 -- have been created in the activation record so far. Normally the name
319 -- is just a copy of the Chars field of the entity. The exception is
320 -- when the name has already been used, in which case we suffix the name
321 -- with the index value Index to avoid duplication. This happens with
322 -- declare blocks and generic parameters at least.
323
324 ---------------
325 -- AREC_Name --
326 ---------------
327
328 function AREC_Name (J : Pos; S : String) return Name_Id is
329 begin
330 return Name_Find ("AREC" & Img_Pos (J) & S);
331 end AREC_Name;
332
333 --------------------
334 -- Enclosing_Subp --
335 --------------------
336
337 function Enclosing_Subp (Subp : SI_Type) return SI_Type is
338 STJ : Subp_Entry renames Subps.Table (Subp);
339 Ret : constant SI_Type := Subp_Index (Enclosing_Subprogram (STJ.Ent));
340 begin
341 pragma Assert (STJ.Lev > 1);
342 pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1);
343 return Ret;
344 end Enclosing_Subp;
345
346 -------------
347 -- Img_Pos --
348 -------------
349
350 function Img_Pos (N : Pos) return String is
351 Buf : String (1 .. 20);
352 Ptr : Natural;
353 NV : Nat;
354
355 begin
356 Ptr := Buf'Last;
357 NV := N;
358 while NV /= 0 loop
359 Buf (Ptr) := Character'Val (48 + NV mod 10);
360 Ptr := Ptr - 1;
361 NV := NV / 10;
362 end loop;
363
364 return Buf (Ptr + 1 .. Buf'Last);
365 end Img_Pos;
366
367 ----------------
368 -- Upref_Name --
369 ----------------
370
371 function Upref_Name
372 (Ent : Entity_Id;
373 Index : Pos;
374 Clist : List_Id) return Name_Id
375 is
376 C : Node_Id;
377 begin
378 C := First (Clist);
379 loop
380 if No (C) then
381 return Chars (Ent);
382
383 elsif Chars (Defining_Identifier (C)) = Chars (Ent) then
384 return
385 Name_Find (Get_Name_String (Chars (Ent)) & Img_Pos (Index));
386 else
387 Next (C);
388 end if;
389 end loop;
390 end Upref_Name;
391
392 -- Start of processing for Unnest_Subprogram
393
394 begin
395 -- Nothing to do inside a generic (all processing is for instance)
396
397 if Inside_A_Generic then
398 return;
399 end if;
400
401 -- If the main unit is a package body then we need to examine the spec
402 -- to determine whether the main unit is generic (the scope stack is not
403 -- present when this is called on the main unit).
404
405 if Ekind (Cunit_Entity (Main_Unit)) = E_Package_Body
406 and then Is_Generic_Unit (Spec_Entity (Cunit_Entity (Main_Unit)))
407 then
408 return;
409 end if;
410
411 -- Only unnest when generating code for the main source unit
412
413 if not In_Extended_Main_Code_Unit (Subp_Body) then
414 return;
415 end if;
416
417 -- This routine is called late, after the scope stack is gone. The
418 -- following creates a suitable dummy scope stack to be used for the
419 -- analyze/expand calls made from this routine.
420
421 Push_Scope (Subp);
422
423 -- First step, we must mark all nested subprograms that require a static
424 -- link (activation record) because either they contain explicit uplevel
425 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
426 -- this point), or they make calls to other subprograms in the same nest
427 -- that require a static link (in which case we set this flag).
428
429 -- This is a recursive definition, and to implement this, we have to
430 -- build a call graph for the set of nested subprograms, and then go
431 -- over this graph to implement recursively the invariant that if a
432 -- subprogram has a call to a subprogram requiring a static link, then
433 -- the calling subprogram requires a static link.
434
435 -- First populate the above tables
436
437 Subps_First := Subps.Last + 1;
438 Calls.Init;
439 Urefs.Init;
440
441 Build_Tables : declare
442 Current_Subprogram : Entity_Id := Empty;
443 -- When we scan a subprogram body, we set Current_Subprogram to the
444 -- corresponding entity. This gets recursively saved and restored.
445
446 function Visit_Node (N : Node_Id) return Traverse_Result;
447 -- Visit a single node in Subp
448
449 -----------
450 -- Visit --
451 -----------
452
453 procedure Visit is new Traverse_Proc (Visit_Node);
454 -- Used to traverse the body of Subp, populating the tables
455
456 ----------------
457 -- Visit_Node --
458 ----------------
459
460 function Visit_Node (N : Node_Id) return Traverse_Result is
461 Ent : Entity_Id;
462 Caller : Entity_Id;
463 Callee : Entity_Id;
464
465 procedure Check_Static_Type
466 (T : Entity_Id;
467 N : Node_Id;
468 DT : in out Boolean;
469 Check_Designated : Boolean := False);
470 -- Given a type T, checks if it is a static type defined as a type
471 -- with no dynamic bounds in sight. If so, the only action is to
472 -- set Is_Static_Type True for T. If T is not a static type, then
473 -- all types with dynamic bounds associated with T are detected,
474 -- and their bounds are marked as uplevel referenced if not at the
475 -- library level, and DT is set True. If N is specified, it's the
476 -- node that will need to be replaced. If not specified, it means
477 -- we can't do a replacement because the bound is implicit.
478
479 -- If Check_Designated is True and T or its full view is an access
480 -- type, check whether the designated type has dynamic bounds.
481
482 procedure Note_Uplevel_Ref
483 (E : Entity_Id;
484 N : Node_Id;
485 Caller : Entity_Id;
486 Callee : Entity_Id);
487 -- Called when we detect an explicit or implicit uplevel reference
488 -- from within Caller to entity E declared in Callee. E can be a
489 -- an object or a type.
490
491 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id);
492 -- Enter a subprogram whose body is visible or which is a
493 -- subprogram instance into the subprogram table.
494
495 -----------------------
496 -- Check_Static_Type --
497 -----------------------
498
499 procedure Check_Static_Type
500 (T : Entity_Id;
501 N : Node_Id;
502 DT : in out Boolean;
503 Check_Designated : Boolean := False)
504 is
505 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id);
506 -- N is the bound of a dynamic type. This procedure notes that
507 -- this bound is uplevel referenced, it can handle references
508 -- to entities (typically _FIRST and _LAST entities), and also
509 -- attribute references of the form T'name (name is typically
510 -- FIRST or LAST) where T is the uplevel referenced bound.
511 -- Ref, if Present, is the location of the reference to
512 -- replace.
513
514 ------------------------
515 -- Note_Uplevel_Bound --
516 ------------------------
517
518 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id) is
519 begin
520 -- Entity name case. Make sure that the entity is declared
521 -- in a subprogram. This may not be the case for for a type
522 -- in a loop appearing in a precondition.
523 -- Exclude explicitly discriminants (that can appear
524 -- in bounds of discriminated components).
525
526 if Is_Entity_Name (N) then
527 if Present (Entity (N))
528 and then not Is_Type (Entity (N))
529 and then Present (Enclosing_Subprogram (Entity (N)))
530 and then Ekind (Entity (N)) /= E_Discriminant
531 then
532 Note_Uplevel_Ref
533 (E => Entity (N),
534 N => Empty,
535 Caller => Current_Subprogram,
536 Callee => Enclosing_Subprogram (Entity (N)));
537 end if;
538
539 -- Attribute or indexed component case
540
541 elsif Nkind_In (N, N_Attribute_Reference,
542 N_Indexed_Component)
543 then
544 Note_Uplevel_Bound (Prefix (N), Ref);
545
546 -- The indices of the indexed components, or the
547 -- associated expressions of an attribute reference,
548 -- may also involve uplevel references.
549
550 declare
551 Expr : Node_Id;
552
553 begin
554 Expr := First (Expressions (N));
555 while Present (Expr) loop
556 Note_Uplevel_Bound (Expr, Ref);
557 Next (Expr);
558 end loop;
559 end;
560
561 -- The type of the prefix may be have an uplevel
562 -- reference if this needs bounds.
563
564 if Nkind (N) = N_Attribute_Reference then
565 declare
566 Attr : constant Attribute_Id :=
567 Get_Attribute_Id (Attribute_Name (N));
568 DT : Boolean := False;
569
570 begin
571 if (Attr = Attribute_First
572 or else Attr = Attribute_Last
573 or else Attr = Attribute_Length)
574 and then Is_Constrained (Etype (Prefix (N)))
575 then
576 Check_Static_Type
577 (Etype (Prefix (N)), Empty, DT);
578 end if;
579 end;
580 end if;
581
582 -- Binary operator cases. These can apply to arrays for
583 -- which we may need bounds.
584
585 elsif Nkind (N) in N_Binary_Op then
586 Note_Uplevel_Bound (Left_Opnd (N), Ref);
587 Note_Uplevel_Bound (Right_Opnd (N), Ref);
588
589 -- Unary operator case
590
591 elsif Nkind (N) in N_Unary_Op then
592 Note_Uplevel_Bound (Right_Opnd (N), Ref);
593
594 -- Explicit dereference and selected component case
595
596 elsif Nkind_In (N, N_Explicit_Dereference,
597 N_Selected_Component)
598 then
599 Note_Uplevel_Bound (Prefix (N), Ref);
600
601 -- Conditional expressions.
602
603 elsif Nkind (N) = N_If_Expression then
604 declare
605 Expr : Node_Id;
606
607 begin
608 Expr := First (Expressions (N));
609 while Present (Expr) loop
610 Note_Uplevel_Bound (Expr, Ref);
611 Next (Expr);
612 end loop;
613 end;
614
615 elsif Nkind (N) = N_Case_Expression then
616 declare
617 Alternative : Node_Id;
618
619 begin
620 Note_Uplevel_Bound (Expression (N), Ref);
621
622 Alternative := First (Alternatives (N));
623 while Present (Alternative) loop
624 Note_Uplevel_Bound (Expression (Alternative), Ref);
625 end loop;
626 end;
627
628 -- Conversion case
629
630 elsif Nkind (N) = N_Type_Conversion then
631 Note_Uplevel_Bound (Expression (N), Ref);
632 end if;
633 end Note_Uplevel_Bound;
634
635 -- Start of processing for Check_Static_Type
636
637 begin
638 -- If already marked static, immediate return
639
640 if Is_Static_Type (T) and then not Check_Designated then
641 return;
642 end if;
643
644 -- If the type is at library level, always consider it static,
645 -- since such uplevel references are irrelevant.
646
647 if Is_Library_Level_Entity (T) then
648 Set_Is_Static_Type (T);
649 return;
650 end if;
651
652 -- Otherwise figure out what the story is with this type
653
654 -- For a scalar type, check bounds
655
656 if Is_Scalar_Type (T) then
657
658 -- If both bounds static, then this is a static type
659
660 declare
661 LB : constant Node_Id := Type_Low_Bound (T);
662 UB : constant Node_Id := Type_High_Bound (T);
663
664 begin
665 if not Is_Static_Expression (LB) then
666 Note_Uplevel_Bound (LB, N);
667 DT := True;
668 end if;
669
670 if not Is_Static_Expression (UB) then
671 Note_Uplevel_Bound (UB, N);
672 DT := True;
673 end if;
674 end;
675
676 -- For record type, check all components and discriminant
677 -- constraints if present.
678
679 elsif Is_Record_Type (T) then
680 declare
681 C : Entity_Id;
682 D : Elmt_Id;
683
684 begin
685 C := First_Component_Or_Discriminant (T);
686 while Present (C) loop
687 Check_Static_Type (Etype (C), N, DT);
688 Next_Component_Or_Discriminant (C);
689 end loop;
690
691 if Has_Discriminants (T)
692 and then Present (Discriminant_Constraint (T))
693 then
694 D := First_Elmt (Discriminant_Constraint (T));
695 while Present (D) loop
696 if not Is_Static_Expression (Node (D)) then
697 Note_Uplevel_Bound (Node (D), N);
698 DT := True;
699 end if;
700
701 Next_Elmt (D);
702 end loop;
703 end if;
704 end;
705
706 -- For array type, check index types and component type
707
708 elsif Is_Array_Type (T) then
709 declare
710 IX : Node_Id;
711 begin
712 Check_Static_Type (Component_Type (T), N, DT);
713
714 IX := First_Index (T);
715 while Present (IX) loop
716 Check_Static_Type (Etype (IX), N, DT);
717 Next_Index (IX);
718 end loop;
719 end;
720
721 -- For private type, examine whether full view is static
722
723 elsif Is_Incomplete_Or_Private_Type (T)
724 and then Present (Full_View (T))
725 then
726 Check_Static_Type (Full_View (T), N, DT, Check_Designated);
727
728 if Is_Static_Type (Full_View (T)) then
729 Set_Is_Static_Type (T);
730 end if;
731
732 -- For access types, check designated type when required
733
734 elsif Is_Access_Type (T) and then Check_Designated then
735 Check_Static_Type (Directly_Designated_Type (T), N, DT);
736
737 -- For now, ignore other types
738
739 else
740 return;
741 end if;
742
743 if not DT then
744 Set_Is_Static_Type (T);
745 end if;
746 end Check_Static_Type;
747
748 ----------------------
749 -- Note_Uplevel_Ref --
750 ----------------------
751
752 procedure Note_Uplevel_Ref
753 (E : Entity_Id;
754 N : Node_Id;
755 Caller : Entity_Id;
756 Callee : Entity_Id)
757 is
758 Full_E : Entity_Id := E;
759 begin
760 -- Nothing to do for static type
761
762 if Is_Static_Type (E) then
763 return;
764 end if;
765
766 -- Nothing to do if Caller and Callee are the same
767
768 if Caller = Callee then
769 return;
770
771 -- Callee may be a function that returns an array, and that has
772 -- been rewritten as a procedure. If caller is that procedure,
773 -- nothing to do either.
774
775 elsif Ekind (Callee) = E_Function
776 and then Rewritten_For_C (Callee)
777 and then Corresponding_Procedure (Callee) = Caller
778 then
779 return;
780
781 elsif Ekind_In (Callee, E_Entry, E_Entry_Family) then
782 return;
783 end if;
784
785 -- We have a new uplevel referenced entity
786
787 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
788 Full_E := Full_View (E);
789 end if;
790
791 -- All we do at this stage is to add the uplevel reference to
792 -- the table. It's too early to do anything else, since this
793 -- uplevel reference may come from an unreachable subprogram
794 -- in which case the entry will be deleted.
795
796 Urefs.Append ((N, Full_E, Caller, Callee));
797 end Note_Uplevel_Ref;
798
799 -------------------------
800 -- Register_Subprogram --
801 -------------------------
802
803 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id) is
804 L : constant Nat := Get_Level (Subp, E);
805
806 begin
807 -- Subprograms declared in tasks and protected types cannot be
808 -- eliminated because calls to them may be in other units, so
809 -- they must be treated as reachable.
810
811 Subps.Append
812 ((Ent => E,
813 Bod => Bod,
814 Lev => L,
815 Reachable => In_Synchronized_Unit (E)
816 or else Address_Taken (E),
817 Uplevel_Ref => L,
818 Declares_AREC => False,
819 Uents => No_Elist,
820 Last => 0,
821 ARECnF => Empty,
822 ARECn => Empty,
823 ARECnT => Empty,
824 ARECnPT => Empty,
825 ARECnP => Empty,
826 ARECnU => Empty));
827
828 Set_Subps_Index (E, UI_From_Int (Subps.Last));
829
830 -- If we marked this reachable because it's in a synchronized
831 -- unit, we have to mark all enclosing subprograms as reachable
832 -- as well.
833
834 if In_Synchronized_Unit (E) then
835 declare
836 S : Entity_Id := E;
837
838 begin
839 for J in reverse 1 .. L - 1 loop
840 S := Enclosing_Subprogram (S);
841 Subps.Table (Subp_Index (S)).Reachable := True;
842 end loop;
843 end;
844 end if;
845 end Register_Subprogram;
846
847 -- Start of processing for Visit_Node
848
849 begin
850 case Nkind (N) is
851
852 -- Record a subprogram call
853
854 when N_Function_Call
855 | N_Procedure_Call_Statement
856 =>
857 -- We are only interested in direct calls, not indirect
858 -- calls (where Name (N) is an explicit dereference) at
859 -- least for now!
860
861 if Nkind (Name (N)) in N_Has_Entity then
862 Ent := Entity (Name (N));
863
864 -- We are only interested in calls to subprograms nested
865 -- within Subp. Calls to Subp itself or to subprograms
866 -- outside the nested structure do not affect us.
867
868 if Scope_Within (Ent, Subp)
869 and then Is_Subprogram (Ent)
870 and then not Is_Imported (Ent)
871 then
872 Append_Unique_Call ((N, Current_Subprogram, Ent));
873 end if;
874 end if;
875
876 -- For all calls where the formal is an unconstrained array
877 -- and the actual is constrained we need to check the bounds
878 -- for uplevel references.
879
880 declare
881 Actual : Entity_Id;
882 DT : Boolean := False;
883 Formal : Node_Id;
884 Subp : Entity_Id;
885
886 begin
887 if Nkind (Name (N)) = N_Explicit_Dereference then
888 Subp := Etype (Name (N));
889 else
890 Subp := Entity (Name (N));
891 end if;
892
893 Actual := First_Actual (N);
894 Formal := First_Formal_With_Extras (Subp);
895 while Present (Actual) loop
896 if Is_Array_Type (Etype (Formal))
897 and then not Is_Constrained (Etype (Formal))
898 and then Is_Constrained (Etype (Actual))
899 then
900 Check_Static_Type (Etype (Actual), Empty, DT);
901 end if;
902
903 Next_Actual (Actual);
904 Next_Formal_With_Extras (Formal);
905 end loop;
906 end;
907
908 -- An At_End_Proc in a statement sequence indicates that there
909 -- is a call from the enclosing construct or block to that
910 -- subprogram. As above, the called entity must be local and
911 -- not imported.
912
913 when N_Handled_Sequence_Of_Statements =>
914 if Present (At_End_Proc (N))
915 and then Scope_Within (Entity (At_End_Proc (N)), Subp)
916 and then not Is_Imported (Entity (At_End_Proc (N)))
917 then
918 Append_Unique_Call
919 ((N, Current_Subprogram, Entity (At_End_Proc (N))));
920 end if;
921
922 -- Similarly, the following constructs include a semantic
923 -- attribute Procedure_To_Call that must be handled like
924 -- other calls. Likewise for attribute Storage_Pool.
925
926 when N_Allocator
927 | N_Extended_Return_Statement
928 | N_Free_Statement
929 | N_Simple_Return_Statement
930 =>
931 declare
932 Pool : constant Entity_Id := Storage_Pool (N);
933 Proc : constant Entity_Id := Procedure_To_Call (N);
934
935 begin
936 if Present (Proc)
937 and then Scope_Within (Proc, Subp)
938 and then not Is_Imported (Proc)
939 then
940 Append_Unique_Call ((N, Current_Subprogram, Proc));
941 end if;
942
943 if Present (Pool)
944 and then not Is_Library_Level_Entity (Pool)
945 and then Scope_Within_Or_Same (Scope (Pool), Subp)
946 then
947 Caller := Current_Subprogram;
948 Callee := Enclosing_Subprogram (Pool);
949
950 if Callee /= Caller then
951 Note_Uplevel_Ref (Pool, Empty, Caller, Callee);
952 end if;
953 end if;
954 end;
955
956 -- For an allocator with a qualified expression, check type
957 -- of expression being qualified. The explicit type name is
958 -- handled as an entity reference.
959
960 if Nkind (N) = N_Allocator
961 and then Nkind (Expression (N)) = N_Qualified_Expression
962 then
963 declare
964 DT : Boolean := False;
965 begin
966 Check_Static_Type
967 (Etype (Expression (Expression (N))), Empty, DT);
968 end;
969
970 -- For a Return or Free (all other nodes we handle here),
971 -- we usually need the size of the object, so we need to be
972 -- sure that any nonstatic bounds of the expression's type
973 -- that are uplevel are handled.
974
975 elsif Nkind (N) /= N_Allocator
976 and then Present (Expression (N))
977 then
978 declare
979 DT : Boolean := False;
980 begin
981 Check_Static_Type
982 (Etype (Expression (N)),
983 Empty,
984 DT,
985 Check_Designated => Nkind (N) = N_Free_Statement);
986 end;
987 end if;
988
989 -- A 'Access reference is a (potential) call. So is 'Address,
990 -- in particular on imported subprograms. Other attributes
991 -- require special handling.
992
993 when N_Attribute_Reference =>
994 declare
995 Attr : constant Attribute_Id :=
996 Get_Attribute_Id (Attribute_Name (N));
997 begin
998 case Attr is
999 when Attribute_Access
1000 | Attribute_Unchecked_Access
1001 | Attribute_Unrestricted_Access
1002 | Attribute_Address
1003 =>
1004 if Nkind (Prefix (N)) in N_Has_Entity then
1005 Ent := Entity (Prefix (N));
1006
1007 -- We only need to examine calls to subprograms
1008 -- nested within current Subp.
1009
1010 if Scope_Within (Ent, Subp) then
1011 if Is_Imported (Ent) then
1012 null;
1013
1014 elsif Is_Subprogram (Ent) then
1015 Append_Unique_Call
1016 ((N, Current_Subprogram, Ent));
1017 end if;
1018 end if;
1019 end if;
1020
1021 -- References to bounds can be uplevel references if
1022 -- the type isn't static.
1023
1024 when Attribute_First
1025 | Attribute_Last
1026 | Attribute_Length
1027 =>
1028 -- Special-case attributes of objects whose bounds
1029 -- may be uplevel references. More complex prefixes
1030 -- handled during full traversal. Note that if the
1031 -- nominal subtype of the prefix is unconstrained,
1032 -- the bound must be obtained from the object, not
1033 -- from the (possibly) uplevel reference.
1034
1035 if Is_Constrained (Etype (Prefix (N))) then
1036 declare
1037 DT : Boolean := False;
1038 begin
1039 Check_Static_Type
1040 (Etype (Prefix (N)), Empty, DT);
1041 end;
1042
1043 return OK;
1044 end if;
1045
1046 when others =>
1047 null;
1048 end case;
1049 end;
1050
1051 -- Component associations in aggregates are either static or
1052 -- else the aggregate will be expanded into assignments, in
1053 -- which case the expression is analyzed later and provides
1054 -- no relevant code generation.
1055
1056 when N_Component_Association =>
1057 if No (Expression (N))
1058 or else No (Etype (Expression (N)))
1059 then
1060 return Skip;
1061 end if;
1062
1063 -- Generic associations are not analyzed: the actuals are
1064 -- transferred to renaming and subtype declarations that
1065 -- are the ones that must be examined.
1066
1067 when N_Generic_Association =>
1068 return Skip;
1069
1070 -- Indexed references can be uplevel if the type isn't static
1071 -- and if the lower bound (or an inner bound for a multi-
1072 -- dimensional array) is uplevel.
1073
1074 when N_Indexed_Component
1075 | N_Slice
1076 =>
1077 if Is_Constrained (Etype (Prefix (N))) then
1078 declare
1079 DT : Boolean := False;
1080 begin
1081 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
1082 end;
1083 end if;
1084
1085 -- A selected component can have an implicit up-level
1086 -- reference due to the bounds of previous fields in the
1087 -- record. We simplify the processing here by examining
1088 -- all components of the record.
1089
1090 -- Selected components appear as unit names and end labels
1091 -- for child units. Prefixes of these nodes denote parent
1092 -- units and carry no type information so they are skipped.
1093
1094 when N_Selected_Component =>
1095 if Present (Etype (Prefix (N))) then
1096 declare
1097 DT : Boolean := False;
1098 begin
1099 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
1100 end;
1101 end if;
1102
1103 -- For EQ/NE comparisons, we need the type of the operands
1104 -- in order to do the comparison, which means we need the
1105 -- bounds.
1106
1107 when N_Op_Eq
1108 | N_Op_Ne
1109 =>
1110 declare
1111 DT : Boolean := False;
1112 begin
1113 Check_Static_Type (Etype (Left_Opnd (N)), Empty, DT);
1114 Check_Static_Type (Etype (Right_Opnd (N)), Empty, DT);
1115 end;
1116
1117 -- Likewise we need the sizes to compute how much to move in
1118 -- an assignment.
1119
1120 when N_Assignment_Statement =>
1121 declare
1122 DT : Boolean := False;
1123 begin
1124 Check_Static_Type (Etype (Name (N)), Empty, DT);
1125 Check_Static_Type (Etype (Expression (N)), Empty, DT);
1126 end;
1127
1128 -- Record a subprogram. We record a subprogram body that acts
1129 -- as a spec. Otherwise we record a subprogram declaration,
1130 -- providing that it has a corresponding body we can get hold
1131 -- of. The case of no corresponding body being available is
1132 -- ignored for now.
1133
1134 when N_Subprogram_Body =>
1135 Ent := Unique_Defining_Entity (N);
1136
1137 -- Ignore generic subprogram
1138
1139 if Is_Generic_Subprogram (Ent) then
1140 return Skip;
1141 end if;
1142
1143 -- Make new entry in subprogram table if not already made
1144
1145 Register_Subprogram (Ent, N);
1146
1147 -- We make a recursive call to scan the subprogram body, so
1148 -- that we can save and restore Current_Subprogram.
1149
1150 declare
1151 Save_CS : constant Entity_Id := Current_Subprogram;
1152 Decl : Node_Id;
1153
1154 begin
1155 Current_Subprogram := Ent;
1156
1157 -- Scan declarations
1158
1159 Decl := First (Declarations (N));
1160 while Present (Decl) loop
1161 Visit (Decl);
1162 Next (Decl);
1163 end loop;
1164
1165 -- Scan statements
1166
1167 Visit (Handled_Statement_Sequence (N));
1168
1169 -- Restore current subprogram setting
1170
1171 Current_Subprogram := Save_CS;
1172 end;
1173
1174 -- Now at this level, return skipping the subprogram body
1175 -- descendants, since we already took care of them!
1176
1177 return Skip;
1178
1179 -- If we have a body stub, visit the associated subunit, which
1180 -- is a semantic descendant of the stub.
1181
1182 when N_Body_Stub =>
1183 Visit (Library_Unit (N));
1184
1185 -- A declaration of a wrapper package indicates a subprogram
1186 -- instance for which there is no explicit body. Enter the
1187 -- subprogram instance in the table.
1188
1189 when N_Package_Declaration =>
1190 if Is_Wrapper_Package (Defining_Entity (N)) then
1191 Register_Subprogram
1192 (Related_Instance (Defining_Entity (N)), Empty);
1193 end if;
1194
1195 -- Skip generic declarations
1196
1197 when N_Generic_Declaration =>
1198 return Skip;
1199
1200 -- Skip generic package body
1201
1202 when N_Package_Body =>
1203 if Present (Corresponding_Spec (N))
1204 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
1205 then
1206 return Skip;
1207 end if;
1208
1209 -- Pragmas and component declarations are ignored. Quantified
1210 -- expressions are expanded into explicit loops and the
1211 -- original epression must be ignored.
1212
1213 when N_Component_Declaration
1214 | N_Pragma
1215 | N_Quantified_Expression
1216 =>
1217 return Skip;
1218
1219 -- We want to skip the function spec for a generic function
1220 -- to avoid looking at any generic types that might be in
1221 -- its formals.
1222
1223 when N_Function_Specification =>
1224 if Is_Generic_Subprogram (Unique_Defining_Entity (N)) then
1225 return Skip;
1226 end if;
1227
1228 -- Otherwise record an uplevel reference in a local identifier
1229
1230 when others =>
1231 if Nkind (N) in N_Has_Entity
1232 and then Present (Entity (N))
1233 then
1234 Ent := Entity (N);
1235
1236 -- Only interested in entities declared within our nest
1237
1238 if not Is_Library_Level_Entity (Ent)
1239 and then Scope_Within_Or_Same (Scope (Ent), Subp)
1240
1241 -- Skip entities defined in inlined subprograms
1242
1243 and then
1244 Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
1245
1246 -- Constants and variables are potentially uplevel
1247 -- references to global declarations.
1248
1249 and then
1250 (Ekind_In (Ent, E_Constant,
1251 E_Loop_Parameter,
1252 E_Variable)
1253
1254 -- Formals are interesting, but not if being used
1255 -- as mere names of parameters for name notation
1256 -- calls.
1257
1258 or else
1259 (Is_Formal (Ent)
1260 and then not
1261 (Nkind (Parent (N)) = N_Parameter_Association
1262 and then Selector_Name (Parent (N)) = N))
1263
1264 -- Types other than known Is_Static types are
1265 -- potentially interesting.
1266
1267 or else
1268 (Is_Type (Ent) and then not Is_Static_Type (Ent)))
1269 then
1270 -- Here we have a potentially interesting uplevel
1271 -- reference to examine.
1272
1273 if Is_Type (Ent) then
1274 declare
1275 DT : Boolean := False;
1276
1277 begin
1278 Check_Static_Type (Ent, N, DT);
1279 return OK;
1280 end;
1281 end if;
1282
1283 Caller := Current_Subprogram;
1284 Callee := Enclosing_Subprogram (Ent);
1285
1286 if Callee /= Caller
1287 and then (not Is_Static_Type (Ent)
1288 or else Needs_Fat_Pointer (Ent))
1289 then
1290 Note_Uplevel_Ref (Ent, N, Caller, Callee);
1291
1292 -- Check the type of a formal parameter of the current
1293 -- subprogram, whose formal type may be an uplevel
1294 -- reference.
1295
1296 elsif Is_Formal (Ent)
1297 and then Scope (Ent) = Current_Subprogram
1298 then
1299 declare
1300 DT : Boolean := False;
1301
1302 begin
1303 Check_Static_Type (Etype (Ent), Empty, DT);
1304 end;
1305 end if;
1306 end if;
1307 end if;
1308 end case;
1309
1310 -- Fall through to continue scanning children of this node
1311
1312 return OK;
1313 end Visit_Node;
1314
1315 -- Start of processing for Build_Tables
1316
1317 begin
1318 -- Traverse the body to get subprograms, calls and uplevel references
1319
1320 Visit (Subp_Body);
1321 end Build_Tables;
1322
1323 -- Now do the first transitive closure which determines which
1324 -- subprograms in the nest are actually reachable.
1325
1326 Reachable_Closure : declare
1327 Modified : Boolean;
1328
1329 begin
1330 Subps.Table (Subps_First).Reachable := True;
1331
1332 -- We use a simple minded algorithm as follows (obviously this can
1333 -- be done more efficiently, using one of the standard algorithms
1334 -- for efficient transitive closure computation, but this is simple
1335 -- and most likely fast enough that its speed does not matter).
1336
1337 -- Repeatedly scan the list of calls. Any time we find a call from
1338 -- A to B, where A is reachable, but B is not, then B is reachable,
1339 -- and note that we have made a change by setting Modified True. We
1340 -- repeat this until we make a pass with no modifications.
1341
1342 Outer : loop
1343 Modified := False;
1344 Inner : for J in Calls.First .. Calls.Last loop
1345 declare
1346 CTJ : Call_Entry renames Calls.Table (J);
1347
1348 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1349 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1350
1351 SUBF : Subp_Entry renames Subps.Table (SINF);
1352 SUBT : Subp_Entry renames Subps.Table (SINT);
1353
1354 begin
1355 if SUBF.Reachable and then not SUBT.Reachable then
1356 SUBT.Reachable := True;
1357 Modified := True;
1358 end if;
1359 end;
1360 end loop Inner;
1361
1362 exit Outer when not Modified;
1363 end loop Outer;
1364 end Reachable_Closure;
1365
1366 -- Remove calls from unreachable subprograms
1367
1368 declare
1369 New_Index : Nat;
1370
1371 begin
1372 New_Index := 0;
1373 for J in Calls.First .. Calls.Last loop
1374 declare
1375 CTJ : Call_Entry renames Calls.Table (J);
1376
1377 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1378 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1379
1380 SUBF : Subp_Entry renames Subps.Table (SINF);
1381 SUBT : Subp_Entry renames Subps.Table (SINT);
1382
1383 begin
1384 if SUBF.Reachable then
1385 pragma Assert (SUBT.Reachable);
1386 New_Index := New_Index + 1;
1387 Calls.Table (New_Index) := Calls.Table (J);
1388 end if;
1389 end;
1390 end loop;
1391
1392 Calls.Set_Last (New_Index);
1393 end;
1394
1395 -- Remove uplevel references from unreachable subprograms
1396
1397 declare
1398 New_Index : Nat;
1399
1400 begin
1401 New_Index := 0;
1402 for J in Urefs.First .. Urefs.Last loop
1403 declare
1404 URJ : Uref_Entry renames Urefs.Table (J);
1405
1406 SINF : constant SI_Type := Subp_Index (URJ.Caller);
1407 SINT : constant SI_Type := Subp_Index (URJ.Callee);
1408
1409 SUBF : Subp_Entry renames Subps.Table (SINF);
1410 SUBT : Subp_Entry renames Subps.Table (SINT);
1411
1412 S : Entity_Id;
1413
1414 begin
1415 -- Keep reachable reference
1416
1417 if SUBF.Reachable then
1418 New_Index := New_Index + 1;
1419 Urefs.Table (New_Index) := Urefs.Table (J);
1420
1421 -- And since we know we are keeping this one, this is a good
1422 -- place to fill in information for a good reference.
1423
1424 -- Mark all enclosing subprograms need to declare AREC
1425
1426 S := URJ.Caller;
1427 loop
1428 S := Enclosing_Subprogram (S);
1429
1430 -- If we are at the top level, as can happen with
1431 -- references to formals in aspects of nested subprogram
1432 -- declarations, there are no further subprograms to mark
1433 -- as requiring activation records.
1434
1435 exit when No (S);
1436
1437 declare
1438 SUBI : Subp_Entry renames Subps.Table (Subp_Index (S));
1439 begin
1440 SUBI.Declares_AREC := True;
1441
1442 -- If this entity was marked reachable because it is
1443 -- in a task or protected type, there may not appear
1444 -- to be any calls to it, which would normally adjust
1445 -- the levels of the parent subprograms. So we need to
1446 -- be sure that the uplevel reference of that entity
1447 -- takes into account possible calls.
1448
1449 if In_Synchronized_Unit (SUBF.Ent)
1450 and then SUBT.Lev < SUBI.Uplevel_Ref
1451 then
1452 SUBI.Uplevel_Ref := SUBT.Lev;
1453 end if;
1454 end;
1455
1456 exit when S = URJ.Callee;
1457 end loop;
1458
1459 -- Add to list of uplevel referenced entities for Callee.
1460 -- We do not add types to this list, only actual references
1461 -- to objects that will be referenced uplevel, and we use
1462 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1463 -- duplicate entries in the list. Discriminants are also
1464 -- excluded, only the enclosing object can appear in the
1465 -- list.
1466
1467 if not Is_Uplevel_Referenced_Entity (URJ.Ent)
1468 and then Ekind (URJ.Ent) /= E_Discriminant
1469 then
1470 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
1471 Append_New_Elmt (URJ.Ent, SUBT.Uents);
1472 end if;
1473
1474 -- And set uplevel indication for caller
1475
1476 if SUBT.Lev < SUBF.Uplevel_Ref then
1477 SUBF.Uplevel_Ref := SUBT.Lev;
1478 end if;
1479 end if;
1480 end;
1481 end loop;
1482
1483 Urefs.Set_Last (New_Index);
1484 end;
1485
1486 -- Remove unreachable subprograms from Subps table. Note that we do
1487 -- this after eliminating entries from the other two tables, since
1488 -- those elimination steps depend on referencing the Subps table.
1489
1490 declare
1491 New_SI : SI_Type;
1492
1493 begin
1494 New_SI := Subps_First - 1;
1495 for J in Subps_First .. Subps.Last loop
1496 declare
1497 STJ : Subp_Entry renames Subps.Table (J);
1498 Spec : Node_Id;
1499 Decl : Node_Id;
1500
1501 begin
1502 -- Subprogram is reachable, copy and reset index
1503
1504 if STJ.Reachable then
1505 New_SI := New_SI + 1;
1506 Subps.Table (New_SI) := STJ;
1507 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
1508
1509 -- Subprogram is not reachable
1510
1511 else
1512 -- Clear index, since no longer active
1513
1514 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
1515
1516 -- Output debug information if -gnatd.3 set
1517
1518 if Debug_Flag_Dot_3 then
1519 Write_Str ("Eliminate ");
1520 Write_Name (Chars (Subps.Table (J).Ent));
1521 Write_Str (" at ");
1522 Write_Location (Sloc (Subps.Table (J).Ent));
1523 Write_Str (" (not referenced)");
1524 Write_Eol;
1525 end if;
1526
1527 -- Rewrite declaration, body, and corresponding freeze node
1528 -- to null statements.
1529
1530 -- A subprogram instantiation does not have an explicit
1531 -- body. If unused, we could remove the corresponding
1532 -- wrapper package and its body (TBD).
1533
1534 if Present (STJ.Bod) then
1535 Spec := Corresponding_Spec (STJ.Bod);
1536
1537 if Present (Spec) then
1538 Decl := Parent (Declaration_Node (Spec));
1539 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
1540
1541 if Present (Freeze_Node (Spec)) then
1542 Rewrite (Freeze_Node (Spec),
1543 Make_Null_Statement (Sloc (Decl)));
1544 end if;
1545 end if;
1546
1547 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
1548 end if;
1549 end if;
1550 end;
1551 end loop;
1552
1553 Subps.Set_Last (New_SI);
1554 end;
1555
1556 -- Now it is time for the second transitive closure, which follows calls
1557 -- and makes sure that A calls B, and B has uplevel references, then A
1558 -- is also marked as having uplevel references.
1559
1560 Closure_Uplevel : declare
1561 Modified : Boolean;
1562
1563 begin
1564 -- We use a simple minded algorithm as follows (obviously this can
1565 -- be done more efficiently, using one of the standard algorithms
1566 -- for efficient transitive closure computation, but this is simple
1567 -- and most likely fast enough that its speed does not matter).
1568
1569 -- Repeatedly scan the list of calls. Any time we find a call from
1570 -- A to B, where B has uplevel references, make sure that A is marked
1571 -- as having at least the same level of uplevel referencing.
1572
1573 Outer2 : loop
1574 Modified := False;
1575 Inner2 : for J in Calls.First .. Calls.Last loop
1576 declare
1577 CTJ : Call_Entry renames Calls.Table (J);
1578 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1579 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1580 SUBF : Subp_Entry renames Subps.Table (SINF);
1581 SUBT : Subp_Entry renames Subps.Table (SINT);
1582 begin
1583 if SUBT.Lev > SUBT.Uplevel_Ref
1584 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
1585 then
1586 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
1587 Modified := True;
1588 end if;
1589 end;
1590 end loop Inner2;
1591
1592 exit Outer2 when not Modified;
1593 end loop Outer2;
1594 end Closure_Uplevel;
1595
1596 -- We have one more step before the tables are complete. An uplevel
1597 -- call from subprogram A to subprogram B where subprogram B has uplevel
1598 -- references is in effect an uplevel reference, and must arrange for
1599 -- the proper activation link to be passed.
1600
1601 for J in Calls.First .. Calls.Last loop
1602 declare
1603 CTJ : Call_Entry renames Calls.Table (J);
1604
1605 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1606 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1607
1608 SUBF : Subp_Entry renames Subps.Table (SINF);
1609 SUBT : Subp_Entry renames Subps.Table (SINT);
1610
1611 A : Entity_Id;
1612
1613 begin
1614 -- If callee has uplevel references
1615
1616 if SUBT.Uplevel_Ref < SUBT.Lev
1617
1618 -- And this is an uplevel call
1619
1620 and then SUBT.Lev < SUBF.Lev
1621 then
1622 -- We need to arrange for finding the uplink
1623
1624 A := CTJ.Caller;
1625 loop
1626 A := Enclosing_Subprogram (A);
1627 Subps.Table (Subp_Index (A)).Declares_AREC := True;
1628 exit when A = CTJ.Callee;
1629
1630 -- In any case exit when we get to the outer level. This
1631 -- happens in some odd cases with generics (in particular
1632 -- sem_ch3.adb does not compile without this kludge ???).
1633
1634 exit when A = Subp;
1635 end loop;
1636 end if;
1637 end;
1638 end loop;
1639
1640 -- The tables are now complete, so we can record the last index in the
1641 -- Subps table for later reference in Cprint.
1642
1643 Subps.Table (Subps_First).Last := Subps.Last;
1644
1645 -- Next step, create the entities for code we will insert. We do this
1646 -- at the start so that all the entities are defined, regardless of the
1647 -- order in which we do the code insertions.
1648
1649 Create_Entities : for J in Subps_First .. Subps.Last loop
1650 declare
1651 STJ : Subp_Entry renames Subps.Table (J);
1652 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1653
1654 begin
1655 -- First we create the ARECnF entity for the additional formal for
1656 -- all subprograms which need an activation record passed.
1657
1658 if STJ.Uplevel_Ref < STJ.Lev then
1659 STJ.ARECnF :=
1660 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
1661 end if;
1662
1663 -- Define the AREC entities for the activation record if needed
1664
1665 if STJ.Declares_AREC then
1666 STJ.ARECn :=
1667 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
1668 STJ.ARECnT :=
1669 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
1670 STJ.ARECnPT :=
1671 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
1672 STJ.ARECnP :=
1673 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
1674
1675 -- Define uplink component entity if inner nesting case
1676
1677 if Present (STJ.ARECnF) then
1678 STJ.ARECnU :=
1679 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
1680 end if;
1681 end if;
1682 end;
1683 end loop Create_Entities;
1684
1685 -- Loop through subprograms
1686
1687 Subp_Loop : declare
1688 Addr : Entity_Id := Empty;
1689
1690 begin
1691 for J in Subps_First .. Subps.Last loop
1692 declare
1693 STJ : Subp_Entry renames Subps.Table (J);
1694
1695 begin
1696 -- First add the extra formal if needed. This applies to all
1697 -- nested subprograms that require an activation record to be
1698 -- passed, as indicated by ARECnF being defined.
1699
1700 if Present (STJ.ARECnF) then
1701
1702 -- Here we need the extra formal. We do the expansion and
1703 -- analysis of this manually, since it is fairly simple,
1704 -- and it is not obvious how we can get what we want if we
1705 -- try to use the normal Analyze circuit.
1706
1707 Add_Extra_Formal : declare
1708 Encl : constant SI_Type := Enclosing_Subp (J);
1709 STJE : Subp_Entry renames Subps.Table (Encl);
1710 -- Index and Subp_Entry for enclosing routine
1711
1712 Form : constant Entity_Id := STJ.ARECnF;
1713 -- The formal to be added. Note that n here is one less
1714 -- than the level of the subprogram itself (STJ.Ent).
1715
1716 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1717 -- S is an N_Function/Procedure_Specification node, and F
1718 -- is the new entity to add to this subprogramn spec as
1719 -- the last Extra_Formal.
1720
1721 ----------------------
1722 -- Add_Form_To_Spec --
1723 ----------------------
1724
1725 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1726 Sub : constant Entity_Id := Defining_Entity (S);
1727 Ent : Entity_Id;
1728
1729 begin
1730 -- Case of at least one Extra_Formal is present, set
1731 -- ARECnF as the new last entry in the list.
1732
1733 if Present (Extra_Formals (Sub)) then
1734 Ent := Extra_Formals (Sub);
1735 while Present (Extra_Formal (Ent)) loop
1736 Ent := Extra_Formal (Ent);
1737 end loop;
1738
1739 Set_Extra_Formal (Ent, F);
1740
1741 -- No Extra formals present
1742
1743 else
1744 Set_Extra_Formals (Sub, F);
1745 Ent := Last_Formal (Sub);
1746
1747 if Present (Ent) then
1748 Set_Extra_Formal (Ent, F);
1749 end if;
1750 end if;
1751 end Add_Form_To_Spec;
1752
1753 -- Start of processing for Add_Extra_Formal
1754
1755 begin
1756 -- Decorate the new formal entity
1757
1758 Set_Scope (Form, STJ.Ent);
1759 Set_Ekind (Form, E_In_Parameter);
1760 Set_Etype (Form, STJE.ARECnPT);
1761 Set_Mechanism (Form, By_Copy);
1762 Set_Never_Set_In_Source (Form, True);
1763 Set_Analyzed (Form, True);
1764 Set_Comes_From_Source (Form, False);
1765 Set_Is_Activation_Record (Form, True);
1766
1767 -- Case of only body present
1768
1769 if Acts_As_Spec (STJ.Bod) then
1770 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1771
1772 -- Case of separate spec
1773
1774 else
1775 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1776 end if;
1777 end Add_Extra_Formal;
1778 end if;
1779
1780 -- Processing for subprograms that declare an activation record
1781
1782 if Present (STJ.ARECn) then
1783
1784 -- Local declarations for one such subprogram
1785
1786 declare
1787 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1788
1789 Decls : constant List_Id := New_List;
1790 -- List of new declarations we create
1791
1792 Clist : List_Id;
1793 Comp : Entity_Id;
1794
1795 Decl_Assign : Node_Id;
1796 -- Assigment to set uplink, Empty if none
1797
1798 Decl_ARECnT : Node_Id;
1799 Decl_ARECnPT : Node_Id;
1800 Decl_ARECn : Node_Id;
1801 Decl_ARECnP : Node_Id;
1802 -- Declaration nodes for the AREC entities we build
1803
1804 begin
1805 -- Build list of component declarations for ARECnT and
1806 -- load System.Address.
1807
1808 Clist := Empty_List;
1809
1810 if No (Addr) then
1811 Addr := RTE (RE_Address);
1812 end if;
1813
1814 -- If we are in a subprogram that has a static link that
1815 -- is passed in (as indicated by ARECnF being defined),
1816 -- then include ARECnU : ARECmPT where ARECmPT comes from
1817 -- the level one higher than the current level, and the
1818 -- entity ARECnPT comes from the enclosing subprogram.
1819
1820 if Present (STJ.ARECnF) then
1821 declare
1822 STJE : Subp_Entry
1823 renames Subps.Table (Enclosing_Subp (J));
1824 begin
1825 Append_To (Clist,
1826 Make_Component_Declaration (Loc,
1827 Defining_Identifier => STJ.ARECnU,
1828 Component_Definition =>
1829 Make_Component_Definition (Loc,
1830 Subtype_Indication =>
1831 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1832 end;
1833 end if;
1834
1835 -- Add components for uplevel referenced entities
1836
1837 if Present (STJ.Uents) then
1838 declare
1839 Elmt : Elmt_Id;
1840 Ptr_Decl : Node_Id;
1841 Uent : Entity_Id;
1842
1843 Indx : Nat;
1844 -- 1's origin of index in list of elements. This is
1845 -- used to uniquify names if needed in Upref_Name.
1846
1847 begin
1848 Elmt := First_Elmt (STJ.Uents);
1849 Indx := 0;
1850 while Present (Elmt) loop
1851 Uent := Node (Elmt);
1852 Indx := Indx + 1;
1853
1854 Comp :=
1855 Make_Defining_Identifier (Loc,
1856 Chars => Upref_Name (Uent, Indx, Clist));
1857
1858 Set_Activation_Record_Component
1859 (Uent, Comp);
1860
1861 if Needs_Fat_Pointer (Uent) then
1862
1863 -- Build corresponding access type
1864
1865 Ptr_Decl :=
1866 Build_Access_Type_Decl
1867 (Etype (Uent), STJ.Ent);
1868 Append_To (Decls, Ptr_Decl);
1869
1870 -- And use its type in the corresponding
1871 -- component.
1872
1873 Append_To (Clist,
1874 Make_Component_Declaration (Loc,
1875 Defining_Identifier => Comp,
1876 Component_Definition =>
1877 Make_Component_Definition (Loc,
1878 Subtype_Indication =>
1879 New_Occurrence_Of
1880 (Defining_Identifier (Ptr_Decl),
1881 Loc))));
1882 else
1883 Append_To (Clist,
1884 Make_Component_Declaration (Loc,
1885 Defining_Identifier => Comp,
1886 Component_Definition =>
1887 Make_Component_Definition (Loc,
1888 Subtype_Indication =>
1889 New_Occurrence_Of (Addr, Loc))));
1890 end if;
1891 Next_Elmt (Elmt);
1892 end loop;
1893 end;
1894 end if;
1895
1896 -- Now we can insert the AREC declarations into the body
1897 -- type ARECnT is record .. end record;
1898 -- pragma Suppress_Initialization (ARECnT);
1899
1900 -- Note that we need to set the Suppress_Initialization
1901 -- flag after Decl_ARECnT has been analyzed.
1902
1903 Decl_ARECnT :=
1904 Make_Full_Type_Declaration (Loc,
1905 Defining_Identifier => STJ.ARECnT,
1906 Type_Definition =>
1907 Make_Record_Definition (Loc,
1908 Component_List =>
1909 Make_Component_List (Loc,
1910 Component_Items => Clist)));
1911 Append_To (Decls, Decl_ARECnT);
1912
1913 -- type ARECnPT is access all ARECnT;
1914
1915 Decl_ARECnPT :=
1916 Make_Full_Type_Declaration (Loc,
1917 Defining_Identifier => STJ.ARECnPT,
1918 Type_Definition =>
1919 Make_Access_To_Object_Definition (Loc,
1920 All_Present => True,
1921 Subtype_Indication =>
1922 New_Occurrence_Of (STJ.ARECnT, Loc)));
1923 Append_To (Decls, Decl_ARECnPT);
1924
1925 -- ARECn : aliased ARECnT;
1926
1927 Decl_ARECn :=
1928 Make_Object_Declaration (Loc,
1929 Defining_Identifier => STJ.ARECn,
1930 Aliased_Present => True,
1931 Object_Definition =>
1932 New_Occurrence_Of (STJ.ARECnT, Loc));
1933 Append_To (Decls, Decl_ARECn);
1934
1935 -- ARECnP : constant ARECnPT := ARECn'Access;
1936
1937 Decl_ARECnP :=
1938 Make_Object_Declaration (Loc,
1939 Defining_Identifier => STJ.ARECnP,
1940 Constant_Present => True,
1941 Object_Definition =>
1942 New_Occurrence_Of (STJ.ARECnPT, Loc),
1943 Expression =>
1944 Make_Attribute_Reference (Loc,
1945 Prefix =>
1946 New_Occurrence_Of (STJ.ARECn, Loc),
1947 Attribute_Name => Name_Access));
1948 Append_To (Decls, Decl_ARECnP);
1949
1950 -- If we are in a subprogram that has a static link that
1951 -- is passed in (as indicated by ARECnF being defined),
1952 -- then generate ARECn.ARECmU := ARECmF where m is
1953 -- one less than the current level to set the uplink.
1954
1955 if Present (STJ.ARECnF) then
1956 Decl_Assign :=
1957 Make_Assignment_Statement (Loc,
1958 Name =>
1959 Make_Selected_Component (Loc,
1960 Prefix =>
1961 New_Occurrence_Of (STJ.ARECn, Loc),
1962 Selector_Name =>
1963 New_Occurrence_Of (STJ.ARECnU, Loc)),
1964 Expression =>
1965 New_Occurrence_Of (STJ.ARECnF, Loc));
1966 Append_To (Decls, Decl_Assign);
1967
1968 else
1969 Decl_Assign := Empty;
1970 end if;
1971
1972 if No (Declarations (STJ.Bod)) then
1973 Set_Declarations (STJ.Bod, Decls);
1974 else
1975 Prepend_List_To (Declarations (STJ.Bod), Decls);
1976 end if;
1977
1978 -- Analyze the newly inserted declarations. Note that we
1979 -- do not need to establish the whole scope stack, since
1980 -- we have already set all entity fields (so there will
1981 -- be no searching of upper scopes to resolve names). But
1982 -- we do set the scope of the current subprogram, so that
1983 -- newly created entities go in the right entity chain.
1984
1985 -- We analyze with all checks suppressed (since we do
1986 -- not expect any exceptions).
1987
1988 Push_Scope (STJ.Ent);
1989 Analyze (Decl_ARECnT, Suppress => All_Checks);
1990
1991 -- Note that we need to call Set_Suppress_Initialization
1992 -- after Decl_ARECnT has been analyzed, but before
1993 -- analyzing Decl_ARECnP so that the flag is properly
1994 -- taking into account.
1995
1996 Set_Suppress_Initialization (STJ.ARECnT);
1997
1998 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1999 Analyze (Decl_ARECn, Suppress => All_Checks);
2000 Analyze (Decl_ARECnP, Suppress => All_Checks);
2001
2002 if Present (Decl_Assign) then
2003 Analyze (Decl_Assign, Suppress => All_Checks);
2004 end if;
2005
2006 Pop_Scope;
2007
2008 -- Next step, for each uplevel referenced entity, add
2009 -- assignment operations to set the component in the
2010 -- activation record.
2011
2012 if Present (STJ.Uents) then
2013 declare
2014 Elmt : Elmt_Id;
2015
2016 begin
2017 Elmt := First_Elmt (STJ.Uents);
2018 while Present (Elmt) loop
2019 declare
2020 Ent : constant Entity_Id := Node (Elmt);
2021 Loc : constant Source_Ptr := Sloc (Ent);
2022 Dec : constant Node_Id :=
2023 Declaration_Node (Ent);
2024
2025 Asn : Node_Id;
2026 Attr : Name_Id;
2027 Comp : Entity_Id;
2028 Ins : Node_Id;
2029 Rhs : Node_Id;
2030
2031 begin
2032 -- For parameters, we insert the assignment
2033 -- right after the declaration of ARECnP.
2034 -- For all other entities, we insert the
2035 -- assignment immediately after the
2036 -- declaration of the entity or after the
2037 -- freeze node if present.
2038
2039 -- Note: we don't need to mark the entity
2040 -- as being aliased, because the address
2041 -- attribute will mark it as Address_Taken,
2042 -- and that is good enough.
2043
2044 if Is_Formal (Ent) then
2045 Ins := Decl_ARECnP;
2046
2047 elsif Has_Delayed_Freeze (Ent) then
2048 Ins := Freeze_Node (Ent);
2049
2050 else
2051 Ins := Dec;
2052 end if;
2053
2054 -- Build and insert the assignment:
2055 -- ARECn.nam := nam'Address
2056 -- or else 'Access for unconstrained array
2057
2058 if Needs_Fat_Pointer (Ent) then
2059 Attr := Name_Access;
2060 else
2061 Attr := Name_Address;
2062 end if;
2063
2064 Rhs :=
2065 Make_Attribute_Reference (Loc,
2066 Prefix =>
2067 New_Occurrence_Of (Ent, Loc),
2068 Attribute_Name => Attr);
2069
2070 -- If the entity is an unconstrained formal
2071 -- we wrap the attribute reference in an
2072 -- unchecked conversion to the type of the
2073 -- activation record component, to prevent
2074 -- spurious subtype conformance errors within
2075 -- instances.
2076
2077 if Is_Formal (Ent)
2078 and then not Is_Constrained (Etype (Ent))
2079 then
2080 -- Find target component and its type
2081
2082 Comp := First_Component (STJ.ARECnT);
2083 while Chars (Comp) /= Chars (Ent) loop
2084 Comp := Next_Component (Comp);
2085 end loop;
2086
2087 Rhs :=
2088 Unchecked_Convert_To (Etype (Comp), Rhs);
2089 end if;
2090
2091 Asn :=
2092 Make_Assignment_Statement (Loc,
2093 Name =>
2094 Make_Selected_Component (Loc,
2095 Prefix =>
2096 New_Occurrence_Of (STJ.ARECn, Loc),
2097 Selector_Name =>
2098 New_Occurrence_Of
2099 (Activation_Record_Component
2100 (Ent),
2101 Loc)),
2102 Expression => Rhs);
2103
2104 -- If we have a loop parameter, we have
2105 -- to insert before the first statement
2106 -- of the loop. Ins points to the
2107 -- N_Loop_Parameter_Specification or to
2108 -- an N_Iterator_Specification.
2109
2110 if Nkind_In
2111 (Ins, N_Iterator_Specification,
2112 N_Loop_Parameter_Specification)
2113 then
2114 -- Quantified expression are rewritten as
2115 -- loops during expansion.
2116
2117 if Nkind (Parent (Ins)) =
2118 N_Quantified_Expression
2119 then
2120 null;
2121
2122 else
2123 Ins :=
2124 First
2125 (Statements
2126 (Parent (Parent (Ins))));
2127 Insert_Before (Ins, Asn);
2128 end if;
2129
2130 else
2131 Insert_After (Ins, Asn);
2132 end if;
2133
2134 -- Analyze the assignment statement. We do
2135 -- not need to establish the relevant scope
2136 -- stack entries here, because we have
2137 -- already set the correct entity references,
2138 -- so no name resolution is required, and no
2139 -- new entities are created, so we don't even
2140 -- need to set the current scope.
2141
2142 -- We analyze with all checks suppressed
2143 -- (since we do not expect any exceptions).
2144
2145 Analyze (Asn, Suppress => All_Checks);
2146 end;
2147
2148 Next_Elmt (Elmt);
2149 end loop;
2150 end;
2151 end if;
2152 end;
2153 end if;
2154 end;
2155 end loop;
2156 end Subp_Loop;
2157
2158 -- Next step, process uplevel references. This has to be done in a
2159 -- separate pass, after completing the processing in Sub_Loop because we
2160 -- need all the AREC declarations generated, inserted, and analyzed so
2161 -- that the uplevel references can be successfully analyzed.
2162
2163 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
2164 declare
2165 UPJ : Uref_Entry renames Urefs.Table (J);
2166
2167 begin
2168 -- Ignore type references, these are implicit references that do
2169 -- not need rewriting (e.g. the appearence in a conversion).
2170 -- Also ignore if no reference was specified or if the rewriting
2171 -- has already been done (this can happen if the N_Identifier
2172 -- occurs more than one time in the tree).
2173
2174 if No (UPJ.Ref)
2175 or else not Is_Entity_Name (UPJ.Ref)
2176 or else not Present (Entity (UPJ.Ref))
2177 then
2178 goto Continue;
2179 end if;
2180
2181 -- Rewrite one reference
2182
2183 Rewrite_One_Ref : declare
2184 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
2185 -- Source location for the reference
2186
2187 Typ : constant Entity_Id := Etype (UPJ.Ent);
2188 -- The type of the referenced entity
2189
2190 Atyp : Entity_Id;
2191 -- The actual subtype of the reference
2192
2193 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
2194 -- Subp_Index for caller containing reference
2195
2196 STJR : Subp_Entry renames Subps.Table (RS_Caller);
2197 -- Subp_Entry for subprogram containing reference
2198
2199 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
2200 -- Subp_Index for subprogram containing referenced entity
2201
2202 STJE : Subp_Entry renames Subps.Table (RS_Callee);
2203 -- Subp_Entry for subprogram containing referenced entity
2204
2205 Pfx : Node_Id;
2206 Comp : Entity_Id;
2207 SI : SI_Type;
2208
2209 begin
2210 Atyp := Etype (UPJ.Ref);
2211
2212 if Ekind (Atyp) /= E_Record_Subtype then
2213 Atyp := Get_Actual_Subtype (UPJ.Ref);
2214 end if;
2215
2216 -- Ignore if no ARECnF entity for enclosing subprogram which
2217 -- probably happens as a result of not properly treating
2218 -- instance bodies. To be examined ???
2219
2220 -- If this test is omitted, then the compilation of freeze.adb
2221 -- and inline.adb fail in unnesting mode.
2222
2223 if No (STJR.ARECnF) then
2224 goto Continue;
2225 end if;
2226
2227 -- If this is a reference to a global constant, use its value
2228 -- rather than create a reference. It is more efficient and
2229 -- furthermore indispensable if the context requires a
2230 -- constant, such as a branch of a case statement.
2231
2232 if Ekind (UPJ.Ent) = E_Constant
2233 and then Is_True_Constant (UPJ.Ent)
2234 and then Present (Constant_Value (UPJ.Ent))
2235 and then Is_Static_Expression (Constant_Value (UPJ.Ent))
2236 then
2237 Rewrite (UPJ.Ref, New_Copy_Tree (Constant_Value (UPJ.Ent)));
2238 goto Continue;
2239 end if;
2240
2241 -- Push the current scope, so that the pointer type Tnn, and
2242 -- any subsidiary entities resulting from the analysis of the
2243 -- rewritten reference, go in the right entity chain.
2244
2245 Push_Scope (STJR.Ent);
2246
2247 -- Now we need to rewrite the reference. We have a reference
2248 -- from level STJR.Lev to level STJE.Lev. The general form of
2249 -- the rewritten reference for entity X is:
2250
2251 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
2252
2253 -- where a,b,c,d .. m =
2254 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
2255
2256 pragma Assert (STJR.Lev > STJE.Lev);
2257
2258 -- Compute the prefix of X. Here are examples to make things
2259 -- clear (with parens to show groupings, the prefix is
2260 -- everything except the .X at the end).
2261
2262 -- level 2 to level 1
2263
2264 -- AREC1F.X
2265
2266 -- level 3 to level 1
2267
2268 -- (AREC2F.AREC1U).X
2269
2270 -- level 4 to level 1
2271
2272 -- ((AREC3F.AREC2U).AREC1U).X
2273
2274 -- level 6 to level 2
2275
2276 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
2277
2278 -- In the above, ARECnF and ARECnU are pointers, so there are
2279 -- explicit dereferences required for these occurrences.
2280
2281 Pfx :=
2282 Make_Explicit_Dereference (Loc,
2283 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
2284 SI := RS_Caller;
2285 for L in STJE.Lev .. STJR.Lev - 2 loop
2286 SI := Enclosing_Subp (SI);
2287 Pfx :=
2288 Make_Explicit_Dereference (Loc,
2289 Prefix =>
2290 Make_Selected_Component (Loc,
2291 Prefix => Pfx,
2292 Selector_Name =>
2293 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
2294 end loop;
2295
2296 -- Get activation record component (must exist)
2297
2298 Comp := Activation_Record_Component (UPJ.Ent);
2299 pragma Assert (Present (Comp));
2300
2301 -- Do the replacement. If the component type is an access type,
2302 -- this is an uplevel reference for an entity that requires a
2303 -- fat pointer, so dereference the component.
2304
2305 if Is_Access_Type (Etype (Comp)) then
2306 Rewrite (UPJ.Ref,
2307 Make_Explicit_Dereference (Loc,
2308 Prefix =>
2309 Make_Selected_Component (Loc,
2310 Prefix => Pfx,
2311 Selector_Name =>
2312 New_Occurrence_Of (Comp, Loc))));
2313
2314 else
2315 Rewrite (UPJ.Ref,
2316 Make_Attribute_Reference (Loc,
2317 Prefix => New_Occurrence_Of (Atyp, Loc),
2318 Attribute_Name => Name_Deref,
2319 Expressions => New_List (
2320 Make_Selected_Component (Loc,
2321 Prefix => Pfx,
2322 Selector_Name =>
2323 New_Occurrence_Of (Comp, Loc)))));
2324 end if;
2325
2326 -- Analyze and resolve the new expression. We do not need to
2327 -- establish the relevant scope stack entries here, because we
2328 -- have already set all the correct entity references, so no
2329 -- name resolution is needed. We have already set the current
2330 -- scope, so that any new entities created will be in the right
2331 -- scope.
2332
2333 -- We analyze with all checks suppressed (since we do not
2334 -- expect any exceptions)
2335
2336 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
2337 Pop_Scope;
2338 end Rewrite_One_Ref;
2339 end;
2340
2341 <<Continue>>
2342 null;
2343 end loop Uplev_Refs;
2344
2345 -- Finally, loop through all calls adding extra actual for the
2346 -- activation record where it is required.
2347
2348 Adjust_Calls : for J in Calls.First .. Calls.Last loop
2349
2350 -- Process a single call, we are only interested in a call to a
2351 -- subprogram that actually needs a pointer to an activation record,
2352 -- as indicated by the ARECnF entity being set. This excludes the
2353 -- top level subprogram, and any subprogram not having uplevel refs.
2354
2355 Adjust_One_Call : declare
2356 CTJ : Call_Entry renames Calls.Table (J);
2357 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
2358 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
2359
2360 Loc : constant Source_Ptr := Sloc (CTJ.N);
2361
2362 Extra : Node_Id;
2363 ExtraP : Node_Id;
2364 SubX : SI_Type;
2365 Act : Node_Id;
2366
2367 begin
2368 if Present (STT.ARECnF)
2369 and then Nkind (CTJ.N) in N_Subprogram_Call
2370 then
2371 -- CTJ.N is a call to a subprogram which may require a pointer
2372 -- to an activation record. The subprogram containing the call
2373 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2374 -- have a call from level STF.Lev to level STT.Lev.
2375
2376 -- There are three possibilities:
2377
2378 -- For a call to the same level, we just pass the activation
2379 -- record passed to the calling subprogram.
2380
2381 if STF.Lev = STT.Lev then
2382 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2383
2384 -- For a call that goes down a level, we pass a pointer to the
2385 -- activation record constructed within the caller (which may
2386 -- be the outer-level subprogram, but also may be a more deeply
2387 -- nested caller).
2388
2389 elsif STT.Lev = STF.Lev + 1 then
2390 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
2391
2392 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2393 -- since it is not possible to do a downcall of more than
2394 -- one level.
2395
2396 -- For a call from level STF.Lev to level STT.Lev, we
2397 -- have to find the activation record needed by the
2398 -- callee. This is as follows:
2399
2400 -- ARECaF.ARECbU.ARECcU....ARECmU
2401
2402 -- where a,b,c .. m =
2403 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2404
2405 else
2406 pragma Assert (STT.Lev < STF.Lev);
2407
2408 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2409 SubX := Subp_Index (CTJ.Caller);
2410 for K in reverse STT.Lev .. STF.Lev - 1 loop
2411 SubX := Enclosing_Subp (SubX);
2412 Extra :=
2413 Make_Selected_Component (Loc,
2414 Prefix => Extra,
2415 Selector_Name =>
2416 New_Occurrence_Of
2417 (Subps.Table (SubX).ARECnU, Loc));
2418 end loop;
2419 end if;
2420
2421 -- Extra is the additional parameter to be added. Build a
2422 -- parameter association that we can append to the actuals.
2423
2424 ExtraP :=
2425 Make_Parameter_Association (Loc,
2426 Selector_Name =>
2427 New_Occurrence_Of (STT.ARECnF, Loc),
2428 Explicit_Actual_Parameter => Extra);
2429
2430 if No (Parameter_Associations (CTJ.N)) then
2431 Set_Parameter_Associations (CTJ.N, Empty_List);
2432 end if;
2433
2434 Append (ExtraP, Parameter_Associations (CTJ.N));
2435
2436 -- We need to deal with the actual parameter chain as well. The
2437 -- newly added parameter is always the last actual.
2438
2439 Act := First_Named_Actual (CTJ.N);
2440
2441 if No (Act) then
2442 Set_First_Named_Actual (CTJ.N, Extra);
2443
2444 -- If call has been relocated (as with an expression in
2445 -- an aggregate), set First_Named pointer in original node
2446 -- as well, because that's the parent of the parameter list.
2447
2448 Set_First_Named_Actual
2449 (Parent (List_Containing (ExtraP)), Extra);
2450
2451 -- Here we must follow the chain and append the new entry
2452
2453 else
2454 loop
2455 declare
2456 PAN : Node_Id;
2457 NNA : Node_Id;
2458
2459 begin
2460 PAN := Parent (Act);
2461 pragma Assert (Nkind (PAN) = N_Parameter_Association);
2462 NNA := Next_Named_Actual (PAN);
2463
2464 if No (NNA) then
2465 Set_Next_Named_Actual (PAN, Extra);
2466 exit;
2467 end if;
2468
2469 Act := NNA;
2470 end;
2471 end loop;
2472 end if;
2473
2474 -- Analyze and resolve the new actual. We do not need to
2475 -- establish the relevant scope stack entries here, because
2476 -- we have already set all the correct entity references, so
2477 -- no name resolution is needed.
2478
2479 -- We analyze with all checks suppressed (since we do not
2480 -- expect any exceptions, and also we temporarily turn off
2481 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2482 -- references (not needed at this stage, and in fact causes
2483 -- a bit of recursive chaos).
2484
2485 Opt.Unnest_Subprogram_Mode := False;
2486 Analyze_And_Resolve
2487 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
2488 Opt.Unnest_Subprogram_Mode := True;
2489 end if;
2490 end Adjust_One_Call;
2491 end loop Adjust_Calls;
2492
2493 return;
2494 end Unnest_Subprogram;
2495
2496 ------------------------
2497 -- Unnest_Subprograms --
2498 ------------------------
2499
2500 procedure Unnest_Subprograms (N : Node_Id) is
2501 function Search_Subprograms (N : Node_Id) return Traverse_Result;
2502 -- Tree visitor that search for outer level procedures with nested
2503 -- subprograms and invokes Unnest_Subprogram()
2504
2505 ---------------
2506 -- Do_Search --
2507 ---------------
2508
2509 procedure Do_Search is new Traverse_Proc (Search_Subprograms);
2510 -- Subtree visitor instantiation
2511
2512 ------------------------
2513 -- Search_Subprograms --
2514 ------------------------
2515
2516 function Search_Subprograms (N : Node_Id) return Traverse_Result is
2517 begin
2518 if Nkind_In (N, N_Subprogram_Body, N_Subprogram_Body_Stub) then
2519 declare
2520 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
2521
2522 begin
2523 -- We are only interested in subprograms (not generic
2524 -- subprograms), that have nested subprograms.
2525
2526 if Is_Subprogram (Spec_Id)
2527 and then Has_Nested_Subprogram (Spec_Id)
2528 and then Is_Library_Level_Entity (Spec_Id)
2529 then
2530 Unnest_Subprogram (Spec_Id, N);
2531 end if;
2532 end;
2533
2534 -- The proper body of a stub may contain nested subprograms, and
2535 -- therefore must be visited explicitly. Nested stubs are examined
2536 -- recursively in Visit_Node.
2537
2538 elsif Nkind (N) in N_Body_Stub then
2539 Do_Search (Library_Unit (N));
2540
2541 -- Skip generic packages
2542
2543 elsif Nkind (N) = N_Package_Body
2544 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
2545 then
2546 return Skip;
2547 end if;
2548
2549 return OK;
2550 end Search_Subprograms;
2551
2552 -- Start of processing for Unnest_Subprograms
2553
2554 begin
2555 if not Opt.Unnest_Subprogram_Mode or not Opt.Expander_Active then
2556 return;
2557 end if;
2558
2559 -- A specification will contain bodies if it contains instantiations so
2560 -- examine package or subprogram declaration of the main unit, when it
2561 -- is present.
2562
2563 if Nkind (Unit (N)) = N_Package_Body
2564 or else (Nkind (Unit (N)) = N_Subprogram_Body
2565 and then not Acts_As_Spec (N))
2566 then
2567 Do_Search (Library_Unit (N));
2568 end if;
2569
2570 Do_Search (N);
2571 end Unnest_Subprograms;
2572
2573 end Exp_Unst;