gcc/testsuite/ChangeLog:
[official-gcc.git] / gcc / ada / exp_unst.adb
blobf1c371a765c43969d6e2771ce188e2ff2f5fc005
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 ------------------------------------------------------------------------------
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;
50 package body Exp_Unst is
52 -----------------------
53 -- Local Subprograms --
54 -----------------------
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.
64 -----------
65 -- Calls --
66 -----------
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.
73 type Call_Entry is record
74 N : Node_Id;
75 -- The actual call
77 Caller : Entity_Id;
78 -- Entity of the subprogram containing the call (can be at any level)
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;
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.
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.
101 ----------------------------------
102 -- Subprograms For Fat Pointers --
103 ----------------------------------
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.
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.
118 -----------
119 -- Urefs --
120 -----------
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.
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.
137 Ent : Entity_Id;
138 -- The Entity_Id of the uplevel referenced object or type
140 Caller : Entity_Id;
141 -- The entity for the subprogram immediately containing this entity
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;
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");
157 ------------------------
158 -- Append_Unique_Call --
159 ------------------------
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;
169 Calls.Append (Call);
170 end Append_Unique_Call;
172 -----------------------------
173 -- Build_Access_Type_Decl --
174 -----------------------------
176 function Build_Access_Type_Decl
177 (E : Entity_Id;
178 Scop : Entity_Id) return Node_Id
180 Loc : constant Source_Ptr := Sloc (E);
181 Typ : Entity_Id;
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));
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;
198 ---------------
199 -- Get_Level --
200 ---------------
202 function Get_Level (Subp : Entity_Id; Sub : Entity_Id) return Nat is
203 Lev : Nat;
204 S : Entity_Id;
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;
219 --------------------------
220 -- In_Synchronized_Unit --
221 --------------------------
223 function In_Synchronized_Unit (Subp : Entity_Id) return Boolean is
224 S : Entity_Id := Scope (Subp);
226 begin
227 while Present (S) and then S /= Standard_Standard loop
228 if Is_Concurrent_Type (S) then
229 return True;
230 end if;
232 S := Scope (S);
233 end loop;
235 return False;
236 end In_Synchronized_Unit;
238 -----------------------
239 -- Needs_Fat_Pointer --
240 -----------------------
242 function Needs_Fat_Pointer (E : Entity_Id) return Boolean is
243 begin
244 return Is_Formal (E)
245 and then Is_Array_Type (Etype (E))
246 and then not Is_Constrained (Etype (E));
247 end Needs_Fat_Pointer;
249 ----------------
250 -- Subp_Index --
251 ----------------
253 function Subp_Index (Sub : Entity_Id) return SI_Type is
254 E : Entity_Id := Sub;
256 begin
257 pragma Assert (Is_Subprogram (E));
259 if Subps_Index (E) = Uint_0 then
260 E := Ultimate_Alias (E);
262 if Ekind (E) = E_Function
263 and then Rewritten_For_C (E)
264 and then Present (Corresponding_Procedure (E))
265 then
266 E := Corresponding_Procedure (E);
267 end if;
268 end if;
270 pragma Assert (Subps_Index (E) /= Uint_0);
271 return SI_Type (UI_To_Int (Subps_Index (E)));
272 end Subp_Index;
274 -----------------------
275 -- Unnest_Subprogram --
276 -----------------------
278 procedure Unnest_Subprogram (Subp : Entity_Id; Subp_Body : Node_Id) is
279 function AREC_Name (J : Pos; S : String) return Name_Id;
280 -- Returns name for string ARECjS, where j is the decimal value of j
282 function Enclosing_Subp (Subp : SI_Type) return SI_Type;
283 -- Subp is the index of a subprogram which has a Lev greater than 1.
284 -- This function returns the index of the enclosing subprogram which
285 -- will have a Lev value one less than this.
287 function Img_Pos (N : Pos) return String;
288 -- Return image of N without leading blank
290 function Upref_Name
291 (Ent : Entity_Id;
292 Index : Pos;
293 Clist : List_Id) return Name_Id;
294 -- This function returns the name to be used in the activation record to
295 -- reference the variable uplevel. Clist is the list of components that
296 -- have been created in the activation record so far. Normally the name
297 -- is just a copy of the Chars field of the entity. The exception is
298 -- when the name has already been used, in which case we suffix the name
299 -- with the index value Index to avoid duplication. This happens with
300 -- declare blocks and generic parameters at least.
302 ---------------
303 -- AREC_Name --
304 ---------------
306 function AREC_Name (J : Pos; S : String) return Name_Id is
307 begin
308 return Name_Find ("AREC" & Img_Pos (J) & S);
309 end AREC_Name;
311 --------------------
312 -- Enclosing_Subp --
313 --------------------
315 function Enclosing_Subp (Subp : SI_Type) return SI_Type is
316 STJ : Subp_Entry renames Subps.Table (Subp);
317 Ret : constant SI_Type := Subp_Index (Enclosing_Subprogram (STJ.Ent));
318 begin
319 pragma Assert (STJ.Lev > 1);
320 pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1);
321 return Ret;
322 end Enclosing_Subp;
324 -------------
325 -- Img_Pos --
326 -------------
328 function Img_Pos (N : Pos) return String is
329 Buf : String (1 .. 20);
330 Ptr : Natural;
331 NV : Nat;
333 begin
334 Ptr := Buf'Last;
335 NV := N;
336 while NV /= 0 loop
337 Buf (Ptr) := Character'Val (48 + NV mod 10);
338 Ptr := Ptr - 1;
339 NV := NV / 10;
340 end loop;
342 return Buf (Ptr + 1 .. Buf'Last);
343 end Img_Pos;
345 ----------------
346 -- Upref_Name --
347 ----------------
349 function Upref_Name
350 (Ent : Entity_Id;
351 Index : Pos;
352 Clist : List_Id) return Name_Id
354 C : Node_Id;
355 begin
356 C := First (Clist);
357 loop
358 if No (C) then
359 return Chars (Ent);
361 elsif Chars (Defining_Identifier (C)) = Chars (Ent) then
362 return
363 Name_Find (Get_Name_String (Chars (Ent)) & Img_Pos (Index));
364 else
365 Next (C);
366 end if;
367 end loop;
368 end Upref_Name;
370 -- Start of processing for Unnest_Subprogram
372 begin
373 -- Nothing to do inside a generic (all processing is for instance)
375 if Inside_A_Generic then
376 return;
377 end if;
379 -- If the main unit is a package body then we need to examine the spec
380 -- to determine whether the main unit is generic (the scope stack is not
381 -- present when this is called on the main unit).
383 if Ekind (Cunit_Entity (Main_Unit)) = E_Package_Body
384 and then Is_Generic_Unit (Spec_Entity (Cunit_Entity (Main_Unit)))
385 then
386 return;
387 end if;
389 -- Only unnest when generating code for the main source unit
391 if not In_Extended_Main_Code_Unit (Subp_Body) then
392 return;
393 end if;
395 -- This routine is called late, after the scope stack is gone. The
396 -- following creates a suitable dummy scope stack to be used for the
397 -- analyze/expand calls made from this routine.
399 Push_Scope (Subp);
401 -- First step, we must mark all nested subprograms that require a static
402 -- link (activation record) because either they contain explicit uplevel
403 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
404 -- this point), or they make calls to other subprograms in the same nest
405 -- that require a static link (in which case we set this flag).
407 -- This is a recursive definition, and to implement this, we have to
408 -- build a call graph for the set of nested subprograms, and then go
409 -- over this graph to implement recursively the invariant that if a
410 -- subprogram has a call to a subprogram requiring a static link, then
411 -- the calling subprogram requires a static link.
413 -- First populate the above tables
415 Subps_First := Subps.Last + 1;
416 Calls.Init;
417 Urefs.Init;
419 Build_Tables : declare
420 Current_Subprogram : Entity_Id;
421 -- When we scan a subprogram body, we set Current_Subprogram to the
422 -- corresponding entity. This gets recursively saved and restored.
424 function Visit_Node (N : Node_Id) return Traverse_Result;
425 -- Visit a single node in Subp
427 -----------
428 -- Visit --
429 -----------
431 procedure Visit is new Traverse_Proc (Visit_Node);
432 -- Used to traverse the body of Subp, populating the tables
434 ----------------
435 -- Visit_Node --
436 ----------------
438 function Visit_Node (N : Node_Id) return Traverse_Result is
439 Ent : Entity_Id;
440 Caller : Entity_Id;
441 Callee : Entity_Id;
443 procedure Check_Static_Type
444 (T : Entity_Id; N : Node_Id; DT : in out Boolean);
445 -- Given a type T, checks if it is a static type defined as a type
446 -- with no dynamic bounds in sight. If so, the only action is to
447 -- set Is_Static_Type True for T. If T is not a static type, then
448 -- all types with dynamic bounds associated with T are detected,
449 -- and their bounds are marked as uplevel referenced if not at the
450 -- library level, and DT is set True. If N is specified, it's the
451 -- node that will need to be replaced. If not specified, it means
452 -- we can't do a replacement because the bound is implicit.
454 procedure Note_Uplevel_Ref
455 (E : Entity_Id;
456 N : Node_Id;
457 Caller : Entity_Id;
458 Callee : Entity_Id);
459 -- Called when we detect an explicit or implicit uplevel reference
460 -- from within Caller to entity E declared in Callee. E can be a
461 -- an object or a type.
463 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id);
464 -- Enter a subprogram whose body is visible or which is a
465 -- subprogram instance into the subprogram table.
467 -----------------------
468 -- Check_Static_Type --
469 -----------------------
471 procedure Check_Static_Type
472 (T : Entity_Id; N : Node_Id; DT : in out Boolean)
474 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id);
475 -- N is the bound of a dynamic type. This procedure notes that
476 -- this bound is uplevel referenced, it can handle references
477 -- to entities (typically _FIRST and _LAST entities), and also
478 -- attribute references of the form T'name (name is typically
479 -- FIRST or LAST) where T is the uplevel referenced bound.
480 -- Ref, if Present, is the location of the reference to
481 -- replace.
483 ------------------------
484 -- Note_Uplevel_Bound --
485 ------------------------
487 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id) is
488 begin
489 -- Entity name case. Make sure that the entity is declared
490 -- in a subprogram. This may not be the case for for a type
491 -- in a loop appearing in a precondition.
492 -- Exclude explicitly discriminants (that can appear
493 -- in bounds of discriminated components).
495 if Is_Entity_Name (N) then
496 if Present (Entity (N))
497 and then Present (Enclosing_Subprogram (Entity (N)))
498 and then Ekind (Entity (N)) /= E_Discriminant
499 then
500 Note_Uplevel_Ref
501 (E => Entity (N),
502 N => Ref,
503 Caller => Current_Subprogram,
504 Callee => Enclosing_Subprogram (Entity (N)));
505 end if;
507 -- Attribute or indexed component case
509 elsif Nkind_In (N, N_Attribute_Reference,
510 N_Indexed_Component)
511 then
512 Note_Uplevel_Bound (Prefix (N), Ref);
514 -- The indices of the indexed components, or the
515 -- associated expressions of an attribute reference,
516 -- may also involve uplevel references.
518 declare
519 Expr : Node_Id;
521 begin
522 Expr := First (Expressions (N));
523 while Present (Expr) loop
524 Note_Uplevel_Bound (Expr, Ref);
525 Next (Expr);
526 end loop;
527 end;
529 -- Binary operator cases. These can apply to arrays for
530 -- which we may need bounds.
532 elsif Nkind (N) in N_Binary_Op then
533 Note_Uplevel_Bound (Left_Opnd (N), Ref);
534 Note_Uplevel_Bound (Right_Opnd (N), Ref);
536 -- Unary operator case
538 elsif Nkind (N) in N_Unary_Op then
539 Note_Uplevel_Bound (Right_Opnd (N), Ref);
541 -- Explicit dereference case
543 elsif Nkind (N) = N_Explicit_Dereference then
544 Note_Uplevel_Bound (Prefix (N), Ref);
546 -- Conversion case
548 elsif Nkind (N) = N_Type_Conversion then
549 Note_Uplevel_Bound (Expression (N), Ref);
550 end if;
551 end Note_Uplevel_Bound;
553 -- Start of processing for Check_Static_Type
555 begin
556 -- If already marked static, immediate return
558 if Is_Static_Type (T) then
559 return;
560 end if;
562 -- If the type is at library level, always consider it static,
563 -- since such uplevel references are irrelevant.
565 if Is_Library_Level_Entity (T) then
566 Set_Is_Static_Type (T);
567 return;
568 end if;
570 -- Otherwise figure out what the story is with this type
572 -- For a scalar type, check bounds
574 if Is_Scalar_Type (T) then
576 -- If both bounds static, then this is a static type
578 declare
579 LB : constant Node_Id := Type_Low_Bound (T);
580 UB : constant Node_Id := Type_High_Bound (T);
582 begin
583 if not Is_Static_Expression (LB) then
584 Note_Uplevel_Bound (LB, N);
585 DT := True;
586 end if;
588 if not Is_Static_Expression (UB) then
589 Note_Uplevel_Bound (UB, N);
590 DT := True;
591 end if;
592 end;
594 -- For record type, check all components and discriminant
595 -- constraints if present.
597 elsif Is_Record_Type (T) then
598 declare
599 C : Entity_Id;
600 D : Elmt_Id;
602 begin
603 C := First_Component_Or_Discriminant (T);
604 while Present (C) loop
605 Check_Static_Type (Etype (C), N, DT);
606 Next_Component_Or_Discriminant (C);
607 end loop;
609 if Has_Discriminants (T)
610 and then Present (Discriminant_Constraint (T))
611 then
612 D := First_Elmt (Discriminant_Constraint (T));
613 while Present (D) loop
614 if not Is_Static_Expression (Node (D)) then
615 Note_Uplevel_Bound (Node (D), N);
616 DT := True;
617 end if;
619 Next_Elmt (D);
620 end loop;
621 end if;
622 end;
624 -- For array type, check index types and component type
626 elsif Is_Array_Type (T) then
627 declare
628 IX : Node_Id;
629 begin
630 Check_Static_Type (Component_Type (T), N, DT);
632 IX := First_Index (T);
633 while Present (IX) loop
634 Check_Static_Type (Etype (IX), N, DT);
635 Next_Index (IX);
636 end loop;
637 end;
639 -- For private type, examine whether full view is static
641 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
642 Check_Static_Type (Full_View (T), N, DT);
644 if Is_Static_Type (Full_View (T)) then
645 Set_Is_Static_Type (T);
646 end if;
648 -- For now, ignore other types
650 else
651 return;
652 end if;
654 if not DT then
655 Set_Is_Static_Type (T);
656 end if;
657 end Check_Static_Type;
659 ----------------------
660 -- Note_Uplevel_Ref --
661 ----------------------
663 procedure Note_Uplevel_Ref
664 (E : Entity_Id;
665 N : Node_Id;
666 Caller : Entity_Id;
667 Callee : Entity_Id)
669 Full_E : Entity_Id := E;
670 begin
671 -- Nothing to do for static type
673 if Is_Static_Type (E) then
674 return;
675 end if;
677 -- Nothing to do if Caller and Callee are the same
679 if Caller = Callee then
680 return;
682 -- Callee may be a function that returns an array, and that has
683 -- been rewritten as a procedure. If caller is that procedure,
684 -- nothing to do either.
686 elsif Ekind (Callee) = E_Function
687 and then Rewritten_For_C (Callee)
688 and then Corresponding_Procedure (Callee) = Caller
689 then
690 return;
691 end if;
693 -- We have a new uplevel referenced entity
695 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
696 Full_E := Full_View (E);
697 end if;
699 -- All we do at this stage is to add the uplevel reference to
700 -- the table. It's too early to do anything else, since this
701 -- uplevel reference may come from an unreachable subprogram
702 -- in which case the entry will be deleted.
704 Urefs.Append ((N, Full_E, Caller, Callee));
705 end Note_Uplevel_Ref;
707 -------------------------
708 -- Register_Subprogram --
709 -------------------------
711 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id) is
712 L : constant Nat := Get_Level (Subp, E);
714 begin
715 -- Subprograms declared in tasks and protected types cannot
716 -- be eliminated because calls to them may be in other units,
717 -- so they must be treated as reachable.
719 Subps.Append
720 ((Ent => E,
721 Bod => Bod,
722 Lev => L,
723 Reachable => In_Synchronized_Unit (E),
725 -- Subprograms declared in tasks and protected types are
726 -- reachable and cannot be eliminated.
728 Uplevel_Ref => L,
729 Declares_AREC => False,
730 Uents => No_Elist,
731 Last => 0,
732 ARECnF => Empty,
733 ARECn => Empty,
734 ARECnT => Empty,
735 ARECnPT => Empty,
736 ARECnP => Empty,
737 ARECnU => Empty));
739 Set_Subps_Index (E, UI_From_Int (Subps.Last));
740 end Register_Subprogram;
742 -- Start of processing for Visit_Node
744 begin
745 case Nkind (N) is
747 -- Record a subprogram call
749 when N_Function_Call
750 | N_Procedure_Call_Statement
752 -- We are only interested in direct calls, not indirect
753 -- calls (where Name (N) is an explicit dereference) at
754 -- least for now!
756 if Nkind (Name (N)) in N_Has_Entity then
757 Ent := Entity (Name (N));
759 -- We are only interested in calls to subprograms nested
760 -- within Subp. Calls to Subp itself or to subprograms
761 -- outside the nested structure do not affect us.
763 if Scope_Within (Ent, Subp)
764 and then Is_Subprogram (Ent)
765 and then not Is_Imported (Ent)
766 then
767 Append_Unique_Call ((N, Current_Subprogram, Ent));
768 end if;
769 end if;
771 -- For all calls where the formal is an unconstrained array
772 -- and the actual is constrained we need to check the bounds
773 -- for uplevel references.
775 declare
776 Actual : Entity_Id;
777 DT : Boolean := False;
778 Formal : Node_Id;
779 Subp : Entity_Id;
781 begin
782 if Nkind (Name (N)) = N_Explicit_Dereference then
783 Subp := Etype (Name (N));
784 else
785 Subp := Entity (Name (N));
786 end if;
788 Actual := First_Actual (N);
789 Formal := First_Formal_With_Extras (Subp);
790 while Present (Actual) loop
791 if Is_Array_Type (Etype (Formal))
792 and then not Is_Constrained (Etype (Formal))
793 and then Is_Constrained (Etype (Actual))
794 then
795 Check_Static_Type (Etype (Actual), Empty, DT);
796 end if;
798 Next_Actual (Actual);
799 Next_Formal_With_Extras (Formal);
800 end loop;
801 end;
803 -- An At_End_Proc in a statement sequence indicates that there
804 -- is a call from the enclosing construct or block to that
805 -- subprogram. As above, the called entity must be local and
806 -- not imported.
808 when N_Handled_Sequence_Of_Statements =>
809 if Present (At_End_Proc (N))
810 and then Scope_Within (Entity (At_End_Proc (N)), Subp)
811 and then not Is_Imported (Entity (At_End_Proc (N)))
812 then
813 Append_Unique_Call
814 ((N, Current_Subprogram, Entity (At_End_Proc (N))));
815 end if;
817 -- Similarly, the following constructs include a semantic
818 -- attribute Procedure_To_Call that must be handled like
819 -- other calls. Likewise for attribute Storage_Pool.
821 when N_Allocator
822 | N_Extended_Return_Statement
823 | N_Free_Statement
824 | N_Simple_Return_Statement
826 declare
827 Pool : constant Entity_Id := Storage_Pool (N);
828 Proc : constant Entity_Id := Procedure_To_Call (N);
830 begin
831 if Present (Proc)
832 and then Scope_Within (Proc, Subp)
833 and then not Is_Imported (Proc)
834 then
835 Append_Unique_Call ((N, Current_Subprogram, Proc));
836 end if;
838 if Present (Pool)
839 and then not Is_Library_Level_Entity (Pool)
840 and then Scope_Within_Or_Same (Scope (Pool), Subp)
841 then
842 Caller := Current_Subprogram;
843 Callee := Enclosing_Subprogram (Pool);
845 if Callee /= Caller then
846 Note_Uplevel_Ref (Pool, Empty, Caller, Callee);
847 end if;
848 end if;
849 end;
851 -- For an allocator with a qualified expression, check type
852 -- of expression being qualified. The explicit type name is
853 -- handled as an entity reference.
855 if Nkind (N) = N_Allocator
856 and then Nkind (Expression (N)) = N_Qualified_Expression
857 then
858 declare
859 DT : Boolean := False;
860 begin
861 Check_Static_Type
862 (Etype (Expression (Expression (N))), Empty, DT);
863 end;
864 end if;
866 -- A 'Access reference is a (potential) call. So is 'Address,
867 -- in particular on imported subprograms. Other attributes
868 -- require special handling.
870 when N_Attribute_Reference =>
871 declare
872 Attr : constant Attribute_Id :=
873 Get_Attribute_Id (Attribute_Name (N));
874 begin
875 case Attr is
876 when Attribute_Access
877 | Attribute_Unchecked_Access
878 | Attribute_Unrestricted_Access
879 | Attribute_Address
881 if Nkind (Prefix (N)) in N_Has_Entity then
882 Ent := Entity (Prefix (N));
884 -- We only need to examine calls to subprograms
885 -- nested within current Subp.
887 if Scope_Within (Ent, Subp) then
888 if Is_Imported (Ent) then
889 null;
891 elsif Is_Subprogram (Ent) then
892 Append_Unique_Call
893 ((N, Current_Subprogram, Ent));
894 end if;
895 end if;
896 end if;
898 -- References to bounds can be uplevel references if
899 -- the type isn't static.
901 when Attribute_First
902 | Attribute_Last
903 | Attribute_Length
905 -- Special-case attributes of objects whose bounds
906 -- may be uplevel references. More complex prefixes
907 -- handled during full traversal. Note that if the
908 -- nominal subtype of the prefix is unconstrained,
909 -- the bound must be obtained from the object, not
910 -- from the (possibly) uplevel reference.
912 if Is_Constrained (Etype (Prefix (N))) then
913 declare
914 DT : Boolean := False;
915 begin
916 Check_Static_Type
917 (Etype (Prefix (N)), Empty, DT);
918 end;
920 return OK;
921 end if;
923 when others =>
924 null;
925 end case;
926 end;
928 -- Component associations in aggregates are either static or
929 -- else the aggregate will be expanded into assignments, in
930 -- which case the expression is analyzed later and provides
931 -- no relevant code generation.
933 when N_Component_Association =>
934 if No (Expression (N))
935 or else No (Etype (Expression (N)))
936 then
937 return Skip;
938 end if;
940 -- Generic associations are not analyzed: the actuals are
941 -- transferred to renaming and subtype declarations that
942 -- are the ones that must be examined.
944 when N_Generic_Association =>
945 return Skip;
947 -- Indexed references can be uplevel if the type isn't static
948 -- and if the lower bound (or an inner bound for a multi-
949 -- dimensional array) is uplevel.
951 when N_Indexed_Component
952 | N_Slice
954 if Is_Constrained (Etype (Prefix (N))) then
955 declare
956 DT : Boolean := False;
957 begin
958 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
959 end;
960 end if;
962 -- A selected component can have an implicit up-level
963 -- reference due to the bounds of previous fields in the
964 -- record. We simplify the processing here by examining
965 -- all components of the record.
967 -- Selected components appear as unit names and end labels
968 -- for child units. Prefixes of these nodes denote parent
969 -- units and carry no type information so they are skipped.
971 when N_Selected_Component =>
972 if Present (Etype (Prefix (N))) then
973 declare
974 DT : Boolean := False;
975 begin
976 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
977 end;
978 end if;
980 -- For EQ/NE comparisons, we need the type of the operands
981 -- in order to do the comparison, which means we need the
982 -- bounds.
984 when N_Op_Eq
985 | N_Op_Ne
987 declare
988 DT : Boolean := False;
989 begin
990 Check_Static_Type (Etype (Left_Opnd (N)), Empty, DT);
991 Check_Static_Type (Etype (Right_Opnd (N)), Empty, DT);
992 end;
994 -- Likewise we need the sizes to compute how much to move in
995 -- an assignment.
997 when N_Assignment_Statement =>
998 declare
999 DT : Boolean := False;
1000 begin
1001 Check_Static_Type (Etype (Name (N)), Empty, DT);
1002 Check_Static_Type (Etype (Expression (N)), Empty, DT);
1003 end;
1005 -- Record a subprogram. We record a subprogram body that acts
1006 -- as a spec. Otherwise we record a subprogram declaration,
1007 -- providing that it has a corresponding body we can get hold
1008 -- of. The case of no corresponding body being available is
1009 -- ignored for now.
1011 when N_Subprogram_Body =>
1012 Ent := Unique_Defining_Entity (N);
1014 -- Ignore generic subprogram
1016 if Is_Generic_Subprogram (Ent) then
1017 return Skip;
1018 end if;
1020 -- Make new entry in subprogram table if not already made
1022 Register_Subprogram (Ent, N);
1024 -- We make a recursive call to scan the subprogram body, so
1025 -- that we can save and restore Current_Subprogram.
1027 declare
1028 Save_CS : constant Entity_Id := Current_Subprogram;
1029 Decl : Node_Id;
1031 begin
1032 Current_Subprogram := Ent;
1034 -- Scan declarations
1036 Decl := First (Declarations (N));
1037 while Present (Decl) loop
1038 Visit (Decl);
1039 Next (Decl);
1040 end loop;
1042 -- Scan statements
1044 Visit (Handled_Statement_Sequence (N));
1046 -- Restore current subprogram setting
1048 Current_Subprogram := Save_CS;
1049 end;
1051 -- Now at this level, return skipping the subprogram body
1052 -- descendants, since we already took care of them!
1054 return Skip;
1056 -- If we have a body stub, visit the associated subunit, which
1057 -- is a semantic descendant of the stub.
1059 when N_Body_Stub =>
1060 Visit (Library_Unit (N));
1062 -- A declaration of a wrapper package indicates a subprogram
1063 -- instance for which there is no explicit body. Enter the
1064 -- subprogram instance in the table.
1066 when N_Package_Declaration =>
1067 if Is_Wrapper_Package (Defining_Entity (N)) then
1068 Register_Subprogram
1069 (Related_Instance (Defining_Entity (N)), Empty);
1070 end if;
1072 -- Skip generic declarations
1074 when N_Generic_Declaration =>
1075 return Skip;
1077 -- Skip generic package body
1079 when N_Package_Body =>
1080 if Present (Corresponding_Spec (N))
1081 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
1082 then
1083 return Skip;
1084 end if;
1086 -- Pragmas and component declarations can be ignored
1088 when N_Component_Declaration
1089 | N_Pragma
1091 return Skip;
1093 -- Otherwise record an uplevel reference in a local identifier
1095 when others =>
1096 if Nkind (N) in N_Has_Entity
1097 and then Present (Entity (N))
1098 then
1099 Ent := Entity (N);
1101 -- Only interested in entities declared within our nest
1103 if not Is_Library_Level_Entity (Ent)
1104 and then Scope_Within_Or_Same (Scope (Ent), Subp)
1106 -- Skip entities defined in inlined subprograms
1108 and then
1109 Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
1111 -- Constants and variables are potentially uplevel
1112 -- references to global declarations.
1114 and then
1115 (Ekind_In (Ent, E_Constant,
1116 E_Loop_Parameter,
1117 E_Variable)
1119 -- Formals are interesting, but not if being used
1120 -- as mere names of parameters for name notation
1121 -- calls.
1123 or else
1124 (Is_Formal (Ent)
1125 and then not
1126 (Nkind (Parent (N)) = N_Parameter_Association
1127 and then Selector_Name (Parent (N)) = N))
1129 -- Types other than known Is_Static types are
1130 -- potentially interesting.
1132 or else
1133 (Is_Type (Ent) and then not Is_Static_Type (Ent)))
1134 then
1135 -- Here we have a potentially interesting uplevel
1136 -- reference to examine.
1138 if Is_Type (Ent) then
1139 declare
1140 DT : Boolean := False;
1142 begin
1143 Check_Static_Type (Ent, N, DT);
1145 if Is_Static_Type (Ent) then
1146 return OK;
1147 end if;
1148 end;
1149 end if;
1151 Caller := Current_Subprogram;
1152 Callee := Enclosing_Subprogram (Ent);
1154 if Callee /= Caller
1155 and then (not Is_Static_Type (Ent)
1156 or else Needs_Fat_Pointer (Ent))
1157 then
1158 Note_Uplevel_Ref (Ent, N, Caller, Callee);
1160 -- Check the type of a formal parameter of the current
1161 -- subprogram, whose formal type may be an uplevel
1162 -- reference.
1164 elsif Is_Formal (Ent)
1165 and then Scope (Ent) = Current_Subprogram
1166 then
1167 declare
1168 DT : Boolean := False;
1170 begin
1171 Check_Static_Type (Etype (Ent), Empty, DT);
1172 end;
1173 end if;
1174 end if;
1175 end if;
1176 end case;
1178 -- Fall through to continue scanning children of this node
1180 return OK;
1181 end Visit_Node;
1183 -- Start of processing for Build_Tables
1185 begin
1186 -- Traverse the body to get subprograms, calls and uplevel references
1188 Visit (Subp_Body);
1189 end Build_Tables;
1191 -- Now do the first transitive closure which determines which
1192 -- subprograms in the nest are actually reachable.
1194 Reachable_Closure : declare
1195 Modified : Boolean;
1197 begin
1198 Subps.Table (Subps_First).Reachable := True;
1200 -- We use a simple minded algorithm as follows (obviously this can
1201 -- be done more efficiently, using one of the standard algorithms
1202 -- for efficient transitive closure computation, but this is simple
1203 -- and most likely fast enough that its speed does not matter).
1205 -- Repeatedly scan the list of calls. Any time we find a call from
1206 -- A to B, where A is reachable, but B is not, then B is reachable,
1207 -- and note that we have made a change by setting Modified True. We
1208 -- repeat this until we make a pass with no modifications.
1210 Outer : loop
1211 Modified := False;
1212 Inner : for J in Calls.First .. Calls.Last loop
1213 declare
1214 CTJ : Call_Entry renames Calls.Table (J);
1216 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1217 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1219 SUBF : Subp_Entry renames Subps.Table (SINF);
1220 SUBT : Subp_Entry renames Subps.Table (SINT);
1222 begin
1223 if SUBF.Reachable and then not SUBT.Reachable then
1224 SUBT.Reachable := True;
1225 Modified := True;
1226 end if;
1227 end;
1228 end loop Inner;
1230 exit Outer when not Modified;
1231 end loop Outer;
1232 end Reachable_Closure;
1234 -- Remove calls from unreachable subprograms
1236 declare
1237 New_Index : Nat;
1239 begin
1240 New_Index := 0;
1241 for J in Calls.First .. Calls.Last loop
1242 declare
1243 CTJ : Call_Entry renames Calls.Table (J);
1245 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1246 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1248 SUBF : Subp_Entry renames Subps.Table (SINF);
1249 SUBT : Subp_Entry renames Subps.Table (SINT);
1251 begin
1252 if SUBF.Reachable then
1253 pragma Assert (SUBT.Reachable);
1254 New_Index := New_Index + 1;
1255 Calls.Table (New_Index) := Calls.Table (J);
1256 end if;
1257 end;
1258 end loop;
1260 Calls.Set_Last (New_Index);
1261 end;
1263 -- Remove uplevel references from unreachable subprograms
1265 declare
1266 New_Index : Nat;
1268 begin
1269 New_Index := 0;
1270 for J in Urefs.First .. Urefs.Last loop
1271 declare
1272 URJ : Uref_Entry renames Urefs.Table (J);
1274 SINF : constant SI_Type := Subp_Index (URJ.Caller);
1275 SINT : constant SI_Type := Subp_Index (URJ.Callee);
1277 SUBF : Subp_Entry renames Subps.Table (SINF);
1278 SUBT : Subp_Entry renames Subps.Table (SINT);
1280 S : Entity_Id;
1282 begin
1283 -- Keep reachable reference
1285 if SUBF.Reachable then
1286 New_Index := New_Index + 1;
1287 Urefs.Table (New_Index) := Urefs.Table (J);
1289 -- And since we know we are keeping this one, this is a good
1290 -- place to fill in information for a good reference.
1292 -- Mark all enclosing subprograms need to declare AREC
1294 S := URJ.Caller;
1295 loop
1296 S := Enclosing_Subprogram (S);
1298 -- If we are at the top level, as can happen with
1299 -- references to formals in aspects of nested subprogram
1300 -- declarations, there are no further subprograms to mark
1301 -- as requiring activation records.
1303 exit when No (S);
1305 declare
1306 SUBI : Subp_Entry renames Subps.Table (Subp_Index (S));
1307 begin
1308 SUBI.Declares_AREC := True;
1310 -- If this entity was marked reachable because it is
1311 -- in a task or protected type, there may not appear
1312 -- to be any calls to it, which would normally adjust
1313 -- the levels of the parent subprograms. So we need to
1314 -- be sure that the uplevel reference of that entity
1315 -- takes into account possible calls.
1317 if In_Synchronized_Unit (SUBF.Ent)
1318 and then SUBT.Lev < SUBI.Uplevel_Ref
1319 then
1320 SUBI.Uplevel_Ref := SUBT.Lev;
1321 end if;
1322 end;
1324 exit when S = URJ.Callee;
1325 end loop;
1327 -- Add to list of uplevel referenced entities for Callee.
1328 -- We do not add types to this list, only actual references
1329 -- to objects that will be referenced uplevel, and we use
1330 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1331 -- duplicate entries in the list.
1332 -- Discriminants are also excluded, only the enclosing
1333 -- object can appear in the list.
1335 if not Is_Uplevel_Referenced_Entity (URJ.Ent)
1336 and then Ekind (URJ.Ent) /= E_Discriminant
1337 then
1338 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
1340 if not Is_Type (URJ.Ent) then
1341 Append_New_Elmt (URJ.Ent, SUBT.Uents);
1342 end if;
1343 end if;
1345 -- And set uplevel indication for caller
1347 if SUBT.Lev < SUBF.Uplevel_Ref then
1348 SUBF.Uplevel_Ref := SUBT.Lev;
1349 end if;
1350 end if;
1351 end;
1352 end loop;
1354 Urefs.Set_Last (New_Index);
1355 end;
1357 -- Remove unreachable subprograms from Subps table. Note that we do
1358 -- this after eliminating entries from the other two tables, since
1359 -- those elimination steps depend on referencing the Subps table.
1361 declare
1362 New_SI : SI_Type;
1364 begin
1365 New_SI := Subps_First - 1;
1366 for J in Subps_First .. Subps.Last loop
1367 declare
1368 STJ : Subp_Entry renames Subps.Table (J);
1369 Spec : Node_Id;
1370 Decl : Node_Id;
1372 begin
1373 -- Subprogram is reachable, copy and reset index
1375 if STJ.Reachable then
1376 New_SI := New_SI + 1;
1377 Subps.Table (New_SI) := STJ;
1378 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
1380 -- Subprogram is not reachable
1382 else
1383 -- Clear index, since no longer active
1385 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
1387 -- Output debug information if -gnatd.3 set
1389 if Debug_Flag_Dot_3 then
1390 Write_Str ("Eliminate ");
1391 Write_Name (Chars (Subps.Table (J).Ent));
1392 Write_Str (" at ");
1393 Write_Location (Sloc (Subps.Table (J).Ent));
1394 Write_Str (" (not referenced)");
1395 Write_Eol;
1396 end if;
1398 -- Rewrite declaration and body to null statements
1400 -- A subprogram instantiation does not have an explicit
1401 -- body. If unused, we could remove the corresponding
1402 -- wrapper package and its body (TBD).
1404 if Present (STJ.Bod) then
1405 Spec := Corresponding_Spec (STJ.Bod);
1407 if Present (Spec) then
1408 Decl := Parent (Declaration_Node (Spec));
1409 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
1410 end if;
1412 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
1413 end if;
1414 end if;
1415 end;
1416 end loop;
1418 Subps.Set_Last (New_SI);
1419 end;
1421 -- Now it is time for the second transitive closure, which follows calls
1422 -- and makes sure that A calls B, and B has uplevel references, then A
1423 -- is also marked as having uplevel references.
1425 Closure_Uplevel : declare
1426 Modified : Boolean;
1428 begin
1429 -- We use a simple minded algorithm as follows (obviously this can
1430 -- be done more efficiently, using one of the standard algorithms
1431 -- for efficient transitive closure computation, but this is simple
1432 -- and most likely fast enough that its speed does not matter).
1434 -- Repeatedly scan the list of calls. Any time we find a call from
1435 -- A to B, where B has uplevel references, make sure that A is marked
1436 -- as having at least the same level of uplevel referencing.
1438 Outer2 : loop
1439 Modified := False;
1440 Inner2 : for J in Calls.First .. Calls.Last loop
1441 declare
1442 CTJ : Call_Entry renames Calls.Table (J);
1443 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1444 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1445 SUBF : Subp_Entry renames Subps.Table (SINF);
1446 SUBT : Subp_Entry renames Subps.Table (SINT);
1447 begin
1448 if SUBT.Lev > SUBT.Uplevel_Ref
1449 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
1450 then
1451 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
1452 Modified := True;
1453 end if;
1454 end;
1455 end loop Inner2;
1457 exit Outer2 when not Modified;
1458 end loop Outer2;
1459 end Closure_Uplevel;
1461 -- We have one more step before the tables are complete. An uplevel
1462 -- call from subprogram A to subprogram B where subprogram B has uplevel
1463 -- references is in effect an uplevel reference, and must arrange for
1464 -- the proper activation link to be passed.
1466 for J in Calls.First .. Calls.Last loop
1467 declare
1468 CTJ : Call_Entry renames Calls.Table (J);
1470 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1471 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1473 SUBF : Subp_Entry renames Subps.Table (SINF);
1474 SUBT : Subp_Entry renames Subps.Table (SINT);
1476 A : Entity_Id;
1478 begin
1479 -- If callee has uplevel references
1481 if SUBT.Uplevel_Ref < SUBT.Lev
1483 -- And this is an uplevel call
1485 and then SUBT.Lev < SUBF.Lev
1486 then
1487 -- We need to arrange for finding the uplink
1489 A := CTJ.Caller;
1490 loop
1491 A := Enclosing_Subprogram (A);
1492 Subps.Table (Subp_Index (A)).Declares_AREC := True;
1493 exit when A = CTJ.Callee;
1495 -- In any case exit when we get to the outer level. This
1496 -- happens in some odd cases with generics (in particular
1497 -- sem_ch3.adb does not compile without this kludge ???).
1499 exit when A = Subp;
1500 end loop;
1501 end if;
1502 end;
1503 end loop;
1505 -- The tables are now complete, so we can record the last index in the
1506 -- Subps table for later reference in Cprint.
1508 Subps.Table (Subps_First).Last := Subps.Last;
1510 -- Next step, create the entities for code we will insert. We do this
1511 -- at the start so that all the entities are defined, regardless of the
1512 -- order in which we do the code insertions.
1514 Create_Entities : for J in Subps_First .. Subps.Last loop
1515 declare
1516 STJ : Subp_Entry renames Subps.Table (J);
1517 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1519 begin
1520 -- First we create the ARECnF entity for the additional formal for
1521 -- all subprograms which need an activation record passed.
1523 if STJ.Uplevel_Ref < STJ.Lev then
1524 STJ.ARECnF :=
1525 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
1526 end if;
1528 -- Define the AREC entities for the activation record if needed
1530 if STJ.Declares_AREC then
1531 STJ.ARECn :=
1532 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
1533 STJ.ARECnT :=
1534 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
1535 STJ.ARECnPT :=
1536 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
1537 STJ.ARECnP :=
1538 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
1540 -- Define uplink component entity if inner nesting case
1542 if Present (STJ.ARECnF) then
1543 STJ.ARECnU :=
1544 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
1545 end if;
1546 end if;
1547 end;
1548 end loop Create_Entities;
1550 -- Loop through subprograms
1552 Subp_Loop : declare
1553 Addr : constant Entity_Id := RTE (RE_Address);
1555 begin
1556 for J in Subps_First .. Subps.Last loop
1557 declare
1558 STJ : Subp_Entry renames Subps.Table (J);
1560 begin
1561 -- First add the extra formal if needed. This applies to all
1562 -- nested subprograms that require an activation record to be
1563 -- passed, as indicated by ARECnF being defined.
1565 if Present (STJ.ARECnF) then
1567 -- Here we need the extra formal. We do the expansion and
1568 -- analysis of this manually, since it is fairly simple,
1569 -- and it is not obvious how we can get what we want if we
1570 -- try to use the normal Analyze circuit.
1572 Add_Extra_Formal : declare
1573 Encl : constant SI_Type := Enclosing_Subp (J);
1574 STJE : Subp_Entry renames Subps.Table (Encl);
1575 -- Index and Subp_Entry for enclosing routine
1577 Form : constant Entity_Id := STJ.ARECnF;
1578 -- The formal to be added. Note that n here is one less
1579 -- than the level of the subprogram itself (STJ.Ent).
1581 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1582 -- S is an N_Function/Procedure_Specification node, and F
1583 -- is the new entity to add to this subprogramn spec as
1584 -- the last Extra_Formal.
1586 ----------------------
1587 -- Add_Form_To_Spec --
1588 ----------------------
1590 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1591 Sub : constant Entity_Id := Defining_Entity (S);
1592 Ent : Entity_Id;
1594 begin
1595 -- Case of at least one Extra_Formal is present, set
1596 -- ARECnF as the new last entry in the list.
1598 if Present (Extra_Formals (Sub)) then
1599 Ent := Extra_Formals (Sub);
1600 while Present (Extra_Formal (Ent)) loop
1601 Ent := Extra_Formal (Ent);
1602 end loop;
1604 Set_Extra_Formal (Ent, F);
1606 -- No Extra formals present
1608 else
1609 Set_Extra_Formals (Sub, F);
1610 Ent := Last_Formal (Sub);
1612 if Present (Ent) then
1613 Set_Extra_Formal (Ent, F);
1614 end if;
1615 end if;
1616 end Add_Form_To_Spec;
1618 -- Start of processing for Add_Extra_Formal
1620 begin
1621 -- Decorate the new formal entity
1623 Set_Scope (Form, STJ.Ent);
1624 Set_Ekind (Form, E_In_Parameter);
1625 Set_Etype (Form, STJE.ARECnPT);
1626 Set_Mechanism (Form, By_Copy);
1627 Set_Never_Set_In_Source (Form, True);
1628 Set_Analyzed (Form, True);
1629 Set_Comes_From_Source (Form, False);
1630 Set_Is_Activation_Record (Form, True);
1632 -- Case of only body present
1634 if Acts_As_Spec (STJ.Bod) then
1635 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1637 -- Case of separate spec
1639 else
1640 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1641 end if;
1642 end Add_Extra_Formal;
1643 end if;
1645 -- Processing for subprograms that declare an activation record
1647 if Present (STJ.ARECn) then
1649 -- Local declarations for one such subprogram
1651 declare
1652 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1654 Decls : constant List_Id := New_List;
1655 -- List of new declarations we create
1657 Clist : List_Id;
1658 Comp : Entity_Id;
1660 Decl_Assign : Node_Id;
1661 -- Assigment to set uplink, Empty if none
1663 Decl_ARECnT : Node_Id;
1664 Decl_ARECnPT : Node_Id;
1665 Decl_ARECn : Node_Id;
1666 Decl_ARECnP : Node_Id;
1667 -- Declaration nodes for the AREC entities we build
1669 begin
1670 -- Build list of component declarations for ARECnT
1672 Clist := Empty_List;
1674 -- If we are in a subprogram that has a static link that
1675 -- is passed in (as indicated by ARECnF being defined),
1676 -- then include ARECnU : ARECmPT where ARECmPT comes from
1677 -- the level one higher than the current level, and the
1678 -- entity ARECnPT comes from the enclosing subprogram.
1680 if Present (STJ.ARECnF) then
1681 declare
1682 STJE : Subp_Entry
1683 renames Subps.Table (Enclosing_Subp (J));
1684 begin
1685 Append_To (Clist,
1686 Make_Component_Declaration (Loc,
1687 Defining_Identifier => STJ.ARECnU,
1688 Component_Definition =>
1689 Make_Component_Definition (Loc,
1690 Subtype_Indication =>
1691 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1692 end;
1693 end if;
1695 -- Add components for uplevel referenced entities
1697 if Present (STJ.Uents) then
1698 declare
1699 Elmt : Elmt_Id;
1700 Ptr_Decl : Node_Id;
1701 Uent : Entity_Id;
1703 Indx : Nat;
1704 -- 1's origin of index in list of elements. This is
1705 -- used to uniquify names if needed in Upref_Name.
1707 begin
1708 Elmt := First_Elmt (STJ.Uents);
1709 Indx := 0;
1710 while Present (Elmt) loop
1711 Uent := Node (Elmt);
1712 Indx := Indx + 1;
1714 Comp :=
1715 Make_Defining_Identifier (Loc,
1716 Chars => Upref_Name (Uent, Indx, Clist));
1718 Set_Activation_Record_Component
1719 (Uent, Comp);
1721 if Needs_Fat_Pointer (Uent) then
1723 -- Build corresponding access type
1725 Ptr_Decl :=
1726 Build_Access_Type_Decl
1727 (Etype (Uent), STJ.Ent);
1728 Append_To (Decls, Ptr_Decl);
1730 -- And use its type in the corresponding
1731 -- component.
1733 Append_To (Clist,
1734 Make_Component_Declaration (Loc,
1735 Defining_Identifier => Comp,
1736 Component_Definition =>
1737 Make_Component_Definition (Loc,
1738 Subtype_Indication =>
1739 New_Occurrence_Of
1740 (Defining_Identifier (Ptr_Decl),
1741 Loc))));
1742 else
1743 Append_To (Clist,
1744 Make_Component_Declaration (Loc,
1745 Defining_Identifier => Comp,
1746 Component_Definition =>
1747 Make_Component_Definition (Loc,
1748 Subtype_Indication =>
1749 New_Occurrence_Of (Addr, Loc))));
1750 end if;
1751 Next_Elmt (Elmt);
1752 end loop;
1753 end;
1754 end if;
1756 -- Now we can insert the AREC declarations into the body
1757 -- type ARECnT is record .. end record;
1758 -- pragma Suppress_Initialization (ARECnT);
1760 -- Note that we need to set the Suppress_Initialization
1761 -- flag after Decl_ARECnT has been analyzed.
1763 Decl_ARECnT :=
1764 Make_Full_Type_Declaration (Loc,
1765 Defining_Identifier => STJ.ARECnT,
1766 Type_Definition =>
1767 Make_Record_Definition (Loc,
1768 Component_List =>
1769 Make_Component_List (Loc,
1770 Component_Items => Clist)));
1771 Append_To (Decls, Decl_ARECnT);
1773 -- type ARECnPT is access all ARECnT;
1775 Decl_ARECnPT :=
1776 Make_Full_Type_Declaration (Loc,
1777 Defining_Identifier => STJ.ARECnPT,
1778 Type_Definition =>
1779 Make_Access_To_Object_Definition (Loc,
1780 All_Present => True,
1781 Subtype_Indication =>
1782 New_Occurrence_Of (STJ.ARECnT, Loc)));
1783 Append_To (Decls, Decl_ARECnPT);
1785 -- ARECn : aliased ARECnT;
1787 Decl_ARECn :=
1788 Make_Object_Declaration (Loc,
1789 Defining_Identifier => STJ.ARECn,
1790 Aliased_Present => True,
1791 Object_Definition =>
1792 New_Occurrence_Of (STJ.ARECnT, Loc));
1793 Append_To (Decls, Decl_ARECn);
1795 -- ARECnP : constant ARECnPT := ARECn'Access;
1797 Decl_ARECnP :=
1798 Make_Object_Declaration (Loc,
1799 Defining_Identifier => STJ.ARECnP,
1800 Constant_Present => True,
1801 Object_Definition =>
1802 New_Occurrence_Of (STJ.ARECnPT, Loc),
1803 Expression =>
1804 Make_Attribute_Reference (Loc,
1805 Prefix =>
1806 New_Occurrence_Of (STJ.ARECn, Loc),
1807 Attribute_Name => Name_Access));
1808 Append_To (Decls, Decl_ARECnP);
1810 -- If we are in a subprogram that has a static link that
1811 -- is passed in (as indicated by ARECnF being defined),
1812 -- then generate ARECn.ARECmU := ARECmF where m is
1813 -- one less than the current level to set the uplink.
1815 if Present (STJ.ARECnF) then
1816 Decl_Assign :=
1817 Make_Assignment_Statement (Loc,
1818 Name =>
1819 Make_Selected_Component (Loc,
1820 Prefix =>
1821 New_Occurrence_Of (STJ.ARECn, Loc),
1822 Selector_Name =>
1823 New_Occurrence_Of (STJ.ARECnU, Loc)),
1824 Expression =>
1825 New_Occurrence_Of (STJ.ARECnF, Loc));
1826 Append_To (Decls, Decl_Assign);
1828 else
1829 Decl_Assign := Empty;
1830 end if;
1832 Prepend_List_To (Declarations (STJ.Bod), Decls);
1834 -- Analyze the newly inserted declarations. Note that we
1835 -- do not need to establish the whole scope stack, since
1836 -- we have already set all entity fields (so there will
1837 -- be no searching of upper scopes to resolve names). But
1838 -- we do set the scope of the current subprogram, so that
1839 -- newly created entities go in the right entity chain.
1841 -- We analyze with all checks suppressed (since we do
1842 -- not expect any exceptions).
1844 Push_Scope (STJ.Ent);
1845 Analyze (Decl_ARECnT, Suppress => All_Checks);
1847 -- Note that we need to call Set_Suppress_Initialization
1848 -- after Decl_ARECnT has been analyzed, but before
1849 -- analyzing Decl_ARECnP so that the flag is properly
1850 -- taking into account.
1852 Set_Suppress_Initialization (STJ.ARECnT);
1854 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1855 Analyze (Decl_ARECn, Suppress => All_Checks);
1856 Analyze (Decl_ARECnP, Suppress => All_Checks);
1858 if Present (Decl_Assign) then
1859 Analyze (Decl_Assign, Suppress => All_Checks);
1860 end if;
1862 Pop_Scope;
1864 -- Next step, for each uplevel referenced entity, add
1865 -- assignment operations to set the component in the
1866 -- activation record.
1868 if Present (STJ.Uents) then
1869 declare
1870 Elmt : Elmt_Id;
1872 begin
1873 Elmt := First_Elmt (STJ.Uents);
1874 while Present (Elmt) loop
1875 declare
1876 Ent : constant Entity_Id := Node (Elmt);
1877 Loc : constant Source_Ptr := Sloc (Ent);
1878 Dec : constant Node_Id :=
1879 Declaration_Node (Ent);
1881 Asn : Node_Id;
1882 Attr : Name_Id;
1883 Ins : Node_Id;
1885 begin
1886 -- For parameters, we insert the assignment
1887 -- right after the declaration of ARECnP.
1888 -- For all other entities, we insert the
1889 -- assignment immediately after the
1890 -- declaration of the entity or after the
1891 -- freeze node if present.
1893 -- Note: we don't need to mark the entity
1894 -- as being aliased, because the address
1895 -- attribute will mark it as Address_Taken,
1896 -- and that is good enough.
1898 if Is_Formal (Ent) then
1899 Ins := Decl_ARECnP;
1901 elsif Has_Delayed_Freeze (Ent) then
1902 Ins := Freeze_Node (Ent);
1904 else
1905 Ins := Dec;
1906 end if;
1908 -- Build and insert the assignment:
1909 -- ARECn.nam := nam'Address
1910 -- or else 'Access for unconstrained array
1912 if Needs_Fat_Pointer (Ent) then
1913 Attr := Name_Access;
1914 else
1915 Attr := Name_Address;
1916 end if;
1918 Asn :=
1919 Make_Assignment_Statement (Loc,
1920 Name =>
1921 Make_Selected_Component (Loc,
1922 Prefix =>
1923 New_Occurrence_Of (STJ.ARECn, Loc),
1924 Selector_Name =>
1925 New_Occurrence_Of
1926 (Activation_Record_Component
1927 (Ent),
1928 Loc)),
1930 Expression =>
1931 Make_Attribute_Reference (Loc,
1932 Prefix =>
1933 New_Occurrence_Of (Ent, Loc),
1934 Attribute_Name => Attr));
1936 -- If we have a loop parameter, we have
1937 -- to insert before the first statement
1938 -- of the loop. Ins points to the
1939 -- N_Loop_Parameter_Specification.
1941 if Ekind (Ent) = E_Loop_Parameter then
1942 Ins :=
1943 First
1944 (Statements (Parent (Parent (Ins))));
1945 Insert_Before (Ins, Asn);
1947 else
1948 Insert_After (Ins, Asn);
1949 end if;
1951 -- Analyze the assignment statement. We do
1952 -- not need to establish the relevant scope
1953 -- stack entries here, because we have
1954 -- already set the correct entity references,
1955 -- so no name resolution is required, and no
1956 -- new entities are created, so we don't even
1957 -- need to set the current scope.
1959 -- We analyze with all checks suppressed
1960 -- (since we do not expect any exceptions).
1962 Analyze (Asn, Suppress => All_Checks);
1963 end;
1965 Next_Elmt (Elmt);
1966 end loop;
1967 end;
1968 end if;
1969 end;
1970 end if;
1971 end;
1972 end loop;
1973 end Subp_Loop;
1975 -- Next step, process uplevel references. This has to be done in a
1976 -- separate pass, after completing the processing in Sub_Loop because we
1977 -- need all the AREC declarations generated, inserted, and analyzed so
1978 -- that the uplevel references can be successfully analyzed.
1980 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
1981 declare
1982 UPJ : Uref_Entry renames Urefs.Table (J);
1984 begin
1985 -- Ignore type references, these are implicit references that do
1986 -- not need rewriting (e.g. the appearence in a conversion).
1987 -- Also ignore if no reference was specified or if the rewriting
1988 -- has already been done (this can happen if the N_Identifier
1989 -- occurs more than one time in the tree).
1990 -- Also ignore uplevel references to bounds of types that come
1991 -- from the original type reference.
1993 if Is_Type (UPJ.Ent)
1994 or else No (UPJ.Ref)
1995 or else not Is_Entity_Name (UPJ.Ref)
1996 or else not Present (Entity (UPJ.Ref))
1997 or else Is_Type (Entity (UPJ.Ref))
1998 then
1999 goto Continue;
2000 end if;
2002 -- Also ignore uplevel references to bounds of types that come
2003 -- from the original type reference.
2005 if Is_Entity_Name (UPJ.Ref)
2006 and then Present (Entity (UPJ.Ref))
2007 and then Is_Type (Entity (UPJ.Ref))
2008 then
2009 goto Continue;
2010 end if;
2012 -- Rewrite one reference
2014 Rewrite_One_Ref : declare
2015 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
2016 -- Source location for the reference
2018 Typ : constant Entity_Id := Etype (UPJ.Ent);
2019 -- The type of the referenced entity
2021 Atyp : Entity_Id;
2022 -- The actual subtype of the reference
2024 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
2025 -- Subp_Index for caller containing reference
2027 STJR : Subp_Entry renames Subps.Table (RS_Caller);
2028 -- Subp_Entry for subprogram containing reference
2030 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
2031 -- Subp_Index for subprogram containing referenced entity
2033 STJE : Subp_Entry renames Subps.Table (RS_Callee);
2034 -- Subp_Entry for subprogram containing referenced entity
2036 Pfx : Node_Id;
2037 Comp : Entity_Id;
2038 SI : SI_Type;
2040 begin
2041 Atyp := Etype (UPJ.Ref);
2043 if Ekind (Atyp) /= E_Record_Subtype then
2044 Atyp := Get_Actual_Subtype (UPJ.Ref);
2045 end if;
2047 -- Ignore if no ARECnF entity for enclosing subprogram which
2048 -- probably happens as a result of not properly treating
2049 -- instance bodies. To be examined ???
2051 -- If this test is omitted, then the compilation of freeze.adb
2052 -- and inline.adb fail in unnesting mode.
2054 if No (STJR.ARECnF) then
2055 goto Continue;
2056 end if;
2058 -- Push the current scope, so that the pointer type Tnn, and
2059 -- any subsidiary entities resulting from the analysis of the
2060 -- rewritten reference, go in the right entity chain.
2062 Push_Scope (STJR.Ent);
2064 -- Now we need to rewrite the reference. We have a reference
2065 -- from level STJR.Lev to level STJE.Lev. The general form of
2066 -- the rewritten reference for entity X is:
2068 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
2070 -- where a,b,c,d .. m =
2071 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
2073 pragma Assert (STJR.Lev > STJE.Lev);
2075 -- Compute the prefix of X. Here are examples to make things
2076 -- clear (with parens to show groupings, the prefix is
2077 -- everything except the .X at the end).
2079 -- level 2 to level 1
2081 -- AREC1F.X
2083 -- level 3 to level 1
2085 -- (AREC2F.AREC1U).X
2087 -- level 4 to level 1
2089 -- ((AREC3F.AREC2U).AREC1U).X
2091 -- level 6 to level 2
2093 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
2095 -- In the above, ARECnF and ARECnU are pointers, so there are
2096 -- explicit dereferences required for these occurrences.
2098 Pfx :=
2099 Make_Explicit_Dereference (Loc,
2100 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
2101 SI := RS_Caller;
2102 for L in STJE.Lev .. STJR.Lev - 2 loop
2103 SI := Enclosing_Subp (SI);
2104 Pfx :=
2105 Make_Explicit_Dereference (Loc,
2106 Prefix =>
2107 Make_Selected_Component (Loc,
2108 Prefix => Pfx,
2109 Selector_Name =>
2110 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
2111 end loop;
2113 -- Get activation record component (must exist)
2115 Comp := Activation_Record_Component (UPJ.Ent);
2116 pragma Assert (Present (Comp));
2118 -- Do the replacement. If the component type is an access type,
2119 -- this is an uplevel reference for an entity that requires a
2120 -- fat pointer, so dereference the component.
2122 if Is_Access_Type (Etype (Comp)) then
2123 Rewrite (UPJ.Ref,
2124 Make_Explicit_Dereference (Loc,
2125 Prefix =>
2126 Make_Selected_Component (Loc,
2127 Prefix => Pfx,
2128 Selector_Name =>
2129 New_Occurrence_Of (Comp, Loc))));
2131 else
2132 Rewrite (UPJ.Ref,
2133 Make_Attribute_Reference (Loc,
2134 Prefix => New_Occurrence_Of (Atyp, Loc),
2135 Attribute_Name => Name_Deref,
2136 Expressions => New_List (
2137 Make_Selected_Component (Loc,
2138 Prefix => Pfx,
2139 Selector_Name =>
2140 New_Occurrence_Of (Comp, Loc)))));
2141 end if;
2143 -- Analyze and resolve the new expression. We do not need to
2144 -- establish the relevant scope stack entries here, because we
2145 -- have already set all the correct entity references, so no
2146 -- name resolution is needed. We have already set the current
2147 -- scope, so that any new entities created will be in the right
2148 -- scope.
2150 -- We analyze with all checks suppressed (since we do not
2151 -- expect any exceptions)
2153 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
2154 Pop_Scope;
2155 end Rewrite_One_Ref;
2156 end;
2158 <<Continue>>
2159 null;
2160 end loop Uplev_Refs;
2162 -- Finally, loop through all calls adding extra actual for the
2163 -- activation record where it is required.
2165 Adjust_Calls : for J in Calls.First .. Calls.Last loop
2167 -- Process a single call, we are only interested in a call to a
2168 -- subprogram that actually needs a pointer to an activation record,
2169 -- as indicated by the ARECnF entity being set. This excludes the
2170 -- top level subprogram, and any subprogram not having uplevel refs.
2172 Adjust_One_Call : declare
2173 CTJ : Call_Entry renames Calls.Table (J);
2174 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
2175 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
2177 Loc : constant Source_Ptr := Sloc (CTJ.N);
2179 Extra : Node_Id;
2180 ExtraP : Node_Id;
2181 SubX : SI_Type;
2182 Act : Node_Id;
2184 begin
2185 if Present (STT.ARECnF)
2186 and then Nkind (CTJ.N) in N_Subprogram_Call
2187 then
2188 -- CTJ.N is a call to a subprogram which may require a pointer
2189 -- to an activation record. The subprogram containing the call
2190 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2191 -- have a call from level STF.Lev to level STT.Lev.
2193 -- There are three possibilities:
2195 -- For a call to the same level, we just pass the activation
2196 -- record passed to the calling subprogram.
2198 if STF.Lev = STT.Lev then
2199 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2201 -- For a call that goes down a level, we pass a pointer to the
2202 -- activation record constructed within the caller (which may
2203 -- be the outer-level subprogram, but also may be a more deeply
2204 -- nested caller).
2206 elsif STT.Lev = STF.Lev + 1 then
2207 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
2209 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2210 -- since it is not possible to do a downcall of more than
2211 -- one level.
2213 -- For a call from level STF.Lev to level STT.Lev, we
2214 -- have to find the activation record needed by the
2215 -- callee. This is as follows:
2217 -- ARECaF.ARECbU.ARECcU....ARECmU
2219 -- where a,b,c .. m =
2220 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2222 else
2223 pragma Assert (STT.Lev < STF.Lev);
2225 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2226 SubX := Subp_Index (CTJ.Caller);
2227 for K in reverse STT.Lev .. STF.Lev - 1 loop
2228 SubX := Enclosing_Subp (SubX);
2229 Extra :=
2230 Make_Selected_Component (Loc,
2231 Prefix => Extra,
2232 Selector_Name =>
2233 New_Occurrence_Of
2234 (Subps.Table (SubX).ARECnU, Loc));
2235 end loop;
2236 end if;
2238 -- Extra is the additional parameter to be added. Build a
2239 -- parameter association that we can append to the actuals.
2241 ExtraP :=
2242 Make_Parameter_Association (Loc,
2243 Selector_Name =>
2244 New_Occurrence_Of (STT.ARECnF, Loc),
2245 Explicit_Actual_Parameter => Extra);
2247 if No (Parameter_Associations (CTJ.N)) then
2248 Set_Parameter_Associations (CTJ.N, Empty_List);
2249 end if;
2251 Append (ExtraP, Parameter_Associations (CTJ.N));
2253 -- We need to deal with the actual parameter chain as well. The
2254 -- newly added parameter is always the last actual.
2256 Act := First_Named_Actual (CTJ.N);
2258 if No (Act) then
2259 Set_First_Named_Actual (CTJ.N, Extra);
2261 -- If call has been relocated (as with an expression in
2262 -- an aggregate), set First_Named pointer in original node
2263 -- as well, because that's the parent of the parameter list.
2265 Set_First_Named_Actual
2266 (Parent (List_Containing (ExtraP)), Extra);
2268 -- Here we must follow the chain and append the new entry
2270 else
2271 loop
2272 declare
2273 PAN : Node_Id;
2274 NNA : Node_Id;
2276 begin
2277 PAN := Parent (Act);
2278 pragma Assert (Nkind (PAN) = N_Parameter_Association);
2279 NNA := Next_Named_Actual (PAN);
2281 if No (NNA) then
2282 Set_Next_Named_Actual (PAN, Extra);
2283 exit;
2284 end if;
2286 Act := NNA;
2287 end;
2288 end loop;
2289 end if;
2291 -- Analyze and resolve the new actual. We do not need to
2292 -- establish the relevant scope stack entries here, because
2293 -- we have already set all the correct entity references, so
2294 -- no name resolution is needed.
2296 -- We analyze with all checks suppressed (since we do not
2297 -- expect any exceptions, and also we temporarily turn off
2298 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2299 -- references (not needed at this stage, and in fact causes
2300 -- a bit of recursive chaos).
2302 Opt.Unnest_Subprogram_Mode := False;
2303 Analyze_And_Resolve
2304 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
2305 Opt.Unnest_Subprogram_Mode := True;
2306 end if;
2307 end Adjust_One_Call;
2308 end loop Adjust_Calls;
2310 return;
2311 end Unnest_Subprogram;
2313 ------------------------
2314 -- Unnest_Subprograms --
2315 ------------------------
2317 procedure Unnest_Subprograms (N : Node_Id) is
2318 function Search_Subprograms (N : Node_Id) return Traverse_Result;
2319 -- Tree visitor that search for outer level procedures with nested
2320 -- subprograms and invokes Unnest_Subprogram()
2322 ---------------
2323 -- Do_Search --
2324 ---------------
2326 procedure Do_Search is new Traverse_Proc (Search_Subprograms);
2327 -- Subtree visitor instantiation
2329 ------------------------
2330 -- Search_Subprograms --
2331 ------------------------
2333 function Search_Subprograms (N : Node_Id) return Traverse_Result is
2334 begin
2335 if Nkind_In (N, N_Subprogram_Body, N_Subprogram_Body_Stub) then
2336 declare
2337 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
2339 begin
2340 -- We are only interested in subprograms (not generic
2341 -- subprograms), that have nested subprograms.
2343 if Is_Subprogram (Spec_Id)
2344 and then Has_Nested_Subprogram (Spec_Id)
2345 and then Is_Library_Level_Entity (Spec_Id)
2346 then
2347 Unnest_Subprogram (Spec_Id, N);
2348 end if;
2349 end;
2350 end if;
2352 -- The proper body of a stub may contain nested subprograms, and
2353 -- therefore must be visited explicitly. Nested stubs are examined
2354 -- recursively in Visit_Node.
2356 if Nkind (N) in N_Body_Stub then
2357 Do_Search (Library_Unit (N));
2358 end if;
2360 return OK;
2361 end Search_Subprograms;
2363 -- Start of processing for Unnest_Subprograms
2365 begin
2366 if not Opt.Unnest_Subprogram_Mode or not Opt.Expander_Active then
2367 return;
2368 end if;
2370 -- A specification will contain bodies if it contains instantiations so
2371 -- examine package or subprogram declaration of the main unit, when it
2372 -- is present.
2374 if Nkind (Unit (N)) = N_Package_Body
2375 or else (Nkind (Unit (N)) = N_Subprogram_Body
2376 and then not Acts_As_Spec (N))
2377 then
2378 Do_Search (Library_Unit (N));
2379 end if;
2381 Do_Search (N);
2382 end Unnest_Subprograms;
2384 end Exp_Unst;