Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / ada / exp_intr.adb
blob977e335567d02b49404320918e97ec6a3f3eee18
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-2010, 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_Eval; use Sem_Eval;
48 with Sem_Res; use Sem_Res;
49 with Sem_Type; use Sem_Type;
50 with Sem_Util; use Sem_Util;
51 with Sinfo; use Sinfo;
52 with Sinput; use Sinput;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Stringt; use Stringt;
56 with Tbuild; use Tbuild;
57 with Uintp; use Uintp;
58 with Urealp; use Urealp;
60 package body Exp_Intr is
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 procedure Expand_Binary_Operator_Call (N : Node_Id);
67 -- Expand a call to an intrinsic arithmetic operator when the operand
68 -- types or sizes are not identical.
70 procedure Expand_Is_Negative (N : Node_Id);
71 -- Expand a call to the intrinsic Is_Negative function
73 procedure Expand_Dispatching_Constructor_Call (N : Node_Id);
74 -- Expand a call to an instantiation of Generic_Dispatching_Constructor
75 -- into a dispatching call to the actual subprogram associated with the
76 -- Constructor formal subprogram, passing it the Parameters actual of
77 -- the call to the instantiation and dispatching based on call's Tag
78 -- parameter.
80 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
81 -- Expand a call to Exception_Information/Message/Name. The first
82 -- parameter, N, is the node for the function call, and Ent is the
83 -- entity for the corresponding routine in the Ada.Exceptions package.
85 procedure Expand_Import_Call (N : Node_Id);
86 -- Expand a call to Import_Address/Longest_Integer/Value. The parameter
87 -- N is the node for the function call.
89 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
90 -- Expand an intrinsic shift operation, N and E are from the call to
91 -- Expand_Intrinsic_Call (call node and subprogram spec entity) and
92 -- K is the kind for the shift node
94 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
95 -- Expand a call to an instantiation of Unchecked_Conversion into a node
96 -- N_Unchecked_Type_Conversion.
98 procedure Expand_Unc_Deallocation (N : Node_Id);
99 -- Expand a call to an instantiation of Unchecked_Deallocation into a node
100 -- N_Free_Statement and appropriate context.
102 procedure Expand_To_Address (N : Node_Id);
103 procedure Expand_To_Pointer (N : Node_Id);
104 -- Expand a call to corresponding function, declared in an instance of
105 -- System.Address_To_Access_Conversions.
107 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
108 -- Rewrite the node by the appropriate string or positive constant.
109 -- Nam can be one of the following:
110 -- Name_File - expand string that is the name of source file
111 -- Name_Line - expand integer line number
112 -- Name_Source_Location - expand string of form file:line
113 -- Name_Enclosing_Entity - expand string with name of enclosing entity
115 ---------------------------------
116 -- Expand_Binary_Operator_Call --
117 ---------------------------------
119 procedure Expand_Binary_Operator_Call (N : Node_Id) is
120 T1 : constant Entity_Id := Underlying_Type (Etype (Left_Opnd (N)));
121 T2 : constant Entity_Id := Underlying_Type (Etype (Right_Opnd (N)));
122 TR : constant Entity_Id := Etype (N);
123 T3 : Entity_Id;
124 Res : Node_Id;
126 Siz : constant Uint := UI_Max (Esize (T1), Esize (T2));
127 -- Maximum of operand sizes
129 begin
130 -- Nothing to do if the operands have the same modular type
132 if Base_Type (T1) = Base_Type (T2)
133 and then Is_Modular_Integer_Type (T1)
134 then
135 return;
136 end if;
138 -- Use Unsigned_32 for sizes of 32 or below, else Unsigned_64
140 if Siz > 32 then
141 T3 := RTE (RE_Unsigned_64);
142 else
143 T3 := RTE (RE_Unsigned_32);
144 end if;
146 -- Copy operator node, and reset type and entity fields, for
147 -- subsequent reanalysis.
149 Res := New_Copy (N);
150 Set_Etype (Res, T3);
152 case Nkind (N) is
153 when N_Op_And =>
154 Set_Entity (Res, Standard_Op_And);
155 when N_Op_Or =>
156 Set_Entity (Res, Standard_Op_Or);
157 when N_Op_Xor =>
158 Set_Entity (Res, Standard_Op_Xor);
159 when others =>
160 raise Program_Error;
161 end case;
163 -- Convert operands to large enough intermediate type
165 Set_Left_Opnd (Res,
166 Unchecked_Convert_To (T3, Relocate_Node (Left_Opnd (N))));
167 Set_Right_Opnd (Res,
168 Unchecked_Convert_To (T3, Relocate_Node (Right_Opnd (N))));
170 -- Analyze and resolve result formed by conversion to target type
172 Rewrite (N, Unchecked_Convert_To (TR, Res));
173 Analyze_And_Resolve (N, TR);
174 end Expand_Binary_Operator_Call;
176 -----------------------------------------
177 -- Expand_Dispatching_Constructor_Call --
178 -----------------------------------------
180 -- Transform a call to an instantiation of Generic_Dispatching_Constructor
181 -- of the form:
183 -- GDC_Instance (The_Tag, Parameters'Access)
185 -- to a class-wide conversion of a dispatching call to the actual
186 -- associated with the formal subprogram Construct, designating The_Tag
187 -- as the controlling tag of the call:
189 -- T'Class (Construct'Actual (Params)) -- Controlling tag is The_Tag
191 -- which will eventually be expanded to the following:
193 -- T'Class (The_Tag.all (Construct'Actual'Index).all (Params))
195 -- A class-wide membership test is also generated, preceding the call, to
196 -- ensure that the controlling tag denotes a type in T'Class.
198 procedure Expand_Dispatching_Constructor_Call (N : Node_Id) is
199 Loc : constant Source_Ptr := Sloc (N);
200 Tag_Arg : constant Node_Id := First_Actual (N);
201 Param_Arg : constant Node_Id := Next_Actual (Tag_Arg);
202 Subp_Decl : constant Node_Id := Parent (Parent (Entity (Name (N))));
203 Inst_Pkg : constant Node_Id := Parent (Subp_Decl);
204 Act_Rename : Node_Id;
205 Act_Constr : Entity_Id;
206 Iface_Tag : Node_Id := Empty;
207 Cnstr_Call : Node_Id;
208 Result_Typ : Entity_Id;
210 begin
211 -- The subprogram is the third actual in the instantiation, and is
212 -- retrieved from the corresponding renaming declaration. However,
213 -- freeze nodes may appear before, so we retrieve the declaration
214 -- with an explicit loop.
216 Act_Rename := First (Visible_Declarations (Inst_Pkg));
217 while Nkind (Act_Rename) /= N_Subprogram_Renaming_Declaration loop
218 Next (Act_Rename);
219 end loop;
221 Act_Constr := Entity (Name (Act_Rename));
222 Result_Typ := Class_Wide_Type (Etype (Act_Constr));
224 -- Ada 2005 (AI-251): If the result is an interface type, the function
225 -- returns a class-wide interface type (otherwise the resulting object
226 -- would be abstract!)
228 if Is_Interface (Etype (Act_Constr)) then
229 Set_Etype (Act_Constr, Result_Typ);
231 -- If the result type is not parent of Tag_Arg then we need to
232 -- locate the tag of the secondary dispatch table.
234 if not Is_Ancestor (Etype (Result_Typ), Etype (Tag_Arg)) then
235 pragma Assert (not Is_Interface (Etype (Tag_Arg)));
237 Iface_Tag :=
238 Make_Object_Declaration (Loc,
239 Defining_Identifier => Make_Temporary (Loc, 'V'),
240 Object_Definition =>
241 New_Reference_To (RTE (RE_Tag), Loc),
242 Expression =>
243 Make_Function_Call (Loc,
244 Name => New_Reference_To (RTE (RE_Secondary_Tag), Loc),
245 Parameter_Associations => New_List (
246 Relocate_Node (Tag_Arg),
247 New_Reference_To
248 (Node (First_Elmt (Access_Disp_Table
249 (Etype (Etype (Act_Constr))))),
250 Loc))));
251 Insert_Action (N, Iface_Tag);
252 end if;
253 end if;
255 -- Create the call to the actual Constructor function
257 Cnstr_Call :=
258 Make_Function_Call (Loc,
259 Name => New_Occurrence_Of (Act_Constr, Loc),
260 Parameter_Associations => New_List (Relocate_Node (Param_Arg)));
262 -- Establish its controlling tag from the tag passed to the instance
263 -- The tag may be given by a function call, in which case a temporary
264 -- should be generated now, to prevent out-of-order insertions during
265 -- the expansion of that call when stack-checking is enabled.
267 if Present (Iface_Tag) then
268 Set_Controlling_Argument (Cnstr_Call,
269 New_Occurrence_Of (Defining_Identifier (Iface_Tag), Loc));
270 else
271 Remove_Side_Effects (Tag_Arg);
272 Set_Controlling_Argument (Cnstr_Call,
273 Relocate_Node (Tag_Arg));
274 end if;
276 -- Rewrite and analyze the call to the instance as a class-wide
277 -- conversion of the call to the actual constructor.
279 Rewrite (N, Convert_To (Result_Typ, Cnstr_Call));
280 Analyze_And_Resolve (N, Etype (Act_Constr));
282 -- Do not generate a run-time check on the built object if tag
283 -- checks are suppressed for the result type or VM_Target /= No_VM
285 if Tag_Checks_Suppressed (Etype (Result_Typ))
286 or else not Tagged_Type_Expansion
287 then
288 null;
290 -- Generate a class-wide membership test to ensure that the call's tag
291 -- argument denotes a type within the class. We must keep separate the
292 -- case in which the Result_Type of the constructor function is a tagged
293 -- type from the case in which it is an abstract interface because the
294 -- run-time subprogram required to check these cases differ (and have
295 -- one difference in their parameters profile).
297 -- Call CW_Membership if the Result_Type is a tagged type to look for
298 -- the tag in the table of ancestor tags.
300 elsif not Is_Interface (Result_Typ) then
301 declare
302 Obj_Tag_Node : Node_Id := Duplicate_Subexpr (Tag_Arg);
303 CW_Test_Node : Node_Id;
305 begin
306 Build_CW_Membership (Loc,
307 Obj_Tag_Node => Obj_Tag_Node,
308 Typ_Tag_Node =>
309 New_Reference_To (
310 Node (First_Elmt (Access_Disp_Table (
311 Root_Type (Result_Typ)))), Loc),
312 Related_Nod => N,
313 New_Node => CW_Test_Node);
315 Insert_Action (N,
316 Make_Implicit_If_Statement (N,
317 Condition =>
318 Make_Op_Not (Loc, CW_Test_Node),
319 Then_Statements =>
320 New_List (Make_Raise_Statement (Loc,
321 New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
322 end;
324 -- Call IW_Membership test if the Result_Type is an abstract interface
325 -- to look for the tag in the table of interface tags.
327 else
328 Insert_Action (N,
329 Make_Implicit_If_Statement (N,
330 Condition =>
331 Make_Op_Not (Loc,
332 Make_Function_Call (Loc,
333 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
334 Parameter_Associations => New_List (
335 Make_Attribute_Reference (Loc,
336 Prefix => Duplicate_Subexpr (Tag_Arg),
337 Attribute_Name => Name_Address),
339 New_Reference_To (
340 Node (First_Elmt (Access_Disp_Table (
341 Root_Type (Result_Typ)))), Loc)))),
342 Then_Statements =>
343 New_List (
344 Make_Raise_Statement (Loc,
345 Name => New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
346 end if;
347 end Expand_Dispatching_Constructor_Call;
349 ---------------------------
350 -- Expand_Exception_Call --
351 ---------------------------
353 -- If the function call is not within an exception handler, then the call
354 -- is replaced by a null string. Otherwise the appropriate routine in
355 -- Ada.Exceptions is called passing the choice parameter specification
356 -- from the enclosing handler. If the enclosing handler lacks a choice
357 -- parameter, then one is supplied.
359 procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
360 Loc : constant Source_Ptr := Sloc (N);
361 P : Node_Id;
362 E : Entity_Id;
364 begin
365 -- Climb up parents to see if we are in exception handler
367 P := Parent (N);
368 loop
369 -- Case of not in exception handler, replace by null string
371 if No (P) then
372 Rewrite (N,
373 Make_String_Literal (Loc,
374 Strval => ""));
375 exit;
377 -- Case of in exception handler
379 elsif Nkind (P) = N_Exception_Handler then
381 -- Handler cannot be used for a local raise, and furthermore, this
382 -- is a violation of the No_Exception_Propagation restriction.
384 Set_Local_Raise_Not_OK (P);
385 Check_Restriction (No_Exception_Propagation, N);
387 -- If no choice parameter present, then put one there. Note that
388 -- we do not need to put it on the entity chain, since no one will
389 -- be referencing it by normal visibility methods.
391 if No (Choice_Parameter (P)) then
392 E := Make_Temporary (Loc, 'E');
393 Set_Choice_Parameter (P, E);
394 Set_Ekind (E, E_Variable);
395 Set_Etype (E, RTE (RE_Exception_Occurrence));
396 Set_Scope (E, Current_Scope);
397 end if;
399 Rewrite (N,
400 Make_Function_Call (Loc,
401 Name => New_Occurrence_Of (RTE (Ent), Loc),
402 Parameter_Associations => New_List (
403 New_Occurrence_Of (Choice_Parameter (P), Loc))));
404 exit;
406 -- Keep climbing!
408 else
409 P := Parent (P);
410 end if;
411 end loop;
413 Analyze_And_Resolve (N, Standard_String);
414 end Expand_Exception_Call;
416 ------------------------
417 -- Expand_Import_Call --
418 ------------------------
420 -- The function call must have a static string as its argument. We create
421 -- a dummy variable which uses this string as the external name in an
422 -- Import pragma. The result is then obtained as the address of this
423 -- dummy variable, converted to the appropriate target type.
425 procedure Expand_Import_Call (N : Node_Id) is
426 Loc : constant Source_Ptr := Sloc (N);
427 Ent : constant Entity_Id := Entity (Name (N));
428 Str : constant Node_Id := First_Actual (N);
429 Dum : constant Entity_Id := Make_Temporary (Loc, 'D');
431 begin
432 Insert_Actions (N, New_List (
433 Make_Object_Declaration (Loc,
434 Defining_Identifier => Dum,
435 Object_Definition =>
436 New_Occurrence_Of (Standard_Character, Loc)),
438 Make_Pragma (Loc,
439 Chars => Name_Import,
440 Pragma_Argument_Associations => New_List (
441 Make_Pragma_Argument_Association (Loc,
442 Expression => Make_Identifier (Loc, Name_Ada)),
444 Make_Pragma_Argument_Association (Loc,
445 Expression => Make_Identifier (Loc, Chars (Dum))),
447 Make_Pragma_Argument_Association (Loc,
448 Chars => Name_Link_Name,
449 Expression => Relocate_Node (Str))))));
451 Rewrite (N,
452 Unchecked_Convert_To (Etype (Ent),
453 Make_Attribute_Reference (Loc,
454 Prefix => Make_Identifier (Loc, Chars (Dum)),
455 Attribute_Name => Name_Address)));
457 Analyze_And_Resolve (N, Etype (Ent));
458 end Expand_Import_Call;
460 ---------------------------
461 -- Expand_Intrinsic_Call --
462 ---------------------------
464 procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
465 Nam : Name_Id;
467 begin
468 -- If an external name is specified for the intrinsic, it is handled
469 -- by the back-end: leave the call node unchanged for now.
471 if Present (Interface_Name (E)) then
472 return;
473 end if;
475 -- If the intrinsic subprogram is generic, gets its original name
477 if Present (Parent (E))
478 and then Present (Generic_Parent (Parent (E)))
479 then
480 Nam := Chars (Generic_Parent (Parent (E)));
481 else
482 Nam := Chars (E);
483 end if;
485 if Nam = Name_Asm then
486 Expand_Asm_Call (N);
488 elsif Nam = Name_Divide then
489 Expand_Decimal_Divide_Call (N);
491 elsif Nam = Name_Exception_Information then
492 Expand_Exception_Call (N, RE_Exception_Information);
494 elsif Nam = Name_Exception_Message then
495 Expand_Exception_Call (N, RE_Exception_Message);
497 elsif Nam = Name_Exception_Name then
498 Expand_Exception_Call (N, RE_Exception_Name_Simple);
500 elsif Nam = Name_Generic_Dispatching_Constructor then
501 Expand_Dispatching_Constructor_Call (N);
503 elsif Nam = Name_Import_Address
504 or else
505 Nam = Name_Import_Largest_Value
506 or else
507 Nam = Name_Import_Value
508 then
509 Expand_Import_Call (N);
511 elsif Nam = Name_Is_Negative then
512 Expand_Is_Negative (N);
514 elsif Nam = Name_Rotate_Left then
515 Expand_Shift (N, E, N_Op_Rotate_Left);
517 elsif Nam = Name_Rotate_Right then
518 Expand_Shift (N, E, N_Op_Rotate_Right);
520 elsif Nam = Name_Shift_Left then
521 Expand_Shift (N, E, N_Op_Shift_Left);
523 elsif Nam = Name_Shift_Right then
524 Expand_Shift (N, E, N_Op_Shift_Right);
526 elsif Nam = Name_Shift_Right_Arithmetic then
527 Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
529 elsif Nam = Name_Unchecked_Conversion then
530 Expand_Unc_Conversion (N, E);
532 elsif Nam = Name_Unchecked_Deallocation then
533 Expand_Unc_Deallocation (N);
535 elsif Nam = Name_To_Address then
536 Expand_To_Address (N);
538 elsif Nam = Name_To_Pointer then
539 Expand_To_Pointer (N);
541 elsif Nam = Name_File
542 or else Nam = Name_Line
543 or else Nam = Name_Source_Location
544 or else Nam = Name_Enclosing_Entity
545 then
546 Expand_Source_Info (N, Nam);
548 -- If we have a renaming, expand the call to the original operation,
549 -- which must itself be intrinsic, since renaming requires matching
550 -- conventions and this has already been checked.
552 elsif Present (Alias (E)) then
553 Expand_Intrinsic_Call (N, Alias (E));
555 elsif Nkind (N) in N_Binary_Op then
556 Expand_Binary_Operator_Call (N);
558 -- The only other case is where an external name was specified,
559 -- since this is the only way that an otherwise unrecognized
560 -- name could escape the checking in Sem_Prag. Nothing needs
561 -- to be done in such a case, since we pass such a call to the
562 -- back end unchanged.
564 else
565 null;
566 end if;
567 end Expand_Intrinsic_Call;
569 ------------------------
570 -- Expand_Is_Negative --
571 ------------------------
573 procedure Expand_Is_Negative (N : Node_Id) is
574 Loc : constant Source_Ptr := Sloc (N);
575 Opnd : constant Node_Id := Relocate_Node (First_Actual (N));
577 begin
579 -- We replace the function call by the following expression
581 -- if Opnd < 0.0 then
582 -- True
583 -- else
584 -- if Opnd > 0.0 then
585 -- False;
586 -- else
587 -- Float_Unsigned!(Float (Opnd)) /= 0
588 -- end if;
589 -- end if;
591 Rewrite (N,
592 Make_Conditional_Expression (Loc,
593 Expressions => New_List (
594 Make_Op_Lt (Loc,
595 Left_Opnd => Duplicate_Subexpr (Opnd),
596 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
598 New_Occurrence_Of (Standard_True, Loc),
600 Make_Conditional_Expression (Loc,
601 Expressions => New_List (
602 Make_Op_Gt (Loc,
603 Left_Opnd => Duplicate_Subexpr_No_Checks (Opnd),
604 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
606 New_Occurrence_Of (Standard_False, Loc),
608 Make_Op_Ne (Loc,
609 Left_Opnd =>
610 Unchecked_Convert_To
611 (RTE (RE_Float_Unsigned),
612 Convert_To
613 (Standard_Float,
614 Duplicate_Subexpr_No_Checks (Opnd))),
615 Right_Opnd =>
616 Make_Integer_Literal (Loc, 0)))))));
618 Analyze_And_Resolve (N, Standard_Boolean);
619 end Expand_Is_Negative;
621 ------------------
622 -- Expand_Shift --
623 ------------------
625 -- This procedure is used to convert a call to a shift function to the
626 -- corresponding operator node. This conversion is not done by the usual
627 -- circuit for converting calls to operator functions (e.g. "+"(1,2)) to
628 -- operator nodes, because shifts are not predefined operators.
630 -- As a result, whenever a shift is used in the source program, it will
631 -- remain as a call until converted by this routine to the operator node
632 -- form which Gigi is expecting to see.
634 -- Note: it is possible for the expander to generate shift operator nodes
635 -- directly, which will be analyzed in the normal manner by calling Analyze
636 -- and Resolve. Such shift operator nodes will not be seen by Expand_Shift.
638 procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
639 Loc : constant Source_Ptr := Sloc (N);
640 Typ : constant Entity_Id := Etype (N);
641 Left : constant Node_Id := First_Actual (N);
642 Right : constant Node_Id := Next_Actual (Left);
643 Ltyp : constant Node_Id := Etype (Left);
644 Rtyp : constant Node_Id := Etype (Right);
645 Snode : Node_Id;
647 begin
648 Snode := New_Node (K, Loc);
649 Set_Left_Opnd (Snode, Relocate_Node (Left));
650 Set_Right_Opnd (Snode, Relocate_Node (Right));
651 Set_Chars (Snode, Chars (E));
652 Set_Etype (Snode, Base_Type (Typ));
653 Set_Entity (Snode, E);
655 if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
656 and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
657 then
658 Set_Shift_Count_OK (Snode, True);
659 end if;
661 -- Do the rewrite. Note that we don't call Analyze and Resolve on
662 -- this node, because it already got analyzed and resolved when
663 -- it was a function call!
665 Rewrite (N, Snode);
666 Set_Analyzed (N);
667 end Expand_Shift;
669 ------------------------
670 -- Expand_Source_Info --
671 ------------------------
673 procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
674 Loc : constant Source_Ptr := Sloc (N);
675 Ent : Entity_Id;
677 procedure Write_Entity_Name (E : Entity_Id);
678 -- Recursive procedure to construct string for qualified name of
679 -- enclosing program unit. The qualification stops at an enclosing
680 -- scope has no source name (block or loop). If entity is a subprogram
681 -- instance, skip enclosing wrapper package.
683 -----------------------
684 -- Write_Entity_Name --
685 -----------------------
687 procedure Write_Entity_Name (E : Entity_Id) is
688 SDef : Source_Ptr;
689 TDef : constant Source_Buffer_Ptr :=
690 Source_Text (Get_Source_File_Index (Sloc (E)));
692 begin
693 -- Nothing to do if at outer level
695 if Scope (E) = Standard_Standard then
696 null;
698 -- If scope comes from source, write its name
700 elsif Comes_From_Source (Scope (E)) then
701 Write_Entity_Name (Scope (E));
702 Add_Char_To_Name_Buffer ('.');
704 -- If in wrapper package skip past it
706 elsif Is_Wrapper_Package (Scope (E)) then
707 Write_Entity_Name (Scope (Scope (E)));
708 Add_Char_To_Name_Buffer ('.');
710 -- Otherwise nothing to output (happens in unnamed block statements)
712 else
713 null;
714 end if;
716 -- Loop to output the name
718 -- is this right wrt wide char encodings ??? (no!)
720 SDef := Sloc (E);
721 while TDef (SDef) in '0' .. '9'
722 or else TDef (SDef) >= 'A'
723 or else TDef (SDef) = ASCII.ESC
724 loop
725 Add_Char_To_Name_Buffer (TDef (SDef));
726 SDef := SDef + 1;
727 end loop;
728 end Write_Entity_Name;
730 -- Start of processing for Expand_Source_Info
732 begin
733 -- Integer cases
735 if Nam = Name_Line then
736 Rewrite (N,
737 Make_Integer_Literal (Loc,
738 Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
739 Analyze_And_Resolve (N, Standard_Positive);
741 -- String cases
743 else
744 Name_Len := 0;
746 case Nam is
747 when Name_File =>
748 Get_Decoded_Name_String
749 (Reference_Name (Get_Source_File_Index (Loc)));
751 when Name_Source_Location =>
752 Build_Location_String (Loc);
754 when Name_Enclosing_Entity =>
756 -- Skip enclosing blocks to reach enclosing unit
758 Ent := Current_Scope;
759 while Present (Ent) loop
760 exit when Ekind (Ent) /= E_Block
761 and then Ekind (Ent) /= E_Loop;
762 Ent := Scope (Ent);
763 end loop;
765 -- Ent now points to the relevant defining entity
767 Write_Entity_Name (Ent);
769 when others =>
770 raise Program_Error;
771 end case;
773 Rewrite (N,
774 Make_String_Literal (Loc,
775 Strval => String_From_Name_Buffer));
776 Analyze_And_Resolve (N, Standard_String);
777 end if;
779 Set_Is_Static_Expression (N);
780 end Expand_Source_Info;
782 ---------------------------
783 -- Expand_Unc_Conversion --
784 ---------------------------
786 procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
787 Func : constant Entity_Id := Entity (Name (N));
788 Conv : Node_Id;
789 Ftyp : Entity_Id;
790 Ttyp : Entity_Id;
792 begin
793 -- Rewrite as unchecked conversion node. Note that we must convert
794 -- the operand to the formal type of the input parameter of the
795 -- function, so that the resulting N_Unchecked_Type_Conversion
796 -- call indicates the correct types for Gigi.
798 -- Right now, we only do this if a scalar type is involved. It is
799 -- not clear if it is needed in other cases. If we do attempt to
800 -- do the conversion unconditionally, it crashes 3411-018. To be
801 -- investigated further ???
803 Conv := Relocate_Node (First_Actual (N));
804 Ftyp := Etype (First_Formal (Func));
806 if Is_Scalar_Type (Ftyp) then
807 Conv := Convert_To (Ftyp, Conv);
808 Set_Parent (Conv, N);
809 Analyze_And_Resolve (Conv);
810 end if;
812 -- The instantiation of Unchecked_Conversion creates a wrapper package,
813 -- and the target type is declared as a subtype of the actual. Recover
814 -- the actual, which is the subtype indic. in the subtype declaration
815 -- for the target type. This is semantically correct, and avoids
816 -- anomalies with access subtypes. For entities, leave type as is.
818 -- We do the analysis here, because we do not want the compiler
819 -- to try to optimize or otherwise reorganize the unchecked
820 -- conversion node.
822 Ttyp := Etype (E);
824 if Is_Entity_Name (Conv) then
825 null;
827 elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
828 Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
830 elsif Is_Itype (Ttyp) then
831 Ttyp :=
832 Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
833 else
834 raise Program_Error;
835 end if;
837 Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
838 Set_Etype (N, Ttyp);
839 Set_Analyzed (N);
841 if Nkind (N) = N_Unchecked_Type_Conversion then
842 Expand_N_Unchecked_Type_Conversion (N);
843 end if;
844 end Expand_Unc_Conversion;
846 -----------------------------
847 -- Expand_Unc_Deallocation --
848 -----------------------------
850 -- Generate the following Code :
852 -- if Arg /= null then
853 -- <Finalize_Call> (.., T'Class(Arg.all), ..); -- for controlled types
854 -- Free (Arg);
855 -- Arg := Null;
856 -- end if;
858 -- For a task, we also generate a call to Free_Task to ensure that the
859 -- task itself is freed if it is terminated, ditto for a simple protected
860 -- object, with a call to Finalize_Protection. For composite types that
861 -- have tasks or simple protected objects as components, we traverse the
862 -- structures to find and terminate those components.
864 procedure Expand_Unc_Deallocation (N : Node_Id) is
865 Loc : constant Source_Ptr := Sloc (N);
866 Arg : constant Node_Id := First_Actual (N);
867 Typ : constant Entity_Id := Etype (Arg);
868 Stmts : constant List_Id := New_List;
869 Rtyp : constant Entity_Id := Underlying_Type (Root_Type (Typ));
870 Pool : constant Entity_Id := Associated_Storage_Pool (Rtyp);
872 Desig_T : constant Entity_Id := Designated_Type (Typ);
873 Gen_Code : Node_Id;
874 Free_Node : Node_Id;
875 Deref : Node_Id;
876 Free_Arg : Node_Id;
877 Free_Cod : List_Id;
878 Blk : Node_Id;
880 Arg_Known_Non_Null : constant Boolean := Known_Non_Null (N);
881 -- This captures whether we know the argument to be non-null so that
882 -- we can avoid the test. The reason that we need to capture this is
883 -- that we analyze some generated statements before properly attaching
884 -- them to the tree, and that can disturb current value settings.
886 begin
887 -- Nothing to do if we know the argument is null
889 if Known_Null (N) then
890 return;
891 end if;
893 -- Processing for pointer to controlled type
895 if Needs_Finalization (Desig_T) then
896 Deref :=
897 Make_Explicit_Dereference (Loc,
898 Prefix => Duplicate_Subexpr_No_Checks (Arg));
900 -- If the type is tagged, then we must force dispatching on the
901 -- finalization call because the designated type may not be the
902 -- actual type of the object.
904 if Is_Tagged_Type (Desig_T)
905 and then not Is_Class_Wide_Type (Desig_T)
906 then
907 Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
909 elsif not Is_Tagged_Type (Desig_T) then
911 -- Set type of result, to force a conversion when needed (see
912 -- exp_ch7, Convert_View), given that Deep_Finalize may be
913 -- inherited from the parent type, and we need the type of the
914 -- expression to see whether the conversion is in fact needed.
916 Set_Etype (Deref, Desig_T);
917 end if;
919 Free_Cod :=
920 Make_Final_Call
921 (Ref => Deref,
922 Typ => Desig_T,
923 With_Detach => New_Reference_To (Standard_True, Loc));
925 if Abort_Allowed then
926 Prepend_To (Free_Cod,
927 Build_Runtime_Call (Loc, RE_Abort_Defer));
929 Blk :=
930 Make_Block_Statement (Loc, Handled_Statement_Sequence =>
931 Make_Handled_Sequence_Of_Statements (Loc,
932 Statements => Free_Cod,
933 At_End_Proc =>
934 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc)));
936 -- We now expand the exception (at end) handler. We set a
937 -- temporary parent pointer since we have not attached Blk
938 -- to the tree yet.
940 Set_Parent (Blk, N);
941 Analyze (Blk);
942 Expand_At_End_Handler
943 (Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
944 Append (Blk, Stmts);
946 -- We kill saved current values, since analyzing statements not
947 -- properly attached to the tree can set wrong current values.
949 Kill_Current_Values;
951 else
952 Append_List_To (Stmts, Free_Cod);
953 end if;
954 end if;
956 -- For a task type, call Free_Task before freeing the ATCB
958 if Is_Task_Type (Desig_T) then
959 declare
960 Stat : Node_Id := Prev (N);
961 Nam1 : Node_Id;
962 Nam2 : Node_Id;
964 begin
965 -- An Abort followed by a Free will not do what the user
966 -- expects, because the abort is not immediate. This is
967 -- worth a friendly warning.
969 while Present (Stat)
970 and then not Comes_From_Source (Original_Node (Stat))
971 loop
972 Prev (Stat);
973 end loop;
975 if Present (Stat)
976 and then Nkind (Original_Node (Stat)) = N_Abort_Statement
977 then
978 Stat := Original_Node (Stat);
979 Nam1 := First (Names (Stat));
980 Nam2 := Original_Node (First (Parameter_Associations (N)));
982 if Nkind (Nam1) = N_Explicit_Dereference
983 and then Is_Entity_Name (Prefix (Nam1))
984 and then Is_Entity_Name (Nam2)
985 and then Entity (Prefix (Nam1)) = Entity (Nam2)
986 then
987 Error_Msg_N ("abort may take time to complete?", N);
988 Error_Msg_N ("\deallocation might have no effect?", N);
989 Error_Msg_N ("\safer to wait for termination.?", N);
990 end if;
991 end if;
992 end;
994 Append_To
995 (Stmts, Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
997 -- For composite types that contain tasks, recurse over the structure
998 -- to build the selectors for the task subcomponents.
1000 elsif Has_Task (Desig_T) then
1001 if Is_Record_Type (Desig_T) then
1002 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
1004 elsif Is_Array_Type (Desig_T) then
1005 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
1006 end if;
1007 end if;
1009 -- Same for simple protected types. Eventually call Finalize_Protection
1010 -- before freeing the PO for each protected component.
1012 if Is_Simple_Protected_Type (Desig_T) then
1013 Append_To (Stmts,
1014 Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
1016 elsif Has_Simple_Protected_Object (Desig_T) then
1017 if Is_Record_Type (Desig_T) then
1018 Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
1019 elsif Is_Array_Type (Desig_T) then
1020 Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
1021 end if;
1022 end if;
1024 -- Normal processing for non-controlled types
1026 Free_Arg := Duplicate_Subexpr_No_Checks (Arg);
1027 Free_Node := Make_Free_Statement (Loc, Empty);
1028 Append_To (Stmts, Free_Node);
1029 Set_Storage_Pool (Free_Node, Pool);
1031 -- Attach to tree before analysis of generated subtypes below.
1033 Set_Parent (Stmts, Parent (N));
1035 -- Deal with storage pool
1037 if Present (Pool) then
1039 -- Freeing the secondary stack is meaningless
1041 if Is_RTE (Pool, RE_SS_Pool) then
1042 null;
1044 elsif Is_Class_Wide_Type (Etype (Pool)) then
1046 -- Case of a class-wide pool type: make a dispatching call
1047 -- to Deallocate through the class-wide Deallocate_Any.
1049 Set_Procedure_To_Call (Free_Node,
1050 RTE (RE_Deallocate_Any));
1052 else
1053 -- Case of a specific pool type: make a statically bound call
1055 Set_Procedure_To_Call (Free_Node,
1056 Find_Prim_Op (Etype (Pool), Name_Deallocate));
1057 end if;
1058 end if;
1060 if Present (Procedure_To_Call (Free_Node)) then
1062 -- For all cases of a Deallocate call, the back-end needs to be
1063 -- able to compute the size of the object being freed. This may
1064 -- require some adjustments for objects of dynamic size.
1066 -- If the type is class wide, we generate an implicit type with the
1067 -- right dynamic size, so that the deallocate call gets the right
1068 -- size parameter computed by GIGI. Same for an access to
1069 -- unconstrained packed array.
1071 if Is_Class_Wide_Type (Desig_T)
1072 or else
1073 (Is_Array_Type (Desig_T)
1074 and then not Is_Constrained (Desig_T)
1075 and then Is_Packed (Desig_T))
1076 then
1077 declare
1078 Deref : constant Node_Id :=
1079 Make_Explicit_Dereference (Loc,
1080 Duplicate_Subexpr_No_Checks (Arg));
1081 D_Subtyp : Node_Id;
1082 D_Type : Entity_Id;
1084 begin
1085 Set_Etype (Deref, Typ);
1086 Set_Parent (Deref, Free_Node);
1087 D_Subtyp := Make_Subtype_From_Expr (Deref, Desig_T);
1089 if Nkind (D_Subtyp) in N_Has_Entity then
1090 D_Type := Entity (D_Subtyp);
1092 else
1093 D_Type := Make_Temporary (Loc, 'A');
1094 Insert_Action (Deref,
1095 Make_Subtype_Declaration (Loc,
1096 Defining_Identifier => D_Type,
1097 Subtype_Indication => D_Subtyp));
1098 end if;
1100 -- Force freezing at the point of the dereference. For the
1101 -- class wide case, this avoids having the subtype frozen
1102 -- before the equivalent type.
1104 Freeze_Itype (D_Type, Deref);
1106 Set_Actual_Designated_Subtype (Free_Node, D_Type);
1107 end;
1109 end if;
1110 end if;
1112 -- Ada 2005 (AI-251): In case of abstract interface type we must
1113 -- displace the pointer to reference the base of the object to
1114 -- deallocate its memory, unless we're targetting a VM, in which case
1115 -- no special processing is required.
1117 -- Generate:
1118 -- free (Base_Address (Obj_Ptr))
1120 if Is_Interface (Directly_Designated_Type (Typ))
1121 and then Tagged_Type_Expansion
1122 then
1123 Set_Expression (Free_Node,
1124 Unchecked_Convert_To (Typ,
1125 Make_Function_Call (Loc,
1126 Name => New_Reference_To (RTE (RE_Base_Address), Loc),
1127 Parameter_Associations => New_List (
1128 Unchecked_Convert_To (RTE (RE_Address), Free_Arg)))));
1130 -- Generate:
1131 -- free (Obj_Ptr)
1133 else
1134 Set_Expression (Free_Node, Free_Arg);
1135 end if;
1137 -- Only remaining step is to set result to null, or generate a
1138 -- raise of constraint error if the target object is "not null".
1140 if Can_Never_Be_Null (Etype (Arg)) then
1141 Append_To (Stmts,
1142 Make_Raise_Constraint_Error (Loc,
1143 Reason => CE_Access_Check_Failed));
1145 else
1146 declare
1147 Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
1148 begin
1149 Set_Assignment_OK (Lhs);
1150 Append_To (Stmts,
1151 Make_Assignment_Statement (Loc,
1152 Name => Lhs,
1153 Expression => Make_Null (Loc)));
1154 end;
1155 end if;
1157 -- If we know the argument is non-null, then make a block statement
1158 -- that contains the required statements, no need for a test.
1160 if Arg_Known_Non_Null then
1161 Gen_Code :=
1162 Make_Block_Statement (Loc,
1163 Handled_Statement_Sequence =>
1164 Make_Handled_Sequence_Of_Statements (Loc,
1165 Statements => Stmts));
1167 -- If the argument may be null, wrap the statements inside an IF that
1168 -- does an explicit test to exclude the null case.
1170 else
1171 Gen_Code :=
1172 Make_Implicit_If_Statement (N,
1173 Condition =>
1174 Make_Op_Ne (Loc,
1175 Left_Opnd => Duplicate_Subexpr (Arg),
1176 Right_Opnd => Make_Null (Loc)),
1177 Then_Statements => Stmts);
1178 end if;
1180 -- Rewrite the call
1182 Rewrite (N, Gen_Code);
1183 Analyze (N);
1184 end Expand_Unc_Deallocation;
1186 -----------------------
1187 -- Expand_To_Address --
1188 -----------------------
1190 procedure Expand_To_Address (N : Node_Id) is
1191 Loc : constant Source_Ptr := Sloc (N);
1192 Arg : constant Node_Id := First_Actual (N);
1193 Obj : Node_Id;
1195 begin
1196 Remove_Side_Effects (Arg);
1198 Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
1200 Rewrite (N,
1201 Make_Conditional_Expression (Loc,
1202 Expressions => New_List (
1203 Make_Op_Eq (Loc,
1204 Left_Opnd => New_Copy_Tree (Arg),
1205 Right_Opnd => Make_Null (Loc)),
1206 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
1207 Make_Attribute_Reference (Loc,
1208 Prefix => Obj,
1209 Attribute_Name => Name_Address))));
1211 Analyze_And_Resolve (N, RTE (RE_Address));
1212 end Expand_To_Address;
1214 -----------------------
1215 -- Expand_To_Pointer --
1216 -----------------------
1218 procedure Expand_To_Pointer (N : Node_Id) is
1219 Arg : constant Node_Id := First_Actual (N);
1221 begin
1222 Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
1223 Analyze (N);
1224 end Expand_To_Pointer;
1226 end Exp_Intr;