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