LWG 3035. std::allocator's constructors should be constexpr
[official-gcc.git] / gcc / ada / exp_unst.adb
blob1ac96364345ca8f465fdad6ef11aa12d63761be8
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 -- Conversion case
531 elsif Nkind (N) = N_Type_Conversion then
532 Note_Uplevel_Bound (Expression (N), Ref);
533 end if;
534 end Note_Uplevel_Bound;
536 -- Start of processing for Check_Static_Type
538 begin
539 -- If already marked static, immediate return
541 if Is_Static_Type (T) then
542 return;
543 end if;
545 -- If the type is at library level, always consider it static,
546 -- since such uplevel references are irrelevant.
548 if Is_Library_Level_Entity (T) then
549 Set_Is_Static_Type (T);
550 return;
551 end if;
553 -- Otherwise figure out what the story is with this type
555 -- For a scalar type, check bounds
557 if Is_Scalar_Type (T) then
559 -- If both bounds static, then this is a static type
561 declare
562 LB : constant Node_Id := Type_Low_Bound (T);
563 UB : constant Node_Id := Type_High_Bound (T);
565 begin
566 if not Is_Static_Expression (LB) then
567 Note_Uplevel_Bound (LB, N);
568 DT := True;
569 end if;
571 if not Is_Static_Expression (UB) then
572 Note_Uplevel_Bound (UB, N);
573 DT := True;
574 end if;
575 end;
577 -- For record type, check all components and discriminant
578 -- constraints if present.
580 elsif Is_Record_Type (T) then
581 declare
582 C : Entity_Id;
583 D : Elmt_Id;
585 begin
586 C := First_Component_Or_Discriminant (T);
587 while Present (C) loop
588 Check_Static_Type (Etype (C), N, DT);
589 Next_Component_Or_Discriminant (C);
590 end loop;
592 if Has_Discriminants (T)
593 and then Present (Discriminant_Constraint (T))
594 then
595 D := First_Elmt (Discriminant_Constraint (T));
596 while Present (D) loop
597 if not Is_Static_Expression (Node (D)) then
598 Note_Uplevel_Bound (Node (D), N);
599 DT := True;
600 end if;
602 Next_Elmt (D);
603 end loop;
604 end if;
605 end;
607 -- For array type, check index types and component type
609 elsif Is_Array_Type (T) then
610 declare
611 IX : Node_Id;
612 begin
613 Check_Static_Type (Component_Type (T), N, DT);
615 IX := First_Index (T);
616 while Present (IX) loop
617 Check_Static_Type (Etype (IX), N, DT);
618 Next_Index (IX);
619 end loop;
620 end;
622 -- For private type, examine whether full view is static
624 elsif Is_Private_Type (T) and then Present (Full_View (T)) then
625 Check_Static_Type (Full_View (T), N, DT);
627 if Is_Static_Type (Full_View (T)) then
628 Set_Is_Static_Type (T);
629 end if;
631 -- For now, ignore other types
633 else
634 return;
635 end if;
637 if not DT then
638 Set_Is_Static_Type (T);
639 end if;
640 end Check_Static_Type;
642 ----------------------
643 -- Note_Uplevel_Ref --
644 ----------------------
646 procedure Note_Uplevel_Ref
647 (E : Entity_Id;
648 N : Node_Id;
649 Caller : Entity_Id;
650 Callee : Entity_Id)
652 Full_E : Entity_Id := E;
653 begin
654 -- Nothing to do for static type
656 if Is_Static_Type (E) then
657 return;
658 end if;
660 -- Nothing to do if Caller and Callee are the same
662 if Caller = Callee then
663 return;
665 -- Callee may be a function that returns an array, and that has
666 -- been rewritten as a procedure. If caller is that procedure,
667 -- nothing to do either.
669 elsif Ekind (Callee) = E_Function
670 and then Rewritten_For_C (Callee)
671 and then Corresponding_Procedure (Callee) = Caller
672 then
673 return;
674 end if;
676 -- We have a new uplevel referenced entity
678 if Ekind (E) = E_Constant and then Present (Full_View (E)) then
679 Full_E := Full_View (E);
680 end if;
682 -- All we do at this stage is to add the uplevel reference to
683 -- the table. It's too early to do anything else, since this
684 -- uplevel reference may come from an unreachable subprogram
685 -- in which case the entry will be deleted.
687 Urefs.Append ((N, Full_E, Caller, Callee));
688 end Note_Uplevel_Ref;
690 -------------------------
691 -- Register_Subprogram --
692 -------------------------
694 procedure Register_Subprogram (E : Entity_Id; Bod : Node_Id) is
695 L : constant Nat := Get_Level (Subp, E);
697 begin
698 Subps.Append
699 ((Ent => E,
700 Bod => Bod,
701 Lev => L,
702 Reachable => False,
703 Uplevel_Ref => L,
704 Declares_AREC => False,
705 Uents => No_Elist,
706 Last => 0,
707 ARECnF => Empty,
708 ARECn => Empty,
709 ARECnT => Empty,
710 ARECnPT => Empty,
711 ARECnP => Empty,
712 ARECnU => Empty));
714 Set_Subps_Index (E, UI_From_Int (Subps.Last));
715 end Register_Subprogram;
717 -- Start of processing for Visit_Node
719 begin
720 case Nkind (N) is
722 -- Record a subprogram call
724 when N_Function_Call
725 | N_Procedure_Call_Statement
727 -- We are only interested in direct calls, not indirect
728 -- calls (where Name (N) is an explicit dereference) at
729 -- least for now!
731 if Nkind (Name (N)) in N_Has_Entity then
732 Ent := Entity (Name (N));
734 -- We are only interested in calls to subprograms nested
735 -- within Subp. Calls to Subp itself or to subprograms
736 -- outside the nested structure do not affect us.
738 if Scope_Within (Ent, Subp)
739 and then Is_Subprogram (Ent)
740 and then not Is_Imported (Ent)
741 then
742 Append_Unique_Call ((N, Current_Subprogram, Ent));
743 end if;
744 end if;
746 -- For all calls where the formal is an unconstrained array
747 -- and the actual is constrained we need to check the bounds
748 -- for uplevel references.
750 declare
751 Actual : Entity_Id;
752 DT : Boolean := False;
753 Formal : Node_Id;
754 Subp : Entity_Id;
756 begin
757 if Nkind (Name (N)) = N_Explicit_Dereference then
758 Subp := Etype (Name (N));
759 else
760 Subp := Entity (Name (N));
761 end if;
763 Actual := First_Actual (N);
764 Formal := First_Formal_With_Extras (Subp);
765 while Present (Actual) loop
766 if Is_Array_Type (Etype (Formal))
767 and then not Is_Constrained (Etype (Formal))
768 and then Is_Constrained (Etype (Actual))
769 then
770 Check_Static_Type (Etype (Actual), Empty, DT);
771 end if;
773 Next_Actual (Actual);
774 Next_Formal_With_Extras (Formal);
775 end loop;
776 end;
778 -- An At_End_Proc in a statement sequence indicates that there
779 -- is a call from the enclosing construct or block to that
780 -- subprogram. As above, the called entity must be local and
781 -- not imported.
783 when N_Handled_Sequence_Of_Statements =>
784 if Present (At_End_Proc (N))
785 and then Scope_Within (Entity (At_End_Proc (N)), Subp)
786 and then not Is_Imported (Entity (At_End_Proc (N)))
787 then
788 Append_Unique_Call
789 ((N, Current_Subprogram, Entity (At_End_Proc (N))));
790 end if;
792 -- Similarly, the following constructs include a semantic
793 -- attribute Procedure_To_Call that must be handled like
794 -- other calls.
796 when N_Allocator
797 | N_Extended_Return_Statement
798 | N_Free_Statement
799 | N_Simple_Return_Statement
801 declare
802 Proc : constant Entity_Id := Procedure_To_Call (N);
803 begin
804 if Present (Proc)
805 and then Scope_Within (Proc, Subp)
806 and then not Is_Imported (Proc)
807 then
808 Append_Unique_Call ((N, Current_Subprogram, Proc));
809 end if;
810 end;
812 -- For an allocator with a qualified expression, check type
813 -- of expression being qualified. The explicit type name is
814 -- handled as an entity reference.
816 if Nkind (N) = N_Allocator
817 and then Nkind (Expression (N)) = N_Qualified_Expression
818 then
819 declare
820 DT : Boolean := False;
821 begin
822 Check_Static_Type
823 (Etype (Expression (Expression (N))), Empty, DT);
824 end;
825 end if;
827 -- A 'Access reference is a (potential) call. Other attributes
828 -- require special handling.
830 when N_Attribute_Reference =>
831 declare
832 Attr : constant Attribute_Id :=
833 Get_Attribute_Id (Attribute_Name (N));
834 begin
835 case Attr is
836 when Attribute_Access
837 | Attribute_Unchecked_Access
838 | Attribute_Unrestricted_Access
840 if Nkind (Prefix (N)) in N_Has_Entity then
841 Ent := Entity (Prefix (N));
843 -- We only need to examine calls to subprograms
844 -- nested within current Subp.
846 if Scope_Within (Ent, Subp) then
847 if Is_Imported (Ent) then
848 null;
850 elsif Is_Subprogram (Ent) then
851 Append_Unique_Call
852 ((N, Current_Subprogram, Ent));
853 end if;
854 end if;
855 end if;
857 -- References to bounds can be uplevel references if
858 -- the type isn't static.
860 when Attribute_First
861 | Attribute_Last
862 | Attribute_Length
864 -- Special-case attributes of objects whose bounds
865 -- may be uplevel references. More complex prefixes
866 -- handled during full traversal. Note that if the
867 -- nominal subtype of the prefix is unconstrained,
868 -- the bound must be obtained from the object, not
869 -- from the (possibly) uplevel reference.
871 if Is_Constrained (Etype (Prefix (N))) then
872 declare
873 DT : Boolean := False;
874 begin
875 Check_Static_Type
876 (Etype (Prefix (N)), Empty, DT);
877 end;
879 return OK;
880 end if;
882 when others =>
883 null;
884 end case;
885 end;
887 -- Component associations in aggregates are either static or
888 -- else the aggregate will be expanded into assignments, in
889 -- which case the expression is analyzed later and provides
890 -- no relevant code generation.
892 when N_Component_Association =>
893 if No (Etype (Expression (N))) then
894 return Skip;
895 end if;
897 -- Generic associations are not analyzed: the actuals are
898 -- transferred to renaming and subtype declarations that
899 -- are the ones that must be examined.
901 when N_Generic_Association =>
902 return Skip;
904 -- Indexed references can be uplevel if the type isn't static
905 -- and if the lower bound (or an inner bound for a multi-
906 -- dimensional array) is uplevel.
908 when N_Indexed_Component | N_Slice =>
909 if Is_Constrained (Etype (Prefix (N))) then
910 declare
911 DT : Boolean := False;
912 begin
913 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
914 end;
915 end if;
917 -- A selected component can have an implicit up-level
918 -- reference due to the bounds of previous fields in the
919 -- record. We simplify the processing here by examining
920 -- all components of the record.
922 -- Selected components appear as unit names and end labels
923 -- for child units. Prefixes of these nodes denote parent
924 -- units and carry no type information so they are skipped.
926 when N_Selected_Component =>
927 if Present (Etype (Prefix (N))) then
928 declare
929 DT : Boolean := False;
930 begin
931 Check_Static_Type (Etype (Prefix (N)), Empty, DT);
932 end;
933 end if;
935 -- Record a subprogram. We record a subprogram body that acts
936 -- as a spec. Otherwise we record a subprogram declaration,
937 -- providing that it has a corresponding body we can get hold
938 -- of. The case of no corresponding body being available is
939 -- ignored for now.
941 when N_Subprogram_Body =>
942 Ent := Unique_Defining_Entity (N);
944 -- Ignore generic subprogram
946 if Is_Generic_Subprogram (Ent) then
947 return Skip;
948 end if;
950 -- Make new entry in subprogram table if not already made
952 Register_Subprogram (Ent, N);
954 -- We make a recursive call to scan the subprogram body, so
955 -- that we can save and restore Current_Subprogram.
957 declare
958 Save_CS : constant Entity_Id := Current_Subprogram;
959 Decl : Node_Id;
961 begin
962 Current_Subprogram := Ent;
964 -- Scan declarations
966 Decl := First (Declarations (N));
967 while Present (Decl) loop
968 Visit (Decl);
969 Next (Decl);
970 end loop;
972 -- Scan statements
974 Visit (Handled_Statement_Sequence (N));
976 -- Restore current subprogram setting
978 Current_Subprogram := Save_CS;
979 end;
981 -- Now at this level, return skipping the subprogram body
982 -- descendants, since we already took care of them!
984 return Skip;
986 -- If we have a body stub, visit the associated subunit, which
987 -- is a semantic descendant of the stub.
989 when N_Body_Stub =>
990 Visit (Library_Unit (N));
992 -- A declaration of a wrapper package indicates a subprogram
993 -- instance for which there is no explicit body. Enter the
994 -- subprogram instance in the table.
996 when N_Package_Declaration =>
997 if Is_Wrapper_Package (Defining_Entity (N)) then
998 Register_Subprogram
999 (Related_Instance (Defining_Entity (N)), Empty);
1000 end if;
1002 -- Skip generic declarations
1004 when N_Generic_Declaration =>
1005 return Skip;
1007 -- Skip generic package body
1009 when N_Package_Body =>
1010 if Present (Corresponding_Spec (N))
1011 and then Ekind (Corresponding_Spec (N)) = E_Generic_Package
1012 then
1013 return Skip;
1014 end if;
1016 -- Otherwise record an uplevel reference in a local
1017 -- identifier.
1019 when others =>
1020 if Nkind (N) in N_Has_Entity
1021 and then Present (Entity (N))
1022 then
1023 Ent := Entity (N);
1025 -- Only interested in entities declared within our nest
1027 if not Is_Library_Level_Entity (Ent)
1028 and then Scope_Within_Or_Same (Scope (Ent), Subp)
1030 -- Skip entities defined in inlined subprograms
1032 and then
1033 Chars (Enclosing_Subprogram (Ent)) /= Name_uParent
1035 -- Constants and variables are potentially uplevel
1036 -- references to global declarations.
1038 and then
1039 (Ekind_In (Ent, E_Constant, E_Variable)
1041 -- Formals are interesting, but not if being used as
1042 -- mere names of parameters for name notation calls.
1044 or else
1045 (Is_Formal (Ent)
1046 and then not
1047 (Nkind (Parent (N)) = N_Parameter_Association
1048 and then Selector_Name (Parent (N)) = N))
1050 -- Types other than known Is_Static types are
1051 -- potentially interesting.
1053 or else (Is_Type (Ent)
1054 and then not Is_Static_Type (Ent)))
1055 then
1056 -- Here we have a potentially interesting uplevel
1057 -- reference to examine.
1059 if Is_Type (Ent) then
1060 declare
1061 DT : Boolean := False;
1063 begin
1064 Check_Static_Type (Ent, N, DT);
1066 if Is_Static_Type (Ent) then
1067 return OK;
1068 end if;
1069 end;
1070 end if;
1072 Caller := Current_Subprogram;
1073 Callee := Enclosing_Subprogram (Ent);
1075 if Callee /= Caller
1076 and then (not Is_Static_Type (Ent)
1077 or else Needs_Fat_Pointer (Ent))
1078 then
1079 Note_Uplevel_Ref (Ent, N, Caller, Callee);
1081 -- Check the type of a formal parameter of the current
1082 -- subprogram, whose formal type may be an uplevel
1083 -- reference.
1085 elsif Is_Formal (Ent)
1086 and then Scope (Ent) = Current_Subprogram
1087 then
1088 declare
1089 DT : Boolean := False;
1091 begin
1092 Check_Static_Type (Etype (Ent), Empty, DT);
1093 end;
1094 end if;
1095 end if;
1096 end if;
1097 end case;
1099 -- Fall through to continue scanning children of this node
1101 return OK;
1102 end Visit_Node;
1104 -- Start of processing for Build_Tables
1106 begin
1107 -- Traverse the body to get subprograms, calls and uplevel references
1109 Visit (Subp_Body);
1110 end Build_Tables;
1112 -- Now do the first transitive closure which determines which
1113 -- subprograms in the nest are actually reachable.
1115 Reachable_Closure : declare
1116 Modified : Boolean;
1118 begin
1119 Subps.Table (Subps_First).Reachable := True;
1121 -- We use a simple minded algorithm as follows (obviously this can
1122 -- be done more efficiently, using one of the standard algorithms
1123 -- for efficient transitive closure computation, but this is simple
1124 -- and most likely fast enough that its speed does not matter).
1126 -- Repeatedly scan the list of calls. Any time we find a call from
1127 -- A to B, where A is reachable, but B is not, then B is reachable,
1128 -- and note that we have made a change by setting Modified True. We
1129 -- repeat this until we make a pass with no modifications.
1131 Outer : loop
1132 Modified := False;
1133 Inner : for J in Calls.First .. Calls.Last loop
1134 declare
1135 CTJ : Call_Entry renames Calls.Table (J);
1137 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1138 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1140 SUBF : Subp_Entry renames Subps.Table (SINF);
1141 SUBT : Subp_Entry renames Subps.Table (SINT);
1143 begin
1144 if SUBF.Reachable and then not SUBT.Reachable then
1145 SUBT.Reachable := True;
1146 Modified := True;
1147 end if;
1148 end;
1149 end loop Inner;
1151 exit Outer when not Modified;
1152 end loop Outer;
1153 end Reachable_Closure;
1155 -- Remove calls from unreachable subprograms
1157 declare
1158 New_Index : Nat;
1160 begin
1161 New_Index := 0;
1162 for J in Calls.First .. Calls.Last loop
1163 declare
1164 CTJ : Call_Entry renames Calls.Table (J);
1166 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1167 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1169 SUBF : Subp_Entry renames Subps.Table (SINF);
1170 SUBT : Subp_Entry renames Subps.Table (SINT);
1172 begin
1173 if SUBF.Reachable then
1174 pragma Assert (SUBT.Reachable);
1175 New_Index := New_Index + 1;
1176 Calls.Table (New_Index) := Calls.Table (J);
1177 end if;
1178 end;
1179 end loop;
1181 Calls.Set_Last (New_Index);
1182 end;
1184 -- Remove uplevel references from unreachable subprograms
1186 declare
1187 New_Index : Nat;
1189 begin
1190 New_Index := 0;
1191 for J in Urefs.First .. Urefs.Last loop
1192 declare
1193 URJ : Uref_Entry renames Urefs.Table (J);
1195 SINF : constant SI_Type := Subp_Index (URJ.Caller);
1196 SINT : constant SI_Type := Subp_Index (URJ.Callee);
1198 SUBF : Subp_Entry renames Subps.Table (SINF);
1199 SUBT : Subp_Entry renames Subps.Table (SINT);
1201 S : Entity_Id;
1203 begin
1204 -- Keep reachable reference
1206 if SUBF.Reachable then
1207 New_Index := New_Index + 1;
1208 Urefs.Table (New_Index) := Urefs.Table (J);
1210 -- And since we know we are keeping this one, this is a good
1211 -- place to fill in information for a good reference.
1213 -- Mark all enclosing subprograms need to declare AREC
1215 S := URJ.Caller;
1216 loop
1217 S := Enclosing_Subprogram (S);
1219 -- if we are at the top level, as can happen with
1220 -- references to formals in aspects of nested subprogram
1221 -- declarations, there are no further subprograms to
1222 -- mark as requiring activation records.
1224 exit when No (S);
1225 Subps.Table (Subp_Index (S)).Declares_AREC := True;
1226 exit when S = URJ.Callee;
1227 end loop;
1229 -- Add to list of uplevel referenced entities for Callee.
1230 -- We do not add types to this list, only actual references
1231 -- to objects that will be referenced uplevel, and we use
1232 -- the flag Is_Uplevel_Referenced_Entity to avoid making
1233 -- duplicate entries in the list.
1234 -- Discriminants are also excluded, only the enclosing
1235 -- object can appear in the list.
1237 if not Is_Uplevel_Referenced_Entity (URJ.Ent)
1238 and then Ekind (URJ.Ent) /= E_Discriminant
1239 then
1240 Set_Is_Uplevel_Referenced_Entity (URJ.Ent);
1242 if not Is_Type (URJ.Ent) then
1243 Append_New_Elmt (URJ.Ent, SUBT.Uents);
1244 end if;
1245 end if;
1247 -- And set uplevel indication for caller
1249 if SUBT.Lev < SUBF.Uplevel_Ref then
1250 SUBF.Uplevel_Ref := SUBT.Lev;
1251 end if;
1252 end if;
1253 end;
1254 end loop;
1256 Urefs.Set_Last (New_Index);
1257 end;
1259 -- Remove unreachable subprograms from Subps table. Note that we do
1260 -- this after eliminating entries from the other two tables, since
1261 -- those elimination steps depend on referencing the Subps table.
1263 declare
1264 New_SI : SI_Type;
1266 begin
1267 New_SI := Subps_First - 1;
1268 for J in Subps_First .. Subps.Last loop
1269 declare
1270 STJ : Subp_Entry renames Subps.Table (J);
1271 Spec : Node_Id;
1272 Decl : Node_Id;
1274 begin
1275 -- Subprograms declared in tasks and protected types are
1276 -- reachable and cannot be eliminated.
1278 if In_Synchronized_Unit (STJ.Ent) then
1279 STJ.Reachable := True;
1280 end if;
1282 -- Subprogram is reachable, copy and reset index
1284 if STJ.Reachable then
1285 New_SI := New_SI + 1;
1286 Subps.Table (New_SI) := STJ;
1287 Set_Subps_Index (STJ.Ent, UI_From_Int (New_SI));
1289 -- Subprogram is not reachable
1291 else
1292 -- Clear index, since no longer active
1294 Set_Subps_Index (Subps.Table (J).Ent, Uint_0);
1296 -- Output debug information if -gnatd.3 set
1298 if Debug_Flag_Dot_3 then
1299 Write_Str ("Eliminate ");
1300 Write_Name (Chars (Subps.Table (J).Ent));
1301 Write_Str (" at ");
1302 Write_Location (Sloc (Subps.Table (J).Ent));
1303 Write_Str (" (not referenced)");
1304 Write_Eol;
1305 end if;
1307 -- Rewrite declaration and body to null statements
1309 -- A subprogram instantiation does not have an explicit
1310 -- body. If unused, we could remove the corresponding
1311 -- wrapper package and its body (TBD).
1313 if Present (STJ.Bod) then
1314 Spec := Corresponding_Spec (STJ.Bod);
1316 if Present (Spec) then
1317 Decl := Parent (Declaration_Node (Spec));
1318 Rewrite (Decl, Make_Null_Statement (Sloc (Decl)));
1319 end if;
1321 Rewrite (STJ.Bod, Make_Null_Statement (Sloc (STJ.Bod)));
1322 end if;
1323 end if;
1324 end;
1325 end loop;
1327 Subps.Set_Last (New_SI);
1328 end;
1330 -- Now it is time for the second transitive closure, which follows calls
1331 -- and makes sure that A calls B, and B has uplevel references, then A
1332 -- is also marked as having uplevel references.
1334 Closure_Uplevel : declare
1335 Modified : Boolean;
1337 begin
1338 -- We use a simple minded algorithm as follows (obviously this can
1339 -- be done more efficiently, using one of the standard algorithms
1340 -- for efficient transitive closure computation, but this is simple
1341 -- and most likely fast enough that its speed does not matter).
1343 -- Repeatedly scan the list of calls. Any time we find a call from
1344 -- A to B, where B has uplevel references, make sure that A is marked
1345 -- as having at least the same level of uplevel referencing.
1347 Outer2 : loop
1348 Modified := False;
1349 Inner2 : for J in Calls.First .. Calls.Last loop
1350 declare
1351 CTJ : Call_Entry renames Calls.Table (J);
1352 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1353 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1354 SUBF : Subp_Entry renames Subps.Table (SINF);
1355 SUBT : Subp_Entry renames Subps.Table (SINT);
1356 begin
1357 if SUBT.Lev > SUBT.Uplevel_Ref
1358 and then SUBF.Uplevel_Ref > SUBT.Uplevel_Ref
1359 then
1360 SUBF.Uplevel_Ref := SUBT.Uplevel_Ref;
1361 Modified := True;
1362 end if;
1363 end;
1364 end loop Inner2;
1366 exit Outer2 when not Modified;
1367 end loop Outer2;
1368 end Closure_Uplevel;
1370 -- We have one more step before the tables are complete. An uplevel
1371 -- call from subprogram A to subprogram B where subprogram B has uplevel
1372 -- references is in effect an uplevel reference, and must arrange for
1373 -- the proper activation link to be passed.
1375 for J in Calls.First .. Calls.Last loop
1376 declare
1377 CTJ : Call_Entry renames Calls.Table (J);
1379 SINF : constant SI_Type := Subp_Index (CTJ.Caller);
1380 SINT : constant SI_Type := Subp_Index (CTJ.Callee);
1382 SUBF : Subp_Entry renames Subps.Table (SINF);
1383 SUBT : Subp_Entry renames Subps.Table (SINT);
1385 A : Entity_Id;
1387 begin
1388 -- If callee has uplevel references
1390 if SUBT.Uplevel_Ref < SUBT.Lev
1392 -- And this is an uplevel call
1394 and then SUBT.Lev < SUBF.Lev
1395 then
1396 -- We need to arrange for finding the uplink
1398 A := CTJ.Caller;
1399 loop
1400 A := Enclosing_Subprogram (A);
1401 Subps.Table (Subp_Index (A)).Declares_AREC := True;
1402 exit when A = CTJ.Callee;
1404 -- In any case exit when we get to the outer level. This
1405 -- happens in some odd cases with generics (in particular
1406 -- sem_ch3.adb does not compile without this kludge ???).
1408 exit when A = Subp;
1409 end loop;
1410 end if;
1411 end;
1412 end loop;
1414 -- The tables are now complete, so we can record the last index in the
1415 -- Subps table for later reference in Cprint.
1417 Subps.Table (Subps_First).Last := Subps.Last;
1419 -- Next step, create the entities for code we will insert. We do this
1420 -- at the start so that all the entities are defined, regardless of the
1421 -- order in which we do the code insertions.
1423 Create_Entities : for J in Subps_First .. Subps.Last loop
1424 declare
1425 STJ : Subp_Entry renames Subps.Table (J);
1426 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1428 begin
1429 -- First we create the ARECnF entity for the additional formal for
1430 -- all subprograms which need an activation record passed.
1432 if STJ.Uplevel_Ref < STJ.Lev then
1433 STJ.ARECnF :=
1434 Make_Defining_Identifier (Loc, Chars => AREC_Name (J, "F"));
1435 end if;
1437 -- Define the AREC entities for the activation record if needed
1439 if STJ.Declares_AREC then
1440 STJ.ARECn :=
1441 Make_Defining_Identifier (Loc, AREC_Name (J, ""));
1442 STJ.ARECnT :=
1443 Make_Defining_Identifier (Loc, AREC_Name (J, "T"));
1444 STJ.ARECnPT :=
1445 Make_Defining_Identifier (Loc, AREC_Name (J, "PT"));
1446 STJ.ARECnP :=
1447 Make_Defining_Identifier (Loc, AREC_Name (J, "P"));
1449 -- Define uplink component entity if inner nesting case
1451 if Present (STJ.ARECnF) then
1452 STJ.ARECnU :=
1453 Make_Defining_Identifier (Loc, AREC_Name (J, "U"));
1454 end if;
1455 end if;
1456 end;
1457 end loop Create_Entities;
1459 -- Loop through subprograms
1461 Subp_Loop : declare
1462 Addr : constant Entity_Id := RTE (RE_Address);
1464 begin
1465 for J in Subps_First .. Subps.Last loop
1466 declare
1467 STJ : Subp_Entry renames Subps.Table (J);
1469 begin
1470 -- First add the extra formal if needed. This applies to all
1471 -- nested subprograms that require an activation record to be
1472 -- passed, as indicated by ARECnF being defined.
1474 if Present (STJ.ARECnF) then
1476 -- Here we need the extra formal. We do the expansion and
1477 -- analysis of this manually, since it is fairly simple,
1478 -- and it is not obvious how we can get what we want if we
1479 -- try to use the normal Analyze circuit.
1481 Add_Extra_Formal : declare
1482 Encl : constant SI_Type := Enclosing_Subp (J);
1483 STJE : Subp_Entry renames Subps.Table (Encl);
1484 -- Index and Subp_Entry for enclosing routine
1486 Form : constant Entity_Id := STJ.ARECnF;
1487 -- The formal to be added. Note that n here is one less
1488 -- than the level of the subprogram itself (STJ.Ent).
1490 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id);
1491 -- S is an N_Function/Procedure_Specification node, and F
1492 -- is the new entity to add to this subprogramn spec as
1493 -- the last Extra_Formal.
1495 ----------------------
1496 -- Add_Form_To_Spec --
1497 ----------------------
1499 procedure Add_Form_To_Spec (F : Entity_Id; S : Node_Id) is
1500 Sub : constant Entity_Id := Defining_Entity (S);
1501 Ent : Entity_Id;
1503 begin
1504 -- Case of at least one Extra_Formal is present, set
1505 -- ARECnF as the new last entry in the list.
1507 if Present (Extra_Formals (Sub)) then
1508 Ent := Extra_Formals (Sub);
1509 while Present (Extra_Formal (Ent)) loop
1510 Ent := Extra_Formal (Ent);
1511 end loop;
1513 Set_Extra_Formal (Ent, F);
1515 -- No Extra formals present
1517 else
1518 Set_Extra_Formals (Sub, F);
1519 Ent := Last_Formal (Sub);
1521 if Present (Ent) then
1522 Set_Extra_Formal (Ent, F);
1523 end if;
1524 end if;
1525 end Add_Form_To_Spec;
1527 -- Start of processing for Add_Extra_Formal
1529 begin
1530 -- Decorate the new formal entity
1532 Set_Scope (Form, STJ.Ent);
1533 Set_Ekind (Form, E_In_Parameter);
1534 Set_Etype (Form, STJE.ARECnPT);
1535 Set_Mechanism (Form, By_Copy);
1536 Set_Never_Set_In_Source (Form, True);
1537 Set_Analyzed (Form, True);
1538 Set_Comes_From_Source (Form, False);
1539 Set_Is_Activation_Record (Form, True);
1541 -- Case of only body present
1543 if Acts_As_Spec (STJ.Bod) then
1544 Add_Form_To_Spec (Form, Specification (STJ.Bod));
1546 -- Case of separate spec
1548 else
1549 Add_Form_To_Spec (Form, Parent (STJ.Ent));
1550 end if;
1551 end Add_Extra_Formal;
1552 end if;
1554 -- Processing for subprograms that declare an activation record
1556 if Present (STJ.ARECn) then
1558 -- Local declarations for one such subprogram
1560 declare
1561 Loc : constant Source_Ptr := Sloc (STJ.Bod);
1563 Decls : constant List_Id := New_List;
1564 -- List of new declarations we create
1566 Clist : List_Id;
1567 Comp : Entity_Id;
1569 Decl_Assign : Node_Id;
1570 -- Assigment to set uplink, Empty if none
1572 Decl_ARECnT : Node_Id;
1573 Decl_ARECnPT : Node_Id;
1574 Decl_ARECn : Node_Id;
1575 Decl_ARECnP : Node_Id;
1576 -- Declaration nodes for the AREC entities we build
1578 begin
1579 -- Build list of component declarations for ARECnT
1581 Clist := Empty_List;
1583 -- If we are in a subprogram that has a static link that
1584 -- is passed in (as indicated by ARECnF being defined),
1585 -- then include ARECnU : ARECmPT where ARECmPT comes from
1586 -- the level one higher than the current level, and the
1587 -- entity ARECnPT comes from the enclosing subprogram.
1589 if Present (STJ.ARECnF) then
1590 declare
1591 STJE : Subp_Entry
1592 renames Subps.Table (Enclosing_Subp (J));
1593 begin
1594 Append_To (Clist,
1595 Make_Component_Declaration (Loc,
1596 Defining_Identifier => STJ.ARECnU,
1597 Component_Definition =>
1598 Make_Component_Definition (Loc,
1599 Subtype_Indication =>
1600 New_Occurrence_Of (STJE.ARECnPT, Loc))));
1601 end;
1602 end if;
1604 -- Add components for uplevel referenced entities
1606 if Present (STJ.Uents) then
1607 declare
1608 Elmt : Elmt_Id;
1609 Ptr_Decl : Node_Id;
1610 Uent : Entity_Id;
1612 Indx : Nat;
1613 -- 1's origin of index in list of elements. This is
1614 -- used to uniquify names if needed in Upref_Name.
1616 begin
1617 Elmt := First_Elmt (STJ.Uents);
1618 Indx := 0;
1619 while Present (Elmt) loop
1620 Uent := Node (Elmt);
1621 Indx := Indx + 1;
1623 Comp :=
1624 Make_Defining_Identifier (Loc,
1625 Chars => Upref_Name (Uent, Indx, Clist));
1627 Set_Activation_Record_Component
1628 (Uent, Comp);
1630 if Needs_Fat_Pointer (Uent) then
1632 -- Build corresponding access type
1634 Ptr_Decl :=
1635 Build_Access_Type_Decl
1636 (Etype (Uent), STJ.Ent);
1637 Append_To (Decls, Ptr_Decl);
1639 -- And use its type in the corresponding
1640 -- component.
1642 Append_To (Clist,
1643 Make_Component_Declaration (Loc,
1644 Defining_Identifier => Comp,
1645 Component_Definition =>
1646 Make_Component_Definition (Loc,
1647 Subtype_Indication =>
1648 New_Occurrence_Of
1649 (Defining_Identifier (Ptr_Decl),
1650 Loc))));
1651 else
1652 Append_To (Clist,
1653 Make_Component_Declaration (Loc,
1654 Defining_Identifier => Comp,
1655 Component_Definition =>
1656 Make_Component_Definition (Loc,
1657 Subtype_Indication =>
1658 New_Occurrence_Of (Addr, Loc))));
1659 end if;
1660 Next_Elmt (Elmt);
1661 end loop;
1662 end;
1663 end if;
1665 -- Now we can insert the AREC declarations into the body
1666 -- type ARECnT is record .. end record;
1667 -- pragma Suppress_Initialization (ARECnT);
1669 -- Note that we need to set the Suppress_Initialization
1670 -- flag after Decl_ARECnT has been analyzed.
1672 Decl_ARECnT :=
1673 Make_Full_Type_Declaration (Loc,
1674 Defining_Identifier => STJ.ARECnT,
1675 Type_Definition =>
1676 Make_Record_Definition (Loc,
1677 Component_List =>
1678 Make_Component_List (Loc,
1679 Component_Items => Clist)));
1680 Append_To (Decls, Decl_ARECnT);
1682 -- type ARECnPT is access all ARECnT;
1684 Decl_ARECnPT :=
1685 Make_Full_Type_Declaration (Loc,
1686 Defining_Identifier => STJ.ARECnPT,
1687 Type_Definition =>
1688 Make_Access_To_Object_Definition (Loc,
1689 All_Present => True,
1690 Subtype_Indication =>
1691 New_Occurrence_Of (STJ.ARECnT, Loc)));
1692 Append_To (Decls, Decl_ARECnPT);
1694 -- ARECn : aliased ARECnT;
1696 Decl_ARECn :=
1697 Make_Object_Declaration (Loc,
1698 Defining_Identifier => STJ.ARECn,
1699 Aliased_Present => True,
1700 Object_Definition =>
1701 New_Occurrence_Of (STJ.ARECnT, Loc));
1702 Append_To (Decls, Decl_ARECn);
1704 -- ARECnP : constant ARECnPT := ARECn'Access;
1706 Decl_ARECnP :=
1707 Make_Object_Declaration (Loc,
1708 Defining_Identifier => STJ.ARECnP,
1709 Constant_Present => True,
1710 Object_Definition =>
1711 New_Occurrence_Of (STJ.ARECnPT, Loc),
1712 Expression =>
1713 Make_Attribute_Reference (Loc,
1714 Prefix =>
1715 New_Occurrence_Of (STJ.ARECn, Loc),
1716 Attribute_Name => Name_Access));
1717 Append_To (Decls, Decl_ARECnP);
1719 -- If we are in a subprogram that has a static link that
1720 -- is passed in (as indicated by ARECnF being defined),
1721 -- then generate ARECn.ARECmU := ARECmF where m is
1722 -- one less than the current level to set the uplink.
1724 if Present (STJ.ARECnF) then
1725 Decl_Assign :=
1726 Make_Assignment_Statement (Loc,
1727 Name =>
1728 Make_Selected_Component (Loc,
1729 Prefix =>
1730 New_Occurrence_Of (STJ.ARECn, Loc),
1731 Selector_Name =>
1732 New_Occurrence_Of (STJ.ARECnU, Loc)),
1733 Expression =>
1734 New_Occurrence_Of (STJ.ARECnF, Loc));
1735 Append_To (Decls, Decl_Assign);
1737 else
1738 Decl_Assign := Empty;
1739 end if;
1741 Prepend_List_To (Declarations (STJ.Bod), Decls);
1743 -- Analyze the newly inserted declarations. Note that we
1744 -- do not need to establish the whole scope stack, since
1745 -- we have already set all entity fields (so there will
1746 -- be no searching of upper scopes to resolve names). But
1747 -- we do set the scope of the current subprogram, so that
1748 -- newly created entities go in the right entity chain.
1750 -- We analyze with all checks suppressed (since we do
1751 -- not expect any exceptions).
1753 Push_Scope (STJ.Ent);
1754 Analyze (Decl_ARECnT, Suppress => All_Checks);
1756 -- Note that we need to call Set_Suppress_Initialization
1757 -- after Decl_ARECnT has been analyzed, but before
1758 -- analyzing Decl_ARECnP so that the flag is properly
1759 -- taking into account.
1761 Set_Suppress_Initialization (STJ.ARECnT);
1763 Analyze (Decl_ARECnPT, Suppress => All_Checks);
1764 Analyze (Decl_ARECn, Suppress => All_Checks);
1765 Analyze (Decl_ARECnP, Suppress => All_Checks);
1767 if Present (Decl_Assign) then
1768 Analyze (Decl_Assign, Suppress => All_Checks);
1769 end if;
1771 Pop_Scope;
1773 -- Next step, for each uplevel referenced entity, add
1774 -- assignment operations to set the component in the
1775 -- activation record.
1777 if Present (STJ.Uents) then
1778 declare
1779 Elmt : Elmt_Id;
1781 begin
1782 Elmt := First_Elmt (STJ.Uents);
1783 while Present (Elmt) loop
1784 declare
1785 Ent : constant Entity_Id := Node (Elmt);
1786 Loc : constant Source_Ptr := Sloc (Ent);
1787 Dec : constant Node_Id :=
1788 Declaration_Node (Ent);
1790 Asn : Node_Id;
1791 Attr : Name_Id;
1792 Ins : Node_Id;
1794 begin
1795 -- For parameters, we insert the assignment
1796 -- right after the declaration of ARECnP.
1797 -- For all other entities, we insert
1798 -- the assignment immediately after the
1799 -- declaration of the entity.
1801 -- Note: we don't need to mark the entity
1802 -- as being aliased, because the address
1803 -- attribute will mark it as Address_Taken,
1804 -- and that is good enough.
1806 if Is_Formal (Ent) then
1807 Ins := Decl_ARECnP;
1808 else
1809 Ins := Dec;
1810 end if;
1812 -- Build and insert the assignment:
1813 -- ARECn.nam := nam'Address
1814 -- or else 'Access for unconstrained array
1816 if Needs_Fat_Pointer (Ent) then
1817 Attr := Name_Access;
1818 else
1819 Attr := Name_Address;
1820 end if;
1822 Asn :=
1823 Make_Assignment_Statement (Loc,
1824 Name =>
1825 Make_Selected_Component (Loc,
1826 Prefix =>
1827 New_Occurrence_Of (STJ.ARECn, Loc),
1828 Selector_Name =>
1829 New_Occurrence_Of
1830 (Activation_Record_Component
1831 (Ent),
1832 Loc)),
1834 Expression =>
1835 Make_Attribute_Reference (Loc,
1836 Prefix =>
1837 New_Occurrence_Of (Ent, Loc),
1838 Attribute_Name => Attr));
1840 Insert_After (Ins, Asn);
1842 -- Analyze the assignment statement. We do
1843 -- not need to establish the relevant scope
1844 -- stack entries here, because we have
1845 -- already set the correct entity references,
1846 -- so no name resolution is required, and no
1847 -- new entities are created, so we don't even
1848 -- need to set the current scope.
1850 -- We analyze with all checks suppressed
1851 -- (since we do not expect any exceptions).
1853 Analyze (Asn, Suppress => All_Checks);
1854 end;
1856 Next_Elmt (Elmt);
1857 end loop;
1858 end;
1859 end if;
1860 end;
1861 end if;
1862 end;
1863 end loop;
1864 end Subp_Loop;
1866 -- Next step, process uplevel references. This has to be done in a
1867 -- separate pass, after completing the processing in Sub_Loop because we
1868 -- need all the AREC declarations generated, inserted, and analyzed so
1869 -- that the uplevel references can be successfully analyzed.
1871 Uplev_Refs : for J in Urefs.First .. Urefs.Last loop
1872 declare
1873 UPJ : Uref_Entry renames Urefs.Table (J);
1875 begin
1876 -- Ignore type references, these are implicit references that do
1877 -- not need rewriting (e.g. the appearence in a conversion).
1878 -- Also ignore if no reference was specified.
1880 if Is_Type (UPJ.Ent) or else No (UPJ.Ref) then
1881 goto Continue;
1882 end if;
1884 -- Also ignore uplevel references to bounds of types that come
1885 -- from the original type reference.
1887 if Is_Entity_Name (UPJ.Ref)
1888 and then Present (Entity (UPJ.Ref))
1889 and then Is_Type (Entity (UPJ.Ref))
1890 then
1891 goto Continue;
1892 end if;
1894 -- Rewrite one reference
1896 Rewrite_One_Ref : declare
1897 Loc : constant Source_Ptr := Sloc (UPJ.Ref);
1898 -- Source location for the reference
1900 Typ : constant Entity_Id := Etype (UPJ.Ent);
1901 -- The type of the referenced entity
1903 Atyp : constant Entity_Id := Get_Actual_Subtype (UPJ.Ref);
1904 -- The actual subtype of the reference
1906 RS_Caller : constant SI_Type := Subp_Index (UPJ.Caller);
1907 -- Subp_Index for caller containing reference
1909 STJR : Subp_Entry renames Subps.Table (RS_Caller);
1910 -- Subp_Entry for subprogram containing reference
1912 RS_Callee : constant SI_Type := Subp_Index (UPJ.Callee);
1913 -- Subp_Index for subprogram containing referenced entity
1915 STJE : Subp_Entry renames Subps.Table (RS_Callee);
1916 -- Subp_Entry for subprogram containing referenced entity
1918 Pfx : Node_Id;
1919 Comp : Entity_Id;
1920 SI : SI_Type;
1922 begin
1923 -- Ignore if no ARECnF entity for enclosing subprogram which
1924 -- probably happens as a result of not properly treating
1925 -- instance bodies. To be examined ???
1927 -- If this test is omitted, then the compilation of freeze.adb
1928 -- and inline.adb fail in unnesting mode.
1930 if No (STJR.ARECnF) then
1931 goto Continue;
1932 end if;
1934 -- Push the current scope, so that the pointer type Tnn, and
1935 -- any subsidiary entities resulting from the analysis of the
1936 -- rewritten reference, go in the right entity chain.
1938 Push_Scope (STJR.Ent);
1940 -- Now we need to rewrite the reference. We have a reference
1941 -- from level STJR.Lev to level STJE.Lev. The general form of
1942 -- the rewritten reference for entity X is:
1944 -- Typ'Deref (ARECaF.ARECbU.ARECcU.ARECdU....ARECmU.X)
1946 -- where a,b,c,d .. m =
1947 -- STJR.Lev - 1, STJR.Lev - 2, .. STJE.Lev
1949 pragma Assert (STJR.Lev > STJE.Lev);
1951 -- Compute the prefix of X. Here are examples to make things
1952 -- clear (with parens to show groupings, the prefix is
1953 -- everything except the .X at the end).
1955 -- level 2 to level 1
1957 -- AREC1F.X
1959 -- level 3 to level 1
1961 -- (AREC2F.AREC1U).X
1963 -- level 4 to level 1
1965 -- ((AREC3F.AREC2U).AREC1U).X
1967 -- level 6 to level 2
1969 -- (((AREC5F.AREC4U).AREC3U).AREC2U).X
1971 -- In the above, ARECnF and ARECnU are pointers, so there are
1972 -- explicit dereferences required for these occurrences.
1974 Pfx :=
1975 Make_Explicit_Dereference (Loc,
1976 Prefix => New_Occurrence_Of (STJR.ARECnF, Loc));
1977 SI := RS_Caller;
1978 for L in STJE.Lev .. STJR.Lev - 2 loop
1979 SI := Enclosing_Subp (SI);
1980 Pfx :=
1981 Make_Explicit_Dereference (Loc,
1982 Prefix =>
1983 Make_Selected_Component (Loc,
1984 Prefix => Pfx,
1985 Selector_Name =>
1986 New_Occurrence_Of (Subps.Table (SI).ARECnU, Loc)));
1987 end loop;
1989 -- Get activation record component (must exist)
1991 Comp := Activation_Record_Component (UPJ.Ent);
1992 pragma Assert (Present (Comp));
1994 -- Do the replacement. If the component type is an access type,
1995 -- this is an uplevel reference for an entity that requires a
1996 -- fat pointer, so dereference the component.
1998 if Is_Access_Type (Etype (Comp)) then
1999 Rewrite (UPJ.Ref,
2000 Make_Explicit_Dereference (Loc,
2001 Prefix =>
2002 Make_Selected_Component (Loc,
2003 Prefix => Pfx,
2004 Selector_Name =>
2005 New_Occurrence_Of (Comp, Loc))));
2007 else
2008 Rewrite (UPJ.Ref,
2009 Make_Attribute_Reference (Loc,
2010 Prefix => New_Occurrence_Of (Atyp, Loc),
2011 Attribute_Name => Name_Deref,
2012 Expressions => New_List (
2013 Make_Selected_Component (Loc,
2014 Prefix => Pfx,
2015 Selector_Name =>
2016 New_Occurrence_Of (Comp, Loc)))));
2017 end if;
2019 -- Analyze and resolve the new expression. We do not need to
2020 -- establish the relevant scope stack entries here, because we
2021 -- have already set all the correct entity references, so no
2022 -- name resolution is needed. We have already set the current
2023 -- scope, so that any new entities created will be in the right
2024 -- scope.
2026 -- We analyze with all checks suppressed (since we do not
2027 -- expect any exceptions)
2029 Analyze_And_Resolve (UPJ.Ref, Typ, Suppress => All_Checks);
2030 Pop_Scope;
2031 end Rewrite_One_Ref;
2032 end;
2034 <<Continue>>
2035 null;
2036 end loop Uplev_Refs;
2038 -- Finally, loop through all calls adding extra actual for the
2039 -- activation record where it is required.
2041 Adjust_Calls : for J in Calls.First .. Calls.Last loop
2043 -- Process a single call, we are only interested in a call to a
2044 -- subprogram that actually needs a pointer to an activation record,
2045 -- as indicated by the ARECnF entity being set. This excludes the
2046 -- top level subprogram, and any subprogram not having uplevel refs.
2048 Adjust_One_Call : declare
2049 CTJ : Call_Entry renames Calls.Table (J);
2050 STF : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Caller));
2051 STT : Subp_Entry renames Subps.Table (Subp_Index (CTJ.Callee));
2053 Loc : constant Source_Ptr := Sloc (CTJ.N);
2055 Extra : Node_Id;
2056 ExtraP : Node_Id;
2057 SubX : SI_Type;
2058 Act : Node_Id;
2060 begin
2061 if Present (STT.ARECnF)
2062 and then Nkind (CTJ.N) in N_Subprogram_Call
2063 then
2064 -- CTJ.N is a call to a subprogram which may require a pointer
2065 -- to an activation record. The subprogram containing the call
2066 -- is CTJ.From and the subprogram being called is CTJ.To, so we
2067 -- have a call from level STF.Lev to level STT.Lev.
2069 -- There are three possibilities:
2071 -- For a call to the same level, we just pass the activation
2072 -- record passed to the calling subprogram.
2074 if STF.Lev = STT.Lev then
2075 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2077 -- For a call that goes down a level, we pass a pointer to the
2078 -- activation record constructed within the caller (which may
2079 -- be the outer-level subprogram, but also may be a more deeply
2080 -- nested caller).
2082 elsif STT.Lev = STF.Lev + 1 then
2083 Extra := New_Occurrence_Of (STF.ARECnP, Loc);
2085 -- Otherwise we must have an upcall (STT.Lev < STF.LEV),
2086 -- since it is not possible to do a downcall of more than
2087 -- one level.
2089 -- For a call from level STF.Lev to level STT.Lev, we
2090 -- have to find the activation record needed by the
2091 -- callee. This is as follows:
2093 -- ARECaF.ARECbU.ARECcU....ARECmU
2095 -- where a,b,c .. m =
2096 -- STF.Lev - 1, STF.Lev - 2, STF.Lev - 3 .. STT.Lev
2098 else
2099 pragma Assert (STT.Lev < STF.Lev);
2101 Extra := New_Occurrence_Of (STF.ARECnF, Loc);
2102 SubX := Subp_Index (CTJ.Caller);
2103 for K in reverse STT.Lev .. STF.Lev - 1 loop
2104 SubX := Enclosing_Subp (SubX);
2105 Extra :=
2106 Make_Selected_Component (Loc,
2107 Prefix => Extra,
2108 Selector_Name =>
2109 New_Occurrence_Of
2110 (Subps.Table (SubX).ARECnU, Loc));
2111 end loop;
2112 end if;
2114 -- Extra is the additional parameter to be added. Build a
2115 -- parameter association that we can append to the actuals.
2117 ExtraP :=
2118 Make_Parameter_Association (Loc,
2119 Selector_Name =>
2120 New_Occurrence_Of (STT.ARECnF, Loc),
2121 Explicit_Actual_Parameter => Extra);
2123 if No (Parameter_Associations (CTJ.N)) then
2124 Set_Parameter_Associations (CTJ.N, Empty_List);
2125 end if;
2127 Append (ExtraP, Parameter_Associations (CTJ.N));
2129 -- We need to deal with the actual parameter chain as well. The
2130 -- newly added parameter is always the last actual.
2132 Act := First_Named_Actual (CTJ.N);
2134 if No (Act) then
2135 Set_First_Named_Actual (CTJ.N, Extra);
2137 -- If call has been relocated (as with an expression in
2138 -- an aggregate), set First_Named pointer in original node
2139 -- as well, because that's the parent of the parameter list.
2141 Set_First_Named_Actual
2142 (Parent (List_Containing (ExtraP)), Extra);
2144 -- Here we must follow the chain and append the new entry
2146 else
2147 loop
2148 declare
2149 PAN : Node_Id;
2150 NNA : Node_Id;
2152 begin
2153 PAN := Parent (Act);
2154 pragma Assert (Nkind (PAN) = N_Parameter_Association);
2155 NNA := Next_Named_Actual (PAN);
2157 if No (NNA) then
2158 Set_Next_Named_Actual (PAN, Extra);
2159 exit;
2160 end if;
2162 Act := NNA;
2163 end;
2164 end loop;
2165 end if;
2167 -- Analyze and resolve the new actual. We do not need to
2168 -- establish the relevant scope stack entries here, because
2169 -- we have already set all the correct entity references, so
2170 -- no name resolution is needed.
2172 -- We analyze with all checks suppressed (since we do not
2173 -- expect any exceptions, and also we temporarily turn off
2174 -- Unested_Subprogram_Mode to avoid trying to mark uplevel
2175 -- references (not needed at this stage, and in fact causes
2176 -- a bit of recursive chaos).
2178 Opt.Unnest_Subprogram_Mode := False;
2179 Analyze_And_Resolve
2180 (Extra, Etype (STT.ARECnF), Suppress => All_Checks);
2181 Opt.Unnest_Subprogram_Mode := True;
2182 end if;
2183 end Adjust_One_Call;
2184 end loop Adjust_Calls;
2186 return;
2187 end Unnest_Subprogram;
2189 ------------------------
2190 -- Unnest_Subprograms --
2191 ------------------------
2193 procedure Unnest_Subprograms (N : Node_Id) is
2194 function Search_Subprograms (N : Node_Id) return Traverse_Result;
2195 -- Tree visitor that search for outer level procedures with nested
2196 -- subprograms and invokes Unnest_Subprogram()
2198 ---------------
2199 -- Do_Search --
2200 ---------------
2202 procedure Do_Search is new Traverse_Proc (Search_Subprograms);
2203 -- Subtree visitor instantiation
2205 ------------------------
2206 -- Search_Subprograms --
2207 ------------------------
2209 function Search_Subprograms (N : Node_Id) return Traverse_Result is
2210 begin
2211 if Nkind_In (N, N_Subprogram_Body, N_Subprogram_Body_Stub) then
2212 declare
2213 Spec_Id : constant Entity_Id := Unique_Defining_Entity (N);
2215 begin
2216 -- We are only interested in subprograms (not generic
2217 -- subprograms), that have nested subprograms.
2219 if Is_Subprogram (Spec_Id)
2220 and then Has_Nested_Subprogram (Spec_Id)
2221 and then Is_Library_Level_Entity (Spec_Id)
2222 then
2223 Unnest_Subprogram (Spec_Id, N);
2224 end if;
2225 end;
2226 end if;
2228 -- The proper body of a stub may contain nested subprograms, and
2229 -- therefore must be visited explicitly. Nested stubs are examined
2230 -- recursively in Visit_Node.
2232 if Nkind (N) in N_Body_Stub then
2233 Do_Search (Library_Unit (N));
2234 end if;
2236 return OK;
2237 end Search_Subprograms;
2239 -- Start of processing for Unnest_Subprograms
2241 begin
2242 if not Opt.Unnest_Subprogram_Mode then
2243 return;
2244 end if;
2246 -- A specification will contain bodies if it contains instantiations so
2247 -- examine package or subprogram declaration of the main unit, when it
2248 -- is present.
2250 if Nkind (Unit (N)) = N_Package_Body
2251 or else (Nkind (Unit (N)) = N_Subprogram_Body
2252 and then not Acts_As_Spec (N))
2253 then
2254 Do_Search (Library_Unit (N));
2255 end if;
2257 Do_Search (N);
2258 end Unnest_Subprograms;
2260 end Exp_Unst;