1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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_Util; use Exp_Util;
33 with Expander; use Expander;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
38 with Restrict; use Restrict;
39 with Rident; use Rident;
40 with Rtsfind; use Rtsfind;
42 with Sem_Res; use Sem_Res;
43 with Sem_Util; use Sem_Util;
44 with Sinfo; use Sinfo;
45 with Sinput; use Sinput;
46 with Snames; use Snames;
47 with Stringt; use Stringt;
48 with Stand; use Stand;
49 with Targparm; use Targparm;
50 with Tbuild; use Tbuild;
51 with Uintp; use Uintp;
53 package body Exp_Prag is
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
59 function Arg1 (N : Node_Id) return Node_Id;
60 function Arg2 (N : Node_Id) return Node_Id;
61 function Arg3 (N : Node_Id) return Node_Id;
62 -- Obtain specified pragma argument expression
64 procedure Expand_Pragma_Abort_Defer (N : Node_Id);
65 procedure Expand_Pragma_Check (N : Node_Id);
66 procedure Expand_Pragma_Common_Object (N : Node_Id);
67 procedure Expand_Pragma_Import_Or_Interface (N : Node_Id);
68 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id);
69 procedure Expand_Pragma_Inspection_Point (N : Node_Id);
70 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id);
71 procedure Expand_Pragma_Psect_Object (N : Node_Id);
72 procedure Expand_Pragma_Relative_Deadline (N : Node_Id);
78 function Arg1 (N : Node_Id) return Node_Id is
79 Arg : constant Node_Id := First (Pragma_Argument_Associations (N));
82 and then Nkind (Arg) = N_Pragma_Argument_Association
84 return Expression (Arg);
94 function Arg2 (N : Node_Id) return Node_Id is
95 Arg1 : constant Node_Id := First (Pragma_Argument_Associations (N));
103 Arg : constant Node_Id := Next (Arg1);
106 and then Nkind (Arg) = N_Pragma_Argument_Association
108 return Expression (Arg);
120 function Arg3 (N : Node_Id) return Node_Id is
121 Arg1 : constant Node_Id := First (Pragma_Argument_Associations (N));
129 Arg : Node_Id := Next (Arg1);
138 and then Nkind (Arg) = N_Pragma_Argument_Association
140 return Expression (Arg);
149 ---------------------
150 -- Expand_N_Pragma --
151 ---------------------
153 procedure Expand_N_Pragma (N : Node_Id) is
154 Pname : constant Name_Id := Pragma_Name (N);
157 -- Note: we may have a pragma whose Pragma_Identifier field is not a
158 -- recognized pragma, and we must ignore it at this stage.
160 if Is_Pragma_Name (Pname) then
161 case Get_Pragma_Id (Pname) is
163 -- Pragmas requiring special expander action
165 when Pragma_Abort_Defer =>
166 Expand_Pragma_Abort_Defer (N);
169 Expand_Pragma_Check (N);
171 when Pragma_Common_Object =>
172 Expand_Pragma_Common_Object (N);
174 when Pragma_Export_Exception =>
175 Expand_Pragma_Import_Export_Exception (N);
177 when Pragma_Import =>
178 Expand_Pragma_Import_Or_Interface (N);
180 when Pragma_Import_Exception =>
181 Expand_Pragma_Import_Export_Exception (N);
183 when Pragma_Inspection_Point =>
184 Expand_Pragma_Inspection_Point (N);
186 when Pragma_Interface =>
187 Expand_Pragma_Import_Or_Interface (N);
189 when Pragma_Interrupt_Priority =>
190 Expand_Pragma_Interrupt_Priority (N);
192 when Pragma_Psect_Object =>
193 Expand_Pragma_Psect_Object (N);
195 when Pragma_Relative_Deadline =>
196 Expand_Pragma_Relative_Deadline (N);
198 -- All other pragmas need no expander action
206 -------------------------------
207 -- Expand_Pragma_Abort_Defer --
208 -------------------------------
210 -- An Abort_Defer pragma appears as the first statement in a handled
211 -- statement sequence (right after the begin). It defers aborts for
212 -- the entire statement sequence, but not for any declarations or
213 -- handlers (if any) associated with this statement sequence.
215 -- The transformation is to transform
217 -- pragma Abort_Defer;
226 -- when all others =>
227 -- Abort_Undefer.all;
230 -- Abort_Undefer_Direct;
233 procedure Expand_Pragma_Abort_Defer (N : Node_Id) is
234 Loc : constant Source_Ptr := Sloc (N);
238 Blk : constant Entity_Id :=
239 New_Internal_Entity (E_Block, Current_Scope, Sloc (N), 'B');
242 Stms := New_List (Build_Runtime_Call (Loc, RE_Abort_Defer));
245 Stm := Remove_Next (N);
251 Make_Handled_Sequence_Of_Statements (Loc,
254 New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
257 Make_Block_Statement (Loc,
258 Handled_Statement_Sequence => HSS));
260 Set_Scope (Blk, Current_Scope);
261 Set_Etype (Blk, Standard_Void_Type);
262 Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
263 Expand_At_End_Handler (HSS, Blk);
265 end Expand_Pragma_Abort_Defer;
267 --------------------------
268 -- Expand_Pragma_Check --
269 --------------------------
271 procedure Expand_Pragma_Check (N : Node_Id) is
272 Cond : constant Node_Id := Arg2 (N);
273 Loc : constant Source_Ptr := Sloc (Cond);
274 Nam : constant Name_Id := Chars (Arg1 (N));
278 -- We already know that this check is enabled, because otherwise the
279 -- semantic pass dealt with rewriting the assertion (see Sem_Prag)
281 -- Since this check is enabled, we rewrite the pragma into a
282 -- corresponding if statement, and then analyze the statement
284 -- The normal case expansion transforms:
286 -- pragma Check (name, condition [,message]);
290 -- if not condition then
291 -- System.Assertions.Raise_Assert_Failure (Str);
294 -- where Str is the message if one is present, or the default of
295 -- name failed at file:line if no message is given (the "name failed
296 -- at" is omitted for name = Assertion, since it is redundant, given
297 -- that the name of the exception is Assert_Failure.)
299 -- An alternative expansion is used when the No_Exception_Propagation
300 -- restriction is active and there is a local Assert_Failure handler.
301 -- This is not a common combination of circumstances, but it occurs in
302 -- the context of Aunit and the zero footprint profile. In this case we
305 -- if not condition then
306 -- raise Assert_Failure;
309 -- This will then be transformed into a goto, and the local handler will
310 -- be able to handle the assert error (which would not be the case if a
311 -- call is made to the Raise_Assert_Failure procedure).
313 -- We also generate the direct raise if the Suppress_Exception_Locations
314 -- is active, since we don't want to generate messages in this case.
316 -- Note that the reason we do not always generate a direct raise is that
317 -- the form in which the procedure is called allows for more efficient
318 -- breakpointing of assertion errors.
320 -- Generate the appropriate if statement. Note that we consider this to
321 -- be an explicit conditional in the source, not an implicit if, so we
322 -- do not call Make_Implicit_If_Statement.
324 -- In formal verification mode, we keep the pragma check in the code,
325 -- and its enclosed expression is not expanded. This requires that no
326 -- transient scope is introduced for pragma check in this mode in
327 -- Exp_Ch7.Establish_Transient_Scope.
333 -- Case where we generate a direct raise
335 if ((Debug_Flag_Dot_G
336 or else Restriction_Active (No_Exception_Propagation))
337 and then Present (Find_Local_Handler (RTE (RE_Assert_Failure), N)))
338 or else (Opt.Exception_Locations_Suppressed and then No (Arg3 (N)))
341 Make_If_Statement (Loc,
345 Then_Statements => New_List (
346 Make_Raise_Statement (Loc,
348 New_Reference_To (RTE (RE_Assert_Failure), Loc)))));
350 -- Case where we call the procedure
353 -- If we have a message given, use it
355 if Present (Arg3 (N)) then
356 Msg := Get_Pragma_Arg (Arg3 (N));
358 -- Here we have no string, so prepare one
362 Msg_Loc : constant String := Build_Location_String (Loc);
367 -- For Assert, we just use the location
369 if Nam = Name_Assertion then
372 -- For predicate, we generate the string "predicate failed
373 -- at yyy". We prefer all lower case for predicate.
375 elsif Nam = Name_Predicate then
376 Add_Str_To_Name_Buffer ("predicate failed at ");
378 -- For special case of Precondition/Postcondition the string is
379 -- "failed xx from yy" where xx is precondition/postcondition
380 -- in all lower case. The reason for this different wording is
381 -- that the failure is not at the point of occurrence of the
382 -- pragma, unlike the other Check cases.
384 elsif Nam = Name_Precondition
386 Nam = Name_Postcondition
388 Get_Name_String (Nam);
389 Insert_Str_In_Name_Buffer ("failed ", 1);
390 Add_Str_To_Name_Buffer (" from ");
392 -- For all other checks, the string is "xxx failed at yyy"
393 -- where xxx is the check name with current source file casing.
396 Get_Name_String (Nam);
397 Set_Casing (Identifier_Casing (Current_Source_File));
398 Add_Str_To_Name_Buffer (" failed at ");
401 -- In all cases, add location string
403 Add_Str_To_Name_Buffer (Msg_Loc);
407 Msg := Make_String_Literal (Loc, Name_Buffer (1 .. Name_Len));
411 -- Now rewrite as an if statement
414 Make_If_Statement (Loc,
418 Then_Statements => New_List (
419 Make_Procedure_Call_Statement (Loc,
421 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
422 Parameter_Associations => New_List (Relocate_Node (Msg))))));
427 -- If new condition is always false, give a warning
429 if Warn_On_Assertion_Failure
430 and then Nkind (N) = N_Procedure_Call_Statement
431 and then Is_RTE (Entity (Name (N)), RE_Raise_Assert_Failure)
433 -- If original condition was a Standard.False, we assume that this is
434 -- indeed intended to raise assert error and no warning is required.
436 if Is_Entity_Name (Original_Node (Cond))
437 and then Entity (Original_Node (Cond)) = Standard_False
440 elsif Nam = Name_Assertion then
441 Error_Msg_N ("?assertion will fail at run time", N);
443 Error_Msg_N ("?check will fail at run time", N);
446 end Expand_Pragma_Check;
448 ---------------------------------
449 -- Expand_Pragma_Common_Object --
450 ---------------------------------
452 -- Use a machine attribute to replicate semantic effect in DEC Ada
454 -- pragma Machine_Attribute (intern_name, "common_object", extern_name);
456 -- For now we do nothing with the size attribute ???
458 -- Note: Psect_Object shares this processing
460 procedure Expand_Pragma_Common_Object (N : Node_Id) is
461 Loc : constant Source_Ptr := Sloc (N);
463 Internal : constant Node_Id := Arg1 (N);
464 External : constant Node_Id := Arg2 (N);
467 -- Psect value upper cased as string literal
469 Iloc : constant Source_Ptr := Sloc (Internal);
470 Eloc : constant Source_Ptr := Sloc (External);
474 -- Acquire Psect value and fold to upper case
476 if Present (External) then
477 if Nkind (External) = N_String_Literal then
478 String_To_Name_Buffer (Strval (External));
480 Get_Name_String (Chars (External));
486 Make_String_Literal (Eloc,
487 Strval => String_From_Name_Buffer);
490 Get_Name_String (Chars (Internal));
493 Make_String_Literal (Iloc,
494 Strval => String_From_Name_Buffer);
497 Ploc := Sloc (Psect);
501 Insert_After_And_Analyze (N,
503 Chars => Name_Machine_Attribute,
504 Pragma_Argument_Associations => New_List (
505 Make_Pragma_Argument_Association (Iloc,
506 Expression => New_Copy_Tree (Internal)),
507 Make_Pragma_Argument_Association (Eloc,
509 Make_String_Literal (Sloc => Ploc,
510 Strval => "common_object")),
511 Make_Pragma_Argument_Association (Ploc,
512 Expression => New_Copy_Tree (Psect)))));
514 end Expand_Pragma_Common_Object;
516 ---------------------------------------
517 -- Expand_Pragma_Import_Or_Interface --
518 ---------------------------------------
520 -- When applied to a variable, the default initialization must not be
521 -- done. As it is already done when the pragma is found, we just get rid
522 -- of the call the initialization procedure which followed the object
523 -- declaration. The call is inserted after the declaration, but validity
524 -- checks may also have been inserted and the initialization call does
525 -- not necessarily appear immediately after the object declaration.
527 -- We can't use the freezing mechanism for this purpose, since we
528 -- have to elaborate the initialization expression when it is first
529 -- seen (i.e. this elaboration cannot be deferred to the freeze point).
531 procedure Expand_Pragma_Import_Or_Interface (N : Node_Id) is
532 Def_Id : constant Entity_Id := Entity (Arg2 (N));
536 if Ekind (Def_Id) = E_Variable then
538 -- Find generated initialization call for object, if any
540 Init_Call := Find_Init_Call (Def_Id, Rep_Clause => N);
541 if Present (Init_Call) then
545 -- Any default initialization expression should be removed
546 -- (e.g., null defaults for access objects, zero initialization
547 -- of packed bit arrays). Imported objects aren't allowed to
548 -- have explicit initialization, so the expression must have
549 -- been generated by the compiler.
551 if No (Init_Call) and then Present (Expression (Parent (Def_Id))) then
552 Set_Expression (Parent (Def_Id), Empty);
555 end Expand_Pragma_Import_Or_Interface;
557 -------------------------------------------
558 -- Expand_Pragma_Import_Export_Exception --
559 -------------------------------------------
561 -- For a VMS exception fix up the language field with "VMS"
562 -- instead of "Ada" (gigi needs this), create a constant that will be the
563 -- value of the VMS condition code and stuff the Interface_Name field
564 -- with the unexpanded name of the exception (if not already set).
565 -- For a Ada exception, just stuff the Interface_Name field
566 -- with the unexpanded name of the exception (if not already set).
568 procedure Expand_Pragma_Import_Export_Exception (N : Node_Id) is
570 -- This pragma is only effective on OpenVMS systems, it was ignored
571 -- on non-VMS systems, and we need to ignore it here as well.
573 if not OpenVMS_On_Target then
578 Id : constant Entity_Id := Entity (Arg1 (N));
579 Call : constant Node_Id := Register_Exception_Call (Id);
580 Loc : constant Source_Ptr := Sloc (N);
583 if Present (Call) then
585 Excep_Internal : constant Node_Id := Make_Temporary (Loc, 'V');
586 Export_Pragma : Node_Id;
587 Excep_Alias : Node_Id;
588 Excep_Object : Node_Id;
589 Excep_Image : String_Id;
595 if Present (Interface_Name (Id)) then
596 Excep_Image := Strval (Interface_Name (Id));
598 Get_Name_String (Chars (Id));
600 Excep_Image := String_From_Name_Buffer;
603 Exdata := Component_Associations (Expression (Parent (Id)));
605 if Is_VMS_Exception (Id) then
606 Lang_Char := Next (First (Exdata));
608 -- Change the one-character language designator to 'V'
610 Rewrite (Expression (Lang_Char),
611 Make_Character_Literal (Loc,
613 Char_Literal_Value =>
614 UI_From_Int (Character'Pos ('V'))));
615 Analyze (Expression (Lang_Char));
617 if Exception_Code (Id) /= No_Uint then
619 Make_Integer_Literal (Loc,
620 Intval => Exception_Code (Id));
623 Make_Object_Declaration (Loc,
624 Defining_Identifier => Excep_Internal,
626 New_Reference_To (RTE (RE_Exception_Code), Loc));
628 Insert_Action (N, Excep_Object);
629 Analyze (Excep_Object);
633 (UI_To_Int (Exception_Code (Id)) / 8 * 8);
640 (Make_Pragma_Argument_Association
643 New_Reference_To (Excep_Internal, Loc)),
645 Make_Pragma_Argument_Association
650 Strval => End_String))));
652 Insert_Action (N, Excep_Alias);
653 Analyze (Excep_Alias);
660 (Make_Pragma_Argument_Association (Loc,
661 Expression => Make_Identifier (Loc, Name_C)),
663 Make_Pragma_Argument_Association (Loc,
665 New_Reference_To (Excep_Internal, Loc)),
667 Make_Pragma_Argument_Association (Loc,
669 Make_String_Literal (Loc, Excep_Image)),
671 Make_Pragma_Argument_Association (Loc,
673 Make_String_Literal (Loc, Excep_Image))));
675 Insert_Action (N, Export_Pragma);
676 Analyze (Export_Pragma);
680 Unchecked_Convert_To (RTE (RE_Exception_Code),
681 Make_Function_Call (Loc,
683 New_Reference_To (RTE (RE_Import_Value), Loc),
684 Parameter_Associations => New_List
685 (Make_String_Literal (Loc,
686 Strval => Excep_Image))));
690 Make_Procedure_Call_Statement (Loc,
691 Name => New_Reference_To
692 (RTE (RE_Register_VMS_Exception), Loc),
693 Parameter_Associations => New_List (
695 Unchecked_Convert_To (RTE (RE_Exception_Data_Ptr),
696 Make_Attribute_Reference (Loc,
697 Prefix => New_Occurrence_Of (Id, Loc),
698 Attribute_Name => Name_Unrestricted_Access)))));
700 Analyze_And_Resolve (Code, RTE (RE_Exception_Code));
704 if No (Interface_Name (Id)) then
705 Set_Interface_Name (Id,
708 Strval => Excep_Image));
713 end Expand_Pragma_Import_Export_Exception;
715 ------------------------------------
716 -- Expand_Pragma_Inspection_Point --
717 ------------------------------------
719 -- If no argument is given, then we supply a default argument list that
720 -- includes all objects declared at the source level in all subprograms
721 -- that enclose the inspection point pragma.
723 procedure Expand_Pragma_Inspection_Point (N : Node_Id) is
724 Loc : constant Source_Ptr := Sloc (N);
731 if No (Pragma_Argument_Associations (N)) then
735 while S /= Standard_Standard loop
736 E := First_Entity (S);
737 while Present (E) loop
738 if Comes_From_Source (E)
739 and then Is_Object (E)
740 and then not Is_Entry_Formal (E)
741 and then Ekind (E) /= E_Component
742 and then Ekind (E) /= E_Discriminant
743 and then Ekind (E) /= E_Generic_In_Parameter
744 and then Ekind (E) /= E_Generic_In_Out_Parameter
747 Make_Pragma_Argument_Association (Loc,
748 Expression => New_Occurrence_Of (E, Loc)));
757 Set_Pragma_Argument_Associations (N, A);
760 -- Expand the arguments of the pragma. Expanding an entity reference
761 -- is a noop, except in a protected operation, where a reference may
762 -- have to be transformed into a reference to the corresponding prival.
763 -- Are there other pragmas that may require this ???
765 Assoc := First (Pragma_Argument_Associations (N));
767 while Present (Assoc) loop
768 Expand (Expression (Assoc));
771 end Expand_Pragma_Inspection_Point;
773 --------------------------------------
774 -- Expand_Pragma_Interrupt_Priority --
775 --------------------------------------
777 -- Supply default argument if none exists (System.Interrupt_Priority'Last)
779 procedure Expand_Pragma_Interrupt_Priority (N : Node_Id) is
780 Loc : constant Source_Ptr := Sloc (N);
783 if No (Pragma_Argument_Associations (N)) then
784 Set_Pragma_Argument_Associations (N, New_List (
785 Make_Pragma_Argument_Association (Loc,
787 Make_Attribute_Reference (Loc,
789 New_Occurrence_Of (RTE (RE_Interrupt_Priority), Loc),
790 Attribute_Name => Name_Last))));
792 end Expand_Pragma_Interrupt_Priority;
794 --------------------------------
795 -- Expand_Pragma_Psect_Object --
796 --------------------------------
798 -- Convert to Common_Object, and expand the resulting pragma
800 procedure Expand_Pragma_Psect_Object (N : Node_Id)
801 renames Expand_Pragma_Common_Object;
803 -------------------------------------
804 -- Expand_Pragma_Relative_Deadline --
805 -------------------------------------
807 procedure Expand_Pragma_Relative_Deadline (N : Node_Id) is
808 P : constant Node_Id := Parent (N);
809 Loc : constant Source_Ptr := Sloc (N);
812 -- Expand the pragma only in the case of the main subprogram. For tasks
813 -- the expansion is done in exp_ch9. Generate a call to Set_Deadline
814 -- at Clock plus the relative deadline specified in the pragma. Time
815 -- values are translated into Duration to allow for non-private
816 -- addition operation.
818 if Nkind (P) = N_Subprogram_Body then
821 Make_Procedure_Call_Statement (Loc,
822 Name => New_Reference_To (RTE (RE_Set_Deadline), Loc),
823 Parameter_Associations => New_List (
824 Unchecked_Convert_To (RTE (RO_RT_Time),
827 Make_Function_Call (Loc,
828 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
829 New_List (Make_Function_Call (Loc,
830 New_Reference_To (RTE (RE_Clock), Loc)))),
832 Unchecked_Convert_To (Standard_Duration, Arg1 (N)))))));
836 end Expand_Pragma_Relative_Deadline;