b81b1b97a2d4f64fe34a6a8effe125fe8bcd7037
[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 -- Conversion case
602
603 elsif Nkind (N) = N_Type_Conversion then
604 Note_Uplevel_Bound (Expression (N), Ref);
605 end if;
606 end Note_Uplevel_Bound;
607
608 -- Start of processing for Check_Static_Type
609
610 begin
611 -- If already marked static, immediate return
612
613 if Is_Static_Type (T) and then not Check_Designated then
614 return;
615 end if;
616
617 -- If the type is at library level, always consider it static,
618 -- since such uplevel references are irrelevant.
619
620 if Is_Library_Level_Entity (T) then
621 Set_Is_Static_Type (T);
622 return;
623 end if;
624
625 -- Otherwise figure out what the story is with this type
626
627 -- For a scalar type, check bounds
628
629 if Is_Scalar_Type (T) then
630
631 -- If both bounds static, then this is a static type
632
633 declare
634 LB : constant Node_Id := Type_Low_Bound (T);
635 UB : constant Node_Id := Type_High_Bound (T);
636
637 begin
638 if not Is_Static_Expression (LB) then
639 Note_Uplevel_Bound (LB, N);
640 DT := True;
641 end if;
642
643 if not Is_Static_Expression (UB) then
644 Note_Uplevel_Bound (UB, N);
645 DT := True;
646 end if;
647 end;
648
649 -- For record type, check all components and discriminant
650 -- constraints if present.
651
652 elsif Is_Record_Type (T) then
653 declare
654 C : Entity_Id;
655 D : Elmt_Id;
656
657 begin
658 C := First_Component_Or_Discriminant (T);
659 while Present (C) loop
660 Check_Static_Type (Etype (C), N, DT);
661 Next_Component_Or_Discriminant (C);
662 end loop;
663
664 if Has_Discriminants (T)
665 and then Present (Discriminant_Constraint (T))
666 then
667 D := First_Elmt (Discriminant_Constraint (T));
668 while Present (D) loop
669 if not Is_Static_Expression (Node (D)) then
670 Note_Uplevel_Bound (Node (D), N);
671 DT := True;
672 end if;
673
674 Next_Elmt (D);
675 end loop;
676 end if;
677 end;
678
679 -- For array type, check index types and component type
680
681 elsif Is_Array_Type (T) then
682 declare
683 IX : Node_Id;
684 begin
685 Check_Static_Type (Component_Type (T), N, DT);
686
687 IX := First_Index (T);
688 while Present (IX) loop
689 Check_Static_Type (Etype (IX), N, DT);
690 Next_Index (IX);
691 end loop;
692 end;
693
694 -- For private type, examine whether full view is static
695
696 elsif Is_Incomplete_Or_Private_Type (T)
697 and then Present (Full_View (T))
698 then
699 Check_Static_Type (Full_View (T), N, DT, Check_Designated);
700
701 if Is_Static_Type (Full_View (T)) then
702 Set_Is_Static_Type (T);
703 end if;
704
705 -- For access types, check designated type when required
706
707 elsif Is_Access_Type (T) and then Check_Designated then
708 Check_Static_Type (Directly_Designated_Type (T), N, DT);
709
710 -- For now, ignore other types
711
712 else
713 return;
714 end if;
715
716 if not DT then
717 Set_Is_Static_Type (T);
718 end if;
719 end Check_Static_Type;
720
721 ----------------------
722 -- Note_Uplevel_Ref --
723 ----------------------
724
725 procedure Note_Uplevel_Ref
726 (E : Entity_Id;
727 N : Node_Id;
728 Caller : Entity_Id;
729 Callee : Entity_Id)
730 is
731 Full_E : Entity_Id := E;
732 begin
733 -- Nothing to do for static type
734
735 if Is_Static_Type (E) then
736 return;
737 end if;
738
739 -- Nothing to do if Caller and Callee are the same
740
741 if Caller = Callee then
742 return;
743
744 -- Callee may be a function that returns an array, and that has
745 -- been rewritten as a procedure. If caller is that procedure,
746 -- nothing to do either.
747
748 elsif Ekind (Callee) = E_Function
749 and then Rewritten_For_C (Callee)
750 and then Corresponding_Procedure (Callee) = Caller
751 then
752 return;
753
754 elsif Ekind_In (Callee, E_Entry, E_Entry_Family) then
755 return;
756 end if;
757
758 -- We have a new uplevel referenced entity
759
760 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
761 Full_E := Full_View (E);
762 end if;
763
764 -- All we do at this stage is to add the uplevel reference to
765 -- the table. It's too early to do anything else, since this
766 -- uplevel reference may come from an unreachable subprogram
767 -- in which case the entry will be deleted.
768
769 Urefs.Append ((N, Full_E, Caller, Callee));
770 end Note_Uplevel_Ref;
771
772 -------------------------
773 -- Register_Subprogram --
774 -------------------------
775
776 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id) is
777 L : constant Nat := Get_Level (Subp, E);
778
779 begin
780 -- Subprograms declared in tasks and protected types cannot be
781 -- eliminated because calls to them may be in other units, so
782 -- they must be treated as reachable.
783
784 Subps.Append
785 ((Ent => E,
786 Bod => Bod,
787 Lev => L,
788 Reachable => In_Synchronized_Unit (E)
789 or else Address_Taken (E),
790 Uplevel_Ref => L,
791 Declares_AREC => False,
792 Uents => No_Elist,
793 Last => 0,
794 ARECnF => Empty,
795 ARECn => Empty,
796 ARECnT => Empty,
797 ARECnPT => Empty,
798 ARECnP => Empty,
799 ARECnU => Empty));
800
801 Set_Subps_Index (E, UI_From_Int (Subps.Last));
802
803 -- If we marked this reachable because it's in a synchronized
804 -- unit, we have to mark all enclosing subprograms as reachable
805 -- as well.
806
807 if In_Synchronized_Unit (E) then
808 declare
809 S : Entity_Id := E;
810
811 begin
812 for J in reverse 1 .. L - 1 loop
813 S := Enclosing_Subprogram (S);
814 Subps.Table (Subp_Index (S)).Reachable := True;
815 end loop;
816 end;
817 end if;
818 end Register_Subprogram;
819
820 -- Start of processing for Visit_Node
821
822 begin
823 case Nkind (N) is
824
825 -- Record a subprogram call
826
827 when N_Function_Call
828 | N_Procedure_Call_Statement
829 =>
830 -- We are only interested in direct calls, not indirect
831 -- calls (where Name (N) is an explicit dereference) at
832 -- least for now!
833
834 if Nkind (Name (N)) in N_Has_Entity then
835 Ent := Entity (Name (N));
836
837 -- We are only interested in calls to subprograms nested
838 -- within Subp. Calls to Subp itself or to subprograms
839 -- outside the nested structure do not affect us.
840
841 if Scope_Within (Ent, Subp)
842 and then Is_Subprogram (Ent)
843 and then not Is_Imported (Ent)
844 then
845 Append_Unique_Call ((N, Current_Subprogram, Ent));
846 end if;
847 end if;
848
849 -- For all calls where the formal is an unconstrained array
850 -- and the actual is constrained we need to check the bounds
851 -- for uplevel references.
852
853 declare
854 Actual : Entity_Id;
855 DT : Boolean := False;
856 Formal : Node_Id;
857 Subp : Entity_Id;
858
859 begin
860 if Nkind (Name (N)) = N_Explicit_Dereference then
861 Subp := Etype (Name (N));
862 else
863 Subp := Entity (Name (N));
864 end if;
865
866 Actual := First_Actual (N);
867 Formal := First_Formal_With_Extras (Subp);
868 while Present (Actual) loop
869 if Is_Array_Type (Etype (Formal))
870 and then not Is_Constrained (Etype (Formal))
871 and then Is_Constrained (Etype (Actual))
872 then
873 Check_Static_Type (Etype (Actual), Empty, DT);
874 end if;
875
876 Next_Actual (Actual);
877 Next_Formal_With_Extras (Formal);
878 end loop;
879 end;
880
881 -- An At_End_Proc in a statement sequence indicates that there
882 -- is a call from the enclosing construct or block to that
883 -- subprogram. As above, the called entity must be local and
884 -- not imported.
885
886 when N_Handled_Sequence_Of_Statements =>
887 if Present (At_End_Proc (N))
888 and then Scope_Within (Entity (At_End_Proc (N)), Subp)
889 and then not Is_Imported (Entity (At_End_Proc (N)))
890 then
891 Append_Unique_Call
892 ((N, Current_Subprogram, Entity (At_End_Proc (N))));
893 end if;
894
895 -- Similarly, the following constructs include a semantic
896 -- attribute Procedure_To_Call that must be handled like
897 -- other calls. Likewise for attribute Storage_Pool.
898
899 when N_Allocator
900 | N_Extended_Return_Statement
901 | N_Free_Statement
902 | N_Simple_Return_Statement
903 =>
904 declare
905 Pool : constant Entity_Id := Storage_Pool (N);
906 Proc : constant Entity_Id := Procedure_To_Call (N);
907
908 begin
909 if Present (Proc)
910 and then Scope_Within (Proc, Subp)
911 and then not Is_Imported (Proc)
912 then
913 Append_Unique_Call ((N, Current_Subprogram, Proc));
914 end if;
915
916 if Present (Pool)
917 and then not Is_Library_Level_Entity (Pool)
918 and then Scope_Within_Or_Same (Scope (Pool), Subp)
919 then
920 Caller := Current_Subprogram;
921 Callee := Enclosing_Subprogram (Pool);
922
923 if Callee /= Caller then
924 Note_Uplevel_Ref (Pool, Empty, Caller, Callee);
925 end if;
926 end if;
927 end;
928
929 -- For an allocator with a qualified expression, check type
930 -- of expression being qualified. The explicit type name is
931 -- handled as an entity reference.
932
933 if Nkind (N) = N_Allocator
934 and then Nkind (Expression (N)) = N_Qualified_Expression
935 then
936 declare
937 DT : Boolean := False;
938 begin
939 Check_Static_Type
940 (Etype (Expression (Expression (N))), Empty, DT);
941 end;
942
943 -- For a Return or Free (all other nodes we handle here),
944 -- we usually need the size of the object, so we need to be
945 -- sure that any nonstatic bounds of the expression's type
946 -- that are uplevel are handled.
947
948 elsif Nkind (N) /= N_Allocator
949 and then Present (Expression (N))
950 then
951 declare
952 DT : Boolean := False;
953 begin
954 Check_Static_Type
955 (Etype (Expression (N)),
956 Empty,
957 DT,
958 Check_Designated => Nkind (N) = N_Free_Statement);
959 end;
960 end if;
961
962 -- A 'Access reference is a (potential) call. So is 'Address,
963 -- in particular on imported subprograms. Other attributes
964 -- require special handling.
965
966 when N_Attribute_Reference =>
967 declare
968 Attr : constant Attribute_Id :=
969 Get_Attribute_Id (Attribute_Name (N));
970 begin
971 case Attr is
972 when Attribute_Access
973 | Attribute_Unchecked_Access
974 | Attribute_Unrestricted_Access
975 | Attribute_Address
976 =>
977 if Nkind (Prefix (N)) in N_Has_Entity then
978 Ent := Entity (Prefix (N));
979
980 -- We only need to examine calls to subprograms
981 -- nested within current Subp.
982
983 if Scope_Within (Ent, Subp) then
984 if Is_Imported (Ent) then
985 null;
986
987 elsif Is_Subprogram (Ent) then
988 Append_Unique_Call
989 ((N, Current_Subprogram, Ent));
990 end if;
991 end if;
992 end if;
993
994 -- References to bounds can be uplevel references if
995 -- the type isn't static.
996
997 when Attribute_First
998 | Attribute_Last
999 | Attribute_Length
1000 =>
1001 -- Special-case attributes of objects whose bounds
1002 -- may be uplevel references. More complex prefixes
1003 -- handled during full traversal. Note that if the
1004 -- nominal subtype of the prefix is unconstrained,
1005 -- the bound must be obtained from the object, not
1006 -- from the (possibly) uplevel reference.
1007
1008 if Is_Constrained (Etype (Prefix (N))) then
1009 declare
1010 DT : Boolean := False;
1011 begin
1012 Check_Static_Type
1013 (Etype (Prefix (N)), Empty, DT);
1014 end;
1015
1016 return OK;
1017 end if;
1018
1019 when others =>
1020 null;
1021 end case;
1022 end;
1023
1024 -- Component associations in aggregates are either static or
1025 -- else the aggregate will be expanded into assignments, in
1026 -- which case the expression is analyzed later and provides
1027 -- no relevant code generation.
1028
1029 when N_Component_Association =>
1030 if No (Expression (N))
1031 or else No (Etype (Expression (N)))
1032 then
1033 return Skip;
1034 end if;
1035
1036 -- Generic associations are not analyzed: the actuals are
1037 -- transferred to renaming and subtype declarations that
1038 -- are the ones that must be examined.
1039
1040 when N_Generic_Association =>
1041 return Skip;
1042
1043 -- Indexed references can be uplevel if the type isn't static
1044 -- and if the lower bound (or an inner bound for a multi-
1045 -- dimensional array) is uplevel.
1046
1047 when N_Indexed_Component
1048 | N_Slice
1049 =>
1050 if Is_Constrained (Etype (Prefix (N))) then
1051 declare
1052 DT : Boolean := False;
1053 begin
1054 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
1055 end;
1056 end if;
1057
1058 -- A selected component can have an implicit up-level
1059 -- reference due to the bounds of previous fields in the
1060 -- record. We simplify the processing here by examining
1061 -- all components of the record.
1062
1063 -- Selected components appear as unit names and end labels
1064 -- for child units. Prefixes of these nodes denote parent
1065 -- units and carry no type information so they are skipped.
1066
1067 when N_Selected_Component =>
1068 if Present (Etype (Prefix (N))) then
1069 declare
1070 DT : Boolean := False;
1071 begin
1072 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
1073 end;
1074 end if;
1075
1076 -- For EQ/NE comparisons, we need the type of the operands
1077 -- in order to do the comparison, which means we need the
1078 -- bounds.
1079
1080 when N_Op_Eq
1081 | N_Op_Ne
1082 =>
1083 declare
1084 DT : Boolean := False;
1085 begin
1086 Check_Static_Type (Etype (Left_Opnd (N)), Empty, DT);
1087 Check_Static_Type (Etype (Right_Opnd (N)), Empty, DT);
1088 end;
1089
1090 -- Likewise we need the sizes to compute how much to move in
1091 -- an assignment.
1092
1093 when N_Assignment_Statement =>
1094 declare
1095 DT : Boolean := False;
1096 begin
1097 Check_Static_Type (Etype (Name (N)), Empty, DT);
1098 Check_Static_Type (Etype (Expression (N)), Empty, DT);
1099 end;
1100
1101 -- Record a subprogram. We record a subprogram body that acts
1102 -- as a spec. Otherwise we record a subprogram declaration,
1103 -- providing that it has a corresponding body we can get hold
1104 -- of. The case of no corresponding body being available is
1105 -- ignored for now.
1106
1107 when N_Subprogram_Body =>
1108 Ent := Unique_Defining_Entity (N);
1109
1110 -- Ignore generic subprogram
1111
1112 if Is_Generic_Subprogram (Ent) then
1113 return Skip;
1114 end if;
1115
1116 -- Make new entry in subprogram table if not already made
1117
1118 Register_Subprogram (Ent, N);
1119
1120 -- We make a recursive call to scan the subprogram body, so
1121 -- that we can save and restore Current_Subprogram.
1122
1123 declare
1124 Save_CS : constant Entity_Id := Current_Subprogram;
1125 Decl : Node_Id;
1126
1127 begin
1128 Current_Subprogram := Ent;
1129
1130 -- Scan declarations
1131
1132 Decl := First (Declarations (N));
1133 while Present (Decl) loop
1134 Visit (Decl);
1135 Next (Decl);
1136 end loop;
1137
1138 -- Scan statements
1139
1140 Visit (Handled_Statement_Sequence (N));
1141
1142 -- Restore current subprogram setting
1143
1144 Current_Subprogram := Save_CS;
1145 end;
1146
1147 -- Now at this level, return skipping the subprogram body
1148 -- descendants, since we already took care of them!
1149
1150 return Skip;
1151
1152 -- If we have a body stub, visit the associated subunit, which
1153 -- is a semantic descendant of the stub.
1154
1155 when N_Body_Stub =>
1156 Visit (Library_Unit (N));
1157
1158 -- A declaration of a wrapper package indicates a subprogram
1159 -- instance for which there is no explicit body. Enter the
1160 -- subprogram instance in the table.
1161
1162 when N_Package_Declaration =>
1163 if Is_Wrapper_Package (Defining_Entity (N)) then
1164 Register_Subprogram
1165 (Related_Instance (Defining_Entity (N)), Empty);
1166 end if;
1167
1168 -- Skip generic declarations
1169
1170 when N_Generic_Declaration =>
1171 return Skip;
1172
1173 -- Skip generic package body
1174
1175 when N_Package_Body =>
1176 if Present (Corresponding_Spec (N))
1177 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
1178 then
1179 return Skip;
1180 end if;
1181
1182 -- Pragmas and component declarations are ignored. Quantified
1183 -- expressions are expanded into explicit loops and the
1184 -- original epression must be ignored.
1185
1186 when N_Component_Declaration
1187 | N_Pragma
1188 | N_Quantified_Expression
1189 =>
1190 return Skip;
1191
1192 -- We want to skip the function spec for a generic function
1193 -- to avoid looking at any generic types that might be in
1194 -- its formals.
1195
1196 when N_Function_Specification =>
1197 if Is_Generic_Subprogram (Unique_Defining_Entity (N)) then
1198 return Skip;
1199 end if;
1200
1201 -- Otherwise record an uplevel reference in a local identifier
1202
1203 when others =>
1204 if Nkind (N) in N_Has_Entity
1205 and then Present (Entity (N))
1206 then
1207 Ent := Entity (N);
1208
1209 -- Only interested in entities declared within our nest
1210
1211 if not Is_Library_Level_Entity (Ent)
1212 and then Scope_Within_Or_Same (Scope (Ent), Subp)
1213
1214 -- Skip entities defined in inlined subprograms
1215
1216 and then
1217 Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
1218
1219 -- Constants and variables are potentially uplevel
1220 -- references to global declarations.
1221
1222 and then
1223 (Ekind_In (Ent, E_Constant,
1224 E_Loop_Parameter,
1225 E_Variable)
1226
1227 -- Formals are interesting, but not if being used
1228 -- as mere names of parameters for name notation
1229 -- calls.
1230
1231 or else
1232 (Is_Formal (Ent)
1233 and then not
1234 (Nkind (Parent (N)) = N_Parameter_Association
1235 and then Selector_Name (Parent (N)) = N))
1236
1237 -- Types other than known Is_Static types are
1238 -- potentially interesting.
1239
1240 or else
1241 (Is_Type (Ent) and then not Is_Static_Type (Ent)))
1242 then
1243 -- Here we have a potentially interesting uplevel
1244 -- reference to examine.
1245
1246 if Is_Type (Ent) then
1247 declare
1248 DT : Boolean := False;
1249
1250 begin
1251 Check_Static_Type (Ent, N, DT);
1252 return OK;
1253 end;
1254 end if;
1255
1256 Caller := Current_Subprogram;
1257 Callee := Enclosing_Subprogram (Ent);
1258
1259 if Callee /= Caller
1260 and then (not Is_Static_Type (Ent)
1261 or else Needs_Fat_Pointer (Ent))
1262 then
1263 Note_Uplevel_Ref (Ent, N, Caller, Callee);
1264
1265 -- Check the type of a formal parameter of the current
1266 -- subprogram, whose formal type may be an uplevel
1267 -- reference.
1268
1269 elsif Is_Formal (Ent)
1270 and then Scope (Ent) = Current_Subprogram
1271 then
1272 declare
1273 DT : Boolean := False;
1274
1275 begin
1276 Check_Static_Type (Etype (Ent), Empty, DT);
1277 end;
1278 end if;
1279 end if;
1280 end if;
1281 end case;
1282
1283 -- Fall through to continue scanning children of this node
1284
1285 return OK;
1286 end Visit_Node;
1287
1288 -- Start of processing for Build_Tables
1289
1290 begin
1291 -- Traverse the body to get subprograms, calls and uplevel references
1292
1293 Visit (Subp_Body);
1294 end Build_Tables;
1295
1296 -- Now do the first transitive closure which determines which
1297 -- subprograms in the nest are actually reachable.
1298
1299 Reachable_Closure : declare
1300 Modified : Boolean;
1301
1302 begin
1303 Subps.Table (Subps_First).Reachable := True;
1304
1305 -- We use a simple minded algorithm as follows (obviously this can
1306 -- be done more efficiently, using one of the standard algorithms
1307 -- for efficient transitive closure computation, but this is simple
1308 -- and most likely fast enough that its speed does not matter).
1309
1310 -- Repeatedly scan the list of calls. Any time we find a call from
1311 -- A to B, where A is reachable, but B is not, then B is reachable,
1312 -- and note that we have made a change by setting Modified True. We
1313 -- repeat this until we make a pass with no modifications.
1314
1315 Outer : loop
1316 Modified := False;
1317 Inner : for J in Calls.First .. Calls.Last loop
1318 declare
1319 CTJ : Call_Entry renames Calls.Table (J);
1320
1321 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1322 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1323
1324 SUBF : Subp_Entry renames Subps.Table (SINF);
1325 SUBT : Subp_Entry renames Subps.Table (SINT);
1326
1327 begin
1328 if SUBF.Reachable and then not SUBT.Reachable then
1329 SUBT.Reachable := True;
1330 Modified := True;
1331 end if;
1332 end;
1333 end loop Inner;
1334
1335 exit Outer when not Modified;
1336 end loop Outer;
1337 end Reachable_Closure;
1338
1339 -- Remove calls from unreachable subprograms
1340
1341 declare
1342 New_Index : Nat;
1343
1344 begin
1345 New_Index := 0;
1346 for J in Calls.First .. Calls.Last loop
1347 declare
1348 CTJ : Call_Entry renames Calls.Table (J);
1349
1350 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1351 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1352
1353 SUBF : Subp_Entry renames Subps.Table (SINF);
1354 SUBT : Subp_Entry renames Subps.Table (SINT);
1355
1356 begin
1357 if SUBF.Reachable then
1358 pragma Assert (SUBT.Reachable);
1359 New_Index := New_Index + 1;
1360 Calls.Table (New_Index) := Calls.Table (J);
1361 end if;
1362 end;
1363 end loop;
1364
1365 Calls.Set_Last (New_Index);
1366 end;
1367
1368 -- Remove uplevel references from unreachable subprograms
1369
1370 declare
1371 New_Index : Nat;
1372
1373 begin
1374 New_Index := 0;
1375 for J in Urefs.First .. Urefs.Last loop
1376 declare
1377 URJ : Uref_Entry renames Urefs.Table (J);
1378
1379 SINF : constant SI_Type := Subp_Index (URJ.Caller);
1380 SINT : constant SI_Type := Subp_Index (URJ.Callee);
1381
1382 SUBF : Subp_Entry renames Subps.Table (SINF);
1383 SUBT : Subp_Entry renames Subps.Table (SINT);
1384
1385 S : Entity_Id;
1386
1387 begin
1388 -- Keep reachable reference
1389
1390 if SUBF.Reachable then
1391 New_Index := New_Index + 1;
1392 Urefs.Table (New_Index) := Urefs.Table (J);
1393
1394 -- And since we know we are keeping this one, this is a good
1395 -- place to fill in information for a good reference.
1396
1397 -- Mark all enclosing subprograms need to declare AREC
1398
1399 S := URJ.Caller;
1400 loop
1401 S := Enclosing_Subprogram (S);
1402
1403 -- If we are at the top level, as can happen with
1404 -- references to formals in aspects of nested subprogram
1405 -- declarations, there are no further subprograms to mark
1406 -- as requiring activation records.
1407
1408 exit when No (S);
1409
1410 declare
1411 SUBI : Subp_Entry renames Subps.Table (Subp_Index (S));
1412 begin
1413 SUBI.Declares_AREC := True;
1414
1415 -- If this entity was marked reachable because it is
1416 -- in a task or protected type, there may not appear
1417 -- to be any calls to it, which would normally adjust
1418 -- the levels of the parent subprograms. So we need to
1419 -- be sure that the uplevel reference of that entity
1420 -- takes into account possible calls.
1421
1422 if In_Synchronized_Unit (SUBF.Ent)
1423 and then SUBT.Lev < SUBI.Uplevel_Ref
1424 then
1425 SUBI.Uplevel_Ref := SUBT.Lev;
1426 end if;
1427 end;
1428
1429 exit when S = URJ.Callee;
1430 end loop;
1431
1432 -- Add to list of uplevel referenced entities for Callee.
1433 -- We do not add types to this list, only actual references
1434 -- to objects that will be referenced uplevel, and we use
1435 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1436 -- duplicate entries in the list. Discriminants are also
1437 -- excluded, only the enclosing object can appear in the
1438 -- list.
1439
1440 if not Is_Uplevel_Referenced_Entity (URJ.Ent)
1441 and then Ekind (URJ.Ent) /= E_Discriminant
1442 then
1443 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
1444 Append_New_Elmt (URJ.Ent, SUBT.Uents);
1445 end if;
1446
1447 -- And set uplevel indication for caller
1448
1449 if SUBT.Lev < SUBF.Uplevel_Ref then
1450 SUBF.Uplevel_Ref := SUBT.Lev;
1451 end if;
1452 end if;
1453 end;
1454 end loop;
1455
1456 Urefs.Set_Last (New_Index);
1457 end;
1458
1459 -- Remove unreachable subprograms from Subps table. Note that we do
1460 -- this after eliminating entries from the other two tables, since
1461 -- those elimination steps depend on referencing the Subps table.
1462
1463 declare
1464 New_SI : SI_Type;
1465
1466 begin
1467 New_SI := Subps_First - 1;
1468 for J in Subps_First .. Subps.Last loop
1469 declare
1470 STJ : Subp_Entry renames Subps.Table (J);
1471 Spec : Node_Id;
1472 Decl : Node_Id;
1473
1474 begin
1475 -- Subprogram is reachable, copy and reset index
1476
1477 if STJ.Reachable then
1478 New_SI := New_SI + 1;
1479 Subps.Table (New_SI) := STJ;
1480 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
1481
1482 -- Subprogram is not reachable
1483
1484 else
1485 -- Clear index, since no longer active
1486
1487 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
1488
1489 -- Output debug information if -gnatd.3 set
1490
1491 if Debug_Flag_Dot_3 then
1492 Write_Str ("Eliminate ");
1493 Write_Name (Chars (Subps.Table (J).Ent));
1494 Write_Str (" at ");
1495 Write_Location (Sloc (Subps.Table (J).Ent));
1496 Write_Str (" (not referenced)");
1497 Write_Eol;
1498 end if;
1499
1500 -- Rewrite declaration, body, and corresponding freeze node
1501 -- to null statements.
1502
1503 -- A subprogram instantiation does not have an explicit
1504 -- body. If unused, we could remove the corresponding
1505 -- wrapper package and its body (TBD).
1506
1507 if Present (STJ.Bod) then
1508 Spec := Corresponding_Spec (STJ.Bod);
1509
1510 if Present (Spec) then
1511 Decl := Parent (Declaration_Node (Spec));
1512 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
1513
1514 if Present (Freeze_Node (Spec)) then
1515 Rewrite (Freeze_Node (Spec),
1516 Make_Null_Statement (Sloc (Decl)));
1517 end if;
1518 end if;
1519
1520 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
1521 end if;
1522 end if;
1523 end;
1524 end loop;
1525
1526 Subps.Set_Last (New_SI);
1527 end;
1528
1529 -- Now it is time for the second transitive closure, which follows calls
1530 -- and makes sure that A calls B, and B has uplevel references, then A
1531 -- is also marked as having uplevel references.
1532
1533 Closure_Uplevel : declare
1534 Modified : Boolean;
1535
1536 begin
1537 -- We use a simple minded algorithm as follows (obviously this can
1538 -- be done more efficiently, using one of the standard algorithms
1539 -- for efficient transitive closure computation, but this is simple
1540 -- and most likely fast enough that its speed does not matter).
1541
1542 -- Repeatedly scan the list of calls. Any time we find a call from
1543 -- A to B, where B has uplevel references, make sure that A is marked
1544 -- as having at least the same level of uplevel referencing.
1545
1546 Outer2 : loop
1547 Modified := False;
1548 Inner2 : for J in Calls.First .. Calls.Last loop
1549 declare
1550 CTJ : Call_Entry renames Calls.Table (J);
1551 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1552 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1553 SUBF : Subp_Entry renames Subps.Table (SINF);
1554 SUBT : Subp_Entry renames Subps.Table (SINT);
1555 begin
1556 if SUBT.Lev > SUBT.Uplevel_Ref
1557 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
1558 then
1559 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
1560 Modified := True;
1561 end if;
1562 end;
1563 end loop Inner2;
1564
1565 exit Outer2 when not Modified;
1566 end loop Outer2;
1567 end Closure_Uplevel;
1568
1569 -- We have one more step before the tables are complete. An uplevel
1570 -- call from subprogram A to subprogram B where subprogram B has uplevel
1571 -- references is in effect an uplevel reference, and must arrange for
1572 -- the proper activation link to be passed.
1573
1574 for J in Calls.First .. Calls.Last loop
1575 declare
1576 CTJ : Call_Entry renames Calls.Table (J);
1577
1578 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1579 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1580
1581 SUBF : Subp_Entry renames Subps.Table (SINF);
1582 SUBT : Subp_Entry renames Subps.Table (SINT);
1583
1584 A : Entity_Id;
1585
1586 begin
1587 -- If callee has uplevel references
1588
1589 if SUBT.Uplevel_Ref < SUBT.Lev
1590
1591 -- And this is an uplevel call
1592
1593 and then SUBT.Lev < SUBF.Lev
1594 then
1595 -- We need to arrange for finding the uplink
1596
1597 A := CTJ.Caller;
1598 loop
1599 A := Enclosing_Subprogram (A);
1600 Subps.Table (Subp_Index (A)).Declares_AREC := True;
1601 exit when A = CTJ.Callee;
1602
1603 -- In any case exit when we get to the outer level. This
1604 -- happens in some odd cases with generics (in particular
1605 -- sem_ch3.adb does not compile without this kludge ???).
1606
1607 exit when A = Subp;
1608 end loop;
1609 end if;
1610 end;
1611 end loop;
1612
1613 -- The tables are now complete, so we can record the last index in the
1614 -- Subps table for later reference in Cprint.
1615
1616 Subps.Table (Subps_First).Last := Subps.Last;
1617
1618 -- Next step, create the entities for code we will insert. We do this
1619 -- at the start so that all the entities are defined, regardless of the
1620 -- order in which we do the code insertions.
1621
1622 Create_Entities : for J in Subps_First .. Subps.Last loop
1623 declare
1624 STJ : Subp_Entry renames Subps.Table (J);
1625 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1626
1627 begin
1628 -- First we create the ARECnF entity for the additional formal for
1629 -- all subprograms which need an activation record passed.
1630
1631 if STJ.Uplevel_Ref < STJ.Lev then
1632 STJ.ARECnF :=
1633 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
1634 end if;
1635
1636 -- Define the AREC entities for the activation record if needed
1637
1638 if STJ.Declares_AREC then
1639 STJ.ARECn :=
1640 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
1641 STJ.ARECnT :=
1642 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
1643 STJ.ARECnPT :=
1644 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
1645 STJ.ARECnP :=
1646 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
1647
1648 -- Define uplink component entity if inner nesting case
1649
1650 if Present (STJ.ARECnF) then
1651 STJ.ARECnU :=
1652 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
1653 end if;
1654 end if;
1655 end;
1656 end loop Create_Entities;
1657
1658 -- Loop through subprograms
1659
1660 Subp_Loop : declare
1661 Addr : Entity_Id := Empty;
1662
1663 begin
1664 for J in Subps_First .. Subps.Last loop
1665 declare
1666 STJ : Subp_Entry renames Subps.Table (J);
1667
1668 begin
1669 -- First add the extra formal if needed. This applies to all
1670 -- nested subprograms that require an activation record to be
1671 -- passed, as indicated by ARECnF being defined.
1672
1673 if Present (STJ.ARECnF) then
1674
1675 -- Here we need the extra formal. We do the expansion and
1676 -- analysis of this manually, since it is fairly simple,
1677 -- and it is not obvious how we can get what we want if we
1678 -- try to use the normal Analyze circuit.
1679
1680 Add_Extra_Formal : declare
1681 Encl : constant SI_Type := Enclosing_Subp (J);
1682 STJE : Subp_Entry renames Subps.Table (Encl);
1683 -- Index and Subp_Entry for enclosing routine
1684
1685 Form : constant Entity_Id := STJ.ARECnF;
1686 -- The formal to be added. Note that n here is one less
1687 -- than the level of the subprogram itself (STJ.Ent).
1688
1689 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1690 -- S is an N_Function/Procedure_Specification node, and F
1691 -- is the new entity to add to this subprogramn spec as
1692 -- the last Extra_Formal.
1693
1694 ----------------------
1695 -- Add_Form_To_Spec --
1696 ----------------------
1697
1698 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1699 Sub : constant Entity_Id := Defining_Entity (S);
1700 Ent : Entity_Id;
1701
1702 begin
1703 -- Case of at least one Extra_Formal is present, set
1704 -- ARECnF as the new last entry in the list.
1705
1706 if Present (Extra_Formals (Sub)) then
1707 Ent := Extra_Formals (Sub);
1708 while Present (Extra_Formal (Ent)) loop
1709 Ent := Extra_Formal (Ent);
1710 end loop;
1711
1712 Set_Extra_Formal (Ent, F);
1713
1714 -- No Extra formals present
1715
1716 else
1717 Set_Extra_Formals (Sub, F);
1718 Ent := Last_Formal (Sub);
1719
1720 if Present (Ent) then
1721 Set_Extra_Formal (Ent, F);
1722 end if;
1723 end if;
1724 end Add_Form_To_Spec;
1725
1726 -- Start of processing for Add_Extra_Formal
1727
1728 begin
1729 -- Decorate the new formal entity
1730
1731 Set_Scope (Form, STJ.Ent);
1732 Set_Ekind (Form, E_In_Parameter);
1733 Set_Etype (Form, STJE.ARECnPT);
1734 Set_Mechanism (Form, By_Copy);
1735 Set_Never_Set_In_Source (Form, True);
1736 Set_Analyzed (Form, True);
1737 Set_Comes_From_Source (Form, False);
1738 Set_Is_Activation_Record (Form, True);
1739
1740 -- Case of only body present
1741
1742 if Acts_As_Spec (STJ.Bod) then
1743 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1744
1745 -- Case of separate spec
1746
1747 else
1748 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1749 end if;
1750 end Add_Extra_Formal;
1751 end if;
1752
1753 -- Processing for subprograms that declare an activation record
1754
1755 if Present (STJ.ARECn) then
1756
1757 -- Local declarations for one such subprogram
1758
1759 declare
1760 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1761
1762 Decls : constant List_Id := New_List;
1763 -- List of new declarations we create
1764
1765 Clist : List_Id;
1766 Comp : Entity_Id;
1767
1768 Decl_Assign : Node_Id;
1769 -- Assigment to set uplink, Empty if none
1770
1771 Decl_ARECnT : Node_Id;
1772 Decl_ARECnPT : Node_Id;
1773 Decl_ARECn : Node_Id;
1774 Decl_ARECnP : Node_Id;
1775 -- Declaration nodes for the AREC entities we build
1776
1777 begin
1778 -- Build list of component declarations for ARECnT and
1779 -- load System.Address.
1780
1781 Clist := Empty_List;
1782
1783 if No (Addr) then
1784 Addr := RTE (RE_Address);
1785 end if;
1786
1787 -- If we are in a subprogram that has a static link that
1788 -- is passed in (as indicated by ARECnF being defined),
1789 -- then include ARECnU : ARECmPT where ARECmPT comes from
1790 -- the level one higher than the current level, and the
1791 -- entity ARECnPT comes from the enclosing subprogram.
1792
1793 if Present (STJ.ARECnF) then
1794 declare
1795 STJE : Subp_Entry
1796 renames Subps.Table (Enclosing_Subp (J));
1797 begin
1798 Append_To (Clist,
1799 Make_Component_Declaration (Loc,
1800 Defining_Identifier => STJ.ARECnU,
1801 Component_Definition =>
1802 Make_Component_Definition (Loc,
1803 Subtype_Indication =>
1804 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1805 end;
1806 end if;
1807
1808 -- Add components for uplevel referenced entities
1809
1810 if Present (STJ.Uents) then
1811 declare
1812 Elmt : Elmt_Id;
1813 Ptr_Decl : Node_Id;
1814 Uent : Entity_Id;
1815
1816 Indx : Nat;
1817 -- 1's origin of index in list of elements. This is
1818 -- used to uniquify names if needed in Upref_Name.
1819
1820 begin
1821 Elmt := First_Elmt (STJ.Uents);
1822 Indx := 0;
1823 while Present (Elmt) loop
1824 Uent := Node (Elmt);
1825 Indx := Indx + 1;
1826
1827 Comp :=
1828 Make_Defining_Identifier (Loc,
1829 Chars => Upref_Name (Uent, Indx, Clist));
1830
1831 Set_Activation_Record_Component
1832 (Uent, Comp);
1833
1834 if Needs_Fat_Pointer (Uent) then
1835
1836 -- Build corresponding access type
1837
1838 Ptr_Decl :=
1839 Build_Access_Type_Decl
1840 (Etype (Uent), STJ.Ent);
1841 Append_To (Decls, Ptr_Decl);
1842
1843 -- And use its type in the corresponding
1844 -- component.
1845
1846 Append_To (Clist,
1847 Make_Component_Declaration (Loc,
1848 Defining_Identifier => Comp,
1849 Component_Definition =>
1850 Make_Component_Definition (Loc,
1851 Subtype_Indication =>
1852 New_Occurrence_Of
1853 (Defining_Identifier (Ptr_Decl),
1854 Loc))));
1855 else
1856 Append_To (Clist,
1857 Make_Component_Declaration (Loc,
1858 Defining_Identifier => Comp,
1859 Component_Definition =>
1860 Make_Component_Definition (Loc,
1861 Subtype_Indication =>
1862 New_Occurrence_Of (Addr, Loc))));
1863 end if;
1864 Next_Elmt (Elmt);
1865 end loop;
1866 end;
1867 end if;
1868
1869 -- Now we can insert the AREC declarations into the body
1870 -- type ARECnT is record .. end record;
1871 -- pragma Suppress_Initialization (ARECnT);
1872
1873 -- Note that we need to set the Suppress_Initialization
1874 -- flag after Decl_ARECnT has been analyzed.
1875
1876 Decl_ARECnT :=
1877 Make_Full_Type_Declaration (Loc,
1878 Defining_Identifier => STJ.ARECnT,
1879 Type_Definition =>
1880 Make_Record_Definition (Loc,
1881 Component_List =>
1882 Make_Component_List (Loc,
1883 Component_Items => Clist)));
1884 Append_To (Decls, Decl_ARECnT);
1885
1886 -- type ARECnPT is access all ARECnT;
1887
1888 Decl_ARECnPT :=
1889 Make_Full_Type_Declaration (Loc,
1890 Defining_Identifier => STJ.ARECnPT,
1891 Type_Definition =>
1892 Make_Access_To_Object_Definition (Loc,
1893 All_Present => True,
1894 Subtype_Indication =>
1895 New_Occurrence_Of (STJ.ARECnT, Loc)));
1896 Append_To (Decls, Decl_ARECnPT);
1897
1898 -- ARECn : aliased ARECnT;
1899
1900 Decl_ARECn :=
1901 Make_Object_Declaration (Loc,
1902 Defining_Identifier => STJ.ARECn,
1903 Aliased_Present => True,
1904 Object_Definition =>
1905 New_Occurrence_Of (STJ.ARECnT, Loc));
1906 Append_To (Decls, Decl_ARECn);
1907
1908 -- ARECnP : constant ARECnPT := ARECn'Access;
1909
1910 Decl_ARECnP :=
1911 Make_Object_Declaration (Loc,
1912 Defining_Identifier => STJ.ARECnP,
1913 Constant_Present => True,
1914 Object_Definition =>
1915 New_Occurrence_Of (STJ.ARECnPT, Loc),
1916 Expression =>
1917 Make_Attribute_Reference (Loc,
1918 Prefix =>
1919 New_Occurrence_Of (STJ.ARECn, Loc),
1920 Attribute_Name => Name_Access));
1921 Append_To (Decls, Decl_ARECnP);
1922
1923 -- If we are in a subprogram that has a static link that
1924 -- is passed in (as indicated by ARECnF being defined),
1925 -- then generate ARECn.ARECmU := ARECmF where m is
1926 -- one less than the current level to set the uplink.
1927
1928 if Present (STJ.ARECnF) then
1929 Decl_Assign :=
1930 Make_Assignment_Statement (Loc,
1931 Name =>
1932 Make_Selected_Component (Loc,
1933 Prefix =>
1934 New_Occurrence_Of (STJ.ARECn, Loc),
1935 Selector_Name =>
1936 New_Occurrence_Of (STJ.ARECnU, Loc)),
1937 Expression =>
1938 New_Occurrence_Of (STJ.ARECnF, Loc));
1939 Append_To (Decls, Decl_Assign);
1940
1941 else
1942 Decl_Assign := Empty;
1943 end if;
1944
1945 if No (Declarations (STJ.Bod)) then
1946 Set_Declarations (STJ.Bod, Decls);
1947 else
1948 Prepend_List_To (Declarations (STJ.Bod), Decls);
1949 end if;
1950
1951 -- Analyze the newly inserted declarations. Note that we
1952 -- do not need to establish the whole scope stack, since
1953 -- we have already set all entity fields (so there will
1954 -- be no searching of upper scopes to resolve names). But
1955 -- we do set the scope of the current subprogram, so that
1956 -- newly created entities go in the right entity chain.
1957
1958 -- We analyze with all checks suppressed (since we do
1959 -- not expect any exceptions).
1960
1961 Push_Scope (STJ.Ent);
1962 Analyze (Decl_ARECnT, Suppress => All_Checks);
1963
1964 -- Note that we need to call Set_Suppress_Initialization
1965 -- after Decl_ARECnT has been analyzed, but before
1966 -- analyzing Decl_ARECnP so that the flag is properly
1967 -- taking into account.
1968
1969 Set_Suppress_Initialization (STJ.ARECnT);
1970
1971 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1972 Analyze (Decl_ARECn, Suppress => All_Checks);
1973 Analyze (Decl_ARECnP, Suppress => All_Checks);
1974
1975 if Present (Decl_Assign) then
1976 Analyze (Decl_Assign, Suppress => All_Checks);
1977 end if;
1978
1979 Pop_Scope;
1980
1981 -- Next step, for each uplevel referenced entity, add
1982 -- assignment operations to set the component in the
1983 -- activation record.
1984
1985 if Present (STJ.Uents) then
1986 declare
1987 Elmt : Elmt_Id;
1988
1989 begin
1990 Elmt := First_Elmt (STJ.Uents);
1991 while Present (Elmt) loop
1992 declare
1993 Ent : constant Entity_Id := Node (Elmt);
1994 Loc : constant Source_Ptr := Sloc (Ent);
1995 Dec : constant Node_Id :=
1996 Declaration_Node (Ent);
1997
1998 Asn : Node_Id;
1999 Attr : Name_Id;
2000 Comp : Entity_Id;
2001 Ins : Node_Id;
2002 Rhs : Node_Id;
2003
2004 begin
2005 -- For parameters, we insert the assignment
2006 -- right after the declaration of ARECnP.
2007 -- For all other entities, we insert the
2008 -- assignment immediately after the
2009 -- declaration of the entity or after the
2010 -- freeze node if present.
2011
2012 -- Note: we don't need to mark the entity
2013 -- as being aliased, because the address
2014 -- attribute will mark it as Address_Taken,
2015 -- and that is good enough.
2016
2017 if Is_Formal (Ent) then
2018 Ins := Decl_ARECnP;
2019
2020 elsif Has_Delayed_Freeze (Ent) then
2021 Ins := Freeze_Node (Ent);
2022
2023 else
2024 Ins := Dec;
2025 end if;
2026
2027 -- Build and insert the assignment:
2028 -- ARECn.nam := nam'Address
2029 -- or else 'Access for unconstrained array
2030
2031 if Needs_Fat_Pointer (Ent) then
2032 Attr := Name_Access;
2033 else
2034 Attr := Name_Address;
2035 end if;
2036
2037 Rhs :=
2038 Make_Attribute_Reference (Loc,
2039 Prefix =>
2040 New_Occurrence_Of (Ent, Loc),
2041 Attribute_Name => Attr);
2042
2043 -- If the entity is an unconstrained formal
2044 -- we wrap the attribute reference in an
2045 -- unchecked conversion to the type of the
2046 -- activation record component, to prevent
2047 -- spurious subtype conformance errors within
2048 -- instances.
2049
2050 if Is_Formal (Ent)
2051 and then not Is_Constrained (Etype (Ent))
2052 then
2053 -- Find target component and its type
2054
2055 Comp := First_Component (STJ.ARECnT);
2056 while Chars (Comp) /= Chars (Ent) loop
2057 Comp := Next_Component (Comp);
2058 end loop;
2059
2060 Rhs :=
2061 Unchecked_Convert_To (Etype (Comp), Rhs);
2062 end if;
2063
2064 Asn :=
2065 Make_Assignment_Statement (Loc,
2066 Name =>
2067 Make_Selected_Component (Loc,
2068 Prefix =>
2069 New_Occurrence_Of (STJ.ARECn, Loc),
2070 Selector_Name =>
2071 New_Occurrence_Of
2072 (Activation_Record_Component
2073 (Ent),
2074 Loc)),
2075 Expression => Rhs);
2076
2077 -- If we have a loop parameter, we have
2078 -- to insert before the first statement
2079 -- of the loop. Ins points to the
2080 -- N_Loop_Parameter_Specification or to
2081 -- an N_Iterator_Specification.
2082
2083 if Nkind_In
2084 (Ins, N_Iterator_Specification,
2085 N_Loop_Parameter_Specification)
2086 then
2087 -- Quantified expression are rewritten as
2088 -- loops during expansion.
2089
2090 if Nkind (Parent (Ins)) =
2091 N_Quantified_Expression
2092 then
2093 null;
2094
2095 else
2096 Ins :=
2097 First
2098 (Statements
2099 (Parent (Parent (Ins))));
2100 Insert_Before (Ins, Asn);
2101 end if;
2102
2103 else
2104 Insert_After (Ins, Asn);
2105 end if;
2106
2107 -- Analyze the assignment statement. We do
2108 -- not need to establish the relevant scope
2109 -- stack entries here, because we have
2110 -- already set the correct entity references,
2111 -- so no name resolution is required, and no
2112 -- new entities are created, so we don't even
2113 -- need to set the current scope.
2114
2115 -- We analyze with all checks suppressed
2116 -- (since we do not expect any exceptions).
2117
2118 Analyze (Asn, Suppress => All_Checks);
2119 end;
2120
2121 Next_Elmt (Elmt);
2122 end loop;
2123 end;
2124 end if;
2125 end;
2126 end if;
2127 end;
2128 end loop;
2129 end Subp_Loop;
2130
2131 -- Next step, process uplevel references. This has to be done in a
2132 -- separate pass, after completing the processing in Sub_Loop because we
2133 -- need all the AREC declarations generated, inserted, and analyzed so
2134 -- that the uplevel references can be successfully analyzed.
2135
2136 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
2137 declare
2138 UPJ : Uref_Entry renames Urefs.Table (J);
2139
2140 begin
2141 -- Ignore type references, these are implicit references that do
2142 -- not need rewriting (e.g. the appearence in a conversion).
2143 -- Also ignore if no reference was specified or if the rewriting
2144 -- has already been done (this can happen if the N_Identifier
2145 -- occurs more than one time in the tree).
2146
2147 if No (UPJ.Ref)
2148 or else not Is_Entity_Name (UPJ.Ref)
2149 or else not Present (Entity (UPJ.Ref))
2150 then
2151 goto Continue;
2152 end if;
2153
2154 -- Rewrite one reference
2155
2156 Rewrite_One_Ref : declare
2157 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
2158 -- Source location for the reference
2159
2160 Typ : constant Entity_Id := Etype (UPJ.Ent);
2161 -- The type of the referenced entity
2162
2163 Atyp : Entity_Id;
2164 -- The actual subtype of the reference
2165
2166 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
2167 -- Subp_Index for caller containing reference
2168
2169 STJR : Subp_Entry renames Subps.Table (RS_Caller);
2170 -- Subp_Entry for subprogram containing reference
2171
2172 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
2173 -- Subp_Index for subprogram containing referenced entity
2174
2175 STJE : Subp_Entry renames Subps.Table (RS_Callee);
2176 -- Subp_Entry for subprogram containing referenced entity
2177
2178 Pfx : Node_Id;
2179 Comp : Entity_Id;
2180 SI : SI_Type;
2181
2182 begin
2183 Atyp := Etype (UPJ.Ref);
2184
2185 if Ekind (Atyp) /= E_Record_Subtype then
2186 Atyp := Get_Actual_Subtype (UPJ.Ref);
2187 end if;
2188
2189 -- Ignore if no ARECnF entity for enclosing subprogram which
2190 -- probably happens as a result of not properly treating
2191 -- instance bodies. To be examined ???
2192
2193 -- If this test is omitted, then the compilation of freeze.adb
2194 -- and inline.adb fail in unnesting mode.
2195
2196 if No (STJR.ARECnF) then
2197 goto Continue;
2198 end if;
2199
2200 -- If this is a reference to a global constant, use its value
2201 -- rather than create a reference. It is more efficient and
2202 -- furthermore indispensable if the context requires a
2203 -- constant, such as a branch of a case statement.
2204
2205 if Ekind (UPJ.Ent) = E_Constant
2206 and then Is_True_Constant (UPJ.Ent)
2207 and then Present (Constant_Value (UPJ.Ent))
2208 and then Is_Static_Expression (Constant_Value (UPJ.Ent))
2209 then
2210 Rewrite (UPJ.Ref, New_Copy_Tree (Constant_Value (UPJ.Ent)));
2211 goto Continue;
2212 end if;
2213
2214 -- Push the current scope, so that the pointer type Tnn, and
2215 -- any subsidiary entities resulting from the analysis of the
2216 -- rewritten reference, go in the right entity chain.
2217
2218 Push_Scope (STJR.Ent);
2219
2220 -- Now we need to rewrite the reference. We have a reference
2221 -- from level STJR.Lev to level STJE.Lev. The general form of
2222 -- the rewritten reference for entity X is:
2223
2224 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
2225
2226 -- where a,b,c,d .. m =
2227 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
2228
2229 pragma Assert (STJR.Lev > STJE.Lev);
2230
2231 -- Compute the prefix of X. Here are examples to make things
2232 -- clear (with parens to show groupings, the prefix is
2233 -- everything except the .X at the end).
2234
2235 -- level 2 to level 1
2236
2237 -- AREC1F.X
2238
2239 -- level 3 to level 1
2240
2241 -- (AREC2F.AREC1U).X
2242
2243 -- level 4 to level 1
2244
2245 -- ((AREC3F.AREC2U).AREC1U).X
2246
2247 -- level 6 to level 2
2248
2249 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
2250
2251 -- In the above, ARECnF and ARECnU are pointers, so there are
2252 -- explicit dereferences required for these occurrences.
2253
2254 Pfx :=
2255 Make_Explicit_Dereference (Loc,
2256 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
2257 SI := RS_Caller;
2258 for L in STJE.Lev .. STJR.Lev - 2 loop
2259 SI := Enclosing_Subp (SI);
2260 Pfx :=
2261 Make_Explicit_Dereference (Loc,
2262 Prefix =>
2263 Make_Selected_Component (Loc,
2264 Prefix => Pfx,
2265 Selector_Name =>
2266 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
2267 end loop;
2268
2269 -- Get activation record component (must exist)
2270
2271 Comp := Activation_Record_Component (UPJ.Ent);
2272 pragma Assert (Present (Comp));
2273
2274 -- Do the replacement. If the component type is an access type,
2275 -- this is an uplevel reference for an entity that requires a
2276 -- fat pointer, so dereference the component.
2277
2278 if Is_Access_Type (Etype (Comp)) then
2279 Rewrite (UPJ.Ref,
2280 Make_Explicit_Dereference (Loc,
2281 Prefix =>
2282 Make_Selected_Component (Loc,
2283 Prefix => Pfx,
2284 Selector_Name =>
2285 New_Occurrence_Of (Comp, Loc))));
2286
2287 else
2288 Rewrite (UPJ.Ref,
2289 Make_Attribute_Reference (Loc,
2290 Prefix => New_Occurrence_Of (Atyp, Loc),
2291 Attribute_Name => Name_Deref,
2292 Expressions => New_List (
2293 Make_Selected_Component (Loc,
2294 Prefix => Pfx,
2295 Selector_Name =>
2296 New_Occurrence_Of (Comp, Loc)))));
2297 end if;
2298
2299 -- Analyze and resolve the new expression. We do not need to
2300 -- establish the relevant scope stack entries here, because we
2301 -- have already set all the correct entity references, so no
2302 -- name resolution is needed. We have already set the current
2303 -- scope, so that any new entities created will be in the right
2304 -- scope.
2305
2306 -- We analyze with all checks suppressed (since we do not
2307 -- expect any exceptions)
2308
2309 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
2310 Pop_Scope;
2311 end Rewrite_One_Ref;
2312 end;
2313
2314 <<Continue>>
2315 null;
2316 end loop Uplev_Refs;
2317
2318 -- Finally, loop through all calls adding extra actual for the
2319 -- activation record where it is required.
2320
2321 Adjust_Calls : for J in Calls.First .. Calls.Last loop
2322
2323 -- Process a single call, we are only interested in a call to a
2324 -- subprogram that actually needs a pointer to an activation record,
2325 -- as indicated by the ARECnF entity being set. This excludes the
2326 -- top level subprogram, and any subprogram not having uplevel refs.
2327
2328 Adjust_One_Call : declare
2329 CTJ : Call_Entry renames Calls.Table (J);
2330 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
2331 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
2332
2333 Loc : constant Source_Ptr := Sloc (CTJ.N);
2334
2335 Extra : Node_Id;
2336 ExtraP : Node_Id;
2337 SubX : SI_Type;
2338 Act : Node_Id;
2339
2340 begin
2341 if Present (STT.ARECnF)
2342 and then Nkind (CTJ.N) in N_Subprogram_Call
2343 then
2344 -- CTJ.N is a call to a subprogram which may require a pointer
2345 -- to an activation record. The subprogram containing the call
2346 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2347 -- have a call from level STF.Lev to level STT.Lev.
2348
2349 -- There are three possibilities:
2350
2351 -- For a call to the same level, we just pass the activation
2352 -- record passed to the calling subprogram.
2353
2354 if STF.Lev = STT.Lev then
2355 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2356
2357 -- For a call that goes down a level, we pass a pointer to the
2358 -- activation record constructed within the caller (which may
2359 -- be the outer-level subprogram, but also may be a more deeply
2360 -- nested caller).
2361
2362 elsif STT.Lev = STF.Lev + 1 then
2363 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
2364
2365 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2366 -- since it is not possible to do a downcall of more than
2367 -- one level.
2368
2369 -- For a call from level STF.Lev to level STT.Lev, we
2370 -- have to find the activation record needed by the
2371 -- callee. This is as follows:
2372
2373 -- ARECaF.ARECbU.ARECcU....ARECmU
2374
2375 -- where a,b,c .. m =
2376 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2377
2378 else
2379 pragma Assert (STT.Lev < STF.Lev);
2380
2381 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2382 SubX := Subp_Index (CTJ.Caller);
2383 for K in reverse STT.Lev .. STF.Lev - 1 loop
2384 SubX := Enclosing_Subp (SubX);
2385 Extra :=
2386 Make_Selected_Component (Loc,
2387 Prefix => Extra,
2388 Selector_Name =>
2389 New_Occurrence_Of
2390 (Subps.Table (SubX).ARECnU, Loc));
2391 end loop;
2392 end if;
2393
2394 -- Extra is the additional parameter to be added. Build a
2395 -- parameter association that we can append to the actuals.
2396
2397 ExtraP :=
2398 Make_Parameter_Association (Loc,
2399 Selector_Name =>
2400 New_Occurrence_Of (STT.ARECnF, Loc),
2401 Explicit_Actual_Parameter => Extra);
2402
2403 if No (Parameter_Associations (CTJ.N)) then
2404 Set_Parameter_Associations (CTJ.N, Empty_List);
2405 end if;
2406
2407 Append (ExtraP, Parameter_Associations (CTJ.N));
2408
2409 -- We need to deal with the actual parameter chain as well. The
2410 -- newly added parameter is always the last actual.
2411
2412 Act := First_Named_Actual (CTJ.N);
2413
2414 if No (Act) then
2415 Set_First_Named_Actual (CTJ.N, Extra);
2416
2417 -- If call has been relocated (as with an expression in
2418 -- an aggregate), set First_Named pointer in original node
2419 -- as well, because that's the parent of the parameter list.
2420
2421 Set_First_Named_Actual
2422 (Parent (List_Containing (ExtraP)), Extra);
2423
2424 -- Here we must follow the chain and append the new entry
2425
2426 else
2427 loop
2428 declare
2429 PAN : Node_Id;
2430 NNA : Node_Id;
2431
2432 begin
2433 PAN := Parent (Act);
2434 pragma Assert (Nkind (PAN) = N_Parameter_Association);
2435 NNA := Next_Named_Actual (PAN);
2436
2437 if No (NNA) then
2438 Set_Next_Named_Actual (PAN, Extra);
2439 exit;
2440 end if;
2441
2442 Act := NNA;
2443 end;
2444 end loop;
2445 end if;
2446
2447 -- Analyze and resolve the new actual. We do not need to
2448 -- establish the relevant scope stack entries here, because
2449 -- we have already set all the correct entity references, so
2450 -- no name resolution is needed.
2451
2452 -- We analyze with all checks suppressed (since we do not
2453 -- expect any exceptions, and also we temporarily turn off
2454 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2455 -- references (not needed at this stage, and in fact causes
2456 -- a bit of recursive chaos).
2457
2458 Opt.Unnest_Subprogram_Mode := False;
2459 Analyze_And_Resolve
2460 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
2461 Opt.Unnest_Subprogram_Mode := True;
2462 end if;
2463 end Adjust_One_Call;
2464 end loop Adjust_Calls;
2465
2466 return;
2467 end Unnest_Subprogram;
2468
2469 ------------------------
2470 -- Unnest_Subprograms --
2471 ------------------------
2472
2473 procedure Unnest_Subprograms (N : Node_Id) is
2474 function Search_Subprograms (N : Node_Id) return Traverse_Result;
2475 -- Tree visitor that search for outer level procedures with nested
2476 -- subprograms and invokes Unnest_Subprogram()
2477
2478 ---------------
2479 -- Do_Search --
2480 ---------------
2481
2482 procedure Do_Search is new Traverse_Proc (Search_Subprograms);
2483 -- Subtree visitor instantiation
2484
2485 ------------------------
2486 -- Search_Subprograms --
2487 ------------------------
2488
2489 function Search_Subprograms (N : Node_Id) return Traverse_Result is
2490 begin
2491 if Nkind_In (N, N_Subprogram_Body, N_Subprogram_Body_Stub) then
2492 declare
2493 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
2494
2495 begin
2496 -- We are only interested in subprograms (not generic
2497 -- subprograms), that have nested subprograms.
2498
2499 if Is_Subprogram (Spec_Id)
2500 and then Has_Nested_Subprogram (Spec_Id)
2501 and then Is_Library_Level_Entity (Spec_Id)
2502 then
2503 Unnest_Subprogram (Spec_Id, N);
2504 end if;
2505 end;
2506
2507 -- The proper body of a stub may contain nested subprograms, and
2508 -- therefore must be visited explicitly. Nested stubs are examined
2509 -- recursively in Visit_Node.
2510
2511 elsif Nkind (N) in N_Body_Stub then
2512 Do_Search (Library_Unit (N));
2513
2514 -- Skip generic packages
2515
2516 elsif Nkind (N) = N_Package_Body
2517 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
2518 then
2519 return Skip;
2520 end if;
2521
2522 return OK;
2523 end Search_Subprograms;
2524
2525 -- Start of processing for Unnest_Subprograms
2526
2527 begin
2528 if not Opt.Unnest_Subprogram_Mode or not Opt.Expander_Active then
2529 return;
2530 end if;
2531
2532 -- A specification will contain bodies if it contains instantiations so
2533 -- examine package or subprogram declaration of the main unit, when it
2534 -- is present.
2535
2536 if Nkind (Unit (N)) = N_Package_Body
2537 or else (Nkind (Unit (N)) = N_Subprogram_Body
2538 and then not Acts_As_Spec (N))
2539 then
2540 Do_Search (Library_Unit (N));
2541 end if;
2542
2543 Do_Search (N);
2544 end Unnest_Subprograms;
2545
2546 end Exp_Unst;