* config/rs6000/aix61.h (TARGET_DEFAULT): Add MASK_PPC_GPOPT,
[official-gcc.git] / gcc / ada / exp_intr.adb
blobdcf6b52650578fc87c7520e81e41e9f7486c47ad
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ I N T R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Atag; use Exp_Atag;
32 with Exp_Ch4; use Exp_Ch4;
33 with Exp_Ch7; use Exp_Ch7;
34 with Exp_Ch11; use Exp_Ch11;
35 with Exp_Code; use Exp_Code;
36 with Exp_Fixd; use Exp_Fixd;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Namet; use Namet;
40 with Nmake; use Nmake;
41 with Nlists; use Nlists;
42 with Opt; use Opt;
43 with Restrict; use Restrict;
44 with Rident; use Rident;
45 with Rtsfind; use Rtsfind;
46 with Sem; use Sem;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Eval; use Sem_Eval;
49 with Sem_Res; use Sem_Res;
50 with Sem_Type; use Sem_Type;
51 with Sem_Util; use Sem_Util;
52 with Sinfo; use Sinfo;
53 with Sinput; use Sinput;
54 with Snames; use Snames;
55 with Stand; use Stand;
56 with Stringt; use Stringt;
57 with Targparm; use Targparm;
58 with Tbuild; use Tbuild;
59 with Uintp; use Uintp;
60 with Urealp; use Urealp;
62 package body Exp_Intr is
64 -----------------------
65 -- Local Subprograms --
66 -----------------------
68 procedure Expand_Binary_Operator_Call (N : Node_Id);
69 -- Expand a call to an intrinsic arithmetic operator when the operand
70 -- types or sizes are not identical.
72 procedure Expand_Is_Negative (N : Node_Id);
73 -- Expand a call to the intrinsic Is_Negative function
75 procedure Expand_Dispatching_Constructor_Call (N : Node_Id);
76 -- Expand a call to an instantiation of Generic_Dispatching_Constructor
77 -- into a dispatching call to the actual subprogram associated with the
78 -- Constructor formal subprogram, passing it the Parameters actual of
79 -- the call to the instantiation and dispatching based on call's Tag
80 -- parameter.
82 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
83 -- Expand a call to Exception_Information/Message/Name. The first
84 -- parameter, N, is the node for the function call, and Ent is the
85 -- entity for the corresponding routine in the Ada.Exceptions package.
87 procedure Expand_Import_Call (N : Node_Id);
88 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
89 -- N is the node for the function call.
91 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
92 -- Expand an intrinsic shift operation, N and E are from the call to
93 -- Expand_Intrinsic_Call (call node and subprogram spec entity) and
94 -- K is the kind for the shift node
96 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
97 -- Expand a call to an instantiation of Unchecked_Conversion into a node
98 -- N_Unchecked_Type_Conversion.
100 procedure Expand_Unc_Deallocation (N : Node_Id);
101 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
102 -- N_Free_Statement and appropriate context.
104 procedure Expand_To_Address (N : Node_Id);
105 procedure Expand_To_Pointer (N : Node_Id);
106 -- Expand a call to corresponding function, declared in an instance of
107 -- System.Address_To_Access_Conversions.
109 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
110 -- Rewrite the node by the appropriate string or positive constant.
111 -- Nam can be one of the following:
112 -- Name_File - expand string that is the name of source file
113 -- Name_Line - expand integer line number
114 -- Name_Source_Location - expand string of form file:line
115 -- Name_Enclosing_Entity - expand string with name of enclosing entity
117 ---------------------------------
118 -- Expand_Binary_Operator_Call --
119 ---------------------------------
121 procedure Expand_Binary_Operator_Call (N : Node_Id) is
122 T1 : constant Entity_Id := Underlying_Type (Etype (Left_Opnd (N)));
123 T2 : constant Entity_Id := Underlying_Type (Etype (Right_Opnd (N)));
124 TR : constant Entity_Id := Etype (N);
125 T3 : Entity_Id;
126 Res : Node_Id;
128 Siz : constant Uint := UI_Max (RM_Size (T1), RM_Size (T2));
129 -- Maximum of operand sizes
131 begin
132 -- Nothing to do if the operands have the same modular type
134 if Base_Type (T1) = Base_Type (T2)
135 and then Is_Modular_Integer_Type (T1)
136 then
137 return;
138 end if;
140 -- Use Unsigned_32 for sizes of 32 or below, else Unsigned_64
142 if Siz > 32 then
143 T3 := RTE (RE_Unsigned_64);
144 else
145 T3 := RTE (RE_Unsigned_32);
146 end if;
148 -- Copy operator node, and reset type and entity fields, for
149 -- subsequent reanalysis.
151 Res := New_Copy (N);
152 Set_Etype (Res, T3);
154 case Nkind (N) is
155 when N_Op_And =>
156 Set_Entity (Res, Standard_Op_And);
157 when N_Op_Or =>
158 Set_Entity (Res, Standard_Op_Or);
159 when N_Op_Xor =>
160 Set_Entity (Res, Standard_Op_Xor);
161 when others =>
162 raise Program_Error;
163 end case;
165 -- Convert operands to large enough intermediate type
167 Set_Left_Opnd (Res,
168 Unchecked_Convert_To (T3, Relocate_Node (Left_Opnd (N))));
169 Set_Right_Opnd (Res,
170 Unchecked_Convert_To (T3, Relocate_Node (Right_Opnd (N))));
172 -- Analyze and resolve result formed by conversion to target type
174 Rewrite (N, Unchecked_Convert_To (TR, Res));
175 Analyze_And_Resolve (N, TR);
176 end Expand_Binary_Operator_Call;
178 -----------------------------------------
179 -- Expand_Dispatching_Constructor_Call --
180 -----------------------------------------
182 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
183 -- of the form:
185 -- GDC_Instance (The_Tag, Parameters'Access)
187 -- to a class-wide conversion of a dispatching call to the actual
188 -- associated with the formal subprogram Construct, designating The_Tag
189 -- as the controlling tag of the call:
191 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
193 -- which will eventually be expanded to the following:
195 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
197 -- A class-wide membership test is also generated, preceding the call, to
198 -- ensure that the controlling tag denotes a type in T'Class.
200 procedure Expand_Dispatching_Constructor_Call (N : Node_Id) is
201 Loc : constant Source_Ptr := Sloc (N);
202 Tag_Arg : constant Node_Id := First_Actual (N);
203 Param_Arg : constant Node_Id := Next_Actual (Tag_Arg);
204 Subp_Decl : constant Node_Id := Parent (Parent (Entity (Name (N))));
205 Inst_Pkg : constant Node_Id := Parent (Subp_Decl);
206 Act_Rename : Node_Id;
207 Act_Constr : Entity_Id;
208 Iface_Tag : Node_Id := Empty;
209 Cnstr_Call : Node_Id;
210 Result_Typ : Entity_Id;
212 begin
213 -- The subprogram is the third actual in the instantiation, and is
214 -- retrieved from the corresponding renaming declaration. However,
215 -- freeze nodes may appear before, so we retrieve the declaration
216 -- with an explicit loop.
218 Act_Rename := First (Visible_Declarations (Inst_Pkg));
219 while Nkind (Act_Rename) /= N_Subprogram_Renaming_Declaration loop
220 Next (Act_Rename);
221 end loop;
223 Act_Constr := Entity (Name (Act_Rename));
224 Result_Typ := Class_Wide_Type (Etype (Act_Constr));
226 -- Ada 2005 (AI-251): If the result is an interface type, the function
227 -- returns a class-wide interface type (otherwise the resulting object
228 -- would be abstract!)
230 if Is_Interface (Etype (Act_Constr)) then
231 Set_Etype (Act_Constr, Result_Typ);
233 -- If the result type is not parent of Tag_Arg then we need to
234 -- locate the tag of the secondary dispatch table.
236 if not Is_Ancestor (Etype (Result_Typ), Etype (Tag_Arg),
237 Use_Full_View => True)
238 and then Tagged_Type_Expansion
239 then
240 -- Obtain the reference to the Ada.Tags service before generating
241 -- the Object_Declaration node to ensure that if this service is
242 -- not available in the runtime then we generate a clear error.
244 declare
245 Fname : constant Node_Id :=
246 New_Reference_To (RTE (RE_Secondary_Tag), Loc);
248 begin
249 pragma Assert (not Is_Interface (Etype (Tag_Arg)));
251 Iface_Tag :=
252 Make_Object_Declaration (Loc,
253 Defining_Identifier => Make_Temporary (Loc, 'V'),
254 Object_Definition =>
255 New_Reference_To (RTE (RE_Tag), Loc),
256 Expression =>
257 Make_Function_Call (Loc,
258 Name => Fname,
259 Parameter_Associations => New_List (
260 Relocate_Node (Tag_Arg),
261 New_Reference_To
262 (Node (First_Elmt (Access_Disp_Table
263 (Etype (Etype (Act_Constr))))),
264 Loc))));
265 Insert_Action (N, Iface_Tag);
266 end;
267 end if;
268 end if;
270 -- Create the call to the actual Constructor function
272 Cnstr_Call :=
273 Make_Function_Call (Loc,
274 Name => New_Occurrence_Of (Act_Constr, Loc),
275 Parameter_Associations => New_List (Relocate_Node (Param_Arg)));
277 -- Establish its controlling tag from the tag passed to the instance
278 -- The tag may be given by a function call, in which case a temporary
279 -- should be generated now, to prevent out-of-order insertions during
280 -- the expansion of that call when stack-checking is enabled.
282 if Present (Iface_Tag) then
283 Set_Controlling_Argument (Cnstr_Call,
284 New_Occurrence_Of (Defining_Identifier (Iface_Tag), Loc));
285 else
286 Remove_Side_Effects (Tag_Arg);
287 Set_Controlling_Argument (Cnstr_Call,
288 Relocate_Node (Tag_Arg));
289 end if;
291 -- Rewrite and analyze the call to the instance as a class-wide
292 -- conversion of the call to the actual constructor.
294 Rewrite (N, Convert_To (Result_Typ, Cnstr_Call));
295 Analyze_And_Resolve (N, Etype (Act_Constr));
297 -- Do not generate a run-time check on the built object if tag
298 -- checks are suppressed for the result type or VM_Target /= No_VM
300 if Tag_Checks_Suppressed (Etype (Result_Typ))
301 or else not Tagged_Type_Expansion
302 then
303 null;
305 -- Generate a class-wide membership test to ensure that the call's tag
306 -- argument denotes a type within the class. We must keep separate the
307 -- case in which the Result_Type of the constructor function is a tagged
308 -- type from the case in which it is an abstract interface because the
309 -- run-time subprogram required to check these cases differ (and have
310 -- one difference in their parameters profile).
312 -- Call CW_Membership if the Result_Type is a tagged type to look for
313 -- the tag in the table of ancestor tags.
315 elsif not Is_Interface (Result_Typ) then
316 declare
317 Obj_Tag_Node : Node_Id := Duplicate_Subexpr (Tag_Arg);
318 CW_Test_Node : Node_Id;
320 begin
321 Build_CW_Membership (Loc,
322 Obj_Tag_Node => Obj_Tag_Node,
323 Typ_Tag_Node =>
324 New_Reference_To (
325 Node (First_Elmt (Access_Disp_Table (
326 Root_Type (Result_Typ)))), Loc),
327 Related_Nod => N,
328 New_Node => CW_Test_Node);
330 Insert_Action (N,
331 Make_Implicit_If_Statement (N,
332 Condition =>
333 Make_Op_Not (Loc, CW_Test_Node),
334 Then_Statements =>
335 New_List (Make_Raise_Statement (Loc,
336 New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
337 end;
339 -- Call IW_Membership test if the Result_Type is an abstract interface
340 -- to look for the tag in the table of interface tags.
342 else
343 Insert_Action (N,
344 Make_Implicit_If_Statement (N,
345 Condition =>
346 Make_Op_Not (Loc,
347 Make_Function_Call (Loc,
348 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
349 Parameter_Associations => New_List (
350 Make_Attribute_Reference (Loc,
351 Prefix => Duplicate_Subexpr (Tag_Arg),
352 Attribute_Name => Name_Address),
354 New_Reference_To (
355 Node (First_Elmt (Access_Disp_Table (
356 Root_Type (Result_Typ)))), Loc)))),
357 Then_Statements =>
358 New_List (
359 Make_Raise_Statement (Loc,
360 Name => New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
361 end if;
362 end Expand_Dispatching_Constructor_Call;
364 ---------------------------
365 -- Expand_Exception_Call --
366 ---------------------------
368 -- If the function call is not within an exception handler, then the call
369 -- is replaced by a null string. Otherwise the appropriate routine in
370 -- Ada.Exceptions is called passing the choice parameter specification
371 -- from the enclosing handler. If the enclosing handler lacks a choice
372 -- parameter, then one is supplied.
374 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
375 Loc : constant Source_Ptr := Sloc (N);
376 P : Node_Id;
377 E : Entity_Id;
379 begin
380 -- Climb up parents to see if we are in exception handler
382 P := Parent (N);
383 loop
384 -- Case of not in exception handler, replace by null string
386 if No (P) then
387 Rewrite (N,
388 Make_String_Literal (Loc,
389 Strval => ""));
390 exit;
392 -- Case of in exception handler
394 elsif Nkind (P) = N_Exception_Handler then
396 -- Handler cannot be used for a local raise, and furthermore, this
397 -- is a violation of the No_Exception_Propagation restriction.
399 Set_Local_Raise_Not_OK (P);
400 Check_Restriction (No_Exception_Propagation, N);
402 -- If no choice parameter present, then put one there. Note that
403 -- we do not need to put it on the entity chain, since no one will
404 -- be referencing it by normal visibility methods.
406 if No (Choice_Parameter (P)) then
407 E := Make_Temporary (Loc, 'E');
408 Set_Choice_Parameter (P, E);
409 Set_Ekind (E, E_Variable);
410 Set_Etype (E, RTE (RE_Exception_Occurrence));
411 Set_Scope (E, Current_Scope);
412 end if;
414 Rewrite (N,
415 Make_Function_Call (Loc,
416 Name => New_Occurrence_Of (RTE (Ent), Loc),
417 Parameter_Associations => New_List (
418 New_Occurrence_Of (Choice_Parameter (P), Loc))));
419 exit;
421 -- Keep climbing!
423 else
424 P := Parent (P);
425 end if;
426 end loop;
428 Analyze_And_Resolve (N, Standard_String);
429 end Expand_Exception_Call;
431 ------------------------
432 -- Expand_Import_Call --
433 ------------------------
435 -- The function call must have a static string as its argument. We create
436 -- a dummy variable which uses this string as the external name in an
437 -- Import pragma. The result is then obtained as the address of this
438 -- dummy variable, converted to the appropriate target type.
440 procedure Expand_Import_Call (N : Node_Id) is
441 Loc : constant Source_Ptr := Sloc (N);
442 Ent : constant Entity_Id := Entity (Name (N));
443 Str : constant Node_Id := First_Actual (N);
444 Dum : constant Entity_Id := Make_Temporary (Loc, 'D');
446 begin
447 Insert_Actions (N, New_List (
448 Make_Object_Declaration (Loc,
449 Defining_Identifier => Dum,
450 Object_Definition =>
451 New_Occurrence_Of (Standard_Character, Loc)),
453 Make_Pragma (Loc,
454 Chars => Name_Import,
455 Pragma_Argument_Associations => New_List (
456 Make_Pragma_Argument_Association (Loc,
457 Expression => Make_Identifier (Loc, Name_Ada)),
459 Make_Pragma_Argument_Association (Loc,
460 Expression => Make_Identifier (Loc, Chars (Dum))),
462 Make_Pragma_Argument_Association (Loc,
463 Chars => Name_Link_Name,
464 Expression => Relocate_Node (Str))))));
466 Rewrite (N,
467 Unchecked_Convert_To (Etype (Ent),
468 Make_Attribute_Reference (Loc,
469 Prefix => Make_Identifier (Loc, Chars (Dum)),
470 Attribute_Name => Name_Address)));
472 Analyze_And_Resolve (N, Etype (Ent));
473 end Expand_Import_Call;
475 ---------------------------
476 -- Expand_Intrinsic_Call --
477 ---------------------------
479 procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
480 Nam : Name_Id;
482 begin
483 -- If an external name is specified for the intrinsic, it is handled
484 -- by the back-end: leave the call node unchanged for now.
486 if Present (Interface_Name (E)) then
487 return;
488 end if;
490 -- If the intrinsic subprogram is generic, gets its original name
492 if Present (Parent (E))
493 and then Present (Generic_Parent (Parent (E)))
494 then
495 Nam := Chars (Generic_Parent (Parent (E)));
496 else
497 Nam := Chars (E);
498 end if;
500 if Nam = Name_Asm then
501 Expand_Asm_Call (N);
503 elsif Nam = Name_Divide then
504 Expand_Decimal_Divide_Call (N);
506 elsif Nam = Name_Exception_Information then
507 Expand_Exception_Call (N, RE_Exception_Information);
509 elsif Nam = Name_Exception_Message then
510 Expand_Exception_Call (N, RE_Exception_Message);
512 elsif Nam = Name_Exception_Name then
513 Expand_Exception_Call (N, RE_Exception_Name_Simple);
515 elsif Nam = Name_Generic_Dispatching_Constructor then
516 Expand_Dispatching_Constructor_Call (N);
518 elsif Nam = Name_Import_Address
519 or else
520 Nam = Name_Import_Largest_Value
521 or else
522 Nam = Name_Import_Value
523 then
524 Expand_Import_Call (N);
526 elsif Nam = Name_Is_Negative then
527 Expand_Is_Negative (N);
529 elsif Nam = Name_Rotate_Left then
530 Expand_Shift (N, E, N_Op_Rotate_Left);
532 elsif Nam = Name_Rotate_Right then
533 Expand_Shift (N, E, N_Op_Rotate_Right);
535 elsif Nam = Name_Shift_Left then
536 Expand_Shift (N, E, N_Op_Shift_Left);
538 elsif Nam = Name_Shift_Right then
539 Expand_Shift (N, E, N_Op_Shift_Right);
541 elsif Nam = Name_Shift_Right_Arithmetic then
542 Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
544 elsif Nam = Name_Unchecked_Conversion then
545 Expand_Unc_Conversion (N, E);
547 elsif Nam = Name_Unchecked_Deallocation then
548 Expand_Unc_Deallocation (N);
550 elsif Nam = Name_To_Address then
551 Expand_To_Address (N);
553 elsif Nam = Name_To_Pointer then
554 Expand_To_Pointer (N);
556 elsif Nam = Name_File
557 or else Nam = Name_Line
558 or else Nam = Name_Source_Location
559 or else Nam = Name_Enclosing_Entity
560 then
561 Expand_Source_Info (N, Nam);
563 -- If we have a renaming, expand the call to the original operation,
564 -- which must itself be intrinsic, since renaming requires matching
565 -- conventions and this has already been checked.
567 elsif Present (Alias (E)) then
568 Expand_Intrinsic_Call (N, Alias (E));
570 elsif Nkind (N) in N_Binary_Op then
571 Expand_Binary_Operator_Call (N);
573 -- The only other case is where an external name was specified, since
574 -- this is the only way that an otherwise unrecognized name could
575 -- escape the checking in Sem_Prag. Nothing needs to be done in such
576 -- a case, since we pass such a call to the back end unchanged.
578 else
579 null;
580 end if;
581 end Expand_Intrinsic_Call;
583 ------------------------
584 -- Expand_Is_Negative --
585 ------------------------
587 procedure Expand_Is_Negative (N : Node_Id) is
588 Loc : constant Source_Ptr := Sloc (N);
589 Opnd : constant Node_Id := Relocate_Node (First_Actual (N));
591 begin
593 -- We replace the function call by the following expression
595 -- if Opnd < 0.0 then
596 -- True
597 -- else
598 -- if Opnd > 0.0 then
599 -- False;
600 -- else
601 -- Float_Unsigned!(Float (Opnd)) /= 0
602 -- end if;
603 -- end if;
605 Rewrite (N,
606 Make_Conditional_Expression (Loc,
607 Expressions => New_List (
608 Make_Op_Lt (Loc,
609 Left_Opnd => Duplicate_Subexpr (Opnd),
610 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
612 New_Occurrence_Of (Standard_True, Loc),
614 Make_Conditional_Expression (Loc,
615 Expressions => New_List (
616 Make_Op_Gt (Loc,
617 Left_Opnd => Duplicate_Subexpr_No_Checks (Opnd),
618 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
620 New_Occurrence_Of (Standard_False, Loc),
622 Make_Op_Ne (Loc,
623 Left_Opnd =>
624 Unchecked_Convert_To
625 (RTE (RE_Float_Unsigned),
626 Convert_To
627 (Standard_Float,
628 Duplicate_Subexpr_No_Checks (Opnd))),
629 Right_Opnd =>
630 Make_Integer_Literal (Loc, 0)))))));
632 Analyze_And_Resolve (N, Standard_Boolean);
633 end Expand_Is_Negative;
635 ------------------
636 -- Expand_Shift --
637 ------------------
639 -- This procedure is used to convert a call to a shift function to the
640 -- corresponding operator node. This conversion is not done by the usual
641 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
642 -- operator nodes, because shifts are not predefined operators.
644 -- As a result, whenever a shift is used in the source program, it will
645 -- remain as a call until converted by this routine to the operator node
646 -- form which Gigi is expecting to see.
648 -- Note: it is possible for the expander to generate shift operator nodes
649 -- directly, which will be analyzed in the normal manner by calling Analyze
650 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
652 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
653 Entyp : constant Entity_Id := Etype (E);
654 Left : constant Node_Id := First_Actual (N);
655 Loc : constant Source_Ptr := Sloc (N);
656 Right : constant Node_Id := Next_Actual (Left);
657 Ltyp : constant Node_Id := Etype (Left);
658 Rtyp : constant Node_Id := Etype (Right);
659 Typ : constant Entity_Id := Etype (N);
660 Snode : Node_Id;
662 begin
663 Snode := New_Node (K, Loc);
664 Set_Right_Opnd (Snode, Relocate_Node (Right));
665 Set_Chars (Snode, Chars (E));
666 Set_Etype (Snode, Base_Type (Entyp));
667 Set_Entity (Snode, E);
669 if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
670 and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
671 then
672 Set_Shift_Count_OK (Snode, True);
673 end if;
675 if Typ = Entyp then
677 -- Note that we don't call Analyze and Resolve on this node, because
678 -- it already got analyzed and resolved when it was a function call.
680 Set_Left_Opnd (Snode, Relocate_Node (Left));
681 Rewrite (N, Snode);
682 Set_Analyzed (N);
684 else
686 -- If the context type is not the type of the operator, it is an
687 -- inherited operator for a derived type. Wrap the node in a
688 -- conversion so that it is type-consistent for possible further
689 -- expansion (e.g. within a lock-free protected type).
691 Set_Left_Opnd (Snode,
692 Unchecked_Convert_To (Base_Type (Entyp), Relocate_Node (Left)));
693 Rewrite (N, Unchecked_Convert_To (Typ, Snode));
695 -- Analyze and resolve result formed by conversion to target type
697 Analyze_And_Resolve (N, Typ);
698 end if;
699 end Expand_Shift;
701 ------------------------
702 -- Expand_Source_Info --
703 ------------------------
705 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
706 Loc : constant Source_Ptr := Sloc (N);
707 Ent : Entity_Id;
709 procedure Write_Entity_Name (E : Entity_Id);
710 -- Recursive procedure to construct string for qualified name of
711 -- enclosing program unit. The qualification stops at an enclosing
712 -- scope has no source name (block or loop). If entity is a subprogram
713 -- instance, skip enclosing wrapper package.
715 -----------------------
716 -- Write_Entity_Name --
717 -----------------------
719 procedure Write_Entity_Name (E : Entity_Id) is
720 SDef : Source_Ptr;
721 TDef : constant Source_Buffer_Ptr :=
722 Source_Text (Get_Source_File_Index (Sloc (E)));
724 begin
725 -- Nothing to do if at outer level
727 if Scope (E) = Standard_Standard then
728 null;
730 -- If scope comes from source, write its name
732 elsif Comes_From_Source (Scope (E)) then
733 Write_Entity_Name (Scope (E));
734 Add_Char_To_Name_Buffer ('.');
736 -- If in wrapper package skip past it
738 elsif Is_Wrapper_Package (Scope (E)) then
739 Write_Entity_Name (Scope (Scope (E)));
740 Add_Char_To_Name_Buffer ('.');
742 -- Otherwise nothing to output (happens in unnamed block statements)
744 else
745 null;
746 end if;
748 -- Loop to output the name
750 -- is this right wrt wide char encodings ??? (no!)
752 SDef := Sloc (E);
753 while TDef (SDef) in '0' .. '9'
754 or else TDef (SDef) >= 'A'
755 or else TDef (SDef) = ASCII.ESC
756 loop
757 Add_Char_To_Name_Buffer (TDef (SDef));
758 SDef := SDef + 1;
759 end loop;
760 end Write_Entity_Name;
762 -- Start of processing for Expand_Source_Info
764 begin
765 -- Integer cases
767 if Nam = Name_Line then
768 Rewrite (N,
769 Make_Integer_Literal (Loc,
770 Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
771 Analyze_And_Resolve (N, Standard_Positive);
773 -- String cases
775 else
776 Name_Len := 0;
778 case Nam is
779 when Name_File =>
780 Get_Decoded_Name_String
781 (Reference_Name (Get_Source_File_Index (Loc)));
783 when Name_Source_Location =>
784 Build_Location_String (Loc);
786 when Name_Enclosing_Entity =>
788 -- Skip enclosing blocks to reach enclosing unit
790 Ent := Current_Scope;
791 while Present (Ent) loop
792 exit when Ekind (Ent) /= E_Block
793 and then Ekind (Ent) /= E_Loop;
794 Ent := Scope (Ent);
795 end loop;
797 -- Ent now points to the relevant defining entity
799 Write_Entity_Name (Ent);
801 when others =>
802 raise Program_Error;
803 end case;
805 Rewrite (N,
806 Make_String_Literal (Loc,
807 Strval => String_From_Name_Buffer));
808 Analyze_And_Resolve (N, Standard_String);
809 end if;
811 Set_Is_Static_Expression (N);
812 end Expand_Source_Info;
814 ---------------------------
815 -- Expand_Unc_Conversion --
816 ---------------------------
818 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
819 Func : constant Entity_Id := Entity (Name (N));
820 Conv : Node_Id;
821 Ftyp : Entity_Id;
822 Ttyp : Entity_Id;
824 begin
825 -- Rewrite as unchecked conversion node. Note that we must convert
826 -- the operand to the formal type of the input parameter of the
827 -- function, so that the resulting N_Unchecked_Type_Conversion
828 -- call indicates the correct types for Gigi.
830 -- Right now, we only do this if a scalar type is involved. It is
831 -- not clear if it is needed in other cases. If we do attempt to
832 -- do the conversion unconditionally, it crashes 3411-018. To be
833 -- investigated further ???
835 Conv := Relocate_Node (First_Actual (N));
836 Ftyp := Etype (First_Formal (Func));
838 if Is_Scalar_Type (Ftyp) then
839 Conv := Convert_To (Ftyp, Conv);
840 Set_Parent (Conv, N);
841 Analyze_And_Resolve (Conv);
842 end if;
844 -- The instantiation of Unchecked_Conversion creates a wrapper package,
845 -- and the target type is declared as a subtype of the actual. Recover
846 -- the actual, which is the subtype indic. in the subtype declaration
847 -- for the target type. This is semantically correct, and avoids
848 -- anomalies with access subtypes. For entities, leave type as is.
850 -- We do the analysis here, because we do not want the compiler
851 -- to try to optimize or otherwise reorganize the unchecked
852 -- conversion node.
854 Ttyp := Etype (E);
856 if Is_Entity_Name (Conv) then
857 null;
859 elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
860 Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
862 elsif Is_Itype (Ttyp) then
863 Ttyp :=
864 Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
865 else
866 raise Program_Error;
867 end if;
869 Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
870 Set_Etype (N, Ttyp);
871 Set_Analyzed (N);
873 if Nkind (N) = N_Unchecked_Type_Conversion then
874 Expand_N_Unchecked_Type_Conversion (N);
875 end if;
876 end Expand_Unc_Conversion;
878 -----------------------------
879 -- Expand_Unc_Deallocation --
880 -----------------------------
882 -- Generate the following Code :
884 -- if Arg /= null then
885 -- <Finalize_Call> (.., T'Class(Arg.all), ..); -- for controlled types
886 -- Free (Arg);
887 -- Arg := Null;
888 -- end if;
890 -- For a task, we also generate a call to Free_Task to ensure that the
891 -- task itself is freed if it is terminated, ditto for a simple protected
892 -- object, with a call to Finalize_Protection. For composite types that
893 -- have tasks or simple protected objects as components, we traverse the
894 -- structures to find and terminate those components.
896 procedure Expand_Unc_Deallocation (N : Node_Id) is
897 Arg : constant Node_Id := First_Actual (N);
898 Loc : constant Source_Ptr := Sloc (N);
899 Typ : constant Entity_Id := Etype (Arg);
900 Desig_T : constant Entity_Id := Designated_Type (Typ);
901 Rtyp : constant Entity_Id := Underlying_Type (Root_Type (Typ));
902 Pool : constant Entity_Id := Associated_Storage_Pool (Rtyp);
903 Stmts : constant List_Id := New_List;
904 Needs_Fin : constant Boolean := Needs_Finalization (Desig_T);
906 Finalizer_Data : Finalization_Exception_Data;
908 Blk : Node_Id := Empty;
909 Deref : Node_Id;
910 Final_Code : List_Id;
911 Free_Arg : Node_Id;
912 Free_Node : Node_Id;
913 Gen_Code : Node_Id;
915 Arg_Known_Non_Null : constant Boolean := Known_Non_Null (N);
916 -- This captures whether we know the argument to be non-null so that
917 -- we can avoid the test. The reason that we need to capture this is
918 -- that we analyze some generated statements before properly attaching
919 -- them to the tree, and that can disturb current value settings.
921 begin
922 -- Nothing to do if we know the argument is null
924 if Known_Null (N) then
925 return;
926 end if;
928 -- Processing for pointer to controlled type
930 if Needs_Fin then
931 Deref :=
932 Make_Explicit_Dereference (Loc,
933 Prefix => Duplicate_Subexpr_No_Checks (Arg));
935 -- If the type is tagged, then we must force dispatching on the
936 -- finalization call because the designated type may not be the
937 -- actual type of the object.
939 if Is_Tagged_Type (Desig_T)
940 and then not Is_Class_Wide_Type (Desig_T)
941 then
942 Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
944 elsif not Is_Tagged_Type (Desig_T) then
946 -- Set type of result, to force a conversion when needed (see
947 -- exp_ch7, Convert_View), given that Deep_Finalize may be
948 -- inherited from the parent type, and we need the type of the
949 -- expression to see whether the conversion is in fact needed.
951 Set_Etype (Deref, Desig_T);
952 end if;
954 -- The finalization call is expanded wrapped in a block to catch any
955 -- possible exception. If an exception does occur, then Program_Error
956 -- must be raised following the freeing of the object and its removal
957 -- from the finalization collection's list. We set a flag to record
958 -- that an exception was raised, and save its occurrence for use in
959 -- the later raise.
961 -- Generate:
962 -- Abort : constant Boolean :=
963 -- Exception_Occurrence (Get_Current_Excep.all.all) =
964 -- Standard'Abort_Signal'Identity;
965 -- <or>
966 -- Abort : constant Boolean := False; -- no abort
968 -- E : Exception_Occurrence;
969 -- Raised : Boolean := False;
971 -- begin
972 -- [Deep_]Finalize (Obj);
973 -- exception
974 -- when others =>
975 -- Raised := True;
976 -- Save_Occurrence (E, Get_Current_Excep.all.all);
977 -- end;
979 Build_Object_Declarations (Finalizer_Data, Stmts, Loc);
981 Final_Code := New_List (
982 Make_Block_Statement (Loc,
983 Handled_Statement_Sequence =>
984 Make_Handled_Sequence_Of_Statements (Loc,
985 Statements => New_List (
986 Make_Final_Call (Obj_Ref => Deref, Typ => Desig_T)),
987 Exception_Handlers => New_List (
988 Build_Exception_Handler (Finalizer_Data)))));
990 -- For .NET/JVM, detach the object from the containing finalization
991 -- collection before finalizing it.
993 if VM_Target /= No_VM and then Is_Controlled (Desig_T) then
994 Prepend_To (Final_Code,
995 Make_Detach_Call (New_Copy_Tree (Arg)));
996 end if;
998 -- If aborts are allowed, then the finalization code must be
999 -- protected by an abort defer/undefer pair.
1001 if Abort_Allowed then
1002 Prepend_To (Final_Code,
1003 Build_Runtime_Call (Loc, RE_Abort_Defer));
1005 Blk :=
1006 Make_Block_Statement (Loc, Handled_Statement_Sequence =>
1007 Make_Handled_Sequence_Of_Statements (Loc,
1008 Statements => Final_Code,
1009 At_End_Proc =>
1010 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc)));
1012 Append (Blk, Stmts);
1013 else
1014 Append_List_To (Stmts, Final_Code);
1015 end if;
1016 end if;
1018 -- For a task type, call Free_Task before freeing the ATCB
1020 if Is_Task_Type (Desig_T) then
1021 declare
1022 Stat : Node_Id := Prev (N);
1023 Nam1 : Node_Id;
1024 Nam2 : Node_Id;
1026 begin
1027 -- An Abort followed by a Free will not do what the user expects,
1028 -- because the abort is not immediate. This is worth a warning.
1030 while Present (Stat)
1031 and then not Comes_From_Source (Original_Node (Stat))
1032 loop
1033 Prev (Stat);
1034 end loop;
1036 if Present (Stat)
1037 and then Nkind (Original_Node (Stat)) = N_Abort_Statement
1038 then
1039 Stat := Original_Node (Stat);
1040 Nam1 := First (Names (Stat));
1041 Nam2 := Original_Node (First (Parameter_Associations (N)));
1043 if Nkind (Nam1) = N_Explicit_Dereference
1044 and then Is_Entity_Name (Prefix (Nam1))
1045 and then Is_Entity_Name (Nam2)
1046 and then Entity (Prefix (Nam1)) = Entity (Nam2)
1047 then
1048 Error_Msg_N ("abort may take time to complete?", N);
1049 Error_Msg_N ("\deallocation might have no effect?", N);
1050 Error_Msg_N ("\safer to wait for termination.?", N);
1051 end if;
1052 end if;
1053 end;
1055 Append_To
1056 (Stmts, Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
1058 -- For composite types that contain tasks, recurse over the structure
1059 -- to build the selectors for the task subcomponents.
1061 elsif Has_Task (Desig_T) then
1062 if Is_Record_Type (Desig_T) then
1063 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
1065 elsif Is_Array_Type (Desig_T) then
1066 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
1067 end if;
1068 end if;
1070 -- Same for simple protected types. Eventually call Finalize_Protection
1071 -- before freeing the PO for each protected component.
1073 if Is_Simple_Protected_Type (Desig_T) then
1074 Append_To (Stmts,
1075 Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
1077 elsif Has_Simple_Protected_Object (Desig_T) then
1078 if Is_Record_Type (Desig_T) then
1079 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
1080 elsif Is_Array_Type (Desig_T) then
1081 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
1082 end if;
1083 end if;
1085 -- Normal processing for non-controlled types
1087 Free_Arg := Duplicate_Subexpr_No_Checks (Arg);
1088 Free_Node := Make_Free_Statement (Loc, Empty);
1089 Append_To (Stmts, Free_Node);
1090 Set_Storage_Pool (Free_Node, Pool);
1092 -- Attach to tree before analysis of generated subtypes below
1094 Set_Parent (Stmts, Parent (N));
1096 -- Deal with storage pool
1098 if Present (Pool) then
1100 -- Freeing the secondary stack is meaningless
1102 if Is_RTE (Pool, RE_SS_Pool) then
1103 null;
1105 -- If the pool object is of a simple storage pool type, then attempt
1106 -- to locate the type's Deallocate procedure, if any, and set the
1107 -- free operation's procedure to call. If the type doesn't have a
1108 -- Deallocate (which is allowed), then the actual will simply be set
1109 -- to null.
1111 elsif Present (Get_Rep_Pragma
1112 (Etype (Pool), Name_Simple_Storage_Pool_Type))
1113 then
1114 declare
1115 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
1116 Dealloc_Op : Entity_Id;
1117 begin
1118 Dealloc_Op := Get_Name_Entity_Id (Name_Deallocate);
1119 while Present (Dealloc_Op) loop
1120 if Scope (Dealloc_Op) = Scope (Pool_Type)
1121 and then Present (First_Formal (Dealloc_Op))
1122 and then Etype (First_Formal (Dealloc_Op)) = Pool_Type
1123 then
1124 Set_Procedure_To_Call (Free_Node, Dealloc_Op);
1125 exit;
1126 else
1127 Dealloc_Op := Homonym (Dealloc_Op);
1128 end if;
1129 end loop;
1130 end;
1132 -- Case of a class-wide pool type: make a dispatching call to
1133 -- Deallocate through the class-wide Deallocate_Any.
1135 elsif Is_Class_Wide_Type (Etype (Pool)) then
1136 Set_Procedure_To_Call (Free_Node, RTE (RE_Deallocate_Any));
1138 -- Case of a specific pool type: make a statically bound call
1140 else
1141 Set_Procedure_To_Call (Free_Node,
1142 Find_Prim_Op (Etype (Pool), Name_Deallocate));
1143 end if;
1144 end if;
1146 if Present (Procedure_To_Call (Free_Node)) then
1148 -- For all cases of a Deallocate call, the back-end needs to be able
1149 -- to compute the size of the object being freed. This may require
1150 -- some adjustments for objects of dynamic size.
1152 -- If the type is class wide, we generate an implicit type with the
1153 -- right dynamic size, so that the deallocate call gets the right
1154 -- size parameter computed by GIGI. Same for an access to
1155 -- unconstrained packed array.
1157 if Is_Class_Wide_Type (Desig_T)
1158 or else
1159 (Is_Array_Type (Desig_T)
1160 and then not Is_Constrained (Desig_T)
1161 and then Is_Packed (Desig_T))
1162 then
1163 declare
1164 Deref : constant Node_Id :=
1165 Make_Explicit_Dereference (Loc,
1166 Duplicate_Subexpr_No_Checks (Arg));
1167 D_Subtyp : Node_Id;
1168 D_Type : Entity_Id;
1170 begin
1171 -- Perform minor decoration as it is needed by the side effect
1172 -- removal mechanism.
1174 Set_Etype (Deref, Desig_T);
1175 Set_Parent (Deref, Free_Node);
1176 D_Subtyp := Make_Subtype_From_Expr (Deref, Desig_T);
1178 if Nkind (D_Subtyp) in N_Has_Entity then
1179 D_Type := Entity (D_Subtyp);
1181 else
1182 D_Type := Make_Temporary (Loc, 'A');
1183 Insert_Action (Deref,
1184 Make_Subtype_Declaration (Loc,
1185 Defining_Identifier => D_Type,
1186 Subtype_Indication => D_Subtyp));
1187 end if;
1189 -- Force freezing at the point of the dereference. For the
1190 -- class wide case, this avoids having the subtype frozen
1191 -- before the equivalent type.
1193 Freeze_Itype (D_Type, Deref);
1195 Set_Actual_Designated_Subtype (Free_Node, D_Type);
1196 end;
1198 end if;
1199 end if;
1201 -- Ada 2005 (AI-251): In case of abstract interface type we must
1202 -- displace the pointer to reference the base of the object to
1203 -- deallocate its memory, unless we're targetting a VM, in which case
1204 -- no special processing is required.
1206 -- Generate:
1207 -- free (Base_Address (Obj_Ptr))
1209 if Is_Interface (Directly_Designated_Type (Typ))
1210 and then Tagged_Type_Expansion
1211 then
1212 Set_Expression (Free_Node,
1213 Unchecked_Convert_To (Typ,
1214 Make_Function_Call (Loc,
1215 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
1216 Parameter_Associations => New_List (
1217 Unchecked_Convert_To (RTE (RE_Address), Free_Arg)))));
1219 -- Generate:
1220 -- free (Obj_Ptr)
1222 else
1223 Set_Expression (Free_Node, Free_Arg);
1224 end if;
1226 -- Only remaining step is to set result to null, or generate a raise of
1227 -- Constraint_Error if the target object is "not null".
1229 if Can_Never_Be_Null (Etype (Arg)) then
1230 Append_To (Stmts,
1231 Make_Raise_Constraint_Error (Loc,
1232 Reason => CE_Access_Check_Failed));
1234 else
1235 declare
1236 Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
1237 begin
1238 Set_Assignment_OK (Lhs);
1239 Append_To (Stmts,
1240 Make_Assignment_Statement (Loc,
1241 Name => Lhs,
1242 Expression => Make_Null (Loc)));
1243 end;
1244 end if;
1246 -- Generate a test of whether any earlier finalization raised an
1247 -- exception, and in that case raise Program_Error with the previous
1248 -- exception occurrence.
1250 -- Generate:
1251 -- if Raised and then not Abort then
1252 -- raise Program_Error; -- for .NET and
1253 -- -- restricted RTS
1254 -- <or>
1255 -- Raise_From_Controlled_Operation (E); -- all other cases
1256 -- end if;
1258 if Needs_Fin then
1259 Append_To (Stmts, Build_Raise_Statement (Finalizer_Data));
1260 end if;
1262 -- If we know the argument is non-null, then make a block statement
1263 -- that contains the required statements, no need for a test.
1265 if Arg_Known_Non_Null then
1266 Gen_Code :=
1267 Make_Block_Statement (Loc,
1268 Handled_Statement_Sequence =>
1269 Make_Handled_Sequence_Of_Statements (Loc,
1270 Statements => Stmts));
1272 -- If the argument may be null, wrap the statements inside an IF that
1273 -- does an explicit test to exclude the null case.
1275 else
1276 Gen_Code :=
1277 Make_Implicit_If_Statement (N,
1278 Condition =>
1279 Make_Op_Ne (Loc,
1280 Left_Opnd => Duplicate_Subexpr (Arg),
1281 Right_Opnd => Make_Null (Loc)),
1282 Then_Statements => Stmts);
1283 end if;
1285 -- Rewrite the call
1287 Rewrite (N, Gen_Code);
1288 Analyze (N);
1290 -- If we generated a block with an At_End_Proc, expand the exception
1291 -- handler. We need to wait until after everything else is analyzed.
1293 if Present (Blk) then
1294 Expand_At_End_Handler
1295 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
1296 end if;
1297 end Expand_Unc_Deallocation;
1299 -----------------------
1300 -- Expand_To_Address --
1301 -----------------------
1303 procedure Expand_To_Address (N : Node_Id) is
1304 Loc : constant Source_Ptr := Sloc (N);
1305 Arg : constant Node_Id := First_Actual (N);
1306 Obj : Node_Id;
1308 begin
1309 Remove_Side_Effects (Arg);
1311 Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
1313 Rewrite (N,
1314 Make_Conditional_Expression (Loc,
1315 Expressions => New_List (
1316 Make_Op_Eq (Loc,
1317 Left_Opnd => New_Copy_Tree (Arg),
1318 Right_Opnd => Make_Null (Loc)),
1319 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
1320 Make_Attribute_Reference (Loc,
1321 Prefix => Obj,
1322 Attribute_Name => Name_Address))));
1324 Analyze_And_Resolve (N, RTE (RE_Address));
1325 end Expand_To_Address;
1327 -----------------------
1328 -- Expand_To_Pointer --
1329 -----------------------
1331 procedure Expand_To_Pointer (N : Node_Id) is
1332 Arg : constant Node_Id := First_Actual (N);
1334 begin
1335 Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
1336 Analyze (N);
1337 end Expand_To_Pointer;
1339 end Exp_Intr;