Remove not needed __builtin_expect due to malloc predictor.
[official-gcc.git] / gcc / ada / exp_unst.adb
blobc5b03c4100d519b81fda97299a09ef099a5f31ab
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 -- The body of a protected operation has a different name and
263 -- has been scanned at this point, and thus has an entry in
264 -- the subprogram table.
266 if E = Sub
267 and then Convention (E) = Convention_Protected
268 then
269 E := Protected_Body_Subprogram (E);
270 end if;
272 if Ekind (E) = E_Function
273 and then Rewritten_For_C (E)
274 and then Present (Corresponding_Procedure (E))
275 then
276 E := Corresponding_Procedure (E);
277 end if;
278 end if;
280 pragma Assert (Subps_Index (E) /= Uint_0);
281 return SI_Type (UI_To_Int (Subps_Index (E)));
282 end Subp_Index;
284 -----------------------
285 -- Unnest_Subprogram --
286 -----------------------
288 procedure Unnest_Subprogram (Subp : Entity_Id; Subp_Body : Node_Id) is
289 function AREC_Name (J : Pos; S : String) return Name_Id;
290 -- Returns name for string ARECjS, where j is the decimal value of j
292 function Enclosing_Subp (Subp : SI_Type) return SI_Type;
293 -- Subp is the index of a subprogram which has a Lev greater than 1.
294 -- This function returns the index of the enclosing subprogram which
295 -- will have a Lev value one less than this.
297 function Img_Pos (N : Pos) return String;
298 -- Return image of N without leading blank
300 function Upref_Name
301 (Ent : Entity_Id;
302 Index : Pos;
303 Clist : List_Id) return Name_Id;
304 -- This function returns the name to be used in the activation record to
305 -- reference the variable uplevel. Clist is the list of components that
306 -- have been created in the activation record so far. Normally the name
307 -- is just a copy of the Chars field of the entity. The exception is
308 -- when the name has already been used, in which case we suffix the name
309 -- with the index value Index to avoid duplication. This happens with
310 -- declare blocks and generic parameters at least.
312 ---------------
313 -- AREC_Name --
314 ---------------
316 function AREC_Name (J : Pos; S : String) return Name_Id is
317 begin
318 return Name_Find ("AREC" & Img_Pos (J) & S);
319 end AREC_Name;
321 --------------------
322 -- Enclosing_Subp --
323 --------------------
325 function Enclosing_Subp (Subp : SI_Type) return SI_Type is
326 STJ : Subp_Entry renames Subps.Table (Subp);
327 Ret : constant SI_Type := Subp_Index (Enclosing_Subprogram (STJ.Ent));
328 begin
329 pragma Assert (STJ.Lev > 1);
330 pragma Assert (Subps.Table (Ret).Lev = STJ.Lev - 1);
331 return Ret;
332 end Enclosing_Subp;
334 -------------
335 -- Img_Pos --
336 -------------
338 function Img_Pos (N : Pos) return String is
339 Buf : String (1 .. 20);
340 Ptr : Natural;
341 NV : Nat;
343 begin
344 Ptr := Buf'Last;
345 NV := N;
346 while NV /= 0 loop
347 Buf (Ptr) := Character'Val (48 + NV mod 10);
348 Ptr := Ptr - 1;
349 NV := NV / 10;
350 end loop;
352 return Buf (Ptr + 1 .. Buf'Last);
353 end Img_Pos;
355 ----------------
356 -- Upref_Name --
357 ----------------
359 function Upref_Name
360 (Ent : Entity_Id;
361 Index : Pos;
362 Clist : List_Id) return Name_Id
364 C : Node_Id;
365 begin
366 C := First (Clist);
367 loop
368 if No (C) then
369 return Chars (Ent);
371 elsif Chars (Defining_Identifier (C)) = Chars (Ent) then
372 return
373 Name_Find (Get_Name_String (Chars (Ent)) & Img_Pos (Index));
374 else
375 Next (C);
376 end if;
377 end loop;
378 end Upref_Name;
380 -- Start of processing for Unnest_Subprogram
382 begin
383 -- Nothing to do inside a generic (all processing is for instance)
385 if Inside_A_Generic then
386 return;
387 end if;
389 -- If the main unit is a package body then we need to examine the spec
390 -- to determine whether the main unit is generic (the scope stack is not
391 -- present when this is called on the main unit).
393 if Ekind (Cunit_Entity (Main_Unit)) = E_Package_Body
394 and then Is_Generic_Unit (Spec_Entity (Cunit_Entity (Main_Unit)))
395 then
396 return;
397 end if;
399 -- Only unnest when generating code for the main source unit
401 if not In_Extended_Main_Code_Unit (Subp_Body) then
402 return;
403 end if;
405 -- This routine is called late, after the scope stack is gone. The
406 -- following creates a suitable dummy scope stack to be used for the
407 -- analyze/expand calls made from this routine.
409 Push_Scope (Subp);
411 -- First step, we must mark all nested subprograms that require a static
412 -- link (activation record) because either they contain explicit uplevel
413 -- references (as indicated by Is_Uplevel_Referenced_Entity being set at
414 -- this point), or they make calls to other subprograms in the same nest
415 -- that require a static link (in which case we set this flag).
417 -- This is a recursive definition, and to implement this, we have to
418 -- build a call graph for the set of nested subprograms, and then go
419 -- over this graph to implement recursively the invariant that if a
420 -- subprogram has a call to a subprogram requiring a static link, then
421 -- the calling subprogram requires a static link.
423 -- First populate the above tables
425 Subps_First := Subps.Last + 1;
426 Calls.Init;
427 Urefs.Init;
429 Build_Tables : declare
430 Current_Subprogram : Entity_Id;
431 -- When we scan a subprogram body, we set Current_Subprogram to the
432 -- corresponding entity. This gets recursively saved and restored.
434 function Visit_Node (N : Node_Id) return Traverse_Result;
435 -- Visit a single node in Subp
437 -----------
438 -- Visit --
439 -----------
441 procedure Visit is new Traverse_Proc (Visit_Node);
442 -- Used to traverse the body of Subp, populating the tables
444 ----------------
445 -- Visit_Node --
446 ----------------
448 function Visit_Node (N : Node_Id) return Traverse_Result is
449 Ent : Entity_Id;
450 Caller : Entity_Id;
451 Callee : Entity_Id;
453 procedure Check_Static_Type
454 (T : Entity_Id; N : Node_Id; DT : in out Boolean);
455 -- Given a type T, checks if it is a static type defined as a type
456 -- with no dynamic bounds in sight. If so, the only action is to
457 -- set Is_Static_Type True for T. If T is not a static type, then
458 -- all types with dynamic bounds associated with T are detected,
459 -- and their bounds are marked as uplevel referenced if not at the
460 -- library level, and DT is set True. If N is specified, it's the
461 -- node that will need to be replaced. If not specified, it means
462 -- we can't do a replacement because the bound is implicit.
464 procedure Note_Uplevel_Ref
465 (E : Entity_Id;
466 N : Node_Id;
467 Caller : Entity_Id;
468 Callee : Entity_Id);
469 -- Called when we detect an explicit or implicit uplevel reference
470 -- from within Caller to entity E declared in Callee. E can be a
471 -- an object or a type.
473 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id);
474 -- Enter a subprogram whose body is visible or which is a
475 -- subprogram instance into the subprogram table.
477 -----------------------
478 -- Check_Static_Type --
479 -----------------------
481 procedure Check_Static_Type
482 (T : Entity_Id; N : Node_Id; DT : in out Boolean)
484 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id);
485 -- N is the bound of a dynamic type. This procedure notes that
486 -- this bound is uplevel referenced, it can handle references
487 -- to entities (typically _FIRST and _LAST entities), and also
488 -- attribute references of the form T'name (name is typically
489 -- FIRST or LAST) where T is the uplevel referenced bound.
490 -- Ref, if Present, is the location of the reference to
491 -- replace.
493 ------------------------
494 -- Note_Uplevel_Bound --
495 ------------------------
497 procedure Note_Uplevel_Bound (N : Node_Id; Ref : Node_Id) is
498 begin
499 -- Entity name case. Make sure that the entity is declared
500 -- in a subprogram. This may not be the case for for a type
501 -- in a loop appearing in a precondition.
502 -- Exclude explicitly discriminants (that can appear
503 -- in bounds of discriminated components).
505 if Is_Entity_Name (N) then
506 if Present (Entity (N))
507 and then not Is_Type (Entity (N))
508 and then Present (Enclosing_Subprogram (Entity (N)))
509 and then Ekind (Entity (N)) /= E_Discriminant
510 then
511 Note_Uplevel_Ref
512 (E => Entity (N),
513 N => Empty,
514 Caller => Current_Subprogram,
515 Callee => Enclosing_Subprogram (Entity (N)));
516 end if;
518 -- Attribute or indexed component case
520 elsif Nkind_In (N, N_Attribute_Reference,
521 N_Indexed_Component)
522 then
523 Note_Uplevel_Bound (Prefix (N), Ref);
525 -- The indices of the indexed components, or the
526 -- associated expressions of an attribute reference,
527 -- may also involve uplevel references.
529 declare
530 Expr : Node_Id;
532 begin
533 Expr := First (Expressions (N));
534 while Present (Expr) loop
535 Note_Uplevel_Bound (Expr, Ref);
536 Next (Expr);
537 end loop;
538 end;
540 -- Binary operator cases. These can apply to arrays for
541 -- which we may need bounds.
543 elsif Nkind (N) in N_Binary_Op then
544 Note_Uplevel_Bound (Left_Opnd (N), Ref);
545 Note_Uplevel_Bound (Right_Opnd (N), Ref);
547 -- Unary operator case
549 elsif Nkind (N) in N_Unary_Op then
550 Note_Uplevel_Bound (Right_Opnd (N), Ref);
552 -- Explicit dereference and selected component case
554 elsif Nkind_In (N,
555 N_Explicit_Dereference,
556 N_Selected_Component)
557 then
558 Note_Uplevel_Bound (Prefix (N), Ref);
560 -- Conversion case
562 elsif Nkind (N) = N_Type_Conversion then
563 Note_Uplevel_Bound (Expression (N), Ref);
564 end if;
565 end Note_Uplevel_Bound;
567 -- Start of processing for Check_Static_Type
569 begin
570 -- If already marked static, immediate return
572 if Is_Static_Type (T) then
573 return;
574 end if;
576 -- If the type is at library level, always consider it static,
577 -- since such uplevel references are irrelevant.
579 if Is_Library_Level_Entity (T) then
580 Set_Is_Static_Type (T);
581 return;
582 end if;
584 -- Otherwise figure out what the story is with this type
586 -- For a scalar type, check bounds
588 if Is_Scalar_Type (T) then
590 -- If both bounds static, then this is a static type
592 declare
593 LB : constant Node_Id := Type_Low_Bound (T);
594 UB : constant Node_Id := Type_High_Bound (T);
596 begin
597 if not Is_Static_Expression (LB) then
598 Note_Uplevel_Bound (LB, N);
599 DT := True;
600 end if;
602 if not Is_Static_Expression (UB) then
603 Note_Uplevel_Bound (UB, N);
604 DT := True;
605 end if;
606 end;
608 -- For record type, check all components and discriminant
609 -- constraints if present.
611 elsif Is_Record_Type (T) then
612 declare
613 C : Entity_Id;
614 D : Elmt_Id;
616 begin
617 C := First_Component_Or_Discriminant (T);
618 while Present (C) loop
619 Check_Static_Type (Etype (C), N, DT);
620 Next_Component_Or_Discriminant (C);
621 end loop;
623 if Has_Discriminants (T)
624 and then Present (Discriminant_Constraint (T))
625 then
626 D := First_Elmt (Discriminant_Constraint (T));
627 while Present (D) loop
628 if not Is_Static_Expression (Node (D)) then
629 Note_Uplevel_Bound (Node (D), N);
630 DT := True;
631 end if;
633 Next_Elmt (D);
634 end loop;
635 end if;
636 end;
638 -- For array type, check index types and component type
640 elsif Is_Array_Type (T) then
641 declare
642 IX : Node_Id;
643 begin
644 Check_Static_Type (Component_Type (T), N, DT);
646 IX := First_Index (T);
647 while Present (IX) loop
648 Check_Static_Type (Etype (IX), N, DT);
649 Next_Index (IX);
650 end loop;
651 end;
653 -- For private type, examine whether full view is static
655 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
656 Check_Static_Type (Full_View (T), N, DT);
658 if Is_Static_Type (Full_View (T)) then
659 Set_Is_Static_Type (T);
660 end if;
662 -- For now, ignore other types
664 else
665 return;
666 end if;
668 if not DT then
669 Set_Is_Static_Type (T);
670 end if;
671 end Check_Static_Type;
673 ----------------------
674 -- Note_Uplevel_Ref --
675 ----------------------
677 procedure Note_Uplevel_Ref
678 (E : Entity_Id;
679 N : Node_Id;
680 Caller : Entity_Id;
681 Callee : Entity_Id)
683 Full_E : Entity_Id := E;
684 begin
685 -- Nothing to do for static type
687 if Is_Static_Type (E) then
688 return;
689 end if;
691 -- Nothing to do if Caller and Callee are the same
693 if Caller = Callee then
694 return;
696 -- Callee may be a function that returns an array, and that has
697 -- been rewritten as a procedure. If caller is that procedure,
698 -- nothing to do either.
700 elsif Ekind (Callee) = E_Function
701 and then Rewritten_For_C (Callee)
702 and then Corresponding_Procedure (Callee) = Caller
703 then
704 return;
705 end if;
707 -- We have a new uplevel referenced entity
709 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
710 Full_E := Full_View (E);
711 end if;
713 -- All we do at this stage is to add the uplevel reference to
714 -- the table. It's too early to do anything else, since this
715 -- uplevel reference may come from an unreachable subprogram
716 -- in which case the entry will be deleted.
718 Urefs.Append ((N, Full_E, Caller, Callee));
719 end Note_Uplevel_Ref;
721 -------------------------
722 -- Register_Subprogram --
723 -------------------------
725 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id) is
726 L : constant Nat := Get_Level (Subp, E);
728 begin
729 -- Subprograms declared in tasks and protected types cannot
730 -- be eliminated because calls to them may be in other units,
731 -- so they must be treated as reachable.
733 Subps.Append
734 ((Ent => E,
735 Bod => Bod,
736 Lev => L,
737 Reachable => In_Synchronized_Unit (E),
739 -- Subprograms declared in tasks and protected types are
740 -- reachable and cannot be eliminated.
742 Uplevel_Ref => L,
743 Declares_AREC => False,
744 Uents => No_Elist,
745 Last => 0,
746 ARECnF => Empty,
747 ARECn => Empty,
748 ARECnT => Empty,
749 ARECnPT => Empty,
750 ARECnP => Empty,
751 ARECnU => Empty));
753 Set_Subps_Index (E, UI_From_Int (Subps.Last));
754 end Register_Subprogram;
756 -- Start of processing for Visit_Node
758 begin
759 case Nkind (N) is
761 -- Record a subprogram call
763 when N_Function_Call
764 | N_Procedure_Call_Statement
766 -- We are only interested in direct calls, not indirect
767 -- calls (where Name (N) is an explicit dereference) at
768 -- least for now!
770 if Nkind (Name (N)) in N_Has_Entity then
771 Ent := Entity (Name (N));
773 -- We are only interested in calls to subprograms nested
774 -- within Subp. Calls to Subp itself or to subprograms
775 -- outside the nested structure do not affect us.
777 if Scope_Within (Ent, Subp)
778 and then Is_Subprogram (Ent)
779 and then not Is_Imported (Ent)
780 then
781 Append_Unique_Call ((N, Current_Subprogram, Ent));
782 end if;
783 end if;
785 -- For all calls where the formal is an unconstrained array
786 -- and the actual is constrained we need to check the bounds
787 -- for uplevel references.
789 declare
790 Actual : Entity_Id;
791 DT : Boolean := False;
792 Formal : Node_Id;
793 Subp : Entity_Id;
795 begin
796 if Nkind (Name (N)) = N_Explicit_Dereference then
797 Subp := Etype (Name (N));
798 else
799 Subp := Entity (Name (N));
800 end if;
802 Actual := First_Actual (N);
803 Formal := First_Formal_With_Extras (Subp);
804 while Present (Actual) loop
805 if Is_Array_Type (Etype (Formal))
806 and then not Is_Constrained (Etype (Formal))
807 and then Is_Constrained (Etype (Actual))
808 then
809 Check_Static_Type (Etype (Actual), Empty, DT);
810 end if;
812 Next_Actual (Actual);
813 Next_Formal_With_Extras (Formal);
814 end loop;
815 end;
817 -- An At_End_Proc in a statement sequence indicates that there
818 -- is a call from the enclosing construct or block to that
819 -- subprogram. As above, the called entity must be local and
820 -- not imported.
822 when N_Handled_Sequence_Of_Statements =>
823 if Present (At_End_Proc (N))
824 and then Scope_Within (Entity (At_End_Proc (N)), Subp)
825 and then not Is_Imported (Entity (At_End_Proc (N)))
826 then
827 Append_Unique_Call
828 ((N, Current_Subprogram, Entity (At_End_Proc (N))));
829 end if;
831 -- Similarly, the following constructs include a semantic
832 -- attribute Procedure_To_Call that must be handled like
833 -- other calls. Likewise for attribute Storage_Pool.
835 when N_Allocator
836 | N_Extended_Return_Statement
837 | N_Free_Statement
838 | N_Simple_Return_Statement
840 declare
841 Pool : constant Entity_Id := Storage_Pool (N);
842 Proc : constant Entity_Id := Procedure_To_Call (N);
844 begin
845 if Present (Proc)
846 and then Scope_Within (Proc, Subp)
847 and then not Is_Imported (Proc)
848 then
849 Append_Unique_Call ((N, Current_Subprogram, Proc));
850 end if;
852 if Present (Pool)
853 and then not Is_Library_Level_Entity (Pool)
854 and then Scope_Within_Or_Same (Scope (Pool), Subp)
855 then
856 Caller := Current_Subprogram;
857 Callee := Enclosing_Subprogram (Pool);
859 if Callee /= Caller then
860 Note_Uplevel_Ref (Pool, Empty, Caller, Callee);
861 end if;
862 end if;
863 end;
865 -- For an allocator with a qualified expression, check type
866 -- of expression being qualified. The explicit type name is
867 -- handled as an entity reference.
869 if Nkind (N) = N_Allocator
870 and then Nkind (Expression (N)) = N_Qualified_Expression
871 then
872 declare
873 DT : Boolean := False;
874 begin
875 Check_Static_Type
876 (Etype (Expression (Expression (N))), Empty, DT);
877 end;
879 -- For a Return or Free (all other nodes we handle here),
880 -- we usually need the size of the object, so we need to be
881 -- sure that any nonstatic bounds of the expression's type
882 -- that are uplevel are handled.
884 elsif Nkind (N) /= N_Allocator
885 and then Present (Expression (N))
886 then
887 declare
888 DT : Boolean := False;
889 begin
890 Check_Static_Type (Etype (Expression (N)), Empty, DT);
891 end;
892 end if;
894 -- A 'Access reference is a (potential) call. So is 'Address,
895 -- in particular on imported subprograms. Other attributes
896 -- require special handling.
898 when N_Attribute_Reference =>
899 declare
900 Attr : constant Attribute_Id :=
901 Get_Attribute_Id (Attribute_Name (N));
902 begin
903 case Attr is
904 when Attribute_Access
905 | Attribute_Unchecked_Access
906 | Attribute_Unrestricted_Access
907 | Attribute_Address
909 if Nkind (Prefix (N)) in N_Has_Entity then
910 Ent := Entity (Prefix (N));
912 -- We only need to examine calls to subprograms
913 -- nested within current Subp.
915 if Scope_Within (Ent, Subp) then
916 if Is_Imported (Ent) then
917 null;
919 elsif Is_Subprogram (Ent) then
920 Append_Unique_Call
921 ((N, Current_Subprogram, Ent));
922 end if;
923 end if;
924 end if;
926 -- References to bounds can be uplevel references if
927 -- the type isn't static.
929 when Attribute_First
930 | Attribute_Last
931 | Attribute_Length
933 -- Special-case attributes of objects whose bounds
934 -- may be uplevel references. More complex prefixes
935 -- handled during full traversal. Note that if the
936 -- nominal subtype of the prefix is unconstrained,
937 -- the bound must be obtained from the object, not
938 -- from the (possibly) uplevel reference.
940 if Is_Constrained (Etype (Prefix (N))) then
941 declare
942 DT : Boolean := False;
943 begin
944 Check_Static_Type
945 (Etype (Prefix (N)), Empty, DT);
946 end;
948 return OK;
949 end if;
951 when others =>
952 null;
953 end case;
954 end;
956 -- Component associations in aggregates are either static or
957 -- else the aggregate will be expanded into assignments, in
958 -- which case the expression is analyzed later and provides
959 -- no relevant code generation.
961 when N_Component_Association =>
962 if No (Expression (N))
963 or else No (Etype (Expression (N)))
964 then
965 return Skip;
966 end if;
968 -- Generic associations are not analyzed: the actuals are
969 -- transferred to renaming and subtype declarations that
970 -- are the ones that must be examined.
972 when N_Generic_Association =>
973 return Skip;
975 -- Indexed references can be uplevel if the type isn't static
976 -- and if the lower bound (or an inner bound for a multi-
977 -- dimensional array) is uplevel.
979 when N_Indexed_Component
980 | N_Slice
982 if Is_Constrained (Etype (Prefix (N))) then
983 declare
984 DT : Boolean := False;
985 begin
986 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
987 end;
988 end if;
990 -- A selected component can have an implicit up-level
991 -- reference due to the bounds of previous fields in the
992 -- record. We simplify the processing here by examining
993 -- all components of the record.
995 -- Selected components appear as unit names and end labels
996 -- for child units. Prefixes of these nodes denote parent
997 -- units and carry no type information so they are skipped.
999 when N_Selected_Component =>
1000 if Present (Etype (Prefix (N))) then
1001 declare
1002 DT : Boolean := False;
1003 begin
1004 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
1005 end;
1006 end if;
1008 -- For EQ/NE comparisons, we need the type of the operands
1009 -- in order to do the comparison, which means we need the
1010 -- bounds.
1012 when N_Op_Eq
1013 | N_Op_Ne
1015 declare
1016 DT : Boolean := False;
1017 begin
1018 Check_Static_Type (Etype (Left_Opnd (N)), Empty, DT);
1019 Check_Static_Type (Etype (Right_Opnd (N)), Empty, DT);
1020 end;
1022 -- Likewise we need the sizes to compute how much to move in
1023 -- an assignment.
1025 when N_Assignment_Statement =>
1026 declare
1027 DT : Boolean := False;
1028 begin
1029 Check_Static_Type (Etype (Name (N)), Empty, DT);
1030 Check_Static_Type (Etype (Expression (N)), Empty, DT);
1031 end;
1033 -- Record a subprogram. We record a subprogram body that acts
1034 -- as a spec. Otherwise we record a subprogram declaration,
1035 -- providing that it has a corresponding body we can get hold
1036 -- of. The case of no corresponding body being available is
1037 -- ignored for now.
1039 when N_Subprogram_Body =>
1040 Ent := Unique_Defining_Entity (N);
1042 -- Ignore generic subprogram
1044 if Is_Generic_Subprogram (Ent) then
1045 return Skip;
1046 end if;
1048 -- Make new entry in subprogram table if not already made
1050 Register_Subprogram (Ent, N);
1052 -- We make a recursive call to scan the subprogram body, so
1053 -- that we can save and restore Current_Subprogram.
1055 declare
1056 Save_CS : constant Entity_Id := Current_Subprogram;
1057 Decl : Node_Id;
1059 begin
1060 Current_Subprogram := Ent;
1062 -- Scan declarations
1064 Decl := First (Declarations (N));
1065 while Present (Decl) loop
1066 Visit (Decl);
1067 Next (Decl);
1068 end loop;
1070 -- Scan statements
1072 Visit (Handled_Statement_Sequence (N));
1074 -- Restore current subprogram setting
1076 Current_Subprogram := Save_CS;
1077 end;
1079 -- Now at this level, return skipping the subprogram body
1080 -- descendants, since we already took care of them!
1082 return Skip;
1084 -- If we have a body stub, visit the associated subunit, which
1085 -- is a semantic descendant of the stub.
1087 when N_Body_Stub =>
1088 Visit (Library_Unit (N));
1090 -- A declaration of a wrapper package indicates a subprogram
1091 -- instance for which there is no explicit body. Enter the
1092 -- subprogram instance in the table.
1094 when N_Package_Declaration =>
1095 if Is_Wrapper_Package (Defining_Entity (N)) then
1096 Register_Subprogram
1097 (Related_Instance (Defining_Entity (N)), Empty);
1098 end if;
1100 -- Skip generic declarations
1102 when N_Generic_Declaration =>
1103 return Skip;
1105 -- Skip generic package body
1107 when N_Package_Body =>
1108 if Present (Corresponding_Spec (N))
1109 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
1110 then
1111 return Skip;
1112 end if;
1114 -- Pragmas and component declarations can be ignored
1116 when N_Component_Declaration
1117 | N_Pragma
1119 return Skip;
1121 -- Otherwise record an uplevel reference in a local identifier
1123 when others =>
1124 if Nkind (N) in N_Has_Entity
1125 and then Present (Entity (N))
1126 then
1127 Ent := Entity (N);
1129 -- Only interested in entities declared within our nest
1131 if not Is_Library_Level_Entity (Ent)
1132 and then Scope_Within_Or_Same (Scope (Ent), Subp)
1134 -- Skip entities defined in inlined subprograms
1136 and then
1137 Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
1139 -- Constants and variables are potentially uplevel
1140 -- references to global declarations.
1142 and then
1143 (Ekind_In (Ent, E_Constant,
1144 E_Loop_Parameter,
1145 E_Variable)
1147 -- Formals are interesting, but not if being used
1148 -- as mere names of parameters for name notation
1149 -- calls.
1151 or else
1152 (Is_Formal (Ent)
1153 and then not
1154 (Nkind (Parent (N)) = N_Parameter_Association
1155 and then Selector_Name (Parent (N)) = N))
1157 -- Types other than known Is_Static types are
1158 -- potentially interesting.
1160 or else
1161 (Is_Type (Ent) and then not Is_Static_Type (Ent)))
1162 then
1163 -- Here we have a potentially interesting uplevel
1164 -- reference to examine.
1166 if Is_Type (Ent) then
1167 declare
1168 DT : Boolean := False;
1170 begin
1171 Check_Static_Type (Ent, N, DT);
1172 return OK;
1173 end;
1174 end if;
1176 Caller := Current_Subprogram;
1177 Callee := Enclosing_Subprogram (Ent);
1179 if Callee /= Caller
1180 and then (not Is_Static_Type (Ent)
1181 or else Needs_Fat_Pointer (Ent))
1182 then
1183 Note_Uplevel_Ref (Ent, N, Caller, Callee);
1185 -- Check the type of a formal parameter of the current
1186 -- subprogram, whose formal type may be an uplevel
1187 -- reference.
1189 elsif Is_Formal (Ent)
1190 and then Scope (Ent) = Current_Subprogram
1191 then
1192 declare
1193 DT : Boolean := False;
1195 begin
1196 Check_Static_Type (Etype (Ent), Empty, DT);
1197 end;
1198 end if;
1199 end if;
1200 end if;
1201 end case;
1203 -- Fall through to continue scanning children of this node
1205 return OK;
1206 end Visit_Node;
1208 -- Start of processing for Build_Tables
1210 begin
1211 -- Traverse the body to get subprograms, calls and uplevel references
1213 Visit (Subp_Body);
1214 end Build_Tables;
1216 -- Now do the first transitive closure which determines which
1217 -- subprograms in the nest are actually reachable.
1219 Reachable_Closure : declare
1220 Modified : Boolean;
1222 begin
1223 Subps.Table (Subps_First).Reachable := True;
1225 -- We use a simple minded algorithm as follows (obviously this can
1226 -- be done more efficiently, using one of the standard algorithms
1227 -- for efficient transitive closure computation, but this is simple
1228 -- and most likely fast enough that its speed does not matter).
1230 -- Repeatedly scan the list of calls. Any time we find a call from
1231 -- A to B, where A is reachable, but B is not, then B is reachable,
1232 -- and note that we have made a change by setting Modified True. We
1233 -- repeat this until we make a pass with no modifications.
1235 Outer : loop
1236 Modified := False;
1237 Inner : for J in Calls.First .. Calls.Last loop
1238 declare
1239 CTJ : Call_Entry renames Calls.Table (J);
1241 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1242 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1244 SUBF : Subp_Entry renames Subps.Table (SINF);
1245 SUBT : Subp_Entry renames Subps.Table (SINT);
1247 begin
1248 if SUBF.Reachable and then not SUBT.Reachable then
1249 SUBT.Reachable := True;
1250 Modified := True;
1251 end if;
1252 end;
1253 end loop Inner;
1255 exit Outer when not Modified;
1256 end loop Outer;
1257 end Reachable_Closure;
1259 -- Remove calls from unreachable subprograms
1261 declare
1262 New_Index : Nat;
1264 begin
1265 New_Index := 0;
1266 for J in Calls.First .. Calls.Last loop
1267 declare
1268 CTJ : Call_Entry renames Calls.Table (J);
1270 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1271 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1273 SUBF : Subp_Entry renames Subps.Table (SINF);
1274 SUBT : Subp_Entry renames Subps.Table (SINT);
1276 begin
1277 if SUBF.Reachable then
1278 pragma Assert (SUBT.Reachable);
1279 New_Index := New_Index + 1;
1280 Calls.Table (New_Index) := Calls.Table (J);
1281 end if;
1282 end;
1283 end loop;
1285 Calls.Set_Last (New_Index);
1286 end;
1288 -- Remove uplevel references from unreachable subprograms
1290 declare
1291 New_Index : Nat;
1293 begin
1294 New_Index := 0;
1295 for J in Urefs.First .. Urefs.Last loop
1296 declare
1297 URJ : Uref_Entry renames Urefs.Table (J);
1299 SINF : constant SI_Type := Subp_Index (URJ.Caller);
1300 SINT : constant SI_Type := Subp_Index (URJ.Callee);
1302 SUBF : Subp_Entry renames Subps.Table (SINF);
1303 SUBT : Subp_Entry renames Subps.Table (SINT);
1305 S : Entity_Id;
1307 begin
1308 -- Keep reachable reference
1310 if SUBF.Reachable then
1311 New_Index := New_Index + 1;
1312 Urefs.Table (New_Index) := Urefs.Table (J);
1314 -- And since we know we are keeping this one, this is a good
1315 -- place to fill in information for a good reference.
1317 -- Mark all enclosing subprograms need to declare AREC
1319 S := URJ.Caller;
1320 loop
1321 S := Enclosing_Subprogram (S);
1323 -- If we are at the top level, as can happen with
1324 -- references to formals in aspects of nested subprogram
1325 -- declarations, there are no further subprograms to mark
1326 -- as requiring activation records.
1328 exit when No (S);
1330 declare
1331 SUBI : Subp_Entry renames Subps.Table (Subp_Index (S));
1332 begin
1333 SUBI.Declares_AREC := True;
1335 -- If this entity was marked reachable because it is
1336 -- in a task or protected type, there may not appear
1337 -- to be any calls to it, which would normally adjust
1338 -- the levels of the parent subprograms. So we need to
1339 -- be sure that the uplevel reference of that entity
1340 -- takes into account possible calls.
1342 if In_Synchronized_Unit (SUBF.Ent)
1343 and then SUBT.Lev < SUBI.Uplevel_Ref
1344 then
1345 SUBI.Uplevel_Ref := SUBT.Lev;
1346 end if;
1347 end;
1349 exit when S = URJ.Callee;
1350 end loop;
1352 -- Add to list of uplevel referenced entities for Callee.
1353 -- We do not add types to this list, only actual references
1354 -- to objects that will be referenced uplevel, and we use
1355 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1356 -- duplicate entries in the list.
1357 -- Discriminants are also excluded, only the enclosing
1358 -- object can appear in the list.
1360 if not Is_Uplevel_Referenced_Entity (URJ.Ent)
1361 and then Ekind (URJ.Ent) /= E_Discriminant
1362 then
1363 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
1364 Append_New_Elmt (URJ.Ent, SUBT.Uents);
1365 end if;
1367 -- And set uplevel indication for caller
1369 if SUBT.Lev < SUBF.Uplevel_Ref then
1370 SUBF.Uplevel_Ref := SUBT.Lev;
1371 end if;
1372 end if;
1373 end;
1374 end loop;
1376 Urefs.Set_Last (New_Index);
1377 end;
1379 -- Remove unreachable subprograms from Subps table. Note that we do
1380 -- this after eliminating entries from the other two tables, since
1381 -- those elimination steps depend on referencing the Subps table.
1383 declare
1384 New_SI : SI_Type;
1386 begin
1387 New_SI := Subps_First - 1;
1388 for J in Subps_First .. Subps.Last loop
1389 declare
1390 STJ : Subp_Entry renames Subps.Table (J);
1391 Spec : Node_Id;
1392 Decl : Node_Id;
1394 begin
1395 -- Subprogram is reachable, copy and reset index
1397 if STJ.Reachable then
1398 New_SI := New_SI + 1;
1399 Subps.Table (New_SI) := STJ;
1400 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
1402 -- Subprogram is not reachable
1404 else
1405 -- Clear index, since no longer active
1407 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
1409 -- Output debug information if -gnatd.3 set
1411 if Debug_Flag_Dot_3 then
1412 Write_Str ("Eliminate ");
1413 Write_Name (Chars (Subps.Table (J).Ent));
1414 Write_Str (" at ");
1415 Write_Location (Sloc (Subps.Table (J).Ent));
1416 Write_Str (" (not referenced)");
1417 Write_Eol;
1418 end if;
1420 -- Rewrite declaration, body, and corresponding freeze node
1421 -- to null statements.
1423 -- A subprogram instantiation does not have an explicit
1424 -- body. If unused, we could remove the corresponding
1425 -- wrapper package and its body (TBD).
1427 if Present (STJ.Bod) then
1428 Spec := Corresponding_Spec (STJ.Bod);
1430 if Present (Spec) then
1431 Decl := Parent (Declaration_Node (Spec));
1432 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
1434 if Present (Freeze_Node (Spec)) then
1435 Rewrite (Freeze_Node (Spec),
1436 Make_Null_Statement (Sloc (Decl)));
1437 end if;
1438 end if;
1440 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
1441 end if;
1442 end if;
1443 end;
1444 end loop;
1446 Subps.Set_Last (New_SI);
1447 end;
1449 -- Now it is time for the second transitive closure, which follows calls
1450 -- and makes sure that A calls B, and B has uplevel references, then A
1451 -- is also marked as having uplevel references.
1453 Closure_Uplevel : declare
1454 Modified : Boolean;
1456 begin
1457 -- We use a simple minded algorithm as follows (obviously this can
1458 -- be done more efficiently, using one of the standard algorithms
1459 -- for efficient transitive closure computation, but this is simple
1460 -- and most likely fast enough that its speed does not matter).
1462 -- Repeatedly scan the list of calls. Any time we find a call from
1463 -- A to B, where B has uplevel references, make sure that A is marked
1464 -- as having at least the same level of uplevel referencing.
1466 Outer2 : loop
1467 Modified := False;
1468 Inner2 : for J in Calls.First .. Calls.Last loop
1469 declare
1470 CTJ : Call_Entry renames Calls.Table (J);
1471 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1472 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1473 SUBF : Subp_Entry renames Subps.Table (SINF);
1474 SUBT : Subp_Entry renames Subps.Table (SINT);
1475 begin
1476 if SUBT.Lev > SUBT.Uplevel_Ref
1477 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
1478 then
1479 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
1480 Modified := True;
1481 end if;
1482 end;
1483 end loop Inner2;
1485 exit Outer2 when not Modified;
1486 end loop Outer2;
1487 end Closure_Uplevel;
1489 -- We have one more step before the tables are complete. An uplevel
1490 -- call from subprogram A to subprogram B where subprogram B has uplevel
1491 -- references is in effect an uplevel reference, and must arrange for
1492 -- the proper activation link to be passed.
1494 for J in Calls.First .. Calls.Last loop
1495 declare
1496 CTJ : Call_Entry renames Calls.Table (J);
1498 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1499 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1501 SUBF : Subp_Entry renames Subps.Table (SINF);
1502 SUBT : Subp_Entry renames Subps.Table (SINT);
1504 A : Entity_Id;
1506 begin
1507 -- If callee has uplevel references
1509 if SUBT.Uplevel_Ref < SUBT.Lev
1511 -- And this is an uplevel call
1513 and then SUBT.Lev < SUBF.Lev
1514 then
1515 -- We need to arrange for finding the uplink
1517 A := CTJ.Caller;
1518 loop
1519 A := Enclosing_Subprogram (A);
1520 Subps.Table (Subp_Index (A)).Declares_AREC := True;
1521 exit when A = CTJ.Callee;
1523 -- In any case exit when we get to the outer level. This
1524 -- happens in some odd cases with generics (in particular
1525 -- sem_ch3.adb does not compile without this kludge ???).
1527 exit when A = Subp;
1528 end loop;
1529 end if;
1530 end;
1531 end loop;
1533 -- The tables are now complete, so we can record the last index in the
1534 -- Subps table for later reference in Cprint.
1536 Subps.Table (Subps_First).Last := Subps.Last;
1538 -- Next step, create the entities for code we will insert. We do this
1539 -- at the start so that all the entities are defined, regardless of the
1540 -- order in which we do the code insertions.
1542 Create_Entities : for J in Subps_First .. Subps.Last loop
1543 declare
1544 STJ : Subp_Entry renames Subps.Table (J);
1545 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1547 begin
1548 -- First we create the ARECnF entity for the additional formal for
1549 -- all subprograms which need an activation record passed.
1551 if STJ.Uplevel_Ref < STJ.Lev then
1552 STJ.ARECnF :=
1553 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
1554 end if;
1556 -- Define the AREC entities for the activation record if needed
1558 if STJ.Declares_AREC then
1559 STJ.ARECn :=
1560 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
1561 STJ.ARECnT :=
1562 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
1563 STJ.ARECnPT :=
1564 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
1565 STJ.ARECnP :=
1566 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
1568 -- Define uplink component entity if inner nesting case
1570 if Present (STJ.ARECnF) then
1571 STJ.ARECnU :=
1572 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
1573 end if;
1574 end if;
1575 end;
1576 end loop Create_Entities;
1578 -- Loop through subprograms
1580 Subp_Loop : declare
1581 Addr : constant Entity_Id := RTE (RE_Address);
1583 begin
1584 for J in Subps_First .. Subps.Last loop
1585 declare
1586 STJ : Subp_Entry renames Subps.Table (J);
1588 begin
1589 -- First add the extra formal if needed. This applies to all
1590 -- nested subprograms that require an activation record to be
1591 -- passed, as indicated by ARECnF being defined.
1593 if Present (STJ.ARECnF) then
1595 -- Here we need the extra formal. We do the expansion and
1596 -- analysis of this manually, since it is fairly simple,
1597 -- and it is not obvious how we can get what we want if we
1598 -- try to use the normal Analyze circuit.
1600 Add_Extra_Formal : declare
1601 Encl : constant SI_Type := Enclosing_Subp (J);
1602 STJE : Subp_Entry renames Subps.Table (Encl);
1603 -- Index and Subp_Entry for enclosing routine
1605 Form : constant Entity_Id := STJ.ARECnF;
1606 -- The formal to be added. Note that n here is one less
1607 -- than the level of the subprogram itself (STJ.Ent).
1609 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1610 -- S is an N_Function/Procedure_Specification node, and F
1611 -- is the new entity to add to this subprogramn spec as
1612 -- the last Extra_Formal.
1614 ----------------------
1615 -- Add_Form_To_Spec --
1616 ----------------------
1618 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1619 Sub : constant Entity_Id := Defining_Entity (S);
1620 Ent : Entity_Id;
1622 begin
1623 -- Case of at least one Extra_Formal is present, set
1624 -- ARECnF as the new last entry in the list.
1626 if Present (Extra_Formals (Sub)) then
1627 Ent := Extra_Formals (Sub);
1628 while Present (Extra_Formal (Ent)) loop
1629 Ent := Extra_Formal (Ent);
1630 end loop;
1632 Set_Extra_Formal (Ent, F);
1634 -- No Extra formals present
1636 else
1637 Set_Extra_Formals (Sub, F);
1638 Ent := Last_Formal (Sub);
1640 if Present (Ent) then
1641 Set_Extra_Formal (Ent, F);
1642 end if;
1643 end if;
1644 end Add_Form_To_Spec;
1646 -- Start of processing for Add_Extra_Formal
1648 begin
1649 -- Decorate the new formal entity
1651 Set_Scope (Form, STJ.Ent);
1652 Set_Ekind (Form, E_In_Parameter);
1653 Set_Etype (Form, STJE.ARECnPT);
1654 Set_Mechanism (Form, By_Copy);
1655 Set_Never_Set_In_Source (Form, True);
1656 Set_Analyzed (Form, True);
1657 Set_Comes_From_Source (Form, False);
1658 Set_Is_Activation_Record (Form, True);
1660 -- Case of only body present
1662 if Acts_As_Spec (STJ.Bod) then
1663 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1665 -- Case of separate spec
1667 else
1668 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1669 end if;
1670 end Add_Extra_Formal;
1671 end if;
1673 -- Processing for subprograms that declare an activation record
1675 if Present (STJ.ARECn) then
1677 -- Local declarations for one such subprogram
1679 declare
1680 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1682 Decls : constant List_Id := New_List;
1683 -- List of new declarations we create
1685 Clist : List_Id;
1686 Comp : Entity_Id;
1688 Decl_Assign : Node_Id;
1689 -- Assigment to set uplink, Empty if none
1691 Decl_ARECnT : Node_Id;
1692 Decl_ARECnPT : Node_Id;
1693 Decl_ARECn : Node_Id;
1694 Decl_ARECnP : Node_Id;
1695 -- Declaration nodes for the AREC entities we build
1697 begin
1698 -- Build list of component declarations for ARECnT
1700 Clist := Empty_List;
1702 -- If we are in a subprogram that has a static link that
1703 -- is passed in (as indicated by ARECnF being defined),
1704 -- then include ARECnU : ARECmPT where ARECmPT comes from
1705 -- the level one higher than the current level, and the
1706 -- entity ARECnPT comes from the enclosing subprogram.
1708 if Present (STJ.ARECnF) then
1709 declare
1710 STJE : Subp_Entry
1711 renames Subps.Table (Enclosing_Subp (J));
1712 begin
1713 Append_To (Clist,
1714 Make_Component_Declaration (Loc,
1715 Defining_Identifier => STJ.ARECnU,
1716 Component_Definition =>
1717 Make_Component_Definition (Loc,
1718 Subtype_Indication =>
1719 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1720 end;
1721 end if;
1723 -- Add components for uplevel referenced entities
1725 if Present (STJ.Uents) then
1726 declare
1727 Elmt : Elmt_Id;
1728 Ptr_Decl : Node_Id;
1729 Uent : Entity_Id;
1731 Indx : Nat;
1732 -- 1's origin of index in list of elements. This is
1733 -- used to uniquify names if needed in Upref_Name.
1735 begin
1736 Elmt := First_Elmt (STJ.Uents);
1737 Indx := 0;
1738 while Present (Elmt) loop
1739 Uent := Node (Elmt);
1740 Indx := Indx + 1;
1742 Comp :=
1743 Make_Defining_Identifier (Loc,
1744 Chars => Upref_Name (Uent, Indx, Clist));
1746 Set_Activation_Record_Component
1747 (Uent, Comp);
1749 if Needs_Fat_Pointer (Uent) then
1751 -- Build corresponding access type
1753 Ptr_Decl :=
1754 Build_Access_Type_Decl
1755 (Etype (Uent), STJ.Ent);
1756 Append_To (Decls, Ptr_Decl);
1758 -- And use its type in the corresponding
1759 -- component.
1761 Append_To (Clist,
1762 Make_Component_Declaration (Loc,
1763 Defining_Identifier => Comp,
1764 Component_Definition =>
1765 Make_Component_Definition (Loc,
1766 Subtype_Indication =>
1767 New_Occurrence_Of
1768 (Defining_Identifier (Ptr_Decl),
1769 Loc))));
1770 else
1771 Append_To (Clist,
1772 Make_Component_Declaration (Loc,
1773 Defining_Identifier => Comp,
1774 Component_Definition =>
1775 Make_Component_Definition (Loc,
1776 Subtype_Indication =>
1777 New_Occurrence_Of (Addr, Loc))));
1778 end if;
1779 Next_Elmt (Elmt);
1780 end loop;
1781 end;
1782 end if;
1784 -- Now we can insert the AREC declarations into the body
1785 -- type ARECnT is record .. end record;
1786 -- pragma Suppress_Initialization (ARECnT);
1788 -- Note that we need to set the Suppress_Initialization
1789 -- flag after Decl_ARECnT has been analyzed.
1791 Decl_ARECnT :=
1792 Make_Full_Type_Declaration (Loc,
1793 Defining_Identifier => STJ.ARECnT,
1794 Type_Definition =>
1795 Make_Record_Definition (Loc,
1796 Component_List =>
1797 Make_Component_List (Loc,
1798 Component_Items => Clist)));
1799 Append_To (Decls, Decl_ARECnT);
1801 -- type ARECnPT is access all ARECnT;
1803 Decl_ARECnPT :=
1804 Make_Full_Type_Declaration (Loc,
1805 Defining_Identifier => STJ.ARECnPT,
1806 Type_Definition =>
1807 Make_Access_To_Object_Definition (Loc,
1808 All_Present => True,
1809 Subtype_Indication =>
1810 New_Occurrence_Of (STJ.ARECnT, Loc)));
1811 Append_To (Decls, Decl_ARECnPT);
1813 -- ARECn : aliased ARECnT;
1815 Decl_ARECn :=
1816 Make_Object_Declaration (Loc,
1817 Defining_Identifier => STJ.ARECn,
1818 Aliased_Present => True,
1819 Object_Definition =>
1820 New_Occurrence_Of (STJ.ARECnT, Loc));
1821 Append_To (Decls, Decl_ARECn);
1823 -- ARECnP : constant ARECnPT := ARECn'Access;
1825 Decl_ARECnP :=
1826 Make_Object_Declaration (Loc,
1827 Defining_Identifier => STJ.ARECnP,
1828 Constant_Present => True,
1829 Object_Definition =>
1830 New_Occurrence_Of (STJ.ARECnPT, Loc),
1831 Expression =>
1832 Make_Attribute_Reference (Loc,
1833 Prefix =>
1834 New_Occurrence_Of (STJ.ARECn, Loc),
1835 Attribute_Name => Name_Access));
1836 Append_To (Decls, Decl_ARECnP);
1838 -- If we are in a subprogram that has a static link that
1839 -- is passed in (as indicated by ARECnF being defined),
1840 -- then generate ARECn.ARECmU := ARECmF where m is
1841 -- one less than the current level to set the uplink.
1843 if Present (STJ.ARECnF) then
1844 Decl_Assign :=
1845 Make_Assignment_Statement (Loc,
1846 Name =>
1847 Make_Selected_Component (Loc,
1848 Prefix =>
1849 New_Occurrence_Of (STJ.ARECn, Loc),
1850 Selector_Name =>
1851 New_Occurrence_Of (STJ.ARECnU, Loc)),
1852 Expression =>
1853 New_Occurrence_Of (STJ.ARECnF, Loc));
1854 Append_To (Decls, Decl_Assign);
1856 else
1857 Decl_Assign := Empty;
1858 end if;
1860 if No (Declarations (STJ.Bod)) then
1861 Set_Declarations (STJ.Bod, Decls);
1862 else
1863 Prepend_List_To (Declarations (STJ.Bod), Decls);
1864 end if;
1866 -- Analyze the newly inserted declarations. Note that we
1867 -- do not need to establish the whole scope stack, since
1868 -- we have already set all entity fields (so there will
1869 -- be no searching of upper scopes to resolve names). But
1870 -- we do set the scope of the current subprogram, so that
1871 -- newly created entities go in the right entity chain.
1873 -- We analyze with all checks suppressed (since we do
1874 -- not expect any exceptions).
1876 Push_Scope (STJ.Ent);
1877 Analyze (Decl_ARECnT, Suppress => All_Checks);
1879 -- Note that we need to call Set_Suppress_Initialization
1880 -- after Decl_ARECnT has been analyzed, but before
1881 -- analyzing Decl_ARECnP so that the flag is properly
1882 -- taking into account.
1884 Set_Suppress_Initialization (STJ.ARECnT);
1886 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1887 Analyze (Decl_ARECn, Suppress => All_Checks);
1888 Analyze (Decl_ARECnP, Suppress => All_Checks);
1890 if Present (Decl_Assign) then
1891 Analyze (Decl_Assign, Suppress => All_Checks);
1892 end if;
1894 Pop_Scope;
1896 -- Next step, for each uplevel referenced entity, add
1897 -- assignment operations to set the component in the
1898 -- activation record.
1900 if Present (STJ.Uents) then
1901 declare
1902 Elmt : Elmt_Id;
1904 begin
1905 Elmt := First_Elmt (STJ.Uents);
1906 while Present (Elmt) loop
1907 declare
1908 Ent : constant Entity_Id := Node (Elmt);
1909 Loc : constant Source_Ptr := Sloc (Ent);
1910 Dec : constant Node_Id :=
1911 Declaration_Node (Ent);
1913 Asn : Node_Id;
1914 Attr : Name_Id;
1915 Ins : Node_Id;
1917 begin
1918 -- For parameters, we insert the assignment
1919 -- right after the declaration of ARECnP.
1920 -- For all other entities, we insert the
1921 -- assignment immediately after the
1922 -- declaration of the entity or after the
1923 -- freeze node if present.
1925 -- Note: we don't need to mark the entity
1926 -- as being aliased, because the address
1927 -- attribute will mark it as Address_Taken,
1928 -- and that is good enough.
1930 if Is_Formal (Ent) then
1931 Ins := Decl_ARECnP;
1933 elsif Has_Delayed_Freeze (Ent) then
1934 Ins := Freeze_Node (Ent);
1936 else
1937 Ins := Dec;
1938 end if;
1940 -- Build and insert the assignment:
1941 -- ARECn.nam := nam'Address
1942 -- or else 'Access for unconstrained array
1944 if Needs_Fat_Pointer (Ent) then
1945 Attr := Name_Access;
1946 else
1947 Attr := Name_Address;
1948 end if;
1950 Asn :=
1951 Make_Assignment_Statement (Loc,
1952 Name =>
1953 Make_Selected_Component (Loc,
1954 Prefix =>
1955 New_Occurrence_Of (STJ.ARECn, Loc),
1956 Selector_Name =>
1957 New_Occurrence_Of
1958 (Activation_Record_Component
1959 (Ent),
1960 Loc)),
1962 Expression =>
1963 Make_Attribute_Reference (Loc,
1964 Prefix =>
1965 New_Occurrence_Of (Ent, Loc),
1966 Attribute_Name => Attr));
1968 -- If we have a loop parameter, we have
1969 -- to insert before the first statement
1970 -- of the loop. Ins points to the
1971 -- N_Loop_Parameter_Specification.
1973 if Ekind (Ent) = E_Loop_Parameter then
1974 Ins :=
1975 First
1976 (Statements (Parent (Parent (Ins))));
1977 Insert_Before (Ins, Asn);
1979 else
1980 Insert_After (Ins, Asn);
1981 end if;
1983 -- Analyze the assignment statement. We do
1984 -- not need to establish the relevant scope
1985 -- stack entries here, because we have
1986 -- already set the correct entity references,
1987 -- so no name resolution is required, and no
1988 -- new entities are created, so we don't even
1989 -- need to set the current scope.
1991 -- We analyze with all checks suppressed
1992 -- (since we do not expect any exceptions).
1994 Analyze (Asn, Suppress => All_Checks);
1995 end;
1997 Next_Elmt (Elmt);
1998 end loop;
1999 end;
2000 end if;
2001 end;
2002 end if;
2003 end;
2004 end loop;
2005 end Subp_Loop;
2007 -- Next step, process uplevel references. This has to be done in a
2008 -- separate pass, after completing the processing in Sub_Loop because we
2009 -- need all the AREC declarations generated, inserted, and analyzed so
2010 -- that the uplevel references can be successfully analyzed.
2012 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
2013 declare
2014 UPJ : Uref_Entry renames Urefs.Table (J);
2016 begin
2017 -- Ignore type references, these are implicit references that do
2018 -- not need rewriting (e.g. the appearence in a conversion).
2019 -- Also ignore if no reference was specified or if the rewriting
2020 -- has already been done (this can happen if the N_Identifier
2021 -- occurs more than one time in the tree).
2023 if No (UPJ.Ref)
2024 or else not Is_Entity_Name (UPJ.Ref)
2025 or else not Present (Entity (UPJ.Ref))
2026 then
2027 goto Continue;
2028 end if;
2030 -- Rewrite one reference
2032 Rewrite_One_Ref : declare
2033 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
2034 -- Source location for the reference
2036 Typ : constant Entity_Id := Etype (UPJ.Ent);
2037 -- The type of the referenced entity
2039 Atyp : Entity_Id;
2040 -- The actual subtype of the reference
2042 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
2043 -- Subp_Index for caller containing reference
2045 STJR : Subp_Entry renames Subps.Table (RS_Caller);
2046 -- Subp_Entry for subprogram containing reference
2048 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
2049 -- Subp_Index for subprogram containing referenced entity
2051 STJE : Subp_Entry renames Subps.Table (RS_Callee);
2052 -- Subp_Entry for subprogram containing referenced entity
2054 Pfx : Node_Id;
2055 Comp : Entity_Id;
2056 SI : SI_Type;
2058 begin
2059 Atyp := Etype (UPJ.Ref);
2061 if Ekind (Atyp) /= E_Record_Subtype then
2062 Atyp := Get_Actual_Subtype (UPJ.Ref);
2063 end if;
2065 -- Ignore if no ARECnF entity for enclosing subprogram which
2066 -- probably happens as a result of not properly treating
2067 -- instance bodies. To be examined ???
2069 -- If this test is omitted, then the compilation of freeze.adb
2070 -- and inline.adb fail in unnesting mode.
2072 if No (STJR.ARECnF) then
2073 goto Continue;
2074 end if;
2076 -- Push the current scope, so that the pointer type Tnn, and
2077 -- any subsidiary entities resulting from the analysis of the
2078 -- rewritten reference, go in the right entity chain.
2080 Push_Scope (STJR.Ent);
2082 -- Now we need to rewrite the reference. We have a reference
2083 -- from level STJR.Lev to level STJE.Lev. The general form of
2084 -- the rewritten reference for entity X is:
2086 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
2088 -- where a,b,c,d .. m =
2089 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
2091 pragma Assert (STJR.Lev > STJE.Lev);
2093 -- Compute the prefix of X. Here are examples to make things
2094 -- clear (with parens to show groupings, the prefix is
2095 -- everything except the .X at the end).
2097 -- level 2 to level 1
2099 -- AREC1F.X
2101 -- level 3 to level 1
2103 -- (AREC2F.AREC1U).X
2105 -- level 4 to level 1
2107 -- ((AREC3F.AREC2U).AREC1U).X
2109 -- level 6 to level 2
2111 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
2113 -- In the above, ARECnF and ARECnU are pointers, so there are
2114 -- explicit dereferences required for these occurrences.
2116 Pfx :=
2117 Make_Explicit_Dereference (Loc,
2118 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
2119 SI := RS_Caller;
2120 for L in STJE.Lev .. STJR.Lev - 2 loop
2121 SI := Enclosing_Subp (SI);
2122 Pfx :=
2123 Make_Explicit_Dereference (Loc,
2124 Prefix =>
2125 Make_Selected_Component (Loc,
2126 Prefix => Pfx,
2127 Selector_Name =>
2128 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
2129 end loop;
2131 -- Get activation record component (must exist)
2133 Comp := Activation_Record_Component (UPJ.Ent);
2134 pragma Assert (Present (Comp));
2136 -- Do the replacement. If the component type is an access type,
2137 -- this is an uplevel reference for an entity that requires a
2138 -- fat pointer, so dereference the component.
2140 if Is_Access_Type (Etype (Comp)) then
2141 Rewrite (UPJ.Ref,
2142 Make_Explicit_Dereference (Loc,
2143 Prefix =>
2144 Make_Selected_Component (Loc,
2145 Prefix => Pfx,
2146 Selector_Name =>
2147 New_Occurrence_Of (Comp, Loc))));
2149 else
2150 Rewrite (UPJ.Ref,
2151 Make_Attribute_Reference (Loc,
2152 Prefix => New_Occurrence_Of (Atyp, Loc),
2153 Attribute_Name => Name_Deref,
2154 Expressions => New_List (
2155 Make_Selected_Component (Loc,
2156 Prefix => Pfx,
2157 Selector_Name =>
2158 New_Occurrence_Of (Comp, Loc)))));
2159 end if;
2161 -- Analyze and resolve the new expression. We do not need to
2162 -- establish the relevant scope stack entries here, because we
2163 -- have already set all the correct entity references, so no
2164 -- name resolution is needed. We have already set the current
2165 -- scope, so that any new entities created will be in the right
2166 -- scope.
2168 -- We analyze with all checks suppressed (since we do not
2169 -- expect any exceptions)
2171 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
2172 Pop_Scope;
2173 end Rewrite_One_Ref;
2174 end;
2176 <<Continue>>
2177 null;
2178 end loop Uplev_Refs;
2180 -- Finally, loop through all calls adding extra actual for the
2181 -- activation record where it is required.
2183 Adjust_Calls : for J in Calls.First .. Calls.Last loop
2185 -- Process a single call, we are only interested in a call to a
2186 -- subprogram that actually needs a pointer to an activation record,
2187 -- as indicated by the ARECnF entity being set. This excludes the
2188 -- top level subprogram, and any subprogram not having uplevel refs.
2190 Adjust_One_Call : declare
2191 CTJ : Call_Entry renames Calls.Table (J);
2192 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
2193 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
2195 Loc : constant Source_Ptr := Sloc (CTJ.N);
2197 Extra : Node_Id;
2198 ExtraP : Node_Id;
2199 SubX : SI_Type;
2200 Act : Node_Id;
2202 begin
2203 if Present (STT.ARECnF)
2204 and then Nkind (CTJ.N) in N_Subprogram_Call
2205 then
2206 -- CTJ.N is a call to a subprogram which may require a pointer
2207 -- to an activation record. The subprogram containing the call
2208 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2209 -- have a call from level STF.Lev to level STT.Lev.
2211 -- There are three possibilities:
2213 -- For a call to the same level, we just pass the activation
2214 -- record passed to the calling subprogram.
2216 if STF.Lev = STT.Lev then
2217 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2219 -- For a call that goes down a level, we pass a pointer to the
2220 -- activation record constructed within the caller (which may
2221 -- be the outer-level subprogram, but also may be a more deeply
2222 -- nested caller).
2224 elsif STT.Lev = STF.Lev + 1 then
2225 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
2227 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2228 -- since it is not possible to do a downcall of more than
2229 -- one level.
2231 -- For a call from level STF.Lev to level STT.Lev, we
2232 -- have to find the activation record needed by the
2233 -- callee. This is as follows:
2235 -- ARECaF.ARECbU.ARECcU....ARECmU
2237 -- where a,b,c .. m =
2238 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2240 else
2241 pragma Assert (STT.Lev < STF.Lev);
2243 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2244 SubX := Subp_Index (CTJ.Caller);
2245 for K in reverse STT.Lev .. STF.Lev - 1 loop
2246 SubX := Enclosing_Subp (SubX);
2247 Extra :=
2248 Make_Selected_Component (Loc,
2249 Prefix => Extra,
2250 Selector_Name =>
2251 New_Occurrence_Of
2252 (Subps.Table (SubX).ARECnU, Loc));
2253 end loop;
2254 end if;
2256 -- Extra is the additional parameter to be added. Build a
2257 -- parameter association that we can append to the actuals.
2259 ExtraP :=
2260 Make_Parameter_Association (Loc,
2261 Selector_Name =>
2262 New_Occurrence_Of (STT.ARECnF, Loc),
2263 Explicit_Actual_Parameter => Extra);
2265 if No (Parameter_Associations (CTJ.N)) then
2266 Set_Parameter_Associations (CTJ.N, Empty_List);
2267 end if;
2269 Append (ExtraP, Parameter_Associations (CTJ.N));
2271 -- We need to deal with the actual parameter chain as well. The
2272 -- newly added parameter is always the last actual.
2274 Act := First_Named_Actual (CTJ.N);
2276 if No (Act) then
2277 Set_First_Named_Actual (CTJ.N, Extra);
2279 -- If call has been relocated (as with an expression in
2280 -- an aggregate), set First_Named pointer in original node
2281 -- as well, because that's the parent of the parameter list.
2283 Set_First_Named_Actual
2284 (Parent (List_Containing (ExtraP)), Extra);
2286 -- Here we must follow the chain and append the new entry
2288 else
2289 loop
2290 declare
2291 PAN : Node_Id;
2292 NNA : Node_Id;
2294 begin
2295 PAN := Parent (Act);
2296 pragma Assert (Nkind (PAN) = N_Parameter_Association);
2297 NNA := Next_Named_Actual (PAN);
2299 if No (NNA) then
2300 Set_Next_Named_Actual (PAN, Extra);
2301 exit;
2302 end if;
2304 Act := NNA;
2305 end;
2306 end loop;
2307 end if;
2309 -- Analyze and resolve the new actual. We do not need to
2310 -- establish the relevant scope stack entries here, because
2311 -- we have already set all the correct entity references, so
2312 -- no name resolution is needed.
2314 -- We analyze with all checks suppressed (since we do not
2315 -- expect any exceptions, and also we temporarily turn off
2316 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2317 -- references (not needed at this stage, and in fact causes
2318 -- a bit of recursive chaos).
2320 Opt.Unnest_Subprogram_Mode := False;
2321 Analyze_And_Resolve
2322 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
2323 Opt.Unnest_Subprogram_Mode := True;
2324 end if;
2325 end Adjust_One_Call;
2326 end loop Adjust_Calls;
2328 return;
2329 end Unnest_Subprogram;
2331 ------------------------
2332 -- Unnest_Subprograms --
2333 ------------------------
2335 procedure Unnest_Subprograms (N : Node_Id) is
2336 function Search_Subprograms (N : Node_Id) return Traverse_Result;
2337 -- Tree visitor that search for outer level procedures with nested
2338 -- subprograms and invokes Unnest_Subprogram()
2340 ---------------
2341 -- Do_Search --
2342 ---------------
2344 procedure Do_Search is new Traverse_Proc (Search_Subprograms);
2345 -- Subtree visitor instantiation
2347 ------------------------
2348 -- Search_Subprograms --
2349 ------------------------
2351 function Search_Subprograms (N : Node_Id) return Traverse_Result is
2352 begin
2353 if Nkind_In (N, N_Subprogram_Body, N_Subprogram_Body_Stub) then
2354 declare
2355 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
2357 begin
2358 -- We are only interested in subprograms (not generic
2359 -- subprograms), that have nested subprograms.
2361 if Is_Subprogram (Spec_Id)
2362 and then Has_Nested_Subprogram (Spec_Id)
2363 and then Is_Library_Level_Entity (Spec_Id)
2364 then
2365 Unnest_Subprogram (Spec_Id, N);
2366 end if;
2367 end;
2369 -- The proper body of a stub may contain nested subprograms, and
2370 -- therefore must be visited explicitly. Nested stubs are examined
2371 -- recursively in Visit_Node.
2373 elsif Nkind (N) in N_Body_Stub then
2374 Do_Search (Library_Unit (N));
2375 end if;
2377 return OK;
2378 end Search_Subprograms;
2380 -- Start of processing for Unnest_Subprograms
2382 begin
2383 if not Opt.Unnest_Subprogram_Mode or not Opt.Expander_Active then
2384 return;
2385 end if;
2387 -- A specification will contain bodies if it contains instantiations so
2388 -- examine package or subprogram declaration of the main unit, when it
2389 -- is present.
2391 if Nkind (Unit (N)) = N_Package_Body
2392 or else (Nkind (Unit (N)) = N_Subprogram_Body
2393 and then not Acts_As_Spec (N))
2394 then
2395 Do_Search (Library_Unit (N));
2396 end if;
2398 Do_Search (N);
2399 end Unnest_Subprograms;
2401 end Exp_Unst;