Merge from mainline
[official-gcc.git] / gcc / ada / exp_ch6.adb
blobc42b1f3c6cf0b85e3791f9de0ccec3aa24ea641e
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2006, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Elists; use Elists;
33 with Exp_Ch2; use Exp_Ch2;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Intr; use Exp_Intr;
41 with Exp_Pakd; use Exp_Pakd;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Fname; use Fname;
45 with Freeze; use Freeze;
46 with Hostparm; use Hostparm;
47 with Inline; use Inline;
48 with Lib; use Lib;
49 with Nlists; use Nlists;
50 with Nmake; use Nmake;
51 with Opt; use Opt;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
55 with Sem; use Sem;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch12; use Sem_Ch12;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Mech; use Sem_Mech;
63 with Sem_Res; use Sem_Res;
64 with Sem_Util; use Sem_Util;
65 with Sinfo; use Sinfo;
66 with Snames; use Snames;
67 with Stand; use Stand;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Uintp; use Uintp;
71 with Validsw; use Validsw;
73 package body Exp_Ch6 is
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 procedure Check_Overriding_Operation (Subp : Entity_Id);
80 -- Subp is a dispatching operation. Check whether it may override an
81 -- inherited private operation, in which case its DT entry is that of
82 -- the hidden operation, not the one it may have received earlier.
83 -- This must be done before emitting the code to set the corresponding
84 -- DT to the address of the subprogram. The actual placement of Subp in
85 -- the proper place in the list of primitive operations is done in
86 -- Declare_Inherited_Private_Subprograms, which also has to deal with
87 -- implicit operations. This duplication is unavoidable for now???
89 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
90 -- This procedure is called only if the subprogram body N, whose spec
91 -- has the given entity Spec, contains a parameterless recursive call.
92 -- It attempts to generate runtime code to detect if this a case of
93 -- infinite recursion.
95 -- The body is scanned to determine dependencies. If the only external
96 -- dependencies are on a small set of scalar variables, then the values
97 -- of these variables are captured on entry to the subprogram, and if
98 -- the values are not changed for the call, we know immediately that
99 -- we have an infinite recursion.
101 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id);
102 -- For each actual of an in-out or out parameter which is a numeric
103 -- (view) conversion of the form T (A), where A denotes a variable,
104 -- we insert the declaration:
106 -- Temp : T[ := T (A)];
108 -- prior to the call. Then we replace the actual with a reference to Temp,
109 -- and append the assignment:
111 -- A := TypeA (Temp);
113 -- after the call. Here TypeA is the actual type of variable A.
114 -- For out parameters, the initial declaration has no expression.
115 -- If A is not an entity name, we generate instead:
117 -- Var : TypeA renames A;
118 -- Temp : T := Var; -- omitting expression for out parameter.
119 -- ...
120 -- Var := TypeA (Temp);
122 -- For other in-out parameters, we emit the required constraint checks
123 -- before and/or after the call.
125 -- For all parameter modes, actuals that denote components and slices
126 -- of packed arrays are expanded into suitable temporaries.
128 -- For non-scalar objects that are possibly unaligned, add call by copy
129 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
131 procedure Expand_Inlined_Call
132 (N : Node_Id;
133 Subp : Entity_Id;
134 Orig_Subp : Entity_Id);
135 -- If called subprogram can be inlined by the front-end, retrieve the
136 -- analyzed body, replace formals with actuals and expand call in place.
137 -- Generate thunks for actuals that are expressions, and insert the
138 -- corresponding constant declarations before the call. If the original
139 -- call is to a derived operation, the return type is the one of the
140 -- derived operation, but the body is that of the original, so return
141 -- expressions in the body must be converted to the desired type (which
142 -- is simply not noted in the tree without inline expansion).
144 function Expand_Protected_Object_Reference
145 (N : Node_Id;
146 Scop : Entity_Id)
147 return Node_Id;
149 procedure Expand_Protected_Subprogram_Call
150 (N : Node_Id;
151 Subp : Entity_Id;
152 Scop : Entity_Id);
153 -- A call to a protected subprogram within the protected object may appear
154 -- as a regular call. The list of actuals must be expanded to contain a
155 -- reference to the object itself, and the call becomes a call to the
156 -- corresponding protected subprogram.
158 --------------------------------
159 -- Check_Overriding_Operation --
160 --------------------------------
162 procedure Check_Overriding_Operation (Subp : Entity_Id) is
163 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
164 Op_List : constant Elist_Id := Primitive_Operations (Typ);
165 Op_Elmt : Elmt_Id;
166 Prim_Op : Entity_Id;
167 Par_Op : Entity_Id;
169 begin
170 if Is_Derived_Type (Typ)
171 and then not Is_Private_Type (Typ)
172 and then In_Open_Scopes (Scope (Etype (Typ)))
173 and then Typ = Base_Type (Typ)
174 then
175 -- Subp overrides an inherited private operation if there is an
176 -- inherited operation with a different name than Subp (see
177 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
178 -- same name as Subp.
180 Op_Elmt := First_Elmt (Op_List);
181 while Present (Op_Elmt) loop
182 Prim_Op := Node (Op_Elmt);
183 Par_Op := Alias (Prim_Op);
185 if Present (Par_Op)
186 and then not Comes_From_Source (Prim_Op)
187 and then Chars (Prim_Op) /= Chars (Par_Op)
188 and then Chars (Par_Op) = Chars (Subp)
189 and then Is_Hidden (Par_Op)
190 and then Type_Conformant (Prim_Op, Subp)
191 then
192 Set_DT_Position (Subp, DT_Position (Prim_Op));
193 end if;
195 Next_Elmt (Op_Elmt);
196 end loop;
197 end if;
198 end Check_Overriding_Operation;
200 -------------------------------
201 -- Detect_Infinite_Recursion --
202 -------------------------------
204 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
205 Loc : constant Source_Ptr := Sloc (N);
207 Var_List : constant Elist_Id := New_Elmt_List;
208 -- List of globals referenced by body of procedure
210 Call_List : constant Elist_Id := New_Elmt_List;
211 -- List of recursive calls in body of procedure
213 Shad_List : constant Elist_Id := New_Elmt_List;
214 -- List of entity id's for entities created to capture the value of
215 -- referenced globals on entry to the procedure.
217 Scop : constant Uint := Scope_Depth (Spec);
218 -- This is used to record the scope depth of the current procedure, so
219 -- that we can identify global references.
221 Max_Vars : constant := 4;
222 -- Do not test more than four global variables
224 Count_Vars : Natural := 0;
225 -- Count variables found so far
227 Var : Entity_Id;
228 Elm : Elmt_Id;
229 Ent : Entity_Id;
230 Call : Elmt_Id;
231 Decl : Node_Id;
232 Test : Node_Id;
233 Elm1 : Elmt_Id;
234 Elm2 : Elmt_Id;
235 Last : Node_Id;
237 function Process (Nod : Node_Id) return Traverse_Result;
238 -- Function to traverse the subprogram body (using Traverse_Func)
240 -------------
241 -- Process --
242 -------------
244 function Process (Nod : Node_Id) return Traverse_Result is
245 begin
246 -- Procedure call
248 if Nkind (Nod) = N_Procedure_Call_Statement then
250 -- Case of one of the detected recursive calls
252 if Is_Entity_Name (Name (Nod))
253 and then Has_Recursive_Call (Entity (Name (Nod)))
254 and then Entity (Name (Nod)) = Spec
255 then
256 Append_Elmt (Nod, Call_List);
257 return Skip;
259 -- Any other procedure call may have side effects
261 else
262 return Abandon;
263 end if;
265 -- A call to a pure function can always be ignored
267 elsif Nkind (Nod) = N_Function_Call
268 and then Is_Entity_Name (Name (Nod))
269 and then Is_Pure (Entity (Name (Nod)))
270 then
271 return Skip;
273 -- Case of an identifier reference
275 elsif Nkind (Nod) = N_Identifier then
276 Ent := Entity (Nod);
278 -- If no entity, then ignore the reference
280 -- Not clear why this can happen. To investigate, remove this
281 -- test and look at the crash that occurs here in 3401-004 ???
283 if No (Ent) then
284 return Skip;
286 -- Ignore entities with no Scope, again not clear how this
287 -- can happen, to investigate, look at 4108-008 ???
289 elsif No (Scope (Ent)) then
290 return Skip;
292 -- Ignore the reference if not to a more global object
294 elsif Scope_Depth (Scope (Ent)) >= Scop then
295 return Skip;
297 -- References to types, exceptions and constants are always OK
299 elsif Is_Type (Ent)
300 or else Ekind (Ent) = E_Exception
301 or else Ekind (Ent) = E_Constant
302 then
303 return Skip;
305 -- If other than a non-volatile scalar variable, we have some
306 -- kind of global reference (e.g. to a function) that we cannot
307 -- deal with so we forget the attempt.
309 elsif Ekind (Ent) /= E_Variable
310 or else not Is_Scalar_Type (Etype (Ent))
311 or else Treat_As_Volatile (Ent)
312 then
313 return Abandon;
315 -- Otherwise we have a reference to a global scalar
317 else
318 -- Loop through global entities already detected
320 Elm := First_Elmt (Var_List);
321 loop
322 -- If not detected before, record this new global reference
324 if No (Elm) then
325 Count_Vars := Count_Vars + 1;
327 if Count_Vars <= Max_Vars then
328 Append_Elmt (Entity (Nod), Var_List);
329 else
330 return Abandon;
331 end if;
333 exit;
335 -- If recorded before, ignore
337 elsif Node (Elm) = Entity (Nod) then
338 return Skip;
340 -- Otherwise keep looking
342 else
343 Next_Elmt (Elm);
344 end if;
345 end loop;
347 return Skip;
348 end if;
350 -- For all other node kinds, recursively visit syntactic children
352 else
353 return OK;
354 end if;
355 end Process;
357 function Traverse_Body is new Traverse_Func;
359 -- Start of processing for Detect_Infinite_Recursion
361 begin
362 -- Do not attempt detection in No_Implicit_Conditional mode, since we
363 -- won't be able to generate the code to handle the recursion in any
364 -- case.
366 if Restriction_Active (No_Implicit_Conditionals) then
367 return;
368 end if;
370 -- Otherwise do traversal and quit if we get abandon signal
372 if Traverse_Body (N) = Abandon then
373 return;
375 -- We must have a call, since Has_Recursive_Call was set. If not just
376 -- ignore (this is only an error check, so if we have a funny situation,
377 -- due to bugs or errors, we do not want to bomb!)
379 elsif Is_Empty_Elmt_List (Call_List) then
380 return;
381 end if;
383 -- Here is the case where we detect recursion at compile time
385 -- Push our current scope for analyzing the declarations and code that
386 -- we will insert for the checking.
388 New_Scope (Spec);
390 -- This loop builds temporary variables for each of the referenced
391 -- globals, so that at the end of the loop the list Shad_List contains
392 -- these temporaries in one-to-one correspondence with the elements in
393 -- Var_List.
395 Last := Empty;
396 Elm := First_Elmt (Var_List);
397 while Present (Elm) loop
398 Var := Node (Elm);
399 Ent :=
400 Make_Defining_Identifier (Loc,
401 Chars => New_Internal_Name ('S'));
402 Append_Elmt (Ent, Shad_List);
404 -- Insert a declaration for this temporary at the start of the
405 -- declarations for the procedure. The temporaries are declared as
406 -- constant objects initialized to the current values of the
407 -- corresponding temporaries.
409 Decl :=
410 Make_Object_Declaration (Loc,
411 Defining_Identifier => Ent,
412 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
413 Constant_Present => True,
414 Expression => New_Occurrence_Of (Var, Loc));
416 if No (Last) then
417 Prepend (Decl, Declarations (N));
418 else
419 Insert_After (Last, Decl);
420 end if;
422 Last := Decl;
423 Analyze (Decl);
424 Next_Elmt (Elm);
425 end loop;
427 -- Loop through calls
429 Call := First_Elmt (Call_List);
430 while Present (Call) loop
432 -- Build a predicate expression of the form
434 -- True
435 -- and then global1 = temp1
436 -- and then global2 = temp2
437 -- ...
439 -- This predicate determines if any of the global values
440 -- referenced by the procedure have changed since the
441 -- current call, if not an infinite recursion is assured.
443 Test := New_Occurrence_Of (Standard_True, Loc);
445 Elm1 := First_Elmt (Var_List);
446 Elm2 := First_Elmt (Shad_List);
447 while Present (Elm1) loop
448 Test :=
449 Make_And_Then (Loc,
450 Left_Opnd => Test,
451 Right_Opnd =>
452 Make_Op_Eq (Loc,
453 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
454 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
456 Next_Elmt (Elm1);
457 Next_Elmt (Elm2);
458 end loop;
460 -- Now we replace the call with the sequence
462 -- if no-changes (see above) then
463 -- raise Storage_Error;
464 -- else
465 -- original-call
466 -- end if;
468 Rewrite (Node (Call),
469 Make_If_Statement (Loc,
470 Condition => Test,
471 Then_Statements => New_List (
472 Make_Raise_Storage_Error (Loc,
473 Reason => SE_Infinite_Recursion)),
475 Else_Statements => New_List (
476 Relocate_Node (Node (Call)))));
478 Analyze (Node (Call));
480 Next_Elmt (Call);
481 end loop;
483 -- Remove temporary scope stack entry used for analysis
485 Pop_Scope;
486 end Detect_Infinite_Recursion;
488 --------------------
489 -- Expand_Actuals --
490 --------------------
492 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
493 Loc : constant Source_Ptr := Sloc (N);
494 Actual : Node_Id;
495 Formal : Entity_Id;
496 N_Node : Node_Id;
497 Post_Call : List_Id;
498 E_Formal : Entity_Id;
500 procedure Add_Call_By_Copy_Code;
501 -- For cases where the parameter must be passed by copy, this routine
502 -- generates a temporary variable into which the actual is copied and
503 -- then passes this as the parameter. For an OUT or IN OUT parameter,
504 -- an assignment is also generated to copy the result back. The call
505 -- also takes care of any constraint checks required for the type
506 -- conversion case (on both the way in and the way out).
508 procedure Add_Simple_Call_By_Copy_Code;
509 -- This is similar to the above, but is used in cases where we know
510 -- that all that is needed is to simply create a temporary and copy
511 -- the value in and out of the temporary.
513 procedure Check_Fortran_Logical;
514 -- A value of type Logical that is passed through a formal parameter
515 -- must be normalized because .TRUE. usually does not have the same
516 -- representation as True. We assume that .FALSE. = False = 0.
517 -- What about functions that return a logical type ???
519 function Is_Legal_Copy return Boolean;
520 -- Check that an actual can be copied before generating the temporary
521 -- to be used in the call. If the actual is of a by_reference type then
522 -- the program is illegal (this can only happen in the presence of
523 -- rep. clauses that force an incorrect alignment). If the formal is
524 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
525 -- the effect that this might lead to unaligned arguments.
527 function Make_Var (Actual : Node_Id) return Entity_Id;
528 -- Returns an entity that refers to the given actual parameter,
529 -- Actual (not including any type conversion). If Actual is an
530 -- entity name, then this entity is returned unchanged, otherwise
531 -- a renaming is created to provide an entity for the actual.
533 procedure Reset_Packed_Prefix;
534 -- The expansion of a packed array component reference is delayed in
535 -- the context of a call. Now we need to complete the expansion, so we
536 -- unmark the analyzed bits in all prefixes.
538 ---------------------------
539 -- Add_Call_By_Copy_Code --
540 ---------------------------
542 procedure Add_Call_By_Copy_Code is
543 Expr : Node_Id;
544 Init : Node_Id;
545 Temp : Entity_Id;
546 Indic : Node_Id;
547 Var : Entity_Id;
548 F_Typ : constant Entity_Id := Etype (Formal);
549 V_Typ : Entity_Id;
550 Crep : Boolean;
552 begin
553 if not Is_Legal_Copy then
554 return;
555 end if;
557 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
559 -- Use formal type for temp, unless formal type is an unconstrained
560 -- array, in which case we don't have to worry about bounds checks,
561 -- and we use the actual type, since that has appropriate bounds.
563 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
564 Indic := New_Occurrence_Of (Etype (Actual), Loc);
565 else
566 Indic := New_Occurrence_Of (Etype (Formal), Loc);
567 end if;
569 if Nkind (Actual) = N_Type_Conversion then
570 V_Typ := Etype (Expression (Actual));
572 -- If the formal is an (in-)out parameter, capture the name
573 -- of the variable in order to build the post-call assignment.
575 Var := Make_Var (Expression (Actual));
577 Crep := not Same_Representation
578 (F_Typ, Etype (Expression (Actual)));
580 else
581 V_Typ := Etype (Actual);
582 Var := Make_Var (Actual);
583 Crep := False;
584 end if;
586 -- Setup initialization for case of in out parameter, or an out
587 -- parameter where the formal is an unconstrained array (in the
588 -- latter case, we have to pass in an object with bounds).
590 -- If this is an out parameter, the initial copy is wasteful, so as
591 -- an optimization for the one-dimensional case we extract the
592 -- bounds of the actual and build an uninitialized temporary of the
593 -- right size.
595 if Ekind (Formal) = E_In_Out_Parameter
596 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
597 then
598 if Nkind (Actual) = N_Type_Conversion then
599 if Conversion_OK (Actual) then
600 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
601 else
602 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
603 end if;
605 elsif Ekind (Formal) = E_Out_Parameter
606 and then Is_Array_Type (F_Typ)
607 and then Number_Dimensions (F_Typ) = 1
608 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
609 then
610 -- Actual is a one-dimensional array or slice, and the type
611 -- requires no initialization. Create a temporary of the
612 -- right size, but do not copy actual into it (optimization).
614 Init := Empty;
615 Indic :=
616 Make_Subtype_Indication (Loc,
617 Subtype_Mark =>
618 New_Occurrence_Of (F_Typ, Loc),
619 Constraint =>
620 Make_Index_Or_Discriminant_Constraint (Loc,
621 Constraints => New_List (
622 Make_Range (Loc,
623 Low_Bound =>
624 Make_Attribute_Reference (Loc,
625 Prefix => New_Occurrence_Of (Var, Loc),
626 Attribute_name => Name_First),
627 High_Bound =>
628 Make_Attribute_Reference (Loc,
629 Prefix => New_Occurrence_Of (Var, Loc),
630 Attribute_Name => Name_Last)))));
632 else
633 Init := New_Occurrence_Of (Var, Loc);
634 end if;
636 -- An initialization is created for packed conversions as
637 -- actuals for out parameters to enable Make_Object_Declaration
638 -- to determine the proper subtype for N_Node. Note that this
639 -- is wasteful because the extra copying on the call side is
640 -- not required for such out parameters. ???
642 elsif Ekind (Formal) = E_Out_Parameter
643 and then Nkind (Actual) = N_Type_Conversion
644 and then (Is_Bit_Packed_Array (F_Typ)
645 or else
646 Is_Bit_Packed_Array (Etype (Expression (Actual))))
647 then
648 if Conversion_OK (Actual) then
649 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
650 else
651 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
652 end if;
654 elsif Ekind (Formal) = E_In_Parameter then
655 Init := New_Occurrence_Of (Var, Loc);
657 else
658 Init := Empty;
659 end if;
661 N_Node :=
662 Make_Object_Declaration (Loc,
663 Defining_Identifier => Temp,
664 Object_Definition => Indic,
665 Expression => Init);
666 Set_Assignment_OK (N_Node);
667 Insert_Action (N, N_Node);
669 -- Now, normally the deal here is that we use the defining
670 -- identifier created by that object declaration. There is
671 -- one exception to this. In the change of representation case
672 -- the above declaration will end up looking like:
674 -- temp : type := identifier;
676 -- And in this case we might as well use the identifier directly
677 -- and eliminate the temporary. Note that the analysis of the
678 -- declaration was not a waste of time in that case, since it is
679 -- what generated the necessary change of representation code. If
680 -- the change of representation introduced additional code, as in
681 -- a fixed-integer conversion, the expression is not an identifier
682 -- and must be kept.
684 if Crep
685 and then Present (Expression (N_Node))
686 and then Is_Entity_Name (Expression (N_Node))
687 then
688 Temp := Entity (Expression (N_Node));
689 Rewrite (N_Node, Make_Null_Statement (Loc));
690 end if;
692 -- For IN parameter, all we do is to replace the actual
694 if Ekind (Formal) = E_In_Parameter then
695 Rewrite (Actual, New_Reference_To (Temp, Loc));
696 Analyze (Actual);
698 -- Processing for OUT or IN OUT parameter
700 else
701 -- Kill current value indications for the temporary variable we
702 -- created, since we just passed it as an OUT parameter.
704 Kill_Current_Values (Temp);
706 -- If type conversion, use reverse conversion on exit
708 if Nkind (Actual) = N_Type_Conversion then
709 if Conversion_OK (Actual) then
710 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
711 else
712 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
713 end if;
714 else
715 Expr := New_Occurrence_Of (Temp, Loc);
716 end if;
718 Rewrite (Actual, New_Reference_To (Temp, Loc));
719 Analyze (Actual);
721 Append_To (Post_Call,
722 Make_Assignment_Statement (Loc,
723 Name => New_Occurrence_Of (Var, Loc),
724 Expression => Expr));
726 Set_Assignment_OK (Name (Last (Post_Call)));
727 end if;
728 end Add_Call_By_Copy_Code;
730 ----------------------------------
731 -- Add_Simple_Call_By_Copy_Code --
732 ----------------------------------
734 procedure Add_Simple_Call_By_Copy_Code is
735 Temp : Entity_Id;
736 Decl : Node_Id;
737 Incod : Node_Id;
738 Outcod : Node_Id;
739 Lhs : Node_Id;
740 Rhs : Node_Id;
741 Indic : Node_Id;
742 F_Typ : constant Entity_Id := Etype (Formal);
744 begin
745 if not Is_Legal_Copy then
746 return;
747 end if;
749 -- Use formal type for temp, unless formal type is an unconstrained
750 -- array, in which case we don't have to worry about bounds checks,
751 -- and we use the actual type, since that has appropriate bounds.
753 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
754 Indic := New_Occurrence_Of (Etype (Actual), Loc);
755 else
756 Indic := New_Occurrence_Of (Etype (Formal), Loc);
757 end if;
759 -- Prepare to generate code
761 Reset_Packed_Prefix;
763 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
764 Incod := Relocate_Node (Actual);
765 Outcod := New_Copy_Tree (Incod);
767 -- Generate declaration of temporary variable, initializing it
768 -- with the input parameter unless we have an OUT formal or
769 -- this is an initialization call.
771 -- If the formal is an out parameter with discriminants, the
772 -- discriminants must be captured even if the rest of the object
773 -- is in principle uninitialized, because the discriminants may
774 -- be read by the called subprogram.
776 if Ekind (Formal) = E_Out_Parameter then
777 Incod := Empty;
779 if Has_Discriminants (Etype (Formal)) then
780 Indic := New_Occurrence_Of (Etype (Actual), Loc);
781 end if;
783 elsif Inside_Init_Proc then
785 -- Could use a comment here to match comment below ???
787 if Nkind (Actual) /= N_Selected_Component
788 or else
789 not Has_Discriminant_Dependent_Constraint
790 (Entity (Selector_Name (Actual)))
791 then
792 Incod := Empty;
794 -- Otherwise, keep the component in order to generate the proper
795 -- actual subtype, that depends on enclosing discriminants.
797 else
798 null;
799 end if;
800 end if;
802 Decl :=
803 Make_Object_Declaration (Loc,
804 Defining_Identifier => Temp,
805 Object_Definition => Indic,
806 Expression => Incod);
808 if Inside_Init_Proc
809 and then No (Incod)
810 then
811 -- If the call is to initialize a component of a composite type,
812 -- and the component does not depend on discriminants, use the
813 -- actual type of the component. This is required in case the
814 -- component is constrained, because in general the formal of the
815 -- initialization procedure will be unconstrained. Note that if
816 -- the component being initialized is constrained by an enclosing
817 -- discriminant, the presence of the initialization in the
818 -- declaration will generate an expression for the actual subtype.
820 Set_No_Initialization (Decl);
821 Set_Object_Definition (Decl,
822 New_Occurrence_Of (Etype (Actual), Loc));
823 end if;
825 Insert_Action (N, Decl);
827 -- The actual is simply a reference to the temporary
829 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
831 -- Generate copy out if OUT or IN OUT parameter
833 if Ekind (Formal) /= E_In_Parameter then
834 Lhs := Outcod;
835 Rhs := New_Occurrence_Of (Temp, Loc);
837 -- Deal with conversion
839 if Nkind (Lhs) = N_Type_Conversion then
840 Lhs := Expression (Lhs);
841 Rhs := Convert_To (Etype (Actual), Rhs);
842 end if;
844 Append_To (Post_Call,
845 Make_Assignment_Statement (Loc,
846 Name => Lhs,
847 Expression => Rhs));
848 Set_Assignment_OK (Name (Last (Post_Call)));
849 end if;
850 end Add_Simple_Call_By_Copy_Code;
852 ---------------------------
853 -- Check_Fortran_Logical --
854 ---------------------------
856 procedure Check_Fortran_Logical is
857 Logical : constant Entity_Id := Etype (Formal);
858 Var : Entity_Id;
860 -- Note: this is very incomplete, e.g. it does not handle arrays
861 -- of logical values. This is really not the right approach at all???)
863 begin
864 if Convention (Subp) = Convention_Fortran
865 and then Root_Type (Etype (Formal)) = Standard_Boolean
866 and then Ekind (Formal) /= E_In_Parameter
867 then
868 Var := Make_Var (Actual);
869 Append_To (Post_Call,
870 Make_Assignment_Statement (Loc,
871 Name => New_Occurrence_Of (Var, Loc),
872 Expression =>
873 Unchecked_Convert_To (
874 Logical,
875 Make_Op_Ne (Loc,
876 Left_Opnd => New_Occurrence_Of (Var, Loc),
877 Right_Opnd =>
878 Unchecked_Convert_To (
879 Logical,
880 New_Occurrence_Of (Standard_False, Loc))))));
881 end if;
882 end Check_Fortran_Logical;
884 -------------------
885 -- Is_Legal_Copy --
886 -------------------
888 function Is_Legal_Copy return Boolean is
889 begin
890 -- An attempt to copy a value of such a type can only occur if
891 -- representation clauses give the actual a misaligned address.
893 if Is_By_Reference_Type (Etype (Formal)) then
894 Error_Msg_N
895 ("misaligned actual cannot be passed by reference", Actual);
896 return False;
898 -- For users of Starlet, we assume that the specification of by-
899 -- reference mechanism is mandatory. This may lead to unligned
900 -- objects but at least for DEC legacy code it is known to work.
901 -- The warning will alert users of this code that a problem may
902 -- be lurking.
904 elsif Mechanism (Formal) = By_Reference
905 and then Is_Valued_Procedure (Scope (Formal))
906 then
907 Error_Msg_N
908 ("by_reference actual may be misaligned?", Actual);
909 return False;
911 else
912 return True;
913 end if;
914 end Is_Legal_Copy;
916 --------------
917 -- Make_Var --
918 --------------
920 function Make_Var (Actual : Node_Id) return Entity_Id is
921 Var : Entity_Id;
923 begin
924 if Is_Entity_Name (Actual) then
925 return Entity (Actual);
927 else
928 Var := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
930 N_Node :=
931 Make_Object_Renaming_Declaration (Loc,
932 Defining_Identifier => Var,
933 Subtype_Mark =>
934 New_Occurrence_Of (Etype (Actual), Loc),
935 Name => Relocate_Node (Actual));
937 Insert_Action (N, N_Node);
938 return Var;
939 end if;
940 end Make_Var;
942 -------------------------
943 -- Reset_Packed_Prefix --
944 -------------------------
946 procedure Reset_Packed_Prefix is
947 Pfx : Node_Id := Actual;
948 begin
949 loop
950 Set_Analyzed (Pfx, False);
951 exit when Nkind (Pfx) /= N_Selected_Component
952 and then Nkind (Pfx) /= N_Indexed_Component;
953 Pfx := Prefix (Pfx);
954 end loop;
955 end Reset_Packed_Prefix;
957 -- Start of processing for Expand_Actuals
959 begin
960 Post_Call := New_List;
962 Formal := First_Formal (Subp);
963 Actual := First_Actual (N);
964 while Present (Formal) loop
965 E_Formal := Etype (Formal);
967 if Is_Scalar_Type (E_Formal)
968 or else Nkind (Actual) = N_Slice
969 then
970 Check_Fortran_Logical;
972 -- RM 6.4.1 (11)
974 elsif Ekind (Formal) /= E_Out_Parameter then
976 -- The unusual case of the current instance of a protected type
977 -- requires special handling. This can only occur in the context
978 -- of a call within the body of a protected operation.
980 if Is_Entity_Name (Actual)
981 and then Ekind (Entity (Actual)) = E_Protected_Type
982 and then In_Open_Scopes (Entity (Actual))
983 then
984 if Scope (Subp) /= Entity (Actual) then
985 Error_Msg_N ("operation outside protected type may not "
986 & "call back its protected operations?", Actual);
987 end if;
989 Rewrite (Actual,
990 Expand_Protected_Object_Reference (N, Entity (Actual)));
991 end if;
993 Apply_Constraint_Check (Actual, E_Formal);
995 -- Out parameter case. No constraint checks on access type
996 -- RM 6.4.1 (13)
998 elsif Is_Access_Type (E_Formal) then
999 null;
1001 -- RM 6.4.1 (14)
1003 elsif Has_Discriminants (Base_Type (E_Formal))
1004 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1005 then
1006 Apply_Constraint_Check (Actual, E_Formal);
1008 -- RM 6.4.1 (15)
1010 else
1011 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1012 end if;
1014 -- Processing for IN-OUT and OUT parameters
1016 if Ekind (Formal) /= E_In_Parameter then
1018 -- For type conversions of arrays, apply length/range checks
1020 if Is_Array_Type (E_Formal)
1021 and then Nkind (Actual) = N_Type_Conversion
1022 then
1023 if Is_Constrained (E_Formal) then
1024 Apply_Length_Check (Expression (Actual), E_Formal);
1025 else
1026 Apply_Range_Check (Expression (Actual), E_Formal);
1027 end if;
1028 end if;
1030 -- If argument is a type conversion for a type that is passed
1031 -- by copy, then we must pass the parameter by copy.
1033 if Nkind (Actual) = N_Type_Conversion
1034 and then
1035 (Is_Numeric_Type (E_Formal)
1036 or else Is_Access_Type (E_Formal)
1037 or else Is_Enumeration_Type (E_Formal)
1038 or else Is_Bit_Packed_Array (Etype (Formal))
1039 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1041 -- Also pass by copy if change of representation
1043 or else not Same_Representation
1044 (Etype (Formal),
1045 Etype (Expression (Actual))))
1046 then
1047 Add_Call_By_Copy_Code;
1049 -- References to components of bit packed arrays are expanded
1050 -- at this point, rather than at the point of analysis of the
1051 -- actuals, to handle the expansion of the assignment to
1052 -- [in] out parameters.
1054 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1055 Add_Simple_Call_By_Copy_Code;
1057 -- If a non-scalar actual is possibly unaligned, we need a copy
1059 elsif Is_Possibly_Unaligned_Object (Actual)
1060 and then not Represented_As_Scalar (Etype (Formal))
1061 then
1062 Add_Simple_Call_By_Copy_Code;
1064 -- References to slices of bit packed arrays are expanded
1066 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1067 Add_Call_By_Copy_Code;
1069 -- References to possibly unaligned slices of arrays are expanded
1071 elsif Is_Possibly_Unaligned_Slice (Actual) then
1072 Add_Call_By_Copy_Code;
1074 -- Deal with access types where the actual subtpe and the
1075 -- formal subtype are not the same, requiring a check.
1077 -- It is necessary to exclude tagged types because of "downward
1078 -- conversion" errors and a strange assertion error in namet
1079 -- from gnatf in bug 1215-001 ???
1081 elsif Is_Access_Type (E_Formal)
1082 and then not Same_Type (E_Formal, Etype (Actual))
1083 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1084 then
1085 Add_Call_By_Copy_Code;
1087 -- If the actual is not a scalar and is marked for volatile
1088 -- treatment, whereas the formal is not volatile, then pass
1089 -- by copy unless it is a by-reference type.
1091 elsif Is_Entity_Name (Actual)
1092 and then Treat_As_Volatile (Entity (Actual))
1093 and then not Is_By_Reference_Type (Etype (Actual))
1094 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1095 and then not Treat_As_Volatile (E_Formal)
1096 then
1097 Add_Call_By_Copy_Code;
1099 elsif Nkind (Actual) = N_Indexed_Component
1100 and then Is_Entity_Name (Prefix (Actual))
1101 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1102 then
1103 Add_Call_By_Copy_Code;
1104 end if;
1106 -- Processing for IN parameters
1108 else
1109 -- For IN parameters is in the packed array case, we expand an
1110 -- indexed component (the circuit in Exp_Ch4 deliberately left
1111 -- indexed components appearing as actuals untouched, so that
1112 -- the special processing above for the OUT and IN OUT cases
1113 -- could be performed. We could make the test in Exp_Ch4 more
1114 -- complex and have it detect the parameter mode, but it is
1115 -- easier simply to handle all cases here.)
1117 if Nkind (Actual) = N_Indexed_Component
1118 and then Is_Packed (Etype (Prefix (Actual)))
1119 then
1120 Reset_Packed_Prefix;
1121 Expand_Packed_Element_Reference (Actual);
1123 -- If we have a reference to a bit packed array, we copy it,
1124 -- since the actual must be byte aligned.
1126 -- Is this really necessary in all cases???
1128 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1129 Add_Simple_Call_By_Copy_Code;
1131 -- If a non-scalar actual is possibly unaligned, we need a copy
1133 elsif Is_Possibly_Unaligned_Object (Actual)
1134 and then not Represented_As_Scalar (Etype (Formal))
1135 then
1136 Add_Simple_Call_By_Copy_Code;
1138 -- Similarly, we have to expand slices of packed arrays here
1139 -- because the result must be byte aligned.
1141 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1142 Add_Call_By_Copy_Code;
1144 -- Only processing remaining is to pass by copy if this is a
1145 -- reference to a possibly unaligned slice, since the caller
1146 -- expects an appropriately aligned argument.
1148 elsif Is_Possibly_Unaligned_Slice (Actual) then
1149 Add_Call_By_Copy_Code;
1150 end if;
1151 end if;
1153 Next_Formal (Formal);
1154 Next_Actual (Actual);
1155 end loop;
1157 -- Find right place to put post call stuff if it is present
1159 if not Is_Empty_List (Post_Call) then
1161 -- If call is not a list member, it must be the triggering statement
1162 -- of a triggering alternative or an entry call alternative, and we
1163 -- can add the post call stuff to the corresponding statement list.
1165 if not Is_List_Member (N) then
1166 declare
1167 P : constant Node_Id := Parent (N);
1169 begin
1170 pragma Assert (Nkind (P) = N_Triggering_Alternative
1171 or else Nkind (P) = N_Entry_Call_Alternative);
1173 if Is_Non_Empty_List (Statements (P)) then
1174 Insert_List_Before_And_Analyze
1175 (First (Statements (P)), Post_Call);
1176 else
1177 Set_Statements (P, Post_Call);
1178 end if;
1179 end;
1181 -- Otherwise, normal case where N is in a statement sequence,
1182 -- just put the post-call stuff after the call statement.
1184 else
1185 Insert_Actions_After (N, Post_Call);
1186 end if;
1187 end if;
1189 -- The call node itself is re-analyzed in Expand_Call
1191 end Expand_Actuals;
1193 -----------------
1194 -- Expand_Call --
1195 -----------------
1197 -- This procedure handles expansion of function calls and procedure call
1198 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1199 -- Expand_N_Procedure_Call_Statement. Processing for calls includes:
1201 -- Replace call to Raise_Exception by Raise_Exception always if possible
1202 -- Provide values of actuals for all formals in Extra_Formals list
1203 -- Replace "call" to enumeration literal function by literal itself
1204 -- Rewrite call to predefined operator as operator
1205 -- Replace actuals to in-out parameters that are numeric conversions,
1206 -- with explicit assignment to temporaries before and after the call.
1207 -- Remove optional actuals if First_Optional_Parameter specified.
1209 -- Note that the list of actuals has been filled with default expressions
1210 -- during semantic analysis of the call. Only the extra actuals required
1211 -- for the 'Constrained attribute and for accessibility checks are added
1212 -- at this point.
1214 procedure Expand_Call (N : Node_Id) is
1215 Loc : constant Source_Ptr := Sloc (N);
1216 Remote : constant Boolean := Is_Remote_Call (N);
1217 Subp : Entity_Id;
1218 Orig_Subp : Entity_Id := Empty;
1219 Parent_Subp : Entity_Id;
1220 Parent_Formal : Entity_Id;
1221 Actual : Node_Id;
1222 Formal : Entity_Id;
1223 Prev : Node_Id := Empty;
1225 Prev_Orig : Node_Id;
1226 -- Original node for an actual, which may have been rewritten. If the
1227 -- actual is a function call that has been transformed from a selected
1228 -- component, the original node is unanalyzed. Otherwise, it carries
1229 -- semantic information used to generate additional actuals.
1231 Scop : Entity_Id;
1232 Extra_Actuals : List_Id := No_List;
1234 CW_Interface_Formals_Present : Boolean := False;
1236 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1237 -- Adds one entry to the end of the actual parameter list. Used for
1238 -- default parameters and for extra actuals (for Extra_Formals). The
1239 -- argument is an N_Parameter_Association node.
1241 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1242 -- Adds an extra actual to the list of extra actuals. Expr is the
1243 -- expression for the value of the actual, EF is the entity for the
1244 -- extra formal.
1246 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1247 -- Within an instance, a type derived from a non-tagged formal derived
1248 -- type inherits from the original parent, not from the actual. This is
1249 -- tested in 4723-003. The current derivation mechanism has the derived
1250 -- type inherit from the actual, which is only correct outside of the
1251 -- instance. If the subprogram is inherited, we test for this particular
1252 -- case through a convoluted tree traversal before setting the proper
1253 -- subprogram to be called.
1255 --------------------------
1256 -- Add_Actual_Parameter --
1257 --------------------------
1259 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1260 Actual_Expr : constant Node_Id :=
1261 Explicit_Actual_Parameter (Insert_Param);
1263 begin
1264 -- Case of insertion is first named actual
1266 if No (Prev) or else
1267 Nkind (Parent (Prev)) /= N_Parameter_Association
1268 then
1269 Set_Next_Named_Actual (Insert_Param, First_Named_Actual (N));
1270 Set_First_Named_Actual (N, Actual_Expr);
1272 if No (Prev) then
1273 if No (Parameter_Associations (N)) then
1274 Set_Parameter_Associations (N, New_List);
1275 Append (Insert_Param, Parameter_Associations (N));
1276 end if;
1277 else
1278 Insert_After (Prev, Insert_Param);
1279 end if;
1281 -- Case of insertion is not first named actual
1283 else
1284 Set_Next_Named_Actual
1285 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1286 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1287 Append (Insert_Param, Parameter_Associations (N));
1288 end if;
1290 Prev := Actual_Expr;
1291 end Add_Actual_Parameter;
1293 ----------------------
1294 -- Add_Extra_Actual --
1295 ----------------------
1297 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1298 Loc : constant Source_Ptr := Sloc (Expr);
1300 begin
1301 if Extra_Actuals = No_List then
1302 Extra_Actuals := New_List;
1303 Set_Parent (Extra_Actuals, N);
1304 end if;
1306 Append_To (Extra_Actuals,
1307 Make_Parameter_Association (Loc,
1308 Explicit_Actual_Parameter => Expr,
1309 Selector_Name =>
1310 Make_Identifier (Loc, Chars (EF))));
1312 Analyze_And_Resolve (Expr, Etype (EF));
1313 end Add_Extra_Actual;
1315 ---------------------------
1316 -- Inherited_From_Formal --
1317 ---------------------------
1319 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1320 Par : Entity_Id;
1321 Gen_Par : Entity_Id;
1322 Gen_Prim : Elist_Id;
1323 Elmt : Elmt_Id;
1324 Indic : Node_Id;
1326 begin
1327 -- If the operation is inherited, it is attached to the corresponding
1328 -- type derivation. If the parent in the derivation is a generic
1329 -- actual, it is a subtype of the actual, and we have to recover the
1330 -- original derived type declaration to find the proper parent.
1332 if Nkind (Parent (S)) /= N_Full_Type_Declaration
1333 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
1334 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
1335 N_Derived_Type_Definition
1336 or else not In_Instance
1337 then
1338 return Empty;
1340 else
1341 Indic :=
1342 (Subtype_Indication
1343 (Type_Definition (Original_Node (Parent (S)))));
1345 if Nkind (Indic) = N_Subtype_Indication then
1346 Par := Entity (Subtype_Mark (Indic));
1347 else
1348 Par := Entity (Indic);
1349 end if;
1350 end if;
1352 if not Is_Generic_Actual_Type (Par)
1353 or else Is_Tagged_Type (Par)
1354 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1355 or else not In_Open_Scopes (Scope (Par))
1356 then
1357 return Empty;
1359 else
1360 Gen_Par := Generic_Parent_Type (Parent (Par));
1361 end if;
1363 -- If the generic parent type is still the generic type, this is a
1364 -- private formal, not a derived formal, and there are no operations
1365 -- inherited from the formal.
1367 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1368 return Empty;
1369 end if;
1371 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
1373 Elmt := First_Elmt (Gen_Prim);
1374 while Present (Elmt) loop
1375 if Chars (Node (Elmt)) = Chars (S) then
1376 declare
1377 F1 : Entity_Id;
1378 F2 : Entity_Id;
1380 begin
1381 F1 := First_Formal (S);
1382 F2 := First_Formal (Node (Elmt));
1383 while Present (F1)
1384 and then Present (F2)
1385 loop
1386 if Etype (F1) = Etype (F2)
1387 or else Etype (F2) = Gen_Par
1388 then
1389 Next_Formal (F1);
1390 Next_Formal (F2);
1391 else
1392 Next_Elmt (Elmt);
1393 exit; -- not the right subprogram
1394 end if;
1396 return Node (Elmt);
1397 end loop;
1398 end;
1400 else
1401 Next_Elmt (Elmt);
1402 end if;
1403 end loop;
1405 raise Program_Error;
1406 end Inherited_From_Formal;
1408 -- Start of processing for Expand_Call
1410 begin
1411 -- Ignore if previous error
1413 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1414 return;
1415 end if;
1417 -- Call using access to subprogram with explicit dereference
1419 if Nkind (Name (N)) = N_Explicit_Dereference then
1420 Subp := Etype (Name (N));
1421 Parent_Subp := Empty;
1423 -- Case of call to simple entry, where the Name is a selected component
1424 -- whose prefix is the task, and whose selector name is the entry name
1426 elsif Nkind (Name (N)) = N_Selected_Component then
1427 Subp := Entity (Selector_Name (Name (N)));
1428 Parent_Subp := Empty;
1430 -- Case of call to member of entry family, where Name is an indexed
1431 -- component, with the prefix being a selected component giving the
1432 -- task and entry family name, and the index being the entry index.
1434 elsif Nkind (Name (N)) = N_Indexed_Component then
1435 Subp := Entity (Selector_Name (Prefix (Name (N))));
1436 Parent_Subp := Empty;
1438 -- Normal case
1440 else
1441 Subp := Entity (Name (N));
1442 Parent_Subp := Alias (Subp);
1444 -- Replace call to Raise_Exception by call to Raise_Exception_Always
1445 -- if we can tell that the first parameter cannot possibly be null.
1446 -- This helps optimization and also generation of warnings.
1448 if not Restriction_Active (No_Exception_Handlers)
1449 and then Is_RTE (Subp, RE_Raise_Exception)
1450 then
1451 declare
1452 FA : constant Node_Id := Original_Node (First_Actual (N));
1454 begin
1455 -- The case we catch is where the first argument is obtained
1456 -- using the Identity attribute (which must always be
1457 -- non-null).
1459 if Nkind (FA) = N_Attribute_Reference
1460 and then Attribute_Name (FA) = Name_Identity
1461 then
1462 Subp := RTE (RE_Raise_Exception_Always);
1463 Set_Entity (Name (N), Subp);
1464 end if;
1465 end;
1466 end if;
1468 if Ekind (Subp) = E_Entry then
1469 Parent_Subp := Empty;
1470 end if;
1471 end if;
1473 -- Ada 2005 (AI-345): We have a procedure call as a triggering
1474 -- alternative in an asynchronous select or as an entry call in
1475 -- a conditional or timed select. Check whether the procedure call
1476 -- is a renaming of an entry and rewrite it as an entry call.
1478 if Ada_Version >= Ada_05
1479 and then Nkind (N) = N_Procedure_Call_Statement
1480 and then
1481 ((Nkind (Parent (N)) = N_Triggering_Alternative
1482 and then Triggering_Statement (Parent (N)) = N)
1483 or else
1484 (Nkind (Parent (N)) = N_Entry_Call_Alternative
1485 and then Entry_Call_Statement (Parent (N)) = N))
1486 then
1487 declare
1488 Ren_Decl : Node_Id;
1489 Ren_Root : Entity_Id := Subp;
1491 begin
1492 -- This may be a chain of renamings, find the root
1494 if Present (Alias (Ren_Root)) then
1495 Ren_Root := Alias (Ren_Root);
1496 end if;
1498 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
1499 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
1501 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
1502 Rewrite (N,
1503 Make_Entry_Call_Statement (Loc,
1504 Name =>
1505 New_Copy_Tree (Name (Ren_Decl)),
1506 Parameter_Associations =>
1507 New_Copy_List_Tree (Parameter_Associations (N))));
1509 return;
1510 end if;
1511 end if;
1512 end;
1513 end if;
1515 -- First step, compute extra actuals, corresponding to any
1516 -- Extra_Formals present. Note that we do not access Extra_Formals
1517 -- directly, instead we simply note the presence of the extra
1518 -- formals as we process the regular formals and collect the
1519 -- corresponding actuals in Extra_Actuals.
1521 -- We also generate any required range checks for actuals as we go
1522 -- through the loop, since this is a convenient place to do this.
1524 Formal := First_Formal (Subp);
1525 Actual := First_Actual (N);
1526 while Present (Formal) loop
1528 -- Generate range check if required (not activated yet ???)
1530 -- if Do_Range_Check (Actual) then
1531 -- Set_Do_Range_Check (Actual, False);
1532 -- Generate_Range_Check
1533 -- (Actual, Etype (Formal), CE_Range_Check_Failed);
1534 -- end if;
1536 -- Prepare to examine current entry
1538 Prev := Actual;
1539 Prev_Orig := Original_Node (Prev);
1541 if not Analyzed (Prev_Orig)
1542 and then Nkind (Actual) = N_Function_Call
1543 then
1544 Prev_Orig := Prev;
1545 end if;
1547 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
1548 -- to expand it in a further round.
1550 CW_Interface_Formals_Present :=
1551 CW_Interface_Formals_Present
1552 or else
1553 (Ekind (Etype (Formal)) = E_Class_Wide_Type
1554 and then Is_Interface (Etype (Etype (Formal))))
1555 or else
1556 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
1557 and then Is_Interface (Directly_Designated_Type
1558 (Etype (Etype (Formal)))));
1560 -- Create possible extra actual for constrained case. Usually, the
1561 -- extra actual is of the form actual'constrained, but since this
1562 -- attribute is only available for unconstrained records, TRUE is
1563 -- expanded if the type of the formal happens to be constrained (for
1564 -- instance when this procedure is inherited from an unconstrained
1565 -- record to a constrained one) or if the actual has no discriminant
1566 -- (its type is constrained). An exception to this is the case of a
1567 -- private type without discriminants. In this case we pass FALSE
1568 -- because the object has underlying discriminants with defaults.
1570 if Present (Extra_Constrained (Formal)) then
1571 if Ekind (Etype (Prev)) in Private_Kind
1572 and then not Has_Discriminants (Base_Type (Etype (Prev)))
1573 then
1574 Add_Extra_Actual (
1575 New_Occurrence_Of (Standard_False, Loc),
1576 Extra_Constrained (Formal));
1578 elsif Is_Constrained (Etype (Formal))
1579 or else not Has_Discriminants (Etype (Prev))
1580 then
1581 Add_Extra_Actual (
1582 New_Occurrence_Of (Standard_True, Loc),
1583 Extra_Constrained (Formal));
1585 -- Do not produce extra actuals for Unchecked_Union parameters.
1586 -- Jump directly to the end of the loop.
1588 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
1589 goto Skip_Extra_Actual_Generation;
1591 else
1592 -- If the actual is a type conversion, then the constrained
1593 -- test applies to the actual, not the target type.
1595 declare
1596 Act_Prev : Node_Id;
1598 begin
1599 -- Test for unchecked conversions as well, which can occur
1600 -- as out parameter actuals on calls to stream procedures.
1602 Act_Prev := Prev;
1603 while Nkind (Act_Prev) = N_Type_Conversion
1604 or else Nkind (Act_Prev) = N_Unchecked_Type_Conversion
1605 loop
1606 Act_Prev := Expression (Act_Prev);
1607 end loop;
1609 -- If the expression is a conversion of a dereference,
1610 -- this is internally generated code that manipulates
1611 -- addresses, e.g. when building interface tables. No
1612 -- check should occur in this case, and the discriminated
1613 -- object is not directly a hand.
1615 if not Comes_From_Source (Actual)
1616 and then Nkind (Actual) = N_Unchecked_Type_Conversion
1617 and then Nkind (Act_Prev) = N_Explicit_Dereference
1618 then
1619 Add_Extra_Actual
1620 (New_Occurrence_Of (Standard_False, Loc),
1621 Extra_Constrained (Formal));
1623 else
1624 Add_Extra_Actual
1625 (Make_Attribute_Reference (Sloc (Prev),
1626 Prefix =>
1627 Duplicate_Subexpr_No_Checks
1628 (Act_Prev, Name_Req => True),
1629 Attribute_Name => Name_Constrained),
1630 Extra_Constrained (Formal));
1631 end if;
1632 end;
1633 end if;
1634 end if;
1636 -- Create possible extra actual for accessibility level
1638 if Present (Extra_Accessibility (Formal)) then
1639 if Is_Entity_Name (Prev_Orig) then
1641 -- When passing an access parameter as the actual to another
1642 -- access parameter we need to pass along the actual's own
1643 -- associated access level parameter. This is done if we are
1644 -- in the scope of the formal access parameter (if this is an
1645 -- inlined body the extra formal is irrelevant).
1647 if Ekind (Entity (Prev_Orig)) in Formal_Kind
1648 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
1649 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
1650 then
1651 declare
1652 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
1654 begin
1655 pragma Assert (Present (Parm_Ent));
1657 if Present (Extra_Accessibility (Parm_Ent)) then
1658 Add_Extra_Actual
1659 (New_Occurrence_Of
1660 (Extra_Accessibility (Parm_Ent), Loc),
1661 Extra_Accessibility (Formal));
1663 -- If the actual access parameter does not have an
1664 -- associated extra formal providing its scope level,
1665 -- then treat the actual as having library-level
1666 -- accessibility.
1668 else
1669 Add_Extra_Actual
1670 (Make_Integer_Literal (Loc,
1671 Intval => Scope_Depth (Standard_Standard)),
1672 Extra_Accessibility (Formal));
1673 end if;
1674 end;
1676 -- The actual is a normal access value, so just pass the
1677 -- level of the actual's access type.
1679 else
1680 Add_Extra_Actual
1681 (Make_Integer_Literal (Loc,
1682 Intval => Type_Access_Level (Etype (Prev_Orig))),
1683 Extra_Accessibility (Formal));
1684 end if;
1686 else
1687 case Nkind (Prev_Orig) is
1689 when N_Attribute_Reference =>
1691 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
1693 -- For X'Access, pass on the level of the prefix X
1695 when Attribute_Access =>
1696 Add_Extra_Actual (
1697 Make_Integer_Literal (Loc,
1698 Intval =>
1699 Object_Access_Level (Prefix (Prev_Orig))),
1700 Extra_Accessibility (Formal));
1702 -- Treat the unchecked attributes as library-level
1704 when Attribute_Unchecked_Access |
1705 Attribute_Unrestricted_Access =>
1706 Add_Extra_Actual (
1707 Make_Integer_Literal (Loc,
1708 Intval => Scope_Depth (Standard_Standard)),
1709 Extra_Accessibility (Formal));
1711 -- No other cases of attributes returning access
1712 -- values that can be passed to access parameters
1714 when others =>
1715 raise Program_Error;
1717 end case;
1719 -- For allocators we pass the level of the execution of
1720 -- the called subprogram, which is one greater than the
1721 -- current scope level.
1723 when N_Allocator =>
1724 Add_Extra_Actual (
1725 Make_Integer_Literal (Loc,
1726 Scope_Depth (Current_Scope) + 1),
1727 Extra_Accessibility (Formal));
1729 -- For other cases we simply pass the level of the
1730 -- actual's access type.
1732 when others =>
1733 Add_Extra_Actual (
1734 Make_Integer_Literal (Loc,
1735 Intval => Type_Access_Level (Etype (Prev_Orig))),
1736 Extra_Accessibility (Formal));
1738 end case;
1739 end if;
1740 end if;
1742 -- Perform the check of 4.6(49) that prevents a null value from being
1743 -- passed as an actual to an access parameter. Note that the check is
1744 -- elided in the common cases of passing an access attribute or
1745 -- access parameter as an actual. Also, we currently don't enforce
1746 -- this check for expander-generated actuals and when -gnatdj is set.
1748 if Ada_Version >= Ada_05 then
1750 -- Ada 2005 (AI-231): Check null-excluding access types
1752 if Is_Access_Type (Etype (Formal))
1753 and then Can_Never_Be_Null (Etype (Formal))
1754 and then Nkind (Prev) /= N_Raise_Constraint_Error
1755 and then (Nkind (Prev) = N_Null
1756 or else not Can_Never_Be_Null (Etype (Prev)))
1757 then
1758 Install_Null_Excluding_Check (Prev);
1759 end if;
1761 -- Ada_Version < Ada_05
1763 else
1764 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
1765 or else Access_Checks_Suppressed (Subp)
1766 then
1767 null;
1769 elsif Debug_Flag_J then
1770 null;
1772 elsif not Comes_From_Source (Prev) then
1773 null;
1775 elsif Is_Entity_Name (Prev)
1776 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
1777 then
1778 null;
1780 elsif Nkind (Prev) = N_Allocator
1781 or else Nkind (Prev) = N_Attribute_Reference
1782 then
1783 null;
1785 -- Suppress null checks when passing to access parameters of Java
1786 -- subprograms. (Should this be done for other foreign conventions
1787 -- as well ???)
1789 elsif Convention (Subp) = Convention_Java then
1790 null;
1792 else
1793 Install_Null_Excluding_Check (Prev);
1794 end if;
1795 end if;
1797 -- Perform appropriate validity checks on parameters that
1798 -- are entities.
1800 if Validity_Checks_On then
1801 if (Ekind (Formal) = E_In_Parameter
1802 and then Validity_Check_In_Params)
1803 or else
1804 (Ekind (Formal) = E_In_Out_Parameter
1805 and then Validity_Check_In_Out_Params)
1806 then
1807 -- If the actual is an indexed component of a packed
1808 -- type, it has not been expanded yet. It will be
1809 -- copied in the validity code that follows, and has
1810 -- to be expanded appropriately, so reanalyze it.
1812 if Nkind (Actual) = N_Indexed_Component then
1813 Set_Analyzed (Actual, False);
1814 end if;
1816 Ensure_Valid (Actual);
1817 end if;
1818 end if;
1820 -- For IN OUT and OUT parameters, ensure that subscripts are valid
1821 -- since this is a left side reference. We only do this for calls
1822 -- from the source program since we assume that compiler generated
1823 -- calls explicitly generate any required checks. We also need it
1824 -- only if we are doing standard validity checks, since clearly it
1825 -- is not needed if validity checks are off, and in subscript
1826 -- validity checking mode, all indexed components are checked with
1827 -- a call directly from Expand_N_Indexed_Component.
1829 if Comes_From_Source (N)
1830 and then Ekind (Formal) /= E_In_Parameter
1831 and then Validity_Checks_On
1832 and then Validity_Check_Default
1833 and then not Validity_Check_Subscripts
1834 then
1835 Check_Valid_Lvalue_Subscripts (Actual);
1836 end if;
1838 -- Mark any scalar OUT parameter that is a simple variable as no
1839 -- longer known to be valid (unless the type is always valid). This
1840 -- reflects the fact that if an OUT parameter is never set in a
1841 -- procedure, then it can become invalid on the procedure return.
1843 if Ekind (Formal) = E_Out_Parameter
1844 and then Is_Entity_Name (Actual)
1845 and then Ekind (Entity (Actual)) = E_Variable
1846 and then not Is_Known_Valid (Etype (Actual))
1847 then
1848 Set_Is_Known_Valid (Entity (Actual), False);
1849 end if;
1851 -- For an OUT or IN OUT parameter, if the actual is an entity, then
1852 -- clear current values, since they can be clobbered. We are probably
1853 -- doing this in more places than we need to, but better safe than
1854 -- sorry when it comes to retaining bad current values!
1856 if Ekind (Formal) /= E_In_Parameter
1857 and then Is_Entity_Name (Actual)
1858 then
1859 Kill_Current_Values (Entity (Actual));
1860 end if;
1862 -- If the formal is class wide and the actual is an aggregate, force
1863 -- evaluation so that the back end who does not know about class-wide
1864 -- type, does not generate a temporary of the wrong size.
1866 if not Is_Class_Wide_Type (Etype (Formal)) then
1867 null;
1869 elsif Nkind (Actual) = N_Aggregate
1870 or else (Nkind (Actual) = N_Qualified_Expression
1871 and then Nkind (Expression (Actual)) = N_Aggregate)
1872 then
1873 Force_Evaluation (Actual);
1874 end if;
1876 -- In a remote call, if the formal is of a class-wide type, check
1877 -- that the actual meets the requirements described in E.4(18).
1879 if Remote
1880 and then Is_Class_Wide_Type (Etype (Formal))
1881 then
1882 Insert_Action (Actual,
1883 Make_Implicit_If_Statement (N,
1884 Condition =>
1885 Make_Op_Not (Loc,
1886 Get_Remotely_Callable
1887 (Duplicate_Subexpr_Move_Checks (Actual))),
1888 Then_Statements => New_List (
1889 Make_Raise_Program_Error (Loc,
1890 Reason => PE_Illegal_RACW_E_4_18))));
1891 end if;
1893 -- This label is required when skipping extra actual generation for
1894 -- Unchecked_Union parameters.
1896 <<Skip_Extra_Actual_Generation>>
1898 Next_Actual (Actual);
1899 Next_Formal (Formal);
1900 end loop;
1902 -- If we are expanding a rhs of an assignment we need to check if tag
1903 -- propagation is needed. You might expect this processing to be in
1904 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
1905 -- assignment might be transformed to a declaration for an unconstrained
1906 -- value if the expression is classwide.
1908 if Nkind (N) = N_Function_Call
1909 and then Is_Tag_Indeterminate (N)
1910 and then Is_Entity_Name (Name (N))
1911 then
1912 declare
1913 Ass : Node_Id := Empty;
1915 begin
1916 if Nkind (Parent (N)) = N_Assignment_Statement then
1917 Ass := Parent (N);
1919 elsif Nkind (Parent (N)) = N_Qualified_Expression
1920 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
1921 then
1922 Ass := Parent (Parent (N));
1923 end if;
1925 if Present (Ass)
1926 and then Is_Class_Wide_Type (Etype (Name (Ass)))
1927 then
1928 if Etype (N) /= Root_Type (Etype (Name (Ass))) then
1929 Error_Msg_NE
1930 ("tag-indeterminate expression must have type&"
1931 & "('R'M 5.2 (6))", N, Root_Type (Etype (Name (Ass))));
1932 else
1933 Propagate_Tag (Name (Ass), N);
1934 end if;
1936 -- The call will be rewritten as a dispatching call, and
1937 -- expanded as such.
1939 return;
1940 end if;
1941 end;
1942 end if;
1944 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
1945 -- it to point to the correct secondary virtual table
1947 if (Nkind (N) = N_Function_Call
1948 or else Nkind (N) = N_Procedure_Call_Statement)
1949 and then CW_Interface_Formals_Present
1950 then
1951 Expand_Interface_Actuals (N);
1952 end if;
1954 -- Deals with Dispatch_Call if we still have a call, before expanding
1955 -- extra actuals since this will be done on the re-analysis of the
1956 -- dispatching call. Note that we do not try to shorten the actual
1957 -- list for a dispatching call, it would not make sense to do so.
1958 -- Expansion of dispatching calls is suppressed when Java_VM, because
1959 -- the JVM back end directly handles the generation of dispatching
1960 -- calls and would have to undo any expansion to an indirect call.
1962 if (Nkind (N) = N_Function_Call
1963 or else Nkind (N) = N_Procedure_Call_Statement)
1964 and then Present (Controlling_Argument (N))
1965 and then not Java_VM
1966 then
1967 Expand_Dispatching_Call (N);
1969 -- The following return is worrisome. Is it really OK to
1970 -- skip all remaining processing in this procedure ???
1972 return;
1974 -- Similarly, expand calls to RCI subprograms on which pragma
1975 -- All_Calls_Remote applies. The rewriting will be reanalyzed
1976 -- later. Do this only when the call comes from source since we do
1977 -- not want such a rewritting to occur in expanded code.
1979 elsif Is_All_Remote_Call (N) then
1980 Expand_All_Calls_Remote_Subprogram_Call (N);
1982 -- Similarly, do not add extra actuals for an entry call whose entity
1983 -- is a protected procedure, or for an internal protected subprogram
1984 -- call, because it will be rewritten as a protected subprogram call
1985 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
1987 elsif Is_Protected_Type (Scope (Subp))
1988 and then (Ekind (Subp) = E_Procedure
1989 or else Ekind (Subp) = E_Function)
1990 then
1991 null;
1993 -- During that loop we gathered the extra actuals (the ones that
1994 -- correspond to Extra_Formals), so now they can be appended.
1996 else
1997 while Is_Non_Empty_List (Extra_Actuals) loop
1998 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
1999 end loop;
2000 end if;
2002 -- At this point we have all the actuals, so this is the point at
2003 -- which the various expansion activities for actuals is carried out.
2005 Expand_Actuals (N, Subp);
2007 -- If the subprogram is a renaming, or if it is inherited, replace it
2008 -- in the call with the name of the actual subprogram being called.
2009 -- If this is a dispatching call, the run-time decides what to call.
2010 -- The Alias attribute does not apply to entries.
2012 if Nkind (N) /= N_Entry_Call_Statement
2013 and then No (Controlling_Argument (N))
2014 and then Present (Parent_Subp)
2015 then
2016 if Present (Inherited_From_Formal (Subp)) then
2017 Parent_Subp := Inherited_From_Formal (Subp);
2018 else
2019 while Present (Alias (Parent_Subp)) loop
2020 Parent_Subp := Alias (Parent_Subp);
2021 end loop;
2022 end if;
2024 -- The below setting of Entity is suspect, see F109-018 discussion???
2026 Set_Entity (Name (N), Parent_Subp);
2028 if Is_Abstract (Parent_Subp)
2029 and then not In_Instance
2030 then
2031 Error_Msg_NE
2032 ("cannot call abstract subprogram &!", Name (N), Parent_Subp);
2033 end if;
2035 -- Add an explicit conversion for parameter of the derived type.
2036 -- This is only done for scalar and access in-parameters. Others
2037 -- have been expanded in expand_actuals.
2039 Formal := First_Formal (Subp);
2040 Parent_Formal := First_Formal (Parent_Subp);
2041 Actual := First_Actual (N);
2043 -- It is not clear that conversion is needed for intrinsic
2044 -- subprograms, but it certainly is for those that are user-
2045 -- defined, and that can be inherited on derivation, namely
2046 -- unchecked conversion and deallocation.
2047 -- General case needs study ???
2049 if not Is_Intrinsic_Subprogram (Parent_Subp)
2050 or else Is_Generic_Instance (Parent_Subp)
2051 then
2052 while Present (Formal) loop
2053 if Etype (Formal) /= Etype (Parent_Formal)
2054 and then Is_Scalar_Type (Etype (Formal))
2055 and then Ekind (Formal) = E_In_Parameter
2056 and then not Raises_Constraint_Error (Actual)
2057 then
2058 Rewrite (Actual,
2059 OK_Convert_To (Etype (Parent_Formal),
2060 Relocate_Node (Actual)));
2062 Analyze (Actual);
2063 Resolve (Actual, Etype (Parent_Formal));
2064 Enable_Range_Check (Actual);
2066 elsif Is_Access_Type (Etype (Formal))
2067 and then Base_Type (Etype (Parent_Formal)) /=
2068 Base_Type (Etype (Actual))
2069 then
2070 if Ekind (Formal) /= E_In_Parameter then
2071 Rewrite (Actual,
2072 Convert_To (Etype (Parent_Formal),
2073 Relocate_Node (Actual)));
2075 Analyze (Actual);
2076 Resolve (Actual, Etype (Parent_Formal));
2078 elsif
2079 Ekind (Etype (Parent_Formal)) = E_Anonymous_Access_Type
2080 and then Designated_Type (Etype (Parent_Formal))
2082 Designated_Type (Etype (Actual))
2083 and then not Is_Controlling_Formal (Formal)
2084 then
2085 -- This unchecked conversion is not necessary unless
2086 -- inlining is enabled, because in that case the type
2087 -- mismatch may become visible in the body about to be
2088 -- inlined.
2090 Rewrite (Actual,
2091 Unchecked_Convert_To (Etype (Parent_Formal),
2092 Relocate_Node (Actual)));
2094 Analyze (Actual);
2095 Resolve (Actual, Etype (Parent_Formal));
2096 end if;
2097 end if;
2099 Next_Formal (Formal);
2100 Next_Formal (Parent_Formal);
2101 Next_Actual (Actual);
2102 end loop;
2103 end if;
2105 Orig_Subp := Subp;
2106 Subp := Parent_Subp;
2107 end if;
2109 -- Check for violation of No_Abort_Statements
2111 if Is_RTE (Subp, RE_Abort_Task) then
2112 Check_Restriction (No_Abort_Statements, N);
2114 -- Check for violation of No_Dynamic_Attachment
2116 elsif RTU_Loaded (Ada_Interrupts)
2117 and then (Is_RTE (Subp, RE_Is_Reserved) or else
2118 Is_RTE (Subp, RE_Is_Attached) or else
2119 Is_RTE (Subp, RE_Current_Handler) or else
2120 Is_RTE (Subp, RE_Attach_Handler) or else
2121 Is_RTE (Subp, RE_Exchange_Handler) or else
2122 Is_RTE (Subp, RE_Detach_Handler) or else
2123 Is_RTE (Subp, RE_Reference))
2124 then
2125 Check_Restriction (No_Dynamic_Attachment, N);
2126 end if;
2128 -- Deal with case where call is an explicit dereference
2130 if Nkind (Name (N)) = N_Explicit_Dereference then
2132 -- Handle case of access to protected subprogram type
2134 if Ekind (Base_Type (Etype (Prefix (Name (N))))) =
2135 E_Access_Protected_Subprogram_Type
2136 then
2137 -- If this is a call through an access to protected operation,
2138 -- the prefix has the form (object'address, operation'access).
2139 -- Rewrite as a for other protected calls: the object is the
2140 -- first parameter of the list of actuals.
2142 declare
2143 Call : Node_Id;
2144 Parm : List_Id;
2145 Nam : Node_Id;
2146 Obj : Node_Id;
2147 Ptr : constant Node_Id := Prefix (Name (N));
2149 T : constant Entity_Id :=
2150 Equivalent_Type (Base_Type (Etype (Ptr)));
2152 D_T : constant Entity_Id :=
2153 Designated_Type (Base_Type (Etype (Ptr)));
2155 begin
2156 Obj :=
2157 Make_Selected_Component (Loc,
2158 Prefix => Unchecked_Convert_To (T, Ptr),
2159 Selector_Name =>
2160 New_Occurrence_Of (First_Entity (T), Loc));
2162 Nam :=
2163 Make_Selected_Component (Loc,
2164 Prefix => Unchecked_Convert_To (T, Ptr),
2165 Selector_Name =>
2166 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
2168 Nam := Make_Explicit_Dereference (Loc, Nam);
2170 if Present (Parameter_Associations (N)) then
2171 Parm := Parameter_Associations (N);
2172 else
2173 Parm := New_List;
2174 end if;
2176 Prepend (Obj, Parm);
2178 if Etype (D_T) = Standard_Void_Type then
2179 Call := Make_Procedure_Call_Statement (Loc,
2180 Name => Nam,
2181 Parameter_Associations => Parm);
2182 else
2183 Call := Make_Function_Call (Loc,
2184 Name => Nam,
2185 Parameter_Associations => Parm);
2186 end if;
2188 Set_First_Named_Actual (Call, First_Named_Actual (N));
2189 Set_Etype (Call, Etype (D_T));
2191 -- We do not re-analyze the call to avoid infinite recursion.
2192 -- We analyze separately the prefix and the object, and set
2193 -- the checks on the prefix that would otherwise be emitted
2194 -- when resolving a call.
2196 Rewrite (N, Call);
2197 Analyze (Nam);
2198 Apply_Access_Check (Nam);
2199 Analyze (Obj);
2200 return;
2201 end;
2202 end if;
2203 end if;
2205 -- If this is a call to an intrinsic subprogram, then perform the
2206 -- appropriate expansion to the corresponding tree node and we
2207 -- are all done (since after that the call is gone!)
2209 -- In the case where the intrinsic is to be processed by the back end,
2210 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
2211 -- since the idea in this case is to pass the call unchanged.
2213 if Is_Intrinsic_Subprogram (Subp) then
2214 Expand_Intrinsic_Call (N, Subp);
2215 return;
2216 end if;
2218 if Ekind (Subp) = E_Function
2219 or else Ekind (Subp) = E_Procedure
2220 then
2221 if Is_Inlined (Subp) then
2223 Inlined_Subprogram : declare
2224 Bod : Node_Id;
2225 Must_Inline : Boolean := False;
2226 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2227 Scop : constant Entity_Id := Scope (Subp);
2229 function In_Unfrozen_Instance return Boolean;
2230 -- If the subprogram comes from an instance in the same
2231 -- unit, and the instance is not yet frozen, inlining might
2232 -- trigger order-of-elaboration problems in gigi.
2234 --------------------------
2235 -- In_Unfrozen_Instance --
2236 --------------------------
2238 function In_Unfrozen_Instance return Boolean is
2239 S : Entity_Id;
2241 begin
2242 S := Scop;
2243 while Present (S)
2244 and then S /= Standard_Standard
2245 loop
2246 if Is_Generic_Instance (S)
2247 and then Present (Freeze_Node (S))
2248 and then not Analyzed (Freeze_Node (S))
2249 then
2250 return True;
2251 end if;
2253 S := Scope (S);
2254 end loop;
2256 return False;
2257 end In_Unfrozen_Instance;
2259 -- Start of processing for Inlined_Subprogram
2261 begin
2262 -- Verify that the body to inline has already been seen, and
2263 -- that if the body is in the current unit the inlining does
2264 -- not occur earlier. This avoids order-of-elaboration problems
2265 -- in the back end.
2267 -- This should be documented in sinfo/einfo ???
2269 if No (Spec)
2270 or else Nkind (Spec) /= N_Subprogram_Declaration
2271 or else No (Body_To_Inline (Spec))
2272 then
2273 Must_Inline := False;
2275 -- If this an inherited function that returns a private
2276 -- type, do not inline if the full view is an unconstrained
2277 -- array, because such calls cannot be inlined.
2279 elsif Present (Orig_Subp)
2280 and then Is_Array_Type (Etype (Orig_Subp))
2281 and then not Is_Constrained (Etype (Orig_Subp))
2282 then
2283 Must_Inline := False;
2285 elsif In_Unfrozen_Instance then
2286 Must_Inline := False;
2288 else
2289 Bod := Body_To_Inline (Spec);
2291 if (In_Extended_Main_Code_Unit (N)
2292 or else In_Extended_Main_Code_Unit (Parent (N))
2293 or else Is_Always_Inlined (Subp))
2294 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
2295 or else
2296 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
2297 then
2298 Must_Inline := True;
2300 -- If we are compiling a package body that is not the main
2301 -- unit, it must be for inlining/instantiation purposes,
2302 -- in which case we inline the call to insure that the same
2303 -- temporaries are generated when compiling the body by
2304 -- itself. Otherwise link errors can occur.
2306 -- If the function being called is itself in the main unit,
2307 -- we cannot inline, because there is a risk of double
2308 -- elaboration and/or circularity: the inlining can make
2309 -- visible a private entity in the body of the main unit,
2310 -- that gigi will see before its sees its proper definition.
2312 elsif not (In_Extended_Main_Code_Unit (N))
2313 and then In_Package_Body
2314 then
2315 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
2316 end if;
2317 end if;
2319 if Must_Inline then
2320 Expand_Inlined_Call (N, Subp, Orig_Subp);
2322 else
2323 -- Let the back end handle it
2325 Add_Inlined_Body (Subp);
2327 if Front_End_Inlining
2328 and then Nkind (Spec) = N_Subprogram_Declaration
2329 and then (In_Extended_Main_Code_Unit (N))
2330 and then No (Body_To_Inline (Spec))
2331 and then not Has_Completion (Subp)
2332 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
2333 then
2334 Cannot_Inline
2335 ("cannot inline& (body not seen yet)?",
2336 N, Subp);
2337 end if;
2338 end if;
2339 end Inlined_Subprogram;
2340 end if;
2341 end if;
2343 -- Check for a protected subprogram. This is either an intra-object
2344 -- call, or a protected function call. Protected procedure calls are
2345 -- rewritten as entry calls and handled accordingly.
2347 -- In Ada 2005, this may be an indirect call to an access parameter
2348 -- that is an access_to_subprogram. In that case the anonymous type
2349 -- has a scope that is a protected operation, but the call is a
2350 -- regular one.
2352 Scop := Scope (Subp);
2354 if Nkind (N) /= N_Entry_Call_Statement
2355 and then Is_Protected_Type (Scop)
2356 and then Ekind (Subp) /= E_Subprogram_Type
2357 then
2358 -- If the call is an internal one, it is rewritten as a call to
2359 -- to the corresponding unprotected subprogram.
2361 Expand_Protected_Subprogram_Call (N, Subp, Scop);
2362 end if;
2364 -- Functions returning controlled objects need special attention
2366 if Controlled_Type (Etype (Subp))
2367 and then not Is_Return_By_Reference_Type (Etype (Subp))
2368 then
2369 Expand_Ctrl_Function_Call (N);
2370 end if;
2372 -- Test for First_Optional_Parameter, and if so, truncate parameter
2373 -- list if there are optional parameters at the trailing end.
2374 -- Note we never delete procedures for call via a pointer.
2376 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
2377 and then Present (First_Optional_Parameter (Subp))
2378 then
2379 declare
2380 Last_Keep_Arg : Node_Id;
2382 begin
2383 -- Last_Keep_Arg will hold the last actual that should be
2384 -- retained. If it remains empty at the end, it means that
2385 -- all parameters are optional.
2387 Last_Keep_Arg := Empty;
2389 -- Find first optional parameter, must be present since we
2390 -- checked the validity of the parameter before setting it.
2392 Formal := First_Formal (Subp);
2393 Actual := First_Actual (N);
2394 while Formal /= First_Optional_Parameter (Subp) loop
2395 Last_Keep_Arg := Actual;
2396 Next_Formal (Formal);
2397 Next_Actual (Actual);
2398 end loop;
2400 -- We have Formal and Actual pointing to the first potentially
2401 -- droppable argument. We can drop all the trailing arguments
2402 -- whose actual matches the default. Note that we know that all
2403 -- remaining formals have defaults, because we checked that this
2404 -- requirement was met before setting First_Optional_Parameter.
2406 -- We use Fully_Conformant_Expressions to check for identity
2407 -- between formals and actuals, which may miss some cases, but
2408 -- on the other hand, this is only an optimization (if we fail
2409 -- to truncate a parameter it does not affect functionality).
2410 -- So if the default is 3 and the actual is 1+2, we consider
2411 -- them unequal, which hardly seems worrisome.
2413 while Present (Formal) loop
2414 if not Fully_Conformant_Expressions
2415 (Actual, Default_Value (Formal))
2416 then
2417 Last_Keep_Arg := Actual;
2418 end if;
2420 Next_Formal (Formal);
2421 Next_Actual (Actual);
2422 end loop;
2424 -- If no arguments, delete entire list, this is the easy case
2426 if No (Last_Keep_Arg) then
2427 while Is_Non_Empty_List (Parameter_Associations (N)) loop
2428 Delete_Tree (Remove_Head (Parameter_Associations (N)));
2429 end loop;
2431 Set_Parameter_Associations (N, No_List);
2432 Set_First_Named_Actual (N, Empty);
2434 -- Case where at the last retained argument is positional. This
2435 -- is also an easy case, since the retained arguments are already
2436 -- in the right form, and we don't need to worry about the order
2437 -- of arguments that get eliminated.
2439 elsif Is_List_Member (Last_Keep_Arg) then
2440 while Present (Next (Last_Keep_Arg)) loop
2441 Delete_Tree (Remove_Next (Last_Keep_Arg));
2442 end loop;
2444 Set_First_Named_Actual (N, Empty);
2446 -- This is the annoying case where the last retained argument
2447 -- is a named parameter. Since the original arguments are not
2448 -- in declaration order, we may have to delete some fairly
2449 -- random collection of arguments.
2451 else
2452 declare
2453 Temp : Node_Id;
2454 Passoc : Node_Id;
2456 Discard : Node_Id;
2457 pragma Warnings (Off, Discard);
2459 begin
2460 -- First step, remove all the named parameters from the
2461 -- list (they are still chained using First_Named_Actual
2462 -- and Next_Named_Actual, so we have not lost them!)
2464 Temp := First (Parameter_Associations (N));
2466 -- Case of all parameters named, remove them all
2468 if Nkind (Temp) = N_Parameter_Association then
2469 while Is_Non_Empty_List (Parameter_Associations (N)) loop
2470 Temp := Remove_Head (Parameter_Associations (N));
2471 end loop;
2473 -- Case of mixed positional/named, remove named parameters
2475 else
2476 while Nkind (Next (Temp)) /= N_Parameter_Association loop
2477 Next (Temp);
2478 end loop;
2480 while Present (Next (Temp)) loop
2481 Discard := Remove_Next (Temp);
2482 end loop;
2483 end if;
2485 -- Now we loop through the named parameters, till we get
2486 -- to the last one to be retained, adding them to the list.
2487 -- Note that the Next_Named_Actual list does not need to be
2488 -- touched since we are only reordering them on the actual
2489 -- parameter association list.
2491 Passoc := Parent (First_Named_Actual (N));
2492 loop
2493 Temp := Relocate_Node (Passoc);
2494 Append_To
2495 (Parameter_Associations (N), Temp);
2496 exit when
2497 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
2498 Passoc := Parent (Next_Named_Actual (Passoc));
2499 end loop;
2501 Set_Next_Named_Actual (Temp, Empty);
2503 loop
2504 Temp := Next_Named_Actual (Passoc);
2505 exit when No (Temp);
2506 Set_Next_Named_Actual
2507 (Passoc, Next_Named_Actual (Parent (Temp)));
2508 Delete_Tree (Temp);
2509 end loop;
2510 end;
2511 end if;
2512 end;
2513 end if;
2515 -- Special processing for Ada 2005 AI-329, which requires a call to
2516 -- Raise_Exception to raise Constraint_Error if the Exception_Id is
2517 -- null. Note that we never need to do this in GNAT mode, or if the
2518 -- parameter to Raise_Exception is a use of Identity, since in these
2519 -- cases we know that the parameter is never null.
2521 if Ada_Version >= Ada_05
2522 and then not GNAT_Mode
2523 and then Is_RTE (Subp, RE_Raise_Exception)
2524 and then (Nkind (First_Actual (N)) /= N_Attribute_Reference
2525 or else Attribute_Name (First_Actual (N)) /= Name_Identity)
2526 then
2527 declare
2528 RCE : constant Node_Id :=
2529 Make_Raise_Constraint_Error (Loc,
2530 Reason => CE_Null_Exception_Id);
2531 begin
2532 Insert_After (N, RCE);
2533 Analyze (RCE);
2534 end;
2535 end if;
2536 end Expand_Call;
2538 --------------------------
2539 -- Expand_Inlined_Call --
2540 --------------------------
2542 procedure Expand_Inlined_Call
2543 (N : Node_Id;
2544 Subp : Entity_Id;
2545 Orig_Subp : Entity_Id)
2547 Loc : constant Source_Ptr := Sloc (N);
2548 Is_Predef : constant Boolean :=
2549 Is_Predefined_File_Name
2550 (Unit_File_Name (Get_Source_Unit (Subp)));
2551 Orig_Bod : constant Node_Id :=
2552 Body_To_Inline (Unit_Declaration_Node (Subp));
2554 Blk : Node_Id;
2555 Bod : Node_Id;
2556 Decl : Node_Id;
2557 Decls : constant List_Id := New_List;
2558 Exit_Lab : Entity_Id := Empty;
2559 F : Entity_Id;
2560 A : Node_Id;
2561 Lab_Decl : Node_Id;
2562 Lab_Id : Node_Id;
2563 New_A : Node_Id;
2564 Num_Ret : Int := 0;
2565 Ret_Type : Entity_Id;
2566 Targ : Node_Id;
2567 Targ1 : Node_Id;
2568 Temp : Entity_Id;
2569 Temp_Typ : Entity_Id;
2571 Is_Unc : constant Boolean :=
2572 Is_Array_Type (Etype (Subp))
2573 and then not Is_Constrained (Etype (Subp));
2574 -- If the type returned by the function is unconstrained and the
2575 -- call can be inlined, special processing is required.
2577 procedure Find_Result;
2578 -- For a function that returns an unconstrained type, retrieve the
2579 -- name of the single variable that is the expression of a return
2580 -- statement in the body of the function. Build_Body_To_Inline has
2581 -- verified that this variable is unique, even in the presence of
2582 -- multiple return statements.
2584 procedure Make_Exit_Label;
2585 -- Build declaration for exit label to be used in Return statements
2587 function Process_Formals (N : Node_Id) return Traverse_Result;
2588 -- Replace occurrence of a formal with the corresponding actual, or
2589 -- the thunk generated for it.
2591 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
2592 -- If the call being expanded is that of an internal subprogram,
2593 -- set the sloc of the generated block to that of the call itself,
2594 -- so that the expansion is skipped by the -next- command in gdb.
2595 -- Same processing for a subprogram in a predefined file, e.g.
2596 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change
2597 -- to simplify our own development.
2599 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
2600 -- If the function body is a single expression, replace call with
2601 -- expression, else insert block appropriately.
2603 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
2604 -- If procedure body has no local variables, inline body without
2605 -- creating block, otherwise rewrite call with block.
2607 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2608 -- Determine whether a formal parameter is used only once in Orig_Bod
2610 -----------------
2611 -- Find_Result --
2612 -----------------
2614 procedure Find_Result is
2615 Decl : Node_Id;
2616 Id : Node_Id;
2618 function Get_Return (N : Node_Id) return Traverse_Result;
2619 -- Recursive function to locate return statements in body.
2621 function Get_Return (N : Node_Id) return Traverse_Result is
2622 begin
2623 if Nkind (N) = N_Return_Statement then
2624 Id := Expression (N);
2625 return Abandon;
2626 else
2627 return OK;
2628 end if;
2629 end Get_Return;
2631 procedure Find_It is new Traverse_Proc (Get_Return);
2633 -- Start of processing for Find_Result
2635 begin
2636 Find_It (Handled_Statement_Sequence (Orig_Bod));
2638 -- At this point the body is unanalyzed. Traverse the list of
2639 -- declarations to locate the defining_identifier for it.
2641 Decl := First (Declarations (Blk));
2643 while Present (Decl) loop
2644 if Chars (Defining_Identifier (Decl)) = Chars (Id) then
2645 Targ1 := Defining_Identifier (Decl);
2646 exit;
2648 else
2649 Next (Decl);
2650 end if;
2651 end loop;
2652 end Find_Result;
2654 ---------------------
2655 -- Make_Exit_Label --
2656 ---------------------
2658 procedure Make_Exit_Label is
2659 begin
2660 -- Create exit label for subprogram if one does not exist yet
2662 if No (Exit_Lab) then
2663 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
2664 Set_Entity (Lab_Id,
2665 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
2666 Exit_Lab := Make_Label (Loc, Lab_Id);
2668 Lab_Decl :=
2669 Make_Implicit_Label_Declaration (Loc,
2670 Defining_Identifier => Entity (Lab_Id),
2671 Label_Construct => Exit_Lab);
2672 end if;
2673 end Make_Exit_Label;
2675 ---------------------
2676 -- Process_Formals --
2677 ---------------------
2679 function Process_Formals (N : Node_Id) return Traverse_Result is
2680 A : Entity_Id;
2681 E : Entity_Id;
2682 Ret : Node_Id;
2684 begin
2685 if Is_Entity_Name (N)
2686 and then Present (Entity (N))
2687 then
2688 E := Entity (N);
2690 if Is_Formal (E)
2691 and then Scope (E) = Subp
2692 then
2693 A := Renamed_Object (E);
2695 if Is_Entity_Name (A) then
2696 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
2698 elsif Nkind (A) = N_Defining_Identifier then
2699 Rewrite (N, New_Occurrence_Of (A, Loc));
2701 else -- numeric literal
2702 Rewrite (N, New_Copy (A));
2703 end if;
2704 end if;
2706 return Skip;
2708 elsif Nkind (N) = N_Return_Statement then
2710 if No (Expression (N)) then
2711 Make_Exit_Label;
2712 Rewrite (N, Make_Goto_Statement (Loc,
2713 Name => New_Copy (Lab_Id)));
2715 else
2716 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
2717 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
2718 then
2719 -- Function body is a single expression. No need for
2720 -- exit label.
2722 null;
2724 else
2725 Num_Ret := Num_Ret + 1;
2726 Make_Exit_Label;
2727 end if;
2729 -- Because of the presence of private types, the views of the
2730 -- expression and the context may be different, so place an
2731 -- unchecked conversion to the context type to avoid spurious
2732 -- errors, eg. when the expression is a numeric literal and
2733 -- the context is private. If the expression is an aggregate,
2734 -- use a qualified expression, because an aggregate is not a
2735 -- legal argument of a conversion.
2737 if Nkind (Expression (N)) = N_Aggregate
2738 or else Nkind (Expression (N)) = N_Null
2739 then
2740 Ret :=
2741 Make_Qualified_Expression (Sloc (N),
2742 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2743 Expression => Relocate_Node (Expression (N)));
2744 else
2745 Ret :=
2746 Unchecked_Convert_To
2747 (Ret_Type, Relocate_Node (Expression (N)));
2748 end if;
2750 if Nkind (Targ) = N_Defining_Identifier then
2751 Rewrite (N,
2752 Make_Assignment_Statement (Loc,
2753 Name => New_Occurrence_Of (Targ, Loc),
2754 Expression => Ret));
2755 else
2756 Rewrite (N,
2757 Make_Assignment_Statement (Loc,
2758 Name => New_Copy (Targ),
2759 Expression => Ret));
2760 end if;
2762 Set_Assignment_OK (Name (N));
2764 if Present (Exit_Lab) then
2765 Insert_After (N,
2766 Make_Goto_Statement (Loc,
2767 Name => New_Copy (Lab_Id)));
2768 end if;
2769 end if;
2771 return OK;
2773 -- Remove pragma Unreferenced since it may refer to formals that
2774 -- are not visible in the inlined body, and in any case we will
2775 -- not be posting warnings on the inlined body so it is unneeded.
2777 elsif Nkind (N) = N_Pragma
2778 and then Chars (N) = Name_Unreferenced
2779 then
2780 Rewrite (N, Make_Null_Statement (Sloc (N)));
2781 return OK;
2783 else
2784 return OK;
2785 end if;
2786 end Process_Formals;
2788 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
2790 ------------------
2791 -- Process_Sloc --
2792 ------------------
2794 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
2795 begin
2796 if not Debug_Generated_Code then
2797 Set_Sloc (Nod, Sloc (N));
2798 Set_Comes_From_Source (Nod, False);
2799 end if;
2801 return OK;
2802 end Process_Sloc;
2804 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
2806 ---------------------------
2807 -- Rewrite_Function_Call --
2808 ---------------------------
2810 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
2811 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2812 Fst : constant Node_Id := First (Statements (HSS));
2814 begin
2815 -- Optimize simple case: function body is a single return statement,
2816 -- which has been expanded into an assignment.
2818 if Is_Empty_List (Declarations (Blk))
2819 and then Nkind (Fst) = N_Assignment_Statement
2820 and then No (Next (Fst))
2821 then
2823 -- The function call may have been rewritten as the temporary
2824 -- that holds the result of the call, in which case remove the
2825 -- now useless declaration.
2827 if Nkind (N) = N_Identifier
2828 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2829 then
2830 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
2831 end if;
2833 Rewrite (N, Expression (Fst));
2835 elsif Nkind (N) = N_Identifier
2836 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2837 then
2838 -- The block assigns the result of the call to the temporary
2840 Insert_After (Parent (Entity (N)), Blk);
2842 elsif Nkind (Parent (N)) = N_Assignment_Statement
2843 and then
2844 (Is_Entity_Name (Name (Parent (N)))
2845 or else
2846 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
2847 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
2848 then
2849 -- Replace assignment with the block
2851 declare
2852 Original_Assignment : constant Node_Id := Parent (N);
2854 begin
2855 -- Preserve the original assignment node to keep the complete
2856 -- assignment subtree consistent enough for Analyze_Assignment
2857 -- to proceed (specifically, the original Lhs node must still
2858 -- have an assignment statement as its parent).
2860 -- We cannot rely on Original_Node to go back from the block
2861 -- node to the assignment node, because the assignment might
2862 -- already be a rewrite substitution.
2864 Discard_Node (Relocate_Node (Original_Assignment));
2865 Rewrite (Original_Assignment, Blk);
2866 end;
2868 elsif Nkind (Parent (N)) = N_Object_Declaration then
2869 Set_Expression (Parent (N), Empty);
2870 Insert_After (Parent (N), Blk);
2872 elsif Is_Unc then
2873 Insert_Before (Parent (N), Blk);
2874 end if;
2875 end Rewrite_Function_Call;
2877 ----------------------------
2878 -- Rewrite_Procedure_Call --
2879 ----------------------------
2881 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
2882 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2883 begin
2884 if Is_Empty_List (Declarations (Blk)) then
2885 Insert_List_After (N, Statements (HSS));
2886 Rewrite (N, Make_Null_Statement (Loc));
2887 else
2888 Rewrite (N, Blk);
2889 end if;
2890 end Rewrite_Procedure_Call;
2892 -------------------------
2893 -- Formal_Is_Used_Once --
2894 ------------------------
2896 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
2897 Use_Counter : Int := 0;
2899 function Count_Uses (N : Node_Id) return Traverse_Result;
2900 -- Traverse the tree and count the uses of the formal parameter.
2901 -- In this case, for optimization purposes, we do not need to
2902 -- continue the traversal once more than one use is encountered.
2904 ----------------
2905 -- Count_Uses --
2906 ----------------
2908 function Count_Uses (N : Node_Id) return Traverse_Result is
2909 begin
2910 -- The original node is an identifier
2912 if Nkind (N) = N_Identifier
2913 and then Present (Entity (N))
2915 -- Original node's entity points to the one in the copied body
2917 and then Nkind (Entity (N)) = N_Identifier
2918 and then Present (Entity (Entity (N)))
2920 -- The entity of the copied node is the formal parameter
2922 and then Entity (Entity (N)) = Formal
2923 then
2924 Use_Counter := Use_Counter + 1;
2926 if Use_Counter > 1 then
2928 -- Denote more than one use and abandon the traversal
2930 Use_Counter := 2;
2931 return Abandon;
2933 end if;
2934 end if;
2936 return OK;
2937 end Count_Uses;
2939 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
2941 -- Start of processing for Formal_Is_Used_Once
2943 begin
2944 Count_Formal_Uses (Orig_Bod);
2945 return Use_Counter = 1;
2946 end Formal_Is_Used_Once;
2948 -- Start of processing for Expand_Inlined_Call
2950 begin
2951 -- Check for special case of To_Address call, and if so, just do an
2952 -- unchecked conversion instead of expanding the call. Not only is this
2953 -- more efficient, but it also avoids problem with order of elaboration
2954 -- when address clauses are inlined (address expression elaborated at
2955 -- wrong point).
2957 if Subp = RTE (RE_To_Address) then
2958 Rewrite (N,
2959 Unchecked_Convert_To
2960 (RTE (RE_Address),
2961 Relocate_Node (First_Actual (N))));
2962 return;
2963 end if;
2965 -- Check for an illegal attempt to inline a recursive procedure. If the
2966 -- subprogram has parameters this is detected when trying to supply a
2967 -- binding for parameters that already have one. For parameterless
2968 -- subprograms this must be done explicitly.
2970 if In_Open_Scopes (Subp) then
2971 Error_Msg_N ("call to recursive subprogram cannot be inlined?", N);
2972 Set_Is_Inlined (Subp, False);
2973 return;
2974 end if;
2976 if Nkind (Orig_Bod) = N_Defining_Identifier
2977 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
2978 then
2979 -- Subprogram is a renaming_as_body. Calls appearing after the
2980 -- renaming can be replaced with calls to the renamed entity
2981 -- directly, because the subprograms are subtype conformant. If
2982 -- the renamed subprogram is an inherited operation, we must redo
2983 -- the expansion because implicit conversions may be needed.
2985 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
2987 if Present (Alias (Orig_Bod)) then
2988 Expand_Call (N);
2989 end if;
2991 return;
2992 end if;
2994 -- Use generic machinery to copy body of inlined subprogram, as if it
2995 -- were an instantiation, resetting source locations appropriately, so
2996 -- that nested inlined calls appear in the main unit.
2998 Save_Env (Subp, Empty);
2999 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
3001 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3002 Blk :=
3003 Make_Block_Statement (Loc,
3004 Declarations => Declarations (Bod),
3005 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
3007 if No (Declarations (Bod)) then
3008 Set_Declarations (Blk, New_List);
3009 end if;
3011 -- For the unconstrained case, capture the name of the local
3012 -- variable that holds the result.
3014 if Is_Unc then
3015 Find_Result;
3016 end if;
3018 -- If this is a derived function, establish the proper return type
3020 if Present (Orig_Subp)
3021 and then Orig_Subp /= Subp
3022 then
3023 Ret_Type := Etype (Orig_Subp);
3024 else
3025 Ret_Type := Etype (Subp);
3026 end if;
3028 -- Create temporaries for the actuals that are expressions, or that
3029 -- are scalars and require copying to preserve semantics.
3031 F := First_Formal (Subp);
3032 A := First_Actual (N);
3033 while Present (F) loop
3034 if Present (Renamed_Object (F)) then
3035 Error_Msg_N ("cannot inline call to recursive subprogram", N);
3036 return;
3037 end if;
3039 -- If the argument may be a controlling argument in a call within
3040 -- the inlined body, we must preserve its classwide nature to insure
3041 -- that dynamic dispatching take place subsequently. If the formal
3042 -- has a constraint it must be preserved to retain the semantics of
3043 -- the body.
3045 if Is_Class_Wide_Type (Etype (F))
3046 or else (Is_Access_Type (Etype (F))
3047 and then
3048 Is_Class_Wide_Type (Designated_Type (Etype (F))))
3049 then
3050 Temp_Typ := Etype (F);
3052 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3053 and then Etype (F) /= Base_Type (Etype (F))
3054 then
3055 Temp_Typ := Etype (F);
3057 else
3058 Temp_Typ := Etype (A);
3059 end if;
3061 -- If the actual is a simple name or a literal, no need to
3062 -- create a temporary, object can be used directly.
3064 if (Is_Entity_Name (A)
3065 and then
3066 (not Is_Scalar_Type (Etype (A))
3067 or else Ekind (Entity (A)) = E_Enumeration_Literal))
3069 -- When the actual is an identifier and the corresponding formal
3070 -- is used only once in the original body, the formal can be
3071 -- substituted directly with the actual parameter.
3073 or else (Nkind (A) = N_Identifier
3074 and then Formal_Is_Used_Once (F))
3076 or else Nkind (A) = N_Real_Literal
3077 or else Nkind (A) = N_Integer_Literal
3078 or else Nkind (A) = N_Character_Literal
3079 then
3080 if Etype (F) /= Etype (A) then
3081 Set_Renamed_Object
3082 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3083 else
3084 Set_Renamed_Object (F, A);
3085 end if;
3087 else
3088 Temp :=
3089 Make_Defining_Identifier (Loc,
3090 Chars => New_Internal_Name ('C'));
3092 -- If the actual for an in/in-out parameter is a view conversion,
3093 -- make it into an unchecked conversion, given that an untagged
3094 -- type conversion is not a proper object for a renaming.
3096 -- In-out conversions that involve real conversions have already
3097 -- been transformed in Expand_Actuals.
3099 if Nkind (A) = N_Type_Conversion
3100 and then Ekind (F) /= E_In_Parameter
3101 then
3102 New_A := Make_Unchecked_Type_Conversion (Loc,
3103 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3104 Expression => Relocate_Node (Expression (A)));
3106 elsif Etype (F) /= Etype (A) then
3107 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3108 Temp_Typ := Etype (F);
3110 else
3111 New_A := Relocate_Node (A);
3112 end if;
3114 Set_Sloc (New_A, Sloc (N));
3116 if Ekind (F) = E_In_Parameter
3117 and then not Is_Limited_Type (Etype (A))
3118 then
3119 Decl :=
3120 Make_Object_Declaration (Loc,
3121 Defining_Identifier => Temp,
3122 Constant_Present => True,
3123 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3124 Expression => New_A);
3125 else
3126 Decl :=
3127 Make_Object_Renaming_Declaration (Loc,
3128 Defining_Identifier => Temp,
3129 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3130 Name => New_A);
3131 end if;
3133 Append (Decl, Decls);
3134 Set_Renamed_Object (F, Temp);
3135 end if;
3137 Next_Formal (F);
3138 Next_Actual (A);
3139 end loop;
3141 -- Establish target of function call. If context is not assignment or
3142 -- declaration, create a temporary as a target. The declaration for
3143 -- the temporary may be subsequently optimized away if the body is a
3144 -- single expression, or if the left-hand side of the assignment is
3145 -- simple enough, i.e. an entity or an explicit dereference of one.
3147 if Ekind (Subp) = E_Function then
3148 if Nkind (Parent (N)) = N_Assignment_Statement
3149 and then Is_Entity_Name (Name (Parent (N)))
3150 then
3151 Targ := Name (Parent (N));
3153 elsif Nkind (Parent (N)) = N_Assignment_Statement
3154 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
3155 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3156 then
3157 Targ := Name (Parent (N));
3159 else
3160 -- Replace call with temporary and create its declaration
3162 Temp :=
3163 Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
3164 Set_Is_Internal (Temp);
3166 -- For the unconstrained case. the generated temporary has the
3167 -- same constrained declaration as the result variable.
3168 -- It may eventually be possible to remove that temporary and
3169 -- use the result variable directly.
3171 if Is_Unc then
3172 Decl :=
3173 Make_Object_Declaration (Loc,
3174 Defining_Identifier => Temp,
3175 Object_Definition =>
3176 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3178 Replace_Formals (Decl);
3180 else
3181 Decl :=
3182 Make_Object_Declaration (Loc,
3183 Defining_Identifier => Temp,
3184 Object_Definition =>
3185 New_Occurrence_Of (Ret_Type, Loc));
3187 Set_Etype (Temp, Ret_Type);
3188 end if;
3190 Set_No_Initialization (Decl);
3191 Append (Decl, Decls);
3192 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3193 Targ := Temp;
3194 end if;
3195 end if;
3197 Insert_Actions (N, Decls);
3199 -- Traverse the tree and replace formals with actuals or their thunks.
3200 -- Attach block to tree before analysis and rewriting.
3202 Replace_Formals (Blk);
3203 Set_Parent (Blk, N);
3205 if not Comes_From_Source (Subp)
3206 or else Is_Predef
3207 then
3208 Reset_Slocs (Blk);
3209 end if;
3211 if Present (Exit_Lab) then
3213 -- If the body was a single expression, the single return statement
3214 -- and the corresponding label are useless.
3216 if Num_Ret = 1
3217 and then
3218 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3219 N_Goto_Statement
3220 then
3221 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3222 else
3223 Append (Lab_Decl, (Declarations (Blk)));
3224 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
3225 end if;
3226 end if;
3228 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
3229 -- conflicting private views that Gigi would ignore. If this is
3230 -- predefined unit, analyze with checks off, as is done in the non-
3231 -- inlined run-time units.
3233 declare
3234 I_Flag : constant Boolean := In_Inlined_Body;
3236 begin
3237 In_Inlined_Body := True;
3239 if Is_Predef then
3240 declare
3241 Style : constant Boolean := Style_Check;
3242 begin
3243 Style_Check := False;
3244 Analyze (Blk, Suppress => All_Checks);
3245 Style_Check := Style;
3246 end;
3248 else
3249 Analyze (Blk);
3250 end if;
3252 In_Inlined_Body := I_Flag;
3253 end;
3255 if Ekind (Subp) = E_Procedure then
3256 Rewrite_Procedure_Call (N, Blk);
3257 else
3258 Rewrite_Function_Call (N, Blk);
3260 -- For the unconstrained case, the replacement of the call has been
3261 -- made prior to the complete analysis of the generated declarations.
3262 -- Propagate the proper type now.
3264 if Is_Unc then
3265 if Nkind (N) = N_Identifier then
3266 Set_Etype (N, Etype (Entity (N)));
3267 else
3268 Set_Etype (N, Etype (Targ1));
3269 end if;
3270 end if;
3271 end if;
3273 Restore_Env;
3275 -- Cleanup mapping between formals and actuals for other expansions
3277 F := First_Formal (Subp);
3278 while Present (F) loop
3279 Set_Renamed_Object (F, Empty);
3280 Next_Formal (F);
3281 end loop;
3282 end Expand_Inlined_Call;
3284 ----------------------------
3285 -- Expand_N_Function_Call --
3286 ----------------------------
3288 procedure Expand_N_Function_Call (N : Node_Id) is
3289 Typ : constant Entity_Id := Etype (N);
3291 function Returned_By_Reference return Boolean;
3292 -- If the return type is returned through the secondary stack. that is
3293 -- by reference, we don't want to create a temp to force stack checking.
3294 -- Shouldn't this function be moved to exp_util???
3296 function Rhs_Of_Assign_Or_Decl (N : Node_Id) return Boolean;
3297 -- If the call is the right side of an assignment or the expression in
3298 -- an object declaration, we don't need to create a temp as the left
3299 -- side will already trigger stack checking if necessary.
3301 -- If the call is a component in an extension aggregate, it will be
3302 -- expanded into assignments as well, so no temporary is needed. This
3303 -- also solves the problem of functions returning types with unknown
3304 -- discriminants, where it is not possible to declare an object of the
3305 -- type altogether.
3307 ---------------------------
3308 -- Returned_By_Reference --
3309 ---------------------------
3311 function Returned_By_Reference return Boolean is
3312 S : Entity_Id;
3314 begin
3315 if Is_Return_By_Reference_Type (Typ) then
3316 return True;
3318 elsif Nkind (Parent (N)) /= N_Return_Statement then
3319 return False;
3321 elsif Requires_Transient_Scope (Typ) then
3323 -- Verify that the return type of the enclosing function has the
3324 -- same constrained status as that of the expression.
3326 S := Current_Scope;
3327 while Ekind (S) /= E_Function loop
3328 S := Scope (S);
3329 end loop;
3331 return Is_Constrained (Typ) = Is_Constrained (Etype (S));
3332 else
3333 return False;
3334 end if;
3335 end Returned_By_Reference;
3337 ---------------------------
3338 -- Rhs_Of_Assign_Or_Decl --
3339 ---------------------------
3341 function Rhs_Of_Assign_Or_Decl (N : Node_Id) return Boolean is
3342 begin
3343 if (Nkind (Parent (N)) = N_Assignment_Statement
3344 and then Expression (Parent (N)) = N)
3345 or else
3346 (Nkind (Parent (N)) = N_Qualified_Expression
3347 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
3348 and then Expression (Parent (Parent (N))) = Parent (N))
3349 or else
3350 (Nkind (Parent (N)) = N_Object_Declaration
3351 and then Expression (Parent (N)) = N)
3352 or else
3353 (Nkind (Parent (N)) = N_Component_Association
3354 and then Expression (Parent (N)) = N
3355 and then Nkind (Parent (Parent (N))) = N_Aggregate
3356 and then Rhs_Of_Assign_Or_Decl (Parent (Parent (N))))
3357 or else
3358 (Nkind (Parent (N)) = N_Extension_Aggregate
3359 and then Is_Private_Type (Etype (Typ)))
3360 then
3361 return True;
3362 else
3363 return False;
3364 end if;
3365 end Rhs_Of_Assign_Or_Decl;
3367 -- Start of processing for Expand_N_Function_Call
3369 begin
3370 -- A special check. If stack checking is enabled, and the return type
3371 -- might generate a large temporary, and the call is not the right side
3372 -- of an assignment, then generate an explicit temporary. We do this
3373 -- because otherwise gigi may generate a large temporary on the fly and
3374 -- this can cause trouble with stack checking.
3376 -- This is unecessary if the call is the expression in an object
3377 -- declaration, or if it appears outside of any library unit. This can
3378 -- only happen if it appears as an actual in a library-level instance,
3379 -- in which case a temporary will be generated for it once the instance
3380 -- itself is installed.
3382 if May_Generate_Large_Temp (Typ)
3383 and then not Rhs_Of_Assign_Or_Decl (N)
3384 and then not Returned_By_Reference
3385 and then Current_Scope /= Standard_Standard
3386 then
3387 if Stack_Checking_Enabled then
3389 -- Note: it might be thought that it would be OK to use a call to
3390 -- Force_Evaluation here, but that's not good enough, because
3391 -- that can results in a 'Reference construct that may still need
3392 -- a temporary.
3394 declare
3395 Loc : constant Source_Ptr := Sloc (N);
3396 Temp_Obj : constant Entity_Id :=
3397 Make_Defining_Identifier (Loc,
3398 Chars => New_Internal_Name ('F'));
3399 Temp_Typ : Entity_Id := Typ;
3400 Decl : Node_Id;
3401 A : Node_Id;
3402 F : Entity_Id;
3403 Proc : Entity_Id;
3405 begin
3406 if Is_Tagged_Type (Typ)
3407 and then Present (Controlling_Argument (N))
3408 then
3409 if Nkind (Parent (N)) /= N_Procedure_Call_Statement
3410 and then Nkind (Parent (N)) /= N_Function_Call
3411 then
3412 -- If this is a tag-indeterminate call, the object must
3413 -- be classwide.
3415 if Is_Tag_Indeterminate (N) then
3416 Temp_Typ := Class_Wide_Type (Typ);
3417 end if;
3419 else
3420 -- If this is a dispatching call that is itself the
3421 -- controlling argument of an enclosing call, the
3422 -- nominal subtype of the object that replaces it must
3423 -- be classwide, so that dispatching will take place
3424 -- properly. If it is not a controlling argument, the
3425 -- object is not classwide.
3427 Proc := Entity (Name (Parent (N)));
3429 F := First_Formal (Proc);
3430 A := First_Actual (Parent (N));
3431 while A /= N loop
3432 Next_Formal (F);
3433 Next_Actual (A);
3434 end loop;
3436 if Is_Controlling_Formal (F) then
3437 Temp_Typ := Class_Wide_Type (Typ);
3438 end if;
3439 end if;
3440 end if;
3442 Decl :=
3443 Make_Object_Declaration (Loc,
3444 Defining_Identifier => Temp_Obj,
3445 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3446 Constant_Present => True,
3447 Expression => Relocate_Node (N));
3448 Set_Assignment_OK (Decl);
3450 Insert_Actions (N, New_List (Decl));
3451 Rewrite (N, New_Occurrence_Of (Temp_Obj, Loc));
3452 end;
3454 else
3455 -- If stack-checking is not enabled, increment serial number
3456 -- for internal names, so that subsequent symbols are consistent
3457 -- with and without stack-checking.
3459 Synchronize_Serial_Number;
3461 -- Now we can expand the call with consistent symbol names
3463 Expand_Call (N);
3464 end if;
3466 -- Normal case, expand the call
3468 else
3469 Expand_Call (N);
3470 end if;
3471 end Expand_N_Function_Call;
3473 ---------------------------------------
3474 -- Expand_N_Procedure_Call_Statement --
3475 ---------------------------------------
3477 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
3478 begin
3479 Expand_Call (N);
3480 end Expand_N_Procedure_Call_Statement;
3482 ------------------------------
3483 -- Expand_N_Subprogram_Body --
3484 ------------------------------
3486 -- Add poll call if ATC polling is enabled, unless the body will be
3487 -- inlined by the back-end.
3489 -- Add return statement if last statement in body is not a return statement
3490 -- (this makes things easier on Gigi which does not want to have to handle
3491 -- a missing return).
3493 -- Add call to Activate_Tasks if body is a task activator
3495 -- Deal with possible detection of infinite recursion
3497 -- Eliminate body completely if convention stubbed
3499 -- Encode entity names within body, since we will not need to reference
3500 -- these entities any longer in the front end.
3502 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
3504 -- Reset Pure indication if any parameter has root type System.Address
3506 -- Wrap thread body
3508 procedure Expand_N_Subprogram_Body (N : Node_Id) is
3509 Loc : constant Source_Ptr := Sloc (N);
3510 H : constant Node_Id := Handled_Statement_Sequence (N);
3511 Body_Id : Entity_Id;
3512 Spec_Id : Entity_Id;
3513 Except_H : Node_Id;
3514 Scop : Entity_Id;
3515 Dec : Node_Id;
3516 Next_Op : Node_Id;
3517 L : List_Id;
3519 procedure Add_Return (S : List_Id);
3520 -- Append a return statement to the statement sequence S if the last
3521 -- statement is not already a return or a goto statement. Note that
3522 -- the latter test is not critical, it does not matter if we add a
3523 -- few extra returns, since they get eliminated anyway later on.
3525 procedure Expand_Thread_Body;
3526 -- Perform required expansion of a thread body
3528 ----------------
3529 -- Add_Return --
3530 ----------------
3532 procedure Add_Return (S : List_Id) is
3533 begin
3534 if not Is_Transfer (Last (S)) then
3536 -- The source location for the return is the end label
3537 -- of the procedure in all cases. This is a bit odd when
3538 -- there are exception handlers, but not much else we can do.
3540 Append_To (S, Make_Return_Statement (Sloc (End_Label (H))));
3541 end if;
3542 end Add_Return;
3544 ------------------------
3545 -- Expand_Thread_Body --
3546 ------------------------
3548 -- The required expansion of a thread body is as follows
3550 -- procedure <thread body procedure name> is
3552 -- _Secondary_Stack : aliased
3553 -- Storage_Elements.Storage_Array
3554 -- (1 .. Storage_Offset (Sec_Stack_Size));
3555 -- for _Secondary_Stack'Alignment use Standard'Maximum_Alignment;
3557 -- _Process_ATSD : aliased System.Threads.ATSD;
3559 -- begin
3560 -- System.Threads.Thread_Body_Enter;
3561 -- (_Secondary_Stack'Address,
3562 -- _Secondary_Stack'Length,
3563 -- _Process_ATSD'Address);
3565 -- declare
3566 -- <user declarations>
3567 -- begin
3568 -- <user statements>
3569 -- <user exception handlers>
3570 -- end;
3572 -- System.Threads.Thread_Body_Leave;
3574 -- exception
3575 -- when E : others =>
3576 -- System.Threads.Thread_Body_Exceptional_Exit (E);
3577 -- end;
3579 -- Note the exception handler is omitted if pragma Restriction
3580 -- No_Exception_Handlers is currently active.
3582 procedure Expand_Thread_Body is
3583 User_Decls : constant List_Id := Declarations (N);
3584 Sec_Stack_Len : Node_Id;
3586 TB_Pragma : constant Node_Id :=
3587 Get_Rep_Pragma (Spec_Id, Name_Thread_Body);
3589 Ent_SS : Entity_Id;
3590 Ent_ATSD : Entity_Id;
3591 Ent_EO : Entity_Id;
3593 Decl_SS : Node_Id;
3594 Decl_ATSD : Node_Id;
3596 Excep_Handlers : List_Id;
3598 begin
3599 New_Scope (Spec_Id);
3601 -- Get proper setting for secondary stack size
3603 if List_Length (Pragma_Argument_Associations (TB_Pragma)) = 2 then
3604 Sec_Stack_Len :=
3605 Expression (Last (Pragma_Argument_Associations (TB_Pragma)));
3606 else
3607 Sec_Stack_Len :=
3608 New_Occurrence_Of (RTE (RE_Default_Secondary_Stack_Size), Loc);
3609 end if;
3611 Sec_Stack_Len := Convert_To (RTE (RE_Storage_Offset), Sec_Stack_Len);
3613 -- Build and set declarations for the wrapped thread body
3615 Ent_SS := Make_Defining_Identifier (Loc, Name_uSecondary_Stack);
3616 Ent_ATSD := Make_Defining_Identifier (Loc, Name_uProcess_ATSD);
3618 Decl_SS :=
3619 Make_Object_Declaration (Loc,
3620 Defining_Identifier => Ent_SS,
3621 Aliased_Present => True,
3622 Object_Definition =>
3623 Make_Subtype_Indication (Loc,
3624 Subtype_Mark =>
3625 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
3626 Constraint =>
3627 Make_Index_Or_Discriminant_Constraint (Loc,
3628 Constraints => New_List (
3629 Make_Range (Loc,
3630 Low_Bound => Make_Integer_Literal (Loc, 1),
3631 High_Bound => Sec_Stack_Len)))));
3633 Decl_ATSD :=
3634 Make_Object_Declaration (Loc,
3635 Defining_Identifier => Ent_ATSD,
3636 Aliased_Present => True,
3637 Object_Definition => New_Occurrence_Of (RTE (RE_ATSD), Loc));
3639 Set_Declarations (N, New_List (Decl_SS, Decl_ATSD));
3640 Analyze (Decl_SS);
3641 Analyze (Decl_ATSD);
3642 Set_Alignment (Ent_SS, UI_From_Int (Maximum_Alignment));
3644 -- Create new exception handler
3646 if Restriction_Active (No_Exception_Handlers) then
3647 Excep_Handlers := No_List;
3649 else
3650 Check_Restriction (No_Exception_Handlers, N);
3652 Ent_EO := Make_Defining_Identifier (Loc, Name_uE);
3654 Excep_Handlers := New_List (
3655 Make_Exception_Handler (Loc,
3656 Choice_Parameter => Ent_EO,
3657 Exception_Choices => New_List (
3658 Make_Others_Choice (Loc)),
3659 Statements => New_List (
3660 Make_Procedure_Call_Statement (Loc,
3661 Name =>
3662 New_Occurrence_Of
3663 (RTE (RE_Thread_Body_Exceptional_Exit), Loc),
3664 Parameter_Associations => New_List (
3665 New_Occurrence_Of (Ent_EO, Loc))))));
3666 end if;
3668 -- Now build new handled statement sequence and analyze it
3670 Set_Handled_Statement_Sequence (N,
3671 Make_Handled_Sequence_Of_Statements (Loc,
3672 Statements => New_List (
3674 Make_Procedure_Call_Statement (Loc,
3675 Name => New_Occurrence_Of (RTE (RE_Thread_Body_Enter), Loc),
3676 Parameter_Associations => New_List (
3678 Make_Attribute_Reference (Loc,
3679 Prefix => New_Occurrence_Of (Ent_SS, Loc),
3680 Attribute_Name => Name_Address),
3682 Make_Attribute_Reference (Loc,
3683 Prefix => New_Occurrence_Of (Ent_SS, Loc),
3684 Attribute_Name => Name_Length),
3686 Make_Attribute_Reference (Loc,
3687 Prefix => New_Occurrence_Of (Ent_ATSD, Loc),
3688 Attribute_Name => Name_Address))),
3690 Make_Block_Statement (Loc,
3691 Declarations => User_Decls,
3692 Handled_Statement_Sequence => H),
3694 Make_Procedure_Call_Statement (Loc,
3695 Name => New_Occurrence_Of (RTE (RE_Thread_Body_Leave), Loc))),
3697 Exception_Handlers => Excep_Handlers));
3699 Analyze (Handled_Statement_Sequence (N));
3700 End_Scope;
3701 end Expand_Thread_Body;
3703 -- Start of processing for Expand_N_Subprogram_Body
3705 begin
3706 -- Set L to either the list of declarations if present, or
3707 -- to the list of statements if no declarations are present.
3708 -- This is used to insert new stuff at the start.
3710 if Is_Non_Empty_List (Declarations (N)) then
3711 L := Declarations (N);
3712 else
3713 L := Statements (Handled_Statement_Sequence (N));
3714 end if;
3716 -- Find entity for subprogram
3718 Body_Id := Defining_Entity (N);
3720 if Present (Corresponding_Spec (N)) then
3721 Spec_Id := Corresponding_Spec (N);
3722 else
3723 Spec_Id := Body_Id;
3724 end if;
3726 -- Need poll on entry to subprogram if polling enabled. We only
3727 -- do this for non-empty subprograms, since it does not seem
3728 -- necessary to poll for a dummy null subprogram. Do not add polling
3729 -- point if calls to this subprogram will be inlined by the back-end,
3730 -- to avoid repeated polling points in nested inlinings.
3732 if Is_Non_Empty_List (L) then
3733 if Is_Inlined (Spec_Id)
3734 and then Front_End_Inlining
3735 and then Optimization_Level > 1
3736 then
3737 null;
3738 else
3739 Generate_Poll_Call (First (L));
3740 end if;
3741 end if;
3743 -- If this is a Pure function which has any parameters whose root
3744 -- type is System.Address, reset the Pure indication, since it will
3745 -- likely cause incorrect code to be generated as the parameter is
3746 -- probably a pointer, and the fact that the same pointer is passed
3747 -- does not mean that the same value is being referenced.
3749 -- Note that if the programmer gave an explicit Pure_Function pragma,
3750 -- then we believe the programmer, and leave the subprogram Pure.
3752 -- This code should probably be at the freeze point, so that it
3753 -- happens even on a -gnatc (or more importantly -gnatt) compile
3754 -- so that the semantic tree has Is_Pure set properly ???
3756 if Is_Pure (Spec_Id)
3757 and then Is_Subprogram (Spec_Id)
3758 and then not Has_Pragma_Pure_Function (Spec_Id)
3759 then
3760 declare
3761 F : Entity_Id;
3763 begin
3764 F := First_Formal (Spec_Id);
3765 while Present (F) loop
3766 if Is_Descendent_Of_Address (Etype (F)) then
3767 Set_Is_Pure (Spec_Id, False);
3769 if Spec_Id /= Body_Id then
3770 Set_Is_Pure (Body_Id, False);
3771 end if;
3773 exit;
3774 end if;
3776 Next_Formal (F);
3777 end loop;
3778 end;
3779 end if;
3781 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
3783 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
3784 declare
3785 F : Entity_Id;
3786 V : constant Boolean := Validity_Checks_On;
3788 begin
3789 -- We turn off validity checking, since we do not want any
3790 -- check on the initializing value itself (which we know
3791 -- may well be invalid!)
3793 Validity_Checks_On := False;
3795 -- Loop through formals
3797 F := First_Formal (Spec_Id);
3798 while Present (F) loop
3799 if Is_Scalar_Type (Etype (F))
3800 and then Ekind (F) = E_Out_Parameter
3801 then
3802 Insert_Before_And_Analyze (First (L),
3803 Make_Assignment_Statement (Loc,
3804 Name => New_Occurrence_Of (F, Loc),
3805 Expression => Get_Simple_Init_Val (Etype (F), Loc)));
3806 end if;
3808 Next_Formal (F);
3809 end loop;
3811 Validity_Checks_On := V;
3812 end;
3813 end if;
3815 Scop := Scope (Spec_Id);
3817 -- Add discriminal renamings to protected subprograms. Install new
3818 -- discriminals for expansion of the next subprogram of this protected
3819 -- type, if any.
3821 if Is_List_Member (N)
3822 and then Present (Parent (List_Containing (N)))
3823 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
3824 then
3825 Add_Discriminal_Declarations
3826 (Declarations (N), Scop, Name_uObject, Loc);
3827 Add_Private_Declarations (Declarations (N), Scop, Name_uObject, Loc);
3829 -- Associate privals and discriminals with the next protected
3830 -- operation body to be expanded. These are used to expand references
3831 -- to private data objects and discriminants, respectively.
3833 Next_Op := Next_Protected_Operation (N);
3835 if Present (Next_Op) then
3836 Dec := Parent (Base_Type (Scop));
3837 Set_Privals (Dec, Next_Op, Loc);
3838 Set_Discriminals (Dec);
3839 end if;
3840 end if;
3842 -- Clear out statement list for stubbed procedure
3844 if Present (Corresponding_Spec (N)) then
3845 Set_Elaboration_Flag (N, Spec_Id);
3847 if Convention (Spec_Id) = Convention_Stubbed
3848 or else Is_Eliminated (Spec_Id)
3849 then
3850 Set_Declarations (N, Empty_List);
3851 Set_Handled_Statement_Sequence (N,
3852 Make_Handled_Sequence_Of_Statements (Loc,
3853 Statements => New_List (
3854 Make_Null_Statement (Loc))));
3855 return;
3856 end if;
3857 end if;
3859 -- Returns_By_Ref flag is normally set when the subprogram is frozen
3860 -- but subprograms with no specs are not frozen.
3862 declare
3863 Typ : constant Entity_Id := Etype (Spec_Id);
3864 Utyp : constant Entity_Id := Underlying_Type (Typ);
3866 begin
3867 if not Acts_As_Spec (N)
3868 and then Nkind (Parent (Parent (Spec_Id))) /=
3869 N_Subprogram_Body_Stub
3870 then
3871 null;
3873 elsif Is_Return_By_Reference_Type (Typ) then
3874 Set_Returns_By_Ref (Spec_Id);
3876 elsif Present (Utyp) and then Controlled_Type (Utyp) then
3877 Set_Returns_By_Ref (Spec_Id);
3878 end if;
3879 end;
3881 -- For a procedure, we add a return for all possible syntactic ends
3882 -- of the subprogram. Note that reanalysis is not necessary in this
3883 -- case since it would require a lot of work and accomplish nothing.
3885 if Ekind (Spec_Id) = E_Procedure
3886 or else Ekind (Spec_Id) = E_Generic_Procedure
3887 then
3888 Add_Return (Statements (H));
3890 if Present (Exception_Handlers (H)) then
3891 Except_H := First_Non_Pragma (Exception_Handlers (H));
3892 while Present (Except_H) loop
3893 Add_Return (Statements (Except_H));
3894 Next_Non_Pragma (Except_H);
3895 end loop;
3896 end if;
3898 -- For a function, we must deal with the case where there is at least
3899 -- one missing return. What we do is to wrap the entire body of the
3900 -- function in a block:
3902 -- begin
3903 -- ...
3904 -- end;
3906 -- becomes
3908 -- begin
3909 -- begin
3910 -- ...
3911 -- end;
3913 -- raise Program_Error;
3914 -- end;
3916 -- This approach is necessary because the raise must be signalled
3917 -- to the caller, not handled by any local handler (RM 6.4(11)).
3919 -- Note: we do not need to analyze the constructed sequence here,
3920 -- since it has no handler, and an attempt to analyze the handled
3921 -- statement sequence twice is risky in various ways (e.g. the
3922 -- issue of expanding cleanup actions twice).
3924 elsif Has_Missing_Return (Spec_Id) then
3925 declare
3926 Hloc : constant Source_Ptr := Sloc (H);
3927 Blok : constant Node_Id :=
3928 Make_Block_Statement (Hloc,
3929 Handled_Statement_Sequence => H);
3930 Rais : constant Node_Id :=
3931 Make_Raise_Program_Error (Hloc,
3932 Reason => PE_Missing_Return);
3934 begin
3935 Set_Handled_Statement_Sequence (N,
3936 Make_Handled_Sequence_Of_Statements (Hloc,
3937 Statements => New_List (Blok, Rais)));
3939 New_Scope (Spec_Id);
3940 Analyze (Blok);
3941 Analyze (Rais);
3942 Pop_Scope;
3943 end;
3944 end if;
3946 -- If subprogram contains a parameterless recursive call, then we may
3947 -- have an infinite recursion, so see if we can generate code to check
3948 -- for this possibility if storage checks are not suppressed.
3950 if Ekind (Spec_Id) = E_Procedure
3951 and then Has_Recursive_Call (Spec_Id)
3952 and then not Storage_Checks_Suppressed (Spec_Id)
3953 then
3954 Detect_Infinite_Recursion (N, Spec_Id);
3955 end if;
3957 -- Finally, if we are in Normalize_Scalars mode, then any scalar out
3958 -- parameters must be initialized to the appropriate default value.
3960 if Ekind (Spec_Id) = E_Procedure and then Normalize_Scalars then
3961 declare
3962 Floc : Source_Ptr;
3963 Formal : Entity_Id;
3964 Stm : Node_Id;
3966 begin
3967 Formal := First_Formal (Spec_Id);
3968 while Present (Formal) loop
3969 Floc := Sloc (Formal);
3971 if Ekind (Formal) = E_Out_Parameter
3972 and then Is_Scalar_Type (Etype (Formal))
3973 then
3974 Stm :=
3975 Make_Assignment_Statement (Floc,
3976 Name => New_Occurrence_Of (Formal, Floc),
3977 Expression =>
3978 Get_Simple_Init_Val (Etype (Formal), Floc));
3979 Prepend (Stm, Declarations (N));
3980 Analyze (Stm);
3981 end if;
3983 Next_Formal (Formal);
3984 end loop;
3985 end;
3986 end if;
3988 -- Deal with thread body
3990 if Is_Thread_Body (Spec_Id) then
3991 Expand_Thread_Body;
3992 end if;
3994 -- Set to encode entity names in package body before gigi is called
3996 Qualify_Entity_Names (N);
3997 end Expand_N_Subprogram_Body;
3999 -----------------------------------
4000 -- Expand_N_Subprogram_Body_Stub --
4001 -----------------------------------
4003 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
4004 begin
4005 if Present (Corresponding_Body (N)) then
4006 Expand_N_Subprogram_Body (
4007 Unit_Declaration_Node (Corresponding_Body (N)));
4008 end if;
4009 end Expand_N_Subprogram_Body_Stub;
4011 -------------------------------------
4012 -- Expand_N_Subprogram_Declaration --
4013 -------------------------------------
4015 -- If the declaration appears within a protected body, it is a private
4016 -- operation of the protected type. We must create the corresponding
4017 -- protected subprogram an associated formals. For a normal protected
4018 -- operation, this is done when expanding the protected type declaration.
4020 -- If the declaration is for a null procedure, emit null body
4022 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
4023 Loc : constant Source_Ptr := Sloc (N);
4024 Subp : constant Entity_Id := Defining_Entity (N);
4025 Scop : constant Entity_Id := Scope (Subp);
4026 Prot_Decl : Node_Id;
4027 Prot_Bod : Node_Id;
4028 Prot_Id : Entity_Id;
4030 begin
4031 -- Deal with case of protected subprogram. Do not generate protected
4032 -- operation if operation is flagged as eliminated.
4034 if Is_List_Member (N)
4035 and then Present (Parent (List_Containing (N)))
4036 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
4037 and then Is_Protected_Type (Scop)
4038 then
4039 if No (Protected_Body_Subprogram (Subp))
4040 and then not Is_Eliminated (Subp)
4041 then
4042 Prot_Decl :=
4043 Make_Subprogram_Declaration (Loc,
4044 Specification =>
4045 Build_Protected_Sub_Specification
4046 (N, Scop, Unprotected_Mode));
4048 -- The protected subprogram is declared outside of the protected
4049 -- body. Given that the body has frozen all entities so far, we
4050 -- analyze the subprogram and perform freezing actions explicitly.
4051 -- If the body is a subunit, the insertion point is before the
4052 -- stub in the parent.
4054 Prot_Bod := Parent (List_Containing (N));
4056 if Nkind (Parent (Prot_Bod)) = N_Subunit then
4057 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
4058 end if;
4060 Insert_Before (Prot_Bod, Prot_Decl);
4061 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
4063 New_Scope (Scope (Scop));
4064 Analyze (Prot_Decl);
4065 Create_Extra_Formals (Prot_Id);
4066 Set_Protected_Body_Subprogram (Subp, Prot_Id);
4067 Pop_Scope;
4068 end if;
4070 elsif Nkind (Specification (N)) = N_Procedure_Specification
4071 and then Null_Present (Specification (N))
4072 then
4073 declare
4074 Bod : constant Node_Id :=
4075 Make_Subprogram_Body (Loc,
4076 Specification =>
4077 New_Copy_Tree (Specification (N)),
4078 Declarations => New_List,
4079 Handled_Statement_Sequence =>
4080 Make_Handled_Sequence_Of_Statements (Loc,
4081 Statements => New_List (Make_Null_Statement (Loc))));
4082 begin
4083 Set_Body_To_Inline (N, Bod);
4084 Insert_After (N, Bod);
4085 Analyze (Bod);
4087 -- Corresponding_Spec isn't being set by Analyze_Subprogram_Body,
4088 -- evidently because Set_Has_Completion is called earlier for null
4089 -- procedures in Analyze_Subprogram_Declaration, so we force its
4090 -- setting here. If the setting of Has_Completion is not set
4091 -- earlier, then it can result in missing body errors if other
4092 -- errors were already reported (since expansion is turned off).
4094 -- Should creation of the empty body be moved to the analyzer???
4096 Set_Corresponding_Spec (Bod, Defining_Entity (Specification (N)));
4097 end;
4098 end if;
4099 end Expand_N_Subprogram_Declaration;
4101 ---------------------------------------
4102 -- Expand_Protected_Object_Reference --
4103 ---------------------------------------
4105 function Expand_Protected_Object_Reference
4106 (N : Node_Id;
4107 Scop : Entity_Id)
4108 return Node_Id
4110 Loc : constant Source_Ptr := Sloc (N);
4111 Corr : Entity_Id;
4112 Rec : Node_Id;
4113 Param : Entity_Id;
4114 Proc : Entity_Id;
4116 begin
4117 Rec := Make_Identifier (Loc, Name_uObject);
4118 Set_Etype (Rec, Corresponding_Record_Type (Scop));
4120 -- Find enclosing protected operation, and retrieve its first parameter,
4121 -- which denotes the enclosing protected object. If the enclosing
4122 -- operation is an entry, we are immediately within the protected body,
4123 -- and we can retrieve the object from the service entries procedure. A
4124 -- barrier function has has the same signature as an entry. A barrier
4125 -- function is compiled within the protected object, but unlike
4126 -- protected operations its never needs locks, so that its protected
4127 -- body subprogram points to itself.
4129 Proc := Current_Scope;
4130 while Present (Proc)
4131 and then Scope (Proc) /= Scop
4132 loop
4133 Proc := Scope (Proc);
4134 end loop;
4136 Corr := Protected_Body_Subprogram (Proc);
4138 if No (Corr) then
4140 -- Previous error left expansion incomplete.
4141 -- Nothing to do on this call.
4143 return Empty;
4144 end if;
4146 Param :=
4147 Defining_Identifier
4148 (First (Parameter_Specifications (Parent (Corr))));
4150 if Is_Subprogram (Proc)
4151 and then Proc /= Corr
4152 then
4153 -- Protected function or procedure
4155 Set_Entity (Rec, Param);
4157 -- Rec is a reference to an entity which will not be in scope when
4158 -- the call is reanalyzed, and needs no further analysis.
4160 Set_Analyzed (Rec);
4162 else
4163 -- Entry or barrier function for entry body. The first parameter of
4164 -- the entry body procedure is pointer to the object. We create a
4165 -- local variable of the proper type, duplicating what is done to
4166 -- define _object later on.
4168 declare
4169 Decls : List_Id;
4170 Obj_Ptr : constant Entity_Id := Make_Defining_Identifier (Loc,
4171 Chars =>
4172 New_Internal_Name ('T'));
4174 begin
4175 Decls := New_List (
4176 Make_Full_Type_Declaration (Loc,
4177 Defining_Identifier => Obj_Ptr,
4178 Type_Definition =>
4179 Make_Access_To_Object_Definition (Loc,
4180 Subtype_Indication =>
4181 New_Reference_To
4182 (Corresponding_Record_Type (Scop), Loc))));
4184 Insert_Actions (N, Decls);
4185 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
4187 Rec :=
4188 Make_Explicit_Dereference (Loc,
4189 Unchecked_Convert_To (Obj_Ptr,
4190 New_Occurrence_Of (Param, Loc)));
4192 -- Analyze new actual. Other actuals in calls are already analyzed
4193 -- and the list of actuals is not renalyzed after rewriting.
4195 Set_Parent (Rec, N);
4196 Analyze (Rec);
4197 end;
4198 end if;
4200 return Rec;
4201 end Expand_Protected_Object_Reference;
4203 --------------------------------------
4204 -- Expand_Protected_Subprogram_Call --
4205 --------------------------------------
4207 procedure Expand_Protected_Subprogram_Call
4208 (N : Node_Id;
4209 Subp : Entity_Id;
4210 Scop : Entity_Id)
4212 Rec : Node_Id;
4214 begin
4215 -- If the protected object is not an enclosing scope, this is
4216 -- an inter-object function call. Inter-object procedure
4217 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
4218 -- The call is intra-object only if the subprogram being
4219 -- called is in the protected body being compiled, and if the
4220 -- protected object in the call is statically the enclosing type.
4221 -- The object may be an component of some other data structure,
4222 -- in which case this must be handled as an inter-object call.
4224 if not In_Open_Scopes (Scop)
4225 or else not Is_Entity_Name (Name (N))
4226 then
4227 if Nkind (Name (N)) = N_Selected_Component then
4228 Rec := Prefix (Name (N));
4230 else
4231 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
4232 Rec := Prefix (Prefix (Name (N)));
4233 end if;
4235 Build_Protected_Subprogram_Call (N,
4236 Name => New_Occurrence_Of (Subp, Sloc (N)),
4237 Rec => Convert_Concurrent (Rec, Etype (Rec)),
4238 External => True);
4240 else
4241 Rec := Expand_Protected_Object_Reference (N, Scop);
4243 if No (Rec) then
4244 return;
4245 end if;
4247 Build_Protected_Subprogram_Call (N,
4248 Name => Name (N),
4249 Rec => Rec,
4250 External => False);
4252 end if;
4254 Analyze (N);
4256 -- If it is a function call it can appear in elaboration code and
4257 -- the called entity must be frozen here.
4259 if Ekind (Subp) = E_Function then
4260 Freeze_Expression (Name (N));
4261 end if;
4262 end Expand_Protected_Subprogram_Call;
4264 -----------------------
4265 -- Freeze_Subprogram --
4266 -----------------------
4268 procedure Freeze_Subprogram (N : Node_Id) is
4269 Loc : constant Source_Ptr := Sloc (N);
4270 E : constant Entity_Id := Entity (N);
4272 procedure Check_Overriding_Inherited_Interfaces (E : Entity_Id);
4273 -- (Ada 2005): Check if the primitive E covers some interface already
4274 -- implemented by some ancestor of the tagged-type associated with E.
4276 procedure Register_Interface_DT_Entry
4277 (Prim : Entity_Id;
4278 Ancestor_Iface_Prim : Entity_Id := Empty);
4279 -- (Ada 2005): Register an interface primitive in a secondary dispatch
4280 -- table. If Prim overrides an ancestor primitive of its associated
4281 -- tagged-type then Ancestor_Iface_Prim indicates the entity of that
4282 -- immediate ancestor associated with the interface.
4284 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
4285 -- (Ada 2005): Register a predefined primitive in all the secondary
4286 -- dispatch tables of its primitive type.
4288 -------------------------------------------
4289 -- Check_Overriding_Inherited_Interfaces --
4290 -------------------------------------------
4292 procedure Check_Overriding_Inherited_Interfaces (E : Entity_Id) is
4293 Typ : Entity_Id;
4294 Elmt : Elmt_Id;
4295 Prim_Op : Entity_Id;
4296 Overriden_Op : Entity_Id := Empty;
4298 begin
4299 if Ada_Version < Ada_05
4300 or else not Is_Overriding_Operation (E)
4301 or else Is_Predefined_Dispatching_Operation (E)
4302 or else Present (Alias (E))
4303 then
4304 return;
4305 end if;
4307 -- Get the entity associated with this primitive operation
4309 Typ := Scope (DTC_Entity (E));
4310 loop
4311 exit when Etype (Typ) = Typ
4312 or else (Present (Full_View (Etype (Typ)))
4313 and then Full_View (Etype (Typ)) = Typ);
4315 -- Climb to the immediate ancestor handling private types
4317 if Present (Full_View (Etype (Typ))) then
4318 Typ := Full_View (Etype (Typ));
4319 else
4320 Typ := Etype (Typ);
4321 end if;
4323 if Present (Abstract_Interfaces (Typ)) then
4325 -- Look for the overriden subprogram in the primary dispatch
4326 -- table of the ancestor.
4328 Overriden_Op := Empty;
4329 Elmt := First_Elmt (Primitive_Operations (Typ));
4330 while Present (Elmt) loop
4331 Prim_Op := Node (Elmt);
4333 if Chars (Prim_Op) = Chars (E)
4334 and then Type_Conformant
4335 (New_Id => Prim_Op,
4336 Old_Id => E,
4337 Skip_Controlling_Formals => True)
4338 and then DT_Position (Prim_Op) = DT_Position (E)
4339 and then Etype (DTC_Entity (Prim_Op)) = RTE (RE_Tag)
4340 and then No (Abstract_Interface_Alias (Prim_Op))
4341 then
4342 if Overriden_Op = Empty then
4343 Overriden_Op := Prim_Op;
4345 -- Additional check to ensure that if two candidates have
4346 -- been found then they refer to the same subprogram.
4348 else
4349 declare
4350 A1 : Entity_Id;
4351 A2 : Entity_Id;
4353 begin
4354 A1 := Overriden_Op;
4355 while Present (Alias (A1)) loop
4356 A1 := Alias (A1);
4357 end loop;
4359 A2 := Prim_Op;
4360 while Present (Alias (A2)) loop
4361 A2 := Alias (A2);
4362 end loop;
4364 if A1 /= A2 then
4365 raise Program_Error;
4366 end if;
4367 end;
4368 end if;
4369 end if;
4371 Next_Elmt (Elmt);
4372 end loop;
4374 -- If not found this is the first overriding of some abstract
4375 -- interface.
4377 if Overriden_Op /= Empty then
4379 -- Find the entries associated with interfaces that are
4380 -- alias of this primitive operation in the ancestor.
4382 Elmt := First_Elmt (Primitive_Operations (Typ));
4383 while Present (Elmt) loop
4384 Prim_Op := Node (Elmt);
4386 if Present (Abstract_Interface_Alias (Prim_Op))
4387 and then Alias (Prim_Op) = Overriden_Op
4388 then
4389 Register_Interface_DT_Entry (E, Prim_Op);
4390 end if;
4392 Next_Elmt (Elmt);
4393 end loop;
4394 end if;
4395 end if;
4396 end loop;
4397 end Check_Overriding_Inherited_Interfaces;
4399 ---------------------------------
4400 -- Register_Interface_DT_Entry --
4401 ---------------------------------
4403 procedure Register_Interface_DT_Entry
4404 (Prim : Entity_Id;
4405 Ancestor_Iface_Prim : Entity_Id := Empty)
4407 Prim_Typ : Entity_Id;
4408 Prim_Op : Entity_Id;
4409 Iface_Typ : Entity_Id;
4410 Iface_DT_Ptr : Entity_Id;
4411 Iface_Tag : Entity_Id;
4412 New_Thunk : Node_Id;
4413 Thunk_Id : Entity_Id;
4415 begin
4416 -- Nothing to do if the run-time does not give support to abstract
4417 -- interfaces.
4419 if not (RTE_Available (RE_Interface_Tag)) then
4420 return;
4421 end if;
4423 if No (Ancestor_Iface_Prim) then
4424 Prim_Typ := Scope (DTC_Entity (Alias (Prim)));
4425 Iface_Typ := Scope (DTC_Entity (Abstract_Interface_Alias (Prim)));
4427 -- Generate the code of the thunk only when this primitive
4428 -- operation is associated with a secondary dispatch table.
4430 if Is_Interface (Iface_Typ) then
4431 Iface_Tag := Find_Interface_Tag
4432 (T => Prim_Typ,
4433 Iface => Iface_Typ);
4435 if Etype (Iface_Tag) = RTE (RE_Interface_Tag) then
4436 Thunk_Id :=
4437 Make_Defining_Identifier (Loc,
4438 Chars => New_Internal_Name ('T'));
4440 New_Thunk :=
4441 Expand_Interface_Thunk
4442 (N => Prim,
4443 Thunk_Alias => Alias (Prim),
4444 Thunk_Id => Thunk_Id);
4446 Insert_After (N, New_Thunk);
4448 Iface_DT_Ptr :=
4449 Find_Interface_ADT
4450 (T => Prim_Typ,
4451 Iface => Iface_Typ);
4453 Insert_After (New_Thunk,
4454 Fill_Secondary_DT_Entry (Sloc (Prim),
4455 Prim => Prim,
4456 Iface_DT_Ptr => Iface_DT_Ptr,
4457 Thunk_Id => Thunk_Id));
4458 end if;
4459 end if;
4461 else
4462 Iface_Typ :=
4463 Scope (DTC_Entity (Abstract_Interface_Alias
4464 (Ancestor_Iface_Prim)));
4466 Iface_Tag :=
4467 Find_Interface_Tag
4468 (T => Scope (DTC_Entity (Alias (Ancestor_Iface_Prim))),
4469 Iface => Iface_Typ);
4471 -- Generate the thunk only if the associated tag is an interface
4472 -- tag. The case in which the associated tag is the primary tag
4473 -- occurs when a tagged type is a direct derivation of an
4474 -- interface. For example:
4476 -- type I is interface;
4477 -- ...
4478 -- type T is new I with ...
4480 if Etype (Iface_Tag) = RTE (RE_Interface_Tag) then
4481 Thunk_Id :=
4482 Make_Defining_Identifier (Loc,
4483 Chars => New_Internal_Name ('T'));
4485 if Present (Alias (Prim)) then
4486 Prim_Op := Alias (Prim);
4487 else
4488 Prim_Op := Prim;
4489 end if;
4491 New_Thunk :=
4492 Expand_Interface_Thunk
4493 (N => Ancestor_Iface_Prim,
4494 Thunk_Alias => Prim_Op,
4495 Thunk_Id => Thunk_Id);
4497 Insert_After (N, New_Thunk);
4499 Iface_DT_Ptr :=
4500 Find_Interface_ADT
4501 (T => Scope (DTC_Entity (Prim_Op)),
4502 Iface => Iface_Typ);
4504 Insert_After (New_Thunk,
4505 Fill_Secondary_DT_Entry (Sloc (Prim),
4506 Prim => Ancestor_Iface_Prim,
4507 Iface_DT_Ptr => Iface_DT_Ptr,
4508 Thunk_Id => Thunk_Id));
4509 end if;
4510 end if;
4511 end Register_Interface_DT_Entry;
4513 ----------------------------------
4514 -- Register_Predefined_DT_Entry --
4515 ----------------------------------
4517 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
4518 Iface_DT_Ptr : Elmt_Id;
4519 Iface_Tag : Entity_Id;
4520 Iface_Typ : Elmt_Id;
4521 New_Thunk : Entity_Id;
4522 Prim_Typ : Entity_Id;
4523 Thunk_Id : Entity_Id;
4525 begin
4526 Prim_Typ := Scope (DTC_Entity (Prim));
4528 if No (Access_Disp_Table (Prim_Typ))
4529 or else No (Abstract_Interfaces (Prim_Typ))
4530 or else not RTE_Available (RE_Interface_Tag)
4531 then
4532 return;
4533 end if;
4535 -- Skip the first acces-to-dispatch-table pointer since it leads
4536 -- to the primary dispatch table. We are only concerned with the
4537 -- secondary dispatch table pointers. Note that the access-to-
4538 -- dispatch-table pointer corresponds to the first implemented
4539 -- interface retrieved below.
4541 Iface_DT_Ptr := Next_Elmt (First_Elmt (Access_Disp_Table (Prim_Typ)));
4542 Iface_Typ := First_Elmt (Abstract_Interfaces (Prim_Typ));
4543 while Present (Iface_DT_Ptr) and then Present (Iface_Typ) loop
4544 Iface_Tag := Find_Interface_Tag (Prim_Typ, Node (Iface_Typ));
4545 pragma Assert (Present (Iface_Tag));
4547 if Etype (Iface_Tag) = RTE (RE_Interface_Tag) then
4548 Thunk_Id := Make_Defining_Identifier (Loc,
4549 New_Internal_Name ('T'));
4551 New_Thunk :=
4552 Expand_Interface_Thunk
4553 (N => Prim,
4554 Thunk_Alias => Prim,
4555 Thunk_Id => Thunk_Id);
4557 Insert_After (N, New_Thunk);
4558 Insert_After (New_Thunk,
4559 Make_DT_Access_Action (Node (Iface_Typ),
4560 Action => Set_Predefined_Prim_Op_Address,
4561 Args => New_List (
4562 Unchecked_Convert_To (RTE (RE_Tag),
4563 New_Reference_To (Node (Iface_DT_Ptr), Loc)),
4565 Make_Integer_Literal (Loc, DT_Position (Prim)),
4567 Make_Attribute_Reference (Loc,
4568 Prefix => New_Reference_To (Thunk_Id, Loc),
4569 Attribute_Name => Name_Address))));
4570 end if;
4572 Next_Elmt (Iface_DT_Ptr);
4573 Next_Elmt (Iface_Typ);
4574 end loop;
4575 end Register_Predefined_DT_Entry;
4577 -- Start of processing for Freeze_Subprogram
4579 begin
4580 -- When a primitive is frozen, enter its name in the corresponding
4581 -- dispatch table. If the DTC_Entity field is not set this is an
4582 -- overridden primitive that can be ignored. We suppress the
4583 -- initialization of the dispatch table entry when Java_VM because
4584 -- the dispatching mechanism is handled internally by the JVM.
4586 if Is_Dispatching_Operation (E)
4587 and then not Is_Abstract (E)
4588 and then Present (DTC_Entity (E))
4589 and then not Java_VM
4590 and then not Is_CPP_Class (Scope (DTC_Entity (E)))
4591 then
4592 Check_Overriding_Operation (E);
4594 -- Ada 95 case: Register the subprogram in the primary dispatch table
4596 if Ada_Version < Ada_05 then
4598 -- Do not register the subprogram in the dispatch table if we
4599 -- are compiling with the No_Dispatching_Calls restriction.
4601 if not Restriction_Active (No_Dispatching_Calls) then
4602 Insert_After (N,
4603 Fill_DT_Entry (Sloc (N), Prim => E));
4604 end if;
4606 -- Ada 2005 case: Register the subprogram in the secondary dispatch
4607 -- tables associated with abstract interfaces.
4609 else
4610 declare
4611 Typ : constant Entity_Id := Scope (DTC_Entity (E));
4613 begin
4614 -- There is no dispatch table associated with abstract
4615 -- interface types. Each type implementing interfaces will
4616 -- fill the associated secondary DT entries.
4618 if not Is_Interface (Typ)
4619 or else Present (Alias (E))
4620 then
4621 -- Ada 2005 (AI-251): Check if this entry corresponds with
4622 -- a subprogram that covers an abstract interface type.
4624 if Present (Abstract_Interface_Alias (E)) then
4625 Register_Interface_DT_Entry (E);
4627 -- Common case: Primitive subprogram
4629 else
4630 -- Generate thunks for all the predefined operations
4632 if not Restriction_Active (No_Dispatching_Calls) then
4633 if Is_Predefined_Dispatching_Operation (E) then
4634 Register_Predefined_DT_Entry (E);
4635 end if;
4637 Insert_After (N,
4638 Fill_DT_Entry (Sloc (N), Prim => E));
4639 end if;
4641 Check_Overriding_Inherited_Interfaces (E);
4642 end if;
4643 end if;
4644 end;
4645 end if;
4646 end if;
4648 -- Mark functions that return by reference. Note that it cannot be
4649 -- part of the normal semantic analysis of the spec since the
4650 -- underlying returned type may not be known yet (for private types).
4652 declare
4653 Typ : constant Entity_Id := Etype (E);
4654 Utyp : constant Entity_Id := Underlying_Type (Typ);
4656 begin
4657 if Is_Return_By_Reference_Type (Typ) then
4658 Set_Returns_By_Ref (E);
4660 elsif Present (Utyp) and then Controlled_Type (Utyp) then
4661 Set_Returns_By_Ref (E);
4662 end if;
4663 end;
4664 end Freeze_Subprogram;
4666 end Exp_Ch6;