1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Casing; use Casing;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Ch11; use Exp_Ch11;
32 with Exp_Tss; use Exp_Tss;
33 with Exp_Util; use Exp_Util;
34 with Expander; use Expander;
35 with Namet; use Namet;
36 with Nlists; use Nlists;
37 with Nmake; use Nmake;
39 with Restrict; use Restrict;
40 with Rident; use Rident;
41 with Rtsfind; use Rtsfind;
43 with Sem_Res; use Sem_Res;
44 with Sem_Util; use Sem_Util;
45 with Sinfo; use Sinfo;
46 with Sinput; use Sinput;
47 with Snames; use Snames;
48 with Stringt; use Stringt;
49 with Stand; use Stand;
50 with Targparm; use Targparm;
51 with Tbuild; use Tbuild;
52 with Uintp; use Uintp;
54 package body Exp_Prag is
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
60 function Arg1 (N : Node_Id) return Node_Id;
61 function Arg2 (N : Node_Id) return Node_Id;
62 function Arg3 (N : Node_Id) return Node_Id;
63 -- Obtain specified pragma argument expression
65 procedure Expand_Pragma_Abort_Defer (N : Node_Id);
66 procedure Expand_Pragma_Check (N : Node_Id);
67 procedure Expand_Pragma_Common_Object (N : Node_Id);
68 procedure Expand_Pragma_Import_Or_Interface (N : Node_Id);
69 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id);
70 procedure Expand_Pragma_Inspection_Point (N : Node_Id);
71 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id);
72 procedure Expand_Pragma_Psect_Object (N : Node_Id);
73 procedure Expand_Pragma_Relative_Deadline (N : Node_Id);
79 function Arg1 (N : Node_Id) return Node_Id is
80 Arg : constant Node_Id := First (Pragma_Argument_Associations (N));
83 and then Nkind (Arg) = N_Pragma_Argument_Association
85 return Expression (Arg);
95 function Arg2 (N : Node_Id) return Node_Id is
96 Arg1 : constant Node_Id := First (Pragma_Argument_Associations (N));
104 Arg : constant Node_Id := Next (Arg1);
107 and then Nkind (Arg) = N_Pragma_Argument_Association
109 return Expression (Arg);
121 function Arg3 (N : Node_Id) return Node_Id is
122 Arg1 : constant Node_Id := First (Pragma_Argument_Associations (N));
130 Arg : Node_Id := Next (Arg1);
139 and then Nkind (Arg) = N_Pragma_Argument_Association
141 return Expression (Arg);
150 ---------------------
151 -- Expand_N_Pragma --
152 ---------------------
154 procedure Expand_N_Pragma (N : Node_Id) is
155 Pname : constant Name_Id := Pragma_Name (N);
158 -- Note: we may have a pragma whose Pragma_Identifier field is not a
159 -- recognized pragma, and we must ignore it at this stage.
161 if Is_Pragma_Name (Pname) then
162 case Get_Pragma_Id (Pname) is
164 -- Pragmas requiring special expander action
166 when Pragma_Abort_Defer =>
167 Expand_Pragma_Abort_Defer (N);
170 Expand_Pragma_Check (N);
172 when Pragma_Common_Object =>
173 Expand_Pragma_Common_Object (N);
175 when Pragma_Export_Exception =>
176 Expand_Pragma_Import_Export_Exception (N);
178 when Pragma_Import =>
179 Expand_Pragma_Import_Or_Interface (N);
181 when Pragma_Import_Exception =>
182 Expand_Pragma_Import_Export_Exception (N);
184 when Pragma_Inspection_Point =>
185 Expand_Pragma_Inspection_Point (N);
187 when Pragma_Interface =>
188 Expand_Pragma_Import_Or_Interface (N);
190 when Pragma_Interrupt_Priority =>
191 Expand_Pragma_Interrupt_Priority (N);
193 when Pragma_Psect_Object =>
194 Expand_Pragma_Psect_Object (N);
196 when Pragma_Relative_Deadline =>
197 Expand_Pragma_Relative_Deadline (N);
199 -- All other pragmas need no expander action
207 -------------------------------
208 -- Expand_Pragma_Abort_Defer --
209 -------------------------------
211 -- An Abort_Defer pragma appears as the first statement in a handled
212 -- statement sequence (right after the begin). It defers aborts for
213 -- the entire statement sequence, but not for any declarations or
214 -- handlers (if any) associated with this statement sequence.
216 -- The transformation is to transform
218 -- pragma Abort_Defer;
227 -- when all others =>
228 -- Abort_Undefer.all;
231 -- Abort_Undefer_Direct;
234 procedure Expand_Pragma_Abort_Defer (N : Node_Id) is
235 Loc : constant Source_Ptr := Sloc (N);
239 Blk : constant Entity_Id :=
240 New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
243 Stms := New_List (Build_Runtime_Call (Loc, RE_Abort_Defer));
246 Stm := Remove_Next (N);
252 Make_Handled_Sequence_Of_Statements (Loc,
255 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
258 Make_Block_Statement (Loc,
259 Handled_Statement_Sequence => HSS));
261 Set_Scope (Blk, Current_Scope);
262 Set_Etype (Blk, Standard_Void_Type);
263 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
264 Expand_At_End_Handler (HSS, Blk);
266 end Expand_Pragma_Abort_Defer;
268 --------------------------
269 -- Expand_Pragma_Check --
270 --------------------------
272 procedure Expand_Pragma_Check (N : Node_Id) is
273 Loc : constant Source_Ptr := Sloc (N);
274 Cond : constant Node_Id := Arg2 (N);
275 Nam : constant Name_Id := Chars (Arg1 (N));
279 -- We already know that this check is enabled, because otherwise the
280 -- semantic pass dealt with rewriting the assertion (see Sem_Prag)
282 -- Since this check is enabled, we rewrite the pragma into a
283 -- corresponding if statement, and then analyze the statement
285 -- The normal case expansion transforms:
287 -- pragma Check (name, condition [,message]);
291 -- if not condition then
292 -- System.Assertions.Raise_Assert_Failure (Str);
295 -- where Str is the message if one is present, or the default of
296 -- name failed at file:line if no message is given (the "name failed
297 -- at" is omitted for name = Assertion, since it is redundant, given
298 -- that the name of the exception is Assert_Failure.
300 -- An alternative expansion is used when the No_Exception_Propagation
301 -- restriction is active and there is a local Assert_Failure handler.
302 -- This is not a common combination of circumstances, but it occurs in
303 -- the context of Aunit and the zero footprint profile. In this case we
306 -- if not condition then
307 -- raise Assert_Failure;
310 -- This will then be transformed into a goto, and the local handler will
311 -- be able to handle the assert error (which would not be the case if a
312 -- call is made to the Raise_Assert_Failure procedure).
314 -- Note that the reason we do not always generate a direct raise is that
315 -- the form in which the procedure is called allows for more efficient
316 -- breakpointing of assertion errors.
318 -- Generate the appropriate if statement. Note that we consider this to
319 -- be an explicit conditional in the source, not an implicit if, so we
320 -- do not call Make_Implicit_If_Statement.
322 -- Case where we generate a direct raise
325 or else Restriction_Active (No_Exception_Propagation))
326 and then Present (Find_Local_Handler (RTE (RE_Assert_Failure), N))
329 Make_If_Statement (Loc,
333 Then_Statements => New_List (
334 Make_Raise_Statement (Loc,
336 New_Reference_To (RTE (RE_Assert_Failure), Loc)))));
338 -- Case where we call the procedure
341 -- First, we need to prepare the string argument
343 -- If we have a message given, use it
345 if Present (Arg3 (N)) then
348 -- Otherwise string is "name failed at location" except in the case
349 -- of Assertion where "name failed at" is omitted.
352 if Nam = Name_Assertion then
355 Get_Name_String (Nam);
356 Set_Casing (Identifier_Casing (Current_Source_File));
357 Add_Str_To_Name_Buffer (" failed at ");
360 Build_Location_String (Loc);
362 Make_String_Literal (Loc,
363 Strval => String_From_Name_Buffer);
366 -- Now rewrite as an if statement
369 Make_If_Statement (Loc,
373 Then_Statements => New_List (
374 Make_Procedure_Call_Statement (Loc,
376 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
377 Parameter_Associations => New_List (Msg)))));
382 -- If new condition is always false, give a warning
384 if Warn_On_Assertion_Failure
385 and then Nkind (N) = N_Procedure_Call_Statement
386 and then Is_RTE (Entity (Name (N)), RE_Raise_Assert_Failure)
388 -- If original condition was a Standard.False, we assume that this is
389 -- indeed intended to raise assert error and no warning is required.
391 if Is_Entity_Name (Original_Node (Cond))
392 and then Entity (Original_Node (Cond)) = Standard_False
395 elsif Nam = Name_Assertion then
396 Error_Msg_N ("?assertion will fail at run-time", N);
398 Error_Msg_N ("?check will fail at run time", N);
401 end Expand_Pragma_Check;
403 ---------------------------------
404 -- Expand_Pragma_Common_Object --
405 ---------------------------------
407 -- Use a machine attribute to replicate semantic effect in DEC Ada
409 -- pragma Machine_Attribute (intern_name, "common_object", extern_name);
411 -- For now we do nothing with the size attribute ???
413 -- Note: Psect_Object shares this processing
415 procedure Expand_Pragma_Common_Object (N : Node_Id) is
416 Loc : constant Source_Ptr := Sloc (N);
418 Internal : constant Node_Id := Arg1 (N);
419 External : constant Node_Id := Arg2 (N);
422 -- Psect value upper cased as string literal
424 Iloc : constant Source_Ptr := Sloc (Internal);
425 Eloc : constant Source_Ptr := Sloc (External);
429 -- Acquire Psect value and fold to upper case
431 if Present (External) then
432 if Nkind (External) = N_String_Literal then
433 String_To_Name_Buffer (Strval (External));
435 Get_Name_String (Chars (External));
441 Make_String_Literal (Eloc,
442 Strval => String_From_Name_Buffer);
445 Get_Name_String (Chars (Internal));
448 Make_String_Literal (Iloc,
449 Strval => String_From_Name_Buffer);
452 Ploc := Sloc (Psect);
456 Insert_After_And_Analyze (N,
458 Chars => Name_Machine_Attribute,
459 Pragma_Argument_Associations => New_List (
460 Make_Pragma_Argument_Association (Iloc,
461 Expression => New_Copy_Tree (Internal)),
462 Make_Pragma_Argument_Association (Eloc,
464 Make_String_Literal (Sloc => Ploc,
465 Strval => "common_object")),
466 Make_Pragma_Argument_Association (Ploc,
467 Expression => New_Copy_Tree (Psect)))));
469 end Expand_Pragma_Common_Object;
471 ---------------------------------------
472 -- Expand_Pragma_Import_Or_Interface --
473 ---------------------------------------
475 -- When applied to a variable, the default initialization must not be
476 -- done. As it is already done when the pragma is found, we just get rid
477 -- of the call the initialization procedure which followed the object
478 -- declaration. The call is inserted after the declaration, but validity
479 -- checks may also have been inserted and the initialization call does
480 -- not necessarily appear immediately after the object declaration.
482 -- We can't use the freezing mechanism for this purpose, since we
483 -- have to elaborate the initialization expression when it is first
484 -- seen (i.e. this elaboration cannot be deferred to the freeze point).
486 procedure Expand_Pragma_Import_Or_Interface (N : Node_Id) is
487 Def_Id : constant Entity_Id := Entity (Arg2 (N));
492 if Ekind (Def_Id) = E_Variable then
493 Typ := Etype (Def_Id);
495 -- Iterate from declaration of object to import pragma, to find
496 -- generated initialization call for object, if any.
498 Init_Call := Next (Parent (Def_Id));
499 while Present (Init_Call) and then Init_Call /= N loop
500 if Has_Non_Null_Base_Init_Proc (Typ)
501 and then Nkind (Init_Call) = N_Procedure_Call_Statement
502 and then Is_Entity_Name (Name (Init_Call))
503 and then Entity (Name (Init_Call)) = Base_Init_Proc (Typ)
512 -- Any default initialization expression should be removed
513 -- (e.g., null defaults for access objects, zero initialization
514 -- of packed bit arrays). Imported objects aren't allowed to
515 -- have explicit initialization, so the expression must have
516 -- been generated by the compiler.
519 and then Present (Expression (Parent (Def_Id)))
521 Set_Expression (Parent (Def_Id), Empty);
524 end Expand_Pragma_Import_Or_Interface;
526 -------------------------------------------
527 -- Expand_Pragma_Import_Export_Exception --
528 -------------------------------------------
530 -- For a VMS exception fix up the language field with "VMS"
531 -- instead of "Ada" (gigi needs this), create a constant that will be the
532 -- value of the VMS condition code and stuff the Interface_Name field
533 -- with the unexpanded name of the exception (if not already set).
534 -- For a Ada exception, just stuff the Interface_Name field
535 -- with the unexpanded name of the exception (if not already set).
537 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id) is
539 -- This pragma is only effective on OpenVMS systems, it was ignored
540 -- on non-VMS systems, and we need to ignore it here as well.
542 if not OpenVMS_On_Target then
547 Id : constant Entity_Id := Entity (Arg1 (N));
548 Call : constant Node_Id := Register_Exception_Call (Id);
549 Loc : constant Source_Ptr := Sloc (N);
552 if Present (Call) then
554 Excep_Internal : constant Node_Id :=
555 Make_Defining_Identifier
556 (Loc, New_Internal_Name ('V'));
557 Export_Pragma : Node_Id;
558 Excep_Alias : Node_Id;
559 Excep_Object : Node_Id;
560 Excep_Image : String_Id;
568 if Present (Interface_Name (Id)) then
569 Excep_Image := Strval (Interface_Name (Id));
571 Get_Name_String (Chars (Id));
573 Excep_Image := String_From_Name_Buffer;
576 Exdata := Component_Associations (Expression (Parent (Id)));
578 if Is_VMS_Exception (Id) then
579 Lang1 := Next (First (Exdata));
580 Lang2 := Next (Lang1);
581 Lang3 := Next (Lang2);
583 Rewrite (Expression (Lang1),
584 Make_Character_Literal (Loc,
586 Char_Literal_Value =>
587 UI_From_Int (Character'Pos ('V'))));
588 Analyze (Expression (Lang1));
590 Rewrite (Expression (Lang2),
591 Make_Character_Literal (Loc,
593 Char_Literal_Value =>
594 UI_From_Int (Character'Pos ('M'))));
595 Analyze (Expression (Lang2));
597 Rewrite (Expression (Lang3),
598 Make_Character_Literal (Loc,
600 Char_Literal_Value =>
601 UI_From_Int (Character'Pos ('S'))));
602 Analyze (Expression (Lang3));
604 if Exception_Code (Id) /= No_Uint then
606 Make_Integer_Literal (Loc,
607 Intval => Exception_Code (Id));
610 Make_Object_Declaration (Loc,
611 Defining_Identifier => Excep_Internal,
613 New_Reference_To (RTE (RE_Exception_Code), Loc));
615 Insert_Action (N, Excep_Object);
616 Analyze (Excep_Object);
620 (UI_To_Int (Exception_Code (Id)) / 8 * 8);
627 (Make_Pragma_Argument_Association
630 New_Reference_To (Excep_Internal, Loc)),
632 Make_Pragma_Argument_Association
637 Strval => End_String))));
639 Insert_Action (N, Excep_Alias);
640 Analyze (Excep_Alias);
647 (Make_Pragma_Argument_Association
649 Expression => Make_Identifier (Loc, Name_C)),
651 Make_Pragma_Argument_Association
654 New_Reference_To (Excep_Internal, Loc)),
656 Make_Pragma_Argument_Association
661 Strval => Excep_Image)),
663 Make_Pragma_Argument_Association
668 Strval => Excep_Image))));
670 Insert_Action (N, Export_Pragma);
671 Analyze (Export_Pragma);
675 Unchecked_Convert_To (RTE (RE_Exception_Code),
676 Make_Function_Call (Loc,
678 New_Reference_To (RTE (RE_Import_Value), Loc),
679 Parameter_Associations => New_List
680 (Make_String_Literal (Loc,
681 Strval => Excep_Image))));
685 Make_Procedure_Call_Statement (Loc,
686 Name => New_Reference_To
687 (RTE (RE_Register_VMS_Exception), Loc),
688 Parameter_Associations => New_List (
690 Unchecked_Convert_To (RTE (RE_Exception_Data_Ptr),
691 Make_Attribute_Reference (Loc,
692 Prefix => New_Occurrence_Of (Id, Loc),
693 Attribute_Name => Name_Unrestricted_Access)))));
695 Analyze_And_Resolve (Code, RTE (RE_Exception_Code));
699 if No (Interface_Name (Id)) then
700 Set_Interface_Name (Id,
703 Strval => Excep_Image));
708 end Expand_Pragma_Import_Export_Exception;
710 ------------------------------------
711 -- Expand_Pragma_Inspection_Point --
712 ------------------------------------
714 -- If no argument is given, then we supply a default argument list that
715 -- includes all objects declared at the source level in all subprograms
716 -- that enclose the inspection point pragma.
718 procedure Expand_Pragma_Inspection_Point (N : Node_Id) is
719 Loc : constant Source_Ptr := Sloc (N);
726 if No (Pragma_Argument_Associations (N)) then
730 while S /= Standard_Standard loop
731 E := First_Entity (S);
732 while Present (E) loop
733 if Comes_From_Source (E)
734 and then Is_Object (E)
735 and then not Is_Entry_Formal (E)
736 and then Ekind (E) /= E_Component
737 and then Ekind (E) /= E_Discriminant
738 and then Ekind (E) /= E_Generic_In_Parameter
739 and then Ekind (E) /= E_Generic_In_Out_Parameter
742 Make_Pragma_Argument_Association (Loc,
743 Expression => New_Occurrence_Of (E, Loc)));
752 Set_Pragma_Argument_Associations (N, A);
755 -- Expand the arguments of the pragma. Expanding an entity reference
756 -- is a noop, except in a protected operation, where a reference may
757 -- have to be transformed into a reference to the corresponding prival.
758 -- Are there other pragmas that may require this ???
760 Assoc := First (Pragma_Argument_Associations (N));
762 while Present (Assoc) loop
763 Expand (Expression (Assoc));
766 end Expand_Pragma_Inspection_Point;
768 --------------------------------------
769 -- Expand_Pragma_Interrupt_Priority --
770 --------------------------------------
772 -- Supply default argument if none exists (System.Interrupt_Priority'Last)
774 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id) is
775 Loc : constant Source_Ptr := Sloc (N);
778 if No (Pragma_Argument_Associations (N)) then
779 Set_Pragma_Argument_Associations (N, New_List (
780 Make_Pragma_Argument_Association (Loc,
782 Make_Attribute_Reference (Loc,
784 New_Occurrence_Of (RTE (RE_Interrupt_Priority), Loc),
785 Attribute_Name => Name_Last))));
787 end Expand_Pragma_Interrupt_Priority;
789 --------------------------------
790 -- Expand_Pragma_Psect_Object --
791 --------------------------------
793 -- Convert to Common_Object, and expand the resulting pragma
795 procedure Expand_Pragma_Psect_Object (N : Node_Id)
796 renames Expand_Pragma_Common_Object;
798 -------------------------------------
799 -- Expand_Pragma_Relative_Deadline --
800 -------------------------------------
802 procedure Expand_Pragma_Relative_Deadline (N : Node_Id) is
803 P : constant Node_Id := Parent (N);
804 Loc : constant Source_Ptr := Sloc (N);
807 -- Expand the pragma only in the case of the main subprogram. For tasks
808 -- the expansion is done in exp_ch9. Generate a call to Set_Deadline
809 -- at Clock plus the relative deadline specified in the pragma. Time
810 -- values are translated into Duration to allow for non-private
811 -- addition operation.
813 if Nkind (P) = N_Subprogram_Body then
816 Make_Procedure_Call_Statement (Loc,
817 Name => New_Reference_To (RTE (RE_Set_Deadline), Loc),
818 Parameter_Associations => New_List (
819 Unchecked_Convert_To (RTE (RO_RT_Time),
822 Make_Function_Call (Loc,
823 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
824 New_List (Make_Function_Call (Loc,
825 New_Reference_To (RTE (RE_Clock), Loc)))),
827 Unchecked_Convert_To (Standard_Duration, Arg1 (N)))))));
831 end Expand_Pragma_Relative_Deadline;