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